diff --git a/.github/mergeable.yml b/.github/mergeable.yml new file mode 100644 index 0000000000..c4170019c1 --- /dev/null +++ b/.github/mergeable.yml @@ -0,0 +1,18 @@ +mergeable: + pull_requests: + label: + and: + - must_exclude: + regex: '^disposition/DO NOT MERGE' + message: 'Pull request marked not mergeable' + - or: + - and: + - must_include: + regex: 'release notes: yes' + message: 'Please include release note: yes' + - must_include: + regex: '^(c#|c\+\+|cleanup|conformance tests|integration|java|javascript|go|objective-c|php|python|ruby)' + message: 'Please include at least a language label (e.g., c++, java, python). Or apply one of the following labels: cleanup, conformance tests, integration.' + - must_include: + regex: 'release notes: no' + message: 'Please include release note: no' diff --git a/.gitignore b/.gitignore index a8de0e549e..76f6403ea7 100644 --- a/.gitignore +++ b/.gitignore @@ -145,7 +145,9 @@ php/ext/google/protobuf/Makefile.objects php/ext/google/protobuf/acinclude.m4 php/ext/google/protobuf/build/ php/ext/google/protobuf/config.h +php/ext/google/protobuf/config.h.in~ php/ext/google/protobuf/config.nice +php/ext/google/protobuf/configure.ac php/ext/google/protobuf/configure.in php/ext/google/protobuf/mkinstalldirs php/ext/google/protobuf/run-tests.php @@ -171,9 +173,14 @@ js/testproto_libs2.js # ruby test output ruby/lib/ +ruby/tests/basic_test_pb.rb +ruby/tests/basic_test_proto2_pb.rb ruby/tests/generated_code_pb.rb ruby/tests/test_import_pb.rb ruby/tests/test_ruby_package_pb.rb +ruby/tests/generated_code_proto2_pb.rb +ruby/tests/test_import_proto2_pb.rb +ruby/tests/test_ruby_package_proto2_pb.rb ruby/Gemfile.lock ruby/compatibility_tests/v3.0.0/protoc ruby/compatibility_tests/v3.0.0/tests/generated_code_pb.rb @@ -189,3 +196,10 @@ cmake/cmake-build-debug/ # Visual Studio 2017 .vs + +# Visual Studio Code +/.vscode/ + +# IntelliJ +.idea +*.iml diff --git a/BUILD b/BUILD index 934c23ed4e..7b0cd4c7fc 100644 --- a/BUILD +++ b/BUILD @@ -15,46 +15,53 @@ config_setting( }, ) +################################################################################ +# ZLIB configuration +################################################################################ + +ZLIB_DEPS = ["@zlib//:zlib"] + ################################################################################ # Protobuf Runtime Library ################################################################################ MSVC_COPTS = [ "/DHAVE_PTHREAD", - "/wd4018", # -Wno-sign-compare - "/wd4065", # switch statement contains 'default' but no 'case' labels - "/wd4146", # unary minus operator applied to unsigned type, result still unsigned - "/wd4244", # 'conversion' conversion from 'type1' to 'type2', possible loss of data - "/wd4251", # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2' - "/wd4267", # 'var' : conversion from 'size_t' to 'type', possible loss of data - "/wd4305", # 'identifier' : truncation from 'type1' to 'type2' - "/wd4307", # 'operator' : integral constant overflow - "/wd4309", # 'conversion' : truncation of constant value - "/wd4334", # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) - "/wd4355", # 'this' : used in base member initializer list - "/wd4506", # no definition for inline function 'function' - "/wd4514", # -Wno-unused-function - "/wd4800", # 'type' : forcing value to bool 'true' or 'false' (performance warning) - "/wd4996", # The compiler encountered a deprecated declaration. + "/wd4018", # -Wno-sign-compare + "/wd4065", # switch statement contains 'default' but no 'case' labels + "/wd4146", # unary minus operator applied to unsigned type, result still unsigned + "/wd4244", # 'conversion' conversion from 'type1' to 'type2', possible loss of data + "/wd4251", # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2' + "/wd4267", # 'var' : conversion from 'size_t' to 'type', possible loss of data + "/wd4305", # 'identifier' : truncation from 'type1' to 'type2' + "/wd4307", # 'operator' : integral constant overflow + "/wd4309", # 'conversion' : truncation of constant value + "/wd4334", # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) + "/wd4355", # 'this' : used in base member initializer list + "/wd4506", # no definition for inline function 'function' + "/wd4514", # -Wno-unused-function + "/wd4800", # 'type' : forcing value to bool 'true' or 'false' (performance warning) + "/wd4996", # The compiler encountered a deprecated declaration. ] COPTS = select({ - ":msvc" : MSVC_COPTS, + ":msvc": MSVC_COPTS, "//conditions:default": [ "-DHAVE_PTHREAD", - "-Wall", + "-DHAVE_ZLIB", "-Woverloaded-virtual", "-Wno-sign-compare", "-Wno-unused-function", # Prevents ISO C++ const string assignment warnings for pyext sources. - "-Wno-writable-strings", "-Wno-write-strings", ], }) -config_setting( +load(":compiler_config_setting.bzl", "create_compiler_config_setting") + +create_compiler_config_setting( name = "msvc", - values = { "compiler": "msvc-cl" }, + value = "msvc-cl", ) config_setting( @@ -64,14 +71,33 @@ config_setting( }, ) +config_setting( + name = "android-libcpp", + values = { + "crosstool_top": "@androidndk//:toolchain-libcpp", + }, +) + +config_setting( + name = "android-gnu-libstdcpp", + values = { + "crosstool_top": "@androidndk//:toolchain-gnu-libstdcpp", + }, +) + # Android and MSVC builds do not need to link in a separate pthread library. LINK_OPTS = select({ ":android": [], + ":android-libcpp": [], + ":android-gnu-libstdcpp": [], ":msvc": [ # Suppress linker warnings about files with no symbols defined. "-ignore:4221", ], - "//conditions:default": ["-lpthread", "-lm"], + "//conditions:default": [ + "-lpthread", + "-lm", + ], }) load( @@ -87,21 +113,25 @@ cc_library( name = "protobuf_lite", srcs = [ # AUTOGEN(protobuf_lite_srcs) + "src/google/protobuf/any_lite.cc", "src/google/protobuf/arena.cc", - "src/google/protobuf/arenastring.cc", "src/google/protobuf/extension_set.cc", + "src/google/protobuf/generated_enum_util.cc", "src/google/protobuf/generated_message_table_driven_lite.cc", "src/google/protobuf/generated_message_util.cc", "src/google/protobuf/implicit_weak_message.cc", "src/google/protobuf/io/coded_stream.cc", + "src/google/protobuf/io/io_win32.cc", + "src/google/protobuf/io/strtod.cc", "src/google/protobuf/io/zero_copy_stream.cc", + "src/google/protobuf/io/zero_copy_stream_impl.cc", "src/google/protobuf/io/zero_copy_stream_impl_lite.cc", "src/google/protobuf/message_lite.cc", + "src/google/protobuf/parse_context.cc", "src/google/protobuf/repeated_field.cc", "src/google/protobuf/stubs/bytestream.cc", "src/google/protobuf/stubs/common.cc", "src/google/protobuf/stubs/int128.cc", - "src/google/protobuf/stubs/io_win32.cc", "src/google/protobuf/stubs/status.cc", "src/google/protobuf/stubs/statusor.cc", "src/google/protobuf/stubs/stringpiece.cc", @@ -111,13 +141,21 @@ cc_library( "src/google/protobuf/stubs/time.cc", "src/google/protobuf/wire_format_lite.cc", ], - hdrs = glob(["src/google/protobuf/**/*.h", "src/google/protobuf/**/*.inc"]), + hdrs = glob([ + "src/google/protobuf/**/*.h", + "src/google/protobuf/**/*.inc", + ]), copts = COPTS, includes = ["src/"], linkopts = LINK_OPTS, visibility = ["//visibility:public"], ) +PROTOBUF_DEPS = select({ + ":msvc": [], + "//conditions:default": ZLIB_DEPS, +}) + cc_library( name = "protobuf", srcs = [ @@ -139,9 +177,7 @@ cc_library( "src/google/protobuf/generated_message_table_driven.cc", "src/google/protobuf/io/gzip_stream.cc", "src/google/protobuf/io/printer.cc", - "src/google/protobuf/io/strtod.cc", "src/google/protobuf/io/tokenizer.cc", - "src/google/protobuf/io/zero_copy_stream_impl.cc", "src/google/protobuf/map_field.cc", "src/google/protobuf/message.cc", "src/google/protobuf/reflection_ops.cc", @@ -178,12 +214,15 @@ cc_library( "src/google/protobuf/wire_format.cc", "src/google/protobuf/wrappers.pb.cc", ], - hdrs = glob(["src/**/*.h", "src/**/*.inc"]), + hdrs = glob([ + "src/**/*.h", + "src/**/*.inc", + ]), copts = COPTS, includes = ["src/"], linkopts = LINK_OPTS, visibility = ["//visibility:public"], - deps = [":protobuf_lite"], + deps = [":protobuf_lite"] + PROTOBUF_DEPS, ) # This provides just the header files for use in projects that need to build @@ -192,34 +231,44 @@ cc_library( # TODO(keveman): Remove this target once the support gets added to Bazel. cc_library( name = "protobuf_headers", - hdrs = glob(["src/**/*.h"]), + hdrs = glob([ + "src/**/*.h", + "src/**/*.inc", + ]), includes = ["src/"], visibility = ["//visibility:public"], ) -objc_library( - name = "protobuf_objc", - hdrs = ["objectivec/GPBProtocolBuffers.h"], - includes = ["objectivec"], - non_arc_srcs = ["objectivec/GPBProtocolBuffers.m"], - visibility = ["//visibility:public"], -) - # Map of all well known protos. # name => (include path, imports) WELL_KNOWN_PROTO_MAP = { - "any" : ("google/protobuf/any.proto", []), - "api" : ("google/protobuf/api.proto", ["source_context", "type"]), - "compiler_plugin" : ("google/protobuf/compiler/plugin.proto", ["descriptor"]), - "descriptor" : ("google/protobuf/descriptor.proto", []), - "duration" : ("google/protobuf/duration.proto", []), - "empty" : ("google/protobuf/empty.proto", []), - "field_mask" : ("google/protobuf/field_mask.proto", []), - "source_context" : ("google/protobuf/source_context.proto", []), - "struct" : ("google/protobuf/struct.proto", []), - "timestamp" : ("google/protobuf/timestamp.proto", []), - "type" : ("google/protobuf/type.proto", ["any", "source_context"]), - "wrappers" : ("google/protobuf/wrappers.proto", []), + "any": ("google/protobuf/any.proto", []), + "api": ( + "google/protobuf/api.proto", + [ + "source_context", + "type", + ], + ), + "compiler_plugin": ( + "google/protobuf/compiler/plugin.proto", + ["descriptor"], + ), + "descriptor": ("google/protobuf/descriptor.proto", []), + "duration": ("google/protobuf/duration.proto", []), + "empty": ("google/protobuf/empty.proto", []), + "field_mask": ("google/protobuf/field_mask.proto", []), + "source_context": ("google/protobuf/source_context.proto", []), + "struct": ("google/protobuf/struct.proto", []), + "timestamp": ("google/protobuf/timestamp.proto", []), + "type": ( + "google/protobuf/type.proto", + [ + "any", + "source_context", + ], + ), + "wrappers": ("google/protobuf/wrappers.proto", []), } RELATIVE_WELL_KNOWN_PROTOS = [proto[1][0] for proto in WELL_KNOWN_PROTO_MAP.items()] @@ -265,9 +314,9 @@ internal_copied_filegroup( [proto_library( name = proto[0] + "_proto", srcs = [proto[1][0]], - deps = [dep + "_proto" for dep in proto[1][1]], visibility = ["//visibility:public"], - ) for proto in WELL_KNOWN_PROTO_MAP.items()] + deps = [dep + "_proto" for dep in proto[1][1]], +) for proto in WELL_KNOWN_PROTO_MAP.items()] ################################################################################ # Protocol Buffers Compiler @@ -356,7 +405,6 @@ cc_library( "src/google/protobuf/compiler/plugin.pb.cc", "src/google/protobuf/compiler/python/python_generator.cc", "src/google/protobuf/compiler/ruby/ruby_generator.cc", - "src/google/protobuf/compiler/scc.cc", "src/google/protobuf/compiler/subprocess.cc", "src/google/protobuf/compiler/zip_writer.cc", ], @@ -454,7 +502,6 @@ cc_proto_library( COMMON_TEST_SRCS = [ # AUTOGEN(common_test_srcs) "src/google/protobuf/arena_test_util.cc", - "src/google/protobuf/map_test_util.cc", "src/google/protobuf/test_util.cc", "src/google/protobuf/test_util.inc", "src/google/protobuf/testing/file.cc", @@ -478,12 +525,15 @@ cc_binary( cc_test( name = "win32_test", - srcs = ["src/google/protobuf/stubs/io_win32_unittest.cc"], + srcs = ["src/google/protobuf/io/io_win32_unittest.cc"], + tags = [ + "manual", + "windows", + ], deps = [ ":protobuf_lite", "//external:gtest_main", ], - tags = ["manual", "windows"], ) cc_test( @@ -494,7 +544,6 @@ cc_test( "src/google/protobuf/arena_unittest.cc", "src/google/protobuf/arenastring_unittest.cc", "src/google/protobuf/compiler/annotation_test_util.cc", - "src/google/protobuf/compiler/command_line_interface_unittest.cc", "src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc", "src/google/protobuf/compiler/cpp/cpp_move_unittest.cc", "src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc", @@ -518,6 +567,7 @@ cc_test( "src/google/protobuf/extension_set_unittest.cc", "src/google/protobuf/generated_message_reflection_unittest.cc", "src/google/protobuf/io/coded_stream_unittest.cc", + "src/google/protobuf/io/io_win32_unittest.cc", "src/google/protobuf/io/printer_unittest.cc", "src/google/protobuf/io/tokenizer_unittest.cc", "src/google/protobuf/io/zero_copy_stream_unittest.cc", @@ -537,7 +587,6 @@ cc_test( "src/google/protobuf/stubs/bytestream_unittest.cc", "src/google/protobuf/stubs/common_unittest.cc", "src/google/protobuf/stubs/int128_unittest.cc", - "src/google/protobuf/stubs/io_win32_unittest.cc", "src/google/protobuf/stubs/status_test.cc", "src/google/protobuf/stubs/statusor_test.cc", "src/google/protobuf/stubs/stringpiece_unittest.cc", @@ -563,7 +612,13 @@ cc_test( "src/google/protobuf/util/type_resolver_util_test.cc", "src/google/protobuf/well_known_types_unittest.cc", "src/google/protobuf/wire_format_unittest.cc", - ], + ] + select({ + "//conditions:default": [ + # Doesn't pass on Windows with MSVC + "src/google/protobuf/compiler/command_line_interface_unittest.cc", + ], + ":msvc": [], + }), copts = COPTS, data = [ ":test_plugin", @@ -582,7 +637,7 @@ cc_test( ":protobuf", ":protoc_lib", "//external:gtest_main", - ], + ] + PROTOBUF_DEPS, ) ################################################################################ @@ -600,8 +655,112 @@ java_library( ":gen_well_known_protos_java", ], javacopts = select({ - "//:jdk9": ["--add-modules=jdk.unsupported"], - "//conditions:default": ["-source 7", "-target 7"], + "//:jdk9": ["--add-modules=jdk.unsupported"], + "//conditions:default": [ + "-source 7", + "-target 7", + ], + }), + visibility = ["//visibility:public"], +) + +java_library( + name = "protobuf_javalite", + srcs = [ + # Keep in sync with java/lite/pom.xml + "java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java", + "java/core/src/main/java/com/google/protobuf/AbstractParser.java", + "java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java", + "java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java", + "java/core/src/main/java/com/google/protobuf/Android.java", + "java/core/src/main/java/com/google/protobuf/ArrayDecoders.java", + "java/core/src/main/java/com/google/protobuf/BinaryReader.java", + "java/core/src/main/java/com/google/protobuf/BinaryWriter.java", + "java/core/src/main/java/com/google/protobuf/BooleanArrayList.java", + "java/core/src/main/java/com/google/protobuf/BufferAllocator.java", + "java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java", + "java/core/src/main/java/com/google/protobuf/ByteOutput.java", + "java/core/src/main/java/com/google/protobuf/ByteString.java", + "java/core/src/main/java/com/google/protobuf/CodedInputStream.java", + "java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java", + "java/core/src/main/java/com/google/protobuf/CodedOutputStream.java", + "java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java", + "java/core/src/main/java/com/google/protobuf/DoubleArrayList.java", + "java/core/src/main/java/com/google/protobuf/ExperimentalApi.java", + "java/core/src/main/java/com/google/protobuf/ExtensionLite.java", + "java/core/src/main/java/com/google/protobuf/ExtensionRegistryFactory.java", + "java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java", + "java/core/src/main/java/com/google/protobuf/ExtensionSchema.java", + "java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java", + "java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java", + "java/core/src/main/java/com/google/protobuf/FieldInfo.java", + "java/core/src/main/java/com/google/protobuf/FieldSet.java", + "java/core/src/main/java/com/google/protobuf/FieldType.java", + "java/core/src/main/java/com/google/protobuf/FloatArrayList.java", + "java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java", + "java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java", + "java/core/src/main/java/com/google/protobuf/IntArrayList.java", + "java/core/src/main/java/com/google/protobuf/Internal.java", + "java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java", + "java/core/src/main/java/com/google/protobuf/IterableByteBufferInputStream.java", + "java/core/src/main/java/com/google/protobuf/JavaType.java", + "java/core/src/main/java/com/google/protobuf/LazyField.java", + "java/core/src/main/java/com/google/protobuf/LazyFieldLite.java", + "java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java", + "java/core/src/main/java/com/google/protobuf/LazyStringList.java", + "java/core/src/main/java/com/google/protobuf/ListFieldSchema.java", + "java/core/src/main/java/com/google/protobuf/LongArrayList.java", + "java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java", + "java/core/src/main/java/com/google/protobuf/MapEntryLite.java", + "java/core/src/main/java/com/google/protobuf/MapFieldLite.java", + "java/core/src/main/java/com/google/protobuf/MapFieldSchema.java", + "java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java", + "java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java", + "java/core/src/main/java/com/google/protobuf/MessageInfo.java", + "java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java", + "java/core/src/main/java/com/google/protobuf/MessageLite.java", + "java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java", + "java/core/src/main/java/com/google/protobuf/MessageLiteToString.java", + "java/core/src/main/java/com/google/protobuf/MessageSchema.java", + "java/core/src/main/java/com/google/protobuf/MessageSetSchema.java", + "java/core/src/main/java/com/google/protobuf/MutabilityOracle.java", + "java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java", + "java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java", + "java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java", + "java/core/src/main/java/com/google/protobuf/NioByteString.java", + "java/core/src/main/java/com/google/protobuf/OneofInfo.java", + "java/core/src/main/java/com/google/protobuf/Parser.java", + "java/core/src/main/java/com/google/protobuf/PrimitiveNonBoxingCollection.java", + "java/core/src/main/java/com/google/protobuf/ProtoSyntax.java", + "java/core/src/main/java/com/google/protobuf/Protobuf.java", + "java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java", + "java/core/src/main/java/com/google/protobuf/ProtobufLists.java", + "java/core/src/main/java/com/google/protobuf/ProtocolStringList.java", + "java/core/src/main/java/com/google/protobuf/RawMessageInfo.java", + "java/core/src/main/java/com/google/protobuf/Reader.java", + "java/core/src/main/java/com/google/protobuf/RopeByteString.java", + "java/core/src/main/java/com/google/protobuf/Schema.java", + "java/core/src/main/java/com/google/protobuf/SchemaFactory.java", + "java/core/src/main/java/com/google/protobuf/SchemaUtil.java", + "java/core/src/main/java/com/google/protobuf/SmallSortedMap.java", + "java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java", + "java/core/src/main/java/com/google/protobuf/TextFormatEscaper.java", + "java/core/src/main/java/com/google/protobuf/UninitializedMessageException.java", + "java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java", + "java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java", + "java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java", + "java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java", + "java/core/src/main/java/com/google/protobuf/UnsafeUtil.java", + "java/core/src/main/java/com/google/protobuf/Utf8.java", + "java/core/src/main/java/com/google/protobuf/WireFormat.java", + "java/core/src/main/java/com/google/protobuf/Writer.java", + ], + javacopts = select({ + "//:jdk9": ["--add-modules=jdk.unsupported"], + "//conditions:default": [ + "-source 7", + "-target 7", + ], }), visibility = ["//visibility:public"], ) @@ -611,10 +770,14 @@ java_library( srcs = glob([ "java/util/src/main/java/com/google/protobuf/util/*.java", ]), - javacopts = ["-source 7", "-target 7"], + javacopts = [ + "-source 7", + "-target 7", + ], visibility = ["//visibility:public"], deps = [ "protobuf_java", + "//external:error_prone_annotations", "//external:gson", "//external:guava", ], @@ -727,11 +890,11 @@ py_proto_library( }), default_runtime = "", protoc = ":protoc", + py_extra_srcs = glob(["python/**/__init__.py"]), py_libs = [ ":python_srcs", "//external:six", ], - py_extra_srcs = glob(["python/**/__init__.py"]), srcs_version = "PY2AND3", visibility = ["//visibility:public"], ) @@ -823,18 +986,18 @@ internal_protobuf_py_tests( cc_library( name = "proto_api", hdrs = ["python/google/protobuf/proto_api.h"], + visibility = ["//visibility:public"], deps = [ "//external:python_headers", ], - visibility = ["//visibility:public"], ) proto_lang_toolchain( name = "cc_toolchain", + blacklisted_protos = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()], command_line = "--cpp_out=$(OUT)", runtime = ":protobuf", visibility = ["//visibility:public"], - blacklisted_protos = [":_internal_wkt_protos_genrule"], ) proto_lang_toolchain( @@ -844,87 +1007,88 @@ proto_lang_toolchain( visibility = ["//visibility:public"], ) -OBJC_HDRS = [ - "objectivec/GPBArray.h", - "objectivec/GPBBootstrap.h", - "objectivec/GPBCodedInputStream.h", - "objectivec/GPBCodedOutputStream.h", - "objectivec/GPBDescriptor.h", - "objectivec/GPBDictionary.h", - "objectivec/GPBExtensionInternals.h", - "objectivec/GPBExtensionRegistry.h", - "objectivec/GPBMessage.h", - "objectivec/GPBProtocolBuffers.h", - "objectivec/GPBProtocolBuffers_RuntimeSupport.h", - "objectivec/GPBRootObject.h", - "objectivec/GPBRuntimeTypes.h", - "objectivec/GPBUnknownField.h", - "objectivec/GPBUnknownFieldSet.h", - "objectivec/GPBUtilities.h", - "objectivec/GPBWellKnownTypes.h", - "objectivec/GPBWireFormat.h", - "objectivec/google/protobuf/Any.pbobjc.h", - "objectivec/google/protobuf/Api.pbobjc.h", - "objectivec/google/protobuf/Duration.pbobjc.h", - "objectivec/google/protobuf/Empty.pbobjc.h", - "objectivec/google/protobuf/FieldMask.pbobjc.h", - "objectivec/google/protobuf/SourceContext.pbobjc.h", - "objectivec/google/protobuf/Struct.pbobjc.h", - "objectivec/google/protobuf/Timestamp.pbobjc.h", - "objectivec/google/protobuf/Type.pbobjc.h", - "objectivec/google/protobuf/Wrappers.pbobjc.h", -] - -OBJC_PRIVATE_HDRS = [ - "objectivec/GPBArray_PackagePrivate.h", - "objectivec/GPBCodedInputStream_PackagePrivate.h", - "objectivec/GPBCodedOutputStream_PackagePrivate.h", - "objectivec/GPBDescriptor_PackagePrivate.h", - "objectivec/GPBDictionary_PackagePrivate.h", - "objectivec/GPBMessage_PackagePrivate.h", - "objectivec/GPBRootObject_PackagePrivate.h", - "objectivec/GPBUnknownFieldSet_PackagePrivate.h", - "objectivec/GPBUnknownField_PackagePrivate.h", - "objectivec/GPBUtilities_PackagePrivate.h", -] - -OBJC_SRCS = [ - "objectivec/GPBArray.m", - "objectivec/GPBCodedInputStream.m", - "objectivec/GPBCodedOutputStream.m", - "objectivec/GPBDescriptor.m", - "objectivec/GPBDictionary.m", - "objectivec/GPBExtensionInternals.m", - "objectivec/GPBExtensionRegistry.m", - "objectivec/GPBMessage.m", - "objectivec/GPBRootObject.m", - "objectivec/GPBUnknownField.m", - "objectivec/GPBUnknownFieldSet.m", - "objectivec/GPBUtilities.m", - "objectivec/GPBWellKnownTypes.m", - "objectivec/GPBWireFormat.m", - "objectivec/google/protobuf/Any.pbobjc.m", - "objectivec/google/protobuf/Api.pbobjc.m", - "objectivec/google/protobuf/Duration.pbobjc.m", - "objectivec/google/protobuf/Empty.pbobjc.m", - "objectivec/google/protobuf/FieldMask.pbobjc.m", - "objectivec/google/protobuf/SourceContext.pbobjc.m", - "objectivec/google/protobuf/Struct.pbobjc.m", - "objectivec/google/protobuf/Timestamp.pbobjc.m", - "objectivec/google/protobuf/Type.pbobjc.m", - "objectivec/google/protobuf/Wrappers.pbobjc.m", -] +alias( + name = "objectivec", + actual = ":protobuf_objc", + visibility = ["//visibility:public"], +) objc_library( - name = "objectivec", - hdrs = OBJC_HDRS + OBJC_PRIVATE_HDRS, + name = "protobuf_objc", + hdrs = [ + "objectivec/GPBArray.h", + "objectivec/GPBBootstrap.h", + "objectivec/GPBCodedInputStream.h", + "objectivec/GPBCodedOutputStream.h", + "objectivec/GPBDescriptor.h", + "objectivec/GPBDictionary.h", + "objectivec/GPBExtensionInternals.h", + "objectivec/GPBExtensionRegistry.h", + "objectivec/GPBMessage.h", + "objectivec/GPBProtocolBuffers.h", + "objectivec/GPBProtocolBuffers_RuntimeSupport.h", + "objectivec/GPBRootObject.h", + "objectivec/GPBRuntimeTypes.h", + "objectivec/GPBUnknownField.h", + "objectivec/GPBUnknownFieldSet.h", + "objectivec/GPBUtilities.h", + "objectivec/GPBWellKnownTypes.h", + "objectivec/GPBWireFormat.h", + "objectivec/google/protobuf/Any.pbobjc.h", + "objectivec/google/protobuf/Api.pbobjc.h", + "objectivec/google/protobuf/Duration.pbobjc.h", + "objectivec/google/protobuf/Empty.pbobjc.h", + "objectivec/google/protobuf/FieldMask.pbobjc.h", + "objectivec/google/protobuf/SourceContext.pbobjc.h", + "objectivec/google/protobuf/Struct.pbobjc.h", + "objectivec/google/protobuf/Timestamp.pbobjc.h", + "objectivec/google/protobuf/Type.pbobjc.h", + "objectivec/google/protobuf/Wrappers.pbobjc.h", + # Package private headers, but exposed because the generated sources + # need to use them. + "objectivec/GPBArray_PackagePrivate.h", + "objectivec/GPBCodedInputStream_PackagePrivate.h", + "objectivec/GPBCodedOutputStream_PackagePrivate.h", + "objectivec/GPBDescriptor_PackagePrivate.h", + "objectivec/GPBDictionary_PackagePrivate.h", + "objectivec/GPBMessage_PackagePrivate.h", + "objectivec/GPBRootObject_PackagePrivate.h", + "objectivec/GPBUnknownFieldSet_PackagePrivate.h", + "objectivec/GPBUnknownField_PackagePrivate.h", + "objectivec/GPBUtilities_PackagePrivate.h", + ], copts = [ "-Wno-vla", ], includes = [ "objectivec", ], - non_arc_srcs = OBJC_SRCS, + non_arc_srcs = [ + "objectivec/GPBArray.m", + "objectivec/GPBCodedInputStream.m", + "objectivec/GPBCodedOutputStream.m", + "objectivec/GPBDescriptor.m", + "objectivec/GPBDictionary.m", + "objectivec/GPBExtensionInternals.m", + "objectivec/GPBExtensionRegistry.m", + "objectivec/GPBMessage.m", + "objectivec/GPBRootObject.m", + "objectivec/GPBUnknownField.m", + "objectivec/GPBUnknownFieldSet.m", + "objectivec/GPBUtilities.m", + "objectivec/GPBWellKnownTypes.m", + "objectivec/GPBWireFormat.m", + "objectivec/google/protobuf/Any.pbobjc.m", + "objectivec/google/protobuf/Api.pbobjc.m", + "objectivec/google/protobuf/Duration.pbobjc.m", + "objectivec/google/protobuf/Empty.pbobjc.m", + "objectivec/google/protobuf/FieldMask.pbobjc.m", + "objectivec/google/protobuf/SourceContext.pbobjc.m", + "objectivec/google/protobuf/Struct.pbobjc.m", + "objectivec/google/protobuf/Timestamp.pbobjc.m", + "objectivec/google/protobuf/Type.pbobjc.m", + "objectivec/google/protobuf/Wrappers.pbobjc.m", + ], visibility = ["//visibility:public"], ) @@ -936,23 +1100,128 @@ genrule( name = "generated_protos", srcs = ["src/google/protobuf/unittest_import.proto"], outs = ["unittest_gen.proto"], - cmd = "cat $(SRCS) | sed 's|google/|src/google/|' > $(OUTS)" + cmd = "cat $(SRCS) | sed 's|google/|src/google/|' > $(OUTS)", ) proto_library( name = "generated_protos_proto", srcs = [ - "unittest_gen.proto", "src/google/protobuf/unittest_import_public.proto", + "unittest_gen.proto", ], ) py_proto_library( name = "generated_protos_py", srcs = [ - "unittest_gen.proto", "src/google/protobuf/unittest_import_public.proto", + "unittest_gen.proto", ], default_runtime = "", protoc = ":protoc", ) + +################################################################################ +# Conformance tests +################################################################################ + +proto_library( + name = "test_messages_proto2_proto", + srcs = ["src/google/protobuf/test_messages_proto2.proto"], + visibility = ["//visibility:public"], +) + +proto_library( + name = "test_messages_proto3_proto", + srcs = ["src/google/protobuf/test_messages_proto3.proto"], + visibility = ["//visibility:public"], + deps = [ + ":any_proto", + ":duration_proto", + ":field_mask_proto", + ":struct_proto", + ":timestamp_proto", + ":wrappers_proto", + ], +) + +cc_proto_library( + name = "test_messages_proto2_proto_cc", + srcs = ["src/google/protobuf/test_messages_proto2.proto"], +) + +cc_proto_library( + name = "test_messages_proto3_proto_cc", + srcs = ["src/google/protobuf/test_messages_proto3.proto"], + deps = [ + ":cc_wkt_protos", + ], +) + +proto_library( + name = "conformance_proto", + srcs = ["conformance/conformance.proto"], + visibility = ["//visibility:public"], +) + +cc_proto_library( + name = "conformance_proto_cc", + srcs = ["conformance/conformance.proto"], +) + +cc_library( + name = "jsoncpp", + srcs = ["conformance/third_party/jsoncpp/jsoncpp.cpp"], + hdrs = ["conformance/third_party/jsoncpp/json.h"], + includes = ["conformance"], +) + +cc_library( + name = "conformance_test", + srcs = [ + "conformance/conformance_test.cc", + "conformance/conformance_test_runner.cc", + ], + hdrs = [ + "conformance/conformance_test.h", + ], + includes = [ + "conformance", + "src", + ], + deps = [":conformance_proto_cc"], +) + +cc_library( + name = "binary_json_conformance_suite", + srcs = ["conformance/binary_json_conformance_suite.cc"], + hdrs = ["conformance/binary_json_conformance_suite.h"], + deps = [ + ":conformance_test", + ":jsoncpp", + ":test_messages_proto2_proto_cc", + ":test_messages_proto3_proto_cc", + ], +) + +cc_library( + name = "text_format_conformance_suite", + srcs = ["conformance/text_format_conformance_suite.cc"], + hdrs = ["conformance/text_format_conformance_suite.h"], + deps = [ + ":conformance_test", + ":test_messages_proto2_proto_cc", + ":test_messages_proto3_proto_cc", + ], +) + +cc_binary( + name = "conformance_test_runner", + srcs = ["conformance/conformance_test_main.cc"], + visibility = ["//visibility:public"], + deps = [ + ":binary_json_conformance_suite", + ":conformance_test", + ":text_format_conformance_suite", + ], +) diff --git a/CHANGES.txt b/CHANGES.txt index a381084af7..72ee37175f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,140 @@ +2019-04-29 version 3.8.0 (C++/Java/Python/PHP/Objective-C/C#/Ruby/JavaScript) + + C++ + * Use std::atomic in case of myriad2 platform + * Always declare enums to be int-sized + * Added DebugString() and ShortDebugString() methods on MessageLite + * Specialized different parse loop control flows + * Make hasbits potentially in register. The or's start forming an obstacle because it's a read modify store on the same mem address on each iteration. + * Move to an internal MACRO for parser validity checks. + * Improve map parsing performance. + * Make MergePartialFromCodedStream non virtual. This allows direct calls, potential inlining and is also a code health improvement + * Add an overall limit to parse_context to prevent reading past it. This allows to remove a annoying level of indirection. + * Fix a mistake, we shouldn't verify map key/value strings for utf8 in opt mode for proto2. + * Further improvements to cut binary size. + * Prepare to make MergePartialFromCodedStream non-virtual. + * A report on some interesting behavior change in python (caused by b/27494216) made me realize there is a check that needs to be done in case the parse ended on a end group tag. + * Add a note of caution to the comments around skip in CodedOutputStream. + * Simplify end check. + * Add overload for ParseMessage for MessageLite/Message types. If the explicit type is not known inlining won't help de-virtualizing the virtual call. + * Reduce linker input. It turns out that ParseMessage is not inlined, producing template instantiations that are used only once and save nothing but cost more. + * Improve the parser. + * [c++17] Changed proto2::RepeatedPtrField iterators to no longer derive from the deprecated std::iterator class. + * Change the default value of case_insensitive_enum_parsing to false for JsonStringToMessage. + * Add a warning if a field name doesn't match the style guide. + * Fix TextFormat not round-trip correctly when float value is max float. + * Added locationed info for some errors at compiler + * Python reserved keywords are now working with getattr()/setattr() for most descriptors. + * Added AllowUnknownField() in text_format + * Append '_' to C++ reserved keywords for message, enum, extension + * Fix MSVC warning C4244 in protobuf's parse_context.h. + * Updating Iterators to be compatible with C++17 in MSVC. + * Use capability annotation in mutex.h + * Fix "UndefinedBehaviorSanitizer: cfi-bad-type" + * CriticalSectionLock class as a lightweight replacement for std::mutex on Windows platforms. + * Removed vestigial wire_format_lite_inl.h + + C# + * Added System.Memory dependency. + + Java + * Make Java protoc code generator ignore optimize_for LITE_RUNTIME. Users should instead use the Java lite protoc plugin. + * Change Extension getMessageDefaultInstance() to return Message instead of MessageLite. + * Prevent malicious input streams from leaking buffers for ByteString or ByteBuffer parsing. + * Release new Javalite runtime. + * Show warning in case potential file name conflict. + * Allow Java reserved keywords to be used in extensions. + * Added setAllowUnknownFields() in text format + * Add memoization to ExtensionRegistryLite.getEmptyRegistry() + * Improve performance of CodedOutputStream.writeUInt32NoTag + * Add an optimized mismatch-finding algorithm to UnsafeUtil. + * When serializing uint32 varints, check that we have MAX_VARINT32_SIZE bytes left, not just MAX_VARINT_SIZE. + * Minor optimization to RopeByteString.PieceIterator + + JavaScript + * Simplify generated toObject code when the default value is used. + + Python + * Changes implementation of Name() for enums that allow aliases in proto2 in Python to be in line with claims in C++ implementation (to return first value). + * Added double_format option in text format printer. + * Added iter and __contains__ to extension dict + * Added allow_unknown_field option in python text format parser + * Fixed Timestamp.ToDatetime() loses precision issue + * Support unknown field in text format printer. + * Float field will be convert to inf if bigger than struct.unpack('f', b'\xff\xff\x7f\x7f')[0] which is about 3.4028234664e+38, + convert to -inf if smaller than -3.4028234664e+38 + * Allowed casting str->bytes in Message.__setstate__ + + Ruby + * Helper methods to get enum name for Ruby. + + +2019-01-24 version 3.7.0 (C++/Java/Python/PHP/Objective-C/C#/Ruby/JavaScript) + + C++ + * Introduced new MOMI (maybe-outside-memory-interval) parser. + * Add an option to json_util to parse enum as case-insensitive. In the future, enum parsing in json_util will become case-sensitive. + * Added conformance test for enum aliases + * Added support for --cpp_out=speed:... + * Added use of C++ override keyword where appropriate + * Many other cleanups and fixes. + + Java + * Fix illegal reflective access warning in JDK 9+ + * Add BOM + + Python + * Added Python 3.7 compatibility. + * Modified ParseFromString to return bytes parsed . + * Introduce Proto C API. + * FindFileContainingSymbol in descriptor pool is now able to find field and enum values. + * reflection.MakeClass() and reflection.ParseMessage() are deprecated. + * Added DescriptorPool.FindMethodByName() method in pure python (c extension alreay has it) + * Flipped proto3 to preserve unknown fields by default. + * Added support for memoryview in python3 proto message parsing. + * Added MergeFrom for repeated scalar fields in c extension (pure python already has it) + * Surrogates are now rejected at setters in python3. + * Added public unknown field API. + * RecursionLimit is also set to max if allow_oversize_protos is enabled. + * Disallow duplicate scalars in proto3 text_format parse. + * Fix some segment faults for c extension map field. + + PHP + * Most issues for json encoding/decoding in the c extension have been fixed. There are still some edge cases not fixed. For more details, check conformance/failure_list_php_c.txt. + * Supports php 7.3 + * Added helper methods to convert between enum values and names. + * Allow setting/getting wrapper message fields using primitive values. + * Various bug fixes. + + Ruby + * Ruby 2.6 support. + * Drops support for ruby < 2.3. + * Most issues for json encoding/decoding in the c extension have been fixed. There are still some edge cases not fixed. For more details, check conformance/failure_list_ruby.txt. + * Json parsing can specify an option to ignore unknown fields: msg.decode_json(data, {ignore_unknown_fields: true}). + * Added support for proto2 syntax (partially). + * Various bug fixes. + + Csharp + * More support for FieldMask include merge, intersect and more. + * Increasing the default recursion limit to 100. + * Support loading FileDescriptors dynamically. + * Provide access to comments from descriptors. + * Added Any.Is method. + * Compatible with C# 6 + * Added IComparable and comparison operators on Timestamp. + + Objective C + * Add ability to introspect list of enum values (#4678) + * Copy the value when setting message/data fields (#5215) + * Support suppressing the objc package prefix checks on a list of files (#5309) + * More complete keyword and NSObject method (via categories) checks for field names, can result in more fields being rename, but avoids the collisions at runtime (#5289) + * Small fixes to TextFormat generation for extensions (#5362) + * Provide more details/context in deprecation messages (#5412) + * Array/Dictionary enumeration blocks NS_NOESCAPE annotation for Swift (#5421) + * Properly annotate extensions for ARC when their names imply behaviors (#5427) + * Enum alias name collision improvements (#5480) + + 2018-07-27 version 3.6.1 (C++/Java/Python/PHP/Objective-C/C#/Ruby/JavaScript) C++ @@ -102,7 +239,7 @@ Planned Future Changes * Make C++ implementation C++11 only: we plan to require C++11 to build protobuf code starting from 3.6.0 release. Please join this github issue: - https://github.com/google/protobuf/issues/2780 to provide your feedback. + https://github.com/protocolbuffers/protobuf/issues/2780 to provide your feedback. protoc * Fixed a bug introduced in 3.5.0 and protoc in Windows now accepts non-ascii @@ -133,7 +270,7 @@ Planned Future Changes * Make C++ implementation C++11 only: we plan to require C++11 to build protobuf code starting from 3.6.0 release. Please join this github issue: - https://github.com/google/protobuf/issues/2780 to provide your feedback. + https://github.com/protocolbuffers/protobuf/issues/2780 to provide your feedback. General * Unknown fields are now preserved in proto3 for most of the language @@ -220,14 +357,14 @@ For issues and discussions: - https://github.com/google/protobuf/issues/272 + https://github.com/protocolbuffers/protobuf/issues/272 - Make C++ implementation C++11 only: we plan to require C++11 to build protobuf code starting from 3.5.0 or 3.6.0 release, after unknown fields semantic changes are finished. Please join this github issue: - https://github.com/google/protobuf/issues/2780 + https://github.com/protocolbuffers/protobuf/issues/2780 to provide your feedback. @@ -347,14 +484,14 @@ for the timeline and follow up this github issue: - https://github.com/google/protobuf/issues/272 + https://github.com/protocolbuffers/protobuf/issues/272 for discussion. - Make C++ implementation C++11 only: we plan to require C++11 to build protobuf code starting from 3.4.0 or 3.5.0 release. Please join this github issue: - https://github.com/google/protobuf/issues/2780 + https://github.com/protocolbuffers/protobuf/issues/2780 to provide your feedback. @@ -581,7 +718,7 @@ details about APIs. Objective-C - * Helpers are now provided for working the the Any well known type (see + * Helpers are now provided for working the Any well known type (see GPBWellKnownTypes.h for the api additions). * Some improvements in startup code (especially when extensions aren’t used). diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..db1ff31cac --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,98 @@ +# Contributing to Protocol Buffers + +We welcome your contributions to protocol buffers. This doc describes the +process to contribute patches to protobuf and the general guidelines we +expect contributors to follow. + +## Before You Start + +We accept patches in the form of github pull requests. If you are new to +github, please read [How to create github pull requests](https://help.github.com/articles/about-pull-requests/) +first. + +### Contributor License Agreements + +Contributions to this project must be accompanied by a Contributor License +Agreement. You (or your employer) retain the copyright to your contribution, +this simply gives us permission to use and redistribute your contributions +as part of the project. + +* If you are an individual writing original source code and you're sure you + own the intellectual property, then you'll need to sign an [individual CLA](https://cla.developers.google.com/about/google-individual?csw=1). +* If you work for a company that wants to allow you to contribute your work, + then you'll need to sign a [corporate CLA](https://cla.developers.google.com/about/google-corporate?csw=1). + +### Coding Style + +This project follows [Google’s Coding Style Guides](https://github.com/google/styleguide). +Before sending out your pull request, please familiarize yourself with the +corresponding style guides and make sure the proposed code change is style +conforming. + +## Contributing Process + +Most pull requests should go to the master branch and the change will be +included in the next major/minor version release (e.g., 3.6.0 release). If you +need to include a bug fix in a patch release (e.g., 3.5.2), make sure it’s +already merged to master, and then create a pull request cherry-picking the +commits from master branch to the release branch (e.g., branch 3.5.x). + +For each pull request, a protobuf team member will be assigned to review the +pull request. For minor cleanups, the pull request may be merged right away +after an initial review. For larger changes, you will likely receive multiple +rounds of comments and it may take some time to complete. We will try to keep +our response time within 7-days but if you don’t get any response in a few +days, feel free to comment on the threads to get our attention. We also expect +you to respond to our comments within a reasonable amount of time. If we don’t +hear from you for 2 weeks or longer, we may close the pull request. You can +still send the pull request again once you have time to work on it. + +Once a pull request is merged, we will take care of the rest and get it into +the final release. + +## Pull Request Guidelines + +* If you are a Googler, it is preferable to first create an internal CL and + have it reviewed and submitted. The code propagation process will deliver the + change to GitHub. +* Create small PRs that are narrowly focused on addressing a single concern. + We often receive PRs that are trying to fix several things at a time, but if + only one fix is considered acceptable, nothing gets merged and both author's + & review's time is wasted. Create more PRs to address different concerns and + everyone will be happy. +* For speculative changes, consider opening an issue and discussing it first. + If you are suggesting a behavioral or API change, make sure you get explicit + support from a protobuf team member before sending us the pull request. +* Provide a good PR description as a record of what change is being made and + why it was made. Link to a GitHub issue if it exists. +* Don't fix code style and formatting unless you are already changing that + line to address an issue. PRs with irrelevant changes won't be merged. If + you do want to fix formatting or style, do that in a separate PR. +* Unless your PR is trivial, you should expect there will be reviewer comments + that you'll need to address before merging. We expect you to be reasonably + responsive to those comments, otherwise the PR will be closed after 2-3 weeks + of inactivity. +* Maintain clean commit history and use meaningful commit messages. PRs with + messy commit history are difficult to review and won't be merged. Use rebase + -i upstream/master to curate your commit history and/or to bring in latest + changes from master (but avoid rebasing in the middle of a code review). +* Keep your PR up to date with upstream/master (if there are merge conflicts, + we can't really merge your change). +* All tests need to be passing before your change can be merged. We recommend + you run tests locally before creating your PR to catch breakages early on. + Ultimately, the green signal will be provided by our testing infrastructure. + The reviewer will help you if there are test failures that seem not related + to the change you are making. + +## Reviewer Guidelines + +* Make sure that all tests are passing before approval. +* Apply the "release notes: yes" label if the pull request's description should + be included in the next release (e.g., any new feature / bug fix). + Apply the "release notes: no" label if the pull request's description should + not be included in the next release (e.g., refactoring changes that does not + change behavior, integration from Google internal, updating tests, etc.). +* Apply the appropriate language label (e.g., C++, Java, Python, etc.) to the + pull request. This will make it easier to identify which languages the pull + request affects, allowing us to better identify appropriate reviewer, create + a better release note, and make it easier to identify issues in the future. diff --git a/Makefile.am b/Makefile.am index 6694dffef1..c03b45c78a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -49,6 +49,7 @@ pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = protobuf.pc protobuf-lite.pc csharp_EXTRA_DIST= \ + global.json \ csharp/.gitignore \ csharp/CHANGES.txt \ csharp/Google.Protobuf.Tools.targets \ @@ -56,9 +57,10 @@ csharp_EXTRA_DIST= \ csharp/README.md \ csharp/build_packages.bat \ csharp/build_tools.sh \ + csharp/buildall.bat \ csharp/buildall.sh \ csharp/generate_protos.sh \ - csharp/global.json \ + csharp/install_dotnet_sdk.ps1 \ csharp/keys/Google.Protobuf.public.snk \ csharp/keys/Google.Protobuf.snk \ csharp/keys/README.md \ @@ -75,6 +77,12 @@ csharp_EXTRA_DIST= \ csharp/src/AddressBook/ListPeople.cs \ csharp/src/AddressBook/Program.cs \ csharp/src/AddressBook/SampleUsage.cs \ + csharp/src/Google.Protobuf.Benchmarks/SerializationConfig.cs \ + csharp/src/Google.Protobuf.Benchmarks/SerializationBenchmark.cs \ + csharp/src/Google.Protobuf.Benchmarks/Program.cs \ + csharp/src/Google.Protobuf.Benchmarks/Google.Protobuf.Benchmarks.csproj \ + csharp/src/Google.Protobuf.Benchmarks/Benchmarks.cs \ + csharp/src/Google.Protobuf.Benchmarks/BenchmarkMessage1Proto3.cs \ csharp/src/Google.Protobuf.Conformance/Conformance.cs \ csharp/src/Google.Protobuf.Conformance/Google.Protobuf.Conformance.csproj \ csharp/src/Google.Protobuf.Conformance/Program.cs \ @@ -93,6 +101,7 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf.Test/DeprecatedMemberTest.cs \ csharp/src/Google.Protobuf.Test/EqualityTester.cs \ csharp/src/Google.Protobuf.Test/FieldCodecTest.cs \ + csharp/src/Google.Protobuf.Test/FieldMaskTreeTest.cs \ csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs \ csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj \ csharp/src/Google.Protobuf.Test/IssuesTest.cs \ @@ -100,6 +109,7 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf.Test/JsonParserTest.cs \ csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs \ csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs \ + csharp/src/Google.Protobuf.Test/Reflection/DescriptorDeclarationTest.cs \ csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs \ csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs \ csharp/src/Google.Protobuf.Test/Reflection/TypeRegistryTest.cs \ @@ -122,6 +132,7 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs \ csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs \ csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs \ + csharp/src/Google.Protobuf.Test/testprotos.pb \ csharp/src/Google.Protobuf.sln \ csharp/src/Google.Protobuf/ByteArray.cs \ csharp/src/Google.Protobuf/ByteString.cs \ @@ -137,11 +148,17 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs \ csharp/src/Google.Protobuf/Compatibility/StreamExtensions.cs \ csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs \ + csharp/src/Google.Protobuf/Extension.cs \ + csharp/src/Google.Protobuf/ExtensionRegistry.cs \ + csharp/src/Google.Protobuf/ExtensionSet.cs \ + csharp/src/Google.Protobuf/ExtensionValue.cs \ csharp/src/Google.Protobuf/FieldCodec.cs \ + csharp/src/Google.Protobuf/FieldMaskTree.cs \ csharp/src/Google.Protobuf/FrameworkPortability.cs \ csharp/src/Google.Protobuf/Google.Protobuf.csproj \ csharp/src/Google.Protobuf/ICustomDiagnosticMessage.cs \ csharp/src/Google.Protobuf/IDeepCloneable.cs \ + csharp/src/Google.Protobuf/IExtendableMessage.cs \ csharp/src/Google.Protobuf/IMessage.cs \ csharp/src/Google.Protobuf/InvalidJsonException.cs \ csharp/src/Google.Protobuf/InvalidProtocolBufferException.cs \ @@ -152,16 +169,20 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf/LimitedInputStream.cs \ csharp/src/Google.Protobuf/MessageExtensions.cs \ csharp/src/Google.Protobuf/MessageParser.cs \ + csharp/src/Google.Protobuf/ObjectIntPair.cs \ csharp/src/Google.Protobuf/ProtoPreconditions.cs \ csharp/src/Google.Protobuf/Properties/AssemblyInfo.cs \ csharp/src/Google.Protobuf/Reflection/CustomOptions.cs \ csharp/src/Google.Protobuf/Reflection/Descriptor.cs \ csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs \ + csharp/src/Google.Protobuf/Reflection/DescriptorDeclaration.cs \ csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs \ csharp/src/Google.Protobuf/Reflection/DescriptorUtil.cs \ csharp/src/Google.Protobuf/Reflection/DescriptorValidationException.cs \ csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs \ csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs \ + csharp/src/Google.Protobuf/Reflection/ExtensionAccessor.cs \ + csharp/src/Google.Protobuf/Reflection/ExtensionCollection.cs \ csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs \ csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs \ csharp/src/Google.Protobuf/Reflection/FieldType.cs \ @@ -176,7 +197,6 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs \ csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs \ csharp/src/Google.Protobuf/Reflection/PackageDescriptor.cs \ - csharp/src/Google.Protobuf/Reflection/PartialClasses.cs \ csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs \ csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs \ csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs \ @@ -205,6 +225,7 @@ csharp_EXTRA_DIST= \ java_EXTRA_DIST= \ java/README.md \ + java/bom/pom.xml \ java/core/generate-sources-build.xml \ java/core/generate-test-sources-build.xml \ java/core/pom.xml \ @@ -212,17 +233,25 @@ java_EXTRA_DIST= java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java \ java/core/src/main/java/com/google/protobuf/AbstractParser.java \ java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java \ + java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java \ java/core/src/main/java/com/google/protobuf/Android.java \ + java/core/src/main/java/com/google/protobuf/ArrayDecoders.java \ + java/core/src/main/java/com/google/protobuf/BinaryReader.java \ + java/core/src/main/java/com/google/protobuf/BinaryWriter.java \ java/core/src/main/java/com/google/protobuf/BlockingRpcChannel.java \ java/core/src/main/java/com/google/protobuf/BlockingService.java \ java/core/src/main/java/com/google/protobuf/BooleanArrayList.java \ + java/core/src/main/java/com/google/protobuf/BufferAllocator.java \ java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java \ java/core/src/main/java/com/google/protobuf/ByteOutput.java \ java/core/src/main/java/com/google/protobuf/ByteString.java \ java/core/src/main/java/com/google/protobuf/CodedInputStream.java \ + java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java \ java/core/src/main/java/com/google/protobuf/CodedOutputStream.java \ - java/core/src/main/java/com/google/protobuf/DiscardUnknownFieldsParser.java \ + java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java \ + java/core/src/main/java/com/google/protobuf/DescriptorMessageInfoFactory.java \ java/core/src/main/java/com/google/protobuf/Descriptors.java \ + java/core/src/main/java/com/google/protobuf/DiscardUnknownFieldsParser.java \ java/core/src/main/java/com/google/protobuf/DoubleArrayList.java \ java/core/src/main/java/com/google/protobuf/DynamicMessage.java \ java/core/src/main/java/com/google/protobuf/ExperimentalApi.java \ @@ -231,37 +260,65 @@ java_EXTRA_DIST= java/core/src/main/java/com/google/protobuf/ExtensionRegistry.java \ java/core/src/main/java/com/google/protobuf/ExtensionRegistryFactory.java \ java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java \ + java/core/src/main/java/com/google/protobuf/ExtensionSchema.java \ + java/core/src/main/java/com/google/protobuf/ExtensionSchemaFull.java \ + java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java \ + java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java \ + java/core/src/main/java/com/google/protobuf/FieldInfo.java \ java/core/src/main/java/com/google/protobuf/FieldSet.java \ + java/core/src/main/java/com/google/protobuf/FieldType.java \ java/core/src/main/java/com/google/protobuf/FloatArrayList.java \ java/core/src/main/java/com/google/protobuf/GeneratedMessage.java \ + java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java \ java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java \ java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java \ java/core/src/main/java/com/google/protobuf/IntArrayList.java \ java/core/src/main/java/com/google/protobuf/Internal.java \ java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java \ java/core/src/main/java/com/google/protobuf/IterableByteBufferInputStream.java \ + java/core/src/main/java/com/google/protobuf/JavaType.java \ java/core/src/main/java/com/google/protobuf/LazyField.java \ java/core/src/main/java/com/google/protobuf/LazyFieldLite.java \ java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java \ java/core/src/main/java/com/google/protobuf/LazyStringList.java \ + java/core/src/main/java/com/google/protobuf/ListFieldSchema.java \ java/core/src/main/java/com/google/protobuf/LongArrayList.java \ + java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java \ java/core/src/main/java/com/google/protobuf/MapEntry.java \ java/core/src/main/java/com/google/protobuf/MapEntryLite.java \ java/core/src/main/java/com/google/protobuf/MapField.java \ java/core/src/main/java/com/google/protobuf/MapFieldLite.java \ + java/core/src/main/java/com/google/protobuf/MapFieldSchema.java \ + java/core/src/main/java/com/google/protobuf/MapFieldSchemaFull.java \ + java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java \ + java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java \ java/core/src/main/java/com/google/protobuf/Message.java \ + java/core/src/main/java/com/google/protobuf/MessageInfo.java \ + java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java \ java/core/src/main/java/com/google/protobuf/MessageLite.java \ java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java \ java/core/src/main/java/com/google/protobuf/MessageLiteToString.java \ java/core/src/main/java/com/google/protobuf/MessageOrBuilder.java \ java/core/src/main/java/com/google/protobuf/MessageReflection.java \ + java/core/src/main/java/com/google/protobuf/MessageSchema.java \ + java/core/src/main/java/com/google/protobuf/MessageSetSchema.java \ java/core/src/main/java/com/google/protobuf/MutabilityOracle.java \ + java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java \ + java/core/src/main/java/com/google/protobuf/NewInstanceSchemaFull.java \ + java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java \ + java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java \ java/core/src/main/java/com/google/protobuf/NioByteString.java \ + java/core/src/main/java/com/google/protobuf/OneofInfo.java \ java/core/src/main/java/com/google/protobuf/Parser.java \ java/core/src/main/java/com/google/protobuf/PrimitiveNonBoxingCollection.java \ + java/core/src/main/java/com/google/protobuf/ProtoSyntax.java \ + java/core/src/main/java/com/google/protobuf/Protobuf.java \ java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java \ + java/core/src/main/java/com/google/protobuf/ProtobufLists.java \ java/core/src/main/java/com/google/protobuf/ProtocolMessageEnum.java \ java/core/src/main/java/com/google/protobuf/ProtocolStringList.java \ + java/core/src/main/java/com/google/protobuf/RawMessageInfo.java \ + java/core/src/main/java/com/google/protobuf/Reader.java \ java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java \ java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java \ java/core/src/main/java/com/google/protobuf/RopeByteString.java \ @@ -269,30 +326,47 @@ java_EXTRA_DIST= java/core/src/main/java/com/google/protobuf/RpcChannel.java \ java/core/src/main/java/com/google/protobuf/RpcController.java \ java/core/src/main/java/com/google/protobuf/RpcUtil.java \ + java/core/src/main/java/com/google/protobuf/Schema.java \ + java/core/src/main/java/com/google/protobuf/SchemaFactory.java \ + java/core/src/main/java/com/google/protobuf/SchemaUtil.java \ java/core/src/main/java/com/google/protobuf/Service.java \ java/core/src/main/java/com/google/protobuf/ServiceException.java \ java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java \ java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java \ java/core/src/main/java/com/google/protobuf/SmallSortedMap.java \ + java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java \ java/core/src/main/java/com/google/protobuf/TextFormat.java \ java/core/src/main/java/com/google/protobuf/TextFormatEscaper.java \ java/core/src/main/java/com/google/protobuf/TextFormatParseInfoTree.java \ java/core/src/main/java/com/google/protobuf/TextFormatParseLocation.java \ java/core/src/main/java/com/google/protobuf/UninitializedMessageException.java \ + java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java \ java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java \ java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java \ + java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java \ + java/core/src/main/java/com/google/protobuf/UnknownFieldSetSchema.java \ java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java \ java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java \ java/core/src/main/java/com/google/protobuf/UnsafeUtil.java \ java/core/src/main/java/com/google/protobuf/Utf8.java \ java/core/src/main/java/com/google/protobuf/WireFormat.java \ + java/core/src/main/java/com/google/protobuf/Writer.java \ java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java \ + java/core/src/test/java/com/google/protobuf/AbstractProto2LiteSchemaTest.java \ + java/core/src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java \ + java/core/src/test/java/com/google/protobuf/AbstractProto3LiteSchemaTest.java \ + java/core/src/test/java/com/google/protobuf/AbstractProto3SchemaTest.java \ + java/core/src/test/java/com/google/protobuf/AbstractSchemaTest.java \ java/core/src/test/java/com/google/protobuf/AnyTest.java \ + java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java \ + java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java \ java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java \ java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java \ java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java \ java/core/src/test/java/com/google/protobuf/ByteStringTest.java \ + java/core/src/test/java/com/google/protobuf/CachedFieldSizeTest.java \ java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java \ + java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java \ java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java \ java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java \ java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java \ @@ -302,6 +376,9 @@ java_EXTRA_DIST= java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java \ java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java \ java/core/src/test/java/com/google/protobuf/EnumTest.java \ + java/core/src/test/java/com/google/protobuf/ExperimentalMessageFactory.java \ + java/core/src/test/java/com/google/protobuf/ExperimentalSerializationUtil.java \ + java/core/src/test/java/com/google/protobuf/ExperimentalTestDataProvider.java \ java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java \ java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java \ java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java \ @@ -316,17 +393,32 @@ java_EXTRA_DIST= java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java \ java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java \ java/core/src/test/java/com/google/protobuf/LiteEqualsAndHashTest.java \ - java/core/src/test/java/com/google/protobuf/LiteTest.java \ java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java \ java/core/src/test/java/com/google/protobuf/LongArrayListTest.java \ java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java \ java/core/src/test/java/com/google/protobuf/MapForProto2Test.java \ + java/core/src/test/java/com/google/protobuf/MapLiteTest.java \ java/core/src/test/java/com/google/protobuf/MapTest.java \ java/core/src/test/java/com/google/protobuf/MessageTest.java \ java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java \ java/core/src/test/java/com/google/protobuf/NioByteStringTest.java \ + java/core/src/test/java/com/google/protobuf/PackedFieldTest.java \ java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java \ + java/core/src/test/java/com/google/protobuf/ParserLiteTest.java \ java/core/src/test/java/com/google/protobuf/ParserTest.java \ + java/core/src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java \ + java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java \ + java/core/src/test/java/com/google/protobuf/Proto2MessageFactory.java \ + java/core/src/test/java/com/google/protobuf/Proto2MessageInfoFactory.java \ + java/core/src/test/java/com/google/protobuf/Proto2MessageLiteFactory.java \ + java/core/src/test/java/com/google/protobuf/Proto2SchemaTest.java \ + java/core/src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java \ + java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java \ + java/core/src/test/java/com/google/protobuf/Proto3MessageFactory.java \ + java/core/src/test/java/com/google/protobuf/Proto3MessageInfoFactory.java \ + java/core/src/test/java/com/google/protobuf/Proto3MessageLiteFactory.java \ + java/core/src/test/java/com/google/protobuf/Proto3MessageLiteInfoFactory.java \ + java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java \ java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java \ java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java \ java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java \ @@ -336,18 +428,25 @@ java_EXTRA_DIST= java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java \ java/core/src/test/java/com/google/protobuf/TestBadIdentifiers.java \ java/core/src/test/java/com/google/protobuf/TestBadIdentifiersLite.java \ + java/core/src/test/java/com/google/protobuf/TestSchemas.java \ + java/core/src/test/java/com/google/protobuf/TestSchemasLite.java \ java/core/src/test/java/com/google/protobuf/TestUtil.java \ java/core/src/test/java/com/google/protobuf/TestUtilLite.java \ java/core/src/test/java/com/google/protobuf/TextFormatParseInfoTreeTest.java \ java/core/src/test/java/com/google/protobuf/TextFormatParseLocationTest.java \ java/core/src/test/java/com/google/protobuf/TextFormatTest.java \ java/core/src/test/java/com/google/protobuf/UnknownEnumValueTest.java \ - java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java \ java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java \ java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java \ + java/core/src/test/java/com/google/protobuf/Utf8Test.java \ + java/core/src/test/java/com/google/protobuf/Utf8Utils.java \ java/core/src/test/java/com/google/protobuf/WellKnownTypesTest.java \ + java/core/src/test/java/com/google/protobuf/WireFormatLiteTest.java \ java/core/src/test/java/com/google/protobuf/WireFormatTest.java \ + java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java \ + java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java \ java/core/src/test/proto/com/google/protobuf/any_test.proto \ + java/core/src/test/proto/com/google/protobuf/cached_field_size_test.proto \ java/core/src/test/proto/com/google/protobuf/deprecated_file.proto \ java/core/src/test/proto/com/google/protobuf/field_presence_test.proto \ java/core/src/test/proto/com/google/protobuf/lazy_fields_lite.proto \ @@ -357,6 +456,7 @@ java_EXTRA_DIST= java/core/src/test/proto/com/google/protobuf/map_initialization_order_test.proto \ java/core/src/test/proto/com/google/protobuf/map_lite_test.proto \ java/core/src/test/proto/com/google/protobuf/map_test.proto \ + java/core/src/test/proto/com/google/protobuf/message_lite_extension_util_test.proto\ java/core/src/test/proto/com/google/protobuf/multiple_files_test.proto \ java/core/src/test/proto/com/google/protobuf/nested_builders_test.proto \ java/core/src/test/proto/com/google/protobuf/nested_extension.proto \ @@ -366,12 +466,26 @@ java_EXTRA_DIST= java/core/src/test/proto/com/google/protobuf/outer_class_name_test.proto \ java/core/src/test/proto/com/google/protobuf/outer_class_name_test2.proto \ java/core/src/test/proto/com/google/protobuf/outer_class_name_test3.proto \ + java/core/src/test/proto/com/google/protobuf/packed_field_test.proto \ + java/core/src/test/proto/com/google/protobuf/proto2_message.proto \ + java/core/src/test/proto/com/google/protobuf/proto2_message_lite.proto \ + java/core/src/test/proto/com/google/protobuf/proto2_unknown_enum_values.proto \ + java/core/src/test/proto/com/google/protobuf/proto3_message.proto \ + java/core/src/test/proto/com/google/protobuf/proto3_message_lite.proto \ java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto \ java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto \ java/core/src/test/proto/com/google/protobuf/test_check_utf8_size.proto \ java/core/src/test/proto/com/google/protobuf/test_custom_options.proto \ java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto \ + java/core/src/test/proto/com/google/protobuf/wrappers_test.proto \ java/lite.md \ + java/lite/generate-sources-build.xml \ + java/lite/generate-test-sources-build.xml \ + java/lite/lite.awk \ + java/lite/pom.xml \ + java/lite/process-lite-sources-build.xml \ + java/lite/src/test/java/com/google/protobuf/LiteTest.java \ + java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java \ java/pom.xml \ java/util/pom.xml \ java/util/src/main/java/com/google/protobuf/util/Durations.java \ @@ -379,11 +493,15 @@ java_EXTRA_DIST= java/util/src/main/java/com/google/protobuf/util/FieldMaskUtil.java \ java/util/src/main/java/com/google/protobuf/util/JsonFormat.java \ java/util/src/main/java/com/google/protobuf/util/TimeUtil.java \ + java/util/src/main/java/com/google/protobuf/util/Structs.java \ java/util/src/main/java/com/google/protobuf/util/Timestamps.java \ + java/util/src/main/java/com/google/protobuf/util/Values.java \ java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java \ java/util/src/test/java/com/google/protobuf/util/FieldMaskUtilTest.java \ java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java \ + java/util/src/test/java/com/google/protobuf/util/StructsTest.java \ java/util/src/test/java/com/google/protobuf/util/TimeUtilTest.java \ + java/util/src/test/java/com/google/protobuf/util/ValuesTest.java \ java/util/src/test/proto/com/google/protobuf/util/json_test.proto objectivec_EXTRA_DIST= \ @@ -468,6 +586,12 @@ objectivec_EXTRA_DIST= \ objectivec/ProtocolBuffers_OSX.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings \ objectivec/ProtocolBuffers_OSX.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme \ objectivec/ProtocolBuffers_OSX.xcodeproj/xcshareddata/xcschemes/ProtocolBuffers.xcscheme \ + objectivec/ProtocolBuffers_tvOS.xcodeproj/project.pbxproj \ + objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata \ + objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist \ + objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings \ + objectivec/ProtocolBuffers_tvOS.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme \ + objectivec/ProtocolBuffers_tvOS.xcodeproj/xcshareddata/xcschemes/ProtocolBuffers.xcscheme \ objectivec/README.md \ objectivec/Tests/CocoaPods/OSXCocoaPodsTester/OSXCocoaPodsTester.xcodeproj/project.pbxproj \ objectivec/Tests/CocoaPods/OSXCocoaPodsTester/OSXCocoaPodsTester.xcodeproj/project.xcworkspace/contents.xcworkspacedata \ @@ -553,6 +677,7 @@ objectivec_EXTRA_DIST= \ objectivec/Tests/GPBUtilitiesTests.m \ objectivec/Tests/GPBWellKnownTypesTest.m \ objectivec/Tests/GPBWireFormatTests.m \ + objectivec/Tests/text_format_extensions_unittest_data.txt \ objectivec/Tests/text_format_map_unittest_data.txt \ objectivec/Tests/text_format_unittest_data.txt \ objectivec/Tests/unittest_cycle.proto \ @@ -566,6 +691,7 @@ objectivec_EXTRA_DIST= \ objectivec/Tests/unittest_extension_chain_f.proto \ objectivec/Tests/unittest_extension_chain_g.proto \ objectivec/Tests/unittest_objc.proto \ + objectivec/Tests/unittest_objc_options.proto \ objectivec/Tests/unittest_objc_startup.proto \ objectivec/Tests/unittest_runtime_proto2.proto \ objectivec/Tests/unittest_runtime_proto3.proto \ @@ -578,6 +704,7 @@ php_EXTRA_DIST= \ php/README.md \ php/composer.json \ php/ext/google/protobuf/array.c \ + php/ext/google/protobuf/builtin_descriptors.inc \ php/ext/google/protobuf/config.m4 \ php/ext/google/protobuf/def.c \ php/ext/google/protobuf/encode_decode.c \ @@ -594,6 +721,7 @@ php_EXTRA_DIST= \ php/ext/google/protobuf/utf8.h \ php/generate_descriptor_protos.sh \ php/phpunit.xml \ + php/release.sh \ php/src/GPBMetadata/Google/Protobuf/Any.php \ php/src/GPBMetadata/Google/Protobuf/Api.php \ php/src/GPBMetadata/Google/Protobuf/Duration.php \ @@ -721,6 +849,7 @@ php_EXTRA_DIST= \ php/tests/autoload.php \ php/tests/bootstrap_phpunit.php \ php/tests/compatibility_test.sh \ + php/tests/compile_extension.sh \ php/tests/descriptors_test.php \ php/tests/encode_decode_test.php \ php/tests/gdb_test.sh \ @@ -747,11 +876,13 @@ php_EXTRA_DIST= \ php/tests/proto/test_reserved_message_upper.proto \ php/tests/proto/test_service.proto \ php/tests/proto/test_service_namespace.proto \ + php/tests/proto/test_wrapper_type_setters.proto \ php/tests/test.sh \ php/tests/test_base.php \ php/tests/test_util.php \ php/tests/undefined_test.php \ - php/tests/well_known_test.php + php/tests/well_known_test.php \ + php/tests/wrapper_type_setters_test.php python_EXTRA_DIST= \ python/MANIFEST.in \ @@ -776,6 +907,7 @@ python_EXTRA_DIST= \ python/google/protobuf/internal/descriptor_test.py \ python/google/protobuf/internal/encoder.py \ python/google/protobuf/internal/enum_type_wrapper.py \ + python/google/protobuf/internal/extension_dict.py \ python/google/protobuf/internal/factory_test1.proto \ python/google/protobuf/internal/factory_test2.proto \ python/google/protobuf/internal/file_options_test.proto \ @@ -784,6 +916,7 @@ python_EXTRA_DIST= \ python/google/protobuf/internal/import_test_package/inner.proto \ python/google/protobuf/internal/import_test_package/outer.proto \ python/google/protobuf/internal/json_format_test.py \ + python/google/protobuf/internal/keywords_test.py \ python/google/protobuf/internal/message_factory_test.py \ python/google/protobuf/internal/message_listener.py \ python/google/protobuf/internal/message_set_extensions.proto \ @@ -851,7 +984,6 @@ python_EXTRA_DIST= \ python/google/protobuf/pyext/repeated_scalar_container.h \ python/google/protobuf/pyext/safe_numerics.h \ python/google/protobuf/pyext/scoped_pyobject_ptr.h \ - python/google/protobuf/pyext/thread_unsafe_shared_ptr.h \ python/google/protobuf/reflection.py \ python/google/protobuf/service.py \ python/google/protobuf/service_reflection.py \ @@ -917,16 +1049,25 @@ ruby_EXTRA_DIST= \ ruby/src/main/java/com/google/protobuf/jruby/Utils.java \ ruby/src/main/java/google/ProtobufJavaService.java \ ruby/src/main/sentinel.proto \ + ruby/tests/basic_proto2.rb \ + ruby/tests/basic_test_proto2.proto \ + ruby/tests/basic_test.proto \ ruby/tests/basic.rb \ + ruby/tests/common_tests.rb \ ruby/tests/encode_decode_test.rb \ ruby/tests/gc_test.rb \ ruby/tests/repeated_field_test.rb \ ruby/tests/stress.rb \ + ruby/tests/generated_code_proto2_test.rb \ + ruby/tests/generated_code_proto2.proto \ ruby/tests/generated_code.proto \ + ruby/tests/test_import_proto2.proto \ ruby/tests/test_import.proto \ + ruby/tests/test_ruby_package_proto2.proto \ ruby/tests/test_ruby_package.proto \ ruby/tests/generated_code_test.rb \ ruby/tests/well_known_types_test.rb \ + ruby/tests/type_errors.rb \ ruby/travis-test.sh js_EXTRA_DIST= \ @@ -1025,6 +1166,11 @@ js_EXTRA_DIST= \ js/test8.proto \ js/test9.proto \ js/test10.proto \ + js/test11.proto \ + js/test12.proto \ + js/test13.proto \ + js/test14.proto \ + js/test15.proto \ js/test_bootstrap.js \ js/testbinary.proto \ js/testempty.proto @@ -1043,6 +1189,7 @@ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \ WORKSPACE \ cmake/CMakeLists.txt \ cmake/README.md \ + cmake/conformance.cmake \ cmake/examples.cmake \ cmake/extract_includes.bat.in \ cmake/install.cmake \ @@ -1058,6 +1205,7 @@ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \ cmake/protoc.cmake \ cmake/tests.cmake \ cmake/version.rc.in \ + compiler_config_setting.bzl \ editors/README.txt \ editors/proto.vim \ editors/protobuf-mode.el \ @@ -1069,20 +1217,26 @@ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \ examples/README.md \ examples/WORKSPACE \ examples/add_person.cc \ + examples/add_person.dart \ examples/add_person.go \ examples/add_person.py \ examples/add_person_test.go \ examples/addressbook.proto \ examples/list_people.cc \ + examples/list_people.dart \ examples/list_people.go \ examples/list_people.py \ examples/list_people_test.go \ + examples/pubspec.yaml \ + examples/third_party/zlib.BUILD \ protobuf.bzl \ + protobuf_deps.bzl \ python/release/wheel/build_wheel_manylinux.sh \ python/release/wheel/Dockerfile \ python/release/wheel/protobuf_optimized_pip.sh \ python/release/wheel/README.md \ six.BUILD \ + third_party/zlib.BUILD \ util/python/BUILD diff --git a/Protobuf.podspec b/Protobuf.podspec index f282f540bb..3881007dae 100644 --- a/Protobuf.podspec +++ b/Protobuf.podspec @@ -5,14 +5,14 @@ # dependent projects use the :git notation to refer to the library. Pod::Spec.new do |s| s.name = 'Protobuf' - s.version = '3.6.1' + s.version = '3.9.0-rc1' s.summary = 'Protocol Buffers v.3 runtime library for Objective-C.' - s.homepage = 'https://github.com/google/protobuf' + s.homepage = 'https://github.com/protocolbuffers/protobuf' s.license = '3-Clause BSD License' s.authors = { 'The Protocol Buffers contributors' => 'protobuf@googlegroups.com' } s.cocoapods_version = '>= 1.0' - s.source = { :git => 'https://github.com/google/protobuf.git', + s.source = { :git => 'https://github.com/protocolbuffers/protobuf.git', :tag => "v#{s.version}" } s.source_files = 'objectivec/*.{h,m}', diff --git a/README.md b/README.md index 618c6d7829..0e5ae61e26 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ with the C++ runtime. For non-C++ users, the simplest way to install the protocol compiler is to download a pre-built binary from our release page: - [https://github.com/google/protobuf/releases](https://github.com/google/protobuf/releases) + [https://github.com/protocolbuffers/protobuf/releases](https://github.com/protocolbuffers/protobuf/releases) In the downloads section of each release, you can find pre-built binaries in zip packages: protoc-$VERSION-$PLATFORM.zip. It contains the protoc binary @@ -54,16 +54,16 @@ how to install protobuf runtime for that specific language: | Language | Source | Ubuntu | MacOS | Windows | |--------------------------------------|-------------------------------------------------------------|--------|-------|---------| -| C++ (include C++ runtime and protoc) | [src](src) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-cpp_distcheck.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fcpp_distcheck%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-bazel.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fbazel%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fcpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-cpp_distcheck.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fcpp_distcheck%2Fcontinuous) | [![Build status](https://ci.appveyor.com/api/projects/status/73ctee6ua4w2ruin?svg=true)](https://ci.appveyor.com/project/protobuf/protobuf) | -| Java | [java](java) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-java_compatibility.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjava_compatibility%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-java_jdk7.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjava_jdk7%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-java_oracle7.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjava_oracle7%2Fcontinuous) | | | -| Python | [python](python) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python_compatibility.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython_compatibility%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython_cpp%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython_cpp%2Fcontinuous) | | +| C++ (include C++ runtime and protoc) | [src](src) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-cpp_distcheck.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fcpp_distcheck%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-bazel.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fbazel%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-dist_install.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fdist_install%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fcpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-cpp_distcheck.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fcpp_distcheck%2Fcontinuous) | [![Build status](https://ci.appveyor.com/api/projects/status/73ctee6ua4w2ruin?svg=true)](https://ci.appveyor.com/project/protobuf/protobuf) | +| Java | [java](java) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-java_compatibility.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjava_compatibility%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-java_jdk7.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjava_jdk7%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-java_oracle7.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjava_oracle7%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-java_linkage_monitor.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjava_linkage_monitor%2Fcontinuous) | | | +| Python | [python](python) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python27.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython27%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python33.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython33%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python34.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython34%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python35.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython35%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python36.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython36%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python37.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython37%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python_compatibility.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython_compatibility%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python27_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython27_cpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python33_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython33_cpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python34_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython34_cpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python35_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython35_cpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python36_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython36_cpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python37_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython37_cpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python-release.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython_release%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython_cpp%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python-release.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython_release%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/windows-python-release.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fwindows%2Fpython_release%2Fcontinuous) | | Objective-C | [objectivec](objectivec) | | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-objectivec_cocoapods_integration.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fobjectivec_cocoapods_integration%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-objectivec_ios_debug.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fobjectivec_ios_debug%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-objectivec_ios_release.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fobjectivec_ios_release%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-objectivec_osx.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fobjectivec_osx%2Fcontinuous) | | -| C# | [csharp](csharp) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-csharp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fcsharp%2Fcontinuous) | | [![Build status](https://ci.appveyor.com/api/projects/status/73ctee6ua4w2ruin?svg=true)](https://ci.appveyor.com/project/protobuf/protobuf) | +| C# | [csharp](csharp) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-csharp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fcsharp%2Fcontinuous) | | [![Build status](https://ci.appveyor.com/api/projects/status/73ctee6ua4w2ruin?svg=true)](https://ci.appveyor.com/project/protobuf/protobuf)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/windows-csharp-release.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fwindows%2Fcsharp_release%2Fcontinuous) | | JavaScript | [js](js) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-javascript.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjavascript%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-javascript.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fjavascript%2Fcontinuous) | | -| Ruby | [ruby](ruby) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-ruby_all.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fruby_all%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-ruby21.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fruby21%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-ruby22.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fruby22%2Fcontinuous) | | +| Ruby | [ruby](ruby) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-ruby23.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fruby23%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-ruby24.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fruby24%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-ruby25.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fruby25%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-ruby26.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fruby26%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-ruby-release.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fruby_release%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-ruby23.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fruby23%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-ruby24.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fruby24%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-ruby25.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fruby25%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-ruby26.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fruby26%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-ruby-release.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fruby_release%2Fcontinuous) | | | Go | [golang/protobuf](https://github.com/golang/protobuf) | | | | | PHP | [php](php) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-php_all.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fphp_all%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-32-bit.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2F32-bit%2Fcontinuous) | [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-php5.6_mac.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fphp5.6_mac%2Fcontinuous)
[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-php7.0_mac.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fphp7.0_mac%2Fcontinuous) | | -| Dart | [dart-lang/protobuf](https://github.com/dart-lang/protobuf) | | | | +| Dart | [dart-lang/protobuf](https://github.com/dart-lang/protobuf) | [![Build Status](https://travis-ci.org/dart-lang/protobuf.svg?branch=master)](https://travis-ci.org/dart-lang/protobuf) | | | Quick Start ----------- diff --git a/WORKSPACE b/WORKSPACE index f3020843f1..3687680024 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,10 +1,15 @@ workspace(name = "com_google_protobuf") + load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("//:protobuf_deps.bzl", "protobuf_deps") + +# Load common dependencies. +protobuf_deps() new_local_repository( name = "submodule_gmock", + build_file = "@//:third_party/googletest/BUILD.bazel", path = "third_party/googletest", - build_file = "@//:third_party/googletest/BUILD.bazel" ) http_archive( @@ -60,3 +65,13 @@ bind( name = "gson", actual = "@gson_maven//jar", ) + +maven_jar( + name = "error_prone_annotations_maven", + artifact = "com.google.errorprone:error_prone_annotations:2.3.2", +) + +bind( + name = "error_prone_annotations", + actual = "@error_prone_annotations_maven//jar", +) diff --git a/appveyor.bat b/appveyor.bat index 29ec4922ae..7a35ceb4d6 100644 --- a/appveyor.bat +++ b/appveyor.bat @@ -38,7 +38,7 @@ dotnet restore dotnet build -c %configuration% || goto error echo Testing C# -dotnet test -c %configuration% -f netcoreapp1.0 Google.Protobuf.Test\Google.Protobuf.Test.csproj || goto error +dotnet test -c %configuration% -f netcoreapp2.1 Google.Protobuf.Test\Google.Protobuf.Test.csproj || goto error dotnet test -c %configuration% -f net451 Google.Protobuf.Test\Google.Protobuf.Test.csproj || goto error goto :EOF diff --git a/benchmarks/Makefile.am b/benchmarks/Makefile.am index c758ee6bed..423349720a 100644 --- a/benchmarks/Makefile.am +++ b/benchmarks/Makefile.am @@ -25,6 +25,7 @@ make_tmp_dir: mkdir -p 'tmp/java/src/main/java' touch make_tmp_dir + # We have to cd to $(srcdir) before executing protoc because $(protoc_inputs) is # relative to srcdir, which may not be the same as the current directory when # building out-of-tree. @@ -90,8 +91,8 @@ $(benchmarks_protoc_outputs_proto2_header): protoc_middleman2 initialize_submodule: oldpwd=`pwd` - cd $(top_srcdir) && git submodule update --init -r third_party/benchmark - cd $(top_srcdir)/third_party/benchmark && cmake -DCMAKE_BUILD_TYPE=Release && make + cd $(top_srcdir) && git submodule update --init -r third_party/benchmark && \ + cd third_party/benchmark && cmake -DCMAKE_BUILD_TYPE=Release && make cd $$oldpwd touch initialize_submodule @@ -126,7 +127,7 @@ java_benchmark_testing_files = \ java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java javac_middleman: $(java_benchmark_testing_files) protoc_middleman protoc_middleman2 - cp -r $(srcdir)/java tmp + cp -r $(srcdir)/java tmp mkdir -p tmp/java/lib cp $(top_srcdir)/java/core/target/*.jar tmp/java/lib/protobuf-java.jar cd tmp/java && mvn clean compile assembly:single -Dprotobuf.version=$(PACKAGE_VERSION) && cd ../.. @@ -253,19 +254,19 @@ go_protoc_middleman: make_tmp_dir $(top_srcdir)/src/protoc$(EXEEXT) $(benchmarks oldpwd=`pwd` && ( cd $(srcdir) && $$oldpwd/../src/protoc$(EXEEXT) -I. -I$(top_srcdir)/src --go_out=$$oldpwd/tmp $(benchmarks_protoc_inputs_proto2_message4) ) touch go_protoc_middleman -go-benchmark: go_protoc_middleman +go-benchmark: go_protoc_middleman @echo "Writing shortcut script go-benchmark..." @echo '#! /bin/bash' > go-benchmark @echo 'cd $(srcdir)/go' >> go-benchmark @echo 'all_data=""' >> go-benchmark @echo 'conf=()' >> go-benchmark @echo 'data_files=()' >> go-benchmark - @echo 'for arg in $$@; do if [[ $${arg:0:1} == "-" ]]; then conf+=($$arg); else data_files+=("../$$arg"); fi; done' >> go-benchmark + @echo 'for arg in $$@; do if [[ $${arg:0:1} == "-" ]]; then conf+=($$arg); else data_files+=("$$arg"); fi; done' >> go-benchmark @echo 'go test -bench=. $${conf[*]} -- $${data_files[*]}' >> go-benchmark @echo 'cd ..' >> go-benchmark @chmod +x go-benchmark -go: go_protoc_middleman go-benchmark +go: go_protoc_middleman go-benchmark ./go-benchmark $(all_data) ############# GO RULES END ############## @@ -322,9 +323,9 @@ $(cpp_no_group_benchmarks_protoc_outputs_proto2_header): cpp_no_group_protoc_mid generate_cpp_no_group_benchmark_code: cp $(srcdir)/cpp/cpp_benchmark.cc gogo/cpp_no_group/cpp_benchmark.cc sed -i -e "s/\#include \"datasets/\#include \"gogo\/cpp_no_group\/datasets/g" gogo/cpp_no_group/cpp_benchmark.cc - sed -i -e "s/\#include \"benchmarks.pb.h/\#include \"gogo\/cpp_no_group\/benchmarks.pb.h/g" gogo/cpp_no_group/cpp_benchmark.cc + sed -i -e "s/\#include \"benchmarks.pb.h/\#include \"gogo\/cpp_no_group\/benchmarks.pb.h/g" gogo/cpp_no_group/cpp_benchmark.cc touch generate_cpp_no_group_benchmark_code - + bin_PROGRAMS += cpp-no-group-benchmark cpp_no_group_benchmark_LDADD = $(top_srcdir)/src/libprotobuf.la $(top_srcdir)/third_party/benchmark/src/libbenchmark.a cpp_no_group_benchmark_SOURCES = gogo/cpp_no_group/cpp_benchmark.cc @@ -343,7 +344,7 @@ nodist_cpp_no_group_benchmark_SOURCES = \ cpp_no_group: cpp_no_group_protoc_middleman generate_gogo_data cpp-no-group-benchmark ./cpp-no-group-benchmark $(gogo_data) - + gogo_proto_middleman: protoc-gen-gogoproto mkdir -p "tmp/gogo_proto" oldpwd=`pwd` && ( cd $(srcdir) && $$oldpwd/../src/protoc$(EXEEXT) -I$(srcdir) -I$(top_srcdir) --plugin=protoc-gen-gogoproto --gogoproto_out=$$oldpwd/tmp/gogo_proto $(benchmarks_protoc_inputs) $(benchmarks_protoc_inputs_benchmark_wrapper) $(benchmarks_protoc_inputs_proto2) ) @@ -355,7 +356,7 @@ generate_gogo_data: protoc_middleman protoc_middleman2 gogo-data-scrubber mkdir -p `dirname $(gogo_data)` ./gogo-data-scrubber $(all_data) $(gogo_data) touch generate_gogo_data - + make_tmp_dir_gogo: mkdir -p tmp/go_no_group/benchmark_code mkdir -p tmp/gogofast/benchmark_code @@ -435,10 +436,10 @@ gogo-benchmark: go_no_group: go_no_group_protoc_middleman generate_gogo_data generate_all_gogo_benchmark_code gogo-benchmark ./gogo-benchmark go_no_group $(gogo_data) - -gogofast: gogofast_protoc_middleman generate_gogo_data gogo-benchmark generate_all_gogo_benchmark_code + +gogofast: gogofast_protoc_middleman generate_gogo_data gogo-benchmark generate_all_gogo_benchmark_code ./gogo-benchmark gogofast $(gogo_data) - + gogofaster: gogofaster_protoc_middleman generate_gogo_data gogo-benchmark generate_all_gogo_benchmark_code ./gogo-benchmark gogofaster $(gogo_data) @@ -448,7 +449,7 @@ gogoslick: gogoslick_protoc_middleman generate_gogo_data gogo-benchmark generat ############# GOGO RULES END ############ - + ############ UTIL RULES BEGIN ############ bin_PROGRAMS += protoc-gen-gogoproto gogo-data-scrubber protoc-gen-proto2_to_proto3 proto3-data-stripper @@ -481,7 +482,7 @@ nodist_proto3_data_stripper_SOURCES = \ $(benchmarks_protoc_outputs_proto2_header) \ $(benchmarks_protoc_outputs_header) - + ############ UTIL RULES END ############ ############ PROTO3 PREPARATION BEGIN ############# @@ -510,7 +511,7 @@ proto3_middleman_php: proto3_proto_middleman php-benchmark: proto3_middleman_php generate_proto3_data mkdir -p "tmp/php/Google/Protobuf/Benchmark" && cp php/PhpBenchmark.php "tmp/php/Google/Protobuf/Benchmark" - cp php/autoload.php "tmp/php" + cp php/autoload.php "tmp/php" @echo "Writing shortcut script php-benchmark..." @echo '#! /bin/bash' > php-benchmark @echo 'export PROTOBUF_PHP_SRCDIR="$$(cd $(top_srcdir) && pwd)/php/src"' >> php-benchmark @@ -521,19 +522,18 @@ php-benchmark: proto3_middleman_php generate_proto3_data @chmod +x php-benchmark php: php-benchmark proto3_middleman_php - ./php-benchmark $(proto3_data) + ./php-benchmark --behavior_prefix="php" $(proto3_data) php_c_extension: - cd $(top_srcdir)/php/ext/google/protobuf && ./configure CFLAGS='-O3' && make -j8 + cd $(top_srcdir)/php/ext/google/protobuf && phpize && ./configure CFLAGS='-O3' && make -j8 php-c-benchmark: proto3_middleman_php generate_proto3_data php_c_extension php_c_extension - mkdir -p "tmp/php/Google/Protobuf/Benchmark" && cp php/PhpBenchmark.php "tmp/php/Google/Protobuf/Benchmark" - cp php/autoload.php "tmp/php" + mkdir -p "tmp/php/Google/Protobuf/Benchmark" && cp php/PhpBenchmark.php "tmp/php/Google/Protobuf/Benchmark" + cp php/autoload.php "tmp/php" @echo "Writing shortcut script php-c-benchmark..." @echo '#! /bin/bash' > php-c-benchmark @echo 'export PROTOBUF_PHP_SRCDIR="$$(cd $(top_srcdir) && pwd)/php/src"' >> php-c-benchmark @echo 'export PROTOBUF_PHP_EXTDIR="$$PROTOBUF_PHP_SRCDIR/../ext/google/protobuf/modules"' >> php-c-benchmark - @echo 'echo "$$PROTOBUF_PHP_EXTDIR/protobuf.so"' >> php-c-benchmark @echo 'cd tmp/php' >> php-c-benchmark @echo 'export CURRENT_DIR=$$(pwd)' >> php-c-benchmark @echo 'php -d auto_prepend_file="autoload.php" -d include_path="$$(pwd)" -d extension="$$PROTOBUF_PHP_EXTDIR/protobuf.so" Google/Protobuf/Benchmark/PhpBenchmark.php $$@' >> php-c-benchmark @@ -541,7 +541,7 @@ php-c-benchmark: proto3_middleman_php generate_proto3_data php_c_extension php_c @chmod +x php-c-benchmark php_c: php-c-benchmark proto3_middleman_php - ./php-c-benchmark $(proto3_data) + ./php-c-benchmark --behavior_prefix="php_c" $(proto3_data) ############ PHP RULES END ################# @@ -655,4 +655,4 @@ CLEANFILES = \ clean-local: -rm -rf tmp/* - + diff --git a/benchmarks/README.md b/benchmarks/README.md index 534e0bded7..9711fa167d 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -3,9 +3,9 @@ This directory contains benchmarking schemas and data sets that you can use to test a variety of performance scenarios against your -protobuf language runtime. If you are looking for performance +protobuf language runtime. If you are looking for performance numbers of officially support languages, see [here]( -https://github.com/google/protobuf/blob/master/docs/performance.md) +https://github.com/protocolbuffers/protobuf/blob/master/docs/performance.md) ## Prerequisite @@ -45,8 +45,8 @@ And you also need to make sure `pkg-config` is installed. ### Go Go protobufs are maintained at [github.com/golang/protobuf]( -http://github.com/golang/protobuf). If not done already, you need to install the -toolchain and the Go protoc-gen-go plugin for protoc. +http://github.com/golang/protobuf). If not done already, you need to install the +toolchain and the Go protoc-gen-go plugin for protoc. To install protoc-gen-go, run: @@ -59,11 +59,17 @@ The first command installs `protoc-gen-go` into the `bin` directory in your loca The second command adds the `bin` directory to your `PATH` so that `protoc` can locate the plugin later. ### PHP -PHP benchmark's requirement is the same as PHP protobuf's requirements. The benchmark will automaticly +PHP benchmark's requirement is the same as PHP protobuf's requirements. The benchmark will automaticly include PHP protobuf's src and build the c extension if required. ### Node.js -Node.js benchmark need [node](https://nodejs.org/en/)(higher than V6) and [npm](https://www.npmjs.com/) package manager installed. This benchmark is using the [benchmark](https://www.npmjs.com/package/benchmark) framework to test, which needn't to manually install. And another prerequisite is [protobuf js](https://github.com/google/protobuf/tree/master/js), which needn't to manually install either +Node.js benchmark need [node](https://nodejs.org/en/)(higher than V6) and [npm](https://www.npmjs.com/) package manager installed. This benchmark is using the [benchmark](https://www.npmjs.com/package/benchmark) framework to test, which needn't to manually install. And another prerequisite is [protobuf js](https://github.com/protocolbuffers/protobuf/tree/master/js), which needn't to manually install either + +### C# +The C# benchmark code is built as part of the main Google.Protobuf +solution. It requires the .NET Core SDK, and depends on +[BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet), which +will be downloaded automatically. ### Big data @@ -74,7 +80,7 @@ initially, you need to run the following command to download the testing data: $ ./download_data.sh ``` -After doing this the big data file will automaticly generated in the +After doing this the big data file will automatically generated in the benchmark directory. ## Run instructions @@ -209,6 +215,15 @@ $ make js-benchmark $ ./js-benchmark $(specific generated dataset file name) ``` +### C# +From `csharp/src/Google.Protobuf.Benchmarks`, run: + +``` +$ dotnet run -c Release +``` + +We intend to add support for this within the makefile in due course. + ## Benchmark datasets Each data set is in the format of benchmarks.proto: diff --git a/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java b/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java index c766d74ee4..a4402481b0 100644 --- a/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java +++ b/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java @@ -24,8 +24,8 @@ import java.util.ArrayList; import java.util.List; // Caliper set CICompilerCount to 1 for making sure compilation doesn't run in parallel with itself, -// This makes TieredCompilation not working. We just disable TieredCompilation by default. In master -// branch this has been disabled by default in caliper: +// This makes TieredCompilation not working. We just disable TieredCompilation by default. In master +// branch this has been disabled by default in caliper: // https://github.com/google/caliper/blob/master/caliper-runner/src/main/java/com/google/caliper/runner/target/Jvm.java#L38:14 // But this haven't been added into most recent release. @VmOptions("-XX:-TieredCompilation") @@ -89,7 +89,7 @@ public class ProtoCaliperBenchmark { return com.google.protobuf.benchmarks.BenchmarkMessage4.GoogleMessage4.getDefaultInstance(); } }; - + abstract ExtensionRegistry getExtensionRegistry(); abstract Message getDefaultInstance(); } @@ -97,7 +97,7 @@ public class ProtoCaliperBenchmark { private BenchmarkMessageType benchmarkMessageType; @Param("") private String dataFile; - + private byte[] inputData; private BenchmarkDataset benchmarkDataset; private Message defaultMessage; @@ -125,7 +125,7 @@ public class ProtoCaliperBenchmark { + benchmarkDataset.getMessageName()); } } - + @BeforeExperiment void setUp() throws IOException { if (!dataFile.equals("")) { @@ -145,7 +145,7 @@ public class ProtoCaliperBenchmark { inputStreamList = new ArrayList(); inputStringList = new ArrayList(); sampleMessageList = new ArrayList(); - + for (int i = 0; i < benchmarkDataset.getPayloadCount(); i++) { byte[] singleInputData = benchmarkDataset.getPayload(i).toByteArray(); inputDataList.add(benchmarkDataset.getPayload(i).toByteArray()); @@ -156,8 +156,8 @@ public class ProtoCaliperBenchmark { defaultMessage.newBuilderForType().mergeFrom(singleInputData, extensions).build()); } } - - + + @Benchmark void serializeToByteArray(int reps) throws IOException { if (sampleMessageList.size() == 0) { @@ -165,11 +165,11 @@ public class ProtoCaliperBenchmark { } for (int i = 0; i < reps; i++) { for (int j = 0; j < sampleMessageList.size(); j++) { - sampleMessageList.get(j).toByteArray(); + sampleMessageList.get(j).toByteArray(); } } } - + @Benchmark void serializeToMemoryStream(int reps) throws IOException { if (sampleMessageList.size() == 0) { @@ -178,11 +178,11 @@ public class ProtoCaliperBenchmark { for (int i = 0; i < reps; i++) { for (int j = 0; j < sampleMessageList.size(); j++) { ByteArrayOutputStream output = new ByteArrayOutputStream(); - sampleMessageList.get(j).writeTo(output); + sampleMessageList.get(j).writeTo(output); } } } - + @Benchmark void deserializeFromByteArray(int reps) throws IOException { if (inputDataList.size() == 0) { @@ -195,7 +195,7 @@ public class ProtoCaliperBenchmark { } } } - + @Benchmark void deserializeFromMemoryStream(int reps) throws IOException { if (inputStreamList.size() == 0) { diff --git a/benchmarks/js/benchmark_suite.js b/benchmarks/js/benchmark_suite.js index c95024b2e2..c5c3e5105a 100644 --- a/benchmarks/js/benchmark_suite.js +++ b/benchmarks/js/benchmark_suite.js @@ -9,8 +9,8 @@ function newBenchmark(messageName, filename, language) { }) .on("start", function() { process.stdout.write( - "benchmarking message " + messageName - + " of dataset file " + filename + "benchmarking message " + messageName + + " of dataset file " + filename + "'s performance ..." + "\n\n"); }) .on("cycle", function(event) { @@ -21,7 +21,7 @@ function newBenchmark(messageName, filename, language) { return 1 / (bench.stats.mean + bench.stats.moe); } benches.forEach(function(val, index) { - benches[index] = getHz(val); + benches[index] = getHz(val); }); }), benches: benches diff --git a/benchmarks/js/js_benchmark.js b/benchmarks/js/js_benchmark.js index 14905e312d..c44fee01c3 100644 --- a/benchmarks/js/js_benchmark.js +++ b/benchmarks/js/js_benchmark.js @@ -18,6 +18,7 @@ function getNewPrototype(name) { } var results = []; +var json_file = ""; console.log("#####################################################"); console.log("Js Benchmark: "); @@ -25,6 +26,11 @@ process.argv.forEach(function(filename, index) { if (index < 2) { return; } + if (filename.indexOf("--json_output") != -1) { + json_file = filename.replace(/^--json_output=/, ''); + return; + } + var benchmarkDataset = proto.benchmarks.BenchmarkDataset.deserializeBinary(fs.readFileSync(filename)); var messageList = []; @@ -34,7 +40,7 @@ process.argv.forEach(function(filename, index) { messageList.push(message.deserializeBinary(onePayload)); totalBytes += onePayload.length; }); - + var senarios = benchmarkSuite.newBenchmark( benchmarkDataset.getMessageName(), filename, "js"); senarios.suite @@ -42,29 +48,35 @@ process.argv.forEach(function(filename, index) { benchmarkDataset.getPayloadList().forEach(function(onePayload) { var protoType = getNewPrototype(benchmarkDataset.getMessageName()); protoType.deserializeBinary(onePayload); - }); + }); }) .add("js serialize", function() { var protoType = getNewPrototype(benchmarkDataset.getMessageName()); messageList.forEach(function(message) { message.serializeBinary(); }); - }) + }) .run({"Async": false}); results.push({ filename: filename, benchmarks: { - protobufjs_decoding: senarios.benches[0] * totalBytes, - protobufjs_encoding: senarios.benches[1] * totalBytes + protobufjs_decoding: senarios.benches[0] * totalBytes / 1024 / 1024, + protobufjs_encoding: senarios.benches[1] * totalBytes / 1024 / 1024 } }) - console.log("Throughput for deserialize: " + console.log("Throughput for deserialize: " + senarios.benches[0] * totalBytes / 1024 / 1024 + "MB/s" ); - console.log("Throughput for serialize: " + console.log("Throughput for serialize: " + senarios.benches[1] * totalBytes / 1024 / 1024 + "MB/s" ); console.log(""); }); console.log("#####################################################"); +if (json_file != "") { + fs.writeFile(json_file, JSON.stringify(results), (err) => { + if (err) throw err; + }); +} + diff --git a/benchmarks/php/PhpBenchmark.php b/benchmarks/php/PhpBenchmark.php index 058940d151..d3db61d248 100644 --- a/benchmarks/php/PhpBenchmark.php +++ b/benchmarks/php/PhpBenchmark.php @@ -1,6 +1,7 @@ mergeFromString($payloads->offsetGet($i)); } } - + // $args: array of message static function serialize(&$args) { foreach ($args as &$temp_message) { @@ -48,7 +49,7 @@ class Benchmark private $benchmark_time; private $total_bytes; private $coefficient; - + public function __construct($benchmark_name, $args, $total_bytes, $benchmark_time = 5.0) { $this->args = $args; @@ -57,15 +58,15 @@ class Benchmark $this->total_bytes = $total_bytes; $this->coefficient = pow (10, 0) / pow(2, 20); } - + public function runBenchmark() { $t = $this->runBenchmarkWithTimes(1); $times = ceil($this->benchmark_time / $t); return $this->total_bytes * $times / - $this->runBenchmarkWithTimes($times) * + ($times == 1 ? $t : $this->runBenchmarkWithTimes($times)) * $this->coefficient; } - + private function runBenchmarkWithTimes($times) { $st = microtime(true); for ($i = 0; $i < $times; $i++) { @@ -93,7 +94,7 @@ function getMessageName(&$dataset) { } } -function runBenchmark($file) { +function runBenchmark($file, $behavior_prefix) { $datafile = fopen($file, "r") or die("Unable to open file " . $file); $bytes = fread($datafile, filesize($file)); $dataset = new BenchmarkDataset(NULL); @@ -108,19 +109,19 @@ function runBenchmark($file) { array_push($message_list, $new_message); $total_bytes += strlen($payloads->offsetGet($i)); } - + $parse_benchmark = new Benchmark( "\Google\Protobuf\Benchmark\BenchmarkMethod::parse", array($dataset, $message_name), $total_bytes); $serialize_benchmark = new Benchmark( "\Google\Protobuf\Benchmark\BenchmarkMethod::serialize", $message_list, $total_bytes); - + return array( "filename" => $file, "benchmarks" => array( - "parse_php" => $parse_benchmark->runBenchmark(), - "serailize_php" => $serialize_benchmark->runBenchmark() + $behavior_prefix . "_parse" => $parse_benchmark->runBenchmark(), + $behavior_prefix . "_serailize" => $serialize_benchmark->runBenchmark() ), "message_name" => $dataset->getMessageName() ); @@ -129,15 +130,27 @@ function runBenchmark($file) { // main $json_output = false; $results = array(); +$behavior_prefix = ""; + foreach ($argv as $index => $arg) { if ($index == 0) { continue; } if ($arg == "--json") { $json_output = true; + } else if (strpos($arg, "--behavior_prefix") == 0) { + $behavior_prefix = str_replace("--behavior_prefix=", "", $arg); + } +} + +foreach ($argv as $index => $arg) { + if ($index == 0) { + continue; + } + if (substr($arg, 0, 2) == "--") { continue; } else { - array_push($results, runBenchmark($arg)); + array_push($results, runBenchmark($arg, $behavior_prefix)); } } diff --git a/benchmarks/protobuf.js/protobufjs_benchmark.js b/benchmarks/protobuf.js/protobufjs_benchmark.js index 2629e9f46f..19e54972b8 100644 --- a/benchmarks/protobuf.js/protobufjs_benchmark.js +++ b/benchmarks/protobuf.js/protobufjs_benchmark.js @@ -30,7 +30,7 @@ process.argv.forEach(function(filename, index) { messageList.push(message.decode(onePayload)); totalBytes += onePayload.length; }); - + var senarios = benchmarkSuite.newBenchmark( benchmarkDataset.messageName, filename, "protobufjs"); senarios.suite @@ -38,14 +38,14 @@ process.argv.forEach(function(filename, index) { benchmarkDataset.payload.forEach(function(onePayload) { var protoType = getNewPrototype(benchmarkDataset.messageName); protoType.decode(onePayload); - }); + }); }) .add("protobuf.js static encoding", function() { var protoType = getNewPrototype(benchmarkDataset.messageName); messageList.forEach(function(message) { protoType.encode(message).finish(); }); - }) + }) .run({"Async": false}); results.push({ @@ -56,9 +56,9 @@ process.argv.forEach(function(filename, index) { } }) - console.log("Throughput for decoding: " + console.log("Throughput for decoding: " + senarios.benches[0] * totalBytes / 1024 / 1024 + "MB/s" ); - console.log("Throughput for encoding: " + console.log("Throughput for encoding: " + senarios.benches[1] * totalBytes / 1024 / 1024 + "MB/s" ); console.log(""); }); diff --git a/benchmarks/python/py_benchmark.py b/benchmarks/python/py_benchmark.py index e86b61e73c..60e377e4b8 100755 --- a/benchmarks/python/py_benchmark.py +++ b/benchmarks/python/py_benchmark.py @@ -8,7 +8,7 @@ import fnmatch import json parser = argparse.ArgumentParser(description="Python protobuf benchmark") -parser.add_argument("data_files", metavar="dataFile", nargs="+", +parser.add_argument("data_files", metavar="dataFile", nargs="+", help="testing data files.") parser.add_argument("--json", action="store_const", dest="json", const="yes", default="no", @@ -41,12 +41,16 @@ import benchmarks_pb2 as benchmarks_pb2 def run_one_test(filename): - data = open(filename).read() + data = open(filename, "rb").read() benchmark_dataset = benchmarks_pb2.BenchmarkDataset() benchmark_dataset.ParseFromString(data) + total_bytes = 0 + for payload in benchmark_dataset.payload: + total_bytes += len(payload) benchmark_util = Benchmark(full_iteration=len(benchmark_dataset.payload), module="py_benchmark", - setup_method="init") + setup_method="init", + total_bytes=total_bytes) result={} result["filename"] = filename result["message_name"] = benchmark_dataset.message_name @@ -61,10 +65,11 @@ def run_one_test(filename): def init(filename): - global benchmark_dataset, message_class, message_list, counter + global benchmark_dataset, message_class, message_list, counter, total_bytes message_list=[] counter = 0 - data = open(os.path.dirname(sys.argv[0]) + "/../" + filename).read() + total_bytes = 0 + data = open(filename, "rb").read() benchmark_dataset = benchmarks_pb2.BenchmarkDataset() benchmark_dataset.ParseFromString(data) @@ -85,6 +90,7 @@ def init(filename): temp = message_class() temp.ParseFromString(one_payload) message_list.append(temp) + total_bytes += len(one_payload) def parse_from_benchmark(): @@ -101,11 +107,12 @@ def serialize_to_benchmark(): class Benchmark: def __init__(self, module=None, test_method=None, - setup_method=None, full_iteration = 1): + setup_method=None, total_bytes=None, full_iteration = 1): self.full_iteration = full_iteration self.module = module self.test_method = test_method self.setup_method = setup_method + self.total_bytes = total_bytes def set_test_method(self, test_method): self.test_method = test_method @@ -127,27 +134,28 @@ class Benchmark: t = self.dry_run(test_method_args, setup_method_args); if t < 3 : reps = int(math.ceil(3 / t)) * self.full_iteration - t = timeit.timeit(stmt="%s(%s)" % (self.test_method, test_method_args), - setup=self.full_setup_code(setup_method_args), - number=reps); - return 1.0 * t / reps * (10 ** 9) - + if reps != self.full_iteration: + t = timeit.timeit(stmt="%s(%s)" % (self.test_method, test_method_args), + setup=self.full_setup_code(setup_method_args), + number=reps); + return self.total_bytes * 1.0 / 2 ** 20 / (1.0 * t / reps * self.full_iteration) + if __name__ == "__main__": results = [] for file in args.data_files: results.append(run_one_test(file)) - + if args.json != "no": print(json.dumps(results)) else: for result in results: print("Message %s of dataset file %s" % \ (result["message_name"], result["filename"])) - print("Average time for parse_from_benchmark: %.2f ns" % \ + print("Average throughput for parse_from_benchmark: %.2f MB/s" % \ (result["benchmarks"][ \ args.behavior_prefix + "_parse_from_benchmark"])) - print("Average time for serialize_to_benchmark: %.2f ns" % \ + print("Average throughput for serialize_to_benchmark: %.2f MB/s" % \ (result["benchmarks"][ \ args.behavior_prefix + "_serialize_to_benchmark"])) print("") diff --git a/benchmarks/util/result_parser.py b/benchmarks/util/result_parser.py index a843923aae..b09f387a6b 100755 --- a/benchmarks/util/result_parser.py +++ b/benchmarks/util/result_parser.py @@ -18,7 +18,7 @@ def __get_data_size(filename): return __file_size_map[filename] benchmark_dataset = benchmarks_pb2.BenchmarkDataset() benchmark_dataset.ParseFromString( - open(filename).read()) + open(filename, "rb").read()) size = 0 count = 0 for payload in benchmark_dataset.payload: @@ -29,7 +29,7 @@ def __get_data_size(filename): def __extract_file_name(file_name): - name_list = re.split("[/\.]", file_name) + name_list = re.split(r"[/\.]", file_name) short_file_name = "" for name in name_list: if name[:14] == "google_message": @@ -45,9 +45,10 @@ __results = [] # "benchmarks": [ # { # "bytes_per_second": int, -# "cpu_time": int, +# "cpu_time_ns": double, +# "iterations": int, # "name: string, -# "time_unit: string, +# "real_time_ns: double, # ... # }, # ... @@ -59,7 +60,7 @@ def __parse_cpp_result(filename): return if filename[0] != '/': filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename - with open(filename) as f: + with open(filename, "rb") as f: results = json.loads(f.read()) for benchmark in results["benchmarks"]: data_filename = "".join( @@ -75,6 +76,36 @@ def __parse_cpp_result(filename): }) +# Synthetic benchmark results example: +# [ +# "benchmarks": [ +# { +# "cpu_time_ns": double, +# "iterations": int, +# "name: string, +# "real_time_ns: double, +# ... +# }, +# ... +# ], +# ... +# ] +def __parse_synthetic_result(filename): + if filename == "": + return + if filename[0] != "/": + filename = os.path.dirname(os.path.abspath(__file__)) + "/" + filename + with open(filename, "rb") as f: + results = json.loads(f.read()) + for benchmark in results["benchmarks"]: + __results.append({ + "language": "cpp", + "dataFilename": "", + "behavior": "synthetic", + "throughput": 10.0**9 / benchmark["cpu_time_ns"] + }) + + # Python results example: # [ # [ @@ -84,7 +115,6 @@ def __parse_cpp_result(filename): # behavior: results, # ... # }, -# "message_name": STRING # }, # ... # ], #pure-python @@ -95,7 +125,7 @@ def __parse_python_result(filename): return if filename[0] != '/': filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename - with open(filename) as f: + with open(filename, "rb") as f: results_list = json.loads(f.read()) for results in results_list: for result in results: @@ -105,8 +135,7 @@ def __parse_python_result(filename): "language": "python", "dataFilename": __extract_file_name(result["filename"]), "behavior": behavior, - "throughput": avg_size / - result["benchmarks"][behavior] * 1e9 / 2 ** 20 + "throughput": result["benchmarks"][behavior] }) @@ -146,7 +175,7 @@ def __parse_java_result(filename): return if filename[0] != '/': filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename - with open(filename) as f: + with open(filename, "rb") as f: results = json.loads(f.read()) for result in results: total_weight = 0 @@ -182,14 +211,14 @@ def __parse_go_result(filename): return if filename[0] != '/': filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename - with open(filename) as f: + with open(filename, "rb") as f: for line in f: - result_list = re.split("[\ \t]+", line) + result_list = re.split(r"[\ \t]+", line) if result_list[0][:9] != "Benchmark": continue first_slash_index = result_list[0].find('/') last_slash_index = result_list[0].rfind('/') - full_filename = result_list[0][first_slash_index+4:last_slash_index] # delete ../ prefix + full_filename = result_list[0][first_slash_index+1:last_slash_index] total_bytes, _ = __get_data_size(full_filename) behavior_with_suffix = result_list[0][last_slash_index+1:] last_dash = behavior_with_suffix.rfind("-") @@ -204,7 +233,52 @@ def __parse_go_result(filename): "language": "go" }) -def get_result_from_file(cpp_file="", java_file="", python_file="", go_file=""): + +# Self built json results example: +# +# [ +# { +# "filename": string, +# "benchmarks": { +# behavior: results, +# ... +# }, +# }, +# ... +# ] +def __parse_custom_result(filename, language): + if filename == "": + return + if filename[0] != '/': + filename = os.path.dirname(os.path.abspath(__file__)) + '/' + filename + with open(filename, "rb") as f: + results = json.loads(f.read()) + for result in results: + _, avg_size = __get_data_size(result["filename"]) + for behavior in result["benchmarks"]: + __results.append({ + "language": language, + "dataFilename": __extract_file_name(result["filename"]), + "behavior": behavior, + "throughput": result["benchmarks"][behavior] + }) + + +def __parse_js_result(filename, language): + return __parse_custom_result(filename, language) + +def __parse_php_result(filename, language): + return __parse_custom_result(filename, language) + + +def get_result_from_file(cpp_file="", + java_file="", + python_file="", + go_file="", + synthetic_file="", + node_file="", + php_c_file="", + php_file=""): results = {} if cpp_file != "": __parse_cpp_result(cpp_file) @@ -214,5 +288,13 @@ def get_result_from_file(cpp_file="", java_file="", python_file="", go_file=""): __parse_python_result(python_file) if go_file != "": __parse_go_result(go_file) + if synthetic_file != "": + __parse_synthetic_result(synthetic_file) + if node_file != "": + __parse_js_result(node_file, "node") + if php_file != "": + __parse_php_result(php_file, "php") + if php_c_file != "": + __parse_php_result(php_c_file, "php") - return __results \ No newline at end of file + return __results diff --git a/benchmarks/util/result_uploader.py b/benchmarks/util/result_uploader.py index a667da0252..2a35d9694d 100755 --- a/benchmarks/util/result_uploader.py +++ b/benchmarks/util/result_uploader.py @@ -59,12 +59,13 @@ def upload_result(result_list, metadata): labels_string += ",|%s:%s|" % (key, result[key]) new_result["labels"] = labels_string[1:] new_result["timestamp"] = _INITIAL_TIME + print(labels_string) bq = big_query_utils.create_big_query() row = big_query_utils.make_row(str(uuid.uuid4()), new_result) if not big_query_utils.insert_rows(bq, _PROJECT_ID, _DATASET, - _TABLE + "$" + _NOW, - [row]): + _TABLE + "$" + _NOW, + [row]): print('Error when uploading result', new_result) @@ -82,6 +83,15 @@ if __name__ == "__main__": parser.add_argument("-go", "--go_input_file", help="The golang benchmark result file's name", default="") + parser.add_argument("-node", "--node_input_file", + help="The node.js benchmark result file's name", + default="") + parser.add_argument("-php", "--php_input_file", + help="The pure php benchmark result file's name", + default="") + parser.add_argument("-php_c", "--php_c_input_file", + help="The php with c ext benchmark result file's name", + default="") args = parser.parse_args() metadata = get_metadata() @@ -90,5 +100,8 @@ if __name__ == "__main__": cpp_file=args.cpp_input_file, java_file=args.java_input_file, python_file=args.python_input_file, - go_file=args.go_input_file - ), metadata) \ No newline at end of file + go_file=args.go_input_file, + node_file=args.node_input_file, + php_file=args.php_input_file, + php_c_file=args.php_c_input_file, + ), metadata) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index ece39f7fa7..f87b0928e0 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -30,6 +30,7 @@ endif() # Options option(protobuf_BUILD_TESTS "Build tests" ON) +option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF) option(protobuf_BUILD_EXAMPLES "Build examples" OFF) option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON) if (BUILD_SHARED_LIBS) @@ -67,23 +68,28 @@ string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\2" string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\3" protobuf_CONTACT "${protobuf_AC_INIT_LINE}") # Parse version tweaks -set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)-?(.*)$") +set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$") string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1" protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}") string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2" protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}") string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3" protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}") -string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\4" +string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\5" protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}") +message(STATUS "${protobuf_VERSION_PRERELEASE}") + # Package version set(protobuf_VERSION "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}") if(protobuf_VERSION_PRERELEASE) - set(protobuf_VERSION "${protobuf_VERSION}-${protobuf_VERSION_PRERELEASE}") + set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}") +else() + set(protobuf_VERSION "${protobuf_VERSION}.0") endif() +message(STATUS "${protobuf_VERSION}") if(protobuf_VERBOSE) message(STATUS "Configuration script parsing status [") @@ -126,6 +132,25 @@ if (HAVE_ZLIB) add_definitions(-DHAVE_ZLIB) endif (HAVE_ZLIB) +# We need to link with libatomic on systems that do not have builtin atomics, or +# don't have builtin support for 8 byte atomics +set(protobuf_LINK_LIBATOMIC false) +if (NOT MSVC) + include(CheckCXXSourceCompiles) + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++11) + check_cxx_source_compiles(" + #include + int main() { + return std::atomic{}; + } + " protobuf_HAVE_BUILTIN_ATOMICS) + if (NOT protobuf_HAVE_BUILTIN_ATOMICS) + set(protobuf_LINK_LIBATOMIC true) + endif (NOT protobuf_HAVE_BUILTIN_ATOMICS) + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) +endif (NOT MSVC) + if (protobuf_BUILD_SHARED_LIBS) set(protobuf_SHARED_OR_STATIC "SHARED") else (protobuf_BUILD_SHARED_LIBS) @@ -218,6 +243,10 @@ if (protobuf_BUILD_TESTS) include(tests.cmake) endif (protobuf_BUILD_TESTS) +if (protobuf_BUILD_CONFORMANCE) + include(conformance.cmake) +endif (protobuf_BUILD_CONFORMANCE) + include(install.cmake) if (protobuf_BUILD_EXAMPLES) diff --git a/cmake/README.md b/cmake/README.md index 26a516c7dd..96aaf4f97d 100644 --- a/cmake/README.md +++ b/cmake/README.md @@ -43,7 +43,7 @@ Getting Sources You can get the latest stable source packages from the release page: - https://github.com/google/protobuf/releases/latest + https://github.com/protocolbuffers/protobuf/releases/latest For example: if you only need C++, download `protobuf-cpp-[VERSION].tar.gz`; if you need C++ and Java, download `protobuf-java-[VERSION].tar.gz` (every package @@ -52,7 +52,7 @@ download `protobuf-all-[VERSION].tar.gz`. Or you can use git to clone from protobuf git repository. - C:\Path\to> git clone -b [release_tag] https://github.com/google/protobuf.git + C:\Path\to> git clone -b [release_tag] https://github.com/protocolbuffers/protobuf.git Where *[release_tag]* is a git tag like *v3.0.0-beta-1* or a branch name like *master* if you want to get the latest code. @@ -130,6 +130,13 @@ It will generate *Visual Studio* solution file *protobuf.sln* in current directo If the *gmock* directory does not exist, and you do not want to build protobuf unit tests, you need to add *cmake* command argument `-Dprotobuf_BUILD_TESTS=OFF` to disable testing. +To make a *Visual Studio* file for Visual Studio 15 2017, create the *Visual Studio* +solution file above and edit the CmakeCache file. + + C:Path\to\protobuf\cmake\build\solution\CMakeCache + +Then create the *Visual Studio* solution file again + Compiling ========= @@ -170,9 +177,9 @@ You should see output similar to: Running main() from gmock_main.cc [==========] Running 1546 tests from 165 test cases. - + ... - + [==========] 1546 tests from 165 test cases ran. (2529 ms total) [ PASSED ] 1546 tests. @@ -191,7 +198,7 @@ To run specific tests: [ RUN ] AnyTest.TestIs [ OK ] AnyTest.TestIs (0 ms) [----------] 3 tests from AnyTest (1 ms total) - + [----------] Global test environment tear-down [==========] 3 tests from 1 test case ran. (2 ms total) [ PASSED ] 3 tests. @@ -303,7 +310,7 @@ If you already have ZLIB library and headers at some other location on your syst -DZLIB_INCLUDE_DIR= -DZLIB_LIB= - + Build and testing protobuf as usual. Notes on Compiler Warnings diff --git a/cmake/conformance.cmake b/cmake/conformance.cmake new file mode 100644 index 0000000000..82b4cf580a --- /dev/null +++ b/cmake/conformance.cmake @@ -0,0 +1,50 @@ + +add_custom_command( + OUTPUT ${protobuf_source_dir}/conformance/conformance.pb.cc + DEPENDS protoc ${protobuf_source_dir}/conformance/conformance.proto + COMMAND protoc ${protobuf_source_dir}/conformance/conformance.proto + --proto_path=${protobuf_source_dir}/conformance + --cpp_out=${protobuf_source_dir}/conformance +) + +add_custom_command( + OUTPUT ${protobuf_source_dir}/src/google/protobuf/test_messages_proto3.pb.cc + ${protobuf_source_dir}/src/google/protobuf/test_messages_proto2.pb.cc + DEPENDS protoc ${protobuf_source_dir}/src/google/protobuf/test_messages_proto3.proto + protoc ${protobuf_source_dir}/src/google/protobuf/test_messages_proto2.proto + COMMAND protoc ${protobuf_source_dir}/src/google/protobuf/test_messages_proto3.proto + ${protobuf_source_dir}/src/google/protobuf/test_messages_proto2.proto + --proto_path=${protobuf_source_dir}/src + --cpp_out=${protobuf_source_dir}/src +) + +add_executable(conformance_test_runner + ${protobuf_source_dir}/conformance/conformance.pb.cc + ${protobuf_source_dir}/conformance/conformance_test.cc + ${protobuf_source_dir}/conformance/binary_json_conformance_main.cc + ${protobuf_source_dir}/conformance/binary_json_conformance_suite.cc + ${protobuf_source_dir}/conformance/binary_json_conformance_suite.h + ${protobuf_source_dir}/conformance/conformance_test_runner.cc + ${protobuf_source_dir}/conformance/third_party/jsoncpp/json.h + ${protobuf_source_dir}/conformance/third_party/jsoncpp/jsoncpp.cpp + ${protobuf_source_dir}/src/google/protobuf/test_messages_proto3.pb.cc + ${protobuf_source_dir}/src/google/protobuf/test_messages_proto2.pb.cc +) + +add_executable(conformance_cpp + ${protobuf_source_dir}/conformance/conformance.pb.cc + ${protobuf_source_dir}/conformance/conformance_cpp.cc + ${protobuf_source_dir}/src/google/protobuf/test_messages_proto3.pb.cc + ${protobuf_source_dir}/src/google/protobuf/test_messages_proto2.pb.cc +) + +target_include_directories( + conformance_test_runner + PUBLIC ${protobuf_source_dir}/conformance) + +target_include_directories( + conformance_cpp + PUBLIC ${protobuf_source_dir}/conformance) + +target_link_libraries(conformance_test_runner libprotobuf) +target_link_libraries(conformance_cpp libprotobuf) diff --git a/cmake/extract_includes.bat.in b/cmake/extract_includes.bat.in index 3818c4ef5b..c9a1524b12 100644 --- a/cmake/extract_includes.bat.in +++ b/cmake/extract_includes.bat.in @@ -44,6 +44,7 @@ copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\duration.pb.h" includ copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\dynamic_message.h" include\google\protobuf\dynamic_message.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\empty.pb.h" include\google\protobuf\empty.pb.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\extension_set.h" include\google\protobuf\extension_set.h +copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\extension_set_inl.h" include\google\protobuf\extension_set_inl.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\field_mask.pb.h" include\google\protobuf\field_mask.pb.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\generated_enum_reflection.h" include\google\protobuf\generated_enum_reflection.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\generated_enum_util.h" include\google\protobuf\generated_enum_util.h @@ -72,6 +73,7 @@ copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\message.h" include\go copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\message_lite.h" include\google\protobuf\message_lite.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\metadata.h" include\google\protobuf\metadata.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\metadata_lite.h" include\google\protobuf\metadata_lite.h +copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\parse_context.h" include\google\protobuf\parse_context.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\port.h" include\google\protobuf\port.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\port_def.inc" include\google\protobuf\port_def.inc copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\port_undef.inc" include\google\protobuf\port_undef.inc @@ -87,16 +89,17 @@ copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\casts.h" includ copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\common.h" include\google\protobuf\stubs\common.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\fastmem.h" include\google\protobuf\stubs\fastmem.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\hash.h" include\google\protobuf\stubs\hash.h +copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\io_win32.h" include\google\protobuf\stubs\io_win32.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\logging.h" include\google\protobuf\stubs\logging.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\macros.h" include\google\protobuf\stubs\macros.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\mutex.h" include\google\protobuf\stubs\mutex.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\once.h" include\google\protobuf\stubs\once.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\platform_macros.h" include\google\protobuf\stubs\platform_macros.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\port.h" include\google\protobuf\stubs\port.h -copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\singleton.h" include\google\protobuf\stubs\singleton.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\status.h" include\google\protobuf\stubs\status.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\stl_util.h" include\google\protobuf\stubs\stl_util.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\stringpiece.h" include\google\protobuf\stubs\stringpiece.h +copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\strutil.h" include\google\protobuf\stubs\strutil.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\stubs\template_util.h" include\google\protobuf\stubs\template_util.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\text_format.h" include\google\protobuf\text_format.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\timestamp.pb.h" include\google\protobuf\timestamp.pb.h @@ -112,7 +115,6 @@ copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\util\type_resolver.h" copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\util\type_resolver_util.h" include\google\protobuf\util\type_resolver_util.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\wire_format.h" include\google\protobuf\wire_format.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\wire_format_lite.h" include\google\protobuf\wire_format_lite.h -copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\wire_format_lite_inl.h" include\google\protobuf\wire_format_lite_inl.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\wrappers.pb.h" include\google\protobuf\wrappers.pb.h copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\any.proto" include\google\protobuf\any.proto copy "${PROTOBUF_SOURCE_WIN32_PATH}\..\src\google\protobuf\api.proto" include\google\protobuf\api.proto diff --git a/cmake/install.cmake b/cmake/install.cmake index 9b2ae93cf6..be47c54a1e 100644 --- a/cmake/install.cmake +++ b/cmake/install.cmake @@ -33,7 +33,7 @@ if (protobuf_BUILD_PROTOC_BINARIES) RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc) if (UNIX AND NOT APPLE) set_property(TARGET protoc - PROPERTY INSTALL_RPATH "$ORIGIN/../lib") + PROPERTY INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}") elseif (APPLE) set_property(TARGET protoc PROPERTY INSTALL_RPATH "@loader_path/../lib") diff --git a/cmake/libprotobuf-lite.cmake b/cmake/libprotobuf-lite.cmake index 1d0b0b848a..39c39c1310 100644 --- a/cmake/libprotobuf-lite.cmake +++ b/cmake/libprotobuf-lite.cmake @@ -1,19 +1,23 @@ set(libprotobuf_lite_files + ${protobuf_source_dir}/src/google/protobuf/any_lite.cc ${protobuf_source_dir}/src/google/protobuf/arena.cc - ${protobuf_source_dir}/src/google/protobuf/arenastring.cc ${protobuf_source_dir}/src/google/protobuf/extension_set.cc + ${protobuf_source_dir}/src/google/protobuf/generated_enum_util.cc ${protobuf_source_dir}/src/google/protobuf/generated_message_table_driven_lite.cc ${protobuf_source_dir}/src/google/protobuf/generated_message_util.cc ${protobuf_source_dir}/src/google/protobuf/implicit_weak_message.cc ${protobuf_source_dir}/src/google/protobuf/io/coded_stream.cc + ${protobuf_source_dir}/src/google/protobuf/io/io_win32.cc + ${protobuf_source_dir}/src/google/protobuf/io/strtod.cc ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream.cc + ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl.cc ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl_lite.cc ${protobuf_source_dir}/src/google/protobuf/message_lite.cc + ${protobuf_source_dir}/src/google/protobuf/parse_context.cc ${protobuf_source_dir}/src/google/protobuf/repeated_field.cc ${protobuf_source_dir}/src/google/protobuf/stubs/bytestream.cc ${protobuf_source_dir}/src/google/protobuf/stubs/common.cc ${protobuf_source_dir}/src/google/protobuf/stubs/int128.cc - ${protobuf_source_dir}/src/google/protobuf/stubs/io_win32.cc ${protobuf_source_dir}/src/google/protobuf/stubs/status.cc ${protobuf_source_dir}/src/google/protobuf/stubs/statusor.cc ${protobuf_source_dir}/src/google/protobuf/stubs/stringpiece.cc @@ -30,8 +34,11 @@ set(libprotobuf_lite_includes ${protobuf_source_dir}/src/google/protobuf/extension_set.h ${protobuf_source_dir}/src/google/protobuf/generated_message_util.h ${protobuf_source_dir}/src/google/protobuf/implicit_weak_message.h + ${protobuf_source_dir}/src/google/protobuf/parse_context.h ${protobuf_source_dir}/src/google/protobuf/io/coded_stream.h + ${protobuf_source_dir}/src/google/protobuf/io/strtod.h ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream.h + ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl.h ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl_lite.h ${protobuf_source_dir}/src/google/protobuf/message_lite.h ${protobuf_source_dir}/src/google/protobuf/repeated_field.h @@ -57,6 +64,9 @@ endif() add_library(libprotobuf-lite ${protobuf_SHARED_OR_STATIC} ${libprotobuf_lite_files} ${libprotobuf_lite_includes} ${libprotobuf_lite_rc_files}) target_link_libraries(libprotobuf-lite ${CMAKE_THREAD_LIBS_INIT}) +if(protobuf_LINK_LIBATOMIC) + target_link_libraries(libprotobuf-lite atomic) +endif() target_include_directories(libprotobuf-lite PUBLIC ${protobuf_source_dir}/src) if(MSVC AND protobuf_BUILD_SHARED_LIBS) target_compile_definitions(libprotobuf-lite diff --git a/cmake/libprotobuf.cmake b/cmake/libprotobuf.cmake index bd570be7d9..710b34c6da 100644 --- a/cmake/libprotobuf.cmake +++ b/cmake/libprotobuf.cmake @@ -16,9 +16,7 @@ set(libprotobuf_files ${protobuf_source_dir}/src/google/protobuf/generated_message_table_driven.cc ${protobuf_source_dir}/src/google/protobuf/io/gzip_stream.cc ${protobuf_source_dir}/src/google/protobuf/io/printer.cc - ${protobuf_source_dir}/src/google/protobuf/io/strtod.cc ${protobuf_source_dir}/src/google/protobuf/io/tokenizer.cc - ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl.cc ${protobuf_source_dir}/src/google/protobuf/map_field.cc ${protobuf_source_dir}/src/google/protobuf/message.cc ${protobuf_source_dir}/src/google/protobuf/reflection_ops.cc @@ -72,9 +70,7 @@ set(libprotobuf_includes ${protobuf_source_dir}/src/google/protobuf/generated_message_reflection.h ${protobuf_source_dir}/src/google/protobuf/io/gzip_stream.h ${protobuf_source_dir}/src/google/protobuf/io/printer.h - ${protobuf_source_dir}/src/google/protobuf/io/strtod.h ${protobuf_source_dir}/src/google/protobuf/io/tokenizer.h - ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl.h ${protobuf_source_dir}/src/google/protobuf/map_field.h ${protobuf_source_dir}/src/google/protobuf/message.h ${protobuf_source_dir}/src/google/protobuf/reflection_ops.h @@ -122,7 +118,10 @@ add_library(libprotobuf ${protobuf_SHARED_OR_STATIC} ${libprotobuf_lite_files} ${libprotobuf_files} ${libprotobuf_includes} ${libprotobuf_rc_files}) target_link_libraries(libprotobuf ${CMAKE_THREAD_LIBS_INIT}) if(protobuf_WITH_ZLIB) - target_link_libraries(libprotobuf ${ZLIB_LIBRARIES}) + target_link_libraries(libprotobuf ${ZLIB_LIBRARIES}) +endif() +if(protobuf_LINK_LIBATOMIC) + target_link_libraries(libprotobuf atomic) endif() target_include_directories(libprotobuf PUBLIC ${protobuf_source_dir}/src) if(MSVC AND protobuf_BUILD_SHARED_LIBS) diff --git a/cmake/libprotoc.cmake b/cmake/libprotoc.cmake index 850a85a675..b71f2f1ba9 100644 --- a/cmake/libprotoc.cmake +++ b/cmake/libprotoc.cmake @@ -78,7 +78,6 @@ set(libprotoc_files ${protobuf_source_dir}/src/google/protobuf/compiler/plugin.pb.cc ${protobuf_source_dir}/src/google/protobuf/compiler/python/python_generator.cc ${protobuf_source_dir}/src/google/protobuf/compiler/ruby/ruby_generator.cc - ${protobuf_source_dir}/src/google/protobuf/compiler/scc.cc ${protobuf_source_dir}/src/google/protobuf/compiler/subprocess.cc ${protobuf_source_dir}/src/google/protobuf/compiler/zip_writer.cc ) @@ -152,6 +151,7 @@ set(libprotoc_headers ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_map_field.h ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_message.h ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_message_field.h + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_nsobject_methods.h ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_oneof.h ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.h ${protobuf_source_dir}/src/google/protobuf/compiler/scc.h diff --git a/cmake/protobuf-config.cmake.in b/cmake/protobuf-config.cmake.in index 69d63e3eb5..29e39d88af 100644 --- a/cmake/protobuf-config.cmake.in +++ b/cmake/protobuf-config.cmake.in @@ -37,7 +37,7 @@ function(protobuf_generate) if(NOT protobuf_generate_PROTOC_OUT_DIR) set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) endif() - + if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp) set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:") endif() @@ -105,7 +105,7 @@ function(protobuf_generate) add_custom_command( OUTPUT ${_generated_srcs} COMMAND protobuf::protoc - ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR}/${_rel_dir} ${_protobuf_include_path} ${_abs_file} + ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_protobuf_include_path} ${_abs_file} DEPENDS ${_abs_file} protobuf::protoc COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}" VERBATIM ) diff --git a/cmake/protoc.cmake b/cmake/protoc.cmake index bb160f479e..f90e525e80 100644 --- a/cmake/protoc.cmake +++ b/cmake/protoc.cmake @@ -9,7 +9,7 @@ set(protoc_rc_files endif() add_executable(protoc ${protoc_files} ${protoc_rc_files}) -target_link_libraries(protoc libprotobuf libprotoc) +target_link_libraries(protoc libprotoc libprotobuf) add_executable(protobuf::protoc ALIAS protoc) set_target_properties(protoc PROPERTIES diff --git a/cmake/tests.cmake b/cmake/tests.cmake index f4679d1a1e..5c25acc68a 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -114,7 +114,7 @@ endforeach(proto_file) set(common_test_files ${protobuf_source_dir}/src/google/protobuf/arena_test_util.cc - ${protobuf_source_dir}/src/google/protobuf/map_test_util.cc + ${protobuf_source_dir}/src/google/protobuf/map_test_util.inc ${protobuf_source_dir}/src/google/protobuf/test_util.cc ${protobuf_source_dir}/src/google/protobuf/test_util.inc ${protobuf_source_dir}/src/google/protobuf/testing/file.cc @@ -156,6 +156,7 @@ set(tests_files ${protobuf_source_dir}/src/google/protobuf/extension_set_unittest.cc ${protobuf_source_dir}/src/google/protobuf/generated_message_reflection_unittest.cc ${protobuf_source_dir}/src/google/protobuf/io/coded_stream_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/io/io_win32_unittest.cc ${protobuf_source_dir}/src/google/protobuf/io/printer_unittest.cc ${protobuf_source_dir}/src/google/protobuf/io/tokenizer_unittest.cc ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_unittest.cc @@ -175,7 +176,6 @@ set(tests_files ${protobuf_source_dir}/src/google/protobuf/stubs/bytestream_unittest.cc ${protobuf_source_dir}/src/google/protobuf/stubs/common_unittest.cc ${protobuf_source_dir}/src/google/protobuf/stubs/int128_unittest.cc - ${protobuf_source_dir}/src/google/protobuf/stubs/io_win32_unittest.cc ${protobuf_source_dir}/src/google/protobuf/stubs/status_test.cc ${protobuf_source_dir}/src/google/protobuf/stubs/statusor_test.cc ${protobuf_source_dir}/src/google/protobuf/stubs/stringpiece_unittest.cc diff --git a/cmake/version.rc.in b/cmake/version.rc.in index cbce1e5389..f6e7c4a3f9 100644 --- a/cmake/version.rc.in +++ b/cmake/version.rc.in @@ -15,15 +15,15 @@ VS_VERSION_INFO VERSIONINFO - FILEVERSION @protobuf_RC_FILEVERSION@,0 - PRODUCTVERSION @protobuf_RC_FILEVERSION@,0 + FILEVERSION @protobuf_RC_FILEVERSION@ + PRODUCTVERSION @protobuf_RC_FILEVERSION@ FILEFLAGSMASK VS_FFI_FILEFLAGSMASK FILEFLAGS VER_DEBUG FILEOS VOS__WINDOWS32 FILETYPE VFT_DLL BEGIN BLOCK "VarFileInfo" - BEGIN + BEGIN // English language (0x409) and the Windows Unicode codepage (1200) VALUE "Translation", 0x409, 1200 END diff --git a/compiler_config_setting.bzl b/compiler_config_setting.bzl new file mode 100644 index 0000000000..5e52a6524f --- /dev/null +++ b/compiler_config_setting.bzl @@ -0,0 +1,21 @@ +"""Creates config_setting that allows selecting based on 'compiler' value.""" + +def create_compiler_config_setting(name, value): + # The "do_not_use_tools_cpp_compiler_present" attribute exists to + # distinguish between older versions of Bazel that do not support + # "@bazel_tools//tools/cpp:compiler" flag_value, and newer ones that do. + # In the future, the only way to select on the compiler will be through + # flag_values{"@bazel_tools//tools/cpp:compiler"} and the else branch can + # be removed. + if hasattr(cc_common, "do_not_use_tools_cpp_compiler_present"): + native.config_setting( + name = name, + flag_values = { + "@bazel_tools//tools/cpp:compiler": value, + }, + ) + else: + native.config_setting( + name = name, + values = {"compiler": value}, + ) diff --git a/configure.ac b/configure.ac index 8e1c308ce1..ca80e8cb08 100644 --- a/configure.ac +++ b/configure.ac @@ -17,7 +17,7 @@ AC_PREREQ(2.59) # In the SVN trunk, the version should always be the next anticipated release # version with the "-pre" suffix. (We used to use "-SNAPSHOT" but this pushed # the size of one file name in the dist tarfile over the 99-char limit.) -AC_INIT([Protocol Buffers],[3.6.1],[protobuf@googlegroups.com],[protobuf]) +AC_INIT([Protocol Buffers],[3.9.0-rc-1],[protobuf@googlegroups.com],[protobuf]) AM_MAINTAINER_MODE([enable]) @@ -47,6 +47,11 @@ AC_CANONICAL_TARGET AM_INIT_AUTOMAKE([1.9 tar-ustar subdir-objects]) +# Silent rules enabled: the output is minimal but informative. +# In particular, the warnings from the compiler stick out very clearly. +# To see all logs, use the --disable-silent-rules on configure or via make V=1 +AM_SILENT_RULES([yes]) + AC_ARG_WITH([zlib], [AS_HELP_STRING([--with-zlib], [include classes for streaming compressed data in and out @<:@default=check@:>@])], @@ -165,6 +170,26 @@ AS_IF([test "$with_zlib" != no], [ ]) AM_CONDITIONAL([HAVE_ZLIB], [test $HAVE_ZLIB = 1]) +# Add -std=c++11 if necesssary. It is important for us to do this before the +# libatomic check below, since that also depends on C++11. +AX_CXX_COMPILE_STDCXX([11], [noext], [mandatory]) + +dnl On some platforms, std::atomic needs a helper library +AC_MSG_CHECKING(whether -latomic is needed) +AC_LINK_IFELSE([AC_LANG_SOURCE([[ + #include + #include + std::atomic v; + int main() { + return v; + } +]])], STD_ATOMIC_NEED_LIBATOMIC=no, STD_ATOMIC_NEED_LIBATOMIC=yes) +AC_MSG_RESULT($STD_ATOMIC_NEED_LIBATOMIC) +if test "x$STD_ATOMIC_NEED_LIBATOMIC" = xyes; then + LIBATOMIC_LIBS="-latomic" +fi +AC_SUBST([LIBATOMIC_LIBS]) + AS_IF([test "$with_protoc" != "no"], [ PROTOC=$with_protoc AS_IF([test "$with_protoc" = "yes"], [ @@ -189,15 +214,6 @@ AM_CONDITIONAL([HAVE_PTHREAD], [test "x$ax_pthread_ok" = "xyes"]) # We still keep this for improving pbconfig.h for unsupported platforms. AC_CXX_STL_HASH -case "$target_os" in - mingw* | cygwin* | win* | aix* | *android* ) - ;; - *) - # Need to link against rt on Solaris - AC_SEARCH_LIBS([sched_yield], [rt], [], [AC_MSG_FAILURE([sched_yield was not found on your system])]) - ;; -esac - # Enable ObjC support for conformance directory on OS X. OBJC_CONFORMANCE_TEST=0 case "$target_os" in @@ -207,8 +223,6 @@ case "$target_os" in esac AM_CONDITIONAL([OBJC_CONFORMANCE_TEST], [test $OBJC_CONFORMANCE_TEST = 1]) -AX_CXX_COMPILE_STDCXX([11], [noext], [mandatory]) - # HACK: Make gmock's configure script pick up our copy of CFLAGS and CXXFLAGS, # since the flags added by ACX_CHECK_SUNCC must be used when compiling gmock # too. diff --git a/conformance/ConformanceJava.java b/conformance/ConformanceJava.java index d1ce4059f6..c5b3f4e163 100644 --- a/conformance/ConformanceJava.java +++ b/conformance/ConformanceJava.java @@ -1,16 +1,17 @@ -import com.google.protobuf.ByteString; import com.google.protobuf.AbstractMessage; -import com.google.protobuf.Parser; +import com.google.protobuf.ByteString; import com.google.protobuf.CodedInputStream; -import com.google.protobuf.conformance.Conformance; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf_test_messages.proto3.TestMessagesProto3; -import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3; -import com.google.protobuf_test_messages.proto2.TestMessagesProto2; -import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2; import com.google.protobuf.ExtensionRegistry; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.Parser; +import com.google.protobuf.TextFormat; +import com.google.protobuf.conformance.Conformance; import com.google.protobuf.util.JsonFormat; import com.google.protobuf.util.JsonFormat.TypeRegistry; +import com.google.protobuf_test_messages.proto2.TestMessagesProto2; +import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2; +import com.google.protobuf_test_messages.proto3.TestMessagesProto3; +import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -56,7 +57,7 @@ class ConformanceJava { buf[3] = (byte)(val >> 24); writeToStdout(buf); } - + private enum BinaryDecoderType { BTYE_STRING_DECODER, BYTE_ARRAY_DECODER, @@ -68,11 +69,11 @@ class ConformanceJava { } private static class BinaryDecoder { - public MessageType decode (ByteString bytes, BinaryDecoderType type, + public MessageType decode (ByteString bytes, BinaryDecoderType type, Parser parser, ExtensionRegistry extensions) throws InvalidProtocolBufferException { switch (type) { - case BTYE_STRING_DECODER: + case BTYE_STRING_DECODER: return parser.parseFrom(bytes, extensions); case BYTE_ARRAY_DECODER: return parser.parseFrom(bytes.toByteArray(), extensions); @@ -93,7 +94,7 @@ class ConformanceJava { } catch (InvalidProtocolBufferException e) { throw e; } - } + } case DIRECT_BYTE_BUFFER_DECODER: { ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size()); bytes.copyTo(buffer); @@ -134,7 +135,7 @@ class ConformanceJava { ArrayList messages = new ArrayList (); ArrayList exceptions = new ArrayList (); - + for (int i = 0; i < BinaryDecoderType.values().length; i++) { messages.add(null); exceptions.add(null); @@ -247,6 +248,34 @@ class ConformanceJava { } break; } + case TEXT_PAYLOAD: { + if (isProto3) { + try { + TestMessagesProto3.TestAllTypesProto3.Builder builder = + TestMessagesProto3.TestAllTypesProto3.newBuilder(); + TextFormat.merge(request.getTextPayload(), builder); + testMessage = builder.build(); + } catch (TextFormat.ParseException e) { + return Conformance.ConformanceResponse.newBuilder() + .setParseError(e.getMessage()) + .build(); + } + } else if (isProto2) { + try { + TestMessagesProto2.TestAllTypesProto2.Builder builder = + TestMessagesProto2.TestAllTypesProto2.newBuilder(); + TextFormat.merge(request.getTextPayload(), builder); + testMessage = builder.build(); + } catch (TextFormat.ParseException e) { + return Conformance.ConformanceResponse.newBuilder() + .setParseError(e.getMessage()) + .build(); + } + } else { + throw new RuntimeException("Protobuf request doesn't have specific payload type."); + } + break; + } case PAYLOAD_NOT_SET: { throw new RuntimeException("Request didn't have payload."); } @@ -261,7 +290,7 @@ class ConformanceJava { throw new RuntimeException("Unspecified output format."); case PROTOBUF: { - ByteString MessageString = testMessage.toByteString(); + ByteString MessageString = testMessage.toByteString(); return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(MessageString).build(); } @@ -274,6 +303,10 @@ class ConformanceJava { e.getMessage()).build(); } + case TEXT_FORMAT: + return Conformance.ConformanceResponse.newBuilder().setTextPayload( + TextFormat.printToString(testMessage)).build(); + default: { throw new RuntimeException("Unexpected request output."); } diff --git a/conformance/Makefile.am b/conformance/Makefile.am index e51ab80af9..e47fd1081d 100644 --- a/conformance/Makefile.am +++ b/conformance/Makefile.am @@ -107,6 +107,7 @@ other_language_protoc_outputs = \ google/protobuf/wrappers_pb2.py \ Conformance/ConformanceRequest.php \ Conformance/ConformanceResponse.php \ + Conformance/FailureSet.php \ Conformance/WireFormat.php \ GPBMetadata/Conformance.php \ GPBMetadata/Google/Protobuf/Any.php \ @@ -206,6 +207,11 @@ EXTRA_DIST = \ conformance_test_runner_LDADD = $(top_srcdir)/src/libprotobuf.la conformance_test_runner_SOURCES = conformance_test.h conformance_test.cc \ + conformance_test_main.cc \ + binary_json_conformance_suite.h \ + binary_json_conformance_suite.cc \ + text_format_conformance_suite.h \ + text_format_conformance_suite.cc \ conformance_test_runner.cc \ third_party/jsoncpp/json.h \ third_party/jsoncpp/jsoncpp.cpp @@ -310,7 +316,7 @@ conformance-java-lite: javac_middleman_lite conformance-csharp: $(other_language_protoc_outputs) @echo "Writing shortcut script conformance-csharp..." @echo '#! /bin/sh' > conformance-csharp - @echo 'dotnet ../csharp/src/Google.Protobuf.Conformance/bin/Release/netcoreapp1.0/Google.Protobuf.Conformance.dll "$$@"' >> conformance-csharp + @echo 'dotnet ../csharp/src/Google.Protobuf.Conformance/bin/Release/netcoreapp2.1/Google.Protobuf.Conformance.dll "$$@"' >> conformance-csharp @chmod +x conformance-csharp conformance-php: @@ -330,30 +336,33 @@ test_cpp: protoc_middleman conformance-test-runner conformance-cpp ./conformance-test-runner --enforce_recommended --failure_list failure_list_cpp.txt ./conformance-cpp test_java: protoc_middleman conformance-test-runner conformance-java - ./conformance-test-runner --enforce_recommended --failure_list failure_list_java.txt ./conformance-java + ./conformance-test-runner --enforce_recommended --failure_list failure_list_java.txt --text_format_failure_list text_format_failure_list_java.txt ./conformance-java test_java_lite: protoc_middleman conformance-test-runner conformance-java-lite ./conformance-test-runner --enforce_recommended ./conformance-java-lite test_csharp: protoc_middleman conformance-test-runner conformance-csharp - ./conformance-test-runner --enforce_recommended --failure_list failure_list_csharp.txt ./conformance-csharp + ./conformance-test-runner --enforce_recommended --failure_list failure_list_csharp.txt --text_format_failure_list text_format_failure_list_csharp.txt ./conformance-csharp test_ruby: protoc_middleman conformance-test-runner $(other_language_protoc_outputs) - RUBYLIB=../ruby/lib:. ./conformance-test-runner --enforce_recommended --failure_list failure_list_ruby.txt ./conformance_ruby.rb + RUBYLIB=../ruby/lib:. ./conformance-test-runner --enforce_recommended --failure_list failure_list_ruby.txt --text_format_failure_list text_format_failure_list_ruby.txt ./conformance_ruby.rb + +test_ruby_mac: protoc_middleman conformance-test-runner $(other_language_protoc_outputs) + RUBYLIB=../ruby/lib:. ./conformance-test-runner --enforce_recommended --failure_list failure_list_ruby_mac.txt --text_format_failure_list text_format_failure_list_ruby.txt ./conformance_ruby.rb test_php: protoc_middleman conformance-test-runner conformance-php $(other_language_protoc_outputs) - ./conformance-test-runner --enforce_recommended --failure_list failure_list_php.txt ./conformance-php + ./conformance-test-runner --enforce_recommended --failure_list failure_list_php.txt --text_format_failure_list text_format_failure_list_php.txt ./conformance-php test_php_c: protoc_middleman conformance-test-runner conformance-php-c $(other_language_protoc_outputs) - ./conformance-test-runner --enforce_recommended --failure_list failure_list_php_c.txt ./conformance-php-c + ./conformance-test-runner --enforce_recommended --failure_list failure_list_php_c.txt --text_format_failure_list text_format_failure_list_php.txt ./conformance-php-c test_php_zts_c: protoc_middleman conformance-test-runner conformance-php-c $(other_language_protoc_outputs) - ./conformance-test-runner --enforce_recommended --failure_list failure_list_php_zts_c.txt ./conformance-php-c + ./conformance-test-runner --enforce_recommended --failure_list failure_list_php_zts_c.txt --text_format_failure_list text_format_failure_list_php.txt ./conformance-php-c # These depend on library paths being properly set up. The easiest way to # run them is to just use "tox" from the python dir. test_python: protoc_middleman conformance-test-runner - ./conformance-test-runner --enforce_recommended --failure_list failure_list_python.txt ./conformance_python.py + ./conformance-test-runner --enforce_recommended --failure_list failure_list_python.txt --text_format_failure_list text_format_failure_list_python.txt ./conformance_python.py test_python_cpp: protoc_middleman conformance-test-runner ./conformance-test-runner --enforce_recommended --failure_list failure_list_python_cpp.txt ./conformance_python.py diff --git a/conformance/README.md b/conformance/README.md index a5419bf0da..06925dbb55 100644 --- a/conformance/README.md +++ b/conformance/README.md @@ -57,10 +57,10 @@ Testing other Protocol Buffer implementations To run these tests against a new Protocol Buffers implementation, write a program in your language that uses the protobuf implementation you want to test. This program should implement the testing protocol defined in -[conformance.proto](https://github.com/google/protobuf/blob/master/conformance/conformance.proto). +[conformance.proto](https://github.com/protocolbuffers/protobuf/blob/master/conformance/conformance.proto). This is designed to be as easy as possible: the C++ version is only 150 lines and is a good example for what this program should look like -(see [conformance_cpp.cc](https://github.com/google/protobuf/blob/master/conformance/conformance_cpp.cc)). +(see [conformance_cpp.cc](https://github.com/protocolbuffers/protobuf/blob/master/conformance/conformance_cpp.cc)). The program only needs to be able to read from stdin and write to stdout. Portability diff --git a/conformance/conformance_test_impl.cc b/conformance/binary_json_conformance_suite.cc similarity index 92% rename from conformance/conformance_test_impl.cc rename to conformance/binary_json_conformance_suite.cc index acd0f25988..9c34b42a0d 100644 --- a/conformance/conformance_test_impl.cc +++ b/conformance/binary_json_conformance_suite.cc @@ -28,6 +28,7 @@ // (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 "binary_json_conformance_suite.h" #include "conformance_test.h" #include "third_party/jsoncpp/json.h" @@ -37,11 +38,13 @@ #include #include #include +#include #include #include using conformance::ConformanceRequest; using conformance::ConformanceResponse; +using conformance::WireFormat; using google::protobuf::Descriptor; using google::protobuf::FieldDescriptor; using google::protobuf::Message; @@ -189,73 +192,83 @@ std::unique_ptr NewTestMessage(bool is_proto3) { namespace google { namespace protobuf { -class ConformanceTestSuiteImpl : public ConformanceTestSuite { - public: - ConformanceTestSuiteImpl() {} - - private: - void RunSuiteImpl(); - void RunValidJsonTest(const string& test_name, - ConformanceLevel level, - const string& input_json, - const string& equivalent_text_format); - void RunValidJsonTestWithProtobufInput( - const string& test_name, - ConformanceLevel level, - const protobuf_test_messages::proto3::TestAllTypesProto3& input, - const string& equivalent_text_format); - void RunValidJsonIgnoreUnknownTest( - const string& test_name, ConformanceLevel level, const string& input_json, - const string& equivalent_text_format); - void RunValidProtobufTest(const string& test_name, ConformanceLevel level, - const string& input_protobuf, - const string& equivalent_text_format, - bool is_proto3); - void RunValidBinaryProtobufTest(const string& test_name, - ConformanceLevel level, - const string& input_protobuf, - bool is_proto3); - void RunValidProtobufTestWithMessage( - const string& test_name, ConformanceLevel level, - const Message *input, - const string& equivalent_text_format, - bool is_proto3); - - typedef std::function Validator; - void RunValidJsonTestWithValidator(const string& test_name, - ConformanceLevel level, - const string& input_json, - const Validator& validator); - void ExpectParseFailureForJson(const string& test_name, - ConformanceLevel level, - const string& input_json); - void ExpectSerializeFailureForJson(const string& test_name, - ConformanceLevel level, - const string& text_format); - void ExpectParseFailureForProtoWithProtoVersion (const string& proto, - const string& test_name, - ConformanceLevel level, - bool is_proto3); - void ExpectParseFailureForProto(const std::string& proto, - const std::string& test_name, - ConformanceLevel level); - void ExpectHardParseFailureForProto(const std::string& proto, - const std::string& test_name, - ConformanceLevel level); - void TestPrematureEOFForType(google::protobuf::FieldDescriptor::Type type); - void TestIllegalTags(); - template - void TestOneofMessage (MessageType &message, - bool is_proto3); - template - void TestUnknownMessage (MessageType &message, - bool is_proto3); - void TestValidDataForType( - google::protobuf::FieldDescriptor::Type, - std::vector> values); -}; - -void ConformanceTestSuiteImpl::ExpectParseFailureForProtoWithProtoVersion ( +bool BinaryAndJsonConformanceSuite::ParseJsonResponse( + const ConformanceResponse& response, + Message* test_message) { + string binary_protobuf; + util::Status status = + JsonToBinaryString(type_resolver_.get(), type_url_, + response.json_payload(), &binary_protobuf); + + if (!status.ok()) { + return false; + } + + if (!test_message->ParseFromString(binary_protobuf)) { + GOOGLE_LOG(FATAL) + << "INTERNAL ERROR: internal JSON->protobuf transcode " + << "yielded unparseable proto."; + return false; + } + + return true; +} + +bool BinaryAndJsonConformanceSuite::ParseResponse( + const ConformanceResponse& response, + const ConformanceRequestSetting& setting, + Message* test_message) { + const ConformanceRequest& request = setting.GetRequest(); + WireFormat requested_output = request.requested_output_format(); + const string& test_name = setting.GetTestName(); + ConformanceLevel level = setting.GetLevel(); + + switch (response.result_case()) { + case ConformanceResponse::kProtobufPayload: { + if (requested_output != conformance::PROTOBUF) { + ReportFailure( + test_name, level, request, response, + StrCat("Test was asked for ", WireFormatToString(requested_output), + " output but provided PROTOBUF instead.").c_str()); + return false; + } + + if (!test_message->ParseFromString(response.protobuf_payload())) { + ReportFailure(test_name, level, request, response, + "Protobuf output we received from test was unparseable."); + return false; + } + + break; + } + + case ConformanceResponse::kJsonPayload: { + if (requested_output != conformance::JSON) { + ReportFailure( + test_name, level, request, response, + StrCat("Test was asked for ", WireFormatToString(requested_output), + " output but provided JSON instead.").c_str()); + return false; + } + + if (!ParseJsonResponse(response, test_message)) { + ReportFailure(test_name, level, request, response, + "JSON output we received from test was unparseable."); + return false; + } + + break; + } + + default: + GOOGLE_LOG(FATAL) << test_name << ": unknown payload type: " + << response.result_case(); + } + + return true; +} + +void BinaryAndJsonConformanceSuite::ExpectParseFailureForProtoWithProtoVersion ( const string& proto, const string& test_name, ConformanceLevel level, bool is_proto3) { std::unique_ptr prototype = NewTestMessage(is_proto3); @@ -285,7 +298,7 @@ void ConformanceTestSuiteImpl::ExpectParseFailureForProtoWithProtoVersion ( } // Expect that this precise protobuf will cause a parse error. -void ConformanceTestSuiteImpl::ExpectParseFailureForProto( +void BinaryAndJsonConformanceSuite::ExpectParseFailureForProto( const string& proto, const string& test_name, ConformanceLevel level) { ExpectParseFailureForProtoWithProtoVersion(proto, test_name, level, true); ExpectParseFailureForProtoWithProtoVersion(proto, test_name, level, false); @@ -296,12 +309,12 @@ void ConformanceTestSuiteImpl::ExpectParseFailureForProto( // data verbatim and once with this data followed by some valid data. // // TODO(haberman): implement the second of these. -void ConformanceTestSuiteImpl::ExpectHardParseFailureForProto( +void BinaryAndJsonConformanceSuite::ExpectHardParseFailureForProto( const string& proto, const string& test_name, ConformanceLevel level) { return ExpectParseFailureForProto(proto, test_name, level); } -void ConformanceTestSuiteImpl::RunValidJsonTest( +void BinaryAndJsonConformanceSuite::RunValidJsonTest( const string& test_name, ConformanceLevel level, const string& input_json, const string& equivalent_text_format) { TestAllTypesProto3 prototype; @@ -317,7 +330,7 @@ void ConformanceTestSuiteImpl::RunValidJsonTest( RunValidInputTest(setting2, equivalent_text_format); } -void ConformanceTestSuiteImpl::RunValidJsonTestWithProtobufInput( +void BinaryAndJsonConformanceSuite::RunValidJsonTestWithProtobufInput( const string& test_name, ConformanceLevel level, const TestAllTypesProto3& input, const string& equivalent_text_format) { ConformanceRequestSetting setting( @@ -327,7 +340,7 @@ void ConformanceTestSuiteImpl::RunValidJsonTestWithProtobufInput( RunValidInputTest(setting, equivalent_text_format); } -void ConformanceTestSuiteImpl::RunValidJsonIgnoreUnknownTest( +void BinaryAndJsonConformanceSuite::RunValidJsonIgnoreUnknownTest( const string& test_name, ConformanceLevel level, const string& input_json, const string& equivalent_text_format) { TestAllTypesProto3 prototype; @@ -338,7 +351,7 @@ void ConformanceTestSuiteImpl::RunValidJsonIgnoreUnknownTest( RunValidInputTest(setting, equivalent_text_format); } -void ConformanceTestSuiteImpl::RunValidProtobufTest( +void BinaryAndJsonConformanceSuite::RunValidProtobufTest( const string& test_name, ConformanceLevel level, const string& input_protobuf, const string& equivalent_text_format, bool is_proto3) { @@ -359,7 +372,7 @@ void ConformanceTestSuiteImpl::RunValidProtobufTest( } } -void ConformanceTestSuiteImpl::RunValidBinaryProtobufTest( +void BinaryAndJsonConformanceSuite::RunValidBinaryProtobufTest( const string& test_name, ConformanceLevel level, const string& input_protobuf, bool is_proto3) { std::unique_ptr prototype = NewTestMessage(is_proto3); @@ -370,7 +383,7 @@ void ConformanceTestSuiteImpl::RunValidBinaryProtobufTest( RunValidBinaryInputTest(setting, input_protobuf); } -void ConformanceTestSuiteImpl::RunValidProtobufTestWithMessage( +void BinaryAndJsonConformanceSuite::RunValidProtobufTestWithMessage( const string& test_name, ConformanceLevel level, const Message *input, const string& equivalent_text_format, bool is_proto3) { RunValidProtobufTest(test_name, level, input->SerializeAsString(), @@ -382,7 +395,7 @@ void ConformanceTestSuiteImpl::RunValidProtobufTestWithMessage( // numbers while the parser is allowed to accept them as JSON strings). This // method allows strict checking on a proto3 JSON serializer by inspecting // the JSON output directly. -void ConformanceTestSuiteImpl::RunValidJsonTestWithValidator( +void BinaryAndJsonConformanceSuite::RunValidJsonTestWithValidator( const string& test_name, ConformanceLevel level, const string& input_json, const Validator& validator) { TestAllTypesProto3 prototype; @@ -426,7 +439,7 @@ void ConformanceTestSuiteImpl::RunValidJsonTestWithValidator( ReportSuccess(effective_test_name); } -void ConformanceTestSuiteImpl::ExpectParseFailureForJson( +void BinaryAndJsonConformanceSuite::ExpectParseFailureForJson( const string& test_name, ConformanceLevel level, const string& input_json) { TestAllTypesProto3 prototype; // We don't expect output, but if the program erroneously accepts the protobuf @@ -452,7 +465,7 @@ void ConformanceTestSuiteImpl::ExpectParseFailureForJson( } } -void ConformanceTestSuiteImpl::ExpectSerializeFailureForJson( +void BinaryAndJsonConformanceSuite::ExpectSerializeFailureForJson( const string& test_name, ConformanceLevel level, const string& text_format) { TestAllTypesProto3 payload_message; GOOGLE_CHECK( @@ -482,7 +495,7 @@ void ConformanceTestSuiteImpl::ExpectSerializeFailureForJson( } //TODO: proto2? -void ConformanceTestSuiteImpl::TestPrematureEOFForType( +void BinaryAndJsonConformanceSuite::TestPrematureEOFForType( FieldDescriptor::Type type) { // Incomplete values for each wire type. static const string incompletes[6] = { @@ -570,7 +583,7 @@ void ConformanceTestSuiteImpl::TestPrematureEOFForType( } } -void ConformanceTestSuiteImpl::TestValidDataForType( +void BinaryAndJsonConformanceSuite::TestValidDataForType( FieldDescriptor::Type type, std::vector> values) { for (int is_proto3 = 0; is_proto3 < 2; is_proto3++) { @@ -606,7 +619,7 @@ void ConformanceTestSuiteImpl::TestValidDataForType( } // TODO: proto2? -void ConformanceTestSuiteImpl::TestIllegalTags() { +void BinaryAndJsonConformanceSuite::TestIllegalTags() { // field num 0 is illegal string nullfield[] = { "\1DEADBEEF", @@ -621,7 +634,7 @@ void ConformanceTestSuiteImpl::TestIllegalTags() { } } template -void ConformanceTestSuiteImpl::TestOneofMessage ( +void BinaryAndJsonConformanceSuite::TestOneofMessage ( MessageType &message, bool is_proto3) { message.set_oneof_uint32(0); RunValidProtobufTestWithMessage( @@ -660,14 +673,28 @@ void ConformanceTestSuiteImpl::TestOneofMessage ( } template -void ConformanceTestSuiteImpl::TestUnknownMessage( +void BinaryAndJsonConformanceSuite::TestUnknownMessage( MessageType& message, bool is_proto3) { message.ParseFromString("\xA8\x1F\x01"); RunValidBinaryProtobufTest("UnknownVarint", REQUIRED, message.SerializeAsString(), is_proto3); } -void ConformanceTestSuiteImpl::RunSuiteImpl() { +void BinaryAndJsonConformanceSuite::RunSuiteImpl() { + // Hack to get the list of test failures based on whether + // GOOGLE3_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER is enabled or not. + conformance::FailureSet failure_set; + ConformanceRequest req; + ConformanceResponse res; + req.set_message_type(failure_set.GetTypeName()); + req.set_protobuf_payload(""); + req.set_requested_output_format(conformance::WireFormat::PROTOBUF); + RunTest("FindFailures", req, &res); + GOOGLE_CHECK(failure_set.MergeFromString(res.protobuf_payload())); + for (const string& failure : failure_set.failure()) { + AddExpectedFailedTest(failure); + } + type_resolver_.reset(NewTypeResolverForDescriptorPool( kTypeUrlPrefix, DescriptorPool::generated_pool())); type_url_ = GetTypeUrl(TestAllTypesProto3::descriptor()); @@ -1373,6 +1400,23 @@ void ConformanceTestSuiteImpl::RunSuiteImpl() { "EnumField", REQUIRED, R"({"optionalNestedEnum": "FOO"})", "optional_nested_enum: FOO"); + // Enum fields with alias + RunValidJsonTest( + "EnumFieldWithAlias", REQUIRED, + R"({"optionalAliasedEnum": "ALIAS_BAZ"})", + "optional_aliased_enum: ALIAS_BAZ"); + RunValidJsonTest( + "EnumFieldWithAliasUseAlias", REQUIRED, + R"({"optionalAliasedEnum": "QUX"})", + "optional_aliased_enum: ALIAS_BAZ"); + RunValidJsonTest( + "EnumFieldWithAliasLowerCase", REQUIRED, + R"({"optionalAliasedEnum": "qux"})", + "optional_aliased_enum: ALIAS_BAZ"); + RunValidJsonTest( + "EnumFieldWithAliasDifferentCase", REQUIRED, + R"({"optionalAliasedEnum": "bAz"})", + "optional_aliased_enum: ALIAS_BAZ"); // Enum values must be represented as strings. ExpectParseFailureForJson( "EnumFieldNotQuoted", REQUIRED, @@ -1934,14 +1978,12 @@ void ConformanceTestSuiteImpl::RunSuiteImpl() { })", "repeated_timestamp: {seconds: -62135596800}" "repeated_timestamp: {seconds: 253402300799 nanos: 999999999}"); - RunValidJsonTest( - "TimestampWithPositiveOffset", REQUIRED, - R"({"optionalTimestamp": "1970-01-01T08:00:00+08:00"})", - "optional_timestamp: {seconds: 0}"); - RunValidJsonTest( - "TimestampWithNegativeOffset", REQUIRED, - R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})", - "optional_timestamp: {seconds: 0}"); + RunValidJsonTest("TimestampWithPositiveOffset", REQUIRED, + R"({"optionalTimestamp": "1970-01-01T08:00:01+08:00"})", + "optional_timestamp: {seconds: 1}"); + RunValidJsonTest("TimestampWithNegativeOffset", REQUIRED, + R"({"optionalTimestamp": "1969-12-31T16:00:01-08:00"})", + "optional_timestamp: {seconds: 1}"); RunValidJsonTest( "TimestampNull", REQUIRED, R"({"optionalTimestamp": null})", @@ -2012,6 +2054,10 @@ void ConformanceTestSuiteImpl::RunSuiteImpl() { "FieldMask", REQUIRED, R"({"optionalFieldMask": "foo,barBaz"})", R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})"); + RunValidJsonTest( + "EmptyFieldMask", REQUIRED, + R"({"optionalFieldMask": ""})", + R"(optional_field_mask: {})"); ExpectParseFailureForJson( "FieldMaskInvalidCharacter", RECOMMENDED, R"({"optionalFieldMask": "foo,bar_bar"})"); @@ -2091,6 +2137,24 @@ void ConformanceTestSuiteImpl::RunSuiteImpl() { } } )"); + RunValidJsonTest( + "StructWithEmptyListValue", REQUIRED, + R"({ + "optionalStruct": { + "listValue": [] + } + })", + R"( + optional_struct: { + fields: { + key: "listValue" + value: { + list_value: { + } + } + } + } + )"); // Value RunValidJsonTest( "ValueAcceptInteger", REQUIRED, @@ -2142,6 +2206,36 @@ void ConformanceTestSuiteImpl::RunSuiteImpl() { } } )"); + RunValidJsonTest( + "RepeatedValue", REQUIRED, + R"({ + "repeatedValue": [["a"]] + })", + R"( + repeated_value: [ + { + list_value: { + values: [ + { string_value: "a"} + ] + } + } + ] + )"); + RunValidJsonTest( + "RepeatedListValue", REQUIRED, + R"({ + "repeatedListValue": [["a"]] + })", + R"( + repeated_list_value: [ + { + values: [ + { string_value: "a"} + ] + } + ] + )"); // Any RunValidJsonTest( @@ -2356,13 +2450,9 @@ void ConformanceTestSuiteImpl::RunSuiteImpl() { "unknown": {"a": 1} })", ""); -} -struct StaticTestSuiteInitializer { - StaticTestSuiteInitializer() { - AddTestSuite(new ConformanceTestSuiteImpl()); - } -} static_test_suite_initializer; + ExpectParseFailureForJson("RejectTopLevelNull", REQUIRED, "null"); +} } // namespace protobuf } // namespace google diff --git a/conformance/binary_json_conformance_suite.h b/conformance/binary_json_conformance_suite.h new file mode 100644 index 0000000000..7a03545fa5 --- /dev/null +++ b/conformance/binary_json_conformance_suite.h @@ -0,0 +1,121 @@ +// 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 CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H +#define CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H + +#include "conformance_test.h" +#include "third_party/jsoncpp/json.h" + +namespace google { +namespace protobuf { + +class BinaryAndJsonConformanceSuite : public ConformanceTestSuite { + public: + BinaryAndJsonConformanceSuite() {} + + private: + void RunSuiteImpl(); + void RunValidJsonTest(const string& test_name, + ConformanceLevel level, + const string& input_json, + const string& equivalent_text_format); + void RunValidJsonTestWithProtobufInput( + const string& test_name, + ConformanceLevel level, + const protobuf_test_messages::proto3::TestAllTypesProto3& input, + const string& equivalent_text_format); + void RunValidJsonIgnoreUnknownTest( + const string& test_name, ConformanceLevel level, const string& input_json, + const string& equivalent_text_format); + void RunValidProtobufTest(const string& test_name, ConformanceLevel level, + const string& input_protobuf, + const string& equivalent_text_format, + bool is_proto3); + void RunValidBinaryProtobufTest(const string& test_name, + ConformanceLevel level, + const string& input_protobuf, + bool is_proto3); + void RunValidProtobufTestWithMessage( + const string& test_name, ConformanceLevel level, + const Message *input, + const string& equivalent_text_format, + bool is_proto3); + + bool ParseJsonResponse( + const conformance::ConformanceResponse& response, + Message* test_message); + bool ParseResponse( + const conformance::ConformanceResponse& response, + const ConformanceRequestSetting& setting, + Message* test_message) override; + + typedef std::function Validator; + void RunValidJsonTestWithValidator(const string& test_name, + ConformanceLevel level, + const string& input_json, + const Validator& validator); + void ExpectParseFailureForJson(const string& test_name, + ConformanceLevel level, + const string& input_json); + void ExpectSerializeFailureForJson(const string& test_name, + ConformanceLevel level, + const string& text_format); + void ExpectParseFailureForProtoWithProtoVersion (const string& proto, + const string& test_name, + ConformanceLevel level, + bool is_proto3); + void ExpectParseFailureForProto(const std::string& proto, + const std::string& test_name, + ConformanceLevel level); + void ExpectHardParseFailureForProto(const std::string& proto, + const std::string& test_name, + ConformanceLevel level); + void TestPrematureEOFForType(google::protobuf::FieldDescriptor::Type type); + void TestIllegalTags(); + template + void TestOneofMessage (MessageType &message, + bool is_proto3); + template + void TestUnknownMessage (MessageType &message, + bool is_proto3); + void TestValidDataForType( + google::protobuf::FieldDescriptor::Type, + std::vector> values); + + std::unique_ptr + type_resolver_; + std::string type_url_; +}; + +} // namespace protobuf +} // namespace google + +#endif // CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H diff --git a/conformance/conformance.proto b/conformance/conformance.proto index af89152d10..54da406c3c 100644 --- a/conformance/conformance.proto +++ b/conformance/conformance.proto @@ -55,18 +55,32 @@ enum WireFormat { UNSPECIFIED = 0; PROTOBUF = 1; JSON = 2; + JSPB = 3; // Google internal only. Opensource testees just skip it. + TEXT_FORMAT = 4; } enum TestCategory { UNSPECIFIED_TEST = 0; BINARY_TEST = 1; // Test binary wire format. - JSON_TEST = 2; // Test json wire format. + JSON_TEST = 2; // Test json wire format. // Similar to JSON_TEST. However, during parsing json, testee should ignore // unknown fields. This feature is optional. Each implementation can descide // whether to support it. See // https://developers.google.com/protocol-buffers/docs/proto3#json_options // for more detail. JSON_IGNORE_UNKNOWN_PARSING_TEST = 3; + // Test jspb wire format. Google internal only. Opensource testees just skip it. + JSPB_TEST = 4; + // Test text format. For cpp, java and python, testees can already deal with + // this type. Testees of other languages can simply skip it. + TEXT_FORMAT_TEST = 5; +} + +// The conformance runner will request a list of failures as the first request. +// This will be known by message_type == "conformance.FailureSet", a conformance +// test should return a serialized FailureSet in protobuf_payload. +message FailureSet { + repeated string failure = 1; } // Represents a single test case's input. The testee should: @@ -85,6 +99,9 @@ message ConformanceRequest { oneof payload { bytes protobuf_payload = 1; string json_payload = 2; + // Google internal only. Opensource testees just skip it. + string jspb_payload = 7; + string text_payload = 8; } // Which format should the testee serialize its message to? @@ -99,6 +116,13 @@ message ConformanceRequest { // spedific support in testee programs. Refer to the defintion of TestCategory // for more information. TestCategory test_category = 5; + + // Specify details for how to encode jspb. + JspbEncodingConfig jspb_encoding_options = 6; + + // This can be used in json and text format. If true, testee should print + // unknown fields instead of ignore. This feature is optional. + bool print_unknown_fields = 9; } // Represents a single test case's output. @@ -132,5 +156,21 @@ message ConformanceResponse { // For when the testee skipped the test, likely because a certain feature // wasn't supported, like JSON input/output. string skipped = 5; + + // If the input was successfully parsed and the requested output was JSPB, + // serialize to JSPB and set it in this field. JSPB is google internal only + // format. Opensource testees can just skip it. + string jspb_payload = 7; + + // If the input was successfully parsed and the requested output was + // TEXT_FORMAT, serialize to TEXT_FORMAT and set it in this field. + string text_payload = 8; } } + +// Encoding options for jspb format. +message JspbEncodingConfig { + // Encode the value field of Any as jspb array if ture, otherwise binary. + bool use_jspb_array_any_format = 1; +} + diff --git a/conformance/conformance_cpp.cc b/conformance/conformance_cpp.cc index ac2f6dea1b..5a1f21404d 100644 --- a/conformance/conformance_cpp.cc +++ b/conformance/conformance_cpp.cc @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -45,18 +46,48 @@ using google::protobuf::Descriptor; using google::protobuf::DescriptorPool; using google::protobuf::Message; using google::protobuf::MessageFactory; +using google::protobuf::TextFormat; using google::protobuf::util::BinaryToJsonString; using google::protobuf::util::JsonParseOptions; using google::protobuf::util::JsonToBinaryString; using google::protobuf::util::NewTypeResolverForDescriptorPool; using google::protobuf::util::Status; using google::protobuf::util::TypeResolver; -using protobuf_test_messages::proto3::TestAllTypesProto3; using protobuf_test_messages::proto2::TestAllTypesProto2; +using protobuf_test_messages::proto3::TestAllTypesProto3; using std::string; static const char kTypeUrlPrefix[] = "type.googleapis.com"; +const char* kFailures[] = { +#if !GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + "Required.Proto2.ProtobufInput." + "PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE", + "Required.Proto2.ProtobufInput." + "PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.BOOL", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.ENUM", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT32", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT64", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT32", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT64", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT32", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT64", + "Required.Proto3.ProtobufInput." + "PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE", + "Required.Proto3.ProtobufInput." + "PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.BOOL", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.ENUM", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT32", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT64", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT32", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT64", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT32", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT64", +#endif +}; + static string GetTypeUrl(const Descriptor* message) { return string(kTypeUrlPrefix) + "/" + message->full_name(); } @@ -134,9 +165,28 @@ void DoTest(const ConformanceRequest& request, ConformanceResponse* response) { break; } + case ConformanceRequest::kTextPayload: { + if (!TextFormat::ParseFromString(request.text_payload(), test_message)) { + response->set_parse_error("Parse error"); + return; + } + break; + } + case ConformanceRequest::PAYLOAD_NOT_SET: GOOGLE_LOG(FATAL) << "Request didn't have payload."; break; + + default: + GOOGLE_LOG(FATAL) << "unknown payload type: " + << request.payload_case(); + break; + } + + conformance::FailureSet failures; + if (descriptor == failures.GetDescriptor()) { + for (const char* s : kFailures) failures.add_failure(s); + test_message = &failures; } switch (request.requested_output_format()) { @@ -163,6 +213,14 @@ void DoTest(const ConformanceRequest& request, ConformanceResponse* response) { break; } + case conformance::TEXT_FORMAT: { + TextFormat::Printer printer; + printer.SetHideUnknownFields(!request.print_unknown_fields()); + GOOGLE_CHECK(printer.PrintToString(*test_message, + response->mutable_text_payload())); + break; + } + default: GOOGLE_LOG(FATAL) << "Unknown output format: " << request.requested_output_format(); diff --git a/conformance/conformance_nodejs.js b/conformance/conformance_nodejs.js index 5d3955f77b..4d96a3851e 100755 --- a/conformance/conformance_nodejs.js +++ b/conformance/conformance_nodejs.js @@ -76,6 +76,10 @@ function doTest(request) { response.setSkipped("JSON not supported."); return response; + case conformance.ConformanceRequest.PayloadCase.TEXT_PAYLOAD: + response.setSkipped("Text format not supported."); + return response; + case conformance.ConformanceRequest.PayloadCase.PAYLOAD_NOT_SET: response.setRuntimeError("Request didn't have payload"); return response; diff --git a/conformance/conformance_objc.m b/conformance/conformance_objc.m index 84a43811fa..60b2ac59ed 100644 --- a/conformance/conformance_objc.m +++ b/conformance/conformance_objc.m @@ -92,6 +92,16 @@ static ConformanceResponse *DoTest(ConformanceRequest *request) { case ConformanceRequest_Payload_OneOfCase_JsonPayload: response.skipped = @"ObjC doesn't support parsing JSON"; break; + + case ConformanceRequest_Payload_OneOfCase_JspbPayload: + response.skipped = + @"ConformanceRequest had a jspb_payload ConformanceRequest.payload;" + " those aren't supposed to happen with opensource."; + break; + + case ConformanceRequest_Payload_OneOfCase_TextPayload: + response.skipped = @"ObjC doesn't support parsing TextFormat"; + break; } if (testMessage) { @@ -112,6 +122,18 @@ static ConformanceResponse *DoTest(ConformanceRequest *request) { case WireFormat_Json: response.skipped = @"ObjC doesn't support generating JSON"; break; + + case WireFormat_Jspb: + response.skipped = + @"ConformanceRequest had a requested_output_format of JSPB WireFormat; that" + " isn't supposed to happen with opensource."; + break; + + case WireFormat_TextFormat: + // ObjC only has partial objc generation, so don't attempt any tests that need + // support. + response.skipped = @"ObjC doesn't support generating TextFormat"; + break; } } diff --git a/conformance/conformance_php.php b/conformance/conformance_php.php index 799cc3e361..9eef0c5a4a 100755 --- a/conformance/conformance_php.php +++ b/conformance/conformance_php.php @@ -3,10 +3,13 @@ require_once("Conformance/WireFormat.php"); require_once("Conformance/ConformanceResponse.php"); require_once("Conformance/ConformanceRequest.php"); +require_once("Conformance/FailureSet.php"); +require_once("Conformance/JspbEncodingConfig.php"); require_once("Conformance/TestCategory.php"); require_once("Protobuf_test_messages/Proto3/ForeignMessage.php"); require_once("Protobuf_test_messages/Proto3/ForeignEnum.php"); require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3.php"); +require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/AliasedEnum.php"); require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/NestedMessage.php"); require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/NestedEnum.php"); @@ -27,7 +30,10 @@ function doTest($request) $test_message = new \Protobuf_test_messages\Proto3\TestAllTypesProto3(); $response = new \Conformance\ConformanceResponse(); if ($request->getPayload() == "protobuf_payload") { - if ($request->getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") { + if ($request->getMessageType() == "conformance.FailureSet") { + $response->setProtobufPayload(""); + return $response; + } elseif ($request->getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") { try { $test_message->mergeFromString($request->getProtobufPayload()); } catch (Exception $e) { @@ -51,7 +57,10 @@ function doTest($request) $response->setParseError($e->getMessage()); return $response; } - } else { + } elseif ($request->getPayload() == "text_payload") { + $response->setSkipped("PHP doesn't support text format yet"); + return $response; + } else { trigger_error("Request didn't have payload.", E_USER_ERROR); } diff --git a/conformance/conformance_python.py b/conformance/conformance_python.py index 876642bc79..88d9749389 100755 --- a/conformance/conformance_python.py +++ b/conformance/conformance_python.py @@ -44,6 +44,7 @@ from google.protobuf import json_format from google.protobuf import message from google.protobuf import test_messages_proto3_pb2 from google.protobuf import test_messages_proto2_pb2 +from google.protobuf import text_format import conformance_pb2 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) @@ -56,26 +57,80 @@ class ProtocolError(Exception): pass def do_test(request): + response = conformance_pb2.ConformanceResponse() + + if request.message_type == "conformance.FailureSet": + failure_set = conformance_pb2.FailureSet() + failures = [] + # TODO(gerbens): Remove, this is a hack to detect if the old vs new + # parser is used by the cpp code. Relying on a bug in the old parser. + hack_proto = test_messages_proto2_pb2.TestAllTypesProto2() + old_parser = True + try: + hack_proto.ParseFromString(b"\322\002\001") + except message.DecodeError as e: + old_parser = False + if old_parser: + # the string above is one of the failing conformance test strings of the + # old parser. If we succeed the c++ implementation is using the old + # parser so we add the list of failing conformance tests. + failures = [ + "Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE", + "Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.BOOL", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.DOUBLE", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.ENUM", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FIXED32", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FIXED64", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FLOAT", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT32", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT64", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SFIXED32", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SFIXED64", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT32", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT64", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT32", + "Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT64", + "Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE", + "Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.BOOL", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.DOUBLE", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.ENUM", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FIXED32", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FIXED64", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FLOAT", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT32", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT64", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SFIXED32", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SFIXED64", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT32", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT64", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT32", + "Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT64", + ] + for x in failures: + failure_set.failure.append(x) + response.protobuf_payload = failure_set.SerializeToString() + return response + isProto3 = (request.message_type == "protobuf_test_messages.proto3.TestAllTypesProto3") isJson = (request.WhichOneof('payload') == 'json_payload') isProto2 = (request.message_type == "protobuf_test_messages.proto2.TestAllTypesProto2") - + if (not isProto3) and (not isJson) and (not isProto2): raise ProtocolError("Protobuf request doesn't have specific payload type") - + test_message = test_messages_proto2_pb2.TestAllTypesProto2() if isProto2 else \ test_messages_proto3_pb2.TestAllTypesProto3() - response = conformance_pb2.ConformanceResponse() - try: if request.WhichOneof('payload') == 'protobuf_payload': try: test_message.ParseFromString(request.protobuf_payload) except message.DecodeError as e: response.parse_error = str(e) - return response - + return response + elif request.WhichOneof('payload') == 'json_payload': try: ignore_unknown_fields = \ @@ -87,6 +142,13 @@ def do_test(request): response.parse_error = str(e) return response + elif request.WhichOneof('payload') == 'text_payload': + try: + text_format.Parse(request.text_payload, test_message) + except Exception as e: + response.parse_error = str(e) + return response + else: raise ProtocolError("Request didn't have payload.") @@ -97,12 +159,16 @@ def do_test(request): response.protobuf_payload = test_message.SerializeToString() elif request.requested_output_format == conformance_pb2.JSON: - try: + try: response.json_payload = json_format.MessageToJson(test_message) except Exception as e: response.serialize_error = str(e) return response + elif request.requested_output_format == conformance_pb2.TEXT_FORMAT: + response.text_payload = text_format.MessageToString( + test_message, print_unknown_fields=request.print_unknown_fields) + except Exception as e: response.runtime_error = str(e) diff --git a/conformance/conformance_ruby.rb b/conformance/conformance_ruby.rb index df63bf7ced..79d8d3dd9e 100755 --- a/conformance/conformance_ruby.rb +++ b/conformance/conformance_ruby.rb @@ -54,7 +54,7 @@ def do_test(request) elsif request.message_type.eql?('protobuf_test_messages.proto2.TestAllTypesProto2') response.skipped = "Ruby doesn't support proto2" return response - else + else fail "Protobuf request doesn't have specific payload type" end @@ -66,6 +66,12 @@ def do_test(request) response.parse_error = err.message.encode('utf-8') return response end + + when :text_payload + begin + response.skipped = "Ruby doesn't support proto2" + return response + end when nil fail "Request didn't have payload" diff --git a/conformance/conformance_test.cc b/conformance/conformance_test.cc index 0a45f9b904..6325b354a3 100644 --- a/conformance/conformance_test.cc +++ b/conformance/conformance_test.cc @@ -57,34 +57,6 @@ using std::string; namespace google { namespace protobuf { -std::set *conformance_test_suite_set; -GOOGLE_PROTOBUF_DECLARE_ONCE(conformance_test_suite_set_init_); - -void DeleteConformanceTestSuiteSet() { - delete conformance_test_suite_set; -} - -static void InitConformanceTestSuiteSet() { - conformance_test_suite_set = new std::set(); - internal::OnShutdown(&DeleteConformanceTestSuiteSet); -} - -static void InitConformanceTestSuiteSetOnce() { - ::google::protobuf::GoogleOnceInit( - &conformance_test_suite_set_init_, - &InitConformanceTestSuiteSet); -} - -void AddTestSuite(ConformanceTestSuite* suite) { - InitConformanceTestSuiteSetOnce(); - conformance_test_suite_set->insert(suite); -} - -const std::set& GetTestSuiteSet() { - InitConformanceTestSuiteSetOnce(); - return *conformance_test_suite_set; -} - ConformanceTestSuite::ConformanceRequestSetting::ConformanceRequestSetting( ConformanceLevel level, conformance::WireFormat input_format, @@ -96,6 +68,7 @@ ConformanceTestSuite::ConformanceRequestSetting::ConformanceRequestSetting( input_format_(input_format), output_format_(output_format), prototype_message_(prototype_message), + prototype_message_for_compare_(prototype_message.New()), test_name_(test_name) { switch (input_format) { case conformance::PROTOBUF: { @@ -108,6 +81,16 @@ ConformanceTestSuite::ConformanceRequestSetting::ConformanceRequestSetting( break; } + case conformance::JSPB: { + request_.set_jspb_payload(input); + break; + } + + case conformance::TEXT_FORMAT: { + request_.set_text_payload(input); + break; + } + default: GOOGLE_LOG(FATAL) << "Unspecified input format"; } @@ -120,7 +103,7 @@ ConformanceTestSuite::ConformanceRequestSetting::ConformanceRequestSetting( Message* ConformanceTestSuite::ConformanceRequestSetting:: GetTestMessage() const { - return prototype_message_.New(); + return prototype_message_for_compare_->New(); } string ConformanceTestSuite::ConformanceRequestSetting:: @@ -154,6 +137,8 @@ string ConformanceTestSuite::ConformanceRequestSetting:: return "ProtobufInput"; case conformance::JSON: return "JsonInput"; + case conformance::TEXT_FORMAT: + return "TextFormatInput"; default: GOOGLE_LOG(FATAL) << "Unspecified output format"; } @@ -167,21 +152,14 @@ string ConformanceTestSuite::ConformanceRequestSetting:: return "ProtobufOutput"; case conformance::JSON: return "JsonOutput"; + case conformance::TEXT_FORMAT: + return "TextFormatOutput"; default: GOOGLE_LOG(FATAL) << "Unspecified output format"; } return ""; } -void ConformanceTestSuite::SetFailureList( - const string& filename, - const std::vector& failure_list) { - failure_list_filename_ = filename; - expected_to_fail_.clear(); - std::copy(failure_list.begin(), failure_list.end(), - std::inserter(expected_to_fail_, expected_to_fail_.end())); -} - void ConformanceTestSuite::ReportSuccess(const string& test_name) { if (expected_to_fail_.erase(test_name) != 0) { StringAppendF(&output_, @@ -243,23 +221,27 @@ void ConformanceTestSuite::RunValidInputTest( void ConformanceTestSuite::RunValidBinaryInputTest( const ConformanceRequestSetting& setting, const string& equivalent_wire_format) { + const ConformanceRequest& request = setting.GetRequest(); + ConformanceResponse response; + RunTest(setting.GetTestName(), request, &response); + VerifyResponse(setting, equivalent_wire_format, response, true); +} + +void ConformanceTestSuite::VerifyResponse( + const ConformanceRequestSetting& setting, + const string& equivalent_wire_format, + const ConformanceResponse& response, + bool need_report_success) { + Message* test_message = setting.GetTestMessage(); + const ConformanceRequest& request = setting.GetRequest(); const string& test_name = setting.GetTestName(); ConformanceLevel level = setting.GetLevel(); - Message* reference_message = setting.GetTestMessage(); + GOOGLE_CHECK( reference_message->ParseFromString(equivalent_wire_format)) << "Failed to parse wire data for test case: " << test_name; - const ConformanceRequest& request = setting.GetRequest(); - ConformanceResponse response; - - RunTest(test_name, request, &response); - - Message* test_message = setting.GetTestMessage(); - - WireFormat requested_output = request.requested_output_format(); - switch (response.result_case()) { case ConformanceResponse::RESULT_NOT_SET: ReportFailure(test_name, level, request, response, @@ -277,53 +259,8 @@ void ConformanceTestSuite::RunValidBinaryInputTest( ReportSkip(test_name, request, response); return; - case ConformanceResponse::kJsonPayload: { - if (requested_output != conformance::JSON) { - ReportFailure( - test_name, level, request, response, - "Test was asked for protobuf output but provided JSON instead."); - return; - } - string binary_protobuf; - Status status = - JsonToBinaryString(type_resolver_.get(), type_url_, - response.json_payload(), &binary_protobuf); - if (!status.ok()) { - ReportFailure(test_name, level, request, response, - "JSON output we received from test was unparseable."); - return; - } - - if (!test_message->ParseFromString(binary_protobuf)) { - ReportFailure(test_name, level, request, response, - "INTERNAL ERROR: internal JSON->protobuf transcode " - "yielded unparseable proto."); - return; - } - - break; - } - - case ConformanceResponse::kProtobufPayload: { - if (requested_output != conformance::PROTOBUF) { - ReportFailure( - test_name, level, request, response, - "Test was asked for JSON output but provided protobuf instead."); - return; - } - - if (!test_message->ParseFromString(response.protobuf_payload())) { - ReportFailure(test_name, level, request, response, - "Protobuf output we received from test was unparseable."); - return; - } - - break; - } - default: - GOOGLE_LOG(FATAL) << test_name << ": unknown payload type: " - << response.result_case(); + if (!ParseResponse(response, setting, test_message)) return; } MessageDifferencer differencer; @@ -336,7 +273,9 @@ void ConformanceTestSuite::RunValidBinaryInputTest( bool check; check = differencer.Compare(*reference_message, *test_message); if (check) { - ReportSuccess(test_name); + if (need_report_success) { + ReportSuccess(test_name); + } } else { ReportFailure(test_name, level, request, response, "Output was not equivalent to reference message: %s.", @@ -403,8 +342,33 @@ bool ConformanceTestSuite::CheckSetEmpty( } } -bool ConformanceTestSuite::RunSuite( - ConformanceTestRunner* runner, std::string* output) { +string ConformanceTestSuite::WireFormatToString( + WireFormat wire_format) { + switch (wire_format) { + case conformance::PROTOBUF: + return "PROTOBUF"; + case conformance::JSON: + return "JSON"; + case conformance::JSPB: + return "JSPB"; + case conformance::TEXT_FORMAT: + return "TEXT_FORMAT"; + case conformance::UNSPECIFIED: + return "UNSPECIFIED"; + default: + GOOGLE_LOG(FATAL) << "unknown wire type: " + << wire_format; + } + return ""; +} + +void ConformanceTestSuite::AddExpectedFailedTest(const std::string& test_name) { + expected_to_fail_.insert(test_name); +} + +bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner, + std::string* output, const string& filename, + conformance::FailureSet* failure_list) { runner_ = runner; successes_ = 0; expected_failures_ = 0; @@ -415,6 +379,11 @@ bool ConformanceTestSuite::RunSuite( output_ = "\nCONFORMANCE TEST BEGIN ====================================\n\n"; + failure_list_filename_ = filename; + expected_to_fail_.clear(); + for (const string& failure : failure_list->failure()) { + AddExpectedFailedTest(failure); + } RunSuiteImpl(); bool ok = true; diff --git a/conformance/conformance_test.h b/conformance/conformance_test.h index 87387350f3..4d741e7f15 100644 --- a/conformance/conformance_test.h +++ b/conformance/conformance_test.h @@ -62,6 +62,8 @@ class TestAllTypesProto3; namespace google { namespace protobuf { +class ConformanceTestSuite; + class ConformanceTestRunner { public: virtual ~ConformanceTestRunner() {} @@ -78,6 +80,37 @@ class ConformanceTestRunner { std::string* output) = 0; }; +// Test runner that spawns the process being tested and communicates with it +// over a pipe. +class ForkPipeRunner : public ConformanceTestRunner { + public: + // Note: Run() doesn't take ownership of the pointers inside suites. + static int Run(int argc, char *argv[], + const std::vector& suites); + + ForkPipeRunner(const std::string &executable) + : child_pid_(-1), executable_(executable) {} + + virtual ~ForkPipeRunner() {} + + void RunTest(const std::string& test_name, + const std::string& request, + std::string* response); + + private: + void SpawnTestProgram(); + + void CheckedWrite(int fd, const void *buf, size_t len); + bool TryRead(int fd, void *buf, size_t len); + void CheckedRead(int fd, void *buf, size_t len); + + int write_fd_; + int read_fd_; + pid_t child_pid_; + std::string executable_; + std::string current_test_name_; +}; + // Class representing the test suite itself. To run it, implement your own // class derived from ConformanceTestRunner, class derived from // ConformanceTestSuite and then write code like: @@ -89,46 +122,32 @@ class ConformanceTestRunner { // } // }; // -// // Force MyConformanceTestSuite to be added at dynamic initialization -// // time. -// struct StaticTestSuiteInitializer { -// StaticTestSuiteInitializer() { -// AddTestSuite(new MyConformanceTestSuite()); -// } -// } static_test_suite_initializer; -// // class MyConformanceTestRunner : public ConformanceTestRunner { // public: +// static int Run(int argc, char *argv[], +// ConformanceTestSuite* suite); +// +// private: // virtual void RunTest(...) { // // INSERT YOUR FRAMEWORK-SPECIFIC CODE HERE. // } // }; // // int main() { -// MyConformanceTestRunner runner; -// const std::set& test_suite_set = -// ::google::protobuf::GetTestSuiteSet(); -// for (auto suite : test_suite_set) { -// suite->RunSuite(&runner, &output); -// } +// MyConformanceTestSuite suite; +// MyConformanceTestRunner::Run(argc, argv, &suite); // } // class ConformanceTestSuite { public: - ConformanceTestSuite() : verbose_(false), enforce_recommended_(false) {} + ConformanceTestSuite() + : verbose_(false), + enforce_recommended_(false), + failure_list_flag_name_("--failure_list") {} virtual ~ConformanceTestSuite() {} void SetVerbose(bool verbose) { verbose_ = verbose; } - // Sets the list of tests that are expected to fail when RunSuite() is called. - // RunSuite() will fail unless the set of failing tests is exactly the same - // as this list. - // - // The filename here is *only* used to create/format useful error messages for - // how to update the failure list. We do NOT read this file at all. - void SetFailureList(const std::string& filename, - const std::vector& failure_list); - // Whether to require the testee to pass RECOMMENDED tests. By default failing // a RECOMMENDED test case will not fail the entire suite but will only // generated a warning. If this flag is set to true, RECOMMENDED tests will @@ -141,13 +160,26 @@ class ConformanceTestSuite { enforce_recommended_ = value; } + // Gets the flag name to the failure list file. + // By default, this would return --failure_list + string GetFailureListFlagName() { + return failure_list_flag_name_; + } + + void SetFailureListFlagName(const std::string& failure_list_flag_name) { + failure_list_flag_name_ = failure_list_flag_name; + } + // Run all the conformance tests against the given test runner. // Test output will be stored in "output". // // Returns true if the set of failing tests was exactly the same as the - // failure list. If SetFailureList() was not called, returns true if all - // tests passed. - bool RunSuite(ConformanceTestRunner* runner, std::string* output); + // failure list. + // The filename here is *only* used to create/format useful error messages for + // how to update the failure list. We do NOT read this file at all. + bool RunSuite(ConformanceTestRunner* runner, std::string* output, + const std::string& filename, + conformance::FailureSet* failure_list); protected: // Test cases are classified into a few categories: @@ -192,21 +224,44 @@ class ConformanceTestSuite { string ConformanceLevelToString(ConformanceLevel level) const; + void SetPrintUnknownFields(bool print_unknown_fields) { + request_.set_print_unknown_fields(true); + } + + void SetPrototypeMessageForCompare(const Message& message) { + prototype_message_for_compare_.reset(message.New()); + } + protected: virtual string InputFormatString(conformance::WireFormat format) const; virtual string OutputFormatString(conformance::WireFormat format) const; + conformance::ConformanceRequest request_; private: ConformanceLevel level_; ::conformance::WireFormat input_format_; ::conformance::WireFormat output_format_; const Message& prototype_message_; + std::unique_ptr prototype_message_for_compare_; string test_name_; - conformance::ConformanceRequest request_; }; bool CheckSetEmpty(const std::set& set_to_check, const std::string& write_to_file, const std::string& msg); + string WireFormatToString(conformance::WireFormat wire_format); + + // Parse payload in the response to the given message. Returns true on + // success. + virtual bool ParseResponse( + const conformance::ConformanceResponse& response, + const ConformanceRequestSetting& setting, + Message* test_message) = 0; + + void VerifyResponse( + const ConformanceRequestSetting& setting, + const string& equivalent_wire_format, + const conformance::ConformanceResponse& response, + bool need_report_success); void ReportSuccess(const std::string& test_name); void ReportFailure(const string& test_name, @@ -227,6 +282,8 @@ class ConformanceTestSuite { const conformance::ConformanceRequest& request, conformance::ConformanceResponse* response); + void AddExpectedFailedTest(const std::string& test_name); + virtual void RunSuiteImpl() = 0; ConformanceTestRunner* runner_; @@ -235,6 +292,7 @@ class ConformanceTestSuite { bool verbose_; bool enforce_recommended_; std::string output_; + std::string failure_list_flag_name_; std::string failure_list_filename_; // The set of test names that are expected to fail in this run, but haven't @@ -253,15 +311,8 @@ class ConformanceTestSuite { // The set of tests that the testee opted out of; std::set skipped_; - - std::unique_ptr - type_resolver_; - std::string type_url_; }; -void AddTestSuite(ConformanceTestSuite* suite); -const std::set& GetTestSuiteSet(); - } // namespace protobuf } // namespace google diff --git a/conformance/conformance_test_main.cc b/conformance/conformance_test_main.cc new file mode 100644 index 0000000000..c7ac9fc55a --- /dev/null +++ b/conformance/conformance_test_main.cc @@ -0,0 +1,40 @@ +// 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 "binary_json_conformance_suite.h" +#include "conformance_test.h" +#include "text_format_conformance_suite.h" + +int main(int argc, char *argv[]) { + google::protobuf::BinaryAndJsonConformanceSuite binary_and_json_suite; + google::protobuf::TextFormatConformanceTestSuite text_format_suite; + return google::protobuf::ForkPipeRunner::Run( + argc, argv, {&binary_and_json_suite, &text_format_suite}); +} diff --git a/conformance/conformance_test_runner.cc b/conformance/conformance_test_runner.cc index 3340f1cd12..e7aa0a552a 100644 --- a/conformance/conformance_test_runner.cc +++ b/conformance/conformance_test_runner.cc @@ -80,162 +80,31 @@ using std::vector; exit(1); \ } -// Test runner that spawns the process being tested and communicates with it -// over a pipe. -class ForkPipeRunner : public google::protobuf::ConformanceTestRunner { - public: - ForkPipeRunner(const std::string &executable) - : child_pid_(-1), executable_(executable) {} - - virtual ~ForkPipeRunner() {} - - void RunTest(const std::string& test_name, - const std::string& request, - std::string* response) { - if (child_pid_ < 0) { - SpawnTestProgram(); - } - - current_test_name_ = test_name; - - uint32_t len = request.size(); - CheckedWrite(write_fd_, &len, sizeof(uint32_t)); - CheckedWrite(write_fd_, request.c_str(), request.size()); - - if (!TryRead(read_fd_, &len, sizeof(uint32_t))) { - // We failed to read from the child, assume a crash and try to reap. - GOOGLE_LOG(INFO) << "Trying to reap child, pid=" << child_pid_; - - int status; - waitpid(child_pid_, &status, WEXITED); - - string error_msg; - if (WIFEXITED(status)) { - StringAppendF(&error_msg, - "child exited, status=%d", WEXITSTATUS(status)); - } else if (WIFSIGNALED(status)) { - StringAppendF(&error_msg, - "child killed by signal %d", WTERMSIG(status)); - } - GOOGLE_LOG(INFO) << error_msg; - child_pid_ = -1; - - conformance::ConformanceResponse response_obj; - response_obj.set_runtime_error(error_msg); - response_obj.SerializeToString(response); - return; - } +namespace google { +namespace protobuf { - response->resize(len); - CheckedRead(read_fd_, (void*)response->c_str(), len); - } - - private: - // TODO(haberman): make this work on Windows, instead of using these - // UNIX-specific APIs. - // - // There is a platform-agnostic API in - // src/google/protobuf/compiler/subprocess.h - // - // However that API only supports sending a single message to the subprocess. - // We really want to be able to send messages and receive responses one at a - // time: - // - // 1. Spawning a new process for each test would take way too long for thousands - // of tests and subprocesses like java that can take 100ms or more to start - // up. - // - // 2. Sending all the tests in one big message and receiving all results in one - // big message would take away our visibility about which test(s) caused a - // crash or other fatal error. It would also give us only a single failure - // instead of all of them. - void SpawnTestProgram() { - int toproc_pipe_fd[2]; - int fromproc_pipe_fd[2]; - if (pipe(toproc_pipe_fd) < 0 || pipe(fromproc_pipe_fd) < 0) { - perror("pipe"); - exit(1); - } - - pid_t pid = fork(); - if (pid < 0) { - perror("fork"); - exit(1); - } - - if (pid) { - // Parent. - CHECK_SYSCALL(close(toproc_pipe_fd[0])); - CHECK_SYSCALL(close(fromproc_pipe_fd[1])); - write_fd_ = toproc_pipe_fd[1]; - read_fd_ = fromproc_pipe_fd[0]; - child_pid_ = pid; - } else { - // Child. - CHECK_SYSCALL(close(STDIN_FILENO)); - CHECK_SYSCALL(close(STDOUT_FILENO)); - CHECK_SYSCALL(dup2(toproc_pipe_fd[0], STDIN_FILENO)); - CHECK_SYSCALL(dup2(fromproc_pipe_fd[1], STDOUT_FILENO)); - - CHECK_SYSCALL(close(toproc_pipe_fd[0])); - CHECK_SYSCALL(close(fromproc_pipe_fd[1])); - CHECK_SYSCALL(close(toproc_pipe_fd[1])); - CHECK_SYSCALL(close(fromproc_pipe_fd[0])); - - std::unique_ptr executable(new char[executable_.size() + 1]); - memcpy(executable.get(), executable_.c_str(), executable_.size()); - executable[executable_.size()] = '\0'; - - char *const argv[] = {executable.get(), NULL}; - CHECK_SYSCALL(execv(executable.get(), argv)); // Never returns. - } - } +void ParseFailureList(const char *filename, + conformance::FailureSet *failure_list) { + std::ifstream infile(filename); - void CheckedWrite(int fd, const void *buf, size_t len) { - if (write(fd, buf, len) != len) { - GOOGLE_LOG(FATAL) << current_test_name_ - << ": error writing to test program: " - << strerror(errno); - } + if (!infile.is_open()) { + fprintf(stderr, "Couldn't open failure list file: %s\n", filename); + exit(1); } - bool TryRead(int fd, void *buf, size_t len) { - size_t ofs = 0; - while (len > 0) { - ssize_t bytes_read = read(fd, (char*)buf + ofs, len); - - if (bytes_read == 0) { - GOOGLE_LOG(ERROR) << current_test_name_ - << ": unexpected EOF from test program"; - return false; - } else if (bytes_read < 0) { - GOOGLE_LOG(ERROR) << current_test_name_ - << ": error reading from test program: " - << strerror(errno); - return false; - } - - len -= bytes_read; - ofs += bytes_read; - } + for (string line; getline(infile, line);) { + // Remove whitespace. + line.erase(std::remove_if(line.begin(), line.end(), ::isspace), + line.end()); - return true; - } + // Remove comments. + line = line.substr(0, line.find("#")); - void CheckedRead(int fd, void *buf, size_t len) { - if (!TryRead(fd, buf, len)) { - GOOGLE_LOG(FATAL) << current_test_name_ - << ": error reading from test program: " - << strerror(errno); + if (!line.empty()) { + failure_list->add_failure(line); } } - - int write_fd_; - int read_fd_; - pid_t child_pid_; - std::string executable_; - std::string current_test_name_; -}; +} void UsageError() { fprintf(stderr, @@ -250,6 +119,19 @@ void UsageError() { " should contain one test name per\n"); fprintf(stderr, " line. Use '#' for comments.\n"); + fprintf(stderr, + " --text_format_failure_list Use to specify list \n"); + fprintf(stderr, + " of tests that are expected to \n"); + fprintf(stderr, + " fail in the \n"); + fprintf(stderr, + " text_format_conformance_suite. \n"); + fprintf(stderr, + " File should contain one test name \n"); + fprintf(stderr, + " per line. Use '#' for comments.\n"); + fprintf(stderr, " --enforce_recommended Enforce that recommended test\n"); fprintf(stderr, @@ -263,73 +145,199 @@ void UsageError() { exit(1); } -void ParseFailureList(const char *filename, std::vector* failure_list) { - std::ifstream infile(filename); - - if (!infile.is_open()) { - fprintf(stderr, "Couldn't open failure list file: %s\n", filename); - exit(1); +void ForkPipeRunner::RunTest( + const std::string& test_name, + const std::string& request, + std::string* response) { + if (child_pid_ < 0) { + SpawnTestProgram(); } - for (string line; getline(infile, line);) { - // Remove whitespace. - line.erase(std::remove_if(line.begin(), line.end(), ::isspace), - line.end()); + current_test_name_ = test_name; - // Remove comments. - line = line.substr(0, line.find("#")); + uint32_t len = request.size(); + CheckedWrite(write_fd_, &len, sizeof(uint32_t)); + CheckedWrite(write_fd_, request.c_str(), request.size()); - if (!line.empty()) { - failure_list->push_back(line); + if (!TryRead(read_fd_, &len, sizeof(uint32_t))) { + // We failed to read from the child, assume a crash and try to reap. + GOOGLE_LOG(INFO) << "Trying to reap child, pid=" << child_pid_; + + int status; + waitpid(child_pid_, &status, WEXITED); + + string error_msg; + if (WIFEXITED(status)) { + StringAppendF(&error_msg, + "child exited, status=%d", WEXITSTATUS(status)); + } else if (WIFSIGNALED(status)) { + StringAppendF(&error_msg, + "child killed by signal %d", WTERMSIG(status)); } + GOOGLE_LOG(INFO) << error_msg; + child_pid_ = -1; + + conformance::ConformanceResponse response_obj; + response_obj.set_runtime_error(error_msg); + response_obj.SerializeToString(response); + return; } + + response->resize(len); + CheckedRead(read_fd_, (void*)response->c_str(), len); } -int main(int argc, char *argv[]) { - char *program; - const std::set& test_suite_set = - ::google::protobuf::GetTestSuiteSet(); - - string failure_list_filename; - std::vector failure_list; - - for (int arg = 1; arg < argc; ++arg) { - if (strcmp(argv[arg], "--failure_list") == 0) { - if (++arg == argc) UsageError(); - failure_list_filename = argv[arg]; - ParseFailureList(argv[arg], &failure_list); - } else if (strcmp(argv[arg], "--verbose") == 0) { - for (auto *suite : test_suite_set) { +int ForkPipeRunner::Run( + int argc, char *argv[], const std::vector& suites) { + if (suites.empty()) { + fprintf(stderr, "No test suites found.\n"); + return EXIT_FAILURE; + } + bool all_ok = true; + for (ConformanceTestSuite* suite : suites) { + char *program; + string failure_list_filename; + conformance::FailureSet failure_list; + + for (int arg = 1; arg < argc; ++arg) { + if (strcmp(argv[arg], suite->GetFailureListFlagName().c_str()) == 0) { + if (++arg == argc) UsageError(); + failure_list_filename = argv[arg]; + ParseFailureList(argv[arg], &failure_list); + } else if (strcmp(argv[arg], "--verbose") == 0) { suite->SetVerbose(true); - } - } else if (strcmp(argv[arg], "--enforce_recommended") == 0) { - for (auto suite : test_suite_set) { + } else if (strcmp(argv[arg], "--enforce_recommended") == 0) { suite->SetEnforceRecommended(true); + } else if (argv[arg][0] == '-') { + bool recognized_flag = false; + for (ConformanceTestSuite* suite : suites) { + if (strcmp(argv[arg], suite->GetFailureListFlagName().c_str()) == 0) { + if (++arg == argc) UsageError(); + recognized_flag = true; + } + } + if (!recognized_flag) { + fprintf(stderr, "Unknown option: %s\n", argv[arg]); + UsageError(); + } + } else { + if (arg != argc - 1) { + fprintf(stderr, "Too many arguments.\n"); + UsageError(); + } + program = argv[arg]; } - } else if (argv[arg][0] == '-') { - fprintf(stderr, "Unknown option: %s\n", argv[arg]); - UsageError(); - } else { - if (arg != argc - 1) { - fprintf(stderr, "Too many arguments.\n"); - UsageError(); - } - program = argv[arg]; } + + ForkPipeRunner runner(program); + + std::string output; + all_ok = all_ok && + suite->RunSuite(&runner, &output, failure_list_filename, &failure_list); + + fwrite(output.c_str(), 1, output.size(), stderr); + } + return all_ok ? EXIT_SUCCESS : EXIT_FAILURE; +} + +// TODO(haberman): make this work on Windows, instead of using these +// UNIX-specific APIs. +// +// There is a platform-agnostic API in +// src/google/protobuf/compiler/subprocess.h +// +// However that API only supports sending a single message to the subprocess. +// We really want to be able to send messages and receive responses one at a +// time: +// +// 1. Spawning a new process for each test would take way too long for thousands +// of tests and subprocesses like java that can take 100ms or more to start +// up. +// +// 2. Sending all the tests in one big message and receiving all results in one +// big message would take away our visibility about which test(s) caused a +// crash or other fatal error. It would also give us only a single failure +// instead of all of them. +void ForkPipeRunner::SpawnTestProgram() { + int toproc_pipe_fd[2]; + int fromproc_pipe_fd[2]; + if (pipe(toproc_pipe_fd) < 0 || pipe(fromproc_pipe_fd) < 0) { + perror("pipe"); + exit(1); + } + + pid_t pid = fork(); + if (pid < 0) { + perror("fork"); + exit(1); } - for (auto suite : test_suite_set) { - suite->SetFailureList(failure_list_filename, failure_list); + if (pid) { + // Parent. + CHECK_SYSCALL(close(toproc_pipe_fd[0])); + CHECK_SYSCALL(close(fromproc_pipe_fd[1])); + write_fd_ = toproc_pipe_fd[1]; + read_fd_ = fromproc_pipe_fd[0]; + child_pid_ = pid; + } else { + // Child. + CHECK_SYSCALL(close(STDIN_FILENO)); + CHECK_SYSCALL(close(STDOUT_FILENO)); + CHECK_SYSCALL(dup2(toproc_pipe_fd[0], STDIN_FILENO)); + CHECK_SYSCALL(dup2(fromproc_pipe_fd[1], STDOUT_FILENO)); + + CHECK_SYSCALL(close(toproc_pipe_fd[0])); + CHECK_SYSCALL(close(fromproc_pipe_fd[1])); + CHECK_SYSCALL(close(toproc_pipe_fd[1])); + CHECK_SYSCALL(close(fromproc_pipe_fd[0])); + + std::unique_ptr executable(new char[executable_.size() + 1]); + memcpy(executable.get(), executable_.c_str(), executable_.size()); + executable[executable_.size()] = '\0'; + + char *const argv[] = {executable.get(), NULL}; + CHECK_SYSCALL(execv(executable.get(), argv)); // Never returns. } - ForkPipeRunner runner(program); +} - std::string output; - bool ok = true; - for (auto suite : test_suite_set) { - ok &= suite->RunSuite(&runner, &output); +void ForkPipeRunner::CheckedWrite(int fd, const void *buf, size_t len) { + if (write(fd, buf, len) != len) { + GOOGLE_LOG(FATAL) << current_test_name_ + << ": error writing to test program: " + << strerror(errno); } +} - fwrite(output.c_str(), 1, output.size(), stderr); +bool ForkPipeRunner::TryRead(int fd, void *buf, size_t len) { + size_t ofs = 0; + while (len > 0) { + ssize_t bytes_read = read(fd, (char*)buf + ofs, len); + + if (bytes_read == 0) { + GOOGLE_LOG(ERROR) << current_test_name_ + << ": unexpected EOF from test program"; + return false; + } else if (bytes_read < 0) { + GOOGLE_LOG(ERROR) << current_test_name_ + << ": error reading from test program: " + << strerror(errno); + return false; + } - return ok ? EXIT_SUCCESS : EXIT_FAILURE; + len -= bytes_read; + ofs += bytes_read; + } + + return true; } + +void ForkPipeRunner::CheckedRead(int fd, void *buf, size_t len) { + if (!TryRead(fd, buf, len)) { + GOOGLE_LOG(FATAL) << current_test_name_ + << ": error reading from test program: " + << strerror(errno); + } +} + +} // namespace protobuf +} // namespace google diff --git a/conformance/failure_list_cpp.txt b/conformance/failure_list_cpp.txt index 752fbb5d9b..0c01e1e40c 100644 --- a/conformance/failure_list_cpp.txt +++ b/conformance/failure_list_cpp.txt @@ -34,23 +34,3 @@ Recommended.Proto3.JsonInput.TrailingCommaInAnObject Recommended.Proto3.JsonInput.TrailingCommaInAnObjectWithNewlines Recommended.Proto3.JsonInput.TrailingCommaInAnObjectWithSpace Recommended.Proto3.JsonInput.TrailingCommaInAnObjectWithSpaceCommaSpace -Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE -Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE -Required.Proto3.ProtobufInput.PrematureEofInPackedField.BOOL -Required.Proto3.ProtobufInput.PrematureEofInPackedField.ENUM -Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT32 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT64 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT32 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT64 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT32 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT64 -Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE -Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE -Required.Proto2.ProtobufInput.PrematureEofInPackedField.BOOL -Required.Proto2.ProtobufInput.PrematureEofInPackedField.ENUM -Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT32 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT64 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT32 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT64 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT32 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT64 diff --git a/conformance/failure_list_php.txt b/conformance/failure_list_php.txt index 0d2341127e..28ca461d47 100644 --- a/conformance/failure_list_php.txt +++ b/conformance/failure_list_php.txt @@ -3,16 +3,16 @@ Recommended.FieldMaskPathsDontRoundTrip.JsonOutput Recommended.FieldMaskTooManyUnderscore.JsonOutput Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput -Recommended.Proto3.JsonInput.DurationHas3FractionalDigits.Validator -Recommended.Proto3.JsonInput.DurationHas6FractionalDigits.Validator -Recommended.Proto3.JsonInput.DurationHas9FractionalDigits.Validator -Recommended.Proto3.JsonInput.DurationHasZeroFractionalDigit.Validator Recommended.Proto3.JsonInput.FieldMaskInvalidCharacter Required.Proto3.JsonInput.FloatFieldTooLarge Required.Proto3.JsonInput.FloatFieldTooSmall Required.Proto3.JsonInput.DoubleFieldTooSmall Required.Proto3.JsonInput.Int32FieldNotInteger Required.Proto3.JsonInput.Int64FieldNotInteger +Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotInt +Required.Proto3.JsonInput.RepeatedListValue.JsonOutput +Required.Proto3.JsonInput.RepeatedListValue.ProtobufOutput +Required.Proto3.JsonInput.StringFieldNotAString Required.Proto3.JsonInput.Uint32FieldNotInteger Required.Proto3.JsonInput.Uint64FieldNotInteger Required.Proto3.JsonInput.Int32FieldLeadingSpace diff --git a/conformance/failure_list_php_c.txt b/conformance/failure_list_php_c.txt index bb32ace57c..8f7af61855 100644 --- a/conformance/failure_list_php_c.txt +++ b/conformance/failure_list_php_c.txt @@ -5,9 +5,7 @@ Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput Recommended.Proto3.JsonInput.DurationHas3FractionalDigits.Validator Recommended.Proto3.JsonInput.DurationHas6FractionalDigits.Validator -Recommended.Proto3.JsonInput.DurationHas9FractionalDigits.Validator -Recommended.Proto3.JsonInput.DurationHasZeroFractionalDigit.Validator -Recommended.Proto3.JsonInput.Int64FieldBeString.Validator +Recommended.Proto3.JsonInput.FieldMaskInvalidCharacter Recommended.Proto3.JsonInput.MapFieldValueIsNull Recommended.Proto3.JsonInput.OneofZeroBytes.JsonOutput Recommended.Proto3.JsonInput.RepeatedFieldMessageElementIsNull @@ -18,93 +16,29 @@ Recommended.Proto3.JsonInput.StringFieldUnpairedHighSurrogate Recommended.Proto3.JsonInput.StringFieldUnpairedLowSurrogate Recommended.Proto3.JsonInput.TimestampHas3FractionalDigits.Validator Recommended.Proto3.JsonInput.TimestampHas6FractionalDigits.Validator -Recommended.Proto3.JsonInput.TimestampHas9FractionalDigits.Validator -Recommended.Proto3.JsonInput.TimestampHasZeroFractionalDigit.Validator -Recommended.Proto3.JsonInput.TimestampZeroNormalized.Validator -Recommended.Proto3.JsonInput.Uint64FieldBeString.Validator Recommended.Proto3.ProtobufInput.OneofZeroBytes.JsonOutput Required.DurationProtoInputTooLarge.JsonOutput Required.DurationProtoInputTooSmall.JsonOutput -Required.Proto3.JsonInput.Any.JsonOutput -Required.Proto3.JsonInput.Any.ProtobufOutput -Required.Proto3.JsonInput.AnyNested.JsonOutput -Required.Proto3.JsonInput.AnyNested.ProtobufOutput -Required.Proto3.JsonInput.AnyUnorderedTypeTag.JsonOutput -Required.Proto3.JsonInput.AnyUnorderedTypeTag.ProtobufOutput -Required.Proto3.JsonInput.AnyWithDuration.JsonOutput -Required.Proto3.JsonInput.AnyWithDuration.ProtobufOutput -Required.Proto3.JsonInput.AnyWithFieldMask.JsonOutput -Required.Proto3.JsonInput.AnyWithFieldMask.ProtobufOutput -Required.Proto3.JsonInput.AnyWithInt32ValueWrapper.JsonOutput -Required.Proto3.JsonInput.AnyWithInt32ValueWrapper.ProtobufOutput -Required.Proto3.JsonInput.AnyWithStruct.JsonOutput -Required.Proto3.JsonInput.AnyWithStruct.ProtobufOutput -Required.Proto3.JsonInput.AnyWithTimestamp.JsonOutput -Required.Proto3.JsonInput.AnyWithTimestamp.ProtobufOutput -Required.Proto3.JsonInput.AnyWithValueForInteger.JsonOutput -Required.Proto3.JsonInput.AnyWithValueForInteger.ProtobufOutput -Required.Proto3.JsonInput.AnyWithValueForJsonObject.JsonOutput -Required.Proto3.JsonInput.AnyWithValueForJsonObject.ProtobufOutput -Required.Proto3.JsonInput.BoolMapField.JsonOutput +Required.TimestampProtoInputTooLarge.JsonOutput +Required.TimestampProtoInputTooSmall.JsonOutput Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.JsonOutput Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.ProtobufOutput Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.JsonOutput Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.ProtobufOutput Required.Proto3.JsonInput.DoubleFieldNan.JsonOutput -Required.Proto3.JsonInput.DurationMaxValue.JsonOutput -Required.Proto3.JsonInput.DurationMaxValue.ProtobufOutput Required.Proto3.JsonInput.DurationMinValue.JsonOutput -Required.Proto3.JsonInput.DurationMinValue.ProtobufOutput Required.Proto3.JsonInput.DurationRepeatedValue.JsonOutput -Required.Proto3.JsonInput.DurationRepeatedValue.ProtobufOutput -Required.Proto3.JsonInput.FieldMask.JsonOutput -Required.Proto3.JsonInput.FieldMask.ProtobufOutput Required.Proto3.JsonInput.FloatFieldInfinity.JsonOutput Required.Proto3.JsonInput.FloatFieldNan.JsonOutput Required.Proto3.JsonInput.FloatFieldNegativeInfinity.JsonOutput Required.Proto3.JsonInput.OneofFieldDuplicate Required.Proto3.JsonInput.OptionalWrapperTypesWithNonDefaultValue.JsonOutput Required.Proto3.JsonInput.OptionalWrapperTypesWithNonDefaultValue.ProtobufOutput -Required.Proto3.JsonInput.StringFieldEscape.JsonOutput -Required.Proto3.JsonInput.StringFieldEscape.ProtobufOutput Required.Proto3.JsonInput.StringFieldSurrogatePair.JsonOutput Required.Proto3.JsonInput.StringFieldSurrogatePair.ProtobufOutput -Required.Proto3.JsonInput.StringFieldUnicodeEscape.JsonOutput -Required.Proto3.JsonInput.StringFieldUnicodeEscape.ProtobufOutput -Required.Proto3.JsonInput.StringFieldUnicodeEscapeWithLowercaseHexLetters.JsonOutput -Required.Proto3.JsonInput.StringFieldUnicodeEscapeWithLowercaseHexLetters.ProtobufOutput -Required.Proto3.JsonInput.Struct.JsonOutput -Required.Proto3.JsonInput.Struct.ProtobufOutput -Required.Proto3.JsonInput.TimestampMaxValue.JsonOutput -Required.Proto3.JsonInput.TimestampMaxValue.ProtobufOutput -Required.Proto3.JsonInput.TimestampMinValue.JsonOutput -Required.Proto3.JsonInput.TimestampMinValue.ProtobufOutput -Required.Proto3.JsonInput.TimestampRepeatedValue.JsonOutput -Required.Proto3.JsonInput.TimestampRepeatedValue.ProtobufOutput -Required.Proto3.JsonInput.TimestampWithNegativeOffset.JsonOutput -Required.Proto3.JsonInput.TimestampWithNegativeOffset.ProtobufOutput -Required.Proto3.JsonInput.TimestampWithPositiveOffset.JsonOutput -Required.Proto3.JsonInput.TimestampWithPositiveOffset.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptBool.JsonOutput -Required.Proto3.JsonInput.ValueAcceptBool.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptFloat.JsonOutput -Required.Proto3.JsonInput.ValueAcceptFloat.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptInteger.JsonOutput -Required.Proto3.JsonInput.ValueAcceptInteger.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptList.JsonOutput -Required.Proto3.JsonInput.ValueAcceptList.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptListWithNull.JsonOutput -Required.Proto3.JsonInput.ValueAcceptListWithNull.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptNull.JsonOutput -Required.Proto3.JsonInput.ValueAcceptNull.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptObject.JsonOutput -Required.Proto3.JsonInput.ValueAcceptObject.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptString.JsonOutput -Required.Proto3.JsonInput.ValueAcceptString.ProtobufOutput Required.Proto3.ProtobufInput.DoubleFieldNormalizeQuietNan.JsonOutput Required.Proto3.ProtobufInput.DoubleFieldNormalizeSignalingNan.JsonOutput Required.Proto3.ProtobufInput.FloatFieldNormalizeQuietNan.JsonOutput Required.Proto3.ProtobufInput.FloatFieldNormalizeSignalingNan.JsonOutput Required.Proto3.ProtobufInput.ValidDataRepeated.FLOAT.JsonOutput -Required.TimestampProtoInputTooLarge.JsonOutput -Required.TimestampProtoInputTooSmall.JsonOutput +Required.Proto3.JsonInput.RejectTopLevelNull diff --git a/conformance/failure_list_python_cpp.txt b/conformance/failure_list_python_cpp.txt index a498ad1a3e..59a969d2f5 100644 --- a/conformance/failure_list_python_cpp.txt +++ b/conformance/failure_list_python_cpp.txt @@ -20,35 +20,3 @@ Required.Proto3.JsonInput.FloatFieldTooLarge Required.Proto3.JsonInput.FloatFieldTooSmall Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingIntegersGotBool Required.Proto3.JsonInput.TimestampJsonInputLowercaseT -Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE -Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE -Required.Proto3.ProtobufInput.PrematureEofInPackedField.BOOL -Required.Proto3.ProtobufInput.PrematureEofInPackedField.DOUBLE -Required.Proto3.ProtobufInput.PrematureEofInPackedField.ENUM -Required.Proto3.ProtobufInput.PrematureEofInPackedField.FIXED32 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.FIXED64 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.FLOAT -Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT32 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT64 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.SFIXED32 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.SFIXED64 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT32 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT64 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT32 -Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT64 -Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE -Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE -Required.Proto2.ProtobufInput.PrematureEofInPackedField.BOOL -Required.Proto2.ProtobufInput.PrematureEofInPackedField.DOUBLE -Required.Proto2.ProtobufInput.PrematureEofInPackedField.ENUM -Required.Proto2.ProtobufInput.PrematureEofInPackedField.FIXED32 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.FIXED64 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.FLOAT -Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT32 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT64 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.SFIXED32 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.SFIXED64 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT32 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT64 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT32 -Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT64 diff --git a/conformance/failure_list_ruby.txt b/conformance/failure_list_ruby.txt index 1bab1359fc..fc6455d8e4 100644 --- a/conformance/failure_list_ruby.txt +++ b/conformance/failure_list_ruby.txt @@ -5,9 +5,7 @@ Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput Recommended.Proto3.JsonInput.DurationHas3FractionalDigits.Validator Recommended.Proto3.JsonInput.DurationHas6FractionalDigits.Validator -Recommended.Proto3.JsonInput.DurationHas9FractionalDigits.Validator -Recommended.Proto3.JsonInput.DurationHasZeroFractionalDigit.Validator -Recommended.Proto3.JsonInput.Int64FieldBeString.Validator +Recommended.Proto3.JsonInput.FieldMaskInvalidCharacter Recommended.Proto3.JsonInput.MapFieldValueIsNull Recommended.Proto3.JsonInput.RepeatedFieldMessageElementIsNull Recommended.Proto3.JsonInput.RepeatedFieldPrimitiveElementIsNull @@ -17,115 +15,41 @@ Recommended.Proto3.JsonInput.StringFieldUnpairedHighSurrogate Recommended.Proto3.JsonInput.StringFieldUnpairedLowSurrogate Recommended.Proto3.JsonInput.TimestampHas3FractionalDigits.Validator Recommended.Proto3.JsonInput.TimestampHas6FractionalDigits.Validator -Recommended.Proto3.JsonInput.TimestampHas9FractionalDigits.Validator -Recommended.Proto3.JsonInput.TimestampHasZeroFractionalDigit.Validator -Recommended.Proto3.JsonInput.TimestampZeroNormalized.Validator -Recommended.Proto3.JsonInput.Uint64FieldBeString.Validator Required.DurationProtoInputTooLarge.JsonOutput Required.DurationProtoInputTooSmall.JsonOutput -Required.Proto3.JsonInput.Any.JsonOutput -Required.Proto3.JsonInput.Any.ProtobufOutput -Required.Proto3.JsonInput.AnyNested.JsonOutput -Required.Proto3.JsonInput.AnyNested.ProtobufOutput -Required.Proto3.JsonInput.AnyUnorderedTypeTag.JsonOutput -Required.Proto3.JsonInput.AnyUnorderedTypeTag.ProtobufOutput -Required.Proto3.JsonInput.AnyWithDuration.JsonOutput -Required.Proto3.JsonInput.AnyWithDuration.ProtobufOutput -Required.Proto3.JsonInput.AnyWithFieldMask.JsonOutput -Required.Proto3.JsonInput.AnyWithFieldMask.ProtobufOutput -Required.Proto3.JsonInput.AnyWithInt32ValueWrapper.JsonOutput -Required.Proto3.JsonInput.AnyWithInt32ValueWrapper.ProtobufOutput -Required.Proto3.JsonInput.AnyWithStruct.JsonOutput -Required.Proto3.JsonInput.AnyWithStruct.ProtobufOutput -Required.Proto3.JsonInput.AnyWithTimestamp.JsonOutput -Required.Proto3.JsonInput.AnyWithTimestamp.ProtobufOutput -Required.Proto3.JsonInput.AnyWithValueForInteger.JsonOutput -Required.Proto3.JsonInput.AnyWithValueForInteger.ProtobufOutput -Required.Proto3.JsonInput.AnyWithValueForJsonObject.JsonOutput -Required.Proto3.JsonInput.AnyWithValueForJsonObject.ProtobufOutput Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.JsonOutput Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.ProtobufOutput Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.JsonOutput Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.ProtobufOutput Required.Proto3.JsonInput.DoubleFieldNan.JsonOutput -Required.Proto3.JsonInput.DurationMaxValue.JsonOutput -Required.Proto3.JsonInput.DurationMaxValue.ProtobufOutput Required.Proto3.JsonInput.DurationMinValue.JsonOutput -Required.Proto3.JsonInput.DurationMinValue.ProtobufOutput Required.Proto3.JsonInput.DurationRepeatedValue.JsonOutput -Required.Proto3.JsonInput.DurationRepeatedValue.ProtobufOutput -Required.Proto3.JsonInput.FieldMask.JsonOutput -Required.Proto3.JsonInput.FieldMask.ProtobufOutput Required.Proto3.JsonInput.FloatFieldInfinity.JsonOutput Required.Proto3.JsonInput.FloatFieldNan.JsonOutput Required.Proto3.JsonInput.FloatFieldNegativeInfinity.JsonOutput Required.Proto3.JsonInput.OneofFieldDuplicate Required.Proto3.JsonInput.OptionalBoolWrapper.JsonOutput -Required.Proto3.JsonInput.OptionalBoolWrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalBytesWrapper.JsonOutput -Required.Proto3.JsonInput.OptionalBytesWrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalDoubleWrapper.JsonOutput -Required.Proto3.JsonInput.OptionalDoubleWrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalFloatWrapper.JsonOutput -Required.Proto3.JsonInput.OptionalFloatWrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalInt32Wrapper.JsonOutput -Required.Proto3.JsonInput.OptionalInt32Wrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalInt64Wrapper.JsonOutput -Required.Proto3.JsonInput.OptionalInt64Wrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalStringWrapper.JsonOutput -Required.Proto3.JsonInput.OptionalStringWrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalUint32Wrapper.JsonOutput -Required.Proto3.JsonInput.OptionalUint32Wrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalUint64Wrapper.JsonOutput -Required.Proto3.JsonInput.OptionalUint64Wrapper.ProtobufOutput Required.Proto3.JsonInput.OptionalWrapperTypesWithNonDefaultValue.JsonOutput Required.Proto3.JsonInput.OptionalWrapperTypesWithNonDefaultValue.ProtobufOutput Required.Proto3.JsonInput.RepeatedBoolWrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedBoolWrapper.ProtobufOutput Required.Proto3.JsonInput.RepeatedBytesWrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedBytesWrapper.ProtobufOutput Required.Proto3.JsonInput.RepeatedDoubleWrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedDoubleWrapper.ProtobufOutput Required.Proto3.JsonInput.RepeatedFloatWrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedFloatWrapper.ProtobufOutput Required.Proto3.JsonInput.RepeatedInt32Wrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedInt32Wrapper.ProtobufOutput Required.Proto3.JsonInput.RepeatedInt64Wrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedInt64Wrapper.ProtobufOutput Required.Proto3.JsonInput.RepeatedStringWrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedStringWrapper.ProtobufOutput Required.Proto3.JsonInput.RepeatedUint32Wrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedUint32Wrapper.ProtobufOutput Required.Proto3.JsonInput.RepeatedUint64Wrapper.JsonOutput -Required.Proto3.JsonInput.RepeatedUint64Wrapper.ProtobufOutput Required.Proto3.JsonInput.StringFieldSurrogatePair.JsonOutput Required.Proto3.JsonInput.StringFieldSurrogatePair.ProtobufOutput -Required.Proto3.JsonInput.Struct.JsonOutput -Required.Proto3.JsonInput.Struct.ProtobufOutput -Required.Proto3.JsonInput.TimestampMaxValue.JsonOutput -Required.Proto3.JsonInput.TimestampMaxValue.ProtobufOutput -Required.Proto3.JsonInput.TimestampMinValue.JsonOutput -Required.Proto3.JsonInput.TimestampMinValue.ProtobufOutput -Required.Proto3.JsonInput.TimestampRepeatedValue.JsonOutput -Required.Proto3.JsonInput.TimestampRepeatedValue.ProtobufOutput -Required.Proto3.JsonInput.TimestampWithNegativeOffset.JsonOutput -Required.Proto3.JsonInput.TimestampWithNegativeOffset.ProtobufOutput -Required.Proto3.JsonInput.TimestampWithPositiveOffset.JsonOutput -Required.Proto3.JsonInput.TimestampWithPositiveOffset.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptBool.JsonOutput -Required.Proto3.JsonInput.ValueAcceptBool.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptFloat.JsonOutput -Required.Proto3.JsonInput.ValueAcceptFloat.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptInteger.JsonOutput -Required.Proto3.JsonInput.ValueAcceptInteger.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptList.JsonOutput -Required.Proto3.JsonInput.ValueAcceptList.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptNull.JsonOutput -Required.Proto3.JsonInput.ValueAcceptNull.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptObject.JsonOutput -Required.Proto3.JsonInput.ValueAcceptObject.ProtobufOutput -Required.Proto3.JsonInput.ValueAcceptString.JsonOutput -Required.Proto3.JsonInput.ValueAcceptString.ProtobufOutput Required.Proto3.ProtobufInput.DoubleFieldNormalizeQuietNan.JsonOutput Required.Proto3.ProtobufInput.DoubleFieldNormalizeSignalingNan.JsonOutput Required.Proto3.ProtobufInput.FloatFieldNormalizeQuietNan.JsonOutput @@ -133,3 +57,10 @@ Required.Proto3.ProtobufInput.FloatFieldNormalizeSignalingNan.JsonOutput Required.Proto3.ProtobufInput.ValidDataRepeated.FLOAT.JsonOutput Required.TimestampProtoInputTooLarge.JsonOutput Required.TimestampProtoInputTooSmall.JsonOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonFalse.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonNull.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonNumber.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonObject.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonString.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonTrue.ProtobufOutput +Required.Proto3.JsonInput.RejectTopLevelNull diff --git a/conformance/failure_list_ruby_mac.txt b/conformance/failure_list_ruby_mac.txt new file mode 100644 index 0000000000..153b0a2019 --- /dev/null +++ b/conformance/failure_list_ruby_mac.txt @@ -0,0 +1,66 @@ +Recommended.FieldMaskNumbersDontRoundTrip.JsonOutput +Recommended.FieldMaskPathsDontRoundTrip.JsonOutput +Recommended.FieldMaskTooManyUnderscore.JsonOutput +Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput +Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput +Recommended.Proto3.JsonInput.DurationHas3FractionalDigits.Validator +Recommended.Proto3.JsonInput.DurationHas6FractionalDigits.Validator +Recommended.Proto3.JsonInput.MapFieldValueIsNull +Recommended.Proto3.JsonInput.RepeatedFieldMessageElementIsNull +Recommended.Proto3.JsonInput.RepeatedFieldPrimitiveElementIsNull +Recommended.Proto3.JsonInput.StringEndsWithEscapeChar +Recommended.Proto3.JsonInput.StringFieldSurrogateInWrongOrder +Recommended.Proto3.JsonInput.StringFieldUnpairedHighSurrogate +Recommended.Proto3.JsonInput.StringFieldUnpairedLowSurrogate +Recommended.Proto3.JsonInput.TimestampHas3FractionalDigits.Validator +Recommended.Proto3.JsonInput.TimestampHas6FractionalDigits.Validator +Required.DurationProtoInputTooLarge.JsonOutput +Required.DurationProtoInputTooSmall.JsonOutput +Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.JsonOutput +Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.ProtobufOutput +Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.JsonOutput +Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.ProtobufOutput +Required.Proto3.JsonInput.DoubleFieldNan.JsonOutput +Required.Proto3.JsonInput.DurationMinValue.JsonOutput +Required.Proto3.JsonInput.DurationRepeatedValue.JsonOutput +Required.Proto3.JsonInput.FloatFieldInfinity.JsonOutput +Required.Proto3.JsonInput.FloatFieldNan.JsonOutput +Required.Proto3.JsonInput.FloatFieldNegativeInfinity.JsonOutput +Required.Proto3.JsonInput.OneofFieldDuplicate +Required.Proto3.JsonInput.OptionalBoolWrapper.JsonOutput +Required.Proto3.JsonInput.OptionalBytesWrapper.JsonOutput +Required.Proto3.JsonInput.OptionalDoubleWrapper.JsonOutput +Required.Proto3.JsonInput.OptionalFloatWrapper.JsonOutput +Required.Proto3.JsonInput.OptionalInt32Wrapper.JsonOutput +Required.Proto3.JsonInput.OptionalInt64Wrapper.JsonOutput +Required.Proto3.JsonInput.OptionalStringWrapper.JsonOutput +Required.Proto3.JsonInput.OptionalUint32Wrapper.JsonOutput +Required.Proto3.JsonInput.OptionalUint64Wrapper.JsonOutput +Required.Proto3.JsonInput.OptionalWrapperTypesWithNonDefaultValue.JsonOutput +Required.Proto3.JsonInput.OptionalWrapperTypesWithNonDefaultValue.ProtobufOutput +Required.Proto3.JsonInput.RepeatedBoolWrapper.JsonOutput +Required.Proto3.JsonInput.RepeatedBytesWrapper.JsonOutput +Required.Proto3.JsonInput.RepeatedDoubleWrapper.JsonOutput +Required.Proto3.JsonInput.RepeatedFloatWrapper.JsonOutput +Required.Proto3.JsonInput.RepeatedInt32Wrapper.JsonOutput +Required.Proto3.JsonInput.RepeatedInt64Wrapper.JsonOutput +Required.Proto3.JsonInput.RepeatedStringWrapper.JsonOutput +Required.Proto3.JsonInput.RepeatedUint32Wrapper.JsonOutput +Required.Proto3.JsonInput.RepeatedUint64Wrapper.JsonOutput +Required.Proto3.JsonInput.StringFieldSurrogatePair.JsonOutput +Required.Proto3.JsonInput.StringFieldSurrogatePair.ProtobufOutput +Required.Proto3.ProtobufInput.DoubleFieldNormalizeQuietNan.JsonOutput +Required.Proto3.ProtobufInput.DoubleFieldNormalizeSignalingNan.JsonOutput +Required.Proto3.ProtobufInput.FloatFieldNormalizeQuietNan.JsonOutput +Required.Proto3.ProtobufInput.FloatFieldNormalizeSignalingNan.JsonOutput +Required.Proto3.ProtobufInput.ValidDataRepeated.FLOAT.JsonOutput +Required.TimestampProtoInputTooLarge.JsonOutput +Required.TimestampProtoInputTooSmall.JsonOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonFalse.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonNull.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonNumber.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonObject.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonString.ProtobufOutput +Required.Proto3.JsonInput.IgnoreUnknownJsonTrue.ProtobufOutput +Recommended.Proto3.JsonInput.FieldMaskInvalidCharacter +Required.Proto3.JsonInput.RejectTopLevelNull diff --git a/conformance/text_format_conformance_suite.cc b/conformance/text_format_conformance_suite.cc new file mode 100644 index 0000000000..97b7b90727 --- /dev/null +++ b/conformance/text_format_conformance_suite.cc @@ -0,0 +1,317 @@ +// 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 "text_format_conformance_suite.h" + +#include "conformance_test.h" + +#include +#include +#include +#include + +using conformance::ConformanceRequest; +using conformance::ConformanceResponse; +using conformance::WireFormat; +using google::protobuf::Message; +using google::protobuf::TextFormat; +using protobuf_test_messages::proto2::TestAllTypesProto2; +using protobuf_test_messages::proto2::UnknownToTestAllTypes; +using protobuf_test_messages::proto3::TestAllTypesProto3; +using std::string; + +namespace google { +namespace protobuf { + +TextFormatConformanceTestSuite::TextFormatConformanceTestSuite() { + SetFailureListFlagName("--text_format_failure_list"); +} + +bool TextFormatConformanceTestSuite::ParseTextFormatResponse( + const ConformanceResponse& response, + const ConformanceRequestSetting& setting, Message* test_message) { + TextFormat::Parser parser; + const ConformanceRequest& request = setting.GetRequest(); + if (request.print_unknown_fields()) { + parser.AllowFieldNumber(true); + } + if (!parser.ParseFromString(response.text_payload(), test_message)) { + GOOGLE_LOG(ERROR) << "INTERNAL ERROR: internal text->protobuf transcode " + << "yielded unparseable proto. Text payload: " + << response.text_payload(); + return false; + } + + return true; +} + +bool TextFormatConformanceTestSuite::ParseResponse( + const ConformanceResponse& response, + const ConformanceRequestSetting& setting, Message* test_message) { + const ConformanceRequest& request = setting.GetRequest(); + WireFormat requested_output = request.requested_output_format(); + const string& test_name = setting.GetTestName(); + ConformanceLevel level = setting.GetLevel(); + + switch (response.result_case()) { + case ConformanceResponse::kProtobufPayload: { + if (requested_output != conformance::PROTOBUF) { + ReportFailure( + test_name, level, request, response, + StrCat("Test was asked for ", WireFormatToString(requested_output), + " output but provided PROTOBUF instead.") + .c_str()); + return false; + } + + if (!test_message->ParseFromString(response.protobuf_payload())) { + ReportFailure(test_name, level, request, response, + "Protobuf output we received from test was unparseable."); + return false; + } + + break; + } + + case ConformanceResponse::kTextPayload: { + if (requested_output != conformance::TEXT_FORMAT) { + ReportFailure( + test_name, level, request, response, + StrCat("Test was asked for ", WireFormatToString(requested_output), + " output but provided TEXT_FORMAT instead.") + .c_str()); + return false; + } + + if (!ParseTextFormatResponse(response, setting, test_message)) { + ReportFailure( + test_name, level, request, response, + "TEXT_FORMAT output we received from test was unparseable."); + return false; + } + + break; + } + + default: + GOOGLE_LOG(FATAL) << test_name + << ": unknown payload type: " << response.result_case(); + } + + return true; +} + +void TextFormatConformanceTestSuite::ExpectParseFailure(const string& test_name, + ConformanceLevel level, + const string& input) { + TestAllTypesProto3 prototype; + // We don't expect output, but if the program erroneously accepts the protobuf + // we let it send its response as this. We must not leave it unspecified. + ConformanceRequestSetting setting( + level, conformance::TEXT_FORMAT, conformance::TEXT_FORMAT, + conformance::TEXT_FORMAT_TEST, prototype, test_name, input); + const ConformanceRequest& request = setting.GetRequest(); + ConformanceResponse response; + string effective_test_name = StrCat(setting.ConformanceLevelToString(level), + ".Proto3.TextFormatInput.", test_name); + + RunTest(effective_test_name, request, &response); + if (response.result_case() == ConformanceResponse::kParseError) { + ReportSuccess(effective_test_name); + } else if (response.result_case() == ConformanceResponse::kSkipped) { + ReportSkip(effective_test_name, request, response); + } else { + ReportFailure(effective_test_name, level, request, response, + "Should have failed to parse, but didn't."); + } +} + +void TextFormatConformanceTestSuite::RunValidTextFormatTest( + const string& test_name, ConformanceLevel level, const string& input_text) { + TestAllTypesProto3 prototype; + RunValidTextFormatTestWithMessage(test_name, level, input_text, prototype); +} + +void TextFormatConformanceTestSuite::RunValidTextFormatTestProto2( + const string& test_name, ConformanceLevel level, const string& input_text) { + TestAllTypesProto2 prototype; + RunValidTextFormatTestWithMessage(test_name, level, input_text, prototype); +} + +void TextFormatConformanceTestSuite::RunValidTextFormatTestWithMessage( + const string& test_name, ConformanceLevel level, const string& input_text, + const Message& prototype) { + ConformanceRequestSetting setting1( + level, conformance::TEXT_FORMAT, conformance::PROTOBUF, + conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text); + RunValidInputTest(setting1, input_text); + ConformanceRequestSetting setting2( + level, conformance::TEXT_FORMAT, conformance::TEXT_FORMAT, + conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text); + RunValidInputTest(setting2, input_text); +} + +void TextFormatConformanceTestSuite::RunValidUnknownTextFormatTest( + const string& test_name, const Message& message) { + string serialized_input; + message.SerializeToString(&serialized_input); + TestAllTypesProto3 prototype; + ConformanceRequestSetting setting1( + RECOMMENDED, conformance::PROTOBUF, conformance::TEXT_FORMAT, + conformance::TEXT_FORMAT_TEST, prototype, test_name + "_Drop", + serialized_input); + setting1.SetPrototypeMessageForCompare(message); + RunValidBinaryInputTest(setting1, ""); + + ConformanceRequestSetting setting2( + RECOMMENDED, conformance::PROTOBUF, conformance::TEXT_FORMAT, + conformance::TEXT_FORMAT_TEST, prototype, test_name + "_Print", + serialized_input); + setting2.SetPrototypeMessageForCompare(message); + setting2.SetPrintUnknownFields(true); + RunValidBinaryInputTest(setting2, serialized_input); +} + +void TextFormatConformanceTestSuite::RunSuiteImpl() { + RunValidTextFormatTest("HelloWorld", REQUIRED, + "optional_string: 'Hello, World!'"); + // Integer fields. + RunValidTextFormatTest("Int32FieldMaxValue", REQUIRED, + "optional_int32: 2147483647"); + RunValidTextFormatTest("Int32FieldMinValue", REQUIRED, + "optional_int32: -2147483648"); + RunValidTextFormatTest("Uint32FieldMaxValue", REQUIRED, + "optional_uint32: 4294967295"); + RunValidTextFormatTest("Int64FieldMaxValue", REQUIRED, + "optional_int64: 9223372036854775807"); + RunValidTextFormatTest("Int64FieldMinValue", REQUIRED, + "optional_int64: -9223372036854775808"); + RunValidTextFormatTest("Uint64FieldMaxValue", REQUIRED, + "optional_uint64: 18446744073709551615"); + + // Parsers reject out-of-bound integer values. + ExpectParseFailure("Int32FieldTooLarge", REQUIRED, + "optional_int32: 2147483648"); + ExpectParseFailure("Int32FieldTooSmall", REQUIRED, + "optional_int32: -2147483649"); + ExpectParseFailure("Uint32FieldTooLarge", REQUIRED, + "optional_uint32: 4294967296"); + ExpectParseFailure("Int64FieldTooLarge", REQUIRED, + "optional_int64: 9223372036854775808"); + ExpectParseFailure("Int64FieldTooSmall", REQUIRED, + "optional_int64: -9223372036854775809"); + ExpectParseFailure("Uint64FieldTooLarge", REQUIRED, + "optional_uint64: 18446744073709551616"); + + // Floating point fields + RunValidTextFormatTest("FloatField", REQUIRED, + "optional_float: 3.192837"); + RunValidTextFormatTest("FloatFieldWithVeryPreciseNumber", REQUIRED, + "optional_float: 3.123456789123456789"); + RunValidTextFormatTest("FloatFieldMaxValue", REQUIRED, + "optional_float: 3.4028235e+38"); + RunValidTextFormatTest("FloatFieldMinValue", REQUIRED, + "optional_float: 1.17549e-38"); + RunValidTextFormatTest("FloatFieldNaNValue", REQUIRED, + "optional_float: NaN"); + RunValidTextFormatTest("FloatFieldPosInfValue", REQUIRED, + "optional_float: inf"); + RunValidTextFormatTest("FloatFieldNegInfValue", REQUIRED, + "optional_float: -inf"); + RunValidTextFormatTest("FloatFieldWithInt32Max", REQUIRED, + "optional_float: 4294967296"); + RunValidTextFormatTest("FloatFieldLargerThanInt64", REQUIRED, + "optional_float: 9223372036854775808"); + RunValidTextFormatTest("FloatFieldTooLarge", REQUIRED, + "optional_float: 3.4028235e+39"); + RunValidTextFormatTest("FloatFieldTooSmall", REQUIRED, + "optional_float: 1.17549e-39"); + RunValidTextFormatTest("FloatFieldLargerThanUint64", REQUIRED, + "optional_float: 18446744073709551616"); + + // Group fields + RunValidTextFormatTestProto2("GroupFieldNoColon", REQUIRED, + "Data { group_int32: 1 }"); + RunValidTextFormatTestProto2("GroupFieldWithColon", REQUIRED, + "Data: { group_int32: 1 }"); + RunValidTextFormatTestProto2("GroupFieldEmpty", REQUIRED, + "Data {}"); + + + // Unknown Fields + UnknownToTestAllTypes message; + // Unable to print unknown Fixed32/Fixed64 fields as if they are known. + // Fixed32/Fixed64 fields are not added in the tests. + message.set_optional_int32(123); + message.set_optional_string("hello"); + message.set_optional_bool(true); + RunValidUnknownTextFormatTest("ScalarUnknownFields", message); + + message.Clear(); + message.mutable_nested_message()->set_c(111); + RunValidUnknownTextFormatTest("MessageUnknownFields", message); + + message.Clear(); + message.mutable_optionalgroup()->set_a(321); + RunValidUnknownTextFormatTest("GroupUnknownFields", message); + + message.add_repeated_int32(1); + message.add_repeated_int32(2); + message.add_repeated_int32(3); + RunValidUnknownTextFormatTest("RepeatedUnknownFields", message); + + // Any fields + RunValidTextFormatTest("AnyField", REQUIRED, + R"( + optional_any: { + [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] { + optional_int32: 12345 + } + } + )"); + RunValidTextFormatTest("AnyFieldWithRawBytes", REQUIRED, + R"( + optional_any: { + type_url: "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3" + value: "\b\271`" + } + )"); + ExpectParseFailure("AnyFieldWithInvalidType", REQUIRED, + R"( + optional_any: { + [type.googleapis.com/unknown] { + optional_int32: 12345 + } + } + )"); +} + +} // namespace protobuf +} // namespace google diff --git a/python/google/protobuf/pyext/thread_unsafe_shared_ptr.h b/conformance/text_format_conformance_suite.h similarity index 52% rename from python/google/protobuf/pyext/thread_unsafe_shared_ptr.h rename to conformance/text_format_conformance_suite.h index 79fa9e3d7f..dd258f50b8 100644 --- a/python/google/protobuf/pyext/thread_unsafe_shared_ptr.h +++ b/conformance/text_format_conformance_suite.h @@ -28,77 +28,42 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// ThreadUnsafeSharedPtr is the same as shared_ptr without the locking -// overhread (and thread-safety). -#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_THREAD_UNSAFE_SHARED_PTR_H__ -#define GOOGLE_PROTOBUF_PYTHON_CPP_THREAD_UNSAFE_SHARED_PTR_H__ +#ifndef TEXT_FORMAT_CONFORMANCE_SUITE_H_ +#define TEXT_FORMAT_CONFORMANCE_SUITE_H_ -#include -#include - -#include -#include +#include "conformance_test.h" namespace google { namespace protobuf { -namespace python { -template -class ThreadUnsafeSharedPtr { +class TextFormatConformanceTestSuite : public ConformanceTestSuite { public: - // Takes ownership. - explicit ThreadUnsafeSharedPtr(T* ptr) - : ptr_(ptr), refcount_(ptr ? new RefcountT(1) : nullptr) { - } - - ThreadUnsafeSharedPtr(const ThreadUnsafeSharedPtr& other) - : ThreadUnsafeSharedPtr(nullptr) { - *this = other; - } - - ThreadUnsafeSharedPtr& operator=(const ThreadUnsafeSharedPtr& other) { - if (other.refcount_ == refcount_) { - return *this; - } - this->~ThreadUnsafeSharedPtr(); - ptr_ = other.ptr_; - refcount_ = other.refcount_; - if (refcount_) { - ++*refcount_; - } - return *this; - } - - ~ThreadUnsafeSharedPtr() { - if (refcount_ == nullptr) { - GOOGLE_DCHECK(ptr_ == nullptr); - return; - } - if (--*refcount_ == 0) { - delete refcount_; - delete ptr_; - } - } - - void reset(T* ptr = nullptr) { *this = ThreadUnsafeSharedPtr(ptr); } - - T* get() { return ptr_; } - const T* get() const { return ptr_; } - - void swap(ThreadUnsafeSharedPtr& other) { - using std::swap; - swap(ptr_, other.ptr_); - swap(refcount_, other.refcount_); - } + TextFormatConformanceTestSuite(); private: - typedef int RefcountT; - T* ptr_; - RefcountT* refcount_; + void RunSuiteImpl(); + void RunValidTextFormatTest(const string& test_name, ConformanceLevel level, + const string& input); + void RunValidTextFormatTestProto2(const string& test_name, + ConformanceLevel level, + const string& input); + void RunValidTextFormatTestWithMessage(const string& test_name, + ConformanceLevel level, + const string& input_text, + const Message& prototype); + void RunValidUnknownTextFormatTest(const string& test_name, + const Message& message); + void ExpectParseFailure(const string& test_name, ConformanceLevel level, + const string& input); + bool ParseTextFormatResponse(const conformance::ConformanceResponse& response, + const ConformanceRequestSetting& setting, + Message* test_message); + bool ParseResponse(const conformance::ConformanceResponse& response, + const ConformanceRequestSetting& setting, + Message* test_message) override; }; -} // namespace python } // namespace protobuf } // namespace google -#endif // GOOGLE_PROTOBUF_PYTHON_CPP_THREAD_UNSAFE_SHARED_PTR_H__ +#endif // TEXT_FORMAT_CONFORMANCE_SUITE_H_ diff --git a/conformance/text_format_failure_list_csharp.txt b/conformance/text_format_failure_list_csharp.txt new file mode 100644 index 0000000000..404b64a584 --- /dev/null +++ b/conformance/text_format_failure_list_csharp.txt @@ -0,0 +1,8 @@ +Recommended.Proto3.ProtobufInput.GroupUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.GroupUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.MessageUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.MessageUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Print.TextFormatOutput diff --git a/conformance/text_format_failure_list_java.txt b/conformance/text_format_failure_list_java.txt new file mode 100755 index 0000000000..81433b441a --- /dev/null +++ b/conformance/text_format_failure_list_java.txt @@ -0,0 +1,6 @@ +Recommended.Proto3.ProtobufInput.GroupUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.MessageUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput +Required.Proto3.TextFormatInput.AnyField.ProtobufOutput +Required.Proto3.TextFormatInput.AnyField.TextFormatOutput diff --git a/conformance/text_format_failure_list_php.txt b/conformance/text_format_failure_list_php.txt new file mode 100644 index 0000000000..404b64a584 --- /dev/null +++ b/conformance/text_format_failure_list_php.txt @@ -0,0 +1,8 @@ +Recommended.Proto3.ProtobufInput.GroupUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.GroupUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.MessageUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.MessageUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Print.TextFormatOutput diff --git a/conformance/text_format_failure_list_python.txt b/conformance/text_format_failure_list_python.txt new file mode 100644 index 0000000000..b2db95e7de --- /dev/null +++ b/conformance/text_format_failure_list_python.txt @@ -0,0 +1,5 @@ +# This is the list of text format conformance tests that are known to fail right +# now. +# TODO: These should be fixed. +Required.Proto3.TextFormatInput.FloatFieldMaxValue.ProtobufOutput +Required.Proto3.TextFormatInput.FloatFieldMaxValue.TextFormatOutput diff --git a/conformance/text_format_failure_list_ruby.txt b/conformance/text_format_failure_list_ruby.txt new file mode 100644 index 0000000000..404b64a584 --- /dev/null +++ b/conformance/text_format_failure_list_ruby.txt @@ -0,0 +1,8 @@ +Recommended.Proto3.ProtobufInput.GroupUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.GroupUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.MessageUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.MessageUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Print.TextFormatOutput +Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput +Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Print.TextFormatOutput diff --git a/conformance/third_party/jsoncpp/json.h b/conformance/third_party/jsoncpp/json.h index 42e7e7f4ad..32fd0720d4 100644 --- a/conformance/third_party/jsoncpp/json.h +++ b/conformance/third_party/jsoncpp/json.h @@ -6,28 +6,28 @@ // ////////////////////////////////////////////////////////////////////// /* -The JsonCpp library's source code, including accompanying documentation, +The JsonCpp library's source code, including accompanying documentation, tests and demonstration applications, are licensed under the following conditions... -The author (Baptiste Lepilleur) explicitly disclaims copyright in all -jurisdictions which recognize such a disclaimer. In such jurisdictions, +The author (Baptiste Lepilleur) explicitly disclaims copyright in all +jurisdictions which recognize such a disclaimer. In such jurisdictions, this software is released into the Public Domain. In jurisdictions which do not recognize Public Domain property (e.g. Germany as of 2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is released under the terms of the MIT License (see below). -In jurisdictions which recognize Public Domain property, the user of this -software may choose to accept it either as 1) Public Domain, 2) under the -conditions of the MIT License (see below), or 3) under the terms of dual +In jurisdictions which recognize Public Domain property, the user of this +software may choose to accept it either as 1) Public Domain, 2) under the +conditions of the MIT License (see below), or 3) under the terms of dual Public Domain/MIT License conditions described here, as they choose. The MIT License is about as close to Public Domain as a license can get, and is described in clear, concise terms at: http://en.wikipedia.org/wiki/MIT_License - + The full text of the MIT License follows: ======================================================================== @@ -434,7 +434,7 @@ protected: /** Exceptions which the user cannot easily avoid. * * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input - * + * * \remark derived from Json::Exception */ class JSON_API RuntimeError : public Exception { @@ -445,7 +445,7 @@ public: /** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros. * * These are precondition-violations (user bugs) and internal errors (our bugs). - * + * * \remark derived from Json::Exception */ class JSON_API LogicError : public Exception { @@ -1570,7 +1570,7 @@ public: - `"rejectDupKeys": false or true` - If true, `parse()` returns false when a key is duplicated within an object. - `"allowSpecialFloats": false or true` - - If true, special float values (NaNs and infinities) are allowed + - If true, special float values (NaNs and infinities) are allowed and their values are lossfree restorable. You can examine 'settings_` yourself diff --git a/conformance/third_party/jsoncpp/jsoncpp.cpp b/conformance/third_party/jsoncpp/jsoncpp.cpp index f803962ade..4d3e0f2f31 100644 --- a/conformance/third_party/jsoncpp/jsoncpp.cpp +++ b/conformance/third_party/jsoncpp/jsoncpp.cpp @@ -6,28 +6,28 @@ // ////////////////////////////////////////////////////////////////////// /* -The JsonCpp library's source code, including accompanying documentation, +The JsonCpp library's source code, including accompanying documentation, tests and demonstration applications, are licensed under the following conditions... -The author (Baptiste Lepilleur) explicitly disclaims copyright in all -jurisdictions which recognize such a disclaimer. In such jurisdictions, +The author (Baptiste Lepilleur) explicitly disclaims copyright in all +jurisdictions which recognize such a disclaimer. In such jurisdictions, this software is released into the Public Domain. In jurisdictions which do not recognize Public Domain property (e.g. Germany as of 2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is released under the terms of the MIT License (see below). -In jurisdictions which recognize Public Domain property, the user of this -software may choose to accept it either as 1) Public Domain, 2) under the -conditions of the MIT License (see below), or 3) under the terms of dual +In jurisdictions which recognize Public Domain property, the user of this +software may choose to accept it either as 1) Public Domain, 2) under the +conditions of the MIT License (see below), or 3) under the terms of dual Public Domain/MIT License conditions described here, as they choose. The MIT License is about as close to Public Domain as a license can get, and is described in clear, concise terms at: http://en.wikipedia.org/wiki/MIT_License - + The full text of the MIT License follows: ======================================================================== @@ -207,7 +207,7 @@ static inline void fixNumericLocale(char* begin, char* end) { #include #if defined(_MSC_VER) -#if !defined(WINCE) && defined(__STDC_SECURE_LIB__) && _MSC_VER >= 1500 // VC++ 9.0 and above +#if !defined(WINCE) && defined(__STDC_SECURE_LIB__) && _MSC_VER >= 1500 // VC++ 9.0 and above #define snprintf sprintf_s #elif _MSC_VER >= 1900 // VC++ 14.0 and above #define snprintf std::snprintf @@ -4029,7 +4029,7 @@ Value& Path::make(Value& root) const { #define snprintf std::snprintf #endif -#if defined(__BORLANDC__) +#if defined(__BORLANDC__) #include #define isfinite _finite #define snprintf _snprintf @@ -5096,7 +5096,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const std::string cs_str = settings_["commentStyle"].asString(); bool eyc = settings_["enableYAMLCompatibility"].asBool(); bool dnp = settings_["dropNullPlaceholders"].asBool(); - bool usf = settings_["useSpecialFloats"].asBool(); + bool usf = settings_["useSpecialFloats"].asBool(); unsigned int pre = settings_["precision"].asUInt(); CommentStyle::Enum cs = CommentStyle::All; if (cs_str == "All") { diff --git a/csharp/.gitignore b/csharp/.gitignore index 8ba8849985..d0d7ae04f7 100644 --- a/csharp/.gitignore +++ b/csharp/.gitignore @@ -29,3 +29,6 @@ mono/*.exe mono/*.dll lib/protoc.exe *.ncrunch* + +# Benchmark output +BenchmarkDotNet.Artifacts/ diff --git a/csharp/CHANGES.txt b/csharp/CHANGES.txt index a87cd4d5df..8574b7c4a3 100644 --- a/csharp/CHANGES.txt +++ b/csharp/CHANGES.txt @@ -26,7 +26,7 @@ Changes: - Optimized enum parsing. Fixes: -- Fix for bug in limited input stream's Position, Introduced Position on +- Fix for bug in limited input stream's Position, Introduced Position on output stream - Fix for writing a character to a JSON output overflows allocated buffer - Optimize FromBase64String to return Empty when presented with empty string. @@ -47,14 +47,14 @@ Changes: - Added 'Unsafe' static type in ByteString to allow direct buffer access Fixes: -- Issue 50: The XML serializer will fail to deserialize a message with empty +- Issue 50: The XML serializer will fail to deserialize a message with empty child message - Issue 45: Use of 'item' as a field name causes AmbiguousMatchException - Issue 49: Generated nested static Types class should be partial - Issue 38: Disable CLSCompliant warnings (3021) - Issue 40: proto_path does not work for command-line file names - Issue 54: should retire all bytes in buffer (bufferSize) -- Issue 43: Fix to correct identical 'umbrella_classname' options from trying +- Issue 43: Fix to correct identical 'umbrella_classname' options from trying to write to the same filename. =============================================================================== @@ -66,7 +66,7 @@ Features: NONE, GENERIC, INTERFACE, or IRPCDISPATCH - Added interfaces IRpcDispatch and IRpcServerStub to provide for blocking services and implementations. -- Added ProtoGen.exe command-line argument "--protoc_dir=" to specify the +- Added ProtoGen.exe command-line argument "--protoc_dir=" to specify the location of protoc.exe. - Extracted interfaces for ICodedInputStream and ICodedOutputStream to allow custom implementation of writers with both speed and size optimizations. @@ -86,9 +86,9 @@ Fixes: - Issue 16: Does not integrate well with other tooling - Issue 19: Support for negative enum values - Issue 26: AddRange in GeneratedBuilder iterates twice. -- Issue 27: Remove XML documentation output from test projects to clear +- Issue 27: Remove XML documentation output from test projects to clear warnings/errors. -- Issue 28: Circular message dependencies result in null default values for +- Issue 28: Circular message dependencies result in null default values for Message fields. - Issue 29: Message classes generated have a public default constructor. You can disable private ctor generation with the option generate_private_ctor. @@ -109,14 +109,14 @@ RELEASE NOTES - Version 2.3.0.277 =============================================================================== Features: -- Added cls_compliance option to generate attributes indicating +- Added cls_compliance option to generate attributes indicating non-CLS-compliance. - Added file_extension option to control the generated output file's extension. - Added umbrella_namespace option to place the umbrella class into a nested - namespace to address issues with proto files having the same name as a + namespace to address issues with proto files having the same name as a message it contains. - Added output_directory option to set the output path for the source file(s). -- Added ignore_google_protobuf option to avoid generating code for includes +- Added ignore_google_protobuf option to avoid generating code for includes from the google.protobuf package. - Added the LITE framework (Google.ProtoBuffersLite.dll) and the ability to generate code with "option optimize_for = LITE_RUNTIME;". diff --git a/csharp/Google.Protobuf.Tools.nuspec b/csharp/Google.Protobuf.Tools.nuspec index 901d5ce925..7094658176 100644 --- a/csharp/Google.Protobuf.Tools.nuspec +++ b/csharp/Google.Protobuf.Tools.nuspec @@ -1,39 +1,39 @@ - + Google.Protobuf.Tools Google Protocol Buffers tools Tools for Protocol Buffers - Google's data interchange format. See project site for more info. - 3.6.1 + 3.9.0-rc1 Google Inc. protobuf-packages - https://github.com/google/protobuf/blob/master/LICENSE - https://github.com/google/protobuf + https://github.com/protocolbuffers/protobuf/blob/master/LICENSE + https://github.com/protocolbuffers/protobuf false Tools for Protocol Buffers Copyright 2015, Google Inc. Protocol Buffers Binary Serialization Format Google proto proto3 - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/csharp/README.md b/csharp/README.md index 9d1225f1a8..9aab782da1 100644 --- a/csharp/README.md +++ b/csharp/README.md @@ -10,7 +10,7 @@ You will also want to install the `Google.Protobuf.Tools` NuGet package, which contains precompiled version of `protoc.exe` and a copy of well known `.proto` files under the package's `tools` directory. -To generate C# files from your `.proto` files, invoke `protoc` with the +To generate C# files from your `.proto` files, invoke `protoc` with the `--csharp_out` option. Supported platforms @@ -37,8 +37,8 @@ later. Although *users* of this project are only expected to have Visual Studio 2012 or later, *developers* of the library are required to have Visual Studio 2017 or later, as the library uses C# 6 features -in its implementation, as well as the new Visual Studio 2017 csproj -format. These features have no impact when using the compiled code - +in its implementation, as well as the new Visual Studio 2017 csproj +format. These features have no impact when using the compiled code - they're only relevant when building the `Google.Protobuf` assembly. In order to run and debug the AddressBook example in the IDE, you must @@ -56,19 +56,19 @@ run using the Visual Studio Test Explorer or `dotnet test`. .NET 3.5 ======== -We don't officially support .NET 3.5. However, there has been some effort -to make enabling .NET 3.5 support relatively painless in case you require it. -There's no guarantee that this will continue in the future, so rely on .NET +We don't officially support .NET 3.5. However, there has been some effort +to make enabling .NET 3.5 support relatively painless in case you require it. +There's no guarantee that this will continue in the future, so rely on .NET 3.5 support at your peril. -To enable .NET 3.5 support, you must edit the `TargetFrameworks` elements of -[src/Google.Protobuf/Google.Protobuf.csproj](src/Google.Protobuf/Google.Protobuf.csproj) -(and [src/Google.Protobuf.Test/Google.Protobuf.Test.csproj](src/Google.Protobuf.Test/Google.Protobuf.Test.csproj) -if you want to run the unit tests): +To enable .NET 3.5 support, you must edit the `TargetFrameworks` elements of +[src/Google.Protobuf/Google.Protobuf.csproj](src/Google.Protobuf/Google.Protobuf.csproj) +(and [src/Google.Protobuf.Test/Google.Protobuf.Test.csproj](src/Google.Protobuf.Test/Google.Protobuf.Test.csproj) +if you want to run the unit tests): -Open the .csproj file in a text editor and simply add `net35` to the list of -target frameworks, noting that the `TargetFrameworks` element appears twice in -the file (once in the first `PropertyGroup` element, and again in the second +Open the .csproj file in a text editor and simply add `net35` to the list of +target frameworks, noting that the `TargetFrameworks` element appears twice in +the file (once in the first `PropertyGroup` element, and again in the second `PropertyGroup` element, i.e., the one with the conditional). History of C# protobufs @@ -77,7 +77,7 @@ History of C# protobufs This subtree was originally imported from https://github.com/jskeet/protobuf-csharp-port and represents the latest development version of C# protobufs, that will now be developed and maintained by Google. All the development will be done in open, under this repository -(https://github.com/google/protobuf). +(https://github.com/protocolbuffers/protobuf). The previous project differs from this project in a number of ways: diff --git a/csharp/build_packages.bat b/csharp/build_packages.bat index 8157bbab75..d7205659f1 100644 --- a/csharp/build_packages.bat +++ b/csharp/build_packages.bat @@ -1,7 +1,7 @@ @rem Builds Google.Protobuf NuGet packages dotnet restore src/Google.Protobuf.sln -dotnet pack -c Release src/Google.Protobuf.sln /p:SourceLinkCreate=true || goto :error +dotnet pack -c Release src/Google.Protobuf.sln || goto :error goto :EOF diff --git a/csharp/buildall.bat b/csharp/buildall.bat new file mode 100644 index 0000000000..821ffb3102 --- /dev/null +++ b/csharp/buildall.bat @@ -0,0 +1,13 @@ +@rem Builds Google.Protobuf and runs the tests + +dotnet build src/Google.Protobuf.sln || goto :error + +echo Running tests. + +dotnet test src/Google.Protobuf.Test/Google.Protobuf.Test.csproj || goto :error + +goto :EOF + +:error +echo Failed! +exit /b %errorlevel% diff --git a/csharp/buildall.sh b/csharp/buildall.sh index 50d8906dce..43b5ac3ffb 100755 --- a/csharp/buildall.sh +++ b/csharp/buildall.sh @@ -10,8 +10,8 @@ dotnet restore $SRC/Google.Protobuf.sln dotnet build -c $CONFIG $SRC/Google.Protobuf.sln echo Running tests. -# Only test netcoreapp1.0, which uses the .NET Core runtime. +# Only test netcoreapp2.1, which uses the .NET Core runtime. # If we want to test the .NET 4.5 version separately, we could # run Mono explicitly. However, we don't have any differences between -# the .NET 4.5 and netstandard1.0 assemblies. -dotnet test -c $CONFIG -f netcoreapp1.0 $SRC/Google.Protobuf.Test/Google.Protobuf.Test.csproj +# the .NET 4.5 and netstandard2.1 assemblies. +dotnet test -c $CONFIG -f netcoreapp2.1 $SRC/Google.Protobuf.Test/Google.Protobuf.Test.csproj diff --git a/csharp/compatibility_tests/v3.0.0/protos/csharp/protos/unittest_issues.proto b/csharp/compatibility_tests/v3.0.0/protos/csharp/protos/unittest_issues.proto index 6c9f76344a..b6178bf1af 100644 --- a/csharp/compatibility_tests/v3.0.0/protos/csharp/protos/unittest_issues.proto +++ b/csharp/compatibility_tests/v3.0.0/protos/csharp/protos/unittest_issues.proto @@ -19,8 +19,8 @@ message Issue307 { } // Old issue 13: http://code.google.com/p/protobuf-csharp-port/issues/detail?id=13 -// New issue 309: https://github.com/google/protobuf/issues/309 - +// New issue 309: https://github.com/protocolbuffers/protobuf/issues/309 + // message A { // optional int32 _A = 1; // } @@ -35,7 +35,7 @@ message Issue307 { // Similar issue with numeric names // Java code failed too, so probably best for this to be a restriction. -// See https://github.com/google/protobuf/issues/308 +// See https://github.com/protocolbuffers/protobuf/issues/308 // message NumberField { // optional int32 _01 = 1; // } @@ -101,21 +101,21 @@ message TestJsonFieldOrdering { // that will require fixing other tests in multiple platforms. // Alternatively, consider just adding this to // unittest_proto3.proto if multiple platforms want it. - + int32 plain_int32 = 4; oneof o1 { string o1_string = 2; int32 o1_int32 = 5; } - + string plain_string = 1; - + oneof o2 { int32 o2_int32 = 6; string o2_string = 3; } - + } message TestJsonName { diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/CodedInputStreamTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/CodedInputStreamTest.cs index ff44895c06..73a578d2b5 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/CodedInputStreamTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/CodedInputStreamTest.cs @@ -313,14 +313,14 @@ namespace Google.Protobuf [Test] public void MaliciousRecursion() { - ByteString data64 = MakeRecursiveMessage(64).ToByteString(); - ByteString data65 = MakeRecursiveMessage(65).ToByteString(); + ByteString atRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit).ToByteString(); + ByteString beyondRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit + 1).ToByteString(); - AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(data64), 64); + AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(atRecursiveLimit), CodedInputStream.DefaultRecursionLimit); - Assert.Throws(() => TestRecursiveMessage.Parser.ParseFrom(data65)); + Assert.Throws(() => TestRecursiveMessage.Parser.ParseFrom(beyondRecursiveLimit)); - CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(data64.ToByteArray()), 1000000, 63); + CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(atRecursiveLimit.ToByteArray()), 1000000, CodedInputStream.DefaultRecursionLimit - 1); Assert.Throws(() => TestRecursiveMessage.Parser.ParseFrom(input)); } diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Collections/MapFieldTest.cs index 9c8459073c..3b4e1d33b1 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Collections/MapFieldTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Collections/MapFieldTest.cs @@ -86,7 +86,7 @@ namespace Google.Protobuf.Collections var map = new MapField(); Assert.Throws(() => map[null] = new ForeignMessage()); } - + [Test] public void AddPreservesInsertionOrder() { @@ -471,7 +471,7 @@ namespace Google.Protobuf.Collections keys.CopyTo(array, 1); CollectionAssert.AreEqual(new[] { null, "foo", "x", null }, array); } - + // Just test keys - we know the implementation is the same for values [Test] public void NonGenericViewCopyTo() diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs index f430b06bed..e8a3d360c2 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs @@ -59,7 +59,7 @@ namespace Google.Protobuf.Compatibility [TestCase(typeof(string), typeof(int), false)] [TestCase(typeof(int), typeof(int), true)] [TestCase(typeof(ValueType), typeof(int), true)] - [TestCase(typeof(long), typeof(int), false)] // + [TestCase(typeof(long), typeof(int), false)] // public void IsAssignableFrom(Type target, Type argument, bool expected) { Assert.AreEqual(expected, TypeExtensions.IsAssignableFrom(target, argument)); diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/FieldCodecTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/FieldCodecTest.cs index 0e2bad59e5..3907666827 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/FieldCodecTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/FieldCodecTest.cs @@ -43,7 +43,7 @@ namespace Google.Protobuf #pragma warning disable 0414 // Used by tests via reflection - do not remove! private static readonly List Codecs = new List { - new FieldCodecTestData(FieldCodec.ForBool(100), true, "Bool"), + new FieldCodecTestData(FieldCodec.ForBool(100), true, "FixedBool"), new FieldCodecTestData(FieldCodec.ForString(100), "sample", "String"), new FieldCodecTestData(FieldCodec.ForBytes(100), ByteString.CopyFrom(1, 2, 3), "Bytes"), new FieldCodecTestData(FieldCodec.ForInt32(100), -1000, "Int32"), @@ -56,8 +56,8 @@ namespace Google.Protobuf new FieldCodecTestData(FieldCodec.ForSFixed64(100), -1000, "SFixed64"), new FieldCodecTestData(FieldCodec.ForUInt64(100), 1234, "UInt64"), new FieldCodecTestData(FieldCodec.ForFixed64(100), 1234, "Fixed64"), - new FieldCodecTestData(FieldCodec.ForFloat(100), 1234.5f, "Float"), - new FieldCodecTestData(FieldCodec.ForDouble(100), 1234567890.5d, "Double"), + new FieldCodecTestData(FieldCodec.ForFloat(100), 1234.5f, "FixedFloat"), + new FieldCodecTestData(FieldCodec.ForDouble(100), 1234567890.5d, "FixedDouble"), new FieldCodecTestData( FieldCodec.ForEnum(100, t => (int) t, t => (ForeignEnum) t), ForeignEnum.ForeignBaz, "Enum"), new FieldCodecTestData( diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj index 06d07b9f4a..1ef5bf8637 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj @@ -2,7 +2,7 @@ Exe - net451;netcoreapp1.0 + net451;netcoreapp2.1 ../../keys/Google.Protobuf.snk true true @@ -18,13 +18,13 @@ - - netcoreapp1.0 + netcoreapp2.1 - + diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/JsonTokenizerTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/JsonTokenizerTest.cs index 527ab3361e..2e690c182d 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/JsonTokenizerTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/JsonTokenizerTest.cs @@ -240,7 +240,7 @@ namespace Google.Protobuf AssertTokens("{'x': 'y'}", JsonToken.StartObject, JsonToken.Name("x"), JsonToken.Value("y"), JsonToken.EndObject); } - + [Test] [TestCase("[10, 20", 3)] [TestCase("[10,", 2)] @@ -305,7 +305,7 @@ namespace Google.Protobuf [Test] public void ObjectMixedType() { - AssertTokens(@"{'a': 1, 'b': 'bar', 'c': null, 'd': false, 'e': true, + AssertTokens(@"{'a': 1, 'b': 'bar', 'c': null, 'd': false, 'e': true, 'f': [2], 'g': {'x':'y' }}", JsonToken.StartObject, JsonToken.Name("a"), @@ -349,12 +349,12 @@ namespace Google.Protobuf Assert.AreEqual(JsonToken.EndDocument, tokenizer.Next()); Assert.Throws(() => tokenizer.Next()); } - + /// /// Asserts that the specified JSON is tokenized into the given sequence of tokens. /// All apostrophes are first converted to double quotes, allowing any tests /// that don't need to check actual apostrophe handling to use apostrophes in the JSON, avoiding - /// messy string literal escaping. The "end document" token is not specified in the list of + /// messy string literal escaping. The "end document" token is not specified in the list of /// expected tokens, but is implicit. /// private static void AssertTokens(string json, params JsonToken[] expectedTokens) @@ -366,7 +366,7 @@ namespace Google.Protobuf /// Asserts that the specified JSON is tokenized into the given sequence of tokens. /// Unlike , this does not perform any character /// replacement on the specified JSON, and should be used when the text contains apostrophes which - /// are expected to be used *as* apostrophes. The "end document" token is not specified in the list of + /// are expected to be used *as* apostrophes. The "end document" token is not specified in the list of /// expected tokens, but is implicit. /// private static void AssertTokensNoReplacement(string json, params JsonToken[] expectedTokens) diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs index 52d5a67697..0520ada30a 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs @@ -150,7 +150,7 @@ namespace Google.Protobuf.Reflection Assert.AreEqual(UnittestProto3Reflection.Descriptor, primitiveField.File); Assert.AreEqual(FieldType.Int32, primitiveField.FieldType); Assert.IsNull(primitiveField.Proto.Options); - + Assert.AreEqual("single_nested_enum", enumField.Name); Assert.AreEqual(FieldType.Enum, enumField.FieldType); // Assert.AreEqual(TestAllTypes.Types.NestedEnum.DescriptorProtoFile, enumField.EnumType); @@ -242,7 +242,7 @@ namespace Google.Protobuf.Reflection // NestedMessage single_nested_message = 200; [Test] public void FieldListOrderings() - { + { var fields = TestFieldOrderings.Descriptor.Fields; Assert.AreEqual(new[] { 11, 1, 101, 200 }, fields.InDeclarationOrder().Select(x => x.FieldNumber)); Assert.AreEqual(new[] { 1, 11, 101, 200 }, fields.InFieldNumberOrder().Select(x => x.FieldNumber)); diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs index a488af30d4..46cb1afcd5 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs @@ -213,6 +213,6 @@ namespace Google.Protobuf.Reflection var descriptor = TestAllTypes.Descriptor; Assert.Throws(() => descriptor.Fields[999999].ToString()); Assert.Throws(() => descriptor.Fields["not found"].ToString()); - } + } } } diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/SampleEnum.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/SampleEnum.cs index 77447afa12..44f232a53e 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/SampleEnum.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/SampleEnum.cs @@ -29,7 +29,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion - + namespace Google.Protobuf { // Just a sample enum with positive and negative values to be used in tests. diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs index 9ecd24c62a..548c585a1c 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs @@ -99,7 +99,7 @@ namespace Google.Protobuf.WellKnownTypes Duration difference = new Duration { Seconds = 1999, Nanos = Duration.NanosecondsPerSecond - 5000 }; Assert.AreEqual(difference, t1 - t2); Assert.AreEqual(-difference, t2 - t1); - + Assert.AreEqual(t1, t2 + difference); Assert.AreEqual(t2, t1 - difference); } diff --git a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs index 5b7185dcd2..0df4ac4c79 100644 --- a/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs +++ b/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs @@ -135,7 +135,7 @@ namespace Google.Protobuf.WellKnownTypes DoubleField = { 12.5, -1.5, 0d }, FloatField = { 123.25f, -20f, 0f }, Int32Field = { int.MaxValue, int.MinValue, 0 }, - Int64Field = { long.MaxValue, long.MinValue, 0L }, + Int64Field = { long.MaxValue, long.MinValue, 0L }, StringField = { "First", "Second", "" }, Uint32Field = { uint.MaxValue, uint.MinValue, 0U }, Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL }, @@ -403,7 +403,7 @@ namespace Google.Protobuf.WellKnownTypes output.Flush(); stream.Position = 0; - + var message = TestWellKnownTypes.Parser.ParseFrom(stream); Assert.AreEqual(6, message.Int32Field); } diff --git a/csharp/compatibility_tests/v3.0.0/test.sh b/csharp/compatibility_tests/v3.0.0/test.sh index 54d28dfc26..9fcf406c25 100755 --- a/csharp/compatibility_tests/v3.0.0/test.sh +++ b/csharp/compatibility_tests/v3.0.0/test.sh @@ -2,17 +2,17 @@ function run_test() { # Generate test proto files. - ./protoc_1 -Iprotos/src -I../../../src/ --csharp_out=src/Google.Protobuf.Test \ + $1 -Iprotos/src -I../../../src/ --csharp_out=src/Google.Protobuf.Test \ --csharp_opt=base_namespace=Google.Protobuf \ protos/src/google/protobuf/unittest_import_proto3.proto \ protos/src/google/protobuf/unittest_import_public_proto3.proto \ protos/src/google/protobuf/unittest_well_known_types.proto - ./protoc_1 -Iprotos/csharp --csharp_out=src/Google.Protobuf.Test \ + $1 -Iprotos/csharp --csharp_out=src/Google.Protobuf.Test \ --csharp_opt=base_namespace=UnitTest.Issues \ protos/csharp/protos/unittest_issues.proto - ./protoc_2 -Iprotos/src --csharp_out=src/Google.Protobuf.Test \ + $2 -Iprotos/src --csharp_out=src/Google.Protobuf.Test \ --csharp_opt=base_namespace=Google.Protobuf \ protos/src/google/protobuf/unittest_proto3.proto \ protos/src/google/protobuf/map_unittest_proto3.proto @@ -22,7 +22,7 @@ function run_test() { dotnet restore src/Google.Protobuf.Test/Google.Protobuf.Test.csproj dotnet build -c Release src/Google.Protobuf/Google.Protobuf.csproj dotnet build -c Release src/Google.Protobuf.Test/Google.Protobuf.Test.csproj - dotnet run -c Release -f netcoreapp1.0 -p src/Google.Protobuf.Test/Google.Protobuf.Test.csproj + dotnet run -c Release -f netcoreapp2.1 -p src/Google.Protobuf.Test/Google.Protobuf.Test.csproj } set -ex @@ -79,26 +79,18 @@ cp ../../keys . -r # Test A.1: # proto set 1: use old version # proto set 2 which may import protos in set 1: use old version -cp old_protoc protoc_1 -cp old_protoc protoc_2 -run_test +run_test "./old_protoc" "./old_protoc" # Test A.2: # proto set 1: use new version # proto set 2 which may import protos in set 1: use old version -cp ../../../src/protoc protoc_1 -cp old_protoc protoc_2 -run_test +run_test "../../../src/protoc" "./old_protoc" # Test A.3: # proto set 1: use old version # proto set 2 which may import protos in set 1: use new version -cp old_protoc protoc_1 -cp ../../../src/protoc protoc_2 -run_test +run_test "./old_protoc" "../../../src/protoc" -rm protoc_1 -rm protoc_2 rm old_protoc rm keys -r rm src/Google.Protobuf -r diff --git a/csharp/generate_protos.sh b/csharp/generate_protos.sh index 5c748e35cf..5d3f5b7759 100755 --- a/csharp/generate_protos.sh +++ b/csharp/generate_protos.sh @@ -43,6 +43,9 @@ $PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf \ # Test protos $PROTOC -Isrc -Icsharp/protos \ --csharp_out=csharp/src/Google.Protobuf.Test/TestProtos \ + --descriptor_set_out=csharp/src/Google.Protobuf.Test/testprotos.pb \ + --include_source_info \ + --include_imports \ csharp/protos/map_unittest_proto3.proto \ csharp/protos/unittest_issues.proto \ csharp/protos/unittest_custom_options_proto3.proto \ @@ -58,3 +61,9 @@ $PROTOC -Iexamples -Isrc --csharp_out=csharp/src/AddressBook \ $PROTOC -Iconformance -Isrc --csharp_out=csharp/src/Google.Protobuf.Conformance \ conformance/conformance.proto + +# Benchmark protos +$PROTOC -Ibenchmarks \ + benchmarks/datasets/google_message1/proto3/*.proto \ + benchmarks/benchmarks.proto \ + --csharp_out=csharp/src/Google.Protobuf.Benchmarks diff --git a/csharp/global.json b/csharp/global.json deleted file mode 100644 index 080dcb9403..0000000000 --- a/csharp/global.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "sdk": { - "version": "2.1.3" - } -} diff --git a/csharp/install_dotnet_sdk.ps1 b/csharp/install_dotnet_sdk.ps1 new file mode 100644 index 0000000000..b4132ab31f --- /dev/null +++ b/csharp/install_dotnet_sdk.ps1 @@ -0,0 +1,21 @@ +#!/usr/bin/env powershell +# Install dotnet SDK based on the SDK version from global.json + +Set-StrictMode -Version 2 +$ErrorActionPreference = 'Stop' + +# avoid "Unknown error on a send" in Invoke-WebRequest +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +$InstallScriptUrl = 'https://dot.net/v1/dotnet-install.ps1' +$InstallScriptPath = Join-Path "$env:TEMP" 'dotnet-install.ps1' +$GlobalJsonPath = Join-Path $PSScriptRoot '..' | Join-Path -ChildPath 'global.json' + +# Resolve SDK version from global.json file +$GlobalJson = Get-Content -Raw $GlobalJsonPath | ConvertFrom-Json +$SDKVersion = $GlobalJson.sdk.version + +# Download install script +Write-Host "Downloading install script: $InstallScriptUrl => $InstallScriptPath" +Invoke-WebRequest -Uri $InstallScriptUrl -OutFile $InstallScriptPath +&$InstallScriptPath -Version $SDKVersion diff --git a/csharp/protos/unittest_issues.proto b/csharp/protos/unittest_issues.proto index 0d8793e1e9..b4a88d8648 100644 --- a/csharp/protos/unittest_issues.proto +++ b/csharp/protos/unittest_issues.proto @@ -18,8 +18,8 @@ message Issue307 { } // Old issue 13: http://code.google.com/p/protobuf-csharp-port/issues/detail?id=13 -// New issue 309: https://github.com/google/protobuf/issues/309 - +// New issue 309: https://github.com/protocolbuffers/protobuf/issues/309 + // message A { // optional int32 _A = 1; // } @@ -34,7 +34,7 @@ message Issue307 { // Similar issue with numeric names // Java code failed too, so probably best for this to be a restriction. -// See https://github.com/google/protobuf/issues/308 +// See https://github.com/protocolbuffers/protobuf/issues/308 // message NumberField { // optional int32 _01 = 1; // } @@ -100,21 +100,21 @@ message TestJsonFieldOrdering { // that will require fixing other tests in multiple platforms. // Alternatively, consider just adding this to // unittest_proto3.proto if multiple platforms want it. - + int32 plain_int32 = 4; oneof o1 { string o1_string = 2; int32 o1_int32 = 5; } - + string plain_string = 1; - + oneof o2 { int32 o2_int32 = 6; string o2_string = 3; } - + } message TestJsonName { diff --git a/csharp/protos/unittest_proto3.proto b/csharp/protos/unittest_proto3.proto index ef4933a509..884beae4e5 100644 --- a/csharp/protos/unittest_proto3.proto +++ b/csharp/protos/unittest_proto3.proto @@ -368,7 +368,9 @@ message FooResponse {} message FooClientMessage {} message FooServerMessage{} +// This is a test service service TestService { + // This is a test method rpc Foo(FooRequest) returns (FooResponse); rpc Bar(BarRequest) returns (BarResponse); } @@ -378,3 +380,31 @@ message BarRequest {} message BarResponse {} message TestEmptyMessage {} + +// This is leading detached comment 1 + +// This is leading detached comment 2 + +// This is a leading comment +message CommentMessage { + // Leading nested message comment + message NestedCommentMessage { + // Leading nested message field comment + string nested_text = 1; + } + + // Leading nested enum comment + enum NestedCommentEnum { + // Zero value comment + ZERO_VALUE = 0; + } + + // Leading field comment + string text = 1; // Trailing field comment +} + +// Leading enum comment +enum CommentEnum { + // Zero value comment + ZERO_VALUE = 0; +} diff --git a/csharp/src/AddressBook/AddressBook.csproj b/csharp/src/AddressBook/AddressBook.csproj index 6edfdcabb3..f3268c0acf 100644 --- a/csharp/src/AddressBook/AddressBook.csproj +++ b/csharp/src/AddressBook/AddressBook.csproj @@ -1,7 +1,7 @@  - netcoreapp1.0 + netcoreapp2.1 Exe Google.Protobuf.Examples.AddressBook.Program False diff --git a/csharp/src/AddressBook/Addressbook.cs b/csharp/src/AddressBook/Addressbook.cs index 21a8ce02d1..cbd9772696 100644 --- a/csharp/src/AddressBook/Addressbook.cs +++ b/csharp/src/AddressBook/Addressbook.cs @@ -37,9 +37,9 @@ namespace Google.Protobuf.Examples.AddressBook { "ZHJlc3NCb29rYgZwcm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.TimestampReflection.Descriptor, }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.Person), global::Google.Protobuf.Examples.AddressBook.Person.Parser, new[]{ "Name", "Id", "Email", "Phones", "LastUpdated" }, null, new[]{ typeof(global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType) }, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneNumber), global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneNumber.Parser, new[]{ "Number", "Type" }, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.AddressBook), global::Google.Protobuf.Examples.AddressBook.AddressBook.Parser, new[]{ "People" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.Person), global::Google.Protobuf.Examples.AddressBook.Person.Parser, new[]{ "Name", "Id", "Email", "Phones", "LastUpdated" }, null, new[]{ typeof(global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType) }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneNumber), global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneNumber.Parser, new[]{ "Number", "Type" }, null, null, null, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.AddressBook), global::Google.Protobuf.Examples.AddressBook.AddressBook.Parser, new[]{ "People" }, null, null, null, null) })); } #endregion @@ -247,7 +247,7 @@ namespace Google.Protobuf.Examples.AddressBook { phones_.Add(other.phones_); if (other.lastUpdated_ != null) { if (lastUpdated_ == null) { - lastUpdated_ = new global::Google.Protobuf.WellKnownTypes.Timestamp(); + LastUpdated = new global::Google.Protobuf.WellKnownTypes.Timestamp(); } LastUpdated.MergeFrom(other.LastUpdated); } @@ -280,9 +280,9 @@ namespace Google.Protobuf.Examples.AddressBook { } case 42: { if (lastUpdated_ == null) { - lastUpdated_ = new global::Google.Protobuf.WellKnownTypes.Timestamp(); + LastUpdated = new global::Google.Protobuf.WellKnownTypes.Timestamp(); } - input.ReadMessage(lastUpdated_); + input.ReadMessage(LastUpdated); break; } } @@ -347,7 +347,7 @@ namespace Google.Protobuf.Examples.AddressBook { /// Field number for the "type" field. public const int TypeFieldNumber = 2; - private global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType type_ = 0; + private global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType type_ = global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType.Mobile; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType Type { get { return type_; } @@ -378,7 +378,7 @@ namespace Google.Protobuf.Examples.AddressBook { public override int GetHashCode() { int hash = 1; if (Number.Length != 0) hash ^= Number.GetHashCode(); - if (Type != 0) hash ^= Type.GetHashCode(); + if (Type != global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType.Mobile) hash ^= Type.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -396,7 +396,7 @@ namespace Google.Protobuf.Examples.AddressBook { output.WriteRawTag(10); output.WriteString(Number); } - if (Type != 0) { + if (Type != global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType.Mobile) { output.WriteRawTag(16); output.WriteEnum((int) Type); } @@ -411,7 +411,7 @@ namespace Google.Protobuf.Examples.AddressBook { if (Number.Length != 0) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Number); } - if (Type != 0) { + if (Type != global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType.Mobile) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Type); } if (_unknownFields != null) { @@ -428,7 +428,7 @@ namespace Google.Protobuf.Examples.AddressBook { if (other.Number.Length != 0) { Number = other.Number; } - if (other.Type != 0) { + if (other.Type != global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType.Mobile) { Type = other.Type; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -447,7 +447,7 @@ namespace Google.Protobuf.Examples.AddressBook { break; } case 16: { - type_ = (global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType) input.ReadEnum(); + Type = (global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType) input.ReadEnum(); break; } } diff --git a/csharp/src/Google.Protobuf.Benchmarks/BenchmarkMessage1Proto3.cs b/csharp/src/Google.Protobuf.Benchmarks/BenchmarkMessage1Proto3.cs new file mode 100644 index 0000000000..9e8c330a2b --- /dev/null +++ b/csharp/src/Google.Protobuf.Benchmarks/BenchmarkMessage1Proto3.cs @@ -0,0 +1,1980 @@ +// +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: datasets/google_message1/proto3/benchmark_message1_proto3.proto +// +#pragma warning disable 1591, 0612, 3021 +#region Designer generated code + +using pb = global::Google.Protobuf; +using pbc = global::Google.Protobuf.Collections; +using pbr = global::Google.Protobuf.Reflection; +using scg = global::System.Collections.Generic; +namespace Benchmarks.Proto3 { + + /// Holder for reflection information generated from datasets/google_message1/proto3/benchmark_message1_proto3.proto + public static partial class BenchmarkMessage1Proto3Reflection { + + #region Descriptor + /// File descriptor for datasets/google_message1/proto3/benchmark_message1_proto3.proto + public static pbr::FileDescriptor Descriptor { + get { return descriptor; } + } + private static pbr::FileDescriptor descriptor; + + static BenchmarkMessage1Proto3Reflection() { + byte[] descriptorData = global::System.Convert.FromBase64String( + string.Concat( + "Cj9kYXRhc2V0cy9nb29nbGVfbWVzc2FnZTEvcHJvdG8zL2JlbmNobWFya19t", + "ZXNzYWdlMV9wcm90bzMucHJvdG8SEWJlbmNobWFya3MucHJvdG8zIoMGCg5H", + "b29nbGVNZXNzYWdlMRIOCgZmaWVsZDEYASABKAkSDgoGZmllbGQ5GAkgASgJ", + "Eg8KB2ZpZWxkMTgYEiABKAkSDwoHZmllbGQ4MBhQIAEoCBIPCgdmaWVsZDgx", + "GFEgASgIEg4KBmZpZWxkMhgCIAEoBRIOCgZmaWVsZDMYAyABKAUSEQoIZmll", + "bGQyODAYmAIgASgFEg4KBmZpZWxkNhgGIAEoBRIPCgdmaWVsZDIyGBYgASgD", + "Eg4KBmZpZWxkNBgEIAEoCRIOCgZmaWVsZDUYBSADKAYSDwoHZmllbGQ1ORg7", + "IAEoCBIOCgZmaWVsZDcYByABKAkSDwoHZmllbGQxNhgQIAEoBRIRCghmaWVs", + "ZDEzMBiCASABKAUSDwoHZmllbGQxMhgMIAEoCBIPCgdmaWVsZDE3GBEgASgI", + "Eg8KB2ZpZWxkMTMYDSABKAgSDwoHZmllbGQxNBgOIAEoCBIQCghmaWVsZDEw", + "NBhoIAEoBRIQCghmaWVsZDEwMBhkIAEoBRIQCghmaWVsZDEwMRhlIAEoBRIQ", + "CghmaWVsZDEwMhhmIAEoCRIQCghmaWVsZDEwMxhnIAEoCRIPCgdmaWVsZDI5", + "GB0gASgFEg8KB2ZpZWxkMzAYHiABKAgSDwoHZmllbGQ2MBg8IAEoBRIRCghm", + "aWVsZDI3MRiPAiABKAUSEQoIZmllbGQyNzIYkAIgASgFEhEKCGZpZWxkMTUw", + "GJYBIAEoBRIPCgdmaWVsZDIzGBcgASgFEg8KB2ZpZWxkMjQYGCABKAgSDwoH", + "ZmllbGQyNRgZIAEoBRI8CgdmaWVsZDE1GA8gASgLMisuYmVuY2htYXJrcy5w", + "cm90bzMuR29vZ2xlTWVzc2FnZTFTdWJNZXNzYWdlEg8KB2ZpZWxkNzgYTiAB", + "KAgSDwoHZmllbGQ2NxhDIAEoBRIPCgdmaWVsZDY4GEQgASgFEhEKCGZpZWxk", + "MTI4GIABIAEoBRIRCghmaWVsZDEyORiBASABKAkSEQoIZmllbGQxMzEYgwEg", + "ASgFIvcCChhHb29nbGVNZXNzYWdlMVN1Yk1lc3NhZ2USDgoGZmllbGQxGAEg", + "ASgFEg4KBmZpZWxkMhgCIAEoBRIOCgZmaWVsZDMYAyABKAUSDwoHZmllbGQx", + "NRgPIAEoCRIPCgdmaWVsZDEyGAwgASgIEg8KB2ZpZWxkMTMYDSABKAMSDwoH", + "ZmllbGQxNBgOIAEoAxIPCgdmaWVsZDE2GBAgASgFEg8KB2ZpZWxkMTkYEyAB", + "KAUSDwoHZmllbGQyMBgUIAEoCBIPCgdmaWVsZDI4GBwgASgIEg8KB2ZpZWxk", + "MjEYFSABKAYSDwoHZmllbGQyMhgWIAEoBRIPCgdmaWVsZDIzGBcgASgIEhEK", + "CGZpZWxkMjA2GM4BIAEoCBIRCghmaWVsZDIwMxjLASABKAcSEQoIZmllbGQy", + "MDQYzAEgASgFEhEKCGZpZWxkMjA1GM0BIAEoCRIRCghmaWVsZDIwNxjPASAB", + "KAQSEQoIZmllbGQzMDAYrAIgASgEQiUKHmNvbS5nb29nbGUucHJvdG9idWYu", + "YmVuY2htYXJrc0gB+AEBYgZwcm90bzM=")); + descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, + new pbr::FileDescriptor[] { }, + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Benchmarks.Proto3.GoogleMessage1), global::Benchmarks.Proto3.GoogleMessage1.Parser, new[]{ "Field1", "Field9", "Field18", "Field80", "Field81", "Field2", "Field3", "Field280", "Field6", "Field22", "Field4", "Field5", "Field59", "Field7", "Field16", "Field130", "Field12", "Field17", "Field13", "Field14", "Field104", "Field100", "Field101", "Field102", "Field103", "Field29", "Field30", "Field60", "Field271", "Field272", "Field150", "Field23", "Field24", "Field25", "Field15", "Field78", "Field67", "Field68", "Field128", "Field129", "Field131" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Benchmarks.Proto3.GoogleMessage1SubMessage), global::Benchmarks.Proto3.GoogleMessage1SubMessage.Parser, new[]{ "Field1", "Field2", "Field3", "Field15", "Field12", "Field13", "Field14", "Field16", "Field19", "Field20", "Field28", "Field21", "Field22", "Field23", "Field206", "Field203", "Field204", "Field205", "Field207", "Field300" }, null, null, null, null) + })); + } + #endregion + + } + #region Messages + public sealed partial class GoogleMessage1 : pb::IMessage { + private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new GoogleMessage1()); + private pb::UnknownFieldSet _unknownFields; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pb::MessageParser Parser { get { return _parser; } } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pbr::MessageDescriptor Descriptor { + get { return global::Benchmarks.Proto3.BenchmarkMessage1Proto3Reflection.Descriptor.MessageTypes[0]; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + pbr::MessageDescriptor pb::IMessage.Descriptor { + get { return Descriptor; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public GoogleMessage1() { + OnConstruction(); + } + + partial void OnConstruction(); + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public GoogleMessage1(GoogleMessage1 other) : this() { + field1_ = other.field1_; + field9_ = other.field9_; + field18_ = other.field18_; + field80_ = other.field80_; + field81_ = other.field81_; + field2_ = other.field2_; + field3_ = other.field3_; + field280_ = other.field280_; + field6_ = other.field6_; + field22_ = other.field22_; + field4_ = other.field4_; + field5_ = other.field5_.Clone(); + field59_ = other.field59_; + field7_ = other.field7_; + field16_ = other.field16_; + field130_ = other.field130_; + field12_ = other.field12_; + field17_ = other.field17_; + field13_ = other.field13_; + field14_ = other.field14_; + field104_ = other.field104_; + field100_ = other.field100_; + field101_ = other.field101_; + field102_ = other.field102_; + field103_ = other.field103_; + field29_ = other.field29_; + field30_ = other.field30_; + field60_ = other.field60_; + field271_ = other.field271_; + field272_ = other.field272_; + field150_ = other.field150_; + field23_ = other.field23_; + field24_ = other.field24_; + field25_ = other.field25_; + field15_ = other.field15_ != null ? other.field15_.Clone() : null; + field78_ = other.field78_; + field67_ = other.field67_; + field68_ = other.field68_; + field128_ = other.field128_; + field129_ = other.field129_; + field131_ = other.field131_; + _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public GoogleMessage1 Clone() { + return new GoogleMessage1(this); + } + + /// Field number for the "field1" field. + public const int Field1FieldNumber = 1; + private string field1_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field1 { + get { return field1_; } + set { + field1_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field9" field. + public const int Field9FieldNumber = 9; + private string field9_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field9 { + get { return field9_; } + set { + field9_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field18" field. + public const int Field18FieldNumber = 18; + private string field18_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field18 { + get { return field18_; } + set { + field18_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field80" field. + public const int Field80FieldNumber = 80; + private bool field80_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field80 { + get { return field80_; } + set { + field80_ = value; + } + } + + /// Field number for the "field81" field. + public const int Field81FieldNumber = 81; + private bool field81_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field81 { + get { return field81_; } + set { + field81_ = value; + } + } + + /// Field number for the "field2" field. + public const int Field2FieldNumber = 2; + private int field2_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field2 { + get { return field2_; } + set { + field2_ = value; + } + } + + /// Field number for the "field3" field. + public const int Field3FieldNumber = 3; + private int field3_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field3 { + get { return field3_; } + set { + field3_ = value; + } + } + + /// Field number for the "field280" field. + public const int Field280FieldNumber = 280; + private int field280_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field280 { + get { return field280_; } + set { + field280_ = value; + } + } + + /// Field number for the "field6" field. + public const int Field6FieldNumber = 6; + private int field6_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field6 { + get { return field6_; } + set { + field6_ = value; + } + } + + /// Field number for the "field22" field. + public const int Field22FieldNumber = 22; + private long field22_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public long Field22 { + get { return field22_; } + set { + field22_ = value; + } + } + + /// Field number for the "field4" field. + public const int Field4FieldNumber = 4; + private string field4_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field4 { + get { return field4_; } + set { + field4_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field5" field. + public const int Field5FieldNumber = 5; + private static readonly pb::FieldCodec _repeated_field5_codec + = pb::FieldCodec.ForFixed64(42); + private readonly pbc::RepeatedField field5_ = new pbc::RepeatedField(); + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public pbc::RepeatedField Field5 { + get { return field5_; } + } + + /// Field number for the "field59" field. + public const int Field59FieldNumber = 59; + private bool field59_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field59 { + get { return field59_; } + set { + field59_ = value; + } + } + + /// Field number for the "field7" field. + public const int Field7FieldNumber = 7; + private string field7_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field7 { + get { return field7_; } + set { + field7_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field16" field. + public const int Field16FieldNumber = 16; + private int field16_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field16 { + get { return field16_; } + set { + field16_ = value; + } + } + + /// Field number for the "field130" field. + public const int Field130FieldNumber = 130; + private int field130_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field130 { + get { return field130_; } + set { + field130_ = value; + } + } + + /// Field number for the "field12" field. + public const int Field12FieldNumber = 12; + private bool field12_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field12 { + get { return field12_; } + set { + field12_ = value; + } + } + + /// Field number for the "field17" field. + public const int Field17FieldNumber = 17; + private bool field17_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field17 { + get { return field17_; } + set { + field17_ = value; + } + } + + /// Field number for the "field13" field. + public const int Field13FieldNumber = 13; + private bool field13_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field13 { + get { return field13_; } + set { + field13_ = value; + } + } + + /// Field number for the "field14" field. + public const int Field14FieldNumber = 14; + private bool field14_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field14 { + get { return field14_; } + set { + field14_ = value; + } + } + + /// Field number for the "field104" field. + public const int Field104FieldNumber = 104; + private int field104_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field104 { + get { return field104_; } + set { + field104_ = value; + } + } + + /// Field number for the "field100" field. + public const int Field100FieldNumber = 100; + private int field100_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field100 { + get { return field100_; } + set { + field100_ = value; + } + } + + /// Field number for the "field101" field. + public const int Field101FieldNumber = 101; + private int field101_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field101 { + get { return field101_; } + set { + field101_ = value; + } + } + + /// Field number for the "field102" field. + public const int Field102FieldNumber = 102; + private string field102_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field102 { + get { return field102_; } + set { + field102_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field103" field. + public const int Field103FieldNumber = 103; + private string field103_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field103 { + get { return field103_; } + set { + field103_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field29" field. + public const int Field29FieldNumber = 29; + private int field29_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field29 { + get { return field29_; } + set { + field29_ = value; + } + } + + /// Field number for the "field30" field. + public const int Field30FieldNumber = 30; + private bool field30_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field30 { + get { return field30_; } + set { + field30_ = value; + } + } + + /// Field number for the "field60" field. + public const int Field60FieldNumber = 60; + private int field60_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field60 { + get { return field60_; } + set { + field60_ = value; + } + } + + /// Field number for the "field271" field. + public const int Field271FieldNumber = 271; + private int field271_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field271 { + get { return field271_; } + set { + field271_ = value; + } + } + + /// Field number for the "field272" field. + public const int Field272FieldNumber = 272; + private int field272_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field272 { + get { return field272_; } + set { + field272_ = value; + } + } + + /// Field number for the "field150" field. + public const int Field150FieldNumber = 150; + private int field150_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field150 { + get { return field150_; } + set { + field150_ = value; + } + } + + /// Field number for the "field23" field. + public const int Field23FieldNumber = 23; + private int field23_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field23 { + get { return field23_; } + set { + field23_ = value; + } + } + + /// Field number for the "field24" field. + public const int Field24FieldNumber = 24; + private bool field24_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field24 { + get { return field24_; } + set { + field24_ = value; + } + } + + /// Field number for the "field25" field. + public const int Field25FieldNumber = 25; + private int field25_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field25 { + get { return field25_; } + set { + field25_ = value; + } + } + + /// Field number for the "field15" field. + public const int Field15FieldNumber = 15; + private global::Benchmarks.Proto3.GoogleMessage1SubMessage field15_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public global::Benchmarks.Proto3.GoogleMessage1SubMessage Field15 { + get { return field15_; } + set { + field15_ = value; + } + } + + /// Field number for the "field78" field. + public const int Field78FieldNumber = 78; + private bool field78_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field78 { + get { return field78_; } + set { + field78_ = value; + } + } + + /// Field number for the "field67" field. + public const int Field67FieldNumber = 67; + private int field67_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field67 { + get { return field67_; } + set { + field67_ = value; + } + } + + /// Field number for the "field68" field. + public const int Field68FieldNumber = 68; + private int field68_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field68 { + get { return field68_; } + set { + field68_ = value; + } + } + + /// Field number for the "field128" field. + public const int Field128FieldNumber = 128; + private int field128_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field128 { + get { return field128_; } + set { + field128_ = value; + } + } + + /// Field number for the "field129" field. + public const int Field129FieldNumber = 129; + private string field129_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field129 { + get { return field129_; } + set { + field129_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field131" field. + public const int Field131FieldNumber = 131; + private int field131_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field131 { + get { return field131_; } + set { + field131_ = value; + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override bool Equals(object other) { + return Equals(other as GoogleMessage1); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Equals(GoogleMessage1 other) { + if (ReferenceEquals(other, null)) { + return false; + } + if (ReferenceEquals(other, this)) { + return true; + } + if (Field1 != other.Field1) return false; + if (Field9 != other.Field9) return false; + if (Field18 != other.Field18) return false; + if (Field80 != other.Field80) return false; + if (Field81 != other.Field81) return false; + if (Field2 != other.Field2) return false; + if (Field3 != other.Field3) return false; + if (Field280 != other.Field280) return false; + if (Field6 != other.Field6) return false; + if (Field22 != other.Field22) return false; + if (Field4 != other.Field4) return false; + if(!field5_.Equals(other.field5_)) return false; + if (Field59 != other.Field59) return false; + if (Field7 != other.Field7) return false; + if (Field16 != other.Field16) return false; + if (Field130 != other.Field130) return false; + if (Field12 != other.Field12) return false; + if (Field17 != other.Field17) return false; + if (Field13 != other.Field13) return false; + if (Field14 != other.Field14) return false; + if (Field104 != other.Field104) return false; + if (Field100 != other.Field100) return false; + if (Field101 != other.Field101) return false; + if (Field102 != other.Field102) return false; + if (Field103 != other.Field103) return false; + if (Field29 != other.Field29) return false; + if (Field30 != other.Field30) return false; + if (Field60 != other.Field60) return false; + if (Field271 != other.Field271) return false; + if (Field272 != other.Field272) return false; + if (Field150 != other.Field150) return false; + if (Field23 != other.Field23) return false; + if (Field24 != other.Field24) return false; + if (Field25 != other.Field25) return false; + if (!object.Equals(Field15, other.Field15)) return false; + if (Field78 != other.Field78) return false; + if (Field67 != other.Field67) return false; + if (Field68 != other.Field68) return false; + if (Field128 != other.Field128) return false; + if (Field129 != other.Field129) return false; + if (Field131 != other.Field131) return false; + return Equals(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override int GetHashCode() { + int hash = 1; + if (Field1.Length != 0) hash ^= Field1.GetHashCode(); + if (Field9.Length != 0) hash ^= Field9.GetHashCode(); + if (Field18.Length != 0) hash ^= Field18.GetHashCode(); + if (Field80 != false) hash ^= Field80.GetHashCode(); + if (Field81 != false) hash ^= Field81.GetHashCode(); + if (Field2 != 0) hash ^= Field2.GetHashCode(); + if (Field3 != 0) hash ^= Field3.GetHashCode(); + if (Field280 != 0) hash ^= Field280.GetHashCode(); + if (Field6 != 0) hash ^= Field6.GetHashCode(); + if (Field22 != 0L) hash ^= Field22.GetHashCode(); + if (Field4.Length != 0) hash ^= Field4.GetHashCode(); + hash ^= field5_.GetHashCode(); + if (Field59 != false) hash ^= Field59.GetHashCode(); + if (Field7.Length != 0) hash ^= Field7.GetHashCode(); + if (Field16 != 0) hash ^= Field16.GetHashCode(); + if (Field130 != 0) hash ^= Field130.GetHashCode(); + if (Field12 != false) hash ^= Field12.GetHashCode(); + if (Field17 != false) hash ^= Field17.GetHashCode(); + if (Field13 != false) hash ^= Field13.GetHashCode(); + if (Field14 != false) hash ^= Field14.GetHashCode(); + if (Field104 != 0) hash ^= Field104.GetHashCode(); + if (Field100 != 0) hash ^= Field100.GetHashCode(); + if (Field101 != 0) hash ^= Field101.GetHashCode(); + if (Field102.Length != 0) hash ^= Field102.GetHashCode(); + if (Field103.Length != 0) hash ^= Field103.GetHashCode(); + if (Field29 != 0) hash ^= Field29.GetHashCode(); + if (Field30 != false) hash ^= Field30.GetHashCode(); + if (Field60 != 0) hash ^= Field60.GetHashCode(); + if (Field271 != 0) hash ^= Field271.GetHashCode(); + if (Field272 != 0) hash ^= Field272.GetHashCode(); + if (Field150 != 0) hash ^= Field150.GetHashCode(); + if (Field23 != 0) hash ^= Field23.GetHashCode(); + if (Field24 != false) hash ^= Field24.GetHashCode(); + if (Field25 != 0) hash ^= Field25.GetHashCode(); + if (field15_ != null) hash ^= Field15.GetHashCode(); + if (Field78 != false) hash ^= Field78.GetHashCode(); + if (Field67 != 0) hash ^= Field67.GetHashCode(); + if (Field68 != 0) hash ^= Field68.GetHashCode(); + if (Field128 != 0) hash ^= Field128.GetHashCode(); + if (Field129.Length != 0) hash ^= Field129.GetHashCode(); + if (Field131 != 0) hash ^= Field131.GetHashCode(); + if (_unknownFields != null) { + hash ^= _unknownFields.GetHashCode(); + } + return hash; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override string ToString() { + return pb::JsonFormatter.ToDiagnosticString(this); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void WriteTo(pb::CodedOutputStream output) { + if (Field1.Length != 0) { + output.WriteRawTag(10); + output.WriteString(Field1); + } + if (Field2 != 0) { + output.WriteRawTag(16); + output.WriteInt32(Field2); + } + if (Field3 != 0) { + output.WriteRawTag(24); + output.WriteInt32(Field3); + } + if (Field4.Length != 0) { + output.WriteRawTag(34); + output.WriteString(Field4); + } + field5_.WriteTo(output, _repeated_field5_codec); + if (Field6 != 0) { + output.WriteRawTag(48); + output.WriteInt32(Field6); + } + if (Field7.Length != 0) { + output.WriteRawTag(58); + output.WriteString(Field7); + } + if (Field9.Length != 0) { + output.WriteRawTag(74); + output.WriteString(Field9); + } + if (Field12 != false) { + output.WriteRawTag(96); + output.WriteBool(Field12); + } + if (Field13 != false) { + output.WriteRawTag(104); + output.WriteBool(Field13); + } + if (Field14 != false) { + output.WriteRawTag(112); + output.WriteBool(Field14); + } + if (field15_ != null) { + output.WriteRawTag(122); + output.WriteMessage(Field15); + } + if (Field16 != 0) { + output.WriteRawTag(128, 1); + output.WriteInt32(Field16); + } + if (Field17 != false) { + output.WriteRawTag(136, 1); + output.WriteBool(Field17); + } + if (Field18.Length != 0) { + output.WriteRawTag(146, 1); + output.WriteString(Field18); + } + if (Field22 != 0L) { + output.WriteRawTag(176, 1); + output.WriteInt64(Field22); + } + if (Field23 != 0) { + output.WriteRawTag(184, 1); + output.WriteInt32(Field23); + } + if (Field24 != false) { + output.WriteRawTag(192, 1); + output.WriteBool(Field24); + } + if (Field25 != 0) { + output.WriteRawTag(200, 1); + output.WriteInt32(Field25); + } + if (Field29 != 0) { + output.WriteRawTag(232, 1); + output.WriteInt32(Field29); + } + if (Field30 != false) { + output.WriteRawTag(240, 1); + output.WriteBool(Field30); + } + if (Field59 != false) { + output.WriteRawTag(216, 3); + output.WriteBool(Field59); + } + if (Field60 != 0) { + output.WriteRawTag(224, 3); + output.WriteInt32(Field60); + } + if (Field67 != 0) { + output.WriteRawTag(152, 4); + output.WriteInt32(Field67); + } + if (Field68 != 0) { + output.WriteRawTag(160, 4); + output.WriteInt32(Field68); + } + if (Field78 != false) { + output.WriteRawTag(240, 4); + output.WriteBool(Field78); + } + if (Field80 != false) { + output.WriteRawTag(128, 5); + output.WriteBool(Field80); + } + if (Field81 != false) { + output.WriteRawTag(136, 5); + output.WriteBool(Field81); + } + if (Field100 != 0) { + output.WriteRawTag(160, 6); + output.WriteInt32(Field100); + } + if (Field101 != 0) { + output.WriteRawTag(168, 6); + output.WriteInt32(Field101); + } + if (Field102.Length != 0) { + output.WriteRawTag(178, 6); + output.WriteString(Field102); + } + if (Field103.Length != 0) { + output.WriteRawTag(186, 6); + output.WriteString(Field103); + } + if (Field104 != 0) { + output.WriteRawTag(192, 6); + output.WriteInt32(Field104); + } + if (Field128 != 0) { + output.WriteRawTag(128, 8); + output.WriteInt32(Field128); + } + if (Field129.Length != 0) { + output.WriteRawTag(138, 8); + output.WriteString(Field129); + } + if (Field130 != 0) { + output.WriteRawTag(144, 8); + output.WriteInt32(Field130); + } + if (Field131 != 0) { + output.WriteRawTag(152, 8); + output.WriteInt32(Field131); + } + if (Field150 != 0) { + output.WriteRawTag(176, 9); + output.WriteInt32(Field150); + } + if (Field271 != 0) { + output.WriteRawTag(248, 16); + output.WriteInt32(Field271); + } + if (Field272 != 0) { + output.WriteRawTag(128, 17); + output.WriteInt32(Field272); + } + if (Field280 != 0) { + output.WriteRawTag(192, 17); + output.WriteInt32(Field280); + } + if (_unknownFields != null) { + _unknownFields.WriteTo(output); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int CalculateSize() { + int size = 0; + if (Field1.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(Field1); + } + if (Field9.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(Field9); + } + if (Field18.Length != 0) { + size += 2 + pb::CodedOutputStream.ComputeStringSize(Field18); + } + if (Field80 != false) { + size += 2 + 1; + } + if (Field81 != false) { + size += 2 + 1; + } + if (Field2 != 0) { + size += 1 + pb::CodedOutputStream.ComputeInt32Size(Field2); + } + if (Field3 != 0) { + size += 1 + pb::CodedOutputStream.ComputeInt32Size(Field3); + } + if (Field280 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field280); + } + if (Field6 != 0) { + size += 1 + pb::CodedOutputStream.ComputeInt32Size(Field6); + } + if (Field22 != 0L) { + size += 2 + pb::CodedOutputStream.ComputeInt64Size(Field22); + } + if (Field4.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(Field4); + } + size += field5_.CalculateSize(_repeated_field5_codec); + if (Field59 != false) { + size += 2 + 1; + } + if (Field7.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(Field7); + } + if (Field16 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field16); + } + if (Field130 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field130); + } + if (Field12 != false) { + size += 1 + 1; + } + if (Field17 != false) { + size += 2 + 1; + } + if (Field13 != false) { + size += 1 + 1; + } + if (Field14 != false) { + size += 1 + 1; + } + if (Field104 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field104); + } + if (Field100 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field100); + } + if (Field101 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field101); + } + if (Field102.Length != 0) { + size += 2 + pb::CodedOutputStream.ComputeStringSize(Field102); + } + if (Field103.Length != 0) { + size += 2 + pb::CodedOutputStream.ComputeStringSize(Field103); + } + if (Field29 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field29); + } + if (Field30 != false) { + size += 2 + 1; + } + if (Field60 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field60); + } + if (Field271 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field271); + } + if (Field272 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field272); + } + if (Field150 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field150); + } + if (Field23 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field23); + } + if (Field24 != false) { + size += 2 + 1; + } + if (Field25 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field25); + } + if (field15_ != null) { + size += 1 + pb::CodedOutputStream.ComputeMessageSize(Field15); + } + if (Field78 != false) { + size += 2 + 1; + } + if (Field67 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field67); + } + if (Field68 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field68); + } + if (Field128 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field128); + } + if (Field129.Length != 0) { + size += 2 + pb::CodedOutputStream.ComputeStringSize(Field129); + } + if (Field131 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field131); + } + if (_unknownFields != null) { + size += _unknownFields.CalculateSize(); + } + return size; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(GoogleMessage1 other) { + if (other == null) { + return; + } + if (other.Field1.Length != 0) { + Field1 = other.Field1; + } + if (other.Field9.Length != 0) { + Field9 = other.Field9; + } + if (other.Field18.Length != 0) { + Field18 = other.Field18; + } + if (other.Field80 != false) { + Field80 = other.Field80; + } + if (other.Field81 != false) { + Field81 = other.Field81; + } + if (other.Field2 != 0) { + Field2 = other.Field2; + } + if (other.Field3 != 0) { + Field3 = other.Field3; + } + if (other.Field280 != 0) { + Field280 = other.Field280; + } + if (other.Field6 != 0) { + Field6 = other.Field6; + } + if (other.Field22 != 0L) { + Field22 = other.Field22; + } + if (other.Field4.Length != 0) { + Field4 = other.Field4; + } + field5_.Add(other.field5_); + if (other.Field59 != false) { + Field59 = other.Field59; + } + if (other.Field7.Length != 0) { + Field7 = other.Field7; + } + if (other.Field16 != 0) { + Field16 = other.Field16; + } + if (other.Field130 != 0) { + Field130 = other.Field130; + } + if (other.Field12 != false) { + Field12 = other.Field12; + } + if (other.Field17 != false) { + Field17 = other.Field17; + } + if (other.Field13 != false) { + Field13 = other.Field13; + } + if (other.Field14 != false) { + Field14 = other.Field14; + } + if (other.Field104 != 0) { + Field104 = other.Field104; + } + if (other.Field100 != 0) { + Field100 = other.Field100; + } + if (other.Field101 != 0) { + Field101 = other.Field101; + } + if (other.Field102.Length != 0) { + Field102 = other.Field102; + } + if (other.Field103.Length != 0) { + Field103 = other.Field103; + } + if (other.Field29 != 0) { + Field29 = other.Field29; + } + if (other.Field30 != false) { + Field30 = other.Field30; + } + if (other.Field60 != 0) { + Field60 = other.Field60; + } + if (other.Field271 != 0) { + Field271 = other.Field271; + } + if (other.Field272 != 0) { + Field272 = other.Field272; + } + if (other.Field150 != 0) { + Field150 = other.Field150; + } + if (other.Field23 != 0) { + Field23 = other.Field23; + } + if (other.Field24 != false) { + Field24 = other.Field24; + } + if (other.Field25 != 0) { + Field25 = other.Field25; + } + if (other.field15_ != null) { + if (field15_ == null) { + Field15 = new global::Benchmarks.Proto3.GoogleMessage1SubMessage(); + } + Field15.MergeFrom(other.Field15); + } + if (other.Field78 != false) { + Field78 = other.Field78; + } + if (other.Field67 != 0) { + Field67 = other.Field67; + } + if (other.Field68 != 0) { + Field68 = other.Field68; + } + if (other.Field128 != 0) { + Field128 = other.Field128; + } + if (other.Field129.Length != 0) { + Field129 = other.Field129; + } + if (other.Field131 != 0) { + Field131 = other.Field131; + } + _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(pb::CodedInputStream input) { + uint tag; + while ((tag = input.ReadTag()) != 0) { + switch(tag) { + default: + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + break; + case 10: { + Field1 = input.ReadString(); + break; + } + case 16: { + Field2 = input.ReadInt32(); + break; + } + case 24: { + Field3 = input.ReadInt32(); + break; + } + case 34: { + Field4 = input.ReadString(); + break; + } + case 42: + case 41: { + field5_.AddEntriesFrom(input, _repeated_field5_codec); + break; + } + case 48: { + Field6 = input.ReadInt32(); + break; + } + case 58: { + Field7 = input.ReadString(); + break; + } + case 74: { + Field9 = input.ReadString(); + break; + } + case 96: { + Field12 = input.ReadBool(); + break; + } + case 104: { + Field13 = input.ReadBool(); + break; + } + case 112: { + Field14 = input.ReadBool(); + break; + } + case 122: { + if (field15_ == null) { + Field15 = new global::Benchmarks.Proto3.GoogleMessage1SubMessage(); + } + input.ReadMessage(Field15); + break; + } + case 128: { + Field16 = input.ReadInt32(); + break; + } + case 136: { + Field17 = input.ReadBool(); + break; + } + case 146: { + Field18 = input.ReadString(); + break; + } + case 176: { + Field22 = input.ReadInt64(); + break; + } + case 184: { + Field23 = input.ReadInt32(); + break; + } + case 192: { + Field24 = input.ReadBool(); + break; + } + case 200: { + Field25 = input.ReadInt32(); + break; + } + case 232: { + Field29 = input.ReadInt32(); + break; + } + case 240: { + Field30 = input.ReadBool(); + break; + } + case 472: { + Field59 = input.ReadBool(); + break; + } + case 480: { + Field60 = input.ReadInt32(); + break; + } + case 536: { + Field67 = input.ReadInt32(); + break; + } + case 544: { + Field68 = input.ReadInt32(); + break; + } + case 624: { + Field78 = input.ReadBool(); + break; + } + case 640: { + Field80 = input.ReadBool(); + break; + } + case 648: { + Field81 = input.ReadBool(); + break; + } + case 800: { + Field100 = input.ReadInt32(); + break; + } + case 808: { + Field101 = input.ReadInt32(); + break; + } + case 818: { + Field102 = input.ReadString(); + break; + } + case 826: { + Field103 = input.ReadString(); + break; + } + case 832: { + Field104 = input.ReadInt32(); + break; + } + case 1024: { + Field128 = input.ReadInt32(); + break; + } + case 1034: { + Field129 = input.ReadString(); + break; + } + case 1040: { + Field130 = input.ReadInt32(); + break; + } + case 1048: { + Field131 = input.ReadInt32(); + break; + } + case 1200: { + Field150 = input.ReadInt32(); + break; + } + case 2168: { + Field271 = input.ReadInt32(); + break; + } + case 2176: { + Field272 = input.ReadInt32(); + break; + } + case 2240: { + Field280 = input.ReadInt32(); + break; + } + } + } + } + + } + + public sealed partial class GoogleMessage1SubMessage : pb::IMessage { + private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new GoogleMessage1SubMessage()); + private pb::UnknownFieldSet _unknownFields; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pb::MessageParser Parser { get { return _parser; } } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pbr::MessageDescriptor Descriptor { + get { return global::Benchmarks.Proto3.BenchmarkMessage1Proto3Reflection.Descriptor.MessageTypes[1]; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + pbr::MessageDescriptor pb::IMessage.Descriptor { + get { return Descriptor; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public GoogleMessage1SubMessage() { + OnConstruction(); + } + + partial void OnConstruction(); + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public GoogleMessage1SubMessage(GoogleMessage1SubMessage other) : this() { + field1_ = other.field1_; + field2_ = other.field2_; + field3_ = other.field3_; + field15_ = other.field15_; + field12_ = other.field12_; + field13_ = other.field13_; + field14_ = other.field14_; + field16_ = other.field16_; + field19_ = other.field19_; + field20_ = other.field20_; + field28_ = other.field28_; + field21_ = other.field21_; + field22_ = other.field22_; + field23_ = other.field23_; + field206_ = other.field206_; + field203_ = other.field203_; + field204_ = other.field204_; + field205_ = other.field205_; + field207_ = other.field207_; + field300_ = other.field300_; + _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public GoogleMessage1SubMessage Clone() { + return new GoogleMessage1SubMessage(this); + } + + /// Field number for the "field1" field. + public const int Field1FieldNumber = 1; + private int field1_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field1 { + get { return field1_; } + set { + field1_ = value; + } + } + + /// Field number for the "field2" field. + public const int Field2FieldNumber = 2; + private int field2_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field2 { + get { return field2_; } + set { + field2_ = value; + } + } + + /// Field number for the "field3" field. + public const int Field3FieldNumber = 3; + private int field3_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field3 { + get { return field3_; } + set { + field3_ = value; + } + } + + /// Field number for the "field15" field. + public const int Field15FieldNumber = 15; + private string field15_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field15 { + get { return field15_; } + set { + field15_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field12" field. + public const int Field12FieldNumber = 12; + private bool field12_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field12 { + get { return field12_; } + set { + field12_ = value; + } + } + + /// Field number for the "field13" field. + public const int Field13FieldNumber = 13; + private long field13_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public long Field13 { + get { return field13_; } + set { + field13_ = value; + } + } + + /// Field number for the "field14" field. + public const int Field14FieldNumber = 14; + private long field14_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public long Field14 { + get { return field14_; } + set { + field14_ = value; + } + } + + /// Field number for the "field16" field. + public const int Field16FieldNumber = 16; + private int field16_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field16 { + get { return field16_; } + set { + field16_ = value; + } + } + + /// Field number for the "field19" field. + public const int Field19FieldNumber = 19; + private int field19_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field19 { + get { return field19_; } + set { + field19_ = value; + } + } + + /// Field number for the "field20" field. + public const int Field20FieldNumber = 20; + private bool field20_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field20 { + get { return field20_; } + set { + field20_ = value; + } + } + + /// Field number for the "field28" field. + public const int Field28FieldNumber = 28; + private bool field28_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field28 { + get { return field28_; } + set { + field28_ = value; + } + } + + /// Field number for the "field21" field. + public const int Field21FieldNumber = 21; + private ulong field21_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public ulong Field21 { + get { return field21_; } + set { + field21_ = value; + } + } + + /// Field number for the "field22" field. + public const int Field22FieldNumber = 22; + private int field22_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field22 { + get { return field22_; } + set { + field22_ = value; + } + } + + /// Field number for the "field23" field. + public const int Field23FieldNumber = 23; + private bool field23_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field23 { + get { return field23_; } + set { + field23_ = value; + } + } + + /// Field number for the "field206" field. + public const int Field206FieldNumber = 206; + private bool field206_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Field206 { + get { return field206_; } + set { + field206_ = value; + } + } + + /// Field number for the "field203" field. + public const int Field203FieldNumber = 203; + private uint field203_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public uint Field203 { + get { return field203_; } + set { + field203_ = value; + } + } + + /// Field number for the "field204" field. + public const int Field204FieldNumber = 204; + private int field204_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Field204 { + get { return field204_; } + set { + field204_ = value; + } + } + + /// Field number for the "field205" field. + public const int Field205FieldNumber = 205; + private string field205_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Field205 { + get { return field205_; } + set { + field205_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "field207" field. + public const int Field207FieldNumber = 207; + private ulong field207_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public ulong Field207 { + get { return field207_; } + set { + field207_ = value; + } + } + + /// Field number for the "field300" field. + public const int Field300FieldNumber = 300; + private ulong field300_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public ulong Field300 { + get { return field300_; } + set { + field300_ = value; + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override bool Equals(object other) { + return Equals(other as GoogleMessage1SubMessage); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Equals(GoogleMessage1SubMessage other) { + if (ReferenceEquals(other, null)) { + return false; + } + if (ReferenceEquals(other, this)) { + return true; + } + if (Field1 != other.Field1) return false; + if (Field2 != other.Field2) return false; + if (Field3 != other.Field3) return false; + if (Field15 != other.Field15) return false; + if (Field12 != other.Field12) return false; + if (Field13 != other.Field13) return false; + if (Field14 != other.Field14) return false; + if (Field16 != other.Field16) return false; + if (Field19 != other.Field19) return false; + if (Field20 != other.Field20) return false; + if (Field28 != other.Field28) return false; + if (Field21 != other.Field21) return false; + if (Field22 != other.Field22) return false; + if (Field23 != other.Field23) return false; + if (Field206 != other.Field206) return false; + if (Field203 != other.Field203) return false; + if (Field204 != other.Field204) return false; + if (Field205 != other.Field205) return false; + if (Field207 != other.Field207) return false; + if (Field300 != other.Field300) return false; + return Equals(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override int GetHashCode() { + int hash = 1; + if (Field1 != 0) hash ^= Field1.GetHashCode(); + if (Field2 != 0) hash ^= Field2.GetHashCode(); + if (Field3 != 0) hash ^= Field3.GetHashCode(); + if (Field15.Length != 0) hash ^= Field15.GetHashCode(); + if (Field12 != false) hash ^= Field12.GetHashCode(); + if (Field13 != 0L) hash ^= Field13.GetHashCode(); + if (Field14 != 0L) hash ^= Field14.GetHashCode(); + if (Field16 != 0) hash ^= Field16.GetHashCode(); + if (Field19 != 0) hash ^= Field19.GetHashCode(); + if (Field20 != false) hash ^= Field20.GetHashCode(); + if (Field28 != false) hash ^= Field28.GetHashCode(); + if (Field21 != 0UL) hash ^= Field21.GetHashCode(); + if (Field22 != 0) hash ^= Field22.GetHashCode(); + if (Field23 != false) hash ^= Field23.GetHashCode(); + if (Field206 != false) hash ^= Field206.GetHashCode(); + if (Field203 != 0) hash ^= Field203.GetHashCode(); + if (Field204 != 0) hash ^= Field204.GetHashCode(); + if (Field205.Length != 0) hash ^= Field205.GetHashCode(); + if (Field207 != 0UL) hash ^= Field207.GetHashCode(); + if (Field300 != 0UL) hash ^= Field300.GetHashCode(); + if (_unknownFields != null) { + hash ^= _unknownFields.GetHashCode(); + } + return hash; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override string ToString() { + return pb::JsonFormatter.ToDiagnosticString(this); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void WriteTo(pb::CodedOutputStream output) { + if (Field1 != 0) { + output.WriteRawTag(8); + output.WriteInt32(Field1); + } + if (Field2 != 0) { + output.WriteRawTag(16); + output.WriteInt32(Field2); + } + if (Field3 != 0) { + output.WriteRawTag(24); + output.WriteInt32(Field3); + } + if (Field12 != false) { + output.WriteRawTag(96); + output.WriteBool(Field12); + } + if (Field13 != 0L) { + output.WriteRawTag(104); + output.WriteInt64(Field13); + } + if (Field14 != 0L) { + output.WriteRawTag(112); + output.WriteInt64(Field14); + } + if (Field15.Length != 0) { + output.WriteRawTag(122); + output.WriteString(Field15); + } + if (Field16 != 0) { + output.WriteRawTag(128, 1); + output.WriteInt32(Field16); + } + if (Field19 != 0) { + output.WriteRawTag(152, 1); + output.WriteInt32(Field19); + } + if (Field20 != false) { + output.WriteRawTag(160, 1); + output.WriteBool(Field20); + } + if (Field21 != 0UL) { + output.WriteRawTag(169, 1); + output.WriteFixed64(Field21); + } + if (Field22 != 0) { + output.WriteRawTag(176, 1); + output.WriteInt32(Field22); + } + if (Field23 != false) { + output.WriteRawTag(184, 1); + output.WriteBool(Field23); + } + if (Field28 != false) { + output.WriteRawTag(224, 1); + output.WriteBool(Field28); + } + if (Field203 != 0) { + output.WriteRawTag(221, 12); + output.WriteFixed32(Field203); + } + if (Field204 != 0) { + output.WriteRawTag(224, 12); + output.WriteInt32(Field204); + } + if (Field205.Length != 0) { + output.WriteRawTag(234, 12); + output.WriteString(Field205); + } + if (Field206 != false) { + output.WriteRawTag(240, 12); + output.WriteBool(Field206); + } + if (Field207 != 0UL) { + output.WriteRawTag(248, 12); + output.WriteUInt64(Field207); + } + if (Field300 != 0UL) { + output.WriteRawTag(224, 18); + output.WriteUInt64(Field300); + } + if (_unknownFields != null) { + _unknownFields.WriteTo(output); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int CalculateSize() { + int size = 0; + if (Field1 != 0) { + size += 1 + pb::CodedOutputStream.ComputeInt32Size(Field1); + } + if (Field2 != 0) { + size += 1 + pb::CodedOutputStream.ComputeInt32Size(Field2); + } + if (Field3 != 0) { + size += 1 + pb::CodedOutputStream.ComputeInt32Size(Field3); + } + if (Field15.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(Field15); + } + if (Field12 != false) { + size += 1 + 1; + } + if (Field13 != 0L) { + size += 1 + pb::CodedOutputStream.ComputeInt64Size(Field13); + } + if (Field14 != 0L) { + size += 1 + pb::CodedOutputStream.ComputeInt64Size(Field14); + } + if (Field16 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field16); + } + if (Field19 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field19); + } + if (Field20 != false) { + size += 2 + 1; + } + if (Field28 != false) { + size += 2 + 1; + } + if (Field21 != 0UL) { + size += 2 + 8; + } + if (Field22 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field22); + } + if (Field23 != false) { + size += 2 + 1; + } + if (Field206 != false) { + size += 2 + 1; + } + if (Field203 != 0) { + size += 2 + 4; + } + if (Field204 != 0) { + size += 2 + pb::CodedOutputStream.ComputeInt32Size(Field204); + } + if (Field205.Length != 0) { + size += 2 + pb::CodedOutputStream.ComputeStringSize(Field205); + } + if (Field207 != 0UL) { + size += 2 + pb::CodedOutputStream.ComputeUInt64Size(Field207); + } + if (Field300 != 0UL) { + size += 2 + pb::CodedOutputStream.ComputeUInt64Size(Field300); + } + if (_unknownFields != null) { + size += _unknownFields.CalculateSize(); + } + return size; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(GoogleMessage1SubMessage other) { + if (other == null) { + return; + } + if (other.Field1 != 0) { + Field1 = other.Field1; + } + if (other.Field2 != 0) { + Field2 = other.Field2; + } + if (other.Field3 != 0) { + Field3 = other.Field3; + } + if (other.Field15.Length != 0) { + Field15 = other.Field15; + } + if (other.Field12 != false) { + Field12 = other.Field12; + } + if (other.Field13 != 0L) { + Field13 = other.Field13; + } + if (other.Field14 != 0L) { + Field14 = other.Field14; + } + if (other.Field16 != 0) { + Field16 = other.Field16; + } + if (other.Field19 != 0) { + Field19 = other.Field19; + } + if (other.Field20 != false) { + Field20 = other.Field20; + } + if (other.Field28 != false) { + Field28 = other.Field28; + } + if (other.Field21 != 0UL) { + Field21 = other.Field21; + } + if (other.Field22 != 0) { + Field22 = other.Field22; + } + if (other.Field23 != false) { + Field23 = other.Field23; + } + if (other.Field206 != false) { + Field206 = other.Field206; + } + if (other.Field203 != 0) { + Field203 = other.Field203; + } + if (other.Field204 != 0) { + Field204 = other.Field204; + } + if (other.Field205.Length != 0) { + Field205 = other.Field205; + } + if (other.Field207 != 0UL) { + Field207 = other.Field207; + } + if (other.Field300 != 0UL) { + Field300 = other.Field300; + } + _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(pb::CodedInputStream input) { + uint tag; + while ((tag = input.ReadTag()) != 0) { + switch(tag) { + default: + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + break; + case 8: { + Field1 = input.ReadInt32(); + break; + } + case 16: { + Field2 = input.ReadInt32(); + break; + } + case 24: { + Field3 = input.ReadInt32(); + break; + } + case 96: { + Field12 = input.ReadBool(); + break; + } + case 104: { + Field13 = input.ReadInt64(); + break; + } + case 112: { + Field14 = input.ReadInt64(); + break; + } + case 122: { + Field15 = input.ReadString(); + break; + } + case 128: { + Field16 = input.ReadInt32(); + break; + } + case 152: { + Field19 = input.ReadInt32(); + break; + } + case 160: { + Field20 = input.ReadBool(); + break; + } + case 169: { + Field21 = input.ReadFixed64(); + break; + } + case 176: { + Field22 = input.ReadInt32(); + break; + } + case 184: { + Field23 = input.ReadBool(); + break; + } + case 224: { + Field28 = input.ReadBool(); + break; + } + case 1629: { + Field203 = input.ReadFixed32(); + break; + } + case 1632: { + Field204 = input.ReadInt32(); + break; + } + case 1642: { + Field205 = input.ReadString(); + break; + } + case 1648: { + Field206 = input.ReadBool(); + break; + } + case 1656: { + Field207 = input.ReadUInt64(); + break; + } + case 2400: { + Field300 = input.ReadUInt64(); + break; + } + } + } + } + + } + + #endregion + +} + +#endregion Designer generated code diff --git a/csharp/src/Google.Protobuf.Benchmarks/Benchmarks.cs b/csharp/src/Google.Protobuf.Benchmarks/Benchmarks.cs new file mode 100644 index 0000000000..01dfcd72dc --- /dev/null +++ b/csharp/src/Google.Protobuf.Benchmarks/Benchmarks.cs @@ -0,0 +1,250 @@ +// +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: benchmarks.proto +// +#pragma warning disable 1591, 0612, 3021 +#region Designer generated code + +using pb = global::Google.Protobuf; +using pbc = global::Google.Protobuf.Collections; +using pbr = global::Google.Protobuf.Reflection; +using scg = global::System.Collections.Generic; +namespace Benchmarks { + + /// Holder for reflection information generated from benchmarks.proto + public static partial class BenchmarksReflection { + + #region Descriptor + /// File descriptor for benchmarks.proto + public static pbr::FileDescriptor Descriptor { + get { return descriptor; } + } + private static pbr::FileDescriptor descriptor; + + static BenchmarksReflection() { + byte[] descriptorData = global::System.Convert.FromBase64String( + string.Concat( + "ChBiZW5jaG1hcmtzLnByb3RvEgpiZW5jaG1hcmtzIkcKEEJlbmNobWFya0Rh", + "dGFzZXQSDAoEbmFtZRgBIAEoCRIUCgxtZXNzYWdlX25hbWUYAiABKAkSDwoH", + "cGF5bG9hZBgDIAMoDEIgCh5jb20uZ29vZ2xlLnByb3RvYnVmLmJlbmNobWFy", + "a3NiBnByb3RvMw==")); + descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, + new pbr::FileDescriptor[] { }, + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Benchmarks.BenchmarkDataset), global::Benchmarks.BenchmarkDataset.Parser, new[]{ "Name", "MessageName", "Payload" }, null, null, null, null) + })); + } + #endregion + + } + #region Messages + public sealed partial class BenchmarkDataset : pb::IMessage { + private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new BenchmarkDataset()); + private pb::UnknownFieldSet _unknownFields; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pb::MessageParser Parser { get { return _parser; } } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pbr::MessageDescriptor Descriptor { + get { return global::Benchmarks.BenchmarksReflection.Descriptor.MessageTypes[0]; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + pbr::MessageDescriptor pb::IMessage.Descriptor { + get { return Descriptor; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public BenchmarkDataset() { + OnConstruction(); + } + + partial void OnConstruction(); + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public BenchmarkDataset(BenchmarkDataset other) : this() { + name_ = other.name_; + messageName_ = other.messageName_; + payload_ = other.payload_.Clone(); + _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public BenchmarkDataset Clone() { + return new BenchmarkDataset(this); + } + + /// Field number for the "name" field. + public const int NameFieldNumber = 1; + private string name_ = ""; + /// + /// Name of the benchmark dataset. This should be unique across all datasets. + /// Should only contain word characters: [a-zA-Z0-9_] + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Name { + get { return name_; } + set { + name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "message_name" field. + public const int MessageNameFieldNumber = 2; + private string messageName_ = ""; + /// + /// Fully-qualified name of the protobuf message for this dataset. + /// It will be one of the messages defined benchmark_messages_proto2.proto + /// or benchmark_messages_proto3.proto. + /// + /// Implementations that do not support reflection can implement this with + /// an explicit "if/else" chain that lists every known message defined + /// in those files. + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string MessageName { + get { return messageName_; } + set { + messageName_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + /// Field number for the "payload" field. + public const int PayloadFieldNumber = 3; + private static readonly pb::FieldCodec _repeated_payload_codec + = pb::FieldCodec.ForBytes(26); + private readonly pbc::RepeatedField payload_ = new pbc::RepeatedField(); + /// + /// The payload(s) for this dataset. They should be parsed or serialized + /// in sequence, in a loop, ie. + /// + /// while (!benchmarkDone) { // Benchmark runner decides when to exit. + /// for (i = 0; i < benchmark.payload.length; i++) { + /// parse(benchmark.payload[i]) + /// } + /// } + /// + /// This is intended to let datasets include a variety of data to provide + /// potentially more realistic results than just parsing the same message + /// over and over. A single message parsed repeatedly could yield unusually + /// good branch prediction performance. + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public pbc::RepeatedField Payload { + get { return payload_; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override bool Equals(object other) { + return Equals(other as BenchmarkDataset); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Equals(BenchmarkDataset other) { + if (ReferenceEquals(other, null)) { + return false; + } + if (ReferenceEquals(other, this)) { + return true; + } + if (Name != other.Name) return false; + if (MessageName != other.MessageName) return false; + if(!payload_.Equals(other.payload_)) return false; + return Equals(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override int GetHashCode() { + int hash = 1; + if (Name.Length != 0) hash ^= Name.GetHashCode(); + if (MessageName.Length != 0) hash ^= MessageName.GetHashCode(); + hash ^= payload_.GetHashCode(); + if (_unknownFields != null) { + hash ^= _unknownFields.GetHashCode(); + } + return hash; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override string ToString() { + return pb::JsonFormatter.ToDiagnosticString(this); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void WriteTo(pb::CodedOutputStream output) { + if (Name.Length != 0) { + output.WriteRawTag(10); + output.WriteString(Name); + } + if (MessageName.Length != 0) { + output.WriteRawTag(18); + output.WriteString(MessageName); + } + payload_.WriteTo(output, _repeated_payload_codec); + if (_unknownFields != null) { + _unknownFields.WriteTo(output); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int CalculateSize() { + int size = 0; + if (Name.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); + } + if (MessageName.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(MessageName); + } + size += payload_.CalculateSize(_repeated_payload_codec); + if (_unknownFields != null) { + size += _unknownFields.CalculateSize(); + } + return size; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(BenchmarkDataset other) { + if (other == null) { + return; + } + if (other.Name.Length != 0) { + Name = other.Name; + } + if (other.MessageName.Length != 0) { + MessageName = other.MessageName; + } + payload_.Add(other.payload_); + _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(pb::CodedInputStream input) { + uint tag; + while ((tag = input.ReadTag()) != 0) { + switch(tag) { + default: + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + break; + case 10: { + Name = input.ReadString(); + break; + } + case 18: { + MessageName = input.ReadString(); + break; + } + case 26: { + payload_.AddEntriesFrom(input, _repeated_payload_codec); + break; + } + } + } + } + + } + + #endregion + +} + +#endregion Designer generated code diff --git a/csharp/src/Google.Protobuf.Benchmarks/Google.Protobuf.Benchmarks.csproj b/csharp/src/Google.Protobuf.Benchmarks/Google.Protobuf.Benchmarks.csproj new file mode 100644 index 0000000000..ecc064ea7d --- /dev/null +++ b/csharp/src/Google.Protobuf.Benchmarks/Google.Protobuf.Benchmarks.csproj @@ -0,0 +1,18 @@ + + + + Exe + netcoreapp2.1 + False + + + + + + + + + + + + diff --git a/csharp/src/Google.Protobuf.Benchmarks/Program.cs b/csharp/src/Google.Protobuf.Benchmarks/Program.cs new file mode 100644 index 0000000000..66f71d1391 --- /dev/null +++ b/csharp/src/Google.Protobuf.Benchmarks/Program.cs @@ -0,0 +1,46 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2019 Google Inc. All rights reserved. +// https://github.com/protocolbuffers/protobuf +// +// 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. +#endregion + +using BenchmarkDotNet.Running; + +namespace Google.Protobuf.Benchmarks +{ + /// + /// Entry point, that currently runs the sole benchmark we have. + /// Eventually we might want to be able to specify a particular dataset + /// from the command line. + /// + class Program + { + static void Main() => BenchmarkRunner.Run(); + } +} diff --git a/csharp/src/Google.Protobuf.Benchmarks/SerializationBenchmark.cs b/csharp/src/Google.Protobuf.Benchmarks/SerializationBenchmark.cs new file mode 100644 index 0000000000..d8c2ec11d7 --- /dev/null +++ b/csharp/src/Google.Protobuf.Benchmarks/SerializationBenchmark.cs @@ -0,0 +1,120 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2019 Google Inc. All rights reserved. +// https://github.com/protocolbuffers/protobuf +// +// 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. +#endregion + +using BenchmarkDotNet.Attributes; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace Google.Protobuf.Benchmarks +{ + /// + /// Benchmark for serializing (to a MemoryStream) and deserializing (from a ByteString). + /// Over time we may wish to test the various different approaches to serialization and deserialization separately. + /// + [MemoryDiagnoser] + public class SerializationBenchmark + { + /// + /// All the configurations to be tested. Add more datasets to the array as they're available. + /// (When C# supports proto2, this will increase significantly.) + /// + public static SerializationConfig[] Configurations => new[] + { + new SerializationConfig("dataset.google_message1_proto3.pb") + }; + + [ParamsSource(nameof(Configurations))] + public SerializationConfig Configuration { get; set; } + + private MessageParser parser; + /// + /// Each data set can contain multiple messages in a single file. + /// Each "write" operation should write each message in turn, and each "parse" + /// operation should parse each message in turn. + /// + private List subTests; + + [GlobalSetup] + public void GlobalSetup() + { + parser = Configuration.Parser; + subTests = Configuration.Payloads.Select(p => new SubTest(p, parser.ParseFrom(p))).ToList(); + } + + [Benchmark] + public void WriteToStream() => subTests.ForEach(item => item.WriteToStream()); + + [Benchmark] + public void ToByteArray() => subTests.ForEach(item => item.ToByteArray()); + + [Benchmark] + public void ParseFromByteString() => subTests.ForEach(item => item.ParseFromByteString(parser)); + + [Benchmark] + public void ParseFromStream() => subTests.ForEach(item => item.ParseFromStream(parser)); + + private class SubTest + { + private readonly Stream destinationStream; + private readonly Stream sourceStream; + private readonly ByteString data; + private readonly IMessage message; + + public SubTest(ByteString data, IMessage message) + { + destinationStream = new MemoryStream(data.Length); + sourceStream = new MemoryStream(data.ToByteArray()); + this.data = data; + this.message = message; + } + + public void Reset() => destinationStream.Position = 0; + + public void WriteToStream() + { + destinationStream.Position = 0; + message.WriteTo(destinationStream); + } + + public void ToByteArray() => message.ToByteArray(); + + public void ParseFromByteString(MessageParser parser) => parser.ParseFrom(data); + + public void ParseFromStream(MessageParser parser) + { + sourceStream.Position = 0; + parser.ParseFrom(sourceStream); + } + } + } +} diff --git a/csharp/src/Google.Protobuf.Benchmarks/SerializationConfig.cs b/csharp/src/Google.Protobuf.Benchmarks/SerializationConfig.cs new file mode 100644 index 0000000000..679f16cb9a --- /dev/null +++ b/csharp/src/Google.Protobuf.Benchmarks/SerializationConfig.cs @@ -0,0 +1,89 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2019 Google Inc. All rights reserved. +// https://github.com/protocolbuffers/protobuf +// +// 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. +#endregion + +using Benchmarks; +using Google.Protobuf.Reflection; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; + +namespace Google.Protobuf.Benchmarks +{ + /// + /// The configuration for a single serialization test, loaded from a dataset. + /// + public class SerializationConfig + { + private static readonly Dictionary parsersByMessageName = + typeof(SerializationBenchmark).Assembly.GetTypes() + .Where(t => typeof(IMessage).IsAssignableFrom(t)) + .ToDictionary( + t => ((MessageDescriptor) t.GetProperty("Descriptor", BindingFlags.Static | BindingFlags.Public).GetValue(null)).FullName, + t => ((MessageParser) t.GetProperty("Parser", BindingFlags.Static | BindingFlags.Public).GetValue(null))); + + public MessageParser Parser { get; } + public IEnumerable Payloads { get; } + public string Name { get; } + + public SerializationConfig(string resource) + { + var data = LoadData(resource); + var dataset = BenchmarkDataset.Parser.ParseFrom(data); + + if (!parsersByMessageName.TryGetValue(dataset.MessageName, out var parser)) + { + throw new ArgumentException($"No parser for message {dataset.MessageName} in this assembly"); + } + Parser = parser; + Payloads = dataset.Payload; + Name = dataset.Name; + } + + private static byte[] LoadData(string resource) + { + using (var stream = typeof(SerializationBenchmark).Assembly.GetManifestResourceStream($"Google.Protobuf.Benchmarks.{resource}")) + { + if (stream == null) + { + throw new ArgumentException($"Unable to load embedded resource {resource}"); + } + var copy = new MemoryStream(); + stream.CopyTo(copy); + return copy.ToArray(); + } + } + + public override string ToString() => Name; + } +} diff --git a/csharp/src/Google.Protobuf.Conformance/Conformance.cs b/csharp/src/Google.Protobuf.Conformance/Conformance.cs index 46793ce142..65ba9b1265 100644 --- a/csharp/src/Google.Protobuf.Conformance/Conformance.cs +++ b/csharp/src/Google.Protobuf.Conformance/Conformance.cs @@ -24,26 +24,35 @@ namespace Conformance { static ConformanceReflection() { byte[] descriptorData = global::System.Convert.FromBase64String( string.Concat( - "ChFjb25mb3JtYW5jZS5wcm90bxILY29uZm9ybWFuY2Ui1QEKEkNvbmZvcm1h", - "bmNlUmVxdWVzdBIaChBwcm90b2J1Zl9wYXlsb2FkGAEgASgMSAASFgoManNv", - "bl9wYXlsb2FkGAIgASgJSAASOAoXcmVxdWVzdGVkX291dHB1dF9mb3JtYXQY", - "AyABKA4yFy5jb25mb3JtYW5jZS5XaXJlRm9ybWF0EhQKDG1lc3NhZ2VfdHlw", - "ZRgEIAEoCRIwCg10ZXN0X2NhdGVnb3J5GAUgASgOMhkuY29uZm9ybWFuY2Uu", - "VGVzdENhdGVnb3J5QgkKB3BheWxvYWQisQEKE0NvbmZvcm1hbmNlUmVzcG9u", - "c2USFQoLcGFyc2VfZXJyb3IYASABKAlIABIZCg9zZXJpYWxpemVfZXJyb3IY", - "BiABKAlIABIXCg1ydW50aW1lX2Vycm9yGAIgASgJSAASGgoQcHJvdG9idWZf", - "cGF5bG9hZBgDIAEoDEgAEhYKDGpzb25fcGF5bG9hZBgEIAEoCUgAEhEKB3Nr", - "aXBwZWQYBSABKAlIAEIICgZyZXN1bHQqNQoKV2lyZUZvcm1hdBIPCgtVTlNQ", - "RUNJRklFRBAAEgwKCFBST1RPQlVGEAESCAoESlNPThACKmoKDFRlc3RDYXRl", - "Z29yeRIUChBVTlNQRUNJRklFRF9URVNUEAASDwoLQklOQVJZX1RFU1QQARIN", - "CglKU09OX1RFU1QQAhIkCiBKU09OX0lHTk9SRV9VTktOT1dOX1BBUlNJTkdf", - "VEVTVBADQiEKH2NvbS5nb29nbGUucHJvdG9idWYuY29uZm9ybWFuY2ViBnBy", - "b3RvMw==")); + "ChFjb25mb3JtYW5jZS5wcm90bxILY29uZm9ybWFuY2UiHQoKRmFpbHVyZVNl", + "dBIPCgdmYWlsdXJlGAEgAygJIuMCChJDb25mb3JtYW5jZVJlcXVlc3QSGgoQ", + "cHJvdG9idWZfcGF5bG9hZBgBIAEoDEgAEhYKDGpzb25fcGF5bG9hZBgCIAEo", + "CUgAEhYKDGpzcGJfcGF5bG9hZBgHIAEoCUgAEhYKDHRleHRfcGF5bG9hZBgI", + "IAEoCUgAEjgKF3JlcXVlc3RlZF9vdXRwdXRfZm9ybWF0GAMgASgOMhcuY29u", + "Zm9ybWFuY2UuV2lyZUZvcm1hdBIUCgxtZXNzYWdlX3R5cGUYBCABKAkSMAoN", + "dGVzdF9jYXRlZ29yeRgFIAEoDjIZLmNvbmZvcm1hbmNlLlRlc3RDYXRlZ29y", + "eRI+ChVqc3BiX2VuY29kaW5nX29wdGlvbnMYBiABKAsyHy5jb25mb3JtYW5j", + "ZS5Kc3BiRW5jb2RpbmdDb25maWcSHAoUcHJpbnRfdW5rbm93bl9maWVsZHMY", + "CSABKAhCCQoHcGF5bG9hZCLhAQoTQ29uZm9ybWFuY2VSZXNwb25zZRIVCgtw", + "YXJzZV9lcnJvchgBIAEoCUgAEhkKD3NlcmlhbGl6ZV9lcnJvchgGIAEoCUgA", + "EhcKDXJ1bnRpbWVfZXJyb3IYAiABKAlIABIaChBwcm90b2J1Zl9wYXlsb2Fk", + "GAMgASgMSAASFgoManNvbl9wYXlsb2FkGAQgASgJSAASEQoHc2tpcHBlZBgF", + "IAEoCUgAEhYKDGpzcGJfcGF5bG9hZBgHIAEoCUgAEhYKDHRleHRfcGF5bG9h", + "ZBgIIAEoCUgAQggKBnJlc3VsdCI3ChJKc3BiRW5jb2RpbmdDb25maWcSIQoZ", + "dXNlX2pzcGJfYXJyYXlfYW55X2Zvcm1hdBgBIAEoCCpQCgpXaXJlRm9ybWF0", + "Eg8KC1VOU1BFQ0lGSUVEEAASDAoIUFJPVE9CVUYQARIICgRKU09OEAISCAoE", + "SlNQQhADEg8KC1RFWFRfRk9STUFUEAQqjwEKDFRlc3RDYXRlZ29yeRIUChBV", + "TlNQRUNJRklFRF9URVNUEAASDwoLQklOQVJZX1RFU1QQARINCglKU09OX1RF", + "U1QQAhIkCiBKU09OX0lHTk9SRV9VTktOT1dOX1BBUlNJTkdfVEVTVBADEg0K", + "CUpTUEJfVEVTVBAEEhQKEFRFWFRfRk9STUFUX1RFU1QQBUIhCh9jb20uZ29v", + "Z2xlLnByb3RvYnVmLmNvbmZvcm1hbmNlYgZwcm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Conformance.WireFormat), typeof(global::Conformance.TestCategory), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Conformance.ConformanceRequest), global::Conformance.ConformanceRequest.Parser, new[]{ "ProtobufPayload", "JsonPayload", "RequestedOutputFormat", "MessageType", "TestCategory" }, new[]{ "Payload" }, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Conformance.ConformanceResponse), global::Conformance.ConformanceResponse.Parser, new[]{ "ParseError", "SerializeError", "RuntimeError", "ProtobufPayload", "JsonPayload", "Skipped" }, new[]{ "Result" }, null, null) + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Conformance.WireFormat), typeof(global::Conformance.TestCategory), }, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Conformance.FailureSet), global::Conformance.FailureSet.Parser, new[]{ "Failure" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Conformance.ConformanceRequest), global::Conformance.ConformanceRequest.Parser, new[]{ "ProtobufPayload", "JsonPayload", "JspbPayload", "TextPayload", "RequestedOutputFormat", "MessageType", "TestCategory", "JspbEncodingOptions", "PrintUnknownFields" }, new[]{ "Payload" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Conformance.ConformanceResponse), global::Conformance.ConformanceResponse.Parser, new[]{ "ParseError", "SerializeError", "RuntimeError", "ProtobufPayload", "JsonPayload", "Skipped", "JspbPayload", "TextPayload" }, new[]{ "Result" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Conformance.JspbEncodingConfig), global::Conformance.JspbEncodingConfig.Parser, new[]{ "UseJspbArrayAnyFormat" }, null, null, null, null) })); } #endregion @@ -54,6 +63,11 @@ namespace Conformance { [pbr::OriginalName("UNSPECIFIED")] Unspecified = 0, [pbr::OriginalName("PROTOBUF")] Protobuf = 1, [pbr::OriginalName("JSON")] Json = 2, + /// + /// Google internal only. Opensource testees just skip it. + /// + [pbr::OriginalName("JSPB")] Jspb = 3, + [pbr::OriginalName("TEXT_FORMAT")] TextFormat = 4, } public enum TestCategory { @@ -74,11 +88,146 @@ namespace Conformance { /// for more detail. /// [pbr::OriginalName("JSON_IGNORE_UNKNOWN_PARSING_TEST")] JsonIgnoreUnknownParsingTest = 3, + /// + /// Test jspb wire format. Google internal only. Opensource testees just skip it. + /// + [pbr::OriginalName("JSPB_TEST")] JspbTest = 4, + /// + /// Test text format. For cpp, java and python, testees can already deal with + /// this type. Testees of other languages can simply skip it. + /// + [pbr::OriginalName("TEXT_FORMAT_TEST")] TextFormatTest = 5, } #endregion #region Messages + /// + /// The conformance runner will request a list of failures as the first request. + /// This will be known by message_type == "conformance.FailureSet", a conformance + /// test should return a serialized FailureSet in protobuf_payload. + /// + public sealed partial class FailureSet : pb::IMessage { + private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FailureSet()); + private pb::UnknownFieldSet _unknownFields; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pb::MessageParser Parser { get { return _parser; } } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pbr::MessageDescriptor Descriptor { + get { return global::Conformance.ConformanceReflection.Descriptor.MessageTypes[0]; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + pbr::MessageDescriptor pb::IMessage.Descriptor { + get { return Descriptor; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public FailureSet() { + OnConstruction(); + } + + partial void OnConstruction(); + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public FailureSet(FailureSet other) : this() { + failure_ = other.failure_.Clone(); + _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public FailureSet Clone() { + return new FailureSet(this); + } + + /// Field number for the "failure" field. + public const int FailureFieldNumber = 1; + private static readonly pb::FieldCodec _repeated_failure_codec + = pb::FieldCodec.ForString(10); + private readonly pbc::RepeatedField failure_ = new pbc::RepeatedField(); + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public pbc::RepeatedField Failure { + get { return failure_; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override bool Equals(object other) { + return Equals(other as FailureSet); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Equals(FailureSet other) { + if (ReferenceEquals(other, null)) { + return false; + } + if (ReferenceEquals(other, this)) { + return true; + } + if(!failure_.Equals(other.failure_)) return false; + return Equals(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override int GetHashCode() { + int hash = 1; + hash ^= failure_.GetHashCode(); + if (_unknownFields != null) { + hash ^= _unknownFields.GetHashCode(); + } + return hash; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override string ToString() { + return pb::JsonFormatter.ToDiagnosticString(this); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void WriteTo(pb::CodedOutputStream output) { + failure_.WriteTo(output, _repeated_failure_codec); + if (_unknownFields != null) { + _unknownFields.WriteTo(output); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int CalculateSize() { + int size = 0; + size += failure_.CalculateSize(_repeated_failure_codec); + if (_unknownFields != null) { + size += _unknownFields.CalculateSize(); + } + return size; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(FailureSet other) { + if (other == null) { + return; + } + failure_.Add(other.failure_); + _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(pb::CodedInputStream input) { + uint tag; + while ((tag = input.ReadTag()) != 0) { + switch(tag) { + default: + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + break; + case 10: { + failure_.AddEntriesFrom(input, _repeated_failure_codec); + break; + } + } + } + } + + } + /// /// Represents a single test case's input. The testee should: /// @@ -94,7 +243,7 @@ namespace Conformance { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pbr::MessageDescriptor Descriptor { - get { return global::Conformance.ConformanceReflection.Descriptor.MessageTypes[0]; } + get { return global::Conformance.ConformanceReflection.Descriptor.MessageTypes[1]; } } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -114,6 +263,8 @@ namespace Conformance { requestedOutputFormat_ = other.requestedOutputFormat_; messageType_ = other.messageType_; testCategory_ = other.testCategory_; + jspbEncodingOptions_ = other.jspbEncodingOptions_ != null ? other.jspbEncodingOptions_.Clone() : null; + printUnknownFields_ = other.printUnknownFields_; switch (other.PayloadCase) { case PayloadOneofCase.ProtobufPayload: ProtobufPayload = other.ProtobufPayload; @@ -121,6 +272,12 @@ namespace Conformance { case PayloadOneofCase.JsonPayload: JsonPayload = other.JsonPayload; break; + case PayloadOneofCase.JspbPayload: + JspbPayload = other.JspbPayload; + break; + case PayloadOneofCase.TextPayload: + TextPayload = other.TextPayload; + break; } _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); @@ -153,9 +310,34 @@ namespace Conformance { } } + /// Field number for the "jspb_payload" field. + public const int JspbPayloadFieldNumber = 7; + /// + /// Google internal only. Opensource testees just skip it. + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string JspbPayload { + get { return payloadCase_ == PayloadOneofCase.JspbPayload ? (string) payload_ : ""; } + set { + payload_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + payloadCase_ = PayloadOneofCase.JspbPayload; + } + } + + /// Field number for the "text_payload" field. + public const int TextPayloadFieldNumber = 8; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string TextPayload { + get { return payloadCase_ == PayloadOneofCase.TextPayload ? (string) payload_ : ""; } + set { + payload_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + payloadCase_ = PayloadOneofCase.TextPayload; + } + } + /// Field number for the "requested_output_format" field. public const int RequestedOutputFormatFieldNumber = 3; - private global::Conformance.WireFormat requestedOutputFormat_ = 0; + private global::Conformance.WireFormat requestedOutputFormat_ = global::Conformance.WireFormat.Unspecified; /// /// Which format should the testee serialize its message to? /// @@ -185,7 +367,7 @@ namespace Conformance { /// Field number for the "test_category" field. public const int TestCategoryFieldNumber = 5; - private global::Conformance.TestCategory testCategory_ = 0; + private global::Conformance.TestCategory testCategory_ = global::Conformance.TestCategory.UnspecifiedTest; /// /// Each test is given a specific test category. Some category may need /// spedific support in testee programs. Refer to the defintion of TestCategory @@ -199,12 +381,43 @@ namespace Conformance { } } + /// Field number for the "jspb_encoding_options" field. + public const int JspbEncodingOptionsFieldNumber = 6; + private global::Conformance.JspbEncodingConfig jspbEncodingOptions_; + /// + /// Specify details for how to encode jspb. + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public global::Conformance.JspbEncodingConfig JspbEncodingOptions { + get { return jspbEncodingOptions_; } + set { + jspbEncodingOptions_ = value; + } + } + + /// Field number for the "print_unknown_fields" field. + public const int PrintUnknownFieldsFieldNumber = 9; + private bool printUnknownFields_; + /// + /// This can be used in json and text format. If true, testee should print + /// unknown fields instead of ignore. This feature is optional. + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool PrintUnknownFields { + get { return printUnknownFields_; } + set { + printUnknownFields_ = value; + } + } + private object payload_; /// Enum of possible cases for the "payload" oneof. public enum PayloadOneofCase { None = 0, ProtobufPayload = 1, JsonPayload = 2, + JspbPayload = 7, + TextPayload = 8, } private PayloadOneofCase payloadCase_ = PayloadOneofCase.None; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -233,9 +446,13 @@ namespace Conformance { } if (ProtobufPayload != other.ProtobufPayload) return false; if (JsonPayload != other.JsonPayload) return false; + if (JspbPayload != other.JspbPayload) return false; + if (TextPayload != other.TextPayload) return false; if (RequestedOutputFormat != other.RequestedOutputFormat) return false; if (MessageType != other.MessageType) return false; if (TestCategory != other.TestCategory) return false; + if (!object.Equals(JspbEncodingOptions, other.JspbEncodingOptions)) return false; + if (PrintUnknownFields != other.PrintUnknownFields) return false; if (PayloadCase != other.PayloadCase) return false; return Equals(_unknownFields, other._unknownFields); } @@ -245,9 +462,13 @@ namespace Conformance { int hash = 1; if (payloadCase_ == PayloadOneofCase.ProtobufPayload) hash ^= ProtobufPayload.GetHashCode(); if (payloadCase_ == PayloadOneofCase.JsonPayload) hash ^= JsonPayload.GetHashCode(); - if (RequestedOutputFormat != 0) hash ^= RequestedOutputFormat.GetHashCode(); + if (payloadCase_ == PayloadOneofCase.JspbPayload) hash ^= JspbPayload.GetHashCode(); + if (payloadCase_ == PayloadOneofCase.TextPayload) hash ^= TextPayload.GetHashCode(); + if (RequestedOutputFormat != global::Conformance.WireFormat.Unspecified) hash ^= RequestedOutputFormat.GetHashCode(); if (MessageType.Length != 0) hash ^= MessageType.GetHashCode(); - if (TestCategory != 0) hash ^= TestCategory.GetHashCode(); + if (TestCategory != global::Conformance.TestCategory.UnspecifiedTest) hash ^= TestCategory.GetHashCode(); + if (jspbEncodingOptions_ != null) hash ^= JspbEncodingOptions.GetHashCode(); + if (PrintUnknownFields != false) hash ^= PrintUnknownFields.GetHashCode(); hash ^= (int) payloadCase_; if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); @@ -270,7 +491,7 @@ namespace Conformance { output.WriteRawTag(18); output.WriteString(JsonPayload); } - if (RequestedOutputFormat != 0) { + if (RequestedOutputFormat != global::Conformance.WireFormat.Unspecified) { output.WriteRawTag(24); output.WriteEnum((int) RequestedOutputFormat); } @@ -278,10 +499,26 @@ namespace Conformance { output.WriteRawTag(34); output.WriteString(MessageType); } - if (TestCategory != 0) { + if (TestCategory != global::Conformance.TestCategory.UnspecifiedTest) { output.WriteRawTag(40); output.WriteEnum((int) TestCategory); } + if (jspbEncodingOptions_ != null) { + output.WriteRawTag(50); + output.WriteMessage(JspbEncodingOptions); + } + if (payloadCase_ == PayloadOneofCase.JspbPayload) { + output.WriteRawTag(58); + output.WriteString(JspbPayload); + } + if (payloadCase_ == PayloadOneofCase.TextPayload) { + output.WriteRawTag(66); + output.WriteString(TextPayload); + } + if (PrintUnknownFields != false) { + output.WriteRawTag(72); + output.WriteBool(PrintUnknownFields); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -296,15 +533,27 @@ namespace Conformance { if (payloadCase_ == PayloadOneofCase.JsonPayload) { size += 1 + pb::CodedOutputStream.ComputeStringSize(JsonPayload); } - if (RequestedOutputFormat != 0) { + if (payloadCase_ == PayloadOneofCase.JspbPayload) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(JspbPayload); + } + if (payloadCase_ == PayloadOneofCase.TextPayload) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(TextPayload); + } + if (RequestedOutputFormat != global::Conformance.WireFormat.Unspecified) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) RequestedOutputFormat); } if (MessageType.Length != 0) { size += 1 + pb::CodedOutputStream.ComputeStringSize(MessageType); } - if (TestCategory != 0) { + if (TestCategory != global::Conformance.TestCategory.UnspecifiedTest) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) TestCategory); } + if (jspbEncodingOptions_ != null) { + size += 1 + pb::CodedOutputStream.ComputeMessageSize(JspbEncodingOptions); + } + if (PrintUnknownFields != false) { + size += 1 + 1; + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -316,15 +565,24 @@ namespace Conformance { if (other == null) { return; } - if (other.RequestedOutputFormat != 0) { + if (other.RequestedOutputFormat != global::Conformance.WireFormat.Unspecified) { RequestedOutputFormat = other.RequestedOutputFormat; } if (other.MessageType.Length != 0) { MessageType = other.MessageType; } - if (other.TestCategory != 0) { + if (other.TestCategory != global::Conformance.TestCategory.UnspecifiedTest) { TestCategory = other.TestCategory; } + if (other.jspbEncodingOptions_ != null) { + if (jspbEncodingOptions_ == null) { + JspbEncodingOptions = new global::Conformance.JspbEncodingConfig(); + } + JspbEncodingOptions.MergeFrom(other.JspbEncodingOptions); + } + if (other.PrintUnknownFields != false) { + PrintUnknownFields = other.PrintUnknownFields; + } switch (other.PayloadCase) { case PayloadOneofCase.ProtobufPayload: ProtobufPayload = other.ProtobufPayload; @@ -332,6 +590,12 @@ namespace Conformance { case PayloadOneofCase.JsonPayload: JsonPayload = other.JsonPayload; break; + case PayloadOneofCase.JspbPayload: + JspbPayload = other.JspbPayload; + break; + case PayloadOneofCase.TextPayload: + TextPayload = other.TextPayload; + break; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -354,7 +618,7 @@ namespace Conformance { break; } case 24: { - requestedOutputFormat_ = (global::Conformance.WireFormat) input.ReadEnum(); + RequestedOutputFormat = (global::Conformance.WireFormat) input.ReadEnum(); break; } case 34: { @@ -362,7 +626,26 @@ namespace Conformance { break; } case 40: { - testCategory_ = (global::Conformance.TestCategory) input.ReadEnum(); + TestCategory = (global::Conformance.TestCategory) input.ReadEnum(); + break; + } + case 50: { + if (jspbEncodingOptions_ == null) { + JspbEncodingOptions = new global::Conformance.JspbEncodingConfig(); + } + input.ReadMessage(JspbEncodingOptions); + break; + } + case 58: { + JspbPayload = input.ReadString(); + break; + } + case 66: { + TextPayload = input.ReadString(); + break; + } + case 72: { + PrintUnknownFields = input.ReadBool(); break; } } @@ -382,7 +665,7 @@ namespace Conformance { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pbr::MessageDescriptor Descriptor { - get { return global::Conformance.ConformanceReflection.Descriptor.MessageTypes[1]; } + get { return global::Conformance.ConformanceReflection.Descriptor.MessageTypes[2]; } } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -418,6 +701,12 @@ namespace Conformance { case ResultOneofCase.Skipped: Skipped = other.Skipped; break; + case ResultOneofCase.JspbPayload: + JspbPayload = other.JspbPayload; + break; + case ResultOneofCase.TextPayload: + TextPayload = other.TextPayload; + break; } _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); @@ -523,6 +812,37 @@ namespace Conformance { } } + /// Field number for the "jspb_payload" field. + public const int JspbPayloadFieldNumber = 7; + /// + /// If the input was successfully parsed and the requested output was JSPB, + /// serialize to JSPB and set it in this field. JSPB is google internal only + /// format. Opensource testees can just skip it. + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string JspbPayload { + get { return resultCase_ == ResultOneofCase.JspbPayload ? (string) result_ : ""; } + set { + result_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + resultCase_ = ResultOneofCase.JspbPayload; + } + } + + /// Field number for the "text_payload" field. + public const int TextPayloadFieldNumber = 8; + /// + /// If the input was successfully parsed and the requested output was + /// TEXT_FORMAT, serialize to TEXT_FORMAT and set it in this field. + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string TextPayload { + get { return resultCase_ == ResultOneofCase.TextPayload ? (string) result_ : ""; } + set { + result_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + resultCase_ = ResultOneofCase.TextPayload; + } + } + private object result_; /// Enum of possible cases for the "result" oneof. public enum ResultOneofCase { @@ -533,6 +853,8 @@ namespace Conformance { ProtobufPayload = 3, JsonPayload = 4, Skipped = 5, + JspbPayload = 7, + TextPayload = 8, } private ResultOneofCase resultCase_ = ResultOneofCase.None; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -565,6 +887,8 @@ namespace Conformance { if (ProtobufPayload != other.ProtobufPayload) return false; if (JsonPayload != other.JsonPayload) return false; if (Skipped != other.Skipped) return false; + if (JspbPayload != other.JspbPayload) return false; + if (TextPayload != other.TextPayload) return false; if (ResultCase != other.ResultCase) return false; return Equals(_unknownFields, other._unknownFields); } @@ -578,6 +902,8 @@ namespace Conformance { if (resultCase_ == ResultOneofCase.ProtobufPayload) hash ^= ProtobufPayload.GetHashCode(); if (resultCase_ == ResultOneofCase.JsonPayload) hash ^= JsonPayload.GetHashCode(); if (resultCase_ == ResultOneofCase.Skipped) hash ^= Skipped.GetHashCode(); + if (resultCase_ == ResultOneofCase.JspbPayload) hash ^= JspbPayload.GetHashCode(); + if (resultCase_ == ResultOneofCase.TextPayload) hash ^= TextPayload.GetHashCode(); hash ^= (int) resultCase_; if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); @@ -616,6 +942,14 @@ namespace Conformance { output.WriteRawTag(50); output.WriteString(SerializeError); } + if (resultCase_ == ResultOneofCase.JspbPayload) { + output.WriteRawTag(58); + output.WriteString(JspbPayload); + } + if (resultCase_ == ResultOneofCase.TextPayload) { + output.WriteRawTag(66); + output.WriteString(TextPayload); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -642,6 +976,12 @@ namespace Conformance { if (resultCase_ == ResultOneofCase.Skipped) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Skipped); } + if (resultCase_ == ResultOneofCase.JspbPayload) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(JspbPayload); + } + if (resultCase_ == ResultOneofCase.TextPayload) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(TextPayload); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -672,6 +1012,12 @@ namespace Conformance { case ResultOneofCase.Skipped: Skipped = other.Skipped; break; + case ResultOneofCase.JspbPayload: + JspbPayload = other.JspbPayload; + break; + case ResultOneofCase.TextPayload: + TextPayload = other.TextPayload; + break; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -709,6 +1055,149 @@ namespace Conformance { SerializeError = input.ReadString(); break; } + case 58: { + JspbPayload = input.ReadString(); + break; + } + case 66: { + TextPayload = input.ReadString(); + break; + } + } + } + } + + } + + /// + /// Encoding options for jspb format. + /// + public sealed partial class JspbEncodingConfig : pb::IMessage { + private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new JspbEncodingConfig()); + private pb::UnknownFieldSet _unknownFields; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pb::MessageParser Parser { get { return _parser; } } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pbr::MessageDescriptor Descriptor { + get { return global::Conformance.ConformanceReflection.Descriptor.MessageTypes[3]; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + pbr::MessageDescriptor pb::IMessage.Descriptor { + get { return Descriptor; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public JspbEncodingConfig() { + OnConstruction(); + } + + partial void OnConstruction(); + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public JspbEncodingConfig(JspbEncodingConfig other) : this() { + useJspbArrayAnyFormat_ = other.useJspbArrayAnyFormat_; + _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public JspbEncodingConfig Clone() { + return new JspbEncodingConfig(this); + } + + /// Field number for the "use_jspb_array_any_format" field. + public const int UseJspbArrayAnyFormatFieldNumber = 1; + private bool useJspbArrayAnyFormat_; + /// + /// Encode the value field of Any as jspb array if ture, otherwise binary. + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool UseJspbArrayAnyFormat { + get { return useJspbArrayAnyFormat_; } + set { + useJspbArrayAnyFormat_ = value; + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override bool Equals(object other) { + return Equals(other as JspbEncodingConfig); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Equals(JspbEncodingConfig other) { + if (ReferenceEquals(other, null)) { + return false; + } + if (ReferenceEquals(other, this)) { + return true; + } + if (UseJspbArrayAnyFormat != other.UseJspbArrayAnyFormat) return false; + return Equals(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override int GetHashCode() { + int hash = 1; + if (UseJspbArrayAnyFormat != false) hash ^= UseJspbArrayAnyFormat.GetHashCode(); + if (_unknownFields != null) { + hash ^= _unknownFields.GetHashCode(); + } + return hash; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override string ToString() { + return pb::JsonFormatter.ToDiagnosticString(this); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void WriteTo(pb::CodedOutputStream output) { + if (UseJspbArrayAnyFormat != false) { + output.WriteRawTag(8); + output.WriteBool(UseJspbArrayAnyFormat); + } + if (_unknownFields != null) { + _unknownFields.WriteTo(output); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int CalculateSize() { + int size = 0; + if (UseJspbArrayAnyFormat != false) { + size += 1 + 1; + } + if (_unknownFields != null) { + size += _unknownFields.CalculateSize(); + } + return size; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(JspbEncodingConfig other) { + if (other == null) { + return; + } + if (other.UseJspbArrayAnyFormat != false) { + UseJspbArrayAnyFormat = other.UseJspbArrayAnyFormat; + } + _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(pb::CodedInputStream input) { + uint tag; + while ((tag = input.ReadTag()) != 0) { + switch(tag) { + default: + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + break; + case 8: { + UseJspbArrayAnyFormat = input.ReadBool(); + break; + } } } } diff --git a/csharp/src/Google.Protobuf.Conformance/Google.Protobuf.Conformance.csproj b/csharp/src/Google.Protobuf.Conformance/Google.Protobuf.Conformance.csproj index b654c0b2cf..ec8fb91389 100644 --- a/csharp/src/Google.Protobuf.Conformance/Google.Protobuf.Conformance.csproj +++ b/csharp/src/Google.Protobuf.Conformance/Google.Protobuf.Conformance.csproj @@ -1,7 +1,7 @@  - netcoreapp1.0 + netcoreapp2.1 Exe False diff --git a/csharp/src/Google.Protobuf.Conformance/Program.cs b/csharp/src/Google.Protobuf.Conformance/Program.cs index 1eac00be2f..f5e1dad17e 100644 --- a/csharp/src/Google.Protobuf.Conformance/Program.cs +++ b/csharp/src/Google.Protobuf.Conformance/Program.cs @@ -93,22 +93,26 @@ namespace Google.Protobuf.Conformance var parser = new JsonParser(new JsonParser.Settings(20, typeRegistry)); message = parser.Parse(request.JsonPayload); break; - case ConformanceRequest.PayloadOneofCase.ProtobufPayload: + case ConformanceRequest.PayloadOneofCase.ProtobufPayload: { if (request.MessageType.Equals("protobuf_test_messages.proto3.TestAllTypesProto3")) { message = ProtobufTestMessages.Proto3.TestAllTypesProto3.Parser.ParseFrom(request.ProtobufPayload); - } - else if (request.MessageType.Equals("protobuf_test_messages.proto2.TestAllTypesProto2")) + } + else if (request.MessageType.Equals("protobuf_test_messages.proto2.TestAllTypesProto2")) { return new ConformanceResponse { Skipped = "CSharp doesn't support proto2" }; } - else + else { throw new Exception(" Protobuf request doesn't have specific payload type"); } break; } + case ConformanceRequest.PayloadOneofCase.TextPayload: + { + return new ConformanceResponse { Skipped = "CSharp doesn't support text format" }; + } default: throw new Exception("Unsupported request payload: " + request.PayloadCase); } diff --git a/csharp/src/Google.Protobuf.JsonDump/Google.Protobuf.JsonDump.csproj b/csharp/src/Google.Protobuf.JsonDump/Google.Protobuf.JsonDump.csproj index 4eda641ad6..fee35be991 100644 --- a/csharp/src/Google.Protobuf.JsonDump/Google.Protobuf.JsonDump.csproj +++ b/csharp/src/Google.Protobuf.JsonDump/Google.Protobuf.JsonDump.csproj @@ -1,7 +1,7 @@  - netcoreapp1.0 + netcoreapp2.1 Exe False diff --git a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs index afdd491f36..84e6341e95 100644 --- a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs +++ b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs @@ -227,7 +227,7 @@ namespace Google.Protobuf { // We used to have an awful hash algorithm where only the last four // bytes were relevant. This is a regression test for - // https://github.com/google/protobuf/issues/2511 + // https://github.com/protocolbuffers/protobuf/issues/2511 ByteString b1 = ByteString.CopyFrom(100, 1, 2, 3, 4); ByteString b2 = ByteString.CopyFrom(200, 1, 2, 3, 4); diff --git a/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs b/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs index 8795fa65f4..ba65b328e8 100644 --- a/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs +++ b/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs @@ -327,14 +327,14 @@ namespace Google.Protobuf [Test] public void MaliciousRecursion() { - ByteString data64 = MakeRecursiveMessage(64).ToByteString(); - ByteString data65 = MakeRecursiveMessage(65).ToByteString(); + ByteString atRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit).ToByteString(); + ByteString beyondRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit + 1).ToByteString(); - AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(data64), 64); + AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(atRecursiveLimit), CodedInputStream.DefaultRecursionLimit); - Assert.Throws(() => TestRecursiveMessage.Parser.ParseFrom(data65)); + Assert.Throws(() => TestRecursiveMessage.Parser.ParseFrom(beyondRecursiveLimit)); - CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(data64.ToByteArray()), 1000000, 63); + CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(atRecursiveLimit.ToByteArray()), 1000000, CodedInputStream.DefaultRecursionLimit - 1); Assert.Throws(() => TestRecursiveMessage.Parser.ParseFrom(input)); } @@ -373,6 +373,42 @@ namespace Google.Protobuf Assert.AreEqual('\ufffd', text[0]); } + [Test] + public void ReadNegativeSizedStringThrowsInvalidProtocolBufferException() + { + MemoryStream ms = new MemoryStream(); + CodedOutputStream output = new CodedOutputStream(ms); + + uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); + output.WriteRawVarint32(tag); + output.WriteLength(-1); + output.Flush(); + ms.Position = 0; + + CodedInputStream input = new CodedInputStream(ms); + + Assert.AreEqual(tag, input.ReadTag()); + Assert.Throws(() => input.ReadString()); + } + + [Test] + public void ReadNegativeSizedBytesThrowsInvalidProtocolBufferException() + { + MemoryStream ms = new MemoryStream(); + CodedOutputStream output = new CodedOutputStream(ms); + + uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); + output.WriteRawVarint32(tag); + output.WriteLength(-1); + output.Flush(); + ms.Position = 0; + + CodedInputStream input = new CodedInputStream(ms); + + Assert.AreEqual(tag, input.ReadTag()); + Assert.Throws(() => input.ReadBytes()); + } + /// /// A stream which limits the number of bytes it reads at a time. /// We use this to make sure that CodedInputStream doesn't screw up when diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs index 8791dffc35..f64ebac4ef 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs @@ -86,7 +86,7 @@ namespace Google.Protobuf.Collections var map = new MapField(); Assert.Throws(() => map[null] = new ForeignMessage()); } - + [Test] public void AddPreservesInsertionOrder() { @@ -471,7 +471,7 @@ namespace Google.Protobuf.Collections keys.CopyTo(array, 1); CollectionAssert.AreEqual(new[] { null, "foo", "x", null }, array); } - + // Just test keys - we know the implementation is the same for values [Test] public void NonGenericViewCopyTo() diff --git a/csharp/src/Google.Protobuf.Test/Collections/ProtobufEqualityComparersTest.cs b/csharp/src/Google.Protobuf.Test/Collections/ProtobufEqualityComparersTest.cs index c76d7ca125..594a879865 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/ProtobufEqualityComparersTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/ProtobufEqualityComparersTest.cs @@ -120,5 +120,5 @@ namespace Google.Protobuf.Collections } } } - } + } } diff --git a/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs index 129923b609..b3863a4965 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs @@ -750,7 +750,8 @@ namespace Google.Protobuf.Collections var list2 = new RepeatedField { SampleNaNs.Regular, SampleNaNs.PayloadFlipped }; var list3 = new RepeatedField { SampleNaNs.Regular, SampleNaNs.SignallingFlipped }; - EqualityTester.AssertInequality(list1, list2); + // All SampleNaNs have the same hashcode under certain targets (e.g. netcoreapp2.1) + EqualityTester.AssertInequality(list1, list2, checkHashcode: false); EqualityTester.AssertEquality(list1, list3); Assert.True(list1.Contains(SampleNaNs.SignallingFlipped)); Assert.False(list2.Contains(SampleNaNs.SignallingFlipped)); diff --git a/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs b/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs index abbe3c955f..5d86c20896 100644 --- a/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Compatibility/TypeExtensionsTest.cs @@ -59,7 +59,7 @@ namespace Google.Protobuf.Compatibility [TestCase(typeof(string), typeof(int), false)] [TestCase(typeof(int), typeof(int), true)] [TestCase(typeof(ValueType), typeof(int), true)] - [TestCase(typeof(long), typeof(int), false)] // + [TestCase(typeof(long), typeof(int), false)] // public void IsAssignableFrom(Type target, Type argument, bool expected) { Assert.AreEqual(expected, TypeExtensions.IsAssignableFrom(target, argument)); diff --git a/csharp/src/Google.Protobuf.Test/EqualityTester.cs b/csharp/src/Google.Protobuf.Test/EqualityTester.cs index a669baba17..d4b3c1341a 100644 --- a/csharp/src/Google.Protobuf.Test/EqualityTester.cs +++ b/csharp/src/Google.Protobuf.Test/EqualityTester.cs @@ -49,13 +49,14 @@ namespace Google.Protobuf Assert.AreEqual(first.GetHashCode(), second.GetHashCode()); } - public static void AssertInequality(T first, T second) where T : IEquatable + public static void AssertInequality(T first, T second, bool checkHashcode = true) where T : IEquatable { Assert.IsFalse(first.Equals(second)); Assert.IsFalse(first.Equals((object) second)); // While this isn't a requirement, the chances of this test failing due to // coincidence rather than a bug are very small. - if (first != null && second != null) + // For such rare cases, an argument can be used to disable the check. + if (checkHashcode && first != null && second != null) { Assert.AreNotEqual(first.GetHashCode(), second.GetHashCode()); } diff --git a/csharp/src/Google.Protobuf.Test/FieldCodecTest.cs b/csharp/src/Google.Protobuf.Test/FieldCodecTest.cs index 77641163c0..b20ecccde2 100644 --- a/csharp/src/Google.Protobuf.Test/FieldCodecTest.cs +++ b/csharp/src/Google.Protobuf.Test/FieldCodecTest.cs @@ -43,7 +43,7 @@ namespace Google.Protobuf #pragma warning disable 0414 // Used by tests via reflection - do not remove! private static readonly List Codecs = new List { - new FieldCodecTestData(FieldCodec.ForBool(100), true, "Bool"), + new FieldCodecTestData(FieldCodec.ForBool(100), true, "FixedBool"), new FieldCodecTestData(FieldCodec.ForString(100), "sample", "String"), new FieldCodecTestData(FieldCodec.ForBytes(100), ByteString.CopyFrom(1, 2, 3), "Bytes"), new FieldCodecTestData(FieldCodec.ForInt32(100), -1000, "Int32"), @@ -56,8 +56,8 @@ namespace Google.Protobuf new FieldCodecTestData(FieldCodec.ForSFixed64(100), -1000, "SFixed64"), new FieldCodecTestData(FieldCodec.ForUInt64(100), 1234, "UInt64"), new FieldCodecTestData(FieldCodec.ForFixed64(100), 1234, "Fixed64"), - new FieldCodecTestData(FieldCodec.ForFloat(100), 1234.5f, "Float"), - new FieldCodecTestData(FieldCodec.ForDouble(100), 1234567890.5d, "Double"), + new FieldCodecTestData(FieldCodec.ForFloat(100), 1234.5f, "FixedFloat"), + new FieldCodecTestData(FieldCodec.ForDouble(100), 1234567890.5d, "FixedDouble"), new FieldCodecTestData( FieldCodec.ForEnum(100, t => (int) t, t => (ForeignEnum) t), ForeignEnum.ForeignBaz, "Enum"), new FieldCodecTestData( diff --git a/csharp/src/Google.Protobuf.Test/FieldMaskTreeTest.cs b/csharp/src/Google.Protobuf.Test/FieldMaskTreeTest.cs new file mode 100644 index 0000000000..f71744a874 --- /dev/null +++ b/csharp/src/Google.Protobuf.Test/FieldMaskTreeTest.cs @@ -0,0 +1,436 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 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. +#endregion + +using System.Collections.Generic; +using Google.Protobuf.Collections; +using Google.Protobuf.TestProtos; +using NUnit.Framework; +using Google.Protobuf.WellKnownTypes; + +namespace Google.Protobuf +{ + public class FieldMaskTreeTest + { + [Test] + public void AddFieldPath() + { + FieldMaskTree tree = new FieldMaskTree(); + RepeatedField paths = tree.ToFieldMask().Paths; + Assert.AreEqual(0, paths.Count); + + tree.AddFieldPath(""); + paths = tree.ToFieldMask().Paths; + Assert.AreEqual(1, paths.Count); + Assert.Contains("", paths); + + // New branch. + tree.AddFieldPath("foo"); + paths = tree.ToFieldMask().Paths; + Assert.AreEqual(2, paths.Count); + Assert.Contains("foo", paths); + + // Redundant path. + tree.AddFieldPath("foo"); + paths = tree.ToFieldMask().Paths; + Assert.AreEqual(2, paths.Count); + + // New branch. + tree.AddFieldPath("bar.baz"); + paths = tree.ToFieldMask().Paths; + Assert.AreEqual(3, paths.Count); + Assert.Contains("bar.baz", paths); + + // Redundant sub-path. + tree.AddFieldPath("foo.bar"); + paths = tree.ToFieldMask().Paths; + Assert.AreEqual(3, paths.Count); + + // New branch from a non-root node. + tree.AddFieldPath("bar.quz"); + paths = tree.ToFieldMask().Paths; + Assert.AreEqual(4, paths.Count); + Assert.Contains("bar.quz", paths); + + // A path that matches several existing sub-paths. + tree.AddFieldPath("bar"); + paths = tree.ToFieldMask().Paths; + Assert.AreEqual(3, paths.Count); + Assert.Contains("foo", paths); + Assert.Contains("bar", paths); + } + + [Test] + public void MergeFromFieldMask() + { + FieldMaskTree tree = new FieldMaskTree(); + tree.MergeFromFieldMask(new FieldMask + { + Paths = {"foo", "bar.baz", "bar.quz"} + }); + RepeatedField paths = tree.ToFieldMask().Paths; + Assert.AreEqual(3, paths.Count); + Assert.Contains("foo", paths); + Assert.Contains("bar.baz", paths); + Assert.Contains("bar.quz", paths); + + tree.MergeFromFieldMask(new FieldMask + { + Paths = {"foo.bar", "bar"} + }); + paths = tree.ToFieldMask().Paths; + Assert.AreEqual(2, paths.Count); + Assert.Contains("foo", paths); + Assert.Contains("bar", paths); + } + + [Test] + public void IntersectFieldPath() + { + FieldMaskTree tree = new FieldMaskTree(); + FieldMaskTree result = new FieldMaskTree(); + tree.MergeFromFieldMask(new FieldMask + { + Paths = {"foo", "bar.baz", "bar.quz"} + }); + + // Empty path. + tree.IntersectFieldPath("", result); + RepeatedField paths = result.ToFieldMask().Paths; + Assert.AreEqual(0, paths.Count); + + // Non-exist path. + tree.IntersectFieldPath("quz", result); + paths = result.ToFieldMask().Paths; + Assert.AreEqual(0, paths.Count); + + // Sub-path of an existing leaf. + tree.IntersectFieldPath("foo.bar", result); + paths = result.ToFieldMask().Paths; + Assert.AreEqual(1, paths.Count); + Assert.Contains("foo.bar", paths); + + // Match an existing leaf node. + tree.IntersectFieldPath("foo", result); + paths = result.ToFieldMask().Paths; + Assert.AreEqual(1, paths.Count); + Assert.Contains("foo", paths); + + // Non-exist path. + tree.IntersectFieldPath("bar.foo", result); + paths = result.ToFieldMask().Paths; + Assert.AreEqual(1, paths.Count); + Assert.Contains("foo", paths); + + // Match a non-leaf node. + tree.IntersectFieldPath("bar", result); + paths = result.ToFieldMask().Paths; + Assert.AreEqual(3, paths.Count); + Assert.Contains("foo", paths); + Assert.Contains("bar.baz", paths); + Assert.Contains("bar.quz", paths); + } + + private void Merge(FieldMaskTree tree, IMessage source, IMessage destination, FieldMask.MergeOptions options, bool useDynamicMessage) + { + if (useDynamicMessage) + { + var newSource = source.Descriptor.Parser.CreateTemplate(); + newSource.MergeFrom(source.ToByteString()); + + var newDestination = source.Descriptor.Parser.CreateTemplate(); + newDestination.MergeFrom(destination.ToByteString()); + + tree.Merge(newSource, newDestination, options); + + // Clear before merging: + foreach (var fieldDescriptor in destination.Descriptor.Fields.InFieldNumberOrder()) + { + fieldDescriptor.Accessor.Clear(destination); + } + destination.MergeFrom(newDestination.ToByteString()); + } + else + { + tree.Merge(source, destination, options); + } + } + + [Test] + [TestCase(true)] + [TestCase(false)] + public void Merge(bool useDynamicMessage) + { + TestAllTypes value = new TestAllTypes + { + SingleInt32 = 1234, + SingleNestedMessage = new TestAllTypes.Types.NestedMessage {Bb = 5678}, + RepeatedInt32 = {4321}, + RepeatedNestedMessage = {new TestAllTypes.Types.NestedMessage {Bb = 8765}} + }; + + NestedTestAllTypes source = new NestedTestAllTypes + { + Payload = value, + Child = new NestedTestAllTypes {Payload = value} + }; + // Now we have a message source with the following structure: + // [root] -+- payload -+- single_int32 + // | +- single_nested_message + // | +- repeated_int32 + // | +- repeated_nested_message + // | + // +- child --- payload -+- single_int32 + // +- single_nested_message + // +- repeated_int32 + // +- repeated_nested_message + + FieldMask.MergeOptions options = new FieldMask.MergeOptions(); + + // Test merging each individual field. + NestedTestAllTypes destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"), + source, destination, options, useDynamicMessage); + NestedTestAllTypes expected = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleInt32 = 1234 + } + }; + Assert.AreEqual(expected, destination); + + destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("payload.single_nested_message"), + source, destination, options, useDynamicMessage); + expected = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleNestedMessage = new TestAllTypes.Types.NestedMessage {Bb = 5678} + } + }; + Assert.AreEqual(expected, destination); + + destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("payload.repeated_int32"), + source, destination, options, useDynamicMessage); + expected = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + RepeatedInt32 = {4321} + } + }; + Assert.AreEqual(expected, destination); + + destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("payload.repeated_nested_message"), + source, destination, options, useDynamicMessage); + expected = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + RepeatedNestedMessage = {new TestAllTypes.Types.NestedMessage {Bb = 8765}} + } + }; + Assert.AreEqual(expected, destination); + + destination = new NestedTestAllTypes(); + Merge( + new FieldMaskTree().AddFieldPath("child.payload.single_int32"), + source, + destination, + options, + useDynamicMessage); + expected = new NestedTestAllTypes + { + Child = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleInt32 = 1234 + } + } + }; + Assert.AreEqual(expected, destination); + + destination = new NestedTestAllTypes(); + Merge( + new FieldMaskTree().AddFieldPath("child.payload.single_nested_message"), + source, + destination, + options, + useDynamicMessage); + expected = new NestedTestAllTypes + { + Child = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleNestedMessage = new TestAllTypes.Types.NestedMessage {Bb = 5678} + } + } + }; + Assert.AreEqual(expected, destination); + + destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("child.payload.repeated_int32"), + source, destination, options, useDynamicMessage); + expected = new NestedTestAllTypes + { + Child = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + RepeatedInt32 = {4321} + } + } + }; + Assert.AreEqual(expected, destination); + + destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("child.payload.repeated_nested_message"), + source, destination, options, useDynamicMessage); + expected = new NestedTestAllTypes + { + Child = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + RepeatedNestedMessage = {new TestAllTypes.Types.NestedMessage {Bb = 8765}} + } + } + }; + Assert.AreEqual(expected, destination); + + destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("child").AddFieldPath("payload"), + source, destination, options, useDynamicMessage); + Assert.AreEqual(source, destination); + + // Test repeated options. + destination = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + RepeatedInt32 = { 1000 } + } + }; + Merge(new FieldMaskTree().AddFieldPath("payload.repeated_int32"), + source, destination, options, useDynamicMessage); + // Default behavior is to append repeated fields. + Assert.AreEqual(2, destination.Payload.RepeatedInt32.Count); + Assert.AreEqual(1000, destination.Payload.RepeatedInt32[0]); + Assert.AreEqual(4321, destination.Payload.RepeatedInt32[1]); + // Change to replace repeated fields. + options.ReplaceRepeatedFields = true; + Merge(new FieldMaskTree().AddFieldPath("payload.repeated_int32"), + source, destination, options, useDynamicMessage); + Assert.AreEqual(1, destination.Payload.RepeatedInt32.Count); + Assert.AreEqual(4321, destination.Payload.RepeatedInt32[0]); + + // Test message options. + destination = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleInt32 = 1000, + SingleUint32 = 2000 + } + }; + Merge(new FieldMaskTree().AddFieldPath("payload"), + source, destination, options, useDynamicMessage); + // Default behavior is to merge message fields. + Assert.AreEqual(1234, destination.Payload.SingleInt32); + Assert.AreEqual(2000, destination.Payload.SingleUint32); + + // Test merging unset message fields. + NestedTestAllTypes clearedSource = source.Clone(); + clearedSource.Payload = null; + destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("payload"), + clearedSource, destination, options, useDynamicMessage); + Assert.IsNull(destination.Payload); + + // Skip a message field if they are unset in both source and target. + destination = new NestedTestAllTypes(); + Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"), + clearedSource, destination, options, useDynamicMessage); + Assert.IsNull(destination.Payload); + + // Change to replace message fields. + options.ReplaceMessageFields = true; + destination = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleInt32 = 1000, + SingleUint32 = 2000 + } + }; + Merge(new FieldMaskTree().AddFieldPath("payload"), + source, destination, options, useDynamicMessage); + Assert.AreEqual(1234, destination.Payload.SingleInt32); + Assert.AreEqual(0, destination.Payload.SingleUint32); + + // Test merging unset message fields. + destination = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleInt32 = 1000, + SingleUint32 = 2000 + } + }; + Merge(new FieldMaskTree().AddFieldPath("payload"), + clearedSource, destination, options, useDynamicMessage); + Assert.IsNull(destination.Payload); + + // Test merging unset primitive fields. + destination = source.Clone(); + destination.Payload.SingleInt32 = 0; + NestedTestAllTypes sourceWithPayloadInt32Unset = destination; + destination = source.Clone(); + Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"), + sourceWithPayloadInt32Unset, destination, options, useDynamicMessage); + Assert.AreEqual(0, destination.Payload.SingleInt32); + + // Change to clear unset primitive fields. + options.ReplacePrimitiveFields = true; + destination = source.Clone(); + Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"), + sourceWithPayloadInt32Unset, destination, options, useDynamicMessage); + Assert.IsNotNull(destination.Payload); + } + + } +} diff --git a/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj b/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj index 6a430116be..32bb26c990 100644 --- a/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj +++ b/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj @@ -1,7 +1,7 @@  - net451;netcoreapp1.0 + net451;netcoreapp2.1 ../../keys/Google.Protobuf.snk true true @@ -18,13 +18,17 @@ - - netcoreapp1.0 + netcoreapp2.1 + + + + diff --git a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs index 1c7a8cdf24..b07a84161a 100644 --- a/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs @@ -243,7 +243,7 @@ namespace Google.Protobuf [Test] public void InvalidSurrogatePairsFail() { - // Note: don't use TestCase for these, as the strings can't be reliably represented + // Note: don't use TestCase for these, as the strings can't be reliably represented // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/ // Lone low surrogate diff --git a/csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs b/csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs index 33d3503651..df43effd4f 100644 --- a/csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs @@ -240,7 +240,7 @@ namespace Google.Protobuf AssertTokens("{'x': 'y'}", JsonToken.StartObject, JsonToken.Name("x"), JsonToken.Value("y"), JsonToken.EndObject); } - + [Test] [TestCase("[10, 20", 3)] [TestCase("[10,", 2)] @@ -305,7 +305,7 @@ namespace Google.Protobuf [Test] public void ObjectMixedType() { - AssertTokens(@"{'a': 1, 'b': 'bar', 'c': null, 'd': false, 'e': true, + AssertTokens(@"{'a': 1, 'b': 'bar', 'c': null, 'd': false, 'e': true, 'f': [2], 'g': {'x':'y' }}", JsonToken.StartObject, JsonToken.Name("a"), @@ -365,12 +365,12 @@ namespace Google.Protobuf tokenizer.SkipValue(); Assert.AreEqual("next", tokenizer.Next().StringValue); } - + /// /// Asserts that the specified JSON is tokenized into the given sequence of tokens. /// All apostrophes are first converted to double quotes, allowing any tests /// that don't need to check actual apostrophe handling to use apostrophes in the JSON, avoiding - /// messy string literal escaping. The "end document" token is not specified in the list of + /// messy string literal escaping. The "end document" token is not specified in the list of /// expected tokens, but is implicit. /// private static void AssertTokens(string json, params JsonToken[] expectedTokens) @@ -382,7 +382,7 @@ namespace Google.Protobuf /// Asserts that the specified JSON is tokenized into the given sequence of tokens. /// Unlike , this does not perform any character /// replacement on the specified JSON, and should be used when the text contains apostrophes which - /// are expected to be used *as* apostrophes. The "end document" token is not specified in the list of + /// are expected to be used *as* apostrophes. The "end document" token is not specified in the list of /// expected tokens, but is implicit. /// private static void AssertTokensNoReplacement(string json, params JsonToken[] expectedTokens) diff --git a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs index 68b4d6af7c..ff4e9a7ffd 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs @@ -99,66 +99,6 @@ namespace Google.Protobuf.Test.Reflection { delegate bool OptionFetcher(int field, out T value); - [Test] - public void EmptyOptionsIsShared() - { - var structOptions = Struct.Descriptor.CustomOptions; - var timestampOptions = Struct.Descriptor.CustomOptions; - Assert.AreSame(structOptions, timestampOptions); - } - - [Test] - public void SimpleIntegerTest() - { - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - output.WriteTag(MakeTag(1, WireType.Varint)); - output.WriteInt32(1234567); - output.Flush(); - stream.Position = 0; - var input = new CodedInputStream(stream); - input.ReadTag(); - - var options = CustomOptions.Empty; - options = options.ReadOrSkipUnknownField(input); - - int intValue; - Assert.True(options.TryGetInt32(1, out intValue)); - Assert.AreEqual(1234567, intValue); - - string stringValue; - // No ByteString stored values - Assert.False(options.TryGetString(1, out stringValue)); - // Nothing stored for field 2 - Assert.False(options.TryGetInt32(2, out intValue)); - } - - [Test] - public void SimpleStringTest() - { - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - output.WriteTag(MakeTag(1, WireType.LengthDelimited)); - output.WriteString("value"); - output.Flush(); - stream.Position = 0; - var input = new CodedInputStream(stream); - input.ReadTag(); - - var options = CustomOptions.Empty; - options = options.ReadOrSkipUnknownField(input); - - string stringValue; - Assert.True(options.TryGetString(1, out stringValue)); - Assert.AreEqual("value", stringValue); - - int intValue; - // No numeric stored values - Assert.False(options.TryGetInt32(1, out intValue)); - // Nothing stored for field 2 - Assert.False(options.TryGetString(2, out stringValue)); - } - [Test] public void ScalarOptions() { @@ -168,7 +108,7 @@ namespace Google.Protobuf.Test.Reflection AssertOption(1.234567890123456789d, options.TryGetDouble, DoubleOpt); AssertOption("Hello, \"World\"", options.TryGetString, StringOpt); AssertOption(ByteString.CopyFromUtf8("Hello\0World"), options.TryGetBytes, BytesOpt); - AssertOption((int) TestEnumType.TestOptionEnumType2, options.TryGetInt32, EnumOpt); + AssertOption((int)TestEnumType.TestOptionEnumType2, options.TryGetInt32, EnumOpt); } [Test] @@ -177,11 +117,12 @@ namespace Google.Protobuf.Test.Reflection var options = VariousComplexOptions.Descriptor.CustomOptions; AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, options.TryGetMessage, ComplexOpt1); AssertOption(new ComplexOptionType2 - { - Baz = 987, Bar = new ComplexOptionType1 { Foo = 743 }, - Fred = new ComplexOptionType4 { Waldo = 321 }, - Barney = { new ComplexOptionType4 { Waldo = 101 }, new ComplexOptionType4 { Waldo = 212 } } - }, + { + Baz = 987, + Bar = new ComplexOptionType1 { Foo = 743 }, + Fred = new ComplexOptionType4 { Waldo = 321 }, + Barney = { new ComplexOptionType4 { Waldo = 101 }, new ComplexOptionType4 { Waldo = 212 } } + }, options.TryGetMessage, ComplexOpt2); AssertOption(new ComplexOptionType3 { Qux = 9 }, options.TryGetMessage, ComplexOpt3); } @@ -195,7 +136,7 @@ namespace Google.Protobuf.Test.Reflection var messageOptions = TestMessageWithCustomOptions.Descriptor.CustomOptions; AssertOption(-56, messageOptions.TryGetInt32, MessageOpt1); - var fieldOptions = TestMessageWithCustomOptions.Descriptor.Fields["field1"] .CustomOptions; + var fieldOptions = TestMessageWithCustomOptions.Descriptor.Fields["field1"].CustomOptions; AssertOption(8765432109UL, fieldOptions.TryGetFixed64, FieldOpt1); var oneofOptions = TestMessageWithCustomOptions.Descriptor.Oneofs[0].CustomOptions; @@ -213,7 +154,7 @@ namespace Google.Protobuf.Test.Reflection AssertOption(-9876543210, serviceOptions.TryGetSInt64, ServiceOpt1); var methodOptions = service.Methods[0].CustomOptions; - AssertOption((int) UnitTest.Issues.TestProtos.MethodOpt1.Val2, methodOptions.TryGetInt32, CustomOptionNumber.MethodOpt1); + AssertOption((int)UnitTest.Issues.TestProtos.MethodOpt1.Val2, methodOptions.TryGetInt32, CustomOptionNumber.MethodOpt1); } [Test] @@ -264,7 +205,7 @@ namespace Google.Protobuf.Test.Reflection private void AssertOption(T expected, OptionFetcher fetcher, CustomOptionNumber field) { T actual; - Assert.IsTrue(fetcher((int) field, out actual)); + Assert.IsTrue(fetcher((int)field, out actual)); Assert.AreEqual(expected, actual); } } diff --git a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorDeclarationTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorDeclarationTest.cs new file mode 100644 index 0000000000..d9d5667245 --- /dev/null +++ b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorDeclarationTest.cs @@ -0,0 +1,175 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2018 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. +#endregion + +using Google.Protobuf.Reflection; +using NUnit.Framework; +using System.Linq; +using System.Reflection; + +namespace Google.Protobuf.Test.Reflection +{ + // In reality this isn't a test for DescriptorDeclaration so much as the way they're loaded. + public class DescriptorDeclarationTest + { + static readonly FileDescriptor unitTestProto3Descriptor = LoadProtos(); + + // Note: we don't expose a declaration for FileDescriptor as it doesn't have comments + // at the moment and the locations aren't terribly useful. + + // The tests for most elements are quite basic: we don't test every aspect of every element. + // The code within the library falls into two categories: + // - Exposing the properties for *any* declaration + // - Finding the right declaration for an element from the descriptor data + // We have a per-element check to make sure we *are* finding the right declaration, and we + // check every property of declarations in at least one test, but we don't have a cross-product. + // That would effectively be testing protoc, which seems redundant here. + + [Test] + public void ServiceComments() + { + var service = unitTestProto3Descriptor.FindTypeByName("TestService"); + Assert.NotNull(service.Declaration); + Assert.AreEqual(" This is a test service\n", service.Declaration.LeadingComments); + } + + [Test] + public void MethodComments() + { + var service = unitTestProto3Descriptor.FindTypeByName("TestService"); + var method = service.FindMethodByName("Foo"); + Assert.NotNull(method.Declaration); + Assert.AreEqual(" This is a test method\n", method.Declaration.LeadingComments); + } + + [Test] + public void MessageComments() + { + var message = unitTestProto3Descriptor.FindTypeByName("CommentMessage"); + Assert.NotNull(message.Declaration); + Assert.AreEqual(" This is a leading comment\n", message.Declaration.LeadingComments); + Assert.AreEqual(new[] { " This is leading detached comment 1\n", " This is leading detached comment 2\n" }, + message.Declaration.LeadingDetachedComments); + } + + // Note: this test is somewhat brittle; a change earlier in the proto will break it. + [Test] + public void MessageLocations() + { + var message = unitTestProto3Descriptor.FindTypeByName("CommentMessage"); + Assert.NotNull(message.Declaration); + Assert.AreEqual(389, message.Declaration.StartLine); + Assert.AreEqual(1, message.Declaration.StartColumn); + + Assert.AreEqual(404, message.Declaration.EndLine); + Assert.AreEqual(2, message.Declaration.EndColumn); + } + + [Test] + public void EnumComments() + { + var descriptor = unitTestProto3Descriptor.FindTypeByName("CommentEnum"); + Assert.NotNull(descriptor.Declaration); + Assert.AreEqual(" Leading enum comment\n", descriptor.Declaration.LeadingComments); + } + + [Test] + public void NestedMessageComments() + { + var outer = unitTestProto3Descriptor.FindTypeByName("CommentMessage"); + var nested = outer.FindDescriptor("NestedCommentMessage"); + Assert.NotNull(nested.Declaration); + Assert.AreEqual(" Leading nested message comment\n", nested.Declaration.LeadingComments); + } + + [Test] + public void NestedEnumComments() + { + var outer = unitTestProto3Descriptor.FindTypeByName("CommentMessage"); + var nested = outer.FindDescriptor("NestedCommentEnum"); + Assert.NotNull(nested.Declaration); + Assert.AreEqual(" Leading nested enum comment\n", nested.Declaration.LeadingComments); + } + + [Test] + public void FieldComments() + { + var message = unitTestProto3Descriptor.FindTypeByName("CommentMessage"); + var field = message.FindFieldByName("text"); + Assert.NotNull(field.Declaration); + Assert.AreEqual(" Leading field comment\n", field.Declaration.LeadingComments); + Assert.AreEqual(" Trailing field comment\n", field.Declaration.TrailingComments); + } + + [Test] + public void NestedMessageFieldComments() + { + var outer = unitTestProto3Descriptor.FindTypeByName("CommentMessage"); + var nested = outer.FindDescriptor("NestedCommentMessage"); + var field = nested.FindFieldByName("nested_text"); + Assert.NotNull(field.Declaration); + Assert.AreEqual(" Leading nested message field comment\n", field.Declaration.LeadingComments); + } + + [Test] + public void EnumValueComments() + { + var enumDescriptor = unitTestProto3Descriptor.FindTypeByName("CommentEnum"); + var value = enumDescriptor.FindValueByName("ZERO_VALUE"); + Assert.NotNull(value.Declaration); + Assert.AreEqual(" Zero value comment\n", value.Declaration.LeadingComments); + } + + [Test] + public void NestedEnumValueComments() + { + var outer = unitTestProto3Descriptor.FindTypeByName("CommentMessage"); + var nested = outer.FindDescriptor("NestedCommentEnum"); + var value = nested.FindValueByName("ZERO_VALUE"); + Assert.NotNull(value.Declaration); + Assert.AreEqual(" Zero value comment\n", value.Declaration.LeadingComments); + } + + private static FileDescriptor LoadProtos() + { + var type = typeof(DescriptorDeclarationTest); + // TODO: Make this simpler :) + FileDescriptorSet descriptorSet; + using (var stream = type.GetTypeInfo().Assembly.GetManifestResourceStream($"Google.Protobuf.Test.testprotos.pb")) + { + descriptorSet = FileDescriptorSet.Parser.ParseFrom(stream); + } + var byteStrings = descriptorSet.File.Select(f => f.ToByteString()).ToList(); + var descriptors = FileDescriptor.BuildFromByteStrings(byteStrings); + return descriptors.Single(d => d.Name == "unittest_proto3.proto"); + } + } +} diff --git a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs index 29a376e049..264cbbe037 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs @@ -30,10 +30,11 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion -using System.Linq; using Google.Protobuf.TestProtos; using NUnit.Framework; -using UnitTest.Issues.TestProtos; +using System; +using System.Collections.Generic; +using System.Linq; namespace Google.Protobuf.Reflection { @@ -44,10 +45,32 @@ namespace Google.Protobuf.Reflection public class DescriptorsTest { [Test] - public void FileDescriptor() + public void FileDescriptor_GeneratedCode() { - FileDescriptor file = UnittestProto3Reflection.Descriptor; + TestFileDescriptor( + UnittestProto3Reflection.Descriptor, + UnittestImportProto3Reflection.Descriptor, + UnittestImportPublicProto3Reflection.Descriptor); + } + [Test] + public void FileDescriptor_BuildFromByteStrings() + { + // The descriptors have to be supplied in an order such that all the + // dependencies come before the descriptors depending on them. + var descriptorData = new List + { + UnittestImportPublicProto3Reflection.Descriptor.SerializedData, + UnittestImportProto3Reflection.Descriptor.SerializedData, + UnittestProto3Reflection.Descriptor.SerializedData + }; + var converted = FileDescriptor.BuildFromByteStrings(descriptorData); + Assert.AreEqual(3, converted.Count); + TestFileDescriptor(converted[2], converted[1], converted[0]); + } + + private void TestFileDescriptor(FileDescriptor file, FileDescriptor importedFile, FileDescriptor importedPublicFile) + { Assert.AreEqual("unittest_proto3.proto", file.Name); Assert.AreEqual("protobuf_unittest3", file.Package); @@ -56,17 +79,12 @@ namespace Google.Protobuf.Reflection // unittest_proto3.proto doesn't have any public imports, but unittest_import_proto3.proto does. Assert.AreEqual(0, file.PublicDependencies.Count); - Assert.AreEqual(1, UnittestImportProto3Reflection.Descriptor.PublicDependencies.Count); - Assert.AreEqual(UnittestImportPublicProto3Reflection.Descriptor, UnittestImportProto3Reflection.Descriptor.PublicDependencies[0]); + Assert.AreEqual(1, importedFile.PublicDependencies.Count); + Assert.AreEqual(importedPublicFile, importedFile.PublicDependencies[0]); Assert.AreEqual(1, file.Dependencies.Count); - Assert.AreEqual(UnittestImportProto3Reflection.Descriptor, file.Dependencies[0]); + Assert.AreEqual(importedFile, file.Dependencies[0]); - MessageDescriptor messageType = TestAllTypes.Descriptor; - Assert.AreSame(typeof(TestAllTypes), messageType.ClrType); - Assert.AreSame(TestAllTypes.Parser, messageType.Parser); - Assert.AreEqual(messageType, file.MessageTypes[0]); - Assert.AreEqual(messageType, file.FindTypeByName("TestAllTypes")); Assert.Null(file.FindTypeByName("NoSuchType")); Assert.Null(file.FindTypeByName("protobuf_unittest3.TestAllTypes")); for (int i = 0; i < file.MessageTypes.Count; i++) @@ -77,8 +95,8 @@ namespace Google.Protobuf.Reflection Assert.AreEqual(file.EnumTypes[0], file.FindTypeByName("ForeignEnum")); Assert.Null(file.FindTypeByName("NoSuchType")); Assert.Null(file.FindTypeByName("protobuf_unittest3.ForeignEnum")); - Assert.AreEqual(1, UnittestImportProto3Reflection.Descriptor.EnumTypes.Count); - Assert.AreEqual("ImportEnum", UnittestImportProto3Reflection.Descriptor.EnumTypes[0].Name); + Assert.AreEqual(1, importedFile.EnumTypes.Count); + Assert.AreEqual("ImportEnum", importedFile.EnumTypes[0].Name); for (int i = 0; i < file.EnumTypes.Count; i++) { Assert.AreEqual(i, file.EnumTypes[i].Index); @@ -97,6 +115,56 @@ namespace Google.Protobuf.Reflection Assert.AreEqual("protobuf_unittest", file.Package); } + [Test] + public void FileDescriptor_BuildFromByteStrings_MissingDependency() + { + var descriptorData = new List + { + UnittestImportProto3Reflection.Descriptor.SerializedData, + UnittestProto3Reflection.Descriptor.SerializedData, + }; + // This will fail, because we're missing UnittestImportPublicProto3Reflection + Assert.Throws(() => FileDescriptor.BuildFromByteStrings(descriptorData)); + } + + [Test] + public void FileDescriptor_BuildFromByteStrings_DuplicateNames() + { + var descriptorData = new List + { + UnittestImportPublicProto3Reflection.Descriptor.SerializedData, + UnittestImportPublicProto3Reflection.Descriptor.SerializedData, + }; + // This will fail due to the same name being used twice + Assert.Throws(() => FileDescriptor.BuildFromByteStrings(descriptorData)); + } + + [Test] + public void FileDescriptor_BuildFromByteStrings_IncorrectOrder() + { + var descriptorData = new List + { + UnittestProto3Reflection.Descriptor.SerializedData, + UnittestImportPublicProto3Reflection.Descriptor.SerializedData, + UnittestImportProto3Reflection.Descriptor.SerializedData + }; + // This will fail, because the dependencies should come first + Assert.Throws(() => FileDescriptor.BuildFromByteStrings(descriptorData)); + + } + + [Test] + public void MessageDescriptorFromGeneratedCodeFileDescriptor() + { + var file = UnittestProto3Reflection.Descriptor; + + MessageDescriptor messageType = TestAllTypes.Descriptor; + Assert.AreSame(typeof(TestAllTypes), messageType.ClrType); + Assert.AreSame(TestAllTypes.Parser, messageType.Parser); + Assert.AreEqual(messageType, file.MessageTypes[0]); + Assert.AreEqual(messageType, file.FindTypeByName("TestAllTypes")); + } + [Test] public void MessageDescriptor() { @@ -145,29 +213,61 @@ namespace Google.Protobuf.Reflection } [Test] - public void FieldDescriptor() + public void FieldDescriptor_GeneratedCode() { - MessageDescriptor messageType = TestAllTypes.Descriptor; - FieldDescriptor primitiveField = messageType.FindDescriptor("single_int32"); - FieldDescriptor enumField = messageType.FindDescriptor("single_nested_enum"); - FieldDescriptor messageField = messageType.FindDescriptor("single_foreign_message"); + TestFieldDescriptor(UnittestProto3Reflection.Descriptor, TestAllTypes.Descriptor, ForeignMessage.Descriptor, ImportMessage.Descriptor); + } + + [Test] + public void FieldDescriptor_BuildFromByteStrings() + { + // The descriptors have to be supplied in an order such that all the + // dependencies come before the descriptors depending on them. + var descriptorData = new List + { + UnittestImportPublicProto3Reflection.Descriptor.SerializedData, + UnittestImportProto3Reflection.Descriptor.SerializedData, + UnittestProto3Reflection.Descriptor.SerializedData + }; + var converted = FileDescriptor.BuildFromByteStrings(descriptorData); + TestFieldDescriptor( + converted[2], + converted[2].FindTypeByName("TestAllTypes"), + converted[2].FindTypeByName("ForeignMessage"), + converted[1].FindTypeByName("ImportMessage")); + } + + public void TestFieldDescriptor( + FileDescriptor unitTestProto3Descriptor, + MessageDescriptor testAllTypesDescriptor, + MessageDescriptor foreignMessageDescriptor, + MessageDescriptor importMessageDescriptor) + { + FieldDescriptor primitiveField = testAllTypesDescriptor.FindDescriptor("single_int32"); + FieldDescriptor enumField = testAllTypesDescriptor.FindDescriptor("single_nested_enum"); + FieldDescriptor foreignMessageField = testAllTypesDescriptor.FindDescriptor("single_foreign_message"); + FieldDescriptor importMessageField = testAllTypesDescriptor.FindDescriptor("single_import_message"); Assert.AreEqual("single_int32", primitiveField.Name); Assert.AreEqual("protobuf_unittest3.TestAllTypes.single_int32", primitiveField.FullName); Assert.AreEqual(1, primitiveField.FieldNumber); - Assert.AreEqual(messageType, primitiveField.ContainingType); - Assert.AreEqual(UnittestProto3Reflection.Descriptor, primitiveField.File); + Assert.AreEqual(testAllTypesDescriptor, primitiveField.ContainingType); + Assert.AreEqual(unitTestProto3Descriptor, primitiveField.File); Assert.AreEqual(FieldType.Int32, primitiveField.FieldType); Assert.IsNull(primitiveField.Proto.Options); - + Assert.AreEqual("single_nested_enum", enumField.Name); Assert.AreEqual(FieldType.Enum, enumField.FieldType); - // Assert.AreEqual(TestAllTypes.Types.NestedEnum.DescriptorProtoFile, enumField.EnumType); + Assert.AreEqual(testAllTypesDescriptor.EnumTypes[0], enumField.EnumType); + + Assert.AreEqual("single_foreign_message", foreignMessageField.Name); + Assert.AreEqual(FieldType.Message, foreignMessageField.FieldType); + Assert.AreEqual(foreignMessageDescriptor, foreignMessageField.MessageType); - Assert.AreEqual("single_foreign_message", messageField.Name); - Assert.AreEqual(FieldType.Message, messageField.FieldType); - Assert.AreEqual(ForeignMessage.Descriptor, messageField.MessageType); + Assert.AreEqual("single_import_message", importMessageField.Name); + Assert.AreEqual(FieldType.Message, importMessageField.FieldType); + Assert.AreEqual(importMessageDescriptor, importMessageField.MessageType); } [Test] @@ -252,7 +352,7 @@ namespace Google.Protobuf.Reflection // NestedMessage single_nested_message = 200; [Test] public void FieldListOrderings() - { + { var fields = TestFieldOrderings.Descriptor.Fields; Assert.AreEqual(new[] { 11, 1, 101, 200 }, fields.InDeclarationOrder().Select(x => x.FieldNumber)); Assert.AreEqual(new[] { 1, 11, 101, 200 }, fields.InFieldNumberOrder().Select(x => x.FieldNumber)); diff --git a/csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs index a488af30d4..46cb1afcd5 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs @@ -213,6 +213,6 @@ namespace Google.Protobuf.Reflection var descriptor = TestAllTypes.Descriptor; Assert.Throws(() => descriptor.Fields[999999].ToString()); Assert.Throws(() => descriptor.Fields["not found"].ToString()); - } + } } } diff --git a/csharp/src/Google.Protobuf.Test/SampleEnum.cs b/csharp/src/Google.Protobuf.Test/SampleEnum.cs index 77447afa12..44f232a53e 100644 --- a/csharp/src/Google.Protobuf.Test/SampleEnum.cs +++ b/csharp/src/Google.Protobuf.Test/SampleEnum.cs @@ -29,7 +29,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion - + namespace Google.Protobuf { // Just a sample enum with positive and negative values to be used in tests. diff --git a/csharp/src/Google.Protobuf.Test/SampleNaNs.cs b/csharp/src/Google.Protobuf.Test/SampleNaNs.cs index 08b50191d1..34019ac1d2 100644 --- a/csharp/src/Google.Protobuf.Test/SampleNaNs.cs +++ b/csharp/src/Google.Protobuf.Test/SampleNaNs.cs @@ -43,7 +43,7 @@ namespace Google.Protobuf // Signalling bit is inverted compared with double.NaN. Doesn't really matter // whether that makes it quiet or signalling - it's different. - public static double SignallingFlipped { get; } = + public static double SignallingFlipped { get; } = BitConverter.Int64BitsToDouble(BitConverter.DoubleToInt64Bits(double.NaN) ^ -0x8000_0000_0000_0000L); // A bit in the middle of the mantissa is flipped; this difference is preserved when casting to float. diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/MapUnittestProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/MapUnittestProto3.cs index 51715a0c03..197b197d0e 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/MapUnittestProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/MapUnittestProto3.cs @@ -150,14 +150,14 @@ namespace Google.Protobuf.TestProtos { "WhACQh2qAhpHb29nbGUuUHJvdG9idWYuVGVzdFByb3Rvc2IGcHJvdG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { global::Google.Protobuf.TestProtos.UnittestProto3Reflection.Descriptor, }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.TestProtos.MapEnum), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMap), global::Google.Protobuf.TestProtos.TestMap.Parser, new[]{ "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapStringString", "MapInt32Bytes", "MapInt32Enum", "MapInt32ForeignMessage" }, null, null, new pbr::GeneratedClrTypeInfo[] { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, }), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMapSubmessage), global::Google.Protobuf.TestProtos.TestMapSubmessage.Parser, new[]{ "TestMap" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMessageMap), global::Google.Protobuf.TestProtos.TestMessageMap.Parser, new[]{ "MapInt32Message" }, null, null, new pbr::GeneratedClrTypeInfo[] { null, }), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestSameTypeMap), global::Google.Protobuf.TestProtos.TestSameTypeMap.Parser, new[]{ "Map1", "Map2" }, null, null, new pbr::GeneratedClrTypeInfo[] { null, null, }), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestArenaMap), global::Google.Protobuf.TestProtos.TestArenaMap.Parser, new[]{ "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapInt32Enum", "MapInt32ForeignMessage" }, null, null, new pbr::GeneratedClrTypeInfo[] { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, }), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType), global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType.Parser, new[]{ "Type" }, null, new[]{ typeof(global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType.Types.Type) }, new pbr::GeneratedClrTypeInfo[] { null, }), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MessageContainingMapCalledEntry), global::Google.Protobuf.TestProtos.MessageContainingMapCalledEntry.Parser, new[]{ "Entry" }, null, null, new pbr::GeneratedClrTypeInfo[] { null, }) + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.TestProtos.MapEnum), }, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMap), global::Google.Protobuf.TestProtos.TestMap.Parser, new[]{ "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapStringString", "MapInt32Bytes", "MapInt32Enum", "MapInt32ForeignMessage" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, }), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMapSubmessage), global::Google.Protobuf.TestProtos.TestMapSubmessage.Parser, new[]{ "TestMap" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMessageMap), global::Google.Protobuf.TestProtos.TestMessageMap.Parser, new[]{ "MapInt32Message" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { null, }), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestSameTypeMap), global::Google.Protobuf.TestProtos.TestSameTypeMap.Parser, new[]{ "Map1", "Map2" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { null, null, }), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestArenaMap), global::Google.Protobuf.TestProtos.TestArenaMap.Parser, new[]{ "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapInt32Enum", "MapInt32ForeignMessage" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, }), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType), global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType.Parser, new[]{ "Type" }, null, new[]{ typeof(global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType.Types.Type) }, null, new pbr::GeneratedClrTypeInfo[] { null, }), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MessageContainingMapCalledEntry), global::Google.Protobuf.TestProtos.MessageContainingMapCalledEntry.Parser, new[]{ "Entry" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { null, }) })); } #endregion @@ -229,7 +229,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_int32" field. public const int MapInt32Int32FieldNumber = 1; private static readonly pbc::MapField.Codec _map_mapInt32Int32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForInt32(16), 10); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForInt32(16, 0), 10); private readonly pbc::MapField mapInt32Int32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Int32 { @@ -239,7 +239,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int64_int64" field. public const int MapInt64Int64FieldNumber = 2; private static readonly pbc::MapField.Codec _map_mapInt64Int64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt64(8), pb::FieldCodec.ForInt64(16), 18); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt64(8, 0L), pb::FieldCodec.ForInt64(16, 0L), 18); private readonly pbc::MapField mapInt64Int64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt64Int64 { @@ -249,7 +249,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_uint32_uint32" field. public const int MapUint32Uint32FieldNumber = 3; private static readonly pbc::MapField.Codec _map_mapUint32Uint32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForUInt32(8), pb::FieldCodec.ForUInt32(16), 26); + = new pbc::MapField.Codec(pb::FieldCodec.ForUInt32(8, 0), pb::FieldCodec.ForUInt32(16, 0), 26); private readonly pbc::MapField mapUint32Uint32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapUint32Uint32 { @@ -259,7 +259,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_uint64_uint64" field. public const int MapUint64Uint64FieldNumber = 4; private static readonly pbc::MapField.Codec _map_mapUint64Uint64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForUInt64(8), pb::FieldCodec.ForUInt64(16), 34); + = new pbc::MapField.Codec(pb::FieldCodec.ForUInt64(8, 0UL), pb::FieldCodec.ForUInt64(16, 0UL), 34); private readonly pbc::MapField mapUint64Uint64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapUint64Uint64 { @@ -269,7 +269,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_sint32_sint32" field. public const int MapSint32Sint32FieldNumber = 5; private static readonly pbc::MapField.Codec _map_mapSint32Sint32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSInt32(8), pb::FieldCodec.ForSInt32(16), 42); + = new pbc::MapField.Codec(pb::FieldCodec.ForSInt32(8, 0), pb::FieldCodec.ForSInt32(16, 0), 42); private readonly pbc::MapField mapSint32Sint32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSint32Sint32 { @@ -279,7 +279,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_sint64_sint64" field. public const int MapSint64Sint64FieldNumber = 6; private static readonly pbc::MapField.Codec _map_mapSint64Sint64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSInt64(8), pb::FieldCodec.ForSInt64(16), 50); + = new pbc::MapField.Codec(pb::FieldCodec.ForSInt64(8, 0L), pb::FieldCodec.ForSInt64(16, 0L), 50); private readonly pbc::MapField mapSint64Sint64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSint64Sint64 { @@ -289,7 +289,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_fixed32_fixed32" field. public const int MapFixed32Fixed32FieldNumber = 7; private static readonly pbc::MapField.Codec _map_mapFixed32Fixed32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForFixed32(13), pb::FieldCodec.ForFixed32(21), 58); + = new pbc::MapField.Codec(pb::FieldCodec.ForFixed32(13, 0), pb::FieldCodec.ForFixed32(21, 0), 58); private readonly pbc::MapField mapFixed32Fixed32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapFixed32Fixed32 { @@ -299,7 +299,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_fixed64_fixed64" field. public const int MapFixed64Fixed64FieldNumber = 8; private static readonly pbc::MapField.Codec _map_mapFixed64Fixed64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForFixed64(9), pb::FieldCodec.ForFixed64(17), 66); + = new pbc::MapField.Codec(pb::FieldCodec.ForFixed64(9, 0UL), pb::FieldCodec.ForFixed64(17, 0UL), 66); private readonly pbc::MapField mapFixed64Fixed64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapFixed64Fixed64 { @@ -309,7 +309,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_sfixed32_sfixed32" field. public const int MapSfixed32Sfixed32FieldNumber = 9; private static readonly pbc::MapField.Codec _map_mapSfixed32Sfixed32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed32(13), pb::FieldCodec.ForSFixed32(21), 74); + = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed32(13, 0), pb::FieldCodec.ForSFixed32(21, 0), 74); private readonly pbc::MapField mapSfixed32Sfixed32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSfixed32Sfixed32 { @@ -319,7 +319,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_sfixed64_sfixed64" field. public const int MapSfixed64Sfixed64FieldNumber = 10; private static readonly pbc::MapField.Codec _map_mapSfixed64Sfixed64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed64(9), pb::FieldCodec.ForSFixed64(17), 82); + = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed64(9, 0L), pb::FieldCodec.ForSFixed64(17, 0L), 82); private readonly pbc::MapField mapSfixed64Sfixed64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSfixed64Sfixed64 { @@ -329,7 +329,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_float" field. public const int MapInt32FloatFieldNumber = 11; private static readonly pbc::MapField.Codec _map_mapInt32Float_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForFloat(21), 90); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForFloat(21, 0F), 90); private readonly pbc::MapField mapInt32Float_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Float { @@ -339,7 +339,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_double" field. public const int MapInt32DoubleFieldNumber = 12; private static readonly pbc::MapField.Codec _map_mapInt32Double_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForDouble(17), 98); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForDouble(17, 0D), 98); private readonly pbc::MapField mapInt32Double_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Double { @@ -349,7 +349,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_bool_bool" field. public const int MapBoolBoolFieldNumber = 13; private static readonly pbc::MapField.Codec _map_mapBoolBool_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForBool(8), pb::FieldCodec.ForBool(16), 106); + = new pbc::MapField.Codec(pb::FieldCodec.ForBool(8, false), pb::FieldCodec.ForBool(16, false), 106); private readonly pbc::MapField mapBoolBool_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapBoolBool { @@ -359,7 +359,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_string_string" field. public const int MapStringStringFieldNumber = 14; private static readonly pbc::MapField.Codec _map_mapStringString_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForString(10), pb::FieldCodec.ForString(18), 114); + = new pbc::MapField.Codec(pb::FieldCodec.ForString(10, ""), pb::FieldCodec.ForString(18, ""), 114); private readonly pbc::MapField mapStringString_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapStringString { @@ -369,7 +369,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_bytes" field. public const int MapInt32BytesFieldNumber = 15; private static readonly pbc::MapField.Codec _map_mapInt32Bytes_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForBytes(18), 122); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForBytes(18, pb::ByteString.Empty), 122); private readonly pbc::MapField mapInt32Bytes_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Bytes { @@ -379,7 +379,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_enum" field. public const int MapInt32EnumFieldNumber = 16; private static readonly pbc::MapField.Codec _map_mapInt32Enum_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForEnum(16, x => (int) x, x => (global::Google.Protobuf.TestProtos.MapEnum) x), 130); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForEnum(16, x => (int) x, x => (global::Google.Protobuf.TestProtos.MapEnum) x, global::Google.Protobuf.TestProtos.MapEnum.Foo), 130); private readonly pbc::MapField mapInt32Enum_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Enum { @@ -389,7 +389,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_foreign_message" field. public const int MapInt32ForeignMessageFieldNumber = 17; private static readonly pbc::MapField.Codec _map_mapInt32ForeignMessage_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.TestProtos.ForeignMessage.Parser), 138); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.TestProtos.ForeignMessage.Parser), 138); private readonly pbc::MapField mapInt32ForeignMessage_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32ForeignMessage { @@ -724,7 +724,7 @@ namespace Google.Protobuf.TestProtos { } if (other.testMap_ != null) { if (testMap_ == null) { - testMap_ = new global::Google.Protobuf.TestProtos.TestMap(); + TestMap = new global::Google.Protobuf.TestProtos.TestMap(); } TestMap.MergeFrom(other.TestMap); } @@ -741,9 +741,9 @@ namespace Google.Protobuf.TestProtos { break; case 10: { if (testMap_ == null) { - testMap_ = new global::Google.Protobuf.TestProtos.TestMap(); + TestMap = new global::Google.Protobuf.TestProtos.TestMap(); } - input.ReadMessage(testMap_); + input.ReadMessage(TestMap); break; } } @@ -789,7 +789,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_message" field. public const int MapInt32MessageFieldNumber = 1; private static readonly pbc::MapField.Codec _map_mapInt32Message_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.TestProtos.TestAllTypes.Parser), 10); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.TestProtos.TestAllTypes.Parser), 10); private readonly pbc::MapField mapInt32Message_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Message { @@ -914,7 +914,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map1" field. public const int Map1FieldNumber = 1; private static readonly pbc::MapField.Codec _map_map1_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForInt32(16), 10); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForInt32(16, 0), 10); private readonly pbc::MapField map1_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField Map1 { @@ -924,7 +924,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map2" field. public const int Map2FieldNumber = 2; private static readonly pbc::MapField.Codec _map_map2_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForInt32(16), 18); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForInt32(16, 0), 18); private readonly pbc::MapField map2_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField Map2 { @@ -1068,7 +1068,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_int32" field. public const int MapInt32Int32FieldNumber = 1; private static readonly pbc::MapField.Codec _map_mapInt32Int32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForInt32(16), 10); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForInt32(16, 0), 10); private readonly pbc::MapField mapInt32Int32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Int32 { @@ -1078,7 +1078,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int64_int64" field. public const int MapInt64Int64FieldNumber = 2; private static readonly pbc::MapField.Codec _map_mapInt64Int64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt64(8), pb::FieldCodec.ForInt64(16), 18); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt64(8, 0L), pb::FieldCodec.ForInt64(16, 0L), 18); private readonly pbc::MapField mapInt64Int64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt64Int64 { @@ -1088,7 +1088,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_uint32_uint32" field. public const int MapUint32Uint32FieldNumber = 3; private static readonly pbc::MapField.Codec _map_mapUint32Uint32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForUInt32(8), pb::FieldCodec.ForUInt32(16), 26); + = new pbc::MapField.Codec(pb::FieldCodec.ForUInt32(8, 0), pb::FieldCodec.ForUInt32(16, 0), 26); private readonly pbc::MapField mapUint32Uint32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapUint32Uint32 { @@ -1098,7 +1098,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_uint64_uint64" field. public const int MapUint64Uint64FieldNumber = 4; private static readonly pbc::MapField.Codec _map_mapUint64Uint64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForUInt64(8), pb::FieldCodec.ForUInt64(16), 34); + = new pbc::MapField.Codec(pb::FieldCodec.ForUInt64(8, 0UL), pb::FieldCodec.ForUInt64(16, 0UL), 34); private readonly pbc::MapField mapUint64Uint64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapUint64Uint64 { @@ -1108,7 +1108,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_sint32_sint32" field. public const int MapSint32Sint32FieldNumber = 5; private static readonly pbc::MapField.Codec _map_mapSint32Sint32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSInt32(8), pb::FieldCodec.ForSInt32(16), 42); + = new pbc::MapField.Codec(pb::FieldCodec.ForSInt32(8, 0), pb::FieldCodec.ForSInt32(16, 0), 42); private readonly pbc::MapField mapSint32Sint32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSint32Sint32 { @@ -1118,7 +1118,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_sint64_sint64" field. public const int MapSint64Sint64FieldNumber = 6; private static readonly pbc::MapField.Codec _map_mapSint64Sint64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSInt64(8), pb::FieldCodec.ForSInt64(16), 50); + = new pbc::MapField.Codec(pb::FieldCodec.ForSInt64(8, 0L), pb::FieldCodec.ForSInt64(16, 0L), 50); private readonly pbc::MapField mapSint64Sint64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSint64Sint64 { @@ -1128,7 +1128,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_fixed32_fixed32" field. public const int MapFixed32Fixed32FieldNumber = 7; private static readonly pbc::MapField.Codec _map_mapFixed32Fixed32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForFixed32(13), pb::FieldCodec.ForFixed32(21), 58); + = new pbc::MapField.Codec(pb::FieldCodec.ForFixed32(13, 0), pb::FieldCodec.ForFixed32(21, 0), 58); private readonly pbc::MapField mapFixed32Fixed32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapFixed32Fixed32 { @@ -1138,7 +1138,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_fixed64_fixed64" field. public const int MapFixed64Fixed64FieldNumber = 8; private static readonly pbc::MapField.Codec _map_mapFixed64Fixed64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForFixed64(9), pb::FieldCodec.ForFixed64(17), 66); + = new pbc::MapField.Codec(pb::FieldCodec.ForFixed64(9, 0UL), pb::FieldCodec.ForFixed64(17, 0UL), 66); private readonly pbc::MapField mapFixed64Fixed64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapFixed64Fixed64 { @@ -1148,7 +1148,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_sfixed32_sfixed32" field. public const int MapSfixed32Sfixed32FieldNumber = 9; private static readonly pbc::MapField.Codec _map_mapSfixed32Sfixed32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed32(13), pb::FieldCodec.ForSFixed32(21), 74); + = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed32(13, 0), pb::FieldCodec.ForSFixed32(21, 0), 74); private readonly pbc::MapField mapSfixed32Sfixed32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSfixed32Sfixed32 { @@ -1158,7 +1158,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_sfixed64_sfixed64" field. public const int MapSfixed64Sfixed64FieldNumber = 10; private static readonly pbc::MapField.Codec _map_mapSfixed64Sfixed64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed64(9), pb::FieldCodec.ForSFixed64(17), 82); + = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed64(9, 0L), pb::FieldCodec.ForSFixed64(17, 0L), 82); private readonly pbc::MapField mapSfixed64Sfixed64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSfixed64Sfixed64 { @@ -1168,7 +1168,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_float" field. public const int MapInt32FloatFieldNumber = 11; private static readonly pbc::MapField.Codec _map_mapInt32Float_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForFloat(21), 90); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForFloat(21, 0F), 90); private readonly pbc::MapField mapInt32Float_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Float { @@ -1178,7 +1178,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_double" field. public const int MapInt32DoubleFieldNumber = 12; private static readonly pbc::MapField.Codec _map_mapInt32Double_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForDouble(17), 98); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForDouble(17, 0D), 98); private readonly pbc::MapField mapInt32Double_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Double { @@ -1188,7 +1188,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_bool_bool" field. public const int MapBoolBoolFieldNumber = 13; private static readonly pbc::MapField.Codec _map_mapBoolBool_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForBool(8), pb::FieldCodec.ForBool(16), 106); + = new pbc::MapField.Codec(pb::FieldCodec.ForBool(8, false), pb::FieldCodec.ForBool(16, false), 106); private readonly pbc::MapField mapBoolBool_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapBoolBool { @@ -1198,7 +1198,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_enum" field. public const int MapInt32EnumFieldNumber = 14; private static readonly pbc::MapField.Codec _map_mapInt32Enum_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForEnum(16, x => (int) x, x => (global::Google.Protobuf.TestProtos.MapEnum) x), 114); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForEnum(16, x => (int) x, x => (global::Google.Protobuf.TestProtos.MapEnum) x, global::Google.Protobuf.TestProtos.MapEnum.Foo), 114); private readonly pbc::MapField mapInt32Enum_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Enum { @@ -1208,7 +1208,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "map_int32_foreign_message" field. public const int MapInt32ForeignMessageFieldNumber = 15; private static readonly pbc::MapField.Codec _map_mapInt32ForeignMessage_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.TestProtos.ForeignMessage.Parser), 122); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.TestProtos.ForeignMessage.Parser), 122); private readonly pbc::MapField mapInt32ForeignMessage_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32ForeignMessage { @@ -1459,7 +1459,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "type" field. public const int TypeFieldNumber = 1; private static readonly pbc::MapField.Codec _map_type_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType.Parser), 10); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType.Parser), 10); private readonly pbc::MapField type_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField Type { @@ -1594,7 +1594,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "entry" field. public const int EntryFieldNumber = 1; private static readonly pbc::MapField.Codec _map_entry_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForInt32(16), 10); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForInt32(16, 0), 10); private readonly pbc::MapField entry_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField Entry { diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/TestMessagesProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/TestMessagesProto3.cs index 7353be7ba9..822ff26813 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/TestMessagesProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/TestMessagesProto3.cs @@ -29,7 +29,7 @@ namespace ProtobufTestMessages.Proto3 { "dWYvYW55LnByb3RvGh5nb29nbGUvcHJvdG9idWYvZHVyYXRpb24ucHJvdG8a", "IGdvb2dsZS9wcm90b2J1Zi9maWVsZF9tYXNrLnByb3RvGhxnb29nbGUvcHJv", "dG9idWYvc3RydWN0LnByb3RvGh9nb29nbGUvcHJvdG9idWYvdGltZXN0YW1w", - "LnByb3RvGh5nb29nbGUvcHJvdG9idWYvd3JhcHBlcnMucHJvdG8itDsKElRl", + "LnByb3RvGh5nb29nbGUvcHJvdG9idWYvd3JhcHBlcnMucHJvdG8ipz0KElRl", "c3RBbGxUeXBlc1Byb3RvMxIWCg5vcHRpb25hbF9pbnQzMhgBIAEoBRIWCg5v", "cHRpb25hbF9pbnQ2NBgCIAEoAxIXCg9vcHRpb25hbF91aW50MzIYAyABKA0S", "FwoPb3B0aW9uYWxfdWludDY0GAQgASgEEhcKD29wdGlvbmFsX3NpbnQzMhgF", @@ -46,169 +46,174 @@ namespace ProtobufTestMessages.Proto3 { "c3RlZF9lbnVtGBUgASgOMjwucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90", "bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk5lc3RlZEVudW0SSQoVb3B0aW9uYWxf", "Zm9yZWlnbl9lbnVtGBYgASgOMioucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5w", - "cm90bzMuRm9yZWlnbkVudW0SIQoVb3B0aW9uYWxfc3RyaW5nX3BpZWNlGBgg", - "ASgJQgIIAhIZCg1vcHRpb25hbF9jb3JkGBkgASgJQgIIARJMChFyZWN1cnNp", - "dmVfbWVzc2FnZRgbIAEoCzIxLnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJv", - "dG8zLlRlc3RBbGxUeXBlc1Byb3RvMxIWCg5yZXBlYXRlZF9pbnQzMhgfIAMo", - "BRIWCg5yZXBlYXRlZF9pbnQ2NBggIAMoAxIXCg9yZXBlYXRlZF91aW50MzIY", - "ISADKA0SFwoPcmVwZWF0ZWRfdWludDY0GCIgAygEEhcKD3JlcGVhdGVkX3Np", - "bnQzMhgjIAMoERIXCg9yZXBlYXRlZF9zaW50NjQYJCADKBISGAoQcmVwZWF0", - "ZWRfZml4ZWQzMhglIAMoBxIYChByZXBlYXRlZF9maXhlZDY0GCYgAygGEhkK", - "EXJlcGVhdGVkX3NmaXhlZDMyGCcgAygPEhkKEXJlcGVhdGVkX3NmaXhlZDY0", - "GCggAygQEhYKDnJlcGVhdGVkX2Zsb2F0GCkgAygCEhcKD3JlcGVhdGVkX2Rv", - "dWJsZRgqIAMoARIVCg1yZXBlYXRlZF9ib29sGCsgAygIEhcKD3JlcGVhdGVk", - "X3N0cmluZxgsIAMoCRIWCg5yZXBlYXRlZF9ieXRlcxgtIAMoDBJgChdyZXBl", - "YXRlZF9uZXN0ZWRfbWVzc2FnZRgwIAMoCzI/LnByb3RvYnVmX3Rlc3RfbWVz", - "c2FnZXMucHJvdG8zLlRlc3RBbGxUeXBlc1Byb3RvMy5OZXN0ZWRNZXNzYWdl", - "Ek8KGHJlcGVhdGVkX2ZvcmVpZ25fbWVzc2FnZRgxIAMoCzItLnByb3RvYnVm", - "X3Rlc3RfbWVzc2FnZXMucHJvdG8zLkZvcmVpZ25NZXNzYWdlEloKFHJlcGVh", - "dGVkX25lc3RlZF9lbnVtGDMgAygOMjwucHJvdG9idWZfdGVzdF9tZXNzYWdl", - "cy5wcm90bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk5lc3RlZEVudW0SSQoVcmVw", - "ZWF0ZWRfZm9yZWlnbl9lbnVtGDQgAygOMioucHJvdG9idWZfdGVzdF9tZXNz", - "YWdlcy5wcm90bzMuRm9yZWlnbkVudW0SIQoVcmVwZWF0ZWRfc3RyaW5nX3Bp", - "ZWNlGDYgAygJQgIIAhIZCg1yZXBlYXRlZF9jb3JkGDcgAygJQgIIARJdCg9t", - "YXBfaW50MzJfaW50MzIYOCADKAsyRC5wcm90b2J1Zl90ZXN0X21lc3NhZ2Vz", - "LnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMuTWFwSW50MzJJbnQzMkVudHJ5", - "El0KD21hcF9pbnQ2NF9pbnQ2NBg5IAMoCzJELnByb3RvYnVmX3Rlc3RfbWVz", - "c2FnZXMucHJvdG8zLlRlc3RBbGxUeXBlc1Byb3RvMy5NYXBJbnQ2NEludDY0", - "RW50cnkSYQoRbWFwX3VpbnQzMl91aW50MzIYOiADKAsyRi5wcm90b2J1Zl90", - "ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMuTWFwVWlu", - "dDMyVWludDMyRW50cnkSYQoRbWFwX3VpbnQ2NF91aW50NjQYOyADKAsyRi5w", - "cm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90", - "bzMuTWFwVWludDY0VWludDY0RW50cnkSYQoRbWFwX3NpbnQzMl9zaW50MzIY", - "PCADKAsyRi5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxs", - "VHlwZXNQcm90bzMuTWFwU2ludDMyU2ludDMyRW50cnkSYQoRbWFwX3NpbnQ2", - "NF9zaW50NjQYPSADKAsyRi5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3Rv", - "My5UZXN0QWxsVHlwZXNQcm90bzMuTWFwU2ludDY0U2ludDY0RW50cnkSZQoT", - "bWFwX2ZpeGVkMzJfZml4ZWQzMhg+IAMoCzJILnByb3RvYnVmX3Rlc3RfbWVz", - "c2FnZXMucHJvdG8zLlRlc3RBbGxUeXBlc1Byb3RvMy5NYXBGaXhlZDMyRml4", - "ZWQzMkVudHJ5EmUKE21hcF9maXhlZDY0X2ZpeGVkNjQYPyADKAsySC5wcm90", - "b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMu", - "TWFwRml4ZWQ2NEZpeGVkNjRFbnRyeRJpChVtYXBfc2ZpeGVkMzJfc2ZpeGVk", - "MzIYQCADKAsySi5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0", - "QWxsVHlwZXNQcm90bzMuTWFwU2ZpeGVkMzJTZml4ZWQzMkVudHJ5EmkKFW1h", - "cF9zZml4ZWQ2NF9zZml4ZWQ2NBhBIAMoCzJKLnByb3RvYnVmX3Rlc3RfbWVz", - "c2FnZXMucHJvdG8zLlRlc3RBbGxUeXBlc1Byb3RvMy5NYXBTZml4ZWQ2NFNm", - "aXhlZDY0RW50cnkSXQoPbWFwX2ludDMyX2Zsb2F0GEIgAygLMkQucHJvdG9i", + "cm90bzMuRm9yZWlnbkVudW0SXAoVb3B0aW9uYWxfYWxpYXNlZF9lbnVtGBcg", + "ASgOMj0ucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVzdEFsbFR5", + "cGVzUHJvdG8zLkFsaWFzZWRFbnVtEiEKFW9wdGlvbmFsX3N0cmluZ19waWVj", + "ZRgYIAEoCUICCAISGQoNb3B0aW9uYWxfY29yZBgZIAEoCUICCAESTAoRcmVj", + "dXJzaXZlX21lc3NhZ2UYGyABKAsyMS5wcm90b2J1Zl90ZXN0X21lc3NhZ2Vz", + "LnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMSFgoOcmVwZWF0ZWRfaW50MzIY", + "HyADKAUSFgoOcmVwZWF0ZWRfaW50NjQYICADKAMSFwoPcmVwZWF0ZWRfdWlu", + "dDMyGCEgAygNEhcKD3JlcGVhdGVkX3VpbnQ2NBgiIAMoBBIXCg9yZXBlYXRl", + "ZF9zaW50MzIYIyADKBESFwoPcmVwZWF0ZWRfc2ludDY0GCQgAygSEhgKEHJl", + "cGVhdGVkX2ZpeGVkMzIYJSADKAcSGAoQcmVwZWF0ZWRfZml4ZWQ2NBgmIAMo", + "BhIZChFyZXBlYXRlZF9zZml4ZWQzMhgnIAMoDxIZChFyZXBlYXRlZF9zZml4", + "ZWQ2NBgoIAMoEBIWCg5yZXBlYXRlZF9mbG9hdBgpIAMoAhIXCg9yZXBlYXRl", + "ZF9kb3VibGUYKiADKAESFQoNcmVwZWF0ZWRfYm9vbBgrIAMoCBIXCg9yZXBl", + "YXRlZF9zdHJpbmcYLCADKAkSFgoOcmVwZWF0ZWRfYnl0ZXMYLSADKAwSYAoX", + "cmVwZWF0ZWRfbmVzdGVkX21lc3NhZ2UYMCADKAsyPy5wcm90b2J1Zl90ZXN0", + "X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMuTmVzdGVkTWVz", + "c2FnZRJPChhyZXBlYXRlZF9mb3JlaWduX21lc3NhZ2UYMSADKAsyLS5wcm90", + "b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5Gb3JlaWduTWVzc2FnZRJaChRy", + "ZXBlYXRlZF9uZXN0ZWRfZW51bRgzIAMoDjI8LnByb3RvYnVmX3Rlc3RfbWVz", + "c2FnZXMucHJvdG8zLlRlc3RBbGxUeXBlc1Byb3RvMy5OZXN0ZWRFbnVtEkkK", + "FXJlcGVhdGVkX2ZvcmVpZ25fZW51bRg0IAMoDjIqLnByb3RvYnVmX3Rlc3Rf", + "bWVzc2FnZXMucHJvdG8zLkZvcmVpZ25FbnVtEiEKFXJlcGVhdGVkX3N0cmlu", + "Z19waWVjZRg2IAMoCUICCAISGQoNcmVwZWF0ZWRfY29yZBg3IAMoCUICCAES", + "XQoPbWFwX2ludDMyX2ludDMyGDggAygLMkQucHJvdG9idWZfdGVzdF9tZXNz", + "YWdlcy5wcm90bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk1hcEludDMySW50MzJF", + "bnRyeRJdCg9tYXBfaW50NjRfaW50NjQYOSADKAsyRC5wcm90b2J1Zl90ZXN0", + "X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMuTWFwSW50NjRJ", + "bnQ2NEVudHJ5EmEKEW1hcF91aW50MzJfdWludDMyGDogAygLMkYucHJvdG9i", "dWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk1h", - "cEludDMyRmxvYXRFbnRyeRJfChBtYXBfaW50MzJfZG91YmxlGEMgAygLMkUu", + "cFVpbnQzMlVpbnQzMkVudHJ5EmEKEW1hcF91aW50NjRfdWludDY0GDsgAygL", + "MkYucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVzdEFsbFR5cGVz", + "UHJvdG8zLk1hcFVpbnQ2NFVpbnQ2NEVudHJ5EmEKEW1hcF9zaW50MzJfc2lu", + "dDMyGDwgAygLMkYucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVz", + "dEFsbFR5cGVzUHJvdG8zLk1hcFNpbnQzMlNpbnQzMkVudHJ5EmEKEW1hcF9z", + "aW50NjRfc2ludDY0GD0gAygLMkYucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5w", + "cm90bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk1hcFNpbnQ2NFNpbnQ2NEVudHJ5", + "EmUKE21hcF9maXhlZDMyX2ZpeGVkMzIYPiADKAsySC5wcm90b2J1Zl90ZXN0", + "X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMuTWFwRml4ZWQz", + "MkZpeGVkMzJFbnRyeRJlChNtYXBfZml4ZWQ2NF9maXhlZDY0GD8gAygLMkgu", "cHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVzdEFsbFR5cGVzUHJv", - "dG8zLk1hcEludDMyRG91YmxlRW50cnkSWQoNbWFwX2Jvb2xfYm9vbBhEIAMo", - "CzJCLnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRlc3RBbGxUeXBl", - "c1Byb3RvMy5NYXBCb29sQm9vbEVudHJ5EmEKEW1hcF9zdHJpbmdfc3RyaW5n", - "GEUgAygLMkYucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVzdEFs", - "bFR5cGVzUHJvdG8zLk1hcFN0cmluZ1N0cmluZ0VudHJ5El8KEG1hcF9zdHJp", - "bmdfYnl0ZXMYRiADKAsyRS5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3Rv", - "My5UZXN0QWxsVHlwZXNQcm90bzMuTWFwU3RyaW5nQnl0ZXNFbnRyeRJwChlt", - "YXBfc3RyaW5nX25lc3RlZF9tZXNzYWdlGEcgAygLMk0ucHJvdG9idWZfdGVz", - "dF9tZXNzYWdlcy5wcm90bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk1hcFN0cmlu", - "Z05lc3RlZE1lc3NhZ2VFbnRyeRJyChptYXBfc3RyaW5nX2ZvcmVpZ25fbWVz", - "c2FnZRhIIAMoCzJOLnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRl", - "c3RBbGxUeXBlc1Byb3RvMy5NYXBTdHJpbmdGb3JlaWduTWVzc2FnZUVudHJ5", - "EmoKFm1hcF9zdHJpbmdfbmVzdGVkX2VudW0YSSADKAsySi5wcm90b2J1Zl90", - "ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMuTWFwU3Ry", - "aW5nTmVzdGVkRW51bUVudHJ5EmwKF21hcF9zdHJpbmdfZm9yZWlnbl9lbnVt", - "GEogAygLMksucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVzdEFs", - "bFR5cGVzUHJvdG8zLk1hcFN0cmluZ0ZvcmVpZ25FbnVtRW50cnkSFgoMb25l", - "b2ZfdWludDMyGG8gASgNSAASXwoUb25lb2ZfbmVzdGVkX21lc3NhZ2UYcCAB", - "KAsyPy5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlw", - "ZXNQcm90bzMuTmVzdGVkTWVzc2FnZUgAEhYKDG9uZW9mX3N0cmluZxhxIAEo", - "CUgAEhUKC29uZW9mX2J5dGVzGHIgASgMSAASFAoKb25lb2ZfYm9vbBhzIAEo", - "CEgAEhYKDG9uZW9mX3VpbnQ2NBh0IAEoBEgAEhUKC29uZW9mX2Zsb2F0GHUg", - "ASgCSAASFgoMb25lb2ZfZG91YmxlGHYgASgBSAASUgoKb25lb2ZfZW51bRh3", - "IAEoDjI8LnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRlc3RBbGxU", - "eXBlc1Byb3RvMy5OZXN0ZWRFbnVtSAASOgoVb3B0aW9uYWxfYm9vbF93cmFw", - "cGVyGMkBIAEoCzIaLmdvb2dsZS5wcm90b2J1Zi5Cb29sVmFsdWUSPAoWb3B0", - "aW9uYWxfaW50MzJfd3JhcHBlchjKASABKAsyGy5nb29nbGUucHJvdG9idWYu", - "SW50MzJWYWx1ZRI8ChZvcHRpb25hbF9pbnQ2NF93cmFwcGVyGMsBIAEoCzIb", - "Lmdvb2dsZS5wcm90b2J1Zi5JbnQ2NFZhbHVlEj4KF29wdGlvbmFsX3VpbnQz", - "Ml93cmFwcGVyGMwBIAEoCzIcLmdvb2dsZS5wcm90b2J1Zi5VSW50MzJWYWx1", - "ZRI+ChdvcHRpb25hbF91aW50NjRfd3JhcHBlchjNASABKAsyHC5nb29nbGUu", - "cHJvdG9idWYuVUludDY0VmFsdWUSPAoWb3B0aW9uYWxfZmxvYXRfd3JhcHBl", - "chjOASABKAsyGy5nb29nbGUucHJvdG9idWYuRmxvYXRWYWx1ZRI+ChdvcHRp", - "b25hbF9kb3VibGVfd3JhcHBlchjPASABKAsyHC5nb29nbGUucHJvdG9idWYu", - "RG91YmxlVmFsdWUSPgoXb3B0aW9uYWxfc3RyaW5nX3dyYXBwZXIY0AEgASgL", - "MhwuZ29vZ2xlLnByb3RvYnVmLlN0cmluZ1ZhbHVlEjwKFm9wdGlvbmFsX2J5", - "dGVzX3dyYXBwZXIY0QEgASgLMhsuZ29vZ2xlLnByb3RvYnVmLkJ5dGVzVmFs", - "dWUSOgoVcmVwZWF0ZWRfYm9vbF93cmFwcGVyGNMBIAMoCzIaLmdvb2dsZS5w", - "cm90b2J1Zi5Cb29sVmFsdWUSPAoWcmVwZWF0ZWRfaW50MzJfd3JhcHBlchjU", - "ASADKAsyGy5nb29nbGUucHJvdG9idWYuSW50MzJWYWx1ZRI8ChZyZXBlYXRl", - "ZF9pbnQ2NF93cmFwcGVyGNUBIAMoCzIbLmdvb2dsZS5wcm90b2J1Zi5JbnQ2", - "NFZhbHVlEj4KF3JlcGVhdGVkX3VpbnQzMl93cmFwcGVyGNYBIAMoCzIcLmdv", - "b2dsZS5wcm90b2J1Zi5VSW50MzJWYWx1ZRI+ChdyZXBlYXRlZF91aW50NjRf", - "d3JhcHBlchjXASADKAsyHC5nb29nbGUucHJvdG9idWYuVUludDY0VmFsdWUS", - "PAoWcmVwZWF0ZWRfZmxvYXRfd3JhcHBlchjYASADKAsyGy5nb29nbGUucHJv", - "dG9idWYuRmxvYXRWYWx1ZRI+ChdyZXBlYXRlZF9kb3VibGVfd3JhcHBlchjZ", - "ASADKAsyHC5nb29nbGUucHJvdG9idWYuRG91YmxlVmFsdWUSPgoXcmVwZWF0", - "ZWRfc3RyaW5nX3dyYXBwZXIY2gEgAygLMhwuZ29vZ2xlLnByb3RvYnVmLlN0", - "cmluZ1ZhbHVlEjwKFnJlcGVhdGVkX2J5dGVzX3dyYXBwZXIY2wEgAygLMhsu", - "Z29vZ2xlLnByb3RvYnVmLkJ5dGVzVmFsdWUSNQoRb3B0aW9uYWxfZHVyYXRp", - "b24YrQIgASgLMhkuZ29vZ2xlLnByb3RvYnVmLkR1cmF0aW9uEjcKEm9wdGlv", - "bmFsX3RpbWVzdGFtcBiuAiABKAsyGi5nb29nbGUucHJvdG9idWYuVGltZXN0", - "YW1wEjgKE29wdGlvbmFsX2ZpZWxkX21hc2sYrwIgASgLMhouZ29vZ2xlLnBy", - "b3RvYnVmLkZpZWxkTWFzaxIxCg9vcHRpb25hbF9zdHJ1Y3QYsAIgASgLMhcu", - "Z29vZ2xlLnByb3RvYnVmLlN0cnVjdBIrCgxvcHRpb25hbF9hbnkYsQIgASgL", - "MhQuZ29vZ2xlLnByb3RvYnVmLkFueRIvCg5vcHRpb25hbF92YWx1ZRiyAiAB", - "KAsyFi5nb29nbGUucHJvdG9idWYuVmFsdWUSNQoRcmVwZWF0ZWRfZHVyYXRp", - "b24YtwIgAygLMhkuZ29vZ2xlLnByb3RvYnVmLkR1cmF0aW9uEjcKEnJlcGVh", - "dGVkX3RpbWVzdGFtcBi4AiADKAsyGi5nb29nbGUucHJvdG9idWYuVGltZXN0", - "YW1wEjcKEnJlcGVhdGVkX2ZpZWxkbWFzaxi5AiADKAsyGi5nb29nbGUucHJv", - "dG9idWYuRmllbGRNYXNrEjEKD3JlcGVhdGVkX3N0cnVjdBjEAiADKAsyFy5n", - "b29nbGUucHJvdG9idWYuU3RydWN0EisKDHJlcGVhdGVkX2FueRi7AiADKAsy", - "FC5nb29nbGUucHJvdG9idWYuQW55Ei8KDnJlcGVhdGVkX3ZhbHVlGLwCIAMo", - "CzIWLmdvb2dsZS5wcm90b2J1Zi5WYWx1ZRITCgpmaWVsZG5hbWUxGJEDIAEo", - "BRIUCgtmaWVsZF9uYW1lMhiSAyABKAUSFQoMX2ZpZWxkX25hbWUzGJMDIAEo", - "BRIWCg1maWVsZF9fbmFtZTRfGJQDIAEoBRIUCgtmaWVsZDBuYW1lNRiVAyAB", - "KAUSFgoNZmllbGRfMF9uYW1lNhiWAyABKAUSEwoKZmllbGROYW1lNxiXAyAB", - "KAUSEwoKRmllbGROYW1lOBiYAyABKAUSFAoLZmllbGRfTmFtZTkYmQMgASgF", - "EhUKDEZpZWxkX05hbWUxMBiaAyABKAUSFQoMRklFTERfTkFNRTExGJsDIAEo", - "BRIVCgxGSUVMRF9uYW1lMTIYnAMgASgFEhcKDl9fZmllbGRfbmFtZTEzGJ0D", - "IAEoBRIXCg5fX0ZpZWxkX25hbWUxNBieAyABKAUSFgoNZmllbGRfX25hbWUx", - "NRifAyABKAUSFgoNZmllbGRfX05hbWUxNhigAyABKAUSFwoOZmllbGRfbmFt", - "ZTE3X18YoQMgASgFEhcKDkZpZWxkX25hbWUxOF9fGKIDIAEoBRpiCg1OZXN0", - "ZWRNZXNzYWdlEgkKAWEYASABKAUSRgoLY29yZWN1cnNpdmUYAiABKAsyMS5w", - "cm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90", - "bzMaNAoSTWFwSW50MzJJbnQzMkVudHJ5EgsKA2tleRgBIAEoBRINCgV2YWx1", - "ZRgCIAEoBToCOAEaNAoSTWFwSW50NjRJbnQ2NEVudHJ5EgsKA2tleRgBIAEo", - "AxINCgV2YWx1ZRgCIAEoAzoCOAEaNgoUTWFwVWludDMyVWludDMyRW50cnkS", - "CwoDa2V5GAEgASgNEg0KBXZhbHVlGAIgASgNOgI4ARo2ChRNYXBVaW50NjRV", - "aW50NjRFbnRyeRILCgNrZXkYASABKAQSDQoFdmFsdWUYAiABKAQ6AjgBGjYK", - "FE1hcFNpbnQzMlNpbnQzMkVudHJ5EgsKA2tleRgBIAEoERINCgV2YWx1ZRgC", - "IAEoEToCOAEaNgoUTWFwU2ludDY0U2ludDY0RW50cnkSCwoDa2V5GAEgASgS", - "Eg0KBXZhbHVlGAIgASgSOgI4ARo4ChZNYXBGaXhlZDMyRml4ZWQzMkVudHJ5", - "EgsKA2tleRgBIAEoBxINCgV2YWx1ZRgCIAEoBzoCOAEaOAoWTWFwRml4ZWQ2", - "NEZpeGVkNjRFbnRyeRILCgNrZXkYASABKAYSDQoFdmFsdWUYAiABKAY6AjgB", - "GjoKGE1hcFNmaXhlZDMyU2ZpeGVkMzJFbnRyeRILCgNrZXkYASABKA8SDQoF", - "dmFsdWUYAiABKA86AjgBGjoKGE1hcFNmaXhlZDY0U2ZpeGVkNjRFbnRyeRIL", - "CgNrZXkYASABKBASDQoFdmFsdWUYAiABKBA6AjgBGjQKEk1hcEludDMyRmxv", - "YXRFbnRyeRILCgNrZXkYASABKAUSDQoFdmFsdWUYAiABKAI6AjgBGjUKE01h", - "cEludDMyRG91YmxlRW50cnkSCwoDa2V5GAEgASgFEg0KBXZhbHVlGAIgASgB", - "OgI4ARoyChBNYXBCb29sQm9vbEVudHJ5EgsKA2tleRgBIAEoCBINCgV2YWx1", - "ZRgCIAEoCDoCOAEaNgoUTWFwU3RyaW5nU3RyaW5nRW50cnkSCwoDa2V5GAEg", - "ASgJEg0KBXZhbHVlGAIgASgJOgI4ARo1ChNNYXBTdHJpbmdCeXRlc0VudHJ5", - "EgsKA2tleRgBIAEoCRINCgV2YWx1ZRgCIAEoDDoCOAEafgobTWFwU3RyaW5n", - "TmVzdGVkTWVzc2FnZUVudHJ5EgsKA2tleRgBIAEoCRJOCgV2YWx1ZRgCIAEo", - "CzI/LnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRlc3RBbGxUeXBl", - "c1Byb3RvMy5OZXN0ZWRNZXNzYWdlOgI4ARptChxNYXBTdHJpbmdGb3JlaWdu", - "TWVzc2FnZUVudHJ5EgsKA2tleRgBIAEoCRI8CgV2YWx1ZRgCIAEoCzItLnBy", - "b3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLkZvcmVpZ25NZXNzYWdlOgI4", - "ARp4ChhNYXBTdHJpbmdOZXN0ZWRFbnVtRW50cnkSCwoDa2V5GAEgASgJEksK", - "BXZhbHVlGAIgASgOMjwucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMu", - "VGVzdEFsbFR5cGVzUHJvdG8zLk5lc3RlZEVudW06AjgBGmcKGU1hcFN0cmlu", - "Z0ZvcmVpZ25FbnVtRW50cnkSCwoDa2V5GAEgASgJEjkKBXZhbHVlGAIgASgO", - "MioucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuRm9yZWlnbkVudW06", - "AjgBIjkKCk5lc3RlZEVudW0SBwoDRk9PEAASBwoDQkFSEAESBwoDQkFaEAIS", - "EAoDTkVHEP///////////wFCDQoLb25lb2ZfZmllbGRKBgj1AxD/AyIbCg5G", - "b3JlaWduTWVzc2FnZRIJCgFjGAEgASgFKkAKC0ZvcmVpZ25FbnVtEg8KC0ZP", - "UkVJR05fRk9PEAASDwoLRk9SRUlHTl9CQVIQARIPCgtGT1JFSUdOX0JBWhAC", - "QjgKKGNvbS5nb29nbGUucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzNI", - "AfgBAaICBlByb3RvM2IGcHJvdG8z")); + "dG8zLk1hcEZpeGVkNjRGaXhlZDY0RW50cnkSaQoVbWFwX3NmaXhlZDMyX3Nm", + "aXhlZDMyGEAgAygLMkoucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMu", + "VGVzdEFsbFR5cGVzUHJvdG8zLk1hcFNmaXhlZDMyU2ZpeGVkMzJFbnRyeRJp", + "ChVtYXBfc2ZpeGVkNjRfc2ZpeGVkNjQYQSADKAsySi5wcm90b2J1Zl90ZXN0", + "X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMuTWFwU2ZpeGVk", + "NjRTZml4ZWQ2NEVudHJ5El0KD21hcF9pbnQzMl9mbG9hdBhCIAMoCzJELnBy", + "b3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRlc3RBbGxUeXBlc1Byb3Rv", + "My5NYXBJbnQzMkZsb2F0RW50cnkSXwoQbWFwX2ludDMyX2RvdWJsZRhDIAMo", + "CzJFLnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRlc3RBbGxUeXBl", + "c1Byb3RvMy5NYXBJbnQzMkRvdWJsZUVudHJ5ElkKDW1hcF9ib29sX2Jvb2wY", + "RCADKAsyQi5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxs", + "VHlwZXNQcm90bzMuTWFwQm9vbEJvb2xFbnRyeRJhChFtYXBfc3RyaW5nX3N0", + "cmluZxhFIAMoCzJGLnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRl", + "c3RBbGxUeXBlc1Byb3RvMy5NYXBTdHJpbmdTdHJpbmdFbnRyeRJfChBtYXBf", + "c3RyaW5nX2J5dGVzGEYgAygLMkUucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5w", + "cm90bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk1hcFN0cmluZ0J5dGVzRW50cnkS", + "cAoZbWFwX3N0cmluZ19uZXN0ZWRfbWVzc2FnZRhHIAMoCzJNLnByb3RvYnVm", + "X3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRlc3RBbGxUeXBlc1Byb3RvMy5NYXBT", + "dHJpbmdOZXN0ZWRNZXNzYWdlRW50cnkScgoabWFwX3N0cmluZ19mb3JlaWdu", + "X21lc3NhZ2UYSCADKAsyTi5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3Rv", + "My5UZXN0QWxsVHlwZXNQcm90bzMuTWFwU3RyaW5nRm9yZWlnbk1lc3NhZ2VF", + "bnRyeRJqChZtYXBfc3RyaW5nX25lc3RlZF9lbnVtGEkgAygLMkoucHJvdG9i", + "dWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk1h", + "cFN0cmluZ05lc3RlZEVudW1FbnRyeRJsChdtYXBfc3RyaW5nX2ZvcmVpZ25f", + "ZW51bRhKIAMoCzJLLnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8zLlRl", + "c3RBbGxUeXBlc1Byb3RvMy5NYXBTdHJpbmdGb3JlaWduRW51bUVudHJ5EhYK", + "DG9uZW9mX3VpbnQzMhhvIAEoDUgAEl8KFG9uZW9mX25lc3RlZF9tZXNzYWdl", + "GHAgASgLMj8ucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMuVGVzdEFs", + "bFR5cGVzUHJvdG8zLk5lc3RlZE1lc3NhZ2VIABIWCgxvbmVvZl9zdHJpbmcY", + "cSABKAlIABIVCgtvbmVvZl9ieXRlcxhyIAEoDEgAEhQKCm9uZW9mX2Jvb2wY", + "cyABKAhIABIWCgxvbmVvZl91aW50NjQYdCABKARIABIVCgtvbmVvZl9mbG9h", + "dBh1IAEoAkgAEhYKDG9uZW9mX2RvdWJsZRh2IAEoAUgAElIKCm9uZW9mX2Vu", + "dW0YdyABKA4yPC5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnByb3RvMy5UZXN0", + "QWxsVHlwZXNQcm90bzMuTmVzdGVkRW51bUgAEjoKFW9wdGlvbmFsX2Jvb2xf", + "d3JhcHBlchjJASABKAsyGi5nb29nbGUucHJvdG9idWYuQm9vbFZhbHVlEjwK", + "Fm9wdGlvbmFsX2ludDMyX3dyYXBwZXIYygEgASgLMhsuZ29vZ2xlLnByb3Rv", + "YnVmLkludDMyVmFsdWUSPAoWb3B0aW9uYWxfaW50NjRfd3JhcHBlchjLASAB", + "KAsyGy5nb29nbGUucHJvdG9idWYuSW50NjRWYWx1ZRI+ChdvcHRpb25hbF91", + "aW50MzJfd3JhcHBlchjMASABKAsyHC5nb29nbGUucHJvdG9idWYuVUludDMy", + "VmFsdWUSPgoXb3B0aW9uYWxfdWludDY0X3dyYXBwZXIYzQEgASgLMhwuZ29v", + "Z2xlLnByb3RvYnVmLlVJbnQ2NFZhbHVlEjwKFm9wdGlvbmFsX2Zsb2F0X3dy", + "YXBwZXIYzgEgASgLMhsuZ29vZ2xlLnByb3RvYnVmLkZsb2F0VmFsdWUSPgoX", + "b3B0aW9uYWxfZG91YmxlX3dyYXBwZXIYzwEgASgLMhwuZ29vZ2xlLnByb3Rv", + "YnVmLkRvdWJsZVZhbHVlEj4KF29wdGlvbmFsX3N0cmluZ193cmFwcGVyGNAB", + "IAEoCzIcLmdvb2dsZS5wcm90b2J1Zi5TdHJpbmdWYWx1ZRI8ChZvcHRpb25h", + "bF9ieXRlc193cmFwcGVyGNEBIAEoCzIbLmdvb2dsZS5wcm90b2J1Zi5CeXRl", + "c1ZhbHVlEjoKFXJlcGVhdGVkX2Jvb2xfd3JhcHBlchjTASADKAsyGi5nb29n", + "bGUucHJvdG9idWYuQm9vbFZhbHVlEjwKFnJlcGVhdGVkX2ludDMyX3dyYXBw", + "ZXIY1AEgAygLMhsuZ29vZ2xlLnByb3RvYnVmLkludDMyVmFsdWUSPAoWcmVw", + "ZWF0ZWRfaW50NjRfd3JhcHBlchjVASADKAsyGy5nb29nbGUucHJvdG9idWYu", + "SW50NjRWYWx1ZRI+ChdyZXBlYXRlZF91aW50MzJfd3JhcHBlchjWASADKAsy", + "HC5nb29nbGUucHJvdG9idWYuVUludDMyVmFsdWUSPgoXcmVwZWF0ZWRfdWlu", + "dDY0X3dyYXBwZXIY1wEgAygLMhwuZ29vZ2xlLnByb3RvYnVmLlVJbnQ2NFZh", + "bHVlEjwKFnJlcGVhdGVkX2Zsb2F0X3dyYXBwZXIY2AEgAygLMhsuZ29vZ2xl", + "LnByb3RvYnVmLkZsb2F0VmFsdWUSPgoXcmVwZWF0ZWRfZG91YmxlX3dyYXBw", + "ZXIY2QEgAygLMhwuZ29vZ2xlLnByb3RvYnVmLkRvdWJsZVZhbHVlEj4KF3Jl", + "cGVhdGVkX3N0cmluZ193cmFwcGVyGNoBIAMoCzIcLmdvb2dsZS5wcm90b2J1", + "Zi5TdHJpbmdWYWx1ZRI8ChZyZXBlYXRlZF9ieXRlc193cmFwcGVyGNsBIAMo", + "CzIbLmdvb2dsZS5wcm90b2J1Zi5CeXRlc1ZhbHVlEjUKEW9wdGlvbmFsX2R1", + "cmF0aW9uGK0CIAEoCzIZLmdvb2dsZS5wcm90b2J1Zi5EdXJhdGlvbhI3ChJv", + "cHRpb25hbF90aW1lc3RhbXAYrgIgASgLMhouZ29vZ2xlLnByb3RvYnVmLlRp", + "bWVzdGFtcBI4ChNvcHRpb25hbF9maWVsZF9tYXNrGK8CIAEoCzIaLmdvb2ds", + "ZS5wcm90b2J1Zi5GaWVsZE1hc2sSMQoPb3B0aW9uYWxfc3RydWN0GLACIAEo", + "CzIXLmdvb2dsZS5wcm90b2J1Zi5TdHJ1Y3QSKwoMb3B0aW9uYWxfYW55GLEC", + "IAEoCzIULmdvb2dsZS5wcm90b2J1Zi5BbnkSLwoOb3B0aW9uYWxfdmFsdWUY", + "sgIgASgLMhYuZ29vZ2xlLnByb3RvYnVmLlZhbHVlEjUKEXJlcGVhdGVkX2R1", + "cmF0aW9uGLcCIAMoCzIZLmdvb2dsZS5wcm90b2J1Zi5EdXJhdGlvbhI3ChJy", + "ZXBlYXRlZF90aW1lc3RhbXAYuAIgAygLMhouZ29vZ2xlLnByb3RvYnVmLlRp", + "bWVzdGFtcBI3ChJyZXBlYXRlZF9maWVsZG1hc2sYuQIgAygLMhouZ29vZ2xl", + "LnByb3RvYnVmLkZpZWxkTWFzaxIxCg9yZXBlYXRlZF9zdHJ1Y3QYxAIgAygL", + "MhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdBIrCgxyZXBlYXRlZF9hbnkYuwIg", + "AygLMhQuZ29vZ2xlLnByb3RvYnVmLkFueRIvCg5yZXBlYXRlZF92YWx1ZRi8", + "AiADKAsyFi5nb29nbGUucHJvdG9idWYuVmFsdWUSOAoTcmVwZWF0ZWRfbGlz", + "dF92YWx1ZRi9AiADKAsyGi5nb29nbGUucHJvdG9idWYuTGlzdFZhbHVlEhMK", + "CmZpZWxkbmFtZTEYkQMgASgFEhQKC2ZpZWxkX25hbWUyGJIDIAEoBRIVCgxf", + "ZmllbGRfbmFtZTMYkwMgASgFEhYKDWZpZWxkX19uYW1lNF8YlAMgASgFEhQK", + "C2ZpZWxkMG5hbWU1GJUDIAEoBRIWCg1maWVsZF8wX25hbWU2GJYDIAEoBRIT", + "CgpmaWVsZE5hbWU3GJcDIAEoBRITCgpGaWVsZE5hbWU4GJgDIAEoBRIUCgtm", + "aWVsZF9OYW1lORiZAyABKAUSFQoMRmllbGRfTmFtZTEwGJoDIAEoBRIVCgxG", + "SUVMRF9OQU1FMTEYmwMgASgFEhUKDEZJRUxEX25hbWUxMhicAyABKAUSFwoO", + "X19maWVsZF9uYW1lMTMYnQMgASgFEhcKDl9fRmllbGRfbmFtZTE0GJ4DIAEo", + "BRIWCg1maWVsZF9fbmFtZTE1GJ8DIAEoBRIWCg1maWVsZF9fTmFtZTE2GKAD", + "IAEoBRIXCg5maWVsZF9uYW1lMTdfXxihAyABKAUSFwoORmllbGRfbmFtZTE4", + "X18YogMgASgFGmIKDU5lc3RlZE1lc3NhZ2USCQoBYRgBIAEoBRJGCgtjb3Jl", + "Y3Vyc2l2ZRgCIAEoCzIxLnByb3RvYnVmX3Rlc3RfbWVzc2FnZXMucHJvdG8z", + "LlRlc3RBbGxUeXBlc1Byb3RvMxo0ChJNYXBJbnQzMkludDMyRW50cnkSCwoD", + "a2V5GAEgASgFEg0KBXZhbHVlGAIgASgFOgI4ARo0ChJNYXBJbnQ2NEludDY0", + "RW50cnkSCwoDa2V5GAEgASgDEg0KBXZhbHVlGAIgASgDOgI4ARo2ChRNYXBV", + "aW50MzJVaW50MzJFbnRyeRILCgNrZXkYASABKA0SDQoFdmFsdWUYAiABKA06", + "AjgBGjYKFE1hcFVpbnQ2NFVpbnQ2NEVudHJ5EgsKA2tleRgBIAEoBBINCgV2", + "YWx1ZRgCIAEoBDoCOAEaNgoUTWFwU2ludDMyU2ludDMyRW50cnkSCwoDa2V5", + "GAEgASgREg0KBXZhbHVlGAIgASgROgI4ARo2ChRNYXBTaW50NjRTaW50NjRF", + "bnRyeRILCgNrZXkYASABKBISDQoFdmFsdWUYAiABKBI6AjgBGjgKFk1hcEZp", + "eGVkMzJGaXhlZDMyRW50cnkSCwoDa2V5GAEgASgHEg0KBXZhbHVlGAIgASgH", + "OgI4ARo4ChZNYXBGaXhlZDY0Rml4ZWQ2NEVudHJ5EgsKA2tleRgBIAEoBhIN", + "CgV2YWx1ZRgCIAEoBjoCOAEaOgoYTWFwU2ZpeGVkMzJTZml4ZWQzMkVudHJ5", + "EgsKA2tleRgBIAEoDxINCgV2YWx1ZRgCIAEoDzoCOAEaOgoYTWFwU2ZpeGVk", + "NjRTZml4ZWQ2NEVudHJ5EgsKA2tleRgBIAEoEBINCgV2YWx1ZRgCIAEoEDoC", + "OAEaNAoSTWFwSW50MzJGbG9hdEVudHJ5EgsKA2tleRgBIAEoBRINCgV2YWx1", + "ZRgCIAEoAjoCOAEaNQoTTWFwSW50MzJEb3VibGVFbnRyeRILCgNrZXkYASAB", + "KAUSDQoFdmFsdWUYAiABKAE6AjgBGjIKEE1hcEJvb2xCb29sRW50cnkSCwoD", + "a2V5GAEgASgIEg0KBXZhbHVlGAIgASgIOgI4ARo2ChRNYXBTdHJpbmdTdHJp", + "bmdFbnRyeRILCgNrZXkYASABKAkSDQoFdmFsdWUYAiABKAk6AjgBGjUKE01h", + "cFN0cmluZ0J5dGVzRW50cnkSCwoDa2V5GAEgASgJEg0KBXZhbHVlGAIgASgM", + "OgI4ARp+ChtNYXBTdHJpbmdOZXN0ZWRNZXNzYWdlRW50cnkSCwoDa2V5GAEg", + "ASgJEk4KBXZhbHVlGAIgASgLMj8ucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5w", + "cm90bzMuVGVzdEFsbFR5cGVzUHJvdG8zLk5lc3RlZE1lc3NhZ2U6AjgBGm0K", + "HE1hcFN0cmluZ0ZvcmVpZ25NZXNzYWdlRW50cnkSCwoDa2V5GAEgASgJEjwK", + "BXZhbHVlGAIgASgLMi0ucHJvdG9idWZfdGVzdF9tZXNzYWdlcy5wcm90bzMu", + "Rm9yZWlnbk1lc3NhZ2U6AjgBGngKGE1hcFN0cmluZ05lc3RlZEVudW1FbnRy", + "eRILCgNrZXkYASABKAkSSwoFdmFsdWUYAiABKA4yPC5wcm90b2J1Zl90ZXN0", + "X21lc3NhZ2VzLnByb3RvMy5UZXN0QWxsVHlwZXNQcm90bzMuTmVzdGVkRW51", + "bToCOAEaZwoZTWFwU3RyaW5nRm9yZWlnbkVudW1FbnRyeRILCgNrZXkYASAB", + "KAkSOQoFdmFsdWUYAiABKA4yKi5wcm90b2J1Zl90ZXN0X21lc3NhZ2VzLnBy", + "b3RvMy5Gb3JlaWduRW51bToCOAEiOQoKTmVzdGVkRW51bRIHCgNGT08QABIH", + "CgNCQVIQARIHCgNCQVoQAhIQCgNORUcQ////////////ASJZCgtBbGlhc2Vk", + "RW51bRINCglBTElBU19GT08QABINCglBTElBU19CQVIQARINCglBTElBU19C", + "QVoQAhIHCgNRVVgQAhIHCgNxdXgQAhIHCgNiQXoQAhoCEAFCDQoLb25lb2Zf", + "ZmllbGRKBgj1AxD/AyIbCg5Gb3JlaWduTWVzc2FnZRIJCgFjGAEgASgFKkAK", + "C0ZvcmVpZ25FbnVtEg8KC0ZPUkVJR05fRk9PEAASDwoLRk9SRUlHTl9CQVIQ", + "ARIPCgtGT1JFSUdOX0JBWhACQjgKKGNvbS5nb29nbGUucHJvdG9idWZfdGVz", + "dF9tZXNzYWdlcy5wcm90bzNIAfgBAaICBlByb3RvM2IGcHJvdG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.AnyReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.DurationReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.FieldMaskReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.StructReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.TimestampReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.WrappersReflection.Descriptor, }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::ProtobufTestMessages.Proto3.ForeignEnum), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufTestMessages.Proto3.TestAllTypesProto3), global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Parser, new[]{ "OptionalInt32", "OptionalInt64", "OptionalUint32", "OptionalUint64", "OptionalSint32", "OptionalSint64", "OptionalFixed32", "OptionalFixed64", "OptionalSfixed32", "OptionalSfixed64", "OptionalFloat", "OptionalDouble", "OptionalBool", "OptionalString", "OptionalBytes", "OptionalNestedMessage", "OptionalForeignMessage", "OptionalNestedEnum", "OptionalForeignEnum", "OptionalStringPiece", "OptionalCord", "RecursiveMessage", "RepeatedInt32", "RepeatedInt64", "RepeatedUint32", "RepeatedUint64", "RepeatedSint32", "RepeatedSint64", "RepeatedFixed32", "RepeatedFixed64", "RepeatedSfixed32", "RepeatedSfixed64", "RepeatedFloat", "RepeatedDouble", "RepeatedBool", "RepeatedString", "RepeatedBytes", "RepeatedNestedMessage", "RepeatedForeignMessage", "RepeatedNestedEnum", "RepeatedForeignEnum", "RepeatedStringPiece", "RepeatedCord", "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapStringString", "MapStringBytes", "MapStringNestedMessage", "MapStringForeignMessage", "MapStringNestedEnum", "MapStringForeignEnum", "OneofUint32", "OneofNestedMessage", "OneofString", "OneofBytes", "OneofBool", "OneofUint64", "OneofFloat", "OneofDouble", "OneofEnum", "OptionalBoolWrapper", "OptionalInt32Wrapper", "OptionalInt64Wrapper", "OptionalUint32Wrapper", "OptionalUint64Wrapper", "OptionalFloatWrapper", "OptionalDoubleWrapper", "OptionalStringWrapper", "OptionalBytesWrapper", "RepeatedBoolWrapper", "RepeatedInt32Wrapper", "RepeatedInt64Wrapper", "RepeatedUint32Wrapper", "RepeatedUint64Wrapper", "RepeatedFloatWrapper", "RepeatedDoubleWrapper", "RepeatedStringWrapper", "RepeatedBytesWrapper", "OptionalDuration", "OptionalTimestamp", "OptionalFieldMask", "OptionalStruct", "OptionalAny", "OptionalValue", "RepeatedDuration", "RepeatedTimestamp", "RepeatedFieldmask", "RepeatedStruct", "RepeatedAny", "RepeatedValue", "Fieldname1", "FieldName2", "FieldName3", "FieldName4", "Field0Name5", "Field0Name6", "FieldName7", "FieldName8", "FieldName9", "FieldName10", "FIELDNAME11", "FIELDName12", "FieldName13", "FieldName14", "FieldName15", "FieldName16", "FieldName17", "FieldName18" }, new[]{ "OneofField" }, new[]{ typeof(global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum) }, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage), global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage.Parser, new[]{ "A", "Corecursive" }, null, null, null), + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::ProtobufTestMessages.Proto3.ForeignEnum), }, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufTestMessages.Proto3.TestAllTypesProto3), global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Parser, new[]{ "OptionalInt32", "OptionalInt64", "OptionalUint32", "OptionalUint64", "OptionalSint32", "OptionalSint64", "OptionalFixed32", "OptionalFixed64", "OptionalSfixed32", "OptionalSfixed64", "OptionalFloat", "OptionalDouble", "OptionalBool", "OptionalString", "OptionalBytes", "OptionalNestedMessage", "OptionalForeignMessage", "OptionalNestedEnum", "OptionalForeignEnum", "OptionalAliasedEnum", "OptionalStringPiece", "OptionalCord", "RecursiveMessage", "RepeatedInt32", "RepeatedInt64", "RepeatedUint32", "RepeatedUint64", "RepeatedSint32", "RepeatedSint64", "RepeatedFixed32", "RepeatedFixed64", "RepeatedSfixed32", "RepeatedSfixed64", "RepeatedFloat", "RepeatedDouble", "RepeatedBool", "RepeatedString", "RepeatedBytes", "RepeatedNestedMessage", "RepeatedForeignMessage", "RepeatedNestedEnum", "RepeatedForeignEnum", "RepeatedStringPiece", "RepeatedCord", "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapStringString", "MapStringBytes", "MapStringNestedMessage", "MapStringForeignMessage", "MapStringNestedEnum", "MapStringForeignEnum", "OneofUint32", "OneofNestedMessage", "OneofString", "OneofBytes", "OneofBool", "OneofUint64", "OneofFloat", "OneofDouble", "OneofEnum", "OptionalBoolWrapper", "OptionalInt32Wrapper", "OptionalInt64Wrapper", "OptionalUint32Wrapper", "OptionalUint64Wrapper", "OptionalFloatWrapper", "OptionalDoubleWrapper", "OptionalStringWrapper", "OptionalBytesWrapper", "RepeatedBoolWrapper", "RepeatedInt32Wrapper", "RepeatedInt64Wrapper", "RepeatedUint32Wrapper", "RepeatedUint64Wrapper", "RepeatedFloatWrapper", "RepeatedDoubleWrapper", "RepeatedStringWrapper", "RepeatedBytesWrapper", "OptionalDuration", "OptionalTimestamp", "OptionalFieldMask", "OptionalStruct", "OptionalAny", "OptionalValue", "RepeatedDuration", "RepeatedTimestamp", "RepeatedFieldmask", "RepeatedStruct", "RepeatedAny", "RepeatedValue", "RepeatedListValue", "Fieldname1", "FieldName2", "FieldName3", "FieldName4", "Field0Name5", "Field0Name6", "FieldName7", "FieldName8", "FieldName9", "FieldName10", "FIELDNAME11", "FIELDName12", "FieldName13", "FieldName14", "FieldName15", "FieldName16", "FieldName17", "FieldName18" }, new[]{ "OneofField" }, new[]{ typeof(global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum), typeof(global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum) }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage), global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage.Parser, new[]{ "A", "Corecursive" }, null, null, null, null), null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, }), - new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufTestMessages.Proto3.ForeignMessage), global::ProtobufTestMessages.Proto3.ForeignMessage.Parser, new[]{ "C" }, null, null, null) + new pbr::GeneratedClrTypeInfo(typeof(global::ProtobufTestMessages.Proto3.ForeignMessage), global::ProtobufTestMessages.Proto3.ForeignMessage.Parser, new[]{ "C" }, null, null, null, null) })); } #endregion @@ -277,6 +282,7 @@ namespace ProtobufTestMessages.Proto3 { optionalForeignMessage_ = other.optionalForeignMessage_ != null ? other.optionalForeignMessage_.Clone() : null; optionalNestedEnum_ = other.optionalNestedEnum_; optionalForeignEnum_ = other.optionalForeignEnum_; + optionalAliasedEnum_ = other.optionalAliasedEnum_; optionalStringPiece_ = other.optionalStringPiece_; optionalCord_ = other.optionalCord_; recursiveMessage_ = other.recursiveMessage_ != null ? other.recursiveMessage_.Clone() : null; @@ -350,6 +356,7 @@ namespace ProtobufTestMessages.Proto3 { repeatedStruct_ = other.repeatedStruct_.Clone(); repeatedAny_ = other.repeatedAny_.Clone(); repeatedValue_ = other.repeatedValue_.Clone(); + repeatedListValue_ = other.repeatedListValue_.Clone(); fieldname1_ = other.fieldname1_; fieldName2_ = other.fieldName2_; FieldName3_ = other.FieldName3_; @@ -598,7 +605,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "optional_nested_enum" field. public const int OptionalNestedEnumFieldNumber = 21; - private global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum optionalNestedEnum_ = 0; + private global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum optionalNestedEnum_ = global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum.Foo; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum OptionalNestedEnum { get { return optionalNestedEnum_; } @@ -609,7 +616,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "optional_foreign_enum" field. public const int OptionalForeignEnumFieldNumber = 22; - private global::ProtobufTestMessages.Proto3.ForeignEnum optionalForeignEnum_ = 0; + private global::ProtobufTestMessages.Proto3.ForeignEnum optionalForeignEnum_ = global::ProtobufTestMessages.Proto3.ForeignEnum.ForeignFoo; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::ProtobufTestMessages.Proto3.ForeignEnum OptionalForeignEnum { get { return optionalForeignEnum_; } @@ -618,6 +625,17 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_aliased_enum" field. + public const int OptionalAliasedEnumFieldNumber = 23; + private global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum optionalAliasedEnum_ = global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum.AliasFoo; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum OptionalAliasedEnum { + get { return optionalAliasedEnum_; } + set { + optionalAliasedEnum_ = value; + } + } + /// Field number for the "optional_string_piece" field. public const int OptionalStringPieceFieldNumber = 24; private string optionalStringPiece_ = ""; @@ -867,7 +885,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_int32_int32" field. public const int MapInt32Int32FieldNumber = 56; private static readonly pbc::MapField.Codec _map_mapInt32Int32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForInt32(16), 450); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForInt32(16, 0), 450); private readonly pbc::MapField mapInt32Int32_ = new pbc::MapField(); /// /// Map @@ -880,7 +898,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_int64_int64" field. public const int MapInt64Int64FieldNumber = 57; private static readonly pbc::MapField.Codec _map_mapInt64Int64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt64(8), pb::FieldCodec.ForInt64(16), 458); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt64(8, 0L), pb::FieldCodec.ForInt64(16, 0L), 458); private readonly pbc::MapField mapInt64Int64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt64Int64 { @@ -890,7 +908,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_uint32_uint32" field. public const int MapUint32Uint32FieldNumber = 58; private static readonly pbc::MapField.Codec _map_mapUint32Uint32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForUInt32(8), pb::FieldCodec.ForUInt32(16), 466); + = new pbc::MapField.Codec(pb::FieldCodec.ForUInt32(8, 0), pb::FieldCodec.ForUInt32(16, 0), 466); private readonly pbc::MapField mapUint32Uint32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapUint32Uint32 { @@ -900,7 +918,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_uint64_uint64" field. public const int MapUint64Uint64FieldNumber = 59; private static readonly pbc::MapField.Codec _map_mapUint64Uint64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForUInt64(8), pb::FieldCodec.ForUInt64(16), 474); + = new pbc::MapField.Codec(pb::FieldCodec.ForUInt64(8, 0UL), pb::FieldCodec.ForUInt64(16, 0UL), 474); private readonly pbc::MapField mapUint64Uint64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapUint64Uint64 { @@ -910,7 +928,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_sint32_sint32" field. public const int MapSint32Sint32FieldNumber = 60; private static readonly pbc::MapField.Codec _map_mapSint32Sint32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSInt32(8), pb::FieldCodec.ForSInt32(16), 482); + = new pbc::MapField.Codec(pb::FieldCodec.ForSInt32(8, 0), pb::FieldCodec.ForSInt32(16, 0), 482); private readonly pbc::MapField mapSint32Sint32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSint32Sint32 { @@ -920,7 +938,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_sint64_sint64" field. public const int MapSint64Sint64FieldNumber = 61; private static readonly pbc::MapField.Codec _map_mapSint64Sint64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSInt64(8), pb::FieldCodec.ForSInt64(16), 490); + = new pbc::MapField.Codec(pb::FieldCodec.ForSInt64(8, 0L), pb::FieldCodec.ForSInt64(16, 0L), 490); private readonly pbc::MapField mapSint64Sint64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSint64Sint64 { @@ -930,7 +948,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_fixed32_fixed32" field. public const int MapFixed32Fixed32FieldNumber = 62; private static readonly pbc::MapField.Codec _map_mapFixed32Fixed32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForFixed32(13), pb::FieldCodec.ForFixed32(21), 498); + = new pbc::MapField.Codec(pb::FieldCodec.ForFixed32(13, 0), pb::FieldCodec.ForFixed32(21, 0), 498); private readonly pbc::MapField mapFixed32Fixed32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapFixed32Fixed32 { @@ -940,7 +958,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_fixed64_fixed64" field. public const int MapFixed64Fixed64FieldNumber = 63; private static readonly pbc::MapField.Codec _map_mapFixed64Fixed64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForFixed64(9), pb::FieldCodec.ForFixed64(17), 506); + = new pbc::MapField.Codec(pb::FieldCodec.ForFixed64(9, 0UL), pb::FieldCodec.ForFixed64(17, 0UL), 506); private readonly pbc::MapField mapFixed64Fixed64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapFixed64Fixed64 { @@ -950,7 +968,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_sfixed32_sfixed32" field. public const int MapSfixed32Sfixed32FieldNumber = 64; private static readonly pbc::MapField.Codec _map_mapSfixed32Sfixed32_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed32(13), pb::FieldCodec.ForSFixed32(21), 514); + = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed32(13, 0), pb::FieldCodec.ForSFixed32(21, 0), 514); private readonly pbc::MapField mapSfixed32Sfixed32_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSfixed32Sfixed32 { @@ -960,7 +978,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_sfixed64_sfixed64" field. public const int MapSfixed64Sfixed64FieldNumber = 65; private static readonly pbc::MapField.Codec _map_mapSfixed64Sfixed64_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed64(9), pb::FieldCodec.ForSFixed64(17), 522); + = new pbc::MapField.Codec(pb::FieldCodec.ForSFixed64(9, 0L), pb::FieldCodec.ForSFixed64(17, 0L), 522); private readonly pbc::MapField mapSfixed64Sfixed64_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapSfixed64Sfixed64 { @@ -970,7 +988,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_int32_float" field. public const int MapInt32FloatFieldNumber = 66; private static readonly pbc::MapField.Codec _map_mapInt32Float_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForFloat(21), 530); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForFloat(21, 0F), 530); private readonly pbc::MapField mapInt32Float_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Float { @@ -980,7 +998,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_int32_double" field. public const int MapInt32DoubleFieldNumber = 67; private static readonly pbc::MapField.Codec _map_mapInt32Double_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForDouble(17), 538); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForDouble(17, 0D), 538); private readonly pbc::MapField mapInt32Double_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapInt32Double { @@ -990,7 +1008,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_bool_bool" field. public const int MapBoolBoolFieldNumber = 68; private static readonly pbc::MapField.Codec _map_mapBoolBool_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForBool(8), pb::FieldCodec.ForBool(16), 546); + = new pbc::MapField.Codec(pb::FieldCodec.ForBool(8, false), pb::FieldCodec.ForBool(16, false), 546); private readonly pbc::MapField mapBoolBool_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapBoolBool { @@ -1000,7 +1018,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_string_string" field. public const int MapStringStringFieldNumber = 69; private static readonly pbc::MapField.Codec _map_mapStringString_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForString(10), pb::FieldCodec.ForString(18), 554); + = new pbc::MapField.Codec(pb::FieldCodec.ForString(10, ""), pb::FieldCodec.ForString(18, ""), 554); private readonly pbc::MapField mapStringString_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapStringString { @@ -1010,7 +1028,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_string_bytes" field. public const int MapStringBytesFieldNumber = 70; private static readonly pbc::MapField.Codec _map_mapStringBytes_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForString(10), pb::FieldCodec.ForBytes(18), 562); + = new pbc::MapField.Codec(pb::FieldCodec.ForString(10, ""), pb::FieldCodec.ForBytes(18, pb::ByteString.Empty), 562); private readonly pbc::MapField mapStringBytes_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapStringBytes { @@ -1020,7 +1038,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_string_nested_message" field. public const int MapStringNestedMessageFieldNumber = 71; private static readonly pbc::MapField.Codec _map_mapStringNestedMessage_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForString(10), pb::FieldCodec.ForMessage(18, global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage.Parser), 570); + = new pbc::MapField.Codec(pb::FieldCodec.ForString(10, ""), pb::FieldCodec.ForMessage(18, global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage.Parser), 570); private readonly pbc::MapField mapStringNestedMessage_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapStringNestedMessage { @@ -1030,7 +1048,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_string_foreign_message" field. public const int MapStringForeignMessageFieldNumber = 72; private static readonly pbc::MapField.Codec _map_mapStringForeignMessage_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForString(10), pb::FieldCodec.ForMessage(18, global::ProtobufTestMessages.Proto3.ForeignMessage.Parser), 578); + = new pbc::MapField.Codec(pb::FieldCodec.ForString(10, ""), pb::FieldCodec.ForMessage(18, global::ProtobufTestMessages.Proto3.ForeignMessage.Parser), 578); private readonly pbc::MapField mapStringForeignMessage_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapStringForeignMessage { @@ -1040,7 +1058,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_string_nested_enum" field. public const int MapStringNestedEnumFieldNumber = 73; private static readonly pbc::MapField.Codec _map_mapStringNestedEnum_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForString(10), pb::FieldCodec.ForEnum(16, x => (int) x, x => (global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum) x), 586); + = new pbc::MapField.Codec(pb::FieldCodec.ForString(10, ""), pb::FieldCodec.ForEnum(16, x => (int) x, x => (global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum) x, global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum.Foo), 586); private readonly pbc::MapField mapStringNestedEnum_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapStringNestedEnum { @@ -1050,7 +1068,7 @@ namespace ProtobufTestMessages.Proto3 { /// Field number for the "map_string_foreign_enum" field. public const int MapStringForeignEnumFieldNumber = 74; private static readonly pbc::MapField.Codec _map_mapStringForeignEnum_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForString(10), pb::FieldCodec.ForEnum(16, x => (int) x, x => (global::ProtobufTestMessages.Proto3.ForeignEnum) x), 594); + = new pbc::MapField.Codec(pb::FieldCodec.ForString(10, ""), pb::FieldCodec.ForEnum(16, x => (int) x, x => (global::ProtobufTestMessages.Proto3.ForeignEnum) x, global::ProtobufTestMessages.Proto3.ForeignEnum.ForeignFoo), 594); private readonly pbc::MapField mapStringForeignEnum_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField MapStringForeignEnum { @@ -1149,7 +1167,7 @@ namespace ProtobufTestMessages.Proto3 { public const int OneofEnumFieldNumber = 119; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum OneofEnum { - get { return oneofFieldCase_ == OneofFieldOneofCase.OneofEnum ? (global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum) oneofField_ : 0; } + get { return oneofFieldCase_ == OneofFieldOneofCase.OneofEnum ? (global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum) oneofField_ : global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum.Foo; } set { oneofField_ = value; oneofFieldCase_ = OneofFieldOneofCase.OneofEnum; @@ -1171,6 +1189,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_int32_wrapper" field. public const int OptionalInt32WrapperFieldNumber = 202; private static readonly pb::FieldCodec _single_optionalInt32Wrapper_codec = pb::FieldCodec.ForStructWrapper(1618); @@ -1183,6 +1202,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_int64_wrapper" field. public const int OptionalInt64WrapperFieldNumber = 203; private static readonly pb::FieldCodec _single_optionalInt64Wrapper_codec = pb::FieldCodec.ForStructWrapper(1626); @@ -1195,6 +1215,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_uint32_wrapper" field. public const int OptionalUint32WrapperFieldNumber = 204; private static readonly pb::FieldCodec _single_optionalUint32Wrapper_codec = pb::FieldCodec.ForStructWrapper(1634); @@ -1207,6 +1228,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_uint64_wrapper" field. public const int OptionalUint64WrapperFieldNumber = 205; private static readonly pb::FieldCodec _single_optionalUint64Wrapper_codec = pb::FieldCodec.ForStructWrapper(1642); @@ -1219,6 +1241,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_float_wrapper" field. public const int OptionalFloatWrapperFieldNumber = 206; private static readonly pb::FieldCodec _single_optionalFloatWrapper_codec = pb::FieldCodec.ForStructWrapper(1650); @@ -1231,6 +1254,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_double_wrapper" field. public const int OptionalDoubleWrapperFieldNumber = 207; private static readonly pb::FieldCodec _single_optionalDoubleWrapper_codec = pb::FieldCodec.ForStructWrapper(1658); @@ -1243,6 +1267,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_string_wrapper" field. public const int OptionalStringWrapperFieldNumber = 208; private static readonly pb::FieldCodec _single_optionalStringWrapper_codec = pb::FieldCodec.ForClassWrapper(1666); @@ -1255,6 +1280,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "optional_bytes_wrapper" field. public const int OptionalBytesWrapperFieldNumber = 209; private static readonly pb::FieldCodec _single_optionalBytesWrapper_codec = pb::FieldCodec.ForClassWrapper(1674); @@ -1267,6 +1293,7 @@ namespace ProtobufTestMessages.Proto3 { } } + /// Field number for the "repeated_bool_wrapper" field. public const int RepeatedBoolWrapperFieldNumber = 211; private static readonly pb::FieldCodec _repeated_repeatedBoolWrapper_codec @@ -1483,6 +1510,16 @@ namespace ProtobufTestMessages.Proto3 { get { return repeatedValue_; } } + /// Field number for the "repeated_list_value" field. + public const int RepeatedListValueFieldNumber = 317; + private static readonly pb::FieldCodec _repeated_repeatedListValue_codec + = pb::FieldCodec.ForMessage(2538, global::Google.Protobuf.WellKnownTypes.ListValue.Parser); + private readonly pbc::RepeatedField repeatedListValue_ = new pbc::RepeatedField(); + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public pbc::RepeatedField RepeatedListValue { + get { return repeatedListValue_; } + } + /// Field number for the "fieldname1" field. public const int Fieldname1FieldNumber = 401; private int fieldname1_; @@ -1743,6 +1780,7 @@ namespace ProtobufTestMessages.Proto3 { if (!object.Equals(OptionalForeignMessage, other.OptionalForeignMessage)) return false; if (OptionalNestedEnum != other.OptionalNestedEnum) return false; if (OptionalForeignEnum != other.OptionalForeignEnum) return false; + if (OptionalAliasedEnum != other.OptionalAliasedEnum) return false; if (OptionalStringPiece != other.OptionalStringPiece) return false; if (OptionalCord != other.OptionalCord) return false; if (!object.Equals(RecursiveMessage, other.RecursiveMessage)) return false; @@ -1825,6 +1863,7 @@ namespace ProtobufTestMessages.Proto3 { if(!repeatedStruct_.Equals(other.repeatedStruct_)) return false; if(!repeatedAny_.Equals(other.repeatedAny_)) return false; if(!repeatedValue_.Equals(other.repeatedValue_)) return false; + if(!repeatedListValue_.Equals(other.repeatedListValue_)) return false; if (Fieldname1 != other.Fieldname1) return false; if (FieldName2 != other.FieldName2) return false; if (FieldName3 != other.FieldName3) return false; @@ -1867,8 +1906,9 @@ namespace ProtobufTestMessages.Proto3 { if (OptionalBytes.Length != 0) hash ^= OptionalBytes.GetHashCode(); if (optionalNestedMessage_ != null) hash ^= OptionalNestedMessage.GetHashCode(); if (optionalForeignMessage_ != null) hash ^= OptionalForeignMessage.GetHashCode(); - if (OptionalNestedEnum != 0) hash ^= OptionalNestedEnum.GetHashCode(); - if (OptionalForeignEnum != 0) hash ^= OptionalForeignEnum.GetHashCode(); + if (OptionalNestedEnum != global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum.Foo) hash ^= OptionalNestedEnum.GetHashCode(); + if (OptionalForeignEnum != global::ProtobufTestMessages.Proto3.ForeignEnum.ForeignFoo) hash ^= OptionalForeignEnum.GetHashCode(); + if (OptionalAliasedEnum != global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum.AliasFoo) hash ^= OptionalAliasedEnum.GetHashCode(); if (OptionalStringPiece.Length != 0) hash ^= OptionalStringPiece.GetHashCode(); if (OptionalCord.Length != 0) hash ^= OptionalCord.GetHashCode(); if (recursiveMessage_ != null) hash ^= RecursiveMessage.GetHashCode(); @@ -1951,6 +1991,7 @@ namespace ProtobufTestMessages.Proto3 { hash ^= repeatedStruct_.GetHashCode(); hash ^= repeatedAny_.GetHashCode(); hash ^= repeatedValue_.GetHashCode(); + hash ^= repeatedListValue_.GetHashCode(); if (Fieldname1 != 0) hash ^= Fieldname1.GetHashCode(); if (FieldName2 != 0) hash ^= FieldName2.GetHashCode(); if (FieldName3 != 0) hash ^= FieldName3.GetHashCode(); @@ -2051,14 +2092,18 @@ namespace ProtobufTestMessages.Proto3 { output.WriteRawTag(154, 1); output.WriteMessage(OptionalForeignMessage); } - if (OptionalNestedEnum != 0) { + if (OptionalNestedEnum != global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum.Foo) { output.WriteRawTag(168, 1); output.WriteEnum((int) OptionalNestedEnum); } - if (OptionalForeignEnum != 0) { + if (OptionalForeignEnum != global::ProtobufTestMessages.Proto3.ForeignEnum.ForeignFoo) { output.WriteRawTag(176, 1); output.WriteEnum((int) OptionalForeignEnum); } + if (OptionalAliasedEnum != global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum.AliasFoo) { + output.WriteRawTag(184, 1); + output.WriteEnum((int) OptionalAliasedEnum); + } if (OptionalStringPiece.Length != 0) { output.WriteRawTag(194, 1); output.WriteString(OptionalStringPiece); @@ -2212,6 +2257,7 @@ namespace ProtobufTestMessages.Proto3 { repeatedFieldmask_.WriteTo(output, _repeated_repeatedFieldmask_codec); repeatedAny_.WriteTo(output, _repeated_repeatedAny_codec); repeatedValue_.WriteTo(output, _repeated_repeatedValue_codec); + repeatedListValue_.WriteTo(output, _repeated_repeatedListValue_codec); repeatedStruct_.WriteTo(output, _repeated_repeatedStruct_codec); if (Fieldname1 != 0) { output.WriteRawTag(136, 25); @@ -2344,12 +2390,15 @@ namespace ProtobufTestMessages.Proto3 { if (optionalForeignMessage_ != null) { size += 2 + pb::CodedOutputStream.ComputeMessageSize(OptionalForeignMessage); } - if (OptionalNestedEnum != 0) { + if (OptionalNestedEnum != global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum.Foo) { size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) OptionalNestedEnum); } - if (OptionalForeignEnum != 0) { + if (OptionalForeignEnum != global::ProtobufTestMessages.Proto3.ForeignEnum.ForeignFoo) { size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) OptionalForeignEnum); } + if (OptionalAliasedEnum != global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum.AliasFoo) { + size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) OptionalAliasedEnum); + } if (OptionalStringPiece.Length != 0) { size += 2 + pb::CodedOutputStream.ComputeStringSize(OptionalStringPiece); } @@ -2486,6 +2535,7 @@ namespace ProtobufTestMessages.Proto3 { size += repeatedStruct_.CalculateSize(_repeated_repeatedStruct_codec); size += repeatedAny_.CalculateSize(_repeated_repeatedAny_codec); size += repeatedValue_.CalculateSize(_repeated_repeatedValue_codec); + size += repeatedListValue_.CalculateSize(_repeated_repeatedListValue_codec); if (Fieldname1 != 0) { size += 2 + pb::CodedOutputStream.ComputeInt32Size(Fieldname1); } @@ -2598,22 +2648,25 @@ namespace ProtobufTestMessages.Proto3 { } if (other.optionalNestedMessage_ != null) { if (optionalNestedMessage_ == null) { - optionalNestedMessage_ = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage(); + OptionalNestedMessage = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage(); } OptionalNestedMessage.MergeFrom(other.OptionalNestedMessage); } if (other.optionalForeignMessage_ != null) { if (optionalForeignMessage_ == null) { - optionalForeignMessage_ = new global::ProtobufTestMessages.Proto3.ForeignMessage(); + OptionalForeignMessage = new global::ProtobufTestMessages.Proto3.ForeignMessage(); } OptionalForeignMessage.MergeFrom(other.OptionalForeignMessage); } - if (other.OptionalNestedEnum != 0) { + if (other.OptionalNestedEnum != global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum.Foo) { OptionalNestedEnum = other.OptionalNestedEnum; } - if (other.OptionalForeignEnum != 0) { + if (other.OptionalForeignEnum != global::ProtobufTestMessages.Proto3.ForeignEnum.ForeignFoo) { OptionalForeignEnum = other.OptionalForeignEnum; } + if (other.OptionalAliasedEnum != global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum.AliasFoo) { + OptionalAliasedEnum = other.OptionalAliasedEnum; + } if (other.OptionalStringPiece.Length != 0) { OptionalStringPiece = other.OptionalStringPiece; } @@ -2622,7 +2675,7 @@ namespace ProtobufTestMessages.Proto3 { } if (other.recursiveMessage_ != null) { if (recursiveMessage_ == null) { - recursiveMessage_ = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3(); + RecursiveMessage = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3(); } RecursiveMessage.MergeFrom(other.RecursiveMessage); } @@ -2722,37 +2775,37 @@ namespace ProtobufTestMessages.Proto3 { repeatedBytesWrapper_.Add(other.repeatedBytesWrapper_); if (other.optionalDuration_ != null) { if (optionalDuration_ == null) { - optionalDuration_ = new global::Google.Protobuf.WellKnownTypes.Duration(); + OptionalDuration = new global::Google.Protobuf.WellKnownTypes.Duration(); } OptionalDuration.MergeFrom(other.OptionalDuration); } if (other.optionalTimestamp_ != null) { if (optionalTimestamp_ == null) { - optionalTimestamp_ = new global::Google.Protobuf.WellKnownTypes.Timestamp(); + OptionalTimestamp = new global::Google.Protobuf.WellKnownTypes.Timestamp(); } OptionalTimestamp.MergeFrom(other.OptionalTimestamp); } if (other.optionalFieldMask_ != null) { if (optionalFieldMask_ == null) { - optionalFieldMask_ = new global::Google.Protobuf.WellKnownTypes.FieldMask(); + OptionalFieldMask = new global::Google.Protobuf.WellKnownTypes.FieldMask(); } OptionalFieldMask.MergeFrom(other.OptionalFieldMask); } if (other.optionalStruct_ != null) { if (optionalStruct_ == null) { - optionalStruct_ = new global::Google.Protobuf.WellKnownTypes.Struct(); + OptionalStruct = new global::Google.Protobuf.WellKnownTypes.Struct(); } OptionalStruct.MergeFrom(other.OptionalStruct); } if (other.optionalAny_ != null) { if (optionalAny_ == null) { - optionalAny_ = new global::Google.Protobuf.WellKnownTypes.Any(); + OptionalAny = new global::Google.Protobuf.WellKnownTypes.Any(); } OptionalAny.MergeFrom(other.OptionalAny); } if (other.optionalValue_ != null) { if (optionalValue_ == null) { - optionalValue_ = new global::Google.Protobuf.WellKnownTypes.Value(); + OptionalValue = new global::Google.Protobuf.WellKnownTypes.Value(); } OptionalValue.MergeFrom(other.OptionalValue); } @@ -2762,6 +2815,7 @@ namespace ProtobufTestMessages.Proto3 { repeatedStruct_.Add(other.repeatedStruct_); repeatedAny_.Add(other.repeatedAny_); repeatedValue_.Add(other.repeatedValue_); + repeatedListValue_.Add(other.repeatedListValue_); if (other.Fieldname1 != 0) { Fieldname1 = other.Fieldname1; } @@ -2922,24 +2976,28 @@ namespace ProtobufTestMessages.Proto3 { } case 146: { if (optionalNestedMessage_ == null) { - optionalNestedMessage_ = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage(); + OptionalNestedMessage = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedMessage(); } - input.ReadMessage(optionalNestedMessage_); + input.ReadMessage(OptionalNestedMessage); break; } case 154: { if (optionalForeignMessage_ == null) { - optionalForeignMessage_ = new global::ProtobufTestMessages.Proto3.ForeignMessage(); + OptionalForeignMessage = new global::ProtobufTestMessages.Proto3.ForeignMessage(); } - input.ReadMessage(optionalForeignMessage_); + input.ReadMessage(OptionalForeignMessage); break; } case 168: { - optionalNestedEnum_ = (global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum) input.ReadEnum(); + OptionalNestedEnum = (global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.NestedEnum) input.ReadEnum(); break; } case 176: { - optionalForeignEnum_ = (global::ProtobufTestMessages.Proto3.ForeignEnum) input.ReadEnum(); + OptionalForeignEnum = (global::ProtobufTestMessages.Proto3.ForeignEnum) input.ReadEnum(); + break; + } + case 184: { + OptionalAliasedEnum = (global::ProtobufTestMessages.Proto3.TestAllTypesProto3.Types.AliasedEnum) input.ReadEnum(); break; } case 194: { @@ -2952,9 +3010,9 @@ namespace ProtobufTestMessages.Proto3 { } case 218: { if (recursiveMessage_ == null) { - recursiveMessage_ = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3(); + RecursiveMessage = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3(); } - input.ReadMessage(recursiveMessage_); + input.ReadMessage(RecursiveMessage); break; } case 250: @@ -3275,44 +3333,44 @@ namespace ProtobufTestMessages.Proto3 { } case 2410: { if (optionalDuration_ == null) { - optionalDuration_ = new global::Google.Protobuf.WellKnownTypes.Duration(); + OptionalDuration = new global::Google.Protobuf.WellKnownTypes.Duration(); } - input.ReadMessage(optionalDuration_); + input.ReadMessage(OptionalDuration); break; } case 2418: { if (optionalTimestamp_ == null) { - optionalTimestamp_ = new global::Google.Protobuf.WellKnownTypes.Timestamp(); + OptionalTimestamp = new global::Google.Protobuf.WellKnownTypes.Timestamp(); } - input.ReadMessage(optionalTimestamp_); + input.ReadMessage(OptionalTimestamp); break; } case 2426: { if (optionalFieldMask_ == null) { - optionalFieldMask_ = new global::Google.Protobuf.WellKnownTypes.FieldMask(); + OptionalFieldMask = new global::Google.Protobuf.WellKnownTypes.FieldMask(); } - input.ReadMessage(optionalFieldMask_); + input.ReadMessage(OptionalFieldMask); break; } case 2434: { if (optionalStruct_ == null) { - optionalStruct_ = new global::Google.Protobuf.WellKnownTypes.Struct(); + OptionalStruct = new global::Google.Protobuf.WellKnownTypes.Struct(); } - input.ReadMessage(optionalStruct_); + input.ReadMessage(OptionalStruct); break; } case 2442: { if (optionalAny_ == null) { - optionalAny_ = new global::Google.Protobuf.WellKnownTypes.Any(); + OptionalAny = new global::Google.Protobuf.WellKnownTypes.Any(); } - input.ReadMessage(optionalAny_); + input.ReadMessage(OptionalAny); break; } case 2450: { if (optionalValue_ == null) { - optionalValue_ = new global::Google.Protobuf.WellKnownTypes.Value(); + OptionalValue = new global::Google.Protobuf.WellKnownTypes.Value(); } - input.ReadMessage(optionalValue_); + input.ReadMessage(OptionalValue); break; } case 2490: { @@ -3335,6 +3393,10 @@ namespace ProtobufTestMessages.Proto3 { repeatedValue_.AddEntriesFrom(input, _repeated_repeatedValue_codec); break; } + case 2538: { + repeatedListValue_.AddEntriesFrom(input, _repeated_repeatedListValue_codec); + break; + } case 2594: { repeatedStruct_.AddEntriesFrom(input, _repeated_repeatedStruct_codec); break; @@ -3429,6 +3491,15 @@ namespace ProtobufTestMessages.Proto3 { [pbr::OriginalName("NEG")] Neg = -1, } + public enum AliasedEnum { + [pbr::OriginalName("ALIAS_FOO")] AliasFoo = 0, + [pbr::OriginalName("ALIAS_BAR")] AliasBar = 1, + [pbr::OriginalName("ALIAS_BAZ")] AliasBaz = 2, + [pbr::OriginalName("QUX", PreferredAlias = false)] Qux = 2, + [pbr::OriginalName("qux", PreferredAlias = false)] Qux_ = 2, + [pbr::OriginalName("bAz", PreferredAlias = false)] BAz = 2, + } + public sealed partial class NestedMessage : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new NestedMessage()); private pb::UnknownFieldSet _unknownFields; @@ -3560,7 +3631,7 @@ namespace ProtobufTestMessages.Proto3 { } if (other.corecursive_ != null) { if (corecursive_ == null) { - corecursive_ = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3(); + Corecursive = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3(); } Corecursive.MergeFrom(other.Corecursive); } @@ -3581,9 +3652,9 @@ namespace ProtobufTestMessages.Proto3 { } case 18: { if (corecursive_ == null) { - corecursive_ = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3(); + Corecursive = new global::ProtobufTestMessages.Proto3.TestAllTypesProto3(); } - input.ReadMessage(corecursive_); + input.ReadMessage(Corecursive); break; } } diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestCustomOptionsProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestCustomOptionsProto3.cs index 3ab5a48b84..6b19b5cce3 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestCustomOptionsProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestCustomOptionsProto3.cs @@ -138,32 +138,106 @@ namespace UnitTest.Issues.TestProtos { "Eg5GaWxlQW5ub3RhdGlvbhoWEhROZXN0ZWRGaWxlQW5ub3RhdGlvbmIGcHJv", "dG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, - new pbr::FileDescriptor[] { pbr::FileDescriptor.DescriptorProtoFileDescriptor, }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::UnitTest.Issues.TestProtos.MethodOpt1), typeof(global::UnitTest.Issues.TestProtos.AggregateEnum), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.TestMessageWithCustomOptions), global::UnitTest.Issues.TestProtos.TestMessageWithCustomOptions.Parser, new[]{ "Field1", "OneofField" }, new[]{ "AnOneof" }, new[]{ typeof(global::UnitTest.Issues.TestProtos.TestMessageWithCustomOptions.Types.AnEnum) }, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionFooRequest), global::UnitTest.Issues.TestProtos.CustomOptionFooRequest.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionFooResponse), global::UnitTest.Issues.TestProtos.CustomOptionFooResponse.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionFooClientMessage), global::UnitTest.Issues.TestProtos.CustomOptionFooClientMessage.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionFooServerMessage), global::UnitTest.Issues.TestProtos.CustomOptionFooServerMessage.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.DummyMessageContainingEnum), global::UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Parser, null, null, new[]{ typeof(global::UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types.TestEnumType) }, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.DummyMessageInvalidAsOptionType), global::UnitTest.Issues.TestProtos.DummyMessageInvalidAsOptionType.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionMinIntegerValues), global::UnitTest.Issues.TestProtos.CustomOptionMinIntegerValues.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionMaxIntegerValues), global::UnitTest.Issues.TestProtos.CustomOptionMaxIntegerValues.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionOtherValues), global::UnitTest.Issues.TestProtos.CustomOptionOtherValues.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.SettingRealsFromPositiveInts), global::UnitTest.Issues.TestProtos.SettingRealsFromPositiveInts.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.SettingRealsFromNegativeInts), global::UnitTest.Issues.TestProtos.SettingRealsFromNegativeInts.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ComplexOptionType1), global::UnitTest.Issues.TestProtos.ComplexOptionType1.Parser, new[]{ "Foo", "Foo2", "Foo3", "Foo4" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ComplexOptionType2), global::UnitTest.Issues.TestProtos.ComplexOptionType2.Parser, new[]{ "Bar", "Baz", "Fred", "Barney" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4), global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4.Parser, new[]{ "Waldo" }, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ComplexOptionType3), global::UnitTest.Issues.TestProtos.ComplexOptionType3.Parser, new[]{ "Qux" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.VariousComplexOptions), global::UnitTest.Issues.TestProtos.VariousComplexOptions.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.Aggregate), global::UnitTest.Issues.TestProtos.Aggregate.Parser, new[]{ "I", "S", "Sub" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.AggregateMessage), global::UnitTest.Issues.TestProtos.AggregateMessage.Parser, new[]{ "Fieldname" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.NestedOptionType), global::UnitTest.Issues.TestProtos.NestedOptionType.Parser, null, null, new[]{ typeof(global::UnitTest.Issues.TestProtos.NestedOptionType.Types.NestedEnum) }, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.NestedOptionType.Types.NestedMessage), global::UnitTest.Issues.TestProtos.NestedOptionType.Types.NestedMessage.Parser, new[]{ "NestedField" }, null, null, null)}) + new pbr::FileDescriptor[] { global::Google.Protobuf.Reflection.DescriptorReflection.Descriptor, }, + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::UnitTest.Issues.TestProtos.MethodOpt1), typeof(global::UnitTest.Issues.TestProtos.AggregateEnum), }, new pb::Extension[] { UnittestCustomOptionsProto3Extensions.FileOpt1, UnittestCustomOptionsProto3Extensions.MessageOpt1, UnittestCustomOptionsProto3Extensions.FieldOpt1, UnittestCustomOptionsProto3Extensions.OneofOpt1, UnittestCustomOptionsProto3Extensions.EnumOpt1, UnittestCustomOptionsProto3Extensions.EnumValueOpt1, UnittestCustomOptionsProto3Extensions.ServiceOpt1, UnittestCustomOptionsProto3Extensions.MethodOpt1, UnittestCustomOptionsProto3Extensions.BoolOpt, UnittestCustomOptionsProto3Extensions.Int32Opt, UnittestCustomOptionsProto3Extensions.Int64Opt, UnittestCustomOptionsProto3Extensions.Uint32Opt, UnittestCustomOptionsProto3Extensions.Uint64Opt, UnittestCustomOptionsProto3Extensions.Sint32Opt, UnittestCustomOptionsProto3Extensions.Sint64Opt, UnittestCustomOptionsProto3Extensions.Fixed32Opt, UnittestCustomOptionsProto3Extensions.Fixed64Opt, UnittestCustomOptionsProto3Extensions.Sfixed32Opt, UnittestCustomOptionsProto3Extensions.Sfixed64Opt, UnittestCustomOptionsProto3Extensions.FloatOpt, UnittestCustomOptionsProto3Extensions.DoubleOpt, UnittestCustomOptionsProto3Extensions.StringOpt, UnittestCustomOptionsProto3Extensions.BytesOpt, UnittestCustomOptionsProto3Extensions.EnumOpt, UnittestCustomOptionsProto3Extensions.MessageTypeOpt, UnittestCustomOptionsProto3Extensions.ComplexOpt1, UnittestCustomOptionsProto3Extensions.ComplexOpt2, UnittestCustomOptionsProto3Extensions.ComplexOpt3, UnittestCustomOptionsProto3Extensions.Fileopt, UnittestCustomOptionsProto3Extensions.Msgopt, UnittestCustomOptionsProto3Extensions.Fieldopt, UnittestCustomOptionsProto3Extensions.Enumopt, UnittestCustomOptionsProto3Extensions.Enumvalopt, UnittestCustomOptionsProto3Extensions.Serviceopt, UnittestCustomOptionsProto3Extensions.Methodopt }, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.TestMessageWithCustomOptions), global::UnitTest.Issues.TestProtos.TestMessageWithCustomOptions.Parser, new[]{ "Field1", "OneofField" }, new[]{ "AnOneof" }, new[]{ typeof(global::UnitTest.Issues.TestProtos.TestMessageWithCustomOptions.Types.AnEnum) }, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionFooRequest), global::UnitTest.Issues.TestProtos.CustomOptionFooRequest.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionFooResponse), global::UnitTest.Issues.TestProtos.CustomOptionFooResponse.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionFooClientMessage), global::UnitTest.Issues.TestProtos.CustomOptionFooClientMessage.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionFooServerMessage), global::UnitTest.Issues.TestProtos.CustomOptionFooServerMessage.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.DummyMessageContainingEnum), global::UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Parser, null, null, new[]{ typeof(global::UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types.TestEnumType) }, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.DummyMessageInvalidAsOptionType), global::UnitTest.Issues.TestProtos.DummyMessageInvalidAsOptionType.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionMinIntegerValues), global::UnitTest.Issues.TestProtos.CustomOptionMinIntegerValues.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionMaxIntegerValues), global::UnitTest.Issues.TestProtos.CustomOptionMaxIntegerValues.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.CustomOptionOtherValues), global::UnitTest.Issues.TestProtos.CustomOptionOtherValues.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.SettingRealsFromPositiveInts), global::UnitTest.Issues.TestProtos.SettingRealsFromPositiveInts.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.SettingRealsFromNegativeInts), global::UnitTest.Issues.TestProtos.SettingRealsFromNegativeInts.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ComplexOptionType1), global::UnitTest.Issues.TestProtos.ComplexOptionType1.Parser, new[]{ "Foo", "Foo2", "Foo3", "Foo4" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ComplexOptionType2), global::UnitTest.Issues.TestProtos.ComplexOptionType2.Parser, new[]{ "Bar", "Baz", "Fred", "Barney" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4), global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4.Parser, new[]{ "Waldo" }, null, null, new pb::Extension[] { global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4.Extensions.ComplexOpt4 }, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ComplexOptionType3), global::UnitTest.Issues.TestProtos.ComplexOptionType3.Parser, new[]{ "Qux" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.VariousComplexOptions), global::UnitTest.Issues.TestProtos.VariousComplexOptions.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.Aggregate), global::UnitTest.Issues.TestProtos.Aggregate.Parser, new[]{ "I", "S", "Sub" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.AggregateMessage), global::UnitTest.Issues.TestProtos.AggregateMessage.Parser, new[]{ "Fieldname" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.NestedOptionType), global::UnitTest.Issues.TestProtos.NestedOptionType.Parser, null, null, new[]{ typeof(global::UnitTest.Issues.TestProtos.NestedOptionType.Types.NestedEnum) }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.NestedOptionType.Types.NestedMessage), global::UnitTest.Issues.TestProtos.NestedOptionType.Types.NestedMessage.Parser, new[]{ "NestedField" }, null, null, null, null)}) })); } #endregion } + /// Holder for extension identifiers generated from the top level of unittest_custom_options_proto3.proto + internal static partial class UnittestCustomOptionsProto3Extensions { + public static readonly pb::Extension FileOpt1 = + new pb::Extension(7736974, pb::FieldCodec.ForUInt64(61895792, 0UL)); + public static readonly pb::Extension MessageOpt1 = + new pb::Extension(7739036, pb::FieldCodec.ForInt32(61912288, 0)); + public static readonly pb::Extension FieldOpt1 = + new pb::Extension(7740936, pb::FieldCodec.ForFixed64(61927489, 0UL)); + public static readonly pb::Extension OneofOpt1 = + new pb::Extension(7740111, pb::FieldCodec.ForInt32(61920888, 0)); + public static readonly pb::Extension EnumOpt1 = + new pb::Extension(7753576, pb::FieldCodec.ForSFixed32(62028613, 0)); + public static readonly pb::Extension EnumValueOpt1 = + new pb::Extension(1560678, pb::FieldCodec.ForInt32(12485424, 0)); + public static readonly pb::Extension ServiceOpt1 = + new pb::Extension(7887650, pb::FieldCodec.ForSInt64(63101200, 0L)); + public static readonly pb::Extension MethodOpt1 = + new pb::Extension(7890860, pb::FieldCodec.ForEnum(63126880, x => (int) x, x => (global::UnitTest.Issues.TestProtos.MethodOpt1) x, global::UnitTest.Issues.TestProtos.MethodOpt1.Unspecified)); + public static readonly pb::Extension BoolOpt = + new pb::Extension(7706090, pb::FieldCodec.ForBool(61648720, false)); + public static readonly pb::Extension Int32Opt = + new pb::Extension(7705709, pb::FieldCodec.ForInt32(61645672, 0)); + public static readonly pb::Extension Int64Opt = + new pb::Extension(7705542, pb::FieldCodec.ForInt64(61644336, 0L)); + public static readonly pb::Extension Uint32Opt = + new pb::Extension(7704880, pb::FieldCodec.ForUInt32(61639040, 0)); + public static readonly pb::Extension Uint64Opt = + new pb::Extension(7702367, pb::FieldCodec.ForUInt64(61618936, 0UL)); + public static readonly pb::Extension Sint32Opt = + new pb::Extension(7701568, pb::FieldCodec.ForSInt32(61612544, 0)); + public static readonly pb::Extension Sint64Opt = + new pb::Extension(7700863, pb::FieldCodec.ForSInt64(61606904, 0L)); + public static readonly pb::Extension Fixed32Opt = + new pb::Extension(7700307, pb::FieldCodec.ForFixed32(61602461, 0)); + public static readonly pb::Extension Fixed64Opt = + new pb::Extension(7700194, pb::FieldCodec.ForFixed64(61601553, 0UL)); + public static readonly pb::Extension Sfixed32Opt = + new pb::Extension(7698645, pb::FieldCodec.ForSFixed32(61589165, 0)); + public static readonly pb::Extension Sfixed64Opt = + new pb::Extension(7685475, pb::FieldCodec.ForSFixed64(61483801, 0L)); + public static readonly pb::Extension FloatOpt = + new pb::Extension(7675390, pb::FieldCodec.ForFloat(61403125, 0F)); + public static readonly pb::Extension DoubleOpt = + new pb::Extension(7673293, pb::FieldCodec.ForDouble(61386345, 0D)); + public static readonly pb::Extension StringOpt = + new pb::Extension(7673285, pb::FieldCodec.ForString(61386282, "")); + public static readonly pb::Extension BytesOpt = + new pb::Extension(7673238, pb::FieldCodec.ForBytes(61385906, pb::ByteString.Empty)); + public static readonly pb::Extension EnumOpt = + new pb::Extension(7673233, pb::FieldCodec.ForEnum(61385864, x => (int) x, x => (global::UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types.TestEnumType) x, global::UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types.TestEnumType.TestOptionEnumUnspecified)); + public static readonly pb::Extension MessageTypeOpt = + new pb::Extension(7665967, pb::FieldCodec.ForMessage(61327738, global::UnitTest.Issues.TestProtos.DummyMessageInvalidAsOptionType.Parser)); + public static readonly pb::Extension ComplexOpt1 = + new pb::Extension(7646756, pb::FieldCodec.ForMessage(61174050, global::UnitTest.Issues.TestProtos.ComplexOptionType1.Parser)); + public static readonly pb::Extension ComplexOpt2 = + new pb::Extension(7636949, pb::FieldCodec.ForMessage(61095594, global::UnitTest.Issues.TestProtos.ComplexOptionType2.Parser)); + public static readonly pb::Extension ComplexOpt3 = + new pb::Extension(7636463, pb::FieldCodec.ForMessage(61091706, global::UnitTest.Issues.TestProtos.ComplexOptionType3.Parser)); + public static readonly pb::Extension Fileopt = + new pb::Extension(15478479, pb::FieldCodec.ForMessage(123827834, global::UnitTest.Issues.TestProtos.Aggregate.Parser)); + public static readonly pb::Extension Msgopt = + new pb::Extension(15480088, pb::FieldCodec.ForMessage(123840706, global::UnitTest.Issues.TestProtos.Aggregate.Parser)); + public static readonly pb::Extension Fieldopt = + new pb::Extension(15481374, pb::FieldCodec.ForMessage(123850994, global::UnitTest.Issues.TestProtos.Aggregate.Parser)); + public static readonly pb::Extension Enumopt = + new pb::Extension(15483218, pb::FieldCodec.ForMessage(123865746, global::UnitTest.Issues.TestProtos.Aggregate.Parser)); + public static readonly pb::Extension Enumvalopt = + new pb::Extension(15486921, pb::FieldCodec.ForMessage(123895370, global::UnitTest.Issues.TestProtos.Aggregate.Parser)); + public static readonly pb::Extension Serviceopt = + new pb::Extension(15497145, pb::FieldCodec.ForMessage(123977162, global::UnitTest.Issues.TestProtos.Aggregate.Parser)); + public static readonly pb::Extension Methodopt = + new pb::Extension(15512713, pb::FieldCodec.ForMessage(124101706, global::UnitTest.Issues.TestProtos.Aggregate.Parser)); + } + #region Enums public enum MethodOpt1 { [pbr::OriginalName("METHODOPT1_UNSPECIFIED")] Unspecified = 0, @@ -1879,7 +1953,7 @@ namespace UnitTest.Issues.TestProtos { } if (other.bar_ != null) { if (bar_ == null) { - bar_ = new global::UnitTest.Issues.TestProtos.ComplexOptionType1(); + Bar = new global::UnitTest.Issues.TestProtos.ComplexOptionType1(); } Bar.MergeFrom(other.Bar); } @@ -1888,7 +1962,7 @@ namespace UnitTest.Issues.TestProtos { } if (other.fred_ != null) { if (fred_ == null) { - fred_ = new global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4(); + Fred = new global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4(); } Fred.MergeFrom(other.Fred); } @@ -1906,9 +1980,9 @@ namespace UnitTest.Issues.TestProtos { break; case 10: { if (bar_ == null) { - bar_ = new global::UnitTest.Issues.TestProtos.ComplexOptionType1(); + Bar = new global::UnitTest.Issues.TestProtos.ComplexOptionType1(); } - input.ReadMessage(bar_); + input.ReadMessage(Bar); break; } case 16: { @@ -1917,9 +1991,9 @@ namespace UnitTest.Issues.TestProtos { } case 26: { if (fred_ == null) { - fred_ = new global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4(); + Fred = new global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4(); } - input.ReadMessage(fred_); + input.ReadMessage(Fred); break; } case 34: { @@ -2061,6 +2135,15 @@ namespace UnitTest.Issues.TestProtos { } } + #region Extensions + /// Container for extensions for other messages declared in the ComplexOptionType4 message type. + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + internal static partial class Extensions { + public static readonly pb::Extension ComplexOpt4 = + new pb::Extension(7633546, pb::FieldCodec.ForMessage(61068370, global::UnitTest.Issues.TestProtos.ComplexOptionType2.Types.ComplexOptionType4.Parser)); + } + #endregion + } } @@ -2462,7 +2545,7 @@ namespace UnitTest.Issues.TestProtos { } if (other.sub_ != null) { if (sub_ == null) { - sub_ = new global::UnitTest.Issues.TestProtos.Aggregate(); + Sub = new global::UnitTest.Issues.TestProtos.Aggregate(); } Sub.MergeFrom(other.Sub); } @@ -2487,9 +2570,9 @@ namespace UnitTest.Issues.TestProtos { } case 26: { if (sub_ == null) { - sub_ = new global::UnitTest.Issues.TestProtos.Aggregate(); + Sub = new global::UnitTest.Issues.TestProtos.Aggregate(); } - input.ReadMessage(sub_); + input.ReadMessage(Sub); break; } } diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportProto3.cs index 6bf9715171..19a1a34634 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportProto3.cs @@ -32,8 +32,8 @@ namespace Google.Protobuf.TestProtos { "cm90b2J1Zi5UZXN0UHJvdG9zUABiBnByb3RvMw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { global::Google.Protobuf.TestProtos.UnittestImportPublicProto3Reflection.Descriptor, }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.TestProtos.ImportEnum), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.ImportMessage), global::Google.Protobuf.TestProtos.ImportMessage.Parser, new[]{ "D" }, null, null, null) + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.TestProtos.ImportEnum), }, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.ImportMessage), global::Google.Protobuf.TestProtos.ImportMessage.Parser, new[]{ "D" }, null, null, null, null) })); } #endregion diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportPublicProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportPublicProto3.cs index 97d181aff4..a455655469 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportPublicProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestImportPublicProto3.cs @@ -30,8 +30,8 @@ namespace Google.Protobuf.TestProtos { "Mw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.PublicImportMessage), global::Google.Protobuf.TestProtos.PublicImportMessage.Parser, new[]{ "E" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.PublicImportMessage), global::Google.Protobuf.TestProtos.PublicImportMessage.Parser, new[]{ "E" }, null, null, null, null) })); } #endregion diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs index 819fc2019f..f3f40c6a70 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestIssues.cs @@ -54,16 +54,16 @@ namespace UnitTest.Issues.TestProtos { "cy5UZXN0UHJvdG9zYgZwcm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::UnitTest.Issues.TestProtos.NegativeEnum), typeof(global::UnitTest.Issues.TestProtos.DeprecatedEnum), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.Issue307), global::UnitTest.Issues.TestProtos.Issue307.Parser, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce), global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce.Parser, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce.Types.NestedTwice), global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce.Types.NestedTwice.Parser, null, null, null, null)})}), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.NegativeEnumMessage), global::UnitTest.Issues.TestProtos.NegativeEnumMessage.Parser, new[]{ "Value", "Values", "PackedValues" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.DeprecatedChild), global::UnitTest.Issues.TestProtos.DeprecatedChild.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.DeprecatedFieldsMessage), global::UnitTest.Issues.TestProtos.DeprecatedFieldsMessage.Parser, new[]{ "PrimitiveValue", "PrimitiveArray", "MessageValue", "MessageArray", "EnumValue", "EnumArray" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ItemField), global::UnitTest.Issues.TestProtos.ItemField.Parser, new[]{ "Item" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ReservedNames), global::UnitTest.Issues.TestProtos.ReservedNames.Parser, new[]{ "Types_", "Descriptor_" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ReservedNames.Types.SomeNestedType), global::UnitTest.Issues.TestProtos.ReservedNames.Types.SomeNestedType.Parser, null, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.TestJsonFieldOrdering), global::UnitTest.Issues.TestProtos.TestJsonFieldOrdering.Parser, new[]{ "PlainInt32", "O1String", "O1Int32", "PlainString", "O2Int32", "O2String" }, new[]{ "O1", "O2" }, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.TestJsonName), global::UnitTest.Issues.TestProtos.TestJsonName.Parser, new[]{ "Name", "Description", "Guid" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.OneofMerging), global::UnitTest.Issues.TestProtos.OneofMerging.Parser, new[]{ "Text", "Nested" }, new[]{ "Value" }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.OneofMerging.Types.Nested), global::UnitTest.Issues.TestProtos.OneofMerging.Types.Nested.Parser, new[]{ "X", "Y" }, null, null, null)}) + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::UnitTest.Issues.TestProtos.NegativeEnum), typeof(global::UnitTest.Issues.TestProtos.DeprecatedEnum), }, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.Issue307), global::UnitTest.Issues.TestProtos.Issue307.Parser, null, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce), global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce.Parser, null, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce.Types.NestedTwice), global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce.Types.NestedTwice.Parser, null, null, null, null, null)})}), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.NegativeEnumMessage), global::UnitTest.Issues.TestProtos.NegativeEnumMessage.Parser, new[]{ "Value", "Values", "PackedValues" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.DeprecatedChild), global::UnitTest.Issues.TestProtos.DeprecatedChild.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.DeprecatedFieldsMessage), global::UnitTest.Issues.TestProtos.DeprecatedFieldsMessage.Parser, new[]{ "PrimitiveValue", "PrimitiveArray", "MessageValue", "MessageArray", "EnumValue", "EnumArray" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ItemField), global::UnitTest.Issues.TestProtos.ItemField.Parser, new[]{ "Item" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ReservedNames), global::UnitTest.Issues.TestProtos.ReservedNames.Parser, new[]{ "Types_", "Descriptor_" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.ReservedNames.Types.SomeNestedType), global::UnitTest.Issues.TestProtos.ReservedNames.Types.SomeNestedType.Parser, null, null, null, null, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.TestJsonFieldOrdering), global::UnitTest.Issues.TestProtos.TestJsonFieldOrdering.Parser, new[]{ "PlainInt32", "O1String", "O1Int32", "PlainString", "O2Int32", "O2String" }, new[]{ "O1", "O2" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.TestJsonName), global::UnitTest.Issues.TestProtos.TestJsonName.Parser, new[]{ "Name", "Description", "Guid" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.OneofMerging), global::UnitTest.Issues.TestProtos.OneofMerging.Parser, new[]{ "Text", "Nested" }, new[]{ "Value" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::UnitTest.Issues.TestProtos.OneofMerging.Types.Nested), global::UnitTest.Issues.TestProtos.OneofMerging.Types.Nested.Parser, new[]{ "X", "Y" }, null, null, null, null)}) })); } #endregion @@ -443,7 +443,7 @@ namespace UnitTest.Issues.TestProtos { /// Field number for the "value" field. public const int ValueFieldNumber = 1; - private global::UnitTest.Issues.TestProtos.NegativeEnum value_ = 0; + private global::UnitTest.Issues.TestProtos.NegativeEnum value_ = global::UnitTest.Issues.TestProtos.NegativeEnum.Zero; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::UnitTest.Issues.TestProtos.NegativeEnum Value { get { return value_; } @@ -494,7 +494,7 @@ namespace UnitTest.Issues.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Value != 0) hash ^= Value.GetHashCode(); + if (Value != global::UnitTest.Issues.TestProtos.NegativeEnum.Zero) hash ^= Value.GetHashCode(); hash ^= values_.GetHashCode(); hash ^= packedValues_.GetHashCode(); if (_unknownFields != null) { @@ -510,7 +510,7 @@ namespace UnitTest.Issues.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Value != 0) { + if (Value != global::UnitTest.Issues.TestProtos.NegativeEnum.Zero) { output.WriteRawTag(8); output.WriteEnum((int) Value); } @@ -524,7 +524,7 @@ namespace UnitTest.Issues.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Value != 0) { + if (Value != global::UnitTest.Issues.TestProtos.NegativeEnum.Zero) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Value); } size += values_.CalculateSize(_repeated_values_codec); @@ -540,7 +540,7 @@ namespace UnitTest.Issues.TestProtos { if (other == null) { return; } - if (other.Value != 0) { + if (other.Value != global::UnitTest.Issues.TestProtos.NegativeEnum.Zero) { Value = other.Value; } values_.Add(other.values_); @@ -557,7 +557,7 @@ namespace UnitTest.Issues.TestProtos { _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); break; case 8: { - value_ = (global::UnitTest.Issues.TestProtos.NegativeEnum) input.ReadEnum(); + Value = (global::UnitTest.Issues.TestProtos.NegativeEnum) input.ReadEnum(); break; } case 18: @@ -764,7 +764,7 @@ namespace UnitTest.Issues.TestProtos { /// Field number for the "EnumValue" field. public const int EnumValueFieldNumber = 5; - private global::UnitTest.Issues.TestProtos.DeprecatedEnum enumValue_ = 0; + private global::UnitTest.Issues.TestProtos.DeprecatedEnum enumValue_ = global::UnitTest.Issues.TestProtos.DeprecatedEnum.DeprecatedZero; [global::System.ObsoleteAttribute] [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::UnitTest.Issues.TestProtos.DeprecatedEnum EnumValue { @@ -814,7 +814,7 @@ namespace UnitTest.Issues.TestProtos { hash ^= primitiveArray_.GetHashCode(); if (messageValue_ != null) hash ^= MessageValue.GetHashCode(); hash ^= messageArray_.GetHashCode(); - if (EnumValue != 0) hash ^= EnumValue.GetHashCode(); + if (EnumValue != global::UnitTest.Issues.TestProtos.DeprecatedEnum.DeprecatedZero) hash ^= EnumValue.GetHashCode(); hash ^= enumArray_.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); @@ -839,7 +839,7 @@ namespace UnitTest.Issues.TestProtos { output.WriteMessage(MessageValue); } messageArray_.WriteTo(output, _repeated_messageArray_codec); - if (EnumValue != 0) { + if (EnumValue != global::UnitTest.Issues.TestProtos.DeprecatedEnum.DeprecatedZero) { output.WriteRawTag(40); output.WriteEnum((int) EnumValue); } @@ -860,7 +860,7 @@ namespace UnitTest.Issues.TestProtos { size += 1 + pb::CodedOutputStream.ComputeMessageSize(MessageValue); } size += messageArray_.CalculateSize(_repeated_messageArray_codec); - if (EnumValue != 0) { + if (EnumValue != global::UnitTest.Issues.TestProtos.DeprecatedEnum.DeprecatedZero) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) EnumValue); } size += enumArray_.CalculateSize(_repeated_enumArray_codec); @@ -881,12 +881,12 @@ namespace UnitTest.Issues.TestProtos { primitiveArray_.Add(other.primitiveArray_); if (other.messageValue_ != null) { if (messageValue_ == null) { - messageValue_ = new global::UnitTest.Issues.TestProtos.DeprecatedChild(); + MessageValue = new global::UnitTest.Issues.TestProtos.DeprecatedChild(); } MessageValue.MergeFrom(other.MessageValue); } messageArray_.Add(other.messageArray_); - if (other.EnumValue != 0) { + if (other.EnumValue != global::UnitTest.Issues.TestProtos.DeprecatedEnum.DeprecatedZero) { EnumValue = other.EnumValue; } enumArray_.Add(other.enumArray_); @@ -912,9 +912,9 @@ namespace UnitTest.Issues.TestProtos { } case 26: { if (messageValue_ == null) { - messageValue_ = new global::UnitTest.Issues.TestProtos.DeprecatedChild(); + MessageValue = new global::UnitTest.Issues.TestProtos.DeprecatedChild(); } - input.ReadMessage(messageValue_); + input.ReadMessage(MessageValue); break; } case 34: { @@ -922,7 +922,7 @@ namespace UnitTest.Issues.TestProtos { break; } case 40: { - enumValue_ = (global::UnitTest.Issues.TestProtos.DeprecatedEnum) input.ReadEnum(); + EnumValue = (global::UnitTest.Issues.TestProtos.DeprecatedEnum) input.ReadEnum(); break; } case 50: diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs index d5dbe866df..2f489ad743 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs @@ -139,58 +139,62 @@ namespace Google.Protobuf.TestProtos { "NBj//w8gAygEIigKG1Rlc3RDb21tZW50SW5qZWN0aW9uTWVzc2FnZRIJCgFh", "GAEgASgJIgwKCkZvb1JlcXVlc3QiDQoLRm9vUmVzcG9uc2UiEgoQRm9vQ2xp", "ZW50TWVzc2FnZSISChBGb29TZXJ2ZXJNZXNzYWdlIgwKCkJhclJlcXVlc3Qi", - "DQoLQmFyUmVzcG9uc2UiEgoQVGVzdEVtcHR5TWVzc2FnZSpZCgtGb3JlaWdu", - "RW51bRIXChNGT1JFSUdOX1VOU1BFQ0lGSUVEEAASDwoLRk9SRUlHTl9GT08Q", - "BBIPCgtGT1JFSUdOX0JBUhAFEg8KC0ZPUkVJR05fQkFaEAYqdQoUVGVzdEVu", - "dW1XaXRoRHVwVmFsdWUSKAokVEVTVF9FTlVNX1dJVEhfRFVQX1ZBTFVFX1VO", - "U1BFQ0lGSUVEEAASCAoERk9PMRABEggKBEJBUjEQAhIHCgNCQVoQAxIICgRG", - "T08yEAESCAoEQkFSMhACGgIQASqdAQoOVGVzdFNwYXJzZUVudW0SIAocVEVT", - "VF9TUEFSU0VfRU5VTV9VTlNQRUNJRklFRBAAEgwKCFNQQVJTRV9BEHsSDgoI", - "U1BBUlNFX0IQpucDEg8KCFNQQVJTRV9DELKxgAYSFQoIU1BBUlNFX0QQ8f//", - "////////ARIVCghTUEFSU0VfRRC03vz///////8BEgwKCFNQQVJTRV9HEAIy", - "nQEKC1Rlc3RTZXJ2aWNlEkYKA0ZvbxIeLnByb3RvYnVmX3VuaXR0ZXN0My5G", - "b29SZXF1ZXN0Gh8ucHJvdG9idWZfdW5pdHRlc3QzLkZvb1Jlc3BvbnNlEkYK", - "A0JhchIeLnByb3RvYnVmX3VuaXR0ZXN0My5CYXJSZXF1ZXN0Gh8ucHJvdG9i", - "dWZfdW5pdHRlc3QzLkJhclJlc3BvbnNlQixCDVVuaXR0ZXN0UHJvdG+qAhpH", - "b29nbGUuUHJvdG9idWYuVGVzdFByb3Rvc2IGcHJvdG8z")); + "DQoLQmFyUmVzcG9uc2UiEgoQVGVzdEVtcHR5TWVzc2FnZSJwCg5Db21tZW50", + "TWVzc2FnZRIMCgR0ZXh0GAEgASgJGisKFE5lc3RlZENvbW1lbnRNZXNzYWdl", + "EhMKC25lc3RlZF90ZXh0GAEgASgJIiMKEU5lc3RlZENvbW1lbnRFbnVtEg4K", + "ClpFUk9fVkFMVUUQACpZCgtGb3JlaWduRW51bRIXChNGT1JFSUdOX1VOU1BF", + "Q0lGSUVEEAASDwoLRk9SRUlHTl9GT08QBBIPCgtGT1JFSUdOX0JBUhAFEg8K", + "C0ZPUkVJR05fQkFaEAYqdQoUVGVzdEVudW1XaXRoRHVwVmFsdWUSKAokVEVT", + "VF9FTlVNX1dJVEhfRFVQX1ZBTFVFX1VOU1BFQ0lGSUVEEAASCAoERk9PMRAB", + "EggKBEJBUjEQAhIHCgNCQVoQAxIICgRGT08yEAESCAoEQkFSMhACGgIQASqd", + "AQoOVGVzdFNwYXJzZUVudW0SIAocVEVTVF9TUEFSU0VfRU5VTV9VTlNQRUNJ", + "RklFRBAAEgwKCFNQQVJTRV9BEHsSDgoIU1BBUlNFX0IQpucDEg8KCFNQQVJT", + "RV9DELKxgAYSFQoIU1BBUlNFX0QQ8f//////////ARIVCghTUEFSU0VfRRC0", + "3vz///////8BEgwKCFNQQVJTRV9HEAIqHQoLQ29tbWVudEVudW0SDgoKWkVS", + "T19WQUxVRRAAMp0BCgtUZXN0U2VydmljZRJGCgNGb28SHi5wcm90b2J1Zl91", + "bml0dGVzdDMuRm9vUmVxdWVzdBofLnByb3RvYnVmX3VuaXR0ZXN0My5Gb29S", + "ZXNwb25zZRJGCgNCYXISHi5wcm90b2J1Zl91bml0dGVzdDMuQmFyUmVxdWVz", + "dBofLnByb3RvYnVmX3VuaXR0ZXN0My5CYXJSZXNwb25zZUIsQg1Vbml0dGVz", + "dFByb3RvqgIaR29vZ2xlLlByb3RvYnVmLlRlc3RQcm90b3NiBnByb3RvMw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { global::Google.Protobuf.TestProtos.UnittestImportProto3Reflection.Descriptor, }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.TestProtos.ForeignEnum), typeof(global::Google.Protobuf.TestProtos.TestEnumWithDupValue), typeof(global::Google.Protobuf.TestProtos.TestSparseEnum), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestAllTypes), global::Google.Protobuf.TestProtos.TestAllTypes.Parser, new[]{ "SingleInt32", "SingleInt64", "SingleUint32", "SingleUint64", "SingleSint32", "SingleSint64", "SingleFixed32", "SingleFixed64", "SingleSfixed32", "SingleSfixed64", "SingleFloat", "SingleDouble", "SingleBool", "SingleString", "SingleBytes", "SingleNestedMessage", "SingleForeignMessage", "SingleImportMessage", "SingleNestedEnum", "SingleForeignEnum", "SingleImportEnum", "SinglePublicImportMessage", "RepeatedInt32", "RepeatedInt64", "RepeatedUint32", "RepeatedUint64", "RepeatedSint32", "RepeatedSint64", "RepeatedFixed32", "RepeatedFixed64", "RepeatedSfixed32", "RepeatedSfixed64", "RepeatedFloat", "RepeatedDouble", "RepeatedBool", "RepeatedString", "RepeatedBytes", "RepeatedNestedMessage", "RepeatedForeignMessage", "RepeatedImportMessage", "RepeatedNestedEnum", "RepeatedForeignEnum", "RepeatedImportEnum", "RepeatedPublicImportMessage", "OneofUint32", "OneofNestedMessage", "OneofString", "OneofBytes" }, new[]{ "OneofField" }, new[]{ typeof(global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum) }, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage), global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage.Parser, new[]{ "Bb" }, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.NestedTestAllTypes), global::Google.Protobuf.TestProtos.NestedTestAllTypes.Parser, new[]{ "Child", "Payload", "RepeatedChild" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestDeprecatedFields), global::Google.Protobuf.TestProtos.TestDeprecatedFields.Parser, new[]{ "DeprecatedInt32" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.ForeignMessage), global::Google.Protobuf.TestProtos.ForeignMessage.Parser, new[]{ "C" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestReservedFields), global::Google.Protobuf.TestProtos.TestReservedFields.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestForeignNested), global::Google.Protobuf.TestProtos.TestForeignNested.Parser, new[]{ "ForeignNested" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestReallyLargeTagNumber), global::Google.Protobuf.TestProtos.TestReallyLargeTagNumber.Parser, new[]{ "A", "Bb" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestRecursiveMessage), global::Google.Protobuf.TestProtos.TestRecursiveMessage.Parser, new[]{ "A", "I" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMutualRecursionA), global::Google.Protobuf.TestProtos.TestMutualRecursionA.Parser, new[]{ "Bb" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMutualRecursionB), global::Google.Protobuf.TestProtos.TestMutualRecursionB.Parser, new[]{ "A", "OptionalInt32" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestEnumAllowAlias), global::Google.Protobuf.TestProtos.TestEnumAllowAlias.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestCamelCaseFieldNames), global::Google.Protobuf.TestProtos.TestCamelCaseFieldNames.Parser, new[]{ "PrimitiveField", "StringField", "EnumField", "MessageField", "RepeatedPrimitiveField", "RepeatedStringField", "RepeatedEnumField", "RepeatedMessageField" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestFieldOrderings), global::Google.Protobuf.TestProtos.TestFieldOrderings.Parser, new[]{ "MyString", "MyInt", "MyFloat", "SingleNestedMessage" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage), global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage.Parser, new[]{ "Oo", "Bb" }, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.SparseEnumMessage), global::Google.Protobuf.TestProtos.SparseEnumMessage.Parser, new[]{ "SparseEnum" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.OneString), global::Google.Protobuf.TestProtos.OneString.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MoreString), global::Google.Protobuf.TestProtos.MoreString.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.OneBytes), global::Google.Protobuf.TestProtos.OneBytes.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MoreBytes), global::Google.Protobuf.TestProtos.MoreBytes.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.Int32Message), global::Google.Protobuf.TestProtos.Int32Message.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.Uint32Message), global::Google.Protobuf.TestProtos.Uint32Message.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.Int64Message), global::Google.Protobuf.TestProtos.Int64Message.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.Uint64Message), global::Google.Protobuf.TestProtos.Uint64Message.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.BoolMessage), global::Google.Protobuf.TestProtos.BoolMessage.Parser, new[]{ "Data" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestOneof), global::Google.Protobuf.TestProtos.TestOneof.Parser, new[]{ "FooInt", "FooString", "FooMessage" }, new[]{ "Foo" }, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestPackedTypes), global::Google.Protobuf.TestProtos.TestPackedTypes.Parser, new[]{ "PackedInt32", "PackedInt64", "PackedUint32", "PackedUint64", "PackedSint32", "PackedSint64", "PackedFixed32", "PackedFixed64", "PackedSfixed32", "PackedSfixed64", "PackedFloat", "PackedDouble", "PackedBool", "PackedEnum" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestUnpackedTypes), global::Google.Protobuf.TestProtos.TestUnpackedTypes.Parser, new[]{ "UnpackedInt32", "UnpackedInt64", "UnpackedUint32", "UnpackedUint64", "UnpackedSint32", "UnpackedSint64", "UnpackedFixed32", "UnpackedFixed64", "UnpackedSfixed32", "UnpackedSfixed64", "UnpackedFloat", "UnpackedDouble", "UnpackedBool", "UnpackedEnum" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestRepeatedScalarDifferentTagSizes), global::Google.Protobuf.TestProtos.TestRepeatedScalarDifferentTagSizes.Parser, new[]{ "RepeatedFixed32", "RepeatedInt32", "RepeatedFixed64", "RepeatedInt64", "RepeatedFloat", "RepeatedUint64" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestCommentInjectionMessage), global::Google.Protobuf.TestProtos.TestCommentInjectionMessage.Parser, new[]{ "A" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.FooRequest), global::Google.Protobuf.TestProtos.FooRequest.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.FooResponse), global::Google.Protobuf.TestProtos.FooResponse.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.FooClientMessage), global::Google.Protobuf.TestProtos.FooClientMessage.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.FooServerMessage), global::Google.Protobuf.TestProtos.FooServerMessage.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.BarRequest), global::Google.Protobuf.TestProtos.BarRequest.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.BarResponse), global::Google.Protobuf.TestProtos.BarResponse.Parser, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestEmptyMessage), global::Google.Protobuf.TestProtos.TestEmptyMessage.Parser, null, null, null, null) + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.TestProtos.ForeignEnum), typeof(global::Google.Protobuf.TestProtos.TestEnumWithDupValue), typeof(global::Google.Protobuf.TestProtos.TestSparseEnum), typeof(global::Google.Protobuf.TestProtos.CommentEnum), }, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestAllTypes), global::Google.Protobuf.TestProtos.TestAllTypes.Parser, new[]{ "SingleInt32", "SingleInt64", "SingleUint32", "SingleUint64", "SingleSint32", "SingleSint64", "SingleFixed32", "SingleFixed64", "SingleSfixed32", "SingleSfixed64", "SingleFloat", "SingleDouble", "SingleBool", "SingleString", "SingleBytes", "SingleNestedMessage", "SingleForeignMessage", "SingleImportMessage", "SingleNestedEnum", "SingleForeignEnum", "SingleImportEnum", "SinglePublicImportMessage", "RepeatedInt32", "RepeatedInt64", "RepeatedUint32", "RepeatedUint64", "RepeatedSint32", "RepeatedSint64", "RepeatedFixed32", "RepeatedFixed64", "RepeatedSfixed32", "RepeatedSfixed64", "RepeatedFloat", "RepeatedDouble", "RepeatedBool", "RepeatedString", "RepeatedBytes", "RepeatedNestedMessage", "RepeatedForeignMessage", "RepeatedImportMessage", "RepeatedNestedEnum", "RepeatedForeignEnum", "RepeatedImportEnum", "RepeatedPublicImportMessage", "OneofUint32", "OneofNestedMessage", "OneofString", "OneofBytes" }, new[]{ "OneofField" }, new[]{ typeof(global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum) }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage), global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage.Parser, new[]{ "Bb" }, null, null, null, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.NestedTestAllTypes), global::Google.Protobuf.TestProtos.NestedTestAllTypes.Parser, new[]{ "Child", "Payload", "RepeatedChild" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestDeprecatedFields), global::Google.Protobuf.TestProtos.TestDeprecatedFields.Parser, new[]{ "DeprecatedInt32" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.ForeignMessage), global::Google.Protobuf.TestProtos.ForeignMessage.Parser, new[]{ "C" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestReservedFields), global::Google.Protobuf.TestProtos.TestReservedFields.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestForeignNested), global::Google.Protobuf.TestProtos.TestForeignNested.Parser, new[]{ "ForeignNested" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestReallyLargeTagNumber), global::Google.Protobuf.TestProtos.TestReallyLargeTagNumber.Parser, new[]{ "A", "Bb" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestRecursiveMessage), global::Google.Protobuf.TestProtos.TestRecursiveMessage.Parser, new[]{ "A", "I" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMutualRecursionA), global::Google.Protobuf.TestProtos.TestMutualRecursionA.Parser, new[]{ "Bb" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestMutualRecursionB), global::Google.Protobuf.TestProtos.TestMutualRecursionB.Parser, new[]{ "A", "OptionalInt32" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestEnumAllowAlias), global::Google.Protobuf.TestProtos.TestEnumAllowAlias.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestCamelCaseFieldNames), global::Google.Protobuf.TestProtos.TestCamelCaseFieldNames.Parser, new[]{ "PrimitiveField", "StringField", "EnumField", "MessageField", "RepeatedPrimitiveField", "RepeatedStringField", "RepeatedEnumField", "RepeatedMessageField" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestFieldOrderings), global::Google.Protobuf.TestProtos.TestFieldOrderings.Parser, new[]{ "MyString", "MyInt", "MyFloat", "SingleNestedMessage" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage), global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage.Parser, new[]{ "Oo", "Bb" }, null, null, null, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.SparseEnumMessage), global::Google.Protobuf.TestProtos.SparseEnumMessage.Parser, new[]{ "SparseEnum" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.OneString), global::Google.Protobuf.TestProtos.OneString.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MoreString), global::Google.Protobuf.TestProtos.MoreString.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.OneBytes), global::Google.Protobuf.TestProtos.OneBytes.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MoreBytes), global::Google.Protobuf.TestProtos.MoreBytes.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.Int32Message), global::Google.Protobuf.TestProtos.Int32Message.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.Uint32Message), global::Google.Protobuf.TestProtos.Uint32Message.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.Int64Message), global::Google.Protobuf.TestProtos.Int64Message.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.Uint64Message), global::Google.Protobuf.TestProtos.Uint64Message.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.BoolMessage), global::Google.Protobuf.TestProtos.BoolMessage.Parser, new[]{ "Data" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestOneof), global::Google.Protobuf.TestProtos.TestOneof.Parser, new[]{ "FooInt", "FooString", "FooMessage" }, new[]{ "Foo" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestPackedTypes), global::Google.Protobuf.TestProtos.TestPackedTypes.Parser, new[]{ "PackedInt32", "PackedInt64", "PackedUint32", "PackedUint64", "PackedSint32", "PackedSint64", "PackedFixed32", "PackedFixed64", "PackedSfixed32", "PackedSfixed64", "PackedFloat", "PackedDouble", "PackedBool", "PackedEnum" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestUnpackedTypes), global::Google.Protobuf.TestProtos.TestUnpackedTypes.Parser, new[]{ "UnpackedInt32", "UnpackedInt64", "UnpackedUint32", "UnpackedUint64", "UnpackedSint32", "UnpackedSint64", "UnpackedFixed32", "UnpackedFixed64", "UnpackedSfixed32", "UnpackedSfixed64", "UnpackedFloat", "UnpackedDouble", "UnpackedBool", "UnpackedEnum" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestRepeatedScalarDifferentTagSizes), global::Google.Protobuf.TestProtos.TestRepeatedScalarDifferentTagSizes.Parser, new[]{ "RepeatedFixed32", "RepeatedInt32", "RepeatedFixed64", "RepeatedInt64", "RepeatedFloat", "RepeatedUint64" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestCommentInjectionMessage), global::Google.Protobuf.TestProtos.TestCommentInjectionMessage.Parser, new[]{ "A" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.FooRequest), global::Google.Protobuf.TestProtos.FooRequest.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.FooResponse), global::Google.Protobuf.TestProtos.FooResponse.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.FooClientMessage), global::Google.Protobuf.TestProtos.FooClientMessage.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.FooServerMessage), global::Google.Protobuf.TestProtos.FooServerMessage.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.BarRequest), global::Google.Protobuf.TestProtos.BarRequest.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.BarResponse), global::Google.Protobuf.TestProtos.BarResponse.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestEmptyMessage), global::Google.Protobuf.TestProtos.TestEmptyMessage.Parser, null, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.CommentMessage), global::Google.Protobuf.TestProtos.CommentMessage.Parser, new[]{ "Text" }, null, new[]{ typeof(global::Google.Protobuf.TestProtos.CommentMessage.Types.NestedCommentEnum) }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.CommentMessage.Types.NestedCommentMessage), global::Google.Protobuf.TestProtos.CommentMessage.Types.NestedCommentMessage.Parser, new[]{ "NestedText" }, null, null, null, null)}) })); } #endregion @@ -233,6 +237,16 @@ namespace Google.Protobuf.TestProtos { [pbr::OriginalName("SPARSE_G")] SparseG = 2, } + /// + /// Leading enum comment + /// + public enum CommentEnum { + /// + /// Zero value comment + /// + [pbr::OriginalName("ZERO_VALUE")] ZeroValue = 0, + } + #endregion #region Messages @@ -535,7 +549,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "single_nested_enum" field. public const int SingleNestedEnumFieldNumber = 21; - private global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum singleNestedEnum_ = 0; + private global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum singleNestedEnum_ = global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum.Unspecified; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum SingleNestedEnum { get { return singleNestedEnum_; } @@ -546,7 +560,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "single_foreign_enum" field. public const int SingleForeignEnumFieldNumber = 22; - private global::Google.Protobuf.TestProtos.ForeignEnum singleForeignEnum_ = 0; + private global::Google.Protobuf.TestProtos.ForeignEnum singleForeignEnum_ = global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.TestProtos.ForeignEnum SingleForeignEnum { get { return singleForeignEnum_; } @@ -557,7 +571,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "single_import_enum" field. public const int SingleImportEnumFieldNumber = 23; - private global::Google.Protobuf.TestProtos.ImportEnum singleImportEnum_ = 0; + private global::Google.Protobuf.TestProtos.ImportEnum singleImportEnum_ = global::Google.Protobuf.TestProtos.ImportEnum.Unspecified; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.TestProtos.ImportEnum SingleImportEnum { get { return singleImportEnum_; } @@ -957,9 +971,9 @@ namespace Google.Protobuf.TestProtos { if (singleNestedMessage_ != null) hash ^= SingleNestedMessage.GetHashCode(); if (singleForeignMessage_ != null) hash ^= SingleForeignMessage.GetHashCode(); if (singleImportMessage_ != null) hash ^= SingleImportMessage.GetHashCode(); - if (SingleNestedEnum != 0) hash ^= SingleNestedEnum.GetHashCode(); - if (SingleForeignEnum != 0) hash ^= SingleForeignEnum.GetHashCode(); - if (SingleImportEnum != 0) hash ^= SingleImportEnum.GetHashCode(); + if (SingleNestedEnum != global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum.Unspecified) hash ^= SingleNestedEnum.GetHashCode(); + if (SingleForeignEnum != global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified) hash ^= SingleForeignEnum.GetHashCode(); + if (SingleImportEnum != global::Google.Protobuf.TestProtos.ImportEnum.Unspecified) hash ^= SingleImportEnum.GetHashCode(); if (singlePublicImportMessage_ != null) hash ^= SinglePublicImportMessage.GetHashCode(); hash ^= repeatedInt32_.GetHashCode(); hash ^= repeatedInt64_.GetHashCode(); @@ -1073,15 +1087,15 @@ namespace Google.Protobuf.TestProtos { output.WriteRawTag(162, 1); output.WriteMessage(SingleImportMessage); } - if (SingleNestedEnum != 0) { + if (SingleNestedEnum != global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum.Unspecified) { output.WriteRawTag(168, 1); output.WriteEnum((int) SingleNestedEnum); } - if (SingleForeignEnum != 0) { + if (SingleForeignEnum != global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified) { output.WriteRawTag(176, 1); output.WriteEnum((int) SingleForeignEnum); } - if (SingleImportEnum != 0) { + if (SingleImportEnum != global::Google.Protobuf.TestProtos.ImportEnum.Unspecified) { output.WriteRawTag(184, 1); output.WriteEnum((int) SingleImportEnum); } @@ -1189,13 +1203,13 @@ namespace Google.Protobuf.TestProtos { if (singleImportMessage_ != null) { size += 2 + pb::CodedOutputStream.ComputeMessageSize(SingleImportMessage); } - if (SingleNestedEnum != 0) { + if (SingleNestedEnum != global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum.Unspecified) { size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) SingleNestedEnum); } - if (SingleForeignEnum != 0) { + if (SingleForeignEnum != global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified) { size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) SingleForeignEnum); } - if (SingleImportEnum != 0) { + if (SingleImportEnum != global::Google.Protobuf.TestProtos.ImportEnum.Unspecified) { size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) SingleImportEnum); } if (singlePublicImportMessage_ != null) { @@ -1293,34 +1307,34 @@ namespace Google.Protobuf.TestProtos { } if (other.singleNestedMessage_ != null) { if (singleNestedMessage_ == null) { - singleNestedMessage_ = new global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage(); + SingleNestedMessage = new global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage(); } SingleNestedMessage.MergeFrom(other.SingleNestedMessage); } if (other.singleForeignMessage_ != null) { if (singleForeignMessage_ == null) { - singleForeignMessage_ = new global::Google.Protobuf.TestProtos.ForeignMessage(); + SingleForeignMessage = new global::Google.Protobuf.TestProtos.ForeignMessage(); } SingleForeignMessage.MergeFrom(other.SingleForeignMessage); } if (other.singleImportMessage_ != null) { if (singleImportMessage_ == null) { - singleImportMessage_ = new global::Google.Protobuf.TestProtos.ImportMessage(); + SingleImportMessage = new global::Google.Protobuf.TestProtos.ImportMessage(); } SingleImportMessage.MergeFrom(other.SingleImportMessage); } - if (other.SingleNestedEnum != 0) { + if (other.SingleNestedEnum != global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum.Unspecified) { SingleNestedEnum = other.SingleNestedEnum; } - if (other.SingleForeignEnum != 0) { + if (other.SingleForeignEnum != global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified) { SingleForeignEnum = other.SingleForeignEnum; } - if (other.SingleImportEnum != 0) { + if (other.SingleImportEnum != global::Google.Protobuf.TestProtos.ImportEnum.Unspecified) { SingleImportEnum = other.SingleImportEnum; } if (other.singlePublicImportMessage_ != null) { if (singlePublicImportMessage_ == null) { - singlePublicImportMessage_ = new global::Google.Protobuf.TestProtos.PublicImportMessage(); + SinglePublicImportMessage = new global::Google.Protobuf.TestProtos.PublicImportMessage(); } SinglePublicImportMessage.MergeFrom(other.SinglePublicImportMessage); } @@ -1437,42 +1451,42 @@ namespace Google.Protobuf.TestProtos { } case 146: { if (singleNestedMessage_ == null) { - singleNestedMessage_ = new global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage(); + SingleNestedMessage = new global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage(); } - input.ReadMessage(singleNestedMessage_); + input.ReadMessage(SingleNestedMessage); break; } case 154: { if (singleForeignMessage_ == null) { - singleForeignMessage_ = new global::Google.Protobuf.TestProtos.ForeignMessage(); + SingleForeignMessage = new global::Google.Protobuf.TestProtos.ForeignMessage(); } - input.ReadMessage(singleForeignMessage_); + input.ReadMessage(SingleForeignMessage); break; } case 162: { if (singleImportMessage_ == null) { - singleImportMessage_ = new global::Google.Protobuf.TestProtos.ImportMessage(); + SingleImportMessage = new global::Google.Protobuf.TestProtos.ImportMessage(); } - input.ReadMessage(singleImportMessage_); + input.ReadMessage(SingleImportMessage); break; } case 168: { - singleNestedEnum_ = (global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum) input.ReadEnum(); + SingleNestedEnum = (global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedEnum) input.ReadEnum(); break; } case 176: { - singleForeignEnum_ = (global::Google.Protobuf.TestProtos.ForeignEnum) input.ReadEnum(); + SingleForeignEnum = (global::Google.Protobuf.TestProtos.ForeignEnum) input.ReadEnum(); break; } case 184: { - singleImportEnum_ = (global::Google.Protobuf.TestProtos.ImportEnum) input.ReadEnum(); + SingleImportEnum = (global::Google.Protobuf.TestProtos.ImportEnum) input.ReadEnum(); break; } case 210: { if (singlePublicImportMessage_ == null) { - singlePublicImportMessage_ = new global::Google.Protobuf.TestProtos.PublicImportMessage(); + SinglePublicImportMessage = new global::Google.Protobuf.TestProtos.PublicImportMessage(); } - input.ReadMessage(singlePublicImportMessage_); + input.ReadMessage(SinglePublicImportMessage); break; } case 250: @@ -1904,13 +1918,13 @@ namespace Google.Protobuf.TestProtos { } if (other.child_ != null) { if (child_ == null) { - child_ = new global::Google.Protobuf.TestProtos.NestedTestAllTypes(); + Child = new global::Google.Protobuf.TestProtos.NestedTestAllTypes(); } Child.MergeFrom(other.Child); } if (other.payload_ != null) { if (payload_ == null) { - payload_ = new global::Google.Protobuf.TestProtos.TestAllTypes(); + Payload = new global::Google.Protobuf.TestProtos.TestAllTypes(); } Payload.MergeFrom(other.Payload); } @@ -1928,16 +1942,16 @@ namespace Google.Protobuf.TestProtos { break; case 10: { if (child_ == null) { - child_ = new global::Google.Protobuf.TestProtos.NestedTestAllTypes(); + Child = new global::Google.Protobuf.TestProtos.NestedTestAllTypes(); } - input.ReadMessage(child_); + input.ReadMessage(Child); break; } case 18: { if (payload_ == null) { - payload_ = new global::Google.Protobuf.TestProtos.TestAllTypes(); + Payload = new global::Google.Protobuf.TestProtos.TestAllTypes(); } - input.ReadMessage(payload_); + input.ReadMessage(Payload); break; } case 26: { @@ -2424,7 +2438,7 @@ namespace Google.Protobuf.TestProtos { } if (other.foreignNested_ != null) { if (foreignNested_ == null) { - foreignNested_ = new global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage(); + ForeignNested = new global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage(); } ForeignNested.MergeFrom(other.ForeignNested); } @@ -2441,9 +2455,9 @@ namespace Google.Protobuf.TestProtos { break; case 10: { if (foreignNested_ == null) { - foreignNested_ = new global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage(); + ForeignNested = new global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage(); } - input.ReadMessage(foreignNested_); + input.ReadMessage(ForeignNested); break; } } @@ -2744,7 +2758,7 @@ namespace Google.Protobuf.TestProtos { } if (other.a_ != null) { if (a_ == null) { - a_ = new global::Google.Protobuf.TestProtos.TestRecursiveMessage(); + A = new global::Google.Protobuf.TestProtos.TestRecursiveMessage(); } A.MergeFrom(other.A); } @@ -2764,9 +2778,9 @@ namespace Google.Protobuf.TestProtos { break; case 10: { if (a_ == null) { - a_ = new global::Google.Protobuf.TestProtos.TestRecursiveMessage(); + A = new global::Google.Protobuf.TestProtos.TestRecursiveMessage(); } - input.ReadMessage(a_); + input.ReadMessage(A); break; } case 16: { @@ -2889,7 +2903,7 @@ namespace Google.Protobuf.TestProtos { } if (other.bb_ != null) { if (bb_ == null) { - bb_ = new global::Google.Protobuf.TestProtos.TestMutualRecursionB(); + Bb = new global::Google.Protobuf.TestProtos.TestMutualRecursionB(); } Bb.MergeFrom(other.Bb); } @@ -2906,9 +2920,9 @@ namespace Google.Protobuf.TestProtos { break; case 10: { if (bb_ == null) { - bb_ = new global::Google.Protobuf.TestProtos.TestMutualRecursionB(); + Bb = new global::Google.Protobuf.TestProtos.TestMutualRecursionB(); } - input.ReadMessage(bb_); + input.ReadMessage(Bb); break; } } @@ -3045,7 +3059,7 @@ namespace Google.Protobuf.TestProtos { } if (other.a_ != null) { if (a_ == null) { - a_ = new global::Google.Protobuf.TestProtos.TestMutualRecursionA(); + A = new global::Google.Protobuf.TestProtos.TestMutualRecursionA(); } A.MergeFrom(other.A); } @@ -3065,9 +3079,9 @@ namespace Google.Protobuf.TestProtos { break; case 10: { if (a_ == null) { - a_ = new global::Google.Protobuf.TestProtos.TestMutualRecursionA(); + A = new global::Google.Protobuf.TestProtos.TestMutualRecursionA(); } - input.ReadMessage(a_); + input.ReadMessage(A); break; } case 16: { @@ -3116,7 +3130,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "value" field. public const int ValueFieldNumber = 1; - private global::Google.Protobuf.TestProtos.TestEnumWithDupValue value_ = 0; + private global::Google.Protobuf.TestProtos.TestEnumWithDupValue value_ = global::Google.Protobuf.TestProtos.TestEnumWithDupValue.Unspecified; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.TestProtos.TestEnumWithDupValue Value { get { return value_; } @@ -3145,7 +3159,7 @@ namespace Google.Protobuf.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Value != 0) hash ^= Value.GetHashCode(); + if (Value != global::Google.Protobuf.TestProtos.TestEnumWithDupValue.Unspecified) hash ^= Value.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -3159,7 +3173,7 @@ namespace Google.Protobuf.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Value != 0) { + if (Value != global::Google.Protobuf.TestProtos.TestEnumWithDupValue.Unspecified) { output.WriteRawTag(8); output.WriteEnum((int) Value); } @@ -3171,7 +3185,7 @@ namespace Google.Protobuf.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Value != 0) { + if (Value != global::Google.Protobuf.TestProtos.TestEnumWithDupValue.Unspecified) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Value); } if (_unknownFields != null) { @@ -3185,7 +3199,7 @@ namespace Google.Protobuf.TestProtos { if (other == null) { return; } - if (other.Value != 0) { + if (other.Value != global::Google.Protobuf.TestProtos.TestEnumWithDupValue.Unspecified) { Value = other.Value; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -3200,7 +3214,7 @@ namespace Google.Protobuf.TestProtos { _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); break; case 8: { - value_ = (global::Google.Protobuf.TestProtos.TestEnumWithDupValue) input.ReadEnum(); + Value = (global::Google.Protobuf.TestProtos.TestEnumWithDupValue) input.ReadEnum(); break; } } @@ -3278,7 +3292,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "EnumField" field. public const int EnumFieldFieldNumber = 3; - private global::Google.Protobuf.TestProtos.ForeignEnum enumField_ = 0; + private global::Google.Protobuf.TestProtos.ForeignEnum enumField_ = global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.TestProtos.ForeignEnum EnumField { get { return enumField_; } @@ -3367,7 +3381,7 @@ namespace Google.Protobuf.TestProtos { int hash = 1; if (PrimitiveField != 0) hash ^= PrimitiveField.GetHashCode(); if (StringField.Length != 0) hash ^= StringField.GetHashCode(); - if (EnumField != 0) hash ^= EnumField.GetHashCode(); + if (EnumField != global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified) hash ^= EnumField.GetHashCode(); if (messageField_ != null) hash ^= MessageField.GetHashCode(); hash ^= repeatedPrimitiveField_.GetHashCode(); hash ^= repeatedStringField_.GetHashCode(); @@ -3394,7 +3408,7 @@ namespace Google.Protobuf.TestProtos { output.WriteRawTag(18); output.WriteString(StringField); } - if (EnumField != 0) { + if (EnumField != global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified) { output.WriteRawTag(24); output.WriteEnum((int) EnumField); } @@ -3420,7 +3434,7 @@ namespace Google.Protobuf.TestProtos { if (StringField.Length != 0) { size += 1 + pb::CodedOutputStream.ComputeStringSize(StringField); } - if (EnumField != 0) { + if (EnumField != global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) EnumField); } if (messageField_ != null) { @@ -3447,12 +3461,12 @@ namespace Google.Protobuf.TestProtos { if (other.StringField.Length != 0) { StringField = other.StringField; } - if (other.EnumField != 0) { + if (other.EnumField != global::Google.Protobuf.TestProtos.ForeignEnum.ForeignUnspecified) { EnumField = other.EnumField; } if (other.messageField_ != null) { if (messageField_ == null) { - messageField_ = new global::Google.Protobuf.TestProtos.ForeignMessage(); + MessageField = new global::Google.Protobuf.TestProtos.ForeignMessage(); } MessageField.MergeFrom(other.MessageField); } @@ -3480,14 +3494,14 @@ namespace Google.Protobuf.TestProtos { break; } case 24: { - enumField_ = (global::Google.Protobuf.TestProtos.ForeignEnum) input.ReadEnum(); + EnumField = (global::Google.Protobuf.TestProtos.ForeignEnum) input.ReadEnum(); break; } case 34: { if (messageField_ == null) { - messageField_ = new global::Google.Protobuf.TestProtos.ForeignMessage(); + MessageField = new global::Google.Protobuf.TestProtos.ForeignMessage(); } - input.ReadMessage(messageField_); + input.ReadMessage(MessageField); break; } case 58: @@ -3697,7 +3711,7 @@ namespace Google.Protobuf.TestProtos { } if (other.singleNestedMessage_ != null) { if (singleNestedMessage_ == null) { - singleNestedMessage_ = new global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage(); + SingleNestedMessage = new global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage(); } SingleNestedMessage.MergeFrom(other.SingleNestedMessage); } @@ -3726,9 +3740,9 @@ namespace Google.Protobuf.TestProtos { } case 1602: { if (singleNestedMessage_ == null) { - singleNestedMessage_ = new global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage(); + SingleNestedMessage = new global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage(); } - input.ReadMessage(singleNestedMessage_); + input.ReadMessage(SingleNestedMessage); break; } } @@ -3942,7 +3956,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "sparse_enum" field. public const int SparseEnumFieldNumber = 1; - private global::Google.Protobuf.TestProtos.TestSparseEnum sparseEnum_ = 0; + private global::Google.Protobuf.TestProtos.TestSparseEnum sparseEnum_ = global::Google.Protobuf.TestProtos.TestSparseEnum.Unspecified; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.TestProtos.TestSparseEnum SparseEnum { get { return sparseEnum_; } @@ -3971,7 +3985,7 @@ namespace Google.Protobuf.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (SparseEnum != 0) hash ^= SparseEnum.GetHashCode(); + if (SparseEnum != global::Google.Protobuf.TestProtos.TestSparseEnum.Unspecified) hash ^= SparseEnum.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -3985,7 +3999,7 @@ namespace Google.Protobuf.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (SparseEnum != 0) { + if (SparseEnum != global::Google.Protobuf.TestProtos.TestSparseEnum.Unspecified) { output.WriteRawTag(8); output.WriteEnum((int) SparseEnum); } @@ -3997,7 +4011,7 @@ namespace Google.Protobuf.TestProtos { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (SparseEnum != 0) { + if (SparseEnum != global::Google.Protobuf.TestProtos.TestSparseEnum.Unspecified) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) SparseEnum); } if (_unknownFields != null) { @@ -4011,7 +4025,7 @@ namespace Google.Protobuf.TestProtos { if (other == null) { return; } - if (other.SparseEnum != 0) { + if (other.SparseEnum != global::Google.Protobuf.TestProtos.TestSparseEnum.Unspecified) { SparseEnum = other.SparseEnum; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -4026,7 +4040,7 @@ namespace Google.Protobuf.TestProtos { _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); break; case 8: { - sparseEnum_ = (global::Google.Protobuf.TestProtos.TestSparseEnum) input.ReadEnum(); + SparseEnum = (global::Google.Protobuf.TestProtos.TestSparseEnum) input.ReadEnum(); break; } } @@ -7301,6 +7315,293 @@ namespace Google.Protobuf.TestProtos { } + /// + /// This is a leading comment + /// + public sealed partial class CommentMessage : pb::IMessage { + private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new CommentMessage()); + private pb::UnknownFieldSet _unknownFields; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pb::MessageParser Parser { get { return _parser; } } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pbr::MessageDescriptor Descriptor { + get { return global::Google.Protobuf.TestProtos.UnittestProto3Reflection.Descriptor.MessageTypes[35]; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + pbr::MessageDescriptor pb::IMessage.Descriptor { + get { return Descriptor; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public CommentMessage() { + OnConstruction(); + } + + partial void OnConstruction(); + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public CommentMessage(CommentMessage other) : this() { + text_ = other.text_; + _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public CommentMessage Clone() { + return new CommentMessage(this); + } + + /// Field number for the "text" field. + public const int TextFieldNumber = 1; + private string text_ = ""; + /// + /// Leading field comment + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string Text { + get { return text_; } + set { + text_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override bool Equals(object other) { + return Equals(other as CommentMessage); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Equals(CommentMessage other) { + if (ReferenceEquals(other, null)) { + return false; + } + if (ReferenceEquals(other, this)) { + return true; + } + if (Text != other.Text) return false; + return Equals(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override int GetHashCode() { + int hash = 1; + if (Text.Length != 0) hash ^= Text.GetHashCode(); + if (_unknownFields != null) { + hash ^= _unknownFields.GetHashCode(); + } + return hash; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override string ToString() { + return pb::JsonFormatter.ToDiagnosticString(this); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void WriteTo(pb::CodedOutputStream output) { + if (Text.Length != 0) { + output.WriteRawTag(10); + output.WriteString(Text); + } + if (_unknownFields != null) { + _unknownFields.WriteTo(output); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int CalculateSize() { + int size = 0; + if (Text.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(Text); + } + if (_unknownFields != null) { + size += _unknownFields.CalculateSize(); + } + return size; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(CommentMessage other) { + if (other == null) { + return; + } + if (other.Text.Length != 0) { + Text = other.Text; + } + _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(pb::CodedInputStream input) { + uint tag; + while ((tag = input.ReadTag()) != 0) { + switch(tag) { + default: + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + break; + case 10: { + Text = input.ReadString(); + break; + } + } + } + } + + #region Nested types + /// Container for nested types declared in the CommentMessage message type. + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static partial class Types { + /// + /// Leading nested enum comment + /// + public enum NestedCommentEnum { + /// + /// Zero value comment + /// + [pbr::OriginalName("ZERO_VALUE")] ZeroValue = 0, + } + + /// + /// Leading nested message comment + /// + public sealed partial class NestedCommentMessage : pb::IMessage { + private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new NestedCommentMessage()); + private pb::UnknownFieldSet _unknownFields; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pb::MessageParser Parser { get { return _parser; } } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pbr::MessageDescriptor Descriptor { + get { return global::Google.Protobuf.TestProtos.CommentMessage.Descriptor.NestedTypes[0]; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + pbr::MessageDescriptor pb::IMessage.Descriptor { + get { return Descriptor; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public NestedCommentMessage() { + OnConstruction(); + } + + partial void OnConstruction(); + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public NestedCommentMessage(NestedCommentMessage other) : this() { + nestedText_ = other.nestedText_; + _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public NestedCommentMessage Clone() { + return new NestedCommentMessage(this); + } + + /// Field number for the "nested_text" field. + public const int NestedTextFieldNumber = 1; + private string nestedText_ = ""; + /// + /// Leading nested message field comment + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string NestedText { + get { return nestedText_; } + set { + nestedText_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override bool Equals(object other) { + return Equals(other as NestedCommentMessage); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Equals(NestedCommentMessage other) { + if (ReferenceEquals(other, null)) { + return false; + } + if (ReferenceEquals(other, this)) { + return true; + } + if (NestedText != other.NestedText) return false; + return Equals(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override int GetHashCode() { + int hash = 1; + if (NestedText.Length != 0) hash ^= NestedText.GetHashCode(); + if (_unknownFields != null) { + hash ^= _unknownFields.GetHashCode(); + } + return hash; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override string ToString() { + return pb::JsonFormatter.ToDiagnosticString(this); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void WriteTo(pb::CodedOutputStream output) { + if (NestedText.Length != 0) { + output.WriteRawTag(10); + output.WriteString(NestedText); + } + if (_unknownFields != null) { + _unknownFields.WriteTo(output); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int CalculateSize() { + int size = 0; + if (NestedText.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(NestedText); + } + if (_unknownFields != null) { + size += _unknownFields.CalculateSize(); + } + return size; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(NestedCommentMessage other) { + if (other == null) { + return; + } + if (other.NestedText.Length != 0) { + NestedText = other.NestedText; + } + _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(pb::CodedInputStream input) { + uint tag; + while ((tag = input.ReadTag()) != 0) { + switch(tag) { + default: + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + break; + case 10: { + NestedText = input.ReadString(); + break; + } + } + } + } + + } + + } + #endregion + + } + #endregion } diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestWellKnownTypes.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestWellKnownTypes.cs index fe913802f7..90b338436b 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestWellKnownTypes.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestWellKnownTypes.cs @@ -163,11 +163,11 @@ namespace Google.Protobuf.TestProtos { "dFByb3Rvc2IGcHJvdG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.AnyReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.ApiReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.DurationReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.EmptyReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.FieldMaskReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.SourceContextReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.StructReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.TimestampReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.TypeReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.WrappersReflection.Descriptor, }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestWellKnownTypes), global::Google.Protobuf.TestProtos.TestWellKnownTypes.Parser, new[]{ "AnyField", "ApiField", "DurationField", "EmptyField", "FieldMaskField", "SourceContextField", "StructField", "TimestampField", "TypeField", "DoubleField", "FloatField", "Int64Field", "Uint64Field", "Int32Field", "Uint32Field", "BoolField", "StringField", "BytesField", "ValueField" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.RepeatedWellKnownTypes), global::Google.Protobuf.TestProtos.RepeatedWellKnownTypes.Parser, new[]{ "AnyField", "ApiField", "DurationField", "EmptyField", "FieldMaskField", "SourceContextField", "StructField", "TimestampField", "TypeField", "DoubleField", "FloatField", "Int64Field", "Uint64Field", "Int32Field", "Uint32Field", "BoolField", "StringField", "BytesField" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.OneofWellKnownTypes), global::Google.Protobuf.TestProtos.OneofWellKnownTypes.Parser, new[]{ "AnyField", "ApiField", "DurationField", "EmptyField", "FieldMaskField", "SourceContextField", "StructField", "TimestampField", "TypeField", "DoubleField", "FloatField", "Int64Field", "Uint64Field", "Int32Field", "Uint32Field", "BoolField", "StringField", "BytesField" }, new[]{ "OneofField" }, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MapWellKnownTypes), global::Google.Protobuf.TestProtos.MapWellKnownTypes.Parser, new[]{ "AnyField", "ApiField", "DurationField", "EmptyField", "FieldMaskField", "SourceContextField", "StructField", "TimestampField", "TypeField", "DoubleField", "FloatField", "Int64Field", "Uint64Field", "Int32Field", "Uint32Field", "BoolField", "StringField", "BytesField" }, null, null, new pbr::GeneratedClrTypeInfo[] { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, }) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.TestWellKnownTypes), global::Google.Protobuf.TestProtos.TestWellKnownTypes.Parser, new[]{ "AnyField", "ApiField", "DurationField", "EmptyField", "FieldMaskField", "SourceContextField", "StructField", "TimestampField", "TypeField", "DoubleField", "FloatField", "Int64Field", "Uint64Field", "Int32Field", "Uint32Field", "BoolField", "StringField", "BytesField", "ValueField" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.RepeatedWellKnownTypes), global::Google.Protobuf.TestProtos.RepeatedWellKnownTypes.Parser, new[]{ "AnyField", "ApiField", "DurationField", "EmptyField", "FieldMaskField", "SourceContextField", "StructField", "TimestampField", "TypeField", "DoubleField", "FloatField", "Int64Field", "Uint64Field", "Int32Field", "Uint32Field", "BoolField", "StringField", "BytesField" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.OneofWellKnownTypes), global::Google.Protobuf.TestProtos.OneofWellKnownTypes.Parser, new[]{ "AnyField", "ApiField", "DurationField", "EmptyField", "FieldMaskField", "SourceContextField", "StructField", "TimestampField", "TypeField", "DoubleField", "FloatField", "Int64Field", "Uint64Field", "Int32Field", "Uint32Field", "BoolField", "StringField", "BytesField" }, new[]{ "OneofField" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.TestProtos.MapWellKnownTypes), global::Google.Protobuf.TestProtos.MapWellKnownTypes.Parser, new[]{ "AnyField", "ApiField", "DurationField", "EmptyField", "FieldMaskField", "SourceContextField", "StructField", "TimestampField", "TypeField", "DoubleField", "FloatField", "Int64Field", "Uint64Field", "Int32Field", "Uint32Field", "BoolField", "StringField", "BytesField" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, }) })); } #endregion @@ -342,6 +342,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "float_field" field. public const int FloatFieldFieldNumber = 11; private static readonly pb::FieldCodec _single_floatField_codec = pb::FieldCodec.ForStructWrapper(90); @@ -354,6 +355,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "int64_field" field. public const int Int64FieldFieldNumber = 12; private static readonly pb::FieldCodec _single_int64Field_codec = pb::FieldCodec.ForStructWrapper(98); @@ -366,6 +368,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "uint64_field" field. public const int Uint64FieldFieldNumber = 13; private static readonly pb::FieldCodec _single_uint64Field_codec = pb::FieldCodec.ForStructWrapper(106); @@ -378,6 +381,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "int32_field" field. public const int Int32FieldFieldNumber = 14; private static readonly pb::FieldCodec _single_int32Field_codec = pb::FieldCodec.ForStructWrapper(114); @@ -390,6 +394,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "uint32_field" field. public const int Uint32FieldFieldNumber = 15; private static readonly pb::FieldCodec _single_uint32Field_codec = pb::FieldCodec.ForStructWrapper(122); @@ -402,6 +407,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "bool_field" field. public const int BoolFieldFieldNumber = 16; private static readonly pb::FieldCodec _single_boolField_codec = pb::FieldCodec.ForStructWrapper(130); @@ -414,6 +420,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "string_field" field. public const int StringFieldFieldNumber = 17; private static readonly pb::FieldCodec _single_stringField_codec = pb::FieldCodec.ForClassWrapper(138); @@ -426,6 +433,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "bytes_field" field. public const int BytesFieldFieldNumber = 18; private static readonly pb::FieldCodec _single_bytesField_codec = pb::FieldCodec.ForClassWrapper(146); @@ -438,6 +446,7 @@ namespace Google.Protobuf.TestProtos { } } + /// Field number for the "value_field" field. public const int ValueFieldFieldNumber = 19; private global::Google.Protobuf.WellKnownTypes.Value valueField_; @@ -667,55 +676,55 @@ namespace Google.Protobuf.TestProtos { } if (other.anyField_ != null) { if (anyField_ == null) { - anyField_ = new global::Google.Protobuf.WellKnownTypes.Any(); + AnyField = new global::Google.Protobuf.WellKnownTypes.Any(); } AnyField.MergeFrom(other.AnyField); } if (other.apiField_ != null) { if (apiField_ == null) { - apiField_ = new global::Google.Protobuf.WellKnownTypes.Api(); + ApiField = new global::Google.Protobuf.WellKnownTypes.Api(); } ApiField.MergeFrom(other.ApiField); } if (other.durationField_ != null) { if (durationField_ == null) { - durationField_ = new global::Google.Protobuf.WellKnownTypes.Duration(); + DurationField = new global::Google.Protobuf.WellKnownTypes.Duration(); } DurationField.MergeFrom(other.DurationField); } if (other.emptyField_ != null) { if (emptyField_ == null) { - emptyField_ = new global::Google.Protobuf.WellKnownTypes.Empty(); + EmptyField = new global::Google.Protobuf.WellKnownTypes.Empty(); } EmptyField.MergeFrom(other.EmptyField); } if (other.fieldMaskField_ != null) { if (fieldMaskField_ == null) { - fieldMaskField_ = new global::Google.Protobuf.WellKnownTypes.FieldMask(); + FieldMaskField = new global::Google.Protobuf.WellKnownTypes.FieldMask(); } FieldMaskField.MergeFrom(other.FieldMaskField); } if (other.sourceContextField_ != null) { if (sourceContextField_ == null) { - sourceContextField_ = new global::Google.Protobuf.WellKnownTypes.SourceContext(); + SourceContextField = new global::Google.Protobuf.WellKnownTypes.SourceContext(); } SourceContextField.MergeFrom(other.SourceContextField); } if (other.structField_ != null) { if (structField_ == null) { - structField_ = new global::Google.Protobuf.WellKnownTypes.Struct(); + StructField = new global::Google.Protobuf.WellKnownTypes.Struct(); } StructField.MergeFrom(other.StructField); } if (other.timestampField_ != null) { if (timestampField_ == null) { - timestampField_ = new global::Google.Protobuf.WellKnownTypes.Timestamp(); + TimestampField = new global::Google.Protobuf.WellKnownTypes.Timestamp(); } TimestampField.MergeFrom(other.TimestampField); } if (other.typeField_ != null) { if (typeField_ == null) { - typeField_ = new global::Google.Protobuf.WellKnownTypes.Type(); + TypeField = new global::Google.Protobuf.WellKnownTypes.Type(); } TypeField.MergeFrom(other.TypeField); } @@ -766,7 +775,7 @@ namespace Google.Protobuf.TestProtos { } if (other.valueField_ != null) { if (valueField_ == null) { - valueField_ = new global::Google.Protobuf.WellKnownTypes.Value(); + ValueField = new global::Google.Protobuf.WellKnownTypes.Value(); } ValueField.MergeFrom(other.ValueField); } @@ -783,65 +792,65 @@ namespace Google.Protobuf.TestProtos { break; case 10: { if (anyField_ == null) { - anyField_ = new global::Google.Protobuf.WellKnownTypes.Any(); + AnyField = new global::Google.Protobuf.WellKnownTypes.Any(); } - input.ReadMessage(anyField_); + input.ReadMessage(AnyField); break; } case 18: { if (apiField_ == null) { - apiField_ = new global::Google.Protobuf.WellKnownTypes.Api(); + ApiField = new global::Google.Protobuf.WellKnownTypes.Api(); } - input.ReadMessage(apiField_); + input.ReadMessage(ApiField); break; } case 26: { if (durationField_ == null) { - durationField_ = new global::Google.Protobuf.WellKnownTypes.Duration(); + DurationField = new global::Google.Protobuf.WellKnownTypes.Duration(); } - input.ReadMessage(durationField_); + input.ReadMessage(DurationField); break; } case 34: { if (emptyField_ == null) { - emptyField_ = new global::Google.Protobuf.WellKnownTypes.Empty(); + EmptyField = new global::Google.Protobuf.WellKnownTypes.Empty(); } - input.ReadMessage(emptyField_); + input.ReadMessage(EmptyField); break; } case 42: { if (fieldMaskField_ == null) { - fieldMaskField_ = new global::Google.Protobuf.WellKnownTypes.FieldMask(); + FieldMaskField = new global::Google.Protobuf.WellKnownTypes.FieldMask(); } - input.ReadMessage(fieldMaskField_); + input.ReadMessage(FieldMaskField); break; } case 50: { if (sourceContextField_ == null) { - sourceContextField_ = new global::Google.Protobuf.WellKnownTypes.SourceContext(); + SourceContextField = new global::Google.Protobuf.WellKnownTypes.SourceContext(); } - input.ReadMessage(sourceContextField_); + input.ReadMessage(SourceContextField); break; } case 58: { if (structField_ == null) { - structField_ = new global::Google.Protobuf.WellKnownTypes.Struct(); + StructField = new global::Google.Protobuf.WellKnownTypes.Struct(); } - input.ReadMessage(structField_); + input.ReadMessage(StructField); break; } case 66: { if (timestampField_ == null) { - timestampField_ = new global::Google.Protobuf.WellKnownTypes.Timestamp(); + TimestampField = new global::Google.Protobuf.WellKnownTypes.Timestamp(); } - input.ReadMessage(timestampField_); + input.ReadMessage(TimestampField); break; } case 74: { if (typeField_ == null) { - typeField_ = new global::Google.Protobuf.WellKnownTypes.Type(); + TypeField = new global::Google.Protobuf.WellKnownTypes.Type(); } - input.ReadMessage(typeField_); + input.ReadMessage(TypeField); break; } case 82: { @@ -909,9 +918,9 @@ namespace Google.Protobuf.TestProtos { } case 154: { if (valueField_ == null) { - valueField_ = new global::Google.Protobuf.WellKnownTypes.Value(); + ValueField = new global::Google.Protobuf.WellKnownTypes.Value(); } - input.ReadMessage(valueField_); + input.ReadMessage(ValueField); break; } } @@ -2202,7 +2211,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "any_field" field. public const int AnyFieldFieldNumber = 1; private static readonly pbc::MapField.Codec _map_anyField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Any.Parser), 10); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Any.Parser), 10); private readonly pbc::MapField anyField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField AnyField { @@ -2212,7 +2221,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "api_field" field. public const int ApiFieldFieldNumber = 2; private static readonly pbc::MapField.Codec _map_apiField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Api.Parser), 18); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Api.Parser), 18); private readonly pbc::MapField apiField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField ApiField { @@ -2222,7 +2231,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "duration_field" field. public const int DurationFieldFieldNumber = 3; private static readonly pbc::MapField.Codec _map_durationField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Duration.Parser), 26); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Duration.Parser), 26); private readonly pbc::MapField durationField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField DurationField { @@ -2232,7 +2241,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "empty_field" field. public const int EmptyFieldFieldNumber = 4; private static readonly pbc::MapField.Codec _map_emptyField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Empty.Parser), 34); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Empty.Parser), 34); private readonly pbc::MapField emptyField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField EmptyField { @@ -2242,7 +2251,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "field_mask_field" field. public const int FieldMaskFieldFieldNumber = 5; private static readonly pbc::MapField.Codec _map_fieldMaskField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.FieldMask.Parser), 42); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.FieldMask.Parser), 42); private readonly pbc::MapField fieldMaskField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField FieldMaskField { @@ -2252,7 +2261,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "source_context_field" field. public const int SourceContextFieldFieldNumber = 6; private static readonly pbc::MapField.Codec _map_sourceContextField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.SourceContext.Parser), 50); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.SourceContext.Parser), 50); private readonly pbc::MapField sourceContextField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField SourceContextField { @@ -2262,7 +2271,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "struct_field" field. public const int StructFieldFieldNumber = 7; private static readonly pbc::MapField.Codec _map_structField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Struct.Parser), 58); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Struct.Parser), 58); private readonly pbc::MapField structField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField StructField { @@ -2272,7 +2281,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "timestamp_field" field. public const int TimestampFieldFieldNumber = 8; private static readonly pbc::MapField.Codec _map_timestampField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Timestamp.Parser), 66); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Timestamp.Parser), 66); private readonly pbc::MapField timestampField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField TimestampField { @@ -2282,7 +2291,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "type_field" field. public const int TypeFieldFieldNumber = 9; private static readonly pbc::MapField.Codec _map_typeField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Type.Parser), 74); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Type.Parser), 74); private readonly pbc::MapField typeField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField TypeField { @@ -2292,7 +2301,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "double_field" field. public const int DoubleFieldFieldNumber = 10; private static readonly pbc::MapField.Codec _map_doubleField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForStructWrapper(18), 82); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForStructWrapper(18), 82); private readonly pbc::MapField doubleField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField DoubleField { @@ -2302,7 +2311,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "float_field" field. public const int FloatFieldFieldNumber = 11; private static readonly pbc::MapField.Codec _map_floatField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForStructWrapper(18), 90); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForStructWrapper(18), 90); private readonly pbc::MapField floatField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField FloatField { @@ -2312,7 +2321,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "int64_field" field. public const int Int64FieldFieldNumber = 12; private static readonly pbc::MapField.Codec _map_int64Field_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForStructWrapper(18), 98); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForStructWrapper(18), 98); private readonly pbc::MapField int64Field_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField Int64Field { @@ -2322,7 +2331,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "uint64_field" field. public const int Uint64FieldFieldNumber = 13; private static readonly pbc::MapField.Codec _map_uint64Field_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForStructWrapper(18), 106); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForStructWrapper(18), 106); private readonly pbc::MapField uint64Field_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField Uint64Field { @@ -2332,7 +2341,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "int32_field" field. public const int Int32FieldFieldNumber = 14; private static readonly pbc::MapField.Codec _map_int32Field_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForStructWrapper(18), 114); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForStructWrapper(18), 114); private readonly pbc::MapField int32Field_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField Int32Field { @@ -2342,7 +2351,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "uint32_field" field. public const int Uint32FieldFieldNumber = 15; private static readonly pbc::MapField.Codec _map_uint32Field_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForStructWrapper(18), 122); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForStructWrapper(18), 122); private readonly pbc::MapField uint32Field_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField Uint32Field { @@ -2352,7 +2361,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "bool_field" field. public const int BoolFieldFieldNumber = 16; private static readonly pbc::MapField.Codec _map_boolField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForStructWrapper(18), 130); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForStructWrapper(18), 130); private readonly pbc::MapField boolField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField BoolField { @@ -2362,7 +2371,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "string_field" field. public const int StringFieldFieldNumber = 17; private static readonly pbc::MapField.Codec _map_stringField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForClassWrapper(18), 138); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForClassWrapper(18), 138); private readonly pbc::MapField stringField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField StringField { @@ -2372,7 +2381,7 @@ namespace Google.Protobuf.TestProtos { /// Field number for the "bytes_field" field. public const int BytesFieldFieldNumber = 18; private static readonly pbc::MapField.Codec _map_bytesField_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8), pb::FieldCodec.ForClassWrapper(18), 146); + = new pbc::MapField.Codec(pb::FieldCodec.ForInt32(8, 0), pb::FieldCodec.ForClassWrapper(18), 146); private readonly pbc::MapField bytesField_ = new pbc::MapField(); [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pbc::MapField BytesField { diff --git a/csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs b/csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs index ddf623219a..438b2ece36 100644 --- a/csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs +++ b/csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs @@ -172,5 +172,23 @@ namespace Google.Protobuf assertEmpty(discardingParser1.ParseFrom(new MemoryStream(data))); assertEmpty(discardingParser2.ParseFrom(new MemoryStream(data))); } + + [Test] + public void TestReadInvalidWireTypeThrowsInvalidProtocolBufferException() + { + MemoryStream ms = new MemoryStream(); + CodedOutputStream output = new CodedOutputStream(ms); + + uint tag = WireFormat.MakeTag(1, (WireFormat.WireType)6); + output.WriteRawVarint32(tag); + output.WriteLength(-1); + output.Flush(); + ms.Position = 0; + + CodedInputStream input = new CodedInputStream(ms); + Assert.AreEqual(tag, input.ReadTag()); + + Assert.Throws(() => UnknownFieldSet.MergeFieldFrom(null, input)); + } } } diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs index 6ca1e1f04b..4b9a569395 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs @@ -140,5 +140,18 @@ namespace Google.Protobuf.WellKnownTypes var message = new TestWellKnownTypes { AnyField = new Any() }; Assert.AreEqual("{ \"anyField\": { \"@type\": \"\", \"@value\": \"\" } }", message.ToString()); } + + [Test] + public void IsWrongType() + { + var any = Any.Pack(SampleMessages.CreateFullTestAllTypes()); + Assert.False(any.Is(TestOneof.Descriptor)); + } + + public void IsRightType() + { + var any = Any.Pack(SampleMessages.CreateFullTestAllTypes()); + Assert.True(any.Is(TestAllTypes.Descriptor)); + } } } diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/FieldMaskTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/FieldMaskTest.cs index 1d9908b4d3..5dc5035703 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/FieldMaskTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/FieldMaskTest.cs @@ -30,7 +30,8 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion - +using System; +using Google.Protobuf.TestProtos; using NUnit.Framework; namespace Google.Protobuf.WellKnownTypes @@ -58,5 +59,187 @@ namespace Google.Protobuf.WellKnownTypes "{ \"@warning\": \"Invalid FieldMask\", \"paths\": [ \"x\", \"foo__bar\", \"x\\\\y\" ] }", mask.ToString()); } + + [Test] + public void IsValid() + { + Assert.IsTrue(FieldMask.IsValid("payload")); + Assert.IsFalse(FieldMask.IsValid("nonexist")); + Assert.IsTrue(FieldMask.IsValid("payload.single_int32")); + Assert.IsTrue(FieldMask.IsValid("payload.repeated_int32")); + Assert.IsTrue(FieldMask.IsValid("payload.single_nested_message")); + Assert.IsTrue(FieldMask.IsValid("payload.repeated_nested_message")); + Assert.IsFalse(FieldMask.IsValid("payload.nonexist")); + + Assert.IsTrue(FieldMask.IsValid(FieldMask.FromString("payload"))); + Assert.IsFalse(FieldMask.IsValid(FieldMask.FromString("nonexist"))); + Assert.IsFalse(FieldMask.IsValid(FieldMask.FromString("payload,nonexist"))); + + Assert.IsTrue(FieldMask.IsValid(NestedTestAllTypes.Descriptor, "payload")); + Assert.IsFalse(FieldMask.IsValid(NestedTestAllTypes.Descriptor, "nonexist")); + + Assert.IsTrue(FieldMask.IsValid(NestedTestAllTypes.Descriptor, FieldMask.FromString("payload"))); + Assert.IsFalse(FieldMask.IsValid(NestedTestAllTypes.Descriptor, FieldMask.FromString("nonexist"))); + + Assert.IsTrue(FieldMask.IsValid("payload.single_nested_message.bb")); + + // Repeated fields cannot have sub-paths. + Assert.IsFalse(FieldMask.IsValid("payload.repeated_nested_message.bb")); + + // Non-message fields cannot have sub-paths. + Assert.IsFalse(FieldMask.IsValid("payload.single_int32.bb")); + } + + [Test] + [TestCase(new string[] { }, "\"\"")] + [TestCase(new string[] { "foo" }, "\"foo\"")] + [TestCase(new string[] { "foo", "bar" }, "\"foo,bar\"")] + [TestCase(new string[] { "", "foo", "", "bar", "" }, "\",foo,,bar,\"")] + public void ToString(string[] input, string expectedOutput) + { + FieldMask mask = new FieldMask(); + mask.Paths.AddRange(input); + Assert.AreEqual(expectedOutput, mask.ToString()); + } + + [Test] + [TestCase("", new string[] { })] + [TestCase("foo", new string[] { "foo" })] + [TestCase("foo,bar.baz", new string[] { "foo", "bar.baz" })] + [TestCase(",foo,,bar,", new string[] { "foo", "bar" })] + public void FromString(string input, string[] expectedOutput) + { + FieldMask mask = FieldMask.FromString(input); + Assert.AreEqual(expectedOutput.Length, mask.Paths.Count); + for (int i = 0; i < expectedOutput.Length; i++) + { + Assert.AreEqual(expectedOutput[i], mask.Paths[i]); + } + } + + [Test] + public void FromString_Validated() + { + // Check whether the field paths are valid if a class parameter is provided. + Assert.DoesNotThrow(() => FieldMask.FromString(",payload")); + Assert.Throws(() => FieldMask.FromString("payload,nonexist")); + } + + [Test] + [TestCase(new int[] { }, new string[] { })] + [TestCase(new int[] { TestAllTypes.SingleInt32FieldNumber }, new string[] { "single_int32" })] + [TestCase(new int[] { TestAllTypes.SingleInt32FieldNumber, TestAllTypes.SingleInt64FieldNumber }, new string[] { "single_int32", "single_int64" })] + public void FromFieldNumbers(int[] input, string[] expectedOutput) + { + FieldMask mask = FieldMask.FromFieldNumbers(input); + Assert.AreEqual(expectedOutput.Length, mask.Paths.Count); + for (int i = 0; i < expectedOutput.Length; i++) + { + Assert.AreEqual(expectedOutput[i], mask.Paths[i]); + } + } + + [Test] + public void FromFieldNumbers_Invalid() + { + Assert.Throws(() => + { + int invalidFieldNumber = 1000; + FieldMask.FromFieldNumbers(invalidFieldNumber); + }); + } + + [Test] + [TestCase(new string[] { }, "\"\"")] + [TestCase(new string[] { "foo" }, "\"foo\"")] + [TestCase(new string[] { "foo", "bar" }, "\"foo,bar\"")] + [TestCase(new string[] { "", "foo", "", "bar", "" }, "\",foo,bar\"")] + public void Normalize(string[] input, string expectedOutput) + { + FieldMask mask = new FieldMask(); + mask.Paths.AddRange(input); + FieldMask result = mask.Normalize(); + Assert.AreEqual(expectedOutput, result.ToString()); + } + + [Test] + public void Union() + { + // Only test a simple case here and expect + // {@link FieldMaskTreeTest#AddFieldPath} to cover all scenarios. + FieldMask mask1 = FieldMask.FromString("foo,bar.baz,bar.quz"); + FieldMask mask2 = FieldMask.FromString("foo.bar,bar"); + FieldMask result = mask1.Union(mask2); + Assert.AreEqual(2, result.Paths.Count); + Assert.Contains("bar", result.Paths); + Assert.Contains("foo", result.Paths); + Assert.That(result.Paths, Has.No.Member("bar.baz")); + Assert.That(result.Paths, Has.No.Member("bar.quz")); + Assert.That(result.Paths, Has.No.Member("foo.bar")); + } + + [Test] + public void Union_UsingVarArgs() + { + FieldMask mask1 = FieldMask.FromString("foo"); + FieldMask mask2 = FieldMask.FromString("foo.bar,bar.quz"); + FieldMask mask3 = FieldMask.FromString("bar.quz"); + FieldMask mask4 = FieldMask.FromString("bar"); + FieldMask result = mask1.Union(mask2, mask3, mask4); + Assert.AreEqual(2, result.Paths.Count); + Assert.Contains("bar", result.Paths); + Assert.Contains("foo", result.Paths); + Assert.That(result.Paths, Has.No.Member("foo.bar")); + Assert.That(result.Paths, Has.No.Member("bar.quz")); + } + + [Test] + public void Intersection() + { + // Only test a simple case here and expect + // {@link FieldMaskTreeTest#IntersectFieldPath} to cover all scenarios. + FieldMask mask1 = FieldMask.FromString("foo,bar.baz,bar.quz"); + FieldMask mask2 = FieldMask.FromString("foo.bar,bar"); + FieldMask result = mask1.Intersection(mask2); + Assert.AreEqual(3, result.Paths.Count); + Assert.Contains("foo.bar", result.Paths); + Assert.Contains("bar.baz", result.Paths); + Assert.Contains("bar.quz", result.Paths); + Assert.That(result.Paths, Has.No.Member("foo")); + Assert.That(result.Paths, Has.No.Member("bar")); + } + + [Test] + public void Merge() + { + // Only test a simple case here and expect + // {@link FieldMaskTreeTest#Merge} to cover all scenarios. + FieldMask fieldMask = FieldMask.FromString("payload"); + NestedTestAllTypes source = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleInt32 = 1234, + SingleFixed64 = 4321 + } + }; + NestedTestAllTypes destination = new NestedTestAllTypes(); + fieldMask.Merge(source, destination); + Assert.AreEqual(1234, destination.Payload.SingleInt32); + Assert.AreEqual(4321, destination.Payload.SingleFixed64); + + destination = new NestedTestAllTypes + { + Payload = new TestAllTypes + { + SingleInt32 = 4321, + SingleInt64 = 5678 + } + }; + fieldMask.Merge(source, destination); + Assert.AreEqual(1234, destination.Payload.SingleInt32); + Assert.AreEqual(5678, destination.Payload.SingleInt64); + Assert.AreEqual(4321, destination.Payload.SingleFixed64); + } } } diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs index b8c07ef51b..ffc6041924 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs @@ -99,7 +99,7 @@ namespace Google.Protobuf.WellKnownTypes Duration difference = new Duration { Seconds = 1999, Nanos = Duration.NanosecondsPerSecond - 5000 }; Assert.AreEqual(difference, t1 - t2); Assert.AreEqual(-difference, t2 - t1); - + Assert.AreEqual(t1, t2 + difference); Assert.AreEqual(t2, t1 - difference); } @@ -190,7 +190,7 @@ namespace Google.Protobuf.WellKnownTypes Assert.IsTrue(e > d); Assert.IsTrue(e == e); Assert.IsTrue(e == e.Clone()); - + Assert.IsTrue(b >= a); Assert.IsTrue(b <= c); Assert.IsTrue(b <= d); diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs index 8ed557445a..8a9c3d0c68 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs @@ -135,7 +135,7 @@ namespace Google.Protobuf.WellKnownTypes DoubleField = { 12.5, -1.5, 0d }, FloatField = { 123.25f, -20f, 0f }, Int32Field = { int.MaxValue, int.MinValue, 0 }, - Int64Field = { long.MaxValue, long.MinValue, 0L }, + Int64Field = { long.MaxValue, long.MinValue, 0L }, StringField = { "First", "Second", "" }, Uint32Field = { uint.MaxValue, uint.MinValue, 0U }, Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL }, @@ -403,7 +403,7 @@ namespace Google.Protobuf.WellKnownTypes output.Flush(); stream.Position = 0; - + var message = TestWellKnownTypes.Parser.ParseFrom(stream); Assert.AreEqual(6, message.Int32Field); } diff --git a/csharp/src/Google.Protobuf.Test/testprotos.pb b/csharp/src/Google.Protobuf.Test/testprotos.pb new file mode 100644 index 0000000000..71256aea77 Binary files /dev/null and b/csharp/src/Google.Protobuf.Test/testprotos.pb differ diff --git a/csharp/src/Google.Protobuf.sln b/csharp/src/Google.Protobuf.sln index 443ee3e93e..262a43b78f 100644 --- a/csharp/src/Google.Protobuf.sln +++ b/csharp/src/Google.Protobuf.sln @@ -2,15 +2,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26114.2 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddressBook", "AddressBook\AddressBook.csproj", "{AFB63919-1E05-43B4-802A-8FB8C9B2F463}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AddressBook", "AddressBook\AddressBook.csproj", "{AFB63919-1E05-43B4-802A-8FB8C9B2F463}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Protobuf", "Google.Protobuf\Google.Protobuf.csproj", "{9B576380-726D-4142-8238-60A43AB0E35A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Google.Protobuf", "Google.Protobuf\Google.Protobuf.csproj", "{9B576380-726D-4142-8238-60A43AB0E35A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Protobuf.Test", "Google.Protobuf.Test\Google.Protobuf.Test.csproj", "{580EB013-D3C7-4578-B845-015F4A3B0591}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Google.Protobuf.Test", "Google.Protobuf.Test\Google.Protobuf.Test.csproj", "{580EB013-D3C7-4578-B845-015F4A3B0591}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Protobuf.Conformance", "Google.Protobuf.Conformance\Google.Protobuf.Conformance.csproj", "{DDDC055B-E185-4181-BAB0-072F0F984569}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Google.Protobuf.Conformance", "Google.Protobuf.Conformance\Google.Protobuf.Conformance.csproj", "{DDDC055B-E185-4181-BAB0-072F0F984569}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Protobuf.JsonDump", "Google.Protobuf.JsonDump\Google.Protobuf.JsonDump.csproj", "{9695E08F-9829-497D-B95C-B38F28D48690}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Google.Protobuf.JsonDump", "Google.Protobuf.JsonDump\Google.Protobuf.JsonDump.csproj", "{9695E08F-9829-497D-B95C-B38F28D48690}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Protobuf.Benchmarks", "Google.Protobuf.Benchmarks\Google.Protobuf.Benchmarks.csproj", "{D25E4804-4DEA-45AB-9F8C-BA4DBD8E5A07}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -38,8 +40,15 @@ Global {9695E08F-9829-497D-B95C-B38F28D48690}.Debug|Any CPU.Build.0 = Debug|Any CPU {9695E08F-9829-497D-B95C-B38F28D48690}.Release|Any CPU.ActiveCfg = Release|Any CPU {9695E08F-9829-497D-B95C-B38F28D48690}.Release|Any CPU.Build.0 = Release|Any CPU + {D25E4804-4DEA-45AB-9F8C-BA4DBD8E5A07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D25E4804-4DEA-45AB-9F8C-BA4DBD8E5A07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D25E4804-4DEA-45AB-9F8C-BA4DBD8E5A07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D25E4804-4DEA-45AB-9F8C-BA4DBD8E5A07}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7B06C87B-83E1-4F5F-A0DD-6E9AFAC03DAC} + EndGlobalSection EndGlobal diff --git a/csharp/src/Google.Protobuf/ByteString.cs b/csharp/src/Google.Protobuf/ByteString.cs index 4abdb7182c..32078b4df5 100644 --- a/csharp/src/Google.Protobuf/ByteString.cs +++ b/csharp/src/Google.Protobuf/ByteString.cs @@ -67,15 +67,6 @@ namespace Google.Protobuf { return new ByteString(bytes); } - - /// - /// Provides direct, unrestricted access to the bytes contained in this instance. - /// You must not modify or resize the byte array returned by this method. - /// - internal static byte[] GetBuffer(ByteString bytes) - { - return bytes.bytes; - } } /// @@ -119,6 +110,14 @@ namespace Google.Protobuf get { return Length == 0; } } +#if NETSTANDARD2_0 + /// + /// Provides read-only access to the data of this . + /// No data is copied so this is the most efficient way of accessing. + /// + public ReadOnlySpan Span => new ReadOnlySpan(bytes); +#endif + /// /// Converts this into a byte array. /// @@ -161,7 +160,7 @@ namespace Google.Protobuf int capacity = stream.CanSeek ? checked((int) (stream.Length - stream.Position)) : 0; var memoryStream = new MemoryStream(capacity); stream.CopyTo(memoryStream); -#if NETSTANDARD1_0 +#if NETSTANDARD1_0 || NETSTANDARD2_0 byte[] bytes = memoryStream.ToArray(); #else // Avoid an extra copy if we can. @@ -187,7 +186,7 @@ namespace Google.Protobuf // We have to specify the buffer size here, as there's no overload accepting the cancellation token // alone. But it's documented to use 81920 by default if not specified. await stream.CopyToAsync(memoryStream, 81920, cancellationToken); -#if NETSTANDARD1_0 +#if NETSTANDARD1_0 || NETSTANDARD2_0 byte[] bytes = memoryStream.ToArray(); #else // Avoid an extra copy if we can. @@ -219,6 +218,18 @@ namespace Google.Protobuf return new ByteString(portion); } +#if NETSTANDARD2_0 + /// + /// Constructs a from a read only span. The contents + /// are copied, so further modifications to the span will not + /// be reflected in the returned . + /// + public static ByteString CopyFrom(ReadOnlySpan bytes) + { + return new ByteString(bytes.ToArray()); + } +#endif + /// /// Creates a new by encoding the specified text with /// the given encoding. diff --git a/csharp/src/Google.Protobuf/CodedInputStream.cs b/csharp/src/Google.Protobuf/CodedInputStream.cs index 0a829545e2..4d13f88ab9 100644 --- a/csharp/src/Google.Protobuf/CodedInputStream.cs +++ b/csharp/src/Google.Protobuf/CodedInputStream.cs @@ -93,7 +93,7 @@ namespace Google.Protobuf private uint nextTag = 0; private bool hasNextTag = false; - internal const int DefaultRecursionLimit = 64; + internal const int DefaultRecursionLimit = 100; internal const int DefaultSizeLimit = Int32.MaxValue; internal const int BufferSize = 4096; @@ -260,7 +260,7 @@ namespace Google.Protobuf /// to avoid maliciously-recursive data. /// /// - /// The default limit is 64. + /// The default limit is 100. /// /// /// The recursion limit for this stream. @@ -272,6 +272,11 @@ namespace Google.Protobuf /// internal bool DiscardUnknownFields { get; set; } + /// + /// Internal-only property; provides extension identifiers to compatible messages while parsing. + /// + internal ExtensionRegistry ExtensionRegistry { get; set; } + /// /// Disposes of this instance, potentially closing any underlying stream. /// @@ -373,7 +378,7 @@ namespace Google.Protobuf if (IsAtEnd) { lastTag = 0; - return 0; // This is the only case in which we return 0. + return 0; } lastTag = ReadRawVarint32(); @@ -383,6 +388,10 @@ namespace Google.Protobuf // If we actually read a tag with a field of 0, that's not a valid tag. throw InvalidProtocolBufferException.InvalidTag(); } + if (ReachedLimit) + { + return 0; + } return lastTag; } @@ -556,7 +565,7 @@ namespace Google.Protobuf { return ""; } - if (length <= bufferSize - bufferPos) + if (length <= bufferSize - bufferPos && length > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. @@ -570,7 +579,7 @@ namespace Google.Protobuf /// /// Reads an embedded message field value from the stream. - /// + /// public void ReadMessage(IMessage builder) { int length = ReadLength(); @@ -591,6 +600,20 @@ namespace Google.Protobuf PopLimit(oldLimit); } + /// + /// Reads an embedded group field from the stream. + /// + public void ReadGroup(IMessage builder) + { + if (recursionDepth >= recursionLimit) + { + throw InvalidProtocolBufferException.RecursionLimitExceeded(); + } + ++recursionDepth; + builder.MergeFrom(this); + --recursionDepth; + } + /// /// Reads a bytes field value from the stream. /// diff --git a/csharp/src/Google.Protobuf/CodedOutputStream.ComputeSize.cs b/csharp/src/Google.Protobuf/CodedOutputStream.ComputeSize.cs index bf221c9c0a..aa7932470b 100644 --- a/csharp/src/Google.Protobuf/CodedOutputStream.ComputeSize.cs +++ b/csharp/src/Google.Protobuf/CodedOutputStream.ComputeSize.cs @@ -42,13 +42,17 @@ namespace Google.Protobuf private const int LittleEndian64Size = 8; private const int LittleEndian32Size = 4; + internal const int DoubleSize = LittleEndian64Size; + internal const int FloatSize = LittleEndian32Size; + internal const int BoolSize = 1; + /// /// Computes the number of bytes that would be needed to encode a /// double field, including the tag. /// public static int ComputeDoubleSize(double value) { - return LittleEndian64Size; + return DoubleSize; } /// @@ -57,7 +61,7 @@ namespace Google.Protobuf /// public static int ComputeFloatSize(float value) { - return LittleEndian32Size; + return FloatSize; } /// @@ -119,7 +123,7 @@ namespace Google.Protobuf /// public static int ComputeBoolSize(bool value) { - return 1; + return BoolSize; } /// diff --git a/csharp/src/Google.Protobuf/CodedOutputStream.cs b/csharp/src/Google.Protobuf/CodedOutputStream.cs index 6211aac32c..f9ad29086b 100644 --- a/csharp/src/Google.Protobuf/CodedOutputStream.cs +++ b/csharp/src/Google.Protobuf/CodedOutputStream.cs @@ -303,6 +303,15 @@ namespace Google.Protobuf value.WriteTo(this); } + /// + /// Writes a group, without a tag, to the stream. + /// + /// The value to write + public void WriteGroup(IMessage value) + { + value.WriteTo(this); + } + /// /// Write a byte string, without a tag, to the stream. /// The data is length-prefixed. diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index dbbcc14873..1924439e5c 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -1,771 +1,771 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 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. -#endregion - -using Google.Protobuf.Compatibility; -using Google.Protobuf.Reflection; -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace Google.Protobuf.Collections -{ - /// - /// Representation of a map field in a Protocol Buffer message. - /// - /// Key type in the map. Must be a type supported by Protocol Buffer map keys. - /// Value type in the map. Must be a type supported by Protocol Buffers. - /// - /// - /// For string keys, the equality comparison is provided by . - /// - /// - /// Null values are not permitted in the map, either for wrapper types or regular messages. - /// If a map is deserialized from a data stream and the value is missing from an entry, a default value - /// is created instead. For primitive types, that is the regular default value (0, the empty string and so - /// on); for message types, an empty instance of the message is created, as if the map entry contained a 0-length - /// encoded value for the field. - /// - /// - /// This implementation does not generally prohibit the use of key/value types which are not - /// supported by Protocol Buffers (e.g. using a key type of byte) but nor does it guarantee - /// that all operations will work in such cases. - /// - /// - /// The order in which entries are returned when iterating over this object is undefined, and may change - /// in future versions. - /// - /// - public sealed class MapField : IDeepCloneable>, IDictionary, IEquatable>, IDictionary -#if !NET35 - , IReadOnlyDictionary -#endif - { - private static readonly EqualityComparer ValueEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer(); - private static readonly EqualityComparer KeyEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer(); - - // TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.) - private readonly Dictionary>> map = - new Dictionary>>(KeyEqualityComparer); - private readonly LinkedList> list = new LinkedList>(); - - /// - /// Creates a deep clone of this object. - /// - /// - /// A deep clone of this object. - /// - public MapField Clone() - { - var clone = new MapField(); - // Keys are never cloneable. Values might be. - if (typeof(IDeepCloneable).IsAssignableFrom(typeof(TValue))) - { - foreach (var pair in list) - { - clone.Add(pair.Key, ((IDeepCloneable)pair.Value).Clone()); - } - } - else - { - // Nothing is cloneable, so we don't need to worry. - clone.Add(this); - } - return clone; - } - - /// - /// Adds the specified key/value pair to the map. - /// - /// - /// This operation fails if the key already exists in the map. To replace an existing entry, use the indexer. - /// - /// The key to add - /// The value to add. - /// The given key already exists in map. - public void Add(TKey key, TValue value) - { - // Validation of arguments happens in ContainsKey and the indexer - if (ContainsKey(key)) - { - throw new ArgumentException("Key already exists in map", nameof(key)); - } - this[key] = value; - } - - /// - /// Determines whether the specified key is present in the map. - /// - /// The key to check. - /// true if the map contains the given key; false otherwise. - public bool ContainsKey(TKey key) - { - ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); - return map.ContainsKey(key); - } - - private bool ContainsValue(TValue value) => - list.Any(pair => ValueEqualityComparer.Equals(pair.Value, value)); - - /// - /// Removes the entry identified by the given key from the map. - /// - /// The key indicating the entry to remove from the map. - /// true if the map contained the given key before the entry was removed; false otherwise. - public bool Remove(TKey key) - { - ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); - LinkedListNode> node; - if (map.TryGetValue(key, out node)) - { - map.Remove(key); - node.List.Remove(node); - return true; - } - else - { - return false; - } - } - - /// - /// Gets the value associated with the specified key. - /// - /// The key whose value to get. - /// When this method returns, the value associated with the specified key, if the key is found; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// true if the map contains an element with the specified key; otherwise, false. - public bool TryGetValue(TKey key, out TValue value) - { - LinkedListNode> node; - if (map.TryGetValue(key, out node)) - { - value = node.Value.Value; - return true; - } - else - { - value = default(TValue); - return false; - } - } - - /// - /// Gets or sets the value associated with the specified key. - /// - /// The key of the value to get or set. - /// The property is retrieved and key does not exist in the collection. - /// The value associated with the specified key. If the specified key is not found, - /// a get operation throws a , and a set operation creates a new element with the specified key. - public TValue this[TKey key] - { - get - { - ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); - TValue value; - if (TryGetValue(key, out value)) - { - return value; - } - throw new KeyNotFoundException(); - } - set - { - ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); - // value == null check here is redundant, but avoids boxing. - if (value == null) - { - ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value)); - } - LinkedListNode> node; - var pair = new KeyValuePair(key, value); - if (map.TryGetValue(key, out node)) - { - node.Value = pair; - } - else - { - node = list.AddLast(pair); - map[key] = node; - } - } - } - - /// - /// Gets a collection containing the keys in the map. - /// - public ICollection Keys { get { return new MapView(this, pair => pair.Key, ContainsKey); } } - - /// - /// Gets a collection containing the values in the map. - /// - public ICollection Values { get { return new MapView(this, pair => pair.Value, ContainsValue); } } - - /// - /// Adds the specified entries to the map. The keys and values are not automatically cloned. - /// - /// The entries to add to the map. - public void Add(IDictionary entries) - { - ProtoPreconditions.CheckNotNull(entries, nameof(entries)); - foreach (var pair in entries) - { - Add(pair.Key, pair.Value); - } - } - - /// - /// Returns an enumerator that iterates through the collection. - /// - /// - /// An enumerator that can be used to iterate through the collection. - /// - public IEnumerator> GetEnumerator() - { - return list.GetEnumerator(); - } - - /// - /// Returns an enumerator that iterates through a collection. - /// - /// - /// An object that can be used to iterate through the collection. - /// - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - /// - /// Adds the specified item to the map. - /// - /// The item to add to the map. - void ICollection>.Add(KeyValuePair item) - { - Add(item.Key, item.Value); - } - - /// - /// Removes all items from the map. - /// - public void Clear() - { - list.Clear(); - map.Clear(); - } - - /// - /// Determines whether map contains an entry equivalent to the given key/value pair. - /// - /// The key/value pair to find. - /// - bool ICollection>.Contains(KeyValuePair item) - { - TValue value; - return TryGetValue(item.Key, out value) && ValueEqualityComparer.Equals(item.Value, value); - } - - /// - /// Copies the key/value pairs in this map to an array. - /// - /// The array to copy the entries into. - /// The index of the array at which to start copying values. - void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) - { - list.CopyTo(array, arrayIndex); - } - - /// - /// Removes the specified key/value pair from the map. - /// - /// Both the key and the value must be found for the entry to be removed. - /// The key/value pair to remove. - /// true if the key/value pair was found and removed; false otherwise. - bool ICollection>.Remove(KeyValuePair item) - { - if (item.Key == null) - { - throw new ArgumentException("Key is null", nameof(item)); - } - LinkedListNode> node; - if (map.TryGetValue(item.Key, out node) && - EqualityComparer.Default.Equals(item.Value, node.Value.Value)) - { - map.Remove(item.Key); - node.List.Remove(node); - return true; - } - else - { - return false; - } - } - - /// - /// Gets the number of elements contained in the map. - /// - public int Count { get { return list.Count; } } - - /// - /// Gets a value indicating whether the map is read-only. - /// - public bool IsReadOnly { get { return false; } } - - /// - /// Determines whether the specified , is equal to this instance. - /// - /// The to compare with this instance. - /// - /// true if the specified is equal to this instance; otherwise, false. - /// - public override bool Equals(object other) - { - return Equals(other as MapField); - } - - /// - /// Returns a hash code for this instance. - /// - /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - /// - public override int GetHashCode() - { - var keyComparer = KeyEqualityComparer; - var valueComparer = ValueEqualityComparer; - int hash = 0; - foreach (var pair in list) - { - hash ^= keyComparer.GetHashCode(pair.Key) * 31 + valueComparer.GetHashCode(pair.Value); - } - return hash; - } - - /// - /// Compares this map with another for equality. - /// - /// - /// The order of the key/value pairs in the maps is not deemed significant in this comparison. - /// - /// The map to compare this with. - /// true if refers to an equal map; false otherwise. - public bool Equals(MapField other) - { - if (other == null) - { - return false; - } - if (other == this) - { - return true; - } - if (other.Count != this.Count) - { - return false; - } - var valueComparer = ValueEqualityComparer; - foreach (var pair in this) - { - TValue value; - if (!other.TryGetValue(pair.Key, out value)) - { - return false; - } - if (!valueComparer.Equals(value, pair.Value)) - { - return false; - } - } - return true; - } - - /// - /// Adds entries to the map from the given stream. - /// - /// - /// It is assumed that the stream is initially positioned after the tag specified by the codec. - /// This method will continue reading entries from the stream until the end is reached, or - /// a different tag is encountered. - /// - /// Stream to read from - /// Codec describing how the key/value pairs are encoded - public void AddEntriesFrom(CodedInputStream input, Codec codec) - { - var adapter = new Codec.MessageAdapter(codec); - do - { - adapter.Reset(); - input.ReadMessage(adapter); - this[adapter.Key] = adapter.Value; - } while (input.MaybeConsumeTag(codec.MapTag)); - } - - /// - /// Writes the contents of this map to the given coded output stream, using the specified codec - /// to encode each entry. - /// - /// The output stream to write to. - /// The codec to use for each entry. - public void WriteTo(CodedOutputStream output, Codec codec) - { - var message = new Codec.MessageAdapter(codec); - foreach (var entry in list) - { - message.Key = entry.Key; - message.Value = entry.Value; - output.WriteTag(codec.MapTag); - output.WriteMessage(message); - } - } - - /// - /// Calculates the size of this map based on the given entry codec. - /// - /// The codec to use to encode each entry. - /// - public int CalculateSize(Codec codec) - { - if (Count == 0) - { - return 0; - } - var message = new Codec.MessageAdapter(codec); - int size = 0; - foreach (var entry in list) - { - message.Key = entry.Key; - message.Value = entry.Value; - size += CodedOutputStream.ComputeRawVarint32Size(codec.MapTag); - size += CodedOutputStream.ComputeMessageSize(message); - } - return size; - } - - /// - /// Returns a string representation of this repeated field, in the same - /// way as it would be represented by the default JSON formatter. - /// - public override string ToString() - { - var writer = new StringWriter(); - JsonFormatter.Default.WriteDictionary(writer, this); - return writer.ToString(); - } - - #region IDictionary explicit interface implementation - void IDictionary.Add(object key, object value) - { - Add((TKey)key, (TValue)value); - } - - bool IDictionary.Contains(object key) - { - if (!(key is TKey)) - { - return false; - } - return ContainsKey((TKey)key); - } - - IDictionaryEnumerator IDictionary.GetEnumerator() - { - return new DictionaryEnumerator(GetEnumerator()); - } - - void IDictionary.Remove(object key) - { - ProtoPreconditions.CheckNotNull(key, nameof(key)); - if (!(key is TKey)) - { - return; - } - Remove((TKey)key); - } - - void ICollection.CopyTo(Array array, int index) - { - // This is ugly and slow as heck, but with any luck it will never be used anyway. - ICollection temp = this.Select(pair => new DictionaryEntry(pair.Key, pair.Value)).ToList(); - temp.CopyTo(array, index); - } - - bool IDictionary.IsFixedSize { get { return false; } } - - ICollection IDictionary.Keys { get { return (ICollection)Keys; } } - - ICollection IDictionary.Values { get { return (ICollection)Values; } } - - bool ICollection.IsSynchronized { get { return false; } } - - object ICollection.SyncRoot { get { return this; } } - - object IDictionary.this[object key] - { - get - { - ProtoPreconditions.CheckNotNull(key, nameof(key)); - if (!(key is TKey)) - { - return null; - } - TValue value; - TryGetValue((TKey)key, out value); - return value; - } - - set - { - this[(TKey)key] = (TValue)value; - } - } - #endregion - - #region IReadOnlyDictionary explicit interface implementation -#if !NET35 - IEnumerable IReadOnlyDictionary.Keys => Keys; - - IEnumerable IReadOnlyDictionary.Values => Values; -#endif - #endregion - - private class DictionaryEnumerator : IDictionaryEnumerator - { - private readonly IEnumerator> enumerator; - - internal DictionaryEnumerator(IEnumerator> enumerator) - { - this.enumerator = enumerator; - } - - public bool MoveNext() - { - return enumerator.MoveNext(); - } - - public void Reset() - { - enumerator.Reset(); - } - - public object Current { get { return Entry; } } - public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } } - public object Key { get { return enumerator.Current.Key; } } - public object Value { get { return enumerator.Current.Value; } } - } - - /// - /// A codec for a specific map field. This contains all the information required to encode and - /// decode the nested messages. - /// - public sealed class Codec - { - private readonly FieldCodec keyCodec; - private readonly FieldCodec valueCodec; - private readonly uint mapTag; - - /// - /// Creates a new entry codec based on a separate key codec and value codec, - /// and the tag to use for each map entry. - /// - /// The key codec. - /// The value codec. - /// The map tag to use to introduce each map entry. - public Codec(FieldCodec keyCodec, FieldCodec valueCodec, uint mapTag) - { - this.keyCodec = keyCodec; - this.valueCodec = valueCodec; - this.mapTag = mapTag; - } - - /// - /// The tag used in the enclosing message to indicate map entries. - /// - internal uint MapTag { get { return mapTag; } } - - /// - /// A mutable message class, used for parsing and serializing. This - /// delegates the work to a codec, but implements the interface - /// for interop with and . - /// This is nested inside Codec as it's tightly coupled to the associated codec, - /// and it's simpler if it has direct access to all its fields. - /// - internal class MessageAdapter : IMessage - { - private static readonly byte[] ZeroLengthMessageStreamData = new byte[] { 0 }; - - private readonly Codec codec; - internal TKey Key { get; set; } - internal TValue Value { get; set; } - - internal MessageAdapter(Codec codec) - { - this.codec = codec; - } - - internal void Reset() - { - Key = codec.keyCodec.DefaultValue; - Value = codec.valueCodec.DefaultValue; - } - - public void MergeFrom(CodedInputStream input) - { - uint tag; - while ((tag = input.ReadTag()) != 0) - { - if (tag == codec.keyCodec.Tag) - { - Key = codec.keyCodec.Read(input); - } - else if (tag == codec.valueCodec.Tag) - { - Value = codec.valueCodec.Read(input); - } - else - { - input.SkipLastField(); - } - } - - // Corner case: a map entry with a key but no value, where the value type is a message. - // Read it as if we'd seen an input stream with no data (i.e. create a "default" message). - if (Value == null) - { - Value = codec.valueCodec.Read(new CodedInputStream(ZeroLengthMessageStreamData)); - } - } - - public void WriteTo(CodedOutputStream output) - { - codec.keyCodec.WriteTagAndValue(output, Key); - codec.valueCodec.WriteTagAndValue(output, Value); - } - - public int CalculateSize() - { - return codec.keyCodec.CalculateSizeWithTag(Key) + codec.valueCodec.CalculateSizeWithTag(Value); - } - - MessageDescriptor IMessage.Descriptor { get { return null; } } - } - } - - private class MapView : ICollection, ICollection - { - private readonly MapField parent; - private readonly Func, T> projection; - private readonly Func containsCheck; - - internal MapView( - MapField parent, - Func, T> projection, - Func containsCheck) - { - this.parent = parent; - this.projection = projection; - this.containsCheck = containsCheck; - } - - public int Count { get { return parent.Count; } } - - public bool IsReadOnly { get { return true; } } - - public bool IsSynchronized { get { return false; } } - - public object SyncRoot { get { return parent; } } - - public void Add(T item) - { - throw new NotSupportedException(); - } - - public void Clear() - { - throw new NotSupportedException(); - } - - public bool Contains(T item) - { - return containsCheck(item); - } - - public void CopyTo(T[] array, int arrayIndex) - { - if (arrayIndex < 0) - { - throw new ArgumentOutOfRangeException(nameof(arrayIndex)); - } - if (arrayIndex + Count > array.Length) - { - throw new ArgumentException("Not enough space in the array", nameof(array)); - } - foreach (var item in this) - { - array[arrayIndex++] = item; - } - } - - public IEnumerator GetEnumerator() - { - return parent.list.Select(projection).GetEnumerator(); - } - - public bool Remove(T item) - { - throw new NotSupportedException(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - public void CopyTo(Array array, int index) - { - if (index < 0) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - if (index + Count > array.Length) - { - throw new ArgumentException("Not enough space in the array", nameof(array)); - } - foreach (var item in this) - { - array.SetValue(item, index++); - } - } - } - } -} +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 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. +#endregion + +using Google.Protobuf.Compatibility; +using Google.Protobuf.Reflection; +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace Google.Protobuf.Collections +{ + /// + /// Representation of a map field in a Protocol Buffer message. + /// + /// Key type in the map. Must be a type supported by Protocol Buffer map keys. + /// Value type in the map. Must be a type supported by Protocol Buffers. + /// + /// + /// For string keys, the equality comparison is provided by . + /// + /// + /// Null values are not permitted in the map, either for wrapper types or regular messages. + /// If a map is deserialized from a data stream and the value is missing from an entry, a default value + /// is created instead. For primitive types, that is the regular default value (0, the empty string and so + /// on); for message types, an empty instance of the message is created, as if the map entry contained a 0-length + /// encoded value for the field. + /// + /// + /// This implementation does not generally prohibit the use of key/value types which are not + /// supported by Protocol Buffers (e.g. using a key type of byte) but nor does it guarantee + /// that all operations will work in such cases. + /// + /// + /// The order in which entries are returned when iterating over this object is undefined, and may change + /// in future versions. + /// + /// + public sealed class MapField : IDeepCloneable>, IDictionary, IEquatable>, IDictionary +#if !NET35 + , IReadOnlyDictionary +#endif + { + private static readonly EqualityComparer ValueEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer(); + private static readonly EqualityComparer KeyEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer(); + + // TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.) + private readonly Dictionary>> map = + new Dictionary>>(KeyEqualityComparer); + private readonly LinkedList> list = new LinkedList>(); + + /// + /// Creates a deep clone of this object. + /// + /// + /// A deep clone of this object. + /// + public MapField Clone() + { + var clone = new MapField(); + // Keys are never cloneable. Values might be. + if (typeof(IDeepCloneable).IsAssignableFrom(typeof(TValue))) + { + foreach (var pair in list) + { + clone.Add(pair.Key, ((IDeepCloneable)pair.Value).Clone()); + } + } + else + { + // Nothing is cloneable, so we don't need to worry. + clone.Add(this); + } + return clone; + } + + /// + /// Adds the specified key/value pair to the map. + /// + /// + /// This operation fails if the key already exists in the map. To replace an existing entry, use the indexer. + /// + /// The key to add + /// The value to add. + /// The given key already exists in map. + public void Add(TKey key, TValue value) + { + // Validation of arguments happens in ContainsKey and the indexer + if (ContainsKey(key)) + { + throw new ArgumentException("Key already exists in map", nameof(key)); + } + this[key] = value; + } + + /// + /// Determines whether the specified key is present in the map. + /// + /// The key to check. + /// true if the map contains the given key; false otherwise. + public bool ContainsKey(TKey key) + { + ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); + return map.ContainsKey(key); + } + + private bool ContainsValue(TValue value) => + list.Any(pair => ValueEqualityComparer.Equals(pair.Value, value)); + + /// + /// Removes the entry identified by the given key from the map. + /// + /// The key indicating the entry to remove from the map. + /// true if the map contained the given key before the entry was removed; false otherwise. + public bool Remove(TKey key) + { + ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); + LinkedListNode> node; + if (map.TryGetValue(key, out node)) + { + map.Remove(key); + node.List.Remove(node); + return true; + } + else + { + return false; + } + } + + /// + /// Gets the value associated with the specified key. + /// + /// The key whose value to get. + /// When this method returns, the value associated with the specified key, if the key is found; + /// otherwise, the default value for the type of the parameter. + /// This parameter is passed uninitialized. + /// true if the map contains an element with the specified key; otherwise, false. + public bool TryGetValue(TKey key, out TValue value) + { + LinkedListNode> node; + if (map.TryGetValue(key, out node)) + { + value = node.Value.Value; + return true; + } + else + { + value = default(TValue); + return false; + } + } + + /// + /// Gets or sets the value associated with the specified key. + /// + /// The key of the value to get or set. + /// The property is retrieved and key does not exist in the collection. + /// The value associated with the specified key. If the specified key is not found, + /// a get operation throws a , and a set operation creates a new element with the specified key. + public TValue this[TKey key] + { + get + { + ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); + TValue value; + if (TryGetValue(key, out value)) + { + return value; + } + throw new KeyNotFoundException(); + } + set + { + ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); + // value == null check here is redundant, but avoids boxing. + if (value == null) + { + ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value)); + } + LinkedListNode> node; + var pair = new KeyValuePair(key, value); + if (map.TryGetValue(key, out node)) + { + node.Value = pair; + } + else + { + node = list.AddLast(pair); + map[key] = node; + } + } + } + + /// + /// Gets a collection containing the keys in the map. + /// + public ICollection Keys { get { return new MapView(this, pair => pair.Key, ContainsKey); } } + + /// + /// Gets a collection containing the values in the map. + /// + public ICollection Values { get { return new MapView(this, pair => pair.Value, ContainsValue); } } + + /// + /// Adds the specified entries to the map. The keys and values are not automatically cloned. + /// + /// The entries to add to the map. + public void Add(IDictionary entries) + { + ProtoPreconditions.CheckNotNull(entries, nameof(entries)); + foreach (var pair in entries) + { + Add(pair.Key, pair.Value); + } + } + + /// + /// Returns an enumerator that iterates through the collection. + /// + /// + /// An enumerator that can be used to iterate through the collection. + /// + public IEnumerator> GetEnumerator() + { + return list.GetEnumerator(); + } + + /// + /// Returns an enumerator that iterates through a collection. + /// + /// + /// An object that can be used to iterate through the collection. + /// + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// Adds the specified item to the map. + /// + /// The item to add to the map. + void ICollection>.Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// Removes all items from the map. + /// + public void Clear() + { + list.Clear(); + map.Clear(); + } + + /// + /// Determines whether map contains an entry equivalent to the given key/value pair. + /// + /// The key/value pair to find. + /// + bool ICollection>.Contains(KeyValuePair item) + { + TValue value; + return TryGetValue(item.Key, out value) && ValueEqualityComparer.Equals(item.Value, value); + } + + /// + /// Copies the key/value pairs in this map to an array. + /// + /// The array to copy the entries into. + /// The index of the array at which to start copying values. + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) + { + list.CopyTo(array, arrayIndex); + } + + /// + /// Removes the specified key/value pair from the map. + /// + /// Both the key and the value must be found for the entry to be removed. + /// The key/value pair to remove. + /// true if the key/value pair was found and removed; false otherwise. + bool ICollection>.Remove(KeyValuePair item) + { + if (item.Key == null) + { + throw new ArgumentException("Key is null", nameof(item)); + } + LinkedListNode> node; + if (map.TryGetValue(item.Key, out node) && + EqualityComparer.Default.Equals(item.Value, node.Value.Value)) + { + map.Remove(item.Key); + node.List.Remove(node); + return true; + } + else + { + return false; + } + } + + /// + /// Gets the number of elements contained in the map. + /// + public int Count { get { return list.Count; } } + + /// + /// Gets a value indicating whether the map is read-only. + /// + public bool IsReadOnly { get { return false; } } + + /// + /// Determines whether the specified , is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(object other) + { + return Equals(other as MapField); + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + public override int GetHashCode() + { + var keyComparer = KeyEqualityComparer; + var valueComparer = ValueEqualityComparer; + int hash = 0; + foreach (var pair in list) + { + hash ^= keyComparer.GetHashCode(pair.Key) * 31 + valueComparer.GetHashCode(pair.Value); + } + return hash; + } + + /// + /// Compares this map with another for equality. + /// + /// + /// The order of the key/value pairs in the maps is not deemed significant in this comparison. + /// + /// The map to compare this with. + /// true if refers to an equal map; false otherwise. + public bool Equals(MapField other) + { + if (other == null) + { + return false; + } + if (other == this) + { + return true; + } + if (other.Count != this.Count) + { + return false; + } + var valueComparer = ValueEqualityComparer; + foreach (var pair in this) + { + TValue value; + if (!other.TryGetValue(pair.Key, out value)) + { + return false; + } + if (!valueComparer.Equals(value, pair.Value)) + { + return false; + } + } + return true; + } + + /// + /// Adds entries to the map from the given stream. + /// + /// + /// It is assumed that the stream is initially positioned after the tag specified by the codec. + /// This method will continue reading entries from the stream until the end is reached, or + /// a different tag is encountered. + /// + /// Stream to read from + /// Codec describing how the key/value pairs are encoded + public void AddEntriesFrom(CodedInputStream input, Codec codec) + { + var adapter = new Codec.MessageAdapter(codec); + do + { + adapter.Reset(); + input.ReadMessage(adapter); + this[adapter.Key] = adapter.Value; + } while (input.MaybeConsumeTag(codec.MapTag)); + } + + /// + /// Writes the contents of this map to the given coded output stream, using the specified codec + /// to encode each entry. + /// + /// The output stream to write to. + /// The codec to use for each entry. + public void WriteTo(CodedOutputStream output, Codec codec) + { + var message = new Codec.MessageAdapter(codec); + foreach (var entry in list) + { + message.Key = entry.Key; + message.Value = entry.Value; + output.WriteTag(codec.MapTag); + output.WriteMessage(message); + } + } + + /// + /// Calculates the size of this map based on the given entry codec. + /// + /// The codec to use to encode each entry. + /// + public int CalculateSize(Codec codec) + { + if (Count == 0) + { + return 0; + } + var message = new Codec.MessageAdapter(codec); + int size = 0; + foreach (var entry in list) + { + message.Key = entry.Key; + message.Value = entry.Value; + size += CodedOutputStream.ComputeRawVarint32Size(codec.MapTag); + size += CodedOutputStream.ComputeMessageSize(message); + } + return size; + } + + /// + /// Returns a string representation of this repeated field, in the same + /// way as it would be represented by the default JSON formatter. + /// + public override string ToString() + { + var writer = new StringWriter(); + JsonFormatter.Default.WriteDictionary(writer, this); + return writer.ToString(); + } + + #region IDictionary explicit interface implementation + void IDictionary.Add(object key, object value) + { + Add((TKey)key, (TValue)value); + } + + bool IDictionary.Contains(object key) + { + if (!(key is TKey)) + { + return false; + } + return ContainsKey((TKey)key); + } + + IDictionaryEnumerator IDictionary.GetEnumerator() + { + return new DictionaryEnumerator(GetEnumerator()); + } + + void IDictionary.Remove(object key) + { + ProtoPreconditions.CheckNotNull(key, nameof(key)); + if (!(key is TKey)) + { + return; + } + Remove((TKey)key); + } + + void ICollection.CopyTo(Array array, int index) + { + // This is ugly and slow as heck, but with any luck it will never be used anyway. + ICollection temp = this.Select(pair => new DictionaryEntry(pair.Key, pair.Value)).ToList(); + temp.CopyTo(array, index); + } + + bool IDictionary.IsFixedSize { get { return false; } } + + ICollection IDictionary.Keys { get { return (ICollection)Keys; } } + + ICollection IDictionary.Values { get { return (ICollection)Values; } } + + bool ICollection.IsSynchronized { get { return false; } } + + object ICollection.SyncRoot { get { return this; } } + + object IDictionary.this[object key] + { + get + { + ProtoPreconditions.CheckNotNull(key, nameof(key)); + if (!(key is TKey)) + { + return null; + } + TValue value; + TryGetValue((TKey)key, out value); + return value; + } + + set + { + this[(TKey)key] = (TValue)value; + } + } + #endregion + + #region IReadOnlyDictionary explicit interface implementation +#if !NET35 + IEnumerable IReadOnlyDictionary.Keys => Keys; + + IEnumerable IReadOnlyDictionary.Values => Values; +#endif + #endregion + + private class DictionaryEnumerator : IDictionaryEnumerator + { + private readonly IEnumerator> enumerator; + + internal DictionaryEnumerator(IEnumerator> enumerator) + { + this.enumerator = enumerator; + } + + public bool MoveNext() + { + return enumerator.MoveNext(); + } + + public void Reset() + { + enumerator.Reset(); + } + + public object Current { get { return Entry; } } + public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } } + public object Key { get { return enumerator.Current.Key; } } + public object Value { get { return enumerator.Current.Value; } } + } + + /// + /// A codec for a specific map field. This contains all the information required to encode and + /// decode the nested messages. + /// + public sealed class Codec + { + private readonly FieldCodec keyCodec; + private readonly FieldCodec valueCodec; + private readonly uint mapTag; + + /// + /// Creates a new entry codec based on a separate key codec and value codec, + /// and the tag to use for each map entry. + /// + /// The key codec. + /// The value codec. + /// The map tag to use to introduce each map entry. + public Codec(FieldCodec keyCodec, FieldCodec valueCodec, uint mapTag) + { + this.keyCodec = keyCodec; + this.valueCodec = valueCodec; + this.mapTag = mapTag; + } + + /// + /// The tag used in the enclosing message to indicate map entries. + /// + internal uint MapTag { get { return mapTag; } } + + /// + /// A mutable message class, used for parsing and serializing. This + /// delegates the work to a codec, but implements the interface + /// for interop with and . + /// This is nested inside Codec as it's tightly coupled to the associated codec, + /// and it's simpler if it has direct access to all its fields. + /// + internal class MessageAdapter : IMessage + { + private static readonly byte[] ZeroLengthMessageStreamData = new byte[] { 0 }; + + private readonly Codec codec; + internal TKey Key { get; set; } + internal TValue Value { get; set; } + + internal MessageAdapter(Codec codec) + { + this.codec = codec; + } + + internal void Reset() + { + Key = codec.keyCodec.DefaultValue; + Value = codec.valueCodec.DefaultValue; + } + + public void MergeFrom(CodedInputStream input) + { + uint tag; + while ((tag = input.ReadTag()) != 0) + { + if (tag == codec.keyCodec.Tag) + { + Key = codec.keyCodec.Read(input); + } + else if (tag == codec.valueCodec.Tag) + { + Value = codec.valueCodec.Read(input); + } + else + { + input.SkipLastField(); + } + } + + // Corner case: a map entry with a key but no value, where the value type is a message. + // Read it as if we'd seen an input stream with no data (i.e. create a "default" message). + if (Value == null) + { + Value = codec.valueCodec.Read(new CodedInputStream(ZeroLengthMessageStreamData)); + } + } + + public void WriteTo(CodedOutputStream output) + { + codec.keyCodec.WriteTagAndValue(output, Key); + codec.valueCodec.WriteTagAndValue(output, Value); + } + + public int CalculateSize() + { + return codec.keyCodec.CalculateSizeWithTag(Key) + codec.valueCodec.CalculateSizeWithTag(Value); + } + + MessageDescriptor IMessage.Descriptor { get { return null; } } + } + } + + private class MapView : ICollection, ICollection + { + private readonly MapField parent; + private readonly Func, T> projection; + private readonly Func containsCheck; + + internal MapView( + MapField parent, + Func, T> projection, + Func containsCheck) + { + this.parent = parent; + this.projection = projection; + this.containsCheck = containsCheck; + } + + public int Count { get { return parent.Count; } } + + public bool IsReadOnly { get { return true; } } + + public bool IsSynchronized { get { return false; } } + + public object SyncRoot { get { return parent; } } + + public void Add(T item) + { + throw new NotSupportedException(); + } + + public void Clear() + { + throw new NotSupportedException(); + } + + public bool Contains(T item) + { + return containsCheck(item); + } + + public void CopyTo(T[] array, int arrayIndex) + { + if (arrayIndex < 0) + { + throw new ArgumentOutOfRangeException(nameof(arrayIndex)); + } + if (arrayIndex + Count > array.Length) + { + throw new ArgumentException("Not enough space in the array", nameof(array)); + } + foreach (var item in this) + { + array[arrayIndex++] = item; + } + } + + public IEnumerator GetEnumerator() + { + return parent.list.Select(projection).GetEnumerator(); + } + + public bool Remove(T item) + { + throw new NotSupportedException(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void CopyTo(Array array, int index) + { + if (index < 0) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + if (index + Count > array.Length) + { + throw new ArgumentException("Not enough space in the array", nameof(array)); + } + foreach (var item in this) + { + array.SetValue(item, index++); + } + } + } + } +} diff --git a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs index c18b63e23f..827bc71183 100644 --- a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs +++ b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs @@ -1,595 +1,599 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 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. -#endregion - -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; - -namespace Google.Protobuf.Collections -{ - /// - /// The contents of a repeated field: essentially, a collection with some extra - /// restrictions (no null values) and capabilities (deep cloning). - /// - /// - /// This implementation does not generally prohibit the use of types which are not - /// supported by Protocol Buffers but nor does it guarantee that all operations will work in such cases. - /// - /// The element type of the repeated field. - public sealed class RepeatedField : IList, IList, IDeepCloneable>, IEquatable> -#if !NET35 - , IReadOnlyList -#endif - { - private static readonly EqualityComparer EqualityComparer = ProtobufEqualityComparers.GetEqualityComparer(); - private static readonly T[] EmptyArray = new T[0]; - private const int MinArraySize = 8; - - private T[] array = EmptyArray; - private int count = 0; - - /// - /// Creates a deep clone of this repeated field. - /// - /// - /// If the field type is - /// a message type, each element is also cloned; otherwise, it is - /// assumed that the field type is primitive (including string and - /// bytes, both of which are immutable) and so a simple copy is - /// equivalent to a deep clone. - /// - /// A deep clone of this repeated field. - public RepeatedField Clone() - { - RepeatedField clone = new RepeatedField(); - if (array != EmptyArray) - { - clone.array = (T[])array.Clone(); - IDeepCloneable[] cloneableArray = clone.array as IDeepCloneable[]; - if (cloneableArray != null) - { - for (int i = 0; i < count; i++) - { - clone.array[i] = cloneableArray[i].Clone(); - } - } - } - clone.count = count; - return clone; - } - - /// - /// Adds the entries from the given input stream, decoding them with the specified codec. - /// - /// The input stream to read from. - /// The codec to use in order to read each entry. - public void AddEntriesFrom(CodedInputStream input, FieldCodec codec) - { - // TODO: Inline some of the Add code, so we can avoid checking the size on every - // iteration. - uint tag = input.LastTag; - var reader = codec.ValueReader; - // Non-nullable value types can be packed or not. - if (FieldCodec.IsPackedRepeatedField(tag)) - { - int length = input.ReadLength(); - if (length > 0) - { - int oldLimit = input.PushLimit(length); - while (!input.ReachedLimit) - { - Add(reader(input)); - } - input.PopLimit(oldLimit); - } - // Empty packed field. Odd, but valid - just ignore. - } - else - { - // Not packed... (possibly not packable) - do - { - Add(reader(input)); - } while (input.MaybeConsumeTag(tag)); - } - } - - /// - /// Calculates the size of this collection based on the given codec. - /// - /// The codec to use when encoding each field. - /// The number of bytes that would be written to a by , - /// using the same codec. - public int CalculateSize(FieldCodec codec) - { - if (count == 0) - { - return 0; - } - uint tag = codec.Tag; - if (codec.PackedRepeatedField) - { - int dataSize = CalculatePackedDataSize(codec); - return CodedOutputStream.ComputeRawVarint32Size(tag) + - CodedOutputStream.ComputeLengthSize(dataSize) + - dataSize; - } - else - { - var sizeCalculator = codec.ValueSizeCalculator; - int size = count * CodedOutputStream.ComputeRawVarint32Size(tag); - for (int i = 0; i < count; i++) - { - size += sizeCalculator(array[i]); - } - return size; - } - } - - private int CalculatePackedDataSize(FieldCodec codec) - { - int fixedSize = codec.FixedSize; - if (fixedSize == 0) - { - var calculator = codec.ValueSizeCalculator; - int tmp = 0; - for (int i = 0; i < count; i++) - { - tmp += calculator(array[i]); - } - return tmp; - } - else - { - return fixedSize * Count; - } - } - - /// - /// Writes the contents of this collection to the given , - /// encoding each value using the specified codec. - /// - /// The output stream to write to. - /// The codec to use when encoding each value. - public void WriteTo(CodedOutputStream output, FieldCodec codec) - { - if (count == 0) - { - return; - } - var writer = codec.ValueWriter; - var tag = codec.Tag; - if (codec.PackedRepeatedField) - { - // Packed primitive type - uint size = (uint)CalculatePackedDataSize(codec); - output.WriteTag(tag); - output.WriteRawVarint32(size); - for (int i = 0; i < count; i++) - { - writer(output, array[i]); - } - } - else - { - // Not packed: a simple tag/value pair for each value. - // Can't use codec.WriteTagAndValue, as that omits default values. - for (int i = 0; i < count; i++) - { - output.WriteTag(tag); - writer(output, array[i]); - } - } - } - - private void EnsureSize(int size) - { - if (array.Length < size) - { - size = Math.Max(size, MinArraySize); - int newSize = Math.Max(array.Length * 2, size); - var tmp = new T[newSize]; - Array.Copy(array, 0, tmp, 0, array.Length); - array = tmp; - } - } - - /// - /// Adds the specified item to the collection. - /// - /// The item to add. - public void Add(T item) - { - ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); - EnsureSize(count + 1); - array[count++] = item; - } - - /// - /// Removes all items from the collection. - /// - public void Clear() - { - array = EmptyArray; - count = 0; - } - - /// - /// Determines whether this collection contains the given item. - /// - /// The item to find. - /// true if this collection contains the given item; false otherwise. - public bool Contains(T item) - { - return IndexOf(item) != -1; - } - - /// - /// Copies this collection to the given array. - /// - /// The array to copy to. - /// The first index of the array to copy to. - public void CopyTo(T[] array, int arrayIndex) - { - Array.Copy(this.array, 0, array, arrayIndex, count); - } - - /// - /// Removes the specified item from the collection - /// - /// The item to remove. - /// true if the item was found and removed; false otherwise. - public bool Remove(T item) - { - int index = IndexOf(item); - if (index == -1) - { - return false; - } - Array.Copy(array, index + 1, array, index, count - index - 1); - count--; - array[count] = default(T); - return true; - } - - /// - /// Gets the number of elements contained in the collection. - /// - public int Count => count; - - /// - /// Gets a value indicating whether the collection is read-only. - /// - public bool IsReadOnly => false; - - /// - /// Adds all of the specified values into this collection. - /// - /// The values to add to this collection. - public void AddRange(IEnumerable values) - { - ProtoPreconditions.CheckNotNull(values, nameof(values)); - - // Optimization 1: If the collection we're adding is already a RepeatedField, - // we know the values are valid. - var otherRepeatedField = values as RepeatedField; - if (otherRepeatedField != null) - { - EnsureSize(count + otherRepeatedField.count); - Array.Copy(otherRepeatedField.array, 0, array, count, otherRepeatedField.count); - count += otherRepeatedField.count; - return; - } - - // Optimization 2: The collection is an ICollection, so we can expand - // just once and ask the collection to copy itself into the array. - var collection = values as ICollection; - if (collection != null) - { - var extraCount = collection.Count; - // For reference types and nullable value types, we need to check that there are no nulls - // present. (This isn't a thread-safe approach, but we don't advertise this is thread-safe.) - // We expect the JITter to optimize this test to true/false, so it's effectively conditional - // specialization. - if (default(T) == null) - { - // TODO: Measure whether iterating once to check and then letting the collection copy - // itself is faster or slower than iterating and adding as we go. For large - // collections this will not be great in terms of cache usage... but the optimized - // copy may be significantly faster than doing it one at a time. - foreach (var item in collection) - { - if (item == null) - { - throw new ArgumentException("Sequence contained null element", nameof(values)); - } - } - } - EnsureSize(count + extraCount); - collection.CopyTo(array, count); - count += extraCount; - return; - } - - // We *could* check for ICollection as well, but very very few collections implement - // ICollection but not ICollection. (HashSet does, for one...) - - // Fall back to a slower path of adding items one at a time. - foreach (T item in values) - { - Add(item); - } - } - - /// - /// Adds all of the specified values into this collection. This method is present to - /// allow repeated fields to be constructed from queries within collection initializers. - /// Within non-collection-initializer code, consider using the equivalent - /// method instead for clarity. - /// - /// The values to add to this collection. - public void Add(IEnumerable values) - { - AddRange(values); - } - - /// - /// Returns an enumerator that iterates through the collection. - /// - /// - /// An enumerator that can be used to iterate through the collection. - /// - public IEnumerator GetEnumerator() - { - for (int i = 0; i < count; i++) - { - yield return array[i]; - } - } - - /// - /// Determines whether the specified , is equal to this instance. - /// - /// The to compare with this instance. - /// - /// true if the specified is equal to this instance; otherwise, false. - /// - public override bool Equals(object obj) - { - return Equals(obj as RepeatedField); - } - - /// - /// Returns an enumerator that iterates through a collection. - /// - /// - /// An object that can be used to iterate through the collection. - /// - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - /// - /// Returns a hash code for this instance. - /// - /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - /// - public override int GetHashCode() - { - int hash = 0; - for (int i = 0; i < count; i++) - { - hash = hash * 31 + array[i].GetHashCode(); - } - return hash; - } - - /// - /// Compares this repeated field with another for equality. - /// - /// The repeated field to compare this with. - /// true if refers to an equal repeated field; false otherwise. - public bool Equals(RepeatedField other) - { - if (ReferenceEquals(other, null)) - { - return false; - } - if (ReferenceEquals(other, this)) - { - return true; - } - if (other.Count != this.Count) - { - return false; - } - EqualityComparer comparer = EqualityComparer; - for (int i = 0; i < count; i++) - { - if (!comparer.Equals(array[i], other.array[i])) - { - return false; - } - } - return true; - } - - /// - /// Returns the index of the given item within the collection, or -1 if the item is not - /// present. - /// - /// The item to find in the collection. - /// The zero-based index of the item, or -1 if it is not found. - public int IndexOf(T item) - { - ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); - EqualityComparer comparer = EqualityComparer; - for (int i = 0; i < count; i++) - { - if (comparer.Equals(array[i], item)) - { - return i; - } - } - return -1; - } - - /// - /// Inserts the given item at the specified index. - /// - /// The index at which to insert the item. - /// The item to insert. - public void Insert(int index, T item) - { - ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); - if (index < 0 || index > count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - EnsureSize(count + 1); - Array.Copy(array, index, array, index + 1, count - index); - array[index] = item; - count++; - } - - /// - /// Removes the item at the given index. - /// - /// The zero-based index of the item to remove. - public void RemoveAt(int index) - { - if (index < 0 || index >= count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - Array.Copy(array, index + 1, array, index, count - index - 1); - count--; - array[count] = default(T); - } - - /// - /// Returns a string representation of this repeated field, in the same - /// way as it would be represented by the default JSON formatter. - /// - public override string ToString() - { - var writer = new StringWriter(); - JsonFormatter.Default.WriteList(writer, this); - return writer.ToString(); - } - - /// - /// Gets or sets the item at the specified index. - /// - /// - /// The element at the specified index. - /// - /// The zero-based index of the element to get or set. - /// The item at the specified index. - public T this[int index] - { - get - { - if (index < 0 || index >= count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - return array[index]; - } - set - { - if (index < 0 || index >= count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value)); - array[index] = value; - } - } - - #region Explicit interface implementation for IList and ICollection. - bool IList.IsFixedSize => false; - - void ICollection.CopyTo(Array array, int index) - { - Array.Copy(this.array, 0, array, index, count); - } - - bool ICollection.IsSynchronized => false; - - object ICollection.SyncRoot => this; - - object IList.this[int index] - { - get { return this[index]; } - set { this[index] = (T)value; } - } - - int IList.Add(object value) - { - Add((T) value); - return count - 1; - } - - bool IList.Contains(object value) - { - return (value is T && Contains((T)value)); - } - - int IList.IndexOf(object value) - { - if (!(value is T)) - { - return -1; - } - return IndexOf((T)value); - } - - void IList.Insert(int index, object value) - { - Insert(index, (T) value); - } - - void IList.Remove(object value) - { - if (!(value is T)) - { - return; - } - Remove((T)value); - } - #endregion - } -} +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 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. +#endregion + +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; + +namespace Google.Protobuf.Collections +{ + /// + /// The contents of a repeated field: essentially, a collection with some extra + /// restrictions (no null values) and capabilities (deep cloning). + /// + /// + /// This implementation does not generally prohibit the use of types which are not + /// supported by Protocol Buffers but nor does it guarantee that all operations will work in such cases. + /// + /// The element type of the repeated field. + public sealed class RepeatedField : IList, IList, IDeepCloneable>, IEquatable> +#if !NET35 + , IReadOnlyList +#endif + { + private static readonly EqualityComparer EqualityComparer = ProtobufEqualityComparers.GetEqualityComparer(); + private static readonly T[] EmptyArray = new T[0]; + private const int MinArraySize = 8; + + private T[] array = EmptyArray; + private int count = 0; + + /// + /// Creates a deep clone of this repeated field. + /// + /// + /// If the field type is + /// a message type, each element is also cloned; otherwise, it is + /// assumed that the field type is primitive (including string and + /// bytes, both of which are immutable) and so a simple copy is + /// equivalent to a deep clone. + /// + /// A deep clone of this repeated field. + public RepeatedField Clone() + { + RepeatedField clone = new RepeatedField(); + if (array != EmptyArray) + { + clone.array = (T[])array.Clone(); + IDeepCloneable[] cloneableArray = clone.array as IDeepCloneable[]; + if (cloneableArray != null) + { + for (int i = 0; i < count; i++) + { + clone.array[i] = cloneableArray[i].Clone(); + } + } + } + clone.count = count; + return clone; + } + + /// + /// Adds the entries from the given input stream, decoding them with the specified codec. + /// + /// The input stream to read from. + /// The codec to use in order to read each entry. + public void AddEntriesFrom(CodedInputStream input, FieldCodec codec) + { + // TODO: Inline some of the Add code, so we can avoid checking the size on every + // iteration. + uint tag = input.LastTag; + var reader = codec.ValueReader; + // Non-nullable value types can be packed or not. + if (FieldCodec.IsPackedRepeatedField(tag)) + { + int length = input.ReadLength(); + if (length > 0) + { + int oldLimit = input.PushLimit(length); + while (!input.ReachedLimit) + { + Add(reader(input)); + } + input.PopLimit(oldLimit); + } + // Empty packed field. Odd, but valid - just ignore. + } + else + { + // Not packed... (possibly not packable) + do + { + Add(reader(input)); + } while (input.MaybeConsumeTag(tag)); + } + } + + /// + /// Calculates the size of this collection based on the given codec. + /// + /// The codec to use when encoding each field. + /// The number of bytes that would be written to a by , + /// using the same codec. + public int CalculateSize(FieldCodec codec) + { + if (count == 0) + { + return 0; + } + uint tag = codec.Tag; + if (codec.PackedRepeatedField) + { + int dataSize = CalculatePackedDataSize(codec); + return CodedOutputStream.ComputeRawVarint32Size(tag) + + CodedOutputStream.ComputeLengthSize(dataSize) + + dataSize; + } + else + { + var sizeCalculator = codec.ValueSizeCalculator; + int size = count * CodedOutputStream.ComputeRawVarint32Size(tag); + for (int i = 0; i < count; i++) + { + size += sizeCalculator(array[i]); + } + return size; + } + } + + private int CalculatePackedDataSize(FieldCodec codec) + { + int fixedSize = codec.FixedSize; + if (fixedSize == 0) + { + var calculator = codec.ValueSizeCalculator; + int tmp = 0; + for (int i = 0; i < count; i++) + { + tmp += calculator(array[i]); + } + return tmp; + } + else + { + return fixedSize * Count; + } + } + + /// + /// Writes the contents of this collection to the given , + /// encoding each value using the specified codec. + /// + /// The output stream to write to. + /// The codec to use when encoding each value. + public void WriteTo(CodedOutputStream output, FieldCodec codec) + { + if (count == 0) + { + return; + } + var writer = codec.ValueWriter; + var tag = codec.Tag; + if (codec.PackedRepeatedField) + { + // Packed primitive type + uint size = (uint)CalculatePackedDataSize(codec); + output.WriteTag(tag); + output.WriteRawVarint32(size); + for (int i = 0; i < count; i++) + { + writer(output, array[i]); + } + } + else + { + // Not packed: a simple tag/value pair for each value. + // Can't use codec.WriteTagAndValue, as that omits default values. + for (int i = 0; i < count; i++) + { + output.WriteTag(tag); + writer(output, array[i]); + if (codec.EndTag != 0) + { + output.WriteTag(codec.EndTag); + } + } + } + } + + private void EnsureSize(int size) + { + if (array.Length < size) + { + size = Math.Max(size, MinArraySize); + int newSize = Math.Max(array.Length * 2, size); + var tmp = new T[newSize]; + Array.Copy(array, 0, tmp, 0, array.Length); + array = tmp; + } + } + + /// + /// Adds the specified item to the collection. + /// + /// The item to add. + public void Add(T item) + { + ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); + EnsureSize(count + 1); + array[count++] = item; + } + + /// + /// Removes all items from the collection. + /// + public void Clear() + { + array = EmptyArray; + count = 0; + } + + /// + /// Determines whether this collection contains the given item. + /// + /// The item to find. + /// true if this collection contains the given item; false otherwise. + public bool Contains(T item) + { + return IndexOf(item) != -1; + } + + /// + /// Copies this collection to the given array. + /// + /// The array to copy to. + /// The first index of the array to copy to. + public void CopyTo(T[] array, int arrayIndex) + { + Array.Copy(this.array, 0, array, arrayIndex, count); + } + + /// + /// Removes the specified item from the collection + /// + /// The item to remove. + /// true if the item was found and removed; false otherwise. + public bool Remove(T item) + { + int index = IndexOf(item); + if (index == -1) + { + return false; + } + Array.Copy(array, index + 1, array, index, count - index - 1); + count--; + array[count] = default(T); + return true; + } + + /// + /// Gets the number of elements contained in the collection. + /// + public int Count => count; + + /// + /// Gets a value indicating whether the collection is read-only. + /// + public bool IsReadOnly => false; + + /// + /// Adds all of the specified values into this collection. + /// + /// The values to add to this collection. + public void AddRange(IEnumerable values) + { + ProtoPreconditions.CheckNotNull(values, nameof(values)); + + // Optimization 1: If the collection we're adding is already a RepeatedField, + // we know the values are valid. + var otherRepeatedField = values as RepeatedField; + if (otherRepeatedField != null) + { + EnsureSize(count + otherRepeatedField.count); + Array.Copy(otherRepeatedField.array, 0, array, count, otherRepeatedField.count); + count += otherRepeatedField.count; + return; + } + + // Optimization 2: The collection is an ICollection, so we can expand + // just once and ask the collection to copy itself into the array. + var collection = values as ICollection; + if (collection != null) + { + var extraCount = collection.Count; + // For reference types and nullable value types, we need to check that there are no nulls + // present. (This isn't a thread-safe approach, but we don't advertise this is thread-safe.) + // We expect the JITter to optimize this test to true/false, so it's effectively conditional + // specialization. + if (default(T) == null) + { + // TODO: Measure whether iterating once to check and then letting the collection copy + // itself is faster or slower than iterating and adding as we go. For large + // collections this will not be great in terms of cache usage... but the optimized + // copy may be significantly faster than doing it one at a time. + foreach (var item in collection) + { + if (item == null) + { + throw new ArgumentException("Sequence contained null element", nameof(values)); + } + } + } + EnsureSize(count + extraCount); + collection.CopyTo(array, count); + count += extraCount; + return; + } + + // We *could* check for ICollection as well, but very very few collections implement + // ICollection but not ICollection. (HashSet does, for one...) + + // Fall back to a slower path of adding items one at a time. + foreach (T item in values) + { + Add(item); + } + } + + /// + /// Adds all of the specified values into this collection. This method is present to + /// allow repeated fields to be constructed from queries within collection initializers. + /// Within non-collection-initializer code, consider using the equivalent + /// method instead for clarity. + /// + /// The values to add to this collection. + public void Add(IEnumerable values) + { + AddRange(values); + } + + /// + /// Returns an enumerator that iterates through the collection. + /// + /// + /// An enumerator that can be used to iterate through the collection. + /// + public IEnumerator GetEnumerator() + { + for (int i = 0; i < count; i++) + { + yield return array[i]; + } + } + + /// + /// Determines whether the specified , is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(object obj) + { + return Equals(obj as RepeatedField); + } + + /// + /// Returns an enumerator that iterates through a collection. + /// + /// + /// An object that can be used to iterate through the collection. + /// + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + public override int GetHashCode() + { + int hash = 0; + for (int i = 0; i < count; i++) + { + hash = hash * 31 + array[i].GetHashCode(); + } + return hash; + } + + /// + /// Compares this repeated field with another for equality. + /// + /// The repeated field to compare this with. + /// true if refers to an equal repeated field; false otherwise. + public bool Equals(RepeatedField other) + { + if (ReferenceEquals(other, null)) + { + return false; + } + if (ReferenceEquals(other, this)) + { + return true; + } + if (other.Count != this.Count) + { + return false; + } + EqualityComparer comparer = EqualityComparer; + for (int i = 0; i < count; i++) + { + if (!comparer.Equals(array[i], other.array[i])) + { + return false; + } + } + return true; + } + + /// + /// Returns the index of the given item within the collection, or -1 if the item is not + /// present. + /// + /// The item to find in the collection. + /// The zero-based index of the item, or -1 if it is not found. + public int IndexOf(T item) + { + ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); + EqualityComparer comparer = EqualityComparer; + for (int i = 0; i < count; i++) + { + if (comparer.Equals(array[i], item)) + { + return i; + } + } + return -1; + } + + /// + /// Inserts the given item at the specified index. + /// + /// The index at which to insert the item. + /// The item to insert. + public void Insert(int index, T item) + { + ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); + if (index < 0 || index > count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + EnsureSize(count + 1); + Array.Copy(array, index, array, index + 1, count - index); + array[index] = item; + count++; + } + + /// + /// Removes the item at the given index. + /// + /// The zero-based index of the item to remove. + public void RemoveAt(int index) + { + if (index < 0 || index >= count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + Array.Copy(array, index + 1, array, index, count - index - 1); + count--; + array[count] = default(T); + } + + /// + /// Returns a string representation of this repeated field, in the same + /// way as it would be represented by the default JSON formatter. + /// + public override string ToString() + { + var writer = new StringWriter(); + JsonFormatter.Default.WriteList(writer, this); + return writer.ToString(); + } + + /// + /// Gets or sets the item at the specified index. + /// + /// + /// The element at the specified index. + /// + /// The zero-based index of the element to get or set. + /// The item at the specified index. + public T this[int index] + { + get + { + if (index < 0 || index >= count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + return array[index]; + } + set + { + if (index < 0 || index >= count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value)); + array[index] = value; + } + } + + #region Explicit interface implementation for IList and ICollection. + bool IList.IsFixedSize => false; + + void ICollection.CopyTo(Array array, int index) + { + Array.Copy(this.array, 0, array, index, count); + } + + bool ICollection.IsSynchronized => false; + + object ICollection.SyncRoot => this; + + object IList.this[int index] + { + get { return this[index]; } + set { this[index] = (T)value; } + } + + int IList.Add(object value) + { + Add((T) value); + return count - 1; + } + + bool IList.Contains(object value) + { + return (value is T && Contains((T)value)); + } + + int IList.IndexOf(object value) + { + if (!(value is T)) + { + return -1; + } + return IndexOf((T)value); + } + + void IList.Insert(int index, object value) + { + Insert(index, (T) value); + } + + void IList.Remove(object value) + { + if (!(value is T)) + { + return; + } + Remove((T)value); + } + #endregion + } +} diff --git a/csharp/src/Google.Protobuf/Extension.cs b/csharp/src/Google.Protobuf/Extension.cs new file mode 100644 index 0000000000..ddeb97795a --- /dev/null +++ b/csharp/src/Google.Protobuf/Extension.cs @@ -0,0 +1,111 @@ +#region Copyright notice and license +// 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. +#endregion + +using System; + +namespace Google.Protobuf +{ + /// + /// Represents a non-generic extension definition + /// + public abstract class Extension + { + internal abstract Type TargetType { get; } + + /// + /// Internal use. Creates a new extension with the specified field number. + /// + protected Extension(int number) + { + FieldNumber = number; + } + + internal abstract IExtensionValue CreateValue(); + + /// + /// Gets the field number of this extension + /// + public int FieldNumber { get; } + } + + /// + /// Represents a type-safe extension identifier used for getting and setting single extension values in instances + /// + /// The message type this field applies to + /// The field value type of this extension + public sealed class Extension : Extension where TTarget : IExtendableMessage + { + private readonly FieldCodec codec; + + /// + /// Creates a new extension identifier with the specified field number and codec + /// + public Extension(int number, FieldCodec codec) : base(number) + { + this.codec = codec; + } + + internal TValue DefaultValue => codec.DefaultValue; + + internal override Type TargetType => typeof(TTarget); + + internal override IExtensionValue CreateValue() + { + return new ExtensionValue(codec); + } + } + + /// + /// Represents a type-safe extension identifier used for getting repeated extension values in instances + /// + /// The message type this field applies to + /// The repeated field value type of this extension + public sealed class RepeatedExtension : Extension where TTarget : IExtendableMessage + { + private readonly FieldCodec codec; + + /// + /// Creates a new repeated extension identifier with the specified field number and codec + /// + public RepeatedExtension(int number, FieldCodec codec) : base(number) + { + this.codec = codec; + } + + internal override Type TargetType => typeof(TTarget); + + internal override IExtensionValue CreateValue() + { + return new RepeatedExtensionValue(codec); + } + } +} diff --git a/csharp/src/Google.Protobuf/ExtensionRegistry.cs b/csharp/src/Google.Protobuf/ExtensionRegistry.cs new file mode 100644 index 0000000000..ea655c123e --- /dev/null +++ b/csharp/src/Google.Protobuf/ExtensionRegistry.cs @@ -0,0 +1,175 @@ +#region Copyright notice and license +// 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. +#endregion + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace Google.Protobuf +{ + /// + /// Provides extensions to messages while parsing + /// + public sealed class ExtensionRegistry : ICollection, IDeepCloneable + { + private IDictionary, Extension> extensions; + + /// + /// Creates a new empty extension registry + /// + public ExtensionRegistry() + { + extensions = new Dictionary, Extension>(); + } + + private ExtensionRegistry(IDictionary, Extension> collection) + { + extensions = collection.ToDictionary(k => k.Key, v => v.Value); + } + + /// + /// Gets the total number of extensions in this extension registry + /// + public int Count => extensions.Count; + + /// + /// Returns whether the registry is readonly + /// + bool ICollection.IsReadOnly => false; + + internal bool ContainsInputField(CodedInputStream stream, Type target, out Extension extension) + { + return extensions.TryGetValue(new ObjectIntPair(target, WireFormat.GetTagFieldNumber(stream.LastTag)), out extension); + } + + /// + /// Adds the specified extension to the registry + /// + public void Add(Extension extension) + { + ProtoPreconditions.CheckNotNull(extension, nameof(extension)); + + extensions.Add(new ObjectIntPair(extension.TargetType, extension.FieldNumber), extension); + } + + /// + /// Adds the specified extensions to the registry + /// + public void Add(params Extension[] newExtensions) + { + ProtoPreconditions.CheckNotNull(newExtensions, nameof(newExtensions)); + + Add((IEnumerable)newExtensions); + } + + /// + /// Adds the specified extensions to the reigstry + /// + public void Add(IEnumerable newExtensions) + { + ProtoPreconditions.CheckNotNull(newExtensions, nameof(newExtensions)); + + foreach (var extension in newExtensions) + Add(extension); + } + + /// + /// Clears the registry of all values + /// + public void Clear() + { + extensions.Clear(); + } + + /// + /// Gets whether the extension registry contains the specified extension + /// + public bool Contains(Extension item) + { + ProtoPreconditions.CheckNotNull(item, nameof(item)); + + return extensions.ContainsKey(new ObjectIntPair(item.TargetType, item.FieldNumber)); + } + + /// + /// Copies the arrays in the registry set to the specified array at the specified index + /// + /// The array to copy to + /// The array index to start at + void ICollection.CopyTo(Extension[] array, int arrayIndex) + { + ProtoPreconditions.CheckNotNull(array, nameof(array)); + if (arrayIndex < 0 || arrayIndex >= array.Length) + throw new ArgumentOutOfRangeException(nameof(arrayIndex)); + if (array.Length - arrayIndex < Count) + throw new ArgumentException("The provided array is shorter than the number of elements in the registry"); + + for (int i = 0; i < array.Length; i++) + { + Extension extension = array[i]; + extensions.Add(new ObjectIntPair(extension.TargetType, extension.FieldNumber), extension); + } + } + + /// + /// Returns an enumerator to enumerate through the items in the registry + /// + /// Returns an enumerator for the extensions in this registry + public IEnumerator GetEnumerator() + { + return extensions.Values.GetEnumerator(); + } + + /// + /// Removes the specified extension from the set + /// + /// The extension + /// true if the extension was removed, otherwise false + public bool Remove(Extension item) + { + ProtoPreconditions.CheckNotNull(item, nameof(item)); + + return extensions.Remove(new ObjectIntPair(item.TargetType, item.FieldNumber)); + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + /// Clones the registry into a new registry + /// + public ExtensionRegistry Clone() + { + return new ExtensionRegistry(extensions); + } + } +} diff --git a/csharp/src/Google.Protobuf/ExtensionSet.cs b/csharp/src/Google.Protobuf/ExtensionSet.cs new file mode 100644 index 0000000000..fa81e6a633 --- /dev/null +++ b/csharp/src/Google.Protobuf/ExtensionSet.cs @@ -0,0 +1,341 @@ +#region Copyright notice and license +// 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. +#endregion + +using Google.Protobuf.Collections; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Google.Protobuf +{ + /// + /// Methods for managing s with null checking. + /// + /// Most users will not use this class directly + /// + public static class ExtensionSet + { + private static bool GetValue(ref ExtensionSet set, Extension extension, out IExtensionValue value) where TTarget : IExtendableMessage + { + if (set == null) + { + value = null; + return false; + } + return set.ValuesByNumber.TryGetValue(extension.FieldNumber, out value); + } + + /// + /// Gets the value of the specified extension + /// + public static TValue Get(ref ExtensionSet set, Extension extension) where TTarget : IExtendableMessage + { + IExtensionValue value; + if (GetValue(ref set, extension, out value)) + { + return ((ExtensionValue)value).GetValue(); + } + else + { + return extension.DefaultValue; + } + } + + /// + /// Gets the value of the specified repeated extension or null if it doesn't exist in this set + /// + public static RepeatedField Get(ref ExtensionSet set, RepeatedExtension extension) where TTarget : IExtendableMessage + { + IExtensionValue value; + if (GetValue(ref set, extension, out value)) + { + return ((RepeatedExtensionValue)value).GetValue(); + } + else + { + return null; + } + } + + /// + /// Gets the value of the specified repeated extension, registering it if it doesn't exist + /// + public static RepeatedField GetOrRegister(ref ExtensionSet set, RepeatedExtension extension) where TTarget : IExtendableMessage + { + IExtensionValue value; + if (set == null) + { + value = extension.CreateValue(); + set = new ExtensionSet(); + set.ValuesByNumber.Add(extension.FieldNumber, value); + } + else + { + if (!set.ValuesByNumber.TryGetValue(extension.FieldNumber, out value)) + { + value = extension.CreateValue(); + set.ValuesByNumber.Add(extension.FieldNumber, value); + } + } + + return ((RepeatedExtensionValue)value).GetValue(); + } + + /// + /// Sets the value of the specified extension + /// + public static void Set(ref ExtensionSet set, Extension extension, TValue value) where TTarget : IExtendableMessage + { + IExtensionValue extensionValue; + if (set == null) + { + extensionValue = extension.CreateValue(); + set = new ExtensionSet(); + set.ValuesByNumber.Add(extension.FieldNumber, extensionValue); + } + else + { + if (!set.ValuesByNumber.TryGetValue(extension.FieldNumber, out extensionValue)) + { + extensionValue = extension.CreateValue(); + set.ValuesByNumber.Add(extension.FieldNumber, extensionValue); + } + } + + ((ExtensionValue)extensionValue).SetValue(value); + } + + /// + /// Gets whether the value of the specified extension is set + /// + public static bool Has(ref ExtensionSet set, Extension extension) where TTarget : IExtendableMessage + { + IExtensionValue value; + if (GetValue(ref set, extension, out value)) + { + return ((ExtensionValue)value).HasValue; + } + else + { + return false; + } + } + + /// + /// Clears the value of the specified extension + /// + public static void Clear(ref ExtensionSet set, Extension extension) where TTarget : IExtendableMessage + { + if (set == null) + { + return; + } + set.ValuesByNumber.Remove(extension.FieldNumber); + if (set.ValuesByNumber.Count == 0) + { + set = null; + } + } + + /// + /// Clears the value of the specified extension + /// + public static void Clear(ref ExtensionSet set, RepeatedExtension extension) where TTarget : IExtendableMessage + { + if (set == null) + { + return; + } + set.ValuesByNumber.Remove(extension.FieldNumber); + if (set.ValuesByNumber.Count == 0) + { + set = null; + } + } + + /// + /// Tries to merge a field from the coded input, returning true if the field was merged. + /// If the set is null or the field was not otherwise merged, this returns false. + /// + public static bool TryMergeFieldFrom(ref ExtensionSet set, CodedInputStream stream) where TTarget : IExtendableMessage + { + Extension extension; + int lastFieldNumber = WireFormat.GetTagFieldNumber(stream.LastTag); + + IExtensionValue extensionValue; + if (set != null && set.ValuesByNumber.TryGetValue(lastFieldNumber, out extensionValue)) + { + extensionValue.MergeFrom(stream); + return true; + } + else if (stream.ExtensionRegistry != null && stream.ExtensionRegistry.ContainsInputField(stream, typeof(TTarget), out extension)) + { + IExtensionValue value = extension.CreateValue(); + value.MergeFrom(stream); + set = (set ?? new ExtensionSet()); + set.ValuesByNumber.Add(extension.FieldNumber, value); + return true; + } + else + { + return false; + } + } + + /// + /// Merges the second set into the first set, creating a new instance if first is null + /// + public static void MergeFrom(ref ExtensionSet first, ExtensionSet second) where TTarget : IExtendableMessage + { + if (second == null) + { + return; + } + if (first == null) + { + first = new ExtensionSet(); + } + foreach (var pair in second.ValuesByNumber) + { + IExtensionValue value; + if (first.ValuesByNumber.TryGetValue(pair.Key, out value)) + { + value.MergeFrom(pair.Value); + } + else + { + var cloned = pair.Value.Clone(); + first.ValuesByNumber[pair.Key] = cloned; + } + } + } + + /// + /// Clones the set into a new set. If the set is null, this returns null + /// + public static ExtensionSet Clone(ExtensionSet set) where TTarget : IExtendableMessage + { + if (set == null) + { + return null; + } + + var newSet = new ExtensionSet(); + foreach (var pair in set.ValuesByNumber) + { + var cloned = pair.Value.Clone(); + newSet.ValuesByNumber[pair.Key] = cloned; + } + return newSet; + } + } + + /// + /// Used for keeping track of extensions in messages. + /// methods route to this set. + /// + /// Most users will not need to use this class directly + /// + /// The message type that extensions in this set target + public sealed class ExtensionSet where TTarget : IExtendableMessage + { + internal Dictionary ValuesByNumber { get; } = new Dictionary(); + + /// + /// Gets a hash code of the set + /// + public override int GetHashCode() + { + int ret = typeof(TTarget).GetHashCode(); + foreach (KeyValuePair field in ValuesByNumber) + { + // Use ^ here to make the field order irrelevant. + int hash = field.Key.GetHashCode() ^ field.Value.GetHashCode(); + ret ^= hash; + } + return ret; + } + + /// + /// Returns whether this set is equal to the other object + /// + public override bool Equals(object other) + { + if (ReferenceEquals(this, other)) + { + return true; + } + ExtensionSet otherSet = other as ExtensionSet; + if (ValuesByNumber.Count != otherSet.ValuesByNumber.Count) + { + return false; + } + foreach (var pair in ValuesByNumber) + { + IExtensionValue secondValue; + if (!otherSet.ValuesByNumber.TryGetValue(pair.Key, out secondValue)) + { + return false; + } + if (!pair.Value.Equals(secondValue)) + { + return false; + } + } + return true; + } + + /// + /// Calculates the size of this extension set + /// + public int CalculateSize() + { + int size = 0; + foreach (var value in ValuesByNumber.Values) + { + size += value.CalculateSize(); + } + return size; + } + + /// + /// Writes the extension values in this set to the output stream + /// + public void WriteTo(CodedOutputStream stream) + { + foreach (var value in ValuesByNumber.Values) + { + value.WriteTo(stream); + } + } + } +} diff --git a/csharp/src/Google.Protobuf/ExtensionValue.cs b/csharp/src/Google.Protobuf/ExtensionValue.cs new file mode 100644 index 0000000000..181e88264c --- /dev/null +++ b/csharp/src/Google.Protobuf/ExtensionValue.cs @@ -0,0 +1,207 @@ +#region Copyright notice and license +// 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. +#endregion + +using Google.Protobuf.Collections; +using System; + +namespace Google.Protobuf +{ + internal interface IExtensionValue : IEquatable, IDeepCloneable + { + void MergeFrom(CodedInputStream input); + void MergeFrom(IExtensionValue value); + void WriteTo(CodedOutputStream output); + int CalculateSize(); + } + + internal sealed class ExtensionValue : IExtensionValue + { + private bool hasValue; + private T field; + private FieldCodec codec; + + internal ExtensionValue(FieldCodec codec) + { + this.codec = codec; + field = codec.DefaultValue; + } + + public int CalculateSize() + { + if (!hasValue) + { + return 0; + } + return codec.CalculateSizeWithTag(field); + } + + public IExtensionValue Clone() + { + return new ExtensionValue(codec) + { + hasValue = hasValue, + field = field is IDeepCloneable ? (field as IDeepCloneable).Clone() : field + }; + } + + public bool Equals(IExtensionValue other) + { + if (ReferenceEquals(this, other)) + return true; + + return other is ExtensionValue + && codec.Equals((other as ExtensionValue).codec) + && hasValue.Equals((other as ExtensionValue).hasValue) + && Equals(field, (other as ExtensionValue).field); + // we check for equality in the codec since we could have equal field values however the values could be written in different ways + } + + public override int GetHashCode() + { + unchecked + { + int hash = 17; + hash = hash * 31 + hasValue.GetHashCode(); + hash = hash * 31 + field.GetHashCode(); + hash = hash * 31 + codec.GetHashCode(); + return hash; + } + } + + public void MergeFrom(CodedInputStream input) + { + hasValue = true; + codec.ValueMerger(input, ref field); + } + + public void MergeFrom(IExtensionValue value) + { + if (value is ExtensionValue) + { + var extensionValue = value as ExtensionValue; + if (extensionValue.hasValue) + { + hasValue |= codec.FieldMerger(ref field, extensionValue.field); + } + } + } + + public void WriteTo(CodedOutputStream output) + { + if (hasValue) + { + output.WriteTag(codec.Tag); + codec.ValueWriter(output, field); + if (codec.EndTag != 0) + { + output.WriteTag(codec.EndTag); + } + } + } + + public T GetValue() => field; + + public void SetValue(T value) + { + hasValue = true; + field = value; + } + + public bool HasValue => hasValue; + } + + internal sealed class RepeatedExtensionValue : IExtensionValue + { + private RepeatedField field; + private readonly FieldCodec codec; + + internal RepeatedExtensionValue(FieldCodec codec) + { + this.codec = codec; + field = new RepeatedField(); + } + + public int CalculateSize() + { + return field.CalculateSize(codec); + } + + public IExtensionValue Clone() + { + return new RepeatedExtensionValue(codec) + { + field = field.Clone() + }; + } + + public bool Equals(IExtensionValue other) + { + if (ReferenceEquals(this, other)) + return true; + + return other is RepeatedExtensionValue + && field.Equals((other as RepeatedExtensionValue).field) + && codec.Equals((other as RepeatedExtensionValue).codec); + } + + public override int GetHashCode() + { + unchecked + { + int hash = 17; + hash = hash * 31 + field.GetHashCode(); + hash = hash * 31 + codec.GetHashCode(); + return hash; + } + } + + public void MergeFrom(CodedInputStream input) + { + field.AddEntriesFrom(input, codec); + } + + public void MergeFrom(IExtensionValue value) + { + if (value is RepeatedExtensionValue) + { + field.Add((value as RepeatedExtensionValue).field); + } + } + + public void WriteTo(CodedOutputStream output) + { + field.WriteTo(output, codec); + } + + public RepeatedField GetValue() => field; + } +} diff --git a/csharp/src/Google.Protobuf/FieldCodec.cs b/csharp/src/Google.Protobuf/FieldCodec.cs index a11f2420e3..60e64effd4 100644 --- a/csharp/src/Google.Protobuf/FieldCodec.cs +++ b/csharp/src/Google.Protobuf/FieldCodec.cs @@ -49,8 +49,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for a string field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForString(uint tag) + public static FieldCodec ForString(uint tag, string defaultValue = "") { return new FieldCodec(input => input.ReadString(), (output, value) => output.WriteString(value), CodedOutputStream.ComputeStringSize, tag); } @@ -59,8 +60,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for a bytes field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForBytes(uint tag) + public static FieldCodec ForBytes(uint tag, ByteString defaultValue = null) { return new FieldCodec(input => input.ReadBytes(), (output, value) => output.WriteBytes(value), CodedOutputStream.ComputeBytesSize, tag); } @@ -69,18 +71,20 @@ namespace Google.Protobuf /// Retrieves a codec suitable for a bool field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForBool(uint tag) + public static FieldCodec ForBool(uint tag, bool defaultValue = false) { - return new FieldCodec(input => input.ReadBool(), (output, value) => output.WriteBool(value), CodedOutputStream.ComputeBoolSize, tag); + return new FieldCodec(input => input.ReadBool(), (output, value) => output.WriteBool(value), CodedOutputStream.BoolSize, tag); } /// /// Retrieves a codec suitable for an int32 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForInt32(uint tag) + public static FieldCodec ForInt32(uint tag, int defaultValue = 0) { return new FieldCodec(input => input.ReadInt32(), (output, value) => output.WriteInt32(value), CodedOutputStream.ComputeInt32Size, tag); } @@ -89,8 +93,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for an sint32 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForSInt32(uint tag) + public static FieldCodec ForSInt32(uint tag, int defaultValue = 0) { return new FieldCodec(input => input.ReadSInt32(), (output, value) => output.WriteSInt32(value), CodedOutputStream.ComputeSInt32Size, tag); } @@ -99,8 +104,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for a fixed32 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForFixed32(uint tag) + public static FieldCodec ForFixed32(uint tag, uint defaultValue = 0) { return new FieldCodec(input => input.ReadFixed32(), (output, value) => output.WriteFixed32(value), 4, tag); } @@ -109,8 +115,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for an sfixed32 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForSFixed32(uint tag) + public static FieldCodec ForSFixed32(uint tag, int defaultValue = 0) { return new FieldCodec(input => input.ReadSFixed32(), (output, value) => output.WriteSFixed32(value), 4, tag); } @@ -119,8 +126,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for a uint32 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForUInt32(uint tag) + public static FieldCodec ForUInt32(uint tag, uint defaultValue = 0) { return new FieldCodec(input => input.ReadUInt32(), (output, value) => output.WriteUInt32(value), CodedOutputStream.ComputeUInt32Size, tag); } @@ -129,8 +137,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for an int64 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForInt64(uint tag) + public static FieldCodec ForInt64(uint tag, long defaultValue = 0) { return new FieldCodec(input => input.ReadInt64(), (output, value) => output.WriteInt64(value), CodedOutputStream.ComputeInt64Size, tag); } @@ -139,8 +148,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for an sint64 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForSInt64(uint tag) + public static FieldCodec ForSInt64(uint tag, long defaultValue = 0) { return new FieldCodec(input => input.ReadSInt64(), (output, value) => output.WriteSInt64(value), CodedOutputStream.ComputeSInt64Size, tag); } @@ -149,8 +159,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for a fixed64 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForFixed64(uint tag) + public static FieldCodec ForFixed64(uint tag, ulong defaultValue = 0) { return new FieldCodec(input => input.ReadFixed64(), (output, value) => output.WriteFixed64(value), 8, tag); } @@ -159,8 +170,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for an sfixed64 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForSFixed64(uint tag) + public static FieldCodec ForSFixed64(uint tag, long defaultValue = 0) { return new FieldCodec(input => input.ReadSFixed64(), (output, value) => output.WriteSFixed64(value), 8, tag); } @@ -169,8 +181,9 @@ namespace Google.Protobuf /// Retrieves a codec suitable for a uint64 field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForUInt64(uint tag) + public static FieldCodec ForUInt64(uint tag, ulong defaultValue = 0) { return new FieldCodec(input => input.ReadUInt64(), (output, value) => output.WriteUInt64(value), CodedOutputStream.ComputeUInt64Size, tag); } @@ -179,20 +192,22 @@ namespace Google.Protobuf /// Retrieves a codec suitable for a float field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForFloat(uint tag) + public static FieldCodec ForFloat(uint tag, float defaultValue = 0) { - return new FieldCodec(input => input.ReadFloat(), (output, value) => output.WriteFloat(value), CodedOutputStream.ComputeFloatSize, tag); + return new FieldCodec(input => input.ReadFloat(), (output, value) => output.WriteFloat(value), CodedOutputStream.FloatSize, tag); } /// /// Retrieves a codec suitable for a double field with the given tag. /// /// The tag. + /// The default value. /// A codec for the given tag. - public static FieldCodec ForDouble(uint tag) + public static FieldCodec ForDouble(uint tag, double defaultValue = 0) { - return new FieldCodec(input => input.ReadDouble(), (output, value) => output.WriteDouble(value), CodedOutputStream.ComputeDoubleSize, tag); + return new FieldCodec(input => input.ReadDouble(), (output, value) => output.WriteDouble(value), CodedOutputStream.DoubleSize, tag); } // Enums are tricky. We can probably use expression trees to build these delegates automatically, @@ -204,8 +219,9 @@ namespace Google.Protobuf /// The tag. /// A conversion function from to the enum type. /// A conversion function from the enum type to . + /// The default value. /// A codec for the given tag. - public static FieldCodec ForEnum(uint tag, Func toInt32, Func fromInt32) + public static FieldCodec ForEnum(uint tag, Func toInt32, Func fromInt32, T defaultValue = default(T)) { return new FieldCodec(input => fromInt32( input.ReadEnum()), @@ -219,10 +235,70 @@ namespace Google.Protobuf /// The tag. /// A parser to use for the message type. /// A codec for the given tag. - public static FieldCodec ForMessage(uint tag, MessageParser parser) where T : IMessage + public static FieldCodec ForMessage(uint tag, MessageParser parser) where T : class, IMessage { return new FieldCodec(input => { T message = parser.CreateTemplate(); input.ReadMessage(message); return message; }, - (output, value) => output.WriteMessage(value), message => CodedOutputStream.ComputeMessageSize(message), tag); + (output, value) => output.WriteMessage(value), (CodedInputStream i, ref T v) => + { + if (v == null) + { + v = parser.CreateTemplate(); + } + + i.ReadMessage(v); + }, + (ref T v, T v2) => + { + if (v2 == null) + { + return false; + } + else if (v == null) + { + v = v2.Clone(); + } + else + { + v.MergeFrom(v2); + } + return true; + }, message => CodedOutputStream.ComputeMessageSize(message), tag); + } + + /// + /// Retrieves a codec suitable for a group field with the given tag. + /// + /// The start group tag. + /// The end group tag. + /// A parser to use for the group message type. + /// A codec for given tag + public static FieldCodec ForGroup(uint startTag, uint endTag, MessageParser parser) where T : class, IMessage + { + return new FieldCodec(input => { T message = parser.CreateTemplate(); input.ReadGroup(message); return message; }, + (output, value) => output.WriteGroup(value), (CodedInputStream i, ref T v) => { + if (v == null) + { + v = parser.CreateTemplate(); + } + + i.ReadGroup(v); + }, + (ref T v, T v2) => + { + if (v2 == null) + { + return v == null; + } + else if (v == null) + { + v = v2.Clone(); + } + else + { + v.MergeFrom(v2); + } + return true; + }, message => CodedOutputStream.ComputeGroupSize(message), startTag, endTag); } /// @@ -234,8 +310,10 @@ namespace Google.Protobuf return new FieldCodec( input => WrapperCodecs.Read(input, nestedCodec), (output, value) => WrapperCodecs.Write(output, value, nestedCodec), + (CodedInputStream i, ref T v) => v = WrapperCodecs.Read(i, nestedCodec), + (ref T v, T v2) => { v = v2; return v == null; }, value => WrapperCodecs.CalculateSize(value, nestedCodec), - tag, + tag, 0, null); // Default value for the wrapper } @@ -249,8 +327,10 @@ namespace Google.Protobuf return new FieldCodec( input => WrapperCodecs.Read(input, nestedCodec), (output, value) => WrapperCodecs.Write(output, value.Value, nestedCodec), + (CodedInputStream i, ref T? v) => v = WrapperCodecs.Read(i, nestedCodec), + (ref T? v, T? v2) => { if (v2.HasValue) { v = v2; } return v.HasValue; }, value => value == null ? 0 : WrapperCodecs.CalculateSize(value.Value, nestedCodec), - tag, + tag, 0, null); // Default value for the wrapper } @@ -279,7 +359,7 @@ namespace Google.Protobuf /// /// Returns a field codec which effectively wraps a value of type T in a message. - /// + /// /// internal static FieldCodec GetCodec() { @@ -352,6 +432,16 @@ namespace Google.Protobuf // Only non-nullable value types support packing. This is the simplest way of detecting that. private static readonly bool TypeSupportsPacking = default(T) != null; + /// + /// Merges an input stream into a value + /// + internal delegate void InputMerger(CodedInputStream input, ref T value); + + /// + /// Merges a value into a reference to another value, returning a boolean if the value was set + /// + internal delegate bool ValuesMerger(ref T value, T other); + static FieldCodec() { if (typeof(T) == typeof(string)) @@ -386,6 +476,17 @@ namespace Google.Protobuf /// internal Func ValueReader { get; } + /// + /// Returns a delegate to merge a value from a coded input stream. + /// It is assumed that the stream is already positioned on the appropriate tag + /// + internal InputMerger ValueMerger { get; } + + /// + /// Returns a delegate to merge two values together. + /// + internal ValuesMerger FieldMerger { get; } + /// /// Returns the fixed size for an entry, or 0 if sizes vary. /// @@ -399,6 +500,14 @@ namespace Google.Protobuf /// internal uint Tag { get; } + /// + /// Gets the end tag of the codec or 0 if there is no end tag + /// + /// + /// The end tag of the codec. + /// + internal uint EndTag { get; } + /// /// Default value for this codec. Usually the same for every instance of the same type, but /// for string/ByteString wrapper fields the codec's default value is null, whereas for @@ -410,7 +519,7 @@ namespace Google.Protobuf internal T DefaultValue { get; } private readonly int tagSize; - + internal FieldCodec( Func reader, Action writer, @@ -424,24 +533,43 @@ namespace Google.Protobuf Func reader, Action writer, Func sizeCalculator, - uint tag) : this(reader, writer, sizeCalculator, tag, DefaultDefault) + uint tag, + uint endTag = 0) : this(reader, writer, (CodedInputStream i, ref T v) => v = reader(i), (ref T v, T v2) => { v = v2; return true; }, sizeCalculator, tag, endTag, DefaultDefault) + { + } + + internal FieldCodec( + Func reader, + Action writer, + InputMerger inputMerger, + ValuesMerger valuesMerger, + Func sizeCalculator, + uint tag, + uint endTag = 0) : this(reader, writer, inputMerger, valuesMerger, sizeCalculator, tag, endTag, DefaultDefault) { } internal FieldCodec( Func reader, Action writer, + InputMerger inputMerger, + ValuesMerger valuesMerger, Func sizeCalculator, uint tag, + uint endTag, T defaultValue) { ValueReader = reader; ValueWriter = writer; + ValueMerger = inputMerger; + FieldMerger = valuesMerger; ValueSizeCalculator = sizeCalculator; FixedSize = 0; Tag = tag; DefaultValue = defaultValue; tagSize = CodedOutputStream.ComputeRawVarint32Size(tag); + if (endTag != 0) + tagSize += CodedOutputStream.ComputeRawVarint32Size(endTag); // Detect packed-ness once, so we can check for it within RepeatedField. PackedRepeatedField = IsPackedRepeatedField(tag); } @@ -455,6 +583,10 @@ namespace Google.Protobuf { output.WriteTag(Tag); ValueWriter(output, value); + if (EndTag != 0) + { + output.WriteTag(EndTag); + } } } diff --git a/csharp/src/Google.Protobuf/FieldMaskTree.cs b/csharp/src/Google.Protobuf/FieldMaskTree.cs new file mode 100644 index 0000000000..2297e7a119 --- /dev/null +++ b/csharp/src/Google.Protobuf/FieldMaskTree.cs @@ -0,0 +1,365 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 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. +#endregion + +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using Google.Protobuf.Reflection; +using Google.Protobuf.WellKnownTypes; + +namespace Google.Protobuf +{ + /// + /// A tree representation of a FieldMask. Each leaf node in this tree represent + /// a field path in the FieldMask. + /// + /// For example, FieldMask "foo.bar,foo.baz,bar.baz" as a tree will be: + /// + /// [root] -+- foo -+- bar + /// | | + /// | +- baz + /// | + /// +- bar --- baz + /// + /// + /// By representing FieldMasks with this tree structure we can easily convert + /// a FieldMask to a canonical form, merge two FieldMasks, calculate the + /// intersection to two FieldMasks and traverse all fields specified by the + /// FieldMask in a message tree. + /// + internal sealed class FieldMaskTree + { + private const char FIELD_PATH_SEPARATOR = '.'; + + internal sealed class Node + { + public Dictionary Children { get; } = new Dictionary(); + } + + private readonly Node root = new Node(); + + /// + /// Creates an empty FieldMaskTree. + /// + public FieldMaskTree() + { + } + + /// + /// Creates a FieldMaskTree for a given FieldMask. + /// + public FieldMaskTree(FieldMask mask) + { + MergeFromFieldMask(mask); + } + + public override string ToString() + { + return ToFieldMask().ToString(); + } + + /// + /// Adds a field path to the tree. In a FieldMask, every field path matches the + /// specified field as well as all its sub-fields. For example, a field path + /// "foo.bar" matches field "foo.bar" and also "foo.bar.baz", etc. When adding + /// a field path to the tree, redundant sub-paths will be removed. That is, + /// after adding "foo.bar" to the tree, "foo.bar.baz" will be removed if it + /// exists, which will turn the tree node for "foo.bar" to a leaf node. + /// Likewise, if the field path to add is a sub-path of an existing leaf node, + /// nothing will be changed in the tree. + /// + public FieldMaskTree AddFieldPath(string path) + { + var parts = path.Split(FIELD_PATH_SEPARATOR); + if (parts.Length == 0) + { + return this; + } + + var node = root; + var createNewBranch = false; + + // Find the matching node in the tree. + foreach (var part in parts) + { + // Check whether the path matches an existing leaf node. + if (!createNewBranch + && node != root + && node.Children.Count == 0) + { + // The path to add is a sub-path of an existing leaf node. + return this; + } + + Node childNode; + if (!node.Children.TryGetValue(part, out childNode)) + { + createNewBranch = true; + childNode = new Node(); + node.Children.Add(part, childNode); + } + node = childNode; + } + + // Turn the matching node into a leaf node (i.e., remove sub-paths). + node.Children.Clear(); + return this; + } + + /// + /// Merges all field paths in a FieldMask into this tree. + /// + public FieldMaskTree MergeFromFieldMask(FieldMask mask) + { + foreach (var path in mask.Paths) + { + AddFieldPath(path); + } + + return this; + } + + /// + /// Converts this tree to a FieldMask. + /// + public FieldMask ToFieldMask() + { + var mask = new FieldMask(); + if (root.Children.Count != 0) + { + var paths = new List(); + GetFieldPaths(root, "", paths); + mask.Paths.AddRange(paths); + } + + return mask; + } + + /// + /// Gathers all field paths in a sub-tree. + /// + private void GetFieldPaths(Node node, string path, List paths) + { + if (node.Children.Count == 0) + { + paths.Add(path); + return; + } + + foreach (var entry in node.Children) + { + var childPath = path.Length == 0 ? entry.Key : path + "." + entry.Key; + GetFieldPaths(entry.Value, childPath, paths); + } + } + + /// + /// Adds the intersection of this tree with the given to . + /// + public void IntersectFieldPath(string path, FieldMaskTree output) + { + if (root.Children.Count == 0) + { + return; + } + + var parts = path.Split(FIELD_PATH_SEPARATOR); + if (parts.Length == 0) + { + return; + } + + var node = root; + foreach (var part in parts) + { + if (node != root + && node.Children.Count == 0) + { + // The given path is a sub-path of an existing leaf node in the tree. + output.AddFieldPath(path); + return; + } + + if (!node.Children.TryGetValue(part, out node)) + { + return; + } + } + + // We found a matching node for the path. All leaf children of this matching + // node is in the intersection. + var paths = new List(); + GetFieldPaths(node, path, paths); + foreach (var value in paths) + { + output.AddFieldPath(value); + } + } + + /// + /// Merges all fields specified by this FieldMaskTree from to . + /// + public void Merge(IMessage source, IMessage destination, FieldMask.MergeOptions options) + { + if (source.Descriptor != destination.Descriptor) + { + throw new InvalidProtocolBufferException("Cannot merge messages of different types."); + } + + if (root.Children.Count == 0) + { + return; + } + + Merge(root, "", source, destination, options); + } + + /// + /// Merges all fields specified by a sub-tree from to . + /// + private void Merge( + Node node, + string path, + IMessage source, + IMessage destination, + FieldMask.MergeOptions options) + { + if (source.Descriptor != destination.Descriptor) + { + throw new InvalidProtocolBufferException($"source ({source.Descriptor}) and destination ({destination.Descriptor}) descriptor must be equal"); + } + + var descriptor = source.Descriptor; + foreach (var entry in node.Children) + { + var field = descriptor.FindFieldByName(entry.Key); + if (field == null) + { + Debug.WriteLine($"Cannot find field \"{entry.Key}\" in message type \"{descriptor.FullName}\""); + continue; + } + + if (entry.Value.Children.Count != 0) + { + if (field.IsRepeated + || field.FieldType != FieldType.Message) + { + Debug.WriteLine($"Field \"{field.FullName}\" is not a singular message field and cannot have sub-fields."); + continue; + } + + var sourceField = field.Accessor.GetValue(source); + var destinationField = field.Accessor.GetValue(destination); + if (sourceField == null + && destinationField == null) + { + // If the message field is not present in both source and destination, skip recursing + // so we don't create unnecessary empty messages. + continue; + } + + if (destinationField == null) + { + // If we have to merge but the destination does not contain the field, create it. + destinationField = field.MessageType.Parser.CreateTemplate(); + field.Accessor.SetValue(destination, destinationField); + } + + var childPath = path.Length == 0 ? entry.Key : path + "." + entry.Key; + Merge(entry.Value, childPath, (IMessage)sourceField, (IMessage)destinationField, options); + continue; + } + + if (field.IsRepeated) + { + if (options.ReplaceRepeatedFields) + { + field.Accessor.Clear(destination); + } + + var sourceField = (IList)field.Accessor.GetValue(source); + var destinationField = (IList)field.Accessor.GetValue(destination); + foreach (var element in sourceField) + { + destinationField.Add(element); + } + } + else + { + var sourceField = field.Accessor.GetValue(source); + if (field.FieldType == FieldType.Message) + { + if (options.ReplaceMessageFields) + { + if (sourceField == null) + { + field.Accessor.Clear(destination); + } + else + { + field.Accessor.SetValue(destination, sourceField); + } + } + else + { + if (sourceField != null) + { + var sourceByteString = ((IMessage)sourceField).ToByteString(); + var destinationValue = (IMessage)field.Accessor.GetValue(destination); + if (destinationValue != null) + { + destinationValue.MergeFrom(sourceByteString); + } + else + { + field.Accessor.SetValue(destination, field.MessageType.Parser.ParseFrom(sourceByteString)); + } + } + } + } + else + { + if (sourceField != null + || !options.ReplacePrimitiveFields) + { + field.Accessor.SetValue(destination, sourceField); + } + else + { + field.Accessor.Clear(destination); + } + } + } + } + } + } +} diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.csproj b/csharp/src/Google.Protobuf/Google.Protobuf.csproj index a056b43537..f93897f3e9 100644 --- a/csharp/src/Google.Protobuf/Google.Protobuf.csproj +++ b/csharp/src/Google.Protobuf/Google.Protobuf.csproj @@ -1,35 +1,42 @@ - + C# runtime library for Protocol Buffers - Google's data interchange format. Copyright 2015, Google Inc. Google Protocol Buffers - 3.6.1 + 3.9.0-rc1 + 6 Google Inc. - netstandard1.0;net45 + netstandard1.0;netstandard2.0;net45 true ../../keys/Google.Protobuf.snk true true Protocol;Buffers;Binary;Serialization;Format;Google;proto;proto3 C# proto3 support - https://github.com/google/protobuf - https://github.com/google/protobuf/blob/master/LICENSE + https://github.com/protocolbuffers/protobuf + https://github.com/protocolbuffers/protobuf/blob/master/LICENSE git - https://github.com/google/protobuf.git + https://github.com/protocolbuffers/protobuf.git + + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - - netstandard1.0 + netstandard1.0;netstandard2.0 + + + + - + diff --git a/csharp/src/Google.Protobuf/IExtendableMessage.cs b/csharp/src/Google.Protobuf/IExtendableMessage.cs new file mode 100644 index 0000000000..5cc2ae31a6 --- /dev/null +++ b/csharp/src/Google.Protobuf/IExtendableMessage.cs @@ -0,0 +1,78 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 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. +#endregion + +using Google.Protobuf.Collections; + +namespace Google.Protobuf +{ + /// + /// Generic interface for a Protocol Buffers message containing one or more extensions, where the type parameter is expected to be the same type as the implementation class + /// + public interface IExtendableMessage : IMessage where T : IExtendableMessage + { + /// + /// Gets the value of the specified extension + /// + TValue GetExtension(Extension extension); + + /// + /// Gets the value of the specified repeated extension or null if the extension isn't registered in this set. + /// For a version of this method that never returns null, use + /// + RepeatedField GetExtension(RepeatedExtension extension); + + /// + /// Gets the value of the specified repeated extension, registering it if it isn't + /// + RepeatedField GetOrRegisterExtension(RepeatedExtension extension); + + /// + /// Sets the value of the specified extension + /// + void SetExtension(Extension extension, TValue value); + + /// + /// Gets whether the value of the specified extension is set + /// + bool HasExtension(Extension extension); + + /// + /// Clears the value of the specified extension + /// + void ClearExtension(Extension extension); + + /// + /// Clears the value of the specified repeated extension + /// + void ClearExtension(RepeatedExtension extension); + } +} diff --git a/csharp/src/Google.Protobuf/InvalidProtocolBufferException.cs b/csharp/src/Google.Protobuf/InvalidProtocolBufferException.cs index 0fbc530631..c5ffe9bbf8 100644 --- a/csharp/src/Google.Protobuf/InvalidProtocolBufferException.cs +++ b/csharp/src/Google.Protobuf/InvalidProtocolBufferException.cs @@ -88,6 +88,12 @@ namespace Google.Protobuf "Protocol message contained an invalid tag (zero)."); } + internal static InvalidProtocolBufferException InvalidWireType() + { + return new InvalidProtocolBufferException( + "Protocol message contained a tag with an invalid wire type."); + } + internal static InvalidProtocolBufferException InvalidBase64(Exception innerException) { return new InvalidProtocolBufferException("Invalid base64 data", innerException); @@ -125,5 +131,10 @@ namespace Google.Protobuf return new InvalidProtocolBufferException( "Stream of protocol messages had invalid tag. Expected tag is length-delimited field 1."); } - } + + internal static InvalidProtocolBufferException MissingFields() + { + return new InvalidProtocolBufferException("Message was missing required fields"); + } +} } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index 4ae10d8b73..2b4c5f0a80 100644 --- a/csharp/src/Google.Protobuf/JsonFormatter.cs +++ b/csharp/src/Google.Protobuf/JsonFormatter.cs @@ -70,7 +70,7 @@ namespace Google.Protobuf /// public static JsonFormatter Default { get; } = new JsonFormatter(Settings.Default); - // A JSON formatter which *only* exists + // A JSON formatter which *only* exists private static readonly JsonFormatter diagnosticFormatter = new JsonFormatter(Settings.Default); /// @@ -271,7 +271,25 @@ namespace Google.Protobuf } return result.ToString(); } - + + internal static string FromJsonName(string name) + { + StringBuilder result = new StringBuilder(name.Length); + foreach (char ch in name) + { + if (char.IsUpper(ch)) + { + result.Append('_'); + result.Append(char.ToLowerInvariant(ch)); + } + else + { + result.Append(ch); + } + } + return result.ToString(); + } + private static void WriteNull(TextWriter writer) { writer.Write("null"); @@ -561,7 +579,7 @@ namespace Google.Protobuf writer.Write(data.ToBase64()); writer.Write('"'); writer.Write(" }"); - } + } private void WriteStruct(TextWriter writer, IMessage message) { @@ -598,7 +616,7 @@ namespace Google.Protobuf } object value = specifiedField.Accessor.GetValue(message); - + switch (specifiedField.FieldNumber) { case Value.BoolValueFieldNumber: @@ -853,7 +871,7 @@ namespace Google.Protobuf // the platforms we target have it. private static readonly Dictionary> dictionaries = new Dictionary>(); - + internal static string GetOriginalName(object value) { var enumType = value.GetType(); diff --git a/csharp/src/Google.Protobuf/JsonParser.cs b/csharp/src/Google.Protobuf/JsonParser.cs index 284bce939b..fb594f2a43 100644 --- a/csharp/src/Google.Protobuf/JsonParser.cs +++ b/csharp/src/Google.Protobuf/JsonParser.cs @@ -674,7 +674,7 @@ namespace Google.Protobuf if (value != Math.Floor(value)) { throw new InvalidProtocolBufferException($"Value not an integer: {value}"); - } + } } private static object ParseSingleStringValue(FieldDescriptor field, string text) @@ -919,7 +919,7 @@ namespace Google.Protobuf messagePaths.Add(ToSnakeCase(path)); } } - + // Ported from src/google/protobuf/util/internal/utility.cc private static string ToSnakeCase(string text) { diff --git a/csharp/src/Google.Protobuf/JsonTokenizer.cs b/csharp/src/Google.Protobuf/JsonTokenizer.cs index 0e403f78aa..4725e7cc51 100644 --- a/csharp/src/Google.Protobuf/JsonTokenizer.cs +++ b/csharp/src/Google.Protobuf/JsonTokenizer.cs @@ -614,7 +614,7 @@ namespace Google.Protobuf /// where ^ represents the current position within the text stream. The examples all use string values, /// but could be any value, including nested objects/arrays. /// The complete state of the tokenizer also includes a stack to indicate the contexts (arrays/objects). - /// Any additional notional state of "AfterValue" indicates that a value has been completed, at which + /// Any additional notional state of "AfterValue" indicates that a value has been completed, at which /// point there's an immediate transition to ExpectedEndOfDocument, ObjectAfterProperty or ArrayAfterValue. /// /// @@ -655,7 +655,7 @@ namespace Google.Protobuf /// /// { "foo" : ^ "bar", "x": "y" } /// Before any property other than the first in an object. - /// (Equivalently: after any property in an object) + /// (Equivalently: after any property in an object) /// Next states: /// "AfterValue" (value is simple) /// ObjectStart (value is object) diff --git a/csharp/src/Google.Protobuf/LimitedInputStream.cs b/csharp/src/Google.Protobuf/LimitedInputStream.cs index f11d19d944..50ead9c992 100644 --- a/csharp/src/Google.Protobuf/LimitedInputStream.cs +++ b/csharp/src/Google.Protobuf/LimitedInputStream.cs @@ -29,7 +29,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion - + using System; using System.IO; diff --git a/csharp/src/Google.Protobuf/MessageExtensions.cs b/csharp/src/Google.Protobuf/MessageExtensions.cs index 62181eb9d9..5ca772f58e 100644 --- a/csharp/src/Google.Protobuf/MessageExtensions.cs +++ b/csharp/src/Google.Protobuf/MessageExtensions.cs @@ -30,7 +30,10 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Reflection; +using System.Collections; using System.IO; +using System.Linq; namespace Google.Protobuf { @@ -45,7 +48,7 @@ namespace Google.Protobuf /// The message to merge the data into. /// The data to merge, which must be protobuf-encoded binary data. public static void MergeFrom(this IMessage message, byte[] data) => - MergeFrom(message, data, false); + MergeFrom(message, data, false, null); /// /// Merges data from the given byte array slice into an existing message. @@ -55,7 +58,7 @@ namespace Google.Protobuf /// The offset of the slice to merge. /// The length of the slice to merge. public static void MergeFrom(this IMessage message, byte[] data, int offset, int length) => - MergeFrom(message, data, offset, length, false); + MergeFrom(message, data, offset, length, false, null); /// /// Merges data from the given byte string into an existing message. @@ -63,7 +66,7 @@ namespace Google.Protobuf /// The message to merge the data into. /// The data to merge, which must be protobuf-encoded binary data. public static void MergeFrom(this IMessage message, ByteString data) => - MergeFrom(message, data, false); + MergeFrom(message, data, false, null); /// /// Merges data from the given stream into an existing message. @@ -71,7 +74,7 @@ namespace Google.Protobuf /// The message to merge the data into. /// Stream containing the data to merge, which must be protobuf-encoded binary data. public static void MergeFrom(this IMessage message, Stream input) => - MergeFrom(message, input, false); + MergeFrom(message, input, false, null); /// /// Merges length-delimited data from the given stream into an existing message. @@ -83,7 +86,7 @@ namespace Google.Protobuf /// The message to merge the data into. /// Stream containing the data to merge, which must be protobuf-encoded binary data. public static void MergeDelimitedFrom(this IMessage message, Stream input) => - MergeDelimitedFrom(message, input, false); + MergeDelimitedFrom(message, input, false, null); /// /// Converts the given message into a byte array in protobuf encoding. @@ -140,54 +143,105 @@ namespace Google.Protobuf return ByteString.AttachBytes(message.ToByteArray()); } + /// + /// Checks if all required fields in a message have values set. For proto3 messages, this returns true + /// + public static bool IsInitialized(this IMessage message) + { + if (message.Descriptor.File.Proto.Syntax != "proto2") + { + return true; + } + + return message.Descriptor + .Fields + .InDeclarationOrder() + .All(f => + { + if (f.IsMap) + { + var map = (IDictionary)f.Accessor.GetValue(message); + return map.Values.OfType().All(IsInitialized); + } + else if (f.IsRepeated && f.FieldType == FieldType.Message || f.FieldType == FieldType.Group) + { + var enumerable = (IEnumerable)f.Accessor.GetValue(message); + return enumerable.Cast().All(IsInitialized); + } + else if (f.FieldType == FieldType.Message || f.FieldType == FieldType.Group) + { + if (f.Accessor.HasValue(message)) + { + return ((IMessage)f.Accessor.GetValue(message)).IsInitialized(); + } + else + { + return !f.IsRequired; + } + } + else if (f.IsRequired) + { + return f.Accessor.HasValue(message); + } + else + { + return true; + } + }); + } + // Implementations allowing unknown fields to be discarded. - internal static void MergeFrom(this IMessage message, byte[] data, bool discardUnknownFields) + internal static void MergeFrom(this IMessage message, byte[] data, bool discardUnknownFields, ExtensionRegistry registry) { ProtoPreconditions.CheckNotNull(message, "message"); ProtoPreconditions.CheckNotNull(data, "data"); CodedInputStream input = new CodedInputStream(data); input.DiscardUnknownFields = discardUnknownFields; + input.ExtensionRegistry = registry; message.MergeFrom(input); input.CheckReadEndOfStreamTag(); } - internal static void MergeFrom(this IMessage message, byte[] data, int offset, int length, bool discardUnknownFields) + internal static void MergeFrom(this IMessage message, byte[] data, int offset, int length, bool discardUnknownFields, ExtensionRegistry registry) { ProtoPreconditions.CheckNotNull(message, "message"); ProtoPreconditions.CheckNotNull(data, "data"); CodedInputStream input = new CodedInputStream(data, offset, length); input.DiscardUnknownFields = discardUnknownFields; + input.ExtensionRegistry = registry; message.MergeFrom(input); input.CheckReadEndOfStreamTag(); } - internal static void MergeFrom(this IMessage message, ByteString data, bool discardUnknownFields) + internal static void MergeFrom(this IMessage message, ByteString data, bool discardUnknownFields, ExtensionRegistry registry) { ProtoPreconditions.CheckNotNull(message, "message"); ProtoPreconditions.CheckNotNull(data, "data"); CodedInputStream input = data.CreateCodedInput(); input.DiscardUnknownFields = discardUnknownFields; + input.ExtensionRegistry = registry; message.MergeFrom(input); input.CheckReadEndOfStreamTag(); } - internal static void MergeFrom(this IMessage message, Stream input, bool discardUnknownFields) + internal static void MergeFrom(this IMessage message, Stream input, bool discardUnknownFields, ExtensionRegistry registry) { ProtoPreconditions.CheckNotNull(message, "message"); ProtoPreconditions.CheckNotNull(input, "input"); CodedInputStream codedInput = new CodedInputStream(input); codedInput.DiscardUnknownFields = discardUnknownFields; + codedInput.ExtensionRegistry = registry; message.MergeFrom(codedInput); codedInput.CheckReadEndOfStreamTag(); } - internal static void MergeDelimitedFrom(this IMessage message, Stream input, bool discardUnknownFields) + internal static void MergeDelimitedFrom(this IMessage message, Stream input, bool discardUnknownFields, ExtensionRegistry registry) { ProtoPreconditions.CheckNotNull(message, "message"); ProtoPreconditions.CheckNotNull(input, "input"); int size = (int) CodedInputStream.ReadRawVarint32(input); Stream limitedStream = new LimitedInputStream(input, size); - MergeFrom(message, limitedStream, discardUnknownFields); + MergeFrom(message, limitedStream, discardUnknownFields, registry); } } } diff --git a/csharp/src/Google.Protobuf/MessageParser.cs b/csharp/src/Google.Protobuf/MessageParser.cs index 4d35554a25..76a350ce4c 100644 --- a/csharp/src/Google.Protobuf/MessageParser.cs +++ b/csharp/src/Google.Protobuf/MessageParser.cs @@ -29,7 +29,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion - + using System; using System.IO; @@ -45,10 +45,13 @@ namespace Google.Protobuf // TODO: When we use a C# 7.1 compiler, make this private protected. internal bool DiscardUnknownFields { get; } - internal MessageParser(Func factory, bool discardUnknownFields) + internal ExtensionRegistry Extensions { get; } + + internal MessageParser(Func factory, bool discardUnknownFields, ExtensionRegistry extensions) { this.factory = factory; DiscardUnknownFields = discardUnknownFields; + Extensions = extensions; } /// @@ -68,7 +71,8 @@ namespace Google.Protobuf public IMessage ParseFrom(byte[] data) { IMessage message = factory(); - message.MergeFrom(data, DiscardUnknownFields); + message.MergeFrom(data, DiscardUnknownFields, Extensions); + CheckMergedRequiredFields(message); return message; } @@ -82,7 +86,8 @@ namespace Google.Protobuf public IMessage ParseFrom(byte[] data, int offset, int length) { IMessage message = factory(); - message.MergeFrom(data, offset, length, DiscardUnknownFields); + message.MergeFrom(data, offset, length, DiscardUnknownFields, Extensions); + CheckMergedRequiredFields(message); return message; } @@ -94,7 +99,8 @@ namespace Google.Protobuf public IMessage ParseFrom(ByteString data) { IMessage message = factory(); - message.MergeFrom(data, DiscardUnknownFields); + message.MergeFrom(data, DiscardUnknownFields, Extensions); + CheckMergedRequiredFields(message); return message; } @@ -106,7 +112,8 @@ namespace Google.Protobuf public IMessage ParseFrom(Stream input) { IMessage message = factory(); - message.MergeFrom(input, DiscardUnknownFields); + message.MergeFrom(input, DiscardUnknownFields, Extensions); + CheckMergedRequiredFields(message); return message; } @@ -122,7 +129,8 @@ namespace Google.Protobuf public IMessage ParseDelimitedFrom(Stream input) { IMessage message = factory(); - message.MergeDelimitedFrom(input, DiscardUnknownFields); + message.MergeDelimitedFrom(input, DiscardUnknownFields, Extensions); + CheckMergedRequiredFields(message); return message; } @@ -135,6 +143,7 @@ namespace Google.Protobuf { IMessage message = factory(); MergeFrom(message, input); + CheckMergedRequiredFields(message); return message; } @@ -167,13 +176,27 @@ namespace Google.Protobuf } } + internal static void CheckMergedRequiredFields(IMessage message) + { + if (!message.IsInitialized()) + throw new InvalidOperationException("Parsed message does not contain all required fields"); + } + /// /// Creates a new message parser which optionally discards unknown fields when parsing. /// /// Whether or not to discard unknown fields when parsing. /// A newly configured message parser. public MessageParser WithDiscardUnknownFields(bool discardUnknownFields) => - new MessageParser(factory, discardUnknownFields); + new MessageParser(factory, discardUnknownFields, Extensions); + + /// + /// Creates a new message parser which registers extensions from the specified registry upon creating the message instance + /// + /// The extensions to register + /// A newly configured message parser. + public MessageParser WithExtensionRegistry(ExtensionRegistry registry) => + new MessageParser(factory, DiscardUnknownFields, registry); } /// @@ -198,7 +221,7 @@ namespace Google.Protobuf // The current implementation avoids a virtual method call and a cast, which *may* be significant in some cases. // Benchmarking work is required to measure the significance - but it's only a few lines of code in any case. // The API wouldn't change anyway - just the implementation - so this work can be deferred. - private readonly Func factory; + private readonly Func factory; /// /// Creates a new parser. @@ -208,11 +231,11 @@ namespace Google.Protobuf /// to require a parameterless constructor: delegates are significantly faster to execute. /// /// Function to invoke when a new, empty message is required. - public MessageParser(Func factory) : this(factory, false) + public MessageParser(Func factory) : this(factory, false, null) { } - internal MessageParser(Func factory, bool discardUnknownFields) : base(() => factory(), discardUnknownFields) + internal MessageParser(Func factory, bool discardUnknownFields, ExtensionRegistry extensions) : base(() => factory(), discardUnknownFields, extensions) { this.factory = factory; } @@ -234,7 +257,7 @@ namespace Google.Protobuf public new T ParseFrom(byte[] data) { T message = factory(); - message.MergeFrom(data, DiscardUnknownFields); + message.MergeFrom(data, DiscardUnknownFields, Extensions); return message; } @@ -248,7 +271,7 @@ namespace Google.Protobuf public new T ParseFrom(byte[] data, int offset, int length) { T message = factory(); - message.MergeFrom(data, offset, length, DiscardUnknownFields); + message.MergeFrom(data, offset, length, DiscardUnknownFields, Extensions); return message; } @@ -260,7 +283,7 @@ namespace Google.Protobuf public new T ParseFrom(ByteString data) { T message = factory(); - message.MergeFrom(data, DiscardUnknownFields); + message.MergeFrom(data, DiscardUnknownFields, Extensions); return message; } @@ -272,7 +295,7 @@ namespace Google.Protobuf public new T ParseFrom(Stream input) { T message = factory(); - message.MergeFrom(input, DiscardUnknownFields); + message.MergeFrom(input, DiscardUnknownFields, Extensions); return message; } @@ -288,7 +311,7 @@ namespace Google.Protobuf public new T ParseDelimitedFrom(Stream input) { T message = factory(); - message.MergeDelimitedFrom(input, DiscardUnknownFields); + message.MergeDelimitedFrom(input, DiscardUnknownFields, Extensions); return message; } @@ -324,6 +347,14 @@ namespace Google.Protobuf /// Whether or not to discard unknown fields when parsing. /// A newly configured message parser. public new MessageParser WithDiscardUnknownFields(bool discardUnknownFields) => - new MessageParser(factory, discardUnknownFields); + new MessageParser(factory, discardUnknownFields, Extensions); + + /// + /// Creates a new message parser which registers extensions from the specified registry upon creating the message instance + /// + /// The extensions to register + /// A newly configured message parser. + public new MessageParser WithExtensionRegistry(ExtensionRegistry registry) => + new MessageParser(factory, DiscardUnknownFields, registry); } } diff --git a/csharp/src/Google.Protobuf/ObjectIntPair.cs b/csharp/src/Google.Protobuf/ObjectIntPair.cs new file mode 100644 index 0000000000..b98d93a517 --- /dev/null +++ b/csharp/src/Google.Protobuf/ObjectIntPair.cs @@ -0,0 +1,40 @@ +using System; + +namespace Google.Protobuf +{ + /// + /// Struct used to hold the keys for the fieldByNumber table in DescriptorPool and the keys for the + /// extensionByNumber table in ExtensionRegistry. + /// + internal struct ObjectIntPair : IEquatable> where T : class + { + private readonly int number; + private readonly T obj; + + internal ObjectIntPair(T obj, int number) + { + this.number = number; + this.obj = obj; + } + + public bool Equals(ObjectIntPair other) + { + return obj == other.obj + && number == other.number; + } + + public override bool Equals(object obj) + { + if (obj is ObjectIntPair) + { + return Equals((ObjectIntPair)obj); + } + return false; + } + + public override int GetHashCode() + { + return obj.GetHashCode() * ((1 << 16) - 1) + number; + } + } +} diff --git a/csharp/src/Google.Protobuf/ProtoPreconditions.cs b/csharp/src/Google.Protobuf/ProtoPreconditions.cs index abaeb9b481..590a3bb20f 100644 --- a/csharp/src/Google.Protobuf/ProtoPreconditions.cs +++ b/csharp/src/Google.Protobuf/ProtoPreconditions.cs @@ -1,79 +1,79 @@ -#region Copyright notice and license -// 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. -#endregion - -using System; - -namespace Google.Protobuf -{ - /// - /// Helper methods for throwing exceptions when preconditions are not met. - /// - /// - /// This class is used internally and by generated code; it is not particularly - /// expected to be used from application code, although nothing prevents it - /// from being used that way. - /// - public static class ProtoPreconditions - { - /// - /// Throws an ArgumentNullException if the given value is null, otherwise - /// return the value to the caller. - /// - public static T CheckNotNull(T value, string name) where T : class - { - if (value == null) - { - throw new ArgumentNullException(name); - } - return value; - } - - /// - /// Throws an ArgumentNullException if the given value is null, otherwise - /// return the value to the caller. - /// - /// - /// This is equivalent to but without the type parameter - /// constraint. In most cases, the constraint is useful to prevent you from calling CheckNotNull - /// with a value type - but it gets in the way if either you want to use it with a nullable - /// value type, or you want to use it with an unconstrained type parameter. - /// - internal static T CheckNotNullUnconstrained(T value, string name) - { - if (value == null) - { - throw new ArgumentNullException(name); - } - return value; - } - } +#region Copyright notice and license +// 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. +#endregion + +using System; + +namespace Google.Protobuf +{ + /// + /// Helper methods for throwing exceptions when preconditions are not met. + /// + /// + /// This class is used internally and by generated code; it is not particularly + /// expected to be used from application code, although nothing prevents it + /// from being used that way. + /// + public static class ProtoPreconditions + { + /// + /// Throws an ArgumentNullException if the given value is null, otherwise + /// return the value to the caller. + /// + public static T CheckNotNull(T value, string name) where T : class + { + if (value == null) + { + throw new ArgumentNullException(name); + } + return value; + } + + /// + /// Throws an ArgumentNullException if the given value is null, otherwise + /// return the value to the caller. + /// + /// + /// This is equivalent to but without the type parameter + /// constraint. In most cases, the constraint is useful to prevent you from calling CheckNotNull + /// with a value type - but it gets in the way if either you want to use it with a nullable + /// value type, or you want to use it with an unconstrained type parameter. + /// + internal static T CheckNotNullUnconstrained(T value, string name) + { + if (value == null) + { + throw new ArgumentNullException(name); + } + return value; + } + } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/CustomOptions.cs b/csharp/src/Google.Protobuf/Reflection/CustomOptions.cs index 88b3ec000d..57df2ed4a8 100644 --- a/csharp/src/Google.Protobuf/Reflection/CustomOptions.cs +++ b/csharp/src/Google.Protobuf/Reflection/CustomOptions.cs @@ -30,8 +30,12 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Collections; using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; +using System.Reflection; namespace Google.Protobuf.Reflection { @@ -59,19 +63,13 @@ namespace Google.Protobuf.Reflection /// public sealed class CustomOptions { - /// - /// Singleton for all descriptors with an empty set of options. - /// - internal static readonly CustomOptions Empty = new CustomOptions(); - - /// - /// A sequence of values per field. This needs to be per field rather than per tag to allow correct deserialization - /// of repeated fields which could be "int, ByteString, int" - unlikely as that is. The fact that values are boxed - /// is unfortunate; we might be able to use a struct instead, and we could combine uint and ulong values. - /// - private readonly Dictionary> valuesByField = new Dictionary>(); + private static readonly object[] EmptyParameters = new object[0]; + private readonly IDictionary values; - private CustomOptions() { } + internal CustomOptions(IDictionary values) + { + this.values = values; + } /// /// Retrieves a Boolean value for the specified option field. @@ -79,12 +77,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetBool(int field, out bool value) - { - ulong? tmp = GetLastNumericValue(field); - value = tmp == 1UL; - return tmp != null; - } + public bool TryGetBool(int field, out bool value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves a signed 32-bit integer value for the specified option field. @@ -92,12 +85,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetInt32(int field, out int value) - { - ulong? tmp = GetLastNumericValue(field); - value = (int) tmp.GetValueOrDefault(); - return tmp != null; - } + public bool TryGetInt32(int field, out int value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves a signed 64-bit integer value for the specified option field. @@ -105,12 +93,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetInt64(int field, out long value) - { - ulong? tmp = GetLastNumericValue(field); - value = (long) tmp.GetValueOrDefault(); - return tmp != null; - } + public bool TryGetInt64(int field, out long value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves an unsigned 32-bit integer value for the specified option field, @@ -147,7 +130,7 @@ namespace Google.Protobuf.Reflection /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. public bool TryGetSFixed64(int field, out long value) => TryGetInt64(field, out value); - + /// /// Retrieves a signed 32-bit integer value for the specified option field, /// assuming a zigzag encoding. @@ -155,12 +138,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetSInt32(int field, out int value) - { - ulong? tmp = GetLastNumericValue(field); - value = CodedInputStream.DecodeZigZag32((uint) tmp.GetValueOrDefault()); - return tmp != null; - } + public bool TryGetSInt32(int field, out int value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves a signed 64-bit integer value for the specified option field, @@ -169,12 +147,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetSInt64(int field, out long value) - { - ulong? tmp = GetLastNumericValue(field); - value = CodedInputStream.DecodeZigZag64(tmp.GetValueOrDefault()); - return tmp != null; - } + public bool TryGetSInt64(int field, out long value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves an unsigned 32-bit integer value for the specified option field. @@ -182,12 +155,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetUInt32(int field, out uint value) - { - ulong? tmp = GetLastNumericValue(field); - value = (uint) tmp.GetValueOrDefault(); - return tmp != null; - } + public bool TryGetUInt32(int field, out uint value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves an unsigned 64-bit integer value for the specified option field. @@ -195,12 +163,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetUInt64(int field, out ulong value) - { - ulong? tmp = GetLastNumericValue(field); - value = tmp.GetValueOrDefault(); - return tmp != null; - } + public bool TryGetUInt64(int field, out ulong value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves a 32-bit floating point value for the specified option field. @@ -208,14 +171,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetFloat(int field, out float value) - { - ulong? tmp = GetLastNumericValue(field); - int int32 = (int) tmp.GetValueOrDefault(); - byte[] bytes = BitConverter.GetBytes(int32); - value = BitConverter.ToSingle(bytes, 0); - return tmp != null; - } + public bool TryGetFloat(int field, out float value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves a 64-bit floating point value for the specified option field. @@ -223,12 +179,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetDouble(int field, out double value) - { - ulong? tmp = GetLastNumericValue(field); - value = BitConverter.Int64BitsToDouble((long) tmp.GetValueOrDefault()); - return tmp != null; - } + public bool TryGetDouble(int field, out double value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves a string value for the specified option field. @@ -236,12 +187,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetString(int field, out string value) - { - ByteString bytes = GetLastByteStringValue(field); - value = bytes?.ToStringUtf8(); - return bytes != null; - } + public bool TryGetString(int field, out string value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves a bytes value for the specified option field. @@ -249,12 +195,7 @@ namespace Google.Protobuf.Reflection /// The field to fetch the value for. /// The output variable to populate. /// true if a suitable value for the field was found; false otherwise. - public bool TryGetBytes(int field, out ByteString value) - { - ByteString bytes = GetLastByteStringValue(field); - value = bytes; - return bytes != null; - } + public bool TryGetBytes(int field, out ByteString value) => TryGetPrimitiveValue(field, out value); /// /// Retrieves a message value for the specified option field. @@ -264,127 +205,106 @@ namespace Google.Protobuf.Reflection /// true if a suitable value for the field was found; false otherwise. public bool TryGetMessage(int field, out T value) where T : class, IMessage, new() { - value = null; - List values; - if (!valuesByField.TryGetValue(field, out values)) + if (values == null) { + value = default(T); return false; } - foreach (FieldValue fieldValue in values) - { - if (fieldValue.ByteString != null) - { - if (value == null) - { - value = new T(); - } - value.MergeFrom(fieldValue.ByteString); - } - } - return value != null; - } - private ulong? GetLastNumericValue(int field) - { - List values; - if (!valuesByField.TryGetValue(field, out values)) + IExtensionValue extensionValue; + if (values.TryGetValue(field, out extensionValue)) { - return null; - } - for (int i = values.Count - 1; i >= 0; i--) - { - // A non-bytestring value is a numeric value - if (values[i].ByteString == null) + if (extensionValue is ExtensionValue) { - return values[i].Number; + ExtensionValue single = extensionValue as ExtensionValue; + ByteString bytes = single.GetValue().ToByteString(); + value = new T(); + value.MergeFrom(bytes); + return true; } - } - return null; - } - - private ByteString GetLastByteStringValue(int field) - { - List values; - if (!valuesByField.TryGetValue(field, out values)) - { - return null; - } - for (int i = values.Count - 1; i >= 0; i--) - { - if (values[i].ByteString != null) + else if (extensionValue is RepeatedExtensionValue) { - return values[i].ByteString; + RepeatedExtensionValue repeated = extensionValue as RepeatedExtensionValue; + value = repeated.GetValue() + .Select(v => v.ToByteString()) + .Aggregate(new T(), (t, b) => + { + t.MergeFrom(b); + return t; + }); + return true; } } - return null; - } - /// - /// Reads an unknown field, either parsing it and storing it or skipping it. - /// - /// - /// If the current set of options is empty and we manage to read a field, a new set of options - /// will be created and returned. Otherwise, the return value is this. This allows - /// us to start with a singleton empty set of options and just create new ones where necessary. - /// - /// Input stream to read from. - /// The resulting set of custom options, either this or a new set. - internal CustomOptions ReadOrSkipUnknownField(CodedInputStream input) - { - var tag = input.LastTag; - var field = WireFormat.GetTagFieldNumber(tag); - switch (WireFormat.GetTagWireType(tag)) - { - case WireFormat.WireType.LengthDelimited: - return AddValue(field, new FieldValue(input.ReadBytes())); - case WireFormat.WireType.Fixed32: - return AddValue(field, new FieldValue(input.ReadFixed32())); - case WireFormat.WireType.Fixed64: - return AddValue(field, new FieldValue(input.ReadFixed64())); - case WireFormat.WireType.Varint: - return AddValue(field, new FieldValue(input.ReadRawVarint64())); - // For StartGroup, EndGroup or any wire format we don't understand, - // just use the normal behavior (call SkipLastField). - default: - input.SkipLastField(); - return this; - } + value = null; + return false; } - private CustomOptions AddValue(int field, FieldValue value) + private bool TryGetPrimitiveValue(int field, out T value) { - var ret = valuesByField.Count == 0 ? new CustomOptions() : this; - List valuesForField; - if (!ret.valuesByField.TryGetValue(field, out valuesForField)) + if (values == null) { - // Expect almost all - valuesForField = new List(1); - ret.valuesByField[field] = valuesForField; + value = default(T); + return false; } - valuesForField.Add(value); - return ret; - } - - /// - /// All field values can be stored as a byte string or a 64-bit integer. - /// This struct avoids unnecessary boxing. - /// - private struct FieldValue - { - internal ulong Number { get; } - internal ByteString ByteString { get; } - internal FieldValue(ulong number) + IExtensionValue extensionValue; + if (values.TryGetValue(field, out extensionValue)) { - Number = number; - ByteString = null; + if (extensionValue is ExtensionValue) + { + ExtensionValue single = extensionValue as ExtensionValue; + if (single.HasValue) + { + value = single.GetValue(); + return true; + } + } + else if (extensionValue is RepeatedExtensionValue) + { + RepeatedExtensionValue repeated = extensionValue as RepeatedExtensionValue; + if (repeated.GetValue().Count != 0) + { + RepeatedField repeatedField = repeated.GetValue(); + value = repeatedField[repeatedField.Count - 1]; + return true; + } + } + else // and here we find explicit enum handling since T : Enum ! x is ExtensionValue + { + var type = extensionValue.GetType(); + if (type.GetGenericTypeDefinition() == typeof(ExtensionValue<>)) + { + var typeInfo = type.GetTypeInfo(); + var typeArgs = typeInfo.GenericTypeArguments; + if (typeArgs.Length == 1 && typeArgs[0].GetTypeInfo().IsEnum) + { + if ((bool)typeInfo.GetDeclaredProperty(nameof(ExtensionValue.HasValue)).GetValue(extensionValue)) + { + value = (T)typeInfo.GetDeclaredMethod(nameof(ExtensionValue.GetValue)).Invoke(extensionValue, EmptyParameters); + return true; + } + } + } + else if (type.GetGenericTypeDefinition() == typeof(RepeatedExtensionValue<>)) + { + var typeInfo = type.GetTypeInfo(); + var typeArgs = typeInfo.GenericTypeArguments; + if (typeArgs.Length == 1 && typeArgs[0].GetTypeInfo().IsEnum) + { + var values = (IList)typeInfo.GetDeclaredMethod(nameof(RepeatedExtensionValue.GetValue)).Invoke(extensionValue, EmptyParameters); + if (values.Count != 0) + { + value = (T)values[values.Count - 1]; + return true; + } + } + } + } } - internal FieldValue(ByteString byteString) - { - Number = 0; - ByteString = byteString; - } + value = default(T); + return false; } } } diff --git a/csharp/src/Google.Protobuf/Reflection/Descriptor.cs b/csharp/src/Google.Protobuf/Reflection/Descriptor.cs index 4cbed33b65..162f17512e 100644 --- a/csharp/src/Google.Protobuf/Reflection/Descriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/Descriptor.cs @@ -160,29 +160,29 @@ namespace Google.Protobuf.Reflection { "AQGiAgNHUEKqAhpHb29nbGUuUHJvdG9idWYuUmVmbGVjdGlvbg==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FileDescriptorSet), global::Google.Protobuf.Reflection.FileDescriptorSet.Parser, new[]{ "File" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FileDescriptorProto), global::Google.Protobuf.Reflection.FileDescriptorProto.Parser, new[]{ "Name", "Package", "Dependency", "PublicDependency", "WeakDependency", "MessageType", "EnumType", "Service", "Extension", "Options", "SourceCodeInfo", "Syntax" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto), global::Google.Protobuf.Reflection.DescriptorProto.Parser, new[]{ "Name", "Field", "Extension", "NestedType", "EnumType", "ExtensionRange", "OneofDecl", "Options", "ReservedRange", "ReservedName" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto.Types.ExtensionRange), global::Google.Protobuf.Reflection.DescriptorProto.Types.ExtensionRange.Parser, new[]{ "Start", "End", "Options" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto.Types.ReservedRange), global::Google.Protobuf.Reflection.DescriptorProto.Types.ReservedRange.Parser, new[]{ "Start", "End" }, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.ExtensionRangeOptions), global::Google.Protobuf.Reflection.ExtensionRangeOptions.Parser, new[]{ "UninterpretedOption" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto), global::Google.Protobuf.Reflection.FieldDescriptorProto.Parser, new[]{ "Name", "Number", "Label", "Type", "TypeName", "Extendee", "DefaultValue", "OneofIndex", "JsonName", "Options" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type), typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label) }, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.OneofDescriptorProto), global::Google.Protobuf.Reflection.OneofDescriptorProto.Parser, new[]{ "Name", "Options" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumDescriptorProto), global::Google.Protobuf.Reflection.EnumDescriptorProto.Parser, new[]{ "Name", "Value", "Options", "ReservedRange", "ReservedName" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumDescriptorProto.Types.EnumReservedRange), global::Google.Protobuf.Reflection.EnumDescriptorProto.Types.EnumReservedRange.Parser, new[]{ "Start", "End" }, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumValueDescriptorProto), global::Google.Protobuf.Reflection.EnumValueDescriptorProto.Parser, new[]{ "Name", "Number", "Options" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.ServiceDescriptorProto), global::Google.Protobuf.Reflection.ServiceDescriptorProto.Parser, new[]{ "Name", "Method", "Options" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.MethodDescriptorProto), global::Google.Protobuf.Reflection.MethodDescriptorProto.Parser, new[]{ "Name", "InputType", "OutputType", "Options", "ClientStreaming", "ServerStreaming" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FileOptions), global::Google.Protobuf.Reflection.FileOptions.Parser, new[]{ "JavaPackage", "JavaOuterClassname", "JavaMultipleFiles", "JavaGenerateEqualsAndHash", "JavaStringCheckUtf8", "OptimizeFor", "GoPackage", "CcGenericServices", "JavaGenericServices", "PyGenericServices", "PhpGenericServices", "Deprecated", "CcEnableArenas", "ObjcClassPrefix", "CsharpNamespace", "SwiftPrefix", "PhpClassPrefix", "PhpNamespace", "PhpMetadataNamespace", "RubyPackage", "UninterpretedOption" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode) }, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.MessageOptions), global::Google.Protobuf.Reflection.MessageOptions.Parser, new[]{ "MessageSetWireFormat", "NoStandardDescriptorAccessor", "Deprecated", "MapEntry", "UninterpretedOption" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FieldOptions), global::Google.Protobuf.Reflection.FieldOptions.Parser, new[]{ "Ctype", "Packed", "Jstype", "Lazy", "Deprecated", "Weak", "UninterpretedOption" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FieldOptions.Types.CType), typeof(global::Google.Protobuf.Reflection.FieldOptions.Types.JSType) }, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.OneofOptions), global::Google.Protobuf.Reflection.OneofOptions.Parser, new[]{ "UninterpretedOption" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumOptions), global::Google.Protobuf.Reflection.EnumOptions.Parser, new[]{ "AllowAlias", "Deprecated", "UninterpretedOption" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumValueOptions), global::Google.Protobuf.Reflection.EnumValueOptions.Parser, new[]{ "Deprecated", "UninterpretedOption" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.ServiceOptions), global::Google.Protobuf.Reflection.ServiceOptions.Parser, new[]{ "Deprecated", "UninterpretedOption" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.MethodOptions), global::Google.Protobuf.Reflection.MethodOptions.Parser, new[]{ "Deprecated", "IdempotencyLevel", "UninterpretedOption" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel) }, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.UninterpretedOption), global::Google.Protobuf.Reflection.UninterpretedOption.Parser, new[]{ "Name", "IdentifierValue", "PositiveIntValue", "NegativeIntValue", "DoubleValue", "StringValue", "AggregateValue" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.UninterpretedOption.Types.NamePart), global::Google.Protobuf.Reflection.UninterpretedOption.Types.NamePart.Parser, new[]{ "NamePart_", "IsExtension" }, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.SourceCodeInfo), global::Google.Protobuf.Reflection.SourceCodeInfo.Parser, new[]{ "Location" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.SourceCodeInfo.Types.Location), global::Google.Protobuf.Reflection.SourceCodeInfo.Types.Location.Parser, new[]{ "Path", "Span", "LeadingComments", "TrailingComments", "LeadingDetachedComments" }, null, null, null)}), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.GeneratedCodeInfo), global::Google.Protobuf.Reflection.GeneratedCodeInfo.Parser, new[]{ "Annotation" }, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.GeneratedCodeInfo.Types.Annotation), global::Google.Protobuf.Reflection.GeneratedCodeInfo.Types.Annotation.Parser, new[]{ "Path", "SourceFile", "Begin", "End" }, null, null, null)}) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FileDescriptorSet), global::Google.Protobuf.Reflection.FileDescriptorSet.Parser, new[]{ "File" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FileDescriptorProto), global::Google.Protobuf.Reflection.FileDescriptorProto.Parser, new[]{ "Name", "Package", "Dependency", "PublicDependency", "WeakDependency", "MessageType", "EnumType", "Service", "Extension", "Options", "SourceCodeInfo", "Syntax" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto), global::Google.Protobuf.Reflection.DescriptorProto.Parser, new[]{ "Name", "Field", "Extension", "NestedType", "EnumType", "ExtensionRange", "OneofDecl", "Options", "ReservedRange", "ReservedName" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto.Types.ExtensionRange), global::Google.Protobuf.Reflection.DescriptorProto.Types.ExtensionRange.Parser, new[]{ "Start", "End", "Options" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto.Types.ReservedRange), global::Google.Protobuf.Reflection.DescriptorProto.Types.ReservedRange.Parser, new[]{ "Start", "End" }, null, null, null, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.ExtensionRangeOptions), global::Google.Protobuf.Reflection.ExtensionRangeOptions.Parser, new[]{ "UninterpretedOption" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto), global::Google.Protobuf.Reflection.FieldDescriptorProto.Parser, new[]{ "Name", "Number", "Label", "Type", "TypeName", "Extendee", "DefaultValue", "OneofIndex", "JsonName", "Options" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type), typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label) }, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.OneofDescriptorProto), global::Google.Protobuf.Reflection.OneofDescriptorProto.Parser, new[]{ "Name", "Options" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumDescriptorProto), global::Google.Protobuf.Reflection.EnumDescriptorProto.Parser, new[]{ "Name", "Value", "Options", "ReservedRange", "ReservedName" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumDescriptorProto.Types.EnumReservedRange), global::Google.Protobuf.Reflection.EnumDescriptorProto.Types.EnumReservedRange.Parser, new[]{ "Start", "End" }, null, null, null, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumValueDescriptorProto), global::Google.Protobuf.Reflection.EnumValueDescriptorProto.Parser, new[]{ "Name", "Number", "Options" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.ServiceDescriptorProto), global::Google.Protobuf.Reflection.ServiceDescriptorProto.Parser, new[]{ "Name", "Method", "Options" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.MethodDescriptorProto), global::Google.Protobuf.Reflection.MethodDescriptorProto.Parser, new[]{ "Name", "InputType", "OutputType", "Options", "ClientStreaming", "ServerStreaming" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FileOptions), global::Google.Protobuf.Reflection.FileOptions.Parser, new[]{ "JavaPackage", "JavaOuterClassname", "JavaMultipleFiles", "JavaGenerateEqualsAndHash", "JavaStringCheckUtf8", "OptimizeFor", "GoPackage", "CcGenericServices", "JavaGenericServices", "PyGenericServices", "PhpGenericServices", "Deprecated", "CcEnableArenas", "ObjcClassPrefix", "CsharpNamespace", "SwiftPrefix", "PhpClassPrefix", "PhpNamespace", "PhpMetadataNamespace", "RubyPackage", "UninterpretedOption" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode) }, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.MessageOptions), global::Google.Protobuf.Reflection.MessageOptions.Parser, new[]{ "MessageSetWireFormat", "NoStandardDescriptorAccessor", "Deprecated", "MapEntry", "UninterpretedOption" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.FieldOptions), global::Google.Protobuf.Reflection.FieldOptions.Parser, new[]{ "Ctype", "Packed", "Jstype", "Lazy", "Deprecated", "Weak", "UninterpretedOption" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FieldOptions.Types.CType), typeof(global::Google.Protobuf.Reflection.FieldOptions.Types.JSType) }, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.OneofOptions), global::Google.Protobuf.Reflection.OneofOptions.Parser, new[]{ "UninterpretedOption" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumOptions), global::Google.Protobuf.Reflection.EnumOptions.Parser, new[]{ "AllowAlias", "Deprecated", "UninterpretedOption" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.EnumValueOptions), global::Google.Protobuf.Reflection.EnumValueOptions.Parser, new[]{ "Deprecated", "UninterpretedOption" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.ServiceOptions), global::Google.Protobuf.Reflection.ServiceOptions.Parser, new[]{ "Deprecated", "UninterpretedOption" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.MethodOptions), global::Google.Protobuf.Reflection.MethodOptions.Parser, new[]{ "Deprecated", "IdempotencyLevel", "UninterpretedOption" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel) }, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.UninterpretedOption), global::Google.Protobuf.Reflection.UninterpretedOption.Parser, new[]{ "Name", "IdentifierValue", "PositiveIntValue", "NegativeIntValue", "DoubleValue", "StringValue", "AggregateValue" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.UninterpretedOption.Types.NamePart), global::Google.Protobuf.Reflection.UninterpretedOption.Types.NamePart.Parser, new[]{ "NamePart_", "IsExtension" }, null, null, null, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.SourceCodeInfo), global::Google.Protobuf.Reflection.SourceCodeInfo.Parser, new[]{ "Location" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.SourceCodeInfo.Types.Location), global::Google.Protobuf.Reflection.SourceCodeInfo.Types.Location.Parser, new[]{ "Path", "Span", "LeadingComments", "TrailingComments", "LeadingDetachedComments" }, null, null, null, null)}), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.GeneratedCodeInfo), global::Google.Protobuf.Reflection.GeneratedCodeInfo.Parser, new[]{ "Annotation" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.Reflection.GeneratedCodeInfo.Types.Annotation), global::Google.Protobuf.Reflection.GeneratedCodeInfo.Types.Annotation.Parser, new[]{ "Path", "SourceFile", "Begin", "End" }, null, null, null, null)}) })); } #endregion @@ -351,8 +351,8 @@ namespace Google.Protobuf.Reflection { enumType_ = other.enumType_.Clone(); service_ = other.service_.Clone(); extension_ = other.extension_.Clone(); - options_ = other.options_ != null ? other.options_.Clone() : null; - sourceCodeInfo_ = other.sourceCodeInfo_ != null ? other.sourceCodeInfo_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; + sourceCodeInfo_ = other.HasSourceCodeInfo ? other.sourceCodeInfo_.Clone() : null; syntax_ = other.syntax_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } @@ -364,31 +364,55 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name" field. public const int NameFieldNumber = 1; - private string name_ = ""; + private readonly static string NameDefaultValue = ""; + + private string name_; /// /// file name, relative to root of source tree /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Name { - get { return name_; } + get { return name_ ?? NameDefaultValue; } set { name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasName { + get { return name_ != null; } + } + /// Clears the value of the "name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearName() { + name_ = null; + } /// Field number for the "package" field. public const int PackageFieldNumber = 2; - private string package_ = ""; + private readonly static string PackageDefaultValue = ""; + + private string package_; /// /// e.g. "foo", "foo.bar", etc. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Package { - get { return package_; } + get { return package_ ?? PackageDefaultValue; } set { package_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "package" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasPackage { + get { return package_ != null; } + } + /// Clears the value of the "package" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearPackage() { + package_ = null; + } /// Field number for the "dependency" field. public const int DependencyFieldNumber = 3; @@ -483,6 +507,16 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } /// Field number for the "source_code_info" field. public const int SourceCodeInfoFieldNumber = 9; @@ -500,21 +534,43 @@ namespace Google.Protobuf.Reflection { sourceCodeInfo_ = value; } } + /// Gets whether the source_code_info field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasSourceCodeInfo { + get { return sourceCodeInfo_ != null; } + } + /// Clears the value of the source_code_info field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearSourceCodeInfo() { + sourceCodeInfo_ = null; + } /// Field number for the "syntax" field. public const int SyntaxFieldNumber = 12; - private string syntax_ = ""; + private readonly static string SyntaxDefaultValue = ""; + + private string syntax_; /// /// The syntax of the proto file. /// The supported values are "proto2" and "proto3". /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Syntax { - get { return syntax_; } + get { return syntax_ ?? SyntaxDefaultValue; } set { syntax_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "syntax" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasSyntax { + get { return syntax_ != null; } + } + /// Clears the value of the "syntax" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearSyntax() { + syntax_ = null; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -547,8 +603,8 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Name.Length != 0) hash ^= Name.GetHashCode(); - if (Package.Length != 0) hash ^= Package.GetHashCode(); + if (HasName) hash ^= Name.GetHashCode(); + if (HasPackage) hash ^= Package.GetHashCode(); hash ^= dependency_.GetHashCode(); hash ^= publicDependency_.GetHashCode(); hash ^= weakDependency_.GetHashCode(); @@ -556,9 +612,9 @@ namespace Google.Protobuf.Reflection { hash ^= enumType_.GetHashCode(); hash ^= service_.GetHashCode(); hash ^= extension_.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); - if (sourceCodeInfo_ != null) hash ^= SourceCodeInfo.GetHashCode(); - if (Syntax.Length != 0) hash ^= Syntax.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); + if (HasSourceCodeInfo) hash ^= SourceCodeInfo.GetHashCode(); + if (HasSyntax) hash ^= Syntax.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -572,11 +628,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Name.Length != 0) { + if (HasName) { output.WriteRawTag(10); output.WriteString(Name); } - if (Package.Length != 0) { + if (HasPackage) { output.WriteRawTag(18); output.WriteString(Package); } @@ -585,17 +641,17 @@ namespace Google.Protobuf.Reflection { enumType_.WriteTo(output, _repeated_enumType_codec); service_.WriteTo(output, _repeated_service_codec); extension_.WriteTo(output, _repeated_extension_codec); - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(66); output.WriteMessage(Options); } - if (sourceCodeInfo_ != null) { + if (HasSourceCodeInfo) { output.WriteRawTag(74); output.WriteMessage(SourceCodeInfo); } publicDependency_.WriteTo(output, _repeated_publicDependency_codec); weakDependency_.WriteTo(output, _repeated_weakDependency_codec); - if (Syntax.Length != 0) { + if (HasSyntax) { output.WriteRawTag(98); output.WriteString(Syntax); } @@ -607,10 +663,10 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Name.Length != 0) { + if (HasName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); } - if (Package.Length != 0) { + if (HasPackage) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Package); } size += dependency_.CalculateSize(_repeated_dependency_codec); @@ -620,13 +676,13 @@ namespace Google.Protobuf.Reflection { size += enumType_.CalculateSize(_repeated_enumType_codec); size += service_.CalculateSize(_repeated_service_codec); size += extension_.CalculateSize(_repeated_extension_codec); - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } - if (sourceCodeInfo_ != null) { + if (HasSourceCodeInfo) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(SourceCodeInfo); } - if (Syntax.Length != 0) { + if (HasSyntax) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Syntax); } if (_unknownFields != null) { @@ -640,10 +696,10 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Name.Length != 0) { + if (other.HasName) { Name = other.Name; } - if (other.Package.Length != 0) { + if (other.HasPackage) { Package = other.Package; } dependency_.Add(other.dependency_); @@ -653,19 +709,19 @@ namespace Google.Protobuf.Reflection { enumType_.Add(other.enumType_); service_.Add(other.service_); extension_.Add(other.extension_); - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.FileOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.FileOptions(); } Options.MergeFrom(other.Options); } - if (other.sourceCodeInfo_ != null) { - if (sourceCodeInfo_ == null) { - sourceCodeInfo_ = new global::Google.Protobuf.Reflection.SourceCodeInfo(); + if (other.HasSourceCodeInfo) { + if (!HasSourceCodeInfo) { + SourceCodeInfo = new global::Google.Protobuf.Reflection.SourceCodeInfo(); } SourceCodeInfo.MergeFrom(other.SourceCodeInfo); } - if (other.Syntax.Length != 0) { + if (other.HasSyntax) { Syntax = other.Syntax; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -708,17 +764,17 @@ namespace Google.Protobuf.Reflection { break; } case 66: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.FileOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.FileOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } case 74: { - if (sourceCodeInfo_ == null) { - sourceCodeInfo_ = new global::Google.Protobuf.Reflection.SourceCodeInfo(); + if (!HasSourceCodeInfo) { + SourceCodeInfo = new global::Google.Protobuf.Reflection.SourceCodeInfo(); } - input.ReadMessage(sourceCodeInfo_); + input.ReadMessage(SourceCodeInfo); break; } case 82: @@ -776,7 +832,7 @@ namespace Google.Protobuf.Reflection { enumType_ = other.enumType_.Clone(); extensionRange_ = other.extensionRange_.Clone(); oneofDecl_ = other.oneofDecl_.Clone(); - options_ = other.options_ != null ? other.options_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; reservedRange_ = other.reservedRange_.Clone(); reservedName_ = other.reservedName_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); @@ -789,14 +845,26 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name" field. public const int NameFieldNumber = 1; - private string name_ = ""; + private readonly static string NameDefaultValue = ""; + + private string name_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Name { - get { return name_; } + get { return name_ ?? NameDefaultValue; } set { name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasName { + get { return name_ != null; } + } + /// Clears the value of the "name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearName() { + name_ = null; + } /// Field number for the "field" field. public const int FieldFieldNumber = 2; @@ -868,6 +936,16 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } /// Field number for the "reserved_range" field. public const int ReservedRangeFieldNumber = 9; @@ -922,14 +1000,14 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Name.Length != 0) hash ^= Name.GetHashCode(); + if (HasName) hash ^= Name.GetHashCode(); hash ^= field_.GetHashCode(); hash ^= extension_.GetHashCode(); hash ^= nestedType_.GetHashCode(); hash ^= enumType_.GetHashCode(); hash ^= extensionRange_.GetHashCode(); hash ^= oneofDecl_.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); hash ^= reservedRange_.GetHashCode(); hash ^= reservedName_.GetHashCode(); if (_unknownFields != null) { @@ -945,7 +1023,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Name.Length != 0) { + if (HasName) { output.WriteRawTag(10); output.WriteString(Name); } @@ -954,7 +1032,7 @@ namespace Google.Protobuf.Reflection { enumType_.WriteTo(output, _repeated_enumType_codec); extensionRange_.WriteTo(output, _repeated_extensionRange_codec); extension_.WriteTo(output, _repeated_extension_codec); - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(58); output.WriteMessage(Options); } @@ -969,7 +1047,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Name.Length != 0) { + if (HasName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); } size += field_.CalculateSize(_repeated_field_codec); @@ -978,7 +1056,7 @@ namespace Google.Protobuf.Reflection { size += enumType_.CalculateSize(_repeated_enumType_codec); size += extensionRange_.CalculateSize(_repeated_extensionRange_codec); size += oneofDecl_.CalculateSize(_repeated_oneofDecl_codec); - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } size += reservedRange_.CalculateSize(_repeated_reservedRange_codec); @@ -994,7 +1072,7 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Name.Length != 0) { + if (other.HasName) { Name = other.Name; } field_.Add(other.field_); @@ -1003,9 +1081,9 @@ namespace Google.Protobuf.Reflection { enumType_.Add(other.enumType_); extensionRange_.Add(other.extensionRange_); oneofDecl_.Add(other.oneofDecl_); - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.MessageOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.MessageOptions(); } Options.MergeFrom(other.Options); } @@ -1047,10 +1125,10 @@ namespace Google.Protobuf.Reflection { break; } case 58: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.MessageOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.MessageOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } case 66: { @@ -1076,6 +1154,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class ExtensionRange : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new ExtensionRange()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -1098,9 +1177,10 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public ExtensionRange(ExtensionRange other) : this() { + _hasBits0 = other._hasBits0; start_ = other.start_; end_ = other.end_; - options_ = other.options_ != null ? other.options_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } @@ -1111,25 +1191,57 @@ namespace Google.Protobuf.Reflection { /// Field number for the "start" field. public const int StartFieldNumber = 1; + private readonly static int StartDefaultValue = 0; + private int start_; + /// + /// Inclusive. + /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int Start { - get { return start_; } + get { if ((_hasBits0 & 1) != 0) { return start_; } else { return StartDefaultValue; } } set { + _hasBits0 |= 1; start_ = value; } } + /// Gets whether the "start" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasStart { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "start" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearStart() { + _hasBits0 &= ~1; + } /// Field number for the "end" field. public const int EndFieldNumber = 2; + private readonly static int EndDefaultValue = 0; + private int end_; + /// + /// Exclusive. + /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int End { - get { return end_; } + get { if ((_hasBits0 & 2) != 0) { return end_; } else { return EndDefaultValue; } } set { + _hasBits0 |= 2; end_ = value; } } + /// Gets whether the "end" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasEnd { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "end" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearEnd() { + _hasBits0 &= ~2; + } /// Field number for the "options" field. public const int OptionsFieldNumber = 3; @@ -1141,6 +1253,16 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -1164,9 +1286,9 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Start != 0) hash ^= Start.GetHashCode(); - if (End != 0) hash ^= End.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); + if (HasStart) hash ^= Start.GetHashCode(); + if (HasEnd) hash ^= End.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -1180,15 +1302,15 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Start != 0) { + if (HasStart) { output.WriteRawTag(8); output.WriteInt32(Start); } - if (End != 0) { + if (HasEnd) { output.WriteRawTag(16); output.WriteInt32(End); } - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(26); output.WriteMessage(Options); } @@ -1200,13 +1322,13 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Start != 0) { + if (HasStart) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(Start); } - if (End != 0) { + if (HasEnd) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(End); } - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } if (_unknownFields != null) { @@ -1220,15 +1342,15 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Start != 0) { + if (other.HasStart) { Start = other.Start; } - if (other.End != 0) { + if (other.HasEnd) { End = other.End; } - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.ExtensionRangeOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.ExtensionRangeOptions(); } Options.MergeFrom(other.Options); } @@ -1252,10 +1374,10 @@ namespace Google.Protobuf.Reflection { break; } case 26: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.ExtensionRangeOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.ExtensionRangeOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } } @@ -1272,6 +1394,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class ReservedRange : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new ReservedRange()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -1294,6 +1417,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public ReservedRange(ReservedRange other) : this() { + _hasBits0 = other._hasBits0; start_ = other.start_; end_ = other.end_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); @@ -1306,31 +1430,57 @@ namespace Google.Protobuf.Reflection { /// Field number for the "start" field. public const int StartFieldNumber = 1; + private readonly static int StartDefaultValue = 0; + private int start_; /// /// Inclusive. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int Start { - get { return start_; } + get { if ((_hasBits0 & 1) != 0) { return start_; } else { return StartDefaultValue; } } set { + _hasBits0 |= 1; start_ = value; } } + /// Gets whether the "start" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasStart { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "start" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearStart() { + _hasBits0 &= ~1; + } /// Field number for the "end" field. public const int EndFieldNumber = 2; + private readonly static int EndDefaultValue = 0; + private int end_; /// /// Exclusive. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int End { - get { return end_; } + get { if ((_hasBits0 & 2) != 0) { return end_; } else { return EndDefaultValue; } } set { + _hasBits0 |= 2; end_ = value; } } + /// Gets whether the "end" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasEnd { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "end" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearEnd() { + _hasBits0 &= ~2; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -1353,8 +1503,8 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Start != 0) hash ^= Start.GetHashCode(); - if (End != 0) hash ^= End.GetHashCode(); + if (HasStart) hash ^= Start.GetHashCode(); + if (HasEnd) hash ^= End.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -1368,11 +1518,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Start != 0) { + if (HasStart) { output.WriteRawTag(8); output.WriteInt32(Start); } - if (End != 0) { + if (HasEnd) { output.WriteRawTag(16); output.WriteInt32(End); } @@ -1384,10 +1534,10 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Start != 0) { + if (HasStart) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(Start); } - if (End != 0) { + if (HasEnd) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(End); } if (_unknownFields != null) { @@ -1401,10 +1551,10 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Start != 0) { + if (other.HasStart) { Start = other.Start; } - if (other.End != 0) { + if (other.HasEnd) { End = other.End; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -1437,9 +1587,10 @@ namespace Google.Protobuf.Reflection { } - internal sealed partial class ExtensionRangeOptions : pb::IMessage { + internal sealed partial class ExtensionRangeOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new ExtensionRangeOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -1464,6 +1615,7 @@ namespace Google.Protobuf.Reflection { public ExtensionRangeOptions(ExtensionRangeOptions other) : this() { uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -1498,6 +1650,9 @@ namespace Google.Protobuf.Reflection { return true; } if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } @@ -1505,6 +1660,9 @@ namespace Google.Protobuf.Reflection { public override int GetHashCode() { int hash = 1; hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -1519,6 +1677,9 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -1528,6 +1689,9 @@ namespace Google.Protobuf.Reflection { public int CalculateSize() { int size = 0; size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -1540,6 +1704,7 @@ namespace Google.Protobuf.Reflection { return; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -1549,7 +1714,9 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 7994: { uninterpretedOption_.AddEntriesFrom(input, _repeated_uninterpretedOption_codec); @@ -1559,6 +1726,28 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + } /// @@ -1567,6 +1756,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class FieldDescriptorProto : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FieldDescriptorProto()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -1589,6 +1779,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public FieldDescriptorProto(FieldDescriptorProto other) : this() { + _hasBits0 = other._hasBits0; name_ = other.name_; number_ = other.number_; label_ = other.label_; @@ -1598,7 +1789,7 @@ namespace Google.Protobuf.Reflection { defaultValue_ = other.defaultValue_; oneofIndex_ = other.oneofIndex_; jsonName_ = other.jsonName_; - options_ = other.options_ != null ? other.options_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } @@ -1609,55 +1800,108 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name" field. public const int NameFieldNumber = 1; - private string name_ = ""; + private readonly static string NameDefaultValue = ""; + + private string name_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Name { - get { return name_; } + get { return name_ ?? NameDefaultValue; } set { name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasName { + get { return name_ != null; } + } + /// Clears the value of the "name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearName() { + name_ = null; + } /// Field number for the "number" field. public const int NumberFieldNumber = 3; + private readonly static int NumberDefaultValue = 0; + private int number_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int Number { - get { return number_; } + get { if ((_hasBits0 & 1) != 0) { return number_; } else { return NumberDefaultValue; } } set { + _hasBits0 |= 1; number_ = value; } } + /// Gets whether the "number" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasNumber { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "number" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearNumber() { + _hasBits0 &= ~1; + } /// Field number for the "label" field. public const int LabelFieldNumber = 4; - private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label label_ = 0; + private readonly static global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label LabelDefaultValue = global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label.Optional; + + private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label label_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label Label { - get { return label_; } + get { if ((_hasBits0 & 2) != 0) { return label_; } else { return LabelDefaultValue; } } set { + _hasBits0 |= 2; label_ = value; } } + /// Gets whether the "label" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasLabel { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "label" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearLabel() { + _hasBits0 &= ~2; + } /// Field number for the "type" field. public const int TypeFieldNumber = 5; - private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type type_ = 0; + private readonly static global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type TypeDefaultValue = global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type.Double; + + private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type type_; /// /// If type_name is set, this need not be set. If both this and type_name /// are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or TYPE_GROUP. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type Type { - get { return type_; } + get { if ((_hasBits0 & 4) != 0) { return type_; } else { return TypeDefaultValue; } } set { + _hasBits0 |= 4; type_ = value; } } + /// Gets whether the "type" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasType { + get { return (_hasBits0 & 4) != 0; } + } + /// Clears the value of the "type" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearType() { + _hasBits0 &= ~4; + } /// Field number for the "type_name" field. public const int TypeNameFieldNumber = 6; - private string typeName_ = ""; + private readonly static string TypeNameDefaultValue = ""; + + private string typeName_; /// /// For message and enum types, this is the name of the type. If the name /// starts with a '.', it is fully-qualified. Otherwise, C++-like scoping @@ -1667,30 +1911,54 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string TypeName { - get { return typeName_; } + get { return typeName_ ?? TypeNameDefaultValue; } set { typeName_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "type_name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasTypeName { + get { return typeName_ != null; } + } + /// Clears the value of the "type_name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearTypeName() { + typeName_ = null; + } /// Field number for the "extendee" field. public const int ExtendeeFieldNumber = 2; - private string extendee_ = ""; + private readonly static string ExtendeeDefaultValue = ""; + + private string extendee_; /// /// For extensions, this is the name of the type being extended. It is /// resolved in the same manner as type_name. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Extendee { - get { return extendee_; } + get { return extendee_ ?? ExtendeeDefaultValue; } set { extendee_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "extendee" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasExtendee { + get { return extendee_ != null; } + } + /// Clears the value of the "extendee" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearExtendee() { + extendee_ = null; + } /// Field number for the "default_value" field. public const int DefaultValueFieldNumber = 7; - private string defaultValue_ = ""; + private readonly static string DefaultValueDefaultValue = ""; + + private string defaultValue_; /// /// For numeric types, contains the original text representation of the value. /// For booleans, "true" or "false". @@ -1700,14 +1968,26 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string DefaultValue { - get { return defaultValue_; } + get { return defaultValue_ ?? DefaultValueDefaultValue; } set { defaultValue_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "default_value" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDefaultValue { + get { return defaultValue_ != null; } + } + /// Clears the value of the "default_value" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDefaultValue() { + defaultValue_ = null; + } /// Field number for the "oneof_index" field. public const int OneofIndexFieldNumber = 9; + private readonly static int OneofIndexDefaultValue = 0; + private int oneofIndex_; /// /// If set, gives the index of a oneof in the containing type's oneof_decl @@ -1715,15 +1995,28 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int OneofIndex { - get { return oneofIndex_; } + get { if ((_hasBits0 & 8) != 0) { return oneofIndex_; } else { return OneofIndexDefaultValue; } } set { + _hasBits0 |= 8; oneofIndex_ = value; } } + /// Gets whether the "oneof_index" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOneofIndex { + get { return (_hasBits0 & 8) != 0; } + } + /// Clears the value of the "oneof_index" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOneofIndex() { + _hasBits0 &= ~8; + } /// Field number for the "json_name" field. public const int JsonNameFieldNumber = 10; - private string jsonName_ = ""; + private readonly static string JsonNameDefaultValue = ""; + + private string jsonName_; /// /// JSON name of this field. The value is set by protocol compiler. If the /// user has set a "json_name" option on this field, that option's value @@ -1732,11 +2025,21 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string JsonName { - get { return jsonName_; } + get { return jsonName_ ?? JsonNameDefaultValue; } set { jsonName_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "json_name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasJsonName { + get { return jsonName_ != null; } + } + /// Clears the value of the "json_name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearJsonName() { + jsonName_ = null; + } /// Field number for the "options" field. public const int OptionsFieldNumber = 8; @@ -1748,6 +2051,16 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -1778,16 +2091,16 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Name.Length != 0) hash ^= Name.GetHashCode(); - if (Number != 0) hash ^= Number.GetHashCode(); - if (Label != 0) hash ^= Label.GetHashCode(); - if (Type != 0) hash ^= Type.GetHashCode(); - if (TypeName.Length != 0) hash ^= TypeName.GetHashCode(); - if (Extendee.Length != 0) hash ^= Extendee.GetHashCode(); - if (DefaultValue.Length != 0) hash ^= DefaultValue.GetHashCode(); - if (OneofIndex != 0) hash ^= OneofIndex.GetHashCode(); - if (JsonName.Length != 0) hash ^= JsonName.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); + if (HasName) hash ^= Name.GetHashCode(); + if (HasNumber) hash ^= Number.GetHashCode(); + if (HasLabel) hash ^= Label.GetHashCode(); + if (HasType) hash ^= Type.GetHashCode(); + if (HasTypeName) hash ^= TypeName.GetHashCode(); + if (HasExtendee) hash ^= Extendee.GetHashCode(); + if (HasDefaultValue) hash ^= DefaultValue.GetHashCode(); + if (HasOneofIndex) hash ^= OneofIndex.GetHashCode(); + if (HasJsonName) hash ^= JsonName.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -1801,43 +2114,43 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Name.Length != 0) { + if (HasName) { output.WriteRawTag(10); output.WriteString(Name); } - if (Extendee.Length != 0) { + if (HasExtendee) { output.WriteRawTag(18); output.WriteString(Extendee); } - if (Number != 0) { + if (HasNumber) { output.WriteRawTag(24); output.WriteInt32(Number); } - if (Label != 0) { + if (HasLabel) { output.WriteRawTag(32); output.WriteEnum((int) Label); } - if (Type != 0) { + if (HasType) { output.WriteRawTag(40); output.WriteEnum((int) Type); } - if (TypeName.Length != 0) { + if (HasTypeName) { output.WriteRawTag(50); output.WriteString(TypeName); } - if (DefaultValue.Length != 0) { + if (HasDefaultValue) { output.WriteRawTag(58); output.WriteString(DefaultValue); } - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(66); output.WriteMessage(Options); } - if (OneofIndex != 0) { + if (HasOneofIndex) { output.WriteRawTag(72); output.WriteInt32(OneofIndex); } - if (JsonName.Length != 0) { + if (HasJsonName) { output.WriteRawTag(82); output.WriteString(JsonName); } @@ -1849,34 +2162,34 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Name.Length != 0) { + if (HasName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); } - if (Number != 0) { + if (HasNumber) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(Number); } - if (Label != 0) { + if (HasLabel) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Label); } - if (Type != 0) { + if (HasType) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Type); } - if (TypeName.Length != 0) { + if (HasTypeName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(TypeName); } - if (Extendee.Length != 0) { + if (HasExtendee) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Extendee); } - if (DefaultValue.Length != 0) { + if (HasDefaultValue) { size += 1 + pb::CodedOutputStream.ComputeStringSize(DefaultValue); } - if (OneofIndex != 0) { + if (HasOneofIndex) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(OneofIndex); } - if (JsonName.Length != 0) { + if (HasJsonName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(JsonName); } - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } if (_unknownFields != null) { @@ -1890,36 +2203,36 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Name.Length != 0) { + if (other.HasName) { Name = other.Name; } - if (other.Number != 0) { + if (other.HasNumber) { Number = other.Number; } - if (other.Label != 0) { + if (other.HasLabel) { Label = other.Label; } - if (other.Type != 0) { + if (other.HasType) { Type = other.Type; } - if (other.TypeName.Length != 0) { + if (other.HasTypeName) { TypeName = other.TypeName; } - if (other.Extendee.Length != 0) { + if (other.HasExtendee) { Extendee = other.Extendee; } - if (other.DefaultValue.Length != 0) { + if (other.HasDefaultValue) { DefaultValue = other.DefaultValue; } - if (other.OneofIndex != 0) { + if (other.HasOneofIndex) { OneofIndex = other.OneofIndex; } - if (other.JsonName.Length != 0) { + if (other.HasJsonName) { JsonName = other.JsonName; } - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.FieldOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.FieldOptions(); } Options.MergeFrom(other.Options); } @@ -1947,11 +2260,11 @@ namespace Google.Protobuf.Reflection { break; } case 32: { - label_ = (global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label) input.ReadEnum(); + Label = (global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label) input.ReadEnum(); break; } case 40: { - type_ = (global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type) input.ReadEnum(); + Type = (global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type) input.ReadEnum(); break; } case 50: { @@ -1963,10 +2276,10 @@ namespace Google.Protobuf.Reflection { break; } case 66: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.FieldOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.FieldOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } case 72: { @@ -2079,7 +2392,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public OneofDescriptorProto(OneofDescriptorProto other) : this() { name_ = other.name_; - options_ = other.options_ != null ? other.options_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } @@ -2090,14 +2403,26 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name" field. public const int NameFieldNumber = 1; - private string name_ = ""; + private readonly static string NameDefaultValue = ""; + + private string name_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Name { - get { return name_; } + get { return name_ ?? NameDefaultValue; } set { name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasName { + get { return name_ != null; } + } + /// Clears the value of the "name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearName() { + name_ = null; + } /// Field number for the "options" field. public const int OptionsFieldNumber = 2; @@ -2109,6 +2434,16 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -2131,8 +2466,8 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Name.Length != 0) hash ^= Name.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); + if (HasName) hash ^= Name.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -2146,11 +2481,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Name.Length != 0) { + if (HasName) { output.WriteRawTag(10); output.WriteString(Name); } - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(18); output.WriteMessage(Options); } @@ -2162,10 +2497,10 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Name.Length != 0) { + if (HasName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); } - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } if (_unknownFields != null) { @@ -2179,12 +2514,12 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Name.Length != 0) { + if (other.HasName) { Name = other.Name; } - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.OneofOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.OneofOptions(); } Options.MergeFrom(other.Options); } @@ -2204,10 +2539,10 @@ namespace Google.Protobuf.Reflection { break; } case 18: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.OneofOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.OneofOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } } @@ -2246,7 +2581,7 @@ namespace Google.Protobuf.Reflection { public EnumDescriptorProto(EnumDescriptorProto other) : this() { name_ = other.name_; value_ = other.value_.Clone(); - options_ = other.options_ != null ? other.options_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; reservedRange_ = other.reservedRange_.Clone(); reservedName_ = other.reservedName_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); @@ -2259,14 +2594,26 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name" field. public const int NameFieldNumber = 1; - private string name_ = ""; + private readonly static string NameDefaultValue = ""; + + private string name_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Name { - get { return name_; } + get { return name_ ?? NameDefaultValue; } set { name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasName { + get { return name_ != null; } + } + /// Clears the value of the "name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearName() { + name_ = null; + } /// Field number for the "value" field. public const int ValueFieldNumber = 2; @@ -2288,6 +2635,16 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } /// Field number for the "reserved_range" field. public const int ReservedRangeFieldNumber = 4; @@ -2342,9 +2699,9 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Name.Length != 0) hash ^= Name.GetHashCode(); + if (HasName) hash ^= Name.GetHashCode(); hash ^= value_.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); hash ^= reservedRange_.GetHashCode(); hash ^= reservedName_.GetHashCode(); if (_unknownFields != null) { @@ -2360,12 +2717,12 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Name.Length != 0) { + if (HasName) { output.WriteRawTag(10); output.WriteString(Name); } value_.WriteTo(output, _repeated_value_codec); - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(26); output.WriteMessage(Options); } @@ -2379,11 +2736,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Name.Length != 0) { + if (HasName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); } size += value_.CalculateSize(_repeated_value_codec); - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } size += reservedRange_.CalculateSize(_repeated_reservedRange_codec); @@ -2399,13 +2756,13 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Name.Length != 0) { + if (other.HasName) { Name = other.Name; } value_.Add(other.value_); - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.EnumOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.EnumOptions(); } Options.MergeFrom(other.Options); } @@ -2431,10 +2788,10 @@ namespace Google.Protobuf.Reflection { break; } case 26: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.EnumOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.EnumOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } case 34: { @@ -2464,6 +2821,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class EnumReservedRange : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new EnumReservedRange()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -2486,6 +2844,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public EnumReservedRange(EnumReservedRange other) : this() { + _hasBits0 = other._hasBits0; start_ = other.start_; end_ = other.end_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); @@ -2498,31 +2857,57 @@ namespace Google.Protobuf.Reflection { /// Field number for the "start" field. public const int StartFieldNumber = 1; + private readonly static int StartDefaultValue = 0; + private int start_; /// /// Inclusive. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int Start { - get { return start_; } + get { if ((_hasBits0 & 1) != 0) { return start_; } else { return StartDefaultValue; } } set { + _hasBits0 |= 1; start_ = value; } } + /// Gets whether the "start" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasStart { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "start" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearStart() { + _hasBits0 &= ~1; + } /// Field number for the "end" field. public const int EndFieldNumber = 2; + private readonly static int EndDefaultValue = 0; + private int end_; /// /// Inclusive. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int End { - get { return end_; } + get { if ((_hasBits0 & 2) != 0) { return end_; } else { return EndDefaultValue; } } set { + _hasBits0 |= 2; end_ = value; } } + /// Gets whether the "end" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasEnd { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "end" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearEnd() { + _hasBits0 &= ~2; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -2545,8 +2930,8 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Start != 0) hash ^= Start.GetHashCode(); - if (End != 0) hash ^= End.GetHashCode(); + if (HasStart) hash ^= Start.GetHashCode(); + if (HasEnd) hash ^= End.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -2560,11 +2945,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Start != 0) { + if (HasStart) { output.WriteRawTag(8); output.WriteInt32(Start); } - if (End != 0) { + if (HasEnd) { output.WriteRawTag(16); output.WriteInt32(End); } @@ -2576,10 +2961,10 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Start != 0) { + if (HasStart) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(Start); } - if (End != 0) { + if (HasEnd) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(End); } if (_unknownFields != null) { @@ -2593,10 +2978,10 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Start != 0) { + if (other.HasStart) { Start = other.Start; } - if (other.End != 0) { + if (other.HasEnd) { End = other.End; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -2635,6 +3020,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class EnumValueDescriptorProto : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new EnumValueDescriptorProto()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -2657,9 +3043,10 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public EnumValueDescriptorProto(EnumValueDescriptorProto other) : this() { + _hasBits0 = other._hasBits0; name_ = other.name_; number_ = other.number_; - options_ = other.options_ != null ? other.options_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } @@ -2670,25 +3057,50 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name" field. public const int NameFieldNumber = 1; - private string name_ = ""; + private readonly static string NameDefaultValue = ""; + + private string name_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Name { - get { return name_; } + get { return name_ ?? NameDefaultValue; } set { name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasName { + get { return name_ != null; } + } + /// Clears the value of the "name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearName() { + name_ = null; + } /// Field number for the "number" field. public const int NumberFieldNumber = 2; + private readonly static int NumberDefaultValue = 0; + private int number_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int Number { - get { return number_; } + get { if ((_hasBits0 & 1) != 0) { return number_; } else { return NumberDefaultValue; } } set { + _hasBits0 |= 1; number_ = value; } } + /// Gets whether the "number" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasNumber { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "number" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearNumber() { + _hasBits0 &= ~1; + } /// Field number for the "options" field. public const int OptionsFieldNumber = 3; @@ -2700,6 +3112,16 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -2723,9 +3145,9 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Name.Length != 0) hash ^= Name.GetHashCode(); - if (Number != 0) hash ^= Number.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); + if (HasName) hash ^= Name.GetHashCode(); + if (HasNumber) hash ^= Number.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -2739,15 +3161,15 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Name.Length != 0) { + if (HasName) { output.WriteRawTag(10); output.WriteString(Name); } - if (Number != 0) { + if (HasNumber) { output.WriteRawTag(16); output.WriteInt32(Number); } - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(26); output.WriteMessage(Options); } @@ -2759,13 +3181,13 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Name.Length != 0) { + if (HasName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); } - if (Number != 0) { + if (HasNumber) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(Number); } - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } if (_unknownFields != null) { @@ -2779,15 +3201,15 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Name.Length != 0) { + if (other.HasName) { Name = other.Name; } - if (other.Number != 0) { + if (other.HasNumber) { Number = other.Number; } - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.EnumValueOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.EnumValueOptions(); } Options.MergeFrom(other.Options); } @@ -2811,10 +3233,10 @@ namespace Google.Protobuf.Reflection { break; } case 26: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.EnumValueOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.EnumValueOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } } @@ -2853,7 +3275,7 @@ namespace Google.Protobuf.Reflection { public ServiceDescriptorProto(ServiceDescriptorProto other) : this() { name_ = other.name_; method_ = other.method_.Clone(); - options_ = other.options_ != null ? other.options_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } @@ -2864,14 +3286,26 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name" field. public const int NameFieldNumber = 1; - private string name_ = ""; + private readonly static string NameDefaultValue = ""; + + private string name_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Name { - get { return name_; } + get { return name_ ?? NameDefaultValue; } set { name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasName { + get { return name_ != null; } + } + /// Clears the value of the "name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearName() { + name_ = null; + } /// Field number for the "method" field. public const int MethodFieldNumber = 2; @@ -2893,6 +3327,16 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -2916,9 +3360,9 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Name.Length != 0) hash ^= Name.GetHashCode(); + if (HasName) hash ^= Name.GetHashCode(); hash ^= method_.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -2932,12 +3376,12 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Name.Length != 0) { + if (HasName) { output.WriteRawTag(10); output.WriteString(Name); } method_.WriteTo(output, _repeated_method_codec); - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(26); output.WriteMessage(Options); } @@ -2949,11 +3393,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Name.Length != 0) { + if (HasName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); } size += method_.CalculateSize(_repeated_method_codec); - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } if (_unknownFields != null) { @@ -2967,13 +3411,13 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Name.Length != 0) { + if (other.HasName) { Name = other.Name; } method_.Add(other.method_); - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.ServiceOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.ServiceOptions(); } Options.MergeFrom(other.Options); } @@ -2997,10 +3441,10 @@ namespace Google.Protobuf.Reflection { break; } case 26: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.ServiceOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.ServiceOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } } @@ -3015,6 +3459,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class MethodDescriptorProto : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new MethodDescriptorProto()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -3037,10 +3482,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public MethodDescriptorProto(MethodDescriptorProto other) : this() { + _hasBits0 = other._hasBits0; name_ = other.name_; inputType_ = other.inputType_; outputType_ = other.outputType_; - options_ = other.options_ != null ? other.options_.Clone() : null; + options_ = other.HasOptions ? other.options_.Clone() : null; clientStreaming_ = other.clientStreaming_; serverStreaming_ = other.serverStreaming_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); @@ -3053,40 +3499,76 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name" field. public const int NameFieldNumber = 1; - private string name_ = ""; + private readonly static string NameDefaultValue = ""; + + private string name_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string Name { - get { return name_; } + get { return name_ ?? NameDefaultValue; } set { name_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasName { + get { return name_ != null; } + } + /// Clears the value of the "name" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearName() { + name_ = null; + } /// Field number for the "input_type" field. public const int InputTypeFieldNumber = 2; - private string inputType_ = ""; + private readonly static string InputTypeDefaultValue = ""; + + private string inputType_; /// /// Input and output type names. These are resolved in the same way as /// FieldDescriptorProto.type_name, but must refer to a message type. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string InputType { - get { return inputType_; } + get { return inputType_ ?? InputTypeDefaultValue; } set { inputType_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "input_type" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasInputType { + get { return inputType_ != null; } + } + /// Clears the value of the "input_type" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearInputType() { + inputType_ = null; + } /// Field number for the "output_type" field. public const int OutputTypeFieldNumber = 3; - private string outputType_ = ""; + private readonly static string OutputTypeDefaultValue = ""; + + private string outputType_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string OutputType { - get { return outputType_; } + get { return outputType_ ?? OutputTypeDefaultValue; } set { outputType_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "output_type" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOutputType { + get { return outputType_ != null; } + } + /// Clears the value of the "output_type" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOutputType() { + outputType_ = null; + } /// Field number for the "options" field. public const int OptionsFieldNumber = 4; @@ -3098,34 +3580,70 @@ namespace Google.Protobuf.Reflection { options_ = value; } } + /// Gets whether the options field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptions { + get { return options_ != null; } + } + /// Clears the value of the options field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptions() { + options_ = null; + } /// Field number for the "client_streaming" field. public const int ClientStreamingFieldNumber = 5; + private readonly static bool ClientStreamingDefaultValue = false; + private bool clientStreaming_; /// /// Identifies if client streams multiple client messages /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool ClientStreaming { - get { return clientStreaming_; } + get { if ((_hasBits0 & 1) != 0) { return clientStreaming_; } else { return ClientStreamingDefaultValue; } } set { + _hasBits0 |= 1; clientStreaming_ = value; } } + /// Gets whether the "client_streaming" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasClientStreaming { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "client_streaming" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearClientStreaming() { + _hasBits0 &= ~1; + } /// Field number for the "server_streaming" field. public const int ServerStreamingFieldNumber = 6; + private readonly static bool ServerStreamingDefaultValue = false; + private bool serverStreaming_; /// /// Identifies if server streams multiple server messages /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool ServerStreaming { - get { return serverStreaming_; } + get { if ((_hasBits0 & 2) != 0) { return serverStreaming_; } else { return ServerStreamingDefaultValue; } } set { + _hasBits0 |= 2; serverStreaming_ = value; } } + /// Gets whether the "server_streaming" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasServerStreaming { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "server_streaming" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearServerStreaming() { + _hasBits0 &= ~2; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -3152,12 +3670,12 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Name.Length != 0) hash ^= Name.GetHashCode(); - if (InputType.Length != 0) hash ^= InputType.GetHashCode(); - if (OutputType.Length != 0) hash ^= OutputType.GetHashCode(); - if (options_ != null) hash ^= Options.GetHashCode(); - if (ClientStreaming != false) hash ^= ClientStreaming.GetHashCode(); - if (ServerStreaming != false) hash ^= ServerStreaming.GetHashCode(); + if (HasName) hash ^= Name.GetHashCode(); + if (HasInputType) hash ^= InputType.GetHashCode(); + if (HasOutputType) hash ^= OutputType.GetHashCode(); + if (HasOptions) hash ^= Options.GetHashCode(); + if (HasClientStreaming) hash ^= ClientStreaming.GetHashCode(); + if (HasServerStreaming) hash ^= ServerStreaming.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -3171,27 +3689,27 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Name.Length != 0) { + if (HasName) { output.WriteRawTag(10); output.WriteString(Name); } - if (InputType.Length != 0) { + if (HasInputType) { output.WriteRawTag(18); output.WriteString(InputType); } - if (OutputType.Length != 0) { + if (HasOutputType) { output.WriteRawTag(26); output.WriteString(OutputType); } - if (options_ != null) { + if (HasOptions) { output.WriteRawTag(34); output.WriteMessage(Options); } - if (ClientStreaming != false) { + if (HasClientStreaming) { output.WriteRawTag(40); output.WriteBool(ClientStreaming); } - if (ServerStreaming != false) { + if (HasServerStreaming) { output.WriteRawTag(48); output.WriteBool(ServerStreaming); } @@ -3203,22 +3721,22 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Name.Length != 0) { + if (HasName) { size += 1 + pb::CodedOutputStream.ComputeStringSize(Name); } - if (InputType.Length != 0) { + if (HasInputType) { size += 1 + pb::CodedOutputStream.ComputeStringSize(InputType); } - if (OutputType.Length != 0) { + if (HasOutputType) { size += 1 + pb::CodedOutputStream.ComputeStringSize(OutputType); } - if (options_ != null) { + if (HasOptions) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(Options); } - if (ClientStreaming != false) { + if (HasClientStreaming) { size += 1 + 1; } - if (ServerStreaming != false) { + if (HasServerStreaming) { size += 1 + 1; } if (_unknownFields != null) { @@ -3232,25 +3750,25 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Name.Length != 0) { + if (other.HasName) { Name = other.Name; } - if (other.InputType.Length != 0) { + if (other.HasInputType) { InputType = other.InputType; } - if (other.OutputType.Length != 0) { + if (other.HasOutputType) { OutputType = other.OutputType; } - if (other.options_ != null) { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.MethodOptions(); + if (other.HasOptions) { + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.MethodOptions(); } Options.MergeFrom(other.Options); } - if (other.ClientStreaming != false) { + if (other.HasClientStreaming) { ClientStreaming = other.ClientStreaming; } - if (other.ServerStreaming != false) { + if (other.HasServerStreaming) { ServerStreaming = other.ServerStreaming; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -3277,10 +3795,10 @@ namespace Google.Protobuf.Reflection { break; } case 34: { - if (options_ == null) { - options_ = new global::Google.Protobuf.Reflection.MethodOptions(); + if (!HasOptions) { + Options = new global::Google.Protobuf.Reflection.MethodOptions(); } - input.ReadMessage(options_); + input.ReadMessage(Options); break; } case 40: { @@ -3297,9 +3815,11 @@ namespace Google.Protobuf.Reflection { } - internal sealed partial class FileOptions : pb::IMessage { + internal sealed partial class FileOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FileOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -3313,8 +3833,6 @@ namespace Google.Protobuf.Reflection { get { return Descriptor; } } - internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public FileOptions() { OnConstruction(); @@ -3324,6 +3842,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public FileOptions(FileOptions other) : this() { + _hasBits0 = other._hasBits0; javaPackage_ = other.javaPackage_; javaOuterClassname_ = other.javaOuterClassname_; javaMultipleFiles_ = other.javaMultipleFiles_; @@ -3346,6 +3865,7 @@ namespace Google.Protobuf.Reflection { rubyPackage_ = other.rubyPackage_; uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -3355,7 +3875,9 @@ namespace Google.Protobuf.Reflection { /// Field number for the "java_package" field. public const int JavaPackageFieldNumber = 1; - private string javaPackage_ = ""; + private readonly static string JavaPackageDefaultValue = ""; + + private string javaPackage_; /// /// Sets the Java package where classes generated from this .proto will be /// placed. By default, the proto package is used, but this is often @@ -3364,15 +3886,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string JavaPackage { - get { return javaPackage_; } + get { return javaPackage_ ?? JavaPackageDefaultValue; } set { javaPackage_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "java_package" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasJavaPackage { + get { return javaPackage_ != null; } + } + /// Clears the value of the "java_package" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearJavaPackage() { + javaPackage_ = null; + } /// Field number for the "java_outer_classname" field. public const int JavaOuterClassnameFieldNumber = 8; - private string javaOuterClassname_ = ""; + private readonly static string JavaOuterClassnameDefaultValue = ""; + + private string javaOuterClassname_; /// /// If set, all the classes from the .proto file are wrapped in a single /// outer class with the given name. This applies to both Proto1 @@ -3382,14 +3916,26 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string JavaOuterClassname { - get { return javaOuterClassname_; } + get { return javaOuterClassname_ ?? JavaOuterClassnameDefaultValue; } set { javaOuterClassname_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "java_outer_classname" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasJavaOuterClassname { + get { return javaOuterClassname_ != null; } + } + /// Clears the value of the "java_outer_classname" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearJavaOuterClassname() { + javaOuterClassname_ = null; + } /// Field number for the "java_multiple_files" field. public const int JavaMultipleFilesFieldNumber = 10; + private readonly static bool JavaMultipleFilesDefaultValue = false; + private bool javaMultipleFiles_; /// /// If set true, then the Java code generator will generate a separate .java @@ -3401,14 +3947,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool JavaMultipleFiles { - get { return javaMultipleFiles_; } + get { if ((_hasBits0 & 2) != 0) { return javaMultipleFiles_; } else { return JavaMultipleFilesDefaultValue; } } set { + _hasBits0 |= 2; javaMultipleFiles_ = value; } } + /// Gets whether the "java_multiple_files" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasJavaMultipleFiles { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "java_multiple_files" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearJavaMultipleFiles() { + _hasBits0 &= ~2; + } /// Field number for the "java_generate_equals_and_hash" field. public const int JavaGenerateEqualsAndHashFieldNumber = 20; + private readonly static bool JavaGenerateEqualsAndHashDefaultValue = false; + private bool javaGenerateEqualsAndHash_; /// /// This option does nothing. @@ -3416,14 +3975,29 @@ namespace Google.Protobuf.Reflection { [global::System.ObsoleteAttribute] [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool JavaGenerateEqualsAndHash { - get { return javaGenerateEqualsAndHash_; } + get { if ((_hasBits0 & 32) != 0) { return javaGenerateEqualsAndHash_; } else { return JavaGenerateEqualsAndHashDefaultValue; } } set { + _hasBits0 |= 32; javaGenerateEqualsAndHash_ = value; } } + /// Gets whether the "java_generate_equals_and_hash" field is set + [global::System.ObsoleteAttribute] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasJavaGenerateEqualsAndHash { + get { return (_hasBits0 & 32) != 0; } + } + /// Clears the value of the "java_generate_equals_and_hash" field + [global::System.ObsoleteAttribute] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearJavaGenerateEqualsAndHash() { + _hasBits0 &= ~32; + } /// Field number for the "java_string_check_utf8" field. public const int JavaStringCheckUtf8FieldNumber = 27; + private readonly static bool JavaStringCheckUtf8DefaultValue = false; + private bool javaStringCheckUtf8_; /// /// If set true, then the Java2 code generator will generate code that @@ -3435,26 +4009,52 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool JavaStringCheckUtf8 { - get { return javaStringCheckUtf8_; } + get { if ((_hasBits0 & 128) != 0) { return javaStringCheckUtf8_; } else { return JavaStringCheckUtf8DefaultValue; } } set { + _hasBits0 |= 128; javaStringCheckUtf8_ = value; } } + /// Gets whether the "java_string_check_utf8" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasJavaStringCheckUtf8 { + get { return (_hasBits0 & 128) != 0; } + } + /// Clears the value of the "java_string_check_utf8" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearJavaStringCheckUtf8() { + _hasBits0 &= ~128; + } /// Field number for the "optimize_for" field. public const int OptimizeForFieldNumber = 9; - private global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode optimizeFor_ = 0; + private readonly static global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode OptimizeForDefaultValue = global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode.Speed; + + private global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode optimizeFor_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode OptimizeFor { - get { return optimizeFor_; } + get { if ((_hasBits0 & 1) != 0) { return optimizeFor_; } else { return OptimizeForDefaultValue; } } set { + _hasBits0 |= 1; optimizeFor_ = value; } } + /// Gets whether the "optimize_for" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasOptimizeFor { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "optimize_for" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearOptimizeFor() { + _hasBits0 &= ~1; + } /// Field number for the "go_package" field. public const int GoPackageFieldNumber = 11; - private string goPackage_ = ""; + private readonly static string GoPackageDefaultValue = ""; + + private string goPackage_; /// /// Sets the Go package where structs generated from this .proto will be /// placed. If omitted, the Go package will be derived from the following: @@ -3464,14 +4064,26 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string GoPackage { - get { return goPackage_; } + get { return goPackage_ ?? GoPackageDefaultValue; } set { goPackage_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "go_package" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasGoPackage { + get { return goPackage_ != null; } + } + /// Clears the value of the "go_package" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearGoPackage() { + goPackage_ = null; + } /// Field number for the "cc_generic_services" field. public const int CcGenericServicesFieldNumber = 16; + private readonly static bool CcGenericServicesDefaultValue = false; + private bool ccGenericServices_; /// /// Should generic services be generated in each language? "Generic" services @@ -3487,47 +4099,99 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool CcGenericServices { - get { return ccGenericServices_; } + get { if ((_hasBits0 & 4) != 0) { return ccGenericServices_; } else { return CcGenericServicesDefaultValue; } } set { + _hasBits0 |= 4; ccGenericServices_ = value; } } - - /// Field number for the "java_generic_services" field. - public const int JavaGenericServicesFieldNumber = 17; + /// Gets whether the "cc_generic_services" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasCcGenericServices { + get { return (_hasBits0 & 4) != 0; } + } + /// Clears the value of the "cc_generic_services" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearCcGenericServices() { + _hasBits0 &= ~4; + } + + /// Field number for the "java_generic_services" field. + public const int JavaGenericServicesFieldNumber = 17; + private readonly static bool JavaGenericServicesDefaultValue = false; + private bool javaGenericServices_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool JavaGenericServices { - get { return javaGenericServices_; } + get { if ((_hasBits0 & 8) != 0) { return javaGenericServices_; } else { return JavaGenericServicesDefaultValue; } } set { + _hasBits0 |= 8; javaGenericServices_ = value; } } + /// Gets whether the "java_generic_services" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasJavaGenericServices { + get { return (_hasBits0 & 8) != 0; } + } + /// Clears the value of the "java_generic_services" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearJavaGenericServices() { + _hasBits0 &= ~8; + } /// Field number for the "py_generic_services" field. public const int PyGenericServicesFieldNumber = 18; + private readonly static bool PyGenericServicesDefaultValue = false; + private bool pyGenericServices_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool PyGenericServices { - get { return pyGenericServices_; } + get { if ((_hasBits0 & 16) != 0) { return pyGenericServices_; } else { return PyGenericServicesDefaultValue; } } set { + _hasBits0 |= 16; pyGenericServices_ = value; } } + /// Gets whether the "py_generic_services" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasPyGenericServices { + get { return (_hasBits0 & 16) != 0; } + } + /// Clears the value of the "py_generic_services" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearPyGenericServices() { + _hasBits0 &= ~16; + } /// Field number for the "php_generic_services" field. public const int PhpGenericServicesFieldNumber = 42; + private readonly static bool PhpGenericServicesDefaultValue = false; + private bool phpGenericServices_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool PhpGenericServices { - get { return phpGenericServices_; } + get { if ((_hasBits0 & 512) != 0) { return phpGenericServices_; } else { return PhpGenericServicesDefaultValue; } } set { + _hasBits0 |= 512; phpGenericServices_ = value; } } + /// Gets whether the "php_generic_services" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasPhpGenericServices { + get { return (_hasBits0 & 512) != 0; } + } + /// Clears the value of the "php_generic_services" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearPhpGenericServices() { + _hasBits0 &= ~512; + } /// Field number for the "deprecated" field. public const int DeprecatedFieldNumber = 23; + private readonly static bool DeprecatedDefaultValue = false; + private bool deprecated_; /// /// Is this file deprecated? @@ -3537,14 +4201,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Deprecated { - get { return deprecated_; } + get { if ((_hasBits0 & 64) != 0) { return deprecated_; } else { return DeprecatedDefaultValue; } } set { + _hasBits0 |= 64; deprecated_ = value; } } + /// Gets whether the "deprecated" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDeprecated { + get { return (_hasBits0 & 64) != 0; } + } + /// Clears the value of the "deprecated" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDeprecated() { + _hasBits0 &= ~64; + } /// Field number for the "cc_enable_arenas" field. public const int CcEnableArenasFieldNumber = 31; + private readonly static bool CcEnableArenasDefaultValue = false; + private bool ccEnableArenas_; /// /// Enables the use of arenas for the proto messages in this file. This applies @@ -3552,44 +4229,81 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool CcEnableArenas { - get { return ccEnableArenas_; } + get { if ((_hasBits0 & 256) != 0) { return ccEnableArenas_; } else { return CcEnableArenasDefaultValue; } } set { + _hasBits0 |= 256; ccEnableArenas_ = value; } } + /// Gets whether the "cc_enable_arenas" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasCcEnableArenas { + get { return (_hasBits0 & 256) != 0; } + } + /// Clears the value of the "cc_enable_arenas" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearCcEnableArenas() { + _hasBits0 &= ~256; + } /// Field number for the "objc_class_prefix" field. public const int ObjcClassPrefixFieldNumber = 36; - private string objcClassPrefix_ = ""; + private readonly static string ObjcClassPrefixDefaultValue = ""; + + private string objcClassPrefix_; /// /// Sets the objective c class prefix which is prepended to all objective c /// generated classes from this .proto. There is no default. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string ObjcClassPrefix { - get { return objcClassPrefix_; } + get { return objcClassPrefix_ ?? ObjcClassPrefixDefaultValue; } set { objcClassPrefix_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "objc_class_prefix" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasObjcClassPrefix { + get { return objcClassPrefix_ != null; } + } + /// Clears the value of the "objc_class_prefix" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearObjcClassPrefix() { + objcClassPrefix_ = null; + } /// Field number for the "csharp_namespace" field. public const int CsharpNamespaceFieldNumber = 37; - private string csharpNamespace_ = ""; + private readonly static string CsharpNamespaceDefaultValue = ""; + + private string csharpNamespace_; /// /// Namespace for generated classes; defaults to the package. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string CsharpNamespace { - get { return csharpNamespace_; } + get { return csharpNamespace_ ?? CsharpNamespaceDefaultValue; } set { csharpNamespace_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "csharp_namespace" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasCsharpNamespace { + get { return csharpNamespace_ != null; } + } + /// Clears the value of the "csharp_namespace" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearCsharpNamespace() { + csharpNamespace_ = null; + } /// Field number for the "swift_prefix" field. public const int SwiftPrefixFieldNumber = 39; - private string swiftPrefix_ = ""; + private readonly static string SwiftPrefixDefaultValue = ""; + + private string swiftPrefix_; /// /// By default Swift generators will take the proto package and CamelCase it /// replacing '.' with underscore and use that to prefix the types/symbols @@ -3598,30 +4312,54 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string SwiftPrefix { - get { return swiftPrefix_; } + get { return swiftPrefix_ ?? SwiftPrefixDefaultValue; } set { swiftPrefix_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "swift_prefix" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasSwiftPrefix { + get { return swiftPrefix_ != null; } + } + /// Clears the value of the "swift_prefix" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearSwiftPrefix() { + swiftPrefix_ = null; + } /// Field number for the "php_class_prefix" field. public const int PhpClassPrefixFieldNumber = 40; - private string phpClassPrefix_ = ""; + private readonly static string PhpClassPrefixDefaultValue = ""; + + private string phpClassPrefix_; /// /// Sets the php class prefix which is prepended to all php generated classes /// from this .proto. Default is empty. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string PhpClassPrefix { - get { return phpClassPrefix_; } + get { return phpClassPrefix_ ?? PhpClassPrefixDefaultValue; } set { phpClassPrefix_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "php_class_prefix" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasPhpClassPrefix { + get { return phpClassPrefix_ != null; } + } + /// Clears the value of the "php_class_prefix" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearPhpClassPrefix() { + phpClassPrefix_ = null; + } /// Field number for the "php_namespace" field. public const int PhpNamespaceFieldNumber = 41; - private string phpNamespace_ = ""; + private readonly static string PhpNamespaceDefaultValue = ""; + + private string phpNamespace_; /// /// Use this option to change the namespace of php generated classes. Default /// is empty. When this option is empty, the package name will be used for @@ -3629,31 +4367,55 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string PhpNamespace { - get { return phpNamespace_; } + get { return phpNamespace_ ?? PhpNamespaceDefaultValue; } set { phpNamespace_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "php_namespace" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasPhpNamespace { + get { return phpNamespace_ != null; } + } + /// Clears the value of the "php_namespace" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearPhpNamespace() { + phpNamespace_ = null; + } /// Field number for the "php_metadata_namespace" field. public const int PhpMetadataNamespaceFieldNumber = 44; - private string phpMetadataNamespace_ = ""; + private readonly static string PhpMetadataNamespaceDefaultValue = ""; + + private string phpMetadataNamespace_; /// /// Use this option to change the namespace of php generated metadata classes. - /// Default is empty. When this option is empty, the proto file name will be used - /// for determining the namespace. + /// Default is empty. When this option is empty, the proto file name will be + /// used for determining the namespace. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string PhpMetadataNamespace { - get { return phpMetadataNamespace_; } + get { return phpMetadataNamespace_ ?? PhpMetadataNamespaceDefaultValue; } set { phpMetadataNamespace_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "php_metadata_namespace" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasPhpMetadataNamespace { + get { return phpMetadataNamespace_ != null; } + } + /// Clears the value of the "php_metadata_namespace" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearPhpMetadataNamespace() { + phpMetadataNamespace_ = null; + } /// Field number for the "ruby_package" field. public const int RubyPackageFieldNumber = 45; - private string rubyPackage_ = ""; + private readonly static string RubyPackageDefaultValue = ""; + + private string rubyPackage_; /// /// Use this option to change the package of ruby generated classes. Default /// is empty. When this option is not set, the package name will be used for @@ -3661,11 +4423,21 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string RubyPackage { - get { return rubyPackage_; } + get { return rubyPackage_ ?? RubyPackageDefaultValue; } set { rubyPackage_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "ruby_package" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasRubyPackage { + get { return rubyPackage_ != null; } + } + /// Clears the value of the "ruby_package" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearRubyPackage() { + rubyPackage_ = null; + } /// Field number for the "uninterpreted_option" field. public const int UninterpretedOptionFieldNumber = 999; @@ -3715,33 +4487,39 @@ namespace Google.Protobuf.Reflection { if (PhpMetadataNamespace != other.PhpMetadataNamespace) return false; if (RubyPackage != other.RubyPackage) return false; if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (JavaPackage.Length != 0) hash ^= JavaPackage.GetHashCode(); - if (JavaOuterClassname.Length != 0) hash ^= JavaOuterClassname.GetHashCode(); - if (JavaMultipleFiles != false) hash ^= JavaMultipleFiles.GetHashCode(); - if (JavaGenerateEqualsAndHash != false) hash ^= JavaGenerateEqualsAndHash.GetHashCode(); - if (JavaStringCheckUtf8 != false) hash ^= JavaStringCheckUtf8.GetHashCode(); - if (OptimizeFor != 0) hash ^= OptimizeFor.GetHashCode(); - if (GoPackage.Length != 0) hash ^= GoPackage.GetHashCode(); - if (CcGenericServices != false) hash ^= CcGenericServices.GetHashCode(); - if (JavaGenericServices != false) hash ^= JavaGenericServices.GetHashCode(); - if (PyGenericServices != false) hash ^= PyGenericServices.GetHashCode(); - if (PhpGenericServices != false) hash ^= PhpGenericServices.GetHashCode(); - if (Deprecated != false) hash ^= Deprecated.GetHashCode(); - if (CcEnableArenas != false) hash ^= CcEnableArenas.GetHashCode(); - if (ObjcClassPrefix.Length != 0) hash ^= ObjcClassPrefix.GetHashCode(); - if (CsharpNamespace.Length != 0) hash ^= CsharpNamespace.GetHashCode(); - if (SwiftPrefix.Length != 0) hash ^= SwiftPrefix.GetHashCode(); - if (PhpClassPrefix.Length != 0) hash ^= PhpClassPrefix.GetHashCode(); - if (PhpNamespace.Length != 0) hash ^= PhpNamespace.GetHashCode(); - if (PhpMetadataNamespace.Length != 0) hash ^= PhpMetadataNamespace.GetHashCode(); - if (RubyPackage.Length != 0) hash ^= RubyPackage.GetHashCode(); + if (HasJavaPackage) hash ^= JavaPackage.GetHashCode(); + if (HasJavaOuterClassname) hash ^= JavaOuterClassname.GetHashCode(); + if (HasJavaMultipleFiles) hash ^= JavaMultipleFiles.GetHashCode(); + if (HasJavaGenerateEqualsAndHash) hash ^= JavaGenerateEqualsAndHash.GetHashCode(); + if (HasJavaStringCheckUtf8) hash ^= JavaStringCheckUtf8.GetHashCode(); + if (HasOptimizeFor) hash ^= OptimizeFor.GetHashCode(); + if (HasGoPackage) hash ^= GoPackage.GetHashCode(); + if (HasCcGenericServices) hash ^= CcGenericServices.GetHashCode(); + if (HasJavaGenericServices) hash ^= JavaGenericServices.GetHashCode(); + if (HasPyGenericServices) hash ^= PyGenericServices.GetHashCode(); + if (HasPhpGenericServices) hash ^= PhpGenericServices.GetHashCode(); + if (HasDeprecated) hash ^= Deprecated.GetHashCode(); + if (HasCcEnableArenas) hash ^= CcEnableArenas.GetHashCode(); + if (HasObjcClassPrefix) hash ^= ObjcClassPrefix.GetHashCode(); + if (HasCsharpNamespace) hash ^= CsharpNamespace.GetHashCode(); + if (HasSwiftPrefix) hash ^= SwiftPrefix.GetHashCode(); + if (HasPhpClassPrefix) hash ^= PhpClassPrefix.GetHashCode(); + if (HasPhpNamespace) hash ^= PhpNamespace.GetHashCode(); + if (HasPhpMetadataNamespace) hash ^= PhpMetadataNamespace.GetHashCode(); + if (HasRubyPackage) hash ^= RubyPackage.GetHashCode(); hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -3755,87 +4533,90 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (JavaPackage.Length != 0) { + if (HasJavaPackage) { output.WriteRawTag(10); output.WriteString(JavaPackage); } - if (JavaOuterClassname.Length != 0) { + if (HasJavaOuterClassname) { output.WriteRawTag(66); output.WriteString(JavaOuterClassname); } - if (OptimizeFor != 0) { + if (HasOptimizeFor) { output.WriteRawTag(72); output.WriteEnum((int) OptimizeFor); } - if (JavaMultipleFiles != false) { + if (HasJavaMultipleFiles) { output.WriteRawTag(80); output.WriteBool(JavaMultipleFiles); } - if (GoPackage.Length != 0) { + if (HasGoPackage) { output.WriteRawTag(90); output.WriteString(GoPackage); } - if (CcGenericServices != false) { + if (HasCcGenericServices) { output.WriteRawTag(128, 1); output.WriteBool(CcGenericServices); } - if (JavaGenericServices != false) { + if (HasJavaGenericServices) { output.WriteRawTag(136, 1); output.WriteBool(JavaGenericServices); } - if (PyGenericServices != false) { + if (HasPyGenericServices) { output.WriteRawTag(144, 1); output.WriteBool(PyGenericServices); } - if (JavaGenerateEqualsAndHash != false) { + if (HasJavaGenerateEqualsAndHash) { output.WriteRawTag(160, 1); output.WriteBool(JavaGenerateEqualsAndHash); } - if (Deprecated != false) { + if (HasDeprecated) { output.WriteRawTag(184, 1); output.WriteBool(Deprecated); } - if (JavaStringCheckUtf8 != false) { + if (HasJavaStringCheckUtf8) { output.WriteRawTag(216, 1); output.WriteBool(JavaStringCheckUtf8); } - if (CcEnableArenas != false) { + if (HasCcEnableArenas) { output.WriteRawTag(248, 1); output.WriteBool(CcEnableArenas); } - if (ObjcClassPrefix.Length != 0) { + if (HasObjcClassPrefix) { output.WriteRawTag(162, 2); output.WriteString(ObjcClassPrefix); } - if (CsharpNamespace.Length != 0) { + if (HasCsharpNamespace) { output.WriteRawTag(170, 2); output.WriteString(CsharpNamespace); } - if (SwiftPrefix.Length != 0) { + if (HasSwiftPrefix) { output.WriteRawTag(186, 2); output.WriteString(SwiftPrefix); } - if (PhpClassPrefix.Length != 0) { + if (HasPhpClassPrefix) { output.WriteRawTag(194, 2); output.WriteString(PhpClassPrefix); } - if (PhpNamespace.Length != 0) { + if (HasPhpNamespace) { output.WriteRawTag(202, 2); output.WriteString(PhpNamespace); } - if (PhpGenericServices != false) { + if (HasPhpGenericServices) { output.WriteRawTag(208, 2); output.WriteBool(PhpGenericServices); } - if (PhpMetadataNamespace.Length != 0) { + if (HasPhpMetadataNamespace) { output.WriteRawTag(226, 2); output.WriteString(PhpMetadataNamespace); } - if (RubyPackage.Length != 0) { + if (HasRubyPackage) { output.WriteRawTag(234, 2); output.WriteString(RubyPackage); } uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -3844,67 +4625,70 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (JavaPackage.Length != 0) { + if (HasJavaPackage) { size += 1 + pb::CodedOutputStream.ComputeStringSize(JavaPackage); } - if (JavaOuterClassname.Length != 0) { + if (HasJavaOuterClassname) { size += 1 + pb::CodedOutputStream.ComputeStringSize(JavaOuterClassname); } - if (JavaMultipleFiles != false) { + if (HasJavaMultipleFiles) { size += 1 + 1; } - if (JavaGenerateEqualsAndHash != false) { + if (HasJavaGenerateEqualsAndHash) { size += 2 + 1; } - if (JavaStringCheckUtf8 != false) { + if (HasJavaStringCheckUtf8) { size += 2 + 1; } - if (OptimizeFor != 0) { + if (HasOptimizeFor) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) OptimizeFor); } - if (GoPackage.Length != 0) { + if (HasGoPackage) { size += 1 + pb::CodedOutputStream.ComputeStringSize(GoPackage); } - if (CcGenericServices != false) { + if (HasCcGenericServices) { size += 2 + 1; } - if (JavaGenericServices != false) { + if (HasJavaGenericServices) { size += 2 + 1; } - if (PyGenericServices != false) { + if (HasPyGenericServices) { size += 2 + 1; } - if (PhpGenericServices != false) { + if (HasPhpGenericServices) { size += 2 + 1; } - if (Deprecated != false) { + if (HasDeprecated) { size += 2 + 1; } - if (CcEnableArenas != false) { + if (HasCcEnableArenas) { size += 2 + 1; } - if (ObjcClassPrefix.Length != 0) { + if (HasObjcClassPrefix) { size += 2 + pb::CodedOutputStream.ComputeStringSize(ObjcClassPrefix); } - if (CsharpNamespace.Length != 0) { + if (HasCsharpNamespace) { size += 2 + pb::CodedOutputStream.ComputeStringSize(CsharpNamespace); } - if (SwiftPrefix.Length != 0) { + if (HasSwiftPrefix) { size += 2 + pb::CodedOutputStream.ComputeStringSize(SwiftPrefix); } - if (PhpClassPrefix.Length != 0) { + if (HasPhpClassPrefix) { size += 2 + pb::CodedOutputStream.ComputeStringSize(PhpClassPrefix); } - if (PhpNamespace.Length != 0) { + if (HasPhpNamespace) { size += 2 + pb::CodedOutputStream.ComputeStringSize(PhpNamespace); } - if (PhpMetadataNamespace.Length != 0) { + if (HasPhpMetadataNamespace) { size += 2 + pb::CodedOutputStream.ComputeStringSize(PhpMetadataNamespace); } - if (RubyPackage.Length != 0) { + if (HasRubyPackage) { size += 2 + pb::CodedOutputStream.ComputeStringSize(RubyPackage); } size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -3916,67 +4700,68 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.JavaPackage.Length != 0) { + if (other.HasJavaPackage) { JavaPackage = other.JavaPackage; } - if (other.JavaOuterClassname.Length != 0) { + if (other.HasJavaOuterClassname) { JavaOuterClassname = other.JavaOuterClassname; } - if (other.JavaMultipleFiles != false) { + if (other.HasJavaMultipleFiles) { JavaMultipleFiles = other.JavaMultipleFiles; } - if (other.JavaGenerateEqualsAndHash != false) { + if (other.HasJavaGenerateEqualsAndHash) { JavaGenerateEqualsAndHash = other.JavaGenerateEqualsAndHash; } - if (other.JavaStringCheckUtf8 != false) { + if (other.HasJavaStringCheckUtf8) { JavaStringCheckUtf8 = other.JavaStringCheckUtf8; } - if (other.OptimizeFor != 0) { + if (other.HasOptimizeFor) { OptimizeFor = other.OptimizeFor; } - if (other.GoPackage.Length != 0) { + if (other.HasGoPackage) { GoPackage = other.GoPackage; } - if (other.CcGenericServices != false) { + if (other.HasCcGenericServices) { CcGenericServices = other.CcGenericServices; } - if (other.JavaGenericServices != false) { + if (other.HasJavaGenericServices) { JavaGenericServices = other.JavaGenericServices; } - if (other.PyGenericServices != false) { + if (other.HasPyGenericServices) { PyGenericServices = other.PyGenericServices; } - if (other.PhpGenericServices != false) { + if (other.HasPhpGenericServices) { PhpGenericServices = other.PhpGenericServices; } - if (other.Deprecated != false) { + if (other.HasDeprecated) { Deprecated = other.Deprecated; } - if (other.CcEnableArenas != false) { + if (other.HasCcEnableArenas) { CcEnableArenas = other.CcEnableArenas; } - if (other.ObjcClassPrefix.Length != 0) { + if (other.HasObjcClassPrefix) { ObjcClassPrefix = other.ObjcClassPrefix; } - if (other.CsharpNamespace.Length != 0) { + if (other.HasCsharpNamespace) { CsharpNamespace = other.CsharpNamespace; } - if (other.SwiftPrefix.Length != 0) { + if (other.HasSwiftPrefix) { SwiftPrefix = other.SwiftPrefix; } - if (other.PhpClassPrefix.Length != 0) { + if (other.HasPhpClassPrefix) { PhpClassPrefix = other.PhpClassPrefix; } - if (other.PhpNamespace.Length != 0) { + if (other.HasPhpNamespace) { PhpNamespace = other.PhpNamespace; } - if (other.PhpMetadataNamespace.Length != 0) { + if (other.HasPhpMetadataNamespace) { PhpMetadataNamespace = other.PhpMetadataNamespace; } - if (other.RubyPackage.Length != 0) { + if (other.HasRubyPackage) { RubyPackage = other.RubyPackage; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -3986,7 +4771,9 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - CustomOptions = CustomOptions.ReadOrSkipUnknownField(input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 10: { JavaPackage = input.ReadString(); @@ -3997,7 +4784,7 @@ namespace Google.Protobuf.Reflection { break; } case 72: { - optimizeFor_ = (global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode) input.ReadEnum(); + OptimizeFor = (global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode) input.ReadEnum(); break; } case 80: { @@ -4076,6 +4863,28 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + #region Nested types /// Container for nested types declared in the FileOptions message type. [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -4103,9 +4912,11 @@ namespace Google.Protobuf.Reflection { } - internal sealed partial class MessageOptions : pb::IMessage { + internal sealed partial class MessageOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new MessageOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -4119,8 +4930,6 @@ namespace Google.Protobuf.Reflection { get { return Descriptor; } } - internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public MessageOptions() { OnConstruction(); @@ -4130,12 +4939,14 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public MessageOptions(MessageOptions other) : this() { + _hasBits0 = other._hasBits0; messageSetWireFormat_ = other.messageSetWireFormat_; noStandardDescriptorAccessor_ = other.noStandardDescriptorAccessor_; deprecated_ = other.deprecated_; mapEntry_ = other.mapEntry_; uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -4145,6 +4956,8 @@ namespace Google.Protobuf.Reflection { /// Field number for the "message_set_wire_format" field. public const int MessageSetWireFormatFieldNumber = 1; + private readonly static bool MessageSetWireFormatDefaultValue = false; + private bool messageSetWireFormat_; /// /// Set true to use the old proto1 MessageSet wire format for extensions. @@ -4168,14 +4981,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool MessageSetWireFormat { - get { return messageSetWireFormat_; } + get { if ((_hasBits0 & 1) != 0) { return messageSetWireFormat_; } else { return MessageSetWireFormatDefaultValue; } } set { + _hasBits0 |= 1; messageSetWireFormat_ = value; } } + /// Gets whether the "message_set_wire_format" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasMessageSetWireFormat { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "message_set_wire_format" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearMessageSetWireFormat() { + _hasBits0 &= ~1; + } /// Field number for the "no_standard_descriptor_accessor" field. public const int NoStandardDescriptorAccessorFieldNumber = 2; + private readonly static bool NoStandardDescriptorAccessorDefaultValue = false; + private bool noStandardDescriptorAccessor_; /// /// Disables the generation of the standard "descriptor()" accessor, which can @@ -4184,14 +5010,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool NoStandardDescriptorAccessor { - get { return noStandardDescriptorAccessor_; } + get { if ((_hasBits0 & 2) != 0) { return noStandardDescriptorAccessor_; } else { return NoStandardDescriptorAccessorDefaultValue; } } set { + _hasBits0 |= 2; noStandardDescriptorAccessor_ = value; } } + /// Gets whether the "no_standard_descriptor_accessor" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasNoStandardDescriptorAccessor { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "no_standard_descriptor_accessor" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearNoStandardDescriptorAccessor() { + _hasBits0 &= ~2; + } /// Field number for the "deprecated" field. public const int DeprecatedFieldNumber = 3; + private readonly static bool DeprecatedDefaultValue = false; + private bool deprecated_; /// /// Is this message deprecated? @@ -4201,14 +5040,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Deprecated { - get { return deprecated_; } + get { if ((_hasBits0 & 4) != 0) { return deprecated_; } else { return DeprecatedDefaultValue; } } set { + _hasBits0 |= 4; deprecated_ = value; } } + /// Gets whether the "deprecated" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDeprecated { + get { return (_hasBits0 & 4) != 0; } + } + /// Clears the value of the "deprecated" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDeprecated() { + _hasBits0 &= ~4; + } /// Field number for the "map_entry" field. public const int MapEntryFieldNumber = 7; + private readonly static bool MapEntryDefaultValue = false; + private bool mapEntry_; /// /// Whether the message is an automatically generated map entry type for the @@ -4226,7 +5078,7 @@ namespace Google.Protobuf.Reflection { /// /// Implementations may choose not to generate the map_entry=true message, but /// use a native map in the target language to hold the keys and values. - /// The reflection APIs in such implementions still need to work as + /// The reflection APIs in such implementations still need to work as /// if the field is a repeated message field. /// /// NOTE: Do not set the option in .proto files. Always use the maps syntax @@ -4235,11 +5087,22 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool MapEntry { - get { return mapEntry_; } + get { if ((_hasBits0 & 8) != 0) { return mapEntry_; } else { return MapEntryDefaultValue; } } set { + _hasBits0 |= 8; mapEntry_ = value; } } + /// Gets whether the "map_entry" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasMapEntry { + get { return (_hasBits0 & 8) != 0; } + } + /// Clears the value of the "map_entry" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearMapEntry() { + _hasBits0 &= ~8; + } /// Field number for the "uninterpreted_option" field. public const int UninterpretedOptionFieldNumber = 999; @@ -4272,17 +5135,23 @@ namespace Google.Protobuf.Reflection { if (Deprecated != other.Deprecated) return false; if (MapEntry != other.MapEntry) return false; if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (MessageSetWireFormat != false) hash ^= MessageSetWireFormat.GetHashCode(); - if (NoStandardDescriptorAccessor != false) hash ^= NoStandardDescriptorAccessor.GetHashCode(); - if (Deprecated != false) hash ^= Deprecated.GetHashCode(); - if (MapEntry != false) hash ^= MapEntry.GetHashCode(); + if (HasMessageSetWireFormat) hash ^= MessageSetWireFormat.GetHashCode(); + if (HasNoStandardDescriptorAccessor) hash ^= NoStandardDescriptorAccessor.GetHashCode(); + if (HasDeprecated) hash ^= Deprecated.GetHashCode(); + if (HasMapEntry) hash ^= MapEntry.GetHashCode(); hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -4296,23 +5165,26 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (MessageSetWireFormat != false) { + if (HasMessageSetWireFormat) { output.WriteRawTag(8); output.WriteBool(MessageSetWireFormat); } - if (NoStandardDescriptorAccessor != false) { + if (HasNoStandardDescriptorAccessor) { output.WriteRawTag(16); output.WriteBool(NoStandardDescriptorAccessor); } - if (Deprecated != false) { + if (HasDeprecated) { output.WriteRawTag(24); output.WriteBool(Deprecated); } - if (MapEntry != false) { + if (HasMapEntry) { output.WriteRawTag(56); output.WriteBool(MapEntry); } uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -4321,19 +5193,22 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (MessageSetWireFormat != false) { + if (HasMessageSetWireFormat) { size += 1 + 1; } - if (NoStandardDescriptorAccessor != false) { + if (HasNoStandardDescriptorAccessor) { size += 1 + 1; } - if (Deprecated != false) { + if (HasDeprecated) { size += 1 + 1; } - if (MapEntry != false) { + if (HasMapEntry) { size += 1 + 1; } size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -4345,19 +5220,20 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.MessageSetWireFormat != false) { + if (other.HasMessageSetWireFormat) { MessageSetWireFormat = other.MessageSetWireFormat; } - if (other.NoStandardDescriptorAccessor != false) { + if (other.HasNoStandardDescriptorAccessor) { NoStandardDescriptorAccessor = other.NoStandardDescriptorAccessor; } - if (other.Deprecated != false) { + if (other.HasDeprecated) { Deprecated = other.Deprecated; } - if (other.MapEntry != false) { + if (other.HasMapEntry) { MapEntry = other.MapEntry; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -4367,7 +5243,9 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - CustomOptions = CustomOptions.ReadOrSkipUnknownField(input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 8: { MessageSetWireFormat = input.ReadBool(); @@ -4393,11 +5271,35 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + } - internal sealed partial class FieldOptions : pb::IMessage { + internal sealed partial class FieldOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FieldOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -4411,8 +5313,6 @@ namespace Google.Protobuf.Reflection { get { return Descriptor; } } - internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public FieldOptions() { OnConstruction(); @@ -4422,6 +5322,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public FieldOptions(FieldOptions other) : this() { + _hasBits0 = other._hasBits0; ctype_ = other.ctype_; packed_ = other.packed_; jstype_ = other.jstype_; @@ -4430,6 +5331,7 @@ namespace Google.Protobuf.Reflection { weak_ = other.weak_; uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -4439,7 +5341,9 @@ namespace Google.Protobuf.Reflection { /// Field number for the "ctype" field. public const int CtypeFieldNumber = 1; - private global::Google.Protobuf.Reflection.FieldOptions.Types.CType ctype_ = 0; + private readonly static global::Google.Protobuf.Reflection.FieldOptions.Types.CType CtypeDefaultValue = global::Google.Protobuf.Reflection.FieldOptions.Types.CType.String; + + private global::Google.Protobuf.Reflection.FieldOptions.Types.CType ctype_; /// /// The ctype option instructs the C++ code generator to use a different /// representation of the field than it normally would. See the specific @@ -4448,14 +5352,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.Reflection.FieldOptions.Types.CType Ctype { - get { return ctype_; } + get { if ((_hasBits0 & 1) != 0) { return ctype_; } else { return CtypeDefaultValue; } } set { + _hasBits0 |= 1; ctype_ = value; } } + /// Gets whether the "ctype" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasCtype { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "ctype" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearCtype() { + _hasBits0 &= ~1; + } /// Field number for the "packed" field. public const int PackedFieldNumber = 2; + private readonly static bool PackedDefaultValue = false; + private bool packed_; /// /// The packed option can be enabled for repeated primitive fields to enable @@ -4466,15 +5383,28 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Packed { - get { return packed_; } + get { if ((_hasBits0 & 2) != 0) { return packed_; } else { return PackedDefaultValue; } } set { + _hasBits0 |= 2; packed_ = value; } } + /// Gets whether the "packed" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasPacked { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "packed" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearPacked() { + _hasBits0 &= ~2; + } /// Field number for the "jstype" field. public const int JstypeFieldNumber = 6; - private global::Google.Protobuf.Reflection.FieldOptions.Types.JSType jstype_ = 0; + private readonly static global::Google.Protobuf.Reflection.FieldOptions.Types.JSType JstypeDefaultValue = global::Google.Protobuf.Reflection.FieldOptions.Types.JSType.JsNormal; + + private global::Google.Protobuf.Reflection.FieldOptions.Types.JSType jstype_; /// /// The jstype option determines the JavaScript type used for values of the /// field. The option is permitted only for 64 bit integral and fixed types @@ -4490,14 +5420,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.Reflection.FieldOptions.Types.JSType Jstype { - get { return jstype_; } + get { if ((_hasBits0 & 16) != 0) { return jstype_; } else { return JstypeDefaultValue; } } set { + _hasBits0 |= 16; jstype_ = value; } } + /// Gets whether the "jstype" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasJstype { + get { return (_hasBits0 & 16) != 0; } + } + /// Clears the value of the "jstype" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearJstype() { + _hasBits0 &= ~16; + } /// Field number for the "lazy" field. public const int LazyFieldNumber = 5; + private readonly static bool LazyDefaultValue = false; + private bool lazy_; /// /// Should this field be parsed lazily? Lazy applies only to message-type @@ -4530,14 +5473,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Lazy { - get { return lazy_; } + get { if ((_hasBits0 & 8) != 0) { return lazy_; } else { return LazyDefaultValue; } } set { + _hasBits0 |= 8; lazy_ = value; } } + /// Gets whether the "lazy" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasLazy { + get { return (_hasBits0 & 8) != 0; } + } + /// Clears the value of the "lazy" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearLazy() { + _hasBits0 &= ~8; + } /// Field number for the "deprecated" field. public const int DeprecatedFieldNumber = 3; + private readonly static bool DeprecatedDefaultValue = false; + private bool deprecated_; /// /// Is this field deprecated? @@ -4547,25 +5503,49 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Deprecated { - get { return deprecated_; } + get { if ((_hasBits0 & 4) != 0) { return deprecated_; } else { return DeprecatedDefaultValue; } } set { + _hasBits0 |= 4; deprecated_ = value; } } + /// Gets whether the "deprecated" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDeprecated { + get { return (_hasBits0 & 4) != 0; } + } + /// Clears the value of the "deprecated" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDeprecated() { + _hasBits0 &= ~4; + } /// Field number for the "weak" field. public const int WeakFieldNumber = 10; + private readonly static bool WeakDefaultValue = false; + private bool weak_; /// /// For Google-internal migration only. Do not use. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Weak { - get { return weak_; } + get { if ((_hasBits0 & 32) != 0) { return weak_; } else { return WeakDefaultValue; } } set { + _hasBits0 |= 32; weak_ = value; } } + /// Gets whether the "weak" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasWeak { + get { return (_hasBits0 & 32) != 0; } + } + /// Clears the value of the "weak" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearWeak() { + _hasBits0 &= ~32; + } /// Field number for the "uninterpreted_option" field. public const int UninterpretedOptionFieldNumber = 999; @@ -4600,19 +5580,25 @@ namespace Google.Protobuf.Reflection { if (Deprecated != other.Deprecated) return false; if (Weak != other.Weak) return false; if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Ctype != 0) hash ^= Ctype.GetHashCode(); - if (Packed != false) hash ^= Packed.GetHashCode(); - if (Jstype != 0) hash ^= Jstype.GetHashCode(); - if (Lazy != false) hash ^= Lazy.GetHashCode(); - if (Deprecated != false) hash ^= Deprecated.GetHashCode(); - if (Weak != false) hash ^= Weak.GetHashCode(); + if (HasCtype) hash ^= Ctype.GetHashCode(); + if (HasPacked) hash ^= Packed.GetHashCode(); + if (HasJstype) hash ^= Jstype.GetHashCode(); + if (HasLazy) hash ^= Lazy.GetHashCode(); + if (HasDeprecated) hash ^= Deprecated.GetHashCode(); + if (HasWeak) hash ^= Weak.GetHashCode(); hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -4626,31 +5612,34 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Ctype != 0) { + if (HasCtype) { output.WriteRawTag(8); output.WriteEnum((int) Ctype); } - if (Packed != false) { + if (HasPacked) { output.WriteRawTag(16); output.WriteBool(Packed); } - if (Deprecated != false) { + if (HasDeprecated) { output.WriteRawTag(24); output.WriteBool(Deprecated); } - if (Lazy != false) { + if (HasLazy) { output.WriteRawTag(40); output.WriteBool(Lazy); } - if (Jstype != 0) { + if (HasJstype) { output.WriteRawTag(48); output.WriteEnum((int) Jstype); } - if (Weak != false) { + if (HasWeak) { output.WriteRawTag(80); output.WriteBool(Weak); } uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -4659,25 +5648,28 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Ctype != 0) { + if (HasCtype) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Ctype); } - if (Packed != false) { + if (HasPacked) { size += 1 + 1; } - if (Jstype != 0) { + if (HasJstype) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Jstype); } - if (Lazy != false) { + if (HasLazy) { size += 1 + 1; } - if (Deprecated != false) { + if (HasDeprecated) { size += 1 + 1; } - if (Weak != false) { + if (HasWeak) { size += 1 + 1; } size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -4689,25 +5681,26 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Ctype != 0) { + if (other.HasCtype) { Ctype = other.Ctype; } - if (other.Packed != false) { + if (other.HasPacked) { Packed = other.Packed; } - if (other.Jstype != 0) { + if (other.HasJstype) { Jstype = other.Jstype; } - if (other.Lazy != false) { + if (other.HasLazy) { Lazy = other.Lazy; } - if (other.Deprecated != false) { + if (other.HasDeprecated) { Deprecated = other.Deprecated; } - if (other.Weak != false) { + if (other.HasWeak) { Weak = other.Weak; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -4717,10 +5710,12 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - CustomOptions = CustomOptions.ReadOrSkipUnknownField(input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 8: { - ctype_ = (global::Google.Protobuf.Reflection.FieldOptions.Types.CType) input.ReadEnum(); + Ctype = (global::Google.Protobuf.Reflection.FieldOptions.Types.CType) input.ReadEnum(); break; } case 16: { @@ -4736,7 +5731,7 @@ namespace Google.Protobuf.Reflection { break; } case 48: { - jstype_ = (global::Google.Protobuf.Reflection.FieldOptions.Types.JSType) input.ReadEnum(); + Jstype = (global::Google.Protobuf.Reflection.FieldOptions.Types.JSType) input.ReadEnum(); break; } case 80: { @@ -4751,6 +5746,28 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + #region Nested types /// Container for nested types declared in the FieldOptions message type. [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -4784,9 +5801,10 @@ namespace Google.Protobuf.Reflection { } - internal sealed partial class OneofOptions : pb::IMessage { + internal sealed partial class OneofOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new OneofOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -4800,8 +5818,6 @@ namespace Google.Protobuf.Reflection { get { return Descriptor; } } - internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public OneofOptions() { OnConstruction(); @@ -4813,6 +5829,7 @@ namespace Google.Protobuf.Reflection { public OneofOptions(OneofOptions other) : this() { uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -4847,6 +5864,9 @@ namespace Google.Protobuf.Reflection { return true; } if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } @@ -4854,6 +5874,9 @@ namespace Google.Protobuf.Reflection { public override int GetHashCode() { int hash = 1; hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -4868,6 +5891,9 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -4877,6 +5903,9 @@ namespace Google.Protobuf.Reflection { public int CalculateSize() { int size = 0; size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -4889,6 +5918,7 @@ namespace Google.Protobuf.Reflection { return; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -4898,7 +5928,9 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - CustomOptions = CustomOptions.ReadOrSkipUnknownField(input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 7994: { uninterpretedOption_.AddEntriesFrom(input, _repeated_uninterpretedOption_codec); @@ -4908,11 +5940,35 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + } - internal sealed partial class EnumOptions : pb::IMessage { + internal sealed partial class EnumOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new EnumOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -4926,8 +5982,6 @@ namespace Google.Protobuf.Reflection { get { return Descriptor; } } - internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public EnumOptions() { OnConstruction(); @@ -4937,10 +5991,12 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public EnumOptions(EnumOptions other) : this() { + _hasBits0 = other._hasBits0; allowAlias_ = other.allowAlias_; deprecated_ = other.deprecated_; uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -4950,6 +6006,8 @@ namespace Google.Protobuf.Reflection { /// Field number for the "allow_alias" field. public const int AllowAliasFieldNumber = 2; + private readonly static bool AllowAliasDefaultValue = false; + private bool allowAlias_; /// /// Set this option to true to allow mapping different tag names to the same @@ -4957,14 +6015,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool AllowAlias { - get { return allowAlias_; } + get { if ((_hasBits0 & 1) != 0) { return allowAlias_; } else { return AllowAliasDefaultValue; } } set { + _hasBits0 |= 1; allowAlias_ = value; } } + /// Gets whether the "allow_alias" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasAllowAlias { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "allow_alias" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearAllowAlias() { + _hasBits0 &= ~1; + } /// Field number for the "deprecated" field. public const int DeprecatedFieldNumber = 3; + private readonly static bool DeprecatedDefaultValue = false; + private bool deprecated_; /// /// Is this enum deprecated? @@ -4974,11 +6045,22 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Deprecated { - get { return deprecated_; } + get { if ((_hasBits0 & 2) != 0) { return deprecated_; } else { return DeprecatedDefaultValue; } } set { + _hasBits0 |= 2; deprecated_ = value; } } + /// Gets whether the "deprecated" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDeprecated { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "deprecated" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDeprecated() { + _hasBits0 &= ~2; + } /// Field number for the "uninterpreted_option" field. public const int UninterpretedOptionFieldNumber = 999; @@ -5009,15 +6091,21 @@ namespace Google.Protobuf.Reflection { if (AllowAlias != other.AllowAlias) return false; if (Deprecated != other.Deprecated) return false; if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (AllowAlias != false) hash ^= AllowAlias.GetHashCode(); - if (Deprecated != false) hash ^= Deprecated.GetHashCode(); + if (HasAllowAlias) hash ^= AllowAlias.GetHashCode(); + if (HasDeprecated) hash ^= Deprecated.GetHashCode(); hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -5031,15 +6119,18 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (AllowAlias != false) { + if (HasAllowAlias) { output.WriteRawTag(16); output.WriteBool(AllowAlias); } - if (Deprecated != false) { + if (HasDeprecated) { output.WriteRawTag(24); output.WriteBool(Deprecated); } uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -5048,13 +6139,16 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (AllowAlias != false) { + if (HasAllowAlias) { size += 1 + 1; } - if (Deprecated != false) { + if (HasDeprecated) { size += 1 + 1; } size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -5066,13 +6160,14 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.AllowAlias != false) { + if (other.HasAllowAlias) { AllowAlias = other.AllowAlias; } - if (other.Deprecated != false) { + if (other.HasDeprecated) { Deprecated = other.Deprecated; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -5082,7 +6177,9 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - CustomOptions = CustomOptions.ReadOrSkipUnknownField(input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 16: { AllowAlias = input.ReadBool(); @@ -5100,11 +6197,35 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + } - internal sealed partial class EnumValueOptions : pb::IMessage { + internal sealed partial class EnumValueOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new EnumValueOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -5118,8 +6239,6 @@ namespace Google.Protobuf.Reflection { get { return Descriptor; } } - internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public EnumValueOptions() { OnConstruction(); @@ -5129,9 +6248,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public EnumValueOptions(EnumValueOptions other) : this() { + _hasBits0 = other._hasBits0; deprecated_ = other.deprecated_; uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -5141,6 +6262,8 @@ namespace Google.Protobuf.Reflection { /// Field number for the "deprecated" field. public const int DeprecatedFieldNumber = 1; + private readonly static bool DeprecatedDefaultValue = false; + private bool deprecated_; /// /// Is this enum value deprecated? @@ -5150,11 +6273,22 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Deprecated { - get { return deprecated_; } + get { if ((_hasBits0 & 1) != 0) { return deprecated_; } else { return DeprecatedDefaultValue; } } set { + _hasBits0 |= 1; deprecated_ = value; } } + /// Gets whether the "deprecated" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDeprecated { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "deprecated" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDeprecated() { + _hasBits0 &= ~1; + } /// Field number for the "uninterpreted_option" field. public const int UninterpretedOptionFieldNumber = 999; @@ -5184,14 +6318,20 @@ namespace Google.Protobuf.Reflection { } if (Deprecated != other.Deprecated) return false; if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Deprecated != false) hash ^= Deprecated.GetHashCode(); + if (HasDeprecated) hash ^= Deprecated.GetHashCode(); hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -5205,11 +6345,14 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Deprecated != false) { + if (HasDeprecated) { output.WriteRawTag(8); output.WriteBool(Deprecated); } uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -5218,10 +6361,13 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Deprecated != false) { + if (HasDeprecated) { size += 1 + 1; } size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -5233,10 +6379,11 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Deprecated != false) { + if (other.HasDeprecated) { Deprecated = other.Deprecated; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -5246,7 +6393,9 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - CustomOptions = CustomOptions.ReadOrSkipUnknownField(input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 8: { Deprecated = input.ReadBool(); @@ -5260,11 +6409,35 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + } - internal sealed partial class ServiceOptions : pb::IMessage { + internal sealed partial class ServiceOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new ServiceOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -5278,8 +6451,6 @@ namespace Google.Protobuf.Reflection { get { return Descriptor; } } - internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public ServiceOptions() { OnConstruction(); @@ -5289,9 +6460,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public ServiceOptions(ServiceOptions other) : this() { + _hasBits0 = other._hasBits0; deprecated_ = other.deprecated_; uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -5301,6 +6474,8 @@ namespace Google.Protobuf.Reflection { /// Field number for the "deprecated" field. public const int DeprecatedFieldNumber = 33; + private readonly static bool DeprecatedDefaultValue = false; + private bool deprecated_; /// /// Is this service deprecated? @@ -5310,11 +6485,22 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Deprecated { - get { return deprecated_; } + get { if ((_hasBits0 & 1) != 0) { return deprecated_; } else { return DeprecatedDefaultValue; } } set { + _hasBits0 |= 1; deprecated_ = value; } } + /// Gets whether the "deprecated" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDeprecated { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "deprecated" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDeprecated() { + _hasBits0 &= ~1; + } /// Field number for the "uninterpreted_option" field. public const int UninterpretedOptionFieldNumber = 999; @@ -5344,14 +6530,20 @@ namespace Google.Protobuf.Reflection { } if (Deprecated != other.Deprecated) return false; if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Deprecated != false) hash ^= Deprecated.GetHashCode(); + if (HasDeprecated) hash ^= Deprecated.GetHashCode(); hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -5365,11 +6557,14 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Deprecated != false) { + if (HasDeprecated) { output.WriteRawTag(136, 2); output.WriteBool(Deprecated); } uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -5378,10 +6573,13 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Deprecated != false) { + if (HasDeprecated) { size += 2 + 1; } size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -5393,10 +6591,11 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Deprecated != false) { + if (other.HasDeprecated) { Deprecated = other.Deprecated; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -5406,7 +6605,9 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - CustomOptions = CustomOptions.ReadOrSkipUnknownField(input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 264: { Deprecated = input.ReadBool(); @@ -5420,11 +6621,35 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + } - internal sealed partial class MethodOptions : pb::IMessage { + internal sealed partial class MethodOptions : pb::IExtendableMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new MethodOptions()); private pb::UnknownFieldSet _unknownFields; + internal pb::ExtensionSet _extensions; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -5438,8 +6663,6 @@ namespace Google.Protobuf.Reflection { get { return Descriptor; } } - internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public MethodOptions() { OnConstruction(); @@ -5449,10 +6672,12 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public MethodOptions(MethodOptions other) : this() { + _hasBits0 = other._hasBits0; deprecated_ = other.deprecated_; idempotencyLevel_ = other.idempotencyLevel_; uninterpretedOption_ = other.uninterpretedOption_.Clone(); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); + _extensions = pb::ExtensionSet.Clone(other._extensions); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -5462,6 +6687,8 @@ namespace Google.Protobuf.Reflection { /// Field number for the "deprecated" field. public const int DeprecatedFieldNumber = 33; + private readonly static bool DeprecatedDefaultValue = false; + private bool deprecated_; /// /// Is this method deprecated? @@ -5471,22 +6698,46 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool Deprecated { - get { return deprecated_; } + get { if ((_hasBits0 & 1) != 0) { return deprecated_; } else { return DeprecatedDefaultValue; } } set { + _hasBits0 |= 1; deprecated_ = value; } } + /// Gets whether the "deprecated" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDeprecated { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "deprecated" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDeprecated() { + _hasBits0 &= ~1; + } /// Field number for the "idempotency_level" field. public const int IdempotencyLevelFieldNumber = 34; - private global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel idempotencyLevel_ = 0; + private readonly static global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel IdempotencyLevelDefaultValue = global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel.IdempotencyUnknown; + + private global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel idempotencyLevel_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel IdempotencyLevel { - get { return idempotencyLevel_; } + get { if ((_hasBits0 & 2) != 0) { return idempotencyLevel_; } else { return IdempotencyLevelDefaultValue; } } set { + _hasBits0 |= 2; idempotencyLevel_ = value; } } + /// Gets whether the "idempotency_level" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasIdempotencyLevel { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "idempotency_level" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearIdempotencyLevel() { + _hasBits0 &= ~2; + } /// Field number for the "uninterpreted_option" field. public const int UninterpretedOptionFieldNumber = 999; @@ -5517,15 +6768,21 @@ namespace Google.Protobuf.Reflection { if (Deprecated != other.Deprecated) return false; if (IdempotencyLevel != other.IdempotencyLevel) return false; if(!uninterpretedOption_.Equals(other.uninterpretedOption_)) return false; + if (!Equals(_extensions, other._extensions)) { + return false; + } return Equals(_unknownFields, other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Deprecated != false) hash ^= Deprecated.GetHashCode(); - if (IdempotencyLevel != 0) hash ^= IdempotencyLevel.GetHashCode(); + if (HasDeprecated) hash ^= Deprecated.GetHashCode(); + if (HasIdempotencyLevel) hash ^= IdempotencyLevel.GetHashCode(); hash ^= uninterpretedOption_.GetHashCode(); + if (_extensions != null) { + hash ^= _extensions.GetHashCode(); + } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -5539,15 +6796,18 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Deprecated != false) { + if (HasDeprecated) { output.WriteRawTag(136, 2); output.WriteBool(Deprecated); } - if (IdempotencyLevel != 0) { + if (HasIdempotencyLevel) { output.WriteRawTag(144, 2); output.WriteEnum((int) IdempotencyLevel); } uninterpretedOption_.WriteTo(output, _repeated_uninterpretedOption_codec); + if (_extensions != null) { + _extensions.WriteTo(output); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -5556,13 +6816,16 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Deprecated != false) { + if (HasDeprecated) { size += 2 + 1; } - if (IdempotencyLevel != 0) { + if (HasIdempotencyLevel) { size += 2 + pb::CodedOutputStream.ComputeEnumSize((int) IdempotencyLevel); } size += uninterpretedOption_.CalculateSize(_repeated_uninterpretedOption_codec); + if (_extensions != null) { + size += _extensions.CalculateSize(); + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -5574,13 +6837,14 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.Deprecated != false) { + if (other.HasDeprecated) { Deprecated = other.Deprecated; } - if (other.IdempotencyLevel != 0) { + if (other.HasIdempotencyLevel) { IdempotencyLevel = other.IdempotencyLevel; } uninterpretedOption_.Add(other.uninterpretedOption_); + pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -5590,14 +6854,16 @@ namespace Google.Protobuf.Reflection { while ((tag = input.ReadTag()) != 0) { switch(tag) { default: - CustomOptions = CustomOptions.ReadOrSkipUnknownField(input); + if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) { + _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); + } break; case 264: { Deprecated = input.ReadBool(); break; } case 272: { - idempotencyLevel_ = (global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel) input.ReadEnum(); + IdempotencyLevel = (global::Google.Protobuf.Reflection.MethodOptions.Types.IdempotencyLevel) input.ReadEnum(); break; } case 7994: { @@ -5608,6 +6874,28 @@ namespace Google.Protobuf.Reflection { } } + public TValue GetExtension(pb::Extension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.Get(ref _extensions, extension); + } + public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension extension) { + return pb::ExtensionSet.GetOrRegister(ref _extensions, extension); + } + public void SetExtension(pb::Extension extension, TValue value) { + pb::ExtensionSet.Set(ref _extensions, extension, value); + } + public bool HasExtension(pb::Extension extension) { + return pb::ExtensionSet.Has(ref _extensions, extension); + } + public void ClearExtension(pb::Extension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + public void ClearExtension(pb::RepeatedExtension extension) { + pb::ExtensionSet.Clear(ref _extensions, extension); + } + #region Nested types /// Container for nested types declared in the MethodOptions message type. [global::System.Diagnostics.DebuggerNonUserCodeAttribute] @@ -5645,6 +6933,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class UninterpretedOption : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new UninterpretedOption()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -5667,6 +6956,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public UninterpretedOption(UninterpretedOption other) : this() { + _hasBits0 = other._hasBits0; name_ = other.name_.Clone(); identifierValue_ = other.identifierValue_; positiveIntValue_ = other.positiveIntValue_; @@ -5694,73 +6984,148 @@ namespace Google.Protobuf.Reflection { /// Field number for the "identifier_value" field. public const int IdentifierValueFieldNumber = 3; - private string identifierValue_ = ""; + private readonly static string IdentifierValueDefaultValue = ""; + + private string identifierValue_; /// /// The value of the uninterpreted option, in whatever type the tokenizer /// identified it as during parsing. Exactly one of these should be set. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string IdentifierValue { - get { return identifierValue_; } + get { return identifierValue_ ?? IdentifierValueDefaultValue; } set { identifierValue_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "identifier_value" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasIdentifierValue { + get { return identifierValue_ != null; } + } + /// Clears the value of the "identifier_value" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearIdentifierValue() { + identifierValue_ = null; + } /// Field number for the "positive_int_value" field. public const int PositiveIntValueFieldNumber = 4; + private readonly static ulong PositiveIntValueDefaultValue = 0UL; + private ulong positiveIntValue_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public ulong PositiveIntValue { - get { return positiveIntValue_; } + get { if ((_hasBits0 & 1) != 0) { return positiveIntValue_; } else { return PositiveIntValueDefaultValue; } } set { + _hasBits0 |= 1; positiveIntValue_ = value; } } + /// Gets whether the "positive_int_value" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasPositiveIntValue { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "positive_int_value" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearPositiveIntValue() { + _hasBits0 &= ~1; + } /// Field number for the "negative_int_value" field. public const int NegativeIntValueFieldNumber = 5; + private readonly static long NegativeIntValueDefaultValue = 0L; + private long negativeIntValue_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public long NegativeIntValue { - get { return negativeIntValue_; } + get { if ((_hasBits0 & 2) != 0) { return negativeIntValue_; } else { return NegativeIntValueDefaultValue; } } set { + _hasBits0 |= 2; negativeIntValue_ = value; } } + /// Gets whether the "negative_int_value" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasNegativeIntValue { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "negative_int_value" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearNegativeIntValue() { + _hasBits0 &= ~2; + } /// Field number for the "double_value" field. public const int DoubleValueFieldNumber = 6; + private readonly static double DoubleValueDefaultValue = 0D; + private double doubleValue_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public double DoubleValue { - get { return doubleValue_; } + get { if ((_hasBits0 & 4) != 0) { return doubleValue_; } else { return DoubleValueDefaultValue; } } set { + _hasBits0 |= 4; doubleValue_ = value; } } + /// Gets whether the "double_value" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasDoubleValue { + get { return (_hasBits0 & 4) != 0; } + } + /// Clears the value of the "double_value" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearDoubleValue() { + _hasBits0 &= ~4; + } /// Field number for the "string_value" field. public const int StringValueFieldNumber = 7; - private pb::ByteString stringValue_ = pb::ByteString.Empty; + private readonly static pb::ByteString StringValueDefaultValue = pb::ByteString.Empty; + + private pb::ByteString stringValue_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public pb::ByteString StringValue { - get { return stringValue_; } + get { return stringValue_ ?? StringValueDefaultValue; } set { stringValue_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "string_value" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasStringValue { + get { return stringValue_ != null; } + } + /// Clears the value of the "string_value" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearStringValue() { + stringValue_ = null; + } /// Field number for the "aggregate_value" field. public const int AggregateValueFieldNumber = 8; - private string aggregateValue_ = ""; + private readonly static string AggregateValueDefaultValue = ""; + + private string aggregateValue_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string AggregateValue { - get { return aggregateValue_; } + get { return aggregateValue_ ?? AggregateValueDefaultValue; } set { aggregateValue_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "aggregate_value" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasAggregateValue { + get { return aggregateValue_ != null; } + } + /// Clears the value of the "aggregate_value" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearAggregateValue() { + aggregateValue_ = null; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -5789,12 +7154,12 @@ namespace Google.Protobuf.Reflection { public override int GetHashCode() { int hash = 1; hash ^= name_.GetHashCode(); - if (IdentifierValue.Length != 0) hash ^= IdentifierValue.GetHashCode(); - if (PositiveIntValue != 0UL) hash ^= PositiveIntValue.GetHashCode(); - if (NegativeIntValue != 0L) hash ^= NegativeIntValue.GetHashCode(); - if (DoubleValue != 0D) hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(DoubleValue); - if (StringValue.Length != 0) hash ^= StringValue.GetHashCode(); - if (AggregateValue.Length != 0) hash ^= AggregateValue.GetHashCode(); + if (HasIdentifierValue) hash ^= IdentifierValue.GetHashCode(); + if (HasPositiveIntValue) hash ^= PositiveIntValue.GetHashCode(); + if (HasNegativeIntValue) hash ^= NegativeIntValue.GetHashCode(); + if (HasDoubleValue) hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(DoubleValue); + if (HasStringValue) hash ^= StringValue.GetHashCode(); + if (HasAggregateValue) hash ^= AggregateValue.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -5809,27 +7174,27 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { name_.WriteTo(output, _repeated_name_codec); - if (IdentifierValue.Length != 0) { + if (HasIdentifierValue) { output.WriteRawTag(26); output.WriteString(IdentifierValue); } - if (PositiveIntValue != 0UL) { + if (HasPositiveIntValue) { output.WriteRawTag(32); output.WriteUInt64(PositiveIntValue); } - if (NegativeIntValue != 0L) { + if (HasNegativeIntValue) { output.WriteRawTag(40); output.WriteInt64(NegativeIntValue); } - if (DoubleValue != 0D) { + if (HasDoubleValue) { output.WriteRawTag(49); output.WriteDouble(DoubleValue); } - if (StringValue.Length != 0) { + if (HasStringValue) { output.WriteRawTag(58); output.WriteBytes(StringValue); } - if (AggregateValue.Length != 0) { + if (HasAggregateValue) { output.WriteRawTag(66); output.WriteString(AggregateValue); } @@ -5842,22 +7207,22 @@ namespace Google.Protobuf.Reflection { public int CalculateSize() { int size = 0; size += name_.CalculateSize(_repeated_name_codec); - if (IdentifierValue.Length != 0) { + if (HasIdentifierValue) { size += 1 + pb::CodedOutputStream.ComputeStringSize(IdentifierValue); } - if (PositiveIntValue != 0UL) { + if (HasPositiveIntValue) { size += 1 + pb::CodedOutputStream.ComputeUInt64Size(PositiveIntValue); } - if (NegativeIntValue != 0L) { + if (HasNegativeIntValue) { size += 1 + pb::CodedOutputStream.ComputeInt64Size(NegativeIntValue); } - if (DoubleValue != 0D) { + if (HasDoubleValue) { size += 1 + 8; } - if (StringValue.Length != 0) { + if (HasStringValue) { size += 1 + pb::CodedOutputStream.ComputeBytesSize(StringValue); } - if (AggregateValue.Length != 0) { + if (HasAggregateValue) { size += 1 + pb::CodedOutputStream.ComputeStringSize(AggregateValue); } if (_unknownFields != null) { @@ -5872,22 +7237,22 @@ namespace Google.Protobuf.Reflection { return; } name_.Add(other.name_); - if (other.IdentifierValue.Length != 0) { + if (other.HasIdentifierValue) { IdentifierValue = other.IdentifierValue; } - if (other.PositiveIntValue != 0UL) { + if (other.HasPositiveIntValue) { PositiveIntValue = other.PositiveIntValue; } - if (other.NegativeIntValue != 0L) { + if (other.HasNegativeIntValue) { NegativeIntValue = other.NegativeIntValue; } - if (other.DoubleValue != 0D) { + if (other.HasDoubleValue) { DoubleValue = other.DoubleValue; } - if (other.StringValue.Length != 0) { + if (other.HasStringValue) { StringValue = other.StringValue; } - if (other.AggregateValue.Length != 0) { + if (other.HasAggregateValue) { AggregateValue = other.AggregateValue; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -5947,6 +7312,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class NamePart : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new NamePart()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -5969,6 +7335,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public NamePart(NamePart other) : this() { + _hasBits0 = other._hasBits0; namePart_ = other.namePart_; isExtension_ = other.isExtension_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); @@ -5981,25 +7348,50 @@ namespace Google.Protobuf.Reflection { /// Field number for the "name_part" field. public const int NamePart_FieldNumber = 1; - private string namePart_ = ""; + private readonly static string NamePart_DefaultValue = ""; + + private string namePart_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string NamePart_ { - get { return namePart_; } + get { return namePart_ ?? NamePart_DefaultValue; } set { namePart_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "name_part" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasNamePart_ { + get { return namePart_ != null; } + } + /// Clears the value of the "name_part" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearNamePart_() { + namePart_ = null; + } /// Field number for the "is_extension" field. public const int IsExtensionFieldNumber = 2; + private readonly static bool IsExtensionDefaultValue = false; + private bool isExtension_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public bool IsExtension { - get { return isExtension_; } + get { if ((_hasBits0 & 1) != 0) { return isExtension_; } else { return IsExtensionDefaultValue; } } set { + _hasBits0 |= 1; isExtension_ = value; } } + /// Gets whether the "is_extension" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasIsExtension { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "is_extension" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearIsExtension() { + _hasBits0 &= ~1; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -6022,8 +7414,8 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (NamePart_.Length != 0) hash ^= NamePart_.GetHashCode(); - if (IsExtension != false) hash ^= IsExtension.GetHashCode(); + if (HasNamePart_) hash ^= NamePart_.GetHashCode(); + if (HasIsExtension) hash ^= IsExtension.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -6037,11 +7429,11 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (NamePart_.Length != 0) { + if (HasNamePart_) { output.WriteRawTag(10); output.WriteString(NamePart_); } - if (IsExtension != false) { + if (HasIsExtension) { output.WriteRawTag(16); output.WriteBool(IsExtension); } @@ -6053,10 +7445,10 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (NamePart_.Length != 0) { + if (HasNamePart_) { size += 1 + pb::CodedOutputStream.ComputeStringSize(NamePart_); } - if (IsExtension != false) { + if (HasIsExtension) { size += 1 + 1; } if (_unknownFields != null) { @@ -6070,10 +7462,10 @@ namespace Google.Protobuf.Reflection { if (other == null) { return; } - if (other.NamePart_.Length != 0) { + if (other.HasNamePart_) { NamePart_ = other.NamePart_; } - if (other.IsExtension != false) { + if (other.HasIsExtension) { IsExtension = other.IsExtension; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -6187,7 +7579,7 @@ namespace Google.Protobuf.Reflection { /// beginning of the "extend" block and is shared by all extensions within /// the block. /// - Just because a location's span is a subset of some other location's span - /// does not mean that it is a descendent. For example, a "group" defines + /// does not mean that it is a descendant. For example, a "group" defines /// both a type and a field in a single declaration. Thus, the locations /// corresponding to the type and field and their components will overlap. /// - Code which tries to interpret locations should probably be designed to @@ -6370,7 +7762,9 @@ namespace Google.Protobuf.Reflection { /// Field number for the "leading_comments" field. public const int LeadingCommentsFieldNumber = 3; - private string leadingComments_ = ""; + private readonly static string LeadingCommentsDefaultValue = ""; + + private string leadingComments_; /// /// If this SourceCodeInfo represents a complete declaration, these are any /// comments appearing before and after the declaration which appear to be @@ -6422,22 +7816,44 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string LeadingComments { - get { return leadingComments_; } + get { return leadingComments_ ?? LeadingCommentsDefaultValue; } set { leadingComments_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "leading_comments" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasLeadingComments { + get { return leadingComments_ != null; } + } + /// Clears the value of the "leading_comments" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearLeadingComments() { + leadingComments_ = null; + } /// Field number for the "trailing_comments" field. public const int TrailingCommentsFieldNumber = 4; - private string trailingComments_ = ""; + private readonly static string TrailingCommentsDefaultValue = ""; + + private string trailingComments_; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string TrailingComments { - get { return trailingComments_; } + get { return trailingComments_ ?? TrailingCommentsDefaultValue; } set { trailingComments_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "trailing_comments" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasTrailingComments { + get { return trailingComments_ != null; } + } + /// Clears the value of the "trailing_comments" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearTrailingComments() { + trailingComments_ = null; + } /// Field number for the "leading_detached_comments" field. public const int LeadingDetachedCommentsFieldNumber = 6; @@ -6475,8 +7891,8 @@ namespace Google.Protobuf.Reflection { int hash = 1; hash ^= path_.GetHashCode(); hash ^= span_.GetHashCode(); - if (LeadingComments.Length != 0) hash ^= LeadingComments.GetHashCode(); - if (TrailingComments.Length != 0) hash ^= TrailingComments.GetHashCode(); + if (HasLeadingComments) hash ^= LeadingComments.GetHashCode(); + if (HasTrailingComments) hash ^= TrailingComments.GetHashCode(); hash ^= leadingDetachedComments_.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); @@ -6493,11 +7909,11 @@ namespace Google.Protobuf.Reflection { public void WriteTo(pb::CodedOutputStream output) { path_.WriteTo(output, _repeated_path_codec); span_.WriteTo(output, _repeated_span_codec); - if (LeadingComments.Length != 0) { + if (HasLeadingComments) { output.WriteRawTag(26); output.WriteString(LeadingComments); } - if (TrailingComments.Length != 0) { + if (HasTrailingComments) { output.WriteRawTag(34); output.WriteString(TrailingComments); } @@ -6512,10 +7928,10 @@ namespace Google.Protobuf.Reflection { int size = 0; size += path_.CalculateSize(_repeated_path_codec); size += span_.CalculateSize(_repeated_span_codec); - if (LeadingComments.Length != 0) { + if (HasLeadingComments) { size += 1 + pb::CodedOutputStream.ComputeStringSize(LeadingComments); } - if (TrailingComments.Length != 0) { + if (HasTrailingComments) { size += 1 + pb::CodedOutputStream.ComputeStringSize(TrailingComments); } size += leadingDetachedComments_.CalculateSize(_repeated_leadingDetachedComments_codec); @@ -6532,10 +7948,10 @@ namespace Google.Protobuf.Reflection { } path_.Add(other.path_); span_.Add(other.span_); - if (other.LeadingComments.Length != 0) { + if (other.HasLeadingComments) { LeadingComments = other.LeadingComments; } - if (other.TrailingComments.Length != 0) { + if (other.HasTrailingComments) { TrailingComments = other.TrailingComments; } leadingDetachedComments_.Add(other.leadingDetachedComments_); @@ -6718,6 +8134,7 @@ namespace Google.Protobuf.Reflection { internal sealed partial class Annotation : pb::IMessage { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new Annotation()); private pb::UnknownFieldSet _unknownFields; + private int _hasBits0; [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public static pb::MessageParser Parser { get { return _parser; } } @@ -6740,6 +8157,7 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public Annotation(Annotation other) : this() { + _hasBits0 = other._hasBits0; path_ = other.path_.Clone(); sourceFile_ = other.sourceFile_; begin_ = other.begin_; @@ -6768,20 +8186,34 @@ namespace Google.Protobuf.Reflection { /// Field number for the "source_file" field. public const int SourceFileFieldNumber = 2; - private string sourceFile_ = ""; + private readonly static string SourceFileDefaultValue = ""; + + private string sourceFile_; /// /// Identifies the filesystem path to the original source .proto. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public string SourceFile { - get { return sourceFile_; } + get { return sourceFile_ ?? SourceFileDefaultValue; } set { sourceFile_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); } } + /// Gets whether the "source_file" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasSourceFile { + get { return sourceFile_ != null; } + } + /// Clears the value of the "source_file" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearSourceFile() { + sourceFile_ = null; + } /// Field number for the "begin" field. public const int BeginFieldNumber = 3; + private readonly static int BeginDefaultValue = 0; + private int begin_; /// /// Identifies the starting offset in bytes in the generated code @@ -6789,14 +8221,27 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int Begin { - get { return begin_; } + get { if ((_hasBits0 & 1) != 0) { return begin_; } else { return BeginDefaultValue; } } set { + _hasBits0 |= 1; begin_ = value; } } + /// Gets whether the "begin" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasBegin { + get { return (_hasBits0 & 1) != 0; } + } + /// Clears the value of the "begin" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearBegin() { + _hasBits0 &= ~1; + } /// Field number for the "end" field. public const int EndFieldNumber = 4; + private readonly static int EndDefaultValue = 0; + private int end_; /// /// Identifies the ending offset in bytes in the generated code that @@ -6805,11 +8250,22 @@ namespace Google.Protobuf.Reflection { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int End { - get { return end_; } + get { if ((_hasBits0 & 2) != 0) { return end_; } else { return EndDefaultValue; } } set { + _hasBits0 |= 2; end_ = value; } } + /// Gets whether the "end" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool HasEnd { + get { return (_hasBits0 & 2) != 0; } + } + /// Clears the value of the "end" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void ClearEnd() { + _hasBits0 &= ~2; + } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { @@ -6835,9 +8291,9 @@ namespace Google.Protobuf.Reflection { public override int GetHashCode() { int hash = 1; hash ^= path_.GetHashCode(); - if (SourceFile.Length != 0) hash ^= SourceFile.GetHashCode(); - if (Begin != 0) hash ^= Begin.GetHashCode(); - if (End != 0) hash ^= End.GetHashCode(); + if (HasSourceFile) hash ^= SourceFile.GetHashCode(); + if (HasBegin) hash ^= Begin.GetHashCode(); + if (HasEnd) hash ^= End.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -6852,15 +8308,15 @@ namespace Google.Protobuf.Reflection { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { path_.WriteTo(output, _repeated_path_codec); - if (SourceFile.Length != 0) { + if (HasSourceFile) { output.WriteRawTag(18); output.WriteString(SourceFile); } - if (Begin != 0) { + if (HasBegin) { output.WriteRawTag(24); output.WriteInt32(Begin); } - if (End != 0) { + if (HasEnd) { output.WriteRawTag(32); output.WriteInt32(End); } @@ -6873,13 +8329,13 @@ namespace Google.Protobuf.Reflection { public int CalculateSize() { int size = 0; size += path_.CalculateSize(_repeated_path_codec); - if (SourceFile.Length != 0) { + if (HasSourceFile) { size += 1 + pb::CodedOutputStream.ComputeStringSize(SourceFile); } - if (Begin != 0) { + if (HasBegin) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(Begin); } - if (End != 0) { + if (HasEnd) { size += 1 + pb::CodedOutputStream.ComputeInt32Size(End); } if (_unknownFields != null) { @@ -6894,13 +8350,13 @@ namespace Google.Protobuf.Reflection { return; } path_.Add(other.path_); - if (other.SourceFile.Length != 0) { + if (other.HasSourceFile) { SourceFile = other.SourceFile; } - if (other.Begin != 0) { + if (other.HasBegin) { Begin = other.Begin; } - if (other.End != 0) { + if (other.HasEnd) { End = other.End; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs index 194041a889..6befbf1c2b 100644 --- a/csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs @@ -30,6 +30,8 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using System.Collections.Generic; + namespace Google.Protobuf.Reflection { /// @@ -37,29 +39,22 @@ namespace Google.Protobuf.Reflection /// public abstract class DescriptorBase : IDescriptor { - private readonly FileDescriptor file; - private readonly string fullName; - private readonly int index; - internal DescriptorBase(FileDescriptor file, string fullName, int index) { - this.file = file; - this.fullName = fullName; - this.index = index; + File = file; + FullName = fullName; + Index = index; } /// - /// The index of this descriptor within its parent descriptor. + /// The index of this descriptor within its parent descriptor. /// /// /// This returns the index of this descriptor within its parent, for /// this descriptor's type. (There can be duplicate values for different /// types, e.g. one enum type with index 0 and one message type with index 0.) /// - public int Index - { - get { return index; } - } + public int Index { get; } /// /// Returns the name of the entity (field, message etc) being described. @@ -69,17 +64,29 @@ namespace Google.Protobuf.Reflection /// /// The fully qualified name of the descriptor's target. /// - public string FullName - { - get { return fullName; } - } + public string FullName { get; } /// /// The file this descriptor was declared in. /// - public FileDescriptor File - { - get { return file; } - } + public FileDescriptor File { get; } + + /// + /// The declaration information about the descriptor, or null if no declaration information + /// is available for this descriptor. + /// + /// + /// This information is typically only available for dynamically loaded descriptors, + /// for example within a protoc plugin where the full descriptors, including source info, + /// are passed to the code by protoc. + /// + public DescriptorDeclaration Declaration => File.GetDeclaration(this); + + /// + /// Retrieves the list of nested descriptors corresponding to the given field number, if any. + /// If the field is unknown or not a nested descriptor list, return null to terminate the search. + /// The default implementation returns null. + /// + internal virtual IReadOnlyList GetNestedDescriptorListForField(int fieldNumber) => null; } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorDeclaration.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorDeclaration.cs new file mode 100644 index 0000000000..b22048f0a8 --- /dev/null +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorDeclaration.cs @@ -0,0 +1,112 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2018 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. +#endregion + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using static Google.Protobuf.Reflection.SourceCodeInfo.Types; + +namespace Google.Protobuf.Reflection +{ + /// + /// Provides additional information about the declaration of a descriptor, + /// such as source location and comments. + /// + public sealed class DescriptorDeclaration + { + /// + /// The descriptor this declaration relates to. + /// + public IDescriptor Descriptor { get; } + + /// + /// The start line of the declaration within the source file. This value is 1-based. + /// + public int StartLine { get; } + /// + /// The start column of the declaration within the source file. This value is 1-based. + /// + public int StartColumn { get; } + + /// + /// // The end line of the declaration within the source file. This value is 1-based. + /// + public int EndLine { get; } + /// + /// The end column of the declaration within the source file. This value is 1-based, and + /// exclusive. (The final character of the declaration is on the column before this value.) + /// + public int EndColumn { get; } + + /// + /// Comments appearing before the declaration. Never null, but may be empty. Multi-line comments + /// are represented as a newline-separated string. Leading whitespace and the comment marker ("//") + /// are removed from each line. + /// + public string LeadingComments { get; } + + /// + /// Comments appearing after the declaration. Never null, but may be empty. Multi-line comments + /// are represented as a newline-separated string. Leading whitespace and the comment marker ("//") + /// are removed from each line. + /// + public string TrailingComments { get; } + + /// + /// Comments appearing before the declaration, but separated from it by blank + /// lines. Each string represents a newline-separated paragraph of comments. + /// Leading whitespace and the comment marker ("//") are removed from each line. + /// The list is never null, but may be empty. Likewise each element is never null, but may be empty. + /// + public IReadOnlyList LeadingDetachedComments { get; } + + private DescriptorDeclaration(IDescriptor descriptor, Location location) + { + // TODO: Validation + Descriptor = descriptor; + bool hasEndLine = location.Span.Count == 4; + // Lines and columns are 0-based in the proto. + StartLine = location.Span[0] + 1; + StartColumn = location.Span[1] + 1; + EndLine = hasEndLine ? location.Span[2] + 1 : StartLine; + EndColumn = location.Span[hasEndLine ? 3 : 2] + 1; + LeadingComments = location.LeadingComments; + TrailingComments = location.TrailingComments; + LeadingDetachedComments = new ReadOnlyCollection(location.LeadingDetachedComments.ToList()); + } + + internal static DescriptorDeclaration FromProto(IDescriptor descriptor, Location location) => + new DescriptorDeclaration(descriptor, location); + } +} diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs index 99ca4bf34f..93f2fa9a4a 100644 --- a/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs @@ -45,21 +45,21 @@ namespace Google.Protobuf.Reflection private readonly IDictionary descriptorsByName = new Dictionary(); - private readonly IDictionary fieldsByNumber = - new Dictionary(); + private readonly IDictionary, FieldDescriptor> fieldsByNumber = + new Dictionary, FieldDescriptor>(); - private readonly IDictionary enumValuesByNumber = - new Dictionary(); + private readonly IDictionary, EnumValueDescriptor> enumValuesByNumber = + new Dictionary, EnumValueDescriptor>(); private readonly HashSet dependencies; - internal DescriptorPool(FileDescriptor[] dependencyFiles) + internal DescriptorPool(IEnumerable dependencyFiles) { dependencies = new HashSet(); - for (int i = 0; i < dependencyFiles.Length; i++) + foreach (var dependencyFile in dependencyFiles) { - dependencies.Add(dependencyFiles[i]); - ImportPublicDependencies(dependencyFiles[i]); + dependencies.Add(dependencyFile); + ImportPublicDependencies(dependencyFile); } foreach (FileDescriptor dependency in dependencyFiles) @@ -209,14 +209,14 @@ namespace Google.Protobuf.Reflection internal FieldDescriptor FindFieldByNumber(MessageDescriptor messageDescriptor, int number) { FieldDescriptor ret; - fieldsByNumber.TryGetValue(new DescriptorIntPair(messageDescriptor, number), out ret); + fieldsByNumber.TryGetValue(new ObjectIntPair(messageDescriptor, number), out ret); return ret; } internal EnumValueDescriptor FindEnumValueByNumber(EnumDescriptor enumDescriptor, int number) { EnumValueDescriptor ret; - enumValuesByNumber.TryGetValue(new DescriptorIntPair(enumDescriptor, number), out ret); + enumValuesByNumber.TryGetValue(new ObjectIntPair(enumDescriptor, number), out ret); return ret; } @@ -227,7 +227,8 @@ namespace Google.Protobuf.Reflection /// containing type and number already exists. internal void AddFieldByNumber(FieldDescriptor field) { - DescriptorIntPair key = new DescriptorIntPair(field.ContainingType, field.FieldNumber); + // for extensions, we use the extended type, otherwise we use the containing type + ObjectIntPair key = new ObjectIntPair(field.Proto.HasExtendee ? field.ExtendeeType : field.ContainingType, field.FieldNumber); FieldDescriptor old; if (fieldsByNumber.TryGetValue(key, out old)) { @@ -246,7 +247,7 @@ namespace Google.Protobuf.Reflection /// internal void AddEnumValueByNumber(EnumValueDescriptor enumValue) { - DescriptorIntPair key = new DescriptorIntPair(enumValue.EnumDescriptor, enumValue.Number); + ObjectIntPair key = new ObjectIntPair(enumValue.EnumDescriptor, enumValue.Number); if (!enumValuesByNumber.ContainsKey(key)) { enumValuesByNumber[key] = enumValue; @@ -329,40 +330,5 @@ namespace Google.Protobuf.Reflection return result; } } - - /// - /// Struct used to hold the keys for the fieldByNumber table. - /// - private struct DescriptorIntPair : IEquatable - { - private readonly int number; - private readonly IDescriptor descriptor; - - internal DescriptorIntPair(IDescriptor descriptor, int number) - { - this.number = number; - this.descriptor = descriptor; - } - - public bool Equals(DescriptorIntPair other) - { - return descriptor == other.descriptor - && number == other.number; - } - - public override bool Equals(object obj) - { - if (obj is DescriptorIntPair) - { - return Equals((DescriptorIntPair) obj); - } - return false; - } - - public override int GetHashCode() - { - return descriptor.GetHashCode()*((1 << 16) - 1) + number; - } - } } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs index 89c73a61b2..36a1d0a5ec 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs @@ -30,6 +30,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Collections; using System; using System.Collections.Generic; @@ -72,6 +73,17 @@ namespace Google.Protobuf.Reflection /// public override string Name { get { return proto.Name; } } + internal override IReadOnlyList GetNestedDescriptorListForField(int fieldNumber) + { + switch (fieldNumber) + { + case EnumDescriptorProto.ValueFieldNumber: + return (IReadOnlyList) Values; + default: + return null; + } + } + /// /// The CLR type for this enum. For generated code, this will be a CLR enum type. /// @@ -116,6 +128,26 @@ namespace Google.Protobuf.Reflection /// /// The (possibly empty) set of custom options for this enum. /// - public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; + //[Obsolete("CustomOptions are obsolete. Use GetOption")] + public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + + /* // uncomment this in the full proto2 support PR + /// + /// Gets a single value enum option for this descriptor + /// + public T GetOption(Extension extension) + { + var value = Proto.Options.GetExtension(extension); + return value is IDeepCloneable clonable ? clonable.Clone() : value; + } + + /// + /// Gets a repeated value enum option for this descriptor + /// + public RepeatedField GetOption(RepeatedExtension extension) + { + return Proto.Options.GetExtension(extension).Clone(); + } + */ } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs index 8b838c68f0..99c989130c 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs @@ -30,12 +30,15 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Collections; +using System; + namespace Google.Protobuf.Reflection { /// /// Descriptor for a single enum value within an enum in a .proto file. /// - public sealed class EnumValueDescriptor : DescriptorBase + public sealed class EnumValueDescriptor : DescriptorBase { private readonly EnumDescriptor enumDescriptor; private readonly EnumValueDescriptorProto proto; @@ -70,6 +73,27 @@ namespace Google.Protobuf.Reflection /// /// The (possibly empty) set of custom options for this enum value. /// - public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; + //[Obsolete("CustomOptions are obsolete. Use GetOption")] + public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + + /* // uncomment this in the full proto2 support PR + /// + /// Gets a single value enum option for this descriptor + /// + public T GetOption(Extension extension) + { + var value = Proto.Options.GetExtension(extension); + return value is IDeepCloneable clonable ? clonable.Clone() : value; + } + + /// + /// Gets a repeated value enum option for this descriptor + /// + public RepeatedField GetOption(RepeatedExtension extension) + { + return Proto.Options.GetExtension(extension).Clone(); + } + */ } -} \ No newline at end of file +} + \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/PartialClasses.cs b/csharp/src/Google.Protobuf/Reflection/ExtensionAccessor.cs similarity index 62% rename from csharp/src/Google.Protobuf/Reflection/PartialClasses.cs rename to csharp/src/Google.Protobuf/Reflection/ExtensionAccessor.cs index 8c055d6d92..81d6e35f01 100644 --- a/csharp/src/Google.Protobuf/Reflection/PartialClasses.cs +++ b/csharp/src/Google.Protobuf/Reflection/ExtensionAccessor.cs @@ -1,59 +1,69 @@ -#region Copyright notice and license -// 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. -#endregion - -// This file just contains partial classes for any autogenerated classes that need additional support. -namespace Google.Protobuf.Reflection -{ - internal partial class FieldDescriptorProto - { - // We can't tell the difference between "explicitly set to 0" and "not set" - // in proto3, but we need to tell the difference for OneofIndex. descriptor.proto - // is really a proto2 file, but the runtime doesn't know about proto2 semantics... - // We fake it by defaulting to -1. - partial void OnConstruction() - { - OneofIndex = -1; - } - } - - internal partial class FieldOptions - { - // We can't tell the difference between "explicitly set to false" and "not set" - // in proto3, but we need to tell the difference for FieldDescriptor.IsPacked. - // This won't work if we ever need to support proto2, but at that point we'll be - // able to remove this hack and use field presence instead. - partial void OnConstruction() - { - Packed = true; - } - } -} \ No newline at end of file +#region Copyright notice and license +// 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. +#endregion + +namespace Google.Protobuf.Reflection +{ + internal sealed class ExtensionAccessor : IFieldAccessor + { + private readonly Extension extension; + private readonly ReflectionUtil.IExtensionReflectionHelper helper; + + internal ExtensionAccessor(FieldDescriptor descriptor) + { + Descriptor = descriptor; + extension = descriptor.Extension; + helper = ReflectionUtil.CreateExtensionHelper(extension); + } + + public FieldDescriptor Descriptor { get; } + + public void Clear(IMessage message) + { + helper.ClearExtension(message); + } + + public bool HasValue(IMessage message) + { + return helper.HasExtension(message); + } + + public object GetValue(IMessage message) + { + return helper.GetExtension(message); + } + + public void SetValue(IMessage message, object value) + { + helper.SetExtension(message, value); + } + } +} diff --git a/csharp/src/Google.Protobuf/Reflection/ExtensionCollection.cs b/csharp/src/Google.Protobuf/Reflection/ExtensionCollection.cs new file mode 100644 index 0000000000..9cb6abaf9a --- /dev/null +++ b/csharp/src/Google.Protobuf/Reflection/ExtensionCollection.cs @@ -0,0 +1,105 @@ +#region Copyright notice and license +// 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. +#endregion + +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; + +namespace Google.Protobuf.Reflection +{ + /// + /// A collection to simplify retrieving the descriptors of extensions in a descriptor for a message + /// + public class ExtensionCollection + { + private IDictionary> extensionsByTypeInDeclarationOrder; + private IDictionary> extensionsByTypeInNumberOrder; + + internal ExtensionCollection(FileDescriptor file, Extension[] extensions) + { + UnorderedExtensions = DescriptorUtil.ConvertAndMakeReadOnly( + file.Proto.Extension, + (extension, i) => new FieldDescriptor(extension, file, null, i, null, extensions?[i])); + } + + internal ExtensionCollection(MessageDescriptor message, Extension[] extensions) + { + UnorderedExtensions = DescriptorUtil.ConvertAndMakeReadOnly( + message.Proto.Extension, + (extension, i) => new FieldDescriptor(extension, message.File, message, i, null, extensions?[i])); + } + + /// + /// Returns a readonly list of all the extensions defined in this type in + /// the order they were defined in the source .proto file + /// + public IList UnorderedExtensions { get; } + + /// + /// Returns a readonly list of all the extensions define in this type that extend + /// the provided descriptor type in the order they were defined in the source .proto file + /// + public IList GetExtensionsInDeclarationOrder(MessageDescriptor descriptor) + { + return extensionsByTypeInDeclarationOrder[descriptor]; + } + + /// + /// Returns a readonly list of all the extensions define in this type that extend + /// the provided descriptor type in accending field order + /// + public IList GetExtensionsInNumberOrder(MessageDescriptor descriptor) + { + return extensionsByTypeInNumberOrder[descriptor]; + } + + internal void CrossLink() + { + Dictionary> declarationOrder = new Dictionary>(); + foreach (FieldDescriptor descriptor in UnorderedExtensions) + { + descriptor.CrossLink(); + + IList _; + if (!declarationOrder.TryGetValue(descriptor.ExtendeeType, out _)) + declarationOrder.Add(descriptor.ExtendeeType, new List()); + + declarationOrder[descriptor.ExtendeeType].Add(descriptor); + } + + extensionsByTypeInDeclarationOrder = declarationOrder + .ToDictionary(kvp => kvp.Key, kvp => (IList)new ReadOnlyCollection(kvp.Value)); + extensionsByTypeInNumberOrder = declarationOrder + .ToDictionary(kvp => kvp.Key, kvp => (IList)new ReadOnlyCollection(kvp.Value.OrderBy(field => field.FieldNumber).ToArray())); + } + } +} diff --git a/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs b/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs index 82ce50518d..85b7d39a83 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs @@ -57,6 +57,7 @@ namespace Google.Protobuf.Reflection return getValueDelegate(message); } + public abstract bool HasValue(IMessage message); public abstract void Clear(IMessage message); public abstract void SetValue(IMessage message, object value); } diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index 2a3d5c7a2d..5a2f7f3d8e 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -30,6 +30,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Collections; using Google.Protobuf.Compatibility; using System; @@ -41,13 +42,14 @@ namespace Google.Protobuf.Reflection public sealed class FieldDescriptor : DescriptorBase, IComparable { private EnumDescriptor enumType; + private MessageDescriptor extendeeType; private MessageDescriptor messageType; private FieldType fieldType; private readonly string propertyName; // Annoyingly, needed in Crosslink. private IFieldAccessor accessor; /// - /// Get the field's containing message type. + /// Get the field's containing message type, or null if it is a field defined at the top level of a file as an extension. /// public MessageDescriptor ContainingType { get; } @@ -64,8 +66,10 @@ namespace Google.Protobuf.Reflection internal FieldDescriptorProto Proto { get; } + internal Extension Extension { get; } + internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file, - MessageDescriptor parent, int index, string propertyName) + MessageDescriptor parent, int index, string propertyName, Extension extension) : base(file, file.ComputeFullName(parent, proto.Name), index) { Proto = proto; @@ -79,8 +83,7 @@ namespace Google.Protobuf.Reflection throw new DescriptorValidationException(this, "Field numbers must be positive integers."); } ContainingType = parent; - // OneofIndex "defaults" to -1 due to a hack in FieldDescriptor.OnConstruction. - if (proto.OneofIndex != -1) + if (proto.HasOneofIndex) { if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDecl.Count) { @@ -97,9 +100,10 @@ namespace Google.Protobuf.Reflection // We could trust the generated code and check whether the type of the property is // a MapField, but that feels a tad nasty. this.propertyName = propertyName; + Extension = extension; JsonName = Proto.JsonName == "" ? JsonFormatter.ToJsonName(Proto.Name) : Proto.JsonName; } - + /// /// The brief name of the descriptor's target. @@ -116,16 +120,21 @@ namespace Google.Protobuf.Reflection /// that is the responsibility of the accessor. /// /// - /// The value returned by this property will be non-null for all regular fields. However, - /// if a message containing a map field is introspected, the list of nested messages will include + /// In descriptors for generated code, the value returned by this property will be non-null for all + /// regular fields. However, if a message containing a map field is introspected, the list of nested messages will include /// an auto-generated nested key/value pair message for the field. This is not represented in any /// generated type, and the value of the map field itself is represented by a dictionary in the /// reflection API. There are never instances of those "hidden" messages, so no accessor is provided /// and this property will return null. /// + /// + /// In dynamically loaded descriptors, the value returned by this property will current be null; + /// if and when dynamic messages are supported, it will return a suitable accessor to work with + /// them. + /// /// public IFieldAccessor Accessor => accessor; - + /// /// Maps a field type as included in the .proto file to a FieldType. /// @@ -179,6 +188,11 @@ namespace Google.Protobuf.Reflection /// public bool IsRepeated => Proto.Label == FieldDescriptorProto.Types.Label.Repeated; + /// + /// Returns true if this field is a required field; false otherwise. + /// + public bool IsRequired => Proto.Label == FieldDescriptorProto.Types.Label.Required; + /// /// Returns true if this field is a map field; false otherwise. /// @@ -187,13 +201,8 @@ namespace Google.Protobuf.Reflection /// /// Returns true if this field is a packed, repeated field; false otherwise. /// - public bool IsPacked => - // Note the || rather than && here - we're effectively defaulting to packed, because that *is* - // the default in proto3, which is all we support. We may give the wrong result for the protos - // within descriptor.proto, but that's okay, as they're never exposed and we don't use IsPacked - // within the runtime. - Proto.Options == null || Proto.Options.Packed; - + public bool IsPacked => File.Proto.Syntax == "proto2" ? Proto.Options?.Packed ?? false : !Proto.Options.HasPacked || Proto.Options.Packed; + /// /// Returns the type of the field. /// @@ -242,30 +251,65 @@ namespace Google.Protobuf.Reflection { get { - if (fieldType != FieldType.Message) + if (fieldType != FieldType.Message && fieldType != FieldType.Group) { - throw new InvalidOperationException("MessageType is only valid for message fields."); + throw new InvalidOperationException("MessageType is only valid for message or group fields."); } return messageType; } } + /// + /// For extension fields, returns the extended type + /// + public MessageDescriptor ExtendeeType + { + get + { + if (!Proto.HasExtendee) + { + throw new InvalidOperationException("ExtendeeType is only valid for extension fields."); + } + return extendeeType; + } + } + /// /// The (possibly empty) set of custom options for this field. /// - public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; + //[Obsolete("CustomOptions are obsolete. Use GetOption")] + public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + + /* // uncomment this in the full proto2 support PR + /// + /// Gets a single value enum option for this descriptor + /// + public T GetOption(Extension extension) + { + var value = Proto.Options.GetExtension(extension); + return value is IDeepCloneable clonable ? clonable.Clone() : value; + } + + /// + /// Gets a repeated value enum option for this descriptor + /// + public RepeatedField GetOption(RepeatedExtension extension) + { + return Proto.Options.GetExtension(extension).Clone(); + } + */ /// /// Look up and cross-link all field types etc. /// internal void CrossLink() { - if (Proto.TypeName != "") + if (Proto.HasTypeName) { IDescriptor typeDescriptor = File.DescriptorPool.LookupSymbol(Proto.TypeName, this); - if (Proto.Type != 0) + if (Proto.HasType) { // Choose field type based on symbol. if (typeDescriptor is MessageDescriptor) @@ -282,7 +326,7 @@ namespace Google.Protobuf.Reflection } } - if (fieldType == FieldType.Message) + if (fieldType == FieldType.Message || fieldType == FieldType.Group) { if (!(typeDescriptor is MessageDescriptor)) { @@ -290,7 +334,7 @@ namespace Google.Protobuf.Reflection } messageType = (MessageDescriptor) typeDescriptor; - if (Proto.DefaultValue != "") + if (Proto.HasDefaultValue) { throw new DescriptorValidationException(this, "Messages can't have default values."); } @@ -316,11 +360,16 @@ namespace Google.Protobuf.Reflection } } + if (Proto.HasExtendee) + { + extendeeType = File.DescriptorPool.LookupSymbol(Proto.Extendee, this) as MessageDescriptor; + } + // Note: no attempt to perform any default value parsing File.DescriptorPool.AddFieldByNumber(this); - if (ContainingType != null && ContainingType.Proto.Options != null && ContainingType.Proto.Options.MessageSetWireFormat) + if (ContainingType != null && ContainingType.Proto.HasOptions && ContainingType.Proto.Options.MessageSetWireFormat) { throw new DescriptorValidationException(this, "MessageSet format is not supported."); } @@ -330,11 +379,17 @@ namespace Google.Protobuf.Reflection private IFieldAccessor CreateAccessor() { // If we're given no property name, that's because we really don't want an accessor. - // (At the moment, that means it's a map entry message...) + // This could be because it's a map message, or it could be that we're loading a FileDescriptor dynamically. + // TODO: Support dynamic messages. if (propertyName == null) { return null; } + + if (Extension != null) + { + return new ExtensionAccessor(this); + } var property = ContainingType.ClrType.GetProperty(propertyName); if (property == null) { @@ -345,4 +400,5 @@ namespace Google.Protobuf.Reflection : (IFieldAccessor) new SingleFieldAccessor(property, this); } } -} \ No newline at end of file +} + \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/FieldType.cs b/csharp/src/Google.Protobuf/Reflection/FieldType.cs index 1658e34cd1..37af17915e 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldType.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldType.cs @@ -74,7 +74,7 @@ namespace Google.Protobuf.Reflection /// String, /// - /// The field type used for groups (not supported in this implementation). + /// The field type used for groups. /// Group, /// diff --git a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs index be94cb100e..4c2fe54d56 100644 --- a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs @@ -30,10 +30,15 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Collections; using Google.Protobuf.WellKnownTypes; using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; +using System.Linq; +using System.Threading; +using static Google.Protobuf.Reflection.SourceCodeInfo.Types; namespace Google.Protobuf.Reflection { @@ -54,12 +59,14 @@ namespace Google.Protobuf.Reflection ForceReflectionInitialization(); } - private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo) + private readonly Lazy> declarations; + + private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, IEnumerable dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo) { SerializedData = descriptorData; DescriptorPool = pool; Proto = proto; - Dependencies = new ReadOnlyCollection((FileDescriptor[]) dependencies.Clone()); + Dependencies = new ReadOnlyCollection(dependencies.ToList()); PublicDependencies = DeterminePublicDependencies(this, proto, dependencies, allowUnknownDependencies); @@ -67,15 +74,93 @@ namespace Google.Protobuf.Reflection MessageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageType, (message, index) => - new MessageDescriptor(message, this, null, index, generatedCodeInfo.NestedTypes[index])); + new MessageDescriptor(message, this, null, index, generatedCodeInfo?.NestedTypes[index])); EnumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType, (enumType, index) => - new EnumDescriptor(enumType, this, null, index, generatedCodeInfo.NestedEnums[index])); + new EnumDescriptor(enumType, this, null, index, generatedCodeInfo?.NestedEnums[index])); Services = DescriptorUtil.ConvertAndMakeReadOnly(proto.Service, (service, index) => new ServiceDescriptor(service, this, index)); + + Extensions = new ExtensionCollection(this, generatedCodeInfo?.Extensions); + + declarations = new Lazy>(CreateDeclarationMap, LazyThreadSafetyMode.ExecutionAndPublication); + } + + private Dictionary CreateDeclarationMap() + { + var dictionary = new Dictionary(); + foreach (var location in Proto.SourceCodeInfo?.Location ?? Enumerable.Empty()) + { + var descriptor = FindDescriptorForPath(location.Path); + if (descriptor != null) + { + dictionary[descriptor] = DescriptorDeclaration.FromProto(descriptor, location); + } + } + return dictionary; + } + + private IDescriptor FindDescriptorForPath(IList path) + { + // All complete declarations have an even, non-empty path length + // (There can be an empty path for a descriptor declaration, but that can't have any comments, + // so we currently ignore it.) + if (path.Count == 0 || (path.Count & 1) != 0) + { + return null; + } + IReadOnlyList topLevelList = GetNestedDescriptorListForField(path[0]); + DescriptorBase current = GetDescriptorFromList(topLevelList, path[1]); + + for (int i = 2; current != null && i < path.Count; i += 2) + { + var list = current.GetNestedDescriptorListForField(path[i]); + current = GetDescriptorFromList(list, path[i + 1]); + } + return current; + } + + private DescriptorBase GetDescriptorFromList(IReadOnlyList list, int index) + { + // This is fine: it may be a newer version of protobuf than we understand, with a new descriptor + // field. + if (list == null) + { + return null; + } + // We *could* return null to silently continue, but this is basically data corruption. + if (index < 0 || index >= list.Count) + { + // We don't have much extra information to give at this point unfortunately. If this becomes a problem, + // we can pass in the complete path and report that and the file name. + throw new InvalidProtocolBufferException($"Invalid descriptor location path: index out of range"); + } + return list[index]; + } + + private IReadOnlyList GetNestedDescriptorListForField(int fieldNumber) + { + switch (fieldNumber) + { + case FileDescriptorProto.ServiceFieldNumber: + return (IReadOnlyList) Services; + case FileDescriptorProto.MessageTypeFieldNumber: + return (IReadOnlyList) MessageTypes; + case FileDescriptorProto.EnumTypeFieldNumber: + return (IReadOnlyList) EnumTypes; + default: + return null; + } + } + + internal DescriptorDeclaration GetDeclaration(IDescriptor descriptor) + { + DescriptorDeclaration declaration; + declarations.Value.TryGetValue(descriptor, out declaration); + return declaration; } /// @@ -98,13 +183,9 @@ namespace Google.Protobuf.Reflection /// Extracts public dependencies from direct dependencies. This is a static method despite its /// first parameter, as the value we're in the middle of constructing is only used for exceptions. /// - private static IList DeterminePublicDependencies(FileDescriptor @this, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies) + private static IList DeterminePublicDependencies(FileDescriptor @this, FileDescriptorProto proto, IEnumerable dependencies, bool allowUnknownDependencies) { - var nameToFileMap = new Dictionary(); - foreach (var file in dependencies) - { - nameToFileMap[file.Name] = file; - } + var nameToFileMap = dependencies.ToDictionary(file => file.Name); var publicDependencies = new List(); for (int i = 0; i < proto.PublicDependency.Count; i++) { @@ -114,8 +195,8 @@ namespace Google.Protobuf.Reflection throw new DescriptorValidationException(@this, "Invalid public dependency index."); } string name = proto.Dependency[index]; - FileDescriptor file = nameToFileMap[name]; - if (file == null) + FileDescriptor file; + if (!nameToFileMap.TryGetValue(name, out file)) { if (!allowUnknownDependencies) { @@ -162,6 +243,11 @@ namespace Google.Protobuf.Reflection /// public IList Services { get; } + /// + /// Unmodifiable list of top-level extensions declared in this file. + /// + public ExtensionCollection Extensions { get; } + /// /// Unmodifiable list of this file's dependencies (imports). /// @@ -278,6 +364,8 @@ namespace Google.Protobuf.Reflection { service.CrossLink(); } + + Extensions.CrossLink(); } /// @@ -293,10 +381,12 @@ namespace Google.Protobuf.Reflection FileDescriptor[] dependencies, GeneratedClrTypeInfo generatedCodeInfo) { + ExtensionRegistry registry = new ExtensionRegistry(); + AddAllExtensions(dependencies, generatedCodeInfo, registry); FileDescriptorProto proto; try { - proto = FileDescriptorProto.Parser.ParseFrom(descriptorData); + proto = FileDescriptorProto.Parser.WithExtensionRegistry(registry).ParseFrom(descriptorData); } catch (InvalidProtocolBufferException e) { @@ -315,6 +405,76 @@ namespace Google.Protobuf.Reflection } } + private static void AddAllExtensions(FileDescriptor[] dependencies, GeneratedClrTypeInfo generatedInfo, ExtensionRegistry registry) + { + registry.Add(dependencies.SelectMany(GetAllDependedExtensions).Concat(GetAllGeneratedExtensions(generatedInfo)).ToArray()); + } + + private static IEnumerable GetAllGeneratedExtensions(GeneratedClrTypeInfo generated) + { + return generated.Extensions.Concat(generated.NestedTypes.Where(t => t != null).SelectMany(GetAllGeneratedExtensions)); + } + + private static IEnumerable GetAllDependedExtensions(FileDescriptor descriptor) + { + return descriptor.Extensions.UnorderedExtensions + .Select(s => s.Extension) + .Concat(descriptor.Dependencies.Concat(descriptor.PublicDependencies).SelectMany(GetAllDependedExtensions)) + .Concat(descriptor.MessageTypes.SelectMany(GetAllDependedExtensionsFromMessage)); + } + + private static IEnumerable GetAllDependedExtensionsFromMessage(MessageDescriptor descriptor) + { + return descriptor.Extensions.UnorderedExtensions + .Select(s => s.Extension) + .Concat(descriptor.NestedTypes.SelectMany(GetAllDependedExtensionsFromMessage)); + } + + /// + /// Converts the given descriptor binary data into FileDescriptor objects. + /// Note: reflection using the returned FileDescriptors is not currently supported. + /// + /// The binary file descriptor proto data. Must not be null, and any + /// dependencies must come before the descriptor which depends on them. (If A depends on B, and B + /// depends on C, then the descriptors must be presented in the order C, B, A.) This is compatible + /// with the order in which protoc provides descriptors to plugins. + /// The file descriptors corresponding to . + public static IReadOnlyList BuildFromByteStrings(IEnumerable descriptorData) + { + ProtoPreconditions.CheckNotNull(descriptorData, nameof(descriptorData)); + + // TODO: See if we can build a single DescriptorPool instead of building lots of them. + // This will all behave correctly, but it's less efficient than we'd like. + var descriptors = new List(); + var descriptorsByName = new Dictionary(); + foreach (var data in descriptorData) + { + var proto = FileDescriptorProto.Parser.ParseFrom(data); + var dependencies = new List(); + foreach (var dependencyName in proto.Dependency) + { + FileDescriptor dependency; + if (!descriptorsByName.TryGetValue(dependencyName, out dependency)) + { + throw new ArgumentException($"Dependency missing: {dependencyName}"); + } + dependencies.Add(dependency); + } + var pool = new DescriptorPool(dependencies); + FileDescriptor descriptor = new FileDescriptor( + data, proto, dependencies, pool, + allowUnknownDependencies: false, generatedCodeInfo: null); + descriptor.CrossLink(); + descriptors.Add(descriptor); + if (descriptorsByName.ContainsKey(descriptor.Name)) + { + throw new ArgumentException($"Duplicate descriptor name: {descriptor.Name}"); + } + descriptorsByName.Add(descriptor.Name, descriptor); + } + return new ReadOnlyCollection(descriptors); + } + /// /// Returns a that represents this instance. /// @@ -344,7 +504,27 @@ namespace Google.Protobuf.Reflection /// /// The (possibly empty) set of custom options for this file. /// - public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; + //[Obsolete("CustomOptions are obsolete. Use GetOption")] + public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + + /* // uncomment this in the full proto2 support PR + /// + /// Gets a single value enum option for this descriptor + /// + public T GetOption(Extension extension) + { + var value = Proto.Options.GetExtension(extension); + return value is IDeepCloneable clonable ? clonable.Clone() : value; + } + + /// + /// Gets a repeated value enum option for this descriptor + /// + public RepeatedField GetOption(RepeatedExtension extension) + { + return Proto.Options.GetExtension(extension).Clone(); + } + */ /// /// Performs initialization for the given generic type argument. diff --git a/csharp/src/Google.Protobuf/Reflection/GeneratedClrTypeInfo.cs b/csharp/src/Google.Protobuf/Reflection/GeneratedClrTypeInfo.cs index fe5db65656..479e177130 100644 --- a/csharp/src/Google.Protobuf/Reflection/GeneratedClrTypeInfo.cs +++ b/csharp/src/Google.Protobuf/Reflection/GeneratedClrTypeInfo.cs @@ -42,6 +42,7 @@ namespace Google.Protobuf.Reflection { private static readonly string[] EmptyNames = new string[0]; private static readonly GeneratedClrTypeInfo[] EmptyCodeInfo = new GeneratedClrTypeInfo[0]; + private static readonly Extension[] EmptyExtensions = new Extension[0]; /// /// Irrelevant for file descriptors; the CLR type for the message for message descriptors. @@ -59,6 +60,11 @@ namespace Google.Protobuf.Reflection /// public string[] PropertyNames { get; } + /// + /// The extensions defined within this file/message descriptor + /// + public Extension[] Extensions { get; } + /// /// Irrelevant for file descriptors; the CLR property "base" names (in message descriptor oneof order) /// for oneofs in the message for message descriptors. It is expected that for a oneof name of "Foo", @@ -82,16 +88,35 @@ namespace Google.Protobuf.Reflection /// Each array parameter may be null, to indicate a lack of values. /// The parameter order is designed to make it feasible to format the generated code readably. /// - public GeneratedClrTypeInfo(Type clrType, MessageParser parser, string[] propertyNames, string[] oneofNames, Type[] nestedEnums, GeneratedClrTypeInfo[] nestedTypes) + public GeneratedClrTypeInfo(Type clrType, MessageParser parser, string[] propertyNames, string[] oneofNames, Type[] nestedEnums, Extension[] extensions, GeneratedClrTypeInfo[] nestedTypes) { NestedTypes = nestedTypes ?? EmptyCodeInfo; NestedEnums = nestedEnums ?? ReflectionUtil.EmptyTypes; ClrType = clrType; + Extensions = extensions ?? EmptyExtensions; Parser = parser; PropertyNames = propertyNames ?? EmptyNames; OneofNames = oneofNames ?? EmptyNames; } + /// + /// Creates a GeneratedClrTypeInfo for a message descriptor, with nested types, nested enums, the CLR type, property names and oneof names. + /// Each array parameter may be null, to indicate a lack of values. + /// The parameter order is designed to make it feasible to format the generated code readably. + /// + public GeneratedClrTypeInfo(Type clrType, MessageParser parser, string[] propertyNames, string[] oneofNames, Type[] nestedEnums, GeneratedClrTypeInfo[] nestedTypes) + : this(clrType, parser, propertyNames, oneofNames, nestedEnums, null, nestedTypes) + { + } + + /// + /// Creates a GeneratedClrTypeInfo for a file descriptor, with only types, enums, and extensions. + /// + public GeneratedClrTypeInfo(Type[] nestedEnums, Extension[] extensions, GeneratedClrTypeInfo[] nestedTypes) + : this(null, null, null, null, nestedEnums, extensions, nestedTypes) + { + } + /// /// Creates a GeneratedClrTypeInfo for a file descriptor, with only types and enums. /// diff --git a/csharp/src/Google.Protobuf/Reflection/IDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/IDescriptor.cs index 318d58c968..1c1ef387da 100644 --- a/csharp/src/Google.Protobuf/Reflection/IDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/IDescriptor.cs @@ -51,5 +51,5 @@ namespace Google.Protobuf.Reflection /// Returns the descriptor for the .proto file that this entity is part of. /// FileDescriptor File { get; } - } + } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs index cfe56fde67..8f7ef3c67c 100644 --- a/csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs @@ -51,6 +51,11 @@ namespace Google.Protobuf.Reflection /// void Clear(IMessage message); + /// + /// Indicates whether the field in the specified message is set. For proto3 fields, this throws an + /// + bool HasValue(IMessage message); + /// /// Fetches the field value. For repeated values, this will be an /// implementation. For map values, this will be an diff --git a/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs index 9ed7f8c4df..56e3dddc94 100644 --- a/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs @@ -51,6 +51,11 @@ namespace Google.Protobuf.Reflection list.Clear(); } + public override bool HasValue(IMessage message) + { + throw new InvalidOperationException("HasValue is not implemented for map fields"); + } + public override void SetValue(IMessage message, object value) { throw new InvalidOperationException("SetValue is not implemented for map fields"); diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index 86942acc02..15bfbfadd0 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -63,7 +63,7 @@ namespace Google.Protobuf.Reflection private readonly IList fieldsInDeclarationOrder; private readonly IList fieldsInNumberOrder; private readonly IDictionary jsonFieldMap; - + internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, GeneratedClrTypeInfo generatedCodeInfo) : base(file, file.ComputeFullName(parent, proto.Name), typeIndex) { @@ -72,28 +72,28 @@ namespace Google.Protobuf.Reflection ClrType = generatedCodeInfo?.ClrType; ContainingType = parent; - // Note use of generatedCodeInfo. rather than generatedCodeInfo?. here... we don't expect - // to see any nested oneofs, types or enums in "not actually generated" code... we do - // expect fields though (for map entry messages). + // If generatedCodeInfo is null, we just won't generate an accessor for any fields. Oneofs = DescriptorUtil.ConvertAndMakeReadOnly( proto.OneofDecl, (oneof, index) => - new OneofDescriptor(oneof, file, this, index, generatedCodeInfo.OneofNames[index])); + new OneofDescriptor(oneof, file, this, index, generatedCodeInfo?.OneofNames[index])); NestedTypes = DescriptorUtil.ConvertAndMakeReadOnly( proto.NestedType, (type, index) => - new MessageDescriptor(type, file, this, index, generatedCodeInfo.NestedTypes[index])); + new MessageDescriptor(type, file, this, index, generatedCodeInfo?.NestedTypes[index])); EnumTypes = DescriptorUtil.ConvertAndMakeReadOnly( proto.EnumType, (type, index) => - new EnumDescriptor(type, file, this, index, generatedCodeInfo.NestedEnums[index])); + new EnumDescriptor(type, file, this, index, generatedCodeInfo?.NestedEnums[index])); + + Extensions = new ExtensionCollection(this, generatedCodeInfo?.Extensions); fieldsInDeclarationOrder = DescriptorUtil.ConvertAndMakeReadOnly( proto.Field, (field, index) => - new FieldDescriptor(field, file, this, index, generatedCodeInfo?.PropertyNames[index])); + new FieldDescriptor(field, file, this, index, generatedCodeInfo?.PropertyNames[index], null)); fieldsInNumberOrder = new ReadOnlyCollection(fieldsInDeclarationOrder.OrderBy(field => field.FieldNumber).ToArray()); // TODO: Use field => field.Proto.JsonName when we're confident it's appropriate. (And then use it in the formatter, too.) jsonFieldMap = CreateJsonFieldMap(fieldsInNumberOrder); @@ -117,6 +117,21 @@ namespace Google.Protobuf.Reflection /// public override string Name => Proto.Name; + internal override IReadOnlyList GetNestedDescriptorListForField(int fieldNumber) + { + switch (fieldNumber) + { + case DescriptorProto.FieldFieldNumber: + return (IReadOnlyList) fieldsInDeclarationOrder; + case DescriptorProto.NestedTypeFieldNumber: + return (IReadOnlyList) NestedTypes; + case DescriptorProto.EnumTypeFieldNumber: + return (IReadOnlyList) EnumTypes; + default: + return null; + } + } + internal DescriptorProto Proto { get; } /// @@ -182,6 +197,11 @@ namespace Google.Protobuf.Reflection /// public FieldCollection Fields { get; } + /// + /// An unmodifiable list of extensions defined in this message's scrope + /// + public ExtensionCollection Extensions { get; } + /// /// An unmodifiable list of this message type's nested types. /// @@ -223,7 +243,27 @@ namespace Google.Protobuf.Reflection /// /// The (possibly empty) set of custom options for this message. /// - public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; + //[Obsolete("CustomOptions are obsolete. Use GetOption")] + public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + + /* // uncomment this in the full proto2 support PR + /// + /// Gets a single value enum option for this descriptor + /// + public T GetOption(Extension extension) + { + var value = Proto.Options.GetExtension(extension); + return value is IDeepCloneable clonable ? clonable.Clone() : value; + } + + /// + /// Gets a repeated value enum option for this descriptor + /// + public Collections.RepeatedField GetOption(RepeatedExtension extension) + { + return Proto.Options.GetExtension(extension).Clone(); + } + */ /// /// Looks up and cross-links all fields and nested types. @@ -244,6 +284,8 @@ namespace Google.Protobuf.Reflection { oneof.CrossLink(); } + + Extensions.CrossLink(); } /// diff --git a/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs index 19d7f8a092..1b37d9b172 100644 --- a/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs @@ -30,6 +30,9 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Collections; +using System; + namespace Google.Protobuf.Reflection { /// @@ -70,7 +73,27 @@ namespace Google.Protobuf.Reflection /// /// The (possibly empty) set of custom options for this method. /// - public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; + //[Obsolete("CustomOptions are obsolete. Use GetOption")] + public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + + /* // uncomment this in the full proto2 support PR + /// + /// Gets a single value enum option for this descriptor + /// + public T GetOption(Extension extension) + { + var value = Proto.Options.GetExtension(extension); + return value is IDeepCloneable clonable ? clonable.Clone() : value; + } + + /// + /// Gets a repeated value enum option for this descriptor + /// + public RepeatedField GetOption(RepeatedExtension extension) + { + return Proto.Options.GetExtension(extension).Clone(); + } + */ internal MethodDescriptor(MethodDescriptorProto proto, FileDescriptor file, ServiceDescriptor parent, int index) @@ -105,4 +128,5 @@ namespace Google.Protobuf.Reflection outputType = (MessageDescriptor) lookup; } } -} \ No newline at end of file +} + \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs b/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs index 9759621879..f4bf628a6f 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs @@ -45,7 +45,7 @@ namespace Google.Protobuf.Reflection private readonly Action clearDelegate; private OneofDescriptor descriptor; - internal OneofAccessor(PropertyInfo caseProperty, MethodInfo clearMethod, OneofDescriptor descriptor) + internal OneofAccessor(PropertyInfo caseProperty, MethodInfo clearMethod, OneofDescriptor descriptor) { if (!caseProperty.CanRead) { diff --git a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs index 5906c2e36d..9f3d805d05 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs @@ -30,8 +30,10 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using Google.Protobuf.Collections; using Google.Protobuf.Compatibility; namespace Google.Protobuf.Reflection @@ -85,6 +87,16 @@ namespace Google.Protobuf.Reflection /// Gets an accessor for reflective access to the values associated with the oneof /// in a particular message. /// + /// + /// + /// In descriptors for generated code, the value returned by this property will always be non-null. + /// + /// + /// In dynamically loaded descriptors, the value returned by this property will current be null; + /// if and when dynamic messages are supported, it will return a suitable accessor to work with + /// them. + /// + /// /// /// The accessor used for reflective access. /// @@ -93,7 +105,27 @@ namespace Google.Protobuf.Reflection /// /// The (possibly empty) set of custom options for this oneof. /// - public CustomOptions CustomOptions => proto.Options?.CustomOptions ?? CustomOptions.Empty; + //[Obsolete("CustomOptions are obsolete. Use GetOption")] + public CustomOptions CustomOptions => new CustomOptions(proto.Options._extensions?.ValuesByNumber); + + /* // uncomment this in the full proto2 support PR + /// + /// Gets a single value enum option for this descriptor + /// + public T GetOption(Extension extension) + { + var value = proto.Options.GetExtension(extension); + return value is IDeepCloneable clonable ? clonable.Clone() : value; + } + + /// + /// Gets a repeated value enum option for this descriptor + /// + public RepeatedField GetOption(RepeatedExtension extension) + { + return proto.Options.GetExtension(extension).Clone(); + } + */ internal void CrossLink() { @@ -110,6 +142,12 @@ namespace Google.Protobuf.Reflection private OneofAccessor CreateAccessor(string clrName) { + // We won't have a CLR name if this is from a dynamically-loaded FileDescriptor. + // TODO: Support dynamic messages. + if (clrName == null) + { + return null; + } var caseProperty = containingType.ClrType.GetProperty(clrName + "Case"); if (caseProperty == null) { diff --git a/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs b/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs index 07d0fd99af..63f5228f3d 100644 --- a/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs +++ b/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs @@ -60,6 +60,6 @@ namespace Google.Protobuf.Reflection Name = ProtoPreconditions.CheckNotNull(name, nameof(name)); PreferredAlias = true; } - + } } diff --git a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs index feaeba0ee7..ca9c808343 100644 --- a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs +++ b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs @@ -112,6 +112,16 @@ namespace Google.Protobuf.Reflection internal static Action CreateActionIMessage(MethodInfo method) => GetReflectionHelper(method.DeclaringType, typeof(object)).CreateActionIMessage(method); + internal static Func CreateFuncIMessageBool(MethodInfo method) => + GetReflectionHelper(method.DeclaringType, method.ReturnType).CreateFuncIMessageBool(method); + + /// + /// Creates a delegate which will execute the given method after casting the first argument to + /// the type that declares the method, and the second argument to the first parameter type of the method. + /// + internal static IExtensionReflectionHelper CreateExtensionHelper(Extension extension) => + (IExtensionReflectionHelper)Activator.CreateInstance(typeof(ExtensionReflectionHelper<,>).MakeGenericType(extension.TargetType, extension.GetType().GenericTypeArguments[1])); + /// /// Creates a reflection helper for the given type arguments. Currently these are created on demand /// rather than cached; this will be "busy" when initially loading a message's descriptor, but after that @@ -129,6 +139,15 @@ namespace Google.Protobuf.Reflection Action CreateActionIMessage(MethodInfo method); Func CreateFuncIMessageObject(MethodInfo method); Action CreateActionIMessageObject(MethodInfo method); + Func CreateFuncIMessageBool(MethodInfo method); + } + + internal interface IExtensionReflectionHelper + { + object GetExtension(IMessage message); + void SetExtension(IMessage message, object value); + bool HasExtension(IMessage message); + void ClearExtension(IMessage message); } private class ReflectionHelper : IReflectionHelper @@ -170,6 +189,115 @@ namespace Google.Protobuf.Reflection var del = (Action) method.CreateDelegate(typeof(Action)); return (message, arg) => del((T1) message, (T2) arg); } + + public Func CreateFuncIMessageBool(MethodInfo method) + { + var del = (Func)method.CreateDelegate(typeof(Func)); + return message => del((T1)message); + } + } + + private class ExtensionReflectionHelper : IExtensionReflectionHelper + where T1 : IExtendableMessage + { + private readonly Extension extension; + + public ExtensionReflectionHelper(Extension extension) + { + this.extension = extension; + } + + public object GetExtension(IMessage message) + { + if (!(message is T1)) + { + throw new InvalidCastException("Cannot access extension on message that isn't IExtensionMessage"); + } + + T1 extensionMessage = (T1)message; + + if (extension is Extension) + { + return extensionMessage.GetExtension(extension as Extension); + } + else if (extension is RepeatedExtension) + { + return extensionMessage.GetExtension(extension as RepeatedExtension); + } + else + { + throw new InvalidCastException("The provided extension is not a valid extension identifier type"); + } + } + + public bool HasExtension(IMessage message) + { + if (!(message is T1)) + { + throw new InvalidCastException("Cannot access extension on message that isn't IExtensionMessage"); + } + + T1 extensionMessage = (T1)message; + + if (extension is Extension) + { + return extensionMessage.HasExtension(extension as Extension); + } + else if (extension is RepeatedExtension) + { + throw new InvalidOperationException("HasValue is not implemented for repeated extensions"); + } + else + { + throw new InvalidCastException("The provided extension is not a valid extension identifier type"); + } + } + + public void SetExtension(IMessage message, object value) + { + if (!(message is T1)) + { + throw new InvalidCastException("Cannot access extension on message that isn't IExtensionMessage"); + } + + T1 extensionMessage = (T1)message; + + if (extension is Extension) + { + extensionMessage.SetExtension(extension as Extension, (T3)value); + } + else if (extension is RepeatedExtension) + { + throw new InvalidOperationException("SetValue is not implemented for repeated extensions"); + } + else + { + throw new InvalidCastException("The provided extension is not a valid extension identifier type"); + } + } + + public void ClearExtension(IMessage message) + { + if (!(message is T1)) + { + throw new InvalidCastException("Cannot access extension on message that isn't IExtensionMessage"); + } + + T1 extensionMessage = (T1)message; + + if (extension is Extension) + { + extensionMessage.ClearExtension(extension as Extension); + } + else if (extension is RepeatedExtension) + { + extensionMessage.GetExtension(extension as RepeatedExtension).Clear(); + } + else + { + throw new InvalidCastException("The provided extension is not a valid extension identifier type"); + } + } } // Runtime compatibility checking code - see ReflectionHelper.CreateFuncIMessageInt32 for diff --git a/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs index bd40847092..afb4a693fc 100644 --- a/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs @@ -51,6 +51,11 @@ namespace Google.Protobuf.Reflection list.Clear(); } + public override bool HasValue(IMessage message) + { + throw new InvalidOperationException("HasValue is not implemented for repeated fields"); + } + public override void SetValue(IMessage message, object value) { throw new InvalidOperationException("SetValue is not implemented for repeated fields"); diff --git a/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs index fe5c072c8e..54f4df7298 100644 --- a/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs @@ -30,8 +30,10 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Collections; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; namespace Google.Protobuf.Reflection { @@ -58,6 +60,17 @@ namespace Google.Protobuf.Reflection /// public override string Name { get { return proto.Name; } } + internal override IReadOnlyList GetNestedDescriptorListForField(int fieldNumber) + { + switch (fieldNumber) + { + case ServiceDescriptorProto.MethodFieldNumber: + return (IReadOnlyList) methods; + default: + return null; + } + } + internal ServiceDescriptorProto Proto { get { return proto; } } /// @@ -81,7 +94,27 @@ namespace Google.Protobuf.Reflection /// /// The (possibly empty) set of custom options for this service. /// - public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; + //[Obsolete("CustomOptions are obsolete. Use GetOption")] + public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + + /* // uncomment this in the full proto2 support PR + /// + /// Gets a single value enum option for this descriptor + /// + public T GetOption(Extension extension) + { + var value = Proto.Options.GetExtension(extension); + return value is IDeepCloneable clonable ? clonable.Clone() : value; + } + + /// + /// Gets a repeated value enum option for this descriptor + /// + public RepeatedField GetOption(RepeatedExtension extension) + { + return Proto.Options.GetExtension(extension).Clone(); + } + */ internal void CrossLink() { @@ -91,4 +124,5 @@ namespace Google.Protobuf.Reflection } } } -} \ No newline at end of file +} + \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs index bbac2173d4..dfc5b6263c 100644 --- a/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs @@ -48,6 +48,7 @@ namespace Google.Protobuf.Reflection private readonly Action setValueDelegate; private readonly Action clearDelegate; + private readonly Func hasDelegate; internal SingleFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor) { @@ -56,16 +57,34 @@ namespace Google.Protobuf.Reflection throw new ArgumentException("Not all required properties/methods available"); } setValueDelegate = ReflectionUtil.CreateActionIMessageObject(property.GetSetMethod()); + if (descriptor.File.Proto.Syntax == "proto2") + { + MethodInfo hasMethod = property.DeclaringType.GetRuntimeProperty("Has" + property.Name).GetMethod; + if (hasMethod == null) { + throw new ArgumentException("Not all required properties/methods are available"); + } + hasDelegate = ReflectionUtil.CreateFuncIMessageBool(hasMethod); + MethodInfo clearMethod = property.DeclaringType.GetRuntimeMethod("Clear" + property.Name, ReflectionUtil.EmptyTypes); + if (clearMethod == null) { + throw new ArgumentException("Not all required properties/methods are available"); + } + clearDelegate = ReflectionUtil.CreateActionIMessage(clearMethod); + } + else + { + hasDelegate = message => { + throw new InvalidOperationException("HasValue is not implemented for proto3 fields"); + }; + var clrType = property.PropertyType; - var clrType = property.PropertyType; - - // TODO: Validate that this is a reasonable single field? (Should be a value type, a message type, or string/ByteString.) - object defaultValue = - descriptor.FieldType == FieldType.Message ? null - : clrType == typeof(string) ? "" - : clrType == typeof(ByteString) ? ByteString.Empty - : Activator.CreateInstance(clrType); - clearDelegate = message => SetValue(message, defaultValue); + // TODO: Validate that this is a reasonable single field? (Should be a value type, a message type, or string/ByteString.) + object defaultValue = + descriptor.FieldType == FieldType.Message ? null + : clrType == typeof(string) ? "" + : clrType == typeof(ByteString) ? ByteString.Empty + : Activator.CreateInstance(clrType); + clearDelegate = message => SetValue(message, defaultValue); + } } public override void Clear(IMessage message) @@ -73,6 +92,11 @@ namespace Google.Protobuf.Reflection clearDelegate(message); } + public override bool HasValue(IMessage message) + { + return hasDelegate(message); + } + public override void SetValue(IMessage message, object value) { setValueDelegate(message, value); diff --git a/csharp/src/Google.Protobuf/UnknownField.cs b/csharp/src/Google.Protobuf/UnknownField.cs index 0d6eed6357..e3ce0e86ef 100644 --- a/csharp/src/Google.Protobuf/UnknownField.cs +++ b/csharp/src/Google.Protobuf/UnknownField.cs @@ -55,6 +55,7 @@ namespace Google.Protobuf private List fixed32List; private List fixed64List; private List lengthDelimitedList; + private List groupList; /// /// Creates a new UnknownField. @@ -77,7 +78,8 @@ namespace Google.Protobuf && Lists.Equals(varintList, otherField.varintList) && Lists.Equals(fixed32List, otherField.fixed32List) && Lists.Equals(fixed64List, otherField.fixed64List) - && Lists.Equals(lengthDelimitedList, otherField.lengthDelimitedList); + && Lists.Equals(lengthDelimitedList, otherField.lengthDelimitedList) + && Lists.Equals(groupList, otherField.groupList); } /// @@ -90,6 +92,7 @@ namespace Google.Protobuf hash = hash * 47 + Lists.GetHashCode(fixed32List); hash = hash * 47 + Lists.GetHashCode(fixed64List); hash = hash * 47 + Lists.GetHashCode(lengthDelimitedList); + hash = hash * 47 + Lists.GetHashCode(groupList); return hash; } @@ -133,6 +136,15 @@ namespace Google.Protobuf output.WriteBytes(value); } } + if (groupList != null) + { + foreach (UnknownFieldSet value in groupList) + { + output.WriteTag(fieldNumber, WireFormat.WireType.StartGroup); + value.WriteTo(output); + output.WriteTag(fieldNumber, WireFormat.WireType.EndGroup); + } + } } /// @@ -159,7 +171,7 @@ namespace Google.Protobuf { result += CodedOutputStream.ComputeTagSize(fieldNumber) * fixed64List.Count; result += CodedOutputStream.ComputeFixed64Size(1) * fixed64List.Count; - } + } if (lengthDelimitedList != null) { result += CodedOutputStream.ComputeTagSize(fieldNumber) * lengthDelimitedList.Count; @@ -168,6 +180,14 @@ namespace Google.Protobuf result += CodedOutputStream.ComputeBytesSize(value); } } + if (groupList != null) + { + result += CodedOutputStream.ComputeTagSize(fieldNumber) * 2 * groupList.Count; + foreach (UnknownFieldSet value in groupList) + { + result += value.CalculateSize(); + } + } return result; } @@ -182,6 +202,7 @@ namespace Google.Protobuf fixed32List = AddAll(fixed32List, other.fixed32List); fixed64List = AddAll(fixed64List, other.fixed64List); lengthDelimitedList = AddAll(lengthDelimitedList, other.lengthDelimitedList); + groupList = AddAll(groupList, other.groupList); return this; } @@ -245,6 +266,12 @@ namespace Google.Protobuf return this; } + internal UnknownField AddGroup(UnknownFieldSet value) + { + groupList = Add(groupList, value); + return this; + } + /// /// Adds to the , creating /// a new list if is null. The list is returned - either diff --git a/csharp/src/Google.Protobuf/UnknownFieldSet.cs b/csharp/src/Google.Protobuf/UnknownFieldSet.cs index 6404c3c08f..d136cf1e65 100644 --- a/csharp/src/Google.Protobuf/UnknownFieldSet.cs +++ b/csharp/src/Google.Protobuf/UnknownFieldSet.cs @@ -183,7 +183,7 @@ namespace Google.Protobuf /// /// The coded input stream containing the field /// false if the tag is an "end group" tag, true otherwise - private void MergeFieldFrom(CodedInputStream input) + private bool MergeFieldFrom(CodedInputStream input) { uint tag = input.LastTag; int number = WireFormat.GetTagFieldNumber(tag); @@ -193,37 +193,43 @@ namespace Google.Protobuf { ulong uint64 = input.ReadUInt64(); GetOrAddField(number).AddVarint(uint64); - return; + return true; } case WireFormat.WireType.Fixed32: { uint uint32 = input.ReadFixed32(); GetOrAddField(number).AddFixed32(uint32); - return; + return true; } case WireFormat.WireType.Fixed64: { ulong uint64 = input.ReadFixed64(); GetOrAddField(number).AddFixed64(uint64); - return; + return true; } case WireFormat.WireType.LengthDelimited: { ByteString bytes = input.ReadBytes(); GetOrAddField(number).AddLengthDelimited(bytes); - return; + return true; } case WireFormat.WireType.StartGroup: { - input.SkipGroup(tag); - return; + uint endTag = WireFormat.MakeTag(number, WireFormat.WireType.EndGroup); + UnknownFieldSet set = new UnknownFieldSet(); + while (input.ReadTag() != endTag) + { + set.MergeFieldFrom(input); + } + GetOrAddField(number).AddGroup(set); + return true; } case WireFormat.WireType.EndGroup: { - throw new InvalidProtocolBufferException("Merge an unknown field of end-group tag, indicating that the corresponding start-group was missing."); + return false; } default: - throw new InvalidOperationException("Wire Type is invalid."); + throw InvalidProtocolBufferException.InvalidWireType(); } } @@ -248,9 +254,12 @@ namespace Google.Protobuf { unknownFields = new UnknownFieldSet(); } - unknownFields.MergeFieldFrom(input); + if (!unknownFields.MergeFieldFrom(input)) + { + throw new InvalidProtocolBufferException("Merge an unknown field of end-group tag, indicating that the corresponding start-group was missing."); // match the old code-gen + } return unknownFields; - } + } /// /// Merges the fields from into this set. diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Any.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Any.cs index 378b61d4ba..09e0e29f0d 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Any.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Any.cs @@ -31,8 +31,8 @@ namespace Google.Protobuf.WellKnownTypes { "dWYuV2VsbEtub3duVHlwZXNiBnByb3RvMw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Any), global::Google.Protobuf.WellKnownTypes.Any.Parser, new[]{ "TypeUrl", "Value" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Any), global::Google.Protobuf.WellKnownTypes.Any.Parser, new[]{ "TypeUrl", "Value" }, null, null, null, null) })); } #endregion @@ -159,7 +159,8 @@ namespace Google.Protobuf.WellKnownTypes { private string typeUrl_ = ""; /// /// A URL/resource name that uniquely identifies the type of the serialized - /// protocol buffer message. The last segment of the URL's path must represent + /// protocol buffer message. This string must contain at least + /// one "/" character. The last segment of the URL's path must represent /// the fully qualified name of the type (as in /// `path/google.protobuf.Duration`). The name should be in a canonical form /// (e.g., leading "." is not accepted). diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs index fca689dc08..49f259982a 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs @@ -49,7 +49,7 @@ namespace Google.Protobuf.WellKnownTypes /// /// /// - /// This is always just the last part of the URL, after the final slash. No validation of + /// This is always just the last part of the URL, after the final slash. No validation of /// anything before the trailing slash is performed. If the type URL does not include a slash, /// an empty string is returned rather than an exception being thrown; this won't match any types, /// and the calling code is probably in a better position to give a meaningful error. @@ -67,6 +67,17 @@ namespace Google.Protobuf.WellKnownTypes return lastSlash == -1 ? "" : typeUrl.Substring(lastSlash + 1); } + /// + /// Returns a bool indictating whether this Any message is of the target message type + /// + /// The descriptor of the message type + /// true if the type name matches the descriptor's full name or false otherwise + public bool Is(MessageDescriptor descriptor) + { + ProtoPreconditions.CheckNotNull(descriptor, nameof(descriptor)); + return GetTypeName(TypeUrl) == descriptor.FullName; + } + /// /// Unpacks the content of this Any message into the target message type, /// which must match the type URL within this Any message. diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs index e4a4a36544..06a9905919 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs @@ -43,10 +43,10 @@ namespace Google.Protobuf.WellKnownTypes { "cm90b2J1Zi5XZWxsS25vd25UeXBlc2IGcHJvdG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.SourceContextReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.TypeReflection.Descriptor, }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Api), global::Google.Protobuf.WellKnownTypes.Api.Parser, new[]{ "Name", "Methods", "Options", "Version", "SourceContext", "Mixins", "Syntax" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Method), global::Google.Protobuf.WellKnownTypes.Method.Parser, new[]{ "Name", "RequestTypeUrl", "RequestStreaming", "ResponseTypeUrl", "ResponseStreaming", "Options", "Syntax" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Mixin), global::Google.Protobuf.WellKnownTypes.Mixin.Parser, new[]{ "Name", "Root" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Api), global::Google.Protobuf.WellKnownTypes.Api.Parser, new[]{ "Name", "Methods", "Options", "Version", "SourceContext", "Mixins", "Syntax" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Method), global::Google.Protobuf.WellKnownTypes.Method.Parser, new[]{ "Name", "RequestTypeUrl", "RequestStreaming", "ResponseTypeUrl", "ResponseStreaming", "Options", "Syntax" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Mixin), global::Google.Protobuf.WellKnownTypes.Mixin.Parser, new[]{ "Name", "Root" }, null, null, null, null) })); } #endregion @@ -207,7 +207,7 @@ namespace Google.Protobuf.WellKnownTypes { /// Field number for the "syntax" field. public const int SyntaxFieldNumber = 7; - private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = 0; + private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = global::Google.Protobuf.WellKnownTypes.Syntax.Proto2; /// /// The source syntax of the service. /// @@ -251,7 +251,7 @@ namespace Google.Protobuf.WellKnownTypes { if (Version.Length != 0) hash ^= Version.GetHashCode(); if (sourceContext_ != null) hash ^= SourceContext.GetHashCode(); hash ^= mixins_.GetHashCode(); - if (Syntax != 0) hash ^= Syntax.GetHashCode(); + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) hash ^= Syntax.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -280,7 +280,7 @@ namespace Google.Protobuf.WellKnownTypes { output.WriteMessage(SourceContext); } mixins_.WriteTo(output, _repeated_mixins_codec); - if (Syntax != 0) { + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { output.WriteRawTag(56); output.WriteEnum((int) Syntax); } @@ -304,7 +304,7 @@ namespace Google.Protobuf.WellKnownTypes { size += 1 + pb::CodedOutputStream.ComputeMessageSize(SourceContext); } size += mixins_.CalculateSize(_repeated_mixins_codec); - if (Syntax != 0) { + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Syntax); } if (_unknownFields != null) { @@ -328,12 +328,12 @@ namespace Google.Protobuf.WellKnownTypes { } if (other.sourceContext_ != null) { if (sourceContext_ == null) { - sourceContext_ = new global::Google.Protobuf.WellKnownTypes.SourceContext(); + SourceContext = new global::Google.Protobuf.WellKnownTypes.SourceContext(); } SourceContext.MergeFrom(other.SourceContext); } mixins_.Add(other.mixins_); - if (other.Syntax != 0) { + if (other.Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { Syntax = other.Syntax; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -365,9 +365,9 @@ namespace Google.Protobuf.WellKnownTypes { } case 42: { if (sourceContext_ == null) { - sourceContext_ = new global::Google.Protobuf.WellKnownTypes.SourceContext(); + SourceContext = new global::Google.Protobuf.WellKnownTypes.SourceContext(); } - input.ReadMessage(sourceContext_); + input.ReadMessage(SourceContext); break; } case 50: { @@ -375,7 +375,7 @@ namespace Google.Protobuf.WellKnownTypes { break; } case 56: { - syntax_ = (global::Google.Protobuf.WellKnownTypes.Syntax) input.ReadEnum(); + Syntax = (global::Google.Protobuf.WellKnownTypes.Syntax) input.ReadEnum(); break; } } @@ -512,7 +512,7 @@ namespace Google.Protobuf.WellKnownTypes { /// Field number for the "syntax" field. public const int SyntaxFieldNumber = 7; - private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = 0; + private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = global::Google.Protobuf.WellKnownTypes.Syntax.Proto2; /// /// The source syntax of this method. /// @@ -556,7 +556,7 @@ namespace Google.Protobuf.WellKnownTypes { if (ResponseTypeUrl.Length != 0) hash ^= ResponseTypeUrl.GetHashCode(); if (ResponseStreaming != false) hash ^= ResponseStreaming.GetHashCode(); hash ^= options_.GetHashCode(); - if (Syntax != 0) hash ^= Syntax.GetHashCode(); + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) hash ^= Syntax.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -591,7 +591,7 @@ namespace Google.Protobuf.WellKnownTypes { output.WriteBool(ResponseStreaming); } options_.WriteTo(output, _repeated_options_codec); - if (Syntax != 0) { + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { output.WriteRawTag(56); output.WriteEnum((int) Syntax); } @@ -619,7 +619,7 @@ namespace Google.Protobuf.WellKnownTypes { size += 1 + 1; } size += options_.CalculateSize(_repeated_options_codec); - if (Syntax != 0) { + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Syntax); } if (_unknownFields != null) { @@ -649,7 +649,7 @@ namespace Google.Protobuf.WellKnownTypes { ResponseStreaming = other.ResponseStreaming; } options_.Add(other.options_); - if (other.Syntax != 0) { + if (other.Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { Syntax = other.Syntax; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -688,7 +688,7 @@ namespace Google.Protobuf.WellKnownTypes { break; } case 56: { - syntax_ = (global::Google.Protobuf.WellKnownTypes.Syntax) input.ReadEnum(); + Syntax = (global::Google.Protobuf.WellKnownTypes.Syntax) input.ReadEnum(); break; } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Duration.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Duration.cs index 2858b532b6..7a6f50190e 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Duration.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Duration.cs @@ -32,8 +32,8 @@ namespace Google.Protobuf.WellKnownTypes { "bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Duration), global::Google.Protobuf.WellKnownTypes.Duration.Parser, new[]{ "Seconds", "Nanos" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Duration), global::Google.Protobuf.WellKnownTypes.Duration.Parser, new[]{ "Seconds", "Nanos" }, null, null, null, null) })); } #endregion diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs index f164bfd19d..9a468fc198 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs @@ -156,7 +156,7 @@ namespace Google.Protobuf.WellKnownTypes return Normalize(lhs.Seconds - rhs.Seconds, lhs.Nanos - rhs.Nanos); } } - + /// /// Creates a duration with the normalized values from the given number of seconds and /// nanoseconds, conforming with the description in the proto file. diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Empty.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Empty.cs index 2113add9bc..fa435cd23e 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Empty.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Empty.cs @@ -31,8 +31,8 @@ namespace Google.Protobuf.WellKnownTypes { "dG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Empty), global::Google.Protobuf.WellKnownTypes.Empty.Parser, null, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Empty), global::Google.Protobuf.WellKnownTypes.Empty.Parser, null, null, null, null, null) })); } #endregion diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/FieldMask.cs b/csharp/src/Google.Protobuf/WellKnownTypes/FieldMask.cs index 6ad31a50ea..0bbf313971 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/FieldMask.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/FieldMask.cs @@ -32,8 +32,8 @@ namespace Google.Protobuf.WellKnownTypes { "cm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.FieldMask), global::Google.Protobuf.WellKnownTypes.FieldMask.Parser, new[]{ "Paths" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.FieldMask), global::Google.Protobuf.WellKnownTypes.FieldMask.Parser, new[]{ "Paths" }, null, null, null, null) })); } #endregion diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs index 4b0670f6fd..91d2ee9094 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/FieldMaskPartial.cs @@ -35,15 +35,18 @@ using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; +using Google.Protobuf.Reflection; namespace Google.Protobuf.WellKnownTypes { // Manually-written partial class for the FieldMask well-known type. public partial class FieldMask : ICustomDiagnosticMessage { + private const char FIELD_PATH_SEPARATOR = ','; + private const char FIELD_SEPARATOR_REGEX = '.'; + /// - /// Converts a timestamp specified in seconds/nanoseconds to a string. + /// Converts a field mask specified by paths to a string. /// /// /// If the value is a normalized duration in the range described in field_mask.proto, @@ -55,7 +58,7 @@ namespace Google.Protobuf.WellKnownTypes /// The represented field mask is invalid, and is false. internal static string ToJson(IList paths, bool diagnosticOnly) { - var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p)); + var firstInvalid = paths.FirstOrDefault(p => !IsPathValid(p)); if (firstInvalid == null) { var writer = new StringWriter(); @@ -84,11 +87,103 @@ namespace Google.Protobuf.WellKnownTypes } } + /// + /// Returns a string representation of this for diagnostic purposes. + /// + /// + /// Normally the returned value will be a JSON string value (including leading and trailing quotes) but + /// when the value is non-normalized or out of range, a JSON object representation will be returned + /// instead, including a warning. This is to avoid exceptions being thrown when trying to + /// diagnose problems - the regular JSON formatter will still throw an exception for non-normalized + /// values. + /// + /// A string representation of this value. + public string ToDiagnosticString() + { + return ToJson(Paths, true); + } + + /// + /// Parses from a string to a FieldMask. + /// + public static FieldMask FromString(string value) + { + return FromStringEnumerable(new List(value.Split(FIELD_PATH_SEPARATOR))); + } + + /// + /// Parses from a string to a FieldMask and validates all field paths. + /// + /// The type to validate the field paths against. + public static FieldMask FromString(string value) where T : IMessage + { + return FromStringEnumerable(new List(value.Split(FIELD_PATH_SEPARATOR))); + } + + /// + /// Constructs a FieldMask for a list of field paths in a certain type. + /// + /// The type to validate the field paths against. + public static FieldMask FromStringEnumerable(IEnumerable paths) where T : IMessage + { + var mask = new FieldMask(); + foreach (var path in paths) + { + if (path.Length == 0) + { + // Ignore empty field paths. + continue; + } + + if (typeof(T) != typeof(Empty) + && !IsValid(path)) + { + throw new InvalidProtocolBufferException(path + " is not a valid path for " + typeof(T)); + } + + mask.Paths.Add(path); + } + + return mask; + } + + /// + /// Constructs a FieldMask from the passed field numbers. + /// + /// The type to validate the field paths against. + public static FieldMask FromFieldNumbers(params int[] fieldNumbers) where T : IMessage + { + return FromFieldNumbers((IEnumerable)fieldNumbers); + } + + /// + /// Constructs a FieldMask from the passed field numbers. + /// + /// The type to validate the field paths against. + public static FieldMask FromFieldNumbers(IEnumerable fieldNumbers) where T : IMessage + { + var descriptor = Activator.CreateInstance().Descriptor; + + var mask = new FieldMask(); + foreach (var fieldNumber in fieldNumbers) + { + var field = descriptor.FindFieldByNumber(fieldNumber); + if (field == null) + { + throw new ArgumentNullException($"{fieldNumber} is not a valid field number for {descriptor.Name}"); + } + + mask.Paths.Add(field.Name); + } + + return mask; + } + /// /// Checks whether the given path is valid for a field mask. /// /// true if the path is valid; false otherwise - private static bool ValidatePath(string input) + private static bool IsPathValid(string input) { for (int i = 0; i < input.Length; i++) { @@ -110,19 +205,166 @@ namespace Google.Protobuf.WellKnownTypes } /// - /// Returns a string representation of this for diagnostic purposes. + /// Checks whether paths in a given fields mask are valid. /// - /// - /// Normally the returned value will be a JSON string value (including leading and trailing quotes) but - /// when the value is non-normalized or out of range, a JSON object representation will be returned - /// instead, including a warning. This is to avoid exceptions being thrown when trying to - /// diagnose problems - the regular JSON formatter will still throw an exception for non-normalized - /// values. - /// - /// A string representation of this value. - public string ToDiagnosticString() + /// The type to validate the field paths against. + public static bool IsValid(FieldMask fieldMask) where T : IMessage { - return ToJson(Paths, true); + var descriptor = Activator.CreateInstance().Descriptor; + + return IsValid(descriptor, fieldMask); + } + + /// + /// Checks whether paths in a given fields mask are valid. + /// + public static bool IsValid(MessageDescriptor descriptor, FieldMask fieldMask) + { + foreach (var path in fieldMask.Paths) + { + if (!IsValid(descriptor, path)) + { + return false; + } + } + + return true; + } + + /// + /// Checks whether a given field path is valid. + /// + /// The type to validate the field paths against. + public static bool IsValid(string path) where T : IMessage + { + var descriptor = Activator.CreateInstance().Descriptor; + + return IsValid(descriptor, path); + } + + /// + /// Checks whether paths in a given fields mask are valid. + /// + public static bool IsValid(MessageDescriptor descriptor, string path) + { + var parts = path.Split(FIELD_SEPARATOR_REGEX); + if (parts.Length == 0) + { + return false; + } + + foreach (var name in parts) + { + var field = descriptor?.FindFieldByName(name); + if (field == null) + { + return false; + } + + if (!field.IsRepeated + && field.FieldType == FieldType.Message) + { + descriptor = field.MessageType; + } + else + { + descriptor = null; + } + } + + return true; + } + + /// + /// Converts this FieldMask to its canonical form. In the canonical form of a + /// FieldMask, all field paths are sorted alphabetically and redundant field + /// paths are removed. + /// + public FieldMask Normalize() + { + return new FieldMaskTree(this).ToFieldMask(); + } + + /// + /// Creates a union of two or more FieldMasks. + /// + public FieldMask Union(params FieldMask[] otherMasks) + { + var maskTree = new FieldMaskTree(this); + foreach (var mask in otherMasks) + { + maskTree.MergeFromFieldMask(mask); + } + + return maskTree.ToFieldMask(); + } + + /// + /// Calculates the intersection of two FieldMasks. + /// + public FieldMask Intersection(FieldMask additionalMask) + { + var tree = new FieldMaskTree(this); + var result = new FieldMaskTree(); + foreach (var path in additionalMask.Paths) + { + tree.IntersectFieldPath(path, result); + } + + return result.ToFieldMask(); + } + + /// + /// Merges fields specified by this FieldMask from one message to another with the + /// specified merge options. + /// + public void Merge(IMessage source, IMessage destination, MergeOptions options) + { + new FieldMaskTree(this).Merge(source, destination, options); + } + + /// + /// Merges fields specified by this FieldMask from one message to another. + /// + public void Merge(IMessage source, IMessage destination) + { + Merge(source, destination, new MergeOptions()); + } + + /// + /// Options to customize merging behavior. + /// + public sealed class MergeOptions + { + /// + /// Whether to replace message fields(i.e., discard existing content in + /// destination message fields) when merging. + /// Default behavior is to merge the source message field into the + /// destination message field. + /// + public bool ReplaceMessageFields { get; set; } = false; + + /// + /// Whether to replace repeated fields (i.e., discard existing content in + /// destination repeated fields) when merging. + /// Default behavior is to append elements from source repeated field to the + /// destination repeated field. + /// + public bool ReplaceRepeatedFields { get; set; } = false; + + /// + /// Whether to replace primitive (non-repeated and non-message) fields in + /// destination message fields with the source primitive fields (i.e., if the + /// field is set in the source, the value is copied to the + /// destination; if the field is unset in the source, the field is cleared + /// from the destination) when merging. + /// + /// Default behavior is to always set the value of the source primitive + /// field to the destination primitive field, and if the source field is + /// unset, the default value of the source field is copied to the + /// destination. + /// + public bool ReplacePrimitiveFields { get; set; } = false; } } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs b/csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs index 124ddaa712..d7076196e0 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs @@ -32,8 +32,8 @@ namespace Google.Protobuf.WellKnownTypes { "YnVmLldlbGxLbm93blR5cGVzYgZwcm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.SourceContext), global::Google.Protobuf.WellKnownTypes.SourceContext.Parser, new[]{ "FileName" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.SourceContext), global::Google.Protobuf.WellKnownTypes.SourceContext.Parser, new[]{ "FileName" }, null, null, null, null) })); } #endregion diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs index 194b81e965..b1dbe239cc 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs @@ -41,10 +41,10 @@ namespace Google.Protobuf.WellKnownTypes { "cGVzYgZwcm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.WellKnownTypes.NullValue), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Struct), global::Google.Protobuf.WellKnownTypes.Struct.Parser, new[]{ "Fields" }, null, null, new pbr::GeneratedClrTypeInfo[] { null, }), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Value), global::Google.Protobuf.WellKnownTypes.Value.Parser, new[]{ "NullValue", "NumberValue", "StringValue", "BoolValue", "StructValue", "ListValue" }, new[]{ "Kind" }, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.ListValue), global::Google.Protobuf.WellKnownTypes.ListValue.Parser, new[]{ "Values" }, null, null, null) + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.WellKnownTypes.NullValue), }, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Struct), global::Google.Protobuf.WellKnownTypes.Struct.Parser, new[]{ "Fields" }, null, null, null, new pbr::GeneratedClrTypeInfo[] { null, }), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Value), global::Google.Protobuf.WellKnownTypes.Value.Parser, new[]{ "NullValue", "NumberValue", "StringValue", "BoolValue", "StructValue", "ListValue" }, new[]{ "Kind" }, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.ListValue), global::Google.Protobuf.WellKnownTypes.ListValue.Parser, new[]{ "Values" }, null, null, null, null) })); } #endregion @@ -114,7 +114,7 @@ namespace Google.Protobuf.WellKnownTypes { /// Field number for the "fields" field. public const int FieldsFieldNumber = 1; private static readonly pbc::MapField.Codec _map_fields_codec - = new pbc::MapField.Codec(pb::FieldCodec.ForString(10), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Value.Parser), 10); + = new pbc::MapField.Codec(pb::FieldCodec.ForString(10, ""), pb::FieldCodec.ForMessage(18, global::Google.Protobuf.WellKnownTypes.Value.Parser), 10); private readonly pbc::MapField fields_ = new pbc::MapField(); /// /// Unordered map of dynamically typed values. @@ -270,7 +270,7 @@ namespace Google.Protobuf.WellKnownTypes { /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public global::Google.Protobuf.WellKnownTypes.NullValue NullValue { - get { return kindCase_ == KindOneofCase.NullValue ? (global::Google.Protobuf.WellKnownTypes.NullValue) kind_ : 0; } + get { return kindCase_ == KindOneofCase.NullValue ? (global::Google.Protobuf.WellKnownTypes.NullValue) kind_ : global::Google.Protobuf.WellKnownTypes.NullValue.NullValue; } set { kind_ = value; kindCase_ = KindOneofCase.NullValue; diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Timestamp.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Timestamp.cs index ef752be7cb..12f4812af3 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Timestamp.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Timestamp.cs @@ -32,8 +32,8 @@ namespace Google.Protobuf.WellKnownTypes { "cHJvdG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Timestamp), global::Google.Protobuf.WellKnownTypes.Timestamp.Parser, new[]{ "Seconds", "Nanos" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Timestamp), global::Google.Protobuf.WellKnownTypes.Timestamp.Parser, new[]{ "Seconds", "Nanos" }, null, null, null, null) })); } #endregion @@ -41,17 +41,19 @@ namespace Google.Protobuf.WellKnownTypes { } #region Messages /// - /// A Timestamp represents a point in time independent of any time zone - /// or calendar, represented as seconds and fractions of seconds at - /// nanosecond resolution in UTC Epoch time. It is encoded using the - /// Proleptic Gregorian Calendar which extends the Gregorian calendar - /// backwards to year one. It is encoded assuming all minutes are 60 - /// seconds long, i.e. leap seconds are "smeared" so that no leap second - /// table is needed for interpretation. Range is from - /// 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. - /// By restricting to that range, we ensure that we can convert to - /// and from RFC 3339 date strings. - /// See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt). + /// A Timestamp represents a point in time independent of any time zone or local + /// calendar, encoded as a count of seconds and fractions of seconds at + /// nanosecond resolution. The count is relative to an epoch at UTC midnight on + /// January 1, 1970, in the proleptic Gregorian calendar which extends the + /// Gregorian calendar backwards to year one. + /// + /// All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap + /// second table is needed for interpretation, using a [24-hour linear + /// smear](https://developers.google.com/time/smear). + /// + /// The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By + /// restricting to that range, we ensure that we can convert to and from [RFC + /// 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings. /// /// # Examples /// @@ -111,12 +113,14 @@ namespace Google.Protobuf.WellKnownTypes { /// 01:30 UTC on January 15, 2017. /// /// In JavaScript, one can convert a Date object to this format using the - /// standard [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString] + /// standard + /// [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) /// method. In Python, a standard `datetime.datetime` object can be converted - /// to this format using [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) - /// with the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one - /// can use the Joda Time's [`ISODateTimeFormat.dateTime()`]( - /// http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime-- + /// to this format using + /// [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with + /// the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use + /// the Joda Time's [`ISODateTimeFormat.dateTime()`]( + /// http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D /// ) to obtain a formatter capable of generating timestamps in this format. /// public sealed partial class Timestamp : pb::IMessage { diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs index a92519745b..2f5172f1ca 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs @@ -308,7 +308,7 @@ namespace Google.Protobuf.WellKnownTypes /// true if the two timestamps refer to the same nanosecond public static bool operator ==(Timestamp a, Timestamp b) { - return ReferenceEquals(a, b) || (a is null ? (b is null ? true : false) : a.Equals(b)); + return ReferenceEquals(a, b) || (ReferenceEquals(a, null) ? (ReferenceEquals(b, null) ? true : false) : a.Equals(b)); } /// diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs index 3e2fe541f8..bfd4b8ec82 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs @@ -62,12 +62,12 @@ namespace Google.Protobuf.WellKnownTypes { "bEtub3duVHlwZXNiBnByb3RvMw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.AnyReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.SourceContextReflection.Descriptor, }, - new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.WellKnownTypes.Syntax), }, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Type), global::Google.Protobuf.WellKnownTypes.Type.Parser, new[]{ "Name", "Fields", "Oneofs", "Options", "SourceContext", "Syntax" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Field), global::Google.Protobuf.WellKnownTypes.Field.Parser, new[]{ "Kind", "Cardinality", "Number", "Name", "TypeUrl", "OneofIndex", "Packed", "Options", "JsonName", "DefaultValue" }, null, new[]{ typeof(global::Google.Protobuf.WellKnownTypes.Field.Types.Kind), typeof(global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality) }, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Enum), global::Google.Protobuf.WellKnownTypes.Enum.Parser, new[]{ "Name", "Enumvalue", "Options", "SourceContext", "Syntax" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.EnumValue), global::Google.Protobuf.WellKnownTypes.EnumValue.Parser, new[]{ "Name", "Number", "Options" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Option), global::Google.Protobuf.WellKnownTypes.Option.Parser, new[]{ "Name", "Value" }, null, null, null) + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Google.Protobuf.WellKnownTypes.Syntax), }, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Type), global::Google.Protobuf.WellKnownTypes.Type.Parser, new[]{ "Name", "Fields", "Oneofs", "Options", "SourceContext", "Syntax" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Field), global::Google.Protobuf.WellKnownTypes.Field.Parser, new[]{ "Kind", "Cardinality", "Number", "Name", "TypeUrl", "OneofIndex", "Packed", "Options", "JsonName", "DefaultValue" }, null, new[]{ typeof(global::Google.Protobuf.WellKnownTypes.Field.Types.Kind), typeof(global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality) }, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Enum), global::Google.Protobuf.WellKnownTypes.Enum.Parser, new[]{ "Name", "Enumvalue", "Options", "SourceContext", "Syntax" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.EnumValue), global::Google.Protobuf.WellKnownTypes.EnumValue.Parser, new[]{ "Name", "Number", "Options" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Option), global::Google.Protobuf.WellKnownTypes.Option.Parser, new[]{ "Name", "Value" }, null, null, null, null) })); } #endregion @@ -202,7 +202,7 @@ namespace Google.Protobuf.WellKnownTypes { /// Field number for the "syntax" field. public const int SyntaxFieldNumber = 6; - private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = 0; + private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = global::Google.Protobuf.WellKnownTypes.Syntax.Proto2; /// /// The source syntax. /// @@ -244,7 +244,7 @@ namespace Google.Protobuf.WellKnownTypes { hash ^= oneofs_.GetHashCode(); hash ^= options_.GetHashCode(); if (sourceContext_ != null) hash ^= SourceContext.GetHashCode(); - if (Syntax != 0) hash ^= Syntax.GetHashCode(); + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) hash ^= Syntax.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -269,7 +269,7 @@ namespace Google.Protobuf.WellKnownTypes { output.WriteRawTag(42); output.WriteMessage(SourceContext); } - if (Syntax != 0) { + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { output.WriteRawTag(48); output.WriteEnum((int) Syntax); } @@ -290,7 +290,7 @@ namespace Google.Protobuf.WellKnownTypes { if (sourceContext_ != null) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(SourceContext); } - if (Syntax != 0) { + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Syntax); } if (_unknownFields != null) { @@ -312,11 +312,11 @@ namespace Google.Protobuf.WellKnownTypes { options_.Add(other.options_); if (other.sourceContext_ != null) { if (sourceContext_ == null) { - sourceContext_ = new global::Google.Protobuf.WellKnownTypes.SourceContext(); + SourceContext = new global::Google.Protobuf.WellKnownTypes.SourceContext(); } SourceContext.MergeFrom(other.SourceContext); } - if (other.Syntax != 0) { + if (other.Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { Syntax = other.Syntax; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -348,13 +348,13 @@ namespace Google.Protobuf.WellKnownTypes { } case 42: { if (sourceContext_ == null) { - sourceContext_ = new global::Google.Protobuf.WellKnownTypes.SourceContext(); + SourceContext = new global::Google.Protobuf.WellKnownTypes.SourceContext(); } - input.ReadMessage(sourceContext_); + input.ReadMessage(SourceContext); break; } case 48: { - syntax_ = (global::Google.Protobuf.WellKnownTypes.Syntax) input.ReadEnum(); + Syntax = (global::Google.Protobuf.WellKnownTypes.Syntax) input.ReadEnum(); break; } } @@ -411,7 +411,7 @@ namespace Google.Protobuf.WellKnownTypes { /// Field number for the "kind" field. public const int KindFieldNumber = 1; - private global::Google.Protobuf.WellKnownTypes.Field.Types.Kind kind_ = 0; + private global::Google.Protobuf.WellKnownTypes.Field.Types.Kind kind_ = global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TypeUnknown; /// /// The field type. /// @@ -425,7 +425,7 @@ namespace Google.Protobuf.WellKnownTypes { /// Field number for the "cardinality" field. public const int CardinalityFieldNumber = 2; - private global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality cardinality_ = 0; + private global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality cardinality_ = global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.Unknown; /// /// The field cardinality. /// @@ -579,8 +579,8 @@ namespace Google.Protobuf.WellKnownTypes { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override int GetHashCode() { int hash = 1; - if (Kind != 0) hash ^= Kind.GetHashCode(); - if (Cardinality != 0) hash ^= Cardinality.GetHashCode(); + if (Kind != global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TypeUnknown) hash ^= Kind.GetHashCode(); + if (Cardinality != global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.Unknown) hash ^= Cardinality.GetHashCode(); if (Number != 0) hash ^= Number.GetHashCode(); if (Name.Length != 0) hash ^= Name.GetHashCode(); if (TypeUrl.Length != 0) hash ^= TypeUrl.GetHashCode(); @@ -602,11 +602,11 @@ namespace Google.Protobuf.WellKnownTypes { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public void WriteTo(pb::CodedOutputStream output) { - if (Kind != 0) { + if (Kind != global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TypeUnknown) { output.WriteRawTag(8); output.WriteEnum((int) Kind); } - if (Cardinality != 0) { + if (Cardinality != global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.Unknown) { output.WriteRawTag(16); output.WriteEnum((int) Cardinality); } @@ -647,10 +647,10 @@ namespace Google.Protobuf.WellKnownTypes { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int CalculateSize() { int size = 0; - if (Kind != 0) { + if (Kind != global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TypeUnknown) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Kind); } - if (Cardinality != 0) { + if (Cardinality != global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.Unknown) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Cardinality); } if (Number != 0) { @@ -686,10 +686,10 @@ namespace Google.Protobuf.WellKnownTypes { if (other == null) { return; } - if (other.Kind != 0) { + if (other.Kind != global::Google.Protobuf.WellKnownTypes.Field.Types.Kind.TypeUnknown) { Kind = other.Kind; } - if (other.Cardinality != 0) { + if (other.Cardinality != global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality.Unknown) { Cardinality = other.Cardinality; } if (other.Number != 0) { @@ -726,11 +726,11 @@ namespace Google.Protobuf.WellKnownTypes { _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); break; case 8: { - kind_ = (global::Google.Protobuf.WellKnownTypes.Field.Types.Kind) input.ReadEnum(); + Kind = (global::Google.Protobuf.WellKnownTypes.Field.Types.Kind) input.ReadEnum(); break; } case 16: { - cardinality_ = (global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality) input.ReadEnum(); + Cardinality = (global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality) input.ReadEnum(); break; } case 24: { @@ -979,7 +979,7 @@ namespace Google.Protobuf.WellKnownTypes { /// Field number for the "syntax" field. public const int SyntaxFieldNumber = 5; - private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = 0; + private global::Google.Protobuf.WellKnownTypes.Syntax syntax_ = global::Google.Protobuf.WellKnownTypes.Syntax.Proto2; /// /// The source syntax. /// @@ -1019,7 +1019,7 @@ namespace Google.Protobuf.WellKnownTypes { hash ^= enumvalue_.GetHashCode(); hash ^= options_.GetHashCode(); if (sourceContext_ != null) hash ^= SourceContext.GetHashCode(); - if (Syntax != 0) hash ^= Syntax.GetHashCode(); + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) hash ^= Syntax.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -1043,7 +1043,7 @@ namespace Google.Protobuf.WellKnownTypes { output.WriteRawTag(34); output.WriteMessage(SourceContext); } - if (Syntax != 0) { + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { output.WriteRawTag(40); output.WriteEnum((int) Syntax); } @@ -1063,7 +1063,7 @@ namespace Google.Protobuf.WellKnownTypes { if (sourceContext_ != null) { size += 1 + pb::CodedOutputStream.ComputeMessageSize(SourceContext); } - if (Syntax != 0) { + if (Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Syntax); } if (_unknownFields != null) { @@ -1084,11 +1084,11 @@ namespace Google.Protobuf.WellKnownTypes { options_.Add(other.options_); if (other.sourceContext_ != null) { if (sourceContext_ == null) { - sourceContext_ = new global::Google.Protobuf.WellKnownTypes.SourceContext(); + SourceContext = new global::Google.Protobuf.WellKnownTypes.SourceContext(); } SourceContext.MergeFrom(other.SourceContext); } - if (other.Syntax != 0) { + if (other.Syntax != global::Google.Protobuf.WellKnownTypes.Syntax.Proto2) { Syntax = other.Syntax; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); @@ -1116,13 +1116,13 @@ namespace Google.Protobuf.WellKnownTypes { } case 34: { if (sourceContext_ == null) { - sourceContext_ = new global::Google.Protobuf.WellKnownTypes.SourceContext(); + SourceContext = new global::Google.Protobuf.WellKnownTypes.SourceContext(); } - input.ReadMessage(sourceContext_); + input.ReadMessage(SourceContext); break; } case 40: { - syntax_ = (global::Google.Protobuf.WellKnownTypes.Syntax) input.ReadEnum(); + Syntax = (global::Google.Protobuf.WellKnownTypes.Syntax) input.ReadEnum(); break; } } @@ -1467,7 +1467,7 @@ namespace Google.Protobuf.WellKnownTypes { } if (other.value_ != null) { if (value_ == null) { - value_ = new global::Google.Protobuf.WellKnownTypes.Any(); + Value = new global::Google.Protobuf.WellKnownTypes.Any(); } Value.MergeFrom(other.Value); } @@ -1488,9 +1488,9 @@ namespace Google.Protobuf.WellKnownTypes { } case 18: { if (value_ == null) { - value_ = new global::Google.Protobuf.WellKnownTypes.Any(); + Value = new global::Google.Protobuf.WellKnownTypes.Any(); } - input.ReadMessage(value_); + input.ReadMessage(Value); break; } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs index 25a65aa72c..556af3c931 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs @@ -36,16 +36,16 @@ namespace Google.Protobuf.WellKnownTypes { "QqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IGcHJvdG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.DoubleValue), global::Google.Protobuf.WellKnownTypes.DoubleValue.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.FloatValue), global::Google.Protobuf.WellKnownTypes.FloatValue.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Int64Value), global::Google.Protobuf.WellKnownTypes.Int64Value.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.UInt64Value), global::Google.Protobuf.WellKnownTypes.UInt64Value.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Int32Value), global::Google.Protobuf.WellKnownTypes.Int32Value.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.UInt32Value), global::Google.Protobuf.WellKnownTypes.UInt32Value.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.BoolValue), global::Google.Protobuf.WellKnownTypes.BoolValue.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.StringValue), global::Google.Protobuf.WellKnownTypes.StringValue.Parser, new[]{ "Value" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.BytesValue), global::Google.Protobuf.WellKnownTypes.BytesValue.Parser, new[]{ "Value" }, null, null, null) + new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.DoubleValue), global::Google.Protobuf.WellKnownTypes.DoubleValue.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.FloatValue), global::Google.Protobuf.WellKnownTypes.FloatValue.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Int64Value), global::Google.Protobuf.WellKnownTypes.Int64Value.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.UInt64Value), global::Google.Protobuf.WellKnownTypes.UInt64Value.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Int32Value), global::Google.Protobuf.WellKnownTypes.Int32Value.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.UInt32Value), global::Google.Protobuf.WellKnownTypes.UInt32Value.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.BoolValue), global::Google.Protobuf.WellKnownTypes.BoolValue.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.StringValue), global::Google.Protobuf.WellKnownTypes.StringValue.Parser, new[]{ "Value" }, null, null, null, null), + new pbr::GeneratedClrTypeInfo(typeof(global::Google.Protobuf.WellKnownTypes.BytesValue), global::Google.Protobuf.WellKnownTypes.BytesValue.Parser, new[]{ "Value" }, null, null, null, null) })); } #endregion diff --git a/csharp/src/Google.Protobuf/WireFormat.cs b/csharp/src/Google.Protobuf/WireFormat.cs index faf1e715ef..68f0f4a1f5 100644 --- a/csharp/src/Google.Protobuf/WireFormat.cs +++ b/csharp/src/Google.Protobuf/WireFormat.cs @@ -61,11 +61,11 @@ namespace Google.Protobuf /// LengthDelimited = 2, /// - /// A "start group" value - not supported by this implementation. + /// A "start group" value /// StartGroup = 3, /// - /// An "end group" value - not supported by this implementation. + /// An "end group" value /// EndGroup = 4, /// @@ -73,7 +73,7 @@ namespace Google.Protobuf /// Fixed32 = 5 } - + private const int TagTypeBits = 3; private const uint TagTypeMask = (1 << TagTypeBits) - 1; @@ -99,6 +99,6 @@ namespace Google.Protobuf public static uint MakeTag(int fieldNumber, WireType wireType) { return (uint) (fieldNumber << TagTypeBits) | (uint) wireType; - } + } } } \ No newline at end of file diff --git a/docs/options.md b/docs/options.md index 529cce5dbc..dbc0998e3b 100644 --- a/docs/options.md +++ b/docs/options.md @@ -7,14 +7,14 @@ extension numbers. If you need an extension number for your custom option (see [custom options]( https://developers.google.com/protocol-buffers/docs/proto#customoptions)), -please [send us a pull request](https://github.com/google/protobuf/pulls) to -add an entry to this doc, or [create an issue](https://github.com/google/protobuf/issues) +please [send us a pull request](https://github.com/protocolbuffers/protobuf/pulls) to +add an entry to this doc, or [create an issue](https://github.com/protocolbuffers/protobuf/issues) with info about your project (name and website) so we can add an entry for you. ## Existing Registered Extensions 1. C# port of protocol buffers - * Website: http://github.com/jskeet/dotnet-protobufs + * Website: https://github.com/jskeet/protobuf-csharp-port * Extensions: 1000 1. Perl/XS port of protocol buffers @@ -50,7 +50,7 @@ with info about your project (name and website) so we can add an entry for you. * Extensions: 1008 1. Goby Underwater Autonomy Project - * Website: https://launchpad.net/goby + * Website: https://github.com/GobySoft/goby * Extensions: 1009 1. Nanopb @@ -62,7 +62,7 @@ with info about your project (name and website) so we can add an entry for you. * Extensions: 1011 1. Dynamic Compact Control Language - * Website: https://launchpad.net/dccl + * Website: http://github.com/GobySoft/dccl * Extensions: 1012 1. ScaleOut StateServer® Native C++ API @@ -188,3 +188,23 @@ with info about your project (name and website) so we can add an entry for you. 1. Netifi Proteus * Website: https://github.com/netifi-proteus * Extensions: 1057 + +1. CGSN Mooring Project + * Website: https://bitbucket.org/ooicgsn/cgsn-mooring + * Extensions: 1058 + +1. Container Storage Interface + * Website: https://github.com/container-storage-interface/spec + * Extensions: 1059-1069 + +1. TwirpQL Plugin + * Website: https://twirpql.dev + * Extensions: 1070 + +1. Protoc-gen-validate + * Website: https://github.com/envoyproxy/protoc-gen-validate + * Extensions: 1071 + +1. protokt + * Website: https://github.com/toasttab/protokt (Currently Private but will be open soon.) + * Extensions: 1072 diff --git a/docs/performance.md b/docs/performance.md index 3bca58afbc..8734c700c1 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -1,25 +1,25 @@ # Protobuf Performance -This benchmark result is tested on workstation with processor of Intel® Xeon® Processor E5-2630 and 32GB RAM +The following benchmark test results were produced on a workstation utilizing an Intel® Xeon® Processor E5-2630 with 32GB of RAM. -This table contains 3 languages' results: +This table contains the results of three separate languages: -* **C++** - For C++ there're 3 kinds of parsing ways: - * **new** - This is for using new operator for creating message instance. - * **new arena** - This is for using arena for creating new message instance. +* **C++** - For C++, there are three parsing methods: + * **new** - This is for using a new operator for creating a message instance. + * **new arena** - This is for using arena for creating a new message instance. * **reuse** - This is for reusing the same message instance for parsing. -* **Java** - For Java there're 3 kinds of parsing/Serialization ways: +* **Java** - For Java, there are three parsing/serialization methods: * **byte[]** - This is for parsing from a Byte Array. - * **ByteString** - This is for parsing from a + * **ByteString** - This is for parsing from a com.google.protobuf.ByteString. - * **InputStream** - This is for parsing from a InputStream -* **Python** - For Pythong there're 3 kinds of python protobuf for testing: - * **C++-genereated-code** - This is for using cpp generated code of the - proto file as dynamic linked library. - * **C++-reflection** - This is for using cpp reflection, which there's no - generated code, but still using cpp protobuf library as dynamic linked + * **InputStream** - This is for parsing from an InputStream. +* **Python** - For Python, there are three types of Python protobuf for testing: + * **C++-generated-code** - This is for using C++ generated code of the + proto file as a dynamic linked library. + * **C++-reflection** - This is for using C++ reflection, for which there's no + generated code, but still using C++ protobuf library as a dynamic linked library. - * **pure-Python** - This is for pure Python version, which don't link with - any cpp protobuf library. + * **pure-Python** - This is for the pure version of Python, which does not link with + any C++ protobuf library. ## Parsing performance @@ -301,4 +301,4 @@ This table contains 3 languages' results: -\* The cpp performance can be improved by using [tcmalloc](https://gperftools.github.io/gperftools/tcmalloc.html), please follow the (instruction)[https://github.com/google/protobuf/blob/master/benchmarks/README.md] to link with tcmalloc to get the faster result. +\* The cpp performance can be improved by using [tcmalloc](https://gperftools.github.io/gperftools/tcmalloc.html), please follow the (instruction)[https://github.com/protocolbuffers/protobuf/blob/master/benchmarks/README.md] to link with tcmalloc to get the faster result. diff --git a/docs/third_party.md b/docs/third_party.md index f710b51ac3..16f088cb72 100644 --- a/docs/third_party.md +++ b/docs/third_party.md @@ -19,7 +19,6 @@ These are projects we know about implementing Protocol Buffers for other program * C++: https://github.com/google/protobuf (Google-official implementation) * C/C++: http://spbc.sf.net/ * C#: http://code.google.com/p/protobuf-csharp-port -* C#: http://code.google.com/p/protosharp/ * C#: https://silentorbit.com/protobuf/ * C#/.NET/WCF/VB: http://code.google.com/p/protobuf-net/ * Clojure: http://github.com/ninjudd/clojure-protobuf @@ -36,11 +35,10 @@ These are projects we know about implementing Protocol Buffers for other program * Elixir: https://github.com/jeremyong/exprotoc * Elixir: https://github.com/tony612/protobuf-elixir * Elm: https://github.com/tiziano88/elm-protobuf -* Erlang: http://github.com/ngerakines/erlang_protobuffs/tree/master -* Erlang: http://piqi.org/ -* Erlang: https://code.google.com/p/protoc-gen-erl/ -* Erlang: https://github.com/basho/erlang_protobuffs * Erlang: https://github.com/tomas-abrahamsson/gpb +* Erlang: http://piqi.org/ +* Erlang: https://github.com/basho/erlang_protobuffs (no longer maintained, use gpb instead) +* Hacklang/HHVM: https://github.com/y3llowcake/proto-hack * GDScript: https://github.com/oniksan/godobuf (Godot v3 engine plugin) * Go: https://github.com/golang/protobuf (Google-official implementation) * Go: https://github.com/akunspy/gopbuf @@ -80,7 +78,7 @@ These are projects we know about implementing Protocol Buffers for other program * PHP: http://drslump.github.com/Protobuf-PHP * Prolog: http://www.swi-prolog.org/pldoc/package/protobufs.html * Python: https://github.com/google/protobuf (Google-official implementation) -* Python: http://eigenein.github.com/protobuf/ +* Python: https://github.com/eigenein/protobuf * R: http://cran.r-project.org/package=RProtoBuf * Ruby: http://code.google.com/p/ruby-protobuf/ * Ruby: http://github.com/mozy/ruby-protocol-buffers @@ -91,8 +89,10 @@ These are projects we know about implementing Protocol Buffers for other program * Scala: http://code.google.com/p/protobuf-scala * Scala: https://github.com/SandroGrzicic/ScalaBuff * Scala: https://scalapb.github.io +* Solidity: https://github.com/celer-network/pb3-gen-sol * Swift: https://github.com/alexeyxo/protobuf-swift * Swift: https://github.com/apple/swift-protobuf/ +* Typescript: https://github.com/y3llowcake/protoc-gen-ts * Vala: https://launchpad.net/protobuf-vala * Visual Basic: http://code.google.com/p/protobuf-net/ @@ -163,6 +163,7 @@ There are miscellaneous other things you may find useful as a Protocol Buffers d * [Multi-platform executable JAR and Java API for protoc](https://github.com/os72/protoc-jar) * [Python scripts to convert between Protocol Buffers and JSON](https://github.com/NextTuesday/py-pb-converters) * [Visual Studio Language Service support for Protocol Buffers](http://visualstudiogallery.msdn.microsoft.com/4bc0f38c-b058-4e05-ae38-155e053c19c5) +* [Visual Studio Code Support for Protocol Buffers](https://marketplace.visualstudio.com/items?itemName=zxh404.vscode-proto3) * [C++ library for serialization/de-serialization between Protocol Buffers and JSON.](https://github.com/yinqiwen/pbjson) * [ProtoBuf with Java EE7 Expression Language 3.0; pure Java ProtoBuf Parser and Builder.](https://github.com/protobufel/protobuf-el) * [Notepad++ Syntax Highlighting for .proto files](https://github.com/chai2010/notepadplus-protobuf) @@ -171,3 +172,5 @@ There are miscellaneous other things you may find useful as a Protocol Buffers d * [Make protoc plugins in NodeJS](https://github.com/konsumer/node-protoc-plugin) * [ProfaneDB - A Protocol Buffers database](https://profanedb.gitlab.io) * [Protocol Buffer property-based testing utility and example message generator (Python / Hypothesis)](https://github.com/CurataEng/hypothesis-protobuf) +* [Protolock - CLI utility to prevent backward-incompatible changes to .proto files](https://github.com/nilslice/protolock) +* [Optional GRPC - GRPC for testable microservices (Python)](https://github.com/mattpaletta/optional-grpc.git) diff --git a/examples/Makefile b/examples/Makefile index 1ff7fa7fc5..4ad605641d 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -5,6 +5,7 @@ all: cpp java python cpp: add_person_cpp list_people_cpp +dart: add_person_dart list_people_dart go: add_person_go list_people_go gotest: add_person_gotest list_people_gotest java: add_person_java list_people_java @@ -16,6 +17,8 @@ clean: rm -f protoc_middleman addressbook.pb.cc addressbook.pb.h addressbook_pb2.py com/example/tutorial/AddressBookProtos.java rm -f *.pyc rm -f protoc_middleman_go tutorial/*.pb.go add_person_go list_people_go + rm -f protoc_middleman_dart dart_tutorial/*.pb*.dart + rmdir dart_tutorial 2>/dev/null || true rmdir tutorial 2>/dev/null || true rmdir com/example/tutorial 2>/dev/null || true rmdir com/example 2>/dev/null || true @@ -30,6 +33,12 @@ protoc_middleman_go: addressbook.proto protoc $$PROTO_PATH --go_out=tutorial addressbook.proto @touch protoc_middleman_go +protoc_middleman_dart: addressbook.proto + mkdir -p dart_tutorial # make directory for the dart package + protoc -I ../src/:. --dart_out=dart_tutorial addressbook.proto ../src/google/protobuf/timestamp.proto + pub get + @touch protoc_middleman_dart + add_person_cpp: add_person.cc protoc_middleman pkg-config --cflags protobuf # fails if protobuf is not installed c++ add_person.cc addressbook.pb.cc -o add_person_cpp `pkg-config --cflags --libs protobuf` @@ -38,6 +47,10 @@ list_people_cpp: list_people.cc protoc_middleman pkg-config --cflags protobuf # fails if protobuf is not installed c++ list_people.cc addressbook.pb.cc -o list_people_cpp `pkg-config --cflags --libs protobuf` +add_person_dart: add_person.dart protoc_middleman_dart + +list_people_dart: list_people.dart protoc_middleman_dart + add_person_go: add_person.go protoc_middleman_go go build -o add_person_go add_person.go diff --git a/examples/README.md b/examples/README.md old mode 100644 new mode 100755 index 20f285cdd7..4bf7c17ccc --- a/examples/README.md +++ b/examples/README.md @@ -38,7 +38,7 @@ and the protobuf runtime for the language you want to build. You can simply run "make" to build the example for all languages (except for Go). However, since different language has different installation requirement, -it will likely fail. It's better to follow individual instrutions below to +it will likely fail. It's better to follow individual instructions below to build only the language you are interested in. ### C++ @@ -119,6 +119,26 @@ is created if it does not exist. To view the data, run: ./list_people_go addressbook.data -Observe that the C++, Python, and Java examples in this directory run in a +Observe that the C++, Python, Java, and Dart examples in this directory run in a similar way and can view/modify files created by the Go example and vice versa. + +### Dart + +First, follow the instructions in [../README.md](../README.md) to install the Protocol Buffer Compiler (protoc). + +Then, install the Dart Protocol Buffer plugin as described [here](https://github.com/dart-lang/dart-protoc-plugin#how-to-build-and-use). +Note, the executable `bin/protoc-gen-dart` must be in your `PATH` for `protoc` to find it. + +Build the Dart samples in this directory with `make dart`. + +To run the examples: + +```sh +$ dart add_person.dart addressbook.data +$ dart list_people.dart addressbook.data +``` + +The two programs take a protocol buffer encoded file as their parameter. +The first can be used to add a person to the file. The file is created +if it does not exist. The second displays the data in the file. diff --git a/examples/WORKSPACE b/examples/WORKSPACE index 153a5932e8..8b97e47f47 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -1,9 +1,11 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + # This com_google_protobuf repository is required for proto_library rule. # It provides the protocol compiler binary (i.e., protoc). http_archive( name = "com_google_protobuf", strip_prefix = "protobuf-master", - urls = ["https://github.com/google/protobuf/archive/master.zip"], + urls = ["https://github.com/protocolbuffers/protobuf/archive/master.zip"], ) # This com_google_protobuf_cc repository is required for cc_proto_library @@ -13,14 +15,14 @@ http_archive( http_archive( name = "com_google_protobuf_cc", strip_prefix = "protobuf-master", - urls = ["https://github.com/google/protobuf/archive/master.zip"], + urls = ["https://github.com/protocolbuffers/protobuf/archive/master.zip"], ) # Similar to com_google_protobuf_cc but for Java (i.e., java_proto_library). http_archive( name = "com_google_protobuf_java", strip_prefix = "protobuf-master", - urls = ["https://github.com/google/protobuf/archive/master.zip"], + urls = ["https://github.com/protocolbuffers/protobuf/archive/master.zip"], ) # Similar to com_google_protobuf_cc but for Java lite. If you are building @@ -29,7 +31,7 @@ http_archive( http_archive( name = "com_google_protobuf_javalite", strip_prefix = "protobuf-javalite", - urls = ["https://github.com/google/protobuf/archive/javalite.zip"], + urls = ["https://github.com/protocolbuffers/protobuf/archive/javalite.zip"], ) http_archive( @@ -39,5 +41,10 @@ http_archive( urls = ["https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz"], ) -load("@bazel_skylib//:lib.bzl", "versions") +load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") + +protobuf_deps() + +load("@bazel_skylib//lib:versions.bzl", "versions") + versions.check(minimum_bazel_version = "0.5.4") diff --git a/examples/add_person.dart b/examples/add_person.dart new file mode 100644 index 0000000000..bb38901a67 --- /dev/null +++ b/examples/add_person.dart @@ -0,0 +1,70 @@ +import 'dart:io'; + +import 'dart_tutorial/addressbook.pb.dart'; + +// This function fills in a Person message based on user input. +Person promptForAddress() { + Person person = Person(); + + print('Enter person ID: '); + String input = stdin.readLineSync(); + person.id = int.parse(input); + + print('Enter name'); + person.name = stdin.readLineSync(); + + print('Enter email address (blank for none) : '); + String email = stdin.readLineSync(); + if (email.isNotEmpty) { + person.email = email; + } + + while (true) { + print('Enter a phone number (or leave blank to finish): '); + String number = stdin.readLineSync(); + if (number.isEmpty) break; + + Person_PhoneNumber phoneNumber = Person_PhoneNumber(); + + phoneNumber.number = number; + print('Is this a mobile, home, or work phone? '); + + String type = stdin.readLineSync(); + switch (type) { + case 'mobile': + phoneNumber.type = Person_PhoneType.MOBILE; + break; + case 'home': + phoneNumber.type = Person_PhoneType.HOME; + break; + case 'work': + phoneNumber.type = Person_PhoneType.WORK; + break; + default: + print('Unknown phone type. Using default.'); + } + person.phones.add(phoneNumber); + } + + return person; +} + +// Reads the entire address book from a file, adds one person based +// on user input, then writes it back out to the same file. +main(List arguments) { + if (arguments.length != 1) { + print('Usage: add_person ADDRESS_BOOK_FILE'); + exit(-1); + } + + File file = File(arguments.first); + AddressBook addressBook; + if (!file.existsSync()) { + print('File not found. Creating new file.'); + addressBook = AddressBook(); + } else { + addressBook = AddressBook.fromBuffer(file.readAsBytesSync()); + } + addressBook.people.add(promptForAddress()); + file.writeAsBytes(addressBook.writeToBuffer()); +} diff --git a/examples/add_person.go b/examples/add_person.go index 4f2e7f7418..7ffb0ab095 100644 --- a/examples/add_person.go +++ b/examples/add_person.go @@ -10,7 +10,7 @@ import ( "strings" "github.com/golang/protobuf/proto" - pb "github.com/google/protobuf/examples/tutorial" + pb "github.com/protocolbuffers/protobuf/examples/tutorial" ) func promptForAddress(r io.Reader) (*pb.Person, error) { diff --git a/examples/add_person_test.go b/examples/add_person_test.go index f8ba93387f..d35f10ece9 100644 --- a/examples/add_person_test.go +++ b/examples/add_person_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/golang/protobuf/proto" - pb "github.com/google/protobuf/examples/tutorial" + pb "github.com/protocolbuffers/protobuf/examples/tutorial" ) func TestPromptForAddressReturnsAddress(t *testing.T) { diff --git a/examples/list_people.dart b/examples/list_people.dart new file mode 100644 index 0000000000..dd5bb41005 --- /dev/null +++ b/examples/list_people.dart @@ -0,0 +1,47 @@ +import 'dart:io'; + +import 'dart_tutorial/addressbook.pb.dart'; +import 'dart_tutorial/addressbook.pbenum.dart'; + +// Iterates though all people in the AddressBook and prints info about them. +void printAddressBook(AddressBook addressBook) { + for (Person person in addressBook.people) { + print('Person ID: ${person.id}'); + print(' Name: ${person.name}'); + if (person.hasEmail()) { + print(' E-mail address:${person.email}'); + } + + for (Person_PhoneNumber phoneNumber in person.phones) { + switch (phoneNumber.type) { + case Person_PhoneType.MOBILE: + print(' Mobile phone #: '); + break; + case Person_PhoneType.HOME: + print(' Home phone #: '); + break; + case Person_PhoneType.WORK: + print(' Work phone #: '); + break; + default: + print(' Unknown phone #: '); + break; + } + print(phoneNumber.number); + } + } +} + +// Reads the entire address book from a file and prints all +// the information inside. +main(List arguments) { + if (arguments.length != 1) { + print('Usage: list_person ADDRESS_BOOK_FILE'); + exit(-1); + } + + // Read the existing address book. + File file = new File(arguments.first); + AddressBook addressBook = new AddressBook.fromBuffer(file.readAsBytesSync()); + printAddressBook(addressBook); +} diff --git a/examples/list_people.go b/examples/list_people.go index 70bc589efc..6c2c34ac33 100644 --- a/examples/list_people.go +++ b/examples/list_people.go @@ -8,7 +8,7 @@ import ( "os" "github.com/golang/protobuf/proto" - pb "github.com/google/protobuf/examples/tutorial" + pb "github.com/protocolbuffers/protobuf/examples/tutorial" ) func writePerson(w io.Writer, p *pb.Person) { diff --git a/examples/list_people_test.go b/examples/list_people_test.go index 887997fe89..aceabd4a61 100644 --- a/examples/list_people_test.go +++ b/examples/list_people_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - pb "github.com/google/protobuf/examples/tutorial" + pb "github.com/protocolbuffers/protobuf/examples/tutorial" ) func TestWritePersonWritesPerson(t *testing.T) { @@ -34,7 +34,7 @@ func TestWritePersonWritesPerson(t *testing.T) { func TestListPeopleWritesList(t *testing.T) { buf := new(bytes.Buffer) - in := pb.AddressBook{People: []*pb.Person { + in := pb.AddressBook{People: []*pb.Person{ { Name: "John Doe", Id: 101, diff --git a/examples/pubspec.yaml b/examples/pubspec.yaml new file mode 100644 index 0000000000..0c36dbdcd3 --- /dev/null +++ b/examples/pubspec.yaml @@ -0,0 +1,5 @@ +name: addressbook +description: dartlang.org example code. + +dependencies: + protobuf: diff --git a/examples/third_party/zlib.BUILD b/examples/third_party/zlib.BUILD new file mode 120000 index 0000000000..1bbef2dbab --- /dev/null +++ b/examples/third_party/zlib.BUILD @@ -0,0 +1 @@ +../../third_party/zlib.BUILD \ No newline at end of file diff --git a/generate_changelog.py b/generate_changelog.py index 8e5bf4236c..60803c59dc 100755 --- a/generate_changelog.py +++ b/generate_changelog.py @@ -62,4 +62,4 @@ for language in languages: print("") print("To view a commit on GitHub: " + - "https://github.com/google/protobuf/commit/") + "https://github.com/protocolbuffers/protobuf/commit/") diff --git a/generate_descriptor_proto.sh b/generate_descriptor_proto.sh index 8a5ed48a2b..e533d05b43 100755 --- a/generate_descriptor_proto.sh +++ b/generate_descriptor_proto.sh @@ -18,14 +18,6 @@ __EOF__ exit 1 fi -if test ! -e src/Makefile; then - cat >&2 << __EOF__ -Could not find src/Makefile. You must run ./configure (and perhaps -./autogen.sh) first. -__EOF__ - exit 1 -fi - cd src declare -a RUNTIME_PROTO_FILES=(\ @@ -51,7 +43,7 @@ while [ $# -gt 0 ]; do case $1 in --bootstrap_protoc) BOOTSTRAP_PROTOC=$2 - shift + shift 2 ;; *) break @@ -78,8 +70,8 @@ do PROTOC="./protoc" fi - $PROTOC --cpp_out=dllexport_decl=LIBPROTOBUF_EXPORT:$TMP ${RUNTIME_PROTO_FILES[@]} && \ - $PROTOC --cpp_out=dllexport_decl=LIBPROTOC_EXPORT:$TMP ${COMPILER_PROTO_FILES[@]} + $PROTOC --cpp_out=dllexport_decl=PROTOBUF_EXPORT:$TMP ${RUNTIME_PROTO_FILES[@]} && \ + $PROTOC --cpp_out=dllexport_decl=PROTOC_EXPORT:$TMP ${COMPILER_PROTO_FILES[@]} for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]} ${COMPILER_PROTO_FILES[@]}; do BASE_NAME=${PROTO_FILE%.*} diff --git a/global.json b/global.json new file mode 100644 index 0000000000..0c7cc9f180 --- /dev/null +++ b/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "2.1.504" + } +} diff --git a/java/README.md b/java/README.md index 5e4fb8b495..d77ff5ea1b 100644 --- a/java/README.md +++ b/java/README.md @@ -13,13 +13,17 @@ generate Java code for your .proto files: $ protoc --java_out=${OUTPUT_DIR} path/to/your/proto/file Include the generated Java files in your project and add a dependency on the -protobuf Java runtime. If you are using Maven, use the following: +protobuf Java runtime. + +### Maven + +If you are using Maven, use the following: ```xml com.google.protobuf protobuf-java - 3.5.1 + 3.8.0 ``` @@ -33,10 +37,18 @@ protobuf-java-util package: com.google.protobuf protobuf-java-util - 3.5.1 + 3.8.0 ``` +### Gradle + +If you are using Gradle, add the following to your `build.gradle` file's dependencies: +``` + compile 'com.google.protobuf:protobuf-java:3.8.0' +``` +Again, be sure to check that the version number maches (or is newer than) the version number of protoc that you are using. + ### Use Java Protocol Buffers on Android For Android users, it's recommended to use protobuf Java Lite runtime because diff --git a/java/bom/pom.xml b/java/bom/pom.xml new file mode 100644 index 0000000000..485dabfacb --- /dev/null +++ b/java/bom/pom.xml @@ -0,0 +1,110 @@ + + + 4.0.0 + + com.google.protobuf + protobuf-bom + 3.9.0-rc-1 + pom + + Protocol Buffers [BOM] + A compatible set of open source libraries for working with protocol buffers. + https://developers.google.com/protocol-buffers/ + + + Google LLC + https://cloud.google.com + + + + + haon + Hao Nguyen + haon@google.com + Google + https://cloud.google.com + America/Los_Angeles + + + + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + https://github.com/protocolbuffers/protobuf + scm:git:https://github.com/protocolbuffers/protobuf.git + + + + UTF-8 + + + + + sonatype-nexus-staging + https://oss.sonatype.org/content/repositories/snapshots + + + sonatype-nexus-staging + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + + com.google.protobuf + protobuf-java + ${project.version} + + + com.google.protobuf + protobuf-java-util + ${project.version} + + + + + + + + release + + + + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.6 + true + + https://oss.sonatype.org/ + sonatype-nexus-staging + false + + + + + + + diff --git a/java/compatibility_tests/v2.5.0/more_protos/src/proto/google/protobuf/unittest_optimize_for.proto b/java/compatibility_tests/v2.5.0/more_protos/src/proto/google/protobuf/unittest_optimize_for.proto index feecbef8d4..658c6a9652 100644 --- a/java/compatibility_tests/v2.5.0/more_protos/src/proto/google/protobuf/unittest_optimize_for.proto +++ b/java/compatibility_tests/v2.5.0/more_protos/src/proto/google/protobuf/unittest_optimize_for.proto @@ -55,7 +55,7 @@ message TestOptimizedForSize { message TestRequiredOptimizedForSize { required int32 x = 1; } - + message TestOptionalOptimizedForSize { optional TestRequiredOptimizedForSize o = 1; } diff --git a/java/compatibility_tests/v2.5.0/protos/src/proto/google/protobuf/unittest_optimize_for.proto b/java/compatibility_tests/v2.5.0/protos/src/proto/google/protobuf/unittest_optimize_for.proto index feecbef8d4..658c6a9652 100644 --- a/java/compatibility_tests/v2.5.0/protos/src/proto/google/protobuf/unittest_optimize_for.proto +++ b/java/compatibility_tests/v2.5.0/protos/src/proto/google/protobuf/unittest_optimize_for.proto @@ -55,7 +55,7 @@ message TestOptimizedForSize { message TestRequiredOptimizedForSize { required int32 x = 1; } - + message TestOptionalOptimizedForSize { optional TestRequiredOptimizedForSize o = 1; } diff --git a/java/compatibility_tests/v2.5.0/test.sh b/java/compatibility_tests/v2.5.0/test.sh index 5d5e9ed439..860437809c 100755 --- a/java/compatibility_tests/v2.5.0/test.sh +++ b/java/compatibility_tests/v2.5.0/test.sh @@ -5,6 +5,9 @@ set -ex # Change to the script's directory. cd $(dirname $0) +MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository +MVN="mvn --batch-mode -e -X -Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY" + # Version of the tests (i.e., the version of protobuf from where we extracted # these tests). TEST_VERSION=`grep "^ .*" pom.xml | sed "s| \(.*\)|\1|"` @@ -21,23 +24,23 @@ case "$1" in ;; 2.6.1) OLD_VERSION=2.6.1 - OLD_VERSION_PROTOC=http://repo1.maven.org/maven2/com/google/protobuf/protoc/2.6.1-build2/protoc-2.6.1-build2-linux-x86_64.exe + OLD_VERSION_PROTOC=https://repo1.maven.org/maven2/com/google/protobuf/protoc/2.6.1-build2/protoc-2.6.1-build2-linux-x86_64.exe ;; 3.0.0-beta-1) OLD_VERSION=3.0.0-beta-1 - OLD_VERSION_PROTOC=http://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0-beta-1/protoc-3.0.0-beta-1-linux-x86_64.exe + OLD_VERSION_PROTOC=https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0-beta-1/protoc-3.0.0-beta-1-linux-x86_64.exe ;; 3.0.0-beta-2) OLD_VERSION=3.0.0-beta-2 - OLD_VERSION_PROTOC=http://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0-beta-2/protoc-3.0.0-beta-2-linux-x86_64.exe + OLD_VERSION_PROTOC=https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0-beta-2/protoc-3.0.0-beta-2-linux-x86_64.exe ;; 3.0.0-beta-3) OLD_VERSION=3.0.0-beta-3 - OLD_VERSION_PROTOC=http://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0-beta-3/protoc-3.0.0-beta-3-linux-x86_64.exe + OLD_VERSION_PROTOC=https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0-beta-3/protoc-3.0.0-beta-3-linux-x86_64.exe ;; 3.0.0-beta-4) OLD_VERSION=3.0.0-beta-4 - OLD_VERSION_PROTOC=http://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0-beta-4/protoc-3.0.0-beta-4-linux-x86_64.exe + OLD_VERSION_PROTOC=https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0-beta-4/protoc-3.0.0-beta-4-linux-x86_64.exe ;; *) echo "[ERROR]: Unknown version number: $1" @@ -59,13 +62,13 @@ echo "Running compatibility tests between $VERSION_NUMBER and $OLD_VERSION" # Build and install protobuf-java-$VERSION_NUMBER.jar [ -f ../../core/target/protobuf-java-$VERSION_NUMBER.jar ] || { pushd ../.. - mvn install -Dmaven.test.skip=true + $MVN install -Dmaven.test.skip=true popd } # Download old version source for the compatibility test [ -d protobuf ] || { - git clone https://github.com/google/protobuf.git + git clone https://github.com/protocolbuffers/protobuf.git cd protobuf git reset --hard v$TEST_VERSION cd .. @@ -81,7 +84,7 @@ chmod +x protoc # Test A.1: # protos: use new version # more_protos: use old version -mvn clean test \ +$MVN clean test \ -Dprotobuf.test.source.path=$(pwd)/protobuf \ -Dprotoc.path=$(pwd)/protoc \ -Dprotos.protoc.path=$(pwd)/../../../src/protoc \ @@ -90,7 +93,7 @@ mvn clean test \ # Test A.2: # protos: use old version # more_protos: use new version -mvn clean test \ +$MVN clean test \ -Dprotobuf.test.source.path=$(pwd)/protobuf \ -Dprotoc.path=$(pwd)/protoc \ -Dmore_protos.protoc.path=$(pwd)/../../../src/protoc \ @@ -103,12 +106,12 @@ mvn clean test \ # make it easier to run binary compatibility test (where we will need to run # the jar files directly). cd deps -mvn assembly:single +$MVN assembly:single cd .. cp -f deps/target/compatibility-test-deps-${TEST_VERSION}-jar-with-dependencies.jar deps.jar # Build the old version of all 3 artifacts. -mvn clean install -Dmaven.test.skip=true -Dprotoc.path=$(pwd)/protoc -Dprotobuf.version=$OLD_VERSION +$MVN clean install -Dmaven.test.skip=true -Dprotoc.path=$(pwd)/protoc -Dprotobuf.version=$OLD_VERSION cp -f protos/target/compatibility-protos-${TEST_VERSION}.jar protos.jar cp -f more_protos/target/compatibility-more-protos-${TEST_VERSION}.jar more_protos.jar cp -f tests/target/compatibility-tests-${TEST_VERSION}.jar tests.jar @@ -125,7 +128,7 @@ cd .. # Test B.2: update protos.jar only. cd protos -mvn clean package -Dmaven.test.skip=true -Dprotoc.path=$(pwd)/../../../../src/protoc -Dprotobuf.version=$VERSION_NUMBER +$MVN clean package -Dmaven.test.skip=true -Dprotoc.path=$(pwd)/../../../../src/protoc -Dprotobuf.version=$VERSION_NUMBER cd .. cd protobuf java -cp ../../../core/target/protobuf-java-$VERSION_NUMBER.jar:../protos/target/compatibility-protos-${TEST_VERSION}.jar:../more_protos.jar:../tests.jar:../deps.jar org.junit.runner.JUnitCore $TESTS @@ -133,7 +136,7 @@ cd .. # Test B.3: update more_protos.jar only. cd more_protos -mvn clean package -Dmaven.test.skip=true -Dprotoc.path=$(pwd)/../../../../src/protoc -Dprotobuf.version=$VERSION_NUMBER +$MVN clean package -Dmaven.test.skip=true -Dprotoc.path=$(pwd)/../../../../src/protoc -Dprotobuf.version=$VERSION_NUMBER cd .. cd protobuf java -cp ../../../core/target/protobuf-java-$VERSION_NUMBER.jar:../protos.jar:../more_protos/target/compatibility-more-protos-${TEST_VERSION}.jar:../tests.jar:../deps.jar org.junit.runner.JUnitCore $TESTS diff --git a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/ByteStringTest.java b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/ByteStringTest.java index 8bb9f731d2..9e334e5e92 100644 --- a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/ByteStringTest.java +++ b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/ByteStringTest.java @@ -342,7 +342,7 @@ public class ByteStringTest extends TestCase { return -1; } } - + // A stream which exposes the byte array passed into write(byte[], int, int). private static class EvilOutputStream extends OutputStream { public byte[] capturedArray = null; @@ -454,13 +454,13 @@ public class ByteStringTest extends TestCase { isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length)); } } - + public void testNewOutputEmpty() throws IOException { // Make sure newOutput() correctly builds empty byte strings ByteString byteString = ByteString.newOutput().toByteString(); assertEquals(ByteString.EMPTY, byteString); } - + public void testNewOutput_Mutating() throws IOException { Output os = ByteString.newOutput(5); os.write(new byte[] {1, 2, 3, 4, 5}); diff --git a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DeprecatedFieldTest.java b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DeprecatedFieldTest.java index ee4e767520..a7b77067f4 100644 --- a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DeprecatedFieldTest.java +++ b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DeprecatedFieldTest.java @@ -39,22 +39,22 @@ import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; /** * Test field deprecation - * + * * @author birdo@google.com (Roberto Scaramuzzi) */ public class DeprecatedFieldTest extends TestCase { private String[] deprecatedGetterNames = { "hasDeprecatedInt32", "getDeprecatedInt32"}; - + private String[] deprecatedBuilderGetterNames = { "hasDeprecatedInt32", "getDeprecatedInt32", "clearDeprecatedInt32"}; - + private String[] deprecatedBuilderSetterNames = { - "setDeprecatedInt32"}; - + "setDeprecatedInt32"}; + public void testDeprecatedField() throws Exception { Class deprecatedFields = TestDeprecatedFields.class; Class deprecatedFieldsBuilder = TestDeprecatedFields.Builder.class; @@ -74,7 +74,7 @@ public class DeprecatedFieldTest extends TestCase { isDeprecated(method)); } } - + private boolean isDeprecated(AnnotatedElement annotated) { return annotated.isAnnotationPresent(Deprecated.class); } diff --git a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DescriptorsTest.java b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DescriptorsTest.java index aabd7b4d73..37e1ca7350 100644 --- a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DescriptorsTest.java +++ b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DescriptorsTest.java @@ -490,7 +490,7 @@ public class DescriptorsTest extends TestCase { .build(); // translate and crosslink FileDescriptor file = - Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, + Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]); // verify resulting descriptors assertNotNull(file); @@ -511,7 +511,7 @@ public class DescriptorsTest extends TestCase { } assertTrue(barFound); } - + public void testInvalidPublicDependency() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .build(); @@ -595,7 +595,7 @@ public class DescriptorsTest extends TestCase { Descriptors.FileDescriptor.buildFrom( fooProto, new FileDescriptor[] {forwardFile}); } - + /** * Tests the translate/crosslink for an example with a more complex namespace * referencing. @@ -644,6 +644,6 @@ public class DescriptorsTest extends TestCase { assertTrue(field.getEnumType().getFile().getName().equals("bar.proto")); assertTrue(field.getEnumType().getFile().getPackage().equals( "a.b.c.d.bar.shared")); - } + } } } diff --git a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/GeneratedMessageTest.java b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/GeneratedMessageTest.java index 49f114643c..1e7d41e851 100644 --- a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/GeneratedMessageTest.java +++ b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/GeneratedMessageTest.java @@ -901,7 +901,7 @@ public class GeneratedMessageTest extends TestCase { FieldDescriptor fieldDescriptor = descriptor.findFieldByName("optional_nested_message"); - // Before setting field, builder is initialized by default value. + // Before setting field, builder is initialized by default value. TestAllTypes.Builder builder = TestAllTypes.newBuilder(); NestedMessage.Builder fieldBuilder = (NestedMessage.Builder) builder.getFieldBuilder(fieldDescriptor); diff --git a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/LiteralByteStringTest.java b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/LiteralByteStringTest.java index b2dcc7e882..5902ea33df 100644 --- a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/LiteralByteStringTest.java +++ b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/LiteralByteStringTest.java @@ -233,7 +233,7 @@ public class LiteralByteStringTest extends TestCase { assertTrue(classUnderTest + ".writeTo() must give back the same bytes", Arrays.equals(referenceBytes, roundTripBytes)); } - + public void testWriteTo_mutating() throws IOException { OutputStream os = new OutputStream() { @Override @@ -274,7 +274,7 @@ public class LiteralByteStringTest extends TestCase { assertEquals("Output.reset() resets the output", 0, output.size()); assertEquals("Output.reset() resets the output", ByteString.EMPTY, output.toByteString()); - + } public void testHashCode() { diff --git a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/MessageTest.java b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/MessageTest.java index c8c95a87c7..629cabc0c5 100644 --- a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/MessageTest.java +++ b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/MessageTest.java @@ -313,7 +313,7 @@ public class MessageTest extends TestCase { assertEquals("Message missing required fields: a, b, c", e.getMessage()); } } - + /** Test reading unset repeated message from DynamicMessage. */ public void testDynamicRepeatedMessageNull() throws Exception { Descriptors.Descriptor descriptor = TestRequired.getDescriptor(); @@ -327,7 +327,7 @@ public class MessageTest extends TestCase { assertEquals(result.getRepeatedFieldCount(result.getDescriptorForType() .findFieldByName("repeated_foreign_message")), 0); } - + /** Test reading repeated message from DynamicMessage. */ public void testDynamicRepeatedMessageNotNull() throws Exception { diff --git a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/RopeByteStringSubstringTest.java b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/RopeByteStringSubstringTest.java index 3c1f5035b7..c122385ac1 100644 --- a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/RopeByteStringSubstringTest.java +++ b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/RopeByteStringSubstringTest.java @@ -36,7 +36,7 @@ import java.util.Iterator; /** * This class tests {@link RopeByteString#substring(int, int)} by inheriting the tests from - * {@link LiteralByteStringTest}. Only a couple of methods are overridden. + * {@link LiteralByteStringTest}. Only a couple of methods are overridden. * * @author carlanton@google.com (Carl Haverl) */ diff --git a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/RopeByteStringTest.java b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/RopeByteStringTest.java index 8caeadd9a1..19098de660 100644 --- a/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/RopeByteStringTest.java +++ b/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/RopeByteStringTest.java @@ -38,7 +38,7 @@ import java.util.Iterator; /** * This class tests {@link RopeByteString} by inheriting the tests from * {@link LiteralByteStringTest}. Only a couple of methods are overridden. - * + * *

A full test of the result of {@link RopeByteString#substring(int, int)} is found in the * separate class {@link RopeByteStringSubstringTest}. * diff --git a/java/core/generate-test-sources-build.xml b/java/core/generate-test-sources-build.xml index 1d11f13144..92c0b1c8ae 100644 --- a/java/core/generate-test-sources-build.xml +++ b/java/core/generate-test-sources-build.xml @@ -5,42 +5,56 @@ - + + + + + - - - - - - - + + + + + + + + + + + - - + + + + + + - - - - - - - + + + + + + + + + diff --git a/java/core/pom.xml b/java/core/pom.xml index 9dbaff15c5..fb518edd5a 100644 --- a/java/core/pom.xml +++ b/java/core/pom.xml @@ -1,12 +1,10 @@ - + 4.0.0 com.google.protobuf protobuf-parent - 3.6.1 + 3.9.0-rc-1 protobuf-java diff --git a/java/core/src/main/java/com/google/protobuf/AbstractMessage.java b/java/core/src/main/java/com/google/protobuf/AbstractMessage.java index 09aaed18a4..15ee53a426 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractMessage.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractMessage.java @@ -44,15 +44,14 @@ import java.util.List; import java.util.Map; /** - * A partial implementation of the {@link Message} interface which implements - * as many methods of that interface as possible in terms of other methods. + * A partial implementation of the {@link Message} interface which implements as many methods of + * that interface as possible in terms of other methods. * * @author kenton@google.com Kenton Varda */ public abstract class AbstractMessage // TODO(dweis): Update GeneratedMessage to parameterize with MessageType and BuilderType. - extends AbstractMessageLite - implements Message { + extends AbstractMessageLite implements Message { @Override public boolean isInitialized() { @@ -60,24 +59,21 @@ public abstract class AbstractMessage } /** - * Interface for the parent of a Builder that allows the builder to - * communicate invalidations back to the parent for use when using nested - * builders. + * Interface for the parent of a Builder that allows the builder to communicate invalidations back + * to the parent for use when using nested builders. */ protected interface BuilderParent { /** - * A builder becomes dirty whenever a field is modified -- including fields - * in nested builders -- and becomes clean when build() is called. Thus, - * when a builder becomes dirty, all its parents become dirty as well, and - * when it becomes clean, all its children become clean. The dirtiness - * state is used to invalidate certain cached values. - *
- * To this end, a builder calls markDirty() on its parent whenever it - * transitions from clean to dirty. The parent must propagate this call to - * its own parent, unless it was already dirty, in which case the - * grandparent must necessarily already be dirty as well. The parent can - * only transition back to "clean" after calling build() on all children. + * A builder becomes dirty whenever a field is modified -- including fields in nested builders + * -- and becomes clean when build() is called. Thus, when a builder becomes dirty, all its + * parents become dirty as well, and when it becomes clean, all its children become clean. The + * dirtiness state is used to invalidate certain cached values. + * + *

To this end, a builder calls markDirty() on its parent whenever it transitions from clean + * to dirty. The parent must propagate this call to its own parent, unless it was already dirty, + * in which case the grandparent must necessarily already be dirty as well. The parent can only + * transition back to "clean" after calling build() on all children. */ void markDirty(); } @@ -107,13 +103,12 @@ public abstract class AbstractMessage /** TODO(jieluo): Clear it when all subclasses have implemented this method. */ @Override public FieldDescriptor getOneofFieldDescriptor(OneofDescriptor oneof) { - throw new UnsupportedOperationException( - "getOneofFieldDescriptor() is not implemented."); + throw new UnsupportedOperationException("getOneofFieldDescriptor() is not implemented."); } @Override public final String toString() { - return TextFormat.printToString(this); + return TextFormat.printer().printToString(this); } @Override @@ -156,8 +151,8 @@ public abstract class AbstractMessage if (getDescriptorForType() != otherMessage.getDescriptorForType()) { return false; } - return compareFields(getAllFields(), otherMessage.getAllFields()) && - getUnknownFields().equals(otherMessage.getUnknownFields()); + return compareFields(getAllFields(), otherMessage.getAllFields()) + && getUnknownFields().equals(otherMessage.getUnknownFields()); } @Override @@ -182,20 +177,17 @@ public abstract class AbstractMessage } /** - * Compares two bytes fields. The parameters must be either a byte array or a - * ByteString object. They can be of different type though. + * Compares two bytes fields. The parameters must be either a byte array or a ByteString object. + * They can be of different type though. */ private static boolean compareBytes(Object a, Object b) { if (a instanceof byte[] && b instanceof byte[]) { - return Arrays.equals((byte[])a, (byte[])b); + return Arrays.equals((byte[]) a, (byte[]) b); } return toByteString(a).equals(toByteString(b)); } - /** - * Converts a list of MapEntry messages into a Map used for equals() and - * hashCode(). - */ + /** Converts a list of MapEntry messages into a Map used for equals() and hashCode(). */ @SuppressWarnings({"rawtypes", "unchecked"}) private static Map convertMapEntryListToMap(List list) { if (list.isEmpty()) { @@ -223,10 +215,7 @@ public abstract class AbstractMessage return result; } - /** - * Compares two map fields. The parameters must be a list of MapEntry - * messages. - */ + /** Compares two map fields. The parameters must be a list of MapEntry messages. */ @SuppressWarnings({"rawtypes", "unchecked"}) private static boolean compareMapField(Object a, Object b) { Map ma = convertMapEntryListToMap((List) a); @@ -235,16 +224,13 @@ public abstract class AbstractMessage } /** - * Compares two set of fields. - * This method is used to implement {@link AbstractMessage#equals(Object)} - * and {@link AbstractMutableMessage#equals(Object)}. It takes special care - * of bytes fields because immutable messages and mutable messages use - * different Java type to represent a bytes field and this method should be - * able to compare immutable messages, mutable messages and also an immutable - * message to a mutable message. + * Compares two set of fields. This method is used to implement {@link + * AbstractMessage#equals(Object)} and {@link AbstractMutableMessage#equals(Object)}. It takes + * special care of bytes fields because immutable messages and mutable messages use different Java + * type to represent a bytes field and this method should be able to compare immutable messages, + * mutable messages and also an immutable message to a mutable message. */ - static boolean compareFields(Map a, - Map b) { + static boolean compareFields(Map a, Map b) { if (a.size() != b.size()) { return false; } @@ -286,10 +272,7 @@ public abstract class AbstractMessage return true; } - /** - * Calculates the hash code of a map field. {@code value} must be a list of - * MapEntry messages. - */ + /** Calculates the hash code of a map field. {@code value} must be a list of MapEntry messages. */ @SuppressWarnings("unchecked") private static int hashMapField(Object value) { return MapFieldLite.calculateHashCodeForMap(convertMapEntryListToMap((List) value)); @@ -304,7 +287,7 @@ public abstract class AbstractMessage hash = (37 * hash) + field.getNumber(); if (field.isMapField()) { hash = (53 * hash) + hashMapField(value); - } else if (field.getType() != FieldDescriptor.Type.ENUM){ + } else if (field.getType() != FieldDescriptor.Type.ENUM) { hash = (53 * hash) + value.hashCode(); } else if (field.isRepeated()) { List list = (List) value; @@ -317,8 +300,8 @@ public abstract class AbstractMessage } /** - * Package private helper method for AbstractParser to create - * UninitializedMessageException with missing field information. + * Package private helper method for AbstractParser to create UninitializedMessageException with + * missing field information. */ @Override UninitializedMessageException newUninitializedMessageException() { @@ -328,14 +311,12 @@ public abstract class AbstractMessage // ================================================================= /** - * A partial implementation of the {@link Message.Builder} interface which - * implements as many methods of that interface as possible in terms of - * other methods. + * A partial implementation of the {@link Message.Builder} interface which implements as many + * methods of that interface as possible in terms of other methods. */ @SuppressWarnings("unchecked") - public static abstract class Builder> - extends AbstractMessageLite.Builder - implements Message.Builder { + public abstract static class Builder> + extends AbstractMessageLite.Builder implements Message.Builder { // The compiler produces an error if this is not declared explicitly. // Method isn't abstract to bypass Java 1.6 compiler issue: // http://bugs.java.com/view_bug.do?bug_id=6908259 @@ -353,8 +334,7 @@ public abstract class AbstractMessage /** TODO(jieluo): Clear it when all subclasses have implemented this method. */ @Override public FieldDescriptor getOneofFieldDescriptor(OneofDescriptor oneof) { - throw new UnsupportedOperationException( - "getOneofFieldDescriptor() is not implemented."); + throw new UnsupportedOperationException("getOneofFieldDescriptor() is not implemented."); } /** TODO(jieluo): Clear it when all subclasses have implemented this method. */ @@ -365,8 +345,7 @@ public abstract class AbstractMessage @Override public BuilderType clear() { - for (final Map.Entry entry : - getAllFields().entrySet()) { + for (final Map.Entry entry : getAllFields().entrySet()) { clearField(entry.getKey()); } return (BuilderType) this; @@ -391,11 +370,11 @@ public abstract class AbstractMessage public BuilderType mergeFrom(final Message other) { return mergeFrom(other, other.getAllFields()); } - + BuilderType mergeFrom(final Message other, Map allFields) { if (other.getDescriptorForType() != getDescriptorForType()) { throw new IllegalArgumentException( - "mergeFrom(Message) can only merge messages of the same type."); + "mergeFrom(Message) can only merge messages of the same type."); } // Note: We don't attempt to verify that other's fields have valid @@ -410,19 +389,21 @@ public abstract class AbstractMessage for (final Map.Entry entry : allFields.entrySet()) { final FieldDescriptor field = entry.getKey(); if (field.isRepeated()) { - for (final Object element : (List)entry.getValue()) { + for (final Object element : (List) entry.getValue()) { addRepeatedField(field, element); } } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { - final Message existingValue = (Message)getField(field); + final Message existingValue = (Message) getField(field); if (existingValue == existingValue.getDefaultInstanceForType()) { setField(field, entry.getValue()); } else { - setField(field, - existingValue.newBuilderForType() - .mergeFrom(existingValue) - .mergeFrom((Message)entry.getValue()) - .build()); + setField( + field, + existingValue + .newBuilderForType() + .mergeFrom(existingValue) + .mergeFrom((Message) entry.getValue()) + .build()); } } else { setField(field, entry.getValue()); @@ -435,15 +416,13 @@ public abstract class AbstractMessage } @Override - public BuilderType mergeFrom(final CodedInputStream input) - throws IOException { + public BuilderType mergeFrom(final CodedInputStream input) throws IOException { return mergeFrom(input, ExtensionRegistry.getEmptyRegistry()); } @Override public BuilderType mergeFrom( - final CodedInputStream input, - final ExtensionRegistryLite extensionRegistry) + final CodedInputStream input, final ExtensionRegistryLite extensionRegistry) throws IOException { boolean discardUnknown = input.shouldDiscardUnknownFields(); final UnknownFieldSet.Builder unknownFields = @@ -456,11 +435,8 @@ public abstract class AbstractMessage MessageReflection.BuilderAdapter builderAdapter = new MessageReflection.BuilderAdapter(this); - if (!MessageReflection.mergeFieldFrom(input, unknownFields, - extensionRegistry, - getDescriptorForType(), - builderAdapter, - tag)) { + if (!MessageReflection.mergeFieldFrom( + input, unknownFields, extensionRegistry, getDescriptorForType(), builderAdapter, tag)) { // end group tag break; } @@ -474,9 +450,7 @@ public abstract class AbstractMessage @Override public BuilderType mergeUnknownFields(final UnknownFieldSet unknownFields) { setUnknownFields( - UnknownFieldSet.newBuilder(getUnknownFields()) - .mergeFrom(unknownFields) - .build()); + UnknownFieldSet.newBuilder(getUnknownFields()).mergeFrom(unknownFields).build()); return (BuilderType) this; } @@ -494,39 +468,33 @@ public abstract class AbstractMessage @Override public String toString() { - return TextFormat.printToString(this); + return TextFormat.printer().printToString(this); } - /** - * Construct an UninitializedMessageException reporting missing fields in - * the given message. - */ - protected static UninitializedMessageException - newUninitializedMessageException(Message message) { - return new UninitializedMessageException( - MessageReflection.findMissingFields(message)); + /** Construct an UninitializedMessageException reporting missing fields in the given message. */ + protected static UninitializedMessageException newUninitializedMessageException( + Message message) { + return new UninitializedMessageException(MessageReflection.findMissingFields(message)); } /** - * Used to support nested builders and called to mark this builder as clean. - * Clean builders will propagate the {@link BuilderParent#markDirty()} event - * to their parent builders, while dirty builders will not, as their parents - * should be dirty already. + * Used to support nested builders and called to mark this builder as clean. Clean builders will + * propagate the {@link BuilderParent#markDirty()} event to their parent builders, while dirty + * builders will not, as their parents should be dirty already. * - * NOTE: Implementations that don't support nested builders don't need to - * override this method. + *

NOTE: Implementations that don't support nested builders don't need to override this + * method. */ void markClean() { throw new IllegalStateException("Should be overridden by subclasses."); } /** - * Used to support nested builders and called when this nested builder is - * no longer used by its parent builder and should release the reference - * to its parent builder. + * Used to support nested builders and called when this nested builder is no longer used by its + * parent builder and should release the reference to its parent builder. * - * NOTE: Implementations that don't support nested builders don't need to - * override this method. + *

NOTE: Implementations that don't support nested builders don't need to override this + * method. */ void dispose() { throw new IllegalStateException("Should be overridden by subclasses."); @@ -552,73 +520,63 @@ public abstract class AbstractMessage // bug. @Override - public BuilderType mergeFrom(final ByteString data) - throws InvalidProtocolBufferException { + public BuilderType mergeFrom(final ByteString data) throws InvalidProtocolBufferException { return (BuilderType) super.mergeFrom(data); } @Override public BuilderType mergeFrom( - final ByteString data, - final ExtensionRegistryLite extensionRegistry) + final ByteString data, final ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { return (BuilderType) super.mergeFrom(data, extensionRegistry); } @Override - public BuilderType mergeFrom(final byte[] data) - throws InvalidProtocolBufferException { + public BuilderType mergeFrom(final byte[] data) throws InvalidProtocolBufferException { return (BuilderType) super.mergeFrom(data); } @Override - public BuilderType mergeFrom( - final byte[] data, final int off, final int len) + public BuilderType mergeFrom(final byte[] data, final int off, final int len) throws InvalidProtocolBufferException { return (BuilderType) super.mergeFrom(data, off, len); } @Override - public BuilderType mergeFrom( - final byte[] data, - final ExtensionRegistryLite extensionRegistry) + public BuilderType mergeFrom(final byte[] data, final ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { return (BuilderType) super.mergeFrom(data, extensionRegistry); } @Override public BuilderType mergeFrom( - final byte[] data, final int off, final int len, + final byte[] data, + final int off, + final int len, final ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { return (BuilderType) super.mergeFrom(data, off, len, extensionRegistry); } @Override - public BuilderType mergeFrom(final InputStream input) - throws IOException { + public BuilderType mergeFrom(final InputStream input) throws IOException { return (BuilderType) super.mergeFrom(input); } @Override public BuilderType mergeFrom( - final InputStream input, - final ExtensionRegistryLite extensionRegistry) - throws IOException { + final InputStream input, final ExtensionRegistryLite extensionRegistry) throws IOException { return (BuilderType) super.mergeFrom(input, extensionRegistry); } @Override - public boolean mergeDelimitedFrom(final InputStream input) - throws IOException { + public boolean mergeDelimitedFrom(final InputStream input) throws IOException { return super.mergeDelimitedFrom(input); } @Override public boolean mergeDelimitedFrom( - final InputStream input, - final ExtensionRegistryLite extensionRegistry) - throws IOException { + final InputStream input, final ExtensionRegistryLite extensionRegistry) throws IOException { return super.mergeDelimitedFrom(input, extensionRegistry); } } diff --git a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java index b22bbaab62..cde78f3a10 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java @@ -41,22 +41,21 @@ import java.util.Collection; import java.util.List; /** - * A partial implementation of the {@link MessageLite} interface which - * implements as many methods of that interface as possible in terms of other - * methods. + * A partial implementation of the {@link MessageLite} interface which implements as many methods of + * that interface as possible in terms of other methods. * * @author kenton@google.com Kenton Varda */ public abstract class AbstractMessageLite< - MessageType extends AbstractMessageLite, - BuilderType extends AbstractMessageLite.Builder> - implements MessageLite { + MessageType extends AbstractMessageLite, + BuilderType extends AbstractMessageLite.Builder> + implements MessageLite { protected int memoizedHashCode = 0; + @Override public ByteString toByteString() { try { - final ByteString.CodedBuilder out = - ByteString.newCodedBuilder(getSerializedSize()); + final ByteString.CodedBuilder out = ByteString.newCodedBuilder(getSerializedSize()); writeTo(out.getCodedOutput()); return out.build(); } catch (IOException e) { @@ -79,10 +78,8 @@ public abstract class AbstractMessageLite< @Override public void writeTo(final OutputStream output) throws IOException { - final int bufferSize = - CodedOutputStream.computePreferredBufferSize(getSerializedSize()); - final CodedOutputStream codedOutput = - CodedOutputStream.newInstance(output, bufferSize); + final int bufferSize = CodedOutputStream.computePreferredBufferSize(getSerializedSize()); + final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output, bufferSize); writeTo(codedOutput); codedOutput.flush(); } @@ -90,10 +87,10 @@ public abstract class AbstractMessageLite< @Override public void writeDelimitedTo(final OutputStream output) throws IOException { final int serialized = getSerializedSize(); - final int bufferSize = CodedOutputStream.computePreferredBufferSize( - CodedOutputStream.computeRawVarint32Size(serialized) + serialized); - final CodedOutputStream codedOutput = - CodedOutputStream.newInstance(output, bufferSize); + final int bufferSize = + CodedOutputStream.computePreferredBufferSize( + CodedOutputStream.computeRawVarint32Size(serialized) + serialized); + final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output, bufferSize); codedOutput.writeRawVarint32(serialized); writeTo(codedOutput); codedOutput.flush(); @@ -110,16 +107,25 @@ public abstract class AbstractMessageLite< } - /** - * Package private helper method for AbstractParser to create - * UninitializedMessageException. - */ + int getSerializedSize(Schema schema) { + int memoizedSerializedSize = getMemoizedSerializedSize(); + if (memoizedSerializedSize == -1) { + memoizedSerializedSize = schema.getSerializedSize(this); + setMemoizedSerializedSize(memoizedSerializedSize); + } + return memoizedSerializedSize; + } + + /** Package private helper method for AbstractParser to create UninitializedMessageException. */ UninitializedMessageException newUninitializedMessageException() { return new UninitializedMessageException(this); } private String getSerializingExceptionMessage(String target) { - return "Serializing " + getClass().getName() + " to a " + target + return "Serializing " + + getClass().getName() + + " to a " + + target + " threw an IOException (should never happen)."; } @@ -141,14 +147,13 @@ public abstract class AbstractMessageLite< } /** - * A partial implementation of the {@link Message.Builder} interface which - * implements as many methods of that interface as possible in terms of - * other methods. + * A partial implementation of the {@link Message.Builder} interface which implements as many + * methods of that interface as possible in terms of other methods. */ @SuppressWarnings("unchecked") public abstract static class Builder< - MessageType extends AbstractMessageLite, - BuilderType extends Builder> + MessageType extends AbstractMessageLite, + BuilderType extends Builder> implements MessageLite.Builder { // The compiler produces an error if this is not declared explicitly. @Override @@ -204,8 +209,7 @@ public abstract class AbstractMessageLite< public BuilderType mergeFrom(final byte[] data, final int off, final int len) throws InvalidProtocolBufferException { try { - final CodedInputStream input = - CodedInputStream.newInstance(data, off, len); + final CodedInputStream input = CodedInputStream.newInstance(data, off, len); mergeFrom(input); input.checkLastTagWas(0); return (BuilderType) this; @@ -230,8 +234,7 @@ public abstract class AbstractMessageLite< final ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { try { - final CodedInputStream input = - CodedInputStream.newInstance(data, off, len); + final CodedInputStream input = CodedInputStream.newInstance(data, off, len); mergeFrom(input, extensionRegistry); input.checkLastTagWas(0); return (BuilderType) this; @@ -260,10 +263,9 @@ public abstract class AbstractMessageLite< } /** - * An InputStream implementations which reads from some other InputStream - * but is limited to a particular number of bytes. Used by - * mergeDelimitedFrom(). This is intentionally package-private so that - * UnknownFieldSet can share it. + * An InputStream implementations which reads from some other InputStream but is limited to a + * particular number of bytes. Used by mergeDelimitedFrom(). This is intentionally + * package-private so that UnknownFieldSet can share it. */ static final class LimitedInputStream extends FilterInputStream { private int limit; @@ -291,8 +293,7 @@ public abstract class AbstractMessageLite< } @Override - public int read(final byte[] b, final int off, int len) - throws IOException { + public int read(final byte[] b, final int off, int len) throws IOException { if (limit <= 0) { return -1; } @@ -329,8 +330,7 @@ public abstract class AbstractMessageLite< @Override public boolean mergeDelimitedFrom(final InputStream input) throws IOException { - return mergeDelimitedFrom(input, - ExtensionRegistryLite.getEmptyRegistry()); + return mergeDelimitedFrom(input, ExtensionRegistryLite.getEmptyRegistry()); } @Override @@ -347,7 +347,10 @@ public abstract class AbstractMessageLite< protected abstract BuilderType internalMergeFrom(MessageType message); private String getReadingExceptionMessage(String target) { - return "Reading " + getClass().getName() + " from a " + target + return "Reading " + + getClass().getName() + + " from a " + + target + " threw an IOException (should never happen)."; } @@ -370,12 +373,9 @@ public abstract class AbstractMessageLite< } } - /** - * Construct an UninitializedMessageException reporting missing fields in - * the given message. - */ - protected static UninitializedMessageException - newUninitializedMessageException(MessageLite message) { + /** Construct an UninitializedMessageException reporting missing fields in the given message. */ + protected static UninitializedMessageException newUninitializedMessageException( + MessageLite message) { return new UninitializedMessageException(message); } diff --git a/java/core/src/main/java/com/google/protobuf/AbstractParser.java b/java/core/src/main/java/com/google/protobuf/AbstractParser.java index ba570e3df1..abfaca8cd8 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractParser.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractParser.java @@ -36,23 +36,19 @@ import java.io.InputStream; import java.nio.ByteBuffer; /** - * A partial implementation of the {@link Parser} interface which implements - * as many methods of that interface as possible in terms of other methods. + * A partial implementation of the {@link Parser} interface which implements as many methods of that + * interface as possible in terms of other methods. * - * Note: This class implements all the convenience methods in the - * {@link Parser} interface. See {@link Parser} for related javadocs. - * Subclasses need to implement - * {@link Parser#parsePartialFrom(CodedInputStream, ExtensionRegistryLite)} + *

Note: This class implements all the convenience methods in the {@link Parser} interface. See + * {@link Parser} for related javadocs. Subclasses need to implement {@link + * Parser#parsePartialFrom(CodedInputStream, ExtensionRegistryLite)} * * @author liujisi@google.com (Pherl Liu) */ public abstract class AbstractParser implements Parser { - /** - * Creates an UninitializedMessageException for MessageType. - */ - private UninitializedMessageException - newUninitializedMessageException(MessageType message) { + /** Creates an UninitializedMessageException for MessageType. */ + private UninitializedMessageException newUninitializedMessageException(MessageType message) { if (message instanceof AbstractMessageLite) { return ((AbstractMessageLite) message).newUninitializedMessageException(); } @@ -75,8 +71,8 @@ public abstract class AbstractParser return message; } - private static final ExtensionRegistryLite EMPTY_REGISTRY - = ExtensionRegistryLite.getEmptyRegistry(); + private static final ExtensionRegistryLite EMPTY_REGISTRY = + ExtensionRegistryLite.getEmptyRegistry(); @Override public MessageType parsePartialFrom(CodedInputStream input) @@ -87,8 +83,7 @@ public abstract class AbstractParser @Override public MessageType parseFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - return checkMessageInitialized( - parsePartialFrom(input, extensionRegistry)); + return checkMessageInitialized(parsePartialFrom(input, extensionRegistry)); } @Override @@ -193,8 +188,7 @@ public abstract class AbstractParser public MessageType parseFrom( byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - return checkMessageInitialized( - parsePartialFrom(data, off, len, extensionRegistry)); + return checkMessageInitialized(parsePartialFrom(data, off, len, extensionRegistry)); } @Override @@ -235,8 +229,7 @@ public abstract class AbstractParser @Override public MessageType parseFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - return checkMessageInitialized( - parsePartialFrom(input, extensionRegistry)); + return checkMessageInitialized(parsePartialFrom(input, extensionRegistry)); } @Override @@ -271,8 +264,7 @@ public abstract class AbstractParser @Override public MessageType parseDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - return checkMessageInitialized( - parsePartialDelimitedFrom(input, extensionRegistry)); + return checkMessageInitialized(parsePartialDelimitedFrom(input, extensionRegistry)); } @Override diff --git a/java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java b/java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java index b17db6e0b3..3220f64072 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java @@ -31,7 +31,6 @@ package com.google.protobuf; import com.google.protobuf.Internal.ProtobufList; - import java.util.AbstractList; import java.util.Collection; import java.util.List; @@ -39,23 +38,19 @@ import java.util.RandomAccess; /** * An abstract implementation of {@link ProtobufList} which manages mutability semantics. All mutate - * methods must check if the list is mutable before proceeding. Subclasses must invoke - * {@link #ensureIsMutable()} manually when overriding those methods. - *

- * This implementation assumes all subclasses are array based, supporting random access. + * methods must check if the list is mutable before proceeding. Subclasses must invoke {@link + * #ensureIsMutable()} manually when overriding those methods. + * + *

This implementation assumes all subclasses are array based, supporting random access. */ abstract class AbstractProtobufList extends AbstractList implements ProtobufList { protected static final int DEFAULT_CAPACITY = 10; - /** - * Whether or not this list is modifiable. - */ + /** Whether or not this list is modifiable. */ private boolean isMutable; - /** - * Constructs a mutable list by default. - */ + /** Constructs a mutable list by default. */ AbstractProtobufList() { isMutable = true; } @@ -115,7 +110,7 @@ abstract class AbstractProtobufList extends AbstractList implements Protob ensureIsMutable(); return super.addAll(c); } - + @Override public boolean addAll(int index, Collection c) { ensureIsMutable(); @@ -127,47 +122,47 @@ abstract class AbstractProtobufList extends AbstractList implements Protob ensureIsMutable(); super.clear(); } - + @Override public boolean isModifiable() { return isMutable; } - + @Override public final void makeImmutable() { isMutable = false; } - + @Override public E remove(int index) { ensureIsMutable(); return super.remove(index); } - + @Override public boolean remove(Object o) { ensureIsMutable(); return super.remove(o); } - + @Override public boolean removeAll(Collection c) { ensureIsMutable(); return super.removeAll(c); } - + @Override public boolean retainAll(Collection c) { ensureIsMutable(); return super.retainAll(c); } - + @Override public E set(int index, E element) { ensureIsMutable(); return super.set(index, element); } - + /** * Throws an {@link UnsupportedOperationException} if the list is immutable. Subclasses are * responsible for invoking this method on mutate operations. diff --git a/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java b/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java new file mode 100755 index 0000000000..a01a6c1a8b --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java @@ -0,0 +1,263 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +import java.nio.ByteBuffer; + +/** + * A buffer that was allocated by a {@link BufferAllocator}. For every buffer, it is guaranteed that + * at least one of {@link #hasArray()} or {@link #hasNioBuffer()} will be {@code true}. + */ +@ExperimentalApi +abstract class AllocatedBuffer { + /** + * Indicates whether this buffer contains a backing {@link ByteBuffer} (i.e. it is safe to call + * {@link #nioBuffer()}). + */ + public abstract boolean hasNioBuffer(); + + /** + * Indicates whether this buffer contains a backing array (i.e. it is safe to call {@link + * #array()}). + */ + public abstract boolean hasArray(); + + /** + * Returns the {@link ByteBuffer} that backs this buffer (optional operation). + * + *

Call {@link #hasNioBuffer()} before invoking this method in order to ensure that this buffer + * has a backing {@link ByteBuffer}. + * + * @return The {@link ByteBuffer} that backs this buffer + * @throws UnsupportedOperationException If this buffer is not backed by a {@link ByteBuffer}. + */ + public abstract ByteBuffer nioBuffer(); + + /** + * Returns the byte array that backs this buffer (optional operation). + * + *

Call {@link #hasArray()} before invoking this method in order to ensure that this buffer has + * an accessible backing array. + * + * @return The array that backs this buffer + * @throws java.nio.ReadOnlyBufferException If this buffer is backed by an array but is read-only + * @throws UnsupportedOperationException If this buffer is not backed by an accessible array + */ + public abstract byte[] array(); + + /** + * Returns the offset within this buffer's backing array of the first element of the buffer + * (optional operation). + * + *

If this buffer is backed by an array then {@link #position()} corresponds to the array index + * {@link #position()} {@code +} {@link #arrayOffset()}. + * + *

Invoke the {@link #hasArray hasArray} method before invoking this method in order to ensure + * that this buffer has an accessible backing array. + * + * @return The offset within this buffer's array of the first element of the buffer + * @throws java.nio.ReadOnlyBufferException If this buffer is backed by an array but is read-only + * @throws UnsupportedOperationException If this buffer is not backed by an accessible array + */ + public abstract int arrayOffset(); + + /** + * Returns this buffer's position. + * + * @return The position of this buffer + */ + public abstract int position(); + + /** + * Sets this buffer's position. + * + * @param position The new position value; must be non-negative and no larger than the current + * limit + * @return This buffer + * @throws IllegalArgumentException If the preconditions on {@code position} do not hold + */ + public abstract AllocatedBuffer position(int position); + + /** + * Returns this buffer's limit. + * + * @return The limit of this buffer + */ + public abstract int limit(); + + /** + * Returns the number of elements between the current {@link #position()} and the {@link #limit()} + * . + * + * @return The number of elements remaining in this buffer + */ + public abstract int remaining(); + + /** + * Creates a new {@link AllocatedBuffer} that is backed by the given array. The returned buffer + * will have {@link #hasArray} == {@code true}, {@link #arrayOffset()} == {@code 0}, {@link + * #position()} == {@code 0} and {@link #limit()} equal to the length of {@code bytes}. + */ + public static AllocatedBuffer wrap(byte[] bytes) { + return wrapNoCheck(bytes, 0, bytes.length); + } + + /** + * Creates a new {@link AllocatedBuffer} that is backed by the given array. The returned buffer + * will have {@link #hasArray} == {@code true}, {@link #arrayOffset()} == {@code offset}, {@link + * #position()} == {@code 0} and {@link #limit()} == {@code length}. + */ + public static AllocatedBuffer wrap(final byte[] bytes, final int offset, final int length) { + if (offset < 0 || length < 0 || (offset + length) > bytes.length) { + throw new IndexOutOfBoundsException( + String.format("bytes.length=%d, offset=%d, length=%d", bytes.length, offset, length)); + } + + return wrapNoCheck(bytes, offset, length); + } + + /** + * Creates a new {@link AllocatedBuffer} that is backed by the given {@link ByteBuffer}. The + * returned buffer will have {@link #hasNioBuffer} == {@code true}. + */ + public static AllocatedBuffer wrap(final ByteBuffer buffer) { + checkNotNull(buffer, "buffer"); + + return new AllocatedBuffer() { + + @Override + public boolean hasNioBuffer() { + return true; + } + + @Override + public ByteBuffer nioBuffer() { + return buffer; + } + + @Override + public boolean hasArray() { + return buffer.hasArray(); + } + + @Override + public byte[] array() { + return buffer.array(); + } + + @Override + public int arrayOffset() { + return buffer.arrayOffset(); + } + + @Override + public int position() { + return buffer.position(); + } + + @Override + public AllocatedBuffer position(int position) { + buffer.position(position); + return this; + } + + @Override + public int limit() { + return buffer.limit(); + } + + @Override + public int remaining() { + return buffer.remaining(); + } + }; + } + + private static AllocatedBuffer wrapNoCheck( + final byte[] bytes, final int offset, final int length) { + return new AllocatedBuffer() { + // Relative to offset. + private int position; + + @Override + public boolean hasNioBuffer() { + return false; + } + + @Override + public ByteBuffer nioBuffer() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean hasArray() { + return true; + } + + @Override + public byte[] array() { + return bytes; + } + + @Override + public int arrayOffset() { + return offset; + } + + @Override + public int position() { + return position; + } + + @Override + public AllocatedBuffer position(int position) { + if (position < 0 || position > length) { + throw new IllegalArgumentException("Invalid position: " + position); + } + this.position = position; + return this; + } + + @Override + public int limit() { + // Relative to offset. + return length; + } + + @Override + public int remaining() { + return length - position; + } + }; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java new file mode 100755 index 0000000000..a969c04c5f --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java @@ -0,0 +1,1076 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.MessageSchema.getMutableUnknownFields; + +import com.google.protobuf.Internal.ProtobufList; +import java.io.IOException; + +/** + * Helper functions to decode protobuf wire format from a byte array. + * + *

Note that these functions don't do boundary check on the byte array but instead rely on Java + * VM to check it. That means parsing rountines utilizing these functions must catch + * IndexOutOfBoundsException and convert it to protobuf's InvalidProtocolBufferException when + * crossing protobuf public API boundaries. + */ +final class ArrayDecoders { + /** + * A helper used to return multiple values in a Java function. Java doesn't natively support + * returning multiple values in a function. Creating a new Object to hold the return values will + * be too expensive. Instead, we pass a Registers instance to functions that want to return + * multiple values and let the function set the return value in this Registers instance instead. + * + *

TODO(xiaofeng): This could be merged into CodedInputStream or CodedInputStreamReader which + * is already being passed through all the parsing rountines. + */ + static final class Registers { + public int int1; + public long long1; + public Object object1; + public final ExtensionRegistryLite extensionRegistry; + + Registers() { + this.extensionRegistry = ExtensionRegistryLite.getEmptyRegistry(); + } + + Registers(ExtensionRegistryLite extensionRegistry) { + if (extensionRegistry == null) { + throw new NullPointerException(); + } + this.extensionRegistry = extensionRegistry; + } + } + + /** + * Decodes a varint. Returns the position after the varint. The decoded varint is stored in + * registers.int1. + */ + static int decodeVarint32(byte[] data, int position, Registers registers) { + int value = data[position++]; + if (value >= 0) { + registers.int1 = value; + return position; + } + return decodeVarint32(value, data, position, registers); + } + + /** Like decodeVarint32 except that the first byte is already read. */ + static int decodeVarint32(int firstByte, byte[] data, int position, Registers registers) { + int value = firstByte & 0x7F; + final byte b2 = data[position++]; + if (b2 >= 0) { + registers.int1 = value | ((int) b2 << 7); + return position; + } + value |= (b2 & 0x7F) << 7; + + final byte b3 = data[position++]; + if (b3 >= 0) { + registers.int1 = value | ((int) b3 << 14); + return position; + } + value |= (b3 & 0x7F) << 14; + + final byte b4 = data[position++]; + if (b4 >= 0) { + registers.int1 = value | ((int) b4 << 21); + return position; + } + value |= (b4 & 0x7F) << 21; + + final byte b5 = data[position++]; + if (b5 >= 0) { + registers.int1 = value | ((int) b5 << 28); + return position; + } + value |= (b5 & 0x7F) << 28; + + while (data[position++] < 0) {} + + registers.int1 = value; + return position; + } + + /** + * Decodes a varint. Returns the position after the varint. The decoded varint is stored in + * registers.long1. + */ + static int decodeVarint64(byte[] data, int position, Registers registers) { + long value = data[position++]; + if (value >= 0) { + registers.long1 = value; + return position; + } else { + return decodeVarint64(value, data, position, registers); + } + } + + /** Like decodeVarint64 except that the first byte is already read. */ + static int decodeVarint64(long firstByte, byte[] data, int position, Registers registers) { + long value = firstByte & 0x7F; + byte next = data[position++]; + int shift = 7; + value |= (long) (next & 0x7F) << 7; + while (next < 0) { + next = data[position++]; + shift += 7; + value |= (long) (next & 0x7F) << shift; + } + registers.long1 = value; + return position; + } + + /** Decodes and returns a fixed32 value. */ + static int decodeFixed32(byte[] data, int position) { + return (data[position] & 0xff) + | ((data[position + 1] & 0xff) << 8) + | ((data[position + 2] & 0xff) << 16) + | ((data[position + 3] & 0xff) << 24); + } + + /** Decodes and returns a fixed64 value. */ + static long decodeFixed64(byte[] data, int position) { + return (data[position] & 0xffL) + | ((data[position + 1] & 0xffL) << 8) + | ((data[position + 2] & 0xffL) << 16) + | ((data[position + 3] & 0xffL) << 24) + | ((data[position + 4] & 0xffL) << 32) + | ((data[position + 5] & 0xffL) << 40) + | ((data[position + 6] & 0xffL) << 48) + | ((data[position + 7] & 0xffL) << 56); + } + + /** Decodes and returns a double value. */ + static double decodeDouble(byte[] data, int position) { + return Double.longBitsToDouble(decodeFixed64(data, position)); + } + + /** Decodes and returns a float value. */ + static float decodeFloat(byte[] data, int position) { + return Float.intBitsToFloat(decodeFixed32(data, position)); + } + + /** Decodes a string value. */ + static int decodeString(byte[] data, int position, Registers registers) + throws InvalidProtocolBufferException { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length == 0) { + registers.object1 = ""; + return position; + } else { + registers.object1 = new String(data, position, length, Internal.UTF_8); + return position + length; + } + } + + /** Decodes a string value with utf8 check. */ + static int decodeStringRequireUtf8(byte[] data, int position, Registers registers) + throws InvalidProtocolBufferException { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length == 0) { + registers.object1 = ""; + return position; + } else { + registers.object1 = Utf8.decodeUtf8(data, position, length); + return position + length; + } + } + + /** Decodes a bytes value. */ + static int decodeBytes(byte[] data, int position, Registers registers) + throws InvalidProtocolBufferException { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length > data.length - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } else if (length == 0) { + registers.object1 = ByteString.EMPTY; + return position; + } else { + registers.object1 = ByteString.copyFrom(data, position, length); + return position + length; + } + } + + /** Decodes a message value. */ + @SuppressWarnings({"unchecked", "rawtypes"}) + static int decodeMessageField( + Schema schema, byte[] data, int position, int limit, Registers registers) throws IOException { + int length = data[position++]; + if (length < 0) { + position = decodeVarint32(length, data, position, registers); + length = registers.int1; + } + if (length < 0 || length > limit - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + Object result = schema.newInstance(); + schema.mergeFrom(result, data, position, position + length, registers); + schema.makeImmutable(result); + registers.object1 = result; + return position + length; + } + + /** Decodes a group value. */ + @SuppressWarnings({"unchecked", "rawtypes"}) + static int decodeGroupField( + Schema schema, byte[] data, int position, int limit, int endGroup, Registers registers) + throws IOException { + // A group field must has a MessageSchema (the only other subclass of Schema is MessageSetSchema + // and it can't be used in group fields). + final MessageSchema messageSchema = (MessageSchema) schema; + Object result = messageSchema.newInstance(); + // It's OK to directly use parseProto2Message since proto3 doesn't have group. + final int endPosition = + messageSchema.parseProto2Message(result, data, position, limit, endGroup, registers); + messageSchema.makeImmutable(result); + registers.object1 = result; + return endPosition; + } + + /** Decodes a repeated 32-bit varint field. Returns the position after all read values. */ + static int decodeVarint32List( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + output.addInt(registers.int1); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + output.addInt(registers.int1); + } + return position; + } + + /** Decodes a repeated 64-bit varint field. Returns the position after all read values. */ + static int decodeVarint64List( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint64(data, position, registers); + output.addLong(registers.long1); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint64(data, nextPosition, registers); + output.addLong(registers.long1); + } + return position; + } + + /** Decodes a repeated fixed32 field. Returns the position after all read values. */ + static int decodeFixed32List( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final IntArrayList output = (IntArrayList) list; + output.addInt(decodeFixed32(data, position)); + position += 4; + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + output.addInt(decodeFixed32(data, nextPosition)); + position = nextPosition + 4; + } + return position; + } + + /** Decodes a repeated fixed64 field. Returns the position after all read values. */ + static int decodeFixed64List( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final LongArrayList output = (LongArrayList) list; + output.addLong(decodeFixed64(data, position)); + position += 8; + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + output.addLong(decodeFixed64(data, nextPosition)); + position = nextPosition + 8; + } + return position; + } + + /** Decodes a repeated float field. Returns the position after all read values. */ + static int decodeFloatList( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final FloatArrayList output = (FloatArrayList) list; + output.addFloat(decodeFloat(data, position)); + position += 4; + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + output.addFloat(decodeFloat(data, nextPosition)); + position = nextPosition + 4; + } + return position; + } + + /** Decodes a repeated double field. Returns the position after all read values. */ + static int decodeDoubleList( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final DoubleArrayList output = (DoubleArrayList) list; + output.addDouble(decodeDouble(data, position)); + position += 8; + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + output.addDouble(decodeDouble(data, nextPosition)); + position = nextPosition + 8; + } + return position; + } + + /** Decodes a repeated boolean field. Returns the position after all read values. */ + static int decodeBoolList( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final BooleanArrayList output = (BooleanArrayList) list; + position = decodeVarint64(data, position, registers); + output.addBoolean(registers.long1 != 0); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint64(data, nextPosition, registers); + output.addBoolean(registers.long1 != 0); + } + return position; + } + + /** Decodes a repeated sint32 field. Returns the position after all read values. */ + static int decodeSInt32List( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + output.addInt(CodedInputStream.decodeZigZag32(registers.int1)); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + output.addInt(CodedInputStream.decodeZigZag32(registers.int1)); + } + return position; + } + + /** Decodes a repeated sint64 field. Returns the position after all read values. */ + static int decodeSInt64List( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint64(data, position, registers); + output.addLong(CodedInputStream.decodeZigZag64(registers.long1)); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint64(data, nextPosition, registers); + output.addLong(CodedInputStream.decodeZigZag64(registers.long1)); + } + return position; + } + + /** Decodes a packed 32-bit varint field. Returns the position after all read values. */ + static int decodePackedVarint32List( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint32(data, position, registers); + output.addInt(registers.int1); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed 64-bit varint field. Returns the position after all read values. */ + static int decodePackedVarint64List( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint64(data, position, registers); + output.addLong(registers.long1); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed fixed32 field. Returns the position after all read values. */ + static int decodePackedFixed32List( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + output.addInt(decodeFixed32(data, position)); + position += 4; + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed fixed64 field. Returns the position after all read values. */ + static int decodePackedFixed64List( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + output.addLong(decodeFixed64(data, position)); + position += 8; + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed float field. Returns the position after all read values. */ + static int decodePackedFloatList( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final FloatArrayList output = (FloatArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + output.addFloat(decodeFloat(data, position)); + position += 4; + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed double field. Returns the position after all read values. */ + static int decodePackedDoubleList( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final DoubleArrayList output = (DoubleArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + output.addDouble(decodeDouble(data, position)); + position += 8; + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed boolean field. Returns the position after all read values. */ + static int decodePackedBoolList( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final BooleanArrayList output = (BooleanArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint64(data, position, registers); + output.addBoolean(registers.long1 != 0); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed sint32 field. Returns the position after all read values. */ + static int decodePackedSInt32List( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint32(data, position, registers); + output.addInt(CodedInputStream.decodeZigZag32(registers.int1)); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed sint64 field. Returns the position after all read values. */ + @SuppressWarnings("unchecked") + static int decodePackedSInt64List( + byte[] data, int position, ProtobufList list, Registers registers) throws IOException { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint64(data, position, registers); + output.addLong(CodedInputStream.decodeZigZag64(registers.long1)); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a repeated string field. Returns the position after all read values. */ + @SuppressWarnings("unchecked") + static int decodeStringList( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) + throws InvalidProtocolBufferException { + final ProtobufList output = (ProtobufList) list; + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length == 0) { + output.add(""); + } else { + String value = new String(data, position, length, Internal.UTF_8); + output.add(value); + position += length; + } + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + final int nextLength = registers.int1; + if (nextLength < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (nextLength == 0) { + output.add(""); + } else { + String value = new String(data, position, nextLength, Internal.UTF_8); + output.add(value); + position += nextLength; + } + } + return position; + } + + /** + * Decodes a repeated string field with utf8 check. Returns the position after all read values. + */ + @SuppressWarnings("unchecked") + static int decodeStringListRequireUtf8( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) + throws InvalidProtocolBufferException { + final ProtobufList output = (ProtobufList) list; + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length == 0) { + output.add(""); + } else { + if (!Utf8.isValidUtf8(data, position, position + length)) { + throw InvalidProtocolBufferException.invalidUtf8(); + } + String value = new String(data, position, length, Internal.UTF_8); + output.add(value); + position += length; + } + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + final int nextLength = registers.int1; + if (nextLength < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (nextLength == 0) { + output.add(""); + } else { + if (!Utf8.isValidUtf8(data, position, position + nextLength)) { + throw InvalidProtocolBufferException.invalidUtf8(); + } + String value = new String(data, position, nextLength, Internal.UTF_8); + output.add(value); + position += nextLength; + } + } + return position; + } + + /** Decodes a repeated bytes field. Returns the position after all read values. */ + @SuppressWarnings("unchecked") + static int decodeBytesList( + int tag, byte[] data, int position, int limit, ProtobufList list, Registers registers) + throws InvalidProtocolBufferException { + final ProtobufList output = (ProtobufList) list; + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length > data.length - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } else if (length == 0) { + output.add(ByteString.EMPTY); + } else { + output.add(ByteString.copyFrom(data, position, length)); + position += length; + } + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + final int nextLength = registers.int1; + if (nextLength < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (nextLength > data.length - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } else if (nextLength == 0) { + output.add(ByteString.EMPTY); + } else { + output.add(ByteString.copyFrom(data, position, nextLength)); + position += nextLength; + } + } + return position; + } + + /** + * Decodes a repeated message field + * + * @return The position of after read all messages + */ + @SuppressWarnings({"unchecked"}) + static int decodeMessageList( + Schema schema, + int tag, + byte[] data, + int position, + int limit, + ProtobufList list, + Registers registers) + throws IOException { + final ProtobufList output = (ProtobufList) list; + position = decodeMessageField(schema, data, position, limit, registers); + output.add(registers.object1); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeMessageField(schema, data, nextPosition, limit, registers); + output.add(registers.object1); + } + return position; + } + + /** + * Decodes a repeated group field + * + * @return The position of after read all groups + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + static int decodeGroupList( + Schema schema, + int tag, + byte[] data, + int position, + int limit, + ProtobufList list, + Registers registers) + throws IOException { + final ProtobufList output = (ProtobufList) list; + final int endgroup = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; + position = decodeGroupField(schema, data, position, limit, endgroup, registers); + output.add(registers.object1); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeGroupField(schema, data, nextPosition, limit, endgroup, registers); + output.add(registers.object1); + } + return position; + } + + static int decodeExtensionOrUnknownField( + int tag, byte[] data, int position, int limit, + Object message, + MessageLite defaultInstance, + UnknownFieldSchema unknownFieldSchema, + Registers registers) + throws IOException { + final int number = tag >>> 3; + GeneratedMessageLite.GeneratedExtension extension = + registers.extensionRegistry.findLiteExtensionByNumber(defaultInstance, number); + if (extension == null) { + return decodeUnknownField( + tag, data, position, limit, getMutableUnknownFields(message), registers); + } else { + ((GeneratedMessageLite.ExtendableMessage) message).ensureExtensionsAreMutable(); + return decodeExtension( + tag, data, position, limit, (GeneratedMessageLite.ExtendableMessage) message, + extension, unknownFieldSchema, registers); + } + } + + static int decodeExtension( + int tag, + byte[] data, + int position, + int limit, + GeneratedMessageLite.ExtendableMessage message, + GeneratedMessageLite.GeneratedExtension extension, + UnknownFieldSchema unknownFieldSchema, + Registers registers) + throws IOException { + final FieldSet extensions = message.extensions; + final int fieldNumber = tag >>> 3; + if (extension.descriptor.isRepeated() && extension.descriptor.isPacked()) { + switch (extension.getLiteType()) { + case DOUBLE: + { + DoubleArrayList list = new DoubleArrayList(); + position = decodePackedDoubleList(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case FLOAT: + { + FloatArrayList list = new FloatArrayList(); + position = decodePackedFloatList(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case INT64: + case UINT64: + { + LongArrayList list = new LongArrayList(); + position = decodePackedVarint64List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case INT32: + case UINT32: + { + IntArrayList list = new IntArrayList(); + position = decodePackedVarint32List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case FIXED64: + case SFIXED64: + { + LongArrayList list = new LongArrayList(); + position = decodePackedFixed64List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case FIXED32: + case SFIXED32: + { + IntArrayList list = new IntArrayList(); + position = decodePackedFixed32List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case BOOL: + { + BooleanArrayList list = new BooleanArrayList(); + position = decodePackedBoolList(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case SINT32: + { + IntArrayList list = new IntArrayList(); + position = decodePackedSInt32List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case SINT64: + { + LongArrayList list = new LongArrayList(); + position = decodePackedSInt64List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case ENUM: + { + IntArrayList list = new IntArrayList(); + position = decodePackedVarint32List(data, position, list, registers); + UnknownFieldSetLite unknownFields = message.unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = null; + } + unknownFields = + SchemaUtil.filterUnknownEnumList( + fieldNumber, + list, + extension.descriptor.getEnumType(), + unknownFields, + unknownFieldSchema); + if (unknownFields != null) { + message.unknownFields = unknownFields; + } + extensions.setField(extension.descriptor, list); + break; + } + default: + throw new IllegalStateException( + "Type cannot be packed: " + extension.descriptor.getLiteType()); + } + } else { + Object value = null; + // Enum is a special case becasue unknown enum values will be put into UnknownFieldSetLite. + if (extension.getLiteType() == WireFormat.FieldType.ENUM) { + position = decodeVarint32(data, position, registers); + Object enumValue = extension.descriptor.getEnumType().findValueByNumber(registers.int1); + if (enumValue == null) { + UnknownFieldSetLite unknownFields = ((GeneratedMessageLite) message).unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = UnknownFieldSetLite.newInstance(); + ((GeneratedMessageLite) message).unknownFields = unknownFields; + } + SchemaUtil.storeUnknownEnum( + fieldNumber, registers.int1, unknownFields, unknownFieldSchema); + return position; + } + // Note, we store the integer value instead of the actual enum object in FieldSet. + // This is also different from full-runtime where we store EnumValueDescriptor. + value = registers.int1; + } else { + switch (extension.getLiteType()) { + case DOUBLE: + value = decodeDouble(data, position); + position += 8; + break; + case FLOAT: + value = decodeFloat(data, position); + position += 4; + break; + case INT64: + case UINT64: + position = decodeVarint64(data, position, registers); + value = registers.long1; + break; + case INT32: + case UINT32: + position = decodeVarint32(data, position, registers); + value = registers.int1; + break; + case FIXED64: + case SFIXED64: + value = decodeFixed64(data, position); + position += 8; + break; + case FIXED32: + case SFIXED32: + value = decodeFixed32(data, position); + position += 4; + break; + case BOOL: + position = decodeVarint64(data, position, registers); + value = (registers.long1 != 0); + break; + case BYTES: + position = decodeBytes(data, position, registers); + value = registers.object1; + break; + case SINT32: + position = decodeVarint32(data, position, registers); + value = CodedInputStream.decodeZigZag32(registers.int1); + break; + case SINT64: + position = decodeVarint64(data, position, registers); + value = CodedInputStream.decodeZigZag64(registers.long1); + break; + case STRING: + position = decodeString(data, position, registers); + value = registers.object1; + break; + case GROUP: + final int endTag = (fieldNumber << 3) | WireFormat.WIRETYPE_END_GROUP; + position = decodeGroupField( + Protobuf.getInstance().schemaFor(extension.getMessageDefaultInstance().getClass()), + data, position, limit, endTag, registers); + value = registers.object1; + break; + + case MESSAGE: + position = decodeMessageField( + Protobuf.getInstance().schemaFor(extension.getMessageDefaultInstance().getClass()), + data, position, limit, registers); + value = registers.object1; + break; + + case ENUM: + throw new IllegalStateException("Shouldn't reach here."); + } + } + if (extension.isRepeated()) { + extensions.addRepeatedField(extension.descriptor, value); + } else { + switch (extension.getLiteType()) { + case MESSAGE: + case GROUP: + Object oldValue = extensions.getField(extension.descriptor); + if (oldValue != null) { + value = Internal.mergeMessage(oldValue, value); + } + break; + default: + break; + } + extensions.setField(extension.descriptor, value); + } + } + return position; + } + + /** Decodes an unknown field. */ + static int decodeUnknownField( + int tag, + byte[] data, + int position, + int limit, + UnknownFieldSetLite unknownFields, + Registers registers) + throws InvalidProtocolBufferException { + if (WireFormat.getTagFieldNumber(tag) == 0) { + throw InvalidProtocolBufferException.invalidTag(); + } + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + position = decodeVarint64(data, position, registers); + unknownFields.storeField(tag, registers.long1); + return position; + case WireFormat.WIRETYPE_FIXED32: + unknownFields.storeField(tag, decodeFixed32(data, position)); + return position + 4; + case WireFormat.WIRETYPE_FIXED64: + unknownFields.storeField(tag, decodeFixed64(data, position)); + return position + 8; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length > data.length - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } else if (length == 0) { + unknownFields.storeField(tag, ByteString.EMPTY); + } else { + unknownFields.storeField(tag, ByteString.copyFrom(data, position, length)); + } + return position + length; + case WireFormat.WIRETYPE_START_GROUP: + final UnknownFieldSetLite child = UnknownFieldSetLite.newInstance(); + final int endGroup = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; + int lastTag = 0; + while (position < limit) { + position = decodeVarint32(data, position, registers); + lastTag = registers.int1; + if (lastTag == endGroup) { + break; + } + position = decodeUnknownField(lastTag, data, position, limit, child, registers); + } + if (position > limit || lastTag != endGroup) { + throw InvalidProtocolBufferException.parseFailure(); + } + unknownFields.storeField(tag, child); + return position; + default: + throw InvalidProtocolBufferException.invalidTag(); + } + } + + /** Skips an unknown field. */ + static int skipField(int tag, byte[] data, int position, int limit, Registers registers) + throws InvalidProtocolBufferException { + if (WireFormat.getTagFieldNumber(tag) == 0) { + throw InvalidProtocolBufferException.invalidTag(); + } + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + position = decodeVarint64(data, position, registers); + return position; + case WireFormat.WIRETYPE_FIXED32: + return position + 4; + case WireFormat.WIRETYPE_FIXED64: + return position + 8; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + position = decodeVarint32(data, position, registers); + return position + registers.int1; + case WireFormat.WIRETYPE_START_GROUP: + final int endGroup = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; + int lastTag = 0; + while (position < limit) { + position = decodeVarint32(data, position, registers); + lastTag = registers.int1; + if (lastTag == endGroup) { + break; + } + position = skipField(lastTag, data, position, limit, registers); + } + if (position > limit || lastTag != endGroup) { + throw InvalidProtocolBufferException.parseFailure(); + } + return position; + default: + throw InvalidProtocolBufferException.invalidTag(); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/BinaryReader.java b/java/core/src/main/java/com/google/protobuf/BinaryReader.java new file mode 100755 index 0000000000..d64574c2a5 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/BinaryReader.java @@ -0,0 +1,1729 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.WireFormat.FIXED32_SIZE; +import static com.google.protobuf.WireFormat.FIXED64_SIZE; +import static com.google.protobuf.WireFormat.WIRETYPE_END_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED32; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED64; +import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; +import static com.google.protobuf.WireFormat.WIRETYPE_START_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_VARINT; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; + +/** + * A {@link Reader} that reads from a buffer containing a message serialized with the binary + * protocol. + */ +@ExperimentalApi +abstract class BinaryReader implements Reader { + private static final int FIXED32_MULTIPLE_MASK = FIXED32_SIZE - 1; + private static final int FIXED64_MULTIPLE_MASK = FIXED64_SIZE - 1; + + /** + * Creates a new reader using the given {@code buffer} as input. + * + * @param buffer the input buffer. The buffer (including position, limit, etc.) will not be + * modified. To increment the buffer position after the read completes, use the value returned + * by {@link #getTotalBytesRead()}. + * @param bufferIsImmutable if {@code true} the reader assumes that the content of {@code buffer} + * will never change and any allocated {@link ByteString} instances will by directly wrap + * slices of {@code buffer}. + * @return the reader + */ + public static BinaryReader newInstance(ByteBuffer buffer, boolean bufferIsImmutable) { + if (buffer.hasArray()) { + // TODO(nathanmittler): Add support for unsafe operations. + return new SafeHeapReader(buffer, bufferIsImmutable); + } + // TODO(nathanmittler): Add support for direct buffers + throw new IllegalArgumentException("Direct buffers not yet supported"); + } + + /** Only allow subclassing for inner classes. */ + private BinaryReader() {} + + /** Returns the total number of bytes read so far from the input buffer. */ + public abstract int getTotalBytesRead(); + + @Override + public boolean shouldDiscardUnknownFields() { + return false; + } + + /** + * A {@link BinaryReader} implementation that operates on a heap {@link ByteBuffer}. Uses only + * safe operations on the underlying array. + */ + private static final class SafeHeapReader extends BinaryReader { + private final boolean bufferIsImmutable; + private final byte[] buffer; + private int pos; + private final int initialPos; + private int limit; + private int tag; + private int endGroupTag; + + public SafeHeapReader(ByteBuffer bytebuf, boolean bufferIsImmutable) { + this.bufferIsImmutable = bufferIsImmutable; + buffer = bytebuf.array(); + initialPos = pos = bytebuf.arrayOffset() + bytebuf.position(); + limit = bytebuf.arrayOffset() + bytebuf.limit(); + } + + private boolean isAtEnd() { + return pos == limit; + } + + @Override + public int getTotalBytesRead() { + return pos - initialPos; + } + + @Override + public int getFieldNumber() throws IOException { + if (isAtEnd()) { + return Reader.READ_DONE; + } + tag = readVarint32(); + if (tag == endGroupTag) { + return Reader.READ_DONE; + } + return WireFormat.getTagFieldNumber(tag); + } + + @Override + public int getTag() { + return tag; + } + + @Override + public boolean skipField() throws IOException { + if (isAtEnd() || tag == endGroupTag) { + return false; + } + + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_VARINT: + skipVarint(); + return true; + case WIRETYPE_FIXED64: + skipBytes(FIXED64_SIZE); + return true; + case WIRETYPE_LENGTH_DELIMITED: + skipBytes(readVarint32()); + return true; + case WIRETYPE_FIXED32: + skipBytes(FIXED32_SIZE); + return true; + case WIRETYPE_START_GROUP: + skipGroup(); + return true; + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + + @Override + public double readDouble() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return Double.longBitsToDouble(readLittleEndian64()); + } + + @Override + public float readFloat() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return Float.intBitsToFloat(readLittleEndian32()); + } + + @Override + public long readUInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint64(); + } + + @Override + public long readInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint64(); + } + + @Override + public int readInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint32(); + } + + @Override + public long readFixed64() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return readLittleEndian64(); + } + + @Override + public int readFixed32() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return readLittleEndian32(); + } + + @Override + public boolean readBool() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint32() != 0; + } + + @Override + public String readString() throws IOException { + return readStringInternal(false); + } + + @Override + public String readStringRequireUtf8() throws IOException { + return readStringInternal(true); + } + + public String readStringInternal(boolean requireUtf8) throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + final int size = readVarint32(); + if (size == 0) { + return ""; + } + + requireBytes(size); + if (requireUtf8 && !Utf8.isValidUtf8(buffer, pos, pos + size)) { + throw InvalidProtocolBufferException.invalidUtf8(); + } + String result = new String(buffer, pos, size, Internal.UTF_8); + pos += size; + return result; + } + + @Override + public T readMessage(Class clazz, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return readMessage(Protobuf.getInstance().schemaFor(clazz), extensionRegistry); + } + + @Override + public T readMessageBySchemaWithCheck( + Schema schema, ExtensionRegistryLite extensionRegistry) throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return readMessage(schema, extensionRegistry); + } + + private T readMessage(Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + int size = readVarint32(); + requireBytes(size); + + // Update the limit. + int prevLimit = limit; + int newLimit = pos + size; + limit = newLimit; + + try { + // Allocate and read the message. + T message = schema.newInstance(); + schema.mergeFrom(message, this, extensionRegistry); + schema.makeImmutable(message); + + if (pos != newLimit) { + throw InvalidProtocolBufferException.parseFailure(); + } + return message; + } finally { + // Restore the limit. + limit = prevLimit; + } + } + + @Override + public T readGroup(Class clazz, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_START_GROUP); + return readGroup(Protobuf.getInstance().schemaFor(clazz), extensionRegistry); + } + + @Override + public T readGroupBySchemaWithCheck( + Schema schema, ExtensionRegistryLite extensionRegistry) throws IOException { + requireWireType(WIRETYPE_START_GROUP); + return readGroup(schema, extensionRegistry); + } + + private T readGroup(Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + int prevEndGroupTag = endGroupTag; + endGroupTag = WireFormat.makeTag(WireFormat.getTagFieldNumber(tag), WIRETYPE_END_GROUP); + + try { + // Allocate and read the message. + T message = schema.newInstance(); + schema.mergeFrom(message, this, extensionRegistry); + schema.makeImmutable(message); + + if (tag != endGroupTag) { + throw InvalidProtocolBufferException.parseFailure(); + } + return message; + } finally { + // Restore the old end group tag. + endGroupTag = prevEndGroupTag; + } + } + + @Override + public ByteString readBytes() throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + int size = readVarint32(); + if (size == 0) { + return ByteString.EMPTY; + } + + requireBytes(size); + ByteString bytes = + bufferIsImmutable + ? ByteString.wrap(buffer, pos, size) + : ByteString.copyFrom(buffer, pos, size); + pos += size; + return bytes; + } + + @Override + public int readUInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint32(); + } + + @Override + public int readEnum() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint32(); + } + + @Override + public int readSFixed32() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return readLittleEndian32(); + } + + @Override + public long readSFixed64() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return readLittleEndian64(); + } + + @Override + public int readSInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return CodedInputStream.decodeZigZag32(readVarint32()); + } + + @Override + public long readSInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return CodedInputStream.decodeZigZag64(readVarint64()); + } + + @Override + public void readDoubleList(List target) throws IOException { + if (target instanceof DoubleArrayList) { + DoubleArrayList plist = (DoubleArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addDouble(Double.longBitsToDouble(readLittleEndian64_NoCheck())); + } + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addDouble(readDouble()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(Double.longBitsToDouble(readLittleEndian64_NoCheck())); + } + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(readDouble()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFloatList(List target) throws IOException { + if (target instanceof FloatArrayList) { + FloatArrayList plist = (FloatArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addFloat(Float.intBitsToFloat(readLittleEndian32_NoCheck())); + } + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addFloat(readFloat()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(Float.intBitsToFloat(readLittleEndian32_NoCheck())); + } + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(readFloat()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readUInt64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(readVarint64()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(readUInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint64()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readUInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readInt64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(readVarint64()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(readInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint64()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readInt32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readVarint32()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(readInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint32()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFixed64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(readLittleEndian64_NoCheck()); + } + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addLong(readFixed64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readLittleEndian64_NoCheck()); + } + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(readFixed64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFixed32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readLittleEndian32_NoCheck()); + } + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addInt(readFixed32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readLittleEndian32_NoCheck()); + } + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(readFixed32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readBoolList(List target) throws IOException { + if (target instanceof BooleanArrayList) { + BooleanArrayList plist = (BooleanArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addBoolean(readVarint32() != 0); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addBoolean(readBool()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint32() != 0); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readBool()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readStringList(List target) throws IOException { + readStringListInternal(target, false); + } + + @Override + public void readStringListRequireUtf8(List target) throws IOException { + readStringListInternal(target, true); + } + + public void readStringListInternal(List target, boolean requireUtf8) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + + if (target instanceof LazyStringList && !requireUtf8) { + LazyStringList lazyList = (LazyStringList) target; + while (true) { + lazyList.add(readBytes()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } else { + while (true) { + target.add(readStringInternal(requireUtf8)); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } + } + + @Override + public void readMessageList( + List target, Class targetType, ExtensionRegistryLite extensionRegistry) + throws IOException { + final Schema schema = Protobuf.getInstance().schemaFor(targetType); + readMessageList(target, schema, extensionRegistry); + } + + @Override + public void readMessageList( + List target, Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + final int listTag = tag; + while (true) { + target.add(readMessage(schema, extensionRegistry)); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != listTag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } + + @Override + public void readGroupList( + List target, Class targetType, ExtensionRegistryLite extensionRegistry) + throws IOException { + final Schema schema = Protobuf.getInstance().schemaFor(targetType); + readGroupList(target, schema, extensionRegistry); + } + + @Override + public void readGroupList( + List target, Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_START_GROUP) { + throw InvalidProtocolBufferException.invalidWireType(); + } + final int listTag = tag; + while (true) { + target.add(readGroup(schema, extensionRegistry)); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != listTag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } + + @Override + public void readBytesList(List target) throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + + while (true) { + target.add(readBytes()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } + + @Override + public void readUInt32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readVarint32()); + } + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(readUInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint32()); + } + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readUInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readEnumList(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readVarint32()); + } + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(readEnum()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint32()); + } + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readEnum()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSFixed32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readLittleEndian32_NoCheck()); + } + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addInt(readSFixed32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readLittleEndian32_NoCheck()); + } + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(readSFixed32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSFixed64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(readLittleEndian64_NoCheck()); + } + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addLong(readSFixed64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readLittleEndian64_NoCheck()); + } + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(readSFixed64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSInt32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(CodedInputStream.decodeZigZag32(readVarint32())); + } + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(readSInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(CodedInputStream.decodeZigZag32(readVarint32())); + } + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readSInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSInt64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(CodedInputStream.decodeZigZag64(readVarint64())); + } + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(readSInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(CodedInputStream.decodeZigZag64(readVarint64())); + } + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readSInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @SuppressWarnings("unchecked") + @Override + public void readMap( + Map target, + MapEntryLite.Metadata metadata, + ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + int size = readVarint32(); + requireBytes(size); + + // Update the limit. + int prevLimit = limit; + int newLimit = pos + size; + limit = newLimit; + + try { + K key = metadata.defaultKey; + V value = metadata.defaultValue; + while (true) { + int number = getFieldNumber(); + if (number == READ_DONE) { + break; + } + try { + switch (number) { + case 1: + key = (K) readField(metadata.keyType, null, null); + break; + case 2: + value = + (V) + readField( + metadata.valueType, + metadata.defaultValue.getClass(), + extensionRegistry); + break; + default: + if (!skipField()) { + throw new InvalidProtocolBufferException("Unable to parse map entry."); + } + break; + } + } catch (InvalidProtocolBufferException.InvalidWireTypeException ignore) { + // the type doesn't match, skip the field. + if (!skipField()) { + throw new InvalidProtocolBufferException("Unable to parse map entry."); + } + } + } + target.put(key, value); + } finally { + // Restore the limit. + limit = prevLimit; + } + } + + private Object readField( + WireFormat.FieldType fieldType, + Class messageType, + ExtensionRegistryLite extensionRegistry) + throws IOException { + switch (fieldType) { + case BOOL: + return readBool(); + case BYTES: + return readBytes(); + case DOUBLE: + return readDouble(); + case ENUM: + return readEnum(); + case FIXED32: + return readFixed32(); + case FIXED64: + return readFixed64(); + case FLOAT: + return readFloat(); + case INT32: + return readInt32(); + case INT64: + return readInt64(); + case MESSAGE: + return readMessage(messageType, extensionRegistry); + case SFIXED32: + return readSFixed32(); + case SFIXED64: + return readSFixed64(); + case SINT32: + return readSInt32(); + case SINT64: + return readSInt64(); + case STRING: + return readStringRequireUtf8(); + case UINT32: + return readUInt32(); + case UINT64: + return readUInt64(); + default: + throw new RuntimeException("unsupported field type."); + } + } + + /** Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits. */ + private int readVarint32() throws IOException { + // See implementation notes for readRawVarint64 + int i = pos; + + if (limit == pos) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + + int x; + if ((x = buffer[i++]) >= 0) { + pos = i; + return x; + } else if (limit - i < 9) { + return (int) readVarint64SlowPath(); + } else if ((x ^= (buffer[i++] << 7)) < 0) { + x ^= (~0 << 7); + } else if ((x ^= (buffer[i++] << 14)) >= 0) { + x ^= (~0 << 7) ^ (~0 << 14); + } else if ((x ^= (buffer[i++] << 21)) < 0) { + x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21); + } else { + int y = buffer[i++]; + x ^= y << 28; + x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21) ^ (~0 << 28); + if (y < 0 + && buffer[i++] < 0 + && buffer[i++] < 0 + && buffer[i++] < 0 + && buffer[i++] < 0 + && buffer[i++] < 0) { + throw InvalidProtocolBufferException.malformedVarint(); + } + } + pos = i; + return x; + } + + public long readVarint64() throws IOException { + // Implementation notes: + // + // Optimized for one-byte values, expected to be common. + // The particular code below was selected from various candidates + // empirically, by winning VarintBenchmark. + // + // Sign extension of (signed) Java bytes is usually a nuisance, but + // we exploit it here to more easily obtain the sign of bytes read. + // Instead of cleaning up the sign extension bits by masking eagerly, + // we delay until we find the final (positive) byte, when we clear all + // accumulated bits with one xor. We depend on javac to constant fold. + int i = pos; + + if (limit == i) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + + final byte[] buffer = this.buffer; + long x; + int y; + if ((y = buffer[i++]) >= 0) { + pos = i; + return y; + } else if (limit - i < 9) { + return readVarint64SlowPath(); + } else if ((y ^= (buffer[i++] << 7)) < 0) { + x = y ^ (~0 << 7); + } else if ((y ^= (buffer[i++] << 14)) >= 0) { + x = y ^ ((~0 << 7) ^ (~0 << 14)); + } else if ((y ^= (buffer[i++] << 21)) < 0) { + x = y ^ ((~0 << 7) ^ (~0 << 14) ^ (~0 << 21)); + } else if ((x = y ^ ((long) buffer[i++] << 28)) >= 0L) { + x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28); + } else if ((x ^= ((long) buffer[i++] << 35)) < 0L) { + x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35); + } else if ((x ^= ((long) buffer[i++] << 42)) >= 0L) { + x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35) ^ (~0L << 42); + } else if ((x ^= ((long) buffer[i++] << 49)) < 0L) { + x ^= + (~0L << 7) + ^ (~0L << 14) + ^ (~0L << 21) + ^ (~0L << 28) + ^ (~0L << 35) + ^ (~0L << 42) + ^ (~0L << 49); + } else { + x ^= ((long) buffer[i++] << 56); + x ^= + (~0L << 7) + ^ (~0L << 14) + ^ (~0L << 21) + ^ (~0L << 28) + ^ (~0L << 35) + ^ (~0L << 42) + ^ (~0L << 49) + ^ (~0L << 56); + if (x < 0L) { + if (buffer[i++] < 0L) { + throw InvalidProtocolBufferException.malformedVarint(); + } + } + } + pos = i; + return x; + } + + private long readVarint64SlowPath() throws IOException { + long result = 0; + for (int shift = 0; shift < 64; shift += 7) { + final byte b = readByte(); + result |= (long) (b & 0x7F) << shift; + if ((b & 0x80) == 0) { + return result; + } + } + throw InvalidProtocolBufferException.malformedVarint(); + } + + private byte readByte() throws IOException { + if (pos == limit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return buffer[pos++]; + } + + private int readLittleEndian32() throws IOException { + requireBytes(FIXED32_SIZE); + return readLittleEndian32_NoCheck(); + } + + private long readLittleEndian64() throws IOException { + requireBytes(FIXED64_SIZE); + return readLittleEndian64_NoCheck(); + } + + private int readLittleEndian32_NoCheck() { + int p = pos; + final byte[] buffer = this.buffer; + pos = p + FIXED32_SIZE; + return (((buffer[p] & 0xff)) + | ((buffer[p + 1] & 0xff) << 8) + | ((buffer[p + 2] & 0xff) << 16) + | ((buffer[p + 3] & 0xff) << 24)); + } + + private long readLittleEndian64_NoCheck() { + int p = pos; + final byte[] buffer = this.buffer; + pos = p + FIXED64_SIZE; + return (((buffer[p] & 0xffL)) + | ((buffer[p + 1] & 0xffL) << 8) + | ((buffer[p + 2] & 0xffL) << 16) + | ((buffer[p + 3] & 0xffL) << 24) + | ((buffer[p + 4] & 0xffL) << 32) + | ((buffer[p + 5] & 0xffL) << 40) + | ((buffer[p + 6] & 0xffL) << 48) + | ((buffer[p + 7] & 0xffL) << 56)); + } + + private void skipVarint() throws IOException { + if (limit - pos >= 10) { + final byte[] buffer = this.buffer; + int p = pos; + for (int i = 0; i < 10; i++) { + if (buffer[p++] >= 0) { + pos = p; + return; + } + } + } + skipVarintSlowPath(); + } + + private void skipVarintSlowPath() throws IOException { + for (int i = 0; i < 10; i++) { + if (readByte() >= 0) { + return; + } + } + throw InvalidProtocolBufferException.malformedVarint(); + } + + private void skipBytes(final int size) throws IOException { + requireBytes(size); + + pos += size; + } + + private void skipGroup() throws IOException { + int prevEndGroupTag = endGroupTag; + endGroupTag = WireFormat.makeTag(WireFormat.getTagFieldNumber(tag), WIRETYPE_END_GROUP); + while (true) { + if (getFieldNumber() == READ_DONE || !skipField()) { + break; + } + } + if (tag != endGroupTag) { + throw InvalidProtocolBufferException.parseFailure(); + } + endGroupTag = prevEndGroupTag; + } + + private void requireBytes(int size) throws IOException { + if (size < 0 || size > (limit - pos)) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + } + + private void requireWireType(int requiredWireType) throws IOException { + if (WireFormat.getTagWireType(tag) != requiredWireType) { + throw InvalidProtocolBufferException.invalidWireType(); + } + } + + private void verifyPackedFixed64Length(int bytes) throws IOException { + requireBytes(bytes); + if ((bytes & FIXED64_MULTIPLE_MASK) != 0) { + // Require that the number of bytes be a multiple of 8. + throw InvalidProtocolBufferException.parseFailure(); + } + } + + private void verifyPackedFixed32Length(int bytes) throws IOException { + requireBytes(bytes); + if ((bytes & FIXED32_MULTIPLE_MASK) != 0) { + // Require that the number of bytes be a multiple of 4. + throw InvalidProtocolBufferException.parseFailure(); + } + } + + private void requirePosition(int expectedPosition) throws IOException { + if (pos != expectedPosition) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/BinaryWriter.java b/java/core/src/main/java/com/google/protobuf/BinaryWriter.java new file mode 100755 index 0000000000..94259ecd32 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/BinaryWriter.java @@ -0,0 +1,3071 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; +import static com.google.protobuf.WireFormat.FIXED32_SIZE; +import static com.google.protobuf.WireFormat.FIXED64_SIZE; +import static com.google.protobuf.WireFormat.MAX_VARINT32_SIZE; +import static com.google.protobuf.WireFormat.MAX_VARINT64_SIZE; +import static com.google.protobuf.WireFormat.MESSAGE_SET_ITEM; +import static com.google.protobuf.WireFormat.MESSAGE_SET_MESSAGE; +import static com.google.protobuf.WireFormat.MESSAGE_SET_TYPE_ID; +import static com.google.protobuf.WireFormat.WIRETYPE_END_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED32; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED64; +import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; +import static com.google.protobuf.WireFormat.WIRETYPE_START_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_VARINT; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayDeque; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +/** + * A protobuf writer that serializes messages in their binary form. Messages are serialized in + * reverse in order to avoid calculating the serialized size of each nested message. Since the + * message size is not known in advance, the writer employs a strategy of chunking and buffer + * chaining. Buffers are allocated as-needed by a provided {@link BufferAllocator}. Once writing is + * finished, the application can access the buffers in forward-writing order by calling {@link + * #complete()}. + * + *

Once {@link #complete()} has been called, the writer can not be reused for additional writes. + * The {@link #getTotalBytesWritten()} will continue to reflect the total of the write and will not + * be reset. + */ +@ExperimentalApi +abstract class BinaryWriter extends ByteOutput implements Writer { + public static final int DEFAULT_CHUNK_SIZE = 4096; + + private final BufferAllocator alloc; + private final int chunkSize; + + final ArrayDeque buffers = new ArrayDeque(4); + int totalDoneBytes; + + /** + * Creates a new {@link BinaryWriter} that will allocate heap buffers of {@link + * #DEFAULT_CHUNK_SIZE} as necessary. + */ + public static BinaryWriter newHeapInstance(BufferAllocator alloc) { + return newHeapInstance(alloc, DEFAULT_CHUNK_SIZE); + } + + /** + * Creates a new {@link BinaryWriter} that will allocate heap buffers of {@code chunkSize} as + * necessary. + */ + public static BinaryWriter newHeapInstance(BufferAllocator alloc, int chunkSize) { + return isUnsafeHeapSupported() + ? newUnsafeHeapInstance(alloc, chunkSize) + : newSafeHeapInstance(alloc, chunkSize); + } + + /** + * Creates a new {@link BinaryWriter} that will allocate direct (i.e. non-heap) buffers of {@link + * #DEFAULT_CHUNK_SIZE} as necessary. + */ + public static BinaryWriter newDirectInstance(BufferAllocator alloc) { + return newDirectInstance(alloc, DEFAULT_CHUNK_SIZE); + } + + /** + * Creates a new {@link BinaryWriter} that will allocate direct (i.e. non-heap) buffers of {@code + * chunkSize} as necessary. + */ + public static BinaryWriter newDirectInstance(BufferAllocator alloc, int chunkSize) { + return isUnsafeDirectSupported() + ? newUnsafeDirectInstance(alloc, chunkSize) + : newSafeDirectInstance(alloc, chunkSize); + } + + static boolean isUnsafeHeapSupported() { + return UnsafeHeapWriter.isSupported(); + } + + static boolean isUnsafeDirectSupported() { + return UnsafeDirectWriter.isSupported(); + } + + static BinaryWriter newSafeHeapInstance(BufferAllocator alloc, int chunkSize) { + return new SafeHeapWriter(alloc, chunkSize); + } + + static BinaryWriter newUnsafeHeapInstance(BufferAllocator alloc, int chunkSize) { + if (!isUnsafeHeapSupported()) { + throw new UnsupportedOperationException("Unsafe operations not supported"); + } + return new UnsafeHeapWriter(alloc, chunkSize); + } + + static BinaryWriter newSafeDirectInstance(BufferAllocator alloc, int chunkSize) { + return new SafeDirectWriter(alloc, chunkSize); + } + + static BinaryWriter newUnsafeDirectInstance(BufferAllocator alloc, int chunkSize) { + if (!isUnsafeDirectSupported()) { + throw new UnsupportedOperationException("Unsafe operations not supported"); + } + return new UnsafeDirectWriter(alloc, chunkSize); + } + + /** Only allow subclassing for inner classes. */ + private BinaryWriter(BufferAllocator alloc, int chunkSize) { + if (chunkSize <= 0) { + throw new IllegalArgumentException("chunkSize must be > 0"); + } + this.alloc = checkNotNull(alloc, "alloc"); + this.chunkSize = chunkSize; + } + + @Override + public final FieldOrder fieldOrder() { + return FieldOrder.DESCENDING; + } + + /** + * Completes the write operation and returns a queue of {@link AllocatedBuffer} objects in + * forward-writing order. This method should only be called once. + * + *

After calling this method, the writer can not be reused. Create a new writer for future + * writes. + */ + public final Queue complete() { + finishCurrentBuffer(); + return buffers; + } + + @Override + public final void writeSFixed32(int fieldNumber, int value) throws IOException { + writeFixed32(fieldNumber, value); + } + + @Override + public final void writeInt64(int fieldNumber, long value) throws IOException { + writeUInt64(fieldNumber, value); + } + + @Override + public final void writeSFixed64(int fieldNumber, long value) throws IOException { + writeFixed64(fieldNumber, value); + } + + @Override + public final void writeFloat(int fieldNumber, float value) throws IOException { + writeFixed32(fieldNumber, Float.floatToRawIntBits(value)); + } + + @Override + public final void writeDouble(int fieldNumber, double value) throws IOException { + writeFixed64(fieldNumber, Double.doubleToRawLongBits(value)); + } + + @Override + public final void writeEnum(int fieldNumber, int value) throws IOException { + writeInt32(fieldNumber, value); + } + + @Override + public final void writeInt32List(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof IntArrayList) { + writeInt32List_Internal(fieldNumber, (IntArrayList) list, packed); + } else { + writeInt32List_Internal(fieldNumber, list, packed); + } + } + + private final void writeInt32List_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeInt32(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeInt32(fieldNumber, list.get(i)); + } + } + } + + private final void writeInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeInt32(list.getInt(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeInt32(fieldNumber, list.getInt(i)); + } + } + } + + @Override + public final void writeFixed32List(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof IntArrayList) { + writeFixed32List_Internal(fieldNumber, (IntArrayList) list, packed); + } else { + writeFixed32List_Internal(fieldNumber, list, packed); + } + } + + private final void writeFixed32List_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(fieldNumber, list.get(i)); + } + } + } + + private final void writeFixed32List_Internal(int fieldNumber, IntArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(list.getInt(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(fieldNumber, list.getInt(i)); + } + } + } + + @Override + public final void writeInt64List(int fieldNumber, List list, boolean packed) + throws IOException { + writeUInt64List(fieldNumber, list, packed); + } + + @Override + public final void writeUInt64List(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof LongArrayList) { + writeUInt64List_Internal(fieldNumber, (LongArrayList) list, packed); + } else { + writeUInt64List_Internal(fieldNumber, list, packed); + } + } + + private final void writeUInt64List_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeVarint64(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeUInt64(fieldNumber, list.get(i)); + } + } + } + + private final void writeUInt64List_Internal(int fieldNumber, LongArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeVarint64(list.getLong(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeUInt64(fieldNumber, list.getLong(i)); + } + } + } + + @Override + public final void writeFixed64List(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof LongArrayList) { + writeFixed64List_Internal(fieldNumber, (LongArrayList) list, packed); + } else { + writeFixed64List_Internal(fieldNumber, list, packed); + } + } + + private final void writeFixed64List_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(fieldNumber, list.get(i)); + } + } + } + + private final void writeFixed64List_Internal(int fieldNumber, LongArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(list.getLong(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(fieldNumber, list.getLong(i)); + } + } + } + + @Override + public final void writeFloatList(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof FloatArrayList) { + writeFloatList_Internal(fieldNumber, (FloatArrayList) list, packed); + } else { + writeFloatList_Internal(fieldNumber, list, packed); + } + } + + private final void writeFloatList_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(Float.floatToRawIntBits(list.get(i))); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFloat(fieldNumber, list.get(i)); + } + } + } + + private final void writeFloatList_Internal(int fieldNumber, FloatArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(Float.floatToRawIntBits(list.getFloat(i))); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFloat(fieldNumber, list.getFloat(i)); + } + } + } + + @Override + public final void writeDoubleList(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof DoubleArrayList) { + writeDoubleList_Internal(fieldNumber, (DoubleArrayList) list, packed); + } else { + writeDoubleList_Internal(fieldNumber, list, packed); + } + } + + private final void writeDoubleList_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(Double.doubleToRawLongBits(list.get(i))); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeDouble(fieldNumber, list.get(i)); + } + } + } + + private final void writeDoubleList_Internal(int fieldNumber, DoubleArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(Double.doubleToRawLongBits(list.getDouble(i))); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeDouble(fieldNumber, list.getDouble(i)); + } + } + } + + @Override + public final void writeEnumList(int fieldNumber, List list, boolean packed) + throws IOException { + writeInt32List(fieldNumber, list, packed); + } + + @Override + public final void writeBoolList(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof BooleanArrayList) { + writeBoolList_Internal(fieldNumber, (BooleanArrayList) list, packed); + } else { + writeBoolList_Internal(fieldNumber, list, packed); + } + } + + private final void writeBoolList_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + list.size()); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeBool(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeBool(fieldNumber, list.get(i)); + } + } + } + + private final void writeBoolList_Internal(int fieldNumber, BooleanArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + list.size()); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeBool(list.getBoolean(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeBool(fieldNumber, list.getBoolean(i)); + } + } + } + + @Override + public final void writeStringList(int fieldNumber, List list) throws IOException { + if (list instanceof LazyStringList) { + final LazyStringList lazyList = (LazyStringList) list; + for (int i = list.size() - 1; i >= 0; i--) { + writeLazyString(fieldNumber, lazyList.getRaw(i)); + } + } else { + for (int i = list.size() - 1; i >= 0; i--) { + writeString(fieldNumber, list.get(i)); + } + } + } + + private void writeLazyString(int fieldNumber, Object value) throws IOException { + if (value instanceof String) { + writeString(fieldNumber, (String) value); + } else { + writeBytes(fieldNumber, (ByteString) value); + } + } + + @Override + public final void writeBytesList(int fieldNumber, List list) throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeBytes(fieldNumber, list.get(i)); + } + } + + @Override + public final void writeUInt32List(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof IntArrayList) { + writeUInt32List_Internal(fieldNumber, (IntArrayList) list, packed); + } else { + writeUInt32List_Internal(fieldNumber, list, packed); + } + } + + private final void writeUInt32List_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeVarint32(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeUInt32(fieldNumber, list.get(i)); + } + } + } + + private final void writeUInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeVarint32(list.getInt(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeUInt32(fieldNumber, list.getInt(i)); + } + } + } + + @Override + public final void writeSFixed32List(int fieldNumber, List list, boolean packed) + throws IOException { + writeFixed32List(fieldNumber, list, packed); + } + + @Override + public final void writeSFixed64List(int fieldNumber, List list, boolean packed) + throws IOException { + writeFixed64List(fieldNumber, list, packed); + } + + @Override + public final void writeSInt32List(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof IntArrayList) { + writeSInt32List_Internal(fieldNumber, (IntArrayList) list, packed); + } else { + writeSInt32List_Internal(fieldNumber, list, packed); + } + } + + private final void writeSInt32List_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt32(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt32(fieldNumber, list.get(i)); + } + } + } + + private final void writeSInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt32(list.getInt(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt32(fieldNumber, list.getInt(i)); + } + } + } + + @Override + public final void writeSInt64List(int fieldNumber, List list, boolean packed) + throws IOException { + if (list instanceof LongArrayList) { + writeSInt64List_Internal(fieldNumber, (LongArrayList) list, packed); + } else { + writeSInt64List_Internal(fieldNumber, list, packed); + } + } + + private static final int MAP_KEY_NUMBER = 1; + private static final int MAP_VALUE_NUMBER = 2; + + @Override + public void writeMap(int fieldNumber, MapEntryLite.Metadata metadata, Map map) + throws IOException { + // TODO(liujisi): Reverse write those entries. + for (Map.Entry entry : map.entrySet()) { + int prevBytes = getTotalBytesWritten(); + writeMapEntryField(this, MAP_VALUE_NUMBER, metadata.valueType, entry.getValue()); + writeMapEntryField(this, MAP_KEY_NUMBER, metadata.keyType, entry.getKey()); + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + } + + static final void writeMapEntryField( + Writer writer, int fieldNumber, WireFormat.FieldType fieldType, Object object) + throws IOException { + switch (fieldType) { + case BOOL: + writer.writeBool(fieldNumber, (Boolean) object); + break; + case FIXED32: + writer.writeFixed32(fieldNumber, (Integer) object); + break; + case FIXED64: + writer.writeFixed64(fieldNumber, (Long) object); + break; + case INT32: + writer.writeInt32(fieldNumber, (Integer) object); + break; + case INT64: + writer.writeInt64(fieldNumber, (Long) object); + break; + case SFIXED32: + writer.writeSFixed32(fieldNumber, (Integer) object); + break; + case SFIXED64: + writer.writeSFixed64(fieldNumber, (Long) object); + break; + case SINT32: + writer.writeSInt32(fieldNumber, (Integer) object); + break; + case SINT64: + writer.writeSInt64(fieldNumber, (Long) object); + break; + case STRING: + writer.writeString(fieldNumber, (String) object); + break; + case UINT32: + writer.writeUInt32(fieldNumber, (Integer) object); + break; + case UINT64: + writer.writeUInt64(fieldNumber, (Long) object); + break; + case FLOAT: + writer.writeFloat(fieldNumber, (Float) object); + break; + case DOUBLE: + writer.writeDouble(fieldNumber, (Double) object); + break; + case MESSAGE: + writer.writeMessage(fieldNumber, object); + break; + case BYTES: + writer.writeBytes(fieldNumber, (ByteString) object); + break; + case ENUM: + if (object instanceof Internal.EnumLite) { + writer.writeEnum(fieldNumber, ((Internal.EnumLite) object).getNumber()); + } else if (object instanceof Integer) { + writer.writeEnum(fieldNumber, (Integer) object); + } else { + throw new IllegalArgumentException("Unexpected type for enum in map."); + } + break; + default: + throw new IllegalArgumentException("Unsupported map value type for: " + fieldType); + } + } + + private final void writeSInt64List_Internal(int fieldNumber, List list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt64(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt64(fieldNumber, list.get(i)); + } + } + } + + private final void writeSInt64List_Internal(int fieldNumber, LongArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt64(list.getLong(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt64(fieldNumber, list.getLong(i)); + } + } + } + + @Override + public final void writeMessageList(int fieldNumber, List list) throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeMessage(fieldNumber, list.get(i)); + } + } + + @Override + public final void writeMessageList(int fieldNumber, List list, Schema schema) + throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeMessage(fieldNumber, list.get(i), schema); + } + } + + @Override + public final void writeGroupList(int fieldNumber, List list) throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeGroup(fieldNumber, list.get(i)); + } + } + + @Override + public final void writeGroupList(int fieldNumber, List list, Schema schema) + throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeGroup(fieldNumber, list.get(i), schema); + } + } + + @Override + public final void writeMessageSetItem(int fieldNumber, Object value) throws IOException { + writeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP); + if (value instanceof ByteString) { + writeBytes(MESSAGE_SET_MESSAGE, (ByteString) value); + } else { + writeMessage(MESSAGE_SET_MESSAGE, value); + } + writeUInt32(MESSAGE_SET_TYPE_ID, fieldNumber); + writeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP); + } + + final AllocatedBuffer newHeapBuffer() { + return alloc.allocateHeapBuffer(chunkSize); + } + + final AllocatedBuffer newHeapBuffer(int capacity) { + return alloc.allocateHeapBuffer(Math.max(capacity, chunkSize)); + } + + final AllocatedBuffer newDirectBuffer() { + return alloc.allocateDirectBuffer(chunkSize); + } + + final AllocatedBuffer newDirectBuffer(int capacity) { + return alloc.allocateDirectBuffer(Math.max(capacity, chunkSize)); + } + + /** + * Gets the total number of bytes that have been written. This will not be reset by a call to + * {@link #complete()}. + */ + public abstract int getTotalBytesWritten(); + + abstract void requireSpace(int size); + + abstract void finishCurrentBuffer(); + + abstract void writeTag(int fieldNumber, int wireType); + + abstract void writeVarint32(int value); + + abstract void writeInt32(int value); + + abstract void writeSInt32(int value); + + abstract void writeFixed32(int value); + + abstract void writeVarint64(long value); + + abstract void writeSInt64(long value); + + abstract void writeFixed64(long value); + + abstract void writeBool(boolean value); + + abstract void writeString(String in); + + /** + * Not using the version in CodedOutputStream due to the fact that benchmarks have shown a + * performance improvement when returning a byte (rather than an int). + */ + private static byte computeUInt64SizeNoTag(long value) { + // handle two popular special cases up front ... + if ((value & (~0L << 7)) == 0L) { + // Byte 1 + return 1; + } + if (value < 0L) { + // Byte 10 + return 10; + } + // ... leaving us with 8 remaining, which we can divide and conquer + byte n = 2; + if ((value & (~0L << 35)) != 0L) { + // Byte 6-9 + n += 4; // + (value >>> 63); + value >>>= 28; + } + if ((value & (~0L << 21)) != 0L) { + // Byte 4-5 or 8-9 + n += 2; + value >>>= 14; + } + if ((value & (~0L << 14)) != 0L) { + // Byte 3 or 7 + n += 1; + } + return n; + } + + /** Writer that uses safe operations on target array. */ + private static final class SafeHeapWriter extends BinaryWriter { + private AllocatedBuffer allocatedBuffer; + private byte[] buffer; + private int offset; + private int limit; + private int offsetMinusOne; + private int limitMinusOne; + private int pos; + + SafeHeapWriter(BufferAllocator alloc, int chunkSize) { + super(alloc, chunkSize); + nextBuffer(); + } + + @Override + void finishCurrentBuffer() { + if (allocatedBuffer != null) { + totalDoneBytes += bytesWrittenToCurrentBuffer(); + allocatedBuffer.position((pos - allocatedBuffer.arrayOffset()) + 1); + allocatedBuffer = null; + pos = 0; + limitMinusOne = 0; + } + } + + private void nextBuffer() { + nextBuffer(newHeapBuffer()); + } + + private void nextBuffer(int capacity) { + nextBuffer(newHeapBuffer(capacity)); + } + + private void nextBuffer(AllocatedBuffer allocatedBuffer) { + if (!allocatedBuffer.hasArray()) { + throw new RuntimeException("Allocator returned non-heap buffer"); + } + + finishCurrentBuffer(); + + buffers.addFirst(allocatedBuffer); + + this.allocatedBuffer = allocatedBuffer; + this.buffer = allocatedBuffer.array(); + int arrayOffset = allocatedBuffer.arrayOffset(); + this.limit = arrayOffset + allocatedBuffer.limit(); + this.offset = arrayOffset + allocatedBuffer.position(); + this.offsetMinusOne = offset - 1; + this.limitMinusOne = limit - 1; + this.pos = limitMinusOne; + } + + @Override + public int getTotalBytesWritten() { + return totalDoneBytes + bytesWrittenToCurrentBuffer(); + } + + int bytesWrittenToCurrentBuffer() { + return limitMinusOne - pos; + } + + int spaceLeft() { + return pos - offsetMinusOne; + } + + @Override + public void writeUInt32(int fieldNumber, int value) throws IOException { + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeInt32(int fieldNumber, int value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt32(int fieldNumber, int value) throws IOException { + requireSpace(MAX_VARINT32_SIZE * 2); + writeSInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed32(int fieldNumber, int value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + FIXED32_SIZE); + writeFixed32(value); + writeTag(fieldNumber, WIRETYPE_FIXED32); + } + + @Override + public void writeUInt64(int fieldNumber, long value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeVarint64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt64(int fieldNumber, long value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeSInt64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed64(int fieldNumber, long value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + FIXED64_SIZE); + writeFixed64(value); + writeTag(fieldNumber, WIRETYPE_FIXED64); + } + + @Override + public void writeBool(int fieldNumber, boolean value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + 1); + write((byte) (value ? 1 : 0)); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeString(int fieldNumber, String value) throws IOException { + int prevBytes = getTotalBytesWritten(); + writeString(value); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(2 * MAX_VARINT32_SIZE); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) throws IOException { + try { + value.writeToReverse(this); + } catch (IOException e) { + // Should never happen since the writer does not throw. + throw new RuntimeException(e); + } + + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value.size()); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + int prevBytes = getTotalBytesWritten(); + Protobuf.getInstance().writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + int prevBytes = getTotalBytesWritten(); + schema.writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + Protobuf.getInstance().writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + schema.writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeStartGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + } + + @Override + void writeInt32(int value) { + if (value >= 0) { + writeVarint32(value); + } else { + writeVarint64(value); + } + } + + @Override + void writeSInt32(int value) { + writeVarint32(CodedOutputStream.encodeZigZag32(value)); + } + + @Override + void writeSInt64(long value) { + writeVarint64(CodedOutputStream.encodeZigZag64(value)); + } + + @Override + void writeBool(boolean value) { + write((byte) (value ? 1 : 0)); + } + + @Override + void writeTag(int fieldNumber, int wireType) { + writeVarint32(WireFormat.makeTag(fieldNumber, wireType)); + } + + @Override + void writeVarint32(int value) { + if ((value & (~0 << 7)) == 0) { + writeVarint32OneByte(value); + } else if ((value & (~0 << 14)) == 0) { + writeVarint32TwoBytes(value); + } else if ((value & (~0 << 21)) == 0) { + writeVarint32ThreeBytes(value); + } else if ((value & (~0 << 28)) == 0) { + writeVarint32FourBytes(value); + } else { + writeVarint32FiveBytes(value); + } + } + + private void writeVarint32OneByte(int value) { + buffer[pos--] = (byte) value; + } + + private void writeVarint32TwoBytes(int value) { + buffer[pos--] = (byte) (value >>> 7); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint32ThreeBytes(int value) { + buffer[pos--] = (byte) (value >>> 14); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint32FourBytes(int value) { + buffer[pos--] = (byte) (value >>> 21); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint32FiveBytes(int value) { + buffer[pos--] = (byte) (value >>> 28); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + @Override + void writeVarint64(long value) { + switch (computeUInt64SizeNoTag(value)) { + case 1: + writeVarint64OneByte(value); + break; + case 2: + writeVarint64TwoBytes(value); + break; + case 3: + writeVarint64ThreeBytes(value); + break; + case 4: + writeVarint64FourBytes(value); + break; + case 5: + writeVarint64FiveBytes(value); + break; + case 6: + writeVarint64SixBytes(value); + break; + case 7: + writeVarint64SevenBytes(value); + break; + case 8: + writeVarint64EightBytes(value); + break; + case 9: + writeVarint64NineBytes(value); + break; + case 10: + writeVarint64TenBytes(value); + break; + } + } + + private void writeVarint64OneByte(long value) { + buffer[pos--] = (byte) value; + } + + private void writeVarint64TwoBytes(long value) { + buffer[pos--] = (byte) (value >>> 7); + buffer[pos--] = (byte) (((int) value & 0x7F) | 0x80); + } + + private void writeVarint64ThreeBytes(long value) { + buffer[pos--] = (byte) (((int) value) >>> 14); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64FourBytes(long value) { + buffer[pos--] = (byte) (value >>> 21); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64FiveBytes(long value) { + buffer[pos--] = (byte) (value >>> 28); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64SixBytes(long value) { + buffer[pos--] = (byte) (value >>> 35); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64SevenBytes(long value) { + buffer[pos--] = (byte) (value >>> 42); + buffer[pos--] = (byte) (((value >>> 35) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64EightBytes(long value) { + buffer[pos--] = (byte) (value >>> 49); + buffer[pos--] = (byte) (((value >>> 42) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 35) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64NineBytes(long value) { + buffer[pos--] = (byte) (value >>> 56); + buffer[pos--] = (byte) (((value >>> 49) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 42) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 35) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64TenBytes(long value) { + buffer[pos--] = (byte) (value >>> 63); + buffer[pos--] = (byte) (((value >>> 56) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 49) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 42) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 35) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + @Override + void writeFixed32(int value) { + buffer[pos--] = (byte) ((value >> 24) & 0xFF); + buffer[pos--] = (byte) ((value >> 16) & 0xFF); + buffer[pos--] = (byte) ((value >> 8) & 0xFF); + buffer[pos--] = (byte) (value & 0xFF); + } + + @Override + void writeFixed64(long value) { + buffer[pos--] = (byte) ((int) (value >> 56) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 48) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 40) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 32) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 24) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 16) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 8) & 0xFF); + buffer[pos--] = (byte) ((int) (value) & 0xFF); + } + + @Override + void writeString(String in) { + // Request enough space to write the ASCII string. + requireSpace(in.length()); + + // We know the buffer is big enough... + int i = in.length() - 1; + // Set pos to the start of the ASCII string. + pos -= i; + // Designed to take advantage of + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination + for (char c; i >= 0 && (c = in.charAt(i)) < 0x80; i--) { + buffer[pos + i] = (byte) c; + } + if (i == -1) { + // Move pos past the String. + pos -= 1; + return; + } + pos += i; + for (char c; i >= 0; i--) { + c = in.charAt(i); + if (c < 0x80 && pos > offsetMinusOne) { + buffer[pos--] = (byte) c; + } else if (c < 0x800 && pos > offset) { // 11 bits, two UTF-8 bytes + buffer[pos--] = (byte) (0x80 | (0x3F & c)); + buffer[pos--] = (byte) ((0xF << 6) | (c >>> 6)); + } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c) + && pos > (offset + 1)) { + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes + buffer[pos--] = (byte) (0x80 | (0x3F & c)); + buffer[pos--] = (byte) (0x80 | (0x3F & (c >>> 6))); + buffer[pos--] = (byte) ((0xF << 5) | (c >>> 12)); + } else if (pos > (offset + 2)) { + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, + // four UTF-8 bytes + char high = 0; + if (i == 0 || !Character.isSurrogatePair(high = in.charAt(i - 1), c)) { + throw new Utf8.UnpairedSurrogateException(i - 1, i); + } + i--; + int codePoint = Character.toCodePoint(high, c); + buffer[pos--] = (byte) (0x80 | (0x3F & codePoint)); + buffer[pos--] = (byte) (0x80 | (0x3F & (codePoint >>> 6))); + buffer[pos--] = (byte) (0x80 | (0x3F & (codePoint >>> 12))); + buffer[pos--] = (byte) ((0xF << 4) | (codePoint >>> 18)); + } else { + // Buffer is full - allocate a new one and revisit the current character. + requireSpace(i); + i++; + } + } + } + + @Override + public void write(byte value) { + buffer[pos--] = value; + } + + @Override + public void write(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + System.arraycopy(value, offset, buffer, pos + 1, length); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value, offset, length)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + System.arraycopy(value, offset, buffer, pos + 1, length); + } + + @Override + public void write(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + value.get(buffer, pos + 1, length); + } + + @Override + public void writeLazy(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + } + + pos -= length; + value.get(buffer, pos + 1, length); + } + + @Override + void requireSpace(int size) { + if (spaceLeft() < size) { + nextBuffer(size); + } + } + } + + /** Writer that uses unsafe operations on a target array. */ + private static final class UnsafeHeapWriter extends BinaryWriter { + private AllocatedBuffer allocatedBuffer; + private byte[] buffer; + private long offset; + private long limit; + private long offsetMinusOne; + private long limitMinusOne; + private long pos; + + UnsafeHeapWriter(BufferAllocator alloc, int chunkSize) { + super(alloc, chunkSize); + nextBuffer(); + } + + /** Indicates whether the required unsafe operations are supported on this platform. */ + static boolean isSupported() { + return UnsafeUtil.hasUnsafeArrayOperations(); + } + + @Override + void finishCurrentBuffer() { + if (allocatedBuffer != null) { + totalDoneBytes += bytesWrittenToCurrentBuffer(); + allocatedBuffer.position((arrayPos() - allocatedBuffer.arrayOffset()) + 1); + allocatedBuffer = null; + pos = 0; + limitMinusOne = 0; + } + } + + private int arrayPos() { + return (int) pos; + } + + private void nextBuffer() { + nextBuffer(newHeapBuffer()); + } + + private void nextBuffer(int capacity) { + nextBuffer(newHeapBuffer(capacity)); + } + + private void nextBuffer(AllocatedBuffer allocatedBuffer) { + if (!allocatedBuffer.hasArray()) { + throw new RuntimeException("Allocator returned non-heap buffer"); + } + + finishCurrentBuffer(); + buffers.addFirst(allocatedBuffer); + + this.allocatedBuffer = allocatedBuffer; + this.buffer = allocatedBuffer.array(); + int arrayOffset = allocatedBuffer.arrayOffset(); + this.limit = arrayOffset + allocatedBuffer.limit(); + this.offset = arrayOffset + allocatedBuffer.position(); + this.offsetMinusOne = offset - 1; + this.limitMinusOne = limit - 1; + this.pos = limitMinusOne; + } + + @Override + public int getTotalBytesWritten() { + return totalDoneBytes + bytesWrittenToCurrentBuffer(); + } + + int bytesWrittenToCurrentBuffer() { + return (int) (limitMinusOne - pos); + } + + int spaceLeft() { + return (int) (pos - offsetMinusOne); + } + + @Override + public void writeUInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeSInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + FIXED32_SIZE); + writeFixed32(value); + writeTag(fieldNumber, WIRETYPE_FIXED32); + } + + @Override + public void writeUInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeVarint64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeSInt64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + FIXED64_SIZE); + writeFixed64(value); + writeTag(fieldNumber, WIRETYPE_FIXED64); + } + + @Override + public void writeBool(int fieldNumber, boolean value) { + requireSpace(MAX_VARINT32_SIZE + 1); + write((byte) (value ? 1 : 0)); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeString(int fieldNumber, String value) { + int prevBytes = getTotalBytesWritten(); + writeString(value); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(2 * MAX_VARINT32_SIZE); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) { + try { + value.writeToReverse(this); + } catch (IOException e) { + // Should never happen since the writer does not throw. + throw new RuntimeException(e); + } + + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value.size()); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + int prevBytes = getTotalBytesWritten(); + Protobuf.getInstance().writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + int prevBytes = getTotalBytesWritten(); + schema.writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + Protobuf.getInstance().writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + schema.writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeStartGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + } + + @Override + void writeInt32(int value) { + if (value >= 0) { + writeVarint32(value); + } else { + writeVarint64(value); + } + } + + @Override + void writeSInt32(int value) { + writeVarint32(CodedOutputStream.encodeZigZag32(value)); + } + + @Override + void writeSInt64(long value) { + writeVarint64(CodedOutputStream.encodeZigZag64(value)); + } + + @Override + void writeBool(boolean value) { + write((byte) (value ? 1 : 0)); + } + + @Override + void writeTag(int fieldNumber, int wireType) { + writeVarint32(WireFormat.makeTag(fieldNumber, wireType)); + } + + @Override + void writeVarint32(int value) { + if ((value & (~0 << 7)) == 0) { + writeVarint32OneByte(value); + } else if ((value & (~0 << 14)) == 0) { + writeVarint32TwoBytes(value); + } else if ((value & (~0 << 21)) == 0) { + writeVarint32ThreeBytes(value); + } else if ((value & (~0 << 28)) == 0) { + writeVarint32FourBytes(value); + } else { + writeVarint32FiveBytes(value); + } + } + + private void writeVarint32OneByte(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) value); + } + + private void writeVarint32TwoBytes(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 7)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32ThreeBytes(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 14)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FourBytes(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 21)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FiveBytes(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 28)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + @Override + void writeVarint64(long value) { + switch (computeUInt64SizeNoTag(value)) { + case 1: + writeVarint64OneByte(value); + break; + case 2: + writeVarint64TwoBytes(value); + break; + case 3: + writeVarint64ThreeBytes(value); + break; + case 4: + writeVarint64FourBytes(value); + break; + case 5: + writeVarint64FiveBytes(value); + break; + case 6: + writeVarint64SixBytes(value); + break; + case 7: + writeVarint64SevenBytes(value); + break; + case 8: + writeVarint64EightBytes(value); + break; + case 9: + writeVarint64NineBytes(value); + break; + case 10: + writeVarint64TenBytes(value); + break; + } + } + + private void writeVarint64OneByte(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) value); + } + + private void writeVarint64TwoBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 7)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((int) value & 0x7F) | 0x80)); + } + + private void writeVarint64ThreeBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (((int) value) >>> 14)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64FourBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 21)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64FiveBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 28)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64SixBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 35)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64SevenBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 42)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64EightBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 49)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64NineBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 56)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 49) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64TenBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 63)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 56) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 49) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + @Override + void writeFixed32(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) ((value >> 24) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value >> 16) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value >> 8) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) (value & 0xFF)); + } + + @Override + void writeFixed64(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 56) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 48) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 40) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 32) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 24) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 16) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 8) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value) & 0xFF)); + } + + @Override + void writeString(String in) { + // Request enough space to write the ASCII string. + requireSpace(in.length()); + + // We know the buffer is big enough... + int i = in.length() - 1; + // Set pos to the start of the ASCII string. + // pos -= i; + // Designed to take advantage of + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination + for (char c; i >= 0 && (c = in.charAt(i)) < 0x80; i--) { + UnsafeUtil.putByte(buffer, pos--, (byte) c); + } + if (i == -1) { + // Move pos past the String. + return; + } + for (char c; i >= 0; i--) { + c = in.charAt(i); + if (c < 0x80 && pos > offsetMinusOne) { + UnsafeUtil.putByte(buffer, pos--, (byte) c); + } else if (c < 0x800 && pos > offset) { // 11 bits, two UTF-8 bytes + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & c))); + UnsafeUtil.putByte(buffer, pos--, (byte) ((0xF << 6) | (c >>> 6))); + } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c) + && pos > offset + 1) { + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & c))); + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & (c >>> 6)))); + UnsafeUtil.putByte(buffer, pos--, (byte) ((0xF << 5) | (c >>> 12))); + } else if (pos > offset + 2) { + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, + // four UTF-8 bytes + final char high; + if (i == 0 || !Character.isSurrogatePair(high = in.charAt(i - 1), c)) { + throw new Utf8.UnpairedSurrogateException(i - 1, i); + } + i--; + int codePoint = Character.toCodePoint(high, c); + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & codePoint))); + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & (codePoint >>> 6)))); + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & (codePoint >>> 12)))); + UnsafeUtil.putByte(buffer, pos--, (byte) ((0xF << 4) | (codePoint >>> 18))); + } else { + // Buffer is full - allocate a new one and revisit the current character. + requireSpace(i); + i++; + } + } + } + + @Override + public void write(byte value) { + UnsafeUtil.putByte(buffer, pos--, value); + } + + @Override + public void write(byte[] value, int offset, int length) { + if (offset < 0 || offset + length > value.length) { + throw new ArrayIndexOutOfBoundsException( + String.format("value.length=%d, offset=%d, length=%d", value.length, offset, length)); + } + requireSpace(length); + + pos -= length; + System.arraycopy(value, offset, buffer, arrayPos() + 1, length); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) { + if (offset < 0 || offset + length > value.length) { + throw new ArrayIndexOutOfBoundsException( + String.format("value.length=%d, offset=%d, length=%d", value.length, offset, length)); + } + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value, offset, length)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + System.arraycopy(value, offset, buffer, arrayPos() + 1, length); + } + + @Override + public void write(ByteBuffer value) { + int length = value.remaining(); + requireSpace(length); + + pos -= length; + value.get(buffer, arrayPos() + 1, length); + } + + @Override + public void writeLazy(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + } + + pos -= length; + value.get(buffer, arrayPos() + 1, length); + } + + @Override + void requireSpace(int size) { + if (spaceLeft() < size) { + nextBuffer(size); + } + } + } + + /** Writer that uses safe operations on a target {@link ByteBuffer}. */ + private static final class SafeDirectWriter extends BinaryWriter { + private ByteBuffer buffer; + private int limitMinusOne; + private int pos; + + SafeDirectWriter(BufferAllocator alloc, int chunkSize) { + super(alloc, chunkSize); + nextBuffer(); + } + + private void nextBuffer() { + nextBuffer(newDirectBuffer()); + } + + private void nextBuffer(int capacity) { + nextBuffer(newDirectBuffer(capacity)); + } + + private void nextBuffer(AllocatedBuffer allocatedBuffer) { + if (!allocatedBuffer.hasNioBuffer()) { + throw new RuntimeException("Allocated buffer does not have NIO buffer"); + } + ByteBuffer nioBuffer = allocatedBuffer.nioBuffer(); + if (!nioBuffer.isDirect()) { + throw new RuntimeException("Allocator returned non-direct buffer"); + } + + finishCurrentBuffer(); + buffers.addFirst(allocatedBuffer); + + buffer = nioBuffer; + buffer.limit(buffer.capacity()); + buffer.position(0); + // Set byte order to little endian for fast writing of fixed 32/64. + buffer.order(ByteOrder.LITTLE_ENDIAN); + + limitMinusOne = buffer.limit() - 1; + pos = limitMinusOne; + } + + @Override + public int getTotalBytesWritten() { + return totalDoneBytes + bytesWrittenToCurrentBuffer(); + } + + private int bytesWrittenToCurrentBuffer() { + return limitMinusOne - pos; + } + + private int spaceLeft() { + return pos + 1; + } + + @Override + void finishCurrentBuffer() { + if (buffer != null) { + totalDoneBytes += bytesWrittenToCurrentBuffer(); + // Update the indices on the netty buffer. + buffer.position(pos + 1); + buffer = null; + pos = 0; + limitMinusOne = 0; + } + } + + @Override + public void writeUInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeSInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + FIXED32_SIZE); + writeFixed32(value); + writeTag(fieldNumber, WIRETYPE_FIXED32); + } + + @Override + public void writeUInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeVarint64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeSInt64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + FIXED64_SIZE); + writeFixed64(value); + writeTag(fieldNumber, WIRETYPE_FIXED64); + } + + @Override + public void writeBool(int fieldNumber, boolean value) { + requireSpace(MAX_VARINT32_SIZE + 1); + write((byte) (value ? 1 : 0)); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeString(int fieldNumber, String value) { + int prevBytes = getTotalBytesWritten(); + writeString(value); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(2 * MAX_VARINT32_SIZE); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) { + try { + value.writeToReverse(this); + } catch (IOException e) { + // Should never happen since the writer does not throw. + throw new RuntimeException(e); + } + + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value.size()); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + int prevBytes = getTotalBytesWritten(); + Protobuf.getInstance().writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + int prevBytes = getTotalBytesWritten(); + schema.writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + Protobuf.getInstance().writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + schema.writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeStartGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + } + + @Override + void writeInt32(int value) { + if (value >= 0) { + writeVarint32(value); + } else { + writeVarint64(value); + } + } + + @Override + void writeSInt32(int value) { + writeVarint32(CodedOutputStream.encodeZigZag32(value)); + } + + @Override + void writeSInt64(long value) { + writeVarint64(CodedOutputStream.encodeZigZag64(value)); + } + + @Override + void writeBool(boolean value) { + write((byte) (value ? 1 : 0)); + } + + @Override + void writeTag(int fieldNumber, int wireType) { + writeVarint32(WireFormat.makeTag(fieldNumber, wireType)); + } + + @Override + void writeVarint32(int value) { + if ((value & (~0 << 7)) == 0) { + writeVarint32OneByte(value); + } else if ((value & (~0 << 14)) == 0) { + writeVarint32TwoBytes(value); + } else if ((value & (~0 << 21)) == 0) { + writeVarint32ThreeBytes(value); + } else if ((value & (~0 << 28)) == 0) { + writeVarint32FourBytes(value); + } else { + writeVarint32FiveBytes(value); + } + } + + private void writeVarint32OneByte(int value) { + buffer.put(pos--, (byte) value); + } + + private void writeVarint32TwoBytes(int value) { + // Byte order is little-endian. + pos -= 2; + buffer.putShort(pos + 1, (short) (((value & (0x7F << 7)) << 1) | ((value & 0x7F) | 0x80))); + } + + private void writeVarint32ThreeBytes(int value) { + // Byte order is little-endian. + pos -= 3; + buffer.putInt( + pos, + ((value & (0x7F << 14)) << 10) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 9) + | ((value & 0x7F) | 0x80) << 8); + } + + private void writeVarint32FourBytes(int value) { + // Byte order is little-endian. + pos -= 4; + buffer.putInt( + pos + 1, + ((value & (0x7F << 21)) << 3) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 2) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 1) + | ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FiveBytes(int value) { + // Byte order is little-endian. + buffer.put(pos--, (byte) (value >>> 28)); + pos -= 4; + buffer.putInt( + pos + 1, + ((((value >>> 21) & 0x7F) | 0x80) << 24) + | ((((value >>> 14) & 0x7F) | 0x80) << 16) + | ((((value >>> 7) & 0x7F) | 0x80) << 8) + | ((value & 0x7F) | 0x80)); + } + + @Override + void writeVarint64(long value) { + switch (computeUInt64SizeNoTag(value)) { + case 1: + writeVarint64OneByte(value); + break; + case 2: + writeVarint64TwoBytes(value); + break; + case 3: + writeVarint64ThreeBytes(value); + break; + case 4: + writeVarint64FourBytes(value); + break; + case 5: + writeVarint64FiveBytes(value); + break; + case 6: + writeVarint64SixBytes(value); + break; + case 7: + writeVarint64SevenBytes(value); + break; + case 8: + writeVarint64EightBytes(value); + break; + case 9: + writeVarint64NineBytes(value); + break; + case 10: + writeVarint64TenBytes(value); + break; + } + } + + private void writeVarint64OneByte(long value) { + writeVarint32OneByte((int) value); + } + + private void writeVarint64TwoBytes(long value) { + writeVarint32TwoBytes((int) value); + } + + private void writeVarint64ThreeBytes(long value) { + writeVarint32ThreeBytes((int) value); + } + + private void writeVarint64FourBytes(long value) { + writeVarint32FourBytes((int) value); + } + + private void writeVarint64FiveBytes(long value) { + // Byte order is little-endian. + pos -= 5; + buffer.putLong( + pos - 2, + ((value & (0x7FL << 28)) << 28) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 27) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 26) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 25) + | (((value & 0x7F) | 0x80)) << 24); + } + + private void writeVarint64SixBytes(long value) { + // Byte order is little-endian. + pos -= 6; + buffer.putLong( + pos - 1, + ((value & (0x7FL << 35)) << 21) + | (((value & (0x7FL << 28)) | (0x80L << 28)) << 20) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 19) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 18) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 17) + | (((value & 0x7F) | 0x80)) << 16); + } + + private void writeVarint64SevenBytes(long value) { + // Byte order is little-endian. + pos -= 7; + buffer.putLong( + pos, + ((value & (0x7FL << 42)) << 14) + | (((value & (0x7FL << 35)) | (0x80L << 35)) << 13) + | (((value & (0x7FL << 28)) | (0x80L << 28)) << 12) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 11) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 10) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 9) + | (((value & 0x7F) | 0x80)) << 8); + } + + private void writeVarint64EightBytes(long value) { + // Byte order is little-endian. + pos -= 8; + buffer.putLong( + pos + 1, + ((value & (0x7FL << 49)) << 7) + | (((value & (0x7FL << 42)) | (0x80L << 42)) << 6) + | (((value & (0x7FL << 35)) | (0x80L << 35)) << 5) + | (((value & (0x7FL << 28)) | (0x80L << 28)) << 4) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 3) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 2) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 1) + | ((value & 0x7F) | 0x80)); + } + + private void writeVarint64EightBytesWithSign(long value) { + // Byte order is little-endian. + pos -= 8; + buffer.putLong( + pos + 1, + (((value & (0x7FL << 49)) | (0x80L << 49)) << 7) + | (((value & (0x7FL << 42)) | (0x80L << 42)) << 6) + | (((value & (0x7FL << 35)) | (0x80L << 35)) << 5) + | (((value & (0x7FL << 28)) | (0x80L << 28)) << 4) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 3) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 2) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 1) + | ((value & 0x7F) | 0x80)); + } + + private void writeVarint64NineBytes(long value) { + buffer.put(pos--, (byte) (value >>> 56)); + writeVarint64EightBytesWithSign(value & 0xFFFFFFFFFFFFFFL); + } + + private void writeVarint64TenBytes(long value) { + buffer.put(pos--, (byte) (value >>> 63)); + buffer.put(pos--, (byte) (((value >>> 56) & 0x7F) | 0x80)); + writeVarint64EightBytesWithSign(value & 0xFFFFFFFFFFFFFFL); + } + + @Override + void writeFixed32(int value) { + pos -= 4; + buffer.putInt(pos + 1, value); + } + + @Override + void writeFixed64(long value) { + pos -= 8; + buffer.putLong(pos + 1, value); + } + + @Override + void writeString(String in) { + // Request enough space to write the ASCII string. + requireSpace(in.length()); + + // We know the buffer is big enough... + int i = in.length() - 1; + pos -= i; + // Designed to take advantage of + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination + for (char c; i >= 0 && (c = in.charAt(i)) < 0x80; i--) { + buffer.put(pos + i, (byte) c); + } + if (i == -1) { + // Move the position past the ASCII string. + pos -= 1; + return; + } + pos += i; + for (char c; i >= 0; i--) { + c = in.charAt(i); + if (c < 0x80 && pos >= 0) { + buffer.put(pos--, (byte) c); + } else if (c < 0x800 && pos > 0) { // 11 bits, two UTF-8 bytes + buffer.put(pos--, (byte) (0x80 | (0x3F & c))); + buffer.put(pos--, (byte) ((0xF << 6) | (c >>> 6))); + } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c) && pos > 1) { + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes + buffer.put(pos--, (byte) (0x80 | (0x3F & c))); + buffer.put(pos--, (byte) (0x80 | (0x3F & (c >>> 6)))); + buffer.put(pos--, (byte) ((0xF << 5) | (c >>> 12))); + } else if (pos > 2) { + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, + // four UTF-8 bytes + char high = 0; + if (i == 0 || !Character.isSurrogatePair(high = in.charAt(i - 1), c)) { + throw new Utf8.UnpairedSurrogateException(i - 1, i); + } + i--; + int codePoint = Character.toCodePoint(high, c); + buffer.put(pos--, (byte) (0x80 | (0x3F & codePoint))); + buffer.put(pos--, (byte) (0x80 | (0x3F & (codePoint >>> 6)))); + buffer.put(pos--, (byte) (0x80 | (0x3F & (codePoint >>> 12)))); + buffer.put(pos--, (byte) ((0xF << 4) | (codePoint >>> 18))); + } else { + // Buffer is full - allocate a new one and revisit the current character. + requireSpace(i); + i++; + } + } + } + + @Override + public void write(byte value) { + buffer.put(pos--, value); + } + + @Override + public void write(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + buffer.position(pos + 1); + buffer.put(value, offset, length); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value, offset, length)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + buffer.position(pos + 1); + buffer.put(value, offset, length); + } + + @Override + public void write(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + buffer.position(pos + 1); + buffer.put(value); + } + + @Override + public void writeLazy(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + buffer.position(pos + 1); + buffer.put(value); + } + + @Override + void requireSpace(int size) { + if (spaceLeft() < size) { + nextBuffer(size); + } + } + } + + /** Writer that uses unsafe operations on a target {@link ByteBuffer}. */ + private static final class UnsafeDirectWriter extends BinaryWriter { + private ByteBuffer buffer; + private long bufferOffset; + private long limitMinusOne; + private long pos; + + UnsafeDirectWriter(BufferAllocator alloc, int chunkSize) { + super(alloc, chunkSize); + nextBuffer(); + } + + /** Indicates whether the required unsafe operations are supported on this platform. */ + private static boolean isSupported() { + return UnsafeUtil.hasUnsafeByteBufferOperations(); + } + + private void nextBuffer() { + nextBuffer(newDirectBuffer()); + } + + private void nextBuffer(int capacity) { + nextBuffer(newDirectBuffer(capacity)); + } + + private void nextBuffer(AllocatedBuffer allocatedBuffer) { + if (!allocatedBuffer.hasNioBuffer()) { + throw new RuntimeException("Allocated buffer does not have NIO buffer"); + } + ByteBuffer nioBuffer = allocatedBuffer.nioBuffer(); + if (!nioBuffer.isDirect()) { + throw new RuntimeException("Allocator returned non-direct buffer"); + } + + finishCurrentBuffer(); + buffers.addFirst(allocatedBuffer); + + buffer = nioBuffer; + buffer.limit(buffer.capacity()); + buffer.position(0); + + bufferOffset = UnsafeUtil.addressOffset(buffer); + limitMinusOne = bufferOffset + (buffer.limit() - 1); + pos = limitMinusOne; + } + + @Override + public int getTotalBytesWritten() { + return totalDoneBytes + bytesWrittenToCurrentBuffer(); + } + + private int bytesWrittenToCurrentBuffer() { + return (int) (limitMinusOne - pos); + } + + private int spaceLeft() { + return bufferPos() + 1; + } + + @Override + void finishCurrentBuffer() { + if (buffer != null) { + totalDoneBytes += bytesWrittenToCurrentBuffer(); + // Update the indices on the netty buffer. + buffer.position(bufferPos() + 1); + buffer = null; + pos = 0; + limitMinusOne = 0; + } + } + + private int bufferPos() { + return (int) (pos - bufferOffset); + } + + @Override + public void writeUInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeSInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + FIXED32_SIZE); + writeFixed32(value); + writeTag(fieldNumber, WIRETYPE_FIXED32); + } + + @Override + public void writeUInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeVarint64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeSInt64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + FIXED64_SIZE); + writeFixed64(value); + writeTag(fieldNumber, WIRETYPE_FIXED64); + } + + @Override + public void writeBool(int fieldNumber, boolean value) { + requireSpace(MAX_VARINT32_SIZE + 1); + write((byte) (value ? 1 : 0)); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeString(int fieldNumber, String value) { + int prevBytes = getTotalBytesWritten(); + writeString(value); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(2 * MAX_VARINT32_SIZE); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) { + try { + value.writeToReverse(this); + } catch (IOException e) { + // Should never happen since the writer does not throw. + throw new RuntimeException(e); + } + + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value.size()); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + int prevBytes = getTotalBytesWritten(); + Protobuf.getInstance().writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + int prevBytes = getTotalBytesWritten(); + schema.writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + Protobuf.getInstance().writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + schema.writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeStartGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + } + + @Override + void writeInt32(int value) { + if (value >= 0) { + writeVarint32(value); + } else { + writeVarint64(value); + } + } + + @Override + void writeSInt32(int value) { + writeVarint32(CodedOutputStream.encodeZigZag32(value)); + } + + @Override + void writeSInt64(long value) { + writeVarint64(CodedOutputStream.encodeZigZag64(value)); + } + + @Override + void writeBool(boolean value) { + write((byte) (value ? 1 : 0)); + } + + @Override + void writeTag(int fieldNumber, int wireType) { + writeVarint32(WireFormat.makeTag(fieldNumber, wireType)); + } + + @Override + void writeVarint32(int value) { + if ((value & (~0 << 7)) == 0) { + writeVarint32OneByte(value); + } else if ((value & (~0 << 14)) == 0) { + writeVarint32TwoBytes(value); + } else if ((value & (~0 << 21)) == 0) { + writeVarint32ThreeBytes(value); + } else if ((value & (~0 << 28)) == 0) { + writeVarint32FourBytes(value); + } else { + writeVarint32FiveBytes(value); + } + } + + private void writeVarint32OneByte(int value) { + UnsafeUtil.putByte(pos--, (byte) value); + } + + private void writeVarint32TwoBytes(int value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 7)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32ThreeBytes(int value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 14)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FourBytes(int value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 21)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FiveBytes(int value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 28)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + @Override + void writeVarint64(long value) { + switch (computeUInt64SizeNoTag(value)) { + case 1: + writeVarint64OneByte(value); + break; + case 2: + writeVarint64TwoBytes(value); + break; + case 3: + writeVarint64ThreeBytes(value); + break; + case 4: + writeVarint64FourBytes(value); + break; + case 5: + writeVarint64FiveBytes(value); + break; + case 6: + writeVarint64SixBytes(value); + break; + case 7: + writeVarint64SevenBytes(value); + break; + case 8: + writeVarint64EightBytes(value); + break; + case 9: + writeVarint64NineBytes(value); + break; + case 10: + writeVarint64TenBytes(value); + break; + } + } + + private void writeVarint64OneByte(long value) { + UnsafeUtil.putByte(pos--, (byte) value); + } + + private void writeVarint64TwoBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 7)); + UnsafeUtil.putByte(pos--, (byte) (((int) value & 0x7F) | 0x80)); + } + + private void writeVarint64ThreeBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (((int) value) >>> 14)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64FourBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 21)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64FiveBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 28)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64SixBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 35)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64SevenBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 42)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64EightBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 49)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64NineBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 56)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 49) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64TenBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 63)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 56) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 49) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + @Override + void writeFixed32(int value) { + UnsafeUtil.putByte(pos--, (byte) ((value >> 24) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((value >> 16) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((value >> 8) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) (value & 0xFF)); + } + + @Override + void writeFixed64(long value) { + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 56) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 48) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 40) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 32) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 24) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 16) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 8) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value) & 0xFF)); + } + + @Override + void writeString(String in) { + // Request enough space to write the ASCII string. + requireSpace(in.length()); + + // We know the buffer is big enough... + int i = in.length() - 1; + // Designed to take advantage of + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination + for (char c; i >= 0 && (c = in.charAt(i)) < 0x80; i--) { + UnsafeUtil.putByte(pos--, (byte) c); + } + if (i == -1) { + // ASCII. + return; + } + for (char c; i >= 0; i--) { + c = in.charAt(i); + if (c < 0x80 && pos >= bufferOffset) { + UnsafeUtil.putByte(pos--, (byte) c); + } else if (c < 0x800 && pos > bufferOffset) { // 11 bits, two UTF-8 bytes + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & c))); + UnsafeUtil.putByte(pos--, (byte) ((0xF << 6) | (c >>> 6))); + } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c) + && pos > bufferOffset + 1) { + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & c))); + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & (c >>> 6)))); + UnsafeUtil.putByte(pos--, (byte) ((0xF << 5) | (c >>> 12))); + } else if (pos > bufferOffset + 2) { + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, + // four UTF-8 bytes + final char high; + if (i == 0 || !Character.isSurrogatePair(high = in.charAt(i - 1), c)) { + throw new Utf8.UnpairedSurrogateException(i - 1, i); + } + i--; + int codePoint = Character.toCodePoint(high, c); + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & codePoint))); + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & (codePoint >>> 6)))); + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & (codePoint >>> 12)))); + UnsafeUtil.putByte(pos--, (byte) ((0xF << 4) | (codePoint >>> 18))); + } else { + // Buffer is full - allocate a new one and revisit the current character. + requireSpace(i); + i++; + } + } + } + + @Override + public void write(byte value) { + UnsafeUtil.putByte(pos--, value); + } + + @Override + public void write(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + buffer.position(bufferPos() + 1); + buffer.put(value, offset, length); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value, offset, length)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + buffer.position(bufferPos() + 1); + buffer.put(value, offset, length); + } + + @Override + public void write(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + buffer.position(bufferPos() + 1); + buffer.put(value); + } + + @Override + public void writeLazy(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + buffer.position(bufferPos() + 1); + buffer.put(value); + } + + @Override + void requireSpace(int size) { + if (spaceLeft() < size) { + nextBuffer(size); + } + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/BlockingRpcChannel.java b/java/core/src/main/java/com/google/protobuf/BlockingRpcChannel.java index d535efb9b6..8af80057f3 100644 --- a/java/core/src/main/java/com/google/protobuf/BlockingRpcChannel.java +++ b/java/core/src/main/java/com/google/protobuf/BlockingRpcChannel.java @@ -31,21 +31,21 @@ package com.google.protobuf; /** - *

Abstract interface for a blocking RPC channel. {@code BlockingRpcChannel} - * is the blocking equivalent to {@link RpcChannel}. + * Abstract interface for a blocking RPC channel. {@code BlockingRpcChannel} is the blocking + * equivalent to {@link RpcChannel}. * * @author kenton@google.com Kenton Varda * @author cpovirk@google.com Chris Povirk */ public interface BlockingRpcChannel { /** - * Call the given method of the remote service and blocks until it returns. - * {@code callBlockingMethod()} is the blocking equivalent to - * {@link RpcChannel#callMethod}. + * Call the given method of the remote service and blocks until it returns. {@code + * callBlockingMethod()} is the blocking equivalent to {@link RpcChannel#callMethod}. */ Message callBlockingMethod( Descriptors.MethodDescriptor method, RpcController controller, Message request, - Message responsePrototype) throws ServiceException; + Message responsePrototype) + throws ServiceException; } diff --git a/java/core/src/main/java/com/google/protobuf/BlockingService.java b/java/core/src/main/java/com/google/protobuf/BlockingService.java index d01f0b8ff0..e2b99c96c3 100644 --- a/java/core/src/main/java/com/google/protobuf/BlockingService.java +++ b/java/core/src/main/java/com/google/protobuf/BlockingService.java @@ -37,28 +37,21 @@ package com.google.protobuf; * @author cpovirk@google.com Chris Povirk */ public interface BlockingService { - /** - * Equivalent to {@link Service#getDescriptorForType}. - */ + /** Equivalent to {@link Service#getDescriptorForType}. */ Descriptors.ServiceDescriptor getDescriptorForType(); /** - * Equivalent to {@link Service#callMethod}, except that - * {@code callBlockingMethod()} returns the result of the RPC or throws a - * {@link ServiceException} if there is a failure, rather than passing the - * information to a callback. + * Equivalent to {@link Service#callMethod}, except that {@code callBlockingMethod()} returns the + * result of the RPC or throws a {@link ServiceException} if there is a failure, rather than + * passing the information to a callback. */ - Message callBlockingMethod(Descriptors.MethodDescriptor method, - RpcController controller, - Message request) throws ServiceException; + Message callBlockingMethod( + Descriptors.MethodDescriptor method, RpcController controller, Message request) + throws ServiceException; - /** - * Equivalent to {@link Service#getRequestPrototype}. - */ + /** Equivalent to {@link Service#getRequestPrototype}. */ Message getRequestPrototype(Descriptors.MethodDescriptor method); - /** - * Equivalent to {@link Service#getResponsePrototype}. - */ + /** Equivalent to {@link Service#getResponsePrototype}. */ Message getResponsePrototype(Descriptors.MethodDescriptor method); } diff --git a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java index 0dedb173c9..65dcd53aa4 100644 --- a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java @@ -42,11 +42,10 @@ import java.util.RandomAccess; * * @author dweis@google.com (Daniel Weis) */ -final class BooleanArrayList - extends AbstractProtobufList +final class BooleanArrayList extends AbstractProtobufList implements BooleanList, RandomAccess, PrimitiveNonBoxingCollection { - private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList(); + private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList(new boolean[0], 0); static { EMPTY_LIST.makeImmutable(); } @@ -55,9 +54,7 @@ final class BooleanArrayList return EMPTY_LIST; } - /** - * The backing store for the list. - */ + /** The backing store for the list. */ private boolean[] array; /** @@ -66,16 +63,14 @@ final class BooleanArrayList */ private int size; - /** - * Constructs a new mutable {@code BooleanArrayList} with default capacity. - */ + /** Constructs a new mutable {@code BooleanArrayList} with default capacity. */ BooleanArrayList() { this(new boolean[DEFAULT_CAPACITY], 0); } /** - * Constructs a new mutable {@code BooleanArrayList} - * containing the same elements as {@code other}. + * Constructs a new mutable {@code BooleanArrayList} containing the same elements as {@code + * other}. */ private BooleanArrayList(boolean[] other, int size) { array = other; @@ -164,22 +159,34 @@ final class BooleanArrayList return previousValue; } + @Override + public boolean add(Boolean element) { + addBoolean(element); + return true; + } + @Override public void add(int index, Boolean element) { addBoolean(index, element); } - /** - * Like {@link #add(Boolean)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Boolean)} but more efficient in that it doesn't box the element. */ @Override public void addBoolean(boolean element) { - addBoolean(size, element); + ensureIsMutable(); + if (size == array.length) { + // Resize to 1.5x the size + int length = ((size * 3) / 2) + 1; + boolean[] newArray = new boolean[length]; + + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + + array[size++] = element; } - /** - * Like {@link #add(int, Boolean)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(int, Boolean)} but more efficient in that it doesn't box the element. */ private void addBoolean(int index, boolean element) { ensureIsMutable(); if (index < 0 || index > size) { @@ -245,7 +252,7 @@ final class BooleanArrayList ensureIsMutable(); for (int i = 0; i < size; i++) { if (o.equals(array[i])) { - System.arraycopy(array, i + 1, array, i, size - i); + System.arraycopy(array, i + 1, array, i, size - i - 1); size--; modCount++; return true; @@ -260,7 +267,7 @@ final class BooleanArrayList ensureIndexInRange(index); boolean value = array[index]; if (index < size - 1) { - System.arraycopy(array, index + 1, array, index, size - index); + System.arraycopy(array, index + 1, array, index, size - index - 1); } size--; modCount++; diff --git a/java/core/src/main/java/com/google/protobuf/BufferAllocator.java b/java/core/src/main/java/com/google/protobuf/BufferAllocator.java new file mode 100755 index 0000000000..bfd9c7237c --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/BufferAllocator.java @@ -0,0 +1,64 @@ +// 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. + +package com.google.protobuf; + +import java.nio.ByteBuffer; + +/** + * An object responsible for allocation of buffers. This is an extension point to enable buffer + * pooling within an application. + */ +@ExperimentalApi +abstract class BufferAllocator { + private static final BufferAllocator UNPOOLED = + new BufferAllocator() { + @Override + public AllocatedBuffer allocateHeapBuffer(int capacity) { + return AllocatedBuffer.wrap(new byte[capacity]); + } + + @Override + public AllocatedBuffer allocateDirectBuffer(int capacity) { + return AllocatedBuffer.wrap(ByteBuffer.allocateDirect(capacity)); + } + }; + + /** Returns an unpooled buffer allocator, which will create a new buffer for each request. */ + public static BufferAllocator unpooled() { + return UNPOOLED; + } + + /** Allocates a buffer with the given capacity that is backed by an array on the heap. */ + public abstract AllocatedBuffer allocateHeapBuffer(int capacity); + + /** Allocates a direct (i.e. non-heap) buffer with the given capacity. */ + public abstract AllocatedBuffer allocateDirectBuffer(int capacity); +} diff --git a/java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java b/java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java index 6157a52f50..2cb3ada67f 100644 --- a/java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java +++ b/java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java @@ -40,45 +40,40 @@ import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; -/** - * Utility class to provide efficient writing of {@link ByteBuffer}s to {@link OutputStream}s. - */ +/** Utility class to provide efficient writing of {@link ByteBuffer}s to {@link OutputStream}s. */ final class ByteBufferWriter { private ByteBufferWriter() {} /** - * Minimum size for a cached buffer. This prevents us from allocating buffers that are too - * small to be easily reused. + * Minimum size for a cached buffer. This prevents us from allocating buffers that are too small + * to be easily reused. */ // TODO(nathanmittler): tune this property or allow configuration? private static final int MIN_CACHED_BUFFER_SIZE = 1024; /** - * Maximum size for a cached buffer. If a larger buffer is required, it will be allocated - * but not cached. + * Maximum size for a cached buffer. If a larger buffer is required, it will be allocated but not + * cached. */ // TODO(nathanmittler): tune this property or allow configuration? private static final int MAX_CACHED_BUFFER_SIZE = 16 * 1024; - /** - * The fraction of the requested buffer size under which the buffer will be reallocated. - */ + /** The fraction of the requested buffer size under which the buffer will be reallocated. */ // TODO(nathanmittler): tune this property or allow configuration? private static final float BUFFER_REALLOCATION_THRESHOLD = 0.5f; /** - * Keeping a soft reference to a thread-local buffer. This buffer is used for writing a - * {@link ByteBuffer} to an {@link OutputStream} when no zero-copy alternative was available. - * Using a "soft" reference since VMs may keep this reference around longer than "weak" - * (e.g. HotSpot will maintain soft references until memory pressure warrants collection). + * Keeping a soft reference to a thread-local buffer. This buffer is used for writing a {@link + * ByteBuffer} to an {@link OutputStream} when no zero-copy alternative was available. Using a + * "soft" reference since VMs may keep this reference around longer than "weak" (e.g. HotSpot will + * maintain soft references until memory pressure warrants collection). */ private static final ThreadLocal> BUFFER = new ThreadLocal>(); - /** - * This is a hack for GAE, where {@code FileOutputStream} is unavailable. - */ + /** This is a hack for GAE, where {@code FileOutputStream} is unavailable. */ private static final Class FILE_OUTPUT_STREAM_CLASS = safeGetClass("java.io.FileOutputStream"); + private static final long CHANNEL_FIELD_OFFSET = getChannelFieldOffset(FILE_OUTPUT_STREAM_CLASS); /** @@ -100,7 +95,7 @@ final class ByteBufferWriter { // Optimized write for array-backed buffers. // Note that we're taking the risk that a malicious OutputStream could modify the array. output.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); - } else if (!writeToChannel(buffer, output)){ + } else if (!writeToChannel(buffer, output)) { // Read all of the data from the buffer to an array. // TODO(nathanmittler): Consider performance improvements for other "known" stream types. final byte[] array = getOrCreateBuffer(buffer.remaining()); @@ -171,6 +166,7 @@ final class ByteBufferWriter { return null; } } + private static long getChannelFieldOffset(Class clazz) { try { if (clazz != null && UnsafeUtil.hasUnsafeArrayOperations()) { diff --git a/java/core/src/main/java/com/google/protobuf/ByteOutput.java b/java/core/src/main/java/com/google/protobuf/ByteOutput.java index ee5887538f..dba7a379f8 100644 --- a/java/core/src/main/java/com/google/protobuf/ByteOutput.java +++ b/java/core/src/main/java/com/google/protobuf/ByteOutput.java @@ -37,9 +37,9 @@ import java.nio.ByteBuffer; * An output target for raw bytes. This interface provides semantics that support two types of * writing: * - *

Traditional write operations: - * (as defined by {@link java.io.OutputStream}) where the target method is responsible for either - * copying the data or completing the write before returning from the method call. + *

Traditional write operations: (as defined by {@link java.io.OutputStream}) where the + * target method is responsible for either copying the data or completing the write before returning + * from the method call. * *

Lazy write operations: where the caller guarantees that it will never modify the * provided buffer and it can therefore be considered immutable. The target method is free to @@ -57,9 +57,9 @@ public abstract class ByteOutput { public abstract void write(byte value) throws IOException; /** - * Writes a sequence of bytes. The {@link ByteOutput} must copy {@code value} if it will - * not be processed prior to the return of this method call, since {@code value} may be - * reused/altered by the caller. + * Writes a sequence of bytes. The {@link ByteOutput} must copy {@code value} if it will not be + * processed prior to the return of this method call, since {@code value} may be reused/altered by + * the caller. * *

NOTE: This method MUST NOT modify the {@code value}. Doing so is a * programming error and will lead to data corruption which will be difficult to debug. @@ -87,15 +87,15 @@ public abstract class ByteOutput { public abstract void writeLazy(byte[] value, int offset, int length) throws IOException; /** - * Writes a sequence of bytes. The {@link ByteOutput} must copy {@code value} if it will - * not be processed prior to the return of this method call, since {@code value} may be - * reused/altered by the caller. + * Writes a sequence of bytes. The {@link ByteOutput} must copy {@code value} if it will not be + * processed prior to the return of this method call, since {@code value} may be reused/altered by + * the caller. * *

NOTE: This method MUST NOT modify the {@code value}. Doing so is a * programming error and will lead to data corruption which will be difficult to debug. * * @param value the bytes to be written. Upon returning from this call, the {@code position} of - * this buffer will be set to the {@code limit} + * this buffer will be set to the {@code limit} * @throws IOException thrown if an error occurred while writing */ public abstract void write(ByteBuffer value) throws IOException; @@ -109,7 +109,7 @@ public abstract class ByteOutput { * programming error and will lead to data corruption which will be difficult to debug. * * @param value the bytes to be written. Upon returning from this call, the {@code position} of - * this buffer will be set to the {@code limit} + * this buffer will be set to the {@code limit} * @throws IOException thrown if an error occurred while writing */ public abstract void writeLazy(ByteBuffer value) throws IOException; diff --git a/java/core/src/main/java/com/google/protobuf/ByteString.java b/java/core/src/main/java/com/google/protobuf/ByteString.java index ddda0f263d..e5454a63d3 100644 --- a/java/core/src/main/java/com/google/protobuf/ByteString.java +++ b/java/core/src/main/java/com/google/protobuf/ByteString.java @@ -67,41 +67,36 @@ import java.util.NoSuchElementException; public abstract class ByteString implements Iterable, Serializable { /** - * When two strings to be concatenated have a combined length shorter than - * this, we just copy their bytes on {@link #concat(ByteString)}. - * The trade-off is copy size versus the overhead of creating tree nodes - * in {@link RopeByteString}. + * When two strings to be concatenated have a combined length shorter than this, we just copy + * their bytes on {@link #concat(ByteString)}. The trade-off is copy size versus the overhead of + * creating tree nodes in {@link RopeByteString}. */ static final int CONCATENATE_BY_COPY_SIZE = 128; /** - * When copying an InputStream into a ByteString with .readFrom(), - * the chunks in the underlying rope start at 256 bytes, but double - * each iteration up to 8192 bytes. + * When copying an InputStream into a ByteString with .readFrom(), the chunks in the underlying + * rope start at 256 bytes, but double each iteration up to 8192 bytes. */ - static final int MIN_READ_FROM_CHUNK_SIZE = 0x100; // 256b - static final int MAX_READ_FROM_CHUNK_SIZE = 0x2000; // 8k + static final int MIN_READ_FROM_CHUNK_SIZE = 0x100; // 256b - /** - * Empty {@code ByteString}. - */ + static final int MAX_READ_FROM_CHUNK_SIZE = 0x2000; // 8k + + /** Empty {@code ByteString}. */ public static final ByteString EMPTY = new LiteralByteString(Internal.EMPTY_BYTE_ARRAY); /** * An interface to efficiently copy {@code byte[]}. * - *

One of the noticeable costs of copying a byte[] into a new array using - * {@code System.arraycopy} is nullification of a new buffer before the copy. It has been shown - * the Hotspot VM is capable to intrisicfy {@code Arrays.copyOfRange} operation to avoid this + *

One of the noticeable costs of copying a byte[] into a new array using {@code + * System.arraycopy} is nullification of a new buffer before the copy. It has been shown the + * Hotspot VM is capable to intrisicfy {@code Arrays.copyOfRange} operation to avoid this * expensive nullification and provide substantial performance gain. Unfortunately this does not - * hold on Android runtimes and could make the copy slightly slower due to additional code in - * the {@code Arrays.copyOfRange}. Thus we provide two different implementation for array copier - * for Hotspot and Android runtimes. + * hold on Android runtimes and could make the copy slightly slower due to additional code in the + * {@code Arrays.copyOfRange}. Thus we provide two different implementation for array copier for + * Hotspot and Android runtimes. */ private interface ByteArrayCopier { - /** - * Copies the specified range of the specified array into a new array - */ + /** Copies the specified range of the specified array into a new array */ byte[] copyFrom(byte[] bytes, int offset, int size); } @@ -124,15 +119,16 @@ public abstract class ByteString implements Iterable, Serializable { } private static final ByteArrayCopier byteArrayCopier; + static { byteArrayCopier = Android.isOnAndroidDevice() ? new SystemByteArrayCopier() : new ArraysByteArrayCopier(); } /** - * Cached hash value. Intentionally accessed via a data race, which - * is safe because of the Java Memory Model's "no out-of-thin-air values" - * guarantees for ints. A value of 0 implies that the hash has not been set. + * Cached hash value. Intentionally accessed via a data race, which is safe because of the Java + * Memory Model's "no out-of-thin-air values" guarantees for ints. A value of 0 implies that the + * hash has not been set. */ private int hash = 0; @@ -140,10 +136,9 @@ public abstract class ByteString implements Iterable, Serializable { ByteString() {} /** - * Gets the byte at the given index. This method should be used only for - * random access to individual bytes. To access bytes sequentially, use the - * {@link ByteIterator} returned by {@link #iterator()}, and call {@link - * #substring(int, int)} first if necessary. + * Gets the byte at the given index. This method should be used only for random access to + * individual bytes. To access bytes sequentially, use the {@link ByteIterator} returned by {@link + * #iterator()}, and call {@link #substring(int, int)} first if necessary. * * @param index index of byte * @return the value @@ -152,15 +147,23 @@ public abstract class ByteString implements Iterable, Serializable { public abstract byte byteAt(int index); /** - * Return a {@link ByteString.ByteIterator} over the bytes in the ByteString. - * To avoid auto-boxing, you may get the iterator manually and call - * {@link ByteIterator#nextByte()}. + * Gets the byte at the given index, assumes bounds checking has already been performed. + * + * @param index index of byte + * @return the value + * @throws IndexOutOfBoundsException {@code index < 0 or index >= size} + */ + abstract byte internalByteAt(int index); + + /** + * Return a {@link ByteString.ByteIterator} over the bytes in the ByteString. To avoid + * auto-boxing, you may get the iterator manually and call {@link ByteIterator#nextByte()}. * * @return the iterator */ @Override - public final ByteIterator iterator() { - return new ByteIterator() { + public ByteIterator iterator() { + return new AbstractByteIterator() { private int position = 0; private final int limit = size(); @@ -169,36 +172,24 @@ public abstract class ByteString implements Iterable, Serializable { return position < limit; } - @Override - public Byte next() { - // Boxing calls Byte.valueOf(byte), which does not instantiate. - return nextByte(); - } - @Override public byte nextByte() { - try { - return byteAt(position++); - } catch (IndexOutOfBoundsException e) { - throw new NoSuchElementException(e.getMessage()); + int currentPos = position; + if (currentPos >= limit) { + throw new NoSuchElementException(); } - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); + position = currentPos + 1; + return internalByteAt(currentPos); } }; } /** - * This interface extends {@code Iterator}, so that we can return an - * unboxed {@code byte}. + * This interface extends {@code Iterator}, so that we can return an unboxed {@code byte}. */ public interface ByteIterator extends Iterator { /** - * An alternative to {@link Iterator#next()} that returns an - * unboxed primitive {@code byte}. + * An alternative to {@link Iterator#next()} that returns an unboxed primitive {@code byte}. * * @return the next {@code byte} in the iteration * @throws NoSuchElementException if the iteration has no more elements @@ -206,6 +197,19 @@ public abstract class ByteString implements Iterable, Serializable { byte nextByte(); } + abstract static class AbstractByteIterator implements ByteIterator { + @Override + public final Byte next() { + // Boxing calls Byte.valueOf(byte), which does not instantiate. + return nextByte(); + } + + @Override + public final void remove() { + throw new UnsupportedOperationException(); + } + } + /** * Gets the number of bytes. * @@ -243,8 +247,8 @@ public abstract class ByteString implements Iterable, Serializable { * Compares two {@link ByteString}s lexicographically, treating their contents as unsigned byte * values between 0 and 255 (inclusive). * - *

For example, {@code (byte) -1} is considered to be greater than {@code (byte) 1} because - * it is interpreted as an unsigned value, {@code 255}. + *

For example, {@code (byte) -1} is considered to be greater than {@code (byte) 1} because it + * is interpreted as an unsigned value, {@code 255}. */ private static final Comparator UNSIGNED_LEXICOGRAPHICAL_COMPARATOR = new Comparator() { @@ -268,15 +272,15 @@ public abstract class ByteString implements Iterable, Serializable { }; /** - * Returns a {@link Comparator} which compares {@link ByteString}-s lexicographically + * Returns a {@link Comparator} which compares {@link ByteString}-s lexicographically * as sequences of unsigned bytes (i.e. values between 0 and 255, inclusive). * - *

For example, {@code (byte) -1} is considered to be greater than {@code (byte) 1} because - * it is interpreted as an unsigned value, {@code 255}: + *

For example, {@code (byte) -1} is considered to be greater than {@code (byte) 1} because it + * is interpreted as an unsigned value, {@code 255}: * *

    - *
  • {@code `-1` -> 0b11111111 (two's complement) -> 255} - *
  • {@code `1` -> 0b00000001 -> 1} + *
  • {@code `-1` -> 0b11111111 (two's complement) -> 255} + *
  • {@code `1` -> 0b00000001 -> 1} *
*/ public static Comparator unsignedLexicographicalComparator() { @@ -287,56 +291,49 @@ public abstract class ByteString implements Iterable, Serializable { // ByteString -> substring /** - * Return the substring from {@code beginIndex}, inclusive, to the end of the - * string. + * Return the substring from {@code beginIndex}, inclusive, to the end of the string. * * @param beginIndex start at this index * @return substring sharing underlying data - * @throws IndexOutOfBoundsException if {@code beginIndex < 0} or - * {@code beginIndex > size()}. + * @throws IndexOutOfBoundsException if {@code beginIndex < 0} or {@code beginIndex > size()}. */ public final ByteString substring(int beginIndex) { return substring(beginIndex, size()); } /** - * Return the substring from {@code beginIndex}, inclusive, to {@code - * endIndex}, exclusive. + * Return the substring from {@code beginIndex}, inclusive, to {@code endIndex}, exclusive. * * @param beginIndex start at this index - * @param endIndex the last character is the one before this index + * @param endIndex the last character is the one before this index * @return substring sharing underlying data - * @throws IndexOutOfBoundsException if {@code beginIndex < 0}, - * {@code endIndex > size()}, or {@code beginIndex > endIndex}. + * @throws IndexOutOfBoundsException if {@code beginIndex < 0}, {@code endIndex > size()}, or + * {@code beginIndex > endIndex}. */ public abstract ByteString substring(int beginIndex, int endIndex); /** - * Tests if this bytestring starts with the specified prefix. - * Similar to {@link String#startsWith(String)} + * Tests if this bytestring starts with the specified prefix. Similar to {@link + * String#startsWith(String)} * * @param prefix the prefix. - * @return true if the byte sequence represented by the - * argument is a prefix of the byte sequence represented by - * this string; false otherwise. + * @return true if the byte sequence represented by the argument is a prefix of the + * byte sequence represented by this string; false otherwise. */ public final boolean startsWith(ByteString prefix) { - return size() >= prefix.size() && - substring(0, prefix.size()).equals(prefix); + return size() >= prefix.size() && substring(0, prefix.size()).equals(prefix); } /** - * Tests if this bytestring ends with the specified suffix. - * Similar to {@link String#endsWith(String)} + * Tests if this bytestring ends with the specified suffix. Similar to {@link + * String#endsWith(String)} * * @param suffix the suffix. - * @return true if the byte sequence represented by the - * argument is a suffix of the byte sequence represented by - * this string; false otherwise. + * @return true if the byte sequence represented by the argument is a suffix of the + * byte sequence represented by this string; false otherwise. */ public final boolean endsWith(ByteString suffix) { - return size() >= suffix.size() && - substring(size() - suffix.size()).equals(suffix); + return size() >= suffix.size() && substring(size() - suffix.size()).equals(suffix); } // ================================================================= @@ -366,9 +363,7 @@ public abstract class ByteString implements Iterable, Serializable { return copyFrom(bytes, 0, bytes.length); } - /** - * Wraps the given bytes into a {@code ByteString}. Intended for internal only usage. - */ + /** Wraps the given bytes into a {@code ByteString}. Intended for internal only usage. */ static ByteString wrap(ByteBuffer buffer) { if (buffer.hasArray()) { final int offset = buffer.arrayOffset(); @@ -379,8 +374,8 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Wraps the given bytes into a {@code ByteString}. Intended for internal only - * usage to force a classload of ByteString before LiteralByteString. + * Wraps the given bytes into a {@code ByteString}. Intended for internal only usage to force a + * classload of ByteString before LiteralByteString. */ static ByteString wrap(byte[] bytes) { // TODO(dweis): Return EMPTY when bytes are empty to reduce allocations? @@ -388,17 +383,16 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Wraps the given bytes into a {@code ByteString}. Intended for internal only - * usage to force a classload of ByteString before BoundedByteString and - * LiteralByteString. + * Wraps the given bytes into a {@code ByteString}. Intended for internal only usage to force a + * classload of ByteString before BoundedByteString and LiteralByteString. */ static ByteString wrap(byte[] bytes, int offset, int length) { return new BoundedByteString(bytes, offset, length); } /** - * Copies the next {@code size} bytes from a {@code java.nio.ByteBuffer} into - * a {@code ByteString}. + * Copies the next {@code size} bytes from a {@code java.nio.ByteBuffer} into a {@code + * ByteString}. * * @param bytes source buffer * @param size number of bytes to copy @@ -413,8 +407,7 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Copies the remaining bytes from a {@code java.nio.ByteBuffer} into - * a {@code ByteString}. + * Copies the remaining bytes from a {@code java.nio.ByteBuffer} into a {@code ByteString}. * * @param bytes sourceBuffer * @return new {@code ByteString} @@ -424,8 +417,8 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Encodes {@code text} into a sequence of bytes using the named charset - * and returns the result as a {@code ByteString}. + * Encodes {@code text} into a sequence of bytes using the named charset and returns the result as + * a {@code ByteString}. * * @param text source string * @param charsetName encoding to use @@ -438,8 +431,8 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Encodes {@code text} into a sequence of bytes using the named charset - * and returns the result as a {@code ByteString}. + * Encodes {@code text} into a sequence of bytes using the named charset and returns the result as + * a {@code ByteString}. * * @param text source string * @param charset encode using this charset @@ -450,8 +443,8 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Encodes {@code text} into a sequence of UTF-8 bytes and returns the - * result as a {@code ByteString}. + * Encodes {@code text} into a sequence of UTF-8 bytes and returns the result as a {@code + * ByteString}. * * @param text source string * @return new {@code ByteString} @@ -464,60 +457,48 @@ public abstract class ByteString implements Iterable, Serializable { // InputStream -> ByteString /** - * Completely reads the given stream's bytes into a - * {@code ByteString}, blocking if necessary until all bytes are - * read through to the end of the stream. + * Completely reads the given stream's bytes into a {@code ByteString}, blocking if necessary + * until all bytes are read through to the end of the stream. * - * Performance notes: The returned {@code ByteString} is an - * immutable tree of byte arrays ("chunks") of the stream data. The - * first chunk is small, with subsequent chunks each being double - * the size, up to 8K. + *

Performance notes: The returned {@code ByteString} is an immutable tree of byte + * arrays ("chunks") of the stream data. The first chunk is small, with subsequent chunks each + * being double the size, up to 8K. * - *

Each byte read from the input stream will be copied twice to ensure - * that the resulting ByteString is truly immutable. + *

Each byte read from the input stream will be copied twice to ensure that the resulting + * ByteString is truly immutable. * - * @param streamToDrain The source stream, which is read completely - * but not closed. - * @return A new {@code ByteString} which is made up of chunks of - * various sizes, depending on the behavior of the underlying - * stream. - * @throws IOException IOException is thrown if there is a problem - * reading the underlying stream. + * @param streamToDrain The source stream, which is read completely but not closed. + * @return A new {@code ByteString} which is made up of chunks of various sizes, depending on the + * behavior of the underlying stream. + * @throws IOException IOException is thrown if there is a problem reading the underlying stream. */ - public static ByteString readFrom(InputStream streamToDrain) - throws IOException { + public static ByteString readFrom(InputStream streamToDrain) throws IOException { return readFrom(streamToDrain, MIN_READ_FROM_CHUNK_SIZE, MAX_READ_FROM_CHUNK_SIZE); } /** - * Completely reads the given stream's bytes into a - * {@code ByteString}, blocking if necessary until all bytes are - * read through to the end of the stream. + * Completely reads the given stream's bytes into a {@code ByteString}, blocking if necessary + * until all bytes are read through to the end of the stream. * - * Performance notes: The returned {@code ByteString} is an - * immutable tree of byte arrays ("chunks") of the stream data. The - * chunkSize parameter sets the size of these byte arrays. + *

Performance notes: The returned {@code ByteString} is an immutable tree of byte + * arrays ("chunks") of the stream data. The chunkSize parameter sets the size of these byte + * arrays. * - *

Each byte read from the input stream will be copied twice to ensure - * that the resulting ByteString is truly immutable. + *

Each byte read from the input stream will be copied twice to ensure that the resulting + * ByteString is truly immutable. * - * @param streamToDrain The source stream, which is read completely - * but not closed. - * @param chunkSize The size of the chunks in which to read the - * stream. - * @return A new {@code ByteString} which is made up of chunks of - * the given size. - * @throws IOException IOException is thrown if there is a problem - * reading the underlying stream. + * @param streamToDrain The source stream, which is read completely but not closed. + * @param chunkSize The size of the chunks in which to read the stream. + * @return A new {@code ByteString} which is made up of chunks of the given size. + * @throws IOException IOException is thrown if there is a problem reading the underlying stream. */ - public static ByteString readFrom(InputStream streamToDrain, int chunkSize) - throws IOException { + public static ByteString readFrom(InputStream streamToDrain, int chunkSize) throws IOException { return readFrom(streamToDrain, chunkSize, chunkSize); } // Helper method that takes the chunk size range as a parameter. - public static ByteString readFrom(InputStream streamToDrain, int minChunkSize, - int maxChunkSize) throws IOException { + public static ByteString readFrom(InputStream streamToDrain, int minChunkSize, int maxChunkSize) + throws IOException { Collection results = new ArrayList(); // copy the inbound bytes into a list of chunks; the chunk size @@ -536,42 +517,39 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Blocks until a chunk of the given size can be made from the - * stream, or EOF is reached. Calls read() repeatedly in case the - * given stream implementation doesn't completely fill the given + * Blocks until a chunk of the given size can be made from the stream, or EOF is reached. Calls + * read() repeatedly in case the given stream implementation doesn't completely fill the given * buffer in one read() call. * - * @return A chunk of the desired size, or else a chunk as large as - * was available when end of stream was reached. Returns null if the - * given stream had no more data in it. + * @return A chunk of the desired size, or else a chunk as large as was available when end of + * stream was reached. Returns null if the given stream had no more data in it. */ - private static ByteString readChunk(InputStream in, final int chunkSize) - throws IOException { - final byte[] buf = new byte[chunkSize]; - int bytesRead = 0; - while (bytesRead < chunkSize) { - final int count = in.read(buf, bytesRead, chunkSize - bytesRead); - if (count == -1) { - break; - } - bytesRead += count; + private static ByteString readChunk(InputStream in, final int chunkSize) throws IOException { + final byte[] buf = new byte[chunkSize]; + int bytesRead = 0; + while (bytesRead < chunkSize) { + final int count = in.read(buf, bytesRead, chunkSize - bytesRead); + if (count == -1) { + break; } + bytesRead += count; + } - if (bytesRead == 0) { - return null; - } + if (bytesRead == 0) { + return null; + } - // Always make a copy since InputStream could steal a reference to buf. - return ByteString.copyFrom(buf, 0, bytesRead); + // Always make a copy since InputStream could steal a reference to buf. + return ByteString.copyFrom(buf, 0, bytesRead); } // ================================================================= // Multiple ByteStrings -> One ByteString /** - * Concatenate the given {@code ByteString} to this one. Short concatenations, - * of total size smaller than {@link ByteString#CONCATENATE_BY_COPY_SIZE}, are - * produced by copying the underlying bytes (as per Rope.java, * BAP95 . In general, the concatenate involves no copying. * @@ -580,21 +558,20 @@ public abstract class ByteString implements Iterable, Serializable { */ public final ByteString concat(ByteString other) { if (Integer.MAX_VALUE - size() < other.size()) { - throw new IllegalArgumentException("ByteString would be too long: " + - size() + "+" + other.size()); + throw new IllegalArgumentException( + "ByteString would be too long: " + size() + "+" + other.size()); } return RopeByteString.concatenate(this, other); } /** - * Concatenates all byte strings in the iterable and returns the result. - * This is designed to run in O(list size), not O(total bytes). + * Concatenates all byte strings in the iterable and returns the result. This is designed to run + * in O(list size), not O(total bytes). * - *

The returned {@code ByteString} is not necessarily a unique object. - * If the list is empty, the returned object is the singleton empty - * {@code ByteString}. If the list has only one element, that - * {@code ByteString} will be returned without copying. + *

The returned {@code ByteString} is not necessarily a unique object. If the list is empty, + * the returned object is the singleton empty {@code ByteString}. If the list has only one + * element, that {@code ByteString} will be returned without copying. * * @param byteStrings strings to be concatenated * @return new {@code ByteString} @@ -604,9 +581,9 @@ public abstract class ByteString implements Iterable, Serializable { final int size; if (!(byteStrings instanceof Collection)) { int tempSize = 0; - for (Iterator iter = byteStrings.iterator(); iter.hasNext(); - iter.next(), ++tempSize) { - } + for (Iterator iter = byteStrings.iterator(); + iter.hasNext(); + iter.next(), ++tempSize) {} size = tempSize; } else { size = ((Collection) byteStrings).size(); @@ -663,7 +640,7 @@ public abstract class ByteString implements Iterable, Serializable { * @param targetOffset offset within the target buffer * @param numberToCopy number of bytes to copy * @throws IndexOutOfBoundsException if an offset or size is negative or too large - * @deprecation Instead, call {@code byteString.substring(sourceOffset, sourceOffset + + * @deprecated Instead, call {@code byteString.substring(sourceOffset, sourceOffset + * numberToCopy).copyTo(target, targetOffset)} */ @Deprecated @@ -676,13 +653,11 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Internal (package private) implementation of - * {@link #copyTo(byte[],int,int,int)}. - * It assumes that all error checking has already been performed and that - * {@code numberToCopy > 0}. + * Internal (package private) implementation of {@link #copyTo(byte[],int,int,int)}. It assumes + * that all error checking has already been performed and that {@code numberToCopy > 0}. */ - protected abstract void copyToInternal(byte[] target, int sourceOffset, - int targetOffset, int numberToCopy); + protected abstract void copyToInternal( + byte[] target, int sourceOffset, int targetOffset, int numberToCopy); /** * Copies bytes into a ByteBuffer. @@ -715,22 +690,21 @@ public abstract class ByteString implements Iterable, Serializable { /** * Writes a copy of the contents of this byte string to the specified output stream argument. * - * @param out the output stream to which to write the data. - * @throws IOException if an I/O error occurs. + * @param out the output stream to which to write the data. + * @throws IOException if an I/O error occurs. */ public abstract void writeTo(OutputStream out) throws IOException; /** * Writes a specified part of this byte string to an output stream. * - * @param out the output stream to which to write the data. - * @param sourceOffset offset within these bytes - * @param numberToWrite number of bytes to write - * @throws IOException if an I/O error occurs. + * @param out the output stream to which to write the data. + * @param sourceOffset offset within these bytes + * @param numberToWrite number of bytes to write + * @throws IOException if an I/O error occurs. * @throws IndexOutOfBoundsException if an offset or size is negative or too large */ - final void writeTo(OutputStream out, int sourceOffset, int numberToWrite) - throws IOException { + final void writeTo(OutputStream out, int sourceOffset, int numberToWrite) throws IOException { checkRange(sourceOffset, sourceOffset + numberToWrite, size()); if (numberToWrite > 0) { writeToInternal(out, sourceOffset, numberToWrite); @@ -738,59 +712,65 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Internal version of {@link #writeTo(OutputStream,int,int)} that assumes - * all error checking has already been done. + * Internal version of {@link #writeTo(OutputStream,int,int)} that assumes all error checking has + * already been done. */ abstract void writeToInternal(OutputStream out, int sourceOffset, int numberToWrite) throws IOException; /** - * Writes this {@link ByteString} to the provided {@link ByteOutput}. Calling - * this method may result in multiple operations on the target {@link ByteOutput}. + * Writes this {@link ByteString} to the provided {@link ByteOutput}. Calling this method may + * result in multiple operations on the target {@link ByteOutput}. * *

This method may expose internal backing buffers of the {@link ByteString} to the {@link * ByteOutput} in order to avoid additional copying overhead. It would be possible for a malicious * {@link ByteOutput} to corrupt the {@link ByteString}. Use with caution! * - * @param byteOutput the output target to receive the bytes - * @throws IOException if an I/O error occurs + * @param byteOutput the output target to receive the bytes + * @throws IOException if an I/O error occurs * @see UnsafeByteOperations#unsafeWriteTo(ByteString, ByteOutput) */ abstract void writeTo(ByteOutput byteOutput) throws IOException; + /** + * This method behaves exactly the same as {@link #writeTo(ByteOutput)} unless the {@link + * ByteString} is a rope. For ropes, the leaf nodes are written in reverse order to the {@code + * byteOutput}. + * + * @param byteOutput the output target to receive the bytes + * @throws IOException if an I/O error occurs + * @see UnsafeByteOperations#unsafeWriteToReverse(ByteString, ByteOutput) + */ + abstract void writeToReverse(ByteOutput byteOutput) throws IOException; /** - * Constructs a read-only {@code java.nio.ByteBuffer} whose content - * is equal to the contents of this byte string. - * The result uses the same backing array as the byte string, if possible. + * Constructs a read-only {@code java.nio.ByteBuffer} whose content is equal to the contents of + * this byte string. The result uses the same backing array as the byte string, if possible. * * @return wrapped bytes */ public abstract ByteBuffer asReadOnlyByteBuffer(); /** - * Constructs a list of read-only {@code java.nio.ByteBuffer} objects - * such that the concatenation of their contents is equal to the contents - * of this byte string. The result uses the same backing arrays as the - * byte string. - *

- * By returning a list, implementations of this method may be able to avoid - * copying even when there are multiple backing arrays. + * Constructs a list of read-only {@code java.nio.ByteBuffer} objects such that the concatenation + * of their contents is equal to the contents of this byte string. The result uses the same + * backing arrays as the byte string. + * + *

By returning a list, implementations of this method may be able to avoid copying even when + * there are multiple backing arrays. * * @return a list of wrapped bytes */ public abstract List asReadOnlyByteBufferList(); /** - * Constructs a new {@code String} by decoding the bytes using the - * specified charset. + * Constructs a new {@code String} by decoding the bytes using the specified charset. * * @param charsetName encode using this charset * @return new string * @throws UnsupportedEncodingException if charset isn't recognized */ - public final String toString(String charsetName) - throws UnsupportedEncodingException { + public final String toString(String charsetName) throws UnsupportedEncodingException { try { return toString(Charset.forName(charsetName)); } catch (UnsupportedCharsetException e) { @@ -801,8 +781,8 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Constructs a new {@code String} by decoding the bytes using the - * specified charset. Returns the same empty String if empty. + * Constructs a new {@code String} by decoding the bytes using the specified charset. Returns the + * same empty String if empty. * * @param charset encode using this charset * @return new string @@ -812,8 +792,7 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Constructs a new {@code String} by decoding the bytes using the - * specified charset. + * Constructs a new {@code String} by decoding the bytes using the specified charset. * * @param charset encode using this charset * @return new string @@ -833,50 +812,45 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Tells whether this {@code ByteString} represents a well-formed UTF-8 - * byte sequence, such that the original bytes can be converted to a - * String object and then round tripped back to bytes without loss. + * Tells whether this {@code ByteString} represents a well-formed UTF-8 byte sequence, such that + * the original bytes can be converted to a String object and then round tripped back to bytes + * without loss. * - *

More precisely, returns {@code true} whenever:

 {@code
+   * 

More precisely, returns {@code true} whenever: + * + *

{@code
    * Arrays.equals(byteString.toByteArray(),
    *     new String(byteString.toByteArray(), "UTF-8").getBytes("UTF-8"))
    * }
* - *

This method returns {@code false} for "overlong" byte sequences, - * as well as for 3-byte sequences that would map to a surrogate - * character, in accordance with the restricted definition of UTF-8 - * introduced in Unicode 3.1. Note that the UTF-8 decoder included in - * Oracle's JDK has been modified to also reject "overlong" byte - * sequences, but (as of 2011) still accepts 3-byte surrogate - * character byte sequences. + *

This method returns {@code false} for "overlong" byte sequences, as well as for 3-byte + * sequences that would map to a surrogate character, in accordance with the restricted definition + * of UTF-8 introduced in Unicode 3.1. Note that the UTF-8 decoder included in Oracle's JDK has + * been modified to also reject "overlong" byte sequences, but (as of 2011) still accepts 3-byte + * surrogate character byte sequences. * *

See the Unicode Standard,
* Table 3-6. UTF-8 Bit Distribution,
* Table 3-7. Well Formed UTF-8 Byte Sequences. * - * @return whether the bytes in this {@code ByteString} are a - * well-formed UTF-8 byte sequence + * @return whether the bytes in this {@code ByteString} are a well-formed UTF-8 byte sequence */ public abstract boolean isValidUtf8(); /** - * Tells whether the given byte sequence is a well-formed, malformed, or - * incomplete UTF-8 byte sequence. This method accepts and returns a partial - * state result, allowing the bytes for a complete UTF-8 byte sequence to be - * composed from multiple {@code ByteString} segments. + * Tells whether the given byte sequence is a well-formed, malformed, or incomplete UTF-8 byte + * sequence. This method accepts and returns a partial state result, allowing the bytes for a + * complete UTF-8 byte sequence to be composed from multiple {@code ByteString} segments. * - * @param state either {@code 0} (if this is the initial decoding operation) - * or the value returned from a call to a partial decoding method for the - * previous bytes + * @param state either {@code 0} (if this is the initial decoding operation) or the value returned + * from a call to a partial decoding method for the previous bytes * @param offset offset of the first byte to check * @param length number of bytes to check - * - * @return {@code -1} if the partial byte sequence is definitely malformed, - * {@code 0} if it is well-formed (no additional input needed), or, if the - * byte sequence is "incomplete", i.e. apparently terminated in the middle of - * a character, an opaque integer "state" value containing enough information - * to decode the character when passed to a subsequent invocation of a - * partial decoding method. + * @return {@code -1} if the partial byte sequence is definitely malformed, {@code 0} if it is + * well-formed (no additional input needed), or, if the byte sequence is "incomplete", i.e. + * apparently terminated in the middle of a character, an opaque integer "state" value + * containing enough information to decode the character when passed to a subsequent + * invocation of a partial decoding method. */ protected abstract int partialIsValidUtf8(int state, int offset, int length); @@ -886,9 +860,7 @@ public abstract class ByteString implements Iterable, Serializable { @Override public abstract boolean equals(Object o); - /** - * Base class for leaf {@link ByteString}s (i.e. non-ropes). - */ + /** Base class for leaf {@link ByteString}s (i.e. non-ropes). */ abstract static class LeafByteString extends ByteString { @Override protected final int getTreeDepth() { @@ -900,12 +872,16 @@ public abstract class ByteString implements Iterable, Serializable { return true; } + @Override + void writeToReverse(ByteOutput byteOutput) throws IOException { + writeTo(byteOutput); + } /** - * Check equality of the substring of given length of this object starting at - * zero with another {@code ByteString} substring starting at offset. + * Check equality of the substring of given length of this object starting at zero with another + * {@code ByteString} substring starting at offset. * - * @param other what to compare a substring in + * @param other what to compare a substring in * @param offset offset into other * @param length number of bytes to compare * @return true for equality of substrings, else false. @@ -914,8 +890,7 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Compute the hashCode using the traditional algorithm from {@link - * ByteString}. + * Compute the hashCode using the traditional algorithm from {@link ByteString}. * * @return hashCode value */ @@ -939,26 +914,23 @@ public abstract class ByteString implements Iterable, Serializable { /** * Creates an {@code InputStream} which can be used to read the bytes. - *

- * The {@link InputStream} returned by this method is guaranteed to be - * completely non-blocking. The method {@link InputStream#available()} - * returns the number of bytes remaining in the stream. The methods - * {@link InputStream#read(byte[])}, {@link InputStream#read(byte[],int,int)} - * and {@link InputStream#skip(long)} will read/skip as many bytes as are - * available. The method {@link InputStream#markSupported()} returns - * {@code true}. - *

- * The methods in the returned {@link InputStream} might not be - * thread safe. + * + *

The {@link InputStream} returned by this method is guaranteed to be completely non-blocking. + * The method {@link InputStream#available()} returns the number of bytes remaining in the stream. + * The methods {@link InputStream#read(byte[])}, {@link InputStream#read(byte[],int,int)} and + * {@link InputStream#skip(long)} will read/skip as many bytes as are available. The method {@link + * InputStream#markSupported()} returns {@code true}. + * + *

The methods in the returned {@link InputStream} might not be thread safe. * * @return an input stream that returns the bytes of this byte string. */ public abstract InputStream newInput(); /** - * Creates a {@link CodedInputStream} which can be used to read the bytes. - * Using this is often more efficient than creating a {@link CodedInputStream} - * that wraps the result of {@link #newInput()}. + * Creates a {@link CodedInputStream} which can be used to read the bytes. Using this is often + * more efficient than creating a {@link CodedInputStream} that wraps the result of {@link + * #newInput()}. * * @return stream based on wrapped data */ @@ -970,10 +942,10 @@ public abstract class ByteString implements Iterable, Serializable { /** * Creates a new {@link Output} with the given initial capacity. Call {@link * Output#toByteString()} to create the {@code ByteString} instance. - *

- * A {@link ByteString.Output} offers the same functionality as a - * {@link ByteArrayOutputStream}, except that it returns a {@link ByteString} - * rather than a {@code byte} array. + * + *

A {@link ByteString.Output} offers the same functionality as a {@link + * ByteArrayOutputStream}, except that it returns a {@link ByteString} rather than a {@code byte} + * array. * * @param initialCapacity estimate of number of bytes to be written * @return {@code OutputStream} for building a {@code ByteString} @@ -983,12 +955,12 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Creates a new {@link Output}. Call {@link Output#toByteString()} to create - * the {@code ByteString} instance. - *

- * A {@link ByteString.Output} offers the same functionality as a - * {@link ByteArrayOutputStream}, except that it returns a {@link ByteString} - * rather than a {@code byte array}. + * Creates a new {@link Output}. Call {@link Output#toByteString()} to create the {@code + * ByteString} instance. + * + *

A {@link ByteString.Output} offers the same functionality as a {@link + * ByteArrayOutputStream}, except that it returns a {@link ByteString} rather than a {@code byte + * array}. * * @return {@code OutputStream} for building a {@code ByteString} */ @@ -997,8 +969,8 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Outputs to a {@code ByteString} instance. Call {@link #toByteString()} to - * create the {@code ByteString} instance. + * Outputs to a {@code ByteString} instance. Call {@link #toByteString()} to create the {@code + * ByteString} instance. */ public static final class Output extends OutputStream { // Implementation note. @@ -1020,10 +992,9 @@ public abstract class ByteString implements Iterable, Serializable { private int bufferPos; /** - * Creates a new ByteString output stream with the specified - * initial capacity. + * Creates a new ByteString output stream with the specified initial capacity. * - * @param initialCapacity the initial capacity of the output stream. + * @param initialCapacity the initial capacity of the output stream. */ Output(int initialCapacity) { if (initialCapacity < 0) { @@ -1039,43 +1010,41 @@ public abstract class ByteString implements Iterable, Serializable { if (bufferPos == buffer.length) { flushFullBuffer(1); } - buffer[bufferPos++] = (byte)b; + buffer[bufferPos++] = (byte) b; } @Override - public synchronized void write(byte[] b, int offset, int length) { + public synchronized void write(byte[] b, int offset, int length) { if (length <= buffer.length - bufferPos) { // The bytes can fit into the current buffer. System.arraycopy(b, offset, buffer, bufferPos, length); bufferPos += length; } else { // Use up the current buffer - int copySize = buffer.length - bufferPos; + int copySize = buffer.length - bufferPos; System.arraycopy(b, offset, buffer, bufferPos, copySize); offset += copySize; length -= copySize; // Flush the buffer, and get a new buffer at least big enough to cover // what we still need to output flushFullBuffer(length); - System.arraycopy(b, offset, buffer, 0 /* count */, length); + System.arraycopy(b, offset, buffer, /* count= */ 0, length); bufferPos = length; } } /** - * Creates a byte string. Its size is the current size of this output - * stream and its output has been copied to it. + * Creates a byte string. Its size is the current size of this output stream and its output has + * been copied to it. * - * @return the current contents of this output stream, as a byte string. + * @return the current contents of this output stream, as a byte string. */ public synchronized ByteString toByteString() { flushLastBuffer(); return ByteString.copyFrom(flushedBuffers); } - /** - * Implement java.util.Arrays.copyOf() for jdk 1.5. - */ + /** Implement java.util.Arrays.copyOf() for jdk 1.5. */ private byte[] copyArray(byte[] buffer, int length) { byte[] result = new byte[length]; System.arraycopy(buffer, 0, result, 0, Math.min(buffer.length, length)); @@ -1083,11 +1052,11 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Writes the complete contents of this byte array output stream to - * the specified output stream argument. + * Writes the complete contents of this byte array output stream to the specified output stream + * argument. * * @param out the output stream to which to write the data. - * @throws IOException if an I/O error occurs. + * @throws IOException if an I/O error occurs. */ public void writeTo(OutputStream out) throws IOException { ByteString[] cachedFlushBuffers; @@ -1096,8 +1065,7 @@ public abstract class ByteString implements Iterable, Serializable { synchronized (this) { // Copy the information we need into local variables so as to hold // the lock for as short a time as possible. - cachedFlushBuffers = - flushedBuffers.toArray(new ByteString[flushedBuffers.size()]); + cachedFlushBuffers = flushedBuffers.toArray(new ByteString[flushedBuffers.size()]); cachedBuffer = buffer; cachedBufferPos = bufferPos; } @@ -1111,16 +1079,15 @@ public abstract class ByteString implements Iterable, Serializable { /** * Returns the current size of the output stream. * - * @return the current size of the output stream + * @return the current size of the output stream */ public synchronized int size() { return flushedBuffersTotalBytes + bufferPos; } /** - * Resets this stream, so that all currently accumulated output in the - * output stream is discarded. The output stream can be used again, - * reusing the already allocated buffer space. + * Resets this stream, so that all currently accumulated output in the output stream is + * discarded. The output stream can be used again, reusing the already allocated buffer space. */ public synchronized void reset() { flushedBuffers.clear(); @@ -1130,32 +1097,31 @@ public abstract class ByteString implements Iterable, Serializable { @Override public String toString() { - return String.format("", + return String.format( + "", Integer.toHexString(System.identityHashCode(this)), size()); } /** - * Internal function used by writers. The current buffer is full, and the - * writer needs a new buffer whose size is at least the specified minimum - * size. + * Internal function used by writers. The current buffer is full, and the writer needs a new + * buffer whose size is at least the specified minimum size. */ - private void flushFullBuffer(int minSize) { + private void flushFullBuffer(int minSize) { flushedBuffers.add(new LiteralByteString(buffer)); flushedBuffersTotalBytes += buffer.length; // We want to increase our total capacity by 50%, but as a minimum, // the new buffer should also at least be >= minSize and // >= initial Capacity. - int newSize = Math.max(initialCapacity, - Math.max(minSize, flushedBuffersTotalBytes >>> 1)); + int newSize = Math.max(initialCapacity, Math.max(minSize, flushedBuffersTotalBytes >>> 1)); buffer = new byte[newSize]; bufferPos = 0; } /** - * Internal function used by {@link #toByteString()}. The current buffer may - * or may not be full, but it needs to be flushed. + * Internal function used by {@link #toByteString()}. The current buffer may or may not be full, + * but it needs to be flushed. */ - private void flushLastBuffer() { + private void flushLastBuffer() { if (bufferPos < buffer.length) { if (bufferPos > 0) { byte[] bufferCopy = copyArray(buffer, bufferPos); @@ -1178,17 +1144,15 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Constructs a new {@code ByteString} builder, which allows you to - * efficiently construct a {@code ByteString} by writing to a {@link - * CodedOutputStream}. Using this is much more efficient than calling {@code - * newOutput()} and wrapping that in a {@code CodedOutputStream}. + * Constructs a new {@code ByteString} builder, which allows you to efficiently construct a {@code + * ByteString} by writing to a {@link CodedOutputStream}. Using this is much more efficient than + * calling {@code newOutput()} and wrapping that in a {@code CodedOutputStream}. * - *

This is package-private because it's a somewhat confusing interface. - * Users can call {@link Message#toByteString()} instead of calling this - * directly. + *

This is package-private because it's a somewhat confusing interface. Users can call {@link + * Message#toByteString()} instead of calling this directly. * - * @param size The target byte size of the {@code ByteString}. You must write - * exactly this many bytes before building the result. + * @param size The target byte size of the {@code ByteString}. You must write exactly this many + * bytes before building the result. * @return the builder */ static CodedBuilder newCodedBuilder(int size) { @@ -1224,16 +1188,16 @@ public abstract class ByteString implements Iterable, Serializable { // public API. /** - * Return the depth of the tree representing this {@code ByteString}, if any, - * whose root is this node. If this is a leaf node, return 0. + * Return the depth of the tree representing this {@code ByteString}, if any, whose root is this + * node. If this is a leaf node, return 0. * * @return tree depth or zero */ protected abstract int getTreeDepth(); /** - * Return {@code true} if this ByteString is literal (a leaf node) or a - * flat-enough tree in the sense of {@link RopeByteString}. + * Return {@code true} if this ByteString is literal (a leaf node) or a flat-enough tree in the + * sense of {@link RopeByteString}. * * @return true if the tree is flat enough */ @@ -1249,10 +1213,9 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Compute the hash across the value bytes starting with the given hash, and - * return the result. This is used to compute the hash across strings - * represented as a set of pieces by allowing the hash computation to be - * continued from piece to piece. + * Compute the hash across the value bytes starting with the given hash, and return the result. + * This is used to compute the hash across strings represented as a set of pieces by allowing the + * hash computation to be continued from piece to piece. * * @param h starting hash value * @param offset offset into this value to start looking at data values @@ -1304,16 +1267,15 @@ public abstract class ByteString implements Iterable, Serializable { @Override public final String toString() { - return String.format("", - Integer.toHexString(System.identityHashCode(this)), size()); + return String.format( + "", Integer.toHexString(System.identityHashCode(this)), size()); } /** - * This class implements a {@link com.google.protobuf.ByteString} backed by a - * single array of bytes, contiguous in memory. It supports substring by - * pointing to only a sub-range of the underlying byte array, meaning that a - * substring will reference the full byte-array of the string it's made from, - * exactly as with {@link String}. + * This class implements a {@link com.google.protobuf.ByteString} backed by a single array of + * bytes, contiguous in memory. It supports substring by pointing to only a sub-range of the + * underlying byte array, meaning that a substring will reference the full byte-array of the + * string it's made from, exactly as with {@link String}. * * @author carlanton@google.com (Carl Haverl) */ @@ -1325,8 +1287,7 @@ public abstract class ByteString implements Iterable, Serializable { protected final byte[] bytes; /** - * Creates a {@code LiteralByteString} backed by the given array, without - * copying. + * Creates a {@code LiteralByteString} backed by the given array, without copying. * * @param bytes array to wrap */ @@ -1345,6 +1306,11 @@ public abstract class ByteString implements Iterable, Serializable { return bytes[index]; } + @Override + byte internalByteAt(int index) { + return bytes[index]; + } + @Override public int size() { return bytes.length; @@ -1464,10 +1430,10 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Check equality of the substring of given length of this object starting at - * zero with another {@code LiteralByteString} substring starting at offset. + * Check equality of the substring of given length of this object starting at zero with another + * {@code LiteralByteString} substring starting at offset. * - * @param other what to compare a substring in + * @param other what to compare a substring in * @param offset offset into other * @param length number of bytes to compare * @return true for equality of substrings, else false. @@ -1487,10 +1453,10 @@ public abstract class ByteString implements Iterable, Serializable { byte[] thisBytes = bytes; byte[] otherBytes = lbsOther.bytes; int thisLimit = getOffsetIntoBytes() + length; - for ( - int thisIndex = getOffsetIntoBytes(), + for (int thisIndex = getOffsetIntoBytes(), otherIndex = lbsOther.getOffsetIntoBytes() + offset; - (thisIndex < thisLimit); ++thisIndex, ++otherIndex) { + (thisIndex < thisLimit); + ++thisIndex, ++otherIndex) { if (thisBytes[thisIndex] != otherBytes[otherIndex]) { return false; } @@ -1519,7 +1485,7 @@ public abstract class ByteString implements Iterable, Serializable { // We trust CodedInputStream not to modify the bytes, or to give anyone // else access to them. return CodedInputStream.newInstance( - bytes, getOffsetIntoBytes(), size(), true /* bufferIsImmutable */); + bytes, getOffsetIntoBytes(), size(), /* bufferIsImmutable= */ true); } // ================================================================= @@ -1536,14 +1502,12 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * This class is used to represent the substring of a {@link ByteString} over a - * single byte array. In terms of the public API of {@link ByteString}, you end - * up here by calling {@link ByteString#copyFrom(byte[])} followed by {@link - * ByteString#substring(int, int)}. + * This class is used to represent the substring of a {@link ByteString} over a single byte array. + * In terms of the public API of {@link ByteString}, you end up here by calling {@link + * ByteString#copyFrom(byte[])} followed by {@link ByteString#substring(int, int)}. * - *

This class contains most of the overhead involved in creating a substring - * from a {@link LiteralByteString}. The overhead involves some range-checking - * and two extra fields. + *

This class contains most of the overhead involved in creating a substring from a {@link + * LiteralByteString}. The overhead involves some range-checking and two extra fields. * * @author carlanton@google.com (Carl Haverl) */ @@ -1555,15 +1519,13 @@ public abstract class ByteString implements Iterable, Serializable { private final int bytesLength; /** - * Creates a {@code BoundedByteString} backed by the sub-range of given array, - * without copying. + * Creates a {@code BoundedByteString} backed by the sub-range of given array, without copying. * - * @param bytes array to wrap + * @param bytes array to wrap * @param offset index to first byte to use in bytes * @param length number of bytes to use from bytes - * @throws IllegalArgumentException if {@code offset < 0}, {@code length < 0}, - * or if {@code offset + length > - * bytes.length}. + * @throws IllegalArgumentException if {@code offset < 0}, {@code length < 0}, or if {@code + * offset + length > bytes.length}. */ BoundedByteString(byte[] bytes, int offset, int length) { super(bytes); @@ -1574,10 +1536,9 @@ public abstract class ByteString implements Iterable, Serializable { } /** - * Gets the byte at the given index. - * Throws {@link ArrayIndexOutOfBoundsException} - * for backwards-compatibility reasons although it would more properly be - * {@link IndexOutOfBoundsException}. + * Gets the byte at the given index. Throws {@link ArrayIndexOutOfBoundsException} for + * backwards-compatibility reasons although it would more properly be {@link + * IndexOutOfBoundsException}. * * @param index index of byte * @return the value @@ -1591,6 +1552,11 @@ public abstract class ByteString implements Iterable, Serializable { return bytes[bytesOffset + index]; } + @Override + byte internalByteAt(int index) { + return bytes[bytesOffset + index]; + } + @Override public int size() { return bytesLength; @@ -1605,10 +1571,10 @@ public abstract class ByteString implements Iterable, Serializable { // ByteString -> byte[] @Override - protected void copyToInternal(byte[] target, int sourceOffset, int targetOffset, - int numberToCopy) { - System.arraycopy(bytes, getOffsetIntoBytes() + sourceOffset, target, - targetOffset, numberToCopy); + protected void copyToInternal( + byte[] target, int sourceOffset, int targetOffset, int numberToCopy) { + System.arraycopy( + bytes, getOffsetIntoBytes() + sourceOffset, target, targetOffset, numberToCopy); } // ================================================================= diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java index 4545d0aed9..f2c7cc4723 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java @@ -72,13 +72,19 @@ public abstract class CodedInputStream { /** Visible for subclasses. See setSizeLimit() */ int sizeLimit = DEFAULT_SIZE_LIMIT; + /** Used to adapt to the experimental {@link Reader} interface. */ + CodedInputStreamReader wrapper; + /** Create a new CodedInputStream wrapping the given InputStream. */ public static CodedInputStream newInstance(final InputStream input) { return newInstance(input, DEFAULT_BUFFER_SIZE); } - /** Create a new CodedInputStream wrapping the given InputStream. */ - static CodedInputStream newInstance(final InputStream input, int bufferSize) { + /** Create a new CodedInputStream wrapping the given InputStream, with a specified buffer size. */ + public static CodedInputStream newInstance(final InputStream input, int bufferSize) { + if (bufferSize <= 0) { + throw new IllegalArgumentException("bufferSize must be > 0"); + } if (input == null) { // TODO(nathanmittler): Ideally we should throw here. This is done for backward compatibility. return newInstance(EMPTY_BYTE_ARRAY); @@ -130,7 +136,7 @@ public abstract class CodedInputStream { /** Create a new CodedInputStream wrapping the given byte array slice. */ public static CodedInputStream newInstance(final byte[] buf, final int off, final int len) { - return newInstance(buf, off, len, false /* bufferIsImmutable */); + return newInstance(buf, off, len, /* bufferIsImmutable= */ false); } /** Create a new CodedInputStream wrapping the given byte array slice. */ @@ -166,7 +172,7 @@ public abstract class CodedInputStream { * trying to alter the ByteBuffer's status. */ public static CodedInputStream newInstance(ByteBuffer buf) { - return newInstance(buf, false /* bufferIsImmutable */); + return newInstance(buf, /* bufferIsImmutable= */ false); } /** Create a new CodedInputStream wrapping the given buffer. */ @@ -374,7 +380,7 @@ public abstract class CodedInputStream { /** * Set the maximum message recursion depth. In order to prevent malicious messages from causing * stack overflows, {@code CodedInputStream} limits how deeply messages may be nested. The default - * limit is 64. + * limit is 100. * * @return the old limit. */ @@ -410,7 +416,6 @@ public abstract class CodedInputStream { return oldLimit; } - private boolean shouldDiscardUnknownFields = false; /** @@ -478,7 +483,9 @@ public abstract class CodedInputStream { /** * Returns true if the stream has reached the end of the input. This is the case if either the end * of the underlying input source has been reached or if the stream has reached a limit created - * using {@link #pushLimit(int)}. + * using {@link #pushLimit(int)}. This function may get blocked when using StreamDecoder as it + * invokes {@link #StreamDecoder.tryRefillBuffer(int)} in this function which will try to read + * bytes from input. */ public abstract boolean isAtEnd() throws IOException; @@ -2261,7 +2268,7 @@ public abstract class CodedInputStream { return result; } // Slow path: Build a byte array first then copy it. - return new String(readRawBytesSlowPath(size), UTF_8); + return new String(readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ false), UTF_8); } @Override @@ -2285,7 +2292,7 @@ public abstract class CodedInputStream { pos = tempPos + size; } else { // Slow path: Build a byte array first then copy it. - bytes = readRawBytesSlowPath(size); + bytes = readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ false); tempPos = 0; } return Utf8.decodeUtf8(bytes, tempPos, size); @@ -2390,7 +2397,8 @@ public abstract class CodedInputStream { return result; } else { // Slow path: Build a byte array first then copy it. - return readRawBytesSlowPath(size); + // TODO(dweis): Do we want to protect from malicious input streams here? + return readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ false); } } @@ -2407,7 +2415,10 @@ public abstract class CodedInputStream { return Internal.EMPTY_BYTE_BUFFER; } // Slow path: Build a byte array first then copy it. - return ByteBuffer.wrap(readRawBytesSlowPath(size)); + + // We must copy as the byte array was handed off to the InputStream and a malicious + // implementation could retain a reference. + return ByteBuffer.wrap(readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ true)); } @Override @@ -2781,7 +2792,8 @@ public abstract class CodedInputStream { sizeLimit - totalBytesRetired - bufferSize)); if (bytesRead == 0 || bytesRead < -1 || bytesRead > buffer.length) { throw new IllegalStateException( - "InputStream#read(byte[]) returned invalid result: " + input.getClass() + + "#read(byte[]) returned invalid result: " + bytesRead + "\nThe InputStream implementation is buggy."); } @@ -2809,19 +2821,24 @@ public abstract class CodedInputStream { pos = tempPos + size; return Arrays.copyOfRange(buffer, tempPos, tempPos + size); } else { - return readRawBytesSlowPath(size); + // TODO(dweis): Do we want to protect from malicious input streams here? + return readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ false); } } /** * Exactly like readRawBytes, but caller must have already checked the fast path: (size <= * (bufferSize - pos) && size > 0) + * + * If ensureNoLeakedReferences is true, the value is guaranteed to have not escaped to + * untrusted code. */ - private byte[] readRawBytesSlowPath(final int size) throws IOException { + private byte[] readRawBytesSlowPath( + final int size, boolean ensureNoLeakedReferences) throws IOException { // Attempt to read the data in one byte array when it's safe to do. byte[] result = readRawBytesSlowPathOneChunk(size); if (result != null) { - return result; + return ensureNoLeakedReferences ? result.clone() : result; } final int originalBufferPos = pos; @@ -2859,6 +2876,8 @@ public abstract class CodedInputStream { /** * Attempts to read the data in one byte array when it's safe to do. Returns null if the size to * read is too large and needs to be allocated in smaller chunks for security reasons. + * + * Returns a byte[] that may have escaped to user code via InputStream APIs. */ private byte[] readRawBytesSlowPathOneChunk(final int size) throws IOException { if (size == 0) { @@ -2913,7 +2932,11 @@ public abstract class CodedInputStream { return null; } - /** Reads the remaining data in small chunks from the input stream. */ + /** + * Reads the remaining data in small chunks from the input stream. + * + * Returns a byte[] that may have escaped to user code via InputStream APIs. + */ private List readRawBytesSlowPathRemainingChunks(int sizeLeft) throws IOException { // The size is very large. For security reasons, we can't allocate the // entire byte array yet. The size comes directly from the input, so a @@ -2950,7 +2973,9 @@ public abstract class CodedInputStream { private ByteString readBytesSlowPath(final int size) throws IOException { final byte[] result = readRawBytesSlowPathOneChunk(size); if (result != null) { - return ByteString.wrap(result); + // We must copy as the byte array was handed off to the InputStream and a malicious + // implementation could retain a reference. + return ByteString.copyFrom(result); } final int originalBufferPos = pos; @@ -2968,13 +2993,20 @@ public abstract class CodedInputStream { // chunks. List chunks = readRawBytesSlowPathRemainingChunks(sizeLeft); - // Wrap the byte arrays into a single ByteString. - List byteStrings = new ArrayList(1 + chunks.size()); - byteStrings.add(ByteString.copyFrom(buffer, originalBufferPos, bufferedBytes)); - for (byte[] chunk : chunks) { - byteStrings.add(ByteString.wrap(chunk)); + // OK, got everything. Now concatenate it all into one buffer. + final byte[] bytes = new byte[size]; + + // Start by copying the leftover bytes from this.buffer. + System.arraycopy(buffer, originalBufferPos, bytes, 0, bufferedBytes); + + // And now all the chunks. + int tempPos = bufferedBytes; + for (final byte[] chunk : chunks) { + System.arraycopy(chunk, 0, bytes, tempPos, chunk.length); + tempPos += chunk.length; } - return ByteString.copyFrom(byteStrings); + + return ByteString.wrap(bytes); } @Override @@ -3003,20 +3035,54 @@ public abstract class CodedInputStream { throw InvalidProtocolBufferException.truncatedMessage(); } - // Skipping more bytes than are in the buffer. First skip what we have. - int tempPos = bufferSize - pos; - pos = bufferSize; + int totalSkipped = 0; + if (refillCallback == null) { + // Skipping more bytes than are in the buffer. First skip what we have. + totalBytesRetired += pos; + totalSkipped = bufferSize - pos; + bufferSize = 0; + pos = 0; - // Keep refilling the buffer until we get to the point we wanted to skip to. - // This has the side effect of ensuring the limits are updated correctly. - refillBuffer(1); - while (size - tempPos > bufferSize) { - tempPos += bufferSize; + try { + while (totalSkipped < size) { + int toSkip = size - totalSkipped; + long skipped = input.skip(toSkip); + if (skipped < 0 || skipped > toSkip) { + throw new IllegalStateException( + input.getClass() + + "#skip returned invalid result: " + + skipped + + "\nThe InputStream implementation is buggy."); + } else if (skipped == 0) { + // The API contract of skip() permits an inputstream to skip zero bytes for any reason + // it wants. In particular, ByteArrayInputStream will just return zero over and over + // when it's at the end of its input. In order to actually confirm that we've hit the + // end of input, we need to issue a read call via the other path. + break; + } + totalSkipped += (int) skipped; + } + } finally { + totalBytesRetired += totalSkipped; + recomputeBufferSizeAfterLimit(); + } + } + if (totalSkipped < size) { + // Skipping more bytes than are in the buffer. First skip what we have. + int tempPos = bufferSize - pos; pos = bufferSize; + + // Keep refilling the buffer until we get to the point we wanted to skip to. + // This has the side effect of ensuring the limits are updated correctly. refillBuffer(1); - } + while (size - tempPos > bufferSize) { + tempPos += bufferSize; + pos = bufferSize; + refillBuffer(1); + } - pos = size - tempPos; + pos = size - tempPos; + } } } diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java b/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java new file mode 100755 index 0000000000..7658f629d3 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java @@ -0,0 +1,1333 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.WireFormat.FIXED32_SIZE; +import static com.google.protobuf.WireFormat.FIXED64_SIZE; +import static com.google.protobuf.WireFormat.WIRETYPE_END_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED32; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED64; +import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; +import static com.google.protobuf.WireFormat.WIRETYPE_START_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_VARINT; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** An adapter between the {@link Reader} interface and {@link CodedInputStream}. */ +@ExperimentalApi +final class CodedInputStreamReader implements Reader { + private static final int FIXED32_MULTIPLE_MASK = FIXED32_SIZE - 1; + private static final int FIXED64_MULTIPLE_MASK = FIXED64_SIZE - 1; + private static final int NEXT_TAG_UNSET = 0; + + private final CodedInputStream input; + private int tag; + private int endGroupTag; + private int nextTag = NEXT_TAG_UNSET; + + public static CodedInputStreamReader forCodedInput(CodedInputStream input) { + if (input.wrapper != null) { + return input.wrapper; + } + return new CodedInputStreamReader(input); + } + + private CodedInputStreamReader(CodedInputStream input) { + this.input = Internal.checkNotNull(input, "input"); + this.input.wrapper = this; + } + + @Override + public boolean shouldDiscardUnknownFields() { + return input.shouldDiscardUnknownFields(); + } + + @Override + public int getFieldNumber() throws IOException { + if (nextTag != NEXT_TAG_UNSET) { + tag = nextTag; + nextTag = NEXT_TAG_UNSET; + } else { + tag = input.readTag(); + } + if (tag == 0 || tag == endGroupTag) { + return Reader.READ_DONE; + } + return WireFormat.getTagFieldNumber(tag); + } + + @Override + public int getTag() { + return tag; + } + + @Override + public boolean skipField() throws IOException { + if (input.isAtEnd() || tag == endGroupTag) { + return false; + } + return input.skipField(tag); + } + + private void requireWireType(int requiredWireType) throws IOException { + if (WireFormat.getTagWireType(tag) != requiredWireType) { + throw InvalidProtocolBufferException.invalidWireType(); + } + } + + @Override + public double readDouble() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return input.readDouble(); + } + + @Override + public float readFloat() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return input.readFloat(); + } + + @Override + public long readUInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readUInt64(); + } + + @Override + public long readInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readInt64(); + } + + @Override + public int readInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readInt32(); + } + + @Override + public long readFixed64() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return input.readFixed64(); + } + + @Override + public int readFixed32() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return input.readFixed32(); + } + + @Override + public boolean readBool() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readBool(); + } + + @Override + public String readString() throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return input.readString(); + } + + @Override + public String readStringRequireUtf8() throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return input.readStringRequireUtf8(); + } + + @SuppressWarnings("unchecked") + @Override + public T readMessage(Class clazz, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return readMessage(Protobuf.getInstance().schemaFor(clazz), extensionRegistry); + } + + @SuppressWarnings("unchecked") + @Override + public T readMessageBySchemaWithCheck( + Schema schema, ExtensionRegistryLite extensionRegistry) throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return readMessage(schema, extensionRegistry); + } + + @SuppressWarnings("unchecked") + @Override + public T readGroup(Class clazz, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_START_GROUP); + return readGroup(Protobuf.getInstance().schemaFor(clazz), extensionRegistry); + } + + @SuppressWarnings("unchecked") + @Override + public T readGroupBySchemaWithCheck(Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_START_GROUP); + return readGroup(schema, extensionRegistry); + } + + // Should have the same semantics of CodedInputStream#readMessage() + private T readMessage(Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + int size = input.readUInt32(); + if (input.recursionDepth >= input.recursionLimit) { + throw InvalidProtocolBufferException.recursionLimitExceeded(); + } + + // Push the new limit. + final int prevLimit = input.pushLimit(size); + // Allocate and read the message. + T message = schema.newInstance(); + ++input.recursionDepth; + schema.mergeFrom(message, this, extensionRegistry); + schema.makeImmutable(message); + input.checkLastTagWas(0); + --input.recursionDepth; + // Restore the previous limit. + input.popLimit(prevLimit); + return message; + } + + private T readGroup(Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + int prevEndGroupTag = endGroupTag; + endGroupTag = WireFormat.makeTag(WireFormat.getTagFieldNumber(tag), WIRETYPE_END_GROUP); + + try { + // Allocate and read the message. + T message = schema.newInstance(); + schema.mergeFrom(message, this, extensionRegistry); + schema.makeImmutable(message); + + if (tag != endGroupTag) { + throw InvalidProtocolBufferException.parseFailure(); + } + return message; + } finally { + // Restore the old end group tag. + endGroupTag = prevEndGroupTag; + } + } + + @Override + public ByteString readBytes() throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return input.readBytes(); + } + + @Override + public int readUInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readUInt32(); + } + + @Override + public int readEnum() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readEnum(); + } + + @Override + public int readSFixed32() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return input.readSFixed32(); + } + + @Override + public long readSFixed64() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return input.readSFixed64(); + } + + @Override + public int readSInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readSInt32(); + } + + @Override + public long readSInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readSInt64(); + } + + @Override + public void readDoubleList(List target) throws IOException { + if (target instanceof DoubleArrayList) { + DoubleArrayList plist = (DoubleArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addDouble(input.readDouble()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addDouble(input.readDouble()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readDouble()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(input.readDouble()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFloatList(List target) throws IOException { + if (target instanceof FloatArrayList) { + FloatArrayList plist = (FloatArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addFloat(input.readFloat()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addFloat(input.readFloat()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readFloat()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(input.readFloat()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readUInt64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readUInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(input.readUInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readUInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readUInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readInt64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(input.readInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readInt32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(input.readInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFixed64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readFixed64()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addLong(input.readFixed64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readFixed64()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(input.readFixed64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFixed32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readFixed32()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addInt(input.readFixed32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readFixed32()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(input.readFixed32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readBoolList(List target) throws IOException { + if (target instanceof BooleanArrayList) { + BooleanArrayList plist = (BooleanArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addBoolean(input.readBool()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addBoolean(input.readBool()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readBool()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readBool()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readStringList(List target) throws IOException { + readStringListInternal(target, false); + } + + @Override + public void readStringListRequireUtf8(List target) throws IOException { + readStringListInternal(target, true); + } + + public void readStringListInternal(List target, boolean requireUtf8) throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + + if (target instanceof LazyStringList && !requireUtf8) { + LazyStringList lazyList = (LazyStringList) target; + while (true) { + lazyList.add(readBytes()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } else { + while (true) { + target.add(requireUtf8 ? readStringRequireUtf8() : readString()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } + } + + @Override + public void readMessageList( + List target, Class targetType, ExtensionRegistryLite extensionRegistry) + throws IOException { + final Schema schema = Protobuf.getInstance().schemaFor(targetType); + readMessageList(target, schema, extensionRegistry); + } + + @Override + public void readMessageList( + List target, Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + final int listTag = tag; + while (true) { + target.add(readMessage(schema, extensionRegistry)); + if (input.isAtEnd() || nextTag != NEXT_TAG_UNSET) { + return; + } + int nextTag = input.readTag(); + if (nextTag != listTag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } + + @Override + public void readGroupList( + List target, Class targetType, ExtensionRegistryLite extensionRegistry) + throws IOException { + final Schema schema = Protobuf.getInstance().schemaFor(targetType); + readGroupList(target, schema, extensionRegistry); + } + + @Override + public void readGroupList( + List target, Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_START_GROUP) { + throw InvalidProtocolBufferException.invalidWireType(); + } + final int listTag = tag; + while (true) { + target.add(readGroup(schema, extensionRegistry)); + if (input.isAtEnd() || nextTag != NEXT_TAG_UNSET) { + return; + } + int nextTag = input.readTag(); + if (nextTag != listTag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } + + @Override + public void readBytesList(List target) throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + + while (true) { + target.add(readBytes()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } + + @Override + public void readUInt32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readUInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(input.readUInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readUInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readUInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readEnumList(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readEnum()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(input.readEnum()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readEnum()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readEnum()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSFixed32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readSFixed32()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addInt(input.readSFixed32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readSFixed32()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(input.readSFixed32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSFixed64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readSFixed64()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addLong(input.readSFixed64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readSFixed64()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(input.readSFixed64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSInt32List(List target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readSInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(input.readSInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readSInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readSInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSInt64List(List target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readSInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(input.readSInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readSInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readSInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + private void verifyPackedFixed64Length(int bytes) throws IOException { + if ((bytes & FIXED64_MULTIPLE_MASK) != 0) { + // Require that the number of bytes be a multiple of 8. + throw InvalidProtocolBufferException.parseFailure(); + } + } + + @SuppressWarnings("unchecked") + @Override + public void readMap( + Map target, + MapEntryLite.Metadata metadata, + ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + int size = input.readUInt32(); + final int prevLimit = input.pushLimit(size); + K key = metadata.defaultKey; + V value = metadata.defaultValue; + try { + while (true) { + int number = getFieldNumber(); + if (number == READ_DONE || input.isAtEnd()) { + break; + } + try { + switch (number) { + case 1: + key = (K) readField(metadata.keyType, null, null); + break; + case 2: + value = + (V) + readField( + metadata.valueType, metadata.defaultValue.getClass(), extensionRegistry); + break; + default: + if (!skipField()) { + throw new InvalidProtocolBufferException("Unable to parse map entry."); + } + break; + } + } catch (InvalidProtocolBufferException.InvalidWireTypeException ignore) { + // the type doesn't match, skip the field. + if (!skipField()) { + throw new InvalidProtocolBufferException("Unable to parse map entry."); + } + } + } + target.put(key, value); + } finally { + // Restore the previous limit. + input.popLimit(prevLimit); + } + } + + private Object readField( + WireFormat.FieldType fieldType, Class messageType, ExtensionRegistryLite extensionRegistry) + throws IOException { + switch (fieldType) { + case BOOL: + return readBool(); + case BYTES: + return readBytes(); + case DOUBLE: + return readDouble(); + case ENUM: + return readEnum(); + case FIXED32: + return readFixed32(); + case FIXED64: + return readFixed64(); + case FLOAT: + return readFloat(); + case INT32: + return readInt32(); + case INT64: + return readInt64(); + case MESSAGE: + return readMessage(messageType, extensionRegistry); + case SFIXED32: + return readSFixed32(); + case SFIXED64: + return readSFixed64(); + case SINT32: + return readSInt32(); + case SINT64: + return readSInt64(); + case STRING: + return readStringRequireUtf8(); + case UINT32: + return readUInt32(); + case UINT64: + return readUInt64(); + default: + throw new RuntimeException("unsupported field type."); + } + } + + private void verifyPackedFixed32Length(int bytes) throws IOException { + if ((bytes & FIXED32_MULTIPLE_MASK) != 0) { + // Require that the number of bytes be a multiple of 4. + throw InvalidProtocolBufferException.parseFailure(); + } + } + + private void requirePosition(int expectedPosition) throws IOException { + if (input.getTotalBytesRead() != expectedPosition) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index 7b1ac65120..a9192d33e4 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -32,6 +32,7 @@ package com.google.protobuf; import static com.google.protobuf.WireFormat.FIXED32_SIZE; import static com.google.protobuf.WireFormat.FIXED64_SIZE; +import static com.google.protobuf.WireFormat.MAX_VARINT32_SIZE; import static com.google.protobuf.WireFormat.MAX_VARINT_SIZE; import static java.lang.Math.max; @@ -47,12 +48,11 @@ import java.util.logging.Logger; /** * Encodes and writes protocol message fields. * - *

This class contains two kinds of methods: methods that write specific - * protocol message constructs and field types (e.g. {@link #writeTag} and - * {@link #writeInt32}) and methods that write low-level values (e.g. - * {@link #writeRawVarint32} and {@link #writeRawBytes}). If you are - * writing encoded protocol messages, you should use the former methods, but if - * you are writing some other format of your own design, use the latter. + *

This class contains two kinds of methods: methods that write specific protocol message + * constructs and field types (e.g. {@link #writeTag} and {@link #writeInt32}) and methods that + * write low-level values (e.g. {@link #writeRawVarint32} and {@link #writeRawBytes}). If you are + * writing encoded protocol messages, you should use the former methods, but if you are writing some + * other format of your own design, use the latter. * *

This class is totally unsynchronized. */ @@ -60,23 +60,20 @@ public abstract class CodedOutputStream extends ByteOutput { private static final Logger logger = Logger.getLogger(CodedOutputStream.class.getName()); private static final boolean HAS_UNSAFE_ARRAY_OPERATIONS = UnsafeUtil.hasUnsafeArrayOperations(); - /** - * @deprecated Use {@link #computeFixed32SizeNoTag(int)} instead. - */ - @Deprecated - public static final int LITTLE_ENDIAN_32_SIZE = FIXED32_SIZE; + /** Used to adapt to the experimental {@link Writer} interface. */ + CodedOutputStreamWriter wrapper; - /** - * The buffer size used in {@link #newInstance(OutputStream)}. - */ + /** @deprecated Use {@link #computeFixed32SizeNoTag(int)} instead. */ + @Deprecated public static final int LITTLE_ENDIAN_32_SIZE = FIXED32_SIZE; + + /** The buffer size used in {@link #newInstance(OutputStream)}. */ public static final int DEFAULT_BUFFER_SIZE = 4096; /** - * Returns the buffer size to efficiently write dataLength bytes to this - * CodedOutputStream. Used by AbstractMessageLite. + * Returns the buffer size to efficiently write dataLength bytes to this CodedOutputStream. Used + * by AbstractMessageLite. * - * @return the buffer size to efficiently write dataLength bytes to this - * CodedOutputStream. + * @return the buffer size to efficiently write dataLength bytes to this CodedOutputStream. */ static int computePreferredBufferSize(int dataLength) { if (dataLength > DEFAULT_BUFFER_SIZE) { @@ -88,9 +85,9 @@ public abstract class CodedOutputStream extends ByteOutput { /** * Create a new {@code CodedOutputStream} wrapping the given {@code OutputStream}. * - *

NOTE: The provided {@link OutputStream} MUST NOT retain access or - * modify the provided byte arrays. Doing so may result in corrupted data, which would be - * difficult to debug. + *

NOTE: The provided {@link OutputStream} MUST NOT retain access or modify + * the provided byte arrays. Doing so may result in corrupted data, which would be difficult to + * debug. */ public static CodedOutputStream newInstance(final OutputStream output) { return newInstance(output, DEFAULT_BUFFER_SIZE); @@ -100,30 +97,28 @@ public abstract class CodedOutputStream extends ByteOutput { * Create a new {@code CodedOutputStream} wrapping the given {@code OutputStream} with a given * buffer size. * - *

NOTE: The provided {@link OutputStream} MUST NOT retain access or - * modify the provided byte arrays. Doing so may result in corrupted data, which would be - * difficult to debug. + *

NOTE: The provided {@link OutputStream} MUST NOT retain access or modify + * the provided byte arrays. Doing so may result in corrupted data, which would be difficult to + * debug. */ public static CodedOutputStream newInstance(final OutputStream output, final int bufferSize) { return new OutputStreamEncoder(output, bufferSize); } /** - * Create a new {@code CodedOutputStream} that writes directly to the given - * byte array. If more bytes are written than fit in the array, - * {@link OutOfSpaceException} will be thrown. Writing directly to a flat - * array is faster than writing to an {@code OutputStream}. See also - * {@link ByteString#newCodedBuilder}. + * Create a new {@code CodedOutputStream} that writes directly to the given byte array. If more + * bytes are written than fit in the array, {@link OutOfSpaceException} will be thrown. Writing + * directly to a flat array is faster than writing to an {@code OutputStream}. See also {@link + * ByteString#newCodedBuilder}. */ public static CodedOutputStream newInstance(final byte[] flatArray) { return newInstance(flatArray, 0, flatArray.length); } /** - * Create a new {@code CodedOutputStream} that writes directly to the given - * byte array slice. If more bytes are written than fit in the slice, - * {@link OutOfSpaceException} will be thrown. Writing directly to a flat - * array is faster than writing to an {@code OutputStream}. See also + * Create a new {@code CodedOutputStream} that writes directly to the given byte array slice. If + * more bytes are written than fit in the slice, {@link OutOfSpaceException} will be thrown. + * Writing directly to a flat array is faster than writing to an {@code OutputStream}. See also * {@link ByteString#newCodedBuilder}. */ public static CodedOutputStream newInstance( @@ -162,9 +157,9 @@ public abstract class CodedOutputStream extends ByteOutput { * implies: * *

    - *
  • repeated serialization of a message will return the same bytes - *
  • different processes of the same binary (which may be executing on different machines) will - * serialize equal messages to the same bytes. + *
  • repeated serialization of a message will return the same bytes + *
  • different processes of the same binary (which may be executing on different machines) + * will serialize equal messages to the same bytes. *
* *

Note the deterministic serialization is NOT canonical across languages; it is also unstable @@ -173,14 +168,14 @@ public abstract class CodedOutputStream extends ByteOutput { * their own canonicalization specification and implement the serializer using reflection APIs * rather than relying on this API. * - *

Once set, the serializer will: (Note this is an implementation detail and may subject to + *

Once set, the serializer will: (Note this is an implementation detail and may subject to * change in the future) * *

    - *
  • sort map entries by keys in lexicographical order or numerical order. Note: For string - * keys, the order is based on comparing the Unicode value of each character in the strings. - * The order may be different from the deterministic serialization in other languages where - * maps are sorted on the lexicographical order of the UTF8 encoded keys. + *
  • sort map entries by keys in lexicographical order or numerical order. Note: For string + * keys, the order is based on comparing the Unicode value of each character in the strings. + * The order may be different from the deterministic serialization in other languages where + * maps are sorted on the lexicographical order of the UTF8 encoded keys. *
*/ public void useDeterministicSerialization() { @@ -190,31 +185,32 @@ public abstract class CodedOutputStream extends ByteOutput { boolean isSerializationDeterministic() { return serializationDeterministic; } + private boolean serializationDeterministic; /** * Create a new {@code CodedOutputStream} that writes to the given {@link ByteBuffer}. * * @deprecated the size parameter is no longer used since use of an internal buffer is useless - * (and wasteful) when writing to a {@link ByteBuffer}. Use {@link #newInstance(ByteBuffer)} - * instead. + * (and wasteful) when writing to a {@link ByteBuffer}. Use {@link #newInstance(ByteBuffer)} + * instead. */ @Deprecated - public static CodedOutputStream newInstance(ByteBuffer byteBuffer, - @SuppressWarnings("unused") int unused) { + public static CodedOutputStream newInstance( + ByteBuffer byteBuffer, @SuppressWarnings("unused") int unused) { return newInstance(byteBuffer); } /** * Create a new {@code CodedOutputStream} that writes to the provided {@link ByteOutput}. * - *

NOTE: The {@link ByteOutput} MUST NOT modify the provided buffers. Doing - * so may result in corrupted data, which would be difficult to debug. + *

NOTE: The {@link ByteOutput} MUST NOT modify the provided buffers. Doing so + * may result in corrupted data, which would be difficult to debug. * * @param byteOutput the output target for encoded bytes. * @param bufferSize the size of the internal scratch buffer to be used for string encoding. - * Setting this to {@code 0} will disable buffering, requiring an allocation for each encoded - * string. + * Setting this to {@code 0} will disable buffering, requiring an allocation for each encoded + * string. */ static CodedOutputStream newInstance(ByteOutput byteOutput, int bufferSize) { if (bufferSize < 0) { @@ -225,8 +221,7 @@ public abstract class CodedOutputStream extends ByteOutput { } // Disallow construction outside of this class. - private CodedOutputStream() { - } + private CodedOutputStream() {} // ----------------------------------------------------------------- @@ -294,8 +289,8 @@ public abstract class CodedOutputStream extends ByteOutput { public abstract void writeBool(int fieldNumber, boolean value) throws IOException; /** - * Write an enum field, including tag, to the stream. The provided value is the numeric - * value used to represent the enum value on the wire (not the enum ordinal value). + * Write an enum field, including tag, to the stream. The provided value is the numeric value used + * to represent the enum value on the wire (not the enum ordinal value). */ public final void writeEnum(final int fieldNumber, final int value) throws IOException { writeInt32(fieldNumber, value); @@ -319,21 +314,17 @@ public abstract class CodedOutputStream extends ByteOutput { throws IOException; /** - * Write a {@code bytes} field, including tag, to the stream. - * This method will write all content of the ByteBuffer regardless of the - * current position and limit (i.e., the number of bytes to be written is - * value.capacity(), not value.remaining()). Furthermore, this method doesn't - * alter the state of the passed-in ByteBuffer. Its position, limit, mark, - * etc. will remain unchanged. If you only want to write the remaining bytes - * of a ByteBuffer, you can call - * {@code writeByteBuffer(fieldNumber, byteBuffer.slice())}. + * Write a {@code bytes} field, including tag, to the stream. This method will write all content + * of the ByteBuffer regardless of the current position and limit (i.e., the number of bytes to be + * written is value.capacity(), not value.remaining()). Furthermore, this method doesn't alter the + * state of the passed-in ByteBuffer. Its position, limit, mark, etc. will remain unchanged. If + * you only want to write the remaining bytes of a ByteBuffer, you can call {@code + * writeByteBuffer(fieldNumber, byteBuffer.slice())}. */ // Abstract to avoid overhead of additional virtual method calls. public abstract void writeByteBuffer(int fieldNumber, ByteBuffer value) throws IOException; - /** - * Write a single byte. - */ + /** Write a single byte. */ public final void writeRawByte(final byte value) throws IOException { write(value); } @@ -348,9 +339,7 @@ public abstract class CodedOutputStream extends ByteOutput { write(value, 0, value.length); } - /** - * Write part of an array of bytes. - */ + /** Write part of an array of bytes. */ public final void writeRawBytes(final byte[] value, int offset, int length) throws IOException { write(value, offset, length); } @@ -361,13 +350,11 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Write a ByteBuffer. This method will write all content of the ByteBuffer - * regardless of the current position and limit (i.e., the number of bytes - * to be written is value.capacity(), not value.remaining()). Furthermore, - * this method doesn't alter the state of the passed-in ByteBuffer. Its - * position, limit, mark, etc. will remain unchanged. If you only want to - * write the remaining bytes of a ByteBuffer, you can call - * {@code writeRawBytes(byteBuffer.slice())}. + * Write a ByteBuffer. This method will write all content of the ByteBuffer regardless of the + * current position and limit (i.e., the number of bytes to be written is value.capacity(), not + * value.remaining()). Furthermore, this method doesn't alter the state of the passed-in + * ByteBuffer. Its position, limit, mark, etc. will remain unchanged. If you only want to write + * the remaining bytes of a ByteBuffer, you can call {@code writeRawBytes(byteBuffer.slice())}. */ // Abstract to avoid overhead of additional virtual method calls. public abstract void writeRawBytes(final ByteBuffer value) throws IOException; @@ -377,18 +364,22 @@ public abstract class CodedOutputStream extends ByteOutput { public abstract void writeMessage(final int fieldNumber, final MessageLite value) throws IOException; + /** Write an embedded message field, including tag, to the stream. */ + // Abstract to avoid overhead of additional virtual method calls. + abstract void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException; /** - * Write a MessageSet extension field to the stream. For historical reasons, - * the wire format differs from normal fields. + * Write a MessageSet extension field to the stream. For historical reasons, the wire format + * differs from normal fields. */ // Abstract to avoid overhead of additional virtual method calls. public abstract void writeMessageSetExtension(final int fieldNumber, final MessageLite value) throws IOException; /** - * Write an unparsed MessageSet extension field to the stream. For - * historical reasons, the wire format differs from normal fields. + * Write an unparsed MessageSet extension field to the stream. For historical reasons, the wire + * format differs from normal fields. */ // Abstract to avoid overhead of additional virtual method calls. public abstract void writeRawMessageSetExtension(final int fieldNumber, final ByteString value) @@ -457,8 +448,8 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Write an enum field to the stream. The provided value is the numeric - * value used to represent the enum value on the wire (not the enum ordinal value). + * Write an enum field to the stream. The provided value is the numeric value used to represent + * the enum value on the wire (not the enum ordinal value). */ public final void writeEnumNoTag(final int value) throws IOException { writeInt32NoTag(value); @@ -482,8 +473,11 @@ public abstract class CodedOutputStream extends ByteOutput { // Abstract to avoid overhead of additional virtual method calls. public abstract void writeMessageNoTag(final MessageLite value) throws IOException; + /** Write an embedded message field to the stream. */ + // Abstract to avoid overhead of additional virtual method calls. + abstract void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException; - //================================================================= + // ================================================================= @ExperimentalApi @Override @@ -508,171 +502,177 @@ public abstract class CodedOutputStream extends ByteOutput { // ================================================================= /** - * Compute the number of bytes that would be needed to encode an - * {@code int32} field, including tag. + * Compute the number of bytes that would be needed to encode an {@code int32} field, including + * tag. */ public static int computeInt32Size(final int fieldNumber, final int value) { return computeTagSize(fieldNumber) + computeInt32SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code uint32} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code uint32} field, including + * tag. */ public static int computeUInt32Size(final int fieldNumber, final int value) { return computeTagSize(fieldNumber) + computeUInt32SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode an - * {@code sint32} field, including tag. + * Compute the number of bytes that would be needed to encode an {@code sint32} field, including + * tag. */ public static int computeSInt32Size(final int fieldNumber, final int value) { return computeTagSize(fieldNumber) + computeSInt32SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code fixed32} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code fixed32} field, including + * tag. */ public static int computeFixed32Size(final int fieldNumber, final int value) { return computeTagSize(fieldNumber) + computeFixed32SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode an - * {@code sfixed32} field, including tag. + * Compute the number of bytes that would be needed to encode an {@code sfixed32} field, including + * tag. */ public static int computeSFixed32Size(final int fieldNumber, final int value) { return computeTagSize(fieldNumber) + computeSFixed32SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode an - * {@code int64} field, including tag. + * Compute the number of bytes that would be needed to encode an {@code int64} field, including + * tag. */ public static int computeInt64Size(final int fieldNumber, final long value) { return computeTagSize(fieldNumber) + computeInt64SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code uint64} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code uint64} field, including + * tag. */ public static int computeUInt64Size(final int fieldNumber, final long value) { return computeTagSize(fieldNumber) + computeUInt64SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode an - * {@code sint64} field, including tag. + * Compute the number of bytes that would be needed to encode an {@code sint64} field, including + * tag. */ public static int computeSInt64Size(final int fieldNumber, final long value) { return computeTagSize(fieldNumber) + computeSInt64SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code fixed64} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code fixed64} field, including + * tag. */ public static int computeFixed64Size(final int fieldNumber, final long value) { return computeTagSize(fieldNumber) + computeFixed64SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode an - * {@code sfixed64} field, including tag. + * Compute the number of bytes that would be needed to encode an {@code sfixed64} field, including + * tag. */ public static int computeSFixed64Size(final int fieldNumber, final long value) { return computeTagSize(fieldNumber) + computeSFixed64SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code float} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code float} field, including + * tag. */ public static int computeFloatSize(final int fieldNumber, final float value) { return computeTagSize(fieldNumber) + computeFloatSizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code double} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code double} field, including + * tag. */ public static int computeDoubleSize(final int fieldNumber, final double value) { return computeTagSize(fieldNumber) + computeDoubleSizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code bool} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code bool} field, including tag. */ public static int computeBoolSize(final int fieldNumber, final boolean value) { return computeTagSize(fieldNumber) + computeBoolSizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode an - * enum field, including tag. The provided value is the numeric - * value used to represent the enum value on the wire (not the enum ordinal value). + * Compute the number of bytes that would be needed to encode an enum field, including tag. The + * provided value is the numeric value used to represent the enum value on the wire (not the enum + * ordinal value). */ public static int computeEnumSize(final int fieldNumber, final int value) { return computeTagSize(fieldNumber) + computeEnumSizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code string} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code string} field, including + * tag. */ public static int computeStringSize(final int fieldNumber, final String value) { return computeTagSize(fieldNumber) + computeStringSizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code bytes} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code bytes} field, including + * tag. */ public static int computeBytesSize(final int fieldNumber, final ByteString value) { return computeTagSize(fieldNumber) + computeBytesSizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code bytes} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code bytes} field, including + * tag. */ public static int computeByteArraySize(final int fieldNumber, final byte[] value) { return computeTagSize(fieldNumber) + computeByteArraySizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code bytes} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code bytes} field, including + * tag. */ public static int computeByteBufferSize(final int fieldNumber, final ByteBuffer value) { return computeTagSize(fieldNumber) + computeByteBufferSizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode an - * embedded message in lazy field, including tag. + * Compute the number of bytes that would be needed to encode an embedded message in lazy field, + * including tag. */ public static int computeLazyFieldSize(final int fieldNumber, final LazyFieldLite value) { return computeTagSize(fieldNumber) + computeLazyFieldSizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode an - * embedded message field, including tag. + * Compute the number of bytes that would be needed to encode an embedded message field, including + * tag. */ public static int computeMessageSize(final int fieldNumber, final MessageLite value) { return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value); } + /** + * Compute the number of bytes that would be needed to encode an embedded message field, including + * tag. + */ + static int computeMessageSize( + final int fieldNumber, final MessageLite value, final Schema schema) { + return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value, schema); + } /** - * Compute the number of bytes that would be needed to encode a - * MessageSet extension to the stream. For historical reasons, - * the wire format differs from normal fields. + * Compute the number of bytes that would be needed to encode a MessageSet extension to the + * stream. For historical reasons, the wire format differs from normal fields. */ public static int computeMessageSetExtensionSize(final int fieldNumber, final MessageLite value) { return computeTagSize(WireFormat.MESSAGE_SET_ITEM) * 2 @@ -681,9 +681,8 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Compute the number of bytes that would be needed to encode an - * unparsed MessageSet extension field to the stream. For - * historical reasons, the wire format differs from normal fields. + * Compute the number of bytes that would be needed to encode an unparsed MessageSet extension + * field to the stream. For historical reasons, the wire format differs from normal fields. */ public static int computeRawMessageSetExtensionSize( final int fieldNumber, final ByteString value) { @@ -693,9 +692,9 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Compute the number of bytes that would be needed to encode an - * lazily parsed MessageSet extension field to the stream. For - * historical reasons, the wire format differs from normal fields. + * Compute the number of bytes that would be needed to encode an lazily parsed MessageSet + * extension field to the stream. For historical reasons, the wire format differs from normal + * fields. */ public static int computeLazyFieldMessageSetExtensionSize( final int fieldNumber, final LazyFieldLite value) { @@ -712,8 +711,8 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Compute the number of bytes that would be needed to encode an - * {@code int32} field, including tag. + * Compute the number of bytes that would be needed to encode an {@code int32} field, including + * tag. */ public static int computeInt32SizeNoTag(final int value) { if (value >= 0) { @@ -724,12 +723,9 @@ public abstract class CodedOutputStream extends ByteOutput { } } - /** - * Compute the number of bytes that would be needed to encode a - * {@code uint32} field. - */ + /** Compute the number of bytes that would be needed to encode a {@code uint32} field. */ public static int computeUInt32SizeNoTag(final int value) { - if ((value & (~0 << 7)) == 0) { + if ((value & (~0 << 7)) == 0) { return 1; } if ((value & (~0 << 14)) == 0) { @@ -744,41 +740,32 @@ public abstract class CodedOutputStream extends ByteOutput { return 5; } - /** - * Compute the number of bytes that would be needed to encode an - * {@code sint32} field. - */ + /** Compute the number of bytes that would be needed to encode an {@code sint32} field. */ public static int computeSInt32SizeNoTag(final int value) { return computeUInt32SizeNoTag(encodeZigZag32(value)); } - /** - * Compute the number of bytes that would be needed to encode a - * {@code fixed32} field. - */ + /** Compute the number of bytes that would be needed to encode a {@code fixed32} field. */ public static int computeFixed32SizeNoTag(@SuppressWarnings("unused") final int unused) { return FIXED32_SIZE; } - /** - * Compute the number of bytes that would be needed to encode an - * {@code sfixed32} field. - */ + /** Compute the number of bytes that would be needed to encode an {@code sfixed32} field. */ public static int computeSFixed32SizeNoTag(@SuppressWarnings("unused") final int unused) { return FIXED32_SIZE; } /** - * Compute the number of bytes that would be needed to encode an - * {@code int64} field, including tag. + * Compute the number of bytes that would be needed to encode an {@code int64} field, including + * tag. */ public static int computeInt64SizeNoTag(final long value) { return computeUInt64SizeNoTag(value); } /** - * Compute the number of bytes that would be needed to encode a - * {@code uint64} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code uint64} field, including + * tag. */ public static int computeUInt64SizeNoTag(long value) { // handle two popular special cases up front ... @@ -791,10 +778,12 @@ public abstract class CodedOutputStream extends ByteOutput { // ... leaving us with 8 remaining, which we can divide and conquer int n = 2; if ((value & (~0L << 35)) != 0L) { - n += 4; value >>>= 28; + n += 4; + value >>>= 28; } if ((value & (~0L << 21)) != 0L) { - n += 2; value >>>= 14; + n += 2; + value >>>= 14; } if ((value & (~0L << 14)) != 0L) { n += 1; @@ -802,67 +791,51 @@ public abstract class CodedOutputStream extends ByteOutput { return n; } - /** - * Compute the number of bytes that would be needed to encode an - * {@code sint64} field. - */ + /** Compute the number of bytes that would be needed to encode an {@code sint64} field. */ public static int computeSInt64SizeNoTag(final long value) { return computeUInt64SizeNoTag(encodeZigZag64(value)); } - /** - * Compute the number of bytes that would be needed to encode a - * {@code fixed64} field. - */ + /** Compute the number of bytes that would be needed to encode a {@code fixed64} field. */ public static int computeFixed64SizeNoTag(@SuppressWarnings("unused") final long unused) { return FIXED64_SIZE; } - /** - * Compute the number of bytes that would be needed to encode an - * {@code sfixed64} field. - */ + /** Compute the number of bytes that would be needed to encode an {@code sfixed64} field. */ public static int computeSFixed64SizeNoTag(@SuppressWarnings("unused") final long unused) { return FIXED64_SIZE; } /** - * Compute the number of bytes that would be needed to encode a - * {@code float} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code float} field, including + * tag. */ public static int computeFloatSizeNoTag(@SuppressWarnings("unused") final float unused) { return FIXED32_SIZE; } /** - * Compute the number of bytes that would be needed to encode a - * {@code double} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code double} field, including + * tag. */ public static int computeDoubleSizeNoTag(@SuppressWarnings("unused") final double unused) { return FIXED64_SIZE; } - /** - * Compute the number of bytes that would be needed to encode a - * {@code bool} field. - */ + /** Compute the number of bytes that would be needed to encode a {@code bool} field. */ public static int computeBoolSizeNoTag(@SuppressWarnings("unused") final boolean unused) { return 1; } /** - * Compute the number of bytes that would be needed to encode an enum field. - * The provided value is the numeric value used to represent the enum value on the wire - * (not the enum ordinal value). + * Compute the number of bytes that would be needed to encode an enum field. The provided value is + * the numeric value used to represent the enum value on the wire (not the enum ordinal value). */ public static int computeEnumSizeNoTag(final int value) { return computeInt32SizeNoTag(value); } - /** - * Compute the number of bytes that would be needed to encode a - * {@code string} field. - */ + /** Compute the number of bytes that would be needed to encode a {@code string} field. */ public static int computeStringSizeNoTag(final String value) { int length; try { @@ -877,59 +850,50 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Compute the number of bytes that would be needed to encode an embedded - * message stored in lazy field. + * Compute the number of bytes that would be needed to encode an embedded message stored in lazy + * field. */ public static int computeLazyFieldSizeNoTag(final LazyFieldLite value) { return computeLengthDelimitedFieldSize(value.getSerializedSize()); } - /** - * Compute the number of bytes that would be needed to encode a - * {@code bytes} field. - */ + /** Compute the number of bytes that would be needed to encode a {@code bytes} field. */ public static int computeBytesSizeNoTag(final ByteString value) { return computeLengthDelimitedFieldSize(value.size()); } - /** - * Compute the number of bytes that would be needed to encode a - * {@code bytes} field. - */ + /** Compute the number of bytes that would be needed to encode a {@code bytes} field. */ public static int computeByteArraySizeNoTag(final byte[] value) { return computeLengthDelimitedFieldSize(value.length); } - /** - * Compute the number of bytes that would be needed to encode a - * {@code bytes} field. - */ + /** Compute the number of bytes that would be needed to encode a {@code bytes} field. */ public static int computeByteBufferSizeNoTag(final ByteBuffer value) { return computeLengthDelimitedFieldSize(value.capacity()); } - /** - * Compute the number of bytes that would be needed to encode an embedded - * message field. - */ + /** Compute the number of bytes that would be needed to encode an embedded message field. */ public static int computeMessageSizeNoTag(final MessageLite value) { return computeLengthDelimitedFieldSize(value.getSerializedSize()); } + /** Compute the number of bytes that would be needed to encode an embedded message field. */ + static int computeMessageSizeNoTag(final MessageLite value, final Schema schema) { + return computeLengthDelimitedFieldSize(((AbstractMessageLite) value).getSerializedSize(schema)); + } static int computeLengthDelimitedFieldSize(int fieldLength) { return computeUInt32SizeNoTag(fieldLength) + fieldLength; } /** - * Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers - * into values that can be efficiently encoded with varint. (Otherwise, - * negative values must be sign-extended to 64 bits to be varint encoded, - * thus always taking 10 bytes on the wire.) + * Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers into values that can be + * efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits + * to be varint encoded, thus always taking 10 bytes on the wire.) * * @param n A signed 32-bit integer. - * @return An unsigned 32-bit integer, stored in a signed int because - * Java has no explicit unsigned support. + * @return An unsigned 32-bit integer, stored in a signed int because Java has no explicit + * unsigned support. */ public static int encodeZigZag32(final int n) { // Note: the right-shift must be arithmetic @@ -937,14 +901,13 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers - * into values that can be efficiently encoded with varint. (Otherwise, - * negative values must be sign-extended to 64 bits to be varint encoded, - * thus always taking 10 bytes on the wire.) + * Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers into values that can be + * efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits + * to be varint encoded, thus always taking 10 bytes on the wire.) * * @param n A signed 64-bit integer. - * @return An unsigned 64-bit integer, stored in a signed int because - * Java has no explicit unsigned support. + * @return An unsigned 64-bit integer, stored in a signed int because Java has no explicit + * unsigned support. */ public static long encodeZigZag64(final long n) { // Note: the right-shift must be arithmetic @@ -954,23 +917,22 @@ public abstract class CodedOutputStream extends ByteOutput { // ================================================================= /** - * Flushes the stream and forces any buffered bytes to be written. This - * does not flush the underlying OutputStream. + * Flushes the stream and forces any buffered bytes to be written. This does not flush the + * underlying OutputStream. */ public abstract void flush() throws IOException; /** - * If writing to a flat array, return the space left in the array. - * Otherwise, throws {@code UnsupportedOperationException}. + * If writing to a flat array, return the space left in the array. Otherwise, throws {@code + * UnsupportedOperationException}. */ public abstract int spaceLeft(); /** - * Verifies that {@link #spaceLeft()} returns zero. It's common to create - * a byte array that is exactly big enough to hold a message, then write to - * it with a {@code CodedOutputStream}. Calling {@code checkNoSpaceLeft()} - * after writing verifies that the message was actually as big as expected, - * which can help catch bugs. + * Verifies that {@link #spaceLeft()} returns zero. It's common to create a byte array that is + * exactly big enough to hold a message, then write to it with a {@code CodedOutputStream}. + * Calling {@code checkNoSpaceLeft()} after writing verifies that the message was actually as big + * as expected, which can help catch bugs. */ public final void checkNoSpaceLeft() { if (spaceLeft() != 0) { @@ -979,9 +941,8 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * If you create a CodedOutputStream around a simple flat array, you must - * not attempt to write more bytes than the array has space. Otherwise, - * this exception will be thrown. + * If you create a CodedOutputStream around a simple flat array, you must not attempt to write + * more bytes than the array has space. Otherwise, this exception will be thrown. */ public static class OutOfSpaceException extends IOException { private static final long serialVersionUID = -6947486886997889499L; @@ -1007,9 +968,8 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Get the total number of bytes successfully written to this stream. The - * returned value is not guaranteed to be accurate if exceptions have been - * found in the middle of writing. + * Get the total number of bytes successfully written to this stream. The returned value is not + * guaranteed to be accurate if exceptions have been found in the middle of writing. */ public abstract int getTotalBytesWritten(); @@ -1021,8 +981,10 @@ public abstract class CodedOutputStream extends ByteOutput { final void inefficientWriteStringNoTag(String value, UnpairedSurrogateException cause) throws IOException { - logger.log(Level.WARNING, - "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", cause); + logger.log( + Level.WARNING, + "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", + cause); // Unfortunately there does not appear to be any way to tell Java to encode // UTF-8 directly into our buffer, so we have to let it create its own byte @@ -1053,6 +1015,18 @@ public abstract class CodedOutputStream extends ByteOutput { writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); } + /** + * Write a {@code group} field, including tag, to the stream. + * + * @deprecated groups are deprecated. + */ + @Deprecated + final void writeGroup(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP); + writeGroupNoTag(value, schema); + writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); + } /** * Write a {@code group} field to the stream. @@ -1064,10 +1038,19 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + /** + * Write a {@code group} field to the stream. + * + * @deprecated groups are deprecated. + */ + @Deprecated + final void writeGroupNoTag(final MessageLite value, Schema schema) throws IOException { + schema.writeTo(value, wrapper); + } /** - * Compute the number of bytes that would be needed to encode a - * {@code group} field, including tag. + * Compute the number of bytes that would be needed to encode a {@code group} field, including + * tag. * * @deprecated groups are deprecated. */ @@ -1076,20 +1059,32 @@ public abstract class CodedOutputStream extends ByteOutput { return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value); } - /** - * Compute the number of bytes that would be needed to encode a - * {@code group} field. + * Compute the number of bytes that would be needed to encode a {@code group} field, including + * tag. + * + * @deprecated groups are deprecated. */ @Deprecated + static int computeGroupSize(final int fieldNumber, final MessageLite value, Schema schema) { + return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value, schema); + } + + /** Compute the number of bytes that would be needed to encode a {@code group} field. */ + @Deprecated public static int computeGroupSizeNoTag(final MessageLite value) { return value.getSerializedSize(); } + /** Compute the number of bytes that would be needed to encode a {@code group} field. */ + @Deprecated + static int computeGroupSizeNoTag(final MessageLite value, Schema schema) { + return ((AbstractMessageLite) value).getSerializedSize(schema); + } /** - * Encode and write a varint. {@code value} is treated as - * unsigned, so it won't be sign-extended if negative. + * Encode and write a varint. {@code value} is treated as unsigned, so it won't be sign-extended + * if negative. * * @deprecated use {@link #writeUInt32NoTag} instead. */ @@ -1109,9 +1104,8 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * Compute the number of bytes that would be needed to encode a varint. - * {@code value} is treated as unsigned, so it won't be sign-extended if - * negative. + * Compute the number of bytes that would be needed to encode a varint. {@code value} is treated + * as unsigned, so it won't be sign-extended if negative. * * @deprecated use {@link #computeUInt32SizeNoTag(int)} instead. */ @@ -1152,9 +1146,7 @@ public abstract class CodedOutputStream extends ByteOutput { // ================================================================= - /** - * A {@link CodedOutputStream} that writes directly to a byte array. - */ + /** A {@link CodedOutputStream} that writes directly to a byte array. */ private static class ArrayEncoder extends CodedOutputStream { private final byte[] buffer; private final int offset; @@ -1166,9 +1158,10 @@ public abstract class CodedOutputStream extends ByteOutput { throw new NullPointerException("buffer"); } if ((offset | length | (buffer.length - (offset + length))) < 0) { - throw new IllegalArgumentException(String.format( - "Array range is invalid. Buffer.length=%d, offset=%d, length=%d", - buffer.length, offset, length)); + throw new IllegalArgumentException( + String.format( + "Array range is invalid. Buffer.length=%d, offset=%d, length=%d", + buffer.length, offset, length)); } this.buffer = buffer; this.offset = offset; @@ -1281,6 +1274,13 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override + final void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public final void writeMessageSetExtension(final int fieldNumber, final MessageLite value) @@ -1306,6 +1306,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + final void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public final void write(byte value) throws IOException { @@ -1329,16 +1334,34 @@ public abstract class CodedOutputStream extends ByteOutput { @Override public final void writeUInt32NoTag(int value) throws IOException { - if (HAS_UNSAFE_ARRAY_OPERATIONS && spaceLeft() >= MAX_VARINT_SIZE) { - while (true) { - if ((value & ~0x7F) == 0) { - UnsafeUtil.putByte(buffer, position++, (byte) value); - return; - } else { - UnsafeUtil.putByte(buffer, position++, (byte) ((value & 0x7F) | 0x80)); - value >>>= 7; - } + if (HAS_UNSAFE_ARRAY_OPERATIONS + && !Android.isOnAndroidDevice() + && spaceLeft() >= MAX_VARINT32_SIZE) { + if ((value & ~0x7F) == 0) { + UnsafeUtil.putByte(buffer, position++, (byte) value); + return; } + UnsafeUtil.putByte(buffer, position++, (byte) (value | 0x80)); + value >>>= 7; + if ((value & ~0x7F) == 0) { + UnsafeUtil.putByte(buffer, position++, (byte) value); + return; + } + UnsafeUtil.putByte(buffer, position++, (byte) (value | 0x80)); + value >>>= 7; + if ((value & ~0x7F) == 0) { + UnsafeUtil.putByte(buffer, position++, (byte) value); + return; + } + UnsafeUtil.putByte(buffer, position++, (byte) (value | 0x80)); + value >>>= 7; + if ((value & ~0x7F) == 0) { + UnsafeUtil.putByte(buffer, position++, (byte) value); + return; + } + UnsafeUtil.putByte(buffer, position++, (byte) (value | 0x80)); + value >>>= 7; + UnsafeUtil.putByte(buffer, position++, (byte) value); } else { try { while (true) { @@ -1501,15 +1524,17 @@ public abstract class CodedOutputStream extends ByteOutput { } /** - * A {@link CodedOutputStream} that writes directly to a heap {@link ByteBuffer}. Writes are - * done directly to the underlying array. The buffer position is only updated after a flush. + * A {@link CodedOutputStream} that writes directly to a heap {@link ByteBuffer}. Writes are done + * directly to the underlying array. The buffer position is only updated after a flush. */ private static final class HeapNioEncoder extends ArrayEncoder { private final ByteBuffer byteBuffer; private int initialPosition; HeapNioEncoder(ByteBuffer byteBuffer) { - super(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), + super( + byteBuffer.array(), + byteBuffer.arrayOffset() + byteBuffer.position(), byteBuffer.remaining()); this.byteBuffer = byteBuffer; this.initialPosition = byteBuffer.position(); @@ -1604,20 +1629,24 @@ public abstract class CodedOutputStream extends ByteOutput { } @Override - public void writeByteBuffer(final int fieldNumber, final ByteBuffer value) - throws IOException { + public void writeByteBuffer(final int fieldNumber, final ByteBuffer value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeUInt32NoTag(value.capacity()); writeRawBytes(value); } @Override - public void writeMessage(final int fieldNumber, final MessageLite value) - throws IOException { + public void writeMessage(final int fieldNumber, final MessageLite value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeMessageNoTag(value); } + @Override + void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value, schema); + } @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) @@ -1643,6 +1672,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public void write(byte value) throws IOException { @@ -1940,6 +1974,11 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override + void writeMessage(int fieldNumber, MessageLite value, Schema schema) throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value, schema); + } @Override public void writeMessageSetExtension(int fieldNumber, MessageLite value) throws IOException { @@ -1963,6 +2002,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + void writeMessageNoTag(MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public void write(byte value) throws IOException { @@ -2186,9 +2230,7 @@ public abstract class CodedOutputStream extends ByteOutput { } } - /** - * Abstract base class for buffered encoders. - */ + /** Abstract base class for buffered encoders. */ private abstract static class AbstractBufferedEncoder extends CodedOutputStream { final byte[] buffer; final int limit; @@ -2346,8 +2388,8 @@ public abstract class CodedOutputStream extends ByteOutput { /** * A {@link CodedOutputStream} that decorates a {@link ByteOutput}. It internal buffer only to - * support string encoding operations. All other writes are just passed through to the - * {@link ByteOutput}. + * support string encoding operations. All other writes are just passed through to the {@link + * ByteOutput}. */ private static final class ByteOutputEncoder extends AbstractBufferedEncoder { private final ByteOutput out; @@ -2433,8 +2475,7 @@ public abstract class CodedOutputStream extends ByteOutput { } @Override - public void writeByteBuffer(final int fieldNumber, final ByteBuffer value) - throws IOException { + public void writeByteBuffer(final int fieldNumber, final ByteBuffer value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeUInt32NoTag(value.capacity()); writeRawBytes(value); @@ -2464,12 +2505,17 @@ public abstract class CodedOutputStream extends ByteOutput { } @Override - public void writeMessage(final int fieldNumber, final MessageLite value) - throws IOException { + public void writeMessage(final int fieldNumber, final MessageLite value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeMessageNoTag(value); } + @Override + void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value, schema); + } @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) @@ -2495,6 +2541,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public void write(byte value) throws IOException { @@ -2517,7 +2568,7 @@ public abstract class CodedOutputStream extends ByteOutput { @Override public void writeUInt32NoTag(int value) throws IOException { - flushIfNotAvailable(MAX_VARINT_SIZE); + flushIfNotAvailable(MAX_VARINT32_SIZE); bufferUInt32NoTag(value); } @@ -2738,8 +2789,7 @@ public abstract class CodedOutputStream extends ByteOutput { } @Override - public void writeByteBuffer(final int fieldNumber, final ByteBuffer value) - throws IOException { + public void writeByteBuffer(final int fieldNumber, final ByteBuffer value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeUInt32NoTag(value.capacity()); writeRawBytes(value); @@ -2769,12 +2819,17 @@ public abstract class CodedOutputStream extends ByteOutput { } @Override - public void writeMessage(final int fieldNumber, final MessageLite value) - throws IOException { + public void writeMessage(final int fieldNumber, final MessageLite value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeMessageNoTag(value); } + @Override + void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value, schema); + } @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) @@ -2800,6 +2855,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public void write(byte value) throws IOException { @@ -2822,7 +2882,7 @@ public abstract class CodedOutputStream extends ByteOutput { @Override public void writeUInt32NoTag(int value) throws IOException { - flushIfNotAvailable(MAX_VARINT_SIZE); + flushIfNotAvailable(MAX_VARINT32_SIZE); bufferUInt32NoTag(value); } @@ -2916,8 +2976,7 @@ public abstract class CodedOutputStream extends ByteOutput { } @Override - public void write(byte[] value, int offset, int length) - throws IOException { + public void write(byte[] value, int offset, int length) throws IOException { if (limit - position >= length) { // We have room in the current buffer. System.arraycopy(value, offset, buffer, position, length); diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java new file mode 100755 index 0000000000..0d1983cb07 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java @@ -0,0 +1,691 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; +import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** An adapter between the {@link Writer} interface and {@link CodedOutputStream}. */ +@ExperimentalApi +final class CodedOutputStreamWriter implements Writer { + private final CodedOutputStream output; + + public static CodedOutputStreamWriter forCodedOutput(CodedOutputStream output) { + if (output.wrapper != null) { + return output.wrapper; + } + return new CodedOutputStreamWriter(output); + } + + private CodedOutputStreamWriter(CodedOutputStream output) { + this.output = checkNotNull(output, "output"); + this.output.wrapper = this; + } + + @Override + public FieldOrder fieldOrder() { + return FieldOrder.ASCENDING; + } + + public int getTotalBytesWritten() { + return output.getTotalBytesWritten(); + } + + @Override + public void writeSFixed32(int fieldNumber, int value) throws IOException { + output.writeSFixed32(fieldNumber, value); + } + + @Override + public void writeInt64(int fieldNumber, long value) throws IOException { + output.writeInt64(fieldNumber, value); + } + + @Override + public void writeSFixed64(int fieldNumber, long value) throws IOException { + output.writeSFixed64(fieldNumber, value); + } + + @Override + public void writeFloat(int fieldNumber, float value) throws IOException { + output.writeFloat(fieldNumber, value); + } + + @Override + public void writeDouble(int fieldNumber, double value) throws IOException { + output.writeDouble(fieldNumber, value); + } + + @Override + public void writeEnum(int fieldNumber, int value) throws IOException { + output.writeEnum(fieldNumber, value); + } + + @Override + public void writeUInt64(int fieldNumber, long value) throws IOException { + output.writeUInt64(fieldNumber, value); + } + + @Override + public void writeInt32(int fieldNumber, int value) throws IOException { + output.writeInt32(fieldNumber, value); + } + + @Override + public void writeFixed64(int fieldNumber, long value) throws IOException { + output.writeFixed64(fieldNumber, value); + } + + @Override + public void writeFixed32(int fieldNumber, int value) throws IOException { + output.writeFixed32(fieldNumber, value); + } + + @Override + public void writeBool(int fieldNumber, boolean value) throws IOException { + output.writeBool(fieldNumber, value); + } + + @Override + public void writeString(int fieldNumber, String value) throws IOException { + output.writeString(fieldNumber, value); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) throws IOException { + output.writeBytes(fieldNumber, value); + } + + @Override + public void writeUInt32(int fieldNumber, int value) throws IOException { + output.writeUInt32(fieldNumber, value); + } + + @Override + public void writeSInt32(int fieldNumber, int value) throws IOException { + output.writeSInt32(fieldNumber, value); + } + + @Override + public void writeSInt64(int fieldNumber, long value) throws IOException { + output.writeSInt64(fieldNumber, value); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + output.writeMessage(fieldNumber, (MessageLite) value); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + output.writeMessage(fieldNumber, (MessageLite) value, schema); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + output.writeGroup(fieldNumber, (MessageLite) value); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + output.writeGroup(fieldNumber, (MessageLite) value, schema); + } + + @Override + public void writeStartGroup(int fieldNumber) throws IOException { + output.writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) throws IOException { + output.writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); + } + + @Override + public final void writeMessageSetItem(int fieldNumber, Object value) throws IOException { + if (value instanceof ByteString) { + output.writeRawMessageSetExtension(fieldNumber, (ByteString) value); + } else { + output.writeMessageSetExtension(fieldNumber, (MessageLite) value); + } + } + + @Override + public void writeInt32List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeInt32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeInt32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeInt32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeFixed32List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeFixed32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeFixed32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeFixed32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeInt64List(int fieldNumber, List value, boolean packed) throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeInt64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeInt64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeInt64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeUInt64List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeUInt64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeUInt64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeUInt64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeFixed64List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeFixed64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeFixed64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeFixed64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeFloatList(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeFloatSizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeFloatNoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeFloat(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeDoubleList(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeDoubleSizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeDoubleNoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeDouble(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeEnumList(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeEnumSizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeEnumNoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeEnum(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeBoolList(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeBoolSizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeBoolNoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeBool(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeStringList(int fieldNumber, List value) throws IOException { + if (value instanceof LazyStringList) { + final LazyStringList lazyList = (LazyStringList) value; + for (int i = 0; i < value.size(); ++i) { + writeLazyString(fieldNumber, lazyList.getRaw(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeString(fieldNumber, value.get(i)); + } + } + } + + private void writeLazyString(int fieldNumber, Object value) throws IOException { + if (value instanceof String) { + output.writeString(fieldNumber, (String) value); + } else { + output.writeBytes(fieldNumber, (ByteString) value); + } + } + + @Override + public void writeBytesList(int fieldNumber, List value) throws IOException { + for (int i = 0; i < value.size(); ++i) { + output.writeBytes(fieldNumber, value.get(i)); + } + } + + @Override + public void writeUInt32List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeUInt32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeUInt32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeUInt32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeSFixed32List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeSFixed32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeSFixed32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeSFixed32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeSFixed64List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeSFixed64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeSFixed64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeSFixed64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeSInt32List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeSInt32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeSInt32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeSInt32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeSInt64List(int fieldNumber, List value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeSInt64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeSInt64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeSInt64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeMessageList(int fieldNumber, List value) throws IOException { + for (int i = 0; i < value.size(); ++i) { + writeMessage(fieldNumber, value.get(i)); + } + } + + @Override + public void writeMessageList(int fieldNumber, List value, Schema schema) throws IOException { + for (int i = 0; i < value.size(); ++i) { + writeMessage(fieldNumber, value.get(i), schema); + } + } + + @Override + public void writeGroupList(int fieldNumber, List value) throws IOException { + for (int i = 0; i < value.size(); ++i) { + writeGroup(fieldNumber, value.get(i)); + } + } + + @Override + public void writeGroupList(int fieldNumber, List value, Schema schema) throws IOException { + for (int i = 0; i < value.size(); ++i) { + writeGroup(fieldNumber, value.get(i), schema); + } + } + + @Override + public void writeMap(int fieldNumber, MapEntryLite.Metadata metadata, Map map) + throws IOException { + if (output.isSerializationDeterministic()) { + writeDeterministicMap(fieldNumber, metadata, map); + return; + } + for (Map.Entry entry : map.entrySet()) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag( + MapEntryLite.computeSerializedSize(metadata, entry.getKey(), entry.getValue())); + MapEntryLite.writeTo(output, metadata, entry.getKey(), entry.getValue()); + } + } + + @SuppressWarnings("unchecked") + private void writeDeterministicMap( + int fieldNumber, MapEntryLite.Metadata metadata, Map map) throws IOException { + switch (metadata.keyType) { + case BOOL: + V value; + if ((value = map.get(Boolean.FALSE)) != null) { + writeDeterministicBooleanMapEntry( + fieldNumber, /* key= */ false, value, (MapEntryLite.Metadata) metadata); + } + if ((value = map.get(Boolean.TRUE)) != null) { + writeDeterministicBooleanMapEntry( + fieldNumber, /* key= */ true, value, (MapEntryLite.Metadata) metadata); + } + break; + case FIXED32: + case INT32: + case SFIXED32: + case SINT32: + case UINT32: + writeDeterministicIntegerMap( + fieldNumber, (MapEntryLite.Metadata) metadata, (Map) map); + break; + case FIXED64: + case INT64: + case SFIXED64: + case SINT64: + case UINT64: + writeDeterministicLongMap( + fieldNumber, (MapEntryLite.Metadata) metadata, (Map) map); + break; + case STRING: + writeDeterministicStringMap( + fieldNumber, (MapEntryLite.Metadata) metadata, (Map) map); + break; + default: + throw new IllegalArgumentException("does not support key type: " + metadata.keyType); + } + } + + private void writeDeterministicBooleanMapEntry( + int fieldNumber, boolean key, V value, MapEntryLite.Metadata metadata) + throws IOException { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag(MapEntryLite.computeSerializedSize(metadata, key, value)); + MapEntryLite.writeTo(output, metadata, key, value); + } + + private void writeDeterministicIntegerMap( + int fieldNumber, MapEntryLite.Metadata metadata, Map map) + throws IOException { + int[] keys = new int[map.size()]; + int index = 0; + for (int k : map.keySet()) { + keys[index++] = k; + } + Arrays.sort(keys); + for (int key : keys) { + V value = map.get(key); + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag(MapEntryLite.computeSerializedSize(metadata, key, value)); + MapEntryLite.writeTo(output, metadata, key, value); + } + } + + private void writeDeterministicLongMap( + int fieldNumber, MapEntryLite.Metadata metadata, Map map) + throws IOException { + long[] keys = new long[map.size()]; + int index = 0; + for (long k : map.keySet()) { + keys[index++] = k; + } + Arrays.sort(keys); + for (long key : keys) { + V value = map.get(key); + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag(MapEntryLite.computeSerializedSize(metadata, key, value)); + MapEntryLite.writeTo(output, metadata, key, value); + } + } + + private void writeDeterministicStringMap( + int fieldNumber, MapEntryLite.Metadata metadata, Map map) + throws IOException { + String[] keys = new String[map.size()]; + int index = 0; + for (String k : map.keySet()) { + keys[index++] = k; + } + Arrays.sort(keys); + for (String key : keys) { + V value = map.get(key); + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag(MapEntryLite.computeSerializedSize(metadata, key, value)); + MapEntryLite.writeTo(output, metadata, key, value); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/DescriptorMessageInfoFactory.java b/java/core/src/main/java/com/google/protobuf/DescriptorMessageInfoFactory.java new file mode 100755 index 0000000000..7975136596 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/DescriptorMessageInfoFactory.java @@ -0,0 +1,690 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forFieldWithEnumVerifier; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forPackedField; +import static com.google.protobuf.FieldInfo.forPackedFieldWithEnumVerifier; +import static com.google.protobuf.FieldInfo.forProto2OptionalField; +import static com.google.protobuf.FieldInfo.forProto2RequiredField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.Descriptors.Descriptor; +import com.google.protobuf.Descriptors.FieldDescriptor; +import com.google.protobuf.Descriptors.FieldDescriptor.Type; +import com.google.protobuf.Descriptors.OneofDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; +import java.util.concurrent.ConcurrentHashMap; + +/** A factory for message info based on protobuf descriptors for a {@link GeneratedMessageV3}. */ +@ExperimentalApi +final class DescriptorMessageInfoFactory implements MessageInfoFactory { + private static final String GET_DEFAULT_INSTANCE_METHOD_NAME = "getDefaultInstance"; + private static final DescriptorMessageInfoFactory instance = new DescriptorMessageInfoFactory(); + private static final Set specialFieldNames = + new HashSet<>(Arrays.asList("cached_size", "serialized_size", "class")); + + // Disallow construction - it's a singleton. + private DescriptorMessageInfoFactory() {} + + public static DescriptorMessageInfoFactory getInstance() { + return instance; + } + + @Override + public boolean isSupported(Class messageType) { + return GeneratedMessageV3.class.isAssignableFrom(messageType); + } + + @Override + public MessageInfo messageInfoFor(Class messageType) { + if (!GeneratedMessageV3.class.isAssignableFrom(messageType)) { + throw new IllegalArgumentException("Unsupported message type: " + messageType.getName()); + } + + return convert(messageType, descriptorForType(messageType)); + } + + private static Message getDefaultInstance(Class messageType) { + try { + Method method = messageType.getDeclaredMethod(GET_DEFAULT_INSTANCE_METHOD_NAME); + return (Message) method.invoke(null); + } catch (Exception e) { + throw new IllegalArgumentException( + "Unable to get default instance for message class " + messageType.getName(), e); + } + } + + private static Descriptor descriptorForType(Class messageType) { + return getDefaultInstance(messageType).getDescriptorForType(); + } + + private static MessageInfo convert(Class messageType, Descriptor messageDescriptor) { + switch (messageDescriptor.getFile().getSyntax()) { + case PROTO2: + return convertProto2(messageType, messageDescriptor); + case PROTO3: + return convertProto3(messageType, messageDescriptor); + default: + throw new IllegalArgumentException( + "Unsupported syntax: " + messageDescriptor.getFile().getSyntax()); + } + } + + /** + * A helper class to determine whether a message type needs to implement {@code isInitialized()}. + * + *

If a message type doesn't have any required fields or extensions (directly and + * transitively), it doesn't need to implement isInitialized() and can always return true there. + * It's a bit tricky to determine whether a type has transitive required fields because protobuf + * allows cycle references within the same .proto file (e.g., message Foo has a Bar field, and + * message Bar has a Foo field). For that we use Tarjan's strongly connected components algorithm + * to classify messages into strongly connected groups. Messages in the same group are + * transitively including each other, so they should either all have transitive required fields + * (or extensions), or none have. + * + *

This class is thread-safe. + */ + static class IsInitializedCheckAnalyzer { + + private final Map resultCache = + new ConcurrentHashMap(); + + // The following data members are part of Tarjan's SCC algorithm. See: + // https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm + private int index = 0; + private final Stack stack = new Stack(); + private final Map nodeCache = new HashMap(); + + public boolean needsIsInitializedCheck(Descriptor descriptor) { + Boolean cachedValue = resultCache.get(descriptor); + if (cachedValue != null) { + return cachedValue; + } + synchronized (this) { + // Double-check the cache because some other thread may have updated it while we + // were acquiring the lock. + cachedValue = resultCache.get(descriptor); + if (cachedValue != null) { + return cachedValue; + } + return dfs(descriptor).component.needsIsInitializedCheck; + } + } + + private static class Node { + final Descriptor descriptor; + final int index; + int lowLink; + StronglyConnectedComponent component; // null if the node is still on stack. + + Node(Descriptor descriptor, int index) { + this.descriptor = descriptor; + this.index = index; + this.lowLink = index; + this.component = null; + } + } + + private static class StronglyConnectedComponent { + final List messages = new ArrayList(); + boolean needsIsInitializedCheck = false; + } + + private Node dfs(Descriptor descriptor) { + Node result = new Node(descriptor, index++); + stack.push(result); + nodeCache.put(descriptor, result); + + // Recurse the fields / nodes in graph + for (FieldDescriptor field : descriptor.getFields()) { + if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + Node child = nodeCache.get(field.getMessageType()); + if (child == null) { + // Unexplored node + child = dfs(field.getMessageType()); + result.lowLink = Math.min(result.lowLink, child.lowLink); + } else { + if (child.component == null) { + // Still in the stack so we found a back edge. + result.lowLink = Math.min(result.lowLink, child.lowLink); + } + } + } + } + + if (result.index == result.lowLink) { + // This is the root of a strongly connected component. + StronglyConnectedComponent component = new StronglyConnectedComponent(); + while (true) { + Node node = stack.pop(); + node.component = component; + component.messages.add(node.descriptor); + if (node == result) { + break; + } + } + + analyze(component); + } + + return result; + } + + // Determine whether messages in this SCC needs isInitialized check. + private void analyze(StronglyConnectedComponent component) { + boolean needsIsInitializedCheck = false; + loop: + for (Descriptor descriptor : component.messages) { + if (descriptor.isExtendable()) { + needsIsInitializedCheck = true; + break; + } + + for (FieldDescriptor field : descriptor.getFields()) { + if (field.isRequired()) { + needsIsInitializedCheck = true; + break loop; + } + + if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + // Since we are analyzing the graph bottom-up, all referenced fields should either be + // in this same component or in a different already-analyzed component. + Node node = nodeCache.get(field.getMessageType()); + if (node.component != component) { + if (node.component.needsIsInitializedCheck) { + needsIsInitializedCheck = true; + break loop; + } + } + } + } + } + + component.needsIsInitializedCheck = needsIsInitializedCheck; + + for (Descriptor descriptor : component.messages) { + resultCache.put(descriptor, component.needsIsInitializedCheck); + } + } + } + + private static IsInitializedCheckAnalyzer isInitializedCheckAnalyzer = + new IsInitializedCheckAnalyzer(); + + private static boolean needsIsInitializedCheck(Descriptor descriptor) { + return isInitializedCheckAnalyzer.needsIsInitializedCheck(descriptor); + } + + private static StructuralMessageInfo convertProto2( + Class messageType, Descriptor messageDescriptor) { + List fieldDescriptors = messageDescriptor.getFields(); + StructuralMessageInfo.Builder builder = + StructuralMessageInfo.newBuilder(fieldDescriptors.size()); + builder.withDefaultInstance(getDefaultInstance(messageType)); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withMessageSetWireFormat(messageDescriptor.getOptions().getMessageSetWireFormat()); + + OneofState oneofState = new OneofState(); + int bitFieldIndex = 0; + int presenceMask = 1; + Field bitField = null; + + // Fields in the descriptor are ordered by the index position in which they appear in the + // proto file. This is the same order used to determine the presence mask used in the + // bitFields. So to determine the appropriate presence mask to be used for a field, we simply + // need to shift the presence mask whenever a presence-checked field is encountered. + for (int i = 0; i < fieldDescriptors.size(); ++i) { + final FieldDescriptor fd = fieldDescriptors.get(i); + boolean enforceUtf8 = fd.getFile().getOptions().getJavaStringCheckUtf8(); + Internal.EnumVerifier enumVerifier = null; + if (fd.getJavaType() == Descriptors.FieldDescriptor.JavaType.ENUM) { + enumVerifier = + new Internal.EnumVerifier() { + @Override + public boolean isInRange(int number) { + return fd.getEnumType().findValueByNumber(number) != null; + } + }; + } + if (fd.getContainingOneof() != null) { + // Build a oneof member field. + builder.withField(buildOneofMember(messageType, fd, oneofState, enforceUtf8, enumVerifier)); + } else { + Field field = field(messageType, fd); + int number = fd.getNumber(); + FieldType type = getFieldType(fd); + + if (fd.isMapField()) { + // Map field points to an auto-generated message entry type with the definition: + // message MapEntry { + // K key = 1; + // V value = 2; + // } + final FieldDescriptor valueField = fd.getMessageType().findFieldByNumber(2); + if (valueField.getJavaType() == Descriptors.FieldDescriptor.JavaType.ENUM) { + enumVerifier = + new Internal.EnumVerifier() { + @Override + public boolean isInRange(int number) { + return valueField.getEnumType().findValueByNumber(number) != null; + } + }; + } + builder.withField( + forMapField( + field, + number, + SchemaUtil.getMapDefaultEntry(messageType, fd.getName()), + enumVerifier)); + continue; + } + + if (fd.isRepeated()) { + // Repeated fields are not presence-checked. + if (enumVerifier != null) { + if (fd.isPacked()) { + builder.withField( + forPackedFieldWithEnumVerifier( + field, number, type, enumVerifier, cachedSizeField(messageType, fd))); + } else { + builder.withField(forFieldWithEnumVerifier(field, number, type, enumVerifier)); + } + } else if (fd.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + builder.withField( + forRepeatedMessageField( + field, number, type, getTypeForRepeatedMessageField(messageType, fd))); + } else { + if (fd.isPacked()) { + builder.withField( + forPackedField(field, number, type, cachedSizeField(messageType, fd))); + } else { + builder.withField(forField(field, number, type, enforceUtf8)); + } + } + continue; + } + + if (bitField == null) { + // Lazy-create the next bitfield since we know it must exist. + bitField = bitField(messageType, bitFieldIndex); + } + + // It's a presence-checked field. + if (fd.isRequired()) { + builder.withField( + forProto2RequiredField( + field, number, type, bitField, presenceMask, enforceUtf8, enumVerifier)); + } else { + builder.withField( + forProto2OptionalField( + field, number, type, bitField, presenceMask, enforceUtf8, enumVerifier)); + } + } + + // Update the presence mask for the next iteration. If the shift clears out the mask, we will + // go to the next bitField. + presenceMask <<= 1; + if (presenceMask == 0) { + bitField = null; + presenceMask = 1; + bitFieldIndex++; + } + } + + List fieldsToCheckIsInitialized = new ArrayList(); + for (int i = 0; i < fieldDescriptors.size(); ++i) { + FieldDescriptor fd = fieldDescriptors.get(i); + if (fd.isRequired() + || (fd.getJavaType() == FieldDescriptor.JavaType.MESSAGE + && needsIsInitializedCheck(fd.getMessageType()))) { + fieldsToCheckIsInitialized.add(fd.getNumber()); + } + } + int[] numbers = new int[fieldsToCheckIsInitialized.size()]; + for (int i = 0; i < fieldsToCheckIsInitialized.size(); i++) { + numbers[i] = fieldsToCheckIsInitialized.get(i); + } + builder.withCheckInitialized(numbers); + + return builder.build(); + } + + private static StructuralMessageInfo convertProto3( + Class messageType, Descriptor messageDescriptor) { + List fieldDescriptors = messageDescriptor.getFields(); + StructuralMessageInfo.Builder builder = + StructuralMessageInfo.newBuilder(fieldDescriptors.size()); + builder.withDefaultInstance(getDefaultInstance(messageType)); + builder.withSyntax(ProtoSyntax.PROTO3); + + OneofState oneofState = new OneofState(); + boolean enforceUtf8 = true; + for (int i = 0; i < fieldDescriptors.size(); ++i) { + FieldDescriptor fd = fieldDescriptors.get(i); + if (fd.getContainingOneof() != null) { + // Build a oneof member field. + builder.withField(buildOneofMember(messageType, fd, oneofState, enforceUtf8, null)); + continue; + } + if (fd.isMapField()) { + builder.withField( + forMapField( + field(messageType, fd), + fd.getNumber(), + SchemaUtil.getMapDefaultEntry(messageType, fd.getName()), + null)); + continue; + } + if (fd.isRepeated() && fd.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + builder.withField( + forRepeatedMessageField( + field(messageType, fd), + fd.getNumber(), + getFieldType(fd), + getTypeForRepeatedMessageField(messageType, fd))); + continue; + } + if (fd.isPacked()) { + builder.withField( + forPackedField( + field(messageType, fd), + fd.getNumber(), + getFieldType(fd), + cachedSizeField(messageType, fd))); + } else { + builder.withField( + forField(field(messageType, fd), fd.getNumber(), getFieldType(fd), enforceUtf8)); + } + } + + return builder.build(); + } + + /** Builds info for a oneof member field. */ + private static FieldInfo buildOneofMember( + Class messageType, + FieldDescriptor fd, + OneofState oneofState, + boolean enforceUtf8, + Internal.EnumVerifier enumVerifier) { + OneofInfo oneof = oneofState.getOneof(messageType, fd.getContainingOneof()); + FieldType type = getFieldType(fd); + Class oneofStoredType = getOneofStoredType(messageType, fd, type); + return forOneofMemberField( + fd.getNumber(), type, oneof, oneofStoredType, enforceUtf8, enumVerifier); + } + + private static Class getOneofStoredType( + Class messageType, FieldDescriptor fd, FieldType type) { + switch (type.getJavaType()) { + case BOOLEAN: + return Boolean.class; + case BYTE_STRING: + return ByteString.class; + case DOUBLE: + return Double.class; + case FLOAT: + return Float.class; + case ENUM: + case INT: + return Integer.class; + case LONG: + return Long.class; + case STRING: + return String.class; + case MESSAGE: + return getOneofStoredTypeForMessage(messageType, fd); + default: + throw new IllegalArgumentException("Invalid type for oneof: " + type); + } + } + + private static FieldType getFieldType(FieldDescriptor fd) { + switch (fd.getType()) { + case BOOL: + if (!fd.isRepeated()) { + return FieldType.BOOL; + } + return fd.isPacked() ? FieldType.BOOL_LIST_PACKED : FieldType.BOOL_LIST; + case BYTES: + return fd.isRepeated() ? FieldType.BYTES_LIST : FieldType.BYTES; + case DOUBLE: + if (!fd.isRepeated()) { + return FieldType.DOUBLE; + } + return fd.isPacked() ? FieldType.DOUBLE_LIST_PACKED : FieldType.DOUBLE_LIST; + case ENUM: + if (!fd.isRepeated()) { + return FieldType.ENUM; + } + return fd.isPacked() ? FieldType.ENUM_LIST_PACKED : FieldType.ENUM_LIST; + case FIXED32: + if (!fd.isRepeated()) { + return FieldType.FIXED32; + } + return fd.isPacked() ? FieldType.FIXED32_LIST_PACKED : FieldType.FIXED32_LIST; + case FIXED64: + if (!fd.isRepeated()) { + return FieldType.FIXED64; + } + return fd.isPacked() ? FieldType.FIXED64_LIST_PACKED : FieldType.FIXED64_LIST; + case FLOAT: + if (!fd.isRepeated()) { + return FieldType.FLOAT; + } + return fd.isPacked() ? FieldType.FLOAT_LIST_PACKED : FieldType.FLOAT_LIST; + case GROUP: + return fd.isRepeated() ? FieldType.GROUP_LIST : FieldType.GROUP; + case INT32: + if (!fd.isRepeated()) { + return FieldType.INT32; + } + return fd.isPacked() ? FieldType.INT32_LIST_PACKED : FieldType.INT32_LIST; + case INT64: + if (!fd.isRepeated()) { + return FieldType.INT64; + } + return fd.isPacked() ? FieldType.INT64_LIST_PACKED : FieldType.INT64_LIST; + case MESSAGE: + if (fd.isMapField()) { + return FieldType.MAP; + } + return fd.isRepeated() ? FieldType.MESSAGE_LIST : FieldType.MESSAGE; + case SFIXED32: + if (!fd.isRepeated()) { + return FieldType.SFIXED32; + } + return fd.isPacked() ? FieldType.SFIXED32_LIST_PACKED : FieldType.SFIXED32_LIST; + case SFIXED64: + if (!fd.isRepeated()) { + return FieldType.SFIXED64; + } + return fd.isPacked() ? FieldType.SFIXED64_LIST_PACKED : FieldType.SFIXED64_LIST; + case SINT32: + if (!fd.isRepeated()) { + return FieldType.SINT32; + } + return fd.isPacked() ? FieldType.SINT32_LIST_PACKED : FieldType.SINT32_LIST; + case SINT64: + if (!fd.isRepeated()) { + return FieldType.SINT64; + } + return fd.isPacked() ? FieldType.SINT64_LIST_PACKED : FieldType.SINT64_LIST; + case STRING: + return fd.isRepeated() ? FieldType.STRING_LIST : FieldType.STRING; + case UINT32: + if (!fd.isRepeated()) { + return FieldType.UINT32; + } + return fd.isPacked() ? FieldType.UINT32_LIST_PACKED : FieldType.UINT32_LIST; + case UINT64: + if (!fd.isRepeated()) { + return FieldType.UINT64; + } + return fd.isPacked() ? FieldType.UINT64_LIST_PACKED : FieldType.UINT64_LIST; + default: + throw new IllegalArgumentException("Unsupported field type: " + fd.getType()); + } + } + + private static Field bitField(Class messageType, int index) { + return field(messageType, "bitField" + index + "_"); + } + + private static Field field(Class messageType, FieldDescriptor fd) { + return field(messageType, getFieldName(fd)); + } + + private static Field cachedSizeField(Class messageType, FieldDescriptor fd) { + return field(messageType, getCachedSizeFieldName(fd)); + } + + private static Field field(Class messageType, String fieldName) { + try { + return messageType.getDeclaredField(fieldName); + } catch (Exception e) { + throw new IllegalArgumentException( + "Unable to find field " + fieldName + " in message class " + messageType.getName()); + } + } + + static String getFieldName(FieldDescriptor fd) { + String name = (fd.getType() == FieldDescriptor.Type.GROUP) + ? fd.getMessageType().getName() + : fd.getName(); + String suffix = specialFieldNames.contains(name) ? "__" : "_"; + return snakeCaseToCamelCase(name) + suffix; + } + + private static String getCachedSizeFieldName(FieldDescriptor fd) { + return snakeCaseToCamelCase(fd.getName()) + "MemoizedSerializedSize"; + } + + /** + * This method must match exactly with the corresponding function in protocol compiler. See: + * https://github.com/google/protobuf/blob/v3.0.0/src/google/protobuf/compiler/java/java_helpers.cc#L153 + */ + private static String snakeCaseToCamelCase(String snakeCase) { + StringBuilder sb = new StringBuilder(snakeCase.length() + 1); + boolean capNext = false; + for (int ctr = 0; ctr < snakeCase.length(); ctr++) { + char next = snakeCase.charAt(ctr); + if (next == '_') { + capNext = true; + } else if (Character.isDigit(next)) { + sb.append(next); + capNext = true; + } else if (capNext) { + sb.append(Character.toUpperCase(next)); + capNext = false; + } else if (ctr == 0) { + sb.append(Character.toLowerCase(next)); + } else { + sb.append(next); + } + } + return sb.toString(); + } + + /** + * Inspects the message to identify the stored type for a message field that is part of a oneof. + */ + private static Class getOneofStoredTypeForMessage(Class messageType, FieldDescriptor fd) { + try { + String name = fd.getType() == Type.GROUP ? fd.getMessageType().getName() : fd.getName(); + Method getter = messageType.getDeclaredMethod(getterForField(name)); + return getter.getReturnType(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** Inspects the message to identify the message type of a repeated message field. */ + private static Class getTypeForRepeatedMessageField(Class messageType, FieldDescriptor fd) { + try { + String name = fd.getType() == Type.GROUP ? fd.getMessageType().getName() : fd.getName(); + Method getter = messageType.getDeclaredMethod(getterForField(name), int.class); + return getter.getReturnType(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** Constructs the name of the get method for the given field in the proto. */ + private static String getterForField(String snakeCase) { + String camelCase = snakeCaseToCamelCase(snakeCase); + StringBuilder builder = new StringBuilder("get"); + // Capitalize the first character in the field name. + builder.append(Character.toUpperCase(camelCase.charAt(0))); + builder.append(camelCase.substring(1, camelCase.length())); + return builder.toString(); + } + + private static final class OneofState { + private OneofInfo[] oneofs = new OneofInfo[2]; + + OneofInfo getOneof(Class messageType, OneofDescriptor desc) { + int index = desc.getIndex(); + if (index >= oneofs.length) { + // Grow the array. + oneofs = Arrays.copyOf(oneofs, index * 2); + } + OneofInfo info = oneofs[index]; + if (info == null) { + info = newInfo(messageType, desc); + oneofs[index] = info; + } + return info; + } + + private static OneofInfo newInfo(Class messageType, OneofDescriptor desc) { + String camelCase = snakeCaseToCamelCase(desc.getName()); + String valueFieldName = camelCase + "_"; + String caseFieldName = camelCase + "Case_"; + + return new OneofInfo( + desc.getIndex(), field(messageType, caseFieldName), field(messageType, valueFieldName)); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/Descriptors.java b/java/core/src/main/java/com/google/protobuf/Descriptors.java index 75b16fe30e..a20829e56d 100644 --- a/java/core/src/main/java/com/google/protobuf/Descriptors.java +++ b/java/core/src/main/java/com/google/protobuf/Descriptors.java @@ -32,7 +32,22 @@ package com.google.protobuf; import static com.google.protobuf.Internal.checkNotNull; -import com.google.protobuf.DescriptorProtos.*; +import com.google.protobuf.DescriptorProtos.DescriptorProto; +import com.google.protobuf.DescriptorProtos.EnumDescriptorProto; +import com.google.protobuf.DescriptorProtos.EnumOptions; +import com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto; +import com.google.protobuf.DescriptorProtos.EnumValueOptions; +import com.google.protobuf.DescriptorProtos.FieldDescriptorProto; +import com.google.protobuf.DescriptorProtos.FieldOptions; +import com.google.protobuf.DescriptorProtos.FileDescriptorProto; +import com.google.protobuf.DescriptorProtos.FileOptions; +import com.google.protobuf.DescriptorProtos.MessageOptions; +import com.google.protobuf.DescriptorProtos.MethodDescriptorProto; +import com.google.protobuf.DescriptorProtos.MethodOptions; +import com.google.protobuf.DescriptorProtos.OneofDescriptorProto; +import com.google.protobuf.DescriptorProtos.OneofOptions; +import com.google.protobuf.DescriptorProtos.ServiceDescriptorProto; +import com.google.protobuf.DescriptorProtos.ServiceOptions; import com.google.protobuf.Descriptors.FileDescriptor.Syntax; import java.lang.ref.WeakReference; import java.util.ArrayList; @@ -49,29 +64,25 @@ import java.util.logging.Logger; /** * Contains a collection of classes which describe protocol message types. * - * Every message type has a {@link Descriptor}, which lists all - * its fields and other information about a type. You can get a message - * type's descriptor by calling {@code MessageType.getDescriptor()}, or - * (given a message object of the type) {@code message.getDescriptorForType()}. - * Furthermore, each message is associated with a {@link FileDescriptor} for - * a relevant {@code .proto} file. You can obtain it by calling - * {@code Descriptor.getFile()}. A {@link FileDescriptor} contains descriptors - * for all the messages defined in that file, and file descriptors for all the - * imported {@code .proto} files. + *

Every message type has a {@link Descriptor}, which lists all its fields and other information + * about a type. You can get a message type's descriptor by calling {@code + * MessageType.getDescriptor()}, or (given a message object of the type) {@code + * message.getDescriptorForType()}. Furthermore, each message is associated with a {@link + * FileDescriptor} for a relevant {@code .proto} file. You can obtain it by calling {@code + * Descriptor.getFile()}. A {@link FileDescriptor} contains descriptors for all the messages defined + * in that file, and file descriptors for all the imported {@code .proto} files. * - * Descriptors are built from DescriptorProtos, as defined in - * {@code google/protobuf/descriptor.proto}. + *

Descriptors are built from DescriptorProtos, as defined in {@code + * google/protobuf/descriptor.proto}. * * @author kenton@google.com Kenton Varda */ public final class Descriptors { - private static final Logger logger = - Logger.getLogger(Descriptors.class.getName()); + private static final Logger logger = Logger.getLogger(Descriptors.class.getName()); /** - * Describes a {@code .proto} file, including everything defined within. - * That includes, in particular, descriptors for all the messages and - * file descriptors for all other imported {@code .proto} files - * (dependencies). + * Describes a {@code .proto} file, including everything defined within. That includes, in + * particular, descriptors for all the messages and file descriptors for all other imported {@code + * .proto} files (dependencies). */ public static final class FileDescriptor extends GenericDescriptor { /** Convert the descriptor to its protocol message representation. */ @@ -99,14 +110,17 @@ public final class Descriptors { } /** - * Get the proto package name. This is the package name given by the - * {@code package} statement in the {@code .proto} file, which differs - * from the Java package. + * Get the proto package name. This is the package name given by the {@code package} statement + * in the {@code .proto} file, which differs from the Java package. */ - public String getPackage() { return proto.getPackage(); } + public String getPackage() { + return proto.getPackage(); + } /** Get the {@code FileOptions}, defined in {@code descriptor.proto}. */ - public FileOptions getOptions() { return proto.getOptions(); } + public FileOptions getOptions() { + return proto.getOptions(); + } /** Get a list of top-level message types declared in this file. */ public List getMessageTypes() { @@ -147,6 +161,7 @@ public final class Descriptors { Syntax(String name) { this.name = name; } + private final String name; } @@ -159,7 +174,7 @@ public final class Descriptors { } /** - * Find a message type in the file by name. Does not find nested types. + * Find a message type in the file by name. Does not find nested types. * * @param name The unqualified type name to look for. * @return The message type's descriptor, or {@code null} if not found. @@ -170,20 +185,20 @@ public final class Descriptors { if (name.indexOf('.') != -1) { return null; } - if (getPackage().length() > 0) { - name = getPackage() + '.' + name; + final String packageName = getPackage(); + if (!packageName.isEmpty()) { + name = packageName + '.' + name; } final GenericDescriptor result = pool.findSymbol(name); - if (result != null && result instanceof Descriptor && - result.getFile() == this) { - return (Descriptor)result; + if (result != null && result instanceof Descriptor && result.getFile() == this) { + return (Descriptor) result; } else { return null; } } /** - * Find an enum type in the file by name. Does not find nested types. + * Find an enum type in the file by name. Does not find nested types. * * @param name The unqualified type name to look for. * @return The enum type's descriptor, or {@code null} if not found. @@ -194,13 +209,13 @@ public final class Descriptors { if (name.indexOf('.') != -1) { return null; } - if (getPackage().length() > 0) { - name = getPackage() + '.' + name; + final String packageName = getPackage(); + if (!packageName.isEmpty()) { + name = packageName + '.' + name; } final GenericDescriptor result = pool.findSymbol(name); - if (result != null && result instanceof EnumDescriptor && - result.getFile() == this) { - return (EnumDescriptor)result; + if (result != null && result instanceof EnumDescriptor && result.getFile() == this) { + return (EnumDescriptor) result; } else { return null; } @@ -218,21 +233,20 @@ public final class Descriptors { if (name.indexOf('.') != -1) { return null; } - if (getPackage().length() > 0) { - name = getPackage() + '.' + name; + final String packageName = getPackage(); + if (!packageName.isEmpty()) { + name = packageName + '.' + name; } final GenericDescriptor result = pool.findSymbol(name); - if (result != null && result instanceof ServiceDescriptor && - result.getFile() == this) { - return (ServiceDescriptor)result; + if (result != null && result instanceof ServiceDescriptor && result.getFile() == this) { + return (ServiceDescriptor) result; } else { return null; } } /** - * Find an extension in the file by name. Does not find extensions nested - * inside message types. + * Find an extension in the file by name. Does not find extensions nested inside message types. * * @param name The unqualified extension name to look for. * @return The extension's descriptor, or {@code null} if not found. @@ -241,13 +255,13 @@ public final class Descriptors { if (name.indexOf('.') != -1) { return null; } - if (getPackage().length() > 0) { - name = getPackage() + '.' + name; + final String packageName = getPackage(); + if (!packageName.isEmpty()) { + name = packageName + '.' + name; } final GenericDescriptor result = pool.findSymbol(name); - if (result != null && result instanceof FieldDescriptor && - result.getFile() == this) { - return (FieldDescriptor)result; + if (result != null && result instanceof FieldDescriptor && result.getFile() == this) { + return (FieldDescriptor) result; } else { return null; } @@ -257,36 +271,31 @@ public final class Descriptors { * Construct a {@code FileDescriptor}. * * @param proto The protocol message form of the FileDescriptor. - * @param dependencies {@code FileDescriptor}s corresponding to all of - * the file's dependencies. - * @throws DescriptorValidationException {@code proto} is not a valid - * descriptor. This can occur for a number of reasons, e.g. - * because a field has an undefined type or because two messages - * were defined with the same name. + * @param dependencies {@code FileDescriptor}s corresponding to all of the file's dependencies. + * @throws DescriptorValidationException {@code proto} is not a valid descriptor. This can occur + * for a number of reasons, e.g. because a field has an undefined type or because two + * messages were defined with the same name. */ - public static FileDescriptor buildFrom(final FileDescriptorProto proto, - final FileDescriptor[] dependencies) - throws DescriptorValidationException { + public static FileDescriptor buildFrom( + final FileDescriptorProto proto, final FileDescriptor[] dependencies) + throws DescriptorValidationException { return buildFrom(proto, dependencies, false); } - /** * Construct a {@code FileDescriptor}. * * @param proto The protocol message form of the FileDescriptor. - * @param dependencies {@code FileDescriptor}s corresponding to all of - * the file's dependencies. - * @param allowUnknownDependencies If true, non-exist dependenncies will be - * ignored and undefined message types will be replaced with a - * placeholder type. - * @throws DescriptorValidationException {@code proto} is not a valid - * descriptor. This can occur for a number of reasons, e.g. - * because a field has an undefined type or because two messages - * were defined with the same name. + * @param dependencies {@code FileDescriptor}s corresponding to all of the file's dependencies. + * @param allowUnknownDependencies If true, non-exist dependenncies will be ignored and + * undefined message types will be replaced with a placeholder type. + * @throws DescriptorValidationException {@code proto} is not a valid descriptor. This can occur + * for a number of reasons, e.g. because a field has an undefined type or because two + * messages were defined with the same name. */ public static FileDescriptor buildFrom( - final FileDescriptorProto proto, final FileDescriptor[] dependencies, + final FileDescriptorProto proto, + final FileDescriptor[] dependencies, final boolean allowUnknownDependencies) throws DescriptorValidationException { // Building descriptors involves two steps: translating and linking. @@ -298,23 +307,14 @@ public final class Descriptors { // FieldDescriptor for an embedded message contains a pointer directly // to the Descriptor for that message's type. We also detect undefined // types in the linking step. - final DescriptorPool pool = new DescriptorPool( - dependencies, allowUnknownDependencies); - final FileDescriptor result = new FileDescriptor( - proto, dependencies, pool, allowUnknownDependencies); + final DescriptorPool pool = new DescriptorPool(dependencies, allowUnknownDependencies); + final FileDescriptor result = + new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies); result.crossLink(); return result; } - /** - * This method is to be called by generated code only. It is equivalent - * to {@code buildFrom} except that the {@code FileDescriptorProto} is - * encoded in protocol buffer wire format. - */ - public static void internalBuildGeneratedFileFrom( - final String[] descriptorDataParts, - final FileDescriptor[] dependencies, - final InternalDescriptorAssigner descriptorAssigner) { + private static byte[] latin1Cat(final String[] strings) { // Hack: We can't embed a raw byte array inside generated Java code // (at least, not efficiently), but we can embed Strings. So, the // protocol compiler embeds the FileDescriptorProto as a giant @@ -323,23 +323,52 @@ public final class Descriptors { // characters, each one representing a byte of the FileDescriptorProto's // serialized form. So, if we convert it to bytes in ISO-8859-1, we // should get the original bytes that we want. - - // descriptorData may contain multiple strings in order to get around the - // Java 64k string literal limit. + // Literal strings are limited to 64k, so it may be split into multiple strings. + if (strings.length == 1) { + return strings[0].getBytes(Internal.ISO_8859_1); + } StringBuilder descriptorData = new StringBuilder(); - for (String part : descriptorDataParts) { + for (String part : strings) { descriptorData.append(part); } + return descriptorData.toString().getBytes(Internal.ISO_8859_1); + } + + private static FileDescriptor[] findDescriptors( + final Class descriptorOuterClass, + final String[] dependencyClassNames, + final String[] dependencyFileNames) { + List descriptors = new ArrayList(); + for (int i = 0; i < dependencyClassNames.length; i++) { + try { + Class clazz = descriptorOuterClass.getClassLoader().loadClass(dependencyClassNames[i]); + descriptors.add((FileDescriptor) clazz.getField("descriptor").get(null)); + } catch (Exception e) { + // We allow unknown dependencies by default. If a dependency cannot + // be found we only generate a warning. + logger.warning("Descriptors for \"" + dependencyFileNames[i] + "\" can not be found."); + } + } + return descriptors.toArray(new FileDescriptor[0]); + } - final byte[] descriptorBytes; - descriptorBytes = descriptorData.toString().getBytes(Internal.ISO_8859_1); + /** + * This method is for backward compatibility with generated code which passed an + * InternalDescriptorAssigner. + */ + @Deprecated + public static void internalBuildGeneratedFileFrom( + final String[] descriptorDataParts, + final FileDescriptor[] dependencies, + final InternalDescriptorAssigner descriptorAssigner) { + final byte[] descriptorBytes = latin1Cat(descriptorDataParts); FileDescriptorProto proto; try { proto = FileDescriptorProto.parseFrom(descriptorBytes); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException( - "Failed to parse protocol buffer descriptor for generated code.", e); + "Failed to parse protocol buffer descriptor for generated code.", e); } final FileDescriptor result; @@ -349,11 +378,10 @@ public final class Descriptors { result = buildFrom(proto, dependencies, true); } catch (DescriptorValidationException e) { throw new IllegalArgumentException( - "Invalid embedded descriptor for \"" + proto.getName() + "\".", e); + "Invalid embedded descriptor for \"" + proto.getName() + "\".", e); } - final ExtensionRegistry registry = - descriptorAssigner.assignDescriptors(result); + final ExtensionRegistry registry = descriptorAssigner.assignDescriptors(result); if (registry != null) { // We must re-parse the proto using the registry. @@ -361,8 +389,7 @@ public final class Descriptors { proto = FileDescriptorProto.parseFrom(descriptorBytes, registry); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException( - "Failed to parse protocol buffer descriptor for generated code.", - e); + "Failed to parse protocol buffer descriptor for generated code.", e); } result.setProto(proto); @@ -370,67 +397,94 @@ public final class Descriptors { } /** - * This method is to be called by generated code only. It uses Java - * reflection to load the dependencies' descriptors. + * This method is to be called by generated code only. It is equivalent to {@code buildFrom} + * except that the {@code FileDescriptorProto} is encoded in protocol buffer wire format. + */ + public static FileDescriptor internalBuildGeneratedFileFrom( + final String[] descriptorDataParts, + final FileDescriptor[] dependencies) { + final byte[] descriptorBytes = latin1Cat(descriptorDataParts); + + FileDescriptorProto proto; + try { + proto = FileDescriptorProto.parseFrom(descriptorBytes); + } catch (InvalidProtocolBufferException e) { + throw new IllegalArgumentException( + "Failed to parse protocol buffer descriptor for generated code.", e); + } + + try { + // When building descriptors for generated code, we allow unknown + // dependencies by default. + return buildFrom(proto, dependencies, true); + } catch (DescriptorValidationException e) { + throw new IllegalArgumentException( + "Invalid embedded descriptor for \"" + proto.getName() + "\".", e); + } + } + + /** + * This method is for backward compatibility with generated code which passed an + * InternalDescriptorAssigner. */ + @Deprecated public static void internalBuildGeneratedFileFrom( final String[] descriptorDataParts, final Class descriptorOuterClass, - final String[] dependencies, + final String[] dependencyClassNames, final String[] dependencyFileNames, final InternalDescriptorAssigner descriptorAssigner) { - List descriptors = new ArrayList(); - for (int i = 0; i < dependencies.length; i++) { - try { - Class clazz = - descriptorOuterClass.getClassLoader().loadClass(dependencies[i]); - descriptors.add( - (FileDescriptor) clazz.getField("descriptor").get(null)); - } catch (Exception e) { - // We allow unknown dependencies by default. If a dependency cannot - // be found we only generate a warning. - logger.warning("Descriptors for \"" + dependencyFileNames[i] + - "\" can not be found."); - } - } - FileDescriptor[] descriptorArray = new FileDescriptor[descriptors.size()]; - descriptors.toArray(descriptorArray); + FileDescriptor[] dependencies = findDescriptors( + descriptorOuterClass, dependencyClassNames, dependencyFileNames); internalBuildGeneratedFileFrom( - descriptorDataParts, descriptorArray, descriptorAssigner); + descriptorDataParts, dependencies, descriptorAssigner); + } + + /** + * This method is to be called by generated code only. It uses Java reflection to load the + * dependencies' descriptors. + */ + public static FileDescriptor internalBuildGeneratedFileFrom( + final String[] descriptorDataParts, + final Class descriptorOuterClass, + final String[] dependencyClassNames, + final String[] dependencyFileNames) { + FileDescriptor[] dependencies = findDescriptors( + descriptorOuterClass, dependencyClassNames, dependencyFileNames); + return internalBuildGeneratedFileFrom(descriptorDataParts, dependencies); } /** - * This method is to be called by generated code only. It is used to - * update the FileDescriptorProto associated with the descriptor by - * parsing it again with the given ExtensionRegistry. This is needed to - * recognize custom options. + * This method is to be called by generated code only. It is used to update the + * FileDescriptorProto associated with the descriptor by parsing it again with the given + * ExtensionRegistry. This is needed to recognize custom options. */ public static void internalUpdateFileDescriptor( - final FileDescriptor descriptor, - final ExtensionRegistry registry) { + final FileDescriptor descriptor, final ExtensionRegistry registry) { ByteString bytes = descriptor.proto.toByteString(); FileDescriptorProto proto; try { proto = FileDescriptorProto.parseFrom(bytes, registry); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException( - "Failed to parse protocol buffer descriptor for generated code.", e); + "Failed to parse protocol buffer descriptor for generated code.", e); } descriptor.setProto(proto); } /** - * This class should be used by generated code only. When calling - * {@link FileDescriptor#internalBuildGeneratedFileFrom}, the caller - * provides a callback implementing this interface. The callback is called - * after the FileDescriptor has been constructed, in order to assign all - * the global variables defined in the generated code which point at parts - * of the FileDescriptor. The callback returns an ExtensionRegistry which - * contains any extensions which might be used in the descriptor -- that - * is, extensions of the various "Options" messages defined in - * descriptor.proto. The callback may also return null to indicate that - * no extensions are used in the descriptor. + * This class should be used by generated code only. When calling {@link + * FileDescriptor#internalBuildGeneratedFileFrom}, the caller provides a callback implementing + * this interface. The callback is called after the FileDescriptor has been constructed, in + * order to assign all the global variables defined in the generated code which point at parts + * of the FileDescriptor. The callback returns an ExtensionRegistry which contains any + * extensions which might be used in the descriptor -- that is, extensions of the various + * "Options" messages defined in descriptor.proto. The callback may also return null to indicate + * that no extensions are used in the descriptor. + * + * This interface is deprecated. Use the return value of internalBuildGeneratedFrom() instead. */ + @Deprecated public interface InternalDescriptorAssigner { ExtensionRegistry assignDescriptors(FileDescriptor root); } @@ -444,16 +498,16 @@ public final class Descriptors { private final FileDescriptor[] publicDependencies; private final DescriptorPool pool; - private FileDescriptor(final FileDescriptorProto proto, - final FileDescriptor[] dependencies, - final DescriptorPool pool, - boolean allowUnknownDependencies) - throws DescriptorValidationException { + private FileDescriptor( + final FileDescriptorProto proto, + final FileDescriptor[] dependencies, + final DescriptorPool pool, + boolean allowUnknownDependencies) + throws DescriptorValidationException { this.pool = pool; this.proto = proto; this.dependencies = dependencies.clone(); - HashMap nameToFileMap = - new HashMap(); + HashMap nameToFileMap = new HashMap(); for (FileDescriptor file : dependencies) { nameToFileMap.put(file.getName(), file); } @@ -461,15 +515,13 @@ public final class Descriptors { for (int i = 0; i < proto.getPublicDependencyCount(); i++) { int index = proto.getPublicDependency(i); if (index < 0 || index >= proto.getDependencyCount()) { - throw new DescriptorValidationException(this, - "Invalid public dependency index."); + throw new DescriptorValidationException(this, "Invalid public dependency index."); } String name = proto.getDependency(index); FileDescriptor file = nameToFileMap.get(name); if (file == null) { if (!allowUnknownDependencies) { - throw new DescriptorValidationException(this, - "Invalid public dependency: " + name); + throw new DescriptorValidationException(this, "Invalid public dependency: " + name); } // Ignore unknown dependencies. } else { @@ -483,8 +535,7 @@ public final class Descriptors { messageTypes = new Descriptor[proto.getMessageTypeCount()]; for (int i = 0; i < proto.getMessageTypeCount(); i++) { - messageTypes[i] = - new Descriptor(proto.getMessageType(i), this, null, i); + messageTypes[i] = new Descriptor(proto.getMessageType(i), this, null, i); } enumTypes = new EnumDescriptor[proto.getEnumTypeCount()]; @@ -499,20 +550,19 @@ public final class Descriptors { extensions = new FieldDescriptor[proto.getExtensionCount()]; for (int i = 0; i < proto.getExtensionCount(); i++) { - extensions[i] = new FieldDescriptor( - proto.getExtension(i), this, null, i, true); + extensions[i] = new FieldDescriptor(proto.getExtension(i), this, null, i, true); } } - /** - * Create a placeholder FileDescriptor for a message Descriptor. - */ - FileDescriptor(String packageName, Descriptor message) - throws DescriptorValidationException { + /** Create a placeholder FileDescriptor for a message Descriptor. */ + FileDescriptor(String packageName, Descriptor message) throws DescriptorValidationException { this.pool = new DescriptorPool(new FileDescriptor[0], true); - this.proto = FileDescriptorProto.newBuilder() - .setName(message.getFullName() + ".placeholder.proto") - .setPackage(packageName).addMessageType(message.toProto()).build(); + this.proto = + FileDescriptorProto.newBuilder() + .setName(message.getFullName() + ".placeholder.proto") + .setPackage(packageName) + .addMessageType(message.toProto()) + .build(); this.dependencies = new FileDescriptor[0]; this.publicDependencies = new FileDescriptor[0]; @@ -541,14 +591,12 @@ public final class Descriptors { } /** - * Replace our {@link FileDescriptorProto} with the given one, which is - * identical except that it might contain extensions that weren't present - * in the original. This method is needed for bootstrapping when a file - * defines custom options. The options may be defined in the file itself, - * so we can't actually parse them until we've constructed the descriptors, - * but to construct the descriptors we have to have parsed the descriptor - * protos. So, we have to parse the descriptor protos a second time after - * constructing the descriptors. + * Replace our {@link FileDescriptorProto} with the given one, which is identical except that it + * might contain extensions that weren't present in the original. This method is needed for + * bootstrapping when a file defines custom options. The options may be defined in the file + * itself, so we can't actually parse them until we've constructed the descriptors, but to + * construct the descriptors we have to have parsed the descriptor protos. So, we have to parse + * the descriptor protos a second time after constructing the descriptors. */ private void setProto(final FileDescriptorProto proto) { this.proto = proto; @@ -580,19 +628,24 @@ public final class Descriptors { /** Describes a message type. */ public static final class Descriptor extends GenericDescriptor { /** - * Get the index of this descriptor within its parent. In other words, - * given a {@link FileDescriptor} {@code file}, the following is true: + * Get the index of this descriptor within its parent. In other words, given a {@link + * FileDescriptor} {@code file}, the following is true: + * *

      *   for all i in [0, file.getMessageTypeCount()):
      *     file.getMessageType(i).getIndex() == i
      * 
+ * * Similarly, for a {@link Descriptor} {@code messageType}: + * *
      *   for all i in [0, messageType.getNestedTypeCount()):
      *     messageType.getNestedType(i).getIndex() == i
      * 
*/ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -607,14 +660,15 @@ public final class Descriptors { } /** - * Get the type's fully-qualified name, within the proto language's - * namespace. This differs from the Java name. For example, given this - * {@code .proto}: + * Get the type's fully-qualified name, within the proto language's namespace. This differs from + * the Java name. For example, given this {@code .proto}: + * *
      *   package foo.bar;
      *   option java_package = "com.example.protos"
      *   message Baz {}
      * 
+ * * {@code Baz}'s full name is "foo.bar.Baz". */ @Override @@ -629,10 +683,14 @@ public final class Descriptors { } /** If this is a nested type, get the outer descriptor, otherwise null. */ - public Descriptor getContainingType() { return containingType; } + public Descriptor getContainingType() { + return containingType; + } /** Get the {@code MessageOptions}, defined in {@code descriptor.proto}. */ - public MessageOptions getOptions() { return proto.getOptions(); } + public MessageOptions getOptions() { + return proto.getOptions(); + } /** Get a list of this message type's fields. */ public List getFields() { @@ -661,8 +719,7 @@ public final class Descriptors { /** Determines if the given field number is an extension. */ public boolean isExtensionNumber(final int number) { - for (final DescriptorProto.ExtensionRange range : - proto.getExtensionRangeList()) { + for (final DescriptorProto.ExtensionRange range : proto.getExtensionRangeList()) { if (range.getStart() <= number && number < range.getEnd()) { return true; } @@ -672,8 +729,7 @@ public final class Descriptors { /** Determines if the given field number is reserved. */ public boolean isReservedNumber(final int number) { - for (final DescriptorProto.ReservedRange range : - proto.getReservedRangeList()) { + for (final DescriptorProto.ReservedRange range : proto.getReservedRangeList()) { if (range.getStart() <= number && number < range.getEnd()) { return true; } @@ -693,8 +749,8 @@ public final class Descriptors { } /** - * Indicates whether the message can be extended. That is, whether it has - * any "extensions x to y" ranges declared on it. + * Indicates whether the message can be extended. That is, whether it has any "extensions x to + * y" ranges declared on it. */ public boolean isExtendable() { return proto.getExtensionRangeList().size() != 0; @@ -702,14 +758,14 @@ public final class Descriptors { /** * Finds a field by name. + * * @param name The unqualified name of the field (e.g. "foo"). * @return The field's descriptor, or {@code null} if not found. */ public FieldDescriptor findFieldByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof FieldDescriptor) { - return (FieldDescriptor)result; + return (FieldDescriptor) result; } else { return null; } @@ -717,24 +773,24 @@ public final class Descriptors { /** * Finds a field by field number. + * * @param number The field number within this message type. * @return The field's descriptor, or {@code null} if not found. */ public FieldDescriptor findFieldByNumber(final int number) { - return file.pool.fieldsByNumber.get( - new DescriptorPool.DescriptorIntPair(this, number)); + return file.pool.fieldsByNumber.get(new DescriptorPool.DescriptorIntPair(this, number)); } /** * Finds a nested message type by name. + * * @param name The unqualified name of the nested type (e.g. "Foo"). * @return The types's descriptor, or {@code null} if not found. */ public Descriptor findNestedTypeByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof Descriptor) { - return (Descriptor)result; + return (Descriptor) result; } else { return null; } @@ -742,14 +798,14 @@ public final class Descriptors { /** * Finds a nested enum type by name. + * * @param name The unqualified name of the nested type (e.g. "Foo"). * @return The types's descriptor, or {@code null} if not found. */ public EnumDescriptor findEnumTypeByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof EnumDescriptor) { - return (EnumDescriptor)result; + return (EnumDescriptor) result; } else { return null; } @@ -776,9 +832,12 @@ public final class Descriptors { packageName = fullname.substring(0, pos); } this.index = 0; - this.proto = DescriptorProto.newBuilder().setName(name).addExtensionRange( - DescriptorProto.ExtensionRange.newBuilder().setStart(1) - .setEnd(536870912).build()).build(); + this.proto = + DescriptorProto.newBuilder() + .setName(name) + .addExtensionRange( + DescriptorProto.ExtensionRange.newBuilder().setStart(1).setEnd(536870912).build()) + .build(); this.fullName = fullname; this.containingType = null; @@ -792,11 +851,12 @@ public final class Descriptors { this.file = new FileDescriptor(packageName, this); } - private Descriptor(final DescriptorProto proto, - final FileDescriptor file, - final Descriptor parent, - final int index) - throws DescriptorValidationException { + private Descriptor( + final DescriptorProto proto, + final FileDescriptor file, + final Descriptor parent, + final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, parent, proto.getName()); @@ -805,32 +865,27 @@ public final class Descriptors { oneofs = new OneofDescriptor[proto.getOneofDeclCount()]; for (int i = 0; i < proto.getOneofDeclCount(); i++) { - oneofs[i] = new OneofDescriptor( - proto.getOneofDecl(i), file, this, i); + oneofs[i] = new OneofDescriptor(proto.getOneofDecl(i), file, this, i); } nestedTypes = new Descriptor[proto.getNestedTypeCount()]; for (int i = 0; i < proto.getNestedTypeCount(); i++) { - nestedTypes[i] = new Descriptor( - proto.getNestedType(i), file, this, i); + nestedTypes[i] = new Descriptor(proto.getNestedType(i), file, this, i); } enumTypes = new EnumDescriptor[proto.getEnumTypeCount()]; for (int i = 0; i < proto.getEnumTypeCount(); i++) { - enumTypes[i] = new EnumDescriptor( - proto.getEnumType(i), file, this, i); + enumTypes[i] = new EnumDescriptor(proto.getEnumType(i), file, this, i); } fields = new FieldDescriptor[proto.getFieldCount()]; for (int i = 0; i < proto.getFieldCount(); i++) { - fields[i] = new FieldDescriptor( - proto.getField(i), file, this, i, false); + fields[i] = new FieldDescriptor(proto.getField(i), file, this, i, false); } extensions = new FieldDescriptor[proto.getExtensionCount()]; for (int i = 0; i < proto.getExtensionCount(); i++) { - extensions[i] = new FieldDescriptor( - proto.getExtension(i), file, this, i, true); + extensions[i] = new FieldDescriptor(proto.getExtension(i), file, this, i, true); } for (int i = 0; i < proto.getOneofDeclCount(); i++) { @@ -891,15 +946,16 @@ public final class Descriptors { // ================================================================= /** Describes a field of a message type. */ - public static final class FieldDescriptor - extends GenericDescriptor - implements Comparable, - FieldSet.FieldDescriptorLite { + public static final class FieldDescriptor extends GenericDescriptor + implements Comparable, FieldSet.FieldDescriptorLite { /** * Get the index of this descriptor within its parent. + * * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -921,6 +977,7 @@ public final class Descriptors { /** * Get the field's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -934,10 +991,12 @@ public final class Descriptors { } /** - * Get the field's java type. This is just for convenience. Every - * {@code FieldDescriptorProto.Type} maps to exactly one Java type. + * Get the field's java type. This is just for convenience. Every {@code + * FieldDescriptorProto.Type} maps to exactly one Java type. */ - public JavaType getJavaType() { return type.getJavaType(); } + public JavaType getJavaType() { + return type.getJavaType(); + } /** For internal use only. */ @Override @@ -952,7 +1011,9 @@ public final class Descriptors { } /** Get the field's declared type. */ - public Type getType() { return type; } + public Type getType() { + return type; + } /** For internal use only. */ @Override @@ -976,15 +1037,15 @@ public final class Descriptors { } public boolean isMapField() { - return getType() == Type.MESSAGE && isRepeated() + return getType() == Type.MESSAGE + && isRepeated() && getMessageType().getOptions().getMapEntry(); } // I'm pretty sure values() constructs a new array every time, since there // is nothing stopping the caller from mutating the array. Therefore we // make a static copy here. - private static final WireFormat.FieldType[] table = - WireFormat.FieldType.values(); + private static final WireFormat.FieldType[] table = WireFormat.FieldType.values(); /** Is this field declared required? */ public boolean isRequired() { @@ -1002,8 +1063,9 @@ public final class Descriptors { return proto.getLabel() == FieldDescriptorProto.Label.LABEL_REPEATED; } - /** Does this field have the {@code [packed = true]} option or is this field - * packable in proto3 and not explicitly setted to unpacked? + /** + * Does this field have the {@code [packed = true]} option or is this field packable in proto3 + * and not explicitly setted to unpacked? */ @Override public boolean isPacked() { @@ -1023,42 +1085,50 @@ public final class Descriptors { } /** Returns true if the field had an explicitly-defined default value. */ - public boolean hasDefaultValue() { return proto.hasDefaultValue(); } + public boolean hasDefaultValue() { + return proto.hasDefaultValue(); + } /** - * Returns the field's default value. Valid for all types except for - * messages and groups. For all other types, the object returned is of - * the same class that would returned by Message.getField(this). + * Returns the field's default value. Valid for all types except for messages and groups. For + * all other types, the object returned is of the same class that would returned by + * Message.getField(this). */ public Object getDefaultValue() { if (getJavaType() == JavaType.MESSAGE) { throw new UnsupportedOperationException( - "FieldDescriptor.getDefaultValue() called on an embedded message " + - "field."); + "FieldDescriptor.getDefaultValue() called on an embedded message field."); } return defaultValue; } /** Get the {@code FieldOptions}, defined in {@code descriptor.proto}. */ - public FieldOptions getOptions() { return proto.getOptions(); } + public FieldOptions getOptions() { + return proto.getOptions(); + } /** Is this field an extension? */ - public boolean isExtension() { return proto.hasExtendee(); } + public boolean isExtension() { + return proto.hasExtendee(); + } /** - * Get the field's containing type. For extensions, this is the type being - * extended, not the location where the extension was defined. See - * {@link #getExtensionScope()}. + * Get the field's containing type. For extensions, this is the type being extended, not the + * location where the extension was defined. See {@link #getExtensionScope()}. */ - public Descriptor getContainingType() { return containingType; } + public Descriptor getContainingType() { + return containingType; + } /** Get the field's containing oneof. */ - public OneofDescriptor getContainingOneof() { return containingOneof; } + public OneofDescriptor getContainingOneof() { + return containingOneof; + } /** - * For extensions defined nested within message types, gets the outer - * type. Not valid for non-extension fields. For example, consider - * this {@code .proto} file: + * For extensions defined nested within message types, gets the outer type. Not valid for + * non-extension fields. For example, consider this {@code .proto} file: + * *
      *   message Foo {
      *     extensions 1000 to max;
@@ -1072,14 +1142,14 @@ public final class Descriptors {
      *     }
      *   }
      * 
- * Both {@code baz}'s and {@code qux}'s containing type is {@code Foo}. - * However, {@code baz}'s extension scope is {@code null} while - * {@code qux}'s extension scope is {@code Bar}. + * + * Both {@code baz}'s and {@code qux}'s containing type is {@code Foo}. However, {@code baz}'s + * extension scope is {@code null} while {@code qux}'s extension scope is {@code Bar}. */ public Descriptor getExtensionScope() { if (!isExtension()) { throw new UnsupportedOperationException( - "This field is not an extension."); + String.format("This field is not an extension. (%s)", fullName)); } return extensionScope; } @@ -1088,7 +1158,7 @@ public final class Descriptors { public Descriptor getMessageType() { if (getJavaType() != JavaType.MESSAGE) { throw new UnsupportedOperationException( - "This field is not of message type."); + String.format("This field is not of message type. (%s)", fullName)); } return messageType; } @@ -1098,27 +1168,25 @@ public final class Descriptors { public EnumDescriptor getEnumType() { if (getJavaType() != JavaType.ENUM) { throw new UnsupportedOperationException( - "This field is not of enum type."); + String.format("This field is not of enum type. (%s)", fullName)); } return enumType; } /** - * Compare with another {@code FieldDescriptor}. This orders fields in - * "canonical" order, which simply means ascending order by field number. - * {@code other} must be a field of the same type -- i.e. - * {@code getContainingType()} must return the same {@code Descriptor} for - * both fields. + * Compare with another {@code FieldDescriptor}. This orders fields in "canonical" order, which + * simply means ascending order by field number. {@code other} must be a field of the same type + * -- i.e. {@code getContainingType()} must return the same {@code Descriptor} for both fields. * - * @return negative, zero, or positive if {@code this} is less than, - * equal to, or greater than {@code other}, respectively. + * @return negative, zero, or positive if {@code this} is less than, equal to, or greater than + * {@code other}, respectively. */ @Override public int compareTo(final FieldDescriptor other) { if (other.containingType != containingType) { throw new IllegalArgumentException( - "FieldDescriptors can only be compared to other FieldDescriptors " + - "for fields of the same message type."); + "FieldDescriptors can only be compared to other FieldDescriptors " + + "for fields of the same message type."); } return getNumber() - other.getNumber(); } @@ -1145,24 +1213,24 @@ public final class Descriptors { private Object defaultValue; public enum Type { - DOUBLE (JavaType.DOUBLE ), - FLOAT (JavaType.FLOAT ), - INT64 (JavaType.LONG ), - UINT64 (JavaType.LONG ), - INT32 (JavaType.INT ), - FIXED64 (JavaType.LONG ), - FIXED32 (JavaType.INT ), - BOOL (JavaType.BOOLEAN ), - STRING (JavaType.STRING ), - GROUP (JavaType.MESSAGE ), - MESSAGE (JavaType.MESSAGE ), - BYTES (JavaType.BYTE_STRING), - UINT32 (JavaType.INT ), - ENUM (JavaType.ENUM ), - SFIXED32(JavaType.INT ), - SFIXED64(JavaType.LONG ), - SINT32 (JavaType.INT ), - SINT64 (JavaType.LONG ); + DOUBLE(JavaType.DOUBLE), + FLOAT(JavaType.FLOAT), + INT64(JavaType.LONG), + UINT64(JavaType.LONG), + INT32(JavaType.INT), + FIXED64(JavaType.LONG), + FIXED32(JavaType.INT), + BOOL(JavaType.BOOLEAN), + STRING(JavaType.STRING), + GROUP(JavaType.MESSAGE), + MESSAGE(JavaType.MESSAGE), + BYTES(JavaType.BYTE_STRING), + UINT32(JavaType.INT), + ENUM(JavaType.ENUM), + SFIXED32(JavaType.INT), + SFIXED64(JavaType.LONG), + SINT32(JavaType.INT), + SINT64(JavaType.LONG); Type(final JavaType javaType) { this.javaType = javaType; @@ -1173,7 +1241,10 @@ public final class Descriptors { public FieldDescriptorProto.Type toProto() { return FieldDescriptorProto.Type.forNumber(ordinal() + 1); } - public JavaType getJavaType() { return javaType; } + + public JavaType getJavaType() { + return javaType; + } public static Type valueOf(final FieldDescriptorProto.Type type) { return values()[type.getNumber() - 1]; @@ -1183,9 +1254,8 @@ public final class Descriptors { static { // Refuse to init if someone added a new declared type. if (Type.values().length != FieldDescriptorProto.Type.values().length) { - throw new RuntimeException("" - + "descriptor.proto has a new declared type but Descriptors.java " - + "wasn't updated."); + throw new RuntimeException( + "descriptor.proto has a new declared type but Descriptors.java wasn't updated."); } } @@ -1205,8 +1275,8 @@ public final class Descriptors { } /** - * The default default value for fields of this type, if it's a primitive - * type. This is meant for use inside this file only, hence is private. + * The default default value for fields of this type, if it's a primitive type. This is meant + * for use inside this file only, hence is private. */ private final Object defaultDefault; } @@ -1214,14 +1284,20 @@ public final class Descriptors { // This method should match exactly with the ToJsonName() function in C++ // descriptor.cc. private static String fieldNameToJsonName(String name) { - StringBuilder result = new StringBuilder(name.length()); + final int length = name.length(); + StringBuilder result = new StringBuilder(length); boolean isNextUpperCase = false; - for (int i = 0; i < name.length(); i++) { - Character ch = name.charAt(i); + for (int i = 0; i < length; i++) { + char ch = name.charAt(i); if (ch == '_') { isNextUpperCase = true; } else if (isNextUpperCase) { - result.append(Character.toUpperCase(ch)); + // This closely matches the logic for ASCII characters in: + // http://google3/google/protobuf/descriptor.cc?l=249-251&rcl=228891689 + if ('a' <= ch && ch <= 'z') { + ch = (char) (ch - 'a' + 'A'); + } + result.append(ch); isNextUpperCase = false; } else { result.append(ch); @@ -1230,12 +1306,13 @@ public final class Descriptors { return result.toString(); } - private FieldDescriptor(final FieldDescriptorProto proto, - final FileDescriptor file, - final Descriptor parent, - final int index, - final boolean isExtension) - throws DescriptorValidationException { + private FieldDescriptor( + final FieldDescriptorProto proto, + final FileDescriptor file, + final Descriptor parent, + final int index, + final boolean isExtension) + throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, parent, proto.getName()); @@ -1251,16 +1328,15 @@ public final class Descriptors { } if (getNumber() <= 0) { - throw new DescriptorValidationException(this, - "Field numbers must be positive integers."); + throw new DescriptorValidationException(this, "Field numbers must be positive integers."); } if (isExtension) { if (!proto.hasExtendee()) { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.extendee not set for extension field."); + throw new DescriptorValidationException( + this, "FieldDescriptorProto.extendee not set for extension field."); } - containingType = null; // Will be filled in when cross-linking + containingType = null; // Will be filled in when cross-linking if (parent != null) { extensionScope = parent; } else { @@ -1268,23 +1344,23 @@ public final class Descriptors { } if (proto.hasOneofIndex()) { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.oneof_index set for extension field."); + throw new DescriptorValidationException( + this, "FieldDescriptorProto.oneof_index set for extension field."); } containingOneof = null; } else { if (proto.hasExtendee()) { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.extendee set for non-extension field."); + throw new DescriptorValidationException( + this, "FieldDescriptorProto.extendee set for non-extension field."); } containingType = parent; if (proto.hasOneofIndex()) { - if (proto.getOneofIndex() < 0 || - proto.getOneofIndex() >= parent.toProto().getOneofDeclCount()) { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.oneof_index is out of range for type " - + parent.getName()); + if (proto.getOneofIndex() < 0 + || proto.getOneofIndex() >= parent.toProto().getOneofDeclCount()) { + throw new DescriptorValidationException( + this, + "FieldDescriptorProto.oneof_index is out of range for type " + parent.getName()); } containingOneof = parent.getOneofs().get(proto.getOneofIndex()); containingOneof.fieldCount++; @@ -1301,26 +1377,29 @@ public final class Descriptors { private void crossLink() throws DescriptorValidationException { if (proto.hasExtendee()) { final GenericDescriptor extendee = - file.pool.lookupSymbol(proto.getExtendee(), this, - DescriptorPool.SearchFilter.TYPES_ONLY); + file.pool.lookupSymbol( + proto.getExtendee(), this, DescriptorPool.SearchFilter.TYPES_ONLY); if (!(extendee instanceof Descriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getExtendee() + "\" is not a message type."); + throw new DescriptorValidationException( + this, '\"' + proto.getExtendee() + "\" is not a message type."); } - containingType = (Descriptor)extendee; + containingType = (Descriptor) extendee; if (!getContainingType().isExtensionNumber(getNumber())) { - throw new DescriptorValidationException(this, - '\"' + getContainingType().getFullName() + - "\" does not declare " + getNumber() + - " as an extension number."); + throw new DescriptorValidationException( + this, + '\"' + + getContainingType().getFullName() + + "\" does not declare " + + getNumber() + + " as an extension number."); } } if (proto.hasTypeName()) { final GenericDescriptor typeDescriptor = - file.pool.lookupSymbol(proto.getTypeName(), this, - DescriptorPool.SearchFilter.TYPES_ONLY); + file.pool.lookupSymbol( + proto.getTypeName(), this, DescriptorPool.SearchFilter.TYPES_ONLY); if (!proto.hasType()) { // Choose field type based on symbol. @@ -1329,53 +1408,49 @@ public final class Descriptors { } else if (typeDescriptor instanceof EnumDescriptor) { type = Type.ENUM; } else { - throw new DescriptorValidationException(this, - '\"' + proto.getTypeName() + "\" is not a type."); + throw new DescriptorValidationException( + this, '\"' + proto.getTypeName() + "\" is not a type."); } } if (getJavaType() == JavaType.MESSAGE) { if (!(typeDescriptor instanceof Descriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getTypeName() + "\" is not a message type."); + throw new DescriptorValidationException( + this, '\"' + proto.getTypeName() + "\" is not a message type."); } - messageType = (Descriptor)typeDescriptor; + messageType = (Descriptor) typeDescriptor; if (proto.hasDefaultValue()) { - throw new DescriptorValidationException(this, - "Messages can't have default values."); + throw new DescriptorValidationException(this, "Messages can't have default values."); } } else if (getJavaType() == JavaType.ENUM) { if (!(typeDescriptor instanceof EnumDescriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getTypeName() + "\" is not an enum type."); + throw new DescriptorValidationException( + this, '\"' + proto.getTypeName() + "\" is not an enum type."); } - enumType = (EnumDescriptor)typeDescriptor; + enumType = (EnumDescriptor) typeDescriptor; } else { - throw new DescriptorValidationException(this, - "Field with primitive type has type_name."); + throw new DescriptorValidationException(this, "Field with primitive type has type_name."); } } else { - if (getJavaType() == JavaType.MESSAGE || - getJavaType() == JavaType.ENUM) { - throw new DescriptorValidationException(this, - "Field with message or enum type missing type_name."); + if (getJavaType() == JavaType.MESSAGE || getJavaType() == JavaType.ENUM) { + throw new DescriptorValidationException( + this, "Field with message or enum type missing type_name."); } } // Only repeated primitive fields may be packed. if (proto.getOptions().getPacked() && !isPackable()) { - throw new DescriptorValidationException(this, - "[packed = true] can only be specified for repeated primitive " + - "fields."); + throw new DescriptorValidationException( + this, "[packed = true] can only be specified for repeated primitive fields."); } // We don't attempt to parse the default value until here because for // enums we need the enum type's descriptor. if (proto.hasDefaultValue()) { if (isRepeated()) { - throw new DescriptorValidationException(this, - "Repeated fields cannot have default values."); + throw new DescriptorValidationException( + this, "Repeated fields cannot have default values."); } try { @@ -1428,30 +1503,26 @@ public final class Descriptors { break; case BYTES: try { - defaultValue = - TextFormat.unescapeBytes(proto.getDefaultValue()); + defaultValue = TextFormat.unescapeBytes(proto.getDefaultValue()); } catch (TextFormat.InvalidEscapeSequenceException e) { - throw new DescriptorValidationException(this, - "Couldn't parse default value: " + e.getMessage(), e); + throw new DescriptorValidationException( + this, "Couldn't parse default value: " + e.getMessage(), e); } break; case ENUM: defaultValue = enumType.findValueByName(proto.getDefaultValue()); if (defaultValue == null) { - throw new DescriptorValidationException(this, - "Unknown enum default value: \"" + - proto.getDefaultValue() + '\"'); + throw new DescriptorValidationException( + this, "Unknown enum default value: \"" + proto.getDefaultValue() + '\"'); } break; case MESSAGE: case GROUP: - throw new DescriptorValidationException(this, - "Message type had default value."); + throw new DescriptorValidationException(this, "Message type had default value."); } } catch (NumberFormatException e) { - throw new DescriptorValidationException(this, - "Could not parse default value: \"" + - proto.getDefaultValue() + '\"', e); + throw new DescriptorValidationException( + this, "Could not parse default value: \"" + proto.getDefaultValue() + '\"', e); } } else { // Determine the default default for this field. @@ -1478,16 +1549,15 @@ public final class Descriptors { file.pool.addFieldByNumber(this); } - if (containingType != null && - containingType.getOptions().getMessageSetWireFormat()) { + if (containingType != null && containingType.getOptions().getMessageSetWireFormat()) { if (isExtension()) { if (!isOptional() || getType() != Type.MESSAGE) { - throw new DescriptorValidationException(this, - "Extensions of MessageSets must be optional messages."); + throw new DescriptorValidationException( + this, "Extensions of MessageSets must be optional messages."); } } else { - throw new DescriptorValidationException(this, - "MessageSets cannot have fields, only extensions."); + throw new DescriptorValidationException( + this, "MessageSets cannot have fields, only extensions."); } } } @@ -1497,10 +1567,7 @@ public final class Descriptors { this.proto = proto; } - /** - * For internal use only. This is to satisfy the FieldDescriptorLite - * interface. - */ + /** For internal use only. This is to satisfy the FieldDescriptorLite interface. */ @Override public MessageLite.Builder internalMergeFrom(MessageLite.Builder to, MessageLite from) { // FieldDescriptors are only used with non-lite messages so we can just @@ -1517,9 +1584,12 @@ public final class Descriptors { implements Internal.EnumLiteMap { /** * Get the index of this descriptor within its parent. + * * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -1535,6 +1605,7 @@ public final class Descriptors { /** * Get the type's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -1549,10 +1620,14 @@ public final class Descriptors { } /** If this is a nested type, get the outer descriptor, otherwise null. */ - public Descriptor getContainingType() { return containingType; } + public Descriptor getContainingType() { + return containingType; + } /** Get the {@code EnumOptions}, defined in {@code descriptor.proto}. */ - public EnumOptions getOptions() { return proto.getOptions(); } + public EnumOptions getOptions() { + return proto.getOptions(); + } /** Get a list of defined values for this enum. */ public List getValues() { @@ -1561,34 +1636,34 @@ public final class Descriptors { /** * Find an enum value by name. + * * @param name The unqualified name of the value (e.g. "FOO"). * @return the value's descriptor, or {@code null} if not found. */ public EnumValueDescriptor findValueByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof EnumValueDescriptor) { - return (EnumValueDescriptor)result; + return (EnumValueDescriptor) result; } else { return null; } } /** - * Find an enum value by number. If multiple enum values have the same - * number, this returns the first defined value with that number. + * Find an enum value by number. If multiple enum values have the same number, this returns the + * first defined value with that number. + * * @param number The value's number. * @return the value's descriptor, or {@code null} if not found. */ @Override public EnumValueDescriptor findValueByNumber(final int number) { - return file.pool.enumValuesByNumber.get( - new DescriptorPool.DescriptorIntPair(this, number)); + return file.pool.enumValuesByNumber.get(new DescriptorPool.DescriptorIntPair(this, number)); } /** - * Get the enum value for a number. If no enum value has this number, - * construct an EnumValueDescriptor for it. + * Get the enum value for a number. If no enum value has this number, construct an + * EnumValueDescriptor for it. */ public EnumValueDescriptor findValueByNumberCreatingIfUnknown(final int number) { EnumValueDescriptor result = findValueByNumber(number); @@ -1653,11 +1728,12 @@ public final class Descriptors { private final WeakHashMap> unknownValues = new WeakHashMap>(); - private EnumDescriptor(final EnumDescriptorProto proto, - final FileDescriptor file, - final Descriptor parent, - final int index) - throws DescriptorValidationException { + private EnumDescriptor( + final EnumDescriptorProto proto, + final FileDescriptor file, + final Descriptor parent, + final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, parent, proto.getName()); @@ -1667,14 +1743,12 @@ public final class Descriptors { if (proto.getValueCount() == 0) { // We cannot allow enums with no values because this would mean there // would be no valid default value for fields of this type. - throw new DescriptorValidationException(this, - "Enums must contain at least one value."); + throw new DescriptorValidationException(this, "Enums must contain at least one value."); } values = new EnumValueDescriptor[proto.getValueCount()]; for (int i = 0; i < proto.getValueCount(); i++) { - values[i] = new EnumValueDescriptor( - proto.getValue(i), file, this, i); + values[i] = new EnumValueDescriptor(proto.getValue(i), file, this, i); } file.pool.addSymbol(this); @@ -1693,18 +1767,20 @@ public final class Descriptors { // ================================================================= /** - * Describes one value within an enum type. Note that multiple defined - * values may have the same number. In generated Java code, all values - * with the same number after the first become aliases of the first. - * However, they still have independent EnumValueDescriptors. + * Describes one value within an enum type. Note that multiple defined values may have the same + * number. In generated Java code, all values with the same number after the first become aliases + * of the first. However, they still have independent EnumValueDescriptors. */ public static final class EnumValueDescriptor extends GenericDescriptor implements Internal.EnumLite { /** * Get the index of this descriptor within its parent. + * * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -1725,10 +1801,13 @@ public final class Descriptors { } @Override - public String toString() { return proto.getName(); } + public String toString() { + return proto.getName(); + } /** * Get the value's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -1743,12 +1822,14 @@ public final class Descriptors { } /** Get the value's enum type. */ - public EnumDescriptor getType() { return type; } + public EnumDescriptor getType() { + return type; + } - /** - * Get the {@code EnumValueOptions}, defined in {@code descriptor.proto}. - */ - public EnumValueOptions getOptions() { return proto.getOptions(); } + /** Get the {@code EnumValueOptions}, defined in {@code descriptor.proto}. */ + public EnumValueOptions getOptions() { + return proto.getOptions(); + } private final int index; private EnumValueDescriptorProto proto; @@ -1756,11 +1837,12 @@ public final class Descriptors { private final FileDescriptor file; private final EnumDescriptor type; - private EnumValueDescriptor(final EnumValueDescriptorProto proto, - final FileDescriptor file, - final EnumDescriptor parent, - final int index) - throws DescriptorValidationException { + private EnumValueDescriptor( + final EnumValueDescriptorProto proto, + final FileDescriptor file, + final EnumDescriptor parent, + final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; this.file = file; @@ -1772,21 +1854,17 @@ public final class Descriptors { file.pool.addEnumValueByNumber(this); } - private Integer number; // Create an unknown enum value. private EnumValueDescriptor( - final FileDescriptor file, - final EnumDescriptor parent, - final Integer number) { + final FileDescriptor file, final EnumDescriptor parent, final Integer number) { String name = "UNKNOWN_ENUM_VALUE_" + parent.getName() + "_" + number; - EnumValueDescriptorProto proto = EnumValueDescriptorProto - .newBuilder().setName(name).setNumber(number).build(); + EnumValueDescriptorProto proto = + EnumValueDescriptorProto.newBuilder().setName(name).setNumber(number).build(); this.index = -1; this.proto = proto; this.file = file; this.type = parent; this.fullName = parent.getFullName() + '.' + proto.getName(); - this.number = number; // Don't add this descriptor into pool. } @@ -1802,10 +1880,11 @@ public final class Descriptors { /** Describes a service type. */ public static final class ServiceDescriptor extends GenericDescriptor { /** - * Get the index of this descriptor within its parent. - * * @see Descriptors.Descriptor#getIndex() + * Get the index of this descriptor within its parent. * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -1821,6 +1900,7 @@ public final class Descriptors { /** * Get the type's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -1835,7 +1915,9 @@ public final class Descriptors { } /** Get the {@code ServiceOptions}, defined in {@code descriptor.proto}. */ - public ServiceOptions getOptions() { return proto.getOptions(); } + public ServiceOptions getOptions() { + return proto.getOptions(); + } /** Get a list of methods for this service. */ public List getMethods() { @@ -1844,14 +1926,14 @@ public final class Descriptors { /** * Find a method by name. + * * @param name The unqualified name of the method (e.g. "Foo"). * @return the method's descriptor, or {@code null} if not found. */ public MethodDescriptor findMethodByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof MethodDescriptor) { - return (MethodDescriptor)result; + return (MethodDescriptor) result; } else { return null; } @@ -1863,10 +1945,9 @@ public final class Descriptors { private final FileDescriptor file; private MethodDescriptor[] methods; - private ServiceDescriptor(final ServiceDescriptorProto proto, - final FileDescriptor file, - final int index) - throws DescriptorValidationException { + private ServiceDescriptor( + final ServiceDescriptorProto proto, final FileDescriptor file, final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, null, proto.getName()); @@ -1874,8 +1955,7 @@ public final class Descriptors { methods = new MethodDescriptor[proto.getMethodCount()]; for (int i = 0; i < proto.getMethodCount(); i++) { - methods[i] = new MethodDescriptor( - proto.getMethod(i), file, this, i); + methods[i] = new MethodDescriptor(proto.getMethod(i), file, this, i); } file.pool.addSymbol(this); @@ -1899,15 +1979,14 @@ public final class Descriptors { // ================================================================= - /** - * Describes one method within a service type. - */ + /** Describes one method within a service type. */ public static final class MethodDescriptor extends GenericDescriptor { /** - * Get the index of this descriptor within its parent. - * * @see Descriptors.Descriptor#getIndex() + * Get the index of this descriptor within its parent. * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -1923,6 +2002,7 @@ public final class Descriptors { /** * Get the method's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -1937,18 +2017,34 @@ public final class Descriptors { } /** Get the method's service type. */ - public ServiceDescriptor getService() { return service; } + public ServiceDescriptor getService() { + return service; + } /** Get the method's input type. */ - public Descriptor getInputType() { return inputType; } + public Descriptor getInputType() { + return inputType; + } /** Get the method's output type. */ - public Descriptor getOutputType() { return outputType; } + public Descriptor getOutputType() { + return outputType; + } - /** - * Get the {@code MethodOptions}, defined in {@code descriptor.proto}. - */ - public MethodOptions getOptions() { return proto.getOptions(); } + /** Get whether or not the inputs are streaming. */ + public boolean isClientStreaming() { + return proto.getClientStreaming(); + } + + /** Get whether or not the outputs are streaming. */ + public boolean isServerStreaming() { + return proto.getServerStreaming(); + } + + /** Get the {@code MethodOptions}, defined in {@code descriptor.proto}. */ + public MethodOptions getOptions() { + return proto.getOptions(); + } private final int index; private MethodDescriptorProto proto; @@ -1960,11 +2056,12 @@ public final class Descriptors { private Descriptor inputType; private Descriptor outputType; - private MethodDescriptor(final MethodDescriptorProto proto, - final FileDescriptor file, - final ServiceDescriptor parent, - final int index) - throws DescriptorValidationException { + private MethodDescriptor( + final MethodDescriptorProto proto, + final FileDescriptor file, + final ServiceDescriptor parent, + final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; this.file = file; @@ -1977,22 +2074,22 @@ public final class Descriptors { private void crossLink() throws DescriptorValidationException { final GenericDescriptor input = - file.pool.lookupSymbol(proto.getInputType(), this, - DescriptorPool.SearchFilter.TYPES_ONLY); + file.pool.lookupSymbol( + proto.getInputType(), this, DescriptorPool.SearchFilter.TYPES_ONLY); if (!(input instanceof Descriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getInputType() + "\" is not a message type."); + throw new DescriptorValidationException( + this, '\"' + proto.getInputType() + "\" is not a message type."); } - inputType = (Descriptor)input; + inputType = (Descriptor) input; final GenericDescriptor output = - file.pool.lookupSymbol(proto.getOutputType(), this, - DescriptorPool.SearchFilter.TYPES_ONLY); + file.pool.lookupSymbol( + proto.getOutputType(), this, DescriptorPool.SearchFilter.TYPES_ONLY); if (!(output instanceof Descriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getOutputType() + "\" is not a message type."); + throw new DescriptorValidationException( + this, '\"' + proto.getOutputType() + "\" is not a message type."); } - outputType = (Descriptor)output; + outputType = (Descriptor) output; } /** See {@link FileDescriptor#setProto}. */ @@ -2003,62 +2100,65 @@ public final class Descriptors { // ================================================================= - private static String computeFullName(final FileDescriptor file, - final Descriptor parent, - final String name) { + private static String computeFullName( + final FileDescriptor file, final Descriptor parent, final String name) { if (parent != null) { return parent.getFullName() + '.' + name; - } else if (file.getPackage().length() > 0) { - return file.getPackage() + '.' + name; - } else { - return name; } + + final String packageName = file.getPackage(); + if (!packageName.isEmpty()) { + return packageName + '.' + name; + } + + return name; } // ================================================================= /** - * All descriptors implement this to make it easier to implement tools like - * {@code DescriptorPool}.

- * - * This class is public so that the methods it exposes can be called from - * outside of this package. However, it should only be subclassed from - * nested classes of Descriptors. + * All descriptors implement this to make it easier to implement tools like {@code + * DescriptorPool}. */ public abstract static class GenericDescriptor { + + // Private constructor to prevent subclasses outside of com.google.protobuf.Descriptors + private GenericDescriptor() {} + public abstract Message toProto(); + public abstract String getName(); + public abstract String getFullName(); + public abstract FileDescriptor getFile(); } - /** - * Thrown when building descriptors fails because the source DescriptorProtos - * are not valid. - */ + /** Thrown when building descriptors fails because the source DescriptorProtos are not valid. */ public static class DescriptorValidationException extends Exception { private static final long serialVersionUID = 5750205775490483148L; /** Gets the full name of the descriptor where the error occurred. */ - public String getProblemSymbolName() { return name; } + public String getProblemSymbolName() { + return name; + } - /** - * Gets the protocol message representation of the invalid descriptor. - */ - public Message getProblemProto() { return proto; } + /** Gets the protocol message representation of the invalid descriptor. */ + public Message getProblemProto() { + return proto; + } - /** - * Gets a human-readable description of the error. - */ - public String getDescription() { return description; } + /** Gets a human-readable description of the error. */ + public String getDescription() { + return description; + } private final String name; private final Message proto; private final String description; private DescriptorValidationException( - final GenericDescriptor problemDescriptor, - final String description) { + final GenericDescriptor problemDescriptor, final String description) { super(problemDescriptor.getFullName() + ": " + description); // Note that problemDescriptor may be partially uninitialized, so we @@ -2078,8 +2178,7 @@ public final class Descriptors { } private DescriptorValidationException( - final FileDescriptor problemDescriptor, - final String description) { + final FileDescriptor problemDescriptor, final String description) { super(problemDescriptor.getName() + ": " + description); // Note that problemDescriptor may be partially uninitialized, so we @@ -2094,19 +2193,19 @@ public final class Descriptors { // ================================================================= /** - * A private helper class which contains lookup tables containing all the - * descriptors defined in a particular file. + * A private helper class which contains lookup tables containing all the descriptors defined in a + * particular file. */ private static final class DescriptorPool { - /** Defines what subclass of descriptors to search in the descriptor pool. - */ + /** Defines what subclass of descriptors to search in the descriptor pool. */ enum SearchFilter { - TYPES_ONLY, AGGREGATES_ONLY, ALL_SYMBOLS + TYPES_ONLY, + AGGREGATES_ONLY, + ALL_SYMBOLS } - DescriptorPool(final FileDescriptor[] dependencies, - boolean allowUnknownDependencies) { + DescriptorPool(final FileDescriptor[] dependencies, boolean allowUnknownDependencies) { this.dependencies = new HashSet(); this.allowUnknownDependencies = allowUnknownDependencies; @@ -2127,7 +2226,7 @@ public final class Descriptors { } } - /** Find and put public dependencies of the file into dependencies set.*/ + /** Find and put public dependencies of the file into dependencies set. */ private void importPublicDependencies(final FileDescriptor file) { for (FileDescriptor dependency : file.getPublicDependencies()) { if (dependencies.add(dependency)) { @@ -2140,27 +2239,27 @@ public final class Descriptors { private boolean allowUnknownDependencies; private final Map descriptorsByName = - new HashMap(); + new HashMap(); private final Map fieldsByNumber = - new HashMap(); - private final Map enumValuesByNumber - = new HashMap(); + new HashMap(); + private final Map enumValuesByNumber = + new HashMap(); /** Find a generic descriptor by fully-qualified name. */ GenericDescriptor findSymbol(final String fullName) { return findSymbol(fullName, SearchFilter.ALL_SYMBOLS); } - /** Find a descriptor by fully-qualified name and given option to only - * search valid field type descriptors. + /** + * Find a descriptor by fully-qualified name and given option to only search valid field type + * descriptors. */ - GenericDescriptor findSymbol(final String fullName, - final SearchFilter filter) { + GenericDescriptor findSymbol(final String fullName, final SearchFilter filter) { GenericDescriptor result = descriptorsByName.get(fullName); if (result != null) { - if ((filter==SearchFilter.ALL_SYMBOLS) || - ((filter==SearchFilter.TYPES_ONLY) && isType(result)) || - ((filter==SearchFilter.AGGREGATES_ONLY) && isAggregate(result))) { + if ((filter == SearchFilter.ALL_SYMBOLS) + || ((filter == SearchFilter.TYPES_ONLY) && isType(result)) + || ((filter == SearchFilter.AGGREGATES_ONLY) && isAggregate(result))) { return result; } } @@ -2168,9 +2267,9 @@ public final class Descriptors { for (final FileDescriptor dependency : dependencies) { result = dependency.pool.descriptorsByName.get(fullName); if (result != null) { - if ((filter==SearchFilter.ALL_SYMBOLS) || - ((filter==SearchFilter.TYPES_ONLY) && isType(result)) || - ((filter==SearchFilter.AGGREGATES_ONLY) && isAggregate(result))) { + if ((filter == SearchFilter.ALL_SYMBOLS) + || ((filter == SearchFilter.TYPES_ONLY) && isType(result)) + || ((filter == SearchFilter.AGGREGATES_ONLY) && isAggregate(result))) { return result; } } @@ -2181,28 +2280,27 @@ public final class Descriptors { /** Checks if the descriptor is a valid type for a message field. */ boolean isType(GenericDescriptor descriptor) { - return (descriptor instanceof Descriptor) || - (descriptor instanceof EnumDescriptor); + return (descriptor instanceof Descriptor) || (descriptor instanceof EnumDescriptor); } /** Checks if the descriptor is a valid namespace type. */ boolean isAggregate(GenericDescriptor descriptor) { - return (descriptor instanceof Descriptor) || - (descriptor instanceof EnumDescriptor) || - (descriptor instanceof PackageDescriptor) || - (descriptor instanceof ServiceDescriptor); + return (descriptor instanceof Descriptor) + || (descriptor instanceof EnumDescriptor) + || (descriptor instanceof PackageDescriptor) + || (descriptor instanceof ServiceDescriptor); } /** - * Look up a type descriptor by name, relative to some other descriptor. - * The name may be fully-qualified (with a leading '.'), - * partially-qualified, or unqualified. C++-like name lookup semantics - * are used to search for the matching descriptor. + * Look up a type descriptor by name, relative to some other descriptor. The name may be + * fully-qualified (with a leading '.'), partially-qualified, or unqualified. C++-like name + * lookup semantics are used to search for the matching descriptor. */ - GenericDescriptor lookupSymbol(final String name, - final GenericDescriptor relativeTo, - final DescriptorPool.SearchFilter filter) - throws DescriptorValidationException { + GenericDescriptor lookupSymbol( + final String name, + final GenericDescriptor relativeTo, + final DescriptorPool.SearchFilter filter) + throws DescriptorValidationException { // TODO(kenton): This could be optimized in a number of ways. GenericDescriptor result; @@ -2235,8 +2333,7 @@ public final class Descriptors { // We will search each parent scope of "relativeTo" looking for the // symbol. - final StringBuilder scopeToTry = - new StringBuilder(relativeTo.getFullName()); + final StringBuilder scopeToTry = new StringBuilder(relativeTo.getFullName()); while (true) { // Chop off the last component of the scope. @@ -2250,8 +2347,7 @@ public final class Descriptors { // Append firstPart and try to find scopeToTry.append(firstPart); - result = findSymbol(scopeToTry.toString(), - DescriptorPool.SearchFilter.AGGREGATES_ONLY); + result = findSymbol(scopeToTry.toString(), DescriptorPool.SearchFilter.AGGREGATES_ONLY); if (result != null) { if (firstPartLength != -1) { @@ -2274,22 +2370,23 @@ public final class Descriptors { if (result == null) { if (allowUnknownDependencies && filter == SearchFilter.TYPES_ONLY) { - logger.warning("The descriptor for message type \"" + name + - "\" can not be found and a placeholder is created for it"); + logger.warning( + "The descriptor for message type \"" + + name + + "\" can not be found and a placeholder is created for it"); // We create a dummy message descriptor here regardless of the // expected type. If the type should be message, this dummy // descriptor will work well and if the type should be enum, a // DescriptorValidationException will be thrown latter. In either // case, the code works as expected: we allow unknown message types - // but not unknwon enum types. + // but not unknown enum types. result = new Descriptor(fullname); // Add the placeholder file as a dependency so we can find the // placeholder symbol when resolving other references. this.dependencies.add(result.getFile()); return result; } else { - throw new DescriptorValidationException(relativeTo, - '\"' + name + "\" is not defined."); + throw new DescriptorValidationException(relativeTo, '\"' + name + "\" is not defined."); } } else { return result; @@ -2297,63 +2394,71 @@ public final class Descriptors { } /** - * Adds a symbol to the symbol table. If a symbol with the same name - * already exists, throws an error. + * Adds a symbol to the symbol table. If a symbol with the same name already exists, throws an + * error. */ - void addSymbol(final GenericDescriptor descriptor) - throws DescriptorValidationException { + void addSymbol(final GenericDescriptor descriptor) throws DescriptorValidationException { validateSymbolName(descriptor); final String fullName = descriptor.getFullName(); - final int dotpos = fullName.lastIndexOf('.'); final GenericDescriptor old = descriptorsByName.put(fullName, descriptor); if (old != null) { descriptorsByName.put(fullName, old); if (descriptor.getFile() == old.getFile()) { + final int dotpos = fullName.lastIndexOf('.'); if (dotpos == -1) { - throw new DescriptorValidationException(descriptor, - '\"' + fullName + "\" is already defined."); + throw new DescriptorValidationException( + descriptor, '\"' + fullName + "\" is already defined."); } else { - throw new DescriptorValidationException(descriptor, - '\"' + fullName.substring(dotpos + 1) + - "\" is already defined in \"" + - fullName.substring(0, dotpos) + "\"."); + throw new DescriptorValidationException( + descriptor, + '\"' + + fullName.substring(dotpos + 1) + + "\" is already defined in \"" + + fullName.substring(0, dotpos) + + "\"."); } } else { - throw new DescriptorValidationException(descriptor, - '\"' + fullName + "\" is already defined in file \"" + - old.getFile().getName() + "\"."); + throw new DescriptorValidationException( + descriptor, + '\"' + + fullName + + "\" is already defined in file \"" + + old.getFile().getName() + + "\"."); } } } /** - * Represents a package in the symbol table. We use PackageDescriptors - * just as placeholders so that someone cannot define, say, a message type - * that has the same name as an existing package. + * Represents a package in the symbol table. We use PackageDescriptors just as placeholders so + * that someone cannot define, say, a message type that has the same name as an existing + * package. */ private static final class PackageDescriptor extends GenericDescriptor { @Override public Message toProto() { return file.toProto(); } + @Override public String getName() { return name; } + @Override public String getFullName() { return fullName; } + @Override public FileDescriptor getFile() { return file; } - PackageDescriptor(final String name, final String fullName, - final FileDescriptor file) { + PackageDescriptor(final String name, final String fullName, final FileDescriptor file) { this.file = file; this.fullName = fullName; this.name = name; @@ -2365,13 +2470,12 @@ public final class Descriptors { } /** - * Adds a package to the symbol tables. If a package by the same name - * already exists, that is fine, but if some other kind of symbol exists - * under the same name, an exception is thrown. If the package has - * multiple components, this also adds the parent package(s). + * Adds a package to the symbol tables. If a package by the same name already exists, that is + * fine, but if some other kind of symbol exists under the same name, an exception is thrown. If + * the package has multiple components, this also adds the parent package(s). */ void addPackage(final String fullName, final FileDescriptor file) - throws DescriptorValidationException { + throws DescriptorValidationException { final int dotpos = fullName.lastIndexOf('.'); final String name; if (dotpos == -1) { @@ -2382,14 +2486,18 @@ public final class Descriptors { } final GenericDescriptor old = - descriptorsByName.put(fullName, - new PackageDescriptor(name, fullName, file)); + descriptorsByName.put(fullName, new PackageDescriptor(name, fullName, file)); if (old != null) { descriptorsByName.put(fullName, old); if (!(old instanceof PackageDescriptor)) { - throw new DescriptorValidationException(file, - '\"' + name + "\" is already defined (as something other than a " - + "package) in file \"" + old.getFile().getName() + "\"."); + throw new DescriptorValidationException( + file, + '\"' + + name + + "\" is already defined (as something other than a " + + "package) in file \"" + + old.getFile().getName() + + "\"."); } } } @@ -2408,43 +2516,46 @@ public final class Descriptors { public int hashCode() { return descriptor.hashCode() * ((1 << 16) - 1) + number; } + @Override public boolean equals(final Object obj) { if (!(obj instanceof DescriptorIntPair)) { return false; } - final DescriptorIntPair other = (DescriptorIntPair)obj; + final DescriptorIntPair other = (DescriptorIntPair) obj; return descriptor == other.descriptor && number == other.number; } } /** - * Adds a field to the fieldsByNumber table. Throws an exception if a - * field with the same containing type and number already exists. + * Adds a field to the fieldsByNumber table. Throws an exception if a field with the same + * containing type and number already exists. */ - void addFieldByNumber(final FieldDescriptor field) - throws DescriptorValidationException { + void addFieldByNumber(final FieldDescriptor field) throws DescriptorValidationException { final DescriptorIntPair key = - new DescriptorIntPair(field.getContainingType(), field.getNumber()); + new DescriptorIntPair(field.getContainingType(), field.getNumber()); final FieldDescriptor old = fieldsByNumber.put(key, field); if (old != null) { fieldsByNumber.put(key, old); - throw new DescriptorValidationException(field, - "Field number " + field.getNumber() + - " has already been used in \"" + - field.getContainingType().getFullName() + - "\" by field \"" + old.getName() + "\"."); + throw new DescriptorValidationException( + field, + "Field number " + + field.getNumber() + + " has already been used in \"" + + field.getContainingType().getFullName() + + "\" by field \"" + + old.getName() + + "\"."); } } /** - * Adds an enum value to the enumValuesByNumber table. If an enum value - * with the same type and number already exists, does nothing. (This is - * allowed; the first value define with the number takes precedence.) + * Adds an enum value to the enumValuesByNumber table. If an enum value with the same type and + * number already exists, does nothing. (This is allowed; the first value define with the number + * takes precedence.) */ void addEnumValueByNumber(final EnumValueDescriptor value) { - final DescriptorIntPair key = - new DescriptorIntPair(value.getType(), value.getNumber()); + final DescriptorIntPair key = new DescriptorIntPair(value.getType(), value.getNumber()); final EnumValueDescriptor old = enumValuesByNumber.put(key, value); if (old != null) { enumValuesByNumber.put(key, old); @@ -2454,54 +2565,63 @@ public final class Descriptors { } /** - * Verifies that the descriptor's name is valid (i.e. it contains only - * letters, digits, and underscores, and does not start with a digit). + * Verifies that the descriptor's name is valid (i.e. it contains only letters, digits, and + * underscores, and does not start with a digit). */ static void validateSymbolName(final GenericDescriptor descriptor) - throws DescriptorValidationException { + throws DescriptorValidationException { final String name = descriptor.getName(); if (name.length() == 0) { throw new DescriptorValidationException(descriptor, "Missing name."); - } else { - boolean valid = true; - for (int i = 0; i < name.length(); i++) { - final char c = name.charAt(i); - // Non-ASCII characters are not valid in protobuf identifiers, even - // if they are letters or digits. - if (c >= 128) { - valid = false; - } - // First character must be letter or _. Subsequent characters may - // be letters, numbers, or digits. - if (Character.isLetter(c) || c == '_' || - (Character.isDigit(c) && i > 0)) { - // Valid - } else { - valid = false; - } - } - if (!valid) { - throw new DescriptorValidationException(descriptor, - '\"' + name + "\" is not a valid identifier."); + } + + // Non-ASCII characters are not valid in protobuf identifiers, even + // if they are letters or digits. + // The first character must be a letter or '_'. + // Subsequent characters may be letters, numbers, or digits. + for (int i = 0; i < name.length(); i++) { + final char c = name.charAt(i); + if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') + || (c == '_') + || ('0' <= c && c <= '9' && i > 0)) { + // Valid + continue; } + throw new DescriptorValidationException( + descriptor, '\"' + name + "\" is not a valid identifier."); } } } /** Describes an oneof of a message type. */ - public static final class OneofDescriptor { + public static final class OneofDescriptor extends GenericDescriptor { /** Get the index of this descriptor within its parent. */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } - public String getName() { return proto.getName(); } + @Override + public String getName() { + return proto.getName(); + } - public FileDescriptor getFile() { return file; } + @Override + public FileDescriptor getFile() { + return file; + } - public String getFullName() { return fullName; } + @Override + public String getFullName() { + return fullName; + } - public Descriptor getContainingType() { return containingType; } + public Descriptor getContainingType() { + return containingType; + } - public int getFieldCount() { return fieldCount; } + public int getFieldCount() { + return fieldCount; + } public OneofOptions getOptions() { return proto.getOptions(); @@ -2516,15 +2636,21 @@ public final class Descriptors { return fields[index]; } + @Override + public OneofDescriptorProto toProto() { + return proto; + } + private void setProto(final OneofDescriptorProto proto) { this.proto = proto; } - private OneofDescriptor(final OneofDescriptorProto proto, - final FileDescriptor file, - final Descriptor parent, - final int index) - throws DescriptorValidationException { + private OneofDescriptor( + final OneofDescriptorProto proto, + final FileDescriptor file, + final Descriptor parent, + final int index) + throws DescriptorValidationException { this.proto = proto; fullName = computeFullName(file, parent, proto.getName()); this.file = file; diff --git a/java/core/src/main/java/com/google/protobuf/DiscardUnknownFieldsParser.java b/java/core/src/main/java/com/google/protobuf/DiscardUnknownFieldsParser.java index 7ae9434906..c2378adce8 100644 --- a/java/core/src/main/java/com/google/protobuf/DiscardUnknownFieldsParser.java +++ b/java/core/src/main/java/com/google/protobuf/DiscardUnknownFieldsParser.java @@ -30,21 +30,20 @@ package com.google.protobuf; -/** - * Parsers to discard unknown fields during parsing. - */ +/** Parsers to discard unknown fields during parsing. */ public final class DiscardUnknownFieldsParser { /** - * Warps a given {@link Parser} into a new {@link Parser} that discards unknown fields during + * Wraps a given {@link Parser} into a new {@link Parser} that discards unknown fields during * parsing. * *

Usage example: + * *

{@code
-     * private final static Parser FOO_PARSER = DiscardUnknownFieldsParser.wrap(Foo.parser());
-     * Foo parseFooDiscardUnknown(ByteBuffer input) throws IOException {
-     *   return FOO_PARSER.parseFrom(input);
-     * }
+   * private final static Parser FOO_PARSER = DiscardUnknownFieldsParser.wrap(Foo.parser());
+   * Foo parseFooDiscardUnknown(ByteBuffer input) throws IOException {
+   *   return FOO_PARSER.parseFrom(input);
+   * }
    * }
* *

Like all other implementations of {@code Parser}, this parser is stateless and thread-safe. diff --git a/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java b/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java index 8d987b2e33..ac14949e6f 100644 --- a/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java @@ -42,11 +42,10 @@ import java.util.RandomAccess; * * @author dweis@google.com (Daniel Weis) */ -final class DoubleArrayList - extends AbstractProtobufList +final class DoubleArrayList extends AbstractProtobufList implements DoubleList, RandomAccess, PrimitiveNonBoxingCollection { - private static final DoubleArrayList EMPTY_LIST = new DoubleArrayList(); + private static final DoubleArrayList EMPTY_LIST = new DoubleArrayList(new double[0], 0); static { EMPTY_LIST.makeImmutable(); } @@ -55,9 +54,7 @@ final class DoubleArrayList return EMPTY_LIST; } - /** - * The backing store for the list. - */ + /** The backing store for the list. */ private double[] array; /** @@ -66,16 +63,13 @@ final class DoubleArrayList */ private int size; - /** - * Constructs a new mutable {@code DoubleArrayList} with default capacity. - */ + /** Constructs a new mutable {@code DoubleArrayList} with default capacity. */ DoubleArrayList() { this(new double[DEFAULT_CAPACITY], 0); } /** - * Constructs a new mutable {@code DoubleArrayList} - * containing the same elements as {@code other}. + * Constructs a new mutable {@code DoubleArrayList} containing the same elements as {@code other}. */ private DoubleArrayList(double[] other, int size) { array = other; @@ -109,7 +103,7 @@ final class DoubleArrayList final double[] arr = other.array; for (int i = 0; i < size; i++) { - if (array[i] != arr[i]) { + if (Double.doubleToLongBits(array[i]) != Double.doubleToLongBits(arr[i])) { return false; } } @@ -165,22 +159,34 @@ final class DoubleArrayList return previousValue; } + @Override + public boolean add(Double element) { + addDouble(element); + return true; + } + @Override public void add(int index, Double element) { addDouble(index, element); } - /** - * Like {@link #add(Double)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Double)} but more efficient in that it doesn't box the element. */ @Override public void addDouble(double element) { - addDouble(size, element); + ensureIsMutable(); + if (size == array.length) { + // Resize to 1.5x the size + int length = ((size * 3) / 2) + 1; + double[] newArray = new double[length]; + + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + + array[size++] = element; } - /** - * Like {@link #add(int, Double)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(int, Double)} but more efficient in that it doesn't box the element. */ private void addDouble(int index, double element) { ensureIsMutable(); if (index < 0 || index > size) { @@ -246,7 +252,7 @@ final class DoubleArrayList ensureIsMutable(); for (int i = 0; i < size; i++) { if (o.equals(array[i])) { - System.arraycopy(array, i + 1, array, i, size - i); + System.arraycopy(array, i + 1, array, i, size - i - 1); size--; modCount++; return true; @@ -261,7 +267,7 @@ final class DoubleArrayList ensureIndexInRange(index); double value = array[index]; if (index < size - 1) { - System.arraycopy(array, index + 1, array, index, size - index); + System.arraycopy(array, index + 1, array, index, size - index - 1); } size--; modCount++; diff --git a/java/core/src/main/java/com/google/protobuf/DynamicMessage.java b/java/core/src/main/java/com/google/protobuf/DynamicMessage.java index 2c346e031c..63dda6b9f2 100644 --- a/java/core/src/main/java/com/google/protobuf/DynamicMessage.java +++ b/java/core/src/main/java/com/google/protobuf/DynamicMessage.java @@ -43,8 +43,8 @@ import java.util.List; import java.util.Map; /** - * An implementation of {@link Message} that can represent arbitrary types, - * given a {@link Descriptors.Descriptor}. + * An implementation of {@link Message} that can represent arbitrary types, given a {@link + * Descriptors.Descriptor}. * * @author kenton@google.com Kenton Varda */ @@ -56,88 +56,83 @@ public final class DynamicMessage extends AbstractMessage { private int memoizedSize = -1; /** - * Construct a {@code DynamicMessage} using the given {@code FieldSet}. - * oneofCases stores the FieldDescriptor for each oneof to indicate - * which field is set. Caller should make sure the array is immutable. + * Construct a {@code DynamicMessage} using the given {@code FieldSet}. oneofCases stores the + * FieldDescriptor for each oneof to indicate which field is set. Caller should make sure the + * array is immutable. * - * This constructor is package private and will be used in - * {@code DynamicMutableMessage} to convert a mutable message to an immutable - * message. + *

This constructor is package private and will be used in {@code DynamicMutableMessage} to + * convert a mutable message to an immutable message. */ - DynamicMessage(Descriptor type, FieldSet fields, - FieldDescriptor[] oneofCases, - UnknownFieldSet unknownFields) { + DynamicMessage( + Descriptor type, + FieldSet fields, + FieldDescriptor[] oneofCases, + UnknownFieldSet unknownFields) { this.type = type; this.fields = fields; this.oneofCases = oneofCases; this.unknownFields = unknownFields; } - /** - * Get a {@code DynamicMessage} representing the default instance of the - * given type. - */ + /** Get a {@code DynamicMessage} representing the default instance of the given type. */ public static DynamicMessage getDefaultInstance(Descriptor type) { int oneofDeclCount = type.toProto().getOneofDeclCount(); FieldDescriptor[] oneofCases = new FieldDescriptor[oneofDeclCount]; - return new DynamicMessage(type, FieldSet.emptySet(), - oneofCases, - UnknownFieldSet.getDefaultInstance()); + return new DynamicMessage( + type, + FieldSet.emptySet(), + oneofCases, + UnknownFieldSet.getDefaultInstance()); } /** Parse a message of the given type from the given input stream. */ - public static DynamicMessage parseFrom(Descriptor type, - CodedInputStream input) - throws IOException { + public static DynamicMessage parseFrom(Descriptor type, CodedInputStream input) + throws IOException { return newBuilder(type).mergeFrom(input).buildParsed(); } /** Parse a message of the given type from the given input stream. */ public static DynamicMessage parseFrom( - Descriptor type, - CodedInputStream input, - ExtensionRegistry extensionRegistry) + Descriptor type, CodedInputStream input, ExtensionRegistry extensionRegistry) throws IOException { return newBuilder(type).mergeFrom(input, extensionRegistry).buildParsed(); } /** Parse {@code data} as a message of the given type and return it. */ public static DynamicMessage parseFrom(Descriptor type, ByteString data) - throws InvalidProtocolBufferException { + throws InvalidProtocolBufferException { return newBuilder(type).mergeFrom(data).buildParsed(); } /** Parse {@code data} as a message of the given type and return it. */ - public static DynamicMessage parseFrom(Descriptor type, ByteString data, - ExtensionRegistry extensionRegistry) - throws InvalidProtocolBufferException { + public static DynamicMessage parseFrom( + Descriptor type, ByteString data, ExtensionRegistry extensionRegistry) + throws InvalidProtocolBufferException { return newBuilder(type).mergeFrom(data, extensionRegistry).buildParsed(); } /** Parse {@code data} as a message of the given type and return it. */ public static DynamicMessage parseFrom(Descriptor type, byte[] data) - throws InvalidProtocolBufferException { + throws InvalidProtocolBufferException { return newBuilder(type).mergeFrom(data).buildParsed(); } /** Parse {@code data} as a message of the given type and return it. */ - public static DynamicMessage parseFrom(Descriptor type, byte[] data, - ExtensionRegistry extensionRegistry) - throws InvalidProtocolBufferException { + public static DynamicMessage parseFrom( + Descriptor type, byte[] data, ExtensionRegistry extensionRegistry) + throws InvalidProtocolBufferException { return newBuilder(type).mergeFrom(data, extensionRegistry).buildParsed(); } /** Parse a message of the given type from {@code input} and return it. */ - public static DynamicMessage parseFrom(Descriptor type, InputStream input) - throws IOException { + public static DynamicMessage parseFrom(Descriptor type, InputStream input) throws IOException { return newBuilder(type).mergeFrom(input).buildParsed(); } /** Parse a message of the given type from {@code input} and return it. */ - public static DynamicMessage parseFrom(Descriptor type, InputStream input, - ExtensionRegistry extensionRegistry) - throws IOException { + public static DynamicMessage parseFrom( + Descriptor type, InputStream input, ExtensionRegistry extensionRegistry) throws IOException { return newBuilder(type).mergeFrom(input, extensionRegistry).buildParsed(); } @@ -147,8 +142,8 @@ public final class DynamicMessage extends AbstractMessage { } /** - * Construct a {@link Message.Builder} for a message of the same type as - * {@code prototype}, and initialize it with {@code prototype}'s contents. + * Construct a {@link Message.Builder} for a message of the same type as {@code prototype}, and + * initialize it with {@code prototype}'s contents. */ public static Builder newBuilder(Message prototype) { return new Builder(prototype.getDescriptorForType()).mergeFrom(prototype); @@ -227,8 +222,7 @@ public final class DynamicMessage extends AbstractMessage { return unknownFields; } - static boolean isInitialized(Descriptor type, - FieldSet fields) { + static boolean isInitialized(Descriptor type, FieldSet fields) { // Check that all required fields are present. for (final FieldDescriptor field : type.getFields()) { if (field.isRequired()) { @@ -298,8 +292,7 @@ public final class DynamicMessage extends AbstractMessage { } catch (InvalidProtocolBufferException e) { throw e.setUnfinishedMessage(builder.buildPartial()); } catch (IOException e) { - throw new InvalidProtocolBufferException(e) - .setUnfinishedMessage(builder.buildPartial()); + throw new InvalidProtocolBufferException(e).setUnfinishedMessage(builder.buildPartial()); } return builder.buildPartial(); } @@ -309,24 +302,20 @@ public final class DynamicMessage extends AbstractMessage { /** Verifies that the field is a field of this message. */ private void verifyContainingType(FieldDescriptor field) { if (field.getContainingType() != type) { - throw new IllegalArgumentException( - "FieldDescriptor does not match message type."); + throw new IllegalArgumentException("FieldDescriptor does not match message type."); } } /** Verifies that the oneof is an oneof of this message. */ private void verifyOneofContainingType(OneofDescriptor oneof) { if (oneof.getContainingType() != type) { - throw new IllegalArgumentException( - "OneofDescriptor does not match message type."); + throw new IllegalArgumentException("OneofDescriptor does not match message type."); } } // ================================================================= - /** - * Builder for {@link DynamicMessage}s. - */ + /** Builder for {@link DynamicMessage}s. */ public static final class Builder extends AbstractMessage.Builder { private final Descriptor type; private FieldSet fields; @@ -380,7 +369,7 @@ public final class DynamicMessage extends AbstractMessage { DynamicMessage otherDynamicMessage = (DynamicMessage) other; if (otherDynamicMessage.type != type) { throw new IllegalArgumentException( - "mergeFrom(Message) can only merge messages of the same type."); + "mergeFrom(Message) can only merge messages of the same type."); } ensureIsMutable(); fields.mergeFrom(otherDynamicMessage.fields); @@ -406,23 +395,28 @@ public final class DynamicMessage extends AbstractMessage { public DynamicMessage build() { if (!isInitialized()) { throw newUninitializedMessageException( - new DynamicMessage(type, fields, - java.util.Arrays.copyOf(oneofCases, oneofCases.length), unknownFields)); + new DynamicMessage( + type, + fields, + java.util.Arrays.copyOf(oneofCases, oneofCases.length), + unknownFields)); } return buildPartial(); } /** - * Helper for DynamicMessage.parseFrom() methods to call. Throws - * {@link InvalidProtocolBufferException} instead of - * {@link UninitializedMessageException}. + * Helper for DynamicMessage.parseFrom() methods to call. Throws {@link + * InvalidProtocolBufferException} instead of {@link UninitializedMessageException}. */ private DynamicMessage buildParsed() throws InvalidProtocolBufferException { if (!isInitialized()) { throw newUninitializedMessageException( - new DynamicMessage(type, fields, - java.util.Arrays.copyOf(oneofCases, oneofCases.length), unknownFields)) - .asInvalidProtocolBufferException(); + new DynamicMessage( + type, + fields, + java.util.Arrays.copyOf(oneofCases, oneofCases.length), + unknownFields)) + .asInvalidProtocolBufferException(); } return buildPartial(); } @@ -431,8 +425,8 @@ public final class DynamicMessage extends AbstractMessage { public DynamicMessage buildPartial() { fields.makeImmutable(); DynamicMessage result = - new DynamicMessage(type, fields, - java.util.Arrays.copyOf(oneofCases, oneofCases.length), unknownFields); + new DynamicMessage( + type, fields, java.util.Arrays.copyOf(oneofCases, oneofCases.length), unknownFields); return result; } @@ -441,7 +435,7 @@ public final class DynamicMessage extends AbstractMessage { Builder result = new Builder(type); result.fields.mergeFrom(fields); result.mergeUnknownFields(unknownFields); - System.arraycopy(oneofCases, 0, result.oneofCases, 0 , oneofCases.length); + System.arraycopy(oneofCases, 0, result.oneofCases, 0, oneofCases.length); return result; } @@ -471,7 +465,7 @@ public final class DynamicMessage extends AbstractMessage { if (field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new IllegalArgumentException( - "newBuilderForField is only valid for fields with message type."); + "newBuilderForField is only valid for fields with message type."); } return new Builder(field.getMessageType()); @@ -615,35 +609,30 @@ public final class DynamicMessage extends AbstractMessage { @Override public Builder mergeUnknownFields(UnknownFieldSet unknownFields) { this.unknownFields = - UnknownFieldSet.newBuilder(this.unknownFields) - .mergeFrom(unknownFields) - .build(); + UnknownFieldSet.newBuilder(this.unknownFields).mergeFrom(unknownFields).build(); return this; } /** Verifies that the field is a field of this message. */ private void verifyContainingType(FieldDescriptor field) { if (field.getContainingType() != type) { - throw new IllegalArgumentException( - "FieldDescriptor does not match message type."); + throw new IllegalArgumentException("FieldDescriptor does not match message type."); } } /** Verifies that the oneof is an oneof of this message. */ private void verifyOneofContainingType(OneofDescriptor oneof) { if (oneof.getContainingType() != type) { - throw new IllegalArgumentException( - "OneofDescriptor does not match message type."); + throw new IllegalArgumentException("OneofDescriptor does not match message type."); } } /** Verifies that the value is EnumValueDescriptor and matches Enum Type. */ - private void ensureSingularEnumValueDescriptor( - FieldDescriptor field, Object value) { + private void ensureSingularEnumValueDescriptor(FieldDescriptor field, Object value) { checkNotNull(value); if (!(value instanceof EnumValueDescriptor)) { throw new IllegalArgumentException( - "DynamicMessage should use EnumValueDescriptor to set Enum Value."); + "DynamicMessage should use EnumValueDescriptor to set Enum Value."); } // TODO(xiaofeng): Re-enable this check after Orgstore is fixed to not // set incorrect EnumValueDescriptors. @@ -657,14 +646,13 @@ public final class DynamicMessage extends AbstractMessage { } /** Verifies the value for an enum field. */ - private void ensureEnumValueDescriptor( - FieldDescriptor field, Object value) { + private void ensureEnumValueDescriptor(FieldDescriptor field, Object value) { if (field.isRepeated()) { for (Object item : (List) value) { ensureSingularEnumValueDescriptor(field, item); } } else { - ensureSingularEnumValueDescriptor(field, value); + ensureSingularEnumValueDescriptor(field, value); } } @@ -678,14 +666,14 @@ public final class DynamicMessage extends AbstractMessage { public com.google.protobuf.Message.Builder getFieldBuilder(FieldDescriptor field) { // TODO(xiangl): need implementation for dynamic message throw new UnsupportedOperationException( - "getFieldBuilder() called on a dynamic message type."); + "getFieldBuilder() called on a dynamic message type."); } @Override - public com.google.protobuf.Message.Builder getRepeatedFieldBuilder(FieldDescriptor field, - int index) { + public com.google.protobuf.Message.Builder getRepeatedFieldBuilder( + FieldDescriptor field, int index) { throw new UnsupportedOperationException( - "getRepeatedFieldBuilder() called on a dynamic message type."); + "getRepeatedFieldBuilder() called on a dynamic message type."); } } } diff --git a/java/core/src/main/java/com/google/protobuf/ExperimentalApi.java b/java/core/src/main/java/com/google/protobuf/ExperimentalApi.java index 3cd4c88492..d55b278c3f 100644 --- a/java/core/src/main/java/com/google/protobuf/ExperimentalApi.java +++ b/java/core/src/main/java/com/google/protobuf/ExperimentalApi.java @@ -41,26 +41,25 @@ import java.lang.annotation.Target; * backward-compatibility. * *

Usage guidelines: + * *

    - *
  1. This annotation is used only on public API. Internal interfaces should not use it.
  2. - *
  3. This annotation should only be added to new APIs. Adding it to an existing API is - * considered API-breaking.
  4. - *
  5. Removing this annotation from an API gives it stable status.
  6. + *
  7. This annotation is used only on public API. Internal interfaces should not use it. + *
  8. This annotation should only be added to new APIs. Adding it to an existing API is + * considered API-breaking. + *
  9. Removing this annotation from an API gives it stable status. *
*/ @Retention(RetentionPolicy.SOURCE) @Target({ - ElementType.ANNOTATION_TYPE, - ElementType.CONSTRUCTOR, - ElementType.FIELD, - ElementType.METHOD, - ElementType.PACKAGE, - ElementType.TYPE}) + ElementType.ANNOTATION_TYPE, + ElementType.CONSTRUCTOR, + ElementType.FIELD, + ElementType.METHOD, + ElementType.PACKAGE, + ElementType.TYPE +}) @Documented public @interface ExperimentalApi { - /** - * Context information such as links to discussion thread, tracking issue etc. - */ + /** Context information such as links to discussion thread, tracking issue etc. */ String value() default ""; } - diff --git a/java/core/src/main/java/com/google/protobuf/Extension.java b/java/core/src/main/java/com/google/protobuf/Extension.java index 5df12e6422..30c828e57b 100644 --- a/java/core/src/main/java/com/google/protobuf/Extension.java +++ b/java/core/src/main/java/com/google/protobuf/Extension.java @@ -30,6 +30,7 @@ package com.google.protobuf; +// TODO(chrisn): Change ContainingType to extend Message /** * Interface that generated extensions implement. * @@ -37,6 +38,11 @@ package com.google.protobuf; */ public abstract class Extension extends ExtensionLite { + // TODO(chrisn): Add package-private constructor. + + /** {@inheritDoc} Overridden to return {@link Message} instead of {@link MessageLite}. */ + @Override + public abstract Message getMessageDefaultInstance(); /** Returns the descriptor of the extension. */ public abstract Descriptors.FieldDescriptor getDescriptor(); @@ -49,9 +55,7 @@ public abstract class Extension // All the methods below are extension implementation details. - /** - * The API type that the extension is used for. - */ + /** The API type that the extension is used for. */ protected enum ExtensionType { IMMUTABLE, MUTABLE, @@ -60,24 +64,25 @@ public abstract class Extension protected abstract ExtensionType getExtensionType(); - /** - * Type of a message extension. - */ + /** Type of a message extension. */ public enum MessageType { PROTO1, PROTO2, } /** - * If the extension is a message extension (i.e., getLiteType() == MESSAGE), - * returns the type of the message, otherwise undefined. + * If the extension is a message extension (i.e., getLiteType() == MESSAGE), returns the type of + * the message, otherwise undefined. */ public MessageType getMessageType() { return MessageType.PROTO2; } protected abstract Object fromReflectionType(Object value); + protected abstract Object singularFromReflectionType(Object value); + protected abstract Object toReflectionType(Object value); + protected abstract Object singularToReflectionType(Object value); } diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionLite.java b/java/core/src/main/java/com/google/protobuf/ExtensionLite.java index f8f5bd2c4f..0fb5f49e98 100644 --- a/java/core/src/main/java/com/google/protobuf/ExtensionLite.java +++ b/java/core/src/main/java/com/google/protobuf/ExtensionLite.java @@ -32,9 +32,9 @@ package com.google.protobuf; /** * Lite interface that generated extensions implement. - *

- * Methods are for use by generated code only. You can hold a reference to - * extensions using this type name. + * + *

Methods are for use by generated code only. You can hold a reference to extensions using this + * type name. */ public abstract class ExtensionLite { @@ -50,12 +50,9 @@ public abstract class ExtensionLite { /** Returns the default value of the extension field. */ public abstract Type getDefaultValue(); - /** - * Returns the default instance of the extension field, if it's a message - * extension. - */ + /** Returns the default instance of the extension field, if it's a message extension. */ public abstract MessageLite getMessageDefaultInstance(); - + /** Returns whether or not this extension is a Lite Extension. */ boolean isLite() { return true; diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionRegistry.java b/java/core/src/main/java/com/google/protobuf/ExtensionRegistry.java index a22a74a03c..aeeaee53e2 100644 --- a/java/core/src/main/java/com/google/protobuf/ExtensionRegistry.java +++ b/java/core/src/main/java/com/google/protobuf/ExtensionRegistry.java @@ -40,11 +40,10 @@ import java.util.Map; import java.util.Set; /** - * A table of known extensions, searchable by name or field number. When - * parsing a protocol message that might have extensions, you must provide - * an {@code ExtensionRegistry} in which you have registered any extensions - * that you want to be able to parse. Otherwise, those extensions will just - * be treated like unknown fields. + * A table of known extensions, searchable by name or field number. When parsing a protocol message + * that might have extensions, you must provide an {@code ExtensionRegistry} in which you have + * registered any extensions that you want to be able to parse. Otherwise, those extensions will + * just be treated like unknown fields. * *

For example, if you had the {@code .proto} file: * @@ -70,25 +69,22 @@ import java.util.Set; * *

Background: * - *

You might wonder why this is necessary. Two alternatives might come to - * mind. First, you might imagine a system where generated extensions are - * automatically registered when their containing classes are loaded. This - * is a popular technique, but is bad design; among other things, it creates a - * situation where behavior can change depending on what classes happen to be - * loaded. It also introduces a security vulnerability, because an - * unprivileged class could cause its code to be called unexpectedly from a - * privileged class by registering itself as an extension of the right type. + *

You might wonder why this is necessary. Two alternatives might come to mind. First, you might + * imagine a system where generated extensions are automatically registered when their containing + * classes are loaded. This is a popular technique, but is bad design; among other things, it + * creates a situation where behavior can change depending on what classes happen to be loaded. It + * also introduces a security vulnerability, because an unprivileged class could cause its code to + * be called unexpectedly from a privileged class by registering itself as an extension of the right + * type. * - *

Another option you might consider is lazy parsing: do not parse an - * extension until it is first requested, at which point the caller must - * provide a type to use. This introduces a different set of problems. First, - * it would require a mutex lock any time an extension was accessed, which - * would be slow. Second, corrupt data would not be detected until first - * access, at which point it would be much harder to deal with it. Third, it - * could violate the expectation that message objects are immutable, since the - * type provided could be any arbitrary message class. An unprivileged user - * could take advantage of this to inject a mutable object into a message - * belonging to privileged code and create mischief. + *

Another option you might consider is lazy parsing: do not parse an extension until it is first + * requested, at which point the caller must provide a type to use. This introduces a different set + * of problems. First, it would require a mutex lock any time an extension was accessed, which would + * be slow. Second, corrupt data would not be detected until first access, at which point it would + * be much harder to deal with it. Third, it could violate the expectation that message objects are + * immutable, since the type provided could be any arbitrary message class. An unprivileged user + * could take advantage of this to inject a mutable object into a message belonging to privileged + * code and create mischief. * * @author kenton@google.com Kenton Varda */ @@ -116,8 +112,8 @@ public class ExtensionRegistry extends ExtensionRegistryLite { public final FieldDescriptor descriptor; /** - * A default instance of the extension's type, if it has a message type. - * Otherwise, {@code null}. + * A default instance of the extension's type, if it has a message type. Otherwise, {@code + * null}. */ public final Message defaultInstance; @@ -125,48 +121,41 @@ public class ExtensionRegistry extends ExtensionRegistryLite { this.descriptor = descriptor; defaultInstance = null; } - private ExtensionInfo(final FieldDescriptor descriptor, - final Message defaultInstance) { + + private ExtensionInfo(final FieldDescriptor descriptor, final Message defaultInstance) { this.descriptor = descriptor; this.defaultInstance = defaultInstance; } } - /** - * Deprecated. Use {@link #findImmutableExtensionByName(String)} instead. - */ + /** Deprecated. Use {@link #findImmutableExtensionByName(String)} instead. */ + @Deprecated public ExtensionInfo findExtensionByName(final String fullName) { return findImmutableExtensionByName(fullName); } /** - * Find an extension for immutable APIs by fully-qualified field name, - * in the proto namespace. i.e. {@code result.descriptor.fullName()} will - * match {@code fullName} if a match is found. + * Find an extension for immutable APIs by fully-qualified field name, in the proto namespace. + * i.e. {@code result.descriptor.fullName()} will match {@code fullName} if a match is found. * - * @return Information about the extension if found, or {@code null} - * otherwise. + * @return Information about the extension if found, or {@code null} otherwise. */ public ExtensionInfo findImmutableExtensionByName(final String fullName) { return immutableExtensionsByName.get(fullName); } /** - * Find an extension for mutable APIs by fully-qualified field name, - * in the proto namespace. i.e. {@code result.descriptor.fullName()} will - * match {@code fullName} if a match is found. + * Find an extension for mutable APIs by fully-qualified field name, in the proto namespace. i.e. + * {@code result.descriptor.fullName()} will match {@code fullName} if a match is found. * - * @return Information about the extension if found, or {@code null} - * otherwise. + * @return Information about the extension if found, or {@code null} otherwise. */ public ExtensionInfo findMutableExtensionByName(final String fullName) { return mutableExtensionsByName.get(fullName); } - /** - * Deprecated. Use {@link #findImmutableExtensionByNumber( - * Descriptors.Descriptor, int)} - */ + /** Deprecated. Use {@link #findImmutableExtensionByNumber( Descriptors.Descriptor, int)} */ + @Deprecated public ExtensionInfo findExtensionByNumber( final Descriptor containingType, final int fieldNumber) { return findImmutableExtensionByNumber(containingType, fieldNumber); @@ -175,34 +164,28 @@ public class ExtensionRegistry extends ExtensionRegistryLite { /** * Find an extension by containing type and field number for immutable APIs. * - * @return Information about the extension if found, or {@code null} - * otherwise. + * @return Information about the extension if found, or {@code null} otherwise. */ public ExtensionInfo findImmutableExtensionByNumber( final Descriptor containingType, final int fieldNumber) { - return immutableExtensionsByNumber.get( - new DescriptorIntPair(containingType, fieldNumber)); + return immutableExtensionsByNumber.get(new DescriptorIntPair(containingType, fieldNumber)); } /** * Find an extension by containing type and field number for mutable APIs. * - * @return Information about the extension if found, or {@code null} - * otherwise. + * @return Information about the extension if found, or {@code null} otherwise. */ public ExtensionInfo findMutableExtensionByNumber( final Descriptor containingType, final int fieldNumber) { - return mutableExtensionsByNumber.get( - new DescriptorIntPair(containingType, fieldNumber)); + return mutableExtensionsByNumber.get(new DescriptorIntPair(containingType, fieldNumber)); } /** - * Find all extensions for mutable APIs by fully-qualified name of - * extended class. Note that this method is more computationally expensive - * than getting a single extension by name or number. + * Find all extensions for mutable APIs by fully-qualified name of extended class. Note that this + * method is more computationally expensive than getting a single extension by name or number. * - * @return Information about the extensions found, or {@code null} if there - * are none. + * @return Information about the extensions found, or {@code null} if there are none. */ public Set getAllMutableExtensionsByExtendedType(final String fullName) { HashSet extensions = new HashSet(); @@ -215,12 +198,11 @@ public class ExtensionRegistry extends ExtensionRegistryLite { } /** - * Find all extensions for immutable APIs by fully-qualified name of - * extended class. Note that this method is more computationally expensive - * than getting a single extension by name or number. + * Find all extensions for immutable APIs by fully-qualified name of extended class. Note that + * this method is more computationally expensive than getting a single extension by name or + * number. * - * @return Information about the extensions found, or {@code null} if there - * are none. + * @return Information about the extensions found, or {@code null} if there are none. */ public Set getAllImmutableExtensionsByExtendedType(final String fullName) { HashSet extensions = new HashSet(); @@ -234,8 +216,8 @@ public class ExtensionRegistry extends ExtensionRegistryLite { /** Add an extension from a generated file to the registry. */ public void add(final Extension extension) { - if (extension.getExtensionType() != Extension.ExtensionType.IMMUTABLE && - extension.getExtensionType() != Extension.ExtensionType.MUTABLE) { + if (extension.getExtensionType() != Extension.ExtensionType.IMMUTABLE + && extension.getExtensionType() != Extension.ExtensionType.MUTABLE) { // do not support other extension types. ignore return; } @@ -248,15 +230,14 @@ public class ExtensionRegistry extends ExtensionRegistryLite { } static ExtensionInfo newExtensionInfo(final Extension extension) { - if (extension.getDescriptor().getJavaType() == - FieldDescriptor.JavaType.MESSAGE) { + if (extension.getDescriptor().getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (extension.getMessageDefaultInstance() == null) { throw new IllegalStateException( - "Registered message-type extension had null default instance: " + - extension.getDescriptor().getFullName()); + "Registered message-type extension had null default instance: " + + extension.getDescriptor().getFullName()); } - return new ExtensionInfo(extension.getDescriptor(), - (Message) extension.getMessageDefaultInstance()); + return new ExtensionInfo( + extension.getDescriptor(), (Message) extension.getMessageDefaultInstance()); } else { return new ExtensionInfo(extension.getDescriptor(), null); } @@ -266,8 +247,8 @@ public class ExtensionRegistry extends ExtensionRegistryLite { public void add(final FieldDescriptor type) { if (type.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { throw new IllegalArgumentException( - "ExtensionRegistry.add() must be provided a default instance when " + - "adding an embedded message extension."); + "ExtensionRegistry.add() must be provided a default instance when " + + "adding an embedded message extension."); } ExtensionInfo info = new ExtensionInfo(type, null); add(info, Extension.ExtensionType.IMMUTABLE); @@ -278,11 +259,9 @@ public class ExtensionRegistry extends ExtensionRegistryLite { public void add(final FieldDescriptor type, final Message defaultInstance) { if (type.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new IllegalArgumentException( - "ExtensionRegistry.add() provided a default instance for a " + - "non-message extension."); + "ExtensionRegistry.add() provided a default instance for a non-message extension."); } - add(new ExtensionInfo(type, defaultInstance), - Extension.ExtensionType.IMMUTABLE); + add(new ExtensionInfo(type, defaultInstance), Extension.ExtensionType.IMMUTABLE); } // ================================================================= @@ -291,22 +270,17 @@ public class ExtensionRegistry extends ExtensionRegistryLite { private ExtensionRegistry() { this.immutableExtensionsByName = new HashMap(); this.mutableExtensionsByName = new HashMap(); - this.immutableExtensionsByNumber = - new HashMap(); - this.mutableExtensionsByNumber = - new HashMap(); + this.immutableExtensionsByNumber = new HashMap(); + this.mutableExtensionsByNumber = new HashMap(); } private ExtensionRegistry(ExtensionRegistry other) { super(other); - this.immutableExtensionsByName = - Collections.unmodifiableMap(other.immutableExtensionsByName); - this.mutableExtensionsByName = - Collections.unmodifiableMap(other.mutableExtensionsByName); + this.immutableExtensionsByName = Collections.unmodifiableMap(other.immutableExtensionsByName); + this.mutableExtensionsByName = Collections.unmodifiableMap(other.mutableExtensionsByName); this.immutableExtensionsByNumber = Collections.unmodifiableMap(other.immutableExtensionsByNumber); - this.mutableExtensionsByNumber = - Collections.unmodifiableMap(other.mutableExtensionsByNumber); + this.mutableExtensionsByNumber = Collections.unmodifiableMap(other.mutableExtensionsByNumber); } private final Map immutableExtensionsByName; @@ -316,24 +290,19 @@ public class ExtensionRegistry extends ExtensionRegistryLite { ExtensionRegistry(boolean empty) { super(EMPTY_REGISTRY_LITE); - this.immutableExtensionsByName = - Collections.emptyMap(); - this.mutableExtensionsByName = - Collections.emptyMap(); - this.immutableExtensionsByNumber = - Collections.emptyMap(); - this.mutableExtensionsByNumber = - Collections.emptyMap(); + this.immutableExtensionsByName = Collections.emptyMap(); + this.mutableExtensionsByName = Collections.emptyMap(); + this.immutableExtensionsByNumber = Collections.emptyMap(); + this.mutableExtensionsByNumber = Collections.emptyMap(); } + static final ExtensionRegistry EMPTY_REGISTRY = new ExtensionRegistry(true); - private void add( - final ExtensionInfo extension, - final Extension.ExtensionType extensionType) { + private void add(final ExtensionInfo extension, final Extension.ExtensionType extensionType) { if (!extension.descriptor.isExtension()) { throw new IllegalArgumentException( - "ExtensionRegistry.add() was given a FieldDescriptor for a regular " + - "(non-extension) field."); + "ExtensionRegistry.add() was given a FieldDescriptor for a regular " + + "(non-extension) field."); } Map extensionsByName; @@ -354,15 +323,15 @@ public class ExtensionRegistry extends ExtensionRegistryLite { extensionsByName.put(extension.descriptor.getFullName(), extension); extensionsByNumber.put( - new DescriptorIntPair(extension.descriptor.getContainingType(), - extension.descriptor.getNumber()), - extension); + new DescriptorIntPair( + extension.descriptor.getContainingType(), extension.descriptor.getNumber()), + extension); final FieldDescriptor field = extension.descriptor; - if (field.getContainingType().getOptions().getMessageSetWireFormat() && - field.getType() == FieldDescriptor.Type.MESSAGE && - field.isOptional() && - field.getExtensionScope() == field.getMessageType()) { + if (field.getContainingType().getOptions().getMessageSetWireFormat() + && field.getType() == FieldDescriptor.Type.MESSAGE + && field.isOptional() + && field.getExtensionScope() == field.getMessageType()) { // This is an extension of a MessageSet type defined within the extension // type's own scope. For backwards-compatibility, allow it to be looked // up by type name. @@ -384,12 +353,13 @@ public class ExtensionRegistry extends ExtensionRegistryLite { public int hashCode() { return descriptor.hashCode() * ((1 << 16) - 1) + number; } + @Override public boolean equals(final Object obj) { if (!(obj instanceof DescriptorIntPair)) { return false; } - final DescriptorIntPair other = (DescriptorIntPair)obj; + final DescriptorIntPair other = (DescriptorIntPair) obj; return descriptor == other.descriptor && number == other.number; } } diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionRegistryFactory.java b/java/core/src/main/java/com/google/protobuf/ExtensionRegistryFactory.java index 89f7ab9b33..f070aae796 100644 --- a/java/core/src/main/java/com/google/protobuf/ExtensionRegistryFactory.java +++ b/java/core/src/main/java/com/google/protobuf/ExtensionRegistryFactory.java @@ -35,16 +35,15 @@ import static com.google.protobuf.ExtensionRegistryLite.EMPTY_REGISTRY_LITE; /** * A factory object to create instances of {@link ExtensionRegistryLite}. * - *

- * This factory detects (via reflection) if the full (non-Lite) protocol buffer libraries - * are available, and if so, the instances returned are actually {@link ExtensionRegistry}. + *

This factory detects (via reflection) if the full (non-Lite) protocol buffer libraries are + * available, and if so, the instances returned are actually {@link ExtensionRegistry}. */ final class ExtensionRegistryFactory { static final String FULL_REGISTRY_CLASS_NAME = "com.google.protobuf.ExtensionRegistry"; /* Visible for Testing - @Nullable */ + @Nullable */ static final Class EXTENSION_REGISTRY_CLASS = reflectExtensionRegistry(); /* @Nullable */ @@ -90,7 +89,7 @@ final class ExtensionRegistryFactory { private static final ExtensionRegistryLite invokeSubclassFactory(String methodName) throws Exception { - return (ExtensionRegistryLite) EXTENSION_REGISTRY_CLASS - .getDeclaredMethod(methodName).invoke(null); + return (ExtensionRegistryLite) + EXTENSION_REGISTRY_CLASS.getDeclaredMethod(methodName).invoke(null); } } diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java b/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java index f3d48d3af3..f07d607212 100644 --- a/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java +++ b/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java @@ -36,22 +36,20 @@ import java.util.Map; /** * Equivalent to {@link ExtensionRegistry} but supports only "lite" types. - *

- * If all of your types are lite types, then you only need to use - * {@code ExtensionRegistryLite}. Similarly, if all your types are regular - * types, then you only need {@link ExtensionRegistry}. Typically it does not - * make sense to mix the two, since if you have any regular types in your - * program, you then require the full runtime and lose all the benefits of - * the lite runtime, so you might as well make all your types be regular types. - * However, in some cases (e.g. when depending on multiple third-party libraries - * where one uses lite types and one uses regular), you may find yourself - * wanting to mix the two. In this case things get more complicated. - *

- * There are three factors to consider: Whether the type being extended is - * lite, whether the embedded type (in the case of a message-typed extension) - * is lite, and whether the extension itself is lite. Since all three are - * declared in different files, they could all be different. Here are all - * the combinations and which type of registry to use: + * + *

If all of your types are lite types, then you only need to use {@code ExtensionRegistryLite}. + * Similarly, if all your types are regular types, then you only need {@link ExtensionRegistry}. + * Typically it does not make sense to mix the two, since if you have any regular types in your + * program, you then require the full runtime and lose all the benefits of the lite runtime, so you + * might as well make all your types be regular types. However, in some cases (e.g. when depending + * on multiple third-party libraries where one uses lite types and one uses regular), you may find + * yourself wanting to mix the two. In this case things get more complicated. + * + *

There are three factors to consider: Whether the type being extended is lite, whether the + * embedded type (in the case of a message-typed extension) is lite, and whether the extension + * itself is lite. Since all three are declared in different files, they could all be different. + * Here are all the combinations and which type of registry to use: + * *

  *   Extended type     Inner type    Extension         Use registry
  *   =======================================================================
@@ -60,13 +58,12 @@ import java.util.Map;
  *   regular           regular       regular           ExtensionRegistry
  *   all other combinations                            not supported
  * 
- *

- * Note that just as regular types are not allowed to contain lite-type fields, - * they are also not allowed to contain lite-type extensions. This is because - * regular types must be fully accessible via reflection, which in turn means - * that all the inner messages must also support reflection. On the other hand, - * since regular types implement the entire lite interface, there is no problem - * with embedding regular types inside lite types. + * + *

Note that just as regular types are not allowed to contain lite-type fields, they are also not + * allowed to contain lite-type extensions. This is because regular types must be fully accessible + * via reflection, which in turn means that all the inner messages must also support reflection. On + * the other hand, since regular types implement the entire lite interface, there is no problem with + * embedding regular types inside lite types. * * @author kenton@google.com Kenton Varda */ @@ -79,6 +76,10 @@ public class ExtensionRegistryLite { // applications. Need to support this feature on smaller granularity. private static volatile boolean eagerlyParseMessageSets = false; + // short circuit the ExtensionRegistryFactory via assumevalues trickery + @SuppressWarnings("JavaOptionalSuggestions") + private static boolean doFullRuntimeInheritanceCheck = true; + // Visible for testing. static final String EXTENSION_CLASS_NAME = "com.google.protobuf.Extension"; @@ -110,15 +111,32 @@ public class ExtensionRegistryLite { * available. */ public static ExtensionRegistryLite newInstance() { - return ExtensionRegistryFactory.create(); + return doFullRuntimeInheritanceCheck + ? ExtensionRegistryFactory.create() + : new ExtensionRegistryLite(); } + private static volatile ExtensionRegistryLite emptyRegistry; + /** - * Get the unmodifiable singleton empty instance of either ExtensionRegistryLite or - * {@code ExtensionRegistry} (if the full (non-Lite) proto libraries are available). + * Get the unmodifiable singleton empty instance of either ExtensionRegistryLite or {@code + * ExtensionRegistry} (if the full (non-Lite) proto libraries are available). */ public static ExtensionRegistryLite getEmptyRegistry() { - return ExtensionRegistryFactory.createEmpty(); + ExtensionRegistryLite result = emptyRegistry; + if (result == null) { + synchronized (ExtensionRegistryLite.class) { + result = emptyRegistry; + if (result == null) { + result = + emptyRegistry = + doFullRuntimeInheritanceCheck + ? ExtensionRegistryFactory.createEmpty() + : EMPTY_REGISTRY_LITE; + } + } + } + return result; } @@ -130,37 +148,32 @@ public class ExtensionRegistryLite { /** * Find an extension by containing type and field number. * - * @return Information about the extension if found, or {@code null} - * otherwise. + * @return Information about the extension if found, or {@code null} otherwise. */ @SuppressWarnings("unchecked") public - GeneratedMessageLite.GeneratedExtension - findLiteExtensionByNumber( - final ContainingType containingTypeDefaultInstance, - final int fieldNumber) { + GeneratedMessageLite.GeneratedExtension findLiteExtensionByNumber( + final ContainingType containingTypeDefaultInstance, final int fieldNumber) { return (GeneratedMessageLite.GeneratedExtension) - extensionsByNumber.get( - new ObjectIntPair(containingTypeDefaultInstance, fieldNumber)); + extensionsByNumber.get(new ObjectIntPair(containingTypeDefaultInstance, fieldNumber)); } /** Add an extension from a lite generated file to the registry. */ - public final void add( - final GeneratedMessageLite.GeneratedExtension extension) { + public final void add(final GeneratedMessageLite.GeneratedExtension extension) { extensionsByNumber.put( - new ObjectIntPair(extension.getContainingTypeDefaultInstance(), - extension.getNumber()), - extension); + new ObjectIntPair(extension.getContainingTypeDefaultInstance(), extension.getNumber()), + extension); } /** - * Add an extension from a lite generated file to the registry only if it is - * a non-lite extension i.e. {@link GeneratedMessageLite.GeneratedExtension}. */ + * Add an extension from a lite generated file to the registry only if it is a non-lite extension + * i.e. {@link GeneratedMessageLite.GeneratedExtension}. + */ public final void add(ExtensionLite extension) { if (GeneratedMessageLite.GeneratedExtension.class.isAssignableFrom(extension.getClass())) { add((GeneratedMessageLite.GeneratedExtension) extension); } - if (ExtensionRegistryFactory.isFullRegistry(this)) { + if (doFullRuntimeInheritanceCheck && ExtensionRegistryFactory.isFullRegistry(this)) { try { this.getClass().getMethod("add", extensionClass).invoke(this, extension); } catch (Exception e) { @@ -178,23 +191,20 @@ public class ExtensionRegistryLite { ExtensionRegistryLite() { this.extensionsByNumber = - new HashMap>(); + new HashMap>(); } - static final ExtensionRegistryLite EMPTY_REGISTRY_LITE = - new ExtensionRegistryLite(true); + + static final ExtensionRegistryLite EMPTY_REGISTRY_LITE = new ExtensionRegistryLite(true); ExtensionRegistryLite(ExtensionRegistryLite other) { if (other == EMPTY_REGISTRY_LITE) { this.extensionsByNumber = Collections.emptyMap(); } else { - this.extensionsByNumber = - Collections.unmodifiableMap(other.extensionsByNumber); + this.extensionsByNumber = Collections.unmodifiableMap(other.extensionsByNumber); } } - private final Map> + private final Map> extensionsByNumber; ExtensionRegistryLite(boolean empty) { @@ -215,12 +225,13 @@ public class ExtensionRegistryLite { public int hashCode() { return System.identityHashCode(object) * ((1 << 16) - 1) + number; } + @Override public boolean equals(final Object obj) { if (!(obj instanceof ObjectIntPair)) { return false; } - final ObjectIntPair other = (ObjectIntPair)obj; + final ObjectIntPair other = (ObjectIntPair) obj; return object == other.object && number == other.number; } } diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionSchema.java b/java/core/src/main/java/com/google/protobuf/ExtensionSchema.java new file mode 100755 index 0000000000..2eae22d26a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ExtensionSchema.java @@ -0,0 +1,98 @@ +// 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. + +package com.google.protobuf; + +import java.io.IOException; +import java.util.Map; + +abstract class ExtensionSchema> { + + /** Returns true for messages that support extensions. */ + abstract boolean hasExtensions(MessageLite prototype); + + /** Returns the extension {@link FieldSet} for the message instance. */ + abstract FieldSet getExtensions(Object message); + + /** Replaces the extension {@link FieldSet} for the message instance. */ + abstract void setExtensions(Object message, FieldSet extensions); + + /** Returns the extension {@link FieldSet} and ensures it's mutable. */ + abstract FieldSet getMutableExtensions(Object message); + + /** Marks the extension {@link FieldSet} as immutable. */ + abstract void makeImmutable(Object message); + + /** + * Parses an extension. Returns the passed-in unknownFields parameter if no unknown enum value is + * found or a modified unknownFields (a new instance if the passed-in unknownFields is null) + * containing unknown enum values found while parsing. + * + * @param The type used to store unknown fields. It's either UnknownFieldSet in full runtime + * or UnknownFieldSetLite in lite runtime. + */ + abstract UB parseExtension( + Reader reader, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions, + UB unknownFields, + UnknownFieldSchema unknownFieldSchema) + throws IOException; + + /** Gets the field number of an extension entry. */ + abstract int extensionNumber(Map.Entry extension); + + /** Serializes one extension entry. */ + abstract void serializeExtension(Writer writer, Map.Entry extension) throws IOException; + + /** Finds an extension by field number. */ + abstract Object findExtensionByNumber( + ExtensionRegistryLite extensionRegistry, MessageLite defaultInstance, int number); + + /** Parses a length-prefixed MessageSet item from the reader. */ + abstract void parseLengthPrefixedMessageSetItem( + Reader reader, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions) + throws IOException; + + /** + * Parses the entire content of a {@link ByteString} as one MessageSet item. Unlike {@link + * #parseLengthPrefixedMessageSetItem}, there isn't a length-prefix. + */ + abstract void parseMessageSetItem( + ByteString data, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions) + throws IOException; +} diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionSchemaFull.java b/java/core/src/main/java/com/google/protobuf/ExtensionSchemaFull.java new file mode 100755 index 0000000000..90558518b9 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ExtensionSchemaFull.java @@ -0,0 +1,547 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.Descriptors.EnumValueDescriptor; +import com.google.protobuf.Descriptors.FieldDescriptor; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@SuppressWarnings("unchecked") +final class ExtensionSchemaFull extends ExtensionSchema { + + private static final long EXTENSION_FIELD_OFFSET = getExtensionsFieldOffset(); + + private static long getExtensionsFieldOffset() { + try { + Field field = GeneratedMessageV3.ExtendableMessage.class.getDeclaredField("extensions"); + return UnsafeUtil.objectFieldOffset(field); + } catch (Throwable e) { + throw new IllegalStateException("Unable to lookup extension field offset"); + } + } + + @Override + boolean hasExtensions(MessageLite prototype) { + return prototype instanceof GeneratedMessageV3.ExtendableMessage; + } + + @Override + public FieldSet getExtensions(Object message) { + return (FieldSet) UnsafeUtil.getObject(message, EXTENSION_FIELD_OFFSET); + } + + @Override + void setExtensions(Object message, FieldSet extensions) { + UnsafeUtil.putObject(message, EXTENSION_FIELD_OFFSET, extensions); + } + + @Override + FieldSet getMutableExtensions(Object message) { + FieldSet extensions = getExtensions(message); + if (extensions.isImmutable()) { + extensions = extensions.clone(); + setExtensions(message, extensions); + } + return extensions; + } + + @Override + void makeImmutable(Object message) { + getExtensions(message).makeImmutable(); + } + + @Override + UB parseExtension( + Reader reader, + Object extensionObject, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions, + UB unknownFields, + UnknownFieldSchema unknownFieldSchema) + throws IOException { + ExtensionRegistry.ExtensionInfo extension = (ExtensionRegistry.ExtensionInfo) extensionObject; + int fieldNumber = extension.descriptor.getNumber(); + + if (extension.descriptor.isRepeated() && extension.descriptor.isPacked()) { + Object value = null; + switch (extension.descriptor.getLiteType()) { + case DOUBLE: + { + List list = new ArrayList(); + reader.readDoubleList(list); + value = list; + break; + } + case FLOAT: + { + List list = new ArrayList(); + reader.readFloatList(list); + value = list; + break; + } + case INT64: + { + List list = new ArrayList(); + reader.readInt64List(list); + value = list; + break; + } + case UINT64: + { + List list = new ArrayList(); + reader.readUInt64List(list); + value = list; + break; + } + case INT32: + { + List list = new ArrayList(); + reader.readInt32List(list); + value = list; + break; + } + case FIXED64: + { + List list = new ArrayList(); + reader.readFixed64List(list); + value = list; + break; + } + case FIXED32: + { + List list = new ArrayList(); + reader.readFixed32List(list); + value = list; + break; + } + case BOOL: + { + List list = new ArrayList(); + reader.readBoolList(list); + value = list; + break; + } + case UINT32: + { + List list = new ArrayList(); + reader.readUInt32List(list); + value = list; + break; + } + case SFIXED32: + { + List list = new ArrayList(); + reader.readSFixed32List(list); + value = list; + break; + } + case SFIXED64: + { + List list = new ArrayList(); + reader.readSFixed64List(list); + value = list; + break; + } + case SINT32: + { + List list = new ArrayList(); + reader.readSInt32List(list); + value = list; + break; + } + case SINT64: + { + List list = new ArrayList(); + reader.readSInt64List(list); + value = list; + break; + } + case ENUM: + { + List list = new ArrayList(); + reader.readEnumList(list); + List enumList = new ArrayList(); + for (int number : list) { + EnumValueDescriptor enumDescriptor = + extension.descriptor.getEnumType().findValueByNumber(number); + if (enumDescriptor != null) { + enumList.add(enumDescriptor); + } else { + unknownFields = + SchemaUtil.storeUnknownEnum( + fieldNumber, number, unknownFields, unknownFieldSchema); + } + } + value = enumList; + break; + } + default: + throw new IllegalStateException( + "Type cannot be packed: " + extension.descriptor.getLiteType()); + } + extensions.setField(extension.descriptor, value); + } else { + Object value = null; + // Enum is a special case because unknown enum values will be put into UnknownFieldSetLite. + if (extension.descriptor.getLiteType() == WireFormat.FieldType.ENUM) { + int number = reader.readInt32(); + Object enumValue = extension.descriptor.getEnumType().findValueByNumber(number); + if (enumValue == null) { + return SchemaUtil.storeUnknownEnum( + fieldNumber, number, unknownFields, unknownFieldSchema); + } + value = enumValue; + } else { + switch (extension.descriptor.getLiteType()) { + case DOUBLE: + value = reader.readDouble(); + break; + case FLOAT: + value = reader.readFloat(); + break; + case INT64: + value = reader.readInt64(); + break; + case UINT64: + value = reader.readUInt64(); + break; + case INT32: + value = reader.readInt32(); + break; + case FIXED64: + value = reader.readFixed64(); + break; + case FIXED32: + value = reader.readFixed32(); + break; + case BOOL: + value = reader.readBool(); + break; + case BYTES: + value = reader.readBytes(); + break; + case UINT32: + value = reader.readUInt32(); + break; + case SFIXED32: + value = reader.readSFixed32(); + break; + case SFIXED64: + value = reader.readSFixed64(); + break; + case SINT32: + value = reader.readSInt32(); + break; + case SINT64: + value = reader.readSInt64(); + break; + + case STRING: + value = reader.readString(); + break; + case GROUP: + value = reader.readGroup(extension.defaultInstance.getClass(), extensionRegistry); + break; + + case MESSAGE: + value = reader.readMessage(extension.defaultInstance.getClass(), extensionRegistry); + break; + + case ENUM: + throw new IllegalStateException("Shouldn't reach here."); + } + } + if (extension.descriptor.isRepeated()) { + extensions.addRepeatedField(extension.descriptor, value); + } else { + switch (extension.descriptor.getLiteType()) { + case MESSAGE: + case GROUP: + Object oldValue = extensions.getField(extension.descriptor); + if (oldValue != null) { + value = Internal.mergeMessage(oldValue, value); + } + break; + default: + break; + } + extensions.setField(extension.descriptor, value); + } + } + return unknownFields; + } + + @Override + int extensionNumber(Map.Entry extension) { + FieldDescriptor descriptor = (FieldDescriptor) extension.getKey(); + return descriptor.getNumber(); + } + + @Override + void serializeExtension(Writer writer, Map.Entry extension) throws IOException { + FieldDescriptor descriptor = (FieldDescriptor) extension.getKey(); + if (descriptor.isRepeated()) { + switch (descriptor.getLiteType()) { + case DOUBLE: + SchemaUtil.writeDoubleList( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FLOAT: + SchemaUtil.writeFloatList( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case INT64: + SchemaUtil.writeInt64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case UINT64: + SchemaUtil.writeUInt64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case INT32: + SchemaUtil.writeInt32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FIXED64: + SchemaUtil.writeFixed64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FIXED32: + SchemaUtil.writeFixed32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case BOOL: + SchemaUtil.writeBoolList( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case BYTES: + SchemaUtil.writeBytesList( + descriptor.getNumber(), (List) extension.getValue(), writer); + break; + case UINT32: + SchemaUtil.writeUInt32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SFIXED32: + SchemaUtil.writeSFixed32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SFIXED64: + SchemaUtil.writeSFixed64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SINT32: + SchemaUtil.writeSInt32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SINT64: + SchemaUtil.writeSInt64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case ENUM: + { + List enumList = (List) extension.getValue(); + List list = new ArrayList(); + for (EnumValueDescriptor d : enumList) { + list.add(d.getNumber()); + } + SchemaUtil.writeInt32List(descriptor.getNumber(), list, writer, descriptor.isPacked()); + break; + } + case STRING: + SchemaUtil.writeStringList( + descriptor.getNumber(), (List) extension.getValue(), writer); + break; + case GROUP: + SchemaUtil.writeGroupList(descriptor.getNumber(), (List) extension.getValue(), writer); + break; + case MESSAGE: + SchemaUtil.writeMessageList( + descriptor.getNumber(), (List) extension.getValue(), writer); + break; + } + } else { + switch (descriptor.getLiteType()) { + case DOUBLE: + writer.writeDouble(descriptor.getNumber(), (Double) extension.getValue()); + break; + case FLOAT: + writer.writeFloat(descriptor.getNumber(), (Float) extension.getValue()); + break; + case INT64: + writer.writeInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case UINT64: + writer.writeUInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case INT32: + writer.writeInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case FIXED64: + writer.writeFixed64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case FIXED32: + writer.writeFixed32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case BOOL: + writer.writeBool(descriptor.getNumber(), (Boolean) extension.getValue()); + break; + case BYTES: + writer.writeBytes(descriptor.getNumber(), (ByteString) extension.getValue()); + break; + case UINT32: + writer.writeUInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SFIXED32: + writer.writeSFixed32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SFIXED64: + writer.writeSFixed64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case SINT32: + writer.writeSInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SINT64: + writer.writeSInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case ENUM: + writer.writeInt32( + descriptor.getNumber(), ((EnumValueDescriptor) extension.getValue()).getNumber()); + break; + case STRING: + writer.writeString(descriptor.getNumber(), (String) extension.getValue()); + break; + case GROUP: + writer.writeGroup(descriptor.getNumber(), extension.getValue()); + break; + case MESSAGE: + writer.writeMessage(descriptor.getNumber(), extension.getValue()); + break; + } + } + } + + @Override + Object findExtensionByNumber( + ExtensionRegistryLite extensionRegistry, MessageLite defaultInstance, int number) { + return ((ExtensionRegistry) extensionRegistry) + .findExtensionByNumber(((Message) defaultInstance).getDescriptorForType(), number); + } + + @Override + void parseLengthPrefixedMessageSetItem( + Reader reader, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions) + throws IOException { + ExtensionRegistry.ExtensionInfo extensionInfo = (ExtensionRegistry.ExtensionInfo) extension; + + if (ExtensionRegistryLite.isEagerlyParseMessageSets()) { + Object value = + reader.readMessage(extensionInfo.defaultInstance.getClass(), extensionRegistry); + extensions.setField(extensionInfo.descriptor, value); + } else { + extensions.setField( + extensionInfo.descriptor, + new LazyField(extensionInfo.defaultInstance, extensionRegistry, reader.readBytes())); + } + } + + @Override + void parseMessageSetItem( + ByteString data, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions) + throws IOException { + ExtensionRegistry.ExtensionInfo extensionInfo = (ExtensionRegistry.ExtensionInfo) extension; + Object value = extensionInfo.defaultInstance.newBuilderForType().buildPartial(); + + if (ExtensionRegistryLite.isEagerlyParseMessageSets()) { + Reader reader = BinaryReader.newInstance(ByteBuffer.wrap(data.toByteArray()), true); + Protobuf.getInstance().mergeFrom(value, reader, extensionRegistry); + extensions.setField(extensionInfo.descriptor, value); + + if (reader.getFieldNumber() != Reader.READ_DONE) { + throw InvalidProtocolBufferException.invalidEndTag(); + } + } else { + extensions.setField( + extensionInfo.descriptor, + new LazyField(extensionInfo.defaultInstance, extensionRegistry, data)); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java b/java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java new file mode 100755 index 0000000000..7f6846eb0a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java @@ -0,0 +1,541 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.GeneratedMessageLite.ExtensionDescriptor; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@SuppressWarnings("unchecked") +final class ExtensionSchemaLite extends ExtensionSchema { + + @Override + boolean hasExtensions(MessageLite prototype) { + return prototype instanceof GeneratedMessageLite.ExtendableMessage; + } + + @Override + FieldSet getExtensions(Object message) { + return ((GeneratedMessageLite.ExtendableMessage) message).extensions; + } + + @Override + void setExtensions(Object message, FieldSet extensions) { + ((GeneratedMessageLite.ExtendableMessage) message).extensions = extensions; + } + + @Override + FieldSet getMutableExtensions(Object message) { + return ((GeneratedMessageLite.ExtendableMessage) message).ensureExtensionsAreMutable(); + } + + @Override + void makeImmutable(Object message) { + getExtensions(message).makeImmutable(); + } + + @Override + UB parseExtension( + Reader reader, + Object extensionObject, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions, + UB unknownFields, + UnknownFieldSchema unknownFieldSchema) + throws IOException { + GeneratedMessageLite.GeneratedExtension extension = + (GeneratedMessageLite.GeneratedExtension) extensionObject; + int fieldNumber = extension.getNumber(); + + if (extension.descriptor.isRepeated() && extension.descriptor.isPacked()) { + Object value = null; + switch (extension.getLiteType()) { + case DOUBLE: + { + List list = new ArrayList(); + reader.readDoubleList(list); + value = list; + break; + } + case FLOAT: + { + List list = new ArrayList(); + reader.readFloatList(list); + value = list; + break; + } + case INT64: + { + List list = new ArrayList(); + reader.readInt64List(list); + value = list; + break; + } + case UINT64: + { + List list = new ArrayList(); + reader.readUInt64List(list); + value = list; + break; + } + case INT32: + { + List list = new ArrayList(); + reader.readInt32List(list); + value = list; + break; + } + case FIXED64: + { + List list = new ArrayList(); + reader.readFixed64List(list); + value = list; + break; + } + case FIXED32: + { + List list = new ArrayList(); + reader.readFixed32List(list); + value = list; + break; + } + case BOOL: + { + List list = new ArrayList(); + reader.readBoolList(list); + value = list; + break; + } + case UINT32: + { + List list = new ArrayList(); + reader.readUInt32List(list); + value = list; + break; + } + case SFIXED32: + { + List list = new ArrayList(); + reader.readSFixed32List(list); + value = list; + break; + } + case SFIXED64: + { + List list = new ArrayList(); + reader.readSFixed64List(list); + value = list; + break; + } + case SINT32: + { + List list = new ArrayList(); + reader.readSInt32List(list); + value = list; + break; + } + case SINT64: + { + List list = new ArrayList(); + reader.readSInt64List(list); + value = list; + break; + } + case ENUM: + { + List list = new ArrayList(); + reader.readEnumList(list); + unknownFields = + SchemaUtil.filterUnknownEnumList( + fieldNumber, + list, + extension.descriptor.getEnumType(), + unknownFields, + unknownFieldSchema); + value = list; + break; + } + default: + throw new IllegalStateException( + "Type cannot be packed: " + extension.descriptor.getLiteType()); + } + extensions.setField(extension.descriptor, value); + } else { + Object value = null; + // Enum is a special case becasue unknown enum values will be put into UnknownFieldSetLite. + if (extension.getLiteType() == WireFormat.FieldType.ENUM) { + int number = reader.readInt32(); + Object enumValue = extension.descriptor.getEnumType().findValueByNumber(number); + if (enumValue == null) { + return SchemaUtil.storeUnknownEnum( + fieldNumber, number, unknownFields, unknownFieldSchema); + } + // Note, we store the integer value instead of the actual enum object in FieldSet. + // This is also different from full-runtime where we store EnumValueDescriptor. + value = number; + } else { + switch (extension.getLiteType()) { + case DOUBLE: + value = reader.readDouble(); + break; + case FLOAT: + value = reader.readFloat(); + break; + case INT64: + value = reader.readInt64(); + break; + case UINT64: + value = reader.readUInt64(); + break; + case INT32: + value = reader.readInt32(); + break; + case FIXED64: + value = reader.readFixed64(); + break; + case FIXED32: + value = reader.readFixed32(); + break; + case BOOL: + value = reader.readBool(); + break; + case BYTES: + value = reader.readBytes(); + break; + case UINT32: + value = reader.readUInt32(); + break; + case SFIXED32: + value = reader.readSFixed32(); + break; + case SFIXED64: + value = reader.readSFixed64(); + break; + case SINT32: + value = reader.readSInt32(); + break; + case SINT64: + value = reader.readSInt64(); + break; + + case STRING: + value = reader.readString(); + break; + case GROUP: + value = + reader.readGroup( + extension.getMessageDefaultInstance().getClass(), extensionRegistry); + break; + + case MESSAGE: + value = + reader.readMessage( + extension.getMessageDefaultInstance().getClass(), extensionRegistry); + break; + + case ENUM: + throw new IllegalStateException("Shouldn't reach here."); + } + } + if (extension.isRepeated()) { + extensions.addRepeatedField(extension.descriptor, value); + } else { + switch (extension.getLiteType()) { + case MESSAGE: + case GROUP: + Object oldValue = extensions.getField(extension.descriptor); + if (oldValue != null) { + value = Internal.mergeMessage(oldValue, value); + } + break; + default: + break; + } + extensions.setField(extension.descriptor, value); + } + } + return unknownFields; + } + + @Override + int extensionNumber(Map.Entry extension) { + GeneratedMessageLite.ExtensionDescriptor descriptor = + (GeneratedMessageLite.ExtensionDescriptor) extension.getKey(); + return descriptor.getNumber(); + } + + @Override + void serializeExtension(Writer writer, Map.Entry extension) throws IOException { + GeneratedMessageLite.ExtensionDescriptor descriptor = + (GeneratedMessageLite.ExtensionDescriptor) extension.getKey(); + if (descriptor.isRepeated()) { + switch (descriptor.getLiteType()) { + case DOUBLE: + SchemaUtil.writeDoubleList( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FLOAT: + SchemaUtil.writeFloatList( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case INT64: + SchemaUtil.writeInt64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case UINT64: + SchemaUtil.writeUInt64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case INT32: + SchemaUtil.writeInt32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FIXED64: + SchemaUtil.writeFixed64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FIXED32: + SchemaUtil.writeFixed32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case BOOL: + SchemaUtil.writeBoolList( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case BYTES: + SchemaUtil.writeBytesList( + descriptor.getNumber(), (List) extension.getValue(), writer); + break; + case UINT32: + SchemaUtil.writeUInt32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SFIXED32: + SchemaUtil.writeSFixed32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SFIXED64: + SchemaUtil.writeSFixed64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SINT32: + SchemaUtil.writeSInt32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SINT64: + SchemaUtil.writeSInt64List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case ENUM: + SchemaUtil.writeInt32List( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case STRING: + SchemaUtil.writeStringList( + descriptor.getNumber(), (List) extension.getValue(), writer); + break; + case GROUP: + { + List data = (List) extension.getValue(); + if (data != null && !data.isEmpty()) { + SchemaUtil.writeGroupList( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + Protobuf.getInstance().schemaFor(data.get(0).getClass())); + } + } + break; + case MESSAGE: + { + List data = (List) extension.getValue(); + if (data != null && !data.isEmpty()) { + SchemaUtil.writeMessageList( + descriptor.getNumber(), + (List) extension.getValue(), + writer, + Protobuf.getInstance().schemaFor(data.get(0).getClass())); + } + } + break; + } + } else { + switch (descriptor.getLiteType()) { + case DOUBLE: + writer.writeDouble(descriptor.getNumber(), (Double) extension.getValue()); + break; + case FLOAT: + writer.writeFloat(descriptor.getNumber(), (Float) extension.getValue()); + break; + case INT64: + writer.writeInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case UINT64: + writer.writeUInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case INT32: + writer.writeInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case FIXED64: + writer.writeFixed64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case FIXED32: + writer.writeFixed32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case BOOL: + writer.writeBool(descriptor.getNumber(), (Boolean) extension.getValue()); + break; + case BYTES: + writer.writeBytes(descriptor.getNumber(), (ByteString) extension.getValue()); + break; + case UINT32: + writer.writeUInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SFIXED32: + writer.writeSFixed32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SFIXED64: + writer.writeSFixed64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case SINT32: + writer.writeSInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SINT64: + writer.writeSInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case ENUM: + writer.writeInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case STRING: + writer.writeString(descriptor.getNumber(), (String) extension.getValue()); + break; + case GROUP: + writer.writeGroup( + descriptor.getNumber(), + extension.getValue(), + Protobuf.getInstance().schemaFor(extension.getValue().getClass())); + break; + case MESSAGE: + writer.writeMessage( + descriptor.getNumber(), + extension.getValue(), + Protobuf.getInstance().schemaFor(extension.getValue().getClass())); + break; + } + } + } + + @Override + Object findExtensionByNumber( + ExtensionRegistryLite extensionRegistry, MessageLite defaultInstance, int number) { + return extensionRegistry.findLiteExtensionByNumber(defaultInstance, number); + } + + @Override + void parseLengthPrefixedMessageSetItem( + Reader reader, + Object extensionObject, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions) + throws IOException { + GeneratedMessageLite.GeneratedExtension extension = + (GeneratedMessageLite.GeneratedExtension) extensionObject; + Object value = + reader.readMessage(extension.getMessageDefaultInstance().getClass(), extensionRegistry); + extensions.setField(extension.descriptor, value); + } + + @Override + void parseMessageSetItem( + ByteString data, + Object extensionObject, + ExtensionRegistryLite extensionRegistry, + FieldSet extensions) + throws IOException { + GeneratedMessageLite.GeneratedExtension extension = + (GeneratedMessageLite.GeneratedExtension) extensionObject; + Object value = extension.getMessageDefaultInstance().newBuilderForType().buildPartial(); + + Reader reader = BinaryReader.newInstance(ByteBuffer.wrap(data.toByteArray()), true); + + Protobuf.getInstance().mergeFrom(value, reader, extensionRegistry); + extensions.setField(extension.descriptor, value); + + if (reader.getFieldNumber() != Reader.READ_DONE) { + throw InvalidProtocolBufferException.invalidEndTag(); + } + } +} diff --git a/src/google/protobuf/stubs/singleton.h b/java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java old mode 100644 new mode 100755 similarity index 65% rename from src/google/protobuf/stubs/singleton.h rename to java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java index 5f0f461054..46ce327d13 --- a/src/google/protobuf/stubs/singleton.h +++ b/java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java @@ -1,5 +1,5 @@ // Protocol Buffers - Google's data interchange format -// Copyright 2014 Google Inc. All rights reserved. +// Copyright 2008 Google Inc. All rights reserved. // https://developers.google.com/protocol-buffers/ // // Redistribution and use in source and binary forms, with or without @@ -27,41 +27,30 @@ // 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_SINGLETON_H__ -#define GOOGLE_PROTOBUF_STUBS_SINGLETON_H__ -#include -#include +package com.google.protobuf; -namespace google { -namespace protobuf { -namespace internal { -template -class Singleton { - public: - static T* get() { - GoogleOnceInit(&once_, &Singleton::Init); - return instance_; - } - static void ShutDown() { - delete instance_; - instance_ = nullptr; - } - private: - static void Init() { - instance_ = new T(); - } - static ProtobufOnceType once_; - static T* instance_; -}; +final class ExtensionSchemas { + private static final ExtensionSchema LITE_SCHEMA = new ExtensionSchemaLite(); + private static final ExtensionSchema FULL_SCHEMA = loadSchemaForFullRuntime(); -template -ProtobufOnceType Singleton::once_; + private static ExtensionSchema loadSchemaForFullRuntime() { + try { + Class clazz = Class.forName("com.google.protobuf.ExtensionSchemaFull"); + return (ExtensionSchema) clazz.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + return null; + } + } -template -T* Singleton::instance_ = nullptr; -} // namespace internal -} // namespace protobuf -} // namespace google + static ExtensionSchema lite() { + return LITE_SCHEMA; + } -#endif // GOOGLE_PROTOBUF_STUBS_SINGLETON_H__ + static ExtensionSchema full() { + if (FULL_SCHEMA == null) { + throw new IllegalStateException("Protobuf runtime is not correctly loaded."); + } + return FULL_SCHEMA; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/FieldInfo.java b/java/core/src/main/java/com/google/protobuf/FieldInfo.java new file mode 100755 index 0000000000..71a307a895 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/FieldInfo.java @@ -0,0 +1,577 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +import com.google.protobuf.Internal.EnumVerifier; +import java.lang.reflect.Field; + +/** Information for a single field in a protobuf message class. */ +@ExperimentalApi +final class FieldInfo implements Comparable { + private final Field field; + private final FieldType type; + private final Class messageClass; // The message type for repeated message fields. + private final int fieldNumber; + private final Field presenceField; + private final int presenceMask; + private final boolean required; + private final boolean enforceUtf8; + private final OneofInfo oneof; + private final Field cachedSizeField; + /** + * The actual type stored in the oneof value for this field. Since the oneof value is an {@link + * Object}, primitives will store their boxed type. Only valid in conjunction with {@link #oneof} + * (both must be either null or non-null. + */ + private final Class oneofStoredType; + + // TODO(liujisi): make map default entry lazy? + private final Object mapDefaultEntry; + + private final EnumVerifier enumVerifier; + + /** Constructs a new descriptor for a field. */ + public static FieldInfo forField( + Field field, int fieldNumber, FieldType fieldType, boolean enforceUtf8) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + if (fieldType == FieldType.MESSAGE_LIST || fieldType == FieldType.GROUP_LIST) { + throw new IllegalStateException("Shouldn't be called for repeated message fields."); + } + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + enforceUtf8, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + /* enumVerifier= */ null, + /* cachedSizeField= */ null); + } + + /** Constructs a new descriptor for a packed field. */ + public static FieldInfo forPackedField( + Field field, int fieldNumber, FieldType fieldType, Field cachedSizeField) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + if (fieldType == FieldType.MESSAGE_LIST || fieldType == FieldType.GROUP_LIST) { + throw new IllegalStateException("Shouldn't be called for repeated message fields."); + } + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ false, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + /* enumVerifier= */ null, + cachedSizeField); + } + + /** Constructs a new descriptor for a repeated message field. */ + public static FieldInfo forRepeatedMessageField( + Field field, int fieldNumber, FieldType fieldType, Class messageClass) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(messageClass, "messageClass"); + return new FieldInfo( + field, + fieldNumber, + fieldType, + messageClass, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ false, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + /* enumVerifier= */ null, + /* cachedSizeField= */ null); + } + + public static FieldInfo forFieldWithEnumVerifier( + Field field, int fieldNumber, FieldType fieldType, EnumVerifier enumVerifier) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ false, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + enumVerifier, + /* cachedSizeField= */ null); + } + + public static FieldInfo forPackedFieldWithEnumVerifier( + Field field, + int fieldNumber, + FieldType fieldType, + EnumVerifier enumVerifier, + Field cachedSizeField) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ false, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + enumVerifier, + cachedSizeField); + } + + /** Constructor for a proto2 optional field. */ + public static FieldInfo forProto2OptionalField( + Field field, + int fieldNumber, + FieldType fieldType, + Field presenceField, + int presenceMask, + boolean enforceUtf8, + EnumVerifier enumVerifier) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(presenceField, "presenceField"); + if (presenceField != null && !isExactlyOneBitSet(presenceMask)) { + throw new IllegalArgumentException( + "presenceMask must have exactly one bit set: " + presenceMask); + } + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + presenceField, + presenceMask, + /* required= */ false, + enforceUtf8, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + enumVerifier, + /* cachedSizeField= */ null); + } + + /** + * Constructor for a field that is part of a oneof. + * + * @param fieldNumber the unique field number for this field within the message. + * @param fieldType the type of the field (must be non-null). + * @param oneof the oneof for which this field is associated (must be non-null). + * @param oneofStoredType the actual type stored in the oneof value for this field. Since the + * oneof value is an {@link Object}, primitives will store their boxed type. Must be non-null. + * @param enforceUtf8 Only used for string fields. If {@code true}, will enforce UTF-8 on a string + * field. + * @return the {@link FieldInfo} describing this field. + */ + public static FieldInfo forOneofMemberField( + int fieldNumber, + FieldType fieldType, + OneofInfo oneof, + Class oneofStoredType, + boolean enforceUtf8, + EnumVerifier enumVerifier) { + checkFieldNumber(fieldNumber); + checkNotNull(fieldType, "fieldType"); + checkNotNull(oneof, "oneof"); + checkNotNull(oneofStoredType, "oneofStoredType"); + if (!fieldType.isScalar()) { + throw new IllegalArgumentException( + "Oneof is only supported for scalar fields. Field " + + fieldNumber + + " is of type " + + fieldType); + } + return new FieldInfo( + /* field= */ null, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + enforceUtf8, + oneof, + oneofStoredType, + /* mapDefaultEntry= */ null, + enumVerifier, + /* cachedSizeField= */ null); + } + + private static void checkFieldNumber(int fieldNumber) { + if (fieldNumber <= 0) { + throw new IllegalArgumentException("fieldNumber must be positive: " + fieldNumber); + } + } + + /** Constructor for a proto2 required field. */ + public static FieldInfo forProto2RequiredField( + Field field, + int fieldNumber, + FieldType fieldType, + Field presenceField, + int presenceMask, + boolean enforceUtf8, + EnumVerifier enumVerifier) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(presenceField, "presenceField"); + if (presenceField != null && !isExactlyOneBitSet(presenceMask)) { + throw new IllegalArgumentException( + "presenceMask must have exactly one bit set: " + presenceMask); + } + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + presenceField, + presenceMask, + /* required= */ true, + enforceUtf8, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + /* enumVerifier= */ enumVerifier, + /* cachedSizeField= */ null); + } + + public static FieldInfo forMapField( + Field field, int fieldNumber, Object mapDefaultEntry, EnumVerifier enumVerifier) { + checkNotNull(mapDefaultEntry, "mapDefaultEntry"); + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + return new FieldInfo( + field, + fieldNumber, + FieldType.MAP, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ true, + /* oneof= */ null, + /* oneofStoredType= */ null, + mapDefaultEntry, + enumVerifier, + /* cachedSizeField= */ null); + } + + private FieldInfo( + Field field, + int fieldNumber, + FieldType type, + Class messageClass, + Field presenceField, + int presenceMask, + boolean required, + boolean enforceUtf8, + OneofInfo oneof, + Class oneofStoredType, + Object mapDefaultEntry, + EnumVerifier enumVerifier, + Field cachedSizeField) { + this.field = field; + this.type = type; + this.messageClass = messageClass; + this.fieldNumber = fieldNumber; + this.presenceField = presenceField; + this.presenceMask = presenceMask; + this.required = required; + this.enforceUtf8 = enforceUtf8; + this.oneof = oneof; + this.oneofStoredType = oneofStoredType; + this.mapDefaultEntry = mapDefaultEntry; + this.enumVerifier = enumVerifier; + this.cachedSizeField = cachedSizeField; + } + + /** Gets the field number for the field. */ + public int getFieldNumber() { + return fieldNumber; + } + + /** Gets the subject {@link Field} of this descriptor. */ + public Field getField() { + return field; + } + + /** Gets the type information for the field. */ + public FieldType getType() { + return type; + } + + /** Gets the oneof for which this field is a member, or {@code null} if not part of a oneof. */ + public OneofInfo getOneof() { + return oneof; + } + + /** + * Gets the actual type stored in the oneof value by this field. Since the oneof value is an + * {@link Object}, primitives will store their boxed type. For non-oneof fields, this will always + * be {@code null}. + */ + public Class getOneofStoredType() { + return oneofStoredType; + } + + /** Gets the {@code EnumVerifier} if the field is an enum field. */ + public EnumVerifier getEnumVerifier() { + return enumVerifier; + } + + @Override + public int compareTo(FieldInfo o) { + return fieldNumber - o.fieldNumber; + } + + /** + * For repeated message fields, returns the message type of the field. For other fields, returns + * {@code null}. + */ + public Class getListElementType() { + return messageClass; + } + + /** Gets the presence bit field. Only valid for unary fields. For lists, returns {@code null}. */ + public Field getPresenceField() { + return presenceField; + } + + public Object getMapDefaultEntry() { + return mapDefaultEntry; + } + + /** + * If {@link #getPresenceField()} is non-{@code null}, returns the mask used to identify the + * presence bit for this field in the message. + */ + public int getPresenceMask() { + return presenceMask; + } + + /** Whether this is a required field. */ + public boolean isRequired() { + return required; + } + + /** + * Whether a UTF-8 should be enforced on string fields. Only applies to strings and string lists. + */ + public boolean isEnforceUtf8() { + return enforceUtf8; + } + + public Field getCachedSizeField() { + return cachedSizeField; + } + + /** + * For singular or repeated message fields, returns the message type. For other fields, returns + * {@code null}. + */ + public Class getMessageFieldClass() { + switch (type) { + case MESSAGE: + case GROUP: + return field != null ? field.getType() : oneofStoredType; + case MESSAGE_LIST: + case GROUP_LIST: + return messageClass; + default: + return null; + } + } + + public static Builder newBuilder() { + return new Builder(); + } + + /** A builder for {@link FieldInfo} instances. */ + public static final class Builder { + private Field field; + private FieldType type; + private int fieldNumber; + private Field presenceField; + private int presenceMask; + private boolean required; + private boolean enforceUtf8; + private OneofInfo oneof; + private Class oneofStoredType; + private Object mapDefaultEntry; + private EnumVerifier enumVerifier; + private Field cachedSizeField; + + private Builder() {} + + /** + * Specifies the actual field on the message represented by this field. This should not be + * called for oneof member fields. + */ + public Builder withField(Field field) { + if (oneof != null) { + throw new IllegalStateException("Cannot set field when building a oneof."); + } + this.field = field; + return this; + } + + /** Specifies the type of this field. */ + public Builder withType(FieldType type) { + this.type = type; + return this; + } + + /** Specifies the unique field number for this field within the message. */ + public Builder withFieldNumber(int fieldNumber) { + this.fieldNumber = fieldNumber; + return this; + } + + /** Specifies proto2 presence information. This should not be called for oneof fields. */ + public Builder withPresence(Field presenceField, int presenceMask) { + this.presenceField = checkNotNull(presenceField, "presenceField"); + this.presenceMask = presenceMask; + return this; + } + + /** + * Sets the information for building a oneof member field. This is incompatible with {@link + * #withField(Field)} and {@link #withPresence(Field, int)}. + * + * @param oneof the oneof for which this field is associated. + * @param oneofStoredType the actual type stored in the oneof value for this field. Since the + * oneof value is an {@link Object}, primitives will store their boxed type. + */ + public Builder withOneof(OneofInfo oneof, Class oneofStoredType) { + if (field != null || presenceField != null) { + throw new IllegalStateException( + "Cannot set oneof when field or presenceField have been provided"); + } + this.oneof = oneof; + this.oneofStoredType = oneofStoredType; + return this; + } + + public Builder withRequired(boolean required) { + this.required = required; + return this; + } + + public Builder withMapDefaultEntry(Object mapDefaultEntry) { + this.mapDefaultEntry = mapDefaultEntry; + return this; + } + + public Builder withEnforceUtf8(boolean enforceUtf8) { + this.enforceUtf8 = enforceUtf8; + return this; + } + + public Builder withEnumVerifier(EnumVerifier enumVerifier) { + this.enumVerifier = enumVerifier; + return this; + } + + public Builder withCachedSizeField(Field cachedSizeField) { + this.cachedSizeField = cachedSizeField; + return this; + } + + public FieldInfo build() { + if (oneof != null) { + return forOneofMemberField( + fieldNumber, type, oneof, oneofStoredType, enforceUtf8, enumVerifier); + } + if (mapDefaultEntry != null) { + return forMapField(field, fieldNumber, mapDefaultEntry, enumVerifier); + } + if (presenceField != null) { + if (required) { + return forProto2RequiredField( + field, fieldNumber, type, presenceField, presenceMask, enforceUtf8, enumVerifier); + } else { + return forProto2OptionalField( + field, fieldNumber, type, presenceField, presenceMask, enforceUtf8, enumVerifier); + } + } + if (enumVerifier != null) { + if (cachedSizeField == null) { + return forFieldWithEnumVerifier(field, fieldNumber, type, enumVerifier); + } else { + return forPackedFieldWithEnumVerifier( + field, fieldNumber, type, enumVerifier, cachedSizeField); + } + } else { + if (cachedSizeField == null) { + return forField(field, fieldNumber, type, enforceUtf8); + } else { + return forPackedField(field, fieldNumber, type, cachedSizeField); + } + } + } + } + + private static boolean isExactlyOneBitSet(int value) { + return value != 0 && (value & (value - 1)) == 0; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/FieldSet.java b/java/core/src/main/java/com/google/protobuf/FieldSet.java index c09daa32c7..ddfd0e5601 100644 --- a/java/core/src/main/java/com/google/protobuf/FieldSet.java +++ b/java/core/src/main/java/com/google/protobuf/FieldSet.java @@ -41,32 +41,35 @@ import java.util.List; import java.util.Map; /** - * A class which represents an arbitrary set of fields of some message type. - * This is used to implement {@link DynamicMessage}, and also to represent - * extensions in {@link GeneratedMessage}. This class is package-private, - * since outside users should probably be using {@link DynamicMessage}. + * A class which represents an arbitrary set of fields of some message type. This is used to + * implement {@link DynamicMessage}, and also to represent extensions in {@link GeneratedMessage}. + * This class is package-private, since outside users should probably be using {@link + * DynamicMessage}. * * @author kenton@google.com Kenton Varda */ -final class FieldSet> { +final class FieldSet< + FieldDescriptorType extends FieldSet.FieldDescriptorLite> { /** - * Interface for a FieldDescriptor or lite extension descriptor. This - * prevents FieldSet from depending on {@link Descriptors.FieldDescriptor}. + * Interface for a FieldDescriptor or lite extension descriptor. This prevents FieldSet from + * depending on {@link Descriptors.FieldDescriptor}. */ - public interface FieldDescriptorLite> - extends Comparable { + public interface FieldDescriptorLite> extends Comparable { int getNumber(); + WireFormat.FieldType getLiteType(); + WireFormat.JavaType getLiteJavaType(); + boolean isRepeated(); + boolean isPacked(); + Internal.EnumLiteMap getEnumType(); // If getLiteJavaType() == MESSAGE, this merges a message object of the // type into a builder of the type. Returns {@code to}. - MessageLite.Builder internalMergeFrom( - MessageLite.Builder to, MessageLite from); + MessageLite.Builder internalMergeFrom(MessageLite.Builder to, MessageLite from); } private final SmallSortedMap fields; @@ -78,27 +81,23 @@ final class FieldSet> - FieldSet newFieldSet() { + public static > FieldSet newFieldSet() { return new FieldSet(); } /** Get an immutable empty FieldSet. */ @SuppressWarnings("unchecked") - public static > - FieldSet emptySet() { + public static > FieldSet emptySet() { return DEFAULT_INSTANCE; } + @SuppressWarnings("rawtypes") private static final FieldSet DEFAULT_INSTANCE = new FieldSet(true); @@ -118,8 +117,8 @@ final class FieldSet entry : - fields.getOverflowEntries()) { + for (Map.Entry entry : fields.getOverflowEntries()) { FieldDescriptorType descriptor = entry.getKey(); clone.setField(descriptor, entry.getValue()); } @@ -180,18 +178,14 @@ final class FieldSet getAllFields() { if (hasLazyField) { - SmallSortedMap result = - SmallSortedMap.newFieldMap(16); + SmallSortedMap result = SmallSortedMap.newFieldMap(16); for (int i = 0; i < fields.getNumArrayEntries(); i++) { cloneFieldEntry(result, fields.getArrayEntryAt(i)); } - for (Map.Entry entry : - fields.getOverflowEntries()) { + for (Map.Entry entry : fields.getOverflowEntries()) { cloneFieldEntry(result, entry); } if (fields.isImmutable()) { @@ -202,8 +196,8 @@ final class FieldSet map, - Map.Entry entry) { + private void cloneFieldEntry( + Map map, Map.Entry entry) { FieldDescriptorType key = entry.getKey(); Object value = entry.getValue(); if (value instanceof LazyField) { @@ -214,37 +208,41 @@ final class FieldSet> iterator() { if (hasLazyField) { - return new LazyIterator( - fields.entrySet().iterator()); + return new LazyIterator(fields.entrySet().iterator()); } return fields.entrySet().iterator(); } - /** - * Useful for implementing - * {@link Message#hasField(Descriptors.FieldDescriptor)}. + * Get an iterator over the fields in the map in descending (i.e. reverse) order. This iterator + * should not be leaked out of the protobuf library as it is not protected from mutation when + * fields is not immutable. */ + Iterator> descendingIterator() { + if (hasLazyField) { + return new LazyIterator(fields.descendingEntrySet().iterator()); + } + return fields.descendingEntrySet().iterator(); + } + + /** Useful for implementing {@link Message#hasField(Descriptors.FieldDescriptor)}. */ public boolean hasField(final FieldDescriptorType descriptor) { if (descriptor.isRepeated()) { - throw new IllegalArgumentException( - "hasField() can only be called on non-repeated fields."); + throw new IllegalArgumentException("hasField() can only be called on non-repeated fields."); } return fields.get(descriptor) != null; } /** - * Useful for implementing - * {@link Message#getField(Descriptors.FieldDescriptor)}. This method - * returns {@code null} if the field is not set; in this case it is up - * to the caller to fetch the field's default value. + * Useful for implementing {@link Message#getField(Descriptors.FieldDescriptor)}. This method + * returns {@code null} if the field is not set; in this case it is up to the caller to fetch the + * field's default value. */ public Object getField(final FieldDescriptorType descriptor) { Object o = fields.get(descriptor); @@ -255,16 +253,14 @@ final class FieldSet entry : - fields.getOverflowEntries()) { + for (final Map.Entry entry : fields.getOverflowEntries()) { if (!isInitialized(entry)) { return false; } @@ -454,13 +445,11 @@ final class FieldSet entry) { + private boolean isInitialized(final Map.Entry entry) { final FieldDescriptorType descriptor = entry.getKey(); if (descriptor.getLiteJavaType() == WireFormat.JavaType.MESSAGE) { if (descriptor.isRepeated()) { - for (final MessageLite element: - (List) entry.getValue()) { + for (final MessageLite element : (List) entry.getValue()) { if (!element.isInitialized()) { return false; } @@ -485,11 +474,9 @@ final class FieldSet other) { for (int i = 0; i < other.fields.getNumArrayEntries(); i++) { mergeFromField(other.fields.getArrayEntryAt(i)); } - for (final Map.Entry entry : - other.fields.getOverflowEntries()) { + for (final Map.Entry entry : other.fields.getOverflowEntries()) { mergeFromField(entry); } } @@ -523,8 +506,7 @@ final class FieldSet entry) { + private void mergeFromField(final Map.Entry entry) { final FieldDescriptorType descriptor = entry.getKey(); Object otherValue = entry.getValue(); if (otherValue instanceof LazyField) { @@ -546,9 +528,10 @@ final class FieldSet entry = - fields.getArrayEntryAt(i); + final Map.Entry entry = fields.getArrayEntryAt(i); writeField(entry.getKey(), entry.getValue(), output); } - for (final Map.Entry entry : - fields.getOverflowEntries()) { + for (final Map.Entry entry : fields.getOverflowEntries()) { writeField(entry.getKey(), entry.getValue(), output); } } - /** - * Like {@link #writeTo} but uses MessageSet wire format. - */ - public void writeMessageSetTo(final CodedOutputStream output) - throws IOException { + /** Like {@link #writeTo} but uses MessageSet wire format. */ + public void writeMessageSetTo(final CodedOutputStream output) throws IOException { for (int i = 0; i < fields.getNumArrayEntries(); i++) { writeMessageSetTo(fields.getArrayEntryAt(i), output); } - for (final Map.Entry entry : - fields.getOverflowEntries()) { + for (final Map.Entry entry : fields.getOverflowEntries()) { writeMessageSetTo(entry, output); } } private void writeMessageSetTo( - final Map.Entry entry, - final CodedOutputStream output) throws IOException { + final Map.Entry entry, final CodedOutputStream output) + throws IOException { final FieldDescriptorType descriptor = entry.getKey(); - if (descriptor.getLiteJavaType() == WireFormat.JavaType.MESSAGE && - !descriptor.isRepeated() && !descriptor.isPacked()) { + if (descriptor.getLiteJavaType() == WireFormat.JavaType.MESSAGE + && !descriptor.isRepeated() + && !descriptor.isPacked()) { Object value = entry.getValue(); if (value instanceof LazyField) { value = ((LazyField) value).getValue(); } - output.writeMessageSetExtension(entry.getKey().getNumber(), - (MessageLite) value); + output.writeMessageSetExtension(entry.getKey().getNumber(), (MessageLite) value); } else { writeField(descriptor, entry.getValue(), output); } @@ -636,18 +606,17 @@ final class FieldSet descriptor, - final Object value, - final CodedOutputStream output) - throws IOException { + public static void writeField( + final FieldDescriptorLite descriptor, final Object value, final CodedOutputStream output) + throws IOException { WireFormat.FieldType type = descriptor.getLiteType(); int number = descriptor.getNumber(); if (descriptor.isRepeated()) { - final List valueList = (List)value; + final List valueList = (List) value; if (descriptor.isPacked()) { output.writeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED); // Compute the total data size so the length can be written. @@ -749,44 +744,39 @@ final class FieldSet entry = - fields.getArrayEntryAt(i); + final Map.Entry entry = fields.getArrayEntryAt(i); size += computeFieldSize(entry.getKey(), entry.getValue()); } - for (final Map.Entry entry : - fields.getOverflowEntries()) { + for (final Map.Entry entry : fields.getOverflowEntries()) { size += computeFieldSize(entry.getKey(), entry.getValue()); } return size; } - /** - * Like {@link #getSerializedSize} but uses MessageSet wire format. - */ + /** Like {@link #getSerializedSize} but uses MessageSet wire format. */ public int getMessageSetSerializedSize() { int size = 0; for (int i = 0; i < fields.getNumArrayEntries(); i++) { size += getMessageSetSerializedSize(fields.getArrayEntryAt(i)); } - for (final Map.Entry entry : - fields.getOverflowEntries()) { + for (final Map.Entry entry : fields.getOverflowEntries()) { size += getMessageSetSerializedSize(entry); } return size; } - private int getMessageSetSerializedSize( - final Map.Entry entry) { + private int getMessageSetSerializedSize(final Map.Entry entry) { final FieldDescriptorType descriptor = entry.getKey(); Object value = entry.getValue(); if (descriptor.getLiteJavaType() == WireFormat.JavaType.MESSAGE - && !descriptor.isRepeated() && !descriptor.isPacked()) { + && !descriptor.isRepeated() + && !descriptor.isPacked()) { if (value instanceof LazyField) { return CodedOutputStream.computeLazyFieldMessageSetExtensionSize( entry.getKey().getNumber(), (LazyField) value); @@ -800,15 +790,13 @@ final class FieldSet descriptor, - final Object value) { + /** Compute the number of bytes needed to encode a particular field. */ + public static int computeFieldSize(final FieldDescriptorLite descriptor, final Object value) { WireFormat.FieldType type = descriptor.getLiteType(); int number = descriptor.getNumber(); if (descriptor.isRepeated()) { if (descriptor.isPacked()) { int dataSize = 0; - for (final Object element : (List)value) { + for (final Object element : (List) value) { dataSize += computeElementSizeNoTag(type, element); } - return dataSize + - CodedOutputStream.computeTagSize(number) + - CodedOutputStream.computeRawVarint32Size(dataSize); + return dataSize + + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeRawVarint32Size(dataSize); } else { int size = 0; - for (final Object element : (List)value) { + for (final Object element : (List) value) { size += computeElementSize(type, number, element); } return size; diff --git a/java/core/src/main/java/com/google/protobuf/FieldType.java b/java/core/src/main/java/com/google/protobuf/FieldType.java new file mode 100755 index 0000000000..1b8f9e5da2 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/FieldType.java @@ -0,0 +1,346 @@ +// 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. + +package com.google.protobuf; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.util.List; + +/** Enumeration identifying all relevant type information for a protobuf field. */ +@ExperimentalApi +public enum FieldType { + DOUBLE(0, Collection.SCALAR, JavaType.DOUBLE), + FLOAT(1, Collection.SCALAR, JavaType.FLOAT), + INT64(2, Collection.SCALAR, JavaType.LONG), + UINT64(3, Collection.SCALAR, JavaType.LONG), + INT32(4, Collection.SCALAR, JavaType.INT), + FIXED64(5, Collection.SCALAR, JavaType.LONG), + FIXED32(6, Collection.SCALAR, JavaType.INT), + BOOL(7, Collection.SCALAR, JavaType.BOOLEAN), + STRING(8, Collection.SCALAR, JavaType.STRING), + MESSAGE(9, Collection.SCALAR, JavaType.MESSAGE), + BYTES(10, Collection.SCALAR, JavaType.BYTE_STRING), + UINT32(11, Collection.SCALAR, JavaType.INT), + ENUM(12, Collection.SCALAR, JavaType.ENUM), + SFIXED32(13, Collection.SCALAR, JavaType.INT), + SFIXED64(14, Collection.SCALAR, JavaType.LONG), + SINT32(15, Collection.SCALAR, JavaType.INT), + SINT64(16, Collection.SCALAR, JavaType.LONG), + GROUP(17, Collection.SCALAR, JavaType.MESSAGE), + DOUBLE_LIST(18, Collection.VECTOR, JavaType.DOUBLE), + FLOAT_LIST(19, Collection.VECTOR, JavaType.FLOAT), + INT64_LIST(20, Collection.VECTOR, JavaType.LONG), + UINT64_LIST(21, Collection.VECTOR, JavaType.LONG), + INT32_LIST(22, Collection.VECTOR, JavaType.INT), + FIXED64_LIST(23, Collection.VECTOR, JavaType.LONG), + FIXED32_LIST(24, Collection.VECTOR, JavaType.INT), + BOOL_LIST(25, Collection.VECTOR, JavaType.BOOLEAN), + STRING_LIST(26, Collection.VECTOR, JavaType.STRING), + MESSAGE_LIST(27, Collection.VECTOR, JavaType.MESSAGE), + BYTES_LIST(28, Collection.VECTOR, JavaType.BYTE_STRING), + UINT32_LIST(29, Collection.VECTOR, JavaType.INT), + ENUM_LIST(30, Collection.VECTOR, JavaType.ENUM), + SFIXED32_LIST(31, Collection.VECTOR, JavaType.INT), + SFIXED64_LIST(32, Collection.VECTOR, JavaType.LONG), + SINT32_LIST(33, Collection.VECTOR, JavaType.INT), + SINT64_LIST(34, Collection.VECTOR, JavaType.LONG), + DOUBLE_LIST_PACKED(35, Collection.PACKED_VECTOR, JavaType.DOUBLE), + FLOAT_LIST_PACKED(36, Collection.PACKED_VECTOR, JavaType.FLOAT), + INT64_LIST_PACKED(37, Collection.PACKED_VECTOR, JavaType.LONG), + UINT64_LIST_PACKED(38, Collection.PACKED_VECTOR, JavaType.LONG), + INT32_LIST_PACKED(39, Collection.PACKED_VECTOR, JavaType.INT), + FIXED64_LIST_PACKED(40, Collection.PACKED_VECTOR, JavaType.LONG), + FIXED32_LIST_PACKED(41, Collection.PACKED_VECTOR, JavaType.INT), + BOOL_LIST_PACKED(42, Collection.PACKED_VECTOR, JavaType.BOOLEAN), + UINT32_LIST_PACKED(43, Collection.PACKED_VECTOR, JavaType.INT), + ENUM_LIST_PACKED(44, Collection.PACKED_VECTOR, JavaType.ENUM), + SFIXED32_LIST_PACKED(45, Collection.PACKED_VECTOR, JavaType.INT), + SFIXED64_LIST_PACKED(46, Collection.PACKED_VECTOR, JavaType.LONG), + SINT32_LIST_PACKED(47, Collection.PACKED_VECTOR, JavaType.INT), + SINT64_LIST_PACKED(48, Collection.PACKED_VECTOR, JavaType.LONG), + GROUP_LIST(49, Collection.VECTOR, JavaType.MESSAGE), + MAP(50, Collection.MAP, JavaType.VOID); + + private final JavaType javaType; + private final int id; + private final Collection collection; + private final Class elementType; + private final boolean primitiveScalar; + + FieldType(int id, Collection collection, JavaType javaType) { + this.id = id; + this.collection = collection; + this.javaType = javaType; + + switch (collection) { + case MAP: + elementType = javaType.getBoxedType(); + break; + case VECTOR: + elementType = javaType.getBoxedType(); + break; + case SCALAR: + default: + elementType = null; + break; + } + + boolean primitiveScalar = false; + if (collection == Collection.SCALAR) { + switch (javaType) { + case BYTE_STRING: + case MESSAGE: + case STRING: + break; + default: + primitiveScalar = true; + break; + } + } + this.primitiveScalar = primitiveScalar; + } + + /** A reliable unique identifier for this type. */ + public int id() { + return id; + } + + /** + * Gets the {@link JavaType} for this field. For lists, this identifies the type of the elements + * contained within the list. + */ + public JavaType getJavaType() { + return javaType; + } + + /** Indicates whether a list field should be represented on the wire in packed form. */ + public boolean isPacked() { + return Collection.PACKED_VECTOR.equals(collection); + } + + /** + * Indicates whether this field type represents a primitive scalar value. If this is {@code true}, + * then {@link #isScalar()} will also be {@code true}. + */ + public boolean isPrimitiveScalar() { + return primitiveScalar; + } + + /** Indicates whether this field type represents a scalar value. */ + public boolean isScalar() { + return collection == Collection.SCALAR; + } + + /** Indicates whether this field represents a list of values. */ + public boolean isList() { + return collection.isList(); + } + + /** Indicates whether this field represents a map. */ + public boolean isMap() { + return collection == Collection.MAP; + } + + /** Indicates whether or not this {@link FieldType} can be applied to the given {@link Field}. */ + public boolean isValidForField(Field field) { + if (Collection.VECTOR.equals(collection)) { + return isValidForList(field); + } else { + return javaType.getType().isAssignableFrom(field.getType()); + } + } + + private boolean isValidForList(Field field) { + Class clazz = field.getType(); + if (!javaType.getType().isAssignableFrom(clazz)) { + // The field isn't a List type. + return false; + } + Type[] types = EMPTY_TYPES; + Type genericType = field.getGenericType(); + if (genericType instanceof ParameterizedType) { + types = ((ParameterizedType) field.getGenericType()).getActualTypeArguments(); + } + Type listParameter = getListParameter(clazz, types); + if (!(listParameter instanceof Class)) { + // It's a wildcard, we should allow anything in the list. + return true; + } + return elementType.isAssignableFrom((Class) listParameter); + } + + /** + * Looks up the appropriate {@link FieldType} by it's identifier. + * + * @return the {@link FieldType} or {@code null} if not found. + */ + /* @Nullable */ + public static FieldType forId(int id) { + if (id < 0 || id >= VALUES.length) { + return null; + } + return VALUES[id]; + } + + private static final FieldType[] VALUES; + private static final Type[] EMPTY_TYPES = new Type[0]; + + static { + FieldType[] values = values(); + VALUES = new FieldType[values.length]; + for (FieldType type : values) { + VALUES[type.id] = type; + } + } + + /** + * Given a class, finds a generic super class or interface that extends {@link List}. + * + * @return the generic super class/interface, or {@code null} if not found. + */ + /* @Nullable */ + private static Type getGenericSuperList(Class clazz) { + // First look at interfaces. + Type[] genericInterfaces = clazz.getGenericInterfaces(); + for (Type genericInterface : genericInterfaces) { + if (genericInterface instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericInterface; + Class rawType = (Class) parameterizedType.getRawType(); + if (List.class.isAssignableFrom(rawType)) { + return genericInterface; + } + } + } + + // Try the subclass + Type type = clazz.getGenericSuperclass(); + if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; + Class rawType = (Class) parameterizedType.getRawType(); + if (List.class.isAssignableFrom(rawType)) { + return type; + } + } + + // No super class/interface extends List. + return null; + } + + /** + * Inspects the inheritance hierarchy for the given class and finds the generic type parameter for + * {@link List}. + * + * @param clazz the class to begin the search. + * @param realTypes the array of actual type parameters for {@code clazz}. These will be used to + * substitute generic parameters up the inheritance hierarchy. If {@code clazz} does not have + * any generic parameters, this list should be empty. + * @return the {@link List} parameter. + */ + private static Type getListParameter(Class clazz, Type[] realTypes) { + top: + while (clazz != List.class) { + // First look at generic subclass and interfaces. + Type genericType = getGenericSuperList(clazz); + if (genericType instanceof ParameterizedType) { + // Replace any generic parameters with the real values. + ParameterizedType parameterizedType = (ParameterizedType) genericType; + Type[] superArgs = parameterizedType.getActualTypeArguments(); + for (int i = 0; i < superArgs.length; ++i) { + Type superArg = superArgs[i]; + if (superArg instanceof TypeVariable) { + // Get the type variables for this class so that we can match them to the variables + // used on the super class. + TypeVariable[] clazzParams = clazz.getTypeParameters(); + if (realTypes.length != clazzParams.length) { + throw new RuntimeException("Type array mismatch"); + } + + // Replace the variable parameter with the real type. + boolean foundReplacement = false; + for (int j = 0; j < clazzParams.length; ++j) { + if (superArg == clazzParams[j]) { + Type realType = realTypes[j]; + superArgs[i] = realType; + foundReplacement = true; + break; + } + } + if (!foundReplacement) { + throw new RuntimeException("Unable to find replacement for " + superArg); + } + } + } + + Class parent = (Class) parameterizedType.getRawType(); + + realTypes = superArgs; + clazz = parent; + continue; + } + + // None of the parameterized types inherit List. Just continue up the inheritance hierarchy + // toward the List interface until we can identify the parameters. + realTypes = EMPTY_TYPES; + for (Class iface : clazz.getInterfaces()) { + if (List.class.isAssignableFrom(iface)) { + clazz = iface; + continue top; + } + } + clazz = clazz.getSuperclass(); + } + + if (realTypes.length != 1) { + throw new RuntimeException("Unable to identify parameter type for List"); + } + return realTypes[0]; + } + + enum Collection { + SCALAR(false), + VECTOR(true), + PACKED_VECTOR(true), + MAP(false); + + private final boolean isList; + + Collection(boolean isList) { + this.isList = isList; + } + + /** @return the isList */ + public boolean isList() { + return isList; + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/FloatArrayList.java b/java/core/src/main/java/com/google/protobuf/FloatArrayList.java index 41749f6dbd..9d2ab71d7c 100644 --- a/java/core/src/main/java/com/google/protobuf/FloatArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/FloatArrayList.java @@ -42,11 +42,10 @@ import java.util.RandomAccess; * * @author dweis@google.com (Daniel Weis) */ -final class FloatArrayList - extends AbstractProtobufList +final class FloatArrayList extends AbstractProtobufList implements FloatList, RandomAccess, PrimitiveNonBoxingCollection { - private static final FloatArrayList EMPTY_LIST = new FloatArrayList(); + private static final FloatArrayList EMPTY_LIST = new FloatArrayList(new float[0], 0); static { EMPTY_LIST.makeImmutable(); } @@ -55,9 +54,7 @@ final class FloatArrayList return EMPTY_LIST; } - /** - * The backing store for the list. - */ + /** The backing store for the list. */ private float[] array; /** @@ -66,16 +63,13 @@ final class FloatArrayList */ private int size; - /** - * Constructs a new mutable {@code FloatArrayList} with default capacity. - */ + /** Constructs a new mutable {@code FloatArrayList} with default capacity. */ FloatArrayList() { this(new float[DEFAULT_CAPACITY], 0); } /** - * Constructs a new mutable {@code FloatArrayList} - * containing the same elements as {@code other}. + * Constructs a new mutable {@code FloatArrayList} containing the same elements as {@code other}. */ private FloatArrayList(float[] other, int size) { array = other; @@ -109,7 +103,7 @@ final class FloatArrayList final float[] arr = other.array; for (int i = 0; i < size; i++) { - if (array[i] != arr[i]) { + if (Float.floatToIntBits(array[i]) != Float.floatToIntBits(arr[i])) { return false; } } @@ -164,22 +158,34 @@ final class FloatArrayList return previousValue; } + @Override + public boolean add(Float element) { + addFloat(element); + return true; + } + @Override public void add(int index, Float element) { addFloat(index, element); } - /** - * Like {@link #add(Float)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Float)} but more efficient in that it doesn't box the element. */ @Override public void addFloat(float element) { - addFloat(size, element); + ensureIsMutable(); + if (size == array.length) { + // Resize to 1.5x the size + int length = ((size * 3) / 2) + 1; + float[] newArray = new float[length]; + + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + + array[size++] = element; } - /** - * Like {@link #add(int, Float)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(int, Float)} but more efficient in that it doesn't box the element. */ private void addFloat(int index, float element) { ensureIsMutable(); if (index < 0 || index > size) { @@ -245,7 +251,7 @@ final class FloatArrayList ensureIsMutable(); for (int i = 0; i < size; i++) { if (o.equals(array[i])) { - System.arraycopy(array, i + 1, array, i, size - i); + System.arraycopy(array, i + 1, array, i, size - i - 1); size--; modCount++; return true; @@ -260,7 +266,7 @@ final class FloatArrayList ensureIndexInRange(index); float value = array[index]; if (index < size - 1) { - System.arraycopy(array, index + 1, array, index, size - index); + System.arraycopy(array, index + 1, array, index, size - index - 1); } size--; modCount++; diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java index 60179e371d..6713f43534 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java @@ -3014,7 +3014,7 @@ public abstract class GeneratedMessage extends AbstractMessage return (Extension) extension; } - + protected static int computeStringSize(final int fieldNumber, final Object value) { if (value instanceof String) { return CodedOutputStream.computeStringSize(fieldNumber, (String) value); @@ -3022,7 +3022,7 @@ public abstract class GeneratedMessage extends AbstractMessage return CodedOutputStream.computeBytesSize(fieldNumber, (ByteString) value); } } - + protected static int computeStringSizeNoTag(final Object value) { if (value instanceof String) { return CodedOutputStream.computeStringSizeNoTag((String) value); @@ -3030,7 +3030,7 @@ public abstract class GeneratedMessage extends AbstractMessage return CodedOutputStream.computeBytesSizeNoTag((ByteString) value); } } - + protected static void writeString( CodedOutputStream output, final int fieldNumber, final Object value) throws IOException { if (value instanceof String) { @@ -3039,7 +3039,7 @@ public abstract class GeneratedMessage extends AbstractMessage output.writeBytes(fieldNumber, (ByteString) value); } } - + protected static void writeStringNoTag( CodedOutputStream output, final Object value) throws IOException { if (value instanceof String) { diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java new file mode 100755 index 0000000000..3e3efb929b --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java @@ -0,0 +1,65 @@ +// 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. + +package com.google.protobuf; + +/** A factory for message info that is generated into the message itself. */ +@ExperimentalApi +class GeneratedMessageInfoFactory implements MessageInfoFactory { + + private static final GeneratedMessageInfoFactory instance = new GeneratedMessageInfoFactory(); + + // Disallow construction - it's a singleton. + private GeneratedMessageInfoFactory() {} + + public static GeneratedMessageInfoFactory getInstance() { + return instance; + } + + @Override + public boolean isSupported(Class messageType) { + return GeneratedMessageLite.class.isAssignableFrom(messageType); + } + + @Override + public MessageInfo messageInfoFor(Class messageType) { + if (!GeneratedMessageLite.class.isAssignableFrom(messageType)) { + throw new IllegalArgumentException("Unsupported message type: " + messageType.getName()); + } + + try { + return (MessageInfo) GeneratedMessageLite.getDefaultInstance( + messageType.asSubclass(GeneratedMessageLite.class)) + .buildMessageInfo(); + } catch (Exception e) { + throw new RuntimeException("Unable to get message info for " + messageType.getName(), e); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java index ff670fd831..9248c7ed75 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java @@ -58,20 +58,14 @@ import java.util.concurrent.ConcurrentHashMap; * @author kenton@google.com Kenton Varda */ public abstract class GeneratedMessageLite< - MessageType extends GeneratedMessageLite, - BuilderType extends GeneratedMessageLite.Builder> - extends AbstractMessageLite { - // BEGIN REGULAR - static final boolean ENABLE_EXPERIMENTAL_RUNTIME_AT_BUILD_TIME = false; - // END REGULAR - // BEGIN EXPERIMENTAL - // static final boolean ENABLE_EXPERIMENTAL_RUNTIME_AT_BUILD_TIME = true; - // END EXPERIMENTAL + MessageType extends GeneratedMessageLite, + BuilderType extends GeneratedMessageLite.Builder> + extends AbstractMessageLite { /** For use by generated code only. Lazily initialized to reduce allocations. */ protected UnknownFieldSetLite unknownFields = UnknownFieldSetLite.getDefaultInstance(); - /** For use by generated code only. */ + /** For use by generated code only. */ protected int memoizedSerializedSize = -1; @Override @@ -97,11 +91,11 @@ public abstract class GeneratedMessageLite< * binary size down. The first line of the {@code toString()} representation includes a commented * version of {@code super.toString()} to act as an indicator that this should not be relied on * for comparisons. - *

- * NOTE: This method relies on the field getter methods not being stripped or renamed by proguard. - * If they are, the fields will not be included in the returned string representation. - *

- * NOTE: This implementation is liable to change in the future, and should not be relied on in + * + *

NOTE: This method relies on the field getter methods not being stripped or renamed by + * proguard. If they are, the fields will not be included in the returned string representation. + * + *

NOTE: This implementation is liable to change in the future, and should not be relied on in * code. */ @Override @@ -115,31 +109,9 @@ public abstract class GeneratedMessageLite< if (memoizedHashCode != 0) { return memoizedHashCode; } - // BEGIN EXPERIMENTAL - // memoizedHashCode = Protobuf.getInstance().schemaFor(this).hashCode(this); - // return memoizedHashCode; - // END EXPERIMENTAL - // BEGIN REGULAR - HashCodeVisitor visitor = new HashCodeVisitor(); - visit(visitor, (MessageType) this); - memoizedHashCode = visitor.hashCode; - return memoizedHashCode; - // END REGULAR - } - - // BEGIN REGULAR - @SuppressWarnings("unchecked") // Guaranteed by runtime - int hashCode(HashCodeVisitor visitor) { - if (memoizedHashCode == 0) { - int inProgressHashCode = visitor.hashCode; - visitor.hashCode = 0; - visit(visitor, (MessageType) this); - memoizedHashCode = visitor.hashCode; - visitor.hashCode = inProgressHashCode; - } + memoizedHashCode = Protobuf.getInstance().schemaFor(this).hashCode(this); return memoizedHashCode; } - // END REGULAR @SuppressWarnings("unchecked") // Guaranteed by isInstance + runtime @Override @@ -152,37 +124,9 @@ public abstract class GeneratedMessageLite< return false; } - // BEGIN EXPERIMENTAL - // return Protobuf.getInstance().schemaFor(this).equals(this, (MessageType) other); - // END EXPERIMENTAL - // BEGIN REGULAR - - try { - visit(EqualsVisitor.INSTANCE, (MessageType) other); - } catch (EqualsVisitor.NotEqualsException e) { - return false; - } - return true; - // END REGULAR + return Protobuf.getInstance().schemaFor(this).equals(this, (MessageType) other); } - // BEGIN REGULAR - /** Same as {@link #equals(Object)} but throws {@code NotEqualsException}. */ - @SuppressWarnings("unchecked") // Guaranteed by isInstance + runtime - boolean equals(EqualsVisitor visitor, MessageLite other) { - if (this == other) { - return true; - } - - if (!getDefaultInstanceForType().getClass().isInstance(other)) { - return false; - } - - visit(visitor, (MessageType) other); - return true; - } - // END REGULAR - // The general strategy for unknown fields is to use an UnknownFieldSetLite that is treated as // mutable during the parsing constructor and immutable after. This allows us to avoid // any unnecessary intermediary allocations while reducing the generated code size. @@ -209,46 +153,34 @@ public abstract class GeneratedMessageLite< return unknownFields.mergeFieldFrom(tag, input); } - /** - * Called by subclasses to parse an unknown field. For use by generated code only. - */ + /** Called by subclasses to parse an unknown field. For use by generated code only. */ protected void mergeVarintField(int tag, int value) { ensureUnknownFieldsInitialized(); unknownFields.mergeVarintField(tag, value); } - /** - * Called by subclasses to parse an unknown field. For use by generated code only. - */ + /** Called by subclasses to parse an unknown field. For use by generated code only. */ protected void mergeLengthDelimitedField(int fieldNumber, ByteString value) { ensureUnknownFieldsInitialized(); unknownFields.mergeLengthDelimitedField(fieldNumber, value); } - /** - * Called by subclasses to complete parsing. For use by generated code only. - */ + /** Called by subclasses to complete parsing. For use by generated code only. */ protected void makeImmutable() { - // BEGIN REGULAR - dynamicMethod(MethodToInvoke.MAKE_IMMUTABLE); - unknownFields.makeImmutable(); - // END REGULAR - // BEGIN EXPERIMENTAL - // Protobuf.getInstance().schemaFor(this).makeImmutable(this); - // END EXPERIMENTAL + Protobuf.getInstance().schemaFor(this).makeImmutable(this); } protected final < - MessageType extends GeneratedMessageLite, - BuilderType extends GeneratedMessageLite.Builder> - BuilderType createBuilder() { + MessageType extends GeneratedMessageLite, + BuilderType extends GeneratedMessageLite.Builder> + BuilderType createBuilder() { return (BuilderType) dynamicMethod(MethodToInvoke.NEW_BUILDER); } protected final < - MessageType extends GeneratedMessageLite, - BuilderType extends GeneratedMessageLite.Builder> - BuilderType createBuilder(MessageType prototype) { + MessageType extends GeneratedMessageLite, + BuilderType extends GeneratedMessageLite.Builder> + BuilderType createBuilder(MessageType prototype) { return ((BuilderType) createBuilder()).mergeFrom(prototype); } @@ -268,21 +200,16 @@ public abstract class GeneratedMessageLite< /** * Defines which method path to invoke in {@link GeneratedMessageLite * #dynamicMethod(MethodToInvoke, Object...)}. - *

- * For use by generated code only. + * + *

For use by generated code only. */ public static enum MethodToInvoke { - // BEGIN REGULAR - IS_INITIALIZED, - VISIT, - MERGE_FROM_STREAM, - MAKE_IMMUTABLE, - // END REGULAR // Rely on/modify instance state GET_MEMOIZED_IS_INITIALIZED, SET_MEMOIZED_IS_INITIALIZED, // Rely on static state + BUILD_MESSAGE_INFO, NEW_MUTABLE_INSTANCE, NEW_BUILDER, GET_DEFAULT_INSTANCE, @@ -296,10 +223,6 @@ public abstract class GeneratedMessageLite< * count. * *

    - *
  • {@code MERGE_FROM_STREAM} is parameterized with an {@link CodedInputStream} and {@link - * ExtensionRegistryLite}. It consumes the input stream, parsing the contents into the - * returned protocol buffer. If parsing throws an {@link InvalidProtocolBufferException}, - * the implementation wraps it in a RuntimeException. *
  • {@code NEW_INSTANCE} returns a new instance of the protocol buffer that has not yet been * made immutable. See {@code MAKE_IMMUTABLE}. *
  • {@code IS_INITIALIZED} returns {@code null} for false and the default instance for true. @@ -309,9 +232,6 @@ public abstract class GeneratedMessageLite< *
  • {@code SET_MEMOIZED_IS_INITIALIZED} sets the memoized {@code isInitilaized} byte value to * 1 if the first parameter is not null, or to 0 if the first parameter is null. *
  • {@code NEW_BUILDER} returns a {@code BuilderType} instance. - *
  • {@code VISIT} is parameterized with a {@code Visitor} and a {@code MessageType} and - * recursively iterates through the fields side by side between this and the instance. - *
  • {@code MAKE_IMMUTABLE} sets all internal fields to an immutable state. *
* * This method, plus the implementation of the Builder, enables the Builder class to be proguarded @@ -321,27 +241,16 @@ public abstract class GeneratedMessageLite< */ protected abstract Object dynamicMethod(MethodToInvoke method, Object arg0, Object arg1); - /** - * Same as {@link #dynamicMethod(MethodToInvoke, Object, Object)} with {@code null} padding. - */ + /** Same as {@link #dynamicMethod(MethodToInvoke, Object, Object)} with {@code null} padding. */ protected Object dynamicMethod(MethodToInvoke method, Object arg0) { return dynamicMethod(method, arg0, null); } - /** - * Same as {@link #dynamicMethod(MethodToInvoke, Object, Object)} with {@code null} padding. - */ + /** Same as {@link #dynamicMethod(MethodToInvoke, Object, Object)} with {@code null} padding. */ protected Object dynamicMethod(MethodToInvoke method) { return dynamicMethod(method, null, null); } - // BEGIN REGULAR - void visit(Visitor visitor, MessageType other) { - dynamicMethod(MethodToInvoke.VISIT, visitor, other); - unknownFields = visitor.visitUnknownFields(unknownFields, other.unknownFields); - } - // END REGULAR - @Override int getMemoizedSerializedSize() { return memoizedSerializedSize; @@ -352,7 +261,62 @@ public abstract class GeneratedMessageLite< memoizedSerializedSize = size; } + public void writeTo(CodedOutputStream output) throws IOException { + Protobuf.getInstance() + .schemaFor(this) + .writeTo(this, CodedOutputStreamWriter.forCodedOutput(output)); + } + + public int getSerializedSize() { + if (memoizedSerializedSize == -1) { + memoizedSerializedSize = Protobuf.getInstance().schemaFor(this).getSerializedSize(this); + } + return memoizedSerializedSize; + } + + /** Constructs a {@link MessageInfo} for this message type. */ + Object buildMessageInfo() throws Exception { + return dynamicMethod(MethodToInvoke.BUILD_MESSAGE_INFO); + } + + private static Map> defaultInstanceMap = + new ConcurrentHashMap>(); + @SuppressWarnings("unchecked") + static > T getDefaultInstance(Class clazz) { + T result = (T) defaultInstanceMap.get(clazz); + if (result == null) { + // Foo.class does not initialize the class so we need to force the initialization in order to + // get the default instance registered. + try { + Class.forName(clazz.getName(), true, clazz.getClassLoader()); + } catch (ClassNotFoundException e) { + throw new IllegalStateException("Class initialization cannot fail.", e); + } + result = (T) defaultInstanceMap.get(clazz); + } + if (result == null) { + // On some Samsung devices, this still doesn't return a valid value for some reason. We add a + // reflective fallback to keep the device running. See b/114675342. + result = (T) UnsafeUtil.allocateInstance(clazz).getDefaultInstanceForType(); + // A sanity check to ensure that was actually invoked. + if (result == null) { + throw new IllegalStateException(); + } + defaultInstanceMap.put(clazz, result); + } + return result; + } + + protected static > void registerDefaultInstance( + Class clazz, T defaultInstance) { + defaultInstanceMap.put(clazz, defaultInstance); + } + + protected static Object newMessageInfo( + MessageLite defaultInstance, String info, Object[] objects) { + return new RawMessageInfo(defaultInstance, info, objects); + } /** * Merge some unknown fields into the {@link UnknownFieldSetLite} for this message. @@ -365,9 +329,9 @@ public abstract class GeneratedMessageLite< @SuppressWarnings("unchecked") public abstract static class Builder< - MessageType extends GeneratedMessageLite, - BuilderType extends Builder> - extends AbstractMessageLite.Builder { + MessageType extends GeneratedMessageLite, + BuilderType extends Builder> + extends AbstractMessageLite.Builder { private final MessageType defaultInstance; protected MessageType instance; @@ -381,8 +345,8 @@ public abstract class GeneratedMessageLite< } /** - * Called before any method that would mutate the builder to ensure that it correctly copies - * any state before the write happens to preserve immutability guarantees. + * Called before any method that would mutate the builder to ensure that it correctly copies any + * state before the write happens to preserve immutability guarantees. */ protected void copyOnWrite() { if (isBuilt) { @@ -396,7 +360,7 @@ public abstract class GeneratedMessageLite< @Override public final boolean isInitialized() { - return GeneratedMessageLite.isInitialized(instance, false /* shouldMemoize */); + return GeneratedMessageLite.isInitialized(instance, /* shouldMemoize= */ false); } @Override @@ -408,8 +372,7 @@ public abstract class GeneratedMessageLite< @Override public BuilderType clone() { - BuilderType builder = - (BuilderType) getDefaultInstanceForType().newBuilderForType(); + BuilderType builder = (BuilderType) getDefaultInstanceForType().newBuilderForType(); builder.mergeFrom(buildPartial()); return builder; } @@ -448,12 +411,7 @@ public abstract class GeneratedMessageLite< } private void mergeFromInstance(MessageType dest, MessageType src) { - // BEGIN EXPERIMENTAL - // Protobuf.getInstance().schemaFor(dest).mergeFrom(dest, src); - // END EXPERIMENTAL - // BEGIN REGULAR - dest.visit(MergeFromVisitor.INSTANCE, src); - // END REGULAR + Protobuf.getInstance().schemaFor(dest).mergeFrom(dest, src); } @Override @@ -462,25 +420,29 @@ public abstract class GeneratedMessageLite< } @Override - public BuilderType mergeFrom(byte[] input, int offset, int length) + public BuilderType mergeFrom( + byte[] input, int offset, int length, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - // BEGIN REGULAR - return super.mergeFrom(input, offset, length); - // END REGULAR - // BEGIN EXPERIMENTAL - // copyOnWrite(); - // try { - // Protobuf.getInstance().schemaFor(instance).mergeFrom( - // instance, input, offset, offset + length, new ArrayDecoders.Registers()); - // } catch (InvalidProtocolBufferException e) { - // throw e; - // } catch (IndexOutOfBoundsException e) { - // throw InvalidProtocolBufferException.truncatedMessage(); - // } catch (IOException e) { - // throw new RuntimeException("Reading from byte array should not throw IOException.", e); - // } - // return (BuilderType) this; - // END EXPERIMENTAL + copyOnWrite(); + try { + Protobuf.getInstance().schemaFor(instance).mergeFrom( + instance, input, offset, offset + length, + new ArrayDecoders.Registers(extensionRegistry)); + } catch (InvalidProtocolBufferException e) { + throw e; + } catch (IndexOutOfBoundsException e) { + throw InvalidProtocolBufferException.truncatedMessage(); + } catch (IOException e) { + throw new RuntimeException("Reading from byte array should not throw IOException.", e); + } + return (BuilderType) this; + } + + @Override + public BuilderType mergeFrom( + byte[] input, int offset, int length) + throws InvalidProtocolBufferException { + return mergeFrom(input, offset, length, ExtensionRegistryLite.getEmptyRegistry()); } @Override @@ -490,13 +452,10 @@ public abstract class GeneratedMessageLite< throws IOException { copyOnWrite(); try { - // BEGIN REGULAR - instance.dynamicMethod(MethodToInvoke.MERGE_FROM_STREAM, input, extensionRegistry); - // END REGULAR - // BEGIN EXPERIMENTAL - // Protobuf.getInstance().schemaFor(instance).mergeFrom( - // instance, CodedInputStreamReader.forCodedInput(input), extensionRegistry); - // END EXPERIMENTAL + // TODO(yilunchong): Try to make input with type CodedInpuStream.ArrayDecoder use + // fast path. + Protobuf.getInstance().schemaFor(instance).mergeFrom( + instance, CodedInputStreamReader.forCodedInput(input), extensionRegistry); } catch (RuntimeException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); @@ -511,39 +470,31 @@ public abstract class GeneratedMessageLite< // ================================================================= // Extensions-related stuff - /** - * Lite equivalent of {@link com.google.protobuf.GeneratedMessage.ExtendableMessageOrBuilder}. - */ + /** Lite equivalent of {@link com.google.protobuf.GeneratedMessage.ExtendableMessageOrBuilder}. */ public interface ExtendableMessageOrBuilder< - MessageType extends ExtendableMessage, - BuilderType extends ExtendableBuilder> - extends MessageLiteOrBuilder { + MessageType extends ExtendableMessage, + BuilderType extends ExtendableBuilder> + extends MessageLiteOrBuilder { /** Check if a singular extension is present. */ - boolean hasExtension( - ExtensionLite extension); + boolean hasExtension(ExtensionLite extension); /** Get the number of elements in a repeated extension. */ - int getExtensionCount( - ExtensionLite> extension); + int getExtensionCount(ExtensionLite> extension); /** Get the value of an extension. */ Type getExtension(ExtensionLite extension); /** Get one element of a repeated extension. */ - Type getExtension( - ExtensionLite> extension, - int index); + Type getExtension(ExtensionLite> extension, int index); } - /** - * Lite equivalent of {@link GeneratedMessage.ExtendableMessage}. - */ + /** Lite equivalent of {@link GeneratedMessage.ExtendableMessage}. */ public abstract static class ExtendableMessage< - MessageType extends ExtendableMessage, - BuilderType extends ExtendableBuilder> - extends GeneratedMessageLite - implements ExtendableMessageOrBuilder { + MessageType extends ExtendableMessage, + BuilderType extends ExtendableBuilder> + extends GeneratedMessageLite + implements ExtendableMessageOrBuilder { /** Represents the set of extensions on this message. For use by generated code only. */ protected FieldSet extensions = FieldSet.emptySet(); @@ -556,14 +507,6 @@ public abstract class GeneratedMessageLite< extensions.mergeFrom(((ExtendableMessage) other).extensions); } - // BEGIN REGULAR - @Override - final void visit(Visitor visitor, MessageType other) { - super.visit(visitor, other); - extensions = visitor.visitExtensions(extensions, other.extensions); - } - // END REGULAR - /** * Parse an unknown field or an extension. For use by generated code only. * @@ -581,8 +524,8 @@ public abstract class GeneratedMessageLite< // TODO(dweis): How much bytecode would be saved by not requiring the generated code to // provide the default instance? - GeneratedExtension extension = extensionRegistry.findLiteExtensionByNumber( - defaultInstance, fieldNumber); + GeneratedExtension extension = + extensionRegistry.findLiteExtensionByNumber(defaultInstance, fieldNumber); return parseExtension(input, extensionRegistry, extension, tag, fieldNumber); } @@ -598,22 +541,22 @@ public abstract class GeneratedMessageLite< boolean unknown = false; boolean packed = false; if (extension == null) { - unknown = true; // Unknown field. - } else if (wireType == FieldSet.getWireFormatForFieldType( - extension.descriptor.getLiteType(), - false /* isPacked */)) { - packed = false; // Normal, unpacked value. - } else if (extension.descriptor.isRepeated && - extension.descriptor.type.isPackable() && - wireType == FieldSet.getWireFormatForFieldType( - extension.descriptor.getLiteType(), - true /* isPacked */)) { - packed = true; // Packed value. + unknown = true; // Unknown field. + } else if (wireType + == FieldSet.getWireFormatForFieldType( + extension.descriptor.getLiteType(), /* isPacked= */ false)) { + packed = false; // Normal, unpacked value. + } else if (extension.descriptor.isRepeated + && extension.descriptor.type.isPackable() + && wireType + == FieldSet.getWireFormatForFieldType( + extension.descriptor.getLiteType(), /* isPacked= */ true)) { + packed = true; // Packed value. } else { - unknown = true; // Wrong wire type. + unknown = true; // Wrong wire type. } - if (unknown) { // Unknown field or wrong wire type. Skip. + if (unknown) { // Unknown field or wrong wire type. Skip. return parseUnknownField(tag, input); } @@ -625,22 +568,20 @@ public abstract class GeneratedMessageLite< if (extension.descriptor.getLiteType() == WireFormat.FieldType.ENUM) { while (input.getBytesUntilLimit() > 0) { int rawValue = input.readEnum(); - Object value = - extension.descriptor.getEnumType().findValueByNumber(rawValue); + Object value = extension.descriptor.getEnumType().findValueByNumber(rawValue); if (value == null) { // If the number isn't recognized as a valid value for this // enum, drop it (don't even add it to unknownFields). return true; } - extensions.addRepeatedField(extension.descriptor, - extension.singularToFieldSetType(value)); + extensions.addRepeatedField( + extension.descriptor, extension.singularToFieldSetType(value)); } } else { while (input.getBytesUntilLimit() > 0) { Object value = - FieldSet.readPrimitiveField(input, - extension.descriptor.getLiteType(), - /*checkUtf8=*/ false); + FieldSet.readPrimitiveField( + input, extension.descriptor.getLiteType(), /*checkUtf8=*/ false); extensions.addRepeatedField(extension.descriptor, value); } } @@ -648,33 +589,29 @@ public abstract class GeneratedMessageLite< } else { Object value; switch (extension.descriptor.getLiteJavaType()) { - case MESSAGE: { - MessageLite.Builder subBuilder = null; - if (!extension.descriptor.isRepeated()) { - MessageLite existingValue = - (MessageLite) extensions.getField(extension.descriptor); - if (existingValue != null) { - subBuilder = existingValue.toBuilder(); + case MESSAGE: + { + MessageLite.Builder subBuilder = null; + if (!extension.descriptor.isRepeated()) { + MessageLite existingValue = (MessageLite) extensions.getField(extension.descriptor); + if (existingValue != null) { + subBuilder = existingValue.toBuilder(); + } } + if (subBuilder == null) { + subBuilder = extension.getMessageDefaultInstance().newBuilderForType(); + } + if (extension.descriptor.getLiteType() == WireFormat.FieldType.GROUP) { + input.readGroup(extension.getNumber(), subBuilder, extensionRegistry); + } else { + input.readMessage(subBuilder, extensionRegistry); + } + value = subBuilder.build(); + break; } - if (subBuilder == null) { - subBuilder = extension.getMessageDefaultInstance() - .newBuilderForType(); - } - if (extension.descriptor.getLiteType() == - WireFormat.FieldType.GROUP) { - input.readGroup(extension.getNumber(), - subBuilder, extensionRegistry); - } else { - input.readMessage(subBuilder, extensionRegistry); - } - value = subBuilder.build(); - break; - } case ENUM: int rawValue = input.readEnum(); - value = extension.descriptor.getEnumType() - .findValueByNumber(rawValue); + value = extension.descriptor.getEnumType().findValueByNumber(rawValue); // If the number isn't recognized as a valid value for this enum, // write it to unknown fields object. if (value == null) { @@ -683,23 +620,22 @@ public abstract class GeneratedMessageLite< } break; default: - value = FieldSet.readPrimitiveField(input, - extension.descriptor.getLiteType(), - /*checkUtf8=*/ false); + value = + FieldSet.readPrimitiveField( + input, extension.descriptor.getLiteType(), /*checkUtf8=*/ false); break; } if (extension.descriptor.isRepeated()) { - extensions.addRepeatedField(extension.descriptor, - extension.singularToFieldSetType(value)); + extensions.addRepeatedField( + extension.descriptor, extension.singularToFieldSetType(value)); } else { - extensions.setField(extension.descriptor, - extension.singularToFieldSetType(value)); + extensions.setField(extension.descriptor, extension.singularToFieldSetType(value)); } } return true; } - + /** * Parse an unknown field or an extension. For use by generated code only. * @@ -732,7 +668,7 @@ public abstract class GeneratedMessageLite< /** * Merges the message set from the input stream; requires message set wire format. - * + * * @param defaultInstance the default instance of the containing message we are parsing in * @param input the stream to parse from * @param extensionRegistry the registry to use when parsing @@ -836,33 +772,30 @@ public abstract class GeneratedMessageLite< subBuilder.mergeFrom(rawBytes, extensionRegistry); MessageLite value = subBuilder.build(); - ensureExtensionsAreMutable().setField( - extension.descriptor, extension.singularToFieldSetType(value)); + ensureExtensionsAreMutable() + .setField(extension.descriptor, extension.singularToFieldSetType(value)); } - private FieldSet ensureExtensionsAreMutable() { + FieldSet ensureExtensionsAreMutable() { if (extensions.isImmutable()) { extensions = extensions.clone(); } return extensions; } - private void verifyExtensionContainingType( - final GeneratedExtension extension) { - if (extension.getContainingTypeDefaultInstance() != - getDefaultInstanceForType()) { + private void verifyExtensionContainingType(final GeneratedExtension extension) { + if (extension.getContainingTypeDefaultInstance() != getDefaultInstanceForType()) { // This can only happen if someone uses unchecked operations. throw new IllegalArgumentException( - "This extension is for a different message type. Please make " + - "sure that you are not suppressing any generics type warnings."); + "This extension is for a different message type. Please make " + + "sure that you are not suppressing any generics type warnings."); } } /** Check if a singular extension is present. */ @Override public final boolean hasExtension(final ExtensionLite extension) { - GeneratedExtension extensionLite = - checkIsLite(extension); + GeneratedExtension extensionLite = checkIsLite(extension); verifyExtensionContainingType(extensionLite); return extensions.hasField(extensionLite.descriptor); @@ -872,8 +805,7 @@ public abstract class GeneratedMessageLite< @Override public final int getExtensionCount( final ExtensionLite> extension) { - GeneratedExtension> extensionLite = - checkIsLite(extension); + GeneratedExtension> extensionLite = checkIsLite(extension); verifyExtensionContainingType(extensionLite); return extensions.getRepeatedFieldCount(extensionLite.descriptor); @@ -883,8 +815,7 @@ public abstract class GeneratedMessageLite< @Override @SuppressWarnings("unchecked") public final Type getExtension(final ExtensionLite extension) { - GeneratedExtension extensionLite = - checkIsLite(extension); + GeneratedExtension extensionLite = checkIsLite(extension); verifyExtensionContainingType(extensionLite); final Object value = extensions.getField(extensionLite.descriptor); @@ -900,12 +831,12 @@ public abstract class GeneratedMessageLite< @SuppressWarnings("unchecked") public final Type getExtension( final ExtensionLite> extension, final int index) { - GeneratedExtension> extensionLite = - checkIsLite(extension); + GeneratedExtension> extensionLite = checkIsLite(extension); verifyExtensionContainingType(extensionLite); - return (Type) extensionLite.singularFromFieldSetType( - extensions.getRepeatedField(extensionLite.descriptor, index)); + return (Type) + extensionLite.singularFromFieldSetType( + extensions.getRepeatedField(extensionLite.descriptor, index)); } /** Called by subclasses to check if all extensions are initialized. */ @@ -913,27 +844,16 @@ public abstract class GeneratedMessageLite< return extensions.isInitialized(); } - @Override - protected final void makeImmutable() { - super.makeImmutable(); - // BEGIN REGULAR - extensions.makeImmutable(); - // END REGULAR - } - - /** - * Used by subclasses to serialize extensions. Extension ranges may be - * interleaved with field numbers, but we must write them in canonical - * (sorted by field number) order. ExtensionWriter helps us write - * individual ranges of extensions at once. + * Used by subclasses to serialize extensions. Extension ranges may be interleaved with field + * numbers, but we must write them in canonical (sorted by field number) order. ExtensionWriter + * helps us write individual ranges of extensions at once. */ protected class ExtensionWriter { // Imagine how much simpler this code would be if Java iterators had // a way to get the next element without advancing the iterator. - private final Iterator> iter = - extensions.iterator(); + private final Iterator> iter = extensions.iterator(); private Map.Entry next; private final boolean messageSetWireFormat; @@ -944,15 +864,13 @@ public abstract class GeneratedMessageLite< this.messageSetWireFormat = messageSetWireFormat; } - public void writeUntil(final int end, final CodedOutputStream output) - throws IOException { + public void writeUntil(final int end, final CodedOutputStream output) throws IOException { while (next != null && next.getKey().getNumber() < end) { ExtensionDescriptor extension = next.getKey(); - if (messageSetWireFormat && extension.getLiteJavaType() == - WireFormat.JavaType.MESSAGE && - !extension.isRepeated()) { - output.writeMessageSetExtension(extension.getNumber(), - (MessageLite) next.getValue()); + if (messageSetWireFormat + && extension.getLiteJavaType() == WireFormat.JavaType.MESSAGE + && !extension.isRepeated()) { + output.writeMessageSetExtension(extension.getNumber(), (MessageLite) next.getValue()); } else { FieldSet.writeField(extension, next.getValue(), output); } @@ -968,6 +886,7 @@ public abstract class GeneratedMessageLite< protected ExtensionWriter newExtensionWriter() { return new ExtensionWriter(false); } + protected ExtensionWriter newMessageSetExtensionWriter() { return new ExtensionWriter(true); } @@ -976,18 +895,17 @@ public abstract class GeneratedMessageLite< protected int extensionsSerializedSize() { return extensions.getSerializedSize(); } + protected int extensionsSerializedSizeAsMessageSet() { return extensions.getMessageSetSerializedSize(); } } - /** - * Lite equivalent of {@link GeneratedMessage.ExtendableBuilder}. - */ + /** Lite equivalent of {@link GeneratedMessage.ExtendableBuilder}. */ @SuppressWarnings("unchecked") public abstract static class ExtendableBuilder< - MessageType extends ExtendableMessage, - BuilderType extends ExtendableBuilder> + MessageType extends ExtendableMessage, + BuilderType extends ExtendableBuilder> extends Builder implements ExtendableMessageOrBuilder { protected ExtendableBuilder(MessageType defaultInstance) { @@ -1029,14 +947,12 @@ public abstract class GeneratedMessageLite< return super.buildPartial(); } - private void verifyExtensionContainingType( - final GeneratedExtension extension) { - if (extension.getContainingTypeDefaultInstance() != - getDefaultInstanceForType()) { + private void verifyExtensionContainingType(final GeneratedExtension extension) { + if (extension.getContainingTypeDefaultInstance() != getDefaultInstanceForType()) { // This can only happen if someone uses unchecked operations. throw new IllegalArgumentException( - "This extension is for a different message type. Please make " + - "sure that you are not suppressing any generics type warnings."); + "This extension is for a different message type. Please make " + + "sure that you are not suppressing any generics type warnings."); } } @@ -1070,10 +986,8 @@ public abstract class GeneratedMessageLite< /** Set the value of an extension. */ public final BuilderType setExtension( - final ExtensionLite extension, - final Type value) { - GeneratedExtension extensionLite = - checkIsLite(extension); + final ExtensionLite extension, final Type value) { + GeneratedExtension extensionLite = checkIsLite(extension); verifyExtensionContainingType(extensionLite); copyOnWrite(); @@ -1084,10 +998,8 @@ public abstract class GeneratedMessageLite< /** Set the value of one element of a repeated extension. */ public final BuilderType setExtension( - final ExtensionLite> extension, - final int index, final Type value) { - GeneratedExtension> extensionLite = - checkIsLite(extension); + final ExtensionLite> extension, final int index, final Type value) { + GeneratedExtension> extensionLite = checkIsLite(extension); verifyExtensionContainingType(extensionLite); copyOnWrite(); @@ -1099,10 +1011,8 @@ public abstract class GeneratedMessageLite< /** Append a value to a repeated extension. */ public final BuilderType addExtension( - final ExtensionLite> extension, - final Type value) { - GeneratedExtension> extensionLite = - checkIsLite(extension); + final ExtensionLite> extension, final Type value) { + GeneratedExtension> extensionLite = checkIsLite(extension); verifyExtensionContainingType(extensionLite); copyOnWrite(); @@ -1112,8 +1022,7 @@ public abstract class GeneratedMessageLite< } /** Clear an extension. */ - public final BuilderType clearExtension( - final ExtensionLite extension) { + public final BuilderType clearExtension(final ExtensionLite extension) { GeneratedExtension extensionLite = checkIsLite(extension); verifyExtensionContainingType(extensionLite); @@ -1127,50 +1036,45 @@ public abstract class GeneratedMessageLite< /** For use by generated code only. */ public static - GeneratedExtension - newSingularGeneratedExtension( - final ContainingType containingTypeDefaultInstance, - final Type defaultValue, - final MessageLite messageDefaultInstance, - final Internal.EnumLiteMap enumTypeMap, - final int number, - final WireFormat.FieldType type, - final Class singularType) { + GeneratedExtension newSingularGeneratedExtension( + final ContainingType containingTypeDefaultInstance, + final Type defaultValue, + final MessageLite messageDefaultInstance, + final Internal.EnumLiteMap enumTypeMap, + final int number, + final WireFormat.FieldType type, + final Class singularType) { return new GeneratedExtension( containingTypeDefaultInstance, defaultValue, messageDefaultInstance, - new ExtensionDescriptor(enumTypeMap, number, type, - false /* isRepeated */, - false /* isPacked */), + new ExtensionDescriptor( + enumTypeMap, number, type, /* isRepeated= */ false, /* isPacked= */ false), singularType); } /** For use by generated code only. */ public static - GeneratedExtension - newRepeatedGeneratedExtension( - final ContainingType containingTypeDefaultInstance, - final MessageLite messageDefaultInstance, - final Internal.EnumLiteMap enumTypeMap, - final int number, - final WireFormat.FieldType type, - final boolean isPacked, - final Class singularType) { - @SuppressWarnings("unchecked") // Subclasses ensure Type is a List + GeneratedExtension newRepeatedGeneratedExtension( + final ContainingType containingTypeDefaultInstance, + final MessageLite messageDefaultInstance, + final Internal.EnumLiteMap enumTypeMap, + final int number, + final WireFormat.FieldType type, + final boolean isPacked, + final Class singularType) { + @SuppressWarnings("unchecked") // Subclasses ensure Type is a List Type emptyList = (Type) Collections.emptyList(); return new GeneratedExtension( containingTypeDefaultInstance, emptyList, messageDefaultInstance, - new ExtensionDescriptor( - enumTypeMap, number, type, true /* isRepeated */, isPacked), + new ExtensionDescriptor(enumTypeMap, number, type, /* isRepeated= */ true, isPacked), singularType); } static final class ExtensionDescriptor - implements FieldSet.FieldDescriptorLite< - ExtensionDescriptor> { + implements FieldSet.FieldDescriptorLite { ExtensionDescriptor( final Internal.EnumLiteMap enumTypeMap, final int number, @@ -1242,8 +1146,8 @@ public abstract class GeneratedMessageLite< return clazz.getMethod(name, params); } catch (NoSuchMethodException e) { throw new RuntimeException( - "Generated message class \"" + clazz.getName() + - "\" missing method \"" + name + "\".", e); + "Generated message class \"" + clazz.getName() + "\" missing method \"" + name + "\".", + e); } } @@ -1253,8 +1157,7 @@ public abstract class GeneratedMessageLite< return method.invoke(object, params); } catch (IllegalAccessException e) { throw new RuntimeException( - "Couldn't use Java reflection to implement protocol message " + - "reflection.", e); + "Couldn't use Java reflection to implement protocol message reflection.", e); } catch (InvocationTargetException e) { final Throwable cause = e.getCause(); if (cause instanceof RuntimeException) { @@ -1263,7 +1166,7 @@ public abstract class GeneratedMessageLite< throw (Error) cause; } else { throw new RuntimeException( - "Unexpected exception thrown by generated accessor method.", cause); + "Unexpected exception thrown by generated accessor method.", cause); } } } @@ -1272,20 +1175,18 @@ public abstract class GeneratedMessageLite< /** * Lite equivalent to {@link GeneratedMessage.GeneratedExtension}. * - * Users should ignore the contents of this class and only use objects of - * this type as parameters to extension accessors and ExtensionRegistry.add(). + *

Users should ignore the contents of this class and only use objects of this type as + * parameters to extension accessors and ExtensionRegistry.add(). */ - public static class GeneratedExtension< - ContainingType extends MessageLite, Type> - extends ExtensionLite { + public static class GeneratedExtension + extends ExtensionLite { /** * Create a new instance with the given parameters. * - * The last parameter {@code singularType} is only needed for enum types. - * We store integer values for enum types in a {@link ExtendableMessage} - * and use Java reflection to convert an integer value back into a concrete - * enum object. + *

The last parameter {@code singularType} is only needed for enum types. We store integer + * values for enum types in a {@link ExtendableMessage} and use Java reflection to convert an + * integer value back into a concrete enum object. */ GeneratedExtension( final ContainingType containingTypeDefaultInstance, @@ -1296,13 +1197,11 @@ public abstract class GeneratedMessageLite< // Defensive checks to verify the correct initialization order of // GeneratedExtensions and their related GeneratedMessages. if (containingTypeDefaultInstance == null) { - throw new IllegalArgumentException( - "Null containingTypeDefaultInstance"); + throw new IllegalArgumentException("Null containingTypeDefaultInstance"); } - if (descriptor.getLiteType() == WireFormat.FieldType.MESSAGE && - messageDefaultInstance == null) { - throw new IllegalArgumentException( - "Null messageDefaultInstance"); + if (descriptor.getLiteType() == WireFormat.FieldType.MESSAGE + && messageDefaultInstance == null) { + throw new IllegalArgumentException("Null messageDefaultInstance"); } this.containingTypeDefaultInstance = containingTypeDefaultInstance; this.defaultValue = defaultValue; @@ -1315,9 +1214,7 @@ public abstract class GeneratedMessageLite< final MessageLite messageDefaultInstance; final ExtensionDescriptor descriptor; - /** - * Default instance of the type being extended, used to identify that type. - */ + /** Default instance of the type being extended, used to identify that type. */ public ContainingType getContainingTypeDefaultInstance() { return containingTypeDefaultInstance; } @@ -1328,10 +1225,9 @@ public abstract class GeneratedMessageLite< return descriptor.getNumber(); } - /** - * If the extension is an embedded message or group, returns the default - * instance of the message. + * If the extension is an embedded message or group, returns the default instance of the + * message. */ @Override public MessageLite getMessageDefaultInstance() { @@ -1405,8 +1301,8 @@ public abstract class GeneratedMessageLite< } /** - * A serialized (serializable) form of the generated message. Stores the - * message as a class and a byte array. + * A serialized (serializable) form of the generated message. Stores the message as a class name + * and a byte array. */ protected static final class SerializedForm implements Serializable { @@ -1424,6 +1320,7 @@ public abstract class GeneratedMessageLite< /** * Creates the serialized form by calling {@link com.google.protobuf.MessageLite#toByteArray}. + * * @param regularForm the message to serialize */ SerializedForm(MessageLite regularForm) { @@ -1433,8 +1330,9 @@ public abstract class GeneratedMessageLite< } /** - * When read from an ObjectInputStream, this method converts this object - * back to the regular form. Part of Java's serialization magic. + * When read from an ObjectInputStream, this method converts this object back to the regular + * form. Part of Java's serialization magic. + * * @return a GeneratedMessage of the type that was serialized */ @SuppressWarnings("unchecked") @@ -1445,9 +1343,7 @@ public abstract class GeneratedMessageLite< messageClass.getDeclaredField("DEFAULT_INSTANCE"); defaultInstanceField.setAccessible(true); MessageLite defaultInstance = (MessageLite) defaultInstanceField.get(null); - return defaultInstance.newBuilderForType() - .mergeFrom(asBytes) - .buildPartial(); + return defaultInstance.newBuilderForType().mergeFrom(asBytes).buildPartial(); } catch (ClassNotFoundException e) { throw new RuntimeException("Unable to find proto buffer class: " + messageClassName, e); } catch (NoSuchFieldException e) { @@ -1493,16 +1389,12 @@ public abstract class GeneratedMessageLite< } } - /** - * Checks that the {@link Extension} is Lite and returns it as a - * {@link GeneratedExtension}. - */ + /** Checks that the {@link Extension} is Lite and returns it as a {@link GeneratedExtension}. */ private static < - MessageType extends ExtendableMessage, - BuilderType extends ExtendableBuilder, - T> - GeneratedExtension checkIsLite( - ExtensionLite extension) { + MessageType extends ExtendableMessage, + BuilderType extends ExtendableBuilder, + T> + GeneratedExtension checkIsLite(ExtensionLite extension) { if (!extension.isLite()) { throw new IllegalArgumentException("Expected a lite extension."); } @@ -1512,8 +1404,8 @@ public abstract class GeneratedMessageLite< /** * A static helper method for checking if a message is initialized, optionally memoizing. - *

- * For use by generated code only. + * + *

For use by generated code only. */ protected static final > boolean isInitialized( T message, boolean shouldMemoize) { @@ -1525,13 +1417,7 @@ public abstract class GeneratedMessageLite< if (memoizedIsInitialized == 0) { return false; } - // BEGIN EXPERIMENTAL - // boolean isInitialized = Protobuf.getInstance().schemaFor(message).isInitialized(message); - // END EXPERIMENTAL - // BEGIN REGULAR - boolean isInitialized = - message.dynamicMethod(MethodToInvoke.IS_INITIALIZED, Boolean.FALSE) != null; - // END REGULAR + boolean isInitialized = Protobuf.getInstance().schemaFor(message).isInitialized(message); if (shouldMemoize) { message.dynamicMethod( MethodToInvoke.SET_MEMOIZED_IS_INITIALIZED, isInitialized ? message : null); @@ -1601,8 +1487,8 @@ public abstract class GeneratedMessageLite< /** * A {@link Parser} implementation that delegates to the default instance. - *

- * For use by generated code only. + * + *

For use by generated code only. */ protected static class DefaultInstanceBasedParser> extends AbstractParser { @@ -1620,8 +1506,11 @@ public abstract class GeneratedMessageLite< } @Override - public T parsePartialFrom(byte[] input) throws InvalidProtocolBufferException { - return GeneratedMessageLite.parsePartialFrom(defaultInstance, input); + public T parsePartialFrom( + byte[] input, int offset, int length, ExtensionRegistryLite extensionRegistry) + throws InvalidProtocolBufferException { + return GeneratedMessageLite.parsePartialFrom( + defaultInstance, input, offset, length, extensionRegistry); } } @@ -1632,25 +1521,20 @@ public abstract class GeneratedMessageLite< // TODO(dweis): Should this verify that the last tag was 0? static > T parsePartialFrom( T instance, CodedInputStream input, ExtensionRegistryLite extensionRegistry) - throws InvalidProtocolBufferException { + throws InvalidProtocolBufferException { @SuppressWarnings("unchecked") // Guaranteed by protoc T result = (T) instance.dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE); try { - // BEGIN REGULAR - result.dynamicMethod(MethodToInvoke.MERGE_FROM_STREAM, input, extensionRegistry); - // END REGULAR - // BEGIN EXPERIMENTAL - // Protobuf.getInstance().schemaFor(result).mergeFrom( - // result, CodedInputStreamReader.forCodedInput(input), extensionRegistry); - // END EXPERIMENTAL - result.makeImmutable(); - // BEGIN EXPERIMENTAL - // } catch (IOException e) { - // if (e.getCause() instanceof InvalidProtocolBufferException) { - // throw (InvalidProtocolBufferException) e.getCause(); - // } - // throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); - // END EXPERIMENTAL + // TODO(yilunchong): Try to make input with type CodedInpuStream.ArrayDecoder use + // fast path. + Schema schema = Protobuf.getInstance().schemaFor(result); + schema.mergeFrom(result, CodedInputStreamReader.forCodedInput(input), extensionRegistry); + schema.makeImmutable(result); + } catch (IOException e) { + if (e.getCause() instanceof InvalidProtocolBufferException) { + throw (InvalidProtocolBufferException) e.getCause(); + } + throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); } catch (RuntimeException e) { if (e.getCause() instanceof InvalidProtocolBufferException) { throw (InvalidProtocolBufferException) e.getCause(); @@ -1661,37 +1545,32 @@ public abstract class GeneratedMessageLite< } /** A static helper method for parsing a partial from byte array. */ - static > T parsePartialFrom(T instance, byte[] input) + static > T parsePartialFrom( + T instance, byte[] input, int offset, int length, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - // BEGIN REGULAR - return parsePartialFrom(instance, input, ExtensionRegistryLite.getEmptyRegistry()); - // END REGULAR - // BEGIN EXPERIMENTAL - // @SuppressWarnings("unchecked") // Guaranteed by protoc - // T result = (T) instance.dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE); - // try { - // Protobuf.getInstance().schemaFor(result).mergeFrom( - // result, input, 0, input.length, new ArrayDecoders.Registers()); - // result.makeImmutable(); - // if (result.memoizedHashCode != 0) { - // throw new RuntimeException(); - // } - // } catch (IOException e) { - // if (e.getCause() instanceof InvalidProtocolBufferException) { - // throw (InvalidProtocolBufferException) e.getCause(); - // } - // throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); - // } catch (IndexOutOfBoundsException e) { - // throw InvalidProtocolBufferException.truncatedMessage().setUnfinishedMessage(result); - // } - // return result; - // END EXPERIMENTAL + @SuppressWarnings("unchecked") // Guaranteed by protoc + T result = (T) instance.dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE); + try { + Schema schema = Protobuf.getInstance().schemaFor(result); + schema.mergeFrom( + result, input, offset, offset + length, new ArrayDecoders.Registers(extensionRegistry)); + schema.makeImmutable(result); + if (result.memoizedHashCode != 0) { + throw new RuntimeException(); + } + } catch (IOException e) { + if (e.getCause() instanceof InvalidProtocolBufferException) { + throw (InvalidProtocolBufferException) e.getCause(); + } + throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); + } catch (IndexOutOfBoundsException e) { + throw InvalidProtocolBufferException.truncatedMessage().setUnfinishedMessage(result); + } + return result; } protected static > T parsePartialFrom( - T defaultInstance, - CodedInputStream input) - throws InvalidProtocolBufferException { + T defaultInstance, CodedInputStream input) throws InvalidProtocolBufferException { return parsePartialFrom(defaultInstance, input, ExtensionRegistryLite.getEmptyRegistry()); } @@ -1704,7 +1583,8 @@ public abstract class GeneratedMessageLite< private static > T checkMessageInitialized(T message) throws InvalidProtocolBufferException { if (message != null && !message.isInitialized()) { - throw message.newUninitializedMessageException() + throw message + .newUninitializedMessageException() .asInvalidProtocolBufferException() .setUnfinishedMessage(message); } @@ -1727,8 +1607,7 @@ public abstract class GeneratedMessageLite< // Validates last tag. protected static > T parseFrom( - T defaultInstance, ByteString data) - throws InvalidProtocolBufferException { + T defaultInstance, ByteString data) throws InvalidProtocolBufferException { return checkMessageInitialized( parseFrom(defaultInstance, data, ExtensionRegistryLite.getEmptyRegistry())); } @@ -1765,41 +1644,32 @@ public abstract class GeneratedMessageLite< private static > T parsePartialFrom( T defaultInstance, byte[] data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - T message; - try { - CodedInputStream input = CodedInputStream.newInstance(data); - message = parsePartialFrom(defaultInstance, input, extensionRegistry); - try { - input.checkLastTagWas(0); - } catch (InvalidProtocolBufferException e) { - throw e.setUnfinishedMessage(message); - } - return message; - } catch (InvalidProtocolBufferException e) { - throw e; - } + return checkMessageInitialized( + parsePartialFrom(defaultInstance, data, 0, data.length, extensionRegistry)); } // Validates last tag. protected static > T parseFrom( - T defaultInstance, byte[] data) - throws InvalidProtocolBufferException { - return checkMessageInitialized(parsePartialFrom(defaultInstance, data)); + T defaultInstance, byte[] data) throws InvalidProtocolBufferException { + return checkMessageInitialized(parsePartialFrom( + defaultInstance, data, 0, data.length, ExtensionRegistryLite.getEmptyRegistry())); } // Validates last tag. protected static > T parseFrom( T defaultInstance, byte[] data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - return checkMessageInitialized(parsePartialFrom(defaultInstance, data, extensionRegistry)); + return checkMessageInitialized( + parsePartialFrom(defaultInstance, data, 0, data.length, extensionRegistry)); } // Does not validate last tag. protected static > T parseFrom( - T defaultInstance, InputStream input) - throws InvalidProtocolBufferException { + T defaultInstance, InputStream input) throws InvalidProtocolBufferException { return checkMessageInitialized( - parsePartialFrom(defaultInstance, CodedInputStream.newInstance(input), + parsePartialFrom( + defaultInstance, + CodedInputStream.newInstance(input), ExtensionRegistryLite.getEmptyRegistry())); } @@ -1813,8 +1683,7 @@ public abstract class GeneratedMessageLite< // Does not validate last tag. protected static > T parseFrom( - T defaultInstance, CodedInputStream input) - throws InvalidProtocolBufferException { + T defaultInstance, CodedInputStream input) throws InvalidProtocolBufferException { return parseFrom(defaultInstance, input, ExtensionRegistryLite.getEmptyRegistry()); } @@ -1822,17 +1691,15 @@ public abstract class GeneratedMessageLite< protected static > T parseFrom( T defaultInstance, CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - return checkMessageInitialized( - parsePartialFrom(defaultInstance, input, extensionRegistry)); + return checkMessageInitialized(parsePartialFrom(defaultInstance, input, extensionRegistry)); } // Validates last tag. protected static > T parseDelimitedFrom( - T defaultInstance, InputStream input) - throws InvalidProtocolBufferException { + T defaultInstance, InputStream input) throws InvalidProtocolBufferException { return checkMessageInitialized( - parsePartialDelimitedFrom(defaultInstance, input, - ExtensionRegistryLite.getEmptyRegistry())); + parsePartialDelimitedFrom( + defaultInstance, input, ExtensionRegistryLite.getEmptyRegistry())); } // Validates last tag. @@ -1844,9 +1711,7 @@ public abstract class GeneratedMessageLite< } private static > T parsePartialDelimitedFrom( - T defaultInstance, - InputStream input, - ExtensionRegistryLite extensionRegistry) + T defaultInstance, InputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { int size; try { @@ -1868,692 +1733,4 @@ public abstract class GeneratedMessageLite< } return message; } - - // BEGIN REGULAR - /** - * An abstract visitor that the generated code calls into that we use to implement various - * features. Fields that are not members of oneofs are always visited. Members of a oneof are only - * visited when they are the set oneof case value on the "other" proto. The visitOneofNotSet - * method is invoked if other's oneof case is not set. - */ - protected interface Visitor { - boolean visitBoolean(boolean minePresent, boolean mine, boolean otherPresent, boolean other); - int visitInt(boolean minePresent, int mine, boolean otherPresent, int other); - double visitDouble(boolean minePresent, double mine, boolean otherPresent, double other); - float visitFloat(boolean minePresent, float mine, boolean otherPresent, float other); - long visitLong(boolean minePresent, long mine, boolean otherPresent, long other); - String visitString(boolean minePresent, String mine, boolean otherPresent, String other); - ByteString visitByteString( - boolean minePresent, ByteString mine, boolean otherPresent, ByteString other); - - Object visitOneofBoolean(boolean minePresent, Object mine, Object other); - Object visitOneofInt(boolean minePresent, Object mine, Object other); - Object visitOneofDouble(boolean minePresent, Object mine, Object other); - Object visitOneofFloat(boolean minePresent, Object mine, Object other); - Object visitOneofLong(boolean minePresent, Object mine, Object other); - Object visitOneofString(boolean minePresent, Object mine, Object other); - Object visitOneofByteString(boolean minePresent, Object mine, Object other); - Object visitOneofMessage(boolean minePresent, Object mine, Object other); - void visitOneofNotSet(boolean minePresent); - - /** - * Message fields use null sentinals. - */ - T visitMessage(T mine, T other); - - ProtobufList visitList(ProtobufList mine, ProtobufList other); - BooleanList visitBooleanList(BooleanList mine, BooleanList other); - IntList visitIntList(IntList mine, IntList other); - DoubleList visitDoubleList(DoubleList mine, DoubleList other); - FloatList visitFloatList(FloatList mine, FloatList other); - LongList visitLongList(LongList mine, LongList other); - FieldSet visitExtensions( - FieldSet mine, FieldSet other); - UnknownFieldSetLite visitUnknownFields(UnknownFieldSetLite mine, UnknownFieldSetLite other); - MapFieldLite visitMap(MapFieldLite mine, MapFieldLite other); - } - - /** - * Implements equals. Throws a {@link NotEqualsException} when not equal. - */ - static class EqualsVisitor implements Visitor { - - static final class NotEqualsException extends RuntimeException {} - - static final EqualsVisitor INSTANCE = new EqualsVisitor(); - - static final NotEqualsException NOT_EQUALS = new NotEqualsException(); - - private EqualsVisitor() {} - - @Override - public boolean visitBoolean( - boolean minePresent, boolean mine, boolean otherPresent, boolean other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public int visitInt(boolean minePresent, int mine, boolean otherPresent, int other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public double visitDouble( - boolean minePresent, double mine, boolean otherPresent, double other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public float visitFloat(boolean minePresent, float mine, boolean otherPresent, float other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public long visitLong(boolean minePresent, long mine, boolean otherPresent, long other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public String visitString( - boolean minePresent, String mine, boolean otherPresent, String other) { - if (minePresent != otherPresent || !mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public ByteString visitByteString( - boolean minePresent, ByteString mine, boolean otherPresent, ByteString other) { - if (minePresent != otherPresent || !mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public Object visitOneofBoolean(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofInt(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofDouble(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofFloat(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofLong(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofString(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofByteString(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofMessage(boolean minePresent, Object mine, Object other) { - if (minePresent && ((GeneratedMessageLite) mine).equals(this, (MessageLite) other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public void visitOneofNotSet(boolean minePresent) { - if (minePresent) { - throw NOT_EQUALS; - } - } - - @Override - public T visitMessage(T mine, T other) { - if (mine == null && other == null) { - return null; - } - - if (mine == null || other == null) { - throw NOT_EQUALS; - } - - ((GeneratedMessageLite) mine).equals(this, other); - - return mine; - } - - @Override - public ProtobufList visitList(ProtobufList mine, ProtobufList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public BooleanList visitBooleanList(BooleanList mine, BooleanList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public IntList visitIntList(IntList mine, IntList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public DoubleList visitDoubleList(DoubleList mine, DoubleList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public FloatList visitFloatList(FloatList mine, FloatList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public LongList visitLongList(LongList mine, LongList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public FieldSet visitExtensions( - FieldSet mine, - FieldSet other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public UnknownFieldSetLite visitUnknownFields( - UnknownFieldSetLite mine, - UnknownFieldSetLite other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public MapFieldLite visitMap(MapFieldLite mine, MapFieldLite other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - } - - /** - * Implements hashCode by accumulating state. - */ - static class HashCodeVisitor implements Visitor { - - // The caller must ensure that the visitor is invoked parameterized with this and this such that - // other is this. This is required due to how oneof cases are handled. See the class comment - // on Visitor for more information. - - int hashCode = 0; - - @Override - public boolean visitBoolean( - boolean minePresent, boolean mine, boolean otherPresent, boolean other) { - hashCode = (53 * hashCode) + Internal.hashBoolean(mine); - return mine; - } - - @Override - public int visitInt(boolean minePresent, int mine, boolean otherPresent, int other) { - hashCode = (53 * hashCode) + mine; - return mine; - } - - @Override - public double visitDouble( - boolean minePresent, double mine, boolean otherPresent, double other) { - hashCode = (53 * hashCode) + Internal.hashLong(Double.doubleToLongBits(mine)); - return mine; - } - - @Override - public float visitFloat(boolean minePresent, float mine, boolean otherPresent, float other) { - hashCode = (53 * hashCode) + Float.floatToIntBits(mine); - return mine; - } - - @Override - public long visitLong(boolean minePresent, long mine, boolean otherPresent, long other) { - hashCode = (53 * hashCode) + Internal.hashLong(mine); - return mine; - } - - @Override - public String visitString( - boolean minePresent, String mine, boolean otherPresent, String other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public ByteString visitByteString( - boolean minePresent, ByteString mine, boolean otherPresent, ByteString other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public Object visitOneofBoolean(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + Internal.hashBoolean(((Boolean) mine)); - return mine; - } - - @Override - public Object visitOneofInt(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + (Integer) mine; - return mine; - } - - @Override - public Object visitOneofDouble(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + Internal.hashLong(Double.doubleToLongBits((Double) mine)); - return mine; - } - - @Override - public Object visitOneofFloat(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + Float.floatToIntBits((Float) mine); - return mine; - } - - @Override - public Object visitOneofLong(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + Internal.hashLong((Long) mine); - return mine; - } - - @Override - public Object visitOneofString(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public Object visitOneofByteString(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public Object visitOneofMessage(boolean minePresent, Object mine, Object other) { - return visitMessage((MessageLite) mine, (MessageLite) other); - } - - @Override - public void visitOneofNotSet(boolean minePresent) { - if (minePresent) { - throw new IllegalStateException(); // Can't happen if other == this. - } - } - - @Override - public T visitMessage(T mine, T other) { - final int protoHash; - if (mine != null) { - if (mine instanceof GeneratedMessageLite) { - protoHash = ((GeneratedMessageLite) mine).hashCode(this); - } else { - protoHash = mine.hashCode(); - } - } else { - protoHash = 37; - } - hashCode = (53 * hashCode) + protoHash; - return mine; - } - - @Override - public ProtobufList visitList(ProtobufList mine, ProtobufList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public BooleanList visitBooleanList(BooleanList mine, BooleanList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public IntList visitIntList(IntList mine, IntList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public DoubleList visitDoubleList(DoubleList mine, DoubleList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public FloatList visitFloatList(FloatList mine, FloatList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public LongList visitLongList(LongList mine, LongList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public FieldSet visitExtensions( - FieldSet mine, - FieldSet other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public UnknownFieldSetLite visitUnknownFields( - UnknownFieldSetLite mine, - UnknownFieldSetLite other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public MapFieldLite visitMap(MapFieldLite mine, MapFieldLite other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - } - - /** - * Implements field merging semantics over the visitor interface. - */ - protected static class MergeFromVisitor implements Visitor { - - public static final MergeFromVisitor INSTANCE = new MergeFromVisitor(); - - private MergeFromVisitor() {} - - @Override - public boolean visitBoolean( - boolean minePresent, boolean mine, boolean otherPresent, boolean other) { - return otherPresent ? other : mine; - } - - @Override - public int visitInt(boolean minePresent, int mine, boolean otherPresent, int other) { - return otherPresent ? other : mine; - } - - @Override - public double visitDouble( - boolean minePresent, double mine, boolean otherPresent, double other) { - return otherPresent ? other : mine; - } - - @Override - public float visitFloat(boolean minePresent, float mine, boolean otherPresent, float other) { - return otherPresent ? other : mine; - } - - @Override - public long visitLong(boolean minePresent, long mine, boolean otherPresent, long other) { - return otherPresent ? other : mine; - } - - @Override - public String visitString( - boolean minePresent, String mine, boolean otherPresent, String other) { - return otherPresent ? other : mine; - } - - @Override - public ByteString visitByteString( - boolean minePresent, ByteString mine, boolean otherPresent, ByteString other) { - return otherPresent ? other : mine; - } - - @Override - public Object visitOneofBoolean(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofInt(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofDouble(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofFloat(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofLong(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofString(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofByteString(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofMessage(boolean minePresent, Object mine, Object other) { - if (minePresent) { - return visitMessage((MessageLite) mine, (MessageLite) other); - } - return other; - } - - @Override - public void visitOneofNotSet(boolean minePresent) { - return; - } - - @SuppressWarnings("unchecked") // Guaranteed by runtime. - @Override - public T visitMessage(T mine, T other) { - if (mine != null && other != null) { - return (T) mine.toBuilder().mergeFrom(other).build(); - } - - return mine != null ? mine : other; - } - - @Override - public ProtobufList visitList(ProtobufList mine, ProtobufList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public BooleanList visitBooleanList(BooleanList mine, BooleanList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public IntList visitIntList(IntList mine, IntList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public DoubleList visitDoubleList(DoubleList mine, DoubleList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public FloatList visitFloatList(FloatList mine, FloatList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public LongList visitLongList(LongList mine, LongList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public FieldSet visitExtensions( - FieldSet mine, - FieldSet other) { - if (mine.isImmutable()) { - mine = mine.clone(); - } - mine.mergeFrom(other); - return mine; - } - - @Override - public UnknownFieldSetLite visitUnknownFields( - UnknownFieldSetLite mine, - UnknownFieldSetLite other) { - return other == UnknownFieldSetLite.getDefaultInstance() - ? mine : UnknownFieldSetLite.mutableCopyOf(mine, other); - } - - @Override - public MapFieldLite visitMap(MapFieldLite mine, MapFieldLite other) { - if (!other.isEmpty()) { - if (!mine.isMutable()) { - mine = mine.mutableCopy(); - } - mine.mergeFrom(other); - } - return mine; - } - } - // END REGULAR } diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java index 53af544fc3..bea008c85c 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java @@ -58,6 +58,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.ObjectStreamException; import java.io.Serializable; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; @@ -79,6 +81,12 @@ import java.util.TreeMap; public abstract class GeneratedMessageV3 extends AbstractMessage implements Serializable { private static final long serialVersionUID = 1L; + // Whether to use reflection for FieldAccessor + private static boolean forTestUseReflection = false; + + static void setForTestUseReflection(boolean useReflection) { + forTestUseReflection = useReflection; + } /** * For testing. Allows a test to disable the optimization that avoids using @@ -105,13 +113,20 @@ public abstract class GeneratedMessageV3 extends AbstractMessage } /** - * For testing. Allows a test to disable the optimization that avoids using - * field builders for nested messages until they are requested. By disabling - * this optimization, existing tests can be reused to test the field builders. - * See {@link RepeatedFieldBuilder} and {@link SingleFieldBuilder}. + * @see #setAlwaysUseFieldBuildersForTesting(boolean) */ static void enableAlwaysUseFieldBuildersForTesting() { - alwaysUseFieldBuilders = true; + setAlwaysUseFieldBuildersForTesting(true); + } + + /** + * For testing. Allows a test to disable/re-enable the optimization that avoids + * using field builders for nested messages until they are requested. By disabling + * this optimization, existing tests can be reused to test the field builders. + * See {@link RepeatedFieldBuilder} and {@link SingleFieldBuilder}. + */ + static void setAlwaysUseFieldBuildersForTesting(boolean useBuilders) { + alwaysUseFieldBuilders = useBuilders; } /** @@ -126,6 +141,21 @@ public abstract class GeneratedMessageV3 extends AbstractMessage return internalGetFieldAccessorTable().descriptor; } + protected void mergeFromAndMakeImmutableInternal( + CodedInputStream input, ExtensionRegistryLite extensionRegistry) + throws InvalidProtocolBufferException { + Schema schema = + (Schema) Protobuf.getInstance().schemaFor(this); + try { + schema.mergeFrom(this, CodedInputStreamReader.forCodedInput(input), extensionRegistry); + } catch (InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (IOException e) { + throw new InvalidProtocolBufferException(e).setUnfinishedMessage(this); + } + schema.makeImmutable(this); + } + /** * Internal helper to return a modifiable map containing all the fields. * The returned Map is modifialbe so that the caller can add additional @@ -364,7 +394,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage throw e.unwrapIOException(); } } - + protected static boolean canUseUnsafe() { return UnsafeUtil.hasUnsafeArrayOperations() && UnsafeUtil.hasUnsafeByteBufferOperations(); } @@ -439,6 +469,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); } + @Override public void writeTo(final CodedOutputStream output) throws IOException { MessageReflection.writeMessageTo(this, getAllFieldsRaw(), output, false); @@ -458,6 +489,29 @@ public abstract class GeneratedMessageV3 extends AbstractMessage + /** + * This class is used to make a generated protected method inaccessible from user's code (e.g., + * the {@link #newInstance} method below). When this class is used as a parameter's type in a + * generated protected method, the method is visible to user's code in the same package, but + * since the constructor of this class is private to protobuf runtime, user's code can't obtain + * an instance of this class and as such can't actually make a method call on the protected + * method. + */ + protected static final class UnusedPrivateParameter { + static final UnusedPrivateParameter INSTANCE = new UnusedPrivateParameter(); + + private UnusedPrivateParameter() { + } + } + + /** + * Creates a new instance of this message type. Overridden in the generated code. + */ + @SuppressWarnings({"unused"}) + protected Object newInstance(UnusedPrivateParameter unused) { + throw new UnsupportedOperationException("This method must be overridden by the subclass."); + } + /** * Used by parsing constructors in generated classes. */ @@ -710,19 +764,23 @@ public abstract class GeneratedMessageV3 extends AbstractMessage return (BuilderType) this; } - @Override - public BuilderType setUnknownFields(final UnknownFieldSet unknownFields) { + private BuilderType setUnknownFieldsInternal(final UnknownFieldSet unknownFields) { this.unknownFields = unknownFields; onChanged(); return (BuilderType) this; } + @Override + public BuilderType setUnknownFields(final UnknownFieldSet unknownFields) { + return setUnknownFieldsInternal(unknownFields); + } + /** - * Delegates to setUnknownFields. This method is obsolete, but we must retain it for - * compatibility with older generated code. + * This method is obsolete, but we must retain it for compatibility with + * older generated code. */ protected BuilderType setUnknownFieldsProto3(final UnknownFieldSet unknownFields) { - return setUnknownFields(unknownFields); + return setUnknownFieldsInternal(unknownFields); } @Override @@ -1681,6 +1739,15 @@ public abstract class GeneratedMessageV3 extends AbstractMessage } } + @Override + public Message.Builder newBuilderForField(final FieldDescriptor field) { + if (field.isExtension()) { + return DynamicMessage.newBuilder(field.getMessageType()); + } else { + return super.newBuilderForField(field); + } + } + protected final void mergeExtensionFields(final ExtendableMessage other) { ensureExtensionsIsMutable(); extensions.mergeFrom(other.extensions); @@ -1743,6 +1810,22 @@ public abstract class GeneratedMessageV3 extends AbstractMessage } } + /** Calls invoke and throws a RuntimeException if it fails. */ + private static RuntimeException handleException(Throwable e) { + if (e instanceof ClassCastException) { + // Reflection throws a bad param type as an IllegalArgumentException, whereas MethodHandle + // throws it as a ClassCastException; convert for backwards compatibility + throw new IllegalArgumentException(e); + } else if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } else if (e instanceof Error) { + throw (Error) e; + } else { + throw new RuntimeException( + "Unexpected exception thrown by generated accessor method.", e); + } + } + /** * Gets the map field with the given field number. This method should be * overridden in the generated message class if the message contains map @@ -1989,61 +2072,250 @@ public abstract class GeneratedMessageV3 extends AbstractMessage // --------------------------------------------------------------- private static class SingularFieldAccessor implements FieldAccessor { + private interface MethodInvoker { + Object get(final GeneratedMessageV3 message); + + Object get(GeneratedMessageV3.Builder builder); + + int getOneofFieldNumber(final GeneratedMessageV3 message); + + int getOneofFieldNumber(final GeneratedMessageV3.Builder builder); + + void set(final GeneratedMessageV3.Builder builder, final Object value); + + boolean has(final GeneratedMessageV3 message); + + boolean has(GeneratedMessageV3.Builder builder); + + void clear(final GeneratedMessageV3.Builder builder); + } + + private static final class ReflectionInvoker implements MethodInvoker { + protected final Method getMethod; + protected final Method getMethodBuilder; + protected final Method setMethod; + protected final Method hasMethod; + protected final Method hasMethodBuilder; + protected final Method clearMethod; + protected final Method caseMethod; + protected final Method caseMethodBuilder; + + ReflectionInvoker( + final FieldDescriptor descriptor, + final String camelCaseName, + final Class messageClass, + final Class builderClass, + final String containingOneofCamelCaseName, + boolean isOneofField, + boolean hasHasMethod) { + getMethod = getMethodOrDie(messageClass, "get" + camelCaseName); + getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName); + Class type = getMethod.getReturnType(); + setMethod = getMethodOrDie(builderClass, "set" + camelCaseName, type); + hasMethod = hasHasMethod ? getMethodOrDie(messageClass, "has" + camelCaseName) : null; + hasMethodBuilder = + hasHasMethod ? getMethodOrDie(builderClass, "has" + camelCaseName) : null; + clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); + caseMethod = + isOneofField + ? getMethodOrDie(messageClass, "get" + containingOneofCamelCaseName + "Case") + : null; + caseMethodBuilder = + isOneofField + ? getMethodOrDie(builderClass, "get" + containingOneofCamelCaseName + "Case") + : null; + } + + @Override + public Object get(final GeneratedMessageV3 message) { + return invokeOrDie(getMethod, message); + } + + @Override + public Object get(GeneratedMessageV3.Builder builder) { + return invokeOrDie(getMethodBuilder, builder); + } + + @Override + public int getOneofFieldNumber(final GeneratedMessageV3 message) { + return ((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber(); + } + + @Override + public int getOneofFieldNumber(final GeneratedMessageV3.Builder builder) { + return ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).getNumber(); + } + + @Override + public void set(final GeneratedMessageV3.Builder builder, final Object value) { + invokeOrDie(setMethod, builder, value); + } + + @Override + public boolean has(final GeneratedMessageV3 message) { + return (Boolean) invokeOrDie(hasMethod, message); + } + + @Override + public boolean has(GeneratedMessageV3.Builder builder) { + return (Boolean) invokeOrDie(hasMethodBuilder, builder); + } + + @Override + public void clear(final GeneratedMessageV3.Builder builder) { + invokeOrDie(clearMethod, builder); + } + } + + private static final class MethodHandleInvoker implements MethodInvoker { + protected final MethodHandle getMethod; + protected final MethodHandle getMethodBuilder; + protected final MethodHandle setMethod; + protected final MethodHandle hasMethod; + protected final MethodHandle hasMethodBuilder; + protected final MethodHandle clearMethod; + protected final MethodHandle caseMethod; + protected final MethodHandle caseMethodBuilder; + + MethodHandleInvoker(ReflectionInvoker accessor) throws IllegalAccessException { + MethodHandles.Lookup lookup = MethodHandles.publicLookup(); + + this.getMethod = lookup.unreflect(accessor.getMethod); + this.getMethodBuilder = lookup.unreflect(accessor.getMethodBuilder); + this.setMethod = lookup.unreflect(accessor.setMethod); + this.hasMethod = + (accessor.hasMethod != null) ? lookup.unreflect(accessor.hasMethod) : null; + this.hasMethodBuilder = (accessor.hasMethodBuilder != null) + ? lookup.unreflect(accessor.hasMethodBuilder) : null; + this.clearMethod = lookup.unreflect(accessor.clearMethod); + this.caseMethod = + (accessor.caseMethod != null) ? lookup.unreflect(accessor.caseMethod) : null; + this.caseMethodBuilder = (accessor.caseMethodBuilder != null) + ? lookup.unreflect(accessor.caseMethodBuilder) : null; + } + + @Override + public Object get(final GeneratedMessageV3 message) { + try { + return getMethod.invoke(message); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public Object get(GeneratedMessageV3.Builder builder) { + try { + return getMethodBuilder.invoke(builder); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public int getOneofFieldNumber(final GeneratedMessageV3 message) { + try { + return ((Internal.EnumLite) caseMethod.invoke(message)).getNumber(); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public int getOneofFieldNumber(final GeneratedMessageV3.Builder builder) { + try { + return ((Internal.EnumLite) caseMethodBuilder.invoke(builder)).getNumber(); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public void set(final GeneratedMessageV3.Builder builder, final Object value) { + try { + setMethod.invoke(builder, value); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public boolean has(final GeneratedMessageV3 message) { + try { + return (Boolean) hasMethod.invoke(message); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public boolean has(GeneratedMessageV3.Builder builder) { + try { + return (Boolean) hasMethodBuilder.invoke(builder); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public void clear(final GeneratedMessageV3.Builder builder) { + try { + clearMethod.invoke(builder); + } catch (Throwable e) { + throw handleException(e); + } + } + } + SingularFieldAccessor( final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, final Class builderClass, final String containingOneofCamelCaseName) { - field = descriptor; isOneofField = descriptor.getContainingOneof() != null; hasHasMethod = supportFieldPresence(descriptor.getFile()) || (!isOneofField && descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE); - getMethod = getMethodOrDie(messageClass, "get" + camelCaseName); - getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName); - type = getMethod.getReturnType(); - setMethod = getMethodOrDie(builderClass, "set" + camelCaseName, type); - hasMethod = - hasHasMethod ? getMethodOrDie(messageClass, "has" + camelCaseName) : null; - hasMethodBuilder = - hasHasMethod ? getMethodOrDie(builderClass, "has" + camelCaseName) : null; - clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); - caseMethod = isOneofField ? getMethodOrDie( - messageClass, "get" + containingOneofCamelCaseName + "Case") : null; - caseMethodBuilder = isOneofField ? getMethodOrDie( - builderClass, "get" + containingOneofCamelCaseName + "Case") : null; + ReflectionInvoker reflectionInvoker = + new ReflectionInvoker( + descriptor, + camelCaseName, + messageClass, + builderClass, + containingOneofCamelCaseName, + isOneofField, + hasHasMethod); + field = descriptor; + type = reflectionInvoker.getMethod.getReturnType(); + invoker = tryGetMethodHandleInvoke(reflectionInvoker); + } + + static MethodInvoker tryGetMethodHandleInvoke(ReflectionInvoker accessor) { + if (forTestUseReflection) { + return accessor; + } + try { + return new MethodHandleInvoker(accessor); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } } // Note: We use Java reflection to call public methods rather than // access private fields directly as this avoids runtime security // checks. protected final Class type; - protected final Method getMethod; - protected final Method getMethodBuilder; - protected final Method setMethod; - protected final Method hasMethod; - protected final Method hasMethodBuilder; - protected final Method clearMethod; - protected final Method caseMethod; - protected final Method caseMethodBuilder; protected final FieldDescriptor field; protected final boolean isOneofField; protected final boolean hasHasMethod; - - private int getOneofFieldNumber(final GeneratedMessageV3 message) { - return ((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber(); - } - - private int getOneofFieldNumber(final GeneratedMessageV3.Builder builder) { - return ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).getNumber(); - } + protected final MethodInvoker invoker; @Override public Object get(final GeneratedMessageV3 message) { - return invokeOrDie(getMethod, message); + return invoker.get(message); } @Override public Object get(GeneratedMessageV3.Builder builder) { - return invokeOrDie(getMethodBuilder, builder); + return invoker.get(builder); } @Override public Object getRaw(final GeneratedMessageV3 message) { @@ -2055,134 +2327,324 @@ public abstract class GeneratedMessageV3 extends AbstractMessage } @Override public void set(final Builder builder, final Object value) { - invokeOrDie(setMethod, builder, value); + invoker.set(builder, value); } @Override public Object getRepeated(final GeneratedMessageV3 message, final int index) { - throw new UnsupportedOperationException( - "getRepeatedField() called on a singular field."); + throw new UnsupportedOperationException("getRepeatedField() called on a singular field."); } @Override public Object getRepeatedRaw(final GeneratedMessageV3 message, final int index) { throw new UnsupportedOperationException( - "getRepeatedFieldRaw() called on a singular field."); + "getRepeatedFieldRaw() called on a singular field."); } @Override public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { - throw new UnsupportedOperationException( - "getRepeatedField() called on a singular field."); + throw new UnsupportedOperationException("getRepeatedField() called on a singular field."); } @Override public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index) { throw new UnsupportedOperationException( - "getRepeatedFieldRaw() called on a singular field."); + "getRepeatedFieldRaw() called on a singular field."); } @Override public void setRepeated(final Builder builder, final int index, final Object value) { - throw new UnsupportedOperationException( - "setRepeatedField() called on a singular field."); + throw new UnsupportedOperationException("setRepeatedField() called on a singular field."); } @Override public void addRepeated(final Builder builder, final Object value) { - throw new UnsupportedOperationException( - "addRepeatedField() called on a singular field."); + throw new UnsupportedOperationException("addRepeatedField() called on a singular field."); } @Override public boolean has(final GeneratedMessageV3 message) { if (!hasHasMethod) { if (isOneofField) { - return getOneofFieldNumber(message) == field.getNumber(); + return invoker.getOneofFieldNumber(message) == field.getNumber(); } return !get(message).equals(field.getDefaultValue()); } - return (Boolean) invokeOrDie(hasMethod, message); + return invoker.has(message); } @Override public boolean has(GeneratedMessageV3.Builder builder) { if (!hasHasMethod) { if (isOneofField) { - return getOneofFieldNumber(builder) == field.getNumber(); + return invoker.getOneofFieldNumber(builder) == field.getNumber(); } return !get(builder).equals(field.getDefaultValue()); } - return (Boolean) invokeOrDie(hasMethodBuilder, builder); + return invoker.has(builder); } @Override public int getRepeatedCount(final GeneratedMessageV3 message) { throw new UnsupportedOperationException( - "getRepeatedFieldSize() called on a singular field."); + "getRepeatedFieldSize() called on a singular field."); } @Override public int getRepeatedCount(GeneratedMessageV3.Builder builder) { throw new UnsupportedOperationException( - "getRepeatedFieldSize() called on a singular field."); + "getRepeatedFieldSize() called on a singular field."); } @Override public void clear(final Builder builder) { - invokeOrDie(clearMethod, builder); + invoker.clear(builder); } @Override public Message.Builder newBuilder() { throw new UnsupportedOperationException( - "newBuilderForField() called on a non-Message type."); + "newBuilderForField() called on a non-Message type."); } @Override public Message.Builder getBuilder(GeneratedMessageV3.Builder builder) { - throw new UnsupportedOperationException( - "getFieldBuilder() called on a non-Message type."); + throw new UnsupportedOperationException("getFieldBuilder() called on a non-Message type."); } @Override public Message.Builder getRepeatedBuilder(GeneratedMessageV3.Builder builder, int index) { throw new UnsupportedOperationException( - "getRepeatedFieldBuilder() called on a non-Message type."); + "getRepeatedFieldBuilder() called on a non-Message type."); } } private static class RepeatedFieldAccessor implements FieldAccessor { + interface MethodInvoker { + public Object get(final GeneratedMessageV3 message); + + public Object get(GeneratedMessageV3.Builder builder); + + Object getRepeated(final GeneratedMessageV3 message, final int index); + + Object getRepeated(GeneratedMessageV3.Builder builder, int index); + + void setRepeated( + final GeneratedMessageV3.Builder builder, final int index, final Object value); + + void addRepeated(final GeneratedMessageV3.Builder builder, final Object value); + + int getRepeatedCount(final GeneratedMessageV3 message); + + int getRepeatedCount(GeneratedMessageV3.Builder builder); + + void clear(final GeneratedMessageV3.Builder builder); + } + + private static final class ReflectionInvoker implements MethodInvoker { + protected final Method getMethod; + protected final Method getMethodBuilder; + protected final Method getRepeatedMethod; + protected final Method getRepeatedMethodBuilder; + protected final Method setRepeatedMethod; + protected final Method addRepeatedMethod; + protected final Method getCountMethod; + protected final Method getCountMethodBuilder; + protected final Method clearMethod; + + ReflectionInvoker( + final FieldDescriptor descriptor, + final String camelCaseName, + final Class messageClass, + final Class builderClass) { + getMethod = getMethodOrDie(messageClass, "get" + camelCaseName + "List"); + getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName + "List"); + getRepeatedMethod = getMethodOrDie(messageClass, "get" + camelCaseName, Integer.TYPE); + getRepeatedMethodBuilder = + getMethodOrDie(builderClass, "get" + camelCaseName, Integer.TYPE); + Class type = getRepeatedMethod.getReturnType(); + setRepeatedMethod = + getMethodOrDie(builderClass, "set" + camelCaseName, Integer.TYPE, type); + addRepeatedMethod = getMethodOrDie(builderClass, "add" + camelCaseName, type); + getCountMethod = getMethodOrDie(messageClass, "get" + camelCaseName + "Count"); + getCountMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName + "Count"); + clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); + } + + @Override + public Object get(final GeneratedMessageV3 message) { + return invokeOrDie(getMethod, message); + } + + @Override + public Object get(GeneratedMessageV3.Builder builder) { + return invokeOrDie(getMethodBuilder, builder); + } + + @Override + public Object getRepeated( + final GeneratedMessageV3 message, final int index) { + return invokeOrDie(getRepeatedMethod, message, index); + } + + @Override + public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { + return invokeOrDie(getRepeatedMethodBuilder, builder, index); + } + + @Override + public void setRepeated( + final GeneratedMessageV3.Builder builder, final int index, final Object value) { + invokeOrDie(setRepeatedMethod, builder, index, value); + } + + @Override + public void addRepeated( + final GeneratedMessageV3.Builder builder, final Object value) { + invokeOrDie(addRepeatedMethod, builder, value); + } + + @Override + public int getRepeatedCount(final GeneratedMessageV3 message) { + return (Integer) invokeOrDie(getCountMethod, message); + } + + @Override + public int getRepeatedCount(GeneratedMessageV3.Builder builder) { + return (Integer) invokeOrDie(getCountMethodBuilder, builder); + } + + @Override + public void clear(final GeneratedMessageV3.Builder builder) { + invokeOrDie(clearMethod, builder); + } + } + + private static final class MethodHandleInvoker implements MethodInvoker { + protected final MethodHandle getMethod; + protected final MethodHandle getMethodBuilder; + protected final MethodHandle getRepeatedMethod; + protected final MethodHandle getRepeatedMethodBuilder; + protected final MethodHandle setRepeatedMethod; + protected final MethodHandle addRepeatedMethod; + protected final MethodHandle getCountMethod; + protected final MethodHandle getCountMethodBuilder; + protected final MethodHandle clearMethod; + + MethodHandleInvoker(ReflectionInvoker accessor) throws IllegalAccessException { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + + this.getMethod = lookup.unreflect(accessor.getMethod); + this.getMethodBuilder = lookup.unreflect(accessor.getMethodBuilder); + this.getRepeatedMethod = lookup.unreflect(accessor.getRepeatedMethod); + this.getRepeatedMethodBuilder = lookup.unreflect(accessor.getRepeatedMethodBuilder); + this.setRepeatedMethod = lookup.unreflect(accessor.setRepeatedMethod); + this.addRepeatedMethod = lookup.unreflect(accessor.addRepeatedMethod); + this.getCountMethod = lookup.unreflect(accessor.getCountMethod); + this.getCountMethodBuilder = lookup.unreflect(accessor.getCountMethodBuilder); + this.clearMethod = lookup.unreflect(accessor.clearMethod); + } + + @Override + public Object get(final GeneratedMessageV3 message) { + try { + return getMethod.invoke(message); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public Object get(GeneratedMessageV3.Builder builder) { + try { + return getMethodBuilder.invoke(builder); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public Object getRepeated(final GeneratedMessageV3 message, final int index) { + try { + return getRepeatedMethod.invoke(message, index); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { + try { + return getRepeatedMethodBuilder.invoke(builder, index); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public void setRepeated( + final GeneratedMessageV3.Builder builder, final int index, final Object value) { + try { + setRepeatedMethod.invoke(builder, index, value); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public void addRepeated(final GeneratedMessageV3.Builder builder, final Object value) { + try { + addRepeatedMethod.invoke(builder, value); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public int getRepeatedCount(final GeneratedMessageV3 message) { + try { + return (Integer) getCountMethod.invoke(message); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public int getRepeatedCount(GeneratedMessageV3.Builder builder) { + try { + return (Integer) getCountMethodBuilder.invoke(builder); + } catch (Throwable e) { + throw handleException(e); + } + } + + @Override + public void clear(final GeneratedMessageV3.Builder builder) { + try { + clearMethod.invoke(builder); + } catch (Throwable e) { + throw handleException(e); + } + } + } + protected final Class type; - protected final Method getMethod; - protected final Method getMethodBuilder; - protected final Method getRepeatedMethod; - protected final Method getRepeatedMethodBuilder; - protected final Method setRepeatedMethod; - protected final Method addRepeatedMethod; - protected final Method getCountMethod; - protected final Method getCountMethodBuilder; - protected final Method clearMethod; + protected final MethodInvoker invoker; RepeatedFieldAccessor( final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, final Class builderClass) { - getMethod = getMethodOrDie(messageClass, - "get" + camelCaseName + "List"); - getMethodBuilder = getMethodOrDie(builderClass, - "get" + camelCaseName + "List"); - getRepeatedMethod = - getMethodOrDie(messageClass, "get" + camelCaseName, Integer.TYPE); - getRepeatedMethodBuilder = - getMethodOrDie(builderClass, "get" + camelCaseName, Integer.TYPE); - type = getRepeatedMethod.getReturnType(); - setRepeatedMethod = - getMethodOrDie(builderClass, "set" + camelCaseName, - Integer.TYPE, type); - addRepeatedMethod = - getMethodOrDie(builderClass, "add" + camelCaseName, type); - getCountMethod = - getMethodOrDie(messageClass, "get" + camelCaseName + "Count"); - getCountMethodBuilder = - getMethodOrDie(builderClass, "get" + camelCaseName + "Count"); + ReflectionInvoker reflectionInvoker = + new ReflectionInvoker(descriptor, camelCaseName, messageClass, builderClass); + type = reflectionInvoker.getRepeatedMethod.getReturnType(); + invoker = tryGetMethodHandleInvoke(reflectionInvoker); + } - clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName); + static MethodInvoker tryGetMethodHandleInvoke(ReflectionInvoker accessor) { + if (forTestUseReflection) { + return accessor; + } + try { + return new MethodHandleInvoker(accessor); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } } @Override public Object get(final GeneratedMessageV3 message) { - return invokeOrDie(getMethod, message); + return invoker.get(message); } @Override public Object get(GeneratedMessageV3.Builder builder) { - return invokeOrDie(getMethodBuilder, builder); + return invoker.get(builder); } @Override public Object getRaw(final GeneratedMessageV3 message) { @@ -2205,11 +2667,11 @@ public abstract class GeneratedMessageV3 extends AbstractMessage } @Override public Object getRepeated(final GeneratedMessageV3 message, final int index) { - return invokeOrDie(getRepeatedMethod, message, index); + return invoker.getRepeated(message, index); } @Override public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { - return invokeOrDie(getRepeatedMethodBuilder, builder, index); + return invoker.getRepeated(builder, index); } @Override public Object getRepeatedRaw(GeneratedMessageV3 message, int index) { @@ -2221,48 +2683,45 @@ public abstract class GeneratedMessageV3 extends AbstractMessage } @Override public void setRepeated(final Builder builder, final int index, final Object value) { - invokeOrDie(setRepeatedMethod, builder, index, value); + invoker.setRepeated(builder, index, value); } @Override public void addRepeated(final Builder builder, final Object value) { - invokeOrDie(addRepeatedMethod, builder, value); + invoker.addRepeated(builder, value); } @Override public boolean has(final GeneratedMessageV3 message) { - throw new UnsupportedOperationException( - "hasField() called on a repeated field."); + throw new UnsupportedOperationException("hasField() called on a repeated field."); } @Override public boolean has(GeneratedMessageV3.Builder builder) { - throw new UnsupportedOperationException( - "hasField() called on a repeated field."); + throw new UnsupportedOperationException("hasField() called on a repeated field."); } @Override public int getRepeatedCount(final GeneratedMessageV3 message) { - return (Integer) invokeOrDie(getCountMethod, message); + return invoker.getRepeatedCount(message); } @Override public int getRepeatedCount(GeneratedMessageV3.Builder builder) { - return (Integer) invokeOrDie(getCountMethodBuilder, builder); + return invoker.getRepeatedCount(builder); } @Override public void clear(final Builder builder) { - invokeOrDie(clearMethod, builder); + invoker.clear(builder); } @Override public Message.Builder newBuilder() { throw new UnsupportedOperationException( - "newBuilderForField() called on a non-Message type."); + "newBuilderForField() called on a non-Message type."); } @Override public Message.Builder getBuilder(GeneratedMessageV3.Builder builder) { - throw new UnsupportedOperationException( - "getFieldBuilder() called on a non-Message type."); + throw new UnsupportedOperationException("getFieldBuilder() called on a non-Message type."); } @Override public Message.Builder getRepeatedBuilder(GeneratedMessageV3.Builder builder, int index) { throw new UnsupportedOperationException( - "getRepeatedFieldBuilder() called on a non-Message type."); + "getRepeatedFieldBuilder() called on a non-Message type."); } } @@ -2437,10 +2896,8 @@ public abstract class GeneratedMessageV3 extends AbstractMessage enumDescriptor = descriptor.getEnumType(); - valueOfMethod = getMethodOrDie(type, "valueOf", - EnumValueDescriptor.class); - getValueDescriptorMethod = - getMethodOrDie(type, "getValueDescriptor"); + valueOfMethod = getMethodOrDie(type, "valueOf", EnumValueDescriptor.class); + getValueDescriptorMethod = getMethodOrDie(type, "getValueDescriptor"); supportUnknownEnumValue = descriptor.getFile().supportsUnknownEnumValue(); if (supportUnknownEnumValue) { @@ -2502,10 +2959,8 @@ public abstract class GeneratedMessageV3 extends AbstractMessage enumDescriptor = descriptor.getEnumType(); - valueOfMethod = getMethodOrDie(type, "valueOf", - EnumValueDescriptor.class); - getValueDescriptorMethod = - getMethodOrDie(type, "getValueDescriptor"); + valueOfMethod = getMethodOrDie(type, "valueOf", EnumValueDescriptor.class); + getValueDescriptorMethod = getMethodOrDie(type, "getValueDescriptor"); supportUnknownEnumValue = descriptor.getFile().supportsUnknownEnumValue(); if (supportUnknownEnumValue) { @@ -2553,35 +3008,31 @@ public abstract class GeneratedMessageV3 extends AbstractMessage } @Override - public Object getRepeated(final GeneratedMessageV3 message, - final int index) { + public Object getRepeated(final GeneratedMessageV3 message, final int index) { if (supportUnknownEnumValue) { int value = (Integer) invokeOrDie(getRepeatedValueMethod, message, index); return enumDescriptor.findValueByNumberCreatingIfUnknown(value); } - return invokeOrDie(getValueDescriptorMethod, - super.getRepeated(message, index)); + return invokeOrDie(getValueDescriptorMethod, super.getRepeated(message, index)); } + @Override - public Object getRepeated(final GeneratedMessageV3.Builder builder, - final int index) { + public Object getRepeated(final GeneratedMessageV3.Builder builder, final int index) { if (supportUnknownEnumValue) { int value = (Integer) invokeOrDie(getRepeatedValueMethodBuilder, builder, index); return enumDescriptor.findValueByNumberCreatingIfUnknown(value); } - return invokeOrDie(getValueDescriptorMethod, - super.getRepeated(builder, index)); + return invokeOrDie(getValueDescriptorMethod, super.getRepeated(builder, index)); } + @Override - public void setRepeated(final Builder builder, - final int index, final Object value) { + public void setRepeated(final Builder builder, final int index, final Object value) { if (supportUnknownEnumValue) { invokeOrDie(setRepeatedValueMethod, builder, index, ((EnumValueDescriptor) value).getNumber()); return; } - super.setRepeated(builder, index, invokeOrDie(valueOfMethod, null, - value)); + super.setRepeated(builder, index, invokeOrDie(valueOfMethod, null, value)); } @Override public void addRepeated(final Builder builder, final Object value) { @@ -2677,7 +3128,8 @@ public abstract class GeneratedMessageV3 extends AbstractMessage // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) - .mergeFrom((Message) value).buildPartial(); + .mergeFrom((Message) value) + .buildPartial(); } } @@ -2720,13 +3172,13 @@ public abstract class GeneratedMessageV3 extends AbstractMessage // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. return ((Message.Builder) invokeOrDie(newBuilderMethod, null)) - .mergeFrom((Message) value).build(); + .mergeFrom((Message) value) + .build(); } } @Override - public void setRepeated(final Builder builder, - final int index, final Object value) { + public void setRepeated(final Builder builder, final int index, final Object value) { super.setRepeated(builder, index, coerceType(value)); } @Override @@ -2757,12 +3209,10 @@ public abstract class GeneratedMessageV3 extends AbstractMessage } /** - * Checks that the {@link Extension} is non-Lite and returns it as a - * {@link GeneratedExtension}. + * Checks that the {@link Extension} is non-Lite and returns it as a {@link GeneratedExtension}. */ private static , T> - Extension checkNotLite( - ExtensionLite extension) { + Extension checkNotLite(ExtensionLite extension) { if (extension.isLite()) { throw new IllegalArgumentException("Expected non-lite extension."); } diff --git a/java/core/src/main/java/com/google/protobuf/IntArrayList.java b/java/core/src/main/java/com/google/protobuf/IntArrayList.java index 4993eea746..98f1f81331 100644 --- a/java/core/src/main/java/com/google/protobuf/IntArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/IntArrayList.java @@ -42,11 +42,10 @@ import java.util.RandomAccess; * * @author dweis@google.com (Daniel Weis) */ -final class IntArrayList - extends AbstractProtobufList +final class IntArrayList extends AbstractProtobufList implements IntList, RandomAccess, PrimitiveNonBoxingCollection { - private static final IntArrayList EMPTY_LIST = new IntArrayList(); + private static final IntArrayList EMPTY_LIST = new IntArrayList(new int[0], 0); static { EMPTY_LIST.makeImmutable(); } @@ -55,9 +54,7 @@ final class IntArrayList return EMPTY_LIST; } - /** - * The backing store for the list. - */ + /** The backing store for the list. */ private int[] array; /** @@ -66,16 +63,13 @@ final class IntArrayList */ private int size; - /** - * Constructs a new mutable {@code IntArrayList} with default capacity. - */ + /** Constructs a new mutable {@code IntArrayList} with default capacity. */ IntArrayList() { this(new int[DEFAULT_CAPACITY], 0); } /** - * Constructs a new mutable {@code IntArrayList} - * containing the same elements as {@code other}. + * Constructs a new mutable {@code IntArrayList} containing the same elements as {@code other}. */ private IntArrayList(int[] other, int size) { array = other; @@ -164,22 +158,34 @@ final class IntArrayList return previousValue; } + @Override + public boolean add(Integer element) { + addInt(element); + return true; + } + @Override public void add(int index, Integer element) { addInt(index, element); } - /** - * Like {@link #add(Integer)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Integer)} but more efficient in that it doesn't box the element. */ @Override public void addInt(int element) { - addInt(size, element); + ensureIsMutable(); + if (size == array.length) { + // Resize to 1.5x the size + int length = ((size * 3) / 2) + 1; + int[] newArray = new int[length]; + + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + + array[size++] = element; } - /** - * Like {@link #add(int, Integer)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(int, Integer)} but more efficient in that it doesn't box the element. */ private void addInt(int index, int element) { ensureIsMutable(); if (index < 0 || index > size) { @@ -245,7 +251,7 @@ final class IntArrayList ensureIsMutable(); for (int i = 0; i < size; i++) { if (o.equals(array[i])) { - System.arraycopy(array, i + 1, array, i, size - i); + System.arraycopy(array, i + 1, array, i, size - i - 1); size--; modCount++; return true; @@ -260,7 +266,7 @@ final class IntArrayList ensureIndexInRange(index); int value = array[index]; if (index < size - 1) { - System.arraycopy(array, index + 1, array, index, size - index); + System.arraycopy(array, index + 1, array, index, size - index - 1); } size--; modCount++; diff --git a/java/core/src/main/java/com/google/protobuf/Internal.java b/java/core/src/main/java/com/google/protobuf/Internal.java index 878573d565..0826351bcb 100644 --- a/java/core/src/main/java/com/google/protobuf/Internal.java +++ b/java/core/src/main/java/com/google/protobuf/Internal.java @@ -45,10 +45,9 @@ import java.util.RandomAccess; import java.util.Set; /** - * The classes contained within are used internally by the Protocol Buffer - * library and generated message implementations. They are public only because - * those generated messages do not reside in the {@code protobuf} package. - * Others should not use this class directly. + * The classes contained within are used internally by the Protocol Buffer library and generated + * message implementations. They are public only because those generated messages do not reside in + * the {@code protobuf} package. Others should not use this class directly. * * @author kenton@google.com (Kenton Varda) */ @@ -59,9 +58,7 @@ public final class Internal { static final Charset UTF_8 = Charset.forName("UTF-8"); static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); - /** - * Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. - */ + /** Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. */ static T checkNotNull(T obj) { if (obj == null) { throw new NullPointerException(); @@ -69,9 +66,7 @@ public final class Internal { return obj; } - /** - * Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. - */ + /** Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. */ static T checkNotNull(T obj, String message) { if (obj == null) { throw new NullPointerException(message); @@ -80,73 +75,63 @@ public final class Internal { } /** - * Helper called by generated code to construct default values for string - * fields. - *

- * The protocol compiler does not actually contain a UTF-8 decoder -- it - * just pushes UTF-8-encoded text around without touching it. The one place - * where this presents a problem is when generating Java string literals. - * Unicode characters in the string literal would normally need to be encoded - * using a Unicode escape sequence, which would require decoding them. - * To get around this, protoc instead embeds the UTF-8 bytes into the - * generated code and leaves it to the runtime library to decode them. - *

- * It gets worse, though. If protoc just generated a byte array, like: - * new byte[] {0x12, 0x34, 0x56, 0x78} - * Java actually generates *code* which allocates an array and then fills - * in each value. This is much less efficient than just embedding the bytes - * directly into the bytecode. To get around this, we need another - * work-around. String literals are embedded directly, so protoc actually - * generates a string literal corresponding to the bytes. The easiest way - * to do this is to use the ISO-8859-1 character set, which corresponds to - * the first 256 characters of the Unicode range. Protoc can then use - * good old CEscape to generate the string. - *

- * So we have a string literal which represents a set of bytes which - * represents another string. This function -- stringDefaultValue -- - * converts from the generated string to the string we actually want. The - * generated code calls this automatically. + * Helper called by generated code to construct default values for string fields. + * + *

The protocol compiler does not actually contain a UTF-8 decoder -- it just pushes + * UTF-8-encoded text around without touching it. The one place where this presents a problem is + * when generating Java string literals. Unicode characters in the string literal would normally + * need to be encoded using a Unicode escape sequence, which would require decoding them. To get + * around this, protoc instead embeds the UTF-8 bytes into the generated code and leaves it to the + * runtime library to decode them. + * + *

It gets worse, though. If protoc just generated a byte array, like: new byte[] {0x12, 0x34, + * 0x56, 0x78} Java actually generates *code* which allocates an array and then fills in each + * value. This is much less efficient than just embedding the bytes directly into the bytecode. To + * get around this, we need another work-around. String literals are embedded directly, so protoc + * actually generates a string literal corresponding to the bytes. The easiest way to do this is + * to use the ISO-8859-1 character set, which corresponds to the first 256 characters of the + * Unicode range. Protoc can then use good old CEscape to generate the string. + * + *

So we have a string literal which represents a set of bytes which represents another string. + * This function -- stringDefaultValue -- converts from the generated string to the string we + * actually want. The generated code calls this automatically. */ public static String stringDefaultValue(String bytes) { return new String(bytes.getBytes(ISO_8859_1), UTF_8); } /** - * Helper called by generated code to construct default values for bytes - * fields. - *

- * This is a lot like {@link #stringDefaultValue}, but for bytes fields. - * In this case we only need the second of the two hacks -- allowing us to - * embed raw bytes as a string literal with ISO-8859-1 encoding. + * Helper called by generated code to construct default values for bytes fields. + * + *

This is a lot like {@link #stringDefaultValue}, but for bytes fields. In this case we only + * need the second of the two hacks -- allowing us to embed raw bytes as a string literal with + * ISO-8859-1 encoding. */ public static ByteString bytesDefaultValue(String bytes) { return ByteString.copyFrom(bytes.getBytes(ISO_8859_1)); } /** - * Helper called by generated code to construct default values for bytes - * fields. - *

- * This is like {@link #bytesDefaultValue}, but returns a byte array. + * Helper called by generated code to construct default values for bytes fields. + * + *

This is like {@link #bytesDefaultValue}, but returns a byte array. */ public static byte[] byteArrayDefaultValue(String bytes) { return bytes.getBytes(ISO_8859_1); } /** - * Helper called by generated code to construct default values for bytes - * fields. - *

- * This is like {@link #bytesDefaultValue}, but returns a ByteBuffer. + * Helper called by generated code to construct default values for bytes fields. + * + *

This is like {@link #bytesDefaultValue}, but returns a ByteBuffer. */ public static ByteBuffer byteBufferDefaultValue(String bytes) { return ByteBuffer.wrap(byteArrayDefaultValue(bytes)); } /** - * Create a new ByteBuffer and copy all the content of {@code source} - * ByteBuffer to the new ByteBuffer. The new ByteBuffer's limit and - * capacity will be source.capacity(), and its position will be 0. - * Note that the state of {@code source} ByteBuffer won't be changed. + * Create a new ByteBuffer and copy all the content of {@code source} ByteBuffer to the new + * ByteBuffer. The new ByteBuffer's limit and capacity will be source.capacity(), and its position + * will be 0. Note that the state of {@code source} ByteBuffer won't be changed. */ public static ByteBuffer copyByteBuffer(ByteBuffer source) { // Make a duplicate of the source ByteBuffer and read data from the @@ -162,29 +147,27 @@ public final class Internal { } /** - * Helper called by generated code to determine if a byte array is a valid - * UTF-8 encoded string such that the original bytes can be converted to - * a String object and then back to a byte array round tripping the bytes - * without loss. More precisely, returns {@code true} whenever: - *

   {@code
+   * Helper called by generated code to determine if a byte array is a valid UTF-8 encoded string
+   * such that the original bytes can be converted to a String object and then back to a byte array
+   * round tripping the bytes without loss. More precisely, returns {@code true} whenever:
+   *
+   * 
{@code
    * Arrays.equals(byteString.toByteArray(),
    *     new String(byteString.toByteArray(), "UTF-8").getBytes("UTF-8"))
    * }
* - *

This method rejects "overlong" byte sequences, as well as - * 3-byte sequences that would map to a surrogate character, in - * accordance with the restricted definition of UTF-8 introduced in - * Unicode 3.1. Note that the UTF-8 decoder included in Oracle's - * JDK has been modified to also reject "overlong" byte sequences, - * but currently (2011) still accepts 3-byte surrogate character + *

This method rejects "overlong" byte sequences, as well as 3-byte sequences that would map to + * a surrogate character, in accordance with the restricted definition of UTF-8 introduced in + * Unicode 3.1. Note that the UTF-8 decoder included in Oracle's JDK has been modified to also + * reject "overlong" byte sequences, but currently (2011) still accepts 3-byte surrogate character * byte sequences. * *

See the Unicode Standard,
* Table 3-6. UTF-8 Bit Distribution,
* Table 3-7. Well Formed UTF-8 Byte Sequences. * - *

As of 2011-02, this method simply returns the result of {@link - * ByteString#isValidUtf8()}. Calling that method directly is preferred. + *

As of 2011-02, this method simply returns the result of {@link ByteString#isValidUtf8()}. + * Calling that method directly is preferred. * * @param byteString the string to check * @return whether the byte array is round trippable @@ -193,42 +176,36 @@ public final class Internal { return byteString.isValidUtf8(); } - /** - * Like {@link #isValidUtf8(ByteString)} but for byte arrays. - */ + /** Like {@link #isValidUtf8(ByteString)} but for byte arrays. */ public static boolean isValidUtf8(byte[] byteArray) { return Utf8.isValidUtf8(byteArray); } - /** - * Helper method to get the UTF-8 bytes of a string. - */ + /** Helper method to get the UTF-8 bytes of a string. */ public static byte[] toByteArray(String value) { return value.getBytes(UTF_8); } - /** - * Helper method to convert a byte array to a string using UTF-8 encoding. - */ + /** Helper method to convert a byte array to a string using UTF-8 encoding. */ public static String toStringUtf8(byte[] bytes) { return new String(bytes, UTF_8); } /** - * Interface for an enum value or value descriptor, to be used in FieldSet. - * The lite library stores enum values directly in FieldSets but the full - * library stores EnumValueDescriptors in order to better support reflection. + * Interface for an enum value or value descriptor, to be used in FieldSet. The lite library + * stores enum values directly in FieldSets but the full library stores EnumValueDescriptors in + * order to better support reflection. */ public interface EnumLite { int getNumber(); } /** - * Interface for an object which maps integers to {@link EnumLite}s. - * {@link Descriptors.EnumDescriptor} implements this interface by mapping - * numbers to {@link Descriptors.EnumValueDescriptor}s. Additionally, - * every generated enum type has a static method internalGetValueMap() which - * returns an implementation of this type that maps numbers to enum values. + * Interface for an object which maps integers to {@link EnumLite}s. {@link + * Descriptors.EnumDescriptor} implements this interface by mapping numbers to {@link + * Descriptors.EnumValueDescriptor}s. Additionally, every generated enum type has a static method + * internalGetValueMap() which returns an implementation of this type that maps numbers to enum + * values. */ public interface EnumLiteMap { T findValueByNumber(int number); @@ -241,6 +218,7 @@ public final class Internal { /** * Helper method for implementing {@link Message#hashCode()} for longs. + * * @see Long#hashCode() */ public static int hashLong(long n) { @@ -248,8 +226,8 @@ public final class Internal { } /** - * Helper method for implementing {@link Message#hashCode()} for - * booleans. + * Helper method for implementing {@link Message#hashCode()} for booleans. + * * @see Boolean#hashCode() */ public static int hashBoolean(boolean b) { @@ -258,19 +236,16 @@ public final class Internal { /** * Helper method for implementing {@link Message#hashCode()} for enums. - *

- * This is needed because {@link java.lang.Enum#hashCode()} is final, but we - * need to use the field number as the hash code to ensure compatibility - * between statically and dynamically generated enum objects. + * + *

This is needed because {@link java.lang.Enum#hashCode()} is final, but we need to use the + * field number as the hash code to ensure compatibility between statically and dynamically + * generated enum objects. */ public static int hashEnum(EnumLite e) { return e.getNumber(); } - /** - * Helper method for implementing {@link Message#hashCode()} for - * enum lists. - */ + /** Helper method for implementing {@link Message#hashCode()} for enum lists. */ public static int hashEnumList(List list) { int hash = 1; for (EnumLite e : list) { @@ -279,9 +254,7 @@ public final class Internal { return hash; } - /** - * Helper method for implementing {@link Message#equals(Object)} for bytes field. - */ + /** Helper method for implementing {@link Message#equals(Object)} for bytes field. */ public static boolean equals(List a, List b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); ++i) { @@ -292,9 +265,7 @@ public final class Internal { return true; } - /** - * Helper method for implementing {@link Message#hashCode()} for bytes field. - */ + /** Helper method for implementing {@link Message#hashCode()} for bytes field. */ public static int hashCode(List list) { int hash = 1; for (byte[] bytes : list) { @@ -303,9 +274,7 @@ public final class Internal { return hash; } - /** - * Helper method for implementing {@link Message#hashCode()} for bytes field. - */ + /** Helper method for implementing {@link Message#hashCode()} for bytes field. */ public static int hashCode(byte[] bytes) { // The hash code for a byte array should be the same as the hash code for a // ByteString with the same content. This is to ensure that the generated @@ -313,10 +282,8 @@ public final class Internal { // based hashCode() method. return Internal.hashCode(bytes, 0, bytes.length); } - - /** - * Helper method for implementing {@link LiteralByteString#hashCode()}. - */ + + /** Helper method for implementing {@link LiteralByteString#hashCode()}. */ static int hashCode(byte[] bytes, int offset, int length) { // The hash code for a byte array should be the same as the hash code for a // ByteString with the same content. This is to ensure that the generated @@ -326,20 +293,15 @@ public final class Internal { return h == 0 ? 1 : h; } - /** - * Helper method for continuously hashing bytes. - */ + /** Helper method for continuously hashing bytes. */ static int partialHash(int h, byte[] bytes, int offset, int length) { for (int i = offset; i < offset + length; i++) { h = h * 31 + bytes[i]; } return h; } - - /** - * Helper method for implementing {@link Message#equals(Object)} for bytes - * field. - */ + + /** Helper method for implementing {@link Message#equals(Object)} for bytes field. */ public static boolean equalsByteBuffer(ByteBuffer a, ByteBuffer b) { if (a.capacity() != b.capacity()) { return false; @@ -349,12 +311,8 @@ public final class Internal { return a.duplicate().clear().equals(b.duplicate().clear()); } - /** - * Helper method for implementing {@link Message#equals(Object)} for bytes - * field. - */ - public static boolean equalsByteBuffer( - List a, List b) { + /** Helper method for implementing {@link Message#equals(Object)} for bytes field. */ + public static boolean equalsByteBuffer(List a, List b) { if (a.size() != b.size()) { return false; } @@ -366,10 +324,7 @@ public final class Internal { return true; } - /** - * Helper method for implementing {@link Message#hashCode()} for bytes - * field. - */ + /** Helper method for implementing {@link Message#hashCode()} for bytes field. */ public static int hashCodeByteBuffer(List list) { int hash = 1; for (ByteBuffer bytes : list) { @@ -380,10 +335,7 @@ public final class Internal { private static final int DEFAULT_BUFFER_SIZE = 4096; - /** - * Helper method for implementing {@link Message#hashCode()} for bytes - * field. - */ + /** Helper method for implementing {@link Message#hashCode()} for bytes field. */ public static int hashCodeByteBuffer(ByteBuffer bytes) { if (bytes.hasArray()) { // Fast path. @@ -392,15 +344,15 @@ public final class Internal { } else { // Read the data into a temporary byte array before calculating the // hash value. - final int bufferSize = bytes.capacity() > DEFAULT_BUFFER_SIZE - ? DEFAULT_BUFFER_SIZE : bytes.capacity(); + final int bufferSize = + bytes.capacity() > DEFAULT_BUFFER_SIZE ? DEFAULT_BUFFER_SIZE : bytes.capacity(); final byte[] buffer = new byte[bufferSize]; final ByteBuffer duplicated = bytes.duplicate(); duplicated.clear(); int h = bytes.capacity(); while (duplicated.remaining() > 0) { - final int length = duplicated.remaining() <= bufferSize ? - duplicated.remaining() : bufferSize; + final int length = + duplicated.remaining() <= bufferSize ? duplicated.remaining() : bufferSize; duplicated.get(buffer, 0, length); h = partialHash(h, buffer, 0, length); } @@ -414,8 +366,7 @@ public final class Internal { Method method = clazz.getMethod("getDefaultInstance"); return (T) method.invoke(method); } catch (Exception e) { - throw new RuntimeException( - "Failed to get default instance for " + clazz, e); + throw new RuntimeException("Failed to get default instance for " + clazz, e); } } @@ -423,11 +374,8 @@ public final class Internal { /** An empty byte array constant used in generated code. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; - /** - * An empty byte array constant used in generated code. - */ - public static final ByteBuffer EMPTY_BYTE_BUFFER = - ByteBuffer.wrap(EMPTY_BYTE_ARRAY); + /** An empty byte array constant used in generated code. */ + public static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(EMPTY_BYTE_ARRAY); /** An empty coded input stream constant used in generated code. */ public static final CodedInputStream EMPTY_CODED_INPUT_STREAM = @@ -442,12 +390,10 @@ public final class Internal { /** * Provides an immutable view of {@code List} around a {@code List}. * - * Protobuf internal. Used in protobuf generated code only. + *

Protobuf internal. Used in protobuf generated code only. */ public static class ListAdapter extends AbstractList { - /** - * Convert individual elements of the List from F to T. - */ + /** Convert individual elements of the List from F to T. */ public interface Converter { T convert(F from); } @@ -471,16 +417,12 @@ public final class Internal { } } - /** - * Wrap around a {@code Map} and provide a {@code Map} - * interface. - */ + /** Wrap around a {@code Map} and provide a {@code Map} interface. */ public static class MapAdapter extends AbstractMap { - /** - * An interface used to convert between two types. - */ + /** An interface used to convert between two types. */ public interface Converter { B doForward(A object); + A doBackward(B object); } @@ -503,8 +445,7 @@ public final class Internal { private final Map realMap; private final Converter valueConverter; - public MapAdapter(Map realMap, - Converter valueConverter) { + public MapAdapter(Map realMap, Converter valueConverter) { this.realMap = realMap; this.valueConverter = valueConverter; } @@ -535,6 +476,7 @@ public final class Internal { private class SetAdapter extends AbstractSet> { private final Set> realSet; + public SetAdapter(Set> realSet) { this.realSet = realSet; } @@ -553,8 +495,7 @@ public final class Internal { private class IteratorAdapter implements Iterator> { private final Iterator> realIterator; - public IteratorAdapter( - Iterator> realIterator) { + public IteratorAdapter(Iterator> realIterator) { this.realIterator = realIterator; } @@ -593,27 +534,44 @@ public final class Internal { @Override public V setValue(V value) { - RealValue oldValue = realEntry.setValue( - valueConverter.doBackward(value)); + RealValue oldValue = realEntry.setValue(valueConverter.doBackward(value)); if (oldValue == null) { return null; } return valueConverter.doForward(oldValue); } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (!(o instanceof Map.Entry)) { + return false; + } + @SuppressWarnings("unchecked") + Map.Entry other = (Map.Entry) o; + return getKey().equals(other.getKey()) && getValue().equals(getValue()); + } + + @Override + public int hashCode() { + return realEntry.hashCode(); + } } } /** * Extends {@link List} to add the capability to make the list immutable and inspect if it is * modifiable. - *

- * All implementations must support efficient random access. + * + *

All implementations must support efficient random access. */ public static interface ProtobufList extends List, RandomAccess { /** - * Makes this list immutable. All subsequent modifications will throw an - * {@link UnsupportedOperationException}. + * Makes this list immutable. All subsequent modifications will throw an {@link + * UnsupportedOperationException}. */ void makeImmutable(); @@ -622,9 +580,7 @@ public final class Internal { */ boolean isModifiable(); - /** - * Returns a mutable clone of this list with the specified capacity. - */ + /** Returns a mutable clone of this list with the specified capacity. */ ProtobufList mutableCopyWithCapacity(int capacity); } @@ -634,24 +590,16 @@ public final class Internal { */ public static interface IntList extends ProtobufList { - /** - * Like {@link #get(int)} but more efficient in that it doesn't box the returned value. - */ + /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */ int getInt(int index); - /** - * Like {@link #add(Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */ void addInt(int element); - /** - * Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */ int setInt(int index, int element); - /** - * Returns a mutable clone of this list with the specified capacity. - */ + /** Returns a mutable clone of this list with the specified capacity. */ @Override IntList mutableCopyWithCapacity(int capacity); } @@ -662,52 +610,36 @@ public final class Internal { */ public static interface BooleanList extends ProtobufList { - /** - * Like {@link #get(int)} but more efficient in that it doesn't box the returned value. - */ + /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */ boolean getBoolean(int index); - /** - * Like {@link #add(Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */ void addBoolean(boolean element); - /** - * Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */ boolean setBoolean(int index, boolean element); - /** - * Returns a mutable clone of this list with the specified capacity. - */ + /** Returns a mutable clone of this list with the specified capacity. */ @Override BooleanList mutableCopyWithCapacity(int capacity); } /** - * A {@link java.util.List} implementation that avoids boxing the elements into Longs if - * possible. Does not support null elements. + * A {@link java.util.List} implementation that avoids boxing the elements into Longs if possible. + * Does not support null elements. */ public static interface LongList extends ProtobufList { - /** - * Like {@link #get(int)} but more efficient in that it doesn't box the returned value. - */ + /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */ long getLong(int index); - /** - * Like {@link #add(Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */ void addLong(long element); - /** - * Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */ long setLong(int index, long element); - /** - * Returns a mutable clone of this list with the specified capacity. - */ + /** Returns a mutable clone of this list with the specified capacity. */ @Override LongList mutableCopyWithCapacity(int capacity); } @@ -718,24 +650,16 @@ public final class Internal { */ public static interface DoubleList extends ProtobufList { - /** - * Like {@link #get(int)} but more efficient in that it doesn't box the returned value. - */ + /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */ double getDouble(int index); - /** - * Like {@link #add(Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */ void addDouble(double element); - /** - * Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */ double setDouble(int index, double element); - /** - * Returns a mutable clone of this list with the specified capacity. - */ + /** Returns a mutable clone of this list with the specified capacity. */ @Override DoubleList mutableCopyWithCapacity(int capacity); } @@ -746,25 +670,18 @@ public final class Internal { */ public static interface FloatList extends ProtobufList { - /** - * Like {@link #get(int)} but more efficient in that it doesn't box the returned value. - */ + /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */ float getFloat(int index); - /** - * Like {@link #add(Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */ void addFloat(float element); - /** - * Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */ float setFloat(int index, float element); - /** - * Returns a mutable clone of this list with the specified capacity. - */ + /** Returns a mutable clone of this list with the specified capacity. */ @Override FloatList mutableCopyWithCapacity(int capacity); } + } diff --git a/java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java b/java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java index 510c6aac74..22f31bbc0b 100644 --- a/java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java +++ b/java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java @@ -33,8 +33,8 @@ package com.google.protobuf; import java.io.IOException; /** - * Thrown when a protocol message being parsed is invalid in some way, - * e.g. it contains a malformed varint or a negative byte length. + * Thrown when a protocol message being parsed is invalid in some way, e.g. it contains a malformed + * varint or a negative byte length. * * @author kenton@google.com Kenton Varda */ @@ -55,28 +55,26 @@ public class InvalidProtocolBufferException extends IOException { } /** - * Attaches an unfinished message to the exception to support best-effort - * parsing in {@code Parser} interface. + * Attaches an unfinished message to the exception to support best-effort parsing in {@code + * Parser} interface. * * @return this */ - public InvalidProtocolBufferException setUnfinishedMessage( - MessageLite unfinishedMessage) { + public InvalidProtocolBufferException setUnfinishedMessage(MessageLite unfinishedMessage) { this.unfinishedMessage = unfinishedMessage; return this; } /** - * Returns the unfinished message attached to the exception, or null if - * no message is attached. + * Returns the unfinished message attached to the exception, or null if no message is attached. */ public MessageLite getUnfinishedMessage() { return unfinishedMessage; } /** - * Unwraps the underlying {@link IOException} if this exception was caused by an I/O - * problem. Otherwise, returns {@code this}. + * Unwraps the underlying {@link IOException} if this exception was caused by an I/O problem. + * Otherwise, returns {@code this}. */ public IOException unwrapIOException() { return getCause() instanceof IOException ? (IOException) getCause() : this; @@ -84,41 +82,36 @@ public class InvalidProtocolBufferException extends IOException { static InvalidProtocolBufferException truncatedMessage() { return new InvalidProtocolBufferException( - "While parsing a protocol message, the input ended unexpectedly " + - "in the middle of a field. This could mean either that the " + - "input has been truncated or that an embedded message " + - "misreported its own length."); + "While parsing a protocol message, the input ended unexpectedly " + + "in the middle of a field. This could mean either that the " + + "input has been truncated or that an embedded message " + + "misreported its own length."); } static InvalidProtocolBufferException negativeSize() { return new InvalidProtocolBufferException( - "CodedInputStream encountered an embedded string or message " + - "which claimed to have negative size."); + "CodedInputStream encountered an embedded string or message " + + "which claimed to have negative size."); } static InvalidProtocolBufferException malformedVarint() { - return new InvalidProtocolBufferException( - "CodedInputStream encountered a malformed varint."); + return new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint."); } static InvalidProtocolBufferException invalidTag() { - return new InvalidProtocolBufferException( - "Protocol message contained an invalid tag (zero)."); + return new InvalidProtocolBufferException("Protocol message contained an invalid tag (zero)."); } static InvalidProtocolBufferException invalidEndTag() { return new InvalidProtocolBufferException( - "Protocol message end-group tag did not match expected tag."); + "Protocol message end-group tag did not match expected tag."); } static InvalidWireTypeException invalidWireType() { - return new InvalidWireTypeException( - "Protocol message tag had invalid wire type."); + return new InvalidWireTypeException("Protocol message tag had invalid wire type."); } - /** - * Exception indicating that and unexpected wire type was encountered for a field. - */ + /** Exception indicating that and unexpected wire type was encountered for a field. */ @ExperimentalApi public static class InvalidWireTypeException extends InvalidProtocolBufferException { private static final long serialVersionUID = 3283890091615336259L; @@ -130,14 +123,14 @@ public class InvalidProtocolBufferException extends IOException { static InvalidProtocolBufferException recursionLimitExceeded() { return new InvalidProtocolBufferException( - "Protocol message had too many levels of nesting. May be malicious. " + - "Use CodedInputStream.setRecursionLimit() to increase the depth limit."); + "Protocol message had too many levels of nesting. May be malicious. " + + "Use CodedInputStream.setRecursionLimit() to increase the depth limit."); } static InvalidProtocolBufferException sizeLimitExceeded() { return new InvalidProtocolBufferException( - "Protocol message was too large. May be malicious. " + - "Use CodedInputStream.setSizeLimit() to increase the size limit."); + "Protocol message was too large. May be malicious. " + + "Use CodedInputStream.setSizeLimit() to increase the size limit."); } static InvalidProtocolBufferException parseFailure() { diff --git a/java/core/src/main/java/com/google/protobuf/JavaType.java b/java/core/src/main/java/com/google/protobuf/JavaType.java new file mode 100755 index 0000000000..770aa9ef1f --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/JavaType.java @@ -0,0 +1,76 @@ +// 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. + +package com.google.protobuf; + +/** Enum that identifies the Java types required to store protobuf fields. */ +@ExperimentalApi +public enum JavaType { + VOID(Void.class, Void.class, null), + INT(int.class, Integer.class, 0), + LONG(long.class, Long.class, 0L), + FLOAT(float.class, Float.class, 0F), + DOUBLE(double.class, Double.class, 0D), + BOOLEAN(boolean.class, Boolean.class, false), + STRING(String.class, String.class, ""), + BYTE_STRING(ByteString.class, ByteString.class, ByteString.EMPTY), + ENUM(int.class, Integer.class, null), + MESSAGE(Object.class, Object.class, null); + + private final Class type; + private final Class boxedType; + private final Object defaultDefault; + + JavaType(Class type, Class boxedType, Object defaultDefault) { + this.type = type; + this.boxedType = boxedType; + this.defaultDefault = defaultDefault; + } + + /** The default default value for fields of this type, if it's a primitive type. */ + public Object getDefaultDefault() { + return defaultDefault; + } + + /** Gets the required type for a field that would hold a value of this type. */ + public Class getType() { + return type; + } + + /** @return the boxedType */ + public Class getBoxedType() { + return boxedType; + } + + /** Indicates whether or not this {@link JavaType} can be applied to a field of the given type. */ + public boolean isValidType(Class t) { + return type.isAssignableFrom(t); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/LazyField.java b/java/core/src/main/java/com/google/protobuf/LazyField.java index 98e13ca199..891171d7c8 100644 --- a/java/core/src/main/java/com/google/protobuf/LazyField.java +++ b/java/core/src/main/java/com/google/protobuf/LazyField.java @@ -34,12 +34,12 @@ import java.util.Iterator; import java.util.Map.Entry; /** - * LazyField encapsulates the logic of lazily parsing message fields. It stores - * the message in a ByteString initially and then parse it on-demand. + * LazyField encapsulates the logic of lazily parsing message fields. It stores the message in a + * ByteString initially and then parse it on-demand. * - * Most of key methods are implemented in {@link LazyFieldLite} but this class - * can contain default instance of the message to provide {@code hashCode()}, - * {@code euqals()} and {@code toString()}. + *

Most of key methods are implemented in {@link LazyFieldLite} but this class can contain + * default instance of the message to provide {@code hashCode()}, {@code euqals()} and {@code + * toString()}. * * @author xiangl@google.com (Xiang Li) */ @@ -51,8 +51,8 @@ public class LazyField extends LazyFieldLite { */ private final MessageLite defaultInstance; - public LazyField(MessageLite defaultInstance, - ExtensionRegistryLite extensionRegistry, ByteString bytes) { + public LazyField( + MessageLite defaultInstance, ExtensionRegistryLite extensionRegistry, ByteString bytes) { super(extensionRegistry, bytes); this.defaultInstance = defaultInstance; @@ -85,8 +85,8 @@ public class LazyField extends LazyFieldLite { // ==================================================== /** - * LazyEntry and LazyIterator are used to encapsulate the LazyField, when - * users iterate all fields from FieldSet. + * LazyEntry and LazyIterator are used to encapsulate the LazyField, when users iterate all fields + * from FieldSet. */ static class LazyEntry implements Entry { private Entry entry; @@ -118,7 +118,7 @@ public class LazyField extends LazyFieldLite { if (!(value instanceof MessageLite)) { throw new IllegalArgumentException( "LazyField now only used for MessageSet, " - + "and the value of MessageSet must be an instance of MessageLite"); + + "and the value of MessageSet must be an instance of MessageLite"); } return entry.getValue().setValue((MessageLite) value); } diff --git a/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java b/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java index 49ecfc0be6..d6b594d384 100644 --- a/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java +++ b/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java @@ -33,23 +33,23 @@ package com.google.protobuf; import java.io.IOException; /** - * LazyFieldLite encapsulates the logic of lazily parsing message fields. It stores - * the message in a ByteString initially and then parses it on-demand. + * LazyFieldLite encapsulates the logic of lazily parsing message fields. It stores the message in a + * ByteString initially and then parses it on-demand. * - * LazyFieldLite is thread-compatible: concurrent reads are safe once the proto that this + *

LazyFieldLite is thread-compatible: concurrent reads are safe once the proto that this * LazyFieldLite is a part of is no longer being mutated by its Builder. However, explicit * synchronization is needed under read/write situations. * - * When a LazyFieldLite is used in the context of a MessageLite object, its behavior is considered - * to be immutable and none of the setter methods in its API are expected to be invoked. All of the - * getters are expected to be thread-safe. When used in the context of a MessageLite.Builder, - * setters can be invoked, but there is no guarantee of thread safety. - * - * TODO(yatin,dweis): Consider splitting this class's functionality and put the mutable methods + *

When a LazyFieldLite is used in the context of a MessageLite object, its behavior is + * considered to be immutable and none of the setter methods in its API are expected to be invoked. + * All of the getters are expected to be thread-safe. When used in the context of a + * MessageLite.Builder, setters can be invoked, but there is no guarantee of thread safety. + * + *

TODO(yatin,dweis): Consider splitting this class's functionality and put the mutable methods * into a separate builder class to allow us to give stronger compile-time guarantees. * - * This class is internal implementation detail of the protobuf library, so you don't need to use it - * directly. + *

This class is internal implementation detail of the protobuf library, so you don't need to use + * it directly. * * @author xiangl@google.com (Xiang Li) */ @@ -57,23 +57,27 @@ public class LazyFieldLite { private static final ExtensionRegistryLite EMPTY_REGISTRY = ExtensionRegistryLite.getEmptyRegistry(); - /** + /* * The value associated with the LazyFieldLite object is stored in one or more of the following * three fields (delayedBytes, value, memoizedBytes). They should together be interpreted as * follows. + * * 1) delayedBytes can be non-null, while value and memoizedBytes is null. The object will be in - * this state while the value for the object has not yet been parsed. + * this state while the value for the object has not yet been parsed. + * * 2) Both delayedBytes and value are non-null. The object transitions to this state as soon as - * some caller needs to access the value (by invoking getValue()). + * some caller needs to access the value (by invoking getValue()). + * * 3) memoizedBytes is merely an optimization for calls to LazyFieldLite.toByteString() to avoid - * recomputing the ByteString representation on each call. Instead, when the value is parsed - * from delayedBytes, we will also assign the contents of delayedBytes to memoizedBytes (since - * that is the ByteString representation of value). + * recomputing the ByteString representation on each call. Instead, when the value is parsed from + * delayedBytes, we will also assign the contents of delayedBytes to memoizedBytes (since that is + * the ByteString representation of value). + * * 4) Finally, if the LazyFieldLite was created directly with a parsed MessageLite value, then - * delayedBytes will be null, and memoizedBytes will be initialized only upon the first call to - * LazyFieldLite.toByteString(). + * delayedBytes will be null, and memoizedBytes will be initialized only upon the first call to + * LazyFieldLite.toByteString(). * - * Given the above conditions, any caller that needs a serialized representation of this object + *

Given the above conditions, any caller that needs a serialized representation of this object * must first check if the memoizedBytes or delayedBytes ByteString is non-null and use it * directly; if both of those are null, it can look at the parsed value field. Similarly, any * caller that needs a parsed value must first check if the value field is already non-null, if @@ -84,16 +88,16 @@ public class LazyFieldLite { * A delayed-parsed version of the contents of this field. When this field is non-null, then the * "value" field is allowed to be null until the time that the value needs to be read. * - * When delayedBytes is non-null then {@code extensionRegistry} is required to also be non-null. - * {@code value} and {@code memoizedBytes} will be initialized lazily. + *

When delayedBytes is non-null then {@code extensionRegistry} is required to also be + * non-null. {@code value} and {@code memoizedBytes} will be initialized lazily. */ private ByteString delayedBytes; /** * An {@code ExtensionRegistryLite} for parsing bytes. It is non-null on a best-effort basis. It - * is only guaranteed to be non-null if this message was initialized using bytes and an - * {@code ExtensionRegistry}. If it directly had a value set then it will be null, unless it has - * been merged with another {@code LazyFieldLite} that had an {@code ExtensionRegistry}. + * is only guaranteed to be non-null if this message was initialized using bytes and an {@code + * ExtensionRegistry}. If it directly had a value set then it will be null, unless it has been + * merged with another {@code LazyFieldLite} that had an {@code ExtensionRegistry}. */ private ExtensionRegistryLite extensionRegistry; @@ -105,25 +109,20 @@ public class LazyFieldLite { /** * The memoized bytes for {@code value}. This is an optimization for the toByteString() method to - * not have to recompute its return-value on each invocation. - * TODO(yatin): Figure out whether this optimization is actually necessary. + * not have to recompute its return-value on each invocation. TODO(yatin): Figure out whether this + * optimization is actually necessary. */ private volatile ByteString memoizedBytes; - /** - * Constructs a LazyFieldLite with bytes that will be parsed lazily. - */ + /** Constructs a LazyFieldLite with bytes that will be parsed lazily. */ public LazyFieldLite(ExtensionRegistryLite extensionRegistry, ByteString bytes) { checkArguments(extensionRegistry, bytes); this.extensionRegistry = extensionRegistry; this.delayedBytes = bytes; } - /** - * Constructs a LazyFieldLite with no contents, and no ability to parse extensions. - */ - public LazyFieldLite() { - } + /** Constructs a LazyFieldLite with no contents, and no ability to parse extensions. */ + public LazyFieldLite() {} /** * Constructs a LazyFieldLite instance with a value. The LazyFieldLite may not be able to parse @@ -140,13 +139,13 @@ public class LazyFieldLite { if (this == o) { return true; } - + if (!(o instanceof LazyFieldLite)) { return false; } LazyFieldLite other = (LazyFieldLite) o; - + // Lazy fields do not work well with equals... If both are delayedBytes, we do not have a // mechanism to deserialize them so we rely on bytes equality. Otherwise we coerce into an // actual message (if necessary) and call equals on the message itself. This implies that two @@ -163,7 +162,7 @@ public class LazyFieldLite { return getValue(value2.getDefaultInstanceForType()).equals(value2); } } - + @Override public int hashCode() { // We can't provide a memoizable hash code for lazy fields. The byte strings may have different @@ -171,7 +170,7 @@ public class LazyFieldLite { // a message here if we were not already holding a value. return 1; } - + /** * Determines whether this LazyFieldLite instance represents the default instance of this type. */ @@ -183,8 +182,8 @@ public class LazyFieldLite { /** * Clears the value state of this instance. * - *

LazyField is not thread-safe for write access. Synchronizations are needed - * under read/write situations. + *

LazyField is not thread-safe for write access. Synchronizations are needed under read/write + * situations. */ public void clear() { // Don't clear the ExtensionRegistry. It might prove useful later on when merging in another @@ -198,8 +197,8 @@ public class LazyFieldLite { /** * Overrides the contents of this LazyField. * - *

LazyField is not thread-safe for write access. Synchronizations are needed - * under read/write situations. + *

LazyField is not thread-safe for write access. Synchronizations are needed under read/write + * situations. */ public void set(LazyFieldLite other) { this.delayedBytes = other.delayedBytes; @@ -218,7 +217,7 @@ public class LazyFieldLite { * Returns message instance. It may do some thread-safe delayed parsing of bytes. * * @param defaultInstance its message's default instance. It's also used to get parser for the - * message type. + * message type. */ public MessageLite getValue(MessageLite defaultInstance) { ensureInitialized(defaultInstance); @@ -228,8 +227,8 @@ public class LazyFieldLite { /** * Sets the value of the instance and returns the old value without delay parsing anything. * - *

LazyField is not thread-safe for write access. Synchronizations are needed - * under read/write situations. + *

LazyField is not thread-safe for write access. Synchronizations are needed under read/write + * situations. */ public MessageLite setValue(MessageLite value) { MessageLite originalValue = this.value; @@ -244,8 +243,8 @@ public class LazyFieldLite { * contain data. If the other field has an {@code ExtensionRegistry} but this does not, then this * field will copy over that {@code ExtensionRegistry}. * - *

LazyField is not thread-safe for write access. Synchronizations are needed - * under read/write situations. + *

LazyField is not thread-safe for write access. Synchronizations are needed under read/write + * situations. */ public void merge(LazyFieldLite other) { if (other.containsDefaultInstance()) { @@ -287,12 +286,12 @@ public class LazyFieldLite { // At this point we have two fully parsed messages. setValue(this.value.toBuilder().mergeFrom(other.value).build()); } - + /** * Merges another instance's contents from a stream. * - *

LazyField is not thread-safe for write access. Synchronizations are needed - * under read/write situations. + *

LazyField is not thread-safe for write access. Synchronizations are needed under read/write + * situations. */ public void mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws IOException { @@ -339,9 +338,7 @@ public class LazyFieldLite { } } - /** - * Sets this field with bytes to delay-parse. - */ + /** Sets this field with bytes to delay-parse. */ public void setByteString(ByteString bytes, ExtensionRegistryLite extensionRegistry) { checkArguments(extensionRegistry, bytes); this.delayedBytes = bytes; @@ -351,9 +348,8 @@ public class LazyFieldLite { } /** - * Due to the optional field can be duplicated at the end of serialized - * bytes, which will make the serialized size changed after LazyField - * parsed. Be careful when using this method. + * Due to the optional field can be duplicated at the end of serialized bytes, which will make the + * serialized size changed after LazyField parsed. Be careful when using this method. */ public int getSerializedSize() { // We *must* return delayed bytes size if it was ever set because the dependent messages may @@ -369,9 +365,7 @@ public class LazyFieldLite { } } - /** - * Returns a BytesString for this field in a thread-safe way. - */ + /** Returns a BytesString for this field in a thread-safe way. */ public ByteString toByteString() { if (memoizedBytes != null) { return memoizedBytes; @@ -394,10 +388,20 @@ public class LazyFieldLite { } } + /** Writes this lazy field into a {@link Writer}. */ + void writeTo(Writer writer, int fieldNumber) throws IOException { + if (memoizedBytes != null) { + writer.writeBytes(fieldNumber, memoizedBytes); + } else if (delayedBytes != null) { + writer.writeBytes(fieldNumber, delayedBytes); + } else if (value != null) { + writer.writeMessage(fieldNumber, value); + } else { + writer.writeBytes(fieldNumber, ByteString.EMPTY); + } + } - /** - * Might lazily parse the bytes that were previously passed in. Is thread-safe. - */ + /** Might lazily parse the bytes that were previously passed in. Is thread-safe. */ protected void ensureInitialized(MessageLite defaultInstance) { if (value != null) { return; @@ -409,8 +413,8 @@ public class LazyFieldLite { try { if (delayedBytes != null) { // The extensionRegistry shouldn't be null here since we have delayedBytes. - MessageLite parsedValue = defaultInstance.getParserForType() - .parseFrom(delayedBytes, extensionRegistry); + MessageLite parsedValue = + defaultInstance.getParserForType().parseFrom(delayedBytes, extensionRegistry); this.value = parsedValue; this.memoizedBytes = delayedBytes; } else { @@ -426,7 +430,6 @@ public class LazyFieldLite { } } - private static void checkArguments(ExtensionRegistryLite extensionRegistry, ByteString bytes) { if (extensionRegistry == null) { throw new NullPointerException("found null ExtensionRegistry"); diff --git a/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java b/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java index 6cfc14a512..a2de6f9cfe 100644 --- a/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java @@ -39,26 +39,22 @@ import java.util.List; import java.util.RandomAccess; /** - * An implementation of {@link LazyStringList} that wraps an ArrayList. Each - * element is one of String, ByteString, or byte[]. It caches the last one - * requested which is most likely the one needed next. This minimizes memory - * usage while satisfying the most common use cases. - *

- * Note that this implementation is not synchronized. - * If multiple threads access an ArrayList instance concurrently, - * and at least one of the threads modifies the list structurally, it - * must be synchronized externally. (A structural modification is - * any operation that adds or deletes one or more elements, or explicitly - * resizes the backing array; merely setting the value of an element is not - * a structural modification.) This is typically accomplished by - * synchronizing on some object that naturally encapsulates the list. - *

- * If the implementation is accessed via concurrent reads, this is thread safe. - * Conversions are done in a thread safe manner. It's possible that the - * conversion may happen more than once if two threads attempt to access the - * same element and the modifications were not visible to each other, but this - * will not result in any corruption of the list or change in behavior other - * than performance. + * An implementation of {@link LazyStringList} that wraps an ArrayList. Each element is one of + * String, ByteString, or byte[]. It caches the last one requested which is most likely the one + * needed next. This minimizes memory usage while satisfying the most common use cases. + * + *

Note that this implementation is not synchronized. If multiple threads access + * an ArrayList instance concurrently, and at least one of the threads modifies the list + * structurally, it must be synchronized externally. (A structural modification is any + * operation that adds or deletes one or more elements, or explicitly resizes the backing array; + * merely setting the value of an element is not a structural modification.) This is typically + * accomplished by synchronizing on some object that naturally encapsulates the list. + * + *

If the implementation is accessed via concurrent reads, this is thread safe. Conversions are + * done in a thread safe manner. It's possible that the conversion may happen more than once if two + * threads attempt to access the same element and the modifications were not visible to each other, + * but this will not result in any corruption of the list or change in behavior other than + * performance. * * @author jonp@google.com (Jon Perlow) */ @@ -66,6 +62,7 @@ public class LazyStringArrayList extends AbstractProtobufList implements LazyStringList, RandomAccess { private static final LazyStringArrayList EMPTY_LIST = new LazyStringArrayList(); + static { EMPTY_LIST.makeImmutable(); } @@ -177,8 +174,8 @@ public class LazyStringArrayList extends AbstractProtobufList ensureIsMutable(); // When copying from another LazyStringList, directly copy the underlying // elements rather than forcing each element to be decoded to a String. - Collection collection = c instanceof LazyStringList - ? ((LazyStringList) c).getUnderlyingElements() : c; + Collection collection = + c instanceof LazyStringList ? ((LazyStringList) c).getUnderlyingElements() : c; boolean ret = list.addAll(index, collection); modCount++; return ret; @@ -324,8 +321,7 @@ public class LazyStringArrayList extends AbstractProtobufList } } - private static class ByteArrayListView extends AbstractList - implements RandomAccess { + private static class ByteArrayListView extends AbstractList implements RandomAccess { private final LazyStringArrayList list; ByteArrayListView(LazyStringArrayList list) { @@ -368,8 +364,7 @@ public class LazyStringArrayList extends AbstractProtobufList return new ByteArrayListView(this); } - private static class ByteStringListView extends AbstractList - implements RandomAccess { + private static class ByteStringListView extends AbstractList implements RandomAccess { private final LazyStringArrayList list; ByteStringListView(LazyStringArrayList list) { @@ -419,5 +414,4 @@ public class LazyStringArrayList extends AbstractProtobufList } return this; } - } diff --git a/java/core/src/main/java/com/google/protobuf/LazyStringList.java b/java/core/src/main/java/com/google/protobuf/LazyStringList.java index 3eeedca1d3..6b55022caa 100644 --- a/java/core/src/main/java/com/google/protobuf/LazyStringList.java +++ b/java/core/src/main/java/com/google/protobuf/LazyStringList.java @@ -34,14 +34,12 @@ import java.util.Collection; import java.util.List; /** - * An interface extending {@code List} that also provides access to the - * items of the list as UTF8-encoded ByteString or byte[] objects. This is - * used by the protocol buffer implementation to support lazily converting bytes - * parsed over the wire to String objects until needed and also increases the - * efficiency of serialization if the String was never requested as the - * ByteString or byte[] is already cached. The ByteString methods are used in - * immutable API only and byte[] methods used in mutable API only for they use - * different representations for string/bytes fields. + * An interface extending {@code List} that also provides access to the items of the list as + * UTF8-encoded ByteString or byte[] objects. This is used by the protocol buffer implementation to + * support lazily converting bytes parsed over the wire to String objects until needed and also + * increases the efficiency of serialization if the String was never requested as the ByteString or + * byte[] is already cached. The ByteString methods are used in immutable API only and byte[] + * methods used in mutable API only for they use different representations for string/bytes fields. * * @author jonp@google.com (Jon Perlow) */ @@ -52,19 +50,19 @@ public interface LazyStringList extends ProtocolStringList { * * @param index index of the element to return * @return the element at the specified position in this list - * @throws IndexOutOfBoundsException if the index is out of range - * ({@code index < 0 || index >= size()}) + * @throws IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= + * size()}) */ ByteString getByteString(int index); /** - * Returns the element at the specified position in this list as an Object - * that will either be a String or a ByteString. + * Returns the element at the specified position in this list as an Object that will either be a + * String or a ByteString. * * @param index index of the element to return * @return the element at the specified position in this list - * @throws IndexOutOfBoundsException if the index is out of range - * ({@code index < 0 || index >= size()}) + * @throws IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= + * size()}) */ Object getRaw(int index); @@ -73,99 +71,91 @@ public interface LazyStringList extends ProtocolStringList { * * @param index index of the element to return * @return the element at the specified position in this list - * @throws IndexOutOfBoundsException if the index is out of range - * ({@code index < 0 || index >= size()}) + * @throws IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= + * size()}) */ byte[] getByteArray(int index); /** - * Appends the specified element to the end of this list (optional - * operation). + * Appends the specified element to the end of this list (optional operation). * * @param element element to be appended to this list - * @throws UnsupportedOperationException if the add operation - * is not supported by this list + * @throws UnsupportedOperationException if the add operation is not supported by this + * list */ void add(ByteString element); /** - * Appends the specified element to the end of this list (optional - * operation). + * Appends the specified element to the end of this list (optional operation). * * @param element element to be appended to this list - * @throws UnsupportedOperationException if the add operation - * is not supported by this list + * @throws UnsupportedOperationException if the add operation is not supported by this + * list */ void add(byte[] element); /** - * Replaces the element at the specified position in this list with the - * specified element (optional operation). + * Replaces the element at the specified position in this list with the specified element + * (optional operation). * * @param index index of the element to replace * @param element the element to be stored at the specified position - * @throws UnsupportedOperationException if the set operation - * is not supported by this list - * IndexOutOfBoundsException if the index is out of range - * ({@code index < 0 || index >= size()}) + * @throws UnsupportedOperationException if the set operation is not supported by this + * list IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= + * size()}) */ void set(int index, ByteString element); - + /** - * Replaces the element at the specified position in this list with the - * specified element (optional operation). + * Replaces the element at the specified position in this list with the specified element + * (optional operation). * * @param index index of the element to replace * @param element the element to be stored at the specified position - * @throws UnsupportedOperationException if the set operation - * is not supported by this list - * IndexOutOfBoundsException if the index is out of range - * ({@code index < 0 || index >= size()}) + * @throws UnsupportedOperationException if the set operation is not supported by this + * list IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= + * size()}) */ void set(int index, byte[] element); /** - * Appends all elements in the specified ByteString collection to the end of - * this list. + * Appends all elements in the specified ByteString collection to the end of this list. * * @param c collection whose elements are to be added to this list * @return true if this list changed as a result of the call - * @throws UnsupportedOperationException if the addAllByteString - * operation is not supported by this list + * @throws UnsupportedOperationException if the addAllByteString operation is not + * supported by this list */ boolean addAllByteString(Collection c); /** - * Appends all elements in the specified byte[] collection to the end of - * this list. + * Appends all elements in the specified byte[] collection to the end of this list. * * @param c collection whose elements are to be added to this list * @return true if this list changed as a result of the call - * @throws UnsupportedOperationException if the addAllByteArray - * operation is not supported by this list + * @throws UnsupportedOperationException if the addAllByteArray operation is not + * supported by this list */ boolean addAllByteArray(Collection c); /** - * Returns an unmodifiable List of the underlying elements, each of which is - * either a {@code String} or its equivalent UTF-8 encoded {@code ByteString} - * or byte[]. It is an error for the caller to modify the returned - * List, and attempting to do so will result in an - * {@link UnsupportedOperationException}. + * Returns an unmodifiable List of the underlying elements, each of which is either a {@code + * String} or its equivalent UTF-8 encoded {@code ByteString} or byte[]. It is an error for the + * caller to modify the returned List, and attempting to do so will result in an {@link + * UnsupportedOperationException}. */ List getUnderlyingElements(); /** - * Merges all elements from another LazyStringList into this one. This method - * differs from {@link #addAll(Collection)} on that underlying byte arrays are - * copied instead of reference shared. Immutable API doesn't need to use this - * method as byte[] is not used there at all. + * Merges all elements from another LazyStringList into this one. This method differs from {@link + * #addAll(Collection)} on that underlying byte arrays are copied instead of reference shared. + * Immutable API doesn't need to use this method as byte[] is not used there at all. */ void mergeFrom(LazyStringList other); /** - * Returns a mutable view of this list. Changes to the view will be made into - * the original list. This method is used in mutable API only. + * Returns a mutable view of this list. Changes to the view will be made into the original list. + * This method is used in mutable API only. */ List asByteArrayList(); diff --git a/java/core/src/main/java/com/google/protobuf/ListFieldSchema.java b/java/core/src/main/java/com/google/protobuf/ListFieldSchema.java new file mode 100755 index 0000000000..ebc8561a72 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ListFieldSchema.java @@ -0,0 +1,190 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.Internal.ProtobufList; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Utility class that aids in properly manipulating list fields for either the lite or full runtime. + */ +abstract class ListFieldSchema { + // Disallow construction. + private ListFieldSchema() {} + + private static final ListFieldSchema FULL_INSTANCE = new ListFieldSchemaFull(); + private static final ListFieldSchema LITE_INSTANCE = new ListFieldSchemaLite(); + + abstract List mutableListAt(Object msg, long offset); + + abstract void makeImmutableListAt(Object msg, long offset); + + abstract void mergeListsAt(Object msg, Object otherMsg, long offset); + + static ListFieldSchema full() { + return FULL_INSTANCE; + } + + static ListFieldSchema lite() { + return LITE_INSTANCE; + } + + /** Implementation for the full runtime. */ + private static final class ListFieldSchemaFull extends ListFieldSchema { + + private static final Class UNMODIFIABLE_LIST_CLASS = + Collections.unmodifiableList(Collections.emptyList()).getClass(); + + @Override + List mutableListAt(Object message, long offset) { + return mutableListAt(message, offset, AbstractProtobufList.DEFAULT_CAPACITY); + } + + @Override + void makeImmutableListAt(Object message, long offset) { + List list = (List) UnsafeUtil.getObject(message, offset); + Object immutable = null; + if (list instanceof LazyStringList) { + immutable = ((LazyStringList) list).getUnmodifiableView(); + } else if (UNMODIFIABLE_LIST_CLASS.isAssignableFrom(list.getClass())) { + // already immutable + return; + } else if (list instanceof PrimitiveNonBoxingCollection && list instanceof ProtobufList) { + if (((ProtobufList) list).isModifiable()) { + ((ProtobufList) list).makeImmutable(); + } + return; + } else { + immutable = Collections.unmodifiableList((List) list); + } + UnsafeUtil.putObject(message, offset, immutable); + } + + @SuppressWarnings("unchecked") + private static List mutableListAt(Object message, long offset, int additionalCapacity) { + List list = getList(message, offset); + if (list.isEmpty()) { + if (list instanceof LazyStringList) { + list = (List) new LazyStringArrayList(additionalCapacity); + } else if (list instanceof PrimitiveNonBoxingCollection && list instanceof ProtobufList) { + list = ((ProtobufList) list).mutableCopyWithCapacity(additionalCapacity); + } else { + list = new ArrayList(additionalCapacity); + } + UnsafeUtil.putObject(message, offset, list); + } else if (UNMODIFIABLE_LIST_CLASS.isAssignableFrom(list.getClass())) { + ArrayList newList = new ArrayList(list.size() + additionalCapacity); + newList.addAll(list); + list = newList; + UnsafeUtil.putObject(message, offset, list); + } else if (list instanceof UnmodifiableLazyStringList) { + LazyStringArrayList newList = new LazyStringArrayList(list.size() + additionalCapacity); + newList.addAll((UnmodifiableLazyStringList) list); + list = (List) newList; + UnsafeUtil.putObject(message, offset, list); + } else if (list instanceof PrimitiveNonBoxingCollection + && list instanceof ProtobufList + && !((ProtobufList) list).isModifiable()) { + list = ((ProtobufList) list).mutableCopyWithCapacity(list.size() + additionalCapacity); + UnsafeUtil.putObject(message, offset, list); + } + return list; + } + + @Override + void mergeListsAt(Object msg, Object otherMsg, long offset) { + List other = getList(otherMsg, offset); + List mine = mutableListAt(msg, offset, other.size()); + + int size = mine.size(); + int otherSize = other.size(); + if (size > 0 && otherSize > 0) { + mine.addAll(other); + } + + List merged = size > 0 ? mine : other; + UnsafeUtil.putObject(msg, offset, merged); + } + + @SuppressWarnings("unchecked") + static List getList(Object message, long offset) { + return (List) UnsafeUtil.getObject(message, offset); + } + } + + /** Implementation for the lite runtime. */ + private static final class ListFieldSchemaLite extends ListFieldSchema { + + @Override + List mutableListAt(Object message, long offset) { + ProtobufList list = getProtobufList(message, offset); + if (!list.isModifiable()) { + int size = list.size(); + list = + list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + UnsafeUtil.putObject(message, offset, list); + } + return list; + } + + @Override + void makeImmutableListAt(Object message, long offset) { + ProtobufList list = getProtobufList(message, offset); + list.makeImmutable(); + } + + @Override + void mergeListsAt(Object msg, Object otherMsg, long offset) { + ProtobufList mine = getProtobufList(msg, offset); + ProtobufList other = getProtobufList(otherMsg, offset); + + int size = mine.size(); + int otherSize = other.size(); + if (size > 0 && otherSize > 0) { + if (!mine.isModifiable()) { + mine = mine.mutableCopyWithCapacity(size + otherSize); + } + mine.addAll(other); + } + + ProtobufList merged = size > 0 ? mine : other; + UnsafeUtil.putObject(msg, offset, merged); + } + + @SuppressWarnings("unchecked") + static ProtobufList getProtobufList(Object message, long offset) { + return (ProtobufList) UnsafeUtil.getObject(message, offset); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/LongArrayList.java b/java/core/src/main/java/com/google/protobuf/LongArrayList.java index 9a5056be09..2dd15b4deb 100644 --- a/java/core/src/main/java/com/google/protobuf/LongArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/LongArrayList.java @@ -42,11 +42,10 @@ import java.util.RandomAccess; * * @author dweis@google.com (Daniel Weis) */ -final class LongArrayList - extends AbstractProtobufList +final class LongArrayList extends AbstractProtobufList implements LongList, RandomAccess, PrimitiveNonBoxingCollection { - private static final LongArrayList EMPTY_LIST = new LongArrayList(); + private static final LongArrayList EMPTY_LIST = new LongArrayList(new long[0], 0); static { EMPTY_LIST.makeImmutable(); } @@ -55,9 +54,7 @@ final class LongArrayList return EMPTY_LIST; } - /** - * The backing store for the list. - */ + /** The backing store for the list. */ private long[] array; /** @@ -66,16 +63,13 @@ final class LongArrayList */ private int size; - /** - * Constructs a new mutable {@code LongArrayList} with default capacity. - */ + /** Constructs a new mutable {@code LongArrayList} with default capacity. */ LongArrayList() { this(new long[DEFAULT_CAPACITY], 0); } /** - * Constructs a new mutable {@code LongArrayList} - * containing the same elements as {@code other}. + * Constructs a new mutable {@code LongArrayList} containing the same elements as {@code other}. */ private LongArrayList(long[] other, int size) { array = other; @@ -164,22 +158,34 @@ final class LongArrayList return previousValue; } + @Override + public boolean add(Long element) { + addLong(element); + return true; + } + @Override public void add(int index, Long element) { addLong(index, element); } - /** - * Like {@link #add(Long)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(Long)} but more efficient in that it doesn't box the element. */ @Override public void addLong(long element) { - addLong(size, element); + ensureIsMutable(); + if (size == array.length) { + // Resize to 1.5x the size + int length = ((size * 3) / 2) + 1; + long[] newArray = new long[length]; + + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + + array[size++] = element; } - /** - * Like {@link #add(int, Long)} but more efficient in that it doesn't box the element. - */ + /** Like {@link #add(int, Long)} but more efficient in that it doesn't box the element. */ private void addLong(int index, long element) { ensureIsMutable(); if (index < 0 || index > size) { @@ -245,7 +251,7 @@ final class LongArrayList ensureIsMutable(); for (int i = 0; i < size; i++) { if (o.equals(array[i])) { - System.arraycopy(array, i + 1, array, i, size - i); + System.arraycopy(array, i + 1, array, i, size - i - 1); size--; modCount++; return true; @@ -260,7 +266,7 @@ final class LongArrayList ensureIndexInRange(index); long value = array[index]; if (index < size - 1) { - System.arraycopy(array, index + 1, array, index, size - index); + System.arraycopy(array, index + 1, array, index, size - index - 1); } size--; modCount++; diff --git a/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java b/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java new file mode 100755 index 0000000000..84ca9ae0f7 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java @@ -0,0 +1,172 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +/** + * Dynamically generates a manifest-based (i.e. table-based) schema for a given protobuf message. + */ +@ExperimentalApi +final class ManifestSchemaFactory implements SchemaFactory { + + private final MessageInfoFactory messageInfoFactory; + + public ManifestSchemaFactory() { + this(getDefaultMessageInfoFactory()); + } + + private ManifestSchemaFactory(MessageInfoFactory messageInfoFactory) { + this.messageInfoFactory = checkNotNull(messageInfoFactory, "messageInfoFactory"); + } + + @Override + public Schema createSchema(Class messageType) { + SchemaUtil.requireGeneratedMessage(messageType); + + MessageInfo messageInfo = messageInfoFactory.messageInfoFor(messageType); + + // MessageSet has a special schema. + if (messageInfo.isMessageSetWireFormat()) { + if (GeneratedMessageLite.class.isAssignableFrom(messageType)) { + return MessageSetSchema.newSchema( + SchemaUtil.unknownFieldSetLiteSchema(), + ExtensionSchemas.lite(), + messageInfo.getDefaultInstance()); + } + return MessageSetSchema.newSchema( + SchemaUtil.proto2UnknownFieldSetSchema(), + ExtensionSchemas.full(), + messageInfo.getDefaultInstance()); + } + + return newSchema(messageType, messageInfo); + } + + private static Schema newSchema(Class messageType, MessageInfo messageInfo) { + if (GeneratedMessageLite.class.isAssignableFrom(messageType)) { + return isProto2(messageInfo) + ? MessageSchema.newSchema( + messageType, + messageInfo, + NewInstanceSchemas.lite(), + ListFieldSchema.lite(), + SchemaUtil.unknownFieldSetLiteSchema(), + ExtensionSchemas.lite(), + MapFieldSchemas.lite()) + : MessageSchema.newSchema( + messageType, + messageInfo, + NewInstanceSchemas.lite(), + ListFieldSchema.lite(), + SchemaUtil.unknownFieldSetLiteSchema(), + /* extensionSchema= */ null, + MapFieldSchemas.lite()); + } + return isProto2(messageInfo) + ? MessageSchema.newSchema( + messageType, + messageInfo, + NewInstanceSchemas.full(), + ListFieldSchema.full(), + SchemaUtil.proto2UnknownFieldSetSchema(), + ExtensionSchemas.full(), + MapFieldSchemas.full()) + : MessageSchema.newSchema( + messageType, + messageInfo, + NewInstanceSchemas.full(), + ListFieldSchema.full(), + SchemaUtil.proto3UnknownFieldSetSchema(), + /* extensionSchema= */ null, + MapFieldSchemas.full()); + } + + private static boolean isProto2(MessageInfo messageInfo) { + return messageInfo.getSyntax() == ProtoSyntax.PROTO2; + } + + private static MessageInfoFactory getDefaultMessageInfoFactory() { + return new CompositeMessageInfoFactory( + GeneratedMessageInfoFactory.getInstance(), getDescriptorMessageInfoFactory()); + } + + private static class CompositeMessageInfoFactory implements MessageInfoFactory { + private MessageInfoFactory[] factories; + + CompositeMessageInfoFactory(MessageInfoFactory... factories) { + this.factories = factories; + } + + @Override + public boolean isSupported(Class clazz) { + for (MessageInfoFactory factory : factories) { + if (factory.isSupported(clazz)) { + return true; + } + } + return false; + } + + @Override + public MessageInfo messageInfoFor(Class clazz) { + for (MessageInfoFactory factory : factories) { + if (factory.isSupported(clazz)) { + return factory.messageInfoFor(clazz); + } + } + throw new UnsupportedOperationException( + "No factory is available for message type: " + clazz.getName()); + } + } + + private static final MessageInfoFactory EMPTY_FACTORY = + new MessageInfoFactory() { + @Override + public boolean isSupported(Class clazz) { + return false; + } + + @Override + public MessageInfo messageInfoFor(Class clazz) { + throw new IllegalStateException("This should never be called."); + } + }; + + private static MessageInfoFactory getDescriptorMessageInfoFactory() { + try { + Class clazz = Class.forName("com.google.protobuf.DescriptorMessageInfoFactory"); + return (MessageInfoFactory) clazz.getDeclaredMethod("getInstance").invoke(null); + } catch (Exception e) { + return EMPTY_FACTORY; + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MapEntry.java b/java/core/src/main/java/com/google/protobuf/MapEntry.java index 0849b821cf..0175452288 100644 --- a/java/core/src/main/java/com/google/protobuf/MapEntry.java +++ b/java/core/src/main/java/com/google/protobuf/MapEntry.java @@ -41,11 +41,11 @@ import java.util.TreeMap; /** * Implements MapEntry messages. * - * In reflection API, map fields will be treated as repeated message fields and - * each map entry is accessed as a message. This MapEntry class is used to - * represent these map entry messages in reflection API. + *

In reflection API, map fields will be treated as repeated message fields and each map entry is + * accessed as a message. This MapEntry class is used to represent these map entry messages in + * reflection API. * - * Protobuf internal. Users shouldn't use this class. + *

Protobuf internal. Users shouldn't use this class. */ public final class MapEntry extends AbstractMessage { @@ -61,15 +61,16 @@ public final class MapEntry extends AbstractMessage { WireFormat.FieldType valueType) { super(keyType, defaultInstance.key, valueType, defaultInstance.value); this.descriptor = descriptor; - this.parser = new AbstractParser>() { + this.parser = + new AbstractParser>() { - @Override - public MapEntry parsePartialFrom( - CodedInputStream input, ExtensionRegistryLite extensionRegistry) - throws InvalidProtocolBufferException { - return new MapEntry(Metadata.this, input, extensionRegistry); - } - }; + @Override + public MapEntry parsePartialFrom( + CodedInputStream input, ExtensionRegistryLite extensionRegistry) + throws InvalidProtocolBufferException { + return new MapEntry(Metadata.this, input, extensionRegistry); + } + }; } } @@ -80,8 +81,10 @@ public final class MapEntry extends AbstractMessage { /** Create a default MapEntry instance. */ private MapEntry( Descriptor descriptor, - WireFormat.FieldType keyType, K defaultKey, - WireFormat.FieldType valueType, V defaultValue) { + WireFormat.FieldType keyType, + K defaultKey, + WireFormat.FieldType valueType, + V defaultValue) { this.key = defaultKey; this.value = defaultValue; this.metadata = new Metadata(descriptor, this, keyType, valueType); @@ -97,9 +100,7 @@ public final class MapEntry extends AbstractMessage { /** Parsing constructor. */ private MapEntry( - Metadata metadata, - CodedInputStream input, - ExtensionRegistryLite extensionRegistry) + Metadata metadata, CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { try { this.metadata = metadata; @@ -114,17 +115,17 @@ public final class MapEntry extends AbstractMessage { } /** - * Create a default MapEntry instance. A default MapEntry instance should be - * created only once for each map entry message type. Generated code should - * store the created default instance and use it later to create new MapEntry - * messages of the same type. + * Create a default MapEntry instance. A default MapEntry instance should be created only once for + * each map entry message type. Generated code should store the created default instance and use + * it later to create new MapEntry messages of the same type. */ public static MapEntry newDefaultInstance( Descriptor descriptor, - WireFormat.FieldType keyType, K defaultKey, - WireFormat.FieldType valueType, V defaultValue) { - return new MapEntry( - descriptor, keyType, defaultKey, valueType, defaultValue); + WireFormat.FieldType keyType, + K defaultKey, + WireFormat.FieldType valueType, + V defaultValue) { + return new MapEntry(descriptor, keyType, defaultKey, valueType, defaultValue); } public K getKey() { @@ -197,14 +198,17 @@ public final class MapEntry extends AbstractMessage { private void checkFieldDescriptor(FieldDescriptor field) { if (field.getContainingType() != metadata.descriptor) { throw new RuntimeException( - "Wrong FieldDescriptor \"" + field.getFullName() - + "\" used in message \"" + metadata.descriptor.getFullName()); + "Wrong FieldDescriptor \"" + + field.getFullName() + + "\" used in message \"" + + metadata.descriptor.getFullName()); } } @Override public boolean hasField(FieldDescriptor field) { - checkFieldDescriptor(field);; + checkFieldDescriptor(field); + ; // A MapEntry always contains two fields. return true; } @@ -215,22 +219,19 @@ public final class MapEntry extends AbstractMessage { Object result = field.getNumber() == 1 ? getKey() : getValue(); // Convert enums to EnumValueDescriptor. if (field.getType() == FieldDescriptor.Type.ENUM) { - result = field.getEnumType().findValueByNumberCreatingIfUnknown( - (java.lang.Integer) result); + result = field.getEnumType().findValueByNumberCreatingIfUnknown((java.lang.Integer) result); } return result; } @Override public int getRepeatedFieldCount(FieldDescriptor field) { - throw new RuntimeException( - "There is no repeated field in a map entry message."); + throw new RuntimeException("There is no repeated field in a map entry message."); } @Override public Object getRepeatedField(FieldDescriptor field, int index) { - throw new RuntimeException( - "There is no repeated field in a map entry message."); + throw new RuntimeException("There is no repeated field in a map entry message."); } @Override @@ -238,11 +239,8 @@ public final class MapEntry extends AbstractMessage { return UnknownFieldSet.getDefaultInstance(); } - /** - * Builder to create {@link MapEntry} messages. - */ - public static class Builder - extends AbstractMessage.Builder> { + /** Builder to create {@link MapEntry} messages. */ + public static class Builder extends AbstractMessage.Builder> { private final Metadata metadata; private K key; private V value; @@ -315,20 +313,21 @@ public final class MapEntry extends AbstractMessage { private void checkFieldDescriptor(FieldDescriptor field) { if (field.getContainingType() != metadata.descriptor) { throw new RuntimeException( - "Wrong FieldDescriptor \"" + field.getFullName() - + "\" used in message \"" + metadata.descriptor.getFullName()); + "Wrong FieldDescriptor \"" + + field.getFullName() + + "\" used in message \"" + + metadata.descriptor.getFullName()); } } @Override public Message.Builder newBuilderForField(FieldDescriptor field) { - checkFieldDescriptor(field);; + checkFieldDescriptor(field); + ; // This method should be called for message fields and in a MapEntry // message only the value field can possibly be a message field. - if (field.getNumber() != 2 - || field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { - throw new RuntimeException( - "\"" + field.getFullName() + "\" is not a message value field."); + if (field.getNumber() != 2 || field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { + throw new RuntimeException("\"" + field.getFullName() + "\" is not a message value field."); } return ((Message) value).newBuilderForType(); } @@ -369,16 +368,13 @@ public final class MapEntry extends AbstractMessage { } @Override - public Builder setRepeatedField(FieldDescriptor field, int index, - Object value) { - throw new RuntimeException( - "There is no repeated field in a map entry message."); + public Builder setRepeatedField(FieldDescriptor field, int index, Object value) { + throw new RuntimeException("There is no repeated field in a map entry message."); } @Override public Builder addRepeatedField(FieldDescriptor field, Object value) { - throw new RuntimeException( - "There is no repeated field in a map entry message."); + throw new RuntimeException("There is no repeated field in a map entry message."); } @Override @@ -427,14 +423,12 @@ public final class MapEntry extends AbstractMessage { @Override public int getRepeatedFieldCount(FieldDescriptor field) { - throw new RuntimeException( - "There is no repeated field in a map entry message."); + throw new RuntimeException("There is no repeated field in a map entry message."); } @Override public Object getRepeatedField(FieldDescriptor field, int index) { - throw new RuntimeException( - "There is no repeated field in a map entry message."); + throw new RuntimeException("There is no repeated field in a map entry message."); } @Override @@ -455,7 +449,7 @@ public final class MapEntry extends AbstractMessage { } return true; } - + /** Returns the metadata only for experimental runtime. */ final Metadata getMetadata() { return metadata; diff --git a/java/core/src/main/java/com/google/protobuf/MapEntryLite.java b/java/core/src/main/java/com/google/protobuf/MapEntryLite.java index dcb5dfad3d..ca2a3c2a97 100644 --- a/java/core/src/main/java/com/google/protobuf/MapEntryLite.java +++ b/java/core/src/main/java/com/google/protobuf/MapEntryLite.java @@ -37,11 +37,10 @@ import java.util.Map; /** * Implements the lite version of map entry messages. * - * This class serves as an utility class to help do serialization/parsing of - * map entries. It's used in generated code and also in the full version - * MapEntry message. + *

This class serves as an utility class to help do serialization/parsing of map entries. It's + * used in generated code and also in the full version MapEntry message. * - * Protobuf internal. Users shouldn't use. + *

Protobuf internal. Users shouldn't use. */ public class MapEntryLite { @@ -52,8 +51,10 @@ public class MapEntryLite { public final V defaultValue; public Metadata( - WireFormat.FieldType keyType, K defaultKey, - WireFormat.FieldType valueType, V defaultValue) { + WireFormat.FieldType keyType, + K defaultKey, + WireFormat.FieldType valueType, + V defaultValue) { this.keyType = keyType; this.defaultKey = defaultKey; this.valueType = valueType; @@ -70,8 +71,7 @@ public class MapEntryLite { /** Creates a default MapEntryLite message instance. */ private MapEntryLite( - WireFormat.FieldType keyType, K defaultKey, - WireFormat.FieldType valueType, V defaultValue) { + WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) { this.metadata = new Metadata(keyType, defaultKey, valueType, defaultValue); this.key = defaultKey; this.value = defaultValue; @@ -95,16 +95,13 @@ public class MapEntryLite { /** * Creates a default MapEntryLite message instance. * - * This method is used by generated code to create the default instance for - * a map entry message. The created default instance should be used to create - * new map entry messages of the same type. For each map entry message, only - * one default instance should be created. + *

This method is used by generated code to create the default instance for a map entry + * message. The created default instance should be used to create new map entry messages of the + * same type. For each map entry message, only one default instance should be created. */ public static MapEntryLite newDefaultInstance( - WireFormat.FieldType keyType, K defaultKey, - WireFormat.FieldType valueType, V defaultValue) { - return new MapEntryLite( - keyType, defaultKey, valueType, defaultValue); + WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) { + return new MapEntryLite(keyType, defaultKey, valueType, defaultValue); } static void writeTo(CodedOutputStream output, Metadata metadata, K key, V value) @@ -120,8 +117,11 @@ public class MapEntryLite { @SuppressWarnings("unchecked") static T parseField( - CodedInputStream input, ExtensionRegistryLite extensionRegistry, - WireFormat.FieldType type, T value) throws IOException { + CodedInputStream input, + ExtensionRegistryLite extensionRegistry, + WireFormat.FieldType type, + T value) + throws IOException { switch (type) { case MESSAGE: MessageLite.Builder subBuilder = ((MessageLite) value).toBuilder(); @@ -137,9 +137,9 @@ public class MapEntryLite { } /** - * Serializes the provided key and value as though they were wrapped by a {@link MapEntryLite} - * to the output stream. This helper method avoids allocation of a {@link MapEntryLite} - * built with a key and value and is called from generated code directly. + * Serializes the provided key and value as though they were wrapped by a {@link MapEntryLite} to + * the output stream. This helper method avoids allocation of a {@link MapEntryLite} built with a + * key and value and is called from generated code directly. */ public void serializeTo(CodedOutputStream output, int fieldNumber, K key, V value) throws IOException { @@ -149,9 +149,9 @@ public class MapEntryLite { } /** - * Computes the message size for the provided key and value as though they were wrapped - * by a {@link MapEntryLite}. This helper method avoids allocation of a {@link MapEntryLite} - * built with a key and value and is called from generated code directly. + * Computes the message size for the provided key and value as though they were wrapped by a + * {@link MapEntryLite}. This helper method avoids allocation of a {@link MapEntryLite} built with + * a key and value and is called from generated code directly. */ public int computeMessageSize(int fieldNumber, K key, V value) { return CodedOutputStream.computeTagSize(fieldNumber) @@ -160,8 +160,8 @@ public class MapEntryLite { } /** - * Parses an entry off of the input as a {@link Map.Entry}. This helper requires an allocation - * so using {@link #parseInto} is preferred if possible. + * Parses an entry off of the input as a {@link Map.Entry}. This helper requires an allocation so + * using {@link #parseInto} is preferred if possible. */ public Map.Entry parseEntry(ByteString bytes, ExtensionRegistryLite extensionRegistry) throws IOException { @@ -170,7 +170,7 @@ public class MapEntryLite { static Map.Entry parseEntry( CodedInputStream input, Metadata metadata, ExtensionRegistryLite extensionRegistry) - throws IOException{ + throws IOException { K key = metadata.defaultKey; V value = metadata.defaultValue; while (true) { @@ -192,12 +192,12 @@ public class MapEntryLite { } /** - * Parses an entry off of the input into the map. This helper avoids allocaton of a - * {@link MapEntryLite} by parsing directly into the provided {@link MapFieldLite}. + * Parses an entry off of the input into the map. This helper avoids allocaton of a {@link + * MapEntryLite} by parsing directly into the provided {@link MapFieldLite}. */ public void parseInto( MapFieldLite map, CodedInputStream input, ExtensionRegistryLite extensionRegistry) - throws IOException { + throws IOException { int length = input.readRawVarint32(); final int oldLimit = input.pushLimit(length); K key = metadata.defaultKey; diff --git a/java/core/src/main/java/com/google/protobuf/MapField.java b/java/core/src/main/java/com/google/protobuf/MapField.java index ad8ceb02a9..f487736065 100644 --- a/java/core/src/main/java/com/google/protobuf/MapField.java +++ b/java/core/src/main/java/com/google/protobuf/MapField.java @@ -44,38 +44,44 @@ import java.util.Set; /** * Internal representation of map fields in generated messages. * - * This class supports accessing the map field as a {@link Map} to be used in - * generated API and also supports accessing the field as a {@link List} to be - * used in reflection API. It keeps track of where the data is currently stored - * and do necessary conversions between map and list. + *

This class supports accessing the map field as a {@link Map} to be used in generated API and + * also supports accessing the field as a {@link List} to be used in reflection API. It keeps track + * of where the data is currently stored and do necessary conversions between map and list. * - * This class is a protobuf implementation detail. Users shouldn't use this - * class directly. + *

This class is a protobuf implementation detail. Users shouldn't use this class directly. * - * THREAD-SAFETY NOTE: Read-only access is thread-safe. Users can call getMap() - * and getList() concurrently in multiple threads. If write-access is needed, - * all access must be synchronized. + *

THREAD-SAFETY NOTE: Read-only access is thread-safe. Users can call getMap() and getList() + * concurrently in multiple threads. If write-access is needed, all access must be synchronized. */ public class MapField implements MutabilityOracle { + /** * Indicates where the data of this map field is currently stored. * - * MAP: Data is stored in mapData. - * LIST: Data is stored in listData. - * BOTH: mapData and listData have the same data. + *

    + *
  • MAP: Data is stored in mapData. + *
  • LIST: Data is stored in listData. + *
  • BOTH: mapData and listData have the same data. + *
* - * When the map field is accessed (through generated API or reflection API), - * it will shift between these 3 modes: + *

When the map field is accessed (through generated API or reflection API), it will shift + * between these 3 modes: * - * getMap() getList() getMutableMap() getMutableList() - * MAP MAP BOTH MAP LIST - * LIST BOTH LIST MAP LIST - * BOTH BOTH BOTH MAP LIST + *

+   *          getMap()   getList()    getMutableMap()   getMutableList()
+   * MAP      MAP        BOTH         MAP               LIST
+   * LIST     BOTH       LIST         MAP               LIST
+   * BOTH     BOTH       BOTH         MAP               LIST
+   * 
* - * As the map field changes its mode, the list/map reference returned in a - * previous method call may be invalidated. + *

As the map field changes its mode, the list/map reference returned in a previous method call + * may be invalidated. */ - private enum StorageMode {MAP, LIST, BOTH} + private enum StorageMode { + MAP, + LIST, + BOTH + } private volatile boolean isMutable; private volatile StorageMode mode; @@ -85,6 +91,7 @@ public class MapField implements MutabilityOracle { // Convert between a map entry Message and a key-value pair. private static interface Converter { Message convertKeyAndValueToMessage(K key, V value); + void convertMessageToKeyAndValue(Message message, Map map); Message getMessageDefaultInstance(); @@ -92,6 +99,7 @@ public class MapField implements MutabilityOracle { private static class ImmutableMessageConverter implements Converter { private final MapEntry defaultEntry; + public ImmutableMessageConverter(MapEntry defaultEntry) { this.defaultEntry = defaultEntry; } @@ -117,10 +125,7 @@ public class MapField implements MutabilityOracle { private final Converter converter; - private MapField( - Converter converter, - StorageMode mode, - Map mapData) { + private MapField(Converter converter, StorageMode mode, Map mapData) { this.converter = converter; this.isMutable = true; this.mode = mode; @@ -128,26 +133,20 @@ public class MapField implements MutabilityOracle { this.listData = null; } - private MapField( - MapEntry defaultEntry, - StorageMode mode, - Map mapData) { + private MapField(MapEntry defaultEntry, StorageMode mode, Map mapData) { this(new ImmutableMessageConverter(defaultEntry), mode, mapData); } /** Returns an immutable empty MapField. */ - public static MapField emptyMapField( - MapEntry defaultEntry) { - return new MapField( - defaultEntry, StorageMode.MAP, Collections.emptyMap()); + public static MapField emptyMapField(MapEntry defaultEntry) { + return new MapField(defaultEntry, StorageMode.MAP, Collections.emptyMap()); } /** Creates a new mutable empty MapField. */ public static MapField newMapField(MapEntry defaultEntry) { - return new MapField( - defaultEntry, StorageMode.MAP, new LinkedHashMap()); + return new MapField(defaultEntry, StorageMode.MAP, new LinkedHashMap()); } @@ -163,9 +162,7 @@ public class MapField implements MutabilityOracle { private List convertMapToList(MutatabilityAwareMap mapData) { List listData = new ArrayList(); for (Map.Entry entry : mapData.entrySet()) { - listData.add( - convertKeyAndValueToMessage( - entry.getKey(), entry.getValue())); + listData.add(convertKeyAndValueToMessage(entry.getKey(), entry.getValue())); } return listData; } @@ -229,8 +226,7 @@ public class MapField implements MutabilityOracle { /** Returns a deep copy of this MapField. */ public MapField copy() { - return new MapField( - converter, StorageMode.MAP, MapFieldLite.copy(getMap())); + return new MapField(converter, StorageMode.MAP, MapFieldLite.copy(getMap())); } /** Gets the content of this MapField as a read-only List. */ @@ -258,25 +254,20 @@ public class MapField implements MutabilityOracle { return listData; } - /** - * Gets the default instance of the message stored in the list view of this - * map field. - */ + /** Gets the default instance of the message stored in the list view of this map field. */ Message getMapEntryMessageDefaultInstance() { return converter.getMessageDefaultInstance(); } /** - * Makes this list immutable. All subsequent modifications will throw an - * {@link UnsupportedOperationException}. + * Makes this list immutable. All subsequent modifications will throw an {@link + * UnsupportedOperationException}. */ public void makeImmutable() { isMutable = false; } - /** - * Returns whether this field can be modified. - */ + /** Returns whether this field can be modified. */ public boolean isMutable() { return isMutable; } @@ -291,9 +282,7 @@ public class MapField implements MutabilityOracle { } } - /** - * An internal map that checks for mutability before delegating. - */ + /** An internal map that checks for mutability before delegating. */ private static class MutatabilityAwareMap implements Map { private final MutabilityOracle mutabilityOracle; private final Map delegate; @@ -388,9 +377,7 @@ public class MapField implements MutabilityOracle { return delegate.toString(); } - /** - * An internal collection that checks for mutability before delegating. - */ + /** An internal collection that checks for mutability before delegating. */ private static class MutatabilityAwareCollection implements Collection { private final MutabilityOracle mutabilityOracle; private final Collection delegate; @@ -487,9 +474,7 @@ public class MapField implements MutabilityOracle { } } - /** - * An internal set that checks for mutability before delegating. - */ + /** An internal set that checks for mutability before delegating. */ private static class MutatabilityAwareSet implements Set { private final MutabilityOracle mutabilityOracle; private final Set delegate; @@ -586,9 +571,7 @@ public class MapField implements MutabilityOracle { } } - /** - * An internal iterator that checks for mutability before delegating. - */ + /** An internal iterator that checks for mutability before delegating. */ private static class MutatabilityAwareIterator implements Iterator { private final MutabilityOracle mutabilityOracle; private final Iterator delegate; diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldLite.java b/java/core/src/main/java/com/google/protobuf/MapFieldLite.java index a8b3dd8849..7467487b63 100644 --- a/java/core/src/main/java/com/google/protobuf/MapFieldLite.java +++ b/java/core/src/main/java/com/google/protobuf/MapFieldLite.java @@ -42,8 +42,7 @@ import java.util.Set; /** * Internal representation of map fields in generated lite-runtime messages. * - * This class is a protobuf implementation detail. Users shouldn't use this - * class directly. + *

This class is a protobuf implementation detail. Users shouldn't use this class directly. */ public final class MapFieldLite extends LinkedHashMap { @@ -60,6 +59,7 @@ public final class MapFieldLite extends LinkedHashMap { @SuppressWarnings({"rawtypes", "unchecked"}) private static final MapFieldLite EMPTY_MAP_FIELD = new MapFieldLite(); + static { EMPTY_MAP_FIELD.makeImmutable(); } @@ -78,16 +78,19 @@ public final class MapFieldLite extends LinkedHashMap { } @SuppressWarnings({"unchecked", "cast"}) - @Override public Set> entrySet() { + @Override + public Set> entrySet() { return isEmpty() ? Collections.>emptySet() : super.entrySet(); } - @Override public void clear() { + @Override + public void clear() { ensureMutable(); super.clear(); } - @Override public V put(K key, V value) { + @Override + public V put(K key, V value) { ensureMutable(); checkNotNull(key); @@ -99,13 +102,15 @@ public final class MapFieldLite extends LinkedHashMap { return put(entry.getKey(), entry.getValue()); } - @Override public void putAll(Map m) { + @Override + public void putAll(Map m) { ensureMutable(); checkForNullKeysAndValues(m); super.putAll(m); } - @Override public V remove(Object key) { + @Override + public V remove(Object key) { ensureMutable(); return super.remove(key); } @@ -125,9 +130,8 @@ public final class MapFieldLite extends LinkedHashMap { } /** - * Checks whether two {@link Map}s are equal. We don't use the default equals - * method of {@link Map} because it compares by identity not by content for - * byte arrays. + * Checks whether two {@link Map}s are equal. We don't use the default equals method of {@link + * Map} because it compares by identity not by content for byte arrays. */ static boolean equals(Map a, Map b) { if (a == b) { @@ -147,9 +151,7 @@ public final class MapFieldLite extends LinkedHashMap { return true; } - /** - * Checks whether two map fields are equal. - */ + /** Checks whether two map fields are equal. */ @SuppressWarnings("unchecked") @Override public boolean equals(Object object) { @@ -168,15 +170,14 @@ public final class MapFieldLite extends LinkedHashMap { } /** - * Calculates the hash code for a {@link Map}. We don't use the default hash - * code method of {@link Map} because for byte arrays and protobuf enums it - * use {@link Object#hashCode()}. + * Calculates the hash code for a {@link Map}. We don't use the default hash code method of {@link + * Map} because for byte arrays and protobuf enums it use {@link Object#hashCode()}. */ static int calculateHashCodeForMap(Map a) { int result = 0; for (Map.Entry entry : a.entrySet()) { - result += calculateHashCodeForObject(entry.getKey()) - ^ calculateHashCodeForObject(entry.getValue()); + result += + calculateHashCodeForObject(entry.getKey()) ^ calculateHashCodeForObject(entry.getValue()); } return result; } @@ -195,9 +196,9 @@ public final class MapFieldLite extends LinkedHashMap { } /** - * Makes a deep copy of a {@link Map}. Immutable objects in the map will be - * shared (e.g., integers, strings, immutable messages) and mutable ones will - * have a copy (e.g., byte arrays, mutable messages). + * Makes a deep copy of a {@link Map}. Immutable objects in the map will be shared (e.g., + * integers, strings, immutable messages) and mutable ones will have a copy (e.g., byte arrays, + * mutable messages). */ @SuppressWarnings("unchecked") static Map copy(Map map) { @@ -214,16 +215,14 @@ public final class MapFieldLite extends LinkedHashMap { } /** - * Makes this field immutable. All subsequent modifications will throw an - * {@link UnsupportedOperationException}. + * Makes this field immutable. All subsequent modifications will throw an {@link + * UnsupportedOperationException}. */ public void makeImmutable() { isMutable = false; } - /** - * Returns whether this field can be modified. - */ + /** Returns whether this field can be modified. */ public boolean isMutable() { return isMutable; } diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldSchema.java b/java/core/src/main/java/com/google/protobuf/MapFieldSchema.java new file mode 100755 index 0000000000..195126e514 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MapFieldSchema.java @@ -0,0 +1,63 @@ +// 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. + +package com.google.protobuf; + +import java.util.Map; + +interface MapFieldSchema { + /** Returns the map data for mutation. */ + Map forMutableMapData(Object mapField); + + /** Returns the map data for read. */ + Map forMapData(Object mapField); + + /** Whether toImmutable() has been called on this map field. */ + boolean isImmutable(Object mapField); + + /** + * Returns an immutable instance of the map field. It may make the parameter immutable and return + * the parameter, or create an immutable copy. The status of the parameter after the call is + * undefined. + */ + Object toImmutable(Object mapField); + + /** Returns a new instance of the map field given a map default entry. */ + Object newMapField(Object mapDefaultEntry); + + /** Returns the metadata from a default entry. */ + MapEntryLite.Metadata forMapMetadata(Object mapDefaultEntry); + + /** Merges {@code srcMapField} into {@code destMapField}, and returns the merged instance. */ + Object mergeFrom(Object destMapField, Object srcMapField); + + /** Compute the serialized size for the map with a given field number. */ + int getSerializedSize(int fieldNumber, Object mapField, Object mapDefaultEntry); +} diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldSchemaFull.java b/java/core/src/main/java/com/google/protobuf/MapFieldSchemaFull.java new file mode 100755 index 0000000000..1efaab3bce --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MapFieldSchemaFull.java @@ -0,0 +1,112 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.MapEntryLite.Metadata; +import java.util.Map; + +class MapFieldSchemaFull implements MapFieldSchema { + @Override + public Map forMutableMapData(Object mapField) { + return ((MapField) mapField).getMutableMap(); + } + + @Override + public Map forMapData(Object mapField) { + return ((MapField) mapField).getMap(); + } + + @Override + public boolean isImmutable(Object mapField) { + return !((MapField) mapField).isMutable(); + } + + @Override + public Object toImmutable(Object mapField) { + ((MapField) mapField).makeImmutable(); + return mapField; + } + + @Override + public Object newMapField(Object mapDefaultEntry) { + return MapField.newMapField((MapEntry) mapDefaultEntry); + } + + @Override + public Metadata forMapMetadata(Object mapDefaultEntry) { + return ((MapEntry) mapDefaultEntry).getMetadata(); + } + + @Override + public Object mergeFrom(Object destMapField, Object srcMapField) { + return mergeFromFull(destMapField, srcMapField); + } + + @SuppressWarnings("unchecked") + private static Object mergeFromFull(Object destMapField, Object srcMapField) { + MapField mine = (MapField) destMapField; + MapField other = (MapField) srcMapField; + if (!mine.isMutable()) { + mine.copy(); + } + mine.mergeFrom(other); + return mine; + } + + @Override + public int getSerializedSize(int number, Object mapField, Object mapDefaultEntry) { + return getSerializedSizeFull(number, mapField, mapDefaultEntry); + } + + @SuppressWarnings("unchecked") + private static int getSerializedSizeFull( + int number, Object mapField, Object defaultEntryObject) { + // Full runtime allocates map fields lazily. + if (mapField == null) { + return 0; + } + + Map map = ((MapField) mapField).getMap(); + MapEntry defaultEntry = (MapEntry) defaultEntryObject; + if (map.isEmpty()) { + return 0; + } + int size = 0; + for (Map.Entry entry : map.entrySet()) { + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeLengthDelimitedFieldSize( + MapEntryLite.computeSerializedSize( + defaultEntry.getMetadata(), entry.getKey(), entry.getValue())); + } + return size; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java b/java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java new file mode 100755 index 0000000000..8a8c78de8a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java @@ -0,0 +1,107 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.MapEntryLite.Metadata; +import java.util.Map; + +class MapFieldSchemaLite implements MapFieldSchema { + + @Override + public Map forMutableMapData(Object mapField) { + return (MapFieldLite) mapField; + } + + @Override + public Metadata forMapMetadata(Object mapDefaultEntry) { + return ((MapEntryLite) mapDefaultEntry).getMetadata(); + } + + @Override + public Map forMapData(Object mapField) { + return (MapFieldLite) mapField; + } + + @Override + public boolean isImmutable(Object mapField) { + return !((MapFieldLite) mapField).isMutable(); + } + + @Override + public Object toImmutable(Object mapField) { + ((MapFieldLite) mapField).makeImmutable(); + return mapField; + } + + @Override + public Object newMapField(Object unused) { + return MapFieldLite.emptyMapField().mutableCopy(); + } + + @Override + public Object mergeFrom(Object destMapField, Object srcMapField) { + return mergeFromLite(destMapField, srcMapField); + } + + @SuppressWarnings("unchecked") + private static MapFieldLite mergeFromLite(Object destMapField, Object srcMapField) { + MapFieldLite mine = (MapFieldLite) destMapField; + MapFieldLite other = (MapFieldLite) srcMapField; + if (!other.isEmpty()) { + if (!mine.isMutable()) { + mine = mine.mutableCopy(); + } + mine.mergeFrom(other); + } + return mine; + } + + @Override + public int getSerializedSize(int fieldNumber, Object mapField, Object mapDefaultEntry) { + return getSerializedSizeLite(fieldNumber, mapField, mapDefaultEntry); + } + + @SuppressWarnings("unchecked") + private static int getSerializedSizeLite( + int fieldNumber, Object mapField, Object defaultEntry) { + MapFieldLite mapFieldLite = (MapFieldLite) mapField; + MapEntryLite defaultEntryLite = (MapEntryLite) defaultEntry; + + if (mapFieldLite.isEmpty()) { + return 0; + } + int size = 0; + for (Map.Entry entry : mapFieldLite.entrySet()) { + size += defaultEntryLite.computeMessageSize(fieldNumber, entry.getKey(), entry.getValue()); + } + return size; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java b/java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java new file mode 100755 index 0000000000..b398c61022 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java @@ -0,0 +1,53 @@ +// 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. + +package com.google.protobuf; + +final class MapFieldSchemas { + private static final MapFieldSchema FULL_SCHEMA = loadSchemaForFullRuntime(); + private static final MapFieldSchema LITE_SCHEMA = new MapFieldSchemaLite(); + + static MapFieldSchema full() { + return FULL_SCHEMA; + } + + static MapFieldSchema lite() { + return LITE_SCHEMA; + } + + private static MapFieldSchema loadSchemaForFullRuntime() { + try { + Class clazz = Class.forName("com.google.protobuf.MapFieldSchemaFull"); + return (MapFieldSchema) clazz.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + return null; + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/Message.java b/java/core/src/main/java/com/google/protobuf/Message.java index 0770d41787..9b3a015ba9 100644 --- a/java/core/src/main/java/com/google/protobuf/Message.java +++ b/java/core/src/main/java/com/google/protobuf/Message.java @@ -39,12 +39,11 @@ import java.util.Map; /** * Abstract interface implemented by Protocol Message objects. - *

- * See also {@link MessageLite}, which defines most of the methods that typical - * users care about. {@link Message} adds to it methods that are not available - * in the "lite" runtime. The biggest added features are introspection and - * reflection -- i.e., getting descriptors for the message type and accessing - * the field values dynamically. + * + *

See also {@link MessageLite}, which defines most of the methods that typical users care about. + * {@link Message} adds to it methods that are not available in the "lite" runtime. The biggest + * added features are introspection and reflection -- i.e., getting descriptors for the message type + * and accessing the field values dynamically. * * @author kenton@google.com Kenton Varda */ @@ -59,11 +58,10 @@ public interface Message extends MessageLite, MessageOrBuilder { // Comparison and hashing /** - * Compares the specified object with this message for equality. Returns - * {@code true} if the given object is a message of the same type (as - * defined by {@code getDescriptorForType()}) and has identical values for - * all of its fields. Subclasses must implement this; inheriting - * {@code Object.equals()} is incorrect. + * Compares the specified object with this message for equality. Returns {@code true} if the given + * object is a message of the same type (as defined by {@code getDescriptorForType()}) and has + * identical values for all of its fields. Subclasses must implement this; inheriting {@code + * Object.equals()} is incorrect. * * @param other object to be compared for equality with this message * @return {@code true} if the specified object is equal to this message @@ -72,10 +70,9 @@ public interface Message extends MessageLite, MessageOrBuilder { boolean equals(Object other); /** - * Returns the hash code value for this message. The hash code of a message - * should mix the message's type (object identity of the descriptor) with its - * contents (known and unknown field values). Subclasses must implement this; - * inheriting {@code Object.hashCode()} is incorrect. + * Returns the hash code value for this message. The hash code of a message should mix the + * message's type (object identity of the descriptor) with its contents (known and unknown field + * values). Subclasses must implement this; inheriting {@code Object.hashCode()} is incorrect. * * @return the hash code value for this message * @see Map#hashCode() @@ -87,9 +84,8 @@ public interface Message extends MessageLite, MessageOrBuilder { // Convenience methods. /** - * Converts the message to a string in protocol buffer text format. This is - * just a trivial wrapper around {@link - * TextFormat#printToString(MessageOrBuilder)}. + * Converts the message to a string in protocol buffer text format. This is just a trivial wrapper + * around {@link TextFormat.Printer#printToString(MessageOrBuilder)}. */ @Override String toString(); @@ -104,9 +100,7 @@ public interface Message extends MessageLite, MessageOrBuilder { @Override Builder toBuilder(); - /** - * Abstract interface implemented by Protocol Message builders. - */ + /** Abstract interface implemented by Protocol Message builders. */ interface Builder extends MessageLite.Builder, MessageOrBuilder { // (From MessageLite.Builder, re-declared here only for return type // covariance.) @@ -114,23 +108,21 @@ public interface Message extends MessageLite, MessageOrBuilder { Builder clear(); /** - * Merge {@code other} into the message being built. {@code other} must - * have the exact same type as {@code this} (i.e. - * {@code getDescriptorForType() == other.getDescriptorForType()}). + * Merge {@code other} into the message being built. {@code other} must have the exact same type + * as {@code this} (i.e. {@code getDescriptorForType() == other.getDescriptorForType()}). * - * Merging occurs as follows. For each field:
- * * For singular primitive fields, if the field is set in {@code other}, - * then {@code other}'s value overwrites the value in this message.
- * * For singular message fields, if the field is set in {@code other}, - * it is merged into the corresponding sub-message of this message - * using the same merging rules.
- * * For repeated fields, the elements in {@code other} are concatenated - * with the elements in this message.
- * * For oneof groups, if the other message has one of the fields set, - * the group of this message is cleared and replaced by the field - * of the other message, so that the oneof constraint is preserved. + *

Merging occurs as follows. For each field:
+ * * For singular primitive fields, if the field is set in {@code other}, then {@code other}'s + * value overwrites the value in this message.
+ * * For singular message fields, if the field is set in {@code other}, it is merged into the + * corresponding sub-message of this message using the same merging rules.
+ * * For repeated fields, the elements in {@code other} are concatenated with the elements in + * this message.
+ * * For oneof groups, if the other message has one of the fields set, the group of this message + * is cleared and replaced by the field of the other message, so that the oneof constraint is + * preserved. * - * This is equivalent to the {@code Message::MergeFrom} method in C++. + *

This is equivalent to the {@code Message::MergeFrom} method in C++. */ Builder mergeFrom(Message other); @@ -152,101 +144,97 @@ public interface Message extends MessageLite, MessageOrBuilder { Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws IOException; - /** - * Get the message's type's descriptor. - * See {@link Message#getDescriptorForType()}. - */ + /** Get the message's type's descriptor. See {@link Message#getDescriptorForType()}. */ @Override Descriptors.Descriptor getDescriptorForType(); /** - * Create a Builder for messages of the appropriate type for the given - * field. Messages built with this can then be passed to setField(), - * setRepeatedField(), or addRepeatedField(). + * Create a builder for messages of the appropriate type for the given field. The + * builder is NOT nested in the current builder. However, messages built with the + * builder can then be passed to the {@link #setField(Descriptors.FieldDescriptor, Object)}, + * {@link #setRepeatedField(Descriptors.FieldDescriptor, int, Object)}, or + * {@link #addRepeatedField(Descriptors.FieldDescriptor, Object)} + * method of the current builder. + * + *

To obtain a builder nested in the current builder, use + * {@link #getFieldBuilder(Descriptors.FieldDescriptor)} instead. */ Builder newBuilderForField(Descriptors.FieldDescriptor field); /** * Get a nested builder instance for the given field. - *

- * Normally, we hold a reference to the immutable message object for the - * message type field. Some implementations(the generated message builders), - * however, can also hold a reference to the builder object (a nested - * builder) for the field. - *

- * If the field is already backed up by a nested builder, the nested builder - * will be returned. Otherwise, a new field builder will be created and - * returned. The original message field (if exist) will be merged into the - * field builder, which will then be nested into its parent builder. - *

- * NOTE: implementations that do not support nested builders will throw - * UnsupportedOperationException. + * + *

Normally, we hold a reference to the immutable message object for the message type field. + * Some implementations(the generated message builders), however, can also hold a reference to + * the builder object (a nested builder) for the field. + * + *

If the field is already backed up by a nested builder, the nested builder will be + * returned. Otherwise, a new field builder will be created and returned. The original message + * field (if exist) will be merged into the field builder, which will then be nested into its + * parent builder. + * + *

NOTE: implementations that do not support nested builders will throw + * UnsupportedOperationException. */ Builder getFieldBuilder(Descriptors.FieldDescriptor field); /** * Get a nested builder instance for the given repeated field instance. - *

- * Normally, we hold a reference to the immutable message object for the - * message type field. Some implementations(the generated message builders), - * however, can also hold a reference to the builder object (a nested - * builder) for the field. - *

- * If the field is already backed up by a nested builder, the nested builder - * will be returned. Otherwise, a new field builder will be created and - * returned. The original message field (if exist) will be merged into the - * field builder, which will then be nested into its parent builder. - *

- * NOTE: implementations that do not support nested builders will throw - * UnsupportedOperationException. + * + *

Normally, we hold a reference to the immutable message object for the message type field. + * Some implementations(the generated message builders), however, can also hold a reference to + * the builder object (a nested builder) for the field. + * + *

If the field is already backed up by a nested builder, the nested builder will be + * returned. Otherwise, a new field builder will be created and returned. The original message + * field (if exist) will be merged into the field builder, which will then be nested into its + * parent builder. + * + *

NOTE: implementations that do not support nested builders will throw + * UnsupportedOperationException. */ - Builder getRepeatedFieldBuilder(Descriptors.FieldDescriptor field, - int index); + Builder getRepeatedFieldBuilder(Descriptors.FieldDescriptor field, int index); /** - * Sets a field to the given value. The value must be of the correct type - * for this field, i.e. the same type that - * {@link Message#getField(Descriptors.FieldDescriptor)} would return. + * Sets a field to the given value. The value must be of the correct type for this field, i.e. + * the same type that {@link Message#getField(Descriptors.FieldDescriptor)} would return. */ Builder setField(Descriptors.FieldDescriptor field, Object value); /** - * Clears the field. This is exactly equivalent to calling the generated - * "clear" accessor method corresponding to the field. + * Clears the field. This is exactly equivalent to calling the generated "clear" accessor method + * corresponding to the field. */ Builder clearField(Descriptors.FieldDescriptor field); /** - * Clears the oneof. This is exactly equivalent to calling the generated - * "clear" accessor method corresponding to the oneof. + * Clears the oneof. This is exactly equivalent to calling the generated "clear" accessor method + * corresponding to the oneof. */ Builder clearOneof(Descriptors.OneofDescriptor oneof); /** - * Sets an element of a repeated field to the given value. The value must - * be of the correct type for this field, i.e. the same type that - * {@link Message#getRepeatedField(Descriptors.FieldDescriptor,int)} would - * return. - * @throws IllegalArgumentException The field is not a repeated field, or - * {@code field.getContainingType() != getDescriptorForType()}. + * Sets an element of a repeated field to the given value. The value must be of the correct type + * for this field, i.e. the same type that {@link + * Message#getRepeatedField(Descriptors.FieldDescriptor,int)} would return. + * + * @throws IllegalArgumentException The field is not a repeated field, or {@code + * field.getContainingType() != getDescriptorForType()}. */ - Builder setRepeatedField(Descriptors.FieldDescriptor field, - int index, Object value); + Builder setRepeatedField(Descriptors.FieldDescriptor field, int index, Object value); /** * Like {@code setRepeatedField}, but appends the value as a new element. - * @throws IllegalArgumentException The field is not a repeated field, or - * {@code field.getContainingType() != getDescriptorForType()}. + * + * @throws IllegalArgumentException The field is not a repeated field, or {@code + * field.getContainingType() != getDescriptorForType()}. */ Builder addRepeatedField(Descriptors.FieldDescriptor field, Object value); /** Set the {@link UnknownFieldSet} for this message. */ Builder setUnknownFields(UnknownFieldSet unknownFields); - /** - * Merge some unknown fields into the {@link UnknownFieldSet} for this - * message. - */ + /** Merge some unknown fields into the {@link UnknownFieldSet} for this message. */ Builder mergeUnknownFields(UnknownFieldSet unknownFields); // --------------------------------------------------------------- diff --git a/java/core/src/main/java/com/google/protobuf/MessageInfo.java b/java/core/src/main/java/com/google/protobuf/MessageInfo.java new file mode 100755 index 0000000000..69e318696a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MessageInfo.java @@ -0,0 +1,43 @@ +// 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. + +package com.google.protobuf; + +/** A MesageInfo object describes a proto message type. */ +interface MessageInfo { + /** Gets syntax for this type. */ + ProtoSyntax getSyntax(); + + /** Whether this type is MessageSet. */ + boolean isMessageSetWireFormat(); + + /** Gets the default instance of this type. */ + MessageLite getDefaultInstance(); +} diff --git a/java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java b/java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java new file mode 100755 index 0000000000..005c26d05f --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java @@ -0,0 +1,41 @@ +// 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. + +package com.google.protobuf; + +/** A factory that creates {@link MessageInfo} instances for message types. */ +@ExperimentalApi +interface MessageInfoFactory { + /** Whether the message class is supported by this factory. */ + boolean isSupported(Class clazz); + + /** Returns a information of the message class. */ + MessageInfo messageInfoFor(Class clazz); +} diff --git a/java/core/src/main/java/com/google/protobuf/MessageLite.java b/java/core/src/main/java/com/google/protobuf/MessageLite.java index 88f531df3e..7d2ef33a17 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/MessageLite.java @@ -40,91 +40,79 @@ import java.io.OutputStream; /** * Abstract interface implemented by Protocol Message objects. * - *

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 follow line to the .proto file: + *

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 follow 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. + *

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 {@code optimize_for = CODE_SIZE}. This will make the generated - * code smaller while still supporting all the same features (at the expense of - * speed). {@code 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. + *

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 {@code optimize_for = + * CODE_SIZE}. This will make the generated code smaller while still supporting all the same + * features (at the expense of speed). {@code 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. * * @author kenton@google.com Kenton Varda */ public interface MessageLite extends MessageLiteOrBuilder { - /** - * Serializes the message and writes it to {@code output}. This does not - * flush or close the stream. + * Serializes the message and writes it to {@code output}. This does not flush or close the + * stream. */ void writeTo(CodedOutputStream output) throws IOException; /** - * Get the number of bytes required to encode this message. The result - * is only computed on the first call and memoized after that. + * Get the number of bytes required to encode this message. The result is only computed on the + * first call and memoized after that. */ int getSerializedSize(); - - /** - * Gets the parser for a message of the same type as this message. - */ + /** Gets the parser for a message of the same type as this message. */ Parser getParserForType(); // ----------------------------------------------------------------- // Convenience methods. /** - * Serializes the message to a {@code ByteString} and returns it. This is - * just a trivial wrapper around - * {@link #writeTo(CodedOutputStream)}. + * Serializes the message to a {@code ByteString} and returns it. This is just a trivial wrapper + * around {@link #writeTo(CodedOutputStream)}. */ ByteString toByteString(); /** - * Serializes the message to a {@code byte} array and returns it. This is - * just a trivial wrapper around - * {@link #writeTo(CodedOutputStream)}. + * Serializes the message to a {@code byte} array and returns it. This is just a trivial wrapper + * around {@link #writeTo(CodedOutputStream)}. */ byte[] toByteArray(); /** - * Serializes the message and writes it to {@code output}. This is just a - * trivial wrapper around {@link #writeTo(CodedOutputStream)}. This does - * not flush or close the stream. - *

- * NOTE: Protocol Buffers are not self-delimiting. Therefore, if you write - * any more data to the stream after the message, you must somehow ensure - * that the parser on the receiving end does not interpret this as being - * part of the protocol message. This can be done e.g. by writing the size - * of the message before the data, then making sure to limit the input to - * that size on the receiving end (e.g. by wrapping the InputStream in one - * which limits the input). Alternatively, just use - * {@link #writeDelimitedTo(OutputStream)}. + * Serializes the message and writes it to {@code output}. This is just a trivial wrapper around + * {@link #writeTo(CodedOutputStream)}. This does not flush or close the stream. + * + *

NOTE: Protocol Buffers are not self-delimiting. Therefore, if you write any more data to the + * stream after the message, you must somehow ensure that the parser on the receiving end does not + * interpret this as being part of the protocol message. This can be done e.g. by writing the size + * of the message before the data, then making sure to limit the input to that size on the + * receiving end (e.g. by wrapping the InputStream in one which limits the input). Alternatively, + * just use {@link #writeDelimitedTo(OutputStream)}. */ void writeTo(OutputStream output) throws IOException; /** - * Like {@link #writeTo(OutputStream)}, but writes the size of the message - * as a varint before writing the data. This allows more data to be written - * to the stream after the message without the need to delimit the message - * data yourself. Use {@link Builder#mergeDelimitedFrom(InputStream)} (or - * the static method {@code YourMessageType.parseDelimitedFrom(InputStream)}) - * to parse messages written by this method. + * Like {@link #writeTo(OutputStream)}, but writes the size of the message as a varint before + * writing the data. This allows more data to be written to the stream after the message without + * the need to delimit the message data yourself. Use {@link + * Builder#mergeDelimitedFrom(InputStream)} (or the static method {@code + * YourMessageType.parseDelimitedFrom(InputStream)}) to parse messages written by this method. */ void writeDelimitedTo(OutputStream output) throws IOException; @@ -132,210 +120,179 @@ public interface MessageLite extends MessageLiteOrBuilder { // ================================================================= // Builders - /** - * Constructs a new builder for a message of the same type as this message. - */ + /** Constructs a new builder for a message of the same type as this message. */ Builder newBuilderForType(); /** - * Constructs a builder initialized with the current message. Use this to - * derive a new message from the current one. + * Constructs a builder initialized with the current message. Use this to derive a new message + * from the current one. */ Builder toBuilder(); - /** - * Abstract interface implemented by Protocol Message builders. - */ + /** Abstract interface implemented by Protocol Message builders. */ interface Builder extends MessageLiteOrBuilder, Cloneable { /** Resets all fields to their default values. */ Builder clear(); /** - * Constructs the message based on the state of the Builder. Subsequent - * changes to the Builder will not affect the returned message. - * @throws UninitializedMessageException The message is missing one or more - * required fields (i.e. {@link #isInitialized()} returns false). - * Use {@link #buildPartial()} to bypass this check. + * Constructs the message based on the state of the Builder. Subsequent changes to the Builder + * will not affect the returned message. + * + * @throws UninitializedMessageException The message is missing one or more required fields + * (i.e. {@link #isInitialized()} returns false). Use {@link #buildPartial()} to bypass this + * check. */ MessageLite build(); /** - * Like {@link #build()}, but does not throw an exception if the message - * is missing required fields. Instead, a partial message is returned. - * Subsequent changes to the Builder will not affect the returned message. + * Like {@link #build()}, but does not throw an exception if the message is missing required + * fields. Instead, a partial message is returned. Subsequent changes to the Builder will not + * affect the returned message. */ MessageLite buildPartial(); /** * Clones the Builder. + * * @see Object#clone() */ Builder clone(); /** - * Parses a message of this type from the input and merges it with this - * message. + * Parses a message of this type from the input and merges it with this message. + * + *

Warning: This does not verify that all required fields are present in the input message. + * If you call {@link #build()} without setting all required fields, it will throw an {@link + * UninitializedMessageException}, which is a {@code RuntimeException} and thus might not be + * caught. There are a few good ways to deal with this: * - *

Warning: This does not verify that all required fields are present in - * the input message. If you call {@link #build()} without setting all - * required fields, it will throw an {@link UninitializedMessageException}, - * which is a {@code RuntimeException} and thus might not be caught. There - * are a few good ways to deal with this: *

    - *
  • Call {@link #isInitialized()} to verify that all required fields - * are set before building. - *
  • Use {@code buildPartial()} to build, which ignores missing - * required fields. + *
  • Call {@link #isInitialized()} to verify that all required fields are set before + * building. + *
  • Use {@code buildPartial()} to build, which ignores missing required fields. *
* - *

Note: The caller should call - * {@link CodedInputStream#checkLastTagWas(int)} after calling this to - * verify that the last tag seen was the appropriate end-group tag, - * or zero for EOF. + *

Note: The caller should call {@link CodedInputStream#checkLastTagWas(int)} after calling + * this to verify that the last tag seen was the appropriate end-group tag, or zero for EOF. */ Builder mergeFrom(CodedInputStream input) throws IOException; /** - * Like {@link Builder#mergeFrom(CodedInputStream)}, but also - * parses extensions. The extensions that you want to be able to parse - * must be registered in {@code extensionRegistry}. Extensions not in - * the registry will be treated as unknown fields. + * Like {@link Builder#mergeFrom(CodedInputStream)}, but also parses extensions. The extensions + * that you want to be able to parse must be registered in {@code extensionRegistry}. Extensions + * not in the registry will be treated as unknown fields. */ - Builder mergeFrom(CodedInputStream input, - ExtensionRegistryLite extensionRegistry) - throws IOException; + Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) + throws IOException; // --------------------------------------------------------------- // Convenience methods. /** - * Parse {@code data} as a message of this type and merge it with the - * message being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream)}. + * Parse {@code data} as a message of this type and merge it with the message being built. This + * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @return this */ Builder mergeFrom(ByteString data) throws InvalidProtocolBufferException; /** - * Parse {@code data} as a message of this type and merge it with the - * message being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. + * Parse {@code data} as a message of this type and merge it with the message being built. This + * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @return this */ - Builder mergeFrom(ByteString data, - ExtensionRegistryLite extensionRegistry) - throws InvalidProtocolBufferException; + Builder mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry) + throws InvalidProtocolBufferException; /** - * Parse {@code data} as a message of this type and merge it with the - * message being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream)}. + * Parse {@code data} as a message of this type and merge it with the message being built. This + * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @return this */ Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException; /** - * Parse {@code data} as a message of this type and merge it with the - * message being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream)}. + * Parse {@code data} as a message of this type and merge it with the message being built. This + * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @return this */ - Builder mergeFrom(byte[] data, int off, int len) - throws InvalidProtocolBufferException; + Builder mergeFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException; /** - * Parse {@code data} as a message of this type and merge it with the - * message being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. + * Parse {@code data} as a message of this type and merge it with the message being built. This + * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @return this */ - Builder mergeFrom(byte[] data, - ExtensionRegistryLite extensionRegistry) - throws InvalidProtocolBufferException; + Builder mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry) + throws InvalidProtocolBufferException; /** - * Parse {@code data} as a message of this type and merge it with the - * message being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. + * Parse {@code data} as a message of this type and merge it with the message being built. This + * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @return this */ - Builder mergeFrom(byte[] data, int off, int len, - ExtensionRegistryLite extensionRegistry) - throws InvalidProtocolBufferException; + Builder mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry) + throws InvalidProtocolBufferException; /** - * Parse a message of this type from {@code input} and merge it with the - * message being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream)}. Note that this method always - * reads the entire input (unless it throws an exception). If you - * want it to stop earlier, you will need to wrap your input in some - * wrapper stream that limits reading. Or, use - * {@link MessageLite#writeDelimitedTo(OutputStream)} to write your message - * and {@link #mergeDelimitedFrom(InputStream)} to read it. - *

- * Despite usually reading the entire input, this does not close the stream. + * Parse a message of this type from {@code input} and merge it with the message being built. + * This is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. Note that this + * method always reads the entire input (unless it throws an exception). If you want it + * to stop earlier, you will need to wrap your input in some wrapper stream that limits reading. + * Or, use {@link MessageLite#writeDelimitedTo(OutputStream)} to write your message and {@link + * #mergeDelimitedFrom(InputStream)} to read it. + * + *

Despite usually reading the entire input, this does not close the stream. * * @return this */ Builder mergeFrom(InputStream input) throws IOException; /** - * Parse a message of this type from {@code input} and merge it with the - * message being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. + * Parse a message of this type from {@code input} and merge it with the message being built. + * This is just a small wrapper around {@link + * #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @return this */ - Builder mergeFrom(InputStream input, - ExtensionRegistryLite extensionRegistry) - throws IOException; - + Builder mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry) + throws IOException; + /** - * Merge {@code other} into the message being built. {@code other} must - * have the exact same type as {@code this} (i.e. - * {@code getClass().equals(getDefaultInstanceForType().getClass())}). + * Merge {@code other} into the message being built. {@code other} must have the exact same type + * as {@code this} (i.e. {@code getClass().equals(getDefaultInstanceForType().getClass())}). * - * Merging occurs as follows. For each field:
- * * For singular primitive fields, if the field is set in {@code other}, - * then {@code other}'s value overwrites the value in this message.
- * * For singular message fields, if the field is set in {@code other}, - * it is merged into the corresponding sub-message of this message - * using the same merging rules.
- * * For repeated fields, the elements in {@code other} are concatenated - * with the elements in this message. - * * For oneof groups, if the other message has one of the fields set, - * the group of this message is cleared and replaced by the field - * of the other message, so that the oneof constraint is preserved. + *

Merging occurs as follows. For each field:
+ * * For singular primitive fields, if the field is set in {@code other}, then {@code other}'s + * value overwrites the value in this message.
+ * * For singular message fields, if the field is set in {@code other}, it is merged into the + * corresponding sub-message of this message using the same merging rules.
+ * * For repeated fields, the elements in {@code other} are concatenated with the elements in + * this message. * For oneof groups, if the other message has one of the fields set, the group + * of this message is cleared and replaced by the field of the other message, so that the oneof + * constraint is preserved. * - * This is equivalent to the {@code Message::MergeFrom} method in C++. + *

This is equivalent to the {@code Message::MergeFrom} method in C++. */ Builder mergeFrom(MessageLite other); /** - * Like {@link #mergeFrom(InputStream)}, but does not read until EOF. - * Instead, the size of the message (encoded as a varint) is read first, - * then the message data. Use - * {@link MessageLite#writeDelimitedTo(OutputStream)} to write messages in - * this format. + * Like {@link #mergeFrom(InputStream)}, but does not read until EOF. Instead, the size of the + * message (encoded as a varint) is read first, then the message data. Use {@link + * MessageLite#writeDelimitedTo(OutputStream)} to write messages in this format. * - * @return True if successful, or false if the stream is at EOF when the - * method starts. Any other error (including reaching EOF during - * parsing) will cause an exception to be thrown. + * @return True if successful, or false if the stream is at EOF when the method starts. Any + * other error (including reaching EOF during parsing) will cause an exception to be thrown. */ - boolean mergeDelimitedFrom(InputStream input) - throws IOException; + boolean mergeDelimitedFrom(InputStream input) throws IOException; - /** - * Like {@link #mergeDelimitedFrom(InputStream)} but supporting extensions. - */ - boolean mergeDelimitedFrom(InputStream input, - ExtensionRegistryLite extensionRegistry) - throws IOException; + /** Like {@link #mergeDelimitedFrom(InputStream)} but supporting extensions. */ + boolean mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry) + throws IOException; } } diff --git a/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java b/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java index 818386ce58..7a5ef3ed51 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java +++ b/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java @@ -31,30 +31,27 @@ package com.google.protobuf; /** - * Base interface for methods common to {@link MessageLite} - * and {@link MessageLite.Builder} to provide type equivalency. + * Base interface for methods common to {@link MessageLite} and {@link MessageLite.Builder} to + * provide type equivalency. * * @author jonp@google.com (Jon Perlow) */ public interface MessageLiteOrBuilder { /** - * Get an instance of the type with no fields set. Because no fields are set, - * all getters for singular fields will return default values and repeated - * fields will appear empty. - * This may or may not be a singleton. This differs from the - * {@code getDefaultInstance()} method of generated message classes in that - * this method is an abstract method of the {@code MessageLite} interface - * whereas {@code getDefaultInstance()} is a static method of a specific - * class. They return the same thing. + * Get an instance of the type with no fields set. Because no fields are set, all getters for + * singular fields will return default values and repeated fields will appear empty. This may or + * may not be a singleton. This differs from the {@code getDefaultInstance()} method of generated + * message classes in that this method is an abstract method of the {@code MessageLite} interface + * whereas {@code getDefaultInstance()} is a static method of a specific class. They return the + * same thing. */ MessageLite getDefaultInstanceForType(); /** - * Returns true if all required fields in the message and all embedded - * messages are set, false otherwise. + * Returns true if all required fields in the message and all embedded messages are set, false + * otherwise. * *

See also: {@link MessageOrBuilder#getInitializationErrorString()} */ boolean isInitialized(); - } diff --git a/java/core/src/main/java/com/google/protobuf/MessageOrBuilder.java b/java/core/src/main/java/com/google/protobuf/MessageOrBuilder.java index 5e7d782132..0254df99ac 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageOrBuilder.java +++ b/java/core/src/main/java/com/google/protobuf/MessageOrBuilder.java @@ -34,8 +34,8 @@ import java.util.List; import java.util.Map; /** - * Base interface for methods common to {@link Message} and - * {@link Message.Builder} to provide type equivalency. + * Base interface for methods common to {@link Message} and {@link Message.Builder} to provide type + * equivalency. * * @author jonp@google.com (Jon Perlow) */ @@ -46,95 +46,85 @@ public interface MessageOrBuilder extends MessageLiteOrBuilder { Message getDefaultInstanceForType(); /** - * Returns a list of field paths (e.g. "foo.bar.baz") of required fields - * which are not set in this message. You should call - * {@link MessageLiteOrBuilder#isInitialized()} first to check if there - * are any missing fields, as that method is likely to be much faster - * than this one even when the message is fully-initialized. + * Returns a list of field paths (e.g. "foo.bar.baz") of required fields which are not set in this + * message. You should call {@link MessageLiteOrBuilder#isInitialized()} first to check if there + * are any missing fields, as that method is likely to be much faster than this one even when the + * message is fully-initialized. */ List findInitializationErrors(); /** - * Returns a comma-delimited list of required fields which are not set - * in this message object. You should call - * {@link MessageLiteOrBuilder#isInitialized()} first to check if there - * are any missing fields, as that method is likely to be much faster - * than this one even when the message is fully-initialized. + * Returns a comma-delimited list of required fields which are not set in this message object. You + * should call {@link MessageLiteOrBuilder#isInitialized()} first to check if there are any + * missing fields, as that method is likely to be much faster than this one even when the message + * is fully-initialized. */ String getInitializationErrorString(); /** - * Get the message's type's descriptor. This differs from the - * {@code getDescriptor()} method of generated message classes in that - * this method is an abstract method of the {@code Message} interface - * whereas {@code getDescriptor()} is a static method of a specific class. - * They return the same thing. + * Get the message's type's descriptor. This differs from the {@code getDescriptor()} method of + * generated message classes in that this method is an abstract method of the {@code Message} + * interface whereas {@code getDescriptor()} is a static method of a specific class. They return + * the same thing. */ Descriptors.Descriptor getDescriptorForType(); /** - * Returns a collection of all the fields in this message which are set - * and their corresponding values. A singular ("required" or "optional") - * field is set iff hasField() returns true for that field. A "repeated" - * field is set iff getRepeatedFieldCount() is greater than zero. The - * values are exactly what would be returned by calling - * {@link #getField(Descriptors.FieldDescriptor)} for each field. The map - * is guaranteed to be a sorted map, so iterating over it will return fields - * in order by field number. - *
- * If this is for a builder, the returned map may or may not reflect future - * changes to the builder. Either way, the returned map is itself - * unmodifiable. + * Returns a collection of all the fields in this message which are set and their corresponding + * values. A singular ("required" or "optional") field is set iff hasField() returns true for that + * field. A "repeated" field is set iff getRepeatedFieldCount() is greater than zero. The values + * are exactly what would be returned by calling {@link #getField(Descriptors.FieldDescriptor)} + * for each field. The map is guaranteed to be a sorted map, so iterating over it will return + * fields in order by field number.
+ * If this is for a builder, the returned map may or may not reflect future changes to the + * builder. Either way, the returned map is itself unmodifiable. */ Map getAllFields(); /** * Returns true if the given oneof is set. - * @throws IllegalArgumentException if - * {@code oneof.getContainingType() != getDescriptorForType()}. + * + * @throws IllegalArgumentException if {@code oneof.getContainingType() != + * getDescriptorForType()}. */ boolean hasOneof(Descriptors.OneofDescriptor oneof); - /** - * Obtains the FieldDescriptor if the given oneof is set. Returns null - * if no field is set. - */ - Descriptors.FieldDescriptor getOneofFieldDescriptor( - Descriptors.OneofDescriptor oneof); + /** Obtains the FieldDescriptor if the given oneof is set. Returns null if no field is set. */ + Descriptors.FieldDescriptor getOneofFieldDescriptor(Descriptors.OneofDescriptor oneof); /** - * Returns true if the given field is set. This is exactly equivalent to - * calling the generated "has" accessor method corresponding to the field. - * @throws IllegalArgumentException The field is a repeated field, or - * {@code field.getContainingType() != getDescriptorForType()}. + * Returns true if the given field is set. This is exactly equivalent to calling the generated + * "has" accessor method corresponding to the field. + * + * @throws IllegalArgumentException The field is a repeated field, or {@code + * field.getContainingType() != getDescriptorForType()}. */ boolean hasField(Descriptors.FieldDescriptor field); /** - * Obtains the value of the given field, or the default value if it is - * not set. For primitive fields, the boxed primitive value is returned. - * For enum fields, the EnumValueDescriptor for the value is returned. For - * embedded message fields, the sub-message is returned. For repeated + * Obtains the value of the given field, or the default value if it is not set. For primitive + * fields, the boxed primitive value is returned. For enum fields, the EnumValueDescriptor for the + * value is returned. For embedded message fields, the sub-message is returned. For repeated * fields, a java.util.List is returned. */ Object getField(Descriptors.FieldDescriptor field); /** - * Gets the number of elements of a repeated field. This is exactly - * equivalent to calling the generated "Count" accessor method corresponding - * to the field. - * @throws IllegalArgumentException The field is not a repeated field, or - * {@code field.getContainingType() != getDescriptorForType()}. + * Gets the number of elements of a repeated field. This is exactly equivalent to calling the + * generated "Count" accessor method corresponding to the field. + * + * @throws IllegalArgumentException The field is not a repeated field, or {@code + * field.getContainingType() != getDescriptorForType()}. */ int getRepeatedFieldCount(Descriptors.FieldDescriptor field); /** - * Gets an element of a repeated field. For primitive fields, the boxed - * primitive value is returned. For enum fields, the EnumValueDescriptor - * for the value is returned. For embedded message fields, the sub-message - * is returned. - * @throws IllegalArgumentException The field is not a repeated field, or - * {@code field.getContainingType() != getDescriptorForType()}. + * Gets an element of a repeated field. For primitive fields, the boxed primitive value is + * returned. For enum fields, the EnumValueDescriptor for the value is returned. For embedded + * message fields, the sub-message is returned. + * + * @throws IllegalArgumentException The field is not a repeated field, or {@code + * field.getContainingType() != getDescriptorForType()}. */ Object getRepeatedField(Descriptors.FieldDescriptor field, int index); diff --git a/java/core/src/main/java/com/google/protobuf/MessageReflection.java b/java/core/src/main/java/com/google/protobuf/MessageReflection.java index 69ad7ddf94..6741e1cb9c 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageReflection.java +++ b/java/core/src/main/java/com/google/protobuf/MessageReflection.java @@ -54,20 +54,19 @@ class MessageReflection { message.getDescriptorForType().getOptions().getMessageSetWireFormat(); if (alwaysWriteRequiredFields) { fields = new TreeMap(fields); - for (final FieldDescriptor field : - message.getDescriptorForType().getFields()) { + for (final FieldDescriptor field : message.getDescriptorForType().getFields()) { if (field.isRequired() && !fields.containsKey(field)) { fields.put(field, message.getField(field)); } } } - for (final Map.Entry entry : - fields.entrySet()) { + for (final Map.Entry entry : fields.entrySet()) { final Descriptors.FieldDescriptor field = entry.getKey(); final Object value = entry.getValue(); - if (isMessageSet && field.isExtension() && - field.getType() == Descriptors.FieldDescriptor.Type.MESSAGE && - !field.isRepeated()) { + if (isMessageSet + && field.isExtension() + && field.getType() == Descriptors.FieldDescriptor.Type.MESSAGE + && !field.isRepeated()) { output.writeMessageSetExtension(field.getNumber(), (Message) value); } else { FieldSet.writeField(field, value, output); @@ -82,22 +81,20 @@ class MessageReflection { } } - static int getSerializedSize( - Message message, - Map fields) { + static int getSerializedSize(Message message, Map fields) { int size = 0; final boolean isMessageSet = message.getDescriptorForType().getOptions().getMessageSetWireFormat(); - for (final Map.Entry entry : - fields.entrySet()) { + for (final Map.Entry entry : fields.entrySet()) { final Descriptors.FieldDescriptor field = entry.getKey(); final Object value = entry.getValue(); - if (isMessageSet && field.isExtension() && - field.getType() == Descriptors.FieldDescriptor.Type.MESSAGE && - !field.isRepeated()) { - size += CodedOutputStream.computeMessageSetExtensionSize( - field.getNumber(), (Message) value); + if (isMessageSet + && field.isExtension() + && field.getType() == Descriptors.FieldDescriptor.Type.MESSAGE + && !field.isRepeated()) { + size += + CodedOutputStream.computeMessageSetExtensionSize(field.getNumber(), (Message) value); } else { size += FieldSet.computeFieldSize(field, value); } @@ -126,9 +123,7 @@ class MessageReflection { @SuppressWarnings("unchecked") static boolean isInitialized(MessageOrBuilder message) { // Check that all required fields are present. - for (final Descriptors.FieldDescriptor field : message - .getDescriptorForType() - .getFields()) { + for (final Descriptors.FieldDescriptor field : message.getDescriptorForType().getFields()) { if (field.isRequired()) { if (!message.hasField(field)) { return false; @@ -142,8 +137,7 @@ class MessageReflection { final Descriptors.FieldDescriptor field = entry.getKey(); if (field.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { - for (final Message element - : (List) entry.getValue()) { + for (final Message element : (List) entry.getValue()) { if (!element.isInitialized()) { return false; } @@ -159,31 +153,24 @@ class MessageReflection { return true; } - private static String subMessagePrefix(final String prefix, - final Descriptors.FieldDescriptor field, - final int index) { + private static String subMessagePrefix( + final String prefix, final Descriptors.FieldDescriptor field, final int index) { final StringBuilder result = new StringBuilder(prefix); if (field.isExtension()) { - result.append('(') - .append(field.getFullName()) - .append(')'); + result.append('(').append(field.getFullName()).append(')'); } else { result.append(field.getName()); } if (index != -1) { - result.append('[') - .append(index) - .append(']'); + result.append('[').append(index).append(']'); } result.append('.'); return result.toString(); } - private static void findMissingFields(final MessageOrBuilder message, - final String prefix, - final List results) { - for (final Descriptors.FieldDescriptor field : - message.getDescriptorForType().getFields()) { + private static void findMissingFields( + final MessageOrBuilder message, final String prefix, final List results) { + for (final Descriptors.FieldDescriptor field : message.getDescriptorForType().getFields()) { if (field.isRequired() && !message.hasField(field)) { results.add(prefix + field.getName()); } @@ -198,15 +185,13 @@ class MessageReflection { if (field.isRepeated()) { int i = 0; for (final Object element : (List) value) { - findMissingFields((MessageOrBuilder) element, - subMessagePrefix(prefix, field, i++), - results); + findMissingFields( + (MessageOrBuilder) element, subMessagePrefix(prefix, field, i++), results); } } else { if (message.hasField(field)) { - findMissingFields((MessageOrBuilder) value, - subMessagePrefix(prefix, field, -1), - results); + findMissingFields( + (MessageOrBuilder) value, subMessagePrefix(prefix, field, -1), results); } } } @@ -214,11 +199,10 @@ class MessageReflection { } /** - * Populates {@code this.missingFields} with the full "path" of each missing - * required field in the given message. + * Populates {@code this.missingFields} with the full "path" of each missing required field in the + * given message. */ - static List findMissingFields( - final MessageOrBuilder message) { + static List findMissingFields(final MessageOrBuilder message) { final List results = new ArrayList(); findMissingFields(message, "", results); return results; @@ -226,12 +210,11 @@ class MessageReflection { static interface MergeTarget { enum ContainerType { - MESSAGE, EXTENSION_SET + MESSAGE, + EXTENSION_SET } - /** - * Returns the descriptor for the target. - */ + /** Returns the descriptor for the target. */ public Descriptors.Descriptor getDescriptorForType(); public ContainerType getContainerType(); @@ -240,21 +223,19 @@ class MessageReflection { ExtensionRegistry registry, String name); public ExtensionRegistry.ExtensionInfo findExtensionByNumber( - ExtensionRegistry registry, Descriptors.Descriptor containingType, - int fieldNumber); + ExtensionRegistry registry, Descriptors.Descriptor containingType, int fieldNumber); /** - * Obtains the value of the given field, or the default value if it is not - * set. For primitive fields, the boxed primitive value is returned. For - * enum fields, the EnumValueDescriptor for the value is returned. For - * embedded message fields, the sub-message is returned. For repeated + * Obtains the value of the given field, or the default value if it is not set. For primitive + * fields, the boxed primitive value is returned. For enum fields, the EnumValueDescriptor for + * the value is returned. For embedded message fields, the sub-message is returned. For repeated * fields, a java.util.List is returned. */ public Object getField(Descriptors.FieldDescriptor field); /** - * Returns true if the given field is set. This is exactly equivalent to - * calling the generated "has" accessor method corresponding to the field. + * Returns true if the given field is set. This is exactly equivalent to calling the generated + * "has" accessor method corresponding to the field. * * @throws IllegalArgumentException The field is a repeated field, or {@code * field.getContainingType() != getDescriptorForType()}. @@ -262,106 +243,106 @@ class MessageReflection { boolean hasField(Descriptors.FieldDescriptor field); /** - * Sets a field to the given value. The value must be of the correct type - * for this field, i.e. the same type that - * {@link Message#getField(Descriptors.FieldDescriptor)} - * would return. + * Sets a field to the given value. The value must be of the correct type for this field, i.e. + * the same type that {@link Message#getField(Descriptors.FieldDescriptor)} would return. */ MergeTarget setField(Descriptors.FieldDescriptor field, Object value); /** - * Clears the field. This is exactly equivalent to calling the generated - * "clear" accessor method corresponding to the field. + * Clears the field. This is exactly equivalent to calling the generated "clear" accessor method + * corresponding to the field. */ MergeTarget clearField(Descriptors.FieldDescriptor field); /** - * Sets an element of a repeated field to the given value. The value must - * be of the correct type for this field, i.e. the same type that {@link + * Sets an element of a repeated field to the given value. The value must be of the correct type + * for this field, i.e. the same type that {@link * Message#getRepeatedField(Descriptors.FieldDescriptor, int)} would return. * - * @throws IllegalArgumentException The field is not a repeated field, or - * {@code field.getContainingType() != - * getDescriptorForType()}. + * @throws IllegalArgumentException The field is not a repeated field, or {@code + * field.getContainingType() != getDescriptorForType()}. */ - MergeTarget setRepeatedField(Descriptors.FieldDescriptor field, - int index, Object value); + MergeTarget setRepeatedField(Descriptors.FieldDescriptor field, int index, Object value); /** * Like {@code setRepeatedField}, but appends the value as a new element. * - * @throws IllegalArgumentException The field is not a repeated field, or - * {@code field.getContainingType() != - * getDescriptorForType()}. + * @throws IllegalArgumentException The field is not a repeated field, or {@code + * field.getContainingType() != getDescriptorForType()}. */ - MergeTarget addRepeatedField(Descriptors.FieldDescriptor field, - Object value); + MergeTarget addRepeatedField(Descriptors.FieldDescriptor field, Object value); /** * Returns true if the given oneof is set. * - * @throws IllegalArgumentException if - * {@code oneof.getContainingType() != getDescriptorForType()}. + * @throws IllegalArgumentException if {@code oneof.getContainingType() != + * getDescriptorForType()}. */ boolean hasOneof(Descriptors.OneofDescriptor oneof); /** - * Clears the oneof. This is exactly equivalent to calling the generated - * "clear" accessor method corresponding to the oneof. + * Clears the oneof. This is exactly equivalent to calling the generated "clear" accessor method + * corresponding to the oneof. */ MergeTarget clearOneof(Descriptors.OneofDescriptor oneof); - /** - * Obtains the FieldDescriptor if the given oneof is set. Returns null - * if no field is set. - */ + /** Obtains the FieldDescriptor if the given oneof is set. Returns null if no field is set. */ Descriptors.FieldDescriptor getOneofFieldDescriptor(Descriptors.OneofDescriptor oneof); /** - * Parse the input stream into a sub field group defined based on either - * FieldDescriptor or the default instance. + * Parse the input stream into a sub field group defined based on either FieldDescriptor or the + * default instance. */ - Object parseGroup(CodedInputStream input, ExtensionRegistryLite registry, - Descriptors.FieldDescriptor descriptor, Message defaultInstance) + Object parseGroup( + CodedInputStream input, + ExtensionRegistryLite registry, + Descriptors.FieldDescriptor descriptor, + Message defaultInstance) throws IOException; /** - * Parse the input stream into a sub field message defined based on either - * FieldDescriptor or the default instance. + * Parse the input stream into a sub field message defined based on either FieldDescriptor or + * the default instance. */ - Object parseMessage(CodedInputStream input, ExtensionRegistryLite registry, - Descriptors.FieldDescriptor descriptor, Message defaultInstance) + Object parseMessage( + CodedInputStream input, + ExtensionRegistryLite registry, + Descriptors.FieldDescriptor descriptor, + Message defaultInstance) throws IOException; /** - * Parse from a ByteString into a sub field message defined based on either - * FieldDescriptor or the default instance. There isn't a varint indicating - * the length of the message at the beginning of the input ByteString. + * Parse from a ByteString into a sub field message defined based on either FieldDescriptor or + * the default instance. There isn't a varint indicating the length of the message at the + * beginning of the input ByteString. */ Object parseMessageFromBytes( - ByteString bytes, ExtensionRegistryLite registry, - Descriptors.FieldDescriptor descriptor, Message defaultInstance) + ByteString bytes, + ExtensionRegistryLite registry, + Descriptors.FieldDescriptor descriptor, + Message defaultInstance) throws IOException; - /** - * Returns the UTF8 validation level for the field. - */ - WireFormat.Utf8Validation getUtf8Validation(Descriptors.FieldDescriptor - descriptor); + /** Returns the UTF8 validation level for the field. */ + WireFormat.Utf8Validation getUtf8Validation(Descriptors.FieldDescriptor descriptor); /** - * Returns a new merge target for a sub-field. When defaultInstance is - * provided, it indicates the descriptor is for an extension type, and - * implementations should create a new instance from the defaultInstance - * prototype directly. + * Returns a new merge target for a sub-field. When defaultInstance is provided, it indicates + * the descriptor is for an extension type, and implementations should create a new instance + * from the defaultInstance prototype directly. */ MergeTarget newMergeTargetForField( - Descriptors.FieldDescriptor descriptor, - Message defaultInstance); + Descriptors.FieldDescriptor descriptor, Message defaultInstance); /** - * Finishes the merge and returns the underlying object. + * Returns an empty merge target for a sub-field. When defaultInstance is provided, it indicates + * the descriptor is for an extension type, and implementations should create a new instance + * from the defaultInstance prototype directly. */ + MergeTarget newEmptyTargetForField( + Descriptors.FieldDescriptor descriptor, Message defaultInstance); + + /** Finishes the merge and returns the underlying object. */ Object finish(); } @@ -443,8 +424,7 @@ class MessageReflection { @Override public ExtensionRegistry.ExtensionInfo findExtensionByNumber( ExtensionRegistry registry, Descriptors.Descriptor containingType, int fieldNumber) { - return registry.findImmutableExtensionByNumber(containingType, - fieldNumber); + return registry.findImmutableExtensionByNumber(containingType, fieldNumber); } @Override @@ -522,12 +502,31 @@ class MessageReflection { @Override public MergeTarget newMergeTargetForField( Descriptors.FieldDescriptor field, Message defaultInstance) { + Message.Builder subBuilder; + if (defaultInstance != null) { + subBuilder = defaultInstance.newBuilderForType(); + } else { + subBuilder = builder.newBuilderForField(field); + } + if (!field.isRepeated()) { + Message originalMessage = (Message) getField(field); + if (originalMessage != null) { + subBuilder.mergeFrom(originalMessage); + } + } + return new BuilderAdapter(subBuilder); + } + + @Override + public MergeTarget newEmptyTargetForField( + Descriptors.FieldDescriptor field, Message defaultInstance) { + Message.Builder subBuilder; if (defaultInstance != null) { - return new BuilderAdapter( - defaultInstance.newBuilderForType()); + subBuilder = defaultInstance.newBuilderForType(); } else { - return new BuilderAdapter(builder.newBuilderForField(field)); + subBuilder = builder.newBuilderForField(field); } + return new BuilderAdapter(subBuilder); } @Override @@ -536,8 +535,7 @@ class MessageReflection { return WireFormat.Utf8Validation.STRICT; } // TODO(liujisi): support lazy strings for repeated fields. - if (!descriptor.isRepeated() - && builder instanceof GeneratedMessage.Builder) { + if (!descriptor.isRepeated() && builder instanceof GeneratedMessage.Builder) { return WireFormat.Utf8Validation.LAZY; } return WireFormat.Utf8Validation.LOOSE; @@ -560,8 +558,7 @@ class MessageReflection { @Override public Descriptors.Descriptor getDescriptorForType() { - throw new UnsupportedOperationException( - "getDescriptorForType() called on FieldSet object"); + throw new UnsupportedOperationException("getDescriptorForType() called on FieldSet object"); } @Override @@ -629,8 +626,7 @@ class MessageReflection { @Override public ExtensionRegistry.ExtensionInfo findExtensionByNumber( ExtensionRegistry registry, Descriptors.Descriptor containingType, int fieldNumber) { - return registry.findImmutableExtensionByNumber(containingType, - fieldNumber); + return registry.findImmutableExtensionByNumber(containingType, fieldNumber); } @Override @@ -640,8 +636,7 @@ class MessageReflection { Descriptors.FieldDescriptor field, Message defaultInstance) throws IOException { - Message.Builder subBuilder = - defaultInstance.newBuilderForType(); + Message.Builder subBuilder = defaultInstance.newBuilderForType(); if (!field.isRepeated()) { Message originalMessage = (Message) getField(field); if (originalMessage != null) { @@ -659,8 +654,7 @@ class MessageReflection { Descriptors.FieldDescriptor field, Message defaultInstance) throws IOException { - Message.Builder subBuilder = - defaultInstance.newBuilderForType(); + Message.Builder subBuilder = defaultInstance.newBuilderForType(); if (!field.isRepeated()) { Message originalMessage = (Message) getField(field); if (originalMessage != null) { @@ -678,7 +672,7 @@ class MessageReflection { Descriptors.FieldDescriptor field, Message defaultInstance) throws IOException { - Message.Builder subBuilder = defaultInstance.newBuilderForType(); + Message.Builder subBuilder = defaultInstance.newBuilderForType(); if (!field.isRepeated()) { Message originalMessage = (Message) getField(field); if (originalMessage != null) { @@ -692,8 +686,13 @@ class MessageReflection { @Override public MergeTarget newMergeTargetForField( Descriptors.FieldDescriptor descriptor, Message defaultInstance) { - throw new UnsupportedOperationException( - "newMergeTargetForField() called on FieldSet object"); + throw new UnsupportedOperationException("newMergeTargetForField() called on FieldSet object"); + } + + @Override + public MergeTarget newEmptyTargetForField( + Descriptors.FieldDescriptor descriptor, Message defaultInstance) { + throw new UnsupportedOperationException("newEmptyTargetForField() called on FieldSet object"); } @Override @@ -707,8 +706,7 @@ class MessageReflection { @Override public Object finish() { - throw new UnsupportedOperationException( - "finish() called on FieldSet object"); + throw new UnsupportedOperationException("finish() called on FieldSet object"); } } @@ -731,8 +729,7 @@ class MessageReflection { MergeTarget target, int tag) throws IOException { - if (type.getOptions().getMessageSetWireFormat() && - tag == WireFormat.MESSAGE_SET_ITEM_TAG) { + if (type.getOptions().getMessageSetWireFormat() && tag == WireFormat.MESSAGE_SET_ITEM_TAG) { mergeMessageSetExtensionFromCodedStream( input, unknownFields, extensionRegistry, type, target); return true; @@ -752,19 +749,16 @@ class MessageReflection { // were empty. if (extensionRegistry instanceof ExtensionRegistry) { final ExtensionRegistry.ExtensionInfo extension = - target.findExtensionByNumber((ExtensionRegistry) extensionRegistry, - type, fieldNumber); + target.findExtensionByNumber((ExtensionRegistry) extensionRegistry, type, fieldNumber); if (extension == null) { field = null; } else { field = extension.descriptor; defaultInstance = extension.defaultInstance; - if (defaultInstance == null && - field.getJavaType() - == Descriptors.FieldDescriptor.JavaType.MESSAGE) { + if (defaultInstance == null + && field.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE) { throw new IllegalStateException( - "Message-typed extension lacked default instance: " + - field.getFullName()); + "Message-typed extension lacked default instance: " + field.getFullName()); } } } else { @@ -779,21 +773,19 @@ class MessageReflection { boolean unknown = false; boolean packed = false; if (field == null) { - unknown = true; // Unknown field. - } else if (wireType == FieldSet.getWireFormatForFieldType( - field.getLiteType(), - false /* isPacked */)) { + unknown = true; // Unknown field. + } else if (wireType + == FieldSet.getWireFormatForFieldType(field.getLiteType(), /* isPacked= */ false)) { packed = false; - } else if (field.isPackable() && - wireType == FieldSet.getWireFormatForFieldType( - field.getLiteType(), - true /* isPacked */)) { + } else if (field.isPackable() + && wireType + == FieldSet.getWireFormatForFieldType(field.getLiteType(), /* isPacked= */ true)) { packed = true; } else { - unknown = true; // Unknown wire type. + unknown = true; // Unknown wire type. } - if (unknown) { // Unknown field or wrong wire type. Skip. + if (unknown) { // Unknown field or wrong wire type. Skip. if (unknownFields != null) { return unknownFields.mergeFieldFrom(tag, input); } else { @@ -808,22 +800,26 @@ class MessageReflection { while (input.getBytesUntilLimit() > 0) { final int rawValue = input.readEnum(); if (field.getFile().supportsUnknownEnumValue()) { - target.addRepeatedField(field, - field.getEnumType().findValueByNumberCreatingIfUnknown(rawValue)); + target.addRepeatedField( + field, field.getEnumType().findValueByNumberCreatingIfUnknown(rawValue)); } else { final Object value = field.getEnumType().findValueByNumber(rawValue); + // If the number isn't recognized as a valid value for this enum, + // add it to the unknown fields. if (value == null) { - // If the number isn't recognized as a valid value for this - // enum, drop it (don't even add it to unknownFields). - return true; + if (unknownFields != null) { + unknownFields.mergeVarintField(fieldNumber, rawValue); + } + } else { + target.addRepeatedField(field, value); } - target.addRepeatedField(field, value); } } } else { while (input.getBytesUntilLimit() > 0) { - final Object value = WireFormat.readPrimitiveField( - input, field.getLiteType(), target.getUtf8Validation(field)); + final Object value = + WireFormat.readPrimitiveField( + input, field.getLiteType(), target.getUtf8Validation(field)); target.addRepeatedField(field, value); } } @@ -831,16 +827,16 @@ class MessageReflection { } else { final Object value; switch (field.getType()) { - case GROUP: { - value = target - .parseGroup(input, extensionRegistry, field, defaultInstance); - break; - } - case MESSAGE: { - value = target - .parseMessage(input, extensionRegistry, field, defaultInstance); - break; - } + case GROUP: + { + value = target.parseGroup(input, extensionRegistry, field, defaultInstance); + break; + } + case MESSAGE: + { + value = target.parseMessage(input, extensionRegistry, field, defaultInstance); + break; + } case ENUM: final int rawValue = input.readEnum(); if (field.getFile().supportsUnknownEnumValue()) { @@ -848,7 +844,7 @@ class MessageReflection { } else { value = field.getEnumType().findValueByNumber(rawValue); // If the number isn't recognized as a valid value for this enum, - // drop it. + // add it to the unknown fields. if (value == null) { if (unknownFields != null) { unknownFields.mergeVarintField(fieldNumber, rawValue); @@ -858,8 +854,9 @@ class MessageReflection { } break; default: - value = WireFormat.readPrimitiveField( - input, field.getLiteType(), target.getUtf8Validation(field)); + value = + WireFormat.readPrimitiveField( + input, field.getLiteType(), target.getUtf8Validation(field)); break; } @@ -873,16 +870,14 @@ class MessageReflection { return true; } - /** - * Called by {@code #mergeFieldFrom()} to parse a MessageSet extension into - * MergeTarget. - */ + /** Called by {@code #mergeFieldFrom()} to parse a MessageSet extension into MergeTarget. */ private static void mergeMessageSetExtensionFromCodedStream( CodedInputStream input, UnknownFieldSet.Builder unknownFields, ExtensionRegistryLite extensionRegistry, Descriptors.Descriptor type, - MergeTarget target) throws IOException { + MergeTarget target) + throws IOException { // The wire format for MessageSet is: // message MessageSet { @@ -921,19 +916,17 @@ class MessageReflection { // extensions of it. Otherwise we will treat the registry as if it // were empty. if (extensionRegistry instanceof ExtensionRegistry) { - extension = target.findExtensionByNumber( - (ExtensionRegistry) extensionRegistry, type, typeId); + extension = + target.findExtensionByNumber((ExtensionRegistry) extensionRegistry, type, typeId); } } } else if (tag == WireFormat.MESSAGE_SET_MESSAGE_TAG) { if (typeId != 0) { - if (extension != null && - ExtensionRegistryLite.isEagerlyParseMessageSets()) { + if (extension != null && ExtensionRegistryLite.isEagerlyParseMessageSets()) { // We already know the type, so we can parse directly from the // input with no copying. Hooray! - eagerlyMergeMessageSetExtension( - input, extension, extensionRegistry, target); + eagerlyMergeMessageSetExtension(input, extension, extensionRegistry, target); rawBytes = null; continue; } @@ -952,12 +945,11 @@ class MessageReflection { // Process the raw bytes. if (rawBytes != null && typeId != 0) { // Zero is not a valid type ID. if (extension != null) { // We known the type - mergeMessageSetExtensionFromBytes( - rawBytes, extension, extensionRegistry, target); + mergeMessageSetExtensionFromBytes(rawBytes, extension, extensionRegistry, target); } else { // We don't know how to parse this. Ignore it. if (rawBytes != null && unknownFields != null) { - unknownFields.mergeField(typeId, UnknownFieldSet.Field.newBuilder() - .addLengthDelimited(rawBytes).build()); + unknownFields.mergeField( + typeId, UnknownFieldSet.Field.newBuilder().addLengthDelimited(rawBytes).build()); } } } @@ -967,20 +959,21 @@ class MessageReflection { ByteString rawBytes, ExtensionRegistry.ExtensionInfo extension, ExtensionRegistryLite extensionRegistry, - MergeTarget target) throws IOException { + MergeTarget target) + throws IOException { Descriptors.FieldDescriptor field = extension.descriptor; boolean hasOriginalValue = target.hasField(field); if (hasOriginalValue || ExtensionRegistryLite.isEagerlyParseMessageSets()) { // If the field already exists, we just parse the field. - Object value = target.parseMessageFromBytes( - rawBytes, extensionRegistry,field, extension.defaultInstance); + Object value = + target.parseMessageFromBytes( + rawBytes, extensionRegistry, field, extension.defaultInstance); target.setField(field, value); } else { // Use LazyField to load MessageSet lazily. - LazyField lazyField = new LazyField( - extension.defaultInstance, extensionRegistry, rawBytes); + LazyField lazyField = new LazyField(extension.defaultInstance, extensionRegistry, rawBytes); target.setField(field, lazyField); } } @@ -989,10 +982,10 @@ class MessageReflection { CodedInputStream input, ExtensionRegistry.ExtensionInfo extension, ExtensionRegistryLite extensionRegistry, - MergeTarget target) throws IOException { + MergeTarget target) + throws IOException { Descriptors.FieldDescriptor field = extension.descriptor; - Object value = target.parseMessage(input, extensionRegistry, field, - extension.defaultInstance); + Object value = target.parseMessage(input, extensionRegistry, field, extension.defaultInstance); target.setField(field, value); } } diff --git a/java/core/src/main/java/com/google/protobuf/MessageSchema.java b/java/core/src/main/java/com/google/protobuf/MessageSchema.java new file mode 100755 index 0000000000..0c6cf99963 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MessageSchema.java @@ -0,0 +1,5884 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.ArrayDecoders.decodeBoolList; +import static com.google.protobuf.ArrayDecoders.decodeBytes; +import static com.google.protobuf.ArrayDecoders.decodeBytesList; +import static com.google.protobuf.ArrayDecoders.decodeDouble; +import static com.google.protobuf.ArrayDecoders.decodeDoubleList; +import static com.google.protobuf.ArrayDecoders.decodeExtensionOrUnknownField; +import static com.google.protobuf.ArrayDecoders.decodeFixed32; +import static com.google.protobuf.ArrayDecoders.decodeFixed32List; +import static com.google.protobuf.ArrayDecoders.decodeFixed64; +import static com.google.protobuf.ArrayDecoders.decodeFixed64List; +import static com.google.protobuf.ArrayDecoders.decodeFloat; +import static com.google.protobuf.ArrayDecoders.decodeFloatList; +import static com.google.protobuf.ArrayDecoders.decodeGroupField; +import static com.google.protobuf.ArrayDecoders.decodeGroupList; +import static com.google.protobuf.ArrayDecoders.decodeMessageField; +import static com.google.protobuf.ArrayDecoders.decodeMessageList; +import static com.google.protobuf.ArrayDecoders.decodePackedBoolList; +import static com.google.protobuf.ArrayDecoders.decodePackedDoubleList; +import static com.google.protobuf.ArrayDecoders.decodePackedFixed32List; +import static com.google.protobuf.ArrayDecoders.decodePackedFixed64List; +import static com.google.protobuf.ArrayDecoders.decodePackedFloatList; +import static com.google.protobuf.ArrayDecoders.decodePackedSInt32List; +import static com.google.protobuf.ArrayDecoders.decodePackedSInt64List; +import static com.google.protobuf.ArrayDecoders.decodePackedVarint32List; +import static com.google.protobuf.ArrayDecoders.decodePackedVarint64List; +import static com.google.protobuf.ArrayDecoders.decodeSInt32List; +import static com.google.protobuf.ArrayDecoders.decodeSInt64List; +import static com.google.protobuf.ArrayDecoders.decodeString; +import static com.google.protobuf.ArrayDecoders.decodeStringList; +import static com.google.protobuf.ArrayDecoders.decodeStringListRequireUtf8; +import static com.google.protobuf.ArrayDecoders.decodeStringRequireUtf8; +import static com.google.protobuf.ArrayDecoders.decodeUnknownField; +import static com.google.protobuf.ArrayDecoders.decodeVarint32; +import static com.google.protobuf.ArrayDecoders.decodeVarint32List; +import static com.google.protobuf.ArrayDecoders.decodeVarint64; +import static com.google.protobuf.ArrayDecoders.decodeVarint64List; +import static com.google.protobuf.ArrayDecoders.skipField; + +import com.google.protobuf.ArrayDecoders.Registers; +import com.google.protobuf.ByteString.CodedBuilder; +import com.google.protobuf.FieldSet.FieldDescriptorLite; +import com.google.protobuf.Internal.EnumVerifier; +import com.google.protobuf.Internal.ProtobufList; +import com.google.protobuf.MapEntryLite.Metadata; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** Schema used for standard messages. */ +final class MessageSchema implements Schema { + private static final int INTS_PER_FIELD = 3; + private static final int OFFSET_BITS = 20; + private static final int OFFSET_MASK = 0XFFFFF; + private static final int FIELD_TYPE_MASK = 0x0FF00000; + private static final int REQUIRED_MASK = 0x10000000; + private static final int ENFORCE_UTF8_MASK = 0x20000000; + private static final int[] EMPTY_INT_ARRAY = new int[0]; + + /** An offset applied to the field type ID for scalar fields that are a member of a oneof. */ + static final int ONEOF_TYPE_OFFSET = 51 /* FieldType.MAP + 1 */; + + /** + * Keep a direct reference to the unsafe object so we don't need to go through the UnsafeUtil + * wrapper for every call. + */ + private static final sun.misc.Unsafe UNSAFE = UnsafeUtil.getUnsafe(); + + /** + * Holds all information for accessing the message fields. The layout is as follows (field + * positions are relative to the offset of the start of the field in the buffer): + * + *

+ * + *

+   * [ 0 -    3] unused
+   * [ 4 -   31] field number
+   * [32 -   33] unused
+   * [34 -   34] whether UTF-8 enforcement should be applied to a string field.
+   * [35 -   35] whether the field is required
+   * [36 -   43] field type / for oneof: field type + {@link #ONEOF_TYPE_OFFSET}
+   * [44 -   63] field offset / oneof value field offset
+   * [64 -   69] unused
+   * [70 -   75] field presence mask shift (unused for oneof/repeated fields)
+   * [76 -   95] presence field offset / oneof case field offset / cached size field offset
+   * 
+ */ + private final int[] buffer; + + /** + * Holds object references for fields. For each field entry in {@code buffer}, there are two + * corresponding entries in this array. The content is different from different field types: + * + *
+   *   Map fields:
+   *     objects[pos] = map default entry instance
+   *     objects[pos + 1] = EnumVerifier if map value is enum, or message class reference if map
+   *                        value is message.
+   *   Message fields:
+   *     objects[pos] = null or cached message schema
+   *     objects[pos + 1] = message class reference
+   *   Enum fields:
+   *     objects[pos] = null
+   *     objects[pos + 1] = EnumVerifier
+   * 
+ */ + private final Object[] objects; + + private final int minFieldNumber; + private final int maxFieldNumber; + + private final MessageLite defaultInstance; + private final boolean hasExtensions; + private final boolean lite; + private final boolean proto3; + // TODO(xiaofeng): Make both full-runtime and lite-runtime support cached field size. + private final boolean useCachedSizeField; + + /** Represents [checkInitialized positions, map field positions, repeated field offsets]. */ + private final int[] intArray; + + /** + * Values at indices 0 -> checkInitializedCount in intArray are positions to check for + * initialization. + */ + private final int checkInitializedCount; + + /** + * Values at indices checkInitializedCount -> repeatedFieldOffsetStart are map positions. + * Everything after that are repeated field offsets. + */ + private final int repeatedFieldOffsetStart; + + private final NewInstanceSchema newInstanceSchema; + private final ListFieldSchema listFieldSchema; + private final UnknownFieldSchema unknownFieldSchema; + private final ExtensionSchema extensionSchema; + private final MapFieldSchema mapFieldSchema; + + private MessageSchema( + int[] buffer, + Object[] objects, + int minFieldNumber, + int maxFieldNumber, + MessageLite defaultInstance, + boolean proto3, + boolean useCachedSizeField, + int[] intArray, + int checkInitialized, + int mapFieldPositions, + NewInstanceSchema newInstanceSchema, + ListFieldSchema listFieldSchema, + UnknownFieldSchema unknownFieldSchema, + ExtensionSchema extensionSchema, + MapFieldSchema mapFieldSchema) { + this.buffer = buffer; + this.objects = objects; + this.minFieldNumber = minFieldNumber; + this.maxFieldNumber = maxFieldNumber; + + this.lite = defaultInstance instanceof GeneratedMessageLite; + this.proto3 = proto3; + this.hasExtensions = extensionSchema != null && extensionSchema.hasExtensions(defaultInstance); + this.useCachedSizeField = useCachedSizeField; + + this.intArray = intArray; + this.checkInitializedCount = checkInitialized; + this.repeatedFieldOffsetStart = mapFieldPositions; + + this.newInstanceSchema = newInstanceSchema; + this.listFieldSchema = listFieldSchema; + this.unknownFieldSchema = unknownFieldSchema; + this.extensionSchema = extensionSchema; + this.defaultInstance = defaultInstance; + this.mapFieldSchema = mapFieldSchema; + } + + static MessageSchema newSchema( + Class messageClass, + MessageInfo messageInfo, + NewInstanceSchema newInstanceSchema, + ListFieldSchema listFieldSchema, + UnknownFieldSchema unknownFieldSchema, + ExtensionSchema extensionSchema, + MapFieldSchema mapFieldSchema) { + if (messageInfo instanceof RawMessageInfo) { + return newSchemaForRawMessageInfo( + (RawMessageInfo) messageInfo, + newInstanceSchema, + listFieldSchema, + unknownFieldSchema, + extensionSchema, + mapFieldSchema); + + } else { + return newSchemaForMessageInfo( + (StructuralMessageInfo) messageInfo, + newInstanceSchema, + listFieldSchema, + unknownFieldSchema, + extensionSchema, + mapFieldSchema); + } + } + + static MessageSchema newSchemaForRawMessageInfo( + RawMessageInfo messageInfo, + NewInstanceSchema newInstanceSchema, + ListFieldSchema listFieldSchema, + UnknownFieldSchema unknownFieldSchema, + ExtensionSchema extensionSchema, + MapFieldSchema mapFieldSchema) { + final boolean isProto3 = messageInfo.getSyntax() == ProtoSyntax.PROTO3; + + String info = messageInfo.getStringInfo(); + final int length = info.length(); + int i = 0; + + int next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + final int flags = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + final int fieldCount = next; + + final int oneofCount; + final int hasBitsCount; + final int minFieldNumber; + final int maxFieldNumber; + final int numEntries; + final int mapFieldCount; + final int repeatedFieldCount; + final int checkInitialized; + final int[] intArray; + int objectsPosition; + if (fieldCount == 0) { + oneofCount = 0; + hasBitsCount = 0; + minFieldNumber = 0; + maxFieldNumber = 0; + numEntries = 0; + mapFieldCount = 0; + repeatedFieldCount = 0; + checkInitialized = 0; + intArray = EMPTY_INT_ARRAY; + objectsPosition = 0; + } else { + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + oneofCount = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + hasBitsCount = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + minFieldNumber = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + maxFieldNumber = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + numEntries = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + mapFieldCount = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + repeatedFieldCount = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + checkInitialized = next; + intArray = new int[checkInitialized + mapFieldCount + repeatedFieldCount]; + // Field objects are after a list of (oneof, oneofCase) pairs + a list of hasbits fields. + objectsPosition = oneofCount * 2 + hasBitsCount; + } + + final sun.misc.Unsafe unsafe = UNSAFE; + final Object[] messageInfoObjects = messageInfo.getObjects(); + int checkInitializedPosition = 0; + final Class messageClass = messageInfo.getDefaultInstance().getClass(); + int[] buffer = new int[numEntries * INTS_PER_FIELD]; + Object[] objects = new Object[numEntries * 2]; + + int mapFieldIndex = checkInitialized; + int repeatedFieldIndex = checkInitialized + mapFieldCount; + + int bufferIndex = 0; + while (i < length) { + final int fieldNumber; + final int fieldTypeWithExtraBits; + final int fieldType; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + fieldNumber = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + fieldTypeWithExtraBits = next; + fieldType = fieldTypeWithExtraBits & 0xFF; + + if ((fieldTypeWithExtraBits & 0x400) != 0) { + intArray[checkInitializedPosition++] = bufferIndex; + } + + final int fieldOffset; + final int presenceMaskShift; + final int presenceFieldOffset; + + // Oneof + if (fieldType >= ONEOF_TYPE_OFFSET) { + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + int oneofIndex = next; + + final int oneofFieldType = fieldType - ONEOF_TYPE_OFFSET; + if (oneofFieldType == 9 /* FieldType.MESSAGE */ + || oneofFieldType == 17 /* FieldType.GROUP */) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } else if (oneofFieldType == 12 /* FieldType.ENUM */) { + // proto2 + if ((flags & 0x1) == 0x1) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } + } + + final java.lang.reflect.Field oneofField; + int index = oneofIndex * 2; + Object o = messageInfoObjects[index]; + if (o instanceof java.lang.reflect.Field) { + oneofField = (java.lang.reflect.Field) o; + } else { + oneofField = reflectField(messageClass, (String) o); + // Memoize java.lang.reflect.Field instances for oneof/hasbits fields, since they're + // potentially used for many Protobuf fields. Since there's a 1-1 mapping from the + // Protobuf field to the Java Field for non-oneofs, there's no benefit for memoizing + // those. + messageInfoObjects[index] = oneofField; + } + + fieldOffset = (int) unsafe.objectFieldOffset(oneofField); + + final java.lang.reflect.Field oneofCaseField; + index++; + o = messageInfoObjects[index]; + if (o instanceof java.lang.reflect.Field) { + oneofCaseField = (java.lang.reflect.Field) o; + } else { + oneofCaseField = reflectField(messageClass, (String) o); + messageInfoObjects[index] = oneofCaseField; + } + + presenceFieldOffset = (int) unsafe.objectFieldOffset(oneofCaseField); + presenceMaskShift = 0; + } else { + Field field = reflectField(messageClass, (String) messageInfoObjects[objectsPosition++]); + if (fieldType == 9 /* FieldType.MESSAGE */ || fieldType == 17 /* FieldType.GROUP */) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = field.getType(); + } else if (fieldType == 27 /* FieldType.MESSAGE_LIST */ + || fieldType == 49 /* FieldType.GROUP_LIST */) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } else if (fieldType == 12 /* FieldType.ENUM */ + || fieldType == 30 /* FieldType.ENUM_LIST */ + || fieldType == 44 /* FieldType.ENUM_LIST_PACKED */) { + if ((flags & 0x1) == 0x1) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } + } else if (fieldType == 50 /* FieldType.MAP */) { + intArray[mapFieldIndex++] = bufferIndex; + objects[bufferIndex / INTS_PER_FIELD * 2] = messageInfoObjects[objectsPosition++]; + if ((fieldTypeWithExtraBits & 0x800) != 0) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } + } + + fieldOffset = (int) unsafe.objectFieldOffset(field); + if ((flags & 0x1) == 0x1 && fieldType <= 17 /* FieldType.GROUP */) { + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + int hasBitsIndex = next; + + final java.lang.reflect.Field hasBitsField; + int index = oneofCount * 2 + hasBitsIndex / 32; + Object o = messageInfoObjects[index]; + if (o instanceof java.lang.reflect.Field) { + hasBitsField = (java.lang.reflect.Field) o; + } else { + hasBitsField = reflectField(messageClass, (String) o); + messageInfoObjects[index] = hasBitsField; + } + + presenceFieldOffset = (int) unsafe.objectFieldOffset(hasBitsField); + presenceMaskShift = hasBitsIndex % 32; + } else { + presenceFieldOffset = 0; + presenceMaskShift = 0; + } + + if (fieldType >= 18 && fieldType <= 49) { + // Field types of repeated fields are in a consecutive range from 18 (DOUBLE_LIST) to + // 49 (GROUP_LIST). + intArray[repeatedFieldIndex++] = fieldOffset; + } + } + + buffer[bufferIndex++] = fieldNumber; + buffer[bufferIndex++] = + ((fieldTypeWithExtraBits & 0x200) != 0 ? ENFORCE_UTF8_MASK : 0) + | ((fieldTypeWithExtraBits & 0x100) != 0 ? REQUIRED_MASK : 0) + | (fieldType << OFFSET_BITS) + | fieldOffset; + buffer[bufferIndex++] = (presenceMaskShift << OFFSET_BITS) | presenceFieldOffset; + } + + return new MessageSchema( + buffer, + objects, + minFieldNumber, + maxFieldNumber, + messageInfo.getDefaultInstance(), + isProto3, + /* useCachedSizeField= */ false, + intArray, + checkInitialized, + checkInitialized + mapFieldCount, + newInstanceSchema, + listFieldSchema, + unknownFieldSchema, + extensionSchema, + mapFieldSchema); + } + + private static java.lang.reflect.Field reflectField(Class messageClass, String fieldName) { + try { + return messageClass.getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + // Some Samsung devices lie about what fields are present via the getDeclaredField API so + // we do the for loop properly that they seem to have messed up... + java.lang.reflect.Field[] fields = messageClass.getDeclaredFields(); + for (java.lang.reflect.Field field : fields) { + if (fieldName.equals(field.getName())) { + return field; + } + } + + // If we make it here, the runtime still lies about what we know to be true at compile + // time. We throw to alert server monitoring for further remediation. + throw new RuntimeException( + "Field " + + fieldName + + " for " + + messageClass.getName() + + " not found. Known fields are " + + Arrays.toString(fields)); + } + } + + static MessageSchema newSchemaForMessageInfo( + StructuralMessageInfo messageInfo, + NewInstanceSchema newInstanceSchema, + ListFieldSchema listFieldSchema, + UnknownFieldSchema unknownFieldSchema, + ExtensionSchema extensionSchema, + MapFieldSchema mapFieldSchema) { + final boolean isProto3 = messageInfo.getSyntax() == ProtoSyntax.PROTO3; + FieldInfo[] fis = messageInfo.getFields(); + final int minFieldNumber; + final int maxFieldNumber; + if (fis.length == 0) { + minFieldNumber = 0; + maxFieldNumber = 0; + } else { + minFieldNumber = fis[0].getFieldNumber(); + maxFieldNumber = fis[fis.length - 1].getFieldNumber(); + } + + final int numEntries = fis.length; + + int[] buffer = new int[numEntries * INTS_PER_FIELD]; + Object[] objects = new Object[numEntries * 2]; + + int mapFieldCount = 0; + int repeatedFieldCount = 0; + for (FieldInfo fi : fis) { + if (fi.getType() == FieldType.MAP) { + mapFieldCount++; + } else if (fi.getType().id() >= 18 && fi.getType().id() <= 49) { + // Field types of repeated fields are in a consecutive range from 18 (DOUBLE_LIST) to + // 49 (GROUP_LIST). + repeatedFieldCount++; + } + } + int[] mapFieldPositions = mapFieldCount > 0 ? new int[mapFieldCount] : null; + int[] repeatedFieldOffsets = repeatedFieldCount > 0 ? new int[repeatedFieldCount] : null; + mapFieldCount = 0; + repeatedFieldCount = 0; + + int[] checkInitialized = messageInfo.getCheckInitialized(); + if (checkInitialized == null) { + checkInitialized = EMPTY_INT_ARRAY; + } + int checkInitializedIndex = 0; + // Fill in the manifest data from the descriptors. + int fieldIndex = 0; + for (int bufferIndex = 0; fieldIndex < fis.length; bufferIndex += INTS_PER_FIELD) { + final FieldInfo fi = fis[fieldIndex]; + final int fieldNumber = fi.getFieldNumber(); + + // We found the entry for the next field. Store the entry in the manifest for + // this field and increment the field index. + storeFieldData(fi, buffer, bufferIndex, isProto3, objects); + + // Convert field number to index + if (checkInitializedIndex < checkInitialized.length + && checkInitialized[checkInitializedIndex] == fieldNumber) { + checkInitialized[checkInitializedIndex++] = bufferIndex; + } + + if (fi.getType() == FieldType.MAP) { + mapFieldPositions[mapFieldCount++] = bufferIndex; + } else if (fi.getType().id() >= 18 && fi.getType().id() <= 49) { + // Field types of repeated fields are in a consecutive range from 18 (DOUBLE_LIST) to + // 49 (GROUP_LIST). + repeatedFieldOffsets[repeatedFieldCount++] = + (int) UnsafeUtil.objectFieldOffset(fi.getField()); + } + + fieldIndex++; + } + + if (mapFieldPositions == null) { + mapFieldPositions = EMPTY_INT_ARRAY; + } + if (repeatedFieldOffsets == null) { + repeatedFieldOffsets = EMPTY_INT_ARRAY; + } + int[] combined = + new int[checkInitialized.length + mapFieldPositions.length + repeatedFieldOffsets.length]; + System.arraycopy(checkInitialized, 0, combined, 0, checkInitialized.length); + System.arraycopy( + mapFieldPositions, 0, combined, checkInitialized.length, mapFieldPositions.length); + System.arraycopy( + repeatedFieldOffsets, + 0, + combined, + checkInitialized.length + mapFieldPositions.length, + repeatedFieldOffsets.length); + + return new MessageSchema( + buffer, + objects, + minFieldNumber, + maxFieldNumber, + messageInfo.getDefaultInstance(), + isProto3, + /* useCachedSizeField= */ true, + combined, + checkInitialized.length, + checkInitialized.length + mapFieldPositions.length, + newInstanceSchema, + listFieldSchema, + unknownFieldSchema, + extensionSchema, + mapFieldSchema); + } + + private static void storeFieldData( + FieldInfo fi, int[] buffer, int bufferIndex, boolean proto3, Object[] objects) { + final int fieldOffset; + final int typeId; + final int presenceMaskShift; + final int presenceFieldOffset; + + OneofInfo oneof = fi.getOneof(); + if (oneof != null) { + typeId = fi.getType().id() + ONEOF_TYPE_OFFSET; + fieldOffset = (int) UnsafeUtil.objectFieldOffset(oneof.getValueField()); + presenceFieldOffset = (int) UnsafeUtil.objectFieldOffset(oneof.getCaseField()); + presenceMaskShift = 0; + } else { + FieldType type = fi.getType(); + fieldOffset = (int) UnsafeUtil.objectFieldOffset(fi.getField()); + typeId = type.id(); + if (!proto3 && !type.isList() && !type.isMap()) { + presenceFieldOffset = (int) UnsafeUtil.objectFieldOffset(fi.getPresenceField()); + presenceMaskShift = Integer.numberOfTrailingZeros(fi.getPresenceMask()); + } else { + if (fi.getCachedSizeField() == null) { + presenceFieldOffset = 0; + presenceMaskShift = 0; + } else { + presenceFieldOffset = (int) UnsafeUtil.objectFieldOffset(fi.getCachedSizeField()); + presenceMaskShift = 0; + } + } + } + + buffer[bufferIndex] = fi.getFieldNumber(); + buffer[bufferIndex + 1] = + (fi.isEnforceUtf8() ? ENFORCE_UTF8_MASK : 0) + | (fi.isRequired() ? REQUIRED_MASK : 0) + | (typeId << OFFSET_BITS) + | fieldOffset; + buffer[bufferIndex + 2] = (presenceMaskShift << OFFSET_BITS) | presenceFieldOffset; + + Object messageFieldClass = fi.getMessageFieldClass(); + if (fi.getMapDefaultEntry() != null) { + objects[bufferIndex / INTS_PER_FIELD * 2] = fi.getMapDefaultEntry(); + if (messageFieldClass != null) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageFieldClass; + } else if (fi.getEnumVerifier() != null) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = fi.getEnumVerifier(); + } + } else { + if (messageFieldClass != null) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageFieldClass; + } else if (fi.getEnumVerifier() != null) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = fi.getEnumVerifier(); + } + } + } + + @SuppressWarnings("unchecked") + @Override + public T newInstance() { + return (T) newInstanceSchema.newInstance(defaultInstance); + } + + @Override + public boolean equals(T message, T other) { + final int bufferLength = buffer.length; + for (int pos = 0; pos < bufferLength; pos += INTS_PER_FIELD) { + if (!equals(message, other, pos)) { + return false; + } + } + + Object messageUnknown = unknownFieldSchema.getFromMessage(message); + Object otherUnknown = unknownFieldSchema.getFromMessage(other); + if (!messageUnknown.equals(otherUnknown)) { + return false; + } + + if (hasExtensions) { + FieldSet messageExtensions = extensionSchema.getExtensions(message); + FieldSet otherExtensions = extensionSchema.getExtensions(other); + return messageExtensions.equals(otherExtensions); + } + return true; + } + + private boolean equals(T message, T other, int pos) { + final int typeAndOffset = typeAndOffsetAt(pos); + final long offset = offset(typeAndOffset); + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + return arePresentForEquals(message, other, pos) + && Double.doubleToLongBits(UnsafeUtil.getDouble(message, offset)) + == Double.doubleToLongBits(UnsafeUtil.getDouble(other, offset)); + case 1: // FLOAT: + return arePresentForEquals(message, other, pos) + && Float.floatToIntBits(UnsafeUtil.getFloat(message, offset)) + == Float.floatToIntBits(UnsafeUtil.getFloat(other, offset)); + case 2: // INT64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 3: // UINT64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 4: // INT32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 5: // FIXED64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 6: // FIXED32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 7: // BOOL: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getBoolean(message, offset) == UnsafeUtil.getBoolean(other, offset); + case 8: // STRING: + return arePresentForEquals(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 9: // MESSAGE: + return arePresentForEquals(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 10: // BYTES: + return arePresentForEquals(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 11: // UINT32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 12: // ENUM: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 13: // SFIXED32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 14: // SFIXED64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 15: // SINT32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 16: // SINT64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 17: // GROUP: + return arePresentForEquals(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + + case 18: // DOUBLE_LIST: + case 19: // FLOAT_LIST: + case 20: // INT64_LIST: + case 21: // UINT64_LIST: + case 22: // INT32_LIST: + case 23: // FIXED64_LIST: + case 24: // FIXED32_LIST: + case 25: // BOOL_LIST: + case 26: // STRING_LIST: + case 27: // MESSAGE_LIST: + case 28: // BYTES_LIST: + case 29: // UINT32_LIST: + case 30: // ENUM_LIST: + case 31: // SFIXED32_LIST: + case 32: // SFIXED64_LIST: + case 33: // SINT32_LIST: + case 34: // SINT64_LIST: + case 35: // DOUBLE_LIST_PACKED: + case 36: // FLOAT_LIST_PACKED: + case 37: // INT64_LIST_PACKED: + case 38: // UINT64_LIST_PACKED: + case 39: // INT32_LIST_PACKED: + case 40: // FIXED64_LIST_PACKED: + case 41: // FIXED32_LIST_PACKED: + case 42: // BOOL_LIST_PACKED: + case 43: // UINT32_LIST_PACKED: + case 44: // ENUM_LIST_PACKED: + case 45: // SFIXED32_LIST_PACKED: + case 46: // SFIXED64_LIST_PACKED: + case 47: // SINT32_LIST_PACKED: + case 48: // SINT64_LIST_PACKED: + case 49: // GROUP_LIST: + return SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 50: // MAP: + return SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 51: // ONEOF_DOUBLE: + case 52: // ONEOF_FLOAT: + case 53: // ONEOF_INT64: + case 54: // ONEOF_UINT64: + case 55: // ONEOF_INT32: + case 56: // ONEOF_FIXED64: + case 57: // ONEOF_FIXED32: + case 58: // ONEOF_BOOL: + case 59: // ONEOF_STRING: + case 60: // ONEOF_MESSAGE: + case 61: // ONEOF_BYTES: + case 62: // ONEOF_UINT32: + case 63: // ONEOF_ENUM: + case 64: // ONEOF_SFIXED32: + case 65: // ONEOF_SFIXED64: + case 66: // ONEOF_SINT32: + case 67: // ONEOF_SINT64: + case 68: // ONEOF_GROUP: + return isOneofCaseEqual(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + default: + // Assume it's an empty entry - just go to the next entry. + return true; + } + } + + @Override + public int hashCode(T message) { + int hashCode = 0; + final int bufferLength = buffer.length; + for (int pos = 0; pos < bufferLength; pos += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(pos); + final int entryNumber = numberAt(pos); + + final long offset = offset(typeAndOffset); + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + hashCode = + (hashCode * 53) + + Internal.hashLong( + Double.doubleToLongBits(UnsafeUtil.getDouble(message, offset))); + break; + case 1: // FLOAT: + hashCode = (hashCode * 53) + Float.floatToIntBits(UnsafeUtil.getFloat(message, offset)); + break; + case 2: // INT64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + case 3: // UINT64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + case 4: // INT32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 5: // FIXED64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + case 6: // FIXED32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 7: // BOOL: + hashCode = (hashCode * 53) + Internal.hashBoolean(UnsafeUtil.getBoolean(message, offset)); + break; + case 8: // STRING: + hashCode = (hashCode * 53) + ((String) UnsafeUtil.getObject(message, offset)).hashCode(); + break; + case 9: // MESSAGE: + { + int protoHash = 37; + Object submessage = UnsafeUtil.getObject(message, offset); + if (submessage != null) { + protoHash = submessage.hashCode(); + } + hashCode = (53 * hashCode) + protoHash; + break; + } + case 10: // BYTES: + hashCode = (hashCode * 53) + UnsafeUtil.getObject(message, offset).hashCode(); + break; + case 11: // UINT32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 12: // ENUM: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 13: // SFIXED32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 14: // SFIXED64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + case 15: // SINT32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 16: // SINT64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + + case 17: // GROUP: + { + int protoHash = 37; + Object submessage = UnsafeUtil.getObject(message, offset); + if (submessage != null) { + protoHash = submessage.hashCode(); + } + hashCode = (53 * hashCode) + protoHash; + break; + } + case 18: // DOUBLE_LIST: + case 19: // FLOAT_LIST: + case 20: // INT64_LIST: + case 21: // UINT64_LIST: + case 22: // INT32_LIST: + case 23: // FIXED64_LIST: + case 24: // FIXED32_LIST: + case 25: // BOOL_LIST: + case 26: // STRING_LIST: + case 27: // MESSAGE_LIST: + case 28: // BYTES_LIST: + case 29: // UINT32_LIST: + case 30: // ENUM_LIST: + case 31: // SFIXED32_LIST: + case 32: // SFIXED64_LIST: + case 33: // SINT32_LIST: + case 34: // SINT64_LIST: + case 35: // DOUBLE_LIST_PACKED: + case 36: // FLOAT_LIST_PACKED: + case 37: // INT64_LIST_PACKED: + case 38: // UINT64_LIST_PACKED: + case 39: // INT32_LIST_PACKED: + case 40: // FIXED64_LIST_PACKED: + case 41: // FIXED32_LIST_PACKED: + case 42: // BOOL_LIST_PACKED: + case 43: // UINT32_LIST_PACKED: + case 44: // ENUM_LIST_PACKED: + case 45: // SFIXED32_LIST_PACKED: + case 46: // SFIXED64_LIST_PACKED: + case 47: // SINT32_LIST_PACKED: + case 48: // SINT64_LIST_PACKED: + case 49: // GROUP_LIST: + hashCode = (hashCode * 53) + UnsafeUtil.getObject(message, offset).hashCode(); + break; + case 50: // MAP: + hashCode = (hashCode * 53) + UnsafeUtil.getObject(message, offset).hashCode(); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = + (hashCode * 53) + + Internal.hashLong(Double.doubleToLongBits(oneofDoubleAt(message, offset))); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Float.floatToIntBits(oneofFloatAt(message, offset)); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashBoolean(oneofBooleanAt(message, offset)); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = + (hashCode * 53) + ((String) UnsafeUtil.getObject(message, offset)).hashCode(); + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, entryNumber, pos)) { + Object submessage = UnsafeUtil.getObject(message, offset); + hashCode = (53 * hashCode) + submessage.hashCode(); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + UnsafeUtil.getObject(message, offset).hashCode(); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, entryNumber, pos)) { + Object submessage = UnsafeUtil.getObject(message, offset); + hashCode = (53 * hashCode) + submessage.hashCode(); + } + break; + default: + // Assume it's an empty entry - just go to the next entry. + break; + } + } + + hashCode = (hashCode * 53) + unknownFieldSchema.getFromMessage(message).hashCode(); + + if (hasExtensions) { + hashCode = (hashCode * 53) + extensionSchema.getExtensions(message).hashCode(); + } + + return hashCode; + } + + @Override + public void mergeFrom(T message, T other) { + if (other == null) { + throw new NullPointerException(); + } + for (int i = 0; i < buffer.length; i += INTS_PER_FIELD) { + // A separate method allows for better JIT optimizations + mergeSingleField(message, other, i); + } + + if (!proto3) { + SchemaUtil.mergeUnknownFields(unknownFieldSchema, message, other); + + if (hasExtensions) { + SchemaUtil.mergeExtensions(extensionSchema, message, other); + } + } + } + + private void mergeSingleField(T message, T other, int pos) { + final int typeAndOffset = typeAndOffsetAt(pos); + final long offset = offset(typeAndOffset); + final int number = numberAt(pos); + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putDouble(message, offset, UnsafeUtil.getDouble(other, offset)); + setFieldPresent(message, pos); + } + break; + case 1: // FLOAT: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putFloat(message, offset, UnsafeUtil.getFloat(other, offset)); + setFieldPresent(message, pos); + } + break; + case 2: // INT64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 3: // UINT64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 4: // INT32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 5: // FIXED64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 6: // FIXED32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 7: // BOOL: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putBoolean(message, offset, UnsafeUtil.getBoolean(other, offset)); + setFieldPresent(message, pos); + } + break; + case 8: // STRING: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putObject(message, offset, UnsafeUtil.getObject(other, offset)); + setFieldPresent(message, pos); + } + break; + case 9: // MESSAGE: + mergeMessage(message, other, pos); + break; + case 10: // BYTES: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putObject(message, offset, UnsafeUtil.getObject(other, offset)); + setFieldPresent(message, pos); + } + break; + case 11: // UINT32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 12: // ENUM: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 13: // SFIXED32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 14: // SFIXED64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 15: // SINT32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 16: // SINT64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 17: // GROUP: + mergeMessage(message, other, pos); + break; + case 18: // DOUBLE_LIST: + case 19: // FLOAT_LIST: + case 20: // INT64_LIST: + case 21: // UINT64_LIST: + case 22: // INT32_LIST: + case 23: // FIXED64_LIST: + case 24: // FIXED32_LIST: + case 25: // BOOL_LIST: + case 26: // STRING_LIST: + case 27: // MESSAGE_LIST: + case 28: // BYTES_LIST: + case 29: // UINT32_LIST: + case 30: // ENUM_LIST: + case 31: // SFIXED32_LIST: + case 32: // SFIXED64_LIST: + case 33: // SINT32_LIST: + case 34: // SINT64_LIST: + case 35: // DOUBLE_LIST_PACKED: + case 36: // FLOAT_LIST_PACKED: + case 37: // INT64_LIST_PACKED: + case 38: // UINT64_LIST_PACKED: + case 39: // INT32_LIST_PACKED: + case 40: // FIXED64_LIST_PACKED: + case 41: // FIXED32_LIST_PACKED: + case 42: // BOOL_LIST_PACKED: + case 43: // UINT32_LIST_PACKED: + case 44: // ENUM_LIST_PACKED: + case 45: // SFIXED32_LIST_PACKED: + case 46: // SFIXED64_LIST_PACKED: + case 47: // SINT32_LIST_PACKED: + case 48: // SINT64_LIST_PACKED: + case 49: // GROUP_LIST: + listFieldSchema.mergeListsAt(message, other, offset); + break; + case 50: // MAP: + SchemaUtil.mergeMap(mapFieldSchema, message, other, offset); + break; + case 51: // ONEOF_DOUBLE: + case 52: // ONEOF_FLOAT: + case 53: // ONEOF_INT64: + case 54: // ONEOF_UINT64: + case 55: // ONEOF_INT32: + case 56: // ONEOF_FIXED64: + case 57: // ONEOF_FIXED32: + case 58: // ONEOF_BOOL: + case 59: // ONEOF_STRING: + if (isOneofPresent(other, number, pos)) { + UnsafeUtil.putObject(message, offset, UnsafeUtil.getObject(other, offset)); + setOneofPresent(message, number, pos); + } + break; + + case 60: // ONEOF_MESSAGE: + mergeOneofMessage(message, other, pos); + break; + case 61: // ONEOF_BYTES: + case 62: // ONEOF_UINT32: + case 63: // ONEOF_ENUM: + case 64: // ONEOF_SFIXED32: + case 65: // ONEOF_SFIXED64: + case 66: // ONEOF_SINT32: + case 67: // ONEOF_SINT64: + if (isOneofPresent(other, number, pos)) { + UnsafeUtil.putObject(message, offset, UnsafeUtil.getObject(other, offset)); + setOneofPresent(message, number, pos); + } + break; + case 68: // ONEOF_GROUP: + mergeOneofMessage(message, other, pos); + break; + default: + break; + } + } + + private void mergeMessage(T message, T other, int pos) { + final int typeAndOffset = typeAndOffsetAt(pos); + final long offset = offset(typeAndOffset); + + if (!isFieldPresent(other, pos)) { + return; + } + + Object mine = UnsafeUtil.getObject(message, offset); + Object theirs = UnsafeUtil.getObject(other, offset); + if (mine != null && theirs != null) { + Object merged = Internal.mergeMessage(mine, theirs); + UnsafeUtil.putObject(message, offset, merged); + setFieldPresent(message, pos); + } else if (theirs != null) { + UnsafeUtil.putObject(message, offset, theirs); + setFieldPresent(message, pos); + } + } + + private void mergeOneofMessage(T message, T other, int pos) { + int typeAndOffset = typeAndOffsetAt(pos); + int number = numberAt(pos); + long offset = offset(typeAndOffset); + + if (!isOneofPresent(other, number, pos)) { + return; + } + + Object mine = UnsafeUtil.getObject(message, offset); + Object theirs = UnsafeUtil.getObject(other, offset); + if (mine != null && theirs != null) { + Object merged = Internal.mergeMessage(mine, theirs); + UnsafeUtil.putObject(message, offset, merged); + setOneofPresent(message, number, pos); + } else if (theirs != null) { + UnsafeUtil.putObject(message, offset, theirs); + setOneofPresent(message, number, pos); + } + } + + @Override + public int getSerializedSize(T message) { + return proto3 ? getSerializedSizeProto3(message) : getSerializedSizeProto2(message); + } + + @SuppressWarnings("unchecked") + private int getSerializedSizeProto2(T message) { + int size = 0; + + final sun.misc.Unsafe unsafe = UNSAFE; + int currentPresenceFieldOffset = -1; + int currentPresenceField = 0; + for (int i = 0; i < buffer.length; i += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(i); + final int number = numberAt(i); + + int fieldType = type(typeAndOffset); + int presenceMaskAndOffset = 0; + int presenceMask = 0; + if (fieldType <= 17) { + presenceMaskAndOffset = buffer[i + 2]; + final int presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + if (presenceFieldOffset != currentPresenceFieldOffset) { + currentPresenceFieldOffset = presenceFieldOffset; + currentPresenceField = unsafe.getInt(message, (long) presenceFieldOffset); + } + } else if (useCachedSizeField + && fieldType >= FieldType.DOUBLE_LIST_PACKED.id() + && fieldType <= FieldType.SINT64_LIST_PACKED.id()) { + presenceMaskAndOffset = buffer[i + 2] & OFFSET_MASK; + } + + final long offset = offset(typeAndOffset); + + switch (fieldType) { + case 0: // DOUBLE: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeDoubleSize(number, 0); + } + break; + case 1: // FLOAT: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeFloatSize(number, 0); + } + break; + case 2: // INT64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeInt64Size(number, unsafe.getLong(message, offset)); + } + break; + case 3: // UINT64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeUInt64Size(number, unsafe.getLong(message, offset)); + } + break; + case 4: // INT32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeInt32Size(number, unsafe.getInt(message, offset)); + } + break; + case 5: // FIXED64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeFixed64Size(number, 0); + } + break; + case 6: // FIXED32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeFixed32Size(number, 0); + } + break; + case 7: // BOOL: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeBoolSize(number, true); + } + break; + case 8: // STRING: + if ((currentPresenceField & presenceMask) != 0) { + Object value = unsafe.getObject(message, offset); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSize(number, (ByteString) value); + } else { + size += CodedOutputStream.computeStringSize(number, (String) value); + } + } + break; + case 9: // MESSAGE: + if ((currentPresenceField & presenceMask) != 0) { + Object value = unsafe.getObject(message, offset); + size += SchemaUtil.computeSizeMessage(number, value, getMessageFieldSchema(i)); + } + break; + case 10: // BYTES: + if ((currentPresenceField & presenceMask) != 0) { + ByteString value = (ByteString) unsafe.getObject(message, offset); + size += CodedOutputStream.computeBytesSize(number, value); + } + break; + case 11: // UINT32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeUInt32Size(number, unsafe.getInt(message, offset)); + } + break; + case 12: // ENUM: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeEnumSize(number, unsafe.getInt(message, offset)); + } + break; + case 13: // SFIXED32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeSFixed32Size(number, 0); + } + break; + case 14: // SFIXED64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeSFixed64Size(number, 0); + } + break; + case 15: // SINT32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeSInt32Size(number, unsafe.getInt(message, offset)); + } + break; + case 16: // SINT64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeSInt64Size(number, unsafe.getLong(message, offset)); + } + break; + case 17: // GROUP: + if ((currentPresenceField & presenceMask) != 0) { + size += + CodedOutputStream.computeGroupSize( + number, + (MessageLite) unsafe.getObject(message, offset), + getMessageFieldSchema(i)); + } + break; + case 18: // DOUBLE_LIST: + size += + SchemaUtil.computeSizeFixed64List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 19: // FLOAT_LIST: + size += + SchemaUtil.computeSizeFixed32List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 20: // INT64_LIST: + size += + SchemaUtil.computeSizeInt64List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 21: // UINT64_LIST: + size += + SchemaUtil.computeSizeUInt64List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 22: // INT32_LIST: + size += + SchemaUtil.computeSizeInt32List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 23: // FIXED64_LIST: + size += + SchemaUtil.computeSizeFixed64List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 24: // FIXED32_LIST: + size += + SchemaUtil.computeSizeFixed32List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 25: // BOOL_LIST: + size += + SchemaUtil.computeSizeBoolList( + number, (List) unsafe.getObject(message, offset), false); + break; + case 26: // STRING_LIST: + size += + SchemaUtil.computeSizeStringList(number, (List) unsafe.getObject(message, offset)); + break; + case 27: // MESSAGE_LIST: + size += + SchemaUtil.computeSizeMessageList( + number, (List) unsafe.getObject(message, offset), getMessageFieldSchema(i)); + break; + case 28: // BYTES_LIST: + size += + SchemaUtil.computeSizeByteStringList( + number, (List) unsafe.getObject(message, offset)); + break; + case 29: // UINT32_LIST: + size += + SchemaUtil.computeSizeUInt32List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 30: // ENUM_LIST: + size += + SchemaUtil.computeSizeEnumList( + number, (List) unsafe.getObject(message, offset), false); + break; + case 31: // SFIXED32_LIST: + size += + SchemaUtil.computeSizeFixed32List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 32: // SFIXED64_LIST: + size += + SchemaUtil.computeSizeFixed64List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 33: // SINT32_LIST: + size += + SchemaUtil.computeSizeSInt32List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 34: // SINT64_LIST: + size += + SchemaUtil.computeSizeSInt64List( + number, (List) unsafe.getObject(message, offset), false); + break; + case 35: + { // DOUBLE_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 36: + { // FLOAT_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 37: + { // INT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeInt64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 38: + { // UINT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeUInt64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 39: + { // INT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeInt32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 40: + { // FIXED64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 41: + { // FIXED32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 42: + { // BOOL_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeBoolListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 43: + { // UINT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeUInt32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 44: + { // ENUM_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeEnumListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 45: + { // SFIXED32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 46: + { // SFIXED64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 47: + { // SINT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeSInt32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 48: + { // SINT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeSInt64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 49: // GROUP_LIST: + size += + SchemaUtil.computeSizeGroupList( + number, + (List) unsafe.getObject(message, offset), + getMessageFieldSchema(i)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + size += + mapFieldSchema.getSerializedSize( + number, unsafe.getObject(message, offset), getMapFieldDefaultEntry(i)); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeDoubleSize(number, 0); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFloatSize(number, 0); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeUInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFixed64Size(number, 0); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFixed32Size(number, 0); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeBoolSize(number, true); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, i)) { + Object value = unsafe.getObject(message, offset); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSize(number, (ByteString) value); + } else { + size += CodedOutputStream.computeStringSize(number, (String) value); + } + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, i)) { + Object value = unsafe.getObject(message, offset); + size += SchemaUtil.computeSizeMessage(number, value, getMessageFieldSchema(i)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, i)) { + size += + CodedOutputStream.computeBytesSize( + number, (ByteString) unsafe.getObject(message, offset)); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeUInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeEnumSize(number, oneofIntAt(message, offset)); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSFixed32Size(number, 0); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSFixed64Size(number, 0); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, i)) { + size += + CodedOutputStream.computeGroupSize( + number, + (MessageLite) unsafe.getObject(message, offset), + getMessageFieldSchema(i)); + } + break; + default: + // Assume it's an empty entry. + } + } + + size += getUnknownFieldsSerializedSize(unknownFieldSchema, message); + + if (hasExtensions) { + size += extensionSchema.getExtensions(message).getSerializedSize(); + } + + return size; + } + + private int getSerializedSizeProto3(T message) { + final sun.misc.Unsafe unsafe = UNSAFE; + int size = 0; + for (int i = 0; i < buffer.length; i += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(i); + final int fieldType = type(typeAndOffset); + final int number = numberAt(i); + + final long offset = offset(typeAndOffset); + final int cachedSizeOffset = + fieldType >= FieldType.DOUBLE_LIST_PACKED.id() + && fieldType <= FieldType.SINT64_LIST_PACKED.id() + ? buffer[i + 2] & OFFSET_MASK + : 0; + + switch (fieldType) { + case 0: // DOUBLE: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeDoubleSize(number, 0); + } + break; + case 1: // FLOAT: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeFloatSize(number, 0); + } + break; + case 2: // INT64: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeInt64Size(number, UnsafeUtil.getLong(message, offset)); + } + break; + case 3: // UINT64: + if (isFieldPresent(message, i)) { + size += + CodedOutputStream.computeUInt64Size(number, UnsafeUtil.getLong(message, offset)); + } + break; + case 4: // INT32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeInt32Size(number, UnsafeUtil.getInt(message, offset)); + } + break; + case 5: // FIXED64: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeFixed64Size(number, 0); + } + break; + case 6: // FIXED32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeFixed32Size(number, 0); + } + break; + case 7: // BOOL: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeBoolSize(number, true); + } + break; + case 8: // STRING: + if (isFieldPresent(message, i)) { + Object value = UnsafeUtil.getObject(message, offset); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSize(number, (ByteString) value); + } else { + size += CodedOutputStream.computeStringSize(number, (String) value); + } + } + break; + case 9: // MESSAGE: + if (isFieldPresent(message, i)) { + Object value = UnsafeUtil.getObject(message, offset); + size += SchemaUtil.computeSizeMessage(number, value, getMessageFieldSchema(i)); + } + break; + case 10: // BYTES: + if (isFieldPresent(message, i)) { + ByteString value = (ByteString) UnsafeUtil.getObject(message, offset); + size += CodedOutputStream.computeBytesSize(number, value); + } + break; + case 11: // UINT32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeUInt32Size(number, UnsafeUtil.getInt(message, offset)); + } + break; + case 12: // ENUM: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeEnumSize(number, UnsafeUtil.getInt(message, offset)); + } + break; + case 13: // SFIXED32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeSFixed32Size(number, 0); + } + break; + case 14: // SFIXED64: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeSFixed64Size(number, 0); + } + break; + case 15: // SINT32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeSInt32Size(number, UnsafeUtil.getInt(message, offset)); + } + break; + case 16: // SINT64: + if (isFieldPresent(message, i)) { + size += + CodedOutputStream.computeSInt64Size(number, UnsafeUtil.getLong(message, offset)); + } + break; + case 17: // GROUP: + if (isFieldPresent(message, i)) { + size += + CodedOutputStream.computeGroupSize( + number, + (MessageLite) UnsafeUtil.getObject(message, offset), + getMessageFieldSchema(i)); + } + break; + case 18: // DOUBLE_LIST: + size += SchemaUtil.computeSizeFixed64List(number, listAt(message, offset), false); + break; + case 19: // FLOAT_LIST: + size += SchemaUtil.computeSizeFixed32List(number, listAt(message, offset), false); + break; + case 20: // INT64_LIST: + size += + SchemaUtil.computeSizeInt64List(number, (List) listAt(message, offset), false); + break; + case 21: // UINT64_LIST: + size += + SchemaUtil.computeSizeUInt64List(number, (List) listAt(message, offset), false); + break; + case 22: // INT32_LIST: + size += + SchemaUtil.computeSizeInt32List( + number, (List) listAt(message, offset), false); + break; + case 23: // FIXED64_LIST: + size += SchemaUtil.computeSizeFixed64List(number, listAt(message, offset), false); + break; + case 24: // FIXED32_LIST: + size += SchemaUtil.computeSizeFixed32List(number, listAt(message, offset), false); + break; + case 25: // BOOL_LIST: + size += SchemaUtil.computeSizeBoolList(number, listAt(message, offset), false); + break; + case 26: // STRING_LIST: + size += SchemaUtil.computeSizeStringList(number, listAt(message, offset)); + break; + case 27: // MESSAGE_LIST: + size += + SchemaUtil.computeSizeMessageList( + number, listAt(message, offset), getMessageFieldSchema(i)); + break; + case 28: // BYTES_LIST: + size += + SchemaUtil.computeSizeByteStringList( + number, (List) listAt(message, offset)); + break; + case 29: // UINT32_LIST: + size += + SchemaUtil.computeSizeUInt32List( + number, (List) listAt(message, offset), false); + break; + case 30: // ENUM_LIST: + size += + SchemaUtil.computeSizeEnumList( + number, (List) listAt(message, offset), false); + break; + case 31: // SFIXED32_LIST: + size += SchemaUtil.computeSizeFixed32List(number, listAt(message, offset), false); + break; + case 32: // SFIXED64_LIST: + size += SchemaUtil.computeSizeFixed64List(number, listAt(message, offset), false); + break; + case 33: // SINT32_LIST: + size += + SchemaUtil.computeSizeSInt32List( + number, (List) listAt(message, offset), false); + break; + case 34: // SINT64_LIST: + size += + SchemaUtil.computeSizeSInt64List(number, (List) listAt(message, offset), false); + break; + case 35: + { // DOUBLE_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 36: + { // FLOAT_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 37: + { // INT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeInt64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 38: + { // UINT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeUInt64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 39: + { // INT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeInt32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 40: + { // FIXED64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 41: + { // FIXED32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 42: + { // BOOL_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeBoolListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 43: + { // UINT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeUInt32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 44: + { // ENUM_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeEnumListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 45: + { // SFIXED32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 46: + { // SFIXED64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 47: + { // SINT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeSInt32ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 48: + { // SINT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeSInt64ListNoTag( + (List) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 49: // GROUP_LIST: + size += + SchemaUtil.computeSizeGroupList( + number, (List) listAt(message, offset), getMessageFieldSchema(i)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + size += + mapFieldSchema.getSerializedSize( + number, UnsafeUtil.getObject(message, offset), getMapFieldDefaultEntry(i)); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeDoubleSize(number, 0); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFloatSize(number, 0); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeUInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFixed64Size(number, 0); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFixed32Size(number, 0); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeBoolSize(number, true); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, i)) { + Object value = UnsafeUtil.getObject(message, offset); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSize(number, (ByteString) value); + } else { + size += CodedOutputStream.computeStringSize(number, (String) value); + } + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, i)) { + Object value = UnsafeUtil.getObject(message, offset); + size += SchemaUtil.computeSizeMessage(number, value, getMessageFieldSchema(i)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, i)) { + size += + CodedOutputStream.computeBytesSize( + number, (ByteString) UnsafeUtil.getObject(message, offset)); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeUInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeEnumSize(number, oneofIntAt(message, offset)); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSFixed32Size(number, 0); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSFixed64Size(number, 0); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, i)) { + size += + CodedOutputStream.computeGroupSize( + number, + (MessageLite) UnsafeUtil.getObject(message, offset), + getMessageFieldSchema(i)); + } + break; + default: + // Assume it's an empty entry. + } + } + + size += getUnknownFieldsSerializedSize(unknownFieldSchema, message); + + return size; + } + + private int getUnknownFieldsSerializedSize( + UnknownFieldSchema schema, T message) { + UT unknowns = schema.getFromMessage(message); + return schema.getSerializedSize(unknowns); + } + + private static List listAt(Object message, long offset) { + return (List) UnsafeUtil.getObject(message, offset); + } + + @SuppressWarnings("unchecked") + @Override + // TODO(nathanmittler): Consider serializing oneof fields last so that only one entry per + // oneof is actually serialized. This would mean that we would violate the serialization order + // contract. It should also be noted that Go currently does this. + public void writeTo(T message, Writer writer) throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + writeFieldsInDescendingOrder(message, writer); + } else { + if (proto3) { + writeFieldsInAscendingOrderProto3(message, writer); + } else { + writeFieldsInAscendingOrderProto2(message, writer); + } + } + } + + @SuppressWarnings("unchecked") + private void writeFieldsInAscendingOrderProto2(T message, Writer writer) throws IOException { + Iterator> extensionIterator = null; + Map.Entry nextExtension = null; + if (hasExtensions) { + FieldSet extensions = extensionSchema.getExtensions(message); + if (!extensions.isEmpty()) { + extensionIterator = extensions.iterator(); + nextExtension = extensionIterator.next(); + } + } + int currentPresenceFieldOffset = -1; + int currentPresenceField = 0; + final int bufferLength = buffer.length; + final sun.misc.Unsafe unsafe = UNSAFE; + for (int pos = 0; pos < bufferLength; pos += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(pos); + final int number = numberAt(pos); + final int fieldType = type(typeAndOffset); + + int presenceMaskAndOffset = 0; + int presenceMask = 0; + if (!proto3 && fieldType <= 17) { + presenceMaskAndOffset = buffer[pos + 2]; + final int presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + if (presenceFieldOffset != currentPresenceFieldOffset) { + currentPresenceFieldOffset = presenceFieldOffset; + currentPresenceField = unsafe.getInt(message, (long) presenceFieldOffset); + } + presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + } + + // Write any extensions that need to be written before the current field. + while (nextExtension != null && extensionSchema.extensionNumber(nextExtension) <= number) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + final long offset = offset(typeAndOffset); + + switch (fieldType) { + case 0: // DOUBLE: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeDouble(number, doubleAt(message, offset)); + } + break; + case 1: // FLOAT: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeFloat(number, floatAt(message, offset)); + } + break; + case 2: // INT64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeInt64(number, unsafe.getLong(message, offset)); + } + break; + case 3: // UINT64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeUInt64(number, unsafe.getLong(message, offset)); + } + break; + case 4: // INT32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeInt32(number, unsafe.getInt(message, offset)); + } + break; + case 5: // FIXED64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeFixed64(number, unsafe.getLong(message, offset)); + } + break; + case 6: // FIXED32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeFixed32(number, unsafe.getInt(message, offset)); + } + break; + case 7: // BOOL: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeBool(number, booleanAt(message, offset)); + } + break; + case 8: // STRING: + if ((currentPresenceField & presenceMask) != 0) { + writeString(number, unsafe.getObject(message, offset), writer); + } + break; + case 9: // MESSAGE: + if ((currentPresenceField & presenceMask) != 0) { + Object value = unsafe.getObject(message, offset); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 10: // BYTES: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeBytes(number, (ByteString) unsafe.getObject(message, offset)); + } + break; + case 11: // UINT32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeUInt32(number, unsafe.getInt(message, offset)); + } + break; + case 12: // ENUM: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeEnum(number, unsafe.getInt(message, offset)); + } + break; + case 13: // SFIXED32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeSFixed32(number, unsafe.getInt(message, offset)); + } + break; + case 14: // SFIXED64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeSFixed64(number, unsafe.getLong(message, offset)); + } + break; + case 15: // SINT32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeSInt32(number, unsafe.getInt(message, offset)); + } + break; + case 16: // SINT64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeSInt64(number, unsafe.getLong(message, offset)); + } + break; + case 17: // GROUP: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeGroup( + number, unsafe.getObject(message, offset), getMessageFieldSchema(pos)); + } + break; + case 18: // DOUBLE_LIST: + SchemaUtil.writeDoubleList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 19: // FLOAT_LIST: + SchemaUtil.writeFloatList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 20: // INT64_LIST: + SchemaUtil.writeInt64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 21: // UINT64_LIST: + SchemaUtil.writeUInt64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 22: // INT32_LIST: + SchemaUtil.writeInt32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 23: // FIXED64_LIST: + SchemaUtil.writeFixed64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 24: // FIXED32_LIST: + SchemaUtil.writeFixed32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 25: // BOOL_LIST: + SchemaUtil.writeBoolList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 26: // STRING_LIST: + SchemaUtil.writeStringList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer); + break; + case 27: // MESSAGE_LIST: + SchemaUtil.writeMessageList( + numberAt(pos), + (List) unsafe.getObject(message, offset), + writer, + getMessageFieldSchema(pos)); + break; + case 28: // BYTES_LIST: + SchemaUtil.writeBytesList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer); + break; + case 29: // UINT32_LIST: + SchemaUtil.writeUInt32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 30: // ENUM_LIST: + SchemaUtil.writeEnumList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 31: // SFIXED32_LIST: + SchemaUtil.writeSFixed32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 32: // SFIXED64_LIST: + SchemaUtil.writeSFixed64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 33: // SINT32_LIST: + SchemaUtil.writeSInt32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 34: // SINT64_LIST: + SchemaUtil.writeSInt64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, false); + break; + case 35: // DOUBLE_LIST_PACKED: + // TODO(xiaofeng): Make use of cached field size to speed up serialization. + SchemaUtil.writeDoubleList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 36: // FLOAT_LIST_PACKED: + SchemaUtil.writeFloatList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 37: // INT64_LIST_PACKED: + SchemaUtil.writeInt64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 38: // UINT64_LIST_PACKED: + SchemaUtil.writeUInt64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 39: // INT32_LIST_PACKED: + SchemaUtil.writeInt32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 40: // FIXED64_LIST_PACKED: + SchemaUtil.writeFixed64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 41: // FIXED32_LIST_PACKED: + SchemaUtil.writeFixed32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + + break; + case 42: // BOOL_LIST_PACKED: + SchemaUtil.writeBoolList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 43: // UINT32_LIST_PACKED: + SchemaUtil.writeUInt32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 44: // ENUM_LIST_PACKED: + SchemaUtil.writeEnumList( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 45: // SFIXED32_LIST_PACKED: + SchemaUtil.writeSFixed32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 46: // SFIXED64_LIST_PACKED: + SchemaUtil.writeSFixed64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 47: // SINT32_LIST_PACKED: + SchemaUtil.writeSInt32List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 48: // SINT64_LIST_PACKED: + SchemaUtil.writeSInt64List( + numberAt(pos), (List) unsafe.getObject(message, offset), writer, true); + break; + case 49: // GROUP_LIST: + SchemaUtil.writeGroupList( + numberAt(pos), + (List) unsafe.getObject(message, offset), + writer, + getMessageFieldSchema(pos)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + writeMapHelper(writer, number, unsafe.getObject(message, offset), pos); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, pos)) { + writer.writeDouble(number, oneofDoubleAt(message, offset)); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, pos)) { + writer.writeFloat(number, oneofFloatAt(message, offset)); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, pos)) { + writer.writeInt64(number, oneofLongAt(message, offset)); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt64(number, oneofLongAt(message, offset)); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, pos)) { + writer.writeInt32(number, oneofIntAt(message, offset)); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed64(number, oneofLongAt(message, offset)); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed32(number, oneofIntAt(message, offset)); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, pos)) { + writer.writeBool(number, oneofBooleanAt(message, offset)); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, pos)) { + writeString(number, unsafe.getObject(message, offset), writer); + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, pos)) { + Object value = unsafe.getObject(message, offset); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, pos)) { + writer.writeBytes(number, (ByteString) unsafe.getObject(message, offset)); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt32(number, oneofIntAt(message, offset)); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, pos)) { + writer.writeEnum(number, oneofIntAt(message, offset)); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed32(number, oneofIntAt(message, offset)); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed64(number, oneofLongAt(message, offset)); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt32(number, oneofIntAt(message, offset)); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt64(number, oneofLongAt(message, offset)); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, pos)) { + writer.writeGroup( + number, unsafe.getObject(message, offset), getMessageFieldSchema(pos)); + } + break; + default: + // Assume it's an empty entry - just go to the next entry. + break; + } + } + while (nextExtension != null) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + writeUnknownInMessageTo(unknownFieldSchema, message, writer); + } + + @SuppressWarnings("unchecked") + private void writeFieldsInAscendingOrderProto3(T message, Writer writer) throws IOException { + Iterator> extensionIterator = null; + Map.Entry nextExtension = null; + if (hasExtensions) { + FieldSet extensions = extensionSchema.getExtensions(message); + if (!extensions.isEmpty()) { + extensionIterator = extensions.iterator(); + nextExtension = extensionIterator.next(); + } + } + + final int bufferLength = buffer.length; + for (int pos = 0; pos < bufferLength; pos += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(pos); + final int number = numberAt(pos); + + // Write any extensions that need to be written before the current field. + while (nextExtension != null && extensionSchema.extensionNumber(nextExtension) <= number) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + if (isFieldPresent(message, pos)) { + writer.writeDouble(number, doubleAt(message, offset(typeAndOffset))); + } + break; + case 1: // FLOAT: + if (isFieldPresent(message, pos)) { + writer.writeFloat(number, floatAt(message, offset(typeAndOffset))); + } + break; + case 2: // INT64: + if (isFieldPresent(message, pos)) { + writer.writeInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 3: // UINT64: + if (isFieldPresent(message, pos)) { + writer.writeUInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 4: // INT32: + if (isFieldPresent(message, pos)) { + writer.writeInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 5: // FIXED64: + if (isFieldPresent(message, pos)) { + writer.writeFixed64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 6: // FIXED32: + if (isFieldPresent(message, pos)) { + writer.writeFixed32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 7: // BOOL: + if (isFieldPresent(message, pos)) { + writer.writeBool(number, booleanAt(message, offset(typeAndOffset))); + } + break; + case 8: // STRING: + if (isFieldPresent(message, pos)) { + writeString(number, UnsafeUtil.getObject(message, offset(typeAndOffset)), writer); + } + break; + case 9: // MESSAGE: + if (isFieldPresent(message, pos)) { + Object value = UnsafeUtil.getObject(message, offset(typeAndOffset)); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 10: // BYTES: + if (isFieldPresent(message, pos)) { + writer.writeBytes( + number, (ByteString) UnsafeUtil.getObject(message, offset(typeAndOffset))); + } + break; + case 11: // UINT32: + if (isFieldPresent(message, pos)) { + writer.writeUInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 12: // ENUM: + if (isFieldPresent(message, pos)) { + writer.writeEnum(number, intAt(message, offset(typeAndOffset))); + } + break; + case 13: // SFIXED32: + if (isFieldPresent(message, pos)) { + writer.writeSFixed32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 14: // SFIXED64: + if (isFieldPresent(message, pos)) { + writer.writeSFixed64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 15: // SINT32: + if (isFieldPresent(message, pos)) { + writer.writeSInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 16: // SINT64: + if (isFieldPresent(message, pos)) { + writer.writeSInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 17: // GROUP: + if (isFieldPresent(message, pos)) { + writer.writeGroup( + number, + UnsafeUtil.getObject(message, offset(typeAndOffset)), + getMessageFieldSchema(pos)); + } + break; + case 18: // DOUBLE_LIST: + SchemaUtil.writeDoubleList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 19: // FLOAT_LIST: + SchemaUtil.writeFloatList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 20: // INT64_LIST: + SchemaUtil.writeInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 21: // UINT64_LIST: + SchemaUtil.writeUInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 22: // INT32_LIST: + SchemaUtil.writeInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 23: // FIXED64_LIST: + SchemaUtil.writeFixed64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 24: // FIXED32_LIST: + SchemaUtil.writeFixed32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 25: // BOOL_LIST: + SchemaUtil.writeBoolList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 26: // STRING_LIST: + SchemaUtil.writeStringList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer); + break; + case 27: // MESSAGE_LIST: + SchemaUtil.writeMessageList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + getMessageFieldSchema(pos)); + break; + case 28: // BYTES_LIST: + SchemaUtil.writeBytesList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer); + break; + case 29: // UINT32_LIST: + SchemaUtil.writeUInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 30: // ENUM_LIST: + SchemaUtil.writeEnumList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 31: // SFIXED32_LIST: + SchemaUtil.writeSFixed32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 32: // SFIXED64_LIST: + SchemaUtil.writeSFixed64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 33: // SINT32_LIST: + SchemaUtil.writeSInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 34: // SINT64_LIST: + SchemaUtil.writeSInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 35: // DOUBLE_LIST_PACKED: + // TODO(xiaofeng): Make use of cached field size to speed up serialization. + SchemaUtil.writeDoubleList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 36: // FLOAT_LIST_PACKED: + SchemaUtil.writeFloatList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 37: // INT64_LIST_PACKED: + SchemaUtil.writeInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 38: // UINT64_LIST_PACKED: + SchemaUtil.writeUInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 39: // INT32_LIST_PACKED: + SchemaUtil.writeInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 40: // FIXED64_LIST_PACKED: + SchemaUtil.writeFixed64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 41: // FIXED32_LIST_PACKED: + SchemaUtil.writeFixed32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + + break; + case 42: // BOOL_LIST_PACKED: + SchemaUtil.writeBoolList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 43: // UINT32_LIST_PACKED: + SchemaUtil.writeUInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 44: // ENUM_LIST_PACKED: + SchemaUtil.writeEnumList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 45: // SFIXED32_LIST_PACKED: + SchemaUtil.writeSFixed32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 46: // SFIXED64_LIST_PACKED: + SchemaUtil.writeSFixed64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 47: // SINT32_LIST_PACKED: + SchemaUtil.writeSInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 48: // SINT64_LIST_PACKED: + SchemaUtil.writeSInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 49: // GROUP_LIST: + SchemaUtil.writeGroupList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + getMessageFieldSchema(pos)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + writeMapHelper(writer, number, UnsafeUtil.getObject(message, offset(typeAndOffset)), pos); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, pos)) { + writer.writeDouble(number, oneofDoubleAt(message, offset(typeAndOffset))); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, pos)) { + writer.writeFloat(number, oneofFloatAt(message, offset(typeAndOffset))); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, pos)) { + writer.writeInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, pos)) { + writer.writeInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, pos)) { + writer.writeBool(number, oneofBooleanAt(message, offset(typeAndOffset))); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, pos)) { + writeString(number, UnsafeUtil.getObject(message, offset(typeAndOffset)), writer); + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, pos)) { + Object value = UnsafeUtil.getObject(message, offset(typeAndOffset)); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, pos)) { + writer.writeBytes( + number, (ByteString) UnsafeUtil.getObject(message, offset(typeAndOffset))); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, pos)) { + writer.writeEnum(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, pos)) { + writer.writeGroup( + number, + UnsafeUtil.getObject(message, offset(typeAndOffset)), + getMessageFieldSchema(pos)); + } + break; + default: + // Assume it's an empty entry - just go to the next entry. + break; + } + } + while (nextExtension != null) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + writeUnknownInMessageTo(unknownFieldSchema, message, writer); + } + + @SuppressWarnings("unchecked") + private void writeFieldsInDescendingOrder(T message, Writer writer) throws IOException { + writeUnknownInMessageTo(unknownFieldSchema, message, writer); + + Iterator> extensionIterator = null; + Map.Entry nextExtension = null; + if (hasExtensions) { + FieldSet extensions = extensionSchema.getExtensions(message); + if (!extensions.isEmpty()) { + extensionIterator = extensions.descendingIterator(); + nextExtension = extensionIterator.next(); + } + } + + for (int pos = buffer.length - INTS_PER_FIELD; pos >= 0; pos -= INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(pos); + final int number = numberAt(pos); + + // Write any extensions that need to be written before the current field. + while (nextExtension != null && extensionSchema.extensionNumber(nextExtension) > number) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + if (isFieldPresent(message, pos)) { + writer.writeDouble(number, doubleAt(message, offset(typeAndOffset))); + } + break; + case 1: // FLOAT: + if (isFieldPresent(message, pos)) { + writer.writeFloat(number, floatAt(message, offset(typeAndOffset))); + } + break; + case 2: // INT64: + if (isFieldPresent(message, pos)) { + writer.writeInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 3: // UINT64: + if (isFieldPresent(message, pos)) { + writer.writeUInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 4: // INT32: + if (isFieldPresent(message, pos)) { + writer.writeInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 5: // FIXED64: + if (isFieldPresent(message, pos)) { + writer.writeFixed64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 6: // FIXED32: + if (isFieldPresent(message, pos)) { + writer.writeFixed32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 7: // BOOL: + if (isFieldPresent(message, pos)) { + writer.writeBool(number, booleanAt(message, offset(typeAndOffset))); + } + break; + case 8: // STRING: + if (isFieldPresent(message, pos)) { + writeString(number, UnsafeUtil.getObject(message, offset(typeAndOffset)), writer); + } + break; + case 9: // MESSAGE: + if (isFieldPresent(message, pos)) { + Object value = UnsafeUtil.getObject(message, offset(typeAndOffset)); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 10: // BYTES: + if (isFieldPresent(message, pos)) { + writer.writeBytes( + number, (ByteString) UnsafeUtil.getObject(message, offset(typeAndOffset))); + } + break; + case 11: // UINT32: + if (isFieldPresent(message, pos)) { + writer.writeUInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 12: // ENUM: + if (isFieldPresent(message, pos)) { + writer.writeEnum(number, intAt(message, offset(typeAndOffset))); + } + break; + case 13: // SFIXED32: + if (isFieldPresent(message, pos)) { + writer.writeSFixed32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 14: // SFIXED64: + if (isFieldPresent(message, pos)) { + writer.writeSFixed64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 15: // SINT32: + if (isFieldPresent(message, pos)) { + writer.writeSInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 16: // SINT64: + if (isFieldPresent(message, pos)) { + writer.writeSInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 17: // GROUP: + if (isFieldPresent(message, pos)) { + writer.writeGroup( + number, + UnsafeUtil.getObject(message, offset(typeAndOffset)), + getMessageFieldSchema(pos)); + } + break; + case 18: // DOUBLE_LIST: + SchemaUtil.writeDoubleList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 19: // FLOAT_LIST: + SchemaUtil.writeFloatList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 20: // INT64_LIST: + SchemaUtil.writeInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 21: // UINT64_LIST: + SchemaUtil.writeUInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 22: // INT32_LIST: + SchemaUtil.writeInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 23: // FIXED64_LIST: + SchemaUtil.writeFixed64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 24: // FIXED32_LIST: + SchemaUtil.writeFixed32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 25: // BOOL_LIST: + SchemaUtil.writeBoolList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 26: // STRING_LIST: + SchemaUtil.writeStringList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer); + break; + case 27: // MESSAGE_LIST: + SchemaUtil.writeMessageList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + getMessageFieldSchema(pos)); + break; + case 28: // BYTES_LIST: + SchemaUtil.writeBytesList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer); + break; + case 29: // UINT32_LIST: + SchemaUtil.writeUInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 30: // ENUM_LIST: + SchemaUtil.writeEnumList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 31: // SFIXED32_LIST: + SchemaUtil.writeSFixed32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 32: // SFIXED64_LIST: + SchemaUtil.writeSFixed64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 33: // SINT32_LIST: + SchemaUtil.writeSInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 34: // SINT64_LIST: + SchemaUtil.writeSInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 35: // DOUBLE_LIST_PACKED: + SchemaUtil.writeDoubleList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 36: // FLOAT_LIST_PACKED: + SchemaUtil.writeFloatList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 37: // INT64_LIST_PACKED: + SchemaUtil.writeInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 38: // UINT64_LIST_PACKED: + SchemaUtil.writeUInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 39: // INT32_LIST_PACKED: + SchemaUtil.writeInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 40: // FIXED64_LIST_PACKED: + SchemaUtil.writeFixed64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 41: // FIXED32_LIST_PACKED: + SchemaUtil.writeFixed32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + + break; + case 42: // BOOL_LIST_PACKED: + SchemaUtil.writeBoolList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 43: // UINT32_LIST_PACKED: + SchemaUtil.writeUInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 44: // ENUM_LIST_PACKED: + SchemaUtil.writeEnumList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 45: // SFIXED32_LIST_PACKED: + SchemaUtil.writeSFixed32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 46: // SFIXED64_LIST_PACKED: + SchemaUtil.writeSFixed64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 47: // SINT32_LIST_PACKED: + SchemaUtil.writeSInt32List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 48: // SINT64_LIST_PACKED: + SchemaUtil.writeSInt64List( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 49: // GROUP_LIST: + SchemaUtil.writeGroupList( + numberAt(pos), + (List) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + getMessageFieldSchema(pos)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + writeMapHelper(writer, number, UnsafeUtil.getObject(message, offset(typeAndOffset)), pos); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, pos)) { + writer.writeDouble(number, oneofDoubleAt(message, offset(typeAndOffset))); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, pos)) { + writer.writeFloat(number, oneofFloatAt(message, offset(typeAndOffset))); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, pos)) { + writer.writeInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, pos)) { + writer.writeInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, pos)) { + writer.writeBool(number, oneofBooleanAt(message, offset(typeAndOffset))); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, pos)) { + writeString(number, UnsafeUtil.getObject(message, offset(typeAndOffset)), writer); + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, pos)) { + Object value = UnsafeUtil.getObject(message, offset(typeAndOffset)); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, pos)) { + writer.writeBytes( + number, (ByteString) UnsafeUtil.getObject(message, offset(typeAndOffset))); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, pos)) { + writer.writeEnum(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, pos)) { + writer.writeGroup( + number, + UnsafeUtil.getObject(message, offset(typeAndOffset)), + getMessageFieldSchema(pos)); + } + break; + default: + break; + } + } + while (nextExtension != null) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + } + + @SuppressWarnings("unchecked") + private void writeMapHelper(Writer writer, int number, Object mapField, int pos) + throws IOException { + if (mapField != null) { + writer.writeMap( + number, + (MapEntryLite.Metadata) mapFieldSchema.forMapMetadata(getMapFieldDefaultEntry(pos)), + (Map) mapFieldSchema.forMapData(mapField)); + } + } + + private void writeUnknownInMessageTo( + UnknownFieldSchema schema, T message, Writer writer) throws IOException { + schema.writeTo(schema.getFromMessage(message), writer); + } + + @Override + public void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (extensionRegistry == null) { + throw new NullPointerException(); + } + mergeFromHelper(unknownFieldSchema, extensionSchema, message, reader, extensionRegistry); + } + + /** + * A helper method for wildcard capture of {@code unknownFieldSchema}. See: + * https://docs.oracle.com/javase/tutorial/java/generics/capture.html + */ + private > void mergeFromHelper( + UnknownFieldSchema unknownFieldSchema, + ExtensionSchema extensionSchema, + T message, + Reader reader, + ExtensionRegistryLite extensionRegistry) + throws IOException { + UB unknownFields = null; + FieldSet extensions = null; + try { + while (true) { + final int number = reader.getFieldNumber(); + final int pos = positionForFieldNumber(number); + if (pos < 0) { + if (number == Reader.READ_DONE) { + return; + } + // Check if it's an extension. + Object extension = + !hasExtensions + ? null + : extensionSchema.findExtensionByNumber( + extensionRegistry, defaultInstance, number); + if (extension != null) { + if (extensions == null) { + extensions = extensionSchema.getMutableExtensions(message); + } + unknownFields = + extensionSchema.parseExtension( + reader, + extension, + extensionRegistry, + extensions, + unknownFields, + unknownFieldSchema); + continue; + } + if (unknownFieldSchema.shouldDiscardUnknownFields(reader)) { + if (reader.skipField()) { + continue; + } + } else { + if (unknownFields == null) { + unknownFields = unknownFieldSchema.getBuilderFromMessage(message); + } + // Unknown field. + if (unknownFieldSchema.mergeOneFieldFrom(unknownFields, reader)) { + continue; + } + } + // Done reading. + return; + } + final int typeAndOffset = typeAndOffsetAt(pos); + + try { + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + UnsafeUtil.putDouble(message, offset(typeAndOffset), reader.readDouble()); + setFieldPresent(message, pos); + break; + case 1: // FLOAT: + UnsafeUtil.putFloat(message, offset(typeAndOffset), reader.readFloat()); + setFieldPresent(message, pos); + break; + case 2: // INT64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readInt64()); + setFieldPresent(message, pos); + break; + case 3: // UINT64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readUInt64()); + setFieldPresent(message, pos); + break; + case 4: // INT32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readInt32()); + setFieldPresent(message, pos); + break; + case 5: // FIXED64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readFixed64()); + setFieldPresent(message, pos); + break; + case 6: // FIXED32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readFixed32()); + setFieldPresent(message, pos); + break; + case 7: // BOOL: + UnsafeUtil.putBoolean(message, offset(typeAndOffset), reader.readBool()); + setFieldPresent(message, pos); + break; + case 8: // STRING: + readString(message, typeAndOffset, reader); + setFieldPresent(message, pos); + break; + case 9: + { // MESSAGE: + if (isFieldPresent(message, pos)) { + Object mergedResult = + Internal.mergeMessage( + UnsafeUtil.getObject(message, offset(typeAndOffset)), + reader.readMessageBySchemaWithCheck( + (Schema) getMessageFieldSchema(pos), extensionRegistry)); + UnsafeUtil.putObject(message, offset(typeAndOffset), mergedResult); + } else { + UnsafeUtil.putObject( + message, + offset(typeAndOffset), + reader.readMessageBySchemaWithCheck( + (Schema) getMessageFieldSchema(pos), extensionRegistry)); + setFieldPresent(message, pos); + } + break; + } + case 10: // BYTES: + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readBytes()); + setFieldPresent(message, pos); + break; + case 11: // UINT32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readUInt32()); + setFieldPresent(message, pos); + break; + case 12: // ENUM: + { + int enumValue = reader.readEnum(); + EnumVerifier enumVerifier = getEnumFieldVerifier(pos); + if (enumVerifier == null || enumVerifier.isInRange(enumValue)) { + UnsafeUtil.putInt(message, offset(typeAndOffset), enumValue); + setFieldPresent(message, pos); + } else { + unknownFields = + SchemaUtil.storeUnknownEnum( + number, enumValue, unknownFields, unknownFieldSchema); + } + break; + } + case 13: // SFIXED32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readSFixed32()); + setFieldPresent(message, pos); + break; + case 14: // SFIXED64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readSFixed64()); + setFieldPresent(message, pos); + break; + case 15: // SINT32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readSInt32()); + setFieldPresent(message, pos); + break; + case 16: // SINT64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readSInt64()); + setFieldPresent(message, pos); + break; + case 17: + { // GROUP: + if (isFieldPresent(message, pos)) { + Object mergedResult = + Internal.mergeMessage( + UnsafeUtil.getObject(message, offset(typeAndOffset)), + reader.readGroupBySchemaWithCheck( + (Schema) getMessageFieldSchema(pos), extensionRegistry)); + UnsafeUtil.putObject(message, offset(typeAndOffset), mergedResult); + } else { + UnsafeUtil.putObject( + message, + offset(typeAndOffset), + reader.readGroupBySchemaWithCheck( + (Schema) getMessageFieldSchema(pos), extensionRegistry)); + setFieldPresent(message, pos); + } + break; + } + case 18: // DOUBLE_LIST: + reader.readDoubleList( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 19: // FLOAT_LIST: + reader.readFloatList( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 20: // INT64_LIST: + reader.readInt64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 21: // UINT64_LIST: + reader.readUInt64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 22: // INT32_LIST: + reader.readInt32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 23: // FIXED64_LIST: + reader.readFixed64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 24: // FIXED32_LIST: + reader.readFixed32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 25: // BOOL_LIST: + reader.readBoolList( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 26: // STRING_LIST: + readStringList(message, typeAndOffset, reader); + break; + case 27: + { // MESSAGE_LIST: + readMessageList( + message, + typeAndOffset, + reader, + (Schema) getMessageFieldSchema(pos), + extensionRegistry); + break; + } + case 28: // BYTES_LIST: + reader.readBytesList( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 29: // UINT32_LIST: + reader.readUInt32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 30: // ENUM_LIST: + { + List enumList = + listFieldSchema.mutableListAt(message, offset(typeAndOffset)); + reader.readEnumList(enumList); + unknownFields = + SchemaUtil.filterUnknownEnumList( + number, + enumList, + getEnumFieldVerifier(pos), + unknownFields, + unknownFieldSchema); + break; + } + case 31: // SFIXED32_LIST: + reader.readSFixed32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 32: // SFIXED64_LIST: + reader.readSFixed64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 33: // SINT32_LIST: + reader.readSInt32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 34: // SINT64_LIST: + reader.readSInt64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 35: // DOUBLE_LIST_PACKED: + reader.readDoubleList( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 36: // FLOAT_LIST_PACKED: + reader.readFloatList( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 37: // INT64_LIST_PACKED: + reader.readInt64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 38: // UINT64_LIST_PACKED: + reader.readUInt64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 39: // INT32_LIST_PACKED: + reader.readInt32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 40: // FIXED64_LIST_PACKED: + reader.readFixed64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 41: // FIXED32_LIST_PACKED: + reader.readFixed32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 42: // BOOL_LIST_PACKED: + reader.readBoolList( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 43: // UINT32_LIST_PACKED: + reader.readUInt32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 44: // ENUM_LIST_PACKED: + { + List enumList = + listFieldSchema.mutableListAt(message, offset(typeAndOffset)); + reader.readEnumList(enumList); + unknownFields = + SchemaUtil.filterUnknownEnumList( + number, + enumList, + getEnumFieldVerifier(pos), + unknownFields, + unknownFieldSchema); + break; + } + case 45: // SFIXED32_LIST_PACKED: + reader.readSFixed32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 46: // SFIXED64_LIST_PACKED: + reader.readSFixed64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 47: // SINT32_LIST_PACKED: + reader.readSInt32List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 48: // SINT64_LIST_PACKED: + reader.readSInt64List( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + break; + case 49: + { // GROUP_LIST: + readGroupList( + message, + offset(typeAndOffset), + reader, + (Schema) getMessageFieldSchema(pos), + extensionRegistry); + break; + } + case 50: // MAP: + mergeMap(message, pos, getMapFieldDefaultEntry(pos), extensionRegistry, reader); + break; + case 51: // ONEOF_DOUBLE: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Double.valueOf(reader.readDouble())); + setOneofPresent(message, number, pos); + break; + case 52: // ONEOF_FLOAT: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Float.valueOf(reader.readFloat())); + setOneofPresent(message, number, pos); + break; + case 53: // ONEOF_INT64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readInt64())); + setOneofPresent(message, number, pos); + break; + case 54: // ONEOF_UINT64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readUInt64())); + setOneofPresent(message, number, pos); + break; + case 55: // ONEOF_INT32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readInt32())); + setOneofPresent(message, number, pos); + break; + case 56: // ONEOF_FIXED64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readFixed64())); + setOneofPresent(message, number, pos); + break; + case 57: // ONEOF_FIXED32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readFixed32())); + setOneofPresent(message, number, pos); + break; + case 58: // ONEOF_BOOL: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Boolean.valueOf(reader.readBool())); + setOneofPresent(message, number, pos); + break; + case 59: // ONEOF_STRING: + readString(message, typeAndOffset, reader); + setOneofPresent(message, number, pos); + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, pos)) { + Object mergedResult = + Internal.mergeMessage( + UnsafeUtil.getObject(message, offset(typeAndOffset)), + reader.readMessageBySchemaWithCheck( + getMessageFieldSchema(pos), extensionRegistry)); + UnsafeUtil.putObject(message, offset(typeAndOffset), mergedResult); + } else { + UnsafeUtil.putObject( + message, + offset(typeAndOffset), + reader.readMessageBySchemaWithCheck( + getMessageFieldSchema(pos), extensionRegistry)); + setFieldPresent(message, pos); + } + setOneofPresent(message, number, pos); + break; + case 61: // ONEOF_BYTES: + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readBytes()); + setOneofPresent(message, number, pos); + break; + case 62: // ONEOF_UINT32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readUInt32())); + setOneofPresent(message, number, pos); + break; + case 63: // ONEOF_ENUM: + { + int enumValue = reader.readEnum(); + EnumVerifier enumVerifier = getEnumFieldVerifier(pos); + if (enumVerifier == null || enumVerifier.isInRange(enumValue)) { + UnsafeUtil.putObject(message, offset(typeAndOffset), enumValue); + setOneofPresent(message, number, pos); + } else { + unknownFields = + SchemaUtil.storeUnknownEnum( + number, enumValue, unknownFields, unknownFieldSchema); + } + break; + } + case 64: // ONEOF_SFIXED32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readSFixed32())); + setOneofPresent(message, number, pos); + break; + case 65: // ONEOF_SFIXED64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readSFixed64())); + setOneofPresent(message, number, pos); + break; + case 66: // ONEOF_SINT32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readSInt32())); + setOneofPresent(message, number, pos); + break; + case 67: // ONEOF_SINT64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readSInt64())); + setOneofPresent(message, number, pos); + break; + case 68: // ONEOF_GROUP: + UnsafeUtil.putObject( + message, + offset(typeAndOffset), + reader.readGroupBySchemaWithCheck(getMessageFieldSchema(pos), extensionRegistry)); + setOneofPresent(message, number, pos); + break; + default: + // Assume we've landed on an empty entry. Treat it as an unknown field. + if (unknownFields == null) { + unknownFields = unknownFieldSchema.newBuilder(); + } + if (!unknownFieldSchema.mergeOneFieldFrom(unknownFields, reader)) { + return; + } + break; + } + } catch (InvalidProtocolBufferException.InvalidWireTypeException e) { + // Treat fields with an invalid wire type as unknown fields + // (i.e. same as the default case). + if (unknownFieldSchema.shouldDiscardUnknownFields(reader)) { + if (!reader.skipField()) { + return; + } + } else { + if (unknownFields == null) { + unknownFields = unknownFieldSchema.getBuilderFromMessage(message); + } + if (!unknownFieldSchema.mergeOneFieldFrom(unknownFields, reader)) { + return; + } + } + } + } + } finally { + for (int i = checkInitializedCount; i < repeatedFieldOffsetStart; i++) { + unknownFields = + filterMapUnknownEnumValues(message, intArray[i], unknownFields, unknownFieldSchema); + } + if (unknownFields != null) { + unknownFieldSchema.setBuilderToMessage(message, unknownFields); + } + } + } + + @SuppressWarnings("ReferenceEquality") + static UnknownFieldSetLite getMutableUnknownFields(Object message) { + UnknownFieldSetLite unknownFields = ((GeneratedMessageLite) message).unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = UnknownFieldSetLite.newInstance(); + ((GeneratedMessageLite) message).unknownFields = unknownFields; + } + return unknownFields; + } + + /** Decodes a map entry key or value. Stores result in registers.object1. */ + private int decodeMapEntryValue( + byte[] data, + int position, + int limit, + WireFormat.FieldType fieldType, + Class messageType, + Registers registers) + throws IOException { + switch (fieldType) { + case BOOL: + position = decodeVarint64(data, position, registers); + registers.object1 = registers.long1 != 0; + break; + case BYTES: + position = decodeBytes(data, position, registers); + break; + case DOUBLE: + registers.object1 = decodeDouble(data, position); + position += 8; + break; + case FIXED32: + case SFIXED32: + registers.object1 = decodeFixed32(data, position); + position += 4; + break; + case FIXED64: + case SFIXED64: + registers.object1 = decodeFixed64(data, position); + position += 8; + break; + case FLOAT: + registers.object1 = decodeFloat(data, position); + position += 4; + break; + case ENUM: + case INT32: + case UINT32: + position = decodeVarint32(data, position, registers); + registers.object1 = registers.int1; + break; + case INT64: + case UINT64: + position = decodeVarint64(data, position, registers); + registers.object1 = registers.long1; + break; + case MESSAGE: + position = + decodeMessageField( + Protobuf.getInstance().schemaFor(messageType), data, position, limit, registers); + break; + case SINT32: + position = decodeVarint32(data, position, registers); + registers.object1 = CodedInputStream.decodeZigZag32(registers.int1); + break; + case SINT64: + position = decodeVarint64(data, position, registers); + registers.object1 = CodedInputStream.decodeZigZag64(registers.long1); + break; + case STRING: + position = decodeStringRequireUtf8(data, position, registers); + break; + default: + throw new RuntimeException("unsupported field type."); + } + return position; + } + + /** Decodes a map entry. */ + private int decodeMapEntry( + byte[] data, + int position, + int limit, + MapEntryLite.Metadata metadata, + Map target, + Registers registers) + throws IOException { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0 || length > limit - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + final int end = position + length; + K key = metadata.defaultKey; + V value = metadata.defaultValue; + while (position < end) { + int tag = data[position++]; + if (tag < 0) { + position = decodeVarint32(tag, data, position, registers); + tag = registers.int1; + } + final int fieldNumber = tag >>> 3; + final int wireType = tag & 0x7; + switch (fieldNumber) { + case 1: + if (wireType == metadata.keyType.getWireType()) { + position = + decodeMapEntryValue(data, position, limit, metadata.keyType, null, registers); + key = (K) registers.object1; + continue; + } + break; + case 2: + if (wireType == metadata.valueType.getWireType()) { + position = + decodeMapEntryValue( + data, + position, + limit, + metadata.valueType, + metadata.defaultValue.getClass(), + registers); + value = (V) registers.object1; + continue; + } + break; + default: + break; + } + position = skipField(tag, data, position, limit, registers); + } + if (position != end) { + throw InvalidProtocolBufferException.parseFailure(); + } + target.put(key, value); + return end; + } + + @SuppressWarnings("ReferenceEquality") + private int parseRepeatedField( + T message, + byte[] data, + int position, + int limit, + int tag, + int number, + int wireType, + int bufferPosition, + long typeAndOffset, + int fieldType, + long fieldOffset, + Registers registers) + throws IOException { + ProtobufList list = (ProtobufList) UNSAFE.getObject(message, fieldOffset); + if (!list.isModifiable()) { + final int size = list.size(); + list = + list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + UNSAFE.putObject(message, fieldOffset, list); + } + switch (fieldType) { + case 18: // DOUBLE_LIST: + case 35: // DOUBLE_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedDoubleList(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_FIXED64) { + position = decodeDoubleList(tag, data, position, limit, list, registers); + } + break; + case 19: // FLOAT_LIST: + case 36: // FLOAT_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedFloatList(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_FIXED32) { + position = decodeFloatList(tag, data, position, limit, list, registers); + } + break; + case 20: // INT64_LIST: + case 21: // UINT64_LIST: + case 37: // INT64_LIST_PACKED: + case 38: // UINT64_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedVarint64List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64List(tag, data, position, limit, list, registers); + } + break; + case 22: // INT32_LIST: + case 29: // UINT32_LIST: + case 39: // INT32_LIST_PACKED: + case 43: // UINT32_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedVarint32List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32List(tag, data, position, limit, list, registers); + } + break; + case 23: // FIXED64_LIST: + case 32: // SFIXED64_LIST: + case 40: // FIXED64_LIST_PACKED: + case 46: // SFIXED64_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedFixed64List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_FIXED64) { + position = decodeFixed64List(tag, data, position, limit, list, registers); + } + break; + case 24: // FIXED32_LIST: + case 31: // SFIXED32_LIST: + case 41: // FIXED32_LIST_PACKED: + case 45: // SFIXED32_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedFixed32List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_FIXED32) { + position = decodeFixed32List(tag, data, position, limit, list, registers); + } + break; + case 25: // BOOL_LIST: + case 42: // BOOL_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedBoolList(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeBoolList(tag, data, position, limit, list, registers); + } + break; + case 26: // STRING_LIST: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + if ((typeAndOffset & ENFORCE_UTF8_MASK) == 0) { + position = decodeStringList(tag, data, position, limit, list, registers); + } else { + position = decodeStringListRequireUtf8(tag, data, position, limit, list, registers); + } + } + break; + case 27: // MESSAGE_LIST: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = + decodeMessageList( + getMessageFieldSchema(bufferPosition), + tag, + data, + position, + limit, + list, + registers); + } + break; + case 28: // BYTES_LIST: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeBytesList(tag, data, position, limit, list, registers); + } + break; + case 30: // ENUM_LIST: + case 44: // ENUM_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedVarint32List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32List(tag, data, position, limit, list, registers); + } else { + break; + } + UnknownFieldSetLite unknownFields = ((GeneratedMessageLite) message).unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + // filterUnknownEnumList() expects the unknownFields parameter to be mutable or null. + // Since we don't know yet whether there exist unknown enum values, we'd better pass + // null to it instead of allocating a mutable instance. This is also needed to be + // consistent with the behavior of generated parser/builder. + unknownFields = null; + } + unknownFields = + SchemaUtil.filterUnknownEnumList( + number, + (ProtobufList) list, + getEnumFieldVerifier(bufferPosition), + unknownFields, + (UnknownFieldSchema) unknownFieldSchema); + if (unknownFields != null) { + ((GeneratedMessageLite) message).unknownFields = unknownFields; + } + break; + case 33: // SINT32_LIST: + case 47: // SINT32_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedSInt32List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeSInt32List(tag, data, position, limit, list, registers); + } + break; + case 34: // SINT64_LIST: + case 48: // SINT64_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedSInt64List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeSInt64List(tag, data, position, limit, list, registers); + } + break; + case 49: // GROUP_LIST: + if (wireType == WireFormat.WIRETYPE_START_GROUP) { + position = + decodeGroupList( + getMessageFieldSchema(bufferPosition), + tag, + data, + position, + limit, + list, + registers); + } + break; + default: + break; + } + return position; + } + + private int parseMapField( + T message, + byte[] data, + int position, + int limit, + int bufferPosition, + long fieldOffset, + Registers registers) + throws IOException { + final sun.misc.Unsafe unsafe = UNSAFE; + Object mapDefaultEntry = getMapFieldDefaultEntry(bufferPosition); + Object mapField = unsafe.getObject(message, fieldOffset); + if (mapFieldSchema.isImmutable(mapField)) { + Object oldMapField = mapField; + mapField = mapFieldSchema.newMapField(mapDefaultEntry); + mapFieldSchema.mergeFrom(mapField, oldMapField); + unsafe.putObject(message, fieldOffset, mapField); + } + return decodeMapEntry( + data, + position, + limit, + (Metadata) mapFieldSchema.forMapMetadata(mapDefaultEntry), + (Map) mapFieldSchema.forMutableMapData(mapField), + registers); + } + + private int parseOneofField( + T message, + byte[] data, + int position, + int limit, + int tag, + int number, + int wireType, + int typeAndOffset, + int fieldType, + long fieldOffset, + int bufferPosition, + Registers registers) + throws IOException { + final sun.misc.Unsafe unsafe = UNSAFE; + final long oneofCaseOffset = buffer[bufferPosition + 2] & OFFSET_MASK; + switch (fieldType) { + case 51: // ONEOF_DOUBLE: + if (wireType == WireFormat.WIRETYPE_FIXED64) { + unsafe.putObject(message, fieldOffset, decodeDouble(data, position)); + position += 8; + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 52: // ONEOF_FLOAT: + if (wireType == WireFormat.WIRETYPE_FIXED32) { + unsafe.putObject(message, fieldOffset, decodeFloat(data, position)); + position += 4; + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 53: // ONEOF_INT64: + case 54: // ONEOF_UINT64: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.long1); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 55: // ONEOF_INT32: + case 62: // ONEOF_UINT32: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.int1); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 56: // ONEOF_FIXED64: + case 65: // ONEOF_SFIXED64: + if (wireType == WireFormat.WIRETYPE_FIXED64) { + unsafe.putObject(message, fieldOffset, decodeFixed64(data, position)); + position += 8; + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 57: // ONEOF_FIXED32: + case 64: // ONEOF_SFIXED32: + if (wireType == WireFormat.WIRETYPE_FIXED32) { + unsafe.putObject(message, fieldOffset, decodeFixed32(data, position)); + position += 4; + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 58: // ONEOF_BOOL: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.long1 != 0); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 59: // ONEOF_STRING: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length == 0) { + unsafe.putObject(message, fieldOffset, ""); + } else { + if ((typeAndOffset & ENFORCE_UTF8_MASK) != 0 + && !Utf8.isValidUtf8(data, position, position + length)) { + throw InvalidProtocolBufferException.invalidUtf8(); + } + final String value = new String(data, position, length, Internal.UTF_8); + unsafe.putObject(message, fieldOffset, value); + position += length; + } + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 60: // ONEOF_MESSAGE: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = + decodeMessageField( + getMessageFieldSchema(bufferPosition), data, position, limit, registers); + final Object oldValue = + unsafe.getInt(message, oneofCaseOffset) == number + ? unsafe.getObject(message, fieldOffset) + : null; + if (oldValue == null) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, fieldOffset, Internal.mergeMessage(oldValue, registers.object1)); + } + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 61: // ONEOF_BYTES: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeBytes(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.object1); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 63: // ONEOF_ENUM: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + final int enumValue = registers.int1; + EnumVerifier enumVerifier = getEnumFieldVerifier(bufferPosition); + if (enumVerifier == null || enumVerifier.isInRange(enumValue)) { + unsafe.putObject(message, fieldOffset, enumValue); + unsafe.putInt(message, oneofCaseOffset, number); + } else { + // UnknownFieldSetLite requires varint to be represented as Long. + getMutableUnknownFields(message).storeField(tag, (long) enumValue); + } + } + break; + case 66: // ONEOF_SINT32: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putObject(message, fieldOffset, CodedInputStream.decodeZigZag32(registers.int1)); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 67: // ONEOF_SINT64: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putObject(message, fieldOffset, CodedInputStream.decodeZigZag64(registers.long1)); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 68: // ONEOF_GROUP: + if (wireType == WireFormat.WIRETYPE_START_GROUP) { + final int endTag = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; + position = + decodeGroupField( + getMessageFieldSchema(bufferPosition), data, position, limit, endTag, registers); + final Object oldValue = + unsafe.getInt(message, oneofCaseOffset) == number + ? unsafe.getObject(message, fieldOffset) + : null; + if (oldValue == null) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, fieldOffset, Internal.mergeMessage(oldValue, registers.object1)); + } + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + default: + break; + } + return position; + } + + private Schema getMessageFieldSchema(int pos) { + final int index = pos / INTS_PER_FIELD * 2; + Schema schema = (Schema) objects[index]; + if (schema != null) { + return schema; + } + schema = Protobuf.getInstance().schemaFor((Class) objects[index + 1]); + objects[index] = schema; + return schema; + } + + private Object getMapFieldDefaultEntry(int pos) { + return objects[pos / INTS_PER_FIELD * 2]; + } + + private EnumVerifier getEnumFieldVerifier(int pos) { + return (EnumVerifier) objects[pos / INTS_PER_FIELD * 2 + 1]; + } + + /** + * Parses a proto2 message or group and returns the position after the message/group. If it's + * parsing a message (endGroup == 0), returns limit if parsing is successful; It it's parsing a + * group (endGroup != 0), parsing ends when a tag == endGroup is encountered and the position + * after that tag is returned. + */ + int parseProto2Message( + T message, byte[] data, int position, int limit, int endGroup, Registers registers) + throws IOException { + final sun.misc.Unsafe unsafe = UNSAFE; + int currentPresenceFieldOffset = -1; + int currentPresenceField = 0; + int tag = 0; + int oldNumber = -1; + int pos = 0; + while (position < limit) { + tag = data[position++]; + if (tag < 0) { + position = decodeVarint32(tag, data, position, registers); + tag = registers.int1; + } + final int number = tag >>> 3; + final int wireType = tag & 0x7; + if (number > oldNumber) { + pos = positionForFieldNumber(number, pos / INTS_PER_FIELD); + } else { + pos = positionForFieldNumber(number); + } + oldNumber = number; + if (pos == -1) { + // need to reset + pos = 0; + } else { + final int typeAndOffset = buffer[pos + 1]; + final int fieldType = type(typeAndOffset); + final long fieldOffset = offset(typeAndOffset); + if (fieldType <= 17) { + // Proto2 optional fields have has-bits. + final int presenceMaskAndOffset = buffer[pos + 2]; + final int presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + final int presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + // We cache the 32-bit has-bits integer value and only write it back when parsing a field + // using a different has-bits integer. + if (presenceFieldOffset != currentPresenceFieldOffset) { + if (currentPresenceFieldOffset != -1) { + unsafe.putInt(message, (long) currentPresenceFieldOffset, currentPresenceField); + } + currentPresenceFieldOffset = presenceFieldOffset; + currentPresenceField = unsafe.getInt(message, (long) presenceFieldOffset); + } + switch (fieldType) { + case 0: // DOUBLE + if (wireType == WireFormat.WIRETYPE_FIXED64) { + UnsafeUtil.putDouble(message, fieldOffset, decodeDouble(data, position)); + position += 8; + currentPresenceField |= presenceMask; + continue; + } + break; + case 1: // FLOAT + if (wireType == WireFormat.WIRETYPE_FIXED32) { + UnsafeUtil.putFloat(message, fieldOffset, decodeFloat(data, position)); + position += 4; + currentPresenceField |= presenceMask; + continue; + } + break; + case 2: // INT64 + case 3: // UINT64 + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putLong(message, fieldOffset, registers.long1); + currentPresenceField |= presenceMask; + continue; + } + break; + case 4: // INT32 + case 11: // UINT32 + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt(message, fieldOffset, registers.int1); + currentPresenceField |= presenceMask; + continue; + } + break; + case 5: // FIXED64 + case 14: // SFIXED64 + if (wireType == WireFormat.WIRETYPE_FIXED64) { + unsafe.putLong(message, fieldOffset, decodeFixed64(data, position)); + position += 8; + currentPresenceField |= presenceMask; + continue; + } + break; + case 6: // FIXED32 + case 13: // SFIXED32 + if (wireType == WireFormat.WIRETYPE_FIXED32) { + unsafe.putInt(message, fieldOffset, decodeFixed32(data, position)); + position += 4; + currentPresenceField |= presenceMask; + continue; + } + break; + case 7: // BOOL + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + UnsafeUtil.putBoolean(message, fieldOffset, registers.long1 != 0); + currentPresenceField |= presenceMask; + continue; + } + break; + case 8: // STRING + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + if ((typeAndOffset & ENFORCE_UTF8_MASK) == 0) { + position = decodeString(data, position, registers); + } else { + position = decodeStringRequireUtf8(data, position, registers); + } + unsafe.putObject(message, fieldOffset, registers.object1); + currentPresenceField |= presenceMask; + continue; + } + break; + case 9: // MESSAGE + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = + decodeMessageField( + getMessageFieldSchema(pos), data, position, limit, registers); + if ((currentPresenceField & presenceMask) == 0) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, + fieldOffset, + Internal.mergeMessage( + unsafe.getObject(message, fieldOffset), registers.object1)); + } + currentPresenceField |= presenceMask; + continue; + } + break; + case 10: // BYTES + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeBytes(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.object1); + currentPresenceField |= presenceMask; + continue; + } + break; + case 12: // ENUM + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + final int enumValue = registers.int1; + EnumVerifier enumVerifier = getEnumFieldVerifier(pos); + if (enumVerifier == null || enumVerifier.isInRange(enumValue)) { + unsafe.putInt(message, fieldOffset, enumValue); + currentPresenceField |= presenceMask; + } else { + // UnknownFieldSetLite requires varint to be represented as Long. + getMutableUnknownFields(message).storeField(tag, (long) enumValue); + } + continue; + } + break; + case 15: // SINT32 + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt( + message, fieldOffset, CodedInputStream.decodeZigZag32(registers.int1)); + currentPresenceField |= presenceMask; + continue; + } + break; + case 16: // SINT64 + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putLong( + message, fieldOffset, CodedInputStream.decodeZigZag64(registers.long1)); + + currentPresenceField |= presenceMask; + continue; + } + break; + case 17: // GROUP + if (wireType == WireFormat.WIRETYPE_START_GROUP) { + final int endTag = (number << 3) | WireFormat.WIRETYPE_END_GROUP; + position = + decodeGroupField( + getMessageFieldSchema(pos), data, position, limit, endTag, registers); + if ((currentPresenceField & presenceMask) == 0) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, + fieldOffset, + Internal.mergeMessage( + unsafe.getObject(message, fieldOffset), registers.object1)); + } + + currentPresenceField |= presenceMask; + continue; + } + break; + default: + break; + } + } else if (fieldType == 27) { + // Handle repeated message fields. + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + ProtobufList list = (ProtobufList) unsafe.getObject(message, fieldOffset); + if (!list.isModifiable()) { + final int size = list.size(); + list = + list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + unsafe.putObject(message, fieldOffset, list); + } + position = + decodeMessageList( + getMessageFieldSchema(pos), tag, data, position, limit, list, registers); + continue; + } + } else if (fieldType <= 49) { + // Handle all other repeated fields. + final int oldPosition = position; + position = + parseRepeatedField( + message, + data, + position, + limit, + tag, + number, + wireType, + pos, + typeAndOffset, + fieldType, + fieldOffset, + registers); + if (position != oldPosition) { + continue; + } + } else if (fieldType == 50) { + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + final int oldPosition = position; + position = parseMapField(message, data, position, limit, pos, fieldOffset, registers); + if (position != oldPosition) { + continue; + } + } + } else { + final int oldPosition = position; + position = + parseOneofField( + message, + data, + position, + limit, + tag, + number, + wireType, + typeAndOffset, + fieldType, + fieldOffset, + pos, + registers); + if (position != oldPosition) { + continue; + } + } + } + if (tag == endGroup && endGroup != 0) { + break; + } + + if (hasExtensions + && registers.extensionRegistry != ExtensionRegistryLite.getEmptyRegistry()) { + position = decodeExtensionOrUnknownField( + tag, data, position, limit, message, defaultInstance, + (UnknownFieldSchema) unknownFieldSchema, + registers); + } else { + position = decodeUnknownField( + tag, data, position, limit, getMutableUnknownFields(message), registers); + } + } + if (currentPresenceFieldOffset != -1) { + unsafe.putInt(message, (long) currentPresenceFieldOffset, currentPresenceField); + } + UnknownFieldSetLite unknownFields = null; + for (int i = checkInitializedCount; i < repeatedFieldOffsetStart; i++) { + unknownFields = + filterMapUnknownEnumValues( + message, + intArray[i], + unknownFields, + (UnknownFieldSchema) unknownFieldSchema); + } + if (unknownFields != null) { + ((UnknownFieldSchema) unknownFieldSchema) + .setBuilderToMessage(message, unknownFields); + } + if (endGroup == 0) { + if (position != limit) { + throw InvalidProtocolBufferException.parseFailure(); + } + } else { + if (position > limit || tag != endGroup) { + throw InvalidProtocolBufferException.parseFailure(); + } + } + return position; + } + + /** Parses a proto3 message and returns the limit if parsing is successful. */ + private int parseProto3Message( + T message, byte[] data, int position, int limit, Registers registers) throws IOException { + final sun.misc.Unsafe unsafe = UNSAFE; + int tag = 0; + int oldNumber = -1; + int pos = 0; + while (position < limit) { + tag = data[position++]; + if (tag < 0) { + position = decodeVarint32(tag, data, position, registers); + tag = registers.int1; + } + final int number = tag >>> 3; + final int wireType = tag & 0x7; + if (number > oldNumber) { + pos = positionForFieldNumber(number, pos / INTS_PER_FIELD); + } else { + pos = positionForFieldNumber(number); + } + oldNumber = number; + if (pos == -1) { + // need to reset + pos = 0; + } else { + final int typeAndOffset = buffer[pos + 1]; + final int fieldType = type(typeAndOffset); + final long fieldOffset = offset(typeAndOffset); + if (fieldType <= 17) { + switch (fieldType) { + case 0: // DOUBLE: + if (wireType == WireFormat.WIRETYPE_FIXED64) { + UnsafeUtil.putDouble(message, fieldOffset, decodeDouble(data, position)); + position += 8; + continue; + } + break; + case 1: // FLOAT: + if (wireType == WireFormat.WIRETYPE_FIXED32) { + UnsafeUtil.putFloat(message, fieldOffset, decodeFloat(data, position)); + position += 4; + continue; + } + break; + case 2: // INT64: + case 3: // UINT64: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putLong(message, fieldOffset, registers.long1); + continue; + } + break; + case 4: // INT32: + case 11: // UINT32: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt(message, fieldOffset, registers.int1); + continue; + } + break; + case 5: // FIXED64: + case 14: // SFIXED64: + if (wireType == WireFormat.WIRETYPE_FIXED64) { + unsafe.putLong(message, fieldOffset, decodeFixed64(data, position)); + position += 8; + continue; + } + break; + case 6: // FIXED32: + case 13: // SFIXED32: + if (wireType == WireFormat.WIRETYPE_FIXED32) { + unsafe.putInt(message, fieldOffset, decodeFixed32(data, position)); + position += 4; + continue; + } + break; + case 7: // BOOL: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + UnsafeUtil.putBoolean(message, fieldOffset, registers.long1 != 0); + continue; + } + break; + case 8: // STRING: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + if ((typeAndOffset & ENFORCE_UTF8_MASK) == 0) { + position = decodeString(data, position, registers); + } else { + position = decodeStringRequireUtf8(data, position, registers); + } + unsafe.putObject(message, fieldOffset, registers.object1); + continue; + } + break; + case 9: // MESSAGE: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = + decodeMessageField( + getMessageFieldSchema(pos), data, position, limit, registers); + final Object oldValue = unsafe.getObject(message, fieldOffset); + if (oldValue == null) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, fieldOffset, Internal.mergeMessage(oldValue, registers.object1)); + } + continue; + } + break; + case 10: // BYTES: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeBytes(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.object1); + continue; + } + break; + case 12: // ENUM: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt(message, fieldOffset, registers.int1); + continue; + } + break; + case 15: // SINT32: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt( + message, fieldOffset, CodedInputStream.decodeZigZag32(registers.int1)); + continue; + } + break; + case 16: // SINT64: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putLong( + message, fieldOffset, CodedInputStream.decodeZigZag64(registers.long1)); + continue; + } + break; + default: + break; + } + } else if (fieldType == 27) { + // Handle repeated message field. + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + ProtobufList list = (ProtobufList) unsafe.getObject(message, fieldOffset); + if (!list.isModifiable()) { + final int size = list.size(); + list = + list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + unsafe.putObject(message, fieldOffset, list); + } + position = + decodeMessageList( + getMessageFieldSchema(pos), tag, data, position, limit, list, registers); + continue; + } + } else if (fieldType <= 49) { + // Handle all other repeated fields. + final int oldPosition = position; + position = + parseRepeatedField( + message, + data, + position, + limit, + tag, + number, + wireType, + pos, + typeAndOffset, + fieldType, + fieldOffset, + registers); + if (position != oldPosition) { + continue; + } + } else if (fieldType == 50) { + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + final int oldPosition = position; + position = parseMapField(message, data, position, limit, pos, fieldOffset, registers); + if (position != oldPosition) { + continue; + } + } + } else { + final int oldPosition = position; + position = + parseOneofField( + message, + data, + position, + limit, + tag, + number, + wireType, + typeAndOffset, + fieldType, + fieldOffset, + pos, + registers); + if (position != oldPosition) { + continue; + } + } + } + position = decodeUnknownField( + tag, data, position, limit, getMutableUnknownFields(message), registers); + } + if (position != limit) { + throw InvalidProtocolBufferException.parseFailure(); + } + return position; + } + + @Override + public void mergeFrom(T message, byte[] data, int position, int limit, Registers registers) + throws IOException { + if (proto3) { + parseProto3Message(message, data, position, limit, registers); + } else { + parseProto2Message(message, data, position, limit, 0, registers); + } + } + + @Override + public void makeImmutable(T message) { + // Make all repeated/map fields immutable. + for (int i = checkInitializedCount; i < repeatedFieldOffsetStart; i++) { + long offset = offset(typeAndOffsetAt(intArray[i])); + Object mapField = UnsafeUtil.getObject(message, offset); + if (mapField == null) { + continue; + } + UnsafeUtil.putObject(message, offset, mapFieldSchema.toImmutable(mapField)); + } + final int length = intArray.length; + for (int i = repeatedFieldOffsetStart; i < length; i++) { + listFieldSchema.makeImmutableListAt(message, intArray[i]); + } + unknownFieldSchema.makeImmutable(message); + if (hasExtensions) { + extensionSchema.makeImmutable(message); + } + } + + @SuppressWarnings("unchecked") + private final void mergeMap( + Object message, + int pos, + Object mapDefaultEntry, + ExtensionRegistryLite extensionRegistry, + Reader reader) + throws IOException { + long offset = offset(typeAndOffsetAt(pos)); + Object mapField = UnsafeUtil.getObject(message, offset); + // TODO(xiaofeng): Consider creating separate implementations for full and lite. In lite + // runtime map field will never be null but here we still need to check null because the + // code is shared by both full and lite. It might be better if full/lite uses different + // schema implementations. + if (mapField == null) { + mapField = mapFieldSchema.newMapField(mapDefaultEntry); + UnsafeUtil.putObject(message, offset, mapField); + } else if (mapFieldSchema.isImmutable(mapField)) { + Object oldMapField = mapField; + mapField = mapFieldSchema.newMapField(mapDefaultEntry); + mapFieldSchema.mergeFrom(mapField, oldMapField); + UnsafeUtil.putObject(message, offset, mapField); + } + reader.readMap( + (Map) mapFieldSchema.forMutableMapData(mapField), + (Metadata) mapFieldSchema.forMapMetadata(mapDefaultEntry), + extensionRegistry); + } + + private final UB filterMapUnknownEnumValues( + Object message, int pos, UB unknownFields, UnknownFieldSchema unknownFieldSchema) { + int fieldNumber = numberAt(pos); + long offset = offset(typeAndOffsetAt(pos)); + Object mapField = UnsafeUtil.getObject(message, offset); + if (mapField == null) { + return unknownFields; + } + EnumVerifier enumVerifier = getEnumFieldVerifier(pos); + if (enumVerifier == null) { + return unknownFields; + } + Map mapData = mapFieldSchema.forMutableMapData(mapField); + // Filter unknown enum values. + unknownFields = + filterUnknownEnumMap( + pos, fieldNumber, mapData, enumVerifier, unknownFields, unknownFieldSchema); + return unknownFields; + } + + @SuppressWarnings("unchecked") + private final UB filterUnknownEnumMap( + int pos, + int number, + Map mapData, + EnumVerifier enumVerifier, + UB unknownFields, + UnknownFieldSchema unknownFieldSchema) { + Metadata metadata = + (Metadata) mapFieldSchema.forMapMetadata(getMapFieldDefaultEntry(pos)); + for (Iterator> it = mapData.entrySet().iterator(); it.hasNext(); ) { + Map.Entry entry = it.next(); + if (!enumVerifier.isInRange((Integer) entry.getValue())) { + if (unknownFields == null) { + unknownFields = unknownFieldSchema.newBuilder(); + } + int entrySize = + MapEntryLite.computeSerializedSize(metadata, entry.getKey(), entry.getValue()); + CodedBuilder codedBuilder = ByteString.newCodedBuilder(entrySize); + CodedOutputStream codedOutput = codedBuilder.getCodedOutput(); + try { + MapEntryLite.writeTo(codedOutput, metadata, entry.getKey(), entry.getValue()); + } catch (IOException e) { + // Writing to ByteString CodedOutputStream should not throw IOException. + throw new RuntimeException(e); + } + unknownFieldSchema.addLengthDelimited(unknownFields, number, codedBuilder.build()); + it.remove(); + } + } + return unknownFields; + } + + @Override + public final boolean isInitialized(T message) { + int currentPresenceFieldOffset = -1; + int currentPresenceField = 0; + for (int i = 0; i < checkInitializedCount; i++) { + final int pos = intArray[i]; + final int number = numberAt(pos); + + final int typeAndOffset = typeAndOffsetAt(pos); + + int presenceMaskAndOffset = 0; + int presenceMask = 0; + if (!proto3) { + presenceMaskAndOffset = buffer[pos + 2]; + final int presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + if (presenceFieldOffset != currentPresenceFieldOffset) { + currentPresenceFieldOffset = presenceFieldOffset; + currentPresenceField = UNSAFE.getInt(message, (long) presenceFieldOffset); + } + } + + if (isRequired(typeAndOffset)) { + if (!isFieldPresent(message, pos, currentPresenceField, presenceMask)) { + return false; + } + // If a required message field is set but has no required fields of it's own, we still + // proceed and check the message is initialized. It should be fairly cheap to check these + // messages but is worth documenting. + } + // Check nested message and groups. + switch (type(typeAndOffset)) { + case 9: // MESSAGE + case 17: // GROUP + if (isFieldPresent(message, pos, currentPresenceField, presenceMask) + && !isInitialized(message, typeAndOffset, getMessageFieldSchema(pos))) { + return false; + } + break; + case 27: // MESSAGE_LIST + case 49: // GROUP_LIST + if (!isListInitialized(message, typeAndOffset, pos)) { + return false; + } + break; + case 60: // ONEOF_MESSAGE + case 68: // ONEOF_GROUP + if (isOneofPresent(message, number, pos) + && !isInitialized(message, typeAndOffset, getMessageFieldSchema(pos))) { + return false; + } + break; + case 50: // MAP + if (!isMapInitialized(message, typeAndOffset, pos)) { + return false; + } + break; + default: + break; + } + } + + if (hasExtensions) { + if (!extensionSchema.getExtensions(message).isInitialized()) { + return false; + } + } + + return true; + } + + private static boolean isInitialized(Object message, int typeAndOffset, Schema schema) { + Object nested = UnsafeUtil.getObject(message, offset(typeAndOffset)); + return schema.isInitialized(nested); + } + + private boolean isListInitialized(Object message, int typeAndOffset, int pos) { + @SuppressWarnings("unchecked") + List list = (List) UnsafeUtil.getObject(message, offset(typeAndOffset)); + if (list.isEmpty()) { + return true; + } + + Schema schema = getMessageFieldSchema(pos); + for (int i = 0; i < list.size(); i++) { + N nested = list.get(i); + if (!schema.isInitialized(nested)) { + return false; + } + } + return true; + } + + private boolean isMapInitialized(T message, int typeAndOffset, int pos) { + Map map = mapFieldSchema.forMapData(UnsafeUtil.getObject(message, offset(typeAndOffset))); + if (map.isEmpty()) { + return true; + } + Object mapDefaultEntry = getMapFieldDefaultEntry(pos); + MapEntryLite.Metadata metadata = mapFieldSchema.forMapMetadata(mapDefaultEntry); + if (metadata.valueType.getJavaType() != WireFormat.JavaType.MESSAGE) { + return true; + } + // TODO(dweis): Use schema cache. + Schema schema = null; + for (Object nested : map.values()) { + if (schema == null) { + schema = Protobuf.getInstance().schemaFor(nested.getClass()); + } + if (!schema.isInitialized(nested)) { + return false; + } + } + return true; + } + + private void writeString(int fieldNumber, Object value, Writer writer) throws IOException { + if (value instanceof String) { + writer.writeString(fieldNumber, (String) value); + } else { + writer.writeBytes(fieldNumber, (ByteString) value); + } + } + + private void readString(Object message, int typeAndOffset, Reader reader) throws IOException { + if (isEnforceUtf8(typeAndOffset)) { + // Enforce valid UTF-8 on the read. + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readStringRequireUtf8()); + } else if (lite) { + // Lite messages use String fields to store strings. Read a string but do not + // enforce UTF-8 + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readString()); + } else { + // Full runtime messages use Objects to store either a String or ByteString. Read + // the string as a ByteString and do not enforce UTF-8. + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readBytes()); + } + } + + private void readStringList(Object message, int typeAndOffset, Reader reader) throws IOException { + if (isEnforceUtf8(typeAndOffset)) { + reader.readStringListRequireUtf8( + listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + } else { + reader.readStringList(listFieldSchema.mutableListAt(message, offset(typeAndOffset))); + } + } + + private void readMessageList( + Object message, + int typeAndOffset, + Reader reader, + Schema schema, + ExtensionRegistryLite extensionRegistry) + throws IOException { + long offset = offset(typeAndOffset); + reader.readMessageList( + listFieldSchema.mutableListAt(message, offset), schema, extensionRegistry); + } + + private void readGroupList( + Object message, + long offset, + Reader reader, + Schema schema, + ExtensionRegistryLite extensionRegistry) + throws IOException { + reader.readGroupList( + listFieldSchema.mutableListAt(message, offset), schema, extensionRegistry); + } + + private int numberAt(int pos) { + return buffer[pos]; + } + + private int typeAndOffsetAt(int pos) { + return buffer[pos + 1]; + } + + private int presenceMaskAndOffsetAt(int pos) { + return buffer[pos + 2]; + } + + private static int type(int value) { + return (value & FIELD_TYPE_MASK) >>> OFFSET_BITS; + } + + private static boolean isRequired(int value) { + return (value & REQUIRED_MASK) != 0; + } + + private static boolean isEnforceUtf8(int value) { + return (value & ENFORCE_UTF8_MASK) != 0; + } + + private static long offset(int value) { + return value & OFFSET_MASK; + } + + private static double doubleAt(T message, long offset) { + return UnsafeUtil.getDouble(message, offset); + } + + private static float floatAt(T message, long offset) { + return UnsafeUtil.getFloat(message, offset); + } + + private static int intAt(T message, long offset) { + return UnsafeUtil.getInt(message, offset); + } + + private static long longAt(T message, long offset) { + return UnsafeUtil.getLong(message, offset); + } + + private static boolean booleanAt(T message, long offset) { + return UnsafeUtil.getBoolean(message, offset); + } + + private static double oneofDoubleAt(T message, long offset) { + return ((Double) UnsafeUtil.getObject(message, offset)).doubleValue(); + } + + private static float oneofFloatAt(T message, long offset) { + return ((Float) UnsafeUtil.getObject(message, offset)).floatValue(); + } + + private static int oneofIntAt(T message, long offset) { + return ((Integer) UnsafeUtil.getObject(message, offset)).intValue(); + } + + private static long oneofLongAt(T message, long offset) { + return ((Long) UnsafeUtil.getObject(message, offset)).longValue(); + } + + private static boolean oneofBooleanAt(T message, long offset) { + return ((Boolean) UnsafeUtil.getObject(message, offset)).booleanValue(); + } + + /** Returns true the field is present in both messages, or neither. */ + private boolean arePresentForEquals(T message, T other, int pos) { + return isFieldPresent(message, pos) == isFieldPresent(other, pos); + } + + private boolean isFieldPresent(T message, int pos, int presenceField, int presenceMask) { + if (proto3) { + return isFieldPresent(message, pos); + } else { + return (presenceField & presenceMask) != 0; + } + } + + private boolean isFieldPresent(T message, int pos) { + if (proto3) { + final int typeAndOffset = typeAndOffsetAt(pos); + final long offset = offset(typeAndOffset); + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + return UnsafeUtil.getDouble(message, offset) != 0D; + case 1: // FLOAT: + return UnsafeUtil.getFloat(message, offset) != 0F; + case 2: // INT64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 3: // UINT64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 4: // INT32: + return UnsafeUtil.getInt(message, offset) != 0; + case 5: // FIXED64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 6: // FIXED32: + return UnsafeUtil.getInt(message, offset) != 0; + case 7: // BOOL: + return UnsafeUtil.getBoolean(message, offset); + case 8: // STRING: + Object value = UnsafeUtil.getObject(message, offset); + if (value instanceof String) { + return !((String) value).isEmpty(); + } else if (value instanceof ByteString) { + return !ByteString.EMPTY.equals(value); + } else { + throw new IllegalArgumentException(); + } + case 9: // MESSAGE: + return UnsafeUtil.getObject(message, offset) != null; + case 10: // BYTES: + return !ByteString.EMPTY.equals(UnsafeUtil.getObject(message, offset)); + case 11: // UINT32: + return UnsafeUtil.getInt(message, offset) != 0; + case 12: // ENUM: + return UnsafeUtil.getInt(message, offset) != 0; + case 13: // SFIXED32: + return UnsafeUtil.getInt(message, offset) != 0; + case 14: // SFIXED64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 15: // SINT32: + return UnsafeUtil.getInt(message, offset) != 0; + case 16: // SINT64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 17: // GROUP: + return UnsafeUtil.getObject(message, offset) != null; + default: + throw new IllegalArgumentException(); + } + } else { + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + final int presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + return (UnsafeUtil.getInt(message, presenceMaskAndOffset & OFFSET_MASK) & presenceMask) != 0; + } + } + + private void setFieldPresent(T message, int pos) { + if (proto3) { + // Proto3 doesn't have presence fields + return; + } + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + final int presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + final long presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + UnsafeUtil.putInt( + message, + presenceFieldOffset, + UnsafeUtil.getInt(message, presenceFieldOffset) | presenceMask); + } + + private boolean isOneofPresent(T message, int fieldNumber, int pos) { + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + return UnsafeUtil.getInt(message, presenceMaskAndOffset & OFFSET_MASK) == fieldNumber; + } + + private boolean isOneofCaseEqual(T message, T other, int pos) { + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + return UnsafeUtil.getInt(message, presenceMaskAndOffset & OFFSET_MASK) + == UnsafeUtil.getInt(other, presenceMaskAndOffset & OFFSET_MASK); + } + + private void setOneofPresent(T message, int fieldNumber, int pos) { + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + UnsafeUtil.putInt(message, presenceMaskAndOffset & OFFSET_MASK, fieldNumber); + } + + private int positionForFieldNumber(final int number) { + if (number >= minFieldNumber && number <= maxFieldNumber) { + return slowPositionForFieldNumber(number, 0); + } + return -1; + } + + private int positionForFieldNumber(final int number, final int min) { + if (number >= minFieldNumber && number <= maxFieldNumber) { + return slowPositionForFieldNumber(number, min); + } + return -1; + } + + private int slowPositionForFieldNumber(final int number, int min) { + int max = buffer.length / INTS_PER_FIELD - 1; + while (min <= max) { + // Find the midpoint address. + final int mid = (max + min) >>> 1; + final int pos = mid * INTS_PER_FIELD; + final int midFieldNumber = numberAt(pos); + if (number == midFieldNumber) { + // Found the field. + return pos; + } + if (number < midFieldNumber) { + // Search the lower half. + max = mid - 1; + } else { + // Search the upper half. + min = mid + 1; + } + } + return -1; + } + + int getSchemaSize() { + return buffer.length * 3; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MessageSetSchema.java b/java/core/src/main/java/com/google/protobuf/MessageSetSchema.java new file mode 100755 index 0000000000..187dc8b8a5 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MessageSetSchema.java @@ -0,0 +1,392 @@ +// 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. + +package com.google.protobuf; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map.Entry; + +/** Schema used for proto2 messages using message_set_wireformat. */ +final class MessageSetSchema implements Schema { + private final MessageLite defaultInstance; + private final UnknownFieldSchema unknownFieldSchema; + private final boolean hasExtensions; + private final ExtensionSchema extensionSchema; + + private MessageSetSchema( + UnknownFieldSchema unknownFieldSchema, + ExtensionSchema extensionSchema, + MessageLite defaultInstance) { + this.unknownFieldSchema = unknownFieldSchema; + this.hasExtensions = extensionSchema.hasExtensions(defaultInstance); + this.extensionSchema = extensionSchema; + this.defaultInstance = defaultInstance; + } + + static MessageSetSchema newSchema( + UnknownFieldSchema unknownFieldSchema, + ExtensionSchema extensionSchema, + MessageLite defaultInstance) { + return new MessageSetSchema(unknownFieldSchema, extensionSchema, defaultInstance); + } + + @SuppressWarnings("unchecked") + @Override + public T newInstance() { + return (T) defaultInstance.newBuilderForType().buildPartial(); + } + + @Override + public boolean equals(T message, T other) { + Object messageUnknown = unknownFieldSchema.getFromMessage(message); + Object otherUnknown = unknownFieldSchema.getFromMessage(other); + if (!messageUnknown.equals(otherUnknown)) { + return false; + } + if (hasExtensions) { + FieldSet messageExtensions = extensionSchema.getExtensions(message); + FieldSet otherExtensions = extensionSchema.getExtensions(other); + return messageExtensions.equals(otherExtensions); + } + return true; + } + + @Override + public int hashCode(T message) { + int hashCode = unknownFieldSchema.getFromMessage(message).hashCode(); + if (hasExtensions) { + FieldSet extensions = extensionSchema.getExtensions(message); + hashCode = (hashCode * 53) + extensions.hashCode(); + } + return hashCode; + } + + @Override + public void mergeFrom(T message, T other) { + SchemaUtil.mergeUnknownFields(unknownFieldSchema, message, other); + if (hasExtensions) { + SchemaUtil.mergeExtensions(extensionSchema, message, other); + } + } + + @SuppressWarnings("unchecked") + @Override + public void writeTo(T message, Writer writer) throws IOException { + FieldSet extensions = extensionSchema.getExtensions(message); + Iterator iterator = extensions.iterator(); + while (iterator.hasNext()) { + Entry extension = (Entry) iterator.next(); + FieldSet.FieldDescriptorLite fd = (FieldSet.FieldDescriptorLite) extension.getKey(); + if (fd.getLiteJavaType() != WireFormat.JavaType.MESSAGE || fd.isRepeated() || fd.isPacked()) { + throw new IllegalStateException("Found invalid MessageSet item."); + } + if (extension instanceof LazyField.LazyEntry) { + writer.writeMessageSetItem( + fd.getNumber(), ((LazyField.LazyEntry) extension).getField().toByteString()); + } else { + writer.writeMessageSetItem(fd.getNumber(), extension.getValue()); + } + } + writeUnknownFieldsHelper(unknownFieldSchema, message, writer); + } + + /** + * A helper method for wildcard capture of {@code unknownFieldSchema}. See: + * https://docs.oracle.com/javase/tutorial/java/generics/capture.html + */ + private void writeUnknownFieldsHelper( + UnknownFieldSchema unknownFieldSchema, T message, Writer writer) throws IOException { + unknownFieldSchema.writeAsMessageSetTo(unknownFieldSchema.getFromMessage(message), writer); + } + + @SuppressWarnings("ReferenceEquality") + @Override + public void mergeFrom( + T message, byte[] data, int position, int limit, ArrayDecoders.Registers registers) + throws IOException { + UnknownFieldSetLite unknownFields = ((GeneratedMessageLite) message).unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = UnknownFieldSetLite.newInstance(); + ((GeneratedMessageLite) message).unknownFields = unknownFields; + } + final FieldSet extensions = + ((GeneratedMessageLite.ExtendableMessage) message).ensureExtensionsAreMutable(); + GeneratedMessageLite.GeneratedExtension extension = null; + while (position < limit) { + position = ArrayDecoders.decodeVarint32(data, position, registers); + final int startTag = registers.int1; + if (startTag != WireFormat.MESSAGE_SET_ITEM_TAG) { + if (WireFormat.getTagWireType(startTag) == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + extension = + (GeneratedMessageLite.GeneratedExtension) extensionSchema.findExtensionByNumber( + registers.extensionRegistry, defaultInstance, + WireFormat.getTagFieldNumber(startTag)); + if (extension != null) { + position = + ArrayDecoders.decodeMessageField( + Protobuf.getInstance().schemaFor( + extension.getMessageDefaultInstance().getClass()), + data, position, limit, registers); + extensions.setField(extension.descriptor, registers.object1); + } else { + position = + ArrayDecoders.decodeUnknownField( + startTag, data, position, limit, unknownFields, registers); + } + } else { + position = ArrayDecoders.skipField(startTag, data, position, limit, registers); + } + continue; + } + + int typeId = 0; + ByteString rawBytes = null; + + while (position < limit) { + position = ArrayDecoders.decodeVarint32(data, position, registers); + final int tag = registers.int1; + final int number = WireFormat.getTagFieldNumber(tag); + final int wireType = WireFormat.getTagWireType(tag); + switch (number) { + case WireFormat.MESSAGE_SET_TYPE_ID: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = ArrayDecoders.decodeVarint32(data, position, registers); + typeId = registers.int1; + extension = + (GeneratedMessageLite.GeneratedExtension) extensionSchema + .findExtensionByNumber(registers.extensionRegistry, defaultInstance, typeId); + continue; + } + break; + case WireFormat.MESSAGE_SET_MESSAGE: + if (extension != null) { + position = ArrayDecoders.decodeMessageField( + Protobuf.getInstance().schemaFor( + extension.getMessageDefaultInstance().getClass()), + data, position, limit, registers); + extensions.setField(extension.descriptor, registers.object1); + continue; + } else { + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = ArrayDecoders.decodeBytes(data, position, registers); + rawBytes = (ByteString) registers.object1; + continue; + } + break; + } + default: + break; + } + if (tag == WireFormat.MESSAGE_SET_ITEM_END_TAG) { + break; + } + position = ArrayDecoders.skipField(tag, data, position, limit, registers); + } + + if (rawBytes != null) { + unknownFields.storeField( + WireFormat.makeTag(typeId, WireFormat.WIRETYPE_LENGTH_DELIMITED), rawBytes); + } + } + if (position != limit) { + throw InvalidProtocolBufferException.parseFailure(); + } + } + + @Override + public void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) + throws IOException { + mergeFromHelper(unknownFieldSchema, extensionSchema, message, reader, extensionRegistry); + } + + /** + * A helper method for wildcard capture of {@code unknownFieldSchema}. See: + * https://docs.oracle.com/javase/tutorial/java/generics/capture.html + */ + @SuppressWarnings("unchecked") + private > void mergeFromHelper( + UnknownFieldSchema unknownFieldSchema, + ExtensionSchema extensionSchema, + T message, + Reader reader, + ExtensionRegistryLite extensionRegistry) + throws IOException { + UB unknownFields = unknownFieldSchema.getBuilderFromMessage(message); + FieldSet extensions = extensionSchema.getMutableExtensions(message); + try { + while (true) { + final int number = reader.getFieldNumber(); + if (number == Reader.READ_DONE) { + return; + } + if (parseMessageSetItemOrUnknownField( + reader, + extensionRegistry, + extensionSchema, + extensions, + unknownFieldSchema, + unknownFields)) { + continue; + } + // Done reading. + return; + } + } finally { + unknownFieldSchema.setBuilderToMessage(message, unknownFields); + } + } + + @Override + public void makeImmutable(T message) { + unknownFieldSchema.makeImmutable(message); + extensionSchema.makeImmutable(message); + } + + private > + boolean parseMessageSetItemOrUnknownField( + Reader reader, + ExtensionRegistryLite extensionRegistry, + ExtensionSchema extensionSchema, + FieldSet extensions, + UnknownFieldSchema unknownFieldSchema, + UB unknownFields) + throws IOException { + int startTag = reader.getTag(); + if (startTag != WireFormat.MESSAGE_SET_ITEM_TAG) { + if (WireFormat.getTagWireType(startTag) == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + Object extension = + extensionSchema.findExtensionByNumber( + extensionRegistry, defaultInstance, WireFormat.getTagFieldNumber(startTag)); + if (extension != null) { + extensionSchema.parseLengthPrefixedMessageSetItem( + reader, extension, extensionRegistry, extensions); + return true; + } else { + return unknownFieldSchema.mergeOneFieldFrom(unknownFields, reader); + } + } else { + return reader.skipField(); + } + } + + // The wire format for MessageSet is: + // message MessageSet { + // repeated group Item = 1 { + // required int32 typeId = 2; + // required bytes message = 3; + // } + // } + // "typeId" is the extension's field number. The extension can only be + // a message type, where "message" contains the encoded bytes of that + // message. + // + // In practice, we will probably never see a MessageSet item in which + // the message appears before the type ID, or where either field does not + // appear exactly once. However, in theory such cases are valid, so we + // should be prepared to accept them. + + int typeId = 0; + ByteString rawBytes = null; // If we encounter "message" before "typeId" + Object extension = null; + + // Read bytes from input, if we get it's type first then parse it eagerly, + // otherwise we store the raw bytes in a local variable. + loop: + while (true) { + final int number = reader.getFieldNumber(); + if (number == Reader.READ_DONE) { + break; + } + + final int tag = reader.getTag(); + if (tag == WireFormat.MESSAGE_SET_TYPE_ID_TAG) { + typeId = reader.readUInt32(); + extension = + extensionSchema.findExtensionByNumber(extensionRegistry, defaultInstance, typeId); + continue; + } else if (tag == WireFormat.MESSAGE_SET_MESSAGE_TAG) { + if (extension != null) { + extensionSchema.parseLengthPrefixedMessageSetItem( + reader, extension, extensionRegistry, extensions); + continue; + } + // We haven't seen a type ID yet or we want parse message lazily. + rawBytes = reader.readBytes(); + continue; + } else { + if (!reader.skipField()) { + break loop; // End of group + } + } + } + + if (reader.getTag() != WireFormat.MESSAGE_SET_ITEM_END_TAG) { + throw InvalidProtocolBufferException.invalidEndTag(); + } + + // If there are any rawBytes left, it means the message content appears before the type ID. + if (rawBytes != null) { + if (extension != null) { // We known the type + // TODO(xiaofeng): Instead of reading into a temporary ByteString, maybe there is a way + // to read directly from Reader to the submessage? + extensionSchema.parseMessageSetItem(rawBytes, extension, extensionRegistry, extensions); + } else { + unknownFieldSchema.addLengthDelimited(unknownFields, typeId, rawBytes); + } + } + return true; + } + + @Override + public final boolean isInitialized(T message) { + FieldSet extensions = extensionSchema.getExtensions(message); + return extensions.isInitialized(); + } + + @Override + public int getSerializedSize(T message) { + int size = 0; + + size += getUnknownFieldsSerializedSize(unknownFieldSchema, message); + + if (hasExtensions) { + size += extensionSchema.getExtensions(message).getMessageSetSerializedSize(); + } + + return size; + } + + private int getUnknownFieldsSerializedSize( + UnknownFieldSchema schema, T message) { + UT unknowns = schema.getFromMessage(message); + return schema.getSerializedSizeAsMessageSet(unknowns); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MutabilityOracle.java b/java/core/src/main/java/com/google/protobuf/MutabilityOracle.java index 82b723c95d..7c2f157567 100644 --- a/java/core/src/main/java/com/google/protobuf/MutabilityOracle.java +++ b/java/core/src/main/java/com/google/protobuf/MutabilityOracle.java @@ -30,19 +30,16 @@ package com.google.protobuf; -/** - * Verifies that an object is mutable, throwing if not. - */ +/** Verifies that an object is mutable, throwing if not. */ interface MutabilityOracle { - static final MutabilityOracle IMMUTABLE = new MutabilityOracle() { - @Override - public void ensureMutable() { - throw new UnsupportedOperationException(); - } - }; + static final MutabilityOracle IMMUTABLE = + new MutabilityOracle() { + @Override + public void ensureMutable() { + throw new UnsupportedOperationException(); + } + }; - /** - * Throws an {@link UnsupportedOperationException} if not mutable. - */ + /** Throws an {@link UnsupportedOperationException} if not mutable. */ void ensureMutable(); } diff --git a/java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java b/java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java new file mode 100755 index 0000000000..f2dbb8ef9c --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java @@ -0,0 +1,36 @@ +// 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. + +package com.google.protobuf; + +interface NewInstanceSchema { + /** Create a new message instance given the default instance of the message type. */ + Object newInstance(Object defaultInstance); +} diff --git a/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaFull.java b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaFull.java new file mode 100755 index 0000000000..3433b657e6 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaFull.java @@ -0,0 +1,39 @@ +// 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. + +package com.google.protobuf; + +final class NewInstanceSchemaFull implements NewInstanceSchema { + @Override + public Object newInstance(Object defaultInstance) { + return ((GeneratedMessageV3) defaultInstance) + .newInstance(GeneratedMessageV3.UnusedPrivateParameter.INSTANCE); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java new file mode 100755 index 0000000000..9b92266763 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java @@ -0,0 +1,39 @@ +// 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. + +package com.google.protobuf; + +final class NewInstanceSchemaLite implements NewInstanceSchema { + @Override + public Object newInstance(Object defaultInstance) { + return ((GeneratedMessageLite) defaultInstance) + .dynamicMethod(GeneratedMessageLite.MethodToInvoke.NEW_MUTABLE_INSTANCE); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java new file mode 100755 index 0000000000..eff45f67b9 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java @@ -0,0 +1,53 @@ +// 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. + +package com.google.protobuf; + +final class NewInstanceSchemas { + private static final NewInstanceSchema FULL_SCHEMA = loadSchemaForFullRuntime(); + private static final NewInstanceSchema LITE_SCHEMA = new NewInstanceSchemaLite(); + + static NewInstanceSchema full() { + return FULL_SCHEMA; + } + + static NewInstanceSchema lite() { + return LITE_SCHEMA; + } + + private static NewInstanceSchema loadSchemaForFullRuntime() { + try { + Class clazz = Class.forName("com.google.protobuf.NewInstanceSchemaFull"); + return (NewInstanceSchema) clazz.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + return null; + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/NioByteString.java b/java/core/src/main/java/com/google/protobuf/NioByteString.java index 76594809e8..64c46be689 100644 --- a/java/core/src/main/java/com/google/protobuf/NioByteString.java +++ b/java/core/src/main/java/com/google/protobuf/NioByteString.java @@ -44,9 +44,7 @@ import java.nio.charset.Charset; import java.util.Collections; import java.util.List; -/** - * A {@link ByteString} that wraps around a {@link ByteBuffer}. - */ +/** A {@link ByteString} that wraps around a {@link ByteBuffer}. */ final class NioByteString extends ByteString.LeafByteString { private final ByteBuffer buffer; @@ -60,16 +58,12 @@ final class NioByteString extends ByteString.LeafByteString { // ================================================================= // Serializable - /** - * Magic method that lets us override serialization behavior. - */ + /** Magic method that lets us override serialization behavior. */ private Object writeReplace() { return ByteString.copyFrom(buffer.slice()); } - /** - * Magic method that lets us override deserialization behavior. - */ + /** Magic method that lets us override deserialization behavior. */ private void readObject(@SuppressWarnings("unused") ObjectInputStream in) throws IOException { throw new InvalidObjectException("NioByteString instances are not to be serialized directly"); } @@ -87,6 +81,13 @@ final class NioByteString extends ByteString.LeafByteString { } } + @Override + public byte internalByteAt(int index) { + // it isn't possible to avoid the bounds checking inside of ByteBuffer, so just use the default + // implementation. + return byteAt(index); + } + @Override public int size() { return buffer.remaining(); diff --git a/java/core/src/main/java/com/google/protobuf/OneofInfo.java b/java/core/src/main/java/com/google/protobuf/OneofInfo.java new file mode 100755 index 0000000000..bc518fcadd --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/OneofInfo.java @@ -0,0 +1,66 @@ +// 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. + +package com.google.protobuf; + +import java.lang.reflect.Field; + +/** Information for a oneof within a protobuf message. */ +// TODO(nathanmittler): make this private once all of experimental code is migrated to protobuf. +@ExperimentalApi +final class OneofInfo { + private final int id; + private final Field caseField; + private final Field valueField; + + public OneofInfo(int id, Field caseField, Field valueField) { + this.id = id; + this.caseField = caseField; + this.valueField = valueField; + } + + /** + * Returns the unique identifier of the oneof within the message. This is really just an index + * starting at zero. + */ + public int getId() { + return id; + } + + /** The {@code int} field containing the field number of the currently active member. */ + public Field getCaseField() { + return caseField; + } + + /** The {@link Object} field containing the value of the currently active member. */ + public Field getValueField() { + return valueField; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/Parser.java b/java/core/src/main/java/com/google/protobuf/Parser.java index e07c68959a..22ee99b189 100644 --- a/java/core/src/main/java/com/google/protobuf/Parser.java +++ b/java/core/src/main/java/com/google/protobuf/Parser.java @@ -36,7 +36,7 @@ import java.nio.ByteBuffer; /** * Abstract interface for parsing Protocol Messages. * - * The implementation should be stateless and thread-safe. + *

The implementation should be stateless and thread-safe. * *

All methods may throw {@link InvalidProtocolBufferException}. In the event of invalid data, * like an encoding error, the cause of the thrown exception will be {@code null}. However, if an @@ -55,39 +55,31 @@ public interface Parser { /** * Parses a message of {@code MessageType} from the input. * - *

Note: The caller should call - * {@link CodedInputStream#checkLastTagWas(int)} after calling this to - * verify that the last tag seen was the appropriate end-group tag, - * or zero for EOF. + *

Note: The caller should call {@link CodedInputStream#checkLastTagWas(int)} after calling + * this to verify that the last tag seen was the appropriate end-group tag, or zero for EOF. */ - public MessageType parseFrom(CodedInputStream input) - throws InvalidProtocolBufferException; + public MessageType parseFrom(CodedInputStream input) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(CodedInputStream)}, but also parses extensions. - * The extensions that you want to be able to parse must be registered in - * {@code extensionRegistry}. Extensions not in the registry will be treated - * as unknown fields. + * Like {@link #parseFrom(CodedInputStream)}, but also parses extensions. The extensions that you + * want to be able to parse must be registered in {@code extensionRegistry}. Extensions not in the + * registry will be treated as unknown fields. */ - public MessageType parseFrom(CodedInputStream input, - ExtensionRegistryLite extensionRegistry) + public MessageType parseFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(CodedInputStream)}, but does not throw an - * exception if the message is missing required fields. Instead, a partial - * message is returned. + * Like {@link #parseFrom(CodedInputStream)}, but does not throw an exception if the message is + * missing required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(CodedInputStream input) - throws InvalidProtocolBufferException; + public MessageType parsePartialFrom(CodedInputStream input) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(CodedInputStream input, ExtensionRegistryLite)}, - * but does not throw an exception if the message is missing required fields. - * Instead, a partial message is returned. + * Like {@link #parseFrom(CodedInputStream input, ExtensionRegistryLite)}, but does not throw an + * exception if the message is missing required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(CodedInputStream input, - ExtensionRegistryLite extensionRegistry) + public MessageType parsePartialFrom( + CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; // --------------------------------------------------------------- @@ -106,180 +98,145 @@ public interface Parser { public MessageType parseFrom(ByteBuffer data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Parses {@code data} as a message of {@code MessageType}. - * This is just a small wrapper around {@link #parseFrom(CodedInputStream)}. + * Parses {@code data} as a message of {@code MessageType}. This is just a small wrapper around + * {@link #parseFrom(CodedInputStream)}. */ - public MessageType parseFrom(ByteString data) - throws InvalidProtocolBufferException; + public MessageType parseFrom(ByteString data) throws InvalidProtocolBufferException; /** - * Parses {@code data} as a message of {@code MessageType}. - * This is just a small wrapper around + * Parses {@code data} as a message of {@code MessageType}. This is just a small wrapper around * {@link #parseFrom(CodedInputStream, ExtensionRegistryLite)}. */ - public MessageType parseFrom(ByteString data, - ExtensionRegistryLite extensionRegistry) + public MessageType parseFrom(ByteString data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(ByteString)}, but does not throw an - * exception if the message is missing required fields. Instead, a partial - * message is returned. + * Like {@link #parseFrom(ByteString)}, but does not throw an exception if the message is missing + * required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(ByteString data) - throws InvalidProtocolBufferException; + public MessageType parsePartialFrom(ByteString data) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(ByteString, ExtensionRegistryLite)}, - * but does not throw an exception if the message is missing required fields. - * Instead, a partial message is returned. + * Like {@link #parseFrom(ByteString, ExtensionRegistryLite)}, but does not throw an exception if + * the message is missing required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(ByteString data, - ExtensionRegistryLite extensionRegistry) + public MessageType parsePartialFrom(ByteString data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Parses {@code data} as a message of {@code MessageType}. - * This is just a small wrapper around {@link #parseFrom(CodedInputStream)}. + * Parses {@code data} as a message of {@code MessageType}. This is just a small wrapper around + * {@link #parseFrom(CodedInputStream)}. */ - public MessageType parseFrom(byte[] data, int off, int len) - throws InvalidProtocolBufferException; + public MessageType parseFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException; /** - * Parses {@code data} as a message of {@code MessageType}. - * This is just a small wrapper around + * Parses {@code data} as a message of {@code MessageType}. This is just a small wrapper around * {@link #parseFrom(CodedInputStream, ExtensionRegistryLite)}. */ - public MessageType parseFrom(byte[] data, int off, int len, - ExtensionRegistryLite extensionRegistry) + public MessageType parseFrom( + byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Parses {@code data} as a message of {@code MessageType}. - * This is just a small wrapper around {@link #parseFrom(CodedInputStream)}. + * Parses {@code data} as a message of {@code MessageType}. This is just a small wrapper around + * {@link #parseFrom(CodedInputStream)}. */ - public MessageType parseFrom(byte[] data) - throws InvalidProtocolBufferException; + public MessageType parseFrom(byte[] data) throws InvalidProtocolBufferException; /** - * Parses {@code data} as a message of {@code MessageType}. - * This is just a small wrapper around + * Parses {@code data} as a message of {@code MessageType}. This is just a small wrapper around * {@link #parseFrom(CodedInputStream, ExtensionRegistryLite)}. */ - public MessageType parseFrom(byte[] data, - ExtensionRegistryLite extensionRegistry) + public MessageType parseFrom(byte[] data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(byte[], int, int)}, but does not throw an - * exception if the message is missing required fields. Instead, a partial - * message is returned. + * Like {@link #parseFrom(byte[], int, int)}, but does not throw an exception if the message is + * missing required fields. Instead, a partial message is returned. */ public MessageType parsePartialFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(ByteString, ExtensionRegistryLite)}, - * but does not throw an exception if the message is missing required fields. - * Instead, a partial message is returned. + * Like {@link #parseFrom(ByteString, ExtensionRegistryLite)}, but does not throw an exception if + * the message is missing required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(byte[] data, int off, int len, - ExtensionRegistryLite extensionRegistry) + public MessageType parsePartialFrom( + byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(byte[])}, but does not throw an - * exception if the message is missing required fields. Instead, a partial - * message is returned. + * Like {@link #parseFrom(byte[])}, but does not throw an exception if the message is missing + * required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(byte[] data) - throws InvalidProtocolBufferException; + public MessageType parsePartialFrom(byte[] data) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(byte[], ExtensionRegistryLite)}, - * but does not throw an exception if the message is missing required fields. - * Instead, a partial message is returned. + * Like {@link #parseFrom(byte[], ExtensionRegistryLite)}, but does not throw an exception if the + * message is missing required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(byte[] data, - ExtensionRegistryLite extensionRegistry) + public MessageType parsePartialFrom(byte[] data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Parse a message of {@code MessageType} from {@code input}. - * This is just a small wrapper around {@link #parseFrom(CodedInputStream)}. - * Note that this method always reads the entire input (unless it - * throws an exception). If you want it to stop earlier, you will need to - * wrap your input in some wrapper stream that limits reading. Or, use - * {@link MessageLite#writeDelimitedTo(java.io.OutputStream)} to write your - * message and {@link #parseDelimitedFrom(InputStream)} to read it. - *

- * Despite usually reading the entire input, this does not close the stream. + * Parse a message of {@code MessageType} from {@code input}. This is just a small wrapper around + * {@link #parseFrom(CodedInputStream)}. Note that this method always reads the entire + * input (unless it throws an exception). If you want it to stop earlier, you will need to wrap + * your input in some wrapper stream that limits reading. Or, use {@link + * MessageLite#writeDelimitedTo(java.io.OutputStream)} to write your message and {@link + * #parseDelimitedFrom(InputStream)} to read it. + * + *

Despite usually reading the entire input, this does not close the stream. */ - public MessageType parseFrom(InputStream input) - throws InvalidProtocolBufferException; + public MessageType parseFrom(InputStream input) throws InvalidProtocolBufferException; /** - * Parses a message of {@code MessageType} from {@code input}. - * This is just a small wrapper around + * Parses a message of {@code MessageType} from {@code input}. This is just a small wrapper around * {@link #parseFrom(CodedInputStream, ExtensionRegistryLite)}. */ - public MessageType parseFrom(InputStream input, - ExtensionRegistryLite extensionRegistry) + public MessageType parseFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(InputStream)}, but does not throw an - * exception if the message is missing required fields. Instead, a partial - * message is returned. + * Like {@link #parseFrom(InputStream)}, but does not throw an exception if the message is missing + * required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(InputStream input) - throws InvalidProtocolBufferException; + public MessageType parsePartialFrom(InputStream input) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(InputStream, ExtensionRegistryLite)}, - * but does not throw an exception if the message is missing required fields. - * Instead, a partial message is returned. + * Like {@link #parseFrom(InputStream, ExtensionRegistryLite)}, but does not throw an exception if + * the message is missing required fields. Instead, a partial message is returned. */ - public MessageType parsePartialFrom(InputStream input, - ExtensionRegistryLite extensionRegistry) + public MessageType parsePartialFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Like {@link #parseFrom(InputStream)}, but does not read util EOF. - * Instead, the size of message (encoded as a varint) is read first, - * then the message data. Use - * {@link MessageLite#writeDelimitedTo(java.io.OutputStream)} to write - * messages in this format. + * Like {@link #parseFrom(InputStream)}, but does not read until EOF. Instead, the size of message + * (encoded as a varint) is read first, then the message data. Use {@link + * MessageLite#writeDelimitedTo(java.io.OutputStream)} to write messages in this format. * - * @return Parsed message if successful, or null if the stream is at EOF when - * the method starts. Any other error (including reaching EOF during - * parsing) will cause an exception to be thrown. + * @return Parsed message if successful, or null if the stream is at EOF when the method starts. + * Any other error (including reaching EOF during parsing) will cause an exception to be + * thrown. */ - public MessageType parseDelimitedFrom(InputStream input) - throws InvalidProtocolBufferException; + public MessageType parseDelimitedFrom(InputStream input) throws InvalidProtocolBufferException; - /** - * Like {@link #parseDelimitedFrom(InputStream)} but supporting extensions. - */ - public MessageType parseDelimitedFrom(InputStream input, - ExtensionRegistryLite extensionRegistry) + /** Like {@link #parseDelimitedFrom(InputStream)} but supporting extensions. */ + public MessageType parseDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; /** - * Like {@link #parseDelimitedFrom(InputStream)}, but does not throw an - * exception if the message is missing required fields. Instead, a partial - * message is returned. + * Like {@link #parseDelimitedFrom(InputStream)}, but does not throw an exception if the message + * is missing required fields. Instead, a partial message is returned. */ public MessageType parsePartialDelimitedFrom(InputStream input) throws InvalidProtocolBufferException; /** - * Like {@link #parseDelimitedFrom(InputStream, ExtensionRegistryLite)}, - * but does not throw an exception if the message is missing required fields. - * Instead, a partial message is returned. + * Like {@link #parseDelimitedFrom(InputStream, ExtensionRegistryLite)}, but does not throw an + * exception if the message is missing required fields. Instead, a partial message is returned. */ public MessageType parsePartialDelimitedFrom( - InputStream input, - ExtensionRegistryLite extensionRegistry) + InputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; } diff --git a/java/core/src/main/java/com/google/protobuf/ProtoSyntax.java b/java/core/src/main/java/com/google/protobuf/ProtoSyntax.java new file mode 100755 index 0000000000..851b839f6f --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ProtoSyntax.java @@ -0,0 +1,38 @@ +// 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. + +package com.google.protobuf; + +/** Represents the syntax version of the message. */ +@ExperimentalApi +public enum ProtoSyntax { + PROTO2, + PROTO3; +} diff --git a/java/core/src/main/java/com/google/protobuf/Protobuf.java b/java/core/src/main/java/com/google/protobuf/Protobuf.java new file mode 100755 index 0000000000..adaa7fa8f2 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/Protobuf.java @@ -0,0 +1,152 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +import java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * Main runtime interface for protobuf. Applications should interact with this interface (rather + * than directly accessing internal APIs) in order to perform operations on protobuf messages. + */ +@ExperimentalApi +final class Protobuf { + private static final Protobuf INSTANCE = new Protobuf(); + + private final SchemaFactory schemaFactory; + + // TODO(nathanmittler): Consider using ClassValue instead. + private final ConcurrentMap, Schema> schemaCache = + new ConcurrentHashMap, Schema>(); + + /** Gets the singleton instance of the Protobuf runtime. */ + public static Protobuf getInstance() { + return INSTANCE; + } + + /** Writes the given message to the target {@link Writer}. */ + public void writeTo(T message, Writer writer) throws IOException { + schemaFor(message).writeTo(message, writer); + } + + /** Reads fields from the given {@link Reader} and merges them into the message. */ + public void mergeFrom(T message, Reader reader) throws IOException { + mergeFrom(message, reader, ExtensionRegistryLite.getEmptyRegistry()); + } + + /** Reads fields from the given {@link Reader} and merges them into the message. */ + public void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) + throws IOException { + schemaFor(message).mergeFrom(message, reader, extensionRegistry); + } + + /** Marks repeated/map/extension/unknown fields as immutable. */ + public void makeImmutable(T message) { + schemaFor(message).makeImmutable(message); + } + + /** + * Checks if all required fields are set. TODO(xiaofeng): Make this package private when the tests + * are moved to protobuf package. + */ + public boolean isInitialized(T message) { + return schemaFor(message).isInitialized(message); + } + + /** Gets the schema for the given message type. */ + public Schema schemaFor(Class messageType) { + checkNotNull(messageType, "messageType"); + @SuppressWarnings("unchecked") + Schema schema = (Schema) schemaCache.get(messageType); + if (schema == null) { + schema = schemaFactory.createSchema(messageType); + @SuppressWarnings("unchecked") + Schema previous = (Schema) registerSchema(messageType, schema); + if (previous != null) { + // A new schema was registered by another thread. + schema = previous; + } + } + return schema; + } + + /** Gets the schema for the given message. */ + @SuppressWarnings("unchecked") + public Schema schemaFor(T message) { + return schemaFor((Class) message.getClass()); + } + + /** + * Registers the given schema for the message type only if a schema was not already registered. + * + * @param messageType the type of message on which the schema operates. + * @param schema the schema for the message type. + * @return the previously registered schema, or {@code null} if the given schema was successfully + * registered. + */ + public Schema registerSchema(Class messageType, Schema schema) { + checkNotNull(messageType, "messageType"); + checkNotNull(schema, "schema"); + return schemaCache.putIfAbsent(messageType, schema); + } + + /** + * Visible for testing only. Registers the given schema for the message type. If a schema was + * previously registered, it will be replaced by the provided schema. + * + * @param messageType the type of message on which the schema operates. + * @param schema the schema for the message type. + * @return the previously registered schema, or {@code null} if no schema was registered + * previously. + */ + public Schema registerSchemaOverride(Class messageType, Schema schema) { + checkNotNull(messageType, "messageType"); + checkNotNull(schema, "schema"); + return schemaCache.put(messageType, schema); + } + + private Protobuf() { + schemaFactory = new ManifestSchemaFactory(); + } + + int getTotalSchemaSize() { + int result = 0; + for (Schema schema : schemaCache.values()) { + if (schema instanceof MessageSchema) { + result += ((MessageSchema) schema).getSchemaSize(); + } + } + return result; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java b/java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java index 81255ec291..175b1ddf46 100644 --- a/java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java @@ -31,75 +31,144 @@ package com.google.protobuf; import com.google.protobuf.Internal.ProtobufList; +import java.util.Arrays; -import java.util.ArrayList; -import java.util.List; - -/** - * Implements {@link ProtobufList} for non-primitive and {@link String} types. - */ +/** Implements {@link ProtobufList} for non-primitive and {@link String} types. */ final class ProtobufArrayList extends AbstractProtobufList { - private static final ProtobufArrayList EMPTY_LIST = new ProtobufArrayList(); + private static final ProtobufArrayList EMPTY_LIST = + new ProtobufArrayList(new Object[0], 0); + static { EMPTY_LIST.makeImmutable(); } - + @SuppressWarnings("unchecked") // Guaranteed safe by runtime. public static ProtobufArrayList emptyList() { return (ProtobufArrayList) EMPTY_LIST; } - - private final List list; + private E[] array; + private int size; + + @SuppressWarnings("unchecked") ProtobufArrayList() { - this(new ArrayList(DEFAULT_CAPACITY)); + this((E[]) new Object[DEFAULT_CAPACITY], 0); } - - private ProtobufArrayList(List list) { - this.list = list; + + private ProtobufArrayList(E[] array, int size) { + this.array = array; + this.size = size; } @Override public ProtobufArrayList mutableCopyWithCapacity(int capacity) { - if (capacity < size()) { + if (capacity < size) { throw new IllegalArgumentException(); } - List newList = new ArrayList(capacity); - newList.addAll(list); - return new ProtobufArrayList(newList); + + E[] newArray = Arrays.copyOf(array, capacity); + + return new ProtobufArrayList(newArray, size); + } + + @Override + public boolean add(E element) { + ensureIsMutable(); + + if (size == array.length) { + // Resize to 1.5x the size + int length = ((size * 3) / 2) + 1; + E[] newArray = Arrays.copyOf(array, length); + + array = newArray; + } + + array[size++] = element; + modCount++; + + return true; } - + @Override public void add(int index, E element) { ensureIsMutable(); - list.add(index, element); + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)); + } + + if (size < array.length) { + // Shift everything over to make room + System.arraycopy(array, index, array, index + 1, size - index); + } else { + // Resize to 1.5x the size + int length = ((size * 3) / 2) + 1; + E[] newArray = createArray(length); + + // Copy the first part directly + System.arraycopy(array, 0, newArray, 0, index); + + // Copy the rest shifted over by one to make room + System.arraycopy(array, index, newArray, index + 1, size - index); + array = newArray; + } + + array[index] = element; + size++; modCount++; } @Override public E get(int index) { - return list.get(index); + ensureIndexInRange(index); + return array[index]; } - + @Override public E remove(int index) { ensureIsMutable(); - E toReturn = list.remove(index); + ensureIndexInRange(index); + + E value = array[index]; + if (index < size - 1) { + System.arraycopy(array, index + 1, array, index, size - index - 1); + } + + size--; modCount++; - return toReturn; + return value; } - + @Override public E set(int index, E element) { ensureIsMutable(); - E toReturn = list.set(index, element); + ensureIndexInRange(index); + + E toReturn = array[index]; + array[index] = element; + modCount++; return toReturn; } @Override public int size() { - return list.size(); + return size; + } + + @SuppressWarnings("unchecked") + private static E[] createArray(int capacity) { + return (E[]) new Object[capacity]; + } + + private void ensureIndexInRange(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)); + } + } + + private String makeOutOfBoundsExceptionMessage(int index) { + return "Index:" + index + ", Size:" + size; } } diff --git a/java/core/src/main/java/com/google/protobuf/ProtobufLists.java b/java/core/src/main/java/com/google/protobuf/ProtobufLists.java new file mode 100755 index 0000000000..271c849b4d --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ProtobufLists.java @@ -0,0 +1,94 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.Internal.BooleanList; +import com.google.protobuf.Internal.DoubleList; +import com.google.protobuf.Internal.FloatList; +import com.google.protobuf.Internal.IntList; +import com.google.protobuf.Internal.LongList; +import com.google.protobuf.Internal.ProtobufList; + +/** Utility class for construction of lists that extend {@link ProtobufList}. */ +@ExperimentalApi +final class ProtobufLists { + private ProtobufLists() {} + + public static ProtobufList emptyProtobufList() { + return ProtobufArrayList.emptyList(); + } + + public static ProtobufList mutableCopy(ProtobufList list) { + int size = list.size(); + return list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + } + + public static BooleanList emptyBooleanList() { + return BooleanArrayList.emptyList(); + } + + public static BooleanList newBooleanList() { + return new BooleanArrayList(); + } + + public static IntList emptyIntList() { + return IntArrayList.emptyList(); + } + + public static IntList newIntList() { + return new IntArrayList(); + } + + public static LongList emptyLongList() { + return LongArrayList.emptyList(); + } + + public static LongList newLongList() { + return new LongArrayList(); + } + + public static FloatList emptyFloatList() { + return FloatArrayList.emptyList(); + } + + public static FloatList newFloatList() { + return new FloatArrayList(); + } + + public static DoubleList emptyDoubleList() { + return DoubleArrayList.emptyList(); + } + + public static DoubleList newDoubleList() { + return new DoubleArrayList(); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ProtocolMessageEnum.java b/java/core/src/main/java/com/google/protobuf/ProtocolMessageEnum.java index a596d30190..5d6f5ac442 100644 --- a/java/core/src/main/java/com/google/protobuf/ProtocolMessageEnum.java +++ b/java/core/src/main/java/com/google/protobuf/ProtocolMessageEnum.java @@ -33,27 +33,20 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.EnumDescriptor; import com.google.protobuf.Descriptors.EnumValueDescriptor; -/** - * Interface of useful methods added to all enums generated by the protocol - * compiler. - */ +/** Interface of useful methods added to all enums generated by the protocol compiler. */ public interface ProtocolMessageEnum extends Internal.EnumLite { - /** - * Return the value's numeric value as defined in the .proto file. - */ + /** Return the value's numeric value as defined in the .proto file. */ @Override int getNumber(); /** - * Return the value's descriptor, which contains information such as - * value name, number, and type. + * Return the value's descriptor, which contains information such as value name, number, and type. */ EnumValueDescriptor getValueDescriptor(); /** - * Return the enum type's descriptor, which contains information - * about each defined value, etc. + * Return the enum type's descriptor, which contains information about each defined value, etc. */ EnumDescriptor getDescriptorForType(); } diff --git a/java/core/src/main/java/com/google/protobuf/ProtocolStringList.java b/java/core/src/main/java/com/google/protobuf/ProtocolStringList.java index d553b41e52..5df3dbdbda 100644 --- a/java/core/src/main/java/com/google/protobuf/ProtocolStringList.java +++ b/java/core/src/main/java/com/google/protobuf/ProtocolStringList.java @@ -33,16 +33,14 @@ package com.google.protobuf; import java.util.List; /** - * An interface extending {@code List} used for repeated string fields - * to provide optional access to the data as a list of ByteStrings. The - * underlying implementation stores values as either ByteStrings or Strings - * (see {@link LazyStringArrayList}) depending on how the value was initialized - * or last read, and it is often more efficient to deal with lists of - * ByteStrings when handling protos that have been deserialized from bytes. + * An interface extending {@code List} used for repeated string fields to provide optional + * access to the data as a list of ByteStrings. The underlying implementation stores values as + * either ByteStrings or Strings (see {@link LazyStringArrayList}) depending on how the value was + * initialized or last read, and it is often more efficient to deal with lists of ByteStrings when + * handling protos that have been deserialized from bytes. */ public interface ProtocolStringList extends List { /** Returns a view of the data as a list of ByteStrings. */ List asByteStringList(); - } diff --git a/java/core/src/main/java/com/google/protobuf/RawMessageInfo.java b/java/core/src/main/java/com/google/protobuf/RawMessageInfo.java new file mode 100755 index 0000000000..b9a3d2bb55 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/RawMessageInfo.java @@ -0,0 +1,220 @@ +// 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. + +package com.google.protobuf; + +/** + * RawMessageInfo stores the same amount of information as {@link MessageInfo} but in a more compact + * format. + */ +final class RawMessageInfo implements MessageInfo { + + private final MessageLite defaultInstance; + + /** + * The compact format packs everything in a String object and a Object[] array. The String object + * is encoded with field number, field type, hasbits offset, oneof index, etc., whereas the + * Object[] array contains field references, class references, instance references, etc. + * + *

The String object encodes a sequence of integers into UTF-16 characters. For each int, it + * will be encoding into 1 to 3 UTF-16 characters depending on its unsigned value: + * + *

    + *
  • 1 char: [c1: 0x0000 - 0xD7FF] = int of the same value. + *
  • 2 chars: [c1: 0xE000 - 0xFFFF], [c2: 0x0000 - 0xD7FF] = (c2 << 13) | (c1 & 0x1FFF) + *
  • 3 chars: [c1: 0xE000 - 0xFFFF], [c2: 0xE000 - 0xFFFF], [c3: 0x0000 - 0xD7FF] = (c3 << 26) + * | ((c2 & 0x1FFF) << 13) | (c1 & 0x1FFF) + *
+ * + *

Note that we don't use UTF-16 surrogate pairs [0xD800 - 0xDFFF] because they have to come in + * pairs to form a valid UTF-16char sequence and don't help us encode values more efficiently. + * + *

The integer sequence encoded in the String object has the following layout: + * + *

    + *
  • [0]: flags, flags & 0x1 = is proto2?, flags & 0x2 = is message?. + *
  • [1]: field count, if 0, this is the end of the integer sequence and the corresponding + * Object[] array should be null. + *
  • [2]: oneof count + *
  • [3]: hasbits count, how many hasbits integers are generated. + *
  • [4]: min field number + *
  • [5]: max field number + *
  • [6]: total number of entries need to allocate + *
  • [7]: map field count + *
  • [8]: repeated field count, this doesn't include map fields. + *
  • [9]: size of checkInitialized array + *
  • [...]: field entries + *
+ * + *

Each field entry starts with a field number and the field type: + * + *

    + *
  • [0]: field number + *
  • [1]: field type with extra bits: + *
      + *
    • v & 0xFF = field type as defined in the FieldType class + *
    • v & 0x100 = is required? + *
    • v & 0x200 = is checkUtf8? + *
    • v & 0x400 = needs isInitialized check? + *
    • v & 0x800 = is map field with proto2 enum value? + *
    + *
+ * + * If the file is proto2 and this is a singular field: + * + *
    + *
  • [2]: hasbits offset + *
+ * + * If the field is in an oneof: + * + *
    + *
  • [2]: oenof index + *
+ * + * For other types, the field entry only has field number and field type. + * + *

The Object[] array has 3 sections: + * + *

    + *
  • ---- oneof section ---- + *
      + *
    • [0]: value field for oneof 1. + *
    • [1]: case field for oneof 1. + *
    • ... + *
    • [.]: value field for oneof n. + *
    • [.]: case field for oneof n. + *
    + *
  • ---- hasbits section ---- + *
      + *
    • [.]: hasbits field 1 + *
    • [.]: hasbits field 2 + *
    • ... + *
    • [.]: hasbits field n + *
    + *
  • ---- field section ---- + *
      + *
    • [...]: field entries + *
    + *
+ * + *

In the Object[] array, field entries are ordered in the same way as field entries in the + * String object. The size of each entry is determined by the field type. + * + *

    + *
  • Oneof field: + *
      + *
    • Oneof message field: + *
        + *
      • [0]: message class reference. + *
      + *
    • Oneof enum fieldin proto2: + *
        + *
      • [0]: EnumLiteMap + *
      + *
    • For all other oneof fields, field entry in the Object[] array is empty. + *
    + *
  • Repeated message field: + *
      + *
    • [0]: field reference + *
    • [1]: message class reference + *
    + *
  • Proto2 singular/repeated enum field: + *
      + *
    • [0]: field reference + *
    • [1]: EnumLiteMap + *
    + *
  • Map field with a proto2 enum value: + *
      + *
    • [0]: field reference + *
    • [1]: map default entry instance + *
    • [2]: EnumLiteMap + *
    + *
  • Map field with other value types: + *
      + *
    • [0]: field reference + *
    • [1]: map default entry instance + *
    + *
  • All other field type: + *
      + *
    • [0]: field reference + *
    + *
+ * + *

In order to read the field info from this compact format, a reader needs to progress through + * the String object and the Object[] array simultaneously. + */ + private final String info; + + private final Object[] objects; + private final int flags; + + RawMessageInfo(MessageLite defaultInstance, String info, Object[] objects) { + this.defaultInstance = defaultInstance; + this.info = info; + this.objects = objects; + int position = 0; + int value = (int) info.charAt(position++); + if (value < 0xD800) { + flags = value; + } else { + int result = value & 0x1FFF; + int shift = 13; + while ((value = info.charAt(position++)) >= 0xD800) { + result |= (value & 0x1FFF) << shift; + shift += 13; + } + flags = result | (value << shift); + } + } + + String getStringInfo() { + return info; + } + + Object[] getObjects() { + return objects; + } + + @Override + public MessageLite getDefaultInstance() { + return defaultInstance; + } + + @Override + public ProtoSyntax getSyntax() { + return (flags & 0x1) == 0x1 ? ProtoSyntax.PROTO2 : ProtoSyntax.PROTO3; + } + + @Override + public boolean isMessageSetWireFormat() { + return (flags & 0x2) == 0x2; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/Reader.java b/java/core/src/main/java/com/google/protobuf/Reader.java new file mode 100755 index 0000000000..705096f2d8 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/Reader.java @@ -0,0 +1,379 @@ +// 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. + +package com.google.protobuf; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** A reader of fields from a serialized protobuf message. */ +// TODO(nathanmittler): Refactor to allow the reader to allocate properly sized lists. +@ExperimentalApi +interface Reader { + /** Value used to indicate that the end of input has been reached. */ + int READ_DONE = Integer.MAX_VALUE; + + /** Value used to indicate that the reader does not know the tag about the field. */ + int TAG_UNKNOWN = 0; + + boolean shouldDiscardUnknownFields(); + + /** + * Gets the field number for the current field being read. + * + *

TODO(liujisi): Rename it to make it more explicit about the side effect on the underlying + * buffer. + * + * @return the current field number or {@link #READ_DONE} if the end of input has been reached. + */ + int getFieldNumber() throws IOException; + + /** + * Gets the wire tag of the current field. + * + * @return the current wire tag or {@link #TAG_UNKNOWN} if the reader does not know the tag of the + * current field. + */ + int getTag(); + + /** + * Skips the current field and advances the reader to the next field. + * + * @return {@code true} if there are more fields or {@code false} if the end of input has been + * reached. + */ + boolean skipField() throws IOException; + + /** + * Reads and returns the next field of type {@code DOUBLE} and advances the reader to the next + * field. + */ + double readDouble() throws IOException; + + /** + * Reads and returns the next field of type {@code FLOAT} and advances the reader to the next + * field. + */ + float readFloat() throws IOException; + + /** + * Reads and returns the next field of type {@code UINT64} and advances the reader to the next + * field. + */ + long readUInt64() throws IOException; + + /** + * Reads and returns the next field of type {@code INT64} and advances the reader to the next + * field. + */ + long readInt64() throws IOException; + + /** + * Reads and returns the next field of type {@code INT32} and advances the reader to the next + * field. + */ + int readInt32() throws IOException; + + /** + * Reads and returns the next field of type {@code FIXED64} and advances the reader to the next + * field. + */ + long readFixed64() throws IOException; + + /** + * Reads and returns the next field of type {@code FIXED32} and advances the reader to the next + * field. + */ + int readFixed32() throws IOException; + + /** + * Reads and returns the next field of type {@code BOOL} and advances the reader to the next + * field. + */ + boolean readBool() throws IOException; + + /** + * Reads and returns the next field of type {@code STRING} and advances the reader to the next + * field. If the stream contains malformed UTF-8, replace the offending bytes with the standard + * UTF-8 replacement character. + */ + String readString() throws IOException; + + /** + * Reads and returns the next field of type {@code STRING} and advances the reader to the next + * field. If the stream contains malformed UTF-8, throw exception {@link + * InvalidProtocolBufferException}. + */ + String readStringRequireUtf8() throws IOException; + + // TODO(yilunchong): the lack of other opinions for whether to expose this on the interface + T readMessageBySchemaWithCheck(Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Reads and returns the next field of type {@code MESSAGE} and advances the reader to the next + * field. + */ + T readMessage(Class clazz, ExtensionRegistryLite extensionRegistry) throws IOException; + + /** + * Reads and returns the next field of type {@code GROUP} and advances the reader to the next + * field. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + T readGroup(Class clazz, ExtensionRegistryLite extensionRegistry) throws IOException; + + // TODO(yilunchong): the lack of other opinions for whether to expose this on the interface + @Deprecated + T readGroupBySchemaWithCheck(Schema schema, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Reads and returns the next field of type {@code BYTES} and advances the reader to the next + * field. + */ + ByteString readBytes() throws IOException; + + /** + * Reads and returns the next field of type {@code UINT32} and advances the reader to the next + * field. + */ + int readUInt32() throws IOException; + + /** + * Reads and returns the next field of type {@code ENUM} and advances the reader to the next + * field. + */ + int readEnum() throws IOException; + + /** + * Reads and returns the next field of type {@code SFIXED32} and advances the reader to the next + * field. + */ + int readSFixed32() throws IOException; + + /** + * Reads and returns the next field of type {@code SFIXED64} and advances the reader to the next + * field. + */ + long readSFixed64() throws IOException; + + /** + * Reads and returns the next field of type {@code SINT32} and advances the reader to the next + * field. + */ + int readSInt32() throws IOException; + + /** + * Reads and returns the next field of type {@code SINT64} and advances the reader to the next + * field. + */ + long readSInt64() throws IOException; + + /** + * Reads the next field of type {@code DOUBLE_LIST} or {@code DOUBLE_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readDoubleList(List target) throws IOException; + + /** + * Reads the next field of type {@code FLOAT_LIST} or {@code FLOAT_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readFloatList(List target) throws IOException; + + /** + * Reads the next field of type {@code UINT64_LIST} or {@code UINT64_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readUInt64List(List target) throws IOException; + + /** + * Reads the next field of type {@code INT64_LIST} or {@code INT64_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readInt64List(List target) throws IOException; + + /** + * Reads the next field of type {@code INT32_LIST} or {@code INT32_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readInt32List(List target) throws IOException; + + /** + * Reads the next field of type {@code FIXED64_LIST} or {@code FIXED64_LIST_PACKED} and advances + * the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readFixed64List(List target) throws IOException; + + /** + * Reads the next field of type {@code FIXED32_LIST} or {@code FIXED32_LIST_PACKED} and advances + * the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readFixed32List(List target) throws IOException; + + /** + * Reads the next field of type {@code BOOL_LIST} or {@code BOOL_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readBoolList(List target) throws IOException; + + /** + * Reads the next field of type {@code STRING_LIST} and advances the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readStringList(List target) throws IOException; + + /** + * Reads the next field of type {@code STRING_LIST} and advances the reader to the next field. If + * the stream contains malformed UTF-8, throw exception {@link InvalidProtocolBufferException}. + * + * @param target the list that will receive the read values. + */ + void readStringListRequireUtf8(List target) throws IOException; + + /** + * Reads the next field of type {@code MESSAGE_LIST} and advances the reader to the next field. + * + * @param target the list that will receive the read values. + * @param targetType the type of the elements stored in the {@code target} list. + */ + void readMessageList( + List target, Schema schema, ExtensionRegistryLite extensionRegistry) throws IOException; + + void readMessageList( + List target, Class targetType, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Reads the next field of type {@code GROUP_LIST} and advances the reader to the next field. + * + * @param target the list that will receive the read values. + * @param targetType the type of the elements stored in the {@code target} list. + * @deprecated groups fields are deprecated. + */ + @Deprecated + void readGroupList( + List target, Class targetType, ExtensionRegistryLite extensionRegistry) + throws IOException; + + @Deprecated + void readGroupList( + List target, Schema targetType, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Reads the next field of type {@code BYTES_LIST} and advances the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readBytesList(List target) throws IOException; + + /** + * Reads the next field of type {@code UINT32_LIST} or {@code UINT32_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readUInt32List(List target) throws IOException; + + /** + * Reads the next field of type {@code ENUM_LIST} or {@code ENUM_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readEnumList(List target) throws IOException; + + /** + * Reads the next field of type {@code SFIXED32_LIST} or {@code SFIXED32_LIST_PACKED} and advances + * the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readSFixed32List(List target) throws IOException; + + /** + * Reads the next field of type {@code SFIXED64_LIST} or {@code SFIXED64_LIST_PACKED} and advances + * the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readSFixed64List(List target) throws IOException; + + /** + * Reads the next field of type {@code SINT32_LIST} or {@code SINT32_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readSInt32List(List target) throws IOException; + + /** + * Reads the next field of type {@code SINT64_LIST} or {@code SINT64_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readSInt64List(List target) throws IOException; + + /** + * Reads the next field of type {@code MAP} and advances the reader to the next field. + * + * @param target the mutable map that will receive the read values. + * @param mapDefaultEntry the default entry of the map field. + * @param extensionRegistry the extension registry for parsing message value fields. + */ + void readMap( + Map target, + MapEntryLite.Metadata mapDefaultEntry, + ExtensionRegistryLite extensionRegistry) + throws IOException; +} diff --git a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java index b593b56653..f3b09fbacc 100644 --- a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java +++ b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java @@ -39,36 +39,29 @@ import java.util.Collections; import java.util.List; /** - * {@code RepeatedFieldBuilder} implements a structure that a protocol - * message uses to hold a repeated field of other protocol messages. It supports - * the classical use case of adding immutable {@link Message}'s to the - * repeated field and is highly optimized around this (no extra memory - * allocations and sharing of immutable arrays). - *
- * It also supports the additional use case of adding a {@link Message.Builder} - * to the repeated field and deferring conversion of that {@code Builder} - * to an immutable {@code Message}. In this way, it's possible to maintain - * a tree of {@code Builder}'s that acts as a fully read/write data - * structure. - *
- * Logically, one can think of a tree of builders as converting the entire tree - * to messages when build is called on the root or when any method is called - * that desires a Message instead of a Builder. In terms of the implementation, - * the {@code SingleFieldBuilder} and {@code RepeatedFieldBuilder} - * classes cache messages that were created so that messages only need to be - * created when some change occurred in its builder or a builder for one of its - * descendants. + * {@code RepeatedFieldBuilder} implements a structure that a protocol message uses to hold a + * repeated field of other protocol messages. It supports the classical use case of adding immutable + * {@link Message}'s to the repeated field and is highly optimized around this (no extra memory + * allocations and sharing of immutable arrays).
+ * It also supports the additional use case of adding a {@link Message.Builder} to the repeated + * field and deferring conversion of that {@code Builder} to an immutable {@code Message}. In this + * way, it's possible to maintain a tree of {@code Builder}'s that acts as a fully read/write data + * structure.
+ * Logically, one can think of a tree of builders as converting the entire tree to messages when + * build is called on the root or when any method is called that desires a Message instead of a + * Builder. In terms of the implementation, the {@code SingleFieldBuilder} and {@code + * RepeatedFieldBuilder} classes cache messages that were created so that messages only need to be + * created when some change occurred in its builder or a builder for one of its descendants. * * @param the type of message for the field * @param the type of builder for the field * @param the common interface for the message and the builder - * * @author jonp@google.com (Jon Perlow) */ -public class RepeatedFieldBuilder - +public class RepeatedFieldBuilder< + MType extends GeneratedMessage, + BType extends GeneratedMessage.Builder, + IType extends MessageOrBuilder> implements GeneratedMessage.BuilderParent { // Parent to send changes to. @@ -120,8 +113,7 @@ public class RepeatedFieldBuilder // is fully backed by this object and all changes are reflected in it. // Access to any item returns either a builder or message depending on // what is most efficient. - private MessageOrBuilderExternalList - externalMessageOrBuilderList; + private MessageOrBuilderExternalList externalMessageOrBuilderList; /** * Constructs a new builder with an empty list of messages. @@ -148,8 +140,8 @@ public class RepeatedFieldBuilder } /** - * Ensures that the list of messages is mutable so it can be updated. If it's - * immutable, a copy is made. + * Ensures that the list of messages is mutable so it can be updated. If it's immutable, a copy is + * made. */ private void ensureMutableMessageList() { if (!isMessagesListMutable) { @@ -159,15 +151,12 @@ public class RepeatedFieldBuilder } /** - * Ensures that the list of builders is not null. If it's null, the list is - * created and initialized to be the same size as the messages list with - * null entries. + * Ensures that the list of builders is not null. If it's null, the list is created and + * initialized to be the same size as the messages list with null entries. */ private void ensureBuilders() { if (this.builders == null) { - this.builders = - new ArrayList>( - messages.size()); + this.builders = new ArrayList>(messages.size()); for (int i = 0; i < messages.size(); i++) { builders.add(null); } @@ -193,9 +182,9 @@ public class RepeatedFieldBuilder } /** - * Get the message at the specified index. If the message is currently stored - * as a {@code Builder}, it is converted to a {@code Message} by - * calling {@link Message.Builder#buildPartial} on it. + * Get the message at the specified index. If the message is currently stored as a {@code + * Builder}, it is converted to a {@code Message} by calling {@link Message.Builder#buildPartial} + * on it. * * @param index the index of the message to get * @return the message for the specified index @@ -205,13 +194,13 @@ public class RepeatedFieldBuilder } /** - * Get the message at the specified index. If the message is currently stored - * as a {@code Builder}, it is converted to a {@code Message} by - * calling {@link Message.Builder#buildPartial} on it. + * Get the message at the specified index. If the message is currently stored as a {@code + * Builder}, it is converted to a {@code Message} by calling {@link Message.Builder#buildPartial} + * on it. * * @param index the index of the message to get - * @param forBuild this is being called for build so we want to make sure - * we SingleFieldBuilder.build to send dirty invalidations + * @param forBuild this is being called for build so we want to make sure we + * SingleFieldBuilder.build to send dirty invalidations * @return the message for the specified index */ private MType getMessage(int index, boolean forBuild) { @@ -235,9 +224,8 @@ public class RepeatedFieldBuilder } /** - * Gets a builder for the specified index. If no builder has been created for - * that index, a builder is created on demand by calling - * {@link Message#toBuilder}. + * Gets a builder for the specified index. If no builder has been created for that index, a + * builder is created on demand by calling {@link Message#toBuilder}. * * @param index the index of the message to get * @return The builder for that index @@ -247,16 +235,15 @@ public class RepeatedFieldBuilder SingleFieldBuilder builder = builders.get(index); if (builder == null) { MType message = messages.get(index); - builder = new SingleFieldBuilder( - message, this, isClean); + builder = new SingleFieldBuilder(message, this, isClean); builders.set(index, builder); } return builder.getBuilder(); } /** - * Gets the base class interface for the specified index. This may either be - * a builder or a message. It will return whatever is more efficient. + * Gets the base class interface for the specified index. This may either be a builder or a + * message. It will return whatever is more efficient. * * @param index the index of the message to get * @return the message or builder for the index as the base class interface @@ -283,21 +270,18 @@ public class RepeatedFieldBuilder } /** - * Sets a message at the specified index replacing the existing item at - * that index. + * Sets a message at the specified index replacing the existing item at that index. * * @param index the index to set. * @param message the message to set * @return the builder */ - public RepeatedFieldBuilder setMessage( - int index, MType message) { + public RepeatedFieldBuilder setMessage(int index, MType message) { checkNotNull(message); ensureMutableMessageList(); messages.set(index, message); if (builders != null) { - SingleFieldBuilder entry = - builders.set(index, null); + SingleFieldBuilder entry = builders.set(index, null); if (entry != null) { entry.dispose(); } @@ -313,8 +297,7 @@ public class RepeatedFieldBuilder * @param message the message to add * @return the builder */ - public RepeatedFieldBuilder addMessage( - MType message) { + public RepeatedFieldBuilder addMessage(MType message) { checkNotNull(message); ensureMutableMessageList(); messages.add(message); @@ -327,16 +310,15 @@ public class RepeatedFieldBuilder } /** - * Inserts the specified message at the specified position in this list. - * Shifts the element currently at that position (if any) and any subsequent - * elements to the right (adds one to their indices). + * Inserts the specified message at the specified position in this list. Shifts the element + * currently at that position (if any) and any subsequent elements to the right (adds one to their + * indices). * * @param index the index at which to insert the message * @param message the message to add * @return the builder */ - public RepeatedFieldBuilder addMessage( - int index, MType message) { + public RepeatedFieldBuilder addMessage(int index, MType message) { checkNotNull(message); ensureMutableMessageList(); messages.add(index, message); @@ -349,9 +331,8 @@ public class RepeatedFieldBuilder } /** - * Appends all of the messages in the specified collection to the end of - * this list, in the order that they are returned by the specified - * collection's iterator. + * Appends all of the messages in the specified collection to the end of this list, in the order + * that they are returned by the specified collection's iterator. * * @param values the messages to add * @return the builder @@ -365,8 +346,8 @@ public class RepeatedFieldBuilder // If we can inspect the size, we can more efficiently add messages. int size = -1; if (values instanceof Collection) { - @SuppressWarnings("unchecked") final - Collection collection = (Collection) values; + @SuppressWarnings("unchecked") + final Collection collection = (Collection) values; if (collection.size() == 0) { return this; } @@ -375,8 +356,7 @@ public class RepeatedFieldBuilder ensureMutableMessageList(); if (size >= 0 && messages instanceof ArrayList) { - ((ArrayList) messages) - .ensureCapacity(messages.size() + size); + ((ArrayList) messages).ensureCapacity(messages.size() + size); } for (MType value : values) { @@ -398,8 +378,7 @@ public class RepeatedFieldBuilder ensureMutableMessageList(); ensureBuilders(); SingleFieldBuilder builder = - new SingleFieldBuilder( - message, this, isClean); + new SingleFieldBuilder(message, this, isClean); messages.add(null); builders.add(builder); onChanged(); @@ -408,9 +387,8 @@ public class RepeatedFieldBuilder } /** - * Inserts a new builder at the specified position in this list. - * Shifts the element currently at that position (if any) and any subsequent - * elements to the right (adds one to their indices). + * Inserts a new builder at the specified position in this list. Shifts the element currently at + * that position (if any) and any subsequent elements to the right (adds one to their indices). * * @param index the index at which to insert the builder * @param message the message to add which is the basis of the builder @@ -420,8 +398,7 @@ public class RepeatedFieldBuilder ensureMutableMessageList(); ensureBuilders(); SingleFieldBuilder builder = - new SingleFieldBuilder( - message, this, isClean); + new SingleFieldBuilder(message, this, isClean); messages.add(index, null); builders.add(index, builder); onChanged(); @@ -430,9 +407,9 @@ public class RepeatedFieldBuilder } /** - * Removes the element at the specified position in this list. Shifts any - * subsequent elements to the left (subtracts one from their indices). - * Returns the element that was removed from the list. + * Removes the element at the specified position in this list. Shifts any subsequent elements to + * the left (subtracts one from their indices). Returns the element that was removed from the + * list. * * @param index the index at which to remove the message */ @@ -440,8 +417,7 @@ public class RepeatedFieldBuilder ensureMutableMessageList(); messages.remove(index); if (builders != null) { - SingleFieldBuilder entry = - builders.remove(index); + SingleFieldBuilder entry = builders.remove(index); if (entry != null) { entry.dispose(); } @@ -450,16 +426,12 @@ public class RepeatedFieldBuilder incrementModCounts(); } - /** - * Removes all of the elements from this list. - * The list will be empty after this call returns. - */ + /** Removes all of the elements from this list. The list will be empty after this call returns. */ public void clear() { messages = Collections.emptyList(); isMessagesListMutable = false; if (builders != null) { - for (SingleFieldBuilder entry : - builders) { + for (SingleFieldBuilder entry : builders) { if (entry != null) { entry.dispose(); } @@ -519,50 +491,47 @@ public class RepeatedFieldBuilder } /** - * Gets a view of the builder as a list of messages. The returned list is live - * and will reflect any changes to the underlying builder. + * Gets a view of the builder as a list of messages. The returned list is live and will reflect + * any changes to the underlying builder. * * @return the messages in the list */ public List getMessageList() { if (externalMessageList == null) { - externalMessageList = - new MessageExternalList(this); + externalMessageList = new MessageExternalList(this); } return externalMessageList; } /** - * Gets a view of the builder as a list of builders. This returned list is - * live and will reflect any changes to the underlying builder. + * Gets a view of the builder as a list of builders. This returned list is live and will reflect + * any changes to the underlying builder. * * @return the builders in the list */ public List getBuilderList() { if (externalBuilderList == null) { - externalBuilderList = - new BuilderExternalList(this); + externalBuilderList = new BuilderExternalList(this); } return externalBuilderList; } /** - * Gets a view of the builder as a list of MessageOrBuilders. This returned - * list is live and will reflect any changes to the underlying builder. + * Gets a view of the builder as a list of MessageOrBuilders. This returned list is live and will + * reflect any changes to the underlying builder. * * @return the builders in the list */ public List getMessageOrBuilderList() { if (externalMessageOrBuilderList == null) { - externalMessageOrBuilderList = - new MessageOrBuilderExternalList(this); + externalMessageOrBuilderList = new MessageOrBuilderExternalList(this); } return externalMessageOrBuilderList; } /** - * Called when a the builder or one of its nested children has changed - * and any parent should be notified of its invalidation. + * Called when a the builder or one of its nested children has changed and any parent should be + * notified of its invalidation. */ private void onChanged() { if (isClean && parent != null) { @@ -579,9 +548,8 @@ public class RepeatedFieldBuilder } /** - * Increments the mod counts so that an ConcurrentModificationException can - * be thrown if calling code tries to modify the builder while its iterating - * the list. + * Increments the mod counts so that an ConcurrentModificationException can be thrown if calling + * code tries to modify the builder while its iterating the list. */ private void incrementModCounts() { if (externalMessageList != null) { @@ -603,15 +571,14 @@ public class RepeatedFieldBuilder * @param the common interface for the message and the builder */ private static class MessageExternalList< - MType extends GeneratedMessage, - BType extends GeneratedMessage.Builder, - IType extends MessageOrBuilder> + MType extends GeneratedMessage, + BType extends GeneratedMessage.Builder, + IType extends MessageOrBuilder> extends AbstractList implements List { RepeatedFieldBuilder builder; - MessageExternalList( - RepeatedFieldBuilder builder) { + MessageExternalList(RepeatedFieldBuilder builder) { this.builder = builder; } @@ -638,15 +605,14 @@ public class RepeatedFieldBuilder * @param the common interface for the message and the builder */ private static class BuilderExternalList< - MType extends GeneratedMessage, - BType extends GeneratedMessage.Builder, - IType extends MessageOrBuilder> + MType extends GeneratedMessage, + BType extends GeneratedMessage.Builder, + IType extends MessageOrBuilder> extends AbstractList implements List { RepeatedFieldBuilder builder; - BuilderExternalList( - RepeatedFieldBuilder builder) { + BuilderExternalList(RepeatedFieldBuilder builder) { this.builder = builder; } @@ -673,15 +639,14 @@ public class RepeatedFieldBuilder * @param the common interface for the message and the builder */ private static class MessageOrBuilderExternalList< - MType extends GeneratedMessage, - BType extends GeneratedMessage.Builder, - IType extends MessageOrBuilder> + MType extends GeneratedMessage, + BType extends GeneratedMessage.Builder, + IType extends MessageOrBuilder> extends AbstractList implements List { RepeatedFieldBuilder builder; - MessageOrBuilderExternalList( - RepeatedFieldBuilder builder) { + MessageOrBuilderExternalList(RepeatedFieldBuilder builder) { this.builder = builder; } diff --git a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java index 30c991d40b..fb1667c4c6 100644 --- a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java +++ b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java @@ -39,36 +39,29 @@ import java.util.Collections; import java.util.List; /** - * {@code RepeatedFieldBuilderV3} implements a structure that a protocol - * message uses to hold a repeated field of other protocol messages. It supports - * the classical use case of adding immutable {@link Message}'s to the - * repeated field and is highly optimized around this (no extra memory - * allocations and sharing of immutable arrays). - *
- * It also supports the additional use case of adding a {@link Message.Builder} - * to the repeated field and deferring conversion of that {@code Builder} - * to an immutable {@code Message}. In this way, it's possible to maintain - * a tree of {@code Builder}'s that acts as a fully read/write data - * structure. - *
- * Logically, one can think of a tree of builders as converting the entire tree - * to messages when build is called on the root or when any method is called - * that desires a Message instead of a Builder. In terms of the implementation, - * the {@code SingleFieldBuilderV3} and {@code RepeatedFieldBuilderV3} - * classes cache messages that were created so that messages only need to be - * created when some change occurred in its builder or a builder for one of its - * descendants. + * {@code RepeatedFieldBuilderV3} implements a structure that a protocol message uses to hold a + * repeated field of other protocol messages. It supports the classical use case of adding immutable + * {@link Message}'s to the repeated field and is highly optimized around this (no extra memory + * allocations and sharing of immutable arrays).
+ * It also supports the additional use case of adding a {@link Message.Builder} to the repeated + * field and deferring conversion of that {@code Builder} to an immutable {@code Message}. In this + * way, it's possible to maintain a tree of {@code Builder}'s that acts as a fully read/write data + * structure.
+ * Logically, one can think of a tree of builders as converting the entire tree to messages when + * build is called on the root or when any method is called that desires a Message instead of a + * Builder. In terms of the implementation, the {@code SingleFieldBuilderV3} and {@code + * RepeatedFieldBuilderV3} classes cache messages that were created so that messages only need to be + * created when some change occurred in its builder or a builder for one of its descendants. * * @param the type of message for the field * @param the type of builder for the field * @param the common interface for the message and the builder - * * @author jonp@google.com (Jon Perlow) */ -public class RepeatedFieldBuilderV3 - +public class RepeatedFieldBuilderV3< + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, + IType extends MessageOrBuilder> implements AbstractMessage.BuilderParent { // Parent to send changes to. @@ -120,8 +113,7 @@ public class RepeatedFieldBuilderV3 // is fully backed by this object and all changes are reflected in it. // Access to any item returns either a builder or message depending on // what is most efficient. - private MessageOrBuilderExternalList - externalMessageOrBuilderList; + private MessageOrBuilderExternalList externalMessageOrBuilderList; /** * Constructs a new builder with an empty list of messages. @@ -148,8 +140,8 @@ public class RepeatedFieldBuilderV3 } /** - * Ensures that the list of messages is mutable so it can be updated. If it's - * immutable, a copy is made. + * Ensures that the list of messages is mutable so it can be updated. If it's immutable, a copy is + * made. */ private void ensureMutableMessageList() { if (!isMessagesListMutable) { @@ -159,15 +151,12 @@ public class RepeatedFieldBuilderV3 } /** - * Ensures that the list of builders is not null. If it's null, the list is - * created and initialized to be the same size as the messages list with - * null entries. + * Ensures that the list of builders is not null. If it's null, the list is created and + * initialized to be the same size as the messages list with null entries. */ private void ensureBuilders() { if (this.builders == null) { - this.builders = - new ArrayList>( - messages.size()); + this.builders = new ArrayList>(messages.size()); for (int i = 0; i < messages.size(); i++) { builders.add(null); } @@ -193,9 +182,9 @@ public class RepeatedFieldBuilderV3 } /** - * Get the message at the specified index. If the message is currently stored - * as a {@code Builder}, it is converted to a {@code Message} by - * calling {@link Message.Builder#buildPartial} on it. + * Get the message at the specified index. If the message is currently stored as a {@code + * Builder}, it is converted to a {@code Message} by calling {@link Message.Builder#buildPartial} + * on it. * * @param index the index of the message to get * @return the message for the specified index @@ -205,13 +194,13 @@ public class RepeatedFieldBuilderV3 } /** - * Get the message at the specified index. If the message is currently stored - * as a {@code Builder}, it is converted to a {@code Message} by - * calling {@link Message.Builder#buildPartial} on it. + * Get the message at the specified index. If the message is currently stored as a {@code + * Builder}, it is converted to a {@code Message} by calling {@link Message.Builder#buildPartial} + * on it. * * @param index the index of the message to get - * @param forBuild this is being called for build so we want to make sure - * we SingleFieldBuilderV3.build to send dirty invalidations + * @param forBuild this is being called for build so we want to make sure we + * SingleFieldBuilderV3.build to send dirty invalidations * @return the message for the specified index */ private MType getMessage(int index, boolean forBuild) { @@ -235,9 +224,8 @@ public class RepeatedFieldBuilderV3 } /** - * Gets a builder for the specified index. If no builder has been created for - * that index, a builder is created on demand by calling - * {@link Message#toBuilder}. + * Gets a builder for the specified index. If no builder has been created for that index, a + * builder is created on demand by calling {@link Message#toBuilder}. * * @param index the index of the message to get * @return The builder for that index @@ -247,16 +235,15 @@ public class RepeatedFieldBuilderV3 SingleFieldBuilderV3 builder = builders.get(index); if (builder == null) { MType message = messages.get(index); - builder = new SingleFieldBuilderV3( - message, this, isClean); + builder = new SingleFieldBuilderV3(message, this, isClean); builders.set(index, builder); } return builder.getBuilder(); } /** - * Gets the base class interface for the specified index. This may either be - * a builder or a message. It will return whatever is more efficient. + * Gets the base class interface for the specified index. This may either be a builder or a + * message. It will return whatever is more efficient. * * @param index the index of the message to get * @return the message or builder for the index as the base class interface @@ -283,21 +270,18 @@ public class RepeatedFieldBuilderV3 } /** - * Sets a message at the specified index replacing the existing item at - * that index. + * Sets a message at the specified index replacing the existing item at that index. * * @param index the index to set. * @param message the message to set * @return the builder */ - public RepeatedFieldBuilderV3 setMessage( - int index, MType message) { + public RepeatedFieldBuilderV3 setMessage(int index, MType message) { checkNotNull(message); ensureMutableMessageList(); messages.set(index, message); if (builders != null) { - SingleFieldBuilderV3 entry = - builders.set(index, null); + SingleFieldBuilderV3 entry = builders.set(index, null); if (entry != null) { entry.dispose(); } @@ -313,8 +297,7 @@ public class RepeatedFieldBuilderV3 * @param message the message to add * @return the builder */ - public RepeatedFieldBuilderV3 addMessage( - MType message) { + public RepeatedFieldBuilderV3 addMessage(MType message) { checkNotNull(message); ensureMutableMessageList(); messages.add(message); @@ -327,16 +310,15 @@ public class RepeatedFieldBuilderV3 } /** - * Inserts the specified message at the specified position in this list. - * Shifts the element currently at that position (if any) and any subsequent - * elements to the right (adds one to their indices). + * Inserts the specified message at the specified position in this list. Shifts the element + * currently at that position (if any) and any subsequent elements to the right (adds one to their + * indices). * * @param index the index at which to insert the message * @param message the message to add * @return the builder */ - public RepeatedFieldBuilderV3 addMessage( - int index, MType message) { + public RepeatedFieldBuilderV3 addMessage(int index, MType message) { checkNotNull(message); ensureMutableMessageList(); messages.add(index, message); @@ -349,9 +331,8 @@ public class RepeatedFieldBuilderV3 } /** - * Appends all of the messages in the specified collection to the end of - * this list, in the order that they are returned by the specified - * collection's iterator. + * Appends all of the messages in the specified collection to the end of this list, in the order + * that they are returned by the specified collection's iterator. * * @param values the messages to add * @return the builder @@ -365,8 +346,8 @@ public class RepeatedFieldBuilderV3 // If we can inspect the size, we can more efficiently add messages. int size = -1; if (values instanceof Collection) { - @SuppressWarnings("unchecked") final - Collection collection = (Collection) values; + @SuppressWarnings("unchecked") + final Collection collection = (Collection) values; if (collection.size() == 0) { return this; } @@ -375,8 +356,7 @@ public class RepeatedFieldBuilderV3 ensureMutableMessageList(); if (size >= 0 && messages instanceof ArrayList) { - ((ArrayList) messages) - .ensureCapacity(messages.size() + size); + ((ArrayList) messages).ensureCapacity(messages.size() + size); } for (MType value : values) { @@ -398,8 +378,7 @@ public class RepeatedFieldBuilderV3 ensureMutableMessageList(); ensureBuilders(); SingleFieldBuilderV3 builder = - new SingleFieldBuilderV3( - message, this, isClean); + new SingleFieldBuilderV3(message, this, isClean); messages.add(null); builders.add(builder); onChanged(); @@ -408,9 +387,8 @@ public class RepeatedFieldBuilderV3 } /** - * Inserts a new builder at the specified position in this list. - * Shifts the element currently at that position (if any) and any subsequent - * elements to the right (adds one to their indices). + * Inserts a new builder at the specified position in this list. Shifts the element currently at + * that position (if any) and any subsequent elements to the right (adds one to their indices). * * @param index the index at which to insert the builder * @param message the message to add which is the basis of the builder @@ -420,8 +398,7 @@ public class RepeatedFieldBuilderV3 ensureMutableMessageList(); ensureBuilders(); SingleFieldBuilderV3 builder = - new SingleFieldBuilderV3( - message, this, isClean); + new SingleFieldBuilderV3(message, this, isClean); messages.add(index, null); builders.add(index, builder); onChanged(); @@ -430,9 +407,9 @@ public class RepeatedFieldBuilderV3 } /** - * Removes the element at the specified position in this list. Shifts any - * subsequent elements to the left (subtracts one from their indices). - * Returns the element that was removed from the list. + * Removes the element at the specified position in this list. Shifts any subsequent elements to + * the left (subtracts one from their indices). Returns the element that was removed from the + * list. * * @param index the index at which to remove the message */ @@ -440,8 +417,7 @@ public class RepeatedFieldBuilderV3 ensureMutableMessageList(); messages.remove(index); if (builders != null) { - SingleFieldBuilderV3 entry = - builders.remove(index); + SingleFieldBuilderV3 entry = builders.remove(index); if (entry != null) { entry.dispose(); } @@ -450,16 +426,12 @@ public class RepeatedFieldBuilderV3 incrementModCounts(); } - /** - * Removes all of the elements from this list. - * The list will be empty after this call returns. - */ + /** Removes all of the elements from this list. The list will be empty after this call returns. */ public void clear() { messages = Collections.emptyList(); isMessagesListMutable = false; if (builders != null) { - for (SingleFieldBuilderV3 entry : - builders) { + for (SingleFieldBuilderV3 entry : builders) { if (entry != null) { entry.dispose(); } @@ -519,50 +491,47 @@ public class RepeatedFieldBuilderV3 } /** - * Gets a view of the builder as a list of messages. The returned list is live - * and will reflect any changes to the underlying builder. + * Gets a view of the builder as a list of messages. The returned list is live and will reflect + * any changes to the underlying builder. * * @return the messages in the list */ public List getMessageList() { if (externalMessageList == null) { - externalMessageList = - new MessageExternalList(this); + externalMessageList = new MessageExternalList(this); } return externalMessageList; } /** - * Gets a view of the builder as a list of builders. This returned list is - * live and will reflect any changes to the underlying builder. + * Gets a view of the builder as a list of builders. This returned list is live and will reflect + * any changes to the underlying builder. * * @return the builders in the list */ public List getBuilderList() { if (externalBuilderList == null) { - externalBuilderList = - new BuilderExternalList(this); + externalBuilderList = new BuilderExternalList(this); } return externalBuilderList; } /** - * Gets a view of the builder as a list of MessageOrBuilders. This returned - * list is live and will reflect any changes to the underlying builder. + * Gets a view of the builder as a list of MessageOrBuilders. This returned list is live and will + * reflect any changes to the underlying builder. * * @return the builders in the list */ public List getMessageOrBuilderList() { if (externalMessageOrBuilderList == null) { - externalMessageOrBuilderList = - new MessageOrBuilderExternalList(this); + externalMessageOrBuilderList = new MessageOrBuilderExternalList(this); } return externalMessageOrBuilderList; } /** - * Called when a the builder or one of its nested children has changed - * and any parent should be notified of its invalidation. + * Called when a the builder or one of its nested children has changed and any parent should be + * notified of its invalidation. */ private void onChanged() { if (isClean && parent != null) { @@ -579,9 +548,8 @@ public class RepeatedFieldBuilderV3 } /** - * Increments the mod counts so that an ConcurrentModificationException can - * be thrown if calling code tries to modify the builder while its iterating - * the list. + * Increments the mod counts so that an ConcurrentModificationException can be thrown if calling + * code tries to modify the builder while its iterating the list. */ private void incrementModCounts() { if (externalMessageList != null) { @@ -603,15 +571,14 @@ public class RepeatedFieldBuilderV3 * @param the common interface for the message and the builder */ private static class MessageExternalList< - MType extends AbstractMessage, - BType extends AbstractMessage.Builder, - IType extends MessageOrBuilder> + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, + IType extends MessageOrBuilder> extends AbstractList implements List { RepeatedFieldBuilderV3 builder; - MessageExternalList( - RepeatedFieldBuilderV3 builder) { + MessageExternalList(RepeatedFieldBuilderV3 builder) { this.builder = builder; } @@ -638,15 +605,14 @@ public class RepeatedFieldBuilderV3 * @param the common interface for the message and the builder */ private static class BuilderExternalList< - MType extends AbstractMessage, - BType extends AbstractMessage.Builder, - IType extends MessageOrBuilder> + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, + IType extends MessageOrBuilder> extends AbstractList implements List { RepeatedFieldBuilderV3 builder; - BuilderExternalList( - RepeatedFieldBuilderV3 builder) { + BuilderExternalList(RepeatedFieldBuilderV3 builder) { this.builder = builder; } @@ -673,15 +639,14 @@ public class RepeatedFieldBuilderV3 * @param the common interface for the message and the builder */ private static class MessageOrBuilderExternalList< - MType extends AbstractMessage, - BType extends AbstractMessage.Builder, - IType extends MessageOrBuilder> + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, + IType extends MessageOrBuilder> extends AbstractList implements List { RepeatedFieldBuilderV3 builder; - MessageOrBuilderExternalList( - RepeatedFieldBuilderV3 builder) { + MessageOrBuilderExternalList(RepeatedFieldBuilderV3 builder) { this.builder = builder; } diff --git a/java/core/src/main/java/com/google/protobuf/RopeByteString.java b/java/core/src/main/java/com/google/protobuf/RopeByteString.java index 6fa555df15..e031839573 100644 --- a/java/core/src/main/java/com/google/protobuf/RopeByteString.java +++ b/java/core/src/main/java/com/google/protobuf/RopeByteString.java @@ -38,81 +38,97 @@ import java.io.ObjectInputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import java.util.Stack; /** - * Class to represent {@code ByteStrings} formed by concatenation of other - * ByteStrings, without copying the data in the pieces. The concatenation is - * represented as a tree whose leaf nodes are each a - * {@link com.google.protobuf.ByteString.LeafByteString}. + * Class to represent {@code ByteStrings} formed by concatenation of other ByteStrings, without + * copying the data in the pieces. The concatenation is represented as a tree whose leaf nodes are + * each a {@link com.google.protobuf.ByteString.LeafByteString}. * *

Most of the operation here is inspired by the now-famous paper - * BAP95 Ropes: an Alternative to Strings hans-j. boehm, russ atkinson and - * michael plass + * BAP95 Ropes: an Alternative to Strings hans-j. boehm, russ atkinson and michael plass * - *

The algorithms described in the paper have been implemented for character - * strings in {@code com.google.common.string.Rope} and in the c++ class {@code - * cord.cc}. + *

The algorithms described in the paper have been implemented for character strings in {@code + * com.google.common.string.Rope} and in the c++ class {@code cord.cc}. * - *

Fundamentally the Rope algorithm represents the collection of pieces as a - * binary tree. BAP95 uses a Fibonacci bound relating depth to a minimum - * sequence length, sequences that are too short relative to their depth cause a - * tree rebalance. More precisely, a tree of depth d is "balanced" in the - * terminology of BAP95 if its length is at least F(d+2), where F(n) is the - * n-the Fibonacci number. Thus for depths 0, 1, 2, 3, 4, 5,... we have minimum - * lengths 1, 2, 3, 5, 8, 13,... + *

Fundamentally the Rope algorithm represents the collection of pieces as a binary tree. BAP95 + * uses a Fibonacci bound relating depth to a minimum sequence length, sequences that are too short + * relative to their depth cause a tree rebalance. More precisely, a tree of depth d is "balanced" + * in the terminology of BAP95 if its length is at least F(d+2), where F(n) is the n-th Fibonacci + * number. Thus for depths 0, 1, 2, 3, 4, 5,... we have minimum lengths 1, 2, 3, 5, 8, 13,... * * @author carlanton@google.com (Carl Haverl) */ final class RopeByteString extends ByteString { /** - * BAP95. Let Fn be the nth Fibonacci number. A {@link RopeByteString} of - * depth n is "balanced", i.e flat enough, if its length is at least Fn+2, - * e.g. a "balanced" {@link RopeByteString} of depth 1 must have length at - * least 2, of depth 4 must have length >= 8, etc. + * BAP95. Let Fn be the nth Fibonacci number. A {@link RopeByteString} of depth n is "balanced", + * i.e flat enough, if its length is at least Fn+2, e.g. a "balanced" {@link RopeByteString} of + * depth 1 must have length at least 2, of depth 4 must have length >= 8, etc. * - *

There's nothing special about using the Fibonacci numbers for this, but - * they are a reasonable sequence for encapsulating the idea that we are OK - * with longer strings being encoded in deeper binary trees. + *

There's nothing special about using the Fibonacci numbers for this, but they are a + * reasonable sequence for encapsulating the idea that we are OK with longer strings being encoded + * in deeper binary trees. * *

For 32-bit integers, this array has length 46. + * + *

The correctness of this constant array is validated in tests. */ - private static final int[] minLengthByDepth; - - static { - // Dynamically generate the list of Fibonacci numbers the first time this - // class is accessed. - List numbers = new ArrayList(); - - // we skip the first Fibonacci number (1). So instead of: 1 1 2 3 5 8 ... - // we have: 1 2 3 5 8 ... - int f1 = 1; - int f2 = 1; - - // get all the values until we roll over. - while (f2 > 0) { - numbers.add(f2); - int temp = f1 + f2; - f1 = f2; - f2 = temp; - } - - // we include this here so that we can index this array to [x + 1] in the - // loops below. - numbers.add(Integer.MAX_VALUE); - minLengthByDepth = new int[numbers.size()]; - for (int i = 0; i < minLengthByDepth.length; i++) { - // unbox all the values - minLengthByDepth[i] = numbers.get(i); - } - } + static final int[] minLengthByDepth = { + 1, + 1, + 2, + 3, + 5, + 8, + 13, + 21, + 34, + 55, + 89, + 144, + 233, + 377, + 610, + 987, + 1597, + 2584, + 4181, + 6765, + 10946, + 17711, + 28657, + 46368, + 75025, + 121393, + 196418, + 317811, + 514229, + 832040, + 1346269, + 2178309, + 3524578, + 5702887, + 9227465, + 14930352, + 24157817, + 39088169, + 63245986, + 102334155, + 165580141, + 267914296, + 433494437, + 701408733, + 1134903170, + 1836311903, + Integer.MAX_VALUE + }; private final int totalLength; private final ByteString left; @@ -121,13 +137,11 @@ final class RopeByteString extends ByteString { private final int treeDepth; /** - * Create a new RopeByteString, which can be thought of as a new tree node, by - * recording references to the two given strings. + * Create a new RopeByteString, which can be thought of as a new tree node, by recording + * references to the two given strings. * - * @param left string on the left of this node, should have {@code size() > - * 0} - * @param right string on the right of this node, should have {@code size() > - * 0} + * @param left string on the left of this node, should have {@code size() > 0} + * @param right string on the right of this node, should have {@code size() > 0} */ private RopeByteString(ByteString left, ByteString right) { this.left = left; @@ -138,17 +152,15 @@ final class RopeByteString extends ByteString { } /** - * Concatenate the given strings while performing various optimizations to - * slow the growth rate of tree depth and tree node count. The result is - * either a {@link com.google.protobuf.ByteString.LeafByteString} or a - * {@link RopeByteString} depending on which optimizations, if any, were - * applied. + * Concatenate the given strings while performing various optimizations to slow the growth rate of + * tree depth and tree node count. The result is either a {@link + * com.google.protobuf.ByteString.LeafByteString} or a {@link RopeByteString} depending on which + * optimizations, if any, were applied. * - *

Small pieces of length less than {@link - * ByteString#CONCATENATE_BY_COPY_SIZE} may be copied by value here, as in - * BAP95. Large pieces are referenced without copy. + *

Small pieces of length less than {@link ByteString#CONCATENATE_BY_COPY_SIZE} may be copied + * by value here, as in BAP95. Large pieces are referenced without copy. * - * @param left string on the left + * @param left string on the left * @param right string on the right * @return concatenation representing the same sequence as the given strings */ @@ -208,31 +220,29 @@ final class RopeByteString extends ByteString { } /** - * Concatenates two strings by copying data values. This is called in a few - * cases in order to reduce the growth of the number of tree nodes. + * Concatenates two strings by copying data values. This is called in a few cases in order to + * reduce the growth of the number of tree nodes. * - * @param left string on the left + * @param left string on the left * @param right string on the right * @return string formed by copying data bytes */ - private static ByteString concatenateBytes(ByteString left, - ByteString right) { + private static ByteString concatenateBytes(ByteString left, ByteString right) { int leftSize = left.size(); int rightSize = right.size(); byte[] bytes = new byte[leftSize + rightSize]; left.copyTo(bytes, 0, 0, leftSize); right.copyTo(bytes, 0, leftSize, rightSize); - return ByteString.wrap(bytes); // Constructor wraps bytes + return ByteString.wrap(bytes); // Constructor wraps bytes } /** - * Create a new RopeByteString for testing only while bypassing all the - * defenses of {@link #concatenate(ByteString, ByteString)}. This allows - * testing trees of specific structure. We are also able to insert empty - * leaves, though these are dis-allowed, so that we can make sure the + * Create a new RopeByteString for testing only while bypassing all the defenses of {@link + * #concatenate(ByteString, ByteString)}. This allows testing trees of specific structure. We are + * also able to insert empty leaves, though these are dis-allowed, so that we can make sure the * implementation can withstand their presence. * - * @param left string on the left of this node + * @param left string on the left of this node * @param right string on the right of this node * @return an unsafe instance for testing only */ @@ -241,9 +251,8 @@ final class RopeByteString extends ByteString { } /** - * Gets the byte at the given index. - * Throws {@link ArrayIndexOutOfBoundsException} for backwards-compatibility - * reasons although it would more properly be {@link + * Gets the byte at the given index. Throws {@link ArrayIndexOutOfBoundsException} for + * backwards-compatibility reasons although it would more properly be {@link * IndexOutOfBoundsException}. * * @param index index of byte @@ -253,13 +262,17 @@ final class RopeByteString extends ByteString { @Override public byte byteAt(int index) { checkIndex(index, totalLength); + return internalByteAt(index); + } + @Override + byte internalByteAt(int index) { // Find the relevant piece by recursive descent if (index < leftLength) { - return left.byteAt(index); + return left.internalByteAt(index); } - return right.byteAt(index - leftLength); + return right.internalByteAt(index - leftLength); } @Override @@ -267,6 +280,37 @@ final class RopeByteString extends ByteString { return totalLength; } + @Override + public ByteIterator iterator() { + return new AbstractByteIterator() { + final PieceIterator pieces = new PieceIterator(RopeByteString.this); + ByteIterator current = nextPiece(); + + private ByteIterator nextPiece() { + // NOTE: PieceIterator is guaranteed to return non-empty pieces, so this method will always + // return non-empty iterators (or null) + return pieces.hasNext() ? pieces.next().iterator() : null; + } + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public byte nextByte() { + if (current == null) { + throw new NoSuchElementException(); + } + byte b = current.nextByte(); + if (!current.hasNext()) { + current = nextPiece(); + } + return b; + } + }; + } + // ================================================================= // Pieces @@ -276,10 +320,9 @@ final class RopeByteString extends ByteString { } /** - * Determines if the tree is balanced according to BAP95, which means the tree - * is flat-enough with respect to the bounds. Note that this definition of - * balanced is one where sub-trees of balanced trees are not necessarily - * balanced. + * Determines if the tree is balanced according to BAP95, which means the tree is flat-enough with + * respect to the bounds. Note that this definition of balanced is one where sub-trees of balanced + * trees are not necessarily balanced. * * @return true if the tree is balanced */ @@ -289,17 +332,16 @@ final class RopeByteString extends ByteString { } /** - * Takes a substring of this one. This involves recursive descent along the - * left and right edges of the substring, and referencing any wholly contained - * segments in between. Any leaf nodes entirely uninvolved in the substring - * will not be referenced by the substring. + * Takes a substring of this one. This involves recursive descent along the left and right edges + * of the substring, and referencing any wholly contained segments in between. Any leaf nodes + * entirely uninvolved in the substring will not be referenced by the substring. * - *

Substrings of {@code length < 2} should result in at most a single - * recursive call chain, terminating at a leaf node. Thus the result will be a - * {@link com.google.protobuf.ByteString.LeafByteString}. + *

Substrings of {@code length < 2} should result in at most a single recursive call chain, + * terminating at a leaf node. Thus the result will be a {@link + * com.google.protobuf.ByteString.LeafByteString}. * * @param beginIndex start at this index - * @param endIndex the last character is the one before this index + * @param endIndex the last character is the one before this index * @return substring leaf node or tree */ @Override @@ -340,18 +382,16 @@ final class RopeByteString extends ByteString { // ByteString -> byte[] @Override - protected void copyToInternal(byte[] target, int sourceOffset, - int targetOffset, int numberToCopy) { - if (sourceOffset + numberToCopy <= leftLength) { + protected void copyToInternal( + byte[] target, int sourceOffset, int targetOffset, int numberToCopy) { + if (sourceOffset + numberToCopy <= leftLength) { left.copyToInternal(target, sourceOffset, targetOffset, numberToCopy); } else if (sourceOffset >= leftLength) { - right.copyToInternal(target, sourceOffset - leftLength, targetOffset, - numberToCopy); + right.copyToInternal(target, sourceOffset - leftLength, targetOffset, numberToCopy); } else { int leftLength = this.leftLength - sourceOffset; left.copyToInternal(target, sourceOffset, targetOffset, leftLength); - right.copyToInternal(target, 0, targetOffset + leftLength, - numberToCopy - leftLength); + right.copyToInternal(target, 0, targetOffset + leftLength, numberToCopy - leftLength); } } @@ -387,8 +427,7 @@ final class RopeByteString extends ByteString { } @Override - void writeToInternal(OutputStream out, int sourceOffset, - int numberToWrite) throws IOException { + void writeToInternal(OutputStream out, int sourceOffset, int numberToWrite) throws IOException { if (sourceOffset + numberToWrite <= leftLength) { left.writeToInternal(out, sourceOffset, numberToWrite); } else if (sourceOffset >= leftLength) { @@ -406,6 +445,11 @@ final class RopeByteString extends ByteString { right.writeTo(output); } + @Override + void writeToReverse(ByteOutput output) throws IOException { + right.writeToReverse(output); + left.writeToReverse(output); + } @Override protected String toStringInternal(Charset charset) { @@ -471,13 +515,11 @@ final class RopeByteString extends ByteString { } /** - * Determines if this string is equal to another of the same length by - * iterating over the leaf nodes. On each step of the iteration, the - * overlapping segments of the leaves are compared. + * Determines if this string is equal to another of the same length by iterating over the leaf + * nodes. On each step of the iteration, the overlapping segments of the leaves are compared. * * @param other string of the same length as this one - * @return true if the values of this string equals the value of the given - * one + * @return true if the values of this string equals the value of the given one */ private boolean equalsFragments(ByteString other) { int thisOffset = 0; @@ -495,9 +537,10 @@ final class RopeByteString extends ByteString { int bytesToCompare = Math.min(thisRemaining, thatRemaining); // At least one of the offsets will be zero - boolean stillEqual = (thisOffset == 0) - ? thisString.equalsRange(thatString, thatOffset, bytesToCompare) - : thatString.equalsRange(thisString, thisOffset, bytesToCompare); + boolean stillEqual = + (thisOffset == 0) + ? thisString.equalsRange(thatString, thatOffset, bytesToCompare) + : thatString.equalsRange(thisString, thisOffset, bytesToCompare); if (!stillEqual) { return false; } @@ -553,20 +596,19 @@ final class RopeByteString extends ByteString { } /** - * This class implements the balancing algorithm of BAP95. In the paper the - * authors use an array to keep track of pieces, while here we use a stack. - * The tree is balanced by traversing subtrees in left to right order, and the - * stack always contains the part of the string we've traversed so far. + * This class implements the balancing algorithm of BAP95. In the paper the authors use an array + * to keep track of pieces, while here we use a stack. The tree is balanced by traversing subtrees + * in left to right order, and the stack always contains the part of the string we've traversed so + * far. * - *

One surprising aspect of the algorithm is the result of balancing is not - * necessarily balanced, though it is nearly balanced. For details, see - * BAP95. + *

One surprising aspect of the algorithm is the result of balancing is not necessarily + * balanced, though it is nearly balanced. For details, see BAP95. */ private static class Balancer { // Stack containing the part of the string, starting from the left, that // we've already traversed. The final string should be the equivalent of // concatenating the strings on the stack from bottom to top. - private final Stack prefixesStack = new Stack(); + private final ArrayDeque prefixesStack = new ArrayDeque<>(); private ByteString balance(ByteString left, ByteString right) { doBalance(left); @@ -596,21 +638,18 @@ final class RopeByteString extends ByteString { doBalance(rbs.right); } else { throw new IllegalArgumentException( - "Has a new type of ByteString been created? Found " + - root.getClass()); + "Has a new type of ByteString been created? Found " + root.getClass()); } } /** - * Push a string on the balance stack (BAP95). BAP95 uses an array and - * calls the elements in the array 'bins'. We instead use a stack, so the - * 'bins' of lengths are represented by differences between the elements of - * minLengthByDepth. + * Push a string on the balance stack (BAP95). BAP95 uses an array and calls the elements in the + * array 'bins'. We instead use a stack, so the 'bins' of lengths are represented by differences + * between the elements of minLengthByDepth. * - *

If the length bin for our string, and all shorter length bins, are - * empty, we just push it on the stack. Otherwise, we need to start - * concatenating, putting the given string in the "middle" and continuing - * until we land in an empty length bin that matches the length of our + *

If the length bin for our string, and all shorter length bins, are empty, we just push it + * on the stack. Otherwise, we need to start concatenating, putting the given string in the + * "middle" and continuing until we land in an empty length bin that matches the length of our * concatenation. * * @param byteString string to place on the balance stack @@ -630,8 +669,7 @@ final class RopeByteString extends ByteString { // Concatenate the subtrees of shorter length ByteString newTree = prefixesStack.pop(); - while (!prefixesStack.isEmpty() - && prefixesStack.peek().size() < binStart) { + while (!prefixesStack.isEmpty() && prefixesStack.peek().size() < binStart) { ByteString left = prefixesStack.pop(); newTree = new RopeByteString(left, newTree); } @@ -668,22 +706,26 @@ final class RopeByteString extends ByteString { } /** - * This class is a continuable tree traversal, which keeps the state - * information which would exist on the stack in a recursive traversal instead - * on a stack of "Bread Crumbs". The maximum depth of the stack in this - * iterator is the same as the depth of the tree being traversed. + * This class is a continuable tree traversal, which keeps the state information which would exist + * on the stack in a recursive traversal instead on a stack of "Bread Crumbs". The maximum depth + * of the stack in this iterator is the same as the depth of the tree being traversed. * - *

This iterator is used to implement - * {@link RopeByteString#equalsFragments(ByteString)}. + *

This iterator is used to implement {@link RopeByteString#equalsFragments(ByteString)}. */ - private static class PieceIterator implements Iterator { - - private final Stack breadCrumbs = - new Stack(); + private static final class PieceIterator implements Iterator { + private final ArrayDeque breadCrumbs; private LeafByteString next; private PieceIterator(ByteString root) { - next = getLeafByLeft(root); + if (root instanceof RopeByteString) { + RopeByteString rbs = (RopeByteString) root; + breadCrumbs = new ArrayDeque<>(rbs.getTreeDepth()); + breadCrumbs.push(rbs); + next = getLeafByLeft(rbs.left); + } else { + breadCrumbs = null; + next = (LeafByteString) root; + } } private LeafByteString getLeafByLeft(ByteString root) { @@ -700,7 +742,7 @@ final class RopeByteString extends ByteString { while (true) { // Almost always, we go through this loop exactly once. However, if // we discover an empty string in the rope, we toss it and try again. - if (breadCrumbs.isEmpty()) { + if (breadCrumbs == null || breadCrumbs.isEmpty()) { return null; } else { LeafByteString result = getLeafByLeft(breadCrumbs.pop().right); @@ -717,8 +759,7 @@ final class RopeByteString extends ByteString { } /** - * Returns the next item and advances one - * {@link com.google.protobuf.ByteString.LeafByteString}. + * Returns the next item and advances one {@link com.google.protobuf.ByteString.LeafByteString}. * * @return next non-empty LeafByteString or {@code null} */ @@ -748,14 +789,10 @@ final class RopeByteString extends ByteString { } private void readObject(@SuppressWarnings("unused") ObjectInputStream in) throws IOException { - throw new InvalidObjectException( - "RopeByteStream instances are not to be serialized directly"); + throw new InvalidObjectException("RopeByteStream instances are not to be serialized directly"); } - /** - * This class is the {@link RopeByteString} equivalent for - * {@link ByteArrayInputStream}. - */ + /** This class is the {@link RopeByteString} equivalent for {@link ByteArrayInputStream}. */ private class RopeInputStream extends InputStream { // Iterates through the pieces of the rope private PieceIterator pieceIterator; @@ -775,7 +812,7 @@ final class RopeByteString extends ByteString { } @Override - public int read(byte b[], int offset, int length) { + public int read(byte[] b, int offset, int length) { if (b == null) { throw new NullPointerException(); } else if (offset < 0 || length < 0 || length > b.length - offset) { @@ -795,24 +832,23 @@ final class RopeByteString extends ByteString { } /** - * Internal implementation of read and skip. If b != null, then read the - * next {@code length} bytes into the buffer {@code b} at - * offset {@code offset}. If b == null, then skip the next {@code length} - * bytes. - *

- * This method assumes that all error checking has already happened. - *

- * Returns the actual number of bytes read or skipped. + * Internal implementation of read and skip. If b != null, then read the next {@code length} + * bytes into the buffer {@code b} at offset {@code offset}. If b == null, then skip the next + * {@code length} bytes. + * + *

This method assumes that all error checking has already happened. + * + *

Returns the actual number of bytes read or skipped. */ - private int readSkipInternal(byte b[], int offset, int length) { + private int readSkipInternal(byte[] b, int offset, int length) { int bytesRemaining = length; while (bytesRemaining > 0) { advanceIfCurrentPieceFullyRead(); if (currentPiece == null) { if (bytesRemaining == length) { - // We didn't manage to read anything - return -1; - } + // We didn't manage to read anything + return -1; + } break; } else { // Copy the bytes from this piece. @@ -826,7 +862,7 @@ final class RopeByteString extends ByteString { bytesRemaining -= count; } } - // Return the number of bytes read. + // Return the number of bytes read. return length - bytesRemaining; } @@ -874,9 +910,8 @@ final class RopeByteString extends ByteString { } /** - * Skips to the next piece if we have read all the data in the current - * piece. Sets currentPiece to null if we have reached the end of the - * input. + * Skips to the next piece if we have read all the data in the current piece. Sets currentPiece + * to null if we have reached the end of the input. */ private void advanceIfCurrentPieceFullyRead() { if (currentPiece != null && currentPieceIndex == currentPieceSize) { diff --git a/java/core/src/main/java/com/google/protobuf/RpcCallback.java b/java/core/src/main/java/com/google/protobuf/RpcCallback.java index 10752968e2..51a34b63d4 100644 --- a/java/core/src/main/java/com/google/protobuf/RpcCallback.java +++ b/java/core/src/main/java/com/google/protobuf/RpcCallback.java @@ -31,14 +31,13 @@ package com.google.protobuf; /** - * Interface for an RPC callback, normally called when an RPC completes. - * {@code ParameterType} is normally the method's response message type. + * Interface for an RPC callback, normally called when an RPC completes. {@code ParameterType} is + * normally the method's response message type. * - *

Starting with version 2.3.0, RPC implementations should not try to build - * on this, but should instead provide code generator plugins which generate - * code specific to the particular RPC implementation. This way the generated - * code can be more appropriate for the implementation in use and can avoid - * unnecessary layers of indirection. + *

Starting with version 2.3.0, RPC implementations should not try to build on this, but should + * instead provide code generator plugins which generate code specific to the particular RPC + * implementation. This way the generated code can be more appropriate for the implementation in use + * and can avoid unnecessary layers of indirection. * * @author kenton@google.com Kenton Varda */ diff --git a/java/core/src/main/java/com/google/protobuf/RpcChannel.java b/java/core/src/main/java/com/google/protobuf/RpcChannel.java index f272f4ad72..13b5ec165e 100644 --- a/java/core/src/main/java/com/google/protobuf/RpcChannel.java +++ b/java/core/src/main/java/com/google/protobuf/RpcChannel.java @@ -31,11 +31,10 @@ package com.google.protobuf; /** - *

Abstract interface for an RPC channel. An {@code RpcChannel} represents a - * communication line to a {@link Service} which can be used to call that - * {@link Service}'s methods. The {@link Service} may be running on another - * machine. Normally, you should not call an {@code RpcChannel} directly, but - * instead construct a stub {@link Service} wrapping it. Example: + * Abstract interface for an RPC channel. An {@code RpcChannel} represents a communication line to a + * {@link Service} which can be used to call that {@link Service}'s methods. The {@link Service} may + * be running on another machine. Normally, you should not call an {@code RpcChannel} directly, but + * instead construct a stub {@link Service} wrapping it. Example: * *

  *   RpcChannel channel = rpcImpl.newChannel("remotehost.example.com:1234");
@@ -44,28 +43,26 @@ package com.google.protobuf;
  *   service.myMethod(controller, request, callback);
  * 
* - *

Starting with version 2.3.0, RPC implementations should not try to build - * on this, but should instead provide code generator plugins which generate - * code specific to the particular RPC implementation. This way the generated - * code can be more appropriate for the implementation in use and can avoid - * unnecessary layers of indirection. + *

Starting with version 2.3.0, RPC implementations should not try to build on this, but should + * instead provide code generator plugins which generate code specific to the particular RPC + * implementation. This way the generated code can be more appropriate for the implementation in use + * and can avoid unnecessary layers of indirection. * * @author kenton@google.com Kenton Varda */ public interface RpcChannel { /** - * Call the given method of the remote service. This method is similar to - * {@code Service.callMethod()} with one important difference: the caller - * decides the types of the {@code Message} objects, not the callee. The - * request may be of any type as long as - * {@code request.getDescriptor() == method.getInputType()}. - * The response passed to the callback will be of the same type as - * {@code responsePrototype} (which must have - * {@code getDescriptor() == method.getOutputType()}). + * Call the given method of the remote service. This method is similar to {@code + * Service.callMethod()} with one important difference: the caller decides the types of the {@code + * Message} objects, not the callee. The request may be of any type as long as {@code + * request.getDescriptor() == method.getInputType()}. The response passed to the callback will be + * of the same type as {@code responsePrototype} (which must have {@code getDescriptor() == + * method.getOutputType()}). */ - void callMethod(Descriptors.MethodDescriptor method, - RpcController controller, - Message request, - Message responsePrototype, - RpcCallback done); + void callMethod( + Descriptors.MethodDescriptor method, + RpcController controller, + Message request, + Message responsePrototype, + RpcCallback done); } diff --git a/java/core/src/main/java/com/google/protobuf/RpcController.java b/java/core/src/main/java/com/google/protobuf/RpcController.java index a92dd7be6d..073f27a2aa 100644 --- a/java/core/src/main/java/com/google/protobuf/RpcController.java +++ b/java/core/src/main/java/com/google/protobuf/RpcController.java @@ -31,20 +31,18 @@ package com.google.protobuf; /** - *

An {@code RpcController} mediates a single method call. The primary - * purpose of the controller is to provide a way to manipulate settings - * specific to the RPC implementation and to find out about RPC-level errors. + * An {@code RpcController} mediates a single method call. The primary purpose of the controller is + * to provide a way to manipulate settings specific to the RPC implementation and to find out about + * RPC-level errors. * - *

Starting with version 2.3.0, RPC implementations should not try to build - * on this, but should instead provide code generator plugins which generate - * code specific to the particular RPC implementation. This way the generated - * code can be more appropriate for the implementation in use and can avoid - * unnecessary layers of indirection. + *

Starting with version 2.3.0, RPC implementations should not try to build on this, but should + * instead provide code generator plugins which generate code specific to the particular RPC + * implementation. This way the generated code can be more appropriate for the implementation in use + * and can avoid unnecessary layers of indirection. * - *

The methods provided by the {@code RpcController} interface are intended - * to be a "least common denominator" set of features which we expect all - * implementations to support. Specific implementations may provide more - * advanced features (e.g. deadline propagation). + *

The methods provided by the {@code RpcController} interface are intended to be a "least common + * denominator" set of features which we expect all implementations to support. Specific + * implementations may provide more advanced features (e.g. deadline propagation). * * @author kenton@google.com Kenton Varda */ @@ -54,31 +52,25 @@ public interface RpcController { // are undefined on the server side (may throw RuntimeExceptions). /** - * Resets the RpcController to its initial state so that it may be reused in - * a new call. This can be called from the client side only. It must not - * be called while an RPC is in progress. + * Resets the RpcController to its initial state so that it may be reused in a new call. This can + * be called from the client side only. It must not be called while an RPC is in progress. */ void reset(); /** - * After a call has finished, returns true if the call failed. The possible - * reasons for failure depend on the RPC implementation. {@code failed()} - * most only be called on the client side, and must not be called before a - * call has finished. + * After a call has finished, returns true if the call failed. The possible reasons for failure + * depend on the RPC implementation. {@code failed()} most only be called on the client side, and + * must not be called before a call has finished. */ boolean failed(); - /** - * If {@code failed()} is {@code true}, returns a human-readable description - * of the error. - */ + /** If {@code failed()} is {@code true}, returns a human-readable description of the error. */ String errorText(); /** - * Advises the RPC system that the caller desires that the RPC call be - * canceled. The RPC system may cancel it immediately, may wait awhile and - * then cancel it, or may not even cancel the call at all. If the call is - * canceled, the "done" callback will still be called and the RpcController + * Advises the RPC system that the caller desires that the RPC call be canceled. The RPC system + * may cancel it immediately, may wait awhile and then cancel it, or may not even cancel the call + * at all. If the call is canceled, the "done" callback will still be called and the RpcController * will indicate that the call failed at that time. */ void startCancel(); @@ -88,31 +80,29 @@ public interface RpcController { // are undefined on the client side (may throw RuntimeExceptions). /** - * Causes {@code failed()} to return true on the client side. {@code reason} - * will be incorporated into the message returned by {@code errorText()}. - * If you find you need to return machine-readable information about - * failures, you should incorporate it into your response protocol buffer - * and should NOT call {@code setFailed()}. + * Causes {@code failed()} to return true on the client side. {@code reason} will be incorporated + * into the message returned by {@code errorText()}. If you find you need to return + * machine-readable information about failures, you should incorporate it into your response + * protocol buffer and should NOT call {@code setFailed()}. */ void setFailed(String reason); /** - * If {@code true}, indicates that the client canceled the RPC, so the server - * may as well give up on replying to it. This method must be called on the - * server side only. The server should still call the final "done" callback. + * If {@code true}, indicates that the client canceled the RPC, so the server may as well give up + * on replying to it. This method must be called on the server side only. The server should still + * call the final "done" callback. */ boolean isCanceled(); /** - * Asks that the given callback be called when the RPC is canceled. The - * parameter passed to the callback will always be {@code null}. The - * callback will always be called exactly once. If the RPC completes without - * being canceled, the callback will be called after completion. If the RPC - * has already been canceled when NotifyOnCancel() is called, the callback - * will be called immediately. + * Asks that the given callback be called when the RPC is canceled. The parameter passed to the + * callback will always be {@code null}. The callback will always be called exactly once. If the + * RPC completes without being canceled, the callback will be called after completion. If the RPC + * has already been canceled when NotifyOnCancel() is called, the callback will be called + * immediately. * - *

{@code notifyOnCancel()} must be called no more than once per request. - * It must be called on the server side only. + *

{@code notifyOnCancel()} must be called no more than once per request. It must be called on + * the server side only. */ void notifyOnCancel(RpcCallback callback); } diff --git a/java/core/src/main/java/com/google/protobuf/RpcUtil.java b/java/core/src/main/java/com/google/protobuf/RpcUtil.java index f7d555ae15..7bd056a532 100644 --- a/java/core/src/main/java/com/google/protobuf/RpcUtil.java +++ b/java/core/src/main/java/com/google/protobuf/RpcUtil.java @@ -39,14 +39,13 @@ public final class RpcUtil { private RpcUtil() {} /** - * Take an {@code RpcCallback} and convert it to an - * {@code RpcCallback} accepting a specific message type. This is always - * type-safe (parameter type contravariance). + * Take an {@code RpcCallback} and convert it to an {@code RpcCallback} accepting a + * specific message type. This is always type-safe (parameter type contravariance). */ @SuppressWarnings("unchecked") - public static RpcCallback - specializeCallback(final RpcCallback originalCallback) { - return (RpcCallback)originalCallback; + public static RpcCallback specializeCallback( + final RpcCallback originalCallback) { + return (RpcCallback) originalCallback; // The above cast works, but only due to technical details of the Java // implementation. A more theoretically correct -- but less efficient -- // implementation would be as follows: @@ -58,15 +57,13 @@ public final class RpcUtil { } /** - * Take an {@code RpcCallback} accepting a specific message type and convert - * it to an {@code RpcCallback}. The generalized callback will - * accept any message object which has the same descriptor, and will convert - * it to the correct class before calling the original callback. However, - * if the generalized callback is given a message with a different descriptor, - * an exception will be thrown. + * Take an {@code RpcCallback} accepting a specific message type and convert it to an {@code + * RpcCallback}. The generalized callback will accept any message object which has the + * same descriptor, and will convert it to the correct class before calling the original callback. + * However, if the generalized callback is given a message with a different descriptor, an + * exception will be thrown. */ - public static - RpcCallback generalizeCallback( + public static RpcCallback generalizeCallback( final RpcCallback originalCallback, final Class originalClass, final Type defaultInstance) { @@ -85,25 +82,21 @@ public final class RpcUtil { } /** - * Creates a new message of type "Type" which is a copy of "source". "source" - * must have the same descriptor but may be a different class (e.g. - * DynamicMessage). + * Creates a new message of type "Type" which is a copy of "source". "source" must have the same + * descriptor but may be a different class (e.g. DynamicMessage). */ @SuppressWarnings("unchecked") private static Type copyAsType( final Type typeDefaultInstance, final Message source) { - return (Type) typeDefaultInstance - .newBuilderForType().mergeFrom(source).build(); + return (Type) typeDefaultInstance.newBuilderForType().mergeFrom(source).build(); } /** - * Creates a callback which can only be called once. This may be useful for - * security, when passing a callback to untrusted code: most callbacks do - * not expect to be called more than once, so doing so may expose bugs if it - * is not prevented. + * Creates a callback which can only be called once. This may be useful for security, when passing + * a callback to untrusted code: most callbacks do not expect to be called more than once, so + * doing so may expose bugs if it is not prevented. */ - public static - RpcCallback newOneTimeCallback( + public static RpcCallback newOneTimeCallback( final RpcCallback originalCallback) { return new RpcCallback() { private boolean alreadyCalled = false; @@ -122,15 +115,12 @@ public final class RpcUtil { }; } - /** - * Exception thrown when a one-time callback is called more than once. - */ + /** Exception thrown when a one-time callback is called more than once. */ public static final class AlreadyCalledException extends RuntimeException { private static final long serialVersionUID = 5469741279507848266L; public AlreadyCalledException() { - super("This RpcCallback was already called and cannot be called " + - "multiple times."); + super("This RpcCallback was already called and cannot be called multiple times."); } } } diff --git a/java/core/src/main/java/com/google/protobuf/Schema.java b/java/core/src/main/java/com/google/protobuf/Schema.java new file mode 100755 index 0000000000..d0e1e26e5b --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/Schema.java @@ -0,0 +1,85 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.ArrayDecoders.Registers; +import java.io.IOException; + +/** + * A runtime schema for a single protobuf message. A schema provides operations on message instances + * such as serialization/deserialization. + */ +@ExperimentalApi +interface Schema { + /** Writes the given message to the target {@link Writer}. */ + void writeTo(T message, Writer writer) throws IOException; + + /** + * Reads fields from the given {@link Reader} and merges them into the message. It doesn't make + * the message immutable after parsing is done. To make the message immutable, use {@link + * #makeImmutable}. + */ + void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Like the above but parses from a byte[] without extensions. Entry point of fast path. Note that + * this method may throw IndexOutOfBoundsException if the input data is not valid protobuf wire + * format. Protobuf public API methods should catch and convert that exception to + * InvalidProtocolBufferException. + */ + void mergeFrom(T message, byte[] data, int position, int limit, Registers registers) + throws IOException; + + /** Marks repeated/map/extension/unknown fields as immutable. */ + void makeImmutable(T message); + + /** Checks whether all required fields are set. */ + boolean isInitialized(T message); + + /** Creates a new instance of the message class. */ + T newInstance(); + + /** Determine of the two messages are equal. */ + boolean equals(T message, T other); + + /** Compute a hashCode for the message. */ + int hashCode(T message); + + /** + * Merge values from {@code other} into {@code message}. This method doesn't make the message + * immutable. To make the message immutable after merging, use {@link #makeImmutable}. + */ + void mergeFrom(T message, T other); + + /** Compute the serialized size of the message. */ + int getSerializedSize(T message); +} diff --git a/java/core/src/main/java/com/google/protobuf/SchemaFactory.java b/java/core/src/main/java/com/google/protobuf/SchemaFactory.java new file mode 100755 index 0000000000..cf38dd6994 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/SchemaFactory.java @@ -0,0 +1,38 @@ +// 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. + +package com.google.protobuf; + +/** A factory that manufactures {@link Schema} instances for protobuf messages. */ +@ExperimentalApi +interface SchemaFactory { + /** Creates a schema instance for the given protobuf message type. */ + Schema createSchema(Class messageType); +} diff --git a/java/core/src/main/java/com/google/protobuf/SchemaUtil.java b/java/core/src/main/java/com/google/protobuf/SchemaUtil.java new file mode 100755 index 0000000000..50957fcfdb --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/SchemaUtil.java @@ -0,0 +1,991 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.FieldSet.FieldDescriptorLite; +import com.google.protobuf.Internal.EnumLiteMap; +import com.google.protobuf.Internal.EnumVerifier; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Iterator; +import java.util.List; +import java.util.RandomAccess; + +/** Helper methods used by schemas. */ +@ExperimentalApi +final class SchemaUtil { + private static final Class GENERATED_MESSAGE_CLASS = getGeneratedMessageClass(); + private static final UnknownFieldSchema PROTO2_UNKNOWN_FIELD_SET_SCHEMA = + getUnknownFieldSetSchema(false); + private static final UnknownFieldSchema PROTO3_UNKNOWN_FIELD_SET_SCHEMA = + getUnknownFieldSetSchema(true); + private static final UnknownFieldSchema UNKNOWN_FIELD_SET_LITE_SCHEMA = + new UnknownFieldSetLiteSchema(); + + private static final int DEFAULT_LOOK_UP_START_NUMBER = 40; + + private SchemaUtil() {} + + /** + * Requires that the given message extend {@link com.google.protobuf.GeneratedMessageV3} or {@link + * GeneratedMessageLite}. + */ + public static void requireGeneratedMessage(Class messageType) { + if (!GeneratedMessageLite.class.isAssignableFrom(messageType) + && GENERATED_MESSAGE_CLASS != null + && !GENERATED_MESSAGE_CLASS.isAssignableFrom(messageType)) { + throw new IllegalArgumentException( + "Message classes must extend GeneratedMessage or GeneratedMessageLite"); + } + } + + public static void writeDouble(int fieldNumber, double value, Writer writer) throws IOException { + if (Double.compare(value, 0.0) != 0) { + writer.writeDouble(fieldNumber, value); + } + } + + public static void writeFloat(int fieldNumber, float value, Writer writer) throws IOException { + if (Float.compare(value, 0.0f) != 0) { + writer.writeFloat(fieldNumber, value); + } + } + + public static void writeInt64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeInt64(fieldNumber, value); + } + } + + public static void writeUInt64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeUInt64(fieldNumber, value); + } + } + + public static void writeSInt64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeSInt64(fieldNumber, value); + } + } + + public static void writeFixed64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeFixed64(fieldNumber, value); + } + } + + public static void writeSFixed64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeSFixed64(fieldNumber, value); + } + } + + public static void writeInt32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeInt32(fieldNumber, value); + } + } + + public static void writeUInt32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeUInt32(fieldNumber, value); + } + } + + public static void writeSInt32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeSInt32(fieldNumber, value); + } + } + + public static void writeFixed32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeFixed32(fieldNumber, value); + } + } + + public static void writeSFixed32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeSFixed32(fieldNumber, value); + } + } + + public static void writeEnum(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeEnum(fieldNumber, value); + } + } + + public static void writeBool(int fieldNumber, boolean value, Writer writer) throws IOException { + if (value) { + writer.writeBool(fieldNumber, true); + } + } + + public static void writeString(int fieldNumber, Object value, Writer writer) throws IOException { + if (value instanceof String) { + writeStringInternal(fieldNumber, (String) value, writer); + } else { + writeBytes(fieldNumber, (ByteString) value, writer); + } + } + + private static void writeStringInternal(int fieldNumber, String value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeString(fieldNumber, value); + } + } + + public static void writeBytes(int fieldNumber, ByteString value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeBytes(fieldNumber, value); + } + } + + public static void writeMessage(int fieldNumber, Object value, Writer writer) throws IOException { + if (value != null) { + writer.writeMessage(fieldNumber, value); + } + } + + public static void writeDoubleList( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeDoubleList(fieldNumber, value, packed); + } + } + + public static void writeFloatList( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeFloatList(fieldNumber, value, packed); + } + } + + public static void writeInt64List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeInt64List(fieldNumber, value, packed); + } + } + + public static void writeUInt64List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeUInt64List(fieldNumber, value, packed); + } + } + + public static void writeSInt64List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeSInt64List(fieldNumber, value, packed); + } + } + + public static void writeFixed64List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeFixed64List(fieldNumber, value, packed); + } + } + + public static void writeSFixed64List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeSFixed64List(fieldNumber, value, packed); + } + } + + public static void writeInt32List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeInt32List(fieldNumber, value, packed); + } + } + + public static void writeUInt32List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeUInt32List(fieldNumber, value, packed); + } + } + + public static void writeSInt32List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeSInt32List(fieldNumber, value, packed); + } + } + + public static void writeFixed32List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeFixed32List(fieldNumber, value, packed); + } + } + + public static void writeSFixed32List( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeSFixed32List(fieldNumber, value, packed); + } + } + + public static void writeEnumList( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeEnumList(fieldNumber, value, packed); + } + } + + public static void writeBoolList( + int fieldNumber, List value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeBoolList(fieldNumber, value, packed); + } + } + + public static void writeStringList(int fieldNumber, List value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeStringList(fieldNumber, value); + } + } + + public static void writeBytesList(int fieldNumber, List value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeBytesList(fieldNumber, value); + } + } + + public static void writeMessageList(int fieldNumber, List value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeMessageList(fieldNumber, value); + } + } + + public static void writeMessageList(int fieldNumber, List value, Writer writer, Schema schema) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeMessageList(fieldNumber, value, schema); + } + } + + public static void writeLazyFieldList(int fieldNumber, List value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + for (Object item : value) { + ((LazyFieldLite) item).writeTo(writer, fieldNumber); + } + } + } + + public static void writeGroupList(int fieldNumber, List value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeGroupList(fieldNumber, value); + } + } + + public static void writeGroupList(int fieldNumber, List value, Writer writer, Schema schema) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeGroupList(fieldNumber, value, schema); + } + } + + static int computeSizeInt64ListNoTag(List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof LongArrayList) { + final LongArrayList primitiveList = (LongArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeInt64SizeNoTag(primitiveList.getLong(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeInt64SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeInt64List(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeInt64ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (list.size() * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeUInt64ListNoTag(List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof LongArrayList) { + final LongArrayList primitiveList = (LongArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeUInt64SizeNoTag(primitiveList.getLong(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeUInt64SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeUInt64List(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeUInt64ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeSInt64ListNoTag(List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof LongArrayList) { + final LongArrayList primitiveList = (LongArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeSInt64SizeNoTag(primitiveList.getLong(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeSInt64SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeSInt64List(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeSInt64ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeEnumListNoTag(List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof IntArrayList) { + final IntArrayList primitiveList = (IntArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeEnumSizeNoTag(primitiveList.getInt(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeEnumSizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeEnumList(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeEnumListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeInt32ListNoTag(List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof IntArrayList) { + final IntArrayList primitiveList = (IntArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeInt32SizeNoTag(primitiveList.getInt(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeInt32SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeInt32List(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeInt32ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeUInt32ListNoTag(List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof IntArrayList) { + final IntArrayList primitiveList = (IntArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeUInt32SizeNoTag(primitiveList.getInt(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeUInt32SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeUInt32List(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeUInt32ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeSInt32ListNoTag(List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof IntArrayList) { + final IntArrayList primitiveList = (IntArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeSInt32SizeNoTag(primitiveList.getInt(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeSInt32SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeSInt32List(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = computeSizeSInt32ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeFixed32ListNoTag(List list) { + return list.size() * WireFormat.FIXED32_SIZE; + } + + static int computeSizeFixed32List(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + if (packed) { + int dataSize = length * WireFormat.FIXED32_SIZE; + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(dataSize); + } else { + return length * CodedOutputStream.computeFixed32Size(fieldNumber, 0); + } + } + + static int computeSizeFixed64ListNoTag(List list) { + return list.size() * WireFormat.FIXED64_SIZE; + } + + static int computeSizeFixed64List(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + if (packed) { + final int dataSize = length * WireFormat.FIXED64_SIZE; + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(dataSize); + } else { + return length * CodedOutputStream.computeFixed64Size(fieldNumber, 0); + } + } + + static int computeSizeBoolListNoTag(List list) { + // bools are 1 byte varints + return list.size(); + } + + static int computeSizeBoolList(int fieldNumber, List list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + if (packed) { + // bools are 1 byte varints + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(length); + } else { + return length * CodedOutputStream.computeBoolSize(fieldNumber, true); + } + } + + static int computeSizeStringList(int fieldNumber, List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = length * CodedOutputStream.computeTagSize(fieldNumber); + if (list instanceof LazyStringList) { + LazyStringList lazyList = ((LazyStringList) list); + for (int i = 0; i < length; i++) { + Object value = lazyList.getRaw(i); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSizeNoTag((ByteString) value); + } else { + size += CodedOutputStream.computeStringSizeNoTag((String) value); + } + } + } else { + for (int i = 0; i < length; i++) { + Object value = list.get(i); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSizeNoTag((ByteString) value); + } else { + size += CodedOutputStream.computeStringSizeNoTag((String) value); + } + } + } + return size; + } + + static int computeSizeMessage(int fieldNumber, Object value, Schema schema) { + if (value instanceof LazyFieldLite) { + return CodedOutputStream.computeLazyFieldSize(fieldNumber, (LazyFieldLite) value); + } else { + return CodedOutputStream.computeMessageSize(fieldNumber, (MessageLite) value, schema); + } + } + + static int computeSizeMessageList(int fieldNumber, List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = length * CodedOutputStream.computeTagSize(fieldNumber); + for (int i = 0; i < length; i++) { + Object value = list.get(i); + if (value instanceof LazyFieldLite) { + size += CodedOutputStream.computeLazyFieldSizeNoTag((LazyFieldLite) value); + } else { + size += CodedOutputStream.computeMessageSizeNoTag((MessageLite) value); + } + } + return size; + } + + static int computeSizeMessageList(int fieldNumber, List list, Schema schema) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = length * CodedOutputStream.computeTagSize(fieldNumber); + for (int i = 0; i < length; i++) { + Object value = list.get(i); + if (value instanceof LazyFieldLite) { + size += CodedOutputStream.computeLazyFieldSizeNoTag((LazyFieldLite) value); + } else { + size += CodedOutputStream.computeMessageSizeNoTag((MessageLite) value, schema); + } + } + return size; + } + + static int computeSizeByteStringList(int fieldNumber, List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = length * CodedOutputStream.computeTagSize(fieldNumber); + for (int i = 0; i < list.size(); i++) { + size += CodedOutputStream.computeBytesSizeNoTag(list.get(i)); + } + return size; + } + + static int computeSizeGroupList(int fieldNumber, List list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = 0; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeGroupSize(fieldNumber, list.get(i)); + } + return size; + } + + static int computeSizeGroupList(int fieldNumber, List list, Schema schema) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = 0; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeGroupSize(fieldNumber, list.get(i), schema); + } + return size; + } + + /** + * Determines whether to issue tableswitch or lookupswitch for the mergeFrom method. + * + * @see #shouldUseTableSwitch(int, int, int) + */ + public static boolean shouldUseTableSwitch(FieldInfo[] fields) { + // Determine whether to issue a tableswitch or a lookupswitch + // instruction. + if (fields.length == 0) { + return false; + } + + int lo = fields[0].getFieldNumber(); + int hi = fields[fields.length - 1].getFieldNumber(); + return shouldUseTableSwitch(lo, hi, fields.length); + } + + /** + * Determines whether to issue tableswitch or lookupswitch for the mergeFrom method. This is based + * on the + * logic in the JDK. + * + * @param lo the lowest fieldNumber contained within the message. + * @param hi the higest fieldNumber contained within the message. + * @param numFields the total number of fields in the message. + * @return {@code true} if tableswitch should be used, rather than lookupswitch. + */ + public static boolean shouldUseTableSwitch(int lo, int hi, int numFields) { + if (hi < DEFAULT_LOOK_UP_START_NUMBER) { + return true; + } + long tableSpaceCost = ((long) hi - lo + 1); // words + long tableTimeCost = 3; // comparisons + long lookupSpaceCost = 3 + 2 * (long) numFields; + long lookupTimeCost = 3 + (long) numFields; + return tableSpaceCost + 3 * tableTimeCost <= lookupSpaceCost + 3 * lookupTimeCost; + } + + public static UnknownFieldSchema proto2UnknownFieldSetSchema() { + return PROTO2_UNKNOWN_FIELD_SET_SCHEMA; + } + + public static UnknownFieldSchema proto3UnknownFieldSetSchema() { + return PROTO3_UNKNOWN_FIELD_SET_SCHEMA; + } + + public static UnknownFieldSchema unknownFieldSetLiteSchema() { + return UNKNOWN_FIELD_SET_LITE_SCHEMA; + } + + private static UnknownFieldSchema getUnknownFieldSetSchema(boolean proto3) { + try { + Class clz = getUnknownFieldSetSchemaClass(); + if (clz == null) { + return null; + } + return (UnknownFieldSchema) clz.getConstructor(boolean.class).newInstance(proto3); + } catch (Throwable t) { + return null; + } + } + + private static Class getGeneratedMessageClass() { + try { + return Class.forName("com.google.protobuf.GeneratedMessageV3"); + } catch (Throwable e) { + return null; + } + } + + private static Class getUnknownFieldSetSchemaClass() { + try { + return Class.forName("com.google.protobuf.UnknownFieldSetSchema"); + } catch (Throwable e) { + return null; + } + } + + static Object getMapDefaultEntry(Class clazz, String name) { + try { + Class holder = + Class.forName(clazz.getName() + "$" + toCamelCase(name, true) + "DefaultEntryHolder"); + Field[] fields = holder.getDeclaredFields(); + if (fields.length != 1) { + throw new IllegalStateException( + "Unable to look up map field default entry holder class for " + + name + + " in " + + clazz.getName()); + } + return UnsafeUtil.getStaticObject(fields[0]); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + + static String toCamelCase(String name, boolean capNext) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < name.length(); ++i) { + char c = name.charAt(i); + // Matches protoc field name function: + if ('a' <= c && c <= 'z') { + if (capNext) { + sb.append((char) (c + ('A' - 'a'))); + } else { + sb.append(c); + } + capNext = false; + } else if ('A' <= c && c <= 'Z') { + if (i == 0 && !capNext) { + // Force first letter to lower-case unless explicitly told to capitalize it. + sb.append((char) (c - ('A' - 'a'))); + } else { + sb.append(c); + } + capNext = false; + } else if ('0' <= c && c <= '9') { + sb.append(c); + capNext = true; + } else { + capNext = true; + } + } + return sb.toString(); + } + + /** Returns true if both are null or both are {@link Object#equals}. */ + static boolean safeEquals(Object a, Object b) { + return a == b || (a != null && a.equals(b)); + } + + static void mergeMap(MapFieldSchema mapFieldSchema, T message, T o, long offset) { + Object merged = + mapFieldSchema.mergeFrom( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(o, offset)); + UnsafeUtil.putObject(message, offset, merged); + } + + static > void mergeExtensions( + ExtensionSchema schema, T message, T other) { + FieldSet otherExtensions = schema.getExtensions(other); + if (!otherExtensions.isEmpty()) { + FieldSet messageExtensions = schema.getMutableExtensions(message); + messageExtensions.mergeFrom(otherExtensions); + } + } + + static void mergeUnknownFields( + UnknownFieldSchema schema, T message, T other) { + UT messageUnknowns = schema.getFromMessage(message); + UT otherUnknowns = schema.getFromMessage(other); + UT merged = schema.merge(messageUnknowns, otherUnknowns); + schema.setToMessage(message, merged); + } + + /** Filters unrecognized enum values in a list. */ + static UB filterUnknownEnumList( + int number, + List enumList, + EnumLiteMap enumMap, + UB unknownFields, + UnknownFieldSchema unknownFieldSchema) { + if (enumMap == null) { + return unknownFields; + } + // TODO(dweis): Specialize for IntArrayList to avoid boxing. + if (enumList instanceof RandomAccess) { + int writePos = 0; + int size = enumList.size(); + for (int readPos = 0; readPos < size; ++readPos) { + int enumValue = enumList.get(readPos); + if (enumMap.findValueByNumber(enumValue) != null) { + if (readPos != writePos) { + enumList.set(writePos, enumValue); + } + ++writePos; + } else { + unknownFields = storeUnknownEnum(number, enumValue, unknownFields, unknownFieldSchema); + } + } + if (writePos != size) { + enumList.subList(writePos, size).clear(); + } + } else { + for (Iterator it = enumList.iterator(); it.hasNext(); ) { + int enumValue = it.next(); + if (enumMap.findValueByNumber(enumValue) == null) { + unknownFields = storeUnknownEnum(number, enumValue, unknownFields, unknownFieldSchema); + it.remove(); + } + } + } + return unknownFields; + } + + /** Filters unrecognized enum values in a list. */ + static UB filterUnknownEnumList( + int number, + List enumList, + EnumVerifier enumVerifier, + UB unknownFields, + UnknownFieldSchema unknownFieldSchema) { + if (enumVerifier == null) { + return unknownFields; + } + // TODO(dweis): Specialize for IntArrayList to avoid boxing. + if (enumList instanceof RandomAccess) { + int writePos = 0; + int size = enumList.size(); + for (int readPos = 0; readPos < size; ++readPos) { + int enumValue = enumList.get(readPos); + if (enumVerifier.isInRange(enumValue)) { + if (readPos != writePos) { + enumList.set(writePos, enumValue); + } + ++writePos; + } else { + unknownFields = storeUnknownEnum(number, enumValue, unknownFields, unknownFieldSchema); + } + } + if (writePos != size) { + enumList.subList(writePos, size).clear(); + } + } else { + for (Iterator it = enumList.iterator(); it.hasNext(); ) { + int enumValue = it.next(); + if (!enumVerifier.isInRange(enumValue)) { + unknownFields = storeUnknownEnum(number, enumValue, unknownFields, unknownFieldSchema); + it.remove(); + } + } + } + return unknownFields; + } + + /** Stores an unrecognized enum value as an unknown value. */ + static UB storeUnknownEnum( + int number, int enumValue, UB unknownFields, UnknownFieldSchema unknownFieldSchema) { + if (unknownFields == null) { + unknownFields = unknownFieldSchema.newBuilder(); + } + unknownFieldSchema.addVarint(unknownFields, number, enumValue); + return unknownFields; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/Service.java b/java/core/src/main/java/com/google/protobuf/Service.java index ba7b033ed4..d45cdaec9a 100644 --- a/java/core/src/main/java/com/google/protobuf/Service.java +++ b/java/core/src/main/java/com/google/protobuf/Service.java @@ -31,70 +31,63 @@ package com.google.protobuf; /** - * Abstract base interface for protocol-buffer-based RPC services. Services - * themselves are abstract classes (implemented either by servers or as - * stubs), but they subclass this base interface. The methods of this - * interface can be used to call the methods of the service without knowing - * its exact type at compile time (analogous to the Message interface). + * Abstract base interface for protocol-buffer-based RPC services. Services themselves are abstract + * classes (implemented either by servers or as stubs), but they subclass this base interface. The + * methods of this interface can be used to call the methods of the service without knowing its + * exact type at compile time (analogous to the Message interface). * - *

Starting with version 2.3.0, RPC implementations should not try to build - * on this, but should instead provide code generator plugins which generate - * code specific to the particular RPC implementation. This way the generated - * code can be more appropriate for the implementation in use and can avoid - * unnecessary layers of indirection. + *

Starting with version 2.3.0, RPC implementations should not try to build on this, but should + * instead provide code generator plugins which generate code specific to the particular RPC + * implementation. This way the generated code can be more appropriate for the implementation in use + * and can avoid unnecessary layers of indirection. * * @author kenton@google.com Kenton Varda */ public interface Service { - /** - * Get the {@code ServiceDescriptor} describing this service and its methods. - */ + /** Get the {@code ServiceDescriptor} describing this service and its methods. */ Descriptors.ServiceDescriptor getDescriptorForType(); /** - *

Call a method of the service specified by MethodDescriptor. This is - * normally implemented as a simple {@code switch()} that calls the standard - * definitions of the service's methods. + * Call a method of the service specified by MethodDescriptor. This is normally implemented as a + * simple {@code switch()} that calls the standard definitions of the service's methods. * *

Preconditions: + * *

    *
  • {@code method.getService() == getDescriptorForType()} - *
  • {@code request} is of the exact same class as the object returned by - * {@code getRequestPrototype(method)}. - *
  • {@code controller} is of the correct type for the RPC implementation - * being used by this Service. For stubs, the "correct type" depends - * on the RpcChannel which the stub is using. Server-side Service - * implementations are expected to accept whatever type of - * {@code RpcController} the server-side RPC implementation uses. + *
  • {@code request} is of the exact same class as the object returned by {@code + * getRequestPrototype(method)}. + *
  • {@code controller} is of the correct type for the RPC implementation being used by this + * Service. For stubs, the "correct type" depends on the RpcChannel which the stub is using. + * Server-side Service implementations are expected to accept whatever type of {@code + * RpcController} the server-side RPC implementation uses. *
* *

Postconditions: + * *

    - *
  • {@code done} will be called when the method is complete. This may be - * before {@code callMethod()} returns or it may be at some point in - * the future. - *
  • The parameter to {@code done} is the response. It must be of the - * exact same type as would be returned by - * {@code getResponsePrototype(method)}. - *
  • If the RPC failed, the parameter to {@code done} will be - * {@code null}. Further details about the failure can be found by - * querying {@code controller}. + *
  • {@code done} will be called when the method is complete. This may be before {@code + * callMethod()} returns or it may be at some point in the future. + *
  • The parameter to {@code done} is the response. It must be of the exact same type as would + * be returned by {@code getResponsePrototype(method)}. + *
  • If the RPC failed, the parameter to {@code done} will be {@code null}. Further details + * about the failure can be found by querying {@code controller}. *
*/ - void callMethod(Descriptors.MethodDescriptor method, - RpcController controller, - Message request, - RpcCallback done); + void callMethod( + Descriptors.MethodDescriptor method, + RpcController controller, + Message request, + RpcCallback done); /** - *

{@code callMethod()} requires that the request passed in is of a - * particular subclass of {@code Message}. {@code getRequestPrototype()} - * gets the default instances of this type for a given method. You can then - * call {@code Message.newBuilderForType()} on this instance to - * construct a builder to build an object which you can then pass to - * {@code callMethod()}. + * {@code callMethod()} requires that the request passed in is of a particular subclass of {@code + * Message}. {@code getRequestPrototype()} gets the default instances of this type for a given + * method. You can then call {@code Message.newBuilderForType()} on this instance to construct a + * builder to build an object which you can then pass to {@code callMethod()}. * *

Example: + * *

    *   MethodDescriptor method =
    *     service.getDescriptorForType().findMethodByName("Foo");
@@ -107,11 +100,10 @@ public interface Service {
   Message getRequestPrototype(Descriptors.MethodDescriptor method);
 
   /**
-   * Like {@code getRequestPrototype()}, but gets a prototype of the response
-   * message.  {@code getResponsePrototype()} is generally not needed because
-   * the {@code Service} implementation constructs the response message itself,
-   * but it may be useful in some cases to know ahead of time what type of
-   * object will be returned.
+   * Like {@code getRequestPrototype()}, but gets a prototype of the response message. {@code
+   * getResponsePrototype()} is generally not needed because the {@code Service} implementation
+   * constructs the response message itself, but it may be useful in some cases to know ahead of
+   * time what type of object will be returned.
    */
   Message getResponsePrototype(Descriptors.MethodDescriptor method);
 }
diff --git a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java
index 1f5ec8a2d7..acdc1de183 100644
--- a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java
+++ b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java
@@ -33,35 +33,29 @@ package com.google.protobuf;
 import static com.google.protobuf.Internal.checkNotNull;
 
 /**
- * {@code SingleFieldBuilder} implements a structure that a protocol
- * message uses to hold a single field of another protocol message. It supports
- * the classical use case of setting an immutable {@link Message} as the value
- * of the field and is highly optimized around this.
- * 
- * It also supports the additional use case of setting a {@link Message.Builder} - * as the field and deferring conversion of that {@code Builder} - * to an immutable {@code Message}. In this way, it's possible to maintain - * a tree of {@code Builder}'s that acts as a fully read/write data - * structure. + * {@code SingleFieldBuilder} implements a structure that a protocol message uses to hold a single + * field of another protocol message. It supports the classical use case of setting an immutable + * {@link Message} as the value of the field and is highly optimized around this. + * + *

It also supports the additional use case of setting a {@link Message.Builder} as the field and + * deferring conversion of that {@code Builder} to an immutable {@code Message}. In this way, it's + * possible to maintain a tree of {@code Builder}'s that acts as a fully read/write data structure. *
- * Logically, one can think of a tree of builders as converting the entire tree - * to messages when build is called on the root or when any method is called - * that desires a Message instead of a Builder. In terms of the implementation, - * the {@code SingleFieldBuilder} and {@code RepeatedFieldBuilder} - * classes cache messages that were created so that messages only need to be - * created when some change occurred in its builder or a builder for one of its - * descendants. + * Logically, one can think of a tree of builders as converting the entire tree to messages when + * build is called on the root or when any method is called that desires a Message instead of a + * Builder. In terms of the implementation, the {@code SingleFieldBuilder} and {@code + * RepeatedFieldBuilder} classes cache messages that were created so that messages only need to be + * created when some change occurred in its builder or a builder for one of its descendants. * * @param the type of message for the field * @param the type of builder for the field * @param the common interface for the message and the builder - * * @author jonp@google.com (Jon Perlow) */ -public class SingleFieldBuilder - +public class SingleFieldBuilder< + MType extends GeneratedMessage, + BType extends GeneratedMessage.Builder, + IType extends MessageOrBuilder> implements GeneratedMessage.BuilderParent { // Parent to send changes to. @@ -82,10 +76,7 @@ public class SingleFieldBuilder // to dispatch dirty invalidations. See GeneratedMessage.BuilderListener. private boolean isClean; - public SingleFieldBuilder( - MType message, - GeneratedMessage.BuilderParent parent, - boolean isClean) { + public SingleFieldBuilder(MType message, GeneratedMessage.BuilderParent parent, boolean isClean) { this.message = checkNotNull(message); this.parent = parent; this.isClean = isClean; @@ -97,10 +88,9 @@ public class SingleFieldBuilder } /** - * Get the message for the field. If the message is currently stored - * as a {@code Builder}, it is converted to a {@code Message} by - * calling {@link Message.Builder#buildPartial} on it. If no message has - * been set, returns the default instance of the message. + * Get the message for the field. If the message is currently stored as a {@code Builder}, it is + * converted to a {@code Message} by calling {@link Message.Builder#buildPartial} on it. If no + * message has been set, returns the default instance of the message. * * @return the message for the field */ @@ -126,8 +116,8 @@ public class SingleFieldBuilder } /** - * Gets a builder for the field. If no builder has been created yet, a - * builder is created on demand by calling {@link Message#toBuilder}. + * Gets a builder for the field. If no builder has been created yet, a builder is created on + * demand by calling {@link Message#toBuilder}. * * @return The builder for the field */ @@ -146,28 +136,27 @@ public class SingleFieldBuilder } /** - * Gets the base class interface for the field. This may either be a builder - * or a message. It will return whatever is more efficient. + * Gets the base class interface for the field. This may either be a builder or a message. It will + * return whatever is more efficient. * * @return the message or builder for the field as the base class interface */ @SuppressWarnings("unchecked") public IType getMessageOrBuilder() { if (builder != null) { - return (IType) builder; + return (IType) builder; } else { return (IType) message; } } /** - * Sets a message for the field replacing any existing value. + * Sets a message for the field replacing any existing value. * * @param message the message to set * @return the builder */ - public SingleFieldBuilder setMessage( - MType message) { + public SingleFieldBuilder setMessage(MType message) { this.message = checkNotNull(message); if (builder != null) { builder.dispose(); @@ -183,8 +172,7 @@ public class SingleFieldBuilder * @param value the value to merge from * @return the builder */ - public SingleFieldBuilder mergeFrom( - MType value) { + public SingleFieldBuilder mergeFrom(MType value) { if (builder == null && message == message.getDefaultInstanceForType()) { message = value; } else { @@ -201,9 +189,11 @@ public class SingleFieldBuilder */ @SuppressWarnings("unchecked") public SingleFieldBuilder clear() { - message = (MType) (message != null ? - message.getDefaultInstanceForType() : - builder.getDefaultInstanceForType()); + message = + (MType) + (message != null + ? message.getDefaultInstanceForType() + : builder.getDefaultInstanceForType()); if (builder != null) { builder.dispose(); builder = null; @@ -213,8 +203,8 @@ public class SingleFieldBuilder } /** - * Called when a the builder or one of its nested children has changed - * and any parent should be notified of its invalidation. + * Called when a the builder or one of its nested children has changed and any parent should be + * notified of its invalidation. */ private void onChanged() { // If builder is null, this is the case where onChanged is being called diff --git a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java index 8ab0f26d90..78a4a21664 100644 --- a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java +++ b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java @@ -33,35 +33,29 @@ package com.google.protobuf; import static com.google.protobuf.Internal.checkNotNull; /** - * {@code SingleFieldBuilderV3} implements a structure that a protocol - * message uses to hold a single field of another protocol message. It supports - * the classical use case of setting an immutable {@link Message} as the value - * of the field and is highly optimized around this. - *
- * It also supports the additional use case of setting a {@link Message.Builder} - * as the field and deferring conversion of that {@code Builder} - * to an immutable {@code Message}. In this way, it's possible to maintain - * a tree of {@code Builder}'s that acts as a fully read/write data - * structure. + * {@code SingleFieldBuilderV3} implements a structure that a protocol message uses to hold a single + * field of another protocol message. It supports the classical use case of setting an immutable + * {@link Message} as the value of the field and is highly optimized around this. + * + *

It also supports the additional use case of setting a {@link Message.Builder} as the field and + * deferring conversion of that {@code Builder} to an immutable {@code Message}. In this way, it's + * possible to maintain a tree of {@code Builder}'s that acts as a fully read/write data structure. *
- * Logically, one can think of a tree of builders as converting the entire tree - * to messages when build is called on the root or when any method is called - * that desires a Message instead of a Builder. In terms of the implementation, - * the {@code SingleFieldBuilderV3} and {@code RepeatedFieldBuilderV3} - * classes cache messages that were created so that messages only need to be - * created when some change occurred in its builder or a builder for one of its - * descendants. + * Logically, one can think of a tree of builders as converting the entire tree to messages when + * build is called on the root or when any method is called that desires a Message instead of a + * Builder. In terms of the implementation, the {@code SingleFieldBuilderV3} and {@code + * RepeatedFieldBuilderV3} classes cache messages that were created so that messages only need to be + * created when some change occurred in its builder or a builder for one of its descendants. * * @param the type of message for the field * @param the type of builder for the field * @param the common interface for the message and the builder - * * @author jonp@google.com (Jon Perlow) */ -public class SingleFieldBuilderV3 - +public class SingleFieldBuilderV3< + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, + IType extends MessageOrBuilder> implements AbstractMessage.BuilderParent { // Parent to send changes to. @@ -82,10 +76,7 @@ public class SingleFieldBuilderV3 // to dispatch dirty invalidations. See AbstractMessage.BuilderListener. private boolean isClean; - public SingleFieldBuilderV3( - MType message, - AbstractMessage.BuilderParent parent, - boolean isClean) { + public SingleFieldBuilderV3(MType message, AbstractMessage.BuilderParent parent, boolean isClean) { this.message = checkNotNull(message); this.parent = parent; this.isClean = isClean; @@ -97,10 +88,9 @@ public class SingleFieldBuilderV3 } /** - * Get the message for the field. If the message is currently stored - * as a {@code Builder}, it is converted to a {@code Message} by - * calling {@link Message.Builder#buildPartial} on it. If no message has - * been set, returns the default instance of the message. + * Get the message for the field. If the message is currently stored as a {@code Builder}, it is + * converted to a {@code Message} by calling {@link Message.Builder#buildPartial} on it. If no + * message has been set, returns the default instance of the message. * * @return the message for the field */ @@ -126,8 +116,8 @@ public class SingleFieldBuilderV3 } /** - * Gets a builder for the field. If no builder has been created yet, a - * builder is created on demand by calling {@link Message#toBuilder}. + * Gets a builder for the field. If no builder has been created yet, a builder is created on + * demand by calling {@link Message#toBuilder}. * * @return The builder for the field */ @@ -146,28 +136,27 @@ public class SingleFieldBuilderV3 } /** - * Gets the base class interface for the field. This may either be a builder - * or a message. It will return whatever is more efficient. + * Gets the base class interface for the field. This may either be a builder or a message. It will + * return whatever is more efficient. * * @return the message or builder for the field as the base class interface */ @SuppressWarnings("unchecked") public IType getMessageOrBuilder() { if (builder != null) { - return (IType) builder; + return (IType) builder; } else { return (IType) message; } } /** - * Sets a message for the field replacing any existing value. + * Sets a message for the field replacing any existing value. * * @param message the message to set * @return the builder */ - public SingleFieldBuilderV3 setMessage( - MType message) { + public SingleFieldBuilderV3 setMessage(MType message) { this.message = checkNotNull(message); if (builder != null) { builder.dispose(); @@ -183,8 +172,7 @@ public class SingleFieldBuilderV3 * @param value the value to merge from * @return the builder */ - public SingleFieldBuilderV3 mergeFrom( - MType value) { + public SingleFieldBuilderV3 mergeFrom(MType value) { if (builder == null && message == message.getDefaultInstanceForType()) { message = value; } else { @@ -201,9 +189,11 @@ public class SingleFieldBuilderV3 */ @SuppressWarnings("unchecked") public SingleFieldBuilderV3 clear() { - message = (MType) (message != null ? - message.getDefaultInstanceForType() : - builder.getDefaultInstanceForType()); + message = + (MType) + (message != null + ? message.getDefaultInstanceForType() + : builder.getDefaultInstanceForType()); if (builder != null) { builder.dispose(); builder = null; @@ -213,8 +203,8 @@ public class SingleFieldBuilderV3 } /** - * Called when a the builder or one of its nested children has changed - * and any parent should be notified of its invalidation. + * Called when a the builder or one of its nested children has changed and any parent should be + * notified of its invalidation. */ private void onChanged() { // If builder is null, this is the case where onChanged is being called diff --git a/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java b/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java index 279edc4de1..546e56e856 100644 --- a/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java +++ b/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java @@ -43,15 +43,14 @@ import java.util.SortedMap; import java.util.TreeMap; /** - * A custom map implementation from FieldDescriptor to Object optimized to - * minimize the number of memory allocations for instances with a small number - * of mappings. The implementation stores the first {@code k} mappings in an - * array for a configurable value of {@code k}, allowing direct access to the - * corresponding {@code Entry}s without the need to create an Iterator. The - * remaining entries are stored in an overflow map. Iteration over the entries - * in the map should be done as follows: + * A custom map implementation from FieldDescriptor to Object optimized to minimize the number of + * memory allocations for instances with a small number of mappings. The implementation stores the + * first {@code k} mappings in an array for a configurable value of {@code k}, allowing direct + * access to the corresponding {@code Entry}s without the need to create an Iterator. The remaining + * entries are stored in an overflow map. Iteration over the entries in the map should be done as + * follows: * - *

   {@code
+ * 
{@code
  * for (int i = 0; i < fieldMap.getNumArrayEntries(); i++) {
  *   process(fieldMap.getArrayEntryAt(i));
  * }
@@ -60,24 +59,21 @@ import java.util.TreeMap;
  * }
  * }
* - * The resulting iteration is in order of ascending field tag number. The - * object returned by {@link #entrySet()} adheres to the same contract but is - * less efficient as it necessarily involves creating an object for iteration. - *

- * The tradeoff for this memory efficiency is that the worst case running time - * of the {@code put()} operation is {@code O(k + lg n)}, which happens when - * entries are added in descending order. {@code k} should be chosen such that - * it covers enough common cases without adversely affecting larger maps. In - * practice, the worst case scenario does not happen for extensions because - * extension fields are serialized and deserialized in order of ascending tag - * number, but the worst case scenario can happen for DynamicMessages. - *

- * The running time for all other operations is similar to that of - * {@code TreeMap}. - *

- * Instances are not thread-safe until {@link #makeImmutable()} is called, - * after which any modifying operation will result in an - * {@link UnsupportedOperationException}. + * The resulting iteration is in order of ascending field tag number. The object returned by {@link + * #entrySet()} adheres to the same contract but is less efficient as it necessarily involves + * creating an object for iteration. + * + *

The tradeoff for this memory efficiency is that the worst case running time of the {@code + * put()} operation is {@code O(k + lg n)}, which happens when entries are added in descending + * order. {@code k} should be chosen such that it covers enough common cases without adversely + * affecting larger maps. In practice, the worst case scenario does not happen for extensions + * because extension fields are serialized and deserialized in order of ascending tag number, but + * the worst case scenario can happen for DynamicMessages. + * + *

The running time for all other operations is similar to that of {@code TreeMap}. + * + *

Instances are not thread-safe until {@link #makeImmutable()} is called, after which any + * modifying operation will result in an {@link UnsupportedOperationException}. * * @author darick@google.com Darick Tong */ @@ -87,15 +83,14 @@ import java.util.TreeMap; class SmallSortedMap, V> extends AbstractMap { /** - * Creates a new instance for mapping FieldDescriptors to their values. - * The {@link #makeImmutable()} implementation will convert the List values - * of any repeated fields to unmodifiable lists. + * Creates a new instance for mapping FieldDescriptors to their values. The {@link + * #makeImmutable()} implementation will convert the List values of any repeated fields to + * unmodifiable lists. * - * @param arraySize The size of the entry array containing the - * lexicographically smallest mappings. + * @param arraySize The size of the entry array containing the lexicographically smallest + * mappings. */ - static > + static > SmallSortedMap newFieldMap(int arraySize) { return new SmallSortedMap(arraySize) { @Override @@ -103,15 +98,13 @@ class SmallSortedMap, V> extends AbstractMap { public void makeImmutable() { if (!isImmutable()) { for (int i = 0; i < getNumArrayEntries(); i++) { - final Map.Entry entry = - getArrayEntryAt(i); + final Map.Entry entry = getArrayEntryAt(i); if (entry.getKey().isRepeated()) { final List value = (List) entry.getValue(); entry.setValue(Collections.unmodifiableList(value)); } } - for (Map.Entry entry : - getOverflowEntries()) { + for (Map.Entry entry : getOverflowEntries()) { if (entry.getKey().isRepeated()) { final List value = (List) entry.getValue(); entry.setValue(Collections.unmodifiableList(value)); @@ -126,11 +119,10 @@ class SmallSortedMap, V> extends AbstractMap { /** * Creates a new instance for testing. * - * @param arraySize The size of the entry array containing the - * lexicographically smallest mappings. + * @param arraySize The size of the entry array containing the lexicographically smallest + * mappings. */ - static , V> SmallSortedMap newInstanceForTest( - int arraySize) { + static , V> SmallSortedMap newInstanceForTest(int arraySize) { return new SmallSortedMap(arraySize); } @@ -144,16 +136,18 @@ class SmallSortedMap, V> extends AbstractMap { // The EntrySet is a stateless view of the Map. It's initialized the first // time it is requested and reused henceforth. private volatile EntrySet lazyEntrySet; + private Map overflowEntriesDescending; + private volatile DescendingEntrySet lazyDescendingEntrySet; /** - * @code arraySize Size of the array in which the lexicographically smallest - * mappings are stored. (i.e. the {@code k} referred to in the class - * documentation). + * @code arraySize Size of the array in which the lexicographically smallest mappings are stored. + * (i.e. the {@code k} referred to in the class documentation). */ private SmallSortedMap(int arraySize) { this.maxArraySize = arraySize; this.entryList = Collections.emptyList(); this.overflowEntries = Collections.emptyMap(); + this.overflowEntriesDescending = Collections.emptyMap(); } /** Make this map immutable from this point forward. */ @@ -163,9 +157,14 @@ class SmallSortedMap, V> extends AbstractMap { // because none of the list's accessors are exposed. The iterator() of // overflowEntries, on the other hand, is exposed so it must be made // unmodifiable. - overflowEntries = overflowEntries.isEmpty() ? - Collections.emptyMap() : - Collections.unmodifiableMap(overflowEntries); + overflowEntries = + overflowEntries.isEmpty() + ? Collections.emptyMap() + : Collections.unmodifiableMap(overflowEntries); + overflowEntriesDescending = + overflowEntriesDescending.isEmpty() + ? Collections.emptyMap() + : Collections.unmodifiableMap(overflowEntriesDescending); isImmutable = true; } } @@ -192,11 +191,16 @@ class SmallSortedMap, V> extends AbstractMap { /** @return An iterable over the overflow entries. */ public Iterable> getOverflowEntries() { - return overflowEntries.isEmpty() ? - EmptySet.>iterable() : - overflowEntries.entrySet(); + return overflowEntries.isEmpty() + ? EmptySet.>iterable() + : overflowEntries.entrySet(); } + Iterable> getOverflowEntriesDescending() { + return overflowEntriesDescending.isEmpty() + ? EmptySet.>iterable() + : overflowEntriesDescending.entrySet(); + } @Override public int size() { @@ -204,10 +208,9 @@ class SmallSortedMap, V> extends AbstractMap { } /** - * The implementation throws a {@code ClassCastException} if o is not an - * object of type {@code K}. + * The implementation throws a {@code ClassCastException} if o is not an object of type {@code K}. * - * {@inheritDoc} + *

{@inheritDoc} */ @Override public boolean containsKey(Object o) { @@ -217,10 +220,9 @@ class SmallSortedMap, V> extends AbstractMap { } /** - * The implementation throws a {@code ClassCastException} if o is not an - * object of type {@code K}. + * The implementation throws a {@code ClassCastException} if o is not an object of type {@code K}. * - * {@inheritDoc} + *

{@inheritDoc} */ @Override public V get(Object o) { @@ -251,8 +253,7 @@ class SmallSortedMap, V> extends AbstractMap { if (entryList.size() == maxArraySize) { // Shift the last array entry into overflow. final Entry lastEntryInArray = entryList.remove(maxArraySize - 1); - getOverflowEntriesMutable().put(lastEntryInArray.getKey(), - lastEntryInArray.getValue()); + getOverflowEntriesMutable().put(lastEntryInArray.getKey(), lastEntryInArray.getValue()); } entryList.add(insertionPoint, new Entry(key, value)); return null; @@ -270,10 +271,9 @@ class SmallSortedMap, V> extends AbstractMap { } /** - * The implementation throws a {@code ClassCastException} if o is not an - * object of type {@code K}. + * The implementation throws a {@code ClassCastException} if o is not an object of type {@code K}. * - * {@inheritDoc} + *

{@inheritDoc} */ @Override public V remove(Object o) { @@ -299,8 +299,7 @@ class SmallSortedMap, V> extends AbstractMap { if (!overflowEntries.isEmpty()) { // Shift the first entry in the overflow to be the last entry in the // array. - final Iterator> iterator = - getOverflowEntriesMutable().entrySet().iterator(); + final Iterator> iterator = getOverflowEntriesMutable().entrySet().iterator(); entryList.add(new Entry(iterator.next())); iterator.remove(); } @@ -309,8 +308,8 @@ class SmallSortedMap, V> extends AbstractMap { /** * @param key The key to find in the entry array. - * @return The returned integer position follows the same semantics as the - * value returned by {@link java.util.Arrays#binarySearch()}. + * @return The returned integer position follows the same semantics as the value returned by + * {@link java.util.Arrays#binarySearch()}. */ private int binarySearchInArray(K key) { int left = 0; @@ -322,7 +321,7 @@ class SmallSortedMap, V> extends AbstractMap { if (right >= 0) { int cmp = key.compareTo(entryList.get(right).getKey()); if (cmp > 0) { - return -(right + 2); // Insert point is after "right". + return -(right + 2); // Insert point is after "right". } else if (cmp == 0) { return right; } @@ -343,11 +342,11 @@ class SmallSortedMap, V> extends AbstractMap { } /** - * Similar to the AbstractMap implementation of {@code keySet()} and - * {@code values()}, the entry set is created the first time this method is - * called, and returned in response to all subsequent calls. + * Similar to the AbstractMap implementation of {@code keySet()} and {@code values()}, the entry + * set is created the first time this method is called, and returned in response to all subsequent + * calls. * - * {@inheritDoc} + *

{@inheritDoc} */ @Override public Set> entrySet() { @@ -357,11 +356,14 @@ class SmallSortedMap, V> extends AbstractMap { return lazyEntrySet; } + Set> descendingEntrySet() { + if (lazyDescendingEntrySet == null) { + lazyDescendingEntrySet = new DescendingEntrySet(); + } + return lazyDescendingEntrySet; + } - /** - * @throws UnsupportedOperationException if {@link #makeImmutable()} has - * has been called. - */ + /** @throws UnsupportedOperationException if {@link #makeImmutable()} has has been called. */ private void checkMutable() { if (isImmutable) { throw new UnsupportedOperationException(); @@ -369,24 +371,20 @@ class SmallSortedMap, V> extends AbstractMap { } /** - * @return a {@link SortedMap} to which overflow entries mappings can be - * added or removed. - * @throws UnsupportedOperationException if {@link #makeImmutable()} has been - * called. + * @return a {@link SortedMap} to which overflow entries mappings can be added or removed. + * @throws UnsupportedOperationException if {@link #makeImmutable()} has been called. */ @SuppressWarnings("unchecked") private SortedMap getOverflowEntriesMutable() { checkMutable(); if (overflowEntries.isEmpty() && !(overflowEntries instanceof TreeMap)) { overflowEntries = new TreeMap(); + overflowEntriesDescending = ((TreeMap) overflowEntries).descendingMap(); } return (SortedMap) overflowEntries; } - /** - * Lazily creates the entry list. Any code that adds to the list must first - * call this method. - */ + /** Lazily creates the entry list. Any code that adds to the list must first call this method. */ private void ensureEntryArrayMutable() { checkMutable(); if (entryList.isEmpty() && !(entryList instanceof ArrayList)) { @@ -395,9 +393,8 @@ class SmallSortedMap, V> extends AbstractMap { } /** - * Entry implementation that implements Comparable in order to support - * binary search within the entry array. Also checks mutability in - * {@link #setValue()}. + * Entry implementation that implements Comparable in order to support binary search within the + * entry array. Also checks mutability in {@link #setValue()}. */ private class Entry implements Map.Entry, Comparable { @@ -451,8 +448,7 @@ class SmallSortedMap, V> extends AbstractMap { @Override public int hashCode() { - return (key == null ? 0 : key.hashCode()) ^ - (value == null ? 0 : value.hashCode()); + return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode()); } @Override @@ -466,9 +462,7 @@ class SmallSortedMap, V> extends AbstractMap { } } - /** - * Stateless view of the entries in the field map. - */ + /** Stateless view of the entries in the field map. */ private class EntrySet extends AbstractSet> { @Override @@ -484,7 +478,7 @@ class SmallSortedMap, V> extends AbstractMap { /** * Throws a {@link ClassCastException} if o is not of the expected type. * - * {@inheritDoc} + *

{@inheritDoc} */ @Override public boolean contains(Object o) { @@ -492,8 +486,7 @@ class SmallSortedMap, V> extends AbstractMap { final Map.Entry entry = (Map.Entry) o; final V existing = get(entry.getKey()); final V value = entry.getValue(); - return existing == value || - (existing != null && existing.equals(value)); + return existing == value || (existing != null && existing.equals(value)); } @Override @@ -508,7 +501,7 @@ class SmallSortedMap, V> extends AbstractMap { /** * Throws a {@link ClassCastException} if o is not of the expected type. * - * {@inheritDoc} + *

{@inheritDoc} */ @Override public boolean remove(Object o) { @@ -527,10 +520,16 @@ class SmallSortedMap, V> extends AbstractMap { } } + private class DescendingEntrySet extends EntrySet { + @Override + public Iterator> iterator() { + return new DescendingEntryIterator(); + } + } /** - * Iterator implementation that switches from the entry array to the overflow - * entries appropriately. + * Iterator implementation that switches from the entry array to the overflow entries + * appropriately. */ private class EntryIterator implements Iterator> { @@ -571,10 +570,9 @@ class SmallSortedMap, V> extends AbstractMap { } /** - * It is important to create the overflow iterator only after the array - * entries have been iterated over because the overflow entry set changes - * when the client calls remove() on the array entries, which invalidates - * any existing iterators. + * It is important to create the overflow iterator only after the array entries have been + * iterated over because the overflow entry set changes when the client calls remove() on the + * array entries, which invalidates any existing iterators. */ private Iterator> getOverflowIterator() { if (lazyOverflowIterator == null) { @@ -585,9 +583,49 @@ class SmallSortedMap, V> extends AbstractMap { } /** - * Helper class that holds immutable instances of an Iterable/Iterator that - * we return when the overflow entries is empty. This eliminates the creation - * of an Iterator object when there is nothing to iterate over. + * Reverse Iterator implementation that switches from the entry array to the overflow entries + * appropriately. + */ + private class DescendingEntryIterator implements Iterator> { + + private int pos = entryList.size(); + private Iterator> lazyOverflowIterator; + + @Override + public boolean hasNext() { + return (pos > 0 && pos <= entryList.size()) || getOverflowIterator().hasNext(); + } + + @Override + public Map.Entry next() { + if (getOverflowIterator().hasNext()) { + return getOverflowIterator().next(); + } + return entryList.get(--pos); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + /** + * It is important to create the overflow iterator only after the array entries have been + * iterated over because the overflow entry set changes when the client calls remove() on the + * array entries, which invalidates any existing iterators. + */ + private Iterator> getOverflowIterator() { + if (lazyOverflowIterator == null) { + lazyOverflowIterator = overflowEntriesDescending.entrySet().iterator(); + } + return lazyOverflowIterator; + } + } + + /** + * Helper class that holds immutable instances of an Iterable/Iterator that we return when the + * overflow entries is empty. This eliminates the creation of an Iterator object when there is + * nothing to iterate over. */ private static class EmptySet { @@ -597,10 +635,12 @@ class SmallSortedMap, V> extends AbstractMap { public boolean hasNext() { return false; } + @Override public Object next() { throw new NoSuchElementException(); } + @Override public void remove() { throw new UnsupportedOperationException(); @@ -653,7 +693,6 @@ class SmallSortedMap, V> extends AbstractMap { return overflowEntries.equals(other.overflowEntries); } - return true; } diff --git a/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java b/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java new file mode 100755 index 0000000000..a32b1430ee --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java @@ -0,0 +1,167 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Information for the layout of a protobuf message class. This describes all of the fields + * contained within a message. + */ +@ExperimentalApi +final class StructuralMessageInfo implements MessageInfo { + private final ProtoSyntax syntax; + private final boolean messageSetWireFormat; + private final int[] checkInitialized; + private final FieldInfo[] fields; + private final MessageLite defaultInstance; + + /** + * Constructor. + * + * @param checkInitialized fields to check in isInitialized(). + * @param fields the set of fields for the message, in field number order. + */ + StructuralMessageInfo( + ProtoSyntax syntax, + boolean messageSetWireFormat, + int[] checkInitialized, + FieldInfo[] fields, + Object defaultInstance) { + this.syntax = syntax; + this.messageSetWireFormat = messageSetWireFormat; + this.checkInitialized = checkInitialized; + this.fields = fields; + this.defaultInstance = (MessageLite) checkNotNull(defaultInstance, "defaultInstance"); + } + + /** Gets the syntax for the message (e.g. PROTO2, PROTO3). */ + @Override + public ProtoSyntax getSyntax() { + return syntax; + } + + /** Indicates whether or not the message should be represented with message set wire format. */ + @Override + public boolean isMessageSetWireFormat() { + return messageSetWireFormat; + } + + /** An array of field numbers that need to be checked for isInitialized(). */ + public int[] getCheckInitialized() { + return checkInitialized; + } + + /** + * Gets the information for all fields within this message, sorted in ascending order by their + * field number. + */ + public FieldInfo[] getFields() { + return fields; + } + + @Override + public MessageLite getDefaultInstance() { + return defaultInstance; + } + + /** Helper method for creating a new builder for {@link MessageInfo}. */ + public static Builder newBuilder() { + return new Builder(); + } + + /** Helper method for creating a new builder for {@link MessageInfo}. */ + public static Builder newBuilder(int numFields) { + return new Builder(numFields); + } + + /** A builder of {@link MessageInfo} instances. */ + public static final class Builder { + private final List fields; + private ProtoSyntax syntax; + private boolean wasBuilt; + private boolean messageSetWireFormat; + private int[] checkInitialized = null; + private Object defaultInstance; + + public Builder() { + fields = new ArrayList(); + } + + public Builder(int numFields) { + fields = new ArrayList(numFields); + } + + public void withDefaultInstance(Object defaultInstance) { + this.defaultInstance = defaultInstance; + } + + public void withSyntax(ProtoSyntax syntax) { + this.syntax = checkNotNull(syntax, "syntax"); + } + + public void withMessageSetWireFormat(boolean messageSetWireFormat) { + this.messageSetWireFormat = messageSetWireFormat; + } + + public void withCheckInitialized(int[] checkInitialized) { + this.checkInitialized = checkInitialized; + } + + public void withField(FieldInfo field) { + if (wasBuilt) { + throw new IllegalStateException("Builder can only build once"); + } + fields.add(field); + } + + public StructuralMessageInfo build() { + if (wasBuilt) { + throw new IllegalStateException("Builder can only build once"); + } + if (syntax == null) { + throw new IllegalStateException("Must specify a proto syntax"); + } + wasBuilt = true; + Collections.sort(fields); + return new StructuralMessageInfo( + syntax, + messageSetWireFormat, + checkInitialized, + fields.toArray(new FieldInfo[0]), + defaultInstance); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/TextFormat.java b/java/core/src/main/java/com/google/protobuf/TextFormat.java index 79962e0811..0385b2425a 100644 --- a/java/core/src/main/java/com/google/protobuf/TextFormat.java +++ b/java/core/src/main/java/com/google/protobuf/TextFormat.java @@ -46,8 +46,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * Provide text parsing and formatting support for proto2 instances. - * The implementation largely follows google/protobuf/text_format.cc. + * Provide text parsing and formatting support for proto2 instances. The implementation largely + * follows google/protobuf/text_format.cc. * * @author wenboz@google.com Wenbo Zhu * @author kenton@google.com Kenton Varda @@ -58,158 +58,135 @@ public final class TextFormat { private static final Logger logger = Logger.getLogger(TextFormat.class.getName()); /** - * Outputs a textual representation of the Protocol Message supplied into - * the parameter output. (This representation is the new version of the - * classic "ProtocolPrinter" output from the original Protocol Buffer system) + * Outputs a textual representation of the Protocol Message supplied into the parameter output. + * (This representation is the new version of the classic "ProtocolPrinter" output from the + * original Protocol Buffer system) + * + * @deprecated Use {@code printer().print(MessageOrBuilder, Appendable)} */ - public static void print( - final MessageOrBuilder message, final Appendable output) + @Deprecated + public static void print(final MessageOrBuilder message, final Appendable output) throws IOException { - Printer.DEFAULT.print(message, multiLineOutput(output)); + printer().print(message, output); } - /** Outputs a textual representation of {@code fields} to {@code output}. */ - public static void print(final UnknownFieldSet fields, - final Appendable output) - throws IOException { - Printer.DEFAULT.printUnknownFields(fields, multiLineOutput(output)); + /** + * Outputs a textual representation of {@code fields} to {@code output}. + * + * @deprecated Use {@code printer().print(UnknownFieldSet, Appendable)} + */ + @Deprecated + public static void print(final UnknownFieldSet fields, final Appendable output) + throws IOException { + printer().print(fields, output); } /** - * Same as {@code print()}, except that non-ASCII characters are not - * escaped. + * Same as {@code print()}, except that non-ASCII characters are not escaped. + * + * @deprecated Use {@code printer().escapingNonAscii(false).print(MessageOrBuilder, Appendable)} */ - public static void printUnicode( - final MessageOrBuilder message, final Appendable output) + @Deprecated + public static void printUnicode(final MessageOrBuilder message, final Appendable output) throws IOException { - Printer.UNICODE.print(message, multiLineOutput(output)); + printer().escapingNonAscii(false).print(message, output); } /** - * Same as {@code print()}, except that non-ASCII characters are not - * escaped. + * Same as {@code print()}, except that non-ASCII characters are not escaped. + * + * @deprecated Use {@code printer().escapingNonAscii(false).print(UnknownFieldSet, Appendable)} */ - public static void printUnicode(final UnknownFieldSet fields, - final Appendable output) - throws IOException { - Printer.UNICODE.printUnknownFields(fields, multiLineOutput(output)); + @Deprecated + public static void printUnicode(final UnknownFieldSet fields, final Appendable output) + throws IOException { + printer().escapingNonAscii(false).print(fields, output); } /** - * Generates a human readable form of this message, useful for debugging and - * other purposes, with no newline characters. + * Generates a human readable form of this message, useful for debugging and other purposes, with + * no newline characters. This is just a trivial wrapper around + * {@link TextFormat.Printer#shortDebugString(MessageOrBuilder)}. */ public static String shortDebugString(final MessageOrBuilder message) { - try { - final StringBuilder text = new StringBuilder(); - Printer.DEFAULT.print(message, singleLineOutput(text)); - return text.toString(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + return printer().shortDebugString(message); } /** - * Generates a human readable form of the field, useful for debugging - * and other purposes, with no newline characters. + * Generates a human readable form of the field, useful for debugging and other purposes, with no + * newline characters. + * + * @deprecated Use {@code printer().shortDebugString(FieldDescriptor, Object)} */ - public static String shortDebugString(final FieldDescriptor field, - final Object value) { - try { - final StringBuilder text = new StringBuilder(); - Printer.DEFAULT.printField(field, value, singleLineOutput(text)); - return text.toString(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + @Deprecated + public static String shortDebugString(final FieldDescriptor field, final Object value) { + return printer().shortDebugString(field, value); } /** - * Generates a human readable form of the unknown fields, useful for debugging - * and other purposes, with no newline characters. + * Generates a human readable form of the unknown fields, useful for debugging and other purposes, + * with no newline characters. + * + * @deprecated Use {@code printer().shortDebugString(UnknownFieldSet)} */ + @Deprecated public static String shortDebugString(final UnknownFieldSet fields) { - try { - final StringBuilder text = new StringBuilder(); - Printer.DEFAULT.printUnknownFields(fields, singleLineOutput(text)); - return text.toString(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + return printer().shortDebugString(fields); } /** - * Like {@code print()}, but writes directly to a {@code String} and - * returns it. + * Like {@code print()}, but writes directly to a {@code String} and returns it. + * + * @deprecated Use {@link MessageOrBuilder#toString()} */ + @Deprecated public static String printToString(final MessageOrBuilder message) { - try { - final StringBuilder text = new StringBuilder(); - print(message, text); - return text.toString(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + return printer().printToString(message); } /** - * Like {@code print()}, but writes directly to a {@code String} and - * returns it. + * Like {@code print()}, but writes directly to a {@code String} and returns it. + * + * @deprecated Use {@link UnknownFieldSet#toString()} */ + @Deprecated public static String printToString(final UnknownFieldSet fields) { - try { - final StringBuilder text = new StringBuilder(); - print(fields, text); - return text.toString(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + return printer().printToString(fields); } /** - * Same as {@code printToString()}, except that non-ASCII characters - * in string type fields are not escaped in backslash+octals. + * Same as {@code printToString()}, except that non-ASCII characters in string type fields are not + * escaped in backslash+octals. + * + * @deprecated Use {@code printer().escapingNonAscii(false).printToString(MessageOrBuilder)} */ + @Deprecated public static String printToUnicodeString(final MessageOrBuilder message) { - try { - final StringBuilder text = new StringBuilder(); - Printer.UNICODE.print(message, multiLineOutput(text)); - return text.toString(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + return printer().escapingNonAscii(false).printToString(message); } /** - * Same as {@code printToString()}, except that non-ASCII characters - * in string type fields are not escaped in backslash+octals. + * Same as {@code printToString()}, except that non-ASCII characters in string type fields are not + * escaped in backslash+octals. + * + * @deprecated Use {@code printer().escapingNonAscii(false).printToString(UnknownFieldSet)} */ + @Deprecated public static String printToUnicodeString(final UnknownFieldSet fields) { - try { - final StringBuilder text = new StringBuilder(); - Printer.UNICODE.printUnknownFields(fields, multiLineOutput(text)); - return text.toString(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + return printer().escapingNonAscii(false).printToString(fields); } - public static void printField(final FieldDescriptor field, - final Object value, - final Appendable output) - throws IOException { - Printer.DEFAULT.printField(field, value, multiLineOutput(output)); + /** @deprecated Use {@code printer().printField(FieldDescriptor, Object, Appendable)} */ + @Deprecated + public static void printField( + final FieldDescriptor field, final Object value, final Appendable output) throws IOException { + printer().printField(field, value, output); } - public static String printFieldToString(final FieldDescriptor field, - final Object value) { - try { - final StringBuilder text = new StringBuilder(); - printField(field, value, text); - return text.toString(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + /** @deprecated Use {@code printer().printFieldToString(FieldDescriptor, Object)} */ + @Deprecated + public static String printFieldToString(final FieldDescriptor field, final Object value) { + return printer().printFieldToString(field, value); } /** @@ -218,32 +195,34 @@ public final class TextFormat { *

Same as {@code printFieldValue()}, except that non-ASCII characters in string type fields * are not escaped in backslash+octals. * + * @deprecated Use {@code printer().escapingNonAscii(false).printFieldValue(FieldDescriptor, + * Object, Appendable)} * @param field the descriptor of the field * @param value the value of the field * @param output the output to which to append the formatted value * @throws ClassCastException if the value is not appropriate for the given field descriptor * @throws IOException if there is an exception writing to the output */ + @Deprecated public static void printUnicodeFieldValue( final FieldDescriptor field, final Object value, final Appendable output) throws IOException { - Printer.UNICODE.printFieldValue(field, value, multiLineOutput(output)); + printer().escapingNonAscii(false).printFieldValue(field, value, output); } /** * Outputs a textual representation of the value of given field value. * + * @deprecated Use {@code printer().printFieldValue(FieldDescriptor, Object, Appendable)} * @param field the descriptor of the field * @param value the value of the field * @param output the output to which to append the formatted value - * @throws ClassCastException if the value is not appropriate for the - * given field descriptor + * @throws ClassCastException if the value is not appropriate for the given field descriptor * @throws IOException if there is an exception writing to the output */ - public static void printFieldValue(final FieldDescriptor field, - final Object value, - final Appendable output) - throws IOException { - Printer.DEFAULT.printFieldValue(field, value, multiLineOutput(output)); + @Deprecated + public static void printFieldValue( + final FieldDescriptor field, final Object value, final Appendable output) throws IOException { + printer().printFieldValue(field, value, output); } /** @@ -252,28 +231,22 @@ public final class TextFormat { * @param tag the field's tag number * @param value the value of the field * @param output the output to which to append the formatted value - * @throws ClassCastException if the value is not appropriate for the - * given field descriptor + * @throws ClassCastException if the value is not appropriate for the given field descriptor * @throws IOException if there is an exception writing to the output */ - public static void printUnknownFieldValue(final int tag, - final Object value, - final Appendable output) - throws IOException { + public static void printUnknownFieldValue( + final int tag, final Object value, final Appendable output) throws IOException { printUnknownFieldValue(tag, value, multiLineOutput(output)); } - private static void printUnknownFieldValue(final int tag, - final Object value, - final TextGenerator generator) - throws IOException { + private static void printUnknownFieldValue( + final int tag, final Object value, final TextGenerator generator) throws IOException { switch (WireFormat.getTagWireType(tag)) { case WireFormat.WIRETYPE_VARINT: generator.print(unsignedToString((Long) value)); break; case WireFormat.WIRETYPE_FIXED32: - generator.print( - String.format((Locale) null, "0x%08x", (Integer) value)); + generator.print(String.format((Locale) null, "0x%08x", (Integer) value)); break; case WireFormat.WIRETYPE_FIXED64: generator.print(String.format((Locale) null, "0x%016x", (Long) value)); @@ -285,7 +258,7 @@ public final class TextFormat { generator.print("{"); generator.eol(); generator.indent(); - Printer.DEFAULT.printUnknownFields(message, generator); + Printer.printUnknownFields(message, generator); generator.outdent(); generator.print("}"); } catch (InvalidProtocolBufferException e) { @@ -296,19 +269,23 @@ public final class TextFormat { } break; case WireFormat.WIRETYPE_START_GROUP: - Printer.DEFAULT.printUnknownFields((UnknownFieldSet) value, generator); + Printer.printUnknownFields((UnknownFieldSet) value, generator); break; default: throw new IllegalArgumentException("Bad tag: " + tag); } } + /** Printer instance which escapes non-ASCII characters. */ + public static Printer printer() { + return Printer.DEFAULT; + } + /** Helper class for converting protobufs to text. */ - private static final class Printer { + public static final class Printer { + // Printer instance which escapes non-ASCII characters. - static final Printer DEFAULT = new Printer(true); - // Printer instance which emits Unicode (it still escapes newlines and quotes in strings). - static final Printer UNICODE = new Printer(false); + private static final Printer DEFAULT = new Printer(true); /** Whether to escape non ASCII characters with backslash and octal. */ private final boolean escapeNonAscii; @@ -317,18 +294,56 @@ public final class TextFormat { this.escapeNonAscii = escapeNonAscii; } - private void print( - final MessageOrBuilder message, final TextGenerator generator) + /** + * Return a new Printer instance with the specified escape mode. + * + * @param escapeNonAscii If true, the new Printer will escape non-ASCII characters (this is the + * default behavior. If false, the new Printer will print non-ASCII characters as is. In + * either case, the new Printer still escapes newlines and quotes in strings. + * @return a new Printer that clones all other configurations from the current {@link Printer}, + * with the escape mode set to the given parameter. + */ + public Printer escapingNonAscii(boolean escapeNonAscii) { + return new Printer(escapeNonAscii); + } + + /** + * Outputs a textual representation of the Protocol Message supplied into the parameter output. + * (This representation is the new version of the classic "ProtocolPrinter" output from the + * original Protocol Buffer system) + */ + public void print(final MessageOrBuilder message, final Appendable output) throws IOException { + print(message, multiLineOutput(output)); + } + + /** Outputs a textual representation of {@code fields} to {@code output}. */ + public void print(final UnknownFieldSet fields, final Appendable output) throws IOException { + printUnknownFields(fields, multiLineOutput(output)); + } + + private void print(final MessageOrBuilder message, final TextGenerator generator) throws IOException { - for (Map.Entry field - : message.getAllFields().entrySet()) { - printField(field.getKey(), field.getValue(), generator); + printMessage(message, generator); + } + + public String printFieldToString(final FieldDescriptor field, final Object value) { + try { + final StringBuilder text = new StringBuilder(); + printField(field, value, text); + return text.toString(); + } catch (IOException e) { + throw new IllegalStateException(e); } - printUnknownFields(message.getUnknownFields(), generator); } - private void printField(final FieldDescriptor field, final Object value, - final TextGenerator generator) throws IOException { + public void printField(final FieldDescriptor field, final Object value, final Appendable output) + throws IOException { + printField(field, value, multiLineOutput(output)); + } + + private void printField( + final FieldDescriptor field, final Object value, final TextGenerator generator) + throws IOException { if (field.isRepeated()) { // Repeated field. Print each element. for (Object element : (List) value) { @@ -339,53 +354,24 @@ public final class TextFormat { } } - private void printSingleField(final FieldDescriptor field, - final Object value, - final TextGenerator generator) - throws IOException { - if (field.isExtension()) { - generator.print("["); - // We special-case MessageSet elements for compatibility with proto1. - if (field.getContainingType().getOptions().getMessageSetWireFormat() - && (field.getType() == FieldDescriptor.Type.MESSAGE) - && (field.isOptional()) - // object equality - && (field.getExtensionScope() == field.getMessageType())) { - generator.print(field.getMessageType().getFullName()); - } else { - generator.print(field.getFullName()); - } - generator.print("]"); - } else { - if (field.getType() == FieldDescriptor.Type.GROUP) { - // Groups must be serialized with their original capitalization. - generator.print(field.getMessageType().getName()); - } else { - generator.print(field.getName()); - } - } - - if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { - generator.print(" {"); - generator.eol(); - generator.indent(); - } else { - generator.print(": "); - } - - printFieldValue(field, value, generator); - - if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { - generator.outdent(); - generator.print("}"); - } - generator.eol(); + /** + * Outputs a textual representation of the value of given field value. + * + * @param field the descriptor of the field + * @param value the value of the field + * @param output the output to which to append the formatted value + * @throws ClassCastException if the value is not appropriate for the given field descriptor + * @throws IOException if there is an exception writing to the output + */ + public void printFieldValue( + final FieldDescriptor field, final Object value, final Appendable output) + throws IOException { + printFieldValue(field, value, multiLineOutput(output)); } - private void printFieldValue(final FieldDescriptor field, - final Object value, - final TextGenerator generator) - throws IOException { + private void printFieldValue( + final FieldDescriptor field, final Object value, final TextGenerator generator) + throws IOException { switch (field.getType()) { case INT32: case SINT32: @@ -423,10 +409,10 @@ public final class TextFormat { case STRING: generator.print("\""); - generator.print(escapeNonAscii - ? TextFormatEscaper.escapeText((String) value) - : escapeDoubleQuotesAndBackslashes((String) value) - .replace("\n", "\\n")); + generator.print( + escapeNonAscii + ? TextFormatEscaper.escapeText((String) value) + : escapeDoubleQuotesAndBackslashes((String) value).replace("\n", "\\n")); generator.print("\""); break; @@ -451,21 +437,169 @@ public final class TextFormat { } } - private void printUnknownFields(final UnknownFieldSet unknownFields, - final TextGenerator generator) - throws IOException { - for (Map.Entry entry : - unknownFields.asMap().entrySet()) { + /** Like {@code print()}, but writes directly to a {@code String} and returns it. */ + public String printToString(final MessageOrBuilder message) { + try { + final StringBuilder text = new StringBuilder(); + print(message, text); + return text.toString(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + /** Like {@code print()}, but writes directly to a {@code String} and returns it. */ + public String printToString(final UnknownFieldSet fields) { + try { + final StringBuilder text = new StringBuilder(); + print(fields, text); + return text.toString(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + /** + * Generates a human readable form of this message, useful for debugging and other purposes, + * with no newline characters. + */ + public String shortDebugString(final MessageOrBuilder message) { + try { + final StringBuilder text = new StringBuilder(); + print(message, singleLineOutput(text)); + return text.toString(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + /** + * Generates a human readable form of the field, useful for debugging and other purposes, with + * no newline characters. + */ + public String shortDebugString(final FieldDescriptor field, final Object value) { + try { + final StringBuilder text = new StringBuilder(); + printField(field, value, singleLineOutput(text)); + return text.toString(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + /** + * Generates a human readable form of the unknown fields, useful for debugging and other + * purposes, with no newline characters. + */ + public String shortDebugString(final UnknownFieldSet fields) { + try { + final StringBuilder text = new StringBuilder(); + printUnknownFields(fields, singleLineOutput(text)); + return text.toString(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private static void printUnknownFieldValue( + final int tag, final Object value, final TextGenerator generator) throws IOException { + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + generator.print(unsignedToString((Long) value)); + break; + case WireFormat.WIRETYPE_FIXED32: + generator.print(String.format((Locale) null, "0x%08x", (Integer) value)); + break; + case WireFormat.WIRETYPE_FIXED64: + generator.print(String.format((Locale) null, "0x%016x", (Long) value)); + break; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + try { + // Try to parse and print the field as an embedded message + UnknownFieldSet message = UnknownFieldSet.parseFrom((ByteString) value); + generator.print("{"); + generator.eol(); + generator.indent(); + printUnknownFields(message, generator); + generator.outdent(); + generator.print("}"); + } catch (InvalidProtocolBufferException e) { + // If not parseable as a message, print as a String + generator.print("\""); + generator.print(escapeBytes((ByteString) value)); + generator.print("\""); + } + break; + case WireFormat.WIRETYPE_START_GROUP: + printUnknownFields((UnknownFieldSet) value, generator); + break; + default: + throw new IllegalArgumentException("Bad tag: " + tag); + } + } + + private void printMessage(final MessageOrBuilder message, final TextGenerator generator) + throws IOException { + for (Map.Entry field : message.getAllFields().entrySet()) { + printField(field.getKey(), field.getValue(), generator); + } + printUnknownFields(message.getUnknownFields(), generator); + } + + private void printSingleField( + final FieldDescriptor field, final Object value, final TextGenerator generator) + throws IOException { + if (field.isExtension()) { + generator.print("["); + // We special-case MessageSet elements for compatibility with proto1. + if (field.getContainingType().getOptions().getMessageSetWireFormat() + && (field.getType() == FieldDescriptor.Type.MESSAGE) + && (field.isOptional()) + // object equality + && (field.getExtensionScope() == field.getMessageType())) { + generator.print(field.getMessageType().getFullName()); + } else { + generator.print(field.getFullName()); + } + generator.print("]"); + } else { + if (field.getType() == FieldDescriptor.Type.GROUP) { + // Groups must be serialized with their original capitalization. + generator.print(field.getMessageType().getName()); + } else { + generator.print(field.getName()); + } + } + + if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + generator.print(" {"); + generator.eol(); + generator.indent(); + } else { + generator.print(": "); + } + + printFieldValue(field, value, generator); + + if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + generator.outdent(); + generator.print("}"); + } + generator.eol(); + } + + private static void printUnknownFields( + final UnknownFieldSet unknownFields, final TextGenerator generator) throws IOException { + for (Map.Entry entry : unknownFields.asMap().entrySet()) { final int number = entry.getKey(); final UnknownFieldSet.Field field = entry.getValue(); - printUnknownField(number, WireFormat.WIRETYPE_VARINT, - field.getVarintList(), generator); - printUnknownField(number, WireFormat.WIRETYPE_FIXED32, - field.getFixed32List(), generator); - printUnknownField(number, WireFormat.WIRETYPE_FIXED64, - field.getFixed64List(), generator); - printUnknownField(number, WireFormat.WIRETYPE_LENGTH_DELIMITED, - field.getLengthDelimitedList(), generator); + printUnknownField(number, WireFormat.WIRETYPE_VARINT, field.getVarintList(), generator); + printUnknownField(number, WireFormat.WIRETYPE_FIXED32, field.getFixed32List(), generator); + printUnknownField(number, WireFormat.WIRETYPE_FIXED64, field.getFixed64List(), generator); + printUnknownField( + number, + WireFormat.WIRETYPE_LENGTH_DELIMITED, + field.getLengthDelimitedList(), + generator); for (final UnknownFieldSet value : field.getGroupList()) { generator.print(entry.getKey().toString()); generator.print(" {"); @@ -479,11 +613,9 @@ public final class TextFormat { } } - private void printUnknownField(final int number, - final int wireType, - final List values, - final TextGenerator generator) - throws IOException { + private static void printUnknownField( + final int number, final int wireType, final List values, final TextGenerator generator) + throws IOException { for (final Object value : values) { generator.print(String.valueOf(number)); generator.print(": "); @@ -509,8 +641,7 @@ public final class TextFormat { } else { // Pull off the most-significant bit so that BigInteger doesn't think // the number is negative, then set it again using setBit(). - return BigInteger.valueOf(value & 0x7FFFFFFFFFFFFFFFL) - .setBit(63).toString(); + return BigInteger.valueOf(value & 0x7FFFFFFFFFFFFFFFL).setBit(63).toString(); } } @@ -522,9 +653,7 @@ public final class TextFormat { return new TextGenerator(output, true); } - /** - * An inner class for writing text to the output stream. - */ + /** An inner class for writing text to the output stream. */ private static final class TextGenerator { private final Appendable output; private final StringBuilder indent = new StringBuilder(); @@ -540,23 +669,19 @@ public final class TextFormat { } /** - * Indent text by two spaces. After calling Indent(), two spaces will be - * inserted at the beginning of each line of text. Indent() may be called - * multiple times to produce deeper indents. + * Indent text by two spaces. After calling Indent(), two spaces will be inserted at the + * beginning of each line of text. Indent() may be called multiple times to produce deeper + * indents. */ public void indent() { indent.append(" "); } - /** - * Reduces the current indent level by two spaces, or crashes if the indent - * level is zero. - */ + /** Reduces the current indent level by two spaces, or crashes if the indent level is zero. */ public void outdent() { final int length = indent.length(); if (length == 0) { - throw new IllegalArgumentException( - " Outdent() without matching Indent()."); + throw new IllegalArgumentException(" Outdent() without matching Indent()."); } indent.setLength(length - 2); } @@ -592,30 +717,26 @@ public final class TextFormat { /** * Represents a stream of tokens parsed from a {@code String}. * - *

The Java standard library provides many classes that you might think - * would be useful for implementing this, but aren't. For example: + *

The Java standard library provides many classes that you might think would be useful for + * implementing this, but aren't. For example: * *

    - *
  • {@code java.io.StreamTokenizer}: This almost does what we want -- or, - * at least, something that would get us close to what we want -- except - * for one fatal flaw: It automatically un-escapes strings using Java - * escape sequences, which do not include all the escape sequences we - * need to support (e.g. '\x'). - *
  • {@code java.util.Scanner}: This seems like a great way at least to - * parse regular expressions out of a stream (so we wouldn't have to load - * the entire input into a single string before parsing). Sadly, - * {@code Scanner} requires that tokens be delimited with some delimiter. - * Thus, although the text "foo:" should parse to two tokens ("foo" and - * ":"), {@code Scanner} would recognize it only as a single token. - * Furthermore, {@code Scanner} provides no way to inspect the contents - * of delimiters, making it impossible to keep track of line and column - * numbers. + *
  • {@code java.io.StreamTokenizer}: This almost does what we want -- or, at least, something + * that would get us close to what we want -- except for one fatal flaw: It automatically + * un-escapes strings using Java escape sequences, which do not include all the escape + * sequences we need to support (e.g. '\x'). + *
  • {@code java.util.Scanner}: This seems like a great way at least to parse regular + * expressions out of a stream (so we wouldn't have to load the entire input into a single + * string before parsing). Sadly, {@code Scanner} requires that tokens be delimited with + * some delimiter. Thus, although the text "foo:" should parse to two tokens ("foo" and + * ":"), {@code Scanner} would recognize it only as a single token. Furthermore, {@code + * Scanner} provides no way to inspect the contents of delimiters, making it impossible to + * keep track of line and column numbers. *
* - *

Luckily, Java's regular expression support does manage to be useful to - * us. (Barely: We need {@code Matcher.usePattern()}, which is new in - * Java 1.5.) So, we can use that, at least. Unfortunately, this implies - * that we need to have the entire input in one contiguous string. + *

Luckily, Java's regular expression support does manage to be useful to us. (Barely: We need + * {@code Matcher.usePattern()}, which is new in Java 1.5.) So, we can use that, at least. + * Unfortunately, this implies that we need to have the entire input in one contiguous string. */ private static final class Tokenizer { private final CharSequence text; @@ -636,24 +757,20 @@ public final class TextFormat { // We use possessive quantifiers (*+ and ++) because otherwise the Java // regex matcher has stack overflows on large inputs. - private static final Pattern WHITESPACE = - Pattern.compile("(\\s|(#.*$))++", Pattern.MULTILINE); - private static final Pattern TOKEN = Pattern.compile( - "[a-zA-Z_][0-9a-zA-Z_+-]*+|" + // an identifier - "[.]?[0-9+-][0-9a-zA-Z_.+-]*+|" + // a number - "\"([^\"\n\\\\]|\\\\.)*+(\"|\\\\?$)|" + // a double-quoted string - "\'([^\'\n\\\\]|\\\\.)*+(\'|\\\\?$)", // a single-quoted string - Pattern.MULTILINE); - - private static final Pattern DOUBLE_INFINITY = Pattern.compile( - "-?inf(inity)?", - Pattern.CASE_INSENSITIVE); - private static final Pattern FLOAT_INFINITY = Pattern.compile( - "-?inf(inity)?f?", - Pattern.CASE_INSENSITIVE); - private static final Pattern FLOAT_NAN = Pattern.compile( - "nanf?", - Pattern.CASE_INSENSITIVE); + private static final Pattern WHITESPACE = Pattern.compile("(\\s|(#.*$))++", Pattern.MULTILINE); + private static final Pattern TOKEN = + Pattern.compile( + "[a-zA-Z_][0-9a-zA-Z_+-]*+|" // an identifier + + "[.]?[0-9+-][0-9a-zA-Z_.+-]*+|" // a number + + "\"([^\"\n\\\\]|\\\\.)*+(\"|\\\\?$)|" // a double-quoted string + + "\'([^\'\n\\\\]|\\\\.)*+(\'|\\\\?$)", // a single-quoted string + Pattern.MULTILINE); + + private static final Pattern DOUBLE_INFINITY = + Pattern.compile("-?inf(inity)?", Pattern.CASE_INSENSITIVE); + private static final Pattern FLOAT_INFINITY = + Pattern.compile("-?inf(inity)?f?", Pattern.CASE_INSENSITIVE); + private static final Pattern FLOAT_NAN = Pattern.compile("nanf?", Pattern.CASE_INSENSITIVE); /** Construct a tokenizer that parses tokens from the given text. */ private Tokenizer(final CharSequence text) { @@ -719,10 +836,7 @@ public final class TextFormat { } } - /** - * Skip over any whitespace so that the matcher region starts at the next - * token. - */ + /** Skip over any whitespace so that the matcher region starts at the next token. */ private void skipWhitespace() { matcher.usePattern(WHITESPACE); if (matcher.lookingAt()) { @@ -731,8 +845,8 @@ public final class TextFormat { } /** - * If the next token exactly matches {@code token}, consume it and return - * {@code true}. Otherwise, return {@code false} without doing anything. + * If the next token exactly matches {@code token}, consume it and return {@code true}. + * Otherwise, return {@code false} without doing anything. */ public boolean tryConsume(final String token) { if (currentToken.equals(token)) { @@ -744,8 +858,8 @@ public final class TextFormat { } /** - * If the next token exactly matches {@code token}, consume it. Otherwise, - * throw a {@link ParseException}. + * If the next token exactly matches {@code token}, consume it. Otherwise, throw a {@link + * ParseException}. */ public void consume(final String token) throws ParseException { if (!tryConsume(token)) { @@ -753,31 +867,24 @@ public final class TextFormat { } } - /** - * Returns {@code true} if the next token is an integer, but does - * not consume it. - */ + /** Returns {@code true} if the next token is an integer, but does not consume it. */ public boolean lookingAtInteger() { if (currentToken.length() == 0) { return false; } final char c = currentToken.charAt(0); - return ('0' <= c && c <= '9') - || c == '-' || c == '+'; + return ('0' <= c && c <= '9') || c == '-' || c == '+'; } - /** - * Returns {@code true} if the current token's text is equal to that - * specified. - */ + /** Returns {@code true} if the current token's text is equal to that specified. */ public boolean lookingAt(String text) { return currentToken.equals(text); } /** - * If the next token is an identifier, consume it and return its value. - * Otherwise, throw a {@link ParseException}. + * If the next token is an identifier, consume it and return its value. Otherwise, throw a + * {@link ParseException}. */ public String consumeIdentifier() throws ParseException { for (int i = 0; i < currentToken.length(); i++) { @@ -785,11 +892,11 @@ public final class TextFormat { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') - || (c == '_') || (c == '.')) { + || (c == '_') + || (c == '.')) { // OK } else { - throw parseException( - "Expected identifier. Found '" + currentToken + "'"); + throw parseException("Expected identifier. Found '" + currentToken + "'"); } } @@ -799,8 +906,8 @@ public final class TextFormat { } /** - * If the next token is an identifier, consume it and return {@code true}. - * Otherwise, return {@code false} without doing anything. + * If the next token is an identifier, consume it and return {@code true}. Otherwise, return + * {@code false} without doing anything. */ public boolean tryConsumeIdentifier() { try { @@ -812,8 +919,8 @@ public final class TextFormat { } /** - * If the next token is a 32-bit signed integer, consume it and return its - * value. Otherwise, throw a {@link ParseException}. + * If the next token is a 32-bit signed integer, consume it and return its value. Otherwise, + * throw a {@link ParseException}. */ public int consumeInt32() throws ParseException { try { @@ -826,8 +933,8 @@ public final class TextFormat { } /** - * If the next token is a 32-bit unsigned integer, consume it and return its - * value. Otherwise, throw a {@link ParseException}. + * If the next token is a 32-bit unsigned integer, consume it and return its value. Otherwise, + * throw a {@link ParseException}. */ public int consumeUInt32() throws ParseException { try { @@ -840,8 +947,8 @@ public final class TextFormat { } /** - * If the next token is a 64-bit signed integer, consume it and return its - * value. Otherwise, throw a {@link ParseException}. + * If the next token is a 64-bit signed integer, consume it and return its value. Otherwise, + * throw a {@link ParseException}. */ public long consumeInt64() throws ParseException { try { @@ -854,8 +961,8 @@ public final class TextFormat { } /** - * If the next token is a 64-bit signed integer, consume it and return - * {@code true}. Otherwise, return {@code false} without doing anything. + * If the next token is a 64-bit signed integer, consume it and return {@code true}. Otherwise, + * return {@code false} without doing anything. */ public boolean tryConsumeInt64() { try { @@ -867,8 +974,8 @@ public final class TextFormat { } /** - * If the next token is a 64-bit unsigned integer, consume it and return its - * value. Otherwise, throw a {@link ParseException}. + * If the next token is a 64-bit unsigned integer, consume it and return its value. Otherwise, + * throw a {@link ParseException}. */ public long consumeUInt64() throws ParseException { try { @@ -881,8 +988,8 @@ public final class TextFormat { } /** - * If the next token is a 64-bit unsigned integer, consume it and return - * {@code true}. Otherwise, return {@code false} without doing anything. + * If the next token is a 64-bit unsigned integer, consume it and return {@code true}. + * Otherwise, return {@code false} without doing anything. */ public boolean tryConsumeUInt64() { try { @@ -894,8 +1001,8 @@ public final class TextFormat { } /** - * If the next token is a double, consume it and return its value. - * Otherwise, throw a {@link ParseException}. + * If the next token is a double, consume it and return its value. Otherwise, throw a {@link + * ParseException}. */ public double consumeDouble() throws ParseException { // We need to parse infinity and nan separately because @@ -919,8 +1026,8 @@ public final class TextFormat { } /** - * If the next token is a double, consume it and return {@code true}. - * Otherwise, return {@code false} without doing anything. + * If the next token is a double, consume it and return {@code true}. Otherwise, return {@code + * false} without doing anything. */ public boolean tryConsumeDouble() { try { @@ -932,8 +1039,8 @@ public final class TextFormat { } /** - * If the next token is a float, consume it and return its value. - * Otherwise, throw a {@link ParseException}. + * If the next token is a float, consume it and return its value. Otherwise, throw a {@link + * ParseException}. */ public float consumeFloat() throws ParseException { // We need to parse infinity and nan separately because @@ -957,8 +1064,8 @@ public final class TextFormat { } /** - * If the next token is a float, consume it and return {@code true}. - * Otherwise, return {@code false} without doing anything. + * If the next token is a float, consume it and return {@code true}. Otherwise, return {@code + * false} without doing anything. */ public boolean tryConsumeFloat() { try { @@ -970,8 +1077,8 @@ public final class TextFormat { } /** - * If the next token is a boolean, consume it and return its value. - * Otherwise, throw a {@link ParseException}. + * If the next token is a boolean, consume it and return its value. Otherwise, throw a {@link + * ParseException}. */ public boolean consumeBoolean() throws ParseException { if (currentToken.equals("true") @@ -992,17 +1099,14 @@ public final class TextFormat { } /** - * If the next token is a string, consume it and return its (unescaped) - * value. Otherwise, throw a {@link ParseException}. + * If the next token is a string, consume it and return its (unescaped) value. Otherwise, throw + * a {@link ParseException}. */ public String consumeString() throws ParseException { return consumeByteString().toStringUtf8(); } - /** - * If the next token is a string, consume it and return true. Otherwise, - * return false. - */ + /** If the next token is a string, consume it and return true. Otherwise, return false. */ public boolean tryConsumeString() { try { consumeString(); @@ -1013,9 +1117,8 @@ public final class TextFormat { } /** - * If the next token is a string, consume it, unescape it as a - * {@link ByteString}, and return it. Otherwise, throw a - * {@link ParseException}. + * If the next token is a string, consume it, unescape it as a {@link ByteString}, and return + * it. Otherwise, throw a {@link ParseException}. */ public ByteString consumeByteString() throws ParseException { List list = new ArrayList(); @@ -1027,28 +1130,22 @@ public final class TextFormat { } /** - * Like {@link #consumeByteString()} but adds each token of the string to - * the given list. String literals (whether bytes or text) may come in - * multiple adjacent tokens which are automatically concatenated, like in - * C or Python. + * Like {@link #consumeByteString()} but adds each token of the string to the given list. String + * literals (whether bytes or text) may come in multiple adjacent tokens which are automatically + * concatenated, like in C or Python. */ - private void consumeByteString(List list) - throws ParseException { - final char quote = currentToken.length() > 0 - ? currentToken.charAt(0) - : '\0'; + private void consumeByteString(List list) throws ParseException { + final char quote = currentToken.length() > 0 ? currentToken.charAt(0) : '\0'; if (quote != '\"' && quote != '\'') { throw parseException("Expected string."); } - if (currentToken.length() < 2 - || currentToken.charAt(currentToken.length() - 1) != quote) { + if (currentToken.length() < 2 || currentToken.charAt(currentToken.length() - 1) != quote) { throw parseException("String missing ending quote."); } try { - final String escaped = - currentToken.substring(1, currentToken.length() - 1); + final String escaped = currentToken.substring(1, currentToken.length() - 1); final ByteString result = unescapeBytes(escaped); nextToken(); list.add(result); @@ -1058,53 +1155,48 @@ public final class TextFormat { } /** - * Returns a {@link ParseException} with the current line and column - * numbers in the description, suitable for throwing. + * Returns a {@link ParseException} with the current line and column numbers in the description, + * suitable for throwing. */ public ParseException parseException(final String description) { // Note: People generally prefer one-based line and column numbers. - return new ParseException( - line + 1, column + 1, description); + return new ParseException(line + 1, column + 1, description); } /** - * Returns a {@link ParseException} with the line and column numbers of - * the previous token in the description, suitable for throwing. + * Returns a {@link ParseException} with the line and column numbers of the previous token in + * the description, suitable for throwing. */ - public ParseException parseExceptionPreviousToken( - final String description) { + public ParseException parseExceptionPreviousToken(final String description) { // Note: People generally prefer one-based line and column numbers. - return new ParseException( - previousLine + 1, previousColumn + 1, description); + return new ParseException(previousLine + 1, previousColumn + 1, description); } /** - * Constructs an appropriate {@link ParseException} for the given - * {@code NumberFormatException} when trying to parse an integer. + * Constructs an appropriate {@link ParseException} for the given {@code NumberFormatException} + * when trying to parse an integer. */ - private ParseException integerParseException( - final NumberFormatException e) { + private ParseException integerParseException(final NumberFormatException e) { return parseException("Couldn't parse integer: " + e.getMessage()); } /** - * Constructs an appropriate {@link ParseException} for the given - * {@code NumberFormatException} when trying to parse a float or double. + * Constructs an appropriate {@link ParseException} for the given {@code NumberFormatException} + * when trying to parse a float or double. */ private ParseException floatParseException(final NumberFormatException e) { return parseException("Couldn't parse number: " + e.getMessage()); } /** - * Returns a {@link UnknownFieldParseException} with the line and column - * numbers of the previous token in the description, and the unknown field - * name, suitable for throwing. + * Returns a {@link UnknownFieldParseException} with the line and column numbers of the previous + * token in the description, and the unknown field name, suitable for throwing. */ public UnknownFieldParseException unknownFieldParseExceptionPreviousToken( final String unknownField, final String description) { // Note: People generally prefer one-based line and column numbers. return new UnknownFieldParseException( - previousLine + 1, previousColumn + 1, unknownField, description); + previousLine + 1, previousColumn + 1, unknownField, description); } } @@ -1123,47 +1215,39 @@ public final class TextFormat { /** * Create a new instance * - * @param line the line number where the parse error occurred, - * using 1-offset. - * @param column the column number where the parser error occurred, - * using 1-offset. + * @param line the line number where the parse error occurred, using 1-offset. + * @param column the column number where the parser error occurred, using 1-offset. */ - public ParseException(final int line, final int column, - final String message) { + public ParseException(final int line, final int column, final String message) { super(Integer.toString(line) + ":" + column + ": " + message); this.line = line; this.column = column; } /** - * Return the line where the parse exception occurred, or -1 when - * none is provided. The value is specified as 1-offset, so the first - * line is line 1. + * Return the line where the parse exception occurred, or -1 when none is provided. The value is + * specified as 1-offset, so the first line is line 1. */ public int getLine() { return line; } /** - * Return the column where the parse exception occurred, or -1 when - * none is provided. The value is specified as 1-offset, so the first - * line is line 1. + * Return the column where the parse exception occurred, or -1 when none is provided. The value + * is specified as 1-offset, so the first line is line 1. */ public int getColumn() { return column; } } - /** - * Thrown when encountering an unknown field while parsing - * a text format message. - */ + /** Thrown when encountering an unknown field while parsing a text format message. */ public static class UnknownFieldParseException extends ParseException { private final String unknownField; /** - * Create a new instance, with -1 as the line and column numbers, and an - * empty unknown field name. + * Create a new instance, with -1 as the line and column numbers, and an empty unknown field + * name. */ public UnknownFieldParseException(final String message) { this(-1, -1, "", message); @@ -1172,21 +1256,18 @@ public final class TextFormat { /** * Create a new instance * - * @param line the line number where the parse error occurred, - * using 1-offset. - * @param column the column number where the parser error occurred, - * using 1-offset. + * @param line the line number where the parse error occurred, using 1-offset. + * @param column the column number where the parser error occurred, using 1-offset. * @param unknownField the name of the unknown field found while parsing. */ - public UnknownFieldParseException(final int line, final int column, - final String unknownField, final String message) { + public UnknownFieldParseException( + final int line, final int column, final String unknownField, final String message) { super(line, column, message); this.unknownField = unknownField; } /** - * Return the name of the unknown field encountered while parsing the - * protocol buffer string. + * Return the name of the unknown field encountered while parsing the protocol buffer string. */ public String getUnknownField() { return unknownField; @@ -1196,30 +1277,21 @@ public final class TextFormat { private static final Parser PARSER = Parser.newBuilder().build(); /** - * Return a {@link Parser} instance which can parse text-format - * messages. The returned instance is thread-safe. + * Return a {@link Parser} instance which can parse text-format messages. The returned instance is + * thread-safe. */ public static Parser getParser() { return PARSER; } - /** - * Parse a text-format message from {@code input} and merge the contents - * into {@code builder}. - */ - public static void merge(final Readable input, - final Message.Builder builder) - throws IOException { + /** Parse a text-format message from {@code input} and merge the contents into {@code builder}. */ + public static void merge(final Readable input, final Message.Builder builder) throws IOException { PARSER.merge(input, builder); } - /** - * Parse a text-format message from {@code input} and merge the contents - * into {@code builder}. - */ - public static void merge(final CharSequence input, - final Message.Builder builder) - throws ParseException { + /** Parse a text-format message from {@code input} and merge the contents into {@code builder}. */ + public static void merge(final CharSequence input, final Message.Builder builder) + throws ParseException { PARSER.merge(input, builder); } @@ -1228,11 +1300,9 @@ public final class TextFormat { * * @return the parsed message, guaranteed initialized */ - public static T parse(final CharSequence input, - final Class protoClass) - throws ParseException { - Message.Builder builder = - Internal.getDefaultInstance(protoClass).newBuilderForType(); + public static T parse(final CharSequence input, final Class protoClass) + throws ParseException { + Message.Builder builder = Internal.getDefaultInstance(protoClass).newBuilderForType(); merge(input, builder); @SuppressWarnings("unchecked") T output = (T) builder.build(); @@ -1240,33 +1310,33 @@ public final class TextFormat { } /** - * Parse a text-format message from {@code input} and merge the contents - * into {@code builder}. Extensions will be recognized if they are - * registered in {@code extensionRegistry}. + * Parse a text-format message from {@code input} and merge the contents into {@code builder}. + * Extensions will be recognized if they are registered in {@code extensionRegistry}. */ - public static void merge(final Readable input, - final ExtensionRegistry extensionRegistry, - final Message.Builder builder) - throws IOException { + public static void merge( + final Readable input, + final ExtensionRegistry extensionRegistry, + final Message.Builder builder) + throws IOException { PARSER.merge(input, extensionRegistry, builder); } /** - * Parse a text-format message from {@code input} and merge the contents - * into {@code builder}. Extensions will be recognized if they are - * registered in {@code extensionRegistry}. + * Parse a text-format message from {@code input} and merge the contents into {@code builder}. + * Extensions will be recognized if they are registered in {@code extensionRegistry}. */ - public static void merge(final CharSequence input, - final ExtensionRegistry extensionRegistry, - final Message.Builder builder) - throws ParseException { + public static void merge( + final CharSequence input, + final ExtensionRegistry extensionRegistry, + final Message.Builder builder) + throws ParseException { PARSER.merge(input, extensionRegistry, builder); } /** - * Parse a text-format message from {@code input}. Extensions will be - * recognized if they are registered in {@code extensionRegistry}. + * Parse a text-format message from {@code input}. Extensions will be recognized if they are + * registered in {@code extensionRegistry}. * * @return the parsed message, guaranteed initialized */ @@ -1275,8 +1345,7 @@ public final class TextFormat { final ExtensionRegistry extensionRegistry, final Class protoClass) throws ParseException { - Message.Builder builder = - Internal.getDefaultInstance(protoClass).newBuilderForType(); + Message.Builder builder = Internal.getDefaultInstance(protoClass).newBuilderForType(); merge(input, extensionRegistry, builder); @SuppressWarnings("unchecked") T output = (T) builder.build(); @@ -1285,26 +1354,29 @@ public final class TextFormat { /** - * Parser for text-format proto2 instances. This class is thread-safe. - * The implementation largely follows google/protobuf/text_format.cc. + * Parser for text-format proto2 instances. This class is thread-safe. The implementation largely + * follows google/protobuf/text_format.cc. * - *

Use {@link TextFormat#getParser()} to obtain the default parser, or - * {@link Builder} to control the parser behavior. + *

Use {@link TextFormat#getParser()} to obtain the default parser, or {@link Builder} to + * control the parser behavior. */ public static class Parser { /** - * Determines if repeated values for non-repeated fields and - * oneofs are permitted. For example, given required/optional field "foo" - * and a oneof containing "baz" and "qux": + * Determines if repeated values for non-repeated fields and oneofs are permitted. For example, + * given required/optional field "foo" and a oneof containing "baz" and "qux": + * *

    - *
  • "foo: 1 foo: 2" - *
  • "baz: 1 qux: 2" - *
  • merging "foo: 2" into a proto in which foo is already set, or - *
  • merging "qux: 2" into a proto in which baz is already set. + *
  • "foo: 1 foo: 2" + *
  • "baz: 1 qux: 2" + *
  • merging "foo: 2" into a proto in which foo is already set, or + *
  • merging "qux: 2" into a proto in which baz is already set. *
*/ public enum SingularOverwritePolicy { - /** The last value is retained. */ + /** + * Later values are merged with earlier values. For primitive fields or conflicting oneofs, + * the last value is retained. + */ ALLOW_SINGULAR_OVERWRITES, /** An error is issued. */ FORBID_SINGULAR_OVERWRITES @@ -1312,48 +1384,68 @@ public final class TextFormat { private final boolean allowUnknownFields; private final boolean allowUnknownEnumValues; + private final boolean allowUnknownExtensions; private final SingularOverwritePolicy singularOverwritePolicy; private TextFormatParseInfoTree.Builder parseInfoTreeBuilder; private Parser( boolean allowUnknownFields, boolean allowUnknownEnumValues, + boolean allowUnknownExtensions, SingularOverwritePolicy singularOverwritePolicy, TextFormatParseInfoTree.Builder parseInfoTreeBuilder) { this.allowUnknownFields = allowUnknownFields; this.allowUnknownEnumValues = allowUnknownEnumValues; + this.allowUnknownExtensions = allowUnknownExtensions; this.singularOverwritePolicy = singularOverwritePolicy; this.parseInfoTreeBuilder = parseInfoTreeBuilder; } - /** - * Returns a new instance of {@link Builder}. - */ + /** Returns a new instance of {@link Builder}. */ public static Builder newBuilder() { return new Builder(); } - /** - * Builder that can be used to obtain new instances of {@link Parser}. - */ + /** Builder that can be used to obtain new instances of {@link Parser}. */ public static class Builder { private boolean allowUnknownFields = false; private boolean allowUnknownEnumValues = false; + private boolean allowUnknownExtensions = false; private SingularOverwritePolicy singularOverwritePolicy = SingularOverwritePolicy.ALLOW_SINGULAR_OVERWRITES; private TextFormatParseInfoTree.Builder parseInfoTreeBuilder = null; + /** + * Set whether this parser will allow unknown fields. By default, an exception is thrown if an + * unknown field is encountered. If this is set, the parser will only log a warning. Allow + * unknown fields will also allow unknown extensions. + * + *

Use of this parameter is discouraged which may hide some errors (e.g. + * spelling error on field name). + */ + public Builder setAllowUnknownFields(boolean allowUnknownFields) { + this.allowUnknownFields = allowUnknownFields; + return this; + } /** - * Sets parser behavior when a non-repeated field appears more than once. + * Set whether this parser will allow unknown extensions. By default, an + * exception is thrown if unknown extension is encountered. If this is set true, + * the parser will only log a warning. Allow unknown extensions does not mean + * allow normal unknown fields. */ + public Builder setAllowUnknownExtensions(boolean allowUnknownExtensions) { + this.allowUnknownExtensions = allowUnknownExtensions; + return this; + } + + /** Sets parser behavior when a non-repeated field appears more than once. */ public Builder setSingularOverwritePolicy(SingularOverwritePolicy p) { this.singularOverwritePolicy = p; return this; } - public Builder setParseInfoTreeBuilder( - TextFormatParseInfoTree.Builder parseInfoTreeBuilder) { + public Builder setParseInfoTreeBuilder(TextFormatParseInfoTree.Builder parseInfoTreeBuilder) { this.parseInfoTreeBuilder = parseInfoTreeBuilder; return this; } @@ -1362,40 +1454,36 @@ public final class TextFormat { return new Parser( allowUnknownFields, allowUnknownEnumValues, + allowUnknownExtensions, singularOverwritePolicy, parseInfoTreeBuilder); } } /** - * Parse a text-format message from {@code input} and merge the contents - * into {@code builder}. + * Parse a text-format message from {@code input} and merge the contents into {@code builder}. */ - public void merge(final Readable input, - final Message.Builder builder) - throws IOException { + public void merge(final Readable input, final Message.Builder builder) throws IOException { merge(input, ExtensionRegistry.getEmptyRegistry(), builder); } /** - * Parse a text-format message from {@code input} and merge the contents - * into {@code builder}. + * Parse a text-format message from {@code input} and merge the contents into {@code builder}. */ - public void merge(final CharSequence input, - final Message.Builder builder) - throws ParseException { + public void merge(final CharSequence input, final Message.Builder builder) + throws ParseException { merge(input, ExtensionRegistry.getEmptyRegistry(), builder); } /** - * Parse a text-format message from {@code input} and merge the contents - * into {@code builder}. Extensions will be recognized if they are - * registered in {@code extensionRegistry}. + * Parse a text-format message from {@code input} and merge the contents into {@code builder}. + * Extensions will be recognized if they are registered in {@code extensionRegistry}. */ - public void merge(final Readable input, - final ExtensionRegistry extensionRegistry, - final Message.Builder builder) - throws IOException { + public void merge( + final Readable input, + final ExtensionRegistry extensionRegistry, + final Message.Builder builder) + throws IOException { // Read the entire input to a String then parse that. // If StreamTokenizer were not quite so crippled, or if there were a kind @@ -1412,8 +1500,7 @@ public final class TextFormat { // TODO(chrisn): See if working around java.io.Reader#read(CharBuffer) // overhead is worthwhile - private static StringBuilder toStringBuilder(final Readable input) - throws IOException { + private static StringBuilder toStringBuilder(final Readable input) throws IOException { final StringBuilder text = new StringBuilder(); final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE); while (true) { @@ -1427,42 +1514,71 @@ public final class TextFormat { return text; } + static final class UnknownField { + static enum Type { + FIELD, EXTENSION; + } + + final String message; + final Type type; + + UnknownField(String message, Type type) { + this.message = message; + this.type = type; + } + } + // Check both unknown fields and unknown extensions and log warning messages // or throw exceptions according to the flag. - private void checkUnknownFields(final List unknownFields) - throws ParseException { + private void checkUnknownFields(final List unknownFields) throws ParseException { if (unknownFields.isEmpty()) { return; } StringBuilder msg = new StringBuilder("Input contains unknown fields and/or extensions:"); - for (String field : unknownFields) { - msg.append('\n').append(field); + for (UnknownField field : unknownFields) { + msg.append('\n').append(field.message); } if (allowUnknownFields) { + logger.warning(msg.toString()); + return; + } + + int firstErrorIndex = 0; + if (allowUnknownExtensions) { + boolean allUnknownExtensions = true; + for (UnknownField field : unknownFields) { + if (field.type == UnknownField.Type.FIELD) { + allUnknownExtensions = false; + break; + } + ++firstErrorIndex; + } + if (allUnknownExtensions) { logger.warning(msg.toString()); - } else { - String[] lineColumn = unknownFields.get(0).split(":"); - throw new ParseException( - Integer.parseInt(lineColumn[0]), Integer.parseInt(lineColumn[1]), msg.toString()); + return; + } } + + String[] lineColumn = unknownFields.get(firstErrorIndex).message.split(":"); + throw new ParseException( + Integer.parseInt(lineColumn[0]), Integer.parseInt(lineColumn[1]), msg.toString()); } /** - * Parse a text-format message from {@code input} and merge the contents - * into {@code builder}. Extensions will be recognized if they are - * registered in {@code extensionRegistry}. + * Parse a text-format message from {@code input} and merge the contents into {@code builder}. + * Extensions will be recognized if they are registered in {@code extensionRegistry}. */ - public void merge(final CharSequence input, - final ExtensionRegistry extensionRegistry, - final Message.Builder builder) - throws ParseException { + public void merge( + final CharSequence input, + final ExtensionRegistry extensionRegistry, + final Message.Builder builder) + throws ParseException { final Tokenizer tokenizer = new Tokenizer(input); - MessageReflection.BuilderAdapter target = - new MessageReflection.BuilderAdapter(builder); + MessageReflection.BuilderAdapter target = new MessageReflection.BuilderAdapter(builder); - List unknownFields = new ArrayList(); + List unknownFields = new ArrayList(); while (!tokenizer.atEnd()) { mergeField(tokenizer, extensionRegistry, target, unknownFields); @@ -1472,29 +1588,29 @@ public final class TextFormat { } - /** - * Parse a single field from {@code tokenizer} and merge it into - * {@code builder}. - */ - private void mergeField(final Tokenizer tokenizer, - final ExtensionRegistry extensionRegistry, - final MessageReflection.MergeTarget target, - List unknownFields) - throws ParseException { - mergeField(tokenizer, extensionRegistry, target, parseInfoTreeBuilder, - unknownFields); + /** Parse a single field from {@code tokenizer} and merge it into {@code builder}. */ + private void mergeField( + final Tokenizer tokenizer, + final ExtensionRegistry extensionRegistry, + final MessageReflection.MergeTarget target, + List unknownFields) + throws ParseException { + mergeField( + tokenizer, + extensionRegistry, + target, + parseInfoTreeBuilder, + unknownFields); } - /** - * Parse a single field from {@code tokenizer} and merge it into - * {@code target}. - */ - private void mergeField(final Tokenizer tokenizer, - final ExtensionRegistry extensionRegistry, - final MessageReflection.MergeTarget target, - TextFormatParseInfoTree.Builder parseTreeBuilder, - List unknownFields) - throws ParseException { + /** Parse a single field from {@code tokenizer} and merge it into {@code target}. */ + private void mergeField( + final Tokenizer tokenizer, + final ExtensionRegistry extensionRegistry, + final MessageReflection.MergeTarget target, + TextFormatParseInfoTree.Builder parseTreeBuilder, + List unknownFields) + throws ParseException { FieldDescriptor field = null; int startLine = tokenizer.getLine(); int startColumn = tokenizer.getColumn(); @@ -1503,18 +1619,16 @@ public final class TextFormat { if (tokenizer.tryConsume("[")) { // An extension. - final StringBuilder name = - new StringBuilder(tokenizer.consumeIdentifier()); + final StringBuilder name = new StringBuilder(tokenizer.consumeIdentifier()); while (tokenizer.tryConsume(".")) { name.append('.'); name.append(tokenizer.consumeIdentifier()); } - extension = target.findExtensionByName( - extensionRegistry, name.toString()); + extension = target.findExtensionByName(extensionRegistry, name.toString()); if (extension == null) { - unknownFields.add( + String message = (tokenizer.getPreviousLine() + 1) + ":" + (tokenizer.getPreviousColumn() + 1) @@ -1522,12 +1636,16 @@ public final class TextFormat { + type.getFullName() + ".[" + name - + "]"); + + "]"; + unknownFields.add(new UnknownField(message, UnknownField.Type.EXTENSION)); } else { if (extension.descriptor.getContainingType() != type) { throw tokenizer.parseExceptionPreviousToken( - "Extension \"" + name + "\" does not extend message type \"" - + type.getFullName() + "\"."); + "Extension \"" + + name + + "\" does not extend message type \"" + + type.getFullName() + + "\"."); } field = extension.descriptor; } @@ -1551,20 +1669,21 @@ public final class TextFormat { } } // Again, special-case group names as described above. - if (field != null && field.getType() == FieldDescriptor.Type.GROUP + if (field != null + && field.getType() == FieldDescriptor.Type.GROUP && !field.getMessageType().getName().equals(name)) { field = null; } if (field == null) { - unknownFields.add( - (tokenizer.getPreviousLine() + 1) - + ":" - + (tokenizer.getPreviousColumn() + 1) - + ":\t" - + type.getFullName() - + "." - + name); + String message = (tokenizer.getPreviousLine() + 1) + + ":" + + (tokenizer.getPreviousColumn() + 1) + + ":\t" + + type.getFullName() + + "." + + name; + unknownFields.add(new UnknownField(message, UnknownField.Type.FIELD)); } } @@ -1576,9 +1695,7 @@ public final class TextFormat { // start with "{" or "<" which indicates the beginning of a message body. // If there is no ":" or there is a "{" or "<" after ":", this field has // to be a message or the input is ill-formed. - if (tokenizer.tryConsume(":") - && !tokenizer.lookingAt("{") - && !tokenizer.lookingAt("<")) { + if (tokenizer.tryConsume(":") && !tokenizer.lookingAt("{") && !tokenizer.lookingAt("<")) { skipFieldValue(tokenizer); } else { skipFieldMessage(tokenizer); @@ -1588,25 +1705,42 @@ public final class TextFormat { // Handle potential ':'. if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { - tokenizer.tryConsume(":"); // optional + tokenizer.tryConsume(":"); // optional if (parseTreeBuilder != null) { TextFormatParseInfoTree.Builder childParseTreeBuilder = parseTreeBuilder.getBuilderForSubMessageField(field); - consumeFieldValues(tokenizer, extensionRegistry, target, field, extension, - childParseTreeBuilder, unknownFields); + consumeFieldValues( + tokenizer, + extensionRegistry, + target, + field, + extension, + childParseTreeBuilder, + unknownFields); } else { - consumeFieldValues(tokenizer, extensionRegistry, target, field, extension, - parseTreeBuilder, unknownFields); + consumeFieldValues( + tokenizer, + extensionRegistry, + target, + field, + extension, + parseTreeBuilder, + unknownFields); } } else { - tokenizer.consume(":"); // required - consumeFieldValues(tokenizer, extensionRegistry, target, field, - extension, parseTreeBuilder, unknownFields); + tokenizer.consume(":"); // required + consumeFieldValues( + tokenizer, + extensionRegistry, + target, + field, + extension, + parseTreeBuilder, + unknownFields); } if (parseTreeBuilder != null) { - parseTreeBuilder.setLocation( - field, TextFormatParseLocation.create(startLine, startColumn)); + parseTreeBuilder.setLocation(field, TextFormatParseLocation.create(startLine, startColumn)); } // For historical reasons, fields may optionally be separated by commas or @@ -1617,8 +1751,7 @@ public final class TextFormat { } /** - * Parse a one or more field values from {@code tokenizer} and merge it into - * {@code builder}. + * Parse a one or more field values from {@code tokenizer} and merge it into {@code builder}. */ private void consumeFieldValues( final Tokenizer tokenizer, @@ -1627,12 +1760,12 @@ public final class TextFormat { final FieldDescriptor field, final ExtensionRegistry.ExtensionInfo extension, final TextFormatParseInfoTree.Builder parseTreeBuilder, - List unknownFields) + List unknownFields) throws ParseException { // Support specifying repeated field values as a comma-separated list. // Ex."foo: [1, 2, 3]" if (field.isRepeated() && tokenizer.tryConsume("[")) { - if (!tokenizer.tryConsume("]")) { // Allow "foo: []" to be treated as empty. + if (!tokenizer.tryConsume("]")) { // Allow "foo: []" to be treated as empty. while (true) { consumeFieldValue( tokenizer, @@ -1650,15 +1783,18 @@ public final class TextFormat { } } } else { - consumeFieldValue(tokenizer, extensionRegistry, target, field, - extension, parseTreeBuilder, unknownFields); + consumeFieldValue( + tokenizer, + extensionRegistry, + target, + field, + extension, + parseTreeBuilder, + unknownFields); } } - /** - * Parse a single field value from {@code tokenizer} and merge it into - * {@code builder}. - */ + /** Parse a single field value from {@code tokenizer} and merge it into {@code builder}. */ private void consumeFieldValue( final Tokenizer tokenizer, final ExtensionRegistry extensionRegistry, @@ -1666,8 +1802,27 @@ public final class TextFormat { final FieldDescriptor field, final ExtensionRegistry.ExtensionInfo extension, final TextFormatParseInfoTree.Builder parseTreeBuilder, - List unknownFields) + List unknownFields) throws ParseException { + if (singularOverwritePolicy == SingularOverwritePolicy.FORBID_SINGULAR_OVERWRITES + && !field.isRepeated()) { + if (target.hasField(field)) { + throw tokenizer.parseExceptionPreviousToken( + "Non-repeated field \"" + field.getFullName() + "\" cannot be overwritten."); + } else if (field.getContainingOneof() != null + && target.hasOneof(field.getContainingOneof())) { + Descriptors.OneofDescriptor oneof = field.getContainingOneof(); + throw tokenizer.parseExceptionPreviousToken( + "Field \"" + + field.getFullName() + + "\" is specified along with field \"" + + target.getOneofFieldDescriptor(oneof).getFullName() + + "\", another member of oneof \"" + + oneof.getName() + + "\"."); + } + } + Object value = null; if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { @@ -1679,21 +1834,18 @@ public final class TextFormat { endToken = "}"; } - final MessageReflection.MergeTarget subField; - subField = target.newMergeTargetForField(field, - (extension == null) ? null : extension.defaultInstance); + Message defaultInstance = (extension == null) ? null : extension.defaultInstance; + MessageReflection.MergeTarget subField = + target.newMergeTargetForField(field, defaultInstance); - while (!tokenizer.tryConsume(endToken)) { - if (tokenizer.atEnd()) { - throw tokenizer.parseException( - "Expected \"" + endToken + "\"."); + while (!tokenizer.tryConsume(endToken)) { + if (tokenizer.atEnd()) { + throw tokenizer.parseException("Expected \"" + endToken + "\"."); + } + mergeField(tokenizer, extensionRegistry, subField, parseTreeBuilder, unknownFields); } - mergeField(tokenizer, extensionRegistry, subField, parseTreeBuilder, - unknownFields); - } - - value = subField.finish(); + value = subField.finish(); } else { switch (field.getType()) { case INT32: @@ -1794,28 +1946,13 @@ public final class TextFormat { // TODO(b/29122459): If field.isMapField() and FORBID_SINGULAR_OVERWRITES mode, // check for duplicate map keys here. target.addRepeatedField(field, value); - } else if ((singularOverwritePolicy - == SingularOverwritePolicy.FORBID_SINGULAR_OVERWRITES) - && target.hasField(field)) { - throw tokenizer.parseExceptionPreviousToken("Non-repeated field \"" - + field.getFullName() + "\" cannot be overwritten."); - } else if ((singularOverwritePolicy - == SingularOverwritePolicy.FORBID_SINGULAR_OVERWRITES) - && field.getContainingOneof() != null - && target.hasOneof(field.getContainingOneof())) { - Descriptors.OneofDescriptor oneof = field.getContainingOneof(); - throw tokenizer.parseExceptionPreviousToken("Field \"" - + field.getFullName() + "\" is specified along with field \"" - + target.getOneofFieldDescriptor(oneof).getFullName() - + "\", another member of oneof \"" + oneof.getName() + "\"."); } else { target.setField(field, value); } } - /** - * Skips the next field including the field's name and value. - */ + + /** Skips the next field including the field's name and value. */ private void skipField(Tokenizer tokenizer) throws ParseException { if (tokenizer.tryConsume("[")) { // Extension name. @@ -1833,9 +1970,7 @@ public final class TextFormat { // start with "{" or "<" which indicates the beginning of a message body. // If there is no ":" or there is a "{" or "<" after ":", this field has // to be a message or the input is ill-formed. - if (tokenizer.tryConsume(":") - && !tokenizer.lookingAt("<") - && !tokenizer.lookingAt("{")) { + if (tokenizer.tryConsume(":") && !tokenizer.lookingAt("<") && !tokenizer.lookingAt("{")) { skipFieldValue(tokenizer); } else { skipFieldMessage(tokenizer); @@ -1848,8 +1983,7 @@ public final class TextFormat { } /** - * Skips the whole body of a message including the beginning delimiter and - * the ending delimiter. + * Skips the whole body of a message including the beginning delimiter and the ending delimiter. */ private void skipFieldMessage(Tokenizer tokenizer) throws ParseException { final String delimiter; @@ -1865,21 +1999,18 @@ public final class TextFormat { tokenizer.consume(delimiter); } - /** - * Skips a field value. - */ + /** Skips a field value. */ private void skipFieldValue(Tokenizer tokenizer) throws ParseException { if (tokenizer.tryConsumeString()) { while (tokenizer.tryConsumeString()) {} return; } - if (!tokenizer.tryConsumeIdentifier() // includes enum & boolean - && !tokenizer.tryConsumeInt64() // includes int32 - && !tokenizer.tryConsumeUInt64() // includes uint32 + if (!tokenizer.tryConsumeIdentifier() // includes enum & boolean + && !tokenizer.tryConsumeInt64() // includes int32 + && !tokenizer.tryConsumeUInt64() // includes uint32 && !tokenizer.tryConsumeDouble() && !tokenizer.tryConsumeFloat()) { - throw tokenizer.parseException( - "Invalid field value: " + tokenizer.currentToken); + throw tokenizer.parseException("Invalid field value: " + tokenizer.currentToken); } } } @@ -1891,28 +2022,23 @@ public final class TextFormat { // them. /** - * Escapes bytes in the format used in protocol buffer text format, which - * is the same as the format used for C string literals. All bytes - * that are not printable 7-bit ASCII characters are escaped, as well as - * backslash, single-quote, and double-quote characters. Characters for - * which no defined short-hand escape sequence is defined will be escaped - * using 3-digit octal sequences. + * Escapes bytes in the format used in protocol buffer text format, which is the same as the + * format used for C string literals. All bytes that are not printable 7-bit ASCII characters are + * escaped, as well as backslash, single-quote, and double-quote characters. Characters for which + * no defined short-hand escape sequence is defined will be escaped using 3-digit octal sequences. */ public static String escapeBytes(ByteString input) { return TextFormatEscaper.escapeBytes(input); } - /** - * Like {@link #escapeBytes(ByteString)}, but used for byte array. - */ + /** Like {@link #escapeBytes(ByteString)}, but used for byte array. */ public static String escapeBytes(byte[] input) { return TextFormatEscaper.escapeBytes(input); } /** - * Un-escape a byte sequence as escaped using - * {@link #escapeBytes(ByteString)}. Two-digit hex escapes (starting with - * "\x") are also recognized. + * Un-escape a byte sequence as escaped using {@link #escapeBytes(ByteString)}. Two-digit hex + * escapes (starting with "\x") are also recognized. */ public static ByteString unescapeBytes(final CharSequence charString) throws InvalidEscapeSequenceException { @@ -1948,16 +2074,36 @@ public final class TextFormat { result[pos++] = (byte) code; } else { switch (c) { - case 'a' : result[pos++] = 0x07; break; - case 'b' : result[pos++] = '\b'; break; - case 'f' : result[pos++] = '\f'; break; - case 'n' : result[pos++] = '\n'; break; - case 'r' : result[pos++] = '\r'; break; - case 't' : result[pos++] = '\t'; break; - case 'v' : result[pos++] = 0x0b; break; - case '\\': result[pos++] = '\\'; break; - case '\'': result[pos++] = '\''; break; - case '"' : result[pos++] = '\"'; break; + case 'a': + result[pos++] = 0x07; + break; + case 'b': + result[pos++] = '\b'; + break; + case 'f': + result[pos++] = '\f'; + break; + case 'n': + result[pos++] = '\n'; + break; + case 'r': + result[pos++] = '\r'; + break; + case 't': + result[pos++] = '\t'; + break; + case 'v': + result[pos++] = 0x0b; + break; + case '\\': + result[pos++] = '\\'; + break; + case '\'': + result[pos++] = '\''; + break; + case '"': + result[pos++] = '\"'; + break; case 'x': // hex escape @@ -1991,13 +2137,13 @@ public final class TextFormat { } return result.length == pos - ? ByteString.wrap(result) // This reference has not been out of our control. + ? ByteString.wrap(result) // This reference has not been out of our control. : ByteString.copyFrom(result, 0, pos); } /** - * Thrown by {@link TextFormat#unescapeBytes} and - * {@link TextFormat#unescapeText} when an invalid escape sequence is seen. + * Thrown by {@link TextFormat#unescapeBytes} and {@link TextFormat#unescapeText} when an invalid + * escape sequence is seen. */ public static class InvalidEscapeSequenceException extends IOException { private static final long serialVersionUID = -8164033650142593304L; @@ -2008,27 +2154,24 @@ public final class TextFormat { } /** - * Like {@link #escapeBytes(ByteString)}, but escapes a text string. - * Non-ASCII characters are first encoded as UTF-8, then each byte is escaped - * individually as a 3-digit octal escape. Yes, it's weird. + * Like {@link #escapeBytes(ByteString)}, but escapes a text string. Non-ASCII characters are + * first encoded as UTF-8, then each byte is escaped individually as a 3-digit octal escape. Yes, + * it's weird. */ static String escapeText(final String input) { return escapeBytes(ByteString.copyFromUtf8(input)); } - /** - * Escape double quotes and backslashes in a String for unicode output of a message. - */ + /** Escape double quotes and backslashes in a String for emittingUnicode output of a message. */ public static String escapeDoubleQuotesAndBackslashes(final String input) { return TextFormatEscaper.escapeDoubleQuotesAndBackslashes(input); } /** - * Un-escape a text string as escaped using {@link #escapeText(String)}. - * Two-digit hex escapes (starting with "\x") are also recognized. + * Un-escape a text string as escaped using {@link #escapeText(String)}. Two-digit hex escapes + * (starting with "\x") are also recognized. */ - static String unescapeText(final String input) - throws InvalidEscapeSequenceException { + static String unescapeText(final String input) throws InvalidEscapeSequenceException { return unescapeBytes(input).toStringUtf8(); } @@ -2039,15 +2182,12 @@ public final class TextFormat { /** Is this a hex digit? */ private static boolean isHex(final byte c) { - return ('0' <= c && c <= '9') - || ('a' <= c && c <= 'f') - || ('A' <= c && c <= 'F'); + return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'); } /** - * Interpret a character as a digit (in any base up to 36) and return the - * numeric value. This is like {@code Character.digit()} but we don't accept - * non-ASCII digits. + * Interpret a character as a digit (in any base up to 36) and return the numeric value. This is + * like {@code Character.digit()} but we don't accept non-ASCII digits. */ private static int digitValue(final byte c) { if ('0' <= c && c <= '9') { @@ -2060,49 +2200,45 @@ public final class TextFormat { } /** - * Parse a 32-bit signed integer from the text. Unlike the Java standard - * {@code Integer.parseInt()}, this function recognizes the prefixes "0x" - * and "0" to signify hexadecimal and octal numbers, respectively. + * Parse a 32-bit signed integer from the text. Unlike the Java standard {@code + * Integer.parseInt()}, this function recognizes the prefixes "0x" and "0" to signify hexadecimal + * and octal numbers, respectively. */ static int parseInt32(final String text) throws NumberFormatException { return (int) parseInteger(text, true, false); } /** - * Parse a 32-bit unsigned integer from the text. Unlike the Java standard - * {@code Integer.parseInt()}, this function recognizes the prefixes "0x" - * and "0" to signify hexadecimal and octal numbers, respectively. The - * result is coerced to a (signed) {@code int} when returned since Java has - * no unsigned integer type. + * Parse a 32-bit unsigned integer from the text. Unlike the Java standard {@code + * Integer.parseInt()}, this function recognizes the prefixes "0x" and "0" to signify hexadecimal + * and octal numbers, respectively. The result is coerced to a (signed) {@code int} when returned + * since Java has no unsigned integer type. */ static int parseUInt32(final String text) throws NumberFormatException { return (int) parseInteger(text, false, false); } /** - * Parse a 64-bit signed integer from the text. Unlike the Java standard - * {@code Integer.parseInt()}, this function recognizes the prefixes "0x" - * and "0" to signify hexadecimal and octal numbers, respectively. + * Parse a 64-bit signed integer from the text. Unlike the Java standard {@code + * Integer.parseInt()}, this function recognizes the prefixes "0x" and "0" to signify hexadecimal + * and octal numbers, respectively. */ static long parseInt64(final String text) throws NumberFormatException { return parseInteger(text, true, true); } /** - * Parse a 64-bit unsigned integer from the text. Unlike the Java standard - * {@code Integer.parseInt()}, this function recognizes the prefixes "0x" - * and "0" to signify hexadecimal and octal numbers, respectively. The - * result is coerced to a (signed) {@code long} when returned since Java has - * no unsigned long type. + * Parse a 64-bit unsigned integer from the text. Unlike the Java standard {@code + * Integer.parseInt()}, this function recognizes the prefixes "0x" and "0" to signify hexadecimal + * and octal numbers, respectively. The result is coerced to a (signed) {@code long} when returned + * since Java has no unsigned long type. */ static long parseUInt64(final String text) throws NumberFormatException { return parseInteger(text, false, true); } - private static long parseInteger(final String text, - final boolean isSigned, - final boolean isLong) - throws NumberFormatException { + private static long parseInteger(final String text, final boolean isSigned, final boolean isLong) + throws NumberFormatException { int pos = 0; boolean negative = false; @@ -2139,12 +2275,12 @@ public final class TextFormat { if (isSigned) { if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) { throw new NumberFormatException( - "Number out of range for 32-bit signed integer: " + text); + "Number out of range for 32-bit signed integer: " + text); } } else { if (result >= (1L << 32) || result < 0) { throw new NumberFormatException( - "Number out of range for 32-bit unsigned integer: " + text); + "Number out of range for 32-bit unsigned integer: " + text); } } } @@ -2159,24 +2295,24 @@ public final class TextFormat { if (isSigned) { if (bigValue.bitLength() > 31) { throw new NumberFormatException( - "Number out of range for 32-bit signed integer: " + text); + "Number out of range for 32-bit signed integer: " + text); } } else { if (bigValue.bitLength() > 32) { throw new NumberFormatException( - "Number out of range for 32-bit unsigned integer: " + text); + "Number out of range for 32-bit unsigned integer: " + text); } } } else { if (isSigned) { if (bigValue.bitLength() > 63) { throw new NumberFormatException( - "Number out of range for 64-bit signed integer: " + text); + "Number out of range for 64-bit signed integer: " + text); } } else { if (bigValue.bitLength() > 64) { throw new NumberFormatException( - "Number out of range for 64-bit unsigned integer: " + text); + "Number out of range for 64-bit unsigned integer: " + text); } } } diff --git a/java/core/src/main/java/com/google/protobuf/TextFormatEscaper.java b/java/core/src/main/java/com/google/protobuf/TextFormatEscaper.java index da9ceadd16..2501ec936c 100644 --- a/java/core/src/main/java/com/google/protobuf/TextFormatEscaper.java +++ b/java/core/src/main/java/com/google/protobuf/TextFormatEscaper.java @@ -30,41 +30,58 @@ package com.google.protobuf; -/** - * Provide text format escaping support for proto2 instances. - */ +/** Provide text format escaping support for proto2 instances. */ final class TextFormatEscaper { private TextFormatEscaper() {} private interface ByteSequence { int size(); + byte byteAt(int offset); } /** - * Escapes bytes in the format used in protocol buffer text format, which - * is the same as the format used for C string literals. All bytes - * that are not printable 7-bit ASCII characters are escaped, as well as - * backslash, single-quote, and double-quote characters. Characters for - * which no defined short-hand escape sequence is defined will be escaped - * using 3-digit octal sequences. + * Escapes bytes in the format used in protocol buffer text format, which is the same as the + * format used for C string literals. All bytes that are not printable 7-bit ASCII characters are + * escaped, as well as backslash, single-quote, and double-quote characters. Characters for which + * no defined short-hand escape sequence is defined will be escaped using 3-digit octal sequences. */ static String escapeBytes(final ByteSequence input) { final StringBuilder builder = new StringBuilder(input.size()); for (int i = 0; i < input.size(); i++) { final byte b = input.byteAt(i); switch (b) { - // Java does not recognize \a or \v, apparently. - case 0x07: builder.append("\\a"); break; - case '\b': builder.append("\\b"); break; - case '\f': builder.append("\\f"); break; - case '\n': builder.append("\\n"); break; - case '\r': builder.append("\\r"); break; - case '\t': builder.append("\\t"); break; - case 0x0b: builder.append("\\v"); break; - case '\\': builder.append("\\\\"); break; - case '\'': builder.append("\\\'"); break; - case '"' : builder.append("\\\""); break; + // Java does not recognize \a or \v, apparently. + case 0x07: + builder.append("\\a"); + break; + case '\b': + builder.append("\\b"); + break; + case '\f': + builder.append("\\f"); + break; + case '\n': + builder.append("\\n"); + break; + case '\r': + builder.append("\\r"); + break; + case '\t': + builder.append("\\t"); + break; + case 0x0b: + builder.append("\\v"); + break; + case '\\': + builder.append("\\\\"); + break; + case '\'': + builder.append("\\\'"); + break; + case '"': + builder.append("\\\""); + break; default: // Only ASCII characters between 0x20 (space) and 0x7e (tilde) are // printable. Other byte values must be escaped. @@ -83,54 +100,52 @@ final class TextFormatEscaper { } /** - * Escapes bytes in the format used in protocol buffer text format, which - * is the same as the format used for C string literals. All bytes - * that are not printable 7-bit ASCII characters are escaped, as well as - * backslash, single-quote, and double-quote characters. Characters for - * which no defined short-hand escape sequence is defined will be escaped - * using 3-digit octal sequences. + * Escapes bytes in the format used in protocol buffer text format, which is the same as the + * format used for C string literals. All bytes that are not printable 7-bit ASCII characters are + * escaped, as well as backslash, single-quote, and double-quote characters. Characters for which + * no defined short-hand escape sequence is defined will be escaped using 3-digit octal sequences. */ static String escapeBytes(final ByteString input) { - return escapeBytes(new ByteSequence() { - @Override - public int size() { - return input.size(); - } - @Override - public byte byteAt(int offset) { - return input.byteAt(offset); - } - }); + return escapeBytes( + new ByteSequence() { + @Override + public int size() { + return input.size(); + } + + @Override + public byte byteAt(int offset) { + return input.byteAt(offset); + } + }); } - /** - * Like {@link #escapeBytes(ByteString)}, but used for byte array. - */ + /** Like {@link #escapeBytes(ByteString)}, but used for byte array. */ static String escapeBytes(final byte[] input) { - return escapeBytes(new ByteSequence() { - @Override - public int size() { - return input.length; - } - @Override - public byte byteAt(int offset) { - return input[offset]; - } - }); + return escapeBytes( + new ByteSequence() { + @Override + public int size() { + return input.length; + } + + @Override + public byte byteAt(int offset) { + return input[offset]; + } + }); } /** - * Like {@link #escapeBytes(ByteString)}, but escapes a text string. - * Non-ASCII characters are first encoded as UTF-8, then each byte is escaped - * individually as a 3-digit octal escape. Yes, it's weird. + * Like {@link #escapeBytes(ByteString)}, but escapes a text string. Non-ASCII characters are + * first encoded as UTF-8, then each byte is escaped individually as a 3-digit octal escape. Yes, + * it's weird. */ static String escapeText(final String input) { return escapeBytes(ByteString.copyFromUtf8(input)); } - /** - * Escape double quotes and backslashes in a String for unicode output of a message. - */ + /** Escape double quotes and backslashes in a String for unicode output of a message. */ static String escapeDoubleQuotesAndBackslashes(final String input) { return input.replace("\\", "\\\\").replace("\"", "\\\""); } diff --git a/java/core/src/main/java/com/google/protobuf/TextFormatParseInfoTree.java b/java/core/src/main/java/com/google/protobuf/TextFormatParseInfoTree.java index 0127ce92a3..6ce9f783ac 100644 --- a/java/core/src/main/java/com/google/protobuf/TextFormatParseInfoTree.java +++ b/java/core/src/main/java/com/google/protobuf/TextFormatParseInfoTree.java @@ -31,7 +31,6 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.FieldDescriptor; - import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -39,14 +38,12 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; - /** * Data structure which is populated with the locations of each field value parsed from the text. * - *

The locations of primary fields values are retrieved by {@code getLocation} or - * {@code getLocations}. The locations of sub message values are within nested - * {@code TextFormatParseInfoTree}s and are retrieve by {@code getNestedTree} or - * {@code getNestedTrees}. + *

The locations of primary fields values are retrieved by {@code getLocation} or {@code + * getLocations}. The locations of sub message values are within nested {@code + * TextFormatParseInfoTree}s and are retrieve by {@code getNestedTree} or {@code getNestedTrees}. * *

The {@code TextFormatParseInfoTree} is created by a Builder. */ @@ -90,13 +87,13 @@ public class TextFormatParseInfoTree { this.subtreesFromField = Collections.unmodifiableMap(subs); } - /** - * Retrieve all the locations of a field. - * - * @param fieldDescriptor the the @{link FieldDescriptor} of the desired field - * @return a list of the locations of values of the field. If there are not values - * or the field doesn't exist, an empty list is returned. - */ + /** + * Retrieve all the locations of a field. + * + * @param fieldDescriptor the @{link FieldDescriptor} of the desired field + * @return a list of the locations of values of the field. If there are not values or the field + * doesn't exist, an empty list is returned. + */ public List getLocations(final FieldDescriptor fieldDescriptor) { List result = locationsFromField.get(fieldDescriptor); return (result == null) ? Collections.emptyList() : result; @@ -134,7 +131,7 @@ public class TextFormatParseInfoTree { * @param fieldDescriptor the @{link FieldDescriptor} of the desired sub message * @param index the index of message value. * @return the {@code ParseInfoTree} of the message value. {@code null} is returned if the field - * doesn't exist or the index is out of range. + * doesn't exist or the index is out of range. * @throws IllegalArgumentException if index is out of range */ public TextFormatParseInfoTree getNestedTree(final FieldDescriptor fieldDescriptor, int index) { @@ -151,16 +148,16 @@ public class TextFormatParseInfoTree { } private static T getFromList(List list, int index, FieldDescriptor fieldDescriptor) { - if (index >= list.size() || index < 0) { - throw new IllegalArgumentException(String.format("Illegal index field: %s, index %d", - fieldDescriptor == null ? "" : fieldDescriptor.getName(), index)); + if (index >= list.size() || index < 0) { + throw new IllegalArgumentException( + String.format( + "Illegal index field: %s, index %d", + fieldDescriptor == null ? "" : fieldDescriptor.getName(), index)); } return list.get(index); } - /** - * Builder for a {@link TextFormatParseInfoTree}. - */ + /** Builder for a {@link TextFormatParseInfoTree}. */ public static class Builder { private Map> locationsFromField; @@ -169,9 +166,7 @@ public class TextFormatParseInfoTree { // sub message location information. private Map> subtreeBuildersFromField; - /** - * Create a root level {@ParseInfoTree} builder. - */ + /** Create a root level {@ParseInfoTree} builder. */ private Builder() { locationsFromField = new HashMap>(); subtreeBuildersFromField = new HashMap>(); diff --git a/java/core/src/main/java/com/google/protobuf/TextFormatParseLocation.java b/java/core/src/main/java/com/google/protobuf/TextFormatParseLocation.java index cce286e10f..eebfb1b545 100644 --- a/java/core/src/main/java/com/google/protobuf/TextFormatParseLocation.java +++ b/java/core/src/main/java/com/google/protobuf/TextFormatParseLocation.java @@ -39,9 +39,7 @@ import java.util.Arrays; */ public final class TextFormatParseLocation { - /** - * The empty location. - */ + /** The empty location. */ public static final TextFormatParseLocation EMPTY = new TextFormatParseLocation(-1, -1); /** @@ -92,8 +90,7 @@ public final class TextFormatParseLocation { return false; } TextFormatParseLocation that = (TextFormatParseLocation) o; - return (this.line == that.getLine()) - && (this.column == that.getColumn()); + return (this.line == that.getLine()) && (this.column == that.getColumn()); } @Override diff --git a/java/core/src/main/java/com/google/protobuf/UninitializedMessageException.java b/java/core/src/main/java/com/google/protobuf/UninitializedMessageException.java index 5714c063a9..c633a4e890 100644 --- a/java/core/src/main/java/com/google/protobuf/UninitializedMessageException.java +++ b/java/core/src/main/java/com/google/protobuf/UninitializedMessageException.java @@ -34,15 +34,13 @@ import java.util.Collections; import java.util.List; /** - * Thrown when attempting to build a protocol message that is missing required - * fields. This is a {@code RuntimeException} because it normally represents - * a programming error: it happens when some code which constructs a message - * fails to set all the fields. {@code parseFrom()} methods do not - * throw this; they throw an {@link InvalidProtocolBufferException} if - * required fields are missing, because it is not a programming error to - * receive an incomplete message. In other words, - * {@code UninitializedMessageException} should never be thrown by correct - * code, but {@code InvalidProtocolBufferException} might be. + * Thrown when attempting to build a protocol message that is missing required fields. This is a + * {@code RuntimeException} because it normally represents a programming error: it happens when some + * code which constructs a message fails to set all the fields. {@code parseFrom()} methods do + * not throw this; they throw an {@link InvalidProtocolBufferException} if required fields are + * missing, because it is not a programming error to receive an incomplete message. In other words, + * {@code UninitializedMessageException} should never be thrown by correct code, but {@code + * InvalidProtocolBufferException} might be. * * @author kenton@google.com Kenton Varda */ @@ -50,8 +48,9 @@ public class UninitializedMessageException extends RuntimeException { private static final long serialVersionUID = -7466929953374883507L; public UninitializedMessageException(final MessageLite message) { - super("Message was missing required fields. (Lite runtime could not " + - "determine which fields were missing)."); + super( + "Message was missing required fields. (Lite runtime could not " + + "determine which fields were missing)."); missingFields = null; } @@ -63,19 +62,18 @@ public class UninitializedMessageException extends RuntimeException { private final List missingFields; /** - * Get a list of human-readable names of required fields missing from this - * message. Each name is a full path to a field, e.g. "foo.bar[5].baz". - * Returns null if the lite runtime was used, since it lacks the ability to - * find missing fields. + * Get a list of human-readable names of required fields missing from this message. Each name is a + * full path to a field, e.g. "foo.bar[5].baz". Returns null if the lite runtime was used, since + * it lacks the ability to find missing fields. */ public List getMissingFields() { return Collections.unmodifiableList(missingFields); } /** - * Converts this exception to an {@link InvalidProtocolBufferException}. - * When a parsed message is missing required fields, this should be thrown - * instead of {@code UninitializedMessageException}. + * Converts this exception to an {@link InvalidProtocolBufferException}. When a parsed message is + * missing required fields, this should be thrown instead of {@code + * UninitializedMessageException}. */ public InvalidProtocolBufferException asInvalidProtocolBufferException() { return new InvalidProtocolBufferException(getMessage()); @@ -83,8 +81,7 @@ public class UninitializedMessageException extends RuntimeException { /** Construct the description string for this exception. */ private static String buildDescription(final List missingFields) { - final StringBuilder description = - new StringBuilder("Message missing required fields: "); + final StringBuilder description = new StringBuilder("Message missing required fields: "); boolean first = true; for (final String field : missingFields) { if (first) { diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java new file mode 100755 index 0000000000..e736d5ce9c --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java @@ -0,0 +1,133 @@ +// 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. + +package com.google.protobuf; + +import java.io.IOException; + +@ExperimentalApi +abstract class UnknownFieldSchema { + + /** Whether unknown fields should be dropped. */ + abstract boolean shouldDiscardUnknownFields(Reader reader); + + /** Adds a varint value to the unknown fields. */ + abstract void addVarint(B fields, int number, long value); + + /** Adds a fixed32 value to the unknown fields. */ + abstract void addFixed32(B fields, int number, int value); + + /** Adds a fixed64 value to the unknown fields. */ + abstract void addFixed64(B fields, int number, long value); + + /** Adds a length delimited value to the unknown fields. */ + abstract void addLengthDelimited(B fields, int number, ByteString value); + + /** Adds a group value to the unknown fields. */ + abstract void addGroup(B fields, int number, T subFieldSet); + + /** Create a new builder for unknown fields. */ + abstract B newBuilder(); + + /** Returns an immutable instance of the field container. */ + abstract T toImmutable(B fields); + + /** + * Sets the unknown fields into the message. Caller must take care of the mutability of the + * fields. + */ + abstract void setToMessage(Object message, T fields); + + /** Get the unknown fields from the message. */ + abstract T getFromMessage(Object message); + + /** Returns a builder for unknown fields in the message. */ + abstract B getBuilderFromMessage(Object message); + + /** Sets an unknown field builder into the message. */ + abstract void setBuilderToMessage(Object message, B builder); + + /** Marks unknown fields as immutable. */ + abstract void makeImmutable(Object message); + + /** Merges one field into the unknown fields. */ + final boolean mergeOneFieldFrom(B unknownFields, Reader reader) throws IOException { + int tag = reader.getTag(); + int fieldNumber = WireFormat.getTagFieldNumber(tag); + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + addVarint(unknownFields, fieldNumber, reader.readInt64()); + return true; + case WireFormat.WIRETYPE_FIXED32: + addFixed32(unknownFields, fieldNumber, reader.readFixed32()); + return true; + case WireFormat.WIRETYPE_FIXED64: + addFixed64(unknownFields, fieldNumber, reader.readFixed64()); + return true; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + addLengthDelimited(unknownFields, fieldNumber, reader.readBytes()); + return true; + case WireFormat.WIRETYPE_START_GROUP: + final B subFields = newBuilder(); + int endGroupTag = WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); + mergeFrom(subFields, reader); + if (endGroupTag != reader.getTag()) { + throw InvalidProtocolBufferException.invalidEndTag(); + } + addGroup(unknownFields, fieldNumber, toImmutable(subFields)); + return true; + case WireFormat.WIRETYPE_END_GROUP: + return false; + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + + final void mergeFrom(B unknownFields, Reader reader) throws IOException { + while (true) { + if (reader.getFieldNumber() == Reader.READ_DONE + || !mergeOneFieldFrom(unknownFields, reader)) { + break; + } + } + } + + abstract void writeTo(T unknownFields, Writer writer) throws IOException; + + abstract void writeAsMessageSetTo(T unknownFields, Writer writer) throws IOException; + + /** Merges {@code source} into {@code destination} and returns the merged instance. */ + abstract T merge(T destination, T source); + + /** Get the serialized size for message set serialization. */ + abstract int getSerializedSizeAsMessageSet(T message); + + abstract int getSerializedSize(T unknowns); +} diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java index b3fdebcbb9..ba2f9df08e 100644 --- a/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java @@ -43,14 +43,13 @@ import java.util.Map; import java.util.TreeMap; /** - * {@code UnknownFieldSet} is used to keep track of fields which were seen when - * parsing a protocol message but whose field numbers or types are unrecognized. - * This most frequently occurs when new fields are added to a message type - * and then messages containing those fields are read by old software that was - * compiled before the new types were added. + * {@code UnknownFieldSet} is used to keep track of fields which were seen when parsing a protocol + * message but whose field numbers or types are unrecognized. This most frequently occurs when new + * fields are added to a message type and then messages containing those fields are read by old + * software that was compiled before the new types were added. * - *

Every {@link Message} contains an {@code UnknownFieldSet} (and every - * {@link Message.Builder} contains an {@link Builder}). + *

Every {@link Message} contains an {@code UnknownFieldSet} (and every {@link Message.Builder} + * contains an {@link Builder}). * *

Most users will never need to use this class. * @@ -60,6 +59,7 @@ public final class UnknownFieldSet implements MessageLite { private UnknownFieldSet() { fields = null; + fieldsDescending = null; } /** Create a new {@link Builder}. */ @@ -67,10 +67,7 @@ public final class UnknownFieldSet implements MessageLite { return Builder.create(); } - /** - * Create a new {@link Builder} and initialize it to be a copy - * of {@code copyFrom}. - */ + /** Create a new {@link Builder} and initialize it to be a copy of {@code copyFrom}. */ public static Builder newBuilder(final UnknownFieldSet copyFrom) { return newBuilder().mergeFrom(copyFrom); } @@ -79,32 +76,36 @@ public final class UnknownFieldSet implements MessageLite { public static UnknownFieldSet getDefaultInstance() { return defaultInstance; } + @Override public UnknownFieldSet getDefaultInstanceForType() { return defaultInstance; } + private static final UnknownFieldSet defaultInstance = - new UnknownFieldSet(Collections.emptyMap(), - Collections.emptyMap()); + new UnknownFieldSet( + Collections.emptyMap(), Collections.emptyMap()); /** - * Construct an {@code UnknownFieldSet} around the given map. The map is - * expected to be immutable. + * Construct an {@code UnknownFieldSet} around the given map. The map is expected to be immutable. */ - UnknownFieldSet(final Map fields, - final Map fieldsDescending) { + UnknownFieldSet(final Map fields, final Map fieldsDescending) { this.fields = fields; + this.fieldsDescending = fieldsDescending; } + private final Map fields; + /** A copy of {@link #fields} who's iterator order is reversed. */ + private final Map fieldsDescending; + @Override public boolean equals(final Object other) { if (this == other) { return true; } - return (other instanceof UnknownFieldSet) && - fields.equals(((UnknownFieldSet) other).fields); + return (other instanceof UnknownFieldSet) && fields.equals(((UnknownFieldSet) other).fields); } @Override @@ -122,10 +123,7 @@ public final class UnknownFieldSet implements MessageLite { return fields.containsKey(number); } - /** - * Get a field by number. Returns an empty field if not present. Never - * returns {@code null}. - */ + /** Get a field by number. Returns an empty field if not present. Never returns {@code null}. */ public Field getField(final int number) { final Field result = fields.get(number); return (result == null) ? Field.getDefaultInstance() : result; @@ -141,36 +139,33 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Converts the set to a string in protocol buffer text format. This is - * just a trivial wrapper around - * {@link TextFormat#printToString(UnknownFieldSet)}. + * Converts the set to a string in protocol buffer text format. This is just a trivial wrapper + * around {@link TextFormat.Printer#printToString(UnknownFieldSet)}. */ @Override public String toString() { - return TextFormat.printToString(this); + return TextFormat.printer().printToString(this); } /** - * Serializes the message to a {@code ByteString} and returns it. This is - * just a trivial wrapper around {@link #writeTo(CodedOutputStream)}. + * Serializes the message to a {@code ByteString} and returns it. This is just a trivial wrapper + * around {@link #writeTo(CodedOutputStream)}. */ @Override public ByteString toByteString() { try { - final ByteString.CodedBuilder out = - ByteString.newCodedBuilder(getSerializedSize()); + final ByteString.CodedBuilder out = ByteString.newCodedBuilder(getSerializedSize()); writeTo(out.getCodedOutput()); return out.build(); } catch (final IOException e) { throw new RuntimeException( - "Serializing to a ByteString threw an IOException (should " + - "never happen).", e); + "Serializing to a ByteString threw an IOException (should never happen).", e); } } /** - * Serializes the message to a {@code byte} array and returns it. This is - * just a trivial wrapper around {@link #writeTo(CodedOutputStream)}. + * Serializes the message to a {@code byte} array and returns it. This is just a trivial wrapper + * around {@link #writeTo(CodedOutputStream)}. */ @Override public byte[] toByteArray() { @@ -182,14 +177,13 @@ public final class UnknownFieldSet implements MessageLite { return result; } catch (final IOException e) { throw new RuntimeException( - "Serializing to a byte array threw an IOException " + - "(should never happen).", e); + "Serializing to a byte array threw an IOException (should never happen).", e); } } /** - * Serializes the message and writes it to {@code output}. This is just a - * trivial wrapper around {@link #writeTo(CodedOutputStream)}. + * Serializes the message and writes it to {@code output}. This is just a trivial wrapper around + * {@link #writeTo(CodedOutputStream)}. */ @Override public void writeTo(final OutputStream output) throws IOException { @@ -216,25 +210,48 @@ public final class UnknownFieldSet implements MessageLite { return result; } - /** - * Serializes the set and writes it to {@code output} using - * {@code MessageSet} wire format. - */ - public void writeAsMessageSetTo(final CodedOutputStream output) - throws IOException { + /** Serializes the set and writes it to {@code output} using {@code MessageSet} wire format. */ + public void writeAsMessageSetTo(final CodedOutputStream output) throws IOException { for (final Map.Entry entry : fields.entrySet()) { - entry.getValue().writeAsMessageSetExtensionTo( - entry.getKey(), output); + entry.getValue().writeAsMessageSetExtensionTo(entry.getKey(), output); + } + } + + /** Serializes the set and writes it to {@code writer}. */ + void writeTo(Writer writer) throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + // Write fields in descending order. + for (Map.Entry entry : fieldsDescending.entrySet()) { + entry.getValue().writeTo(entry.getKey(), writer); + } + } else { + // Write fields in ascending order. + for (Map.Entry entry : fields.entrySet()) { + entry.getValue().writeTo(entry.getKey(), writer); + } } } + /** Serializes the set and writes it to {@code writer} using {@code MessageSet} wire format. */ + void writeAsMessageSetTo(final Writer writer) throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + // Write fields in descending order. + for (final Map.Entry entry : fieldsDescending.entrySet()) { + entry.getValue().writeAsMessageSetExtensionTo(entry.getKey(), writer); + } + } else { + // Write fields in ascending order. + for (final Map.Entry entry : fields.entrySet()) { + entry.getValue().writeAsMessageSetExtensionTo(entry.getKey(), writer); + } + } + } /** Get the number of bytes required to encode this set using {@code MessageSet} wire format. */ public int getSerializedSizeAsMessageSet() { int result = 0; for (final Map.Entry entry : fields.entrySet()) { - result += entry.getValue().getSerializedSizeAsMessageSetExtension( - entry.getKey()); + result += entry.getValue().getSerializedSizeAsMessageSetExtension(entry.getKey()); } return result; } @@ -247,8 +264,7 @@ public final class UnknownFieldSet implements MessageLite { } /** Parse an {@code UnknownFieldSet} from the given input stream. */ - public static UnknownFieldSet parseFrom(final CodedInputStream input) - throws IOException { + public static UnknownFieldSet parseFrom(final CodedInputStream input) throws IOException { return newBuilder().mergeFrom(input).build(); } @@ -259,14 +275,12 @@ public final class UnknownFieldSet implements MessageLite { } /** Parse {@code data} as an {@code UnknownFieldSet} and return it. */ - public static UnknownFieldSet parseFrom(final byte[] data) - throws InvalidProtocolBufferException { + public static UnknownFieldSet parseFrom(final byte[] data) throws InvalidProtocolBufferException { return newBuilder().mergeFrom(data).build(); } /** Parse an {@code UnknownFieldSet} from {@code input} and return it. */ - public static UnknownFieldSet parseFrom(final InputStream input) - throws IOException { + public static UnknownFieldSet parseFrom(final InputStream input) throws IOException { return newBuilder().mergeFrom(input).build(); } @@ -283,12 +297,11 @@ public final class UnknownFieldSet implements MessageLite { /** * Builder for {@link UnknownFieldSet}s. * - *

Note that this class maintains {@link Field.Builder}s for all fields - * in the set. Thus, adding one element to an existing {@link Field} does not - * require making a copy. This is important for efficient parsing of - * unknown repeated fields. However, it implies that {@link Field}s cannot - * be constructed independently, nor can two {@link UnknownFieldSet}s share - * the same {@code Field} object. + *

Note that this class maintains {@link Field.Builder}s for all fields in the set. Thus, + * adding one element to an existing {@link Field} does not require making a copy. This is + * important for efficient parsing of unknown repeated fields. However, it implies that {@link + * Field}s cannot be constructed independently, nor can two {@link UnknownFieldSet}s share the + * same {@code Field} object. * *

Use {@link UnknownFieldSet#newBuilder()} to construct a {@code Builder}. */ @@ -311,8 +324,7 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Get a field builder for the given field number which includes any - * values that already exist. + * Get a field builder for the given field number which includes any values that already exist. */ private Field.Builder getFieldBuilder(final int number) { if (lastField != null) { @@ -338,10 +350,9 @@ public final class UnknownFieldSet implements MessageLite { /** * Build the {@link UnknownFieldSet} and return it. * - *

Once {@code build()} has been called, the {@code Builder} will no - * longer be usable. Calling any method after {@code build()} will result - * in undefined behavior and can cause a {@code NullPointerException} to be - * thrown. + *

Once {@code build()} has been called, the {@code Builder} will no longer be usable. + * Calling any method after {@code build()} will result in undefined behavior and can cause a + * {@code NullPointerException} to be thrown. */ @Override public UnknownFieldSet build() { @@ -351,6 +362,8 @@ public final class UnknownFieldSet implements MessageLite { result = getDefaultInstance(); } else { Map descendingFields = null; + descendingFields = + Collections.unmodifiableMap(((TreeMap) fields).descendingMap()); result = new UnknownFieldSet(Collections.unmodifiableMap(fields), descendingFields); } fields = null; @@ -365,10 +378,11 @@ public final class UnknownFieldSet implements MessageLite { @Override public Builder clone() { - getFieldBuilder(0); // Force lastField to be built. + getFieldBuilder(0); // Force lastField to be built. Map descendingFields = null; - return UnknownFieldSet.newBuilder().mergeFrom( - new UnknownFieldSet(fields, descendingFields)); + descendingFields = + Collections.unmodifiableMap(((TreeMap) fields).descendingMap()); + return UnknownFieldSet.newBuilder().mergeFrom(new UnknownFieldSet(fields, descendingFields)); } @Override @@ -388,7 +402,7 @@ public final class UnknownFieldSet implements MessageLite { reinitialize(); return this; } - + /** Clear fields from the set with a given field number. */ public Builder clearField(final int number) { if (number == 0) { @@ -406,9 +420,8 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Merge the fields from {@code other} into this set. If a field number - * exists in both sets, {@code other}'s values for that field will be - * appended to the values in this set. + * Merge the fields from {@code other} into this set. If a field number exists in both sets, + * {@code other}'s values for that field will be appended to the values in this set. */ public Builder mergeFrom(final UnknownFieldSet other) { if (other != getDefaultInstance()) { @@ -420,8 +433,8 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Add a field to the {@code UnknownFieldSet}. If a field with the same - * number already exists, the two are merged. + * Add a field to the {@code UnknownFieldSet}. If a field with the same number already exists, + * the two are merged. */ public Builder mergeField(final int number, final Field field) { if (number == 0) { @@ -439,9 +452,8 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Convenience method for merging a new field containing a single varint - * value. This is used in particular when an unknown enum value is - * encountered. + * Convenience method for merging a new field containing a single varint value. This is used in + * particular when an unknown enum value is encountered. */ public Builder mergeVarintField(final int number, final int value) { if (number == 0) { @@ -451,14 +463,12 @@ public final class UnknownFieldSet implements MessageLite { return this; } - /** * Convenience method for merging a length-delimited field. * *

For use by generated code only. */ - public Builder mergeLengthDelimitedField( - final int number, final ByteString value) { + public Builder mergeLengthDelimitedField(final int number, final ByteString value) { if (number == 0) { throw new IllegalArgumentException("Zero is not a valid field number."); } @@ -475,8 +485,8 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Add a field to the {@code UnknownFieldSet}. If a field with the same - * number already exists, it is removed. + * Add a field to the {@code UnknownFieldSet}. If a field with the same number already exists, + * it is removed. */ public Builder addField(final int number, final Field field) { if (number == 0) { @@ -488,25 +498,22 @@ public final class UnknownFieldSet implements MessageLite { lastFieldNumber = 0; } if (fields.isEmpty()) { - fields = new TreeMap(); + fields = new TreeMap(); } fields.put(number, field); return this; } /** - * Get all present {@code Field}s as an immutable {@code Map}. If more - * fields are added, the changes may or may not be reflected in this map. + * Get all present {@code Field}s as an immutable {@code Map}. If more fields are added, the + * changes may or may not be reflected in this map. */ public Map asMap() { - getFieldBuilder(0); // Force lastField to be built. + getFieldBuilder(0); // Force lastField to be built. return Collections.unmodifiableMap(fields); } - /** - * Parse an entire message from {@code input} and merge its fields into - * this set. - */ + /** Parse an entire message from {@code input} and merge its fields into this set. */ @Override public Builder mergeFrom(final CodedInputStream input) throws IOException { while (true) { @@ -520,11 +527,11 @@ public final class UnknownFieldSet implements MessageLite { /** * Parse a single field from {@code input} and merge it into this set. + * * @param tag The field's tag number, which was already parsed. * @return {@code false} if the tag is an end group tag. */ - public boolean mergeFieldFrom(final int tag, final CodedInputStream input) - throws IOException { + public boolean mergeFieldFrom(final int tag, final CodedInputStream input) throws IOException { final int number = WireFormat.getTagFieldNumber(tag); switch (WireFormat.getTagWireType(tag)) { case WireFormat.WIRETYPE_VARINT: @@ -538,8 +545,7 @@ public final class UnknownFieldSet implements MessageLite { return true; case WireFormat.WIRETYPE_START_GROUP: final Builder subBuilder = newBuilder(); - input.readGroup(number, subBuilder, - ExtensionRegistry.getEmptyRegistry()); + input.readGroup(number, subBuilder, ExtensionRegistry.getEmptyRegistry()); getFieldBuilder(number).addGroup(subBuilder.build()); return true; case WireFormat.WIRETYPE_END_GROUP: @@ -553,9 +559,8 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Parse {@code data} as an {@code UnknownFieldSet} and merge it with the - * set being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream)}. + * Parse {@code data} as an {@code UnknownFieldSet} and merge it with the set being built. This + * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. */ @Override public Builder mergeFrom(final ByteString data) throws InvalidProtocolBufferException { @@ -568,15 +573,13 @@ public final class UnknownFieldSet implements MessageLite { throw e; } catch (final IOException e) { throw new RuntimeException( - "Reading from a ByteString threw an IOException (should " + - "never happen).", e); + "Reading from a ByteString threw an IOException (should never happen).", e); } } /** - * Parse {@code data} as an {@code UnknownFieldSet} and merge it with the - * set being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream)}. + * Parse {@code data} as an {@code UnknownFieldSet} and merge it with the set being built. This + * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. */ @Override public Builder mergeFrom(final byte[] data) throws InvalidProtocolBufferException { @@ -589,15 +592,13 @@ public final class UnknownFieldSet implements MessageLite { throw e; } catch (final IOException e) { throw new RuntimeException( - "Reading from a byte array threw an IOException (should " + - "never happen).", e); + "Reading from a byte array threw an IOException (should never happen).", e); } } /** - * Parse an {@code UnknownFieldSet} from {@code input} and merge it with the - * set being built. This is just a small wrapper around - * {@link #mergeFrom(CodedInputStream)}. + * Parse an {@code UnknownFieldSet} from {@code input} and merge it with the set being built. + * This is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. */ @Override public Builder mergeFrom(final InputStream input) throws IOException { @@ -643,8 +644,7 @@ public final class UnknownFieldSet implements MessageLite { @Override public Builder mergeFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException { try { - final CodedInputStream input = - CodedInputStream.newInstance(data, off, len); + final CodedInputStream input = CodedInputStream.newInstance(data, off, len); mergeFrom(input); input.checkLastTagWas(0); return this; @@ -652,8 +652,7 @@ public final class UnknownFieldSet implements MessageLite { throw e; } catch (IOException e) { throw new RuntimeException( - "Reading from a byte array threw an IOException (should " + - "never happen).", e); + "Reading from a byte array threw an IOException (should never happen).", e); } } @@ -698,19 +697,15 @@ public final class UnknownFieldSet implements MessageLite { /** * Represents a single field in an {@code UnknownFieldSet}. * - *

A {@code Field} consists of five lists of values. The lists correspond - * to the five "wire types" used in the protocol buffer binary format. - * The wire type of each field can be determined from the encoded form alone, - * without knowing the field's declared type. So, we are able to parse - * unknown values at least this far and separate them. Normally, only one - * of the five lists will contain any values, since it is impossible to - * define a valid message type that declares two different types for the - * same field number. However, the code is designed to allow for the case - * where the same unknown field number is encountered using multiple different - * wire types. + *

A {@code Field} consists of five lists of values. The lists correspond to the five "wire + * types" used in the protocol buffer binary format. The wire type of each field can be determined + * from the encoded form alone, without knowing the field's declared type. So, we are able to + * parse unknown values at least this far and separate them. Normally, only one of the five lists + * will contain any values, since it is impossible to define a valid message type that declares + * two different types for the same field number. However, the code is designed to allow for the + * case where the same unknown field number is encountered using multiple different wire types. * - *

{@code Field} is an immutable class. To construct one, you must use a - * {@link Builder}. + *

{@code Field} is an immutable class. To construct one, you must use a {@link Builder}. * * @see UnknownFieldSet */ @@ -722,10 +717,7 @@ public final class UnknownFieldSet implements MessageLite { return Builder.create(); } - /** - * Construct a new {@link Builder} and initialize it to a copy of - * {@code copyFrom}. - */ + /** Construct a new {@link Builder} and initialize it to a copy of {@code copyFrom}. */ public static Builder newBuilder(final Field copyFrom) { return newBuilder().mergeFrom(copyFrom); } @@ -734,26 +726,36 @@ public final class UnknownFieldSet implements MessageLite { public static Field getDefaultInstance() { return fieldDefaultInstance; } + private static final Field fieldDefaultInstance = newBuilder().build(); /** Get the list of varint values for this field. */ - public List getVarintList() { return varint; } + public List getVarintList() { + return varint; + } /** Get the list of fixed32 values for this field. */ - public List getFixed32List() { return fixed32; } + public List getFixed32List() { + return fixed32; + } /** Get the list of fixed64 values for this field. */ - public List getFixed64List() { return fixed64; } + public List getFixed64List() { + return fixed64; + } /** Get the list of length-delimited values for this field. */ - public List getLengthDelimitedList() { return lengthDelimited; } + public List getLengthDelimitedList() { + return lengthDelimited; + } /** - * Get the list of embedded group values for this field. These are - * represented using {@link UnknownFieldSet}s rather than {@link Message}s - * since the group's type is presumably unknown. + * Get the list of embedded group values for this field. These are represented using {@link + * UnknownFieldSet}s rather than {@link Message}s since the group's type is presumably unknown. */ - public List getGroupList() { return group; } + public List getGroupList() { + return group; + } @Override public boolean equals(final Object other) { @@ -763,8 +765,7 @@ public final class UnknownFieldSet implements MessageLite { if (!(other instanceof Field)) { return false; } - return Arrays.equals(getIdentityArray(), - ((Field) other).getIdentityArray()); + return Arrays.equals(getIdentityArray(), ((Field) other).getIdentityArray()); } @Override @@ -772,17 +773,9 @@ public final class UnknownFieldSet implements MessageLite { return Arrays.hashCode(getIdentityArray()); } - /** - * Returns the array of objects to be used to uniquely identify this - * {@link Field} instance. - */ + /** Returns the array of objects to be used to uniquely identify this {@link Field} instance. */ private Object[] getIdentityArray() { - return new Object[] { - varint, - fixed32, - fixed64, - lengthDelimited, - group}; + return new Object[] {varint, fixed32, fixed64, lengthDelimited, group}; } /** @@ -802,12 +795,8 @@ public final class UnknownFieldSet implements MessageLite { } } - /** - * Serializes the field, including field number, and writes it to - * {@code output}. - */ - public void writeTo(final int fieldNumber, final CodedOutputStream output) - throws IOException { + /** Serializes the field, including field number, and writes it to {@code output}. */ + public void writeTo(final int fieldNumber, final CodedOutputStream output) throws IOException { for (final long value : varint) { output.writeUInt64(fieldNumber, value); } @@ -825,10 +814,7 @@ public final class UnknownFieldSet implements MessageLite { } } - /** - * Get the number of bytes required to encode this field, including field - * number. - */ + /** Get the number of bytes required to encode this field, including field number. */ public int getSerializedSize(final int fieldNumber) { int result = 0; for (final long value : varint) { @@ -850,18 +836,57 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Serializes the field, including field number, and writes it to - * {@code output}, using {@code MessageSet} wire format. + * Serializes the field, including field number, and writes it to {@code output}, using {@code + * MessageSet} wire format. */ - public void writeAsMessageSetExtensionTo( - final int fieldNumber, - final CodedOutputStream output) + public void writeAsMessageSetExtensionTo(final int fieldNumber, final CodedOutputStream output) throws IOException { for (final ByteString value : lengthDelimited) { output.writeRawMessageSetExtension(fieldNumber, value); } } + /** Serializes the field, including field number, and writes it to {@code writer}. */ + void writeTo(final int fieldNumber, final Writer writer) throws IOException { + writer.writeInt64List(fieldNumber, varint, false); + writer.writeFixed32List(fieldNumber, fixed32, false); + writer.writeFixed64List(fieldNumber, fixed64, false); + writer.writeBytesList(fieldNumber, lengthDelimited); + + if (writer.fieldOrder() == Writer.FieldOrder.ASCENDING) { + for (int i = 0; i < group.size(); i++) { + writer.writeStartGroup(fieldNumber); + group.get(i).writeTo(writer); + writer.writeEndGroup(fieldNumber); + } + } else { + for (int i = group.size() - 1; i >= 0; i--) { + writer.writeEndGroup(fieldNumber); + group.get(i).writeTo(writer); + writer.writeStartGroup(fieldNumber); + } + } + } + + /** + * Serializes the field, including field number, and writes it to {@code writer}, using {@code + * MessageSet} wire format. + */ + private void writeAsMessageSetExtensionTo(final int fieldNumber, final Writer writer) + throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + // Write in descending field order. + ListIterator iter = lengthDelimited.listIterator(lengthDelimited.size()); + while (iter.hasPrevious()) { + writer.writeMessageSetItem(fieldNumber, iter.previous()); + } + } else { + // Write in ascending field order. + for (final ByteString value : lengthDelimited) { + writer.writeMessageSetItem(fieldNumber, value); + } + } + } /** * Get the number of bytes required to encode this field, including field number, using {@code @@ -870,8 +895,7 @@ public final class UnknownFieldSet implements MessageLite { public int getSerializedSizeAsMessageSetExtension(final int fieldNumber) { int result = 0; for (final ByteString value : lengthDelimited) { - result += CodedOutputStream.computeRawMessageSetExtensionSize( - fieldNumber, value); + result += CodedOutputStream.computeRawMessageSetExtensionSize(fieldNumber, value); } return result; } @@ -900,10 +924,9 @@ public final class UnknownFieldSet implements MessageLite { private Field result; /** - * Build the field. After {@code build()} has been called, the - * {@code Builder} is no longer usable. Calling any other method will - * result in undefined behavior and can cause a - * {@code NullPointerException} to be thrown. + * Build the field. After {@code build()} has been called, the {@code Builder} is no longer + * usable. Calling any other method will result in undefined behavior and can cause a {@code + * NullPointerException} to be thrown. */ public Field build() { if (result.varint == null) { @@ -924,8 +947,7 @@ public final class UnknownFieldSet implements MessageLite { if (result.lengthDelimited == null) { result.lengthDelimited = Collections.emptyList(); } else { - result.lengthDelimited = - Collections.unmodifiableList(result.lengthDelimited); + result.lengthDelimited = Collections.unmodifiableList(result.lengthDelimited); } if (result.group == null) { result.group = Collections.emptyList(); @@ -945,9 +967,8 @@ public final class UnknownFieldSet implements MessageLite { } /** - * Merge the values in {@code other} into this field. For each list - * of values, {@code other}'s values are append to the ones in this - * field. + * Merge the values in {@code other} into this field. For each list of values, {@code other}'s + * values are append to the ones in this field. */ public Builder mergeFrom(final Field other) { if (!other.varint.isEmpty()) { @@ -1030,9 +1051,7 @@ public final class UnknownFieldSet implements MessageLite { } } - /** - * Parser to implement MessageLite interface. - */ + /** Parser to implement MessageLite interface. */ public static final class Parser extends AbstractParser { @Override public UnknownFieldSet parsePartialFrom( @@ -1044,14 +1063,14 @@ public final class UnknownFieldSet implements MessageLite { } catch (InvalidProtocolBufferException e) { throw e.setUnfinishedMessage(builder.buildPartial()); } catch (IOException e) { - throw new InvalidProtocolBufferException(e) - .setUnfinishedMessage(builder.buildPartial()); + throw new InvalidProtocolBufferException(e).setUnfinishedMessage(builder.buildPartial()); } return builder.buildPartial(); } } private static final Parser PARSER = new Parser(); + @Override public final Parser getParserForType() { return PARSER; diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java index f0b919adae..2f6315c80d 100644 --- a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java @@ -34,24 +34,23 @@ import java.io.IOException; import java.util.Arrays; /** - * {@code UnknownFieldSetLite} is used to keep track of fields which were seen - * when parsing a protocol message but whose field numbers or types are - * unrecognized. This most frequently occurs when new fields are added to a - * message type and then messages containing those fields are read by old - * software that was compiled before the new types were added. + * {@code UnknownFieldSetLite} is used to keep track of fields which were seen when parsing a + * protocol message but whose field numbers or types are unrecognized. This most frequently occurs + * when new fields are added to a message type and then messages containing those fields are read by + * old software that was compiled before the new types were added. * *

For use by generated code only. * * @author dweis@google.com (Daniel Weis) */ public final class UnknownFieldSetLite { - + // Arbitrarily chosen. // TODO(dweis): Tune this number? private static final int MIN_CAPACITY = 8; private static final UnknownFieldSetLite DEFAULT_INSTANCE = - new UnknownFieldSetLite(0, new int[0], new Object[0], false /* isMutable */); + new UnknownFieldSetLite(0, new int[0], new Object[0], /* isMutable= */ false); /** * Get an empty {@code UnknownFieldSetLite}. @@ -61,17 +60,15 @@ public final class UnknownFieldSetLite { public static UnknownFieldSetLite getDefaultInstance() { return DEFAULT_INSTANCE; } - - /** - * Returns a new mutable instance. - */ + + /** Returns a new mutable instance. */ static UnknownFieldSetLite newInstance() { return new UnknownFieldSetLite(); } /** - * Returns a mutable {@code UnknownFieldSetLite} that is the composite of {@code first} and - * {@code second}. + * Returns a mutable {@code UnknownFieldSetLite} that is the composite of {@code first} and {@code + * second}. */ static UnknownFieldSetLite mutableCopyOf(UnknownFieldSetLite first, UnknownFieldSetLite second) { int count = first.count + second.count; @@ -79,66 +76,50 @@ public final class UnknownFieldSetLite { System.arraycopy(second.tags, 0, tags, first.count, second.count); Object[] objects = Arrays.copyOf(first.objects, count); System.arraycopy(second.objects, 0, objects, first.count, second.count); - return new UnknownFieldSetLite(count, tags, objects, true /* isMutable */); + return new UnknownFieldSetLite(count, tags, objects, /* isMutable= */ true); } - /** - * The number of elements in the set. - */ + /** The number of elements in the set. */ private int count; - - /** - * The tag numbers for the elements in the set. - */ + + /** The tag numbers for the elements in the set. */ private int[] tags; - - /** - * The boxed values of the elements in the set. - */ + + /** The boxed values of the elements in the set. */ private Object[] objects; - - /** - * The lazily computed serialized size of the set. - */ + + /** The lazily computed serialized size of the set. */ private int memoizedSerializedSize = -1; - - /** - * Indicates that this object is mutable. - */ + + /** Indicates that this object is mutable. */ private boolean isMutable; - /** - * Constructs a mutable {@code UnknownFieldSetLite}. - */ + /** Constructs a mutable {@code UnknownFieldSetLite}. */ private UnknownFieldSetLite() { - this(0, new int[MIN_CAPACITY], new Object[MIN_CAPACITY], true /* isMutable */); + this(0, new int[MIN_CAPACITY], new Object[MIN_CAPACITY], /* isMutable= */ true); } - - /** - * Constructs the {@code UnknownFieldSetLite}. - */ + + /** Constructs the {@code UnknownFieldSetLite}. */ private UnknownFieldSetLite(int count, int[] tags, Object[] objects, boolean isMutable) { this.count = count; this.tags = tags; this.objects = objects; this.isMutable = isMutable; } - + /** * Marks this object as immutable. - * + * *

Future calls to methods that attempt to modify this object will throw. */ public void makeImmutable() { this.isMutable = false; } - - /** - * Throws an {@link UnsupportedOperationException} if immutable. - */ + + /** Throws an {@link UnsupportedOperationException} if immutable. */ void checkMutable() { if (!isMutable) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(); } } @@ -187,6 +168,72 @@ public final class UnknownFieldSetLite { } } + /** Serializes the set and writes it to {@code writer} using {@code MessageSet} wire format. */ + void writeAsMessageSetTo(Writer writer) throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + // Write fields in descending order. + for (int i = count - 1; i >= 0; i--) { + int fieldNumber = WireFormat.getTagFieldNumber(tags[i]); + writer.writeMessageSetItem(fieldNumber, objects[i]); + } + } else { + // Write fields in ascending order. + for (int i = 0; i < count; i++) { + int fieldNumber = WireFormat.getTagFieldNumber(tags[i]); + writer.writeMessageSetItem(fieldNumber, objects[i]); + } + } + } + + /** Serializes the set and writes it to {@code writer}. */ + public void writeTo(Writer writer) throws IOException { + if (count == 0) { + return; + } + + // TODO: tags are not sorted, so there's no write order guarantees + if (writer.fieldOrder() == Writer.FieldOrder.ASCENDING) { + for (int i = 0; i < count; ++i) { + writeField(tags[i], objects[i], writer); + } + } else { + for (int i = count - 1; i >= 0; --i) { + writeField(tags[i], objects[i], writer); + } + } + } + + private static void writeField(int tag, Object object, Writer writer) throws IOException { + int fieldNumber = WireFormat.getTagFieldNumber(tag); + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + writer.writeInt64(fieldNumber, (Long) object); + break; + case WireFormat.WIRETYPE_FIXED32: + writer.writeFixed32(fieldNumber, (Integer) object); + break; + case WireFormat.WIRETYPE_FIXED64: + writer.writeFixed64(fieldNumber, (Long) object); + break; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + writer.writeBytes(fieldNumber, (ByteString) object); + break; + case WireFormat.WIRETYPE_START_GROUP: + if (writer.fieldOrder() == Writer.FieldOrder.ASCENDING) { + writer.writeStartGroup(fieldNumber); + ((UnknownFieldSetLite) object).writeTo(writer); + writer.writeEndGroup(fieldNumber); + } else { + writer.writeEndGroup(fieldNumber); + ((UnknownFieldSetLite) object).writeTo(writer); + writer.writeStartGroup(fieldNumber); + } + break; + default: + // TODO(liujisi): Change writeTo to throw IOException? + throw new RuntimeException(InvalidProtocolBufferException.invalidWireType()); + } + } /** * Get the number of bytes required to encode this field, including field number, using {@code @@ -197,17 +244,17 @@ public final class UnknownFieldSetLite { if (size != -1) { return size; } - + size = 0; for (int i = 0; i < count; i++) { int tag = tags[i]; int fieldNumber = WireFormat.getTagFieldNumber(tag); - size += CodedOutputStream.computeRawMessageSetExtensionSize( - fieldNumber, (ByteString) objects[i]); + size += + CodedOutputStream.computeRawMessageSetExtensionSize(fieldNumber, (ByteString) objects[i]); } - + memoizedSerializedSize = size; - + return size; } @@ -221,7 +268,7 @@ public final class UnknownFieldSetLite { if (size != -1) { return size; } - + size = 0; for (int i = 0; i < count; i++) { int tag = tags[i]; @@ -240,19 +287,20 @@ public final class UnknownFieldSetLite { size += CodedOutputStream.computeBytesSize(fieldNumber, (ByteString) objects[i]); break; case WireFormat.WIRETYPE_START_GROUP: - size += CodedOutputStream.computeTagSize(fieldNumber) * 2 - + ((UnknownFieldSetLite) objects[i]).getSerializedSize(); + size += + CodedOutputStream.computeTagSize(fieldNumber) * 2 + + ((UnknownFieldSetLite) objects[i]).getSerializedSize(); break; default: throw new IllegalStateException(InvalidProtocolBufferException.invalidWireType()); } } - + memoizedSerializedSize = size; - + return size; } - + private static boolean equals(int[] tags1, int[] tags2, int count) { for (int i = 0; i < count; ++i) { if (tags1[i] != tags2[i]) { @@ -284,8 +332,8 @@ public final class UnknownFieldSetLite { if (!(obj instanceof UnknownFieldSetLite)) { return false; } - - UnknownFieldSetLite other = (UnknownFieldSetLite) obj; + + UnknownFieldSetLite other = (UnknownFieldSetLite) obj; if (count != other.count || !equals(tags, other.tags, count) || !equals(objects, other.objects, count)) { @@ -341,25 +389,23 @@ public final class UnknownFieldSetLite { void storeField(int tag, Object value) { checkMutable(); ensureCapacity(); - + tags[count] = tag; objects[count] = value; count++; } - - /** - * Ensures that our arrays are long enough to store more metadata. - */ + + /** Ensures that our arrays are long enough to store more metadata. */ private void ensureCapacity() { - if (count == tags.length) { + if (count == tags.length) { int increment = count < (MIN_CAPACITY / 2) ? MIN_CAPACITY : count >> 1; int newLength = count + increment; - + tags = Arrays.copyOf(tags, newLength); objects = Arrays.copyOf(objects, newLength); } } - + /** * Parse a single field from {@code input} and merge it into this set. * @@ -387,8 +433,7 @@ public final class UnknownFieldSetLite { case WireFormat.WIRETYPE_START_GROUP: final UnknownFieldSetLite subFieldSet = new UnknownFieldSetLite(); subFieldSet.mergeFrom(input); - input.checkLastTagWas( - WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP)); + input.checkLastTagWas(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP)); storeField(tag, subFieldSet); return true; case WireFormat.WIRETYPE_END_GROUP: @@ -399,9 +444,8 @@ public final class UnknownFieldSetLite { } /** - * Convenience method for merging a new field containing a single varint - * value. This is used in particular when an unknown enum value is - * encountered. + * Convenience method for merging a new field containing a single varint value. This is used in + * particular when an unknown enum value is encountered. * *

For use by generated code only. */ @@ -412,7 +456,7 @@ public final class UnknownFieldSetLite { } storeField(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_VARINT), (long) value); - + return this; } @@ -421,21 +465,18 @@ public final class UnknownFieldSetLite { * *

For use by generated code only. */ - UnknownFieldSetLite mergeLengthDelimitedField(final int fieldNumber, final ByteString value) { + UnknownFieldSetLite mergeLengthDelimitedField(final int fieldNumber, final ByteString value) { checkMutable(); if (fieldNumber == 0) { throw new IllegalArgumentException("Zero is not a valid field number."); } storeField(WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED), value); - + return this; } - - /** - * Parse an entire message from {@code input} and merge its fields into - * this set. - */ + + /** Parse an entire message from {@code input} and merge its fields into this set. */ private UnknownFieldSetLite mergeFrom(final CodedInputStream input) throws IOException { // Ensures initialization in mergeFieldFrom. while (true) { diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java new file mode 100755 index 0000000000..ffd7232308 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java @@ -0,0 +1,140 @@ +// 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. + +package com.google.protobuf; + +import java.io.IOException; + +class UnknownFieldSetLiteSchema + extends UnknownFieldSchema { + + UnknownFieldSetLiteSchema() {} + + @Override + boolean shouldDiscardUnknownFields(Reader reader) { + // We never drop unknown fields in lite. + return false; + } + + @Override + UnknownFieldSetLite newBuilder() { + return UnknownFieldSetLite.newInstance(); + } + + @Override + void addVarint(UnknownFieldSetLite fields, int number, long value) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_VARINT), value); + } + + @Override + void addFixed32(UnknownFieldSetLite fields, int number, int value) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_FIXED32), value); + } + + @Override + void addFixed64(UnknownFieldSetLite fields, int number, long value) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_FIXED64), value); + } + + @Override + void addLengthDelimited(UnknownFieldSetLite fields, int number, ByteString value) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED), value); + } + + @Override + void addGroup(UnknownFieldSetLite fields, int number, UnknownFieldSetLite subFieldSet) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_START_GROUP), subFieldSet); + } + + @Override + UnknownFieldSetLite toImmutable(UnknownFieldSetLite fields) { + fields.makeImmutable(); + return fields; + } + + @Override + void setToMessage(Object message, UnknownFieldSetLite fields) { + ((GeneratedMessageLite) message).unknownFields = fields; + } + + @Override + UnknownFieldSetLite getFromMessage(Object message) { + return ((GeneratedMessageLite) message).unknownFields; + } + + @Override + UnknownFieldSetLite getBuilderFromMessage(Object message) { + UnknownFieldSetLite unknownFields = getFromMessage(message); + // When parsing into a lite message object, its UnknownFieldSet is either the default instance + // or mutable. It can't be in a state where it's immutable but not default instance. + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = UnknownFieldSetLite.newInstance(); + setToMessage(message, unknownFields); + } + return unknownFields; + } + + @Override + void setBuilderToMessage(Object message, UnknownFieldSetLite fields) { + setToMessage(message, fields); + } + + @Override + void makeImmutable(Object message) { + getFromMessage(message).makeImmutable(); + } + + @Override + void writeTo(UnknownFieldSetLite fields, Writer writer) throws IOException { + fields.writeTo(writer); + } + + @Override + void writeAsMessageSetTo(UnknownFieldSetLite fields, Writer writer) throws IOException { + fields.writeAsMessageSetTo(writer); + } + + @Override + UnknownFieldSetLite merge(UnknownFieldSetLite message, UnknownFieldSetLite other) { + return other.equals(UnknownFieldSetLite.getDefaultInstance()) + ? message + : UnknownFieldSetLite.mutableCopyOf(message, other); + } + + @Override + int getSerializedSize(UnknownFieldSetLite unknowns) { + return unknowns.getSerializedSize(); + } + + @Override + int getSerializedSizeAsMessageSet(UnknownFieldSetLite unknowns) { + return unknowns.getSerializedSizeAsMessageSet(); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetSchema.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetSchema.java new file mode 100755 index 0000000000..b838c4b54a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetSchema.java @@ -0,0 +1,132 @@ +// 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. + +package com.google.protobuf; + +import java.io.IOException; + +class UnknownFieldSetSchema extends UnknownFieldSchema { + + private final boolean proto3; + + public UnknownFieldSetSchema(boolean proto3) { + this.proto3 = proto3; + } + + @Override + boolean shouldDiscardUnknownFields(Reader reader) { + return reader.shouldDiscardUnknownFields(); + } + + @Override + UnknownFieldSet.Builder newBuilder() { + return UnknownFieldSet.newBuilder(); + } + + @Override + void addVarint(UnknownFieldSet.Builder fields, int number, long value) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addVarint(value).build()); + } + + @Override + void addFixed32(UnknownFieldSet.Builder fields, int number, int value) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addFixed32(value).build()); + } + + @Override + void addFixed64(UnknownFieldSet.Builder fields, int number, long value) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addFixed64(value).build()); + } + + @Override + void addLengthDelimited(UnknownFieldSet.Builder fields, int number, ByteString value) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addLengthDelimited(value).build()); + } + + @Override + void addGroup(UnknownFieldSet.Builder fields, int number, UnknownFieldSet subFieldSet) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addGroup(subFieldSet).build()); + } + + @Override + UnknownFieldSet toImmutable(UnknownFieldSet.Builder fields) { + return fields.build(); + } + + @Override + void writeTo(UnknownFieldSet message, Writer writer) throws IOException { + message.writeTo(writer); + } + + @Override + void writeAsMessageSetTo(UnknownFieldSet message, Writer writer) throws IOException { + message.writeAsMessageSetTo(writer); + } + + @Override + UnknownFieldSet getFromMessage(Object message) { + return ((GeneratedMessageV3) message).unknownFields; + } + + @Override + void setToMessage(Object message, UnknownFieldSet fields) { + ((GeneratedMessageV3) message).unknownFields = fields; + } + + @Override + UnknownFieldSet.Builder getBuilderFromMessage(Object message) { + return ((GeneratedMessageV3) message).unknownFields.toBuilder(); + } + + @Override + void setBuilderToMessage(Object message, UnknownFieldSet.Builder builder) { + ((GeneratedMessageV3) message).unknownFields = builder.build(); + } + + @Override + void makeImmutable(Object message) { + // Already immutable. + } + + @Override + UnknownFieldSet merge(UnknownFieldSet message, UnknownFieldSet other) { + return message.toBuilder().mergeFrom(other).build(); + } + + @Override + int getSerializedSize(UnknownFieldSet message) { + return message.getSerializedSize(); + } + + @Override + int getSerializedSizeAsMessageSet(UnknownFieldSet unknowns) { + return unknowns.getSerializedSizeAsMessageSet(); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java b/java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java index 30e8791178..415b2cde0d 100644 --- a/java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java +++ b/java/core/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java @@ -39,8 +39,8 @@ import java.util.ListIterator; import java.util.RandomAccess; /** - * An implementation of {@link LazyStringList} that wraps another - * {@link LazyStringList} such that it cannot be modified via the wrapper. + * An implementation of {@link LazyStringList} that wraps another {@link LazyStringList} such that + * it cannot be modified via the wrapper. * * @author jonp@google.com (Jon Perlow) */ @@ -57,7 +57,7 @@ public class UnmodifiableLazyStringList extends AbstractList public String get(int index) { return list.get(index); } - + @Override public Object getRaw(int index) { return list.getRaw(index); @@ -97,7 +97,7 @@ public class UnmodifiableLazyStringList extends AbstractList public void add(byte[] element) { throw new UnsupportedOperationException(); } - + @Override public void set(int index, byte[] element) { throw new UnsupportedOperationException(); diff --git a/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java b/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java index 878c775816..bcaf1d2f33 100644 --- a/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java +++ b/java/core/src/main/java/com/google/protobuf/UnsafeByteOperations.java @@ -39,23 +39,24 @@ import java.nio.ByteBuffer; * potentially expose the backing buffer of a {@link ByteString} to the application. * *

DISCLAIMER: The methods in this class should only be called if it is - * guaranteed that the buffer backing the {@link ByteString} will never change! Mutation of a - * {@link ByteString} can lead to unexpected and undesirable consequences in your application, - * and will likely be difficult to debug. Proceed with caution! + * guaranteed that the buffer backing the {@link ByteString} will never change! Mutation of a {@link + * ByteString} can lead to unexpected and undesirable consequences in your application, and will + * likely be difficult to debug. Proceed with caution! + * + *

This can have a number of significant side affects that have spooky-action-at-a-distance-like + * behavior. In particular, if the bytes value changes out from under a Protocol Buffer: * - *

This can have a number of significant side affects that have - * spooky-action-at-a-distance-like behavior. In particular, if the bytes value changes out from - * under a Protocol Buffer: *

    - *
  • serialization may throw - *
  • serialization may succeed but the wrong bytes may be written out - *
  • messages are no longer threadsafe - *
  • hashCode may be incorrect - *
      - *
    • can result in a permanent memory leak when used as a key in a long-lived HashMap - *
    • the semantics of many programs may be violated if this is the case - *
    + *
  • serialization may throw + *
  • serialization may succeed but the wrong bytes may be written out + *
  • messages are no longer threadsafe + *
  • hashCode may be incorrect + *
      + *
    • can result in a permanent memory leak when used as a key in a long-lived HashMap + *
    • the semantics of many programs may be violated if this is the case + *
    *
+ * * Each of these issues will occur in parts of the code base that are entirely distinct from the * parts of the code base modifying the buffer. In fact, both parts of the code base may be correct * - it is the bridging with the unsafe operations that was in error! @@ -99,19 +100,19 @@ public final class UnsafeByteOperations { /** * Writes the given {@link ByteString} to the provided {@link ByteOutput}. Calling this method may - * result in multiple operations on the target {@link ByteOutput} - * (i.e. for roped {@link ByteString}s). + * result in multiple operations on the target {@link ByteOutput} (i.e. for roped {@link + * ByteString}s). * *

This method exposes the internal backing buffer(s) of the {@link ByteString} to the {@link * ByteOutput} in order to avoid additional copying overhead. It would be possible for a malicious * {@link ByteOutput} to corrupt the {@link ByteString}. Use with caution! * - *

NOTE: The {@link ByteOutput} MUST NOT modify the provided buffers. Doing - * so may result in corrupted data, which would be difficult to debug. + *

NOTE: The {@link ByteOutput} MUST NOT modify the provided buffers. Doing so + * may result in corrupted data, which would be difficult to debug. * * @param bytes the {@link ByteString} to be written - * @param output the output to receive the bytes - * @throws IOException if an I/O error occurs + * @param output the output to receive the bytes + * @throws IOException if an I/O error occurs */ public static void unsafeWriteTo(ByteString bytes, ByteOutput output) throws IOException { bytes.writeTo(output); diff --git a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java index f822ce51f8..74c2012749 100644 --- a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java +++ b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java @@ -48,7 +48,7 @@ final class UnsafeUtil { supportsUnsafeByteBufferOperations(); private static final boolean HAS_UNSAFE_ARRAY_OPERATIONS = supportsUnsafeArrayOperations(); - private static final long BYTE_ARRAY_BASE_OFFSET = arrayBaseOffset(byte[].class); + static final long BYTE_ARRAY_BASE_OFFSET = arrayBaseOffset(byte[].class); // Micro-optimization: we can assume a scale of 1 and skip the multiply // private static final long BYTE_ARRAY_INDEX_SCALE = 1; @@ -72,6 +72,13 @@ final class UnsafeUtil { private static final long BUFFER_ADDRESS_OFFSET = fieldOffset(bufferAddressField()); + private static final int STRIDE = 8; + private static final int STRIDE_ALIGNMENT_MASK = STRIDE - 1; + private static final int BYTE_ARRAY_ALIGNMENT = + (int) (BYTE_ARRAY_BASE_OFFSET & STRIDE_ALIGNMENT_MASK); + + static final boolean IS_BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN; + private UnsafeUtil() {} static boolean hasUnsafeArrayOperations() { @@ -83,6 +90,15 @@ final class UnsafeUtil { } + @SuppressWarnings("unchecked") // safe by method contract + static T allocateInstance(Class clazz) { + try { + return (T) UNSAFE.allocateInstance(clazz); + } catch (InstantiationException e) { + throw new IllegalStateException(e); + } + } + static long objectFieldOffset(Field field) { return MEMORY_ACCESSOR.objectFieldOffset(field); } @@ -226,7 +242,7 @@ final class UnsafeUtil { } static void copyMemory(byte[] src, long srcIndex, byte[] target, long targetIndex, long length) { - System.arraycopy(src, (int) srcIndex, target, (int) targetIndex, (int) length); + System.arraycopy(src, (int) srcIndex, target, (int) targetIndex, (int) length); } static byte getByte(long address) { @@ -253,9 +269,7 @@ final class UnsafeUtil { MEMORY_ACCESSOR.putLong(address, value); } - /** - * Gets the offset of the {@code address} field of the given direct {@link ByteBuffer}. - */ + /** Gets the offset of the {@code address} field of the given direct {@link ByteBuffer}. */ static long addressOffset(ByteBuffer buffer) { return MEMORY_ACCESSOR.getLong(buffer, BUFFER_ADDRESS_OFFSET); } @@ -375,6 +389,80 @@ final class UnsafeUtil { return field != null && field.getType() == long.class ? field : null; } + /** + * Returns the index of the first byte where left and right differ, in the range [0, 8]. If {@code + * left == right}, the result will be 8, otherwise less than 8. + * + *

This counts from the *first* byte, which may be the most or least significant byte depending + * on the system endianness. + */ + private static int firstDifferingByteIndexNativeEndian(long left, long right) { + int n = + IS_BIG_ENDIAN + ? Long.numberOfLeadingZeros(left ^ right) + : Long.numberOfTrailingZeros(left ^ right); + return n >> 3; + } + + /** + * Returns the lowest {@code index} such that {@code 0 <= index < length} and {@code left[leftOff + * + index] != right[rightOff + index]}. If no such value exists -- if {@code left} and {@code + * right} match up to {@code length} bytes from their respective offsets -- returns -1. + * + *

{@code leftOff + length} must be less than or equal to {@code left.length}, and the same for + * {@code right}. + */ + static int mismatch(byte[] left, int leftOff, byte[] right, int rightOff, int length) { + if (leftOff < 0 + || rightOff < 0 + || length < 0 + || leftOff + length > left.length + || rightOff + length > right.length) { + throw new IndexOutOfBoundsException(); + } + + int index = 0; + if (HAS_UNSAFE_ARRAY_OPERATIONS) { + int leftAlignment = (BYTE_ARRAY_ALIGNMENT + leftOff) & STRIDE_ALIGNMENT_MASK; + + // Most CPUs handle getting chunks of bytes better on addresses that are a multiple of 4 + // or 8. + // We walk one byte at a time until the left address, at least, is a multiple of 8. + // If the right address is, too, so much the better. + for (; + index < length && (leftAlignment & STRIDE_ALIGNMENT_MASK) != 0; + index++, leftAlignment++) { + if (left[leftOff + index] != right[rightOff + index]) { + return index; + } + } + + // Stride! Grab eight bytes at a time from left and right and check them for equality. + + int strideLength = ((length - index) & ~STRIDE_ALIGNMENT_MASK) + index; + // strideLength is the point where we want to stop striding: it differs from index by + // a multiple of STRIDE, and it's the largest such number <= length. + + for (; index < strideLength; index += STRIDE) { + long leftLongWord = getLong(left, BYTE_ARRAY_BASE_OFFSET + leftOff + index); + long rightLongWord = getLong(right, BYTE_ARRAY_BASE_OFFSET + rightOff + index); + if (leftLongWord != rightLongWord) { + // one of these eight bytes differ! use a helper to find out which one + return index + firstDifferingByteIndexNativeEndian(leftLongWord, rightLongWord); + } + } + } + + // If we were able to stride, there are at most STRIDE - 1 bytes left to compare. + // If we weren't, then this loop covers the whole thing. + for (; index < length; index++) { + if (left[leftOff + index] != right[rightOff + index]) { + return index; + } + } + return -1; + } + /** * Returns the offset of the provided field, or {@code -1} if {@code sun.misc.Unsafe} is not * available. @@ -384,14 +472,12 @@ final class UnsafeUtil { } /** - * Gets the field with the given name within the class, or {@code null} if not found. If found, - * the field is made accessible. + * Gets the field with the given name within the class, or {@code null} if not found. */ private static Field field(Class clazz, String fieldName) { Field field; try { field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); } catch (Throwable t) { // Failed to access the fields. field = null; @@ -472,9 +558,9 @@ final class UnsafeUtil { public abstract void putLong(long address, long value); public abstract Object getStaticObject(Field field); - + public abstract void copyMemory(long srcOffset, byte[] target, long targetIndex, long length); - + public abstract void copyMemory(byte[] src, long srcIndex, long targetOffset, long length); } @@ -553,12 +639,12 @@ final class UnsafeUtil { public void putDouble(Object target, long offset, double value) { unsafe.putDouble(target, offset, value); } - - @Override + + @Override public void copyMemory(long srcOffset, byte[] target, long targetIndex, long length) { unsafe.copyMemory(null, srcOffset, target, BYTE_ARRAY_BASE_OFFSET + targetIndex, length); } - + @Override public void copyMemory(byte[] src, long srcIndex, long targetOffset, long length) { unsafe.copyMemory(src, BYTE_ARRAY_BASE_OFFSET + srcIndex, null, targetOffset, length); diff --git a/java/core/src/main/java/com/google/protobuf/Utf8.java b/java/core/src/main/java/com/google/protobuf/Utf8.java index b4a81ca3a2..104ab09832 100644 --- a/java/core/src/main/java/com/google/protobuf/Utf8.java +++ b/java/core/src/main/java/com/google/protobuf/Utf8.java @@ -44,36 +44,31 @@ import static java.lang.Character.toCodePoint; import java.nio.ByteBuffer; /** - * A set of low-level, high-performance static utility methods related - * to the UTF-8 character encoding. This class has no dependencies - * outside of the core JDK libraries. + * A set of low-level, high-performance static utility methods related to the UTF-8 character + * encoding. This class has no dependencies outside of the core JDK libraries. * - *

There are several variants of UTF-8. The one implemented by - * this class is the restricted definition of UTF-8 introduced in - * Unicode 3.1, which mandates the rejection of "overlong" byte - * sequences as well as rejection of 3-byte surrogate codepoint byte - * sequences. Note that the UTF-8 decoder included in Oracle's JDK - * has been modified to also reject "overlong" byte sequences, but (as - * of 2011) still accepts 3-byte surrogate codepoint byte sequences. + *

There are several variants of UTF-8. The one implemented by this class is the restricted + * definition of UTF-8 introduced in Unicode 3.1, which mandates the rejection of "overlong" byte + * sequences as well as rejection of 3-byte surrogate codepoint byte sequences. Note that the UTF-8 + * decoder included in Oracle's JDK has been modified to also reject "overlong" byte sequences, but + * (as of 2011) still accepts 3-byte surrogate codepoint byte sequences. * - *

The byte sequences considered valid by this class are exactly - * those that can be roundtrip converted to Strings and back to bytes - * using the UTF-8 charset, without loss:

 {@code
+ * 

The byte sequences considered valid by this class are exactly those that can be roundtrip + * converted to Strings and back to bytes using the UTF-8 charset, without loss: + * + *

{@code
  * Arrays.equals(bytes, new String(bytes, Internal.UTF_8).getBytes(Internal.UTF_8))
  * }
* - *

See the Unicode Standard,
- * Table 3-6. UTF-8 Bit Distribution,
- * Table 3-7. Well Formed UTF-8 Byte Sequences. + *

See the Unicode Standard,
Table 3-6. UTF-8 Bit Distribution,
Table 3-7. + * Well Formed UTF-8 Byte Sequences. * - *

This class supports decoding of partial byte sequences, so that the - * bytes in a complete UTF-8 byte sequences can be stored in multiple - * segments. Methods typically return {@link #MALFORMED} if the partial - * byte sequence is definitely not well-formed, {@link #COMPLETE} if it is - * well-formed in the absence of additional input, or if the byte sequence - * apparently terminated in the middle of a character, an opaque integer - * "state" value containing enough information to decode the character when - * passed to a subsequent invocation of a partial decoding method. + *

This class supports decoding of partial byte sequences, so that the bytes in a complete UTF-8 + * byte sequences can be stored in multiple segments. Methods typically return {@link #MALFORMED} if + * the partial byte sequence is definitely not well-formed, {@link #COMPLETE} if it is well-formed + * in the absence of additional input, or if the byte sequence apparently terminated in the middle + * of a character, an opaque integer "state" value containing enough information to decode the + * character when passed to a subsequent invocation of a partial decoding method. * * @author martinrb@google.com (Martin Buchholz) */ @@ -98,31 +93,28 @@ final class Utf8 { /** * Maximum number of bytes per Java UTF-16 char in UTF-8. + * * @see java.nio.charset.CharsetEncoder#maxBytesPerChar() */ static final int MAX_BYTES_PER_CHAR = 3; /** - * State value indicating that the byte sequence is well-formed and - * complete (no further bytes are needed to complete a character). + * State value indicating that the byte sequence is well-formed and complete (no further bytes are + * needed to complete a character). */ public static final int COMPLETE = 0; - /** - * State value indicating that the byte sequence is definitely not - * well-formed. - */ + /** State value indicating that the byte sequence is definitely not well-formed. */ public static final int MALFORMED = -1; /** * Used by {@code Unsafe} UTF-8 string validation logic to determine the minimum string length * above which to employ an optimized algorithm for counting ASCII characters. The reason for this * threshold is that for small strings, the optimization may not be beneficial or may even - * negatively impact performance since it requires additional logic to avoid unaligned reads - * (when calling {@code Unsafe.getLong}). This threshold guarantees that even if the initial - * offset is unaligned, we're guaranteed to make at least one call to {@code Unsafe.getLong()} - * which provides a performance improvement that entirely subsumes the cost of the additional - * logic. + * negatively impact performance since it requires additional logic to avoid unaligned reads (when + * calling {@code Unsafe.getLong}). This threshold guarantees that even if the initial offset is + * unaligned, we're guaranteed to make at least one call to {@code Unsafe.getLong()} which + * provides a performance improvement that entirely subsumes the cost of the additional logic. */ private static final int UNSAFE_COUNT_ASCII_THRESHOLD = 16; @@ -146,76 +138,69 @@ final class Utf8 { // are valid trailing bytes. /** - * Returns {@code true} if the given byte array is a well-formed - * UTF-8 byte sequence. + * Returns {@code true} if the given byte array is a well-formed UTF-8 byte sequence. * - *

This is a convenience method, equivalent to a call to {@code - * isValidUtf8(bytes, 0, bytes.length)}. + *

This is a convenience method, equivalent to a call to {@code isValidUtf8(bytes, 0, + * bytes.length)}. */ public static boolean isValidUtf8(byte[] bytes) { return processor.isValidUtf8(bytes, 0, bytes.length); } /** - * Returns {@code true} if the given byte array slice is a - * well-formed UTF-8 byte sequence. The range of bytes to be - * checked extends from index {@code index}, inclusive, to {@code - * limit}, exclusive. + * Returns {@code true} if the given byte array slice is a well-formed UTF-8 byte sequence. The + * range of bytes to be checked extends from index {@code index}, inclusive, to {@code limit}, + * exclusive. * - *

This is a convenience method, equivalent to {@code - * partialIsValidUtf8(bytes, index, limit) == Utf8.COMPLETE}. + *

This is a convenience method, equivalent to {@code partialIsValidUtf8(bytes, index, limit) + * == Utf8.COMPLETE}. */ public static boolean isValidUtf8(byte[] bytes, int index, int limit) { return processor.isValidUtf8(bytes, index, limit); } /** - * Tells whether the given byte array slice is a well-formed, - * malformed, or incomplete UTF-8 byte sequence. The range of bytes - * to be checked extends from index {@code index}, inclusive, to + * Tells whether the given byte array slice is a well-formed, malformed, or incomplete UTF-8 byte + * sequence. The range of bytes to be checked extends from index {@code index}, inclusive, to * {@code limit}, exclusive. * - * @param state either {@link Utf8#COMPLETE} (if this is the initial decoding - * operation) or the value returned from a call to a partial decoding method - * for the previous bytes - * - * @return {@link #MALFORMED} if the partial byte sequence is - * definitely not well-formed, {@link #COMPLETE} if it is well-formed - * (no additional input needed), or if the byte sequence is - * "incomplete", i.e. apparently terminated in the middle of a character, - * an opaque integer "state" value containing enough information to - * decode the character when passed to a subsequent invocation of a - * partial decoding method. + * @param state either {@link Utf8#COMPLETE} (if this is the initial decoding operation) or the + * value returned from a call to a partial decoding method for the previous bytes + * @return {@link #MALFORMED} if the partial byte sequence is definitely not well-formed, {@link + * #COMPLETE} if it is well-formed (no additional input needed), or if the byte sequence is + * "incomplete", i.e. apparently terminated in the middle of a character, an opaque integer + * "state" value containing enough information to decode the character when passed to a + * subsequent invocation of a partial decoding method. */ public static int partialIsValidUtf8(int state, byte[] bytes, int index, int limit) { return processor.partialIsValidUtf8(state, bytes, index, limit); } private static int incompleteStateFor(int byte1) { - return (byte1 > (byte) 0xF4) ? - MALFORMED : byte1; + return (byte1 > (byte) 0xF4) ? MALFORMED : byte1; } private static int incompleteStateFor(int byte1, int byte2) { - return (byte1 > (byte) 0xF4 || - byte2 > (byte) 0xBF) ? - MALFORMED : byte1 ^ (byte2 << 8); + return (byte1 > (byte) 0xF4 || byte2 > (byte) 0xBF) ? MALFORMED : byte1 ^ (byte2 << 8); } private static int incompleteStateFor(int byte1, int byte2, int byte3) { - return (byte1 > (byte) 0xF4 || - byte2 > (byte) 0xBF || - byte3 > (byte) 0xBF) ? - MALFORMED : byte1 ^ (byte2 << 8) ^ (byte3 << 16); + return (byte1 > (byte) 0xF4 || byte2 > (byte) 0xBF || byte3 > (byte) 0xBF) + ? MALFORMED + : byte1 ^ (byte2 << 8) ^ (byte3 << 16); } private static int incompleteStateFor(byte[] bytes, int index, int limit) { int byte1 = bytes[index - 1]; switch (limit - index) { - case 0: return incompleteStateFor(byte1); - case 1: return incompleteStateFor(byte1, bytes[index]); - case 2: return incompleteStateFor(byte1, bytes[index], bytes[index + 1]); - default: throw new AssertionError(); + case 0: + return incompleteStateFor(byte1); + case 1: + return incompleteStateFor(byte1, bytes[index]); + case 2: + return incompleteStateFor(byte1, bytes[index], bytes[index + 1]); + default: + throw new AssertionError(); } } @@ -236,7 +221,7 @@ final class Utf8 { // These UTF-8 handling methods are copied from Guava's Utf8 class with a modification to throw // a protocol buffer local exception. This exception is then caught in CodedOutputStream so it can // fallback to more lenient behavior. - + static class UnpairedSurrogateException extends IllegalArgumentException { UnpairedSurrogateException(int index, int length) { super("Unpaired surrogate at index " + index + " of " + length); @@ -244,9 +229,9 @@ final class Utf8 { } /** - * Returns the number of bytes in the UTF-8-encoded form of {@code sequence}. For a string, - * this method is equivalent to {@code string.getBytes(UTF_8).length}, but is more efficient in - * both time and space. + * Returns the number of bytes in the UTF-8-encoded form of {@code sequence}. For a string, this + * method is equivalent to {@code string.getBytes(UTF_8).length}, but is more efficient in both + * time and space. * * @throws IllegalArgumentException if {@code sequence} contains ill-formed UTF-16 (unpaired * surrogates) @@ -266,7 +251,7 @@ final class Utf8 { for (; i < utf16Length; i++) { char c = sequence.charAt(i); if (c < 0x800) { - utf8Length += ((0x7f - c) >>> 31); // branch free! + utf8Length += ((0x7f - c) >>> 31); // branch free! } else { utf8Length += encodedLengthGeneral(sequence, i); break; @@ -275,8 +260,8 @@ final class Utf8 { if (utf8Length < utf16Length) { // Necessary and sufficient condition for overflow because of maximum 3x expansion - throw new IllegalArgumentException("UTF-8 length does not fit in int: " - + (utf8Length + (1L << 32))); + throw new IllegalArgumentException( + "UTF-8 length does not fit in int: " + (utf8Length + (1L << 32))); } return utf8Length; } @@ -370,15 +355,15 @@ final class Utf8 { } /** - * Counts (approximately) the number of consecutive ASCII characters in the given buffer. - * The byte order of the {@link ByteBuffer} does not matter, so performance can be improved if - * native byte order is used (i.e. no byte-swapping in {@link ByteBuffer#getLong(int)}). + * Counts (approximately) the number of consecutive ASCII characters in the given buffer. The byte + * order of the {@link ByteBuffer} does not matter, so performance can be improved if native byte + * order is used (i.e. no byte-swapping in {@link ByteBuffer#getLong(int)}). * * @param buffer the buffer to be scanned for ASCII chars * @param index the starting index of the scan * @param limit the limit within buffer for the scan - * @return the number of ASCII characters found. The stopping position will be at or - * before the first non-ASCII byte. + * @return the number of ASCII characters found. The stopping position will be at or before the + * first non-ASCII byte. */ private static int estimateConsecutiveAscii(ByteBuffer buffer, int index, int limit) { int i = index; @@ -390,52 +375,43 @@ final class Utf8 { return i - index; } - /** - * A processor of UTF-8 strings, providing methods for checking validity and encoding. - */ + /** A processor of UTF-8 strings, providing methods for checking validity and encoding. */ // TODO(nathanmittler): Add support for Memory/MemoryBlock on Android. abstract static class Processor { /** - * Returns {@code true} if the given byte array slice is a - * well-formed UTF-8 byte sequence. The range of bytes to be - * checked extends from index {@code index}, inclusive, to {@code - * limit}, exclusive. + * Returns {@code true} if the given byte array slice is a well-formed UTF-8 byte sequence. The + * range of bytes to be checked extends from index {@code index}, inclusive, to {@code limit}, + * exclusive. * - *

This is a convenience method, equivalent to {@code - * partialIsValidUtf8(bytes, index, limit) == Utf8.COMPLETE}. + *

This is a convenience method, equivalent to {@code partialIsValidUtf8(bytes, index, limit) + * == Utf8.COMPLETE}. */ final boolean isValidUtf8(byte[] bytes, int index, int limit) { return partialIsValidUtf8(COMPLETE, bytes, index, limit) == COMPLETE; } /** - * Tells whether the given byte array slice is a well-formed, - * malformed, or incomplete UTF-8 byte sequence. The range of bytes - * to be checked extends from index {@code index}, inclusive, to - * {@code limit}, exclusive. - * - * @param state either {@link Utf8#COMPLETE} (if this is the initial decoding - * operation) or the value returned from a call to a partial decoding method - * for the previous bytes + * Tells whether the given byte array slice is a well-formed, malformed, or incomplete UTF-8 + * byte sequence. The range of bytes to be checked extends from index {@code index}, inclusive, + * to {@code limit}, exclusive. * - * @return {@link #MALFORMED} if the partial byte sequence is - * definitely not well-formed, {@link #COMPLETE} if it is well-formed - * (no additional input needed), or if the byte sequence is - * "incomplete", i.e. apparently terminated in the middle of a character, - * an opaque integer "state" value containing enough information to - * decode the character when passed to a subsequent invocation of a - * partial decoding method. + * @param state either {@link Utf8#COMPLETE} (if this is the initial decoding operation) or the + * value returned from a call to a partial decoding method for the previous bytes + * @return {@link #MALFORMED} if the partial byte sequence is definitely not well-formed, {@link + * #COMPLETE} if it is well-formed (no additional input needed), or if the byte sequence is + * "incomplete", i.e. apparently terminated in the middle of a character, an opaque integer + * "state" value containing enough information to decode the character when passed to a + * subsequent invocation of a partial decoding method. */ abstract int partialIsValidUtf8(int state, byte[] bytes, int index, int limit); /** - * Returns {@code true} if the given portion of the {@link ByteBuffer} is a - * well-formed UTF-8 byte sequence. The range of bytes to be - * checked extends from index {@code index}, inclusive, to {@code - * limit}, exclusive. + * Returns {@code true} if the given portion of the {@link ByteBuffer} is a well-formed UTF-8 + * byte sequence. The range of bytes to be checked extends from index {@code index}, inclusive, + * to {@code limit}, exclusive. * - *

This is a convenience method, equivalent to {@code - * partialIsValidUtf8(bytes, index, limit) == Utf8.COMPLETE}. + *

This is a convenience method, equivalent to {@code partialIsValidUtf8(bytes, index, limit) + * == Utf8.COMPLETE}. */ final boolean isValidUtf8(ByteBuffer buffer, int index, int limit) { return partialIsValidUtf8(COMPLETE, buffer, index, limit) == COMPLETE; @@ -452,22 +428,20 @@ final class Utf8 { if (buffer.hasArray()) { final int offset = buffer.arrayOffset(); return partialIsValidUtf8(state, buffer.array(), offset + index, offset + limit); - } else if (buffer.isDirect()){ + } else if (buffer.isDirect()) { return partialIsValidUtf8Direct(state, buffer, index, limit); } return partialIsValidUtf8Default(state, buffer, index, limit); } - /** - * Performs validation for direct {@link ByteBuffer} instances. - */ + /** Performs validation for direct {@link ByteBuffer} instances. */ abstract int partialIsValidUtf8Direct( final int state, final ByteBuffer buffer, int index, final int limit); /** * Performs validation for {@link ByteBuffer} instances using the {@link ByteBuffer} API rather - * than potentially faster approaches. This first completes validation for the current - * character (provided by {@code state}) and then finishes validation for the sequence. + * than potentially faster approaches. This first completes validation for the current character + * (provided by {@code state}) and then finishes validation for the sequence. */ final int partialIsValidUtf8Default( final int state, final ByteBuffer buffer, int index, final int limit) { @@ -566,7 +540,7 @@ final class Utf8 { private static int partialIsValidUtf8(final ByteBuffer buffer, int index, final int limit) { index += estimateConsecutiveAscii(buffer, index, limit); - for (;;) { + for (; ; ) { // Optimize for interior runs of ASCII bytes. // TODO(nathanmittler): Consider checking 8 bytes at a time after some threshold? // Maybe after seeing a few in a row that are ASCII, go back to fast mode? @@ -658,15 +632,13 @@ final class Utf8 { return decodeUtf8Default(buffer, index, size); } - /** - * Decodes direct {@link ByteBuffer} instances into {@link String}. - */ + /** Decodes direct {@link ByteBuffer} instances into {@link String}. */ abstract String decodeUtf8Direct(ByteBuffer buffer, int index, int size) throws InvalidProtocolBufferException; /** - * Decodes {@link ByteBuffer} instances using the {@link ByteBuffer} API rather than - * potentially faster approaches. + * Decodes {@link ByteBuffer} instances using the {@link ByteBuffer} API rather than potentially + * faster approaches. */ final String decodeUtf8Default(ByteBuffer buffer, int index, int size) throws InvalidProtocolBufferException { @@ -747,21 +719,22 @@ final class Utf8 { /** * Encodes an input character sequence ({@code in}) to UTF-8 in the target array ({@code out}). * For a string, this method is similar to + * *

{@code
      * byte[] a = string.getBytes(UTF_8);
      * System.arraycopy(a, 0, bytes, offset, a.length);
      * return offset + a.length;
      * }
* - * but is more efficient in both time and space. One key difference is that this method - * requires paired surrogates, and therefore does not support chunking. - * While {@code String.getBytes(UTF_8)} replaces unpaired surrogates with the default - * replacement character, this method throws {@link UnpairedSurrogateException}. + * but is more efficient in both time and space. One key difference is that this method requires + * paired surrogates, and therefore does not support chunking. While {@code + * String.getBytes(UTF_8)} replaces unpaired surrogates with the default replacement character, + * this method throws {@link UnpairedSurrogateException}. * *

To ensure sufficient space in the output buffer, either call {@link #encodedLength} to - * compute the exact amount needed, or leave room for - * {@code Utf8.MAX_BYTES_PER_CHAR * sequence.length()}, which is the largest possible number - * of bytes that any input can be encoded to. + * compute the exact amount needed, or leave room for {@code Utf8.MAX_BYTES_PER_CHAR * + * sequence.length()}, which is the largest possible number of bytes that any input can be + * encoded to. * * @param in the input character sequence to be encoded * @param out the target array @@ -778,26 +751,24 @@ final class Utf8 { /** * Encodes an input character sequence ({@code in}) to UTF-8 in the target buffer ({@code out}). * Upon returning from this method, the {@code out} position will point to the position after - * the last encoded byte. This method requires paired surrogates, and therefore does not - * support chunking. + * the last encoded byte. This method requires paired surrogates, and therefore does not support + * chunking. * *

To ensure sufficient space in the output buffer, either call {@link #encodedLength} to - * compute the exact amount needed, or leave room for - * {@code Utf8.MAX_BYTES_PER_CHAR * in.length()}, which is the largest possible number - * of bytes that any input can be encoded to. + * compute the exact amount needed, or leave room for {@code Utf8.MAX_BYTES_PER_CHAR * + * in.length()}, which is the largest possible number of bytes that any input can be encoded to. * * @param in the source character sequence to be encoded * @param out the target buffer * @throws UnpairedSurrogateException if {@code in} contains ill-formed UTF-16 (unpaired * surrogates) - * @throws ArrayIndexOutOfBoundsException if {@code in} encoded in UTF-8 is longer than - * {@code out.remaining()} + * @throws ArrayIndexOutOfBoundsException if {@code in} encoded in UTF-8 is longer than {@code + * out.remaining()} */ final void encodeUtf8(CharSequence in, ByteBuffer out) { if (out.hasArray()) { final int offset = out.arrayOffset(); - int endIndex = - Utf8.encode(in, out.array(), offset + out.position(), out.remaining()); + int endIndex = Utf8.encode(in, out.array(), offset + out.position(), out.remaining()); out.position(endIndex - offset); } else if (out.isDirect()) { encodeUtf8Direct(in, out); @@ -806,9 +777,7 @@ final class Utf8 { } } - /** - * Encodes the input character sequence to a direct {@link ByteBuffer} instance. - */ + /** Encodes the input character sequence to a direct {@link ByteBuffer} instance. */ abstract void encodeUtf8Direct(CharSequence in, ByteBuffer out); /** @@ -825,7 +794,7 @@ final class Utf8 { // if it occurs. try { // Designed to take advantage of - // https://wikis.oracle.com/display/HotSpotInternals/RangeCheckElimination + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination for (char c; inIx < inLength && (c = in.charAt(inIx)) < 0x80; ++inIx) { out.put(outIx + inIx, (byte) c); } @@ -887,9 +856,7 @@ final class Utf8 { } } - /** - * {@link Processor} implementation that does not use any {@code sun.misc.Unsafe} methods. - */ + /** {@link Processor} implementation that does not use any {@code sun.misc.Unsafe} methods. */ static final class SafeProcessor extends Processor { @Override int partialIsValidUtf8(int state, byte[] bytes, int index, int limit) { @@ -901,7 +868,7 @@ final class Utf8 { // // We expect such "straddler characters" to be rare. - if (index >= limit) { // No bytes? No progress. + if (index >= limit) { // No bytes? No progress. return state; } int byte1 = (byte) state; @@ -1074,7 +1041,7 @@ final class Utf8 { int i = 0; int limit = offset + length; // Designed to take advantage of - // https://wikis.oracle.com/display/HotSpotInternals/RangeCheckElimination + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination for (char c; i < utf16Length && i + j < limit && (c = in.charAt(i)) < 0x80; i++) { out[j + i] = (byte) c; } @@ -1098,8 +1065,7 @@ final class Utf8 { // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, // four UTF-8 bytes final char low; - if (i + 1 == in.length() - || !Character.isSurrogatePair(c, (low = in.charAt(++i)))) { + if (i + 1 == in.length() || !Character.isSurrogatePair(c, (low = in.charAt(++i)))) { throw new UnpairedSurrogateException((i - 1), utf16Length); } int codePoint = Character.toCodePoint(c, low); @@ -1111,8 +1077,7 @@ final class Utf8 { // If we are surrogates and we're not a surrogate pair, always throw an // UnpairedSurrogateException instead of an ArrayOutOfBoundsException. if ((Character.MIN_SURROGATE <= c && c <= Character.MAX_SURROGATE) - && (i + 1 == in.length() - || !Character.isSurrogatePair(c, in.charAt(i + 1)))) { + && (i + 1 == in.length() || !Character.isSurrogatePair(c, in.charAt(i + 1)))) { throw new UnpairedSurrogateException(i, utf16Length); } throw new ArrayIndexOutOfBoundsException("Failed writing " + c + " at index " + j); @@ -1138,8 +1103,9 @@ final class Utf8 { } private static int partialIsValidUtf8NonAscii(byte[] bytes, int index, int limit) { - for (;;) { - int byte1, byte2; + for (; ; ) { + int byte1; + int byte2; // Optimize for interior runs of ASCII bytes. do { @@ -1158,8 +1124,7 @@ final class Utf8 { // Simultaneously checks for illegal trailing-byte in // leading position and overlong 2-byte form. - if (byte1 < (byte) 0xC2 - || bytes[index++] > (byte) 0xBF) { + if (byte1 < (byte) 0xC2 || bytes[index++] > (byte) 0xBF) { return MALFORMED; } } else if (byte1 < (byte) 0xF0) { @@ -1180,7 +1145,7 @@ final class Utf8 { } else { // four-byte form - if (index >= limit - 2) { // incomplete sequence + if (index >= limit - 2) { // incomplete sequence return incompleteStateFor(bytes, index, limit); } if ((byte2 = bytes[index++]) > (byte) 0xBF @@ -1200,13 +1165,9 @@ final class Utf8 { } } - /** - * {@link Processor} that uses {@code sun.misc.Unsafe} where possible to improve performance. - */ + /** {@link Processor} that uses {@code sun.misc.Unsafe} where possible to improve performance. */ static final class UnsafeProcessor extends Processor { - /** - * Indicates whether or not all required unsafe operations are supported on this platform. - */ + /** Indicates whether or not all required unsafe operations are supported on this platform. */ static boolean isAvailable() { return hasUnsafeArrayOperations() && hasUnsafeByteBufferOperations(); } @@ -1228,7 +1189,7 @@ final class Utf8 { // // We expect such "straddler characters" to be rare. - if (offset >= offsetLimit) { // No bytes? No progress. + if (offset >= offsetLimit) { // No bytes? No progress. return state; } int byte1 = (byte) state; @@ -1566,7 +1527,7 @@ final class Utf8 { } // Designed to take advantage of - // https://wikis.oracle.com/display/HotSpotInternals/RangeCheckElimination + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination int inIx = 0; for (char c; inIx < inLimit && (c = in.charAt(inIx)) < 0x80; ++inIx) { UnsafeUtil.putByte(out, outIx++, (byte) c); @@ -1628,7 +1589,7 @@ final class Utf8 { } // Designed to take advantage of - // https://wikis.oracle.com/display/HotSpotInternals/RangeCheckElimination + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination int inIx = 0; for (char c; inIx < inLimit && (c = in.charAt(inIx)) < 0x80; ++inIx) { UnsafeUtil.putByte(outIx++, (byte) c); @@ -1685,8 +1646,8 @@ final class Utf8 { * @param bytes the array containing the character sequence * @param offset the offset position of the index (same as index + arrayBaseOffset) * @param maxChars the maximum number of characters to count - * @return the number of ASCII characters found. The stopping position will be at or - * before the first non-ASCII byte. + * @return the number of ASCII characters found. The stopping position will be at or before the + * first non-ASCII byte. */ private static int unsafeEstimateConsecutiveAscii( byte[] bytes, long offset, final int maxChars) { @@ -1728,24 +1689,24 @@ final class Utf8 { // To speed things up further, we're reading longs instead of bytes so we use a mask to // determine if any byte in the current long is non-ASCII. remaining -= unaligned; - for (; remaining >= 8 && (UnsafeUtil.getLong(address) & ASCII_MASK_LONG) == 0; + for (; + remaining >= 8 && (UnsafeUtil.getLong(address) & ASCII_MASK_LONG) == 0; address += 8, remaining -= 8) {} return maxChars - remaining; } private static int partialIsValidUtf8(final byte[] bytes, long offset, int remaining) { - // Skip past ASCII characters as quickly as possible. + // Skip past ASCII characters as quickly as possible. final int skipped = unsafeEstimateConsecutiveAscii(bytes, offset, remaining); remaining -= skipped; offset += skipped; - for (;;) { + for (; ; ) { // Optimize for interior runs of ASCII bytes. // TODO(nathanmittler): Consider checking 8 bytes at a time after some threshold? // Maybe after seeing a few in a row that are ASCII, go back to fast mode? int byte1 = 0; - for (; remaining > 0 && (byte1 = UnsafeUtil.getByte(bytes, offset++)) >= 0; --remaining) { - } + for (; remaining > 0 && (byte1 = UnsafeUtil.getByte(bytes, offset++)) >= 0; --remaining) {} if (remaining == 0) { return COMPLETE; } @@ -1762,8 +1723,7 @@ final class Utf8 { // Simultaneously checks for illegal trailing-byte in // leading position and overlong 2-byte form. - if (byte1 < (byte) 0xC2 - || UnsafeUtil.getByte(bytes, offset++) > (byte) 0xBF) { + if (byte1 < (byte) 0xC2 || UnsafeUtil.getByte(bytes, offset++) > (byte) 0xBF) { return MALFORMED; } } else if (byte1 < (byte) 0xF0) { @@ -1815,13 +1775,12 @@ final class Utf8 { address += skipped; remaining -= skipped; - for (;;) { + for (; ; ) { // Optimize for interior runs of ASCII bytes. // TODO(nathanmittler): Consider checking 8 bytes at a time after some threshold? // Maybe after seeing a few in a row that are ASCII, go back to fast mode? int byte1 = 0; - for (; remaining > 0 && (byte1 = UnsafeUtil.getByte(address++)) >= 0; --remaining) { - } + for (; remaining > 0 && (byte1 = UnsafeUtil.getByte(address++)) >= 0; --remaining) {} if (remaining == 0) { return COMPLETE; } @@ -1886,40 +1845,32 @@ final class Utf8 { } } - private static int unsafeIncompleteStateFor(byte[] bytes, int byte1, long offset, - int remaining) { + private static int unsafeIncompleteStateFor( + byte[] bytes, int byte1, long offset, int remaining) { switch (remaining) { - case 0: { + case 0: return incompleteStateFor(byte1); - } - case 1: { + case 1: return incompleteStateFor(byte1, UnsafeUtil.getByte(bytes, offset)); - } - case 2: { - return incompleteStateFor(byte1, UnsafeUtil.getByte(bytes, offset), - UnsafeUtil.getByte(bytes, offset + 1)); - } - default: { + case 2: + return incompleteStateFor( + byte1, UnsafeUtil.getByte(bytes, offset), UnsafeUtil.getByte(bytes, offset + 1)); + default: throw new AssertionError(); - } } } private static int unsafeIncompleteStateFor(long address, final int byte1, int remaining) { switch (remaining) { - case 0: { + case 0: return incompleteStateFor(byte1); - } - case 1: { + case 1: return incompleteStateFor(byte1, UnsafeUtil.getByte(address)); - } - case 2: { - return incompleteStateFor(byte1, UnsafeUtil.getByte(address), - UnsafeUtil.getByte(address + 1)); - } - default: { + case 2: + return incompleteStateFor( + byte1, UnsafeUtil.getByte(address), UnsafeUtil.getByte(address + 1)); + default: throw new AssertionError(); - } } } } @@ -1931,23 +1882,17 @@ final class Utf8 { */ private static class DecodeUtil { - /** - * Returns whether this is a single-byte codepoint (i.e., ASCII) with the form '0XXXXXXX'. - */ + /** Returns whether this is a single-byte codepoint (i.e., ASCII) with the form '0XXXXXXX'. */ private static boolean isOneByte(byte b) { return b >= 0; } - /** - * Returns whether this is a two-byte codepoint with the form '10XXXXXX'. - */ + /** Returns whether this is a two-byte codepoint with the form '10XXXXXX'. */ private static boolean isTwoBytes(byte b) { return b < (byte) 0xE0; } - /** - * Returns whether this is a three-byte codepoint with the form '110XXXXX'. - */ + /** Returns whether this is a three-byte codepoint with the form '110XXXXX'. */ private static boolean isThreeBytes(byte b) { return b < (byte) 0xF0; } @@ -1956,13 +1901,11 @@ final class Utf8 { resultArr[resultPos] = (char) byte1; } - private static void handleTwoBytes( - byte byte1, byte byte2, char[] resultArr, int resultPos) + private static void handleTwoBytes(byte byte1, byte byte2, char[] resultArr, int resultPos) throws InvalidProtocolBufferException { // Simultaneously checks for illegal trailing-byte in leading position (<= '11000000') and // overlong 2-byte, '11000001'. - if (byte1 < (byte) 0xC2 - || isNotTrailingByte(byte2)) { + if (byte1 < (byte) 0xC2 || isNotTrailingByte(byte2)) { throw InvalidProtocolBufferException.invalidUtf8(); } resultArr[resultPos] = (char) (((byte1 & 0x1F) << 6) | trailingByteValue(byte2)); @@ -1979,13 +1922,14 @@ final class Utf8 { || isNotTrailingByte(byte3)) { throw InvalidProtocolBufferException.invalidUtf8(); } - resultArr[resultPos] = (char) - (((byte1 & 0x0F) << 12) | (trailingByteValue(byte2) << 6) | trailingByteValue(byte3)); + resultArr[resultPos] = + (char) + (((byte1 & 0x0F) << 12) | (trailingByteValue(byte2) << 6) | trailingByteValue(byte3)); } private static void handleFourBytes( byte byte1, byte byte2, byte byte3, byte byte4, char[] resultArr, int resultPos) - throws InvalidProtocolBufferException{ + throws InvalidProtocolBufferException { if (isNotTrailingByte(byte2) // Check that 1 <= plane <= 16. Tricky optimized form of: // valid 4-byte leading byte? @@ -1999,31 +1943,28 @@ final class Utf8 { || isNotTrailingByte(byte4)) { throw InvalidProtocolBufferException.invalidUtf8(); } - int codepoint = ((byte1 & 0x07) << 18) - | (trailingByteValue(byte2) << 12) - | (trailingByteValue(byte3) << 6) - | trailingByteValue(byte4); + int codepoint = + ((byte1 & 0x07) << 18) + | (trailingByteValue(byte2) << 12) + | (trailingByteValue(byte3) << 6) + | trailingByteValue(byte4); resultArr[resultPos] = DecodeUtil.highSurrogate(codepoint); resultArr[resultPos + 1] = DecodeUtil.lowSurrogate(codepoint); } - /** - * Returns whether the byte is not a valid continuation of the form '10XXXXXX'. - */ + /** Returns whether the byte is not a valid continuation of the form '10XXXXXX'. */ private static boolean isNotTrailingByte(byte b) { return b > (byte) 0xBF; } - /** - * Returns the actual value of the trailing byte (removes the prefix '10') for composition. - */ + /** Returns the actual value of the trailing byte (removes the prefix '10') for composition. */ private static int trailingByteValue(byte b) { return b & 0x3F; } private static char highSurrogate(int codePoint) { - return (char) ((MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10)) - + (codePoint >>> 10)); + return (char) + ((MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10)) + (codePoint >>> 10)); } private static char lowSurrogate(int codePoint) { diff --git a/java/core/src/main/java/com/google/protobuf/WireFormat.java b/java/core/src/main/java/com/google/protobuf/WireFormat.java index 8b837ee5e1..3c6e8dda4d 100644 --- a/java/core/src/main/java/com/google/protobuf/WireFormat.java +++ b/java/core/src/main/java/com/google/protobuf/WireFormat.java @@ -33,13 +33,12 @@ package com.google.protobuf; import java.io.IOException; /** - * This class is used internally by the Protocol Buffer library and generated - * message implementations. It is public only because those generated messages - * do not reside in the {@code protobuf} package. Others should not use this - * class directly. + * This class is used internally by the Protocol Buffer library and generated message + * implementations. It is public only because those generated messages do not reside in the {@code + * protobuf} package. Others should not use this class directly. * - * This class contains constants and helper functions useful for dealing with - * the Protocol Buffer wire format. + *

This class contains constants and helper functions useful for dealing with the Protocol Buffer + * wire format. * * @author kenton@google.com Kenton Varda */ @@ -53,12 +52,12 @@ public final class WireFormat { static final int MAX_VARINT64_SIZE = 10; static final int MAX_VARINT_SIZE = 10; - public static final int WIRETYPE_VARINT = 0; - public static final int WIRETYPE_FIXED64 = 1; + public static final int WIRETYPE_VARINT = 0; + public static final int WIRETYPE_FIXED64 = 1; public static final int WIRETYPE_LENGTH_DELIMITED = 2; - public static final int WIRETYPE_START_GROUP = 3; - public static final int WIRETYPE_END_GROUP = 4; - public static final int WIRETYPE_FIXED32 = 5; + public static final int WIRETYPE_START_GROUP = 3; + public static final int WIRETYPE_END_GROUP = 4; + public static final int WIRETYPE_FIXED32 = 5; static final int TAG_TYPE_BITS = 3; static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1; @@ -79,8 +78,8 @@ public final class WireFormat { } /** - * Lite equivalent to {@link Descriptors.FieldDescriptor.JavaType}. This is - * only here to support the lite runtime and should not be used by users. + * Lite equivalent to {@link Descriptors.FieldDescriptor.JavaType}. This is only here to support + * the lite runtime and should not be used by users. */ public enum JavaType { INT(0), @@ -97,10 +96,7 @@ public final class WireFormat { this.defaultDefault = defaultDefault; } - /** - * The default default value for fields of this type, if it's a primitive - * type. - */ + /** The default default value for fields of this type, if it's a primitive type. */ Object getDefaultDefault() { return defaultDefault; } @@ -109,44 +105,48 @@ public final class WireFormat { } /** - * Lite equivalent to {@link Descriptors.FieldDescriptor.Type}. This is - * only here to support the lite runtime and should not be used by users. + * Lite equivalent to {@link Descriptors.FieldDescriptor.Type}. This is only here to support the + * lite runtime and should not be used by users. */ public enum FieldType { - DOUBLE (JavaType.DOUBLE , WIRETYPE_FIXED64 ), - FLOAT (JavaType.FLOAT , WIRETYPE_FIXED32 ), - INT64 (JavaType.LONG , WIRETYPE_VARINT ), - UINT64 (JavaType.LONG , WIRETYPE_VARINT ), - INT32 (JavaType.INT , WIRETYPE_VARINT ), - FIXED64 (JavaType.LONG , WIRETYPE_FIXED64 ), - FIXED32 (JavaType.INT , WIRETYPE_FIXED32 ), - BOOL (JavaType.BOOLEAN , WIRETYPE_VARINT ), - STRING (JavaType.STRING , WIRETYPE_LENGTH_DELIMITED) { + DOUBLE(JavaType.DOUBLE, WIRETYPE_FIXED64), + FLOAT(JavaType.FLOAT, WIRETYPE_FIXED32), + INT64(JavaType.LONG, WIRETYPE_VARINT), + UINT64(JavaType.LONG, WIRETYPE_VARINT), + INT32(JavaType.INT, WIRETYPE_VARINT), + FIXED64(JavaType.LONG, WIRETYPE_FIXED64), + FIXED32(JavaType.INT, WIRETYPE_FIXED32), + BOOL(JavaType.BOOLEAN, WIRETYPE_VARINT), + STRING(JavaType.STRING, WIRETYPE_LENGTH_DELIMITED) { @Override public boolean isPackable() { - return false; } + return false; + } }, - GROUP (JavaType.MESSAGE , WIRETYPE_START_GROUP ) { + GROUP(JavaType.MESSAGE, WIRETYPE_START_GROUP) { @Override public boolean isPackable() { - return false; } + return false; + } }, - MESSAGE (JavaType.MESSAGE , WIRETYPE_LENGTH_DELIMITED) { + MESSAGE(JavaType.MESSAGE, WIRETYPE_LENGTH_DELIMITED) { @Override public boolean isPackable() { - return false; } + return false; + } }, - BYTES (JavaType.BYTE_STRING, WIRETYPE_LENGTH_DELIMITED) { + BYTES(JavaType.BYTE_STRING, WIRETYPE_LENGTH_DELIMITED) { @Override public boolean isPackable() { - return false; } + return false; + } }, - UINT32 (JavaType.INT , WIRETYPE_VARINT ), - ENUM (JavaType.ENUM , WIRETYPE_VARINT ), - SFIXED32(JavaType.INT , WIRETYPE_FIXED32 ), - SFIXED64(JavaType.LONG , WIRETYPE_FIXED64 ), - SINT32 (JavaType.INT , WIRETYPE_VARINT ), - SINT64 (JavaType.LONG , WIRETYPE_VARINT ); + UINT32(JavaType.INT, WIRETYPE_VARINT), + ENUM(JavaType.ENUM, WIRETYPE_VARINT), + SFIXED32(JavaType.INT, WIRETYPE_FIXED32), + SFIXED64(JavaType.LONG, WIRETYPE_FIXED64), + SINT32(JavaType.INT, WIRETYPE_VARINT), + SINT64(JavaType.LONG, WIRETYPE_VARINT); FieldType(final JavaType javaType, final int wireType) { this.javaType = javaType; @@ -156,30 +156,34 @@ public final class WireFormat { private final JavaType javaType; private final int wireType; - public JavaType getJavaType() { return javaType; } - public int getWireType() { return wireType; } + public JavaType getJavaType() { + return javaType; + } - public boolean isPackable() { return true; } + public int getWireType() { + return wireType; + } + + public boolean isPackable() { + return true; + } } // Field numbers for fields in MessageSet wire format. - static final int MESSAGE_SET_ITEM = 1; + static final int MESSAGE_SET_ITEM = 1; static final int MESSAGE_SET_TYPE_ID = 2; static final int MESSAGE_SET_MESSAGE = 3; // Tag numbers. - static final int MESSAGE_SET_ITEM_TAG = - makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP); - static final int MESSAGE_SET_ITEM_END_TAG = - makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP); - static final int MESSAGE_SET_TYPE_ID_TAG = - makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT); + static final int MESSAGE_SET_ITEM_TAG = makeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP); + static final int MESSAGE_SET_ITEM_END_TAG = makeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP); + static final int MESSAGE_SET_TYPE_ID_TAG = makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT); static final int MESSAGE_SET_MESSAGE_TAG = - makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED); + makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED); /** - * Validation level for handling incoming string field data which potentially - * contain non-UTF8 bytes. + * Validation level for handling incoming string field data which potentially contain non-UTF8 + * bytes. */ enum Utf8Validation { /** Eagerly parses to String; silently accepts invalid UTF8 bytes. */ @@ -209,54 +213,59 @@ public final class WireFormat { } /** - * Read a field of any primitive type for immutable messages from a - * CodedInputStream. Enums, groups, and embedded messages are not handled by - * this method. + * Read a field of any primitive type for immutable messages from a CodedInputStream. Enums, + * groups, and embedded messages are not handled by this method. * * @param input The stream from which to read. * @param type Declared type of the field. - * @param utf8Validation Different string UTF8 validation level for handling - * string fields. - * @return An object representing the field's value, of the exact - * type which would be returned by - * {@link Message#getField(Descriptors.FieldDescriptor)} for - * this field. + * @param utf8Validation Different string UTF8 validation level for handling string fields. + * @return An object representing the field's value, of the exact type which would be returned by + * {@link Message#getField(Descriptors.FieldDescriptor)} for this field. */ static Object readPrimitiveField( - CodedInputStream input, - FieldType type, - Utf8Validation utf8Validation) throws IOException { + CodedInputStream input, FieldType type, Utf8Validation utf8Validation) throws IOException { switch (type) { - case DOUBLE : return input.readDouble (); - case FLOAT : return input.readFloat (); - case INT64 : return input.readInt64 (); - case UINT64 : return input.readUInt64 (); - case INT32 : return input.readInt32 (); - case FIXED64 : return input.readFixed64 (); - case FIXED32 : return input.readFixed32 (); - case BOOL : return input.readBool (); - case BYTES : return input.readBytes (); - case UINT32 : return input.readUInt32 (); - case SFIXED32: return input.readSFixed32(); - case SFIXED64: return input.readSFixed64(); - case SINT32 : return input.readSInt32 (); - case SINT64 : return input.readSInt64 (); + case DOUBLE: + return input.readDouble(); + case FLOAT: + return input.readFloat(); + case INT64: + return input.readInt64(); + case UINT64: + return input.readUInt64(); + case INT32: + return input.readInt32(); + case FIXED64: + return input.readFixed64(); + case FIXED32: + return input.readFixed32(); + case BOOL: + return input.readBool(); + case BYTES: + return input.readBytes(); + case UINT32: + return input.readUInt32(); + case SFIXED32: + return input.readSFixed32(); + case SFIXED64: + return input.readSFixed64(); + case SINT32: + return input.readSInt32(); + case SINT64: + return input.readSInt64(); - case STRING : return utf8Validation.readString(input); + case STRING: + return utf8Validation.readString(input); case GROUP: - throw new IllegalArgumentException( - "readPrimitiveField() cannot handle nested groups."); + throw new IllegalArgumentException("readPrimitiveField() cannot handle nested groups."); case MESSAGE: - throw new IllegalArgumentException( - "readPrimitiveField() cannot handle embedded messages."); + throw new IllegalArgumentException("readPrimitiveField() cannot handle embedded messages."); case ENUM: // We don't handle enums because we don't know what to do if the // value is not recognized. - throw new IllegalArgumentException( - "readPrimitiveField() cannot handle enums."); + throw new IllegalArgumentException("readPrimitiveField() cannot handle enums."); } - throw new RuntimeException( - "There is no way to get here, but the compiler thinks otherwise."); + throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise."); } } diff --git a/java/core/src/main/java/com/google/protobuf/Writer.java b/java/core/src/main/java/com/google/protobuf/Writer.java new file mode 100755 index 0000000000..3f95c325de --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/Writer.java @@ -0,0 +1,219 @@ +// 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. + +package com.google.protobuf; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** A writer that performs serialization of protobuf message fields. */ +@ExperimentalApi +interface Writer { + + /** The order in which the fields are written by a {@link Writer}. */ + enum FieldOrder { + /** Fields are written in ascending order by field number. */ + ASCENDING, + + /** Fields are written in descending order by field number. */ + DESCENDING + } + + /** Indicates the order in which the fields are written by this {@link Writer}. */ + FieldOrder fieldOrder(); + + /** Writes a field of type {@link FieldType#SFIXED32}. */ + void writeSFixed32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#INT64}. */ + void writeInt64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#SFIXED64}. */ + void writeSFixed64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#FLOAT}. */ + void writeFloat(int fieldNumber, float value) throws IOException; + + /** Writes a field of type {@link FieldType#DOUBLE}. */ + void writeDouble(int fieldNumber, double value) throws IOException; + + /** Writes a field of type {@link FieldType#ENUM}. */ + void writeEnum(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#UINT64}. */ + void writeUInt64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#INT32}. */ + void writeInt32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#FIXED64}. */ + void writeFixed64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#FIXED32}. */ + void writeFixed32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#BOOL}. */ + void writeBool(int fieldNumber, boolean value) throws IOException; + + /** Writes a field of type {@link FieldType#STRING}. */ + void writeString(int fieldNumber, String value) throws IOException; + + /** Writes a field of type {@link FieldType#BYTES}. */ + void writeBytes(int fieldNumber, ByteString value) throws IOException; + + /** Writes a field of type {@link FieldType#UINT32}. */ + void writeUInt32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#SINT32}. */ + void writeSInt32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#SINT64}. */ + void writeSInt64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#MESSAGE}. */ + void writeMessage(int fieldNumber, Object value) throws IOException; + + /** Writes a field of type {@link FieldType#MESSAGE}. */ + void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException; + + /** + * Writes a field of type {@link FieldType#GROUP}. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeGroup(int fieldNumber, Object value) throws IOException; + + /** + * Writes a field of type {@link FieldType#GROUP}. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException; + + /** + * Writes a single start group tag. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeStartGroup(int fieldNumber) throws IOException; + + /** + * Writes a single end group tag. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeEndGroup(int fieldNumber) throws IOException; + + /** Writes a list field of type {@link FieldType#INT32}. */ + void writeInt32List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#FIXED32}. */ + void writeFixed32List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#INT64}. */ + void writeInt64List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#UINT64}. */ + void writeUInt64List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#FIXED64}. */ + void writeFixed64List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#FLOAT}. */ + void writeFloatList(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#DOUBLE}. */ + void writeDoubleList(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#ENUM}. */ + void writeEnumList(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#BOOL}. */ + void writeBoolList(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#STRING}. */ + void writeStringList(int fieldNumber, List value) throws IOException; + + /** Writes a list field of type {@link FieldType#BYTES}. */ + void writeBytesList(int fieldNumber, List value) throws IOException; + + /** Writes a list field of type {@link FieldType#UINT32}. */ + void writeUInt32List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#SFIXED32}. */ + void writeSFixed32List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#SFIXED64}. */ + void writeSFixed64List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#SINT32}. */ + void writeSInt32List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#SINT64}. */ + void writeSInt64List(int fieldNumber, List value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#MESSAGE}. */ + void writeMessageList(int fieldNumber, List value) throws IOException; + + /** Writes a list field of type {@link FieldType#MESSAGE}. */ + void writeMessageList(int fieldNumber, List value, Schema schema) throws IOException; + + /** + * Writes a list field of type {@link FieldType#GROUP}. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeGroupList(int fieldNumber, List value) throws IOException; + + /** + * Writes a list field of type {@link FieldType#GROUP}. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeGroupList(int fieldNumber, List value, Schema schema) throws IOException; + + /** + * Writes a message field in {@code MessageSet} wire-format. + * + * @param value A message instance or an opaque {@link ByteString} for an unknown field. + */ + void writeMessageSetItem(int fieldNumber, Object value) throws IOException; + + /** Writes a map field. */ + void writeMap(int fieldNumber, MapEntryLite.Metadata metadata, Map map) + throws IOException; +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java b/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java index bb11bd0f96..cc979ac513 100644 --- a/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java @@ -53,10 +53,9 @@ import junit.framework.TestCase; */ public class AbstractMessageTest extends TestCase { /** - * Extends AbstractMessage and wraps some other message object. The methods - * of the Message interface which aren't explicitly implemented by - * AbstractMessage are forwarded to the wrapped object. This allows us to - * test that AbstractMessage's implementations work even if the wrapped + * Extends AbstractMessage and wraps some other message object. The methods of the Message + * interface which aren't explicitly implemented by AbstractMessage are forwarded to the wrapped + * object. This allows us to test that AbstractMessage's implementations work even if the wrapped * object does not use them. */ private static class AbstractMessageWrapper extends AbstractMessage { @@ -70,39 +69,47 @@ public class AbstractMessageTest extends TestCase { public Descriptors.Descriptor getDescriptorForType() { return wrappedMessage.getDescriptorForType(); } + @Override public AbstractMessageWrapper getDefaultInstanceForType() { - return new AbstractMessageWrapper( - wrappedMessage.getDefaultInstanceForType()); + return new AbstractMessageWrapper(wrappedMessage.getDefaultInstanceForType()); } + @Override public Map getAllFields() { return wrappedMessage.getAllFields(); } + @Override public boolean hasField(Descriptors.FieldDescriptor field) { return wrappedMessage.hasField(field); } + @Override public Object getField(Descriptors.FieldDescriptor field) { return wrappedMessage.getField(field); } + @Override public int getRepeatedFieldCount(Descriptors.FieldDescriptor field) { return wrappedMessage.getRepeatedFieldCount(field); } + @Override public Object getRepeatedField(Descriptors.FieldDescriptor field, int index) { return wrappedMessage.getRepeatedField(field, index); } + @Override public UnknownFieldSet getUnknownFields() { return wrappedMessage.getUnknownFields(); } + @Override public Builder newBuilderForType() { return new Builder(wrappedMessage.newBuilderForType()); } + @Override public Builder toBuilder() { return new Builder(wrappedMessage.toBuilder()); @@ -119,85 +126,103 @@ public class AbstractMessageTest extends TestCase { public AbstractMessageWrapper build() { return new AbstractMessageWrapper(wrappedBuilder.build()); } + @Override public AbstractMessageWrapper buildPartial() { return new AbstractMessageWrapper(wrappedBuilder.buildPartial()); } + @Override public Builder clone() { return new Builder(wrappedBuilder.clone()); } + @Override public boolean isInitialized() { return clone().buildPartial().isInitialized(); } + @Override public Descriptors.Descriptor getDescriptorForType() { return wrappedBuilder.getDescriptorForType(); } + @Override public AbstractMessageWrapper getDefaultInstanceForType() { - return new AbstractMessageWrapper( - wrappedBuilder.getDefaultInstanceForType()); + return new AbstractMessageWrapper(wrappedBuilder.getDefaultInstanceForType()); } + @Override public Map getAllFields() { return wrappedBuilder.getAllFields(); } + @Override public Builder newBuilderForField(Descriptors.FieldDescriptor field) { return new Builder(wrappedBuilder.newBuilderForField(field)); } + @Override public boolean hasField(Descriptors.FieldDescriptor field) { return wrappedBuilder.hasField(field); } + @Override public Object getField(Descriptors.FieldDescriptor field) { return wrappedBuilder.getField(field); } + @Override public Builder setField(Descriptors.FieldDescriptor field, Object value) { wrappedBuilder.setField(field, value); return this; } + @Override public Builder clearField(Descriptors.FieldDescriptor field) { wrappedBuilder.clearField(field); return this; } + @Override public int getRepeatedFieldCount(Descriptors.FieldDescriptor field) { return wrappedBuilder.getRepeatedFieldCount(field); } + @Override public Object getRepeatedField(Descriptors.FieldDescriptor field, int index) { return wrappedBuilder.getRepeatedField(field, index); } + @Override public Builder setRepeatedField(Descriptors.FieldDescriptor field, int index, Object value) { wrappedBuilder.setRepeatedField(field, index, value); return this; } + @Override public Builder addRepeatedField(Descriptors.FieldDescriptor field, Object value) { wrappedBuilder.addRepeatedField(field, value); return this; } + @Override public UnknownFieldSet getUnknownFields() { return wrappedBuilder.getUnknownFields(); } + @Override public Builder setUnknownFields(UnknownFieldSet unknownFields) { wrappedBuilder.setUnknownFields(unknownFields); return this; } + @Override public Message.Builder getFieldBuilder(FieldDescriptor field) { return wrappedBuilder.getFieldBuilder(field); } } + @Override public Parser getParserForType() { return wrappedMessage.getParserForType(); @@ -207,7 +232,7 @@ public class AbstractMessageTest extends TestCase { // ================================================================= TestUtil.ReflectionTester reflectionTester = - new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); + new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); TestUtil.ReflectionTester extensionsReflectionTester = new TestUtil.ReflectionTester( @@ -215,16 +240,17 @@ public class AbstractMessageTest extends TestCase { public void testClear() throws Exception { AbstractMessageWrapper message = - new AbstractMessageWrapper.Builder( - TestAllTypes.newBuilder(TestUtil.getAllSet())) - .clear().build(); + new AbstractMessageWrapper.Builder(TestAllTypes.newBuilder(TestUtil.getAllSet())) + .clear() + .build(); TestUtil.assertClear((TestAllTypes) message.wrappedMessage); } public void testCopy() throws Exception { AbstractMessageWrapper message = - new AbstractMessageWrapper.Builder(TestAllTypes.newBuilder()) - .mergeFrom(TestUtil.getAllSet()).build(); + new AbstractMessageWrapper.Builder(TestAllTypes.newBuilder()) + .mergeFrom(TestUtil.getAllSet()) + .build(); TestUtil.assertAllFieldsSet((TestAllTypes) message.wrappedMessage); } @@ -232,25 +258,21 @@ public class AbstractMessageTest extends TestCase { TestAllTypes message = TestUtil.getAllSet(); Message abstractMessage = new AbstractMessageWrapper(TestUtil.getAllSet()); - assertEquals(message.getSerializedSize(), - abstractMessage.getSerializedSize()); + assertEquals(message.getSerializedSize(), abstractMessage.getSerializedSize()); } public void testSerialization() throws Exception { Message abstractMessage = new AbstractMessageWrapper(TestUtil.getAllSet()); - TestUtil.assertAllFieldsSet( - TestAllTypes.parseFrom(abstractMessage.toByteString())); + TestUtil.assertAllFieldsSet(TestAllTypes.parseFrom(abstractMessage.toByteString())); - assertEquals(TestUtil.getAllSet().toByteString(), - abstractMessage.toByteString()); + assertEquals(TestUtil.getAllSet().toByteString(), abstractMessage.toByteString()); } public void testParsing() throws Exception { AbstractMessageWrapper.Builder builder = - new AbstractMessageWrapper.Builder(TestAllTypes.newBuilder()); - AbstractMessageWrapper message = - builder.mergeFrom(TestUtil.getAllSet().toByteString()).build(); + new AbstractMessageWrapper.Builder(TestAllTypes.newBuilder()); + AbstractMessageWrapper message = builder.mergeFrom(TestUtil.getAllSet().toByteString()).build(); TestUtil.assertAllFieldsSet((TestAllTypes) message.wrappedMessage); } @@ -270,8 +292,8 @@ public class AbstractMessageTest extends TestCase { } // test DynamicMessage directly. - Message.Builder dynamicMessageBuilder = DynamicMessage.newBuilder( - TestRequiredForeign.getDescriptor()); + Message.Builder dynamicMessageBuilder = + DynamicMessage.newBuilder(TestRequiredForeign.getDescriptor()); // mergeFrom() should not throw initialization error. dynamicMessageBuilder.mergeFrom(bytes).buildPartial(); try { @@ -283,65 +305,56 @@ public class AbstractMessageTest extends TestCase { } public void testPackedSerialization() throws Exception { - Message abstractMessage = - new AbstractMessageWrapper(TestUtil.getPackedSet()); + Message abstractMessage = new AbstractMessageWrapper(TestUtil.getPackedSet()); - TestUtil.assertPackedFieldsSet( - TestPackedTypes.parseFrom(abstractMessage.toByteString())); + TestUtil.assertPackedFieldsSet(TestPackedTypes.parseFrom(abstractMessage.toByteString())); - assertEquals(TestUtil.getPackedSet().toByteString(), - abstractMessage.toByteString()); + assertEquals(TestUtil.getPackedSet().toByteString(), abstractMessage.toByteString()); } public void testPackedParsing() throws Exception { AbstractMessageWrapper.Builder builder = - new AbstractMessageWrapper.Builder(TestPackedTypes.newBuilder()); + new AbstractMessageWrapper.Builder(TestPackedTypes.newBuilder()); AbstractMessageWrapper message = - builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build(); + builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build(); TestUtil.assertPackedFieldsSet((TestPackedTypes) message.wrappedMessage); } public void testUnpackedSerialization() throws Exception { - Message abstractMessage = - new AbstractMessageWrapper(TestUtil.getUnpackedSet()); + Message abstractMessage = new AbstractMessageWrapper(TestUtil.getUnpackedSet()); - TestUtil.assertUnpackedFieldsSet( - TestUnpackedTypes.parseFrom(abstractMessage.toByteString())); + TestUtil.assertUnpackedFieldsSet(TestUnpackedTypes.parseFrom(abstractMessage.toByteString())); - assertEquals(TestUtil.getUnpackedSet().toByteString(), - abstractMessage.toByteString()); + assertEquals(TestUtil.getUnpackedSet().toByteString(), abstractMessage.toByteString()); } public void testParsePackedToUnpacked() throws Exception { AbstractMessageWrapper.Builder builder = - new AbstractMessageWrapper.Builder(TestUnpackedTypes.newBuilder()); + new AbstractMessageWrapper.Builder(TestUnpackedTypes.newBuilder()); AbstractMessageWrapper message = - builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build(); - TestUtil.assertUnpackedFieldsSet( - (TestUnpackedTypes) message.wrappedMessage); + builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build(); + TestUtil.assertUnpackedFieldsSet((TestUnpackedTypes) message.wrappedMessage); } public void testParseUnpackedToPacked() throws Exception { AbstractMessageWrapper.Builder builder = - new AbstractMessageWrapper.Builder(TestPackedTypes.newBuilder()); + new AbstractMessageWrapper.Builder(TestPackedTypes.newBuilder()); AbstractMessageWrapper message = - builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build(); + builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build(); TestUtil.assertPackedFieldsSet((TestPackedTypes) message.wrappedMessage); } public void testUnpackedParsing() throws Exception { AbstractMessageWrapper.Builder builder = - new AbstractMessageWrapper.Builder(TestUnpackedTypes.newBuilder()); + new AbstractMessageWrapper.Builder(TestUnpackedTypes.newBuilder()); AbstractMessageWrapper message = - builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build(); - TestUtil.assertUnpackedFieldsSet( - (TestUnpackedTypes) message.wrappedMessage); + builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build(); + TestUtil.assertUnpackedFieldsSet((TestUnpackedTypes) message.wrappedMessage); } public void testOptimizedForSize() throws Exception { // We're mostly only checking that this class was compiled successfully. - TestOptimizedForSize message = - TestOptimizedForSize.newBuilder().setI(1).build(); + TestOptimizedForSize message = TestOptimizedForSize.newBuilder().setI(1).build(); message = TestOptimizedForSize.parseFrom(message.toByteString()); assertEquals(2, message.getSerializedSize()); } @@ -351,8 +364,7 @@ public class AbstractMessageTest extends TestCase { public void testIsInitialized() throws Exception { TestRequired.Builder builder = TestRequired.newBuilder(); - AbstractMessageWrapper.Builder abstractBuilder = - new AbstractMessageWrapper.Builder(builder); + AbstractMessageWrapper.Builder abstractBuilder = new AbstractMessageWrapper.Builder(builder); assertFalse(abstractBuilder.isInitialized()); assertEquals("a, b, c", abstractBuilder.getInitializationErrorString()); @@ -369,8 +381,7 @@ public class AbstractMessageTest extends TestCase { public void testForeignIsInitialized() throws Exception { TestRequiredForeign.Builder builder = TestRequiredForeign.newBuilder(); - AbstractMessageWrapper.Builder abstractBuilder = - new AbstractMessageWrapper.Builder(builder); + AbstractMessageWrapper.Builder abstractBuilder = new AbstractMessageWrapper.Builder(builder); assertTrue(abstractBuilder.isInitialized()); assertEquals("", abstractBuilder.getInitializationErrorString()); @@ -378,8 +389,7 @@ public class AbstractMessageTest extends TestCase { builder.setOptionalMessage(TEST_REQUIRED_UNINITIALIZED); assertFalse(abstractBuilder.isInitialized()); assertEquals( - "optional_message.b, optional_message.c", - abstractBuilder.getInitializationErrorString()); + "optional_message.b, optional_message.c", abstractBuilder.getInitializationErrorString()); builder.setOptionalMessage(TEST_REQUIRED_INITIALIZED); assertTrue(abstractBuilder.isInitialized()); @@ -400,36 +410,37 @@ public class AbstractMessageTest extends TestCase { // Tests for mergeFrom static final TestAllTypes MERGE_SOURCE = - TestAllTypes.newBuilder() - .setOptionalInt32(1) - .setOptionalString("foo") - .setOptionalForeignMessage(ForeignMessage.getDefaultInstance()) - .addRepeatedString("bar") - .build(); + TestAllTypes.newBuilder() + .setOptionalInt32(1) + .setOptionalString("foo") + .setOptionalForeignMessage(ForeignMessage.getDefaultInstance()) + .addRepeatedString("bar") + .build(); static final TestAllTypes MERGE_DEST = - TestAllTypes.newBuilder() - .setOptionalInt64(2) - .setOptionalString("baz") - .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(3).build()) - .addRepeatedString("qux") - .build(); + TestAllTypes.newBuilder() + .setOptionalInt64(2) + .setOptionalString("baz") + .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(3).build()) + .addRepeatedString("qux") + .build(); static final String MERGE_RESULT_TEXT = - "optional_int32: 1\n" + - "optional_int64: 2\n" + - "optional_string: \"foo\"\n" + - "optional_foreign_message {\n" + - " c: 3\n" + - "}\n" + - "repeated_string: \"qux\"\n" + - "repeated_string: \"bar\"\n"; + "" + + "optional_int32: 1\n" + + "optional_int64: 2\n" + + "optional_string: \"foo\"\n" + + "optional_foreign_message {\n" + + " c: 3\n" + + "}\n" + + "repeated_string: \"qux\"\n" + + "repeated_string: \"bar\"\n"; public void testMergeFrom() throws Exception { AbstractMessageWrapper result = - new AbstractMessageWrapper.Builder( - TestAllTypes.newBuilder(MERGE_DEST)) - .mergeFrom(MERGE_SOURCE).build(); + new AbstractMessageWrapper.Builder(TestAllTypes.newBuilder(MERGE_DEST)) + .mergeFrom(MERGE_SOURCE) + .build(); assertEquals(MERGE_RESULT_TEXT, result.toString()); } @@ -443,8 +454,10 @@ public class AbstractMessageTest extends TestCase { TestAllTypes c = TestAllTypes.newBuilder(b).addRepeatedString("x").build(); TestAllTypes d = TestAllTypes.newBuilder(c).addRepeatedString("y").build(); TestAllExtensions e = TestUtil.getAllExtensionsSet(); - TestAllExtensions f = TestAllExtensions.newBuilder(e) - .addExtension(UnittestProto.repeatedInt32Extension, 999).build(); + TestAllExtensions f = + TestAllExtensions.newBuilder(e) + .addExtension(UnittestProto.repeatedInt32Extension, 999) + .build(); checkEqualsIsConsistent(a); checkEqualsIsConsistent(b); @@ -489,9 +502,7 @@ public class AbstractMessageTest extends TestCase { checkEqualsIsConsistent(eUnknownFields, eUnknownFields2); } - /** - * Asserts that the given proto has symmetric equals and hashCode methods. - */ + /** Asserts that the given proto has symmetric equals and hashCode methods. */ private void checkEqualsIsConsistent(Message message) { // Object should be equal to itself. assertEquals(message, message); @@ -501,9 +512,7 @@ public class AbstractMessageTest extends TestCase { checkEqualsIsConsistent(message, dynamic); } - /** - * Asserts that the given protos are equal and have the same hash code. - */ + /** Asserts that the given protos are equal and have the same hash code. */ private void checkEqualsIsConsistent(Message message1, Message message2) { assertEquals(message1, message2); assertEquals(message2, message1); @@ -513,9 +522,8 @@ public class AbstractMessageTest extends TestCase { /** * Asserts that the given protos are not equal and have different hash codes. * - * @warning It's valid for non-equal objects to have the same hash code, so - * this test is stricter than it needs to be. However, this should happen - * relatively rarely. + *

Note: It's valid for non-equal objects to have the same hash code, so this test is + * stricter than it needs to be. However, this should happen relatively rarely. */ private void checkNotEqual(Message m1, Message m2) { String equalsError = String.format("%s should not be equal to %s", m1, m2); @@ -535,7 +543,7 @@ public class AbstractMessageTest extends TestCase { public void testCheckByteStringIsUtf8OnNonUtf8() { ByteString byteString = - ByteString.copyFrom(new byte[]{(byte) 0x80}); // A lone continuation byte. + ByteString.copyFrom(new byte[] {(byte) 0x80}); // A lone continuation byte. try { AbstractMessageLite.checkByteStringIsUtf8(byteString); fail("Expected AbstractMessageLite.checkByteStringIsUtf8 to throw IllegalArgumentException"); @@ -543,5 +551,4 @@ public class AbstractMessageTest extends TestCase { assertEquals("Byte string is not UTF-8.", exception.getMessage()); } } - } diff --git a/java/core/src/test/java/com/google/protobuf/AbstractProto2LiteSchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractProto2LiteSchemaTest.java new file mode 100755 index 0000000000..5d3fd3bd69 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractProto2LiteSchemaTest.java @@ -0,0 +1,199 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import com.google.protobuf.testing.Proto2TestingLite.Proto2EmptyLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.TestEnum; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.List; +import org.junit.Test; + +/** Base class for tests using {@link Proto2MessageLite}. */ +public abstract class AbstractProto2LiteSchemaTest extends AbstractSchemaTest { + + @Override + protected Proto2MessageLiteFactory messageFactory() { + return new Proto2MessageLiteFactory(10, 20, 2, 2); + } + + @Test + public void mergeOptionalMessageFields() throws Exception { + Proto2MessageLite message1 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build()) + .build(); + Proto2MessageLite message2 = + newBuilder() + .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build()) + .build(); + Proto2MessageLite message3 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build()) + .build(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + message1.writeTo(output); + message2.writeTo(output); + message3.writeTo(output); + byte[] data = output.toByteArray(); + + Proto2MessageLite merged = + ExperimentalSerializationUtil.fromByteArray(data, Proto2MessageLite.class); + assertEquals(789, merged.getFieldMessage10().getFieldInt643()); + assertEquals(456, merged.getFieldMessage10().getFieldInt325()); + } + + @Test + public void oneofFieldsShouldRoundtrip() throws IOException { + roundtrip("Field 53", newBuilder().setFieldDouble53(100).build()); + roundtrip("Field 54", newBuilder().setFieldFloat54(100).build()); + roundtrip("Field 55", newBuilder().setFieldInt6455(100).build()); + roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build()); + roundtrip("Field 57", newBuilder().setFieldInt3257(100).build()); + roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build()); + roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build()); + roundtrip("Field 60", newBuilder().setFieldBool60(true).build()); + roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build()); + roundtrip( + "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build()); + roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build()); + roundtrip("Field 64", newBuilder().setFieldUint3264(100).build()); + roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build()); + roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build()); + roundtrip("Field 67", newBuilder().setFieldSint3267(100).build()); + roundtrip("Field 68", newBuilder().setFieldSint6468(100).build()); + roundtrip( + "Field 69", + newBuilder() + .setFieldGroup69( + Proto2MessageLite.FieldGroup69.newBuilder().setFieldInt3270(data().getInt())) + .build()); + } + + private Proto2MessageLite.Builder newBuilder() { + return messageFactory().newMessage().toBuilder(); + } + + @Override + protected List newMessagesMissingRequiredFields() { + return messageFactory().newMessagesMissingRequiredFields(); + } + + @Test + public void mapsShouldRoundtrip() throws IOException { + roundtrip( + "Proto2MessageLiteWithMaps", + new Proto2MessageLiteFactory(2, 10, 2, 2).newMessageWithMaps(), + Protobuf.getInstance().schemaFor(Proto2MessageLiteWithMaps.class)); + } + + @Test + public void unknownFieldsUnrecognized() throws Exception { + Proto2MessageLite expectedMessage = messageFactory().newMessage(); + byte[] serializedBytes = expectedMessage.toByteArray(); + Proto2EmptyLite empty = + ExperimentalSerializationUtil.fromByteArray(serializedBytes, Proto2EmptyLite.class); + + // Merge serialized bytes into an empty message, then reserialize and merge it to a new + // Proto2Message. Make sure the two messages equal. + byte[] roundtripBytes = ExperimentalSerializationUtil.toByteArray(empty); + Proto2MessageLite roundtripMessage = + ExperimentalSerializationUtil.fromByteArray(roundtripBytes, Proto2MessageLite.class); + assertEquals(expectedMessage, roundtripMessage); + } + + @Test + public void unknownEnum() throws IOException { + // Use unknown fields to hold invalid enum values. + UnknownFieldSetLite unknowns = UnknownFieldSetLite.newInstance(); + final int outOfRange = 1000; + assertNull(TestEnum.forNumber(outOfRange)); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_13_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.ONE_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.TWO_VALUE); + + { + // Construct a packed enum list. + int packedSize = + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE) + + CodedOutputStream.computeUInt32SizeNoTag(outOfRange) + + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE); + ByteString.CodedBuilder packedBuilder = ByteString.newCodedBuilder(packedSize); + CodedOutputStream packedOut = packedBuilder.getCodedOutput(); + packedOut.writeEnumNoTag(TestEnum.ONE_VALUE); + packedOut.writeEnumNoTag(outOfRange); + packedOut.writeEnumNoTag(TestEnum.TWO_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER, + WireFormat.WIRETYPE_LENGTH_DELIMITED), + packedBuilder.build()); + } + int size = unknowns.getSerializedSize(); + byte[] output = new byte[size]; + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + unknowns.writeTo(codedOutput); + codedOutput.flush(); + + Proto2MessageLite parsed = + ExperimentalSerializationUtil.fromByteArray(output, Proto2MessageLite.class); + assertFalse("out-of-range singular enum should not be in message", parsed.hasFieldEnum13()); + assertEquals( + "out-of-range repeated enum should not be in message", 2, parsed.getFieldEnumList30Count()); + assertEquals(TestEnum.ONE, parsed.getFieldEnumList30(0)); + assertEquals(TestEnum.TWO, parsed.getFieldEnumList30(1)); + assertEquals( + "out-of-range packed repeated enum should not be in message", + 2, + parsed.getFieldEnumListPacked44Count()); + assertEquals(TestEnum.ONE, parsed.getFieldEnumListPacked44(0)); + assertEquals(TestEnum.TWO, parsed.getFieldEnumListPacked44(1)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java new file mode 100755 index 0000000000..0c16818666 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java @@ -0,0 +1,224 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import com.google.protobuf.testing.Proto2Testing.Proto2Empty; +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.TestEnum; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.List; +import org.junit.Test; + +/** Base class for tests using {@link Proto2Message}. */ +public abstract class AbstractProto2SchemaTest extends AbstractSchemaTest { + + @Override + protected Proto2MessageFactory messageFactory() { + return new Proto2MessageFactory(10, 20, 2, 2); + } + + @Test + public void mergeOptionalMessageFields() throws Exception { + Proto2Message message1 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build()) + .build(); + Proto2Message message2 = + newBuilder() + .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build()) + .build(); + Proto2Message message3 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build()) + .build(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + message1.writeTo(output); + message2.writeTo(output); + message3.writeTo(output); + byte[] data = output.toByteArray(); + + Proto2Message merged = ExperimentalSerializationUtil.fromByteArray(data, Proto2Message.class); + assertEquals(789, merged.getFieldMessage10().getFieldInt643()); + assertEquals(456, merged.getFieldMessage10().getFieldInt325()); + } + + @Test + public void oneofFieldsShouldRoundtrip() throws IOException { + roundtrip("Field 53", newBuilder().setFieldDouble53(100).build()); + roundtrip("Field 54", newBuilder().setFieldFloat54(100).build()); + roundtrip("Field 55", newBuilder().setFieldInt6455(100).build()); + roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build()); + roundtrip("Field 57", newBuilder().setFieldInt3257(100).build()); + roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build()); + roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build()); + roundtrip("Field 60", newBuilder().setFieldBool60(true).build()); + roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build()); + roundtrip( + "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build()); + roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build()); + roundtrip("Field 64", newBuilder().setFieldUint3264(100).build()); + roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build()); + roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build()); + roundtrip("Field 67", newBuilder().setFieldSint3267(100).build()); + roundtrip("Field 68", newBuilder().setFieldSint6468(100).build()); + roundtrip( + "Field 69", + newBuilder() + .setFieldGroup69( + Proto2Message.FieldGroup69.newBuilder().setFieldInt3270(data().getInt())) + .build()); + } + + private Proto2Message.Builder newBuilder() { + return messageFactory().newMessage().toBuilder(); + } + + @Test + public void mapsShouldRoundtrip() throws IOException { + roundtrip( + "Proto2MessageWithMaps", + new Proto2MessageFactory(2, 10, 2, 2).newMessageWithMaps(), + Protobuf.getInstance().schemaFor(Proto2MessageWithMaps.class)); + } + + @Test + public void unknownFieldsUnrecognized() throws Exception { + Proto2Message expectedMessage = messageFactory().newMessage(); + byte[] serializedBytes = expectedMessage.toByteArray(); + Proto2Empty empty = + ExperimentalSerializationUtil.fromByteArray(serializedBytes, Proto2Empty.class); + + // Merge serialized bytes into an empty message, then reserialize and merge it to a new + // Proto2Message. Make sure the two messages equal. + byte[] roundtripBytes = ExperimentalSerializationUtil.toByteArray(empty); + assertEquals(serializedBytes.length, roundtripBytes.length); + Proto2Message roundtripMessage = + ExperimentalSerializationUtil.fromByteArray(roundtripBytes, Proto2Message.class); + assertEquals(expectedMessage, roundtripMessage); + } + + @Test + public void unknownEnum() throws IOException { + // Use unknown fields to hold invalid enum values. + UnknownFieldSetLite unknowns = UnknownFieldSetLite.newInstance(); + final int outOfRange = 1000; + assertNull(TestEnum.forNumber(outOfRange)); + unknowns.storeField( + WireFormat.makeTag(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.ONE_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.TWO_VALUE); + + { + // Construct a packed enum list. + int packedSize = + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE) + + CodedOutputStream.computeUInt32SizeNoTag(outOfRange) + + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE); + ByteString.CodedBuilder packedBuilder = ByteString.newCodedBuilder(packedSize); + CodedOutputStream packedOut = packedBuilder.getCodedOutput(); + packedOut.writeEnumNoTag(TestEnum.ONE_VALUE); + packedOut.writeEnumNoTag(outOfRange); + packedOut.writeEnumNoTag(TestEnum.TWO_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER, + WireFormat.WIRETYPE_LENGTH_DELIMITED), + packedBuilder.build()); + } + int size = unknowns.getSerializedSize(); + byte[] output = new byte[size]; + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + unknowns.writeTo(codedOutput); + codedOutput.flush(); + + Proto2Message parsed = ExperimentalSerializationUtil.fromByteArray(output, Proto2Message.class); + assertFalse("out-of-range singular enum should not be in message", parsed.hasFieldEnum13()); + { + List singularEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, singularEnum.size()); + assertEquals((Long) (long) outOfRange, singularEnum.get(0)); + } + { + List repeatedEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, repeatedEnum.size()); + assertEquals((Long) (long) outOfRange, repeatedEnum.get(0)); + } + { + List packedRepeatedEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, packedRepeatedEnum.size()); + assertEquals((Long) (long) outOfRange, packedRepeatedEnum.get(0)); + } + assertEquals( + "out-of-range repeated enum should not be in message", 2, parsed.getFieldEnumList30Count()); + assertEquals(TestEnum.ONE, parsed.getFieldEnumList30(0)); + assertEquals(TestEnum.TWO, parsed.getFieldEnumList30(1)); + assertEquals( + "out-of-range packed repeated enum should not be in message", + 2, + parsed.getFieldEnumListPacked44Count()); + assertEquals(TestEnum.ONE, parsed.getFieldEnumListPacked44(0)); + assertEquals(TestEnum.TWO, parsed.getFieldEnumListPacked44(1)); + } + + @Override + protected List newMessagesMissingRequiredFields() { + return messageFactory().newMessagesMissingRequiredFields(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractProto3LiteSchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractProto3LiteSchemaTest.java new file mode 100755 index 0000000000..9cc04ec7ce --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractProto3LiteSchemaTest.java @@ -0,0 +1,143 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; + +/** Base class for tests using {@link Proto3MessageLite}. */ +public abstract class AbstractProto3LiteSchemaTest extends AbstractSchemaTest { + @Override + protected Proto3MessageLiteFactory messageFactory() { + return new Proto3MessageLiteFactory(10, 20, 2, 2); + } + + @Override + protected List serializedBytesWithInvalidUtf8() throws IOException { + List invalidBytes = new ArrayList<>(); + byte[] invalid = new byte[] {(byte) 0x80}; + { + ByteBuffer buffer = ByteBuffer.allocate(100); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(buffer); + codedOutput.writeByteArray(Proto3MessageLite.FIELD_STRING_9_FIELD_NUMBER, invalid); + codedOutput.flush(); + buffer.flip(); + invalidBytes.add(buffer); + } + { + ByteBuffer buffer = ByteBuffer.allocate(100); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(buffer); + codedOutput.writeByteArray(Proto3MessageLite.FIELD_STRING_LIST_26_FIELD_NUMBER, invalid); + codedOutput.flush(); + buffer.flip(); + invalidBytes.add(buffer); + } + return invalidBytes; + } + + @Test + public void mergeOptionalMessageFields() throws Exception { + Proto3MessageLite message1 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build()) + .build(); + Proto3MessageLite message2 = + newBuilder() + .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build()) + .build(); + Proto3MessageLite message3 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build()) + .build(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + message1.writeTo(output); + message2.writeTo(output); + message3.writeTo(output); + byte[] data = output.toByteArray(); + + Proto3MessageLite merged = + ExperimentalSerializationUtil.fromByteArray(data, Proto3MessageLite.class); + assertEquals(789, merged.getFieldMessage10().getFieldInt643()); + assertEquals(456, merged.getFieldMessage10().getFieldInt325()); + } + + @Test + public void oneofFieldsShouldRoundtrip() throws IOException { + roundtrip("Field 53", newBuilder().setFieldDouble53(100).build()); + roundtrip("Field 54", newBuilder().setFieldFloat54(100).build()); + roundtrip("Field 55", newBuilder().setFieldInt6455(100).build()); + roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build()); + roundtrip("Field 57", newBuilder().setFieldInt3257(100).build()); + roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build()); + roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build()); + roundtrip("Field 60", newBuilder().setFieldBool60(true).build()); + roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build()); + roundtrip( + "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build()); + roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build()); + roundtrip("Field 64", newBuilder().setFieldUint3264(100).build()); + roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build()); + roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build()); + roundtrip("Field 67", newBuilder().setFieldSint3267(100).build()); + roundtrip("Field 68", newBuilder().setFieldSint6468(100).build()); + } + + @Test + public void retainUnknownFields() { + // Unknown fields are retained in lite runtime. + Proto3MessageLite expectedMessage = messageFactory().newMessage(); + Proto3EmptyLite empty = + ExperimentalSerializationUtil.fromByteArray( + expectedMessage.toByteArray(), Proto3EmptyLite.class); + assertEquals(expectedMessage.getSerializedSize(), empty.getSerializedSize()); + } + + @Test + public void mapsShouldRoundtrip() throws IOException { + roundtrip( + "Proto3MessageLiteWithMaps", + new Proto3MessageLiteFactory(2, 10, 2, 2).newMessageWithMaps(), + Protobuf.getInstance().schemaFor(Proto3MessageLiteWithMaps.class)); + } + + private static Proto3MessageLite.Builder newBuilder() { + return Proto3MessageLite.newBuilder(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractProto3SchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractProto3SchemaTest.java new file mode 100755 index 0000000000..358f1e3b14 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractProto3SchemaTest.java @@ -0,0 +1,151 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.testing.Proto3Testing.Proto3Empty; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; + +/** Base class for tests using {@link Proto3Message}. */ +public abstract class AbstractProto3SchemaTest extends AbstractSchemaTest { + @Override + protected Proto3MessageFactory messageFactory() { + return new Proto3MessageFactory(10, 20, 2, 2); + } + + @Override + protected List serializedBytesWithInvalidUtf8() throws IOException { + List invalidBytes = new ArrayList<>(); + byte[] invalid = new byte[] {(byte) 0x80}; + { + ByteBuffer buffer = ByteBuffer.allocate(100); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(buffer); + codedOutput.writeByteArray(Proto3Message.FIELD_STRING_9_FIELD_NUMBER, invalid); + codedOutput.flush(); + buffer.flip(); + invalidBytes.add(buffer); + } + { + ByteBuffer buffer = ByteBuffer.allocate(100); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(buffer); + codedOutput.writeByteArray(Proto3Message.FIELD_STRING_LIST_26_FIELD_NUMBER, invalid); + codedOutput.flush(); + buffer.flip(); + invalidBytes.add(buffer); + } + return invalidBytes; + } + + @Test + public void mergeOptionalMessageFields() throws Exception { + Proto3Message message1 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build()) + .build(); + Proto3Message message2 = + newBuilder() + .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build()) + .build(); + Proto3Message message3 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build()) + .build(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + message1.writeTo(output); + message2.writeTo(output); + message3.writeTo(output); + byte[] data = output.toByteArray(); + + Proto3Message merged = ExperimentalSerializationUtil.fromByteArray(data, Proto3Message.class); + assertEquals(789, merged.getFieldMessage10().getFieldInt643()); + assertEquals(456, merged.getFieldMessage10().getFieldInt325()); + } + + @Test + public void oneofFieldsShouldRoundtrip() throws IOException { + roundtrip("Field 53", newBuilder().setFieldDouble53(100).build()); + roundtrip("Field 54", newBuilder().setFieldFloat54(100).build()); + roundtrip("Field 55", newBuilder().setFieldInt6455(100).build()); + roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build()); + roundtrip("Field 57", newBuilder().setFieldInt3257(100).build()); + roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build()); + roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build()); + roundtrip("Field 60", newBuilder().setFieldBool60(true).build()); + roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build()); + roundtrip( + "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build()); + roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build()); + roundtrip("Field 64", newBuilder().setFieldUint3264(100).build()); + roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build()); + roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build()); + roundtrip("Field 67", newBuilder().setFieldSint3267(100).build()); + roundtrip("Field 68", newBuilder().setFieldSint6468(100).build()); + } + + @Test + public void preserveUnknownFields() { + Proto3Message expectedMessage = messageFactory().newMessage(); + Proto3Empty empty = + ExperimentalSerializationUtil.fromByteArray( + expectedMessage.toByteArray(), Proto3Empty.class); + assertEquals(expectedMessage.getSerializedSize(), empty.getSerializedSize()); + assertEquals(expectedMessage.toByteString(), empty.toByteString()); + } + + @Test + public void preserveUnknownFieldsProto2() { + // Make sure we will be able to preserve valid proto2 wireformat, including those that are not + // supported in proto3, e.g. groups. + byte[] payload = new Proto2MessageFactory(10, 20, 2, 2).newMessage().toByteArray(); + Proto3Empty empty = ExperimentalSerializationUtil.fromByteArray(payload, Proto3Empty.class); + assertEquals(payload.length, empty.getSerializedSize()); + } + + @Test + public void mapsShouldRoundtrip() throws IOException { + roundtrip( + "Proto3MessageWithMaps", + new Proto3MessageFactory(2, 10, 2, 2).newMessageWithMaps(), + Protobuf.getInstance().schemaFor(Proto3MessageWithMaps.class)); + } + + private static Proto3Message.Builder newBuilder() { + return Proto3Message.newBuilder(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractSchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractSchemaTest.java new file mode 100755 index 0000000000..c69a4fd254 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractSchemaTest.java @@ -0,0 +1,157 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.Before; +import org.junit.Test; + +public abstract class AbstractSchemaTest { + private Schema schema; + + @Before + public void setup() { + schema = schema(); + registerSchemas(); + } + + // Subclass should override this method if it needs to register more than one schemas. + protected void registerSchemas() { + // Register this schema with the runtime to support processing of nested messages. + Protobuf.getInstance().registerSchemaOverride(schema.newInstance().getClass(), schema); + } + + protected abstract Schema schema(); + + protected abstract ExperimentalMessageFactory messageFactory(); + + @SuppressWarnings("unused") + protected List serializedBytesWithInvalidUtf8() throws IOException { + return Collections.emptyList(); + } + + @Test + public void randomMessageShouldRoundtrip() throws IOException { + roundtrip("", messageFactory().newMessage()); + } + + @Test + public void invalidUtf8StringParsing() throws IOException { + for (ByteBuffer invalidUtf8Bytes : serializedBytesWithInvalidUtf8()) { + Reader reader = BinaryReader.newInstance(invalidUtf8Bytes, /* bufferIsImmutable= */ true); + + T newMsg = schema.newInstance(); + try { + schema.mergeFrom(newMsg, reader, ExtensionRegistryLite.getEmptyRegistry()); + fail("should throw invalid "); + } catch (InvalidProtocolBufferException expected) { + } + } + } + + @Test + public void mergeFromByteArrayFastPathMayThrowIndexOutOfBoundsException() throws IOException { + if (!Android.isOnAndroidDevice()) { + // Skip this test if not on Android. + return; + } + byte[] data = messageFactory().newMessage().toByteArray(); + int exceptionCount = 0; + for (int i = 0; i <= data.length; i++) { + byte[] truncatedData = Arrays.copyOf(data, i); + try { + T message = schema.newInstance(); + // Test that this method throws the expected exceptions. + schema.mergeFrom(message, truncatedData, 0, i, new ArrayDecoders.Registers()); + } catch (InvalidProtocolBufferException e) { + // Ignore expected exceptions. + } catch (IndexOutOfBoundsException e) { + exceptionCount += 1; + } + } + assertNotEquals(0, exceptionCount); + } + + protected static final void roundtrip( + String failureMessage, M msg, Schema schema) throws IOException { + byte[] serializedBytes = ExperimentalSerializationUtil.toByteArray(msg, schema); + assertEquals(failureMessage, msg.getSerializedSize(), serializedBytes.length); + + // Now read it back in and verify it matches the original. + if (Android.isOnAndroidDevice()) { + // Test the fast path on Android. + M newMsg = schema.newInstance(); + schema.mergeFrom( + newMsg, serializedBytes, 0, serializedBytes.length, new ArrayDecoders.Registers()); + schema.makeImmutable(newMsg); + assertEquals(failureMessage, msg, newMsg); + } + M newMsg = schema.newInstance(); + Reader reader = BinaryReader.newInstance(ByteBuffer.wrap(serializedBytes), true); + schema.mergeFrom(newMsg, reader, ExtensionRegistryLite.getEmptyRegistry()); + schema.makeImmutable(newMsg); + + assertEquals(failureMessage, msg, newMsg); + } + + protected final void roundtrip(String failureMessage, T msg) throws IOException { + roundtrip(failureMessage, msg, schema); + } + + protected final ExperimentalTestDataProvider data() { + return messageFactory().dataProvider(); + } + + protected List newMessagesMissingRequiredFields() { + return Collections.emptyList(); + } + + @SuppressWarnings("unchecked") + @Test + public void testRequiredFields() throws Exception { + for (T msg : newMessagesMissingRequiredFields()) { + if (schema.isInitialized(msg)) { + assertEquals("", msg.toString()); + msg = (T) msg.toBuilder().build(); + } + assertFalse(schema.isInitialized(msg)); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AnyTest.java b/java/core/src/test/java/com/google/protobuf/AnyTest.java index cf91ed91c6..d660ca7372 100644 --- a/java/core/src/test/java/com/google/protobuf/AnyTest.java +++ b/java/core/src/test/java/com/google/protobuf/AnyTest.java @@ -32,20 +32,17 @@ package com.google.protobuf; import any_test.AnyTestProto.TestAny; import protobuf_unittest.UnittestProto.TestAllTypes; - +import java.util.Objects; import junit.framework.TestCase; -/** - * Unit tests for Any message. - */ +/** Unit tests for Any message. */ public class AnyTest extends TestCase { public void testAnyGeneratedApi() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); TestUtil.setAllFields(builder); TestAllTypes message = builder.build(); - TestAny container = TestAny.newBuilder() - .setValue(Any.pack(message)).build(); + TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build(); assertTrue(container.getValue().is(TestAllTypes.class)); assertFalse(container.getValue().is(TestAny.class)); @@ -56,7 +53,7 @@ public class AnyTest extends TestCase { // Unpacking to a wrong type will throw an exception. try { - TestAny wrongMessage = container.getValue().unpack(TestAny.class); + container.getValue().unpack(TestAny.class); fail("Exception is expected."); } catch (InvalidProtocolBufferException e) { // expected. @@ -64,11 +61,10 @@ public class AnyTest extends TestCase { // Test that unpacking throws an exception if parsing fails. TestAny.Builder containerBuilder = container.toBuilder(); - containerBuilder.getValueBuilder().setValue( - ByteString.copyFrom(new byte[]{0x11})); + containerBuilder.getValueBuilder().setValue(ByteString.copyFrom(new byte[] {0x11})); container = containerBuilder.build(); try { - TestAllTypes parsingFailed = container.getValue().unpack(TestAllTypes.class); + container.getValue().unpack(TestAllTypes.class); fail("Exception is expected."); } catch (InvalidProtocolBufferException e) { // expected. @@ -80,12 +76,10 @@ public class AnyTest extends TestCase { TestUtil.setAllFields(builder); TestAllTypes message = builder.build(); - TestAny container = TestAny.newBuilder() - .setValue(Any.pack(message, "xxx.com")).build(); + TestAny container = TestAny.newBuilder().setValue(Any.pack(message, "xxx.com")).build(); assertEquals( - "xxx.com/" + TestAllTypes.getDescriptor().getFullName(), - container.getValue().getTypeUrl()); + "xxx.com/" + TestAllTypes.getDescriptor().getFullName(), container.getValue().getTypeUrl()); assertTrue(container.getValue().is(TestAllTypes.class)); assertFalse(container.getValue().is(TestAny.class)); @@ -93,12 +87,10 @@ public class AnyTest extends TestCase { TestAllTypes result = container.getValue().unpack(TestAllTypes.class); TestUtil.assertAllFieldsSet(result); - container = TestAny.newBuilder() - .setValue(Any.pack(message, "yyy.com/")).build(); + container = TestAny.newBuilder().setValue(Any.pack(message, "yyy.com/")).build(); assertEquals( - "yyy.com/" + TestAllTypes.getDescriptor().getFullName(), - container.getValue().getTypeUrl()); + "yyy.com/" + TestAllTypes.getDescriptor().getFullName(), container.getValue().getTypeUrl()); assertTrue(container.getValue().is(TestAllTypes.class)); assertFalse(container.getValue().is(TestAny.class)); @@ -106,12 +98,10 @@ public class AnyTest extends TestCase { result = container.getValue().unpack(TestAllTypes.class); TestUtil.assertAllFieldsSet(result); - container = TestAny.newBuilder() - .setValue(Any.pack(message, "")).build(); + container = TestAny.newBuilder().setValue(Any.pack(message, "")).build(); assertEquals( - "/" + TestAllTypes.getDescriptor().getFullName(), - container.getValue().getTypeUrl()); + "/" + TestAllTypes.getDescriptor().getFullName(), container.getValue().getTypeUrl()); assertTrue(container.getValue().is(TestAllTypes.class)); assertFalse(container.getValue().is(TestAny.class)); @@ -125,13 +115,12 @@ public class AnyTest extends TestCase { TestUtil.setAllFields(builder); TestAllTypes message = builder.build(); - TestAny container = TestAny.newBuilder() - .setValue(Any.pack(message)).build(); + TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build(); assertTrue(container.getValue().is(TestAllTypes.class)); TestAllTypes result1 = container.getValue().unpack(TestAllTypes.class); TestAllTypes result2 = container.getValue().unpack(TestAllTypes.class); - assertTrue(result1 == result2); + assertTrue(Objects.equals(result1, result2)); } } diff --git a/java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java b/java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java new file mode 100755 index 0000000000..037173b667 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java @@ -0,0 +1,236 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.ArrayDecoders.Registers; +import java.io.IOException; +import junit.framework.TestCase; + +public class ArrayDecodersTest extends TestCase { + + private static final int TAG = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED); + private static final ByteString NEGATIVE_SIZE_0 = generateNegativeLength(0); + private static final ByteString NEGATIVE_SIZE_1 = generateNegativeLength(1); + + private Registers registers; + + @Override + public void setUp() { + registers = new Registers(); + registers.int1 = TAG; + } + + public void testException_decodeString() { + try { + ArrayDecoders.decodeString(NEGATIVE_SIZE_0.toByteArray(), 0, registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringRequireUtf8() { + try { + ArrayDecoders.decodeStringRequireUtf8(NEGATIVE_SIZE_0.toByteArray(), 0, registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeBytes() { + try { + ArrayDecoders.decodeBytes(NEGATIVE_SIZE_0.toByteArray(), 0, registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringList_first() { + try { + ArrayDecoders.decodeStringList( + TAG, + NEGATIVE_SIZE_0.toByteArray(), + 0, + NEGATIVE_SIZE_0.size(), + new ProtobufArrayList(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringList_second() { + try { + ArrayDecoders.decodeStringList( + TAG, + NEGATIVE_SIZE_1.toByteArray(), + 0, + NEGATIVE_SIZE_1.size(), + new ProtobufArrayList(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringListRequireUtf8_first() { + try { + ArrayDecoders.decodeStringListRequireUtf8( + TAG, + NEGATIVE_SIZE_0.toByteArray(), + 0, + NEGATIVE_SIZE_0.size(), + new ProtobufArrayList(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringListRequireUtf8_second() { + try { + ArrayDecoders.decodeStringListRequireUtf8( + TAG, + NEGATIVE_SIZE_1.toByteArray(), + 0, + NEGATIVE_SIZE_1.size(), + new ProtobufArrayList(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeBytesList_first() { + try { + ArrayDecoders.decodeBytesList( + TAG, + NEGATIVE_SIZE_0.toByteArray(), + 0, + NEGATIVE_SIZE_0.size(), + new ProtobufArrayList(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeBytesList_second() { + try { + ArrayDecoders.decodeBytesList( + TAG, + NEGATIVE_SIZE_1.toByteArray(), + 0, + NEGATIVE_SIZE_1.size(), + new ProtobufArrayList(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeUnknownField() { + try { + ArrayDecoders.decodeUnknownField( + TAG, + NEGATIVE_SIZE_0.toByteArray(), + 0, + NEGATIVE_SIZE_0.size(), + UnknownFieldSetLite.newInstance(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeHugeField() { + byte[] badBytes = + new byte[] { + (byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xEF, 0x73, 0x74, 0x69, 0x6E, 0x67 + }; + try { + ArrayDecoders.decodeUnknownField( + TAG, badBytes, 0, badBytes.length, UnknownFieldSetLite.newInstance(), registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + + try { + ArrayDecoders.decodeBytes(badBytes, 0, registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + + byte[] badBytesList = + new byte[] { + 0x01, + 0x77, + 0x0A, + (byte) 0x80, + (byte) 0xFF, + (byte) 0xFF, + (byte) 0xEF, + 0x73, + 0x74, + 0x69, + 0x6E, + 0x67 + }; + try { + ArrayDecoders.decodeBytesList( + TAG, badBytesList, 0, badBytes.length, new ProtobufArrayList<>(), registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + private static ByteString generateNegativeLength(int count) { + try { + ByteString.Output byteStringOutput = ByteString.newOutput(); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(byteStringOutput); + + // Write out count - 1 valid 0 length fields; we only write out tags after the field since + // ArrayDecoders expects the first tag to already have been parsed. + for (int i = 0; i < count; i++) { + codedOutput.writeInt32NoTag(0); + codedOutput.writeInt32NoTag(TAG); + } + + // Write out a negative length + codedOutput.writeInt32NoTag(-1); + + codedOutput.flush(); + + return byteStringOutput.toByteString(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java b/java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java new file mode 100755 index 0000000000..cda2998f5d --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java @@ -0,0 +1,90 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class BinaryProtocolTest { + @Before + public void setup() { + TestSchemas.registerGenericProto2Schemas(); + + Protobuf.getInstance() + .registerSchemaOverride(Proto3Message.class, TestSchemas.genericProto3Schema); + } + + @Test + public void proto3Roundtrip() throws Exception { + Proto3Message expected = new Proto3MessageFactory(5, 10, 2, 2).newMessage(); + byte[] expectedBytes = expected.toByteArray(); + + // Deserialize with BinaryReader and verify that the message matches the original. + Proto3Message result = + ExperimentalSerializationUtil.fromByteArray(expectedBytes, Proto3Message.class); + assertEquals(expected, result); + + // Now write it back out using BinaryWriter and verify the output length. + byte[] actualBytes = ExperimentalSerializationUtil.toByteArray(result); + Assert.assertEquals(expectedBytes.length, actualBytes.length); + + // Read back in the bytes and verify that it matches the original message. + Proto3Message actual = Proto3Message.parseFrom(actualBytes); + assertEquals(expected, actual); + } + + @Test + public void proto2Roundtrip() throws Exception { + Proto2Message expected = new Proto2MessageFactory(5, 10, 2, 2).newMessage(); + byte[] expectedBytes = expected.toByteArray(); + + // Deserialize with BinaryReader and verify that the message matches the original. + Proto2Message result = + ExperimentalSerializationUtil.fromByteArray(expectedBytes, Proto2Message.class); + assertEquals(expected, result); + + // Now write it back out using BinaryWriter and verify the output length. + byte[] actualBytes = ExperimentalSerializationUtil.toByteArray(result); + Assert.assertEquals(expectedBytes.length, actualBytes.length); + + // Read back in the bytes and verify that it matches the original message. + Proto2Message actual = Proto2Message.parseFrom(actualBytes); + assertEquals(expected, actual); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java b/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java index febe8537f6..ae50071398 100644 --- a/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java @@ -45,8 +45,7 @@ import junit.framework.TestCase; */ public class BooleanArrayListTest extends TestCase { - private static final BooleanArrayList UNARY_LIST = - newImmutableBooleanArrayList(true); + private static final BooleanArrayList UNARY_LIST = newImmutableBooleanArrayList(true); private static final BooleanArrayList TERTIARY_LIST = newImmutableBooleanArrayList(true, false, true); @@ -226,8 +225,7 @@ public class BooleanArrayListTest extends TestCase { list.add(i % 2 == 0); } assertEquals( - asList(false, true, false, false, true, true, false, true, false, true, false), - list); + asList(false, true, false, false, true, true, false, true, false, true, false), list); try { list.add(-1, true); @@ -270,6 +268,13 @@ public class BooleanArrayListTest extends TestCase { assertFalse(list.addAll(BooleanArrayList.emptyList())); } + public void testEquals() { + BooleanArrayList list1 = new BooleanArrayList(); + BooleanArrayList list2 = new BooleanArrayList(); + + assertEquals(list1, list2); + } + public void testRemove() { list.addAll(TERTIARY_LIST); assertEquals(true, (boolean) list.remove(0)); @@ -298,17 +303,24 @@ public class BooleanArrayListTest extends TestCase { } } - public void testRemoveEndOfCapacity() { - BooleanList toRemove = - BooleanArrayList.emptyList().mutableCopyWithCapacity(1); + public void testRemoveEnd_listAtCapacity() { + BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addBoolean(true); toRemove.remove(0); assertEquals(0, toRemove.size()); } + public void testRemove_listAtCapacity() { + BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(2); + toRemove.addBoolean(true); + toRemove.addBoolean(false); + toRemove.remove(0); + assertEquals(1, toRemove.size()); + assertEquals(false, (boolean) toRemove.get(0)); + } + public void testSublistRemoveEndOfCapacity() { - BooleanList toRemove = - BooleanArrayList.emptyList().mutableCopyWithCapacity(1); + BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addBoolean(true); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); diff --git a/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java b/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java index db10ee7483..654d62bc35 100644 --- a/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/BoundedByteStringTest.java @@ -38,9 +38,9 @@ import java.io.ObjectOutputStream; import java.io.UnsupportedEncodingException; /** - * This class tests {@link BoundedByteString}, which extends {@link LiteralByteString}, - * by inheriting the tests from {@link LiteralByteStringTest}. The only method which - * is strange enough that it needs to be overridden here is {@link #testToString()}. + * This class tests {@link BoundedByteString}, which extends {@link LiteralByteString}, by + * inheriting the tests from {@link LiteralByteStringTest}. The only method which is strange enough + * that it needs to be overridden here is {@link #testToString()}. * * @author carlanton@google.com (Carl Haverl) */ @@ -63,12 +63,16 @@ public class BoundedByteStringTest extends LiteralByteStringTest { String testString = "I love unicode \u1234\u5678 characters"; ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); - assertEquals(classUnderTest + ".substring() must have the expected type", - classUnderTest, getActualClassName(chopped)); + assertEquals( + classUnderTest + ".substring() must have the expected type", + classUnderTest, + getActualClassName(chopped)); String roundTripString = chopped.toString(UTF_8); - assertEquals(classUnderTest + " unicode bytes must match", - testString.substring(2, testString.length() - 6), roundTripString); + assertEquals( + classUnderTest + " unicode bytes must match", + testString.substring(2, testString.length() - 6), + roundTripString); } @Override @@ -76,12 +80,16 @@ public class BoundedByteStringTest extends LiteralByteStringTest { String testString = "I love unicode \u1234\u5678 characters"; ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); - assertEquals(classUnderTest + ".substring() must have the expected type", - classUnderTest, getActualClassName(chopped)); + assertEquals( + classUnderTest + ".substring() must have the expected type", + classUnderTest, + getActualClassName(chopped)); String roundTripString = chopped.toString(Internal.UTF_8); - assertEquals(classUnderTest + " unicode bytes must match", - testString.substring(2, testString.length() - 6), roundTripString); + assertEquals( + classUnderTest + " unicode bytes must match", + testString.substring(2, testString.length() - 6), + roundTripString); } @Override diff --git a/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java b/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java index 6b1cfe78c1..5f0ef62028 100644 --- a/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java +++ b/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java @@ -37,9 +37,7 @@ import java.util.Arrays; import java.util.Random; import junit.framework.TestCase; -/** - * Tests for {@link ByteBufferWriter}. - */ +/** Tests for {@link ByteBufferWriter}. */ public class ByteBufferWriterTest extends TestCase { public void testHeapBuffer() throws IOException { diff --git a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java index fc22955d06..83b343ada6 100644 --- a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java @@ -124,22 +124,24 @@ public class ByteStringTest extends TestCase { public void testSubstring_BeginIndex() { byte[] bytes = getTestBytes(); ByteString substring = ByteString.copyFrom(bytes).substring(500); - assertTrue("substring must contain the tail of the string", + assertTrue( + "substring must contain the tail of the string", isArrayRange(substring.toByteArray(), bytes, 500, bytes.length - 500)); } public void testCopyFrom_BytesOffsetSize() { byte[] bytes = getTestBytes(); ByteString byteString = ByteString.copyFrom(bytes, 500, 200); - assertTrue("copyFrom sub-range must contain the expected bytes", + assertTrue( + "copyFrom sub-range must contain the expected bytes", isArrayRange(byteString.toByteArray(), bytes, 500, 200)); } public void testCopyFrom_Bytes() { byte[] bytes = getTestBytes(); ByteString byteString = ByteString.copyFrom(bytes); - assertTrue("copyFrom must contain the expected bytes", - isArray(byteString.toByteArray(), bytes)); + assertTrue( + "copyFrom must contain the expected bytes", isArray(byteString.toByteArray(), bytes)); } public void testCopyFrom_ByteBufferSize() { @@ -148,7 +150,8 @@ public class ByteStringTest extends TestCase { byteBuffer.put(bytes); byteBuffer.position(500); ByteString byteString = ByteString.copyFrom(byteBuffer, 200); - assertTrue("copyFrom byteBuffer sub-range must contain the expected bytes", + assertTrue( + "copyFrom byteBuffer sub-range must contain the expected bytes", isArrayRange(byteString.toByteArray(), bytes, 500, 200)); } @@ -158,7 +161,8 @@ public class ByteStringTest extends TestCase { byteBuffer.put(bytes); byteBuffer.position(500); ByteString byteString = ByteString.copyFrom(byteBuffer); - assertTrue("copyFrom byteBuffer sub-range must contain the expected bytes", + assertTrue( + "copyFrom byteBuffer sub-range must contain the expected bytes", isArrayRange(byteString.toByteArray(), bytes, 500, bytes.length - 500)); } @@ -166,7 +170,8 @@ public class ByteStringTest extends TestCase { String testString = "I love unicode \u1234\u5678 characters"; ByteString byteString = ByteString.copyFrom(testString, UTF_16); byte[] testBytes = testString.getBytes(UTF_16); - assertTrue("copyFrom string must respect the charset", + assertTrue( + "copyFrom string must respect the charset", isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); } @@ -174,7 +179,8 @@ public class ByteStringTest extends TestCase { String testString = "I love unicode \u1234\u5678 characters"; ByteString byteString = ByteString.copyFromUtf8(testString); byte[] testBytes = testString.getBytes(Internal.UTF_8); - assertTrue("copyFromUtf8 string must respect the charset", + assertTrue( + "copyFromUtf8 string must respect the charset", isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); } @@ -183,17 +189,20 @@ public class ByteStringTest extends TestCase { final List pieces = makeConcretePieces(testBytes); // Call copyFrom() on a Collection ByteString byteString = ByteString.copyFrom(pieces); - assertTrue("copyFrom a List must contain the expected bytes", + assertTrue( + "copyFrom a List must contain the expected bytes", isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); // Call copyFrom on an iteration that's not a collection - ByteString byteStringAlt = ByteString.copyFrom(new Iterable() { - @Override - public Iterator iterator() { - return pieces.iterator(); - } - }); - assertEquals("copyFrom from an Iteration must contain the expected bytes", - byteString, byteStringAlt); + ByteString byteStringAlt = + ByteString.copyFrom( + new Iterable() { + @Override + public Iterator iterator() { + return pieces.iterator(); + } + }); + assertEquals( + "copyFrom from an Iteration must contain the expected bytes", byteString, byteStringAlt); } public void testCopyFrom_LengthTooBig() { @@ -229,15 +238,17 @@ public class ByteStringTest extends TestCase { ByteString byteString = ByteString.copyFrom(bytes); byte[] target = new byte[bytes.length + 1000]; byteString.copyTo(target, 400); - assertTrue("copyFrom byteBuffer sub-range must contain the expected bytes", + assertTrue( + "copyFrom byteBuffer sub-range must contain the expected bytes", isArrayRange(bytes, target, 400, bytes.length)); } public void testReadFrom_emptyStream() throws IOException { - ByteString byteString = - ByteString.readFrom(new ByteArrayInputStream(new byte[0])); - assertSame("reading an empty stream must result in the EMPTY constant " - + "byte string", ByteString.EMPTY, byteString); + ByteString byteString = ByteString.readFrom(new ByteArrayInputStream(new byte[0])); + assertSame( + "reading an empty stream must result in the EMPTY constant byte string", + ByteString.EMPTY, + byteString); } public void testReadFrom_smallStream() throws IOException { @@ -245,18 +256,18 @@ public class ByteStringTest extends TestCase { } public void testReadFrom_mutating() throws IOException { - byte[] capturedArray = null; EvilInputStream eis = new EvilInputStream(); ByteString byteString = ByteString.readFrom(eis); + byte[] capturedArray = eis.capturedArray; - capturedArray = eis.capturedArray; byte[] originalValue = byteString.toByteArray(); for (int x = 0; x < capturedArray.length; ++x) { capturedArray[x] = (byte) 0; } byte[] newValue = byteString.toByteArray(); - assertTrue("copyFrom byteBuffer must not grant access to underlying array", + assertTrue( + "copyFrom byteBuffer must not grant access to underlying array", Arrays.equals(originalValue, newValue)); } @@ -318,8 +329,8 @@ public class ByteStringTest extends TestCase { fail("readFrom must throw the underlying IOException"); } catch (IOException e) { - assertEquals("readFrom must throw the expected exception", - "synthetic failure", e.getMessage()); + assertEquals( + "readFrom must throw the expected exception", "synthetic failure", e.getMessage()); } } @@ -329,7 +340,8 @@ public class ByteStringTest extends TestCase { final byte[] data = getTestBytes(0x1000); ByteString byteString = ByteString.readFrom(new ReluctantStream(data)); - assertTrue("readFrom byte stream must contain the expected bytes", + assertTrue( + "readFrom byte stream must contain the expected bytes", isArray(byteString.toByteArray(), data)); // Same test as above, but with some specific chunk sizes. @@ -344,11 +356,10 @@ public class ByteStringTest extends TestCase { // Fails unless ByteString.readFrom reads the bytes correctly from a // reluctant stream with the given chunkSize parameter. - private void assertReadFromReluctantStream(byte[] bytes, int chunkSize) - throws IOException { + private void assertReadFromReluctantStream(byte[] bytes, int chunkSize) throws IOException { ByteString b = ByteString.readFrom(new ReluctantStream(bytes), chunkSize); - assertTrue("readFrom byte stream must contain the expected bytes", - isArray(b.toByteArray(), bytes)); + assertTrue( + "readFrom byte stream must contain the expected bytes", isArray(b.toByteArray(), bytes)); } // Tests that ByteString.readFrom works with streams that implement @@ -357,21 +368,23 @@ public class ByteStringTest extends TestCase { final byte[] data = getTestBytes(0x1001); ByteString byteString = ByteString.readFrom(new AvailableStream(data)); - assertTrue("readFrom byte stream must contain the expected bytes", + assertTrue( + "readFrom byte stream must contain the expected bytes", isArray(byteString.toByteArray(), data)); } // Fails unless ByteString.readFrom reads the bytes correctly. private void assertReadFrom(byte[] bytes) throws IOException { - ByteString byteString = - ByteString.readFrom(new ByteArrayInputStream(bytes)); - assertTrue("readFrom byte stream must contain the expected bytes", + ByteString byteString = ByteString.readFrom(new ByteArrayInputStream(bytes)); + assertTrue( + "readFrom byte stream must contain the expected bytes", isArray(byteString.toByteArray(), bytes)); } // A stream that fails when read. private static final class FailStream extends InputStream { - @Override public int read() throws IOException { + @Override + public int read() throws IOException { throw new IOException("synthetic failure"); } } @@ -386,7 +399,8 @@ public class ByteStringTest extends TestCase { this.data = data; } - @Override public int read() { + @Override + public int read() { if (pos == data.length) { return -1; } else { @@ -394,11 +408,13 @@ public class ByteStringTest extends TestCase { } } - @Override public int read(byte[] buf) { + @Override + public int read(byte[] buf) { return read(buf, 0, buf.length); } - @Override public int read(byte[] buf, int offset, int size) { + @Override + public int read(byte[] buf, int offset, int size) { if (pos == data.length) { return -1; } @@ -415,7 +431,8 @@ public class ByteStringTest extends TestCase { super(data); } - @Override public int available() { + @Override + public int available() { return Math.min(250, data.length - pos); } } @@ -465,8 +482,8 @@ public class ByteStringTest extends TestCase { String testString = "I love unicode \u1234\u5678 characters"; byte[] testBytes = testString.getBytes(Internal.UTF_8); ByteString byteString = ByteString.copyFrom(testBytes); - assertEquals("copyToStringUtf8 must respect the charset", - testString, byteString.toStringUtf8()); + assertEquals( + "copyToStringUtf8 must respect the charset", testString, byteString.toStringUtf8()); } public void testNewOutput_InitialCapacity() throws IOException { @@ -484,8 +501,9 @@ public class ByteStringTest extends TestCase { public void testNewOutput_ArrayWrite() { byte[] bytes = getTestBytes(); int length = bytes.length; - int[] bufferSizes = {128, 256, length / 2, length - 1, length, length + 1, - 2 * length, 3 * length}; + int[] bufferSizes = { + 128, 256, length / 2, length - 1, length, length + 1, 2 * length, 3 * length + }; int[] writeSizes = {1, 4, 5, 7, 23, bytes.length}; for (int bufferSize : bufferSizes) { @@ -496,7 +514,8 @@ public class ByteStringTest extends TestCase { output.write(bytes, i, Math.min(writeSize, length - i)); } ByteString byteString = output.toByteString(); - assertTrue("String built from newOutput() must contain the expected bytes", + assertTrue( + "String built from newOutput() must contain the expected bytes", isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length)); } } @@ -507,16 +526,17 @@ public class ByteStringTest extends TestCase { public void testNewOutput_WriteChar() { byte[] bytes = getTestBytes(); int length = bytes.length; - int[] bufferSizes = {0, 1, 128, 256, length / 2, - length - 1, length, length + 1, - 2 * length, 3 * length}; + int[] bufferSizes = { + 0, 1, 128, 256, length / 2, length - 1, length, length + 1, 2 * length, 3 * length + }; for (int bufferSize : bufferSizes) { ByteString.Output output = ByteString.newOutput(bufferSize); for (byte byteValue : bytes) { output.write(byteValue); } ByteString byteString = output.toByteString(); - assertTrue("String built from newOutput() must contain the expected bytes", + assertTrue( + "String built from newOutput() must contain the expected bytes", isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length)); } } @@ -527,9 +547,9 @@ public class ByteStringTest extends TestCase { Random rng = new Random(1); byte[] bytes = getTestBytes(); int length = bytes.length; - int[] bufferSizes = {0, 1, 128, 256, length / 2, - length - 1, length, length + 1, - 2 * length, 3 * length}; + int[] bufferSizes = { + 0, 1, 128, 256, length / 2, length - 1, length, length + 1, 2 * length, 3 * length + }; for (int bufferSize : bufferSizes) { // Test writing the entire output using a mixture of write sizes and @@ -546,12 +566,13 @@ public class ByteStringTest extends TestCase { position++; } assertEquals("size() returns the right value", position, output.size()); - assertTrue("newOutput() substring must have correct bytes", - isArrayRange(output.toByteString().toByteArray(), - bytes, 0, position)); + assertTrue( + "newOutput() substring must have correct bytes", + isArrayRange(output.toByteString().toByteArray(), bytes, 0, position)); } ByteString byteString = output.toByteString(); - assertTrue("String built from newOutput() must contain the expected bytes", + assertTrue( + "String built from newOutput() must contain the expected bytes", isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length)); } } @@ -572,7 +593,8 @@ public class ByteStringTest extends TestCase { byte[] oldValue = byteString.toByteArray(); Arrays.fill(capturedArray, (byte) 0); byte[] newValue = byteString.toByteArray(); - assertTrue("Output must not provide access to the underlying byte array", + assertTrue( + "Output must not provide access to the underlying byte array", Arrays.equals(oldValue, newValue)); } @@ -581,14 +603,15 @@ public class ByteStringTest extends TestCase { ByteString.CodedBuilder builder = ByteString.newCodedBuilder(bytes.length); builder.getCodedOutput().writeRawBytes(bytes); ByteString byteString = builder.build(); - assertTrue("String built from newCodedBuilder() must contain the expected bytes", + assertTrue( + "String built from newCodedBuilder() must contain the expected bytes", isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length)); } public void testSubstringParity() { byte[] bigBytes = getTestBytes(2048 * 1024, 113344L); int start = 512 * 1024 - 3333; - int end = 512 * 1024 + 7777; + int end = 512 * 1024 + 7777; ByteString concreteSubstring = ByteString.copyFrom(bigBytes).substring(start, end); boolean ok = true; for (int i = start; ok && i < end; ++i) { @@ -597,10 +620,11 @@ public class ByteStringTest extends TestCase { assertTrue("Concrete substring didn't capture the right bytes", ok); ByteString literalString = ByteString.copyFrom(bigBytes, start, end - start); - assertTrue("Substring must be equal to literal string", - concreteSubstring.equals(literalString)); - assertEquals("Substring must have same hashcode as literal string", - literalString.hashCode(), concreteSubstring.hashCode()); + assertEquals("Substring must be equal to literal string", literalString, concreteSubstring); + assertEquals( + "Substring must have same hashcode as literal string", + literalString.hashCode(), + concreteSubstring.hashCode()); } public void testCompositeSubstring() { @@ -626,15 +650,22 @@ public class ByteStringTest extends TestCase { assertTrue("Substring must support byteAt() correctly", stillEqual); ByteString literalSubstring = ByteString.copyFrom(referenceBytes, from, to - from); - assertTrue("Composite substring must equal a literal substring over the same bytes", - compositeSubstring.equals(literalSubstring)); - assertTrue("Literal substring must equal a composite substring over the same bytes", - literalSubstring.equals(compositeSubstring)); + assertEquals( + "Composite substring must equal a literal substring over the same bytes", + literalSubstring, + compositeSubstring); + assertEquals( + "Literal substring must equal a composite substring over the same bytes", + compositeSubstring, + literalSubstring); - assertEquals("We must get the same hashcodes for composite and literal substrings", - literalSubstring.hashCode(), compositeSubstring.hashCode()); + assertEquals( + "We must get the same hashcodes for composite and literal substrings", + literalSubstring.hashCode(), + compositeSubstring.hashCode()); - assertFalse("We can't be equal to a proper substring", + assertFalse( + "We can't be equal to a proper substring", compositeSubstring.equals(literalSubstring.substring(0, literalSubstring.size() - 1))); } @@ -645,10 +676,11 @@ public class ByteStringTest extends TestCase { List pieces = makeConcretePieces(referenceBytes); ByteString listString = ByteString.copyFrom(pieces); - assertTrue("Composite string must be equal to literal string", - listString.equals(literalString)); - assertEquals("Composite string must have same hashcode as literal string", - literalString.hashCode(), listString.hashCode()); + assertEquals("Composite string must be equal to literal string", literalString, listString); + assertEquals( + "Composite string must have same hashcode as literal string", + literalString.hashCode(), + listString.hashCode()); } public void testConcat() { @@ -663,30 +695,34 @@ public class ByteStringTest extends TestCase { concatenatedString = concatenatedString.concat(iter.next()); } - assertTrue("Concatenated string must be equal to literal string", - concatenatedString.equals(literalString)); - assertEquals("Concatenated string must have same hashcode as literal string", - literalString.hashCode(), concatenatedString.hashCode()); + assertEquals( + "Concatenated string must be equal to literal string", literalString, concatenatedString); + assertEquals( + "Concatenated string must have same hashcode as literal string", + literalString.hashCode(), + concatenatedString.hashCode()); } /** - * Test the Rope implementation can deal with Empty nodes, even though we - * guard against them. See also {@link LiteralByteStringTest#testConcat_empty()}. + * Test the Rope implementation can deal with Empty nodes, even though we guard against them. See + * also {@link LiteralByteStringTest#testConcat_empty()}. */ public void testConcat_empty() { byte[] referenceBytes = getTestBytes(7748, 113344L); ByteString literalString = ByteString.copyFrom(referenceBytes); ByteString duo = RopeByteString.newInstanceForTest(literalString, literalString); - ByteString temp = RopeByteString.newInstanceForTest( - RopeByteString.newInstanceForTest(literalString, ByteString.EMPTY), - RopeByteString.newInstanceForTest(ByteString.EMPTY, literalString)); + ByteString temp = + RopeByteString.newInstanceForTest( + RopeByteString.newInstanceForTest(literalString, ByteString.EMPTY), + RopeByteString.newInstanceForTest(ByteString.EMPTY, literalString)); ByteString quintet = RopeByteString.newInstanceForTest(temp, ByteString.EMPTY); - assertTrue("String with concatenated nulls must equal simple concatenate", - duo.equals(quintet)); - assertEquals("String with concatenated nulls have same hashcode as simple concatenate", - duo.hashCode(), quintet.hashCode()); + assertEquals("String with concatenated nulls must equal simple concatenate", quintet, duo); + assertEquals( + "String with concatenated nulls have same hashcode as simple concatenate", + duo.hashCode(), + quintet.hashCode()); ByteString.ByteIterator duoIter = duo.iterator(); ByteString.ByteIterator quintetIter = quintet.iterator(); @@ -716,11 +752,13 @@ public class ByteStringTest extends TestCase { // It is possible, using the testing factory method to create deeply nested // trees of empty leaves, to make a string that will fail this test. for (int i = 1; i < duo.size(); ++i) { - assertTrue("Substrings of size() < 2 must not be RopeByteStrings", + assertTrue( + "Substrings of size() < 2 must not be RopeByteStrings", duo.substring(i - 1, i) instanceof ByteString.LeafByteString); } for (int i = 1; i < quintet.size(); ++i) { - assertTrue("Substrings of size() < 2 must not be RopeByteStrings", + assertTrue( + "Substrings of size() < 2 must not be RopeByteStrings", quintet.substring(i - 1, i) instanceof ByteString.LeafByteString); } } @@ -769,8 +807,7 @@ public class ByteStringTest extends TestCase { return pieces; } - private byte[] substringUsingWriteTo( - ByteString data, int offset, int length) throws IOException { + private byte[] substringUsingWriteTo(ByteString data, int offset, int length) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); data.writeTo(output, offset, length); return output.toByteArray(); @@ -781,9 +818,7 @@ public class ByteStringTest extends TestCase { // won't be merged into one byte array due to some optimizations. final int dataSize = ByteString.CONCATENATE_BY_COPY_SIZE + 1; byte[] data1 = new byte[dataSize]; - for (int i = 0; i < data1.length; i++) { - data1[i] = (byte) 1; - } + Arrays.fill(data1, (byte) 1); data1[1] = (byte) 11; // Test LiteralByteString.writeTo(OutputStream,int,int) ByteString left = ByteString.wrap(data1); @@ -792,9 +827,7 @@ public class ByteStringTest extends TestCase { assertEquals((byte) 11, result[0]); byte[] data2 = new byte[dataSize]; - for (int i = 0; i < data1.length; i++) { - data2[i] = (byte) 2; - } + Arrays.fill(data2, 0, data1.length, (byte) 2); ByteString right = ByteString.wrap(data2); // Concatenate two ByteStrings to create a RopeByteString. ByteString root = left.concat(right); @@ -819,10 +852,8 @@ public class ByteStringTest extends TestCase { assertEquals((byte) 2, result[dataSize - dataSize / 2]); assertEquals((byte) 2, result[dataSize - 1]); } - - /** - * Tests ByteString uses Arrays based byte copier when running under Hotstop VM. - */ + + /** Tests ByteString uses Arrays based byte copier when running under Hotstop VM. */ public void testByteArrayCopier() throws Exception { if (Android.isOnAndroidDevice()) { return; diff --git a/java/core/src/test/java/com/google/protobuf/CachedFieldSizeTest.java b/java/core/src/test/java/com/google/protobuf/CachedFieldSizeTest.java new file mode 100755 index 0000000000..96319d93c5 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/CachedFieldSizeTest.java @@ -0,0 +1,65 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import protobuf_unittest.UnittestProto.TestPackedTypes; +import proto3_unittest.UnittestProto3; +import protobuf_unittest.TestCachedFieldSizeMessage; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class CachedFieldSizeTest { + // Regression test for b/74087933 + @Test + public void testCachedFieldSize() throws Exception { + TestCachedFieldSizeMessage.Builder builder = TestCachedFieldSizeMessage.newBuilder(); + builder.setProto2Child(TestUtil.getPackedSet()); + builder.setProto3Child( + UnittestProto3.TestPackedTypes.parseFrom(TestUtil.getPackedSet().toByteArray())); + TestCachedFieldSizeMessage message = builder.build(); + + // Serialize once to cache all field sizes. This will use the experimental runtime because + // the proto has optimize_for = CODE_SIZE. + message.toByteArray(); + // Serialize individual submessages. This will use the generated implementation. If the + // experimental runtime hasn't set the correct cached size, this will throw an exception. + byte[] data2 = message.getProto2Child().toByteArray(); + byte[] data3 = message.getProto3Child().toByteArray(); + + // Make sure the serialized data is correct. + assertEquals(message.getProto2Child(), TestPackedTypes.parseFrom(data2)); + assertEquals(message.getProto3Child(), UnittestProto3.TestPackedTypes.parseFrom(data3)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java b/java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java index 50b87ae31f..e276225e7a 100644 --- a/java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java +++ b/java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java @@ -38,22 +38,21 @@ import java.io.ByteArrayInputStream; import junit.framework.TestCase; /** - * Test that protos generated with file option java_string_check_utf8 do in - * fact perform appropriate UTF-8 checks. + * Test that protos generated with file option java_string_check_utf8 do in fact perform appropriate + * UTF-8 checks. * * @author jbaum@google.com (Jacob Butcher) */ public class CheckUtf8Test extends TestCase { private static final String UTF8_BYTE_STRING_TEXT = "some text"; - private static final ByteString UTF8_BYTE_STRING = - ByteString.copyFromUtf8(UTF8_BYTE_STRING_TEXT); + private static final ByteString UTF8_BYTE_STRING = ByteString.copyFromUtf8(UTF8_BYTE_STRING_TEXT); private static final ByteString NON_UTF8_BYTE_STRING = - ByteString.copyFrom(new byte[]{(byte) 0x80}); // A lone continuation byte. + ByteString.copyFrom(new byte[] {(byte) 0x80}); // A lone continuation byte. public void testBuildRequiredStringWithGoodUtf8() throws Exception { - assertEquals(UTF8_BYTE_STRING_TEXT, - StringWrapper.newBuilder().setReqBytes(UTF8_BYTE_STRING).getReq()); + assertEquals( + UTF8_BYTE_STRING_TEXT, StringWrapper.newBuilder().setReqBytes(UTF8_BYTE_STRING).getReq()); } public void testParseRequiredStringWithGoodUtf8() throws Exception { diff --git a/java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java b/java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java new file mode 100755 index 0000000000..a24b48b51e --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java @@ -0,0 +1,110 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import java.io.IOException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class CodedAdapterTest { + @Before + public void setup() { + TestSchemas.registerGenericProto2Schemas(); + + Protobuf.getInstance() + .registerSchemaOverride(Proto3Message.class, TestSchemas.genericProto3Schema); + } + + @Test + public void proto3Roundtrip() throws Exception { + Proto3Message expected = new Proto3MessageFactory(5, 10, 2, 2).newMessage(); + byte[] expectedBytes = expected.toByteArray(); + + // Deserialize with BinaryReader and verify that the message matches the original. + Proto3Message result = fromByteArray(expectedBytes, Proto3Message.class); + assertEquals(expected, result); + + // Now write it back out using BinaryWriter and verify the output length. + byte[] actualBytes = toByteArray(result, expectedBytes.length); + + // Read back in the bytes and verify that it matches the original message. + Proto3Message actual = Proto3Message.parseFrom(actualBytes); + assertEquals(expected, actual); + } + + @Test + public void proto2Roundtrip() throws Exception { + Proto2Message expected = new Proto2MessageFactory(5, 10, 2, 2).newMessage(); + byte[] expectedBytes = expected.toByteArray(); + + // Deserialize with BinaryReader and verify that the message matches the original. + Proto2Message result = fromByteArray(expectedBytes, Proto2Message.class); + assertEquals(expected, result); + + // Now write it back out using BinaryWriter and verify the output length. + byte[] actualBytes = toByteArray(result, expectedBytes.length); + + // Read back in the bytes and verify that it matches the original message. + Proto2Message actual = Proto2Message.parseFrom(actualBytes); + assertEquals(expected, actual); + } + + public static byte[] toByteArray(T msg, int size) throws Exception { + Schema schema = Protobuf.getInstance().schemaFor(msg); + byte[] out = new byte[size]; + CodedOutputStreamWriter writer = + CodedOutputStreamWriter.forCodedOutput(CodedOutputStream.newInstance(out)); + schema.writeTo(msg, writer); + assertEquals(out.length, writer.getTotalBytesWritten()); + return out; + } + + public static T fromByteArray(byte[] data, Class messageType) { + Schema schema = Protobuf.getInstance().schemaFor(messageType); + try { + T msg = schema.newInstance(); + schema.mergeFrom( + msg, + CodedInputStreamReader.forCodedInput(CodedInputStream.newInstance(data)), + ExtensionRegistryLite.EMPTY_REGISTRY_LITE); + return msg; + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java b/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java index 5ea6b79c79..532052cdb3 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java @@ -30,6 +30,7 @@ package com.google.protobuf; +import static org.junit.Assert.assertArrayEquals; import protobuf_unittest.UnittestProto.BoolMessage; import protobuf_unittest.UnittestProto.Int32Message; import protobuf_unittest.UnittestProto.Int64Message; @@ -43,6 +44,7 @@ import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import junit.framework.TestCase; /** @@ -51,9 +53,9 @@ import junit.framework.TestCase; * @author kenton@google.com Kenton Varda */ public class CodedInputStreamTest extends TestCase { - - private static final int DEFAULT_BLOCK_SIZE = 4096; - + + private static final int DEFAULT_BLOCK_SIZE = 4096; + private enum InputType { ARRAY { @Override @@ -88,10 +90,10 @@ public class CodedInputStreamTest extends TestCase { if (blockSize > DEFAULT_BLOCK_SIZE) { blockSize = DEFAULT_BLOCK_SIZE; } - ArrayList input = new ArrayList (); + ArrayList input = new ArrayList(); for (int i = 0; i < data.length; i += blockSize) { - int rl = Math.min(blockSize, data.length - i); - ByteBuffer rb = ByteBuffer.allocateDirect(rl); + int rl = Math.min(blockSize, data.length - i); + ByteBuffer rb = ByteBuffer.allocateDirect(rl); rb.put(data, i, rl); rb.flip(); input.add(rb); @@ -105,10 +107,10 @@ public class CodedInputStreamTest extends TestCase { if (blockSize > DEFAULT_BLOCK_SIZE) { blockSize = DEFAULT_BLOCK_SIZE; } - ArrayList input = new ArrayList (); + ArrayList input = new ArrayList(); for (int i = 0; i < data.length; i += blockSize) { - int rl = Math.min(blockSize, data.length - i); - ByteBuffer rb = ByteBuffer.allocateDirect(rl); + int rl = Math.min(blockSize, data.length - i); + ByteBuffer rb = ByteBuffer.allocateDirect(rl); rb.put(data, i, rl); rb.flip(); input.add(rb); @@ -116,8 +118,6 @@ public class CodedInputStreamTest extends TestCase { return CodedInputStream.newInstance(new IterableByteBufferInputStream(input)); } }; - - CodedInputStream newDecoder(byte[] data) { return newDecoder(data, data.length); @@ -144,21 +144,37 @@ public class CodedInputStreamTest extends TestCase { */ private static final class SmallBlockInputStream extends FilterInputStream { private final int blockSize; + private int skipCalls; + private int readCalls; public SmallBlockInputStream(byte[] data, int blockSize) { super(new ByteArrayInputStream(data)); this.blockSize = blockSize; } + @Override + public int read() throws IOException { + readCalls++; + return super.read(); + } + @Override public int read(byte[] b) throws IOException { + readCalls++; return super.read(b, 0, Math.min(b.length, blockSize)); } @Override public int read(byte[] b, int off, int len) throws IOException { + readCalls++; return super.read(b, off, Math.min(len, blockSize)); } + + @Override + public long skip(long len) throws IOException { + skipCalls++; + return super.skip(Math.min(len, blockSize)); + } } private void assertDataConsumed(String msg, byte[] data, CodedInputStream input) @@ -430,6 +446,41 @@ public class CodedInputStreamTest extends TestCase { } } + /** + * Test that calling skipRawBytes (when not merging a message) actually skips from the underlying + * inputstream, regardless of the buffer size used. + */ + public void testSkipRawBytesActuallySkips() throws Exception { + SmallBlockInputStream bytes = new SmallBlockInputStream(new byte[] {1, 2, 3, 4, 5}, 3); + CodedInputStream input = CodedInputStream.newInstance(bytes, 1); // Tiny buffer + input.skipRawBytes(3); + input.skipRawBytes(2); + assertEquals(2, bytes.skipCalls); + assertEquals(0, bytes.readCalls); + } + + public void testSkipHugeBlob() throws Exception { + // Allocate and initialize a 1MB blob. + int blobSize = 1 << 20; + byte[] blob = new byte[blobSize]; + for (int i = 0; i < blob.length; i++) { + blob[i] = (byte) i; + } + + for (InputType inputType : InputType.values()) { + CodedInputStream decoder = inputType.newDecoder(blob); + decoder.skipRawBytes(blobSize - 10); + byte[] remaining = decoder.readRawBytes(10); + assertArrayEquals(Arrays.copyOfRange(blob, blobSize - 10, blobSize), remaining); + } + } + + /** Skipping a huge blob should not allocate excessive memory, so there should be no limit */ + public void testSkipMaliciouslyHugeBlob() throws Exception { + InputStream is = new RepeatingInputStream(new byte[]{1}, Integer.MAX_VALUE); + CodedInputStream.newInstance(is).skipRawBytes(Integer.MAX_VALUE); + } + public void testReadHugeBlob() throws Exception { // Allocate and initialize a 1MB blob. byte[] blob = new byte[1 << 20]; @@ -485,8 +536,9 @@ public class CodedInputStreamTest extends TestCase { } /** - * Test we can do messages that are up to CodedInputStream#DEFAULT_SIZE_LIMIT - * in size (2G or Integer#MAX_SIZE). + * Test we can do messages that are up to CodedInputStream#DEFAULT_SIZE_LIMIT in size (2G or + * Integer#MAX_SIZE). + * * @throws IOException */ public void testParseMessagesCloseTo2G() throws IOException { @@ -501,8 +553,9 @@ public class CodedInputStreamTest extends TestCase { } /** - * Test there is an exception if a message exceeds - * CodedInputStream#DEFAULT_SIZE_LIMIT in size (2G or Integer#MAX_SIZE). + * Test there is an exception if a message exceeds CodedInputStream#DEFAULT_SIZE_LIMIT in size (2G + * or Integer#MAX_SIZE). + * * @throws IOException */ public void testParseMessagesOver2G() throws IOException { @@ -526,7 +579,7 @@ public class CodedInputStreamTest extends TestCase { * @return A serialized big message. */ private static byte[] getBigSerializedMessage() { - byte[] value = new byte[16 * 1024 * 1024]; + byte[] value = new byte[16 * 1024 * 1024]; ByteString bsValue = ByteString.wrap(value); return TestAllTypes.newBuilder().setOptionalBytes(bsValue).build().toByteArray(); } @@ -654,7 +707,7 @@ public class CodedInputStreamTest extends TestCase { checkSizeLimitExceeded(expected); } } - + public void testRefillBufferWithCorrectSize() throws Exception { // NOTE: refillBuffer only applies to the stream-backed CIS. byte[] bytes = "123456789".getBytes("UTF-8"); @@ -672,14 +725,15 @@ public class CodedInputStreamTest extends TestCase { output.flush(); // Input is two string with length 9 and one raw byte. - byte[] rawInput = rawOutput.toByteArray(); - for (int inputStreamBufferLength = 8; - inputStreamBufferLength <= rawInput.length + 1; inputStreamBufferLength++) { - CodedInputStream input = CodedInputStream.newInstance( - new ByteArrayInputStream(rawInput), inputStreamBufferLength); + byte[] rawInput = rawOutput.toByteArray(); + for (int inputStreamBufferLength = 8; + inputStreamBufferLength <= rawInput.length + 1; + inputStreamBufferLength++) { + CodedInputStream input = + CodedInputStream.newInstance(new ByteArrayInputStream(rawInput), inputStreamBufferLength); input.setSizeLimit(rawInput.length - 1); input.readString(); - input.readString(); + input.readString(); try { input.readRawByte(); // Hits limit. fail("Should have thrown an exception!"); @@ -690,16 +744,15 @@ public class CodedInputStreamTest extends TestCase { } public void testIsAtEnd() throws Exception { - CodedInputStream input = CodedInputStream.newInstance( - new ByteArrayInputStream(new byte[5])); - try { + CodedInputStream input = CodedInputStream.newInstance(new ByteArrayInputStream(new byte[5])); + try { for (int i = 0; i < 5; i++) { assertEquals(false, input.isAtEnd()); input.readRawByte(); } assertEquals(true, input.isAtEnd()); } catch (Exception e) { - fail("Catch exception in the testIsAtEnd"); + throw new AssertionError("Catch exception in the testIsAtEnd", e); } } @@ -715,19 +768,18 @@ public class CodedInputStreamTest extends TestCase { output.flush(); byte[] rawInput = rawOutput.toByteArray(); - CodedInputStream input = CodedInputStream.newInstance( - new ByteArrayInputStream(rawInput)); + CodedInputStream input = CodedInputStream.newInstance(new ByteArrayInputStream(rawInput)); // The length of the whole rawInput input.setSizeLimit(11); // Some number that is smaller than the rawInput's length - // but larger than 2 + // but larger than 2 input.pushLimit(5); try { input.readString(); fail("Should have thrown an exception"); } catch (InvalidProtocolBufferException expected) { - assertEquals(expected.getMessage(), - InvalidProtocolBufferException.truncatedMessage().getMessage()); + assertEquals( + expected.getMessage(), InvalidProtocolBufferException.truncatedMessage().getMessage()); } } @@ -936,13 +988,14 @@ public class CodedInputStreamTest extends TestCase { output.flush(); byte[] data = rawOutput.toByteString().toByteArray(); - CodedInputStream input = CodedInputStream.newInstance( - new ByteArrayInputStream(data) { - @Override - public synchronized int available() { - return 0; - } - }); + CodedInputStream input = + CodedInputStream.newInstance( + new ByteArrayInputStream(data) { + @Override + public synchronized int available() { + return 0; + } + }); ByteString result = input.readBytes(); assertEquals(ByteString.copyFrom(bytes), result); } @@ -959,13 +1012,14 @@ public class CodedInputStreamTest extends TestCase { output.flush(); byte[] data = rawOutput.toByteString().toByteArray(); - CodedInputStream input = CodedInputStream.newInstance( - new ByteArrayInputStream(data) { - @Override - public synchronized int available() { - return 0; - } - }); + CodedInputStream input = + CodedInputStream.newInstance( + new ByteArrayInputStream(data) { + @Override + public synchronized int available() { + return 0; + } + }); byte[] result = input.readByteArray(); assertTrue(Arrays.equals(bytes, result)); } @@ -1034,8 +1088,8 @@ public class CodedInputStreamTest extends TestCase { byte[] data = byteArrayStream.toByteArray(); for (InputType inputType : InputType.values()) { - if (inputType == InputType.STREAM - || inputType == InputType.STREAM_ITER_DIRECT + if (inputType == InputType.STREAM + || inputType == InputType.STREAM_ITER_DIRECT || inputType == InputType.ITER_DIRECT) { // Aliasing doesn't apply to stream-backed CIS. continue; @@ -1133,4 +1187,68 @@ public class CodedInputStreamTest extends TestCase { } } } + + public void testSkipPastEndOfByteArrayInput() throws Exception { + try { + CodedInputStream.newInstance(new ByteArrayInputStream(new byte[100])).skipRawBytes(101); + fail(); + } catch (InvalidProtocolBufferException e) { + // Expected + } + } + + public void testMaliciousInputStream() throws Exception { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream); + codedOutputStream.writeByteArrayNoTag(new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }); + codedOutputStream.flush(); + final List maliciousCapture = new ArrayList<>(); + InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()) { + @Override + public synchronized int read(byte[] b, int off, int len) { + maliciousCapture.add(b); + return super.read(b, off, len); + } + }; + + // test ByteString + + CodedInputStream codedInputStream = CodedInputStream.newInstance(inputStream, 1); + ByteString byteString = codedInputStream.readBytes(); + assertEquals(0x0, byteString.byteAt(0)); + maliciousCapture.get(1)[0] = 0x9; + assertEquals(0x0, byteString.byteAt(0)); + + // test ByteBuffer + + inputStream.reset(); + maliciousCapture.clear(); + codedInputStream = CodedInputStream.newInstance(inputStream, 1); + ByteBuffer byteBuffer = codedInputStream.readByteBuffer(); + assertEquals(0x0, byteBuffer.get(0)); + maliciousCapture.get(1)[0] = 0x9; + assertEquals(0x0, byteBuffer.get(0)); + + + // test byte[] + + inputStream.reset(); + maliciousCapture.clear(); + codedInputStream = CodedInputStream.newInstance(inputStream, 1); + byte[] byteArray = codedInputStream.readByteArray(); + assertEquals(0x0, byteArray[0]); + maliciousCapture.get(1)[0] = 0x9; + assertEquals(0x9, byteArray[0]); // MODIFICATION! Should we fix? + + // test rawBytes + + inputStream.reset(); + maliciousCapture.clear(); + codedInputStream = CodedInputStream.newInstance(inputStream, 1); + int length = codedInputStream.readRawVarint32(); + byteArray = codedInputStream.readRawBytes(length); + assertEquals(0x0, byteArray[0]); + maliciousCapture.get(1)[0] = 0x9; + assertEquals(0x9, byteArray[0]); // MODIFICATION! Should we fix? + } } diff --git a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index 78f415c254..8dc3e4ce7a 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -257,13 +257,27 @@ public class CodedOutputStreamTest extends TestCase { // 41256202580718336 assertWriteVarint( bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49), - (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) | (0x43L << 28) | (0x49L << 35) - | (0x24L << 42) | (0x49L << 49)); + (0x00 << 0) + | (0x66 << 7) + | (0x6b << 14) + | (0x1c << 21) + | (0x43L << 28) + | (0x49L << 35) + | (0x24L << 42) + | (0x49L << 49)); // 11964378330978735131 assertWriteVarint( bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01), - (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | (0x3bL << 28) | (0x56L << 35) - | (0x00L << 42) | (0x05L << 49) | (0x26L << 56) | (0x01L << 63)); + (0x1b << 0) + | (0x28 << 7) + | (0x79 << 14) + | (0x42 << 21) + | (0x3bL << 28) + | (0x56L << 35) + | (0x00L << 42) + | (0x05L << 49) + | (0x26L << 56) + | (0x01L << 63)); } /** Tests writeRawLittleEndian32() and writeRawLittleEndian64(). */ @@ -344,8 +358,8 @@ public class CodedOutputStreamTest extends TestCase { } /** - * Tests writing a whole message with every packed field type. Ensures the - * wire format of packed fields is compatible with C++. + * Tests writing a whole message with every packed field type. Ensures the wire format of packed + * fields is compatible with C++. */ public void testWriteWholePackedFieldsMessage() throws Exception { byte[] expectedBytes = TestUtil.getGoldenPackedFieldsMessage().toByteArray(); @@ -361,8 +375,8 @@ public class CodedOutputStreamTest extends TestCase { } /** - * Test writing a message containing a negative enum value. This used to - * fail because the size was not properly computed as a sign-extended varint. + * Test writing a message containing a negative enum value. This used to fail because the size was + * not properly computed as a sign-extended varint. */ public void testWriteMessageWithNegativeEnumValue() throws Exception { SparseEnumMessage message = @@ -394,8 +408,9 @@ public class CodedOutputStreamTest extends TestCase { String string = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; // Ensure we take the slower fast path. - assertTrue(CodedOutputStream.computeUInt32SizeNoTag(string.length()) - != CodedOutputStream.computeUInt32SizeNoTag(string.length() * Utf8.MAX_BYTES_PER_CHAR)); + assertTrue( + CodedOutputStream.computeUInt32SizeNoTag(string.length()) + != CodedOutputStream.computeUInt32SizeNoTag(string.length() * Utf8.MAX_BYTES_PER_CHAR)); coder.stream().writeStringNoTag(string); coder.stream().flush(); @@ -438,9 +453,7 @@ public class CodedOutputStreamTest extends TestCase { } final int length2 = 8 * 1024; byte[] data = new byte[length2]; - for (int i = 0; i < length2; i++) { - data[i] = (byte) 2; - } + Arrays.fill(data, 0, length2, (byte) 2); codedStream.writeRawBytes(data); final int length3 = bufferSize - length1 - length2; for (int i = 0; i < length3; i++) { @@ -521,10 +534,14 @@ public class CodedOutputStreamTest extends TestCase { } public void testSerializeInvalidUtf8() throws Exception { - String[] invalidStrings = new String[] {newString(Character.MIN_HIGH_SURROGATE), - "foobar" + newString(Character.MIN_HIGH_SURROGATE), newString(Character.MIN_LOW_SURROGATE), - "foobar" + newString(Character.MIN_LOW_SURROGATE), - newString(Character.MIN_HIGH_SURROGATE, Character.MIN_HIGH_SURROGATE)}; + String[] invalidStrings = + new String[] { + newString(Character.MIN_HIGH_SURROGATE), + "foobar" + newString(Character.MIN_HIGH_SURROGATE), + newString(Character.MIN_LOW_SURROGATE), + "foobar" + newString(Character.MIN_LOW_SURROGATE), + newString(Character.MIN_HIGH_SURROGATE, Character.MIN_HIGH_SURROGATE) + }; CodedOutputStream outputWithStream = CodedOutputStream.newInstance(new ByteArrayOutputStream()); CodedOutputStream outputWithArray = CodedOutputStream.newInstance(new byte[10000]); @@ -562,7 +579,7 @@ public class CodedOutputStreamTest extends TestCase { } } - /** Regression test for https://github.com/google/protobuf/issues/292 */ + /** Regression test for https://github.com/protocolbuffers/protobuf/issues/292 */ public void testCorrectExceptionThrowWhenEncodingStringsWithoutEnoughSpace() throws Exception { String testCase = "Foooooooo"; assertEquals( @@ -594,16 +611,17 @@ public class CodedOutputStreamTest extends TestCase { // with ASCII and Unicode characters requiring different UTF-8 byte counts per // char, hence causing the length delimiter varint to sometimes require more // bytes for the Unicode strings than the ASCII string of the same length. - int[] lengths = new int[] { - 0, - 1, - (1 << 4) - 1, // 1 byte for ASCII and Unicode - (1 << 7) - 1, // 1 byte for ASCII, 2 bytes for Unicode - (1 << 11) - 1, // 2 bytes for ASCII and Unicode - (1 << 14) - 1, // 2 bytes for ASCII, 3 bytes for Unicode - (1 << 17) - 1, - // 3 bytes for ASCII and Unicode - }; + int[] lengths = + new int[] { + 0, + 1, + (1 << 4) - 1, // 1 byte for ASCII and Unicode + (1 << 7) - 1, // 1 byte for ASCII, 2 bytes for Unicode + (1 << 11) - 1, // 2 bytes for ASCII and Unicode + (1 << 14) - 1, // 2 bytes for ASCII, 3 bytes for Unicode + (1 << 17) - 1, + // 3 bytes for ASCII and Unicode + }; for (OutputType outputType : OutputType.values()) { for (int i : lengths) { testEncodingOfString(outputType, 'q', i); // 1 byte per char @@ -626,8 +644,8 @@ public class CodedOutputStreamTest extends TestCase { } /** - * Parses the given bytes using writeRawLittleEndian32() and checks - * that the result matches the given value. + * Parses the given bytes using writeRawLittleEndian32() and checks that the result matches the + * given value. */ private static void assertWriteLittleEndian32(byte[] data, int value) throws Exception { for (OutputType outputType : OutputType.values()) { @@ -647,8 +665,8 @@ public class CodedOutputStreamTest extends TestCase { } /** - * Parses the given bytes using writeRawLittleEndian64() and checks - * that the result matches the given value. + * Parses the given bytes using writeRawLittleEndian64() and checks that the result matches the + * given value. */ private static void assertWriteLittleEndian64(byte[] data, long value) throws Exception { for (OutputType outputType : OutputType.values()) { @@ -691,9 +709,8 @@ public class CodedOutputStreamTest extends TestCase { } /** - * Helper to construct a byte array from a bunch of bytes. The inputs are - * actually ints so that I can use hex notation and not get stupid errors - * about precision. + * Helper to construct a byte array from a bunch of bytes. The inputs are actually ints so that I + * can use hex notation and not get stupid errors about precision. */ private static byte[] bytes(int... bytesAsInts) { byte[] bytes = new byte[bytesAsInts.length]; @@ -703,7 +720,7 @@ public class CodedOutputStreamTest extends TestCase { return bytes; } - /** Arrays.asList() does not work with arrays of primitives. :( */ + /** Arrays.asList() does not work with arrays of primitives. :( */ private static List toList(byte[] bytes) { List result = new ArrayList(); for (byte b : bytes) { @@ -717,8 +734,8 @@ public class CodedOutputStreamTest extends TestCase { } /** - * Writes the given value using writeRawVarint32() and writeRawVarint64() and - * checks that the result matches the given bytes. + * Writes the given value using writeRawVarint32() and writeRawVarint64() and checks that the + * result matches the given bytes. */ private static void assertWriteVarint(byte[] data, long value) throws Exception { for (OutputType outputType : OutputType.values()) { diff --git a/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java b/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java index 9c0997c49b..2addde8428 100644 --- a/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java +++ b/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java @@ -41,35 +41,28 @@ import junit.framework.TestCase; * @author birdo@google.com (Roberto Scaramuzzi) */ public class DeprecatedFieldTest extends TestCase { - private String[] deprecatedGetterNames = { - "hasDeprecatedInt32", - "getDeprecatedInt32"}; + private String[] deprecatedGetterNames = {"hasDeprecatedInt32", "getDeprecatedInt32"}; private String[] deprecatedBuilderGetterNames = { - "hasDeprecatedInt32", - "getDeprecatedInt32", - "clearDeprecatedInt32"}; + "hasDeprecatedInt32", "getDeprecatedInt32", "clearDeprecatedInt32" + }; - private String[] deprecatedBuilderSetterNames = { - "setDeprecatedInt32"}; + private String[] deprecatedBuilderSetterNames = {"setDeprecatedInt32"}; public void testDeprecatedField() throws Exception { Class deprecatedFields = TestDeprecatedFields.class; Class deprecatedFieldsBuilder = TestDeprecatedFields.Builder.class; for (String name : deprecatedGetterNames) { Method method = deprecatedFields.getMethod(name); - assertTrue("Method " + name + " should be deprecated", - isDeprecated(method)); + assertTrue("Method " + name + " should be deprecated", isDeprecated(method)); } for (String name : deprecatedBuilderGetterNames) { Method method = deprecatedFieldsBuilder.getMethod(name); - assertTrue("Method " + name + " should be deprecated", - isDeprecated(method)); + assertTrue("Method " + name + " should be deprecated", isDeprecated(method)); } for (String name : deprecatedBuilderSetterNames) { Method method = deprecatedFieldsBuilder.getMethod(name, int.class); - assertTrue("Method " + name + " should be deprecated", - isDeprecated(method)); + assertTrue("Method " + name + " should be deprecated", isDeprecated(method)); } } @@ -77,8 +70,7 @@ public class DeprecatedFieldTest extends TestCase { Class oneofCase = TestDeprecatedFields.OneofFieldsCase.class; String name = "DEPRECATED_INT32_IN_ONEOF"; java.lang.reflect.Field enumValue = oneofCase.getField(name); - assertTrue("Enum value " + name + " should be deprecated.", - isDeprecated(enumValue)); + assertTrue("Enum value " + name + " should be deprecated.", isDeprecated(enumValue)); } private boolean isDeprecated(AnnotatedElement annotated) { diff --git a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java index b60cd62088..74d42c6702 100644 --- a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java +++ b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java @@ -30,6 +30,11 @@ package com.google.protobuf; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; + +import protobuf_unittest.NestedExtension; +import protobuf_unittest.NonNestedExtension; import com.google.protobuf.DescriptorProtos.DescriptorProto; import com.google.protobuf.DescriptorProtos.EnumDescriptorProto; import com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto; @@ -79,8 +84,7 @@ public class DescriptorsTest extends TestCase { private static final Object STATIC_INIT_TEST = FieldDescriptor.Type.BOOL; public void testFieldTypeEnumMapping() throws Exception { - assertEquals(FieldDescriptor.Type.values().length, - FieldDescriptorProto.Type.values().length); + assertEquals(FieldDescriptor.Type.values().length, FieldDescriptorProto.Type.values().length); for (FieldDescriptor.Type type : FieldDescriptor.Type.values()) { FieldDescriptorProto.Type protoType = type.toProto(); assertEquals("TYPE_" + type.name(), protoType.name()); @@ -95,11 +99,9 @@ public class DescriptorsTest extends TestCase { assertEquals("protobuf_unittest", file.getPackage()); assertEquals("UnittestProto", file.getOptions().getJavaOuterClassname()); - assertEquals("google/protobuf/unittest.proto", - file.toProto().getName()); + assertEquals("google/protobuf/unittest.proto", file.toProto().getName()); - assertEquals(Arrays.asList(UnittestImport.getDescriptor()), - file.getDependencies()); + assertEquals(Arrays.asList(UnittestImport.getDescriptor()), file.getDependencies()); Descriptor messageType = TestAllTypes.getDescriptor(); assertEquals(messageType, file.getMessageTypes().get(0)); @@ -115,9 +117,9 @@ public class DescriptorsTest extends TestCase { assertEquals(enumType, file.findEnumTypeByName("ForeignEnum")); assertNull(file.findEnumTypeByName("NoSuchType")); assertNull(file.findEnumTypeByName("protobuf_unittest.ForeignEnum")); - assertEquals(Arrays.asList(ImportEnum.getDescriptor(), - ImportEnumForMap.getDescriptor()), - UnittestImport.getDescriptor().getEnumTypes()); + assertEquals( + Arrays.asList(ImportEnum.getDescriptor(), ImportEnumForMap.getDescriptor()), + UnittestImport.getDescriptor().getEnumTypes()); for (int i = 0; i < file.getEnumTypes().size(); i++) { assertEquals(i, file.getEnumTypes().get(i).getIndex()); } @@ -127,22 +129,17 @@ public class DescriptorsTest extends TestCase { assertEquals(service, file.findServiceByName("TestService")); assertNull(file.findServiceByName("NoSuchType")); assertNull(file.findServiceByName("protobuf_unittest.TestService")); - assertEquals(Collections.emptyList(), - UnittestImport.getDescriptor().getServices()); + assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getServices()); for (int i = 0; i < file.getServices().size(); i++) { assertEquals(i, file.getServices().get(i).getIndex()); } - FieldDescriptor extension = - UnittestProto.optionalInt32Extension.getDescriptor(); + FieldDescriptor extension = UnittestProto.optionalInt32Extension.getDescriptor(); assertEquals(extension, file.getExtensions().get(0)); - assertEquals(extension, - file.findExtensionByName("optional_int32_extension")); + assertEquals(extension, file.findExtensionByName("optional_int32_extension")); assertNull(file.findExtensionByName("no_such_ext")); - assertNull(file.findExtensionByName( - "protobuf_unittest.optional_int32_extension")); - assertEquals(Collections.emptyList(), - UnittestImport.getDescriptor().getExtensions()); + assertNull(file.findExtensionByName("protobuf_unittest.optional_int32_extension")); + assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getExtensions()); for (int i = 0; i < file.getExtensions().size(); i++) { assertEquals(i, file.getExtensions().get(i).getIndex()); } @@ -156,13 +153,11 @@ public class DescriptorsTest extends TestCase { assertEquals("protobuf_unittest.TestAllTypes", messageType.getFullName()); assertEquals(UnittestProto.getDescriptor(), messageType.getFile()); assertNull(messageType.getContainingType()); - assertEquals(DescriptorProtos.MessageOptions.getDefaultInstance(), - messageType.getOptions()); + assertEquals(DescriptorProtos.MessageOptions.getDefaultInstance(), messageType.getOptions()); assertEquals("TestAllTypes", messageType.toProto().getName()); assertEquals("NestedMessage", nestedType.getName()); - assertEquals("protobuf_unittest.TestAllTypes.NestedMessage", - nestedType.getFullName()); + assertEquals("protobuf_unittest.TestAllTypes.NestedMessage", nestedType.getFullName()); assertEquals(UnittestProto.getDescriptor(), nestedType.getFile()); assertEquals(messageType, nestedType.getContainingType()); @@ -194,36 +189,28 @@ public class DescriptorsTest extends TestCase { public void testFieldDescriptor() throws Exception { Descriptor messageType = TestAllTypes.getDescriptor(); - FieldDescriptor primitiveField = - messageType.findFieldByName("optional_int32"); - FieldDescriptor enumField = - messageType.findFieldByName("optional_nested_enum"); - FieldDescriptor messageField = - messageType.findFieldByName("optional_foreign_message"); - FieldDescriptor cordField = - messageType.findFieldByName("optional_cord"); - FieldDescriptor extension = - UnittestProto.optionalInt32Extension.getDescriptor(); + FieldDescriptor primitiveField = messageType.findFieldByName("optional_int32"); + FieldDescriptor enumField = messageType.findFieldByName("optional_nested_enum"); + FieldDescriptor messageField = messageType.findFieldByName("optional_foreign_message"); + FieldDescriptor cordField = messageType.findFieldByName("optional_cord"); + FieldDescriptor extension = UnittestProto.optionalInt32Extension.getDescriptor(); FieldDescriptor nestedExtension = TestRequired.single.getDescriptor(); assertEquals("optional_int32", primitiveField.getName()); - assertEquals("protobuf_unittest.TestAllTypes.optional_int32", - primitiveField.getFullName()); + assertEquals("protobuf_unittest.TestAllTypes.optional_int32", primitiveField.getFullName()); assertEquals(1, primitiveField.getNumber()); assertEquals(messageType, primitiveField.getContainingType()); assertEquals(UnittestProto.getDescriptor(), primitiveField.getFile()); assertEquals(FieldDescriptor.Type.INT32, primitiveField.getType()); assertEquals(FieldDescriptor.JavaType.INT, primitiveField.getJavaType()); - assertEquals(DescriptorProtos.FieldOptions.getDefaultInstance(), - primitiveField.getOptions()); + assertEquals(DescriptorProtos.FieldOptions.getDefaultInstance(), primitiveField.getOptions()); assertFalse(primitiveField.isExtension()); assertEquals("optional_int32", primitiveField.toProto().getName()); assertEquals("optional_nested_enum", enumField.getName()); assertEquals(FieldDescriptor.Type.ENUM, enumField.getType()); assertEquals(FieldDescriptor.JavaType.ENUM, enumField.getJavaType()); - assertEquals(TestAllTypes.NestedEnum.getDescriptor(), - enumField.getEnumType()); + assertEquals(TestAllTypes.NestedEnum.getDescriptor(), enumField.getEnumType()); assertEquals("optional_foreign_message", messageField.getName()); assertEquals(FieldDescriptor.Type.MESSAGE, messageField.getType()); @@ -233,38 +220,29 @@ public class DescriptorsTest extends TestCase { assertEquals("optional_cord", cordField.getName()); assertEquals(FieldDescriptor.Type.STRING, cordField.getType()); assertEquals(FieldDescriptor.JavaType.STRING, cordField.getJavaType()); - assertEquals(DescriptorProtos.FieldOptions.CType.CORD, - cordField.getOptions().getCtype()); + assertEquals(DescriptorProtos.FieldOptions.CType.CORD, cordField.getOptions().getCtype()); assertEquals("optional_int32_extension", extension.getName()); - assertEquals("protobuf_unittest.optional_int32_extension", - extension.getFullName()); + assertEquals("protobuf_unittest.optional_int32_extension", extension.getFullName()); assertEquals(1, extension.getNumber()); - assertEquals(TestAllExtensions.getDescriptor(), - extension.getContainingType()); + assertEquals(TestAllExtensions.getDescriptor(), extension.getContainingType()); assertEquals(UnittestProto.getDescriptor(), extension.getFile()); assertEquals(FieldDescriptor.Type.INT32, extension.getType()); assertEquals(FieldDescriptor.JavaType.INT, extension.getJavaType()); - assertEquals(DescriptorProtos.FieldOptions.getDefaultInstance(), - extension.getOptions()); + assertEquals(DescriptorProtos.FieldOptions.getDefaultInstance(), extension.getOptions()); assertTrue(extension.isExtension()); assertEquals(null, extension.getExtensionScope()); assertEquals("optional_int32_extension", extension.toProto().getName()); assertEquals("single", nestedExtension.getName()); - assertEquals("protobuf_unittest.TestRequired.single", - nestedExtension.getFullName()); - assertEquals(TestRequired.getDescriptor(), - nestedExtension.getExtensionScope()); + assertEquals("protobuf_unittest.TestRequired.single", nestedExtension.getFullName()); + assertEquals(TestRequired.getDescriptor(), nestedExtension.getExtensionScope()); } public void testFieldDescriptorLabel() throws Exception { - FieldDescriptor requiredField = - TestRequired.getDescriptor().findFieldByName("a"); - FieldDescriptor optionalField = - TestAllTypes.getDescriptor().findFieldByName("optional_int32"); - FieldDescriptor repeatedField = - TestAllTypes.getDescriptor().findFieldByName("repeated_int32"); + FieldDescriptor requiredField = TestRequired.getDescriptor().findFieldByName("a"); + FieldDescriptor optionalField = TestAllTypes.getDescriptor().findFieldByName("optional_int32"); + FieldDescriptor repeatedField = TestAllTypes.getDescriptor().findFieldByName("repeated_int32"); assertTrue(requiredField.isRequired()); assertFalse(requiredField.isRepeated()); @@ -273,7 +251,7 @@ public class DescriptorsTest extends TestCase { assertFalse(repeatedField.isRequired()); assertTrue(repeatedField.isRepeated()); } - + public void testFieldDescriptorJsonName() throws Exception { FieldDescriptor requiredField = TestRequired.getDescriptor().findFieldByName("a"); FieldDescriptor optionalField = TestAllTypes.getDescriptor().findFieldByName("optional_int32"); @@ -292,9 +270,8 @@ public class DescriptorsTest extends TestCase { d = TestExtremeDefaultValues.getDescriptor(); assertEquals( - ByteString.copyFrom( - "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(Internal.ISO_8859_1)), - d.findFieldByName("escaped_bytes").getDefaultValue()); + ByteString.copyFrom("\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(Internal.ISO_8859_1)), + d.findFieldByName("escaped_bytes").getDefaultValue()); assertEquals(-1, d.findFieldByName("large_uint32").getDefaultValue()); assertEquals(-1L, d.findFieldByName("large_uint64").getDefaultValue()); } @@ -307,12 +284,10 @@ public class DescriptorsTest extends TestCase { assertEquals("protobuf_unittest.ForeignEnum", enumType.getFullName()); assertEquals(UnittestProto.getDescriptor(), enumType.getFile()); assertNull(enumType.getContainingType()); - assertEquals(DescriptorProtos.EnumOptions.getDefaultInstance(), - enumType.getOptions()); + assertEquals(DescriptorProtos.EnumOptions.getDefaultInstance(), enumType.getOptions()); assertEquals("NestedEnum", nestedType.getName()); - assertEquals("protobuf_unittest.TestAllTypes.NestedEnum", - nestedType.getFullName()); + assertEquals("protobuf_unittest.TestAllTypes.NestedEnum", nestedType.getFullName()); assertEquals(UnittestProto.getDescriptor(), nestedType.getFile()); assertEquals(TestAllTypes.getDescriptor(), nestedType.getContainingType()); @@ -339,20 +314,17 @@ public class DescriptorsTest extends TestCase { MethodDescriptor fooMethod = service.getMethods().get(0); assertEquals("Foo", fooMethod.getName()); - assertEquals(UnittestProto.FooRequest.getDescriptor(), - fooMethod.getInputType()); - assertEquals(UnittestProto.FooResponse.getDescriptor(), - fooMethod.getOutputType()); + assertEquals(UnittestProto.FooRequest.getDescriptor(), fooMethod.getInputType()); + assertEquals(UnittestProto.FooResponse.getDescriptor(), fooMethod.getOutputType()); assertEquals(fooMethod, service.findMethodByName("Foo")); MethodDescriptor barMethod = service.getMethods().get(1); assertEquals("Bar", barMethod.getName()); - assertEquals(UnittestProto.BarRequest.getDescriptor(), - barMethod.getInputType()); - assertEquals(UnittestProto.BarResponse.getDescriptor(), - barMethod.getOutputType()); + assertEquals(UnittestProto.BarRequest.getDescriptor(), barMethod.getInputType()); + assertEquals(UnittestProto.BarResponse.getDescriptor(), barMethod.getOutputType()); assertEquals(barMethod, service.findMethodByName("Bar")); + assertNull(service.findMethodByName("NoSuchMethod")); for (int i = 0; i < service.getMethods().size(); i++) { @@ -367,58 +339,53 @@ public class DescriptorsTest extends TestCase { // dependencies are also properly initialized. Descriptor descriptor = TestCustomOptions.TestMessageWithCustomOptionsContainer.getDescriptor() - .findFieldByName("field").getMessageType(); + .findFieldByName("field") + .getMessageType(); - assertTrue( - descriptor.getOptions().hasExtension(UnittestCustomOptions.messageOpt1)); - assertEquals(Integer.valueOf(-56), - descriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1)); + assertTrue(descriptor.getOptions().hasExtension(UnittestCustomOptions.messageOpt1)); + assertEquals( + Integer.valueOf(-56), + descriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1)); FieldDescriptor field = descriptor.findFieldByName("field1"); assertNotNull(field); - assertTrue( - field.getOptions().hasExtension(UnittestCustomOptions.fieldOpt1)); - assertEquals(Long.valueOf(8765432109L), - field.getOptions().getExtension(UnittestCustomOptions.fieldOpt1)); + assertTrue(field.getOptions().hasExtension(UnittestCustomOptions.fieldOpt1)); + assertEquals( + Long.valueOf(8765432109L), + field.getOptions().getExtension(UnittestCustomOptions.fieldOpt1)); OneofDescriptor oneof = descriptor.getOneofs().get(0); assertNotNull(oneof); - assertTrue( - oneof.getOptions().hasExtension(UnittestCustomOptions.oneofOpt1)); - assertEquals(Integer.valueOf(-99), - oneof.getOptions().getExtension(UnittestCustomOptions.oneofOpt1)); + assertTrue(oneof.getOptions().hasExtension(UnittestCustomOptions.oneofOpt1)); + assertEquals( + Integer.valueOf(-99), oneof.getOptions().getExtension(UnittestCustomOptions.oneofOpt1)); EnumDescriptor enumType = - UnittestCustomOptions.TestMessageWithCustomOptions.AnEnum.getDescriptor(); + UnittestCustomOptions.TestMessageWithCustomOptions.AnEnum.getDescriptor(); - assertTrue( - enumType.getOptions().hasExtension(UnittestCustomOptions.enumOpt1)); - assertEquals(Integer.valueOf(-789), - enumType.getOptions().getExtension(UnittestCustomOptions.enumOpt1)); + assertTrue(enumType.getOptions().hasExtension(UnittestCustomOptions.enumOpt1)); + assertEquals( + Integer.valueOf(-789), enumType.getOptions().getExtension(UnittestCustomOptions.enumOpt1)); - ServiceDescriptor service = - UnittestCustomOptions.TestServiceWithCustomOptions.getDescriptor(); + ServiceDescriptor service = UnittestCustomOptions.TestServiceWithCustomOptions.getDescriptor(); - assertTrue( - service.getOptions().hasExtension(UnittestCustomOptions.serviceOpt1)); - assertEquals(Long.valueOf(-9876543210L), - service.getOptions().getExtension(UnittestCustomOptions.serviceOpt1)); + assertTrue(service.getOptions().hasExtension(UnittestCustomOptions.serviceOpt1)); + assertEquals( + Long.valueOf(-9876543210L), + service.getOptions().getExtension(UnittestCustomOptions.serviceOpt1)); MethodDescriptor method = service.findMethodByName("Foo"); assertNotNull(method); - assertTrue( - method.getOptions().hasExtension(UnittestCustomOptions.methodOpt1)); - assertEquals(UnittestCustomOptions.MethodOpt1.METHODOPT1_VAL2, - method.getOptions().getExtension(UnittestCustomOptions.methodOpt1)); + assertTrue(method.getOptions().hasExtension(UnittestCustomOptions.methodOpt1)); + assertEquals( + UnittestCustomOptions.MethodOpt1.METHODOPT1_VAL2, + method.getOptions().getExtension(UnittestCustomOptions.methodOpt1)); } - /** - * Test that the FieldDescriptor.Type enum is the same as the - * WireFormat.FieldType enum. - */ + /** Test that the FieldDescriptor.Type enum is the same as the WireFormat.FieldType enum. */ public void testFieldTypeTablesMatch() throws Exception { FieldDescriptor.Type[] values1 = FieldDescriptor.Type.values(); WireFormat.FieldType[] values2 = WireFormat.FieldType.values(); @@ -430,10 +397,7 @@ public class DescriptorsTest extends TestCase { } } - /** - * Test that the FieldDescriptor.JavaType enum is the same as the - * WireFormat.JavaType enum. - */ + /** Test that the FieldDescriptor.JavaType enum is the same as the WireFormat.JavaType enum. */ public void testJavaTypeTablesMatch() throws Exception { FieldDescriptor.JavaType[] values1 = FieldDescriptor.JavaType.values(); WireFormat.JavaType[] values2 = WireFormat.JavaType.values(); @@ -448,31 +412,29 @@ public class DescriptorsTest extends TestCase { public void testEnormousDescriptor() throws Exception { // The descriptor for this file is larger than 64k, yet it did not cause // a compiler error due to an over-long string literal. - assertTrue( - UnittestEnormousDescriptor.getDescriptor() - .toProto().getSerializedSize() > 65536); + assertTrue(UnittestEnormousDescriptor.getDescriptor().toProto().getSerializedSize() > 65536); } - /** - * Tests that the DescriptorValidationException works as intended. - */ + /** Tests that the DescriptorValidationException works as intended. */ public void testDescriptorValidatorException() throws Exception { - FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() - .setName("foo.proto") - .addMessageType(DescriptorProto.newBuilder() - .setName("Foo") - .addField(FieldDescriptorProto.newBuilder() - .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) - .setType(FieldDescriptorProto.Type.TYPE_INT32) - .setName("foo") - .setNumber(1) - .setDefaultValue("invalid") - .build()) - .build()) - .build(); + FileDescriptorProto fileDescriptorProto = + FileDescriptorProto.newBuilder() + .setName("foo.proto") + .addMessageType( + DescriptorProto.newBuilder() + .setName("Foo") + .addField( + FieldDescriptorProto.newBuilder() + .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) + .setType(FieldDescriptorProto.Type.TYPE_INT32) + .setName("foo") + .setNumber(1) + .setDefaultValue("invalid") + .build()) + .build()) + .build(); try { - Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, - new FileDescriptor[0]); + Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { // Expected; check that the error message contains some useful hints @@ -485,35 +447,39 @@ public class DescriptorsTest extends TestCase { } /** - * Tests the translate/crosslink for an example where a message field's name - * and type name are the same. + * Tests the translate/crosslink for an example where a message field's name and type name are the + * same. */ public void testDescriptorComplexCrosslink() throws Exception { - FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() - .setName("foo.proto") - .addMessageType(DescriptorProto.newBuilder() - .setName("Foo") - .addField(FieldDescriptorProto.newBuilder() - .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) - .setType(FieldDescriptorProto.Type.TYPE_INT32) - .setName("foo") - .setNumber(1) - .build()) - .build()) - .addMessageType(DescriptorProto.newBuilder() - .setName("Bar") - .addField(FieldDescriptorProto.newBuilder() - .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) - .setTypeName("Foo") - .setName("Foo") - .setNumber(1) - .build()) - .build()) - .build(); + FileDescriptorProto fileDescriptorProto = + FileDescriptorProto.newBuilder() + .setName("foo.proto") + .addMessageType( + DescriptorProto.newBuilder() + .setName("Foo") + .addField( + FieldDescriptorProto.newBuilder() + .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) + .setType(FieldDescriptorProto.Type.TYPE_INT32) + .setName("foo") + .setNumber(1) + .build()) + .build()) + .addMessageType( + DescriptorProto.newBuilder() + .setName("Bar") + .addField( + FieldDescriptorProto.newBuilder() + .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) + .setTypeName("Foo") + .setName("Foo") + .setNumber(1) + .build()) + .build()) + .build(); // translate and crosslink FileDescriptor file = - Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, - new FileDescriptor[0]); + Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]); // verify resulting descriptors assertNotNull(file); List msglist = file.getMessageTypes(); @@ -535,62 +501,57 @@ public class DescriptorsTest extends TestCase { } public void testDependencyOrder() throws Exception { - FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() - .setName("foo.proto").build(); - FileDescriptorProto barProto = FileDescriptorProto.newBuilder() - .setName("bar.proto") - .addDependency("foo.proto") - .build(); - FileDescriptorProto bazProto = FileDescriptorProto.newBuilder() - .setName("baz.proto") - .addDependency("foo.proto") - .addDependency("bar.proto") - .addPublicDependency(0) - .addPublicDependency(1) - .build(); - FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, - new FileDescriptor[0]); - FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(barProto, - new FileDescriptor[] {fooFile}); + FileDescriptorProto fooProto = FileDescriptorProto.newBuilder().setName("foo.proto").build(); + FileDescriptorProto barProto = + FileDescriptorProto.newBuilder().setName("bar.proto").addDependency("foo.proto").build(); + FileDescriptorProto bazProto = + FileDescriptorProto.newBuilder() + .setName("baz.proto") + .addDependency("foo.proto") + .addDependency("bar.proto") + .addPublicDependency(0) + .addPublicDependency(1) + .build(); + FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); + FileDescriptor barFile = + Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[] {fooFile}); // Items in the FileDescriptor array can be in any order. - Descriptors.FileDescriptor.buildFrom(bazProto, - new FileDescriptor[] {fooFile, barFile}); - Descriptors.FileDescriptor.buildFrom(bazProto, - new FileDescriptor[] {barFile, fooFile}); + Descriptors.FileDescriptor.buildFrom(bazProto, new FileDescriptor[] {fooFile, barFile}); + Descriptors.FileDescriptor.buildFrom(bazProto, new FileDescriptor[] {barFile, fooFile}); } public void testInvalidPublicDependency() throws Exception { - FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() - .setName("foo.proto").build(); - FileDescriptorProto barProto = FileDescriptorProto.newBuilder() - .setName("boo.proto") - .addDependency("foo.proto") - .addPublicDependency(1) // Error, should be 0. - .build(); - FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, - new FileDescriptor[0]); + FileDescriptorProto fooProto = FileDescriptorProto.newBuilder().setName("foo.proto").build(); + FileDescriptorProto barProto = + FileDescriptorProto.newBuilder() + .setName("boo.proto") + .addDependency("foo.proto") + .addPublicDependency(1) // Error, should be 0. + .build(); + FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); try { - Descriptors.FileDescriptor.buildFrom(barProto, - new FileDescriptor[] {fooFile}); + Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[] {fooFile}); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { - assertTrue( - e.getMessage().indexOf("Invalid public dependency index.") != -1); + assertTrue(e.getMessage().indexOf("Invalid public dependency index.") != -1); } } public void testUnknownFieldsDenied() throws Exception { - FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() - .setName("foo.proto") - .addMessageType(DescriptorProto.newBuilder() - .setName("Foo") - .addField(FieldDescriptorProto.newBuilder() - .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) - .setTypeName("Bar") - .setName("bar") - .setNumber(1))) - .build(); + FileDescriptorProto fooProto = + FileDescriptorProto.newBuilder() + .setName("foo.proto") + .addMessageType( + DescriptorProto.newBuilder() + .setName("Foo") + .addField( + FieldDescriptorProto.newBuilder() + .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) + .setTypeName("Bar") + .setName("bar") + .setNumber(1))) + .build(); try { Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); @@ -602,48 +563,54 @@ public class DescriptorsTest extends TestCase { } public void testUnknownFieldsAllowed() throws Exception { - FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() - .setName("foo.proto") - .addDependency("bar.proto") - .addMessageType(DescriptorProto.newBuilder() - .setName("Foo") - .addField(FieldDescriptorProto.newBuilder() - .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) - .setTypeName("Bar") - .setName("bar") - .setNumber(1))) - .build(); + FileDescriptorProto fooProto = + FileDescriptorProto.newBuilder() + .setName("foo.proto") + .addDependency("bar.proto") + .addMessageType( + DescriptorProto.newBuilder() + .setName("Foo") + .addField( + FieldDescriptorProto.newBuilder() + .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) + .setTypeName("Bar") + .setName("bar") + .setNumber(1))) + .build(); Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0], true); } public void testHiddenDependency() throws Exception { - FileDescriptorProto barProto = FileDescriptorProto.newBuilder() - .setName("bar.proto") - .addMessageType(DescriptorProto.newBuilder().setName("Bar")) - .build(); - FileDescriptorProto forwardProto = FileDescriptorProto.newBuilder() - .setName("forward.proto") - .addDependency("bar.proto") - .build(); - FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() - .setName("foo.proto") - .addDependency("forward.proto") - .addMessageType(DescriptorProto.newBuilder() - .setName("Foo") - .addField(FieldDescriptorProto.newBuilder() - .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) - .setTypeName("Bar") - .setName("bar") - .setNumber(1))) - .build(); - FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom( - barProto, new FileDescriptor[0]); - FileDescriptor forwardFile = Descriptors.FileDescriptor.buildFrom( - forwardProto, new FileDescriptor[] {barFile}); + FileDescriptorProto barProto = + FileDescriptorProto.newBuilder() + .setName("bar.proto") + .addMessageType(DescriptorProto.newBuilder().setName("Bar")) + .build(); + FileDescriptorProto forwardProto = + FileDescriptorProto.newBuilder() + .setName("forward.proto") + .addDependency("bar.proto") + .build(); + FileDescriptorProto fooProto = + FileDescriptorProto.newBuilder() + .setName("foo.proto") + .addDependency("forward.proto") + .addMessageType( + DescriptorProto.newBuilder() + .setName("Foo") + .addField( + FieldDescriptorProto.newBuilder() + .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) + .setTypeName("Bar") + .setName("bar") + .setNumber(1))) + .build(); + FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[0]); + FileDescriptor forwardFile = + Descriptors.FileDescriptor.buildFrom(forwardProto, new FileDescriptor[] {barFile}); try { - Descriptors.FileDescriptor.buildFrom( - fooProto, new FileDescriptor[] {forwardFile}); + Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[] {forwardFile}); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { assertTrue(e.getMessage().indexOf("Bar") != -1); @@ -652,65 +619,67 @@ public class DescriptorsTest extends TestCase { } public void testPublicDependency() throws Exception { - FileDescriptorProto barProto = FileDescriptorProto.newBuilder() - .setName("bar.proto") - .addMessageType(DescriptorProto.newBuilder().setName("Bar")) - .build(); - FileDescriptorProto forwardProto = FileDescriptorProto.newBuilder() - .setName("forward.proto") - .addDependency("bar.proto") - .addPublicDependency(0) - .build(); - FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() - .setName("foo.proto") - .addDependency("forward.proto") - .addMessageType(DescriptorProto.newBuilder() - .setName("Foo") - .addField(FieldDescriptorProto.newBuilder() - .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) - .setTypeName("Bar") - .setName("bar") - .setNumber(1))) - .build(); - FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom( - barProto, new FileDescriptor[0]); - FileDescriptor forwardFile = Descriptors.FileDescriptor.buildFrom( - forwardProto, new FileDescriptor[]{barFile}); - Descriptors.FileDescriptor.buildFrom( - fooProto, new FileDescriptor[] {forwardFile}); + FileDescriptorProto barProto = + FileDescriptorProto.newBuilder() + .setName("bar.proto") + .addMessageType(DescriptorProto.newBuilder().setName("Bar")) + .build(); + FileDescriptorProto forwardProto = + FileDescriptorProto.newBuilder() + .setName("forward.proto") + .addDependency("bar.proto") + .addPublicDependency(0) + .build(); + FileDescriptorProto fooProto = + FileDescriptorProto.newBuilder() + .setName("foo.proto") + .addDependency("forward.proto") + .addMessageType( + DescriptorProto.newBuilder() + .setName("Foo") + .addField( + FieldDescriptorProto.newBuilder() + .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) + .setTypeName("Bar") + .setName("bar") + .setNumber(1))) + .build(); + FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[0]); + FileDescriptor forwardFile = + Descriptors.FileDescriptor.buildFrom(forwardProto, new FileDescriptor[] {barFile}); + Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[] {forwardFile}); } - /** - * Tests the translate/crosslink for an example with a more complex namespace - * referencing. - */ + /** Tests the translate/crosslink for an example with a more complex namespace referencing. */ public void testComplexNamespacePublicDependency() throws Exception { - FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() - .setName("bar.proto") - .setPackage("a.b.c.d.bar.shared") - .addEnumType(EnumDescriptorProto.newBuilder() - .setName("MyEnum") - .addValue(EnumValueDescriptorProto.newBuilder() - .setName("BLAH") - .setNumber(1))) - .build(); - FileDescriptorProto barProto = FileDescriptorProto.newBuilder() - .setName("foo.proto") - .addDependency("bar.proto") - .setPackage("a.b.c.d.foo.shared") - .addMessageType(DescriptorProto.newBuilder() - .setName("MyMessage") - .addField(FieldDescriptorProto.newBuilder() - .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) - .setTypeName("bar.shared.MyEnum") - .setName("MyField") - .setNumber(1))) - .build(); + FileDescriptorProto fooProto = + FileDescriptorProto.newBuilder() + .setName("bar.proto") + .setPackage("a.b.c.d.bar.shared") + .addEnumType( + EnumDescriptorProto.newBuilder() + .setName("MyEnum") + .addValue(EnumValueDescriptorProto.newBuilder().setName("BLAH").setNumber(1))) + .build(); + FileDescriptorProto barProto = + FileDescriptorProto.newBuilder() + .setName("foo.proto") + .addDependency("bar.proto") + .setPackage("a.b.c.d.foo.shared") + .addMessageType( + DescriptorProto.newBuilder() + .setName("MyMessage") + .addField( + FieldDescriptorProto.newBuilder() + .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) + .setTypeName("bar.shared.MyEnum") + .setName("MyField") + .setNumber(1))) + .build(); // translate and crosslink - FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom( - fooProto, new FileDescriptor[0]); - FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom( - barProto, new FileDescriptor[]{fooFile}); + FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); + FileDescriptor barFile = + Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[] {fooFile}); // verify resulting descriptors assertNotNull(barFile); List msglist = barFile.getMessageTypes(); @@ -726,15 +695,13 @@ public class DescriptorsTest extends TestCase { assertTrue(field.getType() == FieldDescriptor.Type.ENUM); assertTrue(field.getEnumType().getName().equals("MyEnum")); assertTrue(field.getEnumType().getFile().getName().equals("bar.proto")); - assertTrue(field.getEnumType().getFile().getPackage().equals( - "a.b.c.d.bar.shared")); + assertTrue(field.getEnumType().getFile().getPackage().equals("a.b.c.d.bar.shared")); } } public void testOneofDescriptor() throws Exception { Descriptor messageType = TestAllTypes.getDescriptor(); - FieldDescriptor field = - messageType.findFieldByName("oneof_nested_message"); + FieldDescriptor field = messageType.findFieldByName("oneof_nested_message"); OneofDescriptor oneofDescriptor = field.getContainingOneof(); assertNotNull(oneofDescriptor); assertSame(oneofDescriptor, messageType.getOneofs().get(0)); @@ -774,36 +741,38 @@ public class DescriptorsTest extends TestCase { } public void testToString() { - assertEquals("protobuf_unittest.TestAllTypes.optional_uint64", - UnittestProto.TestAllTypes.getDescriptor().findFieldByNumber( - UnittestProto.TestAllTypes.OPTIONAL_UINT64_FIELD_NUMBER).toString()); + assertEquals( + "protobuf_unittest.TestAllTypes.optional_uint64", + UnittestProto.TestAllTypes.getDescriptor() + .findFieldByNumber(UnittestProto.TestAllTypes.OPTIONAL_UINT64_FIELD_NUMBER) + .toString()); } public void testPackedEnumField() throws Exception { - FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() - .setName("foo.proto") - .addEnumType(EnumDescriptorProto.newBuilder() - .setName("Enum") - .addValue(EnumValueDescriptorProto.newBuilder() - .setName("FOO") - .setNumber(1) - .build()) - .build()) - .addMessageType(DescriptorProto.newBuilder() - .setName("Message") - .addField(FieldDescriptorProto.newBuilder() - .setName("foo") - .setTypeName("Enum") - .setNumber(1) - .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) - .setOptions(DescriptorProtos.FieldOptions.newBuilder() - .setPacked(true) - .build()) - .build()) - .build()) - .build(); - Descriptors.FileDescriptor.buildFrom( - fileDescriptorProto, new FileDescriptor[0]); + FileDescriptorProto fileDescriptorProto = + FileDescriptorProto.newBuilder() + .setName("foo.proto") + .addEnumType( + EnumDescriptorProto.newBuilder() + .setName("Enum") + .addValue( + EnumValueDescriptorProto.newBuilder().setName("FOO").setNumber(1).build()) + .build()) + .addMessageType( + DescriptorProto.newBuilder() + .setName("Message") + .addField( + FieldDescriptorProto.newBuilder() + .setName("foo") + .setTypeName("Enum") + .setNumber(1) + .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) + .setOptions( + DescriptorProtos.FieldOptions.newBuilder().setPacked(true).build()) + .build()) + .build()) + .build(); + Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]); } public void testFieldJsonName() throws Exception { @@ -816,4 +785,23 @@ public class DescriptorsTest extends TestCase { assertEquals("FIELDNAME5", d.getFields().get(4).getJsonName()); assertEquals("@type", d.getFields().get(5).getJsonName()); } + + public void testExtensionRenamesKeywords() { + assertTrue(NonNestedExtension.if_ instanceof GeneratedMessage.GeneratedExtension); + assertTrue( + NestedExtension.MyNestedExtension.default_ + instanceof GeneratedMessage.GeneratedExtension); + + NonNestedExtension.MessageToBeExtended msg = + NonNestedExtension.MessageToBeExtended.newBuilder() + .setExtension(NonNestedExtension.if_, "!fi") + .build(); + assertEquals("!fi", msg.getExtension(NonNestedExtension.if_)); + + msg = + NonNestedExtension.MessageToBeExtended.newBuilder() + .setExtension(NestedExtension.MyNestedExtension.default_, 8) + .build(); + assertEquals(8, msg.getExtension(NestedExtension.MyNestedExtension.default_).intValue()); + } } diff --git a/java/core/src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java b/java/core/src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java index 36c4611fd5..01a96ba550 100644 --- a/java/core/src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java +++ b/java/core/src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java @@ -43,15 +43,13 @@ import org.junit.runners.JUnit4; public class DiscardUnknownFieldsTest { @Test public void testProto2() throws Exception { + testProto2Message(UnittestProto.TestEmptyMessage.getDefaultInstance()); + testProto2Message(UnittestProto.TestEmptyMessageWithExtensions.getDefaultInstance()); testProto2Message( - UnittestProto.TestEmptyMessage.getDefaultInstance()); + DynamicMessage.getDefaultInstance(UnittestProto.TestEmptyMessage.getDescriptor())); testProto2Message( - UnittestProto.TestEmptyMessageWithExtensions.getDefaultInstance()); - testProto2Message( - DynamicMessage.getDefaultInstance(UnittestProto.TestEmptyMessage.getDescriptor())); - testProto2Message( - DynamicMessage.getDefaultInstance( - UnittestProto.TestEmptyMessageWithExtensions.getDescriptor())); + DynamicMessage.getDefaultInstance( + UnittestProto.TestEmptyMessageWithExtensions.getDescriptor())); } @Test @@ -81,18 +79,16 @@ public class DiscardUnknownFieldsTest { payload.copyTo(copied, 0); payload.copyTo(copied, messageSize); CodedInputStream input = CodedInputStream.newInstance(copied); - { - // Use DiscardUnknownFieldsParser to parse the first payload. - int oldLimit = input.pushLimit(messageSize); - Message parsed = DiscardUnknownFieldsParser.wrap(message.getParserForType()).parseFrom(input); - assertEquals(message.getClass().getName(), 0, parsed.getSerializedSize()); - input.popLimit(oldLimit); - } - { - // Use the normal parser to parse the remaining payload should have unknown fields preserved. - Message parsed = message.getParserForType().parseFrom(input); - assertEquals(message.getClass().getName(), payload, parsed.toByteString()); - } + + // Use DiscardUnknownFieldsParser to parse the first payload. + int oldLimit = input.pushLimit(messageSize); + Message parsed = DiscardUnknownFieldsParser.wrap(message.getParserForType()).parseFrom(input); + assertEquals(message.getClass().getName(), 0, parsed.getSerializedSize()); + input.popLimit(oldLimit); + + // Use the normal parser to parse the remaining payload should have unknown fields preserved. + parsed = message.getParserForType().parseFrom(input); + assertEquals(message.getClass().getName(), payload, parsed.toByteString()); } /** @@ -104,34 +100,18 @@ public class DiscardUnknownFieldsTest { UnknownFieldSet unknownFields = UnknownFieldSet.newBuilder().mergeFrom(payload).build(); Message built = message.newBuilderForType().setUnknownFields(unknownFields).build(); assertEquals(message.getClass().getName(), payload, built.toByteString()); - - } - /** - * {@link Message.Builder#setUnknownFields(UnknownFieldSet)} and {@link - * Message.Builder#mergeUnknownFields(UnknownFieldSet)} should discard the unknown fields. - */ - private static void assertUnknownFieldsInUnknownFieldSetAreDiscarded(Message message) - throws Exception { - UnknownFieldSet unknownFields = UnknownFieldSet.newBuilder().mergeFrom(payload).build(); - Message built = message.newBuilderForType().setUnknownFields(unknownFields).build(); - assertEquals(message.getClass().getName(), 0, built.getSerializedSize()); } private static void assertUnknownFieldsPreserved(MessageLite message) throws Exception { - { - MessageLite parsed = message.getParserForType().parseFrom(payload); - assertEquals(message.getClass().getName(), payload, parsed.toByteString()); - } + MessageLite parsed = message.getParserForType().parseFrom(payload); + assertEquals(message.getClass().getName(), payload, parsed.toByteString()); - { - MessageLite parsed = message.newBuilderForType().mergeFrom(payload).build(); - assertEquals(message.getClass().getName(), payload, parsed.toByteString()); - } + parsed = message.newBuilderForType().mergeFrom(payload).build(); + assertEquals(message.getClass().getName(), payload, parsed.toByteString()); } private static void assertUnknownFieldsExplicitlyDiscarded(Message message) throws Exception { - Message parsed = - DiscardUnknownFieldsParser.wrap(message.getParserForType()).parseFrom(payload); + Message parsed = DiscardUnknownFieldsParser.wrap(message.getParserForType()).parseFrom(payload); assertEquals(message.getClass().getName(), 0, parsed.getSerializedSize()); } diff --git a/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java b/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java index a4c2f5aa89..3a8254aa69 100644 --- a/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java @@ -45,10 +45,8 @@ import junit.framework.TestCase; */ public class DoubleArrayListTest extends TestCase { - private static final DoubleArrayList UNARY_LIST = - newImmutableDoubleArrayList(1); - private static final DoubleArrayList TERTIARY_LIST = - newImmutableDoubleArrayList(1, 2, 3); + private static final DoubleArrayList UNARY_LIST = newImmutableDoubleArrayList(1); + private static final DoubleArrayList TERTIARY_LIST = newImmutableDoubleArrayList(1, 2, 3); private DoubleArrayList list; @@ -225,9 +223,7 @@ public class DoubleArrayListTest extends TestCase { for (int i = 0; i < 6; i++) { list.add(Double.valueOf(5 + i)); } - assertEquals( - asList(0D, 1D, 4D, 2D, 3D, 5D, 6D, 7D, 8D, 9D, 10D), - list); + assertEquals(asList(0D, 1D, 4D, 2D, 3D, 5D, 6D, 7D, 8D, 9D, 10D), list); try { list.add(-1, 5D); @@ -270,6 +266,15 @@ public class DoubleArrayListTest extends TestCase { assertFalse(list.addAll(DoubleArrayList.emptyList())); } + public void testEquals() { + DoubleArrayList list1 = new DoubleArrayList(); + DoubleArrayList list2 = new DoubleArrayList(); + + list1.addDouble(Double.longBitsToDouble(0x7ff0000000000001L)); + list2.addDouble(Double.longBitsToDouble(0x7ff0000000000002L)); + assertEquals(list1, list2); + } + public void testRemove() { list.addAll(TERTIARY_LIST); assertEquals(1D, (double) list.remove(0), 0.0); @@ -298,17 +303,24 @@ public class DoubleArrayListTest extends TestCase { } } - public void testRemoveEndOfCapacity() { - DoubleList toRemove = - DoubleArrayList.emptyList().mutableCopyWithCapacity(1); + public void testRemoveEnd_listAtCapacity() { + DoubleList toRemove = DoubleArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addDouble(3); toRemove.remove(0); assertEquals(0, toRemove.size()); } + public void testRemove_listAtCapacity() { + DoubleList toRemove = DoubleArrayList.emptyList().mutableCopyWithCapacity(2); + toRemove.addDouble(3); + toRemove.addDouble(4); + toRemove.remove(0); + assertEquals(1, toRemove.size()); + assertEquals(4D, (double) toRemove.get(0)); + } + public void testSublistRemoveEndOfCapacity() { - DoubleList toRemove = - DoubleArrayList.emptyList().mutableCopyWithCapacity(1); + DoubleList toRemove = DoubleArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addDouble(3); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); diff --git a/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java b/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java index 346c1e6a72..fe37ea8087 100644 --- a/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java @@ -33,40 +33,40 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.EnumDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.Descriptors.OneofDescriptor; +import protobuf_unittest.UnittestProto; import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllTypes; +import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage; import protobuf_unittest.UnittestProto.TestEmptyMessage; import protobuf_unittest.UnittestProto.TestPackedTypes; import java.util.Arrays; import junit.framework.TestCase; /** - * Unit test for {@link DynamicMessage}. See also {@link MessageTest}, which - * tests some {@link DynamicMessage} functionality. + * Unit test for {@link DynamicMessage}. See also {@link MessageTest}, which tests some {@link + * DynamicMessage} functionality. * * @author kenton@google.com Kenton Varda */ public class DynamicMessageTest extends TestCase { TestUtil.ReflectionTester reflectionTester = - new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); + new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); TestUtil.ReflectionTester extensionsReflectionTester = new TestUtil.ReflectionTester( TestAllExtensions.getDescriptor(), TestUtil.getFullExtensionRegistry()); TestUtil.ReflectionTester packedReflectionTester = - new TestUtil.ReflectionTester(TestPackedTypes.getDescriptor(), null); + new TestUtil.ReflectionTester(TestPackedTypes.getDescriptor(), null); public void testDynamicMessageAccessors() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); reflectionTester.setAllFieldsViaReflection(builder); Message message = builder.build(); reflectionTester.assertAllFieldsSetViaReflection(message); } public void testSettersAfterBuild() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); Message firstMessage = builder.build(); // double build() builder.build(); @@ -85,12 +85,12 @@ public class DynamicMessageTest extends TestCase { } public void testUnknownFields() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestEmptyMessage.getDescriptor()); - builder.setUnknownFields(UnknownFieldSet.newBuilder() - .addField(1, UnknownFieldSet.Field.newBuilder().addVarint(1).build()) - .addField(2, UnknownFieldSet.Field.newBuilder().addFixed32(1).build()) - .build()); + Message.Builder builder = DynamicMessage.newBuilder(TestEmptyMessage.getDescriptor()); + builder.setUnknownFields( + UnknownFieldSet.newBuilder() + .addField(1, UnknownFieldSet.Field.newBuilder().addVarint(1).build()) + .addField(2, UnknownFieldSet.Field.newBuilder().addFixed32(1).build()) + .build()); Message message = builder.build(); assertEquals(2, message.getUnknownFields().asMap().size()); // clone() with unknown fields @@ -105,8 +105,7 @@ public class DynamicMessageTest extends TestCase { } public void testDynamicMessageSettersRejectNull() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); reflectionTester.assertReflectionSettersRejectNull(builder); } @@ -114,22 +113,19 @@ public class DynamicMessageTest extends TestCase { // We don't need to extensively test DynamicMessage's handling of // extensions because, frankly, it doesn't do anything special with them. // It treats them just like any other fields. - Message.Builder builder = - DynamicMessage.newBuilder(TestAllExtensions.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestAllExtensions.getDescriptor()); extensionsReflectionTester.setAllFieldsViaReflection(builder); Message message = builder.build(); extensionsReflectionTester.assertAllFieldsSetViaReflection(message); } public void testDynamicMessageExtensionSettersRejectNull() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestAllExtensions.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestAllExtensions.getDescriptor()); extensionsReflectionTester.assertReflectionSettersRejectNull(builder); } public void testDynamicMessageRepeatedSetters() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); reflectionTester.setAllFieldsViaReflection(builder); reflectionTester.modifyRepeatedFieldsViaReflection(builder); Message message = builder.build(); @@ -137,33 +133,29 @@ public class DynamicMessageTest extends TestCase { } public void testDynamicMessageRepeatedSettersRejectNull() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); reflectionTester.assertReflectionRepeatedSettersRejectNull(builder); } public void testDynamicMessageDefaults() throws Exception { reflectionTester.assertClearViaReflection( - DynamicMessage.getDefaultInstance(TestAllTypes.getDescriptor())); + DynamicMessage.getDefaultInstance(TestAllTypes.getDescriptor())); reflectionTester.assertClearViaReflection( - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()).build()); + DynamicMessage.newBuilder(TestAllTypes.getDescriptor()).build()); } public void testDynamicMessageSerializedSize() throws Exception { TestAllTypes message = TestUtil.getAllSet(); - Message.Builder dynamicBuilder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + Message.Builder dynamicBuilder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); reflectionTester.setAllFieldsViaReflection(dynamicBuilder); Message dynamicMessage = dynamicBuilder.build(); - assertEquals(message.getSerializedSize(), - dynamicMessage.getSerializedSize()); + assertEquals(message.getSerializedSize(), dynamicMessage.getSerializedSize()); } public void testDynamicMessageSerialization() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); reflectionTester.setAllFieldsViaReflection(builder); Message message = builder.build(); @@ -183,8 +175,7 @@ public class DynamicMessageTest extends TestCase { ByteString rawBytes = message.toByteString(); - Message message2 = - DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), rawBytes); + Message message2 = DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), rawBytes); reflectionTester.assertAllFieldsSetViaReflection(message2); // Test Parser interface. @@ -200,14 +191,13 @@ public class DynamicMessageTest extends TestCase { extensionsReflectionTester.assertAllFieldsSetViaReflection(message); // Test Parser interface. - Message message2 = message.getParserForType().parseFrom( - rawBytes, TestUtil.getExtensionRegistry()); + Message message2 = + message.getParserForType().parseFrom(rawBytes, TestUtil.getExtensionRegistry()); extensionsReflectionTester.assertAllFieldsSetViaReflection(message2); } public void testDynamicMessagePackedSerialization() throws Exception { - Message.Builder builder = - DynamicMessage.newBuilder(TestPackedTypes.getDescriptor()); + Message.Builder builder = DynamicMessage.newBuilder(TestPackedTypes.getDescriptor()); packedReflectionTester.setPackedFieldsViaReflection(builder); Message message = builder.build(); @@ -227,8 +217,7 @@ public class DynamicMessageTest extends TestCase { ByteString rawBytes = message.toByteString(); - Message message2 = - DynamicMessage.parseFrom(TestPackedTypes.getDescriptor(), rawBytes); + Message message2 = DynamicMessage.parseFrom(TestPackedTypes.getDescriptor(), rawBytes); packedReflectionTester.assertPackedFieldsSetViaReflection(message2); // Test Parser interface. @@ -236,6 +225,17 @@ public class DynamicMessageTest extends TestCase { packedReflectionTester.assertPackedFieldsSetViaReflection(message3); } + public void testGetBuilderForExtensionField() { + DynamicMessage.Builder builder = DynamicMessage.newBuilder(TestAllExtensions.getDescriptor()); + Message.Builder fieldBuilder = + builder.newBuilderForField(UnittestProto.optionalNestedMessageExtension.getDescriptor()); + final int expected = 7432; + FieldDescriptor field = + NestedMessage.getDescriptor().findFieldByNumber(NestedMessage.BB_FIELD_NUMBER); + fieldBuilder.setField(field, expected); + assertEquals(expected, fieldBuilder.build().getField(field)); + } + public void testDynamicMessageCopy() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); TestUtil.setAllFields(builder); @@ -245,41 +245,38 @@ public class DynamicMessageTest extends TestCase { reflectionTester.assertAllFieldsSetViaReflection(copy); // Test oneof behavior - FieldDescriptor bytesField = - TestAllTypes.getDescriptor().findFieldByName("oneof_bytes"); - FieldDescriptor uint32Field = - TestAllTypes.getDescriptor().findFieldByName("oneof_uint32"); + FieldDescriptor bytesField = TestAllTypes.getDescriptor().findFieldByName("oneof_bytes"); + FieldDescriptor uint32Field = TestAllTypes.getDescriptor().findFieldByName("oneof_uint32"); assertTrue(copy.hasField(bytesField)); assertFalse(copy.hasField(uint32Field)); - DynamicMessage copy2 = - DynamicMessage.newBuilder(message).setField(uint32Field, 123).build(); + DynamicMessage copy2 = DynamicMessage.newBuilder(message).setField(uint32Field, 123).build(); assertFalse(copy2.hasField(bytesField)); assertTrue(copy2.hasField(uint32Field)); assertEquals(123, copy2.getField(uint32Field)); } public void testToBuilder() throws Exception { - DynamicMessage.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + DynamicMessage.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); reflectionTester.setAllFieldsViaReflection(builder); int unknownFieldNum = 9; long unknownFieldVal = 90; - builder.setUnknownFields(UnknownFieldSet.newBuilder() - .addField(unknownFieldNum, - UnknownFieldSet.Field.newBuilder() - .addVarint(unknownFieldVal).build()) - .build()); + builder.setUnknownFields( + UnknownFieldSet.newBuilder() + .addField( + unknownFieldNum, + UnknownFieldSet.Field.newBuilder().addVarint(unknownFieldVal).build()) + .build()); DynamicMessage message = builder.build(); DynamicMessage derived = message.toBuilder().build(); reflectionTester.assertAllFieldsSetViaReflection(derived); - assertEquals(Arrays.asList(unknownFieldVal), + assertEquals( + Arrays.asList(unknownFieldVal), derived.getUnknownFields().getField(unknownFieldNum).getVarintList()); } public void testDynamicOneofMessage() throws Exception { - DynamicMessage.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + DynamicMessage.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); OneofDescriptor oneof = TestAllTypes.getDescriptor().getOneofs().get(0); assertFalse(builder.hasOneof(oneof)); assertSame(null, builder.getOneofFieldDescriptor(oneof)); @@ -292,8 +289,7 @@ public class DynamicMessageTest extends TestCase { DynamicMessage message = builder.buildPartial(); assertTrue(message.hasOneof(oneof)); - DynamicMessage.Builder mergedBuilder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + DynamicMessage.Builder mergedBuilder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); FieldDescriptor mergedField = oneof.getField(0); mergedBuilder.setField(mergedField, 123); assertTrue(mergedBuilder.hasField(mergedField)); @@ -310,15 +306,12 @@ public class DynamicMessageTest extends TestCase { // Regression test for a bug that makes setField() not work for repeated // enum fields. public void testSettersForRepeatedEnumField() throws Exception { - DynamicMessage.Builder builder = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); + DynamicMessage.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); FieldDescriptor repeatedEnumField = - TestAllTypes.getDescriptor().findFieldByName( - "repeated_nested_enum"); + TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum"); EnumDescriptor enumDescriptor = TestAllTypes.NestedEnum.getDescriptor(); builder.setField(repeatedEnumField, enumDescriptor.getValues()); DynamicMessage message = builder.build(); - assertEquals( - enumDescriptor.getValues(), message.getField(repeatedEnumField)); + assertEquals(enumDescriptor.getValues(), message.getField(repeatedEnumField)); } } diff --git a/java/core/src/test/java/com/google/protobuf/EnumTest.java b/java/core/src/test/java/com/google/protobuf/EnumTest.java index 14c7406b3d..80c176a771 100644 --- a/java/core/src/test/java/com/google/protobuf/EnumTest.java +++ b/java/core/src/test/java/com/google/protobuf/EnumTest.java @@ -34,43 +34,43 @@ import com.google.protobuf.UnittestLite.ForeignEnumLite; import com.google.protobuf.UnittestLite.TestAllTypesLite; import protobuf_unittest.UnittestProto.ForeignEnum; import protobuf_unittest.UnittestProto.TestAllTypes; - import junit.framework.TestCase; public class EnumTest extends TestCase { - + public void testForNumber() { ForeignEnum e = ForeignEnum.forNumber(ForeignEnum.FOREIGN_BAR.getNumber()); assertEquals(ForeignEnum.FOREIGN_BAR, e); e = ForeignEnum.forNumber(1000); - assertEquals(null, e); + assertNull(e); } - + public void testForNumber_oneof() { - TestAllTypes.OneofFieldCase e = TestAllTypes.OneofFieldCase.forNumber( - TestAllTypes.OneofFieldCase.ONEOF_NESTED_MESSAGE.getNumber()); + TestAllTypes.OneofFieldCase e = + TestAllTypes.OneofFieldCase.forNumber( + TestAllTypes.OneofFieldCase.ONEOF_NESTED_MESSAGE.getNumber()); assertEquals(TestAllTypes.OneofFieldCase.ONEOF_NESTED_MESSAGE, e); e = TestAllTypes.OneofFieldCase.forNumber(1000); - assertEquals(null, e); + assertNull(e); } - + public void testForNumberLite() { ForeignEnumLite e = ForeignEnumLite.forNumber(ForeignEnumLite.FOREIGN_LITE_BAR.getNumber()); assertEquals(ForeignEnumLite.FOREIGN_LITE_BAR, e); e = ForeignEnumLite.forNumber(1000); - assertEquals(null, e); + assertNull(e); } - + public void testForNumberLite_oneof() { - TestAllTypesLite.OneofFieldCase e = TestAllTypesLite.OneofFieldCase.forNumber( - TestAllTypesLite.OneofFieldCase.ONEOF_NESTED_MESSAGE.getNumber()); + TestAllTypesLite.OneofFieldCase e = + TestAllTypesLite.OneofFieldCase.forNumber( + TestAllTypesLite.OneofFieldCase.ONEOF_NESTED_MESSAGE.getNumber()); assertEquals(TestAllTypesLite.OneofFieldCase.ONEOF_NESTED_MESSAGE, e); e = TestAllTypesLite.OneofFieldCase.forNumber(1000); - assertEquals(null, e); + assertNull(e); } } - diff --git a/java/core/src/test/java/com/google/protobuf/ExperimentalMessageFactory.java b/java/core/src/test/java/com/google/protobuf/ExperimentalMessageFactory.java new file mode 100755 index 0000000000..60c8609cf4 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ExperimentalMessageFactory.java @@ -0,0 +1,40 @@ +// 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. + +package com.google.protobuf; + +/** Interface for a test factory for messages. */ +public interface ExperimentalMessageFactory { + /** Creates a new random message instance. */ + T newMessage(); + + /** Gets the underlying data provider. */ + ExperimentalTestDataProvider dataProvider(); +} diff --git a/java/core/src/test/java/com/google/protobuf/ExperimentalSerializationUtil.java b/java/core/src/test/java/com/google/protobuf/ExperimentalSerializationUtil.java new file mode 100755 index 0000000000..05aaa67521 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ExperimentalSerializationUtil.java @@ -0,0 +1,113 @@ +// 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. + +package com.google.protobuf; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Queue; + +/** Utilities for serialization. */ +public class ExperimentalSerializationUtil { + + /** + * Serializes the given message to a byte array using {@link com.google.protobuf.BinaryWriter}. + */ + public static byte[] toByteArray(T msg) throws IOException { + return toByteArray(msg, Protobuf.getInstance().schemaFor(msg)); + } + + /** + * Serializes the given message to a byte array using {@link com.google.protobuf.BinaryWriter} + * with a customized Schema. + */ + public static byte[] toByteArray(T msg, Schema schema) throws IOException { + BinaryWriter writer = BinaryWriter.newHeapInstance(BufferAllocator.unpooled()); + schema.writeTo(msg, writer); + + byte[] out = new byte[writer.getTotalBytesWritten()]; + int outPos = 0; + Queue buffers = writer.complete(); + while (true) { + AllocatedBuffer buffer = buffers.poll(); + if (buffer == null) { + break; + } + int length = buffer.limit() - buffer.position(); + System.arraycopy( + buffer.array(), buffer.arrayOffset() + buffer.position(), out, outPos, length); + outPos += length; + } + if (out.length != outPos) { + throw new IllegalArgumentException("Failed to serialize test message"); + } + return out; + } + + /** Deserializes a message from the given byte array. */ + public static T fromByteArray(byte[] data, Class messageType) { + if (Android.isOnAndroidDevice()) { + return fromByteArrayFastPath(data, messageType); + } else { + return fromByteArray(data, messageType, ExtensionRegistryLite.getEmptyRegistry()); + } + } + + /** + * Deserializes a message from the given byte array using {@link com.google.protobuf.BinaryReader} + * with an extension registry and a customized Schema. + */ + public static T fromByteArray( + byte[] data, Class messageType, ExtensionRegistryLite extensionRegistry) { + try { + Schema schema = Protobuf.getInstance().schemaFor(messageType); + T msg = schema.newInstance(); + schema.mergeFrom( + msg, BinaryReader.newInstance(ByteBuffer.wrap(data), true), extensionRegistry); + schema.makeImmutable(msg); + return msg; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** Deserializes a lite message from the given byte array using fast path. */ + private static T fromByteArrayFastPath(byte[] data, Class messageType) { + try { + Schema schema = Protobuf.getInstance().schemaFor(messageType); + T msg = schema.newInstance(); + schema.mergeFrom(msg, data, 0, data.length, new ArrayDecoders.Registers()); + schema.makeImmutable(msg); + return msg; + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ExperimentalTestDataProvider.java b/java/core/src/test/java/com/google/protobuf/ExperimentalTestDataProvider.java new file mode 100755 index 0000000000..3ef93ac9f8 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ExperimentalTestDataProvider.java @@ -0,0 +1,189 @@ +// 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. + +package com.google.protobuf; + +import java.util.Random; + +/** Utility class that provides data primitives for filling out protobuf messages. */ +public final class ExperimentalTestDataProvider { + private static final Random RANDOM = new Random(100); + + private final Varint32Provider varint32s = new Varint32Provider(); + private final Varint64Provider varint64s = new Varint64Provider(); + private final int stringLength; + + public ExperimentalTestDataProvider(int stringLength) { + this.stringLength = stringLength; + } + + public double getDouble() { + double value = 0.0; + while (Double.compare(0.0, value) == 0) { + value = RANDOM.nextDouble(); + } + return value; + } + + public float getFloat() { + float value = 0.0f; + while (Float.compare(0.0f, value) == 0) { + value = RANDOM.nextFloat(); + } + return value; + } + + public long getLong() { + return varint64s.getLong(); + } + + public int getInt() { + return varint32s.getInt(); + } + + public boolean getBool() { + return true; + } + + public int getEnum() { + return Math.abs(getInt()) % 3; + } + + public String getString() { + StringBuilder builder = new StringBuilder(stringLength); + for (int i = 0; i < stringLength; ++i) { + builder.append((char) (RANDOM.nextInt('z' - 'a') + 'a')); + } + return builder.toString(); + } + + public ByteString getBytes() { + return ByteString.copyFromUtf8(getString()); + } + + /** + * Iterator over integer values. Uses a simple distribution over 32-bit varints (generally + * favoring smaller values). + */ + private static final class Varint32Provider { + private static final int[][] VALUES = { + new int[] {1, 50, 100, 127}, // 1 byte values + new int[] {128, 500, 10000, 16383}, // 2 bytes values + new int[] {16384, 50000, 1000000, 2097151}, // 3 bytes values + new int[] {2097152, 10000000, 200000000, 268435455}, // 4 bytes values + new int[] {268435456, 0x30000000, 0x7FFFFFFF, 0xFFFFFFFF} // 5 bytes values + }; + + /** Number of samples that should be taken from each value array. */ + private static final int[] NUM_SAMPLES = {3, 2, 1, 1, 2}; + + /** + * The index into the {@link #VALUES} array that identifies the list of samples currently being + * iterated over. + */ + private int listIndex; + + /** The index of the next sample within a list. */ + private int sampleIndex; + + /** The number of successive samples that have been taken from the current list. */ + private int samplesTaken; + + public int getInt() { + if (samplesTaken++ > NUM_SAMPLES[listIndex]) { + // Done taking samples from this list. Go to the next one. + listIndex = (listIndex + 1) % VALUES.length; + sampleIndex = 0; + samplesTaken = 0; + } + + int value = VALUES[listIndex][sampleIndex]; + + // All lists are exactly 4 long (i.e. power of 2), so we can optimize the mod operation + // with masking. + sampleIndex = (sampleIndex + 1) & 3; + + return value; + } + } + + /** + * Iterator over integer values. Uses a simple distribution over 64-bit varints (generally + * favoring smaller values). + */ + private static final class Varint64Provider { + private static final long[][] VALUES = { + new long[] {1, 50, 100, 127}, + new long[] {128, 500, 10000, 16383}, + new long[] {16384, 50000, 1000000, 2097151}, + new long[] {2097152, 10000000, 200000000, 268435455}, + new long[] {268435456, 0x30000000, 0x7FFFFFFF, 34359738367L}, + new long[] {34359738368L, 2000000000000L, 4000000000000L, 4398046511103L}, + new long[] {4398046511104L, 200000000000000L, 500000000000000L, 562949953421311L}, + new long[] {0x4000000000000L, 0x5000000000000L, 0x6000000000000L, 0x0FFFFFFFFFFFFFFL}, + new long[] {0x100000000000000L, 0x3FFFFFFFFFFFFFFFL, 0x5FFFFFFFFFFFFFFL, 0x7FFFFFFFFFFFFFFFL}, + new long[] { + 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL + } + }; + + /** Number of samples that should be taken from each value array. */ + private static final int[] NUM_SAMPLES = {4, 2, 2, 1, 1, 1, 1, 2, 2, 4}; + + /** + * The index into the {@link #VALUES} array that identifies the list of samples currently being + * iterated over. + */ + private int listIndex; + + /** The index of the next sample within a list. */ + private int sampleIndex; + + /** The number of successive samples that have been taken from the current list. */ + private int samplesTaken; + + public long getLong() { + if (samplesTaken++ > NUM_SAMPLES[listIndex]) { + // Done taking samples from this list. Go to the next one. + listIndex = (listIndex + 1) % VALUES.length; + sampleIndex = 0; + samplesTaken = 0; + } + + long value = VALUES[listIndex][sampleIndex]; + + // All lists are exactly 4 long (i.e. power of 2), so we can optimize the mod operation + // with masking. + sampleIndex = (sampleIndex + 1) & 3; + + return value; + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java b/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java index 6157e58966..c6aed074fe 100644 --- a/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java +++ b/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java @@ -47,31 +47,31 @@ import junit.framework.TestSuite; * creates. * *

This test simulates the runtime behaviour of the ExtensionRegistryFactory by delegating test - * definitions to two inner classes {@link InnerTest} and {@link InnerLiteTest}, the latter of - * which is executed using a custom ClassLoader, simulating the ProtoLite environment. + * definitions to two inner classes {@link InnerTest} and {@link InnerLiteTest}, the latter of which + * is executed using a custom ClassLoader, simulating the ProtoLite environment. * - *

The test mechanism employed here is based on the pattern in - * {@code com.google.common.util.concurrent.AbstractFutureFallbackAtomicHelperTest} + *

The test mechanism employed here is based on the pattern in {@code + * com.google.common.util.concurrent.AbstractFutureFallbackAtomicHelperTest} */ public class ExtensionRegistryFactoryTest extends TestCase { // A classloader which blacklists some non-Lite classes. private static final ClassLoader LITE_CLASS_LOADER = getLiteOnlyClassLoader(); - /** - * Defines the set of test methods which will be run. - */ + /** Defines the set of test methods which will be run. */ static interface RegistryTests { void testCreate(); + void testEmpty(); + void testIsFullRegistry(); + void testAdd(); + void testAdd_immutable(); } - /** - * Test implementations for the non-Lite usage of ExtensionRegistryFactory. - */ + /** Test implementations for the non-Lite usage of ExtensionRegistryFactory. */ public static class InnerTest implements RegistryTests { @Override @@ -108,20 +108,24 @@ public class ExtensionRegistryFactoryTest extends TestCase { ExtensionRegistry fullRegistry1 = (ExtensionRegistry) registry1; ExtensionRegistry fullRegistry2 = (ExtensionRegistry) registry2; - assertTrue("Test is using a non-lite extension", + assertTrue( + "Test is using a non-lite extension", GeneratedMessageLite.GeneratedExtension.class.isAssignableFrom( NonNestedExtensionLite.nonNestedExtensionLite.getClass())); - assertNull("Extension is not registered in masqueraded full registry", + assertNull( + "Extension is not registered in masqueraded full registry", fullRegistry1.findImmutableExtensionByName("protobuf_unittest.nonNestedExtension")); GeneratedMessageLite.GeneratedExtension - extension = registry1.findLiteExtensionByNumber( - NonNestedExtensionLite.MessageLiteToBeExtended.getDefaultInstance(), 1); + extension = + registry1.findLiteExtensionByNumber( + NonNestedExtensionLite.MessageLiteToBeExtended.getDefaultInstance(), 1); assertNotNull("Extension registered in lite registry", extension); - assertTrue("Test is using a non-lite extension", - GeneratedMessage.GeneratedExtension.class.isAssignableFrom( - NonNestedExtension.nonNestedExtension.getClass())); - assertNotNull("Extension is registered in masqueraded full registry", + assertTrue( + "Test is using a non-lite extension", + Extension.class.isAssignableFrom(NonNestedExtension.nonNestedExtension.getClass())); + assertNotNull( + "Extension is registered in masqueraded full registry", fullRegistry2.findImmutableExtensionByName("protobuf_unittest.nonNestedExtension")); } @@ -131,27 +135,29 @@ public class ExtensionRegistryFactoryTest extends TestCase { try { NonNestedExtensionLite.registerAllExtensions(registry1); fail(); - } catch (UnsupportedOperationException expected) {} + } catch (UnsupportedOperationException expected) { + } try { registry1.add(NonNestedExtensionLite.nonNestedExtensionLite); fail(); - } catch (UnsupportedOperationException expected) {} + } catch (UnsupportedOperationException expected) { + } ExtensionRegistryLite registry2 = ExtensionRegistryLite.newInstance().getUnmodifiable(); try { NonNestedExtension.registerAllExtensions((ExtensionRegistry) registry2); fail(); - } catch (IllegalArgumentException expected) {} + } catch (IllegalArgumentException expected) { + } try { registry2.add(NonNestedExtension.nonNestedExtension); fail(); - } catch (IllegalArgumentException expected) {} + } catch (IllegalArgumentException expected) { + } } } - /** - * Test implementations for the Lite usage of ExtensionRegistryFactory. - */ + /** Test implementations for the Lite usage of ExtensionRegistryFactory. */ public static final class InnerLiteTest implements RegistryTests { @Override @@ -180,8 +186,9 @@ public class ExtensionRegistryFactoryTest extends TestCase { ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); NonNestedExtensionLite.registerAllExtensions(registry); GeneratedMessageLite.GeneratedExtension - extension = registry.findLiteExtensionByNumber( - NonNestedExtensionLite.MessageLiteToBeExtended.getDefaultInstance(), 1); + extension = + registry.findLiteExtensionByNumber( + NonNestedExtensionLite.MessageLiteToBeExtended.getDefaultInstance(), 1); assertNotNull("Extension is registered in Lite registry", extension); } @@ -191,13 +198,12 @@ public class ExtensionRegistryFactoryTest extends TestCase { try { NonNestedExtensionLite.registerAllExtensions(registry); fail(); - } catch (UnsupportedOperationException expected) {} + } catch (UnsupportedOperationException expected) { + } } } - /** - * Defines a suite of tests which the JUnit3 runner retrieves by reflection. - */ + /** Defines a suite of tests which the JUnit3 runner retrieves by reflection. */ public static Test suite() { TestSuite suite = new TestSuite(); for (Method method : RegistryTests.class.getMethods()) { @@ -231,12 +237,12 @@ public class ExtensionRegistryFactoryTest extends TestCase { classLoader.loadClass(ExtensionRegistryFactory.class.getName()); Class test = classLoader.loadClass(testClass.getName()); String testName = getName(); - test.getMethod(testName).invoke(test.newInstance()); + test.getMethod(testName).invoke(test.getDeclaredConstructor().newInstance()); } /** - * Constructs a custom ClassLoader blacklisting the classes which are inspected in the SUT - * to determine the Lite/non-Lite runtime. + * Constructs a custom ClassLoader blacklisting the classes which are inspected in the SUT to + * determine the Lite/non-Lite runtime. */ private static ClassLoader getLiteOnlyClassLoader() { ClassLoader testClassLoader = ExtensionRegistryFactoryTest.class.getClassLoader(); @@ -250,8 +256,8 @@ public class ExtensionRegistryFactoryTest extends TestCase { // Construct a URLClassLoader delegating to the system ClassLoader, and looking up classes // in jar files based on the URLs already configured for this test's UrlClassLoader. // Certain classes throw a ClassNotFoundException by design. - return new URLClassLoader(((URLClassLoader) testClassLoader).getURLs(), - ClassLoader.getSystemClassLoader()) { + return new URLClassLoader( + ((URLClassLoader) testClassLoader).getURLs(), ClassLoader.getSystemClassLoader()) { @Override public Class loadClass(String name, boolean resolve) throws ClassNotFoundException { if (classNamesNotInLite.contains(name)) { @@ -266,7 +272,10 @@ public class ExtensionRegistryFactoryTest extends TestCase { resolveClass(loadedClass); } } - } catch (ClassNotFoundException e) { + } catch (ClassNotFoundException | SecurityException e) { + // Java 8+ would throw a SecurityException if we attempt to find a loaded class from + // java.lang.* package. We don't really care about those anyway, so just delegate to the + // parent class loader. loadedClass = super.loadClass(name, resolve); } return loadedClass; diff --git a/java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java b/java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java index 42da5bb38b..85a2fe086e 100644 --- a/java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java +++ b/java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java @@ -38,12 +38,10 @@ import com.google.protobuf.FieldPresenceTestProto.TestAllTypes; import com.google.protobuf.FieldPresenceTestProto.TestOptionalFieldsOnly; import com.google.protobuf.FieldPresenceTestProto.TestRepeatedFieldsOnly; import protobuf_unittest.UnittestProto; - import junit.framework.TestCase; /** - * Unit tests for protos that doesn't support field presence test for optional - * non-message fields. + * Unit tests for protos that doesn't support field presence test for optional non-message fields. */ public class FieldPresenceTest extends TestCase { private static boolean hasMethod(Class clazz, String name) { @@ -59,9 +57,7 @@ public class FieldPresenceTest extends TestCase { } private static void assertHasMethodRemoved( - Class classWithFieldPresence, - Class classWithoutFieldPresence, - String camelName) { + Class classWithFieldPresence, Class classWithoutFieldPresence, String camelName) { assertTrue(hasMethod(classWithFieldPresence, "get" + camelName)); assertTrue(hasMethod(classWithFieldPresence, "has" + camelName)); assertTrue(hasMethod(classWithoutFieldPresence, "get" + camelName)); @@ -70,72 +66,38 @@ public class FieldPresenceTest extends TestCase { public void testHasMethod() { // Optional non-message fields don't have a hasFoo() method generated. + assertHasMethodRemoved(UnittestProto.TestAllTypes.class, TestAllTypes.class, "OptionalInt32"); + assertHasMethodRemoved(UnittestProto.TestAllTypes.class, TestAllTypes.class, "OptionalString"); + assertHasMethodRemoved(UnittestProto.TestAllTypes.class, TestAllTypes.class, "OptionalBytes"); assertHasMethodRemoved( - UnittestProto.TestAllTypes.class, - TestAllTypes.class, - "OptionalInt32"); - assertHasMethodRemoved( - UnittestProto.TestAllTypes.class, - TestAllTypes.class, - "OptionalString"); - assertHasMethodRemoved( - UnittestProto.TestAllTypes.class, - TestAllTypes.class, - "OptionalBytes"); - assertHasMethodRemoved( - UnittestProto.TestAllTypes.class, - TestAllTypes.class, - "OptionalNestedEnum"); + UnittestProto.TestAllTypes.class, TestAllTypes.class, "OptionalNestedEnum"); assertHasMethodRemoved( - UnittestProto.TestAllTypes.Builder.class, - TestAllTypes.Builder.class, - "OptionalInt32"); + UnittestProto.TestAllTypes.Builder.class, TestAllTypes.Builder.class, "OptionalInt32"); assertHasMethodRemoved( - UnittestProto.TestAllTypes.Builder.class, - TestAllTypes.Builder.class, - "OptionalString"); + UnittestProto.TestAllTypes.Builder.class, TestAllTypes.Builder.class, "OptionalString"); assertHasMethodRemoved( - UnittestProto.TestAllTypes.Builder.class, - TestAllTypes.Builder.class, - "OptionalBytes"); + UnittestProto.TestAllTypes.Builder.class, TestAllTypes.Builder.class, "OptionalBytes"); assertHasMethodRemoved( - UnittestProto.TestAllTypes.Builder.class, - TestAllTypes.Builder.class, - "OptionalNestedEnum"); + UnittestProto.TestAllTypes.Builder.class, TestAllTypes.Builder.class, "OptionalNestedEnum"); // message fields still have the hasFoo() method generated. - assertFalse(TestAllTypes.newBuilder().build().hasOptionalNestedMessage()); + assertFalse(TestAllTypes.getDefaultInstance().hasOptionalNestedMessage()); assertFalse(TestAllTypes.newBuilder().hasOptionalNestedMessage()); // oneof fields don't have hasFoo() methods for non-message types. - assertHasMethodRemoved( - UnittestProto.TestAllTypes.class, - TestAllTypes.class, - "OneofUint32"); - assertHasMethodRemoved( - UnittestProto.TestAllTypes.class, - TestAllTypes.class, - "OneofString"); - assertHasMethodRemoved( - UnittestProto.TestAllTypes.class, - TestAllTypes.class, - "OneofBytes"); - assertFalse(TestAllTypes.newBuilder().build().hasOneofNestedMessage()); + assertHasMethodRemoved(UnittestProto.TestAllTypes.class, TestAllTypes.class, "OneofUint32"); + assertHasMethodRemoved(UnittestProto.TestAllTypes.class, TestAllTypes.class, "OneofString"); + assertHasMethodRemoved(UnittestProto.TestAllTypes.class, TestAllTypes.class, "OneofBytes"); + assertFalse(TestAllTypes.getDefaultInstance().hasOneofNestedMessage()); assertFalse(TestAllTypes.newBuilder().hasOneofNestedMessage()); assertHasMethodRemoved( - UnittestProto.TestAllTypes.Builder.class, - TestAllTypes.Builder.class, - "OneofUint32"); + UnittestProto.TestAllTypes.Builder.class, TestAllTypes.Builder.class, "OneofUint32"); assertHasMethodRemoved( - UnittestProto.TestAllTypes.Builder.class, - TestAllTypes.Builder.class, - "OneofString"); + UnittestProto.TestAllTypes.Builder.class, TestAllTypes.Builder.class, "OneofString"); assertHasMethodRemoved( - UnittestProto.TestAllTypes.Builder.class, - TestAllTypes.Builder.class, - "OneofBytes"); + UnittestProto.TestAllTypes.Builder.class, TestAllTypes.Builder.class, "OneofBytes"); } public void testOneofEquals() throws Exception { @@ -200,10 +162,10 @@ public class FieldPresenceTest extends TestCase { assertEquals(TestAllTypes.NestedEnum.BAR, message.getOptionalNestedEnum()); // equals()/hashCode() should produce the same results. - TestAllTypes empty = TestAllTypes.newBuilder().build(); + TestAllTypes empty = TestAllTypes.getDefaultInstance(); message = builder.build(); - assertTrue(empty.equals(message)); - assertTrue(message.equals(empty)); + assertEquals(message, empty); + assertEquals(empty, message); assertEquals(empty.hashCode(), message.hashCode()); } @@ -215,7 +177,7 @@ public class FieldPresenceTest extends TestCase { FieldDescriptor optionalNestedEnumField = descriptor.findFieldByName("optional_nested_enum"); // Field not present. - TestAllTypes message = TestAllTypes.newBuilder().build(); + TestAllTypes message = TestAllTypes.getDefaultInstance(); assertFalse(message.hasField(optionalInt32Field)); assertFalse(message.hasField(optionalStringField)); assertFalse(message.hasField(optionalBytesField)); @@ -223,12 +185,13 @@ public class FieldPresenceTest extends TestCase { assertEquals(0, message.getAllFields().size()); // Field set to default value is seen as not present. - message = TestAllTypes.newBuilder() - .setOptionalInt32(0) - .setOptionalString("") - .setOptionalBytes(ByteString.EMPTY) - .setOptionalNestedEnum(TestAllTypes.NestedEnum.FOO) - .build(); + message = + TestAllTypes.newBuilder() + .setOptionalInt32(0) + .setOptionalString("") + .setOptionalBytes(ByteString.EMPTY) + .setOptionalNestedEnum(TestAllTypes.NestedEnum.FOO) + .build(); assertFalse(message.hasField(optionalInt32Field)); assertFalse(message.hasField(optionalStringField)); assertFalse(message.hasField(optionalBytesField)); @@ -236,12 +199,13 @@ public class FieldPresenceTest extends TestCase { assertEquals(0, message.getAllFields().size()); // Field set to non-default value is seen as present. - message = TestAllTypes.newBuilder() - .setOptionalInt32(1) - .setOptionalString("x") - .setOptionalBytes(ByteString.copyFromUtf8("y")) - .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR) - .build(); + message = + TestAllTypes.newBuilder() + .setOptionalInt32(1) + .setOptionalString("x") + .setOptionalBytes(ByteString.copyFromUtf8("y")) + .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR) + .build(); assertTrue(message.hasField(optionalInt32Field)); assertTrue(message.hasField(optionalStringField)); assertTrue(message.hasField(optionalBytesField)); @@ -284,7 +248,9 @@ public class FieldPresenceTest extends TestCase { assertEquals(4, message.getAllFields().size()); // Field set to default value is seen as not present. - message = message.toBuilder() + message = + message + .toBuilder() .setField(optionalInt32Field, 0) .setField(optionalStringField, "") .setField(optionalBytesField, ByteString.EMPTY) @@ -302,8 +268,7 @@ public class FieldPresenceTest extends TestCase { assertFalse(builder.hasOptionalNestedMessage()); assertFalse(builder.build().hasOptionalNestedMessage()); - TestAllTypes.NestedMessage.Builder nestedBuilder = - builder.getOptionalNestedMessageBuilder(); + TestAllTypes.NestedMessage.Builder nestedBuilder = builder.getOptionalNestedMessageBuilder(); assertTrue(builder.hasOptionalNestedMessage()); assertTrue(builder.build().hasOptionalNestedMessage()); @@ -341,8 +306,7 @@ public class FieldPresenceTest extends TestCase { assertTrue(message.hasOptionalNestedMessage()); assertEquals(0, message.getOptionalNestedMessage().getValue()); // The oneof field set to its default value is also present. - assertEquals( - TestAllTypes.OneofFieldCase.ONEOF_INT32, message.getOneofFieldCase()); + assertEquals(TestAllTypes.OneofFieldCase.ONEOF_INT32, message.getOneofFieldCase()); } // Regression test for b/16173397 @@ -376,8 +340,7 @@ public class FieldPresenceTest extends TestCase { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); // Test optional proto2 message fields. - UnittestProto.TestRequired.Builder proto2Builder = - builder.getOptionalProto2MessageBuilder(); + UnittestProto.TestRequired.Builder proto2Builder = builder.getOptionalProto2MessageBuilder(); assertFalse(builder.isInitialized()); assertFalse(builder.buildPartial().isInitialized()); diff --git a/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java b/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java index 38eccc93b2..77a2839b90 100644 --- a/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java @@ -45,10 +45,8 @@ import junit.framework.TestCase; */ public class FloatArrayListTest extends TestCase { - private static final FloatArrayList UNARY_LIST = - newImmutableFloatArrayList(1); - private static final FloatArrayList TERTIARY_LIST = - newImmutableFloatArrayList(1, 2, 3); + private static final FloatArrayList UNARY_LIST = newImmutableFloatArrayList(1); + private static final FloatArrayList TERTIARY_LIST = newImmutableFloatArrayList(1, 2, 3); private FloatArrayList list; @@ -225,9 +223,7 @@ public class FloatArrayListTest extends TestCase { for (int i = 0; i < 6; i++) { list.add(Float.valueOf(5 + i)); } - assertEquals( - asList(0F, 1F, 4F, 2F, 3F, 5F, 6F, 7F, 8F, 9F, 10F), - list); + assertEquals(asList(0F, 1F, 4F, 2F, 3F, 5F, 6F, 7F, 8F, 9F, 10F), list); try { list.add(-1, 5F); @@ -270,6 +266,15 @@ public class FloatArrayListTest extends TestCase { assertFalse(list.addAll(FloatArrayList.emptyList())); } + public void testEquals() { + FloatArrayList list1 = new FloatArrayList(); + FloatArrayList list2 = new FloatArrayList(); + + list1.addFloat(Float.intBitsToFloat(0xff800001)); + list2.addFloat(Float.intBitsToFloat(0xff800002)); + assertEquals(list1, list2); + } + public void testRemove() { list.addAll(TERTIARY_LIST); assertEquals(1F, (float) list.remove(0), 0.0f); @@ -298,17 +303,24 @@ public class FloatArrayListTest extends TestCase { } } - public void testRemoveEndOfCapacity() { - FloatList toRemove = - FloatArrayList.emptyList().mutableCopyWithCapacity(1); + public void testRemoveEnd_listAtCapacity() { + FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addFloat(3); toRemove.remove(0); assertEquals(0, toRemove.size()); } + public void testRemove_listAtCapacity() { + FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(2); + toRemove.addFloat(3); + toRemove.addFloat(4); + toRemove.remove(0); + assertEquals(1, toRemove.size()); + assertEquals(4F, (float) toRemove.get(0)); + } + public void testSublistRemoveEndOfCapacity() { - FloatList toRemove = - FloatArrayList.emptyList().mutableCopyWithCapacity(1); + FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addFloat(3); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); diff --git a/java/core/src/test/java/com/google/protobuf/ForceFieldBuildersPreRun.java b/java/core/src/test/java/com/google/protobuf/ForceFieldBuildersPreRun.java index b7eaebf5e2..baa6d08670 100644 --- a/java/core/src/test/java/com/google/protobuf/ForceFieldBuildersPreRun.java +++ b/java/core/src/test/java/com/google/protobuf/ForceFieldBuildersPreRun.java @@ -31,11 +31,10 @@ package com.google.protobuf; /** - * A prerun for a test suite that allows running the full protocol buffer - * tests in a mode that disables the optimization for not using - * {@link RepeatedFieldBuilder} and {@link SingleFieldBuilder} until they are - * requested. This allows us to run all the tests through both code paths - * and ensures that both code paths produce identical results. + * A prerun for a test suite that allows running the full protocol buffer tests in a mode that + * disables the optimization for not using {@link RepeatedFieldBuilder} and {@link + * SingleFieldBuilder} until they are requested. This allows us to run all the tests through both + * code paths and ensures that both code paths produce identical results. * * @author jonp@google.com (Jon Perlow) */ diff --git a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java index 4489dacea5..12bba6b3be 100644 --- a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java @@ -32,7 +32,6 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.FieldDescriptor; -import com.google.protobuf.Int32Value; import com.google.protobuf.test.UnittestImport; import protobuf_unittest.EnumWithNoOuter; import protobuf_unittest.MessageWithNoOuter; @@ -70,22 +69,58 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; import junit.framework.TestCase; +import junit.framework.TestSuite; /** - * Unit test for generated messages and generated code. See also - * {@link MessageTest}, which tests some generated message functionality. + * Unit test for generated messages and generated code. See also {@link MessageTest}, which tests + * some generated message functionality. * * @author kenton@google.com Kenton Varda */ public class GeneratedMessageTest extends TestCase { TestUtil.ReflectionTester reflectionTester = - new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); + new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); + + public static TestSuite suite() { + TestSuite suite = new TestSuite(); + suite.addTestSuite(ReflectionTest.class); + suite.addTestSuite(FastInvokeTest.class); + return suite; + } + + public static class ReflectionTest extends GeneratedMessageTest { + public ReflectionTest() { + super(true); + } + } + + public static class FastInvokeTest extends GeneratedMessageTest { + public FastInvokeTest() { + super(false); + } + } + + private final boolean useReflection; + + GeneratedMessageTest(boolean useReflection) { + this.useReflection = useReflection; + } + + @Override public void setUp() { + GeneratedMessageV3.setForTestUseReflection(useReflection); + } + + @Override public void tearDown() { + GeneratedMessageV3.setForTestUseReflection(false); + GeneratedMessageV3.setAlwaysUseFieldBuildersForTesting(false); + } public void testDefaultInstance() throws Exception { - assertSame(TestAllTypes.getDefaultInstance(), - TestAllTypes.getDefaultInstance().getDefaultInstanceForType()); - assertSame(TestAllTypes.getDefaultInstance(), - TestAllTypes.newBuilder().getDefaultInstanceForType()); + assertSame( + TestAllTypes.getDefaultInstance(), + TestAllTypes.getDefaultInstance().getDefaultInstanceForType()); + assertSame( + TestAllTypes.getDefaultInstance(), TestAllTypes.newBuilder().getDefaultInstanceForType()); } public void testMessageOrBuilder() throws Exception { @@ -111,10 +146,8 @@ public class GeneratedMessageTest extends TestCase { assertEquals(100, value1.getOptionalSfixed64()); assertEquals(100, value1.getRepeatedInt32(0)); - assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, - value1.getOptionalImportEnum()); - assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, - value1.getRepeatedImportEnum(0)); + assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, value1.getOptionalImportEnum()); + assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, value1.getRepeatedImportEnum(0)); assertEquals(1, value1.getOptionalForeignMessage().getC()); assertEquals(1, value1.getRepeatedForeignMessage(0).getC()); @@ -131,20 +164,16 @@ public class GeneratedMessageTest extends TestCase { // Make sure value1 didn't change. assertEquals(100, value1.getOptionalSfixed64()); assertEquals(100, value1.getRepeatedInt32(0)); - assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, - value1.getOptionalImportEnum()); - assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, - value1.getRepeatedImportEnum(0)); + assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, value1.getOptionalImportEnum()); + assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, value1.getRepeatedImportEnum(0)); assertEquals(1, value1.getOptionalForeignMessage().getC()); assertEquals(1, value1.getRepeatedForeignMessage(0).getC()); // Make sure value2 is correct assertEquals(200, value2.getOptionalSfixed64()); assertEquals(200, value2.getRepeatedInt32(0)); - assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, - value2.getOptionalImportEnum()); - assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, - value2.getRepeatedImportEnum(0)); + assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, value2.getOptionalImportEnum()); + assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, value2.getRepeatedImportEnum(0)); assertEquals(2, value2.getOptionalForeignMessage().getC()); assertEquals(2, value2.getRepeatedForeignMessage(0).getC()); } @@ -158,8 +187,7 @@ public class GeneratedMessageTest extends TestCase { TestAllTypes value2 = value1.toBuilder().build(); assertSame(value1.getRepeatedInt32List(), value2.getRepeatedInt32List()); - assertSame(value1.getRepeatedForeignMessageList(), - value2.getRepeatedForeignMessageList()); + assertSame(value1.getRepeatedForeignMessageList(), value2.getRepeatedForeignMessageList()); } public void testRepeatedArraysAreImmutable() throws Exception { @@ -172,7 +200,6 @@ public class GeneratedMessageTest extends TestCase { assertIsUnmodifiable(builder.getRepeatedForeignMessageList()); assertIsUnmodifiable(builder.getRepeatedFloatList()); - TestAllTypes value = builder.build(); assertIsUnmodifiable(value.getRepeatedInt32List()); assertIsUnmodifiable(value.getRepeatedImportEnumList()); @@ -240,8 +267,7 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } try { - builder.setOptionalNestedMessage( - (TestAllTypes.NestedMessage.Builder) null); + builder.setOptionalNestedMessage((TestAllTypes.NestedMessage.Builder) null); fail("Exception was not thrown"); } catch (NullPointerException e) { // We expect this exception. @@ -271,8 +297,7 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } try { - builder.addRepeatedNestedMessage( - (TestAllTypes.NestedMessage.Builder) null); + builder.addRepeatedNestedMessage((TestAllTypes.NestedMessage.Builder) null); fail("Exception was not thrown"); } catch (NullPointerException e) { // We expect this exception. @@ -314,10 +339,8 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } - builder.addRepeatedNestedMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); - builder.addRepeatedNestedMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(456).build()); + builder.addRepeatedNestedMessage(TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); + builder.addRepeatedNestedMessage(TestAllTypes.NestedMessage.newBuilder().setBb(456).build()); try { builder.setRepeatedNestedMessage(1, (TestAllTypes.NestedMessage) null); fail("Exception was not thrown"); @@ -325,8 +348,7 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } try { - builder.setRepeatedNestedMessage( - 1, (TestAllTypes.NestedMessage.Builder) null); + builder.setRepeatedNestedMessage(1, (TestAllTypes.NestedMessage.Builder) null); fail("Exception was not thrown"); } catch (NullPointerException e) { // We expect this exception. @@ -348,14 +370,12 @@ public class GeneratedMessageTest extends TestCase { builder.addAllRepeatedInt32(Arrays.asList(1, 2, 3, 4)); builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ)); - ForeignMessage foreignMessage = - ForeignMessage.newBuilder().setC(12).build(); + ForeignMessage foreignMessage = ForeignMessage.newBuilder().setC(12).build(); builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage)); TestAllTypes message = builder.build(); assertEquals(message.getRepeatedInt32List(), Arrays.asList(1, 2, 3, 4)); - assertEquals(message.getRepeatedForeignEnumList(), - Arrays.asList(ForeignEnum.FOREIGN_BAZ)); + assertEquals(message.getRepeatedForeignEnumList(), Arrays.asList(ForeignEnum.FOREIGN_BAZ)); assertEquals(1, message.getRepeatedForeignMessageCount()); assertEquals(12, message.getRepeatedForeignMessage(0).getC()); } @@ -363,19 +383,16 @@ public class GeneratedMessageTest extends TestCase { public void testRepeatedAppendRejectsNull() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); - ForeignMessage foreignMessage = - ForeignMessage.newBuilder().setC(12).build(); + ForeignMessage foreignMessage = ForeignMessage.newBuilder().setC(12).build(); try { - builder.addAllRepeatedForeignMessage( - Arrays.asList(foreignMessage, (ForeignMessage) null)); + builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage, (ForeignMessage) null)); fail("Exception was not thrown"); } catch (NullPointerException e) { // We expect this exception. } try { - builder.addAllRepeatedForeignEnum( - Arrays.asList(ForeignEnum.FOREIGN_BAZ, null)); + builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ, null)); fail("Exception was not thrown"); } catch (NullPointerException e) { // We expect this exception. @@ -395,20 +412,22 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } } - + public void testRepeatedAppendIterateOnlyOnce() throws Exception { // Create a Iterable that can only be iterated once. - Iterable stringIterable = new Iterable() { - private boolean called = false; - @Override - public Iterator iterator() { - if (called) { - throw new IllegalStateException(); - } - called = true; - return Arrays.asList("one", "two", "three").iterator(); - } - }; + Iterable stringIterable = + new Iterable() { + private boolean called = false; + + @Override + public Iterator iterator() { + if (called) { + throw new IllegalStateException(); + } + called = true; + return Arrays.asList("one", "two", "three").iterator(); + } + }; TestAllTypes.Builder builder = TestAllTypes.newBuilder(); builder.addAllRepeatedString(stringIterable); assertEquals(3, builder.getRepeatedStringCount()); @@ -435,29 +454,31 @@ public class GeneratedMessageTest extends TestCase { } public void testSettingForeignMessageUsingBuilder() throws Exception { - TestAllTypes message = TestAllTypes.newBuilder() - // Pass builder for foreign message instance. - .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(123)) - .build(); - TestAllTypes expectedMessage = TestAllTypes.newBuilder() - // Create expected version passing foreign message instance explicitly. - .setOptionalForeignMessage( - ForeignMessage.newBuilder().setC(123).build()) - .build(); + TestAllTypes message = + TestAllTypes.newBuilder() + // Pass builder for foreign message instance. + .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(123)) + .build(); + TestAllTypes expectedMessage = + TestAllTypes.newBuilder() + // Create expected version passing foreign message instance explicitly. + .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(123).build()) + .build(); // TODO(ngd): Upgrade to using real #equals method once implemented assertEquals(expectedMessage.toString(), message.toString()); } public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception { - TestAllTypes message = TestAllTypes.newBuilder() - // Pass builder for foreign message instance. - .addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(456)) - .build(); - TestAllTypes expectedMessage = TestAllTypes.newBuilder() - // Create expected version passing foreign message instance explicitly. - .addRepeatedForeignMessage( - ForeignMessage.newBuilder().setC(456).build()) - .build(); + TestAllTypes message = + TestAllTypes.newBuilder() + // Pass builder for foreign message instance. + .addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(456)) + .build(); + TestAllTypes expectedMessage = + TestAllTypes.newBuilder() + // Create expected version passing foreign message instance explicitly. + .addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(456).build()) + .build(); assertEquals(expectedMessage.toString(), message.toString()); } @@ -465,14 +486,13 @@ public class GeneratedMessageTest extends TestCase { TestUtil.assertClear(TestAllTypes.getDefaultInstance()); TestUtil.assertClear(TestAllTypes.newBuilder().build()); - TestExtremeDefaultValues message = - TestExtremeDefaultValues.getDefaultInstance(); + TestExtremeDefaultValues message = TestExtremeDefaultValues.getDefaultInstance(); assertEquals("\u1234", message.getUtf8String()); - assertEquals(Double.POSITIVE_INFINITY, message.getInfDouble()); - assertEquals(Double.NEGATIVE_INFINITY, message.getNegInfDouble()); + assertEquals(Double.POSITIVE_INFINITY, message.getInfDouble(), 0.0); + assertEquals(Double.NEGATIVE_INFINITY, message.getNegInfDouble(), 0.0); assertTrue(Double.isNaN(message.getNanDouble())); - assertEquals(Float.POSITIVE_INFINITY, message.getInfFloat()); - assertEquals(Float.NEGATIVE_INFINITY, message.getNegInfFloat()); + assertEquals(Float.POSITIVE_INFINITY, message.getInfFloat(), 0.0f); + assertEquals(Float.NEGATIVE_INFINITY, message.getNegInfFloat(), 0.0f); assertTrue(Float.isNaN(message.getNanFloat())); assertEquals("? ? ?? ?? ??? ??/ ??-", message.getCppTrigraph()); } @@ -524,19 +544,15 @@ public class GeneratedMessageTest extends TestCase { } public void testReflectionDefaults() throws Exception { - reflectionTester.assertClearViaReflection( - TestAllTypes.getDefaultInstance()); - reflectionTester.assertClearViaReflection( - TestAllTypes.newBuilder().build()); + reflectionTester.assertClearViaReflection(TestAllTypes.getDefaultInstance()); + reflectionTester.assertClearViaReflection(TestAllTypes.newBuilder().build()); } public void testReflectionGetOneof() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); reflectionTester.setAllFieldsViaReflection(builder); - Descriptors.OneofDescriptor oneof = - TestAllTypes.getDescriptor().getOneofs().get(0); - Descriptors.FieldDescriptor field = - TestAllTypes.getDescriptor().findFieldByName("oneof_bytes"); + Descriptors.OneofDescriptor oneof = TestAllTypes.getDescriptor().getOneofs().get(0); + Descriptors.FieldDescriptor field = TestAllTypes.getDescriptor().findFieldByName("oneof_bytes"); assertSame(field, builder.getOneofFieldDescriptor(oneof)); TestAllTypes message = builder.build(); @@ -546,10 +562,8 @@ public class GeneratedMessageTest extends TestCase { public void testReflectionClearOneof() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); reflectionTester.setAllFieldsViaReflection(builder); - Descriptors.OneofDescriptor oneof = - TestAllTypes.getDescriptor().getOneofs().get(0); - Descriptors.FieldDescriptor field = - TestAllTypes.getDescriptor().findFieldByName("oneof_bytes"); + Descriptors.OneofDescriptor oneof = TestAllTypes.getDescriptor().getOneofs().get(0); + Descriptors.FieldDescriptor field = TestAllTypes.getDescriptor().findFieldByName("oneof_bytes"); assertTrue(builder.hasOneof(oneof)); assertTrue(builder.hasField(field)); @@ -559,8 +573,8 @@ public class GeneratedMessageTest extends TestCase { } public void testEnumInterface() throws Exception { - assertTrue(TestAllTypes.getDefaultInstance().getDefaultNestedEnum() - instanceof ProtocolMessageEnum); + assertTrue( + TestAllTypes.getDefaultInstance().getDefaultNestedEnum() instanceof ProtocolMessageEnum); } public void testEnumMap() throws Exception { @@ -575,15 +589,13 @@ public class GeneratedMessageTest extends TestCase { public void testParsePackedToUnpacked() throws Exception { TestUnpackedTypes.Builder builder = TestUnpackedTypes.newBuilder(); - TestUnpackedTypes message = - builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build(); + TestUnpackedTypes message = builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build(); TestUtil.assertUnpackedFieldsSet(message); } public void testParseUnpackedToPacked() throws Exception { TestPackedTypes.Builder builder = TestPackedTypes.newBuilder(); - TestPackedTypes message = - builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build(); + TestPackedTypes message = builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build(); TestUtil.assertPackedFieldsSet(message); } @@ -601,6 +613,29 @@ public class GeneratedMessageTest extends TestCase { TestUtil.assertAllExtensionsSet(message); } + public void testGetBuilderForExtensionField() { + TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); + Message.Builder fieldBuilder = + builder.newBuilderForField(UnittestProto.optionalNestedMessageExtension.getDescriptor()); + final int expected = 7432; + FieldDescriptor field = + NestedMessage.getDescriptor().findFieldByNumber(NestedMessage.BB_FIELD_NUMBER); + fieldBuilder.setField(field, expected); + assertEquals(expected, fieldBuilder.build().getField(field)); + } + + + public void testGetBuilderForNonMessageExtensionField() { + TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); + try { + // This should throw an exception because the extension field is not a message. + builder.newBuilderForField(UnittestProto.optionalInt32Extension.getDescriptor()); + fail("Exception was not thrown"); + } catch (UnsupportedOperationException e) { + // This exception is expected. + } + } + public void testExtensionRepeatedSetters() throws Exception { TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); TestUtil.setAllExtensions(builder); @@ -662,32 +697,29 @@ public class GeneratedMessageTest extends TestCase { TestUtil.assertRepeatedExtensionsModified(message); } - public void testExtensionReflectionRepeatedSettersRejectNull() - throws Exception { + public void testExtensionReflectionRepeatedSettersRejectNull() throws Exception { TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); - extensionsReflectionTester.assertReflectionRepeatedSettersRejectNull( - builder); + extensionsReflectionTester.assertReflectionRepeatedSettersRejectNull(builder); } public void testExtensionReflectionDefaults() throws Exception { - extensionsReflectionTester.assertClearViaReflection( - TestAllExtensions.getDefaultInstance()); - extensionsReflectionTester.assertClearViaReflection( - TestAllExtensions.newBuilder().build()); + extensionsReflectionTester.assertClearViaReflection(TestAllExtensions.getDefaultInstance()); + extensionsReflectionTester.assertClearViaReflection(TestAllExtensions.newBuilder().build()); } public void testClearExtension() throws Exception { // clearExtension() is not actually used in TestUtil, so try it manually. assertFalse( - TestAllExtensions.newBuilder() - .setExtension(UnittestProto.optionalInt32Extension, 1) - .clearExtension(UnittestProto.optionalInt32Extension) - .hasExtension(UnittestProto.optionalInt32Extension)); - assertEquals(0, - TestAllExtensions.newBuilder() - .addExtension(UnittestProto.repeatedInt32Extension, 1) - .clearExtension(UnittestProto.repeatedInt32Extension) - .getExtensionCount(UnittestProto.repeatedInt32Extension)); + TestAllExtensions.newBuilder() + .setExtension(UnittestProto.optionalInt32Extension, 1) + .clearExtension(UnittestProto.optionalInt32Extension) + .hasExtension(UnittestProto.optionalInt32Extension)); + assertEquals( + 0, + TestAllExtensions.newBuilder() + .addExtension(UnittestProto.repeatedInt32Extension, 1) + .clearExtension(UnittestProto.repeatedInt32Extension) + .getExtensionCount(UnittestProto.repeatedInt32Extension)); } public void testExtensionCopy() throws Exception { @@ -698,13 +730,12 @@ public class GeneratedMessageTest extends TestCase { public void testExtensionMergeFrom() throws Exception { TestAllExtensions original = - TestAllExtensions.newBuilder() - .setExtension(UnittestProto.optionalInt32Extension, 1).build(); - TestAllExtensions merged = - TestAllExtensions.newBuilder().mergeFrom(original).build(); + TestAllExtensions.newBuilder() + .setExtension(UnittestProto.optionalInt32Extension, 1) + .build(); + TestAllExtensions merged = TestAllExtensions.newBuilder().mergeFrom(original).build(); assertTrue(merged.hasExtension(UnittestProto.optionalInt32Extension)); - assertEquals( - 1, (int) merged.getExtension(UnittestProto.optionalInt32Extension)); + assertEquals(1, (int) merged.getExtension(UnittestProto.optionalInt32Extension)); } // ================================================================= @@ -715,64 +746,69 @@ public class GeneratedMessageTest extends TestCase { // or messages defined in multiple_files_test.proto because the class loading // order affects initialization process of custom options. public void testEnumValueOptionsInMultipleFilesMode() throws Exception { - assertEquals(12345, EnumWithNoOuter.FOO.getValueDescriptor().getOptions() - .getExtension(MultipleFilesTestProto.enumValueOption).intValue()); + assertEquals( + 12345, + EnumWithNoOuter.FOO + .getValueDescriptor() + .getOptions() + .getExtension(MultipleFilesTestProto.enumValueOption) + .intValue()); } public void testMultipleFilesOption() throws Exception { // We mostly just want to check that things compile. MessageWithNoOuter message = - MessageWithNoOuter.newBuilder() - .setNested(MessageWithNoOuter.NestedMessage.newBuilder().setI(1)) - .addForeign(TestAllTypes.newBuilder().setOptionalInt32(1)) - .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ) - .setForeignEnum(EnumWithNoOuter.BAR) - .build(); + MessageWithNoOuter.newBuilder() + .setNested(MessageWithNoOuter.NestedMessage.newBuilder().setI(1)) + .addForeign(TestAllTypes.newBuilder().setOptionalInt32(1)) + .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ) + .setForeignEnum(EnumWithNoOuter.BAR) + .build(); assertEquals(message, MessageWithNoOuter.parseFrom(message.toByteString())); - assertEquals(MultipleFilesTestProto.getDescriptor(), - MessageWithNoOuter.getDescriptor().getFile()); + assertEquals( + MultipleFilesTestProto.getDescriptor(), MessageWithNoOuter.getDescriptor().getFile()); Descriptors.FieldDescriptor field = - MessageWithNoOuter.getDescriptor().findFieldByName("foreign_enum"); - assertEquals(EnumWithNoOuter.BAR.getValueDescriptor(), - message.getField(field)); + MessageWithNoOuter.getDescriptor().findFieldByName("foreign_enum"); + assertEquals(EnumWithNoOuter.BAR.getValueDescriptor(), message.getField(field)); - assertEquals(MultipleFilesTestProto.getDescriptor(), - ServiceWithNoOuter.getDescriptor().getFile()); + assertEquals( + MultipleFilesTestProto.getDescriptor(), ServiceWithNoOuter.getDescriptor().getFile()); assertFalse( - TestAllExtensions.getDefaultInstance().hasExtension( - MultipleFilesTestProto.extensionWithOuter)); + TestAllExtensions.getDefaultInstance() + .hasExtension(MultipleFilesTestProto.extensionWithOuter)); } - public void testOptionalFieldWithRequiredSubfieldsOptimizedForSize() - throws Exception { - TestOptionalOptimizedForSize message = - TestOptionalOptimizedForSize.getDefaultInstance(); + public void testOptionalFieldWithRequiredSubfieldsOptimizedForSize() throws Exception { + TestOptionalOptimizedForSize message = TestOptionalOptimizedForSize.getDefaultInstance(); assertTrue(message.isInitialized()); - message = TestOptionalOptimizedForSize.newBuilder().setO( - TestRequiredOptimizedForSize.newBuilder().buildPartial() - ).buildPartial(); + message = + TestOptionalOptimizedForSize.newBuilder() + .setO(TestRequiredOptimizedForSize.newBuilder().buildPartial()) + .buildPartial(); assertFalse(message.isInitialized()); - message = TestOptionalOptimizedForSize.newBuilder().setO( - TestRequiredOptimizedForSize.newBuilder().setX(5).buildPartial() - ).buildPartial(); + message = + TestOptionalOptimizedForSize.newBuilder() + .setO(TestRequiredOptimizedForSize.newBuilder().setX(5).buildPartial()) + .buildPartial(); assertTrue(message.isInitialized()); } - public void testUninitializedExtensionInOptimizedForSize() - throws Exception { + public void testUninitializedExtensionInOptimizedForSize() throws Exception { TestOptimizedForSize.Builder builder = TestOptimizedForSize.newBuilder(); - builder.setExtension(TestOptimizedForSize.testExtension2, + builder.setExtension( + TestOptimizedForSize.testExtension2, TestRequiredOptimizedForSize.newBuilder().buildPartial()); assertFalse(builder.isInitialized()); assertFalse(builder.buildPartial().isInitialized()); builder = TestOptimizedForSize.newBuilder(); - builder.setExtension(TestOptimizedForSize.testExtension2, + builder.setExtension( + TestOptimizedForSize.testExtension2, TestRequiredOptimizedForSize.newBuilder().setX(10).buildPartial()); assertTrue(builder.isInitialized()); assertTrue(builder.buildPartial().isInitialized()); @@ -803,13 +839,11 @@ public class GeneratedMessageTest extends TestCase { assertEquals(UnittestProto.TestRequired.MULTI_FIELD_NUMBER, 1001); assertEquals(UnittestProto.OPTIONAL_INT32_EXTENSION_FIELD_NUMBER, 1); assertEquals(UnittestProto.OPTIONALGROUP_EXTENSION_FIELD_NUMBER, 16); - assertEquals( - UnittestProto.OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 18); + assertEquals(UnittestProto.OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 18); assertEquals(UnittestProto.OPTIONAL_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 21); assertEquals(UnittestProto.REPEATED_INT32_EXTENSION_FIELD_NUMBER, 31); assertEquals(UnittestProto.REPEATEDGROUP_EXTENSION_FIELD_NUMBER, 46); - assertEquals( - UnittestProto.REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 48); + assertEquals(UnittestProto.REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 48); assertEquals(UnittestProto.REPEATED_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 51); } @@ -818,7 +852,7 @@ public class GeneratedMessageTest extends TestCase { UnittestProto.TestRecursiveMessage.getDefaultInstance(); assertTrue(message != null); assertNotNull(message.getA()); - assertTrue(message.getA() == message); + assertTrue(message.getA().equals(message)); } public void testSerialize() throws Exception { @@ -859,21 +893,19 @@ public class GeneratedMessageTest extends TestCase { // just includes messageClassName and asBytes // Int32Value.newBuilder().setValue(123).build() - byte[] int32ValueBytes = new byte[]{ - -84, -19, 0, 5, 115, 114, 0, 55, 99, 111, 109, 46, 103, 111, 111, - 103, 108, 101, 46, 112, 114, 111, 116, 111, 98, 117, 102, 46, 71, - 101, 110, 101, 114, 97, 116, 101, 100, 77, 101, 115, 115, 97, 103, - 101, 76, 105, 116, 101, 36, 83, 101, 114, 105, 97, 108, 105, 122, - 101, 100, 70, 111, 114, 109, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 91, - 0, 7, 97, 115, 66, 121, 116, 101, 115, 116, 0, 2, 91, 66, 76, 0, - 16, 109, 101, 115, 115, 97, 103, 101, 67, 108, 97, 115, 115, 78, - 97, 109, 101, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, - 103, 47, 83, 116, 114, 105, 110, 103, 59, 120, 112, 117, 114, 0, - 2, 91, 66, -84, -13, 23, -8, 6, 8, 84, -32, 2, 0, 0, 120, 112, 0, - 0, 0, 2, 8, 123, 116, 0, 30, 99, 111, 109, 46, 103, 111, 111, 103, - 108, 101, 46, 112, 114, 111, 116, 111, 98, 117, 102, 46, 73, 110, - 116, 51, 50, 86, 97, 108, 117, 101 - }; + byte[] int32ValueBytes = + new byte[] { + -84, -19, 0, 5, 115, 114, 0, 55, 99, 111, 109, 46, 103, 111, 111, 103, 108, 101, 46, 112, + 114, 111, 116, 111, 98, 117, 102, 46, 71, 101, 110, 101, 114, 97, 116, 101, 100, 77, 101, + 115, 115, 97, 103, 101, 76, 105, 116, 101, 36, 83, 101, 114, 105, 97, 108, 105, 122, 101, + 100, 70, 111, 114, 109, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 91, 0, 7, 97, 115, 66, 121, 116, + 101, 115, 116, 0, 2, 91, 66, 76, 0, 16, 109, 101, 115, 115, 97, 103, 101, 67, 108, 97, + 115, 115, 78, 97, 109, 101, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, + 83, 116, 114, 105, 110, 103, 59, 120, 112, 117, 114, 0, 2, 91, 66, -84, -13, 23, -8, 6, 8, + 84, -32, 2, 0, 0, 120, 112, 0, 0, 0, 2, 8, 123, 116, 0, 30, 99, 111, 109, 46, 103, 111, + 111, 103, 108, 101, 46, 112, 114, 111, 116, 111, 98, 117, 102, 46, 73, 110, 116, 51, 50, + 86, 97, 108, 117, 101 + }; ByteArrayInputStream bais = new ByteArrayInputStream(int32ValueBytes); ObjectInputStream in = new ObjectInputStream(bais); @@ -886,36 +918,30 @@ public class GeneratedMessageTest extends TestCase { // includes messageClass, messageClassName (for compatibility), and asBytes // Int32Value.newBuilder().setValue(123).build() - byte[] int32ValueBytes = new byte[]{ - -84, -19, 0, 5, 115, 114, 0, 55, 99, 111, 109, 46, 103, 111, 111, - 103, 108, 101, 46, 112, 114, 111, 116, 111, 98, 117, 102, 46, 71, - 101, 110, 101, 114, 97, 116, 101, 100, 77, 101, 115, 115, 97, 103, - 101, 76, 105, 116, 101, 36, 83, 101, 114, 105, 97, 108, 105, 122, - 101, 100, 70, 111, 114, 109, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 91, - 0, 7, 97, 115, 66, 121, 116, 101, 115, 116, 0, 2, 91, 66, 76, 0, - 12, 109, 101, 115, 115, 97, 103, 101, 67, 108, 97, 115, 115, 116, - 0, 17, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 67, 108, - 97, 115, 115, 59, 76, 0, 16, 109, 101, 115, 115, 97, 103, 101, 67, - 108, 97, 115, 115, 78, 97, 109, 101, 116, 0, 18, 76, 106, 97, 118, - 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, - 120, 112, 117, 114, 0, 2, 91, 66, -84, -13, 23, -8, 6, 8, 84, -32, - 2, 0, 0, 120, 112, 0, 0, 0, 2, 8, 123, 118, 114, 0, 30, 99, 111, - 109, 46, 103, 111, 111, 103, 108, 101, 46, 112, 114, 111, 116, 111, - 98, 117, 102, 46, 73, 110, 116, 51, 50, 86, 97, 108, 117, 101, 0, 0, - 0, 0, 0, 0, 0, 0, 2, 0, 2, 66, 0, 21, 109, 101, 109, 111, 105, 122, - 101, 100, 73, 115, 73, 110, 105, 116, 105, 97, 108, 105, 122, 101, - 100, 73, 0, 6, 118, 97, 108, 117, 101, 95, 120, 114, 0, 38, 99, 111, - 109, 46, 103, 111, 111, 103, 108, 101, 46, 112, 114, 111, 116, 111, - 98, 117, 102, 46, 71, 101, 110, 101, 114, 97, 116, 101, 100, 77, - 101, 115, 115, 97, 103, 101, 86, 51, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, - 1, 76, 0, 13, 117, 110, 107, 110, 111, 119, 110, 70, 105, 101, 108, - 100, 115, 116, 0, 37, 76, 99, 111, 109, 47, 103, 111, 111, 103, 108, - 101, 47, 112, 114, 111, 116, 111, 98, 117, 102, 47, 85, 110, 107, - 110, 111, 119, 110, 70, 105, 101, 108, 100, 83, 101, 116, 59, 120, - 112, 116, 0, 30, 99, 111, 109, 46, 103, 111, 111, 103, 108, 101, 46, - 112, 114, 111, 116, 111, 98, 117, 102, 46, 73, 110, 116, 51, 50, 86, - 97, 108, 117, 101 - }; + byte[] int32ValueBytes = + new byte[] { + -84, -19, 0, 5, 115, 114, 0, 55, 99, 111, 109, 46, 103, 111, 111, 103, 108, 101, 46, 112, + 114, 111, 116, 111, 98, 117, 102, 46, 71, 101, 110, 101, 114, 97, 116, 101, 100, 77, 101, + 115, 115, 97, 103, 101, 76, 105, 116, 101, 36, 83, 101, 114, 105, 97, 108, 105, 122, 101, + 100, 70, 111, 114, 109, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 91, 0, 7, 97, 115, 66, 121, 116, + 101, 115, 116, 0, 2, 91, 66, 76, 0, 12, 109, 101, 115, 115, 97, 103, 101, 67, 108, 97, + 115, 115, 116, 0, 17, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 67, 108, 97, 115, + 115, 59, 76, 0, 16, 109, 101, 115, 115, 97, 103, 101, 67, 108, 97, 115, 115, 78, 97, 109, + 101, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, + 103, 59, 120, 112, 117, 114, 0, 2, 91, 66, -84, -13, 23, -8, 6, 8, 84, -32, 2, 0, 0, 120, + 112, 0, 0, 0, 2, 8, 123, 118, 114, 0, 30, 99, 111, 109, 46, 103, 111, 111, 103, 108, 101, + 46, 112, 114, 111, 116, 111, 98, 117, 102, 46, 73, 110, 116, 51, 50, 86, 97, 108, 117, + 101, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 66, 0, 21, 109, 101, 109, 111, 105, 122, 101, 100, + 73, 115, 73, 110, 105, 116, 105, 97, 108, 105, 122, 101, 100, 73, 0, 6, 118, 97, 108, 117, + 101, 95, 120, 114, 0, 38, 99, 111, 109, 46, 103, 111, 111, 103, 108, 101, 46, 112, 114, + 111, 116, 111, 98, 117, 102, 46, 71, 101, 110, 101, 114, 97, 116, 101, 100, 77, 101, 115, + 115, 97, 103, 101, 86, 51, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 76, 0, 13, 117, 110, 107, 110, + 111, 119, 110, 70, 105, 101, 108, 100, 115, 116, 0, 37, 76, 99, 111, 109, 47, 103, 111, + 111, 103, 108, 101, 47, 112, 114, 111, 116, 111, 98, 117, 102, 47, 85, 110, 107, 110, 111, + 119, 110, 70, 105, 101, 108, 100, 83, 101, 116, 59, 120, 112, 116, 0, 30, 99, 111, 109, + 46, 103, 111, 111, 103, 108, 101, 46, 112, 114, 111, 116, 111, 98, 117, 102, 46, 73, 110, + 116, 51, 50, 86, 97, 108, 117, 101 + }; ByteArrayInputStream bais = new ByteArrayInputStream(int32ValueBytes); ObjectInputStream in = new ObjectInputStream(bais); @@ -930,33 +956,33 @@ public class GeneratedMessageTest extends TestCase { } public void testNonNestedExtensionInitialization() { - assertTrue(NonNestedExtension.nonNestedExtension - .getMessageDefaultInstance() instanceof MyNonNestedExtension); - assertEquals("nonNestedExtension", - NonNestedExtension.nonNestedExtension.getDescriptor().getName()); + assertTrue( + NonNestedExtension.nonNestedExtension.getMessageDefaultInstance() + instanceof MyNonNestedExtension); + assertEquals( + "nonNestedExtension", NonNestedExtension.nonNestedExtension.getDescriptor().getName()); } public void testNestedExtensionInitialization() { - assertTrue(MyNestedExtension.recursiveExtension.getMessageDefaultInstance() - instanceof MessageToBeExtended); - assertEquals("recursiveExtension", - MyNestedExtension.recursiveExtension.getDescriptor().getName()); + assertTrue( + MyNestedExtension.recursiveExtension.getMessageDefaultInstance() + instanceof MessageToBeExtended); + assertEquals( + "recursiveExtension", MyNestedExtension.recursiveExtension.getDescriptor().getName()); } public void testInvalidations() throws Exception { - GeneratedMessage.enableAlwaysUseFieldBuildersForTesting(); - TestAllTypes.NestedMessage nestedMessage1 = - TestAllTypes.NestedMessage.newBuilder().build(); - TestAllTypes.NestedMessage nestedMessage2 = - TestAllTypes.NestedMessage.newBuilder().build(); + GeneratedMessageV3.setAlwaysUseFieldBuildersForTesting(true); + TestAllTypes.NestedMessage nestedMessage1 = TestAllTypes.NestedMessage.newBuilder().build(); + TestAllTypes.NestedMessage nestedMessage2 = TestAllTypes.NestedMessage.newBuilder().build(); // Set all three flavors (enum, primitive, message and singular/repeated) // and verify no invalidations fired TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - TestAllTypes.Builder builder = (TestAllTypes.Builder) - ((AbstractMessage) TestAllTypes.getDefaultInstance()). - newBuilderForType(mockParent); + TestAllTypes.Builder builder = + (TestAllTypes.Builder) + ((AbstractMessage) TestAllTypes.getDefaultInstance()).newBuilderForType(mockParent); builder.setOptionalInt32(1); builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR); builder.setOptionalNestedMessage(nestedMessage1); @@ -1003,15 +1029,15 @@ public class GeneratedMessageTest extends TestCase { builder.addRepeatedNestedMessage(nestedMessage2); builder.addRepeatedNestedMessage(nestedMessage1); assertEquals(6, mockParent.getInvalidationCount()); - } public void testInvalidations_Extensions() throws Exception { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - TestAllExtensions.Builder builder = (TestAllExtensions.Builder) - ((AbstractMessage) TestAllExtensions.getDefaultInstance()). - newBuilderForType(mockParent); + TestAllExtensions.Builder builder = + (TestAllExtensions.Builder) + ((AbstractMessage) TestAllExtensions.getDefaultInstance()) + .newBuilderForType(mockParent); builder.addExtension(UnittestProto.repeatedInt32Extension, 1); builder.setExtension(UnittestProto.repeatedInt32Extension, 0, 2); @@ -1039,25 +1065,19 @@ public class GeneratedMessageTest extends TestCase { // Mostly just makes sure the base interface exists and has some methods. TestAllTypes.Builder builder = TestAllTypes.newBuilder(); TestAllTypes message = builder.buildPartial(); - TestAllTypesOrBuilder builderAsInterface = (TestAllTypesOrBuilder) builder; TestAllTypesOrBuilder messageAsInterface = (TestAllTypesOrBuilder) message; + assertEquals(messageAsInterface.getDefaultBool(), messageAsInterface.getDefaultBool()); assertEquals( - messageAsInterface.getDefaultBool(), - messageAsInterface.getDefaultBool()); - assertEquals( - messageAsInterface.getOptionalDouble(), - messageAsInterface.getOptionalDouble()); + messageAsInterface.getOptionalDouble(), messageAsInterface.getOptionalDouble(), 0.0); } public void testMessageOrBuilderGetters() { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); // single fields - assertSame(ForeignMessage.getDefaultInstance(), - builder.getOptionalForeignMessageOrBuilder()); - ForeignMessage.Builder subBuilder = - builder.getOptionalForeignMessageBuilder(); + assertSame(ForeignMessage.getDefaultInstance(), builder.getOptionalForeignMessageOrBuilder()); + ForeignMessage.Builder subBuilder = builder.getOptionalForeignMessageBuilder(); assertSame(subBuilder, builder.getOptionalForeignMessageOrBuilder()); // repeated fields @@ -1086,36 +1106,37 @@ public class GeneratedMessageTest extends TestCase { public void testGetFieldBuilder() { Descriptor descriptor = TestAllTypes.getDescriptor(); - FieldDescriptor fieldDescriptor = - descriptor.findFieldByName("optional_nested_message"); - FieldDescriptor foreignFieldDescriptor = - descriptor.findFieldByName("optional_foreign_message"); - FieldDescriptor importFieldDescriptor = - descriptor.findFieldByName("optional_import_message"); + FieldDescriptor fieldDescriptor = descriptor.findFieldByName("optional_nested_message"); + FieldDescriptor foreignFieldDescriptor = descriptor.findFieldByName("optional_foreign_message"); + FieldDescriptor importFieldDescriptor = descriptor.findFieldByName("optional_import_message"); // Mutate the message with new field builder // Mutate nested message TestAllTypes.Builder builder1 = TestAllTypes.newBuilder(); - Message.Builder fieldBuilder1 = builder1.newBuilderForField(fieldDescriptor) - .mergeFrom((Message) builder1.getField(fieldDescriptor)); + Message.Builder fieldBuilder1 = + builder1 + .newBuilderForField(fieldDescriptor) + .mergeFrom((Message) builder1.getField(fieldDescriptor)); FieldDescriptor subFieldDescriptor1 = fieldBuilder1.getDescriptorForType().findFieldByName("bb"); fieldBuilder1.setField(subFieldDescriptor1, 1); builder1.setField(fieldDescriptor, fieldBuilder1.build()); // Mutate foreign message - Message.Builder foreignFieldBuilder1 = builder1.newBuilderForField( - foreignFieldDescriptor) - .mergeFrom((Message) builder1.getField(foreignFieldDescriptor)); + Message.Builder foreignFieldBuilder1 = + builder1 + .newBuilderForField(foreignFieldDescriptor) + .mergeFrom((Message) builder1.getField(foreignFieldDescriptor)); FieldDescriptor subForeignFieldDescriptor1 = foreignFieldBuilder1.getDescriptorForType().findFieldByName("c"); foreignFieldBuilder1.setField(subForeignFieldDescriptor1, 2); builder1.setField(foreignFieldDescriptor, foreignFieldBuilder1.build()); // Mutate import message - Message.Builder importFieldBuilder1 = builder1.newBuilderForField( - importFieldDescriptor) - .mergeFrom((Message) builder1.getField(importFieldDescriptor)); + Message.Builder importFieldBuilder1 = + builder1 + .newBuilderForField(importFieldDescriptor) + .mergeFrom((Message) builder1.getField(importFieldDescriptor)); FieldDescriptor subImportFieldDescriptor1 = importFieldBuilder1.getDescriptorForType().findFieldByName("d"); importFieldBuilder1.setField(subImportFieldDescriptor1, 3); @@ -1133,18 +1154,20 @@ public class GeneratedMessageTest extends TestCase { builder2.setField(fieldDescriptor, fieldBuilder2.build()); // Mutate foreign message - Message.Builder foreignFieldBuilder2 = builder2.newBuilderForField( - foreignFieldDescriptor) - .mergeFrom((Message) builder2.getField(foreignFieldDescriptor)); + Message.Builder foreignFieldBuilder2 = + builder2 + .newBuilderForField(foreignFieldDescriptor) + .mergeFrom((Message) builder2.getField(foreignFieldDescriptor)); FieldDescriptor subForeignFieldDescriptor2 = foreignFieldBuilder2.getDescriptorForType().findFieldByName("c"); foreignFieldBuilder2.setField(subForeignFieldDescriptor2, 2); builder2.setField(foreignFieldDescriptor, foreignFieldBuilder2.build()); // Mutate import message - Message.Builder importFieldBuilder2 = builder2.newBuilderForField( - importFieldDescriptor) - .mergeFrom((Message) builder2.getField(importFieldDescriptor)); + Message.Builder importFieldBuilder2 = + builder2 + .newBuilderForField(importFieldDescriptor) + .mergeFrom((Message) builder2.getField(importFieldDescriptor)); FieldDescriptor subImportFieldDescriptor2 = importFieldBuilder2.getDescriptorForType().findFieldByName("d"); importFieldBuilder2.setField(subImportFieldDescriptor2, 3); @@ -1158,10 +1181,9 @@ public class GeneratedMessageTest extends TestCase { public void testGetFieldBuilderWithInitializedValue() { Descriptor descriptor = TestAllTypes.getDescriptor(); - FieldDescriptor fieldDescriptor = - descriptor.findFieldByName("optional_nested_message"); + FieldDescriptor fieldDescriptor = descriptor.findFieldByName("optional_nested_message"); - // Before setting field, builder is initialized by default value. + // Before setting field, builder is initialized by default value. TestAllTypes.Builder builder = TestAllTypes.newBuilder(); NestedMessage.Builder fieldBuilder = (NestedMessage.Builder) builder.getFieldBuilder(fieldDescriptor); @@ -1169,12 +1191,10 @@ public class GeneratedMessageTest extends TestCase { // Setting field value with new field builder instance. builder = TestAllTypes.newBuilder(); - NestedMessage.Builder newFieldBuilder = - builder.getOptionalNestedMessageBuilder(); + NestedMessage.Builder newFieldBuilder = builder.getOptionalNestedMessageBuilder(); newFieldBuilder.setBb(2); // Then get the field builder instance by getFieldBuilder(). - fieldBuilder = - (NestedMessage.Builder) builder.getFieldBuilder(fieldDescriptor); + fieldBuilder = (NestedMessage.Builder) builder.getFieldBuilder(fieldDescriptor); // It should contain new value. assertEquals(2, fieldBuilder.getBb()); // These two builder should be equal. @@ -1191,8 +1211,7 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } try { - builder.getFieldBuilder( - descriptor.findFieldByName("optional_nested_enum")); + builder.getFieldBuilder(descriptor.findFieldByName("optional_nested_enum")); fail("Exception was not thrown"); } catch (UnsupportedOperationException e) { // We expect this exception. @@ -1204,15 +1223,13 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } try { - builder.getFieldBuilder( - descriptor.findFieldByName("repeated_nested_enum")); + builder.getFieldBuilder(descriptor.findFieldByName("repeated_nested_enum")); fail("Exception was not thrown"); } catch (UnsupportedOperationException e) { // We expect this exception. } try { - builder.getFieldBuilder( - descriptor.findFieldByName("repeated_nested_message")); + builder.getFieldBuilder(descriptor.findFieldByName("repeated_nested_message")); fail("Exception was not thrown"); } catch (UnsupportedOperationException e) { // We expect this exception. @@ -1227,25 +1244,25 @@ public class GeneratedMessageTest extends TestCase { // expected name. There is nothing else to test. OuterClassNameTestOuterClass.OuterClassNameTest message = OuterClassNameTestOuterClass.OuterClassNameTest.newBuilder().build(); - assertTrue(message.getDescriptorForType() == - OuterClassNameTestOuterClass.OuterClassNameTest.getDescriptor()); + assertTrue( + message.getDescriptorForType() + == OuterClassNameTestOuterClass.OuterClassNameTest.getDescriptor()); - OuterClassNameTest2OuterClass.TestMessage2.NestedMessage.OuterClassNameTest2 - message2 = OuterClassNameTest2OuterClass.TestMessage2.NestedMessage - .OuterClassNameTest2.newBuilder().build(); + OuterClassNameTest2OuterClass.TestMessage2.NestedMessage.OuterClassNameTest2 message2 = + OuterClassNameTest2OuterClass.TestMessage2.NestedMessage.OuterClassNameTest2.newBuilder() + .build(); assertEquals(0, message2.getSerializedSize()); - OuterClassNameTest3OuterClass.TestMessage3.NestedMessage.OuterClassNameTest3 - enumValue = OuterClassNameTest3OuterClass.TestMessage3.NestedMessage - .OuterClassNameTest3.DUMMY_VALUE; + OuterClassNameTest3OuterClass.TestMessage3.NestedMessage.OuterClassNameTest3 enumValue = + OuterClassNameTest3OuterClass.TestMessage3.NestedMessage.OuterClassNameTest3.DUMMY_VALUE; assertEquals(1, enumValue.getNumber()); } // ================================================================= // oneof generated code test public void testOneofEnumCase() throws Exception { - TestOneof2 message = TestOneof2.newBuilder() - .setFooInt(123).setFooString("foo").setFooCord("bar").build(); + TestOneof2 message = + TestOneof2.newBuilder().setFooInt(123).setFooString("foo").setFooCord("bar").build(); TestUtil.assertAtMostOneFieldSetOneof(message); } @@ -1258,8 +1275,7 @@ public class GeneratedMessageTest extends TestCase { public void testSetOneofClearsOthers() throws Exception { TestOneof2.Builder builder = TestOneof2.newBuilder(); - TestOneof2 message = - builder.setFooInt(123).setFooString("foo").buildPartial(); + TestOneof2 message = builder.setFooInt(123).setFooString("foo").buildPartial(); assertTrue(message.hasFooString()); TestUtil.assertAtMostOneFieldSetOneof(message); @@ -1279,8 +1295,10 @@ public class GeneratedMessageTest extends TestCase { assertTrue(message.hasFooEnum()); TestUtil.assertAtMostOneFieldSetOneof(message); - message = builder.setFooMessage( - TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build()).buildPartial(); + message = + builder + .setFooMessage(TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build()) + .buildPartial(); assertTrue(message.hasFooMessage()); TestUtil.assertAtMostOneFieldSetOneof(message); @@ -1396,8 +1414,7 @@ public class GeneratedMessageTest extends TestCase { // set TestOneof2.Builder builder = TestOneof2.newBuilder(); assertEquals(0, builder.getFooMessage().getQuxInt()); - builder.setFooMessage( - TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build()); + builder.setFooMessage(TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build()); assertTrue(builder.hasFooMessage()); assertEquals(234, builder.getFooMessage().getQuxInt()); TestOneof2 message = builder.buildPartial(); @@ -1412,8 +1429,7 @@ public class GeneratedMessageTest extends TestCase { // nested builder builder = TestOneof2.newBuilder(); - assertSame(builder.getFooMessageOrBuilder(), - TestOneof2.NestedMessage.getDefaultInstance()); + assertSame(builder.getFooMessageOrBuilder(), TestOneof2.NestedMessage.getDefaultInstance()); assertFalse(builder.hasFooMessage()); builder.getFooMessageBuilder().setQuxInt(123); assertTrue(builder.hasFooMessage()); @@ -1457,8 +1473,10 @@ public class GeneratedMessageTest extends TestCase { // Message { TestOneof2.Builder builder = TestOneof2.newBuilder(); - TestOneof2 message = builder.setFooMessage( - TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build()).build(); + TestOneof2 message = + builder + .setFooMessage(TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build()) + .build(); TestOneof2 message2 = TestOneof2.newBuilder().mergeFrom(message).build(); assertTrue(message2.hasFooMessage()); assertEquals(234, message2.getFooMessage().getQuxInt()); @@ -1499,8 +1517,10 @@ public class GeneratedMessageTest extends TestCase { // Message { TestOneof2.Builder builder = TestOneof2.newBuilder(); - TestOneof2 message = builder.setFooMessage( - TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build()).build(); + TestOneof2 message = + builder + .setFooMessage(TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build()) + .build(); ByteString serialized = message.toByteString(); TestOneof2 message2 = TestOneof2.parseFrom(serialized); assertTrue(message2.hasFooMessage()); @@ -1522,40 +1542,32 @@ public class GeneratedMessageTest extends TestCase { public void testGetRepeatedFieldBuilder() { Descriptor descriptor = TestAllTypes.getDescriptor(); - FieldDescriptor fieldDescriptor = - descriptor.findFieldByName("repeated_nested_message"); - FieldDescriptor foreignFieldDescriptor = - descriptor.findFieldByName("repeated_foreign_message"); - FieldDescriptor importFieldDescriptor = - descriptor.findFieldByName("repeated_import_message"); + FieldDescriptor fieldDescriptor = descriptor.findFieldByName("repeated_nested_message"); + FieldDescriptor foreignFieldDescriptor = descriptor.findFieldByName("repeated_foreign_message"); + FieldDescriptor importFieldDescriptor = descriptor.findFieldByName("repeated_import_message"); // Mutate the message with new field builder // Mutate nested message TestAllTypes.Builder builder1 = TestAllTypes.newBuilder(); - Message.Builder fieldBuilder1 = builder1.newBuilderForField( - fieldDescriptor); + Message.Builder fieldBuilder1 = builder1.newBuilderForField(fieldDescriptor); FieldDescriptor subFieldDescriptor1 = fieldBuilder1.getDescriptorForType().findFieldByName("bb"); fieldBuilder1.setField(subFieldDescriptor1, 1); builder1.addRepeatedField(fieldDescriptor, fieldBuilder1.build()); // Mutate foreign message - Message.Builder foreignFieldBuilder1 = builder1.newBuilderForField( - foreignFieldDescriptor); + Message.Builder foreignFieldBuilder1 = builder1.newBuilderForField(foreignFieldDescriptor); FieldDescriptor subForeignFieldDescriptor1 = foreignFieldBuilder1.getDescriptorForType().findFieldByName("c"); foreignFieldBuilder1.setField(subForeignFieldDescriptor1, 2); - builder1.addRepeatedField(foreignFieldDescriptor, - foreignFieldBuilder1.build()); + builder1.addRepeatedField(foreignFieldDescriptor, foreignFieldBuilder1.build()); // Mutate import message - Message.Builder importFieldBuilder1 = builder1.newBuilderForField( - importFieldDescriptor); + Message.Builder importFieldBuilder1 = builder1.newBuilderForField(importFieldDescriptor); FieldDescriptor subImportFieldDescriptor1 = importFieldBuilder1.getDescriptorForType().findFieldByName("d"); importFieldBuilder1.setField(subImportFieldDescriptor1, 3); - builder1.addRepeatedField(importFieldDescriptor, - importFieldBuilder1.build()); + builder1.addRepeatedField(importFieldDescriptor, importFieldBuilder1.build()); Message newMessage1 = builder1.build(); @@ -1563,29 +1575,24 @@ public class GeneratedMessageTest extends TestCase { // Mutate nested message TestAllTypes.Builder builder2 = TestAllTypes.newBuilder(); builder2.addRepeatedNestedMessageBuilder(); - Message.Builder fieldBuilder2 = builder2.getRepeatedFieldBuilder( - fieldDescriptor, 0); + Message.Builder fieldBuilder2 = builder2.getRepeatedFieldBuilder(fieldDescriptor, 0); FieldDescriptor subFieldDescriptor2 = fieldBuilder2.getDescriptorForType().findFieldByName("bb"); fieldBuilder2.setField(subFieldDescriptor2, 1); // Mutate foreign message - Message.Builder foreignFieldBuilder2 = builder2.newBuilderForField( - foreignFieldDescriptor); + Message.Builder foreignFieldBuilder2 = builder2.newBuilderForField(foreignFieldDescriptor); FieldDescriptor subForeignFieldDescriptor2 = foreignFieldBuilder2.getDescriptorForType().findFieldByName("c"); foreignFieldBuilder2.setField(subForeignFieldDescriptor2, 2); - builder2.addRepeatedField(foreignFieldDescriptor, - foreignFieldBuilder2.build()); + builder2.addRepeatedField(foreignFieldDescriptor, foreignFieldBuilder2.build()); // Mutate import message - Message.Builder importFieldBuilder2 = builder2.newBuilderForField( - importFieldDescriptor); + Message.Builder importFieldBuilder2 = builder2.newBuilderForField(importFieldDescriptor); FieldDescriptor subImportFieldDescriptor2 = importFieldBuilder2.getDescriptorForType().findFieldByName("d"); importFieldBuilder2.setField(subImportFieldDescriptor2, 3); - builder2.addRepeatedField(importFieldDescriptor, - importFieldBuilder2.build()); + builder2.addRepeatedField(importFieldDescriptor, importFieldBuilder2.build()); Message newMessage2 = builder2.build(); @@ -1595,10 +1602,9 @@ public class GeneratedMessageTest extends TestCase { public void testGetRepeatedFieldBuilderWithInitializedValue() { Descriptor descriptor = TestAllTypes.getDescriptor(); - FieldDescriptor fieldDescriptor = - descriptor.findFieldByName("repeated_nested_message"); + FieldDescriptor fieldDescriptor = descriptor.findFieldByName("repeated_nested_message"); - // Before setting field, builder is initialized by default value. + // Before setting field, builder is initialized by default value. TestAllTypes.Builder builder = TestAllTypes.newBuilder(); builder.addRepeatedNestedMessageBuilder(); NestedMessage.Builder fieldBuilder = @@ -1607,12 +1613,10 @@ public class GeneratedMessageTest extends TestCase { // Setting field value with new field builder instance. builder = TestAllTypes.newBuilder(); - NestedMessage.Builder newFieldBuilder = - builder.addRepeatedNestedMessageBuilder(); + NestedMessage.Builder newFieldBuilder = builder.addRepeatedNestedMessageBuilder(); newFieldBuilder.setBb(2); // Then get the field builder instance by getRepeatedFieldBuilder(). - fieldBuilder = - (NestedMessage.Builder) builder.getRepeatedFieldBuilder(fieldDescriptor, 0); + fieldBuilder = (NestedMessage.Builder) builder.getRepeatedFieldBuilder(fieldDescriptor, 0); // It should contain new value. assertEquals(2, fieldBuilder.getBb()); // These two builder should be equal. @@ -1629,8 +1633,7 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } try { - builder.getRepeatedFieldBuilder( - descriptor.findFieldByName("repeated_nested_enum"), 0); + builder.getRepeatedFieldBuilder(descriptor.findFieldByName("repeated_nested_enum"), 0); fail("Exception was not thrown"); } catch (UnsupportedOperationException e) { // We expect this exception. @@ -1642,15 +1645,13 @@ public class GeneratedMessageTest extends TestCase { // We expect this exception. } try { - builder.getRepeatedFieldBuilder( - descriptor.findFieldByName("optional_nested_enum"), 0); + builder.getRepeatedFieldBuilder(descriptor.findFieldByName("optional_nested_enum"), 0); fail("Exception was not thrown"); } catch (UnsupportedOperationException e) { // We expect this exception. } try { - builder.getRepeatedFieldBuilder( - descriptor.findFieldByName("optional_nested_message"), 0); + builder.getRepeatedFieldBuilder(descriptor.findFieldByName("optional_nested_message"), 0); fail("Exception was not thrown"); } catch (UnsupportedOperationException e) { // We expect this exception. diff --git a/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java b/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java index 9edc43441c..51ebc987d0 100644 --- a/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java @@ -45,10 +45,8 @@ import junit.framework.TestCase; */ public class IntArrayListTest extends TestCase { - private static final IntArrayList UNARY_LIST = - newImmutableIntArrayList(1); - private static final IntArrayList TERTIARY_LIST = - newImmutableIntArrayList(1, 2, 3); + private static final IntArrayList UNARY_LIST = newImmutableIntArrayList(1); + private static final IntArrayList TERTIARY_LIST = newImmutableIntArrayList(1, 2, 3); private IntArrayList list; @@ -225,9 +223,7 @@ public class IntArrayListTest extends TestCase { for (int i = 0; i < 6; i++) { list.add(Integer.valueOf(5 + i)); } - assertEquals( - asList(0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10), - list); + assertEquals(asList(0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10), list); try { list.add(-1, 5); @@ -270,6 +266,13 @@ public class IntArrayListTest extends TestCase { assertFalse(list.addAll(IntArrayList.emptyList())); } + public void testEquals() { + IntArrayList list1 = new IntArrayList(); + IntArrayList list2 = new IntArrayList(); + + assertEquals(list1, list2); + } + public void testRemove() { list.addAll(TERTIARY_LIST); assertEquals(1, (int) list.remove(0)); @@ -298,17 +301,24 @@ public class IntArrayListTest extends TestCase { } } - public void testRemoveEndOfCapacity() { - IntList toRemove = - IntArrayList.emptyList().mutableCopyWithCapacity(1); + public void testRemoveEnd_listAtCapacity() { + IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addInt(3); toRemove.remove(0); assertEquals(0, toRemove.size()); } + public void testRemove_listAtCapacity() { + IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(2); + toRemove.addInt(3); + toRemove.addInt(4); + toRemove.remove(0); + assertEquals(1, toRemove.size()); + assertEquals(4, (int) toRemove.get(0)); + } + public void testSublistRemoveEndOfCapacity() { - IntList toRemove = - IntArrayList.emptyList().mutableCopyWithCapacity(1); + IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addInt(3); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); diff --git a/java/core/src/test/java/com/google/protobuf/IsValidUtf8Test.java b/java/core/src/test/java/com/google/protobuf/IsValidUtf8Test.java index 756049b41f..6a737f1621 100644 --- a/java/core/src/test/java/com/google/protobuf/IsValidUtf8Test.java +++ b/java/core/src/test/java/com/google/protobuf/IsValidUtf8Test.java @@ -39,45 +39,36 @@ import static com.google.protobuf.IsValidUtf8TestUtil.testBytes; import com.google.protobuf.IsValidUtf8TestUtil.ByteStringFactory; import com.google.protobuf.IsValidUtf8TestUtil.Shard; - import junit.framework.TestCase; /** - * Tests cases for {@link ByteString#isValidUtf8()}. This includes three - * brute force tests that actually test every permutation of one byte, two byte, - * and three byte sequences to ensure that the method produces the right result - * for every possible byte encoding where "right" means it's consistent with - * java's UTF-8 string encoding/decoding such that the method returns true for - * any sequence that will round trip when converted to a String and then back to - * bytes and will return false for any sequence that will not round trip. - * See also {@link IsValidUtf8FourByteTest}. It also includes some - * other more targeted tests. + * Tests cases for {@link ByteString#isValidUtf8()}. This includes three brute force tests that + * actually test every permutation of one byte, two byte, and three byte sequences to ensure that + * the method produces the right result for every possible byte encoding where "right" means it's + * consistent with java's UTF-8 string encoding/decoding such that the method returns true for any + * sequence that will round trip when converted to a String and then back to bytes and will return + * false for any sequence that will not round trip. See also {@link IsValidUtf8FourByteTest}. It + * also includes some other more targeted tests. * * @author jonp@google.com (Jon Perlow) * @author martinrb@google.com (Martin Buchholz) */ public class IsValidUtf8Test extends TestCase { - /** - * Tests that round tripping of all two byte permutations work. - */ + /** Tests that round tripping of all two byte permutations work. */ public void testIsValidUtf8_1Byte() { testBytes(LITERAL_FACTORY, 1, EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT); testBytes(HEAP_NIO_FACTORY, 1, EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT); testBytes(DIRECT_NIO_FACTORY, 1, EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT); } - /** - * Tests that round tripping of all two byte permutations work. - */ + /** Tests that round tripping of all two byte permutations work. */ public void testIsValidUtf8_2Bytes() { testBytes(LITERAL_FACTORY, 2, IsValidUtf8TestUtil.EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT); testBytes(HEAP_NIO_FACTORY, 2, IsValidUtf8TestUtil.EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT); testBytes(DIRECT_NIO_FACTORY, 2, IsValidUtf8TestUtil.EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT); } - /** - * Tests that round tripping of all three byte permutations work. - */ + /** Tests that round tripping of all three byte permutations work. */ public void testIsValidUtf8_3Bytes() { // Travis' OOM killer doesn't like this test if (System.getenv("TRAVIS") == null) { @@ -88,10 +79,9 @@ public class IsValidUtf8Test extends TestCase { } /** - * Tests that round tripping of a sample of four byte permutations work. - * All permutations are prohibitively expensive to test for automated runs; - * {@link IsValidUtf8FourByteTest} is used for full coverage. This method - * tests specific four-byte cases. + * Tests that round tripping of a sample of four byte permutations work. All permutations are + * prohibitively expensive to test for automated runs; {@link IsValidUtf8FourByteTest} is used for + * full coverage. This method tests specific four-byte cases. */ public void testIsValidUtf8_4BytesSamples() { // Valid 4 byte. @@ -106,9 +96,7 @@ public class IsValidUtf8Test extends TestCase { assertInvalidUtf8(0xF4, 0x90, 0xAD, 0xA2); } - /** - * Tests some hard-coded test cases. - */ + /** Tests some hard-coded test cases. */ public void testSomeSequences() { // Empty assertTrue(asBytes("").isValidUtf8()); @@ -149,11 +137,12 @@ public class IsValidUtf8Test extends TestCase { assertTrue(not ^ leaf.isValidUtf8()); assertTrue(not ^ sub.isValidUtf8()); ByteString[] ropes = { - RopeByteString.newInstanceForTest(ByteString.EMPTY, leaf), - RopeByteString.newInstanceForTest(ByteString.EMPTY, sub), - RopeByteString.newInstanceForTest(leaf, ByteString.EMPTY), - RopeByteString.newInstanceForTest(sub, ByteString.EMPTY), - RopeByteString.newInstanceForTest(sub, leaf)}; + RopeByteString.newInstanceForTest(ByteString.EMPTY, leaf), + RopeByteString.newInstanceForTest(ByteString.EMPTY, sub), + RopeByteString.newInstanceForTest(leaf, ByteString.EMPTY), + RopeByteString.newInstanceForTest(sub, ByteString.EMPTY), + RopeByteString.newInstanceForTest(sub, leaf) + }; for (ByteString rope : ropes) { assertTrue(not ^ rope.isValidUtf8()); } @@ -179,7 +168,7 @@ public class IsValidUtf8Test extends TestCase { // A sanity check. int actual = 0; for (Shard shard : IsValidUtf8TestUtil.FOUR_BYTE_SHARDS) { - actual += shard.expected; + actual = (int) (actual + shard.expected); } assertEquals(IsValidUtf8TestUtil.EXPECTED_FOUR_BYTE_ROUNDTRIPPABLE_COUNT, actual); } diff --git a/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java b/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java index 1bcf63e7d9..67d2f5961d 100644 --- a/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java +++ b/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java @@ -50,8 +50,7 @@ import java.util.Random; import java.util.logging.Logger; /** - * Shared testing code for {@link IsValidUtf8Test} and - * {@link IsValidUtf8FourByteTest}. + * Shared testing code for {@link IsValidUtf8Test} and {@link IsValidUtf8FourByteTest}. * * @author jonp@google.com (Jon Perlow) * @author martinrb@google.com (Martin Buchholz) @@ -65,44 +64,47 @@ final class IsValidUtf8TestUtil { ByteString newByteString(byte[] bytes); } - static final ByteStringFactory LITERAL_FACTORY = new ByteStringFactory() { - @Override - public ByteString newByteString(byte[] bytes) { - return ByteString.wrap(bytes); - } - }; + static final ByteStringFactory LITERAL_FACTORY = + new ByteStringFactory() { + @Override + public ByteString newByteString(byte[] bytes) { + return ByteString.wrap(bytes); + } + }; - static final ByteStringFactory HEAP_NIO_FACTORY = new ByteStringFactory() { - @Override - public ByteString newByteString(byte[] bytes) { - return new NioByteString(ByteBuffer.wrap(bytes)); - } - }; + static final ByteStringFactory HEAP_NIO_FACTORY = + new ByteStringFactory() { + @Override + public ByteString newByteString(byte[] bytes) { + return new NioByteString(ByteBuffer.wrap(bytes)); + } + }; private static ThreadLocal> directBuffer = new ThreadLocal>(); /** - * Factory for direct {@link ByteBuffer} instances. To reduce direct memory usage, this - * uses a thread local direct buffer. This means that each call will overwrite the buffer's - * contents from the previous call, so the calling code must be careful not to continue using - * a buffer returned from a previous invocation. + * Factory for direct {@link ByteBuffer} instances. To reduce direct memory usage, this uses a + * thread local direct buffer. This means that each call will overwrite the buffer's contents from + * the previous call, so the calling code must be careful not to continue using a buffer returned + * from a previous invocation. */ - static final ByteStringFactory DIRECT_NIO_FACTORY = new ByteStringFactory() { - @Override - public ByteString newByteString(byte[] bytes) { - SoftReference ref = directBuffer.get(); - ByteBuffer buffer = ref == null ? null : ref.get(); - if (buffer == null || buffer.capacity() < bytes.length) { - buffer = ByteBuffer.allocateDirect(bytes.length); - directBuffer.set(new SoftReference(buffer)); - } - buffer.clear(); - buffer.put(bytes); - buffer.flip(); - return new NioByteString(buffer); - } - }; + static final ByteStringFactory DIRECT_NIO_FACTORY = + new ByteStringFactory() { + @Override + public ByteString newByteString(byte[] bytes) { + SoftReference ref = directBuffer.get(); + ByteBuffer buffer = ref == null ? null : ref.get(); + if (buffer == null || buffer.capacity() < bytes.length) { + buffer = ByteBuffer.allocateDirect(bytes.length); + directBuffer.set(new SoftReference(buffer)); + } + buffer.clear(); + buffer.put(bytes); + buffer.flip(); + return new NioByteString(buffer); + } + }; // 128 - [chars 0x0000 to 0x007f] static final long ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS = 0x007f - 0x0000 + 1; @@ -116,9 +118,10 @@ final class IsValidUtf8TestUtil { // 18,304 static final long EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT = // Both bytes are one byte characters - (long) Math.pow(EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, 2) + - // The possible number of two byte characters - TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS; + (long) Math.pow(EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, 2) + + + // The possible number of two byte characters + TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS; // 2048 static final long THREE_BYTE_SURROGATES = 2 * 1024; @@ -130,11 +133,13 @@ final class IsValidUtf8TestUtil { // 2,650,112 static final long EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT = // All one byte characters - (long) Math.pow(EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, 3) + - // One two byte character and a one byte character - 2 * TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS * ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS + - // Three byte characters - THREE_BYTE_ROUNDTRIPPABLE_CHARACTERS; + (long) Math.pow(EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, 3) + + + // One two byte character and a one byte character + 2 * TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS * ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS + + + // Three byte characters + THREE_BYTE_ROUNDTRIPPABLE_CHARACTERS; // 1,048,576 [chars 0x10000L to 0x10FFFF] static final long FOUR_BYTE_ROUNDTRIPPABLE_CHARACTERS = 0x10FFFF - 0x10000L + 1; @@ -142,17 +147,22 @@ final class IsValidUtf8TestUtil { // 289,571,839 static final long EXPECTED_FOUR_BYTE_ROUNDTRIPPABLE_COUNT = // All one byte characters - (long) Math.pow(EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, 4) + - // One and three byte characters - 2 * THREE_BYTE_ROUNDTRIPPABLE_CHARACTERS * ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS + - // Two two byte characters - TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS * TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS + - // Permutations of one and two byte characters - 3 * TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS * ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS - * ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS - + - // Four byte characters - FOUR_BYTE_ROUNDTRIPPABLE_CHARACTERS; + (long) Math.pow(EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, 4) + + + // One and three byte characters + 2 * THREE_BYTE_ROUNDTRIPPABLE_CHARACTERS * ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS + + + // Two two byte characters + TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS * TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS + + + // Permutations of one and two byte characters + 3 + * TWO_BYTE_ROUNDTRIPPABLE_CHARACTERS + * ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS + * ONE_BYTE_ROUNDTRIPPABLE_CHARACTERS + + + // Four byte characters + FOUR_BYTE_ROUNDTRIPPABLE_CHARACTERS; static final class Shard { final long index; @@ -160,7 +170,6 @@ final class IsValidUtf8TestUtil { final long lim; final long expected; - public Shard(long index, long start, long lim, long expected) { assertTrue(start < lim); this.index = index; @@ -206,13 +215,12 @@ final class IsValidUtf8TestUtil { static final List FOUR_BYTE_SHARDS = generateFourByteShards(128, FOUR_BYTE_SHARDS_EXPECTED_ROUNTRIPPABLES); - private static List generateFourByteShards(int numShards, long[] expected) { assertEquals(numShards, expected.length); List shards = new ArrayList(numShards); - long LIM = 1L << 32; - long increment = LIM / numShards; - assertTrue(LIM % numShards == 0); + long lim = 1L << 32; + long increment = lim / numShards; + assertTrue(lim % numShards == 0); for (int i = 0; i < numShards; i++) { shards.add(new Shard(i, increment * i, increment * (i + 1), expected[i])); } @@ -220,8 +228,7 @@ final class IsValidUtf8TestUtil { } /** - * Helper to run the loop to test all the permutations for the number of bytes - * specified. + * Helper to run the loop to test all the permutations for the number of bytes specified. * * @param factory the factory for {@link ByteString} instances. * @param numBytes the number of bytes in the byte array @@ -232,16 +239,15 @@ final class IsValidUtf8TestUtil { } /** - * Helper to run the loop to test all the permutations for the number of bytes - * specified. This overload is useful for debugging to get the loop to start - * at a certain character. + * Helper to run the loop to test all the permutations for the number of bytes specified. This + * overload is useful for debugging to get the loop to start at a certain character. * * @param factory the factory for {@link ByteString} instances. * @param numBytes the number of bytes in the byte array * @param expectedCount the expected number of roundtrippable permutations * @param start the starting bytes encoded as a long as big-endian - * @param lim the limit of bytes to process encoded as a long as big-endian, - * or -1 to mean the max limit for numBytes + * @param lim the limit of bytes to process encoded as a long as big-endian, or -1 to mean the max + * limit for numBytes */ static void testBytes( ByteStringFactory factory, int numBytes, long expectedCount, long start, long lim) { @@ -301,9 +307,10 @@ final class IsValidUtf8TestUtil { assertEquals(isRoundTrippable, (state3 == Utf8.COMPLETE)); // Test ropes built out of small partial sequences - ByteString rope = RopeByteString.newInstanceForTest( - bs.substring(0, i), - RopeByteString.newInstanceForTest(bs.substring(i, j), bs.substring(j, numBytes))); + ByteString rope = + RopeByteString.newInstanceForTest( + bs.substring(0, i), + RopeByteString.newInstanceForTest(bs.substring(i, j), bs.substring(j, numBytes))); assertSame(RopeByteString.class, rope.getClass()); ByteString[] byteStrings = {bs, bs.substring(0, numBytes), rope}; @@ -336,27 +343,28 @@ final class IsValidUtf8TestUtil { } /** - * Variation of {@link #testBytes} that does less allocation using the - * low-level encoders/decoders directly. Checked in because it's useful for - * debugging when trying to process bytes faster, but since it doesn't use the - * actual String class, it's possible for incompatibilities to develop + * Variation of {@link #testBytes} that does less allocation using the low-level encoders/decoders + * directly. Checked in because it's useful for debugging when trying to process bytes faster, but + * since it doesn't use the actual String class, it's possible for incompatibilities to develop * (although unlikely). * * @param factory the factory for {@link ByteString} instances. * @param numBytes the number of bytes in the byte array * @param expectedCount the expected number of roundtrippable permutations * @param start the starting bytes encoded as a long as big-endian - * @param lim the limit of bytes to process encoded as a long as big-endian, - * or -1 to mean the max limit for numBytes + * @param lim the limit of bytes to process encoded as a long as big-endian, or -1 to mean the max + * limit for numBytes */ static void testBytesUsingByteBuffers( ByteStringFactory factory, int numBytes, long expectedCount, long start, long lim) { CharsetDecoder decoder = - Internal.UTF_8.newDecoder() + Internal.UTF_8 + .newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); CharsetEncoder encoder = - Internal.UTF_8.newEncoder() + Internal.UTF_8 + .newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); byte[] bytes = new byte[numBytes]; @@ -434,8 +442,10 @@ final class IsValidUtf8TestUtil { } private static void outputFailure(long byteChar, byte[] bytes, byte[] after, int len) { - fail("Failure: (" + Long.toHexString(byteChar) + ") " + toHexString(bytes) + " => " - + toHexString(after, len)); + fail( + String.format( + "Failure: (%s) %s => %s", + Long.toHexString(byteChar), toHexString(bytes), toHexString(after, len))); } private static String toHexString(byte[] b) { diff --git a/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java b/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java index 813fe6bc69..8c13acae42 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java @@ -117,8 +117,8 @@ public class LazyFieldLiteTest extends TestCase { LazyFieldLite original = createLazyFieldLiteFromMessage(message); LazyFieldLite merged = new LazyFieldLite(); merged.merge(original); - TestAllExtensions value = (TestAllExtensions) merged.getValue( - TestAllExtensions.getDefaultInstance()); + TestAllExtensions value = + (TestAllExtensions) merged.getValue(TestAllExtensions.getDefaultInstance()); assertEquals(message, value); } @@ -130,8 +130,8 @@ public class LazyFieldLiteTest extends TestCase { public void testInvalidProto() throws Exception { // Silently fails and uses the default instance. - LazyFieldLite field = new LazyFieldLite( - TestUtil.getExtensionRegistry(), ByteString.copyFromUtf8("invalid")); + LazyFieldLite field = + new LazyFieldLite(TestUtil.getExtensionRegistry(), ByteString.copyFromUtf8("invalid")); assertEquals( TestAllTypes.getDefaultInstance(), field.getValue(TestAllTypes.getDefaultInstance())); assertEquals(0, field.getSerializedSize()); @@ -158,7 +158,7 @@ public class LazyFieldLiteTest extends TestCase { TestAllTypes.newBuilder().setOptionalInt32(1).setOptionalInt64(2).build(); LazyFieldLite field1 = LazyFieldLite.fromValue(message1); - field1.getValue(TestAllTypes.getDefaultInstance()); // Force parsing. + field1.getValue(TestAllTypes.getDefaultInstance()); // Force parsing. LazyFieldLite field2 = createLazyFieldLiteFromMessage(message2); field1.merge(field2); assertEquals(expected, field1.getValue(TestAllTypes.getDefaultInstance())); @@ -166,7 +166,7 @@ public class LazyFieldLiteTest extends TestCase { // Now reverse which one is parsed first. field1 = LazyFieldLite.fromValue(message1); field2 = createLazyFieldLiteFromMessage(message2); - field2.getValue(TestAllTypes.getDefaultInstance()); // Force parsing. + field2.getValue(TestAllTypes.getDefaultInstance()); // Force parsing. field1.merge(field2); assertEquals(expected, field1.getValue(TestAllTypes.getDefaultInstance())); } @@ -175,8 +175,8 @@ public class LazyFieldLiteTest extends TestCase { // Test a few different paths that involve one message that was not parsed. TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(1).build(); LazyFieldLite valid = LazyFieldLite.fromValue(message); - LazyFieldLite invalid = new LazyFieldLite( - TestUtil.getExtensionRegistry(), ByteString.copyFromUtf8("invalid")); + LazyFieldLite invalid = + new LazyFieldLite(TestUtil.getExtensionRegistry(), ByteString.copyFromUtf8("invalid")); invalid.merge(valid); // We swallow the exception and just use the set field. @@ -206,13 +206,13 @@ public class LazyFieldLiteTest extends TestCase { // Now try parsing the empty field first. field = LazyFieldLite.fromValue(messageWithExtensions); LazyFieldLite other = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage); - other.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing. + other.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing. field.merge(other); assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance())); // And again reverse. field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage); - field.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing. + field.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing. other = LazyFieldLite.fromValue(messageWithExtensions); field.merge(other); assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance())); @@ -239,8 +239,7 @@ public class LazyFieldLiteTest extends TestCase { } private void assertNotEqual(Object unexpected, Object actual) { - assertFalse(unexpected == actual - || (unexpected != null && unexpected.equals(actual))); + assertFalse(unexpected == actual || (unexpected != null && unexpected.equals(actual))); } } diff --git a/java/core/src/test/java/com/google/protobuf/LazyFieldTest.java b/java/core/src/test/java/com/google/protobuf/LazyFieldTest.java index f27e8e51b2..a3901e91b9 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyFieldTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyFieldTest.java @@ -42,8 +42,7 @@ import junit.framework.TestCase; public class LazyFieldTest extends TestCase { public void testHashCode() { MessageLite message = TestUtil.getAllSet(); - LazyField lazyField = - createLazyFieldFromMessage(message); + LazyField lazyField = createLazyFieldFromMessage(message); assertEquals(message.hashCode(), lazyField.hashCode()); lazyField.getValue(); assertEquals(message.hashCode(), lazyField.hashCode()); @@ -102,8 +101,8 @@ public class LazyFieldTest extends TestCase { private LazyField createLazyFieldFromMessage(MessageLite message) { ByteString bytes = message.toByteString(); - return new LazyField(message.getDefaultInstanceForType(), - TestUtil.getExtensionRegistry(), bytes); + return new LazyField( + message.getDefaultInstanceForType(), TestUtil.getExtensionRegistry(), bytes); } private void changeValue(LazyField lazyField) { @@ -114,7 +113,6 @@ public class LazyFieldTest extends TestCase { } private void assertNotEqual(Object unexpected, Object actual) { - assertFalse(unexpected == actual - || (unexpected != null && unexpected.equals(actual))); + assertFalse(unexpected == actual || (unexpected != null && unexpected.equals(actual))); } } diff --git a/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java b/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java index 968ca2065c..c5880d5065 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java @@ -44,8 +44,6 @@ import junit.framework.TestCase; */ public class LazyMessageLiteTest extends TestCase { - private Parser originalLazyInnerMessageLiteParser; - @Override protected void setUp() throws Exception { super.setUp(); @@ -57,19 +55,16 @@ public class LazyMessageLiteTest extends TestCase { } public void testSetValues() { - LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder() - .setNum(3) - .build(); - LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() - .setNum(2) - .setNested(nested) - .build(); - LazyMessageLite outer = LazyMessageLite.newBuilder() - .setNum(1) - .setInner(inner) - .setOneofNum(123) - .setOneofInner(inner) - .build(); + LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder().setNum(3).build(); + LazyInnerMessageLite inner = + LazyInnerMessageLite.newBuilder().setNum(2).setNested(nested).build(); + LazyMessageLite outer = + LazyMessageLite.newBuilder() + .setNum(1) + .setInner(inner) + .setOneofNum(123) + .setOneofInner(inner) + .build(); assertEquals(1, outer.getNum()); assertEquals(421, outer.getNumWithDefault()); @@ -90,44 +85,43 @@ public class LazyMessageLiteTest extends TestCase { } public void testSetRepeatedValues() { - LazyMessageLite outer = LazyMessageLite.newBuilder() - .setNum(1) - .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) - .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) - .build(); + LazyMessageLite outer = + LazyMessageLite.newBuilder() + .setNum(1) + .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) + .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) + .build(); assertEquals(1, outer.getNum()); assertEquals(2, outer.getRepeatedInnerCount()); assertEquals(119, outer.getRepeatedInner(0).getNum()); assertEquals(122, outer.getRepeatedInner(1).getNum()); } - + public void testRepeatedMutability() throws Exception { - LazyMessageLite outer = LazyMessageLite.newBuilder() - .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) - .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) - .build(); - + LazyMessageLite outer = + LazyMessageLite.newBuilder() + .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) + .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) + .build(); + outer = LazyMessageLite.parseFrom(outer.toByteArray()); try { outer.getRepeatedInnerList().set(1, null); fail(); - } catch (UnsupportedOperationException expected) {} + } catch (UnsupportedOperationException expected) { + } } public void testAddAll() { - ArrayList inners = new ArrayList(); + ArrayList inners = new ArrayList<>(); int count = 4; for (int i = 0; i < count; i++) { - LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() - .setNum(i) - .build(); + LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder().setNum(i).build(); inners.add(inner); } - LazyMessageLite outer = LazyMessageLite.newBuilder() - .addAllRepeatedInner(inners) - .build(); + LazyMessageLite outer = LazyMessageLite.newBuilder().addAllRepeatedInner(inners).build(); assertEquals(count, outer.getRepeatedInnerCount()); for (int i = 0; i < count; i++) { assertEquals(i, outer.getRepeatedInner(i).getNum()); @@ -135,8 +129,7 @@ public class LazyMessageLiteTest extends TestCase { } public void testGetDefaultValues() { - LazyMessageLite outer = LazyMessageLite.newBuilder() - .build(); + LazyMessageLite outer = LazyMessageLite.getDefaultInstance(); assertEquals(0, outer.getNum()); assertEquals(421, outer.getNumWithDefault()); @@ -156,15 +149,12 @@ public class LazyMessageLiteTest extends TestCase { } public void testClearValues() { - LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() - .setNum(115) - .build(); + LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder().setNum(115).build(); LazyMessageLite.Builder outerBuilder = LazyMessageLite.newBuilder(); assertEquals(0, outerBuilder.build().getNum()); - // Set/Clear num outerBuilder.setNum(100); @@ -178,9 +168,9 @@ public class LazyMessageLiteTest extends TestCase { assertEquals(421, outerBuilder.build().getNumWithDefault()); assertFalse(outerBuilder.build().hasInner()); - // Set/Clear all - outerBuilder.setNum(100) + outerBuilder + .setNum(100) .setInner(inner) .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) @@ -210,23 +200,17 @@ public class LazyMessageLiteTest extends TestCase { } public void testMergeValues() { - LazyMessageLite outerBase = LazyMessageLite.newBuilder() - .setNumWithDefault(122) - .build(); - - LazyInnerMessageLite innerMerging = LazyInnerMessageLite.newBuilder() - .setNum(115) - .build(); - LazyMessageLite outerMerging = LazyMessageLite.newBuilder() - .setNum(119) - .setInner(innerMerging) - .setOneofInner(innerMerging) - .build(); - - LazyMessageLite merged = LazyMessageLite - .newBuilder(outerBase) - .mergeFrom(outerMerging) - .build(); + LazyMessageLite outerBase = LazyMessageLite.newBuilder().setNumWithDefault(122).build(); + + LazyInnerMessageLite innerMerging = LazyInnerMessageLite.newBuilder().setNum(115).build(); + LazyMessageLite outerMerging = + LazyMessageLite.newBuilder() + .setNum(119) + .setInner(innerMerging) + .setOneofInner(innerMerging) + .build(); + + LazyMessageLite merged = LazyMessageLite.newBuilder(outerBase).mergeFrom(outerMerging).build(); assertEquals(119, merged.getNum()); assertEquals(122, merged.getNumWithDefault()); assertEquals(115, merged.getInner().getNum()); @@ -236,23 +220,18 @@ public class LazyMessageLiteTest extends TestCase { } public void testMergeDefaultValues() { - LazyInnerMessageLite innerBase = LazyInnerMessageLite.newBuilder() - .setNum(115) - .build(); - LazyMessageLite outerBase = LazyMessageLite.newBuilder() - .setNum(119) - .setNumWithDefault(122) - .setInner(innerBase) - .setOneofInner(innerBase) - .build(); - - LazyMessageLite outerMerging = LazyMessageLite.newBuilder() - .build(); - - LazyMessageLite merged = LazyMessageLite - .newBuilder(outerBase) - .mergeFrom(outerMerging) - .build(); + LazyInnerMessageLite innerBase = LazyInnerMessageLite.newBuilder().setNum(115).build(); + LazyMessageLite outerBase = + LazyMessageLite.newBuilder() + .setNum(119) + .setNumWithDefault(122) + .setInner(innerBase) + .setOneofInner(innerBase) + .build(); + + LazyMessageLite outerMerging = LazyMessageLite.getDefaultInstance(); + + LazyMessageLite merged = LazyMessageLite.newBuilder(outerBase).mergeFrom(outerMerging).build(); // Merging default-instance shouldn't overwrite values in the base message. assertEquals(119, merged.getNum()); assertEquals(122, merged.getNumWithDefault()); @@ -264,7 +243,7 @@ public class LazyMessageLiteTest extends TestCase { // Regression test for b/28198805. public void testMergeOneofMessages() throws Exception { - LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder().build(); + LazyInnerMessageLite inner = LazyInnerMessageLite.getDefaultInstance(); LazyMessageLite outer = LazyMessageLite.newBuilder().setOneofInner(inner).build(); ByteString data1 = outer.toByteString(); @@ -280,18 +259,11 @@ public class LazyMessageLiteTest extends TestCase { } public void testSerialize() throws InvalidProtocolBufferException { - LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder() - .setNum(3) - .build(); - LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() - .setNum(2) - .setNested(nested) - .build(); - LazyMessageLite outer = LazyMessageLite.newBuilder() - .setNum(1) - .setInner(inner) - .setOneofInner(inner) - .build(); + LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder().setNum(3).build(); + LazyInnerMessageLite inner = + LazyInnerMessageLite.newBuilder().setNum(2).setNested(nested).build(); + LazyMessageLite outer = + LazyMessageLite.newBuilder().setNum(1).setInner(inner).setOneofInner(inner).build(); ByteString bytes = outer.toByteString(); assertEquals(bytes.size(), outer.getSerializedSize()); @@ -299,18 +271,18 @@ public class LazyMessageLiteTest extends TestCase { LazyMessageLite deserialized = LazyMessageLite.parseFrom(bytes); assertEquals(1, deserialized.getNum()); - assertEquals(421, deserialized.getNumWithDefault()); + assertEquals(421, deserialized.getNumWithDefault()); - assertEquals(2, deserialized.getInner().getNum()); - assertEquals(42, deserialized.getInner().getNumWithDefault()); + assertEquals(2, deserialized.getInner().getNum()); + assertEquals(42, deserialized.getInner().getNumWithDefault()); - assertEquals(3, deserialized.getInner().getNested().getNum()); - assertEquals(4, deserialized.getInner().getNested().getNumWithDefault()); + assertEquals(3, deserialized.getInner().getNested().getNum()); + assertEquals(4, deserialized.getInner().getNested().getNumWithDefault()); - assertEquals(2, deserialized.getOneofInner().getNum()); - assertEquals(42, deserialized.getOneofInner().getNumWithDefault()); - assertEquals(3, deserialized.getOneofInner().getNested().getNum()); - assertEquals(4, deserialized.getOneofInner().getNested().getNumWithDefault()); + assertEquals(2, deserialized.getOneofInner().getNum()); + assertEquals(42, deserialized.getOneofInner().getNumWithDefault()); + assertEquals(3, deserialized.getOneofInner().getNested().getNum()); + assertEquals(4, deserialized.getOneofInner().getNested().getNumWithDefault()); assertEquals(bytes, deserialized.toByteString()); } @@ -318,8 +290,7 @@ public class LazyMessageLiteTest extends TestCase { public void testExtensions() throws Exception { LazyInnerMessageLite.Builder innerBuilder = LazyInnerMessageLite.newBuilder(); innerBuilder.setExtension( - LazyExtension.extension, LazyExtension.newBuilder() - .setName("name").build()); + LazyExtension.extension, LazyExtension.newBuilder().setName("name").build()); assertTrue(innerBuilder.hasExtension(LazyExtension.extension)); assertEquals("name", innerBuilder.getExtension(LazyExtension.extension).getName()); @@ -327,8 +298,7 @@ public class LazyMessageLiteTest extends TestCase { assertTrue(innerMessage.hasExtension(LazyExtension.extension)); assertEquals("name", innerMessage.getExtension(LazyExtension.extension).getName()); - LazyMessageLite lite = LazyMessageLite.newBuilder() - .setInner(innerMessage).build(); + LazyMessageLite lite = LazyMessageLite.newBuilder().setInner(innerMessage).build(); assertTrue(lite.getInner().hasExtension(LazyExtension.extension)); assertEquals("name", lite.getInner().getExtension(LazyExtension.extension).getName()); } diff --git a/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java b/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java index 2fc3124d4c..24d0038b3d 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java @@ -46,13 +46,13 @@ import junit.framework.TestCase; */ public class LazyStringArrayListTest extends TestCase { - private static String STRING_A = "A"; - private static String STRING_B = "B"; - private static String STRING_C = "C"; + private static final String STRING_A = "A"; + private static final String STRING_B = "B"; + private static final String STRING_C = "C"; - private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A"); - private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B"); - private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C"); + private static final ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A"); + private static final ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B"); + private static final ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C"); public void testJustStrings() { LazyStringArrayList list = new LazyStringArrayList(); @@ -175,7 +175,7 @@ public class LazyStringArrayListTest extends TestCase { assertSame(BYTE_STRING_B, list2.getByteString(1)); assertSame(BYTE_STRING_C, list2.getByteString(2)); } - + public void testModificationWithIteration() { LazyStringArrayList list = new LazyStringArrayList(); list.addAll(asList(STRING_A, STRING_B, STRING_C)); @@ -183,12 +183,12 @@ public class LazyStringArrayListTest extends TestCase { assertEquals(3, list.size()); assertEquals(STRING_A, list.get(0)); assertEquals(STRING_A, iterator.next()); - + // Does not structurally modify. iterator = list.iterator(); list.set(0, STRING_B); iterator.next(); - + list.remove(0); try { iterator.next(); @@ -196,7 +196,7 @@ public class LazyStringArrayListTest extends TestCase { } catch (ConcurrentModificationException e) { // expected } - + iterator = list.iterator(); list.add(0, STRING_C); try { @@ -206,7 +206,7 @@ public class LazyStringArrayListTest extends TestCase { // expected } } - + public void testMakeImmutable() { LazyStringArrayList list = new LazyStringArrayList(); list.add(STRING_A); @@ -214,52 +214,52 @@ public class LazyStringArrayListTest extends TestCase { list.add(STRING_C); list.makeImmutable(); assertGenericListImmutable(list, STRING_A); - + // LazyStringArrayList has extra methods not covered in the generic // assertion. - + try { list.add(BYTE_STRING_A.toByteArray()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.add(BYTE_STRING_A); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAllByteArray(Collections.singletonList(BYTE_STRING_A.toByteArray())); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAllByteString(asList(BYTE_STRING_A)); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.mergeFrom(new LazyStringArrayList()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.set(0, BYTE_STRING_A.toByteArray()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.set(0, BYTE_STRING_A); fail(); @@ -267,20 +267,20 @@ public class LazyStringArrayListTest extends TestCase { // expected } } - + public void testImmutabilityPropagation() { LazyStringArrayList list = new LazyStringArrayList(); list.add(STRING_A); list.makeImmutable(); assertGenericListImmutable(list.asByteStringList(), BYTE_STRING_A); - + // Arrays use reference equality so need to retrieve the underlying value // to properly test deep immutability. List byteArrayList = list.asByteArrayList(); assertGenericListImmutable(byteArrayList, byteArrayList.get(0)); } - + @SuppressWarnings("unchecked") private static void assertGenericListImmutable(List list, T value) { try { @@ -289,21 +289,21 @@ public class LazyStringArrayListTest extends TestCase { } catch (UnsupportedOperationException e) { // expected } - + try { list.add(0, value); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(asList(value)); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(0, asList(value)); fail(); @@ -317,42 +317,42 @@ public class LazyStringArrayListTest extends TestCase { } catch (UnsupportedOperationException e) { // expected } - + try { list.remove(0); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.remove(value); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.removeAll(asList(value)); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.retainAll(asList()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.retainAll(asList()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.set(0, value); fail(); diff --git a/java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java b/java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java index 006e493366..18c9c74ec3 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java @@ -30,44 +30,42 @@ package com.google.protobuf; - import protobuf_unittest.UnittestProto; import java.io.IOException; import junit.framework.TestCase; /** - * Tests to make sure the lazy conversion of UTF8-encoded byte arrays to - * strings works correctly. + * Tests to make sure the lazy conversion of UTF8-encoded byte arrays to strings works correctly. * * @author jonp@google.com (Jon Perlow) */ public class LazyStringEndToEndTest extends TestCase { - private static ByteString TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8 = - ByteString.copyFrom(new byte[] { - 114, 4, -1, 0, -1, 0, -30, 2, 4, -1, - 0, -1, 0, -30, 2, 4, -1, 0, -1, 0, }); + private static final ByteString TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8 = + ByteString.copyFrom( + new byte[] { + 114, 4, -1, 0, -1, 0, -30, 2, 4, -1, + 0, -1, 0, -30, 2, 4, -1, 0, -1, 0, + }); private ByteString encodedTestAllTypes; @Override protected void setUp() throws Exception { super.setUp(); - this.encodedTestAllTypes = UnittestProto.TestAllTypes.newBuilder() - .setOptionalString("foo") - .addRepeatedString("bar") - .addRepeatedString("baz") - .build() - .toByteString(); + this.encodedTestAllTypes = + UnittestProto.TestAllTypes.newBuilder() + .setOptionalString("foo") + .addRepeatedString("bar") + .addRepeatedString("baz") + .build() + .toByteString(); } - /** - * Tests that an invalid UTF8 string will roundtrip through a parse - * and serialization. - */ + /** Tests that an invalid UTF8 string will roundtrip through a parse and serialization. */ public void testParseAndSerialize() throws InvalidProtocolBufferException { - UnittestProto.TestAllTypes tV2 = UnittestProto.TestAllTypes.parseFrom( - TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8); + UnittestProto.TestAllTypes tV2 = + UnittestProto.TestAllTypes.parseFrom(TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8); ByteString bytes = tV2.toByteString(); assertEquals(TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8, bytes); @@ -77,33 +75,31 @@ public class LazyStringEndToEndTest extends TestCase { } public void testParseAndWrite() throws IOException { - UnittestProto.TestAllTypes tV2 = UnittestProto.TestAllTypes.parseFrom( - TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8); + UnittestProto.TestAllTypes tV2 = + UnittestProto.TestAllTypes.parseFrom(TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8); byte[] sink = new byte[TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8.size()]; CodedOutputStream outputStream = CodedOutputStream.newInstance(sink); tV2.writeTo(outputStream); outputStream.flush(); - assertEquals( - TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8, - ByteString.copyFrom(sink)); + assertEquals(TEST_ALL_TYPES_SERIALIZED_WITH_ILLEGAL_UTF8, ByteString.copyFrom(sink)); } - + public void testCaching() { String a = "a"; String b = "b"; String c = "c"; - UnittestProto.TestAllTypes proto = UnittestProto.TestAllTypes.newBuilder() - .setOptionalString(a) - .addRepeatedString(b) - .addRepeatedString(c) - .build(); + UnittestProto.TestAllTypes proto = + UnittestProto.TestAllTypes.newBuilder() + .setOptionalString(a) + .addRepeatedString(b) + .addRepeatedString(c) + .build(); // String should be the one we passed it. assertSame(a, proto.getOptionalString()); assertSame(b, proto.getRepeatedString(0)); assertSame(c, proto.getRepeatedString(1)); - // Ensure serialization keeps strings cached. proto.toByteString(); @@ -114,8 +110,7 @@ public class LazyStringEndToEndTest extends TestCase { } public void testNoStringCachingIfOnlyBytesAccessed() throws Exception { - UnittestProto.TestAllTypes proto = - UnittestProto.TestAllTypes.parseFrom(encodedTestAllTypes); + UnittestProto.TestAllTypes proto = UnittestProto.TestAllTypes.parseFrom(encodedTestAllTypes); ByteString optional = proto.getOptionalStringBytes(); assertSame(optional, proto.getOptionalStringBytes()); assertSame(optional, proto.toBuilder().getOptionalStringBytes()); diff --git a/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java index eac47448e0..0131ea2528 100644 --- a/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -46,9 +46,9 @@ import java.util.NoSuchElementException; import junit.framework.TestCase; /** - * Test {@code LiteralByteString} by setting up a reference string in {@link #setUp()}. - * This class is designed to be extended for testing extensions of {@code LiteralByteString} - * such as {@code BoundedByteString}, see {@link BoundedByteStringTest}. + * Test {@code LiteralByteString} by setting up a reference string in {@link #setUp()}. This class + * is designed to be extended for testing extensions of {@code LiteralByteString} such as {@code + * BoundedByteString}, see {@link BoundedByteStringTest}. * * @author carlanton@google.com (Carl Haverl) */ @@ -114,7 +114,9 @@ public class LiteralByteStringTest extends TestCase { } public void testSize() { - assertEquals(classUnderTest + " must have the expected size", referenceBytes.length, + assertEquals( + classUnderTest + " must have the expected size", + referenceBytes.length, stringUnderTest.size()); } @@ -146,10 +148,9 @@ public class LiteralByteStringTest extends TestCase { try { // Copy one too many bytes - stringUnderTest.copyTo(destination, stringUnderTest.size() + 1 - length, - destinationOffset, length); - fail("Should have thrown an exception when copying too many bytes of a " - + classUnderTest); + stringUnderTest.copyTo( + destination, stringUnderTest.size() + 1 - length, destinationOffset, length); + fail("Should have thrown an exception when copying too many bytes of a " + classUnderTest); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -157,8 +158,9 @@ public class LiteralByteStringTest extends TestCase { try { // Copy with illegal negative sourceOffset stringUnderTest.copyTo(destination, -1, destinationOffset, length); - fail("Should have thrown an exception when given a negative sourceOffset in " - + classUnderTest); + fail( + "Should have thrown an exception when given a negative sourceOffset in " + + classUnderTest); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -166,8 +168,9 @@ public class LiteralByteStringTest extends TestCase { try { // Copy with illegal negative destinationOffset stringUnderTest.copyTo(destination, 0, -1, length); - fail("Should have thrown an exception when given a negative destinationOffset in " - + classUnderTest); + fail( + "Should have thrown an exception when given a negative destinationOffset in " + + classUnderTest); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -175,8 +178,7 @@ public class LiteralByteStringTest extends TestCase { try { // Copy with illegal negative size stringUnderTest.copyTo(destination, 0, 0, -1); - fail("Should have thrown an exception when given a negative size in " - + classUnderTest); + fail("Should have thrown an exception when given a negative size in " + classUnderTest); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -184,8 +186,9 @@ public class LiteralByteStringTest extends TestCase { try { // Copy with illegal too-large sourceOffset stringUnderTest.copyTo(destination, 2 * stringUnderTest.size(), 0, length); - fail("Should have thrown an exception when the destinationOffset is too large in " - + classUnderTest); + fail( + "Should have thrown an exception when the destinationOffset is too large in " + + classUnderTest); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -193,8 +196,9 @@ public class LiteralByteStringTest extends TestCase { try { // Copy with illegal too-large destinationOffset stringUnderTest.copyTo(destination, 0, 2 * destination.length, length); - fail("Should have thrown an exception when the destinationOffset is too large in " - + classUnderTest); + fail( + "Should have thrown an exception when the destinationOffset is too large in " + + classUnderTest); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -203,7 +207,8 @@ public class LiteralByteStringTest extends TestCase { public void testCopyTo_ByteBuffer() { ByteBuffer myBuffer = ByteBuffer.allocate(referenceBytes.length); stringUnderTest.copyTo(myBuffer); - assertTrue(classUnderTest + ".copyTo(ByteBuffer) must give back the same bytes", + assertTrue( + classUnderTest + ".copyTo(ByteBuffer) must give back the same bytes", Arrays.equals(referenceBytes, myBuffer.array())); } @@ -233,17 +238,15 @@ public class LiteralByteStringTest extends TestCase { } /** - * Discards {@code n} bytes of data from the input stream. This method - * will block until the full amount has been skipped. Does not close the - * stream. + * Discards {@code n} bytes of data from the input stream. This method will block until the full + * amount has been skipped. Does not close the stream. + * *

Copied from com.google.common.io.ByteStreams to avoid adding dependency. * * @param in the input stream to read from * @param n the number of bytes to skip - * @throws EOFException if this stream reaches the end before skipping all - * the bytes - * @throws IOException if an I/O error occurs, or the stream does not - * support skipping + * @throws EOFException if this stream reaches the end before skipping all the bytes + * @throws IOException if an I/O error occurs, or the stream does not support skipping */ static void skipFully(InputStream in, long n) throws IOException { long toSkip = n; @@ -253,8 +256,12 @@ public class LiteralByteStringTest extends TestCase { // Force a blocking read to avoid infinite loop if (in.read() == -1) { long skipped = toSkip - n; - throw new EOFException("reached end of stream after skipping " - + skipped + " bytes; " + toSkip + " bytes expected"); + throw new EOFException( + "reached end of stream after skipping " + + skipped + + " bytes; " + + toSkip + + " bytes expected"); } n--; } else { @@ -269,7 +276,8 @@ public class LiteralByteStringTest extends TestCase { assertTrue(byteBuffer.remaining() == referenceBytes.length); assertTrue(byteBuffer.isReadOnly()); byteBuffer.get(roundTripBytes); - assertTrue(classUnderTest + ".asReadOnlyByteBuffer() must give back the same bytes", + assertTrue( + classUnderTest + ".asReadOnlyByteBuffer() must give back the same bytes", Arrays.equals(referenceBytes, roundTripBytes)); } @@ -285,13 +293,15 @@ public class LiteralByteStringTest extends TestCase { bytesSeen += thisLength; } assertTrue(bytesSeen == referenceBytes.length); - assertTrue(classUnderTest + ".asReadOnlyByteBufferTest() must give back the same bytes", + assertTrue( + classUnderTest + ".asReadOnlyByteBufferTest() must give back the same bytes", Arrays.equals(referenceBytes, roundTripBytes)); } public void testToByteArray() { byte[] roundTripBytes = stringUnderTest.toByteArray(); - assertTrue(classUnderTest + ".toByteArray() must give back the same bytes", + assertTrue( + classUnderTest + ".toByteArray() must give back the same bytes", Arrays.equals(referenceBytes, roundTripBytes)); } @@ -299,78 +309,85 @@ public class LiteralByteStringTest extends TestCase { ByteArrayOutputStream bos = new ByteArrayOutputStream(); stringUnderTest.writeTo(bos); byte[] roundTripBytes = bos.toByteArray(); - assertTrue(classUnderTest + ".writeTo() must give back the same bytes", + assertTrue( + classUnderTest + ".writeTo() must give back the same bytes", Arrays.equals(referenceBytes, roundTripBytes)); } public void testWriteToShouldNotExposeInternalBufferToOutputStream() throws IOException { - OutputStream os = new OutputStream() { - @Override - public void write(byte[] b, int off, int len) { - Arrays.fill(b, off, off + len, (byte) 0); - } - - @Override - public void write(int b) { - throw new UnsupportedOperationException(); - } - }; + OutputStream os = + new OutputStream() { + @Override + public void write(byte[] b, int off, int len) { + Arrays.fill(b, off, off + len, (byte) 0); + } + + @Override + public void write(int b) { + throw new UnsupportedOperationException(); + } + }; stringUnderTest.writeTo(os); - assertTrue(classUnderTest + ".writeTo() must not grant access to underlying array", + assertTrue( + classUnderTest + ".writeTo() must not grant access to underlying array", Arrays.equals(referenceBytes, stringUnderTest.toByteArray())); } public void testWriteToInternalShouldExposeInternalBufferToOutputStream() throws IOException { - OutputStream os = new OutputStream() { - @Override - public void write(byte[] b, int off, int len) { - Arrays.fill(b, off, off + len, (byte) 0); - } - - @Override - public void write(int b) { - throw new UnsupportedOperationException(); - } - }; + OutputStream os = + new OutputStream() { + @Override + public void write(byte[] b, int off, int len) { + Arrays.fill(b, off, off + len, (byte) 0); + } + + @Override + public void write(int b) { + throw new UnsupportedOperationException(); + } + }; stringUnderTest.writeToInternal(os, 0, stringUnderTest.size()); byte[] allZeros = new byte[stringUnderTest.size()]; - assertTrue(classUnderTest + ".writeToInternal() must grant access to underlying array", + assertTrue( + classUnderTest + ".writeToInternal() must grant access to underlying array", Arrays.equals(allZeros, stringUnderTest.toByteArray())); } public void testWriteToShouldExposeInternalBufferToByteOutput() throws IOException { - ByteOutput out = new ByteOutput() { - @Override - public void write(byte value) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public void write(byte[] value, int offset, int length) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public void writeLazy(byte[] value, int offset, int length) throws IOException { - Arrays.fill(value, offset, offset + length, (byte) 0); - } - - @Override - public void write(ByteBuffer value) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public void writeLazy(ByteBuffer value) throws IOException { - throw new UnsupportedOperationException(); - } - }; + ByteOutput out = + new ByteOutput() { + @Override + public void write(byte value) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void write(byte[] value, int offset, int length) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void write(ByteBuffer value) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) throws IOException { + Arrays.fill(value, offset, offset + length, (byte) 0); + } + + @Override + public void writeLazy(ByteBuffer value) throws IOException { + throw new UnsupportedOperationException(); + } + }; stringUnderTest.writeTo(out); byte[] allZeros = new byte[stringUnderTest.size()]; - assertTrue(classUnderTest + ".writeToInternal() must grant access to underlying array", + assertTrue( + classUnderTest + ".writeToInternal() must grant access to underlying array", Arrays.equals(allZeros, stringUnderTest.toByteArray())); } @@ -378,21 +395,22 @@ public class LiteralByteStringTest extends TestCase { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteString.Output output = ByteString.newOutput(); stringUnderTest.writeTo(output); - assertEquals("Output Size returns correct result", - output.size(), stringUnderTest.size()); + assertEquals("Output Size returns correct result", output.size(), stringUnderTest.size()); output.writeTo(bos); - assertTrue("Output.writeTo() must give back the same bytes", + assertTrue( + "Output.writeTo() must give back the same bytes", Arrays.equals(referenceBytes, bos.toByteArray())); // write the output stream to itself! This should cause it to double output.writeTo(output); - assertEquals("Writing an output stream to itself is successful", - stringUnderTest.concat(stringUnderTest), output.toByteString()); + assertEquals( + "Writing an output stream to itself is successful", + stringUnderTest.concat(stringUnderTest), + output.toByteString()); output.reset(); assertEquals("Output.reset() resets the output", 0, output.size()); - assertEquals("Output.reset() resets the output", - ByteString.EMPTY, output.toByteString()); + assertEquals("Output.reset() resets the output", ByteString.EMPTY, output.toByteString()); } public void testToString() throws UnsupportedEncodingException { @@ -410,9 +428,10 @@ public class LiteralByteStringTest extends TestCase { } public void testToString_returnsCanonicalEmptyString() { - assertSame(classUnderTest + " must be the same string references", + assertSame( + classUnderTest + " must be the same string references", ByteString.EMPTY.toString(Internal.UTF_8), - ByteString.wrap(new byte[]{}).toString(Internal.UTF_8)); + ByteString.wrap(new byte[] {}).toString(Internal.UTF_8)); } public void testToString_raisesException() { @@ -434,17 +453,23 @@ public class LiteralByteStringTest extends TestCase { public void testEquals() { assertEquals(classUnderTest + " must not equal null", false, stringUnderTest.equals(null)); assertEquals(classUnderTest + " must equal self", stringUnderTest, stringUnderTest); - assertFalse(classUnderTest + " must not equal the empty string", + assertFalse( + classUnderTest + " must not equal the empty string", stringUnderTest.equals(ByteString.EMPTY)); - assertEquals(classUnderTest + " empty strings must be equal", - ByteString.wrap(new byte[]{}), stringUnderTest.substring(55, 55)); - assertEquals(classUnderTest + " must equal another string with the same value", - stringUnderTest, ByteString.wrap(referenceBytes)); + assertEquals( + classUnderTest + " empty strings must be equal", + ByteString.wrap(new byte[] {}), + stringUnderTest.substring(55, 55)); + assertEquals( + classUnderTest + " must equal another string with the same value", + stringUnderTest, + ByteString.wrap(referenceBytes)); byte[] mungedBytes = new byte[referenceBytes.length]; System.arraycopy(referenceBytes, 0, mungedBytes, 0, referenceBytes.length); mungedBytes[mungedBytes.length - 5] = (byte) (mungedBytes[mungedBytes.length - 5] ^ 0xFF); - assertFalse(classUnderTest + " must not equal every string with the same length", + assertFalse( + classUnderTest + " must not equal every string with the same length", stringUnderTest.equals(ByteString.wrap(mungedBytes))); } @@ -454,32 +479,35 @@ public class LiteralByteStringTest extends TestCase { } public void testPeekCachedHashCode() { - assertEquals(classUnderTest + ".peekCachedHashCode() should return zero at first", 0, + assertEquals( + classUnderTest + ".peekCachedHashCode() should return zero at first", + 0, stringUnderTest.peekCachedHashCode()); stringUnderTest.hashCode(); - assertEquals(classUnderTest + ".peekCachedHashCode should return zero at first", - expectedHashCode, stringUnderTest.peekCachedHashCode()); + assertEquals( + classUnderTest + ".peekCachedHashCode should return zero at first", + expectedHashCode, + stringUnderTest.peekCachedHashCode()); } public void testPartialHash() { // partialHash() is more strenuously tested elsewhere by testing hashes of substrings. // This test would fail if the expected hash were 1. It's not. int hash = stringUnderTest.partialHash(stringUnderTest.size(), 0, stringUnderTest.size()); - assertEquals(classUnderTest + ".partialHash() must yield expected hashCode", - expectedHashCode, hash); + assertEquals( + classUnderTest + ".partialHash() must yield expected hashCode", expectedHashCode, hash); } public void testNewInput() throws IOException { InputStream input = stringUnderTest.newInput(); - assertEquals("InputStream.available() returns correct value", - stringUnderTest.size(), input.available()); + assertEquals( + "InputStream.available() returns correct value", stringUnderTest.size(), input.available()); boolean stillEqual = true; for (byte referenceByte : referenceBytes) { int expectedInt = (referenceByte & 0xFF); stillEqual = (expectedInt == input.read()); } - assertEquals("InputStream.available() returns correct value", - 0, input.available()); + assertEquals("InputStream.available() returns correct value", 0, input.available()); assertTrue(classUnderTest + " must give the same bytes from the InputStream", stillEqual); assertEquals(classUnderTest + " InputStream must now be exhausted", -1, input.read()); } @@ -490,43 +518,44 @@ public class LiteralByteStringTest extends TestCase { int nearEndIndex = stringSize * 2 / 3; long skipped1 = input.skip(nearEndIndex); assertEquals("InputStream.skip()", skipped1, nearEndIndex); - assertEquals("InputStream.available()", - stringSize - skipped1, input.available()); + assertEquals("InputStream.available()", stringSize - skipped1, input.available()); assertTrue("InputStream.mark() is available", input.markSupported()); input.mark(0); - assertEquals("InputStream.skip(), read()", - stringUnderTest.byteAt(nearEndIndex) & 0xFF, input.read()); - assertEquals("InputStream.available()", - stringSize - skipped1 - 1, input.available()); + assertEquals( + "InputStream.skip(), read()", stringUnderTest.byteAt(nearEndIndex) & 0xFF, input.read()); + assertEquals("InputStream.available()", stringSize - skipped1 - 1, input.available()); long skipped2 = input.skip(stringSize); - assertEquals("InputStream.skip() incomplete", - skipped2, stringSize - skipped1 - 1); + assertEquals("InputStream.skip() incomplete", skipped2, stringSize - skipped1 - 1); assertEquals("InputStream.skip(), no more input", 0, input.available()); assertEquals("InputStream.skip(), no more input", -1, input.read()); input.reset(); - assertEquals("InputStream.reset() succeded", - stringSize - skipped1, input.available()); - assertEquals("InputStream.reset(), read()", - stringUnderTest.byteAt(nearEndIndex) & 0xFF, input.read()); + assertEquals("InputStream.reset() succeded", stringSize - skipped1, input.available()); + assertEquals( + "InputStream.reset(), read()", stringUnderTest.byteAt(nearEndIndex) & 0xFF, input.read()); } public void testNewCodedInput() throws IOException { CodedInputStream cis = stringUnderTest.newCodedInput(); byte[] roundTripBytes = cis.readRawBytes(referenceBytes.length); - assertTrue(classUnderTest + " must give the same bytes back from the CodedInputStream", + assertTrue( + classUnderTest + " must give the same bytes back from the CodedInputStream", Arrays.equals(referenceBytes, roundTripBytes)); assertTrue(classUnderTest + " CodedInputStream must now be exhausted", cis.isAtEnd()); } /** - * Make sure we keep things simple when concatenating with empty. See also - * {@link ByteStringTest#testConcat_empty()}. + * Make sure we keep things simple when concatenating with empty. See also {@link + * ByteStringTest#testConcat_empty()}. */ public void testConcat_empty() { - assertSame(classUnderTest + " concatenated with empty must give " + classUnderTest, - stringUnderTest.concat(ByteString.EMPTY), stringUnderTest); - assertSame("empty concatenated with " + classUnderTest + " must give " + classUnderTest, - ByteString.EMPTY.concat(stringUnderTest), stringUnderTest); + assertSame( + classUnderTest + " concatenated with empty must give " + classUnderTest, + stringUnderTest.concat(ByteString.EMPTY), + stringUnderTest); + assertSame( + "empty concatenated with " + classUnderTest + " must give " + classUnderTest, + ByteString.EMPTY.concat(stringUnderTest), + stringUnderTest); } public void testJavaSerialization() throws Exception { diff --git a/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java b/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java index 14a8e15975..1935100503 100644 --- a/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java @@ -45,10 +45,8 @@ import junit.framework.TestCase; */ public class LongArrayListTest extends TestCase { - private static final LongArrayList UNARY_LIST = - newImmutableLongArrayList(1); - private static final LongArrayList TERTIARY_LIST = - newImmutableLongArrayList(1, 2, 3); + private static final LongArrayList UNARY_LIST = newImmutableLongArrayList(1); + private static final LongArrayList TERTIARY_LIST = newImmutableLongArrayList(1, 2, 3); private LongArrayList list; @@ -225,9 +223,7 @@ public class LongArrayListTest extends TestCase { for (int i = 0; i < 6; i++) { list.add(Long.valueOf(5 + i)); } - assertEquals( - asList(0L, 1L, 4L, 2L, 3L, 5L, 6L, 7L, 8L, 9L, 10L), - list); + assertEquals(asList(0L, 1L, 4L, 2L, 3L, 5L, 6L, 7L, 8L, 9L, 10L), list); try { list.add(-1, 5L); @@ -270,6 +266,13 @@ public class LongArrayListTest extends TestCase { assertFalse(list.addAll(LongArrayList.emptyList())); } + public void testEquals() { + LongArrayList list1 = new LongArrayList(); + LongArrayList list2 = new LongArrayList(); + + assertEquals(list1, list2); + } + public void testRemove() { list.addAll(TERTIARY_LIST); assertEquals(1L, (long) list.remove(0)); @@ -298,17 +301,24 @@ public class LongArrayListTest extends TestCase { } } - public void testRemoveEndOfCapacity() { - LongList toRemove = - LongArrayList.emptyList().mutableCopyWithCapacity(1); + public void testRemoveEnd_listAtCapacity() { + LongList toRemove = LongArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addLong(3); toRemove.remove(0); assertEquals(0, toRemove.size()); } + public void testRemove_listAtCapacity() { + LongList toRemove = LongArrayList.emptyList().mutableCopyWithCapacity(2); + toRemove.addLong(3); + toRemove.addLong(4); + toRemove.remove(0); + assertEquals(1, toRemove.size()); + assertEquals(4L, (long) toRemove.get(0)); + } + public void testSublistRemoveEndOfCapacity() { - LongList toRemove = - LongArrayList.emptyList().mutableCopyWithCapacity(1); + LongList toRemove = LongArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addLong(3); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); diff --git a/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java b/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java index da9195f933..adca1d51b6 100644 --- a/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java @@ -43,9 +43,7 @@ import java.util.HashMap; import java.util.Map; import junit.framework.TestCase; -/** - * Unit tests for map fields. - */ +/** Unit tests for map fields. */ public final class MapForProto2LiteTest extends TestCase { private void setMapValues(TestMap.Builder builder) { @@ -53,23 +51,18 @@ public final class MapForProto2LiteTest extends TestCase { .putInt32ToInt32Field(1, 11) .putInt32ToInt32Field(2, 22) .putInt32ToInt32Field(3, 33) - .putInt32ToStringField(1, "11") .putInt32ToStringField(2, "22") .putInt32ToStringField(3, "33") - .putInt32ToBytesField(1, TestUtil.toBytes("11")) .putInt32ToBytesField(2, TestUtil.toBytes("22")) .putInt32ToBytesField(3, TestUtil.toBytes("33")) - .putInt32ToEnumField(1, TestMap.EnumValue.FOO) .putInt32ToEnumField(2, TestMap.EnumValue.BAR) .putInt32ToEnumField(3, TestMap.EnumValue.BAZ) - .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(11).build()) .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(22).build()) .putInt32ToMessageField(3, MessageValue.newBuilder().setValue(33).build()) - .putStringToInt32Field("1", 11) .putStringToInt32Field("2", 22) .putStringToInt32Field("3", 33); @@ -129,23 +122,18 @@ public final class MapForProto2LiteTest extends TestCase { .putInt32ToInt32Field(1, 111) .removeInt32ToInt32Field(2) .putInt32ToInt32Field(4, 44) - .putInt32ToStringField(1, "111") .removeInt32ToStringField(2) .putInt32ToStringField(4, "44") - .putInt32ToBytesField(1, TestUtil.toBytes("111")) .removeInt32ToBytesField(2) .putInt32ToBytesField(4, TestUtil.toBytes("44")) - .putInt32ToEnumField(1, TestMap.EnumValue.BAR) .removeInt32ToEnumField(2) .putInt32ToEnumField(4, TestMap.EnumValue.QUX) - .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(111).build()) .removeInt32ToMessageField(2) .putInt32ToMessageField(4, MessageValue.newBuilder().setValue(44).build()) - .putStringToInt32Field("1", 111) .removeStringToInt32Field("2") .putStringToInt32Field("4", 44); @@ -265,8 +253,7 @@ public final class MapForProto2LiteTest extends TestCase { } public void testMutableMapLifecycle() { - TestMap.Builder builder = TestMap.newBuilder() - .putInt32ToInt32Field(1, 2); + TestMap.Builder builder = TestMap.newBuilder().putInt32ToInt32Field(1, 2); assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); builder.putInt32ToInt32Field(2, 3); @@ -277,8 +264,7 @@ public final class MapForProto2LiteTest extends TestCase { assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.getInt32ToEnumField()); builder.putInt32ToEnumField(2, TestMap.EnumValue.FOO); assertEquals( - newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), - builder.getInt32ToEnumField()); + newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), builder.getInt32ToEnumField()); builder.putInt32ToStringField(1, "1"); assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); @@ -287,14 +273,18 @@ public final class MapForProto2LiteTest extends TestCase { assertEquals(newMap(1, "1", 2, "2"), builder.getInt32ToStringField()); builder.putInt32ToMessageField(1, TestMap.MessageValue.getDefaultInstance()); - assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), + assertEquals( + newMap(1, TestMap.MessageValue.getDefaultInstance()), builder.build().getInt32ToMessageField()); - assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), - builder.getInt32ToMessageField()); + assertEquals( + newMap(1, TestMap.MessageValue.getDefaultInstance()), builder.getInt32ToMessageField()); builder.putInt32ToMessageField(2, TestMap.MessageValue.getDefaultInstance()); assertEquals( - newMap(1, TestMap.MessageValue.getDefaultInstance(), - 2, TestMap.MessageValue.getDefaultInstance()), + newMap( + 1, + TestMap.MessageValue.getDefaultInstance(), + 2, + TestMap.MessageValue.getDefaultInstance()), builder.getInt32ToMessageField()); } @@ -405,30 +395,22 @@ public final class MapForProto2LiteTest extends TestCase { ByteString bytes = TestUtil.toBytes("SOME BYTES"); String stringKey = "a string key"; - TestMap map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToInt32Field(5, bytes) - .build()); + TestMap map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToInt32Field(5, bytes).build()); assertEquals(0, map.getInt32ToInt32FieldOrDefault(5, -1)); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToStringField(stringKey, 5) - .build()); + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToStringField(stringKey, 5).build()); assertEquals("", map.getInt32ToStringFieldOrDefault(0, null)); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToBytesField(stringKey, 5) - .build()); + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToBytesField(stringKey, 5).build()); assertEquals(map.getInt32ToBytesFieldOrDefault(0, null), ByteString.EMPTY); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToEnumField(stringKey, bytes) - .build()); + map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToEnumField(stringKey, bytes).build()); assertEquals(TestMap.EnumValue.FOO, map.getInt32ToEnumFieldOrDefault(0, null)); try { - tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToMessageField(stringKey, bytes) - .build()); + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToMessageField(stringKey, bytes).build()); fail(); } catch (InvalidProtocolBufferException expected) { assertTrue(expected.getUnfinishedMessage() instanceof TestMap); @@ -436,9 +418,9 @@ public final class MapForProto2LiteTest extends TestCase { assertTrue(map.getInt32ToMessageField().isEmpty()); } - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putStringToInt32Field(stringKey, bytes) - .build()); + map = + tryParseTestMap( + BizarroTestMap.newBuilder().putStringToInt32Field(stringKey, bytes).build()); assertEquals(0, map.getStringToInt32FieldOrDefault(stringKey, -1)); } @@ -458,16 +440,18 @@ public final class MapForProto2LiteTest extends TestCase { // We can't control the order of elements in a HashMap. The best we can do // here is to add elements in different order. - TestMap.Builder b1 = TestMap.newBuilder() - .putInt32ToInt32Field(1, 2) - .putInt32ToInt32Field(3, 4) - .putInt32ToInt32Field(5, 6); - TestMap m1 = b1.build(); - - TestMap.Builder b2 = TestMap.newBuilder() - .putInt32ToInt32Field(5, 6) - .putInt32ToInt32Field(1, 2) - .putInt32ToInt32Field(3, 4); + TestMap m1 = + TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToInt32Field(5, 6) + .build(); + + TestMap.Builder b2 = + TestMap.newBuilder() + .putInt32ToInt32Field(5, 6) + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4); TestMap m2 = b2.build(); assertEquals(m1, m2); @@ -482,10 +466,12 @@ public final class MapForProto2LiteTest extends TestCase { } public void testUnknownEnumValues() throws Exception { - TestUnknownEnumValue.Builder builder = TestUnknownEnumValue.newBuilder() - .putInt32ToInt32Field(1, 1) - .putInt32ToInt32Field(2, 54321); - ByteString data = builder.build().toByteString(); + ByteString data = + TestUnknownEnumValue.newBuilder() + .putInt32ToInt32Field(1, 1) + .putInt32ToInt32Field(2, 54321) + .build() + .toByteString(); TestMap message = TestMap.parseFrom(data); // Entries with unknown enum values will be stored into UnknownFieldSet so @@ -494,8 +480,7 @@ public final class MapForProto2LiteTest extends TestCase { assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1)); // Serializing and parsing should preserve the unknown entry. data = message.toByteString(); - TestUnknownEnumValue messageWithUnknownEnums = - TestUnknownEnumValue.parseFrom(data); + TestUnknownEnumValue messageWithUnknownEnums = TestUnknownEnumValue.parseFrom(data); assertEquals(2, messageWithUnknownEnums.getInt32ToInt32Field().size()); assertEquals(1, messageWithUnknownEnums.getInt32ToInt32Field().get(1).intValue()); assertEquals(54321, messageWithUnknownEnums.getInt32ToInt32Field().get(2).intValue()); @@ -506,18 +491,19 @@ public final class MapForProto2LiteTest extends TestCase { setMapValues(builder); TestMap message = builder.build(); - assertEquals(Arrays.asList("1", "2", "3"), + assertEquals( + Arrays.asList("1", "2", "3"), new ArrayList(message.getStringToInt32Field().keySet())); } private static Map newMap(K key1, V value1) { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put(key1, value1); return map; } private static Map newMap(K key1, V value1, K key2, V value2) { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put(key1, value1); map.put(key2, value2); return map; @@ -527,18 +513,10 @@ public final class MapForProto2LiteTest extends TestCase { TestMap.Builder builder = TestMap.newBuilder(); setMapValues(builder); TestMap message = builder.build(); - assertEquals( - message.getStringToInt32Field(), - message.getStringToInt32FieldMap()); - assertEquals( - message.getInt32ToBytesField(), - message.getInt32ToBytesFieldMap()); - assertEquals( - message.getInt32ToEnumField(), - message.getInt32ToEnumFieldMap()); - assertEquals( - message.getInt32ToMessageField(), - message.getInt32ToMessageFieldMap()); + assertEquals(message.getStringToInt32Field(), message.getStringToInt32FieldMap()); + assertEquals(message.getInt32ToBytesField(), message.getInt32ToBytesFieldMap()); + assertEquals(message.getInt32ToEnumField(), message.getInt32ToEnumFieldMap()); + assertEquals(message.getInt32ToMessageField(), message.getInt32ToMessageFieldMap()); } public void testContains() { @@ -629,7 +607,8 @@ public final class MapForProto2LiteTest extends TestCase { assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrDefault(1, null)); assertNull(testMapOrBuilder.getInt32ToEnumFieldOrDefault(-1, null)); - assertEquals(MessageValue.newBuilder().setValue(11).build(), + assertEquals( + MessageValue.newBuilder().setValue(11).build(), testMapOrBuilder.getInt32ToMessageFieldOrDefault(1, null)); assertNull(testMapOrBuilder.getInt32ToMessageFieldOrDefault(-1, null)); @@ -687,7 +666,8 @@ public final class MapForProto2LiteTest extends TestCase { // expected } - assertEquals(MessageValue.newBuilder().setValue(11).build(), + assertEquals( + MessageValue.newBuilder().setValue(11).build(), testMapOrBuilder.getInt32ToMessageFieldOrThrow(1)); try { testMapOrBuilder.getInt32ToMessageFieldOrThrow(-1); diff --git a/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java b/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java index bcfd927c31..bb706326c4 100644 --- a/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java +++ b/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java @@ -49,9 +49,7 @@ import java.util.List; import java.util.Map; import junit.framework.TestCase; -/** - * Unit tests for map fields in proto2 protos. - */ +/** Unit tests for map fields in proto2 protos. */ public class MapForProto2Test extends TestCase { private void setMapValuesUsingMutableMap(TestMap.Builder builder) { @@ -88,23 +86,18 @@ public class MapForProto2Test extends TestCase { .putInt32ToInt32Field(1, 11) .putInt32ToInt32Field(2, 22) .putInt32ToInt32Field(3, 33) - .putInt32ToStringField(1, "11") .putInt32ToStringField(2, "22") .putInt32ToStringField(3, "33") - .putInt32ToBytesField(1, TestUtil.toBytes("11")) .putInt32ToBytesField(2, TestUtil.toBytes("22")) .putInt32ToBytesField(3, TestUtil.toBytes("33")) - .putInt32ToEnumField(1, TestMap.EnumValue.FOO) .putInt32ToEnumField(2, TestMap.EnumValue.BAR) .putInt32ToEnumField(3, TestMap.EnumValue.BAZ) - .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(11).build()) .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(22).build()) .putInt32ToMessageField(3, MessageValue.newBuilder().setValue(33).build()) - .putStringToInt32Field("1", 11) .putStringToInt32Field("2", 22) .putStringToInt32Field("3", 33); @@ -199,23 +192,18 @@ public class MapForProto2Test extends TestCase { .putInt32ToInt32Field(1, 111) .removeInt32ToInt32Field(2) .putInt32ToInt32Field(4, 44) - .putInt32ToStringField(1, "111") .removeInt32ToStringField(2) .putInt32ToStringField(4, "44") - .putInt32ToBytesField(1, TestUtil.toBytes("111")) .removeInt32ToBytesField(2) .putInt32ToBytesField(4, TestUtil.toBytes("44")) - .putInt32ToEnumField(1, TestMap.EnumValue.BAR) .removeInt32ToEnumField(2) .putInt32ToEnumField(4, TestMap.EnumValue.QUX) - .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(111).build()) .removeInt32ToMessageField(2) .putInt32ToMessageField(4, MessageValue.newBuilder().setValue(44).build()) - .putStringToInt32Field("1", 111) .removeStringToInt32Field("2") .putStringToInt32Field("4", 44); @@ -337,7 +325,7 @@ public class MapForProto2Test extends TestCase { assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); builder.getMutableInt32ToInt32Field().put(2, 3); assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); - // + // Map enumMap = builder.getMutableInt32ToEnumField(); enumMap.put(1, TestMap.EnumValue.BAR); assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnumField()); @@ -432,7 +420,19 @@ public class MapForProto2Test extends TestCase { assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); } - + // + private static Map newMap(K key1, V value1) { + Map map = new HashMap(); + map.put(key1, value1); + return map; + } + // + private static Map newMap(K key1, V value1, K key2, V value2) { + Map map = new HashMap(); + map.put(key1, value1); + map.put(key2, value2); + return map; + } public void testGettersAndSetters() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); @@ -543,30 +543,22 @@ public class MapForProto2Test extends TestCase { ByteString bytes = TestUtil.toBytes("SOME BYTES"); String stringKey = "a string key"; - TestMap map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToInt32Field(5, bytes) - .build()); + TestMap map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToInt32Field(5, bytes).build()); assertEquals(0, map.getInt32ToInt32FieldOrDefault(5, -1)); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToStringField(stringKey, 5) - .build()); + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToStringField(stringKey, 5).build()); assertEquals("", map.getInt32ToStringFieldOrDefault(0, null)); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToBytesField(stringKey, 5) - .build()); + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToBytesField(stringKey, 5).build()); assertEquals(map.getInt32ToBytesFieldOrDefault(0, null), ByteString.EMPTY); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToEnumField(stringKey, bytes) - .build()); + map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToEnumField(stringKey, bytes).build()); assertEquals(TestMap.EnumValue.FOO, map.getInt32ToEnumFieldOrDefault(0, null)); try { - tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToMessageField(stringKey, bytes) - .build()); + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToMessageField(stringKey, bytes).build()); fail(); } catch (InvalidProtocolBufferException expected) { assertTrue(expected.getUnfinishedMessage() instanceof TestMap); @@ -574,9 +566,9 @@ public class MapForProto2Test extends TestCase { assertTrue(map.getInt32ToMessageField().isEmpty()); } - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putStringToInt32Field(stringKey, bytes) - .build()); + map = + tryParseTestMap( + BizarroTestMap.newBuilder().putStringToInt32Field(stringKey, bytes).build()); assertEquals(0, map.getStringToInt32FieldOrDefault(stringKey, -1)); } @@ -657,8 +649,7 @@ public class MapForProto2Test extends TestCase { } } - private static - Message newMapEntry(Message.Builder builder, String name, KeyType key, ValueType value) { + private static Message newMapEntry(Message.Builder builder, String name, K key, V value) { FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name); Message.Builder entryBuilder = builder.newBuilderForField(field); FieldDescriptor keyField = entryBuilder.getDescriptorForType().findFieldByName("key"); @@ -677,9 +668,8 @@ public class MapForProto2Test extends TestCase { builder.setField(field, entryList); } - private static Map mapForValues( - KeyType key1, ValueType value1, KeyType key2, ValueType value2) { - Map map = new HashMap(); + private static Map mapForValues(K key1, V value1, K key2, V value2) { + Map map = new HashMap(); map.put(key1, value1); map.put(key2, value2); return map; @@ -687,17 +677,19 @@ public class MapForProto2Test extends TestCase { public void testReflectionApi() throws Exception { // In reflection API, map fields are just repeated message fields. - TestMap.Builder builder = TestMap.newBuilder() - .putInt32ToInt32Field(1, 2) - .putInt32ToInt32Field(3, 4) - .putInt32ToMessageField(11, MessageValue.newBuilder().setValue(22).build()) - .putInt32ToMessageField(33, MessageValue.newBuilder().setValue(44).build()); + TestMap.Builder builder = + TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToMessageField(11, MessageValue.newBuilder().setValue(22).build()) + .putInt32ToMessageField(33, MessageValue.newBuilder().setValue(44).build()); TestMap message = builder.build(); // Test getField(), getRepeatedFieldCount(), getRepeatedField(). - assertHasMapValues(message, "int32_to_int32_field", - mapForValues(1, 2, 3, 4)); - assertHasMapValues(message, "int32_to_message_field", + assertHasMapValues(message, "int32_to_int32_field", mapForValues(1, 2, 3, 4)); + assertHasMapValues( + message, + "int32_to_message_field", mapForValues( 11, MessageValue.newBuilder().setValue(22).build(), 33, MessageValue.newBuilder().setValue(44).build())); @@ -710,9 +702,10 @@ public class MapForProto2Test extends TestCase { assertEquals(0, message.getInt32ToMessageField().size()); // Test setField() - setMapValues(builder, "int32_to_int32_field", - mapForValues(11, 22, 33, 44)); - setMapValues(builder, "int32_to_message_field", + setMapValues(builder, "int32_to_int32_field", mapForValues(11, 22, 33, 44)); + setMapValues( + builder, + "int32_to_message_field", mapForValues( 111, MessageValue.newBuilder().setValue(222).build(), 333, MessageValue.newBuilder().setValue(444).build())); @@ -723,20 +716,28 @@ public class MapForProto2Test extends TestCase { assertEquals(444, message.getInt32ToMessageField().get(333).getValue()); // Test addRepeatedField - builder.addRepeatedField(f("int32_to_int32_field"), - newMapEntry(builder, "int32_to_int32_field", 55, 66)); - builder.addRepeatedField(f("int32_to_message_field"), - newMapEntry(builder, "int32_to_message_field", 555, + builder.addRepeatedField( + f("int32_to_int32_field"), newMapEntry(builder, "int32_to_int32_field", 55, 66)); + builder.addRepeatedField( + f("int32_to_message_field"), + newMapEntry( + builder, + "int32_to_message_field", + 555, MessageValue.newBuilder().setValue(666).build())); message = builder.build(); assertEquals(66, message.getInt32ToInt32Field().get(55).intValue()); assertEquals(666, message.getInt32ToMessageField().get(555).getValue()); // Test addRepeatedField (overriding existing values) - builder.addRepeatedField(f("int32_to_int32_field"), - newMapEntry(builder, "int32_to_int32_field", 55, 55)); - builder.addRepeatedField(f("int32_to_message_field"), - newMapEntry(builder, "int32_to_message_field", 555, + builder.addRepeatedField( + f("int32_to_int32_field"), newMapEntry(builder, "int32_to_int32_field", 55, 55)); + builder.addRepeatedField( + f("int32_to_message_field"), + newMapEntry( + builder, + "int32_to_message_field", + 555, MessageValue.newBuilder().setValue(555).build())); message = builder.build(); assertEquals(55, message.getInt32ToInt32Field().get(55).intValue()); @@ -765,7 +766,7 @@ public class MapForProto2Test extends TestCase { setMapValuesUsingAccessors(builder); TestMap message = builder.build(); - String textData = TextFormat.printToString(message); + String textData = TextFormat.printer().printToString(message); builder = TestMap.newBuilder(); TextFormat.merge(textData, builder); @@ -779,10 +780,9 @@ public class MapForProto2Test extends TestCase { setMapValuesUsingAccessors(builder); TestMap message = builder.build(); - Message dynamicDefaultInstance = - DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); - Message dynamicMessage = dynamicDefaultInstance - .newBuilderForType().mergeFrom(message.toByteString()).build(); + Message dynamicDefaultInstance = DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); + Message dynamicMessage = + dynamicDefaultInstance.newBuilderForType().mergeFrom(message.toByteString()).build(); assertEquals(message, dynamicMessage); assertEquals(message.hashCode(), dynamicMessage.hashCode()); @@ -793,8 +793,7 @@ public class MapForProto2Test extends TestCase { public void testDynamicMessageUnsetKeyAndValue() throws Exception { FieldDescriptor field = f("int32_to_int32_field"); - Message dynamicDefaultInstance = - DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); + Message dynamicDefaultInstance = DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); Message.Builder builder = dynamicDefaultInstance.newBuilderForType(); // Add an entry without key and value. builder.addRepeatedField(field, builder.newBuilderForField(field).build()); @@ -811,8 +810,7 @@ public class MapForProto2Test extends TestCase { // of map entries when comparing/hashing map fields. // We use DynamicMessage to test reflection based equals()/hashCode(). - Message dynamicDefaultInstance = - DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); + Message dynamicDefaultInstance = DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); FieldDescriptor field = f("int32_to_int32_field"); Message.Builder b1 = dynamicDefaultInstance.newBuilderForType(); @@ -839,9 +837,8 @@ public class MapForProto2Test extends TestCase { } public void testUnknownEnumValues() throws Exception { - TestUnknownEnumValue.Builder builder = TestUnknownEnumValue.newBuilder() - .putInt32ToInt32Field(1, 1) - .putInt32ToInt32Field(2, 54321); + TestUnknownEnumValue.Builder builder = + TestUnknownEnumValue.newBuilder().putInt32ToInt32Field(1, 1).putInt32ToInt32Field(2, 54321); ByteString data = builder.build().toByteString(); TestMap message = TestMap.parseFrom(data); @@ -853,8 +850,7 @@ public class MapForProto2Test extends TestCase { assertFalse(message.getUnknownFields().asMap().isEmpty()); // Serializing and parsing should preserve the unknown entry. data = message.toByteString(); - TestUnknownEnumValue messageWithUnknownEnums = - TestUnknownEnumValue.parseFrom(data); + TestUnknownEnumValue messageWithUnknownEnums = TestUnknownEnumValue.parseFrom(data); assertEquals(2, messageWithUnknownEnums.getInt32ToInt32Field().size()); assertEquals(1, messageWithUnknownEnums.getInt32ToInt32Field().get(1).intValue()); assertEquals(54321, messageWithUnknownEnums.getInt32ToInt32Field().get(2).intValue()); @@ -887,7 +883,8 @@ public class MapForProto2Test extends TestCase { setMapValuesUsingAccessors(builder); TestMap message = builder.build(); - assertEquals(Arrays.asList("1", "2", "3"), + assertEquals( + Arrays.asList("1", "2", "3"), new ArrayList(message.getStringToInt32Field().keySet())); } @@ -979,7 +976,8 @@ public class MapForProto2Test extends TestCase { assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrDefault(1, null)); assertNull(testMapOrBuilder.getInt32ToEnumFieldOrDefault(-1, null)); - assertEquals(MessageValue.newBuilder().setValue(11).build(), + assertEquals( + MessageValue.newBuilder().setValue(11).build(), testMapOrBuilder.getInt32ToMessageFieldOrDefault(1, null)); assertNull(testMapOrBuilder.getInt32ToMessageFieldOrDefault(-1, null)); @@ -1037,7 +1035,8 @@ public class MapForProto2Test extends TestCase { // expected } - assertEquals(MessageValue.newBuilder().setValue(11).build(), + assertEquals( + MessageValue.newBuilder().setValue(11).build(), testMapOrBuilder.getInt32ToMessageFieldOrThrow(1)); try { testMapOrBuilder.getInt32ToMessageFieldOrThrow(-1); @@ -1146,15 +1145,20 @@ public class MapForProto2Test extends TestCase { // Regression test for b/20494788 public void testMapInitializationOrder() throws Exception { - assertEquals("RedactAllTypes", map_test.RedactAllTypes - .getDefaultInstance().getDescriptorForType().getName()); + assertEquals( + "RedactAllTypes", + map_test.RedactAllTypes.getDefaultInstance() + .getDescriptorForType() + .getName()); map_test.Message1.Builder builder = map_test.Message1.newBuilder(); builder.putMapField("key", true); map_test.Message1 message = builder.build(); - Message mapEntry = (Message) message.getRepeatedField( - message.getDescriptorForType().findFieldByName("map_field"), 0); + Message mapEntry = + (Message) + message.getRepeatedField( + message.getDescriptorForType().findFieldByName("map_field"), 0); assertEquals(2, mapEntry.getAllFields().size()); } @@ -1163,35 +1167,14 @@ public class MapForProto2Test extends TestCase { ReservedAsMapFieldWithEnumValue.newBuilder().build(); } - private static Map newMap(K key1, V value1) { - Map map = new HashMap(); - map.put(key1, value1); - return map; - } - - private static Map newMap(K key1, V value1, K key2, V value2) { - Map map = new HashMap(); - map.put(key1, value1); - map.put(key2, value2); - return map; - } - public void testGetMap() { TestMap.Builder builder = TestMap.newBuilder(); setMapValuesUsingAccessors(builder); assertMapValuesSet(builder); TestMap message = builder.build(); - assertEquals( - message.getStringToInt32Field(), - message.getStringToInt32FieldMap()); - assertEquals( - message.getInt32ToBytesField(), - message.getInt32ToBytesFieldMap()); - assertEquals( - message.getInt32ToEnumField(), - message.getInt32ToEnumFieldMap()); - assertEquals( - message.getInt32ToMessageField(), - message.getInt32ToMessageFieldMap()); + assertEquals(message.getStringToInt32Field(), message.getStringToInt32FieldMap()); + assertEquals(message.getInt32ToBytesField(), message.getInt32ToBytesFieldMap()); + assertEquals(message.getInt32ToEnumField(), message.getInt32ToEnumFieldMap()); + assertEquals(message.getInt32ToMessageField(), message.getInt32ToMessageFieldMap()); } } diff --git a/java/core/src/test/java/com/google/protobuf/MapLiteTest.java b/java/core/src/test/java/com/google/protobuf/MapLiteTest.java new file mode 100755 index 0000000000..d18fd13e0a --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/MapLiteTest.java @@ -0,0 +1,894 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertArrayEquals; + +import map_lite_test.MapTestProto.BizarroTestMap; +import map_lite_test.MapTestProto.TestMap; +import map_lite_test.MapTestProto.TestMap.MessageValue; +import map_lite_test.MapTestProto.TestMapOrBuilder; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import junit.framework.TestCase; + +/** Unit tests for map fields. */ +public final class MapLiteTest extends TestCase { + + private void setMapValues(TestMap.Builder builder) { + builder + .putInt32ToInt32Field(1, 11) + .putInt32ToInt32Field(2, 22) + .putInt32ToInt32Field(3, 33) + .putInt32ToStringField(1, "11") + .putInt32ToStringField(2, "22") + .putInt32ToStringField(3, "33") + .putInt32ToBytesField(1, TestUtil.toBytes("11")) + .putInt32ToBytesField(2, TestUtil.toBytes("22")) + .putInt32ToBytesField(3, TestUtil.toBytes("33")) + .putInt32ToEnumField(1, TestMap.EnumValue.FOO) + .putInt32ToEnumField(2, TestMap.EnumValue.BAR) + .putInt32ToEnumField(3, TestMap.EnumValue.BAZ) + .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(11).build()) + .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(22).build()) + .putInt32ToMessageField(3, MessageValue.newBuilder().setValue(33).build()) + .putStringToInt32Field("1", 11) + .putStringToInt32Field("2", 22) + .putStringToInt32Field("3", 33); + } + + public void testSetMapValues() { + TestMap.Builder usingMutableMapBuilder = TestMap.newBuilder(); + setMapValues(usingMutableMapBuilder); + TestMap usingMutableMap = usingMutableMapBuilder.build(); + assertMapValuesSet(usingMutableMap); + + TestMap.Builder usingAccessorsBuilder = TestMap.newBuilder(); + setMapValues(usingAccessorsBuilder); + TestMap usingAccessors = usingAccessorsBuilder.build(); + assertMapValuesSet(usingAccessors); + assertEquals(usingAccessors, usingMutableMap); + } + + private void copyMapValues(TestMap source, TestMap.Builder destination) { + destination + .putAllInt32ToInt32Field(source.getInt32ToInt32Field()) + .putAllInt32ToStringField(source.getInt32ToStringField()) + .putAllInt32ToBytesField(source.getInt32ToBytesField()) + .putAllInt32ToEnumField(source.getInt32ToEnumField()) + .putAllInt32ToMessageField(source.getInt32ToMessageField()) + .putAllStringToInt32Field(source.getStringToInt32Field()); + } + + private void assertMapValuesSet(TestMap message) { + assertEquals(3, message.getInt32ToInt32Field().size()); + assertEquals(11, message.getInt32ToInt32Field().get(1).intValue()); + assertEquals(22, message.getInt32ToInt32Field().get(2).intValue()); + assertEquals(33, message.getInt32ToInt32Field().get(3).intValue()); + + assertEquals(3, message.getInt32ToStringField().size()); + assertEquals("11", message.getInt32ToStringField().get(1)); + assertEquals("22", message.getInt32ToStringField().get(2)); + assertEquals("33", message.getInt32ToStringField().get(3)); + + assertEquals(3, message.getInt32ToBytesField().size()); + assertEquals(TestUtil.toBytes("11"), message.getInt32ToBytesField().get(1)); + assertEquals(TestUtil.toBytes("22"), message.getInt32ToBytesField().get(2)); + assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3)); + + assertEquals(3, message.getInt32ToEnumField().size()); + assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(1)); + assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(2)); + assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3)); + + assertEquals(3, message.getInt32ToMessageField().size()); + assertEquals(11, message.getInt32ToMessageField().get(1).getValue()); + assertEquals(22, message.getInt32ToMessageField().get(2).getValue()); + assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); + + assertEquals(3, message.getStringToInt32Field().size()); + assertEquals(11, message.getStringToInt32Field().get("1").intValue()); + assertEquals(22, message.getStringToInt32Field().get("2").intValue()); + assertEquals(33, message.getStringToInt32Field().get("3").intValue()); + } + + private void updateMapValues(TestMap.Builder builder) { + builder + .putInt32ToInt32Field(1, 111) + .removeInt32ToInt32Field(2) + .putInt32ToInt32Field(4, 44) + .putInt32ToStringField(1, "111") + .removeInt32ToStringField(2) + .putInt32ToStringField(4, "44") + .putInt32ToBytesField(1, TestUtil.toBytes("111")) + .removeInt32ToBytesField(2) + .putInt32ToBytesField(4, TestUtil.toBytes("44")) + .putInt32ToEnumField(1, TestMap.EnumValue.BAR) + .removeInt32ToEnumField(2) + .putInt32ToEnumField(4, TestMap.EnumValue.QUX) + .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(111).build()) + .removeInt32ToMessageField(2) + .putInt32ToMessageField(4, MessageValue.newBuilder().setValue(44).build()) + .putStringToInt32Field("1", 111) + .removeStringToInt32Field("2") + .putStringToInt32Field("4", 44); + } + + public void testUpdateMapValues() { + TestMap.Builder mapBuilder = TestMap.newBuilder(); + setMapValues(mapBuilder); + TestMap map = mapBuilder.build(); + assertMapValuesSet(map); + + mapBuilder = map.toBuilder(); + updateMapValues(mapBuilder); + map = mapBuilder.build(); + assertMapValuesUpdated(map); + } + + private void assertMapValuesUpdated(TestMap message) { + assertEquals(3, message.getInt32ToInt32Field().size()); + assertEquals(111, message.getInt32ToInt32Field().get(1).intValue()); + assertEquals(33, message.getInt32ToInt32Field().get(3).intValue()); + assertEquals(44, message.getInt32ToInt32Field().get(4).intValue()); + + assertEquals(3, message.getInt32ToStringField().size()); + assertEquals("111", message.getInt32ToStringField().get(1)); + assertEquals("33", message.getInt32ToStringField().get(3)); + assertEquals("44", message.getInt32ToStringField().get(4)); + + assertEquals(3, message.getInt32ToBytesField().size()); + assertEquals(TestUtil.toBytes("111"), message.getInt32ToBytesField().get(1)); + assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3)); + assertEquals(TestUtil.toBytes("44"), message.getInt32ToBytesField().get(4)); + + assertEquals(3, message.getInt32ToEnumField().size()); + assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1)); + assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3)); + assertEquals(TestMap.EnumValue.QUX, message.getInt32ToEnumField().get(4)); + + assertEquals(3, message.getInt32ToMessageField().size()); + assertEquals(111, message.getInt32ToMessageField().get(1).getValue()); + assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); + assertEquals(44, message.getInt32ToMessageField().get(4).getValue()); + + assertEquals(3, message.getStringToInt32Field().size()); + assertEquals(111, message.getStringToInt32Field().get("1").intValue()); + assertEquals(33, message.getStringToInt32Field().get("3").intValue()); + assertEquals(44, message.getStringToInt32Field().get("4").intValue()); + } + + private void assertMapValuesCleared(TestMapOrBuilder testMapOrBuilder) { + assertEquals(0, testMapOrBuilder.getInt32ToInt32Field().size()); + assertEquals(0, testMapOrBuilder.getInt32ToInt32FieldCount()); + assertEquals(0, testMapOrBuilder.getInt32ToStringField().size()); + assertEquals(0, testMapOrBuilder.getInt32ToStringFieldCount()); + assertEquals(0, testMapOrBuilder.getInt32ToBytesField().size()); + assertEquals(0, testMapOrBuilder.getInt32ToBytesFieldCount()); + assertEquals(0, testMapOrBuilder.getInt32ToEnumField().size()); + assertEquals(0, testMapOrBuilder.getInt32ToEnumFieldCount()); + assertEquals(0, testMapOrBuilder.getInt32ToMessageField().size()); + assertEquals(0, testMapOrBuilder.getInt32ToMessageFieldCount()); + assertEquals(0, testMapOrBuilder.getStringToInt32Field().size()); + assertEquals(0, testMapOrBuilder.getStringToInt32FieldCount()); + } + + public void testSanityCopyOnWrite() throws InvalidProtocolBufferException { + // Since builders are implemented as a thin wrapper around a message + // instance, we attempt to verify that we can't cause the builder to modify + // a produced message. + + TestMap.Builder builder = TestMap.newBuilder(); + TestMap message = builder.build(); + builder.putInt32ToInt32Field(1, 2); + assertTrue(message.getInt32ToInt32Field().isEmpty()); + assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); + message = builder.build(); + builder.putInt32ToInt32Field(2, 3); + assertEquals(newMap(1, 2), message.getInt32ToInt32Field()); + assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); + } + + public void testGetMapIsImmutable() { + TestMap.Builder builder = TestMap.newBuilder(); + assertMapsAreImmutable(builder); + assertMapsAreImmutable(builder.build()); + + setMapValues(builder); + assertMapsAreImmutable(builder); + assertMapsAreImmutable(builder.build()); + } + + private void assertMapsAreImmutable(TestMapOrBuilder testMapOrBuilder) { + assertImmutable(testMapOrBuilder.getInt32ToInt32Field(), 1, 2); + assertImmutable(testMapOrBuilder.getInt32ToStringField(), 1, "2"); + assertImmutable(testMapOrBuilder.getInt32ToBytesField(), 1, TestUtil.toBytes("2")); + assertImmutable(testMapOrBuilder.getInt32ToEnumField(), 1, TestMap.EnumValue.FOO); + assertImmutable( + testMapOrBuilder.getInt32ToMessageField(), 1, MessageValue.getDefaultInstance()); + assertImmutable(testMapOrBuilder.getStringToInt32Field(), "1", 2); + } + + private void assertImmutable(Map map, K key, V value) { + try { + map.put(key, value); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + if (!map.isEmpty()) { + try { + map.entrySet().remove(map.entrySet().iterator().next()); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + } + } + + public void testMapFieldClear() { + TestMap.Builder builder = TestMap.newBuilder().putInt32ToInt32Field(1, 2); + builder.clearInt32ToInt32Field(); + assertEquals(0, builder.getInt32ToInt32FieldCount()); + } + + public void testMutableMapLifecycle() { + TestMap.Builder builder = TestMap.newBuilder().putInt32ToInt32Field(1, 2); + assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); + assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); + builder.putInt32ToInt32Field(2, 3); + assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); + + builder.putInt32ToEnumField(1, TestMap.EnumValue.BAR); + assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnumField()); + assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.getInt32ToEnumField()); + builder.putInt32ToEnumField(2, TestMap.EnumValue.FOO); + assertEquals( + newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), builder.getInt32ToEnumField()); + + builder.putInt32ToStringField(1, "1"); + assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); + assertEquals(newMap(1, "1"), builder.getInt32ToStringField()); + builder.putInt32ToStringField(2, "2"); + assertEquals(newMap(1, "1", 2, "2"), builder.getInt32ToStringField()); + + builder.putInt32ToMessageField(1, TestMap.MessageValue.getDefaultInstance()); + assertEquals( + newMap(1, TestMap.MessageValue.getDefaultInstance()), + builder.build().getInt32ToMessageField()); + assertEquals( + newMap(1, TestMap.MessageValue.getDefaultInstance()), builder.getInt32ToMessageField()); + builder.putInt32ToMessageField(2, TestMap.MessageValue.getDefaultInstance()); + assertEquals( + newMap( + 1, + TestMap.MessageValue.getDefaultInstance(), + 2, + TestMap.MessageValue.getDefaultInstance()), + builder.getInt32ToMessageField()); + } + + public void testGettersAndSetters() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + TestMap message = builder.build(); + assertMapValuesCleared(message); + + builder = message.toBuilder(); + setMapValues(builder); + message = builder.build(); + assertMapValuesSet(message); + + builder = message.toBuilder(); + updateMapValues(builder); + message = builder.build(); + assertMapValuesUpdated(message); + + builder = message.toBuilder(); + builder.clear(); + assertMapValuesCleared(builder); + message = builder.build(); + assertMapValuesCleared(message); + } + + public void testPutAll() throws Exception { + TestMap.Builder sourceBuilder = TestMap.newBuilder(); + setMapValues(sourceBuilder); + TestMap source = sourceBuilder.build(); + assertMapValuesSet(source); + + TestMap.Builder destination = TestMap.newBuilder(); + copyMapValues(source, destination); + assertMapValuesSet(destination.build()); + } + + public void testPutAllForUnknownEnumValues() throws Exception { + TestMap.Builder sourceBuilder = + TestMap.newBuilder() + .putInt32ToEnumFieldValue(0, 0) + .putInt32ToEnumFieldValue(1, 1) + .putAllInt32ToEnumFieldValue(newMap(2, 1000)); // unknown value. + TestMap source = sourceBuilder.build(); + + TestMap.Builder destinationBuilder = TestMap.newBuilder(); + destinationBuilder.putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldValue()); + TestMap destination = destinationBuilder.build(); + + assertEquals(0, destination.getInt32ToEnumFieldValue().get(0).intValue()); + assertEquals(1, destination.getInt32ToEnumFieldValue().get(1).intValue()); + assertEquals(1000, destination.getInt32ToEnumFieldValue().get(2).intValue()); + assertEquals(3, destination.getInt32ToEnumFieldCount()); + } + + public void testPutForUnknownEnumValues() throws Exception { + TestMap.Builder builder = + TestMap.newBuilder() + .putInt32ToEnumFieldValue(0, 0) + .putInt32ToEnumFieldValue(1, 1) + .putInt32ToEnumFieldValue(2, 1000); // unknown value. + TestMap message = builder.build(); + + assertEquals(0, message.getInt32ToEnumFieldValueOrThrow(0)); + assertEquals(1, message.getInt32ToEnumFieldValueOrThrow(1)); + assertEquals(1000, message.getInt32ToEnumFieldValueOrThrow(2)); + assertEquals(3, message.getInt32ToEnumFieldCount()); + } + + public void testPutChecksNullKeysAndValues() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + + try { + builder.putInt32ToStringField(1, null); + fail(); + } catch (NullPointerException e) { + // expected. + } + + try { + builder.putInt32ToBytesField(1, null); + fail(); + } catch (NullPointerException e) { + // expected. + } + + try { + builder.putInt32ToEnumField(1, null); + fail(); + } catch (NullPointerException e) { + // expected. + } + + try { + builder.putInt32ToMessageField(1, null); + fail(); + } catch (NullPointerException e) { + // expected. + } + + try { + builder.putStringToInt32Field(null, 1); + fail(); + } catch (NullPointerException e) { + // expected. + } + } + + public void testSerializeAndParse() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + TestMap message = builder.build(); + assertEquals(message.getSerializedSize(), message.toByteString().size()); + message = TestMap.parser().parseFrom(message.toByteString()); + assertMapValuesSet(message); + + builder = message.toBuilder(); + updateMapValues(builder); + message = builder.build(); + assertEquals(message.getSerializedSize(), message.toByteString().size()); + message = TestMap.parser().parseFrom(message.toByteString()); + assertMapValuesUpdated(message); + + builder = message.toBuilder(); + builder.clear(); + message = builder.build(); + assertEquals(message.getSerializedSize(), message.toByteString().size()); + message = TestMap.parser().parseFrom(message.toByteString()); + assertMapValuesCleared(message); + } + + private TestMap tryParseTestMap(BizarroTestMap bizarroMap) throws IOException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStream); + bizarroMap.writeTo(output); + output.flush(); + return TestMap.parser().parseFrom(ByteString.copyFrom(byteArrayOutputStream.toByteArray())); + } + + public void testParseError() throws Exception { + ByteString bytes = TestUtil.toBytes("SOME BYTES"); + String stringKey = "a string key"; + + TestMap map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToInt32Field(5, bytes).build()); + assertEquals(0, map.getInt32ToInt32FieldOrDefault(5, -1)); + + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToStringField(stringKey, 5).build()); + assertEquals("", map.getInt32ToStringFieldOrDefault(0, null)); + + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToBytesField(stringKey, 5).build()); + assertEquals(map.getInt32ToBytesFieldOrDefault(0, null), ByteString.EMPTY); + + map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToEnumField(stringKey, bytes).build()); + assertEquals(TestMap.EnumValue.FOO, map.getInt32ToEnumFieldOrDefault(0, null)); + + try { + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToMessageField(stringKey, bytes).build()); + fail(); + } catch (InvalidProtocolBufferException expected) { + assertTrue(expected.getUnfinishedMessage() instanceof TestMap); + map = (TestMap) expected.getUnfinishedMessage(); + assertTrue(map.getInt32ToMessageField().isEmpty()); + } + + map = + tryParseTestMap( + BizarroTestMap.newBuilder().putStringToInt32Field(stringKey, bytes).build()); + assertEquals(0, map.getStringToInt32FieldOrDefault(stringKey, -1)); + } + + public void testMergeFrom() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + TestMap message = builder.build(); + + TestMap.Builder other = TestMap.newBuilder(); + other.mergeFrom(message); + assertMapValuesSet(other.build()); + } + + public void testEqualsAndHashCode() throws Exception { + // Test that generated equals() and hashCode() will disregard the order + // of map entries when comparing/hashing map fields. + + // We can't control the order of elements in a HashMap. The best we can do + // here is to add elements in different order. + TestMap.Builder b1 = + TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToInt32Field(5, 6); + TestMap m1 = b1.build(); + + TestMap.Builder b2 = + TestMap.newBuilder() + .putInt32ToInt32Field(5, 6) + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4); + TestMap m2 = b2.build(); + + assertEquals(m1, m2); + assertEquals(m1.hashCode(), m2.hashCode()); + + // Make sure we did compare map fields. + b2.putInt32ToInt32Field(1, 0); + m2 = b2.build(); + assertFalse(m1.equals(m2)); + // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed + // to be different. + + // Regression test for b/18549190: if a map is a subset of the other map, + // equals() should return false. + b2.removeInt32ToInt32Field(1); + m2 = b2.build(); + assertFalse(m1.equals(m2)); + assertFalse(m2.equals(m1)); + } + + public void testUnknownEnumValues() throws Exception { + TestMap.Builder builder = + TestMap.newBuilder() + .putInt32ToEnumFieldValue(0, 0) + .putInt32ToEnumFieldValue(1, 1) + .putInt32ToEnumFieldValue(2, 1000); // unknown value. + TestMap message = builder.build(); + + assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(0)); + assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1)); + assertEquals(TestMap.EnumValue.UNRECOGNIZED, message.getInt32ToEnumField().get(2)); + + builder.putAllInt32ToEnumFieldValue(newMap(2, 1000)); // unknown value. + message = builder.build(); + assertEquals(TestMap.EnumValue.UNRECOGNIZED, message.getInt32ToEnumField().get(2)); + + // Unknown enum values should be preserved after: + // 1. Serialization and parsing. + // 2. toBuild(). + // 3. mergeFrom(). + message = TestMap.parseFrom(message.toByteString()); + assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); + builder = message.toBuilder(); + assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); + builder = TestMap.newBuilder().mergeFrom(message); + assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); + + // hashCode()/equals() should take unknown enum values into account. + builder.putAllInt32ToEnumFieldValue(newMap(2, 1001)); + TestMap message2 = builder.build(); + assertFalse(message.hashCode() == message2.hashCode()); + assertFalse(message.equals(message2)); + // Unknown values will be converted to UNRECOGNIZED so the resulted enum map + // should be the same. + assertEquals(message2.getInt32ToEnumField(), message.getInt32ToEnumField()); + } + + public void testIterationOrder() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + TestMap message = builder.build(); + + assertEquals( + Arrays.asList("1", "2", "3"), new ArrayList<>(message.getStringToInt32Field().keySet())); + } + + public void testGetMap() { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + TestMap message = builder.build(); + assertEquals(message.getStringToInt32Field(), message.getStringToInt32FieldMap()); + assertEquals(message.getInt32ToBytesField(), message.getInt32ToBytesFieldMap()); + assertEquals(message.getInt32ToEnumField(), message.getInt32ToEnumFieldMap()); + assertEquals(message.getInt32ToEnumFieldValue(), message.getInt32ToEnumFieldValueMap()); + assertEquals(message.getInt32ToMessageField(), message.getInt32ToMessageFieldMap()); + } + + public void testContains() { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + assertMapContainsSetValues(builder); + assertMapContainsSetValues(builder.build()); + } + + private void assertMapContainsSetValues(TestMapOrBuilder testMapOrBuilder) { + assertTrue(testMapOrBuilder.containsInt32ToInt32Field(1)); + assertTrue(testMapOrBuilder.containsInt32ToInt32Field(2)); + assertTrue(testMapOrBuilder.containsInt32ToInt32Field(3)); + assertFalse(testMapOrBuilder.containsInt32ToInt32Field(-1)); + + assertTrue(testMapOrBuilder.containsInt32ToStringField(1)); + assertTrue(testMapOrBuilder.containsInt32ToStringField(2)); + assertTrue(testMapOrBuilder.containsInt32ToStringField(3)); + assertFalse(testMapOrBuilder.containsInt32ToStringField(-1)); + + assertTrue(testMapOrBuilder.containsInt32ToBytesField(1)); + assertTrue(testMapOrBuilder.containsInt32ToBytesField(2)); + assertTrue(testMapOrBuilder.containsInt32ToBytesField(3)); + assertFalse(testMapOrBuilder.containsInt32ToBytesField(-1)); + + assertTrue(testMapOrBuilder.containsInt32ToEnumField(1)); + assertTrue(testMapOrBuilder.containsInt32ToEnumField(2)); + assertTrue(testMapOrBuilder.containsInt32ToEnumField(3)); + assertFalse(testMapOrBuilder.containsInt32ToEnumField(-1)); + + assertTrue(testMapOrBuilder.containsInt32ToMessageField(1)); + assertTrue(testMapOrBuilder.containsInt32ToMessageField(2)); + assertTrue(testMapOrBuilder.containsInt32ToMessageField(3)); + assertFalse(testMapOrBuilder.containsInt32ToMessageField(-1)); + + assertTrue(testMapOrBuilder.containsStringToInt32Field("1")); + assertTrue(testMapOrBuilder.containsStringToInt32Field("2")); + assertTrue(testMapOrBuilder.containsStringToInt32Field("3")); + assertFalse(testMapOrBuilder.containsStringToInt32Field("-1")); + } + + public void testCount() { + TestMap.Builder builder = TestMap.newBuilder(); + assertMapCounts(0, builder); + + setMapValues(builder); + assertMapCounts(3, builder); + + TestMap message = builder.build(); + assertMapCounts(3, message); + + builder = message.toBuilder().putInt32ToInt32Field(4, 44); + assertEquals(4, builder.getInt32ToInt32FieldCount()); + assertEquals(4, builder.build().getInt32ToInt32FieldCount()); + + // already present - should be unchanged + builder.putInt32ToInt32Field(4, 44); + assertEquals(4, builder.getInt32ToInt32FieldCount()); + } + + private void assertMapCounts(int expectedCount, TestMapOrBuilder testMapOrBuilder) { + assertEquals(expectedCount, testMapOrBuilder.getInt32ToInt32FieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getInt32ToStringFieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getInt32ToBytesFieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getInt32ToEnumFieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getInt32ToMessageFieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getStringToInt32FieldCount()); + } + + public void testGetOrDefault() { + TestMap.Builder builder = TestMap.newBuilder(); + assertMapCounts(0, builder); + setMapValues(builder); + doTestGetOrDefault(builder); + doTestGetOrDefault(builder.build()); + } + + public void doTestGetOrDefault(TestMapOrBuilder testMapOrBuilder) { + assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(1, -11)); + assertEquals(-11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(-1, -11)); + + assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrDefault(1, "-11")); + assertNull("-11", testMapOrBuilder.getInt32ToStringFieldOrDefault(-1, null)); + + assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOrDefault(1, null)); + assertNull(testMapOrBuilder.getInt32ToBytesFieldOrDefault(-1, null)); + + assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrDefault(1, null)); + assertNull(testMapOrBuilder.getInt32ToEnumFieldOrDefault(-1, null)); + + assertEquals( + TestMap.EnumValue.BAR.getNumber(), + testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(2, -1)); + assertEquals(-1, testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(-1000, -1)); + + assertEquals( + MessageValue.newBuilder().setValue(11).build(), + testMapOrBuilder.getInt32ToMessageFieldOrDefault(1, null)); + assertNull(testMapOrBuilder.getInt32ToMessageFieldOrDefault(-1, null)); + + assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrDefault("1", -11)); + assertEquals(-11, testMapOrBuilder.getStringToInt32FieldOrDefault("-1", -11)); + + try { + testMapOrBuilder.getStringToInt32FieldOrDefault(null, -11); + fail(); + } catch (NullPointerException e) { + // expected + } + } + + public void testGetOrThrow() { + TestMap.Builder builder = TestMap.newBuilder(); + assertMapCounts(0, builder); + setMapValues(builder); + doTestGetOrDefault(builder); + doTestGetOrDefault(builder.build()); + } + + public void doTestGetOrThrow(TestMapOrBuilder testMapOrBuilder) { + assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrThrow(1)); + try { + testMapOrBuilder.getInt32ToInt32FieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrThrow(1)); + + try { + testMapOrBuilder.getInt32ToStringFieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOrThrow(1)); + + try { + testMapOrBuilder.getInt32ToBytesFieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrThrow(1)); + try { + testMapOrBuilder.getInt32ToEnumFieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals( + TestMap.EnumValue.BAR.getNumber(), testMapOrBuilder.getInt32ToEnumFieldValueOrThrow(2)); + try { + testMapOrBuilder.getInt32ToEnumFieldValueOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals( + MessageValue.newBuilder().setValue(11).build(), + testMapOrBuilder.getInt32ToMessageFieldOrThrow(1)); + try { + testMapOrBuilder.getInt32ToMessageFieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrThrow("1")); + try { + testMapOrBuilder.getStringToInt32FieldOrThrow("-1"); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + try { + testMapOrBuilder.getStringToInt32FieldOrThrow(null); + fail(); + } catch (NullPointerException e) { + // expected + } + } + + public void testPut() { + TestMap.Builder builder = TestMap.newBuilder(); + builder.putInt32ToInt32Field(1, 11); + assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); + + builder.putInt32ToStringField(1, "a"); + assertEquals("a", builder.getInt32ToStringFieldOrThrow(1)); + try { + builder.putInt32ToStringField(1, null); + fail(); + } catch (NullPointerException e) { + // expected + } + + builder.putInt32ToBytesField(1, TestUtil.toBytes("11")); + assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1)); + try { + builder.putInt32ToBytesField(1, null); + fail(); + } catch (NullPointerException e) { + // expected + } + + builder.putInt32ToEnumField(1, TestMap.EnumValue.FOO); + assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1)); + try { + builder.putInt32ToEnumField(1, null); + fail(); + } catch (NullPointerException e) { + // expected + } + + builder.putStringToInt32Field("a", 1); + assertEquals(1, builder.getStringToInt32FieldOrThrow("a")); + try { + builder.putStringToInt32Field(null, -1); + } catch (NullPointerException e) { + // expected + } + } + + public void testRemove() { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); + for (int times = 0; times < 2; times++) { + builder.removeInt32ToInt32Field(1); + assertEquals(-1, builder.getInt32ToInt32FieldOrDefault(1, -1)); + } + + assertEquals("11", builder.getInt32ToStringFieldOrThrow(1)); + for (int times = 0; times < 2; times++) { + builder.removeInt32ToStringField(1); + assertNull(builder.getInt32ToStringFieldOrDefault(1, null)); + } + + assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1)); + for (int times = 0; times < 2; times++) { + builder.removeInt32ToBytesField(1); + assertNull(builder.getInt32ToBytesFieldOrDefault(1, null)); + } + + assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1)); + for (int times = 0; times < 2; times++) { + builder.removeInt32ToEnumField(1); + assertNull(builder.getInt32ToEnumFieldOrDefault(1, null)); + } + + assertEquals(11, builder.getStringToInt32FieldOrThrow("1")); + for (int times = 0; times < 2; times++) { + builder.removeStringToInt32Field("1"); + assertEquals(-1, builder.getStringToInt32FieldOrDefault("1", -1)); + } + + try { + builder.removeStringToInt32Field(null); + fail(); + } catch (NullPointerException e) { + // expected + } + } + + private static Map newMap(K key1, V value1) { + Map map = new HashMap<>(); + map.put(key1, value1); + return map; + } + + private static Map newMap(K key1, V value1, K key2, V value2) { + Map map = new HashMap<>(); + map.put(key1, value1); + map.put(key2, value2); + return map; + } + + public void testMap_withNulls() { + TestMap.Builder builder = TestMap.newBuilder(); + + try { + builder.putStringToInt32Field(null, 3); + fail(); + } catch (NullPointerException expected) { + } + + try { + builder.putAllStringToInt32Field(newMap(null, 3, "hi", 4)); + fail(); + } catch (NullPointerException expected) { + } + + try { + builder.putInt32ToMessageField(3, null); + fail(); + } catch (NullPointerException expected) { + } + + try { + builder.putAllInt32ToMessageField( + MapLiteTest.newMap(4, null, 5, null)); + fail(); + } catch (NullPointerException expected) { + } + + try { + builder.putAllInt32ToMessageField(null); + fail(); + } catch (NullPointerException expected) { + } + + assertArrayEquals(new byte[0], builder.build().toByteArray()); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/MapTest.java b/java/core/src/test/java/com/google/protobuf/MapTest.java index 58efce92c0..9f1ebaed69 100644 --- a/java/core/src/test/java/com/google/protobuf/MapTest.java +++ b/java/core/src/test/java/com/google/protobuf/MapTest.java @@ -31,6 +31,7 @@ package com.google.protobuf; import static org.junit.Assert.assertArrayEquals; + import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.EnumDescriptor; import com.google.protobuf.Descriptors.EnumValueDescriptor; @@ -51,9 +52,7 @@ import java.util.List; import java.util.Map; import junit.framework.TestCase; -/** - * Unit tests for map fields. - */ +/** Unit tests for map fields. */ public class MapTest extends TestCase { private void setMapValuesUsingMutableMap(TestMap.Builder builder) { @@ -90,23 +89,18 @@ public class MapTest extends TestCase { .putInt32ToInt32Field(1, 11) .putInt32ToInt32Field(2, 22) .putInt32ToInt32Field(3, 33) - .putInt32ToStringField(1, "11") .putInt32ToStringField(2, "22") .putInt32ToStringField(3, "33") - .putInt32ToBytesField(1, TestUtil.toBytes("11")) .putInt32ToBytesField(2, TestUtil.toBytes("22")) .putInt32ToBytesField(3, TestUtil.toBytes("33")) - .putInt32ToEnumField(1, TestMap.EnumValue.FOO) .putInt32ToEnumField(2, TestMap.EnumValue.BAR) .putInt32ToEnumField(3, TestMap.EnumValue.BAZ) - .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(11).build()) .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(22).build()) .putInt32ToMessageField(3, MessageValue.newBuilder().setValue(33).build()) - .putStringToInt32Field("1", 11) .putStringToInt32Field("2", 22) .putStringToInt32Field("3", 33); @@ -201,23 +195,18 @@ public class MapTest extends TestCase { .putInt32ToInt32Field(1, 111) .removeInt32ToInt32Field(2) .putInt32ToInt32Field(4, 44) - .putInt32ToStringField(1, "111") .removeInt32ToStringField(2) .putInt32ToStringField(4, "44") - .putInt32ToBytesField(1, TestUtil.toBytes("111")) .removeInt32ToBytesField(2) .putInt32ToBytesField(4, TestUtil.toBytes("44")) - .putInt32ToEnumField(1, TestMap.EnumValue.BAR) .removeInt32ToEnumField(2) .putInt32ToEnumField(4, TestMap.EnumValue.QUX) - .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(111).build()) .removeInt32ToMessageField(2) .putInt32ToMessageField(4, MessageValue.newBuilder().setValue(44).build()) - .putStringToInt32Field("1", 111) .removeStringToInt32Field("2") .putStringToInt32Field("4", 44); @@ -435,7 +424,6 @@ public class MapTest extends TestCase { assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); } - public void testGettersAndSetters() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); TestMap message = builder.build(); @@ -470,16 +458,17 @@ public class MapTest extends TestCase { } public void testPutAllForUnknownEnumValues() throws Exception { - TestMap source = TestMap.newBuilder() - .putAllInt32ToEnumFieldValue(newMap( - 0, 0, - 1, 1, - 2, 1000)) // unknown value. - .build(); + TestMap source = + TestMap.newBuilder() + .putAllInt32ToEnumFieldValue( + newMap( + 0, 0, + 1, 1, + 2, 1000)) // unknown value. + .build(); - TestMap destination = TestMap.newBuilder() - .putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldValue()) - .build(); + TestMap destination = + TestMap.newBuilder().putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldValue()).build(); assertEquals(0, destination.getInt32ToEnumFieldValue().get(0).intValue()); assertEquals(1, destination.getInt32ToEnumFieldValue().get(1).intValue()); @@ -488,11 +477,12 @@ public class MapTest extends TestCase { } public void testPutForUnknownEnumValues() throws Exception { - TestMap.Builder builder = TestMap.newBuilder() - .putInt32ToEnumFieldValue(0, 0) - .putInt32ToEnumFieldValue(1, 1) - .putInt32ToEnumFieldValue(2, 1000); // unknown value. - TestMap message = builder.build(); + TestMap message = + TestMap.newBuilder() + .putInt32ToEnumFieldValue(0, 0) + .putInt32ToEnumFieldValue(1, 1) + .putInt32ToEnumFieldValue(2, 1000) // unknown value. + .build(); assertEquals(0, message.getInt32ToEnumFieldValueOrThrow(0)); assertEquals(1, message.getInt32ToEnumFieldValueOrThrow(1)); assertEquals(1000, message.getInt32ToEnumFieldValueOrThrow(2)); @@ -573,30 +563,22 @@ public class MapTest extends TestCase { ByteString bytes = TestUtil.toBytes("SOME BYTES"); String stringKey = "a string key"; - TestMap map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToInt32Field(5, bytes) - .build()); + TestMap map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToInt32Field(5, bytes).build()); assertEquals(0, map.getInt32ToInt32FieldOrDefault(5, -1)); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToStringField(stringKey, 5) - .build()); + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToStringField(stringKey, 5).build()); assertEquals("", map.getInt32ToStringFieldOrDefault(0, null)); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToBytesField(stringKey, 5) - .build()); + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToBytesField(stringKey, 5).build()); assertEquals(map.getInt32ToBytesFieldOrDefault(0, null), ByteString.EMPTY); - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToEnumField(stringKey, bytes) - .build()); + map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToEnumField(stringKey, bytes).build()); assertEquals(TestMap.EnumValue.FOO, map.getInt32ToEnumFieldOrDefault(0, null)); try { - tryParseTestMap(BizarroTestMap.newBuilder() - .putInt32ToMessageField(stringKey, bytes) - .build()); + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToMessageField(stringKey, bytes).build()); fail(); } catch (InvalidProtocolBufferException expected) { assertTrue(expected.getUnfinishedMessage() instanceof TestMap); @@ -604,9 +586,9 @@ public class MapTest extends TestCase { assertTrue(map.getInt32ToMessageField().isEmpty()); } - map = tryParseTestMap(BizarroTestMap.newBuilder() - .putStringToInt32Field(stringKey, bytes) - .build()); + map = + tryParseTestMap( + BizarroTestMap.newBuilder().putStringToInt32Field(stringKey, bytes).build()); assertEquals(0, map.getStringToInt32FieldOrDefault(stringKey, -1)); } @@ -626,16 +608,18 @@ public class MapTest extends TestCase { // We can't control the order of elements in a HashMap. The best we can do // here is to add elements in different order. - TestMap.Builder b1 = TestMap.newBuilder() - .putInt32ToInt32Field(1, 2) - .putInt32ToInt32Field(3, 4) - .putInt32ToInt32Field(5, 6); - TestMap m1 = b1.build(); - - TestMap.Builder b2 = TestMap.newBuilder() - .putInt32ToInt32Field(5, 6) - .putInt32ToInt32Field(1, 2) - .putInt32ToInt32Field(3, 4); + TestMap m1 = + TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToInt32Field(5, 6) + .build(); + + TestMap.Builder b2 = + TestMap.newBuilder() + .putInt32ToInt32Field(5, 6) + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4); TestMap m2 = b2.build(); assertEquals(m1, m2); @@ -657,8 +641,7 @@ public class MapTest extends TestCase { } public void testNestedBuilderOnChangeEventPropagation() { - TestOnChangeEventPropagation.Builder parent = - TestOnChangeEventPropagation.newBuilder(); + TestOnChangeEventPropagation.Builder parent = TestOnChangeEventPropagation.newBuilder(); parent.getOptionalMessageBuilder().putInt32ToInt32Field(1, 2); TestOnChangeEventPropagation message = parent.build(); assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); @@ -671,8 +654,8 @@ public class MapTest extends TestCase { assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); // Make another change using mergeFrom() - TestMap.Builder other = TestMap.newBuilder().putInt32ToInt32Field(1, 4); - parent.getOptionalMessageBuilder().mergeFrom(other.build()); + TestMap other = TestMap.newBuilder().putInt32ToInt32Field(1, 4).build(); + parent.getOptionalMessageBuilder().mergeFrom(other); // Should be able to observe the change. message = parent.build(); @@ -689,16 +672,14 @@ public class MapTest extends TestCase { public void testNestedBuilderOnChangeEventPropagationReflection() { FieldDescriptor intMapField = f("int32_to_int32_field"); // Create an outer message builder with nested builder. - TestOnChangeEventPropagation.Builder parentBuilder = - TestOnChangeEventPropagation.newBuilder(); + TestOnChangeEventPropagation.Builder parentBuilder = TestOnChangeEventPropagation.newBuilder(); TestMap.Builder testMapBuilder = parentBuilder.getOptionalMessageBuilder(); // Create a map entry message. TestMap.Builder entryBuilder = TestMap.newBuilder().putInt32ToInt32Field(1, 1); // Put the entry into the nested builder. - testMapBuilder.addRepeatedField( - intMapField, entryBuilder.getRepeatedField(intMapField, 0)); + testMapBuilder.addRepeatedField(intMapField, entryBuilder.getRepeatedField(intMapField, 0)); // Should be able to observe the change. TestOnChangeEventPropagation message = parentBuilder.build(); @@ -707,13 +688,11 @@ public class MapTest extends TestCase { // Change the entry value. entryBuilder.putInt32ToInt32Field(1, 4); testMapBuilder = parentBuilder.getOptionalMessageBuilder(); - testMapBuilder.setRepeatedField( - intMapField, 0, entryBuilder.getRepeatedField(intMapField, 0)); + testMapBuilder.setRepeatedField(intMapField, 0, entryBuilder.getRepeatedField(intMapField, 0)); // Should be able to observe the change. message = parentBuilder.build(); - assertEquals(4, - message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); + assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); // Clear the nested builder. testMapBuilder = parentBuilder.getOptionalMessageBuilder(); @@ -761,8 +740,7 @@ public class MapTest extends TestCase { } } - private static - Message newMapEntry(Message.Builder builder, String name, KeyType key, ValueType value) { + private static Message newMapEntry(Message.Builder builder, String name, K key, V value) { FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name); Message.Builder entryBuilder = builder.newBuilderForField(field); FieldDescriptor keyField = entryBuilder.getDescriptorForType().findFieldByName("key"); @@ -773,7 +751,7 @@ public class MapTest extends TestCase { } private static void setMapValues(Message.Builder builder, String name, Map values) { - List entryList = new ArrayList(); + List entryList = new ArrayList<>(); for (Map.Entry entry : values.entrySet()) { entryList.add(newMapEntry(builder, name, entry.getKey(), entry.getValue())); } @@ -781,10 +759,8 @@ public class MapTest extends TestCase { builder.setField(field, entryList); } - private static - Map mapForValues( - KeyType key1, ValueType value1, KeyType key2, ValueType value2) { - Map map = new HashMap(); + private static Map mapForValues(K key1, V value1, K key2, V value2) { + Map map = new HashMap<>(); map.put(key1, value1); map.put(key2, value2); return map; @@ -792,17 +768,19 @@ public class MapTest extends TestCase { public void testReflectionApi() throws Exception { // In reflection API, map fields are just repeated message fields. - TestMap.Builder builder = TestMap.newBuilder() - .putInt32ToInt32Field(1, 2) - .putInt32ToInt32Field(3, 4) - .putInt32ToMessageField(11, MessageValue.newBuilder().setValue(22).build()) - .putInt32ToMessageField(33, MessageValue.newBuilder().setValue(44).build()); + TestMap.Builder builder = + TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToMessageField(11, MessageValue.newBuilder().setValue(22).build()) + .putInt32ToMessageField(33, MessageValue.newBuilder().setValue(44).build()); TestMap message = builder.build(); // Test getField(), getRepeatedFieldCount(), getRepeatedField(). - assertHasMapValues(message, "int32_to_int32_field", - mapForValues(1, 2, 3, 4)); - assertHasMapValues(message, "int32_to_message_field", + assertHasMapValues(message, "int32_to_int32_field", mapForValues(1, 2, 3, 4)); + assertHasMapValues( + message, + "int32_to_message_field", mapForValues( 11, MessageValue.newBuilder().setValue(22).build(), 33, MessageValue.newBuilder().setValue(44).build())); @@ -815,9 +793,10 @@ public class MapTest extends TestCase { assertEquals(0, message.getInt32ToMessageField().size()); // Test setField() - setMapValues(builder, "int32_to_int32_field", - mapForValues(11, 22, 33, 44)); - setMapValues(builder, "int32_to_message_field", + setMapValues(builder, "int32_to_int32_field", mapForValues(11, 22, 33, 44)); + setMapValues( + builder, + "int32_to_message_field", mapForValues( 111, MessageValue.newBuilder().setValue(222).build(), 333, MessageValue.newBuilder().setValue(444).build())); @@ -828,20 +807,28 @@ public class MapTest extends TestCase { assertEquals(444, message.getInt32ToMessageField().get(333).getValue()); // Test addRepeatedField - builder.addRepeatedField(f("int32_to_int32_field"), - newMapEntry(builder, "int32_to_int32_field", 55, 66)); - builder.addRepeatedField(f("int32_to_message_field"), - newMapEntry(builder, "int32_to_message_field", 555, + builder.addRepeatedField( + f("int32_to_int32_field"), newMapEntry(builder, "int32_to_int32_field", 55, 66)); + builder.addRepeatedField( + f("int32_to_message_field"), + newMapEntry( + builder, + "int32_to_message_field", + 555, MessageValue.newBuilder().setValue(666).build())); message = builder.build(); assertEquals(66, message.getInt32ToInt32Field().get(55).intValue()); assertEquals(666, message.getInt32ToMessageField().get(555).getValue()); // Test addRepeatedField (overriding existing values) - builder.addRepeatedField(f("int32_to_int32_field"), - newMapEntry(builder, "int32_to_int32_field", 55, 55)); - builder.addRepeatedField(f("int32_to_message_field"), - newMapEntry(builder, "int32_to_message_field", 555, + builder.addRepeatedField( + f("int32_to_int32_field"), newMapEntry(builder, "int32_to_int32_field", 55, 55)); + builder.addRepeatedField( + f("int32_to_message_field"), + newMapEntry( + builder, + "int32_to_message_field", + 555, MessageValue.newBuilder().setValue(555).build())); message = builder.build(); assertEquals(55, message.getInt32ToInt32Field().get(55).intValue()); @@ -870,7 +857,7 @@ public class MapTest extends TestCase { setMapValuesUsingAccessors(builder); TestMap message = builder.build(); - String textData = TextFormat.printToString(message); + String textData = TextFormat.printer().printToString(message); builder = TestMap.newBuilder(); TextFormat.merge(textData, builder); @@ -884,10 +871,9 @@ public class MapTest extends TestCase { setMapValuesUsingAccessors(builder); TestMap message = builder.build(); - Message dynamicDefaultInstance = - DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); - Message dynamicMessage = dynamicDefaultInstance - .newBuilderForType().mergeFrom(message.toByteString()).build(); + Message dynamicDefaultInstance = DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); + Message dynamicMessage = + dynamicDefaultInstance.newBuilderForType().mergeFrom(message.toByteString()).build(); assertEquals(message, dynamicMessage); assertEquals(message.hashCode(), dynamicMessage.hashCode()); @@ -898,8 +884,7 @@ public class MapTest extends TestCase { public void testDynamicMessageUnsetKeyAndValue() throws Exception { FieldDescriptor field = f("int32_to_int32_field"); - Message dynamicDefaultInstance = - DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); + Message dynamicDefaultInstance = DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); Message.Builder builder = dynamicDefaultInstance.newBuilderForType(); // Add an entry without key and value. builder.addRepeatedField(field, builder.newBuilderForField(field).build()); @@ -916,8 +901,7 @@ public class MapTest extends TestCase { // of map entries when comparing/hashing map fields. // We use DynamicMessage to test reflection based equals()/hashCode(). - Message dynamicDefaultInstance = - DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); + Message dynamicDefaultInstance = DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); FieldDescriptor field = f("int32_to_int32_field"); Message.Builder b1 = dynamicDefaultInstance.newBuilderForType(); @@ -944,19 +928,18 @@ public class MapTest extends TestCase { } public void testUnknownEnumValues() throws Exception { - TestMap.Builder builder = TestMap.newBuilder() - .putAllInt32ToEnumFieldValue(newMap( - 0, 0, - 1, 1, - 2, 1000)); // unknown value. + TestMap.Builder builder = + TestMap.newBuilder() + .putAllInt32ToEnumFieldValue( + newMap( + 0, 0, + 1, 1, + 2, 1000)); // unknown value. TestMap message = builder.build(); - assertEquals(TestMap.EnumValue.FOO, - message.getInt32ToEnumField().get(0)); - assertEquals(TestMap.EnumValue.BAR, - message.getInt32ToEnumField().get(1)); - assertEquals(TestMap.EnumValue.UNRECOGNIZED, - message.getInt32ToEnumField().get(2)); + assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(0)); + assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1)); + assertEquals(TestMap.EnumValue.UNRECOGNIZED, message.getInt32ToEnumField().get(2)); assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); // Unknown enum values should be preserved after: @@ -977,7 +960,7 @@ public class MapTest extends TestCase { assertFalse(message.equals(message2)); // Unknown values will be converted to UNRECOGNIZED so the resulted enum map // should be the same. - assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField())); + assertEquals(message2.getInt32ToEnumField(), message.getInt32ToEnumField()); } public void testUnknownEnumValuesInReflectionApi() throws Exception { @@ -985,13 +968,13 @@ public class MapTest extends TestCase { EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor(); FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field"); - Map data = newMap( - 0, 0, - 1, 1, - 2, 1000); // unknown value + Map data = + newMap( + 0, 0, + 1, 1, + 2, 1000); // unknown value - TestMap.Builder builder = TestMap.newBuilder() - .putAllInt32ToEnumFieldValue(data); + TestMap.Builder builder = TestMap.newBuilder().putAllInt32ToEnumFieldValue(data); // Try to read unknown enum values using reflection API. for (int i = 0; i < builder.getRepeatedFieldCount(field); i++) { @@ -1001,8 +984,8 @@ public class MapTest extends TestCase { assertEquals(data.get(key).intValue(), value); Message.Builder mapEntryBuilder = mapEntry.toBuilder(); // Increase the value by 1. - setFieldValue(mapEntryBuilder, "value", - enumDescriptor.findValueByNumberCreatingIfUnknown(value + 1)); + setFieldValue( + mapEntryBuilder, "value", enumDescriptor.findValueByNumberCreatingIfUnknown(value + 1)); builder.setRepeatedField(field, i, mapEntryBuilder.build()); } @@ -1018,29 +1001,19 @@ public class MapTest extends TestCase { setMapValuesUsingAccessors(builder); TestMap message = builder.build(); - assertEquals(Arrays.asList("1", "2", "3"), - new ArrayList(message.getStringToInt32Field().keySet())); + assertEquals( + Arrays.asList("1", "2", "3"), new ArrayList<>(message.getStringToInt32Field().keySet())); } public void testGetMap() { TestMap.Builder builder = TestMap.newBuilder(); setMapValuesUsingAccessors(builder); TestMap message = builder.build(); - assertEquals( - message.getStringToInt32Field(), - message.getStringToInt32FieldMap()); - assertEquals( - message.getInt32ToBytesField(), - message.getInt32ToBytesFieldMap()); - assertEquals( - message.getInt32ToEnumField(), - message.getInt32ToEnumFieldMap()); - assertEquals( - message.getInt32ToEnumFieldValue(), - message.getInt32ToEnumFieldValueMap()); - assertEquals( - message.getInt32ToMessageField(), - message.getInt32ToMessageFieldMap()); + assertEquals(message.getStringToInt32Field(), message.getStringToInt32FieldMap()); + assertEquals(message.getInt32ToBytesField(), message.getInt32ToBytesFieldMap()); + assertEquals(message.getInt32ToEnumField(), message.getInt32ToEnumFieldMap()); + assertEquals(message.getInt32ToEnumFieldValue(), message.getInt32ToEnumFieldValueMap()); + assertEquals(message.getInt32ToMessageField(), message.getInt32ToMessageFieldMap()); } public void testContains() { @@ -1133,10 +1106,11 @@ public class MapTest extends TestCase { assertEquals( TestMap.EnumValue.BAR.getNumber(), - (int) testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(2, -1)); + testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(2, -1)); assertEquals(-1, testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(-1000, -1)); - assertEquals(MessageValue.newBuilder().setValue(11).build(), + assertEquals( + MessageValue.newBuilder().setValue(11).build(), testMapOrBuilder.getInt32ToMessageFieldOrDefault(1, null)); assertNull(testMapOrBuilder.getInt32ToMessageFieldOrDefault(-1, null)); @@ -1203,7 +1177,8 @@ public class MapTest extends TestCase { // expected } - assertEquals(MessageValue.newBuilder().setValue(11).build(), + assertEquals( + MessageValue.newBuilder().setValue(11).build(), testMapOrBuilder.getInt32ToMessageFieldOrThrow(1)); try { testMapOrBuilder.getInt32ToMessageFieldOrThrow(-1); @@ -1261,8 +1236,7 @@ public class MapTest extends TestCase { } builder.putInt32ToEnumFieldValue(1, TestMap.EnumValue.BAR.getNumber()); - assertEquals( - TestMap.EnumValue.BAR.getNumber(), builder.getInt32ToEnumFieldValueOrThrow(1)); + assertEquals(TestMap.EnumValue.BAR.getNumber(), builder.getInt32ToEnumFieldValueOrThrow(1)); builder.putInt32ToEnumFieldValue(1, -1); assertEquals(-1, builder.getInt32ToEnumFieldValueOrThrow(1)); assertEquals(TestMap.EnumValue.UNRECOGNIZED, builder.getInt32ToEnumFieldOrThrow(1)); @@ -1361,10 +1335,10 @@ public class MapTest extends TestCase { output.flush(); CodedInputStream input = CodedInputStream.newInstance(serialized); - List int32Keys = new ArrayList(); - List uint32Keys = new ArrayList(); - List int64Keys = new ArrayList(); - List stringKeys = new ArrayList(); + List int32Keys = new ArrayList<>(); + List uint32Keys = new ArrayList<>(); + List int64Keys = new ArrayList<>(); + List stringKeys = new ArrayList<>(); int tag; while (true) { tag = input.readTag(); @@ -1391,18 +1365,10 @@ public class MapTest extends TestCase { } input.popLimit(oldLimit); } - assertEquals( - Arrays.asList(-2, 0, 1, 4, 5), - int32Keys); - assertEquals( - Arrays.asList(-2, 0, 1, 4, 5), - uint32Keys); - assertEquals( - Arrays.asList(-2L, 0L, 1L, 4L, 5L), - int64Keys); - assertEquals( - Arrays.asList("", "bar", "baz", "foo", "hello", "world"), - stringKeys); + assertEquals(Arrays.asList(-2, 0, 1, 4, 5), int32Keys); + assertEquals(Arrays.asList(-2, 0, 1, 4, 5), uint32Keys); + assertEquals(Arrays.asList(-2L, 0L, 1L, 4L, 5L), int64Keys); + assertEquals(Arrays.asList("", "bar", "baz", "foo", "hello", "world"), stringKeys); } public void testInitFromPartialDynamicMessage() { @@ -1483,20 +1449,20 @@ public class MapTest extends TestCase { } private static Map newMap(K key1, V value1) { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put(key1, value1); return map; } private static Map newMap(K key1, V value1, K key2, V value2) { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put(key1, value1); map.put(key2, value2); return map; } private static Map newMap(K key1, V value1, K key2, V value2, K key3, V value3) { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put(key1, value1); map.put(key2, value2); map.put(key3, value3); @@ -1525,8 +1491,7 @@ public class MapTest extends TestCase { } try { - builder.putAllInt32ToMessageField( - MapTest.newMap(4, null, 5, null)); + builder.putAllInt32ToMessageField(MapTest.newMap(4, null, 5, null)); fail(); } catch (NullPointerException expected) { } diff --git a/java/core/src/test/java/com/google/protobuf/MessageTest.java b/java/core/src/test/java/com/google/protobuf/MessageTest.java index 4fc8f78e03..760511b635 100644 --- a/java/core/src/test/java/com/google/protobuf/MessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/MessageTest.java @@ -39,8 +39,7 @@ import java.util.List; import junit.framework.TestCase; /** - * Misc. unit tests for message operations that apply to both generated - * and dynamic messages. + * Misc. unit tests for message operations that apply to both generated and dynamic messages. * * @author kenton@google.com Kenton Varda */ @@ -49,30 +48,31 @@ public class MessageTest extends TestCase { // Message-merging tests. static final TestAllTypes MERGE_SOURCE = - TestAllTypes.newBuilder() - .setOptionalInt32(1) - .setOptionalString("foo") - .setOptionalForeignMessage(ForeignMessage.getDefaultInstance()) - .addRepeatedString("bar") - .build(); + TestAllTypes.newBuilder() + .setOptionalInt32(1) + .setOptionalString("foo") + .setOptionalForeignMessage(ForeignMessage.getDefaultInstance()) + .addRepeatedString("bar") + .build(); static final TestAllTypes MERGE_DEST = - TestAllTypes.newBuilder() - .setOptionalInt64(2) - .setOptionalString("baz") - .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(3).build()) - .addRepeatedString("qux") - .build(); + TestAllTypes.newBuilder() + .setOptionalInt64(2) + .setOptionalString("baz") + .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(3).build()) + .addRepeatedString("qux") + .build(); static final String MERGE_RESULT_TEXT = - "optional_int32: 1\n" + - "optional_int64: 2\n" + - "optional_string: \"foo\"\n" + - "optional_foreign_message {\n" + - " c: 3\n" + - "}\n" + - "repeated_string: \"qux\"\n" + - "repeated_string: \"bar\"\n"; + "" + + "optional_int32: 1\n" + + "optional_int64: 2\n" + + "optional_string: \"foo\"\n" + + "optional_foreign_message {\n" + + " c: 3\n" + + "}\n" + + "repeated_string: \"qux\"\n" + + "repeated_string: \"bar\"\n"; public void testParsingWithNullExtensionRegistry() throws Exception { try { @@ -83,23 +83,20 @@ public class MessageTest extends TestCase { } public void testMergeFrom() throws Exception { - TestAllTypes result = - TestAllTypes.newBuilder(MERGE_DEST) - .mergeFrom(MERGE_SOURCE).build(); + TestAllTypes result = TestAllTypes.newBuilder(MERGE_DEST).mergeFrom(MERGE_SOURCE).build(); assertEquals(MERGE_RESULT_TEXT, result.toString()); } /** - * Test merging a DynamicMessage into a GeneratedMessage. As long as they - * have the same descriptor, this should work, but it is an entirely different - * code path. + * Test merging a DynamicMessage into a GeneratedMessage. As long as they have the same + * descriptor, this should work, but it is an entirely different code path. */ public void testMergeFromDynamic() throws Exception { TestAllTypes result = - TestAllTypes.newBuilder(MERGE_DEST) - .mergeFrom(DynamicMessage.newBuilder(MERGE_SOURCE).build()) - .build(); + TestAllTypes.newBuilder(MERGE_DEST) + .mergeFrom(DynamicMessage.newBuilder(MERGE_SOURCE).build()) + .build(); assertEquals(MERGE_RESULT_TEXT, result.toString()); } @@ -107,9 +104,9 @@ public class MessageTest extends TestCase { /** Test merging two DynamicMessages. */ public void testDynamicMergeFrom() throws Exception { DynamicMessage result = - DynamicMessage.newBuilder(MERGE_DEST) - .mergeFrom(DynamicMessage.newBuilder(MERGE_SOURCE).build()) - .build(); + DynamicMessage.newBuilder(MERGE_DEST) + .mergeFrom(DynamicMessage.newBuilder(MERGE_SOURCE).build()) + .build(); assertEquals(MERGE_RESULT_TEXT, result.toString()); } @@ -117,10 +114,9 @@ public class MessageTest extends TestCase { // ================================================================= // Required-field-related tests. - private static final TestRequired TEST_REQUIRED_UNINITIALIZED = - TestRequired.getDefaultInstance(); + private static final TestRequired TEST_REQUIRED_UNINITIALIZED = TestRequired.getDefaultInstance(); private static final TestRequired TEST_REQUIRED_INITIALIZED = - TestRequired.newBuilder().setA(1).setB(2).setC(3).build(); + TestRequired.newBuilder().setA(1).setB(2).setC(3).build(); public void testRequired() throws Exception { TestRequired.Builder builder = TestRequired.newBuilder(); @@ -189,20 +185,18 @@ public class MessageTest extends TestCase { assertTrue(builder.isInitialized()); - builder.setField(descriptor.findFieldByName("optional_message"), - TEST_REQUIRED_UNINITIALIZED); + builder.setField(descriptor.findFieldByName("optional_message"), TEST_REQUIRED_UNINITIALIZED); assertFalse(builder.isInitialized()); - builder.setField(descriptor.findFieldByName("optional_message"), - TEST_REQUIRED_INITIALIZED); + builder.setField(descriptor.findFieldByName("optional_message"), TEST_REQUIRED_INITIALIZED); assertTrue(builder.isInitialized()); - builder.addRepeatedField(descriptor.findFieldByName("repeated_message"), - TEST_REQUIRED_UNINITIALIZED); + builder.addRepeatedField( + descriptor.findFieldByName("repeated_message"), TEST_REQUIRED_UNINITIALIZED); assertFalse(builder.isInitialized()); - builder.setRepeatedField(descriptor.findFieldByName("repeated_message"), 0, - TEST_REQUIRED_INITIALIZED); + builder.setRepeatedField( + descriptor.findFieldByName("repeated_message"), 0, TEST_REQUIRED_INITIALIZED); assertTrue(builder.isInitialized()); } @@ -224,35 +218,35 @@ public class MessageTest extends TestCase { public void testNestedUninitializedException() throws Exception { try { TestRequiredForeign.newBuilder() - .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED) - .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) - .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) - .build(); + .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED) + .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) + .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) + .build(); fail("Should have thrown an exception."); } catch (UninitializedMessageException e) { assertEquals( - "Message missing required fields: " + - "optional_message.a, " + - "optional_message.b, " + - "optional_message.c, " + - "repeated_message[0].a, " + - "repeated_message[0].b, " + - "repeated_message[0].c, " + - "repeated_message[1].a, " + - "repeated_message[1].b, " + - "repeated_message[1].c", - e.getMessage()); + "Message missing required fields: " + + "optional_message.a, " + + "optional_message.b, " + + "optional_message.c, " + + "repeated_message[0].a, " + + "repeated_message[0].b, " + + "repeated_message[0].c, " + + "repeated_message[1].a, " + + "repeated_message[1].b, " + + "repeated_message[1].c", + e.getMessage()); } } public void testBuildNestedPartial() throws Exception { // We're mostly testing that no exception is thrown. TestRequiredForeign message = - TestRequiredForeign.newBuilder() - .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED) - .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) - .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) - .buildPartial(); + TestRequiredForeign.newBuilder() + .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED) + .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) + .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) + .buildPartial(); assertFalse(message.isInitialized()); } @@ -267,28 +261,29 @@ public class MessageTest extends TestCase { public void testParseNestedUnititialized() throws Exception { ByteString data = - TestRequiredForeign.newBuilder() - .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED) - .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) - .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) - .buildPartial().toByteString(); + TestRequiredForeign.newBuilder() + .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED) + .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) + .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED) + .buildPartial() + .toByteString(); try { TestRequiredForeign.parseFrom(data); fail("Should have thrown an exception."); } catch (InvalidProtocolBufferException e) { assertEquals( - "Message missing required fields: " + - "optional_message.a, " + - "optional_message.b, " + - "optional_message.c, " + - "repeated_message[0].a, " + - "repeated_message[0].b, " + - "repeated_message[0].c, " + - "repeated_message[1].a, " + - "repeated_message[1].b, " + - "repeated_message[1].c", - e.getMessage()); + "Message missing required fields: " + + "optional_message.a, " + + "optional_message.b, " + + "optional_message.c, " + + "repeated_message[0].a, " + + "repeated_message[0].b, " + + "repeated_message[0].c, " + + "repeated_message[1].a, " + + "repeated_message[1].b, " + + "repeated_message[1].c", + e.getMessage()); } } @@ -303,9 +298,7 @@ public class MessageTest extends TestCase { public void testDynamicBuildPartial() throws Exception { // We're mostly testing that no exception is thrown. - DynamicMessage message = - DynamicMessage.newBuilder(TestRequired.getDescriptor()) - .buildPartial(); + DynamicMessage message = DynamicMessage.newBuilder(TestRequired.getDescriptor()).buildPartial(); assertFalse(message.isInitialized()); } @@ -318,43 +311,44 @@ public class MessageTest extends TestCase { assertEquals("Message missing required fields: a, b, c", e.getMessage()); } } - + /** Test reading unset repeated message from DynamicMessage. */ public void testDynamicRepeatedMessageNull() throws Exception { - Descriptors.Descriptor descriptor = TestRequired.getDescriptor(); + TestRequired.getDescriptor(); DynamicMessage result = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()) - .mergeFrom(DynamicMessage.newBuilder(MERGE_SOURCE).build()) - .build(); + DynamicMessage.newBuilder(TestAllTypes.getDescriptor()) + .mergeFrom(DynamicMessage.newBuilder(MERGE_SOURCE).build()) + .build(); - assertTrue(result.getField(result.getDescriptorForType() - .findFieldByName("repeated_foreign_message")) instanceof List); + assertTrue( + result.getField(result.getDescriptorForType().findFieldByName("repeated_foreign_message")) + instanceof List); assertEquals( 0, result.getRepeatedFieldCount( result.getDescriptorForType().findFieldByName("repeated_foreign_message"))); } - + /** Test reading repeated message from DynamicMessage. */ public void testDynamicRepeatedMessageNotNull() throws Exception { - - TestAllTypes REPEATED_NESTED = - TestAllTypes.newBuilder() - .setOptionalInt32(1) - .setOptionalString("foo") - .setOptionalForeignMessage(ForeignMessage.getDefaultInstance()) - .addRepeatedString("bar") - .addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()) - .addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()) - .build(); - Descriptors.Descriptor descriptor = TestRequired.getDescriptor(); + TestAllTypes repeatedNested = + TestAllTypes.newBuilder() + .setOptionalInt32(1) + .setOptionalString("foo") + .setOptionalForeignMessage(ForeignMessage.getDefaultInstance()) + .addRepeatedString("bar") + .addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()) + .addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()) + .build(); + TestRequired.getDescriptor(); DynamicMessage result = - DynamicMessage.newBuilder(TestAllTypes.getDescriptor()) - .mergeFrom(DynamicMessage.newBuilder(REPEATED_NESTED).build()) - .build(); + DynamicMessage.newBuilder(TestAllTypes.getDescriptor()) + .mergeFrom(DynamicMessage.newBuilder(repeatedNested).build()) + .build(); - assertTrue(result.getField(result.getDescriptorForType() - .findFieldByName("repeated_foreign_message")) instanceof List); + assertTrue( + result.getField(result.getDescriptorForType().findFieldByName("repeated_foreign_message")) + instanceof List); assertEquals( 2, result.getRepeatedFieldCount( diff --git a/java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java b/java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java index 03ed65a550..7bfeaf17c4 100644 --- a/java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java +++ b/java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java @@ -37,8 +37,8 @@ import java.util.List; import junit.framework.TestCase; /** - * Test cases that exercise end-to-end use cases involving - * {@link SingleFieldBuilder} and {@link RepeatedFieldBuilder}. + * Test cases that exercise end-to-end use cases involving {@link SingleFieldBuilder} and {@link + * RepeatedFieldBuilder}. * * @author jonp@google.com (Jon Perlow) */ @@ -46,20 +46,11 @@ public class NestedBuildersTest extends TestCase { public void testMessagesAndBuilders() { Vehicle.Builder vehicleBuilder = Vehicle.newBuilder(); - vehicleBuilder.addWheelBuilder() - .setRadius(4) - .setWidth(1); - vehicleBuilder.addWheelBuilder() - .setRadius(4) - .setWidth(2); - vehicleBuilder.addWheelBuilder() - .setRadius(4) - .setWidth(3); - vehicleBuilder.addWheelBuilder() - .setRadius(4) - .setWidth(4); - vehicleBuilder.getEngineBuilder() - .setLiters(10); + vehicleBuilder.addWheelBuilder().setRadius(4).setWidth(1); + vehicleBuilder.addWheelBuilder().setRadius(4).setWidth(2); + vehicleBuilder.addWheelBuilder().setRadius(4).setWidth(3); + vehicleBuilder.addWheelBuilder().setRadius(4).setWidth(4); + vehicleBuilder.getEngineBuilder().setLiters(10); Vehicle vehicle = vehicleBuilder.build(); assertEquals(4, vehicle.getWheelCount()); @@ -71,9 +62,7 @@ public class NestedBuildersTest extends TestCase { assertEquals(10, vehicle.getEngine().getLiters()); for (int i = 0; i < 4; i++) { - vehicleBuilder.getWheelBuilder(i) - .setRadius(5) - .setWidth(i + 10); + vehicleBuilder.getWheelBuilder(i).setRadius(5).setWidth(i + 10); } vehicleBuilder.getEngineBuilder().setLiters(20); @@ -89,18 +78,10 @@ public class NestedBuildersTest extends TestCase { public void testMessagesAreCached() { Vehicle.Builder vehicleBuilder = Vehicle.newBuilder(); - vehicleBuilder.addWheelBuilder() - .setRadius(1) - .setWidth(2); - vehicleBuilder.addWheelBuilder() - .setRadius(3) - .setWidth(4); - vehicleBuilder.addWheelBuilder() - .setRadius(5) - .setWidth(6); - vehicleBuilder.addWheelBuilder() - .setRadius(7) - .setWidth(8); + vehicleBuilder.addWheelBuilder().setRadius(1).setWidth(2); + vehicleBuilder.addWheelBuilder().setRadius(3).setWidth(4); + vehicleBuilder.addWheelBuilder().setRadius(5).setWidth(6); + vehicleBuilder.addWheelBuilder().setRadius(7).setWidth(8); // Make sure messages are cached. List wheels = new ArrayList(vehicleBuilder.getWheelList()); @@ -117,8 +98,7 @@ public class NestedBuildersTest extends TestCase { } // Change just one - vehicleBuilder.getWheelBuilder(3) - .setRadius(20).setWidth(20); + vehicleBuilder.getWheelBuilder(3).setRadius(20).setWidth(20); // Now get wheels and check that only that one changed for (int i = 0; i < wheels.size(); i++) { @@ -132,12 +112,8 @@ public class NestedBuildersTest extends TestCase { public void testRemove_WithNestedBuilders() { Vehicle.Builder vehicleBuilder = Vehicle.newBuilder(); - vehicleBuilder.addWheelBuilder() - .setRadius(1) - .setWidth(1); - vehicleBuilder.addWheelBuilder() - .setRadius(2) - .setWidth(2); + vehicleBuilder.addWheelBuilder().setRadius(1).setWidth(1); + vehicleBuilder.addWheelBuilder().setRadius(2).setWidth(2); vehicleBuilder.removeWheel(0); assertEquals(1, vehicleBuilder.getWheelCount()); @@ -146,12 +122,8 @@ public class NestedBuildersTest extends TestCase { public void testRemove_WithNestedMessages() { Vehicle.Builder vehicleBuilder = Vehicle.newBuilder(); - vehicleBuilder.addWheel(Wheel.newBuilder() - .setRadius(1) - .setWidth(1)); - vehicleBuilder.addWheel(Wheel.newBuilder() - .setRadius(2) - .setWidth(2)); + vehicleBuilder.addWheel(Wheel.newBuilder().setRadius(1).setWidth(1)); + vehicleBuilder.addWheel(Wheel.newBuilder().setRadius(2).setWidth(2)); vehicleBuilder.removeWheel(0); assertEquals(1, vehicleBuilder.getWheelCount()); @@ -159,14 +131,13 @@ public class NestedBuildersTest extends TestCase { } public void testMerge() { - Vehicle vehicle1 = Vehicle.newBuilder() - .addWheel(Wheel.newBuilder().setRadius(1).build()) - .addWheel(Wheel.newBuilder().setRadius(2).build()) - .build(); - - Vehicle vehicle2 = Vehicle.newBuilder() - .mergeFrom(vehicle1) - .build(); + Vehicle vehicle1 = + Vehicle.newBuilder() + .addWheel(Wheel.newBuilder().setRadius(1).build()) + .addWheel(Wheel.newBuilder().setRadius(2).build()) + .build(); + + Vehicle vehicle2 = Vehicle.newBuilder().mergeFrom(vehicle1).build(); // List should be the same -- no allocation assertSame(vehicle1.getWheelList(), vehicle2.getWheelList()); diff --git a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java index c388bd0561..304261c0a0 100644 --- a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java @@ -48,9 +48,7 @@ import java.util.List; import java.util.NoSuchElementException; import junit.framework.TestCase; -/** - * Tests for {@link NioByteString}. - */ +/** Tests for {@link NioByteString}. */ public class NioByteStringTest extends TestCase { private static final ByteString EMPTY = new NioByteString(ByteBuffer.wrap(new byte[0])); private static final String CLASSNAME = NioByteString.class.getSimpleName(); @@ -108,8 +106,7 @@ public class NioByteStringTest extends TestCase { } public void testSize() { - assertEquals(CLASSNAME + " must have the expected size", BYTES.length, - testString.size()); + assertEquals(CLASSNAME + " must have the expected size", BYTES.length, testString.size()); } public void testGetTreeDepth() { @@ -140,10 +137,8 @@ public class NioByteStringTest extends TestCase { try { // Copy one too many bytes - testString.copyTo(destination, testString.size() + 1 - length, - destinationOffset, length); - fail("Should have thrown an exception when copying too many bytes of a " - + CLASSNAME); + testString.copyTo(destination, testString.size() + 1 - length, destinationOffset, length); + fail("Should have thrown an exception when copying too many bytes of a " + CLASSNAME); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -151,8 +146,7 @@ public class NioByteStringTest extends TestCase { try { // Copy with illegal negative sourceOffset testString.copyTo(destination, -1, destinationOffset, length); - fail("Should have thrown an exception when given a negative sourceOffset in " - + CLASSNAME); + fail("Should have thrown an exception when given a negative sourceOffset in " + CLASSNAME); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -160,8 +154,9 @@ public class NioByteStringTest extends TestCase { try { // Copy with illegal negative destinationOffset testString.copyTo(destination, 0, -1, length); - fail("Should have thrown an exception when given a negative destinationOffset in " - + CLASSNAME); + fail( + "Should have thrown an exception when given a negative destinationOffset in " + + CLASSNAME); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -169,8 +164,7 @@ public class NioByteStringTest extends TestCase { try { // Copy with illegal negative size testString.copyTo(destination, 0, 0, -1); - fail("Should have thrown an exception when given a negative size in " - + CLASSNAME); + fail("Should have thrown an exception when given a negative size in " + CLASSNAME); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -178,8 +172,9 @@ public class NioByteStringTest extends TestCase { try { // Copy with illegal too-large sourceOffset testString.copyTo(destination, 2 * testString.size(), 0, length); - fail("Should have thrown an exception when the destinationOffset is too large in " - + CLASSNAME); + fail( + "Should have thrown an exception when the destinationOffset is too large in " + + CLASSNAME); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -187,8 +182,9 @@ public class NioByteStringTest extends TestCase { try { // Copy with illegal too-large destinationOffset testString.copyTo(destination, 0, 2 * destination.length, length); - fail("Should have thrown an exception when the destinationOffset is too large in " - + CLASSNAME); + fail( + "Should have thrown an exception when the destinationOffset is too large in " + + CLASSNAME); } catch (IndexOutOfBoundsException expected) { // This is success } @@ -199,8 +195,8 @@ public class NioByteStringTest extends TestCase { ByteBuffer myBuffer = ByteBuffer.allocate(BYTES.length); testString.copyTo(myBuffer); myBuffer.flip(); - assertEquals(CLASSNAME + ".copyTo(ByteBuffer) must give back the same bytes", - backingBuffer, myBuffer); + assertEquals( + CLASSNAME + ".copyTo(ByteBuffer) must give back the same bytes", backingBuffer, myBuffer); // Target buffer bigger than required. myBuffer = ByteBuffer.allocate(testString.size() + 1); @@ -241,30 +237,31 @@ public class NioByteStringTest extends TestCase { skipFully(stream, fraction); // Skip a large fraction, but not all. assertEquals( CLASSNAME + ": after skipping to the 'middle', half the bytes are available", - (testString.size() - fraction), stream.available()); + (testString.size() - fraction), + stream.available()); stream.reset(); assertEquals( CLASSNAME + ": after resetting, all bytes are available", - testString.size(), stream.available()); + testString.size(), + stream.available()); skipFully(stream, testString.size()); // Skip to the end. assertEquals( CLASSNAME + ": after skipping to the end, no more bytes are available", - 0, stream.available()); + 0, + stream.available()); } /** - * Discards {@code n} bytes of data from the input stream. This method - * will block until the full amount has been skipped. Does not close the - * stream. + * Discards {@code n} bytes of data from the input stream. This method will block until the full + * amount has been skipped. Does not close the stream. + * *

Copied from com.google.common.io.ByteStreams to avoid adding dependency. * * @param in the input stream to read from * @param n the number of bytes to skip - * @throws EOFException if this stream reaches the end before skipping all - * the bytes - * @throws IOException if an I/O error occurs, or the stream does not - * support skipping + * @throws EOFException if this stream reaches the end before skipping all the bytes + * @throws IOException if an I/O error occurs, or the stream does not support skipping */ static void skipFully(InputStream in, long n) throws IOException { long toSkip = n; @@ -274,8 +271,12 @@ public class NioByteStringTest extends TestCase { // Force a blocking read to avoid infinite loop if (in.read() == -1) { long skipped = toSkip - n; - throw new EOFException("reached end of stream after skipping " - + skipped + " bytes; " + toSkip + " bytes expected"); + throw new EOFException( + "reached end of stream after skipping " + + skipped + + " bytes; " + + toSkip + + " bytes expected"); } n--; } else { @@ -290,7 +291,8 @@ public class NioByteStringTest extends TestCase { assertTrue(byteBuffer.remaining() == BYTES.length); assertTrue(byteBuffer.isReadOnly()); byteBuffer.get(roundTripBytes); - assertTrue(CLASSNAME + ".asReadOnlyByteBuffer() must give back the same bytes", + assertTrue( + CLASSNAME + ".asReadOnlyByteBuffer() must give back the same bytes", Arrays.equals(BYTES, roundTripBytes)); } @@ -306,13 +308,15 @@ public class NioByteStringTest extends TestCase { bytesSeen += thisLength; } assertTrue(bytesSeen == BYTES.length); - assertTrue(CLASSNAME + ".asReadOnlyByteBufferTest() must give back the same bytes", + assertTrue( + CLASSNAME + ".asReadOnlyByteBufferTest() must give back the same bytes", Arrays.equals(BYTES, roundTripBytes)); } public void testToByteArray() { byte[] roundTripBytes = testString.toByteArray(); - assertTrue(CLASSNAME + ".toByteArray() must give back the same bytes", + assertTrue( + CLASSNAME + ".toByteArray() must give back the same bytes", Arrays.equals(BYTES, roundTripBytes)); } @@ -320,80 +324,87 @@ public class NioByteStringTest extends TestCase { ByteArrayOutputStream bos = new ByteArrayOutputStream(); testString.writeTo(bos); byte[] roundTripBytes = bos.toByteArray(); - assertTrue(CLASSNAME + ".writeTo() must give back the same bytes", + assertTrue( + CLASSNAME + ".writeTo() must give back the same bytes", Arrays.equals(BYTES, roundTripBytes)); } public void testWriteToShouldNotExposeInternalBufferToOutputStream() throws IOException { - OutputStream os = new OutputStream() { - @Override - public void write(byte[] b, int off, int len) { - Arrays.fill(b, off, off + len, (byte) 0); - } - - @Override - public void write(int b) { - throw new UnsupportedOperationException(); - } - }; + OutputStream os = + new OutputStream() { + @Override + public void write(byte[] b, int off, int len) { + Arrays.fill(b, off, off + len, (byte) 0); + } + + @Override + public void write(int b) { + throw new UnsupportedOperationException(); + } + }; byte[] original = Arrays.copyOf(BYTES, BYTES.length); testString.writeTo(os); - assertTrue(CLASSNAME + ".writeTo() must NOT grant access to underlying buffer", + assertTrue( + CLASSNAME + ".writeTo() must NOT grant access to underlying buffer", Arrays.equals(original, BYTES)); } public void testWriteToInternalShouldExposeInternalBufferToOutputStream() throws IOException { - OutputStream os = new OutputStream() { - @Override - public void write(byte[] b, int off, int len) { - Arrays.fill(b, off, off + len, (byte) 0); - } - - @Override - public void write(int b) { - throw new UnsupportedOperationException(); - } - }; + OutputStream os = + new OutputStream() { + @Override + public void write(byte[] b, int off, int len) { + Arrays.fill(b, off, off + len, (byte) 0); + } + + @Override + public void write(int b) { + throw new UnsupportedOperationException(); + } + }; testString.writeToInternal(os, 0, testString.size()); byte[] allZeros = new byte[testString.size()]; - assertTrue(CLASSNAME + ".writeToInternal() must grant access to underlying buffer", + assertTrue( + CLASSNAME + ".writeToInternal() must grant access to underlying buffer", Arrays.equals(allZeros, backingBuffer.array())); } public void testWriteToShouldExposeInternalBufferToByteOutput() throws IOException { - ByteOutput out = new ByteOutput() { - @Override - public void write(byte value) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public void write(byte[] value, int offset, int length) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public void writeLazy(byte[] value, int offset, int length) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public void write(ByteBuffer value) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public void writeLazy(ByteBuffer value) throws IOException { - Arrays.fill(value.array(), value.arrayOffset(), value.arrayOffset() + value.limit(), - (byte) 0); - } - }; + ByteOutput out = + new ByteOutput() { + @Override + public void write(byte value) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void write(byte[] value, int offset, int length) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void write(ByteBuffer value) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void writeLazy(ByteBuffer value) throws IOException { + Arrays.fill( + value.array(), value.arrayOffset(), value.arrayOffset() + value.limit(), (byte) 0); + } + }; testString.writeTo(out); byte[] allZeros = new byte[testString.size()]; - assertTrue(CLASSNAME + ".writeTo() must grant access to underlying buffer", + assertTrue( + CLASSNAME + ".writeTo() must grant access to underlying buffer", Arrays.equals(allZeros, backingBuffer.array())); } @@ -401,21 +412,21 @@ public class NioByteStringTest extends TestCase { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteString.Output output = ByteString.newOutput(); testString.writeTo(output); - assertEquals("Output Size returns correct result", - output.size(), testString.size()); + assertEquals("Output Size returns correct result", output.size(), testString.size()); output.writeTo(bos); - assertTrue("Output.writeTo() must give back the same bytes", - Arrays.equals(BYTES, bos.toByteArray())); + assertTrue( + "Output.writeTo() must give back the same bytes", Arrays.equals(BYTES, bos.toByteArray())); // write the output stream to itself! This should cause it to double output.writeTo(output); - assertEquals("Writing an output stream to itself is successful", - testString.concat(testString), output.toByteString()); + assertEquals( + "Writing an output stream to itself is successful", + testString.concat(testString), + output.toByteString()); output.reset(); assertEquals("Output.reset() resets the output", 0, output.size()); - assertEquals("Output.reset() resets the output", - EMPTY, output.toByteString()); + assertEquals("Output.reset() resets the output", EMPTY, output.toByteString()); } public void testToString() { @@ -433,7 +444,8 @@ public class NioByteStringTest extends TestCase { } public void testToString_returnsCanonicalEmptyString() { - assertSame(CLASSNAME + " must be the same string references", + assertSame( + CLASSNAME + " must be the same string references", EMPTY.toString(UTF_8), new NioByteString(ByteBuffer.wrap(new byte[0])).toString(UTF_8)); } @@ -457,33 +469,34 @@ public class NioByteStringTest extends TestCase { public void testEquals() { assertEquals(CLASSNAME + " must not equal null", false, testString.equals(null)); assertEquals(CLASSNAME + " must equal self", testString, testString); - assertFalse(CLASSNAME + " must not equal the empty string", - testString.equals(EMPTY)); - assertEquals(CLASSNAME + " empty strings must be equal", - EMPTY, testString.substring(55, 55)); - assertEquals(CLASSNAME + " must equal another string with the same value", - testString, new NioByteString(backingBuffer)); + assertFalse(CLASSNAME + " must not equal the empty string", testString.equals(EMPTY)); + assertEquals(CLASSNAME + " empty strings must be equal", EMPTY, testString.substring(55, 55)); + assertEquals( + CLASSNAME + " must equal another string with the same value", + testString, + new NioByteString(backingBuffer)); byte[] mungedBytes = mungedBytes(); - assertFalse(CLASSNAME + " must not equal every string with the same length", + assertFalse( + CLASSNAME + " must not equal every string with the same length", testString.equals(new NioByteString(ByteBuffer.wrap(mungedBytes)))); } public void testEqualsLiteralByteString() { ByteString literal = ByteString.copyFrom(BYTES); - assertEquals(CLASSNAME + " must equal LiteralByteString with same value", literal, - testString); - assertEquals(CLASSNAME + " must equal LiteralByteString with same value", testString, - literal); - assertFalse(CLASSNAME + " must not equal the empty string", - testString.equals(ByteString.EMPTY)); - assertEquals(CLASSNAME + " empty strings must be equal", - ByteString.EMPTY, testString.substring(55, 55)); + assertEquals(CLASSNAME + " must equal LiteralByteString with same value", literal, testString); + assertEquals(CLASSNAME + " must equal LiteralByteString with same value", testString, literal); + assertFalse( + CLASSNAME + " must not equal the empty string", testString.equals(ByteString.EMPTY)); + assertEquals( + CLASSNAME + " empty strings must be equal", ByteString.EMPTY, testString.substring(55, 55)); literal = ByteString.copyFrom(mungedBytes()); - assertFalse(CLASSNAME + " must not equal every LiteralByteString with the same length", + assertFalse( + CLASSNAME + " must not equal every LiteralByteString with the same length", testString.equals(literal)); - assertFalse(CLASSNAME + " must not equal every LiteralByteString with the same length", + assertFalse( + CLASSNAME + " must not equal every LiteralByteString with the same length", literal.equals(testString)); } @@ -492,22 +505,25 @@ public class NioByteStringTest extends TestCase { ByteString p2 = ByteString.copyFrom(BYTES, 5, BYTES.length - 5); ByteString rope = p1.concat(p2); - assertEquals(CLASSNAME + " must equal RopeByteString with same value", rope, - testString); - assertEquals(CLASSNAME + " must equal RopeByteString with same value", testString, - rope); - assertFalse(CLASSNAME + " must not equal the empty string", + assertEquals(CLASSNAME + " must equal RopeByteString with same value", rope, testString); + assertEquals(CLASSNAME + " must equal RopeByteString with same value", testString, rope); + assertFalse( + CLASSNAME + " must not equal the empty string", testString.equals(ByteString.EMPTY.concat(ByteString.EMPTY))); - assertEquals(CLASSNAME + " empty strings must be equal", - ByteString.EMPTY.concat(ByteString.EMPTY), testString.substring(55, 55)); + assertEquals( + CLASSNAME + " empty strings must be equal", + ByteString.EMPTY.concat(ByteString.EMPTY), + testString.substring(55, 55)); byte[] mungedBytes = mungedBytes(); p1 = ByteString.copyFrom(mungedBytes, 0, 5); p2 = ByteString.copyFrom(mungedBytes, 5, mungedBytes.length - 5); rope = p1.concat(p2); - assertFalse(CLASSNAME + " must not equal every RopeByteString with the same length", + assertFalse( + CLASSNAME + " must not equal every RopeByteString with the same length", testString.equals(rope)); - assertFalse(CLASSNAME + " must not equal every RopeByteString with the same length", + assertFalse( + CLASSNAME + " must not equal every RopeByteString with the same length", rope.equals(testString)); } @@ -525,32 +541,34 @@ public class NioByteStringTest extends TestCase { public void testPeekCachedHashCode() { ByteString newString = new NioByteString(backingBuffer); - assertEquals(CLASSNAME + ".peekCachedHashCode() should return zero at first", 0, + assertEquals( + CLASSNAME + ".peekCachedHashCode() should return zero at first", + 0, newString.peekCachedHashCode()); newString.hashCode(); - assertEquals(CLASSNAME + ".peekCachedHashCode should return zero at first", - EXPECTED_HASH, newString.peekCachedHashCode()); + assertEquals( + CLASSNAME + ".peekCachedHashCode should return zero at first", + EXPECTED_HASH, + newString.peekCachedHashCode()); } public void testPartialHash() { // partialHash() is more strenuously tested elsewhere by testing hashes of substrings. // This test would fail if the expected hash were 1. It's not. int hash = testString.partialHash(testString.size(), 0, testString.size()); - assertEquals(CLASSNAME + ".partialHash() must yield expected hashCode", - EXPECTED_HASH, hash); + assertEquals(CLASSNAME + ".partialHash() must yield expected hashCode", EXPECTED_HASH, hash); } public void testNewInput() throws IOException { InputStream input = testString.newInput(); - assertEquals("InputStream.available() returns correct value", - testString.size(), input.available()); + assertEquals( + "InputStream.available() returns correct value", testString.size(), input.available()); boolean stillEqual = true; for (byte referenceByte : BYTES) { int expectedInt = (referenceByte & 0xFF); stillEqual = (expectedInt == input.read()); } - assertEquals("InputStream.available() returns correct value", - 0, input.available()); + assertEquals("InputStream.available() returns correct value", 0, input.available()); assertTrue(CLASSNAME + " must give the same bytes from the InputStream", stillEqual); assertEquals(CLASSNAME + " InputStream must now be exhausted", -1, input.read()); } @@ -561,43 +579,44 @@ public class NioByteStringTest extends TestCase { int nearEndIndex = stringSize * 2 / 3; long skipped1 = input.skip(nearEndIndex); assertEquals("InputStream.skip()", skipped1, nearEndIndex); - assertEquals("InputStream.available()", - stringSize - skipped1, input.available()); + assertEquals("InputStream.available()", stringSize - skipped1, input.available()); assertTrue("InputStream.mark() is available", input.markSupported()); input.mark(0); - assertEquals("InputStream.skip(), read()", - testString.byteAt(nearEndIndex) & 0xFF, input.read()); - assertEquals("InputStream.available()", - stringSize - skipped1 - 1, input.available()); + assertEquals( + "InputStream.skip(), read()", testString.byteAt(nearEndIndex) & 0xFF, input.read()); + assertEquals("InputStream.available()", stringSize - skipped1 - 1, input.available()); long skipped2 = input.skip(stringSize); - assertEquals("InputStream.skip() incomplete", - skipped2, stringSize - skipped1 - 1); + assertEquals("InputStream.skip() incomplete", skipped2, stringSize - skipped1 - 1); assertEquals("InputStream.skip(), no more input", 0, input.available()); assertEquals("InputStream.skip(), no more input", -1, input.read()); input.reset(); - assertEquals("InputStream.reset() succeded", - stringSize - skipped1, input.available()); - assertEquals("InputStream.reset(), read()", - testString.byteAt(nearEndIndex) & 0xFF, input.read()); + assertEquals("InputStream.reset() succeded", stringSize - skipped1, input.available()); + assertEquals( + "InputStream.reset(), read()", testString.byteAt(nearEndIndex) & 0xFF, input.read()); } public void testNewCodedInput() throws IOException { CodedInputStream cis = testString.newCodedInput(); byte[] roundTripBytes = cis.readRawBytes(BYTES.length); - assertTrue(CLASSNAME + " must give the same bytes back from the CodedInputStream", + assertTrue( + CLASSNAME + " must give the same bytes back from the CodedInputStream", Arrays.equals(BYTES, roundTripBytes)); assertTrue(CLASSNAME + " CodedInputStream must now be exhausted", cis.isAtEnd()); } /** - * Make sure we keep things simple when concatenating with empty. See also - * {@link ByteStringTest#testConcat_empty()}. + * Make sure we keep things simple when concatenating with empty. See also {@link + * ByteStringTest#testConcat_empty()}. */ public void testConcat_empty() { - assertSame(CLASSNAME + " concatenated with empty must give " + CLASSNAME, - testString.concat(EMPTY), testString); - assertSame("empty concatenated with " + CLASSNAME + " must give " + CLASSNAME, - EMPTY.concat(testString), testString); + assertSame( + CLASSNAME + " concatenated with empty must give " + CLASSNAME, + testString.concat(EMPTY), + testString); + assertSame( + "empty concatenated with " + CLASSNAME + " must give " + CLASSNAME, + EMPTY.concat(testString), + testString); } public void testJavaSerialization() throws Exception { diff --git a/java/core/src/test/java/com/google/protobuf/PackedFieldTest.java b/java/core/src/test/java/com/google/protobuf/PackedFieldTest.java new file mode 100755 index 0000000000..2397d2ebc3 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/PackedFieldTest.java @@ -0,0 +1,232 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.PackedFieldTestProto.TestAllTypes; +import com.google.protobuf.PackedFieldTestProto.TestAllTypes.NestedEnum; +import com.google.protobuf.PackedFieldTestProto.TestUnpackedTypes; +import junit.framework.TestCase; + +/** Tests primitive repeated fields in proto3 are packed in wire format. */ +public class PackedFieldTest extends TestCase { + static final ByteString expectedPackedRawBytes = + ByteString.copyFrom( + new byte[] { + (byte) 0xFA, + 0x01, + 0x01, + 0x01, // repeated int32 + (byte) 0x82, + 0x02, + 0x01, + 0x01, // repeated int64 + (byte) 0x8A, + 0x02, + 0x01, + 0x01, // repeated uint32 + (byte) 0x92, + 0x02, + 0x01, + 0x01, // repeated uint64 + (byte) 0x9A, + 0x02, + 0x01, + 0x02, // repeated sint32 + (byte) 0xA2, + 0x02, + 0x01, + 0x02, // repeated sint64 + (byte) 0xAA, + 0x02, + 0x04, + 0x01, + 0x00, + 0x00, + 0x00, // repeated fixed32 + (byte) 0xB2, + 0x02, + 0x08, + 0x01, + 0x00, + 0x00, + 0x00, // repeated fixed64 + 0x00, + 0x00, + 0x00, + 0x00, + (byte) 0xBA, + 0x02, + 0x04, + 0x01, + 0x00, + 0x00, + 0x00, // repeated sfixed32 + (byte) 0xC2, + 0x02, + 0x08, + 0x01, + 0x00, + 0x00, + 0x00, // repeated sfixed64 + 0x00, + 0x00, + 0x00, + 0x00, + (byte) 0xCA, + 0x02, + 0x04, + 0x00, + 0x00, + (byte) 0x80, + 0x3f, // repeated float + (byte) 0xD2, + 0x02, + 0x08, + 0x00, + 0x00, + 0x00, + 0x00, // repeated double + 0x00, + 0x00, + (byte) 0xf0, + 0x3f, + (byte) 0xDA, + 0x02, + 0x01, + 0x01, // repeated bool + (byte) 0x9A, + 0x03, + 0x01, + 0x01 // repeated nested enum + }); + + static final ByteString expectedUnpackedRawBytes = + ByteString.copyFrom( + new byte[] { + 0x08, + 0x01, // repeated int32 + 0x10, + 0x01, // repeated int64 + 0x18, + 0x01, // repeated uint32 + 0x20, + 0x01, // repeated uint64 + 0x28, + 0x02, // repeated sint32 + 0x30, + 0x02, // repeated sint64 + 0x3D, + 0x01, + 0x00, + 0x00, + 0x00, // repeated fixed32 + 0x41, + 0x01, + 0x00, + 0x00, + 0x00, // repeated fixed64 + 0x00, + 0x00, + 0x00, + 0x00, + 0x4D, + 0x01, + 0x00, + 0x00, + 0x00, // repeated sfixed32 + 0x51, + 0x01, + 0x00, + 0x00, + 0x00, // repeated sfixed64 + 0x00, + 0x00, + 0x00, + 0x00, + 0x5D, + 0x00, + 0x00, + (byte) 0x80, + 0x3f, // repeated float + 0x61, + 0x00, + 0x00, + 0x00, + 0x00, // repeated double + 0x00, + 0x00, + (byte) 0xf0, + 0x3f, + 0x68, + 0x01, // repeated bool + 0x70, + 0x01, // repeated nested enum + }); + + public void testPackedGeneratedMessage() throws Exception { + TestAllTypes message = TestAllTypes.parseFrom(expectedPackedRawBytes); + assertEquals(expectedPackedRawBytes, message.toByteString()); + } + + public void testPackedDynamicMessageSerialize() throws Exception { + DynamicMessage message = + DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), expectedPackedRawBytes); + assertEquals(expectedPackedRawBytes, message.toByteString()); + } + + public void testUnpackedGeneratedMessage() throws Exception { + TestUnpackedTypes message = TestUnpackedTypes.parseFrom(expectedUnpackedRawBytes); + assertEquals(expectedUnpackedRawBytes, message.toByteString()); + } + + public void testUnPackedDynamicMessageSerialize() throws Exception { + DynamicMessage message = + DynamicMessage.parseFrom(TestUnpackedTypes.getDescriptor(), expectedUnpackedRawBytes); + assertEquals(expectedUnpackedRawBytes, message.toByteString()); + } + + // Make sure we haven't screwed up the code generation for packing fields by default. + public void testPackedSerialization() throws Exception { + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedInt32(1234) + .addRepeatedNestedEnum(NestedEnum.BAR) + .build(); + + CodedInputStream in = CodedInputStream.newInstance(message.toByteArray()); + + while (!in.isAtEnd()) { + int tag = in.readTag(); + assertEquals(WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.getTagWireType(tag)); + in.skipField(tag); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java b/java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java index e376b1cd72..4e63ee76ae 100644 --- a/java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java +++ b/java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java @@ -48,9 +48,9 @@ import org.junit.runners.JUnit4; /** * Tests the exceptions thrown when parsing from a stream. The methods on the {@link Parser} * interface are specified to only throw {@link InvalidProtocolBufferException}. But we really want - * to distinguish between invalid protos vs. actual I/O errors (like failures reading from a - * socket, etc.). So, when we're not using the parser directly, an {@link IOException} should be - * thrown where appropriate, instead of always an {@link InvalidProtocolBufferException}. + * to distinguish between invalid protos vs. actual I/O errors (like failures reading from a socket, + * etc.). So, when we're not using the parser directly, an {@link IOException} should be thrown + * where appropriate, instead of always an {@link InvalidProtocolBufferException}. * * @author jh@squareup.com (Joshua Humphries) */ @@ -61,7 +61,7 @@ public class ParseExceptionsTest { DescriptorProto parse(InputStream in) throws IOException; } - private byte serializedProto[]; + private byte[] serializedProto; private void setup() { serializedProto = DescriptorProto.getDescriptor().toProto().toByteArray(); @@ -77,7 +77,8 @@ public class ParseExceptionsTest { serializedProto = bos.toByteArray(); } - @Test public void message_parseFrom_InputStream() { + @Test + public void message_parseFrom_InputStream() { setup(); verifyExceptions( new ParseTester() { @@ -88,7 +89,8 @@ public class ParseExceptionsTest { }); } - @Test public void message_parseFrom_InputStreamAndExtensionRegistry() { + @Test + public void message_parseFrom_InputStreamAndExtensionRegistry() { setup(); verifyExceptions( new ParseTester() { @@ -99,7 +101,8 @@ public class ParseExceptionsTest { }); } - @Test public void message_parseFrom_CodedInputStream() { + @Test + public void message_parseFrom_CodedInputStream() { setup(); verifyExceptions( new ParseTester() { @@ -110,7 +113,8 @@ public class ParseExceptionsTest { }); } - @Test public void message_parseFrom_CodedInputStreamAndExtensionRegistry() { + @Test + public void message_parseFrom_CodedInputStreamAndExtensionRegistry() { setup(); verifyExceptions( new ParseTester() { @@ -122,7 +126,8 @@ public class ParseExceptionsTest { }); } - @Test public void message_parseDelimitedFrom_InputStream() { + @Test + public void message_parseDelimitedFrom_InputStream() { setupDelimited(); verifyExceptions( new ParseTester() { @@ -133,7 +138,8 @@ public class ParseExceptionsTest { }); } - @Test public void message_parseDelimitedFrom_InputStreamAndExtensionRegistry() { + @Test + public void message_parseDelimitedFrom_InputStreamAndExtensionRegistry() { setupDelimited(); verifyExceptions( new ParseTester() { @@ -144,7 +150,8 @@ public class ParseExceptionsTest { }); } - @Test public void messageBuilder_mergeFrom_InputStream() { + @Test + public void messageBuilder_mergeFrom_InputStream() { setup(); verifyExceptions( new ParseTester() { @@ -155,7 +162,8 @@ public class ParseExceptionsTest { }); } - @Test public void messageBuilder_mergeFrom_InputStreamAndExtensionRegistry() { + @Test + public void messageBuilder_mergeFrom_InputStreamAndExtensionRegistry() { setup(); verifyExceptions( new ParseTester() { @@ -168,7 +176,8 @@ public class ParseExceptionsTest { }); } - @Test public void messageBuilder_mergeFrom_CodedInputStream() { + @Test + public void messageBuilder_mergeFrom_CodedInputStream() { setup(); verifyExceptions( new ParseTester() { @@ -179,7 +188,8 @@ public class ParseExceptionsTest { }); } - @Test public void messageBuilder_mergeFrom_CodedInputStreamAndExtensionRegistry() { + @Test + public void messageBuilder_mergeFrom_CodedInputStreamAndExtensionRegistry() { setup(); verifyExceptions( new ParseTester() { @@ -192,7 +202,8 @@ public class ParseExceptionsTest { }); } - @Test public void messageBuilder_mergeDelimitedFrom_InputStream() { + @Test + public void messageBuilder_mergeDelimitedFrom_InputStream() { setupDelimited(); verifyExceptions( new ParseTester() { @@ -205,7 +216,25 @@ public class ParseExceptionsTest { }); } - @Test public void messageBuilder_mergeDelimitedFrom_InputStreamAndExtensionRegistry() { + @Test + public void messageBuilder_mergeDelimitedFrom_InputStream_malformed() throws Exception { + byte[] body = new byte[80]; + CodedOutputStream cos = CodedOutputStream.newInstance(body); + cos.writeRawVarint32(90); // Greater than bytes in stream + cos.writeTag(DescriptorProto.ENUM_TYPE_FIELD_NUMBER, WireFormat.WIRETYPE_LENGTH_DELIMITED); + cos.writeRawVarint32(98); // Nested message with size larger than parent + cos.writeTag(1000, WireFormat.WIRETYPE_LENGTH_DELIMITED); + cos.writeRawVarint32(100); // Unknown field with size larger than parent + ByteArrayInputStream bais = new ByteArrayInputStream(body); + try { + DescriptorProto.parseDelimitedFrom(bais); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + @Test + public void messageBuilder_mergeDelimitedFrom_InputStreamAndExtensionRegistry() { setupDelimited(); verifyExceptions( new ParseTester() { @@ -221,7 +250,8 @@ public class ParseExceptionsTest { private void verifyExceptions(ParseTester parseTester) { // No exception try { - assertEquals(DescriptorProto.getDescriptor().toProto(), + assertEquals( + DescriptorProto.getDescriptor().toProto(), parseTester.parse(new ByteArrayInputStream(serializedProto))); } catch (IOException e) { fail("No exception expected: " + e); @@ -253,14 +283,16 @@ public class ParseExceptionsTest { return new FilterInputStream(i) { int count = 0; - @Override public int read() throws IOException { + @Override + public int read() throws IOException { if (count++ >= 50) { throw new IOException("I'm broken!"); } return super.read(); } - @Override public int read(byte b[], int off, int len) throws IOException { + @Override + public int read(byte[] b, int off, int len) throws IOException { if ((count += len) >= 50) { throw new IOException("I'm broken!"); } diff --git a/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java b/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java new file mode 100755 index 0000000000..eb2dc3dd24 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java @@ -0,0 +1,191 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.UnittestLite.TestAllTypesLite; +import com.google.protobuf.UnittestLite.TestPackedExtensionsLite; +import com.google.protobuf.UnittestLite.TestParsingMergeLite; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import junit.framework.TestCase; + +public class ParserLiteTest extends TestCase { + private void assertRoundTripEquals(MessageLite message, ExtensionRegistryLite registry) + throws Exception { + final byte[] data = message.toByteArray(); + final int offset = 20; + final int length = data.length; + final int padding = 30; + Parser parser = message.getParserForType(); + assertEquals(message, parser.parseFrom(data, registry)); + assertEquals( + message, + parser.parseFrom(generatePaddingArray(data, offset, padding), offset, length, registry)); + assertEquals(message, parser.parseFrom(message.toByteString(), registry)); + assertEquals(message, parser.parseFrom(new ByteArrayInputStream(data), registry)); + assertEquals(message, parser.parseFrom(CodedInputStream.newInstance(data), registry)); + assertEquals( + message, parser.parseFrom(message.toByteString().asReadOnlyByteBuffer(), registry)); + } + + @SuppressWarnings("unchecked") + private void assertRoundTripEquals(MessageLite message) throws Exception { + final byte[] data = message.toByteArray(); + final int offset = 20; + final int length = data.length; + final int padding = 30; + + Parser parser = (Parser) message.getParserForType(); + assertEquals(message, parser.parseFrom(data)); + assertEquals( + message, parser.parseFrom(generatePaddingArray(data, offset, padding), offset, length)); + assertEquals(message, parser.parseFrom(message.toByteString())); + assertEquals(message, parser.parseFrom(new ByteArrayInputStream(data))); + assertEquals(message, parser.parseFrom(CodedInputStream.newInstance(data))); + assertEquals(message, parser.parseFrom(message.toByteString().asReadOnlyByteBuffer())); + } + + private byte[] generatePaddingArray(byte[] data, int offset, int padding) { + byte[] result = new byte[offset + data.length + padding]; + System.arraycopy(data, 0, result, offset, data.length); + return result; + } + + public void testParseExtensionsLite() throws Exception { + assertRoundTripEquals( + TestUtilLite.getAllLiteExtensionsSet(), TestUtilLite.getExtensionRegistryLite()); + } + + public void testParsePacked() throws Exception { + assertRoundTripEquals(TestUtil.getPackedSet()); + assertRoundTripEquals(TestUtil.getPackedExtensionsSet(), TestUtil.getExtensionRegistry()); + } + + public void testParsePackedLite() throws Exception { + assertRoundTripEquals( + TestUtilLite.getLitePackedExtensionsSet(), TestUtilLite.getExtensionRegistryLite()); + } + + public void testParseDelimitedToLite() throws Exception { + // Write MessageLite with packed extension fields. + TestPackedExtensionsLite packedMessage = TestUtilLite.getLitePackedExtensionsSet(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + packedMessage.writeDelimitedTo(output); + packedMessage.writeDelimitedTo(output); + + InputStream input = new ByteArrayInputStream(output.toByteArray()); + assertEquals( + packedMessage, + packedMessage + .getParserForType() + .parseDelimitedFrom(input, TestUtilLite.getExtensionRegistryLite())); + assertEquals( + packedMessage, + packedMessage + .getParserForType() + .parseDelimitedFrom(input, TestUtilLite.getExtensionRegistryLite())); + } + + /** Helper method for {@link #testParsingMergeLite()}. */ + private void assertMessageMerged(TestAllTypesLite allTypes) throws Exception { + assertEquals(3, allTypes.getOptionalInt32()); + assertEquals(2, allTypes.getOptionalInt64()); + assertEquals("hello", allTypes.getOptionalString()); + } + + public void testParsingMergeLite() throws Exception { + // Build messages. + TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder(); + TestAllTypesLite msg1 = builder.setOptionalInt32(1).build(); + builder.clear(); + TestAllTypesLite msg2 = builder.setOptionalInt64(2).build(); + builder.clear(); + TestAllTypesLite msg3 = builder.setOptionalInt32(3).setOptionalString("hello").build(); + + // Build groups. + TestParsingMergeLite.RepeatedFieldsGenerator.Group1 optionalG1 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg1).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group1 optionalG2 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg2).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group1 optionalG3 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg3).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group2 repeatedG1 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg1).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group2 repeatedG2 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg2).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group2 repeatedG3 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg3).build(); + + // Assign and serialize RepeatedFieldsGenerator. + ByteString data = + TestParsingMergeLite.RepeatedFieldsGenerator.newBuilder() + .addField1(msg1) + .addField1(msg2) + .addField1(msg3) + .addField2(msg1) + .addField2(msg2) + .addField2(msg3) + .addField3(msg1) + .addField3(msg2) + .addField3(msg3) + .addGroup1(optionalG1) + .addGroup1(optionalG2) + .addGroup1(optionalG3) + .addGroup2(repeatedG1) + .addGroup2(repeatedG2) + .addGroup2(repeatedG3) + .addExt1(msg1) + .addExt1(msg2) + .addExt1(msg3) + .addExt2(msg1) + .addExt2(msg2) + .addExt2(msg3) + .build() + .toByteString(); + + // Parse TestParsingMergeLite. + ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); + UnittestLite.registerAllExtensions(registry); + TestParsingMergeLite parsingMerge = TestParsingMergeLite.parser().parseFrom(data, registry); + + // Required and optional fields should be merged. + assertMessageMerged(parsingMerge.getRequiredAllTypes()); + assertMessageMerged(parsingMerge.getOptionalAllTypes()); + assertMessageMerged(parsingMerge.getOptionalGroup().getOptionalGroupAllTypes()); + assertMessageMerged(parsingMerge.getExtension(TestParsingMergeLite.optionalExt)); + + // Repeated fields should not be merged. + assertEquals(3, parsingMerge.getRepeatedAllTypesCount()); + assertEquals(3, parsingMerge.getRepeatedGroupCount()); + assertEquals(3, parsingMerge.getExtensionCount(TestParsingMergeLite.repeatedExt)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ParserTest.java b/java/core/src/test/java/com/google/protobuf/ParserTest.java index 1e89111283..983caec769 100644 --- a/java/core/src/test/java/com/google/protobuf/ParserTest.java +++ b/java/core/src/test/java/com/google/protobuf/ParserTest.java @@ -58,8 +58,7 @@ public class ParserTest extends TestCase { } } - private void assertRoundTripEquals(MessageLite message, - ExtensionRegistryLite registry) + private void assertRoundTripEquals(MessageLite message, ExtensionRegistryLite registry) throws Exception { final byte[] data = message.toByteArray(); final int offset = 20; @@ -67,15 +66,12 @@ public class ParserTest extends TestCase { final int padding = 30; Parser parser = message.getParserForType(); assertMessageEquals(message, parser.parseFrom(data, registry)); - assertMessageEquals(message, parser.parseFrom( - generatePaddingArray(data, offset, padding), - offset, length, registry)); - assertMessageEquals(message, parser.parseFrom( - message.toByteString(), registry)); - assertMessageEquals(message, parser.parseFrom( - new ByteArrayInputStream(data), registry)); - assertMessageEquals(message, parser.parseFrom( - CodedInputStream.newInstance(data), registry)); + assertMessageEquals( + message, + parser.parseFrom(generatePaddingArray(data, offset, padding), offset, length, registry)); + assertMessageEquals(message, parser.parseFrom(message.toByteString(), registry)); + assertMessageEquals(message, parser.parseFrom(new ByteArrayInputStream(data), registry)); + assertMessageEquals(message, parser.parseFrom(CodedInputStream.newInstance(data), registry)); assertMessageEquals( message, parser.parseFrom(message.toByteString().asReadOnlyByteBuffer(), registry)); } @@ -87,23 +83,17 @@ public class ParserTest extends TestCase { final int length = data.length; final int padding = 30; - Parser parser = - (Parser) message.getParserForType(); + Parser parser = (Parser) message.getParserForType(); assertMessageEquals(message, parser.parseFrom(data)); - assertMessageEquals(message, parser.parseFrom( - generatePaddingArray(data, offset, padding), - offset, length)); + assertMessageEquals( + message, parser.parseFrom(generatePaddingArray(data, offset, padding), offset, length)); assertMessageEquals(message, parser.parseFrom(message.toByteString())); - assertMessageEquals(message, parser.parseFrom( - new ByteArrayInputStream(data))); - assertMessageEquals(message, parser.parseFrom( - CodedInputStream.newInstance(data))); + assertMessageEquals(message, parser.parseFrom(new ByteArrayInputStream(data))); + assertMessageEquals(message, parser.parseFrom(CodedInputStream.newInstance(data))); assertMessageEquals(message, parser.parseFrom(message.toByteString().asReadOnlyByteBuffer())); } - private void assertMessageEquals( - MessageLite expected, MessageLite actual) - throws Exception { + private void assertMessageEquals(MessageLite expected, MessageLite actual) throws Exception { if (expected instanceof Message) { assertEquals(expected, actual); } else { @@ -126,20 +116,16 @@ public class ParserTest extends TestCase { assertParsePartial(TestRequired.parser(), TestRequired.newBuilder().setA(1).buildPartial()); } - private void assertParsePartial( - Parser parser, T partialMessage) throws Exception { - final String errorString = - "Should throw exceptions when the parsed message isn't initialized."; + private void assertParsePartial(Parser parser, T partialMessage) + throws Exception { + final String errorString = "Should throw exceptions when the parsed message isn't initialized."; // parsePartialFrom should pass. byte[] data = partialMessage.toByteArray(); assertEquals(partialMessage, parser.parsePartialFrom(data)); - assertEquals(partialMessage, parser.parsePartialFrom( - partialMessage.toByteString())); - assertEquals(partialMessage, parser.parsePartialFrom( - new ByteArrayInputStream(data))); - assertEquals(partialMessage, parser.parsePartialFrom( - CodedInputStream.newInstance(data))); + assertEquals(partialMessage, parser.parsePartialFrom(partialMessage.toByteString())); + assertEquals(partialMessage, parser.parsePartialFrom(new ByteArrayInputStream(data))); + assertEquals(partialMessage, parser.parsePartialFrom(CodedInputStream.newInstance(data))); // parseFrom(ByteArray) try { @@ -167,8 +153,7 @@ public class ParserTest extends TestCase { // parseFrom(CodedInputStream) try { - parser.parseFrom(CodedInputStream.newInstance( - partialMessage.toByteArray())); + parser.parseFrom(CodedInputStream.newInstance(partialMessage.toByteArray())); fail(errorString); } catch (IOException e) { // pass. @@ -176,14 +161,12 @@ public class ParserTest extends TestCase { } public void testParseExtensions() throws Exception { - assertRoundTripEquals(TestUtil.getAllExtensionsSet(), - TestUtil.getExtensionRegistry()); + assertRoundTripEquals(TestUtil.getAllExtensionsSet(), TestUtil.getExtensionRegistry()); } public void testParsePacked() throws Exception { assertRoundTripEquals(TestUtil.getPackedSet()); - assertRoundTripEquals(TestUtil.getPackedExtensionsSet(), - TestUtil.getExtensionRegistry()); + assertRoundTripEquals(TestUtil.getPackedExtensionsSet(), TestUtil.getExtensionRegistry()); } public void testParseDelimitedTo() throws Exception { @@ -202,9 +185,7 @@ public class ParserTest extends TestCase { // All fields will be treated as unknown fields in emptyMessage. TestEmptyMessage emptyMessage = TestEmptyMessage.parser().parseFrom(TestUtil.getAllSet().toByteString()); - assertEquals( - TestUtil.getAllSet().toByteString(), - emptyMessage.toByteString()); + assertEquals(TestUtil.getAllSet().toByteString(), emptyMessage.toByteString()); } @@ -212,7 +193,8 @@ public class ParserTest extends TestCase { TestOptimizedForSize.Builder builder = TestOptimizedForSize.newBuilder(); builder.setI(12).setMsg(ForeignMessage.newBuilder().setC(34).build()); builder.setExtension(TestOptimizedForSize.testExtension, 56); - builder.setExtension(TestOptimizedForSize.testExtension2, + builder.setExtension( + TestOptimizedForSize.testExtension2, TestRequiredOptimizedForSize.newBuilder().setX(78).build()); TestOptimizedForSize message = builder.build(); @@ -222,9 +204,8 @@ public class ParserTest extends TestCase { assertRoundTripEquals(message, registry); } - /** Helper method for {@link #testParsingMerge()}.*/ - private void assertMessageMerged(TestAllTypes allTypes) - throws Exception { + /** Helper method for {@link #testParsingMerge()}. */ + private void assertMessageMerged(TestAllTypes allTypes) throws Exception { assertEquals(3, allTypes.getOptionalInt32()); assertEquals(2, allTypes.getOptionalInt64()); assertEquals("hello", allTypes.getOptionalString()); @@ -237,39 +218,48 @@ public class ParserTest extends TestCase { builder.clear(); TestAllTypes msg2 = builder.setOptionalInt64(2).build(); builder.clear(); - TestAllTypes msg3 = builder.setOptionalInt32(3) - .setOptionalString("hello").build(); + TestAllTypes msg3 = builder.setOptionalInt32(3).setOptionalString("hello").build(); // Build groups. TestParsingMerge.RepeatedFieldsGenerator.Group1 optionalG1 = - TestParsingMerge.RepeatedFieldsGenerator.Group1.newBuilder() - .setField1(msg1).build(); + TestParsingMerge.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg1).build(); TestParsingMerge.RepeatedFieldsGenerator.Group1 optionalG2 = - TestParsingMerge.RepeatedFieldsGenerator.Group1.newBuilder() - .setField1(msg2).build(); + TestParsingMerge.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg2).build(); TestParsingMerge.RepeatedFieldsGenerator.Group1 optionalG3 = - TestParsingMerge.RepeatedFieldsGenerator.Group1.newBuilder() - .setField1(msg3).build(); + TestParsingMerge.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg3).build(); TestParsingMerge.RepeatedFieldsGenerator.Group2 repeatedG1 = - TestParsingMerge.RepeatedFieldsGenerator.Group2.newBuilder() - .setField1(msg1).build(); + TestParsingMerge.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg1).build(); TestParsingMerge.RepeatedFieldsGenerator.Group2 repeatedG2 = - TestParsingMerge.RepeatedFieldsGenerator.Group2.newBuilder() - .setField1(msg2).build(); + TestParsingMerge.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg2).build(); TestParsingMerge.RepeatedFieldsGenerator.Group2 repeatedG3 = - TestParsingMerge.RepeatedFieldsGenerator.Group2.newBuilder() - .setField1(msg3).build(); + TestParsingMerge.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg3).build(); // Assign and serialize RepeatedFieldsGenerator. - ByteString data = TestParsingMerge.RepeatedFieldsGenerator.newBuilder() - .addField1(msg1).addField1(msg2).addField1(msg3) - .addField2(msg1).addField2(msg2).addField2(msg3) - .addField3(msg1).addField3(msg2).addField3(msg3) - .addGroup1(optionalG1).addGroup1(optionalG2).addGroup1(optionalG3) - .addGroup2(repeatedG1).addGroup2(repeatedG2).addGroup2(repeatedG3) - .addExt1(msg1).addExt1(msg2).addExt1(msg3) - .addExt2(msg1).addExt2(msg2).addExt2(msg3) - .build().toByteString(); + ByteString data = + TestParsingMerge.RepeatedFieldsGenerator.newBuilder() + .addField1(msg1) + .addField1(msg2) + .addField1(msg3) + .addField2(msg1) + .addField2(msg2) + .addField2(msg3) + .addField3(msg1) + .addField3(msg2) + .addField3(msg3) + .addGroup1(optionalG1) + .addGroup1(optionalG2) + .addGroup1(optionalG3) + .addGroup2(repeatedG1) + .addGroup2(repeatedG2) + .addGroup2(repeatedG3) + .addExt1(msg1) + .addExt1(msg2) + .addExt1(msg3) + .addExt2(msg1) + .addExt2(msg2) + .addExt2(msg3) + .build() + .toByteString(); // Parse TestParsingMerge. ExtensionRegistry registry = ExtensionRegistry.newInstance(); @@ -279,21 +269,18 @@ public class ParserTest extends TestCase { // Required and optional fields should be merged. assertMessageMerged(parsingMerge.getRequiredAllTypes()); assertMessageMerged(parsingMerge.getOptionalAllTypes()); - assertMessageMerged( - parsingMerge.getOptionalGroup().getOptionalGroupAllTypes()); - assertMessageMerged(parsingMerge.getExtension( - TestParsingMerge.optionalExt)); + assertMessageMerged(parsingMerge.getOptionalGroup().getOptionalGroupAllTypes()); + assertMessageMerged(parsingMerge.getExtension(TestParsingMerge.optionalExt)); // Repeated fields should not be merged. assertEquals(3, parsingMerge.getRepeatedAllTypesCount()); assertEquals(3, parsingMerge.getRepeatedGroupCount()); - assertEquals(3, parsingMerge.getExtensionCount( - TestParsingMerge.repeatedExt)); + assertEquals(3, parsingMerge.getExtensionCount(TestParsingMerge.repeatedExt)); } public void testParseDelimitedFrom_firstByteInterrupted_preservesCause() { try { - TestUtil.getAllSet().parseDelimitedFrom( + TestAllTypes.parseDelimitedFrom( new InputStream() { @Override public int read() throws IOException { @@ -308,7 +295,7 @@ public class ParserTest extends TestCase { public void testParseDelimitedFrom_secondByteInterrupted_preservesCause() { try { - TestUtil.getAllSet().parseDelimitedFrom( + TestAllTypes.parseDelimitedFrom( new InputStream() { private int i; diff --git a/java/core/src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java new file mode 100755 index 0000000000..dfda4b3cbd --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java @@ -0,0 +1,171 @@ +// 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. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import com.google.protobuf.testing.Proto2Testing; +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.TestEnum; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto2ExtensionLookupSchemaTest { + private byte[] data; + private ExtensionRegistry extensionRegistry; + + @Before + public void setup() { + TestSchemas.registerGenericProto2Schemas(); + + Protobuf.getInstance().schemaFor(Proto2MessageWithExtensions.class); + data = new Proto2MessageFactory(10, 20, 1, 1).newMessage().toByteArray(); + extensionRegistry = ExtensionRegistry.newInstance(); + Proto2Testing.registerAllExtensions(extensionRegistry); + } + + @Test + public void testExtensions() throws Exception { + Proto2MessageWithExtensions base = + Proto2MessageWithExtensions.parseFrom(data, extensionRegistry); + + Proto2MessageWithExtensions message = + ExperimentalSerializationUtil.fromByteArray( + data, Proto2MessageWithExtensions.class, extensionRegistry); + assertEquals(base, message); + + Proto2MessageWithExtensions roundtripMessage = + ExperimentalSerializationUtil.fromByteArray( + ExperimentalSerializationUtil.toByteArray(message), + Proto2MessageWithExtensions.class, + extensionRegistry); + assertEquals(base, roundtripMessage); + } + + @Test + public void testUnknownEnum() throws Exception { + // Use unknown fields to hold invalid enum values. + UnknownFieldSetLite unknowns = UnknownFieldSetLite.newInstance(); + final int outOfRange = 1000; + assertNull(TestEnum.forNumber(outOfRange)); + unknowns.storeField( + WireFormat.makeTag(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.ONE_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.TWO_VALUE); + + { + // Construct a packed enum list. + int packedSize = + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE) + + CodedOutputStream.computeUInt32SizeNoTag(outOfRange) + + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE); + ByteString.CodedBuilder packedBuilder = ByteString.newCodedBuilder(packedSize); + CodedOutputStream packedOut = packedBuilder.getCodedOutput(); + packedOut.writeEnumNoTag(TestEnum.ONE_VALUE); + packedOut.writeEnumNoTag(outOfRange); + packedOut.writeEnumNoTag(TestEnum.TWO_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER, + WireFormat.WIRETYPE_LENGTH_DELIMITED), + packedBuilder.build()); + } + int size = unknowns.getSerializedSize(); + byte[] output = new byte[size]; + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + unknowns.writeTo(codedOutput); + codedOutput.flush(); + + Proto2MessageWithExtensions parsed = + ExperimentalSerializationUtil.fromByteArray( + output, Proto2MessageWithExtensions.class, extensionRegistry); + assertFalse( + "out-of-range singular enum should not be in message", + parsed.hasExtension(Proto2Testing.fieldEnum13)); + { + List singularEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, singularEnum.size()); + assertEquals((Long) (long) outOfRange, singularEnum.get(0)); + } + { + List repeatedEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, repeatedEnum.size()); + assertEquals((Long) (long) outOfRange, repeatedEnum.get(0)); + } + { + List packedRepeatedEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, packedRepeatedEnum.size()); + assertEquals((Long) (long) outOfRange, packedRepeatedEnum.get(0)); + } + assertEquals( + "out-of-range repeated enum should not be in message", + 2, + parsed.getExtension(Proto2Testing.fieldEnumList30).size()); + assertEquals(TestEnum.ONE, parsed.getExtension(Proto2Testing.fieldEnumList30, 0)); + assertEquals(TestEnum.TWO, parsed.getExtension(Proto2Testing.fieldEnumList30, 1)); + assertEquals( + "out-of-range packed repeated enum should not be in message", + 2, + parsed.getExtension(Proto2Testing.fieldEnumListPacked44).size()); + assertEquals(TestEnum.ONE, parsed.getExtension(Proto2Testing.fieldEnumListPacked44, 0)); + assertEquals(TestEnum.TWO, parsed.getExtension(Proto2Testing.fieldEnumListPacked44, 1)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java new file mode 100755 index 0000000000..8242f847ee --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java @@ -0,0 +1,49 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto2LiteSchemaTest extends AbstractProto2LiteSchemaTest { + + @Override + protected Schema schema() { + return TestSchemasLite.genericProto2LiteSchema; + } + + @Override + protected void registerSchemas() { + TestSchemasLite.registerGenericProto2LiteSchemas(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2MessageFactory.java b/java/core/src/test/java/com/google/protobuf/Proto2MessageFactory.java new file mode 100755 index 0000000000..b5c61ed998 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2MessageFactory.java @@ -0,0 +1,555 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Creates instances of {@link Proto2Message} based on the tree configuration. */ +public final class Proto2MessageFactory implements ExperimentalMessageFactory { + private final int numRepeatedFields; + private final int branchingFactor; + private final Proto2MessageFactory nextLevel; + private final ExperimentalTestDataProvider data; + + public Proto2MessageFactory( + int numRepeatedFields, int stringLength, int branchingFactor, int treeDepth) { + this( + new ExperimentalTestDataProvider(stringLength), + numRepeatedFields, + branchingFactor, + treeDepth); + } + + private Proto2MessageFactory( + ExperimentalTestDataProvider data, + int numRepeatedFields, + int branchingFactor, + int treeDepth) { + this.numRepeatedFields = numRepeatedFields; + this.branchingFactor = branchingFactor; + this.data = data; + if (treeDepth > 0) { + nextLevel = new Proto2MessageFactory(data, numRepeatedFields, branchingFactor, treeDepth - 1); + } else { + nextLevel = null; + } + } + + @Override + public ExperimentalTestDataProvider dataProvider() { + return data; + } + + @Override + public Proto2Message newMessage() { + Proto2Message.Builder builder = Proto2Message.newBuilder(); + builder.setFieldDouble1(data.getDouble()); + builder.setFieldFloat2(data.getFloat()); + builder.setFieldInt643(data.getLong()); + builder.setFieldUint644(data.getLong()); + builder.setFieldInt325(data.getInt()); + builder.setFieldFixed646(data.getLong()); + builder.setFieldFixed327(data.getInt()); + builder.setFieldBool8(data.getBool()); + builder.setFieldString9(data.getString()); + // We don't populate the message field. Instead we apply the branching factor to the + // repeated message field below. + builder.setFieldBytes11(data.getBytes()); + builder.setFieldUint3212(data.getInt()); + builder.setFieldEnum13(Proto2Message.TestEnum.forNumber(data.getEnum())); + builder.setFieldSfixed3214(data.getInt()); + builder.setFieldSfixed6415(data.getLong()); + builder.setFieldSint3216(data.getInt()); + builder.setFieldSint6417(data.getLong()); + + for (int i = 0; i < numRepeatedFields; ++i) { + builder.addFieldDoubleList18(data.getDouble()); + builder.addFieldFloatList19(data.getFloat()); + builder.addFieldInt64List20(data.getLong()); + builder.addFieldUint64List21(data.getLong()); + builder.addFieldInt32List22(data.getInt()); + builder.addFieldFixed64List23(data.getLong()); + builder.addFieldFixed32List24(data.getInt()); + builder.addFieldBoolList25(data.getBool()); + builder.addFieldStringList26(data.getString()); + // Repeated message field is controlled by the branching factor below. + builder.addFieldBytesList28(data.getBytes()); + builder.addFieldUint32List29(data.getInt()); + builder.addFieldEnumList30(Proto2Message.TestEnum.forNumber(data.getEnum())); + builder.addFieldSfixed32List31(data.getInt()); + builder.addFieldSfixed64List32(data.getLong()); + builder.addFieldSint32List33(data.getInt()); + builder.addFieldSint64List34(data.getLong()); + + builder.addFieldDoubleListPacked35(data.getDouble()); + builder.addFieldFloatListPacked36(data.getFloat()); + builder.addFieldInt64ListPacked37(data.getLong()); + builder.addFieldUint64ListPacked38(data.getLong()); + builder.addFieldInt32ListPacked39(data.getInt()); + builder.addFieldFixed64ListPacked40(data.getLong()); + builder.addFieldFixed32ListPacked41(data.getInt()); + builder.addFieldBoolListPacked42(data.getBool()); + builder.addFieldUint32ListPacked43(data.getInt()); + builder.addFieldEnumListPacked44(Proto2Message.TestEnum.forNumber(data.getEnum())); + builder.addFieldSfixed32ListPacked45(data.getInt()); + builder.addFieldSfixed64ListPacked46(data.getLong()); + builder.addFieldSint32ListPacked47(data.getInt()); + builder.addFieldSint64ListPacked48(data.getLong()); + } + + builder.setFieldGroup49(Proto2Message.FieldGroup49.newBuilder().setFieldInt3250(data.getInt())); + + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldGroupList51( + Proto2Message.FieldGroupList51.newBuilder().setFieldInt3252(data.getInt())); + } + + // Set all required fields. + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + + // Handle the branching factor. + if (nextLevel != null) { + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldMessageList27(nextLevel.newMessage()); + } + } + + return builder.build(); + } + + private interface MapValueProvider { + public T getValue(); + } + + private final MapValueProvider integerProvider = + new MapValueProvider() { + @Override + public Integer getValue() { + return data.getInt(); + } + }; + private final MapValueProvider longProvider = + new MapValueProvider() { + @Override + public Long getValue() { + return data.getLong(); + } + }; + private final MapValueProvider stringProvider = + new MapValueProvider() { + @Override + public String getValue() { + return data.getString(); + } + }; + private final MapValueProvider bytesProvider = + new MapValueProvider() { + @Override + public ByteString getValue() { + return data.getBytes(); + } + }; + private final MapValueProvider booleanProvider = + new MapValueProvider() { + @Override + public Boolean getValue() { + return data.getBool(); + } + }; + private final MapValueProvider floatProvider = + new MapValueProvider() { + @Override + public Float getValue() { + return data.getFloat(); + } + }; + private final MapValueProvider doubleProvider = + new MapValueProvider() { + @Override + public Double getValue() { + return data.getDouble(); + } + }; + private final MapValueProvider messageProvider = + new MapValueProvider() { + @Override + public Proto2Message getValue() { + return newMessage(); + } + }; + private final MapValueProvider enumProvider = + new MapValueProvider() { + @Override + public Proto2Message.TestEnum getValue() { + return Proto2Message.TestEnum.forNumber(data.getEnum()); + } + }; + + private Map populateIntegerMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getInt(), provider.getValue()); + } + return map; + } + + private Map populateLongMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getLong(), provider.getValue()); + } + return map; + } + + private Map populateStringMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getString(), provider.getValue()); + } + return map; + } + + private Map populateBooleanMap(MapValueProvider provider) { + Map map = new HashMap<>(); + map.put(false, provider.getValue()); + map.put(true, provider.getValue()); + return map; + } + + public Proto2MessageWithMaps newMessageWithMaps() { + Proto2MessageWithMaps.Builder builder = Proto2MessageWithMaps.newBuilder(); + + builder.putAllFieldMapBoolBool1(populateBooleanMap(booleanProvider)); + builder.putAllFieldMapBoolBytes2(populateBooleanMap(bytesProvider)); + builder.putAllFieldMapBoolDouble3(populateBooleanMap(doubleProvider)); + builder.putAllFieldMapBoolEnum4(populateBooleanMap(enumProvider)); + builder.putAllFieldMapBoolFixed325(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolFixed646(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolFloat7(populateBooleanMap(floatProvider)); + builder.putAllFieldMapBoolInt328(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolInt649(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolMessage10(populateBooleanMap(messageProvider)); + builder.putAllFieldMapBoolSfixed3211(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSfixed6412(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolSint3213(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSint6414(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolString15(populateBooleanMap(stringProvider)); + builder.putAllFieldMapBoolUint3216(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolUint6417(populateBooleanMap(longProvider)); + builder.putAllFieldMapFixed32Bool18(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapFixed32Bytes19(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapFixed32Double20(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapFixed32Enum21(populateIntegerMap(enumProvider)); + builder.putAllFieldMapFixed32Fixed3222(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Fixed6423(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Float24(populateIntegerMap(floatProvider)); + builder.putAllFieldMapFixed32Int3225(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Int6426(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Message27(populateIntegerMap(messageProvider)); + builder.putAllFieldMapFixed32Sfixed3228(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sfixed6429(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Sint3230(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sint6431(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32String32(populateIntegerMap(stringProvider)); + builder.putAllFieldMapFixed32Uint3233(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Uint6434(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed64Bool35(populateLongMap(booleanProvider)); + builder.putAllFieldMapFixed64Bytes36(populateLongMap(bytesProvider)); + builder.putAllFieldMapFixed64Double37(populateLongMap(doubleProvider)); + builder.putAllFieldMapFixed64Enum38(populateLongMap(enumProvider)); + builder.putAllFieldMapFixed64Fixed3239(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Fixed6440(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Float41(populateLongMap(floatProvider)); + builder.putAllFieldMapFixed64Int3242(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Int6443(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Message44(populateLongMap(messageProvider)); + builder.putAllFieldMapFixed64Sfixed3245(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sfixed6446(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Sint3247(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sint6448(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64String49(populateLongMap(stringProvider)); + builder.putAllFieldMapFixed64Uint3250(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Uint6451(populateLongMap(longProvider)); + builder.putAllFieldMapInt32Bool52(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapInt32Bytes53(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapInt32Double54(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapInt32Enum55(populateIntegerMap(enumProvider)); + builder.putAllFieldMapInt32Fixed3256(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Fixed6457(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Float58(populateIntegerMap(floatProvider)); + builder.putAllFieldMapInt32Int3259(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Int6460(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Message61(populateIntegerMap(messageProvider)); + builder.putAllFieldMapInt32Sfixed3262(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sfixed6463(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Sint3264(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sint6465(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32String66(populateIntegerMap(stringProvider)); + builder.putAllFieldMapInt32Uint3267(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Uint6468(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt64Bool69(populateLongMap(booleanProvider)); + builder.putAllFieldMapInt64Bytes70(populateLongMap(bytesProvider)); + builder.putAllFieldMapInt64Double71(populateLongMap(doubleProvider)); + builder.putAllFieldMapInt64Enum72(populateLongMap(enumProvider)); + builder.putAllFieldMapInt64Fixed3273(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Fixed6474(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Float75(populateLongMap(floatProvider)); + builder.putAllFieldMapInt64Int3276(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Int6477(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Message78(populateLongMap(messageProvider)); + builder.putAllFieldMapInt64Sfixed3279(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sfixed6480(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Sint3281(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sint6482(populateLongMap(longProvider)); + builder.putAllFieldMapInt64String83(populateLongMap(stringProvider)); + builder.putAllFieldMapInt64Uint3284(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Uint6485(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed32Bool86(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSfixed32Bytes87(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSfixed32Double88(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSfixed32Enum89(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSfixed32Fixed3290(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Fixed6491(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Float92(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSfixed32Int3293(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Int6494(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Message95(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSfixed32Sfixed3296(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sfixed6497(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Sint3298(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sint6499(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32String100(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSfixed32Uint32101(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Uint64102(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed64Bool103(populateLongMap(booleanProvider)); + builder.putAllFieldMapSfixed64Bytes104(populateLongMap(bytesProvider)); + builder.putAllFieldMapSfixed64Double105(populateLongMap(doubleProvider)); + builder.putAllFieldMapSfixed64Enum106(populateLongMap(enumProvider)); + builder.putAllFieldMapSfixed64Fixed32107(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Fixed64108(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Float109(populateLongMap(floatProvider)); + builder.putAllFieldMapSfixed64Int32110(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Int64111(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Message112(populateLongMap(messageProvider)); + builder.putAllFieldMapSfixed64Sfixed32113(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sfixed64114(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Sint32115(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sint64116(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64String117(populateLongMap(stringProvider)); + builder.putAllFieldMapSfixed64Uint32118(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Uint64119(populateLongMap(longProvider)); + builder.putAllFieldMapSint32Bool120(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSint32Bytes121(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSint32Double122(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSint32Enum123(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSint32Fixed32124(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Fixed64125(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Float126(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSint32Int32127(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Int64128(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Message129(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSint32Sfixed32130(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sfixed64131(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Sint32132(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sint64133(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32String134(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSint32Uint32135(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Uint64136(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint64Bool137(populateLongMap(booleanProvider)); + builder.putAllFieldMapSint64Bytes138(populateLongMap(bytesProvider)); + builder.putAllFieldMapSint64Double139(populateLongMap(doubleProvider)); + builder.putAllFieldMapSint64Enum140(populateLongMap(enumProvider)); + builder.putAllFieldMapSint64Fixed32141(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Fixed64142(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Float143(populateLongMap(floatProvider)); + builder.putAllFieldMapSint64Int32144(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Int64145(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Message146(populateLongMap(messageProvider)); + builder.putAllFieldMapSint64Sfixed32147(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sfixed64148(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Sint32149(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sint64150(populateLongMap(longProvider)); + builder.putAllFieldMapSint64String151(populateLongMap(stringProvider)); + builder.putAllFieldMapSint64Uint32152(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Uint64153(populateLongMap(longProvider)); + builder.putAllFieldMapStringBool154(populateStringMap(booleanProvider)); + builder.putAllFieldMapStringBytes155(populateStringMap(bytesProvider)); + builder.putAllFieldMapStringDouble156(populateStringMap(doubleProvider)); + builder.putAllFieldMapStringEnum157(populateStringMap(enumProvider)); + builder.putAllFieldMapStringFixed32158(populateStringMap(integerProvider)); + builder.putAllFieldMapStringFixed64159(populateStringMap(longProvider)); + builder.putAllFieldMapStringFloat160(populateStringMap(floatProvider)); + builder.putAllFieldMapStringInt32161(populateStringMap(integerProvider)); + builder.putAllFieldMapStringInt64162(populateStringMap(longProvider)); + builder.putAllFieldMapStringMessage163(populateStringMap(messageProvider)); + builder.putAllFieldMapStringSfixed32164(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSfixed64165(populateStringMap(longProvider)); + builder.putAllFieldMapStringSint32166(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSint64167(populateStringMap(longProvider)); + builder.putAllFieldMapStringString168(populateStringMap(stringProvider)); + builder.putAllFieldMapStringUint32169(populateStringMap(integerProvider)); + builder.putAllFieldMapStringUint64170(populateStringMap(longProvider)); + builder.putAllFieldMapUint32Bool171(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapUint32Bytes172(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapUint32Double173(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapUint32Enum174(populateIntegerMap(enumProvider)); + builder.putAllFieldMapUint32Fixed32175(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Fixed64176(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Float177(populateIntegerMap(floatProvider)); + builder.putAllFieldMapUint32Int32178(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Int64179(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Message180(populateIntegerMap(messageProvider)); + builder.putAllFieldMapUint32Sfixed32181(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sfixed64182(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Sint32183(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sint64184(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32String185(populateIntegerMap(stringProvider)); + builder.putAllFieldMapUint32Uint32186(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Uint64187(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint64Bool188(populateLongMap(booleanProvider)); + builder.putAllFieldMapUint64Bytes189(populateLongMap(bytesProvider)); + builder.putAllFieldMapUint64Double190(populateLongMap(doubleProvider)); + builder.putAllFieldMapUint64Enum191(populateLongMap(enumProvider)); + builder.putAllFieldMapUint64Fixed32192(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Fixed64193(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Float194(populateLongMap(floatProvider)); + builder.putAllFieldMapUint64Int32195(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Int64196(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Message197(populateLongMap(messageProvider)); + builder.putAllFieldMapUint64Sfixed32198(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sfixed64199(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Sint32200(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sint64201(populateLongMap(longProvider)); + builder.putAllFieldMapUint64String202(populateLongMap(stringProvider)); + builder.putAllFieldMapUint64Uint32203(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Uint64204(populateLongMap(longProvider)); + + return builder.build(); + } + + public List newMessagesMissingRequiredFields() { + List results = new ArrayList<>(); + for (int i = 71; i <= 88; ++i) { + Proto2Message.Builder builder = Proto2Message.newBuilder(); + populateRequiredFields(builder, i); + results.add(builder.buildPartial()); + } + { + // A nested optional message field is missing required fields. + Proto2Message.Builder builder = Proto2Message.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.setFieldMessage10(Proto2Message.getDefaultInstance()); + results.add(builder.buildPartial()); + } + { + // A nested repeated message field is missing required fields. + Proto2Message.Builder builder = Proto2Message.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.addFieldMessageList27(Proto2Message.getDefaultInstance()); + results.add(builder.buildPartial()); + } + { + // A nested oneof message field is missing required fields. + Proto2Message.Builder builder = Proto2Message.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.setFieldMessage62(Proto2Message.getDefaultInstance()); + results.add(builder.buildPartial()); + } + return results; + } + + // 0 is not a valid field number so we use it to mean no fields are excluded. + private static final int INCLUDE_ALL_REQUIRED_FIELDS = 0; + + private void populateRequiredFields(Proto2Message.Builder builder, int excludedFieldNumber) { + if (excludedFieldNumber != 71) { + builder.setFieldRequiredDouble71(data.getDouble()); + } + if (excludedFieldNumber != 72) { + builder.setFieldRequiredFloat72(data.getFloat()); + } + if (excludedFieldNumber != 73) { + builder.setFieldRequiredInt6473(data.getLong()); + } + if (excludedFieldNumber != 74) { + builder.setFieldRequiredUint6474(data.getLong()); + } + if (excludedFieldNumber != 75) { + builder.setFieldRequiredInt3275(data.getInt()); + } + if (excludedFieldNumber != 76) { + builder.setFieldRequiredFixed6476(data.getLong()); + } + if (excludedFieldNumber != 77) { + builder.setFieldRequiredFixed3277(data.getInt()); + } + if (excludedFieldNumber != 78) { + builder.setFieldRequiredBool78(data.getBool()); + } + if (excludedFieldNumber != 79) { + builder.setFieldRequiredString79(data.getString()); + } + if (excludedFieldNumber != 80) { + builder.setFieldRequiredMessage80( + Proto2Message.RequiredNestedMessage.newBuilder().setValue(data.getInt())); + } + if (excludedFieldNumber != 81) { + builder.setFieldRequiredBytes81(data.getBytes()); + } + if (excludedFieldNumber != 82) { + builder.setFieldRequiredUint3282(data.getInt()); + } + if (excludedFieldNumber != 83) { + builder.setFieldRequiredEnum83(Proto2Message.TestEnum.forNumber(data.getEnum())); + } + if (excludedFieldNumber != 84) { + builder.setFieldRequiredSfixed3284(data.getInt()); + } + if (excludedFieldNumber != 85) { + builder.setFieldRequiredSfixed6485(data.getLong()); + } + if (excludedFieldNumber != 86) { + builder.setFieldRequiredSint3286(data.getInt()); + } + if (excludedFieldNumber != 87) { + builder.setFieldRequiredSint6487(data.getLong()); + } + if (excludedFieldNumber != 88) { + builder.setFieldRequiredGroup88( + Proto2Message.FieldRequiredGroup88.newBuilder().setFieldInt3289(data.getInt())); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2MessageInfoFactory.java b/java/core/src/test/java/com/google/protobuf/Proto2MessageInfoFactory.java new file mode 100755 index 0000000000..af671b22e4 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2MessageInfoFactory.java @@ -0,0 +1,892 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forFieldWithEnumVerifier; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forProto2OptionalField; +import static com.google.protobuf.FieldInfo.forProto2RequiredField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.testing.Proto2Testing; +import com.google.protobuf.testing.Proto2Testing.Proto2Empty; +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.FieldGroup49; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.FieldGroup69; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.FieldGroupList51; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.FieldRequiredGroup88; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.RequiredNestedMessage; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.TestEnum; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import java.lang.reflect.Field; + +/** A factory that generates a hard-coded message info for {@link Proto2Message}. */ +public final class Proto2MessageInfoFactory implements MessageInfoFactory { + private static final Proto2MessageInfoFactory INSTANCE = new Proto2MessageInfoFactory(); + + private Proto2MessageInfoFactory() {} + + public static Proto2MessageInfoFactory getInstance() { + return INSTANCE; + } + + @Override + public boolean isSupported(Class clazz) { + return true; + } + + @Override + public MessageInfo messageInfoFor(Class clazz) { + if (Proto2Message.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2Message(); + } else if (FieldGroup49.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroup49(); + } else if (FieldGroupList51.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroupList51(); + } else if (FieldGroup69.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroup69(); + } else if (FieldRequiredGroup88.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldRequiredGroup88(); + } else if (RequiredNestedMessage.class.isAssignableFrom(clazz)) { + return newMessageInfoForRequiredNestedMessage(); + } else if (Proto2Empty.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2Empty(); + } else if (Proto2MessageWithExtensions.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageWithExtensions(); + } else if (Proto2Testing.FieldGroup49.class.isAssignableFrom(clazz)) { + return newMessageInfoForExtensionFieldGroup49(); + } else if (Proto2Testing.FieldGroupList51.class.isAssignableFrom(clazz)) { + return newMessageInfoForExtensionFieldGroupList51(); + } else if (Proto2Testing.Proto2MessageWithMaps.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageWithMaps(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + /** + * Creates a new hard-coded info for {@link Proto2Message}. Each time this is called, we manually + * go through the entire process of what a message would do if it self-registered its own info, + * including looking up each field by name. This is done for benchmarking purposes, so that we get + * a more accurate representation of the time it takes to perform this process. + */ + private static StructuralMessageInfo newMessageInfoForProto2Message() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(50); + builder.withCheckInitialized( + new int[] { + 10, 27, 62, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + }); + lookupFieldsByName(builder); + return builder.build(); + } + + private static void lookupFieldsByName(StructuralMessageInfo.Builder builder) { + Field bitField0 = field("bitField0_"); + + builder.withDefaultInstance(Proto2Message.getDefaultInstance()); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField( + forProto2OptionalField( + field("fieldDouble1_"), 1, FieldType.DOUBLE, bitField0, 0x00000001, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldFloat2_"), 2, FieldType.FLOAT, bitField0, 0x00000002, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldInt643_"), 3, FieldType.INT64, bitField0, 0x00000004, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldUint644_"), 4, FieldType.UINT64, bitField0, 0x00000008, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldInt325_"), 5, FieldType.INT32, bitField0, 0x00000010, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldFixed646_"), 6, FieldType.FIXED64, bitField0, 0x00000020, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldFixed327_"), 7, FieldType.FIXED32, bitField0, 0x00000040, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldBool8_"), 8, FieldType.BOOL, bitField0, 0x00000080, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldString9_"), 9, FieldType.STRING, bitField0, 0x00000100, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldMessage10_"), 10, FieldType.MESSAGE, bitField0, 0x00000200, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldBytes11_"), 11, FieldType.BYTES, bitField0, 0x00000400, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldUint3212_"), 12, FieldType.UINT32, bitField0, 0x00000800, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldEnum13_"), + 13, + FieldType.ENUM, + bitField0, + 0x00001000, + false, + asVerifier(TestEnum.internalGetValueMap()))); + builder.withField( + forProto2OptionalField( + field("fieldSfixed3214_"), 14, FieldType.SFIXED32, bitField0, 0x00002000, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldSfixed6415_"), 15, FieldType.SFIXED64, bitField0, 0x00004000, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldSint3216_"), 16, FieldType.SINT32, bitField0, 0x00008000, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldSint6417_"), 17, FieldType.SINT64, bitField0, 0x00010000, false, null)); + builder.withField(forField(field("fieldDoubleList18_"), 18, FieldType.DOUBLE_LIST, false)); + builder.withField(forField(field("fieldFloatList19_"), 19, FieldType.FLOAT_LIST, false)); + builder.withField(forField(field("fieldInt64List20_"), 20, FieldType.INT64_LIST, false)); + builder.withField(forField(field("fieldUint64List21_"), 21, FieldType.UINT64_LIST, false)); + builder.withField(forField(field("fieldInt32List22_"), 22, FieldType.INT32_LIST, false)); + builder.withField(forField(field("fieldFixed64List23_"), 23, FieldType.FIXED64_LIST, false)); + builder.withField(forField(field("fieldFixed32List24_"), 24, FieldType.FIXED32_LIST, false)); + builder.withField(forField(field("fieldBoolList25_"), 25, FieldType.BOOL_LIST, false)); + builder.withField(forField(field("fieldStringList26_"), 26, FieldType.STRING_LIST, false)); + builder.withField( + forRepeatedMessageField( + field("fieldMessageList27_"), 27, FieldType.MESSAGE_LIST, Proto2Message.class)); + builder.withField(forField(field("fieldBytesList28_"), 28, FieldType.BYTES_LIST, false)); + builder.withField(forField(field("fieldUint32List29_"), 29, FieldType.UINT32_LIST, false)); + builder.withField( + forFieldWithEnumVerifier( + field("fieldEnumList30_"), + 30, + FieldType.ENUM_LIST, + asVerifier(TestEnum.internalGetValueMap()))); + builder.withField(forField(field("fieldSfixed32List31_"), 31, FieldType.SFIXED32_LIST, false)); + builder.withField(forField(field("fieldSfixed64List32_"), 32, FieldType.SFIXED64_LIST, false)); + builder.withField(forField(field("fieldSint32List33_"), 33, FieldType.SINT32_LIST, false)); + builder.withField(forField(field("fieldSint64List34_"), 34, FieldType.SINT64_LIST, false)); + builder.withField( + forField(field("fieldDoubleListPacked35_"), 35, FieldType.DOUBLE_LIST_PACKED, false)); + builder.withField( + forField(field("fieldFloatListPacked36_"), 36, FieldType.FLOAT_LIST_PACKED, false)); + builder.withField( + forField(field("fieldInt64ListPacked37_"), 37, FieldType.INT64_LIST_PACKED, false)); + builder.withField( + forField(field("fieldUint64ListPacked38_"), 38, FieldType.UINT64_LIST_PACKED, false)); + builder.withField( + forField(field("fieldInt32ListPacked39_"), 39, FieldType.INT32_LIST_PACKED, false)); + builder.withField( + forField(field("fieldFixed64ListPacked40_"), 40, FieldType.FIXED64_LIST_PACKED, false)); + builder.withField( + forField(field("fieldFixed32ListPacked41_"), 41, FieldType.FIXED32_LIST_PACKED, false)); + builder.withField( + forField(field("fieldBoolListPacked42_"), 42, FieldType.BOOL_LIST_PACKED, false)); + builder.withField( + forField(field("fieldUint32ListPacked43_"), 43, FieldType.UINT32_LIST_PACKED, false)); + builder.withField( + forFieldWithEnumVerifier( + field("fieldEnumListPacked44_"), + 44, + FieldType.ENUM_LIST_PACKED, + asVerifier(TestEnum.internalGetValueMap()))); + builder.withField( + forField(field("fieldSfixed32ListPacked45_"), 45, FieldType.SFIXED32_LIST_PACKED, false)); + builder.withField( + forField(field("fieldSfixed64ListPacked46_"), 46, FieldType.SFIXED64_LIST_PACKED, false)); + builder.withField( + forField(field("fieldSint32ListPacked47_"), 47, FieldType.SINT32_LIST_PACKED, false)); + builder.withField( + forField(field("fieldSint64ListPacked48_"), 48, FieldType.SINT64_LIST_PACKED, false)); + + builder.withField( + forProto2OptionalField( + field("fieldGroup49_"), 49, FieldType.GROUP, bitField0, 0x00020000, false, null)); + builder.withField( + forRepeatedMessageField( + field("fieldGroupList51_"), + 51, + FieldType.GROUP_LIST, + Proto2Message.FieldGroupList51.class)); + + OneofInfo oneof = new OneofInfo(0, field("testOneofCase_"), field("testOneof_")); + builder.withField(forOneofMemberField(53, FieldType.DOUBLE, oneof, Double.class, false, null)); + builder.withField(forOneofMemberField(54, FieldType.FLOAT, oneof, Float.class, false, null)); + builder.withField(forOneofMemberField(55, FieldType.INT64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(56, FieldType.UINT64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(57, FieldType.INT32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(58, FieldType.FIXED64, oneof, Long.class, false, null)); + builder.withField( + forOneofMemberField(59, FieldType.FIXED32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(60, FieldType.BOOL, oneof, Boolean.class, false, null)); + builder.withField(forOneofMemberField(61, FieldType.STRING, oneof, String.class, false, null)); + builder.withField( + forOneofMemberField(62, FieldType.MESSAGE, oneof, Proto2Message.class, false, null)); + builder.withField( + forOneofMemberField(63, FieldType.BYTES, oneof, ByteString.class, false, null)); + builder.withField(forOneofMemberField(64, FieldType.UINT32, oneof, Integer.class, false, null)); + builder.withField( + forOneofMemberField(65, FieldType.SFIXED32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(66, FieldType.SFIXED64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(67, FieldType.SINT32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(68, FieldType.SINT64, oneof, Long.class, false, null)); + builder.withField( + forOneofMemberField( + 69, FieldType.GROUP, oneof, Proto2Message.FieldGroup69.class, false, null)); + + Field bitField1 = field("bitField1_"); + builder.withField( + forProto2RequiredField( + field("fieldRequiredDouble71_"), + 71, + FieldType.DOUBLE, + bitField1, + 0x00000008, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredFloat72_"), + 72, + FieldType.FLOAT, + bitField1, + 0x00000010, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredInt6473_"), + 73, + FieldType.INT64, + bitField1, + 0x00000020, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredUint6474_"), + 74, + FieldType.UINT64, + bitField1, + 0x00000040, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredInt3275_"), + 75, + FieldType.INT32, + bitField1, + 0x00000080, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredFixed6476_"), + 76, + FieldType.FIXED64, + bitField1, + 0x00000100, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredFixed3277_"), + 77, + FieldType.FIXED32, + bitField1, + 0x00000200, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredBool78_"), 78, FieldType.BOOL, bitField1, 0x00000400, false, null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredString79_"), + 79, + FieldType.STRING, + bitField1, + 0x00000800, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredMessage80_"), + 80, + FieldType.MESSAGE, + bitField1, + 0x00001000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredBytes81_"), + 81, + FieldType.BYTES, + bitField1, + 0x00002000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredUint3282_"), + 82, + FieldType.UINT32, + bitField1, + 0x00004000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredEnum83_"), + 83, + FieldType.ENUM, + bitField1, + 0x00008000, + false, + asVerifier(TestEnum.internalGetValueMap()))); + builder.withField( + forProto2RequiredField( + field("fieldRequiredSfixed3284_"), + 84, + FieldType.SFIXED32, + bitField1, + 0x00010000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredSfixed6485_"), + 85, + FieldType.SFIXED64, + bitField1, + 0x00020000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredSint3286_"), + 86, + FieldType.SINT32, + bitField1, + 0x00040000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredSint6487_"), + 87, + FieldType.SINT64, + bitField1, + 0x00080000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredGroup88_"), + 88, + FieldType.GROUP, + bitField1, + 0x00100000, + false, + null)); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroup49() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroup49.class, "fieldInt3250_"), + 50, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroupList51() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroupList51.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroupList51.class, "fieldInt3252_"), + 52, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroup69() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup69.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroup69.class, "fieldInt3270_"), + 70, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForRequiredNestedMessage() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(RequiredNestedMessage.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(RequiredNestedMessage.class, "value_"), + 1, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldRequiredGroup88() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldRequiredGroup88.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldRequiredGroup88.class, "fieldInt3289_"), + 89, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2Empty() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2MessageWithExtensions() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(0); + builder.withSyntax(ProtoSyntax.PROTO2); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForExtensionFieldGroup49() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(Proto2Testing.FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(Proto2Testing.FieldGroup49.class, "fieldInt3250_"), + 50, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForExtensionFieldGroupList51() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(Proto2Testing.FieldGroupList51.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(Proto2Testing.FieldGroupList51.class, "fieldInt3252_"), + 52, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2MessageWithMaps() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(); + builder.withCheckInitialized( + new int[] { + 10, 27, 44, 61, 78, 95, 112, 129, 146, 163, 180, 197, + }); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_bool_1", 1)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_bytes_2", 2)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_double_3", 3)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_enum_4", 4)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_fixed32_5", 5)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_fixed64_6", 6)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_float_7", 7)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_int32_8", 8)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_int64_9", 9)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_message_10", 10)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_sfixed32_11", 11)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_sfixed64_12", 12)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_sint32_13", 13)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_sint64_14", 14)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_string_15", 15)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_uint32_16", 16)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_uint64_17", 17)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_bool_18", 18)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_bytes_19", 19)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_double_20", 20)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_enum_21", 21)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_fixed32_22", 22)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_fixed64_23", 23)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_float_24", 24)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_int32_25", 25)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_int64_26", 26)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_message_27", 27)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_sfixed32_28", 28)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_sfixed64_29", 29)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_sint32_30", 30)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_sint64_31", 31)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_string_32", 32)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_uint32_33", 33)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_uint64_34", 34)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_bool_35", 35)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_bytes_36", 36)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_double_37", 37)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_enum_38", 38)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_fixed32_39", 39)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_fixed64_40", 40)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_float_41", 41)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_int32_42", 42)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_int64_43", 43)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_message_44", 44)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_sfixed32_45", 45)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_sfixed64_46", 46)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_sint32_47", 47)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_sint64_48", 48)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_string_49", 49)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_uint32_50", 50)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_uint64_51", 51)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_bool_52", 52)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_bytes_53", 53)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_double_54", 54)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_enum_55", 55)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_fixed32_56", 56)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_fixed64_57", 57)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_float_58", 58)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_int32_59", 59)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_int64_60", 60)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_message_61", 61)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_sfixed32_62", 62)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_sfixed64_63", 63)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_sint32_64", 64)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_sint64_65", 65)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_string_66", 66)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_uint32_67", 67)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_uint64_68", 68)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_bool_69", 69)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_bytes_70", 70)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_double_71", 71)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_enum_72", 72)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_fixed32_73", 73)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_fixed64_74", 74)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_float_75", 75)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_int32_76", 76)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_int64_77", 77)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_message_78", 78)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_sfixed32_79", 79)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_sfixed64_80", 80)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_sint32_81", 81)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_sint64_82", 82)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_string_83", 83)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_uint32_84", 84)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_uint64_85", 85)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_bool_86", 86)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_bytes_87", 87)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_double_88", 88)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_enum_89", 89)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_fixed32_90", 90)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_fixed64_91", 91)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_float_92", 92)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_int32_93", 93)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_int64_94", 94)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_message_95", 95)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_sfixed32_96", 96)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_sfixed64_97", 97)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_sint32_98", 98)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_sint64_99", 99)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_string_100", 100)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_uint32_101", 101)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_uint64_102", 102)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_bool_103", 103)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_bytes_104", 104)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_double_105", 105)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_enum_106", 106)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_fixed32_107", 107)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_fixed64_108", 108)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_float_109", 109)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_int32_110", 110)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_int64_111", 111)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_message_112", 112)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_sfixed32_113", 113)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_sfixed64_114", 114)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_sint32_115", 115)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_sint64_116", 116)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_string_117", 117)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_uint32_118", 118)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_uint64_119", 119)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_bool_120", 120)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_bytes_121", 121)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_double_122", 122)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_enum_123", 123)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_fixed32_124", 124)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_fixed64_125", 125)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_float_126", 126)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_int32_127", 127)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_int64_128", 128)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_message_129", 129)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_sfixed32_130", 130)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_sfixed64_131", 131)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_sint32_132", 132)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_sint64_133", 133)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_string_134", 134)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_uint32_135", 135)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_uint64_136", 136)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_bool_137", 137)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_bytes_138", 138)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_double_139", 139)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_enum_140", 140)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_fixed32_141", 141)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_fixed64_142", 142)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_float_143", 143)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_int32_144", 144)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_int64_145", 145)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_message_146", 146)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_sfixed32_147", 147)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_sfixed64_148", 148)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_sint32_149", 149)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_sint64_150", 150)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_string_151", 151)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_uint32_152", 152)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_uint64_153", 153)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_bool_154", 154)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_bytes_155", 155)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_double_156", 156)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_enum_157", 157)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_fixed32_158", 158)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_fixed64_159", 159)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_float_160", 160)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_int32_161", 161)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_int64_162", 162)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_message_163", 163)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_sfixed32_164", 164)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_sfixed64_165", 165)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_sint32_166", 166)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_sint64_167", 167)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_string_168", 168)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_uint32_169", 169)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_uint64_170", 170)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_bool_171", 171)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_bytes_172", 172)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_double_173", 173)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_enum_174", 174)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_fixed32_175", 175)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_fixed64_176", 176)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_float_177", 177)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_int32_178", 178)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_int64_179", 179)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_message_180", 180)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_sfixed32_181", 181)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_sfixed64_182", 182)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_sint32_183", 183)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_sint64_184", 184)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_string_185", 185)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_uint32_186", 186)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_uint64_187", 187)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_bool_188", 188)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_bytes_189", 189)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_double_190", 190)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_enum_191", 191)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_fixed32_192", 192)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_fixed64_193", 193)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_float_194", 194)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_int32_195", 195)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_int64_196", 196)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_message_197", 197)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_sfixed32_198", 198)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_sfixed64_199", 199)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_sint32_200", 200)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_sint64_201", 201)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_string_202", 202)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_uint32_203", 203)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_uint64_204", 204)); + + return builder.build(); + } + + private static FieldInfo mapFieldInfo(Class clazz, String fieldName, int fieldNumber) { + try { + return forMapField( + field(clazz, SchemaUtil.toCamelCase(fieldName, false) + "_"), + fieldNumber, + SchemaUtil.getMapDefaultEntry(clazz, fieldName), + fieldName.contains("_enum_") ? asVerifier(TestEnum.internalGetValueMap()) : null); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + + + private static Field field(String name) { + return field(Proto2Message.class, name); + } + + private static Field field(Class clazz, String name) { + try { + return clazz.getDeclaredField(name); + } catch (NoSuchFieldException | SecurityException e) { + throw new RuntimeException(e); + } + } + + private static Internal.EnumVerifier asVerifier(final Internal.EnumLiteMap map) { + return new Internal.EnumVerifier() { + @Override + public boolean isInRange(int number) { + return map.findValueByNumber(number) != null; + } + }; + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2MessageLiteFactory.java b/java/core/src/test/java/com/google/protobuf/Proto2MessageLiteFactory.java new file mode 100755 index 0000000000..2da349424e --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2MessageLiteFactory.java @@ -0,0 +1,558 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Creates instances of {@link Proto2MessageLite} based on the tree configuration. */ +public final class Proto2MessageLiteFactory + implements ExperimentalMessageFactory { + private final int numRepeatedFields; + private final int branchingFactor; + private final Proto2MessageLiteFactory nextLevel; + private final ExperimentalTestDataProvider data; + + public Proto2MessageLiteFactory( + int numRepeatedFields, int stringLength, int branchingFactor, int treeDepth) { + this( + new ExperimentalTestDataProvider(stringLength), + numRepeatedFields, + branchingFactor, + treeDepth); + } + + private Proto2MessageLiteFactory( + ExperimentalTestDataProvider data, + int numRepeatedFields, + int branchingFactor, + int treeDepth) { + this.numRepeatedFields = numRepeatedFields; + this.branchingFactor = branchingFactor; + this.data = data; + if (treeDepth > 0) { + nextLevel = + new Proto2MessageLiteFactory(data, numRepeatedFields, branchingFactor, treeDepth - 1); + } else { + nextLevel = null; + } + } + + @Override + public ExperimentalTestDataProvider dataProvider() { + return data; + } + + @Override + public Proto2MessageLite newMessage() { + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + builder.setFieldDouble1(data.getDouble()); + builder.setFieldFloat2(data.getFloat()); + builder.setFieldInt643(data.getLong()); + builder.setFieldUint644(data.getLong()); + builder.setFieldInt325(data.getInt()); + builder.setFieldFixed646(data.getLong()); + builder.setFieldFixed327(data.getInt()); + builder.setFieldBool8(data.getBool()); + builder.setFieldString9(data.getString()); + // We don't populate the message field. Instead we apply the branching factor to the + // repeated message field below. + builder.setFieldBytes11(data.getBytes()); + builder.setFieldUint3212(data.getInt()); + builder.setFieldEnum13(Proto2MessageLite.TestEnum.forNumber(data.getEnum())); + builder.setFieldSfixed3214(data.getInt()); + builder.setFieldSfixed6415(data.getLong()); + builder.setFieldSint3216(data.getInt()); + builder.setFieldSint6417(data.getLong()); + + for (int i = 0; i < numRepeatedFields; ++i) { + builder.addFieldDoubleList18(data.getDouble()); + builder.addFieldFloatList19(data.getFloat()); + builder.addFieldInt64List20(data.getLong()); + builder.addFieldUint64List21(data.getLong()); + builder.addFieldInt32List22(data.getInt()); + builder.addFieldFixed64List23(data.getLong()); + builder.addFieldFixed32List24(data.getInt()); + builder.addFieldBoolList25(data.getBool()); + builder.addFieldStringList26(data.getString()); + // Repeated message field is controlled by the branching factor below. + builder.addFieldBytesList28(data.getBytes()); + builder.addFieldUint32List29(data.getInt()); + builder.addFieldEnumList30(Proto2MessageLite.TestEnum.forNumber(data.getEnum())); + builder.addFieldSfixed32List31(data.getInt()); + builder.addFieldSfixed64List32(data.getLong()); + builder.addFieldSint32List33(data.getInt()); + builder.addFieldSint64List34(data.getLong()); + + builder.addFieldDoubleListPacked35(data.getDouble()); + builder.addFieldFloatListPacked36(data.getFloat()); + builder.addFieldInt64ListPacked37(data.getLong()); + builder.addFieldUint64ListPacked38(data.getLong()); + builder.addFieldInt32ListPacked39(data.getInt()); + builder.addFieldFixed64ListPacked40(data.getLong()); + builder.addFieldFixed32ListPacked41(data.getInt()); + builder.addFieldBoolListPacked42(data.getBool()); + builder.addFieldUint32ListPacked43(data.getInt()); + builder.addFieldEnumListPacked44(Proto2MessageLite.TestEnum.forNumber(data.getEnum())); + builder.addFieldSfixed32ListPacked45(data.getInt()); + builder.addFieldSfixed64ListPacked46(data.getLong()); + builder.addFieldSint32ListPacked47(data.getInt()); + builder.addFieldSint64ListPacked48(data.getLong()); + } + + builder.setFieldGroup49( + Proto2MessageLite.FieldGroup49.newBuilder().setFieldInt3250(data.getInt())); + + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldGroupList51( + Proto2MessageLite.FieldGroupList51.newBuilder().setFieldInt3252(data.getInt())); + } + + // Set all required fields. + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + + // Handle the branching factor. + if (nextLevel != null) { + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldMessageList27(nextLevel.newMessage()); + } + } + + return builder.build(); + } + + private interface MapValueProvider { + public T getValue(); + } + + private final MapValueProvider integerProvider = + new MapValueProvider() { + @Override + public Integer getValue() { + return data.getInt(); + } + }; + private final MapValueProvider longProvider = + new MapValueProvider() { + @Override + public Long getValue() { + return data.getLong(); + } + }; + private final MapValueProvider stringProvider = + new MapValueProvider() { + @Override + public String getValue() { + return data.getString(); + } + }; + private final MapValueProvider bytesProvider = + new MapValueProvider() { + @Override + public ByteString getValue() { + return data.getBytes(); + } + }; + private final MapValueProvider booleanProvider = + new MapValueProvider() { + @Override + public Boolean getValue() { + return data.getBool(); + } + }; + private final MapValueProvider floatProvider = + new MapValueProvider() { + @Override + public Float getValue() { + return data.getFloat(); + } + }; + private final MapValueProvider doubleProvider = + new MapValueProvider() { + @Override + public Double getValue() { + return data.getDouble(); + } + }; + private final MapValueProvider messageProvider = + new MapValueProvider() { + @Override + public Proto2MessageLite getValue() { + return newMessage(); + } + }; + private final MapValueProvider enumProvider = + new MapValueProvider() { + @Override + public Proto2MessageLite.TestEnum getValue() { + return Proto2MessageLite.TestEnum.forNumber(data.getEnum()); + } + }; + + private Map populateIntegerMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getInt(), provider.getValue()); + } + return map; + } + + private Map populateLongMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getLong(), provider.getValue()); + } + return map; + } + + private Map populateStringMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getString(), provider.getValue()); + } + return map; + } + + private Map populateBooleanMap(MapValueProvider provider) { + Map map = new HashMap<>(); + map.put(false, provider.getValue()); + map.put(true, provider.getValue()); + return map; + } + + public Proto2MessageLiteWithMaps newMessageWithMaps() { + Proto2MessageLiteWithMaps.Builder builder = Proto2MessageLiteWithMaps.newBuilder(); + + builder.putAllFieldMapBoolBool1(populateBooleanMap(booleanProvider)); + builder.putAllFieldMapBoolBytes2(populateBooleanMap(bytesProvider)); + builder.putAllFieldMapBoolDouble3(populateBooleanMap(doubleProvider)); + builder.putAllFieldMapBoolEnum4(populateBooleanMap(enumProvider)); + builder.putAllFieldMapBoolFixed325(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolFixed646(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolFloat7(populateBooleanMap(floatProvider)); + builder.putAllFieldMapBoolInt328(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolInt649(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolMessage10(populateBooleanMap(messageProvider)); + builder.putAllFieldMapBoolSfixed3211(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSfixed6412(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolSint3213(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSint6414(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolString15(populateBooleanMap(stringProvider)); + builder.putAllFieldMapBoolUint3216(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolUint6417(populateBooleanMap(longProvider)); + builder.putAllFieldMapFixed32Bool18(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapFixed32Bytes19(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapFixed32Double20(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapFixed32Enum21(populateIntegerMap(enumProvider)); + builder.putAllFieldMapFixed32Fixed3222(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Fixed6423(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Float24(populateIntegerMap(floatProvider)); + builder.putAllFieldMapFixed32Int3225(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Int6426(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Message27(populateIntegerMap(messageProvider)); + builder.putAllFieldMapFixed32Sfixed3228(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sfixed6429(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Sint3230(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sint6431(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32String32(populateIntegerMap(stringProvider)); + builder.putAllFieldMapFixed32Uint3233(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Uint6434(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed64Bool35(populateLongMap(booleanProvider)); + builder.putAllFieldMapFixed64Bytes36(populateLongMap(bytesProvider)); + builder.putAllFieldMapFixed64Double37(populateLongMap(doubleProvider)); + builder.putAllFieldMapFixed64Enum38(populateLongMap(enumProvider)); + builder.putAllFieldMapFixed64Fixed3239(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Fixed6440(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Float41(populateLongMap(floatProvider)); + builder.putAllFieldMapFixed64Int3242(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Int6443(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Message44(populateLongMap(messageProvider)); + builder.putAllFieldMapFixed64Sfixed3245(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sfixed6446(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Sint3247(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sint6448(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64String49(populateLongMap(stringProvider)); + builder.putAllFieldMapFixed64Uint3250(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Uint6451(populateLongMap(longProvider)); + builder.putAllFieldMapInt32Bool52(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapInt32Bytes53(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapInt32Double54(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapInt32Enum55(populateIntegerMap(enumProvider)); + builder.putAllFieldMapInt32Fixed3256(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Fixed6457(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Float58(populateIntegerMap(floatProvider)); + builder.putAllFieldMapInt32Int3259(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Int6460(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Message61(populateIntegerMap(messageProvider)); + builder.putAllFieldMapInt32Sfixed3262(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sfixed6463(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Sint3264(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sint6465(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32String66(populateIntegerMap(stringProvider)); + builder.putAllFieldMapInt32Uint3267(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Uint6468(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt64Bool69(populateLongMap(booleanProvider)); + builder.putAllFieldMapInt64Bytes70(populateLongMap(bytesProvider)); + builder.putAllFieldMapInt64Double71(populateLongMap(doubleProvider)); + builder.putAllFieldMapInt64Enum72(populateLongMap(enumProvider)); + builder.putAllFieldMapInt64Fixed3273(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Fixed6474(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Float75(populateLongMap(floatProvider)); + builder.putAllFieldMapInt64Int3276(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Int6477(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Message78(populateLongMap(messageProvider)); + builder.putAllFieldMapInt64Sfixed3279(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sfixed6480(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Sint3281(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sint6482(populateLongMap(longProvider)); + builder.putAllFieldMapInt64String83(populateLongMap(stringProvider)); + builder.putAllFieldMapInt64Uint3284(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Uint6485(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed32Bool86(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSfixed32Bytes87(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSfixed32Double88(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSfixed32Enum89(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSfixed32Fixed3290(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Fixed6491(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Float92(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSfixed32Int3293(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Int6494(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Message95(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSfixed32Sfixed3296(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sfixed6497(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Sint3298(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sint6499(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32String100(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSfixed32Uint32101(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Uint64102(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed64Bool103(populateLongMap(booleanProvider)); + builder.putAllFieldMapSfixed64Bytes104(populateLongMap(bytesProvider)); + builder.putAllFieldMapSfixed64Double105(populateLongMap(doubleProvider)); + builder.putAllFieldMapSfixed64Enum106(populateLongMap(enumProvider)); + builder.putAllFieldMapSfixed64Fixed32107(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Fixed64108(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Float109(populateLongMap(floatProvider)); + builder.putAllFieldMapSfixed64Int32110(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Int64111(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Message112(populateLongMap(messageProvider)); + builder.putAllFieldMapSfixed64Sfixed32113(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sfixed64114(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Sint32115(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sint64116(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64String117(populateLongMap(stringProvider)); + builder.putAllFieldMapSfixed64Uint32118(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Uint64119(populateLongMap(longProvider)); + builder.putAllFieldMapSint32Bool120(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSint32Bytes121(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSint32Double122(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSint32Enum123(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSint32Fixed32124(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Fixed64125(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Float126(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSint32Int32127(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Int64128(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Message129(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSint32Sfixed32130(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sfixed64131(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Sint32132(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sint64133(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32String134(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSint32Uint32135(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Uint64136(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint64Bool137(populateLongMap(booleanProvider)); + builder.putAllFieldMapSint64Bytes138(populateLongMap(bytesProvider)); + builder.putAllFieldMapSint64Double139(populateLongMap(doubleProvider)); + builder.putAllFieldMapSint64Enum140(populateLongMap(enumProvider)); + builder.putAllFieldMapSint64Fixed32141(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Fixed64142(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Float143(populateLongMap(floatProvider)); + builder.putAllFieldMapSint64Int32144(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Int64145(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Message146(populateLongMap(messageProvider)); + builder.putAllFieldMapSint64Sfixed32147(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sfixed64148(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Sint32149(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sint64150(populateLongMap(longProvider)); + builder.putAllFieldMapSint64String151(populateLongMap(stringProvider)); + builder.putAllFieldMapSint64Uint32152(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Uint64153(populateLongMap(longProvider)); + builder.putAllFieldMapStringBool154(populateStringMap(booleanProvider)); + builder.putAllFieldMapStringBytes155(populateStringMap(bytesProvider)); + builder.putAllFieldMapStringDouble156(populateStringMap(doubleProvider)); + builder.putAllFieldMapStringEnum157(populateStringMap(enumProvider)); + builder.putAllFieldMapStringFixed32158(populateStringMap(integerProvider)); + builder.putAllFieldMapStringFixed64159(populateStringMap(longProvider)); + builder.putAllFieldMapStringFloat160(populateStringMap(floatProvider)); + builder.putAllFieldMapStringInt32161(populateStringMap(integerProvider)); + builder.putAllFieldMapStringInt64162(populateStringMap(longProvider)); + builder.putAllFieldMapStringMessage163(populateStringMap(messageProvider)); + builder.putAllFieldMapStringSfixed32164(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSfixed64165(populateStringMap(longProvider)); + builder.putAllFieldMapStringSint32166(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSint64167(populateStringMap(longProvider)); + builder.putAllFieldMapStringString168(populateStringMap(stringProvider)); + builder.putAllFieldMapStringUint32169(populateStringMap(integerProvider)); + builder.putAllFieldMapStringUint64170(populateStringMap(longProvider)); + builder.putAllFieldMapUint32Bool171(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapUint32Bytes172(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapUint32Double173(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapUint32Enum174(populateIntegerMap(enumProvider)); + builder.putAllFieldMapUint32Fixed32175(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Fixed64176(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Float177(populateIntegerMap(floatProvider)); + builder.putAllFieldMapUint32Int32178(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Int64179(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Message180(populateIntegerMap(messageProvider)); + builder.putAllFieldMapUint32Sfixed32181(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sfixed64182(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Sint32183(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sint64184(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32String185(populateIntegerMap(stringProvider)); + builder.putAllFieldMapUint32Uint32186(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Uint64187(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint64Bool188(populateLongMap(booleanProvider)); + builder.putAllFieldMapUint64Bytes189(populateLongMap(bytesProvider)); + builder.putAllFieldMapUint64Double190(populateLongMap(doubleProvider)); + builder.putAllFieldMapUint64Enum191(populateLongMap(enumProvider)); + builder.putAllFieldMapUint64Fixed32192(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Fixed64193(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Float194(populateLongMap(floatProvider)); + builder.putAllFieldMapUint64Int32195(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Int64196(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Message197(populateLongMap(messageProvider)); + builder.putAllFieldMapUint64Sfixed32198(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sfixed64199(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Sint32200(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sint64201(populateLongMap(longProvider)); + builder.putAllFieldMapUint64String202(populateLongMap(stringProvider)); + builder.putAllFieldMapUint64Uint32203(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Uint64204(populateLongMap(longProvider)); + + return builder.build(); + } + + public List newMessagesMissingRequiredFields() { + List results = new ArrayList<>(); + for (int i = 71; i <= 88; ++i) { + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + populateRequiredFields(builder, i); + results.add(builder.buildPartial()); + } + { + // A nested optional message field is missing required fields. + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.setFieldMessage10(Proto2MessageLite.getDefaultInstance()); + results.add(builder.buildPartial()); + } + { + // A nested repeated message field is missing required fields. + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.addFieldMessageList27(Proto2MessageLite.getDefaultInstance()); + results.add(builder.buildPartial()); + } + { + // A nested oneof message field is missing required fields. + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.setFieldMessage62(Proto2MessageLite.getDefaultInstance()); + results.add(builder.buildPartial()); + } + return results; + } + + // 0 is not a valid field number so we use it to mean no fields are excluded. + private static final int INCLUDE_ALL_REQUIRED_FIELDS = 0; + + private void populateRequiredFields(Proto2MessageLite.Builder builder, int excludedFieldNumber) { + if (excludedFieldNumber != 71) { + builder.setFieldRequiredDouble71(data.getDouble()); + } + if (excludedFieldNumber != 72) { + builder.setFieldRequiredFloat72(data.getFloat()); + } + if (excludedFieldNumber != 73) { + builder.setFieldRequiredInt6473(data.getLong()); + } + if (excludedFieldNumber != 74) { + builder.setFieldRequiredUint6474(data.getLong()); + } + if (excludedFieldNumber != 75) { + builder.setFieldRequiredInt3275(data.getInt()); + } + if (excludedFieldNumber != 76) { + builder.setFieldRequiredFixed6476(data.getLong()); + } + if (excludedFieldNumber != 77) { + builder.setFieldRequiredFixed3277(data.getInt()); + } + if (excludedFieldNumber != 78) { + builder.setFieldRequiredBool78(data.getBool()); + } + if (excludedFieldNumber != 79) { + builder.setFieldRequiredString79(data.getString()); + } + if (excludedFieldNumber != 80) { + builder.setFieldRequiredMessage80( + Proto2MessageLite.RequiredNestedMessage.newBuilder().setValue(data.getInt())); + } + if (excludedFieldNumber != 81) { + builder.setFieldRequiredBytes81(data.getBytes()); + } + if (excludedFieldNumber != 82) { + builder.setFieldRequiredUint3282(data.getInt()); + } + if (excludedFieldNumber != 83) { + builder.setFieldRequiredEnum83(Proto2MessageLite.TestEnum.forNumber(data.getEnum())); + } + if (excludedFieldNumber != 84) { + builder.setFieldRequiredSfixed3284(data.getInt()); + } + if (excludedFieldNumber != 85) { + builder.setFieldRequiredSfixed6485(data.getLong()); + } + if (excludedFieldNumber != 86) { + builder.setFieldRequiredSint3286(data.getInt()); + } + if (excludedFieldNumber != 87) { + builder.setFieldRequiredSint6487(data.getLong()); + } + if (excludedFieldNumber != 88) { + builder.setFieldRequiredGroup88( + Proto2MessageLite.FieldRequiredGroup88.newBuilder().setFieldInt3289(data.getInt())); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2SchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto2SchemaTest.java new file mode 100755 index 0000000000..098e7fe182 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2SchemaTest.java @@ -0,0 +1,49 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto2SchemaTest extends AbstractProto2SchemaTest { + + @Override + protected Schema schema() { + return TestSchemas.genericProto2Schema; + } + + @Override + protected void registerSchemas() { + TestSchemas.registerGenericProto2Schemas(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java b/java/core/src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java new file mode 100755 index 0000000000..57ac0695e4 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java @@ -0,0 +1,111 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.Descriptors.FieldDescriptor; +import protobuf_unittest.UnittestProto; +import protobuf_unittest.UnittestProto.TestAllExtensions; +import protobuf_unittest.UnittestProto.TestAllTypes; +import junit.framework.TestCase; + +/** Unit tests for proto2 that treats unknown enum values as unknown fields. */ +public class Proto2UnknownEnumValueTest extends TestCase { + FieldDescriptor singularField = + TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum"); + FieldDescriptor repeatedField = + TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum"); + byte[] payload = buildPayloadWithUnknownEnumValues(); + + private byte[] buildPayloadWithUnknownEnumValues() { + // Builds a payload with unknown enum values. + UnknownFieldSet.Builder builder = UnknownFieldSet.newBuilder(); + builder.addField( + singularField.getNumber(), + UnknownFieldSet.Field.newBuilder() + .addVarint(TestAllTypes.NestedEnum.BAR.getNumber()) + .addVarint(1901 /* unknown enum value */) + .build()); + builder.addField( + repeatedField.getNumber(), + UnknownFieldSet.Field.newBuilder() + .addVarint(TestAllTypes.NestedEnum.FOO.getNumber()) + .addVarint(1902 /* unknown enum value */) + .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber()) + .addVarint(1903 /* unknown enum value */) + .build()); + return builder.build().toByteArray(); + } + + public void testUnknownEnumValues() throws Exception { + TestAllTypes message = TestAllTypes.parseFrom(payload); + + // Known enum values should be preserved. + assertEquals(TestAllTypes.NestedEnum.BAR, message.getOptionalNestedEnum()); + assertEquals(2, message.getRepeatedNestedEnumList().size()); + assertEquals(TestAllTypes.NestedEnum.FOO, message.getRepeatedNestedEnum(0)); + assertEquals(TestAllTypes.NestedEnum.BAZ, message.getRepeatedNestedEnum(1)); + + // Unknown enum values should be found in UnknownFieldSet. + UnknownFieldSet unknown = message.getUnknownFields(); + assertEquals( + 1901, unknown.getField(singularField.getNumber()).getVarintList().get(0).longValue()); + assertEquals( + 1902, unknown.getField(repeatedField.getNumber()).getVarintList().get(0).longValue()); + assertEquals( + 1903, unknown.getField(repeatedField.getNumber()).getVarintList().get(1).longValue()); + } + + public void testExtensionUnknownEnumValues() throws Exception { + ExtensionRegistry registry = ExtensionRegistry.newInstance(); + UnittestProto.registerAllExtensions(registry); + TestAllExtensions message = TestAllExtensions.parseFrom(payload, registry); + + assertEquals( + TestAllTypes.NestedEnum.BAR, + message.getExtension(UnittestProto.optionalNestedEnumExtension)); + assertEquals(2, message.getExtension(UnittestProto.repeatedNestedEnumExtension).size()); + assertEquals( + TestAllTypes.NestedEnum.FOO, + message.getExtension(UnittestProto.repeatedNestedEnumExtension, 0)); + assertEquals( + TestAllTypes.NestedEnum.BAZ, + message.getExtension(UnittestProto.repeatedNestedEnumExtension, 1)); + + // Unknown enum values should be found in UnknownFieldSet. + UnknownFieldSet unknown = message.getUnknownFields(); + assertEquals( + 1901, unknown.getField(singularField.getNumber()).getVarintList().get(0).longValue()); + assertEquals( + 1902, unknown.getField(repeatedField.getNumber()).getVarintList().get(0).longValue()); + assertEquals( + 1903, unknown.getField(repeatedField.getNumber()).getVarintList().get(1).longValue()); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java new file mode 100755 index 0000000000..ac9d6df70e --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java @@ -0,0 +1,49 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto3LiteSchemaTest extends AbstractProto3LiteSchemaTest { + + @Override + protected Schema schema() { + return TestSchemasLite.genericProto3LiteSchema; + } + + @Override + protected void registerSchemas() { + TestSchemasLite.registerGenericProto3LiteSchemas(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3MessageFactory.java b/java/core/src/test/java/com/google/protobuf/Proto3MessageFactory.java new file mode 100755 index 0000000000..33d5fe0fcc --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3MessageFactory.java @@ -0,0 +1,450 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps; +import java.util.HashMap; +import java.util.Map; + +/** Creates instances of {@link Proto3Message} based on the tree configuration. */ +public final class Proto3MessageFactory implements ExperimentalMessageFactory { + private final int numRepeatedFields; + private final int branchingFactor; + private final Proto3MessageFactory nextLevel; + private final ExperimentalTestDataProvider data; + + public Proto3MessageFactory( + int numRepeatedFields, int stringLength, int branchingFactor, int treeDepth) { + this( + new ExperimentalTestDataProvider(stringLength), + numRepeatedFields, + branchingFactor, + treeDepth); + } + + private Proto3MessageFactory( + ExperimentalTestDataProvider data, + int numRepeatedFields, + int branchingFactor, + int treeDepth) { + this.numRepeatedFields = numRepeatedFields; + this.branchingFactor = branchingFactor; + this.data = data; + if (treeDepth > 0) { + nextLevel = new Proto3MessageFactory(data, numRepeatedFields, branchingFactor, treeDepth - 1); + } else { + nextLevel = null; + } + } + + @Override + public ExperimentalTestDataProvider dataProvider() { + return data; + } + + @Override + public Proto3Message newMessage() { + Proto3Message.Builder builder = Proto3Message.newBuilder(); + builder.setFieldDouble1(data.getDouble()); + builder.setFieldFloat2(data.getFloat()); + builder.setFieldInt643(data.getLong()); + builder.setFieldUint644(data.getLong()); + builder.setFieldInt325(data.getInt()); + builder.setFieldFixed646(data.getLong()); + builder.setFieldFixed327(data.getInt()); + builder.setFieldBool8(data.getBool()); + builder.setFieldString9(data.getString()); + // We don't populate the message field. Instead we apply the branching factor to the + // repeated message field below. + builder.setFieldBytes11(data.getBytes()); + builder.setFieldUint3212(data.getInt()); + builder.setFieldEnum13Value(data.getEnum()); + builder.setFieldSfixed3214(data.getInt()); + builder.setFieldSfixed6415(data.getLong()); + builder.setFieldSint3216(data.getInt()); + builder.setFieldSint6417(data.getLong()); + + for (int i = 0; i < numRepeatedFields; ++i) { + builder.addFieldDoubleList18(data.getDouble()); + builder.addFieldFloatList19(data.getFloat()); + builder.addFieldInt64List20(data.getLong()); + builder.addFieldUint64List21(data.getLong()); + builder.addFieldInt32List22(data.getInt()); + builder.addFieldFixed64List23(data.getLong()); + builder.addFieldFixed32List24(data.getInt()); + builder.addFieldBoolList25(data.getBool()); + builder.addFieldStringList26(data.getString()); + // Repeated message field is controlled by the branching factor below. + builder.addFieldBytesList28(data.getBytes()); + builder.addFieldUint32List29(data.getInt()); + builder.addFieldEnumList30Value(data.getEnum()); + builder.addFieldSfixed32List31(data.getInt()); + builder.addFieldSfixed64List32(data.getLong()); + builder.addFieldSint32List33(data.getInt()); + builder.addFieldSint64List34(data.getLong()); + + builder.addFieldDoubleListPacked35(data.getDouble()); + builder.addFieldFloatListPacked36(data.getFloat()); + builder.addFieldInt64ListPacked37(data.getLong()); + builder.addFieldUint64ListPacked38(data.getLong()); + builder.addFieldInt32ListPacked39(data.getInt()); + builder.addFieldFixed64ListPacked40(data.getLong()); + builder.addFieldFixed32ListPacked41(data.getInt()); + builder.addFieldBoolListPacked42(data.getBool()); + builder.addFieldUint32ListPacked43(data.getInt()); + builder.addFieldEnumListPacked44Value(data.getEnum()); + builder.addFieldSfixed32ListPacked45(data.getInt()); + builder.addFieldSfixed64ListPacked46(data.getLong()); + builder.addFieldSint32ListPacked47(data.getInt()); + builder.addFieldSint64ListPacked48(data.getLong()); + } + + // Handle the branching factor. + if (nextLevel != null) { + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldMessageList27(nextLevel.newMessage()); + } + } + + return builder.build(); + } + + private interface MapValueProvider { + public T getValue(); + } + + private final MapValueProvider integerProvider = + new MapValueProvider() { + @Override + public Integer getValue() { + return data.getInt(); + } + }; + private final MapValueProvider longProvider = + new MapValueProvider() { + @Override + public Long getValue() { + return data.getLong(); + } + }; + private final MapValueProvider stringProvider = + new MapValueProvider() { + @Override + public String getValue() { + return data.getString(); + } + }; + private final MapValueProvider bytesProvider = + new MapValueProvider() { + @Override + public ByteString getValue() { + return data.getBytes(); + } + }; + private final MapValueProvider booleanProvider = + new MapValueProvider() { + @Override + public Boolean getValue() { + return data.getBool(); + } + }; + private final MapValueProvider floatProvider = + new MapValueProvider() { + @Override + public Float getValue() { + return data.getFloat(); + } + }; + private final MapValueProvider doubleProvider = + new MapValueProvider() { + @Override + public Double getValue() { + return data.getDouble(); + } + }; + private final MapValueProvider messageProvider = + new MapValueProvider() { + @Override + public Proto3Message getValue() { + return newMessage(); + } + }; + private final MapValueProvider enumProvider = + new MapValueProvider() { + @Override + public Proto3Message.TestEnum getValue() { + return Proto3Message.TestEnum.forNumber(data.getEnum()); + } + }; + + private Map populateIntegerMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getInt(), provider.getValue()); + } + return map; + } + + private Map populateLongMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getLong(), provider.getValue()); + } + return map; + } + + private Map populateStringMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getString(), provider.getValue()); + } + return map; + } + + private Map populateBooleanMap(MapValueProvider provider) { + Map map = new HashMap<>(); + map.put(false, provider.getValue()); + map.put(true, provider.getValue()); + return map; + } + + public Proto3MessageWithMaps newMessageWithMaps() { + Proto3MessageWithMaps.Builder builder = Proto3MessageWithMaps.newBuilder(); + + builder.putAllFieldMapBoolBool1(populateBooleanMap(booleanProvider)); + builder.putAllFieldMapBoolBytes2(populateBooleanMap(bytesProvider)); + builder.putAllFieldMapBoolDouble3(populateBooleanMap(doubleProvider)); + builder.putAllFieldMapBoolEnum4(populateBooleanMap(enumProvider)); + builder.putAllFieldMapBoolFixed325(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolFixed646(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolFloat7(populateBooleanMap(floatProvider)); + builder.putAllFieldMapBoolInt328(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolInt649(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolMessage10(populateBooleanMap(messageProvider)); + builder.putAllFieldMapBoolSfixed3211(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSfixed6412(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolSint3213(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSint6414(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolString15(populateBooleanMap(stringProvider)); + builder.putAllFieldMapBoolUint3216(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolUint6417(populateBooleanMap(longProvider)); + builder.putAllFieldMapFixed32Bool18(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapFixed32Bytes19(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapFixed32Double20(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapFixed32Enum21(populateIntegerMap(enumProvider)); + builder.putAllFieldMapFixed32Fixed3222(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Fixed6423(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Float24(populateIntegerMap(floatProvider)); + builder.putAllFieldMapFixed32Int3225(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Int6426(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Message27(populateIntegerMap(messageProvider)); + builder.putAllFieldMapFixed32Sfixed3228(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sfixed6429(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Sint3230(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sint6431(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32String32(populateIntegerMap(stringProvider)); + builder.putAllFieldMapFixed32Uint3233(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Uint6434(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed64Bool35(populateLongMap(booleanProvider)); + builder.putAllFieldMapFixed64Bytes36(populateLongMap(bytesProvider)); + builder.putAllFieldMapFixed64Double37(populateLongMap(doubleProvider)); + builder.putAllFieldMapFixed64Enum38(populateLongMap(enumProvider)); + builder.putAllFieldMapFixed64Fixed3239(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Fixed6440(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Float41(populateLongMap(floatProvider)); + builder.putAllFieldMapFixed64Int3242(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Int6443(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Message44(populateLongMap(messageProvider)); + builder.putAllFieldMapFixed64Sfixed3245(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sfixed6446(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Sint3247(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sint6448(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64String49(populateLongMap(stringProvider)); + builder.putAllFieldMapFixed64Uint3250(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Uint6451(populateLongMap(longProvider)); + builder.putAllFieldMapInt32Bool52(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapInt32Bytes53(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapInt32Double54(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapInt32Enum55(populateIntegerMap(enumProvider)); + builder.putAllFieldMapInt32Fixed3256(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Fixed6457(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Float58(populateIntegerMap(floatProvider)); + builder.putAllFieldMapInt32Int3259(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Int6460(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Message61(populateIntegerMap(messageProvider)); + builder.putAllFieldMapInt32Sfixed3262(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sfixed6463(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Sint3264(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sint6465(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32String66(populateIntegerMap(stringProvider)); + builder.putAllFieldMapInt32Uint3267(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Uint6468(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt64Bool69(populateLongMap(booleanProvider)); + builder.putAllFieldMapInt64Bytes70(populateLongMap(bytesProvider)); + builder.putAllFieldMapInt64Double71(populateLongMap(doubleProvider)); + builder.putAllFieldMapInt64Enum72(populateLongMap(enumProvider)); + builder.putAllFieldMapInt64Fixed3273(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Fixed6474(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Float75(populateLongMap(floatProvider)); + builder.putAllFieldMapInt64Int3276(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Int6477(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Message78(populateLongMap(messageProvider)); + builder.putAllFieldMapInt64Sfixed3279(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sfixed6480(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Sint3281(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sint6482(populateLongMap(longProvider)); + builder.putAllFieldMapInt64String83(populateLongMap(stringProvider)); + builder.putAllFieldMapInt64Uint3284(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Uint6485(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed32Bool86(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSfixed32Bytes87(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSfixed32Double88(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSfixed32Enum89(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSfixed32Fixed3290(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Fixed6491(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Float92(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSfixed32Int3293(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Int6494(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Message95(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSfixed32Sfixed3296(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sfixed6497(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Sint3298(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sint6499(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32String100(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSfixed32Uint32101(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Uint64102(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed64Bool103(populateLongMap(booleanProvider)); + builder.putAllFieldMapSfixed64Bytes104(populateLongMap(bytesProvider)); + builder.putAllFieldMapSfixed64Double105(populateLongMap(doubleProvider)); + builder.putAllFieldMapSfixed64Enum106(populateLongMap(enumProvider)); + builder.putAllFieldMapSfixed64Fixed32107(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Fixed64108(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Float109(populateLongMap(floatProvider)); + builder.putAllFieldMapSfixed64Int32110(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Int64111(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Message112(populateLongMap(messageProvider)); + builder.putAllFieldMapSfixed64Sfixed32113(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sfixed64114(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Sint32115(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sint64116(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64String117(populateLongMap(stringProvider)); + builder.putAllFieldMapSfixed64Uint32118(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Uint64119(populateLongMap(longProvider)); + builder.putAllFieldMapSint32Bool120(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSint32Bytes121(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSint32Double122(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSint32Enum123(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSint32Fixed32124(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Fixed64125(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Float126(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSint32Int32127(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Int64128(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Message129(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSint32Sfixed32130(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sfixed64131(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Sint32132(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sint64133(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32String134(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSint32Uint32135(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Uint64136(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint64Bool137(populateLongMap(booleanProvider)); + builder.putAllFieldMapSint64Bytes138(populateLongMap(bytesProvider)); + builder.putAllFieldMapSint64Double139(populateLongMap(doubleProvider)); + builder.putAllFieldMapSint64Enum140(populateLongMap(enumProvider)); + builder.putAllFieldMapSint64Fixed32141(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Fixed64142(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Float143(populateLongMap(floatProvider)); + builder.putAllFieldMapSint64Int32144(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Int64145(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Message146(populateLongMap(messageProvider)); + builder.putAllFieldMapSint64Sfixed32147(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sfixed64148(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Sint32149(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sint64150(populateLongMap(longProvider)); + builder.putAllFieldMapSint64String151(populateLongMap(stringProvider)); + builder.putAllFieldMapSint64Uint32152(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Uint64153(populateLongMap(longProvider)); + builder.putAllFieldMapStringBool154(populateStringMap(booleanProvider)); + builder.putAllFieldMapStringBytes155(populateStringMap(bytesProvider)); + builder.putAllFieldMapStringDouble156(populateStringMap(doubleProvider)); + builder.putAllFieldMapStringEnum157(populateStringMap(enumProvider)); + builder.putAllFieldMapStringFixed32158(populateStringMap(integerProvider)); + builder.putAllFieldMapStringFixed64159(populateStringMap(longProvider)); + builder.putAllFieldMapStringFloat160(populateStringMap(floatProvider)); + builder.putAllFieldMapStringInt32161(populateStringMap(integerProvider)); + builder.putAllFieldMapStringInt64162(populateStringMap(longProvider)); + builder.putAllFieldMapStringMessage163(populateStringMap(messageProvider)); + builder.putAllFieldMapStringSfixed32164(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSfixed64165(populateStringMap(longProvider)); + builder.putAllFieldMapStringSint32166(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSint64167(populateStringMap(longProvider)); + builder.putAllFieldMapStringString168(populateStringMap(stringProvider)); + builder.putAllFieldMapStringUint32169(populateStringMap(integerProvider)); + builder.putAllFieldMapStringUint64170(populateStringMap(longProvider)); + builder.putAllFieldMapUint32Bool171(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapUint32Bytes172(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapUint32Double173(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapUint32Enum174(populateIntegerMap(enumProvider)); + builder.putAllFieldMapUint32Fixed32175(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Fixed64176(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Float177(populateIntegerMap(floatProvider)); + builder.putAllFieldMapUint32Int32178(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Int64179(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Message180(populateIntegerMap(messageProvider)); + builder.putAllFieldMapUint32Sfixed32181(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sfixed64182(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Sint32183(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sint64184(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32String185(populateIntegerMap(stringProvider)); + builder.putAllFieldMapUint32Uint32186(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Uint64187(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint64Bool188(populateLongMap(booleanProvider)); + builder.putAllFieldMapUint64Bytes189(populateLongMap(bytesProvider)); + builder.putAllFieldMapUint64Double190(populateLongMap(doubleProvider)); + builder.putAllFieldMapUint64Enum191(populateLongMap(enumProvider)); + builder.putAllFieldMapUint64Fixed32192(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Fixed64193(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Float194(populateLongMap(floatProvider)); + builder.putAllFieldMapUint64Int32195(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Int64196(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Message197(populateLongMap(messageProvider)); + builder.putAllFieldMapUint64Sfixed32198(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sfixed64199(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Sint32200(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sint64201(populateLongMap(longProvider)); + builder.putAllFieldMapUint64String202(populateLongMap(stringProvider)); + builder.putAllFieldMapUint64Uint32203(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Uint64204(populateLongMap(longProvider)); + + return builder.build(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3MessageInfoFactory.java b/java/core/src/test/java/com/google/protobuf/Proto3MessageInfoFactory.java new file mode 100755 index 0000000000..364071b35d --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3MessageInfoFactory.java @@ -0,0 +1,506 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import com.google.protobuf.testing.Proto3Testing.Proto3Empty; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps; +import java.lang.reflect.Field; + +/** A factory that generates a hard-coded info for {@link Proto3Message}. */ +public final class Proto3MessageInfoFactory implements MessageInfoFactory { + private static final Proto3MessageInfoFactory INSTANCE = new Proto3MessageInfoFactory(); + + private Proto3MessageInfoFactory() {} + + public static Proto3MessageInfoFactory getInstance() { + return INSTANCE; + } + + @Override + public boolean isSupported(Class clazz) { + return true; + } + + @Override + public MessageInfo messageInfoFor(Class clazz) { + if (Proto3Message.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3Message(); + } else if (Proto3Empty.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3Empty(); + } else if (Proto3MessageWithMaps.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3MessageWithMaps(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + /** + * Creates a new hard-coded info for {@link Proto3Message}. Each time this is called, we manually + * go through the entire process of what a message would do if it self-registered its own info, + * including looking up each field by name. This is done for benchmarking purposes, so that we get + * a more accurate representation of the time it takes to perform this process. + */ + private static StructuralMessageInfo newMessageInfoForProto3Message() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(48); + lookupFieldsByName(builder); + return builder.build(); + } + + private static void lookupFieldsByName(StructuralMessageInfo.Builder builder) { + builder.withDefaultInstance(Proto3Message.getDefaultInstance()); + builder.withSyntax(ProtoSyntax.PROTO3); + builder.withField(forField(field("fieldDouble1_"), 1, FieldType.DOUBLE, true)); + builder.withField(forField(field("fieldFloat2_"), 2, FieldType.FLOAT, true)); + builder.withField(forField(field("fieldInt643_"), 3, FieldType.INT64, true)); + builder.withField(forField(field("fieldUint644_"), 4, FieldType.UINT64, true)); + builder.withField(forField(field("fieldInt325_"), 5, FieldType.INT32, true)); + builder.withField(forField(field("fieldFixed646_"), 6, FieldType.FIXED64, true)); + builder.withField(forField(field("fieldFixed327_"), 7, FieldType.FIXED32, true)); + builder.withField(forField(field("fieldBool8_"), 8, FieldType.BOOL, true)); + builder.withField(forField(field("fieldString9_"), 9, FieldType.STRING, true)); + builder.withField(forField(field("fieldMessage10_"), 10, FieldType.MESSAGE, true)); + builder.withField(forField(field("fieldBytes11_"), 11, FieldType.BYTES, true)); + builder.withField(forField(field("fieldUint3212_"), 12, FieldType.UINT32, true)); + builder.withField(forField(field("fieldEnum13_"), 13, FieldType.ENUM, true)); + builder.withField(forField(field("fieldSfixed3214_"), 14, FieldType.SFIXED32, true)); + builder.withField(forField(field("fieldSfixed6415_"), 15, FieldType.SFIXED64, true)); + builder.withField(forField(field("fieldSint3216_"), 16, FieldType.SINT32, true)); + builder.withField(forField(field("fieldSint6417_"), 17, FieldType.SINT64, true)); + builder.withField(forField(field("fieldDoubleList18_"), 18, FieldType.DOUBLE_LIST, true)); + builder.withField(forField(field("fieldFloatList19_"), 19, FieldType.FLOAT_LIST, true)); + builder.withField(forField(field("fieldInt64List20_"), 20, FieldType.INT64_LIST, true)); + builder.withField(forField(field("fieldUint64List21_"), 21, FieldType.UINT64_LIST, true)); + builder.withField(forField(field("fieldInt32List22_"), 22, FieldType.INT32_LIST, true)); + builder.withField(forField(field("fieldFixed64List23_"), 23, FieldType.FIXED64_LIST, true)); + builder.withField(forField(field("fieldFixed32List24_"), 24, FieldType.FIXED32_LIST, true)); + builder.withField(forField(field("fieldBoolList25_"), 25, FieldType.BOOL_LIST, true)); + builder.withField(forField(field("fieldStringList26_"), 26, FieldType.STRING_LIST, true)); + builder.withField( + forRepeatedMessageField( + field("fieldMessageList27_"), 27, FieldType.MESSAGE_LIST, Proto3Message.class)); + builder.withField(forField(field("fieldBytesList28_"), 28, FieldType.BYTES_LIST, true)); + builder.withField(forField(field("fieldUint32List29_"), 29, FieldType.UINT32_LIST, true)); + builder.withField(forField(field("fieldEnumList30_"), 30, FieldType.ENUM_LIST, true)); + builder.withField(forField(field("fieldSfixed32List31_"), 31, FieldType.SFIXED32_LIST, true)); + builder.withField(forField(field("fieldSfixed64List32_"), 32, FieldType.SFIXED64_LIST, true)); + builder.withField(forField(field("fieldSint32List33_"), 33, FieldType.SINT32_LIST, true)); + builder.withField(forField(field("fieldSint64List34_"), 34, FieldType.SINT64_LIST, true)); + builder.withField( + forField(field("fieldDoubleListPacked35_"), 35, FieldType.DOUBLE_LIST_PACKED, true)); + builder.withField( + forField(field("fieldFloatListPacked36_"), 36, FieldType.FLOAT_LIST_PACKED, true)); + builder.withField( + forField(field("fieldInt64ListPacked37_"), 37, FieldType.INT64_LIST_PACKED, true)); + builder.withField( + forField(field("fieldUint64ListPacked38_"), 38, FieldType.UINT64_LIST_PACKED, true)); + builder.withField( + forField(field("fieldInt32ListPacked39_"), 39, FieldType.INT32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldFixed64ListPacked40_"), 40, FieldType.FIXED64_LIST_PACKED, true)); + builder.withField( + forField(field("fieldFixed32ListPacked41_"), 41, FieldType.FIXED32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldBoolListPacked42_"), 42, FieldType.BOOL_LIST_PACKED, true)); + builder.withField( + forField(field("fieldUint32ListPacked43_"), 43, FieldType.UINT32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldEnumListPacked44_"), 44, FieldType.ENUM_LIST_PACKED, true)); + builder.withField( + forField(field("fieldSfixed32ListPacked45_"), 45, FieldType.SFIXED32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldSfixed64ListPacked46_"), 46, FieldType.SFIXED64_LIST_PACKED, true)); + builder.withField( + forField(field("fieldSint32ListPacked47_"), 47, FieldType.SINT32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldSint64ListPacked48_"), 48, FieldType.SINT64_LIST_PACKED, true)); + + OneofInfo oneof = new OneofInfo(0, field("testOneofCase_"), field("testOneof_")); + builder.withField(forOneofMemberField(53, FieldType.DOUBLE, oneof, Double.class, true, null)); + builder.withField(forOneofMemberField(54, FieldType.FLOAT, oneof, Float.class, true, null)); + builder.withField(forOneofMemberField(55, FieldType.INT64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(56, FieldType.UINT64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(57, FieldType.INT32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(58, FieldType.FIXED64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(59, FieldType.FIXED32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(60, FieldType.BOOL, oneof, Boolean.class, true, null)); + builder.withField(forOneofMemberField(61, FieldType.STRING, oneof, String.class, true, null)); + builder.withField( + forOneofMemberField(62, FieldType.MESSAGE, oneof, Proto3Message.class, true, null)); + builder.withField( + forOneofMemberField(63, FieldType.BYTES, oneof, ByteString.class, true, null)); + builder.withField(forOneofMemberField(64, FieldType.UINT32, oneof, Integer.class, true, null)); + builder.withField( + forOneofMemberField(65, FieldType.SFIXED32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(66, FieldType.SFIXED64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(67, FieldType.SINT32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(68, FieldType.SINT64, oneof, Long.class, true, null)); + } + + private StructuralMessageInfo newMessageInfoForProto3Empty() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO3); + return builder.build(); + } + + private StructuralMessageInfo newMessageInfoForProto3MessageWithMaps() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(); + builder.withSyntax(ProtoSyntax.PROTO3); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_bool_1", 1)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_bytes_2", 2)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_double_3", 3)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_enum_4", 4)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_fixed32_5", 5)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_fixed64_6", 6)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_float_7", 7)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_int32_8", 8)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_int64_9", 9)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_message_10", 10)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_sfixed32_11", 11)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_sfixed64_12", 12)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_sint32_13", 13)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_sint64_14", 14)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_string_15", 15)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_uint32_16", 16)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_uint64_17", 17)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_bool_18", 18)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_bytes_19", 19)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_double_20", 20)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_enum_21", 21)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_fixed32_22", 22)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_fixed64_23", 23)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_float_24", 24)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_int32_25", 25)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_int64_26", 26)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_message_27", 27)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_sfixed32_28", 28)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_sfixed64_29", 29)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_sint32_30", 30)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_sint64_31", 31)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_string_32", 32)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_uint32_33", 33)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_uint64_34", 34)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_bool_35", 35)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_bytes_36", 36)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_double_37", 37)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_enum_38", 38)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_fixed32_39", 39)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_fixed64_40", 40)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_float_41", 41)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_int32_42", 42)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_int64_43", 43)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_message_44", 44)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_sfixed32_45", 45)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_sfixed64_46", 46)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_sint32_47", 47)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_sint64_48", 48)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_string_49", 49)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_uint32_50", 50)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_uint64_51", 51)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_bool_52", 52)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_bytes_53", 53)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_double_54", 54)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_enum_55", 55)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_fixed32_56", 56)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_fixed64_57", 57)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_float_58", 58)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_int32_59", 59)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_int64_60", 60)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_message_61", 61)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_sfixed32_62", 62)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_sfixed64_63", 63)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_sint32_64", 64)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_sint64_65", 65)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_string_66", 66)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_uint32_67", 67)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_uint64_68", 68)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_bool_69", 69)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_bytes_70", 70)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_double_71", 71)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_enum_72", 72)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_fixed32_73", 73)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_fixed64_74", 74)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_float_75", 75)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_int32_76", 76)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_int64_77", 77)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_message_78", 78)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_sfixed32_79", 79)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_sfixed64_80", 80)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_sint32_81", 81)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_sint64_82", 82)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_string_83", 83)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_uint32_84", 84)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_uint64_85", 85)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_bool_86", 86)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_bytes_87", 87)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_double_88", 88)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_enum_89", 89)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_fixed32_90", 90)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_fixed64_91", 91)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_float_92", 92)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_int32_93", 93)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_int64_94", 94)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_message_95", 95)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_sfixed32_96", 96)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_sfixed64_97", 97)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_sint32_98", 98)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_sint64_99", 99)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_string_100", 100)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_uint32_101", 101)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_uint64_102", 102)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_bool_103", 103)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_bytes_104", 104)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_double_105", 105)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_enum_106", 106)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_fixed32_107", 107)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_fixed64_108", 108)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_float_109", 109)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_int32_110", 110)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_int64_111", 111)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_message_112", 112)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_sfixed32_113", 113)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_sfixed64_114", 114)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_sint32_115", 115)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_sint64_116", 116)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_string_117", 117)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_uint32_118", 118)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_uint64_119", 119)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_bool_120", 120)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_bytes_121", 121)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_double_122", 122)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_enum_123", 123)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_fixed32_124", 124)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_fixed64_125", 125)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_float_126", 126)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_int32_127", 127)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_int64_128", 128)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_message_129", 129)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_sfixed32_130", 130)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_sfixed64_131", 131)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_sint32_132", 132)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_sint64_133", 133)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_string_134", 134)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_uint32_135", 135)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_uint64_136", 136)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_bool_137", 137)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_bytes_138", 138)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_double_139", 139)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_enum_140", 140)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_fixed32_141", 141)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_fixed64_142", 142)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_float_143", 143)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_int32_144", 144)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_int64_145", 145)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_message_146", 146)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_sfixed32_147", 147)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_sfixed64_148", 148)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_sint32_149", 149)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_sint64_150", 150)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_string_151", 151)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_uint32_152", 152)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_uint64_153", 153)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_bool_154", 154)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_bytes_155", 155)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_double_156", 156)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_enum_157", 157)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_fixed32_158", 158)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_fixed64_159", 159)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_float_160", 160)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_int32_161", 161)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_int64_162", 162)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_message_163", 163)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_sfixed32_164", 164)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_sfixed64_165", 165)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_sint32_166", 166)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_sint64_167", 167)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_string_168", 168)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_uint32_169", 169)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_uint64_170", 170)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_bool_171", 171)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_bytes_172", 172)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_double_173", 173)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_enum_174", 174)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_fixed32_175", 175)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_fixed64_176", 176)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_float_177", 177)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_int32_178", 178)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_int64_179", 179)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_message_180", 180)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_sfixed32_181", 181)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_sfixed64_182", 182)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_sint32_183", 183)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_sint64_184", 184)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_string_185", 185)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_uint32_186", 186)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_uint64_187", 187)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_bool_188", 188)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_bytes_189", 189)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_double_190", 190)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_enum_191", 191)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_fixed32_192", 192)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_fixed64_193", 193)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_float_194", 194)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_int32_195", 195)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_int64_196", 196)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_message_197", 197)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_sfixed32_198", 198)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_sfixed64_199", 199)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_sint32_200", 200)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_sint64_201", 201)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_string_202", 202)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_uint32_203", 203)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_uint64_204", 204)); + return builder.build(); + } + + private static Field field(String name) { + return field(Proto3Message.class, name); + } + + private static Field field(Class clazz, String name) { + try { + return clazz.getDeclaredField(name); + } catch (NoSuchFieldException | SecurityException e) { + throw new RuntimeException(e); + } + } + + private static FieldInfo mapFieldInfo(Class clazz, String fieldName, int fieldNumber) { + try { + return forMapField( + field(clazz, SchemaUtil.toCamelCase(fieldName, false) + "_"), + fieldNumber, + SchemaUtil.getMapDefaultEntry(clazz, fieldName), + null); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteFactory.java b/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteFactory.java new file mode 100755 index 0000000000..2f6947da4f --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteFactory.java @@ -0,0 +1,452 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps; +import java.util.HashMap; +import java.util.Map; + +/** Creates instances of {@link Proto3MessageLite} based on the tree configuration. */ +public final class Proto3MessageLiteFactory + implements ExperimentalMessageFactory { + private final int numRepeatedFields; + private final int branchingFactor; + private final Proto3MessageLiteFactory nextLevel; + private final ExperimentalTestDataProvider data; + + public Proto3MessageLiteFactory( + int numRepeatedFields, int stringLength, int branchingFactor, int treeDepth) { + this( + new ExperimentalTestDataProvider(stringLength), + numRepeatedFields, + branchingFactor, + treeDepth); + } + + private Proto3MessageLiteFactory( + ExperimentalTestDataProvider data, + int numRepeatedFields, + int branchingFactor, + int treeDepth) { + this.numRepeatedFields = numRepeatedFields; + this.branchingFactor = branchingFactor; + this.data = data; + if (treeDepth > 0) { + nextLevel = + new Proto3MessageLiteFactory(data, numRepeatedFields, branchingFactor, treeDepth - 1); + } else { + nextLevel = null; + } + } + + @Override + public ExperimentalTestDataProvider dataProvider() { + return data; + } + + @Override + public Proto3MessageLite newMessage() { + Proto3MessageLite.Builder builder = Proto3MessageLite.newBuilder(); + builder.setFieldDouble1(data.getDouble()); + builder.setFieldFloat2(data.getFloat()); + builder.setFieldInt643(data.getLong()); + builder.setFieldUint644(data.getLong()); + builder.setFieldInt325(data.getInt()); + builder.setFieldFixed646(data.getLong()); + builder.setFieldFixed327(data.getInt()); + builder.setFieldBool8(data.getBool()); + builder.setFieldString9(data.getString()); + // We don't populate the message field. Instead we apply the branching factor to the + // repeated message field below. + builder.setFieldBytes11(data.getBytes()); + builder.setFieldUint3212(data.getInt()); + builder.setFieldEnum13Value(data.getEnum()); + builder.setFieldSfixed3214(data.getInt()); + builder.setFieldSfixed6415(data.getLong()); + builder.setFieldSint3216(data.getInt()); + builder.setFieldSint6417(data.getLong()); + + for (int i = 0; i < numRepeatedFields; ++i) { + builder.addFieldDoubleList18(data.getDouble()); + builder.addFieldFloatList19(data.getFloat()); + builder.addFieldInt64List20(data.getLong()); + builder.addFieldUint64List21(data.getLong()); + builder.addFieldInt32List22(data.getInt()); + builder.addFieldFixed64List23(data.getLong()); + builder.addFieldFixed32List24(data.getInt()); + builder.addFieldBoolList25(data.getBool()); + builder.addFieldStringList26(data.getString()); + // Repeated message field is controlled by the branching factor below. + builder.addFieldBytesList28(data.getBytes()); + builder.addFieldUint32List29(data.getInt()); + builder.addFieldEnumList30Value(data.getEnum()); + builder.addFieldSfixed32List31(data.getInt()); + builder.addFieldSfixed64List32(data.getLong()); + builder.addFieldSint32List33(data.getInt()); + builder.addFieldSint64List34(data.getLong()); + + builder.addFieldDoubleListPacked35(data.getDouble()); + builder.addFieldFloatListPacked36(data.getFloat()); + builder.addFieldInt64ListPacked37(data.getLong()); + builder.addFieldUint64ListPacked38(data.getLong()); + builder.addFieldInt32ListPacked39(data.getInt()); + builder.addFieldFixed64ListPacked40(data.getLong()); + builder.addFieldFixed32ListPacked41(data.getInt()); + builder.addFieldBoolListPacked42(data.getBool()); + builder.addFieldUint32ListPacked43(data.getInt()); + builder.addFieldEnumListPacked44Value(data.getEnum()); + builder.addFieldSfixed32ListPacked45(data.getInt()); + builder.addFieldSfixed64ListPacked46(data.getLong()); + builder.addFieldSint32ListPacked47(data.getInt()); + builder.addFieldSint64ListPacked48(data.getLong()); + } + + // Handle the branching factor. + if (nextLevel != null) { + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldMessageList27(nextLevel.newMessage()); + } + } + + return builder.build(); + } + + private interface MapValueProvider { + public T getValue(); + } + + private final MapValueProvider integerProvider = + new MapValueProvider() { + @Override + public Integer getValue() { + return data.getInt(); + } + }; + private final MapValueProvider longProvider = + new MapValueProvider() { + @Override + public Long getValue() { + return data.getLong(); + } + }; + private final MapValueProvider stringProvider = + new MapValueProvider() { + @Override + public String getValue() { + return data.getString(); + } + }; + private final MapValueProvider bytesProvider = + new MapValueProvider() { + @Override + public ByteString getValue() { + return data.getBytes(); + } + }; + private final MapValueProvider booleanProvider = + new MapValueProvider() { + @Override + public Boolean getValue() { + return data.getBool(); + } + }; + private final MapValueProvider floatProvider = + new MapValueProvider() { + @Override + public Float getValue() { + return data.getFloat(); + } + }; + private final MapValueProvider doubleProvider = + new MapValueProvider() { + @Override + public Double getValue() { + return data.getDouble(); + } + }; + private final MapValueProvider messageProvider = + new MapValueProvider() { + @Override + public Proto3MessageLite getValue() { + return newMessage(); + } + }; + private final MapValueProvider enumProvider = + new MapValueProvider() { + @Override + public Proto3MessageLite.TestEnum getValue() { + return Proto3MessageLite.TestEnum.forNumber(data.getEnum()); + } + }; + + private Map populateIntegerMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getInt(), provider.getValue()); + } + return map; + } + + private Map populateLongMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getLong(), provider.getValue()); + } + return map; + } + + private Map populateStringMap(MapValueProvider provider) { + Map map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getString(), provider.getValue()); + } + return map; + } + + private Map populateBooleanMap(MapValueProvider provider) { + Map map = new HashMap<>(); + map.put(false, provider.getValue()); + map.put(true, provider.getValue()); + return map; + } + + public Proto3MessageLiteWithMaps newMessageWithMaps() { + Proto3MessageLiteWithMaps.Builder builder = Proto3MessageLiteWithMaps.newBuilder(); + + builder.putAllFieldMapBoolBool1(populateBooleanMap(booleanProvider)); + builder.putAllFieldMapBoolBytes2(populateBooleanMap(bytesProvider)); + builder.putAllFieldMapBoolDouble3(populateBooleanMap(doubleProvider)); + builder.putAllFieldMapBoolEnum4(populateBooleanMap(enumProvider)); + builder.putAllFieldMapBoolFixed325(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolFixed646(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolFloat7(populateBooleanMap(floatProvider)); + builder.putAllFieldMapBoolInt328(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolInt649(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolMessage10(populateBooleanMap(messageProvider)); + builder.putAllFieldMapBoolSfixed3211(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSfixed6412(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolSint3213(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSint6414(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolString15(populateBooleanMap(stringProvider)); + builder.putAllFieldMapBoolUint3216(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolUint6417(populateBooleanMap(longProvider)); + builder.putAllFieldMapFixed32Bool18(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapFixed32Bytes19(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapFixed32Double20(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapFixed32Enum21(populateIntegerMap(enumProvider)); + builder.putAllFieldMapFixed32Fixed3222(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Fixed6423(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Float24(populateIntegerMap(floatProvider)); + builder.putAllFieldMapFixed32Int3225(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Int6426(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Message27(populateIntegerMap(messageProvider)); + builder.putAllFieldMapFixed32Sfixed3228(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sfixed6429(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Sint3230(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sint6431(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32String32(populateIntegerMap(stringProvider)); + builder.putAllFieldMapFixed32Uint3233(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Uint6434(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed64Bool35(populateLongMap(booleanProvider)); + builder.putAllFieldMapFixed64Bytes36(populateLongMap(bytesProvider)); + builder.putAllFieldMapFixed64Double37(populateLongMap(doubleProvider)); + builder.putAllFieldMapFixed64Enum38(populateLongMap(enumProvider)); + builder.putAllFieldMapFixed64Fixed3239(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Fixed6440(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Float41(populateLongMap(floatProvider)); + builder.putAllFieldMapFixed64Int3242(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Int6443(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Message44(populateLongMap(messageProvider)); + builder.putAllFieldMapFixed64Sfixed3245(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sfixed6446(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Sint3247(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sint6448(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64String49(populateLongMap(stringProvider)); + builder.putAllFieldMapFixed64Uint3250(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Uint6451(populateLongMap(longProvider)); + builder.putAllFieldMapInt32Bool52(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapInt32Bytes53(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapInt32Double54(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapInt32Enum55(populateIntegerMap(enumProvider)); + builder.putAllFieldMapInt32Fixed3256(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Fixed6457(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Float58(populateIntegerMap(floatProvider)); + builder.putAllFieldMapInt32Int3259(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Int6460(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Message61(populateIntegerMap(messageProvider)); + builder.putAllFieldMapInt32Sfixed3262(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sfixed6463(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Sint3264(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sint6465(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32String66(populateIntegerMap(stringProvider)); + builder.putAllFieldMapInt32Uint3267(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Uint6468(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt64Bool69(populateLongMap(booleanProvider)); + builder.putAllFieldMapInt64Bytes70(populateLongMap(bytesProvider)); + builder.putAllFieldMapInt64Double71(populateLongMap(doubleProvider)); + builder.putAllFieldMapInt64Enum72(populateLongMap(enumProvider)); + builder.putAllFieldMapInt64Fixed3273(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Fixed6474(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Float75(populateLongMap(floatProvider)); + builder.putAllFieldMapInt64Int3276(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Int6477(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Message78(populateLongMap(messageProvider)); + builder.putAllFieldMapInt64Sfixed3279(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sfixed6480(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Sint3281(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sint6482(populateLongMap(longProvider)); + builder.putAllFieldMapInt64String83(populateLongMap(stringProvider)); + builder.putAllFieldMapInt64Uint3284(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Uint6485(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed32Bool86(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSfixed32Bytes87(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSfixed32Double88(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSfixed32Enum89(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSfixed32Fixed3290(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Fixed6491(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Float92(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSfixed32Int3293(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Int6494(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Message95(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSfixed32Sfixed3296(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sfixed6497(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Sint3298(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sint6499(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32String100(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSfixed32Uint32101(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Uint64102(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed64Bool103(populateLongMap(booleanProvider)); + builder.putAllFieldMapSfixed64Bytes104(populateLongMap(bytesProvider)); + builder.putAllFieldMapSfixed64Double105(populateLongMap(doubleProvider)); + builder.putAllFieldMapSfixed64Enum106(populateLongMap(enumProvider)); + builder.putAllFieldMapSfixed64Fixed32107(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Fixed64108(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Float109(populateLongMap(floatProvider)); + builder.putAllFieldMapSfixed64Int32110(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Int64111(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Message112(populateLongMap(messageProvider)); + builder.putAllFieldMapSfixed64Sfixed32113(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sfixed64114(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Sint32115(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sint64116(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64String117(populateLongMap(stringProvider)); + builder.putAllFieldMapSfixed64Uint32118(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Uint64119(populateLongMap(longProvider)); + builder.putAllFieldMapSint32Bool120(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSint32Bytes121(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSint32Double122(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSint32Enum123(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSint32Fixed32124(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Fixed64125(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Float126(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSint32Int32127(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Int64128(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Message129(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSint32Sfixed32130(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sfixed64131(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Sint32132(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sint64133(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32String134(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSint32Uint32135(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Uint64136(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint64Bool137(populateLongMap(booleanProvider)); + builder.putAllFieldMapSint64Bytes138(populateLongMap(bytesProvider)); + builder.putAllFieldMapSint64Double139(populateLongMap(doubleProvider)); + builder.putAllFieldMapSint64Enum140(populateLongMap(enumProvider)); + builder.putAllFieldMapSint64Fixed32141(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Fixed64142(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Float143(populateLongMap(floatProvider)); + builder.putAllFieldMapSint64Int32144(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Int64145(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Message146(populateLongMap(messageProvider)); + builder.putAllFieldMapSint64Sfixed32147(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sfixed64148(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Sint32149(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sint64150(populateLongMap(longProvider)); + builder.putAllFieldMapSint64String151(populateLongMap(stringProvider)); + builder.putAllFieldMapSint64Uint32152(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Uint64153(populateLongMap(longProvider)); + builder.putAllFieldMapStringBool154(populateStringMap(booleanProvider)); + builder.putAllFieldMapStringBytes155(populateStringMap(bytesProvider)); + builder.putAllFieldMapStringDouble156(populateStringMap(doubleProvider)); + builder.putAllFieldMapStringEnum157(populateStringMap(enumProvider)); + builder.putAllFieldMapStringFixed32158(populateStringMap(integerProvider)); + builder.putAllFieldMapStringFixed64159(populateStringMap(longProvider)); + builder.putAllFieldMapStringFloat160(populateStringMap(floatProvider)); + builder.putAllFieldMapStringInt32161(populateStringMap(integerProvider)); + builder.putAllFieldMapStringInt64162(populateStringMap(longProvider)); + builder.putAllFieldMapStringMessage163(populateStringMap(messageProvider)); + builder.putAllFieldMapStringSfixed32164(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSfixed64165(populateStringMap(longProvider)); + builder.putAllFieldMapStringSint32166(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSint64167(populateStringMap(longProvider)); + builder.putAllFieldMapStringString168(populateStringMap(stringProvider)); + builder.putAllFieldMapStringUint32169(populateStringMap(integerProvider)); + builder.putAllFieldMapStringUint64170(populateStringMap(longProvider)); + builder.putAllFieldMapUint32Bool171(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapUint32Bytes172(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapUint32Double173(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapUint32Enum174(populateIntegerMap(enumProvider)); + builder.putAllFieldMapUint32Fixed32175(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Fixed64176(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Float177(populateIntegerMap(floatProvider)); + builder.putAllFieldMapUint32Int32178(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Int64179(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Message180(populateIntegerMap(messageProvider)); + builder.putAllFieldMapUint32Sfixed32181(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sfixed64182(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Sint32183(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sint64184(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32String185(populateIntegerMap(stringProvider)); + builder.putAllFieldMapUint32Uint32186(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Uint64187(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint64Bool188(populateLongMap(booleanProvider)); + builder.putAllFieldMapUint64Bytes189(populateLongMap(bytesProvider)); + builder.putAllFieldMapUint64Double190(populateLongMap(doubleProvider)); + builder.putAllFieldMapUint64Enum191(populateLongMap(enumProvider)); + builder.putAllFieldMapUint64Fixed32192(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Fixed64193(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Float194(populateLongMap(floatProvider)); + builder.putAllFieldMapUint64Int32195(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Int64196(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Message197(populateLongMap(messageProvider)); + builder.putAllFieldMapUint64Sfixed32198(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sfixed64199(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Sint32200(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sint64201(populateLongMap(longProvider)); + builder.putAllFieldMapUint64String202(populateLongMap(stringProvider)); + builder.putAllFieldMapUint64Uint32203(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Uint64204(populateLongMap(longProvider)); + + return builder.build(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteInfoFactory.java b/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteInfoFactory.java new file mode 100755 index 0000000000..66e406af7d --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteInfoFactory.java @@ -0,0 +1,816 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps; +import java.lang.reflect.Field; + +/** A factory that generates a hard-coded info for {@link Proto3MessageLite}. */ +public final class Proto3MessageLiteInfoFactory implements MessageInfoFactory { + private static final Proto3MessageLiteInfoFactory instanceForRawMessageInfo = + new Proto3MessageLiteInfoFactory(true); + private static final Proto3MessageLiteInfoFactory instanceForStructuralMessageInfo = + new Proto3MessageLiteInfoFactory(false); + + public static Proto3MessageLiteInfoFactory getInstanceForRawMessageInfo() { + return instanceForRawMessageInfo; + } + + public static Proto3MessageLiteInfoFactory getInstanceForStructuralMessageInfo() { + return instanceForStructuralMessageInfo; + } + + private final boolean produceRawMessageInfo; + + private Proto3MessageLiteInfoFactory(boolean produceRawMessageInfo) { + this.produceRawMessageInfo = produceRawMessageInfo; + } + + @Override + public boolean isSupported(Class clazz) { + return true; + } + + @Override + public MessageInfo messageInfoFor(Class clazz) { + return produceRawMessageInfo ? rawMessageInfoFor(clazz) : structuralMessageInfoFor(clazz); + } + + private MessageInfo rawMessageInfoFor(Class clazz) { + if (Proto3MessageLite.class.isAssignableFrom(clazz)) { + return newRawMessageInfoForProto3MessageLite(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + private MessageInfo newRawMessageInfoForProto3MessageLite() { + java.lang.Object[] objects = + new java.lang.Object[] { + "testOneof_", + "testOneofCase_", + "fieldDouble1_", + "fieldFloat2_", + "fieldInt643_", + "fieldUint644_", + "fieldInt325_", + "fieldFixed646_", + "fieldFixed327_", + "fieldBool8_", + "fieldString9_", + "fieldMessage10_", + "fieldBytes11_", + "fieldUint3212_", + "fieldEnum13_", + "fieldSfixed3214_", + "fieldSfixed6415_", + "fieldSint3216_", + "fieldSint6417_", + "fieldDoubleList18_", + "fieldFloatList19_", + "fieldInt64List20_", + "fieldUint64List21_", + "fieldInt32List22_", + "fieldFixed64List23_", + "fieldFixed32List24_", + "fieldBoolList25_", + "fieldStringList26_", + "fieldMessageList27_", + Proto3MessageLite.class, + "fieldBytesList28_", + "fieldUint32List29_", + "fieldEnumList30_", + "fieldSfixed32List31_", + "fieldSfixed64List32_", + "fieldSint32List33_", + "fieldSint64List34_", + "fieldDoubleListPacked35_", + "fieldFloatListPacked36_", + "fieldInt64ListPacked37_", + "fieldUint64ListPacked38_", + "fieldInt32ListPacked39_", + "fieldFixed64ListPacked40_", + "fieldFixed32ListPacked41_", + "fieldBoolListPacked42_", + "fieldUint32ListPacked43_", + "fieldEnumListPacked44_", + "fieldSfixed32ListPacked45_", + "fieldSfixed64ListPacked46_", + "fieldSint32ListPacked47_", + "fieldSint64ListPacked48_", + Proto3MessageLite.class, + }; + // To update this after a proto change, run protoc on proto3_message_lite.proto and copy over + // the content of the generated buildMessageInfo() method here. + java.lang.String info = + "\u0000@\u0001\u0000\u0001D@\u0000\u001f\u0000\u0001\u0000\u0002\u0001\u0003\u0002" + + "\u0004\u0003\u0005\u0004\u0006\u0005\u0007\u0006\b\u0007\t\u0208\n\t\u000b\n\f\u000b" + + "\r\f\u000e\r\u000f\u000e\u0010\u000f\u0011\u0010\u0012\u0012\u0013\u0013\u0014\u0014" + + "\u0015\u0015\u0016\u0016\u0017\u0017\u0018\u0018\u0019\u0019\u001a\u021a\u001b\u001b" + + "\u001c\u001c\u001d\u001d\u001e\u001e\u001f\u001f !!\"\"##$$%%&&\'\'(())**++,,--" + + "..//0053\u000064\u000075\u000086\u000097\u0000:8\u0000;9\u0000<:\u0000=\u023b\u0000" + + "><\u0000?=\u0000@>\u0000A@\u0000BA\u0000CB\u0000DC\u0000"; + return new RawMessageInfo(Proto3MessageLite.getDefaultInstance(), info, objects); + } + + private MessageInfo structuralMessageInfoFor(Class clazz) { + if (Proto3MessageLite.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3MessageLite(); + } else if (Proto3EmptyLite.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3EmptyLite(); + } else if (Proto3MessageLiteWithMaps.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3MessageLiteWithMaps(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + /** + * Creates a new hard-coded info for {@link Proto3MessageLite}. Each time this is called, we + * manually go through the entire process of what a message would do if it self-registered its own + * info, including looking up each field by name. This is done for benchmarking purposes, so that + * we get a more accurate representation of the time it takes to perform this process. + */ + private static StructuralMessageInfo newMessageInfoForProto3MessageLite() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(48); + lookupFieldsByName(builder); + return builder.build(); + } + + private static void lookupFieldsByName(StructuralMessageInfo.Builder builder) { + builder.withDefaultInstance(Proto3MessageLite.getDefaultInstance()); + builder.withSyntax(ProtoSyntax.PROTO3); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldDouble1_"), 1, FieldType.DOUBLE, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldFloat2_"), 2, FieldType.FLOAT, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldInt643_"), 3, FieldType.INT64, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldUint644_"), 4, FieldType.UINT64, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldInt325_"), 5, FieldType.INT32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldFixed646_"), 6, FieldType.FIXED64, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldFixed327_"), 7, FieldType.FIXED32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldBool8_"), 8, FieldType.BOOL, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldString9_"), 9, FieldType.STRING, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldMessage10_"), 10, FieldType.MESSAGE, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldBytes11_"), 11, FieldType.BYTES, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldUint3212_"), 12, FieldType.UINT32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldEnum13_"), 13, FieldType.ENUM, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldSfixed3214_"), 14, FieldType.SFIXED32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldSfixed6415_"), 15, FieldType.SFIXED64, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldSint3216_"), 16, FieldType.SINT32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldSint6417_"), 17, FieldType.SINT64, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldDoubleList18_"), 18, FieldType.DOUBLE_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFloatList19_"), 19, FieldType.FLOAT_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldInt64List20_"), 20, FieldType.INT64_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldUint64List21_"), 21, FieldType.UINT64_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldInt32List22_"), 22, FieldType.INT32_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFixed64List23_"), + 23, + FieldType.FIXED64_LIST, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFixed32List24_"), + 24, + FieldType.FIXED32_LIST, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldBoolList25_"), 25, FieldType.BOOL_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldStringList26_"), 26, FieldType.STRING_LIST, true)); + builder.withField( + forRepeatedMessageField( + field(Proto3MessageLite.class, "fieldMessageList27_"), + 27, + FieldType.MESSAGE_LIST, + Proto3MessageLite.class)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldBytesList28_"), 28, FieldType.BYTES_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldUint32List29_"), 29, FieldType.UINT32_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldEnumList30_"), 30, FieldType.ENUM_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSfixed32List31_"), + 31, + FieldType.SFIXED32_LIST, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSfixed64List32_"), + 32, + FieldType.SFIXED64_LIST, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSint32List33_"), 33, FieldType.SINT32_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSint64List34_"), 34, FieldType.SINT64_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldDoubleListPacked35_"), + 35, + FieldType.DOUBLE_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFloatListPacked36_"), + 36, + FieldType.FLOAT_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldInt64ListPacked37_"), + 37, + FieldType.INT64_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldUint64ListPacked38_"), + 38, + FieldType.UINT64_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldInt32ListPacked39_"), + 39, + FieldType.INT32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFixed64ListPacked40_"), + 40, + FieldType.FIXED64_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFixed32ListPacked41_"), + 41, + FieldType.FIXED32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldBoolListPacked42_"), + 42, + FieldType.BOOL_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldUint32ListPacked43_"), + 43, + FieldType.UINT32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldEnumListPacked44_"), + 44, + FieldType.ENUM_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSfixed32ListPacked45_"), + 45, + FieldType.SFIXED32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSfixed64ListPacked46_"), + 46, + FieldType.SFIXED64_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSint32ListPacked47_"), + 47, + FieldType.SINT32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSint64ListPacked48_"), + 48, + FieldType.SINT64_LIST_PACKED, + true)); + + OneofInfo oneof = + new OneofInfo( + 0, + field(Proto3MessageLite.class, "testOneofCase_"), + field(Proto3MessageLite.class, "testOneof_")); + builder.withField(forOneofMemberField(53, FieldType.DOUBLE, oneof, Double.class, true, null)); + builder.withField(forOneofMemberField(54, FieldType.FLOAT, oneof, Float.class, true, null)); + builder.withField(forOneofMemberField(55, FieldType.INT64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(56, FieldType.UINT64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(57, FieldType.INT32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(58, FieldType.FIXED64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(59, FieldType.FIXED32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(60, FieldType.BOOL, oneof, Boolean.class, true, null)); + builder.withField(forOneofMemberField(61, FieldType.STRING, oneof, String.class, true, null)); + builder.withField( + forOneofMemberField(62, FieldType.MESSAGE, oneof, Proto3MessageLite.class, true, null)); + builder.withField( + forOneofMemberField(63, FieldType.BYTES, oneof, ByteString.class, true, null)); + builder.withField(forOneofMemberField(64, FieldType.UINT32, oneof, Integer.class, true, null)); + builder.withField( + forOneofMemberField(65, FieldType.SFIXED32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(66, FieldType.SFIXED64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(67, FieldType.SINT32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(68, FieldType.SINT64, oneof, Long.class, true, null)); + } + + private StructuralMessageInfo newMessageInfoForProto3EmptyLite() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO3); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto3MessageLiteWithMaps() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_bool_1", 1)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_bytes_2", 2)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_double_3", 3)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_enum_4", 4)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_fixed32_5", 5)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_fixed64_6", 6)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_float_7", 7)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_int32_8", 8)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_int64_9", 9)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_message_10", 10)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_sfixed32_11", 11)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_sfixed64_12", 12)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_sint32_13", 13)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_sint64_14", 14)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_string_15", 15)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_uint32_16", 16)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_uint64_17", 17)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_bool_18", 18)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_bytes_19", 19)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_double_20", 20)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_enum_21", 21)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_fixed32_22", 22)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_fixed64_23", 23)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_float_24", 24)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_int32_25", 25)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_int64_26", 26)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_message_27", 27)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_sfixed32_28", 28)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_sfixed64_29", 29)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_sint32_30", 30)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_sint64_31", 31)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_string_32", 32)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_uint32_33", 33)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_uint64_34", 34)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_bool_35", 35)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_bytes_36", 36)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_double_37", 37)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_enum_38", 38)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_fixed32_39", 39)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_fixed64_40", 40)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_float_41", 41)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_int32_42", 42)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_int64_43", 43)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_message_44", 44)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_sfixed32_45", 45)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_sfixed64_46", 46)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_sint32_47", 47)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_sint64_48", 48)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_string_49", 49)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_uint32_50", 50)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_uint64_51", 51)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_bool_52", 52)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_bytes_53", 53)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_double_54", 54)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_enum_55", 55)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_fixed32_56", 56)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_fixed64_57", 57)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_float_58", 58)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_int32_59", 59)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_int64_60", 60)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_message_61", 61)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_sfixed32_62", 62)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_sfixed64_63", 63)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_sint32_64", 64)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_sint64_65", 65)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_string_66", 66)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_uint32_67", 67)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_uint64_68", 68)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_bool_69", 69)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_bytes_70", 70)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_double_71", 71)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_enum_72", 72)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_fixed32_73", 73)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_fixed64_74", 74)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_float_75", 75)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_int32_76", 76)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_int64_77", 77)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_message_78", 78)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_sfixed32_79", 79)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_sfixed64_80", 80)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_sint32_81", 81)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_sint64_82", 82)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_string_83", 83)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_uint32_84", 84)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_uint64_85", 85)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_bool_86", 86)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_bytes_87", 87)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_double_88", 88)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_enum_89", 89)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_fixed32_90", 90)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_fixed64_91", 91)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_float_92", 92)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_int32_93", 93)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_int64_94", 94)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_message_95", 95)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_sfixed32_96", 96)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_sfixed64_97", 97)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_sint32_98", 98)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_sint64_99", 99)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_string_100", 100)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_uint32_101", 101)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_uint64_102", 102)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_bool_103", 103)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_bytes_104", 104)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_double_105", 105)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_enum_106", 106)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_fixed32_107", 107)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_fixed64_108", 108)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_float_109", 109)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_int32_110", 110)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_int64_111", 111)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_message_112", 112)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_sfixed32_113", 113)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_sfixed64_114", 114)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_sint32_115", 115)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_sint64_116", 116)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_string_117", 117)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_uint32_118", 118)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_uint64_119", 119)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_bool_120", 120)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_bytes_121", 121)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_double_122", 122)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_enum_123", 123)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_fixed32_124", 124)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_fixed64_125", 125)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_float_126", 126)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_int32_127", 127)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_int64_128", 128)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_message_129", 129)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_sfixed32_130", 130)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_sfixed64_131", 131)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_sint32_132", 132)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_sint64_133", 133)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_string_134", 134)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_uint32_135", 135)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_uint64_136", 136)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_bool_137", 137)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_bytes_138", 138)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_double_139", 139)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_enum_140", 140)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_fixed32_141", 141)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_fixed64_142", 142)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_float_143", 143)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_int32_144", 144)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_int64_145", 145)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_message_146", 146)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_sfixed32_147", 147)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_sfixed64_148", 148)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_sint32_149", 149)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_sint64_150", 150)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_string_151", 151)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_uint32_152", 152)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_uint64_153", 153)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_bool_154", 154)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_bytes_155", 155)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_double_156", 156)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_enum_157", 157)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_fixed32_158", 158)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_fixed64_159", 159)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_float_160", 160)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_int32_161", 161)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_int64_162", 162)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_message_163", 163)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_sfixed32_164", 164)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_sfixed64_165", 165)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_sint32_166", 166)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_sint64_167", 167)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_string_168", 168)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_uint32_169", 169)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_uint64_170", 170)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_bool_171", 171)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_bytes_172", 172)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_double_173", 173)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_enum_174", 174)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_fixed32_175", 175)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_fixed64_176", 176)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_float_177", 177)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_int32_178", 178)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_int64_179", 179)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_message_180", 180)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_sfixed32_181", 181)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_sfixed64_182", 182)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_sint32_183", 183)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_sint64_184", 184)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_string_185", 185)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_uint32_186", 186)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_uint64_187", 187)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_bool_188", 188)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_bytes_189", 189)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_double_190", 190)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_enum_191", 191)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_fixed32_192", 192)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_fixed64_193", 193)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_float_194", 194)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_int32_195", 195)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_int64_196", 196)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_message_197", 197)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_sfixed32_198", 198)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_sfixed64_199", 199)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_sint32_200", 200)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_sint64_201", 201)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_string_202", 202)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_uint32_203", 203)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_uint64_204", 204)); + + return builder.build(); + } + + private static Field field(Class clazz, String name) { + try { + return clazz.getDeclaredField(name); + } catch (NoSuchFieldException | SecurityException e) { + throw new RuntimeException(e); + } + } + + private static FieldInfo mapFieldInfo(Class clazz, String fieldName, int fieldNumber) { + try { + return forMapField( + field(clazz, SchemaUtil.toCamelCase(fieldName, false) + "_"), + fieldNumber, + SchemaUtil.getMapDefaultEntry(clazz, fieldName), + null); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java new file mode 100755 index 0000000000..5cd145be74 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java @@ -0,0 +1,48 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto3SchemaTest extends AbstractProto3SchemaTest { + @Override + protected void registerSchemas() { + TestSchemas.registerGenericProto3Schemas(); + } + + @Override + protected Schema schema() { + return TestSchemas.genericProto3Schema; + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java b/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java index 0a2c3e3728..67b2881a43 100644 --- a/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java @@ -38,9 +38,7 @@ import java.util.Iterator; import java.util.List; import junit.framework.TestCase; -/** - * Tests for {@link ProtobufArrayList}. - */ +/** Tests for {@link ProtobufArrayList}. */ public class ProtobufArrayListTest extends TestCase { private static final ProtobufArrayList UNARY_LIST = newImmutableProtoArrayList(1); diff --git a/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java b/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java index edbd0afd75..c42813c808 100644 --- a/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java +++ b/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java @@ -37,9 +37,8 @@ import java.util.List; import junit.framework.TestCase; /** - * Tests for {@link RepeatedFieldBuilderV3}. This tests basic functionality. - * More extensive testing is provided via other tests that exercise the - * builder. + * Tests for {@link RepeatedFieldBuilderV3}. This tests basic functionality. More extensive testing is + * provided via other tests that exercise the builder. * * @author jonp@google.com (Jon Perlow) */ @@ -47,8 +46,8 @@ public class RepeatedFieldBuilderV3Test extends TestCase { public void testBasicUse() { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - RepeatedFieldBuilderV3 builder = newRepeatedFieldBuilderV3(mockParent); + RepeatedFieldBuilderV3 builder = + newRepeatedFieldBuilderV3(mockParent); builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); assertEquals(0, builder.getMessage(0).getOptionalInt32()); @@ -68,8 +67,8 @@ public class RepeatedFieldBuilderV3Test extends TestCase { public void testGoingBackAndForth() { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - RepeatedFieldBuilderV3 builder = newRepeatedFieldBuilderV3(mockParent); + RepeatedFieldBuilderV3 builder = + newRepeatedFieldBuilderV3(mockParent); builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); assertEquals(0, builder.getMessage(0).getOptionalInt32()); @@ -89,7 +88,7 @@ public class RepeatedFieldBuilderV3Test extends TestCase { list = builder.build(); assertEquals(2, list.size()); assertEquals(0, list.get(0).getOptionalInt32()); - assertEquals("foo", list.get(0).getOptionalString()); + assertEquals("foo", list.get(0).getOptionalString()); assertEquals(1, list.get(1).getOptionalInt32()); assertIsUnmodifiable(list); assertEquals(1, mockParent.getInvalidationCount()); @@ -97,12 +96,11 @@ public class RepeatedFieldBuilderV3Test extends TestCase { public void testVariousMethods() { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - RepeatedFieldBuilderV3 builder = newRepeatedFieldBuilderV3(mockParent); + RepeatedFieldBuilderV3 builder = + newRepeatedFieldBuilderV3(mockParent); builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(2).build()); - builder.addBuilder(0, TestAllTypes.getDefaultInstance()) - .setOptionalInt32(0); + builder.addBuilder(0, TestAllTypes.getDefaultInstance()).setOptionalInt32(0); builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3); assertEquals(0, builder.getMessage(0).getOptionalInt32()); @@ -139,11 +137,10 @@ public class RepeatedFieldBuilderV3Test extends TestCase { public void testLists() { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - RepeatedFieldBuilderV3 builder = newRepeatedFieldBuilderV3(mockParent); + RepeatedFieldBuilderV3 builder = + newRepeatedFieldBuilderV3(mockParent); builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); - builder.addMessage(0, - TestAllTypes.newBuilder().setOptionalInt32(0).build()); + builder.addMessage(0, TestAllTypes.newBuilder().setOptionalInt32(0).build()); assertEquals(0, builder.getMessage(0).getOptionalInt32()); assertEquals(1, builder.getMessage(1).getOptionalInt32()); @@ -178,11 +175,9 @@ public class RepeatedFieldBuilderV3Test extends TestCase { } } - private RepeatedFieldBuilderV3 + private RepeatedFieldBuilderV3 newRepeatedFieldBuilderV3(GeneratedMessage.BuilderParent parent) { - return new RepeatedFieldBuilderV3(Collections.emptyList(), false, - parent, false); + return new RepeatedFieldBuilderV3( + Collections.emptyList(), false, parent, false); } } diff --git a/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java b/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java index dc56f2e9ac..d7262161dd 100644 --- a/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java +++ b/java/core/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java @@ -34,8 +34,8 @@ import java.io.UnsupportedEncodingException; import java.util.Iterator; /** - * This class tests {@link RopeByteString#substring(int, int)} by inheriting the tests from - * {@link LiteralByteStringTest}. Only a couple of methods are overridden. + * This class tests {@link RopeByteString#substring(int, int)} by inheriting the tests from {@link + * LiteralByteStringTest}. Only a couple of methods are overridden. * * @author carlanton@google.com (Carl Haverl) */ @@ -61,8 +61,8 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest { @Override public void testGetTreeDepth() { - assertEquals(classUnderTest + " must have the expected tree depth", - 3, stringUnderTest.getTreeDepth()); + assertEquals( + classUnderTest + " must have the expected tree depth", 3, stringUnderTest.getTreeDepth()); } @Override @@ -84,15 +84,18 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest { testString = testString.substring(2, testString.length() - 6); unicode = unicode.substring(2, unicode.size() - 6); - assertEquals(classUnderTest + " from string must have the expected type", - classUnderTest, getActualClassName(unicode)); + assertEquals( + classUnderTest + " from string must have the expected type", + classUnderTest, + getActualClassName(unicode)); String roundTripString = unicode.toString(UTF_8); - assertEquals(classUnderTest + " unicode bytes must match", - testString, roundTripString); + assertEquals(classUnderTest + " unicode bytes must match", testString, roundTripString); ByteString flatString = ByteString.copyFromUtf8(testString); assertEquals(classUnderTest + " string must equal the flat string", flatString, unicode); - assertEquals(classUnderTest + " string must must have same hashCode as the flat string", - flatString.hashCode(), unicode.hashCode()); + assertEquals( + classUnderTest + " string must must have same hashCode as the flat string", + flatString.hashCode(), + unicode.hashCode()); } @Override @@ -114,14 +117,17 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest { testString = testString.substring(2, testString.length() - 6); unicode = unicode.substring(2, unicode.size() - 6); - assertEquals(classUnderTest + " from string must have the expected type", - classUnderTest, getActualClassName(unicode)); + assertEquals( + classUnderTest + " from string must have the expected type", + classUnderTest, + getActualClassName(unicode)); String roundTripString = unicode.toString(Internal.UTF_8); - assertEquals(classUnderTest + " unicode bytes must match", - testString, roundTripString); + assertEquals(classUnderTest + " unicode bytes must match", testString, roundTripString); ByteString flatString = ByteString.copyFromUtf8(testString); assertEquals(classUnderTest + " string must equal the flat string", flatString, unicode); - assertEquals(classUnderTest + " string must must have same hashCode as the flat string", - flatString.hashCode(), unicode.hashCode()); + assertEquals( + classUnderTest + " string must must have same hashCode as the flat string", + flatString.hashCode(), + unicode.hashCode()); } } diff --git a/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java index 4ec3a40988..9299267212 100644 --- a/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -40,8 +40,8 @@ import java.util.Arrays; import java.util.Iterator; /** - * This class tests {@link RopeByteString} by inheriting the tests from - * {@link LiteralByteStringTest}. Only a couple of methods are overridden. + * This class tests {@link RopeByteString} by inheriting the tests from {@link + * LiteralByteStringTest}. Only a couple of methods are overridden. * *

A full test of the result of {@link RopeByteString#substring(int, int)} is found in the * separate class {@link RopeByteStringSubstringTest}. @@ -62,10 +62,25 @@ public class RopeByteStringTest extends LiteralByteStringTest { expectedHashCode = -1214197238; } + public void testMinLengthByDepth() { + // minLengthByDepth should match the Fibonacci sequence + int a = 1; + int b = 1; + int i; + for (i = 0; a > 0; i++) { + assertEquals(a, RopeByteString.minLengthByDepth[i]); + int c = a + b; + a = b; + b = c; + } + assertEquals(Integer.MAX_VALUE, RopeByteString.minLengthByDepth[i]); + assertEquals(i + 1, RopeByteString.minLengthByDepth.length); + } + @Override public void testGetTreeDepth() { - assertEquals(classUnderTest + " must have the expected tree depth", - 4, stringUnderTest.getTreeDepth()); + assertEquals( + classUnderTest + " must have the expected tree depth", 4, stringUnderTest.getTreeDepth()); } public void testBalance() { @@ -79,17 +94,22 @@ public class RopeByteStringTest extends LiteralByteStringTest { concatenated = concatenated.concat(ByteString.copyFrom(testBytes, i * pieceSize, pieceSize)); } - assertEquals(classUnderTest + " from string must have the expected type", - classUnderTest, getActualClassName(concatenated)); - assertTrue(classUnderTest + " underlying bytes must match after balancing", + assertEquals( + classUnderTest + " from string must have the expected type", + classUnderTest, + getActualClassName(concatenated)); + assertTrue( + classUnderTest + " underlying bytes must match after balancing", Arrays.equals(testBytes, concatenated.toByteArray())); ByteString testString = ByteString.copyFrom(testBytes); - assertTrue(classUnderTest + " balanced string must equal flat string", - concatenated.equals(testString)); - assertTrue(classUnderTest + " flat string must equal balanced string", - testString.equals(concatenated)); - assertEquals(classUnderTest + " balanced string must have same hash code as flat string", - testString.hashCode(), concatenated.hashCode()); + assertEquals( + classUnderTest + " balanced string must equal flat string", testString, concatenated); + assertEquals( + classUnderTest + " flat string must equal balanced string", concatenated, testString); + assertEquals( + classUnderTest + " balanced string must have same hash code as flat string", + testString.hashCode(), + concatenated.hashCode()); } @Override @@ -107,15 +127,18 @@ public class RopeByteStringTest extends LiteralByteStringTest { } String testString = builder.toString(); - assertEquals(classUnderTest + " from string must have the expected type", - classUnderTest, getActualClassName(unicode)); + assertEquals( + classUnderTest + " from string must have the expected type", + classUnderTest, + getActualClassName(unicode)); String roundTripString = unicode.toString(UTF_8); - assertEquals(classUnderTest + " unicode bytes must match", - testString, roundTripString); + assertEquals(classUnderTest + " unicode bytes must match", testString, roundTripString); ByteString flatString = ByteString.copyFromUtf8(testString); assertEquals(classUnderTest + " string must equal the flat string", flatString, unicode); - assertEquals(classUnderTest + " string must must have same hashCode as the flat string", - flatString.hashCode(), unicode.hashCode()); + assertEquals( + classUnderTest + " string must must have same hashCode as the flat string", + flatString.hashCode(), + unicode.hashCode()); } @Override @@ -133,30 +156,34 @@ public class RopeByteStringTest extends LiteralByteStringTest { } String testString = builder.toString(); - assertEquals(classUnderTest + " from string must have the expected type", - classUnderTest, getActualClassName(unicode)); + assertEquals( + classUnderTest + " from string must have the expected type", + classUnderTest, + getActualClassName(unicode)); String roundTripString = unicode.toString(Internal.UTF_8); - assertEquals(classUnderTest + " unicode bytes must match", - testString, roundTripString); + assertEquals(classUnderTest + " unicode bytes must match", testString, roundTripString); ByteString flatString = ByteString.copyFromUtf8(testString); assertEquals(classUnderTest + " string must equal the flat string", flatString, unicode); - assertEquals(classUnderTest + " string must must have same hashCode as the flat string", - flatString.hashCode(), unicode.hashCode()); + assertEquals( + classUnderTest + " string must must have same hashCode as the flat string", + flatString.hashCode(), + unicode.hashCode()); } @Override public void testToString_returnsCanonicalEmptyString() { RopeByteString ropeByteString = RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); - assertSame(classUnderTest + " must be the same string references", - ByteString.EMPTY.toString(Internal.UTF_8), ropeByteString.toString(Internal.UTF_8)); + assertSame( + classUnderTest + " must be the same string references", + ByteString.EMPTY.toString(Internal.UTF_8), + ropeByteString.toString(Internal.UTF_8)); } @Override public void testToString_raisesException() { try { - ByteString byteString = - RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); + ByteString byteString = RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); byteString.toString("invalid"); fail("Should have thrown an exception."); } catch (UnsupportedEncodingException expected) { @@ -164,8 +191,9 @@ public class RopeByteStringTest extends LiteralByteStringTest { } try { - ByteString byteString = RopeByteString.concatenate(ByteString.copyFromUtf8("foo"), - ByteString.copyFromUtf8("bar")); + ByteString byteString = + RopeByteString.concatenate( + ByteString.copyFromUtf8("foo"), ByteString.copyFromUtf8("bar")); byteString.toString("invalid"); fail("Should have thrown an exception."); } catch (UnsupportedEncodingException expected) { diff --git a/java/core/src/test/java/com/google/protobuf/ServiceTest.java b/java/core/src/test/java/com/google/protobuf/ServiceTest.java index b895ad8d36..1592433c2f 100644 --- a/java/core/src/test/java/com/google/protobuf/ServiceTest.java +++ b/java/core/src/test/java/com/google/protobuf/ServiceTest.java @@ -32,7 +32,6 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.FileDescriptor; import com.google.protobuf.Descriptors.MethodDescriptor; -import google.protobuf.no_generic_services_test.UnittestNoGenericServices; import protobuf_unittest.MessageWithNoOuter; import protobuf_unittest.ServiceWithNoOuter; import protobuf_unittest.UnittestProto.BarRequest; @@ -41,7 +40,7 @@ import protobuf_unittest.UnittestProto.FooRequest; import protobuf_unittest.UnittestProto.FooResponse; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestService; - +import protobuf_unittest.no_generic_services_test.UnittestNoGenericServices; import java.util.HashSet; import java.util.Set; import junit.framework.TestCase; @@ -59,9 +58,9 @@ public class ServiceTest extends TestCase { private RpcController mockController; private final Descriptors.MethodDescriptor fooDescriptor = - TestService.getDescriptor().getMethods().get(0); + TestService.getDescriptor().getMethods().get(0); private final Descriptors.MethodDescriptor barDescriptor = - TestService.getDescriptor().getMethods().get(1); + TestService.getDescriptor().getMethods().get(1); @Override protected void setUp() throws Exception { @@ -80,16 +79,22 @@ public class ServiceTest extends TestCase { MockCallback barCallback = new MockCallback(); TestService mockService = control.createMock(TestService.class); - mockService.foo(EasyMock.same(mockController), EasyMock.same(fooRequest), - this.wrapsCallback(fooCallback)); - mockService.bar(EasyMock.same(mockController), EasyMock.same(barRequest), - this.wrapsCallback(barCallback)); + mockService.foo( + EasyMock.same(mockController), + EasyMock.same(fooRequest), + this.wrapsCallback(fooCallback)); + mockService.bar( + EasyMock.same(mockController), + EasyMock.same(barRequest), + this.wrapsCallback(barCallback)); control.replay(); - mockService.callMethod(fooDescriptor, mockController, - fooRequest, fooCallback); - mockService.callMethod(barDescriptor, mockController, - barRequest, barCallback); + mockService.callMethod( + fooDescriptor, mockController, + fooRequest, fooCallback); + mockService.callMethod( + barDescriptor, mockController, + barRequest, barCallback); control.verify(); } @@ -97,14 +102,10 @@ public class ServiceTest extends TestCase { public void testGetPrototype() throws Exception { TestService mockService = control.createMock(TestService.class); - assertSame(mockService.getRequestPrototype(fooDescriptor), - FooRequest.getDefaultInstance()); - assertSame(mockService.getResponsePrototype(fooDescriptor), - FooResponse.getDefaultInstance()); - assertSame(mockService.getRequestPrototype(barDescriptor), - BarRequest.getDefaultInstance()); - assertSame(mockService.getResponsePrototype(barDescriptor), - BarResponse.getDefaultInstance()); + assertSame(mockService.getRequestPrototype(fooDescriptor), FooRequest.getDefaultInstance()); + assertSame(mockService.getResponsePrototype(fooDescriptor), FooResponse.getDefaultInstance()); + assertSame(mockService.getRequestPrototype(barDescriptor), BarRequest.getDefaultInstance()); + assertSame(mockService.getResponsePrototype(barDescriptor), BarResponse.getDefaultInstance()); } /** Tests generated stubs. */ @@ -117,17 +118,17 @@ public class ServiceTest extends TestCase { TestService stub = TestService.newStub(mockChannel); mockChannel.callMethod( - EasyMock.same(fooDescriptor), - EasyMock.same(mockController), - EasyMock.same(fooRequest), - EasyMock.same(FooResponse.getDefaultInstance()), - this.wrapsCallback(fooCallback)); + EasyMock.same(fooDescriptor), + EasyMock.same(mockController), + EasyMock.same(fooRequest), + EasyMock.same(FooResponse.getDefaultInstance()), + this.wrapsCallback(fooCallback)); mockChannel.callMethod( - EasyMock.same(barDescriptor), - EasyMock.same(mockController), - EasyMock.same(barRequest), - EasyMock.same(BarResponse.getDefaultInstance()), - this.wrapsCallback(barCallback)); + EasyMock.same(barDescriptor), + EasyMock.same(mockController), + EasyMock.same(barRequest), + EasyMock.same(BarResponse.getDefaultInstance()), + this.wrapsCallback(barCallback)); control.replay(); stub.foo(mockController, fooRequest, fooCallback); @@ -139,24 +140,26 @@ public class ServiceTest extends TestCase { public void testBlockingStub() throws Exception { FooRequest fooRequest = FooRequest.newBuilder().build(); BarRequest barRequest = BarRequest.newBuilder().build(); - BlockingRpcChannel mockChannel = - control.createMock(BlockingRpcChannel.class); - TestService.BlockingInterface stub = - TestService.newBlockingStub(mockChannel); + BlockingRpcChannel mockChannel = control.createMock(BlockingRpcChannel.class); + TestService.BlockingInterface stub = TestService.newBlockingStub(mockChannel); FooResponse fooResponse = FooResponse.newBuilder().build(); BarResponse barResponse = BarResponse.newBuilder().build(); - EasyMock.expect(mockChannel.callBlockingMethod( - EasyMock.same(fooDescriptor), - EasyMock.same(mockController), - EasyMock.same(fooRequest), - EasyMock.same(FooResponse.getDefaultInstance()))).andReturn(fooResponse); - EasyMock.expect(mockChannel.callBlockingMethod( - EasyMock.same(barDescriptor), - EasyMock.same(mockController), - EasyMock.same(barRequest), - EasyMock.same(BarResponse.getDefaultInstance()))).andReturn(barResponse); + EasyMock.expect( + mockChannel.callBlockingMethod( + EasyMock.same(fooDescriptor), + EasyMock.same(mockController), + EasyMock.same(fooRequest), + EasyMock.same(FooResponse.getDefaultInstance()))) + .andReturn(fooResponse); + EasyMock.expect( + mockChannel.callBlockingMethod( + EasyMock.same(barDescriptor), + EasyMock.same(mockController), + EasyMock.same(barRequest), + EasyMock.same(BarResponse.getDefaultInstance()))) + .andReturn(barResponse); control.replay(); assertSame(fooResponse, stub.foo(mockController, fooRequest)); @@ -165,13 +168,11 @@ public class ServiceTest extends TestCase { } public void testNewReflectiveService() { - ServiceWithNoOuter.Interface impl = - control.createMock(ServiceWithNoOuter.Interface.class); + ServiceWithNoOuter.Interface impl = control.createMock(ServiceWithNoOuter.Interface.class); RpcController controller = control.createMock(RpcController.class); Service service = ServiceWithNoOuter.newReflectiveService(impl); - MethodDescriptor fooMethod = - ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); + MethodDescriptor fooMethod = ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance(); RpcCallback callback = new RpcCallback() { @@ -181,11 +182,9 @@ public class ServiceTest extends TestCase { fail(); } }; - RpcCallback specializedCallback = - RpcUtil.specializeCallback(callback); + RpcCallback specializedCallback = RpcUtil.specializeCallback(callback); - impl.foo(EasyMock.same(controller), EasyMock.same(request), - EasyMock.same(specializedCallback)); + impl.foo(EasyMock.same(controller), EasyMock.same(request), EasyMock.same(specializedCallback)); EasyMock.expectLastCall(); control.replay(); @@ -199,11 +198,9 @@ public class ServiceTest extends TestCase { ServiceWithNoOuter.BlockingInterface impl = control.createMock(ServiceWithNoOuter.BlockingInterface.class); RpcController controller = control.createMock(RpcController.class); - BlockingService service = - ServiceWithNoOuter.newReflectiveBlockingService(impl); + BlockingService service = ServiceWithNoOuter.newReflectiveBlockingService(impl); - MethodDescriptor fooMethod = - ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); + MethodDescriptor fooMethod = ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance(); TestAllTypes expectedResponse = TestAllTypes.getDefaultInstance(); @@ -212,8 +209,7 @@ public class ServiceTest extends TestCase { control.replay(); - Message response = - service.callBlockingMethod(fooMethod, controller, request); + Message response = service.callBlockingMethod(fooMethod, controller, request); assertEquals(expectedResponse, response); control.verify(); @@ -222,16 +218,16 @@ public class ServiceTest extends TestCase { public void testNoGenericServices() throws Exception { // Non-services should be usable. UnittestNoGenericServices.TestMessage message = - UnittestNoGenericServices.TestMessage.newBuilder() - .setA(123) - .setExtension(UnittestNoGenericServices.testExtension, 456) - .build(); + UnittestNoGenericServices.TestMessage.newBuilder() + .setA(123) + .setExtension(UnittestNoGenericServices.testExtension, 456) + .build(); assertEquals(123, message.getA()); assertEquals(1, UnittestNoGenericServices.TestEnum.FOO.getNumber()); // Build a list of the class names nested in UnittestNoGenericServices. - String outerName = "google.protobuf.no_generic_services_test." + - "UnittestNoGenericServices"; + String outerName = + "protobuf_unittest.no_generic_services_test.UnittestNoGenericServices"; Class outerClass = Class.forName(outerName); Set innerClassNames = new HashSet(); @@ -245,9 +241,9 @@ public class ServiceTest extends TestCase { // separator. assertTrue(fullName.startsWith(outerName)); - if (!Service.class.isAssignableFrom(innerClass) && - !Message.class.isAssignableFrom(innerClass) && - !ProtocolMessageEnum.class.isAssignableFrom(innerClass)) { + if (!Service.class.isAssignableFrom(innerClass) + && !Message.class.isAssignableFrom(innerClass) + && !ProtocolMessageEnum.class.isAssignableFrom(innerClass)) { // Ignore any classes not generated by the base code generator. continue; } @@ -265,36 +261,37 @@ public class ServiceTest extends TestCase { assertEquals(1, file.getServices().size()); assertEquals("TestService", file.getServices().get(0).getName()); assertEquals(1, file.getServices().get(0).getMethods().size()); - assertEquals("Foo", - file.getServices().get(0).getMethods().get(0).getName()); + assertEquals("Foo", file.getServices().get(0).getMethods().get(0).getName()); } - // ================================================================= /** - * wrapsCallback() is an EasyMock argument predicate. wrapsCallback(c) - * matches a callback if calling that callback causes c to be called. - * In other words, c wraps the given callback. + * wrapsCallback() is an EasyMock argument predicate. wrapsCallback(c) matches a callback if + * calling that callback causes c to be called. In other words, c wraps the given callback. */ - private RpcCallback wrapsCallback( - MockCallback callback) { + private RpcCallback wrapsCallback(MockCallback callback) { EasyMock.reportMatcher(new WrapsCallback(callback)); return null; } /** The parameter to wrapsCallback() must be a MockCallback. */ - private static class MockCallback - implements RpcCallback { + private static class MockCallback implements RpcCallback { private boolean called = false; - public boolean isCalled() { return called; } + public boolean isCalled() { + return called; + } + + public void reset() { + called = false; + } - public void reset() { called = false; } @Override - public void run(Type message) { - called = true; } + public void run(T message) { + called = true; + } } /** Implementation of the wrapsCallback() argument matcher. */ @@ -311,7 +308,7 @@ public class ServiceTest extends TestCase { if (!(actual instanceof RpcCallback)) { return false; } - RpcCallback actualCallback = (RpcCallback)actual; + RpcCallback actualCallback = (RpcCallback) actual; callback.reset(); actualCallback.run(null); diff --git a/java/core/src/test/java/com/google/protobuf/SingleFieldBuilderV3Test.java b/java/core/src/test/java/com/google/protobuf/SingleFieldBuilderV3Test.java index e3a8d4f408..f2ae8f9bca 100644 --- a/java/core/src/test/java/com/google/protobuf/SingleFieldBuilderV3Test.java +++ b/java/core/src/test/java/com/google/protobuf/SingleFieldBuilderV3Test.java @@ -32,13 +32,11 @@ package com.google.protobuf; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; - import junit.framework.TestCase; /** - * Tests for {@link SingleFieldBuilderV3}. This tests basic functionality. - * More extensive testing is provided via other tests that exercise the - * builder. + * Tests for {@link SingleFieldBuilderV3}. This tests basic functionality. More extensive testing is + * provided via other tests that exercise the builder. * * @author jonp@google.com (Jon Perlow) */ @@ -46,16 +44,11 @@ public class SingleFieldBuilderV3Test extends TestCase { public void testBasicUseAndInvalidations() { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - SingleFieldBuilderV3 builder = - new SingleFieldBuilderV3( - TestAllTypes.getDefaultInstance(), - mockParent, - false); + SingleFieldBuilderV3 builder = + new SingleFieldBuilderV3( + TestAllTypes.getDefaultInstance(), mockParent, false); assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); - assertEquals(TestAllTypes.getDefaultInstance(), - builder.getBuilder().buildPartial()); + assertEquals(TestAllTypes.getDefaultInstance(), builder.getBuilder().buildPartial()); assertEquals(0, mockParent.getInvalidationCount()); builder.getBuilder().setOptionalInt32(10); @@ -71,18 +64,13 @@ public class SingleFieldBuilderV3Test extends TestCase { // Test that we don't keep getting invalidations on every change builder.getBuilder().setOptionalInt32(30); assertEquals(1, mockParent.getInvalidationCount()); - } public void testSetMessage() { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - SingleFieldBuilderV3 builder = - new SingleFieldBuilderV3( - TestAllTypes.getDefaultInstance(), - mockParent, - false); + SingleFieldBuilderV3 builder = + new SingleFieldBuilderV3( + TestAllTypes.getDefaultInstance(), mockParent, false); builder.setMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); assertEquals(0, builder.getMessage().getOptionalInt32()); @@ -102,13 +90,9 @@ public class SingleFieldBuilderV3Test extends TestCase { public void testClear() { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - SingleFieldBuilderV3 builder = - new SingleFieldBuilderV3( - TestAllTypes.getDefaultInstance(), - mockParent, - false); + SingleFieldBuilderV3 builder = + new SingleFieldBuilderV3( + TestAllTypes.getDefaultInstance(), mockParent, false); builder.setMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); assertNotSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); builder.clear(); @@ -122,13 +106,9 @@ public class SingleFieldBuilderV3Test extends TestCase { public void testMerge() { TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); - SingleFieldBuilderV3 builder = - new SingleFieldBuilderV3( - TestAllTypes.getDefaultInstance(), - mockParent, - false); + SingleFieldBuilderV3 builder = + new SingleFieldBuilderV3( + TestAllTypes.getDefaultInstance(), mockParent, false); // Merge into default field. builder.mergeFrom(TestAllTypes.getDefaultInstance()); @@ -136,20 +116,14 @@ public class SingleFieldBuilderV3Test extends TestCase { // Merge into non-default field on existing builder. builder.getBuilder().setOptionalInt32(2); - builder.mergeFrom(TestAllTypes.newBuilder() - .setOptionalDouble(4.0) - .buildPartial()); + builder.mergeFrom(TestAllTypes.newBuilder().setOptionalDouble(4.0).buildPartial()); assertEquals(2, builder.getMessage().getOptionalInt32()); - assertEquals(4.0, builder.getMessage().getOptionalDouble()); + assertEquals(4.0, builder.getMessage().getOptionalDouble(), 0.0); // Merge into non-default field on existing message - builder.setMessage(TestAllTypes.newBuilder() - .setOptionalInt32(10) - .buildPartial()); - builder.mergeFrom(TestAllTypes.newBuilder() - .setOptionalDouble(5.0) - .buildPartial()); + builder.setMessage(TestAllTypes.newBuilder().setOptionalInt32(10).buildPartial()); + builder.mergeFrom(TestAllTypes.newBuilder().setOptionalDouble(5.0).buildPartial()); assertEquals(10, builder.getMessage().getOptionalInt32()); - assertEquals(5.0, builder.getMessage().getOptionalDouble()); + assertEquals(5.0, builder.getMessage().getOptionalDouble(), 0.0); } } diff --git a/java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java b/java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java index a7f8342d13..a1a7194b43 100644 --- a/java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java +++ b/java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java @@ -40,9 +40,7 @@ import java.util.Set; import java.util.TreeSet; import junit.framework.TestCase; -/** - * @author darick@google.com Darick Tong - */ +/** @author darick@google.com Darick Tong */ public class SmallSortedMapTest extends TestCase { // java.util.AbstractMap.SimpleEntry is private in JDK 1.5. We re-implement it // here for JDK 1.5 users. @@ -78,16 +76,16 @@ public class SmallSortedMapTest extends TestCase { @Override public boolean equals(Object o) { - if (!(o instanceof Map.Entry)) + if (!(o instanceof Map.Entry)) { return false; + } Map.Entry e = (Map.Entry) o; return eq(key, e.getKey()) && eq(value, e.getValue()); } @Override public int hashCode() { - return ((key == null) ? 0 : key.hashCode()) ^ - ((value == null) ? 0 : value.hashCode()); + return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); } } @@ -101,14 +99,10 @@ public class SmallSortedMapTest extends TestCase { private void runPutAndGetTest(int numElements) { // Test with even and odd arraySize - SmallSortedMap map1 = - SmallSortedMap.newInstanceForTest(3); - SmallSortedMap map2 = - SmallSortedMap.newInstanceForTest(4); - SmallSortedMap map3 = - SmallSortedMap.newInstanceForTest(3); - SmallSortedMap map4 = - SmallSortedMap.newInstanceForTest(4); + SmallSortedMap map1 = SmallSortedMap.newInstanceForTest(3); + SmallSortedMap map2 = SmallSortedMap.newInstanceForTest(4); + SmallSortedMap map3 = SmallSortedMap.newInstanceForTest(3); + SmallSortedMap map4 = SmallSortedMap.newInstanceForTest(4); // Test with puts in ascending order. for (int i = 0; i < numElements; i++) { @@ -136,7 +130,7 @@ public class SmallSortedMapTest extends TestCase { for (SmallSortedMap map : allMaps) { assertEquals(numElements, map.size()); for (int i = 0; i < numElements; i++) { - assertEquals(new Integer(i + 1), map.get(i)); + assertEquals(Integer.valueOf(i + 1), map.get(i)); } } @@ -152,7 +146,7 @@ public class SmallSortedMapTest extends TestCase { assertNull(map.remove(i + 1)); } for (int i = 0; i < 6; i++) { - assertEquals(new Integer(i + 1), map.put(i, i + 2)); + assertEquals(Integer.valueOf(i + 1), map.put(i, i + 2)); } } @@ -165,22 +159,22 @@ public class SmallSortedMapTest extends TestCase { assertEquals(3, map.getNumArrayEntries()); assertEquals(3, map.getNumOverflowEntries()); - assertEquals(6, map.size()); + assertEquals(6, map.size()); assertEquals(makeSortedKeySet(0, 1, 2, 3, 4, 5), map.keySet()); - assertEquals(new Integer(2), map.remove(1)); + assertEquals(Integer.valueOf(2), map.remove(1)); assertEquals(3, map.getNumArrayEntries()); assertEquals(2, map.getNumOverflowEntries()); - assertEquals(5, map.size()); + assertEquals(5, map.size()); assertEquals(makeSortedKeySet(0, 2, 3, 4, 5), map.keySet()); - assertEquals(new Integer(5), map.remove(4)); + assertEquals(Integer.valueOf(5), map.remove(4)); assertEquals(3, map.getNumArrayEntries()); assertEquals(1, map.getNumOverflowEntries()); - assertEquals(4, map.size()); + assertEquals(4, map.size()); assertEquals(makeSortedKeySet(0, 2, 3, 5), map.keySet()); - assertEquals(new Integer(4), map.remove(3)); + assertEquals(Integer.valueOf(4), map.remove(3)); assertEquals(3, map.getNumArrayEntries()); assertEquals(0, map.getNumOverflowEntries()); assertEquals(3, map.size()); @@ -191,7 +185,7 @@ public class SmallSortedMapTest extends TestCase { assertEquals(0, map.getNumOverflowEntries()); assertEquals(3, map.size()); - assertEquals(new Integer(1), map.remove(0)); + assertEquals(Integer.valueOf(1), map.remove(0)); assertEquals(2, map.getNumArrayEntries()); assertEquals(0, map.getNumOverflowEntries()); assertEquals(2, map.size()); @@ -216,16 +210,15 @@ public class SmallSortedMapTest extends TestCase { assertEquals(3, map.getNumArrayEntries()); for (int i = 0; i < 3; i++) { Map.Entry entry = map.getArrayEntryAt(i); - assertEquals(new Integer(i), entry.getKey()); - assertEquals(new Integer(i + 1), entry.getValue()); + assertEquals(Integer.valueOf(i), entry.getKey()); + assertEquals(Integer.valueOf(i + 1), entry.getValue()); } - Iterator> it = - map.getOverflowEntries().iterator(); + Iterator> it = map.getOverflowEntries().iterator(); for (int i = 3; i < 6; i++) { assertTrue(it.hasNext()); Map.Entry entry = it.next(); - assertEquals(new Integer(i), entry.getKey()); - assertEquals(new Integer(i + 1), entry.getValue()); + assertEquals(Integer.valueOf(i), entry.getKey()); + assertEquals(Integer.valueOf(i + 1), entry.getValue()); } assertFalse(it.hasNext()); } @@ -237,10 +230,8 @@ public class SmallSortedMapTest extends TestCase { } Set> entrySet = map.entrySet(); for (int i = 0; i < 6; i++) { - assertTrue( - entrySet.contains(new SimpleEntry(i, i + 1))); - assertFalse( - entrySet.contains(new SimpleEntry(i, i))); + assertTrue(entrySet.contains(new SimpleEntry(i, i + 1))); + assertFalse(entrySet.contains(new SimpleEntry(i, i))); } } @@ -248,13 +239,12 @@ public class SmallSortedMapTest extends TestCase { SmallSortedMap map = SmallSortedMap.newInstanceForTest(3); Set> entrySet = map.entrySet(); for (int i = 0; i < 6; i++) { - Map.Entry entry = - new SimpleEntry(i, i + 1); + Map.Entry entry = new SimpleEntry(i, i + 1); assertTrue(entrySet.add(entry)); assertFalse(entrySet.add(entry)); } for (int i = 0; i < 6; i++) { - assertEquals(new Integer(i + 1), map.get(i)); + assertEquals(Integer.valueOf(i + 1), map.get(i)); } assertEquals(3, map.getNumArrayEntries()); assertEquals(3, map.getNumOverflowEntries()); @@ -268,8 +258,7 @@ public class SmallSortedMapTest extends TestCase { assertNull(map.put(i, i + 1)); } for (int i = 0; i < 6; i++) { - Map.Entry entry = - new SimpleEntry(i, i + 1); + Map.Entry entry = new SimpleEntry(i, i + 1); assertTrue(entrySet.remove(entry)); assertFalse(entrySet.remove(entry)); } @@ -284,7 +273,7 @@ public class SmallSortedMapTest extends TestCase { for (int i = 0; i < 6; i++) { assertNull(map.put(i, i + 1)); } - map.entrySet().clear(); + map.clear(); assertTrue(map.isEmpty()); assertEquals(0, map.getNumArrayEntries()); assertEquals(0, map.getNumOverflowEntries()); @@ -300,8 +289,8 @@ public class SmallSortedMapTest extends TestCase { for (int i = 0; i < 6; i++) { assertTrue(it.hasNext()); Map.Entry entry = it.next(); - assertEquals(new Integer(i), entry.getKey()); - assertEquals(new Integer(i + 1), entry.getValue()); + assertEquals(Integer.valueOf(i), entry.getKey()); + assertEquals(Integer.valueOf(i + 1), entry.getValue()); } assertFalse(it.hasNext()); } @@ -332,7 +321,7 @@ public class SmallSortedMapTest extends TestCase { entry.setValue(i + 23); } for (int i = 0; i < 6; i++) { - assertEquals(new Integer(i + 23), map.get(i)); + assertEquals(Integer.valueOf(i + 23), map.get(i)); } } @@ -342,7 +331,7 @@ public class SmallSortedMapTest extends TestCase { assertNull(map.put(i, i + 1)); } map.makeImmutable(); - assertEquals(new Integer(1), map.get(0)); + assertEquals(Integer.valueOf(1), map.get(0)); assertEquals(6, map.size()); try { diff --git a/java/core/src/test/java/com/google/protobuf/TestBadIdentifiers.java b/java/core/src/test/java/com/google/protobuf/TestBadIdentifiers.java index 4af5542912..3d82c5f9af 100644 --- a/java/core/src/test/java/com/google/protobuf/TestBadIdentifiers.java +++ b/java/core/src/test/java/com/google/protobuf/TestBadIdentifiers.java @@ -33,9 +33,9 @@ package com.google.protobuf; import junit.framework.TestCase; /** - * Tests that proto2 api generation doesn't cause compile errors when - * compiling protocol buffers that have names that would otherwise conflict - * if not fully qualified (like @Deprecated and @Override). + * Tests that proto2 api generation doesn't cause compile errors when compiling protocol buffers + * that have names that would otherwise conflict if not fully qualified (like @Deprecated + * and @Override). * * @author jonp@google.com (Jon Perlow) */ @@ -48,17 +48,11 @@ public class TestBadIdentifiers extends TestCase { } public void testGetDescriptor() { - Descriptors.FileDescriptor fileDescriptor = - TestBadIdentifiersProto.getDescriptor(); - String descriptorField = TestBadIdentifiersProto.Descriptor - .getDefaultInstance().getDescriptor(); - Descriptors.Descriptor protoDescriptor = TestBadIdentifiersProto.Descriptor - .getDefaultInstance().getDescriptorForType(); - String nestedDescriptorField = TestBadIdentifiersProto.Descriptor - .NestedDescriptor.getDefaultInstance().getDescriptor(); - Descriptors.Descriptor nestedProtoDescriptor = TestBadIdentifiersProto - .Descriptor.NestedDescriptor.getDefaultInstance() - .getDescriptorForType(); + TestBadIdentifiersProto.getDescriptor(); + TestBadIdentifiersProto.Descriptor.getDefaultInstance().getDescriptor(); + TestBadIdentifiersProto.Descriptor.getDefaultInstance().getDescriptorForType(); + TestBadIdentifiersProto.Descriptor.NestedDescriptor.getDefaultInstance().getDescriptor(); + TestBadIdentifiersProto.Descriptor.NestedDescriptor.getDefaultInstance().getDescriptorForType(); } public void testConflictingFieldNames() throws Exception { @@ -87,10 +81,16 @@ public class TestBadIdentifiers extends TestCase { assertEquals(0, message.getInt32FieldList31()); assertEquals(0, message.getInt64FieldCount()); - assertEquals(0L, message.getExtension( - TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldCount).longValue()); - assertEquals(0L, message.getExtension( - TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldList).longValue()); + assertEquals( + 0L, + message + .getExtension(TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldCount) + .longValue()); + assertEquals( + 0L, + message + .getExtension(TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldList) + .longValue()); assertEquals("", message.getFieldName32()); assertEquals("", message.getFieldName33()); diff --git a/java/core/src/test/java/com/google/protobuf/TestBadIdentifiersLite.java b/java/core/src/test/java/com/google/protobuf/TestBadIdentifiersLite.java index 37f94c03bc..798d7ca612 100644 --- a/java/core/src/test/java/com/google/protobuf/TestBadIdentifiersLite.java +++ b/java/core/src/test/java/com/google/protobuf/TestBadIdentifiersLite.java @@ -75,9 +75,15 @@ public final class TestBadIdentifiersLite extends TestCase { assertEquals(0, message.getInt32FieldList31()); assertEquals(0, message.getInt64FieldCount()); - assertEquals(0L, message.getExtension( - TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldCount).longValue()); - assertEquals(0L, message.getExtension( - TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldList).longValue()); + assertEquals( + 0L, + message + .getExtension(TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldCount) + .longValue()); + assertEquals( + 0L, + message + .getExtension(TestBadIdentifiersProto.TestConflictingFieldNames.int64FieldList) + .longValue()); } } diff --git a/java/core/src/test/java/com/google/protobuf/TestSchemas.java b/java/core/src/test/java/com/google/protobuf/TestSchemas.java new file mode 100755 index 0000000000..ab0ced4cdc --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/TestSchemas.java @@ -0,0 +1,94 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2Testing; +import com.google.protobuf.testing.Proto2Testing.Proto2Empty; +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import com.google.protobuf.testing.Proto3Testing.Proto3Empty; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps; + +/** Schemas to support testing. */ +public class TestSchemas { + public static final Schema genericProto2Schema = + new ManifestSchemaFactory().createSchema(Proto2Message.class); + public static final Schema genericProto3Schema = + new ManifestSchemaFactory().createSchema(Proto3Message.class); + + public static void registerGenericProto2Schemas() { + registerProto2Schemas(); + } + + public static void registerGenericProto3Schemas() { + registerProto3Schemas(); + } + + private static void registerProto2Schemas() { + Protobuf protobuf = Protobuf.getInstance(); + ManifestSchemaFactory factory = new ManifestSchemaFactory(); + protobuf.registerSchemaOverride(Proto2Message.class, factory.createSchema(Proto2Message.class)); + protobuf.registerSchemaOverride( + Proto2Message.FieldGroup49.class, factory.createSchema(Proto2Message.FieldGroup49.class)); + protobuf.registerSchemaOverride( + Proto2Message.FieldGroupList51.class, + factory.createSchema(Proto2Message.FieldGroupList51.class)); + protobuf.registerSchemaOverride( + Proto2Message.FieldGroup69.class, factory.createSchema(Proto2Message.FieldGroup69.class)); + protobuf.registerSchemaOverride( + Proto2Message.RequiredNestedMessage.class, + factory.createSchema(Proto2Message.RequiredNestedMessage.class)); + protobuf.registerSchemaOverride( + Proto2Message.FieldRequiredGroup88.class, + factory.createSchema(Proto2Message.FieldRequiredGroup88.class)); + protobuf.registerSchemaOverride(Proto2Empty.class, factory.createSchema(Proto2Empty.class)); + protobuf.registerSchemaOverride( + Proto2MessageWithExtensions.class, factory.createSchema(Proto2MessageWithExtensions.class)); + protobuf.registerSchemaOverride( + Proto2Testing.FieldGroup49.class, factory.createSchema(Proto2Testing.FieldGroup49.class)); + protobuf.registerSchemaOverride( + Proto2Testing.FieldGroupList51.class, + factory.createSchema(Proto2Testing.FieldGroupList51.class)); + protobuf.registerSchemaOverride( + Proto2MessageWithMaps.class, factory.createSchema(Proto2MessageWithMaps.class)); + } + + private static void registerProto3Schemas() { + Protobuf protobuf = Protobuf.getInstance(); + ManifestSchemaFactory factory = new ManifestSchemaFactory(); + protobuf.registerSchemaOverride(Proto3Message.class, factory.createSchema(Proto3Message.class)); + protobuf.registerSchemaOverride(Proto3Empty.class, factory.createSchema(Proto3Empty.class)); + protobuf.registerSchemaOverride( + Proto3MessageWithMaps.class, factory.createSchema(Proto3MessageWithMaps.class)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/TestSchemasLite.java b/java/core/src/test/java/com/google/protobuf/TestSchemasLite.java new file mode 100755 index 0000000000..28a89d72a1 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/TestSchemasLite.java @@ -0,0 +1,103 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2TestingLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2EmptyLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithExtensions; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps; +import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps; + +/** Schemas to support testing. */ +public final class TestSchemasLite { + + public static final Schema genericProto2LiteSchema = + new ManifestSchemaFactory().createSchema(Proto2MessageLite.class); + public static final Schema genericProto3LiteSchema = + new ManifestSchemaFactory().createSchema(Proto3MessageLite.class); + + public static void registerGenericProto2LiteSchemas() { + registerProto2LiteSchemas(); + } + + public static void registerGenericProto3LiteSchemas() { + registerProto3LiteSchemas(); + } + + private static void registerProto2LiteSchemas() { + Protobuf protobuf = Protobuf.getInstance(); + ManifestSchemaFactory factory = new ManifestSchemaFactory(); + protobuf.registerSchemaOverride( + Proto2MessageLite.class, factory.createSchema(Proto2MessageLite.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.FieldGroup49.class, + factory.createSchema(Proto2MessageLite.FieldGroup49.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.FieldGroupList51.class, + factory.createSchema(Proto2MessageLite.FieldGroupList51.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.FieldGroup69.class, + factory.createSchema(Proto2MessageLite.FieldGroup69.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.RequiredNestedMessage.class, + factory.createSchema(Proto2MessageLite.RequiredNestedMessage.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.FieldRequiredGroup88.class, + factory.createSchema(Proto2MessageLite.FieldRequiredGroup88.class)); + protobuf.registerSchemaOverride( + Proto2EmptyLite.class, factory.createSchema(Proto2EmptyLite.class)); + protobuf.registerSchemaOverride( + Proto2MessageLiteWithExtensions.class, + factory.createSchema(Proto2MessageLiteWithExtensions.class)); + protobuf.registerSchemaOverride( + Proto2TestingLite.FieldGroup49.class, + factory.createSchema(Proto2TestingLite.FieldGroup49.class)); + protobuf.registerSchemaOverride( + Proto2TestingLite.FieldGroupList51.class, + factory.createSchema(Proto2TestingLite.FieldGroupList51.class)); + protobuf.registerSchemaOverride( + Proto2MessageLiteWithMaps.class, factory.createSchema(Proto2MessageLiteWithMaps.class)); + } + + private static void registerProto3LiteSchemas() { + Protobuf protobuf = Protobuf.getInstance(); + ManifestSchemaFactory factory = new ManifestSchemaFactory(); + protobuf.registerSchemaOverride( + Proto3MessageLite.class, factory.createSchema(Proto3MessageLite.class)); + protobuf.registerSchemaOverride( + Proto3EmptyLite.class, factory.createSchema(Proto3EmptyLite.class)); + protobuf.registerSchemaOverride( + Proto3MessageLiteWithMaps.class, factory.createSchema(Proto3MessageLiteWithMaps.class)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/TestUtil.java b/java/core/src/test/java/com/google/protobuf/TestUtil.java index 8ec22d3f12..22c0be2522 100644 --- a/java/core/src/test/java/com/google/protobuf/TestUtil.java +++ b/java/core/src/test/java/com/google/protobuf/TestUtil.java @@ -119,8 +119,6 @@ import static com.google.protobuf.UnittestLite.repeatedStringExtensionLite; import static com.google.protobuf.UnittestLite.repeatedStringPieceExtensionLite; import static com.google.protobuf.UnittestLite.repeatedUint32ExtensionLite; import static com.google.protobuf.UnittestLite.repeatedUint64ExtensionLite; -import static protobuf_unittest.UnittestProto.OptionalGroup_extension; -import static protobuf_unittest.UnittestProto.RepeatedGroup_extension; import static protobuf_unittest.UnittestProto.defaultBoolExtension; import static protobuf_unittest.UnittestProto.defaultBytesExtension; import static protobuf_unittest.UnittestProto.defaultCordExtension; @@ -222,6 +220,8 @@ import com.google.protobuf.test.UnittestImportPublic.PublicImportMessage; import protobuf_unittest.UnittestProto; import protobuf_unittest.UnittestProto.ForeignEnum; import protobuf_unittest.UnittestProto.ForeignMessage; +import protobuf_unittest.UnittestProto.OptionalGroup_extension; +import protobuf_unittest.UnittestProto.RepeatedGroup_extension; import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllExtensionsOrBuilder; import protobuf_unittest.UnittestProto.TestAllTypes; @@ -234,17 +234,20 @@ import protobuf_unittest.UnittestProto.TestUnpackedTypes; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.LogRecord; import junit.framework.Assert; /** - * Contains methods for setting all fields of {@code TestAllTypes} to - * some values as well as checking that all the fields are set to those values. - * These are useful for testing various protocol message features, e.g. - * set all fields of a message, serialize it, parse it, and check that all - * fields are set. + * Contains methods for setting all fields of {@code TestAllTypes} to some values as well as + * checking that all the fields are set to those values. These are useful for testing various + * protocol message features, e.g. set all fields of a message, serialize it, parse it, and check + * that all fields are set. * - *

This code is not to be used outside of {@code com.google.protobuf} and - * subpackages. + *

This code is not to be used outside of {@code com.google.protobuf} and subpackages. * * @author kenton@google.com Kenton Varda */ @@ -262,17 +265,15 @@ public final class TestUtil { } // BEGIN FULL-RUNTIME - /** - * Dirties the message by resetting the momoized serialized size. - */ + /** Dirties the message by resetting the momoized serialized size. */ public static void resetMemoizedSize(AbstractMessage message) { message.memoizedSize = -1; } // END FULL-RUNTIME /** - * Get a {@code TestAllTypes} with all fields set as they would be by - * {@link #setAllFields(TestAllTypes.Builder)}. + * Get a {@code TestAllTypes} with all fields set as they would be by {@link + * #setAllFields(TestAllTypes.Builder)}. */ public static TestAllTypes getAllSet() { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); @@ -281,8 +282,8 @@ public final class TestUtil { } /** - * Get a {@code TestAllTypes.Builder} with all fields set as they would be by - * {@link #setAllFields(TestAllTypes.Builder)}. + * Get a {@code TestAllTypes.Builder} with all fields set as they would be by {@link + * #setAllFields(TestAllTypes.Builder)}. */ public static TestAllTypes.Builder getAllSetBuilder() { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); @@ -291,8 +292,8 @@ public final class TestUtil { } /** - * Get a {@code TestAllExtensions} with all fields set as they would be by - * {@link #setAllExtensions(TestAllExtensions.Builder)}. + * Get a {@code TestAllExtensions} with all fields set as they would be by {@link + * #setAllExtensions(TestAllExtensions.Builder)}. */ public static TestAllExtensions getAllExtensionsSet() { TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); @@ -318,146 +319,126 @@ public final class TestUtil { return builder.build(); } - /** - * Set every field of {@code message} to the values expected by - * {@code assertAllFieldsSet()}. - */ + /** Set every field of {@code message} to the values expected by {@code assertAllFieldsSet()}. */ public static void setAllFields(TestAllTypes.Builder message) { - message.setOptionalInt32 (101); - message.setOptionalInt64 (102); - message.setOptionalUint32 (103); - message.setOptionalUint64 (104); - message.setOptionalSint32 (105); - message.setOptionalSint64 (106); - message.setOptionalFixed32 (107); - message.setOptionalFixed64 (108); + message.setOptionalInt32(101); + message.setOptionalInt64(102); + message.setOptionalUint32(103); + message.setOptionalUint64(104); + message.setOptionalSint32(105); + message.setOptionalSint64(106); + message.setOptionalFixed32(107); + message.setOptionalFixed64(108); message.setOptionalSfixed32(109); message.setOptionalSfixed64(110); - message.setOptionalFloat (111); - message.setOptionalDouble (112); - message.setOptionalBool (true); - message.setOptionalString ("115"); - message.setOptionalBytes (toBytes("116")); - - message.setOptionalGroup( - TestAllTypes.OptionalGroup.newBuilder().setA(117).build()); - message.setOptionalNestedMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(118).build()); - message.setOptionalForeignMessage( - ForeignMessage.newBuilder().setC(119).build()); - message.setOptionalImportMessage( - ImportMessage.newBuilder().setD(120).build()); - message.setOptionalPublicImportMessage( - PublicImportMessage.newBuilder().setE(126).build()); - message.setOptionalLazyMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(127).build()); - - message.setOptionalNestedEnum (TestAllTypes.NestedEnum.BAZ); + message.setOptionalFloat(111); + message.setOptionalDouble(112); + message.setOptionalBool(true); + message.setOptionalString("115"); + message.setOptionalBytes(toBytes("116")); + + message.setOptionalGroup(TestAllTypes.OptionalGroup.newBuilder().setA(117).build()); + message.setOptionalNestedMessage(TestAllTypes.NestedMessage.newBuilder().setBb(118).build()); + message.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(119).build()); + message.setOptionalImportMessage(ImportMessage.newBuilder().setD(120).build()); + message.setOptionalPublicImportMessage(PublicImportMessage.newBuilder().setE(126).build()); + message.setOptionalLazyMessage(TestAllTypes.NestedMessage.newBuilder().setBb(127).build()); + + message.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAZ); message.setOptionalForeignEnum(ForeignEnum.FOREIGN_BAZ); - message.setOptionalImportEnum (ImportEnum.IMPORT_BAZ); + message.setOptionalImportEnum(ImportEnum.IMPORT_BAZ); message.setOptionalStringPiece("124"); message.setOptionalCord("125"); // ----------------------------------------------------------------- - message.addRepeatedInt32 (201); - message.addRepeatedInt64 (202); - message.addRepeatedUint32 (203); - message.addRepeatedUint64 (204); - message.addRepeatedSint32 (205); - message.addRepeatedSint64 (206); - message.addRepeatedFixed32 (207); - message.addRepeatedFixed64 (208); + message.addRepeatedInt32(201); + message.addRepeatedInt64(202); + message.addRepeatedUint32(203); + message.addRepeatedUint64(204); + message.addRepeatedSint32(205); + message.addRepeatedSint64(206); + message.addRepeatedFixed32(207); + message.addRepeatedFixed64(208); message.addRepeatedSfixed32(209); message.addRepeatedSfixed64(210); - message.addRepeatedFloat (211); - message.addRepeatedDouble (212); - message.addRepeatedBool (true); - message.addRepeatedString ("215"); - message.addRepeatedBytes (toBytes("216")); - - message.addRepeatedGroup( - TestAllTypes.RepeatedGroup.newBuilder().setA(217).build()); - message.addRepeatedNestedMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); - message.addRepeatedForeignMessage( - ForeignMessage.newBuilder().setC(219).build()); - message.addRepeatedImportMessage( - ImportMessage.newBuilder().setD(220).build()); - message.addRepeatedLazyMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(227).build()); - - message.addRepeatedNestedEnum (TestAllTypes.NestedEnum.BAR); + message.addRepeatedFloat(211); + message.addRepeatedDouble(212); + message.addRepeatedBool(true); + message.addRepeatedString("215"); + message.addRepeatedBytes(toBytes("216")); + + message.addRepeatedGroup(TestAllTypes.RepeatedGroup.newBuilder().setA(217).build()); + message.addRepeatedNestedMessage(TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); + message.addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(219).build()); + message.addRepeatedImportMessage(ImportMessage.newBuilder().setD(220).build()); + message.addRepeatedLazyMessage(TestAllTypes.NestedMessage.newBuilder().setBb(227).build()); + + message.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR); message.addRepeatedForeignEnum(ForeignEnum.FOREIGN_BAR); - message.addRepeatedImportEnum (ImportEnum.IMPORT_BAR); + message.addRepeatedImportEnum(ImportEnum.IMPORT_BAR); message.addRepeatedStringPiece("224"); message.addRepeatedCord("225"); // Add a second one of each field. - message.addRepeatedInt32 (301); - message.addRepeatedInt64 (302); - message.addRepeatedUint32 (303); - message.addRepeatedUint64 (304); - message.addRepeatedSint32 (305); - message.addRepeatedSint64 (306); - message.addRepeatedFixed32 (307); - message.addRepeatedFixed64 (308); + message.addRepeatedInt32(301); + message.addRepeatedInt64(302); + message.addRepeatedUint32(303); + message.addRepeatedUint64(304); + message.addRepeatedSint32(305); + message.addRepeatedSint64(306); + message.addRepeatedFixed32(307); + message.addRepeatedFixed64(308); message.addRepeatedSfixed32(309); message.addRepeatedSfixed64(310); - message.addRepeatedFloat (311); - message.addRepeatedDouble (312); - message.addRepeatedBool (false); - message.addRepeatedString ("315"); - message.addRepeatedBytes (toBytes("316")); - - message.addRepeatedGroup( - TestAllTypes.RepeatedGroup.newBuilder().setA(317).build()); - message.addRepeatedNestedMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(318).build()); - message.addRepeatedForeignMessage( - ForeignMessage.newBuilder().setC(319).build()); - message.addRepeatedImportMessage( - ImportMessage.newBuilder().setD(320).build()); - message.addRepeatedLazyMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(327).build()); - - message.addRepeatedNestedEnum (TestAllTypes.NestedEnum.BAZ); + message.addRepeatedFloat(311); + message.addRepeatedDouble(312); + message.addRepeatedBool(false); + message.addRepeatedString("315"); + message.addRepeatedBytes(toBytes("316")); + + message.addRepeatedGroup(TestAllTypes.RepeatedGroup.newBuilder().setA(317).build()); + message.addRepeatedNestedMessage(TestAllTypes.NestedMessage.newBuilder().setBb(318).build()); + message.addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(319).build()); + message.addRepeatedImportMessage(ImportMessage.newBuilder().setD(320).build()); + message.addRepeatedLazyMessage(TestAllTypes.NestedMessage.newBuilder().setBb(327).build()); + + message.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAZ); message.addRepeatedForeignEnum(ForeignEnum.FOREIGN_BAZ); - message.addRepeatedImportEnum (ImportEnum.IMPORT_BAZ); + message.addRepeatedImportEnum(ImportEnum.IMPORT_BAZ); message.addRepeatedStringPiece("324"); message.addRepeatedCord("325"); // ----------------------------------------------------------------- - message.setDefaultInt32 (401); - message.setDefaultInt64 (402); - message.setDefaultUint32 (403); - message.setDefaultUint64 (404); - message.setDefaultSint32 (405); - message.setDefaultSint64 (406); - message.setDefaultFixed32 (407); - message.setDefaultFixed64 (408); + message.setDefaultInt32(401); + message.setDefaultInt64(402); + message.setDefaultUint32(403); + message.setDefaultUint64(404); + message.setDefaultSint32(405); + message.setDefaultSint64(406); + message.setDefaultFixed32(407); + message.setDefaultFixed64(408); message.setDefaultSfixed32(409); message.setDefaultSfixed64(410); - message.setDefaultFloat (411); - message.setDefaultDouble (412); - message.setDefaultBool (false); - message.setDefaultString ("415"); - message.setDefaultBytes (toBytes("416")); + message.setDefaultFloat(411); + message.setDefaultDouble(412); + message.setDefaultBool(false); + message.setDefaultString("415"); + message.setDefaultBytes(toBytes("416")); - message.setDefaultNestedEnum (TestAllTypes.NestedEnum.FOO); + message.setDefaultNestedEnum(TestAllTypes.NestedEnum.FOO); message.setDefaultForeignEnum(ForeignEnum.FOREIGN_FOO); - message.setDefaultImportEnum (ImportEnum.IMPORT_FOO); + message.setDefaultImportEnum(ImportEnum.IMPORT_FOO); message.setDefaultStringPiece("424"); message.setDefaultCord("425"); message.setOneofUint32(601); - message.setOneofNestedMessage( - TestAllTypes.NestedMessage.newBuilder().setBb(602).build()); + message.setOneofNestedMessage(TestAllTypes.NestedMessage.newBuilder().setBb(602).build()); message.setOneofString("603"); message.setOneofBytes(toBytes("604")); } @@ -465,40 +446,35 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Modify the repeated fields of {@code message} to contain the values - * expected by {@code assertRepeatedFieldsModified()}. + * Modify the repeated fields of {@code message} to contain the values expected by {@code + * assertRepeatedFieldsModified()}. */ public static void modifyRepeatedFields(TestAllTypes.Builder message) { - message.setRepeatedInt32 (1, 501); - message.setRepeatedInt64 (1, 502); - message.setRepeatedUint32 (1, 503); - message.setRepeatedUint64 (1, 504); - message.setRepeatedSint32 (1, 505); - message.setRepeatedSint64 (1, 506); - message.setRepeatedFixed32 (1, 507); - message.setRepeatedFixed64 (1, 508); + message.setRepeatedInt32(1, 501); + message.setRepeatedInt64(1, 502); + message.setRepeatedUint32(1, 503); + message.setRepeatedUint64(1, 504); + message.setRepeatedSint32(1, 505); + message.setRepeatedSint64(1, 506); + message.setRepeatedFixed32(1, 507); + message.setRepeatedFixed64(1, 508); message.setRepeatedSfixed32(1, 509); message.setRepeatedSfixed64(1, 510); - message.setRepeatedFloat (1, 511); - message.setRepeatedDouble (1, 512); - message.setRepeatedBool (1, true); - message.setRepeatedString (1, "515"); - message.setRepeatedBytes (1, toBytes("516")); - - message.setRepeatedGroup(1, - TestAllTypes.RepeatedGroup.newBuilder().setA(517).build()); - message.setRepeatedNestedMessage(1, - TestAllTypes.NestedMessage.newBuilder().setBb(518).build()); - message.setRepeatedForeignMessage(1, - ForeignMessage.newBuilder().setC(519).build()); - message.setRepeatedImportMessage(1, - ImportMessage.newBuilder().setD(520).build()); - message.setRepeatedLazyMessage(1, - TestAllTypes.NestedMessage.newBuilder().setBb(527).build()); - - message.setRepeatedNestedEnum (1, TestAllTypes.NestedEnum.FOO); + message.setRepeatedFloat(1, 511); + message.setRepeatedDouble(1, 512); + message.setRepeatedBool(1, true); + message.setRepeatedString(1, "515"); + message.setRepeatedBytes(1, toBytes("516")); + + message.setRepeatedGroup(1, TestAllTypes.RepeatedGroup.newBuilder().setA(517).build()); + message.setRepeatedNestedMessage(1, TestAllTypes.NestedMessage.newBuilder().setBb(518).build()); + message.setRepeatedForeignMessage(1, ForeignMessage.newBuilder().setC(519).build()); + message.setRepeatedImportMessage(1, ImportMessage.newBuilder().setD(520).build()); + message.setRepeatedLazyMessage(1, TestAllTypes.NestedMessage.newBuilder().setBb(527).build()); + + message.setRepeatedNestedEnum(1, TestAllTypes.NestedEnum.FOO); message.setRepeatedForeignEnum(1, ForeignEnum.FOREIGN_FOO); - message.setRepeatedImportEnum (1, ImportEnum.IMPORT_FOO); + message.setRepeatedImportEnum(1, ImportEnum.IMPORT_FOO); message.setRepeatedStringPiece(1, "524"); message.setRepeatedCord(1, "525"); @@ -507,65 +483,65 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all fields of - * {@code message} are set to the values assigned by {@code setAllFields}. + * Assert (using {@code junit.framework.Assert}} that all fields of {@code message} are set to the + * values assigned by {@code setAllFields}. */ public static void assertAllFieldsSet(TestAllTypesOrBuilder message) { - Assert.assertTrue(message.hasOptionalInt32 ()); - Assert.assertTrue(message.hasOptionalInt64 ()); - Assert.assertTrue(message.hasOptionalUint32 ()); - Assert.assertTrue(message.hasOptionalUint64 ()); - Assert.assertTrue(message.hasOptionalSint32 ()); - Assert.assertTrue(message.hasOptionalSint64 ()); - Assert.assertTrue(message.hasOptionalFixed32 ()); - Assert.assertTrue(message.hasOptionalFixed64 ()); + Assert.assertTrue(message.hasOptionalInt32()); + Assert.assertTrue(message.hasOptionalInt64()); + Assert.assertTrue(message.hasOptionalUint32()); + Assert.assertTrue(message.hasOptionalUint64()); + Assert.assertTrue(message.hasOptionalSint32()); + Assert.assertTrue(message.hasOptionalSint64()); + Assert.assertTrue(message.hasOptionalFixed32()); + Assert.assertTrue(message.hasOptionalFixed64()); Assert.assertTrue(message.hasOptionalSfixed32()); Assert.assertTrue(message.hasOptionalSfixed64()); - Assert.assertTrue(message.hasOptionalFloat ()); - Assert.assertTrue(message.hasOptionalDouble ()); - Assert.assertTrue(message.hasOptionalBool ()); - Assert.assertTrue(message.hasOptionalString ()); - Assert.assertTrue(message.hasOptionalBytes ()); - - Assert.assertTrue(message.hasOptionalGroup ()); - Assert.assertTrue(message.hasOptionalNestedMessage ()); + Assert.assertTrue(message.hasOptionalFloat()); + Assert.assertTrue(message.hasOptionalDouble()); + Assert.assertTrue(message.hasOptionalBool()); + Assert.assertTrue(message.hasOptionalString()); + Assert.assertTrue(message.hasOptionalBytes()); + + Assert.assertTrue(message.hasOptionalGroup()); + Assert.assertTrue(message.hasOptionalNestedMessage()); Assert.assertTrue(message.hasOptionalForeignMessage()); - Assert.assertTrue(message.hasOptionalImportMessage ()); + Assert.assertTrue(message.hasOptionalImportMessage()); - Assert.assertTrue(message.getOptionalGroup ().hasA()); - Assert.assertTrue(message.getOptionalNestedMessage ().hasBb()); + Assert.assertTrue(message.getOptionalGroup().hasA()); + Assert.assertTrue(message.getOptionalNestedMessage().hasBb()); Assert.assertTrue(message.getOptionalForeignMessage().hasC()); - Assert.assertTrue(message.getOptionalImportMessage ().hasD()); + Assert.assertTrue(message.getOptionalImportMessage().hasD()); - Assert.assertTrue(message.hasOptionalNestedEnum ()); + Assert.assertTrue(message.hasOptionalNestedEnum()); Assert.assertTrue(message.hasOptionalForeignEnum()); - Assert.assertTrue(message.hasOptionalImportEnum ()); + Assert.assertTrue(message.hasOptionalImportEnum()); Assert.assertTrue(message.hasOptionalStringPiece()); Assert.assertTrue(message.hasOptionalCord()); - Assert.assertEquals(101 , message.getOptionalInt32 ()); - Assert.assertEquals(102 , message.getOptionalInt64 ()); - Assert.assertEquals(103 , message.getOptionalUint32 ()); - Assert.assertEquals(104 , message.getOptionalUint64 ()); - Assert.assertEquals(105 , message.getOptionalSint32 ()); - Assert.assertEquals(106 , message.getOptionalSint64 ()); - Assert.assertEquals(107 , message.getOptionalFixed32 ()); - Assert.assertEquals(108 , message.getOptionalFixed64 ()); - Assert.assertEquals(109 , message.getOptionalSfixed32()); - Assert.assertEquals(110 , message.getOptionalSfixed64()); - Assert.assertEquals(111 , message.getOptionalFloat (), 0.0); - Assert.assertEquals(112 , message.getOptionalDouble (), 0.0); - Assert.assertEquals(true , message.getOptionalBool ()); - Assert.assertEquals("115", message.getOptionalString ()); + Assert.assertEquals(101, message.getOptionalInt32()); + Assert.assertEquals(102, message.getOptionalInt64()); + Assert.assertEquals(103, message.getOptionalUint32()); + Assert.assertEquals(104, message.getOptionalUint64()); + Assert.assertEquals(105, message.getOptionalSint32()); + Assert.assertEquals(106, message.getOptionalSint64()); + Assert.assertEquals(107, message.getOptionalFixed32()); + Assert.assertEquals(108, message.getOptionalFixed64()); + Assert.assertEquals(109, message.getOptionalSfixed32()); + Assert.assertEquals(110, message.getOptionalSfixed64()); + Assert.assertEquals(111, message.getOptionalFloat(), 0.0); + Assert.assertEquals(112, message.getOptionalDouble(), 0.0); + Assert.assertEquals(true, message.getOptionalBool()); + Assert.assertEquals("115", message.getOptionalString()); Assert.assertEquals(toBytes("116"), message.getOptionalBytes()); - Assert.assertEquals(117, message.getOptionalGroup ().getA()); - Assert.assertEquals(118, message.getOptionalNestedMessage ().getBb()); - Assert.assertEquals(119, message.getOptionalForeignMessage ().getC()); - Assert.assertEquals(120, message.getOptionalImportMessage ().getD()); + Assert.assertEquals(117, message.getOptionalGroup().getA()); + Assert.assertEquals(118, message.getOptionalNestedMessage().getBb()); + Assert.assertEquals(119, message.getOptionalForeignMessage().getC()); + Assert.assertEquals(120, message.getOptionalImportMessage().getD()); Assert.assertEquals(126, message.getOptionalPublicImportMessage().getE()); - Assert.assertEquals(127, message.getOptionalLazyMessage ().getBb()); + Assert.assertEquals(127, message.getOptionalLazyMessage().getBb()); Assert.assertEquals(TestAllTypes.NestedEnum.BAZ, message.getOptionalNestedEnum()); Assert.assertEquals(ForeignEnum.FOREIGN_BAZ, message.getOptionalForeignEnum()); @@ -576,86 +552,86 @@ public final class TestUtil { // ----------------------------------------------------------------- - Assert.assertEquals(2, message.getRepeatedInt32Count ()); - Assert.assertEquals(2, message.getRepeatedInt64Count ()); - Assert.assertEquals(2, message.getRepeatedUint32Count ()); - Assert.assertEquals(2, message.getRepeatedUint64Count ()); - Assert.assertEquals(2, message.getRepeatedSint32Count ()); - Assert.assertEquals(2, message.getRepeatedSint64Count ()); - Assert.assertEquals(2, message.getRepeatedFixed32Count ()); - Assert.assertEquals(2, message.getRepeatedFixed64Count ()); + Assert.assertEquals(2, message.getRepeatedInt32Count()); + Assert.assertEquals(2, message.getRepeatedInt64Count()); + Assert.assertEquals(2, message.getRepeatedUint32Count()); + Assert.assertEquals(2, message.getRepeatedUint64Count()); + Assert.assertEquals(2, message.getRepeatedSint32Count()); + Assert.assertEquals(2, message.getRepeatedSint64Count()); + Assert.assertEquals(2, message.getRepeatedFixed32Count()); + Assert.assertEquals(2, message.getRepeatedFixed64Count()); Assert.assertEquals(2, message.getRepeatedSfixed32Count()); Assert.assertEquals(2, message.getRepeatedSfixed64Count()); - Assert.assertEquals(2, message.getRepeatedFloatCount ()); - Assert.assertEquals(2, message.getRepeatedDoubleCount ()); - Assert.assertEquals(2, message.getRepeatedBoolCount ()); - Assert.assertEquals(2, message.getRepeatedStringCount ()); - Assert.assertEquals(2, message.getRepeatedBytesCount ()); - - Assert.assertEquals(2, message.getRepeatedGroupCount ()); - Assert.assertEquals(2, message.getRepeatedNestedMessageCount ()); + Assert.assertEquals(2, message.getRepeatedFloatCount()); + Assert.assertEquals(2, message.getRepeatedDoubleCount()); + Assert.assertEquals(2, message.getRepeatedBoolCount()); + Assert.assertEquals(2, message.getRepeatedStringCount()); + Assert.assertEquals(2, message.getRepeatedBytesCount()); + + Assert.assertEquals(2, message.getRepeatedGroupCount()); + Assert.assertEquals(2, message.getRepeatedNestedMessageCount()); Assert.assertEquals(2, message.getRepeatedForeignMessageCount()); - Assert.assertEquals(2, message.getRepeatedImportMessageCount ()); - Assert.assertEquals(2, message.getRepeatedLazyMessageCount ()); - Assert.assertEquals(2, message.getRepeatedNestedEnumCount ()); - Assert.assertEquals(2, message.getRepeatedForeignEnumCount ()); - Assert.assertEquals(2, message.getRepeatedImportEnumCount ()); + Assert.assertEquals(2, message.getRepeatedImportMessageCount()); + Assert.assertEquals(2, message.getRepeatedLazyMessageCount()); + Assert.assertEquals(2, message.getRepeatedNestedEnumCount()); + Assert.assertEquals(2, message.getRepeatedForeignEnumCount()); + Assert.assertEquals(2, message.getRepeatedImportEnumCount()); Assert.assertEquals(2, message.getRepeatedStringPieceCount()); Assert.assertEquals(2, message.getRepeatedCordCount()); - Assert.assertEquals(201 , message.getRepeatedInt32 (0)); - Assert.assertEquals(202 , message.getRepeatedInt64 (0)); - Assert.assertEquals(203 , message.getRepeatedUint32 (0)); - Assert.assertEquals(204 , message.getRepeatedUint64 (0)); - Assert.assertEquals(205 , message.getRepeatedSint32 (0)); - Assert.assertEquals(206 , message.getRepeatedSint64 (0)); - Assert.assertEquals(207 , message.getRepeatedFixed32 (0)); - Assert.assertEquals(208 , message.getRepeatedFixed64 (0)); - Assert.assertEquals(209 , message.getRepeatedSfixed32(0)); - Assert.assertEquals(210 , message.getRepeatedSfixed64(0)); - Assert.assertEquals(211 , message.getRepeatedFloat (0), 0.0); - Assert.assertEquals(212 , message.getRepeatedDouble (0), 0.0); - Assert.assertEquals(true , message.getRepeatedBool (0)); - Assert.assertEquals("215", message.getRepeatedString (0)); + Assert.assertEquals(201, message.getRepeatedInt32(0)); + Assert.assertEquals(202, message.getRepeatedInt64(0)); + Assert.assertEquals(203, message.getRepeatedUint32(0)); + Assert.assertEquals(204, message.getRepeatedUint64(0)); + Assert.assertEquals(205, message.getRepeatedSint32(0)); + Assert.assertEquals(206, message.getRepeatedSint64(0)); + Assert.assertEquals(207, message.getRepeatedFixed32(0)); + Assert.assertEquals(208, message.getRepeatedFixed64(0)); + Assert.assertEquals(209, message.getRepeatedSfixed32(0)); + Assert.assertEquals(210, message.getRepeatedSfixed64(0)); + Assert.assertEquals(211, message.getRepeatedFloat(0), 0.0); + Assert.assertEquals(212, message.getRepeatedDouble(0), 0.0); + Assert.assertEquals(true, message.getRepeatedBool(0)); + Assert.assertEquals("215", message.getRepeatedString(0)); Assert.assertEquals(toBytes("216"), message.getRepeatedBytes(0)); - Assert.assertEquals(217, message.getRepeatedGroup (0).getA()); - Assert.assertEquals(218, message.getRepeatedNestedMessage (0).getBb()); + Assert.assertEquals(217, message.getRepeatedGroup(0).getA()); + Assert.assertEquals(218, message.getRepeatedNestedMessage(0).getBb()); Assert.assertEquals(219, message.getRepeatedForeignMessage(0).getC()); - Assert.assertEquals(220, message.getRepeatedImportMessage (0).getD()); - Assert.assertEquals(227, message.getRepeatedLazyMessage (0).getBb()); + Assert.assertEquals(220, message.getRepeatedImportMessage(0).getD()); + Assert.assertEquals(227, message.getRepeatedLazyMessage(0).getBb()); - Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getRepeatedNestedEnum (0)); + Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getRepeatedNestedEnum(0)); Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getRepeatedForeignEnum(0)); Assert.assertEquals(ImportEnum.IMPORT_BAR, message.getRepeatedImportEnum(0)); Assert.assertEquals("224", message.getRepeatedStringPiece(0)); Assert.assertEquals("225", message.getRepeatedCord(0)); - Assert.assertEquals(301 , message.getRepeatedInt32 (1)); - Assert.assertEquals(302 , message.getRepeatedInt64 (1)); - Assert.assertEquals(303 , message.getRepeatedUint32 (1)); - Assert.assertEquals(304 , message.getRepeatedUint64 (1)); - Assert.assertEquals(305 , message.getRepeatedSint32 (1)); - Assert.assertEquals(306 , message.getRepeatedSint64 (1)); - Assert.assertEquals(307 , message.getRepeatedFixed32 (1)); - Assert.assertEquals(308 , message.getRepeatedFixed64 (1)); - Assert.assertEquals(309 , message.getRepeatedSfixed32(1)); - Assert.assertEquals(310 , message.getRepeatedSfixed64(1)); - Assert.assertEquals(311 , message.getRepeatedFloat (1), 0.0); - Assert.assertEquals(312 , message.getRepeatedDouble (1), 0.0); - Assert.assertEquals(false, message.getRepeatedBool (1)); - Assert.assertEquals("315", message.getRepeatedString (1)); + Assert.assertEquals(301, message.getRepeatedInt32(1)); + Assert.assertEquals(302, message.getRepeatedInt64(1)); + Assert.assertEquals(303, message.getRepeatedUint32(1)); + Assert.assertEquals(304, message.getRepeatedUint64(1)); + Assert.assertEquals(305, message.getRepeatedSint32(1)); + Assert.assertEquals(306, message.getRepeatedSint64(1)); + Assert.assertEquals(307, message.getRepeatedFixed32(1)); + Assert.assertEquals(308, message.getRepeatedFixed64(1)); + Assert.assertEquals(309, message.getRepeatedSfixed32(1)); + Assert.assertEquals(310, message.getRepeatedSfixed64(1)); + Assert.assertEquals(311, message.getRepeatedFloat(1), 0.0); + Assert.assertEquals(312, message.getRepeatedDouble(1), 0.0); + Assert.assertEquals(false, message.getRepeatedBool(1)); + Assert.assertEquals("315", message.getRepeatedString(1)); Assert.assertEquals(toBytes("316"), message.getRepeatedBytes(1)); - Assert.assertEquals(317, message.getRepeatedGroup (1).getA()); - Assert.assertEquals(318, message.getRepeatedNestedMessage (1).getBb()); + Assert.assertEquals(317, message.getRepeatedGroup(1).getA()); + Assert.assertEquals(318, message.getRepeatedNestedMessage(1).getBb()); Assert.assertEquals(319, message.getRepeatedForeignMessage(1).getC()); - Assert.assertEquals(320, message.getRepeatedImportMessage (1).getD()); - Assert.assertEquals(327, message.getRepeatedLazyMessage (1).getBb()); + Assert.assertEquals(320, message.getRepeatedImportMessage(1).getD()); + Assert.assertEquals(327, message.getRepeatedLazyMessage(1).getBb()); - Assert.assertEquals(TestAllTypes.NestedEnum.BAZ, message.getRepeatedNestedEnum (1)); + Assert.assertEquals(TestAllTypes.NestedEnum.BAZ, message.getRepeatedNestedEnum(1)); Assert.assertEquals(ForeignEnum.FOREIGN_BAZ, message.getRepeatedForeignEnum(1)); Assert.assertEquals(ImportEnum.IMPORT_BAZ, message.getRepeatedImportEnum(1)); @@ -664,46 +640,46 @@ public final class TestUtil { // ----------------------------------------------------------------- - Assert.assertTrue(message.hasDefaultInt32 ()); - Assert.assertTrue(message.hasDefaultInt64 ()); - Assert.assertTrue(message.hasDefaultUint32 ()); - Assert.assertTrue(message.hasDefaultUint64 ()); - Assert.assertTrue(message.hasDefaultSint32 ()); - Assert.assertTrue(message.hasDefaultSint64 ()); - Assert.assertTrue(message.hasDefaultFixed32 ()); - Assert.assertTrue(message.hasDefaultFixed64 ()); + Assert.assertTrue(message.hasDefaultInt32()); + Assert.assertTrue(message.hasDefaultInt64()); + Assert.assertTrue(message.hasDefaultUint32()); + Assert.assertTrue(message.hasDefaultUint64()); + Assert.assertTrue(message.hasDefaultSint32()); + Assert.assertTrue(message.hasDefaultSint64()); + Assert.assertTrue(message.hasDefaultFixed32()); + Assert.assertTrue(message.hasDefaultFixed64()); Assert.assertTrue(message.hasDefaultSfixed32()); Assert.assertTrue(message.hasDefaultSfixed64()); - Assert.assertTrue(message.hasDefaultFloat ()); - Assert.assertTrue(message.hasDefaultDouble ()); - Assert.assertTrue(message.hasDefaultBool ()); - Assert.assertTrue(message.hasDefaultString ()); - Assert.assertTrue(message.hasDefaultBytes ()); + Assert.assertTrue(message.hasDefaultFloat()); + Assert.assertTrue(message.hasDefaultDouble()); + Assert.assertTrue(message.hasDefaultBool()); + Assert.assertTrue(message.hasDefaultString()); + Assert.assertTrue(message.hasDefaultBytes()); - Assert.assertTrue(message.hasDefaultNestedEnum ()); + Assert.assertTrue(message.hasDefaultNestedEnum()); Assert.assertTrue(message.hasDefaultForeignEnum()); - Assert.assertTrue(message.hasDefaultImportEnum ()); + Assert.assertTrue(message.hasDefaultImportEnum()); Assert.assertTrue(message.hasDefaultStringPiece()); Assert.assertTrue(message.hasDefaultCord()); - Assert.assertEquals(401 , message.getDefaultInt32 ()); - Assert.assertEquals(402 , message.getDefaultInt64 ()); - Assert.assertEquals(403 , message.getDefaultUint32 ()); - Assert.assertEquals(404 , message.getDefaultUint64 ()); - Assert.assertEquals(405 , message.getDefaultSint32 ()); - Assert.assertEquals(406 , message.getDefaultSint64 ()); - Assert.assertEquals(407 , message.getDefaultFixed32 ()); - Assert.assertEquals(408 , message.getDefaultFixed64 ()); - Assert.assertEquals(409 , message.getDefaultSfixed32()); - Assert.assertEquals(410 , message.getDefaultSfixed64()); - Assert.assertEquals(411 , message.getDefaultFloat (), 0.0); - Assert.assertEquals(412 , message.getDefaultDouble (), 0.0); - Assert.assertEquals(false, message.getDefaultBool ()); - Assert.assertEquals("415", message.getDefaultString ()); + Assert.assertEquals(401, message.getDefaultInt32()); + Assert.assertEquals(402, message.getDefaultInt64()); + Assert.assertEquals(403, message.getDefaultUint32()); + Assert.assertEquals(404, message.getDefaultUint64()); + Assert.assertEquals(405, message.getDefaultSint32()); + Assert.assertEquals(406, message.getDefaultSint64()); + Assert.assertEquals(407, message.getDefaultFixed32()); + Assert.assertEquals(408, message.getDefaultFixed64()); + Assert.assertEquals(409, message.getDefaultSfixed32()); + Assert.assertEquals(410, message.getDefaultSfixed64()); + Assert.assertEquals(411, message.getDefaultFloat(), 0.0); + Assert.assertEquals(412, message.getDefaultDouble(), 0.0); + Assert.assertEquals(false, message.getDefaultBool()); + Assert.assertEquals("415", message.getDefaultString()); Assert.assertEquals(toBytes("416"), message.getDefaultBytes()); - Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getDefaultNestedEnum ()); + Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getDefaultNestedEnum()); Assert.assertEquals(ForeignEnum.FOREIGN_FOO, message.getDefaultForeignEnum()); Assert.assertEquals(ImportEnum.IMPORT_FOO, message.getDefaultImportEnum()); @@ -721,74 +697,73 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all fields of - * {@code message} are cleared, and that getting the fields returns their - * default values. + * Assert (using {@code junit.framework.Assert}} that all fields of {@code message} are cleared, + * and that getting the fields returns their default values. */ public static void assertClear(TestAllTypesOrBuilder message) { // hasBlah() should initially be false for all optional fields. - Assert.assertFalse(message.hasOptionalInt32 ()); - Assert.assertFalse(message.hasOptionalInt64 ()); - Assert.assertFalse(message.hasOptionalUint32 ()); - Assert.assertFalse(message.hasOptionalUint64 ()); - Assert.assertFalse(message.hasOptionalSint32 ()); - Assert.assertFalse(message.hasOptionalSint64 ()); - Assert.assertFalse(message.hasOptionalFixed32 ()); - Assert.assertFalse(message.hasOptionalFixed64 ()); + Assert.assertFalse(message.hasOptionalInt32()); + Assert.assertFalse(message.hasOptionalInt64()); + Assert.assertFalse(message.hasOptionalUint32()); + Assert.assertFalse(message.hasOptionalUint64()); + Assert.assertFalse(message.hasOptionalSint32()); + Assert.assertFalse(message.hasOptionalSint64()); + Assert.assertFalse(message.hasOptionalFixed32()); + Assert.assertFalse(message.hasOptionalFixed64()); Assert.assertFalse(message.hasOptionalSfixed32()); Assert.assertFalse(message.hasOptionalSfixed64()); - Assert.assertFalse(message.hasOptionalFloat ()); - Assert.assertFalse(message.hasOptionalDouble ()); - Assert.assertFalse(message.hasOptionalBool ()); - Assert.assertFalse(message.hasOptionalString ()); - Assert.assertFalse(message.hasOptionalBytes ()); - - Assert.assertFalse(message.hasOptionalGroup ()); - Assert.assertFalse(message.hasOptionalNestedMessage ()); + Assert.assertFalse(message.hasOptionalFloat()); + Assert.assertFalse(message.hasOptionalDouble()); + Assert.assertFalse(message.hasOptionalBool()); + Assert.assertFalse(message.hasOptionalString()); + Assert.assertFalse(message.hasOptionalBytes()); + + Assert.assertFalse(message.hasOptionalGroup()); + Assert.assertFalse(message.hasOptionalNestedMessage()); Assert.assertFalse(message.hasOptionalForeignMessage()); - Assert.assertFalse(message.hasOptionalImportMessage ()); + Assert.assertFalse(message.hasOptionalImportMessage()); - Assert.assertFalse(message.hasOptionalNestedEnum ()); + Assert.assertFalse(message.hasOptionalNestedEnum()); Assert.assertFalse(message.hasOptionalForeignEnum()); - Assert.assertFalse(message.hasOptionalImportEnum ()); + Assert.assertFalse(message.hasOptionalImportEnum()); Assert.assertFalse(message.hasOptionalStringPiece()); Assert.assertFalse(message.hasOptionalCord()); // Optional fields without defaults are set to zero or something like it. - Assert.assertEquals(0 , message.getOptionalInt32 ()); - Assert.assertEquals(0 , message.getOptionalInt64 ()); - Assert.assertEquals(0 , message.getOptionalUint32 ()); - Assert.assertEquals(0 , message.getOptionalUint64 ()); - Assert.assertEquals(0 , message.getOptionalSint32 ()); - Assert.assertEquals(0 , message.getOptionalSint64 ()); - Assert.assertEquals(0 , message.getOptionalFixed32 ()); - Assert.assertEquals(0 , message.getOptionalFixed64 ()); - Assert.assertEquals(0 , message.getOptionalSfixed32()); - Assert.assertEquals(0 , message.getOptionalSfixed64()); - Assert.assertEquals(0 , message.getOptionalFloat (), 0.0); - Assert.assertEquals(0 , message.getOptionalDouble (), 0.0); - Assert.assertEquals(false, message.getOptionalBool ()); - Assert.assertEquals("" , message.getOptionalString ()); + Assert.assertEquals(0, message.getOptionalInt32()); + Assert.assertEquals(0, message.getOptionalInt64()); + Assert.assertEquals(0, message.getOptionalUint32()); + Assert.assertEquals(0, message.getOptionalUint64()); + Assert.assertEquals(0, message.getOptionalSint32()); + Assert.assertEquals(0, message.getOptionalSint64()); + Assert.assertEquals(0, message.getOptionalFixed32()); + Assert.assertEquals(0, message.getOptionalFixed64()); + Assert.assertEquals(0, message.getOptionalSfixed32()); + Assert.assertEquals(0, message.getOptionalSfixed64()); + Assert.assertEquals(0, message.getOptionalFloat(), 0.0); + Assert.assertEquals(0, message.getOptionalDouble(), 0.0); + Assert.assertEquals(false, message.getOptionalBool()); + Assert.assertEquals("", message.getOptionalString()); Assert.assertEquals(ByteString.EMPTY, message.getOptionalBytes()); // Embedded messages should also be clear. - Assert.assertFalse(message.getOptionalGroup ().hasA()); - Assert.assertFalse(message.getOptionalNestedMessage ().hasBb()); - Assert.assertFalse(message.getOptionalForeignMessage ().hasC()); - Assert.assertFalse(message.getOptionalImportMessage ().hasD()); + Assert.assertFalse(message.getOptionalGroup().hasA()); + Assert.assertFalse(message.getOptionalNestedMessage().hasBb()); + Assert.assertFalse(message.getOptionalForeignMessage().hasC()); + Assert.assertFalse(message.getOptionalImportMessage().hasD()); Assert.assertFalse(message.getOptionalPublicImportMessage().hasE()); - Assert.assertFalse(message.getOptionalLazyMessage ().hasBb()); + Assert.assertFalse(message.getOptionalLazyMessage().hasBb()); - Assert.assertEquals(0, message.getOptionalGroup ().getA()); - Assert.assertEquals(0, message.getOptionalNestedMessage ().getBb()); - Assert.assertEquals(0, message.getOptionalForeignMessage ().getC()); - Assert.assertEquals(0, message.getOptionalImportMessage ().getD()); + Assert.assertEquals(0, message.getOptionalGroup().getA()); + Assert.assertEquals(0, message.getOptionalNestedMessage().getBb()); + Assert.assertEquals(0, message.getOptionalForeignMessage().getC()); + Assert.assertEquals(0, message.getOptionalImportMessage().getD()); Assert.assertEquals(0, message.getOptionalPublicImportMessage().getE()); - Assert.assertEquals(0, message.getOptionalLazyMessage ().getBb()); + Assert.assertEquals(0, message.getOptionalLazyMessage().getBb()); // Enums without defaults are set to the first value in the enum. - Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getOptionalNestedEnum ()); + Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getOptionalNestedEnum()); Assert.assertEquals(ForeignEnum.FOREIGN_FOO, message.getOptionalForeignEnum()); Assert.assertEquals(ImportEnum.IMPORT_FOO, message.getOptionalImportEnum()); @@ -796,76 +771,76 @@ public final class TestUtil { Assert.assertEquals("", message.getOptionalCord()); // Repeated fields are empty. - Assert.assertEquals(0, message.getRepeatedInt32Count ()); - Assert.assertEquals(0, message.getRepeatedInt64Count ()); - Assert.assertEquals(0, message.getRepeatedUint32Count ()); - Assert.assertEquals(0, message.getRepeatedUint64Count ()); - Assert.assertEquals(0, message.getRepeatedSint32Count ()); - Assert.assertEquals(0, message.getRepeatedSint64Count ()); - Assert.assertEquals(0, message.getRepeatedFixed32Count ()); - Assert.assertEquals(0, message.getRepeatedFixed64Count ()); + Assert.assertEquals(0, message.getRepeatedInt32Count()); + Assert.assertEquals(0, message.getRepeatedInt64Count()); + Assert.assertEquals(0, message.getRepeatedUint32Count()); + Assert.assertEquals(0, message.getRepeatedUint64Count()); + Assert.assertEquals(0, message.getRepeatedSint32Count()); + Assert.assertEquals(0, message.getRepeatedSint64Count()); + Assert.assertEquals(0, message.getRepeatedFixed32Count()); + Assert.assertEquals(0, message.getRepeatedFixed64Count()); Assert.assertEquals(0, message.getRepeatedSfixed32Count()); Assert.assertEquals(0, message.getRepeatedSfixed64Count()); - Assert.assertEquals(0, message.getRepeatedFloatCount ()); - Assert.assertEquals(0, message.getRepeatedDoubleCount ()); - Assert.assertEquals(0, message.getRepeatedBoolCount ()); - Assert.assertEquals(0, message.getRepeatedStringCount ()); - Assert.assertEquals(0, message.getRepeatedBytesCount ()); - - Assert.assertEquals(0, message.getRepeatedGroupCount ()); - Assert.assertEquals(0, message.getRepeatedNestedMessageCount ()); + Assert.assertEquals(0, message.getRepeatedFloatCount()); + Assert.assertEquals(0, message.getRepeatedDoubleCount()); + Assert.assertEquals(0, message.getRepeatedBoolCount()); + Assert.assertEquals(0, message.getRepeatedStringCount()); + Assert.assertEquals(0, message.getRepeatedBytesCount()); + + Assert.assertEquals(0, message.getRepeatedGroupCount()); + Assert.assertEquals(0, message.getRepeatedNestedMessageCount()); Assert.assertEquals(0, message.getRepeatedForeignMessageCount()); - Assert.assertEquals(0, message.getRepeatedImportMessageCount ()); - Assert.assertEquals(0, message.getRepeatedLazyMessageCount ()); - Assert.assertEquals(0, message.getRepeatedNestedEnumCount ()); - Assert.assertEquals(0, message.getRepeatedForeignEnumCount ()); - Assert.assertEquals(0, message.getRepeatedImportEnumCount ()); + Assert.assertEquals(0, message.getRepeatedImportMessageCount()); + Assert.assertEquals(0, message.getRepeatedLazyMessageCount()); + Assert.assertEquals(0, message.getRepeatedNestedEnumCount()); + Assert.assertEquals(0, message.getRepeatedForeignEnumCount()); + Assert.assertEquals(0, message.getRepeatedImportEnumCount()); Assert.assertEquals(0, message.getRepeatedStringPieceCount()); Assert.assertEquals(0, message.getRepeatedCordCount()); // hasBlah() should also be false for all default fields. - Assert.assertFalse(message.hasDefaultInt32 ()); - Assert.assertFalse(message.hasDefaultInt64 ()); - Assert.assertFalse(message.hasDefaultUint32 ()); - Assert.assertFalse(message.hasDefaultUint64 ()); - Assert.assertFalse(message.hasDefaultSint32 ()); - Assert.assertFalse(message.hasDefaultSint64 ()); - Assert.assertFalse(message.hasDefaultFixed32 ()); - Assert.assertFalse(message.hasDefaultFixed64 ()); + Assert.assertFalse(message.hasDefaultInt32()); + Assert.assertFalse(message.hasDefaultInt64()); + Assert.assertFalse(message.hasDefaultUint32()); + Assert.assertFalse(message.hasDefaultUint64()); + Assert.assertFalse(message.hasDefaultSint32()); + Assert.assertFalse(message.hasDefaultSint64()); + Assert.assertFalse(message.hasDefaultFixed32()); + Assert.assertFalse(message.hasDefaultFixed64()); Assert.assertFalse(message.hasDefaultSfixed32()); Assert.assertFalse(message.hasDefaultSfixed64()); - Assert.assertFalse(message.hasDefaultFloat ()); - Assert.assertFalse(message.hasDefaultDouble ()); - Assert.assertFalse(message.hasDefaultBool ()); - Assert.assertFalse(message.hasDefaultString ()); - Assert.assertFalse(message.hasDefaultBytes ()); + Assert.assertFalse(message.hasDefaultFloat()); + Assert.assertFalse(message.hasDefaultDouble()); + Assert.assertFalse(message.hasDefaultBool()); + Assert.assertFalse(message.hasDefaultString()); + Assert.assertFalse(message.hasDefaultBytes()); - Assert.assertFalse(message.hasDefaultNestedEnum ()); + Assert.assertFalse(message.hasDefaultNestedEnum()); Assert.assertFalse(message.hasDefaultForeignEnum()); - Assert.assertFalse(message.hasDefaultImportEnum ()); + Assert.assertFalse(message.hasDefaultImportEnum()); Assert.assertFalse(message.hasDefaultStringPiece()); Assert.assertFalse(message.hasDefaultCord()); // Fields with defaults have their default values (duh). - Assert.assertEquals( 41 , message.getDefaultInt32 ()); - Assert.assertEquals( 42 , message.getDefaultInt64 ()); - Assert.assertEquals( 43 , message.getDefaultUint32 ()); - Assert.assertEquals( 44 , message.getDefaultUint64 ()); - Assert.assertEquals(-45 , message.getDefaultSint32 ()); - Assert.assertEquals( 46 , message.getDefaultSint64 ()); - Assert.assertEquals( 47 , message.getDefaultFixed32 ()); - Assert.assertEquals( 48 , message.getDefaultFixed64 ()); - Assert.assertEquals( 49 , message.getDefaultSfixed32()); - Assert.assertEquals(-50 , message.getDefaultSfixed64()); - Assert.assertEquals( 51.5 , message.getDefaultFloat (), 0.0); - Assert.assertEquals( 52e3 , message.getDefaultDouble (), 0.0); - Assert.assertEquals(true , message.getDefaultBool ()); - Assert.assertEquals("hello", message.getDefaultString ()); + Assert.assertEquals(41, message.getDefaultInt32()); + Assert.assertEquals(42, message.getDefaultInt64()); + Assert.assertEquals(43, message.getDefaultUint32()); + Assert.assertEquals(44, message.getDefaultUint64()); + Assert.assertEquals(-45, message.getDefaultSint32()); + Assert.assertEquals(46, message.getDefaultSint64()); + Assert.assertEquals(47, message.getDefaultFixed32()); + Assert.assertEquals(48, message.getDefaultFixed64()); + Assert.assertEquals(49, message.getDefaultSfixed32()); + Assert.assertEquals(-50, message.getDefaultSfixed64()); + Assert.assertEquals(51.5, message.getDefaultFloat(), 0.0); + Assert.assertEquals(52e3, message.getDefaultDouble(), 0.0); + Assert.assertEquals(true, message.getDefaultBool()); + Assert.assertEquals("hello", message.getDefaultString()); Assert.assertEquals(toBytes("world"), message.getDefaultBytes()); - Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getDefaultNestedEnum ()); + Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getDefaultNestedEnum()); Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getDefaultForeignEnum()); Assert.assertEquals(ImportEnum.IMPORT_BAR, message.getDefaultImportEnum()); @@ -881,66 +856,64 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all fields of - * {@code message} are set to the values assigned by {@code setAllFields} - * followed by {@code modifyRepeatedFields}. + * Assert (using {@code junit.framework.Assert}} that all fields of {@code message} are set to the + * values assigned by {@code setAllFields} followed by {@code modifyRepeatedFields}. */ - public static void assertRepeatedFieldsModified( - TestAllTypesOrBuilder message) { + public static void assertRepeatedFieldsModified(TestAllTypesOrBuilder message) { // ModifyRepeatedFields only sets the second repeated element of each // field. In addition to verifying this, we also verify that the first // element and size were *not* modified. - Assert.assertEquals(2, message.getRepeatedInt32Count ()); - Assert.assertEquals(2, message.getRepeatedInt64Count ()); - Assert.assertEquals(2, message.getRepeatedUint32Count ()); - Assert.assertEquals(2, message.getRepeatedUint64Count ()); - Assert.assertEquals(2, message.getRepeatedSint32Count ()); - Assert.assertEquals(2, message.getRepeatedSint64Count ()); - Assert.assertEquals(2, message.getRepeatedFixed32Count ()); - Assert.assertEquals(2, message.getRepeatedFixed64Count ()); + Assert.assertEquals(2, message.getRepeatedInt32Count()); + Assert.assertEquals(2, message.getRepeatedInt64Count()); + Assert.assertEquals(2, message.getRepeatedUint32Count()); + Assert.assertEquals(2, message.getRepeatedUint64Count()); + Assert.assertEquals(2, message.getRepeatedSint32Count()); + Assert.assertEquals(2, message.getRepeatedSint64Count()); + Assert.assertEquals(2, message.getRepeatedFixed32Count()); + Assert.assertEquals(2, message.getRepeatedFixed64Count()); Assert.assertEquals(2, message.getRepeatedSfixed32Count()); Assert.assertEquals(2, message.getRepeatedSfixed64Count()); - Assert.assertEquals(2, message.getRepeatedFloatCount ()); - Assert.assertEquals(2, message.getRepeatedDoubleCount ()); - Assert.assertEquals(2, message.getRepeatedBoolCount ()); - Assert.assertEquals(2, message.getRepeatedStringCount ()); - Assert.assertEquals(2, message.getRepeatedBytesCount ()); - - Assert.assertEquals(2, message.getRepeatedGroupCount ()); - Assert.assertEquals(2, message.getRepeatedNestedMessageCount ()); + Assert.assertEquals(2, message.getRepeatedFloatCount()); + Assert.assertEquals(2, message.getRepeatedDoubleCount()); + Assert.assertEquals(2, message.getRepeatedBoolCount()); + Assert.assertEquals(2, message.getRepeatedStringCount()); + Assert.assertEquals(2, message.getRepeatedBytesCount()); + + Assert.assertEquals(2, message.getRepeatedGroupCount()); + Assert.assertEquals(2, message.getRepeatedNestedMessageCount()); Assert.assertEquals(2, message.getRepeatedForeignMessageCount()); - Assert.assertEquals(2, message.getRepeatedImportMessageCount ()); - Assert.assertEquals(2, message.getRepeatedLazyMessageCount ()); - Assert.assertEquals(2, message.getRepeatedNestedEnumCount ()); - Assert.assertEquals(2, message.getRepeatedForeignEnumCount ()); - Assert.assertEquals(2, message.getRepeatedImportEnumCount ()); + Assert.assertEquals(2, message.getRepeatedImportMessageCount()); + Assert.assertEquals(2, message.getRepeatedLazyMessageCount()); + Assert.assertEquals(2, message.getRepeatedNestedEnumCount()); + Assert.assertEquals(2, message.getRepeatedForeignEnumCount()); + Assert.assertEquals(2, message.getRepeatedImportEnumCount()); Assert.assertEquals(2, message.getRepeatedStringPieceCount()); Assert.assertEquals(2, message.getRepeatedCordCount()); - Assert.assertEquals(201 , message.getRepeatedInt32 (0)); - Assert.assertEquals(202L , message.getRepeatedInt64 (0)); - Assert.assertEquals(203 , message.getRepeatedUint32 (0)); - Assert.assertEquals(204L , message.getRepeatedUint64 (0)); - Assert.assertEquals(205 , message.getRepeatedSint32 (0)); - Assert.assertEquals(206L , message.getRepeatedSint64 (0)); - Assert.assertEquals(207 , message.getRepeatedFixed32 (0)); - Assert.assertEquals(208L , message.getRepeatedFixed64 (0)); - Assert.assertEquals(209 , message.getRepeatedSfixed32(0)); - Assert.assertEquals(210L , message.getRepeatedSfixed64(0)); - Assert.assertEquals(211F , message.getRepeatedFloat (0)); - Assert.assertEquals(212D , message.getRepeatedDouble (0)); - Assert.assertEquals(true , message.getRepeatedBool (0)); - Assert.assertEquals("215", message.getRepeatedString (0)); + Assert.assertEquals(201, message.getRepeatedInt32(0)); + Assert.assertEquals(202L, message.getRepeatedInt64(0)); + Assert.assertEquals(203, message.getRepeatedUint32(0)); + Assert.assertEquals(204L, message.getRepeatedUint64(0)); + Assert.assertEquals(205, message.getRepeatedSint32(0)); + Assert.assertEquals(206L, message.getRepeatedSint64(0)); + Assert.assertEquals(207, message.getRepeatedFixed32(0)); + Assert.assertEquals(208L, message.getRepeatedFixed64(0)); + Assert.assertEquals(209, message.getRepeatedSfixed32(0)); + Assert.assertEquals(210L, message.getRepeatedSfixed64(0)); + Assert.assertEquals(211F, message.getRepeatedFloat(0)); + Assert.assertEquals(212D, message.getRepeatedDouble(0)); + Assert.assertEquals(true, message.getRepeatedBool(0)); + Assert.assertEquals("215", message.getRepeatedString(0)); Assert.assertEquals(toBytes("216"), message.getRepeatedBytes(0)); - Assert.assertEquals(217, message.getRepeatedGroup (0).getA()); - Assert.assertEquals(218, message.getRepeatedNestedMessage (0).getBb()); + Assert.assertEquals(217, message.getRepeatedGroup(0).getA()); + Assert.assertEquals(218, message.getRepeatedNestedMessage(0).getBb()); Assert.assertEquals(219, message.getRepeatedForeignMessage(0).getC()); - Assert.assertEquals(220, message.getRepeatedImportMessage (0).getD()); - Assert.assertEquals(227, message.getRepeatedLazyMessage (0).getBb()); + Assert.assertEquals(220, message.getRepeatedImportMessage(0).getD()); + Assert.assertEquals(227, message.getRepeatedLazyMessage(0).getBb()); - Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getRepeatedNestedEnum (0)); + Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getRepeatedNestedEnum(0)); Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getRepeatedForeignEnum(0)); Assert.assertEquals(ImportEnum.IMPORT_BAR, message.getRepeatedImportEnum(0)); @@ -948,29 +921,29 @@ public final class TestUtil { Assert.assertEquals("225", message.getRepeatedCord(0)); // Actually verify the second (modified) elements now. - Assert.assertEquals(501 , message.getRepeatedInt32 (1)); - Assert.assertEquals(502L , message.getRepeatedInt64 (1)); - Assert.assertEquals(503 , message.getRepeatedUint32 (1)); - Assert.assertEquals(504L , message.getRepeatedUint64 (1)); - Assert.assertEquals(505 , message.getRepeatedSint32 (1)); - Assert.assertEquals(506L , message.getRepeatedSint64 (1)); - Assert.assertEquals(507 , message.getRepeatedFixed32 (1)); - Assert.assertEquals(508L , message.getRepeatedFixed64 (1)); - Assert.assertEquals(509 , message.getRepeatedSfixed32(1)); - Assert.assertEquals(510L , message.getRepeatedSfixed64(1)); - Assert.assertEquals(511F , message.getRepeatedFloat (1)); - Assert.assertEquals(512D , message.getRepeatedDouble (1)); - Assert.assertEquals(true , message.getRepeatedBool (1)); - Assert.assertEquals("515", message.getRepeatedString (1)); + Assert.assertEquals(501, message.getRepeatedInt32(1)); + Assert.assertEquals(502L, message.getRepeatedInt64(1)); + Assert.assertEquals(503, message.getRepeatedUint32(1)); + Assert.assertEquals(504L, message.getRepeatedUint64(1)); + Assert.assertEquals(505, message.getRepeatedSint32(1)); + Assert.assertEquals(506L, message.getRepeatedSint64(1)); + Assert.assertEquals(507, message.getRepeatedFixed32(1)); + Assert.assertEquals(508L, message.getRepeatedFixed64(1)); + Assert.assertEquals(509, message.getRepeatedSfixed32(1)); + Assert.assertEquals(510L, message.getRepeatedSfixed64(1)); + Assert.assertEquals(511F, message.getRepeatedFloat(1)); + Assert.assertEquals(512D, message.getRepeatedDouble(1)); + Assert.assertEquals(true, message.getRepeatedBool(1)); + Assert.assertEquals("515", message.getRepeatedString(1)); Assert.assertEquals(toBytes("516"), message.getRepeatedBytes(1)); - Assert.assertEquals(517, message.getRepeatedGroup (1).getA()); - Assert.assertEquals(518, message.getRepeatedNestedMessage (1).getBb()); + Assert.assertEquals(517, message.getRepeatedGroup(1).getA()); + Assert.assertEquals(518, message.getRepeatedNestedMessage(1).getBb()); Assert.assertEquals(519, message.getRepeatedForeignMessage(1).getC()); - Assert.assertEquals(520, message.getRepeatedImportMessage (1).getD()); - Assert.assertEquals(527, message.getRepeatedLazyMessage (1).getBb()); + Assert.assertEquals(520, message.getRepeatedImportMessage(1).getD()); + Assert.assertEquals(527, message.getRepeatedLazyMessage(1).getBb()); - Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getRepeatedNestedEnum (1)); + Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getRepeatedNestedEnum(1)); Assert.assertEquals(ForeignEnum.FOREIGN_FOO, message.getRepeatedForeignEnum(1)); Assert.assertEquals(ImportEnum.IMPORT_FOO, message.getRepeatedImportEnum(1)); @@ -978,172 +951,170 @@ public final class TestUtil { Assert.assertEquals("525", message.getRepeatedCord(1)); } - /** - * Set every field of {@code message} to a unique value. - */ + /** Set every field of {@code message} to a unique value. */ public static void setPackedFields(TestPackedTypes.Builder message) { - message.addPackedInt32 (601); - message.addPackedInt64 (602); - message.addPackedUint32 (603); - message.addPackedUint64 (604); - message.addPackedSint32 (605); - message.addPackedSint64 (606); - message.addPackedFixed32 (607); - message.addPackedFixed64 (608); + message.addPackedInt32(601); + message.addPackedInt64(602); + message.addPackedUint32(603); + message.addPackedUint64(604); + message.addPackedSint32(605); + message.addPackedSint64(606); + message.addPackedFixed32(607); + message.addPackedFixed64(608); message.addPackedSfixed32(609); message.addPackedSfixed64(610); - message.addPackedFloat (611); - message.addPackedDouble (612); - message.addPackedBool (true); - message.addPackedEnum (ForeignEnum.FOREIGN_BAR); + message.addPackedFloat(611); + message.addPackedDouble(612); + message.addPackedBool(true); + message.addPackedEnum(ForeignEnum.FOREIGN_BAR); // Add a second one of each field. - message.addPackedInt32 (701); - message.addPackedInt64 (702); - message.addPackedUint32 (703); - message.addPackedUint64 (704); - message.addPackedSint32 (705); - message.addPackedSint64 (706); - message.addPackedFixed32 (707); - message.addPackedFixed64 (708); + message.addPackedInt32(701); + message.addPackedInt64(702); + message.addPackedUint32(703); + message.addPackedUint64(704); + message.addPackedSint32(705); + message.addPackedSint64(706); + message.addPackedFixed32(707); + message.addPackedFixed64(708); message.addPackedSfixed32(709); message.addPackedSfixed64(710); - message.addPackedFloat (711); - message.addPackedDouble (712); - message.addPackedBool (false); - message.addPackedEnum (ForeignEnum.FOREIGN_BAZ); + message.addPackedFloat(711); + message.addPackedDouble(712); + message.addPackedBool(false); + message.addPackedEnum(ForeignEnum.FOREIGN_BAZ); } /** - * Set every field of {@code message} to a unique value. Must correspond with - * the values applied by {@code setPackedFields}. + * Set every field of {@code message} to a unique value. Must correspond with the values applied + * by {@code setPackedFields}. */ public static void setUnpackedFields(TestUnpackedTypes.Builder message) { - message.addUnpackedInt32 (601); - message.addUnpackedInt64 (602); - message.addUnpackedUint32 (603); - message.addUnpackedUint64 (604); - message.addUnpackedSint32 (605); - message.addUnpackedSint64 (606); - message.addUnpackedFixed32 (607); - message.addUnpackedFixed64 (608); + message.addUnpackedInt32(601); + message.addUnpackedInt64(602); + message.addUnpackedUint32(603); + message.addUnpackedUint64(604); + message.addUnpackedSint32(605); + message.addUnpackedSint64(606); + message.addUnpackedFixed32(607); + message.addUnpackedFixed64(608); message.addUnpackedSfixed32(609); message.addUnpackedSfixed64(610); - message.addUnpackedFloat (611); - message.addUnpackedDouble (612); - message.addUnpackedBool (true); - message.addUnpackedEnum (ForeignEnum.FOREIGN_BAR); + message.addUnpackedFloat(611); + message.addUnpackedDouble(612); + message.addUnpackedBool(true); + message.addUnpackedEnum(ForeignEnum.FOREIGN_BAR); // Add a second one of each field. - message.addUnpackedInt32 (701); - message.addUnpackedInt64 (702); - message.addUnpackedUint32 (703); - message.addUnpackedUint64 (704); - message.addUnpackedSint32 (705); - message.addUnpackedSint64 (706); - message.addUnpackedFixed32 (707); - message.addUnpackedFixed64 (708); + message.addUnpackedInt32(701); + message.addUnpackedInt64(702); + message.addUnpackedUint32(703); + message.addUnpackedUint64(704); + message.addUnpackedSint32(705); + message.addUnpackedSint64(706); + message.addUnpackedFixed32(707); + message.addUnpackedFixed64(708); message.addUnpackedSfixed32(709); message.addUnpackedSfixed64(710); - message.addUnpackedFloat (711); - message.addUnpackedDouble (712); - message.addUnpackedBool (false); - message.addUnpackedEnum (ForeignEnum.FOREIGN_BAZ); + message.addUnpackedFloat(711); + message.addUnpackedDouble(712); + message.addUnpackedBool(false); + message.addUnpackedEnum(ForeignEnum.FOREIGN_BAZ); } /** - * Assert (using {@code junit.framework.Assert}} that all fields of - * {@code message} are set to the values assigned by {@code setPackedFields}. + * Assert (using {@code junit.framework.Assert}} that all fields of {@code message} are set to the + * values assigned by {@code setPackedFields}. */ public static void assertPackedFieldsSet(TestPackedTypes message) { - Assert.assertEquals(2, message.getPackedInt32Count ()); - Assert.assertEquals(2, message.getPackedInt64Count ()); - Assert.assertEquals(2, message.getPackedUint32Count ()); - Assert.assertEquals(2, message.getPackedUint64Count ()); - Assert.assertEquals(2, message.getPackedSint32Count ()); - Assert.assertEquals(2, message.getPackedSint64Count ()); - Assert.assertEquals(2, message.getPackedFixed32Count ()); - Assert.assertEquals(2, message.getPackedFixed64Count ()); + Assert.assertEquals(2, message.getPackedInt32Count()); + Assert.assertEquals(2, message.getPackedInt64Count()); + Assert.assertEquals(2, message.getPackedUint32Count()); + Assert.assertEquals(2, message.getPackedUint64Count()); + Assert.assertEquals(2, message.getPackedSint32Count()); + Assert.assertEquals(2, message.getPackedSint64Count()); + Assert.assertEquals(2, message.getPackedFixed32Count()); + Assert.assertEquals(2, message.getPackedFixed64Count()); Assert.assertEquals(2, message.getPackedSfixed32Count()); Assert.assertEquals(2, message.getPackedSfixed64Count()); - Assert.assertEquals(2, message.getPackedFloatCount ()); - Assert.assertEquals(2, message.getPackedDoubleCount ()); - Assert.assertEquals(2, message.getPackedBoolCount ()); - Assert.assertEquals(2, message.getPackedEnumCount ()); - Assert.assertEquals(601 , message.getPackedInt32 (0)); - Assert.assertEquals(602 , message.getPackedInt64 (0)); - Assert.assertEquals(603 , message.getPackedUint32 (0)); - Assert.assertEquals(604 , message.getPackedUint64 (0)); - Assert.assertEquals(605 , message.getPackedSint32 (0)); - Assert.assertEquals(606 , message.getPackedSint64 (0)); - Assert.assertEquals(607 , message.getPackedFixed32 (0)); - Assert.assertEquals(608 , message.getPackedFixed64 (0)); - Assert.assertEquals(609 , message.getPackedSfixed32(0)); - Assert.assertEquals(610 , message.getPackedSfixed64(0)); - Assert.assertEquals(611 , message.getPackedFloat (0), 0.0); - Assert.assertEquals(612 , message.getPackedDouble (0), 0.0); - Assert.assertEquals(true , message.getPackedBool (0)); + Assert.assertEquals(2, message.getPackedFloatCount()); + Assert.assertEquals(2, message.getPackedDoubleCount()); + Assert.assertEquals(2, message.getPackedBoolCount()); + Assert.assertEquals(2, message.getPackedEnumCount()); + Assert.assertEquals(601, message.getPackedInt32(0)); + Assert.assertEquals(602, message.getPackedInt64(0)); + Assert.assertEquals(603, message.getPackedUint32(0)); + Assert.assertEquals(604, message.getPackedUint64(0)); + Assert.assertEquals(605, message.getPackedSint32(0)); + Assert.assertEquals(606, message.getPackedSint64(0)); + Assert.assertEquals(607, message.getPackedFixed32(0)); + Assert.assertEquals(608, message.getPackedFixed64(0)); + Assert.assertEquals(609, message.getPackedSfixed32(0)); + Assert.assertEquals(610, message.getPackedSfixed64(0)); + Assert.assertEquals(611, message.getPackedFloat(0), 0.0); + Assert.assertEquals(612, message.getPackedDouble(0), 0.0); + Assert.assertEquals(true, message.getPackedBool(0)); Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getPackedEnum(0)); - Assert.assertEquals(701 , message.getPackedInt32 (1)); - Assert.assertEquals(702 , message.getPackedInt64 (1)); - Assert.assertEquals(703 , message.getPackedUint32 (1)); - Assert.assertEquals(704 , message.getPackedUint64 (1)); - Assert.assertEquals(705 , message.getPackedSint32 (1)); - Assert.assertEquals(706 , message.getPackedSint64 (1)); - Assert.assertEquals(707 , message.getPackedFixed32 (1)); - Assert.assertEquals(708 , message.getPackedFixed64 (1)); - Assert.assertEquals(709 , message.getPackedSfixed32(1)); - Assert.assertEquals(710 , message.getPackedSfixed64(1)); - Assert.assertEquals(711 , message.getPackedFloat (1), 0.0); - Assert.assertEquals(712 , message.getPackedDouble (1), 0.0); - Assert.assertEquals(false, message.getPackedBool (1)); + Assert.assertEquals(701, message.getPackedInt32(1)); + Assert.assertEquals(702, message.getPackedInt64(1)); + Assert.assertEquals(703, message.getPackedUint32(1)); + Assert.assertEquals(704, message.getPackedUint64(1)); + Assert.assertEquals(705, message.getPackedSint32(1)); + Assert.assertEquals(706, message.getPackedSint64(1)); + Assert.assertEquals(707, message.getPackedFixed32(1)); + Assert.assertEquals(708, message.getPackedFixed64(1)); + Assert.assertEquals(709, message.getPackedSfixed32(1)); + Assert.assertEquals(710, message.getPackedSfixed64(1)); + Assert.assertEquals(711, message.getPackedFloat(1), 0.0); + Assert.assertEquals(712, message.getPackedDouble(1), 0.0); + Assert.assertEquals(false, message.getPackedBool(1)); Assert.assertEquals(ForeignEnum.FOREIGN_BAZ, message.getPackedEnum(1)); } /** - * Assert (using {@code junit.framework.Assert}} that all fields of - * {@code message} are set to the values assigned by {@code setUnpackedFields}. + * Assert (using {@code junit.framework.Assert}} that all fields of {@code message} are set to the + * values assigned by {@code setUnpackedFields}. */ public static void assertUnpackedFieldsSet(TestUnpackedTypes message) { - Assert.assertEquals(2, message.getUnpackedInt32Count ()); - Assert.assertEquals(2, message.getUnpackedInt64Count ()); - Assert.assertEquals(2, message.getUnpackedUint32Count ()); - Assert.assertEquals(2, message.getUnpackedUint64Count ()); - Assert.assertEquals(2, message.getUnpackedSint32Count ()); - Assert.assertEquals(2, message.getUnpackedSint64Count ()); - Assert.assertEquals(2, message.getUnpackedFixed32Count ()); - Assert.assertEquals(2, message.getUnpackedFixed64Count ()); + Assert.assertEquals(2, message.getUnpackedInt32Count()); + Assert.assertEquals(2, message.getUnpackedInt64Count()); + Assert.assertEquals(2, message.getUnpackedUint32Count()); + Assert.assertEquals(2, message.getUnpackedUint64Count()); + Assert.assertEquals(2, message.getUnpackedSint32Count()); + Assert.assertEquals(2, message.getUnpackedSint64Count()); + Assert.assertEquals(2, message.getUnpackedFixed32Count()); + Assert.assertEquals(2, message.getUnpackedFixed64Count()); Assert.assertEquals(2, message.getUnpackedSfixed32Count()); Assert.assertEquals(2, message.getUnpackedSfixed64Count()); - Assert.assertEquals(2, message.getUnpackedFloatCount ()); - Assert.assertEquals(2, message.getUnpackedDoubleCount ()); - Assert.assertEquals(2, message.getUnpackedBoolCount ()); - Assert.assertEquals(2, message.getUnpackedEnumCount ()); - Assert.assertEquals(601 , message.getUnpackedInt32 (0)); - Assert.assertEquals(602 , message.getUnpackedInt64 (0)); - Assert.assertEquals(603 , message.getUnpackedUint32 (0)); - Assert.assertEquals(604 , message.getUnpackedUint64 (0)); - Assert.assertEquals(605 , message.getUnpackedSint32 (0)); - Assert.assertEquals(606 , message.getUnpackedSint64 (0)); - Assert.assertEquals(607 , message.getUnpackedFixed32 (0)); - Assert.assertEquals(608 , message.getUnpackedFixed64 (0)); - Assert.assertEquals(609 , message.getUnpackedSfixed32(0)); - Assert.assertEquals(610 , message.getUnpackedSfixed64(0)); - Assert.assertEquals(611 , message.getUnpackedFloat (0), 0.0); - Assert.assertEquals(612 , message.getUnpackedDouble (0), 0.0); - Assert.assertEquals(true , message.getUnpackedBool (0)); + Assert.assertEquals(2, message.getUnpackedFloatCount()); + Assert.assertEquals(2, message.getUnpackedDoubleCount()); + Assert.assertEquals(2, message.getUnpackedBoolCount()); + Assert.assertEquals(2, message.getUnpackedEnumCount()); + Assert.assertEquals(601, message.getUnpackedInt32(0)); + Assert.assertEquals(602, message.getUnpackedInt64(0)); + Assert.assertEquals(603, message.getUnpackedUint32(0)); + Assert.assertEquals(604, message.getUnpackedUint64(0)); + Assert.assertEquals(605, message.getUnpackedSint32(0)); + Assert.assertEquals(606, message.getUnpackedSint64(0)); + Assert.assertEquals(607, message.getUnpackedFixed32(0)); + Assert.assertEquals(608, message.getUnpackedFixed64(0)); + Assert.assertEquals(609, message.getUnpackedSfixed32(0)); + Assert.assertEquals(610, message.getUnpackedSfixed64(0)); + Assert.assertEquals(611, message.getUnpackedFloat(0), 0.0); + Assert.assertEquals(612, message.getUnpackedDouble(0), 0.0); + Assert.assertEquals(true, message.getUnpackedBool(0)); Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getUnpackedEnum(0)); - Assert.assertEquals(701 , message.getUnpackedInt32 (1)); - Assert.assertEquals(702 , message.getUnpackedInt64 (1)); - Assert.assertEquals(703 , message.getUnpackedUint32 (1)); - Assert.assertEquals(704 , message.getUnpackedUint64 (1)); - Assert.assertEquals(705 , message.getUnpackedSint32 (1)); - Assert.assertEquals(706 , message.getUnpackedSint64 (1)); - Assert.assertEquals(707 , message.getUnpackedFixed32 (1)); - Assert.assertEquals(708 , message.getUnpackedFixed64 (1)); - Assert.assertEquals(709 , message.getUnpackedSfixed32(1)); - Assert.assertEquals(710 , message.getUnpackedSfixed64(1)); - Assert.assertEquals(711 , message.getUnpackedFloat (1), 0.0); - Assert.assertEquals(712 , message.getUnpackedDouble (1), 0.0); - Assert.assertEquals(false, message.getUnpackedBool (1)); + Assert.assertEquals(701, message.getUnpackedInt32(1)); + Assert.assertEquals(702, message.getUnpackedInt64(1)); + Assert.assertEquals(703, message.getUnpackedUint32(1)); + Assert.assertEquals(704, message.getUnpackedUint64(1)); + Assert.assertEquals(705, message.getUnpackedSint32(1)); + Assert.assertEquals(706, message.getUnpackedSint64(1)); + Assert.assertEquals(707, message.getUnpackedFixed32(1)); + Assert.assertEquals(708, message.getUnpackedFixed64(1)); + Assert.assertEquals(709, message.getUnpackedSfixed32(1)); + Assert.assertEquals(710, message.getUnpackedSfixed64(1)); + Assert.assertEquals(711, message.getUnpackedFloat(1), 0.0); + Assert.assertEquals(712, message.getUnpackedDouble(1), 0.0); + Assert.assertEquals(false, message.getUnpackedBool(1)); Assert.assertEquals(ForeignEnum.FOREIGN_BAZ, message.getUnpackedEnum(1)); } @@ -1156,50 +1127,59 @@ public final class TestUtil { private static void assertEqualsExactType(int a, int b) { Assert.assertEquals(a, b); } + private static void assertEqualsExactType(long a, long b) { Assert.assertEquals(a, b); } + private static void assertEqualsExactType(float a, float b) { Assert.assertEquals(a, b, 0.0); } + private static void assertEqualsExactType(double a, double b) { Assert.assertEquals(a, b, 0.0); } + private static void assertEqualsExactType(boolean a, boolean b) { Assert.assertEquals(a, b); } + private static void assertEqualsExactType(String a, String b) { Assert.assertEquals(a, b); } + private static void assertEqualsExactType(ByteString a, ByteString b) { Assert.assertEquals(a, b); } - private static void assertEqualsExactType(TestAllTypes.NestedEnum a, - TestAllTypes.NestedEnum b) { + + private static void assertEqualsExactType(TestAllTypes.NestedEnum a, TestAllTypes.NestedEnum b) { Assert.assertEquals(a, b); } + private static void assertEqualsExactType(ForeignEnum a, ForeignEnum b) { Assert.assertEquals(a, b); } + private static void assertEqualsExactType(ImportEnum a, ImportEnum b) { Assert.assertEquals(a, b); } - private static void assertEqualsExactType(TestAllTypesLite.NestedEnum a, - TestAllTypesLite.NestedEnum b) { + + private static void assertEqualsExactType( + TestAllTypesLite.NestedEnum a, TestAllTypesLite.NestedEnum b) { Assert.assertEquals(a, b); } - private static void assertEqualsExactType(ForeignEnumLite a, - ForeignEnumLite b) { + + private static void assertEqualsExactType(ForeignEnumLite a, ForeignEnumLite b) { Assert.assertEquals(a, b); } - private static void assertEqualsExactType(ImportEnumLite a, - ImportEnumLite b) { + + private static void assertEqualsExactType(ImportEnumLite a, ImportEnumLite b) { Assert.assertEquals(a, b); } /** - * Get an unmodifiable {@link ExtensionRegistry} containing all the - * extensions of {@code TestAllExtensions}. + * Get an unmodifiable {@link ExtensionRegistry} containing all the extensions of {@code + * TestAllExtensions}. */ public static ExtensionRegistryLite getExtensionRegistry() { ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); @@ -1209,8 +1189,8 @@ public final class TestUtil { // BEGIN FULL-RUNTIME /** - * Get an unmodifiable {@link ExtensionRegistry} containing all the - * extensions of {@code TestAllExtensions}. + * Get an unmodifiable {@link ExtensionRegistry} containing all the extensions of {@code + * TestAllExtensions}. */ public static ExtensionRegistry getFullExtensionRegistry() { ExtensionRegistry registry = ExtensionRegistry.newInstance(); @@ -1220,8 +1200,8 @@ public final class TestUtil { // END FULL-RUNTIME /** - * Register all of {@code TestAllExtensions}'s extensions with the - * given {@link ExtensionRegistry}. + * Register all of {@code TestAllExtensions}'s extensions with the given {@link + * ExtensionRegistry}. */ public static void registerAllExtensions(ExtensionRegistryLite registry) { UnittestProto.registerAllExtensions(registry); @@ -1229,38 +1209,37 @@ public final class TestUtil { } /** - * Set every field of {@code message} to the values expected by - * {@code assertAllExtensionsSet()}. + * Set every field of {@code message} to the values expected by {@code assertAllExtensionsSet()}. */ public static void setAllExtensions(TestAllExtensions.Builder message) { - message.setExtension(optionalInt32Extension , 101); - message.setExtension(optionalInt64Extension , 102L); - message.setExtension(optionalUint32Extension , 103); - message.setExtension(optionalUint64Extension , 104L); - message.setExtension(optionalSint32Extension , 105); - message.setExtension(optionalSint64Extension , 106L); - message.setExtension(optionalFixed32Extension , 107); - message.setExtension(optionalFixed64Extension , 108L); + message.setExtension(optionalInt32Extension, 101); + message.setExtension(optionalInt64Extension, 102L); + message.setExtension(optionalUint32Extension, 103); + message.setExtension(optionalUint64Extension, 104L); + message.setExtension(optionalSint32Extension, 105); + message.setExtension(optionalSint64Extension, 106L); + message.setExtension(optionalFixed32Extension, 107); + message.setExtension(optionalFixed64Extension, 108L); message.setExtension(optionalSfixed32Extension, 109); message.setExtension(optionalSfixed64Extension, 110L); - message.setExtension(optionalFloatExtension , 111F); - message.setExtension(optionalDoubleExtension , 112D); - message.setExtension(optionalBoolExtension , true); - message.setExtension(optionalStringExtension , "115"); - message.setExtension(optionalBytesExtension , toBytes("116")); - - message.setExtension(optionalGroupExtension, - OptionalGroup_extension.newBuilder().setA(117).build()); - message.setExtension(optionalNestedMessageExtension, - TestAllTypes.NestedMessage.newBuilder().setBb(118).build()); - message.setExtension(optionalForeignMessageExtension, - ForeignMessage.newBuilder().setC(119).build()); - message.setExtension(optionalImportMessageExtension, - ImportMessage.newBuilder().setD(120).build()); - message.setExtension(optionalPublicImportMessageExtension, - PublicImportMessage.newBuilder().setE(126).build()); - message.setExtension(optionalLazyMessageExtension, - TestAllTypes.NestedMessage.newBuilder().setBb(127).build()); + message.setExtension(optionalFloatExtension, 111F); + message.setExtension(optionalDoubleExtension, 112D); + message.setExtension(optionalBoolExtension, true); + message.setExtension(optionalStringExtension, "115"); + message.setExtension(optionalBytesExtension, toBytes("116")); + + message.setExtension( + optionalGroupExtension, OptionalGroup_extension.newBuilder().setA(117).build()); + message.setExtension( + optionalNestedMessageExtension, TestAllTypes.NestedMessage.newBuilder().setBb(118).build()); + message.setExtension( + optionalForeignMessageExtension, ForeignMessage.newBuilder().setC(119).build()); + message.setExtension( + optionalImportMessageExtension, ImportMessage.newBuilder().setD(120).build()); + message.setExtension( + optionalPublicImportMessageExtension, PublicImportMessage.newBuilder().setE(126).build()); + message.setExtension( + optionalLazyMessageExtension, TestAllTypes.NestedMessage.newBuilder().setBb(127).build()); message.setExtension(optionalNestedEnumExtension, TestAllTypes.NestedEnum.BAZ); message.setExtension(optionalForeignEnumExtension, ForeignEnum.FOREIGN_BAZ); @@ -1271,32 +1250,32 @@ public final class TestUtil { // ----------------------------------------------------------------- - message.addExtension(repeatedInt32Extension , 201); - message.addExtension(repeatedInt64Extension , 202L); - message.addExtension(repeatedUint32Extension , 203); - message.addExtension(repeatedUint64Extension , 204L); - message.addExtension(repeatedSint32Extension , 205); - message.addExtension(repeatedSint64Extension , 206L); - message.addExtension(repeatedFixed32Extension , 207); - message.addExtension(repeatedFixed64Extension , 208L); + message.addExtension(repeatedInt32Extension, 201); + message.addExtension(repeatedInt64Extension, 202L); + message.addExtension(repeatedUint32Extension, 203); + message.addExtension(repeatedUint64Extension, 204L); + message.addExtension(repeatedSint32Extension, 205); + message.addExtension(repeatedSint64Extension, 206L); + message.addExtension(repeatedFixed32Extension, 207); + message.addExtension(repeatedFixed64Extension, 208L); message.addExtension(repeatedSfixed32Extension, 209); message.addExtension(repeatedSfixed64Extension, 210L); - message.addExtension(repeatedFloatExtension , 211F); - message.addExtension(repeatedDoubleExtension , 212D); - message.addExtension(repeatedBoolExtension , true); - message.addExtension(repeatedStringExtension , "215"); - message.addExtension(repeatedBytesExtension , toBytes("216")); - - message.addExtension(repeatedGroupExtension, - RepeatedGroup_extension.newBuilder().setA(217).build()); - message.addExtension(repeatedNestedMessageExtension, - TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); - message.addExtension(repeatedForeignMessageExtension, - ForeignMessage.newBuilder().setC(219).build()); - message.addExtension(repeatedImportMessageExtension, - ImportMessage.newBuilder().setD(220).build()); - message.addExtension(repeatedLazyMessageExtension, - TestAllTypes.NestedMessage.newBuilder().setBb(227).build()); + message.addExtension(repeatedFloatExtension, 211F); + message.addExtension(repeatedDoubleExtension, 212D); + message.addExtension(repeatedBoolExtension, true); + message.addExtension(repeatedStringExtension, "215"); + message.addExtension(repeatedBytesExtension, toBytes("216")); + + message.addExtension( + repeatedGroupExtension, RepeatedGroup_extension.newBuilder().setA(217).build()); + message.addExtension( + repeatedNestedMessageExtension, TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); + message.addExtension( + repeatedForeignMessageExtension, ForeignMessage.newBuilder().setC(219).build()); + message.addExtension( + repeatedImportMessageExtension, ImportMessage.newBuilder().setD(220).build()); + message.addExtension( + repeatedLazyMessageExtension, TestAllTypes.NestedMessage.newBuilder().setBb(227).build()); message.addExtension(repeatedNestedEnumExtension, TestAllTypes.NestedEnum.BAR); message.addExtension(repeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAR); @@ -1306,32 +1285,32 @@ public final class TestUtil { message.addExtension(repeatedCordExtension, "225"); // Add a second one of each field. - message.addExtension(repeatedInt32Extension , 301); - message.addExtension(repeatedInt64Extension , 302L); - message.addExtension(repeatedUint32Extension , 303); - message.addExtension(repeatedUint64Extension , 304L); - message.addExtension(repeatedSint32Extension , 305); - message.addExtension(repeatedSint64Extension , 306L); - message.addExtension(repeatedFixed32Extension , 307); - message.addExtension(repeatedFixed64Extension , 308L); + message.addExtension(repeatedInt32Extension, 301); + message.addExtension(repeatedInt64Extension, 302L); + message.addExtension(repeatedUint32Extension, 303); + message.addExtension(repeatedUint64Extension, 304L); + message.addExtension(repeatedSint32Extension, 305); + message.addExtension(repeatedSint64Extension, 306L); + message.addExtension(repeatedFixed32Extension, 307); + message.addExtension(repeatedFixed64Extension, 308L); message.addExtension(repeatedSfixed32Extension, 309); message.addExtension(repeatedSfixed64Extension, 310L); - message.addExtension(repeatedFloatExtension , 311F); - message.addExtension(repeatedDoubleExtension , 312D); - message.addExtension(repeatedBoolExtension , false); - message.addExtension(repeatedStringExtension , "315"); - message.addExtension(repeatedBytesExtension , toBytes("316")); - - message.addExtension(repeatedGroupExtension, - RepeatedGroup_extension.newBuilder().setA(317).build()); - message.addExtension(repeatedNestedMessageExtension, - TestAllTypes.NestedMessage.newBuilder().setBb(318).build()); - message.addExtension(repeatedForeignMessageExtension, - ForeignMessage.newBuilder().setC(319).build()); - message.addExtension(repeatedImportMessageExtension, - ImportMessage.newBuilder().setD(320).build()); - message.addExtension(repeatedLazyMessageExtension, - TestAllTypes.NestedMessage.newBuilder().setBb(327).build()); + message.addExtension(repeatedFloatExtension, 311F); + message.addExtension(repeatedDoubleExtension, 312D); + message.addExtension(repeatedBoolExtension, false); + message.addExtension(repeatedStringExtension, "315"); + message.addExtension(repeatedBytesExtension, toBytes("316")); + + message.addExtension( + repeatedGroupExtension, RepeatedGroup_extension.newBuilder().setA(317).build()); + message.addExtension( + repeatedNestedMessageExtension, TestAllTypes.NestedMessage.newBuilder().setBb(318).build()); + message.addExtension( + repeatedForeignMessageExtension, ForeignMessage.newBuilder().setC(319).build()); + message.addExtension( + repeatedImportMessageExtension, ImportMessage.newBuilder().setD(320).build()); + message.addExtension( + repeatedLazyMessageExtension, TestAllTypes.NestedMessage.newBuilder().setBb(327).build()); message.addExtension(repeatedNestedEnumExtension, TestAllTypes.NestedEnum.BAZ); message.addExtension(repeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAZ); @@ -1342,21 +1321,21 @@ public final class TestUtil { // ----------------------------------------------------------------- - message.setExtension(defaultInt32Extension , 401); - message.setExtension(defaultInt64Extension , 402L); - message.setExtension(defaultUint32Extension , 403); - message.setExtension(defaultUint64Extension , 404L); - message.setExtension(defaultSint32Extension , 405); - message.setExtension(defaultSint64Extension , 406L); - message.setExtension(defaultFixed32Extension , 407); - message.setExtension(defaultFixed64Extension , 408L); + message.setExtension(defaultInt32Extension, 401); + message.setExtension(defaultInt64Extension, 402L); + message.setExtension(defaultUint32Extension, 403); + message.setExtension(defaultUint64Extension, 404L); + message.setExtension(defaultSint32Extension, 405); + message.setExtension(defaultSint64Extension, 406L); + message.setExtension(defaultFixed32Extension, 407); + message.setExtension(defaultFixed64Extension, 408L); message.setExtension(defaultSfixed32Extension, 409); message.setExtension(defaultSfixed64Extension, 410L); - message.setExtension(defaultFloatExtension , 411F); - message.setExtension(defaultDoubleExtension , 412D); - message.setExtension(defaultBoolExtension , false); - message.setExtension(defaultStringExtension , "415"); - message.setExtension(defaultBytesExtension , toBytes("416")); + message.setExtension(defaultFloatExtension, 411F); + message.setExtension(defaultDoubleExtension, 412D); + message.setExtension(defaultBoolExtension, false); + message.setExtension(defaultStringExtension, "415"); + message.setExtension(defaultBytesExtension, toBytes("416")); message.setExtension(defaultNestedEnumExtension, TestAllTypes.NestedEnum.FOO); message.setExtension(defaultForeignEnumExtension, ForeignEnum.FOREIGN_FOO); @@ -1366,8 +1345,8 @@ public final class TestUtil { message.setExtension(defaultCordExtension, "425"); message.setExtension(oneofUint32Extension, 601); - message.setExtension(oneofNestedMessageExtension, - TestAllTypes.NestedMessage.newBuilder().setBb(602).build()); + message.setExtension( + oneofNestedMessageExtension, TestAllTypes.NestedMessage.newBuilder().setBb(602).build()); message.setExtension(oneofStringExtension, "603"); message.setExtension(oneofBytesExtension, toBytes("604")); } @@ -1375,41 +1354,44 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Modify the repeated extensions of {@code message} to contain the values - * expected by {@code assertRepeatedExtensionsModified()}. + * Modify the repeated extensions of {@code message} to contain the values expected by {@code + * assertRepeatedExtensionsModified()}. */ - public static void modifyRepeatedExtensions( - TestAllExtensions.Builder message) { - message.setExtension(repeatedInt32Extension , 1, 501); - message.setExtension(repeatedInt64Extension , 1, 502L); - message.setExtension(repeatedUint32Extension , 1, 503); - message.setExtension(repeatedUint64Extension , 1, 504L); - message.setExtension(repeatedSint32Extension , 1, 505); - message.setExtension(repeatedSint64Extension , 1, 506L); - message.setExtension(repeatedFixed32Extension , 1, 507); - message.setExtension(repeatedFixed64Extension , 1, 508L); + public static void modifyRepeatedExtensions(TestAllExtensions.Builder message) { + message.setExtension(repeatedInt32Extension, 1, 501); + message.setExtension(repeatedInt64Extension, 1, 502L); + message.setExtension(repeatedUint32Extension, 1, 503); + message.setExtension(repeatedUint64Extension, 1, 504L); + message.setExtension(repeatedSint32Extension, 1, 505); + message.setExtension(repeatedSint64Extension, 1, 506L); + message.setExtension(repeatedFixed32Extension, 1, 507); + message.setExtension(repeatedFixed64Extension, 1, 508L); message.setExtension(repeatedSfixed32Extension, 1, 509); message.setExtension(repeatedSfixed64Extension, 1, 510L); - message.setExtension(repeatedFloatExtension , 1, 511F); - message.setExtension(repeatedDoubleExtension , 1, 512D); - message.setExtension(repeatedBoolExtension , 1, true); - message.setExtension(repeatedStringExtension , 1, "515"); - message.setExtension(repeatedBytesExtension , 1, toBytes("516")); - - message.setExtension(repeatedGroupExtension, 1, - RepeatedGroup_extension.newBuilder().setA(517).build()); - message.setExtension(repeatedNestedMessageExtension, 1, - TestAllTypes.NestedMessage.newBuilder().setBb(518).build()); - message.setExtension(repeatedForeignMessageExtension, 1, - ForeignMessage.newBuilder().setC(519).build()); - message.setExtension(repeatedImportMessageExtension, 1, - ImportMessage.newBuilder().setD(520).build()); - message.setExtension(repeatedLazyMessageExtension, 1, - TestAllTypes.NestedMessage.newBuilder().setBb(527).build()); - - message.setExtension(repeatedNestedEnumExtension , 1, TestAllTypes.NestedEnum.FOO); + message.setExtension(repeatedFloatExtension, 1, 511F); + message.setExtension(repeatedDoubleExtension, 1, 512D); + message.setExtension(repeatedBoolExtension, 1, true); + message.setExtension(repeatedStringExtension, 1, "515"); + message.setExtension(repeatedBytesExtension, 1, toBytes("516")); + + message.setExtension( + repeatedGroupExtension, 1, RepeatedGroup_extension.newBuilder().setA(517).build()); + message.setExtension( + repeatedNestedMessageExtension, + 1, + TestAllTypes.NestedMessage.newBuilder().setBb(518).build()); + message.setExtension( + repeatedForeignMessageExtension, 1, ForeignMessage.newBuilder().setC(519).build()); + message.setExtension( + repeatedImportMessageExtension, 1, ImportMessage.newBuilder().setD(520).build()); + message.setExtension( + repeatedLazyMessageExtension, + 1, + TestAllTypes.NestedMessage.newBuilder().setBb(527).build()); + + message.setExtension(repeatedNestedEnumExtension, 1, TestAllTypes.NestedEnum.FOO); message.setExtension(repeatedForeignEnumExtension, 1, ForeignEnum.FOREIGN_FOO); - message.setExtension(repeatedImportEnumExtension , 1, ImportEnum.IMPORT_FOO); + message.setExtension(repeatedImportEnumExtension, 1, ImportEnum.IMPORT_FOO); message.setExtension(repeatedStringPieceExtension, 1, "524"); message.setExtension(repeatedCordExtension, 1, "525"); @@ -1418,218 +1400,215 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all extensions of - * {@code message} are set to the values assigned by {@code setAllExtensions}. + * Assert (using {@code junit.framework.Assert}} that all extensions of {@code message} are set to + * the values assigned by {@code setAllExtensions}. */ - public static void assertAllExtensionsSet( - TestAllExtensionsOrBuilder message) { - Assert.assertTrue(message.hasExtension(optionalInt32Extension )); - Assert.assertTrue(message.hasExtension(optionalInt64Extension )); - Assert.assertTrue(message.hasExtension(optionalUint32Extension )); - Assert.assertTrue(message.hasExtension(optionalUint64Extension )); - Assert.assertTrue(message.hasExtension(optionalSint32Extension )); - Assert.assertTrue(message.hasExtension(optionalSint64Extension )); - Assert.assertTrue(message.hasExtension(optionalFixed32Extension )); - Assert.assertTrue(message.hasExtension(optionalFixed64Extension )); + public static void assertAllExtensionsSet(TestAllExtensionsOrBuilder message) { + Assert.assertTrue(message.hasExtension(optionalInt32Extension)); + Assert.assertTrue(message.hasExtension(optionalInt64Extension)); + Assert.assertTrue(message.hasExtension(optionalUint32Extension)); + Assert.assertTrue(message.hasExtension(optionalUint64Extension)); + Assert.assertTrue(message.hasExtension(optionalSint32Extension)); + Assert.assertTrue(message.hasExtension(optionalSint64Extension)); + Assert.assertTrue(message.hasExtension(optionalFixed32Extension)); + Assert.assertTrue(message.hasExtension(optionalFixed64Extension)); Assert.assertTrue(message.hasExtension(optionalSfixed32Extension)); Assert.assertTrue(message.hasExtension(optionalSfixed64Extension)); - Assert.assertTrue(message.hasExtension(optionalFloatExtension )); - Assert.assertTrue(message.hasExtension(optionalDoubleExtension )); - Assert.assertTrue(message.hasExtension(optionalBoolExtension )); - Assert.assertTrue(message.hasExtension(optionalStringExtension )); - Assert.assertTrue(message.hasExtension(optionalBytesExtension )); - - Assert.assertTrue(message.hasExtension(optionalGroupExtension )); - Assert.assertTrue(message.hasExtension(optionalNestedMessageExtension )); + Assert.assertTrue(message.hasExtension(optionalFloatExtension)); + Assert.assertTrue(message.hasExtension(optionalDoubleExtension)); + Assert.assertTrue(message.hasExtension(optionalBoolExtension)); + Assert.assertTrue(message.hasExtension(optionalStringExtension)); + Assert.assertTrue(message.hasExtension(optionalBytesExtension)); + + Assert.assertTrue(message.hasExtension(optionalGroupExtension)); + Assert.assertTrue(message.hasExtension(optionalNestedMessageExtension)); Assert.assertTrue(message.hasExtension(optionalForeignMessageExtension)); - Assert.assertTrue(message.hasExtension(optionalImportMessageExtension )); + Assert.assertTrue(message.hasExtension(optionalImportMessageExtension)); - Assert.assertTrue(message.getExtension(optionalGroupExtension ).hasA()); - Assert.assertTrue(message.getExtension(optionalNestedMessageExtension ).hasBb()); + Assert.assertTrue(message.getExtension(optionalGroupExtension).hasA()); + Assert.assertTrue(message.getExtension(optionalNestedMessageExtension).hasBb()); Assert.assertTrue(message.getExtension(optionalForeignMessageExtension).hasC()); - Assert.assertTrue(message.getExtension(optionalImportMessageExtension ).hasD()); + Assert.assertTrue(message.getExtension(optionalImportMessageExtension).hasD()); - Assert.assertTrue(message.hasExtension(optionalNestedEnumExtension )); + Assert.assertTrue(message.hasExtension(optionalNestedEnumExtension)); Assert.assertTrue(message.hasExtension(optionalForeignEnumExtension)); - Assert.assertTrue(message.hasExtension(optionalImportEnumExtension )); + Assert.assertTrue(message.hasExtension(optionalImportEnumExtension)); Assert.assertTrue(message.hasExtension(optionalStringPieceExtension)); Assert.assertTrue(message.hasExtension(optionalCordExtension)); - assertEqualsExactType(101 , message.getExtension(optionalInt32Extension )); - assertEqualsExactType(102L , message.getExtension(optionalInt64Extension )); - assertEqualsExactType(103 , message.getExtension(optionalUint32Extension )); - assertEqualsExactType(104L , message.getExtension(optionalUint64Extension )); - assertEqualsExactType(105 , message.getExtension(optionalSint32Extension )); - assertEqualsExactType(106L , message.getExtension(optionalSint64Extension )); - assertEqualsExactType(107 , message.getExtension(optionalFixed32Extension )); - assertEqualsExactType(108L , message.getExtension(optionalFixed64Extension )); - assertEqualsExactType(109 , message.getExtension(optionalSfixed32Extension)); - assertEqualsExactType(110L , message.getExtension(optionalSfixed64Extension)); - assertEqualsExactType(111F , message.getExtension(optionalFloatExtension )); - assertEqualsExactType(112D , message.getExtension(optionalDoubleExtension )); - assertEqualsExactType(true , message.getExtension(optionalBoolExtension )); - assertEqualsExactType("115", message.getExtension(optionalStringExtension )); + assertEqualsExactType(101, message.getExtension(optionalInt32Extension)); + assertEqualsExactType(102L, message.getExtension(optionalInt64Extension)); + assertEqualsExactType(103, message.getExtension(optionalUint32Extension)); + assertEqualsExactType(104L, message.getExtension(optionalUint64Extension)); + assertEqualsExactType(105, message.getExtension(optionalSint32Extension)); + assertEqualsExactType(106L, message.getExtension(optionalSint64Extension)); + assertEqualsExactType(107, message.getExtension(optionalFixed32Extension)); + assertEqualsExactType(108L, message.getExtension(optionalFixed64Extension)); + assertEqualsExactType(109, message.getExtension(optionalSfixed32Extension)); + assertEqualsExactType(110L, message.getExtension(optionalSfixed64Extension)); + assertEqualsExactType(111F, message.getExtension(optionalFloatExtension)); + assertEqualsExactType(112D, message.getExtension(optionalDoubleExtension)); + assertEqualsExactType(true, message.getExtension(optionalBoolExtension)); + assertEqualsExactType("115", message.getExtension(optionalStringExtension)); assertEqualsExactType(toBytes("116"), message.getExtension(optionalBytesExtension)); - assertEqualsExactType(117, message.getExtension(optionalGroupExtension ).getA()); - assertEqualsExactType(118, message.getExtension(optionalNestedMessageExtension ).getBb()); - assertEqualsExactType(119, message.getExtension(optionalForeignMessageExtension ).getC()); - assertEqualsExactType(120, message.getExtension(optionalImportMessageExtension ).getD()); + assertEqualsExactType(117, message.getExtension(optionalGroupExtension).getA()); + assertEqualsExactType(118, message.getExtension(optionalNestedMessageExtension).getBb()); + assertEqualsExactType(119, message.getExtension(optionalForeignMessageExtension).getC()); + assertEqualsExactType(120, message.getExtension(optionalImportMessageExtension).getD()); assertEqualsExactType(126, message.getExtension(optionalPublicImportMessageExtension).getE()); - assertEqualsExactType(127, message.getExtension(optionalLazyMessageExtension ).getBb()); + assertEqualsExactType(127, message.getExtension(optionalLazyMessageExtension).getBb()); - assertEqualsExactType(TestAllTypes.NestedEnum.BAZ, - message.getExtension(optionalNestedEnumExtension)); - assertEqualsExactType(ForeignEnum.FOREIGN_BAZ, - message.getExtension(optionalForeignEnumExtension)); - assertEqualsExactType(ImportEnum.IMPORT_BAZ, - message.getExtension(optionalImportEnumExtension)); + assertEqualsExactType( + TestAllTypes.NestedEnum.BAZ, message.getExtension(optionalNestedEnumExtension)); + assertEqualsExactType( + ForeignEnum.FOREIGN_BAZ, message.getExtension(optionalForeignEnumExtension)); + assertEqualsExactType(ImportEnum.IMPORT_BAZ, message.getExtension(optionalImportEnumExtension)); assertEqualsExactType("124", message.getExtension(optionalStringPieceExtension)); assertEqualsExactType("125", message.getExtension(optionalCordExtension)); // ----------------------------------------------------------------- - Assert.assertEquals(2, message.getExtensionCount(repeatedInt32Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedInt64Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedUint32Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedUint64Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedSint32Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedSint64Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64Extension )); + Assert.assertEquals(2, message.getExtensionCount(repeatedInt32Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedInt64Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedUint32Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedUint64Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedSint32Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedSint64Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64Extension)); Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed32Extension)); Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed64Extension)); - Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtension )); - - Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtension )); + Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtension)); + + Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtension)); Assert.assertEquals(2, message.getExtensionCount(repeatedForeignMessageExtension)); - Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedLazyMessageExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtension )); + Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedLazyMessageExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtension)); Assert.assertEquals(2, message.getExtensionCount(repeatedStringPieceExtension)); Assert.assertEquals(2, message.getExtensionCount(repeatedCordExtension)); - assertEqualsExactType(201 , message.getExtension(repeatedInt32Extension , 0)); - assertEqualsExactType(202L , message.getExtension(repeatedInt64Extension , 0)); - assertEqualsExactType(203 , message.getExtension(repeatedUint32Extension , 0)); - assertEqualsExactType(204L , message.getExtension(repeatedUint64Extension , 0)); - assertEqualsExactType(205 , message.getExtension(repeatedSint32Extension , 0)); - assertEqualsExactType(206L , message.getExtension(repeatedSint64Extension , 0)); - assertEqualsExactType(207 , message.getExtension(repeatedFixed32Extension , 0)); - assertEqualsExactType(208L , message.getExtension(repeatedFixed64Extension , 0)); - assertEqualsExactType(209 , message.getExtension(repeatedSfixed32Extension, 0)); - assertEqualsExactType(210L , message.getExtension(repeatedSfixed64Extension, 0)); - assertEqualsExactType(211F , message.getExtension(repeatedFloatExtension , 0)); - assertEqualsExactType(212D , message.getExtension(repeatedDoubleExtension , 0)); - assertEqualsExactType(true , message.getExtension(repeatedBoolExtension , 0)); - assertEqualsExactType("215", message.getExtension(repeatedStringExtension , 0)); + assertEqualsExactType(201, message.getExtension(repeatedInt32Extension, 0)); + assertEqualsExactType(202L, message.getExtension(repeatedInt64Extension, 0)); + assertEqualsExactType(203, message.getExtension(repeatedUint32Extension, 0)); + assertEqualsExactType(204L, message.getExtension(repeatedUint64Extension, 0)); + assertEqualsExactType(205, message.getExtension(repeatedSint32Extension, 0)); + assertEqualsExactType(206L, message.getExtension(repeatedSint64Extension, 0)); + assertEqualsExactType(207, message.getExtension(repeatedFixed32Extension, 0)); + assertEqualsExactType(208L, message.getExtension(repeatedFixed64Extension, 0)); + assertEqualsExactType(209, message.getExtension(repeatedSfixed32Extension, 0)); + assertEqualsExactType(210L, message.getExtension(repeatedSfixed64Extension, 0)); + assertEqualsExactType(211F, message.getExtension(repeatedFloatExtension, 0)); + assertEqualsExactType(212D, message.getExtension(repeatedDoubleExtension, 0)); + assertEqualsExactType(true, message.getExtension(repeatedBoolExtension, 0)); + assertEqualsExactType("215", message.getExtension(repeatedStringExtension, 0)); assertEqualsExactType(toBytes("216"), message.getExtension(repeatedBytesExtension, 0)); - assertEqualsExactType(217, message.getExtension(repeatedGroupExtension , 0).getA()); - assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtension , 0).getBb()); + assertEqualsExactType(217, message.getExtension(repeatedGroupExtension, 0).getA()); + assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtension, 0).getBb()); assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtension, 0).getC()); - assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtension , 0).getD()); - assertEqualsExactType(227, message.getExtension(repeatedLazyMessageExtension , 0).getBb()); + assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtension, 0).getD()); + assertEqualsExactType(227, message.getExtension(repeatedLazyMessageExtension, 0).getBb()); - assertEqualsExactType(TestAllTypes.NestedEnum.BAR, - message.getExtension(repeatedNestedEnumExtension, 0)); - assertEqualsExactType(ForeignEnum.FOREIGN_BAR, - message.getExtension(repeatedForeignEnumExtension, 0)); - assertEqualsExactType(ImportEnum.IMPORT_BAR, - message.getExtension(repeatedImportEnumExtension, 0)); + assertEqualsExactType( + TestAllTypes.NestedEnum.BAR, message.getExtension(repeatedNestedEnumExtension, 0)); + assertEqualsExactType( + ForeignEnum.FOREIGN_BAR, message.getExtension(repeatedForeignEnumExtension, 0)); + assertEqualsExactType( + ImportEnum.IMPORT_BAR, message.getExtension(repeatedImportEnumExtension, 0)); assertEqualsExactType("224", message.getExtension(repeatedStringPieceExtension, 0)); assertEqualsExactType("225", message.getExtension(repeatedCordExtension, 0)); - assertEqualsExactType(301 , message.getExtension(repeatedInt32Extension , 1)); - assertEqualsExactType(302L , message.getExtension(repeatedInt64Extension , 1)); - assertEqualsExactType(303 , message.getExtension(repeatedUint32Extension , 1)); - assertEqualsExactType(304L , message.getExtension(repeatedUint64Extension , 1)); - assertEqualsExactType(305 , message.getExtension(repeatedSint32Extension , 1)); - assertEqualsExactType(306L , message.getExtension(repeatedSint64Extension , 1)); - assertEqualsExactType(307 , message.getExtension(repeatedFixed32Extension , 1)); - assertEqualsExactType(308L , message.getExtension(repeatedFixed64Extension , 1)); - assertEqualsExactType(309 , message.getExtension(repeatedSfixed32Extension, 1)); - assertEqualsExactType(310L , message.getExtension(repeatedSfixed64Extension, 1)); - assertEqualsExactType(311F , message.getExtension(repeatedFloatExtension , 1)); - assertEqualsExactType(312D , message.getExtension(repeatedDoubleExtension , 1)); - assertEqualsExactType(false, message.getExtension(repeatedBoolExtension , 1)); - assertEqualsExactType("315", message.getExtension(repeatedStringExtension , 1)); + assertEqualsExactType(301, message.getExtension(repeatedInt32Extension, 1)); + assertEqualsExactType(302L, message.getExtension(repeatedInt64Extension, 1)); + assertEqualsExactType(303, message.getExtension(repeatedUint32Extension, 1)); + assertEqualsExactType(304L, message.getExtension(repeatedUint64Extension, 1)); + assertEqualsExactType(305, message.getExtension(repeatedSint32Extension, 1)); + assertEqualsExactType(306L, message.getExtension(repeatedSint64Extension, 1)); + assertEqualsExactType(307, message.getExtension(repeatedFixed32Extension, 1)); + assertEqualsExactType(308L, message.getExtension(repeatedFixed64Extension, 1)); + assertEqualsExactType(309, message.getExtension(repeatedSfixed32Extension, 1)); + assertEqualsExactType(310L, message.getExtension(repeatedSfixed64Extension, 1)); + assertEqualsExactType(311F, message.getExtension(repeatedFloatExtension, 1)); + assertEqualsExactType(312D, message.getExtension(repeatedDoubleExtension, 1)); + assertEqualsExactType(false, message.getExtension(repeatedBoolExtension, 1)); + assertEqualsExactType("315", message.getExtension(repeatedStringExtension, 1)); assertEqualsExactType(toBytes("316"), message.getExtension(repeatedBytesExtension, 1)); - assertEqualsExactType(317, message.getExtension(repeatedGroupExtension , 1).getA()); - assertEqualsExactType(318, message.getExtension(repeatedNestedMessageExtension , 1).getBb()); + assertEqualsExactType(317, message.getExtension(repeatedGroupExtension, 1).getA()); + assertEqualsExactType(318, message.getExtension(repeatedNestedMessageExtension, 1).getBb()); assertEqualsExactType(319, message.getExtension(repeatedForeignMessageExtension, 1).getC()); - assertEqualsExactType(320, message.getExtension(repeatedImportMessageExtension , 1).getD()); - assertEqualsExactType(327, message.getExtension(repeatedLazyMessageExtension , 1).getBb()); + assertEqualsExactType(320, message.getExtension(repeatedImportMessageExtension, 1).getD()); + assertEqualsExactType(327, message.getExtension(repeatedLazyMessageExtension, 1).getBb()); - assertEqualsExactType(TestAllTypes.NestedEnum.BAZ, - message.getExtension(repeatedNestedEnumExtension, 1)); - assertEqualsExactType(ForeignEnum.FOREIGN_BAZ, - message.getExtension(repeatedForeignEnumExtension, 1)); - assertEqualsExactType(ImportEnum.IMPORT_BAZ, - message.getExtension(repeatedImportEnumExtension, 1)); + assertEqualsExactType( + TestAllTypes.NestedEnum.BAZ, message.getExtension(repeatedNestedEnumExtension, 1)); + assertEqualsExactType( + ForeignEnum.FOREIGN_BAZ, message.getExtension(repeatedForeignEnumExtension, 1)); + assertEqualsExactType( + ImportEnum.IMPORT_BAZ, message.getExtension(repeatedImportEnumExtension, 1)); assertEqualsExactType("324", message.getExtension(repeatedStringPieceExtension, 1)); assertEqualsExactType("325", message.getExtension(repeatedCordExtension, 1)); // ----------------------------------------------------------------- - Assert.assertTrue(message.hasExtension(defaultInt32Extension )); - Assert.assertTrue(message.hasExtension(defaultInt64Extension )); - Assert.assertTrue(message.hasExtension(defaultUint32Extension )); - Assert.assertTrue(message.hasExtension(defaultUint64Extension )); - Assert.assertTrue(message.hasExtension(defaultSint32Extension )); - Assert.assertTrue(message.hasExtension(defaultSint64Extension )); - Assert.assertTrue(message.hasExtension(defaultFixed32Extension )); - Assert.assertTrue(message.hasExtension(defaultFixed64Extension )); + Assert.assertTrue(message.hasExtension(defaultInt32Extension)); + Assert.assertTrue(message.hasExtension(defaultInt64Extension)); + Assert.assertTrue(message.hasExtension(defaultUint32Extension)); + Assert.assertTrue(message.hasExtension(defaultUint64Extension)); + Assert.assertTrue(message.hasExtension(defaultSint32Extension)); + Assert.assertTrue(message.hasExtension(defaultSint64Extension)); + Assert.assertTrue(message.hasExtension(defaultFixed32Extension)); + Assert.assertTrue(message.hasExtension(defaultFixed64Extension)); Assert.assertTrue(message.hasExtension(defaultSfixed32Extension)); Assert.assertTrue(message.hasExtension(defaultSfixed64Extension)); - Assert.assertTrue(message.hasExtension(defaultFloatExtension )); - Assert.assertTrue(message.hasExtension(defaultDoubleExtension )); - Assert.assertTrue(message.hasExtension(defaultBoolExtension )); - Assert.assertTrue(message.hasExtension(defaultStringExtension )); - Assert.assertTrue(message.hasExtension(defaultBytesExtension )); + Assert.assertTrue(message.hasExtension(defaultFloatExtension)); + Assert.assertTrue(message.hasExtension(defaultDoubleExtension)); + Assert.assertTrue(message.hasExtension(defaultBoolExtension)); + Assert.assertTrue(message.hasExtension(defaultStringExtension)); + Assert.assertTrue(message.hasExtension(defaultBytesExtension)); - Assert.assertTrue(message.hasExtension(defaultNestedEnumExtension )); + Assert.assertTrue(message.hasExtension(defaultNestedEnumExtension)); Assert.assertTrue(message.hasExtension(defaultForeignEnumExtension)); - Assert.assertTrue(message.hasExtension(defaultImportEnumExtension )); + Assert.assertTrue(message.hasExtension(defaultImportEnumExtension)); Assert.assertTrue(message.hasExtension(defaultStringPieceExtension)); Assert.assertTrue(message.hasExtension(defaultCordExtension)); - assertEqualsExactType(401 , message.getExtension(defaultInt32Extension )); - assertEqualsExactType(402L , message.getExtension(defaultInt64Extension )); - assertEqualsExactType(403 , message.getExtension(defaultUint32Extension )); - assertEqualsExactType(404L , message.getExtension(defaultUint64Extension )); - assertEqualsExactType(405 , message.getExtension(defaultSint32Extension )); - assertEqualsExactType(406L , message.getExtension(defaultSint64Extension )); - assertEqualsExactType(407 , message.getExtension(defaultFixed32Extension )); - assertEqualsExactType(408L , message.getExtension(defaultFixed64Extension )); - assertEqualsExactType(409 , message.getExtension(defaultSfixed32Extension)); - assertEqualsExactType(410L , message.getExtension(defaultSfixed64Extension)); - assertEqualsExactType(411F , message.getExtension(defaultFloatExtension )); - assertEqualsExactType(412D , message.getExtension(defaultDoubleExtension )); - assertEqualsExactType(false, message.getExtension(defaultBoolExtension )); - assertEqualsExactType("415", message.getExtension(defaultStringExtension )); + assertEqualsExactType(401, message.getExtension(defaultInt32Extension)); + assertEqualsExactType(402L, message.getExtension(defaultInt64Extension)); + assertEqualsExactType(403, message.getExtension(defaultUint32Extension)); + assertEqualsExactType(404L, message.getExtension(defaultUint64Extension)); + assertEqualsExactType(405, message.getExtension(defaultSint32Extension)); + assertEqualsExactType(406L, message.getExtension(defaultSint64Extension)); + assertEqualsExactType(407, message.getExtension(defaultFixed32Extension)); + assertEqualsExactType(408L, message.getExtension(defaultFixed64Extension)); + assertEqualsExactType(409, message.getExtension(defaultSfixed32Extension)); + assertEqualsExactType(410L, message.getExtension(defaultSfixed64Extension)); + assertEqualsExactType(411F, message.getExtension(defaultFloatExtension)); + assertEqualsExactType(412D, message.getExtension(defaultDoubleExtension)); + assertEqualsExactType(false, message.getExtension(defaultBoolExtension)); + assertEqualsExactType("415", message.getExtension(defaultStringExtension)); assertEqualsExactType(toBytes("416"), message.getExtension(defaultBytesExtension)); - assertEqualsExactType(TestAllTypes.NestedEnum.FOO, - message.getExtension(defaultNestedEnumExtension )); - assertEqualsExactType(ForeignEnum.FOREIGN_FOO, - message.getExtension(defaultForeignEnumExtension)); - assertEqualsExactType(ImportEnum.IMPORT_FOO, - message.getExtension(defaultImportEnumExtension)); + assertEqualsExactType( + TestAllTypes.NestedEnum.FOO, message.getExtension(defaultNestedEnumExtension)); + assertEqualsExactType( + ForeignEnum.FOREIGN_FOO, message.getExtension(defaultForeignEnumExtension)); + assertEqualsExactType(ImportEnum.IMPORT_FOO, message.getExtension(defaultImportEnumExtension)); assertEqualsExactType("424", message.getExtension(defaultStringPieceExtension)); assertEqualsExactType("425", message.getExtension(defaultCordExtension)); @@ -1642,184 +1621,181 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all extensions of - * {@code message} are cleared, and that getting the extensions returns their - * default values. + * Assert (using {@code junit.framework.Assert}} that all extensions of {@code message} are + * cleared, and that getting the extensions returns their default values. */ public static void assertExtensionsClear(TestAllExtensionsOrBuilder message) { // hasBlah() should initially be false for all optional fields. - Assert.assertFalse(message.hasExtension(optionalInt32Extension )); - Assert.assertFalse(message.hasExtension(optionalInt64Extension )); - Assert.assertFalse(message.hasExtension(optionalUint32Extension )); - Assert.assertFalse(message.hasExtension(optionalUint64Extension )); - Assert.assertFalse(message.hasExtension(optionalSint32Extension )); - Assert.assertFalse(message.hasExtension(optionalSint64Extension )); - Assert.assertFalse(message.hasExtension(optionalFixed32Extension )); - Assert.assertFalse(message.hasExtension(optionalFixed64Extension )); + Assert.assertFalse(message.hasExtension(optionalInt32Extension)); + Assert.assertFalse(message.hasExtension(optionalInt64Extension)); + Assert.assertFalse(message.hasExtension(optionalUint32Extension)); + Assert.assertFalse(message.hasExtension(optionalUint64Extension)); + Assert.assertFalse(message.hasExtension(optionalSint32Extension)); + Assert.assertFalse(message.hasExtension(optionalSint64Extension)); + Assert.assertFalse(message.hasExtension(optionalFixed32Extension)); + Assert.assertFalse(message.hasExtension(optionalFixed64Extension)); Assert.assertFalse(message.hasExtension(optionalSfixed32Extension)); Assert.assertFalse(message.hasExtension(optionalSfixed64Extension)); - Assert.assertFalse(message.hasExtension(optionalFloatExtension )); - Assert.assertFalse(message.hasExtension(optionalDoubleExtension )); - Assert.assertFalse(message.hasExtension(optionalBoolExtension )); - Assert.assertFalse(message.hasExtension(optionalStringExtension )); - Assert.assertFalse(message.hasExtension(optionalBytesExtension )); - - Assert.assertFalse(message.hasExtension(optionalGroupExtension )); - Assert.assertFalse(message.hasExtension(optionalNestedMessageExtension )); + Assert.assertFalse(message.hasExtension(optionalFloatExtension)); + Assert.assertFalse(message.hasExtension(optionalDoubleExtension)); + Assert.assertFalse(message.hasExtension(optionalBoolExtension)); + Assert.assertFalse(message.hasExtension(optionalStringExtension)); + Assert.assertFalse(message.hasExtension(optionalBytesExtension)); + + Assert.assertFalse(message.hasExtension(optionalGroupExtension)); + Assert.assertFalse(message.hasExtension(optionalNestedMessageExtension)); Assert.assertFalse(message.hasExtension(optionalForeignMessageExtension)); - Assert.assertFalse(message.hasExtension(optionalImportMessageExtension )); + Assert.assertFalse(message.hasExtension(optionalImportMessageExtension)); - Assert.assertFalse(message.hasExtension(optionalNestedEnumExtension )); + Assert.assertFalse(message.hasExtension(optionalNestedEnumExtension)); Assert.assertFalse(message.hasExtension(optionalForeignEnumExtension)); - Assert.assertFalse(message.hasExtension(optionalImportEnumExtension )); + Assert.assertFalse(message.hasExtension(optionalImportEnumExtension)); Assert.assertFalse(message.hasExtension(optionalStringPieceExtension)); Assert.assertFalse(message.hasExtension(optionalCordExtension)); // Optional fields without defaults are set to zero or something like it. - assertEqualsExactType(0 , message.getExtension(optionalInt32Extension )); - assertEqualsExactType(0L , message.getExtension(optionalInt64Extension )); - assertEqualsExactType(0 , message.getExtension(optionalUint32Extension )); - assertEqualsExactType(0L , message.getExtension(optionalUint64Extension )); - assertEqualsExactType(0 , message.getExtension(optionalSint32Extension )); - assertEqualsExactType(0L , message.getExtension(optionalSint64Extension )); - assertEqualsExactType(0 , message.getExtension(optionalFixed32Extension )); - assertEqualsExactType(0L , message.getExtension(optionalFixed64Extension )); - assertEqualsExactType(0 , message.getExtension(optionalSfixed32Extension)); - assertEqualsExactType(0L , message.getExtension(optionalSfixed64Extension)); - assertEqualsExactType(0F , message.getExtension(optionalFloatExtension )); - assertEqualsExactType(0D , message.getExtension(optionalDoubleExtension )); - assertEqualsExactType(false, message.getExtension(optionalBoolExtension )); - assertEqualsExactType("" , message.getExtension(optionalStringExtension )); + assertEqualsExactType(0, message.getExtension(optionalInt32Extension)); + assertEqualsExactType(0L, message.getExtension(optionalInt64Extension)); + assertEqualsExactType(0, message.getExtension(optionalUint32Extension)); + assertEqualsExactType(0L, message.getExtension(optionalUint64Extension)); + assertEqualsExactType(0, message.getExtension(optionalSint32Extension)); + assertEqualsExactType(0L, message.getExtension(optionalSint64Extension)); + assertEqualsExactType(0, message.getExtension(optionalFixed32Extension)); + assertEqualsExactType(0L, message.getExtension(optionalFixed64Extension)); + assertEqualsExactType(0, message.getExtension(optionalSfixed32Extension)); + assertEqualsExactType(0L, message.getExtension(optionalSfixed64Extension)); + assertEqualsExactType(0F, message.getExtension(optionalFloatExtension)); + assertEqualsExactType(0D, message.getExtension(optionalDoubleExtension)); + assertEqualsExactType(false, message.getExtension(optionalBoolExtension)); + assertEqualsExactType("", message.getExtension(optionalStringExtension)); assertEqualsExactType(ByteString.EMPTY, message.getExtension(optionalBytesExtension)); // Embedded messages should also be clear. - Assert.assertFalse(message.getExtension(optionalGroupExtension ).hasA()); - Assert.assertFalse(message.getExtension(optionalNestedMessageExtension ).hasBb()); + Assert.assertFalse(message.getExtension(optionalGroupExtension).hasA()); + Assert.assertFalse(message.getExtension(optionalNestedMessageExtension).hasBb()); Assert.assertFalse(message.getExtension(optionalForeignMessageExtension).hasC()); - Assert.assertFalse(message.getExtension(optionalImportMessageExtension ).hasD()); + Assert.assertFalse(message.getExtension(optionalImportMessageExtension).hasD()); - assertEqualsExactType(0, message.getExtension(optionalGroupExtension ).getA()); - assertEqualsExactType(0, message.getExtension(optionalNestedMessageExtension ).getBb()); + assertEqualsExactType(0, message.getExtension(optionalGroupExtension).getA()); + assertEqualsExactType(0, message.getExtension(optionalNestedMessageExtension).getBb()); assertEqualsExactType(0, message.getExtension(optionalForeignMessageExtension).getC()); - assertEqualsExactType(0, message.getExtension(optionalImportMessageExtension ).getD()); + assertEqualsExactType(0, message.getExtension(optionalImportMessageExtension).getD()); // Enums without defaults are set to the first value in the enum. - assertEqualsExactType(TestAllTypes.NestedEnum.FOO, - message.getExtension(optionalNestedEnumExtension )); - assertEqualsExactType(ForeignEnum.FOREIGN_FOO, - message.getExtension(optionalForeignEnumExtension)); - assertEqualsExactType(ImportEnum.IMPORT_FOO, - message.getExtension(optionalImportEnumExtension)); + assertEqualsExactType( + TestAllTypes.NestedEnum.FOO, message.getExtension(optionalNestedEnumExtension)); + assertEqualsExactType( + ForeignEnum.FOREIGN_FOO, message.getExtension(optionalForeignEnumExtension)); + assertEqualsExactType(ImportEnum.IMPORT_FOO, message.getExtension(optionalImportEnumExtension)); assertEqualsExactType("", message.getExtension(optionalStringPieceExtension)); assertEqualsExactType("", message.getExtension(optionalCordExtension)); // Repeated fields are empty. - Assert.assertEquals(0, message.getExtensionCount(repeatedInt32Extension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedInt64Extension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedUint32Extension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedUint64Extension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedSint32Extension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedSint64Extension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedFixed32Extension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedFixed64Extension )); + Assert.assertEquals(0, message.getExtensionCount(repeatedInt32Extension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedInt64Extension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedUint32Extension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedUint64Extension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedSint32Extension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedSint64Extension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedFixed32Extension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedFixed64Extension)); Assert.assertEquals(0, message.getExtensionCount(repeatedSfixed32Extension)); Assert.assertEquals(0, message.getExtensionCount(repeatedSfixed64Extension)); - Assert.assertEquals(0, message.getExtensionCount(repeatedFloatExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedDoubleExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedBoolExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedStringExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedBytesExtension )); - - Assert.assertEquals(0, message.getExtensionCount(repeatedGroupExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedNestedMessageExtension )); + Assert.assertEquals(0, message.getExtensionCount(repeatedFloatExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedDoubleExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedBoolExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedStringExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedBytesExtension)); + + Assert.assertEquals(0, message.getExtensionCount(repeatedGroupExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedNestedMessageExtension)); Assert.assertEquals(0, message.getExtensionCount(repeatedForeignMessageExtension)); - Assert.assertEquals(0, message.getExtensionCount(repeatedImportMessageExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedLazyMessageExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedNestedEnumExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedForeignEnumExtension )); - Assert.assertEquals(0, message.getExtensionCount(repeatedImportEnumExtension )); + Assert.assertEquals(0, message.getExtensionCount(repeatedImportMessageExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedLazyMessageExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedNestedEnumExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedForeignEnumExtension)); + Assert.assertEquals(0, message.getExtensionCount(repeatedImportEnumExtension)); Assert.assertEquals(0, message.getExtensionCount(repeatedStringPieceExtension)); Assert.assertEquals(0, message.getExtensionCount(repeatedCordExtension)); // Repeated fields are empty via getExtension().size(). - Assert.assertEquals(0, message.getExtension(repeatedInt32Extension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedInt64Extension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedUint32Extension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedUint64Extension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedSint32Extension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedSint64Extension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedFixed32Extension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedFixed64Extension ).size()); + Assert.assertEquals(0, message.getExtension(repeatedInt32Extension).size()); + Assert.assertEquals(0, message.getExtension(repeatedInt64Extension).size()); + Assert.assertEquals(0, message.getExtension(repeatedUint32Extension).size()); + Assert.assertEquals(0, message.getExtension(repeatedUint64Extension).size()); + Assert.assertEquals(0, message.getExtension(repeatedSint32Extension).size()); + Assert.assertEquals(0, message.getExtension(repeatedSint64Extension).size()); + Assert.assertEquals(0, message.getExtension(repeatedFixed32Extension).size()); + Assert.assertEquals(0, message.getExtension(repeatedFixed64Extension).size()); Assert.assertEquals(0, message.getExtension(repeatedSfixed32Extension).size()); Assert.assertEquals(0, message.getExtension(repeatedSfixed64Extension).size()); - Assert.assertEquals(0, message.getExtension(repeatedFloatExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedDoubleExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedBoolExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedStringExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedBytesExtension ).size()); - - Assert.assertEquals(0, message.getExtension(repeatedGroupExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedNestedMessageExtension ).size()); + Assert.assertEquals(0, message.getExtension(repeatedFloatExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedDoubleExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedBoolExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedStringExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedBytesExtension).size()); + + Assert.assertEquals(0, message.getExtension(repeatedGroupExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedNestedMessageExtension).size()); Assert.assertEquals(0, message.getExtension(repeatedForeignMessageExtension).size()); - Assert.assertEquals(0, message.getExtension(repeatedImportMessageExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedLazyMessageExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedNestedEnumExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedForeignEnumExtension ).size()); - Assert.assertEquals(0, message.getExtension(repeatedImportEnumExtension ).size()); + Assert.assertEquals(0, message.getExtension(repeatedImportMessageExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedLazyMessageExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedNestedEnumExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedForeignEnumExtension).size()); + Assert.assertEquals(0, message.getExtension(repeatedImportEnumExtension).size()); Assert.assertEquals(0, message.getExtension(repeatedStringPieceExtension).size()); Assert.assertEquals(0, message.getExtension(repeatedCordExtension).size()); // hasBlah() should also be false for all default fields. - Assert.assertFalse(message.hasExtension(defaultInt32Extension )); - Assert.assertFalse(message.hasExtension(defaultInt64Extension )); - Assert.assertFalse(message.hasExtension(defaultUint32Extension )); - Assert.assertFalse(message.hasExtension(defaultUint64Extension )); - Assert.assertFalse(message.hasExtension(defaultSint32Extension )); - Assert.assertFalse(message.hasExtension(defaultSint64Extension )); - Assert.assertFalse(message.hasExtension(defaultFixed32Extension )); - Assert.assertFalse(message.hasExtension(defaultFixed64Extension )); + Assert.assertFalse(message.hasExtension(defaultInt32Extension)); + Assert.assertFalse(message.hasExtension(defaultInt64Extension)); + Assert.assertFalse(message.hasExtension(defaultUint32Extension)); + Assert.assertFalse(message.hasExtension(defaultUint64Extension)); + Assert.assertFalse(message.hasExtension(defaultSint32Extension)); + Assert.assertFalse(message.hasExtension(defaultSint64Extension)); + Assert.assertFalse(message.hasExtension(defaultFixed32Extension)); + Assert.assertFalse(message.hasExtension(defaultFixed64Extension)); Assert.assertFalse(message.hasExtension(defaultSfixed32Extension)); Assert.assertFalse(message.hasExtension(defaultSfixed64Extension)); - Assert.assertFalse(message.hasExtension(defaultFloatExtension )); - Assert.assertFalse(message.hasExtension(defaultDoubleExtension )); - Assert.assertFalse(message.hasExtension(defaultBoolExtension )); - Assert.assertFalse(message.hasExtension(defaultStringExtension )); - Assert.assertFalse(message.hasExtension(defaultBytesExtension )); + Assert.assertFalse(message.hasExtension(defaultFloatExtension)); + Assert.assertFalse(message.hasExtension(defaultDoubleExtension)); + Assert.assertFalse(message.hasExtension(defaultBoolExtension)); + Assert.assertFalse(message.hasExtension(defaultStringExtension)); + Assert.assertFalse(message.hasExtension(defaultBytesExtension)); - Assert.assertFalse(message.hasExtension(defaultNestedEnumExtension )); + Assert.assertFalse(message.hasExtension(defaultNestedEnumExtension)); Assert.assertFalse(message.hasExtension(defaultForeignEnumExtension)); - Assert.assertFalse(message.hasExtension(defaultImportEnumExtension )); + Assert.assertFalse(message.hasExtension(defaultImportEnumExtension)); Assert.assertFalse(message.hasExtension(defaultStringPieceExtension)); Assert.assertFalse(message.hasExtension(defaultCordExtension)); // Fields with defaults have their default values (duh). - assertEqualsExactType( 41 , message.getExtension(defaultInt32Extension )); - assertEqualsExactType( 42L , message.getExtension(defaultInt64Extension )); - assertEqualsExactType( 43 , message.getExtension(defaultUint32Extension )); - assertEqualsExactType( 44L , message.getExtension(defaultUint64Extension )); - assertEqualsExactType(-45 , message.getExtension(defaultSint32Extension )); - assertEqualsExactType( 46L , message.getExtension(defaultSint64Extension )); - assertEqualsExactType( 47 , message.getExtension(defaultFixed32Extension )); - assertEqualsExactType( 48L , message.getExtension(defaultFixed64Extension )); - assertEqualsExactType( 49 , message.getExtension(defaultSfixed32Extension)); - assertEqualsExactType(-50L , message.getExtension(defaultSfixed64Extension)); - assertEqualsExactType( 51.5F , message.getExtension(defaultFloatExtension )); - assertEqualsExactType( 52e3D , message.getExtension(defaultDoubleExtension )); - assertEqualsExactType(true , message.getExtension(defaultBoolExtension )); - assertEqualsExactType("hello", message.getExtension(defaultStringExtension )); + assertEqualsExactType(41, message.getExtension(defaultInt32Extension)); + assertEqualsExactType(42L, message.getExtension(defaultInt64Extension)); + assertEqualsExactType(43, message.getExtension(defaultUint32Extension)); + assertEqualsExactType(44L, message.getExtension(defaultUint64Extension)); + assertEqualsExactType(-45, message.getExtension(defaultSint32Extension)); + assertEqualsExactType(46L, message.getExtension(defaultSint64Extension)); + assertEqualsExactType(47, message.getExtension(defaultFixed32Extension)); + assertEqualsExactType(48L, message.getExtension(defaultFixed64Extension)); + assertEqualsExactType(49, message.getExtension(defaultSfixed32Extension)); + assertEqualsExactType(-50L, message.getExtension(defaultSfixed64Extension)); + assertEqualsExactType(51.5F, message.getExtension(defaultFloatExtension)); + assertEqualsExactType(52e3D, message.getExtension(defaultDoubleExtension)); + assertEqualsExactType(true, message.getExtension(defaultBoolExtension)); + assertEqualsExactType("hello", message.getExtension(defaultStringExtension)); assertEqualsExactType(toBytes("world"), message.getExtension(defaultBytesExtension)); - assertEqualsExactType(TestAllTypes.NestedEnum.BAR, - message.getExtension(defaultNestedEnumExtension )); - assertEqualsExactType(ForeignEnum.FOREIGN_BAR, - message.getExtension(defaultForeignEnumExtension)); - assertEqualsExactType(ImportEnum.IMPORT_BAR, - message.getExtension(defaultImportEnumExtension)); + assertEqualsExactType( + TestAllTypes.NestedEnum.BAR, message.getExtension(defaultNestedEnumExtension)); + assertEqualsExactType( + ForeignEnum.FOREIGN_BAR, message.getExtension(defaultForeignEnumExtension)); + assertEqualsExactType(ImportEnum.IMPORT_BAR, message.getExtension(defaultImportEnumExtension)); assertEqualsExactType("abc", message.getExtension(defaultStringPieceExtension)); assertEqualsExactType("123", message.getExtension(defaultCordExtension)); @@ -1833,405 +1809,402 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all extensions of - * {@code message} are set to the values assigned by {@code setAllExtensions} - * followed by {@code modifyRepeatedExtensions}. + * Assert (using {@code junit.framework.Assert}} that all extensions of {@code message} are set to + * the values assigned by {@code setAllExtensions} followed by {@code modifyRepeatedExtensions}. */ - public static void assertRepeatedExtensionsModified( - TestAllExtensionsOrBuilder message) { + public static void assertRepeatedExtensionsModified(TestAllExtensionsOrBuilder message) { // ModifyRepeatedFields only sets the second repeated element of each // field. In addition to verifying this, we also verify that the first // element and size were *not* modified. - Assert.assertEquals(2, message.getExtensionCount(repeatedInt32Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedInt64Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedUint32Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedUint64Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedSint32Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedSint64Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32Extension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64Extension )); + Assert.assertEquals(2, message.getExtensionCount(repeatedInt32Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedInt64Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedUint32Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedUint64Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedSint32Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedSint64Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32Extension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64Extension)); Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed32Extension)); Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed64Extension)); - Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtension )); - - Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtension )); + Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtension)); + + Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtension)); Assert.assertEquals(2, message.getExtensionCount(repeatedForeignMessageExtension)); - Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedLazyMessageExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtension )); - Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtension )); + Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedLazyMessageExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtension)); + Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtension)); Assert.assertEquals(2, message.getExtensionCount(repeatedStringPieceExtension)); Assert.assertEquals(2, message.getExtensionCount(repeatedCordExtension)); - assertEqualsExactType(201 , message.getExtension(repeatedInt32Extension , 0)); - assertEqualsExactType(202L , message.getExtension(repeatedInt64Extension , 0)); - assertEqualsExactType(203 , message.getExtension(repeatedUint32Extension , 0)); - assertEqualsExactType(204L , message.getExtension(repeatedUint64Extension , 0)); - assertEqualsExactType(205 , message.getExtension(repeatedSint32Extension , 0)); - assertEqualsExactType(206L , message.getExtension(repeatedSint64Extension , 0)); - assertEqualsExactType(207 , message.getExtension(repeatedFixed32Extension , 0)); - assertEqualsExactType(208L , message.getExtension(repeatedFixed64Extension , 0)); - assertEqualsExactType(209 , message.getExtension(repeatedSfixed32Extension, 0)); - assertEqualsExactType(210L , message.getExtension(repeatedSfixed64Extension, 0)); - assertEqualsExactType(211F , message.getExtension(repeatedFloatExtension , 0)); - assertEqualsExactType(212D , message.getExtension(repeatedDoubleExtension , 0)); - assertEqualsExactType(true , message.getExtension(repeatedBoolExtension , 0)); - assertEqualsExactType("215", message.getExtension(repeatedStringExtension , 0)); + assertEqualsExactType(201, message.getExtension(repeatedInt32Extension, 0)); + assertEqualsExactType(202L, message.getExtension(repeatedInt64Extension, 0)); + assertEqualsExactType(203, message.getExtension(repeatedUint32Extension, 0)); + assertEqualsExactType(204L, message.getExtension(repeatedUint64Extension, 0)); + assertEqualsExactType(205, message.getExtension(repeatedSint32Extension, 0)); + assertEqualsExactType(206L, message.getExtension(repeatedSint64Extension, 0)); + assertEqualsExactType(207, message.getExtension(repeatedFixed32Extension, 0)); + assertEqualsExactType(208L, message.getExtension(repeatedFixed64Extension, 0)); + assertEqualsExactType(209, message.getExtension(repeatedSfixed32Extension, 0)); + assertEqualsExactType(210L, message.getExtension(repeatedSfixed64Extension, 0)); + assertEqualsExactType(211F, message.getExtension(repeatedFloatExtension, 0)); + assertEqualsExactType(212D, message.getExtension(repeatedDoubleExtension, 0)); + assertEqualsExactType(true, message.getExtension(repeatedBoolExtension, 0)); + assertEqualsExactType("215", message.getExtension(repeatedStringExtension, 0)); assertEqualsExactType(toBytes("216"), message.getExtension(repeatedBytesExtension, 0)); - assertEqualsExactType(217, message.getExtension(repeatedGroupExtension , 0).getA()); - assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtension , 0).getBb()); + assertEqualsExactType(217, message.getExtension(repeatedGroupExtension, 0).getA()); + assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtension, 0).getBb()); assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtension, 0).getC()); - assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtension , 0).getD()); - assertEqualsExactType(227, message.getExtension(repeatedLazyMessageExtension , 0).getBb()); + assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtension, 0).getD()); + assertEqualsExactType(227, message.getExtension(repeatedLazyMessageExtension, 0).getBb()); - assertEqualsExactType(TestAllTypes.NestedEnum.BAR, - message.getExtension(repeatedNestedEnumExtension, 0)); - assertEqualsExactType(ForeignEnum.FOREIGN_BAR, - message.getExtension(repeatedForeignEnumExtension, 0)); - assertEqualsExactType(ImportEnum.IMPORT_BAR, - message.getExtension(repeatedImportEnumExtension, 0)); + assertEqualsExactType( + TestAllTypes.NestedEnum.BAR, message.getExtension(repeatedNestedEnumExtension, 0)); + assertEqualsExactType( + ForeignEnum.FOREIGN_BAR, message.getExtension(repeatedForeignEnumExtension, 0)); + assertEqualsExactType( + ImportEnum.IMPORT_BAR, message.getExtension(repeatedImportEnumExtension, 0)); assertEqualsExactType("224", message.getExtension(repeatedStringPieceExtension, 0)); assertEqualsExactType("225", message.getExtension(repeatedCordExtension, 0)); // Actually verify the second (modified) elements now. - assertEqualsExactType(501 , message.getExtension(repeatedInt32Extension , 1)); - assertEqualsExactType(502L , message.getExtension(repeatedInt64Extension , 1)); - assertEqualsExactType(503 , message.getExtension(repeatedUint32Extension , 1)); - assertEqualsExactType(504L , message.getExtension(repeatedUint64Extension , 1)); - assertEqualsExactType(505 , message.getExtension(repeatedSint32Extension , 1)); - assertEqualsExactType(506L , message.getExtension(repeatedSint64Extension , 1)); - assertEqualsExactType(507 , message.getExtension(repeatedFixed32Extension , 1)); - assertEqualsExactType(508L , message.getExtension(repeatedFixed64Extension , 1)); - assertEqualsExactType(509 , message.getExtension(repeatedSfixed32Extension, 1)); - assertEqualsExactType(510L , message.getExtension(repeatedSfixed64Extension, 1)); - assertEqualsExactType(511F , message.getExtension(repeatedFloatExtension , 1)); - assertEqualsExactType(512D , message.getExtension(repeatedDoubleExtension , 1)); - assertEqualsExactType(true , message.getExtension(repeatedBoolExtension , 1)); - assertEqualsExactType("515", message.getExtension(repeatedStringExtension , 1)); + assertEqualsExactType(501, message.getExtension(repeatedInt32Extension, 1)); + assertEqualsExactType(502L, message.getExtension(repeatedInt64Extension, 1)); + assertEqualsExactType(503, message.getExtension(repeatedUint32Extension, 1)); + assertEqualsExactType(504L, message.getExtension(repeatedUint64Extension, 1)); + assertEqualsExactType(505, message.getExtension(repeatedSint32Extension, 1)); + assertEqualsExactType(506L, message.getExtension(repeatedSint64Extension, 1)); + assertEqualsExactType(507, message.getExtension(repeatedFixed32Extension, 1)); + assertEqualsExactType(508L, message.getExtension(repeatedFixed64Extension, 1)); + assertEqualsExactType(509, message.getExtension(repeatedSfixed32Extension, 1)); + assertEqualsExactType(510L, message.getExtension(repeatedSfixed64Extension, 1)); + assertEqualsExactType(511F, message.getExtension(repeatedFloatExtension, 1)); + assertEqualsExactType(512D, message.getExtension(repeatedDoubleExtension, 1)); + assertEqualsExactType(true, message.getExtension(repeatedBoolExtension, 1)); + assertEqualsExactType("515", message.getExtension(repeatedStringExtension, 1)); assertEqualsExactType(toBytes("516"), message.getExtension(repeatedBytesExtension, 1)); - assertEqualsExactType(517, message.getExtension(repeatedGroupExtension , 1).getA()); - assertEqualsExactType(518, message.getExtension(repeatedNestedMessageExtension , 1).getBb()); + assertEqualsExactType(517, message.getExtension(repeatedGroupExtension, 1).getA()); + assertEqualsExactType(518, message.getExtension(repeatedNestedMessageExtension, 1).getBb()); assertEqualsExactType(519, message.getExtension(repeatedForeignMessageExtension, 1).getC()); - assertEqualsExactType(520, message.getExtension(repeatedImportMessageExtension , 1).getD()); - assertEqualsExactType(527, message.getExtension(repeatedLazyMessageExtension , 1).getBb()); + assertEqualsExactType(520, message.getExtension(repeatedImportMessageExtension, 1).getD()); + assertEqualsExactType(527, message.getExtension(repeatedLazyMessageExtension, 1).getBb()); - assertEqualsExactType(TestAllTypes.NestedEnum.FOO, - message.getExtension(repeatedNestedEnumExtension, 1)); - assertEqualsExactType(ForeignEnum.FOREIGN_FOO, - message.getExtension(repeatedForeignEnumExtension, 1)); - assertEqualsExactType(ImportEnum.IMPORT_FOO, - message.getExtension(repeatedImportEnumExtension, 1)); + assertEqualsExactType( + TestAllTypes.NestedEnum.FOO, message.getExtension(repeatedNestedEnumExtension, 1)); + assertEqualsExactType( + ForeignEnum.FOREIGN_FOO, message.getExtension(repeatedForeignEnumExtension, 1)); + assertEqualsExactType( + ImportEnum.IMPORT_FOO, message.getExtension(repeatedImportEnumExtension, 1)); assertEqualsExactType("524", message.getExtension(repeatedStringPieceExtension, 1)); assertEqualsExactType("525", message.getExtension(repeatedCordExtension, 1)); } public static void setPackedExtensions(TestPackedExtensions.Builder message) { - message.addExtension(packedInt32Extension , 601); - message.addExtension(packedInt64Extension , 602L); - message.addExtension(packedUint32Extension , 603); - message.addExtension(packedUint64Extension , 604L); - message.addExtension(packedSint32Extension , 605); - message.addExtension(packedSint64Extension , 606L); - message.addExtension(packedFixed32Extension , 607); - message.addExtension(packedFixed64Extension , 608L); + message.addExtension(packedInt32Extension, 601); + message.addExtension(packedInt64Extension, 602L); + message.addExtension(packedUint32Extension, 603); + message.addExtension(packedUint64Extension, 604L); + message.addExtension(packedSint32Extension, 605); + message.addExtension(packedSint64Extension, 606L); + message.addExtension(packedFixed32Extension, 607); + message.addExtension(packedFixed64Extension, 608L); message.addExtension(packedSfixed32Extension, 609); message.addExtension(packedSfixed64Extension, 610L); - message.addExtension(packedFloatExtension , 611F); - message.addExtension(packedDoubleExtension , 612D); - message.addExtension(packedBoolExtension , true); + message.addExtension(packedFloatExtension, 611F); + message.addExtension(packedDoubleExtension, 612D); + message.addExtension(packedBoolExtension, true); message.addExtension(packedEnumExtension, ForeignEnum.FOREIGN_BAR); // Add a second one of each field. - message.addExtension(packedInt32Extension , 701); - message.addExtension(packedInt64Extension , 702L); - message.addExtension(packedUint32Extension , 703); - message.addExtension(packedUint64Extension , 704L); - message.addExtension(packedSint32Extension , 705); - message.addExtension(packedSint64Extension , 706L); - message.addExtension(packedFixed32Extension , 707); - message.addExtension(packedFixed64Extension , 708L); + message.addExtension(packedInt32Extension, 701); + message.addExtension(packedInt64Extension, 702L); + message.addExtension(packedUint32Extension, 703); + message.addExtension(packedUint64Extension, 704L); + message.addExtension(packedSint32Extension, 705); + message.addExtension(packedSint64Extension, 706L); + message.addExtension(packedFixed32Extension, 707); + message.addExtension(packedFixed64Extension, 708L); message.addExtension(packedSfixed32Extension, 709); message.addExtension(packedSfixed64Extension, 710L); - message.addExtension(packedFloatExtension , 711F); - message.addExtension(packedDoubleExtension , 712D); - message.addExtension(packedBoolExtension , false); + message.addExtension(packedFloatExtension, 711F); + message.addExtension(packedDoubleExtension, 712D); + message.addExtension(packedBoolExtension, false); message.addExtension(packedEnumExtension, ForeignEnum.FOREIGN_BAZ); } public static void assertPackedExtensionsSet(TestPackedExtensions message) { - Assert.assertEquals(2, message.getExtensionCount(packedInt32Extension )); - Assert.assertEquals(2, message.getExtensionCount(packedInt64Extension )); - Assert.assertEquals(2, message.getExtensionCount(packedUint32Extension )); - Assert.assertEquals(2, message.getExtensionCount(packedUint64Extension )); - Assert.assertEquals(2, message.getExtensionCount(packedSint32Extension )); - Assert.assertEquals(2, message.getExtensionCount(packedSint64Extension )); - Assert.assertEquals(2, message.getExtensionCount(packedFixed32Extension )); - Assert.assertEquals(2, message.getExtensionCount(packedFixed64Extension )); + Assert.assertEquals(2, message.getExtensionCount(packedInt32Extension)); + Assert.assertEquals(2, message.getExtensionCount(packedInt64Extension)); + Assert.assertEquals(2, message.getExtensionCount(packedUint32Extension)); + Assert.assertEquals(2, message.getExtensionCount(packedUint64Extension)); + Assert.assertEquals(2, message.getExtensionCount(packedSint32Extension)); + Assert.assertEquals(2, message.getExtensionCount(packedSint64Extension)); + Assert.assertEquals(2, message.getExtensionCount(packedFixed32Extension)); + Assert.assertEquals(2, message.getExtensionCount(packedFixed64Extension)); Assert.assertEquals(2, message.getExtensionCount(packedSfixed32Extension)); Assert.assertEquals(2, message.getExtensionCount(packedSfixed64Extension)); - Assert.assertEquals(2, message.getExtensionCount(packedFloatExtension )); - Assert.assertEquals(2, message.getExtensionCount(packedDoubleExtension )); - Assert.assertEquals(2, message.getExtensionCount(packedBoolExtension )); + Assert.assertEquals(2, message.getExtensionCount(packedFloatExtension)); + Assert.assertEquals(2, message.getExtensionCount(packedDoubleExtension)); + Assert.assertEquals(2, message.getExtensionCount(packedBoolExtension)); Assert.assertEquals(2, message.getExtensionCount(packedEnumExtension)); - assertEqualsExactType(601 , message.getExtension(packedInt32Extension , 0)); - assertEqualsExactType(602L , message.getExtension(packedInt64Extension , 0)); - assertEqualsExactType(603 , message.getExtension(packedUint32Extension , 0)); - assertEqualsExactType(604L , message.getExtension(packedUint64Extension , 0)); - assertEqualsExactType(605 , message.getExtension(packedSint32Extension , 0)); - assertEqualsExactType(606L , message.getExtension(packedSint64Extension , 0)); - assertEqualsExactType(607 , message.getExtension(packedFixed32Extension , 0)); - assertEqualsExactType(608L , message.getExtension(packedFixed64Extension , 0)); - assertEqualsExactType(609 , message.getExtension(packedSfixed32Extension, 0)); - assertEqualsExactType(610L , message.getExtension(packedSfixed64Extension, 0)); - assertEqualsExactType(611F , message.getExtension(packedFloatExtension , 0)); - assertEqualsExactType(612D , message.getExtension(packedDoubleExtension , 0)); - assertEqualsExactType(true , message.getExtension(packedBoolExtension , 0)); - assertEqualsExactType(ForeignEnum.FOREIGN_BAR, - message.getExtension(packedEnumExtension, 0)); - assertEqualsExactType(701 , message.getExtension(packedInt32Extension , 1)); - assertEqualsExactType(702L , message.getExtension(packedInt64Extension , 1)); - assertEqualsExactType(703 , message.getExtension(packedUint32Extension , 1)); - assertEqualsExactType(704L , message.getExtension(packedUint64Extension , 1)); - assertEqualsExactType(705 , message.getExtension(packedSint32Extension , 1)); - assertEqualsExactType(706L , message.getExtension(packedSint64Extension , 1)); - assertEqualsExactType(707 , message.getExtension(packedFixed32Extension , 1)); - assertEqualsExactType(708L , message.getExtension(packedFixed64Extension , 1)); - assertEqualsExactType(709 , message.getExtension(packedSfixed32Extension, 1)); - assertEqualsExactType(710L , message.getExtension(packedSfixed64Extension, 1)); - assertEqualsExactType(711F , message.getExtension(packedFloatExtension , 1)); - assertEqualsExactType(712D , message.getExtension(packedDoubleExtension , 1)); - assertEqualsExactType(false, message.getExtension(packedBoolExtension , 1)); - assertEqualsExactType(ForeignEnum.FOREIGN_BAZ, - message.getExtension(packedEnumExtension, 1)); + assertEqualsExactType(601, message.getExtension(packedInt32Extension, 0)); + assertEqualsExactType(602L, message.getExtension(packedInt64Extension, 0)); + assertEqualsExactType(603, message.getExtension(packedUint32Extension, 0)); + assertEqualsExactType(604L, message.getExtension(packedUint64Extension, 0)); + assertEqualsExactType(605, message.getExtension(packedSint32Extension, 0)); + assertEqualsExactType(606L, message.getExtension(packedSint64Extension, 0)); + assertEqualsExactType(607, message.getExtension(packedFixed32Extension, 0)); + assertEqualsExactType(608L, message.getExtension(packedFixed64Extension, 0)); + assertEqualsExactType(609, message.getExtension(packedSfixed32Extension, 0)); + assertEqualsExactType(610L, message.getExtension(packedSfixed64Extension, 0)); + assertEqualsExactType(611F, message.getExtension(packedFloatExtension, 0)); + assertEqualsExactType(612D, message.getExtension(packedDoubleExtension, 0)); + assertEqualsExactType(true, message.getExtension(packedBoolExtension, 0)); + assertEqualsExactType(ForeignEnum.FOREIGN_BAR, message.getExtension(packedEnumExtension, 0)); + assertEqualsExactType(701, message.getExtension(packedInt32Extension, 1)); + assertEqualsExactType(702L, message.getExtension(packedInt64Extension, 1)); + assertEqualsExactType(703, message.getExtension(packedUint32Extension, 1)); + assertEqualsExactType(704L, message.getExtension(packedUint64Extension, 1)); + assertEqualsExactType(705, message.getExtension(packedSint32Extension, 1)); + assertEqualsExactType(706L, message.getExtension(packedSint64Extension, 1)); + assertEqualsExactType(707, message.getExtension(packedFixed32Extension, 1)); + assertEqualsExactType(708L, message.getExtension(packedFixed64Extension, 1)); + assertEqualsExactType(709, message.getExtension(packedSfixed32Extension, 1)); + assertEqualsExactType(710L, message.getExtension(packedSfixed64Extension, 1)); + assertEqualsExactType(711F, message.getExtension(packedFloatExtension, 1)); + assertEqualsExactType(712D, message.getExtension(packedDoubleExtension, 1)); + assertEqualsExactType(false, message.getExtension(packedBoolExtension, 1)); + assertEqualsExactType(ForeignEnum.FOREIGN_BAZ, message.getExtension(packedEnumExtension, 1)); } // =================================================================== // Lite extensions /** - * Assert (using {@code junit.framework.Assert}} that all extensions of - * {@code message} are set to the values assigned by {@code setAllExtensions}. + * Assert (using {@code junit.framework.Assert}} that all extensions of {@code message} are set to + * the values assigned by {@code setAllExtensions}. */ - public static void assertAllExtensionsSet( - TestAllExtensionsLiteOrBuilder message) { - Assert.assertTrue(message.hasExtension(optionalInt32ExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalInt64ExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalUint32ExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalUint64ExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalSint32ExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalSint64ExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalFixed32ExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalFixed64ExtensionLite )); + public static void assertAllExtensionsSet(TestAllExtensionsLiteOrBuilder message) { + Assert.assertTrue(message.hasExtension(optionalInt32ExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalInt64ExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalUint32ExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalUint64ExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalSint32ExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalSint64ExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalFixed32ExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalFixed64ExtensionLite)); Assert.assertTrue(message.hasExtension(optionalSfixed32ExtensionLite)); Assert.assertTrue(message.hasExtension(optionalSfixed64ExtensionLite)); - Assert.assertTrue(message.hasExtension(optionalFloatExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalDoubleExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalBoolExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalStringExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalBytesExtensionLite )); - - Assert.assertTrue(message.hasExtension(optionalGroupExtensionLite )); - Assert.assertTrue(message.hasExtension(optionalNestedMessageExtensionLite )); + Assert.assertTrue(message.hasExtension(optionalFloatExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalDoubleExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalBoolExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalStringExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalBytesExtensionLite)); + + Assert.assertTrue(message.hasExtension(optionalGroupExtensionLite)); + Assert.assertTrue(message.hasExtension(optionalNestedMessageExtensionLite)); Assert.assertTrue(message.hasExtension(optionalForeignMessageExtensionLite)); - Assert.assertTrue(message.hasExtension(optionalImportMessageExtensionLite )); + Assert.assertTrue(message.hasExtension(optionalImportMessageExtensionLite)); - Assert.assertTrue(message.getExtension(optionalGroupExtensionLite ).hasA()); - Assert.assertTrue(message.getExtension(optionalNestedMessageExtensionLite ).hasBb()); + Assert.assertTrue(message.getExtension(optionalGroupExtensionLite).hasA()); + Assert.assertTrue(message.getExtension(optionalNestedMessageExtensionLite).hasBb()); Assert.assertTrue(message.getExtension(optionalForeignMessageExtensionLite).hasC()); - Assert.assertTrue(message.getExtension(optionalImportMessageExtensionLite ).hasD()); + Assert.assertTrue(message.getExtension(optionalImportMessageExtensionLite).hasD()); - Assert.assertTrue(message.hasExtension(optionalNestedEnumExtensionLite )); + Assert.assertTrue(message.hasExtension(optionalNestedEnumExtensionLite)); Assert.assertTrue(message.hasExtension(optionalForeignEnumExtensionLite)); - Assert.assertTrue(message.hasExtension(optionalImportEnumExtensionLite )); + Assert.assertTrue(message.hasExtension(optionalImportEnumExtensionLite)); Assert.assertTrue(message.hasExtension(optionalStringPieceExtensionLite)); Assert.assertTrue(message.hasExtension(optionalCordExtensionLite)); - assertEqualsExactType(101 , message.getExtension(optionalInt32ExtensionLite )); - assertEqualsExactType(102L , message.getExtension(optionalInt64ExtensionLite )); - assertEqualsExactType(103 , message.getExtension(optionalUint32ExtensionLite )); - assertEqualsExactType(104L , message.getExtension(optionalUint64ExtensionLite )); - assertEqualsExactType(105 , message.getExtension(optionalSint32ExtensionLite )); - assertEqualsExactType(106L , message.getExtension(optionalSint64ExtensionLite )); - assertEqualsExactType(107 , message.getExtension(optionalFixed32ExtensionLite )); - assertEqualsExactType(108L , message.getExtension(optionalFixed64ExtensionLite )); - assertEqualsExactType(109 , message.getExtension(optionalSfixed32ExtensionLite)); - assertEqualsExactType(110L , message.getExtension(optionalSfixed64ExtensionLite)); - assertEqualsExactType(111F , message.getExtension(optionalFloatExtensionLite )); - assertEqualsExactType(112D , message.getExtension(optionalDoubleExtensionLite )); - assertEqualsExactType(true , message.getExtension(optionalBoolExtensionLite )); - assertEqualsExactType("115", message.getExtension(optionalStringExtensionLite )); + assertEqualsExactType(101, message.getExtension(optionalInt32ExtensionLite)); + assertEqualsExactType(102L, message.getExtension(optionalInt64ExtensionLite)); + assertEqualsExactType(103, message.getExtension(optionalUint32ExtensionLite)); + assertEqualsExactType(104L, message.getExtension(optionalUint64ExtensionLite)); + assertEqualsExactType(105, message.getExtension(optionalSint32ExtensionLite)); + assertEqualsExactType(106L, message.getExtension(optionalSint64ExtensionLite)); + assertEqualsExactType(107, message.getExtension(optionalFixed32ExtensionLite)); + assertEqualsExactType(108L, message.getExtension(optionalFixed64ExtensionLite)); + assertEqualsExactType(109, message.getExtension(optionalSfixed32ExtensionLite)); + assertEqualsExactType(110L, message.getExtension(optionalSfixed64ExtensionLite)); + assertEqualsExactType(111F, message.getExtension(optionalFloatExtensionLite)); + assertEqualsExactType(112D, message.getExtension(optionalDoubleExtensionLite)); + assertEqualsExactType(true, message.getExtension(optionalBoolExtensionLite)); + assertEqualsExactType("115", message.getExtension(optionalStringExtensionLite)); assertEqualsExactType(toBytes("116"), message.getExtension(optionalBytesExtensionLite)); - assertEqualsExactType(117, message.getExtension(optionalGroupExtensionLite ).getA()); - assertEqualsExactType(118, message.getExtension(optionalNestedMessageExtensionLite ).getBb()); + assertEqualsExactType(117, message.getExtension(optionalGroupExtensionLite).getA()); + assertEqualsExactType(118, message.getExtension(optionalNestedMessageExtensionLite).getBb()); assertEqualsExactType(119, message.getExtension(optionalForeignMessageExtensionLite).getC()); - assertEqualsExactType(120, message.getExtension(optionalImportMessageExtensionLite ).getD()); - assertEqualsExactType(126, message.getExtension( - optionalPublicImportMessageExtensionLite).getE()); + assertEqualsExactType(120, message.getExtension(optionalImportMessageExtensionLite).getD()); + assertEqualsExactType( + 126, message.getExtension(optionalPublicImportMessageExtensionLite).getE()); assertEqualsExactType(127, message.getExtension(optionalLazyMessageExtensionLite).getBb()); - assertEqualsExactType(TestAllTypesLite.NestedEnum.BAZ, - message.getExtension(optionalNestedEnumExtensionLite)); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAZ, - message.getExtension(optionalForeignEnumExtensionLite)); - assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAZ, - message.getExtension(optionalImportEnumExtensionLite)); + assertEqualsExactType( + TestAllTypesLite.NestedEnum.BAZ, message.getExtension(optionalNestedEnumExtensionLite)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_BAZ, message.getExtension(optionalForeignEnumExtensionLite)); + assertEqualsExactType( + ImportEnumLite.IMPORT_LITE_BAZ, message.getExtension(optionalImportEnumExtensionLite)); assertEqualsExactType("124", message.getExtension(optionalStringPieceExtensionLite)); assertEqualsExactType("125", message.getExtension(optionalCordExtensionLite)); // ----------------------------------------------------------------- - Assert.assertEquals(2, message.getExtensionCount(repeatedInt32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedInt64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedUint32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedUint64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedSint32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedSint64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64ExtensionLite )); + Assert.assertEquals(2, message.getExtensionCount(repeatedInt32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedInt64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedUint32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedUint64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedSint32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedSint64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64ExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed32ExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed64ExtensionLite)); - Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtensionLite )); - - Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtensionLite )); + Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtensionLite)); + + Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedForeignMessageExtensionLite)); - Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedLazyMessageExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtensionLite )); + Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedLazyMessageExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedStringPieceExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedCordExtensionLite)); - assertEqualsExactType(201 , message.getExtension(repeatedInt32ExtensionLite , 0)); - assertEqualsExactType(202L , message.getExtension(repeatedInt64ExtensionLite , 0)); - assertEqualsExactType(203 , message.getExtension(repeatedUint32ExtensionLite , 0)); - assertEqualsExactType(204L , message.getExtension(repeatedUint64ExtensionLite , 0)); - assertEqualsExactType(205 , message.getExtension(repeatedSint32ExtensionLite , 0)); - assertEqualsExactType(206L , message.getExtension(repeatedSint64ExtensionLite , 0)); - assertEqualsExactType(207 , message.getExtension(repeatedFixed32ExtensionLite , 0)); - assertEqualsExactType(208L , message.getExtension(repeatedFixed64ExtensionLite , 0)); - assertEqualsExactType(209 , message.getExtension(repeatedSfixed32ExtensionLite, 0)); - assertEqualsExactType(210L , message.getExtension(repeatedSfixed64ExtensionLite, 0)); - assertEqualsExactType(211F , message.getExtension(repeatedFloatExtensionLite , 0)); - assertEqualsExactType(212D , message.getExtension(repeatedDoubleExtensionLite , 0)); - assertEqualsExactType(true , message.getExtension(repeatedBoolExtensionLite , 0)); - assertEqualsExactType("215", message.getExtension(repeatedStringExtensionLite , 0)); + assertEqualsExactType(201, message.getExtension(repeatedInt32ExtensionLite, 0)); + assertEqualsExactType(202L, message.getExtension(repeatedInt64ExtensionLite, 0)); + assertEqualsExactType(203, message.getExtension(repeatedUint32ExtensionLite, 0)); + assertEqualsExactType(204L, message.getExtension(repeatedUint64ExtensionLite, 0)); + assertEqualsExactType(205, message.getExtension(repeatedSint32ExtensionLite, 0)); + assertEqualsExactType(206L, message.getExtension(repeatedSint64ExtensionLite, 0)); + assertEqualsExactType(207, message.getExtension(repeatedFixed32ExtensionLite, 0)); + assertEqualsExactType(208L, message.getExtension(repeatedFixed64ExtensionLite, 0)); + assertEqualsExactType(209, message.getExtension(repeatedSfixed32ExtensionLite, 0)); + assertEqualsExactType(210L, message.getExtension(repeatedSfixed64ExtensionLite, 0)); + assertEqualsExactType(211F, message.getExtension(repeatedFloatExtensionLite, 0)); + assertEqualsExactType(212D, message.getExtension(repeatedDoubleExtensionLite, 0)); + assertEqualsExactType(true, message.getExtension(repeatedBoolExtensionLite, 0)); + assertEqualsExactType("215", message.getExtension(repeatedStringExtensionLite, 0)); assertEqualsExactType(toBytes("216"), message.getExtension(repeatedBytesExtensionLite, 0)); - assertEqualsExactType(217, message.getExtension(repeatedGroupExtensionLite ,0).getA()); - assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtensionLite ,0).getBb()); - assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtensionLite,0).getC()); - assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtensionLite ,0).getD()); - assertEqualsExactType(227, message.getExtension(repeatedLazyMessageExtensionLite ,0).getBb()); + assertEqualsExactType(217, message.getExtension(repeatedGroupExtensionLite, 0).getA()); + assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtensionLite, 0).getBb()); + assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtensionLite, 0).getC()); + assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtensionLite, 0).getD()); + assertEqualsExactType(227, message.getExtension(repeatedLazyMessageExtensionLite, 0).getBb()); - assertEqualsExactType(TestAllTypesLite.NestedEnum.BAR, - message.getExtension(repeatedNestedEnumExtensionLite, 0)); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAR, - message.getExtension(repeatedForeignEnumExtensionLite, 0)); - assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAR, - message.getExtension(repeatedImportEnumExtensionLite, 0)); + assertEqualsExactType( + TestAllTypesLite.NestedEnum.BAR, message.getExtension(repeatedNestedEnumExtensionLite, 0)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_BAR, + message.getExtension(repeatedForeignEnumExtensionLite, 0)); + assertEqualsExactType( + ImportEnumLite.IMPORT_LITE_BAR, message.getExtension(repeatedImportEnumExtensionLite, 0)); assertEqualsExactType("224", message.getExtension(repeatedStringPieceExtensionLite, 0)); assertEqualsExactType("225", message.getExtension(repeatedCordExtensionLite, 0)); - assertEqualsExactType(301 , message.getExtension(repeatedInt32ExtensionLite , 1)); - assertEqualsExactType(302L , message.getExtension(repeatedInt64ExtensionLite , 1)); - assertEqualsExactType(303 , message.getExtension(repeatedUint32ExtensionLite , 1)); - assertEqualsExactType(304L , message.getExtension(repeatedUint64ExtensionLite , 1)); - assertEqualsExactType(305 , message.getExtension(repeatedSint32ExtensionLite , 1)); - assertEqualsExactType(306L , message.getExtension(repeatedSint64ExtensionLite , 1)); - assertEqualsExactType(307 , message.getExtension(repeatedFixed32ExtensionLite , 1)); - assertEqualsExactType(308L , message.getExtension(repeatedFixed64ExtensionLite , 1)); - assertEqualsExactType(309 , message.getExtension(repeatedSfixed32ExtensionLite, 1)); - assertEqualsExactType(310L , message.getExtension(repeatedSfixed64ExtensionLite, 1)); - assertEqualsExactType(311F , message.getExtension(repeatedFloatExtensionLite , 1)); - assertEqualsExactType(312D , message.getExtension(repeatedDoubleExtensionLite , 1)); - assertEqualsExactType(false, message.getExtension(repeatedBoolExtensionLite , 1)); - assertEqualsExactType("315", message.getExtension(repeatedStringExtensionLite , 1)); + assertEqualsExactType(301, message.getExtension(repeatedInt32ExtensionLite, 1)); + assertEqualsExactType(302L, message.getExtension(repeatedInt64ExtensionLite, 1)); + assertEqualsExactType(303, message.getExtension(repeatedUint32ExtensionLite, 1)); + assertEqualsExactType(304L, message.getExtension(repeatedUint64ExtensionLite, 1)); + assertEqualsExactType(305, message.getExtension(repeatedSint32ExtensionLite, 1)); + assertEqualsExactType(306L, message.getExtension(repeatedSint64ExtensionLite, 1)); + assertEqualsExactType(307, message.getExtension(repeatedFixed32ExtensionLite, 1)); + assertEqualsExactType(308L, message.getExtension(repeatedFixed64ExtensionLite, 1)); + assertEqualsExactType(309, message.getExtension(repeatedSfixed32ExtensionLite, 1)); + assertEqualsExactType(310L, message.getExtension(repeatedSfixed64ExtensionLite, 1)); + assertEqualsExactType(311F, message.getExtension(repeatedFloatExtensionLite, 1)); + assertEqualsExactType(312D, message.getExtension(repeatedDoubleExtensionLite, 1)); + assertEqualsExactType(false, message.getExtension(repeatedBoolExtensionLite, 1)); + assertEqualsExactType("315", message.getExtension(repeatedStringExtensionLite, 1)); assertEqualsExactType(toBytes("316"), message.getExtension(repeatedBytesExtensionLite, 1)); - assertEqualsExactType(317, message.getExtension(repeatedGroupExtensionLite ,1).getA()); - assertEqualsExactType(318, message.getExtension(repeatedNestedMessageExtensionLite ,1).getBb()); - assertEqualsExactType(319, message.getExtension(repeatedForeignMessageExtensionLite,1).getC()); - assertEqualsExactType(320, message.getExtension(repeatedImportMessageExtensionLite ,1).getD()); - assertEqualsExactType(327, message.getExtension(repeatedLazyMessageExtensionLite ,1).getBb()); + assertEqualsExactType(317, message.getExtension(repeatedGroupExtensionLite, 1).getA()); + assertEqualsExactType(318, message.getExtension(repeatedNestedMessageExtensionLite, 1).getBb()); + assertEqualsExactType(319, message.getExtension(repeatedForeignMessageExtensionLite, 1).getC()); + assertEqualsExactType(320, message.getExtension(repeatedImportMessageExtensionLite, 1).getD()); + assertEqualsExactType(327, message.getExtension(repeatedLazyMessageExtensionLite, 1).getBb()); - assertEqualsExactType(TestAllTypesLite.NestedEnum.BAZ, - message.getExtension(repeatedNestedEnumExtensionLite, 1)); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAZ, - message.getExtension(repeatedForeignEnumExtensionLite, 1)); - assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAZ, - message.getExtension(repeatedImportEnumExtensionLite, 1)); + assertEqualsExactType( + TestAllTypesLite.NestedEnum.BAZ, message.getExtension(repeatedNestedEnumExtensionLite, 1)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_BAZ, + message.getExtension(repeatedForeignEnumExtensionLite, 1)); + assertEqualsExactType( + ImportEnumLite.IMPORT_LITE_BAZ, message.getExtension(repeatedImportEnumExtensionLite, 1)); assertEqualsExactType("324", message.getExtension(repeatedStringPieceExtensionLite, 1)); assertEqualsExactType("325", message.getExtension(repeatedCordExtensionLite, 1)); // ----------------------------------------------------------------- - Assert.assertTrue(message.hasExtension(defaultInt32ExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultInt64ExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultUint32ExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultUint64ExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultSint32ExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultSint64ExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultFixed32ExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultFixed64ExtensionLite )); + Assert.assertTrue(message.hasExtension(defaultInt32ExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultInt64ExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultUint32ExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultUint64ExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultSint32ExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultSint64ExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultFixed32ExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultFixed64ExtensionLite)); Assert.assertTrue(message.hasExtension(defaultSfixed32ExtensionLite)); Assert.assertTrue(message.hasExtension(defaultSfixed64ExtensionLite)); - Assert.assertTrue(message.hasExtension(defaultFloatExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultDoubleExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultBoolExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultStringExtensionLite )); - Assert.assertTrue(message.hasExtension(defaultBytesExtensionLite )); + Assert.assertTrue(message.hasExtension(defaultFloatExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultDoubleExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultBoolExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultStringExtensionLite)); + Assert.assertTrue(message.hasExtension(defaultBytesExtensionLite)); - Assert.assertTrue(message.hasExtension(defaultNestedEnumExtensionLite )); + Assert.assertTrue(message.hasExtension(defaultNestedEnumExtensionLite)); Assert.assertTrue(message.hasExtension(defaultForeignEnumExtensionLite)); - Assert.assertTrue(message.hasExtension(defaultImportEnumExtensionLite )); + Assert.assertTrue(message.hasExtension(defaultImportEnumExtensionLite)); Assert.assertTrue(message.hasExtension(defaultStringPieceExtensionLite)); Assert.assertTrue(message.hasExtension(defaultCordExtensionLite)); - assertEqualsExactType(401 , message.getExtension(defaultInt32ExtensionLite )); - assertEqualsExactType(402L , message.getExtension(defaultInt64ExtensionLite )); - assertEqualsExactType(403 , message.getExtension(defaultUint32ExtensionLite )); - assertEqualsExactType(404L , message.getExtension(defaultUint64ExtensionLite )); - assertEqualsExactType(405 , message.getExtension(defaultSint32ExtensionLite )); - assertEqualsExactType(406L , message.getExtension(defaultSint64ExtensionLite )); - assertEqualsExactType(407 , message.getExtension(defaultFixed32ExtensionLite )); - assertEqualsExactType(408L , message.getExtension(defaultFixed64ExtensionLite )); - assertEqualsExactType(409 , message.getExtension(defaultSfixed32ExtensionLite)); - assertEqualsExactType(410L , message.getExtension(defaultSfixed64ExtensionLite)); - assertEqualsExactType(411F , message.getExtension(defaultFloatExtensionLite )); - assertEqualsExactType(412D , message.getExtension(defaultDoubleExtensionLite )); - assertEqualsExactType(false, message.getExtension(defaultBoolExtensionLite )); - assertEqualsExactType("415", message.getExtension(defaultStringExtensionLite )); + assertEqualsExactType(401, message.getExtension(defaultInt32ExtensionLite)); + assertEqualsExactType(402L, message.getExtension(defaultInt64ExtensionLite)); + assertEqualsExactType(403, message.getExtension(defaultUint32ExtensionLite)); + assertEqualsExactType(404L, message.getExtension(defaultUint64ExtensionLite)); + assertEqualsExactType(405, message.getExtension(defaultSint32ExtensionLite)); + assertEqualsExactType(406L, message.getExtension(defaultSint64ExtensionLite)); + assertEqualsExactType(407, message.getExtension(defaultFixed32ExtensionLite)); + assertEqualsExactType(408L, message.getExtension(defaultFixed64ExtensionLite)); + assertEqualsExactType(409, message.getExtension(defaultSfixed32ExtensionLite)); + assertEqualsExactType(410L, message.getExtension(defaultSfixed64ExtensionLite)); + assertEqualsExactType(411F, message.getExtension(defaultFloatExtensionLite)); + assertEqualsExactType(412D, message.getExtension(defaultDoubleExtensionLite)); + assertEqualsExactType(false, message.getExtension(defaultBoolExtensionLite)); + assertEqualsExactType("415", message.getExtension(defaultStringExtensionLite)); assertEqualsExactType(toBytes("416"), message.getExtension(defaultBytesExtensionLite)); - assertEqualsExactType(TestAllTypesLite.NestedEnum.FOO, - message.getExtension(defaultNestedEnumExtensionLite )); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_FOO, - message.getExtension(defaultForeignEnumExtensionLite)); - assertEqualsExactType(ImportEnumLite.IMPORT_LITE_FOO, - message.getExtension(defaultImportEnumExtensionLite)); + assertEqualsExactType( + TestAllTypesLite.NestedEnum.FOO, message.getExtension(defaultNestedEnumExtensionLite)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_FOO, message.getExtension(defaultForeignEnumExtensionLite)); + assertEqualsExactType( + ImportEnumLite.IMPORT_LITE_FOO, message.getExtension(defaultImportEnumExtensionLite)); assertEqualsExactType("424", message.getExtension(defaultStringPieceExtensionLite)); assertEqualsExactType("425", message.getExtension(defaultCordExtensionLite)); @@ -2244,163 +2217,160 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all extensions of - * {@code message} are cleared, and that getting the extensions returns their - * default values. + * Assert (using {@code junit.framework.Assert}} that all extensions of {@code message} are + * cleared, and that getting the extensions returns their default values. */ - public static void assertExtensionsClear( - TestAllExtensionsLiteOrBuilder message) { + public static void assertExtensionsClear(TestAllExtensionsLiteOrBuilder message) { // hasBlah() should initially be false for all optional fields. - Assert.assertFalse(message.hasExtension(optionalInt32ExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalInt64ExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalUint32ExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalUint64ExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalSint32ExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalSint64ExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalFixed32ExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalFixed64ExtensionLite )); + Assert.assertFalse(message.hasExtension(optionalInt32ExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalInt64ExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalUint32ExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalUint64ExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalSint32ExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalSint64ExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalFixed32ExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalFixed64ExtensionLite)); Assert.assertFalse(message.hasExtension(optionalSfixed32ExtensionLite)); Assert.assertFalse(message.hasExtension(optionalSfixed64ExtensionLite)); - Assert.assertFalse(message.hasExtension(optionalFloatExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalDoubleExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalBoolExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalStringExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalBytesExtensionLite )); - - Assert.assertFalse(message.hasExtension(optionalGroupExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalNestedMessageExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalForeignMessageExtensionLite )); - Assert.assertFalse(message.hasExtension(optionalImportMessageExtensionLite )); + Assert.assertFalse(message.hasExtension(optionalFloatExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalDoubleExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalBoolExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalStringExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalBytesExtensionLite)); + + Assert.assertFalse(message.hasExtension(optionalGroupExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalNestedMessageExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalForeignMessageExtensionLite)); + Assert.assertFalse(message.hasExtension(optionalImportMessageExtensionLite)); Assert.assertFalse(message.hasExtension(optionalPublicImportMessageExtensionLite)); - Assert.assertFalse(message.hasExtension(optionalLazyMessageExtensionLite )); + Assert.assertFalse(message.hasExtension(optionalLazyMessageExtensionLite)); - Assert.assertFalse(message.hasExtension(optionalNestedEnumExtensionLite )); + Assert.assertFalse(message.hasExtension(optionalNestedEnumExtensionLite)); Assert.assertFalse(message.hasExtension(optionalForeignEnumExtensionLite)); - Assert.assertFalse(message.hasExtension(optionalImportEnumExtensionLite )); + Assert.assertFalse(message.hasExtension(optionalImportEnumExtensionLite)); Assert.assertFalse(message.hasExtension(optionalStringPieceExtensionLite)); Assert.assertFalse(message.hasExtension(optionalCordExtensionLite)); // Optional fields without defaults are set to zero or something like it. - assertEqualsExactType(0 , message.getExtension(optionalInt32ExtensionLite )); - assertEqualsExactType(0L , message.getExtension(optionalInt64ExtensionLite )); - assertEqualsExactType(0 , message.getExtension(optionalUint32ExtensionLite )); - assertEqualsExactType(0L , message.getExtension(optionalUint64ExtensionLite )); - assertEqualsExactType(0 , message.getExtension(optionalSint32ExtensionLite )); - assertEqualsExactType(0L , message.getExtension(optionalSint64ExtensionLite )); - assertEqualsExactType(0 , message.getExtension(optionalFixed32ExtensionLite )); - assertEqualsExactType(0L , message.getExtension(optionalFixed64ExtensionLite )); - assertEqualsExactType(0 , message.getExtension(optionalSfixed32ExtensionLite)); - assertEqualsExactType(0L , message.getExtension(optionalSfixed64ExtensionLite)); - assertEqualsExactType(0F , message.getExtension(optionalFloatExtensionLite )); - assertEqualsExactType(0D , message.getExtension(optionalDoubleExtensionLite )); - assertEqualsExactType(false, message.getExtension(optionalBoolExtensionLite )); - assertEqualsExactType("" , message.getExtension(optionalStringExtensionLite )); + assertEqualsExactType(0, message.getExtension(optionalInt32ExtensionLite)); + assertEqualsExactType(0L, message.getExtension(optionalInt64ExtensionLite)); + assertEqualsExactType(0, message.getExtension(optionalUint32ExtensionLite)); + assertEqualsExactType(0L, message.getExtension(optionalUint64ExtensionLite)); + assertEqualsExactType(0, message.getExtension(optionalSint32ExtensionLite)); + assertEqualsExactType(0L, message.getExtension(optionalSint64ExtensionLite)); + assertEqualsExactType(0, message.getExtension(optionalFixed32ExtensionLite)); + assertEqualsExactType(0L, message.getExtension(optionalFixed64ExtensionLite)); + assertEqualsExactType(0, message.getExtension(optionalSfixed32ExtensionLite)); + assertEqualsExactType(0L, message.getExtension(optionalSfixed64ExtensionLite)); + assertEqualsExactType(0F, message.getExtension(optionalFloatExtensionLite)); + assertEqualsExactType(0D, message.getExtension(optionalDoubleExtensionLite)); + assertEqualsExactType(false, message.getExtension(optionalBoolExtensionLite)); + assertEqualsExactType("", message.getExtension(optionalStringExtensionLite)); assertEqualsExactType(ByteString.EMPTY, message.getExtension(optionalBytesExtensionLite)); // Embedded messages should also be clear. - Assert.assertFalse(message.getExtension(optionalGroupExtensionLite ).hasA()); - Assert.assertFalse(message.getExtension(optionalNestedMessageExtensionLite ).hasBb()); - Assert.assertFalse(message.getExtension(optionalForeignMessageExtensionLite ).hasC()); - Assert.assertFalse(message.getExtension(optionalImportMessageExtensionLite ).hasD()); + Assert.assertFalse(message.getExtension(optionalGroupExtensionLite).hasA()); + Assert.assertFalse(message.getExtension(optionalNestedMessageExtensionLite).hasBb()); + Assert.assertFalse(message.getExtension(optionalForeignMessageExtensionLite).hasC()); + Assert.assertFalse(message.getExtension(optionalImportMessageExtensionLite).hasD()); Assert.assertFalse(message.getExtension(optionalPublicImportMessageExtensionLite).hasE()); - Assert.assertFalse(message.getExtension(optionalLazyMessageExtensionLite ).hasBb()); + Assert.assertFalse(message.getExtension(optionalLazyMessageExtensionLite).hasBb()); - assertEqualsExactType(0, message.getExtension(optionalGroupExtensionLite ).getA()); - assertEqualsExactType(0, message.getExtension(optionalNestedMessageExtensionLite ).getBb()); + assertEqualsExactType(0, message.getExtension(optionalGroupExtensionLite).getA()); + assertEqualsExactType(0, message.getExtension(optionalNestedMessageExtensionLite).getBb()); assertEqualsExactType(0, message.getExtension(optionalForeignMessageExtensionLite).getC()); - assertEqualsExactType(0, message.getExtension(optionalImportMessageExtensionLite ).getD()); - assertEqualsExactType(0, message.getExtension( - optionalPublicImportMessageExtensionLite).getE()); - assertEqualsExactType(0, message.getExtension(optionalLazyMessageExtensionLite ).getBb()); + assertEqualsExactType(0, message.getExtension(optionalImportMessageExtensionLite).getD()); + assertEqualsExactType(0, message.getExtension(optionalPublicImportMessageExtensionLite).getE()); + assertEqualsExactType(0, message.getExtension(optionalLazyMessageExtensionLite).getBb()); // Enums without defaults are set to the first value in the enum. - assertEqualsExactType(TestAllTypesLite.NestedEnum.FOO, - message.getExtension(optionalNestedEnumExtensionLite )); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_FOO, - message.getExtension(optionalForeignEnumExtensionLite)); - assertEqualsExactType(ImportEnumLite.IMPORT_LITE_FOO, - message.getExtension(optionalImportEnumExtensionLite)); + assertEqualsExactType( + TestAllTypesLite.NestedEnum.FOO, message.getExtension(optionalNestedEnumExtensionLite)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_FOO, message.getExtension(optionalForeignEnumExtensionLite)); + assertEqualsExactType( + ImportEnumLite.IMPORT_LITE_FOO, message.getExtension(optionalImportEnumExtensionLite)); assertEqualsExactType("", message.getExtension(optionalStringPieceExtensionLite)); assertEqualsExactType("", message.getExtension(optionalCordExtensionLite)); // Repeated fields are empty. - Assert.assertEquals(0, message.getExtensionCount(repeatedInt32ExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedInt64ExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedUint32ExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedUint64ExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedSint32ExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedSint64ExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedFixed32ExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedFixed64ExtensionLite )); + Assert.assertEquals(0, message.getExtensionCount(repeatedInt32ExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedInt64ExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedUint32ExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedUint64ExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedSint32ExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedSint64ExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedFixed32ExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedFixed64ExtensionLite)); Assert.assertEquals(0, message.getExtensionCount(repeatedSfixed32ExtensionLite)); Assert.assertEquals(0, message.getExtensionCount(repeatedSfixed64ExtensionLite)); - Assert.assertEquals(0, message.getExtensionCount(repeatedFloatExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedDoubleExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedBoolExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedStringExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedBytesExtensionLite )); - - Assert.assertEquals(0, message.getExtensionCount(repeatedGroupExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedNestedMessageExtensionLite )); + Assert.assertEquals(0, message.getExtensionCount(repeatedFloatExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedDoubleExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedBoolExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedStringExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedBytesExtensionLite)); + + Assert.assertEquals(0, message.getExtensionCount(repeatedGroupExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedNestedMessageExtensionLite)); Assert.assertEquals(0, message.getExtensionCount(repeatedForeignMessageExtensionLite)); - Assert.assertEquals(0, message.getExtensionCount(repeatedImportMessageExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedLazyMessageExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedNestedEnumExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedForeignEnumExtensionLite )); - Assert.assertEquals(0, message.getExtensionCount(repeatedImportEnumExtensionLite )); + Assert.assertEquals(0, message.getExtensionCount(repeatedImportMessageExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedLazyMessageExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedNestedEnumExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedForeignEnumExtensionLite)); + Assert.assertEquals(0, message.getExtensionCount(repeatedImportEnumExtensionLite)); Assert.assertEquals(0, message.getExtensionCount(repeatedStringPieceExtensionLite)); Assert.assertEquals(0, message.getExtensionCount(repeatedCordExtensionLite)); // hasBlah() should also be false for all default fields. - Assert.assertFalse(message.hasExtension(defaultInt32ExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultInt64ExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultUint32ExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultUint64ExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultSint32ExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultSint64ExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultFixed32ExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultFixed64ExtensionLite )); + Assert.assertFalse(message.hasExtension(defaultInt32ExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultInt64ExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultUint32ExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultUint64ExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultSint32ExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultSint64ExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultFixed32ExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultFixed64ExtensionLite)); Assert.assertFalse(message.hasExtension(defaultSfixed32ExtensionLite)); Assert.assertFalse(message.hasExtension(defaultSfixed64ExtensionLite)); - Assert.assertFalse(message.hasExtension(defaultFloatExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultDoubleExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultBoolExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultStringExtensionLite )); - Assert.assertFalse(message.hasExtension(defaultBytesExtensionLite )); + Assert.assertFalse(message.hasExtension(defaultFloatExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultDoubleExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultBoolExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultStringExtensionLite)); + Assert.assertFalse(message.hasExtension(defaultBytesExtensionLite)); - Assert.assertFalse(message.hasExtension(defaultNestedEnumExtensionLite )); + Assert.assertFalse(message.hasExtension(defaultNestedEnumExtensionLite)); Assert.assertFalse(message.hasExtension(defaultForeignEnumExtensionLite)); - Assert.assertFalse(message.hasExtension(defaultImportEnumExtensionLite )); + Assert.assertFalse(message.hasExtension(defaultImportEnumExtensionLite)); Assert.assertFalse(message.hasExtension(defaultStringPieceExtensionLite)); Assert.assertFalse(message.hasExtension(defaultCordExtensionLite)); // Fields with defaults have their default values (duh). - assertEqualsExactType( 41 , message.getExtension(defaultInt32ExtensionLite )); - assertEqualsExactType( 42L , message.getExtension(defaultInt64ExtensionLite )); - assertEqualsExactType( 43 , message.getExtension(defaultUint32ExtensionLite )); - assertEqualsExactType( 44L , message.getExtension(defaultUint64ExtensionLite )); - assertEqualsExactType(-45 , message.getExtension(defaultSint32ExtensionLite )); - assertEqualsExactType( 46L , message.getExtension(defaultSint64ExtensionLite )); - assertEqualsExactType( 47 , message.getExtension(defaultFixed32ExtensionLite )); - assertEqualsExactType( 48L , message.getExtension(defaultFixed64ExtensionLite )); - assertEqualsExactType( 49 , message.getExtension(defaultSfixed32ExtensionLite)); - assertEqualsExactType(-50L , message.getExtension(defaultSfixed64ExtensionLite)); - assertEqualsExactType( 51.5F , message.getExtension(defaultFloatExtensionLite )); - assertEqualsExactType( 52e3D , message.getExtension(defaultDoubleExtensionLite )); - assertEqualsExactType(true , message.getExtension(defaultBoolExtensionLite )); - assertEqualsExactType("hello", message.getExtension(defaultStringExtensionLite )); + assertEqualsExactType(41, message.getExtension(defaultInt32ExtensionLite)); + assertEqualsExactType(42L, message.getExtension(defaultInt64ExtensionLite)); + assertEqualsExactType(43, message.getExtension(defaultUint32ExtensionLite)); + assertEqualsExactType(44L, message.getExtension(defaultUint64ExtensionLite)); + assertEqualsExactType(-45, message.getExtension(defaultSint32ExtensionLite)); + assertEqualsExactType(46L, message.getExtension(defaultSint64ExtensionLite)); + assertEqualsExactType(47, message.getExtension(defaultFixed32ExtensionLite)); + assertEqualsExactType(48L, message.getExtension(defaultFixed64ExtensionLite)); + assertEqualsExactType(49, message.getExtension(defaultSfixed32ExtensionLite)); + assertEqualsExactType(-50L, message.getExtension(defaultSfixed64ExtensionLite)); + assertEqualsExactType(51.5F, message.getExtension(defaultFloatExtensionLite)); + assertEqualsExactType(52e3D, message.getExtension(defaultDoubleExtensionLite)); + assertEqualsExactType(true, message.getExtension(defaultBoolExtensionLite)); + assertEqualsExactType("hello", message.getExtension(defaultStringExtensionLite)); assertEqualsExactType(toBytes("world"), message.getExtension(defaultBytesExtensionLite)); - assertEqualsExactType(TestAllTypesLite.NestedEnum.BAR, - message.getExtension(defaultNestedEnumExtensionLite )); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAR, - message.getExtension(defaultForeignEnumExtensionLite)); - assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAR, - message.getExtension(defaultImportEnumExtensionLite)); + assertEqualsExactType( + TestAllTypesLite.NestedEnum.BAR, message.getExtension(defaultNestedEnumExtensionLite)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_BAR, message.getExtension(defaultForeignEnumExtensionLite)); + assertEqualsExactType( + ImportEnumLite.IMPORT_LITE_BAR, message.getExtension(defaultImportEnumExtensionLite)); assertEqualsExactType("abc", message.getExtension(defaultStringPieceExtensionLite)); assertEqualsExactType("123", message.getExtension(defaultCordExtensionLite)); @@ -2414,200 +2384,229 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all extensions of - * {@code message} are set to the values assigned by {@code setAllExtensions} - * followed by {@code modifyRepeatedExtensions}. + * Assert (using {@code junit.framework.Assert}} that all extensions of {@code message} are set to + * the values assigned by {@code setAllExtensions} followed by {@code modifyRepeatedExtensions}. */ - public static void assertRepeatedExtensionsModified( - TestAllExtensionsLiteOrBuilder message) { + public static void assertRepeatedExtensionsModified(TestAllExtensionsLiteOrBuilder message) { // ModifyRepeatedFields only sets the second repeated element of each // field. In addition to verifying this, we also verify that the first // element and size were *not* modified. - Assert.assertEquals(2, message.getExtensionCount(repeatedInt32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedInt64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedUint32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedUint64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedSint32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedSint64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64ExtensionLite )); + Assert.assertEquals(2, message.getExtensionCount(repeatedInt32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedInt64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedUint32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedUint64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedSint32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedSint64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64ExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed32ExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed64ExtensionLite)); - Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtensionLite )); - - Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtensionLite )); + Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtensionLite)); + + Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedForeignMessageExtensionLite)); - Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedLazyMessageExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtensionLite )); + Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedLazyMessageExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedStringPieceExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(repeatedCordExtensionLite)); - assertEqualsExactType(201 , message.getExtension(repeatedInt32ExtensionLite , 0)); - assertEqualsExactType(202L , message.getExtension(repeatedInt64ExtensionLite , 0)); - assertEqualsExactType(203 , message.getExtension(repeatedUint32ExtensionLite , 0)); - assertEqualsExactType(204L , message.getExtension(repeatedUint64ExtensionLite , 0)); - assertEqualsExactType(205 , message.getExtension(repeatedSint32ExtensionLite , 0)); - assertEqualsExactType(206L , message.getExtension(repeatedSint64ExtensionLite , 0)); - assertEqualsExactType(207 , message.getExtension(repeatedFixed32ExtensionLite , 0)); - assertEqualsExactType(208L , message.getExtension(repeatedFixed64ExtensionLite , 0)); - assertEqualsExactType(209 , message.getExtension(repeatedSfixed32ExtensionLite, 0)); - assertEqualsExactType(210L , message.getExtension(repeatedSfixed64ExtensionLite, 0)); - assertEqualsExactType(211F , message.getExtension(repeatedFloatExtensionLite , 0)); - assertEqualsExactType(212D , message.getExtension(repeatedDoubleExtensionLite , 0)); - assertEqualsExactType(true , message.getExtension(repeatedBoolExtensionLite , 0)); - assertEqualsExactType("215", message.getExtension(repeatedStringExtensionLite , 0)); + assertEqualsExactType(201, message.getExtension(repeatedInt32ExtensionLite, 0)); + assertEqualsExactType(202L, message.getExtension(repeatedInt64ExtensionLite, 0)); + assertEqualsExactType(203, message.getExtension(repeatedUint32ExtensionLite, 0)); + assertEqualsExactType(204L, message.getExtension(repeatedUint64ExtensionLite, 0)); + assertEqualsExactType(205, message.getExtension(repeatedSint32ExtensionLite, 0)); + assertEqualsExactType(206L, message.getExtension(repeatedSint64ExtensionLite, 0)); + assertEqualsExactType(207, message.getExtension(repeatedFixed32ExtensionLite, 0)); + assertEqualsExactType(208L, message.getExtension(repeatedFixed64ExtensionLite, 0)); + assertEqualsExactType(209, message.getExtension(repeatedSfixed32ExtensionLite, 0)); + assertEqualsExactType(210L, message.getExtension(repeatedSfixed64ExtensionLite, 0)); + assertEqualsExactType(211F, message.getExtension(repeatedFloatExtensionLite, 0)); + assertEqualsExactType(212D, message.getExtension(repeatedDoubleExtensionLite, 0)); + assertEqualsExactType(true, message.getExtension(repeatedBoolExtensionLite, 0)); + assertEqualsExactType("215", message.getExtension(repeatedStringExtensionLite, 0)); assertEqualsExactType(toBytes("216"), message.getExtension(repeatedBytesExtensionLite, 0)); - assertEqualsExactType(217, message.getExtension(repeatedGroupExtensionLite ,0).getA()); - assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtensionLite ,0).getBb()); - assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtensionLite,0).getC()); - assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtensionLite ,0).getD()); - assertEqualsExactType(227, message.getExtension(repeatedLazyMessageExtensionLite ,0).getBb()); + assertEqualsExactType(217, message.getExtension(repeatedGroupExtensionLite, 0).getA()); + assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtensionLite, 0).getBb()); + assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtensionLite, 0).getC()); + assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtensionLite, 0).getD()); + assertEqualsExactType(227, message.getExtension(repeatedLazyMessageExtensionLite, 0).getBb()); - assertEqualsExactType(TestAllTypesLite.NestedEnum.BAR, - message.getExtension(repeatedNestedEnumExtensionLite, 0)); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAR, - message.getExtension(repeatedForeignEnumExtensionLite, 0)); - assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAR, - message.getExtension(repeatedImportEnumExtensionLite, 0)); + assertEqualsExactType( + TestAllTypesLite.NestedEnum.BAR, message.getExtension(repeatedNestedEnumExtensionLite, 0)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_BAR, + message.getExtension(repeatedForeignEnumExtensionLite, 0)); + assertEqualsExactType( + ImportEnumLite.IMPORT_LITE_BAR, message.getExtension(repeatedImportEnumExtensionLite, 0)); assertEqualsExactType("224", message.getExtension(repeatedStringPieceExtensionLite, 0)); assertEqualsExactType("225", message.getExtension(repeatedCordExtensionLite, 0)); // Actually verify the second (modified) elements now. - assertEqualsExactType(501 , message.getExtension(repeatedInt32ExtensionLite , 1)); - assertEqualsExactType(502L , message.getExtension(repeatedInt64ExtensionLite , 1)); - assertEqualsExactType(503 , message.getExtension(repeatedUint32ExtensionLite , 1)); - assertEqualsExactType(504L , message.getExtension(repeatedUint64ExtensionLite , 1)); - assertEqualsExactType(505 , message.getExtension(repeatedSint32ExtensionLite , 1)); - assertEqualsExactType(506L , message.getExtension(repeatedSint64ExtensionLite , 1)); - assertEqualsExactType(507 , message.getExtension(repeatedFixed32ExtensionLite , 1)); - assertEqualsExactType(508L , message.getExtension(repeatedFixed64ExtensionLite , 1)); - assertEqualsExactType(509 , message.getExtension(repeatedSfixed32ExtensionLite, 1)); - assertEqualsExactType(510L , message.getExtension(repeatedSfixed64ExtensionLite, 1)); - assertEqualsExactType(511F , message.getExtension(repeatedFloatExtensionLite , 1)); - assertEqualsExactType(512D , message.getExtension(repeatedDoubleExtensionLite , 1)); - assertEqualsExactType(true , message.getExtension(repeatedBoolExtensionLite , 1)); - assertEqualsExactType("515", message.getExtension(repeatedStringExtensionLite , 1)); + assertEqualsExactType(501, message.getExtension(repeatedInt32ExtensionLite, 1)); + assertEqualsExactType(502L, message.getExtension(repeatedInt64ExtensionLite, 1)); + assertEqualsExactType(503, message.getExtension(repeatedUint32ExtensionLite, 1)); + assertEqualsExactType(504L, message.getExtension(repeatedUint64ExtensionLite, 1)); + assertEqualsExactType(505, message.getExtension(repeatedSint32ExtensionLite, 1)); + assertEqualsExactType(506L, message.getExtension(repeatedSint64ExtensionLite, 1)); + assertEqualsExactType(507, message.getExtension(repeatedFixed32ExtensionLite, 1)); + assertEqualsExactType(508L, message.getExtension(repeatedFixed64ExtensionLite, 1)); + assertEqualsExactType(509, message.getExtension(repeatedSfixed32ExtensionLite, 1)); + assertEqualsExactType(510L, message.getExtension(repeatedSfixed64ExtensionLite, 1)); + assertEqualsExactType(511F, message.getExtension(repeatedFloatExtensionLite, 1)); + assertEqualsExactType(512D, message.getExtension(repeatedDoubleExtensionLite, 1)); + assertEqualsExactType(true, message.getExtension(repeatedBoolExtensionLite, 1)); + assertEqualsExactType("515", message.getExtension(repeatedStringExtensionLite, 1)); assertEqualsExactType(toBytes("516"), message.getExtension(repeatedBytesExtensionLite, 1)); - assertEqualsExactType(517, message.getExtension(repeatedGroupExtensionLite ,1).getA()); - assertEqualsExactType(518, message.getExtension(repeatedNestedMessageExtensionLite ,1).getBb()); - assertEqualsExactType(519, message.getExtension(repeatedForeignMessageExtensionLite,1).getC()); - assertEqualsExactType(520, message.getExtension(repeatedImportMessageExtensionLite ,1).getD()); - assertEqualsExactType(527, message.getExtension(repeatedLazyMessageExtensionLite ,1).getBb()); + assertEqualsExactType(517, message.getExtension(repeatedGroupExtensionLite, 1).getA()); + assertEqualsExactType(518, message.getExtension(repeatedNestedMessageExtensionLite, 1).getBb()); + assertEqualsExactType(519, message.getExtension(repeatedForeignMessageExtensionLite, 1).getC()); + assertEqualsExactType(520, message.getExtension(repeatedImportMessageExtensionLite, 1).getD()); + assertEqualsExactType(527, message.getExtension(repeatedLazyMessageExtensionLite, 1).getBb()); - assertEqualsExactType(TestAllTypesLite.NestedEnum.FOO, - message.getExtension(repeatedNestedEnumExtensionLite, 1)); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_FOO, - message.getExtension(repeatedForeignEnumExtensionLite, 1)); - assertEqualsExactType(ImportEnumLite.IMPORT_LITE_FOO, - message.getExtension(repeatedImportEnumExtensionLite, 1)); + assertEqualsExactType( + TestAllTypesLite.NestedEnum.FOO, message.getExtension(repeatedNestedEnumExtensionLite, 1)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_FOO, + message.getExtension(repeatedForeignEnumExtensionLite, 1)); + assertEqualsExactType( + ImportEnumLite.IMPORT_LITE_FOO, message.getExtension(repeatedImportEnumExtensionLite, 1)); assertEqualsExactType("524", message.getExtension(repeatedStringPieceExtensionLite, 1)); assertEqualsExactType("525", message.getExtension(repeatedCordExtensionLite, 1)); } public static void assertPackedExtensionsSet(TestPackedExtensionsLite message) { - Assert.assertEquals(2, message.getExtensionCount(packedInt32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedInt64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedUint32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedUint64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedSint32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedSint64ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedFixed32ExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedFixed64ExtensionLite )); + Assert.assertEquals(2, message.getExtensionCount(packedInt32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedInt64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedUint32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedUint64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedSint32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedSint64ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedFixed32ExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedFixed64ExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(packedSfixed32ExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(packedSfixed64ExtensionLite)); - Assert.assertEquals(2, message.getExtensionCount(packedFloatExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedDoubleExtensionLite )); - Assert.assertEquals(2, message.getExtensionCount(packedBoolExtensionLite )); + Assert.assertEquals(2, message.getExtensionCount(packedFloatExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedDoubleExtensionLite)); + Assert.assertEquals(2, message.getExtensionCount(packedBoolExtensionLite)); Assert.assertEquals(2, message.getExtensionCount(packedEnumExtensionLite)); - assertEqualsExactType(601 , message.getExtension(packedInt32ExtensionLite , 0)); - assertEqualsExactType(602L , message.getExtension(packedInt64ExtensionLite , 0)); - assertEqualsExactType(603 , message.getExtension(packedUint32ExtensionLite , 0)); - assertEqualsExactType(604L , message.getExtension(packedUint64ExtensionLite , 0)); - assertEqualsExactType(605 , message.getExtension(packedSint32ExtensionLite , 0)); - assertEqualsExactType(606L , message.getExtension(packedSint64ExtensionLite , 0)); - assertEqualsExactType(607 , message.getExtension(packedFixed32ExtensionLite , 0)); - assertEqualsExactType(608L , message.getExtension(packedFixed64ExtensionLite , 0)); - assertEqualsExactType(609 , message.getExtension(packedSfixed32ExtensionLite, 0)); - assertEqualsExactType(610L , message.getExtension(packedSfixed64ExtensionLite, 0)); - assertEqualsExactType(611F , message.getExtension(packedFloatExtensionLite , 0)); - assertEqualsExactType(612D , message.getExtension(packedDoubleExtensionLite , 0)); - assertEqualsExactType(true , message.getExtension(packedBoolExtensionLite , 0)); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAR, - message.getExtension(packedEnumExtensionLite, 0)); - assertEqualsExactType(701 , message.getExtension(packedInt32ExtensionLite , 1)); - assertEqualsExactType(702L , message.getExtension(packedInt64ExtensionLite , 1)); - assertEqualsExactType(703 , message.getExtension(packedUint32ExtensionLite , 1)); - assertEqualsExactType(704L , message.getExtension(packedUint64ExtensionLite , 1)); - assertEqualsExactType(705 , message.getExtension(packedSint32ExtensionLite , 1)); - assertEqualsExactType(706L , message.getExtension(packedSint64ExtensionLite , 1)); - assertEqualsExactType(707 , message.getExtension(packedFixed32ExtensionLite , 1)); - assertEqualsExactType(708L , message.getExtension(packedFixed64ExtensionLite , 1)); - assertEqualsExactType(709 , message.getExtension(packedSfixed32ExtensionLite, 1)); - assertEqualsExactType(710L , message.getExtension(packedSfixed64ExtensionLite, 1)); - assertEqualsExactType(711F , message.getExtension(packedFloatExtensionLite , 1)); - assertEqualsExactType(712D , message.getExtension(packedDoubleExtensionLite , 1)); - assertEqualsExactType(false, message.getExtension(packedBoolExtensionLite , 1)); - assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAZ, - message.getExtension(packedEnumExtensionLite, 1)); + assertEqualsExactType(601, message.getExtension(packedInt32ExtensionLite, 0)); + assertEqualsExactType(602L, message.getExtension(packedInt64ExtensionLite, 0)); + assertEqualsExactType(603, message.getExtension(packedUint32ExtensionLite, 0)); + assertEqualsExactType(604L, message.getExtension(packedUint64ExtensionLite, 0)); + assertEqualsExactType(605, message.getExtension(packedSint32ExtensionLite, 0)); + assertEqualsExactType(606L, message.getExtension(packedSint64ExtensionLite, 0)); + assertEqualsExactType(607, message.getExtension(packedFixed32ExtensionLite, 0)); + assertEqualsExactType(608L, message.getExtension(packedFixed64ExtensionLite, 0)); + assertEqualsExactType(609, message.getExtension(packedSfixed32ExtensionLite, 0)); + assertEqualsExactType(610L, message.getExtension(packedSfixed64ExtensionLite, 0)); + assertEqualsExactType(611F, message.getExtension(packedFloatExtensionLite, 0)); + assertEqualsExactType(612D, message.getExtension(packedDoubleExtensionLite, 0)); + assertEqualsExactType(true, message.getExtension(packedBoolExtensionLite, 0)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_BAR, message.getExtension(packedEnumExtensionLite, 0)); + assertEqualsExactType(701, message.getExtension(packedInt32ExtensionLite, 1)); + assertEqualsExactType(702L, message.getExtension(packedInt64ExtensionLite, 1)); + assertEqualsExactType(703, message.getExtension(packedUint32ExtensionLite, 1)); + assertEqualsExactType(704L, message.getExtension(packedUint64ExtensionLite, 1)); + assertEqualsExactType(705, message.getExtension(packedSint32ExtensionLite, 1)); + assertEqualsExactType(706L, message.getExtension(packedSint64ExtensionLite, 1)); + assertEqualsExactType(707, message.getExtension(packedFixed32ExtensionLite, 1)); + assertEqualsExactType(708L, message.getExtension(packedFixed64ExtensionLite, 1)); + assertEqualsExactType(709, message.getExtension(packedSfixed32ExtensionLite, 1)); + assertEqualsExactType(710L, message.getExtension(packedSfixed64ExtensionLite, 1)); + assertEqualsExactType(711F, message.getExtension(packedFloatExtensionLite, 1)); + assertEqualsExactType(712D, message.getExtension(packedDoubleExtensionLite, 1)); + assertEqualsExactType(false, message.getExtension(packedBoolExtensionLite, 1)); + assertEqualsExactType( + ForeignEnumLite.FOREIGN_LITE_BAZ, message.getExtension(packedEnumExtensionLite, 1)); } // =================================================================== // oneof public static void setOneof(TestOneof2.Builder message) { - message.setFooLazyMessage( - TestOneof2.NestedMessage.newBuilder().setQuxInt(100).build()); + message.setFooLazyMessage(TestOneof2.NestedMessage.newBuilder().setQuxInt(100).build()); message.setBarString("101"); message.setBazInt(102); message.setBazString("103"); } public static void assertOneofSet(TestOneof2 message) { - Assert.assertTrue(message.hasFooLazyMessage ()); + Assert.assertTrue(message.hasFooLazyMessage()); Assert.assertTrue(message.getFooLazyMessage().hasQuxInt()); Assert.assertTrue(message.hasBarString()); - Assert.assertTrue(message.hasBazInt ()); + Assert.assertTrue(message.hasBazInt()); Assert.assertTrue(message.hasBazString()); - Assert.assertEquals(100 , message.getFooLazyMessage().getQuxInt()); - Assert.assertEquals("101", message.getBarString ()); - Assert.assertEquals(102 , message.getBazInt ()); - Assert.assertEquals("103", message.getBazString ()); + Assert.assertEquals(100, message.getFooLazyMessage().getQuxInt()); + Assert.assertEquals("101", message.getBarString()); + Assert.assertEquals(102, message.getBazInt()); + Assert.assertEquals("103", message.getBazString()); } public static void assertAtMostOneFieldSetOneof(TestOneof2 message) { int count = 0; - if (message.hasFooInt()) { ++count; } - if (message.hasFooString()) { ++count; } - if (message.hasFooCord()) { ++count; } - if (message.hasFooStringPiece()) { ++count; } - if (message.hasFooBytes()) { ++count; } - if (message.hasFooEnum()) { ++count; } - if (message.hasFooMessage()) { ++count; } - if (message.hasFooGroup()) { ++count; } - if (message.hasFooLazyMessage()) { ++count; } + if (message.hasFooInt()) { + ++count; + } + if (message.hasFooString()) { + ++count; + } + if (message.hasFooCord()) { + ++count; + } + if (message.hasFooStringPiece()) { + ++count; + } + if (message.hasFooBytes()) { + ++count; + } + if (message.hasFooEnum()) { + ++count; + } + if (message.hasFooMessage()) { + ++count; + } + if (message.hasFooGroup()) { + ++count; + } + if (message.hasFooLazyMessage()) { + ++count; + } Assert.assertTrue(count <= 1); count = 0; - if (message.hasBarInt()) { ++count; } - if (message.hasBarString()) { ++count; } - if (message.hasBarCord()) { ++count; } - if (message.hasBarStringPiece()) { ++count; } - if (message.hasBarBytes()) { ++count; } - if (message.hasBarEnum()) { ++count; } + if (message.hasBarInt()) { + ++count; + } + if (message.hasBarString()) { + ++count; + } + if (message.hasBarCord()) { + ++count; + } + if (message.hasBarStringPiece()) { + ++count; + } + if (message.hasBarBytes()) { + ++count; + } + if (message.hasBarEnum()) { + ++count; + } Assert.assertTrue(count <= 1); switch (message.getFooCase()) { @@ -2640,7 +2639,6 @@ public final class TestUtil { break; case FOO_NOT_SET: break; - default: // TODO(b/18683919): go/enum-switch-lsc } } @@ -2649,9 +2647,8 @@ public final class TestUtil { // BEGIN FULL-RUNTIME /** - * Performs the same things that the methods of {@code TestUtil} do, but - * via the reflection interface. This is its own class because it needs - * to know what descriptor to use. + * Performs the same things that the methods of {@code TestUtil} do, but via the reflection + * interface. This is its own class because it needs to know what descriptor to use. */ public static class ReflectionTester { private final Descriptors.Descriptor baseDescriptor; @@ -2690,20 +2687,16 @@ public final class TestUtil { private final Descriptors.EnumValueDescriptor importBaz; /** - * Construct a {@code ReflectionTester} that will expect messages using - * the given descriptor. + * Construct a {@code ReflectionTester} that will expect messages using the given descriptor. * - * Normally {@code baseDescriptor} should be a descriptor for the type - * {@code TestAllTypes}, defined in - * {@code google/protobuf/unittest.proto}. However, if - * {@code extensionRegistry} is non-null, then {@code baseDescriptor} should - * be for {@code TestAllExtensions} instead, and instead of reading and - * writing normal fields, the tester will read and write extensions. - * All of {@code TestAllExtensions}' extensions must be registered in the - * registry. + *

Normally {@code baseDescriptor} should be a descriptor for the type {@code TestAllTypes}, + * defined in {@code google/protobuf/unittest.proto}. However, if {@code extensionRegistry} + * is non-null, then {@code baseDescriptor} should be for {@code TestAllExtensions} instead, and + * instead of reading and writing normal fields, the tester will read and write extensions. All + * of {@code TestAllExtensions}' extensions must be registered in the registry. */ - public ReflectionTester(Descriptors.Descriptor baseDescriptor, - ExtensionRegistry extensionRegistry) { + public ReflectionTester( + Descriptors.Descriptor baseDescriptor, ExtensionRegistry extensionRegistry) { this.baseDescriptor = baseDescriptor; this.extensionRegistry = extensionRegistry; @@ -2713,7 +2706,7 @@ public final class TestUtil { this.publicImportFile = importFile.getDependencies().get(0); Descriptors.Descriptor testAllTypes; - if (baseDescriptor.getName() == "TestAllTypes") { + if ("TestAllTypes".equals(baseDescriptor.getName())) { testAllTypes = baseDescriptor; } else { testAllTypes = file.findMessageTypeByName("TestAllTypes"); @@ -2724,39 +2717,34 @@ public final class TestUtil { // Use testAllTypes, rather than baseDescriptor, to allow // initialization using TestPackedTypes descriptors. These objects // won't be used by the methods for packed fields. - this.optionalGroup = - testAllTypes.findNestedTypeByName("OptionalGroup"); - this.repeatedGroup = - testAllTypes.findNestedTypeByName("RepeatedGroup"); + this.optionalGroup = testAllTypes.findNestedTypeByName("OptionalGroup"); + this.repeatedGroup = testAllTypes.findNestedTypeByName("RepeatedGroup"); } else { - this.optionalGroup = - file.findMessageTypeByName("OptionalGroup_extension"); - this.repeatedGroup = - file.findMessageTypeByName("RepeatedGroup_extension"); + this.optionalGroup = file.findMessageTypeByName("OptionalGroup_extension"); + this.repeatedGroup = file.findMessageTypeByName("RepeatedGroup_extension"); } this.nestedMessage = testAllTypes.findNestedTypeByName("NestedMessage"); this.foreignMessage = file.findMessageTypeByName("ForeignMessage"); this.importMessage = importFile.findMessageTypeByName("ImportMessage"); - this.publicImportMessage = publicImportFile.findMessageTypeByName( - "PublicImportMessage"); + this.publicImportMessage = publicImportFile.findMessageTypeByName("PublicImportMessage"); this.nestedEnum = testAllTypes.findEnumTypeByName("NestedEnum"); this.foreignEnum = file.findEnumTypeByName("ForeignEnum"); this.importEnum = importFile.findEnumTypeByName("ImportEnum"); - Assert.assertNotNull(optionalGroup ); - Assert.assertNotNull(repeatedGroup ); - Assert.assertNotNull(nestedMessage ); + Assert.assertNotNull(optionalGroup); + Assert.assertNotNull(repeatedGroup); + Assert.assertNotNull(nestedMessage); Assert.assertNotNull(foreignMessage); - Assert.assertNotNull(importMessage ); - Assert.assertNotNull(nestedEnum ); - Assert.assertNotNull(foreignEnum ); - Assert.assertNotNull(importEnum ); + Assert.assertNotNull(importMessage); + Assert.assertNotNull(nestedEnum); + Assert.assertNotNull(foreignEnum); + Assert.assertNotNull(importEnum); - this.nestedB = nestedMessage .findFieldByName("bb"); + this.nestedB = nestedMessage.findFieldByName("bb"); this.foreignC = foreignMessage.findFieldByName("c"); - this.importD = importMessage .findFieldByName("d"); - this.importE = publicImportMessage.findFieldByName("e"); + this.importD = importMessage.findFieldByName("d"); + this.importE = publicImportMessage.findFieldByName("e"); this.nestedFoo = nestedEnum.findValueByName("FOO"); this.nestedBar = nestedEnum.findValueByName("BAR"); this.nestedBaz = nestedEnum.findValueByName("BAZ"); @@ -2770,27 +2758,25 @@ public final class TestUtil { this.groupA = optionalGroup.findFieldByName("a"); this.repeatedGroupA = repeatedGroup.findFieldByName("a"); - Assert.assertNotNull(groupA ); + Assert.assertNotNull(groupA); Assert.assertNotNull(repeatedGroupA); - Assert.assertNotNull(nestedB ); - Assert.assertNotNull(foreignC ); - Assert.assertNotNull(importD ); - Assert.assertNotNull(importE ); - Assert.assertNotNull(nestedFoo ); - Assert.assertNotNull(nestedBar ); - Assert.assertNotNull(nestedBaz ); - Assert.assertNotNull(foreignFoo ); - Assert.assertNotNull(foreignBar ); - Assert.assertNotNull(foreignBaz ); - Assert.assertNotNull(importFoo ); - Assert.assertNotNull(importBar ); - Assert.assertNotNull(importBaz ); + Assert.assertNotNull(nestedB); + Assert.assertNotNull(foreignC); + Assert.assertNotNull(importD); + Assert.assertNotNull(importE); + Assert.assertNotNull(nestedFoo); + Assert.assertNotNull(nestedBar); + Assert.assertNotNull(nestedBaz); + Assert.assertNotNull(foreignFoo); + Assert.assertNotNull(foreignBar); + Assert.assertNotNull(foreignBaz); + Assert.assertNotNull(importFoo); + Assert.assertNotNull(importBar); + Assert.assertNotNull(importBaz); } - /** - * Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes. - */ - private Descriptors.FieldDescriptor f(String name) { + /** Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes. */ + Descriptors.FieldDescriptor f(String name) { Descriptors.FieldDescriptor result; if (extensionRegistry == null) { result = baseDescriptor.findFieldByName(name); @@ -2802,9 +2788,8 @@ public final class TestUtil { } /** - * Calls {@code parent.newBuilderForField()} or uses the - * {@code ExtensionRegistry} to find an appropriate builder, depending - * on what type is being tested. + * Calls {@code parent.newBuilderForField()} or uses the {@code ExtensionRegistry} to find an + * appropriate builder, depending on what type is being tested. */ private Message.Builder newBuilderForField( Message.Builder parent, Descriptors.FieldDescriptor field) { @@ -2812,8 +2797,8 @@ public final class TestUtil { return parent.newBuilderForField(field); } else { ExtensionRegistry.ExtensionInfo extension = - extensionRegistry.findImmutableExtensionByNumber( - field.getContainingType(), field.getNumber()); + extensionRegistry.findImmutableExtensionByNumber( + field.getContainingType(), field.getNumber()); Assert.assertNotNull(extension); Assert.assertNotNull(extension.defaultInstance); return extension.defaultInstance.newBuilderForType(); @@ -2823,210 +2808,223 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Set every field of {@code message} to the values expected by - * {@code assertAllFieldsSet()}, using the {@link Message.Builder} - * reflection interface. + * Set every field of {@code message} to the values expected by {@code assertAllFieldsSet()}, + * using the {@link Message.Builder} reflection interface. */ void setAllFieldsViaReflection(Message.Builder message) { - message.setField(f("optional_int32" ), 101 ); - message.setField(f("optional_int64" ), 102L); - message.setField(f("optional_uint32" ), 103 ); - message.setField(f("optional_uint64" ), 104L); - message.setField(f("optional_sint32" ), 105 ); - message.setField(f("optional_sint64" ), 106L); - message.setField(f("optional_fixed32" ), 107 ); - message.setField(f("optional_fixed64" ), 108L); - message.setField(f("optional_sfixed32"), 109 ); + message.setField(f("optional_int32"), 101); + message.setField(f("optional_int64"), 102L); + message.setField(f("optional_uint32"), 103); + message.setField(f("optional_uint64"), 104L); + message.setField(f("optional_sint32"), 105); + message.setField(f("optional_sint64"), 106L); + message.setField(f("optional_fixed32"), 107); + message.setField(f("optional_fixed64"), 108L); + message.setField(f("optional_sfixed32"), 109); message.setField(f("optional_sfixed64"), 110L); - message.setField(f("optional_float" ), 111F); - message.setField(f("optional_double" ), 112D); - message.setField(f("optional_bool" ), true); - message.setField(f("optional_string" ), "115"); - message.setField(f("optional_bytes" ), toBytes("116")); - - message.setField(f("optionalgroup"), - newBuilderForField(message, f("optionalgroup")) - .setField(groupA, 117).build()); - message.setField(f("optional_nested_message"), - newBuilderForField(message, f("optional_nested_message")) - .setField(nestedB, 118).build()); - message.setField(f("optional_foreign_message"), - newBuilderForField(message, f("optional_foreign_message")) - .setField(foreignC, 119).build()); - message.setField(f("optional_import_message"), - newBuilderForField(message, f("optional_import_message")) - .setField(importD, 120).build()); - message.setField(f("optional_public_import_message"), - newBuilderForField(message, f("optional_public_import_message")) - .setField(importE, 126).build()); - message.setField(f("optional_lazy_message"), - newBuilderForField(message, f("optional_lazy_message")) - .setField(nestedB, 127).build()); - - message.setField(f("optional_nested_enum" ), nestedBaz); + message.setField(f("optional_float"), 111F); + message.setField(f("optional_double"), 112D); + message.setField(f("optional_bool"), true); + message.setField(f("optional_string"), "115"); + message.setField(f("optional_bytes"), toBytes("116")); + + message.setField( + f("optionalgroup"), + newBuilderForField(message, f("optionalgroup")).setField(groupA, 117).build()); + message.setField( + f("optional_nested_message"), + newBuilderForField(message, f("optional_nested_message")).setField(nestedB, 118).build()); + message.setField( + f("optional_foreign_message"), + newBuilderForField(message, f("optional_foreign_message")) + .setField(foreignC, 119) + .build()); + message.setField( + f("optional_import_message"), + newBuilderForField(message, f("optional_import_message")).setField(importD, 120).build()); + message.setField( + f("optional_public_import_message"), + newBuilderForField(message, f("optional_public_import_message")) + .setField(importE, 126) + .build()); + message.setField( + f("optional_lazy_message"), + newBuilderForField(message, f("optional_lazy_message")).setField(nestedB, 127).build()); + + message.setField(f("optional_nested_enum"), nestedBaz); message.setField(f("optional_foreign_enum"), foreignBaz); - message.setField(f("optional_import_enum" ), importBaz); + message.setField(f("optional_import_enum"), importBaz); - message.setField(f("optional_string_piece" ), "124"); - message.setField(f("optional_cord" ), "125"); + message.setField(f("optional_string_piece"), "124"); + message.setField(f("optional_cord"), "125"); // ----------------------------------------------------------------- - message.addRepeatedField(f("repeated_int32" ), 201 ); - message.addRepeatedField(f("repeated_int64" ), 202L); - message.addRepeatedField(f("repeated_uint32" ), 203 ); - message.addRepeatedField(f("repeated_uint64" ), 204L); - message.addRepeatedField(f("repeated_sint32" ), 205 ); - message.addRepeatedField(f("repeated_sint64" ), 206L); - message.addRepeatedField(f("repeated_fixed32" ), 207 ); - message.addRepeatedField(f("repeated_fixed64" ), 208L); - message.addRepeatedField(f("repeated_sfixed32"), 209 ); + message.addRepeatedField(f("repeated_int32"), 201); + message.addRepeatedField(f("repeated_int64"), 202L); + message.addRepeatedField(f("repeated_uint32"), 203); + message.addRepeatedField(f("repeated_uint64"), 204L); + message.addRepeatedField(f("repeated_sint32"), 205); + message.addRepeatedField(f("repeated_sint64"), 206L); + message.addRepeatedField(f("repeated_fixed32"), 207); + message.addRepeatedField(f("repeated_fixed64"), 208L); + message.addRepeatedField(f("repeated_sfixed32"), 209); message.addRepeatedField(f("repeated_sfixed64"), 210L); - message.addRepeatedField(f("repeated_float" ), 211F); - message.addRepeatedField(f("repeated_double" ), 212D); - message.addRepeatedField(f("repeated_bool" ), true); - message.addRepeatedField(f("repeated_string" ), "215"); - message.addRepeatedField(f("repeated_bytes" ), toBytes("216")); - - message.addRepeatedField(f("repeatedgroup"), - newBuilderForField(message, f("repeatedgroup")) - .setField(repeatedGroupA, 217).build()); - message.addRepeatedField(f("repeated_nested_message"), - newBuilderForField(message, f("repeated_nested_message")) - .setField(nestedB, 218).build()); - message.addRepeatedField(f("repeated_foreign_message"), - newBuilderForField(message, f("repeated_foreign_message")) - .setField(foreignC, 219).build()); - message.addRepeatedField(f("repeated_import_message"), - newBuilderForField(message, f("repeated_import_message")) - .setField(importD, 220).build()); - message.addRepeatedField(f("repeated_lazy_message"), - newBuilderForField(message, f("repeated_lazy_message")) - .setField(nestedB, 227).build()); - - message.addRepeatedField(f("repeated_nested_enum" ), nestedBar); + message.addRepeatedField(f("repeated_float"), 211F); + message.addRepeatedField(f("repeated_double"), 212D); + message.addRepeatedField(f("repeated_bool"), true); + message.addRepeatedField(f("repeated_string"), "215"); + message.addRepeatedField(f("repeated_bytes"), toBytes("216")); + + message.addRepeatedField( + f("repeatedgroup"), + newBuilderForField(message, f("repeatedgroup")).setField(repeatedGroupA, 217).build()); + message.addRepeatedField( + f("repeated_nested_message"), + newBuilderForField(message, f("repeated_nested_message")).setField(nestedB, 218).build()); + message.addRepeatedField( + f("repeated_foreign_message"), + newBuilderForField(message, f("repeated_foreign_message")) + .setField(foreignC, 219) + .build()); + message.addRepeatedField( + f("repeated_import_message"), + newBuilderForField(message, f("repeated_import_message")).setField(importD, 220).build()); + message.addRepeatedField( + f("repeated_lazy_message"), + newBuilderForField(message, f("repeated_lazy_message")).setField(nestedB, 227).build()); + + message.addRepeatedField(f("repeated_nested_enum"), nestedBar); message.addRepeatedField(f("repeated_foreign_enum"), foreignBar); - message.addRepeatedField(f("repeated_import_enum" ), importBar); + message.addRepeatedField(f("repeated_import_enum"), importBar); - message.addRepeatedField(f("repeated_string_piece" ), "224"); - message.addRepeatedField(f("repeated_cord" ), "225"); + message.addRepeatedField(f("repeated_string_piece"), "224"); + message.addRepeatedField(f("repeated_cord"), "225"); // Add a second one of each field. - message.addRepeatedField(f("repeated_int32" ), 301 ); - message.addRepeatedField(f("repeated_int64" ), 302L); - message.addRepeatedField(f("repeated_uint32" ), 303 ); - message.addRepeatedField(f("repeated_uint64" ), 304L); - message.addRepeatedField(f("repeated_sint32" ), 305 ); - message.addRepeatedField(f("repeated_sint64" ), 306L); - message.addRepeatedField(f("repeated_fixed32" ), 307 ); - message.addRepeatedField(f("repeated_fixed64" ), 308L); - message.addRepeatedField(f("repeated_sfixed32"), 309 ); + message.addRepeatedField(f("repeated_int32"), 301); + message.addRepeatedField(f("repeated_int64"), 302L); + message.addRepeatedField(f("repeated_uint32"), 303); + message.addRepeatedField(f("repeated_uint64"), 304L); + message.addRepeatedField(f("repeated_sint32"), 305); + message.addRepeatedField(f("repeated_sint64"), 306L); + message.addRepeatedField(f("repeated_fixed32"), 307); + message.addRepeatedField(f("repeated_fixed64"), 308L); + message.addRepeatedField(f("repeated_sfixed32"), 309); message.addRepeatedField(f("repeated_sfixed64"), 310L); - message.addRepeatedField(f("repeated_float" ), 311F); - message.addRepeatedField(f("repeated_double" ), 312D); - message.addRepeatedField(f("repeated_bool" ), false); - message.addRepeatedField(f("repeated_string" ), "315"); - message.addRepeatedField(f("repeated_bytes" ), toBytes("316")); - - message.addRepeatedField(f("repeatedgroup"), - newBuilderForField(message, f("repeatedgroup")) - .setField(repeatedGroupA, 317).build()); - message.addRepeatedField(f("repeated_nested_message"), - newBuilderForField(message, f("repeated_nested_message")) - .setField(nestedB, 318).build()); - message.addRepeatedField(f("repeated_foreign_message"), - newBuilderForField(message, f("repeated_foreign_message")) - .setField(foreignC, 319).build()); - message.addRepeatedField(f("repeated_import_message"), - newBuilderForField(message, f("repeated_import_message")) - .setField(importD, 320).build()); - message.addRepeatedField(f("repeated_lazy_message"), - newBuilderForField(message, f("repeated_lazy_message")) - .setField(nestedB, 327).build()); - - message.addRepeatedField(f("repeated_nested_enum" ), nestedBaz); + message.addRepeatedField(f("repeated_float"), 311F); + message.addRepeatedField(f("repeated_double"), 312D); + message.addRepeatedField(f("repeated_bool"), false); + message.addRepeatedField(f("repeated_string"), "315"); + message.addRepeatedField(f("repeated_bytes"), toBytes("316")); + + message.addRepeatedField( + f("repeatedgroup"), + newBuilderForField(message, f("repeatedgroup")).setField(repeatedGroupA, 317).build()); + message.addRepeatedField( + f("repeated_nested_message"), + newBuilderForField(message, f("repeated_nested_message")).setField(nestedB, 318).build()); + message.addRepeatedField( + f("repeated_foreign_message"), + newBuilderForField(message, f("repeated_foreign_message")) + .setField(foreignC, 319) + .build()); + message.addRepeatedField( + f("repeated_import_message"), + newBuilderForField(message, f("repeated_import_message")).setField(importD, 320).build()); + message.addRepeatedField( + f("repeated_lazy_message"), + newBuilderForField(message, f("repeated_lazy_message")).setField(nestedB, 327).build()); + + message.addRepeatedField(f("repeated_nested_enum"), nestedBaz); message.addRepeatedField(f("repeated_foreign_enum"), foreignBaz); - message.addRepeatedField(f("repeated_import_enum" ), importBaz); + message.addRepeatedField(f("repeated_import_enum"), importBaz); - message.addRepeatedField(f("repeated_string_piece" ), "324"); - message.addRepeatedField(f("repeated_cord" ), "325"); + message.addRepeatedField(f("repeated_string_piece"), "324"); + message.addRepeatedField(f("repeated_cord"), "325"); // ----------------------------------------------------------------- - message.setField(f("default_int32" ), 401 ); - message.setField(f("default_int64" ), 402L); - message.setField(f("default_uint32" ), 403 ); - message.setField(f("default_uint64" ), 404L); - message.setField(f("default_sint32" ), 405 ); - message.setField(f("default_sint64" ), 406L); - message.setField(f("default_fixed32" ), 407 ); - message.setField(f("default_fixed64" ), 408L); - message.setField(f("default_sfixed32"), 409 ); + message.setField(f("default_int32"), 401); + message.setField(f("default_int64"), 402L); + message.setField(f("default_uint32"), 403); + message.setField(f("default_uint64"), 404L); + message.setField(f("default_sint32"), 405); + message.setField(f("default_sint64"), 406L); + message.setField(f("default_fixed32"), 407); + message.setField(f("default_fixed64"), 408L); + message.setField(f("default_sfixed32"), 409); message.setField(f("default_sfixed64"), 410L); - message.setField(f("default_float" ), 411F); - message.setField(f("default_double" ), 412D); - message.setField(f("default_bool" ), false); - message.setField(f("default_string" ), "415"); - message.setField(f("default_bytes" ), toBytes("416")); + message.setField(f("default_float"), 411F); + message.setField(f("default_double"), 412D); + message.setField(f("default_bool"), false); + message.setField(f("default_string"), "415"); + message.setField(f("default_bytes"), toBytes("416")); - message.setField(f("default_nested_enum" ), nestedFoo); + message.setField(f("default_nested_enum"), nestedFoo); message.setField(f("default_foreign_enum"), foreignFoo); - message.setField(f("default_import_enum" ), importFoo); + message.setField(f("default_import_enum"), importFoo); - message.setField(f("default_string_piece" ), "424"); - message.setField(f("default_cord" ), "425"); + message.setField(f("default_string_piece"), "424"); + message.setField(f("default_cord"), "425"); - message.setField(f("oneof_uint32" ), 601); - message.setField(f("oneof_nested_message"), - newBuilderForField(message, f("oneof_nested_message")) - .setField(nestedB, 602).build()); - message.setField(f("oneof_string" ), "603"); - message.setField(f("oneof_bytes" ), toBytes("604")); + message.setField(f("oneof_uint32"), 601); + message.setField( + f("oneof_nested_message"), + newBuilderForField(message, f("oneof_nested_message")).setField(nestedB, 602).build()); + message.setField(f("oneof_string"), "603"); + message.setField(f("oneof_bytes"), toBytes("604")); } // ------------------------------------------------------------------- /** - * Modify the repeated fields of {@code message} to contain the values - * expected by {@code assertRepeatedFieldsModified()}, using the - * {@link Message.Builder} reflection interface. + * Modify the repeated fields of {@code message} to contain the values expected by {@code + * assertRepeatedFieldsModified()}, using the {@link Message.Builder} reflection interface. */ void modifyRepeatedFieldsViaReflection(Message.Builder message) { - message.setRepeatedField(f("repeated_int32" ), 1, 501 ); - message.setRepeatedField(f("repeated_int64" ), 1, 502L); - message.setRepeatedField(f("repeated_uint32" ), 1, 503 ); - message.setRepeatedField(f("repeated_uint64" ), 1, 504L); - message.setRepeatedField(f("repeated_sint32" ), 1, 505 ); - message.setRepeatedField(f("repeated_sint64" ), 1, 506L); - message.setRepeatedField(f("repeated_fixed32" ), 1, 507 ); - message.setRepeatedField(f("repeated_fixed64" ), 1, 508L); - message.setRepeatedField(f("repeated_sfixed32"), 1, 509 ); + message.setRepeatedField(f("repeated_int32"), 1, 501); + message.setRepeatedField(f("repeated_int64"), 1, 502L); + message.setRepeatedField(f("repeated_uint32"), 1, 503); + message.setRepeatedField(f("repeated_uint64"), 1, 504L); + message.setRepeatedField(f("repeated_sint32"), 1, 505); + message.setRepeatedField(f("repeated_sint64"), 1, 506L); + message.setRepeatedField(f("repeated_fixed32"), 1, 507); + message.setRepeatedField(f("repeated_fixed64"), 1, 508L); + message.setRepeatedField(f("repeated_sfixed32"), 1, 509); message.setRepeatedField(f("repeated_sfixed64"), 1, 510L); - message.setRepeatedField(f("repeated_float" ), 1, 511F); - message.setRepeatedField(f("repeated_double" ), 1, 512D); - message.setRepeatedField(f("repeated_bool" ), 1, true); - message.setRepeatedField(f("repeated_string" ), 1, "515"); - message.setRepeatedField(f("repeated_bytes" ), 1, toBytes("516")); - - message.setRepeatedField(f("repeatedgroup"), 1, - newBuilderForField(message, f("repeatedgroup")) - .setField(repeatedGroupA, 517).build()); - message.setRepeatedField(f("repeated_nested_message"), 1, - newBuilderForField(message, f("repeated_nested_message")) - .setField(nestedB, 518).build()); - message.setRepeatedField(f("repeated_foreign_message"), 1, - newBuilderForField(message, f("repeated_foreign_message")) - .setField(foreignC, 519).build()); - message.setRepeatedField(f("repeated_import_message"), 1, - newBuilderForField(message, f("repeated_import_message")) - .setField(importD, 520).build()); - message.setRepeatedField(f("repeated_lazy_message"), 1, - newBuilderForField(message, f("repeated_lazy_message")) - .setField(nestedB, 527).build()); - - message.setRepeatedField(f("repeated_nested_enum" ), 1, nestedFoo); + message.setRepeatedField(f("repeated_float"), 1, 511F); + message.setRepeatedField(f("repeated_double"), 1, 512D); + message.setRepeatedField(f("repeated_bool"), 1, true); + message.setRepeatedField(f("repeated_string"), 1, "515"); + message.setRepeatedField(f("repeated_bytes"), 1, toBytes("516")); + + message.setRepeatedField( + f("repeatedgroup"), + 1, + newBuilderForField(message, f("repeatedgroup")).setField(repeatedGroupA, 517).build()); + message.setRepeatedField( + f("repeated_nested_message"), + 1, + newBuilderForField(message, f("repeated_nested_message")).setField(nestedB, 518).build()); + message.setRepeatedField( + f("repeated_foreign_message"), + 1, + newBuilderForField(message, f("repeated_foreign_message")) + .setField(foreignC, 519) + .build()); + message.setRepeatedField( + f("repeated_import_message"), + 1, + newBuilderForField(message, f("repeated_import_message")).setField(importD, 520).build()); + message.setRepeatedField( + f("repeated_lazy_message"), + 1, + newBuilderForField(message, f("repeated_lazy_message")).setField(nestedB, 527).build()); + + message.setRepeatedField(f("repeated_nested_enum"), 1, nestedFoo); message.setRepeatedField(f("repeated_foreign_enum"), 1, foreignFoo); - message.setRepeatedField(f("repeated_import_enum" ), 1, importFoo); + message.setRepeatedField(f("repeated_import_enum"), 1, importFoo); message.setRepeatedField(f("repeated_string_piece"), 1, "524"); message.setRepeatedField(f("repeated_cord"), 1, "525"); @@ -3035,244 +3033,235 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all fields of - * {@code message} are set to the values assigned by {@code setAllFields}, - * using the {@link Message} reflection interface. + * Assert (using {@code junit.framework.Assert}} that all fields of {@code message} are set to + * the values assigned by {@code setAllFields}, using the {@link Message} reflection interface. */ public void assertAllFieldsSetViaReflection(MessageOrBuilder message) { - Assert.assertTrue(message.hasField(f("optional_int32" ))); - Assert.assertTrue(message.hasField(f("optional_int64" ))); - Assert.assertTrue(message.hasField(f("optional_uint32" ))); - Assert.assertTrue(message.hasField(f("optional_uint64" ))); - Assert.assertTrue(message.hasField(f("optional_sint32" ))); - Assert.assertTrue(message.hasField(f("optional_sint64" ))); - Assert.assertTrue(message.hasField(f("optional_fixed32" ))); - Assert.assertTrue(message.hasField(f("optional_fixed64" ))); + Assert.assertTrue(message.hasField(f("optional_int32"))); + Assert.assertTrue(message.hasField(f("optional_int64"))); + Assert.assertTrue(message.hasField(f("optional_uint32"))); + Assert.assertTrue(message.hasField(f("optional_uint64"))); + Assert.assertTrue(message.hasField(f("optional_sint32"))); + Assert.assertTrue(message.hasField(f("optional_sint64"))); + Assert.assertTrue(message.hasField(f("optional_fixed32"))); + Assert.assertTrue(message.hasField(f("optional_fixed64"))); Assert.assertTrue(message.hasField(f("optional_sfixed32"))); Assert.assertTrue(message.hasField(f("optional_sfixed64"))); - Assert.assertTrue(message.hasField(f("optional_float" ))); - Assert.assertTrue(message.hasField(f("optional_double" ))); - Assert.assertTrue(message.hasField(f("optional_bool" ))); - Assert.assertTrue(message.hasField(f("optional_string" ))); - Assert.assertTrue(message.hasField(f("optional_bytes" ))); - - Assert.assertTrue(message.hasField(f("optionalgroup" ))); - Assert.assertTrue(message.hasField(f("optional_nested_message" ))); + Assert.assertTrue(message.hasField(f("optional_float"))); + Assert.assertTrue(message.hasField(f("optional_double"))); + Assert.assertTrue(message.hasField(f("optional_bool"))); + Assert.assertTrue(message.hasField(f("optional_string"))); + Assert.assertTrue(message.hasField(f("optional_bytes"))); + + Assert.assertTrue(message.hasField(f("optionalgroup"))); + Assert.assertTrue(message.hasField(f("optional_nested_message"))); Assert.assertTrue(message.hasField(f("optional_foreign_message"))); - Assert.assertTrue(message.hasField(f("optional_import_message" ))); + Assert.assertTrue(message.hasField(f("optional_import_message"))); + Assert.assertTrue(((Message) message.getField(f("optionalgroup"))).hasField(groupA)); Assert.assertTrue( - ((Message)message.getField(f("optionalgroup"))).hasField(groupA)); - Assert.assertTrue( - ((Message)message.getField(f("optional_nested_message"))) - .hasField(nestedB)); + ((Message) message.getField(f("optional_nested_message"))).hasField(nestedB)); Assert.assertTrue( - ((Message)message.getField(f("optional_foreign_message"))) - .hasField(foreignC)); + ((Message) message.getField(f("optional_foreign_message"))).hasField(foreignC)); Assert.assertTrue( - ((Message)message.getField(f("optional_import_message"))) - .hasField(importD)); + ((Message) message.getField(f("optional_import_message"))).hasField(importD)); - Assert.assertTrue(message.hasField(f("optional_nested_enum" ))); + Assert.assertTrue(message.hasField(f("optional_nested_enum"))); Assert.assertTrue(message.hasField(f("optional_foreign_enum"))); - Assert.assertTrue(message.hasField(f("optional_import_enum" ))); + Assert.assertTrue(message.hasField(f("optional_import_enum"))); Assert.assertTrue(message.hasField(f("optional_string_piece"))); Assert.assertTrue(message.hasField(f("optional_cord"))); - Assert.assertEquals(101 , message.getField(f("optional_int32" ))); - Assert.assertEquals(102L , message.getField(f("optional_int64" ))); - Assert.assertEquals(103 , message.getField(f("optional_uint32" ))); - Assert.assertEquals(104L , message.getField(f("optional_uint64" ))); - Assert.assertEquals(105 , message.getField(f("optional_sint32" ))); - Assert.assertEquals(106L , message.getField(f("optional_sint64" ))); - Assert.assertEquals(107 , message.getField(f("optional_fixed32" ))); - Assert.assertEquals(108L , message.getField(f("optional_fixed64" ))); - Assert.assertEquals(109 , message.getField(f("optional_sfixed32"))); - Assert.assertEquals(110L , message.getField(f("optional_sfixed64"))); - Assert.assertEquals(111F , message.getField(f("optional_float" ))); - Assert.assertEquals(112D , message.getField(f("optional_double" ))); - Assert.assertEquals(true , message.getField(f("optional_bool" ))); - Assert.assertEquals("115", message.getField(f("optional_string" ))); + Assert.assertEquals(101, message.getField(f("optional_int32"))); + Assert.assertEquals(102L, message.getField(f("optional_int64"))); + Assert.assertEquals(103, message.getField(f("optional_uint32"))); + Assert.assertEquals(104L, message.getField(f("optional_uint64"))); + Assert.assertEquals(105, message.getField(f("optional_sint32"))); + Assert.assertEquals(106L, message.getField(f("optional_sint64"))); + Assert.assertEquals(107, message.getField(f("optional_fixed32"))); + Assert.assertEquals(108L, message.getField(f("optional_fixed64"))); + Assert.assertEquals(109, message.getField(f("optional_sfixed32"))); + Assert.assertEquals(110L, message.getField(f("optional_sfixed64"))); + Assert.assertEquals(111F, message.getField(f("optional_float"))); + Assert.assertEquals(112D, message.getField(f("optional_double"))); + Assert.assertEquals(true, message.getField(f("optional_bool"))); + Assert.assertEquals("115", message.getField(f("optional_string"))); Assert.assertEquals(toBytes("116"), message.getField(f("optional_bytes"))); - Assert.assertEquals(117, - ((Message)message.getField(f("optionalgroup"))).getField(groupA)); - Assert.assertEquals(118, - ((Message)message.getField(f("optional_nested_message"))) - .getField(nestedB)); - Assert.assertEquals(119, - ((Message)message.getField(f("optional_foreign_message"))) - .getField(foreignC)); - Assert.assertEquals(120, - ((Message)message.getField(f("optional_import_message"))) - .getField(importD)); - Assert.assertEquals(126, - ((Message)message.getField(f("optional_public_import_message"))) - .getField(importE)); - Assert.assertEquals(127, - ((Message)message.getField(f("optional_lazy_message"))) - .getField(nestedB)); - - Assert.assertEquals( nestedBaz, message.getField(f("optional_nested_enum" ))); + Assert.assertEquals(117, ((Message) message.getField(f("optionalgroup"))).getField(groupA)); + Assert.assertEquals( + 118, ((Message) message.getField(f("optional_nested_message"))).getField(nestedB)); + Assert.assertEquals( + 119, ((Message) message.getField(f("optional_foreign_message"))).getField(foreignC)); + Assert.assertEquals( + 120, ((Message) message.getField(f("optional_import_message"))).getField(importD)); + Assert.assertEquals( + 126, ((Message) message.getField(f("optional_public_import_message"))).getField(importE)); + Assert.assertEquals( + 127, ((Message) message.getField(f("optional_lazy_message"))).getField(nestedB)); + + Assert.assertEquals(nestedBaz, message.getField(f("optional_nested_enum"))); Assert.assertEquals(foreignBaz, message.getField(f("optional_foreign_enum"))); - Assert.assertEquals( importBaz, message.getField(f("optional_import_enum" ))); + Assert.assertEquals(importBaz, message.getField(f("optional_import_enum"))); Assert.assertEquals("124", message.getField(f("optional_string_piece"))); Assert.assertEquals("125", message.getField(f("optional_cord"))); // ----------------------------------------------------------------- - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed64" ))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed64"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sfixed32"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sfixed64"))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_float" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_double" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bool" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bytes" ))); - - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeatedgroup" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_message" ))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_float"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_double"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bool"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bytes"))); + + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeatedgroup"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_message"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_message"))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_message" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_lazy_message" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_enum" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_enum" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_enum" ))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_message"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_lazy_message"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_enum"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_enum"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_enum"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string_piece"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_cord"))); - Assert.assertEquals(201 , message.getRepeatedField(f("repeated_int32" ), 0)); - Assert.assertEquals(202L , message.getRepeatedField(f("repeated_int64" ), 0)); - Assert.assertEquals(203 , message.getRepeatedField(f("repeated_uint32" ), 0)); - Assert.assertEquals(204L , message.getRepeatedField(f("repeated_uint64" ), 0)); - Assert.assertEquals(205 , message.getRepeatedField(f("repeated_sint32" ), 0)); - Assert.assertEquals(206L , message.getRepeatedField(f("repeated_sint64" ), 0)); - Assert.assertEquals(207 , message.getRepeatedField(f("repeated_fixed32" ), 0)); - Assert.assertEquals(208L , message.getRepeatedField(f("repeated_fixed64" ), 0)); - Assert.assertEquals(209 , message.getRepeatedField(f("repeated_sfixed32"), 0)); - Assert.assertEquals(210L , message.getRepeatedField(f("repeated_sfixed64"), 0)); - Assert.assertEquals(211F , message.getRepeatedField(f("repeated_float" ), 0)); - Assert.assertEquals(212D , message.getRepeatedField(f("repeated_double" ), 0)); - Assert.assertEquals(true , message.getRepeatedField(f("repeated_bool" ), 0)); - Assert.assertEquals("215", message.getRepeatedField(f("repeated_string" ), 0)); + Assert.assertEquals(201, message.getRepeatedField(f("repeated_int32"), 0)); + Assert.assertEquals(202L, message.getRepeatedField(f("repeated_int64"), 0)); + Assert.assertEquals(203, message.getRepeatedField(f("repeated_uint32"), 0)); + Assert.assertEquals(204L, message.getRepeatedField(f("repeated_uint64"), 0)); + Assert.assertEquals(205, message.getRepeatedField(f("repeated_sint32"), 0)); + Assert.assertEquals(206L, message.getRepeatedField(f("repeated_sint64"), 0)); + Assert.assertEquals(207, message.getRepeatedField(f("repeated_fixed32"), 0)); + Assert.assertEquals(208L, message.getRepeatedField(f("repeated_fixed64"), 0)); + Assert.assertEquals(209, message.getRepeatedField(f("repeated_sfixed32"), 0)); + Assert.assertEquals(210L, message.getRepeatedField(f("repeated_sfixed64"), 0)); + Assert.assertEquals(211F, message.getRepeatedField(f("repeated_float"), 0)); + Assert.assertEquals(212D, message.getRepeatedField(f("repeated_double"), 0)); + Assert.assertEquals(true, message.getRepeatedField(f("repeated_bool"), 0)); + Assert.assertEquals("215", message.getRepeatedField(f("repeated_string"), 0)); Assert.assertEquals(toBytes("216"), message.getRepeatedField(f("repeated_bytes"), 0)); - Assert.assertEquals(217, - ((Message)message.getRepeatedField(f("repeatedgroup"), 0)) - .getField(repeatedGroupA)); - Assert.assertEquals(218, - ((Message)message.getRepeatedField(f("repeated_nested_message"), 0)) - .getField(nestedB)); - Assert.assertEquals(219, - ((Message)message.getRepeatedField(f("repeated_foreign_message"), 0)) - .getField(foreignC)); - Assert.assertEquals(220, - ((Message)message.getRepeatedField(f("repeated_import_message"), 0)) - .getField(importD)); - Assert.assertEquals(227, - ((Message)message.getRepeatedField(f("repeated_lazy_message"), 0)) - .getField(nestedB)); - - Assert.assertEquals( nestedBar, message.getRepeatedField(f("repeated_nested_enum" ),0)); - Assert.assertEquals(foreignBar, message.getRepeatedField(f("repeated_foreign_enum"),0)); - Assert.assertEquals( importBar, message.getRepeatedField(f("repeated_import_enum" ),0)); + Assert.assertEquals( + 217, + ((Message) message.getRepeatedField(f("repeatedgroup"), 0)).getField(repeatedGroupA)); + Assert.assertEquals( + 218, + ((Message) message.getRepeatedField(f("repeated_nested_message"), 0)).getField(nestedB)); + Assert.assertEquals( + 219, + ((Message) message.getRepeatedField(f("repeated_foreign_message"), 0)) + .getField(foreignC)); + Assert.assertEquals( + 220, + ((Message) message.getRepeatedField(f("repeated_import_message"), 0)).getField(importD)); + Assert.assertEquals( + 227, + ((Message) message.getRepeatedField(f("repeated_lazy_message"), 0)).getField(nestedB)); + + Assert.assertEquals(nestedBar, message.getRepeatedField(f("repeated_nested_enum"), 0)); + Assert.assertEquals(foreignBar, message.getRepeatedField(f("repeated_foreign_enum"), 0)); + Assert.assertEquals(importBar, message.getRepeatedField(f("repeated_import_enum"), 0)); Assert.assertEquals("224", message.getRepeatedField(f("repeated_string_piece"), 0)); Assert.assertEquals("225", message.getRepeatedField(f("repeated_cord"), 0)); - Assert.assertEquals(301 , message.getRepeatedField(f("repeated_int32" ), 1)); - Assert.assertEquals(302L , message.getRepeatedField(f("repeated_int64" ), 1)); - Assert.assertEquals(303 , message.getRepeatedField(f("repeated_uint32" ), 1)); - Assert.assertEquals(304L , message.getRepeatedField(f("repeated_uint64" ), 1)); - Assert.assertEquals(305 , message.getRepeatedField(f("repeated_sint32" ), 1)); - Assert.assertEquals(306L , message.getRepeatedField(f("repeated_sint64" ), 1)); - Assert.assertEquals(307 , message.getRepeatedField(f("repeated_fixed32" ), 1)); - Assert.assertEquals(308L , message.getRepeatedField(f("repeated_fixed64" ), 1)); - Assert.assertEquals(309 , message.getRepeatedField(f("repeated_sfixed32"), 1)); - Assert.assertEquals(310L , message.getRepeatedField(f("repeated_sfixed64"), 1)); - Assert.assertEquals(311F , message.getRepeatedField(f("repeated_float" ), 1)); - Assert.assertEquals(312D , message.getRepeatedField(f("repeated_double" ), 1)); - Assert.assertEquals(false, message.getRepeatedField(f("repeated_bool" ), 1)); - Assert.assertEquals("315", message.getRepeatedField(f("repeated_string" ), 1)); + Assert.assertEquals(301, message.getRepeatedField(f("repeated_int32"), 1)); + Assert.assertEquals(302L, message.getRepeatedField(f("repeated_int64"), 1)); + Assert.assertEquals(303, message.getRepeatedField(f("repeated_uint32"), 1)); + Assert.assertEquals(304L, message.getRepeatedField(f("repeated_uint64"), 1)); + Assert.assertEquals(305, message.getRepeatedField(f("repeated_sint32"), 1)); + Assert.assertEquals(306L, message.getRepeatedField(f("repeated_sint64"), 1)); + Assert.assertEquals(307, message.getRepeatedField(f("repeated_fixed32"), 1)); + Assert.assertEquals(308L, message.getRepeatedField(f("repeated_fixed64"), 1)); + Assert.assertEquals(309, message.getRepeatedField(f("repeated_sfixed32"), 1)); + Assert.assertEquals(310L, message.getRepeatedField(f("repeated_sfixed64"), 1)); + Assert.assertEquals(311F, message.getRepeatedField(f("repeated_float"), 1)); + Assert.assertEquals(312D, message.getRepeatedField(f("repeated_double"), 1)); + Assert.assertEquals(false, message.getRepeatedField(f("repeated_bool"), 1)); + Assert.assertEquals("315", message.getRepeatedField(f("repeated_string"), 1)); Assert.assertEquals(toBytes("316"), message.getRepeatedField(f("repeated_bytes"), 1)); - Assert.assertEquals(317, - ((Message)message.getRepeatedField(f("repeatedgroup"), 1)) - .getField(repeatedGroupA)); - Assert.assertEquals(318, - ((Message)message.getRepeatedField(f("repeated_nested_message"), 1)) - .getField(nestedB)); - Assert.assertEquals(319, - ((Message)message.getRepeatedField(f("repeated_foreign_message"), 1)) - .getField(foreignC)); - Assert.assertEquals(320, - ((Message)message.getRepeatedField(f("repeated_import_message"), 1)) - .getField(importD)); - Assert.assertEquals(327, - ((Message)message.getRepeatedField(f("repeated_lazy_message"), 1)) - .getField(nestedB)); - - Assert.assertEquals( nestedBaz, message.getRepeatedField(f("repeated_nested_enum" ),1)); - Assert.assertEquals(foreignBaz, message.getRepeatedField(f("repeated_foreign_enum"),1)); - Assert.assertEquals( importBaz, message.getRepeatedField(f("repeated_import_enum" ),1)); + Assert.assertEquals( + 317, + ((Message) message.getRepeatedField(f("repeatedgroup"), 1)).getField(repeatedGroupA)); + Assert.assertEquals( + 318, + ((Message) message.getRepeatedField(f("repeated_nested_message"), 1)).getField(nestedB)); + Assert.assertEquals( + 319, + ((Message) message.getRepeatedField(f("repeated_foreign_message"), 1)) + .getField(foreignC)); + Assert.assertEquals( + 320, + ((Message) message.getRepeatedField(f("repeated_import_message"), 1)).getField(importD)); + Assert.assertEquals( + 327, + ((Message) message.getRepeatedField(f("repeated_lazy_message"), 1)).getField(nestedB)); + + Assert.assertEquals(nestedBaz, message.getRepeatedField(f("repeated_nested_enum"), 1)); + Assert.assertEquals(foreignBaz, message.getRepeatedField(f("repeated_foreign_enum"), 1)); + Assert.assertEquals(importBaz, message.getRepeatedField(f("repeated_import_enum"), 1)); Assert.assertEquals("324", message.getRepeatedField(f("repeated_string_piece"), 1)); Assert.assertEquals("325", message.getRepeatedField(f("repeated_cord"), 1)); // ----------------------------------------------------------------- - Assert.assertTrue(message.hasField(f("default_int32" ))); - Assert.assertTrue(message.hasField(f("default_int64" ))); - Assert.assertTrue(message.hasField(f("default_uint32" ))); - Assert.assertTrue(message.hasField(f("default_uint64" ))); - Assert.assertTrue(message.hasField(f("default_sint32" ))); - Assert.assertTrue(message.hasField(f("default_sint64" ))); - Assert.assertTrue(message.hasField(f("default_fixed32" ))); - Assert.assertTrue(message.hasField(f("default_fixed64" ))); + Assert.assertTrue(message.hasField(f("default_int32"))); + Assert.assertTrue(message.hasField(f("default_int64"))); + Assert.assertTrue(message.hasField(f("default_uint32"))); + Assert.assertTrue(message.hasField(f("default_uint64"))); + Assert.assertTrue(message.hasField(f("default_sint32"))); + Assert.assertTrue(message.hasField(f("default_sint64"))); + Assert.assertTrue(message.hasField(f("default_fixed32"))); + Assert.assertTrue(message.hasField(f("default_fixed64"))); Assert.assertTrue(message.hasField(f("default_sfixed32"))); Assert.assertTrue(message.hasField(f("default_sfixed64"))); - Assert.assertTrue(message.hasField(f("default_float" ))); - Assert.assertTrue(message.hasField(f("default_double" ))); - Assert.assertTrue(message.hasField(f("default_bool" ))); - Assert.assertTrue(message.hasField(f("default_string" ))); - Assert.assertTrue(message.hasField(f("default_bytes" ))); + Assert.assertTrue(message.hasField(f("default_float"))); + Assert.assertTrue(message.hasField(f("default_double"))); + Assert.assertTrue(message.hasField(f("default_bool"))); + Assert.assertTrue(message.hasField(f("default_string"))); + Assert.assertTrue(message.hasField(f("default_bytes"))); - Assert.assertTrue(message.hasField(f("default_nested_enum" ))); + Assert.assertTrue(message.hasField(f("default_nested_enum"))); Assert.assertTrue(message.hasField(f("default_foreign_enum"))); - Assert.assertTrue(message.hasField(f("default_import_enum" ))); + Assert.assertTrue(message.hasField(f("default_import_enum"))); Assert.assertTrue(message.hasField(f("default_string_piece"))); Assert.assertTrue(message.hasField(f("default_cord"))); - Assert.assertEquals(401 , message.getField(f("default_int32" ))); - Assert.assertEquals(402L , message.getField(f("default_int64" ))); - Assert.assertEquals(403 , message.getField(f("default_uint32" ))); - Assert.assertEquals(404L , message.getField(f("default_uint64" ))); - Assert.assertEquals(405 , message.getField(f("default_sint32" ))); - Assert.assertEquals(406L , message.getField(f("default_sint64" ))); - Assert.assertEquals(407 , message.getField(f("default_fixed32" ))); - Assert.assertEquals(408L , message.getField(f("default_fixed64" ))); - Assert.assertEquals(409 , message.getField(f("default_sfixed32"))); - Assert.assertEquals(410L , message.getField(f("default_sfixed64"))); - Assert.assertEquals(411F , message.getField(f("default_float" ))); - Assert.assertEquals(412D , message.getField(f("default_double" ))); - Assert.assertEquals(false, message.getField(f("default_bool" ))); - Assert.assertEquals("415", message.getField(f("default_string" ))); + Assert.assertEquals(401, message.getField(f("default_int32"))); + Assert.assertEquals(402L, message.getField(f("default_int64"))); + Assert.assertEquals(403, message.getField(f("default_uint32"))); + Assert.assertEquals(404L, message.getField(f("default_uint64"))); + Assert.assertEquals(405, message.getField(f("default_sint32"))); + Assert.assertEquals(406L, message.getField(f("default_sint64"))); + Assert.assertEquals(407, message.getField(f("default_fixed32"))); + Assert.assertEquals(408L, message.getField(f("default_fixed64"))); + Assert.assertEquals(409, message.getField(f("default_sfixed32"))); + Assert.assertEquals(410L, message.getField(f("default_sfixed64"))); + Assert.assertEquals(411F, message.getField(f("default_float"))); + Assert.assertEquals(412D, message.getField(f("default_double"))); + Assert.assertEquals(false, message.getField(f("default_bool"))); + Assert.assertEquals("415", message.getField(f("default_string"))); Assert.assertEquals(toBytes("416"), message.getField(f("default_bytes"))); - Assert.assertEquals( nestedFoo, message.getField(f("default_nested_enum" ))); + Assert.assertEquals(nestedFoo, message.getField(f("default_nested_enum"))); Assert.assertEquals(foreignFoo, message.getField(f("default_foreign_enum"))); - Assert.assertEquals( importFoo, message.getField(f("default_import_enum" ))); + Assert.assertEquals(importFoo, message.getField(f("default_import_enum"))); Assert.assertEquals("424", message.getField(f("default_string_piece"))); Assert.assertEquals("425", message.getField(f("default_cord"))); @@ -3289,9 +3278,9 @@ public final class TestUtil { Assert.assertTrue(message.hasField(f("oneof_nested_message"))); Assert.assertTrue(message.hasField(f("oneof_string"))); Assert.assertEquals(601, message.getField(f("oneof_uint32"))); - Assert.assertEquals(602, - ((MessageOrBuilder) message.getField(f("oneof_nested_message"))) - .getField(nestedB)); + Assert.assertEquals( + 602, + ((MessageOrBuilder) message.getField(f("oneof_nested_message"))).getField(nestedB)); Assert.assertEquals("603", message.getField(f("oneof_string"))); } } @@ -3299,175 +3288,163 @@ public final class TestUtil { // ------------------------------------------------------------------- /** - * Assert (using {@code junit.framework.Assert}} that all fields of - * {@code message} are cleared, and that getting the fields returns their - * default values, using the {@link Message} reflection interface. + * Assert (using {@code junit.framework.Assert}} that all fields of {@code message} are cleared, + * and that getting the fields returns their default values, using the {@link Message} + * reflection interface. */ public void assertClearViaReflection(MessageOrBuilder message) { // has_blah() should initially be false for all optional fields. - Assert.assertFalse(message.hasField(f("optional_int32" ))); - Assert.assertFalse(message.hasField(f("optional_int64" ))); - Assert.assertFalse(message.hasField(f("optional_uint32" ))); - Assert.assertFalse(message.hasField(f("optional_uint64" ))); - Assert.assertFalse(message.hasField(f("optional_sint32" ))); - Assert.assertFalse(message.hasField(f("optional_sint64" ))); - Assert.assertFalse(message.hasField(f("optional_fixed32" ))); - Assert.assertFalse(message.hasField(f("optional_fixed64" ))); + Assert.assertFalse(message.hasField(f("optional_int32"))); + Assert.assertFalse(message.hasField(f("optional_int64"))); + Assert.assertFalse(message.hasField(f("optional_uint32"))); + Assert.assertFalse(message.hasField(f("optional_uint64"))); + Assert.assertFalse(message.hasField(f("optional_sint32"))); + Assert.assertFalse(message.hasField(f("optional_sint64"))); + Assert.assertFalse(message.hasField(f("optional_fixed32"))); + Assert.assertFalse(message.hasField(f("optional_fixed64"))); Assert.assertFalse(message.hasField(f("optional_sfixed32"))); Assert.assertFalse(message.hasField(f("optional_sfixed64"))); - Assert.assertFalse(message.hasField(f("optional_float" ))); - Assert.assertFalse(message.hasField(f("optional_double" ))); - Assert.assertFalse(message.hasField(f("optional_bool" ))); - Assert.assertFalse(message.hasField(f("optional_string" ))); - Assert.assertFalse(message.hasField(f("optional_bytes" ))); - - Assert.assertFalse(message.hasField(f("optionalgroup" ))); - Assert.assertFalse(message.hasField(f("optional_nested_message" ))); + Assert.assertFalse(message.hasField(f("optional_float"))); + Assert.assertFalse(message.hasField(f("optional_double"))); + Assert.assertFalse(message.hasField(f("optional_bool"))); + Assert.assertFalse(message.hasField(f("optional_string"))); + Assert.assertFalse(message.hasField(f("optional_bytes"))); + + Assert.assertFalse(message.hasField(f("optionalgroup"))); + Assert.assertFalse(message.hasField(f("optional_nested_message"))); Assert.assertFalse(message.hasField(f("optional_foreign_message"))); - Assert.assertFalse(message.hasField(f("optional_import_message" ))); + Assert.assertFalse(message.hasField(f("optional_import_message"))); - Assert.assertFalse(message.hasField(f("optional_nested_enum" ))); + Assert.assertFalse(message.hasField(f("optional_nested_enum"))); Assert.assertFalse(message.hasField(f("optional_foreign_enum"))); - Assert.assertFalse(message.hasField(f("optional_import_enum" ))); + Assert.assertFalse(message.hasField(f("optional_import_enum"))); Assert.assertFalse(message.hasField(f("optional_string_piece"))); Assert.assertFalse(message.hasField(f("optional_cord"))); // Optional fields without defaults are set to zero or something like it. - Assert.assertEquals(0 , message.getField(f("optional_int32" ))); - Assert.assertEquals(0L , message.getField(f("optional_int64" ))); - Assert.assertEquals(0 , message.getField(f("optional_uint32" ))); - Assert.assertEquals(0L , message.getField(f("optional_uint64" ))); - Assert.assertEquals(0 , message.getField(f("optional_sint32" ))); - Assert.assertEquals(0L , message.getField(f("optional_sint64" ))); - Assert.assertEquals(0 , message.getField(f("optional_fixed32" ))); - Assert.assertEquals(0L , message.getField(f("optional_fixed64" ))); - Assert.assertEquals(0 , message.getField(f("optional_sfixed32"))); - Assert.assertEquals(0L , message.getField(f("optional_sfixed64"))); - Assert.assertEquals(0F , message.getField(f("optional_float" ))); - Assert.assertEquals(0D , message.getField(f("optional_double" ))); - Assert.assertEquals(false, message.getField(f("optional_bool" ))); - Assert.assertEquals("" , message.getField(f("optional_string" ))); + Assert.assertEquals(0, message.getField(f("optional_int32"))); + Assert.assertEquals(0L, message.getField(f("optional_int64"))); + Assert.assertEquals(0, message.getField(f("optional_uint32"))); + Assert.assertEquals(0L, message.getField(f("optional_uint64"))); + Assert.assertEquals(0, message.getField(f("optional_sint32"))); + Assert.assertEquals(0L, message.getField(f("optional_sint64"))); + Assert.assertEquals(0, message.getField(f("optional_fixed32"))); + Assert.assertEquals(0L, message.getField(f("optional_fixed64"))); + Assert.assertEquals(0, message.getField(f("optional_sfixed32"))); + Assert.assertEquals(0L, message.getField(f("optional_sfixed64"))); + Assert.assertEquals(0F, message.getField(f("optional_float"))); + Assert.assertEquals(0D, message.getField(f("optional_double"))); + Assert.assertEquals(false, message.getField(f("optional_bool"))); + Assert.assertEquals("", message.getField(f("optional_string"))); Assert.assertEquals(ByteString.EMPTY, message.getField(f("optional_bytes"))); // Embedded messages should also be clear. + Assert.assertFalse(((Message) message.getField(f("optionalgroup"))).hasField(groupA)); Assert.assertFalse( - ((Message)message.getField(f("optionalgroup"))).hasField(groupA)); - Assert.assertFalse( - ((Message)message.getField(f("optional_nested_message"))) - .hasField(nestedB)); + ((Message) message.getField(f("optional_nested_message"))).hasField(nestedB)); Assert.assertFalse( - ((Message)message.getField(f("optional_foreign_message"))) - .hasField(foreignC)); + ((Message) message.getField(f("optional_foreign_message"))).hasField(foreignC)); Assert.assertFalse( - ((Message)message.getField(f("optional_import_message"))) - .hasField(importD)); + ((Message) message.getField(f("optional_import_message"))).hasField(importD)); Assert.assertFalse( - ((Message)message.getField(f("optional_public_import_message"))) - .hasField(importE)); + ((Message) message.getField(f("optional_public_import_message"))).hasField(importE)); Assert.assertFalse( - ((Message)message.getField(f("optional_lazy_message"))) - .hasField(nestedB)); - - Assert.assertEquals(0, - ((Message)message.getField(f("optionalgroup"))).getField(groupA)); - Assert.assertEquals(0, - ((Message)message.getField(f("optional_nested_message"))) - .getField(nestedB)); - Assert.assertEquals(0, - ((Message)message.getField(f("optional_foreign_message"))) - .getField(foreignC)); - Assert.assertEquals(0, - ((Message)message.getField(f("optional_import_message"))) - .getField(importD)); - Assert.assertEquals(0, - ((Message)message.getField(f("optional_public_import_message"))) - .getField(importE)); - Assert.assertEquals(0, - ((Message)message.getField(f("optional_lazy_message"))) - .getField(nestedB)); + ((Message) message.getField(f("optional_lazy_message"))).hasField(nestedB)); + + Assert.assertEquals(0, ((Message) message.getField(f("optionalgroup"))).getField(groupA)); + Assert.assertEquals( + 0, ((Message) message.getField(f("optional_nested_message"))).getField(nestedB)); + Assert.assertEquals( + 0, ((Message) message.getField(f("optional_foreign_message"))).getField(foreignC)); + Assert.assertEquals( + 0, ((Message) message.getField(f("optional_import_message"))).getField(importD)); + Assert.assertEquals( + 0, ((Message) message.getField(f("optional_public_import_message"))).getField(importE)); + Assert.assertEquals( + 0, ((Message) message.getField(f("optional_lazy_message"))).getField(nestedB)); // Enums without defaults are set to the first value in the enum. - Assert.assertEquals( nestedFoo, message.getField(f("optional_nested_enum" ))); + Assert.assertEquals(nestedFoo, message.getField(f("optional_nested_enum"))); Assert.assertEquals(foreignFoo, message.getField(f("optional_foreign_enum"))); - Assert.assertEquals( importFoo, message.getField(f("optional_import_enum" ))); + Assert.assertEquals(importFoo, message.getField(f("optional_import_enum"))); Assert.assertEquals("", message.getField(f("optional_string_piece"))); Assert.assertEquals("", message.getField(f("optional_cord"))); // Repeated fields are empty. - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_int32" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_int64" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_uint32" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_uint64" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sint32" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sint64" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_fixed32" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_fixed64" ))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_int32"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_int64"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_uint32"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_uint64"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sint32"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sint64"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_fixed32"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_fixed64"))); Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sfixed32"))); Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sfixed64"))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_float" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_double" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_bool" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_string" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_bytes" ))); - - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeatedgroup" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_nested_message" ))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_float"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_double"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_bool"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_string"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_bytes"))); + + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeatedgroup"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_nested_message"))); Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_foreign_message"))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_import_message" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_lazy_message" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_nested_enum" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_foreign_enum" ))); - Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_import_enum" ))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_import_message"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_lazy_message"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_nested_enum"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_foreign_enum"))); + Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_import_enum"))); Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_string_piece"))); Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_cord"))); // has_blah() should also be false for all default fields. - Assert.assertFalse(message.hasField(f("default_int32" ))); - Assert.assertFalse(message.hasField(f("default_int64" ))); - Assert.assertFalse(message.hasField(f("default_uint32" ))); - Assert.assertFalse(message.hasField(f("default_uint64" ))); - Assert.assertFalse(message.hasField(f("default_sint32" ))); - Assert.assertFalse(message.hasField(f("default_sint64" ))); - Assert.assertFalse(message.hasField(f("default_fixed32" ))); - Assert.assertFalse(message.hasField(f("default_fixed64" ))); + Assert.assertFalse(message.hasField(f("default_int32"))); + Assert.assertFalse(message.hasField(f("default_int64"))); + Assert.assertFalse(message.hasField(f("default_uint32"))); + Assert.assertFalse(message.hasField(f("default_uint64"))); + Assert.assertFalse(message.hasField(f("default_sint32"))); + Assert.assertFalse(message.hasField(f("default_sint64"))); + Assert.assertFalse(message.hasField(f("default_fixed32"))); + Assert.assertFalse(message.hasField(f("default_fixed64"))); Assert.assertFalse(message.hasField(f("default_sfixed32"))); Assert.assertFalse(message.hasField(f("default_sfixed64"))); - Assert.assertFalse(message.hasField(f("default_float" ))); - Assert.assertFalse(message.hasField(f("default_double" ))); - Assert.assertFalse(message.hasField(f("default_bool" ))); - Assert.assertFalse(message.hasField(f("default_string" ))); - Assert.assertFalse(message.hasField(f("default_bytes" ))); + Assert.assertFalse(message.hasField(f("default_float"))); + Assert.assertFalse(message.hasField(f("default_double"))); + Assert.assertFalse(message.hasField(f("default_bool"))); + Assert.assertFalse(message.hasField(f("default_string"))); + Assert.assertFalse(message.hasField(f("default_bytes"))); - Assert.assertFalse(message.hasField(f("default_nested_enum" ))); + Assert.assertFalse(message.hasField(f("default_nested_enum"))); Assert.assertFalse(message.hasField(f("default_foreign_enum"))); - Assert.assertFalse(message.hasField(f("default_import_enum" ))); + Assert.assertFalse(message.hasField(f("default_import_enum"))); - Assert.assertFalse(message.hasField(f("default_string_piece" ))); - Assert.assertFalse(message.hasField(f("default_cord" ))); + Assert.assertFalse(message.hasField(f("default_string_piece"))); + Assert.assertFalse(message.hasField(f("default_cord"))); // Fields with defaults have their default values (duh). - Assert.assertEquals( 41 , message.getField(f("default_int32" ))); - Assert.assertEquals( 42L , message.getField(f("default_int64" ))); - Assert.assertEquals( 43 , message.getField(f("default_uint32" ))); - Assert.assertEquals( 44L , message.getField(f("default_uint64" ))); - Assert.assertEquals(-45 , message.getField(f("default_sint32" ))); - Assert.assertEquals( 46L , message.getField(f("default_sint64" ))); - Assert.assertEquals( 47 , message.getField(f("default_fixed32" ))); - Assert.assertEquals( 48L , message.getField(f("default_fixed64" ))); - Assert.assertEquals( 49 , message.getField(f("default_sfixed32"))); - Assert.assertEquals(-50L , message.getField(f("default_sfixed64"))); - Assert.assertEquals( 51.5F , message.getField(f("default_float" ))); - Assert.assertEquals( 52e3D , message.getField(f("default_double" ))); - Assert.assertEquals(true , message.getField(f("default_bool" ))); - Assert.assertEquals("hello", message.getField(f("default_string" ))); + Assert.assertEquals(41, message.getField(f("default_int32"))); + Assert.assertEquals(42L, message.getField(f("default_int64"))); + Assert.assertEquals(43, message.getField(f("default_uint32"))); + Assert.assertEquals(44L, message.getField(f("default_uint64"))); + Assert.assertEquals(-45, message.getField(f("default_sint32"))); + Assert.assertEquals(46L, message.getField(f("default_sint64"))); + Assert.assertEquals(47, message.getField(f("default_fixed32"))); + Assert.assertEquals(48L, message.getField(f("default_fixed64"))); + Assert.assertEquals(49, message.getField(f("default_sfixed32"))); + Assert.assertEquals(-50L, message.getField(f("default_sfixed64"))); + Assert.assertEquals(51.5F, message.getField(f("default_float"))); + Assert.assertEquals(52e3D, message.getField(f("default_double"))); + Assert.assertEquals(true, message.getField(f("default_bool"))); + Assert.assertEquals("hello", message.getField(f("default_string"))); Assert.assertEquals(toBytes("world"), message.getField(f("default_bytes"))); - Assert.assertEquals( nestedBar, message.getField(f("default_nested_enum" ))); + Assert.assertEquals(nestedBar, message.getField(f("default_nested_enum"))); Assert.assertEquals(foreignBar, message.getField(f("default_foreign_enum"))); - Assert.assertEquals( importBar, message.getField(f("default_import_enum" ))); + Assert.assertEquals(importBar, message.getField(f("default_import_enum"))); Assert.assertEquals("abc", message.getField(f("default_string_piece"))); Assert.assertEquals("123", message.getField(f("default_cord"))); @@ -3482,205 +3459,204 @@ public final class TestUtil { Assert.assertEquals(toBytes(""), message.getField(f("oneof_bytes"))); } - // --------------------------------------------------------------- - public void assertRepeatedFieldsModifiedViaReflection( - MessageOrBuilder message) { + public void assertRepeatedFieldsModifiedViaReflection(MessageOrBuilder message) { // ModifyRepeatedFields only sets the second repeated element of each // field. In addition to verifying this, we also verify that the first // element and size were *not* modified. - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed64" ))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed64"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sfixed32"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sfixed64"))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_float" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_double" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bool" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bytes" ))); - - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeatedgroup" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_message" ))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_float"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_double"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bool"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bytes"))); + + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeatedgroup"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_message"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_message"))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_message" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_lazy_message" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_enum" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_enum" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_enum" ))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_message"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_lazy_message"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_enum"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_enum"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_enum"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string_piece"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_cord"))); - Assert.assertEquals(201 , message.getRepeatedField(f("repeated_int32" ), 0)); - Assert.assertEquals(202L , message.getRepeatedField(f("repeated_int64" ), 0)); - Assert.assertEquals(203 , message.getRepeatedField(f("repeated_uint32" ), 0)); - Assert.assertEquals(204L , message.getRepeatedField(f("repeated_uint64" ), 0)); - Assert.assertEquals(205 , message.getRepeatedField(f("repeated_sint32" ), 0)); - Assert.assertEquals(206L , message.getRepeatedField(f("repeated_sint64" ), 0)); - Assert.assertEquals(207 , message.getRepeatedField(f("repeated_fixed32" ), 0)); - Assert.assertEquals(208L , message.getRepeatedField(f("repeated_fixed64" ), 0)); - Assert.assertEquals(209 , message.getRepeatedField(f("repeated_sfixed32"), 0)); - Assert.assertEquals(210L , message.getRepeatedField(f("repeated_sfixed64"), 0)); - Assert.assertEquals(211F , message.getRepeatedField(f("repeated_float" ), 0)); - Assert.assertEquals(212D , message.getRepeatedField(f("repeated_double" ), 0)); - Assert.assertEquals(true , message.getRepeatedField(f("repeated_bool" ), 0)); - Assert.assertEquals("215", message.getRepeatedField(f("repeated_string" ), 0)); + Assert.assertEquals(201, message.getRepeatedField(f("repeated_int32"), 0)); + Assert.assertEquals(202L, message.getRepeatedField(f("repeated_int64"), 0)); + Assert.assertEquals(203, message.getRepeatedField(f("repeated_uint32"), 0)); + Assert.assertEquals(204L, message.getRepeatedField(f("repeated_uint64"), 0)); + Assert.assertEquals(205, message.getRepeatedField(f("repeated_sint32"), 0)); + Assert.assertEquals(206L, message.getRepeatedField(f("repeated_sint64"), 0)); + Assert.assertEquals(207, message.getRepeatedField(f("repeated_fixed32"), 0)); + Assert.assertEquals(208L, message.getRepeatedField(f("repeated_fixed64"), 0)); + Assert.assertEquals(209, message.getRepeatedField(f("repeated_sfixed32"), 0)); + Assert.assertEquals(210L, message.getRepeatedField(f("repeated_sfixed64"), 0)); + Assert.assertEquals(211F, message.getRepeatedField(f("repeated_float"), 0)); + Assert.assertEquals(212D, message.getRepeatedField(f("repeated_double"), 0)); + Assert.assertEquals(true, message.getRepeatedField(f("repeated_bool"), 0)); + Assert.assertEquals("215", message.getRepeatedField(f("repeated_string"), 0)); Assert.assertEquals(toBytes("216"), message.getRepeatedField(f("repeated_bytes"), 0)); - Assert.assertEquals(217, - ((Message)message.getRepeatedField(f("repeatedgroup"), 0)) - .getField(repeatedGroupA)); - Assert.assertEquals(218, - ((Message)message.getRepeatedField(f("repeated_nested_message"), 0)) - .getField(nestedB)); - Assert.assertEquals(219, - ((Message)message.getRepeatedField(f("repeated_foreign_message"), 0)) - .getField(foreignC)); - Assert.assertEquals(220, - ((Message)message.getRepeatedField(f("repeated_import_message"), 0)) - .getField(importD)); - Assert.assertEquals(227, - ((Message)message.getRepeatedField(f("repeated_lazy_message"), 0)) - .getField(nestedB)); - - Assert.assertEquals( nestedBar, message.getRepeatedField(f("repeated_nested_enum" ),0)); - Assert.assertEquals(foreignBar, message.getRepeatedField(f("repeated_foreign_enum"),0)); - Assert.assertEquals( importBar, message.getRepeatedField(f("repeated_import_enum" ),0)); + Assert.assertEquals( + 217, + ((Message) message.getRepeatedField(f("repeatedgroup"), 0)).getField(repeatedGroupA)); + Assert.assertEquals( + 218, + ((Message) message.getRepeatedField(f("repeated_nested_message"), 0)).getField(nestedB)); + Assert.assertEquals( + 219, + ((Message) message.getRepeatedField(f("repeated_foreign_message"), 0)) + .getField(foreignC)); + Assert.assertEquals( + 220, + ((Message) message.getRepeatedField(f("repeated_import_message"), 0)).getField(importD)); + Assert.assertEquals( + 227, + ((Message) message.getRepeatedField(f("repeated_lazy_message"), 0)).getField(nestedB)); + + Assert.assertEquals(nestedBar, message.getRepeatedField(f("repeated_nested_enum"), 0)); + Assert.assertEquals(foreignBar, message.getRepeatedField(f("repeated_foreign_enum"), 0)); + Assert.assertEquals(importBar, message.getRepeatedField(f("repeated_import_enum"), 0)); Assert.assertEquals("224", message.getRepeatedField(f("repeated_string_piece"), 0)); Assert.assertEquals("225", message.getRepeatedField(f("repeated_cord"), 0)); - Assert.assertEquals(501 , message.getRepeatedField(f("repeated_int32" ), 1)); - Assert.assertEquals(502L , message.getRepeatedField(f("repeated_int64" ), 1)); - Assert.assertEquals(503 , message.getRepeatedField(f("repeated_uint32" ), 1)); - Assert.assertEquals(504L , message.getRepeatedField(f("repeated_uint64" ), 1)); - Assert.assertEquals(505 , message.getRepeatedField(f("repeated_sint32" ), 1)); - Assert.assertEquals(506L , message.getRepeatedField(f("repeated_sint64" ), 1)); - Assert.assertEquals(507 , message.getRepeatedField(f("repeated_fixed32" ), 1)); - Assert.assertEquals(508L , message.getRepeatedField(f("repeated_fixed64" ), 1)); - Assert.assertEquals(509 , message.getRepeatedField(f("repeated_sfixed32"), 1)); - Assert.assertEquals(510L , message.getRepeatedField(f("repeated_sfixed64"), 1)); - Assert.assertEquals(511F , message.getRepeatedField(f("repeated_float" ), 1)); - Assert.assertEquals(512D , message.getRepeatedField(f("repeated_double" ), 1)); - Assert.assertEquals(true , message.getRepeatedField(f("repeated_bool" ), 1)); - Assert.assertEquals("515", message.getRepeatedField(f("repeated_string" ), 1)); + Assert.assertEquals(501, message.getRepeatedField(f("repeated_int32"), 1)); + Assert.assertEquals(502L, message.getRepeatedField(f("repeated_int64"), 1)); + Assert.assertEquals(503, message.getRepeatedField(f("repeated_uint32"), 1)); + Assert.assertEquals(504L, message.getRepeatedField(f("repeated_uint64"), 1)); + Assert.assertEquals(505, message.getRepeatedField(f("repeated_sint32"), 1)); + Assert.assertEquals(506L, message.getRepeatedField(f("repeated_sint64"), 1)); + Assert.assertEquals(507, message.getRepeatedField(f("repeated_fixed32"), 1)); + Assert.assertEquals(508L, message.getRepeatedField(f("repeated_fixed64"), 1)); + Assert.assertEquals(509, message.getRepeatedField(f("repeated_sfixed32"), 1)); + Assert.assertEquals(510L, message.getRepeatedField(f("repeated_sfixed64"), 1)); + Assert.assertEquals(511F, message.getRepeatedField(f("repeated_float"), 1)); + Assert.assertEquals(512D, message.getRepeatedField(f("repeated_double"), 1)); + Assert.assertEquals(true, message.getRepeatedField(f("repeated_bool"), 1)); + Assert.assertEquals("515", message.getRepeatedField(f("repeated_string"), 1)); Assert.assertEquals(toBytes("516"), message.getRepeatedField(f("repeated_bytes"), 1)); - Assert.assertEquals(517, - ((Message)message.getRepeatedField(f("repeatedgroup"), 1)) - .getField(repeatedGroupA)); - Assert.assertEquals(518, - ((Message)message.getRepeatedField(f("repeated_nested_message"), 1)) - .getField(nestedB)); - Assert.assertEquals(519, - ((Message)message.getRepeatedField(f("repeated_foreign_message"), 1)) - .getField(foreignC)); - Assert.assertEquals(520, - ((Message)message.getRepeatedField(f("repeated_import_message"), 1)) - .getField(importD)); - Assert.assertEquals(527, - ((Message)message.getRepeatedField(f("repeated_lazy_message"), 1)) - .getField(nestedB)); - - Assert.assertEquals( nestedFoo, message.getRepeatedField(f("repeated_nested_enum" ),1)); - Assert.assertEquals(foreignFoo, message.getRepeatedField(f("repeated_foreign_enum"),1)); - Assert.assertEquals( importFoo, message.getRepeatedField(f("repeated_import_enum" ),1)); + Assert.assertEquals( + 517, + ((Message) message.getRepeatedField(f("repeatedgroup"), 1)).getField(repeatedGroupA)); + Assert.assertEquals( + 518, + ((Message) message.getRepeatedField(f("repeated_nested_message"), 1)).getField(nestedB)); + Assert.assertEquals( + 519, + ((Message) message.getRepeatedField(f("repeated_foreign_message"), 1)) + .getField(foreignC)); + Assert.assertEquals( + 520, + ((Message) message.getRepeatedField(f("repeated_import_message"), 1)).getField(importD)); + Assert.assertEquals( + 527, + ((Message) message.getRepeatedField(f("repeated_lazy_message"), 1)).getField(nestedB)); + + Assert.assertEquals(nestedFoo, message.getRepeatedField(f("repeated_nested_enum"), 1)); + Assert.assertEquals(foreignFoo, message.getRepeatedField(f("repeated_foreign_enum"), 1)); + Assert.assertEquals(importFoo, message.getRepeatedField(f("repeated_import_enum"), 1)); Assert.assertEquals("524", message.getRepeatedField(f("repeated_string_piece"), 1)); Assert.assertEquals("525", message.getRepeatedField(f("repeated_cord"), 1)); } public void setPackedFieldsViaReflection(Message.Builder message) { - message.addRepeatedField(f("packed_int32" ), 601 ); - message.addRepeatedField(f("packed_int64" ), 602L); - message.addRepeatedField(f("packed_uint32" ), 603 ); - message.addRepeatedField(f("packed_uint64" ), 604L); - message.addRepeatedField(f("packed_sint32" ), 605 ); - message.addRepeatedField(f("packed_sint64" ), 606L); - message.addRepeatedField(f("packed_fixed32" ), 607 ); - message.addRepeatedField(f("packed_fixed64" ), 608L); - message.addRepeatedField(f("packed_sfixed32"), 609 ); + message.addRepeatedField(f("packed_int32"), 601); + message.addRepeatedField(f("packed_int64"), 602L); + message.addRepeatedField(f("packed_uint32"), 603); + message.addRepeatedField(f("packed_uint64"), 604L); + message.addRepeatedField(f("packed_sint32"), 605); + message.addRepeatedField(f("packed_sint64"), 606L); + message.addRepeatedField(f("packed_fixed32"), 607); + message.addRepeatedField(f("packed_fixed64"), 608L); + message.addRepeatedField(f("packed_sfixed32"), 609); message.addRepeatedField(f("packed_sfixed64"), 610L); - message.addRepeatedField(f("packed_float" ), 611F); - message.addRepeatedField(f("packed_double" ), 612D); - message.addRepeatedField(f("packed_bool" ), true); - message.addRepeatedField(f("packed_enum" ), foreignBar); + message.addRepeatedField(f("packed_float"), 611F); + message.addRepeatedField(f("packed_double"), 612D); + message.addRepeatedField(f("packed_bool"), true); + message.addRepeatedField(f("packed_enum"), foreignBar); // Add a second one of each field. - message.addRepeatedField(f("packed_int32" ), 701 ); - message.addRepeatedField(f("packed_int64" ), 702L); - message.addRepeatedField(f("packed_uint32" ), 703 ); - message.addRepeatedField(f("packed_uint64" ), 704L); - message.addRepeatedField(f("packed_sint32" ), 705 ); - message.addRepeatedField(f("packed_sint64" ), 706L); - message.addRepeatedField(f("packed_fixed32" ), 707 ); - message.addRepeatedField(f("packed_fixed64" ), 708L); - message.addRepeatedField(f("packed_sfixed32"), 709 ); + message.addRepeatedField(f("packed_int32"), 701); + message.addRepeatedField(f("packed_int64"), 702L); + message.addRepeatedField(f("packed_uint32"), 703); + message.addRepeatedField(f("packed_uint64"), 704L); + message.addRepeatedField(f("packed_sint32"), 705); + message.addRepeatedField(f("packed_sint64"), 706L); + message.addRepeatedField(f("packed_fixed32"), 707); + message.addRepeatedField(f("packed_fixed64"), 708L); + message.addRepeatedField(f("packed_sfixed32"), 709); message.addRepeatedField(f("packed_sfixed64"), 710L); - message.addRepeatedField(f("packed_float" ), 711F); - message.addRepeatedField(f("packed_double" ), 712D); - message.addRepeatedField(f("packed_bool" ), false); - message.addRepeatedField(f("packed_enum" ), foreignBaz); + message.addRepeatedField(f("packed_float"), 711F); + message.addRepeatedField(f("packed_double"), 712D); + message.addRepeatedField(f("packed_bool"), false); + message.addRepeatedField(f("packed_enum"), foreignBaz); } public void assertPackedFieldsSetViaReflection(MessageOrBuilder message) { - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_int32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_int64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_uint32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_uint64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sint32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sint64" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_fixed32" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_fixed64" ))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_int32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_int64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_uint32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_uint64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sint32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sint64"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_fixed32"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_fixed64"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sfixed32"))); Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sfixed64"))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_float" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_double" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_bool" ))); - Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_enum" ))); - Assert.assertEquals(601 , message.getRepeatedField(f("packed_int32" ), 0)); - Assert.assertEquals(602L , message.getRepeatedField(f("packed_int64" ), 0)); - Assert.assertEquals(603 , message.getRepeatedField(f("packed_uint32" ), 0)); - Assert.assertEquals(604L , message.getRepeatedField(f("packed_uint64" ), 0)); - Assert.assertEquals(605 , message.getRepeatedField(f("packed_sint32" ), 0)); - Assert.assertEquals(606L , message.getRepeatedField(f("packed_sint64" ), 0)); - Assert.assertEquals(607 , message.getRepeatedField(f("packed_fixed32" ), 0)); - Assert.assertEquals(608L , message.getRepeatedField(f("packed_fixed64" ), 0)); - Assert.assertEquals(609 , message.getRepeatedField(f("packed_sfixed32"), 0)); - Assert.assertEquals(610L , message.getRepeatedField(f("packed_sfixed64"), 0)); - Assert.assertEquals(611F , message.getRepeatedField(f("packed_float" ), 0)); - Assert.assertEquals(612D , message.getRepeatedField(f("packed_double" ), 0)); - Assert.assertEquals(true , message.getRepeatedField(f("packed_bool" ), 0)); - Assert.assertEquals(foreignBar, message.getRepeatedField(f("packed_enum" ),0)); - Assert.assertEquals(701 , message.getRepeatedField(f("packed_int32" ), 1)); - Assert.assertEquals(702L , message.getRepeatedField(f("packed_int64" ), 1)); - Assert.assertEquals(703 , message.getRepeatedField(f("packed_uint32" ), 1)); - Assert.assertEquals(704L , message.getRepeatedField(f("packed_uint64" ), 1)); - Assert.assertEquals(705 , message.getRepeatedField(f("packed_sint32" ), 1)); - Assert.assertEquals(706L , message.getRepeatedField(f("packed_sint64" ), 1)); - Assert.assertEquals(707 , message.getRepeatedField(f("packed_fixed32" ), 1)); - Assert.assertEquals(708L , message.getRepeatedField(f("packed_fixed64" ), 1)); - Assert.assertEquals(709 , message.getRepeatedField(f("packed_sfixed32"), 1)); - Assert.assertEquals(710L , message.getRepeatedField(f("packed_sfixed64"), 1)); - Assert.assertEquals(711F , message.getRepeatedField(f("packed_float" ), 1)); - Assert.assertEquals(712D , message.getRepeatedField(f("packed_double" ), 1)); - Assert.assertEquals(false, message.getRepeatedField(f("packed_bool" ), 1)); - Assert.assertEquals(foreignBaz, message.getRepeatedField(f("packed_enum" ),1)); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_float"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_double"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_bool"))); + Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_enum"))); + Assert.assertEquals(601, message.getRepeatedField(f("packed_int32"), 0)); + Assert.assertEquals(602L, message.getRepeatedField(f("packed_int64"), 0)); + Assert.assertEquals(603, message.getRepeatedField(f("packed_uint32"), 0)); + Assert.assertEquals(604L, message.getRepeatedField(f("packed_uint64"), 0)); + Assert.assertEquals(605, message.getRepeatedField(f("packed_sint32"), 0)); + Assert.assertEquals(606L, message.getRepeatedField(f("packed_sint64"), 0)); + Assert.assertEquals(607, message.getRepeatedField(f("packed_fixed32"), 0)); + Assert.assertEquals(608L, message.getRepeatedField(f("packed_fixed64"), 0)); + Assert.assertEquals(609, message.getRepeatedField(f("packed_sfixed32"), 0)); + Assert.assertEquals(610L, message.getRepeatedField(f("packed_sfixed64"), 0)); + Assert.assertEquals(611F, message.getRepeatedField(f("packed_float"), 0)); + Assert.assertEquals(612D, message.getRepeatedField(f("packed_double"), 0)); + Assert.assertEquals(true, message.getRepeatedField(f("packed_bool"), 0)); + Assert.assertEquals(foreignBar, message.getRepeatedField(f("packed_enum"), 0)); + Assert.assertEquals(701, message.getRepeatedField(f("packed_int32"), 1)); + Assert.assertEquals(702L, message.getRepeatedField(f("packed_int64"), 1)); + Assert.assertEquals(703, message.getRepeatedField(f("packed_uint32"), 1)); + Assert.assertEquals(704L, message.getRepeatedField(f("packed_uint64"), 1)); + Assert.assertEquals(705, message.getRepeatedField(f("packed_sint32"), 1)); + Assert.assertEquals(706L, message.getRepeatedField(f("packed_sint64"), 1)); + Assert.assertEquals(707, message.getRepeatedField(f("packed_fixed32"), 1)); + Assert.assertEquals(708L, message.getRepeatedField(f("packed_fixed64"), 1)); + Assert.assertEquals(709, message.getRepeatedField(f("packed_sfixed32"), 1)); + Assert.assertEquals(710L, message.getRepeatedField(f("packed_sfixed64"), 1)); + Assert.assertEquals(711F, message.getRepeatedField(f("packed_float"), 1)); + Assert.assertEquals(712D, message.getRepeatedField(f("packed_double"), 1)); + Assert.assertEquals(false, message.getRepeatedField(f("packed_bool"), 1)); + Assert.assertEquals(foreignBaz, message.getRepeatedField(f("packed_enum"), 1)); } /** * Verifies that the reflection setters for the given.Builder object throw a - * NullPointerException if they are passed a null value. Uses Assert to throw an - * appropriate assertion failure, if the condition is not verified. + * NullPointerException if they are passed a null value. Uses Assert to throw an appropriate + * assertion failure, if the condition is not verified. */ - public void assertReflectionSettersRejectNull(Message.Builder builder) - throws Exception { + public void assertReflectionSettersRejectNull(Message.Builder builder) throws Exception { try { builder.setField(f("optional_string"), null); Assert.fail("Exception was not thrown"); @@ -3700,15 +3676,13 @@ public final class TestUtil { // We expect this exception. } try { - builder.setField(f("optional_nested_message"), - (TestAllTypes.NestedMessage) null); + builder.setField(f("optional_nested_message"), (TestAllTypes.NestedMessage) null); Assert.fail("Exception was not thrown"); } catch (NullPointerException e) { // We expect this exception. } try { - builder.setField(f("optional_nested_message"), - (TestAllTypes.NestedMessage.Builder) null); + builder.setField(f("optional_nested_message"), (TestAllTypes.NestedMessage.Builder) null); Assert.fail("Exception was not thrown"); } catch (NullPointerException e) { // We expect this exception. @@ -3742,7 +3716,7 @@ public final class TestUtil { /** * Verifies that the reflection repeated setters for the given Builder object throw a - * NullPointerException if they are passed a null value. Uses Assert to throw an appropriate + * NullPointerException if they are passed a null value. Uses Assert to throw an appropriate * assertion failure, if the condition is not verified. */ public void assertReflectionRepeatedSettersRejectNull(Message.Builder builder) @@ -3772,8 +3746,7 @@ public final class TestUtil { } builder.addRepeatedField( - f("repeated_nested_message"), - TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); + f("repeated_nested_message"), TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); try { builder.setRepeatedField(f("repeated_nested_message"), 0, null); Assert.fail("Exception was not thrown"); @@ -3783,10 +3756,7 @@ public final class TestUtil { } } - /** - * @param filePath The path relative to - * {@link #getTestDataDir}. - */ + /** @param filePath The path relative to {@link #getTestDataDir}. */ public static String readTextFromFile(String filePath) { return readBytesFromFile(filePath).toStringUtf8(); } @@ -3798,8 +3768,7 @@ public final class TestUtil { try { ancestor = ancestor.getCanonicalFile(); } catch (IOException e) { - throw new RuntimeException( - "Couldn't get canonical name of working directory.", e); + throw new RuntimeException("Couldn't get canonical name of working directory.", e); } while (ancestor != null && ancestor.exists()) { if (new File(ancestor, "src/google/protobuf").exists()) { @@ -3809,15 +3778,13 @@ public final class TestUtil { } throw new RuntimeException( - "Could not find golden files. This test must be run from within the " + - "protobuf source package so that it can read test data files from the " + - "C++ source tree: " + initialPath); + "Could not find golden files. This test must be run from within the " + + "protobuf source package so that it can read test data files from the " + + "C++ source tree: " + + initialPath); } - /** - * @param filename The path relative to - * {@link #getTestDataDir}. - */ + /** @param filename The path relative to {@link #getTestDataDir}. */ public static ByteString readBytesFromFile(String filename) { File fullPath = new File(getTestDataDir(), filename); try { @@ -3828,8 +3795,7 @@ public final class TestUtil { } catch (IOException e) { // Throw a RuntimeException here so that we can call this function from // static initializers. - throw new IllegalArgumentException( - "Couldn't read file: " + fullPath.getPath(), e); + throw new IllegalArgumentException("Couldn't read file: " + fullPath.getPath(), e); } } // END FULL-RUNTIME @@ -3844,27 +3810,26 @@ public final class TestUtil { } /** - * Get the bytes of the "golden message". This is a serialized TestAllTypes - * with all fields set as they would be by - * {@link #setAllFields(TestAllTypes.Builder)}, but it is loaded from a file - * on disk rather than generated dynamically. The file is actually generated - * by C++ code, so testing against it verifies compatibility with C++. + * Get the bytes of the "golden message". This is a serialized TestAllTypes with all fields set as + * they would be by {@link #setAllFields(TestAllTypes.Builder)}, but it is loaded from a file on + * disk rather than generated dynamically. The file is actually generated by C++ code, so testing + * against it verifies compatibility with C++. */ public static ByteString getGoldenMessage() { if (goldenMessage == null) { - goldenMessage = readBytesFromResource("/google/protobuf/testdata/golden_message_oneof_implemented"); + goldenMessage = + readBytesFromResource("/google/protobuf/testdata/golden_message_oneof_implemented"); } return goldenMessage; } + private static ByteString goldenMessage = null; /** - * Get the bytes of the "golden packed fields message". This is a serialized - * TestPackedTypes with all fields set as they would be by - * {@link #setPackedFields(TestPackedTypes.Builder)}, but it is loaded from a - * file on disk rather than generated dynamically. The file is actually - * generated by C++ code, so testing against it verifies compatibility with - * C++. + * Get the bytes of the "golden packed fields message". This is a serialized TestPackedTypes with + * all fields set as they would be by {@link #setPackedFields(TestPackedTypes.Builder)}, but it is + * loaded from a file on disk rather than generated dynamically. The file is actually generated by + * C++ code, so testing against it verifies compatibility with C++. */ public static ByteString getGoldenPackedFieldsMessage() { if (goldenPackedFieldsMessage == null) { @@ -3873,6 +3838,7 @@ public final class TestUtil { } return goldenPackedFieldsMessage; } + private static ByteString goldenPackedFieldsMessage = null; // BEGIN FULL-RUNTIME @@ -3881,8 +3847,7 @@ public final class TestUtil { * * @author jonp@google.com (Jon Perlow) */ - public static class MockBuilderParent - implements GeneratedMessage.BuilderParent { + public static class MockBuilderParent implements GeneratedMessage.BuilderParent { private int invalidations; @@ -3896,4 +3861,28 @@ public final class TestUtil { } } // END FULL-RUNTIME + + /** Helper class to test logged messages */ + public static class TestLogHandler extends Handler { + /** We will keep a private list of all logged records */ + private final List list = new ArrayList<>(); + + /** Adds the most recently logged record to our list. */ + @Override + public synchronized void publish(LogRecord record) { + list.add(record); + } + + @Override + public void flush() {} + + @Override + public void close() {} + + /** Returns a snapshot of the logged records. */ + public synchronized List getStoredLogRecords() { + List result = new ArrayList<>(list); + return Collections.unmodifiableList(result); + } + } } diff --git a/java/core/src/test/java/com/google/protobuf/TestUtilLite.java b/java/core/src/test/java/com/google/protobuf/TestUtilLite.java index 8f33fa14ab..31565fc40a 100644 --- a/java/core/src/test/java/com/google/protobuf/TestUtilLite.java +++ b/java/core/src/test/java/com/google/protobuf/TestUtilLite.java @@ -30,8 +30,6 @@ package com.google.protobuf; -import static com.google.protobuf.UnittestLite.OptionalGroup_extension_lite; -import static com.google.protobuf.UnittestLite.RepeatedGroup_extension_lite; import static com.google.protobuf.UnittestLite.defaultBoolExtensionLite; import static com.google.protobuf.UnittestLite.defaultBytesExtensionLite; import static com.google.protobuf.UnittestLite.defaultCordExtensionLite; @@ -127,6 +125,8 @@ import com.google.protobuf.UnittestImportLite.ImportMessageLite; import com.google.protobuf.UnittestImportPublicLite.PublicImportMessageLite; import com.google.protobuf.UnittestLite.ForeignEnumLite; import com.google.protobuf.UnittestLite.ForeignMessageLite; +import com.google.protobuf.UnittestLite.OptionalGroup_extension_lite; +import com.google.protobuf.UnittestLite.RepeatedGroup_extension_lite; import com.google.protobuf.UnittestLite.TestAllExtensionsLite; import com.google.protobuf.UnittestLite.TestAllTypesLite; import com.google.protobuf.UnittestLite.TestPackedExtensionsLite; @@ -136,8 +136,7 @@ import com.google.protobuf.UnittestLite.TestPackedExtensionsLite; * and {@code TestPackedExtensionsLite}. This is analogous to the functionality in TestUtil.java but * does not depend on the presence of any non-lite protos. * - *

This code is not to be used outside of {@code com.google.protobuf} and - * subpackages. + *

This code is not to be used outside of {@code com.google.protobuf} and subpackages. */ public final class TestUtilLite { private TestUtilLite() {} @@ -148,8 +147,8 @@ public final class TestUtilLite { } /** - * Get a {@code TestAllTypesLite.Builder} with all fields set as they would be by - * {@link #setAllFields(TestAllTypesLite.Builder)}. + * Get a {@code TestAllTypesLite.Builder} with all fields set as they would be by {@link + * #setAllFields(TestAllTypesLite.Builder)}. */ public static TestAllTypesLite.Builder getAllLiteSetBuilder() { TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder(); @@ -158,8 +157,8 @@ public final class TestUtilLite { } /** - * Get a {@code TestAllExtensionsLite} with all fields set as they would be by - * {@link #setAllExtensions(TestAllExtensionsLite.Builder)}. + * Get a {@code TestAllExtensionsLite} with all fields set as they would be by {@link + * #setAllExtensions(TestAllExtensionsLite.Builder)}. */ public static TestAllExtensionsLite getAllLiteExtensionsSet() { TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); @@ -172,154 +171,137 @@ public final class TestUtilLite { setPackedExtensions(builder); return builder.build(); } - - /** - * Set every field of {@code builder} to the values expected by - * {@code assertAllFieldsSet()}. - */ + + /** Set every field of {@code builder} to the values expected by {@code assertAllFieldsSet()}. */ public static void setAllFields(TestAllTypesLite.Builder builder) { - builder.setOptionalInt32 (101); - builder.setOptionalInt64 (102); - builder.setOptionalUint32 (103); - builder.setOptionalUint64 (104); - builder.setOptionalSint32 (105); - builder.setOptionalSint64 (106); - builder.setOptionalFixed32 (107); - builder.setOptionalFixed64 (108); + builder.setOptionalInt32(101); + builder.setOptionalInt64(102); + builder.setOptionalUint32(103); + builder.setOptionalUint64(104); + builder.setOptionalSint32(105); + builder.setOptionalSint64(106); + builder.setOptionalFixed32(107); + builder.setOptionalFixed64(108); builder.setOptionalSfixed32(109); builder.setOptionalSfixed64(110); - builder.setOptionalFloat (111); - builder.setOptionalDouble (112); - builder.setOptionalBool (true); - builder.setOptionalString ("115"); - builder.setOptionalBytes (toBytes("116")); - - builder.setOptionalGroup( - TestAllTypesLite.OptionalGroup.newBuilder().setA(117).build()); + builder.setOptionalFloat(111); + builder.setOptionalDouble(112); + builder.setOptionalBool(true); + builder.setOptionalString("115"); + builder.setOptionalBytes(toBytes("116")); + + builder.setOptionalGroup(TestAllTypesLite.OptionalGroup.newBuilder().setA(117).build()); builder.setOptionalNestedMessage( TestAllTypesLite.NestedMessage.newBuilder().setBb(118).build()); - builder.setOptionalForeignMessage( - ForeignMessageLite.newBuilder().setC(119).build()); - builder.setOptionalImportMessage( - ImportMessageLite.newBuilder().setD(120).build()); - builder.setOptionalPublicImportMessage( - PublicImportMessageLite.newBuilder().setE(126).build()); - builder.setOptionalLazyMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(127).build()); + builder.setOptionalForeignMessage(ForeignMessageLite.newBuilder().setC(119).build()); + builder.setOptionalImportMessage(ImportMessageLite.newBuilder().setD(120).build()); + builder.setOptionalPublicImportMessage(PublicImportMessageLite.newBuilder().setE(126).build()); + builder.setOptionalLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(127).build()); - builder.setOptionalNestedEnum (TestAllTypesLite.NestedEnum.BAZ); + builder.setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ); builder.setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAZ); - builder.setOptionalImportEnum (ImportEnumLite.IMPORT_LITE_BAZ); + builder.setOptionalImportEnum(ImportEnumLite.IMPORT_LITE_BAZ); builder.setOptionalStringPiece("124"); builder.setOptionalCord("125"); // ----------------------------------------------------------------- - builder.addRepeatedInt32 (201); - builder.addRepeatedInt64 (202); - builder.addRepeatedUint32 (203); - builder.addRepeatedUint64 (204); - builder.addRepeatedSint32 (205); - builder.addRepeatedSint64 (206); - builder.addRepeatedFixed32 (207); - builder.addRepeatedFixed64 (208); + builder.addRepeatedInt32(201); + builder.addRepeatedInt64(202); + builder.addRepeatedUint32(203); + builder.addRepeatedUint64(204); + builder.addRepeatedSint32(205); + builder.addRepeatedSint64(206); + builder.addRepeatedFixed32(207); + builder.addRepeatedFixed64(208); builder.addRepeatedSfixed32(209); builder.addRepeatedSfixed64(210); - builder.addRepeatedFloat (211); - builder.addRepeatedDouble (212); - builder.addRepeatedBool (true); - builder.addRepeatedString ("215"); - builder.addRepeatedBytes (toBytes("216")); - - builder.addRepeatedGroup( - TestAllTypesLite.RepeatedGroup.newBuilder().setA(217).build()); + builder.addRepeatedFloat(211); + builder.addRepeatedDouble(212); + builder.addRepeatedBool(true); + builder.addRepeatedString("215"); + builder.addRepeatedBytes(toBytes("216")); + + builder.addRepeatedGroup(TestAllTypesLite.RepeatedGroup.newBuilder().setA(217).build()); builder.addRepeatedNestedMessage( TestAllTypesLite.NestedMessage.newBuilder().setBb(218).build()); - builder.addRepeatedForeignMessage( - ForeignMessageLite.newBuilder().setC(219).build()); - builder.addRepeatedImportMessage( - ImportMessageLite.newBuilder().setD(220).build()); - builder.addRepeatedLazyMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(227).build()); + builder.addRepeatedForeignMessage(ForeignMessageLite.newBuilder().setC(219).build()); + builder.addRepeatedImportMessage(ImportMessageLite.newBuilder().setD(220).build()); + builder.addRepeatedLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(227).build()); - builder.addRepeatedNestedEnum (TestAllTypesLite.NestedEnum.BAR); + builder.addRepeatedNestedEnum(TestAllTypesLite.NestedEnum.BAR); builder.addRepeatedForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR); - builder.addRepeatedImportEnum (ImportEnumLite.IMPORT_LITE_BAR); + builder.addRepeatedImportEnum(ImportEnumLite.IMPORT_LITE_BAR); builder.addRepeatedStringPiece("224"); builder.addRepeatedCord("225"); // Add a second one of each field. - builder.addRepeatedInt32 (301); - builder.addRepeatedInt64 (302); - builder.addRepeatedUint32 (303); - builder.addRepeatedUint64 (304); - builder.addRepeatedSint32 (305); - builder.addRepeatedSint64 (306); - builder.addRepeatedFixed32 (307); - builder.addRepeatedFixed64 (308); + builder.addRepeatedInt32(301); + builder.addRepeatedInt64(302); + builder.addRepeatedUint32(303); + builder.addRepeatedUint64(304); + builder.addRepeatedSint32(305); + builder.addRepeatedSint64(306); + builder.addRepeatedFixed32(307); + builder.addRepeatedFixed64(308); builder.addRepeatedSfixed32(309); builder.addRepeatedSfixed64(310); - builder.addRepeatedFloat (311); - builder.addRepeatedDouble (312); - builder.addRepeatedBool (false); - builder.addRepeatedString ("315"); - builder.addRepeatedBytes (toBytes("316")); - - builder.addRepeatedGroup( - TestAllTypesLite.RepeatedGroup.newBuilder().setA(317).build()); + builder.addRepeatedFloat(311); + builder.addRepeatedDouble(312); + builder.addRepeatedBool(false); + builder.addRepeatedString("315"); + builder.addRepeatedBytes(toBytes("316")); + + builder.addRepeatedGroup(TestAllTypesLite.RepeatedGroup.newBuilder().setA(317).build()); builder.addRepeatedNestedMessage( TestAllTypesLite.NestedMessage.newBuilder().setBb(318).build()); - builder.addRepeatedForeignMessage( - ForeignMessageLite.newBuilder().setC(319).build()); - builder.addRepeatedImportMessage( - ImportMessageLite.newBuilder().setD(320).build()); - builder.addRepeatedLazyMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(327).build()); + builder.addRepeatedForeignMessage(ForeignMessageLite.newBuilder().setC(319).build()); + builder.addRepeatedImportMessage(ImportMessageLite.newBuilder().setD(320).build()); + builder.addRepeatedLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(327).build()); - builder.addRepeatedNestedEnum (TestAllTypesLite.NestedEnum.BAZ); + builder.addRepeatedNestedEnum(TestAllTypesLite.NestedEnum.BAZ); builder.addRepeatedForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAZ); - builder.addRepeatedImportEnum (ImportEnumLite.IMPORT_LITE_BAZ); + builder.addRepeatedImportEnum(ImportEnumLite.IMPORT_LITE_BAZ); builder.addRepeatedStringPiece("324"); builder.addRepeatedCord("325"); // ----------------------------------------------------------------- - builder.setDefaultInt32 (401); - builder.setDefaultInt64 (402); - builder.setDefaultUint32 (403); - builder.setDefaultUint64 (404); - builder.setDefaultSint32 (405); - builder.setDefaultSint64 (406); - builder.setDefaultFixed32 (407); - builder.setDefaultFixed64 (408); + builder.setDefaultInt32(401); + builder.setDefaultInt64(402); + builder.setDefaultUint32(403); + builder.setDefaultUint64(404); + builder.setDefaultSint32(405); + builder.setDefaultSint64(406); + builder.setDefaultFixed32(407); + builder.setDefaultFixed64(408); builder.setDefaultSfixed32(409); builder.setDefaultSfixed64(410); - builder.setDefaultFloat (411); - builder.setDefaultDouble (412); - builder.setDefaultBool (false); - builder.setDefaultString ("415"); - builder.setDefaultBytes (toBytes("416")); + builder.setDefaultFloat(411); + builder.setDefaultDouble(412); + builder.setDefaultBool(false); + builder.setDefaultString("415"); + builder.setDefaultBytes(toBytes("416")); - builder.setDefaultNestedEnum (TestAllTypesLite.NestedEnum.FOO); + builder.setDefaultNestedEnum(TestAllTypesLite.NestedEnum.FOO); builder.setDefaultForeignEnum(ForeignEnumLite.FOREIGN_LITE_FOO); - builder.setDefaultImportEnum (ImportEnumLite.IMPORT_LITE_FOO); + builder.setDefaultImportEnum(ImportEnumLite.IMPORT_LITE_FOO); builder.setDefaultStringPiece("424"); builder.setDefaultCord("425"); builder.setOneofUint32(601); - builder.setOneofNestedMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(602).build()); + builder.setOneofNestedMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(602).build()); builder.setOneofString("603"); builder.setOneofBytes(toBytes("604")); } /** - * Get an unmodifiable {@link ExtensionRegistryLite} containing all the - * extensions of {@code TestAllExtensionsLite}. + * Get an unmodifiable {@link ExtensionRegistryLite} containing all the extensions of {@code + * TestAllExtensionsLite}. */ public static ExtensionRegistryLite getExtensionRegistryLite() { ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); @@ -328,8 +310,8 @@ public final class TestUtilLite { } /** - * Register all of {@code TestAllExtensionsLite}'s extensions with the - * given {@link ExtensionRegistryLite}. + * Register all of {@code TestAllExtensionsLite}'s extensions with the given {@link + * ExtensionRegistryLite}. */ public static void registerAllExtensionsLite(ExtensionRegistryLite registry) { UnittestLite.registerAllExtensions(registry); @@ -339,38 +321,40 @@ public final class TestUtilLite { // Lite extensions /** - * Set every field of {@code message} to the values expected by - * {@code assertAllExtensionsSet()}. + * Set every field of {@code message} to the values expected by {@code assertAllExtensionsSet()}. */ public static void setAllExtensions(TestAllExtensionsLite.Builder message) { - message.setExtension(optionalInt32ExtensionLite , 101); - message.setExtension(optionalInt64ExtensionLite , 102L); - message.setExtension(optionalUint32ExtensionLite , 103); - message.setExtension(optionalUint64ExtensionLite , 104L); - message.setExtension(optionalSint32ExtensionLite , 105); - message.setExtension(optionalSint64ExtensionLite , 106L); - message.setExtension(optionalFixed32ExtensionLite , 107); - message.setExtension(optionalFixed64ExtensionLite , 108L); + message.setExtension(optionalInt32ExtensionLite, 101); + message.setExtension(optionalInt64ExtensionLite, 102L); + message.setExtension(optionalUint32ExtensionLite, 103); + message.setExtension(optionalUint64ExtensionLite, 104L); + message.setExtension(optionalSint32ExtensionLite, 105); + message.setExtension(optionalSint64ExtensionLite, 106L); + message.setExtension(optionalFixed32ExtensionLite, 107); + message.setExtension(optionalFixed64ExtensionLite, 108L); message.setExtension(optionalSfixed32ExtensionLite, 109); message.setExtension(optionalSfixed64ExtensionLite, 110L); - message.setExtension(optionalFloatExtensionLite , 111F); - message.setExtension(optionalDoubleExtensionLite , 112D); - message.setExtension(optionalBoolExtensionLite , true); - message.setExtension(optionalStringExtensionLite , "115"); - message.setExtension(optionalBytesExtensionLite , toBytes("116")); - - message.setExtension(optionalGroupExtensionLite, - OptionalGroup_extension_lite.newBuilder().setA(117).build()); - message.setExtension(optionalNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(118).build()); - message.setExtension(optionalForeignMessageExtensionLite, - ForeignMessageLite.newBuilder().setC(119).build()); - message.setExtension(optionalImportMessageExtensionLite, - ImportMessageLite.newBuilder().setD(120).build()); - message.setExtension(optionalPublicImportMessageExtensionLite, - PublicImportMessageLite.newBuilder().setE(126).build()); - message.setExtension(optionalLazyMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(127).build()); + message.setExtension(optionalFloatExtensionLite, 111F); + message.setExtension(optionalDoubleExtensionLite, 112D); + message.setExtension(optionalBoolExtensionLite, true); + message.setExtension(optionalStringExtensionLite, "115"); + message.setExtension(optionalBytesExtensionLite, toBytes("116")); + + message.setExtension( + optionalGroupExtensionLite, OptionalGroup_extension_lite.newBuilder().setA(117).build()); + message.setExtension( + optionalNestedMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(118).build()); + message.setExtension( + optionalForeignMessageExtensionLite, ForeignMessageLite.newBuilder().setC(119).build()); + message.setExtension( + optionalImportMessageExtensionLite, ImportMessageLite.newBuilder().setD(120).build()); + message.setExtension( + optionalPublicImportMessageExtensionLite, + PublicImportMessageLite.newBuilder().setE(126).build()); + message.setExtension( + optionalLazyMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(127).build()); message.setExtension(optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ); message.setExtension(optionalForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ); @@ -381,32 +365,34 @@ public final class TestUtilLite { // ----------------------------------------------------------------- - message.addExtension(repeatedInt32ExtensionLite , 201); - message.addExtension(repeatedInt64ExtensionLite , 202L); - message.addExtension(repeatedUint32ExtensionLite , 203); - message.addExtension(repeatedUint64ExtensionLite , 204L); - message.addExtension(repeatedSint32ExtensionLite , 205); - message.addExtension(repeatedSint64ExtensionLite , 206L); - message.addExtension(repeatedFixed32ExtensionLite , 207); - message.addExtension(repeatedFixed64ExtensionLite , 208L); + message.addExtension(repeatedInt32ExtensionLite, 201); + message.addExtension(repeatedInt64ExtensionLite, 202L); + message.addExtension(repeatedUint32ExtensionLite, 203); + message.addExtension(repeatedUint64ExtensionLite, 204L); + message.addExtension(repeatedSint32ExtensionLite, 205); + message.addExtension(repeatedSint64ExtensionLite, 206L); + message.addExtension(repeatedFixed32ExtensionLite, 207); + message.addExtension(repeatedFixed64ExtensionLite, 208L); message.addExtension(repeatedSfixed32ExtensionLite, 209); message.addExtension(repeatedSfixed64ExtensionLite, 210L); - message.addExtension(repeatedFloatExtensionLite , 211F); - message.addExtension(repeatedDoubleExtensionLite , 212D); - message.addExtension(repeatedBoolExtensionLite , true); - message.addExtension(repeatedStringExtensionLite , "215"); - message.addExtension(repeatedBytesExtensionLite , toBytes("216")); - - message.addExtension(repeatedGroupExtensionLite, - RepeatedGroup_extension_lite.newBuilder().setA(217).build()); - message.addExtension(repeatedNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(218).build()); - message.addExtension(repeatedForeignMessageExtensionLite, - ForeignMessageLite.newBuilder().setC(219).build()); - message.addExtension(repeatedImportMessageExtensionLite, - ImportMessageLite.newBuilder().setD(220).build()); - message.addExtension(repeatedLazyMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(227).build()); + message.addExtension(repeatedFloatExtensionLite, 211F); + message.addExtension(repeatedDoubleExtensionLite, 212D); + message.addExtension(repeatedBoolExtensionLite, true); + message.addExtension(repeatedStringExtensionLite, "215"); + message.addExtension(repeatedBytesExtensionLite, toBytes("216")); + + message.addExtension( + repeatedGroupExtensionLite, RepeatedGroup_extension_lite.newBuilder().setA(217).build()); + message.addExtension( + repeatedNestedMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(218).build()); + message.addExtension( + repeatedForeignMessageExtensionLite, ForeignMessageLite.newBuilder().setC(219).build()); + message.addExtension( + repeatedImportMessageExtensionLite, ImportMessageLite.newBuilder().setD(220).build()); + message.addExtension( + repeatedLazyMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(227).build()); message.addExtension(repeatedNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAR); message.addExtension(repeatedForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAR); @@ -416,32 +402,34 @@ public final class TestUtilLite { message.addExtension(repeatedCordExtensionLite, "225"); // Add a second one of each field. - message.addExtension(repeatedInt32ExtensionLite , 301); - message.addExtension(repeatedInt64ExtensionLite , 302L); - message.addExtension(repeatedUint32ExtensionLite , 303); - message.addExtension(repeatedUint64ExtensionLite , 304L); - message.addExtension(repeatedSint32ExtensionLite , 305); - message.addExtension(repeatedSint64ExtensionLite , 306L); - message.addExtension(repeatedFixed32ExtensionLite , 307); - message.addExtension(repeatedFixed64ExtensionLite , 308L); + message.addExtension(repeatedInt32ExtensionLite, 301); + message.addExtension(repeatedInt64ExtensionLite, 302L); + message.addExtension(repeatedUint32ExtensionLite, 303); + message.addExtension(repeatedUint64ExtensionLite, 304L); + message.addExtension(repeatedSint32ExtensionLite, 305); + message.addExtension(repeatedSint64ExtensionLite, 306L); + message.addExtension(repeatedFixed32ExtensionLite, 307); + message.addExtension(repeatedFixed64ExtensionLite, 308L); message.addExtension(repeatedSfixed32ExtensionLite, 309); message.addExtension(repeatedSfixed64ExtensionLite, 310L); - message.addExtension(repeatedFloatExtensionLite , 311F); - message.addExtension(repeatedDoubleExtensionLite , 312D); - message.addExtension(repeatedBoolExtensionLite , false); - message.addExtension(repeatedStringExtensionLite , "315"); - message.addExtension(repeatedBytesExtensionLite , toBytes("316")); - - message.addExtension(repeatedGroupExtensionLite, - RepeatedGroup_extension_lite.newBuilder().setA(317).build()); - message.addExtension(repeatedNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(318).build()); - message.addExtension(repeatedForeignMessageExtensionLite, - ForeignMessageLite.newBuilder().setC(319).build()); - message.addExtension(repeatedImportMessageExtensionLite, - ImportMessageLite.newBuilder().setD(320).build()); - message.addExtension(repeatedLazyMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(327).build()); + message.addExtension(repeatedFloatExtensionLite, 311F); + message.addExtension(repeatedDoubleExtensionLite, 312D); + message.addExtension(repeatedBoolExtensionLite, false); + message.addExtension(repeatedStringExtensionLite, "315"); + message.addExtension(repeatedBytesExtensionLite, toBytes("316")); + + message.addExtension( + repeatedGroupExtensionLite, RepeatedGroup_extension_lite.newBuilder().setA(317).build()); + message.addExtension( + repeatedNestedMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(318).build()); + message.addExtension( + repeatedForeignMessageExtensionLite, ForeignMessageLite.newBuilder().setC(319).build()); + message.addExtension( + repeatedImportMessageExtensionLite, ImportMessageLite.newBuilder().setD(320).build()); + message.addExtension( + repeatedLazyMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(327).build()); message.addExtension(repeatedNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ); message.addExtension(repeatedForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ); @@ -452,21 +440,21 @@ public final class TestUtilLite { // ----------------------------------------------------------------- - message.setExtension(defaultInt32ExtensionLite , 401); - message.setExtension(defaultInt64ExtensionLite , 402L); - message.setExtension(defaultUint32ExtensionLite , 403); - message.setExtension(defaultUint64ExtensionLite , 404L); - message.setExtension(defaultSint32ExtensionLite , 405); - message.setExtension(defaultSint64ExtensionLite , 406L); - message.setExtension(defaultFixed32ExtensionLite , 407); - message.setExtension(defaultFixed64ExtensionLite , 408L); + message.setExtension(defaultInt32ExtensionLite, 401); + message.setExtension(defaultInt64ExtensionLite, 402L); + message.setExtension(defaultUint32ExtensionLite, 403); + message.setExtension(defaultUint64ExtensionLite, 404L); + message.setExtension(defaultSint32ExtensionLite, 405); + message.setExtension(defaultSint64ExtensionLite, 406L); + message.setExtension(defaultFixed32ExtensionLite, 407); + message.setExtension(defaultFixed64ExtensionLite, 408L); message.setExtension(defaultSfixed32ExtensionLite, 409); message.setExtension(defaultSfixed64ExtensionLite, 410L); - message.setExtension(defaultFloatExtensionLite , 411F); - message.setExtension(defaultDoubleExtensionLite , 412D); - message.setExtension(defaultBoolExtensionLite , false); - message.setExtension(defaultStringExtensionLite , "415"); - message.setExtension(defaultBytesExtensionLite , toBytes("416")); + message.setExtension(defaultFloatExtensionLite, 411F); + message.setExtension(defaultDoubleExtensionLite, 412D); + message.setExtension(defaultBoolExtensionLite, false); + message.setExtension(defaultStringExtensionLite, "415"); + message.setExtension(defaultBytesExtensionLite, toBytes("416")); message.setExtension(defaultNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.FOO); message.setExtension(defaultForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_FOO); @@ -476,8 +464,9 @@ public final class TestUtilLite { message.setExtension(defaultCordExtensionLite, "425"); message.setExtension(oneofUint32ExtensionLite, 601); - message.setExtension(oneofNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(602).build()); + message.setExtension( + oneofNestedMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(602).build()); message.setExtension(oneofStringExtensionLite, "603"); message.setExtension(oneofBytesExtensionLite, toBytes("604")); } @@ -485,75 +474,78 @@ public final class TestUtilLite { // ------------------------------------------------------------------- /** - * Modify the repeated extensions of {@code message} to contain the values - * expected by {@code assertRepeatedExtensionsModified()}. + * Modify the repeated extensions of {@code message} to contain the values expected by {@code + * assertRepeatedExtensionsModified()}. */ - public static void modifyRepeatedExtensions( - TestAllExtensionsLite.Builder message) { - message.setExtension(repeatedInt32ExtensionLite , 1, 501); - message.setExtension(repeatedInt64ExtensionLite , 1, 502L); - message.setExtension(repeatedUint32ExtensionLite , 1, 503); - message.setExtension(repeatedUint64ExtensionLite , 1, 504L); - message.setExtension(repeatedSint32ExtensionLite , 1, 505); - message.setExtension(repeatedSint64ExtensionLite , 1, 506L); - message.setExtension(repeatedFixed32ExtensionLite , 1, 507); - message.setExtension(repeatedFixed64ExtensionLite , 1, 508L); + public static void modifyRepeatedExtensions(TestAllExtensionsLite.Builder message) { + message.setExtension(repeatedInt32ExtensionLite, 1, 501); + message.setExtension(repeatedInt64ExtensionLite, 1, 502L); + message.setExtension(repeatedUint32ExtensionLite, 1, 503); + message.setExtension(repeatedUint64ExtensionLite, 1, 504L); + message.setExtension(repeatedSint32ExtensionLite, 1, 505); + message.setExtension(repeatedSint64ExtensionLite, 1, 506L); + message.setExtension(repeatedFixed32ExtensionLite, 1, 507); + message.setExtension(repeatedFixed64ExtensionLite, 1, 508L); message.setExtension(repeatedSfixed32ExtensionLite, 1, 509); message.setExtension(repeatedSfixed64ExtensionLite, 1, 510L); - message.setExtension(repeatedFloatExtensionLite , 1, 511F); - message.setExtension(repeatedDoubleExtensionLite , 1, 512D); - message.setExtension(repeatedBoolExtensionLite , 1, true); - message.setExtension(repeatedStringExtensionLite , 1, "515"); - message.setExtension(repeatedBytesExtensionLite , 1, toBytes("516")); - - message.setExtension(repeatedGroupExtensionLite, 1, - RepeatedGroup_extension_lite.newBuilder().setA(517).build()); - message.setExtension(repeatedNestedMessageExtensionLite, 1, - TestAllTypesLite.NestedMessage.newBuilder().setBb(518).build()); - message.setExtension(repeatedForeignMessageExtensionLite, 1, - ForeignMessageLite.newBuilder().setC(519).build()); - message.setExtension(repeatedImportMessageExtensionLite, 1, - ImportMessageLite.newBuilder().setD(520).build()); - message.setExtension(repeatedLazyMessageExtensionLite, 1, - TestAllTypesLite.NestedMessage.newBuilder().setBb(527).build()); - - message.setExtension(repeatedNestedEnumExtensionLite , 1, TestAllTypesLite.NestedEnum.FOO); + message.setExtension(repeatedFloatExtensionLite, 1, 511F); + message.setExtension(repeatedDoubleExtensionLite, 1, 512D); + message.setExtension(repeatedBoolExtensionLite, 1, true); + message.setExtension(repeatedStringExtensionLite, 1, "515"); + message.setExtension(repeatedBytesExtensionLite, 1, toBytes("516")); + + message.setExtension( + repeatedGroupExtensionLite, 1, RepeatedGroup_extension_lite.newBuilder().setA(517).build()); + message.setExtension( + repeatedNestedMessageExtensionLite, + 1, + TestAllTypesLite.NestedMessage.newBuilder().setBb(518).build()); + message.setExtension( + repeatedForeignMessageExtensionLite, 1, ForeignMessageLite.newBuilder().setC(519).build()); + message.setExtension( + repeatedImportMessageExtensionLite, 1, ImportMessageLite.newBuilder().setD(520).build()); + message.setExtension( + repeatedLazyMessageExtensionLite, + 1, + TestAllTypesLite.NestedMessage.newBuilder().setBb(527).build()); + + message.setExtension(repeatedNestedEnumExtensionLite, 1, TestAllTypesLite.NestedEnum.FOO); message.setExtension(repeatedForeignEnumExtensionLite, 1, ForeignEnumLite.FOREIGN_LITE_FOO); - message.setExtension(repeatedImportEnumExtensionLite , 1, ImportEnumLite.IMPORT_LITE_FOO); + message.setExtension(repeatedImportEnumExtensionLite, 1, ImportEnumLite.IMPORT_LITE_FOO); message.setExtension(repeatedStringPieceExtensionLite, 1, "524"); message.setExtension(repeatedCordExtensionLite, 1, "525"); } public static void setPackedExtensions(TestPackedExtensionsLite.Builder message) { - message.addExtension(packedInt32ExtensionLite , 601); - message.addExtension(packedInt64ExtensionLite , 602L); - message.addExtension(packedUint32ExtensionLite , 603); - message.addExtension(packedUint64ExtensionLite , 604L); - message.addExtension(packedSint32ExtensionLite , 605); - message.addExtension(packedSint64ExtensionLite , 606L); - message.addExtension(packedFixed32ExtensionLite , 607); - message.addExtension(packedFixed64ExtensionLite , 608L); + message.addExtension(packedInt32ExtensionLite, 601); + message.addExtension(packedInt64ExtensionLite, 602L); + message.addExtension(packedUint32ExtensionLite, 603); + message.addExtension(packedUint64ExtensionLite, 604L); + message.addExtension(packedSint32ExtensionLite, 605); + message.addExtension(packedSint64ExtensionLite, 606L); + message.addExtension(packedFixed32ExtensionLite, 607); + message.addExtension(packedFixed64ExtensionLite, 608L); message.addExtension(packedSfixed32ExtensionLite, 609); message.addExtension(packedSfixed64ExtensionLite, 610L); - message.addExtension(packedFloatExtensionLite , 611F); - message.addExtension(packedDoubleExtensionLite , 612D); - message.addExtension(packedBoolExtensionLite , true); + message.addExtension(packedFloatExtensionLite, 611F); + message.addExtension(packedDoubleExtensionLite, 612D); + message.addExtension(packedBoolExtensionLite, true); message.addExtension(packedEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAR); // Add a second one of each field. - message.addExtension(packedInt32ExtensionLite , 701); - message.addExtension(packedInt64ExtensionLite , 702L); - message.addExtension(packedUint32ExtensionLite , 703); - message.addExtension(packedUint64ExtensionLite , 704L); - message.addExtension(packedSint32ExtensionLite , 705); - message.addExtension(packedSint64ExtensionLite , 706L); - message.addExtension(packedFixed32ExtensionLite , 707); - message.addExtension(packedFixed64ExtensionLite , 708L); + message.addExtension(packedInt32ExtensionLite, 701); + message.addExtension(packedInt64ExtensionLite, 702L); + message.addExtension(packedUint32ExtensionLite, 703); + message.addExtension(packedUint64ExtensionLite, 704L); + message.addExtension(packedSint32ExtensionLite, 705); + message.addExtension(packedSint64ExtensionLite, 706L); + message.addExtension(packedFixed32ExtensionLite, 707); + message.addExtension(packedFixed64ExtensionLite, 708L); message.addExtension(packedSfixed32ExtensionLite, 709); message.addExtension(packedSfixed64ExtensionLite, 710L); - message.addExtension(packedFloatExtensionLite , 711F); - message.addExtension(packedDoubleExtensionLite , 712D); - message.addExtension(packedBoolExtensionLite , false); + message.addExtension(packedFloatExtensionLite, 711F); + message.addExtension(packedDoubleExtensionLite, 712D); + message.addExtension(packedBoolExtensionLite, false); message.addExtension(packedEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ); } } diff --git a/java/core/src/test/java/com/google/protobuf/TextFormatParseInfoTreeTest.java b/java/core/src/test/java/com/google/protobuf/TextFormatParseInfoTreeTest.java index e338af2112..ff413720b7 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatParseInfoTreeTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatParseInfoTreeTest.java @@ -33,12 +33,9 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import protobuf_unittest.UnittestProto.TestAllTypes; - import junit.framework.TestCase; -/** - * Test @{link TextFormatParseInfoTree}. - */ +/** Test @{link TextFormatParseInfoTree}. */ public class TextFormatParseInfoTreeTest extends TestCase { private static final Descriptor DESCRIPTOR = TestAllTypes.getDescriptor(); @@ -75,7 +72,7 @@ public class TextFormatParseInfoTreeTest extends TestCase { TextFormatParseInfoTree root = rootBuilder.build(); assertEquals(LOC0, root.getLocation(OPTIONAL_INT32, 0)); assertEquals(1, root.getLocations(OPTIONAL_INT32).size()); - } + } public void testGetLocationsReturnsNoParseLocationsForUnknownField() { assertTrue(rootBuilder.build().getLocations(OPTIONAL_INT32).isEmpty()); diff --git a/java/core/src/test/java/com/google/protobuf/TextFormatParseLocationTest.java b/java/core/src/test/java/com/google/protobuf/TextFormatParseLocationTest.java index c42bfa6e71..19abc3f88f 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatParseLocationTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatParseLocationTest.java @@ -32,9 +32,7 @@ package com.google.protobuf; import junit.framework.TestCase; -/** - * Test @{link TextFormatParseLocation}. - */ +/** Test @{link TextFormatParseLocation}. */ public class TextFormatParseLocationTest extends TestCase { public void testCreateEmpty() { diff --git a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java index 24d5589f80..4421c0cc4b 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java @@ -49,12 +49,13 @@ import protobuf_unittest.UnittestProto.TestRequired; import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet; import java.io.StringReader; import java.util.List; +import java.util.logging.Logger; import junit.framework.TestCase; /** * Test case for {@link TextFormat}. * - * TODO(wenboz): ExtensionTest and rest of text_format_unittest.cc. + *

TODO(wenboz): ExtensionTest and rest of text_format_unittest.cc. * * @author wenboz@google.com (Wenbo Zhu) */ @@ -62,80 +63,91 @@ public class TextFormatTest extends TestCase { // A basic string with different escapable characters for testing. private static final String ESCAPE_TEST_STRING = - "\"A string with ' characters \n and \r newlines and \t tabs and \001 " + "slashes \\"; + "\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\"; // A representation of the above string with all the characters escaped. private static final String ESCAPE_TEST_STRING_ESCAPED = "\\\"A string with \\' characters \\n and \\r newlines " + "and \\t tabs and \\001 slashes \\\\"; - private static String allFieldsSetText = TestUtil.readTextFromFile( - "text_format_unittest_data_oneof_implemented.txt"); - private static String allExtensionsSetText = TestUtil.readTextFromFile( - "text_format_unittest_extensions_data.txt"); + private static String allFieldsSetText = + TestUtil.readTextFromFile("text_format_unittest_data_oneof_implemented.txt"); + private static String allExtensionsSetText = + TestUtil.readTextFromFile("text_format_unittest_extensions_data.txt"); private static String exoticText = - "repeated_int32: -1\n" + - "repeated_int32: -2147483648\n" + - "repeated_int64: -1,\n" + - "repeated_int64: -9223372036854775808\n" + - "repeated_uint32: 4294967295\n" + - "repeated_uint32: 2147483648\n" + - "repeated_uint64: 18446744073709551615\n" + - "repeated_uint64: 9223372036854775808\n" + - "repeated_double: 123.0\n" + - "repeated_double: 123.5\n" + - "repeated_double: 0.125\n" + - "repeated_double: .125\n" + - "repeated_double: -.125\n" + - "repeated_double: 1.23E17\n" + - "repeated_double: 1.23E+17\n" + - "repeated_double: -1.23e-17\n" + - "repeated_double: .23e+17\n" + - "repeated_double: -.23E17\n" + - "repeated_double: 1.235E22\n" + - "repeated_double: 1.235E-18\n" + - "repeated_double: 123.456789\n" + - "repeated_double: Infinity\n" + - "repeated_double: -Infinity\n" + - "repeated_double: NaN\n" + - "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"" + - "\\341\\210\\264\"\n" + - "repeated_bytes: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\376\"\n"; + "" + + "repeated_int32: -1\n" + + "repeated_int32: -2147483648\n" + + "repeated_int64: -1,\n" + + "repeated_int64: -9223372036854775808\n" + + "repeated_uint32: 4294967295\n" + + "repeated_uint32: 2147483648\n" + + "repeated_uint64: 18446744073709551615\n" + + "repeated_uint64: 9223372036854775808\n" + + "repeated_double: 123.0\n" + + "repeated_double: 123.5\n" + + "repeated_double: 0.125\n" + + "repeated_double: .125\n" + + "repeated_double: -.125\n" + + "repeated_double: 1.23E17\n" + + "repeated_double: 1.23E+17\n" + + "repeated_double: -1.23e-17\n" + + "repeated_double: .23e+17\n" + + "repeated_double: -.23E17\n" + + "repeated_double: 1.235E22\n" + + "repeated_double: 1.235E-18\n" + + "repeated_double: 123.456789\n" + + "repeated_double: Infinity\n" + + "repeated_double: -Infinity\n" + + "repeated_double: NaN\n" + + "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"" + + "\\341\\210\\264\"\n" + + "repeated_bytes: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\376\"\n"; private static String canonicalExoticText = - exoticText.replace(": .", ": 0.").replace(": -.", ": -0.") // short-form double - .replace("23e", "23E").replace("E+", "E").replace("0.23E17", "2.3E16").replace(",", ""); + exoticText + .replace(": .", ": 0.") + .replace(": -.", ": -0.") // short-form double + .replace("23e", "23E") + .replace("E+", "E") + .replace("0.23E17", "2.3E16") + .replace(",", ""); private String messageSetText = - "[protobuf_unittest.TestMessageSetExtension1] {\n" + - " i: 123\n" + - "}\n" + - "[protobuf_unittest.TestMessageSetExtension2] {\n" + - " str: \"foo\"\n" + - "}\n"; + "" + + "[protobuf_unittest.TestMessageSetExtension1] {\n" + + " i: 123\n" + + "}\n" + + "[protobuf_unittest.TestMessageSetExtension2] {\n" + + " str: \"foo\"\n" + + "}\n"; private String messageSetTextWithRepeatedExtension = - "[protobuf_unittest.TestMessageSetExtension1] {\n" + - " i: 123\n" + - "}\n" + - "[protobuf_unittest.TestMessageSetExtension1] {\n" + - " i: 456\n" + - "}\n"; + "" + + "[protobuf_unittest.TestMessageSetExtension1] {\n" + + " i: 123\n" + + "}\n" + + "[protobuf_unittest.TestMessageSetExtension1] {\n" + + " i: 456\n" + + "}\n"; + private final TextFormat.Parser parserAllowingUnknownFields = + TextFormat.Parser.newBuilder().setAllowUnknownFields(true).build(); + + private final TextFormat.Parser parserAllowingUnknownExtensions = + TextFormat.Parser.newBuilder().setAllowUnknownExtensions(true).build(); private final TextFormat.Parser parserWithOverwriteForbidden = TextFormat.Parser.newBuilder() - .setSingularOverwritePolicy( - SingularOverwritePolicy.FORBID_SINGULAR_OVERWRITES) + .setSingularOverwritePolicy(SingularOverwritePolicy.FORBID_SINGULAR_OVERWRITES) .build(); - private final TextFormat.Parser defaultParser = - TextFormat.Parser.newBuilder().build(); + private final TextFormat.Parser defaultParser = TextFormat.Parser.newBuilder().build(); /** Print TestAllTypes and compare with golden file. */ public void testPrintMessage() throws Exception { - String javaText = TextFormat.printToString(TestUtil.getAllSet()); + String javaText = TextFormat.printer().printToString(TestUtil.getAllSet()); // Java likes to add a trailing ".0" to floats and doubles. C printf // (with %g format) does not. Our golden files are used for both @@ -147,7 +159,7 @@ public class TextFormatTest extends TestCase { /** Print TestAllTypes as Builder and compare with golden file. */ public void testPrintMessageBuilder() throws Exception { - String javaText = TextFormat.printToString(TestUtil.getAllSetBuilder()); + String javaText = TextFormat.printer().printToString(TestUtil.getAllSetBuilder()); // Java likes to add a trailing ".0" to floats and doubles. C printf // (with %g format) does not. Our golden files are used for both @@ -159,7 +171,7 @@ public class TextFormatTest extends TestCase { /** Print TestAllExtensions and compare with golden file. */ public void testPrintExtensions() throws Exception { - String javaText = TextFormat.printToString(TestUtil.getAllExtensionsSet()); + String javaText = TextFormat.printer().printToString(TestUtil.getAllExtensionsSet()); // Java likes to add a trailing ".0" to floats and doubles. C printf // (with %g format) does not. Our golden files are used for both @@ -173,38 +185,32 @@ public class TextFormatTest extends TestCase { private UnknownFieldSet makeUnknownFieldSet() { return UnknownFieldSet.newBuilder() - .addField(5, + .addField( + 5, UnknownFieldSet.Field.newBuilder() - .addVarint(1) - .addFixed32(2) - .addFixed64(3) - .addLengthDelimited(ByteString.copyFromUtf8("4")) - .addLengthDelimited(UnknownFieldSet.newBuilder() - .addField(12, - UnknownFieldSet.Field.newBuilder() - .addVarint(6) + .addVarint(1) + .addFixed32(2) + .addFixed64(3) + .addLengthDelimited(ByteString.copyFromUtf8("4")) + .addLengthDelimited( + UnknownFieldSet.newBuilder() + .addField(12, UnknownFieldSet.Field.newBuilder().addVarint(6).build()) + .build() + .toByteString()) + .addGroup( + UnknownFieldSet.newBuilder() + .addField(10, UnknownFieldSet.Field.newBuilder().addVarint(5).build()) .build()) - .build().toByteString()) - .addGroup( - UnknownFieldSet.newBuilder() - .addField(10, - UnknownFieldSet.Field.newBuilder() - .addVarint(5) - .build()) .build()) - .build()) - .addField(8, - UnknownFieldSet.Field.newBuilder() - .addVarint(1) - .addVarint(2) - .addVarint(3) - .build()) - .addField(15, + .addField( + 8, UnknownFieldSet.Field.newBuilder().addVarint(1).addVarint(2).addVarint(3).build()) + .addField( + 15, UnknownFieldSet.Field.newBuilder() - .addVarint(0xABCDEF1234567890L) - .addFixed32(0xABCD1234) - .addFixed64(0xABCDEF1234567890L) - .build()) + .addVarint(0xABCDEF1234567890L) + .addFixed32(0xABCD1234) + .addFixed64(0xABCDEF1234567890L) + .build()) .build(); } @@ -212,9 +218,7 @@ public class TextFormatTest extends TestCase { // Test printing of unknown fields in a message. TestEmptyMessage message = - TestEmptyMessage.newBuilder() - .setUnknownFields(makeUnknownFieldSet()) - .build(); + TestEmptyMessage.newBuilder().setUnknownFields(makeUnknownFieldSet()).build(); assertEquals( "5: 1\n" @@ -233,38 +237,34 @@ public class TextFormatTest extends TestCase { + "15: 12379813812177893520\n" + "15: 0xabcd1234\n" + "15: 0xabcdef1234567890\n", - TextFormat.printToString(message)); + TextFormat.printer().printToString(message)); } public void testPrintField() throws Exception { - final FieldDescriptor dataField = - OneString.getDescriptor().findFieldByName("data"); + final FieldDescriptor dataField = OneString.getDescriptor().findFieldByName("data"); assertEquals( - "data: \"test data\"\n", - TextFormat.printFieldToString(dataField, "test data")); + "data: \"test data\"\n", TextFormat.printer().printFieldToString(dataField, "test data")); final FieldDescriptor optionalField = - TestAllTypes.getDescriptor().findFieldByName("optional_nested_message"); + TestAllTypes.getDescriptor().findFieldByName("optional_nested_message"); final Object value = NestedMessage.newBuilder().setBb(42).build(); assertEquals( - "optional_nested_message {\n bb: 42\n}\n", - TextFormat.printFieldToString(optionalField, value)); + "optional_nested_message {\n bb: 42\n}\n", + TextFormat.printer().printFieldToString(optionalField, value)); } /** - * Helper to construct a ByteString from a String containing only 8-bit - * characters. The characters are converted directly to bytes, *not* - * encoded using UTF-8. + * Helper to construct a ByteString from a String containing only 8-bit characters. The characters + * are converted directly to bytes, *not* encoded using UTF-8. */ private ByteString bytes(String str) { return ByteString.copyFrom(str.getBytes(Internal.ISO_8859_1)); } /** - * Helper to construct a ByteString from a bunch of bytes. The inputs are - * actually ints so that I can use hex notation and not get stupid errors - * about precision. + * Helper to construct a ByteString from a bunch of bytes. The inputs are actually ints so that I + * can use hex notation and not get stupid errors about precision. */ private ByteString bytes(int... bytesAsInts) { byte[] bytes = new byte[bytesAsInts.length]; @@ -275,54 +275,54 @@ public class TextFormatTest extends TestCase { } public void testPrintExotic() throws Exception { - Message message = TestAllTypes.newBuilder() - // Signed vs. unsigned numbers. - .addRepeatedInt32 (-1) - .addRepeatedUint32(-1) - .addRepeatedInt64 (-1) - .addRepeatedUint64(-1) - - .addRepeatedInt32 (1 << 31) - .addRepeatedUint32(1 << 31) - .addRepeatedInt64 (1L << 63) - .addRepeatedUint64(1L << 63) - - // Floats of various precisions and exponents. - .addRepeatedDouble(123) - .addRepeatedDouble(123.5) - .addRepeatedDouble(0.125) - .addRepeatedDouble(.125) - .addRepeatedDouble(-.125) - .addRepeatedDouble(123e15) - .addRepeatedDouble(123e15) - .addRepeatedDouble(-1.23e-17) - .addRepeatedDouble(.23e17) - .addRepeatedDouble(-23e15) - .addRepeatedDouble(123.5e20) - .addRepeatedDouble(123.5e-20) - .addRepeatedDouble(123.456789) - .addRepeatedDouble(Double.POSITIVE_INFINITY) - .addRepeatedDouble(Double.NEGATIVE_INFINITY) - .addRepeatedDouble(Double.NaN) - - // Strings and bytes that needing escaping. - .addRepeatedString("\0\001\007\b\f\n\r\t\013\\\'\"\u1234") - .addRepeatedBytes(bytes("\0\001\007\b\f\n\r\t\013\\\'\"\u00fe")) - .build(); + Message message = + TestAllTypes.newBuilder() + // Signed vs. unsigned numbers. + .addRepeatedInt32(-1) + .addRepeatedUint32(-1) + .addRepeatedInt64(-1) + .addRepeatedUint64(-1) + .addRepeatedInt32(1 << 31) + .addRepeatedUint32(1 << 31) + .addRepeatedInt64(1L << 63) + .addRepeatedUint64(1L << 63) + + // Floats of various precisions and exponents. + .addRepeatedDouble(123) + .addRepeatedDouble(123.5) + .addRepeatedDouble(0.125) + .addRepeatedDouble(.125) + .addRepeatedDouble(-.125) + .addRepeatedDouble(123e15) + .addRepeatedDouble(123e15) + .addRepeatedDouble(-1.23e-17) + .addRepeatedDouble(.23e17) + .addRepeatedDouble(-23e15) + .addRepeatedDouble(123.5e20) + .addRepeatedDouble(123.5e-20) + .addRepeatedDouble(123.456789) + .addRepeatedDouble(Double.POSITIVE_INFINITY) + .addRepeatedDouble(Double.NEGATIVE_INFINITY) + .addRepeatedDouble(Double.NaN) + + // Strings and bytes that needing escaping. + .addRepeatedString("\0\001\007\b\f\n\r\t\013\\\'\"\u1234") + .addRepeatedBytes(bytes("\0\001\007\b\f\n\r\t\013\\\'\"\u00fe")) + .build(); assertEquals(canonicalExoticText, message.toString()); } public void testPrintMessageSet() throws Exception { TestMessageSet messageSet = - TestMessageSet.newBuilder() - .setExtension( - TestMessageSetExtension1.messageSetExtension, - TestMessageSetExtension1.newBuilder().setI(123).build()) - .setExtension( - TestMessageSetExtension2.messageSetExtension, - TestMessageSetExtension2.newBuilder().setStr("foo").build()) - .build(); + TestMessageSet.newBuilder() + .setExtension( + TestMessageSetExtension1.messageSetExtension, + TestMessageSetExtension1.newBuilder().setI(123).build()) + .setExtension( + TestMessageSetExtension2.messageSetExtension, + TestMessageSetExtension2.newBuilder().setStr("foo").build()) + .build(); assertEquals(messageSetText, messageSet.toString()); } @@ -336,22 +336,19 @@ public class TextFormatTest extends TestCase { } public void testParse() throws Exception { - TestUtil.assertAllFieldsSet( - TextFormat.parse(allFieldsSetText, TestAllTypes.class)); + TestUtil.assertAllFieldsSet(TextFormat.parse(allFieldsSetText, TestAllTypes.class)); } public void testMergeInitialized() throws Exception { TestRequired.Builder builder = TestRequired.newBuilder(); TextFormat.merge(TEST_REQUIRED_INITIALIZED.toString(), builder); - assertEquals(TEST_REQUIRED_INITIALIZED.toString(), - builder.buildPartial().toString()); + assertEquals(TEST_REQUIRED_INITIALIZED.toString(), builder.buildPartial().toString()); assertTrue(builder.isInitialized()); } public void testParseInitialized() throws Exception { TestRequired parsed = - TextFormat.parse(TEST_REQUIRED_INITIALIZED.toString(), - TestRequired.class); + TextFormat.parse(TEST_REQUIRED_INITIALIZED.toString(), TestRequired.class); assertEquals(TEST_REQUIRED_INITIALIZED.toString(), parsed.toString()); assertTrue(parsed.isInitialized()); } @@ -359,15 +356,13 @@ public class TextFormatTest extends TestCase { public void testMergeUninitialized() throws Exception { TestRequired.Builder builder = TestRequired.newBuilder(); TextFormat.merge(TEST_REQUIRED_UNINITIALIZED.toString(), builder); - assertEquals(TEST_REQUIRED_UNINITIALIZED.toString(), - builder.buildPartial().toString()); + assertEquals(TEST_REQUIRED_UNINITIALIZED.toString(), builder.buildPartial().toString()); assertFalse(builder.isInitialized()); } public void testParseUninitialized() throws Exception { try { - TextFormat.parse(TEST_REQUIRED_UNINITIALIZED.toString(), - TestRequired.class); + TextFormat.parse(TEST_REQUIRED_UNINITIALIZED.toString(), TestRequired.class); fail("Expected UninitializedMessageException."); } catch (UninitializedMessageException e) { assertEquals("Message missing required fields: b, c", e.getMessage()); @@ -393,30 +388,32 @@ public class TextFormatTest extends TestCase { } public void testMergeAndParseCompatibility() throws Exception { - String original = "repeated_float: inf\n" + - "repeated_float: -inf\n" + - "repeated_float: nan\n" + - "repeated_float: inff\n" + - "repeated_float: -inff\n" + - "repeated_float: nanf\n" + - "repeated_float: 1.0f\n" + - "repeated_float: infinityf\n" + - "repeated_float: -Infinityf\n" + - "repeated_double: infinity\n" + - "repeated_double: -infinity\n" + - "repeated_double: nan\n"; - String canonical = "repeated_float: Infinity\n" + - "repeated_float: -Infinity\n" + - "repeated_float: NaN\n" + - "repeated_float: Infinity\n" + - "repeated_float: -Infinity\n" + - "repeated_float: NaN\n" + - "repeated_float: 1.0\n" + - "repeated_float: Infinity\n" + - "repeated_float: -Infinity\n" + - "repeated_double: Infinity\n" + - "repeated_double: -Infinity\n" + - "repeated_double: NaN\n"; + String original = + "repeated_float: inf\n" + + "repeated_float: -inf\n" + + "repeated_float: nan\n" + + "repeated_float: inff\n" + + "repeated_float: -inff\n" + + "repeated_float: nanf\n" + + "repeated_float: 1.0f\n" + + "repeated_float: infinityf\n" + + "repeated_float: -Infinityf\n" + + "repeated_double: infinity\n" + + "repeated_double: -infinity\n" + + "repeated_double: nan\n"; + String canonical = + "repeated_float: Infinity\n" + + "repeated_float: -Infinity\n" + + "repeated_float: NaN\n" + + "repeated_float: Infinity\n" + + "repeated_float: -Infinity\n" + + "repeated_float: NaN\n" + + "repeated_float: 1.0\n" + + "repeated_float: Infinity\n" + + "repeated_float: -Infinity\n" + + "repeated_double: Infinity\n" + + "repeated_double: -Infinity\n" + + "repeated_double: NaN\n"; // Test merge(). TestAllTypes.Builder builder = TestAllTypes.newBuilder(); @@ -424,8 +421,7 @@ public class TextFormatTest extends TestCase { assertEquals(canonical, builder.build().toString()); // Test parse(). - assertEquals(canonical, - TextFormat.parse(original, TestAllTypes.class).toString()); + assertEquals(canonical, TextFormat.parse(original, TestAllTypes.class).toString()); } public void testMergeAndParseExotic() throws Exception { @@ -435,8 +431,7 @@ public class TextFormatTest extends TestCase { // Too lazy to check things individually. Don't try to debug this // if testPrintExotic() is failing. assertEquals(canonicalExoticText, builder.build().toString()); - assertEquals(canonicalExoticText, - TextFormat.parse(exoticText, TestAllTypes.class).toString()); + assertEquals(canonicalExoticText, TextFormat.parse(exoticText, TestAllTypes.class).toString()); } public void testMergeMessageSet() throws Exception { @@ -448,21 +443,16 @@ public class TextFormatTest extends TestCase { TextFormat.merge(messageSetText, extensionRegistry, builder); TestMessageSet messageSet = builder.build(); - assertTrue(messageSet.hasExtension( - TestMessageSetExtension1.messageSetExtension)); - assertEquals(123, messageSet.getExtension( - TestMessageSetExtension1.messageSetExtension).getI()); - assertTrue(messageSet.hasExtension( - TestMessageSetExtension2.messageSetExtension)); - assertEquals("foo", messageSet.getExtension( - TestMessageSetExtension2.messageSetExtension).getStr()); + assertTrue(messageSet.hasExtension(TestMessageSetExtension1.messageSetExtension)); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + assertTrue(messageSet.hasExtension(TestMessageSetExtension2.messageSetExtension)); + assertEquals( + "foo", messageSet.getExtension(TestMessageSetExtension2.messageSetExtension).getStr()); builder = TestMessageSet.newBuilder(); - TextFormat.merge(messageSetTextWithRepeatedExtension, extensionRegistry, - builder); + TextFormat.merge(messageSetTextWithRepeatedExtension, extensionRegistry, builder); messageSet = builder.build(); - assertEquals(456, messageSet.getExtension( - TestMessageSetExtension1.messageSetExtension).getI()); + assertEquals(456, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); } public void testMergeMessageSetWithOverwriteForbidden() throws Exception { @@ -471,13 +461,11 @@ public class TextFormatTest extends TestCase { extensionRegistry.add(TestMessageSetExtension2.messageSetExtension); TestMessageSet.Builder builder = TestMessageSet.newBuilder(); - parserWithOverwriteForbidden.merge( - messageSetText, extensionRegistry, builder); + parserWithOverwriteForbidden.merge(messageSetText, extensionRegistry, builder); TestMessageSet messageSet = builder.build(); - assertEquals(123, messageSet.getExtension( - TestMessageSetExtension1.messageSetExtension).getI()); - assertEquals("foo", messageSet.getExtension( - TestMessageSetExtension2.messageSetExtension).getStr()); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + assertEquals( + "foo", messageSet.getExtension(TestMessageSetExtension2.messageSetExtension).getStr()); builder = TestMessageSet.newBuilder(); try { @@ -485,9 +473,10 @@ public class TextFormatTest extends TestCase { messageSetTextWithRepeatedExtension, extensionRegistry, builder); fail("expected parse exception"); } catch (TextFormat.ParseException e) { - assertEquals("6:1: Non-repeated field " - + "\"protobuf_unittest.TestMessageSetExtension1.message_set_extension\"" - + " cannot be overwritten.", + assertEquals( + "4:44: Non-repeated field " + + "\"protobuf_unittest.TestMessageSetExtension1.message_set_extension\"" + + " cannot be overwritten.", e.getMessage()); } } @@ -508,14 +497,16 @@ public class TextFormatTest extends TestCase { public void testMergeComment() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); TextFormat.merge( - "# this is a comment\n" + - "optional_int32: 1 # another comment\n" + - "optional_int64: 2\n" + - "# EOF comment", builder); + "# this is a comment\n" + + "optional_int32: 1 # another comment\n" + + "optional_int64: 2\n" + + "# EOF comment", + builder); assertEquals(1, builder.getOptionalInt32()); assertEquals(2, builder.getOptionalInt64()); } + private void assertParseError(String error, String text) { // Test merge(). TestAllTypes.Builder builder = TestAllTypes.newBuilder(); @@ -535,9 +526,41 @@ public class TextFormatTest extends TestCase { } } + private void assertParseErrorWithUnknownFields(String error, String text) { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + try { + parserAllowingUnknownFields.merge(text, TestUtil.getFullExtensionRegistry(), builder); + fail("Expected parse exception."); + } catch (TextFormat.ParseException e) { + assertEquals(error, e.getMessage()); + } + } + + private TestAllTypes assertParseSuccessWithUnknownFields(String text) + throws TextFormat.ParseException { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + parserAllowingUnknownFields.merge(text, TestUtil.getFullExtensionRegistry(), builder); + return builder.build(); + } - private void assertParseErrorWithOverwriteForbidden(String error, - String text) { + private void assertParseErrorWithUnknownExtensions(String error, String text) { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + try { + parserAllowingUnknownExtensions.merge(text, builder); + fail("Expected parse exception."); + } catch (TextFormat.ParseException e) { + assertEquals(error, e.getMessage()); + } + } + + private TestAllTypes assertParseSuccessWithUnknownExtensions(String text) + throws TextFormat.ParseException { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + parserAllowingUnknownExtensions.merge(text, builder); + return builder.build(); + } + + private void assertParseErrorWithOverwriteForbidden(String error, String text) { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); try { parserWithOverwriteForbidden.merge(text, TestUtil.getFullExtensionRegistry(), builder); @@ -547,118 +570,91 @@ public class TextFormatTest extends TestCase { } } - private TestAllTypes assertParseSuccessWithOverwriteForbidden( - String text) throws TextFormat.ParseException { + private TestAllTypes assertParseSuccessWithOverwriteForbidden(String text) + throws TextFormat.ParseException { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); parserWithOverwriteForbidden.merge(text, TestUtil.getFullExtensionRegistry(), builder); return builder.build(); } public void testParseErrors() throws Exception { + assertParseError("1:16: Expected \":\".", "optional_int32 123"); + assertParseError("1:23: Expected identifier. Found '?'", "optional_nested_enum: ?"); assertParseError( - "1:16: Expected \":\".", - "optional_int32 123"); - assertParseError( - "1:23: Expected identifier. Found '?'", - "optional_nested_enum: ?"); - assertParseError( - "1:18: Couldn't parse integer: Number must be positive: -1", - "optional_uint32: -1"); - assertParseError( - "1:17: Couldn't parse integer: Number out of range for 32-bit signed " + - "integer: 82301481290849012385230157", - "optional_int32: 82301481290849012385230157"); - assertParseError( - "1:16: Expected \"true\" or \"false\". Found \"maybe\".", - "optional_bool: maybe"); - assertParseError( - "1:16: Expected \"true\" or \"false\". Found \"2\".", - "optional_bool: 2"); - assertParseError( - "1:18: Expected string.", - "optional_string: 123"); - assertParseError( - "1:18: String missing ending quote.", - "optional_string: \"ueoauaoe"); + "1:18: Couldn't parse integer: Number must be positive: -1", "optional_uint32: -1"); assertParseError( - "1:18: String missing ending quote.", - "optional_string: \"ueoauaoe\n" + - "optional_int32: 123"); + "1:17: Couldn't parse integer: Number out of range for 32-bit signed " + + "integer: 82301481290849012385230157", + "optional_int32: 82301481290849012385230157"); assertParseError( - "1:18: Invalid escape sequence: '\\z'", - "optional_string: \"\\z\""); + "1:16: Expected \"true\" or \"false\". Found \"maybe\".", "optional_bool: maybe"); + assertParseError("1:16: Expected \"true\" or \"false\". Found \"2\".", "optional_bool: 2"); + assertParseError("1:18: Expected string.", "optional_string: 123"); + assertParseError("1:18: String missing ending quote.", "optional_string: \"ueoauaoe"); assertParseError( - "1:18: String missing ending quote.", - "optional_string: \"ueoauaoe\n" + - "optional_int32: 123"); + "1:18: String missing ending quote.", "optional_string: \"ueoauaoe\noptional_int32: 123"); + assertParseError("1:18: Invalid escape sequence: '\\z'", "optional_string: \"\\z\""); assertParseError( - "1:2: Input contains unknown fields and/or extensions:\n" + - "1:2:\tprotobuf_unittest.TestAllTypes.[nosuchext]", - "[nosuchext]: 123"); + "1:18: String missing ending quote.", "optional_string: \"ueoauaoe\noptional_int32: 123"); assertParseError( - "1:20: Extension \"protobuf_unittest.optional_int32_extension\" does " + - "not extend message type \"protobuf_unittest.TestAllTypes\".", - "[protobuf_unittest.optional_int32_extension]: 123"); + "1:2: Input contains unknown fields and/or extensions:\n" + + "1:2:\tprotobuf_unittest.TestAllTypes.[nosuchext]", + "[nosuchext]: 123"); assertParseError( - "1:1: Input contains unknown fields and/or extensions:\n" + - "1:1:\tprotobuf_unittest.TestAllTypes.nosuchfield", - "nosuchfield: 123"); + "1:20: Extension \"protobuf_unittest.optional_int32_extension\" does " + + "not extend message type \"protobuf_unittest.TestAllTypes\".", + "[protobuf_unittest.optional_int32_extension]: 123"); assertParseError( - "1:21: Expected \">\".", - "OptionalGroup < a: 1"); + "1:1: Input contains unknown fields and/or extensions:\n" + + "1:1:\tprotobuf_unittest.TestAllTypes.nosuchfield", + "nosuchfield: 123"); + assertParseError("1:21: Expected \">\".", "OptionalGroup < a: 1"); assertParseError( - "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " + - "value named \"NO_SUCH_VALUE\".", - "optional_nested_enum: NO_SUCH_VALUE"); + "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " + + "value named \"NO_SUCH_VALUE\".", + "optional_nested_enum: NO_SUCH_VALUE"); assertParseError( - "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " + - "value with number 123.", - "optional_nested_enum: 123"); + "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " + + "value with number 123.", + "optional_nested_enum: 123"); // Delimiters must match. - assertParseError( - "1:22: Expected identifier. Found '}'", - "OptionalGroup < a: 1 }"); - assertParseError( - "1:22: Expected identifier. Found '>'", - "OptionalGroup { a: 1 >"); + assertParseError("1:22: Expected identifier. Found '}'", "OptionalGroup < a: 1 }"); + assertParseError("1:22: Expected identifier. Found '>'", "OptionalGroup { a: 1 >"); } // ================================================================= public void testEscape() throws Exception { // Escape sequences. - assertEquals("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\177", - TextFormat.escapeBytes(bytes("\0\001\007\b\f\n\r\t\013\\\'\"\177"))); - assertEquals("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\177", - TextFormat.escapeText("\0\001\007\b\f\n\r\t\013\\\'\"\177")); - assertEquals(bytes("\0\001\007\b\f\n\r\t\013\\\'\""), - TextFormat.unescapeBytes("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"")); - assertEquals("\0\001\007\b\f\n\r\t\013\\\'\"", - TextFormat.unescapeText("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"")); + assertEquals( + "\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\177", + TextFormat.escapeBytes(bytes("\0\001\007\b\f\n\r\t\013\\\'\"\177"))); + assertEquals( + "\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\177", + TextFormat.escapeText("\0\001\007\b\f\n\r\t\013\\\'\"\177")); + assertEquals( + bytes("\0\001\007\b\f\n\r\t\013\\\'\""), + TextFormat.unescapeBytes("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"")); + assertEquals( + "\0\001\007\b\f\n\r\t\013\\\'\"", + TextFormat.unescapeText("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"")); assertEquals(ESCAPE_TEST_STRING_ESCAPED, TextFormat.escapeText(ESCAPE_TEST_STRING)); assertEquals(ESCAPE_TEST_STRING, TextFormat.unescapeText(ESCAPE_TEST_STRING_ESCAPED)); // Invariant - assertEquals("hello", - TextFormat.escapeBytes(bytes("hello"))); - assertEquals("hello", - TextFormat.escapeText("hello")); - assertEquals(bytes("hello"), - TextFormat.unescapeBytes("hello")); - assertEquals("hello", - TextFormat.unescapeText("hello")); + assertEquals("hello", TextFormat.escapeBytes(bytes("hello"))); + assertEquals("hello", TextFormat.escapeText("hello")); + assertEquals(bytes("hello"), TextFormat.unescapeBytes("hello")); + assertEquals("hello", TextFormat.unescapeText("hello")); // Unicode handling. assertEquals("\\341\\210\\264", TextFormat.escapeText("\u1234")); - assertEquals("\\341\\210\\264", - TextFormat.escapeBytes(bytes(0xe1, 0x88, 0xb4))); + assertEquals("\\341\\210\\264", TextFormat.escapeBytes(bytes(0xe1, 0x88, 0xb4))); assertEquals("\u1234", TextFormat.unescapeText("\\341\\210\\264")); - assertEquals(bytes(0xe1, 0x88, 0xb4), - TextFormat.unescapeBytes("\\341\\210\\264")); + assertEquals(bytes(0xe1, 0x88, 0xb4), TextFormat.unescapeBytes("\\341\\210\\264")); assertEquals("\u1234", TextFormat.unescapeText("\\xe1\\x88\\xb4")); - assertEquals(bytes(0xe1, 0x88, 0xb4), - TextFormat.unescapeBytes("\\xe1\\x88\\xb4")); + assertEquals(bytes(0xe1, 0x88, 0xb4), TextFormat.unescapeBytes("\\xe1\\x88\\xb4")); // Handling of strings with unescaped Unicode characters > 255. final String zh = "\u9999\u6e2f\u4e0a\u6d77\ud84f\udf80\u8c50\u9280\u884c"; @@ -689,53 +685,48 @@ public class TextFormatTest extends TestCase { } public void testParseInteger() throws Exception { - assertEquals( 0, TextFormat.parseInt32( "0")); - assertEquals( 1, TextFormat.parseInt32( "1")); - assertEquals( -1, TextFormat.parseInt32( "-1")); - assertEquals( 12345, TextFormat.parseInt32( "12345")); - assertEquals( -12345, TextFormat.parseInt32( "-12345")); - assertEquals( 2147483647, TextFormat.parseInt32( "2147483647")); + assertEquals(0, TextFormat.parseInt32("0")); + assertEquals(1, TextFormat.parseInt32("1")); + assertEquals(-1, TextFormat.parseInt32("-1")); + assertEquals(12345, TextFormat.parseInt32("12345")); + assertEquals(-12345, TextFormat.parseInt32("-12345")); + assertEquals(2147483647, TextFormat.parseInt32("2147483647")); assertEquals(-2147483648, TextFormat.parseInt32("-2147483648")); - assertEquals( 0, TextFormat.parseUInt32( "0")); - assertEquals( 1, TextFormat.parseUInt32( "1")); - assertEquals( 12345, TextFormat.parseUInt32( "12345")); - assertEquals( 2147483647, TextFormat.parseUInt32("2147483647")); + assertEquals(0, TextFormat.parseUInt32("0")); + assertEquals(1, TextFormat.parseUInt32("1")); + assertEquals(12345, TextFormat.parseUInt32("12345")); + assertEquals(2147483647, TextFormat.parseUInt32("2147483647")); assertEquals((int) 2147483648L, TextFormat.parseUInt32("2147483648")); assertEquals((int) 4294967295L, TextFormat.parseUInt32("4294967295")); - assertEquals( 0L, TextFormat.parseInt64( "0")); - assertEquals( 1L, TextFormat.parseInt64( "1")); - assertEquals( -1L, TextFormat.parseInt64( "-1")); - assertEquals( 12345L, TextFormat.parseInt64( "12345")); - assertEquals( -12345L, TextFormat.parseInt64( "-12345")); - assertEquals( 2147483647L, TextFormat.parseInt64( "2147483647")); + assertEquals(0L, TextFormat.parseInt64("0")); + assertEquals(1L, TextFormat.parseInt64("1")); + assertEquals(-1L, TextFormat.parseInt64("-1")); + assertEquals(12345L, TextFormat.parseInt64("12345")); + assertEquals(-12345L, TextFormat.parseInt64("-12345")); + assertEquals(2147483647L, TextFormat.parseInt64("2147483647")); assertEquals(-2147483648L, TextFormat.parseInt64("-2147483648")); - assertEquals( 4294967295L, TextFormat.parseInt64( "4294967295")); - assertEquals( 4294967296L, TextFormat.parseInt64( "4294967296")); - assertEquals(9223372036854775807L, - TextFormat.parseInt64("9223372036854775807")); - assertEquals(-9223372036854775808L, - TextFormat.parseInt64("-9223372036854775808")); - - assertEquals( 0L, TextFormat.parseUInt64( "0")); - assertEquals( 1L, TextFormat.parseUInt64( "1")); - assertEquals( 12345L, TextFormat.parseUInt64( "12345")); - assertEquals( 2147483647L, TextFormat.parseUInt64( "2147483647")); - assertEquals( 4294967295L, TextFormat.parseUInt64( "4294967295")); - assertEquals( 4294967296L, TextFormat.parseUInt64( "4294967296")); - assertEquals(9223372036854775807L, - TextFormat.parseUInt64("9223372036854775807")); - assertEquals(-9223372036854775808L, - TextFormat.parseUInt64("9223372036854775808")); + assertEquals(4294967295L, TextFormat.parseInt64("4294967295")); + assertEquals(4294967296L, TextFormat.parseInt64("4294967296")); + assertEquals(9223372036854775807L, TextFormat.parseInt64("9223372036854775807")); + assertEquals(-9223372036854775808L, TextFormat.parseInt64("-9223372036854775808")); + + assertEquals(0L, TextFormat.parseUInt64("0")); + assertEquals(1L, TextFormat.parseUInt64("1")); + assertEquals(12345L, TextFormat.parseUInt64("12345")); + assertEquals(2147483647L, TextFormat.parseUInt64("2147483647")); + assertEquals(4294967295L, TextFormat.parseUInt64("4294967295")); + assertEquals(4294967296L, TextFormat.parseUInt64("4294967296")); + assertEquals(9223372036854775807L, TextFormat.parseUInt64("9223372036854775807")); + assertEquals(-9223372036854775808L, TextFormat.parseUInt64("9223372036854775808")); assertEquals(-1L, TextFormat.parseUInt64("18446744073709551615")); // Hex assertEquals(0x1234abcd, TextFormat.parseInt32("0x1234abcd")); assertEquals(-0x1234abcd, TextFormat.parseInt32("-0x1234abcd")); assertEquals(-1, TextFormat.parseUInt64("0xffffffffffffffff")); - assertEquals(0x7fffffffffffffffL, - TextFormat.parseInt64("0x7fffffffffffffff")); + assertEquals(0x7fffffffffffffffL, TextFormat.parseInt64("0x7fffffffffffffff")); // Octal assertEquals(01234567, TextFormat.parseInt32("01234567")); @@ -815,26 +806,26 @@ public class TextFormatTest extends TestCase { public void testParseLongString() throws Exception { String longText = - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890" + - "123456789012345678901234567890123456789012345678901234567890"; + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890" + + "123456789012345678901234567890123456789012345678901234567890"; TestAllTypes.Builder builder = TestAllTypes.newBuilder(); TextFormat.merge("optional_string: \"" + longText + "\"", builder); @@ -843,16 +834,16 @@ public class TextFormatTest extends TestCase { public void testParseBoolean() throws Exception { String goodText = - "repeated_bool: t repeated_bool : 0\n" + - "repeated_bool :f repeated_bool:1\n" + - "repeated_bool: False repeated_bool: True"; + "repeated_bool: t repeated_bool : 0\n" + + "repeated_bool :f repeated_bool:1\n" + + "repeated_bool: False repeated_bool: True"; String goodTextCanonical = - "repeated_bool: true\n" + - "repeated_bool: false\n" + - "repeated_bool: false\n" + - "repeated_bool: true\n" + - "repeated_bool: false\n" + - "repeated_bool: true\n"; + "repeated_bool: true\n" + + "repeated_bool: false\n" + + "repeated_bool: false\n" + + "repeated_bool: true\n" + + "repeated_bool: false\n" + + "repeated_bool: true\n"; TestAllTypes.Builder builder = TestAllTypes.newBuilder(); TextFormat.merge(goodText, builder); assertEquals(goodTextCanonical, builder.build().toString()); @@ -881,178 +872,231 @@ public class TextFormatTest extends TestCase { public void testPrintFieldValue() throws Exception { assertPrintFieldValue("\"Hello\"", "Hello", "repeated_string"); - assertPrintFieldValue("123.0", 123f, "repeated_float"); - assertPrintFieldValue("123.0", 123d, "repeated_double"); - assertPrintFieldValue("123", 123, "repeated_int32"); - assertPrintFieldValue("123", 123L, "repeated_int64"); - assertPrintFieldValue("true", true, "repeated_bool"); + assertPrintFieldValue("123.0", 123f, "repeated_float"); + assertPrintFieldValue("123.0", 123d, "repeated_double"); + assertPrintFieldValue("123", 123, "repeated_int32"); + assertPrintFieldValue("123", 123L, "repeated_int64"); + assertPrintFieldValue("true", true, "repeated_bool"); assertPrintFieldValue("4294967295", 0xFFFFFFFF, "repeated_uint32"); - assertPrintFieldValue("18446744073709551615", 0xFFFFFFFFFFFFFFFFL, - "repeated_uint64"); - assertPrintFieldValue("\"\\001\\002\\003\"", - ByteString.copyFrom(new byte[] {1, 2, 3}), "repeated_bytes"); + assertPrintFieldValue("18446744073709551615", 0xFFFFFFFFFFFFFFFFL, "repeated_uint64"); + assertPrintFieldValue( + "\"\\001\\002\\003\"", ByteString.copyFrom(new byte[] {1, 2, 3}), "repeated_bytes"); } - private void assertPrintFieldValue(String expect, Object value, - String fieldName) throws Exception { + private void assertPrintFieldValue(String expect, Object value, String fieldName) + throws Exception { StringBuilder sb = new StringBuilder(); - TextFormat.printFieldValue( - TestAllTypes.getDescriptor().findFieldByName(fieldName), - value, sb); + TextFormat.printer() + .printFieldValue(TestAllTypes.getDescriptor().findFieldByName(fieldName), value, sb); assertEquals(expect, sb.toString()); } public void testShortDebugString() { - assertEquals("optional_nested_message { bb: 42 } repeated_int32: 1" - + " repeated_uint32: 2", - TextFormat.shortDebugString(TestAllTypes.newBuilder() - .addRepeatedInt32(1) - .addRepeatedUint32(2) - .setOptionalNestedMessage( - NestedMessage.newBuilder().setBb(42).build()) - .build())); + assertEquals( + "optional_nested_message { bb: 42 } repeated_int32: 1 repeated_uint32: 2", + TextFormat.shortDebugString( + TestAllTypes.newBuilder() + .addRepeatedInt32(1) + .addRepeatedUint32(2) + .setOptionalNestedMessage(NestedMessage.newBuilder().setBb(42).build()) + .build())); } public void testShortDebugString_field() { - final FieldDescriptor dataField = - OneString.getDescriptor().findFieldByName("data"); + final FieldDescriptor dataField = OneString.getDescriptor().findFieldByName("data"); assertEquals( - "data: \"test data\"", - TextFormat.shortDebugString(dataField, "test data")); + "data: \"test data\"", + TextFormat.printer().shortDebugString(dataField, "test data")); final FieldDescriptor optionalField = - TestAllTypes.getDescriptor().findFieldByName("optional_nested_message"); + TestAllTypes.getDescriptor().findFieldByName("optional_nested_message"); final Object value = NestedMessage.newBuilder().setBb(42).build(); assertEquals( - "optional_nested_message { bb: 42 }", - TextFormat.shortDebugString(optionalField, value)); + "optional_nested_message { bb: 42 }", + TextFormat.printer().shortDebugString(optionalField, value)); } public void testShortDebugString_unknown() { - assertEquals("5: 1 5: 0x00000002 5: 0x0000000000000003 5: \"4\" 5: { 12: 6 } 5 { 10: 5 }" - + " 8: 1 8: 2 8: 3 15: 12379813812177893520 15: 0xabcd1234 15:" - + " 0xabcdef1234567890", - TextFormat.shortDebugString(makeUnknownFieldSet())); + assertEquals( + "5: 1 5: 0x00000002 5: 0x0000000000000003 5: \"4\" 5: { 12: 6 } 5 { 10: 5 }" + + " 8: 1 8: 2 8: 3 15: 12379813812177893520 15: 0xabcd1234 15:" + + " 0xabcdef1234567890", + TextFormat.printer().shortDebugString(makeUnknownFieldSet())); } public void testPrintToUnicodeString() throws Exception { assertEquals( - "optional_string: \"abc\u3042efg\"\n" + - "optional_bytes: \"\\343\\201\\202\"\n" + - "repeated_string: \"\u3093XYZ\"\n", - TextFormat.printToUnicodeString(TestAllTypes.newBuilder() - .setOptionalString("abc\u3042efg") - .setOptionalBytes(bytes(0xe3, 0x81, 0x82)) - .addRepeatedString("\u3093XYZ") - .build())); + "optional_string: \"abc\u3042efg\"\n" + + "optional_bytes: \"\\343\\201\\202\"\n" + + "repeated_string: \"\u3093XYZ\"\n", + TextFormat.printer() + .escapingNonAscii(false) + .printToString( + TestAllTypes.newBuilder() + .setOptionalString("abc\u3042efg") + .setOptionalBytes(bytes(0xe3, 0x81, 0x82)) + .addRepeatedString("\u3093XYZ") + .build())); // Double quotes and backslashes should be escaped assertEquals( "optional_string: \"a\\\\bc\\\"ef\\\"g\"\n", - TextFormat.printToUnicodeString(TestAllTypes.newBuilder() - .setOptionalString("a\\bc\"ef\"g") - .build())); + TextFormat.printer() + .escapingNonAscii(false) + .printToString(TestAllTypes.newBuilder().setOptionalString("a\\bc\"ef\"g").build())); // Test escaping roundtrip - TestAllTypes message = TestAllTypes.newBuilder() - .setOptionalString("a\\bc\\\"ef\"g") - .build(); + TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("a\\bc\\\"ef\"g").build(); TestAllTypes.Builder builder = TestAllTypes.newBuilder(); - TextFormat.merge(TextFormat.printToUnicodeString(message), builder); + TextFormat.merge(TextFormat.printer().escapingNonAscii(false).printToString(message), builder); assertEquals(message.getOptionalString(), builder.getOptionalString()); } public void testPrintToUnicodeStringWithNewlines() throws Exception { // No newlines at start and end - assertEquals("optional_string: \"test newlines\\n\\nin\\nstring\"\n", - TextFormat.printToUnicodeString(TestAllTypes.newBuilder() - .setOptionalString("test newlines\n\nin\nstring") - .build())); + assertEquals( + "optional_string: \"test newlines\\n\\nin\\nstring\"\n", + TextFormat.printer() + .escapingNonAscii(false) + .printToString( + TestAllTypes.newBuilder() + .setOptionalString("test newlines\n\nin\nstring") + .build())); // Newlines at start and end - assertEquals("optional_string: \"\\ntest\\nnewlines\\n\\nin\\nstring\\n\"\n", - TextFormat.printToUnicodeString(TestAllTypes.newBuilder() - .setOptionalString("\ntest\nnewlines\n\nin\nstring\n") - .build())); + assertEquals( + "optional_string: \"\\ntest\\nnewlines\\n\\nin\\nstring\\n\"\n", + TextFormat.printer() + .escapingNonAscii(false) + .printToString( + TestAllTypes.newBuilder() + .setOptionalString("\ntest\nnewlines\n\nin\nstring\n") + .build())); // Strings with 0, 1 and 2 newlines. - assertEquals("optional_string: \"\"\n", - TextFormat.printToUnicodeString(TestAllTypes.newBuilder() - .setOptionalString("") - .build())); - assertEquals("optional_string: \"\\n\"\n", - TextFormat.printToUnicodeString(TestAllTypes.newBuilder() - .setOptionalString("\n") - .build())); - assertEquals("optional_string: \"\\n\\n\"\n", - TextFormat.printToUnicodeString(TestAllTypes.newBuilder() - .setOptionalString("\n\n") - .build())); + assertEquals( + "optional_string: \"\"\n", + TextFormat.printer() + .escapingNonAscii(false) + .printToString(TestAllTypes.newBuilder().setOptionalString("").build())); + assertEquals( + "optional_string: \"\\n\"\n", + TextFormat.printer() + .escapingNonAscii(false) + .printToString(TestAllTypes.newBuilder().setOptionalString("\n").build())); + assertEquals( + "optional_string: \"\\n\\n\"\n", + TextFormat.printer() + .escapingNonAscii(false) + .printToString(TestAllTypes.newBuilder().setOptionalString("\n\n").build())); // Test escaping roundtrip - TestAllTypes message = TestAllTypes.newBuilder() - .setOptionalString("\ntest\nnewlines\n\nin\nstring\n") - .build(); + TestAllTypes message = + TestAllTypes.newBuilder().setOptionalString("\ntest\nnewlines\n\nin\nstring\n").build(); TestAllTypes.Builder builder = TestAllTypes.newBuilder(); - TextFormat.merge(TextFormat.printToUnicodeString(message), builder); + TextFormat.merge(TextFormat.printer().escapingNonAscii(false).printToString(message), builder); assertEquals(message.getOptionalString(), builder.getOptionalString()); } public void testPrintToUnicodeString_unknown() { assertEquals( "1: \"\\343\\201\\202\"\n", - TextFormat.printToUnicodeString(UnknownFieldSet.newBuilder() - .addField(1, - UnknownFieldSet.Field.newBuilder() - .addLengthDelimited(bytes(0xe3, 0x81, 0x82)).build()) - .build())); + TextFormat.printer() + .escapingNonAscii(false) + .printToString( + UnknownFieldSet.newBuilder() + .addField( + 1, + UnknownFieldSet.Field.newBuilder() + .addLengthDelimited(bytes(0xe3, 0x81, 0x82)) + .build()) + .build())); } + public void testParseUnknownExtensions() throws Exception { + TestUtil.TestLogHandler logHandler = new TestUtil.TestLogHandler(); + Logger logger = Logger.getLogger(TextFormat.class.getName()); + logger.addHandler(logHandler); + // Test unknown extension can pass. + assertParseSuccessWithUnknownExtensions("[unknown_extension]: 123"); + assertParseSuccessWithUnknownExtensions( + "[unknown_extension]: 123\n" + "[unknown_ext]: inf\n" + "[unknown]: 1.234"); + // Test warning messages. + assertEquals( + "Input contains unknown fields and/or extensions:\n" + + "1:2:\tprotobuf_unittest.TestAllTypes.[unknown_extension]", + logHandler.getStoredLogRecords().get(0).getMessage()); + assertEquals( + "Input contains unknown fields and/or extensions:\n" + + "1:2:\tprotobuf_unittest.TestAllTypes.[unknown_extension]\n" + + "2:2:\tprotobuf_unittest.TestAllTypes.[unknown_ext]\n" + + "3:2:\tprotobuf_unittest.TestAllTypes.[unknown]", + logHandler.getStoredLogRecords().get(1).getMessage()); + + // Test unknown field can not pass. + assertParseErrorWithUnknownExtensions( + "2:1: Input contains unknown fields and/or extensions:\n" + + "1:2:\tprotobuf_unittest.TestAllTypes.[unknown_extension]\n" + + "2:1:\tprotobuf_unittest.TestAllTypes.unknown_field", + "[unknown_extension]: 1\n" + "unknown_field: 12345"); + assertParseErrorWithUnknownExtensions( + "3:1: Input contains unknown fields and/or extensions:\n" + + "1:2:\tprotobuf_unittest.TestAllTypes.[unknown_extension1]\n" + + "2:2:\tprotobuf_unittest.TestAllTypes.[unknown_extension2]\n" + + "3:1:\tprotobuf_unittest.TestAllTypes.unknown_field\n" + + "4:2:\tprotobuf_unittest.TestAllTypes.[unknown_extension3]", + "[unknown_extension1]: 1\n" + + "[unknown_extension2]: 2\n" + + "unknown_field: 12345\n" + + "[unknown_extension3]: 3\n"); + assertParseErrorWithUnknownExtensions( + "1:1: Input contains unknown fields and/or extensions:\n" + + "1:1:\tprotobuf_unittest.TestAllTypes.unknown_field1\n" + + "2:1:\tprotobuf_unittest.TestAllTypes.unknown_field2\n" + + "3:2:\tprotobuf_unittest.TestAllTypes.[unknown_extension]\n" + + "4:1:\tprotobuf_unittest.TestAllTypes.unknown_field3", + "unknown_field1: 1\n" + + "unknown_field2: 2\n" + + "[unknown_extension]: 12345\n" + + "unknown_field3: 3\n"); + } + // See additional coverage in testOneofOverwriteForbidden and testMapOverwriteForbidden. public void testParseNonRepeatedFields() throws Exception { + assertParseSuccessWithOverwriteForbidden("repeated_int32: 1\nrepeated_int32: 2\n"); + assertParseSuccessWithOverwriteForbidden("RepeatedGroup { a: 1 }\nRepeatedGroup { a: 2 }\n"); assertParseSuccessWithOverwriteForbidden( - "repeated_int32: 1\n" + - "repeated_int32: 2\n"); - assertParseSuccessWithOverwriteForbidden( - "RepeatedGroup { a: 1 }\n" + - "RepeatedGroup { a: 2 }\n"); - assertParseSuccessWithOverwriteForbidden( - "repeated_nested_message { bb: 1 }\n" + - "repeated_nested_message { bb: 2 }\n"); + "repeated_nested_message { bb: 1 }\nrepeated_nested_message { bb: 2 }\n"); assertParseErrorWithOverwriteForbidden( - "3:17: Non-repeated field " + - "\"protobuf_unittest.TestAllTypes.optional_int32\" " + - "cannot be overwritten.", - "optional_int32: 1\n" + - "optional_bool: true\n" + - "optional_int32: 1\n"); + "3:15: Non-repeated field " + + "\"protobuf_unittest.TestAllTypes.optional_int32\" " + + "cannot be overwritten.", + "optional_int32: 1\noptional_bool: true\noptional_int32: 1\n"); assertParseErrorWithOverwriteForbidden( - "2:17: Non-repeated field " + - "\"protobuf_unittest.TestAllTypes.optionalgroup\" " + - "cannot be overwritten.", - "OptionalGroup { a: 1 }\n" + - "OptionalGroup { }\n"); + "2:1: Non-repeated field " + + "\"protobuf_unittest.TestAllTypes.optionalgroup\" " + + "cannot be overwritten.", + "OptionalGroup { a: 1 }\nOptionalGroup { }\n"); assertParseErrorWithOverwriteForbidden( - "2:33: Non-repeated field " + - "\"protobuf_unittest.TestAllTypes.optional_nested_message\" " + - "cannot be overwritten.", - "optional_nested_message { }\n" + - "optional_nested_message { bb: 3 }\n"); + "2:1: Non-repeated field " + + "\"protobuf_unittest.TestAllTypes.optional_nested_message\" " + + "cannot be overwritten.", + "optional_nested_message { }\noptional_nested_message { bb: 3 }\n"); assertParseErrorWithOverwriteForbidden( - "2:16: Non-repeated field " + - "\"protobuf_unittest.TestAllTypes.default_int32\" " + - "cannot be overwritten.", - "default_int32: 41\n" + // the default value - "default_int32: 41\n"); + "2:14: Non-repeated field " + + "\"protobuf_unittest.TestAllTypes.default_int32\" " + + "cannot be overwritten.", + "default_int32: 41\n" + + // the default value + "default_int32: 41\n"); assertParseErrorWithOverwriteForbidden( - "2:17: Non-repeated field " + - "\"protobuf_unittest.TestAllTypes.default_string\" " + - "cannot be overwritten.", - "default_string: \"zxcv\"\n" + - "default_string: \"asdf\"\n"); + "2:15: Non-repeated field " + + "\"protobuf_unittest.TestAllTypes.default_string\" " + + "cannot be overwritten.", + "default_string: \"zxcv\"\ndefault_string: \"asdf\"\n"); } public void testParseShortRepeatedFormOfRepeatedFields() throws Exception { @@ -1073,27 +1117,20 @@ public class TextFormatTest extends TestCase { public void testParseShortRepeatedFormWithTrailingComma() throws Exception { assertParseErrorWithOverwriteForbidden( - "1:38: Expected identifier. Found \']\'", - "repeated_foreign_enum: [FOREIGN_FOO, ]\n"); + "1:38: Expected identifier. Found \']\'", "repeated_foreign_enum: [FOREIGN_FOO, ]\n"); assertParseErrorWithOverwriteForbidden( - "1:22: Couldn't parse integer: For input string: \"]\"", - "repeated_int32: [ 1, ]\n"); + "1:22: Couldn't parse integer: For input string: \"]\"", "repeated_int32: [ 1, ]\n"); + assertParseErrorWithOverwriteForbidden("1:25: Expected \"{\".", "RepeatedGroup [{ a: 1 },]\n"); assertParseErrorWithOverwriteForbidden( - "1:25: Expected \"{\".", - "RepeatedGroup [{ a: 1 },]\n"); - assertParseErrorWithOverwriteForbidden( - "1:37: Expected \"{\".", - "repeated_nested_message [{ bb: 1 }, ]\n"); + "1:37: Expected \"{\".", "repeated_nested_message [{ bb: 1 }, ]\n"); // See also testMapShortFormTrailingComma. } public void testParseShortRepeatedFormOfNonRepeatedFields() throws Exception { assertParseErrorWithOverwriteForbidden( - "1:17: Couldn't parse integer: For input string: \"[\"", - "optional_int32: [1]\n"); + "1:17: Couldn't parse integer: For input string: \"[\"", "optional_int32: [1]\n"); assertParseErrorWithOverwriteForbidden( - "1:17: Couldn't parse integer: For input string: \"[\"", - "optional_int32: []\n"); + "1:17: Couldn't parse integer: For input string: \"[\"", "optional_int32: []\n"); } // ======================================================================= @@ -1104,7 +1141,7 @@ public class TextFormatTest extends TestCase { TestUtil.setOneof(builder); TestOneof2 message = builder.build(); TestOneof2.Builder dest = TestOneof2.newBuilder(); - TextFormat.merge(TextFormat.printToUnicodeString(message), dest); + TextFormat.merge(TextFormat.printer().escapingNonAscii(false).printToString(message), dest); TestUtil.assertOneofSet(dest.build()); } @@ -1115,9 +1152,11 @@ public class TextFormatTest extends TestCase { parserWithOverwriteForbidden.merge(input, TestUtil.getFullExtensionRegistry(), builder); fail("Expected parse exception."); } catch (TextFormat.ParseException e) { - assertEquals("1:36: Field \"protobuf_unittest.TestOneof2.foo_int\"" - + " is specified along with field \"protobuf_unittest.TestOneof2.foo_string\"," - + " another member of oneof \"foo\".", e.getMessage()); + assertEquals( + "1:34: Field \"protobuf_unittest.TestOneof2.foo_int\"" + + " is specified along with field \"protobuf_unittest.TestOneof2.foo_string\"," + + " another member of oneof \"foo\".", + e.getMessage()); } } @@ -1141,7 +1180,7 @@ public class TextFormatTest extends TestCase { .putInt32ToStringField(20, "banana") .putInt32ToStringField(30, "cherry") .build(); - String text = TextFormat.printToUnicodeString(message); + String text = TextFormat.printer().escapingNonAscii(false).printToString(message); { TestMap.Builder dest = TestMap.newBuilder(); TextFormat.merge(text, dest); @@ -1157,8 +1196,8 @@ public class TextFormatTest extends TestCase { public void testMapShortForm() throws Exception { String text = "string_to_int32_field [{ key: 'x' value: 10 }, { key: 'y' value: 20 }]\n" - + "int32_to_message_field " - + "[{ key: 1 value { value: 100 } }, { key: 2 value: { value: 200 } }]\n"; + + "int32_to_message_field " + + "[{ key: 1 value { value: 100 } }, { key: 2 value: { value: 200 } }]\n"; TestMap.Builder dest = TestMap.newBuilder(); parserWithOverwriteForbidden.merge(text, dest); TestMap message = dest.build(); @@ -1169,8 +1208,7 @@ public class TextFormatTest extends TestCase { } public void testMapShortFormEmpty() throws Exception { - String text = "string_to_int32_field []\n" - + "int32_to_message_field: []\n"; + String text = "string_to_int32_field []\nint32_to_message_field: []\n"; TestMap.Builder dest = TestMap.newBuilder(); parserWithOverwriteForbidden.merge(text, dest); TestMap message = dest.build(); @@ -1226,24 +1264,25 @@ public class TextFormatTest extends TestCase { // Set to allow unknown fields TextFormat.Parser parser = TextFormat.Parser.newBuilder() + .setAllowUnknownFields(true) .setParseInfoTreeBuilder(treeBuilder) .build(); final String stringData = "optional_int32: 1\n" - + "optional_int64: 2\n" - + " optional_double: 2.4\n" - + "repeated_int32: 5\n" - + "repeated_int32: 10\n" - + "optional_nested_message <\n" - + " bb: 78\n" - + ">\n" - + "repeated_nested_message <\n" - + " bb: 79\n" - + ">\n" - + "repeated_nested_message <\n" - + " bb: 80\n" - + ">"; + + "optional_int64: 2\n" + + " optional_double: 2.4\n" + + "repeated_int32: 5\n" + + "repeated_int32: 10\n" + + "optional_nested_message <\n" + + " bb: 78\n" + + ">\n" + + "repeated_nested_message <\n" + + " bb: 79\n" + + ">\n" + + "repeated_nested_message <\n" + + " bb: 80\n" + + ">"; parser.merge(stringData, builder); TextFormatParseInfoTree tree = treeBuilder.build(); @@ -1303,9 +1342,7 @@ public class TextFormatTest extends TestCase { fail( String.format( "Tree/descriptor/fieldname did not contain index %d, line %d column %d expected", - index, - line, - column)); + index, line, column)); } } } diff --git a/java/core/src/test/java/com/google/protobuf/UnknownEnumValueTest.java b/java/core/src/test/java/com/google/protobuf/UnknownEnumValueTest.java index 88cbbf862c..7daef026dc 100644 --- a/java/core/src/test/java/com/google/protobuf/UnknownEnumValueTest.java +++ b/java/core/src/test/java/com/google/protobuf/UnknownEnumValueTest.java @@ -35,14 +35,18 @@ import com.google.protobuf.Descriptors.EnumDescriptor; import com.google.protobuf.Descriptors.EnumValueDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.FieldPresenceTestProto.TestAllTypes; +import com.google.protobuf.Proto2UnknownEnumValuesTestProto.Proto2EnumMessage; +import com.google.protobuf.Proto2UnknownEnumValuesTestProto.Proto2EnumMessageWithEnumSubset; +import com.google.protobuf.Proto2UnknownEnumValuesTestProto.Proto2TestEnum; +import com.google.protobuf.Proto2UnknownEnumValuesTestProto.Proto2TestEnumSubset; import com.google.protobuf.TextFormat.ParseException; import junit.framework.TestCase; /** - * Unit tests for protos that keep unknown enum values rather than discard - * them as unknown fields. + * Unit tests for protos that keep unknown enum values rather than discard them as unknown fields. */ public class UnknownEnumValueTest extends TestCase { + public void testUnknownEnumValues() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); builder.setOptionalNestedEnumValue(4321); @@ -58,7 +62,7 @@ public class UnknownEnumValueTest extends TestCase { assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, message.getRepeatedNestedEnum(0)); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, message.getRepeatedNestedEnumList().get(0)); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, message.getPackedNestedEnum(0)); - + // Test serialization and parsing. ByteString data = message.toByteString(); message = TestAllTypes.parseFrom(data); @@ -71,19 +75,19 @@ public class UnknownEnumValueTest extends TestCase { assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, message.getRepeatedNestedEnum(0)); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, message.getRepeatedNestedEnumList().get(0)); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, message.getPackedNestedEnum(0)); - + // Test toBuilder(). builder = message.toBuilder(); assertEquals(4321, builder.getOptionalNestedEnumValue()); assertEquals(5432, builder.getRepeatedNestedEnumValue(0)); assertEquals(5432, builder.getRepeatedNestedEnumValueList().get(0).intValue()); - assertEquals(6543, builder.getPackedNestedEnumValue(0)); + assertEquals(6543, builder.getPackedNestedEnumValue(0)); // Returns UNRECOGNIZED if an enum type is requested. assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, builder.getOptionalNestedEnum()); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, builder.getRepeatedNestedEnum(0)); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, builder.getRepeatedNestedEnumList().get(0)); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, builder.getPackedNestedEnum(0)); - + // Test mergeFrom(). builder = TestAllTypes.newBuilder().mergeFrom(message); assertEquals(4321, builder.getOptionalNestedEnumValue()); @@ -95,7 +99,7 @@ public class UnknownEnumValueTest extends TestCase { assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, builder.getRepeatedNestedEnum(0)); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, builder.getRepeatedNestedEnumList().get(0)); assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, builder.getPackedNestedEnum(0)); - + // Test equals() and hashCode() TestAllTypes sameMessage = builder.build(); assertEquals(message, sameMessage); @@ -123,7 +127,7 @@ public class UnknownEnumValueTest extends TestCase { // Expected. } } - + public void testUnknownEnumValueInReflectionApi() throws Exception { Descriptor descriptor = TestAllTypes.getDescriptor(); FieldDescriptor optionalNestedEnumField = descriptor.findFieldByName("optional_nested_enum"); @@ -132,14 +136,13 @@ public class UnknownEnumValueTest extends TestCase { EnumDescriptor enumType = TestAllTypes.NestedEnum.getDescriptor(); TestAllTypes.Builder builder = TestAllTypes.newBuilder(); - builder.setField(optionalNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(4321)); - builder.addRepeatedField(repeatedNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(5432)); - builder.addRepeatedField(packedNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(6543)); + builder.setField(optionalNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(4321)); + builder.addRepeatedField( + repeatedNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(5432)); + builder.addRepeatedField( + packedNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(6543)); TestAllTypes message = builder.build(); - + // Getters will return unknown enum values as EnumValueDescriptor. EnumValueDescriptor unknown4321 = (EnumValueDescriptor) message.getField(optionalNestedEnumField); @@ -166,79 +169,78 @@ public class UnknownEnumValueTest extends TestCase { message = builder.build(); // Like other descriptors, unknown EnumValueDescriptor can be compared by // object identity. - assertTrue(unknown6543 == message.getField(optionalNestedEnumField)); - assertTrue(unknown4321 == message.getRepeatedField(repeatedNestedEnumField, 0)); - assertTrue(unknown5432 == message.getRepeatedField(packedNestedEnumField, 0)); + assertSame(message.getField(optionalNestedEnumField), unknown6543); + assertSame(message.getRepeatedField(repeatedNestedEnumField, 0), unknown4321); + assertSame(message.getRepeatedField(packedNestedEnumField, 0), unknown5432); } - + public void testUnknownEnumValueWithDynamicMessage() throws Exception { Descriptor descriptor = TestAllTypes.getDescriptor(); FieldDescriptor optionalNestedEnumField = descriptor.findFieldByName("optional_nested_enum"); FieldDescriptor repeatedNestedEnumField = descriptor.findFieldByName("repeated_nested_enum"); FieldDescriptor packedNestedEnumField = descriptor.findFieldByName("packed_nested_enum"); EnumDescriptor enumType = TestAllTypes.NestedEnum.getDescriptor(); - + Message dynamicMessageDefaultInstance = DynamicMessage.getDefaultInstance(descriptor); Message.Builder builder = dynamicMessageDefaultInstance.newBuilderForType(); - builder.setField(optionalNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(4321)); - builder.addRepeatedField(repeatedNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(5432)); - builder.addRepeatedField(packedNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(6543)); + builder.setField(optionalNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(4321)); + builder.addRepeatedField( + repeatedNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(5432)); + builder.addRepeatedField( + packedNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(6543)); Message message = builder.build(); - assertEquals(4321, - ((EnumValueDescriptor) message.getField(optionalNestedEnumField)).getNumber()); - assertEquals(5432, + assertEquals( + 4321, ((EnumValueDescriptor) message.getField(optionalNestedEnumField)).getNumber()); + assertEquals( + 5432, ((EnumValueDescriptor) message.getRepeatedField(repeatedNestedEnumField, 0)).getNumber()); - assertEquals(6543, + assertEquals( + 6543, ((EnumValueDescriptor) message.getRepeatedField(packedNestedEnumField, 0)).getNumber()); - + // Test reflection based serialization/parsing implementation. ByteString data = message.toByteString(); - message = dynamicMessageDefaultInstance - .newBuilderForType() - .mergeFrom(data) - .build(); - assertEquals(4321, - ((EnumValueDescriptor) message.getField(optionalNestedEnumField)).getNumber()); - assertEquals(5432, + message = dynamicMessageDefaultInstance.newBuilderForType().mergeFrom(data).build(); + assertEquals( + 4321, ((EnumValueDescriptor) message.getField(optionalNestedEnumField)).getNumber()); + assertEquals( + 5432, ((EnumValueDescriptor) message.getRepeatedField(repeatedNestedEnumField, 0)).getNumber()); - assertEquals(6543, + assertEquals( + 6543, ((EnumValueDescriptor) message.getRepeatedField(packedNestedEnumField, 0)).getNumber()); - + // Test reflection based equals()/hashCode(). builder = dynamicMessageDefaultInstance.newBuilderForType(); - builder.setField(optionalNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(4321)); - builder.addRepeatedField(repeatedNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(5432)); - builder.addRepeatedField(packedNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(6543)); + builder.setField(optionalNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(4321)); + builder.addRepeatedField( + repeatedNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(5432)); + builder.addRepeatedField( + packedNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(6543)); Message sameMessage = builder.build(); assertEquals(message, sameMessage); assertEquals(message.hashCode(), sameMessage.hashCode()); - builder.setField(optionalNestedEnumField, - enumType.findValueByNumberCreatingIfUnknown(0)); + builder.setField(optionalNestedEnumField, enumType.findValueByNumberCreatingIfUnknown(0)); Message differentMessage = builder.build(); assertFalse(message.equals(differentMessage)); } - + public void testUnknownEnumValuesInTextFormat() { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); builder.setOptionalNestedEnumValue(4321); builder.addRepeatedNestedEnumValue(5432); builder.addPackedNestedEnumValue(6543); TestAllTypes message = builder.build(); - + // We can print a message with unknown enum values. - String textData = TextFormat.printToString(message); + String textData = TextFormat.printer().printToString(message); assertEquals( "optional_nested_enum: UNKNOWN_ENUM_VALUE_NestedEnum_4321\n" - + "repeated_nested_enum: UNKNOWN_ENUM_VALUE_NestedEnum_5432\n" - + "packed_nested_enum: UNKNOWN_ENUM_VALUE_NestedEnum_6543\n", textData); - + + "repeated_nested_enum: UNKNOWN_ENUM_VALUE_NestedEnum_5432\n" + + "packed_nested_enum: UNKNOWN_ENUM_VALUE_NestedEnum_6543\n", + textData); + // Parsing unknown enum values will fail just like parsing other kinds of // unknown fields. try { @@ -248,4 +250,61 @@ public class UnknownEnumValueTest extends TestCase { // expected. } } + + public void testUnknownEnumValuesInProto2() throws Exception { + Proto2EnumMessage.Builder sourceMessage = Proto2EnumMessage.newBuilder(); + sourceMessage + .addRepeatedPackedEnum(Proto2TestEnum.ZERO) + .addRepeatedPackedEnum(Proto2TestEnum.TWO) // Unknown in parsed proto + .addRepeatedPackedEnum(Proto2TestEnum.ONE); + + Proto2EnumMessageWithEnumSubset destMessage = + Proto2EnumMessageWithEnumSubset.parseFrom(sourceMessage.build().toByteArray()); + + // Known enum values should be preserved. + assertEquals(2, destMessage.getRepeatedPackedEnumCount()); + assertEquals(Proto2TestEnumSubset.TESTENUM_SUBSET_ZERO, destMessage.getRepeatedPackedEnum(0)); + assertEquals(Proto2TestEnumSubset.TESTENUM_SUBSET_ONE, destMessage.getRepeatedPackedEnum(1)); + + // Unknown enum values should be found in UnknownFieldSet. + UnknownFieldSet unknown = destMessage.getUnknownFields(); + assertEquals( + Proto2TestEnum.TWO_VALUE, + unknown + .getField(Proto2EnumMessageWithEnumSubset.REPEATED_PACKED_ENUM_FIELD_NUMBER) + .getVarintList() + .get(0) + .longValue()); + } + + public void testUnknownEnumValuesInProto2WithDynamicMessage() throws Exception { + Descriptor descriptor = Proto2EnumMessageWithEnumSubset.getDescriptor(); + FieldDescriptor repeatedPackedField = descriptor.findFieldByName("repeated_packed_enum"); + + Proto2EnumMessage.Builder sourceMessage = Proto2EnumMessage.newBuilder(); + sourceMessage + .addRepeatedPackedEnum(Proto2TestEnum.ZERO) + .addRepeatedPackedEnum(Proto2TestEnum.TWO) // Unknown in parsed proto + .addRepeatedPackedEnum(Proto2TestEnum.ONE); + + DynamicMessage message = + DynamicMessage.parseFrom( + Proto2EnumMessageWithEnumSubset.getDescriptor(), sourceMessage.build().toByteArray()); + + // Known enum values should be preserved. + assertEquals(2, message.getRepeatedFieldCount(repeatedPackedField)); + EnumValueDescriptor enumValue0 = + (EnumValueDescriptor) message.getRepeatedField(repeatedPackedField, 0); + EnumValueDescriptor enumValue1 = + (EnumValueDescriptor) message.getRepeatedField(repeatedPackedField, 1); + + assertEquals(Proto2TestEnumSubset.TESTENUM_SUBSET_ZERO_VALUE, enumValue0.getNumber()); + assertEquals(Proto2TestEnumSubset.TESTENUM_SUBSET_ONE_VALUE, enumValue1.getNumber()); + + // Unknown enum values should be found in UnknownFieldSet. + UnknownFieldSet unknown = message.getUnknownFields(); + assertEquals( + Proto2TestEnum.TWO_VALUE, + unknown.getField(repeatedPackedField.getNumber()).getVarintList().get(0).longValue()); + } } diff --git a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java deleted file mode 100644 index 8ce0ca7362..0000000000 --- a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java +++ /dev/null @@ -1,600 +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. - -package com.google.protobuf; - -import static junit.framework.TestCase.assertEquals; - -import com.google.protobuf.UnittestLite.TestAllExtensionsLite; -import com.google.protobuf.UnittestLite.TestAllTypesLite; -import protobuf_unittest.UnittestProto; -import protobuf_unittest.UnittestProto.ForeignEnum; -import protobuf_unittest.UnittestProto.TestAllExtensions; -import protobuf_unittest.UnittestProto.TestAllTypes; -import protobuf_unittest.UnittestProto.TestEmptyMessage; -import protobuf_unittest.UnittestProto.TestPackedExtensions; -import protobuf_unittest.UnittestProto.TestPackedTypes; -import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash; -import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; -import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.Arrays; -import java.util.Map; -import junit.framework.TestCase; - -/** - * Tests for {@link UnknownFieldSetLite}. - * - * @author dweis@google.com (Daniel Weis) - */ -public class UnknownFieldSetLiteTest extends TestCase { - @Override - public void setUp() throws Exception { - allFields = TestUtil.getAllSet(); - allFieldsData = allFields.toByteString(); - emptyMessage = TestEmptyMessage.parseFrom(allFieldsData); - unknownFields = emptyMessage.getUnknownFields(); - } - - TestAllTypes allFields; - ByteString allFieldsData; - - // Constructs a protocol buffer which contains fields with all the same - // numbers as allFieldsData except that each field is some other wire - // type. - private ByteString getBizarroData() throws Exception { - UnknownFieldSet.Builder bizarroFields = UnknownFieldSet.newBuilder(); - - UnknownFieldSet.Field varintField = UnknownFieldSet.Field.newBuilder().addVarint(1).build(); - UnknownFieldSet.Field fixed32Field = UnknownFieldSet.Field.newBuilder().addFixed32(1).build(); - - for (Map.Entry entry : unknownFields.asMap().entrySet()) { - if (entry.getValue().getVarintList().isEmpty()) { - // Original field is not a varint, so use a varint. - bizarroFields.addField(entry.getKey(), varintField); - } else { - // Original field *is* a varint, so use something else. - bizarroFields.addField(entry.getKey(), fixed32Field); - } - } - - return bizarroFields.build().toByteString(); - } - - // An empty message that has been parsed from allFieldsData. So, it has - // unknown fields of every type. - TestEmptyMessage emptyMessage; - UnknownFieldSet unknownFields; - - public void testDefaultInstance() { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.getDefaultInstance(); - - assertEquals(0, unknownFields.getSerializedSize()); - assertEquals(ByteString.EMPTY, toByteString(unknownFields)); - } - - public void testEmptyInstance() { - UnknownFieldSetLite instance = UnknownFieldSetLite.newInstance(); - - assertEquals(0, instance.getSerializedSize()); - assertEquals(ByteString.EMPTY, toByteString(instance)); - assertEquals(UnknownFieldSetLite.getDefaultInstance(), instance); - } - - public void testMergeFieldFrom() throws IOException { - Foo foo = Foo.newBuilder() - .setValue(2) - .build(); - - CodedInputStream input = CodedInputStream.newInstance(foo.toByteArray()); - - UnknownFieldSetLite instance = UnknownFieldSetLite.newInstance(); - instance.mergeFieldFrom(input.readTag(), input); - - assertEquals(foo.toByteString(), toByteString(instance)); - } - - public void testSerializedSize() throws IOException { - Foo foo = Foo.newBuilder() - .setValue(2) - .build(); - - CodedInputStream input = CodedInputStream.newInstance(foo.toByteArray()); - - UnknownFieldSetLite instance = UnknownFieldSetLite.newInstance(); - instance.mergeFieldFrom(input.readTag(), input); - - assertEquals(foo.toByteString().size(), instance.getSerializedSize()); - } - - public void testHashCodeAfterDeserialization() throws IOException { - Foo foo = Foo.newBuilder() - .setValue(2) - .build(); - - Foo fooDeserialized = Foo.parseFrom(foo.toByteArray()); - - assertEquals(fooDeserialized, foo); - assertEquals(foo.hashCode(), fooDeserialized.hashCode()); - } - - public void testNewInstanceHashCode() { - UnknownFieldSetLite emptyFieldSet = UnknownFieldSetLite.getDefaultInstance(); - UnknownFieldSetLite paddedFieldSet = UnknownFieldSetLite.newInstance(); - - assertEquals(emptyFieldSet, paddedFieldSet); - assertEquals(emptyFieldSet.hashCode(), paddedFieldSet.hashCode()); - } - - public void testMergeVarintField() throws IOException { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance(); - unknownFields.mergeVarintField(10, 2); - - CodedInputStream input = - CodedInputStream.newInstance(toByteString(unknownFields).toByteArray()); - - int tag = input.readTag(); - assertEquals(10, WireFormat.getTagFieldNumber(tag)); - assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag)); - assertEquals(2, input.readUInt64()); - assertTrue(input.isAtEnd()); - } - - public void testMergeVarintField_negative() throws IOException { - UnknownFieldSetLite builder = UnknownFieldSetLite.newInstance(); - builder.mergeVarintField(10, -6); - - CodedInputStream input = - CodedInputStream.newInstance(toByteString(builder).toByteArray()); - - int tag = input.readTag(); - assertEquals(10, WireFormat.getTagFieldNumber(tag)); - assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag)); - assertEquals(-6, input.readUInt64()); - assertTrue(input.isAtEnd()); - } - - public void testEqualsAndHashCode() { - UnknownFieldSetLite unknownFields1 = UnknownFieldSetLite.newInstance(); - unknownFields1.mergeVarintField(10, 2); - - UnknownFieldSetLite unknownFields2 = UnknownFieldSetLite.newInstance(); - unknownFields2.mergeVarintField(10, 2); - - assertEquals(unknownFields1, unknownFields2); - assertEquals(unknownFields1.hashCode(), unknownFields2.hashCode()); - assertFalse(unknownFields1.equals(UnknownFieldSetLite.getDefaultInstance())); - assertFalse(unknownFields1.hashCode() == UnknownFieldSetLite.getDefaultInstance().hashCode()); - } - - public void testMutableCopyOf() throws IOException { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance(); - unknownFields.mergeVarintField(10, 2); - unknownFields = UnknownFieldSetLite.mutableCopyOf(unknownFields, unknownFields); - unknownFields.checkMutable(); - - CodedInputStream input = - CodedInputStream.newInstance(toByteString(unknownFields).toByteArray()); - - int tag = input.readTag(); - assertEquals(10, WireFormat.getTagFieldNumber(tag)); - assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag)); - assertEquals(2, input.readUInt64()); - assertFalse(input.isAtEnd()); - input.readTag(); - assertEquals(10, WireFormat.getTagFieldNumber(tag)); - assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag)); - assertEquals(2, input.readUInt64()); - assertTrue(input.isAtEnd()); - } - - public void testMutableCopyOf_empty() { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.mutableCopyOf( - UnknownFieldSetLite.getDefaultInstance(), UnknownFieldSetLite.getDefaultInstance()); - unknownFields.checkMutable(); - - assertEquals(0, unknownFields.getSerializedSize()); - assertEquals(ByteString.EMPTY, toByteString(unknownFields)); - } - - public void testRoundTrips() throws InvalidProtocolBufferException { - Foo foo = Foo.newBuilder() - .setValue(1) - .setExtension(Bar.fooExt, Bar.newBuilder() - .setName("name") - .build()) - .setExtension(LiteEqualsAndHash.varint, 22) - .setExtension(LiteEqualsAndHash.fixed32, 44) - .setExtension(LiteEqualsAndHash.fixed64, 66L) - .setExtension(LiteEqualsAndHash.myGroup, LiteEqualsAndHash.MyGroup.newBuilder() - .setGroupValue("value") - .build()) - .build(); - - Foo copy = Foo.parseFrom(foo.toByteArray()); - - assertEquals(foo.getSerializedSize(), copy.getSerializedSize()); - assertFalse(foo.equals(copy)); - - Foo secondCopy = Foo.parseFrom(foo.toByteArray()); - assertEquals(copy, secondCopy); - - ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); - LiteEqualsAndHash.registerAllExtensions(extensionRegistry); - Foo copyOfCopy = Foo.parseFrom(copy.toByteArray(), extensionRegistry); - - assertEquals(foo, copyOfCopy); - } - - public void testMalformedBytes() throws Exception { - try { - Foo.parseFrom("this is a malformed protocol buffer".getBytes(Internal.UTF_8)); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testMissingStartGroupTag() throws IOException { - ByteString.Output byteStringOutput = ByteString.newOutput(); - CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput); - output.writeGroupNoTag(Foo.newBuilder().setValue(11).build()); - output.writeTag(100, WireFormat.WIRETYPE_END_GROUP); - output.flush(); - - try { - Foo.parseFrom(byteStringOutput.toByteString()); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testMissingEndGroupTag() throws IOException { - ByteString.Output byteStringOutput = ByteString.newOutput(); - CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput); - output.writeTag(100, WireFormat.WIRETYPE_START_GROUP); - output.writeGroupNoTag(Foo.newBuilder().setValue(11).build()); - output.flush(); - - try { - Foo.parseFrom(byteStringOutput.toByteString()); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testMismatchingGroupTags() throws IOException { - ByteString.Output byteStringOutput = ByteString.newOutput(); - CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput); - output.writeTag(100, WireFormat.WIRETYPE_START_GROUP); - output.writeGroupNoTag(Foo.newBuilder().setValue(11).build()); - output.writeTag(101, WireFormat.WIRETYPE_END_GROUP); - output.flush(); - - try { - Foo.parseFrom(byteStringOutput.toByteString()); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testTruncatedInput() { - Foo foo = Foo.newBuilder() - .setValue(1) - .setExtension(Bar.fooExt, Bar.newBuilder() - .setName("name") - .build()) - .setExtension(LiteEqualsAndHash.varint, 22) - .setExtension(LiteEqualsAndHash.myGroup, LiteEqualsAndHash.MyGroup.newBuilder() - .setGroupValue("value") - .build()) - .build(); - - try { - Foo.parseFrom(foo.toByteString().substring(0, foo.toByteString().size() - 10)); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testMakeImmutable() throws Exception { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance(); - unknownFields.makeImmutable(); - - try { - unknownFields.mergeVarintField(1, 1); - fail(); - } catch (UnsupportedOperationException expected) {} - - try { - unknownFields.mergeLengthDelimitedField(2, ByteString.copyFromUtf8("hello")); - fail(); - } catch (UnsupportedOperationException expected) {} - - try { - unknownFields.mergeFieldFrom(1, CodedInputStream.newInstance(new byte[0])); - fail(); - } catch (UnsupportedOperationException expected) {} - } - - public void testEndToEnd() throws Exception { - TestAllTypesLite testAllTypes = TestAllTypesLite.getDefaultInstance(); - try { - testAllTypes.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) {} - - testAllTypes = TestAllTypesLite.parseFrom(new byte[0]); - try { - testAllTypes.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) {} - - testAllTypes = TestAllTypesLite.newBuilder().build(); - try { - testAllTypes.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) {} - - testAllTypes = TestAllTypesLite.newBuilder() - .setDefaultBool(true) - .build(); - try { - testAllTypes.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) {} - - TestAllExtensionsLite testAllExtensions = TestAllExtensionsLite.newBuilder() - .mergeFrom(TestAllExtensionsLite.newBuilder() - .setExtension(UnittestLite.optionalInt32ExtensionLite, 2) - .build().toByteArray()) - .build(); - try { - testAllExtensions.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) {} - } - - private ByteString toByteString(UnknownFieldSetLite unknownFields) { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStream); - try { - unknownFields.writeTo(output); - output.flush(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return ByteString.copyFrom(byteArrayOutputStream.toByteArray()); - } - - public void testSerializeLite() throws Exception { - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData); - assertEquals(allFieldsData.size(), emptyMessageLite.getSerializedSize()); - ByteString data = emptyMessageLite.toByteString(); - TestAllTypes message = TestAllTypes.parseFrom(data); - TestUtil.assertAllFieldsSet(message); - assertEquals(allFieldsData, data); - } - - public void testAllExtensionsLite() throws Exception { - TestAllExtensions allExtensions = TestUtil.getAllExtensionsSet(); - ByteString allExtensionsData = allExtensions.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parser().parseFrom(allExtensionsData); - ByteString data = emptyMessageLite.toByteString(); - TestAllExtensions message = TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry()); - TestUtil.assertAllExtensionsSet(message); - assertEquals(allExtensionsData, data); - } - - public void testAllPackedFieldsLite() throws Exception { - TestPackedTypes allPackedFields = TestUtil.getPackedSet(); - ByteString allPackedData = allPackedFields.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(allPackedData); - ByteString data = emptyMessageLite.toByteString(); - TestPackedTypes message = TestPackedTypes.parseFrom(data, TestUtil.getExtensionRegistry()); - TestUtil.assertPackedFieldsSet(message); - assertEquals(allPackedData, data); - } - - public void testAllPackedExtensionsLite() throws Exception { - TestPackedExtensions allPackedExtensions = TestUtil.getPackedExtensionsSet(); - ByteString allPackedExtensionsData = allPackedExtensions.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(allPackedExtensionsData); - ByteString data = emptyMessageLite.toByteString(); - TestPackedExtensions message = - TestPackedExtensions.parseFrom(data, TestUtil.getExtensionRegistry()); - TestUtil.assertPackedExtensionsSet(message); - assertEquals(allPackedExtensionsData, data); - } - - public void testCopyFromLite() throws Exception { - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData); - UnittestLite.TestEmptyMessageLite emptyMessageLite2 = - UnittestLite.TestEmptyMessageLite.newBuilder().mergeFrom(emptyMessageLite).build(); - assertEquals(emptyMessageLite.toByteString(), emptyMessageLite2.toByteString()); - } - - public void testMergeFromLite() throws Exception { - TestAllTypes message1 = - TestAllTypes.newBuilder() - .setOptionalInt32(1) - .setOptionalString("foo") - .addRepeatedString("bar") - .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAZ) - .build(); - - TestAllTypes message2 = - TestAllTypes.newBuilder() - .setOptionalInt64(2) - .setOptionalString("baz") - .addRepeatedString("qux") - .setOptionalForeignEnum(ForeignEnum.FOREIGN_BAZ) - .build(); - - ByteString data1 = message1.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite1 = - UnittestLite.TestEmptyMessageLite.parseFrom(data1); - ByteString data2 = message2.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite2 = - UnittestLite.TestEmptyMessageLite.parseFrom(data2); - - message1 = TestAllTypes.newBuilder(message1).mergeFrom(message2).build(); - emptyMessageLite1 = - UnittestLite.TestEmptyMessageLite.newBuilder(emptyMessageLite1) - .mergeFrom(emptyMessageLite2) - .build(); - - data1 = emptyMessageLite1.toByteString(); - message2 = TestAllTypes.parseFrom(data1); - - assertEquals(message1, message2); - } - - public void testWrongTypeTreatedAsUnknownLite() throws Exception { - // Test that fields of the wrong wire type are treated like unknown fields - // when parsing. - - ByteString bizarroData = getBizarroData(); - TestAllTypes allTypesMessage = TestAllTypes.parseFrom(bizarroData); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(bizarroData); - ByteString data = emptyMessageLite.toByteString(); - TestAllTypes allTypesMessage2 = TestAllTypes.parseFrom(data); - - assertEquals(allTypesMessage.toString(), allTypesMessage2.toString()); - } - - public void testUnknownExtensionsLite() throws Exception { - // Make sure fields are properly parsed to the UnknownFieldSet even when - // they are declared as extension numbers. - - UnittestLite.TestEmptyMessageWithExtensionsLite message = - UnittestLite.TestEmptyMessageWithExtensionsLite.parseFrom(allFieldsData); - - assertEquals(allFieldsData, message.toByteString()); - } - - public void testWrongExtensionTypeTreatedAsUnknownLite() throws Exception { - // Test that fields of the wrong wire type are treated like unknown fields - // when parsing extensions. - - ByteString bizarroData = getBizarroData(); - TestAllExtensions allExtensionsMessage = TestAllExtensions.parseFrom(bizarroData); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(bizarroData); - - // All fields should have been interpreted as unknown, so the byte strings - // should be the same. - assertEquals(emptyMessageLite.toByteString(), allExtensionsMessage.toByteString()); - } - - public void testParseUnknownEnumValueLite() throws Exception { - Descriptors.FieldDescriptor singularField = - TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum"); - Descriptors.FieldDescriptor repeatedField = - TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum"); - assertNotNull(singularField); - assertNotNull(repeatedField); - - ByteString data = - UnknownFieldSet.newBuilder() - .addField( - singularField.getNumber(), - UnknownFieldSet.Field.newBuilder() - .addVarint(TestAllTypes.NestedEnum.BAR.getNumber()) - .addVarint(5) // not valid - .build()) - .addField( - repeatedField.getNumber(), - UnknownFieldSet.Field.newBuilder() - .addVarint(TestAllTypes.NestedEnum.FOO.getNumber()) - .addVarint(4) // not valid - .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber()) - .addVarint(6) // not valid - .build()) - .build() - .toByteString(); - - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(data); - data = emptyMessageLite.toByteString(); - - { - TestAllTypes message = TestAllTypes.parseFrom(data); - assertEquals(TestAllTypes.NestedEnum.BAR, message.getOptionalNestedEnum()); - assertEquals( - Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ), - message.getRepeatedNestedEnumList()); - assertEquals( - Arrays.asList(5L), - message.getUnknownFields().getField(singularField.getNumber()).getVarintList()); - assertEquals( - Arrays.asList(4L, 6L), - message.getUnknownFields().getField(repeatedField.getNumber()).getVarintList()); - } - - { - TestAllExtensions message = - TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry()); - assertEquals( - TestAllTypes.NestedEnum.BAR, - message.getExtension(UnittestProto.optionalNestedEnumExtension)); - assertEquals( - Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ), - message.getExtension(UnittestProto.repeatedNestedEnumExtension)); - assertEquals( - Arrays.asList(5L), - message.getUnknownFields().getField(singularField.getNumber()).getVarintList()); - assertEquals( - Arrays.asList(4L, 6L), - message.getUnknownFields().getField(repeatedField.getNumber()).getVarintList()); - } - } - - public void testClearLite() throws Exception { - UnittestLite.TestEmptyMessageLite emptyMessageLite1 = - UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData); - UnittestLite.TestEmptyMessageLite emptyMessageLite2 = - UnittestLite.TestEmptyMessageLite.newBuilder().mergeFrom(emptyMessageLite1).clear().build(); - assertEquals(0, emptyMessageLite2.getSerializedSize()); - ByteString data = emptyMessageLite2.toByteString(); - assertEquals(0, data.size()); - } -} diff --git a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java index 1a84806a46..6316bd8532 100644 --- a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java +++ b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java @@ -69,13 +69,10 @@ public class UnknownFieldSetTest extends TestCase { ByteString getBizarroData() throws Exception { UnknownFieldSet.Builder bizarroFields = UnknownFieldSet.newBuilder(); - UnknownFieldSet.Field varintField = - UnknownFieldSet.Field.newBuilder().addVarint(1).build(); - UnknownFieldSet.Field fixed32Field = - UnknownFieldSet.Field.newBuilder().addFixed32(1).build(); + UnknownFieldSet.Field varintField = UnknownFieldSet.Field.newBuilder().addVarint(1).build(); + UnknownFieldSet.Field fixed32Field = UnknownFieldSet.Field.newBuilder().addFixed32(1).build(); - for (Map.Entry entry : - unknownFields.asMap().entrySet()) { + for (Map.Entry entry : unknownFields.asMap().entrySet()) { if (entry.getValue().getVarintList().isEmpty()) { // Original field is not a varint, so use a varint. bizarroFields.addField(entry.getKey(), varintField); @@ -102,34 +99,30 @@ public class UnknownFieldSetTest extends TestCase { public void testVarint() throws Exception { UnknownFieldSet.Field field = getField("optional_int32"); assertEquals(1, field.getVarintList().size()); - assertEquals(allFields.getOptionalInt32(), - (long) field.getVarintList().get(0)); + assertEquals(allFields.getOptionalInt32(), (long) field.getVarintList().get(0)); } public void testFixed32() throws Exception { UnknownFieldSet.Field field = getField("optional_fixed32"); assertEquals(1, field.getFixed32List().size()); - assertEquals(allFields.getOptionalFixed32(), - (int) field.getFixed32List().get(0)); + assertEquals(allFields.getOptionalFixed32(), (int) field.getFixed32List().get(0)); } public void testFixed64() throws Exception { UnknownFieldSet.Field field = getField("optional_fixed64"); assertEquals(1, field.getFixed64List().size()); - assertEquals(allFields.getOptionalFixed64(), - (long) field.getFixed64List().get(0)); + assertEquals(allFields.getOptionalFixed64(), (long) field.getFixed64List().get(0)); } public void testLengthDelimited() throws Exception { UnknownFieldSet.Field field = getField("optional_bytes"); assertEquals(1, field.getLengthDelimitedList().size()); - assertEquals(allFields.getOptionalBytes(), - field.getLengthDelimitedList().get(0)); + assertEquals(allFields.getOptionalBytes(), field.getLengthDelimitedList().get(0)); } public void testGroup() throws Exception { Descriptors.FieldDescriptor nestedFieldDescriptor = - TestAllTypes.OptionalGroup.getDescriptor().findFieldByName("a"); + TestAllTypes.OptionalGroup.getDescriptor().findFieldByName("a"); assertNotNull(nestedFieldDescriptor); UnknownFieldSet.Field field = getField("optionalgroup"); @@ -139,11 +132,9 @@ public class UnknownFieldSetTest extends TestCase { assertEquals(1, group.asMap().size()); assertTrue(group.hasField(nestedFieldDescriptor.getNumber())); - UnknownFieldSet.Field nestedField = - group.getField(nestedFieldDescriptor.getNumber()); + UnknownFieldSet.Field nestedField = group.getField(nestedFieldDescriptor.getNumber()); assertEquals(1, nestedField.getVarintList().size()); - assertEquals(allFields.getOptionalGroup().getA(), - (long) nestedField.getVarintList().get(0)); + assertEquals(allFields.getOptionalGroup().getA(), (long) nestedField.getVarintList().get(0)); } public void testSerialize() throws Exception { @@ -154,59 +145,44 @@ public class UnknownFieldSetTest extends TestCase { } public void testCopyFrom() throws Exception { - TestEmptyMessage message = - TestEmptyMessage.newBuilder().mergeFrom(emptyMessage).build(); + TestEmptyMessage message = TestEmptyMessage.newBuilder().mergeFrom(emptyMessage).build(); assertEquals(emptyMessage.toString(), message.toString()); } public void testMergeFrom() throws Exception { TestEmptyMessage source = - TestEmptyMessage.newBuilder() - .setUnknownFields( - UnknownFieldSet.newBuilder() - .addField(2, - UnknownFieldSet.Field.newBuilder() - .addVarint(2).build()) - .addField(3, - UnknownFieldSet.Field.newBuilder() - .addVarint(4).build()) - .build()) - .build(); + TestEmptyMessage.newBuilder() + .setUnknownFields( + UnknownFieldSet.newBuilder() + .addField(2, UnknownFieldSet.Field.newBuilder().addVarint(2).build()) + .addField(3, UnknownFieldSet.Field.newBuilder().addVarint(4).build()) + .build()) + .build(); TestEmptyMessage destination = - TestEmptyMessage.newBuilder() - .setUnknownFields( - UnknownFieldSet.newBuilder() - .addField(1, - UnknownFieldSet.Field.newBuilder() - .addVarint(1).build()) - .addField(3, - UnknownFieldSet.Field.newBuilder() - .addVarint(3).build()) - .build()) - .mergeFrom(source) - .build(); - - assertEquals( - "1: 1\n" + - "2: 2\n" + - "3: 3\n" + - "3: 4\n", - destination.toString()); + TestEmptyMessage.newBuilder() + .setUnknownFields( + UnknownFieldSet.newBuilder() + .addField(1, UnknownFieldSet.Field.newBuilder().addVarint(1).build()) + .addField(3, UnknownFieldSet.Field.newBuilder().addVarint(3).build()) + .build()) + .mergeFrom(source) + .build(); + + assertEquals("1: 1\n2: 2\n3: 3\n3: 4\n", destination.toString()); } public void testClear() throws Exception { - UnknownFieldSet fields = - UnknownFieldSet.newBuilder().mergeFrom(unknownFields).clear().build(); + UnknownFieldSet fields = UnknownFieldSet.newBuilder().mergeFrom(unknownFields).clear().build(); assertTrue(fields.asMap().isEmpty()); } public void testClearMessage() throws Exception { TestEmptyMessage message = - TestEmptyMessage.newBuilder().mergeFrom(emptyMessage).clear().build(); + TestEmptyMessage.newBuilder().mergeFrom(emptyMessage).clear().build(); assertEquals(0, message.getSerializedSize()); } - + public void testClearField() throws Exception { int fieldNumber = unknownFields.asMap().keySet().iterator().next(); UnknownFieldSet fields = @@ -218,10 +194,9 @@ public class UnknownFieldSetTest extends TestCase { // Test mixing known and unknown fields when parsing. UnknownFieldSet fields = - UnknownFieldSet.newBuilder(unknownFields) - .addField(123456, - UnknownFieldSet.Field.newBuilder().addVarint(654321).build()) - .build(); + UnknownFieldSet.newBuilder(unknownFields) + .addField(123456, UnknownFieldSet.Field.newBuilder().addVarint(654321).build()) + .build(); ByteString data = fields.toByteString(); TestAllTypes destination = TestAllTypes.parseFrom(data); @@ -229,8 +204,7 @@ public class UnknownFieldSetTest extends TestCase { TestUtil.assertAllFieldsSet(destination); assertEquals(1, destination.getUnknownFields().asMap().size()); - UnknownFieldSet.Field field = - destination.getUnknownFields().getField(123456); + UnknownFieldSet.Field field = destination.getUnknownFields().getField(123456); assertEquals(1, field.getVarintList().size()); assertEquals(654321, (long) field.getVarintList().get(0)); } @@ -253,10 +227,9 @@ public class UnknownFieldSetTest extends TestCase { // they are declared as extension numbers. TestEmptyMessageWithExtensions message = - TestEmptyMessageWithExtensions.parseFrom(allFieldsData); + TestEmptyMessageWithExtensions.parseFrom(allFieldsData); - assertEquals(unknownFields.asMap().size(), - message.getUnknownFields().asMap().size()); + assertEquals(unknownFields.asMap().size(), message.getUnknownFields().asMap().size()); assertEquals(allFieldsData, message.toByteString()); } @@ -265,134 +238,99 @@ public class UnknownFieldSetTest extends TestCase { // when parsing extensions. ByteString bizarroData = getBizarroData(); - TestAllExtensions allExtensionsMessage = - TestAllExtensions.parseFrom(bizarroData); + TestAllExtensions allExtensionsMessage = TestAllExtensions.parseFrom(bizarroData); TestEmptyMessage emptyMessage = TestEmptyMessage.parseFrom(bizarroData); // All fields should have been interpreted as unknown, so the debug strings // should be the same. - assertEquals(emptyMessage.toString(), - allExtensionsMessage.toString()); + assertEquals(emptyMessage.toString(), allExtensionsMessage.toString()); } public void testParseUnknownEnumValue() throws Exception { Descriptors.FieldDescriptor singularField = - TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum"); + TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum"); Descriptors.FieldDescriptor repeatedField = - TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum"); + TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum"); assertNotNull(singularField); assertNotNull(repeatedField); ByteString data = - UnknownFieldSet.newBuilder() - .addField(singularField.getNumber(), - UnknownFieldSet.Field.newBuilder() - .addVarint(TestAllTypes.NestedEnum.BAR.getNumber()) - .addVarint(5) // not valid - .build()) - .addField(repeatedField.getNumber(), - UnknownFieldSet.Field.newBuilder() - .addVarint(TestAllTypes.NestedEnum.FOO.getNumber()) - .addVarint(4) // not valid - .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber()) - .addVarint(6) // not valid - .build()) - .build() - .toByteString(); + UnknownFieldSet.newBuilder() + .addField( + singularField.getNumber(), + UnknownFieldSet.Field.newBuilder() + .addVarint(TestAllTypes.NestedEnum.BAR.getNumber()) + .addVarint(5) // not valid + .build()) + .addField( + repeatedField.getNumber(), + UnknownFieldSet.Field.newBuilder() + .addVarint(TestAllTypes.NestedEnum.FOO.getNumber()) + .addVarint(4) // not valid + .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber()) + .addVarint(6) // not valid + .build()) + .build() + .toByteString(); { TestAllTypes message = TestAllTypes.parseFrom(data); - assertEquals(TestAllTypes.NestedEnum.BAR, - message.getOptionalNestedEnum()); + assertEquals(TestAllTypes.NestedEnum.BAR, message.getOptionalNestedEnum()); assertEquals( - Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ), - message.getRepeatedNestedEnumList()); - assertEquals(Arrays.asList(5L), - message.getUnknownFields() - .getField(singularField.getNumber()) - .getVarintList()); - assertEquals(Arrays.asList(4L, 6L), - message.getUnknownFields() - .getField(repeatedField.getNumber()) - .getVarintList()); + Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ), + message.getRepeatedNestedEnumList()); + assertEquals( + Arrays.asList(5L), + message.getUnknownFields().getField(singularField.getNumber()).getVarintList()); + assertEquals( + Arrays.asList(4L, 6L), + message.getUnknownFields().getField(repeatedField.getNumber()).getVarintList()); } { TestAllExtensions message = - TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry()); - assertEquals(TestAllTypes.NestedEnum.BAR, - message.getExtension(UnittestProto.optionalNestedEnumExtension)); + TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry()); + assertEquals( + TestAllTypes.NestedEnum.BAR, + message.getExtension(UnittestProto.optionalNestedEnumExtension)); + assertEquals( + Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ), + message.getExtension(UnittestProto.repeatedNestedEnumExtension)); assertEquals( - Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ), - message.getExtension(UnittestProto.repeatedNestedEnumExtension)); - assertEquals(Arrays.asList(5L), - message.getUnknownFields() - .getField(singularField.getNumber()) - .getVarintList()); - assertEquals(Arrays.asList(4L, 6L), - message.getUnknownFields() - .getField(repeatedField.getNumber()) - .getVarintList()); + Arrays.asList(5L), + message.getUnknownFields().getField(singularField.getNumber()).getVarintList()); + assertEquals( + Arrays.asList(4L, 6L), + message.getUnknownFields().getField(repeatedField.getNumber()).getVarintList()); } } public void testLargeVarint() throws Exception { ByteString data = - UnknownFieldSet.newBuilder() - .addField(1, - UnknownFieldSet.Field.newBuilder() - .addVarint(0x7FFFFFFFFFFFFFFFL) - .build()) - .build() - .toByteString(); + UnknownFieldSet.newBuilder() + .addField(1, UnknownFieldSet.Field.newBuilder().addVarint(0x7FFFFFFFFFFFFFFFL).build()) + .build() + .toByteString(); UnknownFieldSet parsed = UnknownFieldSet.parseFrom(data); UnknownFieldSet.Field field = parsed.getField(1); assertEquals(1, field.getVarintList().size()); - assertEquals(0x7FFFFFFFFFFFFFFFL, (long)field.getVarintList().get(0)); + assertEquals(0x7FFFFFFFFFFFFFFFL, (long) field.getVarintList().get(0)); } public void testEqualsAndHashCode() { - UnknownFieldSet.Field fixed32Field = - UnknownFieldSet.Field.newBuilder() - .addFixed32(1) - .build(); - UnknownFieldSet.Field fixed64Field = - UnknownFieldSet.Field.newBuilder() - .addFixed64(1) - .build(); - UnknownFieldSet.Field varIntField = - UnknownFieldSet.Field.newBuilder() - .addVarint(1) - .build(); + UnknownFieldSet.Field fixed32Field = UnknownFieldSet.Field.newBuilder().addFixed32(1).build(); + UnknownFieldSet.Field fixed64Field = UnknownFieldSet.Field.newBuilder().addFixed64(1).build(); + UnknownFieldSet.Field varIntField = UnknownFieldSet.Field.newBuilder().addVarint(1).build(); UnknownFieldSet.Field lengthDelimitedField = - UnknownFieldSet.Field.newBuilder() - .addLengthDelimited(ByteString.EMPTY) - .build(); + UnknownFieldSet.Field.newBuilder().addLengthDelimited(ByteString.EMPTY).build(); UnknownFieldSet.Field groupField = - UnknownFieldSet.Field.newBuilder() - .addGroup(unknownFields) - .build(); + UnknownFieldSet.Field.newBuilder().addGroup(unknownFields).build(); - UnknownFieldSet a = - UnknownFieldSet.newBuilder() - .addField(1, fixed32Field) - .build(); - UnknownFieldSet b = - UnknownFieldSet.newBuilder() - .addField(1, fixed64Field) - .build(); - UnknownFieldSet c = - UnknownFieldSet.newBuilder() - .addField(1, varIntField) - .build(); - UnknownFieldSet d = - UnknownFieldSet.newBuilder() - .addField(1, lengthDelimitedField) - .build(); - UnknownFieldSet e = - UnknownFieldSet.newBuilder() - .addField(1, groupField) - .build(); + UnknownFieldSet a = UnknownFieldSet.newBuilder().addField(1, fixed32Field).build(); + UnknownFieldSet b = UnknownFieldSet.newBuilder().addField(1, fixed64Field).build(); + UnknownFieldSet c = UnknownFieldSet.newBuilder().addField(1, varIntField).build(); + UnknownFieldSet d = UnknownFieldSet.newBuilder().addField(1, lengthDelimitedField).build(); + UnknownFieldSet e = UnknownFieldSet.newBuilder().addField(1, groupField).build(); checkEqualsIsConsistent(a); checkEqualsIsConsistent(b); @@ -413,12 +351,10 @@ public class UnknownFieldSetTest extends TestCase { } /** - * Asserts that the given field sets are not equal and have different - * hash codes. + * Asserts that the given field sets are not equal and have different hash codes. * - * @warning It's valid for non-equal objects to have the same hash code, so - * this test is stricter than it needs to be. However, this should happen - * relatively rarely. + *

Note: It's valid for non-equal objects to have the same hash code, so this test is + * stricter than it needs to be. However, this should happen relatively rarely. */ private void checkNotEqual(UnknownFieldSet s1, UnknownFieldSet s2) { String equalsError = String.format("%s should not be equal to %s", s1, s2); @@ -430,9 +366,7 @@ public class UnknownFieldSetTest extends TestCase { s1.hashCode() == s2.hashCode()); } - /** - * Asserts that the given field sets are equal and have identical hash codes. - */ + /** Asserts that the given field sets are equal and have identical hash codes. */ private void checkEqualsIsConsistent(UnknownFieldSet set) { // Object should be equal to itself. assertEquals(set, set); diff --git a/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java b/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java index 00f201ca58..bc73cc9783 100644 --- a/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java +++ b/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java @@ -42,13 +42,13 @@ import junit.framework.TestCase; */ public class UnmodifiableLazyStringListTest extends TestCase { - private static String STRING_A = "A"; - private static String STRING_B = "B"; - private static String STRING_C = "C"; + private static final String STRING_A = "A"; + private static final String STRING_B = "B"; + private static final String STRING_C = "C"; - private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A"); - private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B"); - private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C"); + private static final ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A"); + private static final ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B"); + private static final ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C"); public void testReadOnlyMethods() { LazyStringArrayList rawList = createSampleList(); diff --git a/java/core/src/test/java/com/google/protobuf/Utf8Test.java b/java/core/src/test/java/com/google/protobuf/Utf8Test.java new file mode 100755 index 0000000000..bc3c985104 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Utf8Test.java @@ -0,0 +1,232 @@ +// 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. + +package com.google.protobuf; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Random; +import java.util.regex.Pattern; +import junit.framework.TestCase; + +/** Unit tests for {@link Utf8}. */ +public class Utf8Test extends TestCase { + private static final int NUM_CHARS = 16384; + + private static final Utf8.Processor safeProcessor = new Utf8.SafeProcessor(); + private static final Utf8.Processor unsafeProcessor = new Utf8.UnsafeProcessor(); + + public void testEncode() { + assertEncoding(randomString(0x80)); + assertEncoding(randomString(0x90)); + assertEncoding(randomString(0x800)); + assertEncoding(randomString(0x10000)); + assertEncoding(randomString(0x10ffff)); + } + + public void testEncode_insufficientSpace() { + assertEncoding_insufficientSpace(randomString(0x80)); + assertEncoding_insufficientSpace(randomString(0x90)); + assertEncoding_insufficientSpace(randomString(0x800)); + assertEncoding_insufficientSpace(randomString(0x10000)); + assertEncoding_insufficientSpace(randomString(0x10ffff)); + } + + public void testValid() { + assertIsValid(new byte[] {(byte) 0xE0, (byte) 0xB9, (byte) 0x96}, true); + assertIsValid(new byte[] {(byte) 0xF0, (byte) 0xB2, (byte) 0x83, (byte) 0xBC}, true); + } + + public void testOverlongIsInvalid() { + assertIsValid(new byte[] {(byte) 0xC0, (byte) 0x81}, false); + assertIsValid(new byte[] {(byte) 0xE0, (byte) 0x81, (byte) 0x81}, false); + assertIsValid(new byte[] {(byte) 0xF0, (byte) 0x81, (byte) 0x81, (byte) 0x81}, false); + } + + public void testMaxCodepointExceeded() { + // byte1 > 0xF4 + assertIsValid(new byte[] {(byte) 0xF5, (byte) 0x81, (byte) 0x81, (byte) 0x81}, false); + } + + public void testInvalidSurrogateCodepoint() { + assertIsValid(new byte[] {(byte) 0xED, (byte) 0xA1, (byte) 0x81}, false); + + // byte1 == 0xF0 && byte2 < 0x90 + assertIsValid(new byte[] {(byte) 0xF0, (byte) 0x81, (byte) 0x81, (byte) 0x81}, false); + // byte1 == 0xF4 && byte2 > 0x8F + assertIsValid(new byte[] {(byte) 0xF4, (byte) 0x90, (byte) 0x81, (byte) 0x81}, false); + } + + private static String randomString(int maxCodePoint) { + final long seed = 99; + final Random rnd = new Random(seed); + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < NUM_CHARS; j++) { + int codePoint; + do { + codePoint = rnd.nextInt(maxCodePoint); + } while (Utf8Utils.isSurrogate(codePoint)); + sb.appendCodePoint(codePoint); + } + return sb.toString(); + } + + private static void assertIsValid(byte[] data, boolean valid) { + assertEquals("isValidUtf8[ARRAY]", valid, safeProcessor.isValidUtf8(data, 0, data.length)); + assertEquals( + "isValidUtf8[ARRAY_UNSAFE]", valid, unsafeProcessor.isValidUtf8(data, 0, data.length)); + + ByteBuffer buffer = ByteBuffer.wrap(data); + assertEquals( + "isValidUtf8[NIO_HEAP]", + valid, + safeProcessor.isValidUtf8(buffer, buffer.position(), buffer.remaining())); + + // Direct buffers. + buffer = ByteBuffer.allocateDirect(data.length); + buffer.put(data); + buffer.flip(); + assertEquals( + "isValidUtf8[NIO_DEFAULT]", + valid, + safeProcessor.isValidUtf8(buffer, buffer.position(), buffer.remaining())); + assertEquals( + "isValidUtf8[NIO_UNSAFE]", + valid, + unsafeProcessor.isValidUtf8(buffer, buffer.position(), buffer.remaining())); + } + + private static void assertEncoding(String message) { + byte[] expected = message.getBytes(Internal.UTF_8); + byte[] output = encodeToByteArray(message, expected.length, safeProcessor); + assertTrue("encodeUtf8[ARRAY]", Arrays.equals(expected, output)); + + output = encodeToByteArray(message, expected.length, unsafeProcessor); + assertTrue("encodeUtf8[ARRAY_UNSAFE]", Arrays.equals(expected, output)); + + output = encodeToByteBuffer(message, expected.length, false, safeProcessor); + assertTrue("encodeUtf8[NIO_HEAP]", Arrays.equals(expected, output)); + + output = encodeToByteBuffer(message, expected.length, true, safeProcessor); + assertTrue("encodeUtf8[NIO_DEFAULT]", Arrays.equals(expected, output)); + + output = encodeToByteBuffer(message, expected.length, true, unsafeProcessor); + assertTrue("encodeUtf8[NIO_UNSAFE]", Arrays.equals(expected, output)); + } + + private void assertEncoding_insufficientSpace(String message) { + final int length = message.length() - 1; + Class clazz = ArrayIndexOutOfBoundsException.class; + + try { + encodeToByteArray(message, length, safeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + // Expected + assertExceptionType(t, clazz); + // byte[] + safeProcessor will not exit early. We can't match the message since we don't + // know which char/index due to random input. + } + + try { + encodeToByteArray(message, length, unsafeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + assertExceptionType(t, clazz); + // byte[] + unsafeProcessor will exit early, so we have can match the message. + assertExceptionMessage(t, length); + } + + try { + encodeToByteBuffer(message, length, false, safeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + // Expected + assertExceptionType(t, clazz); + // ByteBuffer + safeProcessor will not exit early. We can't match the message since we don't + // know which char/index due to random input. + } + + try { + encodeToByteBuffer(message, length, true, safeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + // Expected + assertExceptionType(t, clazz); + // ByteBuffer + safeProcessor will not exit early. We can't match the message since we don't + // know which char/index due to random input. + } + + try { + encodeToByteBuffer(message, length, true, unsafeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + // Expected + assertExceptionType(t, clazz); + // Direct ByteBuffer + unsafeProcessor will exit early if it's not on Android, so we can + // match the message. On Android, a direct ByteBuffer will have hasArray() being true and + // it will take a different code path and produces a different message. + if (!Android.isOnAndroidDevice()) { + assertExceptionMessage(t, length); + } + } + } + + private static byte[] encodeToByteArray(String message, int length, Utf8.Processor processor) { + byte[] output = new byte[length]; + processor.encodeUtf8(message, output, 0, output.length); + return output; + } + + private static byte[] encodeToByteBuffer( + String message, int length, boolean direct, Utf8.Processor processor) { + ByteBuffer buffer = direct ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); + + processor.encodeUtf8(message, buffer); + buffer.flip(); + + byte[] output = new byte[buffer.remaining()]; + buffer.get(output); + return output; + } + + private void assertExceptionType(Throwable t, Class expected) { + if (!expected.isAssignableFrom(t.getClass())) { + fail("Expected " + expected.getSimpleName() + ", but found " + t.getClass().getSimpleName()); + } + } + + private void assertExceptionMessage(Throwable t, int index) { + String pattern = "Failed writing (.) at index " + index; + assertTrue( + t.getMessage() + " does not match pattern " + pattern, + Pattern.matches(pattern, t.getMessage())); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Utf8Utils.java b/java/core/src/test/java/com/google/protobuf/Utf8Utils.java new file mode 100755 index 0000000000..dbd3d3961a --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Utf8Utils.java @@ -0,0 +1,193 @@ +// 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. + +package com.google.protobuf; + +import static java.lang.Character.MIN_HIGH_SURROGATE; +import static java.lang.Character.MIN_LOW_SURROGATE; +import static java.lang.Character.MIN_SURROGATE; + +import java.util.Random; + +/** Utilities for benchmarking UTF-8. */ +final class Utf8Utils { + private Utf8Utils() {} + + static class MaxCodePoint { + final int value; + + /** + * Convert the input string to a code point. Accepts regular decimal numerals, hex strings, and + * some symbolic names meaningful to humans. + */ + private static int decode(String userFriendly) { + try { + return Integer.decode(userFriendly); + } catch (NumberFormatException ignored) { + if (userFriendly.matches("(?i)(?:American|English|ASCII)")) { + // 1-byte UTF-8 sequences - "American" ASCII text + return 0x80; + } else if (userFriendly.matches("(?i)(?:Danish|Latin|Western.*European)")) { + // Mostly 1-byte UTF-8 sequences, mixed with occasional 2-byte + // sequences - "Western European" text + return 0x90; + } else if (userFriendly.matches("(?i)(?:Greek|Cyrillic|European|ISO.?8859)")) { + // Mostly 2-byte UTF-8 sequences - "European" text + return 0x800; + } else if (userFriendly.matches("(?i)(?:Chinese|Han|Asian|BMP)")) { + // Mostly 3-byte UTF-8 sequences - "Asian" text + return Character.MIN_SUPPLEMENTARY_CODE_POINT; + } else if (userFriendly.matches("(?i)(?:Cuneiform|rare|exotic|supplementary.*)")) { + // Mostly 4-byte UTF-8 sequences - "rare exotic" text + return Character.MAX_CODE_POINT; + } else { + throw new IllegalArgumentException("Can't decode codepoint " + userFriendly); + } + } + } + + public static MaxCodePoint valueOf(String userFriendly) { + return new MaxCodePoint(userFriendly); + } + + public MaxCodePoint(String userFriendly) { + value = decode(userFriendly); + } + } + + /** + * The Utf8 distribution of real data. The distribution is an array with length 4. + * "distribution[i]" means the total number of characters who are encoded with (i + 1) bytes. + * + *

GMM_UTF8_DISTRIBUTION is the distribution of gmm data set. GSR_UTF8_DISTRIBUTION is the + * distribution of gsreq/gsresp data set + */ + public enum Utf8Distribution { + GMM_UTF8_DISTRIBUTION { + @Override + public int[] getDistribution() { + return new int[] {53059, 104, 0, 0}; + } + }, + GSR_UTF8_DISTRIBUTION { + @Override + public int[] getDistribution() { + return new int[] {119458, 74, 2706, 0}; + } + }; + + public abstract int[] getDistribution(); + } + + /** + * Creates an array of random strings. + * + * @param stringCount the number of strings to be created. + * @param charCount the number of characters per string. + * @param maxCodePoint the maximum code point for the characters in the strings. + * @return an array of random strings. + */ + static String[] randomStrings(int stringCount, int charCount, MaxCodePoint maxCodePoint) { + final long seed = 99; + final Random rnd = new Random(seed); + String[] strings = new String[stringCount]; + for (int i = 0; i < stringCount; i++) { + strings[i] = randomString(rnd, charCount, maxCodePoint); + } + return strings; + } + + /** + * Creates a random string + * + * @param rnd the random generator. + * @param charCount the number of characters per string. + * @param maxCodePoint the maximum code point for the characters in the strings. + */ + static String randomString(Random rnd, int charCount, MaxCodePoint maxCodePoint) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < charCount; i++) { + int codePoint; + do { + codePoint = rnd.nextInt(maxCodePoint.value); + } while (Utf8Utils.isSurrogate(codePoint)); + sb.appendCodePoint(codePoint); + } + return sb.toString(); + } + + /** Character.isSurrogate was added in Java SE 7. */ + static boolean isSurrogate(int c) { + return Character.MIN_HIGH_SURROGATE <= c && c <= Character.MAX_LOW_SURROGATE; + } + + /** + * Creates an array of random strings according to UTF8 distribution. + * + * @param stringCount the number of strings to be created. + * @param charCount the number of characters per string. + */ + static String[] randomStringsWithDistribution( + int stringCount, int charCount, Utf8Distribution utf8Distribution) { + final int[] distribution = utf8Distribution.getDistribution(); + for (int i = 0; i < 3; i++) { + distribution[i + 1] += distribution[i]; + } + final long seed = 99; + final Random rnd = new Random(seed); + String[] strings = new String[stringCount]; + for (int i = 0; i < stringCount; i++) { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < charCount; j++) { + int codePoint; + do { + codePoint = rnd.nextInt(distribution[3]); + if (codePoint < distribution[0]) { + // 1 bytes + sb.append(0x7F); + } else if (codePoint < distribution[1]) { + // 2 bytes + sb.append(0x7FF); + } else if (codePoint < distribution[2]) { + // 3 bytes + sb.append(MIN_SURROGATE - 1); + } else { + // 4 bytes + sb.append(MIN_HIGH_SURROGATE); + sb.append(MIN_LOW_SURROGATE); + } + } while (Utf8Utils.isSurrogate(codePoint)); + sb.appendCodePoint(codePoint); + } + strings[i] = sb.toString(); + } + return strings; + } +} diff --git a/java/core/src/test/java/com/google/protobuf/WireFormatLiteTest.java b/java/core/src/test/java/com/google/protobuf/WireFormatLiteTest.java new file mode 100755 index 0000000000..a725d413ff --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/WireFormatLiteTest.java @@ -0,0 +1,1023 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.UnittestLite.optionalForeignEnumExtensionLite; + +import com.google.protobuf.UnittestLite.ForeignEnumLite; +import com.google.protobuf.UnittestLite.TestAllExtensionsLite; +import com.google.protobuf.UnittestLite.TestPackedExtensionsLite; +import map_test.MapForProto2TestProto; +import map_test.MapTestProto.TestMap; +import protobuf_unittest.UnittestMset.RawMessageSet; +import protobuf_unittest.UnittestMset.TestMessageSetExtension1; +import protobuf_unittest.UnittestMset.TestMessageSetExtension2; +import protobuf_unittest.UnittestProto; +import protobuf_unittest.UnittestProto.TestAllExtensions; +import protobuf_unittest.UnittestProto.TestAllTypes; +import protobuf_unittest.UnittestProto.TestExtensionInsideTable; +import protobuf_unittest.UnittestProto.TestFieldOrderings; +import protobuf_unittest.UnittestProto.TestOneof2; +import protobuf_unittest.UnittestProto.TestOneofBackwardsCompatible; +import protobuf_unittest.UnittestProto.TestPackedExtensions; +import protobuf_unittest.UnittestProto.TestPackedTypes; +import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet; +import proto3_unittest.UnittestProto3; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; +import junit.framework.TestCase; + +public class WireFormatLiteTest extends TestCase { + public void testSerializeExtensionsLite() throws Exception { + // TestAllTypes and TestAllExtensions should have compatible wire formats, + // so if we serialize a TestAllExtensions then parse it as TestAllTypes + // it should work. + + TestAllExtensionsLite message = TestUtilLite.getAllLiteExtensionsSet(); + ByteString rawBytes = message.toByteString(); + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); + + TestUtil.assertAllFieldsSet(message2); + } + + public void testSerializePackedExtensionsLite() throws Exception { + // TestPackedTypes and TestPackedExtensions should have compatible wire + // formats; check that they serialize to the same string. + TestPackedExtensionsLite message = TestUtilLite.getLitePackedExtensionsSet(); + ByteString rawBytes = message.toByteString(); + + TestPackedTypes message2 = TestUtil.getPackedSet(); + ByteString rawBytes2 = message2.toByteString(); + + assertEquals(rawBytes, rawBytes2); + } + + public void testParseExtensionsLite() throws Exception { + // TestAllTypes and TestAllExtensions should have compatible wire formats, + // so if we serialize a TestAllTypes then parse it as TestAllExtensions + // it should work. + + TestAllTypes message = TestUtil.getAllSet(); + ByteString rawBytes = message.toByteString(); + + ExtensionRegistryLite registryLite = TestUtilLite.getExtensionRegistryLite(); + + TestAllExtensionsLite message2 = TestAllExtensionsLite.parseFrom(rawBytes, registryLite); + TestUtil.assertAllExtensionsSet(message2); + message2 = TestAllExtensionsLite.parseFrom(message.toByteArray(), registryLite); + TestUtil.assertAllExtensionsSet(message2); + } + + public void testParsePackedExtensionsLite() throws Exception { + // Ensure that packed extensions can be properly parsed. + TestPackedExtensionsLite message = TestUtilLite.getLitePackedExtensionsSet(); + ByteString rawBytes = message.toByteString(); + + ExtensionRegistryLite registry = TestUtilLite.getExtensionRegistryLite(); + + TestPackedExtensionsLite message2 = TestPackedExtensionsLite.parseFrom(rawBytes, registry); + TestUtil.assertPackedExtensionsSet(message2); + message2 = TestPackedExtensionsLite.parseFrom(message.toByteArray(), registry); + TestUtil.assertPackedExtensionsSet(message2); + } + + public void testSerialization() throws Exception { + TestAllTypes message = TestUtil.getAllSet(); + + ByteString rawBytes = message.toByteString(); + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); + + TestUtil.assertAllFieldsSet(message2); + } + + public void testSerializationPacked() throws Exception { + TestPackedTypes message = TestUtil.getPackedSet(); + + ByteString rawBytes = message.toByteString(); + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestPackedTypes message2 = TestPackedTypes.parseFrom(rawBytes); + + TestUtil.assertPackedFieldsSet(message2); + } + + public void testSerializeExtensions() throws Exception { + // TestAllTypes and TestAllExtensions should have compatible wire formats, + // so if we serialize a TestAllExtensions then parse it as TestAllTypes + // it should work. + + TestAllExtensions message = TestUtil.getAllExtensionsSet(); + ByteString rawBytes = message.toByteString(); + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); + + TestUtil.assertAllFieldsSet(message2); + } + + public void testSerializePackedExtensions() throws Exception { + // TestPackedTypes and TestPackedExtensions should have compatible wire + // formats; check that they serialize to the same string. + TestPackedExtensions message = TestUtil.getPackedExtensionsSet(); + ByteString rawBytes = message.toByteString(); + + TestPackedTypes message2 = TestUtil.getPackedSet(); + ByteString rawBytes2 = message2.toByteString(); + + assertEquals(rawBytes, rawBytes2); + } + + public void testSerializationPackedWithoutGetSerializedSize() throws Exception { + // Write directly to an OutputStream, without invoking getSerializedSize() + // This used to be a bug where the size of a packed field was incorrect, + // since getSerializedSize() was never invoked. + TestPackedTypes message = TestUtil.getPackedSet(); + + // Directly construct a CodedOutputStream around the actual OutputStream, + // in case writeTo(OutputStream output) invokes getSerializedSize(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(outputStream); + + message.writeTo(codedOutput); + + codedOutput.flush(); + + TestPackedTypes message2 = TestPackedTypes.parseFrom(outputStream.toByteArray()); + + TestUtil.assertPackedFieldsSet(message2); + } + + public void testParseExtensions() throws Exception { + // TestAllTypes and TestAllExtensions should have compatible wire formats, + // so if we serialize a TestAllTypes then parse it as TestAllExtensions + // it should work. + + TestAllTypes message = TestUtil.getAllSet(); + ByteString rawBytes = message.toByteString(); + + ExtensionRegistryLite registry = TestUtil.getExtensionRegistry(); + + TestAllExtensions message2 = TestAllExtensions.parseFrom(rawBytes, registry); + + TestUtil.assertAllExtensionsSet(message2); + } + + public void testParsePackedExtensions() throws Exception { + // Ensure that packed extensions can be properly parsed. + TestPackedExtensions message = TestUtil.getPackedExtensionsSet(); + ByteString rawBytes = message.toByteString(); + + ExtensionRegistryLite registry = TestUtil.getExtensionRegistry(); + + TestPackedExtensions message2 = TestPackedExtensions.parseFrom(rawBytes, registry); + + TestUtil.assertPackedExtensionsSet(message2); + } + + public void testSerializeDelimited() throws Exception { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + TestUtil.getAllSet().writeDelimitedTo(output); + output.write(12); + TestUtil.getPackedSet().writeDelimitedTo(output); + output.write(34); + + ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); + + TestUtil.assertAllFieldsSet(TestAllTypes.parseDelimitedFrom(input)); + assertEquals(12, input.read()); + TestUtil.assertPackedFieldsSet(TestPackedTypes.parseDelimitedFrom(input)); + assertEquals(34, input.read()); + assertEquals(-1, input.read()); + + // We're at EOF, so parsing again should return null. + assertNull(TestAllTypes.parseDelimitedFrom(input)); + } + + private ExtensionRegistryLite getTestFieldOrderingsRegistry() { + ExtensionRegistryLite result = ExtensionRegistryLite.newInstance(); + result.add(UnittestProto.myExtensionInt); + result.add(UnittestProto.myExtensionString); + return result; + } + + public void testParseMultipleExtensionRanges() throws Exception { + // Make sure we can parse a message that contains multiple extensions + // ranges. + TestFieldOrderings source = + TestFieldOrderings.newBuilder() + .setMyInt(1) + .setMyString("foo") + .setMyFloat(1.0F) + .setExtension(UnittestProto.myExtensionInt, 23) + .setExtension(UnittestProto.myExtensionString, "bar") + .build(); + TestFieldOrderings dest = + TestFieldOrderings.parseFrom(source.toByteString(), getTestFieldOrderingsRegistry()); + assertEquals(source, dest); + } + + private static ExtensionRegistryLite getTestExtensionInsideTableRegistry() { + ExtensionRegistryLite result = ExtensionRegistryLite.newInstance(); + result.add(UnittestProto.testExtensionInsideTableExtension); + return result; + } + + public void testExtensionInsideTable() throws Exception { + // Make sure the extension within the range of table is parsed correctly in experimental + // runtime. + TestExtensionInsideTable source = + TestExtensionInsideTable.newBuilder() + .setField1(1) + .setExtension(UnittestProto.testExtensionInsideTableExtension, 23) + .build(); + TestExtensionInsideTable dest = + TestExtensionInsideTable.parseFrom( + source.toByteString(), getTestExtensionInsideTableRegistry()); + assertEquals(source, dest); + } + + private static final int UNKNOWN_TYPE_ID = 1550055; + private static final int TYPE_ID_1 = 1545008; + private static final int TYPE_ID_2 = 1547769; + + public void testSerializeMessageSetEagerly() throws Exception { + testSerializeMessageSetWithFlag(true); + } + + public void testSerializeMessageSetNotEagerly() throws Exception { + testSerializeMessageSetWithFlag(false); + } + + private void testSerializeMessageSetWithFlag(boolean eagerParsing) throws Exception { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStream); + output.writeRawMessageSetExtension(UNKNOWN_TYPE_ID, ByteString.copyFromUtf8("bar")); + output.flush(); + byte[] messageSetBytes = byteArrayOutputStream.toByteArray(); + + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + // Set up a TestMessageSet with two known messages and an unknown one. + TestMessageSet messageSet = + TestMessageSet.newBuilder() + .setExtension( + TestMessageSetExtension1.messageSetExtension, + TestMessageSetExtension1.newBuilder().setI(123).build()) + .setExtension( + TestMessageSetExtension2.messageSetExtension, + TestMessageSetExtension2.newBuilder().setStr("foo").build()) + .mergeFrom(messageSetBytes) + .build(); + + ByteString data = messageSet.toByteString(); + + // Parse back using RawMessageSet and check the contents. + RawMessageSet raw = RawMessageSet.parseFrom(data); + + assertEquals(3, raw.getItemCount()); + assertEquals(TYPE_ID_1, raw.getItem(0).getTypeId()); + assertEquals(TYPE_ID_2, raw.getItem(1).getTypeId()); + assertEquals(UNKNOWN_TYPE_ID, raw.getItem(2).getTypeId()); + + TestMessageSetExtension1 message1 = + TestMessageSetExtension1.parseFrom(raw.getItem(0).getMessage()); + assertEquals(123, message1.getI()); + + TestMessageSetExtension2 message2 = + TestMessageSetExtension2.parseFrom(raw.getItem(1).getMessage()); + assertEquals("foo", message2.getStr()); + + assertEquals("bar", raw.getItem(2).getMessage().toStringUtf8()); + } + + public void testParseMessageSetEagerly() throws Exception { + testParseMessageSetWithFlag(true); + } + + public void testParseMessageSetNotEagerly() throws Exception { + testParseMessageSetWithFlag(false); + } + + private void testParseMessageSetWithFlag(boolean eagerParsing) throws Exception { + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); + extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); + extensionRegistry.add(TestMessageSetExtension2.messageSetExtension); + + // Set up a RawMessageSet with two known messages and an unknown one. + RawMessageSet raw = + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_2) + .setMessage( + TestMessageSetExtension2.newBuilder().setStr("foo").build().toByteString()) + .build()) + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(UNKNOWN_TYPE_ID) + .setMessage(ByteString.copyFromUtf8("bar")) + .build()) + .build(); + + ByteString data = raw.toByteString(); + + // Parse as a TestMessageSet and check the contents. + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); + + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + assertEquals( + "foo", messageSet.getExtension(TestMessageSetExtension2.messageSetExtension).getStr()); + } + + public void testParseMessageSetExtensionEagerly() throws Exception { + testParseMessageSetExtensionWithFlag(true); + } + + public void testParseMessageSetExtensionNotEagerly() throws Exception { + testParseMessageSetExtensionWithFlag(false); + } + + private void testParseMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); + extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); + + // Set up a RawMessageSet with a known messages. + int typeId1 = 1545008; + RawMessageSet raw = + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(typeId1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); + + ByteString data = raw.toByteString(); + + // Parse as a TestMessageSet and check the contents. + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + } + + public void testMergeLazyMessageSetExtensionEagerly() throws Exception { + testMergeLazyMessageSetExtensionWithFlag(true); + } + + public void testMergeLazyMessageSetExtensionNotEagerly() throws Exception { + testMergeLazyMessageSetExtensionWithFlag(false); + } + + private void testMergeLazyMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); + extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); + + // Set up a RawMessageSet with a known messages. + int typeId1 = 1545008; + RawMessageSet raw = + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(typeId1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); + + ByteString data = raw.toByteString(); + + // Parse as a TestMessageSet and store value into lazy field + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); + // Merge lazy field check the contents. + messageSet = messageSet.toBuilder().mergeFrom(data, extensionRegistry).build(); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + } + + public void testMergeMessageSetExtensionEagerly() throws Exception { + testMergeMessageSetExtensionWithFlag(true); + } + + public void testMergeMessageSetExtensionNotEagerly() throws Exception { + testMergeMessageSetExtensionWithFlag(false); + } + + private void testMergeMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); + extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); + + // Set up a RawMessageSet with a known messages. + int typeId1 = 1545008; + RawMessageSet raw = + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(typeId1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); + + // Serialize RawMessageSet unnormally (message value before type id) + ByteString.CodedBuilder out = ByteString.newCodedBuilder(raw.getSerializedSize()); + CodedOutputStream output = out.getCodedOutput(); + List items = raw.getItemList(); + for (RawMessageSet.Item item : items) { + output.writeTag(1, WireFormat.WIRETYPE_START_GROUP); + output.writeBytes(3, item.getMessage()); + output.writeInt32(2, item.getTypeId()); + output.writeTag(1, WireFormat.WIRETYPE_END_GROUP); + } + ByteString data = out.build(); + + // Merge bytes into TestMessageSet and check the contents. + TestMessageSet messageSet = + TestMessageSet.newBuilder().mergeFrom(data, extensionRegistry).build(); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + } + + // ================================================================ + // oneof + public void testOneofWireFormat() throws Exception { + TestOneof2.Builder builder = TestOneof2.newBuilder(); + TestUtil.setOneof(builder); + TestOneof2 message = builder.build(); + ByteString rawBytes = message.toByteString(); + + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestOneof2 message2 = TestOneof2.parseFrom(rawBytes); + TestUtil.assertOneofSet(message2); + } + + public void testOneofOnlyLastSet() throws Exception { + TestOneofBackwardsCompatible source = + TestOneofBackwardsCompatible.newBuilder().setFooInt(100).setFooString("101").build(); + + ByteString rawBytes = source.toByteString(); + TestOneof2 message = TestOneof2.parseFrom(rawBytes); + assertFalse(message.hasFooInt()); + assertTrue(message.hasFooString()); + } + + private void assertInvalidWireFormat( + MessageLite defaultInstance, byte[] data, int offset, int length) { + // Test all combinations: (builder vs parser) x (byte[] vs. InputStream). + try { + defaultInstance.newBuilderForType().mergeFrom(data, offset, length); + fail("Expected exception"); + } catch (InvalidProtocolBufferException e) { + // Pass. + } + try { + defaultInstance.getParserForType().parseFrom(data, offset, length); + fail("Expected exception"); + } catch (InvalidProtocolBufferException e) { + // Pass. + } + try { + InputStream input = new ByteArrayInputStream(data, offset, length); + defaultInstance.newBuilderForType().mergeFrom(input); + fail("Expected exception"); + } catch (IOException e) { + // Pass. + } + try { + InputStream input = new ByteArrayInputStream(data, offset, length); + defaultInstance.getParserForType().parseFrom(input); + fail("Expected exception"); + } catch (IOException e) { + // Pass. + } + } + + private void assertInvalidWireFormat(MessageLite defaultInstance, byte[] data) { + assertInvalidWireFormat(defaultInstance, data, 0, data.length); + } + + private void assertInvalidWireFormat(byte[] data) { + assertInvalidWireFormat(TestAllTypes.getDefaultInstance(), data); + assertInvalidWireFormat(UnittestProto3.TestAllTypes.getDefaultInstance(), data); + } + + public void testParserRejectInvalidTag() throws Exception { + byte[] invalidTags = + new byte[] { + // Zero tag is not allowed. + 0, + // Invalid wire types. + (byte) WireFormat.makeTag(1, 6), + (byte) WireFormat.makeTag(1, 7), + // Field number 0 is not allowed. + (byte) WireFormat.makeTag(0, WireFormat.WIRETYPE_VARINT), + }; + for (byte invalidTag : invalidTags) { + // Add a trailing 0 to make sure the parsing actually fails on the tag. + byte[] data = new byte[] {invalidTag, 0}; + assertInvalidWireFormat(data); + + // Invalid tag in an unknown group field. + data = + new byte[] { + (byte) WireFormat.makeTag(1, WireFormat.WIRETYPE_START_GROUP), + invalidTag, + 0, + (byte) WireFormat.makeTag(1, WireFormat.WIRETYPE_END_GROUP), + }; + assertInvalidWireFormat(data); + + // Invalid tag in a MessageSet item. + data = + new byte[] { + (byte) WireFormat.MESSAGE_SET_ITEM_TAG, + (byte) WireFormat.MESSAGE_SET_TYPE_ID_TAG, + 100, // TYPE_ID = 100 + (byte) WireFormat.MESSAGE_SET_MESSAGE_TAG, + 0, // empty payload + invalidTag, + 0, + (byte) WireFormat.MESSAGE_SET_ITEM_END_TAG, + }; + assertInvalidWireFormat(TestMessageSet.getDefaultInstance(), data); + + // Invalid tag inside a MessageSet item's unknown group. + data = + new byte[] { + (byte) WireFormat.MESSAGE_SET_ITEM_TAG, + (byte) WireFormat.MESSAGE_SET_TYPE_ID_TAG, + 100, // TYPE_ID = 100 + (byte) WireFormat.MESSAGE_SET_MESSAGE_TAG, + 0, // empty payload + (byte) WireFormat.makeTag(4, WireFormat.WIRETYPE_START_GROUP), + invalidTag, + 0, + (byte) WireFormat.makeTag(4, WireFormat.WIRETYPE_END_GROUP), + (byte) WireFormat.MESSAGE_SET_ITEM_END_TAG, + }; + assertInvalidWireFormat(TestMessageSet.getDefaultInstance(), data); + + // Invalid tag inside a map field. + data = + new byte[] { + (byte) WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED), 2, invalidTag, 0, + }; + assertInvalidWireFormat(TestMap.getDefaultInstance(), data); + } + } + + public void testUnmatchedGroupTag() throws Exception { + int startTag = WireFormat.makeTag(16, WireFormat.WIRETYPE_START_GROUP); + byte[] data = + new byte[] { + (byte) ((startTag & 0x7F) | 0x80), (byte) ((startTag >>> 7) & 0x7F), + }; + assertInvalidWireFormat(data); + + // Unmatched group tags inside a MessageSet item. + data = + new byte[] { + (byte) WireFormat.MESSAGE_SET_ITEM_TAG, + (byte) WireFormat.MESSAGE_SET_TYPE_ID_TAG, + 100, // TYPE_ID = 100 + (byte) WireFormat.MESSAGE_SET_MESSAGE_TAG, + 0, // empty payload + (byte) WireFormat.makeTag(4, WireFormat.WIRETYPE_START_GROUP), + }; + assertInvalidWireFormat(TestMessageSet.getDefaultInstance(), data); + } + + private void assertAccepted(MessageLite defaultInstance, byte[] data) throws Exception { + MessageLite message1 = defaultInstance.newBuilderForType().mergeFrom(data).build(); + MessageLite message2 = defaultInstance.getParserForType().parseFrom(data); + MessageLite message3 = + defaultInstance.newBuilderForType().mergeFrom(new ByteArrayInputStream(data)).build(); + MessageLite message4 = + defaultInstance.getParserForType().parseFrom(new ByteArrayInputStream(data)); + assertEquals(message1, message2); + assertEquals(message2, message3); + assertEquals(message3, message4); + } + + public void testUnmatchedWireType() throws Exception { + // Build a payload with all fields from 1 to 128 being varints. Parsing it into TestAllTypes + // or other message types should succeed even though the wire type doesn't match for some + // fields. + ByteArrayOutputStream output = new ByteArrayOutputStream(); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + for (int i = 1; i <= 128; i++) { + codedOutput.writeInt32(i, 0); + } + codedOutput.flush(); + byte[] data = output.toByteArray(); + // It can be parsed into any message type that doesn't have required fields. + assertAccepted(TestAllTypes.getDefaultInstance(), data); + assertAccepted(UnittestProto3.TestAllTypes.getDefaultInstance(), data); + assertAccepted(TestMap.getDefaultInstance(), data); + assertAccepted(MapForProto2TestProto.TestMap.getDefaultInstance(), data); + } + + public void testParseTruncatedPackedFields() throws Exception { + TestPackedTypes all = TestUtil.getPackedSet(); + TestPackedTypes[] messages = + new TestPackedTypes[] { + TestPackedTypes.newBuilder().addAllPackedInt32(all.getPackedInt32List()).build(), + TestPackedTypes.newBuilder().addAllPackedInt64(all.getPackedInt64List()).build(), + TestPackedTypes.newBuilder().addAllPackedUint32(all.getPackedUint32List()).build(), + TestPackedTypes.newBuilder().addAllPackedUint64(all.getPackedUint64List()).build(), + TestPackedTypes.newBuilder().addAllPackedSint32(all.getPackedSint32List()).build(), + TestPackedTypes.newBuilder().addAllPackedSint64(all.getPackedSint64List()).build(), + TestPackedTypes.newBuilder().addAllPackedFixed32(all.getPackedFixed32List()).build(), + TestPackedTypes.newBuilder().addAllPackedFixed64(all.getPackedFixed64List()).build(), + TestPackedTypes.newBuilder().addAllPackedSfixed32(all.getPackedSfixed32List()).build(), + TestPackedTypes.newBuilder().addAllPackedSfixed64(all.getPackedSfixed64List()).build(), + TestPackedTypes.newBuilder().addAllPackedFloat(all.getPackedFloatList()).build(), + TestPackedTypes.newBuilder().addAllPackedDouble(all.getPackedDoubleList()).build(), + TestPackedTypes.newBuilder().addAllPackedEnum(all.getPackedEnumList()).build(), + }; + for (TestPackedTypes message : messages) { + byte[] data = message.toByteArray(); + // Parsing truncated payload should fail. + for (int i = 1; i < data.length; i++) { + assertInvalidWireFormat(TestPackedTypes.getDefaultInstance(), data, 0, i); + } + } + } + + public void testParsePackedFieldsWithIncorrectLength() throws Exception { + // Set the length-prefix to 1 with a 4-bytes payload to test what happens when reading a packed + // element moves the reading position past the given length limit. It should result in an + // InvalidProtocolBufferException but an implementation may forget to check it especially for + // packed varint fields. + byte[] data = + new byte[] { + 0, + 0, // first two bytes is reserved for the tag. + 1, // length is 1 + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x01, // a 4-bytes varint + }; + // All fields that can read a 4-bytes varint (all varint fields and fixed 32-bit fields). + int[] fieldNumbers = + new int[] { + TestPackedTypes.PACKED_INT32_FIELD_NUMBER, + TestPackedTypes.PACKED_INT64_FIELD_NUMBER, + TestPackedTypes.PACKED_UINT32_FIELD_NUMBER, + TestPackedTypes.PACKED_UINT64_FIELD_NUMBER, + TestPackedTypes.PACKED_SINT32_FIELD_NUMBER, + TestPackedTypes.PACKED_SINT64_FIELD_NUMBER, + TestPackedTypes.PACKED_FIXED32_FIELD_NUMBER, + TestPackedTypes.PACKED_SFIXED32_FIELD_NUMBER, + TestPackedTypes.PACKED_FLOAT_FIELD_NUMBER, + TestPackedTypes.PACKED_BOOL_FIELD_NUMBER, + TestPackedTypes.PACKED_ENUM_FIELD_NUMBER, + }; + for (int number : fieldNumbers) { + // Set the tag. + data[0] = + (byte) ((WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED) & 0x7F) | 0x80); + data[1] = + (byte) ((WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED) >>> 7) & 0x7F); + assertInvalidWireFormat(TestPackedTypes.getDefaultInstance(), data); + } + + // Data with 8-bytes payload to test some fixed 64-bit fields. + byte[] data8Bytes = + new byte[] { + 0, + 0, // first two bytes is reserved for the tag. + 1, // length is 1 + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x01, // a 8-bytes varint + }; + // All fields that can only read 8-bytes data. + int[] fieldNumbers8Bytes = + new int[] { + TestPackedTypes.PACKED_FIXED64_FIELD_NUMBER, + TestPackedTypes.PACKED_SFIXED64_FIELD_NUMBER, + TestPackedTypes.PACKED_DOUBLE_FIELD_NUMBER, + }; + for (int number : fieldNumbers8Bytes) { + // Set the tag. + data8Bytes[0] = + (byte) ((WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED) & 0x7F) | 0x80); + data8Bytes[1] = + (byte) ((WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED) >>> 7) & 0x7F); + assertInvalidWireFormat(TestPackedTypes.getDefaultInstance(), data8Bytes); + } + } + + public void testParseVarintMinMax() throws Exception { + TestAllTypes message = + TestAllTypes.newBuilder() + .setOptionalInt32(Integer.MIN_VALUE) + .addRepeatedInt32(Integer.MAX_VALUE) + .setOptionalInt64(Long.MIN_VALUE) + .addRepeatedInt64(Long.MAX_VALUE) + .build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(Integer.MIN_VALUE, parsed.getOptionalInt32()); + assertEquals(Integer.MAX_VALUE, parsed.getRepeatedInt32(0)); + assertEquals(Long.MIN_VALUE, parsed.getOptionalInt64()); + assertEquals(Long.MAX_VALUE, parsed.getRepeatedInt64(0)); + } + + public void testParseAllVarintBits() throws Exception { + for (int i = 0; i < 32; i++) { + final int value = 1 << i; + TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(value).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(value, parsed.getOptionalInt32()); + } + for (int i = 0; i < 64; i++) { + final long value = 1L << i; + TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt64(value).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(value, parsed.getOptionalInt64()); + } + } + + public void testParseEmptyUnknownLengthDelimitedField() throws Exception { + byte[] data = + new byte[] {(byte) WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED), 0}; + TestAllTypes parsed = TestAllTypes.parseFrom(data); + assertTrue(Arrays.equals(data, parsed.toByteArray())); + } + + public void testParseEmptyString() throws Exception { + TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("").build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals("", parsed.getOptionalString()); + } + + public void testParseEmptyStringProto3() throws Exception { + TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("").build(); + // Note that we are parsing from a proto2 proto to a proto3 proto because empty string field is + // not serialized in proto3. + UnittestProto3.TestAllTypes parsed = + UnittestProto3.TestAllTypes.parseFrom(message.toByteArray()); + assertEquals("", parsed.getOptionalString()); + } + + public void testParseEmptyBytes() throws Exception { + TestAllTypes message = TestAllTypes.newBuilder().setOptionalBytes(ByteString.EMPTY).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(ByteString.EMPTY, parsed.getOptionalBytes()); + } + + public void testParseEmptyRepeatedStringField() throws Exception { + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedString("") + .addRepeatedString("") + .addRepeatedString("0") + .build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(3, parsed.getRepeatedStringCount()); + assertEquals("", parsed.getRepeatedString(0)); + assertEquals("", parsed.getRepeatedString(1)); + assertEquals("0", parsed.getRepeatedString(2)); + } + + public void testParseEmptyRepeatedStringFieldProto3() throws Exception { + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedString("") + .addRepeatedString("") + .addRepeatedString("0") + .addRepeatedBytes(ByteString.EMPTY) + .build(); + UnittestProto3.TestAllTypes parsed = + UnittestProto3.TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(3, parsed.getRepeatedStringCount()); + assertEquals("", parsed.getRepeatedString(0)); + assertEquals("", parsed.getRepeatedString(1)); + assertEquals("0", parsed.getRepeatedString(2)); + } + + public void testParseEmptyRepeatedBytesField() throws Exception { + ByteString oneByte = ByteString.copyFrom(new byte[] {1}); + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedBytes(ByteString.EMPTY) + .addRepeatedBytes(ByteString.EMPTY) + .addRepeatedBytes(oneByte) + .build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(3, parsed.getRepeatedBytesCount()); + assertEquals(ByteString.EMPTY, parsed.getRepeatedBytes(0)); + assertEquals(ByteString.EMPTY, parsed.getRepeatedBytes(1)); + assertEquals(oneByte, parsed.getRepeatedBytes(2)); + } + + public void testSkipUnknownFieldInMessageSetItem() throws Exception { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + // MessageSet item's start tag. + output.write((byte) WireFormat.MESSAGE_SET_ITEM_TAG); + // Put all field types into the item. + TestUtil.getAllSet().writeTo(output); + // Closing the item with the real payload and closing tag. + output.write( + new byte[] { + (byte) WireFormat.MESSAGE_SET_TYPE_ID_TAG, + 100, // TYPE_ID = 100 + (byte) WireFormat.MESSAGE_SET_MESSAGE_TAG, + 0, // empty payload + (byte) WireFormat.MESSAGE_SET_ITEM_END_TAG, + }); + byte[] data = output.toByteArray(); + TestMessageSet parsed = TestMessageSet.parseFrom(data); + + // Convert to RawMessageSet for inspection. + RawMessageSet raw = RawMessageSet.parseFrom(parsed.toByteArray()); + assertEquals(1, raw.getItemCount()); + assertEquals(100, raw.getItem(0).getTypeId()); + assertEquals(0, raw.getItem(0).getMessage().size()); + } + + public void testProto2UnknownEnumValuesInOptionalField() throws Exception { + // Proto2 doesn't allow setting unknown enum values so we use proto3 to build a message with + // unknown enum values + UnittestProto3.TestAllTypes message = + UnittestProto3.TestAllTypes.newBuilder().setOptionalNestedEnumValue(4321).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertFalse(parsed.hasOptionalNestedEnum()); + // Make sure unknown enum values are preserved. + UnittestProto3.TestAllTypes actual = + UnittestProto3.TestAllTypes.parseFrom(parsed.toByteArray()); + assertEquals(4321, actual.getOptionalNestedEnumValue()); + } + + public void testProto2UnknownEnumValuesInRepeatedField() throws Exception { + // Proto2 doesn't allow setting unknown enum values so we use proto3 to build a message with + // unknown enum values + UnittestProto3.TestAllTypes message = + UnittestProto3.TestAllTypes.newBuilder().addRepeatedNestedEnumValue(5432).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(0, parsed.getRepeatedNestedEnumCount()); + // Make sure unknown enum values are preserved. + UnittestProto3.TestAllTypes actual = + UnittestProto3.TestAllTypes.parseFrom(parsed.toByteArray()); + assertEquals(1, actual.getRepeatedNestedEnumCount()); + assertEquals(5432, actual.getRepeatedNestedEnumValue(0)); + } + + public void testProto2UnknownEnumValuesInMapField() throws Exception { + // Proto2 doesn't allow setting unknown enum values so we use proto3 to build a message with + // unknown enum values + TestMap message = TestMap.newBuilder().putInt32ToEnumFieldValue(1, 4321).build(); + MapForProto2TestProto.TestMap parsed = + MapForProto2TestProto.TestMap.parseFrom(message.toByteArray()); + assertEquals(0, parsed.getInt32ToEnumFieldMap().size()); + // Make sure unknown enum values are preserved. + TestMap actual = TestMap.parseFrom(parsed.toByteArray()); + assertEquals(1, actual.getInt32ToEnumFieldMap().size()); + assertEquals(4321, actual.getInt32ToEnumFieldValueOrThrow(1)); + } + + public void testProto2UnknownEnumValuesInOneof() throws Exception { + // Proto2 doesn't allow setting unknown enum values so we use proto3 to build a message with + // unknown enum values + UnittestProto3.TestOneof2 message = + UnittestProto3.TestOneof2.newBuilder().setFooEnumValue(1234).build(); + TestOneof2 parsed = TestOneof2.parseFrom(message.toByteArray()); + assertFalse(parsed.hasFooEnum()); + // Make sure unknown enum values are preserved. + UnittestProto3.TestOneof2 actual = UnittestProto3.TestOneof2.parseFrom(parsed.toByteArray()); + assertEquals(1234, actual.getFooEnumValue()); + } + + public void testProto2UnknownEnumValuesInExtension() throws Exception { + ExtensionRegistryLite extensionRegistry = TestUtilLite.getExtensionRegistryLite(); + // Raw bytes for "[.optional_foreign_enum_extension_lite]: 10" + final byte[] rawBytes = new byte[]{-80, 1, 10}; + TestAllExtensionsLite testAllExtensionsLite = + TestAllExtensionsLite.parseFrom(rawBytes, extensionRegistry); + assertEquals(ForeignEnumLite.FOREIGN_LITE_FOO, + testAllExtensionsLite.getExtension(optionalForeignEnumExtensionLite)); + final byte[] resultRawBytes = testAllExtensionsLite.toByteArray(); + assertEquals(rawBytes.length, resultRawBytes.length); + for (int i = 0; i < rawBytes.length; i++) { + assertEquals(rawBytes[i], resultRawBytes[i]); + } + } + + public void testProto3UnknownEnumValuesInOptionalField() throws Exception { + UnittestProto3.TestAllTypes message = + UnittestProto3.TestAllTypes.newBuilder().setOptionalNestedEnumValue(4321).build(); + UnittestProto3.TestAllTypes parsed = + UnittestProto3.TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(4321, parsed.getOptionalNestedEnumValue()); + } + + public void testProto3UnknownEnumValuesInRepeatedField() throws Exception { + UnittestProto3.TestAllTypes message = + UnittestProto3.TestAllTypes.newBuilder().addRepeatedNestedEnumValue(5432).build(); + UnittestProto3.TestAllTypes parsed = + UnittestProto3.TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(1, parsed.getRepeatedNestedEnumCount()); + assertEquals(5432, parsed.getRepeatedNestedEnumValue(0)); + } + + public void testProto3UnknownEnumValuesInMapField() throws Exception { + TestMap message = TestMap.newBuilder().putInt32ToEnumFieldValue(1, 4321).build(); + TestMap parsed = TestMap.parseFrom(message.toByteArray()); + assertEquals(1, parsed.getInt32ToEnumFieldMap().size()); + assertEquals(4321, parsed.getInt32ToEnumFieldValueOrThrow(1)); + } + + public void testProto3UnknownEnumValuesInOneof() throws Exception { + UnittestProto3.TestOneof2 message = + UnittestProto3.TestOneof2.newBuilder().setFooEnumValue(1234).build(); + UnittestProto3.TestOneof2 parsed = UnittestProto3.TestOneof2.parseFrom(message.toByteArray()); + assertEquals(1234, parsed.getFooEnumValue()); + } + + public void testProto3MessageFieldMergeBehavior() throws Exception { + UnittestProto3.NestedTestAllTypes message1 = + UnittestProto3.NestedTestAllTypes.newBuilder() + .setPayload( + UnittestProto3.TestAllTypes.newBuilder() + .setOptionalInt32(1234) + .setOptionalInt64(5678)) + .build(); + UnittestProto3.NestedTestAllTypes message2 = + UnittestProto3.NestedTestAllTypes.newBuilder() + .setPayload( + UnittestProto3.TestAllTypes.newBuilder() + .setOptionalInt32(4321) + .setOptionalUint32(8765)) + .build(); + + UnittestProto3.NestedTestAllTypes merged = + UnittestProto3.NestedTestAllTypes.newBuilder() + .mergeFrom(message1.toByteArray()) + .mergeFrom(message2.toByteArray()) + .build(); + // Field values coming later in the stream override earlier values. + assertEquals(4321, merged.getPayload().getOptionalInt32()); + // Field values present in either message should be present in the merged result. + assertEquals(5678, merged.getPayload().getOptionalInt64()); + assertEquals(8765, merged.getPayload().getOptionalUint32()); + } + + public void testMergeFromPartialByteArray() throws Exception { + byte[] data = TestUtil.getAllSet().toByteArray(); + byte[] dataWithPaddings = new byte[data.length + 2]; + System.arraycopy(data, 0, dataWithPaddings, 1, data.length); + // Parsing will fail if the builder (or parser) interprets offset or length incorrectly. + TestAllTypes.newBuilder().mergeFrom(dataWithPaddings, 1, data.length); + TestAllTypes.parser().parseFrom(dataWithPaddings, 1, data.length); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/WireFormatTest.java b/java/core/src/test/java/com/google/protobuf/WireFormatTest.java index 425b56da16..45a396acd8 100644 --- a/java/core/src/test/java/com/google/protobuf/WireFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/WireFormatTest.java @@ -54,6 +54,13 @@ import junit.framework.TestCase; * @author kenton@google.com (Kenton Varda) */ public class WireFormatTest extends TestCase { + + private static final int TYPE_ID_1 = + TestMessageSetExtension1.getDescriptor().getExtensions().get(0).getNumber(); + private static final int TYPE_ID_2 = + TestMessageSetExtension2.getDescriptor().getExtensions().get(0).getNumber(); + private static final int UNKNOWN_TYPE_ID = 1550055; + public void testSerialization() throws Exception { TestAllTypes message = TestUtil.getAllSet(); @@ -102,8 +109,7 @@ public class WireFormatTest extends TestCase { assertEquals(rawBytes, rawBytes2); } - public void testSerializationPackedWithoutGetSerializedSize() - throws Exception { + public void testSerializationPackedWithoutGetSerializedSize() throws Exception { // Write directly to an OutputStream, without invoking getSerializedSize() // This used to be a bug where the size of a packed field was incorrect, // since getSerializedSize() was never invoked. @@ -118,8 +124,7 @@ public class WireFormatTest extends TestCase { codedOutput.flush(); - TestPackedTypes message2 = TestPackedTypes.parseFrom( - outputStream.toByteArray()); + TestPackedTypes message2 = TestPackedTypes.parseFrom(outputStream.toByteArray()); TestUtil.assertPackedFieldsSet(message2); } @@ -134,8 +139,7 @@ public class WireFormatTest extends TestCase { ExtensionRegistryLite registry = TestUtil.getExtensionRegistry(); - TestAllExtensions message2 = - TestAllExtensions.parseFrom(rawBytes, registry); + TestAllExtensions message2 = TestAllExtensions.parseFrom(rawBytes, registry); TestUtil.assertAllExtensionsSet(message2); } @@ -147,8 +151,7 @@ public class WireFormatTest extends TestCase { ExtensionRegistryLite registry = TestUtil.getExtensionRegistry(); - TestPackedExtensions message2 = - TestPackedExtensions.parseFrom(rawBytes, registry); + TestPackedExtensions message2 = TestPackedExtensions.parseFrom(rawBytes, registry); TestUtil.assertPackedExtensionsSet(message2); } @@ -192,25 +195,27 @@ public class WireFormatTest extends TestCase { // Tests that fields are written in order even when extension ranges // are interleaved with field numbers. ByteString data = - TestFieldOrderings.newBuilder() - .setMyInt(1) - .setMyString("foo") - .setMyFloat(1.0F) - .setExtension(UnittestProto.myExtensionInt, 23) - .setExtension(UnittestProto.myExtensionString, "bar") - .build().toByteString(); + TestFieldOrderings.newBuilder() + .setMyInt(1) + .setMyString("foo") + .setMyFloat(1.0F) + .setExtension(UnittestProto.myExtensionInt, 23) + .setExtension(UnittestProto.myExtensionString, "bar") + .build() + .toByteString(); assertFieldsInOrder(data); Descriptors.Descriptor descriptor = TestFieldOrderings.getDescriptor(); - ByteString dynamic_data = - DynamicMessage.newBuilder(TestFieldOrderings.getDescriptor()) - .setField(descriptor.findFieldByName("my_int"), 1L) - .setField(descriptor.findFieldByName("my_string"), "foo") - .setField(descriptor.findFieldByName("my_float"), 1.0F) - .setField(UnittestProto.myExtensionInt.getDescriptor(), 23) - .setField(UnittestProto.myExtensionString.getDescriptor(), "bar") - .build().toByteString(); - assertFieldsInOrder(dynamic_data); + ByteString dynamicData = + DynamicMessage.newBuilder(TestFieldOrderings.getDescriptor()) + .setField(descriptor.findFieldByName("my_int"), 1L) + .setField(descriptor.findFieldByName("my_string"), "foo") + .setField(descriptor.findFieldByName("my_float"), 1.0F) + .setField(UnittestProto.myExtensionInt.getDescriptor(), 23) + .setField(UnittestProto.myExtensionString.getDescriptor(), "bar") + .build() + .toByteString(); + assertFieldsInOrder(dynamicData); } private ExtensionRegistry getTestFieldOrderingsRegistry() { @@ -224,36 +229,35 @@ public class WireFormatTest extends TestCase { // Make sure we can parse a message that contains multiple extensions // ranges. TestFieldOrderings source = - TestFieldOrderings.newBuilder() - .setMyInt(1) - .setMyString("foo") - .setMyFloat(1.0F) - .setExtension(UnittestProto.myExtensionInt, 23) - .setExtension(UnittestProto.myExtensionString, "bar") - .build(); + TestFieldOrderings.newBuilder() + .setMyInt(1) + .setMyString("foo") + .setMyFloat(1.0F) + .setExtension(UnittestProto.myExtensionInt, 23) + .setExtension(UnittestProto.myExtensionString, "bar") + .build(); TestFieldOrderings dest = - TestFieldOrderings.parseFrom(source.toByteString(), - getTestFieldOrderingsRegistry()); + TestFieldOrderings.parseFrom(source.toByteString(), getTestFieldOrderingsRegistry()); assertEquals(source, dest); } - + private static ExtensionRegistry getTestExtensionInsideTableRegistry() { ExtensionRegistry result = ExtensionRegistry.newInstance(); result.add(UnittestProto.testExtensionInsideTableExtension); return result; } - + public void testExtensionInsideTable() throws Exception { // Make sure the extension within the range of table is parsed correctly in experimental // runtime. TestExtensionInsideTable source = TestExtensionInsideTable.newBuilder() - .setField1(1) - .setExtension(UnittestProto.testExtensionInsideTableExtension, 23) - .build(); + .setField1(1) + .setExtension(UnittestProto.testExtensionInsideTableExtension, 23) + .build(); TestExtensionInsideTable dest = - TestExtensionInsideTable.parseFrom(source.toByteString(), - getTestExtensionInsideTableRegistry()); + TestExtensionInsideTable.parseFrom( + source.toByteString(), getTestExtensionInsideTableRegistry()); assertEquals(source, dest); } @@ -261,25 +265,19 @@ public class WireFormatTest extends TestCase { // Same as above except with DynamicMessage. Descriptors.Descriptor descriptor = TestFieldOrderings.getDescriptor(); DynamicMessage source = - DynamicMessage.newBuilder(TestFieldOrderings.getDescriptor()) - .setField(descriptor.findFieldByName("my_int"), 1L) - .setField(descriptor.findFieldByName("my_string"), "foo") - .setField(descriptor.findFieldByName("my_float"), 1.0F) - .setField(UnittestProto.myExtensionInt.getDescriptor(), 23) - .setField(UnittestProto.myExtensionString.getDescriptor(), "bar") - .build(); + DynamicMessage.newBuilder(TestFieldOrderings.getDescriptor()) + .setField(descriptor.findFieldByName("my_int"), 1L) + .setField(descriptor.findFieldByName("my_string"), "foo") + .setField(descriptor.findFieldByName("my_float"), 1.0F) + .setField(UnittestProto.myExtensionInt.getDescriptor(), 23) + .setField(UnittestProto.myExtensionString.getDescriptor(), "bar") + .build(); DynamicMessage dest = - DynamicMessage.parseFrom(descriptor, source.toByteString(), - getTestFieldOrderingsRegistry()); + DynamicMessage.parseFrom( + descriptor, source.toByteString(), getTestFieldOrderingsRegistry()); assertEquals(source, dest); } - private static final int UNKNOWN_TYPE_ID = 1550055; - private static final int TYPE_ID_1 = - TestMessageSetExtension1.getDescriptor().getExtensions().get(0).getNumber(); - private static final int TYPE_ID_2 = - TestMessageSetExtension2.getDescriptor().getExtensions().get(0).getNumber(); - public void testSerializeMessageSetEagerly() throws Exception { testSerializeMessageSetWithFlag(true); } @@ -288,26 +286,26 @@ public class WireFormatTest extends TestCase { testSerializeMessageSetWithFlag(false); } - private void testSerializeMessageSetWithFlag(boolean eagerParsing) - throws Exception { + private void testSerializeMessageSetWithFlag(boolean eagerParsing) throws Exception { ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); // Set up a TestMessageSet with two known messages and an unknown one. TestMessageSet messageSet = - TestMessageSet.newBuilder() - .setExtension( - TestMessageSetExtension1.messageSetExtension, - TestMessageSetExtension1.newBuilder().setI(123).build()) - .setExtension( - TestMessageSetExtension2.messageSetExtension, - TestMessageSetExtension2.newBuilder().setStr("foo").build()) - .setUnknownFields( - UnknownFieldSet.newBuilder() - .addField(UNKNOWN_TYPE_ID, - UnknownFieldSet.Field.newBuilder() - .addLengthDelimited(ByteString.copyFromUtf8("bar")) - .build()) - .build()) - .build(); + TestMessageSet.newBuilder() + .setExtension( + TestMessageSetExtension1.messageSetExtension, + TestMessageSetExtension1.newBuilder().setI(123).build()) + .setExtension( + TestMessageSetExtension2.messageSetExtension, + TestMessageSetExtension2.newBuilder().setStr("foo").build()) + .setUnknownFields( + UnknownFieldSet.newBuilder() + .addField( + UNKNOWN_TYPE_ID, + UnknownFieldSet.Field.newBuilder() + .addLengthDelimited(ByteString.copyFromUtf8("bar")) + .build()) + .build()) + .build(); ByteString data = messageSet.toByteString(); @@ -322,13 +320,11 @@ public class WireFormatTest extends TestCase { assertEquals(UNKNOWN_TYPE_ID, raw.getItem(2).getTypeId()); TestMessageSetExtension1 message1 = - TestMessageSetExtension1.parseFrom( - raw.getItem(0).getMessage().toByteArray()); + TestMessageSetExtension1.parseFrom(raw.getItem(0).getMessage()); assertEquals(123, message1.getI()); TestMessageSetExtension2 message2 = - TestMessageSetExtension2.parseFrom( - raw.getItem(1).getMessage().toByteArray()); + TestMessageSetExtension2.parseFrom(raw.getItem(1).getMessage()); assertEquals("foo", message2.getStr()); assertEquals("bar", raw.getItem(2).getMessage().toStringUtf8()); @@ -338,12 +334,11 @@ public class WireFormatTest extends TestCase { testParseMessageSetWithFlag(true); } - public void testParseMessageSetNotEagerly()throws Exception { + public void testParseMessageSetNotEagerly() throws Exception { testParseMessageSetWithFlag(false); } - private void testParseMessageSetWithFlag(boolean eagerParsing) - throws Exception { + private void testParseMessageSetWithFlag(boolean eagerParsing) throws Exception { ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); @@ -351,40 +346,34 @@ public class WireFormatTest extends TestCase { // Set up a RawMessageSet with two known messages and an unknown one. RawMessageSet raw = - RawMessageSet.newBuilder() - .addItem( - RawMessageSet.Item.newBuilder() - .setTypeId(TYPE_ID_1) - .setMessage( - TestMessageSetExtension1.newBuilder() - .setI(123) - .build().toByteString()) - .build()) - .addItem( - RawMessageSet.Item.newBuilder() - .setTypeId(TYPE_ID_2) - .setMessage( - TestMessageSetExtension2.newBuilder() - .setStr("foo") - .build().toByteString()) - .build()) - .addItem( - RawMessageSet.Item.newBuilder() - .setTypeId(UNKNOWN_TYPE_ID) - .setMessage(ByteString.copyFromUtf8("bar")) - .build()) - .build(); + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_2) + .setMessage( + TestMessageSetExtension2.newBuilder().setStr("foo").build().toByteString()) + .build()) + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(UNKNOWN_TYPE_ID) + .setMessage(ByteString.copyFromUtf8("bar")) + .build()) + .build(); ByteString data = raw.toByteString(); // Parse as a TestMessageSet and check the contents. - TestMessageSet messageSet = - TestMessageSet.parseFrom(data, extensionRegistry); + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); - assertEquals(123, messageSet.getExtension( - TestMessageSetExtension1.messageSetExtension).getI()); - assertEquals("foo", messageSet.getExtension( - TestMessageSetExtension2.messageSetExtension).getStr()); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + assertEquals( + "foo", messageSet.getExtension(TestMessageSetExtension2.messageSetExtension).getStr()); // Check for unknown field with type LENGTH_DELIMITED, // number UNKNOWN_TYPE_ID, and contents "bar". @@ -405,35 +394,27 @@ public class WireFormatTest extends TestCase { testParseMessageSetExtensionWithFlag(false); } - private void testParseMessageSetExtensionWithFlag(boolean eagerParsing) - throws Exception { + private void testParseMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); // Set up a RawMessageSet with a known messages. - int TYPE_ID_1 = - TestMessageSetExtension1 - .getDescriptor().getExtensions().get(0).getNumber(); RawMessageSet raw = - RawMessageSet.newBuilder() - .addItem( - RawMessageSet.Item.newBuilder() - .setTypeId(TYPE_ID_1) - .setMessage( - TestMessageSetExtension1.newBuilder() - .setI(123) - .build().toByteString()) - .build()) - .build(); + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); ByteString data = raw.toByteString(); // Parse as a TestMessageSet and check the contents. - TestMessageSet messageSet = - TestMessageSet.parseFrom(data, extensionRegistry); - assertEquals(123, messageSet.getExtension( - TestMessageSetExtension1.messageSetExtension).getI()); + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); } public void testMergeLazyMessageSetExtensionEagerly() throws Exception { @@ -444,38 +425,29 @@ public class WireFormatTest extends TestCase { testMergeLazyMessageSetExtensionWithFlag(false); } - private void testMergeLazyMessageSetExtensionWithFlag(boolean eagerParsing) - throws Exception { + private void testMergeLazyMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); // Set up a RawMessageSet with a known messages. - int TYPE_ID_1 = - TestMessageSetExtension1 - .getDescriptor().getExtensions().get(0).getNumber(); RawMessageSet raw = - RawMessageSet.newBuilder() - .addItem( - RawMessageSet.Item.newBuilder() - .setTypeId(TYPE_ID_1) - .setMessage( - TestMessageSetExtension1.newBuilder() - .setI(123) - .build().toByteString()) - .build()) - .build(); + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); ByteString data = raw.toByteString(); // Parse as a TestMessageSet and store value into lazy field - TestMessageSet messageSet = - TestMessageSet.parseFrom(data, extensionRegistry); + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); // Merge lazy field check the contents. - messageSet = - messageSet.toBuilder().mergeFrom(data, extensionRegistry).build(); - assertEquals(123, messageSet.getExtension( - TestMessageSetExtension1.messageSetExtension).getI()); + messageSet = messageSet.toBuilder().mergeFrom(data, extensionRegistry).build(); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); } public void testMergeMessageSetExtensionEagerly() throws Exception { @@ -486,31 +458,24 @@ public class WireFormatTest extends TestCase { testMergeMessageSetExtensionWithFlag(false); } - private void testMergeMessageSetExtensionWithFlag(boolean eagerParsing) - throws Exception { + private void testMergeMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); // Set up a RawMessageSet with a known messages. - int TYPE_ID_1 = - TestMessageSetExtension1 - .getDescriptor().getExtensions().get(0).getNumber(); RawMessageSet raw = - RawMessageSet.newBuilder() - .addItem( - RawMessageSet.Item.newBuilder() - .setTypeId(TYPE_ID_1) - .setMessage( - TestMessageSetExtension1.newBuilder() - .setI(123) - .build().toByteString()) - .build()) - .build(); + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); // Serialize RawMessageSet unnormally (message value before type id) - ByteString.CodedBuilder out = ByteString.newCodedBuilder( - raw.getSerializedSize()); + ByteString.CodedBuilder out = ByteString.newCodedBuilder(raw.getSerializedSize()); CodedOutputStream output = out.getCodedOutput(); List items = raw.getItemList(); for (int i = 0; i < items.size(); i++) { @@ -525,8 +490,7 @@ public class WireFormatTest extends TestCase { // Merge bytes into TestMessageSet and check the contents. TestMessageSet messageSet = TestMessageSet.newBuilder().mergeFrom(data, extensionRegistry).build(); - assertEquals(123, messageSet.getExtension( - TestMessageSetExtension1.messageSetExtension).getI()); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); } // ================================================================ @@ -544,8 +508,8 @@ public class WireFormatTest extends TestCase { } public void testOneofOnlyLastSet() throws Exception { - TestOneofBackwardsCompatible source = TestOneofBackwardsCompatible - .newBuilder().setFooInt(100).setFooString("101").build(); + TestOneofBackwardsCompatible source = + TestOneofBackwardsCompatible.newBuilder().setFooInt(100).setFooString("101").build(); ByteString rawBytes = source.toByteString(); TestOneof2 message = TestOneof2.parseFrom(rawBytes); diff --git a/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java b/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java new file mode 100755 index 0000000000..4a3c7647f1 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java @@ -0,0 +1,61 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage; +import junit.framework.TestCase; + +public class WrappersLiteOfMethodTest extends TestCase { + + public void testOf() throws Exception { + TopLevelMessage.Builder builder = TopLevelMessage.newBuilder(); + builder.setFieldDouble(DoubleValue.of(2.333)); + builder.setFieldFloat(FloatValue.of(2.333f)); + builder.setFieldInt32(Int32Value.of(2333)); + builder.setFieldInt64(Int64Value.of(23333333333333L)); + builder.setFieldUint32(UInt32Value.of(2333)); + builder.setFieldUint64(UInt64Value.of(23333333333333L)); + builder.setFieldBool(BoolValue.of(true)); + builder.setFieldString(StringValue.of("23333")); + builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(Internal.UTF_8)))); + + TopLevelMessage message = builder.build(); + assertTrue(2.333 == message.getFieldDouble().getValue()); + assertTrue(2.333f == message.getFieldFloat().getValue()); + assertTrue(2333 == message.getFieldInt32().getValue()); + assertTrue(23333333333333L == message.getFieldInt64().getValue()); + assertTrue(2333 == message.getFieldUint32().getValue()); + assertTrue(23333333333333L == message.getFieldUint64().getValue()); + assertTrue(true == message.getFieldBool().getValue()); + assertTrue(message.getFieldString().getValue().equals("23333")); + assertTrue(message.getFieldBytes().getValue().toStringUtf8().equals("233")); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java b/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java new file mode 100755 index 0000000000..f0d662d0c6 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java @@ -0,0 +1,61 @@ +// 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. + +package com.google.protobuf; + +import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage; +import junit.framework.TestCase; + +public class WrappersOfMethodTest extends TestCase { + + public void testOf() throws Exception { + TopLevelMessage.Builder builder = TopLevelMessage.newBuilder(); + builder.setFieldDouble(DoubleValue.of(2.333)); + builder.setFieldFloat(FloatValue.of(2.333f)); + builder.setFieldInt32(Int32Value.of(2333)); + builder.setFieldInt64(Int64Value.of(23333333333333L)); + builder.setFieldUint32(UInt32Value.of(2333)); + builder.setFieldUint64(UInt64Value.of(23333333333333L)); + builder.setFieldBool(BoolValue.of(true)); + builder.setFieldString(StringValue.of("23333")); + builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(Internal.UTF_8)))); + + TopLevelMessage message = builder.build(); + assertTrue(2.333 == message.getFieldDouble().getValue()); + assertTrue(2.333f == message.getFieldFloat().getValue()); + assertTrue(2333 == message.getFieldInt32().getValue()); + assertTrue(23333333333333L == message.getFieldInt64().getValue()); + assertTrue(2333 == message.getFieldUint32().getValue()); + assertTrue(23333333333333L == message.getFieldUint64().getValue()); + assertTrue(true == message.getFieldBool().getValue()); + assertTrue(message.getFieldString().getValue().equals("23333")); + assertTrue(message.getFieldBytes().getValue().toStringUtf8().equals("233")); + } +} diff --git a/java/core/src/test/proto/com/google/protobuf/any_test.proto b/java/core/src/test/proto/com/google/protobuf/any_test.proto index 80173d8a0e..99d52b75c8 100644 --- a/java/core/src/test/proto/com/google/protobuf/any_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/any_test.proto @@ -32,11 +32,11 @@ syntax = "proto3"; package any_test; +import "google/protobuf/any.proto"; + option java_package = "any_test"; option java_outer_classname = "AnyTestProto"; -import "google/protobuf/any.proto"; - message TestAny { google.protobuf.Any value = 1; } diff --git a/java/core/src/test/proto/com/google/protobuf/cached_field_size_test.proto b/java/core/src/test/proto/com/google/protobuf/cached_field_size_test.proto new file mode 100755 index 0000000000..9579a0a505 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/cached_field_size_test.proto @@ -0,0 +1,45 @@ +// 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 regression test for b/74087933 +syntax = "proto2"; + +package protobuf_unittest; + +import "google/protobuf/unittest.proto"; +import "google/protobuf/unittest_proto3.proto"; + +option optimize_for = CODE_SIZE; +option java_multiple_files = true; + +message TestCachedFieldSizeMessage { + optional protobuf_unittest.TestPackedTypes proto2_child = 1; + optional proto3_unittest.TestPackedTypes proto3_child = 2; +} diff --git a/java/core/src/test/proto/com/google/protobuf/field_presence_test.proto b/java/core/src/test/proto/com/google/protobuf/field_presence_test.proto index 2367bd8bd0..28f3658354 100644 --- a/java/core/src/test/proto/com/google/protobuf/field_presence_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/field_presence_test.proto @@ -53,7 +53,7 @@ message TestAllTypes { NestedEnum optional_nested_enum = 4; NestedMessage optional_nested_message = 5; protobuf_unittest.TestRequired optional_proto2_message = 6; - NestedMessage optional_lazy_message = 7 [lazy=true]; + NestedMessage optional_lazy_message = 7 [lazy = true]; oneof oneof_field { int32 oneof_int32 = 11; @@ -81,7 +81,7 @@ message TestOptionalFieldsOnly { TestAllTypes.NestedEnum optional_nested_enum = 4; TestAllTypes.NestedMessage optional_nested_message = 5; protobuf_unittest.TestRequired optional_proto2_message = 6; - TestAllTypes.NestedMessage optional_lazy_message = 7 [lazy=true]; + TestAllTypes.NestedMessage optional_lazy_message = 7 [lazy = true]; } message TestRepeatedFieldsOnly { diff --git a/java/core/src/test/proto/com/google/protobuf/lite_equals_and_hash.proto b/java/core/src/test/proto/com/google/protobuf/lite_equals_and_hash.proto index 20fa03b929..080c5192ba 100644 --- a/java/core/src/test/proto/com/google/protobuf/lite_equals_and_hash.proto +++ b/java/core/src/test/proto/com/google/protobuf/lite_equals_and_hash.proto @@ -69,8 +69,7 @@ message BarPrime { optional string name = 1; } -message Empty { -} +message Empty {} extend Foo { optional int32 varint = 101; diff --git a/java/core/src/test/proto/com/google/protobuf/map_for_proto2_lite_test.proto b/java/core/src/test/proto/com/google/protobuf/map_for_proto2_lite_test.proto index 2ca0251ca2..4ec968819b 100644 --- a/java/core/src/test/proto/com/google/protobuf/map_for_proto2_lite_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/map_for_proto2_lite_test.proto @@ -44,12 +44,12 @@ message TestMap { QUX = 3; } - map int32_to_int32_field = 1; - map int32_to_string_field = 2; - map int32_to_bytes_field = 3; - map int32_to_enum_field = 4; - map int32_to_message_field = 5; - map string_to_int32_field = 6; + map int32_to_int32_field = 1; + map int32_to_string_field = 2; + map int32_to_bytes_field = 3; + map int32_to_enum_field = 4; + map int32_to_message_field = 5; + map string_to_int32_field = 6; message MessageWithRequiredFields { required int32 value = 1; @@ -70,15 +70,20 @@ message TestRecursiveMap { map recursive_map_field = 2; } - // a decoy of TestMap for testing parsing errors message BizarroTestMap { - map int32_to_int32_field = 1; // same key type, different value - map int32_to_string_field = 2; // different key and value types - map int32_to_bytes_field = 3; // different key types, same value - map int32_to_enum_field = 4; // different key and value types - map int32_to_message_field = 5; // different key and value types - map string_to_int32_field = 6; // same key type, different value + // same key type, different value + map int32_to_int32_field = 1; + // different key and value types + map int32_to_string_field = 2; + // different key types, same value + map int32_to_bytes_field = 3; + // different key and value types + map int32_to_enum_field = 4; + // different key and value types + map int32_to_message_field = 5; + // same key type, different value + map string_to_int32_field = 6; } // Used to test that java reserved words can be used as protobuf field names @@ -92,11 +97,13 @@ message ReservedAsMapField { map class = 4; map int = 5; map void = 6; - map string = 7; // These are also proto keywords + // These are also proto keywords + map string = 7; map package = 8; - map enum = 9; // Most recent Java reserved word - map null = 10; + // Most recent Java reserved word + map enum = 9; // null is not a 'reserved word' per se but as a literal needs similar care + map null = 10; } message ReservedAsMapFieldWithEnumValue { @@ -110,11 +117,13 @@ message ReservedAsMapFieldWithEnumValue { map class = 4; map int = 5; map void = 6; - map string = 7; // These are also proto keywords + // These are also proto keywords + map string = 7; map package = 8; - map enum = 9; // Most recent Java reserved word - map null = 10; + // Most recent Java reserved word + map enum = 9; // null is not a 'reserved word' per se but as a literal needs similar care + map null = 10; } package map_for_proto2_lite_test; option java_package = "map_lite_test"; diff --git a/java/core/src/test/proto/com/google/protobuf/map_for_proto2_test.proto b/java/core/src/test/proto/com/google/protobuf/map_for_proto2_test.proto index 974f8a2c74..f73bd6375b 100644 --- a/java/core/src/test/proto/com/google/protobuf/map_for_proto2_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/map_for_proto2_test.proto @@ -46,12 +46,12 @@ message TestMap { QUX = 3; } - map int32_to_int32_field = 1; - map int32_to_string_field = 2; - map int32_to_bytes_field = 3; - map int32_to_enum_field = 4; - map int32_to_message_field = 5; - map string_to_int32_field = 6; + map int32_to_int32_field = 1; + map int32_to_string_field = 2; + map int32_to_bytes_field = 3; + map int32_to_enum_field = 4; + map int32_to_message_field = 5; + map string_to_int32_field = 6; message MessageWithRequiredFields { required int32 value = 1; @@ -72,15 +72,20 @@ message TestRecursiveMap { map recursive_map_field = 2; } - // a decoy of TestMap for testing parsing errors message BizarroTestMap { - map int32_to_int32_field = 1; // same key type, different value - map int32_to_string_field = 2; // different key and value types - map int32_to_bytes_field = 3; // different key types, same value - map int32_to_enum_field = 4; // different key and value types - map int32_to_message_field = 5; // different key and value types - map string_to_int32_field = 6; // same key type, different value + // same key type, different value + map int32_to_int32_field = 1; + // different key and value types + map int32_to_string_field = 2; + // different key types, same value + map int32_to_bytes_field = 3; + // different key and value types + map int32_to_enum_field = 4; + // different key and value types + map int32_to_message_field = 5; + // same key type, different value + map string_to_int32_field = 6; } // Used to test that java reserved words can be used as protobuf field names @@ -94,11 +99,13 @@ message ReservedAsMapField { map class = 4; map int = 5; map void = 6; - map string = 7; // These are also proto keywords + // These are also proto keywords + map string = 7; map package = 8; - map enum = 9; // Most recent Java reserved word - map null = 10; + // Most recent Java reserved word + map enum = 9; // null is not a 'reserved word' per se but as a literal needs similar care + map null = 10; } message ReservedAsMapFieldWithEnumValue { @@ -112,9 +119,11 @@ message ReservedAsMapFieldWithEnumValue { map class = 4; map int = 5; map void = 6; - map string = 7; // These are also proto keywords + // These are also proto keywords + map string = 7; map package = 8; - map enum = 9; // Most recent Java reserved word - map null = 10; + // Most recent Java reserved word + map enum = 9; // null is not a 'reserved word' per se but as a literal needs similar care + map null = 10; } diff --git a/java/core/src/test/proto/com/google/protobuf/map_initialization_order_test.proto b/java/core/src/test/proto/com/google/protobuf/map_initialization_order_test.proto index b02ac599a5..6ebef5eba3 100644 --- a/java/core/src/test/proto/com/google/protobuf/map_initialization_order_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/map_initialization_order_test.proto @@ -57,5 +57,4 @@ extend Message1 { optional Message1 recursive_extension = 1001; } -message RedactAllTypes { -} +message RedactAllTypes {} diff --git a/java/core/src/test/proto/com/google/protobuf/map_lite_test.proto b/java/core/src/test/proto/com/google/protobuf/map_lite_test.proto index c04f5d57a7..7324653f81 100644 --- a/java/core/src/test/proto/com/google/protobuf/map_lite_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/map_lite_test.proto @@ -47,14 +47,14 @@ message TestMap { QUX = 3; } - map int32_to_int32_field = 1; - map int32_to_string_field = 2; - map int32_to_bytes_field = 3; - map int32_to_enum_field = 4; + map int32_to_int32_field = 1; + map int32_to_string_field = 2; + map int32_to_bytes_field = 3; + map int32_to_enum_field = 4; map int32_to_message_field = 5; - map string_to_int32_field = 6; - map uint32_to_int32_field = 7; - map int64_to_int32_field = 8; + map string_to_int32_field = 6; + map uint32_to_int32_field = 7; + map int64_to_int32_field = 8; } // Used to test that a nested builder containing map fields will properly @@ -66,12 +66,18 @@ message TestOnChangeEventPropagation { // a decoy of TestMap for testing parsing errors message BizarroTestMap { - map int32_to_int32_field = 1; // same key type, different value - map int32_to_string_field = 2; // different key and value types - map int32_to_bytes_field = 3; // different key types, same value - map int32_to_enum_field = 4; // different key and value types - map int32_to_message_field = 5; // different key and value types - map string_to_int32_field = 6; // same key type, different value + // same key type, different value + map int32_to_int32_field = 1; + // different key and value types + map int32_to_string_field = 2; + // different key types, same value + map int32_to_bytes_field = 3; + // different key and value types + map int32_to_enum_field = 4; + // different key and value types + map int32_to_message_field = 5; + // same key type, different value + map string_to_int32_field = 6; } // Used to test that java reserved words can be used as protobuf field names @@ -85,11 +91,13 @@ message ReservedAsMapField { map class = 4; map int = 5; map void = 6; - map string = 7; // These are also proto keywords + // These are also proto keywords + map string = 7; map package = 8; - map enum = 9; // Most recent Java reserved word - map null = 10; + // Most recent Java reserved word + map enum = 9; // null is not a 'reserved word' per se but as a literal needs similar care + map null = 10; } message ReservedAsMapFieldWithEnumValue { @@ -103,9 +111,11 @@ message ReservedAsMapFieldWithEnumValue { map class = 4; map int = 5; map void = 6; - map string = 7; // These are also proto keywords + // These are also proto keywords + map string = 7; map package = 8; - map enum = 9; // Most recent Java reserved word - map null = 10; + // Most recent Java reserved word + map enum = 9; // null is not a 'reserved word' per se but as a literal needs similar care + map null = 10; } diff --git a/java/core/src/test/proto/com/google/protobuf/map_test.proto b/java/core/src/test/proto/com/google/protobuf/map_test.proto index bc2105e50f..240600f2de 100644 --- a/java/core/src/test/proto/com/google/protobuf/map_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/map_test.proto @@ -46,14 +46,14 @@ message TestMap { QUX = 3; } - map int32_to_int32_field = 1; - map int32_to_string_field = 2; - map int32_to_bytes_field = 3; - map int32_to_enum_field = 4; + map int32_to_int32_field = 1; + map int32_to_string_field = 2; + map int32_to_bytes_field = 3; + map int32_to_enum_field = 4; map int32_to_message_field = 5; - map string_to_int32_field = 6; - map uint32_to_int32_field = 7; - map int64_to_int32_field = 8; + map string_to_int32_field = 6; + map uint32_to_int32_field = 7; + map int64_to_int32_field = 8; } // Used to test that a nested builder containing map fields will properly @@ -65,12 +65,18 @@ message TestOnChangeEventPropagation { // a decoy of TestMap for testing parsing errors message BizarroTestMap { - map int32_to_int32_field = 1; // same key type, different value - map int32_to_string_field = 2; // different key and value types - map int32_to_bytes_field = 3; // different key types, same value - map int32_to_enum_field = 4; // different key and value types - map int32_to_message_field = 5; // different key and value types - map string_to_int32_field = 6; // same key type, different value + // same key type, different value + map int32_to_int32_field = 1; + // different key and value types + map int32_to_string_field = 2; + // different key types, same value + map int32_to_bytes_field = 3; + // different key and value types + map int32_to_enum_field = 4; + // different key and value types + map int32_to_message_field = 5; + // same key type, different value + map string_to_int32_field = 6; } // Used to test that java reserved words can be used as protobuf field names @@ -84,11 +90,13 @@ message ReservedAsMapField { map class = 4; map int = 5; map void = 6; - map string = 7; // These are also proto keywords + // These are also proto keywords + map string = 7; map package = 8; - map enum = 9; // Most recent Java reserved word - map null = 10; + // Most recent Java reserved word + map enum = 9; // null is not a 'reserved word' per se but as a literal needs similar care + map null = 10; } message ReservedAsMapFieldWithEnumValue { @@ -102,9 +110,11 @@ message ReservedAsMapFieldWithEnumValue { map class = 4; map int = 5; map void = 6; - map string = 7; // These are also proto keywords + // These are also proto keywords + map string = 7; map package = 8; - map enum = 9; // Most recent Java reserved word - map null = 10; + // Most recent Java reserved word + map enum = 9; // null is not a 'reserved word' per se but as a literal needs similar care + map null = 10; } diff --git a/java/core/src/test/proto/com/google/protobuf/message_lite_extension_util_test.proto b/java/core/src/test/proto/com/google/protobuf/message_lite_extension_util_test.proto new file mode 100755 index 0000000000..9baf948d2e --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/message_lite_extension_util_test.proto @@ -0,0 +1,48 @@ +// 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. + +// Proto definitions used to test MessageLiteExtensionUtil +syntax = "proto2"; + +package protobuf_unittest; + +option java_outer_classname = "MessageLiteExtensionTestProtos"; + +message Car { + optional string make = 1; + extensions 1000 to max; +} + +extend Car { + optional bool turbo = 1001; + optional bool self_driving = 1002; + optional bool flies = 1003; + optional string plate = 9999; +} diff --git a/java/core/src/test/proto/com/google/protobuf/multiple_files_test.proto b/java/core/src/test/proto/com/google/protobuf/multiple_files_test.proto index 92790506f0..0de6f7b868 100644 --- a/java/core/src/test/proto/com/google/protobuf/multiple_files_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/multiple_files_test.proto @@ -36,24 +36,20 @@ syntax = "proto2"; // Some generic_services option(s) added automatically. // See: http://go/proto2-generic-services-default -option java_generic_services = true; // auto-added +package protobuf_unittest; import "google/protobuf/unittest.proto"; import "google/protobuf/descriptor.proto"; -package protobuf_unittest; - +option java_generic_services = true; // auto-added option java_multiple_files = true; option java_outer_classname = "MultipleFilesTestProto"; - message MessageWithNoOuter { message NestedMessage { optional int32 i = 1; } - enum NestedEnum { - BAZ = 3; - } + enum NestedEnum { BAZ = 3; } optional NestedMessage nested = 1; repeated TestAllTypes foreign = 2; optional NestedEnum nested_enum = 3; @@ -70,7 +66,7 @@ enum EnumWithNoOuter { } service ServiceWithNoOuter { - rpc Foo(MessageWithNoOuter) returns(TestAllTypes); + rpc Foo(MessageWithNoOuter) returns (TestAllTypes); } extend TestAllExtensions { diff --git a/java/core/src/test/proto/com/google/protobuf/nested_builders_test.proto b/java/core/src/test/proto/com/google/protobuf/nested_builders_test.proto index a5dd66d889..07933ed449 100644 --- a/java/core/src/test/proto/com/google/protobuf/nested_builders_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/nested_builders_test.proto @@ -37,7 +37,6 @@ package protobuf_unittest; option java_multiple_files = true; option java_outer_classname = "NestedBuilders"; - message Vehicle { optional Engine engine = 1; repeated Wheel wheel = 2; diff --git a/java/core/src/test/proto/com/google/protobuf/nested_extension.proto b/java/core/src/test/proto/com/google/protobuf/nested_extension.proto index 704e649e02..2da6d08e3a 100644 --- a/java/core/src/test/proto/com/google/protobuf/nested_extension.proto +++ b/java/core/src/test/proto/com/google/protobuf/nested_extension.proto @@ -35,13 +35,14 @@ syntax = "proto2"; -import "com/google/protobuf/non_nested_extension.proto"; - package protobuf_unittest; +import "com/google/protobuf/non_nested_extension.proto"; + message MyNestedExtension { extend MessageToBeExtended { optional MessageToBeExtended recursiveExtension = 2; + optional int32 default = 2002; } } diff --git a/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto b/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto index a95c38b234..024097cb5e 100644 --- a/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto +++ b/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto @@ -38,12 +38,13 @@ syntax = "proto2"; package protobuf_unittest; -option optimize_for = LITE_RUNTIME; - import "com/google/protobuf/non_nested_extension_lite.proto"; +option optimize_for = LITE_RUNTIME; + message MyNestedExtensionLite { extend MessageLiteToBeExtended { optional MessageLiteToBeExtended recursiveExtensionLite = 3; + optional double private = 2004; } } diff --git a/java/core/src/test/proto/com/google/protobuf/non_nested_extension.proto b/java/core/src/test/proto/com/google/protobuf/non_nested_extension.proto index 31fac55295..45395a353a 100644 --- a/java/core/src/test/proto/com/google/protobuf/non_nested_extension.proto +++ b/java/core/src/test/proto/com/google/protobuf/non_nested_extension.proto @@ -41,10 +41,9 @@ message MessageToBeExtended { extensions 1 to max; } -message MyNonNestedExtension { -} +message MyNonNestedExtension {} extend MessageToBeExtended { optional MyNonNestedExtension nonNestedExtension = 1; + optional string if = 2000; } - diff --git a/java/core/src/test/proto/com/google/protobuf/non_nested_extension_lite.proto b/java/core/src/test/proto/com/google/protobuf/non_nested_extension_lite.proto index 37c369edaa..782a1e66d4 100644 --- a/java/core/src/test/proto/com/google/protobuf/non_nested_extension_lite.proto +++ b/java/core/src/test/proto/com/google/protobuf/non_nested_extension_lite.proto @@ -42,10 +42,9 @@ message MessageLiteToBeExtended { extensions 1 to max; } -message MyNonNestedExtensionLite { -} +message MyNonNestedExtensionLite {} extend MessageLiteToBeExtended { optional MyNonNestedExtensionLite nonNestedExtensionLite = 1; + optional bool package = 2006; } - diff --git a/java/core/src/test/proto/com/google/protobuf/outer_class_name_test.proto b/java/core/src/test/proto/com/google/protobuf/outer_class_name_test.proto index 4208368146..28b8bb5687 100644 --- a/java/core/src/test/proto/com/google/protobuf/outer_class_name_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/outer_class_name_test.proto @@ -36,5 +36,4 @@ package protobuf_unittest; // This message's name is the same with the default outer class name of this // proto file. It's used to test if the compiler can avoid this conflict // correctly. -message OuterClassNameTest { -} +message OuterClassNameTest {} diff --git a/java/core/src/test/proto/com/google/protobuf/outer_class_name_test2.proto b/java/core/src/test/proto/com/google/protobuf/outer_class_name_test2.proto index 3e5956b0d4..b475510e74 100644 --- a/java/core/src/test/proto/com/google/protobuf/outer_class_name_test2.proto +++ b/java/core/src/test/proto/com/google/protobuf/outer_class_name_test2.proto @@ -38,7 +38,6 @@ message TestMessage2 { // This message's name is the same with the default outer class name of this // proto file. It's used to test if the compiler can avoid this conflict // correctly. - message OuterClassNameTest2 { - } + message OuterClassNameTest2 {} } } diff --git a/java/core/src/test/proto/com/google/protobuf/outer_class_name_test3.proto b/java/core/src/test/proto/com/google/protobuf/outer_class_name_test3.proto index 74a8ba3c3d..e5ef2f0534 100644 --- a/java/core/src/test/proto/com/google/protobuf/outer_class_name_test3.proto +++ b/java/core/src/test/proto/com/google/protobuf/outer_class_name_test3.proto @@ -38,8 +38,6 @@ message TestMessage3 { // This enum's name is the same with the default outer class name of this // proto file. It's used to test if the compiler can avoid this conflict // correctly. - enum OuterClassNameTest3 { - DUMMY_VALUE = 1; - } + enum OuterClassNameTest3 { DUMMY_VALUE = 1; } } } diff --git a/java/core/src/test/proto/com/google/protobuf/packed_field_test.proto b/java/core/src/test/proto/com/google/protobuf/packed_field_test.proto new file mode 100755 index 0000000000..7935322b72 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/packed_field_test.proto @@ -0,0 +1,76 @@ +// 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. + +syntax = "proto3"; + +package packed_field_test; + +option java_package = "com.google.protobuf"; +option java_outer_classname = "PackedFieldTestProto"; + +message TestAllTypes { + enum NestedEnum { + FOO = 0; + BAR = 1; + BAZ = 2; + } + + repeated int32 repeated_int32 = 31; + repeated int64 repeated_int64 = 32; + repeated uint32 repeated_uint32 = 33; + repeated uint64 repeated_uint64 = 34; + repeated sint32 repeated_sint32 = 35; + repeated sint64 repeated_sint64 = 36; + repeated fixed32 repeated_fixed32 = 37; + repeated fixed64 repeated_fixed64 = 38; + repeated sfixed32 repeated_sfixed32 = 39; + repeated sfixed64 repeated_sfixed64 = 40; + repeated float repeated_float = 41; + repeated double repeated_double = 42; + repeated bool repeated_bool = 43; + repeated NestedEnum repeated_nested_enum = 51; +} + +message TestUnpackedTypes { + repeated int32 repeated_int32 = 1 [packed = false]; + repeated int64 repeated_int64 = 2 [packed = false]; + repeated uint32 repeated_uint32 = 3 [packed = false]; + repeated uint64 repeated_uint64 = 4 [packed = false]; + repeated sint32 repeated_sint32 = 5 [packed = false]; + repeated sint64 repeated_sint64 = 6 [packed = false]; + repeated fixed32 repeated_fixed32 = 7 [packed = false]; + repeated fixed64 repeated_fixed64 = 8 [packed = false]; + repeated sfixed32 repeated_sfixed32 = 9 [packed = false]; + repeated sfixed64 repeated_sfixed64 = 10 [packed = false]; + repeated float repeated_float = 11 [packed = false]; + repeated double repeated_double = 12 [packed = false]; + repeated bool repeated_bool = 13 [packed = false]; + repeated TestAllTypes.NestedEnum repeated_nested_enum = 14 [packed = false]; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto2_message.proto b/java/core/src/test/proto/com/google/protobuf/proto2_message.proto new file mode 100755 index 0000000000..86c48b0986 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto2_message.proto @@ -0,0 +1,430 @@ +// 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. + +// LINT: ALLOW_GROUPS +syntax = "proto2"; + +package protobuf.experimental; + +option java_package = "com.google.protobuf.testing"; +option java_outer_classname = "Proto2Testing"; + + +message Proto2SpecialFieldName { + optional double regular_name = 1; + optional int32 cached_size = 2; + optional int64 serialized_size = 3; + optional string class = 4; +} + +message Proto2Message { + + enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; + } + + optional double field_double_1 = 1; + optional float field_float_2 = 2; + optional int64 field_int64_3 = 3; + optional uint64 field_uint64_4 = 4; + optional int32 field_int32_5 = 5; + optional fixed64 field_fixed64_6 = 6; + optional fixed32 field_fixed32_7 = 7; + optional bool field_bool_8 = 8; + optional string field_string_9 = 9; + optional Proto2Message field_message_10 = 10; + optional bytes field_bytes_11 = 11; + optional uint32 field_uint32_12 = 12; + optional TestEnum field_enum_13 = 13; + optional sfixed32 field_sfixed32_14 = 14; + optional sfixed64 field_sfixed64_15 = 15; + optional sint32 field_sint32_16 = 16; + optional sint64 field_sint64_17 = 17; + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto2Message field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated TestEnum field_enum_list_packed_44 = 44 [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + optional group FieldGroup49 = 49 { + optional int32 field_int32_50 = 50; + } + repeated group FieldGroupList51 = 51 { + optional int32 field_int32_52 = 52; + } + oneof test_oneof { + double field_double_53 = 53; + float field_float_54 = 54; + int64 field_int64_55 = 55; + uint64 field_uint64_56 = 56; + int32 field_int32_57 = 57; + fixed64 field_fixed64_58 = 58; + fixed32 field_fixed32_59 = 59; + bool field_bool_60 = 60; + string field_string_61 = 61; + Proto2Message field_message_62 = 62; + bytes field_bytes_63 = 63; + uint32 field_uint32_64 = 64; + sfixed32 field_sfixed32_65 = 65; + sfixed64 field_sfixed64_66 = 66; + sint32 field_sint32_67 = 67; + sint64 field_sint64_68 = 68; + group FieldGroup69 = 69 { + optional int32 field_int32_70 = 70; + } + } + + message RequiredNestedMessage { + optional int32 value = 1; + } + + required double field_required_double_71 = 71; + required float field_required_float_72 = 72; + required int64 field_required_int64_73 = 73; + required uint64 field_required_uint64_74 = 74; + required int32 field_required_int32_75 = 75; + required fixed64 field_required_fixed64_76 = 76; + required fixed32 field_required_fixed32_77 = 77; + required bool field_required_bool_78 = 78; + required string field_required_string_79 = 79; + required RequiredNestedMessage field_required_message_80 = 80; + required bytes field_required_bytes_81 = 81; + required uint32 field_required_uint32_82 = 82; + required TestEnum field_required_enum_83 = 83; + required sfixed32 field_required_sfixed32_84 = 84; + required sfixed64 field_required_sfixed64_85 = 85; + required sint32 field_required_sint32_86 = 86; + required sint64 field_required_sint64_87 = 87; + required group FieldRequiredGroup88 = 88 { + optional int32 field_int32_89 = 89; + } +} + +message Proto2Empty {} + +message Proto2MessageWithExtensions { + extensions 1 to 10000; +} + +extend Proto2MessageWithExtensions { + optional double field_double_1 = 1; + optional float field_float_2 = 2; + optional int64 field_int64_3 = 3; + optional uint64 field_uint64_4 = 4; + optional int32 field_int32_5 = 5; + optional fixed64 field_fixed64_6 = 6; + optional fixed32 field_fixed32_7 = 7; + optional bool field_bool_8 = 8; + optional string field_string_9 = 9; + optional Proto2Message field_message_10 = 10; + optional bytes field_bytes_11 = 11; + optional uint32 field_uint32_12 = 12; + optional Proto2Message.TestEnum field_enum_13 = 13; + optional sfixed32 field_sfixed32_14 = 14; + optional sfixed64 field_sfixed64_15 = 15; + optional sint32 field_sint32_16 = 16; + optional sint64 field_sint64_17 = 17; + + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto2Message field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated Proto2Message.TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated Proto2Message.TestEnum field_enum_list_packed_44 = 44 + [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + + optional group FieldGroup49 = 49 { + optional int32 field_int32_50 = 50; + } + + repeated group FieldGroupList51 = 51 { + optional int32 field_int32_52 = 52; + } +} + +message Proto2MessageWithMaps { + map field_map_bool_bool_1 = 1; + map field_map_bool_bytes_2 = 2; + map field_map_bool_double_3 = 3; + map field_map_bool_enum_4 = 4; + map field_map_bool_fixed32_5 = 5; + map field_map_bool_fixed64_6 = 6; + map field_map_bool_float_7 = 7; + map field_map_bool_int32_8 = 8; + map field_map_bool_int64_9 = 9; + map field_map_bool_message_10 = 10; + map field_map_bool_sfixed32_11 = 11; + map field_map_bool_sfixed64_12 = 12; + map field_map_bool_sint32_13 = 13; + map field_map_bool_sint64_14 = 14; + map field_map_bool_string_15 = 15; + map field_map_bool_uint32_16 = 16; + map field_map_bool_uint64_17 = 17; + map field_map_fixed32_bool_18 = 18; + map field_map_fixed32_bytes_19 = 19; + map field_map_fixed32_double_20 = 20; + map field_map_fixed32_enum_21 = 21; + map field_map_fixed32_fixed32_22 = 22; + map field_map_fixed32_fixed64_23 = 23; + map field_map_fixed32_float_24 = 24; + map field_map_fixed32_int32_25 = 25; + map field_map_fixed32_int64_26 = 26; + map field_map_fixed32_message_27 = 27; + map field_map_fixed32_sfixed32_28 = 28; + map field_map_fixed32_sfixed64_29 = 29; + map field_map_fixed32_sint32_30 = 30; + map field_map_fixed32_sint64_31 = 31; + map field_map_fixed32_string_32 = 32; + map field_map_fixed32_uint32_33 = 33; + map field_map_fixed32_uint64_34 = 34; + map field_map_fixed64_bool_35 = 35; + map field_map_fixed64_bytes_36 = 36; + map field_map_fixed64_double_37 = 37; + map field_map_fixed64_enum_38 = 38; + map field_map_fixed64_fixed32_39 = 39; + map field_map_fixed64_fixed64_40 = 40; + map field_map_fixed64_float_41 = 41; + map field_map_fixed64_int32_42 = 42; + map field_map_fixed64_int64_43 = 43; + map field_map_fixed64_message_44 = 44; + map field_map_fixed64_sfixed32_45 = 45; + map field_map_fixed64_sfixed64_46 = 46; + map field_map_fixed64_sint32_47 = 47; + map field_map_fixed64_sint64_48 = 48; + map field_map_fixed64_string_49 = 49; + map field_map_fixed64_uint32_50 = 50; + map field_map_fixed64_uint64_51 = 51; + map field_map_int32_bool_52 = 52; + map field_map_int32_bytes_53 = 53; + map field_map_int32_double_54 = 54; + map field_map_int32_enum_55 = 55; + map field_map_int32_fixed32_56 = 56; + map field_map_int32_fixed64_57 = 57; + map field_map_int32_float_58 = 58; + map field_map_int32_int32_59 = 59; + map field_map_int32_int64_60 = 60; + map field_map_int32_message_61 = 61; + map field_map_int32_sfixed32_62 = 62; + map field_map_int32_sfixed64_63 = 63; + map field_map_int32_sint32_64 = 64; + map field_map_int32_sint64_65 = 65; + map field_map_int32_string_66 = 66; + map field_map_int32_uint32_67 = 67; + map field_map_int32_uint64_68 = 68; + map field_map_int64_bool_69 = 69; + map field_map_int64_bytes_70 = 70; + map field_map_int64_double_71 = 71; + map field_map_int64_enum_72 = 72; + map field_map_int64_fixed32_73 = 73; + map field_map_int64_fixed64_74 = 74; + map field_map_int64_float_75 = 75; + map field_map_int64_int32_76 = 76; + map field_map_int64_int64_77 = 77; + map field_map_int64_message_78 = 78; + map field_map_int64_sfixed32_79 = 79; + map field_map_int64_sfixed64_80 = 80; + map field_map_int64_sint32_81 = 81; + map field_map_int64_sint64_82 = 82; + map field_map_int64_string_83 = 83; + map field_map_int64_uint32_84 = 84; + map field_map_int64_uint64_85 = 85; + map field_map_sfixed32_bool_86 = 86; + map field_map_sfixed32_bytes_87 = 87; + map field_map_sfixed32_double_88 = 88; + map field_map_sfixed32_enum_89 = 89; + map field_map_sfixed32_fixed32_90 = 90; + map field_map_sfixed32_fixed64_91 = 91; + map field_map_sfixed32_float_92 = 92; + map field_map_sfixed32_int32_93 = 93; + map field_map_sfixed32_int64_94 = 94; + map field_map_sfixed32_message_95 = 95; + map field_map_sfixed32_sfixed32_96 = 96; + map field_map_sfixed32_sfixed64_97 = 97; + map field_map_sfixed32_sint32_98 = 98; + map field_map_sfixed32_sint64_99 = 99; + map field_map_sfixed32_string_100 = 100; + map field_map_sfixed32_uint32_101 = 101; + map field_map_sfixed32_uint64_102 = 102; + map field_map_sfixed64_bool_103 = 103; + map field_map_sfixed64_bytes_104 = 104; + map field_map_sfixed64_double_105 = 105; + map field_map_sfixed64_enum_106 = 106; + map field_map_sfixed64_fixed32_107 = 107; + map field_map_sfixed64_fixed64_108 = 108; + map field_map_sfixed64_float_109 = 109; + map field_map_sfixed64_int32_110 = 110; + map field_map_sfixed64_int64_111 = 111; + map field_map_sfixed64_message_112 = 112; + map field_map_sfixed64_sfixed32_113 = 113; + map field_map_sfixed64_sfixed64_114 = 114; + map field_map_sfixed64_sint32_115 = 115; + map field_map_sfixed64_sint64_116 = 116; + map field_map_sfixed64_string_117 = 117; + map field_map_sfixed64_uint32_118 = 118; + map field_map_sfixed64_uint64_119 = 119; + map field_map_sint32_bool_120 = 120; + map field_map_sint32_bytes_121 = 121; + map field_map_sint32_double_122 = 122; + map field_map_sint32_enum_123 = 123; + map field_map_sint32_fixed32_124 = 124; + map field_map_sint32_fixed64_125 = 125; + map field_map_sint32_float_126 = 126; + map field_map_sint32_int32_127 = 127; + map field_map_sint32_int64_128 = 128; + map field_map_sint32_message_129 = 129; + map field_map_sint32_sfixed32_130 = 130; + map field_map_sint32_sfixed64_131 = 131; + map field_map_sint32_sint32_132 = 132; + map field_map_sint32_sint64_133 = 133; + map field_map_sint32_string_134 = 134; + map field_map_sint32_uint32_135 = 135; + map field_map_sint32_uint64_136 = 136; + map field_map_sint64_bool_137 = 137; + map field_map_sint64_bytes_138 = 138; + map field_map_sint64_double_139 = 139; + map field_map_sint64_enum_140 = 140; + map field_map_sint64_fixed32_141 = 141; + map field_map_sint64_fixed64_142 = 142; + map field_map_sint64_float_143 = 143; + map field_map_sint64_int32_144 = 144; + map field_map_sint64_int64_145 = 145; + map field_map_sint64_message_146 = 146; + map field_map_sint64_sfixed32_147 = 147; + map field_map_sint64_sfixed64_148 = 148; + map field_map_sint64_sint32_149 = 149; + map field_map_sint64_sint64_150 = 150; + map field_map_sint64_string_151 = 151; + map field_map_sint64_uint32_152 = 152; + map field_map_sint64_uint64_153 = 153; + map field_map_string_bool_154 = 154; + map field_map_string_bytes_155 = 155; + map field_map_string_double_156 = 156; + map field_map_string_enum_157 = 157; + map field_map_string_fixed32_158 = 158; + map field_map_string_fixed64_159 = 159; + map field_map_string_float_160 = 160; + map field_map_string_int32_161 = 161; + map field_map_string_int64_162 = 162; + map field_map_string_message_163 = 163; + map field_map_string_sfixed32_164 = 164; + map field_map_string_sfixed64_165 = 165; + map field_map_string_sint32_166 = 166; + map field_map_string_sint64_167 = 167; + map field_map_string_string_168 = 168; + map field_map_string_uint32_169 = 169; + map field_map_string_uint64_170 = 170; + map field_map_uint32_bool_171 = 171; + map field_map_uint32_bytes_172 = 172; + map field_map_uint32_double_173 = 173; + map field_map_uint32_enum_174 = 174; + map field_map_uint32_fixed32_175 = 175; + map field_map_uint32_fixed64_176 = 176; + map field_map_uint32_float_177 = 177; + map field_map_uint32_int32_178 = 178; + map field_map_uint32_int64_179 = 179; + map field_map_uint32_message_180 = 180; + map field_map_uint32_sfixed32_181 = 181; + map field_map_uint32_sfixed64_182 = 182; + map field_map_uint32_sint32_183 = 183; + map field_map_uint32_sint64_184 = 184; + map field_map_uint32_string_185 = 185; + map field_map_uint32_uint32_186 = 186; + map field_map_uint32_uint64_187 = 187; + map field_map_uint64_bool_188 = 188; + map field_map_uint64_bytes_189 = 189; + map field_map_uint64_double_190 = 190; + map field_map_uint64_enum_191 = 191; + map field_map_uint64_fixed32_192 = 192; + map field_map_uint64_fixed64_193 = 193; + map field_map_uint64_float_194 = 194; + map field_map_uint64_int32_195 = 195; + map field_map_uint64_int64_196 = 196; + map field_map_uint64_message_197 = 197; + map field_map_uint64_sfixed32_198 = 198; + map field_map_uint64_sfixed64_199 = 199; + map field_map_uint64_sint32_200 = 200; + map field_map_uint64_sint64_201 = 201; + map field_map_uint64_string_202 = 202; + map field_map_uint64_uint32_203 = 203; + map field_map_uint64_uint64_204 = 204; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto2_message_lite.proto b/java/core/src/test/proto/com/google/protobuf/proto2_message_lite.proto new file mode 100755 index 0000000000..7dfd40eea3 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto2_message_lite.proto @@ -0,0 +1,423 @@ +// 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. + +// LINT: ALLOW_GROUPS +syntax = "proto2"; + +package protobuf.experimental.lite; + +option java_package = "com.google.protobuf.testing"; +option java_outer_classname = "Proto2TestingLite"; + + +message Proto2MessageLite { + + enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; + } + + optional double field_double_1 = 1; + optional float field_float_2 = 2; + optional int64 field_int64_3 = 3; + optional uint64 field_uint64_4 = 4; + optional int32 field_int32_5 = 5; + optional fixed64 field_fixed64_6 = 6; + optional fixed32 field_fixed32_7 = 7; + optional bool field_bool_8 = 8; + optional string field_string_9 = 9; + optional Proto2MessageLite field_message_10 = 10; + optional bytes field_bytes_11 = 11; + optional uint32 field_uint32_12 = 12; + optional TestEnum field_enum_13 = 13; + optional sfixed32 field_sfixed32_14 = 14; + optional sfixed64 field_sfixed64_15 = 15; + optional sint32 field_sint32_16 = 16; + optional sint64 field_sint64_17 = 17; + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto2MessageLite field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated TestEnum field_enum_list_packed_44 = 44 [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + optional group FieldGroup49 = 49 { + optional int32 field_int32_50 = 50; + } + repeated group FieldGroupList51 = 51 { + optional int32 field_int32_52 = 52; + } + oneof test_oneof { + double field_double_53 = 53; + float field_float_54 = 54; + int64 field_int64_55 = 55; + uint64 field_uint64_56 = 56; + int32 field_int32_57 = 57; + fixed64 field_fixed64_58 = 58; + fixed32 field_fixed32_59 = 59; + bool field_bool_60 = 60; + string field_string_61 = 61; + Proto2MessageLite field_message_62 = 62; + bytes field_bytes_63 = 63; + uint32 field_uint32_64 = 64; + sfixed32 field_sfixed32_65 = 65; + sfixed64 field_sfixed64_66 = 66; + sint32 field_sint32_67 = 67; + sint64 field_sint64_68 = 68; + group FieldGroup69 = 69 { + optional int32 field_int32_70 = 70; + } + } + + message RequiredNestedMessage { + optional int32 value = 1; + } + + required double field_required_double_71 = 71; + required float field_required_float_72 = 72; + required int64 field_required_int64_73 = 73; + required uint64 field_required_uint64_74 = 74; + required int32 field_required_int32_75 = 75; + required fixed64 field_required_fixed64_76 = 76; + required fixed32 field_required_fixed32_77 = 77; + required bool field_required_bool_78 = 78; + required string field_required_string_79 = 79; + required RequiredNestedMessage field_required_message_80 = 80; + required bytes field_required_bytes_81 = 81; + required uint32 field_required_uint32_82 = 82; + required TestEnum field_required_enum_83 = 83; + required sfixed32 field_required_sfixed32_84 = 84; + required sfixed64 field_required_sfixed64_85 = 85; + required sint32 field_required_sint32_86 = 86; + required sint64 field_required_sint64_87 = 87; + required group FieldRequiredGroup88 = 88 { + optional int32 field_int32_89 = 89; + } +} + +message Proto2EmptyLite {} + +message Proto2MessageLiteWithExtensions { + extensions 1 to max; +} + +extend Proto2MessageLiteWithExtensions { + optional double field_double_1 = 1; + optional float field_float_2 = 2; + optional int64 field_int64_3 = 3; + optional uint64 field_uint64_4 = 4; + optional int32 field_int32_5 = 5; + optional fixed64 field_fixed64_6 = 6; + optional fixed32 field_fixed32_7 = 7; + optional bool field_bool_8 = 8; + optional string field_string_9 = 9; + optional Proto2MessageLite field_message_10 = 10; + optional bytes field_bytes_11 = 11; + optional uint32 field_uint32_12 = 12; + optional Proto2MessageLite.TestEnum field_enum_13 = 13; + optional sfixed32 field_sfixed32_14 = 14; + optional sfixed64 field_sfixed64_15 = 15; + optional sint32 field_sint32_16 = 16; + optional sint64 field_sint64_17 = 17; + + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto2MessageLite field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated Proto2MessageLite.TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated Proto2MessageLite.TestEnum field_enum_list_packed_44 = 44 + [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + + optional group FieldGroup49 = 49 { + optional int32 field_int32_50 = 50; + } + + repeated group FieldGroupList51 = 51 { + optional int32 field_int32_52 = 52; + } +} + +message Proto2MessageLiteWithMaps { + map field_map_bool_bool_1 = 1; + map field_map_bool_bytes_2 = 2; + map field_map_bool_double_3 = 3; + map field_map_bool_enum_4 = 4; + map field_map_bool_fixed32_5 = 5; + map field_map_bool_fixed64_6 = 6; + map field_map_bool_float_7 = 7; + map field_map_bool_int32_8 = 8; + map field_map_bool_int64_9 = 9; + map field_map_bool_message_10 = 10; + map field_map_bool_sfixed32_11 = 11; + map field_map_bool_sfixed64_12 = 12; + map field_map_bool_sint32_13 = 13; + map field_map_bool_sint64_14 = 14; + map field_map_bool_string_15 = 15; + map field_map_bool_uint32_16 = 16; + map field_map_bool_uint64_17 = 17; + map field_map_fixed32_bool_18 = 18; + map field_map_fixed32_bytes_19 = 19; + map field_map_fixed32_double_20 = 20; + map field_map_fixed32_enum_21 = 21; + map field_map_fixed32_fixed32_22 = 22; + map field_map_fixed32_fixed64_23 = 23; + map field_map_fixed32_float_24 = 24; + map field_map_fixed32_int32_25 = 25; + map field_map_fixed32_int64_26 = 26; + map field_map_fixed32_message_27 = 27; + map field_map_fixed32_sfixed32_28 = 28; + map field_map_fixed32_sfixed64_29 = 29; + map field_map_fixed32_sint32_30 = 30; + map field_map_fixed32_sint64_31 = 31; + map field_map_fixed32_string_32 = 32; + map field_map_fixed32_uint32_33 = 33; + map field_map_fixed32_uint64_34 = 34; + map field_map_fixed64_bool_35 = 35; + map field_map_fixed64_bytes_36 = 36; + map field_map_fixed64_double_37 = 37; + map field_map_fixed64_enum_38 = 38; + map field_map_fixed64_fixed32_39 = 39; + map field_map_fixed64_fixed64_40 = 40; + map field_map_fixed64_float_41 = 41; + map field_map_fixed64_int32_42 = 42; + map field_map_fixed64_int64_43 = 43; + map field_map_fixed64_message_44 = 44; + map field_map_fixed64_sfixed32_45 = 45; + map field_map_fixed64_sfixed64_46 = 46; + map field_map_fixed64_sint32_47 = 47; + map field_map_fixed64_sint64_48 = 48; + map field_map_fixed64_string_49 = 49; + map field_map_fixed64_uint32_50 = 50; + map field_map_fixed64_uint64_51 = 51; + map field_map_int32_bool_52 = 52; + map field_map_int32_bytes_53 = 53; + map field_map_int32_double_54 = 54; + map field_map_int32_enum_55 = 55; + map field_map_int32_fixed32_56 = 56; + map field_map_int32_fixed64_57 = 57; + map field_map_int32_float_58 = 58; + map field_map_int32_int32_59 = 59; + map field_map_int32_int64_60 = 60; + map field_map_int32_message_61 = 61; + map field_map_int32_sfixed32_62 = 62; + map field_map_int32_sfixed64_63 = 63; + map field_map_int32_sint32_64 = 64; + map field_map_int32_sint64_65 = 65; + map field_map_int32_string_66 = 66; + map field_map_int32_uint32_67 = 67; + map field_map_int32_uint64_68 = 68; + map field_map_int64_bool_69 = 69; + map field_map_int64_bytes_70 = 70; + map field_map_int64_double_71 = 71; + map field_map_int64_enum_72 = 72; + map field_map_int64_fixed32_73 = 73; + map field_map_int64_fixed64_74 = 74; + map field_map_int64_float_75 = 75; + map field_map_int64_int32_76 = 76; + map field_map_int64_int64_77 = 77; + map field_map_int64_message_78 = 78; + map field_map_int64_sfixed32_79 = 79; + map field_map_int64_sfixed64_80 = 80; + map field_map_int64_sint32_81 = 81; + map field_map_int64_sint64_82 = 82; + map field_map_int64_string_83 = 83; + map field_map_int64_uint32_84 = 84; + map field_map_int64_uint64_85 = 85; + map field_map_sfixed32_bool_86 = 86; + map field_map_sfixed32_bytes_87 = 87; + map field_map_sfixed32_double_88 = 88; + map field_map_sfixed32_enum_89 = 89; + map field_map_sfixed32_fixed32_90 = 90; + map field_map_sfixed32_fixed64_91 = 91; + map field_map_sfixed32_float_92 = 92; + map field_map_sfixed32_int32_93 = 93; + map field_map_sfixed32_int64_94 = 94; + map field_map_sfixed32_message_95 = 95; + map field_map_sfixed32_sfixed32_96 = 96; + map field_map_sfixed32_sfixed64_97 = 97; + map field_map_sfixed32_sint32_98 = 98; + map field_map_sfixed32_sint64_99 = 99; + map field_map_sfixed32_string_100 = 100; + map field_map_sfixed32_uint32_101 = 101; + map field_map_sfixed32_uint64_102 = 102; + map field_map_sfixed64_bool_103 = 103; + map field_map_sfixed64_bytes_104 = 104; + map field_map_sfixed64_double_105 = 105; + map field_map_sfixed64_enum_106 = 106; + map field_map_sfixed64_fixed32_107 = 107; + map field_map_sfixed64_fixed64_108 = 108; + map field_map_sfixed64_float_109 = 109; + map field_map_sfixed64_int32_110 = 110; + map field_map_sfixed64_int64_111 = 111; + map field_map_sfixed64_message_112 = 112; + map field_map_sfixed64_sfixed32_113 = 113; + map field_map_sfixed64_sfixed64_114 = 114; + map field_map_sfixed64_sint32_115 = 115; + map field_map_sfixed64_sint64_116 = 116; + map field_map_sfixed64_string_117 = 117; + map field_map_sfixed64_uint32_118 = 118; + map field_map_sfixed64_uint64_119 = 119; + map field_map_sint32_bool_120 = 120; + map field_map_sint32_bytes_121 = 121; + map field_map_sint32_double_122 = 122; + map field_map_sint32_enum_123 = 123; + map field_map_sint32_fixed32_124 = 124; + map field_map_sint32_fixed64_125 = 125; + map field_map_sint32_float_126 = 126; + map field_map_sint32_int32_127 = 127; + map field_map_sint32_int64_128 = 128; + map field_map_sint32_message_129 = 129; + map field_map_sint32_sfixed32_130 = 130; + map field_map_sint32_sfixed64_131 = 131; + map field_map_sint32_sint32_132 = 132; + map field_map_sint32_sint64_133 = 133; + map field_map_sint32_string_134 = 134; + map field_map_sint32_uint32_135 = 135; + map field_map_sint32_uint64_136 = 136; + map field_map_sint64_bool_137 = 137; + map field_map_sint64_bytes_138 = 138; + map field_map_sint64_double_139 = 139; + map field_map_sint64_enum_140 = 140; + map field_map_sint64_fixed32_141 = 141; + map field_map_sint64_fixed64_142 = 142; + map field_map_sint64_float_143 = 143; + map field_map_sint64_int32_144 = 144; + map field_map_sint64_int64_145 = 145; + map field_map_sint64_message_146 = 146; + map field_map_sint64_sfixed32_147 = 147; + map field_map_sint64_sfixed64_148 = 148; + map field_map_sint64_sint32_149 = 149; + map field_map_sint64_sint64_150 = 150; + map field_map_sint64_string_151 = 151; + map field_map_sint64_uint32_152 = 152; + map field_map_sint64_uint64_153 = 153; + map field_map_string_bool_154 = 154; + map field_map_string_bytes_155 = 155; + map field_map_string_double_156 = 156; + map field_map_string_enum_157 = 157; + map field_map_string_fixed32_158 = 158; + map field_map_string_fixed64_159 = 159; + map field_map_string_float_160 = 160; + map field_map_string_int32_161 = 161; + map field_map_string_int64_162 = 162; + map field_map_string_message_163 = 163; + map field_map_string_sfixed32_164 = 164; + map field_map_string_sfixed64_165 = 165; + map field_map_string_sint32_166 = 166; + map field_map_string_sint64_167 = 167; + map field_map_string_string_168 = 168; + map field_map_string_uint32_169 = 169; + map field_map_string_uint64_170 = 170; + map field_map_uint32_bool_171 = 171; + map field_map_uint32_bytes_172 = 172; + map field_map_uint32_double_173 = 173; + map field_map_uint32_enum_174 = 174; + map field_map_uint32_fixed32_175 = 175; + map field_map_uint32_fixed64_176 = 176; + map field_map_uint32_float_177 = 177; + map field_map_uint32_int32_178 = 178; + map field_map_uint32_int64_179 = 179; + map field_map_uint32_message_180 = 180; + map field_map_uint32_sfixed32_181 = 181; + map field_map_uint32_sfixed64_182 = 182; + map field_map_uint32_sint32_183 = 183; + map field_map_uint32_sint64_184 = 184; + map field_map_uint32_string_185 = 185; + map field_map_uint32_uint32_186 = 186; + map field_map_uint32_uint64_187 = 187; + map field_map_uint64_bool_188 = 188; + map field_map_uint64_bytes_189 = 189; + map field_map_uint64_double_190 = 190; + map field_map_uint64_enum_191 = 191; + map field_map_uint64_fixed32_192 = 192; + map field_map_uint64_fixed64_193 = 193; + map field_map_uint64_float_194 = 194; + map field_map_uint64_int32_195 = 195; + map field_map_uint64_int64_196 = 196; + map field_map_uint64_message_197 = 197; + map field_map_uint64_sfixed32_198 = 198; + map field_map_uint64_sfixed64_199 = 199; + map field_map_uint64_sint32_200 = 200; + map field_map_uint64_sint64_201 = 201; + map field_map_uint64_string_202 = 202; + map field_map_uint64_uint32_203 = 203; + map field_map_uint64_uint64_204 = 204; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto2_unknown_enum_values.proto b/java/core/src/test/proto/com/google/protobuf/proto2_unknown_enum_values.proto new file mode 100644 index 0000000000..5d470ac0e3 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto2_unknown_enum_values.proto @@ -0,0 +1,60 @@ +// 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. + +syntax = "proto2"; + +package proto2_unknown_enum_values; + +option java_package = "com.google.protobuf"; +option java_outer_classname = "Proto2UnknownEnumValuesTestProto"; + +enum Proto2TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; +} + +message Proto2EnumMessage { + repeated Proto2TestEnum repeated_packed_enum = 1 [packed = true]; +} + +// An enum containing a subset of the values of Proto2TestEnum, to test +// parsing unknown packed enum values +enum Proto2TestEnumSubset { + TESTENUM_SUBSET_ZERO = 0; + TESTENUM_SUBSET_ONE = 1; + // No enum value with number 2. +} + +// Test messages for packed enum, with identical field number as +// Proto2Message, to test parsing unknown packed enums. +message Proto2EnumMessageWithEnumSubset { + repeated Proto2TestEnumSubset repeated_packed_enum = 1 [packed = true]; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto3_message.proto b/java/core/src/test/proto/com/google/protobuf/proto3_message.proto new file mode 100755 index 0000000000..bcf32e1965 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto3_message.proto @@ -0,0 +1,327 @@ +// 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. + +syntax = "proto3"; + +package protobuf.experimental; + +option java_package = "com.google.protobuf.testing"; +option java_outer_classname = "Proto3Testing"; + +message Proto3SpecialFieldName { + double regular_name = 1; + int32 cached_size = 2; + int64 serialized_size = 3; + string class = 4; +} + +message Proto3Message { + enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; + } + + double field_double_1 = 1; + float field_float_2 = 2; + int64 field_int64_3 = 3; + uint64 field_uint64_4 = 4; + int32 field_int32_5 = 5; + fixed64 field_fixed64_6 = 6; + fixed32 field_fixed32_7 = 7; + bool field_bool_8 = 8; + string field_string_9 = 9; + Proto3Message field_message_10 = 10; + bytes field_bytes_11 = 11; + uint32 field_uint32_12 = 12; + TestEnum field_enum_13 = 13; + sfixed32 field_sfixed32_14 = 14; + sfixed64 field_sfixed64_15 = 15; + sint32 field_sint32_16 = 16; + sint64 field_sint64_17 = 17; + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto3Message field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated TestEnum field_enum_list_packed_44 = 44 [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + oneof test_oneof { + double field_double_53 = 53; + float field_float_54 = 54; + int64 field_int64_55 = 55; + uint64 field_uint64_56 = 56; + int32 field_int32_57 = 57; + fixed64 field_fixed64_58 = 58; + fixed32 field_fixed32_59 = 59; + bool field_bool_60 = 60; + string field_string_61 = 61; + Proto3Message field_message_62 = 62; + bytes field_bytes_63 = 63; + uint32 field_uint32_64 = 64; + sfixed32 field_sfixed32_65 = 65; + sfixed64 field_sfixed64_66 = 66; + sint32 field_sint32_67 = 67; + sint64 field_sint64_68 = 68; + } +} + +message Proto3Empty {} + +message Proto3MessageWithMaps { + map field_map_bool_bool_1 = 1; + map field_map_bool_bytes_2 = 2; + map field_map_bool_double_3 = 3; + map field_map_bool_enum_4 = 4; + map field_map_bool_fixed32_5 = 5; + map field_map_bool_fixed64_6 = 6; + map field_map_bool_float_7 = 7; + map field_map_bool_int32_8 = 8; + map field_map_bool_int64_9 = 9; + map field_map_bool_message_10 = 10; + map field_map_bool_sfixed32_11 = 11; + map field_map_bool_sfixed64_12 = 12; + map field_map_bool_sint32_13 = 13; + map field_map_bool_sint64_14 = 14; + map field_map_bool_string_15 = 15; + map field_map_bool_uint32_16 = 16; + map field_map_bool_uint64_17 = 17; + map field_map_fixed32_bool_18 = 18; + map field_map_fixed32_bytes_19 = 19; + map field_map_fixed32_double_20 = 20; + map field_map_fixed32_enum_21 = 21; + map field_map_fixed32_fixed32_22 = 22; + map field_map_fixed32_fixed64_23 = 23; + map field_map_fixed32_float_24 = 24; + map field_map_fixed32_int32_25 = 25; + map field_map_fixed32_int64_26 = 26; + map field_map_fixed32_message_27 = 27; + map field_map_fixed32_sfixed32_28 = 28; + map field_map_fixed32_sfixed64_29 = 29; + map field_map_fixed32_sint32_30 = 30; + map field_map_fixed32_sint64_31 = 31; + map field_map_fixed32_string_32 = 32; + map field_map_fixed32_uint32_33 = 33; + map field_map_fixed32_uint64_34 = 34; + map field_map_fixed64_bool_35 = 35; + map field_map_fixed64_bytes_36 = 36; + map field_map_fixed64_double_37 = 37; + map field_map_fixed64_enum_38 = 38; + map field_map_fixed64_fixed32_39 = 39; + map field_map_fixed64_fixed64_40 = 40; + map field_map_fixed64_float_41 = 41; + map field_map_fixed64_int32_42 = 42; + map field_map_fixed64_int64_43 = 43; + map field_map_fixed64_message_44 = 44; + map field_map_fixed64_sfixed32_45 = 45; + map field_map_fixed64_sfixed64_46 = 46; + map field_map_fixed64_sint32_47 = 47; + map field_map_fixed64_sint64_48 = 48; + map field_map_fixed64_string_49 = 49; + map field_map_fixed64_uint32_50 = 50; + map field_map_fixed64_uint64_51 = 51; + map field_map_int32_bool_52 = 52; + map field_map_int32_bytes_53 = 53; + map field_map_int32_double_54 = 54; + map field_map_int32_enum_55 = 55; + map field_map_int32_fixed32_56 = 56; + map field_map_int32_fixed64_57 = 57; + map field_map_int32_float_58 = 58; + map field_map_int32_int32_59 = 59; + map field_map_int32_int64_60 = 60; + map field_map_int32_message_61 = 61; + map field_map_int32_sfixed32_62 = 62; + map field_map_int32_sfixed64_63 = 63; + map field_map_int32_sint32_64 = 64; + map field_map_int32_sint64_65 = 65; + map field_map_int32_string_66 = 66; + map field_map_int32_uint32_67 = 67; + map field_map_int32_uint64_68 = 68; + map field_map_int64_bool_69 = 69; + map field_map_int64_bytes_70 = 70; + map field_map_int64_double_71 = 71; + map field_map_int64_enum_72 = 72; + map field_map_int64_fixed32_73 = 73; + map field_map_int64_fixed64_74 = 74; + map field_map_int64_float_75 = 75; + map field_map_int64_int32_76 = 76; + map field_map_int64_int64_77 = 77; + map field_map_int64_message_78 = 78; + map field_map_int64_sfixed32_79 = 79; + map field_map_int64_sfixed64_80 = 80; + map field_map_int64_sint32_81 = 81; + map field_map_int64_sint64_82 = 82; + map field_map_int64_string_83 = 83; + map field_map_int64_uint32_84 = 84; + map field_map_int64_uint64_85 = 85; + map field_map_sfixed32_bool_86 = 86; + map field_map_sfixed32_bytes_87 = 87; + map field_map_sfixed32_double_88 = 88; + map field_map_sfixed32_enum_89 = 89; + map field_map_sfixed32_fixed32_90 = 90; + map field_map_sfixed32_fixed64_91 = 91; + map field_map_sfixed32_float_92 = 92; + map field_map_sfixed32_int32_93 = 93; + map field_map_sfixed32_int64_94 = 94; + map field_map_sfixed32_message_95 = 95; + map field_map_sfixed32_sfixed32_96 = 96; + map field_map_sfixed32_sfixed64_97 = 97; + map field_map_sfixed32_sint32_98 = 98; + map field_map_sfixed32_sint64_99 = 99; + map field_map_sfixed32_string_100 = 100; + map field_map_sfixed32_uint32_101 = 101; + map field_map_sfixed32_uint64_102 = 102; + map field_map_sfixed64_bool_103 = 103; + map field_map_sfixed64_bytes_104 = 104; + map field_map_sfixed64_double_105 = 105; + map field_map_sfixed64_enum_106 = 106; + map field_map_sfixed64_fixed32_107 = 107; + map field_map_sfixed64_fixed64_108 = 108; + map field_map_sfixed64_float_109 = 109; + map field_map_sfixed64_int32_110 = 110; + map field_map_sfixed64_int64_111 = 111; + map field_map_sfixed64_message_112 = 112; + map field_map_sfixed64_sfixed32_113 = 113; + map field_map_sfixed64_sfixed64_114 = 114; + map field_map_sfixed64_sint32_115 = 115; + map field_map_sfixed64_sint64_116 = 116; + map field_map_sfixed64_string_117 = 117; + map field_map_sfixed64_uint32_118 = 118; + map field_map_sfixed64_uint64_119 = 119; + map field_map_sint32_bool_120 = 120; + map field_map_sint32_bytes_121 = 121; + map field_map_sint32_double_122 = 122; + map field_map_sint32_enum_123 = 123; + map field_map_sint32_fixed32_124 = 124; + map field_map_sint32_fixed64_125 = 125; + map field_map_sint32_float_126 = 126; + map field_map_sint32_int32_127 = 127; + map field_map_sint32_int64_128 = 128; + map field_map_sint32_message_129 = 129; + map field_map_sint32_sfixed32_130 = 130; + map field_map_sint32_sfixed64_131 = 131; + map field_map_sint32_sint32_132 = 132; + map field_map_sint32_sint64_133 = 133; + map field_map_sint32_string_134 = 134; + map field_map_sint32_uint32_135 = 135; + map field_map_sint32_uint64_136 = 136; + map field_map_sint64_bool_137 = 137; + map field_map_sint64_bytes_138 = 138; + map field_map_sint64_double_139 = 139; + map field_map_sint64_enum_140 = 140; + map field_map_sint64_fixed32_141 = 141; + map field_map_sint64_fixed64_142 = 142; + map field_map_sint64_float_143 = 143; + map field_map_sint64_int32_144 = 144; + map field_map_sint64_int64_145 = 145; + map field_map_sint64_message_146 = 146; + map field_map_sint64_sfixed32_147 = 147; + map field_map_sint64_sfixed64_148 = 148; + map field_map_sint64_sint32_149 = 149; + map field_map_sint64_sint64_150 = 150; + map field_map_sint64_string_151 = 151; + map field_map_sint64_uint32_152 = 152; + map field_map_sint64_uint64_153 = 153; + map field_map_string_bool_154 = 154; + map field_map_string_bytes_155 = 155; + map field_map_string_double_156 = 156; + map field_map_string_enum_157 = 157; + map field_map_string_fixed32_158 = 158; + map field_map_string_fixed64_159 = 159; + map field_map_string_float_160 = 160; + map field_map_string_int32_161 = 161; + map field_map_string_int64_162 = 162; + map field_map_string_message_163 = 163; + map field_map_string_sfixed32_164 = 164; + map field_map_string_sfixed64_165 = 165; + map field_map_string_sint32_166 = 166; + map field_map_string_sint64_167 = 167; + map field_map_string_string_168 = 168; + map field_map_string_uint32_169 = 169; + map field_map_string_uint64_170 = 170; + map field_map_uint32_bool_171 = 171; + map field_map_uint32_bytes_172 = 172; + map field_map_uint32_double_173 = 173; + map field_map_uint32_enum_174 = 174; + map field_map_uint32_fixed32_175 = 175; + map field_map_uint32_fixed64_176 = 176; + map field_map_uint32_float_177 = 177; + map field_map_uint32_int32_178 = 178; + map field_map_uint32_int64_179 = 179; + map field_map_uint32_message_180 = 180; + map field_map_uint32_sfixed32_181 = 181; + map field_map_uint32_sfixed64_182 = 182; + map field_map_uint32_sint32_183 = 183; + map field_map_uint32_sint64_184 = 184; + map field_map_uint32_string_185 = 185; + map field_map_uint32_uint32_186 = 186; + map field_map_uint32_uint64_187 = 187; + map field_map_uint64_bool_188 = 188; + map field_map_uint64_bytes_189 = 189; + map field_map_uint64_double_190 = 190; + map field_map_uint64_enum_191 = 191; + map field_map_uint64_fixed32_192 = 192; + map field_map_uint64_fixed64_193 = 193; + map field_map_uint64_float_194 = 194; + map field_map_uint64_int32_195 = 195; + map field_map_uint64_int64_196 = 196; + map field_map_uint64_message_197 = 197; + map field_map_uint64_sfixed32_198 = 198; + map field_map_uint64_sfixed64_199 = 199; + map field_map_uint64_sint32_200 = 200; + map field_map_uint64_sint64_201 = 201; + map field_map_uint64_string_202 = 202; + map field_map_uint64_uint32_203 = 203; + map field_map_uint64_uint64_204 = 204; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto3_message_lite.proto b/java/core/src/test/proto/com/google/protobuf/proto3_message_lite.proto new file mode 100755 index 0000000000..4a378b5bd3 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto3_message_lite.proto @@ -0,0 +1,320 @@ +// 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. + +syntax = "proto3"; + +package protobuf.experimental; + +option java_package = "com.google.protobuf.testing"; +option java_outer_classname = "Proto3TestingLite"; + +message Proto3MessageLite { + enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; + } + + double field_double_1 = 1; + float field_float_2 = 2; + int64 field_int64_3 = 3; + uint64 field_uint64_4 = 4; + int32 field_int32_5 = 5; + fixed64 field_fixed64_6 = 6; + fixed32 field_fixed32_7 = 7; + bool field_bool_8 = 8; + string field_string_9 = 9; + Proto3MessageLite field_message_10 = 10; + bytes field_bytes_11 = 11; + uint32 field_uint32_12 = 12; + TestEnum field_enum_13 = 13; + sfixed32 field_sfixed32_14 = 14; + sfixed64 field_sfixed64_15 = 15; + sint32 field_sint32_16 = 16; + sint64 field_sint64_17 = 17; + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto3MessageLite field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated TestEnum field_enum_list_packed_44 = 44 [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + oneof test_oneof { + double field_double_53 = 53; + float field_float_54 = 54; + int64 field_int64_55 = 55; + uint64 field_uint64_56 = 56; + int32 field_int32_57 = 57; + fixed64 field_fixed64_58 = 58; + fixed32 field_fixed32_59 = 59; + bool field_bool_60 = 60; + string field_string_61 = 61; + Proto3MessageLite field_message_62 = 62; + bytes field_bytes_63 = 63; + uint32 field_uint32_64 = 64; + sfixed32 field_sfixed32_65 = 65; + sfixed64 field_sfixed64_66 = 66; + sint32 field_sint32_67 = 67; + sint64 field_sint64_68 = 68; + } +} + +message Proto3EmptyLite {} + +message Proto3MessageLiteWithMaps { + map field_map_bool_bool_1 = 1; + map field_map_bool_bytes_2 = 2; + map field_map_bool_double_3 = 3; + map field_map_bool_enum_4 = 4; + map field_map_bool_fixed32_5 = 5; + map field_map_bool_fixed64_6 = 6; + map field_map_bool_float_7 = 7; + map field_map_bool_int32_8 = 8; + map field_map_bool_int64_9 = 9; + map field_map_bool_message_10 = 10; + map field_map_bool_sfixed32_11 = 11; + map field_map_bool_sfixed64_12 = 12; + map field_map_bool_sint32_13 = 13; + map field_map_bool_sint64_14 = 14; + map field_map_bool_string_15 = 15; + map field_map_bool_uint32_16 = 16; + map field_map_bool_uint64_17 = 17; + map field_map_fixed32_bool_18 = 18; + map field_map_fixed32_bytes_19 = 19; + map field_map_fixed32_double_20 = 20; + map field_map_fixed32_enum_21 = 21; + map field_map_fixed32_fixed32_22 = 22; + map field_map_fixed32_fixed64_23 = 23; + map field_map_fixed32_float_24 = 24; + map field_map_fixed32_int32_25 = 25; + map field_map_fixed32_int64_26 = 26; + map field_map_fixed32_message_27 = 27; + map field_map_fixed32_sfixed32_28 = 28; + map field_map_fixed32_sfixed64_29 = 29; + map field_map_fixed32_sint32_30 = 30; + map field_map_fixed32_sint64_31 = 31; + map field_map_fixed32_string_32 = 32; + map field_map_fixed32_uint32_33 = 33; + map field_map_fixed32_uint64_34 = 34; + map field_map_fixed64_bool_35 = 35; + map field_map_fixed64_bytes_36 = 36; + map field_map_fixed64_double_37 = 37; + map field_map_fixed64_enum_38 = 38; + map field_map_fixed64_fixed32_39 = 39; + map field_map_fixed64_fixed64_40 = 40; + map field_map_fixed64_float_41 = 41; + map field_map_fixed64_int32_42 = 42; + map field_map_fixed64_int64_43 = 43; + map field_map_fixed64_message_44 = 44; + map field_map_fixed64_sfixed32_45 = 45; + map field_map_fixed64_sfixed64_46 = 46; + map field_map_fixed64_sint32_47 = 47; + map field_map_fixed64_sint64_48 = 48; + map field_map_fixed64_string_49 = 49; + map field_map_fixed64_uint32_50 = 50; + map field_map_fixed64_uint64_51 = 51; + map field_map_int32_bool_52 = 52; + map field_map_int32_bytes_53 = 53; + map field_map_int32_double_54 = 54; + map field_map_int32_enum_55 = 55; + map field_map_int32_fixed32_56 = 56; + map field_map_int32_fixed64_57 = 57; + map field_map_int32_float_58 = 58; + map field_map_int32_int32_59 = 59; + map field_map_int32_int64_60 = 60; + map field_map_int32_message_61 = 61; + map field_map_int32_sfixed32_62 = 62; + map field_map_int32_sfixed64_63 = 63; + map field_map_int32_sint32_64 = 64; + map field_map_int32_sint64_65 = 65; + map field_map_int32_string_66 = 66; + map field_map_int32_uint32_67 = 67; + map field_map_int32_uint64_68 = 68; + map field_map_int64_bool_69 = 69; + map field_map_int64_bytes_70 = 70; + map field_map_int64_double_71 = 71; + map field_map_int64_enum_72 = 72; + map field_map_int64_fixed32_73 = 73; + map field_map_int64_fixed64_74 = 74; + map field_map_int64_float_75 = 75; + map field_map_int64_int32_76 = 76; + map field_map_int64_int64_77 = 77; + map field_map_int64_message_78 = 78; + map field_map_int64_sfixed32_79 = 79; + map field_map_int64_sfixed64_80 = 80; + map field_map_int64_sint32_81 = 81; + map field_map_int64_sint64_82 = 82; + map field_map_int64_string_83 = 83; + map field_map_int64_uint32_84 = 84; + map field_map_int64_uint64_85 = 85; + map field_map_sfixed32_bool_86 = 86; + map field_map_sfixed32_bytes_87 = 87; + map field_map_sfixed32_double_88 = 88; + map field_map_sfixed32_enum_89 = 89; + map field_map_sfixed32_fixed32_90 = 90; + map field_map_sfixed32_fixed64_91 = 91; + map field_map_sfixed32_float_92 = 92; + map field_map_sfixed32_int32_93 = 93; + map field_map_sfixed32_int64_94 = 94; + map field_map_sfixed32_message_95 = 95; + map field_map_sfixed32_sfixed32_96 = 96; + map field_map_sfixed32_sfixed64_97 = 97; + map field_map_sfixed32_sint32_98 = 98; + map field_map_sfixed32_sint64_99 = 99; + map field_map_sfixed32_string_100 = 100; + map field_map_sfixed32_uint32_101 = 101; + map field_map_sfixed32_uint64_102 = 102; + map field_map_sfixed64_bool_103 = 103; + map field_map_sfixed64_bytes_104 = 104; + map field_map_sfixed64_double_105 = 105; + map field_map_sfixed64_enum_106 = 106; + map field_map_sfixed64_fixed32_107 = 107; + map field_map_sfixed64_fixed64_108 = 108; + map field_map_sfixed64_float_109 = 109; + map field_map_sfixed64_int32_110 = 110; + map field_map_sfixed64_int64_111 = 111; + map field_map_sfixed64_message_112 = 112; + map field_map_sfixed64_sfixed32_113 = 113; + map field_map_sfixed64_sfixed64_114 = 114; + map field_map_sfixed64_sint32_115 = 115; + map field_map_sfixed64_sint64_116 = 116; + map field_map_sfixed64_string_117 = 117; + map field_map_sfixed64_uint32_118 = 118; + map field_map_sfixed64_uint64_119 = 119; + map field_map_sint32_bool_120 = 120; + map field_map_sint32_bytes_121 = 121; + map field_map_sint32_double_122 = 122; + map field_map_sint32_enum_123 = 123; + map field_map_sint32_fixed32_124 = 124; + map field_map_sint32_fixed64_125 = 125; + map field_map_sint32_float_126 = 126; + map field_map_sint32_int32_127 = 127; + map field_map_sint32_int64_128 = 128; + map field_map_sint32_message_129 = 129; + map field_map_sint32_sfixed32_130 = 130; + map field_map_sint32_sfixed64_131 = 131; + map field_map_sint32_sint32_132 = 132; + map field_map_sint32_sint64_133 = 133; + map field_map_sint32_string_134 = 134; + map field_map_sint32_uint32_135 = 135; + map field_map_sint32_uint64_136 = 136; + map field_map_sint64_bool_137 = 137; + map field_map_sint64_bytes_138 = 138; + map field_map_sint64_double_139 = 139; + map field_map_sint64_enum_140 = 140; + map field_map_sint64_fixed32_141 = 141; + map field_map_sint64_fixed64_142 = 142; + map field_map_sint64_float_143 = 143; + map field_map_sint64_int32_144 = 144; + map field_map_sint64_int64_145 = 145; + map field_map_sint64_message_146 = 146; + map field_map_sint64_sfixed32_147 = 147; + map field_map_sint64_sfixed64_148 = 148; + map field_map_sint64_sint32_149 = 149; + map field_map_sint64_sint64_150 = 150; + map field_map_sint64_string_151 = 151; + map field_map_sint64_uint32_152 = 152; + map field_map_sint64_uint64_153 = 153; + map field_map_string_bool_154 = 154; + map field_map_string_bytes_155 = 155; + map field_map_string_double_156 = 156; + map field_map_string_enum_157 = 157; + map field_map_string_fixed32_158 = 158; + map field_map_string_fixed64_159 = 159; + map field_map_string_float_160 = 160; + map field_map_string_int32_161 = 161; + map field_map_string_int64_162 = 162; + map field_map_string_message_163 = 163; + map field_map_string_sfixed32_164 = 164; + map field_map_string_sfixed64_165 = 165; + map field_map_string_sint32_166 = 166; + map field_map_string_sint64_167 = 167; + map field_map_string_string_168 = 168; + map field_map_string_uint32_169 = 169; + map field_map_string_uint64_170 = 170; + map field_map_uint32_bool_171 = 171; + map field_map_uint32_bytes_172 = 172; + map field_map_uint32_double_173 = 173; + map field_map_uint32_enum_174 = 174; + map field_map_uint32_fixed32_175 = 175; + map field_map_uint32_fixed64_176 = 176; + map field_map_uint32_float_177 = 177; + map field_map_uint32_int32_178 = 178; + map field_map_uint32_int64_179 = 179; + map field_map_uint32_message_180 = 180; + map field_map_uint32_sfixed32_181 = 181; + map field_map_uint32_sfixed64_182 = 182; + map field_map_uint32_sint32_183 = 183; + map field_map_uint32_sint64_184 = 184; + map field_map_uint32_string_185 = 185; + map field_map_uint32_uint32_186 = 186; + map field_map_uint32_uint64_187 = 187; + map field_map_uint64_bool_188 = 188; + map field_map_uint64_bytes_189 = 189; + map field_map_uint64_double_190 = 190; + map field_map_uint64_enum_191 = 191; + map field_map_uint64_fixed32_192 = 192; + map field_map_uint64_fixed64_193 = 193; + map field_map_uint64_float_194 = 194; + map field_map_uint64_int32_195 = 195; + map field_map_uint64_int64_196 = 196; + map field_map_uint64_message_197 = 197; + map field_map_uint64_sfixed32_198 = 198; + map field_map_uint64_sfixed64_199 = 199; + map field_map_uint64_sint32_200 = 200; + map field_map_uint64_sint64_201 = 201; + map field_map_uint64_string_202 = 202; + map field_map_uint64_uint32_203 = 203; + map field_map_uint64_uint64_204 = 204; +} diff --git a/java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto b/java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto index ff5bf3ae21..8bf1691431 100644 --- a/java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto +++ b/java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto @@ -37,10 +37,9 @@ syntax = "proto2"; // Some generic_services option(s) added automatically. // See: http://go/proto2-generic-services-default -option java_generic_services = true; // auto-added - package io_protocol_tests; +option java_generic_services = true; // auto-added option java_package = "com.google.protobuf"; option java_outer_classname = "TestBadIdentifiersProto"; @@ -52,9 +51,11 @@ message TestMessage { message Descriptor { option no_standard_descriptor_accessor = true; + optional string descriptor = 1; message NestedDescriptor { option no_standard_descriptor_accessor = true; + optional string descriptor = 1; } optional NestedDescriptor nested_descriptor = 2; @@ -78,12 +79,12 @@ message Deprecated { FOO = 1; // Test if @Deprecated annotation conflicts with Deprecated message name. - BAR = 2 [ deprecated = true ]; + BAR = 2 [deprecated = true]; } - optional int32 field1 = 1 [deprecated=true]; - optional TestEnum field2 = 2 [deprecated=true]; - optional TestMessage field3 = 3 [deprecated=true]; + optional int32 field1 = 1 [deprecated = true]; + optional TestEnum field2 = 2 [deprecated = true]; + optional TestMessage field3 = 3 [deprecated = true]; } message Override { @@ -124,8 +125,7 @@ message TestConflictingFieldNames { UNKNOWN = 0; FOO = 1; } - message TestMessage { - } + message TestMessage {} repeated int32 int32_field = 1; repeated TestEnum enum_field = 2; repeated string string_field = 3; @@ -138,10 +138,10 @@ message TestConflictingFieldNames { optional bytes bytes_field_count = 14; optional TestMessage message_field_count = 15; - repeated int32 Int32Field = 21; // NO_PROTO3 - repeated TestEnum EnumField = 22; // NO_PROTO3 - repeated string StringField = 23; // NO_PROTO3 - repeated bytes BytesField = 24; // NO_PROTO3 + repeated int32 Int32Field = 21; // NO_PROTO3 + repeated TestEnum EnumField = 22; // NO_PROTO3 + repeated string StringField = 23; // NO_PROTO3 + repeated bytes BytesField = 24; // NO_PROTO3 repeated TestMessage MessageField = 25; // NO_PROTO3 // This field conflicts with "int32_field" as they both generate @@ -149,9 +149,9 @@ message TestConflictingFieldNames { required int32 int32_field_list = 31; // NO_PROTO3 // These field pairs have the same Java converted name - optional string field_name = 32; // NO_PROTO3 - optional string field__name = 33; // NO_PROTO3 - optional int32 _2conflict = 34; // NO_PROTO3 + optional string field_name = 32; // NO_PROTO3 + optional string field__name = 33; // NO_PROTO3 + optional int32 _2conflict = 34; // NO_PROTO3 optional int32 __2conflict = 35; extensions 1000 to max; // NO_PROTO3 @@ -161,8 +161,8 @@ message TestConflictingFieldNames { // We don't generate accessors for extensions so the following extension // fields don't conflict with the repeated field "int64_field". optional int64 int64_field_count = 1001; // NO_PROTO3 - optional int64 int64_field_list = 1002; // NO_PROTO3 - } // NO_PROTO3 + optional int64 int64_field_list = 1002; // NO_PROTO3 + } // NO_PROTO3 } message TestMapField { diff --git a/java/core/src/test/proto/com/google/protobuf/test_custom_options.proto b/java/core/src/test/proto/com/google/protobuf/test_custom_options.proto index f8efd455a1..dde4ca5616 100644 --- a/java/core/src/test/proto/com/google/protobuf/test_custom_options.proto +++ b/java/core/src/test/proto/com/google/protobuf/test_custom_options.proto @@ -36,9 +36,9 @@ syntax = "proto2"; package protobuf_unittest; - import "google/protobuf/unittest_custom_options.proto"; + message TestMessageWithCustomOptionsContainer { optional TestMessageWithCustomOptions field = 1; } diff --git a/java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto b/java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto index 645f57b4bb..0a51fc4199 100644 --- a/java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto +++ b/java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto @@ -36,13 +36,10 @@ package protobuf_unittest; message Proto1 { option experimental_java_message_interface = "com.google.protobuf.ExtraInterfaces.HasBoolValue"; - option experimental_java_interface_extends = "com.google.protobuf.ExtraInterfaces.HasByteValue"; - option experimental_java_message_interface = "com.google.protobuf.ExtraInterfaces.HasStringValue"; - option experimental_java_builder_interface = "com.google.protobuf.ExtraInterfaces.HasStringValueBuilder" ""; diff --git a/java/core/src/test/proto/com/google/protobuf/wrappers_test.proto b/java/core/src/test/proto/com/google/protobuf/wrappers_test.proto new file mode 100755 index 0000000000..60f49e08c7 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/wrappers_test.proto @@ -0,0 +1,51 @@ +// 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. + +syntax = "proto3"; + +package wrappers_test; + +import "google/protobuf/wrappers.proto"; + +option java_package = "com.google.protobuf.wrapperstest"; +option java_outer_classname = "WrappersTestProto"; + +message TopLevelMessage { + int32 field1 = 1; + .google.protobuf.DoubleValue field_double = 2; + .google.protobuf.FloatValue field_float = 3; + .google.protobuf.Int64Value field_int64 = 4; + .google.protobuf.UInt64Value field_uint64 = 5; + .google.protobuf.Int32Value field_int32 = 6; + .google.protobuf.UInt32Value field_uint32 = 7; + .google.protobuf.BoolValue field_bool = 8; + .google.protobuf.StringValue field_string = 9; + .google.protobuf.BytesValue field_bytes = 10; +} diff --git a/java/lite.md b/java/lite.md index 84a45ec5ab..2b9e483a1a 100644 --- a/java/lite.md +++ b/java/lite.md @@ -11,22 +11,17 @@ it's designed/implemented with different constraints. In particular, Java Lite runtime has a much smaller code size which makes it more suitable to be used on Android. -To use Java Lite runtime, you need to install protoc and the protoc plugin for -Java Lite runtime. You can obtain protoc following the instructions in the -toplevel [README.md](../README.md) file. For the protoc plugin, you can -download it from maven: +Note that in order to achieve maximum performance and code size, we will +NOT guarantee API/ABI stability for Java Lite. If this is not acceptable +for your use-case, please use the full Java runtime instead. Note that +the latest version of Java Lite is not compatible with the 3.0.0 version. - https://repo1.maven.org/maven2/com/google/protobuf/protoc-gen-javalite/ +You can generate Java Lite code for your .proto files: -Choose the version that works on your platform (e.g., on windows you can -download `protoc-gen-javalite-3.0.0-windows-x86_32.exe`), rename it to -protoc-gen-javalite (or protoc-gen-javalite.exe on windows) and place it -in a directory where it can be find in PATH. + $ protoc --java_out=lite:${OUTPUT_DIR} path/to/your/proto/file -Once you have the protoc and protoc plugin, you can generate Java Lite code -for your .proto files: - - $ protoc --javalite_out=${OUTPUT_DIR} path/to/your/proto/file +Note that "optimize_for = LITE_RUNTIME" option in proto file is deprecated +and will not have any effect any more. Include the generated Java files in your project and add a dependency on the protobuf Java runtime. If you are using Maven, use the following: @@ -34,17 +29,11 @@ protobuf Java runtime. If you are using Maven, use the following: ```xml com.google.protobuf - protobuf-lite - 3.0.1 + protobuf-javalite + 3.8.0 ``` -Make sure the version number of the runtime matches (or is newer than) the -version number of the protoc plugin. The version number of the protoc doesn't -matter and any version >= 3.0.0 should work. - -### Use Protobuf Java Lite Runtime with Bazel +For the older version of Java Lite (v3.0.0), please refer to: -Bazel has native build rules to work with protobuf. For Java Lite runtime, -you can use the `java_lite_proto_library` rule. Check out [our build files -examples](../examples/BUILD) to learn how to use it. + https://github.com/protocolbuffers/protobuf/blob/javalite/java/lite.md diff --git a/java/lite/generate-sources-build.xml b/java/lite/generate-sources-build.xml new file mode 100644 index 0000000000..74d96a2f10 --- /dev/null +++ b/java/lite/generate-sources-build.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/java/lite/generate-test-sources-build.xml b/java/lite/generate-test-sources-build.xml new file mode 100644 index 0000000000..1c1a18c544 --- /dev/null +++ b/java/lite/generate-test-sources-build.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/java/lite/lite.awk b/java/lite/lite.awk new file mode 100644 index 0000000000..b22d965b6f --- /dev/null +++ b/java/lite/lite.awk @@ -0,0 +1,25 @@ +# Remove code enclosed by "BEGIN FULL-RUNTIME" and "END FULL-RUNTIME" to +# create the lite-only version of a test file. + +BEGIN { + in_full_runtime = 0; +} + +/BEGIN FULL-RUNTIME/ { + in_full_runtime = 1; + next; +} + +/END FULL-RUNTIME/ { + in_full_runtime = 0; + next; +} + +in_full_runtime { + # Skip full runtime code path. + next; +} + +{ + print; +} diff --git a/java/lite/pom.xml b/java/lite/pom.xml new file mode 100644 index 0000000000..f6e1c250fc --- /dev/null +++ b/java/lite/pom.xml @@ -0,0 +1,336 @@ + + + 4.0.0 + + com.google.protobuf + protobuf-parent + 3.9.0-rc-1 + + + protobuf-javalite + bundle + + Protocol Buffers [Lite] + + Lite version of Protocol Buffers library. This version is optimized for code size, but does + not guarantee API/ABI stability. + + + + + junit + junit + test + + + org.easymock + easymock + test + + + org.easymock + easymockclassextension + test + + + com.google.guava + guava + test + + + + + + + + ${protobuf.source.dir} + + google/protobuf/any.proto + google/protobuf/api.proto + google/protobuf/empty.proto + google/protobuf/field_mask.proto + google/protobuf/source_context.proto + google/protobuf/struct.proto + google/protobuf/timestamp.proto + google/protobuf/type.proto + google/protobuf/wrappers.proto + + + + + + ${protobuf.source.dir} + + google/protobuf/testdata/golden_message_oneof_implemented + google/protobuf/testdata/golden_packed_fields_message + + + + + + + maven-resources-plugin + 3.1.0 + + + copy-source-files + generate-sources + + copy-resources + + + ${generated.sources.dir}/com/google/protobuf + + + ${basedir}/../core/src/main/java/com/google/protobuf + + + AbstractMessageLite.java + AbstractParser.java + AbstractProtobufList.java + AllocatedBuffer.java + Android.java + ArrayDecoders.java + BinaryReader.java + BinaryWriter.java + BooleanArrayList.java + BufferAllocator.java + ByteBufferWriter.java + ByteOutput.java + ByteString.java + CodedInputStream.java + CodedInputStreamReader.java + CodedOutputStream.java + CodedOutputStreamWriter.java + DoubleArrayList.java + ExperimentalApi.java + ExtensionLite.java + ExtensionRegistryFactory.java + ExtensionRegistryLite.java + ExtensionSchema.java + ExtensionSchemaLite.java + ExtensionSchemas.java + FieldInfo.java + FieldSet.java + FieldType.java + FloatArrayList.java + GeneratedMessageInfoFactory.java + GeneratedMessageLite.java + IntArrayList.java + Internal.java + InvalidProtocolBufferException.java + IterableByteBufferInputStream.java + JavaType.java + LazyField.java + LazyFieldLite.java + LazyStringArrayList.java + LazyStringList.java + ListFieldSchema.java + LongArrayList.java + ManifestSchemaFactory.java + MapEntryLite.java + MapFieldLite.java + MapFieldSchema.java + MapFieldSchemaLite.java + MapFieldSchemas.java + MessageInfo.java + MessageInfoFactory.java + MessageLite.java + MessageLiteOrBuilder.java + MessageLiteToString.java + MessageSchema.java + MessageSetSchema.java + MutabilityOracle.java + NewInstanceSchema.java + NewInstanceSchemaLite.java + NewInstanceSchemas.java + NioByteString.java + OneofInfo.java + Parser.java + PrimitiveNonBoxingCollection.java + ProtoSyntax.java + Protobuf.java + ProtobufArrayList.java + ProtobufLists.java + ProtocolStringList.java + RawMessageInfo.java + Reader.java + RopeByteString.java + Schema.java + SchemaFactory.java + SchemaUtil.java + SmallSortedMap.java + StructuralMessageInfo.java + TextFormatEscaper.java + UninitializedMessageException.java + UnknownFieldSchema.java + UnknownFieldSetLite.java + UnknownFieldSetLiteSchema.java + UnmodifiableLazyStringList.java + UnsafeUtil.java + Utf8.java + WireFormat.java + Writer.java + + + + + + + copy-test-source-files + generate-test-sources + + copy-resources + + + ${generated.testsources.dir}/com/google/protobuf + + + ${basedir}/../core/src/test/java/com/google/protobuf + + + AbstractMessageTest.java + AbstractProto2SchemaTest.java + AnyTest.java + CodedInputStreamTest.java + DeprecatedFieldTest.java + DescriptorsTest.java + DiscardUnknownFieldsTest.java + DynamicMessageTest.java + ExtensionRegistryFactoryTest.java + FieldPresenceTest.java + ForceFieldBuildersPreRun.java + GeneratedMessageTest.java + LazyFieldTest.java + LazyStringEndToEndTest.java + MapForProto2Test.java + MapTest.java + MessageTest.java + NestedBuildersTest.java + PackedFieldTest.java + ParserTest.java + ParseExceptionsTest.java + Proto2ExtensionLookupSchemaTest.java + Proto2SchemaTest.java + Proto2UnknownEnumValueTest.java + RepeatedFieldBuilderV3Test.java + ServiceTest.java + SingleFieldBuilderV3Test.java + TestBadIdentifiers.java + TextFormatParseInfoTreeTest.java + TextFormatParseLocationTest.java + TextFormatTest.java + TestUtil.java + UnknownEnumValueTest.java + UnknownFieldSetLiteTest.java + UnknownFieldSetTest.java + WellKnownTypesTest.java + WireFormatTest.java + + + + + + + + + + + maven-antrun-plugin + + + + generate-sources + generate-sources + + + + + + + run + + + + + + generate-test-sources + generate-test-sources + + + + + + + run + + + + + process-lite-sources + generate-test-sources + + + + + + + run + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add-generated-sources + generate-sources + + add-source + + + + ${generated.sources.dir} + + + + + + add-generated-test-sources + generate-test-sources + + add-test-source + + + + ${generated.testsources.dir} + + + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + https://developers.google.com/protocol-buffers/ + com.google.protobuf + com.google.protobuf;version=${project.version} + sun.misc;resolution:=optional,* + + + + + + + diff --git a/java/lite/process-lite-sources-build.xml b/java/lite/process-lite-sources-build.xml new file mode 100644 index 0000000000..d49cf3aac0 --- /dev/null +++ b/java/lite/process-lite-sources-build.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/java/core/src/test/java/com/google/protobuf/LiteTest.java b/java/lite/src/test/java/com/google/protobuf/LiteTest.java old mode 100644 new mode 100755 similarity index 95% rename from java/core/src/test/java/com/google/protobuf/LiteTest.java rename to java/lite/src/test/java/com/google/protobuf/LiteTest.java index b20114e09b..e37e2ca0da --- a/java/core/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/lite/src/test/java/com/google/protobuf/LiteTest.java @@ -50,6 +50,8 @@ import com.google.protobuf.UnittestLite.TestHugeFieldNumbersLite; import com.google.protobuf.UnittestLite.TestNestedExtensionLite; import map_lite_test.MapTestProto.TestMap; import map_lite_test.MapTestProto.TestMap.MessageValue; +import protobuf_unittest.NestedExtensionLite; +import protobuf_unittest.NonNestedExtensionLite; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; @@ -81,7 +83,7 @@ public class LiteTest extends TestCase { // // We put this in setUp() rather than in its own test method because we // need to make sure it runs before any actual tests. - assertTrue(TestNestedExtensionLite.nestedExtension != null); + assertNotNull(TestNestedExtensionLite.nestedExtension); } public void testLite() throws Exception { @@ -97,7 +99,7 @@ public class LiteTest extends TestCase { TestAllTypesLite.newBuilder() .setOptionalInt32(123) .addRepeatedString("hello") - .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) + .setOptionalNestedMessage(NestedMessage.newBuilder().setBb(7)) .build(); ByteString data = message.toByteString(); @@ -133,11 +135,10 @@ public class LiteTest extends TestCase { TestAllExtensionsLite.newBuilder() .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) .addExtension(UnittestLite.repeatedStringExtensionLite, "hello") - .setExtension( - UnittestLite.optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ) + .setExtension(UnittestLite.optionalNestedEnumExtensionLite, NestedEnum.BAZ) .setExtension( UnittestLite.optionalNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) + NestedMessage.newBuilder().setBb(7).build()) .build(); // Test copying a message, since coping extensions actually does use a @@ -150,8 +151,7 @@ public class LiteTest extends TestCase { assertEquals(1, message2.getExtension(UnittestLite.repeatedStringExtensionLite).size()); assertEquals("hello", message2.getExtension(UnittestLite.repeatedStringExtensionLite, 0)); assertEquals( - TestAllTypesLite.NestedEnum.BAZ, - message2.getExtension(UnittestLite.optionalNestedEnumExtensionLite)); + NestedEnum.BAZ, message2.getExtension(UnittestLite.optionalNestedEnumExtensionLite)); assertEquals(7, message2.getExtension(UnittestLite.optionalNestedMessageExtensionLite).getBb()); } @@ -320,16 +320,16 @@ public class LiteTest extends TestCase { assertEquals(foreignMessage, messageAfterBuild.getOptionalForeignMessage()); message = builder.build(); - ForeignMessageLite.Builder foreignMessageBuilder = ForeignMessageLite.newBuilder().setC(3); - builder.setOptionalForeignMessage(foreignMessageBuilder); + ForeignMessageLite foreignMessageC3 = ForeignMessageLite.newBuilder().setC(3).build(); + builder.setOptionalForeignMessage(foreignMessageC3); assertEquals(ForeignMessageLite.getDefaultInstance(), message.getOptionalForeignMessage()); - assertEquals(foreignMessageBuilder.build(), builder.getOptionalForeignMessage()); + assertEquals(foreignMessageC3, builder.getOptionalForeignMessage()); messageAfterBuild = builder.build(); - assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getOptionalForeignMessage()); + assertEquals(foreignMessageC3, messageAfterBuild.getOptionalForeignMessage()); assertEquals(ForeignMessageLite.getDefaultInstance(), message.getOptionalForeignMessage()); builder.clearOptionalForeignMessage(); assertEquals(ForeignMessageLite.getDefaultInstance(), builder.getOptionalForeignMessage()); - assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getOptionalForeignMessage()); + assertEquals(foreignMessageC3, messageAfterBuild.getOptionalForeignMessage()); message = builder.build(); OptionalGroup optionalGroup = OptionalGroup.newBuilder().setA(1).build(); @@ -1033,11 +1033,11 @@ public class LiteTest extends TestCase { builder.clearRepeatedForeignMessage(); message = builder.build(); - builder.addRepeatedForeignMessage(foreignMessageBuilder); + builder.addRepeatedForeignMessage(foreignMessageC3); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedForeignMessageCount()); builder.setRepeatedForeignMessage(0, ForeignMessageLite.getDefaultInstance()); - assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getRepeatedForeignMessage(0)); + assertEquals(foreignMessageC3, messageAfterBuild.getRepeatedForeignMessage(0)); assertEquals(ForeignMessageLite.getDefaultInstance(), builder.getRepeatedForeignMessage(0)); builder.clearRepeatedForeignMessage(); @@ -1045,9 +1045,9 @@ public class LiteTest extends TestCase { builder.addRepeatedForeignMessage(0, foreignMessage); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedForeignMessageCount()); - builder.setRepeatedForeignMessage(0, foreignMessageBuilder); + builder.setRepeatedForeignMessage(0, foreignMessageC3); assertEquals(foreignMessage, messageAfterBuild.getRepeatedForeignMessage(0)); - assertEquals(foreignMessageBuilder.build(), builder.getRepeatedForeignMessage(0)); + assertEquals(foreignMessageC3, builder.getRepeatedForeignMessage(0)); builder.clearRepeatedForeignMessage(); message = builder.build(); @@ -1375,14 +1375,16 @@ public class LiteTest extends TestCase { proto = TestAllTypesLite.newBuilder() .setOptionalBool(true) - .setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ) + .setOptionalNestedEnum(NestedEnum.BAZ) .build(); - assertToStringEquals("optional_bool: true\noptional_nested_enum: BAZ", proto); + assertToStringEquals( + "optional_bool: true\noptional_nested_enum: " + NestedEnum.BAZ.toString(), proto); proto = TestAllTypesLite.newBuilder().setOptionalFloat(2.72f).setOptionalDouble(3.14).build(); assertToStringEquals("optional_double: 3.14\noptional_float: 2.72", proto); } + public void testToStringStringFields() throws Exception { TestAllTypesLite proto = TestAllTypesLite.newBuilder().setOptionalString("foo\"bar\nbaz\\").build(); @@ -1395,13 +1397,13 @@ public class LiteTest extends TestCase { public void testToStringNestedMessage() throws Exception { TestAllTypesLite proto = TestAllTypesLite.newBuilder() - .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.getDefaultInstance()) + .setOptionalNestedMessage(NestedMessage.getDefaultInstance()) .build(); assertToStringEquals("optional_nested_message {\n}", proto); proto = TestAllTypesLite.newBuilder() - .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) + .setOptionalNestedMessage(NestedMessage.newBuilder().setBb(7)) .build(); assertToStringEquals("optional_nested_message {\n bb: 7\n}", proto); } @@ -1417,8 +1419,8 @@ public class LiteTest extends TestCase { proto = TestAllTypesLite.newBuilder() - .addRepeatedLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) - .addRepeatedLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(8)) + .addRepeatedLazyMessage(NestedMessage.newBuilder().setBb(7)) + .addRepeatedLazyMessage(NestedMessage.newBuilder().setBb(8)) .build(); assertToStringEquals( "repeated_lazy_message {\n bb: 7\n}\nrepeated_lazy_message {\n bb: 8\n}", proto); @@ -1431,7 +1433,10 @@ public class LiteTest extends TestCase { .setOptionalForeignMessage(ForeignMessageLite.newBuilder().setC(3)) .build(); assertToStringEquals( - "optional_foreign_enum: FOREIGN_LITE_BAR\noptional_foreign_message {\n c: 3\n}", proto); + "optional_foreign_enum: " + + ForeignEnumLite.FOREIGN_LITE_BAR + + "\noptional_foreign_message {\n c: 3\n}", + proto); } public void testToStringExtensions() throws Exception { @@ -1440,11 +1445,10 @@ public class LiteTest extends TestCase { .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) .addExtension(UnittestLite.repeatedStringExtensionLite, "spam") .addExtension(UnittestLite.repeatedStringExtensionLite, "eggs") - .setExtension( - UnittestLite.optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ) + .setExtension(UnittestLite.optionalNestedEnumExtensionLite, NestedEnum.BAZ) .setExtension( UnittestLite.optionalNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) + NestedMessage.newBuilder().setBb(7).build()) .build(); assertToStringEquals( "[1]: 123\n[18] {\n bb: 7\n}\n[21]: 3\n[44]: \"spam\"\n[44]: \"eggs\"", message); @@ -1456,11 +1460,10 @@ public class LiteTest extends TestCase { .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) .addExtension(UnittestLite.repeatedStringExtensionLite, "spam") .addExtension(UnittestLite.repeatedStringExtensionLite, "eggs") - .setExtension( - UnittestLite.optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ) + .setExtension(UnittestLite.optionalNestedEnumExtensionLite, NestedEnum.BAZ) .setExtension( UnittestLite.optionalNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) + NestedMessage.newBuilder().setBb(7).build()) .build(); TestAllExtensionsLite messageWithUnknownFields = TestAllExtensionsLite.parseFrom(messageWithExtensions.toByteArray()); @@ -1533,7 +1536,7 @@ public class LiteTest extends TestCase { private static void assertToStringEquals(String expected, MessageLite message) { String toString = message.toString(); assertEquals('#', toString.charAt(0)); - if (toString.indexOf("\n") >= 0) { + if (toString.contains("\n")) { toString = toString.substring(toString.indexOf("\n") + 1); } else { toString = ""; @@ -1609,13 +1612,12 @@ public class LiteTest extends TestCase { public void testMergeFromNoLazyFieldSharing() throws Exception { TestAllTypesLite.Builder sourceBuilder = - TestAllTypesLite.newBuilder() - .setOptionalLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(1)); + TestAllTypesLite.newBuilder().setOptionalLazyMessage(NestedMessage.newBuilder().setBb(1)); TestAllTypesLite.Builder targetBuilder = TestAllTypesLite.newBuilder().mergeFrom(sourceBuilder.build()); assertEquals(1, sourceBuilder.getOptionalLazyMessage().getBb()); // now change the sourceBuilder, and target value shouldn't be affected. - sourceBuilder.setOptionalLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(2)); + sourceBuilder.setOptionalLazyMessage(NestedMessage.newBuilder().setBb(2)); assertEquals(1, targetBuilder.getOptionalLazyMessage().getBb()); } @@ -2113,7 +2115,7 @@ public class LiteTest extends TestCase { TestAllTypesLite.newBuilder() .setOptionalInt32(123) .addRepeatedString("hello") - .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) + .setOptionalNestedMessage(NestedMessage.newBuilder().setBb(7)) .build(); TestAllTypesLite copy = @@ -2148,11 +2150,10 @@ public class LiteTest extends TestCase { TestAllExtensionsLite.newBuilder() .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) .addExtension(UnittestLite.repeatedStringExtensionLite, "hello") - .setExtension( - UnittestLite.optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ) + .setExtension(UnittestLite.optionalNestedEnumExtensionLite, NestedEnum.BAZ) .setExtension( UnittestLite.optionalNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) + NestedMessage.newBuilder().setBb(7).build()) .build(); ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); @@ -2210,29 +2211,29 @@ public class LiteTest extends TestCase { public void testAddAllIteratesOnce() { TestAllTypesLite.newBuilder() - .addAllRepeatedBool(new OneTimeIterableList(false)) - .addAllRepeatedInt32(new OneTimeIterableList(0)) - .addAllRepeatedInt64(new OneTimeIterableList(0L)) - .addAllRepeatedFloat(new OneTimeIterableList(0f)) - .addAllRepeatedDouble(new OneTimeIterableList(0d)) - .addAllRepeatedBytes(new OneTimeIterableList(ByteString.EMPTY)) - .addAllRepeatedString(new OneTimeIterableList("")) - .addAllRepeatedNestedMessage(new OneTimeIterableList(NestedMessage.getDefaultInstance())) - .addAllRepeatedBool(new OneTimeIterable(false)) - .addAllRepeatedInt32(new OneTimeIterable(0)) - .addAllRepeatedInt64(new OneTimeIterable(0L)) - .addAllRepeatedFloat(new OneTimeIterable(0f)) - .addAllRepeatedDouble(new OneTimeIterable(0d)) - .addAllRepeatedBytes(new OneTimeIterable(ByteString.EMPTY)) - .addAllRepeatedString(new OneTimeIterable("")) - .addAllRepeatedNestedMessage(new OneTimeIterable(NestedMessage.getDefaultInstance())) + .addAllRepeatedBool(new OneTimeIterableList<>(false)) + .addAllRepeatedInt32(new OneTimeIterableList<>(0)) + .addAllRepeatedInt64(new OneTimeIterableList<>(0L)) + .addAllRepeatedFloat(new OneTimeIterableList<>(0f)) + .addAllRepeatedDouble(new OneTimeIterableList<>(0d)) + .addAllRepeatedBytes(new OneTimeIterableList<>(ByteString.EMPTY)) + .addAllRepeatedString(new OneTimeIterableList<>("")) + .addAllRepeatedNestedMessage(new OneTimeIterableList<>(NestedMessage.getDefaultInstance())) + .addAllRepeatedBool(new OneTimeIterable<>(false)) + .addAllRepeatedInt32(new OneTimeIterable<>(0)) + .addAllRepeatedInt64(new OneTimeIterable<>(0L)) + .addAllRepeatedFloat(new OneTimeIterable<>(0f)) + .addAllRepeatedDouble(new OneTimeIterable<>(0d)) + .addAllRepeatedBytes(new OneTimeIterable<>(ByteString.EMPTY)) + .addAllRepeatedString(new OneTimeIterable<>("")) + .addAllRepeatedNestedMessage(new OneTimeIterable<>(NestedMessage.getDefaultInstance())) .build(); } public void testAddAllIteratesOnce_throwsOnNull() { TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder(); try { - builder.addAllRepeatedBool(new OneTimeIterableList(true, false, (Boolean) null)); + builder.addAllRepeatedBool(new OneTimeIterableList<>(true, false, null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 2 is null.", expected.getMessage()); @@ -2240,7 +2241,7 @@ public class LiteTest extends TestCase { } try { - builder.addAllRepeatedBool(new OneTimeIterable(true, false, (Boolean) null)); + builder.addAllRepeatedBool(new OneTimeIterable<>(true, false, null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 2 is null.", expected.getMessage()); @@ -2249,7 +2250,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedBool(new OneTimeIterableList((Boolean) null)); + builder.addAllRepeatedBool(new OneTimeIterableList<>((Boolean) null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 0 is null.", expected.getMessage()); @@ -2258,7 +2259,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedInt32(new OneTimeIterableList((Integer) null)); + builder.addAllRepeatedInt32(new OneTimeIterableList<>((Integer) null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 0 is null.", expected.getMessage()); @@ -2267,7 +2268,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedInt64(new OneTimeIterableList((Long) null)); + builder.addAllRepeatedInt64(new OneTimeIterableList<>((Long) null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 0 is null.", expected.getMessage()); @@ -2276,7 +2277,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedFloat(new OneTimeIterableList((Float) null)); + builder.addAllRepeatedFloat(new OneTimeIterableList<>((Float) null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 0 is null.", expected.getMessage()); @@ -2285,7 +2286,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedDouble(new OneTimeIterableList((Double) null)); + builder.addAllRepeatedDouble(new OneTimeIterableList<>((Double) null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 0 is null.", expected.getMessage()); @@ -2294,7 +2295,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedBytes(new OneTimeIterableList((ByteString) null)); + builder.addAllRepeatedBytes(new OneTimeIterableList<>((ByteString) null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 0 is null.", expected.getMessage()); @@ -2303,7 +2304,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedString(new OneTimeIterableList("", "", (String) null, "")); + builder.addAllRepeatedString(new OneTimeIterableList<>("", "", null, "")); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 2 is null.", expected.getMessage()); @@ -2312,7 +2313,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedString(new OneTimeIterable("", "", (String) null, "")); + builder.addAllRepeatedString(new OneTimeIterable<>("", "", null, "")); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 2 is null.", expected.getMessage()); @@ -2321,7 +2322,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedString(new OneTimeIterableList((String) null)); + builder.addAllRepeatedString(new OneTimeIterableList<>((String) null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 0 is null.", expected.getMessage()); @@ -2330,7 +2331,7 @@ public class LiteTest extends TestCase { try { builder = TestAllTypesLite.newBuilder(); - builder.addAllRepeatedNestedMessage(new OneTimeIterableList((NestedMessage) null)); + builder.addAllRepeatedNestedMessage(new OneTimeIterableList<>((NestedMessage) null)); fail(); } catch (NullPointerException expected) { assertEquals("Element at index 0 is null.", expected.getMessage()); @@ -2338,6 +2339,26 @@ public class LiteTest extends TestCase { } } + public void testExtensionRenamesKeywords() { + assertTrue(NonNestedExtensionLite.package_ instanceof GeneratedMessageLite.GeneratedExtension); + assertTrue( + NestedExtensionLite.MyNestedExtensionLite.private_ + instanceof GeneratedMessageLite.GeneratedExtension); + + NonNestedExtensionLite.MessageLiteToBeExtended msg = + NonNestedExtensionLite.MessageLiteToBeExtended.newBuilder() + .setExtension(NonNestedExtensionLite.package_, true) + .build(); + assertTrue(msg.getExtension(NonNestedExtensionLite.package_)); + + msg = + NonNestedExtensionLite.MessageLiteToBeExtended.newBuilder() + .setExtension(NestedExtensionLite.MyNestedExtensionLite.private_, 2.4) + .build(); + assertEquals( + 2.4, msg.getExtension(NestedExtensionLite.MyNestedExtensionLite.private_), 0.001); + } + private static final class OneTimeIterableList extends ArrayList { private boolean wasIterated = false; diff --git a/java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java b/java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java new file mode 100755 index 0000000000..4a1d89b937 --- /dev/null +++ b/java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java @@ -0,0 +1,1327 @@ +// 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. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forFieldWithEnumVerifier; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forProto2OptionalField; +import static com.google.protobuf.FieldInfo.forProto2RequiredField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.testing.Proto2TestingLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2EmptyLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.FieldGroup49; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.FieldGroup69; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.FieldGroupList51; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.FieldRequiredGroup88; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.RequiredNestedMessage; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.TestEnum; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithExtensions; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps; +import java.lang.reflect.Field; + +/** A factory that generates a hard-coded info for {@link Proto2MessageLite}. */ +public final class Proto2MessageLiteInfoFactory implements MessageInfoFactory { + private static final Proto2MessageLiteInfoFactory instanceForRawMessageInfo = + new Proto2MessageLiteInfoFactory(true); + private static final Proto2MessageLiteInfoFactory instanceForStructuralMessageInfo = + new Proto2MessageLiteInfoFactory(false); + + public static Proto2MessageLiteInfoFactory getInstanceForRawMessageInfo() { + return instanceForRawMessageInfo; + } + + public static Proto2MessageLiteInfoFactory getInstanceForStructuralMessageInfo() { + return instanceForStructuralMessageInfo; + } + + private final boolean produceRawMessageInfo; + + private Proto2MessageLiteInfoFactory(boolean produceRawMessageInfo) { + this.produceRawMessageInfo = produceRawMessageInfo; + } + + @Override + public boolean isSupported(Class clazz) { + return true; + } + + @Override + public MessageInfo messageInfoFor(Class clazz) { + return produceRawMessageInfo ? rawMessageInfoFor(clazz) : structuralMessageInfoFor(clazz); + } + + private MessageInfo rawMessageInfoFor(Class clazz) { + if (Proto2MessageLite.class.isAssignableFrom(clazz)) { + return newRawMessageInfoForProto2MessageLite(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + private MessageInfo newRawMessageInfoForProto2MessageLite() { + java.lang.Object[] objects = + new java.lang.Object[] { + "testOneof_", + "testOneofCase_", + "bitField0_", + "bitField1_", + "fieldDouble1_", + "fieldFloat2_", + "fieldInt643_", + "fieldUint644_", + "fieldInt325_", + "fieldFixed646_", + "fieldFixed327_", + "fieldBool8_", + "fieldString9_", + "fieldMessage10_", + "fieldBytes11_", + "fieldUint3212_", + "fieldEnum13_", + Proto2MessageLite.TestEnum.internalGetVerifier(), + "fieldSfixed3214_", + "fieldSfixed6415_", + "fieldSint3216_", + "fieldSint6417_", + "fieldDoubleList18_", + "fieldFloatList19_", + "fieldInt64List20_", + "fieldUint64List21_", + "fieldInt32List22_", + "fieldFixed64List23_", + "fieldFixed32List24_", + "fieldBoolList25_", + "fieldStringList26_", + "fieldMessageList27_", + Proto2MessageLite.class, + "fieldBytesList28_", + "fieldUint32List29_", + "fieldEnumList30_", + Proto2MessageLite.TestEnum.internalGetVerifier(), + "fieldSfixed32List31_", + "fieldSfixed64List32_", + "fieldSint32List33_", + "fieldSint64List34_", + "fieldDoubleListPacked35_", + "fieldFloatListPacked36_", + "fieldInt64ListPacked37_", + "fieldUint64ListPacked38_", + "fieldInt32ListPacked39_", + "fieldFixed64ListPacked40_", + "fieldFixed32ListPacked41_", + "fieldBoolListPacked42_", + "fieldUint32ListPacked43_", + "fieldEnumListPacked44_", + Proto2MessageLite.TestEnum.internalGetVerifier(), + "fieldSfixed32ListPacked45_", + "fieldSfixed64ListPacked46_", + "fieldSint32ListPacked47_", + "fieldSint64ListPacked48_", + "fieldGroup49_", + "fieldGroupList51_", + Proto2MessageLite.FieldGroupList51.class, + Proto2MessageLite.class, + Proto2MessageLite.FieldGroup69.class, + "fieldRequiredDouble71_", + "fieldRequiredFloat72_", + "fieldRequiredInt6473_", + "fieldRequiredUint6474_", + "fieldRequiredInt3275_", + "fieldRequiredFixed6476_", + "fieldRequiredFixed3277_", + "fieldRequiredBool78_", + "fieldRequiredString79_", + "fieldRequiredMessage80_", + "fieldRequiredBytes81_", + "fieldRequiredUint3282_", + "fieldRequiredEnum83_", + Proto2MessageLite.TestEnum.internalGetVerifier(), + "fieldRequiredSfixed3284_", + "fieldRequiredSfixed6485_", + "fieldRequiredSint3286_", + "fieldRequiredSint6487_", + "fieldRequiredGroup88_", + }; + // To update this after a proto change, run protoc on proto2_message_lite.proto and copy over + // the content of the generated buildMessageInfo() method here. + java.lang.String info = + "\u0001U\u0001\u0002\u0001XU\u0000 \u0015\u0001\u0000\u0000\u0002\u0001\u0001\u0003" + + "\u0002\u0002\u0004\u0003\u0003\u0005\u0004\u0004\u0006\u0005\u0005\u0007\u0006\u0006" + + "\b\u0007\u0007\t\b\b\n\u0409\t\u000b\n\n\f\u000b\u000b\r\f\f\u000e\r\r\u000f\u000e" + + "\u000e\u0010\u000f\u000f\u0011\u0010\u0010\u0012\u0012\u0013\u0013\u0014\u0014\u0015" + + "\u0015\u0016\u0016\u0017\u0017\u0018\u0018\u0019\u0019\u001a\u001a\u001b\u041b\u001c" + + "\u001c\u001d\u001d\u001e\u001e\u001f\u001f !!\"\"##$$%%&&\'\'(())**++,,--..//00" + + "1\u0011\u00113153\u000064\u000075\u000086\u000097\u0000:8\u0000;9\u0000<:\u0000=" + + ";\u0000>\u043c\u0000?=\u0000@>\u0000A@\u0000BA\u0000CB\u0000DC\u0000ED\u0000G\u0500" + + "#H\u0501$I\u0502%J\u0503&K\u0504\'L\u0505(M\u0506)N\u0507*O\u0508+P\u0509,Q\u050a" + + "-R\u050b.S\u050c/T\u050d0U\u050e1V\u050f2W\u05103X\u05114"; + return new RawMessageInfo(Proto2MessageLite.getDefaultInstance(), info, objects); + } + + private MessageInfo structuralMessageInfoFor(Class clazz) { + if (Proto2MessageLite.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageLite(); + } else if (FieldGroup49.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroup49(); + } else if (FieldGroupList51.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroupList51(); + } else if (FieldGroup69.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroup69(); + } else if (FieldRequiredGroup88.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldRequiredGroup88(); + } else if (RequiredNestedMessage.class.isAssignableFrom(clazz)) { + return newMessageInfoForRequiredNestedMessage(); + } else if (Proto2EmptyLite.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2EmptyLite(); + } else if (Proto2MessageLiteWithExtensions.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageLiteWithExtensions(); + } else if (Proto2TestingLite.FieldGroup49.class.isAssignableFrom(clazz)) { + return newMessageInfoForExtensionFieldGroup49(); + } else if (Proto2TestingLite.FieldGroupList51.class.isAssignableFrom(clazz)) { + return newMessageInfoForExtensionFieldGroupList51(); + } else if (Proto2TestingLite.Proto2MessageLiteWithMaps.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageLiteWithMaps(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + /** + * Creates a new hard-coded info for {@link Proto2MessageLite}. Each time this is called, we + * manually go through the entire process of what a message would do if it self-registered its own + * info, including looking up each field by name. This is done for benchmarking purposes, so that + * we get a more accurate representation of the time it takes to perform this process. + */ + private static StructuralMessageInfo newMessageInfoForProto2MessageLite() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(48); + builder.withCheckInitialized( + new int[] { + 10, 27, 62, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + }); + lookupFieldsByName(builder); + return builder.build(); + } + + private static void lookupFieldsByName(StructuralMessageInfo.Builder builder) { + Field bitField0 = field(Proto2MessageLite.class, "bitField0_"); + + builder.withDefaultInstance(Proto2MessageLite.getDefaultInstance()); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldDouble1_"), + 1, + FieldType.DOUBLE, + bitField0, + 0x00000001, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldFloat2_"), + 2, + FieldType.FLOAT, + bitField0, + 0x00000002, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldInt643_"), + 3, + FieldType.INT64, + bitField0, + 0x00000004, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldUint644_"), + 4, + FieldType.UINT64, + bitField0, + 0x00000008, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldInt325_"), + 5, + FieldType.INT32, + bitField0, + 0x00000010, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldFixed646_"), + 6, + FieldType.FIXED64, + bitField0, + 0x00000020, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldFixed327_"), + 7, + FieldType.FIXED32, + bitField0, + 0x00000040, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldBool8_"), + 8, + FieldType.BOOL, + bitField0, + 0x00000080, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldString9_"), + 9, + FieldType.STRING, + bitField0, + 0x00000100, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldMessage10_"), + 10, + FieldType.MESSAGE, + bitField0, + 0x00000200, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldBytes11_"), + 11, + FieldType.BYTES, + bitField0, + 0x00000400, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldUint3212_"), + 12, + FieldType.UINT32, + bitField0, + 0x00000800, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldEnum13_"), + 13, + FieldType.ENUM, + bitField0, + 0x00001000, + false, + TestEnum.internalGetVerifier())); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldSfixed3214_"), + 14, + FieldType.SFIXED32, + bitField0, + 0x00002000, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldSfixed6415_"), + 15, + FieldType.SFIXED64, + bitField0, + 0x00004000, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldSint3216_"), + 16, + FieldType.SINT32, + bitField0, + 0x00008000, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldSint6417_"), + 17, + FieldType.SINT64, + bitField0, + 0x00010000, + false, + null)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldDoubleList18_"), + 18, + FieldType.DOUBLE_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFloatList19_"), 19, FieldType.FLOAT_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldInt64List20_"), 20, FieldType.INT64_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldUint64List21_"), + 21, + FieldType.UINT64_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldInt32List22_"), 22, FieldType.INT32_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFixed64List23_"), + 23, + FieldType.FIXED64_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFixed32List24_"), + 24, + FieldType.FIXED32_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldBoolList25_"), 25, FieldType.BOOL_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldStringList26_"), + 26, + FieldType.STRING_LIST, + false)); + builder.withField( + forRepeatedMessageField( + field(Proto2MessageLite.class, "fieldMessageList27_"), + 27, + FieldType.MESSAGE_LIST, + Proto2MessageLite.class)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldBytesList28_"), 28, FieldType.BYTES_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldUint32List29_"), + 29, + FieldType.UINT32_LIST, + false)); + builder.withField( + forFieldWithEnumVerifier( + field(Proto2MessageLite.class, "fieldEnumList30_"), + 30, + FieldType.ENUM_LIST, + TestEnum.internalGetVerifier())); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSfixed32List31_"), + 31, + FieldType.SFIXED32_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSfixed64List32_"), + 32, + FieldType.SFIXED64_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSint32List33_"), + 33, + FieldType.SINT32_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSint64List34_"), + 34, + FieldType.SINT64_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldDoubleListPacked35_"), + 35, + FieldType.DOUBLE_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFloatListPacked36_"), + 36, + FieldType.FLOAT_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldInt64ListPacked37_"), + 37, + FieldType.INT64_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldUint64ListPacked38_"), + 38, + FieldType.UINT64_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldInt32ListPacked39_"), + 39, + FieldType.INT32_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFixed64ListPacked40_"), + 40, + FieldType.FIXED64_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFixed32ListPacked41_"), + 41, + FieldType.FIXED32_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldBoolListPacked42_"), + 42, + FieldType.BOOL_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldUint32ListPacked43_"), + 43, + FieldType.UINT32_LIST_PACKED, + false)); + builder.withField( + forFieldWithEnumVerifier( + field(Proto2MessageLite.class, "fieldEnumListPacked44_"), + 44, + FieldType.ENUM_LIST_PACKED, + TestEnum.internalGetVerifier())); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSfixed32ListPacked45_"), + 45, + FieldType.SFIXED32_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSfixed64ListPacked46_"), + 46, + FieldType.SFIXED64_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSint32ListPacked47_"), + 47, + FieldType.SINT32_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSint64ListPacked48_"), + 48, + FieldType.SINT64_LIST_PACKED, + false)); + + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldGroup49_"), + 49, + FieldType.GROUP, + bitField0, + 0x00020000, + false, + null)); + builder.withField( + forRepeatedMessageField( + field(Proto2MessageLite.class, "fieldGroupList51_"), + 51, + FieldType.GROUP_LIST, + Proto2MessageLite.FieldGroupList51.class)); + + OneofInfo oneof = + new OneofInfo( + 0, + field(Proto2MessageLite.class, "testOneofCase_"), + field(Proto2MessageLite.class, "testOneof_")); + builder.withField(forOneofMemberField(53, FieldType.DOUBLE, oneof, Double.class, false, null)); + builder.withField(forOneofMemberField(54, FieldType.FLOAT, oneof, Float.class, false, null)); + builder.withField(forOneofMemberField(55, FieldType.INT64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(56, FieldType.UINT64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(57, FieldType.INT32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(58, FieldType.FIXED64, oneof, Long.class, false, null)); + builder.withField( + forOneofMemberField(59, FieldType.FIXED32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(60, FieldType.BOOL, oneof, Boolean.class, false, null)); + builder.withField(forOneofMemberField(61, FieldType.STRING, oneof, String.class, false, null)); + builder.withField( + forOneofMemberField(62, FieldType.MESSAGE, oneof, Proto2MessageLite.class, false, null)); + builder.withField( + forOneofMemberField(63, FieldType.BYTES, oneof, ByteString.class, false, null)); + builder.withField(forOneofMemberField(64, FieldType.UINT32, oneof, Integer.class, false, null)); + builder.withField( + forOneofMemberField(65, FieldType.SFIXED32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(66, FieldType.SFIXED64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(67, FieldType.SINT32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(68, FieldType.SINT64, oneof, Long.class, false, null)); + builder.withField( + forOneofMemberField( + 69, FieldType.GROUP, oneof, Proto2MessageLite.FieldGroup69.class, false, null)); + + Field bitField1 = field(Proto2MessageLite.class, "bitField1_"); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredDouble71_"), + 71, + FieldType.DOUBLE, + bitField1, + 0x00000008, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredFloat72_"), + 72, + FieldType.FLOAT, + bitField1, + 0x00000010, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredInt6473_"), + 73, + FieldType.INT64, + bitField1, + 0x00000020, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredUint6474_"), + 74, + FieldType.UINT64, + bitField1, + 0x00000040, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredInt3275_"), + 75, + FieldType.INT32, + bitField1, + 0x00000080, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredFixed6476_"), + 76, + FieldType.FIXED64, + bitField1, + 0x00000100, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredFixed3277_"), + 77, + FieldType.FIXED32, + bitField1, + 0x00000200, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredBool78_"), + 78, + FieldType.BOOL, + bitField1, + 0x00000400, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredString79_"), + 79, + FieldType.STRING, + bitField1, + 0x00000800, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredMessage80_"), + 80, + FieldType.MESSAGE, + bitField1, + 0x00001000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredBytes81_"), + 81, + FieldType.BYTES, + bitField1, + 0x00002000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredUint3282_"), + 82, + FieldType.UINT32, + bitField1, + 0x00004000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredEnum83_"), + 83, + FieldType.ENUM, + bitField1, + 0x00008000, + false, + TestEnum.internalGetVerifier())); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredSfixed3284_"), + 84, + FieldType.SFIXED32, + bitField1, + 0x00010000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredSfixed6485_"), + 85, + FieldType.SFIXED64, + bitField1, + 0x00020000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredSint3286_"), + 86, + FieldType.SINT32, + bitField1, + 0x00040000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredSint6487_"), + 87, + FieldType.SINT64, + bitField1, + 0x00080000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredGroup88_"), + 88, + FieldType.GROUP, + bitField1, + 0x00100000, + false, + null)); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroup49() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroup49.class, "fieldInt3250_"), + 50, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroupList51() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroupList51.class, "fieldInt3252_"), + 52, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroup69() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup69.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroup69.class, "fieldInt3270_"), + 70, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForRequiredNestedMessage() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(RequiredNestedMessage.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(RequiredNestedMessage.class, "value_"), + 1, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldRequiredGroup88() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldRequiredGroup88.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldRequiredGroup88.class, "fieldInt3289_"), + 89, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2EmptyLite() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2MessageLiteWithExtensions() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(0); + builder.withSyntax(ProtoSyntax.PROTO2); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForExtensionFieldGroup49() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(Proto2TestingLite.FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(Proto2TestingLite.FieldGroup49.class, "fieldInt3250_"), + 50, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForExtensionFieldGroupList51() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(Proto2TestingLite.FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(Proto2TestingLite.FieldGroupList51.class, "fieldInt3252_"), + 52, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + + private static StructuralMessageInfo newMessageInfoForProto2MessageLiteWithMaps() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(); + builder.withCheckInitialized( + new int[] { + 10, 27, 44, 61, 78, 95, 112, 129, 146, 163, 180, 197, + }); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_bool_1", 1)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_bytes_2", 2)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_double_3", 3)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_enum_4", 4)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_fixed32_5", 5)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_fixed64_6", 6)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_float_7", 7)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_int32_8", 8)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_int64_9", 9)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_message_10", 10)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_sfixed32_11", 11)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_sfixed64_12", 12)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_sint32_13", 13)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_sint64_14", 14)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_string_15", 15)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_uint32_16", 16)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_uint64_17", 17)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_bool_18", 18)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_bytes_19", 19)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_double_20", 20)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_enum_21", 21)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_fixed32_22", 22)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_fixed64_23", 23)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_float_24", 24)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_int32_25", 25)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_int64_26", 26)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_message_27", 27)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_sfixed32_28", 28)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_sfixed64_29", 29)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_sint32_30", 30)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_sint64_31", 31)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_string_32", 32)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_uint32_33", 33)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_uint64_34", 34)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_bool_35", 35)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_bytes_36", 36)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_double_37", 37)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_enum_38", 38)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_fixed32_39", 39)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_fixed64_40", 40)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_float_41", 41)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_int32_42", 42)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_int64_43", 43)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_message_44", 44)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_sfixed32_45", 45)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_sfixed64_46", 46)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_sint32_47", 47)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_sint64_48", 48)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_string_49", 49)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_uint32_50", 50)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_uint64_51", 51)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_bool_52", 52)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_bytes_53", 53)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_double_54", 54)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_enum_55", 55)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_fixed32_56", 56)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_fixed64_57", 57)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_float_58", 58)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_int32_59", 59)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_int64_60", 60)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_message_61", 61)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_sfixed32_62", 62)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_sfixed64_63", 63)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_sint32_64", 64)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_sint64_65", 65)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_string_66", 66)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_uint32_67", 67)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_uint64_68", 68)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_bool_69", 69)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_bytes_70", 70)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_double_71", 71)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_enum_72", 72)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_fixed32_73", 73)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_fixed64_74", 74)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_float_75", 75)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_int32_76", 76)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_int64_77", 77)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_message_78", 78)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_sfixed32_79", 79)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_sfixed64_80", 80)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_sint32_81", 81)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_sint64_82", 82)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_string_83", 83)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_uint32_84", 84)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_uint64_85", 85)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_bool_86", 86)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_bytes_87", 87)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_double_88", 88)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_enum_89", 89)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_fixed32_90", 90)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_fixed64_91", 91)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_float_92", 92)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_int32_93", 93)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_int64_94", 94)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_message_95", 95)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_sfixed32_96", 96)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_sfixed64_97", 97)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_sint32_98", 98)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_sint64_99", 99)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_string_100", 100)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_uint32_101", 101)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_uint64_102", 102)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_bool_103", 103)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_bytes_104", 104)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_double_105", 105)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_enum_106", 106)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_fixed32_107", 107)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_fixed64_108", 108)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_float_109", 109)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_int32_110", 110)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_int64_111", 111)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_message_112", 112)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_sfixed32_113", 113)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_sfixed64_114", 114)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_sint32_115", 115)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_sint64_116", 116)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_string_117", 117)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_uint32_118", 118)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_uint64_119", 119)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_bool_120", 120)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_bytes_121", 121)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_double_122", 122)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_enum_123", 123)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_fixed32_124", 124)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_fixed64_125", 125)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_float_126", 126)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_int32_127", 127)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_int64_128", 128)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_message_129", 129)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_sfixed32_130", 130)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_sfixed64_131", 131)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_sint32_132", 132)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_sint64_133", 133)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_string_134", 134)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_uint32_135", 135)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_uint64_136", 136)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_bool_137", 137)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_bytes_138", 138)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_double_139", 139)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_enum_140", 140)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_fixed32_141", 141)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_fixed64_142", 142)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_float_143", 143)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_int32_144", 144)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_int64_145", 145)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_message_146", 146)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_sfixed32_147", 147)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_sfixed64_148", 148)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_sint32_149", 149)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_sint64_150", 150)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_string_151", 151)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_uint32_152", 152)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_uint64_153", 153)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_bool_154", 154)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_bytes_155", 155)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_double_156", 156)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_enum_157", 157)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_fixed32_158", 158)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_fixed64_159", 159)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_float_160", 160)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_int32_161", 161)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_int64_162", 162)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_message_163", 163)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_sfixed32_164", 164)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_sfixed64_165", 165)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_sint32_166", 166)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_sint64_167", 167)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_string_168", 168)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_uint32_169", 169)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_uint64_170", 170)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_bool_171", 171)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_bytes_172", 172)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_double_173", 173)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_enum_174", 174)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_fixed32_175", 175)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_fixed64_176", 176)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_float_177", 177)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_int32_178", 178)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_int64_179", 179)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_message_180", 180)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_sfixed32_181", 181)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_sfixed64_182", 182)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_sint32_183", 183)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_sint64_184", 184)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_string_185", 185)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_uint32_186", 186)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_uint64_187", 187)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_bool_188", 188)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_bytes_189", 189)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_double_190", 190)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_enum_191", 191)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_fixed32_192", 192)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_fixed64_193", 193)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_float_194", 194)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_int32_195", 195)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_int64_196", 196)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_message_197", 197)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_sfixed32_198", 198)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_sfixed64_199", 199)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_sint32_200", 200)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_sint64_201", 201)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_string_202", 202)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_uint32_203", 203)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_uint64_204", 204)); + + return builder.build(); + } + + private static Field field(Class clazz, String name) { + try { + return clazz.getDeclaredField(name); + } catch (NoSuchFieldException | SecurityException e) { + throw new RuntimeException(e); + } + } + + private static FieldInfo mapFieldInfo(Class clazz, String fieldName, int fieldNumber) { + try { + return forMapField( + field(clazz, SchemaUtil.toCamelCase(fieldName, false) + "_"), + fieldNumber, + SchemaUtil.getMapDefaultEntry(clazz, fieldName), + fieldName.contains("_enum_") ? TestEnum.internalGetVerifier() : null); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } +} diff --git a/java/pom.xml b/java/pom.xml index 6526b650a8..383ff5d0e6 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -1,17 +1,10 @@ - + 4.0.0 - - com.google - google - 1 - com.google.protobuf protobuf-parent - 3.6.1 + 3.9.0-rc-1 pom Protocol Buffers [Parent] @@ -22,6 +15,17 @@ extensible format. + + + haon + Hao Nguyen + haon@google.com + Google + https://cloud.google.com + America/Los_Angeles + + + UTF-8 UTF-8 @@ -44,8 +48,8 @@ - https://github.com/google/protobuf - scm:git:https://github.com/google/protobuf.git + https://github.com/protocolbuffers/protobuf + scm:git:https://github.com/protocolbuffers/protobuf.git @@ -61,6 +65,13 @@ + + com.google.protobuf + protobuf-bom + ${project.version} + pom + import + junit junit @@ -82,7 +93,19 @@ com.google.guava guava - 19.0 + 28.0-android + + + com.google.guava + guava-testlib + 28.0-jre + test + + + com.google.truth + truth + 0.45 + test @@ -205,6 +228,8 @@ + bom + lite core util diff --git a/java/util/pom.xml b/java/util/pom.xml index f175cf1522..b905180dd5 100644 --- a/java/util/pom.xml +++ b/java/util/pom.xml @@ -1,12 +1,10 @@ - + 4.0.0 com.google.protobuf protobuf-parent - 3.6.1 + 3.9.0-rc-1 protobuf-java-util @@ -19,16 +17,25 @@ ${project.groupId} protobuf-java - ${project.version} com.google.guava guava + + com.google.errorprone + error_prone_annotations + 2.3.2 + + + com.google.guava + guava-testlib + test + com.google.code.gson gson - 2.7 + 2.8.5 junit @@ -42,6 +49,11 @@ org.easymock easymockclassextension + + com.google.truth + truth + test + @@ -65,10 +77,10 @@ - - - - + + + + diff --git a/java/util/src/main/java/com/google/protobuf/util/Durations.java b/java/util/src/main/java/com/google/protobuf/util/Durations.java index 17b41cbbcf..1edc8f0af2 100644 --- a/java/util/src/main/java/com/google/protobuf/util/Durations.java +++ b/java/util/src/main/java/com/google/protobuf/util/Durations.java @@ -30,6 +30,7 @@ package com.google.protobuf.util; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.math.IntMath.checkedAdd; import static com.google.common.math.IntMath.checkedSubtract; import static com.google.common.math.LongMath.checkedAdd; @@ -41,6 +42,7 @@ import static com.google.protobuf.util.Timestamps.NANOS_PER_MICROSECOND; import static com.google.protobuf.util.Timestamps.NANOS_PER_MILLISECOND; import static com.google.protobuf.util.Timestamps.NANOS_PER_SECOND; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.protobuf.Duration; import java.text.ParseException; import java.util.Comparator; @@ -53,6 +55,10 @@ public final class Durations { static final long DURATION_SECONDS_MIN = -315576000000L; static final long DURATION_SECONDS_MAX = 315576000000L; + private static final long SECONDS_PER_MINUTE = 60L; + private static final long SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60; + private static final long SECONDS_PER_DAY = SECONDS_PER_HOUR * 24; + /** A constant holding the minimum valid {@link Duration}, approximately {@code -10,000} years. */ public static final Duration MIN_VALUE = Duration.newBuilder().setSeconds(DURATION_SECONDS_MIN).setNanos(-999999999).build(); @@ -86,8 +92,8 @@ public final class Durations { } /** - * Compares two durations. The value returned is identical to what would be returned by: - * {@code Durations.comparator().compare(x, y)}. + * Compares two durations. The value returned is identical to what would be returned by: {@code + * Durations.comparator().compare(x, y)}. * * @return the value {@code 0} if {@code x == y}; a value less than {@code 0} if {@code x < y}; * and a value greater than {@code 0} if {@code x > y} @@ -118,6 +124,7 @@ public final class Durations { * and a positive or negative {@code nanos} field. For durations of one second or more, a non-zero * value for the {@code nanos} field must be of the same sign as the {@code seconds} field. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static boolean isValid(long seconds, int nanos) { if (seconds < DURATION_SECONDS_MIN || seconds > DURATION_SECONDS_MAX) { return false; @@ -133,20 +140,68 @@ public final class Durations { return true; } + /** Returns whether the given {@link Duration} is negative or not. */ + public static boolean isNegative(Duration duration) { + checkValid(duration); + return (duration.getSeconds() == 0) ? duration.getNanos() < 0 : duration.getSeconds() < 0; + } + + /** + * Ensures that the given {@link Duration} is not negative. + * + * @throws IllegalArgumentException if {@code duration} is negative or invalid + * @throws NullPointerException if {@code duration} is {@code null} + */ + @CanIgnoreReturnValue + public static Duration checkNotNegative(Duration duration) { + checkValid(duration); + checkArgument(!isNegative(duration), "duration (%s) must not be negative", toString(duration)); + return duration; + } + + /** + * Ensures that the given {@link Duration} is positive. + * + * @throws IllegalArgumentException if {@code duration} is negative, {@code ZERO}, or invalid + * @throws NullPointerException if {@code duration} is {@code null} + */ + @CanIgnoreReturnValue + public static Duration checkPositive(Duration duration) { + checkValid(duration); + checkArgument( + !isNegative(duration) && !duration.equals(ZERO), + "duration (%s) must be positive", + toString(duration)); + return duration; + } + /** Throws an {@link IllegalArgumentException} if the given {@link Duration} is not valid. */ + @CanIgnoreReturnValue public static Duration checkValid(Duration duration) { long seconds = duration.getSeconds(); int nanos = duration.getNanos(); if (!isValid(seconds, nanos)) { - throw new IllegalArgumentException(String.format( - "Duration is not valid. See proto definition for valid values. " - + "Seconds (%s) must be in range [-315,576,000,000, +315,576,000,000]. " - + "Nanos (%s) must be in range [-999,999,999, +999,999,999]. " - + "Nanos must have the same sign as seconds", seconds, nanos)); + throw new IllegalArgumentException( + String.format( + "Duration is not valid. See proto definition for valid values. " + + "Seconds (%s) must be in range [-315,576,000,000, +315,576,000,000]. " + + "Nanos (%s) must be in range [-999,999,999, +999,999,999]. " + + "Nanos must have the same sign as seconds", + seconds, nanos)); } return duration; } + /** + * Builds the given builder and throws an {@link IllegalArgumentException} if it is not valid. See + * {@link #checkValid(Duration)}. + * + * @return A valid, built {@link Duration}. + */ + public static Duration checkValid(Duration.Builder durationBuilder) { + return checkValid(durationBuilder.build()); + } + /** * Convert Duration to string format. The string format will contains 3, 6, or 9 fractional digits * depending on the precision required to represent the exact Duration value. For example: "1s", @@ -216,30 +271,125 @@ public final class Durations { } } + // Static factories + + /** Create a Duration from the number of days. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static Duration fromDays(long days) { + return Duration.newBuilder() + .setSeconds(checkedMultiply(days, SECONDS_PER_DAY)) + .setNanos(0) + .build(); + } + + /** Create a Duration from the number of hours. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static Duration fromHours(long hours) { + return Duration.newBuilder() + .setSeconds(checkedMultiply(hours, SECONDS_PER_HOUR)) + .setNanos(0) + .build(); + } + + /** Create a Duration from the number of minutes. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static Duration fromMinutes(long minutes) { + return Duration.newBuilder() + .setSeconds(checkedMultiply(minutes, SECONDS_PER_MINUTE)) + .setNanos(0) + .build(); + } + /** Create a Duration from the number of seconds. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static Duration fromSeconds(long seconds) { return normalizedDuration(seconds, 0); } + /** Create a Duration from the number of milliseconds. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static Duration fromMillis(long milliseconds) { + return normalizedDuration( + milliseconds / MILLIS_PER_SECOND, + (int) (milliseconds % MILLIS_PER_SECOND * NANOS_PER_MILLISECOND)); + } + + /** Create a Duration from the number of microseconds. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static Duration fromMicros(long microseconds) { + return normalizedDuration( + microseconds / MICROS_PER_SECOND, + (int) (microseconds % MICROS_PER_SECOND * NANOS_PER_MICROSECOND)); + } + + /** Create a Duration from the number of nanoseconds. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static Duration fromNanos(long nanoseconds) { + return normalizedDuration( + nanoseconds / NANOS_PER_SECOND, (int) (nanoseconds % NANOS_PER_SECOND)); + } + + // Conversion APIs + + /** + * Convert a Duration to the number of days. The result will be rounded towards 0 to the nearest + * day. + */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static long toDays(Duration duration) { + return checkValid(duration).getSeconds() / SECONDS_PER_DAY; + } + + /** + * Convert a Duration to the number of hours. The result will be rounded towards 0 to the nearest + * hour. + */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static long toHours(Duration duration) { + return checkValid(duration).getSeconds() / SECONDS_PER_HOUR; + } + + /** + * Convert a Duration to the number of minutes. The result will be rounded towards 0 to the + * nearest minute. + */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API + public static long toMinutes(Duration duration) { + return checkValid(duration).getSeconds() / SECONDS_PER_MINUTE; + } + /** * Convert a Duration to the number of seconds. The result will be rounded towards 0 to the * nearest second. E.g., if the duration represents -1 nanosecond, it will be rounded to 0. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static long toSeconds(Duration duration) { return checkValid(duration).getSeconds(); } - /** Create a Duration from the number of milliseconds. */ - public static Duration fromMillis(long milliseconds) { - return normalizedDuration( - milliseconds / MILLIS_PER_SECOND, - (int) (milliseconds % MILLIS_PER_SECOND * NANOS_PER_MILLISECOND)); + /** + * Returns the number of seconds of the given duration as a {@code double}. This method should be + * used to accommodate APIs that only accept durations as {@code double} values. + * + *

This conversion may lose precision. + * + *

If you need the number of seconds in this duration as a {@code long} (not a {@code double}), + * simply use {@code duration.getSeconds()} or {@link #toSeconds} (which includes validation). + */ + @SuppressWarnings({ + "DurationSecondsToDouble", // that's the whole point of this method + "GoodTime" // this is a legacy conversion API + }) + public static double toSecondsAsDouble(Duration duration) { + checkValid(duration); + return duration.getSeconds() + duration.getNanos() / 1e9; } /** * Convert a Duration to the number of milliseconds. The result will be rounded towards 0 to the * nearest millisecond. E.g., if the duration represents -1 nanosecond, it will be rounded to 0. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static long toMillis(Duration duration) { checkValid(duration); return checkedAdd( @@ -247,17 +397,11 @@ public final class Durations { duration.getNanos() / NANOS_PER_MILLISECOND); } - /** Create a Duration from the number of microseconds. */ - public static Duration fromMicros(long microseconds) { - return normalizedDuration( - microseconds / MICROS_PER_SECOND, - (int) (microseconds % MICROS_PER_SECOND * NANOS_PER_MICROSECOND)); - } - /** * Convert a Duration to the number of microseconds. The result will be rounded towards 0 to the * nearest microseconds. E.g., if the duration represents -1 nanosecond, it will be rounded to 0. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static long toMicros(Duration duration) { checkValid(duration); return checkedAdd( @@ -265,19 +409,16 @@ public final class Durations { duration.getNanos() / NANOS_PER_MICROSECOND); } - /** Create a Duration from the number of nanoseconds. */ - public static Duration fromNanos(long nanoseconds) { - return normalizedDuration( - nanoseconds / NANOS_PER_SECOND, (int) (nanoseconds % NANOS_PER_SECOND)); - } - /** Convert a Duration to the number of nanoseconds. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static long toNanos(Duration duration) { checkValid(duration); return checkedAdd( checkedMultiply(duration.getSeconds(), NANOS_PER_SECOND), duration.getNanos()); } + // Math operations + /** Add two durations. */ public static Duration add(Duration d1, Duration d2) { checkValid(d1); diff --git a/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java b/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java index 86f56ad99f..d07731d248 100644 --- a/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java +++ b/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java @@ -30,6 +30,7 @@ package com.google.protobuf.util; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.FieldMask; @@ -88,15 +89,14 @@ final class FieldMaskTree { } /** - * Adds a field path to the tree. In a FieldMask, every field path matches the - * specified field as well as all its sub-fields. For example, a field path - * "foo.bar" matches field "foo.bar" and also "foo.bar.baz", etc. When adding - * a field path to the tree, redundant sub-paths will be removed. That is, - * after adding "foo.bar" to the tree, "foo.bar.baz" will be removed if it - * exists, which will turn the tree node for "foo.bar" to a leaf node. - * Likewise, if the field path to add is a sub-path of an existing leaf node, - * nothing will be changed in the tree. + * Adds a field path to the tree. In a FieldMask, every field path matches the specified field as + * well as all its sub-fields. For example, a field path "foo.bar" matches field "foo.bar" and + * also "foo.bar.baz", etc. When adding a field path to the tree, redundant sub-paths will be + * removed. That is, after adding "foo.bar" to the tree, "foo.bar.baz" will be removed if it + * exists, which will turn the tree node for "foo.bar" to a leaf node. Likewise, if the field path + * to add is a sub-path of an existing leaf node, nothing will be changed in the tree. */ + @CanIgnoreReturnValue FieldMaskTree addFieldPath(String path) { String[] parts = path.split(FIELD_PATH_SEPARATOR_REGEX); if (parts.length == 0) { @@ -125,9 +125,8 @@ final class FieldMaskTree { return this; } - /** - * Merges all field paths in a FieldMask into this tree. - */ + /** Merges all field paths in a FieldMask into this tree. */ + @CanIgnoreReturnValue FieldMaskTree mergeFromFieldMask(FieldMask mask) { for (String path : mask.getPathsList()) { addFieldPath(path); diff --git a/java/util/src/main/java/com/google/protobuf/util/FieldMaskUtil.java b/java/util/src/main/java/com/google/protobuf/util/FieldMaskUtil.java index aedc5eac21..6af1493331 100644 --- a/java/util/src/main/java/com/google/protobuf/util/FieldMaskUtil.java +++ b/java/util/src/main/java/com/google/protobuf/util/FieldMaskUtil.java @@ -36,12 +36,12 @@ import com.google.common.base.CaseFormat; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.primitives.Ints; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.FieldMask; import com.google.protobuf.Internal; import com.google.protobuf.Message; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -277,9 +277,7 @@ public class FieldMaskUtil { /** * Whether to replace message fields (i.e., discard existing content in - * destination message fields) when merging. - * Default behavior is to merge the source message field into the - * destination message field. + * destination message fields). */ public boolean replaceMessageFields() { return replaceMessageFields; @@ -287,9 +285,7 @@ public class FieldMaskUtil { /** * Whether to replace repeated fields (i.e., discard existing content in - * destination repeated fields) when merging. - * Default behavior is to append elements from source repeated field to the - * destination repeated field. + * destination repeated fields). */ public boolean replaceRepeatedFields() { return replaceRepeatedFields; @@ -297,30 +293,51 @@ public class FieldMaskUtil { /** * Whether to replace primitive (non-repeated and non-message) fields in - * destination message fields with the source primitive fields (i.e., if the - * field is set in the source, the value is copied to the - * destination; if the field is unset in the source, the field is cleared - * from the destination) when merging. - * - *

Default behavior is to always set the value of the source primitive - * field to the destination primitive field, and if the source field is - * unset, the default value of the source field is copied to the - * destination. + * destination message fields with the source primitive fields (i.e., clear + * destination field if source field is not set). */ public boolean replacePrimitiveFields() { return replacePrimitiveFields; } + /** + * Specify whether to replace message fields. Defaults to false. + * + *

If true, discard existing content in destination message fields when merging. + * + *

If false, merge the source message field into the destination message field. + */ + @CanIgnoreReturnValue public MergeOptions setReplaceMessageFields(boolean value) { replaceMessageFields = value; return this; } + /** + * Specify whether to replace repeated fields. Defaults to false. + * + *

If true, discard existing content in destination repeated fields) when merging. + * + *

If false, append elements from source repeated field to the destination repeated field. + */ + @CanIgnoreReturnValue public MergeOptions setReplaceRepeatedFields(boolean value) { replaceRepeatedFields = value; return this; } + /** + * Specify whether to replace primitive (non-repeated and non-message) fields in destination + * message fields with the source primitive fields. Defaults to false. + * + *

If true, set the value of the destination primitive field to the source primitive field if + * the source field is set, but clear the destination field otherwise. + * + *

If false, always set the value of the destination primitive field to the source primitive + * field, and if the source field is unset, the default value of the source field is copied to + * the destination. + */ + @CanIgnoreReturnValue public MergeOptions setReplacePrimitiveFields(boolean value) { replacePrimitiveFields = value; return this; diff --git a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java index 955dfd8677..bf2e28ed72 100644 --- a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java +++ b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java @@ -32,6 +32,7 @@ package com.google.protobuf.util; import com.google.common.base.Preconditions; import com.google.common.io.BaseEncoding; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; @@ -77,7 +78,9 @@ import java.io.StringReader; import java.math.BigDecimal; import java.math.BigInteger; import java.text.ParseException; +import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -106,7 +109,7 @@ public class JsonFormat { public static Printer printer() { return new Printer( TypeRegistry.getEmptyTypeRegistry(), false, Collections.emptySet(), - false, false, false); + false, false, false, false); } /** @@ -127,6 +130,7 @@ public class JsonFormat { private final boolean preservingProtoFieldNames; private final boolean omittingInsignificantWhitespace; private final boolean printingEnumsAsInts; + private final boolean sortingMapKeys; private Printer( TypeRegistry registry, @@ -134,13 +138,15 @@ public class JsonFormat { Set includingDefaultValueFields, boolean preservingProtoFieldNames, boolean omittingInsignificantWhitespace, - boolean printingEnumsAsInts) { + boolean printingEnumsAsInts, + boolean sortingMapKeys) { this.registry = registry; this.alwaysOutputDefaultValueFields = alwaysOutputDefaultValueFields; this.includingDefaultValueFields = includingDefaultValueFields; this.preservingProtoFieldNames = preservingProtoFieldNames; this.omittingInsignificantWhitespace = omittingInsignificantWhitespace; this.printingEnumsAsInts = printingEnumsAsInts; + this.sortingMapKeys = sortingMapKeys; } /** @@ -159,7 +165,8 @@ public class JsonFormat { includingDefaultValueFields, preservingProtoFieldNames, omittingInsignificantWhitespace, - printingEnumsAsInts); + printingEnumsAsInts, + sortingMapKeys); } /** @@ -176,7 +183,8 @@ public class JsonFormat { Collections.emptySet(), preservingProtoFieldNames, omittingInsignificantWhitespace, - printingEnumsAsInts); + printingEnumsAsInts, + sortingMapKeys); } /** @@ -193,7 +201,8 @@ public class JsonFormat { Collections.emptySet(), preservingProtoFieldNames, omittingInsignificantWhitespace, - true); + true, + sortingMapKeys); } private void checkUnsetPrintingEnumsAsInts() { @@ -218,10 +227,11 @@ public class JsonFormat { return new Printer( registry, false, - fieldsToAlwaysOutput, + Collections.unmodifiableSet(new HashSet<>(fieldsToAlwaysOutput)), preservingProtoFieldNames, omittingInsignificantWhitespace, - printingEnumsAsInts); + printingEnumsAsInts, + sortingMapKeys); } private void checkUnsetIncludingDefaultValueFields() { @@ -244,7 +254,8 @@ public class JsonFormat { includingDefaultValueFields, true, omittingInsignificantWhitespace, - printingEnumsAsInts); + printingEnumsAsInts, + sortingMapKeys); } @@ -272,7 +283,31 @@ public class JsonFormat { includingDefaultValueFields, preservingProtoFieldNames, true, - printingEnumsAsInts); + printingEnumsAsInts, + sortingMapKeys); + } + + /** + * Create a new {@link Printer} that will sort the map keys in the JSON output. + * + * Use of this modifier is discouraged, the generated JSON messages are equivalent + * with and without this option set, but there are some corner caseuse cases that + * demand a stable output, while order of map keys is otherwise arbitrary. + * + * The generated order is not well-defined and should not be depended on, but + * it's stable. + * + * This new Printer clones all other configurations from the current {@link Printer}. + */ + public Printer sortingMapKeys() { + return new Printer( + registry, + alwaysOutputDefaultValueFields, + includingDefaultValueFields, + preservingProtoFieldNames, + omittingInsignificantWhitespace, + printingEnumsAsInts, + true); } /** @@ -292,7 +327,8 @@ public class JsonFormat { preservingProtoFieldNames, output, omittingInsignificantWhitespace, - printingEnumsAsInts) + printingEnumsAsInts, + sortingMapKeys) .print(message); } @@ -412,29 +448,34 @@ public class JsonFormat { } /** - * Find a type by its full name. Returns null if it cannot be found in - * this {@link TypeRegistry}. + * Find a type by its full name. Returns null if it cannot be found in this {@link + * TypeRegistry}. */ public Descriptor find(String name) { return types.get(name); } + /* @Nullable */ + Descriptor getDescriptorForTypeUrl(String typeUrl) throws InvalidProtocolBufferException { + return find(getTypeName(typeUrl)); + } + private final Map types; private TypeRegistry(Map types) { this.types = types; } - /** - * A Builder is used to build {@link TypeRegistry}. - */ + + /** A Builder is used to build {@link TypeRegistry}. */ public static class Builder { private Builder() {} /** - * Adds a message type and all types defined in the same .proto file as - * well as all transitively imported .proto files to this {@link Builder}. + * Adds a message type and all types defined in the same .proto file as well as all + * transitively imported .proto files to this {@link Builder}. */ + @CanIgnoreReturnValue public Builder add(Descriptor messageType) { if (types == null) { throw new IllegalStateException("A TypeRegistry.Builer can only be used once."); @@ -444,9 +485,10 @@ public class JsonFormat { } /** - * Adds message types and all types defined in the same .proto file as - * well as all transitively imported .proto files to this {@link Builder}. + * Adds message types and all types defined in the same .proto file as well as all + * transitively imported .proto files to this {@link Builder}. */ + @CanIgnoreReturnValue public Builder add(Iterable messageTypes) { if (types == null) { throw new IllegalStateException("A TypeRegistry.Builder can only be used once."); @@ -604,6 +646,7 @@ public class JsonFormat { private final Set includingDefaultValueFields; private final boolean preservingProtoFieldNames; private final boolean printingEnumsAsInts; + private final boolean sortingMapKeys; private final TextGenerator generator; // We use Gson to help handle string escapes. private final Gson gson; @@ -621,12 +664,14 @@ public class JsonFormat { boolean preservingProtoFieldNames, Appendable jsonOutput, boolean omittingInsignificantWhitespace, - boolean printingEnumsAsInts) { + boolean printingEnumsAsInts, + boolean sortingMapKeys) { this.registry = registry; this.alwaysOutputDefaultValueFields = alwaysOutputDefaultValueFields; this.includingDefaultValueFields = includingDefaultValueFields; this.preservingProtoFieldNames = preservingProtoFieldNames; this.printingEnumsAsInts = printingEnumsAsInts; + this.sortingMapKeys = sortingMapKeys; this.gson = GsonHolder.DEFAULT_GSON; // json format related properties, determined by printerType if (omittingInsignificantWhitespace) { @@ -760,15 +805,14 @@ public class JsonFormat { throw new InvalidProtocolBufferException("Invalid Any type."); } String typeUrl = (String) message.getField(typeUrlField); - String typeName = getTypeName(typeUrl); - Descriptor type = registry.find(typeName); + Descriptor type = registry.getDescriptorForTypeUrl(typeUrl); if (type == null) { throw new InvalidProtocolBufferException("Cannot find type for url: " + typeUrl); } ByteString content = (ByteString) message.getField(valueField); Message contentMessage = DynamicMessage.getDefaultInstance(type).getParserForType().parseFrom(content); - WellKnownTypePrinter printer = wellKnownTypePrinters.get(typeName); + WellKnownTypePrinter printer = wellKnownTypePrinters.get(getTypeName(typeUrl)); if (printer != null) { // If the type is one of the well-known types, we use a special // formatting. @@ -957,8 +1001,32 @@ public class JsonFormat { } generator.print("{" + blankOrNewLine); generator.indent(); + + @SuppressWarnings("unchecked") // Object guaranteed to be a List for a map field. + Collection elements = (List) value; + if (sortingMapKeys && !elements.isEmpty()) { + Comparator cmp = null; + if (keyField.getType() == FieldDescriptor.Type.STRING) { + cmp = new Comparator() { + @Override + public int compare(final Object o1, final Object o2) { + ByteString s1 = ByteString.copyFromUtf8((String) o1); + ByteString s2 = ByteString.copyFromUtf8((String) o2); + return ByteString.unsignedLexicographicalComparator().compare(s1, s2); + } + }; + } + TreeMap tm = new TreeMap(cmp); + for (Object element : elements) { + Message entry = (Message) element; + Object entryKey = entry.getField(keyField); + tm.put(entryKey, element); + } + elements = tm.values(); + } + boolean printedElement = false; - for (Object element : (List) value) { + for (Object element : elements) { Message entry = (Message) element; Object entryKey = entry.getField(keyField); Object entryValue = entry.getField(valueField); @@ -1378,7 +1446,7 @@ public class JsonFormat { throw new InvalidProtocolBufferException("Missing type url when parsing: " + json); } String typeUrl = typeUrlElement.getAsString(); - Descriptor contentType = registry.find(getTypeName(typeUrl)); + Descriptor contentType = registry.getDescriptorForTypeUrl(typeUrl); if (contentType == null) { throw new InvalidProtocolBufferException("Cannot resolve type: " + typeUrl); } @@ -1495,16 +1563,6 @@ public class JsonFormat { throw new InvalidProtocolBufferException( "Field " + field.getFullName() + " has already been set."); } - if (field.getContainingOneof() != null - && builder.getOneofFieldDescriptor(field.getContainingOneof()) != null) { - FieldDescriptor other = builder.getOneofFieldDescriptor(field.getContainingOneof()); - throw new InvalidProtocolBufferException( - "Cannot set field " - + field.getFullName() - + " because another field " - + other.getFullName() - + " belonging to the same oneof has already been set "); - } } if (field.isRepeated() && json instanceof JsonNull) { // We allow "null" as value for all field types and treat it as if the @@ -1515,9 +1573,12 @@ public class JsonFormat { mergeMapField(field, json, builder); } else if (field.isRepeated()) { mergeRepeatedField(field, json, builder); + } else if (field.getContainingOneof() != null) { + mergeOneofField(field, json, builder); } else { Object value = parseFieldValue(field, json, builder); if (value != null) { + // A field interpreted as "null" is means it's treated as absent. builder.setField(field, value); } } @@ -1540,7 +1601,7 @@ public class JsonFormat { Object key = parseFieldValue(keyField, new JsonPrimitive(entry.getKey()), entryBuilder); Object value = parseFieldValue(valueField, entry.getValue(), entryBuilder); if (value == null) { - if(ignoringUnknownFields && valueField.getType() == Type.ENUM) { + if (ignoringUnknownFields && valueField.getType() == Type.ENUM) { continue; } else { throw new InvalidProtocolBufferException("Map value cannot be null."); @@ -1552,6 +1613,24 @@ public class JsonFormat { } } + private void mergeOneofField(FieldDescriptor field, JsonElement json, Message.Builder builder) + throws InvalidProtocolBufferException { + Object value = parseFieldValue(field, json, builder); + if (value == null) { + // A field interpreted as "null" is means it's treated as absent. + return; + } + if (builder.getOneofFieldDescriptor(field.getContainingOneof()) != null) { + throw new InvalidProtocolBufferException( + "Cannot set field " + + field.getFullName() + + " because another field " + + builder.getOneofFieldDescriptor(field.getContainingOneof()).getFullName() + + " belonging to the same oneof has already been set "); + } + builder.setField(field, value); + } + private void mergeRepeatedField( FieldDescriptor field, JsonElement json, Message.Builder builder) throws InvalidProtocolBufferException { @@ -1562,11 +1641,11 @@ public class JsonFormat { for (int i = 0; i < array.size(); ++i) { Object value = parseFieldValue(field, array.get(i), builder); if (value == null) { - if(ignoringUnknownFields && field.getType() == Type.ENUM) { + if (ignoringUnknownFields && field.getType() == Type.ENUM) { continue; } else { throw new InvalidProtocolBufferException( - "Repeated field elements cannot be null in field: " + field.getFullName()); + "Repeated field elements cannot be null in field: " + field.getFullName()); } } builder.addRepeatedField(field, value); diff --git a/java/util/src/main/java/com/google/protobuf/util/Structs.java b/java/util/src/main/java/com/google/protobuf/util/Structs.java new file mode 100755 index 0000000000..b1696ecdbc --- /dev/null +++ b/java/util/src/main/java/com/google/protobuf/util/Structs.java @@ -0,0 +1,65 @@ +// 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. + +package com.google.protobuf.util; + +import com.google.protobuf.Struct; +import com.google.protobuf.Value; + +/** Utilities to help create {@code google.protobuf.Struct} messages. */ +public final class Structs { + + /** + * Returns a struct containing the key-value pair. + */ + public static Struct of(String k1, Value v1) { + return Struct.newBuilder().putFields(k1, v1).build(); + } + + /** + * Returns a struct containing each of the key-value pairs. + * + *

Providing duplicate keys is undefined behavior. + */ + public static Struct of(String k1, Value v1, String k2, Value v2) { + return Struct.newBuilder().putFields(k1, v1).putFields(k2, v2).build(); + } + + /** + * Returns a struct containing each of the key-value pairs. + * + *

Providing duplicate keys is undefined behavior. + */ + public static Struct of(String k1, Value v1, String k2, Value v2, String k3, Value v3) { + return Struct.newBuilder().putFields(k1, v1).putFields(k2, v2).putFields(k3, v3).build(); + } + + private Structs() {} +} diff --git a/java/util/src/main/java/com/google/protobuf/util/TimeUtil.java b/java/util/src/main/java/com/google/protobuf/util/TimeUtil.java index 0475847349..4ffb410fbb 100644 --- a/java/util/src/main/java/com/google/protobuf/util/TimeUtil.java +++ b/java/util/src/main/java/com/google/protobuf/util/TimeUtil.java @@ -325,6 +325,7 @@ public final class TimeUtil { // Multiplications and divisions. // TODO(kak): Delete this. + @SuppressWarnings("DurationSecondsToDouble") public static Duration multiply(Duration duration, double times) { double result = duration.getSeconds() * times + duration.getNanos() * times / 1000000000.0; if (result < Long.MIN_VALUE || result > Long.MAX_VALUE) { diff --git a/java/util/src/main/java/com/google/protobuf/util/Timestamps.java b/java/util/src/main/java/com/google/protobuf/util/Timestamps.java index 9e528d4a71..0c19ad5eb5 100644 --- a/java/util/src/main/java/com/google/protobuf/util/Timestamps.java +++ b/java/util/src/main/java/com/google/protobuf/util/Timestamps.java @@ -36,6 +36,7 @@ import static com.google.common.math.LongMath.checkedAdd; import static com.google.common.math.LongMath.checkedMultiply; import static com.google.common.math.LongMath.checkedSubtract; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.protobuf.Duration; import com.google.protobuf.Timestamp; import java.text.ParseException; @@ -111,16 +112,17 @@ public final class Timestamps { }; /** - * Returns a {@link Comparator} for {@link Timestamp}s which sorts in increasing chronological - * order. Nulls and invalid {@link Timestamp}s are not allowed (see {@link #isValid}). + * Returns a {@link Comparator} for {@link Timestamp Timestamps} which sorts in increasing + * chronological order. Nulls and invalid {@link Timestamp Timestamps} are not allowed (see + * {@link #isValid}). */ public static Comparator comparator() { return COMPARATOR; } /** - * Compares two timestamps. The value returned is identical to what would be returned by: - * {@code Timestamps.comparator().compare(x, y)}. + * Compares two timestamps. The value returned is identical to what would be returned by: {@code + * Timestamps.comparator().compare(x, y)}. * * @return the value {@code 0} if {@code x == y}; a value less than {@code 0} if {@code x < y}; * and a value greater than {@code 0} if {@code x > y} @@ -150,6 +152,7 @@ public final class Timestamps { *

Note: Negative second values with fractional seconds must still have non-negative * nanos values that count forward in time. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static boolean isValid(long seconds, int nanos) { if (seconds < TIMESTAMP_SECONDS_MIN || seconds > TIMESTAMP_SECONDS_MAX) { return false; @@ -161,18 +164,31 @@ public final class Timestamps { } /** Throws an {@link IllegalArgumentException} if the given {@link Timestamp} is not valid. */ + @CanIgnoreReturnValue public static Timestamp checkValid(Timestamp timestamp) { long seconds = timestamp.getSeconds(); int nanos = timestamp.getNanos(); if (!isValid(seconds, nanos)) { - throw new IllegalArgumentException(String.format( - "Timestamp is not valid. See proto definition for valid values. " - + "Seconds (%s) must be in range [-62,135,596,800, +253,402,300,799]. " - + "Nanos (%s) must be in range [0, +999,999,999].", seconds, nanos)); + throw new IllegalArgumentException( + String.format( + "Timestamp is not valid. See proto definition for valid values. " + + "Seconds (%s) must be in range [-62,135,596,800, +253,402,300,799]. " + + "Nanos (%s) must be in range [0, +999,999,999].", + seconds, nanos)); } return timestamp; } + /** + * Builds the given builder and throws an {@link IllegalArgumentException} if it is not valid. See + * {@link #checkValid(Timestamp)}. + * + * @return A valid, built {@link Timestamp}. + */ + public static Timestamp checkValid(Timestamp.Builder timestampBuilder) { + return checkValid(timestampBuilder.build()); + } + /** * Convert Timestamp to RFC 3339 date string format. The output will always be Z-normalized and * uses 3, 6 or 9 fractional digits as required to represent the exact value. Note that Timestamp @@ -266,6 +282,7 @@ public final class Timestamps { } /** Create a Timestamp from the number of seconds elapsed from the epoch. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static Timestamp fromSeconds(long seconds) { return normalizedTimestamp(seconds, 0); } @@ -276,11 +293,13 @@ public final class Timestamps { *

The result will be rounded down to the nearest second. E.g., if the timestamp represents * "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 second. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static long toSeconds(Timestamp timestamp) { return checkValid(timestamp).getSeconds(); } /** Create a Timestamp from the number of milliseconds elapsed from the epoch. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static Timestamp fromMillis(long milliseconds) { return normalizedTimestamp( milliseconds / MILLIS_PER_SECOND, @@ -293,6 +312,7 @@ public final class Timestamps { *

The result will be rounded down to the nearest millisecond. E.g., if the timestamp * represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 millisecond. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static long toMillis(Timestamp timestamp) { checkValid(timestamp); return checkedAdd( @@ -301,6 +321,7 @@ public final class Timestamps { } /** Create a Timestamp from the number of microseconds elapsed from the epoch. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static Timestamp fromMicros(long microseconds) { return normalizedTimestamp( microseconds / MICROS_PER_SECOND, @@ -313,6 +334,7 @@ public final class Timestamps { *

The result will be rounded down to the nearest microsecond. E.g., if the timestamp * represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 microsecond. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static long toMicros(Timestamp timestamp) { checkValid(timestamp); return checkedAdd( @@ -321,12 +343,14 @@ public final class Timestamps { } /** Create a Timestamp from the number of nanoseconds elapsed from the epoch. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static Timestamp fromNanos(long nanoseconds) { return normalizedTimestamp( nanoseconds / NANOS_PER_SECOND, (int) (nanoseconds % NANOS_PER_SECOND)); } /** Convert a Timestamp to the number of nanoseconds elapsed from the epoch. */ + @SuppressWarnings("GoodTime") // this is a legacy conversion API public static long toNanos(Timestamp timestamp) { checkValid(timestamp); return checkedAdd( diff --git a/java/util/src/main/java/com/google/protobuf/util/Values.java b/java/util/src/main/java/com/google/protobuf/util/Values.java new file mode 100755 index 0000000000..b3ade2ddfb --- /dev/null +++ b/java/util/src/main/java/com/google/protobuf/util/Values.java @@ -0,0 +1,85 @@ +// 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. + +package com.google.protobuf.util; + +import com.google.protobuf.ListValue; +import com.google.protobuf.NullValue; +import com.google.protobuf.Struct; +import com.google.protobuf.Value; + +/** Utilities to help create {@code google.protobuf.Value} messages. */ +public final class Values { + + private static final Value NULL_VALUE = + Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(); + + public static Value ofNull() { + return NULL_VALUE; + } + + /** Returns a Value object with number set to value. */ + public static Value of(boolean value) { + return Value.newBuilder().setBoolValue(value).build(); + } + + /** Returns a Value object with number set to value. */ + public static Value of(double value) { + return Value.newBuilder().setNumberValue(value).build(); + } + + /** Returns a Value object with string set to value. */ + public static Value of(String value) { + return Value.newBuilder().setStringValue(value).build(); + } + + /** Returns a Value object with struct set to value. */ + public static Value of(Struct value) { + return Value.newBuilder().setStructValue(value).build(); + } + + /** Returns a Value with ListValue set to value. */ + public static Value of(ListValue value) { + return Value.newBuilder().setListValue(value).build(); + } + + /** + * Returns a Value with ListValue set to the appending the result of calling {@link #of(Object)} + * on each element in the iterable. + */ + public static Value of(Iterable values) { + Value.Builder valueBuilder = Value.newBuilder(); + ListValue.Builder listValue = valueBuilder.getListValueBuilder(); + listValue.addAllValues(values); + return valueBuilder.build(); + } + + private Values() {} +} diff --git a/java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java b/java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java index a0d317d44b..b7addab936 100644 --- a/java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java @@ -95,7 +95,7 @@ public class FieldMaskTreeTest extends TestCase { tree.intersectFieldPath("bar", result); assertEquals("bar.baz,bar.quz,foo", result.toString()); } - + public void testMerge() throws Exception { testMergeImpl(true); testMergeImpl(false); diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java index 7637c267f4..04080eab42 100644 --- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java @@ -34,6 +34,7 @@ import com.google.protobuf.Any; import com.google.protobuf.BoolValue; import com.google.protobuf.ByteString; import com.google.protobuf.BytesValue; +import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.DoubleValue; import com.google.protobuf.FloatValue; @@ -49,19 +50,20 @@ import com.google.protobuf.UInt32Value; import com.google.protobuf.UInt64Value; import com.google.protobuf.Value; import com.google.protobuf.util.JsonFormat.TypeRegistry; -import com.google.protobuf.util.JsonTestProto.TestAllTypes; -import com.google.protobuf.util.JsonTestProto.TestAllTypes.NestedEnum; -import com.google.protobuf.util.JsonTestProto.TestAllTypes.NestedMessage; -import com.google.protobuf.util.JsonTestProto.TestAny; -import com.google.protobuf.util.JsonTestProto.TestCustomJsonName; -import com.google.protobuf.util.JsonTestProto.TestDuration; -import com.google.protobuf.util.JsonTestProto.TestFieldMask; -import com.google.protobuf.util.JsonTestProto.TestMap; -import com.google.protobuf.util.JsonTestProto.TestOneof; -import com.google.protobuf.util.JsonTestProto.TestRecursive; -import com.google.protobuf.util.JsonTestProto.TestStruct; -import com.google.protobuf.util.JsonTestProto.TestTimestamp; -import com.google.protobuf.util.JsonTestProto.TestWrappers; +import com.google.protobuf.util.proto.JsonTestProto.TestAllTypes; +import com.google.protobuf.util.proto.JsonTestProto.TestAllTypes.AliasedEnum; +import com.google.protobuf.util.proto.JsonTestProto.TestAllTypes.NestedEnum; +import com.google.protobuf.util.proto.JsonTestProto.TestAllTypes.NestedMessage; +import com.google.protobuf.util.proto.JsonTestProto.TestAny; +import com.google.protobuf.util.proto.JsonTestProto.TestCustomJsonName; +import com.google.protobuf.util.proto.JsonTestProto.TestDuration; +import com.google.protobuf.util.proto.JsonTestProto.TestFieldMask; +import com.google.protobuf.util.proto.JsonTestProto.TestMap; +import com.google.protobuf.util.proto.JsonTestProto.TestOneof; +import com.google.protobuf.util.proto.JsonTestProto.TestRecursive; +import com.google.protobuf.util.proto.JsonTestProto.TestStruct; +import com.google.protobuf.util.proto.JsonTestProto.TestTimestamp; +import com.google.protobuf.util.proto.JsonTestProto.TestWrappers; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -156,12 +158,16 @@ public class JsonFormatTest extends TestCase { private String toCompactJsonString(Message message) throws IOException { return JsonFormat.printer().omittingInsignificantWhitespace().print(message); } + private String toSortedJsonString(Message message) throws IOException { + return JsonFormat.printer().sortingMapKeys().print(message); + } private void mergeFromJson(String json, Message.Builder builder) throws IOException { JsonFormat.parser().merge(json, builder); } - private void mergeFromJsonIgnoringUnknownFields(String json, Message.Builder builder) throws IOException { + private void mergeFromJsonIgnoringUnknownFields(String json, Message.Builder builder) + throws IOException { JsonFormat.parser().ignoringUnknownFields().merge(json, builder); } @@ -458,6 +464,20 @@ public class JsonFormatTest extends TestCase { assertEquals(NullValue.NULL_VALUE, message.getOneofNullValue()); } + public void testNullFirstInDuplicateOneof() throws Exception { + TestOneof.Builder builder = TestOneof.newBuilder(); + mergeFromJson("{\"oneofNestedMessage\": null, \"oneofInt32\": 1}", builder); + TestOneof message = builder.build(); + assertEquals(1, message.getOneofInt32()); + } + + public void testNullLastInDuplicateOneof() throws Exception { + TestOneof.Builder builder = TestOneof.newBuilder(); + mergeFromJson("{\"oneofInt32\": 1, \"oneofNestedMessage\": null}", builder); + TestOneof message = builder.build(); + assertEquals(1, message.getOneofInt32()); + } + public void testParserRejectDuplicatedFields() throws Exception { // TODO(xiaofeng): The parser we are currently using (GSON) will accept and keep the last // one if multiple entries have the same name. This is not the desired behavior but it can @@ -672,22 +692,19 @@ public class JsonFormatTest extends TestCase { + "}", builder); fail(); - + } catch (InvalidProtocolBufferException e) { // Exception expected. } } - + public void testMapEnumNullValueIsIgnored() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); mergeFromJsonIgnoringUnknownFields( - "{\n" - + " \"int32ToEnumMap\": {\"1\": null}\n" - + "}", - builder); - TestMap map = builder.build(); - assertEquals(0, map.getInt32ToEnumMapMap().entrySet().size()); - } + "{\n" + " \"int32ToEnumMap\": {\"1\": null}\n" + "}", builder); + TestMap map = builder.build(); + assertEquals(0, map.getInt32ToEnumMapMap().size()); + } public void testParserAcceptNonQuotedObjectKey() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); @@ -832,6 +849,7 @@ public class JsonFormatTest extends TestCase { assertRoundTripEquals(message); } + public void testAnyFields() throws Exception { TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build(); TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build(); @@ -1158,8 +1176,8 @@ public class JsonFormatTest extends TestCase { } public void testParserAcceptBase64Variants() throws Exception { - assertAccepts("optionalBytes", "AQI"); // No padding - assertAccepts("optionalBytes", "-_w"); // base64Url, no padding + assertAccepts("optionalBytes", "AQI"); // No padding + assertAccepts("optionalBytes", "-_w"); // base64Url, no padding } public void testParserRejectInvalidEnumValue() throws Exception { @@ -1188,40 +1206,53 @@ public class JsonFormatTest extends TestCase { String json = "{\n" + " \"unknownField\": \"XXX\"\n" + "}"; JsonFormat.parser().ignoringUnknownFields().merge(json, builder); } - + public void testParserIgnoringUnknownEnums() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); String json = "{\n" + " \"optionalNestedEnum\": \"XXX\"\n" + "}"; JsonFormat.parser().ignoringUnknownFields().merge(json, builder); assertEquals(0, builder.getOptionalNestedEnumValue()); } - + + public void testParserSupportAliasEnums() throws Exception { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + String json = "{\n" + " \"optionalAliasedEnum\": \"QUX\"\n" + "}"; + JsonFormat.parser().merge(json, builder); + assertEquals(AliasedEnum.ALIAS_BAZ, builder.getOptionalAliasedEnum()); + + builder = TestAllTypes.newBuilder(); + json = "{\n" + " \"optionalAliasedEnum\": \"qux\"\n" + "}"; + JsonFormat.parser().merge(json, builder); + assertEquals(AliasedEnum.ALIAS_BAZ, builder.getOptionalAliasedEnum()); + + builder = TestAllTypes.newBuilder(); + json = "{\n" + " \"optionalAliasedEnum\": \"bAz\"\n" + "}"; + JsonFormat.parser().merge(json, builder); + assertEquals(AliasedEnum.ALIAS_BAZ, builder.getOptionalAliasedEnum()); + } + public void testUnknownEnumMap() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - JsonFormat.parser().ignoringUnknownFields().merge( - "{\n" - + " \"int32ToEnumMap\": {1: XXX, 2: FOO}" - + "}", - builder); - - assertEquals(NestedEnum.FOO, builder.getInt32ToEnumMapMap().get(2)); - assertEquals(1, builder.getInt32ToEnumMapMap().size()); + JsonFormat.parser() + .ignoringUnknownFields() + .merge("{\n" + " \"int32ToEnumMap\": {1: XXX, 2: FOO}" + "}", builder); + + assertEquals(NestedEnum.FOO, builder.getInt32ToEnumMapMap().get(2)); + assertEquals(1, builder.getInt32ToEnumMapMap().size()); } - + public void testRepeatedUnknownEnum() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); - JsonFormat.parser().ignoringUnknownFields().merge( - "{\n" - + " \"repeatedNestedEnum\": [XXX, FOO, BAR, BAZ]" - + "}", - builder); - + JsonFormat.parser() + .ignoringUnknownFields() + .merge("{\n" + " \"repeatedNestedEnum\": [XXX, FOO, BAR, BAZ]" + "}", builder); + assertEquals(NestedEnum.FOO, builder.getRepeatedNestedEnum(0)); assertEquals(NestedEnum.BAR, builder.getRepeatedNestedEnum(1)); assertEquals(NestedEnum.BAZ, builder.getRepeatedNestedEnum(2)); assertEquals(3, builder.getRepeatedNestedEnumList().size()); } - + public void testParserIntegerEnumValue() throws Exception { TestAllTypes.Builder actualBuilder = TestAllTypes.newBuilder(); mergeFromJson("{\n" + " \"optionalNestedEnum\": 2\n" + "}", actualBuilder); @@ -1283,7 +1314,8 @@ public class JsonFormatTest extends TestCase { + " \"repeatedString\": [],\n" + " \"repeatedBytes\": [],\n" + " \"repeatedNestedMessage\": [],\n" - + " \"repeatedNestedEnum\": []\n" + + " \"repeatedNestedEnum\": [],\n" + + " \"optionalAliasedEnum\": \"ALIAS_FOO\"\n" + "}", JsonFormat.printer().includingDefaultValueFields().print(message)); @@ -1641,4 +1673,50 @@ public class JsonFormatTest extends TestCase { // Expected. } } + + public void testSortedMapKeys() throws Exception { + TestMap.Builder mapBuilder = TestMap.newBuilder(); + mapBuilder.putStringToInt32Map("\ud834\udd20", 3); // utf-8 F0 9D 84 A0 + mapBuilder.putStringToInt32Map("foo", 99); + mapBuilder.putStringToInt32Map("xxx", 123); + mapBuilder.putStringToInt32Map("\u20ac", 1); // utf-8 E2 82 AC + mapBuilder.putStringToInt32Map("abc", 20); + mapBuilder.putStringToInt32Map("19", 19); + mapBuilder.putStringToInt32Map("8", 8); + mapBuilder.putStringToInt32Map("\ufb00", 2); // utf-8 EF AC 80 + mapBuilder.putInt32ToInt32Map(3, 3); + mapBuilder.putInt32ToInt32Map(10, 10); + mapBuilder.putInt32ToInt32Map(5, 5); + mapBuilder.putInt32ToInt32Map(4, 4); + mapBuilder.putInt32ToInt32Map(1, 1); + mapBuilder.putInt32ToInt32Map(2, 2); + mapBuilder.putInt32ToInt32Map(-3, -3); + TestMap mapMessage = mapBuilder.build(); + assertEquals( + "{\n" + + " \"int32ToInt32Map\": {\n" + + " \"-3\": -3,\n" + + " \"1\": 1,\n" + + " \"2\": 2,\n" + + " \"3\": 3,\n" + + " \"4\": 4,\n" + + " \"5\": 5,\n" + + " \"10\": 10\n" + + " },\n" + + " \"stringToInt32Map\": {\n" + + " \"19\": 19,\n" + + " \"8\": 8,\n" + + " \"abc\": 20,\n" + + " \"foo\": 99,\n" + + " \"xxx\": 123,\n" + + " \"\u20ac\": 1,\n" + + " \"\ufb00\": 2,\n" + + " \"\ud834\udd20\": 3\n" + + " }\n" + + "}", + toSortedJsonString(mapMessage)); + + TestMap emptyMap = TestMap.getDefaultInstance(); + assertEquals("{\n}", toSortedJsonString(emptyMap)); + } } diff --git a/java/util/src/test/java/com/google/protobuf/util/StructsTest.java b/java/util/src/test/java/com/google/protobuf/util/StructsTest.java new file mode 100755 index 0000000000..265aff5776 --- /dev/null +++ b/java/util/src/test/java/com/google/protobuf/util/StructsTest.java @@ -0,0 +1,59 @@ +// 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. + +package com.google.protobuf.util; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.protobuf.Struct; +import junit.framework.TestCase; + +public final class StructsTest extends TestCase { + + public void test1pair_constructsObject() throws Exception { + Struct.Builder expected = Struct.newBuilder(); + JsonFormat.parser().merge("{\"k1\": 1}", expected); + assertThat(Structs.of("k1", Values.of(1))).isEqualTo(expected.build()); + } + + public void test2pair_constructsObject() throws Exception { + Struct.Builder expected = Struct.newBuilder(); + JsonFormat.parser().merge("{\"k1\": 1, \"k2\": 2}", expected); + assertThat(Structs.of("k1", Values.of(1), "k2", Values.of(2))).isEqualTo(expected.build()); + } + + public void test3pair_constructsObject() throws Exception { + Struct.Builder expected = Struct.newBuilder(); + JsonFormat.parser().merge("{\"k1\": 1, \"k2\": 2, \"k3\": 3}", expected); + assertThat(Structs.of("k1", Values.of(1), "k2", Values.of(2), "k3", Values.of(3))) + .isEqualTo(expected.build()); + } + +} diff --git a/java/util/src/test/java/com/google/protobuf/util/ValuesTest.java b/java/util/src/test/java/com/google/protobuf/util/ValuesTest.java new file mode 100755 index 0000000000..d646e97282 --- /dev/null +++ b/java/util/src/test/java/com/google/protobuf/util/ValuesTest.java @@ -0,0 +1,102 @@ +// 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. + +package com.google.protobuf.util; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.protobuf.ListValue; +import com.google.protobuf.NullValue; +import com.google.protobuf.Struct; +import com.google.protobuf.Value; +import java.util.ArrayList; +import java.util.List; +import junit.framework.TestCase; + +public final class ValuesTest extends TestCase { + public void testOfNull_IsNullValue() throws Exception { + assertThat(Values.ofNull()) + .isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build()); + } + + public void testOfBoolean_ConstructsValue() { + assertThat(Values.of(true)).isEqualTo(Value.newBuilder().setBoolValue(true).build()); + assertThat(Values.of(false)).isEqualTo(Value.newBuilder().setBoolValue(false).build()); + } + + public void testOfNumeric_ConstructsValue() { + assertThat(Values.of(100)).isEqualTo(Value.newBuilder().setNumberValue(100).build()); + assertThat(Values.of(1000L)).isEqualTo(Value.newBuilder().setNumberValue(1000).build()); + assertThat(Values.of(1000.23f)).isEqualTo(Value.newBuilder().setNumberValue(1000.23f).build()); + assertThat(Values.of(10000.23)).isEqualTo(Value.newBuilder().setNumberValue(10000.23).build()); + } + + public void testOfString_ConstructsValue() { + assertThat(Values.of("")).isEqualTo(Value.newBuilder().setStringValue("").build()); + assertThat(Values.of("foo")).isEqualTo(Value.newBuilder().setStringValue("foo").build()); + } + + public void testOfStruct_ConstructsValue() { + Struct.Builder builder = Struct.newBuilder(); + builder.putFields("a", Values.of("a")); + builder.putFields("b", Values.of("b")); + + assertThat(Values.of(builder.build())) + .isEqualTo(Value.newBuilder().setStructValue(builder.build()).build()); + } + + public void testOfListValue_ConstructsInstance() { + ListValue.Builder builder = ListValue.newBuilder(); + builder.addValues(Values.of(1)); + builder.addValues(Values.of(2)); + + assertThat(Values.of(builder.build())) + .isEqualTo(Value.newBuilder().setListValue(builder.build()).build()); + } + + public void testOfIterable_ReturnsTheValue() { + ListValue.Builder builder = ListValue.newBuilder(); + builder.addValues(Values.of(1)); + builder.addValues(Values.of(2)); + builder.addValues(Values.of(true)); + builder.addValues(Value.newBuilder().setListValue(builder.build()).build()); + + List list = new ArrayList<>(); + list.add(Values.of(1)); + list.add(Values.of(2)); + list.add(Values.of(true)); + List copyList = new ArrayList<>(list); + list.add(Values.of(copyList)); + + assertThat(Values.of(list)).isEqualTo(Value.newBuilder().setListValue(builder).build()); + assertThat(Values.of(new ArrayList())) + .isEqualTo(Value.newBuilder().setListValue(ListValue.getDefaultInstance()).build()); + } +} diff --git a/java/util/src/test/proto/com/google/protobuf/util/json_test.proto b/java/util/src/test/proto/com/google/protobuf/util/json_test.proto index d1248cfb71..ba4fe03860 100644 --- a/java/util/src/test/proto/com/google/protobuf/util/json_test.proto +++ b/java/util/src/test/proto/com/google/protobuf/util/json_test.proto @@ -32,15 +32,15 @@ syntax = "proto3"; package json_test; -option java_package = "com.google.protobuf.util"; -option java_outer_classname = "JsonTestProto"; - import "google/protobuf/any.proto"; -import "google/protobuf/wrappers.proto"; -import "google/protobuf/timestamp.proto"; import "google/protobuf/duration.proto"; import "google/protobuf/field_mask.proto"; import "google/protobuf/struct.proto"; +import "google/protobuf/timestamp.proto"; +import "google/protobuf/wrappers.proto"; + +option java_package = "com.google.protobuf.util.proto"; +option java_outer_classname = "JsonTestProto"; message TestAllTypes { enum NestedEnum { @@ -48,6 +48,17 @@ message TestAllTypes { BAR = 1; BAZ = 2; } + + enum AliasedEnum { + option allow_alias = true; + + ALIAS_FOO = 0; + ALIAS_BAR = 1; + ALIAS_BAZ = 2; + QUX = 2; + qux = 2; + bAz = 2; + } message NestedMessage { int32 value = 1; } @@ -69,6 +80,7 @@ message TestAllTypes { bytes optional_bytes = 15; NestedMessage optional_nested_message = 18; NestedEnum optional_nested_enum = 21; + AliasedEnum optional_aliased_enum = 52; // Repeated repeated int32 repeated_int32 = 31; diff --git a/js/README.md b/js/README.md index 97fa7e29d5..3635028fd5 100644 --- a/js/README.md +++ b/js/README.md @@ -19,13 +19,13 @@ resolve imports at compile time. To use Protocol Buffers with JavaScript, you need two main components: 1. The protobuf runtime library. You can install this with - `npm install google-protobuf`, or use the files in this directory. - If npm is not being used, as of 3.3.0, the files needed are located in binary subdirectory; + `npm install google-protobuf`, or use the files in this directory. + If npm is not being used, as of 3.3.0, the files needed are located in binary subdirectory; arith.js, constants.js, decoder.js, encoder.js, map.js, message.js, reader.js, utils.js, writer.js 2. The Protocol Compiler `protoc`. This translates `.proto` files into `.js` files. The compiler is not currently available via npm, but you can download a pre-built binary - [on GitHub](https://github.com/google/protobuf/releases) + [on GitHub](https://github.com/protocolbuffers/protobuf/releases) (look for the `protoc-*.zip` files under **Downloads**). @@ -33,11 +33,11 @@ Setup ===== First, obtain the Protocol Compiler. The easiest way is to download -a pre-built binary from [https://github.com/google/protobuf/releases](https://github.com/google/protobuf/releases). +a pre-built binary from [https://github.com/protocolbuffers/protobuf/releases](https://github.com/protocolbuffers/protobuf/releases). If you want, you can compile `protoc` from source instead. To do this follow the instructions in [the top-level -README](https://github.com/google/protobuf/blob/master/src/README.md). +README](https://github.com/protocolbuffers/protobuf/blob/master/src/README.md). Once you have `protoc` compiled, you can run the tests by typing: @@ -135,7 +135,7 @@ The syntax of the `--js_out` flag is: Where `OPTIONS` are separated by commas. Options are either `opt=val` or just `opt` (for options that don't take a value). The available options are specified and documented in the `GeneratorOptions` struct in -[src/google/protobuf/compiler/js/js_generator.h](https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/js/js_generator.h#L53). +[src/google/protobuf/compiler/js/js_generator.h](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/js/js_generator.h#L53). Some examples: diff --git a/js/binary/constants.js b/js/binary/constants.js index 5c53a3605d..f8b13a8b9e 100644 --- a/js/binary/constants.js +++ b/js/binary/constants.js @@ -55,7 +55,7 @@ goog.forwardDeclare('jspb.BinaryMessage'); goog.forwardDeclare('jspb.BinaryReader'); goog.forwardDeclare('jspb.BinaryWriter'); goog.forwardDeclare('jspb.Message'); -goog.forwardDeclare('jsproto.BinaryExtension'); +goog.forwardDeclare('jsprotolib.BinaryExtension'); @@ -122,7 +122,7 @@ jspb.RepeatedFieldType; !Uint8Array| !jspb.ConstBinaryMessage| !jspb.BinaryMessage| - !jsproto.BinaryExtension} + !jsprotolib.BinaryExtension} */ jspb.AnyFieldType; diff --git a/js/binary/decoder.js b/js/binary/decoder.js index e33bf1be9c..d47c20e4af 100644 --- a/js/binary/decoder.js +++ b/js/binary/decoder.js @@ -63,7 +63,7 @@ goog.require('jspb.utils'); * @struct */ jspb.BinaryIterator = function(opt_decoder, opt_next, opt_elements) { - /** @private {jspb.BinaryDecoder} */ + /** @private {?jspb.BinaryDecoder} */ this.decoder_ = null; /** diff --git a/js/binary/decoder_test.js b/js/binary/decoder_test.js index b19e1d1b27..15a71d74d1 100644 --- a/js/binary/decoder_test.js +++ b/js/binary/decoder_test.js @@ -255,7 +255,6 @@ describe('binaryDecoderTest', function() { /** * Verifies that misuse of the decoder class triggers assertions. - * @suppress {checkTypes|visibility} */ it('testDecodeErrors', function() { // Reading a value past the end of the stream should trigger an assertion. diff --git a/js/binary/proto_test.js b/js/binary/proto_test.js index f5e1b6bbf0..ae31c8d1ce 100644 --- a/js/binary/proto_test.js +++ b/js/binary/proto_test.js @@ -659,5 +659,7 @@ describe('protoBinaryTest', function() { 'jspb.test.TestAllTypes'); checkAllFields(msg, msg2); + }); + }); diff --git a/js/binary/reader.js b/js/binary/reader.js index 731587c7e1..91c7b6dacf 100644 --- a/js/binary/reader.js +++ b/js/binary/reader.js @@ -97,7 +97,7 @@ jspb.BinaryReader = function(opt_bytes, opt_start, opt_length) { /** * User-defined reader callbacks. - * @private {Object} + * @private {?Object} */ this.readCallbacks_ = null; }; diff --git a/js/binary/writer.js b/js/binary/writer.js index 287d29c3f3..ae1c29b822 100644 --- a/js/binary/writer.js +++ b/js/binary/writer.js @@ -799,6 +799,38 @@ jspb.BinaryWriter.prototype.writeMessage = function( }; +/** + * Writes a message set extension to the buffer. + * @param {number} field The field number for the extension. + * @param {?MessageType} value The extension message object to write. Note that + * message set can only have extensions with type of optional message. + * @param {function(!MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback + * Will be invoked with the value to write and the writer to write it with. + * @template MessageType + * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace + * the null in blah|null with none. This is necessary because the compiler will + * infer MessageType to be nullable if the value parameter is nullable. + * @template MessageTypeNonNull := + * cond(isUnknown(MessageType), unknown(), + * mapunion(MessageType, (X) => + * cond(eq(X, 'null'), none(), X))) + * =: + */ +jspb.BinaryWriter.prototype.writeMessageSet = function( + field, value, writerCallback) { + if (value == null) return; + // The wire format for a message set is defined by + // google3/net/proto/message_set.proto + this.writeFieldHeader_(1, jspb.BinaryConstants.WireType.START_GROUP); + this.writeFieldHeader_(2, jspb.BinaryConstants.WireType.VARINT); + this.encoder_.writeSignedVarint32(field); + var bookmark = this.beginDelimited_(3); + writerCallback(value, this); + this.endDelimited_(bookmark); + this.writeFieldHeader_(1, jspb.BinaryConstants.WireType.END_GROUP); +}; + + /** * Writes a group message to the buffer. * diff --git a/js/compatibility_tests/v3.1.0/message_test.js b/js/compatibility_tests/v3.1.0/message_test.js index c1023784fa..d5c73742b6 100644 --- a/js/compatibility_tests/v3.1.0/message_test.js +++ b/js/compatibility_tests/v3.1.0/message_test.js @@ -102,14 +102,14 @@ describe('Message test suite', function() { }); it('testComplexConversion', function() { - var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1]; - var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1]; + var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, true]; + var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, true]; var foo = new proto.jspb.test.Complex(data1); var bar = new proto.jspb.test.Complex(data2); var result = foo.toObject(); assertObjectEquals({ aString: 'a', - anOutOfOrderBool: 1, + anOutOfOrderBool: true, aNestedMessage: { anInt: 11 }, @@ -121,7 +121,7 @@ describe('Message test suite', function() { result = foo.toObject(true /* opt_includeInstance */); assertObjectEquals({ aString: 'a', - anOutOfOrderBool: 1, + anOutOfOrderBool: true, aNestedMessage: { anInt: 11, $jspbMessageInstance: foo.getANestedMessage() diff --git a/js/data.proto b/js/data.proto index 74a8a994c7..ca815caf61 100644 --- a/js/data.proto +++ b/js/data.proto @@ -32,11 +32,11 @@ syntax = "proto2"; +package jspb.test; + option java_package = "com.google.apps.jspb.proto"; option java_multiple_files = true; -package jspb.test; - // legacy data, must be nested message data { message NestedData { @@ -48,4 +48,3 @@ message data { message UnnestedData { required string str = 1; } - diff --git a/js/debug.js b/js/debug.js index ba51bbe01c..9655b2e34c 100644 --- a/js/debug.js +++ b/js/debug.js @@ -37,6 +37,7 @@ goog.provide('jspb.debug'); goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.object'); +goog.require('jspb.Map'); goog.require('jspb.Message'); @@ -90,6 +91,16 @@ jspb.debug.dump_ = function(thing) { goog.asserts.assertArray(thing); return goog.array.map(thing, jspb.debug.dump_); } + + if (message instanceof jspb.Map) { + var mapObject = {}; + var entries = message.entries(); + for (var entry = entries.next(); !entry.done; entry = entries.next()) { + mapObject[entry.value[0]] = jspb.debug.dump_(entry.value[1]); + } + return mapObject; + } + goog.asserts.assert(message instanceof jspb.Message, 'Only messages expected: ' + thing); var ctor = message.constructor; diff --git a/js/debug_test.js b/js/debug_test.js index d0d646a5f8..5096869962 100644 --- a/js/debug_test.js +++ b/js/debug_test.js @@ -38,7 +38,9 @@ goog.require('jspb.debug'); // CommonJS-LoadFromFile: test_pb goog.require('proto.jspb.test.HasExtensions'); goog.require('proto.jspb.test.IsExtension'); +goog.require('proto.jspb.test.MapValueMessageNoBinary'); goog.require('proto.jspb.test.Simple1'); +goog.require('proto.jspb.test.TestMapFieldsNoBinary'); // CommonJS-LoadFromFile: testbinary_pb @@ -113,4 +115,74 @@ describe('debugTest', function() { }, jspb.debug.dump(extendable)); }); + it('testMapsBasicTypes', function() { + if (COMPILED) { + return; + } + + var message = new proto.jspb.test.TestMapFieldsNoBinary(); + message.getMapBoolStringMap().set(true, 'bool_string_value1'); + message.getMapBoolStringMap().set(false, 'bool_string_value2'); + message.getMapStringInt32Map().set('key', 111); + + assertObjectEquals({ + '$name': 'proto.jspb.test.TestMapFieldsNoBinary', + 'mapBoolStringMap': { + true: 'bool_string_value1', + false: 'bool_string_value2' + }, + 'mapInt32StringMap': {}, + 'mapInt64StringMap': {}, + 'mapStringBoolMap': {}, + 'mapStringDoubleMap': {}, + 'mapStringEnumMap': {}, + 'mapStringInt32Map': { + 'key': 111 + }, + 'mapStringInt64Map': {}, + 'mapStringMsgMap': {}, + 'mapStringStringMap': {}, + 'mapStringTestmapfieldsMap': {} + }, jspb.debug.dump(message)); + }); + + it('testMapsMessageValues', function() { + if (COMPILED) { + return; + } + + var value1 = new proto.jspb.test.MapValueMessageNoBinary(); + value1.setFoo(1111); + var value2 = new proto.jspb.test.MapValueMessageNoBinary(); + value2.setFoo(2222); + + var message = new proto.jspb.test.TestMapFieldsNoBinary(); + message.getMapStringMsgMap().set('key1', value1); + message.getMapStringMsgMap().set('key2', value2); + + assertObjectEquals({ + '$name': 'proto.jspb.test.TestMapFieldsNoBinary', + 'mapBoolStringMap': {}, + 'mapInt32StringMap': {}, + 'mapInt64StringMap': {}, + 'mapStringBoolMap': {}, + 'mapStringDoubleMap': {}, + 'mapStringEnumMap': {}, + 'mapStringInt32Map': {}, + 'mapStringInt64Map': {}, + 'mapStringMsgMap': { + 'key1': { + '$name': 'proto.jspb.test.MapValueMessageNoBinary', + 'foo': 1111 + }, + 'key2': { + '$name': 'proto.jspb.test.MapValueMessageNoBinary', + 'foo': 2222 + } + }, + 'mapStringStringMap': {}, + 'mapStringTestmapfieldsMap': {} + }, jspb.debug.dump(message)); + }); + }); diff --git a/js/gulpfile.js b/js/gulpfile.js index 709c5cf9eb..1f0946caf0 100644 --- a/js/gulpfile.js +++ b/js/gulpfile.js @@ -29,6 +29,11 @@ var group1Protos = [ 'test5.proto', 'commonjs/test6/test6.proto', 'test8.proto', + 'test11.proto', + 'test12.proto', + 'test13.proto', + 'test14.proto', + 'test15.proto', 'testbinary.proto', 'testempty.proto', 'test.proto', @@ -132,14 +137,14 @@ function getClosureBuilderCommand(exportsFile, outputFile) { return './node_modules/google-closure-library/closure/bin/build/closurebuilder.py ' + '--root node_modules ' + '-o compiled ' + - '--compiler_jar node_modules/google-closure-compiler/compiler.jar ' + + '--compiler_jar node_modules/google-closure-compiler-java/compiler.jar ' + '-i ' + exportsFile + ' ' + 'map.js message.js binary/arith.js binary/constants.js binary/decoder.js ' + 'binary/encoder.js binary/reader.js binary/utils.js binary/writer.js ' + exportsFile + ' > ' + outputFile; } -gulp.task('dist', ['genproto_wellknowntypes'], function (cb) { +gulp.task('dist', gulp.series(['genproto_wellknowntypes'], function(cb) { // TODO(haberman): minify this more aggressively. // Will require proper externs/exports. exec(getClosureBuilderCommand('commonjs/export.js', 'google-protobuf.js'), @@ -148,7 +153,7 @@ gulp.task('dist', ['genproto_wellknowntypes'], function (cb) { console.log(stderr); cb(err); }); -}); +})); gulp.task('commonjs_asserts', function (cb) { exec('mkdir -p commonjs_out/test_node_modules && ' + @@ -174,57 +179,83 @@ gulp.task('commonjs_testdeps', function (cb) { }); }); -gulp.task('make_commonjs_out', ['dist', 'genproto_well_known_types_commonjs', 'genproto_group1_commonjs', 'genproto_group2_commonjs', 'genproto_commonjs_wellknowntypes', 'commonjs_asserts', 'commonjs_testdeps', 'genproto_group3_commonjs_strict'], function (cb) { - // TODO(haberman): minify this more aggressively. - // Will require proper externs/exports. - var cmd = "mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && "; - function addTestFile(file) { - cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file + - ' > commonjs_out/' + file + '&& '; - } - - glob.sync('*_test.js').forEach(addTestFile); - glob.sync('binary/*_test.js').forEach(addTestFile); - - exec(cmd + - 'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' + - 'cp google-protobuf.js commonjs_out/test_node_modules && ' + - 'cp commonjs/strict_test.js commonjs_out/strict_test.js &&' + - 'cp commonjs/import_test.js commonjs_out/import_test.js', - function (err, stdout, stderr) { - console.log(stdout); - console.log(stderr); - cb(err); - }); -}); +gulp.task( + 'make_commonjs_out', + gulp.series( + [ + 'dist', 'genproto_well_known_types_commonjs', + 'genproto_group1_commonjs', 'genproto_group2_commonjs', + 'genproto_commonjs_wellknowntypes', 'commonjs_asserts', + 'commonjs_testdeps', 'genproto_group3_commonjs_strict' + ], + function(cb) { + // TODO(haberman): minify this more aggressively. + // Will require proper externs/exports. + var cmd = + 'mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && '; + function addTestFile(file) { + cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file + + ' > commonjs_out/' + file + '&& '; + } -gulp.task('deps', ['genproto_well_known_types_closure', 'genproto_group1_closure', 'genproto_group2_closure'], function (cb) { - exec('./node_modules/google-closure-library/closure/bin/build/depswriter.py binary/arith.js binary/constants.js binary/decoder.js binary/encoder.js binary/reader.js binary/utils.js binary/writer.js debug.js map.js message.js node_loader.js test_bootstrap.js > deps.js', - function (err, stdout, stderr) { - console.log(stdout); - console.log(stderr); - cb(err); - }); -}); + glob.sync('*_test.js').forEach(addTestFile); + glob.sync('binary/*_test.js').forEach(addTestFile); -gulp.task('test_closure', ['genproto_well_known_types_closure', 'genproto_group1_closure', 'genproto_group2_closure', 'deps'], function (cb) { - exec('JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine', - function (err, stdout, stderr) { - console.log(stdout); - console.log(stderr); - cb(err); - }); -}); + exec( + cmd + 'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' + + 'cp google-protobuf.js commonjs_out/test_node_modules && ' + + 'cp commonjs/strict_test.js commonjs_out/strict_test.js &&' + + 'cp commonjs/import_test.js commonjs_out/import_test.js', + function(err, stdout, stderr) { + console.log(stdout); + console.log(stderr); + cb(err); + }); + })); + +gulp.task( + 'deps', + gulp.series( + [ + 'genproto_well_known_types_closure', 'genproto_group1_closure', + 'genproto_group2_closure' + ], + function(cb) { + exec( + './node_modules/google-closure-library/closure/bin/build/depswriter.py binary/arith.js binary/constants.js binary/decoder.js binary/encoder.js binary/reader.js binary/utils.js binary/writer.js debug.js map.js message.js node_loader.js test_bootstrap.js > deps.js', + function(err, stdout, stderr) { + console.log(stdout); + console.log(stderr); + cb(err); + }); + })); -gulp.task('test_commonjs', ['make_commonjs_out'], function (cb) { +gulp.task( + 'test_closure', + gulp.series( + [ + 'genproto_well_known_types_closure', 'genproto_group1_closure', + 'genproto_group2_closure', 'deps' + ], + function(cb) { + exec( + 'JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine', + function(err, stdout, stderr) { + console.log(stdout); + console.log(stderr); + cb(err); + }); + })); + +gulp.task('test_commonjs', gulp.series(['make_commonjs_out'], function(cb) { exec('cd commonjs_out && JASMINE_CONFIG_PATH=jasmine.json NODE_PATH=test_node_modules ../node_modules/.bin/jasmine', function (err, stdout, stderr) { console.log(stdout); console.log(stderr); cb(err); }); -}); +})); -gulp.task('test', ['test_closure', 'test_commonjs'], function(cb) { +gulp.task('test', gulp.series(['test_closure', 'test_commonjs'], function(cb) { cb(); -}); +})); diff --git a/js/map.js b/js/map.js index 671bd5160e..b9a48afed7 100644 --- a/js/map.js +++ b/js/map.js @@ -32,8 +32,8 @@ goog.provide('jspb.Map'); goog.require('goog.asserts'); -goog.forwardDeclare('jspb.BinaryReader'); -goog.forwardDeclare('jspb.BinaryWriter'); +goog.requireType('jspb.BinaryReader'); +goog.requireType('jspb.BinaryWriter'); diff --git a/js/maps_test.js b/js/maps_test.js index e496f446bd..4640c98a46 100755 --- a/js/maps_test.js +++ b/js/maps_test.js @@ -340,11 +340,17 @@ function makeTests(msgInfo, submessageCtor, suffix) { assertElementsEquals(entryIterator.next().value, ['key2', 'value2']); assertEquals(entryIterator.next().done, true); - if (typeof(Symbol) != 'undefined') { + try { var entryIterable = m.entries()[Symbol.iterator](); assertElementsEquals(entryIterable.next().value, ['key1', 'value1']); assertElementsEquals(entryIterable.next().value, ['key2', 'value2']); assertEquals(entryIterable.next().done, true); + } catch (err) { + // jspb.Map.ArrayIteratorIterable_.prototype[Symbol.iterator] may be + // undefined in some environment. + if (err.name != 'TypeError' && err.name != 'ReferenceError') { + throw err; + } } var keyIterator = m.keys(); @@ -352,22 +358,34 @@ function makeTests(msgInfo, submessageCtor, suffix) { assertEquals(keyIterator.next().value, 'key2'); assertEquals(keyIterator.next().done, true); - if (typeof(Symbol) != 'undefined') { + try { var keyIterable = m.keys()[Symbol.iterator](); assertEquals(keyIterable.next().value, 'key1'); assertEquals(keyIterable.next().value, 'key2'); assertEquals(keyIterable.next().done, true); + } catch (err) { + // jspb.Map.ArrayIteratorIterable_.prototype[Symbol.iterator] may be + // undefined in some environment. + if (err.name != 'TypeError' && err.name != 'ReferenceError') { + throw err; + } } var valueIterator = m.values(); assertEquals(valueIterator.next().value, 'value1'); assertEquals(valueIterator.next().value, 'value2'); assertEquals(valueIterator.next().done, true); - if (typeof(Symbol) != 'undefined') { + try { var valueIterable = m.values()[Symbol.iterator](); assertEquals(valueIterable.next().value, 'value1'); assertEquals(valueIterable.next().value, 'value2'); assertEquals(valueIterable.next().done, true); + } catch (err) { + // jspb.Map.ArrayIteratorIterable_.prototype[Symbol.iterator] may be + // undefined in some environment. + if (err.name != 'TypeError' && err.name != 'ReferenceError') { + throw err; + } } }); } diff --git a/js/message.js b/js/message.js index 6a37745b1f..23e5af0d4b 100644 --- a/js/message.js +++ b/js/message.js @@ -41,10 +41,9 @@ goog.provide('jspb.Message'); goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.crypt.base64'); +goog.require('jspb.BinaryReader'); goog.require('jspb.Map'); -// Not needed in compilation units that have no protos with xids. -goog.forwardDeclare('xid.String'); @@ -171,7 +170,8 @@ jspb.Message = function() { * dead code eliminate fields used in protocol buffers that are never used * in an application. */ -goog.define('jspb.Message.GENERATE_TO_OBJECT', true); +jspb.Message.GENERATE_TO_OBJECT = + goog.define('jspb.Message.GENERATE_TO_OBJECT', true); /** @@ -181,12 +181,10 @@ goog.define('jspb.Message.GENERATE_TO_OBJECT', true); * calling fromObject. Enabling this might disable the JSCompiler's ability * to dead code eliminate fields used in protocol buffers that are never * used in an application. - * NOTE: By default no protos actually have a fromObject method. You need to - * add the jspb.generate_from_object options to the proto definition to - * activate the feature. * By default this is enabled for test code only. */ -goog.define('jspb.Message.GENERATE_FROM_OBJECT', !goog.DISALLOW_TEST_ONLY_CODE); +jspb.Message.GENERATE_FROM_OBJECT = goog.define( + 'jspb.Message.GENERATE_FROM_OBJECT', !goog.DISALLOW_TEST_ONLY_CODE); /** @@ -194,7 +192,8 @@ goog.define('jspb.Message.GENERATE_FROM_OBJECT', !goog.DISALLOW_TEST_ONLY_CODE); * this off if you do not use toString in your project and want to trim it * from the compiled JS. */ -goog.define('jspb.Message.GENERATE_TO_STRING', true); +jspb.Message.GENERATE_TO_STRING = + goog.define('jspb.Message.GENERATE_TO_STRING', true); /** @@ -202,7 +201,8 @@ goog.define('jspb.Message.GENERATE_TO_STRING', true); * local (e.g. not from another iframe) and thus safely classified with * instanceof Array. */ -goog.define('jspb.Message.ASSUME_LOCAL_ARRAYS', false); +jspb.Message.ASSUME_LOCAL_ARRAYS = + goog.define('jspb.Message.ASSUME_LOCAL_ARRAYS', false); // TODO(jakubvrana): Turn this off by default. @@ -212,7 +212,8 @@ goog.define('jspb.Message.ASSUME_LOCAL_ARRAYS', false); * the proto before serialization. This is enabled by default to be * backwards compatible. Projects are advised to turn this flag always off. */ -goog.define('jspb.Message.SERIALIZE_EMPTY_TRAILING_FIELDS', true); +jspb.Message.SERIALIZE_EMPTY_TRAILING_FIELDS = + goog.define('jspb.Message.SERIALIZE_EMPTY_TRAILING_FIELDS', true); /** @@ -269,12 +270,12 @@ jspb.Message.prototype.messageId_; /** - * Repeated float or double fields which have been converted to include only - * numbers and not strings holding "NaN", "Infinity" and "-Infinity". + * Repeated fields that have been converted to their proper type. This is used + * for numbers stored as strings (typically "NaN", "Infinity" and "-Infinity") + * and for booleans stored as numbers (0 or 1). * @private {!Object|undefined} */ -jspb.Message.prototype.convertedFloatingPointFields_; - +jspb.Message.prototype.convertedPrimitiveFields_; /** * Repeated fields numbers. @@ -283,18 +284,6 @@ jspb.Message.prototype.convertedFloatingPointFields_; jspb.Message.prototype.repeatedFields; -/** - * The xid of this proto type (The same for all instances of a proto). Provides - * a way to identify a proto by stable obfuscated name. - * @see {xid}. - * Available if {@link jspb.generate_xid} is added as a Message option to - * a protocol buffer. - * @const {!xid.String|undefined} The xid or undefined if message is - * annotated to generate the xid. - */ -jspb.Message.prototype.messageXid; - - /** * Returns the JsPb message_id of this proto. @@ -328,6 +317,10 @@ jspb.Message.getIndex_ = function(msg, fieldNumber) { return fieldNumber + msg.arrayIndexOffset_; }; +// This is only here to ensure we are not back sliding on ES6 requiements for +// protos in g3. +jspb.Message.hiddenES6Property_ = class {}; + /** * Returns the tag number based on the index in msg.array. @@ -369,7 +362,7 @@ jspb.Message.initialize = function( msg.arrayIndexOffset_ = messageId === 0 ? -1 : 0; msg.array = data; jspb.Message.initPivotAndExtensionObject_(msg, suggestedPivot); - msg.convertedFloatingPointFields_ = {}; + msg.convertedPrimitiveFields_ = {}; if (!jspb.Message.SERIALIZE_EMPTY_TRAILING_FIELDS) { // TODO(jakubvrana): This is same for all instances, move to prototype. @@ -427,6 +420,24 @@ jspb.Message.isArray_ = function(o) { goog.isArray(o); }; +/** + * Returns true if the provided argument is an extension object. + * @param {*} o The object to classify as array or not. + * @return {boolean} True if the provided object is an extension object. + * @private + */ +jspb.Message.isExtensionObject_ = function(o) { + // Normal fields are never objects, so we can be sure that if we find an + // object here, then it's the extension object. However, we must ensure that + // the object is not an array, since arrays are valid field values (bytes + // fields can also be array). + // NOTE(lukestebbing): We avoid looking at .length to avoid a JIT bug + // in Safari on iOS 8. See the description of CL/86511464 for details. + return (o !== null && typeof o == 'object' && + !jspb.Message.isArray_(o) && + !(jspb.Message.SUPPORTS_UINT8ARRAY_ && o instanceof Uint8Array)); +}; + /** * If the array contains an extension object in its last position, then the @@ -452,13 +463,7 @@ jspb.Message.initPivotAndExtensionObject_ = function(msg, suggestedPivot) { if (msgLength) { lastIndex = msgLength - 1; var obj = msg.array[lastIndex]; - // Normal fields are never objects, so we can be sure that if we find an - // object here, then it's the extension object. However, we must ensure that - // the object is not an array, since arrays are valid field values. - // NOTE(lukestebbing): We avoid looking at .length to avoid a JIT bug - // in Safari on iOS 8. See the description of CL/86511464 for details. - if (obj && typeof obj == 'object' && !jspb.Message.isArray_(obj) && - !(jspb.Message.SUPPORTS_UINT8ARRAY_ && obj instanceof Uint8Array)) { + if (jspb.Message.isExtensionObject_(obj)) { msg.pivot_ = jspb.Message.getFieldNumber_(msg, lastIndex); msg.extensionObject_ = obj; return; @@ -610,10 +615,7 @@ jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions, * Reads an extension field from the given reader and, if a valid extension, * sets the extension value. * @param {!jspb.Message} msg A jspb proto. - * @param {{ - * skipField:function(this:jspb.BinaryReader), - * getFieldNumber:function(this:jspb.BinaryReader):number - * }} reader + * @param {!jspb.BinaryReader} reader * @param {!Object} extensions The extensions object. * @param {function(this:jspb.Message,!jspb.ExtensionFieldInfo)} getExtensionFn * @param {function(this:jspb.Message,!jspb.ExtensionFieldInfo, ?)} setExtensionFn @@ -692,20 +694,7 @@ jspb.Message.getField = function(msg, fieldNumber) { * @protected */ jspb.Message.getRepeatedField = function(msg, fieldNumber) { - if (fieldNumber < msg.pivot_) { - var index = jspb.Message.getIndex_(msg, fieldNumber); - var val = msg.array[index]; - if (val === jspb.Message.EMPTY_LIST_SENTINEL_) { - return msg.array[index] = []; - } - return val; - } - - var val = msg.extensionObject_[fieldNumber]; - if (val === jspb.Message.EMPTY_LIST_SENTINEL_) { - return msg.extensionObject_[fieldNumber] = []; - } - return val; + return /** @type {!Array} */ (jspb.Message.getField(msg, fieldNumber)); }; @@ -723,6 +712,20 @@ jspb.Message.getOptionalFloatingPointField = function(msg, fieldNumber) { }; +/** + * Gets the value of an optional boolean field. + * @param {!jspb.Message} msg A jspb proto. + * @param {number} fieldNumber The field number. + * @return {?boolean|undefined} The field's value. + * @protected + */ +jspb.Message.getBooleanField = function(msg, fieldNumber) { + var value = jspb.Message.getField(msg, fieldNumber); + // TODO(b/122673075): always return null when the value is null-ish. + return value == null ? (value) : !!value; +}; + + /** * Gets the value of a repeated float or double field. * @param {!jspb.Message} msg A jspb proto. @@ -732,20 +735,42 @@ jspb.Message.getOptionalFloatingPointField = function(msg, fieldNumber) { */ jspb.Message.getRepeatedFloatingPointField = function(msg, fieldNumber) { var values = jspb.Message.getRepeatedField(msg, fieldNumber); - if (!msg.convertedFloatingPointFields_) { - msg.convertedFloatingPointFields_ = {}; + if (!msg.convertedPrimitiveFields_) { + msg.convertedPrimitiveFields_ = {}; } - if (!msg.convertedFloatingPointFields_[fieldNumber]) { + if (!msg.convertedPrimitiveFields_[fieldNumber]) { for (var i = 0; i < values.length; i++) { // Converts "NaN", "Infinity" and "-Infinity" to their corresponding // numbers. values[i] = +values[i]; } - msg.convertedFloatingPointFields_[fieldNumber] = true; + msg.convertedPrimitiveFields_[fieldNumber] = true; } return /** @type {!Array} */ (values); }; +/** + * Gets the value of a repeated boolean field. + * @param {!jspb.Message} msg A jspb proto. + * @param {number} fieldNumber The field number. + * @return {!Array} The field's value. + * @protected + */ +jspb.Message.getRepeatedBooleanField = function(msg, fieldNumber) { + var values = jspb.Message.getRepeatedField(msg, fieldNumber); + if (!msg.convertedPrimitiveFields_) { + msg.convertedPrimitiveFields_ = {}; + } + if (!msg.convertedPrimitiveFields_[fieldNumber]) { + for (var i = 0; i < values.length; i++) { + // Converts 0 and 1 to their corresponding booleans. + values[i] = !!values[i]; + } + msg.convertedPrimitiveFields_[fieldNumber] = true; + } + return /** @type {!Array} */ (values); +}; + /** * Coerce a 'bytes' field to a base 64 string. @@ -854,6 +879,49 @@ jspb.Message.getFieldWithDefault = function(msg, fieldNumber, defaultValue) { }; +/** + * Gets the value of a boolean field, with proto3 (non-nullable primitives) + * semantics. Returns `defaultValue` if the field is not otherwise set. + * @template T + * @param {!jspb.Message} msg A jspb proto. + * @param {number} fieldNumber The field number. + * @param {boolean} defaultValue The default value. + * @return {boolean} The field's value. + * @protected + */ +jspb.Message.getBooleanFieldWithDefault = function( + msg, fieldNumber, defaultValue) { + var value = jspb.Message.getBooleanField(msg, fieldNumber); + if (value == null) { + return defaultValue; + } else { + return value; + } +}; + + +/** + * Gets the value of a floating point field, with proto3 (non-nullable + * primitives) semantics. Returns `defaultValue` if the field is not otherwise + * set. + * @template T + * @param {!jspb.Message} msg A jspb proto. + * @param {number} fieldNumber The field number. + * @param {number} defaultValue The default value. + * @return {number} The field's value. + * @protected + */ +jspb.Message.getFloatingPointFieldWithDefault = function( + msg, fieldNumber, defaultValue) { + var value = jspb.Message.getOptionalFloatingPointField(msg, fieldNumber); + if (value == null) { + return defaultValue; + } else { + return value; + } +}; + + /** * Alias for getFieldWithDefault used by older generated code. * @template T @@ -890,19 +958,19 @@ jspb.Message.getMapField = function(msg, fieldNumber, noLazyCreate, // If we already have a map in the map wrappers, return that. if (fieldNumber in msg.wrappers_) { return msg.wrappers_[fieldNumber]; - } else if (noLazyCreate) { - return undefined; - } else { - // Wrap the underlying elements array with a Map. - var arr = jspb.Message.getField(msg, fieldNumber); - if (!arr) { - arr = []; - jspb.Message.setField(msg, fieldNumber, arr); + } + var arr = jspb.Message.getField(msg, fieldNumber); + // Wrap the underlying elements array with a Map. + if (!arr) { + if (noLazyCreate) { + return undefined; } - return msg.wrappers_[fieldNumber] = - new jspb.Map( - /** @type {!Array>} */ (arr), opt_valueCtor); + arr = []; + jspb.Message.setField(msg, fieldNumber, arr); } + return msg.wrappers_[fieldNumber] = + new jspb.Map( + /** @type {!Array>} */ (arr), opt_valueCtor); }; diff --git a/js/message_test.js b/js/message_test.js index 2bfec62b29..a9fc64a98f 100644 --- a/js/message_test.js +++ b/js/message_test.js @@ -33,7 +33,6 @@ goog.setTestOnly(); goog.require('goog.json'); -goog.require('goog.string'); goog.require('goog.testing.PropertyReplacer'); goog.require('goog.testing.asserts'); goog.require('goog.userAgent'); @@ -41,6 +40,38 @@ goog.require('goog.userAgent'); // CommonJS-LoadFromFile: google-protobuf jspb goog.require('jspb.Message'); +// CommonJS-LoadFromFile: test15_pb proto.jspb.filenametest.package1 +goog.require('proto.jspb.filenametest.package1.b'); + +// CommonJS-LoadFromFile: test14_pb proto.jspb.filenametest.package2 +goog.require('proto.jspb.filenametest.package2.TestMessage'); + +// CommonJS-LoadFromFile: test13_pb proto.jspb.filenametest.package1 +goog.require('proto.jspb.filenametest.package1.a'); +goog.require('proto.jspb.filenametest.package1.TestMessage'); + +// CommonJS-LoadFromFile: test12_pb proto.jspb.circulartest +goog.require('proto.jspb.circulartest.ExtensionContainingType1'); +goog.require('proto.jspb.circulartest.ExtensionContainingType2'); +goog.require('proto.jspb.circulartest.ExtensionField1'); +goog.require('proto.jspb.circulartest.ExtensionField2'); +goog.require('proto.jspb.circulartest.ExtensionField3'); +goog.require('proto.jspb.circulartest.MapField1'); +goog.require('proto.jspb.circulartest.MapField2'); +goog.require('proto.jspb.circulartest.MessageField1'); +goog.require('proto.jspb.circulartest.MessageField2'); +goog.require('proto.jspb.circulartest.NestedEnum1'); +goog.require('proto.jspb.circulartest.NestedEnum2'); +goog.require('proto.jspb.circulartest.NestedMessage1'); +goog.require('proto.jspb.circulartest.NestedMessage2'); +goog.require('proto.jspb.circulartest.RepeatedMessageField1'); +goog.require('proto.jspb.circulartest.RepeatedMessageField2'); + +// CommonJS-LoadFromFile: test11_pb proto.jspb.exttest.reverse +goog.require('proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1'); +goog.require('proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage2'); +goog.require('proto.jspb.exttest.reverse.c'); + // CommonJS-LoadFromFile: test8_pb proto.jspb.exttest.nested goog.require('proto.jspb.exttest.nested.TestNestedExtensionsMessage'); goog.require('proto.jspb.exttest.nested.TestOuterMessage'); @@ -55,6 +86,7 @@ goog.require('proto.jspb.exttest.floatingMsgField'); goog.require('proto.jspb.exttest.floatingMsgFieldTwo'); // CommonJS-LoadFromFile: test_pb proto.jspb.test +goog.require('proto.jspb.test.BooleanFields'); goog.require('proto.jspb.test.CloneExtension'); goog.require('proto.jspb.test.Complex'); goog.require('proto.jspb.test.DefaultValues'); @@ -77,6 +109,7 @@ goog.require('proto.jspb.test.TestCloneExtension'); goog.require('proto.jspb.test.TestEndsWithBytes'); goog.require('proto.jspb.test.TestGroup'); goog.require('proto.jspb.test.TestGroup1'); +goog.require('proto.jspb.test.TestLastFieldBeforePivot'); goog.require('proto.jspb.test.TestMessageWithOneof'); goog.require('proto.jspb.test.TestReservedNames'); goog.require('proto.jspb.test.TestReservedNamesExtension'); @@ -117,58 +150,63 @@ describe('Message test suite', function() { }); it('testComplexConversion', function() { - var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1]; - var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1]; + var data1 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , 1]; + var data2 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , 1]; var foo = new proto.jspb.test.Complex(data1); var bar = new proto.jspb.test.Complex(data2); var result = foo.toObject(); - assertObjectEquals({ - aString: 'a', - anOutOfOrderBool: 1, - aNestedMessage: { - anInt: 11 - }, - aRepeatedMessageList: [{anInt: 22}, {anInt: 33}], - aRepeatedStringList: ['s1', 's2'] - }, result); + assertObjectEquals( + { + aString: 'a', + anOutOfOrderBool: true, + aNestedMessage: {anInt: 11}, + aRepeatedMessageList: [{anInt: 22}, {anInt: 33}], + aRepeatedStringList: ['s1', 's2'], + aFloatingPointField: undefined, + }, + result); // Now test with the jspb instances included. result = foo.toObject(true /* opt_includeInstance */); - assertObjectEquals({ - aString: 'a', - anOutOfOrderBool: 1, - aNestedMessage: { - anInt: 11, - $jspbMessageInstance: foo.getANestedMessage() - }, - aRepeatedMessageList: [ - {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]}, - {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]} - ], - aRepeatedStringList: ['s1', 's2'], - $jspbMessageInstance: foo - }, result); + assertObjectEquals( + { + aString: 'a', + anOutOfOrderBool: true, + aNestedMessage: + {anInt: 11, $jspbMessageInstance: foo.getANestedMessage()}, + aRepeatedMessageList: [ + {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]}, + {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]} + ], + aRepeatedStringList: ['s1', 's2'], + aFloatingPointField: undefined, + $jspbMessageInstance: foo + }, + result); }); it('testMissingFields', function() { var foo = new proto.jspb.test.Complex([ - undefined, undefined, undefined, [], - undefined, undefined, undefined, undefined]); + undefined, undefined, undefined, [], undefined, undefined, undefined, + undefined + ]); var bar = new proto.jspb.test.Complex([ - undefined, undefined, undefined, [], - undefined, undefined, undefined, undefined]); + undefined, undefined, undefined, [], undefined, undefined, undefined, + undefined + ]); var result = foo.toObject(); - assertObjectEquals({ - aString: undefined, - anOutOfOrderBool: undefined, - aNestedMessage: { - anInt: undefined - }, - // Note: JsPb converts undefined repeated fields to empty arrays. - aRepeatedMessageList: [], - aRepeatedStringList: [] - }, result); + assertObjectEquals( + { + aString: undefined, + anOutOfOrderBool: undefined, + aNestedMessage: {anInt: undefined}, + // Note: JsPb converts undefined repeated fields to empty arrays. + aRepeatedMessageList: [], + aRepeatedStringList: [], + aFloatingPointField: undefined, + }, + result); }); @@ -182,20 +220,21 @@ describe('Message test suite', function() { it('testSpecialCases', function() { // Note: Some property names are reserved in JavaScript. // These names are converted to the Js property named pb_. - var special = - new proto.jspb.test.SpecialCases(['normal', 'default', 'function', - 'var']); + var special = new proto.jspb.test.SpecialCases( + ['normal', 'default', 'function', 'var']); var result = special.toObject(); - assertObjectEquals({ - normal: 'normal', - pb_default: 'default', - pb_function: 'function', - pb_var: 'var' - }, result); + assertObjectEquals( + { + normal: 'normal', + pb_default: 'default', + pb_function: 'function', + pb_var: 'var' + }, + result); }); it('testDefaultValues', function() { - var defaultString = "default<>\'\"abc"; + var defaultString = 'default<>\'"abc'; var response = new proto.jspb.test.DefaultValues(); // Test toObject @@ -259,8 +298,10 @@ describe('Message test suite', function() { // Test that clearing the values reverts them to the default state. response = makeDefault(['blah', false, 111, 77]); - response.clearStringField(); response.clearBoolField(); - response.clearIntField(); response.clearEnumField(); + response.clearStringField(); + response.clearBoolField(); + response.clearIntField(); + response.clearEnumField(); assertEquals(defaultString, response.getStringField()); assertEquals(true, response.getBoolField()); assertEquals(11, response.getIntField()); @@ -272,8 +313,10 @@ describe('Message test suite', function() { // Test that setFoo(null) clears the values. response = makeDefault(['blah', false, 111, 77]); - response.setStringField(null); response.setBoolField(null); - response.setIntField(undefined); response.setEnumField(undefined); + response.setStringField(null); + response.setBoolField(null); + response.setIntField(undefined); + response.setEnumField(undefined); assertEquals(defaultString, response.getStringField()); assertEquals(true, response.getBoolField()); assertEquals(11, response.getIntField()); @@ -284,54 +327,18 @@ describe('Message test suite', function() { assertFalse(response.hasEnumField()); }); - it('testClearFields', function() { - var data = ['str', true, [11], [[22], [33]], ['s1', 's2']]; - var foo = new proto.jspb.test.OptionalFields(data); - foo.clearAString(); - foo.clearABool(); - foo.clearANestedMessage(); - foo.clearARepeatedMessageList(); - foo.clearARepeatedStringList(); - assertEquals('', foo.getAString()); - assertEquals(false, foo.getABool()); - assertUndefined(foo.getANestedMessage()); - assertFalse(foo.hasAString()); - assertFalse(foo.hasABool()); - assertObjectEquals([], foo.getARepeatedMessageList()); - assertObjectEquals([], foo.getARepeatedStringList()); - // NOTE: We want the missing fields in 'expected' to be undefined, - // but we actually get a sparse array instead. We could use something - // like [1,undefined,2] to avoid this, except that this is still - // sparse on IE. No comment... - var expected = [,,, [], []]; - expected[0] = expected[1] = expected[2] = undefined; - assertObjectEquals(expected, foo.toArray()); - }); - - it('testDifferenceRawObject', /** @suppress {visibility} */ function() { - var p1 = new proto.jspb.test.HasExtensions(['hi', 'diff', {}]); - var p2 = new proto.jspb.test.HasExtensions(['hi', 'what', - {1000: 'unique'}]); - var diff = /** @type {proto.jspb.test.HasExtensions} */ - (jspb.Message.difference(p1, p2)); - assertEquals('', diff.getStr1()); - assertEquals('what', diff.getStr2()); - assertEquals('', diff.getStr3()); - assertEquals('unique', diff.extensionObject_[1000]); - }); - it('testEqualsSimple', function() { var s1 = new proto.jspb.test.Simple1(['hi']); assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi']))); assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye']))); var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]); - assertTrue(jspb.Message.equals(s1b, - new proto.jspb.test.Simple1(['hi', ['hello']]))); - assertTrue(jspb.Message.equals(s1b, - new proto.jspb.test.Simple1(['hi', ['hello', undefined, - undefined, undefined]]))); - assertFalse(jspb.Message.equals(s1b, - new proto.jspb.test.Simple1(['no', ['hello']]))); + assertTrue(jspb.Message.equals( + s1b, new proto.jspb.test.Simple1(['hi', ['hello']]))); + assertTrue(jspb.Message.equals(s1b, new proto.jspb.test.Simple1([ + 'hi', ['hello', undefined, undefined, undefined] + ]))); + assertFalse(jspb.Message.equals( + s1b, new proto.jspb.test.Simple1(['no', ['hello']]))); // Test with messages of different types var s2 = new proto.jspb.test.Simple2(['hi']); assertFalse(jspb.Message.equals(s1, s2)); @@ -339,18 +346,18 @@ describe('Message test suite', function() { it('testEquals_softComparison', function() { var s1 = new proto.jspb.test.Simple1(['hi', [], null]); - assertTrue(jspb.Message.equals(s1, - new proto.jspb.test.Simple1(['hi', []]))); + assertTrue( + jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi', []]))); var s1b = new proto.jspb.test.Simple1(['hi', [], true]); - assertTrue(jspb.Message.equals(s1b, - new proto.jspb.test.Simple1(['hi', [], 1]))); + assertTrue( + jspb.Message.equals(s1b, new proto.jspb.test.Simple1(['hi', [], 1]))); }); it('testEqualsComplex', function() { - var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1]; - var data2 = ['a',,, [, 11], [[, 22], [, 34]],, ['s1', 's2'],, 1]; - var data3 = ['a',,, [, 11], [[, 22]],, ['s1', 's2'],, 1]; + var data1 = ['a', , , [, 11], [[, 22], [, 33]], , ['s1', 's2'], , 1]; + var data2 = ['a', , , [, 11], [[, 22], [, 34]], , ['s1', 's2'], , 1]; + var data3 = ['a', , , [, 11], [[, 22]], , ['s1', 's2'], , 1]; var data4 = ['hi']; var c1a = new proto.jspb.test.Complex(data1); var c1b = new proto.jspb.test.Complex(data1); @@ -367,42 +374,34 @@ describe('Message test suite', function() { it('testEqualsExtensionsConstructed', function() { assertTrue(jspb.Message.equals( new proto.jspb.test.HasExtensions([]), - new proto.jspb.test.HasExtensions([{}]) - )); + new proto.jspb.test.HasExtensions([{}]))); assertTrue(jspb.Message.equals( new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]), - new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]) - )); + new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]))); assertFalse(jspb.Message.equals( new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]), - new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}]) - )); + new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}]))); assertTrue(jspb.Message.equals( new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]), - new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]) - )); + new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]))); assertTrue(jspb.Message.equals( new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]), - new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]) - )); + new proto.jspb.test.HasExtensions([, , , {100: [{200: 'a'}]}]))); assertTrue(jspb.Message.equals( - new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]), - new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]) - )); + new proto.jspb.test.HasExtensions([, , , {100: [{200: 'a'}]}]), + new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]))); assertTrue(jspb.Message.equals( new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]), - new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]) - )); + new proto.jspb.test.HasExtensions(['hi', , , {100: [{200: 'a'}]}]))); assertTrue(jspb.Message.equals( - new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]), - new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]) - )); + new proto.jspb.test.HasExtensions(['hi', , , {100: [{200: 'a'}]}]), + new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]))); }); it('testEqualsExtensionsUnconstructed', function() { assertTrue(jspb.Message.compareFields([], [{}])); - assertTrue(jspb.Message.compareFields([,,, {}], [])); - assertTrue(jspb.Message.compareFields([,,, {}], [,, {}])); + assertTrue(jspb.Message.compareFields([, , , {}], [])); + assertTrue(jspb.Message.compareFields([, , , {}], [, , {}])); assertTrue(jspb.Message.compareFields( ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}])); assertFalse(jspb.Message.compareFields( @@ -410,13 +409,19 @@ describe('Message test suite', function() { assertTrue(jspb.Message.compareFields( [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}])); assertTrue(jspb.Message.compareFields( - [{100: [{200: 'a'}]}], [,,, {100: [{200: 'a'}]}])); + [{100: [{200: 'a'}]}], [, , , {100: [{200: 'a'}]}])); assertTrue(jspb.Message.compareFields( - [,,, {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}])); + [, , , {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}])); assertTrue(jspb.Message.compareFields( - ['hi', {100: [{200: 'a'}]}], ['hi',,, {100: [{200: 'a'}]}])); + ['hi', {100: [{200: 'a'}]}], ['hi', , , {100: [{200: 'a'}]}])); assertTrue(jspb.Message.compareFields( - ['hi',,, {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}])); + ['hi', , , {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}])); + }); + + it('testInitializeMessageWithLastFieldNull', function() { + // This tests for regression to bug http://b/117298778 + var msg = new proto.jspb.test.TestLastFieldBeforePivot([null]); + assertNotUndefined(msg.getLastFieldBeforePivot()); }); it('testEqualsNonFinite', function() { @@ -434,13 +439,13 @@ describe('Message test suite', function() { it('testToMap', function() { var p1 = new proto.jspb.test.Simple1(['k', ['v']]); var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]); - var soymap = jspb.Message.toMap([p1, p2], - proto.jspb.test.Simple1.prototype.getAString, + var soymap = jspb.Message.toMap( + [p1, p2], proto.jspb.test.Simple1.prototype.getAString, proto.jspb.test.Simple1.prototype.toObject); assertEquals('k', soymap['k'].aString); assertArrayEquals(['v'], soymap['k'].aRepeatedStringList); - var protomap = jspb.Message.toMap([p1, p2], - proto.jspb.test.Simple1.prototype.getAString); + var protomap = jspb.Message.toMap( + [p1, p2], proto.jspb.test.Simple1.prototype.getAString); assertEquals('k', protomap['k'].getAString()); assertArrayEquals(['v'], protomap['k'].getARepeatedStringList()); }); @@ -461,8 +466,12 @@ describe('Message test suite', function() { extension.setExt('e1'); original.setExtension(proto.jspb.test.IsExtension.extField, extension); var clone = original.clone(); - assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],, - [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }], + assertArrayEquals( + [ + 'v1', , ['x1', ['y1', 'z1']], , + [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1, , + {100: [, 'e1']} + ], clone.toArray()); clone.setStr('v2'); var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]); @@ -479,11 +488,19 @@ describe('Message test suite', function() { var newExtension = new proto.jspb.test.CloneExtension(); newExtension.setExt('e2'); clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension); - assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],, - [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2,, { 100: [, 'e2'] }], + assertArrayEquals( + [ + 'v2', , ['a1', ['b1', 'c1']], , + [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2, , + {100: [, 'e2']} + ], clone.toArray()); - assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],, - [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }], + assertArrayEquals( + [ + 'v1', , ['x1', ['y1', 'z1']], , + [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1, , + {100: [, 'e1']} + ], original.toArray()); }); @@ -515,11 +532,12 @@ describe('Message test suite', function() { jspb.Message.copyInto(original, dest); assertArrayEquals(original.toArray(), dest.toArray()); assertEquals('x1', dest.getSimple1().getAString()); - assertEquals('e1', + assertEquals( + 'e1', dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt()); dest.getSimple1().setAString('new value'); - assertNotEquals(dest.getSimple1().getAString(), - original.getSimple1().getAString()); + assertNotEquals( + dest.getSimple1().getAString(), original.getSimple1().getAString()); if (supportsUint8Array) { dest.getBytesField()[0] = 7; assertObjectEquals(bytes1, original.getBytesField()); @@ -529,12 +547,12 @@ describe('Message test suite', function() { assertObjectEquals(bytes1, original.getBytesField()); assertObjectEquals('789', dest.getBytesField()); } - dest.getExtension(proto.jspb.test.CloneExtension.extField). - setExt('new value'); + dest.getExtension(proto.jspb.test.CloneExtension.extField) + .setExt('new value'); assertNotEquals( dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(), - original.getExtension( - proto.jspb.test.CloneExtension.extField).getExt()); + original.getExtension(proto.jspb.test.CloneExtension.extField) + .getExt()); }); it('testCopyInto_notSameType', function() { @@ -552,26 +570,32 @@ describe('Message test suite', function() { var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]); var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']); extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1); - extendable.setExtension(proto.jspb.test.IndirectExtension.simple, - extension2); + extendable.setExtension( + proto.jspb.test.IndirectExtension.simple, extension2); extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy'); - extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList, - ['a', 'b']); + extendable.setExtension( + proto.jspb.test.IndirectExtension.repeatedStrList, ['a', 'b']); var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]); var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]); extendable.setExtension( - proto.jspb.test.IndirectExtension.repeatedSimpleList, - [s1, s2]); - assertObjectEquals(extension1, + proto.jspb.test.IndirectExtension.repeatedSimpleList, [s1, s2]); + assertObjectEquals( + extension1, extendable.getExtension(proto.jspb.test.IsExtension.extField)); - assertObjectEquals(extension2, + assertObjectEquals( + extension2, extendable.getExtension(proto.jspb.test.IndirectExtension.simple)); - assertObjectEquals('xyzzy', + assertObjectEquals( + 'xyzzy', extendable.getExtension(proto.jspb.test.IndirectExtension.str)); - assertObjectEquals(['a', 'b'], extendable.getExtension( - proto.jspb.test.IndirectExtension.repeatedStrList)); - assertObjectEquals([s1, s2], extendable.getExtension( - proto.jspb.test.IndirectExtension.repeatedSimpleList)); + assertObjectEquals( + ['a', 'b'], + extendable.getExtension( + proto.jspb.test.IndirectExtension.repeatedStrList)); + assertObjectEquals( + [s1, s2], + extendable.getExtension( + proto.jspb.test.IndirectExtension.repeatedSimpleList)); // Not supported yet, but it should work... extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null); assertNull( @@ -590,29 +614,35 @@ describe('Message test suite', function() { var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']); var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]); extendable.setExtension(proto.jspb.test.simple1, extension); - assertObjectEquals(extension, - extendable.getExtension(proto.jspb.test.simple1)); + assertObjectEquals( + extension, extendable.getExtension(proto.jspb.test.simple1)); // From _lib mode. extension = new proto.jspb.test.ExtensionMessage(['s1']); extendable = new proto.jspb.test.TestExtensionsMessage([16]); extendable.setExtension(proto.jspb.test.floatingMsgField, extension); extendable.setExtension(proto.jspb.test.floatingStrField, 's2'); - assertObjectEquals(extension, - extendable.getExtension(proto.jspb.test.floatingMsgField)); - assertObjectEquals('s2', - extendable.getExtension(proto.jspb.test.floatingStrField)); + assertObjectEquals( + extension, extendable.getExtension(proto.jspb.test.floatingMsgField)); + assertObjectEquals( + 's2', extendable.getExtension(proto.jspb.test.floatingStrField)); assertNotUndefined(proto.jspb.exttest.floatingMsgField); assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo); assertNotUndefined(proto.jspb.exttest.beta.floatingStrField); }); it('testNestedExtensions', function() { - var extendable = new proto.jspb.exttest.nested.TestNestedExtensionsMessage(); - var extension = new proto.jspb.exttest.nested.TestOuterMessage.NestedExtensionMessage(['s1']); - extendable.setExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension, extension); - assertObjectEquals(extension, - extendable.getExtension(proto.jspb.exttest.nested.TestOuterMessage.innerExtension)); + var extendable = + new proto.jspb.exttest.nested.TestNestedExtensionsMessage(); + var extension = + new proto.jspb.exttest.nested.TestOuterMessage.NestedExtensionMessage( + ['s1']); + extendable.setExtension( + proto.jspb.exttest.nested.TestOuterMessage.innerExtension, extension); + assertObjectEquals( + extension, + extendable.getExtension( + proto.jspb.exttest.nested.TestOuterMessage.innerExtension)); }); it('testToObject_extendedObject', function() { @@ -620,60 +650,72 @@ describe('Message test suite', function() { var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]); var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']); extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1); - extendable.setExtension(proto.jspb.test.IndirectExtension.simple, - extension2); + extendable.setExtension( + proto.jspb.test.IndirectExtension.simple, extension2); extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy'); - extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList, - ['a', 'b']); + extendable.setExtension( + proto.jspb.test.IndirectExtension.repeatedStrList, ['a', 'b']); var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]); var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]); extendable.setExtension( - proto.jspb.test.IndirectExtension.repeatedSimpleList, - [s1, s2]); - assertObjectEquals({ - str1: 'v1', str2: 'v2', str3: 'v3', - extField: { ext1: 'ext1field' }, - simple: { - aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true - }, - str: 'xyzzy', - repeatedStrList: ['a', 'b'], - repeatedSimpleList: [ - { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true}, - { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false} - ] - }, extendable.toObject()); + proto.jspb.test.IndirectExtension.repeatedSimpleList, [s1, s2]); + assertObjectEquals( + { + str1: 'v1', + str2: 'v2', + str3: 'v3', + extField: {ext1: 'ext1field'}, + simple: { + aString: 'str', + aRepeatedStringList: ['s1', 's2'], + aBoolean: true + }, + str: 'xyzzy', + repeatedStrList: ['a', 'b'], + repeatedSimpleList: [ + {aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true}, + {aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false} + ] + }, + extendable.toObject()); // Now, with instances included. - assertObjectEquals({ - str1: 'v1', str2: 'v2', str3: 'v3', - extField: { - ext1: 'ext1field', - $jspbMessageInstance: - extendable.getExtension(proto.jspb.test.IsExtension.extField) - }, - simple: { - aString: 'str', - aRepeatedStringList: ['s1', 's2'], - aBoolean: true, - $jspbMessageInstance: - extendable.getExtension(proto.jspb.test.IndirectExtension.simple) - }, - str: 'xyzzy', - repeatedStrList: ['a', 'b'], - repeatedSimpleList: [{ - aString: 'foo', - aRepeatedStringList: ['s1', 's2'], - aBoolean: true, - $jspbMessageInstance: s1 - }, { - aString: 'bar', - aRepeatedStringList: ['t1', 't2'], - aBoolean: false, - $jspbMessageInstance: s2 - }], - $jspbMessageInstance: extendable - }, extendable.toObject(true /* opt_includeInstance */)); + assertObjectEquals( + { + str1: 'v1', + str2: 'v2', + str3: 'v3', + extField: { + ext1: 'ext1field', + $jspbMessageInstance: + extendable.getExtension(proto.jspb.test.IsExtension.extField) + }, + simple: { + aString: 'str', + aRepeatedStringList: ['s1', 's2'], + aBoolean: true, + $jspbMessageInstance: extendable.getExtension( + proto.jspb.test.IndirectExtension.simple) + }, + str: 'xyzzy', + repeatedStrList: ['a', 'b'], + repeatedSimpleList: [ + { + aString: 'foo', + aRepeatedStringList: ['s1', 's2'], + aBoolean: true, + $jspbMessageInstance: s1 + }, + { + aString: 'bar', + aRepeatedStringList: ['t1', 't2'], + aBoolean: false, + $jspbMessageInstance: s2 + } + ], + $jspbMessageInstance: extendable + }, + extendable.toObject(true /* opt_includeInstance */)); }); it('testInitialization_emptyArray', function() { @@ -706,7 +748,8 @@ describe('Message test suite', function() { }); it('testToObject_hasExtensionField', function() { - var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1'], 102: ''}]); + var data = + new proto.jspb.test.HasExtensions(['str1', {100: ['ext1'], 102: ''}]); var obj = data.toObject(); assertEquals('str1', obj.str1); assertEquals('ext1', obj.extField.ext1); @@ -726,8 +769,7 @@ describe('Message test suite', function() { var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']); data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage); var obj = data.toObject(); - assertNotNull( - data.getExtension(proto.jspb.test.IsExtension.extField)); + assertNotNull(data.getExtension(proto.jspb.test.IsExtension.extField)); assertEquals('is_extension', obj.extField.ext1); }); @@ -744,16 +786,18 @@ describe('Message test suite', function() { var groups = group.getRepeatedGroupList(); assertEquals('g1', groups[0].getId()); assertObjectEquals([true, false], groups[0].getSomeBoolList()); - assertObjectEquals({id: 'g1', someBoolList: [true, false]}, - groups[0].toObject()); - assertObjectEquals({ - repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}], - requiredGroup: {id: undefined}, - optionalGroup: undefined, - requiredSimple: {aRepeatedStringList: [], aString: undefined}, - optionalSimple: undefined, - id: undefined - }, group.toObject()); + assertObjectEquals( + {id: 'g1', someBoolList: [true, false]}, groups[0].toObject()); + assertObjectEquals( + { + repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}], + requiredGroup: {id: undefined}, + optionalGroup: undefined, + requiredSimple: {aRepeatedStringList: [], aString: undefined}, + optionalSimple: undefined, + id: undefined + }, + group.toObject()); var group1 = new proto.jspb.test.TestGroup1(); group1.setGroup(someGroup); assertEquals(someGroup, group1.getGroup()); @@ -770,299 +814,265 @@ describe('Message test suite', function() { message.setExtension$(11); message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12); assertEquals(11, message.getExtension$()); - assertEquals(12, message.getExtension( - proto.jspb.test.TestReservedNamesExtension.foo)); + assertEquals( + 12, + message.getExtension(proto.jspb.test.TestReservedNamesExtension.foo)); assertObjectEquals({extension: 11, foo: 12}, message.toObject()); }); it('testInitializeMessageWithUnsetOneof', function() { var message = new proto.jspb.test.TestMessageWithOneof([]); assertEquals( - proto.jspb.test.TestMessageWithOneof.PartialOneofCase. - PARTIAL_ONEOF_NOT_SET, + proto.jspb.test.TestMessageWithOneof.PartialOneofCase + .PARTIAL_ONEOF_NOT_SET, message.getPartialOneofCase()); assertEquals( - proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase. - RECURSIVE_ONEOF_NOT_SET, + proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase + .RECURSIVE_ONEOF_NOT_SET, message.getRecursiveOneofCase()); }); - it('testInitializeMessageWithSingleValueSetInOneof', function() { - var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']); + it('testUnsetsOneofCaseWhenFieldIsCleared', function() { + var message = new proto.jspb.test.TestMessageWithOneof; + assertEquals( + proto.jspb.test.TestMessageWithOneof.PartialOneofCase + .PARTIAL_ONEOF_NOT_SET, + message.getPartialOneofCase()); - assertEquals('x', message.getPone()); - assertEquals('', message.getPthree()); + message.setPone('hi'); assertEquals( proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE, message.getPartialOneofCase()); - }); - - it('testKeepsLastWireValueSetInUnion_multipleValues', function() { - var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']); - assertEquals('', message.getPone()); - assertEquals('y', message.getPthree()); + message.clearPone(); assertEquals( - proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE, + proto.jspb.test.TestMessageWithOneof.PartialOneofCase + .PARTIAL_ONEOF_NOT_SET, message.getPartialOneofCase()); }); - it('testSettingOneofFieldClearsOthers', function() { - var message = new proto.jspb.test.TestMessageWithOneof; - assertEquals('', message.getPone()); - assertEquals('', message.getPthree()); - assertFalse(message.hasPone()); - assertFalse(message.hasPthree()); + it('testFloatingPointFieldsSupportNan', function() { + var assertNan = function(x) { + assertTrue( + 'Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.', + goog.isNumber(x) && isNaN(x)); + }; - message.setPone('hi'); - assertEquals('hi', message.getPone()); - assertEquals('', message.getPthree()); - assertTrue(message.hasPone()); - assertFalse(message.hasPthree()); - - message.setPthree('bye'); - assertEquals('', message.getPone()); - assertEquals('bye', message.getPthree()); - assertFalse(message.hasPone()); - assertTrue(message.hasPthree()); + var message = new proto.jspb.test.FloatingPointFields([ + 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN', 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN' + ]); + assertNan(message.getOptionalFloatField()); + assertNan(message.getRequiredFloatField()); + assertNan(message.getRepeatedFloatFieldList()[0]); + assertNan(message.getRepeatedFloatFieldList()[1]); + assertNan(message.getDefaultFloatField()); + assertNan(message.getOptionalDoubleField()); + assertNan(message.getRequiredDoubleField()); + assertNan(message.getRepeatedDoubleFieldList()[0]); + assertNan(message.getRepeatedDoubleFieldList()[1]); + assertNan(message.getDefaultDoubleField()); }); - it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() { - var other = new proto.jspb.test.TestMessageWithOneof; - var message = new proto.jspb.test.TestMessageWithOneof; - assertEquals('', message.getPone()); - assertEquals('', message.getPthree()); - assertUndefined(message.getRone()); - assertFalse(message.hasPone()); - assertFalse(message.hasPthree()); + it('testFloatingPointsAreConvertedFromStringInput', function() { + var assertInf = function(x) { + assertTrue( + 'Expected ' + x + ' (' + goog.typeOf(x) + ') to be Infinity.', + x === Infinity); + }; + var message = new proto.jspb.test.FloatingPointFields([ + Infinity, 'Infinity', ['Infinity', Infinity], 'Infinity', 'Infinity', + 'Infinity', ['Infinity', Infinity], 'Infinity' + ]); + assertInf(message.getOptionalFloatField()); + assertInf(message.getRequiredFloatField()); + assertInf(message.getRepeatedFloatFieldList()[0]); + assertInf(message.getRepeatedFloatFieldList()[1]); + assertInf(message.getDefaultFloatField()); + assertInf(message.getOptionalDoubleField()); + assertInf(message.getRequiredDoubleField()); + assertInf(message.getRepeatedDoubleFieldList()[0]); + assertInf(message.getRepeatedDoubleFieldList()[1]); + assertInf(message.getDefaultDoubleField()); + }); - message.setPone('hi'); - message.setRone(other); - assertEquals('hi', message.getPone()); - assertEquals('', message.getPthree()); - assertEquals(other, message.getRone()); - assertTrue(message.hasPone()); - assertFalse(message.hasPthree()); - - message.setPthree('bye'); - assertEquals('', message.getPone()); - assertEquals('bye', message.getPthree()); - assertEquals(other, message.getRone()); - assertFalse(message.hasPone()); - assertTrue(message.hasPthree()); + it('testBooleansAreConvertedFromNumberInput', function() { + var assertBooleanFieldTrue = function(x) { + assertTrue( + 'Expected ' + x + ' (' + goog.typeOf(x) + ') to be True.', + x === true); + }; + var message = new proto.jspb.test.BooleanFields([1, 1, [true, 1]]); + assertBooleanFieldTrue(message.getOptionalBooleanField()); + assertBooleanFieldTrue(message.getRequiredBooleanField()); + assertBooleanFieldTrue(message.getRepeatedBooleanFieldList()[0]); + assertBooleanFieldTrue(message.getRepeatedBooleanFieldList()[1]); + assertBooleanFieldTrue(message.getDefaultBooleanField()); + + var assertBooleanFieldFalse = function(x) { + assertTrue( + 'Expected ' + x + ' (' + goog.typeOf(x) + ') to be False.', + x === false); + }; + message = new proto.jspb.test.BooleanFields([0, 0, [0, 0]]); + assertBooleanFieldFalse(message.getOptionalBooleanField()); + assertBooleanFieldFalse(message.getRequiredBooleanField()); + assertBooleanFieldFalse(message.getRepeatedBooleanFieldList()[0]); + assertBooleanFieldFalse(message.getRepeatedBooleanFieldList()[1]); }); - it('testUnsetsOneofCaseWhenFieldIsCleared', function() { - var message = new proto.jspb.test.TestMessageWithOneof; - assertEquals( - proto.jspb.test.TestMessageWithOneof.PartialOneofCase. - PARTIAL_ONEOF_NOT_SET, - message.getPartialOneofCase()); + it('testExtensionReverseOrder', function() { + var message2 = + new proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage2; - message.setPone('hi'); - assertEquals( - proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE, - message.getPartialOneofCase()); + message2.setExtension( + proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1.a, 233); + message2.setExtension( + proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1 + .TestExtensionReverseOrderNestedMessage1.b, + 2333); + message2.setExtension(proto.jspb.exttest.reverse.c, 23333); - message.clearPone(); assertEquals( - proto.jspb.test.TestMessageWithOneof.PartialOneofCase. - PARTIAL_ONEOF_NOT_SET, - message.getPartialOneofCase()); + 233, + message2.getExtension( + proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1.a)); + assertEquals( + 2333, + message2.getExtension( + proto.jspb.exttest.reverse.TestExtensionReverseOrderMessage1 + .TestExtensionReverseOrderNestedMessage1.b)); + assertEquals(23333, message2.getExtension(proto.jspb.exttest.reverse.c)); }); - it('testMessageWithDefaultOneofValues', function() { - var message = new proto.jspb.test.TestMessageWithOneof; - assertEquals(1234, message.getAone()); - assertEquals(0, message.getAtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofACase - .DEFAULT_ONEOF_A_NOT_SET, - message.getDefaultOneofACase()); + it('testCircularDepsBaseOnMessageField', function() { + var nestMessage1 = new proto.jspb.circulartest.MessageField1; + var nestMessage2 = new proto.jspb.circulartest.MessageField2; + var message1 = new proto.jspb.circulartest.MessageField1; + var message2 = new proto.jspb.circulartest.MessageField2; - message.setAone(567); - assertEquals(567, message.getAone()); - assertEquals(0, message.getAtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE, - message.getDefaultOneofACase()); + nestMessage1.setA(1); + nestMessage2.setA(2); + message1.setB(nestMessage2); + message2.setB(nestMessage1); - message.setAtwo(890); - assertEquals(1234, message.getAone()); - assertEquals(890, message.getAtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, - message.getDefaultOneofACase()); - message.clearAtwo(); - assertEquals(1234, message.getAone()); - assertEquals(0, message.getAtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofACase - .DEFAULT_ONEOF_A_NOT_SET, - message.getDefaultOneofACase()); + assertEquals(2, message1.getB().getA()); + assertEquals(1, message2.getB().getA()); }); - it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() { - var message = new proto.jspb.test.TestMessageWithOneof; - assertEquals(0, message.getBone()); - assertEquals(1234, message.getBtwo()); - assertFalse(message.hasBone()); - assertFalse(message.hasBtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase - .DEFAULT_ONEOF_B_NOT_SET, - message.getDefaultOneofBCase()); - - message.setBone(2); - assertEquals(2, message.getBone()); - assertEquals(1234, message.getBtwo()); - assertTrue(message.hasBone()); - assertFalse(message.hasBtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE, - message.getDefaultOneofBCase()); - - message.setBtwo(3); - assertEquals(0, message.getBone()); - assertFalse(message.hasBone()); - assertTrue(message.hasBtwo()); - assertEquals(3, message.getBtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, - message.getDefaultOneofBCase()); - - message.clearBtwo(); - assertEquals(0, message.getBone()); - assertFalse(message.hasBone()); - assertFalse(message.hasBtwo()); - assertEquals(1234, message.getBtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase - .DEFAULT_ONEOF_B_NOT_SET, - message.getDefaultOneofBCase()); + + it('testCircularDepsBaseOnRepeatedMessageField', function() { + var nestMessage1 = new proto.jspb.circulartest.RepeatedMessageField1; + var nestMessage2 = new proto.jspb.circulartest.RepeatedMessageField2; + var message1 = new proto.jspb.circulartest.RepeatedMessageField1; + var message2 = new proto.jspb.circulartest.RepeatedMessageField2; + + nestMessage1.setA(1); + nestMessage2.setA(2); + message1.setB(nestMessage2); + message2.addB(nestMessage1); + + + assertEquals(2, message1.getB().getA()); + assertEquals(1, message2.getBList()[0].getA()); }); - it('testInitializeMessageWithOneofDefaults', function() { - var message = - new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567)); - assertEquals(567, message.getAone()); - assertEquals(0, message.getAtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE, - message.getDefaultOneofACase()); + it('testCircularDepsBaseOnMapField', function() { + var nestMessage1 = new proto.jspb.circulartest.MapField1; + var nestMessage2 = new proto.jspb.circulartest.MapField2; + var message1 = new proto.jspb.circulartest.MapField1; + var message2 = new proto.jspb.circulartest.MapField2; - message = - new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890)); - assertEquals(1234, message.getAone()); - assertEquals(890, message.getAtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, - message.getDefaultOneofACase()); + nestMessage1.setA(1); + nestMessage2.setA(2); + message1.setB(nestMessage2); + message2.getBMap().set(1, nestMessage1); - message = - new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890)); - assertEquals(1234, message.getAone()); - assertEquals(890, message.getAtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, - message.getDefaultOneofACase()); + + assertEquals(2, message1.getB().getA()); + assertEquals(1, message2.getBMap().get(1).getA()); }); - it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField', - function() { - var message; - - message = - new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567)); - assertEquals(567, message.getBone()); - assertEquals(1234, message.getBtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE, - message.getDefaultOneofBCase()); - - message = - new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890)); - assertEquals(0, message.getBone()); - assertEquals(890, message.getBtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, - message.getDefaultOneofBCase()); - - message = new proto.jspb.test.TestMessageWithOneof( - new Array(11).concat(567, 890)); - assertEquals(0, message.getBone()); - assertEquals(890, message.getBtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, - message.getDefaultOneofBCase()); - }); - - it('testOneofContainingAnotherMessage', function() { - var message = new proto.jspb.test.TestMessageWithOneof; - assertEquals( - proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase. - RECURSIVE_ONEOF_NOT_SET, - message.getRecursiveOneofCase()); + it('testCircularDepsBaseOnNestedMessage', function() { + var nestMessage1 = + new proto.jspb.circulartest.NestedMessage1.NestedNestedMessage; + var nestMessage2 = new proto.jspb.circulartest.NestedMessage2; + var message1 = new proto.jspb.circulartest.NestedMessage1; + var message2 = new proto.jspb.circulartest.NestedMessage2; + + nestMessage1.setA(1); + nestMessage2.setA(2); + message1.setB(nestMessage2); + message2.setB(nestMessage1); + + + assertEquals(2, message1.getB().getA()); + assertEquals(1, message2.getB().getA()); + }); + + it('testCircularDepsBaseOnNestedEnum', function() { + var nestMessage2 = new proto.jspb.circulartest.NestedEnum2; + var message1 = new proto.jspb.circulartest.NestedEnum1; + var message2 = new proto.jspb.circulartest.NestedEnum2; + + nestMessage2.setA(2); + message1.setB(nestMessage2); + message2.setB(proto.jspb.circulartest.NestedEnum1.NestedNestedEnum.VALUE_1); - var other = new proto.jspb.test.TestMessageWithOneof; - message.setRone(other); - assertEquals(other, message.getRone()); - assertEquals('', message.getRtwo()); - assertEquals( - proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE, - message.getRecursiveOneofCase()); - message.setRtwo('hi'); - assertUndefined(message.getRone()); - assertEquals('hi', message.getRtwo()); + assertEquals(2, message1.getB().getA()); assertEquals( - proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO, - message.getRecursiveOneofCase()); + proto.jspb.circulartest.NestedEnum1.NestedNestedEnum.VALUE_1, + message2.getB()); }); - it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray', - function() { - var message = new proto.jspb.test.TestMessageWithOneof; - message.setPone('x'); - assertEquals('x', message.getPone()); - assertEquals('', message.getPthree()); + it('testCircularDepsBaseOnExtensionContainingType', function() { + var nestMessage2 = new proto.jspb.circulartest.ExtensionContainingType2; + var message1 = new proto.jspb.circulartest.ExtensionContainingType1; + + nestMessage2.setA(2); + message1.setB(nestMessage2); + message1.setExtension( + proto.jspb.circulartest.ExtensionContainingType2.c, 1); + + + assertEquals(2, message1.getB().getA()); assertEquals( - proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE, - message.getPartialOneofCase()); + 1, + message1.getExtension( + proto.jspb.circulartest.ExtensionContainingType2.c)); + }); + + it('testCircularDepsBaseOnExtensionField', function() { + var nestMessage2 = new proto.jspb.circulartest.ExtensionField2; + var message1 = new proto.jspb.circulartest.ExtensionField1; + var message3 = new proto.jspb.circulartest.ExtensionField3; + + nestMessage2.setA(2); + message1.setB(nestMessage2); + message3.setExtension(proto.jspb.circulartest.ExtensionField2.c, message1); - var array = message.toArray(); - assertEquals('x', array[2]); - assertUndefined(array[4]); - array[4] = 'y'; assertEquals( - proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE, - message.getPartialOneofCase()); - assertUndefined(array[2]); - assertEquals('y', array[4]); + 2, + message3.getExtension(proto.jspb.circulartest.ExtensionField2.c) + .getB() + .getA()); }); - it('testFloatingPointFieldsSupportNan', function() { - var assertNan = function(x) { - assertTrue('Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.', - goog.isNumber(x) && isNaN(x)); - }; + it('testSameMessageNameOuputs', function() { + var package1Message = new proto.jspb.filenametest.package1.TestMessage; + var package2Message = new proto.jspb.filenametest.package2.TestMessage; - var message = new proto.jspb.test.FloatingPointFields([ - 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN', - 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN' - ]); - assertNan(message.getOptionalFloatField()); - assertNan(message.getRequiredFloatField()); - assertNan(message.getRepeatedFloatFieldList()[0]); - assertNan(message.getRepeatedFloatFieldList()[1]); - assertNan(message.getDefaultFloatField()); - assertNan(message.getOptionalDoubleField()); - assertNan(message.getRequiredDoubleField()); - assertNan(message.getRepeatedDoubleFieldList()[0]); - assertNan(message.getRepeatedDoubleFieldList()[1]); - assertNan(message.getDefaultDoubleField()); + package1Message.setExtension(proto.jspb.filenametest.package1.a, 10); + package1Message.setExtension(proto.jspb.filenametest.package1.b, 11); + package2Message.setA(12); + + assertEquals( + 10, package1Message.getExtension(proto.jspb.filenametest.package1.a)); + assertEquals( + 11, package1Message.getExtension(proto.jspb.filenametest.package1.b)); + assertEquals(12, package2Message.getA()); }); }); diff --git a/js/package.json b/js/package.json index dd5b63711e..9336a1e4a9 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "google-protobuf", - "version": "3.6.1", + "version": "3.9.0-rc.1", "description": "Protocol Buffers for JavaScript", "main": "google-protobuf.js", "files": [ @@ -9,9 +9,9 @@ "dependencies": {}, "devDependencies": { "glob": "~6.0.4", - "google-closure-compiler": "~20160619.0.0", - "google-closure-library": "~20160125.0.0", - "gulp": "~3.9.0", + "google-closure-compiler": "~20190301.0.0", + "google-closure-library": "~20190301.0.0", + "gulp": "~4.0.1", "jasmine": "~2.4.1" }, "scripts": { @@ -19,7 +19,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/google/protobuf/tree/master/js" + "url": "https://github.com/protocolbuffers/protobuf/tree/master/js" }, "author": "Google Protocol Buffers Team", "license": "BSD-3-Clause" diff --git a/js/proto3_test.js b/js/proto3_test.js index 4aed88baab..7f9d71d3f6 100644 --- a/js/proto3_test.js +++ b/js/proto3_test.js @@ -30,17 +30,15 @@ goog.require('goog.crypt.base64'); goog.require('goog.testing.asserts'); - // CommonJS-LoadFromFile: testbinary_pb proto.jspb.test goog.require('proto.jspb.test.ForeignMessage'); - // CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test goog.require('proto.jspb.test.Proto3Enum'); goog.require('proto.jspb.test.TestProto3'); - +// CommonJS-LoadFromFile: google/protobuf/any_pb proto.google.protobuf +goog.require('proto.google.protobuf.Any'); // CommonJS-LoadFromFile: google/protobuf/timestamp_pb proto.google.protobuf goog.require('proto.google.protobuf.Timestamp'); - // CommonJS-LoadFromFile: google/protobuf/struct_pb proto.google.protobuf goog.require('proto.google.protobuf.Struct'); @@ -377,6 +375,7 @@ describe('proto3Test', function() { }); + it('testTimestampWellKnownType', function() { var msg = new proto.google.protobuf.Timestamp(); msg.fromDate(new Date(123456789)); @@ -384,6 +383,9 @@ describe('proto3Test', function() { assertEquals(789000000, msg.getNanos()); var date = msg.toDate(); assertEquals(123456789, date.getTime()); + var anotherMsg = proto.google.protobuf.Timestamp.fromDate(date); + assertEquals(msg.getSeconds(), anotherMsg.getSeconds()); + assertEquals(msg.getNanos(), anotherMsg.getNanos()); }); it('testStructWellKnownType', function() { diff --git a/js/proto3_test.proto b/js/proto3_test.proto index 0d073ea03e..f23e19c9d8 100644 --- a/js/proto3_test.proto +++ b/js/proto3_test.proto @@ -35,43 +35,43 @@ import "testbinary.proto"; package jspb.test; message TestProto3 { - int32 optional_int32 = 1; - int64 optional_int64 = 2; - uint32 optional_uint32 = 3; - uint64 optional_uint64 = 4; - sint32 optional_sint32 = 5; - sint64 optional_sint64 = 6; - fixed32 optional_fixed32 = 7; - fixed64 optional_fixed64 = 8; - sfixed32 optional_sfixed32 = 9; + int32 optional_int32 = 1; + int64 optional_int64 = 2; + uint32 optional_uint32 = 3; + uint64 optional_uint64 = 4; + sint32 optional_sint32 = 5; + sint64 optional_sint64 = 6; + fixed32 optional_fixed32 = 7; + fixed64 optional_fixed64 = 8; + sfixed32 optional_sfixed32 = 9; sfixed64 optional_sfixed64 = 10; - float optional_float = 11; - double optional_double = 12; - bool optional_bool = 13; - string optional_string = 14; - bytes optional_bytes = 15; + float optional_float = 11; + double optional_double = 12; + bool optional_bool = 13; + string optional_string = 14; + bytes optional_bytes = 15; ForeignMessage optional_foreign_message = 19; - Proto3Enum optional_foreign_enum = 22; + Proto3Enum optional_foreign_enum = 22; - repeated int32 repeated_int32 = 31; - repeated int64 repeated_int64 = 32; - repeated uint32 repeated_uint32 = 33; - repeated uint64 repeated_uint64 = 34; - repeated sint32 repeated_sint32 = 35; - repeated sint64 repeated_sint64 = 36; - repeated fixed32 repeated_fixed32 = 37; - repeated fixed64 repeated_fixed64 = 38; + repeated int32 repeated_int32 = 31; + repeated int64 repeated_int64 = 32; + repeated uint32 repeated_uint32 = 33; + repeated uint64 repeated_uint64 = 34; + repeated sint32 repeated_sint32 = 35; + repeated sint64 repeated_sint64 = 36; + repeated fixed32 repeated_fixed32 = 37; + repeated fixed64 repeated_fixed64 = 38; repeated sfixed32 repeated_sfixed32 = 39; repeated sfixed64 repeated_sfixed64 = 40; - repeated float repeated_float = 41; - repeated double repeated_double = 42; - repeated bool repeated_bool = 43; - repeated string repeated_string = 44; - repeated bytes repeated_bytes = 45; + repeated float repeated_float = 41; + repeated double repeated_double = 42; + repeated bool repeated_bool = 43; + repeated string repeated_string = 44; + repeated bytes repeated_bytes = 45; repeated ForeignMessage repeated_foreign_message = 49; - repeated Proto3Enum repeated_foreign_enum = 52; + repeated Proto3Enum repeated_foreign_enum = 52; oneof oneof_field { diff --git a/js/test.proto b/js/test.proto index 3b538b5262..7c2a4699a2 100644 --- a/js/test.proto +++ b/js/test.proto @@ -39,8 +39,7 @@ import "google/protobuf/descriptor.proto"; package jspb.test; -message Empty { -} +message Empty {} enum OuterEnum { FOO = 1; @@ -94,10 +93,11 @@ message Complex { required int32 an_int = 2; } required string a_string = 1; - required bool an_out_of_order_bool = 9; + optional bool an_out_of_order_bool = 9; optional Nested a_nested_message = 4; repeated Nested a_repeated_message = 5; repeated string a_repeated_string = 7; + optional double a_floating_point_field = 10; } message OuterMessage { @@ -107,6 +107,13 @@ message OuterMessage { } } +message MineField { + // document.cookie is a banned property in a couple of conformance check + // configs at Google. Verify that having a field called cookie doesn't confuse + // the compiler and break the build. + optional string cookie = 1; +} + message IsExtension { extend HasExtensions { optional IsExtension ext_field = 100; @@ -137,12 +144,13 @@ message DefaultValues { E1 = 13; E2 = 77; } - optional string string_field = 1 [default="default<>\'\"abc"]; - optional bool bool_field = 2 [default=true]; - optional int64 int_field = 3 [default=11]; - optional Enum enum_field = 4 [default=E1]; - optional string empty_field = 6 [default=""]; - optional bytes bytes_field = 8 [default="moo"]; // Base64 encoding is "bW9v" + optional string string_field = 1 [default = "default<>\'\"abc"]; + optional bool bool_field = 2 [default = true]; + optional int64 int_field = 3 [default = 11]; + optional Enum enum_field = 4 [default = E1]; + optional string empty_field = 6 [default = ""]; + optional bytes bytes_field = 8 + [default = "moo"]; // Base64 encoding is "bW9v" } message FloatingPointFields { @@ -156,6 +164,13 @@ message FloatingPointFields { optional double default_double_field = 8 [default = 2.0]; } +message BooleanFields { + optional bool optional_boolean_field = 1; + required bool required_boolean_field = 2; + repeated bool repeated_boolean_field = 3; + optional bool default_boolean_field = 4 [default = true]; +} + message TestClone { optional string str = 1; optional Simple1 simple1 = 3; @@ -241,16 +256,26 @@ message TestEndsWithBytes { optional bytes data = 2; } +// This message is for testing extension handling doesn't affect fields before +// pivot. Don't add new field to this message. See b/117298778 for more detail. +message TestLastFieldBeforePivot { + optional int32 last_field_before_pivot = 1; + extensions 100 to max; +} + +extend TestLastFieldBeforePivot { + optional int32 extend_test_last_field_before_pivot_field = 101; +} + message Int64Types { - optional int64 int64_normal = 1 [jstype=JS_NORMAL]; - optional sint64 int64_string = 2 [jstype=JS_STRING]; - optional uint64 int64_number = 3 [jstype=JS_NUMBER]; + optional int64 int64_normal = 1 [jstype = JS_NORMAL]; + optional sint64 int64_string = 2 [jstype = JS_STRING]; + optional uint64 int64_number = 3 [jstype = JS_NUMBER]; } message TestMapFieldsNoBinary { - map map_string_string = 1; map map_string_int32 = 2; map map_string_int64 = 3; @@ -274,7 +299,6 @@ enum MapValueEnumNoBinary { } message MapValueMessageNoBinary { - optional int32 foo = 1; } @@ -286,3 +310,4 @@ message Deeply { } } + diff --git a/js/test11.proto b/js/test11.proto new file mode 100644 index 0000000000..ae65a46a7f --- /dev/null +++ b/js/test11.proto @@ -0,0 +1,52 @@ +// 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. + +syntax = "proto2"; + +package jspb.exttest.reverse; + +message TestExtensionReverseOrderMessage1 { + extend TestExtensionReverseOrderMessage2 { + optional int32 a = 1; + } + message TestExtensionReverseOrderNestedMessage1 { + extend TestExtensionReverseOrderMessage2 { + optional int32 b = 2; + } + } +} + +extend TestExtensionReverseOrderMessage2 { + optional int32 c = 3; +} + +message TestExtensionReverseOrderMessage2 { + extensions 1 to 100; +} diff --git a/js/test12.proto b/js/test12.proto new file mode 100644 index 0000000000..4ece1d0632 --- /dev/null +++ b/js/test12.proto @@ -0,0 +1,119 @@ +// 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. + +syntax = "proto2"; + + +package jspb.circulartest; + +message MessageField1 { + optional int32 a = 1; + optional MessageField2 b = 2; +} + +message MessageField2 { + optional int32 a = 1; + optional MessageField1 b = 2; +} + + +message RepeatedMessageField1 { + optional int32 a = 1; + optional RepeatedMessageField2 b = 2; +} + +message RepeatedMessageField2 { + optional int32 a = 1; + repeated RepeatedMessageField1 b = 2; +} + +message MapField1 { + optional int32 a = 1; + optional MapField2 b = 2; +} + +message MapField2 { + optional int32 a = 1; + map b = 2; +} + +message NestedMessage1 { + optional NestedMessage2 b = 2; + message NestedNestedMessage { + optional int32 a = 1; + } +} + +message NestedMessage2 { + optional int32 a = 1; + optional NestedMessage1.NestedNestedMessage b = 2; +} + +message NestedEnum1 { + optional NestedEnum2 b = 2; + enum NestedNestedEnum { + UNDEFINED = 0; + VALUE_1 = 1; + } +} + +message NestedEnum2 { + optional int32 a = 1; + optional NestedEnum1.NestedNestedEnum b = 2; +} + +message ExtensionContainingType1 { + optional int32 a = 1; + optional ExtensionContainingType2 b = 2; + extensions 99 to 100; +} + +message ExtensionContainingType2 { + optional int32 a = 1; + extend ExtensionContainingType1 { + optional int32 c = 99; + } +} + +message ExtensionField1 { + optional int32 a = 1; + optional ExtensionField2 b = 2; +} + +message ExtensionField2 { + optional int32 a = 1; + extend ExtensionField3 { + optional ExtensionField1 c = 99; + } +} + +message ExtensionField3 { + extensions 99 to 100; +} diff --git a/js/test13.proto b/js/test13.proto new file mode 100644 index 0000000000..b9895d87ae --- /dev/null +++ b/js/test13.proto @@ -0,0 +1,70 @@ +// 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. + +syntax = "proto2"; + +package jspb.filenametest.package1; + +message TestMessage { + extensions 1 to 100; +} + +extend TestMessage { + optional int32 a = 1; +} + +enum TestEnum { + VALUE_0 = 0; + VALUE_1 = 1; +} + +message TestLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName1 { + optional TestLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName2 + a = 1; + optional int32 b = 2; +} + +message TestLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName2 { + optional TestLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName3 + a = 1; + optional int32 b = 2; +} + +message TestLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName3 { + optional TestLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName4 + a = 1; + optional int32 b = 2; +} + +message TestLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName4 { + optional TestLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName1 + a = 1; + optional int32 b = 2; +} diff --git a/src/google/protobuf/arenastring.cc b/js/test14.proto similarity index 84% rename from src/google/protobuf/arenastring.cc rename to js/test14.proto index 7f33a0c865..2447eb1403 100644 --- a/src/google/protobuf/arenastring.cc +++ b/js/test14.proto @@ -28,16 +28,16 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// The ArenaString implementation is not included in the open-source release. Do -// not include this file in the distribution. +syntax = "proto2"; -#include +package jspb.filenametest.package2; -namespace google { -namespace protobuf { -namespace internal { +message TestMessage { + optional int32 a = 1; +} - -} // namespace internal -} // namespace protobuf -} // namespace google +enum TestEnum { + VALUE_0 = 0; + VALUE_1 = 1; + VALUE_2 = 2; +} diff --git a/js/test15.proto b/js/test15.proto new file mode 100644 index 0000000000..b481116519 --- /dev/null +++ b/js/test15.proto @@ -0,0 +1,39 @@ +// 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. + +syntax = "proto2"; + +package jspb.filenametest.package1; + +import "test13.proto"; + +extend TestMessage { + optional int32 b = 2; +} diff --git a/js/test2.proto b/js/test2.proto index b67f93fa9e..e9457e7c71 100644 --- a/js/test2.proto +++ b/js/test2.proto @@ -30,13 +30,13 @@ syntax = "proto2"; -option java_package = "com.google.apps.jspb.proto"; -option java_multiple_files = true; - package jspb.test; import "test.proto"; +option java_package = "com.google.apps.jspb.proto"; +option java_multiple_files = true; + message TestExtensionsMessage { optional int32 intfield = 1; extensions 100 to max; diff --git a/js/test3.proto b/js/test3.proto index 940a552ed5..3fa037dfba 100644 --- a/js/test3.proto +++ b/js/test3.proto @@ -30,11 +30,11 @@ syntax = "proto2"; +package jspb.exttest; + option java_package = "com.google.apps.jspb.proto"; option java_multiple_files = true; -package jspb.exttest; - message TestExtensionsMessage { optional int32 intfield = 1; extensions 100 to max; diff --git a/js/test4.proto b/js/test4.proto index cf2451e9cb..c3c8342411 100644 --- a/js/test4.proto +++ b/js/test4.proto @@ -30,13 +30,13 @@ syntax = "proto2"; -option java_package = "com.google.apps.jspb.proto"; -option java_multiple_files = true; - package jspb.exttest; import "test3.proto"; +option java_package = "com.google.apps.jspb.proto"; +option java_multiple_files = true; + extend TestExtensionsMessage { optional ExtensionMessage floating_msg_field_two = 103; } diff --git a/js/test5.proto b/js/test5.proto index 34979517e5..db297213a4 100644 --- a/js/test5.proto +++ b/js/test5.proto @@ -30,11 +30,11 @@ syntax = "proto2"; +package jspb.exttest.beta; + option java_package = "com.google.apps.jspb.proto"; option java_multiple_files = true; -package jspb.exttest.beta; - message TestBetaExtensionsMessage { extensions 100 to max; } diff --git a/js/test8.proto b/js/test8.proto index 2ae80dab67..7dbb6eff14 100644 --- a/js/test8.proto +++ b/js/test8.proto @@ -30,11 +30,11 @@ syntax = "proto2"; +package jspb.exttest.nested; + option java_package = "com.google.apps.jspb.proto"; option java_multiple_files = true; -package jspb.exttest.nested; - message TestNestedExtensionsMessage { optional int32 intfield = 1; extensions 100 to max; diff --git a/js/testbinary.proto b/js/testbinary.proto index ed5bdfc68f..2e548454da 100644 --- a/js/testbinary.proto +++ b/js/testbinary.proto @@ -39,66 +39,66 @@ package jspb.test; // to ensure that the binary-format support will handle all field types // properly. message TestAllTypes { - optional int32 optional_int32 = 1; - optional int64 optional_int64 = 2; - optional uint32 optional_uint32 = 3; - optional uint64 optional_uint64 = 4; - optional sint32 optional_sint32 = 5; - optional sint64 optional_sint64 = 6; - optional fixed32 optional_fixed32 = 7; - optional fixed64 optional_fixed64 = 8; - optional sfixed32 optional_sfixed32 = 9; + optional int32 optional_int32 = 1; + optional int64 optional_int64 = 2; + optional uint32 optional_uint32 = 3; + optional uint64 optional_uint64 = 4; + optional sint32 optional_sint32 = 5; + optional sint64 optional_sint64 = 6; + optional fixed32 optional_fixed32 = 7; + optional fixed64 optional_fixed64 = 8; + optional sfixed32 optional_sfixed32 = 9; optional sfixed64 optional_sfixed64 = 10; - optional float optional_float = 11; - optional double optional_double = 12; - optional bool optional_bool = 13; - optional string optional_string = 14; - optional bytes optional_bytes = 15; + optional float optional_float = 11; + optional double optional_double = 12; + optional bool optional_bool = 13; + optional string optional_string = 14; + optional bytes optional_bytes = 15; optional group OptionalGroup = 16 { optional int32 a = 17; } - optional ForeignMessage optional_foreign_message = 19; - optional ForeignEnum optional_foreign_enum = 22; + optional ForeignMessage optional_foreign_message = 19; + optional ForeignEnum optional_foreign_enum = 22; // Repeated - repeated int32 repeated_int32 = 31; - repeated int64 repeated_int64 = 32; - repeated uint32 repeated_uint32 = 33; - repeated uint64 repeated_uint64 = 34; - repeated sint32 repeated_sint32 = 35; - repeated sint64 repeated_sint64 = 36; - repeated fixed32 repeated_fixed32 = 37; - repeated fixed64 repeated_fixed64 = 38; + repeated int32 repeated_int32 = 31; + repeated int64 repeated_int64 = 32; + repeated uint32 repeated_uint32 = 33; + repeated uint64 repeated_uint64 = 34; + repeated sint32 repeated_sint32 = 35; + repeated sint64 repeated_sint64 = 36; + repeated fixed32 repeated_fixed32 = 37; + repeated fixed64 repeated_fixed64 = 38; repeated sfixed32 repeated_sfixed32 = 39; repeated sfixed64 repeated_sfixed64 = 40; - repeated float repeated_float = 41; - repeated double repeated_double = 42; - repeated bool repeated_bool = 43; - repeated string repeated_string = 44; - repeated bytes repeated_bytes = 45; + repeated float repeated_float = 41; + repeated double repeated_double = 42; + repeated bool repeated_bool = 43; + repeated string repeated_string = 44; + repeated bytes repeated_bytes = 45; repeated group RepeatedGroup = 46 { optional int32 a = 47; } - repeated ForeignMessage repeated_foreign_message = 49; - repeated ForeignEnum repeated_foreign_enum = 52; + repeated ForeignMessage repeated_foreign_message = 49; + repeated ForeignEnum repeated_foreign_enum = 52; // Packed repeated - repeated int32 packed_repeated_int32 = 61 [packed=true]; - repeated int64 packed_repeated_int64 = 62 [packed=true]; - repeated uint32 packed_repeated_uint32 = 63 [packed=true]; - repeated uint64 packed_repeated_uint64 = 64 [packed=true]; - repeated sint32 packed_repeated_sint32 = 65 [packed=true]; - repeated sint64 packed_repeated_sint64 = 66 [packed=true]; - repeated fixed32 packed_repeated_fixed32 = 67 [packed=true]; - repeated fixed64 packed_repeated_fixed64 = 68 [packed=true]; - repeated sfixed32 packed_repeated_sfixed32 = 69 [packed=true]; - repeated sfixed64 packed_repeated_sfixed64 = 70 [packed=true]; - repeated float packed_repeated_float = 71 [packed=true]; - repeated double packed_repeated_double = 72 [packed=true]; - repeated bool packed_repeated_bool = 73 [packed=true]; + repeated int32 packed_repeated_int32 = 61 [packed = true]; + repeated int64 packed_repeated_int64 = 62 [packed = true]; + repeated uint32 packed_repeated_uint32 = 63 [packed = true]; + repeated uint64 packed_repeated_uint64 = 64 [packed = true]; + repeated sint32 packed_repeated_sint32 = 65 [packed = true]; + repeated sint64 packed_repeated_sint64 = 66 [packed = true]; + repeated fixed32 packed_repeated_fixed32 = 67 [packed = true]; + repeated fixed64 packed_repeated_fixed64 = 68 [packed = true]; + repeated sfixed32 packed_repeated_sfixed32 = 69 [packed = true]; + repeated sfixed64 packed_repeated_sfixed64 = 70 [packed = true]; + repeated float packed_repeated_float = 71 [packed = true]; + repeated double packed_repeated_double = 72 [packed = true]; + repeated bool packed_repeated_bool = 73 [packed = true]; oneof oneof_field { uint32 oneof_uint32 = 111; @@ -132,55 +132,54 @@ message ExtendsWithMessage { } extend TestExtendable { - optional int32 extend_optional_int32 = 1; - optional int64 extend_optional_int64 = 2; - optional uint32 extend_optional_uint32 = 3; - optional uint64 extend_optional_uint64 = 4; - optional sint32 extend_optional_sint32 = 5; - optional sint64 extend_optional_sint64 = 6; - optional fixed32 extend_optional_fixed32 = 7; - optional fixed64 extend_optional_fixed64 = 8; - optional sfixed32 extend_optional_sfixed32 = 9; + optional int32 extend_optional_int32 = 1; + optional int64 extend_optional_int64 = 2; + optional uint32 extend_optional_uint32 = 3; + optional uint64 extend_optional_uint64 = 4; + optional sint32 extend_optional_sint32 = 5; + optional sint64 extend_optional_sint64 = 6; + optional fixed32 extend_optional_fixed32 = 7; + optional fixed64 extend_optional_fixed64 = 8; + optional sfixed32 extend_optional_sfixed32 = 9; optional sfixed64 extend_optional_sfixed64 = 10; - optional float extend_optional_float = 11; - optional double extend_optional_double = 12; - optional bool extend_optional_bool = 13; - optional string extend_optional_string = 14; - optional bytes extend_optional_bytes = 15; - optional ForeignEnum extend_optional_foreign_enum = 22; - - repeated int32 extend_repeated_int32 = 31; - repeated int64 extend_repeated_int64 = 32; - repeated uint32 extend_repeated_uint32 = 33; - repeated uint64 extend_repeated_uint64 = 34; - repeated sint32 extend_repeated_sint32 = 35; - repeated sint64 extend_repeated_sint64 = 36; - repeated fixed32 extend_repeated_fixed32 = 37; - repeated fixed64 extend_repeated_fixed64 = 38; + optional float extend_optional_float = 11; + optional double extend_optional_double = 12; + optional bool extend_optional_bool = 13; + optional string extend_optional_string = 14; + optional bytes extend_optional_bytes = 15; + optional ForeignEnum extend_optional_foreign_enum = 22; + + repeated int32 extend_repeated_int32 = 31; + repeated int64 extend_repeated_int64 = 32; + repeated uint32 extend_repeated_uint32 = 33; + repeated uint64 extend_repeated_uint64 = 34; + repeated sint32 extend_repeated_sint32 = 35; + repeated sint64 extend_repeated_sint64 = 36; + repeated fixed32 extend_repeated_fixed32 = 37; + repeated fixed64 extend_repeated_fixed64 = 38; repeated sfixed32 extend_repeated_sfixed32 = 39; repeated sfixed64 extend_repeated_sfixed64 = 40; - repeated float extend_repeated_float = 41; - repeated double extend_repeated_double = 42; - repeated bool extend_repeated_bool = 43; - repeated string extend_repeated_string = 44; - repeated bytes extend_repeated_bytes = 45; - repeated ForeignEnum extend_repeated_foreign_enum = 52; - - repeated int32 extend_packed_repeated_int32 = 61 [packed=true]; - repeated int64 extend_packed_repeated_int64 = 62 [packed=true]; - repeated uint32 extend_packed_repeated_uint32 = 63 [packed=true]; - repeated uint64 extend_packed_repeated_uint64 = 64 [packed=true]; - repeated sint32 extend_packed_repeated_sint32 = 65 [packed=true]; - repeated sint64 extend_packed_repeated_sint64 = 66 [packed=true]; - repeated fixed32 extend_packed_repeated_fixed32 = 67 [packed=true]; - repeated fixed64 extend_packed_repeated_fixed64 = 68 [packed=true]; - repeated sfixed32 extend_packed_repeated_sfixed32 = 69 [packed=true]; - repeated sfixed64 extend_packed_repeated_sfixed64 = 70 [packed=true]; - repeated float extend_packed_repeated_float = 71 [packed=true]; - repeated double extend_packed_repeated_double = 72 [packed=true]; - repeated bool extend_packed_repeated_bool = 73 [packed=true]; - repeated ForeignEnum extend_packed_repeated_foreign_enum = 82 - [packed=true]; + repeated float extend_repeated_float = 41; + repeated double extend_repeated_double = 42; + repeated bool extend_repeated_bool = 43; + repeated string extend_repeated_string = 44; + repeated bytes extend_repeated_bytes = 45; + repeated ForeignEnum extend_repeated_foreign_enum = 52; + + repeated int32 extend_packed_repeated_int32 = 61 [packed = true]; + repeated int64 extend_packed_repeated_int64 = 62 [packed = true]; + repeated uint32 extend_packed_repeated_uint32 = 63 [packed = true]; + repeated uint64 extend_packed_repeated_uint64 = 64 [packed = true]; + repeated sint32 extend_packed_repeated_sint32 = 65 [packed = true]; + repeated sint64 extend_packed_repeated_sint64 = 66 [packed = true]; + repeated fixed32 extend_packed_repeated_fixed32 = 67 [packed = true]; + repeated fixed64 extend_packed_repeated_fixed64 = 68 [packed = true]; + repeated sfixed32 extend_packed_repeated_sfixed32 = 69 [packed = true]; + repeated sfixed64 extend_packed_repeated_sfixed64 = 70 [packed = true]; + repeated float extend_packed_repeated_float = 71 [packed = true]; + repeated double extend_packed_repeated_double = 72 [packed = true]; + repeated bool extend_packed_repeated_bool = 73 [packed = true]; + repeated ForeignEnum extend_packed_repeated_foreign_enum = 82 [packed = true]; } @@ -226,7 +225,7 @@ message MapEntryOptionalKeysBoolKey { message TestMapFieldsOptionalKeys { optional MapEntryOptionalKeysStringKey map_string_string = 1; - optional MapEntryOptionalKeysInt32Key map_int32_string= 8; + optional MapEntryOptionalKeysInt32Key map_int32_string = 8; optional MapEntryOptionalKeysInt64Key map_int64_string = 9; optional MapEntryOptionalKeysBoolKey map_bool_string = 10; } @@ -242,3 +241,4 @@ enum MapValueEnum { message MapValueMessage { optional int32 foo = 1; } + diff --git a/js/testempty.proto b/js/testempty.proto index 960bce4e5c..6161753fd5 100644 --- a/js/testempty.proto +++ b/js/testempty.proto @@ -31,4 +31,3 @@ syntax = "proto2"; package javatests.com.google.apps.jspb; - diff --git a/kokoro/linux/32-bit/build.sh b/kokoro/linux/32-bit/build.sh index 04383a5701..8c0a41ea2e 100755 --- a/kokoro/linux/32-bit/build.sh +++ b/kokoro/linux/32-bit/build.sh @@ -10,7 +10,8 @@ # Change to repo root cd $(dirname $0)/../../.. -export DOCKERFILE_DIR=kokoro/linux/32-bit +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/php_32bit export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh export OUTPUT_DIR=testoutput export TEST_SET="php_all_32" diff --git a/kokoro/linux/bazel/build.sh b/kokoro/linux/bazel/build.sh index 1769301d07..6b55ab1a47 100755 --- a/kokoro/linux/bazel/build.sh +++ b/kokoro/linux/bazel/build.sh @@ -7,7 +7,7 @@ set -ex cd $(dirname $0)/../../.. git submodule update --init --recursive -bazel test :protobuf_test +bazel test :protobuf_test --copt=-Werror --host_copt=-Werror cd examples bazel build :all diff --git a/kokoro/linux/benchmark/build.sh b/kokoro/linux/benchmark/build.sh old mode 100755 new mode 100644 index 3463ba9bf5..e563e79166 --- a/kokoro/linux/benchmark/build.sh +++ b/kokoro/linux/benchmark/build.sh @@ -1,92 +1,26 @@ #!/bin/bash -# -# Change to repo root -cd $(dirname $0)/../../.. - -export OUTPUT_DIR=testoutput -oldpwd=`pwd` - -# tcmalloc -if [ ! -f gperftools/.libs/libtcmalloc.so ]; then - git clone https://github.com/gperftools/gperftools.git - cd gperftools - ./autogen.sh - ./configure - make -j8 - cd .. -fi - -# download datasets for benchmark -cd benchmarks -./download_data.sh -datasets=`find . -type f -name "dataset.*.pb"` -cd $oldpwd - -# build Python protobuf -./autogen.sh -./configure CXXFLAGS="-fPIC -O2" -make -j8 -cd python -python setup.py build --cpp_implementation -pip install . --user - - -# build and run Python benchmark -cd ../benchmarks -make python-pure-python-benchmark -make python-cpp-reflection-benchmark -make -j8 python-cpp-generated-code-benchmark -echo "[" > tmp/python_result.json -echo "benchmarking pure python..." -./python-pure-python-benchmark --json --behavior_prefix="pure-python-benchmark" $datasets >> tmp/python_result.json -echo "," >> "tmp/python_result.json" -echo "benchmarking python cpp reflection..." -env LD_PRELOAD="$oldpwd/gperftools/.libs/libtcmalloc.so" LD_LIBRARY_PATH="$oldpwd/src/.libs" ./python-cpp-reflection-benchmark --json --behavior_prefix="cpp-reflection-benchmark" $datasets >> tmp/python_result.json -echo "," >> "tmp/python_result.json" -echo "benchmarking python cpp generated code..." -env LD_PRELOAD="$oldpwd/gperftools/.libs/libtcmalloc.so" LD_LIBRARY_PATH="$oldpwd/src/.libs" ./python-cpp-generated-code-benchmark --json --behavior_prefix="cpp-generated-code-benchmark" $datasets >> tmp/python_result.json -echo "]" >> "tmp/python_result.json" -cd $oldpwd -# build CPP protobuf -./configure -make clean && make -j8 - -# build Java protobuf -cd java -mvn package -cd .. - -# build CPP benchmark -cd benchmarks -mv tmp/python_result.json . && make clean && make -j8 cpp-benchmark && mv python_result.json tmp -echo "benchmarking cpp..." -env LD_PRELOAD="$oldpwd/gperftools/.libs/libtcmalloc.so" ./cpp-benchmark --benchmark_min_time=5.0 --benchmark_out_format=json --benchmark_out="tmp/cpp_result.json" $datasets -cd $oldpwd - -# build go protobuf -export PATH="`pwd`/src:$PATH" -export GOPATH="$HOME/gocode" -mkdir -p "$GOPATH/src/github.com/google" -rm -f "$GOPATH/src/github.com/google/protobuf" -ln -s "`pwd`" "$GOPATH/src/github.com/google/protobuf" -export PATH="$GOPATH/bin:$PATH" -go get github.com/golang/protobuf/protoc-gen-go - -# build go benchmark -cd benchmarks -make go-benchmark -echo "benchmarking go..." -./go-benchmark $datasets > tmp/go_result.txt - -# build java benchmark -make java-benchmark -echo "benchmarking java..." -./java-benchmark -Cresults.file.options.file="tmp/java_result.json" $datasets - -# upload result to bq -make python_add_init -env LD_LIBRARY_PATH="$oldpwd/src/.libs" python -m util.result_uploader -cpp="../tmp/cpp_result.json" -java="../tmp/java_result.json" \ - -python="../tmp/python_result.json" -go="../tmp/go_result.txt" +cd $(dirname $0)/../../.. -cd $oldpwd +# prepare php environments +sudo apt-get update && sudo apt-get install -y --force-yes php5 +sudo ln -sf /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h +mkdir php_temp +cd php_temp +curl -sS https://getcomposer.org/installer | php +sudo mv composer.phar /usr/local/bin/composer +git clone https://github.com/php/php-src +cd php-src && git checkout PHP-7.2.13 && ./buildconf --force +./configure \ + --enable-bcmatch \ + --with-gmp --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.2 && \ +make -j8 && sudo make install && make clean +wget -O phpunit https://phar.phpunit.de/phpunit-7.phar && \ + chmod +x phpunit && \ + sudo cp phpunit /usr/local/php-7.2/bin +sudo apt-get install -y --force-yes valgrind +cd ../.. + +./tests.sh benchmark diff --git a/kokoro/linux/benchmark/continuous.cfg b/kokoro/linux/benchmark/continuous.cfg index a3558c6557..8cc8f96bfa 100755 --- a/kokoro/linux/benchmark/continuous.cfg +++ b/kokoro/linux/benchmark/continuous.cfg @@ -2,7 +2,7 @@ # Location of the build script in repository build_file: "protobuf/kokoro/linux/benchmark/build.sh" -timeout_mins: 240 +timeout_mins: 240 action { define_artifacts { diff --git a/kokoro/linux/benchmark/run.sh b/kokoro/linux/benchmark/run.sh new file mode 100755 index 0000000000..1b1032bd37 --- /dev/null +++ b/kokoro/linux/benchmark/run.sh @@ -0,0 +1,105 @@ +#!/bin/bash +# +# Change to repo root +cd $(dirname $0)/../../.. + +export OUTPUT_DIR=testoutput +oldpwd=`pwd` + +# tcmalloc +if [ ! -f gperftools/.libs/libtcmalloc.so ]; then + git clone https://github.com/gperftools/gperftools.git + cd gperftools + ./autogen.sh + ./configure + make -j8 + cd .. +fi + +# download datasets for benchmark +cd benchmarks +./download_data.sh +datasets=$(for file in $(find . -type f -name "dataset.*.pb" -not -path "./tmp/*"); do echo "$(pwd)/$file"; done | xargs) +echo $datasets +cd $oldpwd + +# build Python protobuf +./autogen.sh +./configure CXXFLAGS="-fPIC -O2" +make -j8 +cd python +python setup.py build --cpp_implementation +pip install . --user + + +# build and run Python benchmark +cd ../benchmarks +make python-pure-python-benchmark +make python-cpp-reflection-benchmark +make -j8 python-cpp-generated-code-benchmark +echo "[" > tmp/python_result.json +echo "benchmarking pure python..." +./python-pure-python-benchmark --json --behavior_prefix="pure-python-benchmark" $datasets >> tmp/python_result.json +echo "," >> "tmp/python_result.json" +echo "benchmarking python cpp reflection..." +env LD_PRELOAD="$oldpwd/gperftools/.libs/libtcmalloc.so" LD_LIBRARY_PATH="$oldpwd/src/.libs" ./python-cpp-reflection-benchmark --json --behavior_prefix="cpp-reflection-benchmark" $datasets >> tmp/python_result.json +echo "," >> "tmp/python_result.json" +echo "benchmarking python cpp generated code..." +env LD_PRELOAD="$oldpwd/gperftools/.libs/libtcmalloc.so" LD_LIBRARY_PATH="$oldpwd/src/.libs" ./python-cpp-generated-code-benchmark --json --behavior_prefix="cpp-generated-code-benchmark" $datasets >> tmp/python_result.json +echo "]" >> "tmp/python_result.json" +cd $oldpwd + +# build CPP protobuf +./configure +make clean && make -j8 + +# build Java protobuf +cd java +mvn package +cd .. + +# build CPP benchmark +cd benchmarks +mv tmp/python_result.json . && make clean && make -j8 cpp-benchmark && mv python_result.json tmp +echo "benchmarking cpp..." +env LD_PRELOAD="$oldpwd/gperftools/.libs/libtcmalloc.so" ./cpp-benchmark --benchmark_min_time=5.0 --benchmark_out_format=json --benchmark_out="tmp/cpp_result.json" $datasets +cd $oldpwd + +# build go protobuf +export PATH="`pwd`/src:$PATH" +export GOPATH="$HOME/gocode" +mkdir -p "$GOPATH/src/github.com/google" +rm -f "$GOPATH/src/github.com/protocolbuffers/protobuf" +ln -s "`pwd`" "$GOPATH/src/github.com/protocolbuffers/protobuf" +export PATH="$GOPATH/bin:$PATH" +go get github.com/golang/protobuf/protoc-gen-go + +# build go benchmark +cd benchmarks +make go-benchmark +echo "benchmarking go..." +./go-benchmark $datasets > tmp/go_result.txt + +# build java benchmark +make java-benchmark +echo "benchmarking java..." +./java-benchmark -Cresults.file.options.file="tmp/java_result.json" $datasets + +make js-benchmark +echo "benchmarking js..." +./js-benchmark $datasets --json_output=$(pwd)/tmp/node_result.json + +make -j8 generate_proto3_data +proto3_datasets=$(for file in $datasets; do echo $(pwd)/tmp/proto3_data/${file#$(pwd)}; done | xargs) +echo $proto3_datasets + +# build php benchmark +make -j8 php-c-benchmark +echo "benchmarking php_c..." +./php-c-benchmark $proto3_datasets --json --behavior_prefix="php_c" > tmp/php_c_result.json + +# upload result to bq +make python_add_init +env LD_LIBRARY_PATH="$oldpwd/src/.libs" python -m util.result_uploader -php_c="../tmp/php_c_result.json" \ + -cpp="../tmp/cpp_result.json" -java="../tmp/java_result.json" -go="../tmp/go_result.txt" -python="../tmp/python_result.json" -node="../tmp/node_result.json" +cd $oldpwd diff --git a/kokoro/linux/build_and_run_docker.sh b/kokoro/linux/build_and_run_docker.sh index b81a3684b7..3dc5dbb82f 100755 --- a/kokoro/linux/build_and_run_docker.sh +++ b/kokoro/linux/build_and_run_docker.sh @@ -3,6 +3,8 @@ # Builds docker image and runs a command under it. # This is a generic script that is configured with the following variables: # +# DOCKERHUB_ORGANIZATION - The organization on docker hub storing the +# Dockerfile. # DOCKERFILE_DIR - Directory in which Dockerfile file is located. # DOCKER_RUN_SCRIPT - Script to run under docker (relative to protobuf repo root) # OUTPUT_DIR - Directory that will be copied from inside docker after finishing. @@ -15,8 +17,16 @@ git_root=$(pwd) cd - # Use image name based on Dockerfile sha1 -DOCKERHUB_ORGANIZATION=grpctesting/protobuf -DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ ) +if [ -z "$DOCKERHUB_ORGANIZATION" ] +then + DOCKERHUB_ORGANIZATION=grpctesting/protobuf + DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ ) +else + # TODO(teboring): Remove this when all tests have been migrated to separate + # docker images. + DOCKERFILE_PREFIX=$(basename $DOCKERFILE_DIR) + DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}/${DOCKERFILE_PREFIX}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ ) +fi # Pull dockerimage from Dockerhub docker pull $DOCKER_IMAGE_NAME diff --git a/kokoro/linux/cpp_distcheck/build.sh b/kokoro/linux/cpp_distcheck/build.sh index 89b8379ed0..1343a8cc84 100755 --- a/kokoro/linux/cpp_distcheck/build.sh +++ b/kokoro/linux/cpp_distcheck/build.sh @@ -2,6 +2,8 @@ # # Build file to set up and run tests +set -ex # exit immediately on error + # Change to repo root cd $(dirname $0)/../../.. diff --git a/kokoro/linux/cpp_tcmalloc/build.sh b/kokoro/linux/cpp_tcmalloc/build.sh new file mode 100755 index 0000000000..3c77b11e70 --- /dev/null +++ b/kokoro/linux/cpp_tcmalloc/build.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# +# Build file to set up and run tests + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/cpp_tcmalloc +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="cpp_tcmalloc" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/cpp_tcmalloc/continuous.cfg b/kokoro/linux/cpp_tcmalloc/continuous.cfg new file mode 100644 index 0000000000..46312a9be2 --- /dev/null +++ b/kokoro/linux/cpp_tcmalloc/continuous.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/cpp_tcmalloc/build.sh" +timeout_mins: 1440 diff --git a/kokoro/linux/cpp_tcmalloc/presubmit.cfg b/kokoro/linux/cpp_tcmalloc/presubmit.cfg new file mode 100644 index 0000000000..46312a9be2 --- /dev/null +++ b/kokoro/linux/cpp_tcmalloc/presubmit.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/cpp_tcmalloc/build.sh" +timeout_mins: 1440 diff --git a/kokoro/linux/csharp/build.sh b/kokoro/linux/csharp/build.sh index de178b844d..9a2eb48621 100755 --- a/kokoro/linux/csharp/build.sh +++ b/kokoro/linux/csharp/build.sh @@ -1,11 +1,18 @@ #!/bin/bash # -# Build file to set up and run tests +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. # Change to repo root cd $(dirname $0)/../../.. -# Prepare worker environment to run tests -source kokoro/linux/prepare_build_linux_rc - -./tests.sh csharp +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/csharp +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="csharp" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/dist_install/build.sh b/kokoro/linux/dist_install/build.sh new file mode 100755 index 0000000000..81a0516aa8 --- /dev/null +++ b/kokoro/linux/dist_install/build.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# +# Build file to set up and run tests + +set -ex # exit immediately on error + +# Change to repo root +cd $(dirname $0)/../../.. + +# Run tests under release docker image. +export DOCKERFILE_DIR=kokoro/linux/64-bit +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="dist_install" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/dist_install/continuous.cfg b/kokoro/linux/dist_install/continuous.cfg new file mode 100644 index 0000000000..b1e0b2013d --- /dev/null +++ b/kokoro/linux/dist_install/continuous.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/dist_install/build.sh" +timeout_mins: 1440 diff --git a/kokoro/linux/dist_install/presubmit.cfg b/kokoro/linux/dist_install/presubmit.cfg new file mode 100644 index 0000000000..b1e0b2013d --- /dev/null +++ b/kokoro/linux/dist_install/presubmit.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/dist_install/build.sh" +timeout_mins: 1440 diff --git a/kokoro/linux/dockerfile/push_testing_images.sh b/kokoro/linux/dockerfile/push_testing_images.sh new file mode 100755 index 0000000000..9a2983c829 --- /dev/null +++ b/kokoro/linux/dockerfile/push_testing_images.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +set -ex + +cd $(dirname $0)/../../.. +git_root=$(pwd) +cd kokoro/linux/dockerfile + +DOCKERHUB_ORGANIZATION=protobuftesting + +for DOCKERFILE_DIR in test/* release/* +do + # Generate image name based on Dockerfile checksum. That works well as long + # as can count on dockerfiles being written in a way that changing the logical + # contents of the docker image always changes the SHA (e.g. using "ADD file" + # cmd in the dockerfile in not ok as contents of the added file will not be + # reflected in the SHA). + DOCKER_IMAGE_NAME=$(basename $DOCKERFILE_DIR)_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ ) + + echo $DOCKER_IMAGE_NAME + # skip the image if it already exists in the repo + curl --silent -f -lSL https://registry.hub.docker.com/v2/repositories/${DOCKERHUB_ORGANIZATION}/${DOCKER_IMAGE_NAME}/tags/latest > /dev/null \ + && continue + + docker build -t ${DOCKERHUB_ORGANIZATION}/${DOCKER_IMAGE_NAME} ${DOCKERFILE_DIR} + + # "docker login" needs to be run in advance + docker push ${DOCKERHUB_ORGANIZATION}/${DOCKER_IMAGE_NAME} +done diff --git a/kokoro/linux/dockerfile/release/ruby_rake_compiler/Dockerfile b/kokoro/linux/dockerfile/release/ruby_rake_compiler/Dockerfile new file mode 100644 index 0000000000..866b289343 --- /dev/null +++ b/kokoro/linux/dockerfile/release/ruby_rake_compiler/Dockerfile @@ -0,0 +1,3 @@ +FROM grpctesting/rake-compiler-dock_53c22085d091183c528303791e7771359f699bcf + +RUN /bin/bash -l -c "gem update --system '2.7.9' && gem install bundler" diff --git a/kokoro/linux/dockerfile/test/cpp_tcmalloc/Dockerfile b/kokoro/linux/dockerfile/test/cpp_tcmalloc/Dockerfile new file mode 100644 index 0000000000..0ce9982ad4 --- /dev/null +++ b/kokoro/linux/dockerfile/test/cpp_tcmalloc/Dockerfile @@ -0,0 +1,29 @@ +FROM debian:jessie + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get update && apt-get install -y \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + +# Install dependencies for TC malloc +RUN apt-get install -y \ + google-perftools \ + libgoogle-perftools4 \ + libgoogle-perftools-dev diff --git a/kokoro/linux/dockerfile/test/csharp/Dockerfile b/kokoro/linux/dockerfile/test/csharp/Dockerfile new file mode 100644 index 0000000000..2073057bc0 --- /dev/null +++ b/kokoro/linux/dockerfile/test/csharp/Dockerfile @@ -0,0 +1,36 @@ +FROM debian:stretch + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get update && apt-get install -y \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + +# dotnet SDK prerequisites +RUN apt-get update && apt-get install -y libunwind8 libicu57 && apt-get clean + +# Install dotnet SDK via install script +RUN wget -q https://dot.net/v1/dotnet-install.sh && \ + chmod u+x dotnet-install.sh && \ + ./dotnet-install.sh --version 2.1.504 && \ + ln -s /root/.dotnet/dotnet /usr/local/bin + +RUN wget -q www.nuget.org/NuGet.exe -O /usr/local/bin/nuget.exe + +ENV DOTNET_SKIP_FIRST_TIME_EXPERIENCE true diff --git a/kokoro/linux/dockerfile/test/java_stretch/Dockerfile b/kokoro/linux/dockerfile/test/java_stretch/Dockerfile new file mode 100644 index 0000000000..fc5e82abdb --- /dev/null +++ b/kokoro/linux/dockerfile/test/java_stretch/Dockerfile @@ -0,0 +1,31 @@ +FROM debian:stretch + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get update && apt-get install -y \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + +# Java dependencies +RUN apt-get install -y \ + # -- For all Java builds -- \ + maven \ + # -- For java_linkage_monitor \ + openjdk-8-jdk \ + && apt-get clean diff --git a/kokoro/linux/dockerfile/test/javascript/Dockerfile b/kokoro/linux/dockerfile/test/javascript/Dockerfile new file mode 100644 index 0000000000..dff7e7bec1 --- /dev/null +++ b/kokoro/linux/dockerfile/test/javascript/Dockerfile @@ -0,0 +1,33 @@ +FROM ubuntu:latest + +RUN apt-get update && apt-get install -y gnupg + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get clean && apt-get update && apt-get install -y --force-yes \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + + +################## +# Javascript dependencies. +RUN apt-get install -y \ + # -- For javascript and closure compiler -- \ + npm \ + default-jre diff --git a/kokoro/linux/dockerfile/test/php/Dockerfile b/kokoro/linux/dockerfile/test/php/Dockerfile new file mode 100644 index 0000000000..276cb73a41 --- /dev/null +++ b/kokoro/linux/dockerfile/test/php/Dockerfile @@ -0,0 +1,238 @@ +FROM debian:jessie + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get update && apt-get install -y \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + +# Install php dependencies +RUN apt-get clean && apt-get update && apt-get install -y --force-yes \ + php5 \ + libcurl4-openssl-dev \ + libgmp-dev \ + libgmp3-dev \ + libssl-dev \ + libxml2-dev \ + unzip \ + zlib1g-dev \ + pkg-config \ + && apt-get clean + +# Install other dependencies +RUN ln -sf /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h +RUN wget http://ftp.gnu.org/gnu/bison/bison-2.6.4.tar.gz -O /var/local/bison-2.6.4.tar.gz +RUN cd /var/local \ + && tar -zxvf bison-2.6.4.tar.gz \ + && cd /var/local/bison-2.6.4 \ + && ./configure \ + && make \ + && make install + +# Install composer +RUN curl -sS https://getcomposer.org/installer | php +RUN mv composer.phar /usr/local/bin/composer + +# Download php source code +RUN git clone https://github.com/php/php-src + +# php 5.5 +RUN cd php-src \ + && git checkout PHP-5.5.38 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-5.5 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-5.5-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-4.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-5.5/bin \ + && mv phpunit /usr/local/php-5.5-zts/bin + +# php 5.6 +RUN cd php-src \ + && git checkout PHP-5.6.39 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-5.6 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-5.6-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-5.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-5.6/bin \ + && mv phpunit /usr/local/php-5.6-zts/bin + +# php 7.0 +RUN cd php-src \ + && git checkout PHP-7.0.33 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.0 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.0-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-6.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-7.0/bin \ + && mv phpunit /usr/local/php-7.0-zts/bin + +# php 7.1 +RUN cd php-src \ + && git checkout PHP-7.1.25 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.1 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.1-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-7.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-7.1/bin \ + && mv phpunit /usr/local/php-7.1-zts/bin + +# php 7.2 +RUN cd php-src \ + && git checkout PHP-7.2.13 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.2 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.2-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-7.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-7.2/bin \ + && mv phpunit /usr/local/php-7.2-zts/bin + +# php 7.3 +RUN cd php-src \ + && git checkout PHP-7.3.0 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.3 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-gmp \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.3-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-7.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-7.3/bin \ + && mv phpunit /usr/local/php-7.3-zts/bin + +# Install php dependencies +RUN apt-get clean && apt-get update && apt-get install -y --force-yes \ + valgrind \ + && apt-get clean diff --git a/kokoro/linux/dockerfile/test/php_32bit/Dockerfile b/kokoro/linux/dockerfile/test/php_32bit/Dockerfile new file mode 100644 index 0000000000..f8027c48e7 --- /dev/null +++ b/kokoro/linux/dockerfile/test/php_32bit/Dockerfile @@ -0,0 +1,224 @@ +FROM 32bit/debian:jessie + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get update && apt-get install -y \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + +# Install php dependencies +RUN apt-get clean && apt-get update && apt-get install -y --force-yes \ + bison \ + php5 \ + libcurl4-openssl-dev \ + libssl-dev \ + libxml2-dev \ + unzip \ + zlib1g-dev \ + pkg-config \ + && apt-get clean + +# Install other dependencies +RUN wget http://ftp.gnu.org/gnu/bison/bison-2.6.4.tar.gz -O /var/local/bison-2.6.4.tar.gz +RUN cd /var/local \ + && tar -zxvf bison-2.6.4.tar.gz \ + && cd /var/local/bison-2.6.4 \ + && ./configure \ + && make \ + && make install + +# Install composer +RUN curl -sS https://getcomposer.org/installer | php +RUN mv composer.phar /usr/local/bin/composer + +# Download php source code +RUN git clone https://github.com/php/php-src + +# php 5.5 +RUN cd php-src \ + && git checkout PHP-5.5.38 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-5.5 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-5.5-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-4.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-5.5/bin \ + && mv phpunit /usr/local/php-5.5-zts/bin + +# php 5.6 +RUN cd php-src \ + && git checkout PHP-5.6.39 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-5.6 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-5.6-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-5.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-5.6/bin \ + && mv phpunit /usr/local/php-5.6-zts/bin + +# php 7.0 +RUN cd php-src \ + && git checkout PHP-7.0.33 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.0 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.0-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-6.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-7.0/bin \ + && mv phpunit /usr/local/php-7.0-zts/bin + +# php 7.1 +RUN cd php-src \ + && git checkout PHP-7.1.25 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.1 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.1-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-7.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-7.1/bin \ + && mv phpunit /usr/local/php-7.1-zts/bin + +# php 7.2 +RUN cd php-src \ + && git checkout PHP-7.2.13 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.2 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.2-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-7.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-7.2/bin \ + && mv phpunit /usr/local/php-7.2-zts/bin + +# php 7.3 +RUN cd php-src \ + && git checkout PHP-7.3.0 \ + && ./buildconf --force +RUN cd php-src \ + && ./configure \ + --enable-bcmath \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.3 \ + && make \ + && make install \ + && make clean +RUN cd php-src \ + && ./configure \ + --enable-maintainer-zts \ + --with-openssl \ + --with-zlib \ + --prefix=/usr/local/php-7.3-zts \ + && make \ + && make install \ + && make clean + +RUN wget -O phpunit https://phar.phpunit.de/phpunit-7.phar \ + && chmod +x phpunit \ + && cp phpunit /usr/local/php-7.3/bin \ + && mv phpunit /usr/local/php-7.3-zts/bin + +# Install php dependencies +RUN apt-get clean && apt-get update && apt-get install -y --force-yes \ + valgrind \ + && apt-get clean diff --git a/kokoro/linux/dockerfile/test/python_jessie/Dockerfile b/kokoro/linux/dockerfile/test/python_jessie/Dockerfile new file mode 100644 index 0000000000..b5a53a3e5f --- /dev/null +++ b/kokoro/linux/dockerfile/test/python_jessie/Dockerfile @@ -0,0 +1,39 @@ +FROM debian:jessie + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get update && apt-get install -y \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + +# Install python dependencies +RUN apt-get update && apt-get install -y \ + python-setuptools \ + python-all-dev \ + python3-all-dev \ + python-pip + +# Install Python packages from PyPI +RUN pip install --upgrade pip==10.0.1 +RUN pip install virtualenv +RUN pip install six==1.10.0 twisted==17.5.0 + +# Install pip and virtualenv for Python 3.4 +RUN curl https://bootstrap.pypa.io/get-pip.py | python3.4 +RUN python3.4 -m pip install virtualenv diff --git a/kokoro/linux/dockerfile/test/python_stretch/Dockerfile b/kokoro/linux/dockerfile/test/python_stretch/Dockerfile new file mode 100644 index 0000000000..1dba53087f --- /dev/null +++ b/kokoro/linux/dockerfile/test/python_stretch/Dockerfile @@ -0,0 +1,47 @@ +FROM debian:stretch + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get update && apt-get install -y \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + +# Install Python 2.7 +RUN apt-get update && apt-get install -y python2.7 python-all-dev +RUN curl https://bootstrap.pypa.io/get-pip.py | python2.7 + +# Install python dependencies +RUN apt-get update && apt-get install -y \ + python-setuptools \ + python-pip + +# Add Debian 'testing' repository +RUN echo 'deb http://ftp.de.debian.org/debian testing main' >> /etc/apt/sources.list +RUN echo 'APT::Default-Release "stable";' | tee -a /etc/apt/apt.conf.d/00local + +# Install Python3 +RUN apt-get update && apt-get -t testing install -y \ + python3.5 \ + python3.6 \ + python3.7 \ + python3-all-dev + +RUN curl https://bootstrap.pypa.io/get-pip.py | python3.5 +RUN curl https://bootstrap.pypa.io/get-pip.py | python3.6 +RUN curl https://bootstrap.pypa.io/get-pip.py | python3.7 diff --git a/kokoro/linux/dockerfile/test/ruby/Dockerfile b/kokoro/linux/dockerfile/test/ruby/Dockerfile new file mode 100644 index 0000000000..41bfedeb33 --- /dev/null +++ b/kokoro/linux/dockerfile/test/ruby/Dockerfile @@ -0,0 +1,37 @@ +FROM debian:jessie + +# Install dependencies. We start with the basic ones require to build protoc +# and the C++ build +RUN apt-get update && apt-get install -y \ + autoconf \ + autotools-dev \ + build-essential \ + bzip2 \ + ccache \ + curl \ + gcc \ + git \ + libc6 \ + libc6-dbg \ + libc6-dev \ + libgtest-dev \ + libtool \ + make \ + parallel \ + time \ + wget \ + && apt-get clean + +# Install rvm +RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys \ + 409B6B1796C275462A1703113804BB82D39DC0E3 \ + 7D2BAF1CF37B13E2069D6956105BD0E739499BDB +RUN \curl -sSL https://get.rvm.io | bash -s stable + +RUN /bin/bash -l -c "rvm install 2.3.8" +RUN /bin/bash -l -c "rvm install 2.4.5" +RUN /bin/bash -l -c "rvm install 2.5.1" +RUN /bin/bash -l -c "rvm install 2.6.0" + +RUN /bin/bash -l -c "echo 'gem: --no-ri --no-rdoc' > ~/.gemrc" +RUN /bin/bash -l -c "echo 'export PATH=/usr/local/rvm/bin:$PATH' >> ~/.bashrc" diff --git a/kokoro/linux/java_compatibility/build.sh b/kokoro/linux/java_compatibility/build.sh index b1ef27963f..de88454fc7 100755 --- a/kokoro/linux/java_compatibility/build.sh +++ b/kokoro/linux/java_compatibility/build.sh @@ -1,11 +1,17 @@ #!/bin/bash # -# Build file to set up and run tests +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. # Change to repo root cd $(dirname $0)/../../.. -# Prepare worker environment to run tests -source kokoro/linux/prepare_build_linux_rc - -./tests.sh java_compatibility +export DOCKERFILE_DIR=kokoro/linux/64-bit +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="java_compatibility" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/java_linkage_monitor/build.sh b/kokoro/linux/java_linkage_monitor/build.sh new file mode 100755 index 0000000000..c447453f60 --- /dev/null +++ b/kokoro/linux/java_linkage_monitor/build.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +# The image of the Dockerfile sha1 is fetched from the organization +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/java_stretch +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="java_linkage_monitor" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/java_linkage_monitor/continuous.cfg b/kokoro/linux/java_linkage_monitor/continuous.cfg new file mode 100644 index 0000000000..d0486cce41 --- /dev/null +++ b/kokoro/linux/java_linkage_monitor/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/java_linkage_monitor/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/java_linkage_monitor/presubmit.cfg b/kokoro/linux/java_linkage_monitor/presubmit.cfg new file mode 100644 index 0000000000..c02a52f0fa --- /dev/null +++ b/kokoro/linux/java_linkage_monitor/presubmit.cfg @@ -0,0 +1,12 @@ +# Config file for running Linkage Monitor in Kokoro +# https://github.com/GoogleCloudPlatform/cloud-opensource-java/tree/master/linkage-monitor + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/java_linkage_monitor/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/javascript/build.sh b/kokoro/linux/javascript/build.sh index c2585fb793..e1e172310c 100755 --- a/kokoro/linux/javascript/build.sh +++ b/kokoro/linux/javascript/build.sh @@ -10,7 +10,8 @@ # Change to repo root cd $(dirname $0)/../../.. -export DOCKERFILE_DIR=kokoro/linux/64-bit +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/javascript export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh export OUTPUT_DIR=testoutput export TEST_SET="javascript" diff --git a/kokoro/linux/php_all/build.sh b/kokoro/linux/php_all/build.sh index e9ab84cb5d..23468a7010 100755 --- a/kokoro/linux/php_all/build.sh +++ b/kokoro/linux/php_all/build.sh @@ -10,7 +10,8 @@ # Change to repo root cd $(dirname $0)/../../.. -export DOCKERFILE_DIR=kokoro/linux/64-bit +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/php export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh export OUTPUT_DIR=testoutput export TEST_SET="php_all" diff --git a/kokoro/linux/prepare_build_linux_rc b/kokoro/linux/prepare_build_linux_rc deleted file mode 100644 index d9b3bec3aa..0000000000 --- a/kokoro/linux/prepare_build_linux_rc +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -# Source this rc script to prepare the environment for Linux builds - -# Set up dotnet -sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-trusty-prod trusty main" > /etc/apt/sources.list.d/dotnetdev.list' -sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EB3E94ADBE1229CF -sudo apt-get update -# We use the .NET Core SDK 2.x to build... -sudo apt-get install -y dotnet-sdk-2.1.3 -# But we also need the 1.x framework to test against, as we -# target netstandard1.x -sudo apt-get install -y dotnet-sharedframework-microsoft.netcore.app-1.0.5 diff --git a/kokoro/linux/64-bit/build.sh b/kokoro/linux/python27/build.sh similarity index 73% rename from kokoro/linux/64-bit/build.sh rename to kokoro/linux/python27/build.sh index 48ddbce532..8c40ba3604 100755 --- a/kokoro/linux/64-bit/build.sh +++ b/kokoro/linux/python27/build.sh @@ -10,8 +10,9 @@ # Change to repo root cd $(dirname $0)/../../.. -export DOCKERFILE_DIR=kokoro/linux/64-bit +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_jessie export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh export OUTPUT_DIR=testoutput -export TEST_SET="csharp java_jdk7 javanano_jdk7 java_oracle7 javanano_oracle7 python python_cpp ruby_all javascript golang php_all" +export TEST_SET="python27" ./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/64-bit/continuous.cfg b/kokoro/linux/python27/continuous.cfg similarity index 76% rename from kokoro/linux/64-bit/continuous.cfg rename to kokoro/linux/python27/continuous.cfg index 3a4faac117..e2fc4136f0 100644 --- a/kokoro/linux/64-bit/continuous.cfg +++ b/kokoro/linux/python27/continuous.cfg @@ -1,7 +1,7 @@ # Config file for running tests in Kokoro # Location of the build script in repository -build_file: "protobuf/kokoro/linux/64-bit/build.sh" +build_file: "protobuf/kokoro/linux/python/build.sh" timeout_mins: 120 action { diff --git a/kokoro/linux/64-bit/presubmit.cfg b/kokoro/linux/python27/presubmit.cfg similarity index 76% rename from kokoro/linux/64-bit/presubmit.cfg rename to kokoro/linux/python27/presubmit.cfg index 3a4faac117..e2fc4136f0 100644 --- a/kokoro/linux/64-bit/presubmit.cfg +++ b/kokoro/linux/python27/presubmit.cfg @@ -1,7 +1,7 @@ # Config file for running tests in Kokoro # Location of the build script in repository -build_file: "protobuf/kokoro/linux/64-bit/build.sh" +build_file: "protobuf/kokoro/linux/python/build.sh" timeout_mins: 120 action { diff --git a/kokoro/linux/python27_cpp/build.sh b/kokoro/linux/python27_cpp/build.sh new file mode 100755 index 0000000000..2ff09a21fd --- /dev/null +++ b/kokoro/linux/python27_cpp/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_jessie +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python27_cpp" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python27_cpp/continuous.cfg b/kokoro/linux/python27_cpp/continuous.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python27_cpp/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python27_cpp/presubmit.cfg b/kokoro/linux/python27_cpp/presubmit.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python27_cpp/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python33/build.sh b/kokoro/linux/python33/build.sh new file mode 100755 index 0000000000..84a9acdb73 --- /dev/null +++ b/kokoro/linux/python33/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_jessie +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python33" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/ruby_all/continuous.cfg b/kokoro/linux/python33/continuous.cfg similarity index 76% rename from kokoro/linux/ruby_all/continuous.cfg rename to kokoro/linux/python33/continuous.cfg index 02d7643728..e2fc4136f0 100644 --- a/kokoro/linux/ruby_all/continuous.cfg +++ b/kokoro/linux/python33/continuous.cfg @@ -1,7 +1,7 @@ # Config file for running tests in Kokoro # Location of the build script in repository -build_file: "protobuf/kokoro/linux/ruby_all/build.sh" +build_file: "protobuf/kokoro/linux/python/build.sh" timeout_mins: 120 action { diff --git a/kokoro/linux/ruby_all/presubmit.cfg b/kokoro/linux/python33/presubmit.cfg similarity index 76% rename from kokoro/linux/ruby_all/presubmit.cfg rename to kokoro/linux/python33/presubmit.cfg index 02d7643728..e2fc4136f0 100644 --- a/kokoro/linux/ruby_all/presubmit.cfg +++ b/kokoro/linux/python33/presubmit.cfg @@ -1,7 +1,7 @@ # Config file for running tests in Kokoro # Location of the build script in repository -build_file: "protobuf/kokoro/linux/ruby_all/build.sh" +build_file: "protobuf/kokoro/linux/python/build.sh" timeout_mins: 120 action { diff --git a/kokoro/linux/python33_cpp/build.sh b/kokoro/linux/python33_cpp/build.sh new file mode 100755 index 0000000000..ad33e89d25 --- /dev/null +++ b/kokoro/linux/python33_cpp/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_jessie +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python33_cpp" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python33_cpp/continuous.cfg b/kokoro/linux/python33_cpp/continuous.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python33_cpp/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python33_cpp/presubmit.cfg b/kokoro/linux/python33_cpp/presubmit.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python33_cpp/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python34/build.sh b/kokoro/linux/python34/build.sh new file mode 100755 index 0000000000..25dfd5dfcd --- /dev/null +++ b/kokoro/linux/python34/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_jessie +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python34" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python34/continuous.cfg b/kokoro/linux/python34/continuous.cfg new file mode 100644 index 0000000000..e2fc4136f0 --- /dev/null +++ b/kokoro/linux/python34/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python34/presubmit.cfg b/kokoro/linux/python34/presubmit.cfg new file mode 100644 index 0000000000..e2fc4136f0 --- /dev/null +++ b/kokoro/linux/python34/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python34_cpp/build.sh b/kokoro/linux/python34_cpp/build.sh new file mode 100755 index 0000000000..e4590ffbad --- /dev/null +++ b/kokoro/linux/python34_cpp/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_jessie +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python34_cpp" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python34_cpp/continuous.cfg b/kokoro/linux/python34_cpp/continuous.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python34_cpp/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python34_cpp/presubmit.cfg b/kokoro/linux/python34_cpp/presubmit.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python34_cpp/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python35/build.sh b/kokoro/linux/python35/build.sh new file mode 100755 index 0000000000..f978e2ad11 --- /dev/null +++ b/kokoro/linux/python35/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_stretch +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python35" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python35/continuous.cfg b/kokoro/linux/python35/continuous.cfg new file mode 100644 index 0000000000..e2fc4136f0 --- /dev/null +++ b/kokoro/linux/python35/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python35/presubmit.cfg b/kokoro/linux/python35/presubmit.cfg new file mode 100644 index 0000000000..e2fc4136f0 --- /dev/null +++ b/kokoro/linux/python35/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python35_cpp/build.sh b/kokoro/linux/python35_cpp/build.sh new file mode 100755 index 0000000000..2a79246d59 --- /dev/null +++ b/kokoro/linux/python35_cpp/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_stretch +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python35_cpp" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python35_cpp/continuous.cfg b/kokoro/linux/python35_cpp/continuous.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python35_cpp/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python35_cpp/presubmit.cfg b/kokoro/linux/python35_cpp/presubmit.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python35_cpp/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python36/build.sh b/kokoro/linux/python36/build.sh new file mode 100755 index 0000000000..633145bcec --- /dev/null +++ b/kokoro/linux/python36/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_stretch +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python36" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python36/continuous.cfg b/kokoro/linux/python36/continuous.cfg new file mode 100644 index 0000000000..e2fc4136f0 --- /dev/null +++ b/kokoro/linux/python36/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python36/presubmit.cfg b/kokoro/linux/python36/presubmit.cfg new file mode 100644 index 0000000000..e2fc4136f0 --- /dev/null +++ b/kokoro/linux/python36/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python36_cpp/build.sh b/kokoro/linux/python36_cpp/build.sh new file mode 100755 index 0000000000..8e120ff37c --- /dev/null +++ b/kokoro/linux/python36_cpp/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_stretch +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python36_cpp" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python36_cpp/continuous.cfg b/kokoro/linux/python36_cpp/continuous.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python36_cpp/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python36_cpp/presubmit.cfg b/kokoro/linux/python36_cpp/presubmit.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python36_cpp/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python37/build.sh b/kokoro/linux/python37/build.sh new file mode 100755 index 0000000000..554aa09448 --- /dev/null +++ b/kokoro/linux/python37/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_stretch +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python37" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python37/continuous.cfg b/kokoro/linux/python37/continuous.cfg new file mode 100644 index 0000000000..e2fc4136f0 --- /dev/null +++ b/kokoro/linux/python37/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python37/presubmit.cfg b/kokoro/linux/python37/presubmit.cfg new file mode 100644 index 0000000000..e2fc4136f0 --- /dev/null +++ b/kokoro/linux/python37/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python37_cpp/build.sh b/kokoro/linux/python37_cpp/build.sh new file mode 100755 index 0000000000..8fdc8f9339 --- /dev/null +++ b/kokoro/linux/python37_cpp/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/python_stretch +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python37_cpp" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/python37_cpp/continuous.cfg b/kokoro/linux/python37_cpp/continuous.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python37_cpp/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python37_cpp/presubmit.cfg b/kokoro/linux/python37_cpp/presubmit.cfg new file mode 100644 index 0000000000..b1b0e550ff --- /dev/null +++ b/kokoro/linux/python37_cpp/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/python_cpp/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/python_compatibility/build.sh b/kokoro/linux/python_compatibility/build.sh index 041e65ffb6..1ba35394d1 100755 --- a/kokoro/linux/python_compatibility/build.sh +++ b/kokoro/linux/python_compatibility/build.sh @@ -1,11 +1,17 @@ #!/bin/bash # -# Build file to set up and run tests +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. # Change to repo root cd $(dirname $0)/../../.. -# Prepare worker environment to run tests -source kokoro/linux/prepare_build_linux_rc - -./tests.sh python_compatibility +export DOCKERFILE_DIR=kokoro/linux/64-bit +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="python_compatibility" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/ruby_all/build.sh b/kokoro/linux/ruby23/build.sh similarity index 79% rename from kokoro/linux/ruby_all/build.sh rename to kokoro/linux/ruby23/build.sh index ea7589585d..503a320de2 100755 --- a/kokoro/linux/ruby_all/build.sh +++ b/kokoro/linux/ruby23/build.sh @@ -10,8 +10,9 @@ # Change to repo root cd $(dirname $0)/../../.. -export DOCKERFILE_DIR=kokoro/linux/64-bit +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/ruby export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh export OUTPUT_DIR=testoutput -export TEST_SET="ruby_all" +export TEST_SET="ruby23" ./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/ruby23/continuous.cfg b/kokoro/linux/ruby23/continuous.cfg new file mode 100644 index 0000000000..f5c64988bf --- /dev/null +++ b/kokoro/linux/ruby23/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/ruby23/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/ruby23/presubmit.cfg b/kokoro/linux/ruby23/presubmit.cfg new file mode 100644 index 0000000000..f5c64988bf --- /dev/null +++ b/kokoro/linux/ruby23/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/ruby23/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/ruby24/build.sh b/kokoro/linux/ruby24/build.sh new file mode 100755 index 0000000000..68585aabc0 --- /dev/null +++ b/kokoro/linux/ruby24/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/ruby +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="ruby24" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/ruby24/continuous.cfg b/kokoro/linux/ruby24/continuous.cfg new file mode 100644 index 0000000000..a1ccfc0b8b --- /dev/null +++ b/kokoro/linux/ruby24/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/ruby24/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/ruby24/presubmit.cfg b/kokoro/linux/ruby24/presubmit.cfg new file mode 100644 index 0000000000..a1ccfc0b8b --- /dev/null +++ b/kokoro/linux/ruby24/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/ruby24/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/ruby25/build.sh b/kokoro/linux/ruby25/build.sh new file mode 100755 index 0000000000..1e82ec5f4c --- /dev/null +++ b/kokoro/linux/ruby25/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/ruby +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="ruby25" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/ruby25/continuous.cfg b/kokoro/linux/ruby25/continuous.cfg new file mode 100644 index 0000000000..dee2db033f --- /dev/null +++ b/kokoro/linux/ruby25/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/ruby25/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/ruby25/presubmit.cfg b/kokoro/linux/ruby25/presubmit.cfg new file mode 100644 index 0000000000..dee2db033f --- /dev/null +++ b/kokoro/linux/ruby25/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/ruby25/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/ruby26/build.sh b/kokoro/linux/ruby26/build.sh new file mode 100755 index 0000000000..72d2d45669 --- /dev/null +++ b/kokoro/linux/ruby26/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# This is the top-level script we give to Kokoro as the entry point for +# running the "pull request" project: +# +# This script selects a specific Dockerfile (for building a Docker image) and +# a script to run inside that image. Then we delegate to the general +# build_and_run_docker.sh script. + +# Change to repo root +cd $(dirname $0)/../../.. + +export DOCKERHUB_ORGANIZATION=protobuftesting +export DOCKERFILE_DIR=kokoro/linux/dockerfile/test/ruby +export DOCKER_RUN_SCRIPT=kokoro/linux/pull_request_in_docker.sh +export OUTPUT_DIR=testoutput +export TEST_SET="ruby26" +./kokoro/linux/build_and_run_docker.sh diff --git a/kokoro/linux/ruby26/continuous.cfg b/kokoro/linux/ruby26/continuous.cfg new file mode 100644 index 0000000000..4779065670 --- /dev/null +++ b/kokoro/linux/ruby26/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/ruby26/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/linux/ruby26/presubmit.cfg b/kokoro/linux/ruby26/presubmit.cfg new file mode 100644 index 0000000000..4779065670 --- /dev/null +++ b/kokoro/linux/ruby26/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/linux/ruby26/build.sh" +timeout_mins: 120 + +action { + define_artifacts { + regex: "**/sponge_log.xml" + } +} diff --git a/kokoro/macos/prepare_build_macos_rc b/kokoro/macos/prepare_build_macos_rc index fb975523a4..6bf2025dfc 100755 --- a/kokoro/macos/prepare_build_macos_rc +++ b/kokoro/macos/prepare_build_macos_rc @@ -5,7 +5,10 @@ ## # Select Xcode version -export DEVELOPER_DIR=/Applications/Xcode_9.1.app/Contents/Developer +# Remember to udpate the Xcode version when xcode_9.4.1 is not available. +# If xcode is not available, it will probaly encounter the failure for +# "autom4te: need GNU m4 1.4 or later: /usr/bin/m4"" +export DEVELOPER_DIR=/Applications/Xcode_9.4.1.app/Contents/Developer ## # Select C/C++ compilers @@ -18,9 +21,11 @@ export CXX=g++ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" source $HOME/.rvm/scripts/rvm -brew tap homebrew/homebrew-php -brew uninstall node icu4c -brew install gflags gpg gpg2 node pcre php56 ruby +brew uninstall node icu4c cmake +brew prune +brew install gflags gpg gpg2 node openssl pcre ruby cmake +sudo chown -R $(whoami) /usr/local +brew postinstall node ## # Install Tox diff --git a/kokoro/macos/ruby21/build.sh b/kokoro/macos/ruby23/build.sh similarity index 91% rename from kokoro/macos/ruby21/build.sh rename to kokoro/macos/ruby23/build.sh index 748ea65595..dc950a20d4 100755 --- a/kokoro/macos/ruby21/build.sh +++ b/kokoro/macos/ruby23/build.sh @@ -8,4 +8,4 @@ cd $(dirname $0)/../../.. # Prepare worker environment to run tests source kokoro/macos/prepare_build_macos_rc -./tests.sh ruby21 +./tests.sh ruby23 diff --git a/kokoro/macos/ruby22/continuous.cfg b/kokoro/macos/ruby23/continuous.cfg similarity index 67% rename from kokoro/macos/ruby22/continuous.cfg rename to kokoro/macos/ruby23/continuous.cfg index d27054417a..c9b1f61284 100644 --- a/kokoro/macos/ruby22/continuous.cfg +++ b/kokoro/macos/ruby23/continuous.cfg @@ -1,5 +1,5 @@ # Config file for running tests in Kokoro # Location of the build script in repository -build_file: "protobuf/kokoro/macos/ruby22/build.sh" +build_file: "protobuf/kokoro/macos/ruby23/build.sh" timeout_mins: 1440 diff --git a/kokoro/macos/ruby22/presubmit.cfg b/kokoro/macos/ruby23/presubmit.cfg similarity index 67% rename from kokoro/macos/ruby22/presubmit.cfg rename to kokoro/macos/ruby23/presubmit.cfg index d27054417a..c9b1f61284 100644 --- a/kokoro/macos/ruby22/presubmit.cfg +++ b/kokoro/macos/ruby23/presubmit.cfg @@ -1,5 +1,5 @@ # Config file for running tests in Kokoro # Location of the build script in repository -build_file: "protobuf/kokoro/macos/ruby22/build.sh" +build_file: "protobuf/kokoro/macos/ruby23/build.sh" timeout_mins: 1440 diff --git a/kokoro/macos/ruby22/build.sh b/kokoro/macos/ruby24/build.sh similarity index 91% rename from kokoro/macos/ruby22/build.sh rename to kokoro/macos/ruby24/build.sh index 5c4de42952..3c3a190d9d 100755 --- a/kokoro/macos/ruby22/build.sh +++ b/kokoro/macos/ruby24/build.sh @@ -8,4 +8,4 @@ cd $(dirname $0)/../../.. # Prepare worker environment to run tests source kokoro/macos/prepare_build_macos_rc -./tests.sh ruby22 +./tests.sh ruby24 diff --git a/kokoro/macos/ruby21/continuous.cfg b/kokoro/macos/ruby24/continuous.cfg similarity index 67% rename from kokoro/macos/ruby21/continuous.cfg rename to kokoro/macos/ruby24/continuous.cfg index 489796daf2..fada028fc5 100644 --- a/kokoro/macos/ruby21/continuous.cfg +++ b/kokoro/macos/ruby24/continuous.cfg @@ -1,5 +1,5 @@ # Config file for running tests in Kokoro # Location of the build script in repository -build_file: "protobuf/kokoro/macos/ruby21/build.sh" +build_file: "protobuf/kokoro/macos/ruby24/build.sh" timeout_mins: 1440 diff --git a/kokoro/macos/ruby21/presubmit.cfg b/kokoro/macos/ruby24/presubmit.cfg similarity index 67% rename from kokoro/macos/ruby21/presubmit.cfg rename to kokoro/macos/ruby24/presubmit.cfg index 489796daf2..fada028fc5 100644 --- a/kokoro/macos/ruby21/presubmit.cfg +++ b/kokoro/macos/ruby24/presubmit.cfg @@ -1,5 +1,5 @@ # Config file for running tests in Kokoro # Location of the build script in repository -build_file: "protobuf/kokoro/macos/ruby21/build.sh" +build_file: "protobuf/kokoro/macos/ruby24/build.sh" timeout_mins: 1440 diff --git a/kokoro/macos/ruby25/build.sh b/kokoro/macos/ruby25/build.sh new file mode 100755 index 0000000000..38e90aa75c --- /dev/null +++ b/kokoro/macos/ruby25/build.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# +# Build file to set up and run tests + +# Change to repo root +cd $(dirname $0)/../../.. + +# Prepare worker environment to run tests +source kokoro/macos/prepare_build_macos_rc + +./tests.sh ruby25 diff --git a/kokoro/macos/ruby25/continuous.cfg b/kokoro/macos/ruby25/continuous.cfg new file mode 100644 index 0000000000..b3755e4104 --- /dev/null +++ b/kokoro/macos/ruby25/continuous.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/macos/ruby25/build.sh" +timeout_mins: 1440 diff --git a/kokoro/macos/ruby25/presubmit.cfg b/kokoro/macos/ruby25/presubmit.cfg new file mode 100644 index 0000000000..b3755e4104 --- /dev/null +++ b/kokoro/macos/ruby25/presubmit.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/macos/ruby25/build.sh" +timeout_mins: 1440 diff --git a/kokoro/macos/ruby26/build.sh b/kokoro/macos/ruby26/build.sh new file mode 100755 index 0000000000..6c33ed0a7e --- /dev/null +++ b/kokoro/macos/ruby26/build.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# +# Build file to set up and run tests + +# Change to repo root +cd $(dirname $0)/../../.. + +# Prepare worker environment to run tests +source kokoro/macos/prepare_build_macos_rc + +./tests.sh ruby26 diff --git a/kokoro/macos/ruby26/continuous.cfg b/kokoro/macos/ruby26/continuous.cfg new file mode 100644 index 0000000000..688f63c2bc --- /dev/null +++ b/kokoro/macos/ruby26/continuous.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/macos/ruby26/build.sh" +timeout_mins: 1440 diff --git a/kokoro/macos/ruby26/presubmit.cfg b/kokoro/macos/ruby26/presubmit.cfg new file mode 100644 index 0000000000..688f63c2bc --- /dev/null +++ b/kokoro/macos/ruby26/presubmit.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/macos/ruby26/build.sh" +timeout_mins: 1440 diff --git a/kokoro/release/collect_all_artifacts.cfg b/kokoro/release/collect_all_artifacts.cfg new file mode 100644 index 0000000000..3da1a7c49a --- /dev/null +++ b/kokoro/release/collect_all_artifacts.cfg @@ -0,0 +1,7 @@ +build_file: "protobuf/kokoro/release/collect_all_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/collect_all_artifacts.sh b/kokoro/release/collect_all_artifacts.sh new file mode 100755 index 0000000000..0023937d53 --- /dev/null +++ b/kokoro/release/collect_all_artifacts.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +set -ex + +# Change to repo root. +cd $(dirname $0)/../.. + +# Initialize any submodules. +git submodule update --init --recursive + +# The directory with all resulting artifacts +mkdir -p artifacts + +# Artifacts from all predecessor jobs get copied to this directory by kokoro +INPUT_ARTIFACTS_DIR="${KOKORO_GFILE_DIR}/github/protobuf" + +# TODO(jtattermusch): remove listing the files, but for now it make it easier +# to iterate on the script. +ls -R ${INPUT_ARTIFACTS_DIR} + +# ==================================== +# Copy to expose all the artifacts from the predecessor jobs to the output +# TODO(jtattermusch): the directory layout of the artifact builds is pretty messy, +# so will be the output artifacts of this job. +cp -r ${INPUT_ARTIFACTS_DIR}/* artifacts + +# ==================================== +# Build Google.Protobuf.Tools C# nuget +# The reason it's being done in this script is that we need access to protoc binaries +# built on multiple platform (the build is performed by the "build artifact" step) +# and adding and extra chained build just for building the Google.Protobuf.Tools +# nuget seems like an overkill. +cd csharp +mkdir -p protoc/windows_x86 +mkdir -p protoc/windows_x64 +cp ${INPUT_ARTIFACTS_DIR}/build32/Release/protoc.exe protoc/windows_x86/protoc.exe +cp ${INPUT_ARTIFACTS_DIR}/build64/Release/protoc.exe protoc/windows_x64/protoc.exe + +mkdir -p protoc/linux_x86 +mkdir -p protoc/linux_x64 +# Because of maven unrelated reasonse the linux protoc binaries have a dummy .exe extension. +# For the Google.Protobuf.Tools nuget, we don't want that expection, so we just remove it. +cp ${INPUT_ARTIFACTS_DIR}/protoc-artifacts/target/linux/x86_32/protoc.exe protoc/linux_x86/protoc +cp ${INPUT_ARTIFACTS_DIR}/protoc-artifacts/target/linux/x86_64/protoc.exe protoc/linux_x64/protoc + +mkdir -p protoc/macosx_x86 +mkdir -p protoc/macosx_x64 +cp ${INPUT_ARTIFACTS_DIR}/build32/src/protoc protoc/macosx_x86/protoc +cp ${INPUT_ARTIFACTS_DIR}/build64/src/protoc protoc/macosx_x64/protoc + +# Install nuget (will also install mono) +# TODO(jtattermusch): use "mono:5.14" docker image instead so we don't have to apt-get install +sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF +sudo apt install apt-transport-https +echo "deb https://download.mono-project.com/repo/ubuntu stable-trusty main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list +sudo apt update +sudo apt-get install -y nuget + +nuget pack Google.Protobuf.Tools.nuspec + +# Copy the nupkg to the output artifacts +cp Google.Protobuf.Tools.*.nupkg ../artifacts diff --git a/kokoro/release/csharp/windows/build_nuget.bat b/kokoro/release/csharp/windows/build_nuget.bat index 5da1e13602..0ff8db0428 100644 --- a/kokoro/release/csharp/windows/build_nuget.bat +++ b/kokoro/release/csharp/windows/build_nuget.bat @@ -2,4 +2,13 @@ cd /d %~dp0\..\..\..\.. cd csharp + +@rem Install dotnet SDK +powershell -File install_dotnet_sdk.ps1 +set PATH=%LOCALAPPDATA%\Microsoft\dotnet;%PATH% + +@rem Disable some unwanted dotnet options +set DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true +set DOTNET_CLI_TELEMETRY_OPTOUT=true + call build_packages.bat diff --git a/kokoro/release/csharp/windows/continuous.cfg b/kokoro/release/csharp/windows/continuous.cfg new file mode 100644 index 0000000000..f508c65bda --- /dev/null +++ b/kokoro/release/csharp/windows/continuous.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/release/csharp/windows/build_nuget.bat" +timeout_mins: 60 + +action { + define_artifacts { + regex: "**/*.nupkg" + } +} diff --git a/kokoro/release/csharp/windows/presubmit.cfg b/kokoro/release/csharp/windows/presubmit.cfg new file mode 100644 index 0000000000..f508c65bda --- /dev/null +++ b/kokoro/release/csharp/windows/presubmit.cfg @@ -0,0 +1,11 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/release/csharp/windows/build_nuget.bat" +timeout_mins: 60 + +action { + define_artifacts { + regex: "**/*.nupkg" + } +} diff --git a/kokoro/release/protoc/macos/build.sh b/kokoro/release/protoc/macos/build.sh index db9d3140db..6a4c79c7de 100644 --- a/kokoro/release/protoc/macos/build.sh +++ b/kokoro/release/protoc/macos/build.sh @@ -1,7 +1,7 @@ #!/bin/bash -set -x -CXXFLAGS_COMMON="-DNDEBUG -mmacosx-version-min=10.9" +set -ex +CXXFLAGS_COMMON="-std=c++14 -DNDEBUG -mmacosx-version-min=10.9" cd github/protobuf ./autogen.sh diff --git a/kokoro/release/python/linux/build_artifacts.sh b/kokoro/release/python/linux/build_artifacts.sh new file mode 100755 index 0000000000..c2806b704e --- /dev/null +++ b/kokoro/release/python/linux/build_artifacts.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +set -ex + +# change to repo root +pushd $(dirname $0)/../../../.. + +# Create stage dir +ORIGINAL_DIR=`pwd` +pushd .. +cp -R $ORIGINAL_DIR stage +export STAGE_DIR="`pwd`/stage" +popd + +export REPO_DIR=protobuf +export BUILD_VERSION=`grep -i "version" python/google/protobuf/__init__.py | grep -o "'.*'" | tr -d "'"` + +export BUILD_COMMIT=`git rev-parse HEAD` +export PLAT=x86_64 +export UNICODE_WIDTH=32 +export MACOSX_DEPLOYMENT_TARGET=10.9 + +rm -rf artifacts/ +rm -rf multibuild/ +mkdir artifacts +export ARTIFACT_DIR=$(pwd)/artifacts + +git clone https://github.com/matthew-brett/multibuild.git +cp kokoro/release/python/linux/config.sh config.sh + +build_artifact_version() { + MB_PYTHON_VERSION=$1 + + # Clean up env + rm -rf venv + sudo rm -rf $REPO_DIR + cp -R $STAGE_DIR $REPO_DIR + + source multibuild/common_utils.sh + source multibuild/travis_steps.sh + before_install + + clean_code $REPO_DIR $BUILD_COMMIT + sed -i '/Wno-sign-compare/a \ \ \ \ \ \ \ \ extra_compile_args.append("-std=c++11")' $REPO_DIR/python/setup.py + cat $REPO_DIR/python/setup.py + + build_wheel $REPO_DIR/python $PLAT + + mv wheelhouse/* $ARTIFACT_DIR +} + +build_artifact_version 2.7 +build_artifact_version 3.4 +build_artifact_version 3.5 +build_artifact_version 3.6 +build_artifact_version 3.7 diff --git a/kokoro/release/python/linux/config.sh b/kokoro/release/python/linux/config.sh new file mode 100644 index 0000000000..7a8f663d81 --- /dev/null +++ b/kokoro/release/python/linux/config.sh @@ -0,0 +1,50 @@ +# Define custom utilities +# Test for OSX with [ -n "$IS_OSX" ] + +function pre_build { + # Any stuff that you need to do before you start building the wheels + # Runs in the root directory of this repository. + pushd protobuf + + yum install -y devtoolset-2-libatomic-devel + + # Build protoc + ./autogen.sh + ./configure + + CXXFLAGS="-fPIC -g -O2" ./configure + make -j8 + + # Generate python dependencies. + pushd python + python setup.py build_py + popd + + popd +} + +function bdist_wheel_cmd { + # Builds wheel with bdist_wheel, puts into wheelhouse + # + # It may sometimes be useful to use bdist_wheel for the wheel building + # process. For example, versioneer has problems with versions which are + # fixed with bdist_wheel: + # https://github.com/warner/python-versioneer/issues/121 + local abs_wheelhouse=$1 + + # Modify build version + pwd + ls + python setup.py bdist_wheel --cpp_implementation --compile_static_extension + cp dist/*.whl $abs_wheelhouse +} + +function build_wheel { + build_wheel_cmd "bdist_wheel_cmd" $@ +} + +function run_tests { + # Runs tests on installed distribution from an empty directory + python --version + python -c "from google.protobuf.pyext import _message;" +} diff --git a/kokoro/release/python/linux/continuous.cfg b/kokoro/release/python/linux/continuous.cfg new file mode 100644 index 0000000000..c0b9a018f1 --- /dev/null +++ b/kokoro/release/python/linux/continuous.cfg @@ -0,0 +1,8 @@ +# Config file for running tests in Kokoro +build_file: "protobuf/kokoro/release/python/linux/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/python/linux/presubmit.cfg b/kokoro/release/python/linux/presubmit.cfg new file mode 100644 index 0000000000..c0b9a018f1 --- /dev/null +++ b/kokoro/release/python/linux/presubmit.cfg @@ -0,0 +1,8 @@ +# Config file for running tests in Kokoro +build_file: "protobuf/kokoro/release/python/linux/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/python/linux/release.cfg b/kokoro/release/python/linux/release.cfg new file mode 100644 index 0000000000..b31b3f1192 --- /dev/null +++ b/kokoro/release/python/linux/release.cfg @@ -0,0 +1,8 @@ +# Configuration for Linux release builds +build_file: "protobuf/kokoro/release/python/linux/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/python/macos/build_artifacts.sh b/kokoro/release/python/macos/build_artifacts.sh new file mode 100755 index 0000000000..02d666f7a2 --- /dev/null +++ b/kokoro/release/python/macos/build_artifacts.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +set -ex + +# change to repo root +pushd $(dirname $0)/../../../.. + +# Create stage dir +ORIGINAL_DIR=`pwd` +pushd .. +cp -R $ORIGINAL_DIR stage +export STAGE_DIR="`pwd`/stage" +popd + +export REPO_DIR=protobuf +export BUILD_VERSION=`grep -i "version" python/google/protobuf/__init__.py | grep -o "'.*'" | tr -d "'"` +export BUILD_COMMIT=`git rev-parse HEAD` +export PLAT=x86_64 +export UNICODE_WIDTH=32 +export MACOSX_DEPLOYMENT_TARGET=10.9 +export TRAVIS_OS_NAME="osx" + +rm -rf artifacts/ +rm -rf multibuild/ +mkdir artifacts +export ARTIFACT_DIR=$(pwd)/artifacts + +git clone https://github.com/matthew-brett/multibuild.git +cp kokoro/release/python/macos/config.sh config.sh + +OLD_PATH=$PATH + +build_artifact_version() { + MB_PYTHON_VERSION=$1 + + # Clean up env + rm -rf venv + sudo rm -rf $REPO_DIR + cp -R $STAGE_DIR $REPO_DIR + export PATH=$OLD_PATH + + source multibuild/common_utils.sh + source multibuild/travis_steps.sh + before_install + + clean_code $REPO_DIR $BUILD_COMMIT + + build_wheel $REPO_DIR/python $PLAT + + mv wheelhouse/* $ARTIFACT_DIR +} + +build_artifact_version 2.7 +build_artifact_version 3.4 +build_artifact_version 3.5 +build_artifact_version 3.6 +build_artifact_version 3.7 diff --git a/kokoro/release/python/macos/config.sh b/kokoro/release/python/macos/config.sh new file mode 100644 index 0000000000..741988a6b5 --- /dev/null +++ b/kokoro/release/python/macos/config.sh @@ -0,0 +1,48 @@ +# Define custom utilities +# Test for OSX with [ -n "$IS_OSX" ] + +function pre_build { + # Any stuff that you need to do before you start building the wheels + # Runs in the root directory of this repository. + pushd protobuf + + # Build protoc + ./autogen.sh + ./configure + + CXXFLAGS="-std=c++14 -fPIC -g -O2" ./configure + make -j8 + + # Generate python dependencies. + pushd python + python setup.py build_py + popd + + popd +} + +function bdist_wheel_cmd { + # Builds wheel with bdist_wheel, puts into wheelhouse + # + # It may sometimes be useful to use bdist_wheel for the wheel building + # process. For example, versioneer has problems with versions which are + # fixed with bdist_wheel: + # https://github.com/warner/python-versioneer/issues/121 + local abs_wheelhouse=$1 + + # Modify build version + pwd + ls + python setup.py bdist_wheel --cpp_implementation --compile_static_extension + cp dist/*.whl $abs_wheelhouse +} + +function build_wheel { + build_wheel_cmd "bdist_wheel_cmd" $@ +} + +function run_tests { + # Runs tests on installed distribution from an empty directory + python --version + python -c "from google.protobuf.pyext import _message;" +} diff --git a/kokoro/release/python/macos/continuous.cfg b/kokoro/release/python/macos/continuous.cfg new file mode 100644 index 0000000000..85cf81b507 --- /dev/null +++ b/kokoro/release/python/macos/continuous.cfg @@ -0,0 +1,8 @@ +# Configuration for Mac OSX release builds +build_file: "protobuf/kokoro/release/python/macos/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/python/macos/presubmit.cfg b/kokoro/release/python/macos/presubmit.cfg new file mode 100644 index 0000000000..85cf81b507 --- /dev/null +++ b/kokoro/release/python/macos/presubmit.cfg @@ -0,0 +1,8 @@ +# Configuration for Mac OSX release builds +build_file: "protobuf/kokoro/release/python/macos/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/python/macos/release.cfg b/kokoro/release/python/macos/release.cfg new file mode 100644 index 0000000000..85cf81b507 --- /dev/null +++ b/kokoro/release/python/macos/release.cfg @@ -0,0 +1,8 @@ +# Configuration for Mac OSX release builds +build_file: "protobuf/kokoro/release/python/macos/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/python/windows/build_artifacts.bat b/kokoro/release/python/windows/build_artifacts.bat new file mode 100644 index 0000000000..9a6842ac91 --- /dev/null +++ b/kokoro/release/python/windows/build_artifacts.bat @@ -0,0 +1,68 @@ +REM Move scripts to root +set REPO_DIR_STAGE=%cd%\github\protobuf-stage +xcopy /S github\protobuf "%REPO_DIR_STAGE%\" +cd github\protobuf +copy kokoro\release\python\windows\build_single_artifact.bat build_single_artifact.bat + +REM Set environment variables +set PACKAGE_NAME=protobuf +set REPO_DIR=protobuf +set BUILD_DLL=OFF +set UNICODE=ON +set PB_TEST_DEP="six==1.9" +set OTHER_TEST_DEP="setuptools==38.5.1" +set OLD_PATH=C:\Program Files (x86)\MSBuild\14.0\bin\;%PATH% + +REM Fetch multibuild +git clone https://github.com/matthew-brett/multibuild.git + +REM Install zlib +mkdir zlib +curl -L -o zlib.zip http://www.winimage.com/zLibDll/zlib123dll.zip +curl -L -o zlib-src.zip http://www.winimage.com/zLibDll/zlib123.zip +7z x zlib.zip -ozlib +7z x zlib-src.zip -ozlib\include +SET ZLIB_ROOT=%cd%\zlib +del /Q zlib.zip +del /Q zlib-src.zip + +REM Create directory for artifacts +SET ARTIFACT_DIR=%cd%\artifacts +mkdir %ARTIFACT_DIR% + +REM Build wheel + +SET PYTHON=C:\python35_32bit +SET PYTHON_VERSION=3.5 +SET PYTHON_ARCH=32 +CALL build_single_artifact.bat || goto :error + +SET PYTHON=C:\python35 +SET PYTHON_VERSION=3.5 +SET PYTHON_ARCH=64 +CALL build_single_artifact.bat || goto :error + +SET PYTHON=C:\python36_32bit +SET PYTHON_VERSION=3.6 +SET PYTHON_ARCH=32 +CALL build_single_artifact.bat || goto :error + +SET PYTHON=C:\python36 +SET PYTHON_VERSION=3.6 +SET PYTHON_ARCH=64 +CALL build_single_artifact.bat || goto :error + +SET PYTHON=C:\python37_32bit +SET PYTHON_VERSION=3.7 +SET PYTHON_ARCH=32 +CALL build_single_artifact.bat || goto :error + +SET PYTHON=C:\python37 +SET PYTHON_VERSION=3.7 +SET PYTHON_ARCH=64 +CALL build_single_artifact.bat || goto :error + +goto :EOF + +:error +exit /b %errorlevel% diff --git a/kokoro/release/python/windows/build_single_artifact.bat b/kokoro/release/python/windows/build_single_artifact.bat new file mode 100644 index 0000000000..d7cd062c10 --- /dev/null +++ b/kokoro/release/python/windows/build_single_artifact.bat @@ -0,0 +1,72 @@ +setlocal + +if %PYTHON%==C:\python35_32bit set generator=Visual Studio 14 +if %PYTHON%==C:\python35_32bit set vcplatform=Win32 + +if %PYTHON%==C:\python35 set generator=Visual Studio 14 Win64 +if %PYTHON%==C:\python35 set vcplatform=x64 + +if %PYTHON%==C:\python36_32bit set generator=Visual Studio 14 +if %PYTHON%==C:\python36_32bit set vcplatform=Win32 + +if %PYTHON%==C:\python36 set generator=Visual Studio 14 Win64 +if %PYTHON%==C:\python36 set vcplatform=x64 + +if %PYTHON%==C:\python37_32bit set generator=Visual Studio 14 +if %PYTHON%==C:\python37_32bit set vcplatform=Win32 + +if %PYTHON%==C:\python37 set generator=Visual Studio 14 Win64 +if %PYTHON%==C:\python37 set vcplatform=x64 + +REM Prepend newly installed Python to the PATH of this build (this cannot be +REM done from inside the powershell script as it would require to restart +REM the parent CMD process). +SET PATH=%PYTHON%;%PYTHON%\Scripts;%OLD_PATH% +python -m pip install -U pip +pip install wheel + +REM Check that we have the expected version and architecture for Python +python --version +python -c "import struct; print(struct.calcsize('P') * 8)" + +rmdir /s/q %REPO_DIR% +xcopy /s %REPO_DIR_STAGE% "%REPO_DIR%\" + +REM Checkout release commit +cd %REPO_DIR% + +REM ====================== +REM Build Protobuf Library +REM ====================== + +mkdir src\.libs + +mkdir vcprojects +pushd vcprojects +cmake -G "%generator%" -Dprotobuf_BUILD_SHARED_LIBS=%BUILD_DLL% -Dprotobuf_UNICODE=%UNICODE% -Dprotobuf_BUILD_TESTS=OFF ../cmake || goto :error +msbuild protobuf.sln /p:Platform=%vcplatform% /p:Configuration=Release || goto :error +dir /s /b +popd +copy vcprojects\Release\libprotobuf.lib src\.libs\libprotobuf.a +copy vcprojects\Release\libprotobuf-lite.lib src\.libs\libprotobuf-lite.a +SET PATH=%cd%\vcprojects\Release;%PATH% +dir vcprojects\Release + +REM ====================== +REM Build python library +REM ====================== + +cd python + +REM sed -i 's/\ extra_compile_args\ =\ \[\]/\ extra_compile_args\ =\ \[\'\/MT\'\]/g' setup.py + +python setup.py bdist_wheel --cpp_implementation --compile_static_extension +dir dist +copy dist\* %ARTIFACT_DIR% +dir %ARTIFACT_DIR% +cd ..\.. + +goto :EOF + +:error +exit /b %errorlevel% diff --git a/kokoro/release/python/windows/continuous.cfg b/kokoro/release/python/windows/continuous.cfg new file mode 100644 index 0000000000..5261606477 --- /dev/null +++ b/kokoro/release/python/windows/continuous.cfg @@ -0,0 +1,8 @@ +# Configuration for Windows protoc release builds +build_file: "protobuf/kokoro/release/python/windows/build_artifacts.bat" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/python/windows/presubmit.cfg b/kokoro/release/python/windows/presubmit.cfg new file mode 100644 index 0000000000..5261606477 --- /dev/null +++ b/kokoro/release/python/windows/presubmit.cfg @@ -0,0 +1,8 @@ +# Configuration for Windows protoc release builds +build_file: "protobuf/kokoro/release/python/windows/build_artifacts.bat" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/python/windows/release.cfg b/kokoro/release/python/windows/release.cfg new file mode 100644 index 0000000000..5261606477 --- /dev/null +++ b/kokoro/release/python/windows/release.cfg @@ -0,0 +1,8 @@ +# Configuration for Windows protoc release builds +build_file: "protobuf/kokoro/release/python/windows/build_artifacts.bat" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/ruby/linux/continuous.cfg b/kokoro/release/ruby/linux/continuous.cfg new file mode 100644 index 0000000000..dbc71b2ceb --- /dev/null +++ b/kokoro/release/ruby/linux/continuous.cfg @@ -0,0 +1,8 @@ +# Configuration for Linux release builds +build_file: "protobuf/kokoro/release/ruby/linux/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/ruby/linux/prepare_build.sh b/kokoro/release/ruby/linux/prepare_build.sh index d3ff87647e..f2257c3c75 100755 --- a/kokoro/release/ruby/linux/prepare_build.sh +++ b/kokoro/release/ruby/linux/prepare_build.sh @@ -8,9 +8,11 @@ echo 'DOCKER_OPTS="${DOCKER_OPTS} --registry-mirror=https://mirror.gcr.io"' | su sudo service docker restart # Download Docker images from DockerHub -export DOCKERHUB_ORGANIZATION=grpctesting +DOCKERHUB_ORGANIZATION=protobuftesting +DOCKERFILE_DIR=kokoro/linux/dockerfile/release/ruby_rake_compiler +DOCKERFILE_PREFIX=$(basename $DOCKERFILE_DIR) +export RAKE_COMPILER_DOCK_IMAGE=${DOCKERHUB_ORGANIZATION}/${DOCKERFILE_PREFIX}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ ) # All artifacts come here mkdir artifacts export ARTIFACT_DIR=$(pwd)/artifacts - diff --git a/kokoro/release/ruby/linux/presubmit.cfg b/kokoro/release/ruby/linux/presubmit.cfg new file mode 100644 index 0000000000..dbc71b2ceb --- /dev/null +++ b/kokoro/release/ruby/linux/presubmit.cfg @@ -0,0 +1,8 @@ +# Configuration for Linux release builds +build_file: "protobuf/kokoro/release/ruby/linux/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/ruby/linux/ruby/ruby_build.sh b/kokoro/release/ruby/linux/ruby/ruby_build.sh index cf6b433d06..9fc42b13eb 100755 --- a/kokoro/release/ruby/linux/ruby/ruby_build.sh +++ b/kokoro/release/ruby/linux/ruby/ruby_build.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -ex + # Build protoc if test ! -e src/protoc; then ./autogen.sh diff --git a/kokoro/release/ruby/macos/continuous.cfg b/kokoro/release/ruby/macos/continuous.cfg new file mode 100644 index 0000000000..cb1c08bb88 --- /dev/null +++ b/kokoro/release/ruby/macos/continuous.cfg @@ -0,0 +1,8 @@ +# Configuration for Mac OSX release builds +build_file: "protobuf/kokoro/release/ruby/macos/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/ruby/macos/presubmit.cfg b/kokoro/release/ruby/macos/presubmit.cfg new file mode 100644 index 0000000000..cb1c08bb88 --- /dev/null +++ b/kokoro/release/ruby/macos/presubmit.cfg @@ -0,0 +1,8 @@ +# Configuration for Mac OSX release builds +build_file: "protobuf/kokoro/release/ruby/macos/build_artifacts.sh" + +action { + define_artifacts { + regex: "github/protobuf/artifacts/**" + } +} diff --git a/kokoro/release/ruby/macos/ruby/ruby_build.sh b/kokoro/release/ruby/macos/ruby/ruby_build.sh index cf6b433d06..9fc42b13eb 100755 --- a/kokoro/release/ruby/macos/ruby/ruby_build.sh +++ b/kokoro/release/ruby/macos/ruby/ruby_build.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -ex + # Build protoc if test ! -e src/protoc; then ./autogen.sh diff --git a/kokoro/release/ruby/macos/ruby/ruby_build_environment.sh b/kokoro/release/ruby/macos/ruby/ruby_build_environment.sh index e9c619cd1c..d42a0a8042 100755 --- a/kokoro/release/ruby/macos/ruby/ruby_build_environment.sh +++ b/kokoro/release/ruby/macos/ruby/ruby_build_environment.sh @@ -34,7 +34,7 @@ patch "$CROSS_RUBY" << EOF + '--without-gmp', '--with-ext=' ] - + @@ -151,6 +153,7 @@ # make file "#{USER_HOME}/builds/#{MINGW_HOST}/#{RUBY_CC_VERSION}/ruby.exe" => ["#{USER_HOME}/builds/#{MINGW_HOST}/#{RUBY_CC_VERSION}/Makefile"] do |t| @@ -47,7 +47,7 @@ EOF MAKE="make -j8" -for v in 2.5.0 2.4.0 2.3.0 2.2.2 2.1.6 2.0.0-p645 ; do +for v in 2.6.0 2.5.1 2.4.0 2.3.0 ; do ccache -c rake -f "$CROSS_RUBY" cross-ruby VERSION="$v" HOST=x86_64-darwin11 MAKE="$MAKE" done diff --git a/kokoro/windows/csharp/build.bat b/kokoro/windows/csharp/build.bat new file mode 100644 index 0000000000..95224f2eb2 --- /dev/null +++ b/kokoro/windows/csharp/build.bat @@ -0,0 +1,14 @@ +@rem enter repo root +cd /d %~dp0\..\..\.. + +cd csharp + +@rem Install dotnet SDK +powershell -File install_dotnet_sdk.ps1 +set PATH=%LOCALAPPDATA%\Microsoft\dotnet;%PATH% + +@rem Disable some unwanted dotnet options +set DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true +set DOTNET_CLI_TELEMETRY_OPTOUT=true + +call buildall.bat diff --git a/kokoro/windows/csharp/continuous.cfg b/kokoro/windows/csharp/continuous.cfg new file mode 100644 index 0000000000..f586585176 --- /dev/null +++ b/kokoro/windows/csharp/continuous.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/windows/csharp/build.bat" +timeout_mins: 1440 diff --git a/kokoro/windows/csharp/presubmit.cfg b/kokoro/windows/csharp/presubmit.cfg new file mode 100644 index 0000000000..f586585176 --- /dev/null +++ b/kokoro/windows/csharp/presubmit.cfg @@ -0,0 +1,5 @@ +# Config file for running tests in Kokoro + +# Location of the build script in repository +build_file: "protobuf/kokoro/windows/csharp/build.bat" +timeout_mins: 1440 diff --git a/m4/acx_check_suncc.m4 b/m4/acx_check_suncc.m4 index 8bc0a890dc..0a9a5770b3 100644 --- a/m4/acx_check_suncc.m4 +++ b/m4/acx_check_suncc.m4 @@ -26,7 +26,7 @@ AC_DEFUN([ACX_CHECK_SUNCC],[ AS_IF([test "$SUNCC" = "yes" -a "x${ac_cv_env_CXXFLAGS_set}" = "x"],[ dnl Sun Studio has a crashing bug with -xO4 in some cases. Keep this dnl at -xO3 until a proper test to detect those crashes can be done. - CXXFLAGS="-g0 -xO3 -xlibmil -xdepend -xbuiltin -mt -compat=5 -library=stlport4 -library=Crun -template=no%extdef ${CXXFLAGS}" + CXXFLAGS="-g0 -xO3 -xlibmil -xdepend -xbuiltin -mt -template=no%extdef ${CXXFLAGS}" ]) case $host_os in @@ -67,4 +67,7 @@ AC_DEFUN([ACX_CHECK_SUNCC],[ ;; esac + AS_IF([test "$target_cpu" = "sparc" -a "x$SUNCC" = "xyes" ],[ + CXXFLAGS="-xregs=no%appl ${CXXFLAGS}" + ]) ]) diff --git a/objectivec/DevTools/compile_testing_protos.sh b/objectivec/DevTools/compile_testing_protos.sh index d7f3f60589..3070ba63bf 100755 --- a/objectivec/DevTools/compile_testing_protos.sh +++ b/objectivec/DevTools/compile_testing_protos.sh @@ -146,4 +146,5 @@ compile_protos \ objectivec/Tests/unittest_runtime_proto2.proto \ objectivec/Tests/unittest_runtime_proto3.proto \ objectivec/Tests/unittest_objc.proto \ - objectivec/Tests/unittest_objc_startup.proto + objectivec/Tests/unittest_objc_startup.proto \ + objectivec/Tests/unittest_objc_options.proto diff --git a/objectivec/DevTools/full_mac_build.sh b/objectivec/DevTools/full_mac_build.sh index c673fcba37..1f87407358 100755 --- a/objectivec/DevTools/full_mac_build.sh +++ b/objectivec/DevTools/full_mac_build.sh @@ -41,8 +41,10 @@ OPTIONS: Skip the Xcode Debug configuration. --skip-xcode-release Skip the Xcode Release configuration. - --skip-xcode-osx + --skip-xcode-osx | --skip-xcode-macos Skip the invoke of Xcode to test the runtime on OS X. + --skip-xcode-tvos + Skip the invoke of Xcode to test the runtime on tvOS. --skip-objc-conformance Skip the Objective C conformance tests (run on OS X). --xcode-quiet @@ -82,6 +84,7 @@ REGEN_DESCRIPTORS=no CORE_ONLY=no DO_XCODE_IOS_TESTS=yes DO_XCODE_OSX_TESTS=yes +DO_XCODE_TVOS_TESTS=yes DO_XCODE_DEBUG=yes DO_XCODE_RELEASE=yes DO_OBJC_CONFORMANCE_TESTS=yes @@ -111,13 +114,17 @@ while [[ $# != 0 ]]; do --skip-xcode ) DO_XCODE_IOS_TESTS=no DO_XCODE_OSX_TESTS=no + DO_XCODE_TVOS_TESTS=no ;; --skip-xcode-ios ) DO_XCODE_IOS_TESTS=no ;; - --skip-xcode-osx ) + --skip-xcode-osx | --skip-xcode-macos) DO_XCODE_OSX_TESTS=no ;; + --skip-xcode-tvos ) + DO_XCODE_TVOS_TESTS=no + ;; --skip-xcode-debug ) DO_XCODE_DEBUG=no ;; @@ -156,8 +163,7 @@ if [[ "${DO_AUTOGEN}" == "yes" ]] ; then header "Running autogen & configure" ./autogen.sh ./configure \ - CPPFLAGS="-mmacosx-version-min=10.9 -Wunused-const-variable -Wunused-function" \ - CXXFLAGS="-Wnon-virtual-dtor -Woverloaded-virtual" + CPPFLAGS="-mmacosx-version-min=10.9 -Wunused-const-variable -Wunused-function" fi if [[ "${DO_CLEAN}" == "yes" ]] ; then @@ -189,6 +195,19 @@ if [[ "${DO_CLEAN}" == "yes" ]] ; then "${XCODEBUILD_CLEAN_BASE_OSX[@]}" -configuration Release clean fi fi + if [[ "${DO_XCODE_TVOS_TESTS}" == "yes" ]] ; then + XCODEBUILD_CLEAN_BASE_OSX=( + xcodebuild + -project objectivec/ProtocolBuffers_tvOS.xcodeproj + -scheme ProtocolBuffers + ) + if [[ "${DO_XCODE_DEBUG}" == "yes" ]] ; then + "${XCODEBUILD_CLEAN_BASE_OSX[@]}" -configuration Debug clean + fi + if [[ "${DO_XCODE_RELEASE}" == "yes" ]] ; then + "${XCODEBUILD_CLEAN_BASE_OSX[@]}" -configuration Release clean + fi + fi fi if [[ "${REGEN_DESCRIPTORS}" == "yes" ]] ; then @@ -239,27 +258,11 @@ if [[ "${DO_XCODE_IOS_TESTS}" == "yes" ]] ; then # just pick a mix of OS Versions and 32/64 bit. # NOTE: Different Xcode have different simulated hardware/os support. case "${XCODE_VERSION}" in - 6.* ) - echo "ERROR: Xcode 6.3/6.4 no longer supported for building, please use 8.0 or higher." 1>&2 - exit 10 - ;; - 7.* ) - echo "ERROR: Xcode 7.x no longer supported for building, please use 8.0 or higher." 1>&2 + [6-8].* ) + echo "ERROR: The unittests include Swift code that is now Swift 4.0." 1>&2 + echo "ERROR: Xcode 9.0 or higher is required to build the test suite, but the library works with Xcode 7.x." 1>&2 exit 11 ;; - 8.0* ) - # The 8.* device seem to hang and never start under Xcode 8. - XCODEBUILD_TEST_BASE_IOS+=( - -destination "platform=iOS Simulator,name=iPhone 4s,OS=9.0" # 32bit - -destination "platform=iOS Simulator,name=iPhone 7,OS=10.0" # 64bit - ) - ;; - 8.[1-3]* ) - XCODEBUILD_TEST_BASE_IOS+=( - -destination "platform=iOS Simulator,name=iPhone 4s,OS=8.1" # 32bit - -destination "platform=iOS Simulator,name=iPhone 7,OS=latest" # 64bit - ) - ;; 9.[0-2]* ) XCODEBUILD_TEST_BASE_IOS+=( -destination "platform=iOS Simulator,name=iPhone 4s,OS=8.1" # 32bit @@ -268,7 +271,7 @@ if [[ "${DO_XCODE_IOS_TESTS}" == "yes" ]] ; then -disable-concurrent-testing ) ;; - 9.3* ) + 9.[3-4]* ) XCODEBUILD_TEST_BASE_IOS+=( # Xcode 9.3 chokes targeting iOS 8.x - http://www.openradar.me/39335367 -destination "platform=iOS Simulator,name=iPhone 4s,OS=9.0" # 32bit @@ -277,11 +280,29 @@ if [[ "${DO_XCODE_IOS_TESTS}" == "yes" ]] ; then -disable-concurrent-testing ) ;; + 10.*) + XCODEBUILD_TEST_BASE_IOS+=( + -destination "platform=iOS Simulator,name=iPhone 4s,OS=8.1" # 32bit + -destination "platform=iOS Simulator,name=iPhone 7,OS=latest" # 64bit + # 10.x also seems to often fail running destinations in parallel (with + # 32bit one include atleast) + -disable-concurrent-destination-testing + ) + ;; + 11.0* ) + XCODEBUILD_TEST_BASE_IOS+=( + -destination "platform=iOS Simulator,name=iPhone 4s,OS=8.1" # 32bit + -destination "platform=iOS Simulator,name=iPhone 7,OS=latest" # 64bit + # 10.x also seems to often fail running destinations in parallel (with + # 32bit one include atleast) + -disable-concurrent-destination-testing + ) + ;; * ) echo "" echo "ATTENTION: Time to update the simulator targets for Xcode ${XCODE_VERSION}" echo "" - echo "Build aborted!" + echo "ERROR: Build aborted!" exit 2 ;; esac @@ -308,13 +329,9 @@ if [[ "${DO_XCODE_OSX_TESTS}" == "yes" ]] ; then XCODEBUILD_TEST_BASE_OSX+=( -quiet ) fi case "${XCODE_VERSION}" in - 6.* ) - echo "ERROR: Xcode 6.3/6.4 no longer supported for building, please use 8.0 or higher." 1>&2 - exit 10 - ;; - 7.* ) - echo "ERROR: The unittests include Swift code that is now Swift 3.0." 1>&2 - echo "ERROR: Xcode 8.0 or higher is required to build the test suite, but the library works with Xcode 7.x." 1>&2 + [6-8].* ) + echo "ERROR: The unittests include Swift code that is now Swift 4.0." 1>&2 + echo "ERROR: Xcode 9.0 or higher is required to build the test suite, but the library works with Xcode 7.x." 1>&2 exit 11 ;; esac @@ -327,6 +344,44 @@ if [[ "${DO_XCODE_OSX_TESTS}" == "yes" ]] ; then "${XCODEBUILD_TEST_BASE_OSX[@]}" -configuration Release test fi fi +if [[ "${DO_XCODE_TVOS_TESTS}" == "yes" ]] ; then + XCODEBUILD_TEST_BASE_TVOS=( + xcodebuild + -project objectivec/ProtocolBuffers_tvOS.xcodeproj + -scheme ProtocolBuffers + ) + case "${XCODE_VERSION}" in + [6-9].* ) + echo "ERROR: Xcode 10.0 or higher is required to build the test suite." 1>&2 + exit 11 + ;; + 10.* | 11.* ) + XCODEBUILD_TEST_BASE_TVOS+=( + # Test on the oldest and current. + -destination "platform=tvOS Simulator,name=Apple TV,OS=11.0" + -destination "platform=tvOS Simulator,name=Apple TV 4K,OS=latest" + ) + ;; + * ) + echo "" + echo "ATTENTION: Time to update the simulator targets for Xcode ${XCODE_VERSION}" + echo "" + echo "ERROR: Build aborted!" + exit 2 + ;; + esac + if [[ "${XCODE_QUIET}" == "yes" ]] ; then + XCODEBUILD_TEST_BASE_TVOS+=( -quiet ) + fi + if [[ "${DO_XCODE_DEBUG}" == "yes" ]] ; then + header "Doing Xcode tvOS build/tests - Debug" + "${XCODEBUILD_TEST_BASE_TVOS[@]}" -configuration Debug test + fi + if [[ "${DO_XCODE_RELEASE}" == "yes" ]] ; then + header "Doing Xcode tvOS build/tests - Release" + "${XCODEBUILD_TEST_BASE_TVOS[@]}" -configuration Release test + fi +fi if [[ "${DO_OBJC_CONFORMANCE_TESTS}" == "yes" ]] ; then header "Running ObjC Conformance Tests" @@ -334,3 +389,6 @@ if [[ "${DO_OBJC_CONFORMANCE_TESTS}" == "yes" ]] ; then wrapped_make -j "${NUM_MAKE_JOBS}" test_objc cd .. fi + +echo "" +echo "$(basename "${0}"): Success!" diff --git a/objectivec/GPBArray.h b/objectivec/GPBArray.h index 638b2882d3..3d22cb8173 100644 --- a/objectivec/GPBArray.h +++ b/objectivec/GPBArray.h @@ -134,7 +134,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -146,7 +146,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block; /** * Adds a value to this array. @@ -306,7 +306,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateValuesWithBlock:(void (^)(uint32_t value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(uint32_t value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -318,7 +318,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(uint32_t value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(uint32_t value, NSUInteger idx, BOOL *stop))block; /** * Adds a value to this array. @@ -478,7 +478,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateValuesWithBlock:(void (^)(int64_t value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(int64_t value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -490,7 +490,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(int64_t value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(int64_t value, NSUInteger idx, BOOL *stop))block; /** * Adds a value to this array. @@ -650,7 +650,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateValuesWithBlock:(void (^)(uint64_t value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(uint64_t value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -662,7 +662,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(uint64_t value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(uint64_t value, NSUInteger idx, BOOL *stop))block; /** * Adds a value to this array. @@ -822,7 +822,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateValuesWithBlock:(void (^)(float value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(float value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -834,7 +834,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(float value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(float value, NSUInteger idx, BOOL *stop))block; /** * Adds a value to this array. @@ -994,7 +994,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateValuesWithBlock:(void (^)(double value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(double value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -1006,7 +1006,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(double value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(double value, NSUInteger idx, BOOL *stop))block; /** * Adds a value to this array. @@ -1166,7 +1166,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateValuesWithBlock:(void (^)(BOOL value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(BOOL value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -1178,7 +1178,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(BOOL value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(BOOL value, NSUInteger idx, BOOL *stop))block; /** * Adds a value to this array. @@ -1369,7 +1369,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -1381,7 +1381,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block; // These methods bypass the validationFunc to provide access to values that were not // known at the time the binary was compiled. @@ -1403,7 +1403,7 @@ NS_ASSUME_NONNULL_BEGIN * **idx**: The index of the current value. * **stop**: A pointer to a boolean that when set stops the enumeration. **/ -- (void)enumerateRawValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block; +- (void)enumerateRawValuesWithBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block; /** * Enumerates the values on this array with the given block. @@ -1415,7 +1415,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateRawValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block; + usingBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block; // If value is not a valid enumerator as defined by validationFunc, these // methods will assert in debug, and will log in release and assign the value @@ -1779,7 +1779,7 @@ NS_ASSUME_NONNULL_END //% * **idx**: The index of the current value. //% * **stop**: A pointer to a boolean that when set stops the enumeration. //% **/ -//%- (void)enumerateRawValuesWithBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block; +//%- (void)enumerateRawValuesWithBlock:(void (NS_NOESCAPE ^)(TYPE value, NSUInteger idx, BOOL *stop))block; //% //%/** //% * Enumerates the values on this array with the given block. @@ -1791,7 +1791,7 @@ NS_ASSUME_NONNULL_END //% * **stop**: A pointer to a boolean that when set stops the enumeration. //% **/ //%- (void)enumerateRawValuesWithOptions:(NSEnumerationOptions)opts -//% usingBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block; +//% usingBlock:(void (NS_NOESCAPE ^)(TYPE value, NSUInteger idx, BOOL *stop))block; //% //%// If value is not a valid enumerator as defined by validationFunc, these //%// methods will assert in debug, and will log in release and assign the value @@ -1821,7 +1821,7 @@ NS_ASSUME_NONNULL_END //% * **idx**: The index of the current value. //% * **stop**: A pointer to a boolean that when set stops the enumeration. //% **/ -//%- (void)enumerateValuesWithBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block; +//%- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(TYPE value, NSUInteger idx, BOOL *stop))block; //% //%/** //% * Enumerates the values on this array with the given block. @@ -1833,7 +1833,7 @@ NS_ASSUME_NONNULL_END //% * **stop**: A pointer to a boolean that when set stops the enumeration. //% **/ //%- (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts -//% usingBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block; +//% usingBlock:(void (NS_NOESCAPE ^)(TYPE value, NSUInteger idx, BOOL *stop))block; //%PDDM-DEFINE ARRAY_MUTABLE_INTERFACE(NAME, TYPE, HELPER_NAME) //%/** diff --git a/objectivec/GPBArray.m b/objectivec/GPBArray.m index 122e0304b3..5ce1e593cb 100644 --- a/objectivec/GPBArray.m +++ b/objectivec/GPBArray.m @@ -194,12 +194,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { //% return result; //%} //% -//%- (void)enumerate##ACCESSOR_NAME##ValuesWithBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block { +//%- (void)enumerate##ACCESSOR_NAME##ValuesWithBlock:(void (NS_NOESCAPE ^)(TYPE value, NSUInteger idx, BOOL *stop))block { //% [self enumerate##ACCESSOR_NAME##ValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; //%} //% //%- (void)enumerate##ACCESSOR_NAME##ValuesWithOptions:(NSEnumerationOptions)opts -//% ACCESSOR_NAME$S usingBlock:(void (^)(TYPE value, NSUInteger idx, BOOL *stop))block { +//% ACCESSOR_NAME$S usingBlock:(void (NS_NOESCAPE ^)(TYPE value, NSUInteger idx, BOOL *stop))block { //% // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). //% BOOL stop = NO; //% if ((opts & NSEnumerationReverse) == 0) { @@ -405,12 +405,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return result; } -- (void)enumerateValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block { [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; if ((opts & NSEnumerationReverse) == 0) { @@ -653,12 +653,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return result; } -- (void)enumerateValuesWithBlock:(void (^)(uint32_t value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(uint32_t value, NSUInteger idx, BOOL *stop))block { [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(uint32_t value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(uint32_t value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; if ((opts & NSEnumerationReverse) == 0) { @@ -901,12 +901,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return result; } -- (void)enumerateValuesWithBlock:(void (^)(int64_t value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(int64_t value, NSUInteger idx, BOOL *stop))block { [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(int64_t value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(int64_t value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; if ((opts & NSEnumerationReverse) == 0) { @@ -1149,12 +1149,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return result; } -- (void)enumerateValuesWithBlock:(void (^)(uint64_t value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(uint64_t value, NSUInteger idx, BOOL *stop))block { [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(uint64_t value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(uint64_t value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; if ((opts & NSEnumerationReverse) == 0) { @@ -1397,12 +1397,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return result; } -- (void)enumerateValuesWithBlock:(void (^)(float value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(float value, NSUInteger idx, BOOL *stop))block { [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(float value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(float value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; if ((opts & NSEnumerationReverse) == 0) { @@ -1645,12 +1645,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return result; } -- (void)enumerateValuesWithBlock:(void (^)(double value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(double value, NSUInteger idx, BOOL *stop))block { [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(double value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(double value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; if ((opts & NSEnumerationReverse) == 0) { @@ -1893,12 +1893,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return result; } -- (void)enumerateValuesWithBlock:(void (^)(BOOL value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(BOOL value, NSUInteger idx, BOOL *stop))block { [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(BOOL value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(BOOL value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; if ((opts & NSEnumerationReverse) == 0) { @@ -2165,12 +2165,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return result; } -- (void)enumerateRawValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateRawValuesWithBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block { [self enumerateRawValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateRawValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; if ((opts & NSEnumerationReverse) == 0) { @@ -2217,12 +2217,12 @@ static BOOL ArrayDefault_IsValidValue(int32_t value) { return _values[index]; } -- (void)enumerateValuesWithBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block { +- (void)enumerateValuesWithBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block { [self enumerateValuesWithOptions:(NSEnumerationOptions)0 usingBlock:block]; } - (void)enumerateValuesWithOptions:(NSEnumerationOptions)opts - usingBlock:(void (^)(int32_t value, NSUInteger idx, BOOL *stop))block { + usingBlock:(void (NS_NOESCAPE ^)(int32_t value, NSUInteger idx, BOOL *stop))block { // NSEnumerationConcurrent isn't currently supported (and Apple's docs say that is ok). BOOL stop = NO; GPBEnumValidationFunc func = _validationFunc; diff --git a/objectivec/GPBBootstrap.h b/objectivec/GPBBootstrap.h index ed53ae7cbc..0ebca25be6 100644 --- a/objectivec/GPBBootstrap.h +++ b/objectivec/GPBBootstrap.h @@ -91,6 +91,24 @@ #endif #endif +/** + * Attribute used for Objective-C proto interface deprecations without messages. + **/ +#ifndef GPB_DEPRECATED +#define GPB_DEPRECATED __attribute__((deprecated)) +#endif + +/** + * Attribute used for Objective-C proto interface deprecations with messages. + **/ +#ifndef GPB_DEPRECATED_MSG +#if __has_extension(attribute_deprecated_with_message) +#define GPB_DEPRECATED_MSG(msg) __attribute__((deprecated(msg))) +#else +#define GPB_DEPRECATED_MSG(msg) __attribute__((deprecated)) +#endif +#endif + // If property name starts with init we need to annotate it to get past ARC. // http://stackoverflow.com/questions/18723226/how-do-i-annotate-an-objective-c-property-with-an-objc-method-family/18723227#18723227 // diff --git a/objectivec/GPBCodedInputStream.m b/objectivec/GPBCodedInputStream.m index dd05ddb48e..57d04dde38 100644 --- a/objectivec/GPBCodedInputStream.m +++ b/objectivec/GPBCodedInputStream.m @@ -46,9 +46,9 @@ NSString *const GPBCodedInputStreamErrorDomain = GPBNSStringifySymbol(GPBCodedInputStreamErrorDomain); // Matching: -// https://github.com/google/protobuf/blob/master/java/core/src/main/java/com/google/protobuf/CodedInputStream.java#L62 +// https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/main/java/com/google/protobuf/CodedInputStream.java#L62 // private static final int DEFAULT_RECURSION_LIMIT = 100; -// https://github.com/google/protobuf/blob/master/src/google/protobuf/io/coded_stream.cc#L86 +// https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/io/coded_stream.cc#L86 // int CodedInputStream::default_recursion_limit_ = 100; static const NSUInteger kDefaultRecursionLimit = 100; diff --git a/objectivec/GPBDescriptor.h b/objectivec/GPBDescriptor.h index 292bce1371..331d4444ab 100644 --- a/objectivec/GPBDescriptor.h +++ b/objectivec/GPBDescriptor.h @@ -75,7 +75,7 @@ typedef NS_ENUM(uint8_t, GPBFieldType) { /** Number of extension ranges declared for the message. */ @property(nonatomic, readonly) uint32_t extensionRangesCount; /** Descriptor for the file where the message was defined. */ -@property(nonatomic, readonly, assign) GPBFileDescriptor *file; +@property(nonatomic, readonly) GPBFileDescriptor *file; /** Whether the message is in wire format or not. */ @property(nonatomic, readonly, getter=isWireFormat) BOOL wireFormat; @@ -188,10 +188,10 @@ typedef NS_ENUM(uint8_t, GPBFieldType) { @property(nonatomic, readonly, getter=isPackable) BOOL packable; /** The containing oneof if this field is part of one, nil otherwise. */ -@property(nonatomic, readonly, assign, nullable) GPBOneofDescriptor *containingOneof; +@property(nonatomic, readonly, nullable) GPBOneofDescriptor *containingOneof; /** Class of the message if the field is of message type. */ -@property(nonatomic, readonly, assign, nullable) Class msgClass; +@property(nonatomic, readonly, nullable) Class msgClass; /** Descriptor for the enum if this field is an enum. */ @property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor; @@ -305,7 +305,7 @@ typedef NS_ENUM(uint8_t, GPBFieldType) { /** Whether the extension is packable. */ @property(nonatomic, readonly, getter=isPackable) BOOL packable; /** The class of the message if the extension is of message type. */ -@property(nonatomic, readonly, assign) Class msgClass; +@property(nonatomic, readonly) Class msgClass; /** The singleton name for the extension. */ @property(nonatomic, readonly) NSString *singletonName; /** The enum descriptor if the extension is of enum type. */ diff --git a/objectivec/GPBDescriptor.m b/objectivec/GPBDescriptor.m index ad46ef4f03..41bf5adbcb 100644 --- a/objectivec/GPBDescriptor.m +++ b/objectivec/GPBDescriptor.m @@ -819,6 +819,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { return; } uint32_t *offsets = malloc(valueCount_ * sizeof(uint32_t)); + if (!offsets) return; const char *scan = valueNames_; for (uint32_t i = 0; i < valueCount_; ++i) { offsets[i] = (uint32_t)(scan - valueNames_); @@ -851,6 +852,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { nameAsCStr += prefixLen; if (nameOffsets_ == NULL) [self calcValueNameOffsets]; + if (nameOffsets_ == NULL) return NO; // Find it. for (uint32_t i = 0; i < valueCount_; ++i) { @@ -867,13 +869,13 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { - (BOOL)getValue:(int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName { if (nameOffsets_ == NULL) [self calcValueNameOffsets]; + if (nameOffsets_ == NULL) return NO; for (uint32_t i = 0; i < valueCount_; ++i) { - int32_t value = values_[i]; - NSString *valueTextFormatName = [self textFormatNameForValue:value]; + NSString *valueTextFormatName = [self getEnumTextFormatNameForIndex:i]; if ([valueTextFormatName isEqual:textFormatName]) { if (outValue) { - *outValue = value; + *outValue = values_[i]; } return YES; } @@ -905,6 +907,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { - (NSString *)getEnumNameForIndex:(uint32_t)index { if (nameOffsets_ == NULL) [self calcValueNameOffsets]; + if (nameOffsets_ == NULL) return nil; if (index >= valueCount_) { return nil; @@ -916,6 +919,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { - (NSString *)getEnumTextFormatNameForIndex:(uint32_t)index { if (nameOffsets_ == NULL) [self calcValueNameOffsets]; + if (nameOffsets_ == NULL) return nil; if (index >= valueCount_) { return nil; diff --git a/objectivec/GPBDictionary.h b/objectivec/GPBDictionary.h index a81165e815..d00b5b3116 100644 --- a/objectivec/GPBDictionary.h +++ b/objectivec/GPBDictionary.h @@ -109,7 +109,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(uint32_t key, uint32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, uint32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -204,7 +204,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(uint32_t key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -299,7 +299,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(uint32_t key, uint64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, uint64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -394,7 +394,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(uint32_t key, int64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, int64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -489,7 +489,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(uint32_t key, BOOL value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, BOOL value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -584,7 +584,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(uint32_t key, float value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, float value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -679,7 +679,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(uint32_t key, double value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, double value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -795,7 +795,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(uint32_t key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block; /** * Gets the raw enum value for the given key. @@ -822,7 +822,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(uint32_t key, int32_t rawValue, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, int32_t rawValue, BOOL *stop))block; /** * Adds the keys and raw enum values from another dictionary. @@ -935,7 +935,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(uint32_t key, ObjectType object, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint32_t key, ObjectType object, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1030,7 +1030,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(int32_t key, uint32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, uint32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1125,7 +1125,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(int32_t key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1220,7 +1220,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(int32_t key, uint64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, uint64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1315,7 +1315,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(int32_t key, int64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, int64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1410,7 +1410,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(int32_t key, BOOL value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, BOOL value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1505,7 +1505,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(int32_t key, float value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, float value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1600,7 +1600,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(int32_t key, double value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, double value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1716,7 +1716,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(int32_t key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block; /** * Gets the raw enum value for the given key. @@ -1743,7 +1743,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(int32_t key, int32_t rawValue, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, int32_t rawValue, BOOL *stop))block; /** * Adds the keys and raw enum values from another dictionary. @@ -1856,7 +1856,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(int32_t key, ObjectType object, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int32_t key, ObjectType object, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -1951,7 +1951,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(uint64_t key, uint32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, uint32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2046,7 +2046,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(uint64_t key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2141,7 +2141,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(uint64_t key, uint64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, uint64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2236,7 +2236,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(uint64_t key, int64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, int64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2331,7 +2331,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(uint64_t key, BOOL value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, BOOL value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2426,7 +2426,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(uint64_t key, float value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, float value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2521,7 +2521,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(uint64_t key, double value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, double value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2637,7 +2637,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(uint64_t key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block; /** * Gets the raw enum value for the given key. @@ -2664,7 +2664,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(uint64_t key, int32_t rawValue, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, int32_t rawValue, BOOL *stop))block; /** * Adds the keys and raw enum values from another dictionary. @@ -2777,7 +2777,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(uint64_t key, ObjectType object, BOOL *stop))block; + (void (NS_NOESCAPE ^)(uint64_t key, ObjectType object, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2872,7 +2872,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(int64_t key, uint32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, uint32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -2967,7 +2967,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(int64_t key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3062,7 +3062,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(int64_t key, uint64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, uint64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3157,7 +3157,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(int64_t key, int64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, int64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3252,7 +3252,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(int64_t key, BOOL value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, BOOL value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3347,7 +3347,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(int64_t key, float value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, float value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3442,7 +3442,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(int64_t key, double value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, double value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3558,7 +3558,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(int64_t key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block; /** * Gets the raw enum value for the given key. @@ -3585,7 +3585,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(int64_t key, int32_t rawValue, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, int32_t rawValue, BOOL *stop))block; /** * Adds the keys and raw enum values from another dictionary. @@ -3698,7 +3698,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(int64_t key, ObjectType object, BOOL *stop))block; + (void (NS_NOESCAPE ^)(int64_t key, ObjectType object, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3793,7 +3793,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(BOOL key, uint32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, uint32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3888,7 +3888,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(BOOL key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, int32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -3983,7 +3983,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(BOOL key, uint64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, uint64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4078,7 +4078,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(BOOL key, int64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, int64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4173,7 +4173,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(BOOL key, BOOL value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, BOOL value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4268,7 +4268,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(BOOL key, float value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, float value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4363,7 +4363,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(BOOL key, double value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, double value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4479,7 +4479,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(BOOL key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, int32_t value, BOOL *stop))block; /** * Gets the raw enum value for the given key. @@ -4506,7 +4506,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, int32_t rawValue, BOOL *stop))block; /** * Adds the keys and raw enum values from another dictionary. @@ -4619,7 +4619,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(BOOL key, ObjectType object, BOOL *stop))block; + (void (NS_NOESCAPE ^)(BOOL key, ObjectType object, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4714,7 +4714,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(NSString *key, uint32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, uint32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4809,7 +4809,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(NSString *key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4904,7 +4904,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(NSString *key, uint64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, uint64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -4999,7 +4999,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(NSString *key, int64_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, int64_t value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -5094,7 +5094,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(NSString *key, BOOL value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, BOOL value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -5189,7 +5189,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(NSString *key, float value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, float value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -5284,7 +5284,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(NSString *key, double value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, double value, BOOL *stop))block; /** * Adds the keys and values from another dictionary. @@ -5400,7 +5400,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(NSString *key, int32_t value, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block; /** * Gets the raw enum value for the given key. @@ -5427,7 +5427,7 @@ NS_ASSUME_NONNULL_BEGIN * **stop**: A pointer to a boolean that when set stops the enumeration. **/ - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(NSString *key, int32_t rawValue, BOOL *stop))block; + (void (NS_NOESCAPE ^)(NSString *key, int32_t rawValue, BOOL *stop))block; /** * Adds the keys and raw enum values from another dictionary. @@ -5693,7 +5693,7 @@ NS_ASSUME_NONNULL_END //% * **stop**: A pointer to a boolean that when set stops the enumeration. //% **/ //%- (void)enumerateKeysAndRawValuesUsingBlock: -//% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE rawValue, BOOL *stop))block; +//% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE rawValue, BOOL *stop))block; //% //%/** //% * Adds the keys and raw enum values from another dictionary. @@ -5728,7 +5728,7 @@ NS_ASSUME_NONNULL_END //% * **stop**: ##VNAME_VAR$S## A pointer to a boolean that when set stops the enumeration. //% **/ //%- (void)enumerateKeysAnd##VNAME##sUsingBlock: -//% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block; +//% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block; //%PDDM-DEFINE DICTIONARY_MUTABLE_INTERFACE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, VHELPER, VNAME, VNAME_VAR) //%/** diff --git a/objectivec/GPBDictionary.m b/objectivec/GPBDictionary.m index cedf5e549d..1bb24be0cd 100644 --- a/objectivec/GPBDictionary.m +++ b/objectivec/GPBDictionary.m @@ -719,7 +719,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //%} //% //%- (void)enumerateKeysAndEnumsUsingBlock: -//% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block { +//% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block { //% GPBEnumValidationFunc func = _validationFunc; //% BOOL stop = NO; //% NSEnumerator *keys = [_dictionary keyEnumerator]; @@ -792,7 +792,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //%} //% //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock: -//% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block { +//% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block { //% BOOL stop = NO; //% NSDictionary *internal = _dictionary; //% NSEnumerator *keys = [internal keyEnumerator]; @@ -857,7 +857,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)]; //%} //% -//%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +//%- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop) { //% #pragma unused(stop) //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR)); @@ -967,7 +967,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //% //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP) //% -//%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +//%- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { //% if (BOOL_DICT_HAS##HELPER(0, )) { //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0])); //% } @@ -977,7 +977,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //%} //% //%- (void)enumerateKeysAnd##VNAME##sUsingBlock: -//% (void (^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block { +//% (void (NS_NOESCAPE ^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block { //% BOOL stop = NO; //% if (BOOL_DICT_HAS##HELPER(0, )) { //% block(NO, _values[0], &stop); @@ -1505,7 +1505,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(uint32_t key, uint32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, uint32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -1570,7 +1570,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]); @@ -1688,7 +1688,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(uint32_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -1753,7 +1753,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]); @@ -1871,7 +1871,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(uint32_t key, uint64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, uint64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -1936,7 +1936,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]); @@ -2054,7 +2054,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(uint32_t key, int64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, int64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -2119,7 +2119,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]); @@ -2237,7 +2237,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(uint32_t key, BOOL value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, BOOL value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -2302,7 +2302,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false")); @@ -2420,7 +2420,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(uint32_t key, float value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, float value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -2485,7 +2485,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); @@ -2603,7 +2603,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(uint32_t key, double value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, double value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -2668,7 +2668,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); @@ -2799,7 +2799,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(uint32_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -2876,7 +2876,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], @(value)); @@ -2904,7 +2904,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(uint32_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block { GPBEnumValidationFunc func = _validationFunc; BOOL stop = NO; NSEnumerator *keys = [_dictionary keyEnumerator]; @@ -3042,7 +3042,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(uint32_t key, id object, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint32_t key, id object, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -3131,7 +3131,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%u", key], object); @@ -3253,7 +3253,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(int32_t key, uint32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, uint32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -3318,7 +3318,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]); @@ -3436,7 +3436,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(int32_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -3501,7 +3501,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]); @@ -3619,7 +3619,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(int32_t key, uint64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, uint64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -3684,7 +3684,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]); @@ -3802,7 +3802,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(int32_t key, int64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, int64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -3867,7 +3867,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]); @@ -3985,7 +3985,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(int32_t key, BOOL value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, BOOL value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -4050,7 +4050,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false")); @@ -4168,7 +4168,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(int32_t key, float value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, float value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -4233,7 +4233,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); @@ -4351,7 +4351,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(int32_t key, double value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, double value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -4416,7 +4416,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); @@ -4547,7 +4547,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(int32_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -4624,7 +4624,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], @(value)); @@ -4652,7 +4652,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(int32_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block { GPBEnumValidationFunc func = _validationFunc; BOOL stop = NO; NSEnumerator *keys = [_dictionary keyEnumerator]; @@ -4790,7 +4790,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(int32_t key, id object, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int32_t key, id object, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -4879,7 +4879,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:value->valueString forKey:@(key->valueInt32)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%d", key], object); @@ -5001,7 +5001,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(uint64_t key, uint32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, uint32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -5066,7 +5066,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]); @@ -5184,7 +5184,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(uint64_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -5249,7 +5249,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]); @@ -5367,7 +5367,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(uint64_t key, uint64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, uint64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -5432,7 +5432,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]); @@ -5550,7 +5550,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(uint64_t key, int64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, int64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -5615,7 +5615,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]); @@ -5733,7 +5733,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(uint64_t key, BOOL value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, BOOL value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -5798,7 +5798,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false")); @@ -5916,7 +5916,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(uint64_t key, float value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, float value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -5981,7 +5981,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); @@ -6099,7 +6099,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(uint64_t key, double value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, double value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -6164,7 +6164,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); @@ -6295,7 +6295,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(uint64_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -6372,7 +6372,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], @(value)); @@ -6400,7 +6400,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(uint64_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block { GPBEnumValidationFunc func = _validationFunc; BOOL stop = NO; NSEnumerator *keys = [_dictionary keyEnumerator]; @@ -6538,7 +6538,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(uint64_t key, id object, BOOL *stop))block { + (void (NS_NOESCAPE ^)(uint64_t key, id object, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -6627,7 +6627,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%llu", key], object); @@ -6749,7 +6749,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(int64_t key, uint32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, uint32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -6814,7 +6814,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]); @@ -6932,7 +6932,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(int64_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -6997,7 +6997,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]); @@ -7115,7 +7115,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(int64_t key, uint64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, uint64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -7180,7 +7180,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]); @@ -7298,7 +7298,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(int64_t key, int64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, int64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -7363,7 +7363,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]); @@ -7481,7 +7481,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(int64_t key, BOOL value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, BOOL value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -7546,7 +7546,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false")); @@ -7664,7 +7664,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(int64_t key, float value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, float value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -7729,7 +7729,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); @@ -7847,7 +7847,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(int64_t key, double value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, double value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -7912,7 +7912,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); @@ -8043,7 +8043,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(int64_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -8120,7 +8120,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], @(value)); @@ -8148,7 +8148,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(int64_t key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block { GPBEnumValidationFunc func = _validationFunc; BOOL stop = NO; NSEnumerator *keys = [_dictionary keyEnumerator]; @@ -8286,7 +8286,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(int64_t key, id object, BOOL *stop))block { + (void (NS_NOESCAPE ^)(int64_t key, id object, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -8375,7 +8375,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:value->valueString forKey:@(key->valueInt64)]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) { #pragma unused(stop) block([NSString stringWithFormat:@"%lld", key], object); @@ -8501,7 +8501,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(NSString *key, uint32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, uint32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -8566,7 +8566,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) { #pragma unused(stop) block(key, [NSString stringWithFormat:@"%u", value]); @@ -8692,7 +8692,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(NSString *key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -8757,7 +8757,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt32) forKey:key->valueString]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, BOOL *stop) { #pragma unused(stop) block(key, [NSString stringWithFormat:@"%d", value]); @@ -8883,7 +8883,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(NSString *key, uint64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, uint64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -8948,7 +8948,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) { #pragma unused(stop) block(key, [NSString stringWithFormat:@"%llu", value]); @@ -9074,7 +9074,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(NSString *key, int64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, int64_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -9139,7 +9139,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueInt64) forKey:key->valueString]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, BOOL *stop) { #pragma unused(stop) block(key, [NSString stringWithFormat:@"%lld", value]); @@ -9265,7 +9265,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(NSString *key, BOOL value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, BOOL value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -9330,7 +9330,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueBool) forKey:key->valueString]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, BOOL *stop) { #pragma unused(stop) block(key, (value ? @"true" : @"false")); @@ -9456,7 +9456,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(NSString *key, float value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, float value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -9521,7 +9521,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueFloat) forKey:key->valueString]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, BOOL *stop) { #pragma unused(stop) block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); @@ -9647,7 +9647,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(NSString *key, double value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, double value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -9712,7 +9712,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueDouble) forKey:key->valueString]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, BOOL *stop) { #pragma unused(stop) block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); @@ -9851,7 +9851,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(NSString *key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block { BOOL stop = NO; NSDictionary *internal = _dictionary; NSEnumerator *keys = [internal keyEnumerator]; @@ -9928,7 +9928,7 @@ void GPBDictionaryReadEntry(id mapDictionary, [_dictionary setObject:@(value->valueEnum) forKey:key->valueString]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) { #pragma unused(stop) block(key, @(value)); @@ -9956,7 +9956,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(NSString *key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block { GPBEnumValidationFunc func = _validationFunc; BOOL stop = NO; NSEnumerator *keys = [_dictionary keyEnumerator]; @@ -10143,7 +10143,7 @@ void GPBDictionaryReadEntry(id mapDictionary, _valueSet[idx] = YES; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_valueSet[0]) { block(@"false", [NSString stringWithFormat:@"%u", _values[0]]); } @@ -10153,7 +10153,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt32sUsingBlock: - (void (^)(BOOL key, uint32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, uint32_t value, BOOL *stop))block { BOOL stop = NO; if (_valueSet[0]) { block(NO, _values[0], &stop); @@ -10352,7 +10352,7 @@ void GPBDictionaryReadEntry(id mapDictionary, _valueSet[idx] = YES; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_valueSet[0]) { block(@"false", [NSString stringWithFormat:@"%d", _values[0]]); } @@ -10362,7 +10362,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt32sUsingBlock: - (void (^)(BOOL key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, int32_t value, BOOL *stop))block { BOOL stop = NO; if (_valueSet[0]) { block(NO, _values[0], &stop); @@ -10561,7 +10561,7 @@ void GPBDictionaryReadEntry(id mapDictionary, _valueSet[idx] = YES; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_valueSet[0]) { block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]); } @@ -10571,7 +10571,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndUInt64sUsingBlock: - (void (^)(BOOL key, uint64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, uint64_t value, BOOL *stop))block { BOOL stop = NO; if (_valueSet[0]) { block(NO, _values[0], &stop); @@ -10770,7 +10770,7 @@ void GPBDictionaryReadEntry(id mapDictionary, _valueSet[idx] = YES; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_valueSet[0]) { block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]); } @@ -10780,7 +10780,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndInt64sUsingBlock: - (void (^)(BOOL key, int64_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, int64_t value, BOOL *stop))block { BOOL stop = NO; if (_valueSet[0]) { block(NO, _values[0], &stop); @@ -10979,7 +10979,7 @@ void GPBDictionaryReadEntry(id mapDictionary, _valueSet[idx] = YES; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_valueSet[0]) { block(@"false", (_values[0] ? @"true" : @"false")); } @@ -10989,7 +10989,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndBoolsUsingBlock: - (void (^)(BOOL key, BOOL value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, BOOL value, BOOL *stop))block { BOOL stop = NO; if (_valueSet[0]) { block(NO, _values[0], &stop); @@ -11188,7 +11188,7 @@ void GPBDictionaryReadEntry(id mapDictionary, _valueSet[idx] = YES; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_valueSet[0]) { block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]); } @@ -11198,7 +11198,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndFloatsUsingBlock: - (void (^)(BOOL key, float value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, float value, BOOL *stop))block { BOOL stop = NO; if (_valueSet[0]) { block(NO, _values[0], &stop); @@ -11397,7 +11397,7 @@ void GPBDictionaryReadEntry(id mapDictionary, _valueSet[idx] = YES; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_valueSet[0]) { block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]); } @@ -11407,7 +11407,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndDoublesUsingBlock: - (void (^)(BOOL key, double value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, double value, BOOL *stop))block { BOOL stop = NO; if (_valueSet[0]) { block(NO, _values[0], &stop); @@ -11598,7 +11598,7 @@ void GPBDictionaryReadEntry(id mapDictionary, _values[idx] = [value->valueString retain]; } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_values[0] != nil) { block(@"false", _values[0]); } @@ -11608,7 +11608,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndObjectsUsingBlock: - (void (^)(BOOL key, id object, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, id object, BOOL *stop))block { BOOL stop = NO; if (_values[0] != nil) { block(NO, _values[0], &stop); @@ -11857,7 +11857,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndRawValuesUsingBlock: - (void (^)(BOOL key, int32_t value, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, int32_t value, BOOL *stop))block { BOOL stop = NO; if (_valueSet[0]) { block(NO, _values[0], &stop); @@ -11868,7 +11868,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)enumerateKeysAndEnumsUsingBlock: - (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block { + (void (NS_NOESCAPE ^)(BOOL key, int32_t rawValue, BOOL *stop))block { BOOL stop = NO; GPBEnumValidationFunc func = _validationFunc; int32_t validatedValue; @@ -11942,7 +11942,7 @@ void GPBDictionaryReadEntry(id mapDictionary, } } -- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { +- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block { if (_valueSet[0]) { block(@"false", @(_values[0])); } diff --git a/objectivec/GPBMessage.h b/objectivec/GPBMessage.h index 276740d2f9..01253a4afc 100644 --- a/objectivec/GPBMessage.h +++ b/objectivec/GPBMessage.h @@ -308,7 +308,7 @@ CF_EXTERN_C_END - (void)writeToOutputStream:(NSOutputStream *)output; /** - * Writes out a varint for the message size followed by the the message to + * Writes out a varint for the message size followed by the message to * the given output stream. * * @param output The coded output stream into which to write the message. @@ -318,7 +318,7 @@ CF_EXTERN_C_END - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output; /** - * Writes out a varint for the message size followed by the the message to + * Writes out a varint for the message size followed by the message to * the given output stream. * * @param output The output stream into which to write the message. diff --git a/objectivec/GPBMessage.m b/objectivec/GPBMessage.m index db5d3b60cd..3d5eccd3e6 100644 --- a/objectivec/GPBMessage.m +++ b/objectivec/GPBMessage.m @@ -1283,16 +1283,20 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) { NSUInteger fieldCount = fieldsArray.count; const GPBExtensionRange *extensionRanges = descriptor.extensionRanges; NSUInteger extensionRangesCount = descriptor.extensionRangesCount; + NSArray *sortedExtensions = + [[extensionMap_ allKeys] sortedArrayUsingSelector:@selector(compareByFieldNumber:)]; for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) { if (i == fieldCount) { [self writeExtensionsToCodedOutputStream:output - range:extensionRanges[j++]]; + range:extensionRanges[j++] + sortedExtensions:sortedExtensions]; } else if (j == extensionRangesCount || GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) { [self writeField:fieldsArray[i++] toCodedOutputStream:output]; } else { [self writeExtensionsToCodedOutputStream:output - range:extensionRanges[j++]]; + range:extensionRanges[j++] + sortedExtensions:sortedExtensions]; } } if (descriptor.isWireFormat) { @@ -1808,17 +1812,20 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) { } - (void)writeExtensionsToCodedOutputStream:(GPBCodedOutputStream *)output - range:(GPBExtensionRange)range { - NSArray *sortedExtensions = [[extensionMap_ allKeys] - sortedArrayUsingSelector:@selector(compareByFieldNumber:)]; + range:(GPBExtensionRange)range + sortedExtensions:(NSArray *)sortedExtensions { uint32_t start = range.start; uint32_t end = range.end; for (GPBExtensionDescriptor *extension in sortedExtensions) { uint32_t fieldNumber = extension.fieldNumber; - if (fieldNumber >= start && fieldNumber < end) { - id value = [extensionMap_ objectForKey:extension]; - GPBWriteExtensionValueToOutputStream(extension, value, output); + if (fieldNumber < start) { + continue; + } + if (fieldNumber >= end) { + break; } + id value = [extensionMap_ objectForKey:extension]; + GPBWriteExtensionValueToOutputStream(extension, value, output); } } @@ -3084,6 +3091,14 @@ static void ResolveIvarSet(__unsafe_unretained GPBFieldDescriptor *field, result->encodingSelector = @selector(set##NAME:); \ break; \ } +#define CASE_SET_COPY(NAME) \ + case GPBDataType##NAME: { \ + result->impToAdd = imp_implementationWithBlock(^(id obj, id value) { \ + return GPBSetRetainedObjectIvarWithFieldInternal(obj, field, [value copy], syntax); \ + }); \ + result->encodingSelector = @selector(set##NAME:); \ + break; \ + } CASE_SET(Bool, BOOL, Bool) CASE_SET(Fixed32, uint32_t, UInt32) CASE_SET(SFixed32, int32_t, Int32) @@ -3097,8 +3112,8 @@ static void ResolveIvarSet(__unsafe_unretained GPBFieldDescriptor *field, CASE_SET(SInt64, int64_t, Int64) CASE_SET(UInt32, uint32_t, UInt32) CASE_SET(UInt64, uint64_t, UInt64) - CASE_SET(Bytes, id, Object) - CASE_SET(String, id, Object) + CASE_SET_COPY(Bytes) + CASE_SET_COPY(String) CASE_SET(Message, id, Object) CASE_SET(Group, id, Object) CASE_SET(Enum, int32_t, Enum) @@ -3178,7 +3193,7 @@ static void ResolveIvarSet(__unsafe_unretained GPBFieldDescriptor *field, // full lookup. const GPBFileSyntax syntax = descriptor.file.syntax; result.impToAdd = imp_implementationWithBlock(^(id obj, id value) { - return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax); + GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax); }); result.encodingSelector = @selector(setArray:); break; @@ -3241,6 +3256,22 @@ static void ResolveIvarSet(__unsafe_unretained GPBFieldDescriptor *field, } - (void)encodeWithCoder:(NSCoder *)aCoder { +#if defined(DEBUG) && DEBUG + if (extensionMap_.count) { + // Hint to go along with the docs on GPBMessage about this. + // + // Note: This is incomplete, in that it only checked the "root" message, + // if a sub message in a field has extensions, the issue still exists. A + // recursive check could be done here (like the work in + // GPBMessageDropUnknownFieldsRecursively()), but that has the potential to + // be expensive and could slow down serialization in DEBUG enought to cause + // developers other problems. + NSLog(@"Warning: writing out a GPBMessage (%@) via NSCoding and it" + @" has %ld extensions; when read back in, those fields will be" + @" in the unknownFields property instead.", + [self class], (long)extensionMap_.count); + } +#endif NSData *data = [self data]; if (data.length) { [aCoder encodeObject:data forKey:kGPBDataCoderKey]; diff --git a/objectivec/GPBRuntimeTypes.h b/objectivec/GPBRuntimeTypes.h index 4d552060b0..81480548fe 100644 --- a/objectivec/GPBRuntimeTypes.h +++ b/objectivec/GPBRuntimeTypes.h @@ -74,7 +74,7 @@ typedef union { /** * Enum listing the possible data types that a field can contain. - * + * * @note Do not change the order of this enum (or add things to it) without * thinking about it very carefully. There are several things that depend * on the order. diff --git a/objectivec/GPBUtilities.m b/objectivec/GPBUtilities.m index e2a12ca4d3..0d3a080083 100644 --- a/objectivec/GPBUtilities.m +++ b/objectivec/GPBUtilities.m @@ -448,6 +448,36 @@ void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof, //% GPBSetObjectIvarWithField(self, field, (id)value); //%} //% +//%PDDM-DEFINE IVAR_ALIAS_DEFN_COPY_OBJECT(NAME, TYPE) +//%// Only exists for public api, no core code should use this. +//%TYPE *GPBGetMessage##NAME##Field(GPBMessage *self, +//% TYPE$S NAME$S GPBFieldDescriptor *field) { +//%#if defined(DEBUG) && DEBUG +//% NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field), +//% GPBDataType##NAME), +//% @"Attempting to get value of TYPE from field %@ " +//% @"of %@ which is of type %@.", +//% [self class], field.name, +//% TypeToString(GPBGetFieldDataType(field))); +//%#endif +//% return (TYPE *)GPBGetObjectIvarWithField(self, field); +//%} +//% +//%// Only exists for public api, no core code should use this. +//%void GPBSetMessage##NAME##Field(GPBMessage *self, +//% NAME$S GPBFieldDescriptor *field, +//% NAME$S TYPE *value) { +//%#if defined(DEBUG) && DEBUG +//% NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field), +//% GPBDataType##NAME), +//% @"Attempting to set field %@ of %@ which is of type %@ with " +//% @"value of type TYPE.", +//% [self class], field.name, +//% TypeToString(GPBGetFieldDataType(field))); +//%#endif +//% GPBSetCopyObjectIvarWithField(self, field, (id)value); +//%} +//% // Object types are handled slightly differently, they need to be released // and retained. @@ -483,6 +513,24 @@ static void GPBSetObjectIvarWithField(GPBMessage *self, syntax); } +static void GPBSetCopyObjectIvarWithField(GPBMessage *self, + GPBFieldDescriptor *field, id value); + +// GPBSetCopyObjectIvarWithField is blocked from the analyzer because it flags +// a leak for the -copy even though GPBSetRetainedObjectIvarWithFieldInternal +// is marked as consuming the value. Note: For some reason this doesn't happen +// with the -retain in GPBSetObjectIvarWithField. +#if !defined(__clang_analyzer__) +// This exists only for briging some aliased types, nothing else should use it. +static void GPBSetCopyObjectIvarWithField(GPBMessage *self, + GPBFieldDescriptor *field, id value) { + if (self == nil || field == nil) return; + GPBFileSyntax syntax = [self descriptor].file.syntax; + GPBSetRetainedObjectIvarWithFieldInternal(self, field, [value copy], + syntax); +} +#endif // !defined(__clang_analyzer__) + void GPBSetObjectIvarWithFieldInternal(GPBMessage *self, GPBFieldDescriptor *field, id value, GPBFileSyntax syntax) { @@ -1168,7 +1216,7 @@ void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self, // Aliases are function calls that are virtually the same. -//%PDDM-EXPAND IVAR_ALIAS_DEFN_OBJECT(String, NSString) +//%PDDM-EXPAND IVAR_ALIAS_DEFN_COPY_OBJECT(String, NSString) // This block of code is generated, do not edit it directly. // Only exists for public api, no core code should use this. @@ -1197,10 +1245,10 @@ void GPBSetMessageStringField(GPBMessage *self, [self class], field.name, TypeToString(GPBGetFieldDataType(field))); #endif - GPBSetObjectIvarWithField(self, field, (id)value); + GPBSetCopyObjectIvarWithField(self, field, (id)value); } -//%PDDM-EXPAND IVAR_ALIAS_DEFN_OBJECT(Bytes, NSData) +//%PDDM-EXPAND IVAR_ALIAS_DEFN_COPY_OBJECT(Bytes, NSData) // This block of code is generated, do not edit it directly. // Only exists for public api, no core code should use this. @@ -1229,7 +1277,7 @@ void GPBSetMessageBytesField(GPBMessage *self, [self class], field.name, TypeToString(GPBGetFieldDataType(field))); #endif - GPBSetObjectIvarWithField(self, field, (id)value); + GPBSetCopyObjectIvarWithField(self, field, (id)value); } //%PDDM-EXPAND IVAR_ALIAS_DEFN_OBJECT(Message, GPBMessage) @@ -1817,7 +1865,7 @@ static void AppendTextFormatForMessageExtensionRange(GPBMessage *message, // Not there yet. continue; } - if (fieldNumber > end) { + if (fieldNumber >= end) { // Done. break; } @@ -1901,10 +1949,10 @@ static void AppendTextFormatForMessageExtensionRange(GPBMessage *message, } // switch(extDataType) - } // for(numValues) + // End the line. + [toStr appendFormat:@"%@\n", lineEnding]; - // End the line. - [toStr appendFormat:@"%@\n", lineEnding]; + } // for(numValues) } // for..in(activeExtensions) } diff --git a/objectivec/ProtocolBuffers_OSX.xcodeproj/project.pbxproj b/objectivec/ProtocolBuffers_OSX.xcodeproj/project.pbxproj index a41be9f280..9b0af314fd 100644 --- a/objectivec/ProtocolBuffers_OSX.xcodeproj/project.pbxproj +++ b/objectivec/ProtocolBuffers_OSX.xcodeproj/project.pbxproj @@ -92,6 +92,7 @@ F4E675A11B21D0000054530B /* Struct.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675911B21D0000054530B /* Struct.pbobjc.m */; }; F4E675A31B21D0000054530B /* Type.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675931B21D0000054530B /* Type.pbobjc.m */; }; F4E675A51B21D0000054530B /* Wrappers.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675951B21D0000054530B /* Wrappers.pbobjc.m */; }; + F4F53F8A219CC4F2001EABF4 /* text_format_extensions_unittest_data.txt in Resources */ = {isa = PBXBuildFile; fileRef = F4F53F89219CC4F2001EABF4 /* text_format_extensions_unittest_data.txt */; }; F4F8D8831D789FD9002CE128 /* GPBUnittestProtos2.m in Sources */ = {isa = PBXBuildFile; fileRef = F4F8D8811D789FCE002CE128 /* GPBUnittestProtos2.m */; }; /* End PBXBuildFile section */ @@ -149,7 +150,7 @@ 8B09AAF614B663A7007B4184 /* unittest_objc.proto */ = {isa = PBXFileReference; lastKnownFileType = text; path = unittest_objc.proto; sourceTree = ""; }; 8B210CCD159383D60032D72D /* golden_message */ = {isa = PBXFileReference; lastKnownFileType = file; path = golden_message; sourceTree = ""; }; 8B210CCF159386920032D72D /* golden_packed_fields_message */ = {isa = PBXFileReference; lastKnownFileType = file; path = golden_packed_fields_message; sourceTree = ""; }; - 8B4248B81A8C254000BC1EC6 /* protobuf */ = {isa = PBXFileReference; lastKnownFileType = text; name = protobuf; path = ../../Intermediates/ProtocolBuffers_OSX.build/DerivedSources/protos/google/protobuf; sourceTree = BUILT_PRODUCTS_DIR; }; + 8B35468421A616F6000BD30D /* unittest_objc_options.proto */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.protobuf; path = unittest_objc_options.proto; sourceTree = ""; }; 8B4248B91A8C256900BC1EC6 /* UnitTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UnitTests-Bridging-Header.h"; sourceTree = ""; }; 8B4248BA1A8C256A00BC1EC6 /* GPBSwiftTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GPBSwiftTests.swift; sourceTree = ""; }; 8B4248CF1A927E1500BC1EC6 /* GPBWellKnownTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBWellKnownTypes.h; sourceTree = ""; }; @@ -266,6 +267,7 @@ F4E675AB1B21D05C0054530B /* struct.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = struct.proto; path = ../src/google/protobuf/struct.proto; sourceTree = ""; }; F4E675AC1B21D05C0054530B /* type.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = type.proto; path = ../src/google/protobuf/type.proto; sourceTree = ""; }; F4E675AD1B21D05C0054530B /* wrappers.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = wrappers.proto; path = ../src/google/protobuf/wrappers.proto; sourceTree = ""; }; + F4F53F89219CC4F2001EABF4 /* text_format_extensions_unittest_data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = text_format_extensions_unittest_data.txt; sourceTree = ""; }; F4F8D8811D789FCE002CE128 /* GPBUnittestProtos2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnittestProtos2.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -438,7 +440,6 @@ 7461B6940F94FDDD00A0C422 /* Tests */ = { isa = PBXGroup; children = ( - 8B4248B81A8C254000BC1EC6 /* protobuf */, 8B210CCD159383D60032D72D /* golden_message */, 8B210CCF159386920032D72D /* golden_packed_fields_message */, 8B8B615C17DF7056002EE618 /* GPBARCUnittestProtos.m */, @@ -496,6 +497,7 @@ 7461B6BA0F94FDF900A0C422 /* GPBUtilitiesTests.m */, 8B4248DB1A92933A00BC1EC6 /* GPBWellKnownTypesTest.m */, 7461B6BC0F94FDF900A0C422 /* GPBWireFormatTests.m */, + F4F53F89219CC4F2001EABF4 /* text_format_extensions_unittest_data.txt */, F45E57C61AE6DC6A000B7D99 /* text_format_map_unittest_data.txt */, F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */, 8B7E6A7414893DBA00F8884A /* unittest_custom_options.proto */, @@ -507,6 +509,7 @@ 8BBD9DB016DD1DC8008E1EC1 /* unittest_lite.proto */, 8B7E6A7B14893DBC00F8884A /* unittest_mset.proto */, 8B7E6A7C14893DBC00F8884A /* unittest_no_generic_services.proto */, + 8B35468421A616F6000BD30D /* unittest_objc_options.proto */, F4CF31701B162ED800BD9B06 /* unittest_objc_startup.proto */, 8B09AAF614B663A7007B4184 /* unittest_objc.proto */, 8B7E6A7D14893DBC00F8884A /* unittest_optimize_for.proto */, @@ -640,7 +643,7 @@ LastUpgradeCheck = 0930; TargetAttributes = { 8BBEA4A5147C727100C4ADB7 = { - LastSwiftMigration = 0800; + LastSwiftMigration = 0940; TestTargetID = 8B9A5EA41831993600A9D33B; }; F45BBC141B0CE3C6002D064D = { @@ -673,6 +676,7 @@ buildActionMask = 2147483647; files = ( 8B210CCE159383D60032D72D /* golden_message in Resources */, + F4F53F8A219CC4F2001EABF4 /* text_format_extensions_unittest_data.txt in Resources */, F43C88D0191D77FC009E917D /* text_format_unittest_data.txt in Resources */, 8B210CD0159386920032D72D /* golden_packed_fields_message in Resources */, F45E57C71AE6DC6A000B7D99 /* text_format_map_unittest_data.txt in Resources */, @@ -820,8 +824,8 @@ buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; - HEADER_SEARCH_PATHS = "$(SRCROOT)"; PRODUCT_NAME = ProtocolBuffers; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; }; name = Debug; }; @@ -830,8 +834,8 @@ buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; - HEADER_SEARCH_PATHS = "$(SRCROOT)"; PRODUCT_NAME = ProtocolBuffers; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; }; name = Release; }; @@ -841,10 +845,6 @@ CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; - HEADER_SEARCH_PATHS = ( - "${PROJECT_DERIVED_FILE_DIR}/protos", - "$(SRCROOT)", - ); INFOPLIST_FILE = "Tests/UnitTests-Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; OTHER_LDFLAGS = "-ObjC"; @@ -852,7 +852,8 @@ PRODUCT_NAME = UnitTests; SWIFT_OBJC_BRIDGING_HEADER = "Tests/UnitTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; + USER_HEADER_SEARCH_PATHS = "${PROJECT_DERIVED_FILE_DIR}/protos $(SRCROOT)"; WARNING_CFLAGS = ( "$(inherited)", "-Wno-documentation-unknown-command", @@ -868,17 +869,14 @@ CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; - HEADER_SEARCH_PATHS = ( - "${PROJECT_DERIVED_FILE_DIR}/protos", - "$(SRCROOT)", - ); INFOPLIST_FILE = "Tests/UnitTests-Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; OTHER_LDFLAGS = "-ObjC"; PRODUCT_BUNDLE_IDENTIFIER = "com.yourcompany.${PRODUCT_NAME:identifier}"; PRODUCT_NAME = UnitTests; SWIFT_OBJC_BRIDGING_HEADER = "Tests/UnitTests-Bridging-Header.h"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; + USER_HEADER_SEARCH_PATHS = "${PROJECT_DERIVED_FILE_DIR}/protos $(SRCROOT)"; WARNING_CFLAGS = ( "$(inherited)", "-Wno-documentation-unknown-command", @@ -891,7 +889,7 @@ C01FCF4F08A954540054247B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ALWAYS_SEARCH_USER_PATHS = YES; + ALWAYS_SEARCH_USER_PATHS = NO; CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES; CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES; @@ -947,6 +945,7 @@ ONLY_ACTIVE_ARCH = YES; RUN_CLANG_STATIC_ANALYZER = YES; SDKROOT = macosx; + USE_HEADERMAP = NO; WARNING_CFLAGS = ( "-Wdocumentation-unknown-command", "-Wundef", @@ -961,7 +960,7 @@ C01FCF5008A954540054247B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ALWAYS_SEARCH_USER_PATHS = YES; + ALWAYS_SEARCH_USER_PATHS = NO; CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES; CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES; @@ -1015,6 +1014,7 @@ RUN_CLANG_STATIC_ANALYZER = YES; SDKROOT = macosx; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + USE_HEADERMAP = NO; WARNING_CFLAGS = ( "-Wdocumentation-unknown-command", "-Wundef", @@ -1031,8 +1031,8 @@ buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; - HEADER_SEARCH_PATHS = "$(SRCROOT)"; PRODUCT_NAME = TestSingleSourceBuild; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; }; name = Debug; }; @@ -1041,8 +1041,8 @@ buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; - HEADER_SEARCH_PATHS = "$(SRCROOT)"; PRODUCT_NAME = TestSingleSourceBuild; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; }; name = Release; }; diff --git a/objectivec/ProtocolBuffers_iOS.xcodeproj/project.pbxproj b/objectivec/ProtocolBuffers_iOS.xcodeproj/project.pbxproj index 470652d07b..9f1d825c6f 100644 --- a/objectivec/ProtocolBuffers_iOS.xcodeproj/project.pbxproj +++ b/objectivec/ProtocolBuffers_iOS.xcodeproj/project.pbxproj @@ -93,6 +93,7 @@ F4E675D51B21D1620054530B /* Struct.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675C21B21D1440054530B /* Struct.pbobjc.m */; }; F4E675D61B21D1620054530B /* Type.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675C51B21D1440054530B /* Type.pbobjc.m */; }; F4E675D71B21D1620054530B /* Wrappers.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675C71B21D1440054530B /* Wrappers.pbobjc.m */; }; + F4F53F8C219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt in Resources */ = {isa = PBXBuildFile; fileRef = F4F53F8B219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt */; }; F4F8D8861D78A193002CE128 /* GPBUnittestProtos2.m in Sources */ = {isa = PBXBuildFile; fileRef = F4F8D8841D78A186002CE128 /* GPBUnittestProtos2.m */; }; /* End PBXBuildFile section */ @@ -150,9 +151,9 @@ 8B09AAF614B663A7007B4184 /* unittest_objc.proto */ = {isa = PBXFileReference; lastKnownFileType = text; path = unittest_objc.proto; sourceTree = ""; }; 8B210CCD159383D60032D72D /* golden_message */ = {isa = PBXFileReference; lastKnownFileType = file; path = golden_message; sourceTree = ""; }; 8B210CCF159386920032D72D /* golden_packed_fields_message */ = {isa = PBXFileReference; lastKnownFileType = file; path = golden_packed_fields_message; sourceTree = ""; }; + 8B35468621A61EB2000BD30D /* unittest_objc_options.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = unittest_objc_options.proto; sourceTree = ""; }; 8B4248B21A8BD96D00BC1EC6 /* UnitTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UnitTests-Bridging-Header.h"; sourceTree = ""; }; 8B4248B31A8BD96E00BC1EC6 /* GPBSwiftTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GPBSwiftTests.swift; sourceTree = ""; }; - 8B4248B71A8BDD9600BC1EC6 /* protobuf */ = {isa = PBXFileReference; lastKnownFileType = text; name = protobuf; path = ../../Intermediates/ProtocolBuffers_iOS.build/DerivedSources/protos/google/protobuf; sourceTree = BUILT_PRODUCTS_DIR; }; 8B4248DD1A929C7D00BC1EC6 /* Duration.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Duration.pbobjc.h; path = google/protobuf/Duration.pbobjc.h; sourceTree = ""; }; 8B4248DE1A929C7D00BC1EC6 /* Duration.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Duration.pbobjc.m; path = google/protobuf/Duration.pbobjc.m; sourceTree = ""; }; 8B4248E01A929C7D00BC1EC6 /* Timestamp.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Timestamp.pbobjc.m; path = google/protobuf/Timestamp.pbobjc.m; sourceTree = ""; }; @@ -269,6 +270,7 @@ F4E675DD1B21D1DE0054530B /* struct.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = struct.proto; path = ../src/google/protobuf/struct.proto; sourceTree = ""; }; F4E675DE1B21D1DE0054530B /* type.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = type.proto; path = ../src/google/protobuf/type.proto; sourceTree = ""; }; F4E675DF1B21D1DE0054530B /* wrappers.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = wrappers.proto; path = ../src/google/protobuf/wrappers.proto; sourceTree = ""; }; + F4F53F8B219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = text_format_extensions_unittest_data.txt; sourceTree = ""; }; F4F8D8841D78A186002CE128 /* GPBUnittestProtos2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnittestProtos2.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -444,7 +446,6 @@ 7461B6940F94FDDD00A0C422 /* Tests */ = { isa = PBXGroup; children = ( - 8B4248B71A8BDD9600BC1EC6 /* protobuf */, 8B210CCD159383D60032D72D /* golden_message */, 8B210CCF159386920032D72D /* golden_packed_fields_message */, 8B8B615C17DF7056002EE618 /* GPBARCUnittestProtos.m */, @@ -502,6 +503,7 @@ 7461B6BA0F94FDF900A0C422 /* GPBUtilitiesTests.m */, 8B4248E51A929C9900BC1EC6 /* GPBWellKnownTypesTest.m */, 7461B6BC0F94FDF900A0C422 /* GPBWireFormatTests.m */, + F4F53F8B219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt */, F45E57C81AE6DC98000B7D99 /* text_format_map_unittest_data.txt */, F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */, 8B7E6A7414893DBA00F8884A /* unittest_custom_options.proto */, @@ -513,6 +515,7 @@ 8BBD9DB016DD1DC8008E1EC1 /* unittest_lite.proto */, 8B7E6A7B14893DBC00F8884A /* unittest_mset.proto */, 8B7E6A7C14893DBC00F8884A /* unittest_no_generic_services.proto */, + 8B35468621A61EB2000BD30D /* unittest_objc_options.proto */, F4CF31711B162EF500BD9B06 /* unittest_objc_startup.proto */, 8B09AAF614B663A7007B4184 /* unittest_objc.proto */, 8B7E6A7D14893DBC00F8884A /* unittest_optimize_for.proto */, @@ -647,7 +650,7 @@ LastUpgradeCheck = 0930; TargetAttributes = { 8BBEA4A5147C727100C4ADB7 = { - LastSwiftMigration = 0800; + LastSwiftMigration = 0940; TestTargetID = 8B9A5EA41831993600A9D33B; }; F45BBC0E1B0CDB50002D064D = { @@ -680,6 +683,7 @@ buildActionMask = 2147483647; files = ( 8B210CCE159383D60032D72D /* golden_message in Resources */, + F4F53F8C219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt in Resources */, F43C88D0191D77FC009E917D /* text_format_unittest_data.txt in Resources */, 8B210CD0159386920032D72D /* golden_packed_fields_message in Resources */, F45E57C91AE6DC98000B7D99 /* text_format_map_unittest_data.txt in Resources */, @@ -826,9 +830,9 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; - HEADER_SEARCH_PATHS = "$(SRCROOT)"; PRODUCT_NAME = ProtocolBuffers; TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; }; name = Debug; }; @@ -836,9 +840,9 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; - HEADER_SEARCH_PATHS = "$(SRCROOT)"; PRODUCT_NAME = ProtocolBuffers; TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; }; name = Release; }; @@ -851,10 +855,6 @@ "\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"", "$(inherited)", ); - HEADER_SEARCH_PATHS = ( - "${PROJECT_DERIVED_FILE_DIR}/protos", - "$(SRCROOT)", - ); INFOPLIST_FILE = "Tests/UnitTests-Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; @@ -866,8 +866,9 @@ PRODUCT_NAME = UnitTests; SWIFT_OBJC_BRIDGING_HEADER = "Tests/UnitTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "${PROJECT_DERIVED_FILE_DIR}/protos $(SRCROOT)"; WARNING_CFLAGS = ( "$(inherited)", "-Wno-documentation-unknown-command", @@ -886,10 +887,6 @@ "\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"", "$(inherited)", ); - HEADER_SEARCH_PATHS = ( - "${PROJECT_DERIVED_FILE_DIR}/protos", - "$(SRCROOT)", - ); INFOPLIST_FILE = "Tests/UnitTests-Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; @@ -900,8 +897,9 @@ OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = UnitTests; SWIFT_OBJC_BRIDGING_HEADER = "Tests/UnitTests-Bridging-Header.h"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "${PROJECT_DERIVED_FILE_DIR}/protos $(SRCROOT)"; WARNING_CFLAGS = ( "$(inherited)", "-Wno-documentation-unknown-command", @@ -914,7 +912,7 @@ C01FCF4F08A954540054247B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ALWAYS_SEARCH_USER_PATHS = YES; + ALWAYS_SEARCH_USER_PATHS = NO; CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES; CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES; @@ -985,7 +983,7 @@ C01FCF5008A954540054247B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ALWAYS_SEARCH_USER_PATHS = YES; + ALWAYS_SEARCH_USER_PATHS = NO; CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES; CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES; @@ -1055,9 +1053,9 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; - HEADER_SEARCH_PATHS = "$(SRCROOT)"; PRODUCT_NAME = TestSingleSourceBuild; TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; }; name = Debug; }; @@ -1065,9 +1063,9 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_OBJC_WEAK = YES; - HEADER_SEARCH_PATHS = "$(SRCROOT)"; PRODUCT_NAME = TestSingleSourceBuild; TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; }; name = Release; }; diff --git a/objectivec/ProtocolBuffers_iOS.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme b/objectivec/ProtocolBuffers_iOS.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme index 77483d381c..1a02f7e4cb 100644 --- a/objectivec/ProtocolBuffers_iOS.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme +++ b/objectivec/ProtocolBuffers_iOS.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme @@ -315,16 +315,6 @@ debugDocumentVersioning = "YES" debugServiceExtension = "internal" allowLocationSimulation = "YES"> - - - - diff --git a/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.pbxproj b/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.pbxproj new file mode 100644 index 0000000000..c40c2aaa15 --- /dev/null +++ b/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.pbxproj @@ -0,0 +1,1129 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 47; + objects = { + +/* Begin PBXBuildFile section */ + 5102DABC1891A073002037B6 /* GPBConcurrencyTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5102DABB1891A052002037B6 /* GPBConcurrencyTests.m */; }; + 7461B5360F94FB4600A0C422 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; }; + 7461B53C0F94FB4E00A0C422 /* GPBCodedInputStream.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B48F0F94F99000A0C422 /* GPBCodedInputStream.m */; }; + 7461B53D0F94FB4E00A0C422 /* GPBCodedOutputStream.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B4910F94F99000A0C422 /* GPBCodedOutputStream.m */; }; + 7461B5490F94FB4E00A0C422 /* GPBExtensionRegistry.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B4A90F94F99000A0C422 /* GPBExtensionRegistry.m */; }; + 7461B54C0F94FB4E00A0C422 /* GPBUnknownField.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B4AF0F94F99000A0C422 /* GPBUnknownField.m */; }; + 7461B5530F94FB4E00A0C422 /* GPBMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B4BF0F94F99000A0C422 /* GPBMessage.m */; }; + 7461B5610F94FB4E00A0C422 /* GPBUnknownFieldSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B4E20F94F99000A0C422 /* GPBUnknownFieldSet.m */; }; + 7461B5630F94FB4E00A0C422 /* GPBUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B4E60F94F99000A0C422 /* GPBUtilities.m */; }; + 7461B5640F94FB4E00A0C422 /* GPBWireFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B4E80F94F99000A0C422 /* GPBWireFormat.m */; }; + 8B210CCE159383D60032D72D /* golden_message in Resources */ = {isa = PBXBuildFile; fileRef = 8B210CCD159383D60032D72D /* golden_message */; }; + 8B210CD0159386920032D72D /* golden_packed_fields_message in Resources */ = {isa = PBXBuildFile; fileRef = 8B210CCF159386920032D72D /* golden_packed_fields_message */; }; + 8B4248B41A8BD96E00BC1EC6 /* GPBSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B4248B31A8BD96E00BC1EC6 /* GPBSwiftTests.swift */; }; + 8B4248E41A929C8900BC1EC6 /* GPBWellKnownTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B4248E21A929C8900BC1EC6 /* GPBWellKnownTypes.m */; }; + 8B4248E61A929C9900BC1EC6 /* GPBWellKnownTypesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B4248E51A929C9900BC1EC6 /* GPBWellKnownTypesTest.m */; }; + 8B79657B14992E3F002FFBFC /* GPBRootObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B79657914992E3E002FFBFC /* GPBRootObject.m */; }; + 8B8B615D17DF7056002EE618 /* GPBARCUnittestProtos.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B8B615C17DF7056002EE618 /* GPBARCUnittestProtos.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; + 8B96157414C8C38C00A2AC0B /* GPBDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B96157314C8C38C00A2AC0B /* GPBDescriptor.m */; }; + 8B9A5EEC18330A0F00A9D33B /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8B9A5E9F1831913D00A9D33B /* UIKit.framework */; }; + 8BBEA4A9147C727D00C4ADB7 /* GPBCodedInputStreamTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B69B0F94FDF800A0C422 /* GPBCodedInputStreamTests.m */; }; + 8BBEA4AA147C727D00C4ADB7 /* GPBCodedOuputStreamTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B69D0F94FDF800A0C422 /* GPBCodedOuputStreamTests.m */; }; + 8BBEA4AC147C727D00C4ADB7 /* GPBMessageTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B6A30F94FDF800A0C422 /* GPBMessageTests.m */; }; + 8BBEA4B0147C727D00C4ADB7 /* GPBTestUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B6AC0F94FDF800A0C422 /* GPBTestUtilities.m */; }; + 8BBEA4B6147C727D00C4ADB7 /* GPBUnknownFieldSetTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B6B80F94FDF900A0C422 /* GPBUnknownFieldSetTest.m */; }; + 8BBEA4B7147C727D00C4ADB7 /* GPBUtilitiesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B6BA0F94FDF900A0C422 /* GPBUtilitiesTests.m */; }; + 8BBEA4B8147C727D00C4ADB7 /* GPBWireFormatTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7461B6BC0F94FDF900A0C422 /* GPBWireFormatTests.m */; }; + 8BBEA4BB147C729200C4ADB7 /* libProtocolBuffers.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7461B52E0F94FAF800A0C422 /* libProtocolBuffers.a */; }; + 8BD3981F14BE59D70081D629 /* GPBUnittestProtos.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BD3981E14BE59D70081D629 /* GPBUnittestProtos.m */; }; + 8BF8193514A0DDA600A2C982 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; }; + F401DC351A8E5C6F00FCC765 /* GPBArrayTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F401DC341A8E5C6F00FCC765 /* GPBArrayTests.m */; }; + F40EE4F0206BF91E0071091A /* GPBCompileTest01.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4CD206BF9170071091A /* GPBCompileTest01.m */; }; + F40EE4F1206BF91E0071091A /* GPBCompileTest02.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4C6206BF9170071091A /* GPBCompileTest02.m */; }; + F40EE4F2206BF91E0071091A /* GPBCompileTest03.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D6206BF9190071091A /* GPBCompileTest03.m */; }; + F40EE4F3206BF91E0071091A /* GPBCompileTest04.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4C3206BF9160071091A /* GPBCompileTest04.m */; }; + F40EE4F4206BF91E0071091A /* GPBCompileTest05.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4C4206BF9160071091A /* GPBCompileTest05.m */; }; + F40EE4F5206BF91E0071091A /* GPBCompileTest06.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D7206BF9190071091A /* GPBCompileTest06.m */; }; + F40EE4F6206BF91E0071091A /* GPBCompileTest07.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4C5206BF9170071091A /* GPBCompileTest07.m */; }; + F40EE4F7206BF91E0071091A /* GPBCompileTest08.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4C2206BF9160071091A /* GPBCompileTest08.m */; }; + F40EE4F8206BF91E0071091A /* GPBCompileTest09.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4C9206BF9170071091A /* GPBCompileTest09.m */; }; + F40EE4F9206BF91E0071091A /* GPBCompileTest10.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D1206BF9180071091A /* GPBCompileTest10.m */; }; + F40EE4FA206BF91E0071091A /* GPBCompileTest11.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D8206BF9190071091A /* GPBCompileTest11.m */; }; + F40EE4FB206BF91E0071091A /* GPBCompileTest12.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D5206BF9180071091A /* GPBCompileTest12.m */; }; + F40EE4FC206BF91E0071091A /* GPBCompileTest13.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4C8206BF9170071091A /* GPBCompileTest13.m */; }; + F40EE4FD206BF91E0071091A /* GPBCompileTest14.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D4206BF9180071091A /* GPBCompileTest14.m */; }; + F40EE4FE206BF91E0071091A /* GPBCompileTest15.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4CE206BF9170071091A /* GPBCompileTest15.m */; }; + F40EE4FF206BF91E0071091A /* GPBCompileTest16.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D0206BF9180071091A /* GPBCompileTest16.m */; }; + F40EE500206BF91E0071091A /* GPBCompileTest17.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D2206BF9180071091A /* GPBCompileTest17.m */; }; + F40EE501206BF91E0071091A /* GPBCompileTest18.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4D3206BF9180071091A /* GPBCompileTest18.m */; }; + F40EE502206BF91E0071091A /* GPBCompileTest19.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4C7206BF9170071091A /* GPBCompileTest19.m */; }; + F40EE503206BF91E0071091A /* GPBCompileTest20.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4CA206BF9170071091A /* GPBCompileTest20.m */; }; + F40EE504206BF91E0071091A /* GPBCompileTest21.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4CB206BF9170071091A /* GPBCompileTest21.m */; }; + F40EE505206BF91E0071091A /* GPBCompileTest22.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4CF206BF9170071091A /* GPBCompileTest22.m */; }; + F40EE506206BF91E0071091A /* GPBCompileTest23.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE4CC206BF9170071091A /* GPBCompileTest23.m */; }; + F40EE511206C068D0071091A /* GPBCompileTest24.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE50E206C06880071091A /* GPBCompileTest24.m */; }; + F40EE512206C068D0071091A /* GPBCompileTest25.m in Sources */ = {isa = PBXBuildFile; fileRef = F40EE50D206C06880071091A /* GPBCompileTest25.m */; }; + F41C175D1833D3310064ED4D /* GPBPerfTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F41C175C1833D3310064ED4D /* GPBPerfTests.m */; }; + F4353D1F1AB88243005A6198 /* GPBDescriptorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F4353D1E1AB88243005A6198 /* GPBDescriptorTests.m */; }; + F4353D271ABB156F005A6198 /* GPBDictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = F4353D251ABB156F005A6198 /* GPBDictionary.m */; }; + F4353D421AC06F31005A6198 /* GPBDictionaryTests+Bool.m in Sources */ = {isa = PBXBuildFile; fileRef = F4353D3B1AC06F31005A6198 /* GPBDictionaryTests+Bool.m */; }; + F4353D431AC06F31005A6198 /* GPBDictionaryTests+Int32.m in Sources */ = {isa = PBXBuildFile; fileRef = F4353D3C1AC06F31005A6198 /* GPBDictionaryTests+Int32.m */; }; + F4353D441AC06F31005A6198 /* GPBDictionaryTests+Int64.m in Sources */ = {isa = PBXBuildFile; fileRef = F4353D3D1AC06F31005A6198 /* GPBDictionaryTests+Int64.m */; }; + F4353D451AC06F31005A6198 /* GPBDictionaryTests+String.m in Sources */ = {isa = PBXBuildFile; fileRef = F4353D3E1AC06F31005A6198 /* GPBDictionaryTests+String.m */; }; + F4353D461AC06F31005A6198 /* GPBDictionaryTests+UInt32.m in Sources */ = {isa = PBXBuildFile; fileRef = F4353D3F1AC06F31005A6198 /* GPBDictionaryTests+UInt32.m */; }; + F4353D471AC06F31005A6198 /* GPBDictionaryTests+UInt64.m in Sources */ = {isa = PBXBuildFile; fileRef = F4353D401AC06F31005A6198 /* GPBDictionaryTests+UInt64.m */; }; + F43C88D0191D77FC009E917D /* text_format_unittest_data.txt in Resources */ = {isa = PBXBuildFile; fileRef = F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */; }; + F4487C6A1A9F8F8100531423 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; }; + F4487C6F1A9F8FFF00531423 /* GPBProtocolBuffers.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BCF338814ED799900BC5317 /* GPBProtocolBuffers.m */; }; + F4487C731A9F906200531423 /* GPBArray.m in Sources */ = {isa = PBXBuildFile; fileRef = F4487C711A9F906200531423 /* GPBArray.m */; }; + F4487C771AADF84900531423 /* GPBMessageTests+Runtime.m in Sources */ = {isa = PBXBuildFile; fileRef = F4487C761AADF84900531423 /* GPBMessageTests+Runtime.m */; }; + F4487C811AAF62FC00531423 /* GPBMessageTests+Serialization.m in Sources */ = {isa = PBXBuildFile; fileRef = F4487C801AAF62FC00531423 /* GPBMessageTests+Serialization.m */; }; + F4487C851AAF6AC500531423 /* GPBMessageTests+Merge.m in Sources */ = {isa = PBXBuildFile; fileRef = F4487C841AAF6AC500531423 /* GPBMessageTests+Merge.m */; }; + F4584D831ECCB53600803AB6 /* GPBExtensionRegistryTest.m in Sources */ = {isa = PBXBuildFile; fileRef = F4584D801ECCB39E00803AB6 /* GPBExtensionRegistryTest.m */; }; + F45C69CC16DFD08D0081955B /* GPBExtensionInternals.m in Sources */ = {isa = PBXBuildFile; fileRef = F45C69CB16DFD08D0081955B /* GPBExtensionInternals.m */; }; + F45E57C91AE6DC98000B7D99 /* text_format_map_unittest_data.txt in Resources */ = {isa = PBXBuildFile; fileRef = F45E57C81AE6DC98000B7D99 /* text_format_map_unittest_data.txt */; }; + F47476E91D21A537007C7B1A /* Duration.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B4248DE1A929C7D00BC1EC6 /* Duration.pbobjc.m */; }; + F47476EA1D21A537007C7B1A /* Timestamp.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B4248E01A929C7D00BC1EC6 /* Timestamp.pbobjc.m */; }; + F4B51B1C1BBC5C7100744318 /* GPBObjectiveCPlusPlusTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = F4B51B1B1BBC5C7100744318 /* GPBObjectiveCPlusPlusTest.mm */; }; + F4C4B9E71E1D97BF00D3B61D /* GPBDictionaryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C4B9E51E1D97BB00D3B61D /* GPBDictionaryTests.m */; }; + F4E675D01B21D1620054530B /* Any.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675B71B21D1440054530B /* Any.pbobjc.m */; }; + F4E675D11B21D1620054530B /* Api.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675B91B21D1440054530B /* Api.pbobjc.m */; }; + F4E675D21B21D1620054530B /* Empty.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675BC1B21D1440054530B /* Empty.pbobjc.m */; }; + F4E675D31B21D1620054530B /* FieldMask.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675BE1B21D1440054530B /* FieldMask.pbobjc.m */; }; + F4E675D41B21D1620054530B /* SourceContext.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675C01B21D1440054530B /* SourceContext.pbobjc.m */; }; + F4E675D51B21D1620054530B /* Struct.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675C21B21D1440054530B /* Struct.pbobjc.m */; }; + F4E675D61B21D1620054530B /* Type.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675C51B21D1440054530B /* Type.pbobjc.m */; }; + F4E675D71B21D1620054530B /* Wrappers.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E675C71B21D1440054530B /* Wrappers.pbobjc.m */; }; + F4F53F8C219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt in Resources */ = {isa = PBXBuildFile; fileRef = F4F53F8B219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt */; }; + F4F8D8861D78A193002CE128 /* GPBUnittestProtos2.m in Sources */ = {isa = PBXBuildFile; fileRef = F4F8D8841D78A186002CE128 /* GPBUnittestProtos2.m */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + 8BBEA4BC147C729A00C4ADB7 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 29B97313FDCFA39411CA2CEA /* Project object */; + proxyType = 1; + remoteGlobalIDString = 7461B52D0F94FAF800A0C422; + remoteInfo = ProtocolBuffers; + }; + F45BBC121B0CDBBA002D064D /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 29B97313FDCFA39411CA2CEA /* Project object */; + proxyType = 1; + remoteGlobalIDString = F45BBC0E1B0CDB50002D064D; + remoteInfo = "Compile Unittest Protos"; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + 1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + 5102DABB1891A052002037B6 /* GPBConcurrencyTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GPBConcurrencyTests.m; sourceTree = ""; }; + 51457B5F18D0B7AF00CCC606 /* GPBCodedInputStream_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBCodedInputStream_PackagePrivate.h; sourceTree = ""; }; + 515B840C18B7DEE30031753B /* GPBDescriptor_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBDescriptor_PackagePrivate.h; sourceTree = ""; }; + 5196A06918CE16B000B759E2 /* GPBMessage_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBMessage_PackagePrivate.h; sourceTree = ""; }; + 7401C1A90F950347006D8281 /* UnitTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "UnitTests-Info.plist"; sourceTree = ""; }; + 7461B48D0F94F99000A0C422 /* GPBBootstrap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBBootstrap.h; sourceTree = ""; }; + 7461B48E0F94F99000A0C422 /* GPBCodedInputStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBCodedInputStream.h; sourceTree = ""; }; + 7461B48F0F94F99000A0C422 /* GPBCodedInputStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = GPBCodedInputStream.m; sourceTree = ""; }; + 7461B4900F94F99000A0C422 /* GPBCodedOutputStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBCodedOutputStream.h; sourceTree = ""; }; + 7461B4910F94F99000A0C422 /* GPBCodedOutputStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCodedOutputStream.m; sourceTree = ""; }; + 7461B4A80F94F99000A0C422 /* GPBExtensionRegistry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBExtensionRegistry.h; sourceTree = ""; }; + 7461B4A90F94F99000A0C422 /* GPBExtensionRegistry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBExtensionRegistry.m; sourceTree = ""; }; + 7461B4AE0F94F99000A0C422 /* GPBUnknownField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBUnknownField.h; sourceTree = ""; }; + 7461B4AF0F94F99000A0C422 /* GPBUnknownField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnknownField.m; sourceTree = ""; }; + 7461B4BE0F94F99000A0C422 /* GPBMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBMessage.h; sourceTree = ""; }; + 7461B4BF0F94F99000A0C422 /* GPBMessage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBMessage.m; sourceTree = ""; }; + 7461B4CD0F94F99000A0C422 /* GPBProtocolBuffers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBProtocolBuffers.h; sourceTree = ""; }; + 7461B4E10F94F99000A0C422 /* GPBUnknownFieldSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBUnknownFieldSet.h; sourceTree = ""; }; + 7461B4E20F94F99000A0C422 /* GPBUnknownFieldSet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnknownFieldSet.m; sourceTree = ""; }; + 7461B4E50F94F99000A0C422 /* GPBUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBUtilities.h; sourceTree = ""; }; + 7461B4E60F94F99000A0C422 /* GPBUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUtilities.m; sourceTree = ""; }; + 7461B4E70F94F99000A0C422 /* GPBWireFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBWireFormat.h; sourceTree = ""; }; + 7461B4E80F94F99000A0C422 /* GPBWireFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBWireFormat.m; sourceTree = ""; }; + 7461B52E0F94FAF800A0C422 /* libProtocolBuffers.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libProtocolBuffers.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 7461B69B0F94FDF800A0C422 /* GPBCodedInputStreamTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCodedInputStreamTests.m; sourceTree = ""; }; + 7461B69D0F94FDF800A0C422 /* GPBCodedOuputStreamTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCodedOuputStreamTests.m; sourceTree = ""; }; + 7461B6A30F94FDF800A0C422 /* GPBMessageTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBMessageTests.m; sourceTree = ""; }; + 7461B6AB0F94FDF800A0C422 /* GPBTestUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBTestUtilities.h; sourceTree = ""; }; + 7461B6AC0F94FDF800A0C422 /* GPBTestUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBTestUtilities.m; sourceTree = ""; }; + 7461B6B80F94FDF900A0C422 /* GPBUnknownFieldSetTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnknownFieldSetTest.m; sourceTree = ""; }; + 7461B6BA0F94FDF900A0C422 /* GPBUtilitiesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUtilitiesTests.m; sourceTree = ""; }; + 7461B6BC0F94FDF900A0C422 /* GPBWireFormatTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBWireFormatTests.m; sourceTree = ""; }; + 8B09AAF614B663A7007B4184 /* unittest_objc.proto */ = {isa = PBXFileReference; lastKnownFileType = text; path = unittest_objc.proto; sourceTree = ""; }; + 8B210CCD159383D60032D72D /* golden_message */ = {isa = PBXFileReference; lastKnownFileType = file; path = golden_message; sourceTree = ""; }; + 8B210CCF159386920032D72D /* golden_packed_fields_message */ = {isa = PBXFileReference; lastKnownFileType = file; path = golden_packed_fields_message; sourceTree = ""; }; + 8B35468621A61EB2000BD30D /* unittest_objc_options.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = unittest_objc_options.proto; sourceTree = ""; }; + 8B4248B21A8BD96D00BC1EC6 /* UnitTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UnitTests-Bridging-Header.h"; sourceTree = ""; }; + 8B4248B31A8BD96E00BC1EC6 /* GPBSwiftTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GPBSwiftTests.swift; sourceTree = ""; }; + 8B4248DD1A929C7D00BC1EC6 /* Duration.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Duration.pbobjc.h; path = google/protobuf/Duration.pbobjc.h; sourceTree = ""; }; + 8B4248DE1A929C7D00BC1EC6 /* Duration.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Duration.pbobjc.m; path = google/protobuf/Duration.pbobjc.m; sourceTree = ""; }; + 8B4248E01A929C7D00BC1EC6 /* Timestamp.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Timestamp.pbobjc.m; path = google/protobuf/Timestamp.pbobjc.m; sourceTree = ""; }; + 8B4248E11A929C8900BC1EC6 /* GPBWellKnownTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBWellKnownTypes.h; sourceTree = ""; }; + 8B4248E21A929C8900BC1EC6 /* GPBWellKnownTypes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBWellKnownTypes.m; sourceTree = ""; }; + 8B4248E51A929C9900BC1EC6 /* GPBWellKnownTypesTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBWellKnownTypesTest.m; sourceTree = ""; }; + 8B4249481A92A02300BC1EC6 /* timestamp.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = timestamp.proto; path = ../src/google/protobuf/timestamp.proto; sourceTree = ""; }; + 8B42494A1A92A0BA00BC1EC6 /* duration.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = duration.proto; path = ../src/google/protobuf/duration.proto; sourceTree = ""; }; + 8B79657814992E3E002FFBFC /* GPBRootObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBRootObject.h; sourceTree = ""; }; + 8B79657914992E3E002FFBFC /* GPBRootObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBRootObject.m; sourceTree = ""; }; + 8B7E6A7414893DBA00F8884A /* unittest_custom_options.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_custom_options.proto; path = ../../src/google/protobuf/unittest_custom_options.proto; sourceTree = ""; }; + 8B7E6A7514893DBA00F8884A /* unittest_embed_optimize_for.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_embed_optimize_for.proto; path = ../../src/google/protobuf/unittest_embed_optimize_for.proto; sourceTree = ""; }; + 8B7E6A7614893DBA00F8884A /* unittest_empty.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_empty.proto; path = ../../src/google/protobuf/unittest_empty.proto; sourceTree = ""; }; + 8B7E6A7814893DBB00F8884A /* unittest_import.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_import.proto; path = ../../src/google/protobuf/unittest_import.proto; sourceTree = ""; }; + 8B7E6A7B14893DBC00F8884A /* unittest_mset.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_mset.proto; path = ../../src/google/protobuf/unittest_mset.proto; sourceTree = ""; }; + 8B7E6A7C14893DBC00F8884A /* unittest_no_generic_services.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_no_generic_services.proto; path = ../../src/google/protobuf/unittest_no_generic_services.proto; sourceTree = ""; }; + 8B7E6A7D14893DBC00F8884A /* unittest_optimize_for.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_optimize_for.proto; path = ../../src/google/protobuf/unittest_optimize_for.proto; sourceTree = ""; }; + 8B7E6A7E14893DBC00F8884A /* unittest.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest.proto; path = ../../src/google/protobuf/unittest.proto; sourceTree = ""; }; + 8B8B615C17DF7056002EE618 /* GPBARCUnittestProtos.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBARCUnittestProtos.m; sourceTree = ""; }; + 8B96157214C8B06000A2AC0B /* GPBDescriptor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBDescriptor.h; sourceTree = ""; }; + 8B96157314C8C38C00A2AC0B /* GPBDescriptor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBDescriptor.m; sourceTree = ""; }; + 8B9742421A8AAA7800DCE92C /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; + 8B9A5E9F1831913D00A9D33B /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; + 8BBD9DB016DD1DC8008E1EC1 /* unittest_lite.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_lite.proto; path = ../../src/google/protobuf/unittest_lite.proto; sourceTree = ""; }; + 8BBEA4A6147C727100C4ADB7 /* UnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = UnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 8BCF338814ED799900BC5317 /* GPBProtocolBuffers.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GPBProtocolBuffers.m; sourceTree = ""; }; + 8BD3981D14BE54220081D629 /* unittest_enormous_descriptor.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = unittest_enormous_descriptor.proto; path = ../../src/google/protobuf/unittest_enormous_descriptor.proto; sourceTree = ""; }; + 8BD3981E14BE59D70081D629 /* GPBUnittestProtos.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnittestProtos.m; sourceTree = ""; }; + 8BEB5AE01498033E0078BF9D /* GPBRuntimeTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBRuntimeTypes.h; sourceTree = ""; }; + F401DC341A8E5C6F00FCC765 /* GPBArrayTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBArrayTests.m; sourceTree = ""; }; + F40EE4C2206BF9160071091A /* GPBCompileTest08.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest08.m; sourceTree = ""; }; + F40EE4C3206BF9160071091A /* GPBCompileTest04.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest04.m; sourceTree = ""; }; + F40EE4C4206BF9160071091A /* GPBCompileTest05.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest05.m; sourceTree = ""; }; + F40EE4C5206BF9170071091A /* GPBCompileTest07.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest07.m; sourceTree = ""; }; + F40EE4C6206BF9170071091A /* GPBCompileTest02.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest02.m; sourceTree = ""; }; + F40EE4C7206BF9170071091A /* GPBCompileTest19.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest19.m; sourceTree = ""; }; + F40EE4C8206BF9170071091A /* GPBCompileTest13.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest13.m; sourceTree = ""; }; + F40EE4C9206BF9170071091A /* GPBCompileTest09.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest09.m; sourceTree = ""; }; + F40EE4CA206BF9170071091A /* GPBCompileTest20.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest20.m; sourceTree = ""; }; + F40EE4CB206BF9170071091A /* GPBCompileTest21.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest21.m; sourceTree = ""; }; + F40EE4CC206BF9170071091A /* GPBCompileTest23.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest23.m; sourceTree = ""; }; + F40EE4CD206BF9170071091A /* GPBCompileTest01.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest01.m; sourceTree = ""; }; + F40EE4CE206BF9170071091A /* GPBCompileTest15.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest15.m; sourceTree = ""; }; + F40EE4CF206BF9170071091A /* GPBCompileTest22.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest22.m; sourceTree = ""; }; + F40EE4D0206BF9180071091A /* GPBCompileTest16.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest16.m; sourceTree = ""; }; + F40EE4D1206BF9180071091A /* GPBCompileTest10.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest10.m; sourceTree = ""; }; + F40EE4D2206BF9180071091A /* GPBCompileTest17.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest17.m; sourceTree = ""; }; + F40EE4D3206BF9180071091A /* GPBCompileTest18.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest18.m; sourceTree = ""; }; + F40EE4D4206BF9180071091A /* GPBCompileTest14.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest14.m; sourceTree = ""; }; + F40EE4D5206BF9180071091A /* GPBCompileTest12.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest12.m; sourceTree = ""; }; + F40EE4D6206BF9190071091A /* GPBCompileTest03.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest03.m; sourceTree = ""; }; + F40EE4D7206BF9190071091A /* GPBCompileTest06.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest06.m; sourceTree = ""; }; + F40EE4D8206BF9190071091A /* GPBCompileTest11.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest11.m; sourceTree = ""; }; + F40EE50D206C06880071091A /* GPBCompileTest25.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest25.m; sourceTree = ""; }; + F40EE50E206C06880071091A /* GPBCompileTest24.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest24.m; sourceTree = ""; }; + F41C175C1833D3310064ED4D /* GPBPerfTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBPerfTests.m; sourceTree = ""; }; + F4353D1E1AB88243005A6198 /* GPBDescriptorTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBDescriptorTests.m; sourceTree = ""; }; + F4353D241ABB156F005A6198 /* GPBDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBDictionary.h; sourceTree = ""; }; + F4353D251ABB156F005A6198 /* GPBDictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBDictionary.m; sourceTree = ""; }; + F4353D3A1AC06F31005A6198 /* GPBDictionaryTests.pddm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GPBDictionaryTests.pddm; sourceTree = ""; }; + F4353D3B1AC06F31005A6198 /* GPBDictionaryTests+Bool.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBDictionaryTests+Bool.m"; sourceTree = ""; }; + F4353D3C1AC06F31005A6198 /* GPBDictionaryTests+Int32.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBDictionaryTests+Int32.m"; sourceTree = ""; }; + F4353D3D1AC06F31005A6198 /* GPBDictionaryTests+Int64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBDictionaryTests+Int64.m"; sourceTree = ""; }; + F4353D3E1AC06F31005A6198 /* GPBDictionaryTests+String.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBDictionaryTests+String.m"; sourceTree = ""; }; + F4353D3F1AC06F31005A6198 /* GPBDictionaryTests+UInt32.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBDictionaryTests+UInt32.m"; sourceTree = ""; }; + F4353D401AC06F31005A6198 /* GPBDictionaryTests+UInt64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBDictionaryTests+UInt64.m"; sourceTree = ""; }; + F43725921AC9835D004DCAFB /* GPBDictionary_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBDictionary_PackagePrivate.h; sourceTree = ""; }; + F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = text_format_unittest_data.txt; sourceTree = ""; }; + F4411BE81AF1301700324B4A /* GPBArray_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBArray_PackagePrivate.h; sourceTree = ""; }; + F4487C6E1A9F8F8100531423 /* libTestSingleSourceBuild.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libTestSingleSourceBuild.a; sourceTree = BUILT_PRODUCTS_DIR; }; + F4487C701A9F906200531423 /* GPBArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBArray.h; sourceTree = ""; }; + F4487C711A9F906200531423 /* GPBArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBArray.m; sourceTree = ""; }; + F4487C761AADF84900531423 /* GPBMessageTests+Runtime.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBMessageTests+Runtime.m"; sourceTree = ""; }; + F4487C7A1AADFB5500531423 /* unittest_runtime_proto2.proto */ = {isa = PBXFileReference; lastKnownFileType = text; path = unittest_runtime_proto2.proto; sourceTree = ""; }; + F4487C7B1AADFB5500531423 /* unittest_runtime_proto3.proto */ = {isa = PBXFileReference; lastKnownFileType = text; path = unittest_runtime_proto3.proto; sourceTree = ""; }; + F4487C7D1AAE06C500531423 /* GPBUtilities_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBUtilities_PackagePrivate.h; sourceTree = ""; }; + F4487C801AAF62FC00531423 /* GPBMessageTests+Serialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBMessageTests+Serialization.m"; sourceTree = ""; }; + F4487C841AAF6AC500531423 /* GPBMessageTests+Merge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GPBMessageTests+Merge.m"; sourceTree = ""; }; + F44929021C866B3B00C2548A /* GPBCodedOutputStream_PackagePrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBCodedOutputStream_PackagePrivate.h; sourceTree = ""; }; + F451D3F61A8AAEA600B8A22C /* GPBProtocolBuffers_RuntimeSupport.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBProtocolBuffers_RuntimeSupport.h; sourceTree = ""; }; + F4584D801ECCB39E00803AB6 /* GPBExtensionRegistryTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GPBExtensionRegistryTest.m; path = Tests/GPBExtensionRegistryTest.m; sourceTree = SOURCE_ROOT; }; + F45C69CB16DFD08D0081955B /* GPBExtensionInternals.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBExtensionInternals.m; sourceTree = ""; }; + F45E57C81AE6DC98000B7D99 /* text_format_map_unittest_data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = text_format_map_unittest_data.txt; sourceTree = ""; }; + F4AC9E1C1A8BEB1000BD6E83 /* unittest_cycle.proto */ = {isa = PBXFileReference; lastKnownFileType = text; path = unittest_cycle.proto; sourceTree = ""; }; + F4B51B1B1BBC5C7100744318 /* GPBObjectiveCPlusPlusTest.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = GPBObjectiveCPlusPlusTest.mm; sourceTree = ""; }; + F4B6B8B01A9CC99500892426 /* GPBUnknownField_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBUnknownField_PackagePrivate.h; sourceTree = ""; }; + F4B6B8B11A9CCBBB00892426 /* GPBUnknownFieldSet_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBUnknownFieldSet_PackagePrivate.h; sourceTree = ""; }; + F4B6B8B31A9CD1C600892426 /* GPBExtensionInternals.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBExtensionInternals.h; sourceTree = ""; }; + F4B6B8B51A9CD1C600892426 /* GPBRootObject_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBRootObject_PackagePrivate.h; sourceTree = ""; }; + F4C4B9E51E1D97BB00D3B61D /* GPBDictionaryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBDictionaryTests.m; sourceTree = ""; }; + F4CF31711B162EF500BD9B06 /* unittest_objc_startup.proto */ = {isa = PBXFileReference; lastKnownFileType = text; path = unittest_objc_startup.proto; sourceTree = ""; }; + F4E675B61B21D1440054530B /* Any.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Any.pbobjc.h; path = google/protobuf/Any.pbobjc.h; sourceTree = ""; }; + F4E675B71B21D1440054530B /* Any.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Any.pbobjc.m; path = google/protobuf/Any.pbobjc.m; sourceTree = ""; }; + F4E675B81B21D1440054530B /* Api.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Api.pbobjc.h; path = google/protobuf/Api.pbobjc.h; sourceTree = ""; }; + F4E675B91B21D1440054530B /* Api.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Api.pbobjc.m; path = google/protobuf/Api.pbobjc.m; sourceTree = ""; }; + F4E675BB1B21D1440054530B /* Empty.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Empty.pbobjc.h; path = google/protobuf/Empty.pbobjc.h; sourceTree = ""; }; + F4E675BC1B21D1440054530B /* Empty.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Empty.pbobjc.m; path = google/protobuf/Empty.pbobjc.m; sourceTree = ""; }; + F4E675BD1B21D1440054530B /* FieldMask.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = FieldMask.pbobjc.h; path = google/protobuf/FieldMask.pbobjc.h; sourceTree = ""; }; + F4E675BE1B21D1440054530B /* FieldMask.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = FieldMask.pbobjc.m; path = google/protobuf/FieldMask.pbobjc.m; sourceTree = ""; }; + F4E675BF1B21D1440054530B /* SourceContext.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SourceContext.pbobjc.h; path = google/protobuf/SourceContext.pbobjc.h; sourceTree = ""; }; + F4E675C01B21D1440054530B /* SourceContext.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SourceContext.pbobjc.m; path = google/protobuf/SourceContext.pbobjc.m; sourceTree = ""; }; + F4E675C11B21D1440054530B /* Struct.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Struct.pbobjc.h; path = google/protobuf/Struct.pbobjc.h; sourceTree = ""; }; + F4E675C21B21D1440054530B /* Struct.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Struct.pbobjc.m; path = google/protobuf/Struct.pbobjc.m; sourceTree = ""; }; + F4E675C31B21D1440054530B /* Timestamp.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Timestamp.pbobjc.h; path = google/protobuf/Timestamp.pbobjc.h; sourceTree = ""; }; + F4E675C41B21D1440054530B /* Type.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Type.pbobjc.h; path = google/protobuf/Type.pbobjc.h; sourceTree = ""; }; + F4E675C51B21D1440054530B /* Type.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Type.pbobjc.m; path = google/protobuf/Type.pbobjc.m; sourceTree = ""; }; + F4E675C61B21D1440054530B /* Wrappers.pbobjc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Wrappers.pbobjc.h; path = google/protobuf/Wrappers.pbobjc.h; sourceTree = ""; }; + F4E675C71B21D1440054530B /* Wrappers.pbobjc.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = Wrappers.pbobjc.m; path = google/protobuf/Wrappers.pbobjc.m; sourceTree = ""; }; + F4E675D81B21D1DE0054530B /* any.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = any.proto; path = ../src/google/protobuf/any.proto; sourceTree = ""; }; + F4E675D91B21D1DE0054530B /* api.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = api.proto; path = ../src/google/protobuf/api.proto; sourceTree = ""; }; + F4E675DA1B21D1DE0054530B /* empty.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = empty.proto; path = ../src/google/protobuf/empty.proto; sourceTree = ""; }; + F4E675DB1B21D1DE0054530B /* field_mask.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = field_mask.proto; path = ../src/google/protobuf/field_mask.proto; sourceTree = ""; }; + F4E675DC1B21D1DE0054530B /* source_context.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = source_context.proto; path = ../src/google/protobuf/source_context.proto; sourceTree = ""; }; + F4E675DD1B21D1DE0054530B /* struct.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = struct.proto; path = ../src/google/protobuf/struct.proto; sourceTree = ""; }; + F4E675DE1B21D1DE0054530B /* type.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = type.proto; path = ../src/google/protobuf/type.proto; sourceTree = ""; }; + F4E675DF1B21D1DE0054530B /* wrappers.proto */ = {isa = PBXFileReference; lastKnownFileType = text; name = wrappers.proto; path = ../src/google/protobuf/wrappers.proto; sourceTree = ""; }; + F4F53F8B219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = text_format_extensions_unittest_data.txt; sourceTree = ""; }; + F4F8D8841D78A186002CE128 /* GPBUnittestProtos2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnittestProtos2.m; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 7461B52C0F94FAF800A0C422 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 7461B5360F94FB4600A0C422 /* Foundation.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 8BBEA4A3147C727100C4ADB7 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 8BBEA4BB147C729200C4ADB7 /* libProtocolBuffers.a in Frameworks */, + 8B9A5EEC18330A0F00A9D33B /* UIKit.framework in Frameworks */, + 8BF8193514A0DDA600A2C982 /* Foundation.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F4487C691A9F8F8100531423 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + F4487C6A1A9F8F8100531423 /* Foundation.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 080E96DDFE201D6D7F000001 /* Core Source */ = { + isa = PBXGroup; + children = ( + 7461B4CD0F94F99000A0C422 /* GPBProtocolBuffers.h */, + F451D3F61A8AAEA600B8A22C /* GPBProtocolBuffers_RuntimeSupport.h */, + 8BCF338814ED799900BC5317 /* GPBProtocolBuffers.m */, + 7461B3C50F94F84100A0C422 /* Extensions */, + 7461B4850F94F96600A0C422 /* Fields */, + 7461B4860F94F96B00A0C422 /* IO */, + 7461B5150F94FA7300A0C422 /* Messages */, + 8BCF334414ED727300BC5317 /* Support */, + 29B97315FDCFA39411CA2CEA /* Generated */, + ); + name = "Core Source"; + sourceTree = ""; + }; + 19C28FACFE9D520D11CA2CBB /* Products */ = { + isa = PBXGroup; + children = ( + 7461B52E0F94FAF800A0C422 /* libProtocolBuffers.a */, + 8BBEA4A6147C727100C4ADB7 /* UnitTests.xctest */, + F4487C6E1A9F8F8100531423 /* libTestSingleSourceBuild.a */, + ); + name = Products; + sourceTree = ""; + }; + 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { + isa = PBXGroup; + children = ( + 080E96DDFE201D6D7F000001 /* Core Source */, + 7461B6940F94FDDD00A0C422 /* Tests */, + 29B97323FDCFA39411CA2CEA /* Frameworks */, + 19C28FACFE9D520D11CA2CBB /* Products */, + ); + name = CustomTemplate; + sourceTree = ""; + }; + 29B97315FDCFA39411CA2CEA /* Generated */ = { + isa = PBXGroup; + children = ( + F4E675B61B21D1440054530B /* Any.pbobjc.h */, + F4E675B71B21D1440054530B /* Any.pbobjc.m */, + F4E675D81B21D1DE0054530B /* any.proto */, + F4E675B81B21D1440054530B /* Api.pbobjc.h */, + F4E675B91B21D1440054530B /* Api.pbobjc.m */, + F4E675D91B21D1DE0054530B /* api.proto */, + 8B4248DD1A929C7D00BC1EC6 /* Duration.pbobjc.h */, + 8B4248DE1A929C7D00BC1EC6 /* Duration.pbobjc.m */, + 8B42494A1A92A0BA00BC1EC6 /* duration.proto */, + F4E675BB1B21D1440054530B /* Empty.pbobjc.h */, + F4E675BC1B21D1440054530B /* Empty.pbobjc.m */, + F4E675DA1B21D1DE0054530B /* empty.proto */, + F4E675DB1B21D1DE0054530B /* field_mask.proto */, + F4E675BD1B21D1440054530B /* FieldMask.pbobjc.h */, + F4E675BE1B21D1440054530B /* FieldMask.pbobjc.m */, + F4E675DC1B21D1DE0054530B /* source_context.proto */, + F4E675BF1B21D1440054530B /* SourceContext.pbobjc.h */, + F4E675C01B21D1440054530B /* SourceContext.pbobjc.m */, + F4E675C11B21D1440054530B /* Struct.pbobjc.h */, + F4E675C21B21D1440054530B /* Struct.pbobjc.m */, + F4E675DD1B21D1DE0054530B /* struct.proto */, + F4E675C31B21D1440054530B /* Timestamp.pbobjc.h */, + 8B4248E01A929C7D00BC1EC6 /* Timestamp.pbobjc.m */, + 8B4249481A92A02300BC1EC6 /* timestamp.proto */, + F4E675C41B21D1440054530B /* Type.pbobjc.h */, + F4E675C51B21D1440054530B /* Type.pbobjc.m */, + F4E675DE1B21D1DE0054530B /* type.proto */, + F4E675C61B21D1440054530B /* Wrappers.pbobjc.h */, + F4E675C71B21D1440054530B /* Wrappers.pbobjc.m */, + F4E675DF1B21D1DE0054530B /* wrappers.proto */, + ); + name = Generated; + sourceTree = ""; + }; + 29B97323FDCFA39411CA2CEA /* Frameworks */ = { + isa = PBXGroup; + children = ( + 8B9742421A8AAA7800DCE92C /* CoreGraphics.framework */, + 8B9A5E9F1831913D00A9D33B /* UIKit.framework */, + 1D30AB110D05D00D00671497 /* Foundation.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + 7461B3C50F94F84100A0C422 /* Extensions */ = { + isa = PBXGroup; + children = ( + F4B6B8B31A9CD1C600892426 /* GPBExtensionInternals.h */, + F45C69CB16DFD08D0081955B /* GPBExtensionInternals.m */, + 7461B4A80F94F99000A0C422 /* GPBExtensionRegistry.h */, + 7461B4A90F94F99000A0C422 /* GPBExtensionRegistry.m */, + F4B6B8B51A9CD1C600892426 /* GPBRootObject_PackagePrivate.h */, + 8B79657814992E3E002FFBFC /* GPBRootObject.h */, + 8B79657914992E3E002FFBFC /* GPBRootObject.m */, + ); + name = Extensions; + sourceTree = ""; + }; + 7461B4850F94F96600A0C422 /* Fields */ = { + isa = PBXGroup; + children = ( + F4B6B8B01A9CC99500892426 /* GPBUnknownField_PackagePrivate.h */, + 7461B4AE0F94F99000A0C422 /* GPBUnknownField.h */, + 7461B4AF0F94F99000A0C422 /* GPBUnknownField.m */, + F4B6B8B11A9CCBBB00892426 /* GPBUnknownFieldSet_PackagePrivate.h */, + 7461B4E10F94F99000A0C422 /* GPBUnknownFieldSet.h */, + 7461B4E20F94F99000A0C422 /* GPBUnknownFieldSet.m */, + ); + name = Fields; + sourceTree = ""; + }; + 7461B4860F94F96B00A0C422 /* IO */ = { + isa = PBXGroup; + children = ( + 51457B5F18D0B7AF00CCC606 /* GPBCodedInputStream_PackagePrivate.h */, + 7461B48E0F94F99000A0C422 /* GPBCodedInputStream.h */, + 7461B48F0F94F99000A0C422 /* GPBCodedInputStream.m */, + F44929021C866B3B00C2548A /* GPBCodedOutputStream_PackagePrivate.h */, + 7461B4900F94F99000A0C422 /* GPBCodedOutputStream.h */, + 7461B4910F94F99000A0C422 /* GPBCodedOutputStream.m */, + 7461B4E70F94F99000A0C422 /* GPBWireFormat.h */, + 7461B4E80F94F99000A0C422 /* GPBWireFormat.m */, + ); + name = IO; + sourceTree = ""; + }; + 7461B5150F94FA7300A0C422 /* Messages */ = { + isa = PBXGroup; + children = ( + 515B840C18B7DEE30031753B /* GPBDescriptor_PackagePrivate.h */, + 8B96157214C8B06000A2AC0B /* GPBDescriptor.h */, + 8B96157314C8C38C00A2AC0B /* GPBDescriptor.m */, + 7461B4BE0F94F99000A0C422 /* GPBMessage.h */, + 5196A06918CE16B000B759E2 /* GPBMessage_PackagePrivate.h */, + 7461B4BF0F94F99000A0C422 /* GPBMessage.m */, + ); + name = Messages; + sourceTree = ""; + }; + 7461B6940F94FDDD00A0C422 /* Tests */ = { + isa = PBXGroup; + children = ( + 8B210CCD159383D60032D72D /* golden_message */, + 8B210CCF159386920032D72D /* golden_packed_fields_message */, + 8B8B615C17DF7056002EE618 /* GPBARCUnittestProtos.m */, + F401DC341A8E5C6F00FCC765 /* GPBArrayTests.m */, + 7461B69B0F94FDF800A0C422 /* GPBCodedInputStreamTests.m */, + 7461B69D0F94FDF800A0C422 /* GPBCodedOuputStreamTests.m */, + F40EE4CD206BF9170071091A /* GPBCompileTest01.m */, + F40EE4C6206BF9170071091A /* GPBCompileTest02.m */, + F40EE4D6206BF9190071091A /* GPBCompileTest03.m */, + F40EE4C3206BF9160071091A /* GPBCompileTest04.m */, + F40EE4C4206BF9160071091A /* GPBCompileTest05.m */, + F40EE4D7206BF9190071091A /* GPBCompileTest06.m */, + F40EE4C5206BF9170071091A /* GPBCompileTest07.m */, + F40EE4C2206BF9160071091A /* GPBCompileTest08.m */, + F40EE4C9206BF9170071091A /* GPBCompileTest09.m */, + F40EE4D1206BF9180071091A /* GPBCompileTest10.m */, + F40EE4D8206BF9190071091A /* GPBCompileTest11.m */, + F40EE4D5206BF9180071091A /* GPBCompileTest12.m */, + F40EE4C8206BF9170071091A /* GPBCompileTest13.m */, + F40EE4D4206BF9180071091A /* GPBCompileTest14.m */, + F40EE4CE206BF9170071091A /* GPBCompileTest15.m */, + F40EE4D0206BF9180071091A /* GPBCompileTest16.m */, + F40EE4D2206BF9180071091A /* GPBCompileTest17.m */, + F40EE4D3206BF9180071091A /* GPBCompileTest18.m */, + F40EE4C7206BF9170071091A /* GPBCompileTest19.m */, + F40EE4CA206BF9170071091A /* GPBCompileTest20.m */, + F40EE4CB206BF9170071091A /* GPBCompileTest21.m */, + F40EE4CF206BF9170071091A /* GPBCompileTest22.m */, + F40EE4CC206BF9170071091A /* GPBCompileTest23.m */, + F40EE50E206C06880071091A /* GPBCompileTest24.m */, + F40EE50D206C06880071091A /* GPBCompileTest25.m */, + 5102DABB1891A052002037B6 /* GPBConcurrencyTests.m */, + F4353D1E1AB88243005A6198 /* GPBDescriptorTests.m */, + F4C4B9E51E1D97BB00D3B61D /* GPBDictionaryTests.m */, + F4353D3A1AC06F31005A6198 /* GPBDictionaryTests.pddm */, + F4353D3B1AC06F31005A6198 /* GPBDictionaryTests+Bool.m */, + F4353D3C1AC06F31005A6198 /* GPBDictionaryTests+Int32.m */, + F4353D3D1AC06F31005A6198 /* GPBDictionaryTests+Int64.m */, + F4353D3E1AC06F31005A6198 /* GPBDictionaryTests+String.m */, + F4353D3F1AC06F31005A6198 /* GPBDictionaryTests+UInt32.m */, + F4353D401AC06F31005A6198 /* GPBDictionaryTests+UInt64.m */, + F4584D801ECCB39E00803AB6 /* GPBExtensionRegistryTest.m */, + 7461B6A30F94FDF800A0C422 /* GPBMessageTests.m */, + F4487C841AAF6AC500531423 /* GPBMessageTests+Merge.m */, + F4487C761AADF84900531423 /* GPBMessageTests+Runtime.m */, + F4487C801AAF62FC00531423 /* GPBMessageTests+Serialization.m */, + F4B51B1B1BBC5C7100744318 /* GPBObjectiveCPlusPlusTest.mm */, + F41C175C1833D3310064ED4D /* GPBPerfTests.m */, + 8B4248B31A8BD96E00BC1EC6 /* GPBSwiftTests.swift */, + 7461B6AB0F94FDF800A0C422 /* GPBTestUtilities.h */, + 7461B6AC0F94FDF800A0C422 /* GPBTestUtilities.m */, + 8BD3981E14BE59D70081D629 /* GPBUnittestProtos.m */, + F4F8D8841D78A186002CE128 /* GPBUnittestProtos2.m */, + 7461B6B80F94FDF900A0C422 /* GPBUnknownFieldSetTest.m */, + 7461B6BA0F94FDF900A0C422 /* GPBUtilitiesTests.m */, + 8B4248E51A929C9900BC1EC6 /* GPBWellKnownTypesTest.m */, + 7461B6BC0F94FDF900A0C422 /* GPBWireFormatTests.m */, + F4F53F8B219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt */, + F45E57C81AE6DC98000B7D99 /* text_format_map_unittest_data.txt */, + F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */, + 8B7E6A7414893DBA00F8884A /* unittest_custom_options.proto */, + F4AC9E1C1A8BEB1000BD6E83 /* unittest_cycle.proto */, + 8B7E6A7514893DBA00F8884A /* unittest_embed_optimize_for.proto */, + 8B7E6A7614893DBA00F8884A /* unittest_empty.proto */, + 8BD3981D14BE54220081D629 /* unittest_enormous_descriptor.proto */, + 8B7E6A7814893DBB00F8884A /* unittest_import.proto */, + 8BBD9DB016DD1DC8008E1EC1 /* unittest_lite.proto */, + 8B7E6A7B14893DBC00F8884A /* unittest_mset.proto */, + 8B7E6A7C14893DBC00F8884A /* unittest_no_generic_services.proto */, + 8B35468621A61EB2000BD30D /* unittest_objc_options.proto */, + F4CF31711B162EF500BD9B06 /* unittest_objc_startup.proto */, + 8B09AAF614B663A7007B4184 /* unittest_objc.proto */, + 8B7E6A7D14893DBC00F8884A /* unittest_optimize_for.proto */, + F4487C7A1AADFB5500531423 /* unittest_runtime_proto2.proto */, + F4487C7B1AADFB5500531423 /* unittest_runtime_proto3.proto */, + 8B7E6A7E14893DBC00F8884A /* unittest.proto */, + 8B4248B21A8BD96D00BC1EC6 /* UnitTests-Bridging-Header.h */, + 7401C1A90F950347006D8281 /* UnitTests-Info.plist */, + ); + path = Tests; + sourceTree = ""; + }; + 8BCF334414ED727300BC5317 /* Support */ = { + isa = PBXGroup; + children = ( + F4411BE81AF1301700324B4A /* GPBArray_PackagePrivate.h */, + F4487C701A9F906200531423 /* GPBArray.h */, + F4487C711A9F906200531423 /* GPBArray.m */, + 7461B48D0F94F99000A0C422 /* GPBBootstrap.h */, + F43725921AC9835D004DCAFB /* GPBDictionary_PackagePrivate.h */, + F4353D241ABB156F005A6198 /* GPBDictionary.h */, + F4353D251ABB156F005A6198 /* GPBDictionary.m */, + 8BEB5AE01498033E0078BF9D /* GPBRuntimeTypes.h */, + F4487C7D1AAE06C500531423 /* GPBUtilities_PackagePrivate.h */, + 7461B4E50F94F99000A0C422 /* GPBUtilities.h */, + 7461B4E60F94F99000A0C422 /* GPBUtilities.m */, + 8B4248E11A929C8900BC1EC6 /* GPBWellKnownTypes.h */, + 8B4248E21A929C8900BC1EC6 /* GPBWellKnownTypes.m */, + ); + name = Support; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 7461B52A0F94FAF800A0C422 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F4487C561A9F8F8100531423 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXLegacyTarget section */ + F45BBC0E1B0CDB50002D064D /* Compile Unittest Protos */ = { + isa = PBXLegacyTarget; + buildArgumentsString = "$(ACTION)"; + buildConfigurationList = F45BBC111B0CDB50002D064D /* Build configuration list for PBXLegacyTarget "Compile Unittest Protos" */; + buildPhases = ( + ); + buildToolPath = DevTools/compile_testing_protos.sh; + buildWorkingDirectory = ""; + dependencies = ( + ); + name = "Compile Unittest Protos"; + passBuildSettingsInEnvironment = 1; + productName = "Compile Unittest Protos"; + }; +/* End PBXLegacyTarget section */ + +/* Begin PBXNativeTarget section */ + 7461B52D0F94FAF800A0C422 /* ProtocolBuffers */ = { + isa = PBXNativeTarget; + buildConfigurationList = 7461B5330F94FAFD00A0C422 /* Build configuration list for PBXNativeTarget "ProtocolBuffers" */; + buildPhases = ( + 7461B52A0F94FAF800A0C422 /* Headers */, + 7461B52B0F94FAF800A0C422 /* Sources */, + 7461B52C0F94FAF800A0C422 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = ProtocolBuffers; + productName = "ProtocolBuffers-iPhoneDevice"; + productReference = 7461B52E0F94FAF800A0C422 /* libProtocolBuffers.a */; + productType = "com.apple.product-type.library.static"; + }; + 8BBEA4A5147C727100C4ADB7 /* UnitTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = 8BBEA4BA147C728600C4ADB7 /* Build configuration list for PBXNativeTarget "UnitTests" */; + buildPhases = ( + F4B62A791AF91F7500AFCEDC /* Script: Check Runtime Stamps */, + 8BBEA4A1147C727100C4ADB7 /* Resources */, + 8BBEA4A2147C727100C4ADB7 /* Sources */, + 8BBEA4A3147C727100C4ADB7 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + 8BBEA4BD147C729A00C4ADB7 /* PBXTargetDependency */, + F45BBC131B0CDBBA002D064D /* PBXTargetDependency */, + ); + name = UnitTests; + productName = UnitTests; + productReference = 8BBEA4A6147C727100C4ADB7 /* UnitTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; + F4487C551A9F8F8100531423 /* TestSingleSourceBuild */ = { + isa = PBXNativeTarget; + buildConfigurationList = F4487C6B1A9F8F8100531423 /* Build configuration list for PBXNativeTarget "TestSingleSourceBuild" */; + buildPhases = ( + F4487C561A9F8F8100531423 /* Headers */, + F4487C5A1A9F8F8100531423 /* Sources */, + F4487C691A9F8F8100531423 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = TestSingleSourceBuild; + productName = "ProtocolBuffers-iPhoneDevice"; + productReference = F4487C6E1A9F8F8100531423 /* libTestSingleSourceBuild.a */; + productType = "com.apple.product-type.library.static"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29B97313FDCFA39411CA2CEA /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0710; + LastTestingUpgradeCheck = 0600; + LastUpgradeCheck = 0930; + TargetAttributes = { + 8BBEA4A5147C727100C4ADB7 = { + LastSwiftMigration = 0940; + TestTargetID = 8B9A5EA41831993600A9D33B; + }; + F45BBC0E1B0CDB50002D064D = { + CreatedOnToolsVersion = 6.3.2; + }; + }; + }; + buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "ProtocolBuffers_tvOS" */; + compatibilityVersion = "Xcode 6.3"; + developmentRegion = English; + hasScannedForEncodings = 1; + knownRegions = ( + en, + ); + mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 7461B52D0F94FAF800A0C422 /* ProtocolBuffers */, + 8BBEA4A5147C727100C4ADB7 /* UnitTests */, + F4487C551A9F8F8100531423 /* TestSingleSourceBuild */, + F45BBC0E1B0CDB50002D064D /* Compile Unittest Protos */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 8BBEA4A1147C727100C4ADB7 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 8B210CCE159383D60032D72D /* golden_message in Resources */, + F4F53F8C219CC5DF001EABF4 /* text_format_extensions_unittest_data.txt in Resources */, + F43C88D0191D77FC009E917D /* text_format_unittest_data.txt in Resources */, + 8B210CD0159386920032D72D /* golden_packed_fields_message in Resources */, + F45E57C91AE6DC98000B7D99 /* text_format_map_unittest_data.txt in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXShellScriptBuildPhase section */ + F4B62A791AF91F7500AFCEDC /* Script: Check Runtime Stamps */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = "Script: Check Runtime Stamps"; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "set -eu\nexec \"${SOURCE_ROOT}/DevTools/check_version_stamps.sh\"\n"; + showEnvVarsInLog = 0; + }; +/* End PBXShellScriptBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 7461B52B0F94FAF800A0C422 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 7461B53C0F94FB4E00A0C422 /* GPBCodedInputStream.m in Sources */, + F4E675D21B21D1620054530B /* Empty.pbobjc.m in Sources */, + F4487C731A9F906200531423 /* GPBArray.m in Sources */, + 7461B53D0F94FB4E00A0C422 /* GPBCodedOutputStream.m in Sources */, + 7461B5490F94FB4E00A0C422 /* GPBExtensionRegistry.m in Sources */, + F4E675D31B21D1620054530B /* FieldMask.pbobjc.m in Sources */, + 7461B54C0F94FB4E00A0C422 /* GPBUnknownField.m in Sources */, + 7461B5530F94FB4E00A0C422 /* GPBMessage.m in Sources */, + F47476E91D21A537007C7B1A /* Duration.pbobjc.m in Sources */, + 7461B5610F94FB4E00A0C422 /* GPBUnknownFieldSet.m in Sources */, + 7461B5630F94FB4E00A0C422 /* GPBUtilities.m in Sources */, + 7461B5640F94FB4E00A0C422 /* GPBWireFormat.m in Sources */, + F4E675D11B21D1620054530B /* Api.pbobjc.m in Sources */, + F4E675D41B21D1620054530B /* SourceContext.pbobjc.m in Sources */, + F4353D271ABB156F005A6198 /* GPBDictionary.m in Sources */, + 8B79657B14992E3F002FFBFC /* GPBRootObject.m in Sources */, + 8B96157414C8C38C00A2AC0B /* GPBDescriptor.m in Sources */, + F4E675D01B21D1620054530B /* Any.pbobjc.m in Sources */, + F45C69CC16DFD08D0081955B /* GPBExtensionInternals.m in Sources */, + 8B4248E41A929C8900BC1EC6 /* GPBWellKnownTypes.m in Sources */, + F4E675D61B21D1620054530B /* Type.pbobjc.m in Sources */, + F4E675D51B21D1620054530B /* Struct.pbobjc.m in Sources */, + F4E675D71B21D1620054530B /* Wrappers.pbobjc.m in Sources */, + F47476EA1D21A537007C7B1A /* Timestamp.pbobjc.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 8BBEA4A2147C727100C4ADB7 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 8BBEA4A9147C727D00C4ADB7 /* GPBCodedInputStreamTests.m in Sources */, + F40EE511206C068D0071091A /* GPBCompileTest24.m in Sources */, + F40EE503206BF91E0071091A /* GPBCompileTest20.m in Sources */, + F401DC351A8E5C6F00FCC765 /* GPBArrayTests.m in Sources */, + F40EE4F9206BF91E0071091A /* GPBCompileTest10.m in Sources */, + F4353D441AC06F31005A6198 /* GPBDictionaryTests+Int64.m in Sources */, + F40EE505206BF91E0071091A /* GPBCompileTest22.m in Sources */, + F40EE4F7206BF91E0071091A /* GPBCompileTest08.m in Sources */, + F40EE500206BF91E0071091A /* GPBCompileTest17.m in Sources */, + F4353D471AC06F31005A6198 /* GPBDictionaryTests+UInt64.m in Sources */, + 8BBEA4AA147C727D00C4ADB7 /* GPBCodedOuputStreamTests.m in Sources */, + F40EE506206BF91E0071091A /* GPBCompileTest23.m in Sources */, + 8BBEA4AC147C727D00C4ADB7 /* GPBMessageTests.m in Sources */, + F4487C811AAF62FC00531423 /* GPBMessageTests+Serialization.m in Sources */, + F40EE502206BF91E0071091A /* GPBCompileTest19.m in Sources */, + F40EE4F5206BF91E0071091A /* GPBCompileTest06.m in Sources */, + F40EE4FB206BF91E0071091A /* GPBCompileTest12.m in Sources */, + 8B4248E61A929C9900BC1EC6 /* GPBWellKnownTypesTest.m in Sources */, + F40EE4F2206BF91E0071091A /* GPBCompileTest03.m in Sources */, + F40EE501206BF91E0071091A /* GPBCompileTest18.m in Sources */, + F40EE4FC206BF91E0071091A /* GPBCompileTest13.m in Sources */, + F40EE4FE206BF91E0071091A /* GPBCompileTest15.m in Sources */, + F40EE4F6206BF91E0071091A /* GPBCompileTest07.m in Sources */, + F4353D1F1AB88243005A6198 /* GPBDescriptorTests.m in Sources */, + F40EE504206BF91E0071091A /* GPBCompileTest21.m in Sources */, + F40EE4FA206BF91E0071091A /* GPBCompileTest11.m in Sources */, + F4F8D8861D78A193002CE128 /* GPBUnittestProtos2.m in Sources */, + F4B51B1C1BBC5C7100744318 /* GPBObjectiveCPlusPlusTest.mm in Sources */, + 8B4248B41A8BD96E00BC1EC6 /* GPBSwiftTests.swift in Sources */, + F40EE512206C068D0071091A /* GPBCompileTest25.m in Sources */, + F4584D831ECCB53600803AB6 /* GPBExtensionRegistryTest.m in Sources */, + 5102DABC1891A073002037B6 /* GPBConcurrencyTests.m in Sources */, + F4487C771AADF84900531423 /* GPBMessageTests+Runtime.m in Sources */, + F40EE4F1206BF91E0071091A /* GPBCompileTest02.m in Sources */, + F4353D431AC06F31005A6198 /* GPBDictionaryTests+Int32.m in Sources */, + F40EE4F4206BF91E0071091A /* GPBCompileTest05.m in Sources */, + F40EE4FD206BF91E0071091A /* GPBCompileTest14.m in Sources */, + 8BBEA4B0147C727D00C4ADB7 /* GPBTestUtilities.m in Sources */, + F40EE4F3206BF91E0071091A /* GPBCompileTest04.m in Sources */, + F40EE4FF206BF91E0071091A /* GPBCompileTest16.m in Sources */, + F41C175D1833D3310064ED4D /* GPBPerfTests.m in Sources */, + F4353D421AC06F31005A6198 /* GPBDictionaryTests+Bool.m in Sources */, + F4487C851AAF6AC500531423 /* GPBMessageTests+Merge.m in Sources */, + F40EE4F0206BF91E0071091A /* GPBCompileTest01.m in Sources */, + 8BBEA4B6147C727D00C4ADB7 /* GPBUnknownFieldSetTest.m in Sources */, + F4353D451AC06F31005A6198 /* GPBDictionaryTests+String.m in Sources */, + F4353D461AC06F31005A6198 /* GPBDictionaryTests+UInt32.m in Sources */, + F40EE4F8206BF91E0071091A /* GPBCompileTest09.m in Sources */, + 8BBEA4B7147C727D00C4ADB7 /* GPBUtilitiesTests.m in Sources */, + F4C4B9E71E1D97BF00D3B61D /* GPBDictionaryTests.m in Sources */, + 8BBEA4B8147C727D00C4ADB7 /* GPBWireFormatTests.m in Sources */, + 8BD3981F14BE59D70081D629 /* GPBUnittestProtos.m in Sources */, + 8B8B615D17DF7056002EE618 /* GPBARCUnittestProtos.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F4487C5A1A9F8F8100531423 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F4487C6F1A9F8FFF00531423 /* GPBProtocolBuffers.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + 8BBEA4BD147C729A00C4ADB7 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 7461B52D0F94FAF800A0C422 /* ProtocolBuffers */; + targetProxy = 8BBEA4BC147C729A00C4ADB7 /* PBXContainerItemProxy */; + }; + F45BBC131B0CDBBA002D064D /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = F45BBC0E1B0CDB50002D064D /* Compile Unittest Protos */; + targetProxy = F45BBC121B0CDBBA002D064D /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin XCBuildConfiguration section */ + 7461B52F0F94FAFA00A0C422 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_OBJC_WEAK = YES; + PRODUCT_NAME = ProtocolBuffers; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; + }; + name = Debug; + }; + 7461B5300F94FAFA00A0C422 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_OBJC_WEAK = YES; + PRODUCT_NAME = ProtocolBuffers; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; + }; + name = Release; + }; + 8BBEA4A7147C727100C4ADB7 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + FRAMEWORK_SEARCH_PATHS = ( + "\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"", + "$(inherited)", + ); + INFOPLIST_FILE = "Tests/UnitTests-Info.plist"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "\"$(DEVELOPER_DIR)/usr/lib\"", + ); + OTHER_LDFLAGS = "-ObjC"; + PRODUCT_NAME = UnitTests; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/UnitTests-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; + USER_HEADER_SEARCH_PATHS = "${PROJECT_DERIVED_FILE_DIR}/protos $(SRCROOT)"; + WARNING_CFLAGS = ( + "$(inherited)", + "-Wno-documentation-unknown-command", + "-Wno-reserved-id-macro", + "-Wno-direct-ivar-access", + ); + }; + name = Debug; + }; + 8BBEA4A8147C727100C4ADB7 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + FRAMEWORK_SEARCH_PATHS = ( + "\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"", + "$(inherited)", + ); + INFOPLIST_FILE = "Tests/UnitTests-Info.plist"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "\"$(DEVELOPER_DIR)/usr/lib\"", + ); + OTHER_LDFLAGS = "-ObjC"; + PRODUCT_NAME = UnitTests; + SWIFT_OBJC_BRIDGING_HEADER = "Tests/UnitTests-Bridging-Header.h"; + SWIFT_VERSION = 4.0; + USER_HEADER_SEARCH_PATHS = "${PROJECT_DERIVED_FILE_DIR}/protos $(SRCROOT)"; + WARNING_CFLAGS = ( + "$(inherited)", + "-Wno-documentation-unknown-command", + "-Wno-reserved-id-macro", + "-Wno-direct-ivar-access", + ); + }; + name = Release; + }; + C01FCF4F08A954540054247B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES; + CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES; + CLANG_ANALYZER_SECURITY_INSECUREAPI_STRCPY = YES; + CLANG_STATIC_ANALYZER_MODE = deep; + CLANG_WARN_ASSIGN_ENUM = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION = YES; + CLANG_WARN_OBJC_EXPLICIT_OWNERSHIP_TYPE = YES; + CLANG_WARN_OBJC_IMPLICIT_ATOMIC_PROPERTIES = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES_AGGRESSIVE; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = c99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = "DEBUG=1"; + GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; + GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES; + GCC_TREAT_WARNINGS_AS_ERRORS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES; + GCC_WARN_ABOUT_MISSING_NEWLINE = YES; + GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES; + GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES; + GCC_WARN_MISSING_PARENTHESES = YES; + GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES; + GCC_WARN_SHADOW = YES; + GCC_WARN_SIGN_COMPARE = YES; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNKNOWN_PRAGMAS = YES; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_LABEL = YES; + GCC_WARN_UNUSED_PARAMETER = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + GENERATE_PROFILING_CODE = NO; + ONLY_ACTIVE_ARCH = YES; + RUN_CLANG_STATIC_ANALYZER = YES; + SDKROOT = appletvos; + TVOS_DEPLOYMENT_TARGET = 9.0; + WARNING_CFLAGS = ( + "-Wdocumentation-unknown-command", + "-Wundef", + "-Wreserved-id-macro", + "-Wswitch-enum", + "-Wdirect-ivar-access", + "-Woverriding-method-mismatch", + ); + }; + name = Debug; + }; + C01FCF5008A954540054247B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES; + CLANG_ANALYZER_SECURITY_INSECUREAPI_RAND = YES; + CLANG_ANALYZER_SECURITY_INSECUREAPI_STRCPY = YES; + CLANG_STATIC_ANALYZER_MODE = deep; + CLANG_WARN_ASSIGN_ENUM = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION = YES; + CLANG_WARN_OBJC_EXPLICIT_OWNERSHIP_TYPE = YES; + CLANG_WARN_OBJC_IMPLICIT_ATOMIC_PROPERTIES = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES_AGGRESSIVE; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = c99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; + GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES; + GCC_TREAT_WARNINGS_AS_ERRORS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES; + GCC_WARN_ABOUT_MISSING_NEWLINE = YES; + GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES; + GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES; + GCC_WARN_MISSING_PARENTHESES = YES; + GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES; + GCC_WARN_SHADOW = YES; + GCC_WARN_SIGN_COMPARE = YES; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNKNOWN_PRAGMAS = YES; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_LABEL = YES; + GCC_WARN_UNUSED_PARAMETER = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + GENERATE_PROFILING_CODE = NO; + RUN_CLANG_STATIC_ANALYZER = YES; + SDKROOT = appletvos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + TVOS_DEPLOYMENT_TARGET = 9.0; + WARNING_CFLAGS = ( + "-Wdocumentation-unknown-command", + "-Wundef", + "-Wreserved-id-macro", + "-Wswitch-enum", + "-Wdirect-ivar-access", + "-Woverriding-method-mismatch", + ); + }; + name = Release; + }; + F4487C6C1A9F8F8100531423 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_OBJC_WEAK = YES; + PRODUCT_NAME = TestSingleSourceBuild; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; + }; + name = Debug; + }; + F4487C6D1A9F8F8100531423 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_OBJC_WEAK = YES; + PRODUCT_NAME = TestSingleSourceBuild; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)"; + }; + name = Release; + }; + F45BBC0F1B0CDB50002D064D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_OBJC_WEAK = YES; + }; + name = Debug; + }; + F45BBC101B0CDB50002D064D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_OBJC_WEAK = YES; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 7461B5330F94FAFD00A0C422 /* Build configuration list for PBXNativeTarget "ProtocolBuffers" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 7461B52F0F94FAFA00A0C422 /* Debug */, + 7461B5300F94FAFA00A0C422 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 8BBEA4BA147C728600C4ADB7 /* Build configuration list for PBXNativeTarget "UnitTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 8BBEA4A7147C727100C4ADB7 /* Debug */, + 8BBEA4A8147C727100C4ADB7 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + C01FCF4E08A954540054247B /* Build configuration list for PBXProject "ProtocolBuffers_tvOS" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C01FCF4F08A954540054247B /* Debug */, + C01FCF5008A954540054247B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F4487C6B1A9F8F8100531423 /* Build configuration list for PBXNativeTarget "TestSingleSourceBuild" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F4487C6C1A9F8F8100531423 /* Debug */, + F4487C6D1A9F8F8100531423 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F45BBC111B0CDB50002D064D /* Build configuration list for PBXLegacyTarget "Compile Unittest Protos" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F45BBC0F1B0CDB50002D064D /* Debug */, + F45BBC101B0CDB50002D064D /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29B97313FDCFA39411CA2CEA /* Project object */; +} diff --git a/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000000..4c32c7f836 --- /dev/null +++ b/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000000..18d981003d --- /dev/null +++ b/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000000..08de0be8d3 --- /dev/null +++ b/objectivec/ProtocolBuffers_tvOS.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded + + + diff --git a/objectivec/ProtocolBuffers_tvOS.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme b/objectivec/ProtocolBuffers_tvOS.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme new file mode 100644 index 0000000000..b11dc6ea93 --- /dev/null +++ b/objectivec/ProtocolBuffers_tvOS.xcodeproj/xcshareddata/xcschemes/PerformanceTests.xcscheme @@ -0,0 +1,370 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/objectivec/ProtocolBuffers_tvOS.xcodeproj/xcshareddata/xcschemes/ProtocolBuffers.xcscheme b/objectivec/ProtocolBuffers_tvOS.xcodeproj/xcshareddata/xcschemes/ProtocolBuffers.xcscheme new file mode 100644 index 0000000000..25d047a188 --- /dev/null +++ b/objectivec/ProtocolBuffers_tvOS.xcodeproj/xcshareddata/xcschemes/ProtocolBuffers.xcscheme @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/objectivec/Tests/GPBDescriptorTests.m b/objectivec/Tests/GPBDescriptorTests.m index d47cc30fce..46c1729150 100644 --- a/objectivec/Tests/GPBDescriptorTests.m +++ b/objectivec/Tests/GPBDescriptorTests.m @@ -244,7 +244,44 @@ XCTAssertTrue([descriptor getValue:&value forEnumName:enumName]); XCTAssertEqual(value, 2); XCTAssertEqualObjects([descriptor getEnumTextFormatNameForIndex:4], @"BAR2"); +} + +- (void)testEnumAliasNameCollisions { + GPBEnumDescriptor *descriptor = TestEnumObjCNameCollision_EnumDescriptor(); + NSString *textFormatName; + int32_t value; + + XCTAssertEqual(descriptor.enumNameCount, 5U); + + XCTAssertEqualObjects([descriptor getEnumNameForIndex:0], @"TestEnumObjCNameCollision_Foo"); + textFormatName = [descriptor getEnumTextFormatNameForIndex:0]; + XCTAssertEqualObjects(textFormatName, @"FOO"); + XCTAssertTrue([descriptor getValue:&value forEnumTextFormatName:textFormatName]); + XCTAssertEqual(value, 1); + XCTAssertEqualObjects([descriptor getEnumNameForIndex:1], @"TestEnumObjCNameCollision_Foo"); + textFormatName = [descriptor getEnumTextFormatNameForIndex:1]; + XCTAssertEqualObjects(textFormatName, @"foo"); + XCTAssertTrue([descriptor getValue:&value forEnumTextFormatName:textFormatName]); + XCTAssertEqual(value, 1); + + XCTAssertEqualObjects([descriptor getEnumNameForIndex:2], @"TestEnumObjCNameCollision_Bar"); + textFormatName = [descriptor getEnumTextFormatNameForIndex:2]; + XCTAssertEqualObjects(textFormatName, @"BAR"); + XCTAssertTrue([descriptor getValue:&value forEnumTextFormatName:textFormatName]); + XCTAssertEqual(value, 2); + + XCTAssertEqualObjects([descriptor getEnumNameForIndex:3], @"TestEnumObjCNameCollision_Mumble"); + textFormatName = [descriptor getEnumTextFormatNameForIndex:3]; + XCTAssertEqualObjects(textFormatName, @"mumble"); + XCTAssertTrue([descriptor getValue:&value forEnumTextFormatName:textFormatName]); + XCTAssertEqual(value, 2); + + XCTAssertEqualObjects([descriptor getEnumNameForIndex:4], @"TestEnumObjCNameCollision_Mumble"); + textFormatName = [descriptor getEnumTextFormatNameForIndex:4]; + XCTAssertEqualObjects(textFormatName, @"MUMBLE"); + XCTAssertTrue([descriptor getValue:&value forEnumTextFormatName:textFormatName]); + XCTAssertEqual(value, 2); } - (void)testEnumValueValidator { diff --git a/objectivec/Tests/GPBMessageTests+Runtime.m b/objectivec/Tests/GPBMessageTests+Runtime.m index 0058311b3b..9d98f91ca7 100644 --- a/objectivec/Tests/GPBMessageTests+Runtime.m +++ b/objectivec/Tests/GPBMessageTests+Runtime.m @@ -1482,7 +1482,7 @@ [msg release]; msg = [[Message2 alloc] init]; - uint32_t values[] = { + int32_t values[] = { Message2_O_OneOfCase_OneofInt32, Message2_O_OneOfCase_OneofInt64, Message2_O_OneOfCase_OneofUint32, @@ -1992,7 +1992,7 @@ [msg release]; msg = [[Message3 alloc] init]; - uint32_t values[] = { + int32_t values[] = { Message3_O_OneOfCase_OneofInt32, Message3_O_OneOfCase_OneofInt64, Message3_O_OneOfCase_OneofUint32, @@ -2113,7 +2113,7 @@ Message2 *msg = [[Message2 alloc] init]; - uint32_t values[] = { + int32_t values[] = { Message2_O_OneOfCase_OneofInt32, Message2_O_OneOfCase_OneofInt64, Message2_O_OneOfCase_OneofUint32, @@ -2242,7 +2242,7 @@ Message3 *msg = [[Message3 alloc] init]; - uint32_t values[] = { + int32_t values[] = { Message3_O_OneOfCase_OneofInt32, Message3_O_OneOfCase_OneofInt64, Message3_O_OneOfCase_OneofUint32, @@ -2491,6 +2491,72 @@ XCTAssertEqualObjects(@"bar", message.mapStringString[@"foo"]); } +- (void)test_StringFieldsCopy { + // ObjC conventions call for NSString properites to be copy, ensure + // that is done correctly and the string isn't simply retained. + + Message2 *msg1 = [Message2 message]; + Message2 *msg2 = [Message2 message]; + + GPBFieldDescriptor *fieldDesc = + [[Message2 descriptor] fieldWithNumber:Message2_FieldNumber_OptionalString]; + NSMutableString *mutableStr = [NSMutableString stringWithString:@"foo"]; + + msg1.optionalString = mutableStr; + GPBSetMessageStringField(msg2, fieldDesc, mutableStr); + + XCTAssertEqualObjects(msg1.optionalString, mutableStr); + XCTAssertEqualObjects(msg1.optionalString, @"foo"); + XCTAssertTrue(msg1.optionalString != mutableStr); // Ptr comparision. + + XCTAssertEqualObjects(msg2.optionalString, mutableStr); + XCTAssertEqualObjects(msg2.optionalString, @"foo"); + XCTAssertTrue(msg2.optionalString != mutableStr); // Ptr comparision. + + [mutableStr appendString:@"bar"]; + + XCTAssertNotEqualObjects(msg1.optionalString, mutableStr); + XCTAssertEqualObjects(msg1.optionalString, @"foo"); + XCTAssertTrue(msg1.optionalString != mutableStr); // Ptr comparision. + + XCTAssertNotEqualObjects(msg2.optionalString, mutableStr); + XCTAssertEqualObjects(msg2.optionalString, @"foo"); + XCTAssertTrue(msg2.optionalString != mutableStr); // Ptr comparision. +} + +- (void)test_BytesFieldsCopy { + // ObjC conventions call for NSData properites to be copy, ensure + // that is done correctly and the data isn't simply retained. + + Message2 *msg1 = [Message2 message]; + Message2 *msg2 = [Message2 message]; + + GPBFieldDescriptor *fieldDesc = + [[Message2 descriptor] fieldWithNumber:Message2_FieldNumber_OptionalBytes]; + NSMutableData *mutableData = [NSMutableData dataWithData:DataFromCStr("abc")]; + + msg1.optionalBytes = mutableData; + GPBSetMessageBytesField(msg2, fieldDesc, mutableData); + + XCTAssertEqualObjects(msg1.optionalBytes, mutableData); + XCTAssertEqualObjects(msg1.optionalBytes, DataFromCStr("abc")); + XCTAssertTrue(msg1.optionalBytes != mutableData); // Ptr comparision. + + XCTAssertEqualObjects(msg2.optionalBytes, mutableData); + XCTAssertEqualObjects(msg2.optionalBytes, DataFromCStr("abc")); + XCTAssertTrue(msg2.optionalBytes != mutableData); // Ptr comparision. + + [mutableData appendData:DataFromCStr("123")]; + + XCTAssertNotEqualObjects(msg1.optionalBytes, mutableData); + XCTAssertEqualObjects(msg1.optionalBytes, DataFromCStr("abc")); + XCTAssertTrue(msg1.optionalBytes != mutableData); // Ptr comparision. + + XCTAssertNotEqualObjects(msg2.optionalBytes, mutableData); + XCTAssertEqualObjects(msg2.optionalBytes, DataFromCStr("abc")); + XCTAssertTrue(msg2.optionalBytes != mutableData); // Ptr comparision. +} + #pragma mark - Subset from from map_tests.cc // TEST(GeneratedMapFieldTest, IsInitialized) diff --git a/objectivec/Tests/GPBMessageTests.m b/objectivec/Tests/GPBMessageTests.m index a314909657..4d0f7f8d08 100644 --- a/objectivec/Tests/GPBMessageTests.m +++ b/objectivec/Tests/GPBMessageTests.m @@ -40,6 +40,7 @@ #import "GPBUnknownFieldSet_PackagePrivate.h" #import "google/protobuf/Unittest.pbobjc.h" #import "google/protobuf/UnittestObjc.pbobjc.h" +#import "google/protobuf/UnittestObjcOptions.pbobjc.h" @interface MessageTests : GPBTestCase @end @@ -1980,8 +1981,53 @@ EnumTestMsg_MyEnum_NegTwo); } +- (void)testReservedWordNaming { + // objectivec_helpers.cc has some special handing to make sure that + // some "reserved" objc names get renamed in a way so they + // don't conflict. + // + // This "test" confirms that the expected names are generated, + // otherwise the test itself will fail to compile. + self_Class *msg = [self_Class message]; + + // Some ObjC/C/C++ keywords. + msg.className_p = msg.hasClassName_p; + msg.cmd = msg.hasCmd; + msg.nullable_p = msg.hasNullable_p; + msg.typeof_p = msg.hasTypeof_p; + msg.instancetype_p = msg.hasInstancetype_p; + msg.nil_p = msg.hasNil_p; + msg.instancetype_p = msg.hasInstancetype_p; + msg.public_p = msg.hasPublic_p; + + // Some that would override NSObject methods + msg.camltype = msg.hasCamltype; + msg.isNsdictionary = msg.hasIsNsdictionary; + msg.dealloc_p = msg.hasDealloc_p; + msg.zone_p = msg.hasZone_p; + msg.accessibilityLabel_p = msg.hasAccessibilityLabel_p; + + // Some that we shouldn't need to handle. + msg.atomic = msg.hasAtomic; + msg.nonatomic = msg.hasNonatomic; + msg.strong = msg.hasStrong; + msg.nullResettable = msg.hasNullResettable; + + // Some that would override GPBMessage methods + msg.clear_p = msg.hasClear_p; + msg.data_p = msg.hasData_p; + + // Some MacTypes + msg.fixed = msg.hasFixed; + msg.style = msg.hasStyle; + + // Some C Identifiers + msg.generic = msg.hasGeneric; + msg.block = msg.hasBlock; +} + - (void)testOneBasedEnumHolder { - // Test case for https://github.com/google/protobuf/issues/1453 + // Test case for https://github.com/protocolbuffers/protobuf/issues/1453 // Message with no explicit defaults, but a non zero default for an enum. MessageWithOneBasedEnum *enumMsg = [MessageWithOneBasedEnum message]; XCTAssertEqual(enumMsg.enumField, MessageWithOneBasedEnum_OneBasedEnum_One); @@ -2052,7 +2098,7 @@ XCTAssertEqual([msg1 hash], [msg1Prime hash]); } -- (void)testCopyingMapFileds { +- (void)testCopyingMapFields { TestMessageOfMaps *msg = [TestMessageOfMaps message]; msg.strToStr[@"foo"] = @"bar"; @@ -2103,4 +2149,38 @@ XCTAssertEqualObjects([msg.boolToMsg objectForKey:YES], [msg2.boolToMsg objectForKey:YES]); } +- (void)testPrefixedNames { + // The fact that this compiles is sufficient as a test. + // The assertions are just there to avoid "not-used" warnings. + + // Verify that enum types and values get the prefix. + GPBTESTTestObjcProtoPrefixEnum value = GPBTESTTestObjcProtoPrefixEnum_Value; + XCTAssertNotEqual(value, 0); + + // Verify that roots get the prefix. + GPBTESTUnittestObjcOptionsRoot *root = nil; + XCTAssertNil(root); + + // Verify that messages that don't already have the prefix get a prefix. + GPBTESTTestObjcProtoPrefixMessage *prefixedMessage = nil; + XCTAssertNil(prefixedMessage); + + // Verify that messages that already have a prefix aren't prefixed twice. + GPBTESTTestHasAPrefixMessage *alreadyPrefixedMessage = nil; + XCTAssertNil(alreadyPrefixedMessage); + + // Verify that enums that already have a prefix aren't prefixed twice. + GPBTESTTestHasAPrefixEnum prefixedValue = GPBTESTTestHasAPrefixEnum_ValueB; + XCTAssertNotEqual(prefixedValue, 0); + + // Verify that classes named the same as prefixes are prefixed. + GPBTESTGPBTEST *prefixMessage = nil; + XCTAssertNil(prefixMessage); + + // Verify that classes that have the prefix followed by a lowercase + // letter DO get the prefix. + GPBTESTGPBTESTshouldGetAPrefixMessage *shouldGetAPrefixMessage = nil; + XCTAssertNil(shouldGetAPrefixMessage); +} + @end diff --git a/objectivec/Tests/GPBUtilitiesTests.m b/objectivec/Tests/GPBUtilitiesTests.m index 8a8ba93e5c..f4a09de586 100644 --- a/objectivec/Tests/GPBUtilitiesTests.m +++ b/objectivec/Tests/GPBUtilitiesTests.m @@ -169,7 +169,27 @@ [expected release]; } -// TODO(thomasvl): add test with extensions once those format with correct names. +- (void)testTextFormatExtensions { + TestAllExtensions *message = [TestAllExtensions message]; + + // Not kGPBDefaultRepeatCount because we are comparing to golden master file + // which was generated with 2. + [self setAllExtensions:message repeatedCount:2]; + + NSString *result = GPBTextFormatForMessage(message, nil); + + // NOTE: ObjC TextFormat doesn't have the proper extension names so it + // uses comments for the ObjC name and raw numbers for the fields instead + // of the bracketed extension name. + NSString *fileName = @"text_format_extensions_unittest_data.txt"; + NSData *resultData = [result dataUsingEncoding:NSUTF8StringEncoding]; + NSData *expectedData = + [self getDataFileNamed:fileName dataToWrite:resultData]; + NSString *expected = [[NSString alloc] initWithData:expectedData + encoding:NSUTF8StringEncoding]; + XCTAssertEqualObjects(expected, result); + [expected release]; +} - (void)testSetRepeatedFields { TestAllTypes *message = [TestAllTypes message]; diff --git a/objectivec/Tests/text_format_extensions_unittest_data.txt b/objectivec/Tests/text_format_extensions_unittest_data.txt new file mode 100644 index 0000000000..d5205456c5 --- /dev/null +++ b/objectivec/Tests/text_format_extensions_unittest_data.txt @@ -0,0 +1,140 @@ +1: 101 # [UnittestRoot_optionalInt32Extension] +2: 102 # [UnittestRoot_optionalInt64Extension] +3: 103 # [UnittestRoot_optionalUint32Extension] +4: 104 # [UnittestRoot_optionalUint64Extension] +5: 105 # [UnittestRoot_optionalSint32Extension] +6: 106 # [UnittestRoot_optionalSint64Extension] +7: 107 # [UnittestRoot_optionalFixed32Extension] +8: 108 # [UnittestRoot_optionalFixed64Extension] +9: 109 # [UnittestRoot_optionalSfixed32Extension] +10: 110 # [UnittestRoot_optionalSfixed64Extension] +11: 111 # [UnittestRoot_optionalFloatExtension] +12: 112 # [UnittestRoot_optionalDoubleExtension] +13: true # [UnittestRoot_optionalBoolExtension] +14: "115" # [UnittestRoot_optionalStringExtension] +15: "\001\000\002\003\000\005" # [UnittestRoot_optionalBytesExtension] +16 { # [UnittestRoot_optionalGroupExtension] + a: 117 +} +18 { # [UnittestRoot_optionalNestedMessageExtension] + bb: 118 +} +19 { # [UnittestRoot_optionalForeignMessageExtension] + c: 119 +} +20 { # [UnittestRoot_optionalImportMessageExtension] + d: 120 +} +21: 3 # [UnittestRoot_optionalNestedEnumExtension] +22: 6 # [UnittestRoot_optionalForeignEnumExtension] +23: 9 # [UnittestRoot_optionalImportEnumExtension] +24: "124" # [UnittestRoot_optionalStringPieceExtension] +25: "125" # [UnittestRoot_optionalCordExtension] +# [UnittestRoot_repeatedInt32Extension] +31: 201 +31: 301 +# [UnittestRoot_repeatedInt64Extension] +32: 202 +32: 302 +# [UnittestRoot_repeatedUint32Extension] +33: 203 +33: 303 +# [UnittestRoot_repeatedUint64Extension] +34: 204 +34: 304 +# [UnittestRoot_repeatedSint32Extension] +35: 205 +35: 305 +# [UnittestRoot_repeatedSint64Extension] +36: 206 +36: 306 +# [UnittestRoot_repeatedFixed32Extension] +37: 207 +37: 307 +# [UnittestRoot_repeatedFixed64Extension] +38: 208 +38: 308 +# [UnittestRoot_repeatedSfixed32Extension] +39: 209 +39: 309 +# [UnittestRoot_repeatedSfixed64Extension] +40: 210 +40: 310 +# [UnittestRoot_repeatedFloatExtension] +41: 211 +41: 311 +# [UnittestRoot_repeatedDoubleExtension] +42: 212 +42: 312 +# [UnittestRoot_repeatedBoolExtension] +43: false +43: true +# [UnittestRoot_repeatedStringExtension] +44: "215" +44: "315" +# [UnittestRoot_repeatedBytesExtension] +45: "\330\000\000\000" +45: "<\001\000\000" +# [UnittestRoot_repeatedGroupExtension] +46 { + a: 217 +} +46 { + a: 317 +} +# [UnittestRoot_repeatedNestedMessageExtension] +48 { + bb: 218 +} +48 { + bb: 318 +} +# [UnittestRoot_repeatedForeignMessageExtension] +49 { + c: 219 +} +49 { + c: 319 +} +# [UnittestRoot_repeatedImportMessageExtension] +50 { + d: 220 +} +50 { + d: 320 +} +# [UnittestRoot_repeatedNestedEnumExtension] +51: 3 +51: 2 +# [UnittestRoot_repeatedForeignEnumExtension] +52: 6 +52: 5 +# [UnittestRoot_repeatedImportEnumExtension] +53: 9 +53: 8 +# [UnittestRoot_repeatedStringPieceExtension] +54: "224" +54: "324" +# [UnittestRoot_repeatedCordExtension] +55: "225" +55: "325" +61: 401 # [UnittestRoot_defaultInt32Extension] +62: 402 # [UnittestRoot_defaultInt64Extension] +63: 403 # [UnittestRoot_defaultUint32Extension] +64: 404 # [UnittestRoot_defaultUint64Extension] +65: 405 # [UnittestRoot_defaultSint32Extension] +66: 406 # [UnittestRoot_defaultSint64Extension] +67: 407 # [UnittestRoot_defaultFixed32Extension] +68: 408 # [UnittestRoot_defaultFixed64Extension] +69: 409 # [UnittestRoot_defaultSfixed32Extension] +70: 410 # [UnittestRoot_defaultSfixed64Extension] +71: 411 # [UnittestRoot_defaultFloatExtension] +72: 412 # [UnittestRoot_defaultDoubleExtension] +73: false # [UnittestRoot_defaultBoolExtension] +74: "415" # [UnittestRoot_defaultStringExtension] +75: "\240\001\000\000" # [UnittestRoot_defaultBytesExtension] +81: 1 # [UnittestRoot_defaultNestedEnumExtension] +82: 4 # [UnittestRoot_defaultForeignEnumExtension] +83: 7 # [UnittestRoot_defaultImportEnumExtension] +84: "424" # [UnittestRoot_defaultStringPieceExtension] +85: "425" # [UnittestRoot_defaultCordExtension] diff --git a/objectivec/Tests/unittest_objc.proto b/objectivec/Tests/unittest_objc.proto index b0eb47237e..91c213921e 100644 --- a/objectivec/Tests/unittest_objc.proto +++ b/objectivec/Tests/unittest_objc.proto @@ -103,37 +103,89 @@ message self { } // Singular - optional bool id = 1; - optional bool _cmd = 2; - optional bool in = 3; - optional bool out = 4; - optional bool inout = 5; - optional bool bycopy = 6; - optional bool byref = 7; - optional bool oneway = 8; - optional bool dealloc = 9; - optional bool zone = 10; - optional bool isProxy = 11; - optional bool copy = 12; - optional bool readonly = 13; - optional bool default = 14; - optional bool assign = 15; - optional bool getter = 16; - optional bool setter = 17; - optional bool weak = 18; - optional bool public = 19; - optional bool case = 20; - - optional autorelease SubEnum = 25; - - optional group New = 50 { - optional string copy = 51; - } - optional group MutableCopy = 60 { - optional int32 extensionRegistry = 61; + // Objective C Keywords + optional bool id = 1; + optional bool _cmd = 2; + // super is used as submessage above + optional bool in = 4; + optional bool out = 5; + optional bool inout = 6; + optional bool bycopy = 7; + optional bool byref = 8; + optional bool oneway = 9; + optional bool self = 10; + optional bool instancetype = 11; + optional bool nullable = 12; + optional bool nonnull = 13; + optional bool nil = 14; + // Nil and nil can't be in the same message + optional bool YES = 16; + optional bool NO = 17; + optional bool weak = 18; + + // Some C/C++ Keywords + optional bool case = 30; + optional bool if = 31; + optional bool and_eq = 32; + optional bool public = 33; + optional bool private = 34; + optional bool typename = 35; + optional bool static_cast = 36; + optional bool typeof = 37; + optional bool restrict = 38; + optional bool NULL = 39; + + // Some NSObject Methods + optional bool dealloc = 110; + optional bool isProxy = 111; + optional bool copy = 112; + optional bool description = 113; + optional bool zone = 114; + optional bool className = 115; + optional bool __retain_OA = 116; + optional bool CAMLType = 117; + optional bool isNSDictionary__ = 118; + optional bool accessibilityLabel = 119; + + // Some Objc "keywords" that we shouldn't + // have to worry about because they + // can only appear in specialized areas. + optional bool assign = 200; + optional bool getter = 201; + optional bool setter = 202; + optional bool atomic = 203; + optional bool nonatomic = 204; + optional bool strong = 205; + optional bool null_resettable = 206; + optional bool readonly = 207; + + // Some GPBMessage methods + optional bool clear = 300; + optional bool data = 301; + optional bool descriptor = 302; + optional bool delimitedData = 303; + + // Some MacTypes + optional bool Fixed = 400; + optional bool Point = 401; + optional bool FixedPoint = 402; + optional bool Style = 403; + + // C/C++ reserved identifiers + optional bool _Generic = 500; + optional bool __block = 501; + + // Try a keyword as a type + optional autorelease SubEnum = 1000; + + optional group New = 2000 { + optional string copy = 1; + } + optional group MutableCopy = 2001 { + optional int32 extensionRegistry = 1; } - extensions 90 to 94; + extensions 3000 to 3999; } @@ -177,11 +229,11 @@ enum Time { } extend self { - repeated int32 debugDescription = 90 [packed = true]; - repeated int64 finalize = 91 [packed = true]; - repeated uint32 hash = 92 [packed = true]; - repeated uint64 classForCoder = 93 [packed = true]; - repeated sint32 byref = 94 [packed = true]; + repeated int32 debugDescription = 3000 [packed = true]; + repeated int64 finalize = 3001 [packed = true]; + repeated uint32 hash = 3002 [packed = true]; + repeated uint64 classForCoder = 3003 [packed = true]; + repeated sint32 byref = 3004 [packed = true]; } // Test handing of fields that start with init* since Xcode 5's ARC support @@ -357,6 +409,335 @@ message ObjCRetainedFoo { repeated Foo mutableCopy_Val_upper_enum_repeated = 724; repeated Foo mutableCopyvalue_lower_no_underscore_enum_repeated = 725; repeated Foo mutableCopyValue_upper_no_underscore_enum_repeated = 726; + + extensions 1000 to 3999; +} + +// Extension fields with retained names. +extend ObjCRetainedFoo { + optional string new_val_lower_complex = 1011; + optional string new_Val_upper_complex = 1012; + optional string newvalue_lower_no_underscore_complex = 1013; + optional string newValue_upper_no_underscore_complex = 1014; + + optional int32 new_val_lower_primitive = 1015; + optional int32 new_Val_upper_primitive = 1016; + optional int32 newvalue_lower_no_underscore_primitive = 1017; + optional int32 newValue_upper_no_underscore_primitive = 1018; + + optional self new_val_lower_message = 1019; + optional self new_Val_upper_message = 1020; + optional self newvalue_lower_no_underscore_message = 1021; + optional self newValue_upper_no_underscore_message = 1022; + + optional Foo new_val_lower_enum = 1023; + optional Foo new_Val_upper_enum = 1024; + optional Foo newvalue_lower_no_underscore_enum = 1025; + optional Foo newValue_upper_no_underscore_enum = 1026; + + repeated string new_val_lower_complex_repeated = 1111; + repeated string new_Val_upper_complex_repeated = 1112; + repeated string newvalue_lower_no_underscore_complex_repeated = 1113; + repeated string newValue_upper_no_underscore_complex_repeated = 1114; + + repeated int32 new_val_lower_primitive_repeated = 1115; + repeated int32 new_Val_upper_primitive_repeated = 1116; + repeated int32 newvalue_lower_no_underscore_primitive_repeated = 1117; + repeated int32 newValue_upper_no_underscore_primitive_repeated = 1118; + + repeated self new_val_lower_message_repeated = 1119; + repeated self new_Val_upper_message_repeated = 1120; + repeated self newvalue_lower_no_underscore_message_repeated = 1121; + repeated self newValue_upper_no_underscore_message_repeated = 1122; + + repeated Foo new_val_lower_enum_repeated = 1123; + repeated Foo new_Val_upper_enum_repeated = 1124; + repeated Foo newvalue_lower_no_underscore_enum_repeated = 1125; + repeated Foo newValue_upper_no_underscore_enum_repeated = 1126; + + optional string alloc_val_lower_complex = 1211; + optional string alloc_Val_upper_complex = 1212; + optional string allocvalue_lower_no_underscore_complex = 1213; + optional string allocValue_upper_no_underscore_complex = 1214; + + optional int32 alloc_val_lower_primitive = 1215; + optional int32 alloc_Val_upper_primitive = 1216; + optional int32 allocvalue_lower_no_underscore_primitive = 1217; + optional int32 allocValue_upper_no_underscore_primitive = 1218; + + optional self alloc_val_lower_message = 1219; + optional self alloc_Val_upper_message = 1220; + optional self allocvalue_lower_no_underscore_message = 1221; + optional self allocValue_upper_no_underscore_message = 1222; + + optional Foo alloc_val_lower_enum = 1223; + optional Foo alloc_Val_upper_enum = 1224; + optional Foo allocvalue_lower_no_underscore_enum = 1225; + optional Foo allocValue_upper_no_underscore_enum = 1226; + + repeated string alloc_val_lower_complex_repeated = 1311; + repeated string alloc_Val_upper_complex_repeated = 1312; + repeated string allocvalue_lower_no_underscore_complex_repeated = 1313; + repeated string allocValue_upper_no_underscore_complex_repeated = 1314; + + repeated int32 alloc_val_lower_primitive_repeated = 1315; + repeated int32 alloc_Val_upper_primitive_repeated = 1316; + repeated int32 allocvalue_lower_no_underscore_primitive_repeated = 1317; + repeated int32 allocValue_upper_no_underscore_primitive_repeated = 1318; + + repeated self alloc_val_lower_message_repeated = 1319; + repeated self alloc_Val_upper_message_repeated = 1320; + repeated self allocvalue_lower_no_underscore_message_repeated = 1321; + repeated self allocValue_upper_no_underscore_message_repeated = 1322; + + repeated Foo alloc_val_lower_enum_repeated = 1323; + repeated Foo alloc_Val_upper_enum_repeated = 1324; + repeated Foo allocvalue_lower_no_underscore_enum_repeated = 1325; + repeated Foo allocValue_upper_no_underscore_enum_repeated = 1326; + + optional string copy_val_lower_complex = 1411; + optional string copy_Val_upper_complex = 1412; + optional string copyvalue_lower_no_underscore_complex = 1413; + optional string copyValue_upper_no_underscore_complex = 1414; + + optional int32 copy_val_lower_primitive = 1415; + optional int32 copy_Val_upper_primitive = 1416; + optional int32 copyvalue_lower_no_underscore_primitive = 1417; + optional int32 copyValue_upper_no_underscore_primitive = 1418; + + optional self copy_val_lower_message = 1419; + optional self copy_Val_upper_message = 1420; + optional self copyvalue_lower_no_underscore_message = 1421; + optional self copyValue_upper_no_underscore_message = 1422; + + optional Foo copy_val_lower_enum = 1423; + optional Foo copy_Val_upper_enum = 1424; + optional Foo copyvalue_lower_no_underscore_enum = 1425; + optional Foo copyValue_upper_no_underscore_enum = 1426; + + repeated string copy_val_lower_complex_repeated = 1511; + repeated string copy_Val_upper_complex_repeated = 1512; + repeated string copyvalue_lower_no_underscore_complex_repeated = 1513; + repeated string copyValue_upper_no_underscore_complex_repeated = 1514; + + repeated int32 copy_val_lower_primitive_repeated = 1515; + repeated int32 copy_Val_upper_primitive_repeated = 1516; + repeated int32 copyvalue_lower_no_underscore_primitive_repeated = 1517; + repeated int32 copyValue_upper_no_underscore_primitive_repeated = 1518; + + repeated self copy_val_lower_message_repeated = 1519; + repeated self copy_Val_upper_message_repeated = 1520; + repeated self copyvalue_lower_no_underscore_message_repeated = 1521; + repeated self copyValue_upper_no_underscore_message_repeated = 1522; + + repeated Foo copy_val_lower_enum_repeated = 1523; + repeated Foo copy_Val_upper_enum_repeated = 1524; + repeated Foo copyvalue_lower_no_underscore_enum_repeated = 1525; + repeated Foo copyValue_upper_no_underscore_enum_repeated = 1526; + + optional string mutableCopy_val_lower_complex = 1611; + optional string mutableCopy_Val_upper_complex = 1612; + optional string mutableCopyvalue_lower_no_underscore_complex = 1613; + optional string mutableCopyValue_upper_no_underscore_complex = 1614; + + optional int32 mutableCopy_val_lower_primitive = 1615; + optional int32 mutableCopy_Val_upper_primitive = 1616; + optional int32 mutableCopyvalue_lower_no_underscore_primitive = 1617; + optional int32 mutableCopyValue_upper_no_underscore_primitive = 1618; + + optional self mutableCopy_val_lower_message = 1619; + optional self mutableCopy_Val_upper_message = 1620; + optional self mutableCopyvalue_lower_no_underscore_message = 1621; + optional self mutableCopyValue_upper_no_underscore_message = 1622; + + optional Foo mutableCopy_val_lower_enum = 1623; + optional Foo mutableCopy_Val_upper_enum = 1624; + optional Foo mutableCopyvalue_lower_no_underscore_enum = 1625; + optional Foo mutableCopyValue_upper_no_underscore_enum = 1626; + + repeated string mutableCopy_val_lower_complex_repeated = 1711; + repeated string mutableCopy_Val_upper_complex_repeated = 1712; + repeated string mutableCopyvalue_lower_no_underscore_complex_repeated = 1713; + repeated string mutableCopyValue_upper_no_underscore_complex_repeated = 1714; + + repeated int32 mutableCopy_val_lower_primitive_repeated = 1715; + repeated int32 mutableCopy_Val_upper_primitive_repeated = 1716; + repeated int32 mutableCopyvalue_lower_no_underscore_primitive_repeated = 1717; + repeated int32 mutableCopyValue_upper_no_underscore_primitive_repeated = 1718; + + repeated self mutableCopy_val_lower_message_repeated = 1719; + repeated self mutableCopy_Val_upper_message_repeated = 1720; + repeated self mutableCopyvalue_lower_no_underscore_message_repeated = 1721; + repeated self mutableCopyValue_upper_no_underscore_message_repeated = 1722; + + repeated Foo mutableCopy_val_lower_enum_repeated = 1723; + repeated Foo mutableCopy_Val_upper_enum_repeated = 1724; + repeated Foo mutableCopyvalue_lower_no_underscore_enum_repeated = 1725; + repeated Foo mutableCopyValue_upper_no_underscore_enum_repeated = 1726; +} + +message JustToScopeExtensions { + extend ObjCRetainedFoo { + optional string new_val_lower_complex = 2011; + optional string new_Val_upper_complex = 2012; + optional string newvalue_lower_no_underscore_complex = 2013; + optional string newValue_upper_no_underscore_complex = 2014; + + optional int32 new_val_lower_primitive = 2015; + optional int32 new_Val_upper_primitive = 2016; + optional int32 newvalue_lower_no_underscore_primitive = 2017; + optional int32 newValue_upper_no_underscore_primitive = 2018; + + optional self new_val_lower_message = 2019; + optional self new_Val_upper_message = 2020; + optional self newvalue_lower_no_underscore_message = 2021; + optional self newValue_upper_no_underscore_message = 2022; + + optional Foo new_val_lower_enum = 2023; + optional Foo new_Val_upper_enum = 2024; + optional Foo newvalue_lower_no_underscore_enum = 2025; + optional Foo newValue_upper_no_underscore_enum = 2026; + + repeated string new_val_lower_complex_repeated = 2111; + repeated string new_Val_upper_complex_repeated = 2112; + repeated string newvalue_lower_no_underscore_complex_repeated = 2113; + repeated string newValue_upper_no_underscore_complex_repeated = 2114; + + repeated int32 new_val_lower_primitive_repeated = 2115; + repeated int32 new_Val_upper_primitive_repeated = 2116; + repeated int32 newvalue_lower_no_underscore_primitive_repeated = 2117; + repeated int32 newValue_upper_no_underscore_primitive_repeated = 2118; + + repeated self new_val_lower_message_repeated = 2119; + repeated self new_Val_upper_message_repeated = 2120; + repeated self newvalue_lower_no_underscore_message_repeated = 2121; + repeated self newValue_upper_no_underscore_message_repeated = 2122; + + repeated Foo new_val_lower_enum_repeated = 2123; + repeated Foo new_Val_upper_enum_repeated = 2124; + repeated Foo newvalue_lower_no_underscore_enum_repeated = 2125; + repeated Foo newValue_upper_no_underscore_enum_repeated = 2126; + + optional string alloc_val_lower_complex = 2211; + optional string alloc_Val_upper_complex = 2212; + optional string allocvalue_lower_no_underscore_complex = 2213; + optional string allocValue_upper_no_underscore_complex = 2214; + + optional int32 alloc_val_lower_primitive = 2215; + optional int32 alloc_Val_upper_primitive = 2216; + optional int32 allocvalue_lower_no_underscore_primitive = 2217; + optional int32 allocValue_upper_no_underscore_primitive = 2218; + + optional self alloc_val_lower_message = 2219; + optional self alloc_Val_upper_message = 2220; + optional self allocvalue_lower_no_underscore_message = 2221; + optional self allocValue_upper_no_underscore_message = 2222; + + optional Foo alloc_val_lower_enum = 2223; + optional Foo alloc_Val_upper_enum = 2224; + optional Foo allocvalue_lower_no_underscore_enum = 2225; + optional Foo allocValue_upper_no_underscore_enum = 2226; + + repeated string alloc_val_lower_complex_repeated = 2311; + repeated string alloc_Val_upper_complex_repeated = 2312; + repeated string allocvalue_lower_no_underscore_complex_repeated = 2313; + repeated string allocValue_upper_no_underscore_complex_repeated = 2314; + + repeated int32 alloc_val_lower_primitive_repeated = 2315; + repeated int32 alloc_Val_upper_primitive_repeated = 2316; + repeated int32 allocvalue_lower_no_underscore_primitive_repeated = 2317; + repeated int32 allocValue_upper_no_underscore_primitive_repeated = 2318; + + repeated self alloc_val_lower_message_repeated = 2319; + repeated self alloc_Val_upper_message_repeated = 2320; + repeated self allocvalue_lower_no_underscore_message_repeated = 2321; + repeated self allocValue_upper_no_underscore_message_repeated = 2322; + + repeated Foo alloc_val_lower_enum_repeated = 2323; + repeated Foo alloc_Val_upper_enum_repeated = 2324; + repeated Foo allocvalue_lower_no_underscore_enum_repeated = 2325; + repeated Foo allocValue_upper_no_underscore_enum_repeated = 2326; + + optional string copy_val_lower_complex = 2411; + optional string copy_Val_upper_complex = 2412; + optional string copyvalue_lower_no_underscore_complex = 2413; + optional string copyValue_upper_no_underscore_complex = 2414; + + optional int32 copy_val_lower_primitive = 2415; + optional int32 copy_Val_upper_primitive = 2416; + optional int32 copyvalue_lower_no_underscore_primitive = 2417; + optional int32 copyValue_upper_no_underscore_primitive = 2418; + + optional self copy_val_lower_message = 2419; + optional self copy_Val_upper_message = 2420; + optional self copyvalue_lower_no_underscore_message = 2421; + optional self copyValue_upper_no_underscore_message = 2422; + + optional Foo copy_val_lower_enum = 2423; + optional Foo copy_Val_upper_enum = 2424; + optional Foo copyvalue_lower_no_underscore_enum = 2425; + optional Foo copyValue_upper_no_underscore_enum = 2426; + + repeated string copy_val_lower_complex_repeated = 2511; + repeated string copy_Val_upper_complex_repeated = 2512; + repeated string copyvalue_lower_no_underscore_complex_repeated = 2513; + repeated string copyValue_upper_no_underscore_complex_repeated = 2514; + + repeated int32 copy_val_lower_primitive_repeated = 2515; + repeated int32 copy_Val_upper_primitive_repeated = 2516; + repeated int32 copyvalue_lower_no_underscore_primitive_repeated = 2517; + repeated int32 copyValue_upper_no_underscore_primitive_repeated = 2518; + + repeated self copy_val_lower_message_repeated = 2519; + repeated self copy_Val_upper_message_repeated = 2520; + repeated self copyvalue_lower_no_underscore_message_repeated = 2521; + repeated self copyValue_upper_no_underscore_message_repeated = 2522; + + repeated Foo copy_val_lower_enum_repeated = 2523; + repeated Foo copy_Val_upper_enum_repeated = 2524; + repeated Foo copyvalue_lower_no_underscore_enum_repeated = 2525; + repeated Foo copyValue_upper_no_underscore_enum_repeated = 2526; + + optional string mutableCopy_val_lower_complex = 2611; + optional string mutableCopy_Val_upper_complex = 2612; + optional string mutableCopyvalue_lower_no_underscore_complex = 2613; + optional string mutableCopyValue_upper_no_underscore_complex = 2614; + + optional int32 mutableCopy_val_lower_primitive = 2615; + optional int32 mutableCopy_Val_upper_primitive = 2616; + optional int32 mutableCopyvalue_lower_no_underscore_primitive = 2617; + optional int32 mutableCopyValue_upper_no_underscore_primitive = 2618; + + optional self mutableCopy_val_lower_message = 2619; + optional self mutableCopy_Val_upper_message = 2620; + optional self mutableCopyvalue_lower_no_underscore_message = 2621; + optional self mutableCopyValue_upper_no_underscore_message = 2622; + + optional Foo mutableCopy_val_lower_enum = 2623; + optional Foo mutableCopy_Val_upper_enum = 2624; + optional Foo mutableCopyvalue_lower_no_underscore_enum = 2625; + optional Foo mutableCopyValue_upper_no_underscore_enum = 2626; + + repeated string mutableCopy_val_lower_complex_repeated = 2711; + repeated string mutableCopy_Val_upper_complex_repeated = 2712; + repeated string mutableCopyvalue_lower_no_underscore_complex_repeated = 2713; + repeated string mutableCopyValue_upper_no_underscore_complex_repeated = 2714; + + repeated int32 mutableCopy_val_lower_primitive_repeated = 2715; + repeated int32 mutableCopy_Val_upper_primitive_repeated = 2716; + repeated int32 mutableCopyvalue_lower_no_underscore_primitive_repeated = 2717; + repeated int32 mutableCopyValue_upper_no_underscore_primitive_repeated = 2718; + + repeated self mutableCopy_val_lower_message_repeated = 2719; + repeated self mutableCopy_Val_upper_message_repeated = 2720; + repeated self mutableCopyvalue_lower_no_underscore_message_repeated = 2721; + repeated self mutableCopyValue_upper_no_underscore_message_repeated = 2722; + + repeated Foo mutableCopy_val_lower_enum_repeated = 2723; + repeated Foo mutableCopy_Val_upper_enum_repeated = 2724; + repeated Foo mutableCopyvalue_lower_no_underscore_enum_repeated = 2725; + repeated Foo mutableCopyValue_upper_no_underscore_enum_repeated = 2726; + } } // Test handling of fields that are the retained names. @@ -433,7 +814,7 @@ message EnumTestMsg { repeated MyEnum mumble = 4; } -// Test case for https://github.com/google/protobuf/issues/1453 +// Test case for https://github.com/protocolbuffers/protobuf/issues/1453 // Message with no explicit defaults, but a non zero default for an enum. message MessageWithOneBasedEnum { enum OneBasedEnum { @@ -486,3 +867,18 @@ message BoolOnlyMessage { message WKTRefereceMessage { optional google.protobuf.Any an_any = 1; } + +// This is in part a compile test, it ensures that when aliases end up with +// the same ObjC name, we drop them to avoid the duplication names. There +// is a test to ensure the descriptors are still generated to support +// reflection and TextFormat. +enum TestEnumObjCNameCollision { + option allow_alias = true; + + FOO = 1; + foo = 1; + + BAR = 2; + mumble = 2; + MUMBLE = 2; +} diff --git a/objectivec/Tests/unittest_objc_options.proto b/objectivec/Tests/unittest_objc_options.proto new file mode 100644 index 0000000000..6129b854e0 --- /dev/null +++ b/objectivec/Tests/unittest_objc_options.proto @@ -0,0 +1,61 @@ +// 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. + +syntax = "proto2"; + +package protobuf_objc_unittest; +option objc_class_prefix = "GPBTEST"; + +// Verify that enum types and values get the prefix. +message TestObjcProtoPrefixMessage { +} + +// Verify that messages that don't already have the prefix get a prefix. +enum TestObjcProtoPrefixEnum { + value = 1; +} + +// Verify that messages that already have a prefix aren't prefixed twice. +message GPBTESTTestHasAPrefixMessage { +} + +// Verify that enums that already have a prefix aren't prefixed twice. +enum GPBTESTTestHasAPrefixEnum { + valueB = 1; +} + +// Verify that classes that have the prefix followed by a lowercase +// letter DO get the prefix. +message GPBTESTshouldGetAPrefixMessage { +} + +// Verify that classes named the same as prefixes are prefixed. +message GPBTEST { +} diff --git a/objectivec/google/protobuf/Any.pbobjc.h b/objectivec/google/protobuf/Any.pbobjc.h index ad261898e4..2091d72df4 100644 --- a/objectivec/google/protobuf/Any.pbobjc.h +++ b/objectivec/google/protobuf/Any.pbobjc.h @@ -140,7 +140,8 @@ typedef GPB_ENUM(GPBAny_FieldNumber) { /** * A URL/resource name that uniquely identifies the type of the serialized - * protocol buffer message. The last segment of the URL's path must represent + * protocol buffer message. This string must contain at least + * one "/" character. The last segment of the URL's path must represent * the fully qualified name of the type (as in * `path/google.protobuf.Duration`). The name should be in a canonical form * (e.g., leading "." is not accepted). diff --git a/objectivec/google/protobuf/Any.pbobjc.m b/objectivec/google/protobuf/Any.pbobjc.m index d210643f05..7ca84ff40a 100644 --- a/objectivec/google/protobuf/Any.pbobjc.m +++ b/objectivec/google/protobuf/Any.pbobjc.m @@ -98,7 +98,9 @@ typedef struct GPBAny__storage_ { "\001\001\004\241!!\000"; [localDescriptor setupExtraTextInfo:extraTextFormatInfo]; #endif // !GPBOBJC_SKIP_MESSAGE_TEXTFORMAT_EXTRAS - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/Api.pbobjc.m b/objectivec/google/protobuf/Api.pbobjc.m index 58b47157de..d63a3e696a 100644 --- a/objectivec/google/protobuf/Api.pbobjc.m +++ b/objectivec/google/protobuf/Api.pbobjc.m @@ -152,7 +152,9 @@ typedef struct GPBApi__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBApi__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -276,7 +278,9 @@ typedef struct GPBMethod__storage_ { "\002\002\007\244\241!!\000\004\010\244\241!!\000"; [localDescriptor setupExtraTextInfo:extraTextFormatInfo]; #endif // !GPBOBJC_SKIP_MESSAGE_TEXTFORMAT_EXTRAS - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -342,7 +346,9 @@ typedef struct GPBMixin__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBMixin__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/Duration.pbobjc.m b/objectivec/google/protobuf/Duration.pbobjc.m index bafb64a04f..3fa702435f 100644 --- a/objectivec/google/protobuf/Duration.pbobjc.m +++ b/objectivec/google/protobuf/Duration.pbobjc.m @@ -93,7 +93,9 @@ typedef struct GPBDuration__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBDuration__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/Empty.pbobjc.m b/objectivec/google/protobuf/Empty.pbobjc.m index 506b500e04..51f15f365c 100644 --- a/objectivec/google/protobuf/Empty.pbobjc.m +++ b/objectivec/google/protobuf/Empty.pbobjc.m @@ -69,7 +69,9 @@ typedef struct GPBEmpty__storage_ { fieldCount:0 storageSize:sizeof(GPBEmpty__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/FieldMask.pbobjc.m b/objectivec/google/protobuf/FieldMask.pbobjc.m index b0915af450..ff27318a21 100644 --- a/objectivec/google/protobuf/FieldMask.pbobjc.m +++ b/objectivec/google/protobuf/FieldMask.pbobjc.m @@ -82,7 +82,9 @@ typedef struct GPBFieldMask__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBFieldMask__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/SourceContext.pbobjc.m b/objectivec/google/protobuf/SourceContext.pbobjc.m index 83bfa3460b..c93871ed3e 100644 --- a/objectivec/google/protobuf/SourceContext.pbobjc.m +++ b/objectivec/google/protobuf/SourceContext.pbobjc.m @@ -82,7 +82,9 @@ typedef struct GPBSourceContext__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBSourceContext__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/Struct.pbobjc.m b/objectivec/google/protobuf/Struct.pbobjc.m index 816fd6df56..169a275813 100644 --- a/objectivec/google/protobuf/Struct.pbobjc.m +++ b/objectivec/google/protobuf/Struct.pbobjc.m @@ -118,7 +118,9 @@ typedef struct GPBStruct__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBStruct__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -222,7 +224,9 @@ typedef struct GPBValue__storage_ { [localDescriptor setupOneofs:oneofs count:(uint32_t)(sizeof(oneofs) / sizeof(char*)) firstHasIndex:-1]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -282,7 +286,9 @@ typedef struct GPBListValue__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBListValue__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/Timestamp.pbobjc.h b/objectivec/google/protobuf/Timestamp.pbobjc.h index 2c4b8b2050..591dbe386d 100644 --- a/objectivec/google/protobuf/Timestamp.pbobjc.h +++ b/objectivec/google/protobuf/Timestamp.pbobjc.h @@ -56,17 +56,19 @@ typedef GPB_ENUM(GPBTimestamp_FieldNumber) { }; /** - * A Timestamp represents a point in time independent of any time zone - * or calendar, represented as seconds and fractions of seconds at - * nanosecond resolution in UTC Epoch time. It is encoded using the - * Proleptic Gregorian Calendar which extends the Gregorian calendar - * backwards to year one. It is encoded assuming all minutes are 60 - * seconds long, i.e. leap seconds are "smeared" so that no leap second - * table is needed for interpretation. Range is from - * 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. - * By restricting to that range, we ensure that we can convert to - * and from RFC 3339 date strings. - * See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt). + * A Timestamp represents a point in time independent of any time zone or local + * calendar, encoded as a count of seconds and fractions of seconds at + * nanosecond resolution. The count is relative to an epoch at UTC midnight on + * January 1, 1970, in the proleptic Gregorian calendar which extends the + * Gregorian calendar backwards to year one. + * + * All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap + * second table is needed for interpretation, using a [24-hour linear + * smear](https://developers.google.com/time/smear). + * + * The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By + * restricting to that range, we ensure that we can convert to and from [RFC + * 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings. * * # Examples * @@ -127,12 +129,14 @@ typedef GPB_ENUM(GPBTimestamp_FieldNumber) { * 01:30 UTC on January 15, 2017. * * In JavaScript, one can convert a Date object to this format using the - * standard [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString] + * standard + * [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) * method. In Python, a standard `datetime.datetime` object can be converted - * to this format using [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) - * with the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one - * can use the Joda Time's [`ISODateTimeFormat.dateTime()`]( - * http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime-- + * to this format using + * [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with + * the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use + * the Joda Time's [`ISODateTimeFormat.dateTime()`]( + * http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D * ) to obtain a formatter capable of generating timestamps in this format. **/ @interface GPBTimestamp : GPBMessage diff --git a/objectivec/google/protobuf/Timestamp.pbobjc.m b/objectivec/google/protobuf/Timestamp.pbobjc.m index 4ab159fb9c..b6393e37aa 100644 --- a/objectivec/google/protobuf/Timestamp.pbobjc.m +++ b/objectivec/google/protobuf/Timestamp.pbobjc.m @@ -93,7 +93,9 @@ typedef struct GPBTimestamp__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBTimestamp__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/Type.pbobjc.m b/objectivec/google/protobuf/Type.pbobjc.m index bb64d87649..6b88a4b5c6 100644 --- a/objectivec/google/protobuf/Type.pbobjc.m +++ b/objectivec/google/protobuf/Type.pbobjc.m @@ -178,7 +178,9 @@ typedef struct GPBType__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBType__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -336,7 +338,9 @@ typedef struct GPBField__storage_ { "\001\006\004\241!!\000"; [localDescriptor setupExtraTextInfo:extraTextFormatInfo]; #endif // !GPBOBJC_SKIP_MESSAGE_TEXTFORMAT_EXTRAS - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -561,7 +565,9 @@ typedef struct GPBEnum__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBEnum__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -638,7 +644,9 @@ typedef struct GPBEnumValue__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBEnumValue__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -692,7 +700,9 @@ typedef struct GPBOption__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBOption__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/objectivec/google/protobuf/Wrappers.pbobjc.m b/objectivec/google/protobuf/Wrappers.pbobjc.m index 5479eb127b..0a163fa8ff 100644 --- a/objectivec/google/protobuf/Wrappers.pbobjc.m +++ b/objectivec/google/protobuf/Wrappers.pbobjc.m @@ -82,7 +82,9 @@ typedef struct GPBDoubleValue__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBDoubleValue__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -125,7 +127,9 @@ typedef struct GPBFloatValue__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBFloatValue__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -168,7 +172,9 @@ typedef struct GPBInt64Value__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBInt64Value__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -211,7 +217,9 @@ typedef struct GPBUInt64Value__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBUInt64Value__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -254,7 +262,9 @@ typedef struct GPBInt32Value__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBInt32Value__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -297,7 +307,9 @@ typedef struct GPBUInt32Value__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBUInt32Value__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -339,7 +351,9 @@ typedef struct GPBBoolValue__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBBoolValue__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -382,7 +396,9 @@ typedef struct GPBStringValue__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBStringValue__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; @@ -425,7 +441,9 @@ typedef struct GPBBytesValue__storage_ { fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription)) storageSize:sizeof(GPBBytesValue__storage_) flags:GPBDescriptorInitializationFlag_None]; - NSAssert(descriptor == nil, @"Startup recursed!"); + #if defined(DEBUG) && DEBUG + NSAssert(descriptor == nil, @"Startup recursed!"); + #endif // DEBUG descriptor = localDescriptor; } return descriptor; diff --git a/php/composer.json b/php/composer.json index 20f6a45eff..b47065b78b 100644 --- a/php/composer.json +++ b/php/composer.json @@ -23,6 +23,6 @@ } }, "scripts": { - "test": "(cd tests && rm -rf generated && mkdir -p generated && ../../src/protoc --php_out=generated proto/empty/echo.proto proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto proto/test_php_namespace.proto proto/test_empty_php_namespace.proto proto/test_reserved_enum_lower.proto proto/test_reserved_enum_upper.proto proto/test_reserved_enum_value_lower.proto proto/test_reserved_enum_value_upper.proto proto/test_reserved_message_lower.proto proto/test_reserved_message_upper.proto proto/test_service.proto proto/test_service_namespace.proto proto/test_descriptors.proto) && (cd ../src && ./protoc --php_out=../php/tests/generated -I../php/tests -I. ../php/tests/proto/test_import_descriptor_proto.proto) && vendor/bin/phpunit" + "test": "(cd tests && rm -rf generated && mkdir -p generated && ../../src/protoc --php_out=generated -I../../src -I. proto/empty/echo.proto proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto proto/test_php_namespace.proto proto/test_empty_php_namespace.proto proto/test_reserved_enum_lower.proto proto/test_reserved_enum_upper.proto proto/test_reserved_enum_value_lower.proto proto/test_reserved_enum_value_upper.proto proto/test_reserved_message_lower.proto proto/test_reserved_message_upper.proto proto/test_service.proto proto/test_service_namespace.proto proto/test_wrapper_type_setters.proto proto/test_descriptors.proto) && (cd ../src && ./protoc --php_out=../php/tests/generated -I../php/tests -I. ../php/tests/proto/test_import_descriptor_proto.proto) && vendor/bin/phpunit" } } diff --git a/php/ext/google/protobuf/builtin_descriptors.inc b/php/ext/google/protobuf/builtin_descriptors.inc new file mode 100644 index 0000000000..1bb5dbfa85 --- /dev/null +++ b/php/ext/google/protobuf/builtin_descriptors.inc @@ -0,0 +1,635 @@ +unsigned char descriptor_proto[] = { + 0x0a, 0x9b, 0x3b, 0x0a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, + 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x12, 0x0f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x22, 0x4d, 0x0a, 0x11, 0x46, 0x69, + 0x6c, 0x65, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, + 0x53, 0x65, 0x74, 0x12, 0x38, 0x0a, 0x04, 0x66, 0x69, 0x6c, 0x65, 0x18, + 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x46, 0x69, 0x6c, 0x65, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, + 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, 0x04, 0x66, 0x69, 0x6c, + 0x65, 0x22, 0xe4, 0x04, 0x0a, 0x13, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, + 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, + 0x1e, 0x0a, 0x0a, 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x63, + 0x79, 0x18, 0x03, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x64, 0x65, 0x70, + 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x63, 0x79, 0x12, 0x2b, 0x0a, 0x11, 0x70, + 0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, + 0x65, 0x6e, 0x63, 0x79, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x05, 0x52, 0x10, + 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x44, 0x65, 0x70, 0x65, 0x6e, 0x64, + 0x65, 0x6e, 0x63, 0x79, 0x12, 0x27, 0x0a, 0x0f, 0x77, 0x65, 0x61, 0x6b, + 0x5f, 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x63, 0x79, 0x18, + 0x0b, 0x20, 0x03, 0x28, 0x05, 0x52, 0x0e, 0x77, 0x65, 0x61, 0x6b, 0x44, + 0x65, 0x70, 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x63, 0x79, 0x12, 0x43, 0x0a, + 0x0c, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x5f, 0x74, 0x79, 0x70, + 0x65, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x20, 0x2e, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, + 0x66, 0x2e, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, + 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, 0x0b, 0x6d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x41, 0x0a, 0x09, 0x65, 0x6e, + 0x75, 0x6d, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x05, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, + 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, + 0x6f, 0x74, 0x6f, 0x52, 0x08, 0x65, 0x6e, 0x75, 0x6d, 0x54, 0x79, 0x70, + 0x65, 0x12, 0x41, 0x0a, 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x18, 0x06, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x27, 0x2e, 0x67, 0x6f, 0x6f, + 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x44, 0x65, 0x73, 0x63, + 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, + 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x43, 0x0a, 0x09, + 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x07, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, + 0x65, 0x6c, 0x64, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, + 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, 0x09, 0x65, 0x78, 0x74, 0x65, + 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x36, 0x0a, 0x07, 0x6f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, + 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x4f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x73, 0x12, 0x49, 0x0a, 0x10, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x5f, + 0x63, 0x6f, 0x64, 0x65, 0x5f, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x09, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x1f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x6f, + 0x75, 0x72, 0x63, 0x65, 0x43, 0x6f, 0x64, 0x65, 0x49, 0x6e, 0x66, 0x6f, + 0x52, 0x0e, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x43, 0x6f, 0x64, 0x65, + 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6e, 0x74, + 0x61, 0x78, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x79, + 0x6e, 0x74, 0x61, 0x78, 0x22, 0xb9, 0x06, 0x0a, 0x0f, 0x44, 0x65, 0x73, + 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, + 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x3b, 0x0a, 0x05, + 0x66, 0x69, 0x65, 0x6c, 0x64, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x25, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x44, + 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, + 0x74, 0x6f, 0x52, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x43, 0x0a, + 0x09, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x06, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, + 0x69, 0x65, 0x6c, 0x64, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, + 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, 0x09, 0x65, 0x78, 0x74, + 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x41, 0x0a, 0x0b, 0x6e, 0x65, + 0x73, 0x74, 0x65, 0x64, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x20, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x52, 0x0a, 0x6e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x54, 0x79, 0x70, + 0x65, 0x12, 0x41, 0x0a, 0x09, 0x65, 0x6e, 0x75, 0x6d, 0x5f, 0x74, 0x79, + 0x70, 0x65, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, + 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x44, 0x65, 0x73, 0x63, 0x72, + 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, 0x08, + 0x65, 0x6e, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x12, 0x58, 0x0a, 0x0f, + 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x72, 0x61, + 0x6e, 0x67, 0x65, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2f, 0x2e, + 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x75, 0x66, 0x2e, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, + 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x45, 0x78, 0x74, 0x65, + 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, 0x0e, + 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x61, 0x6e, + 0x67, 0x65, 0x12, 0x44, 0x0a, 0x0a, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x5f, + 0x64, 0x65, 0x63, 0x6c, 0x18, 0x08, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x25, + 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4f, 0x6e, 0x65, 0x6f, 0x66, 0x44, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x52, 0x09, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x44, 0x65, 0x63, 0x6c, + 0x12, 0x39, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, + 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x73, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, + 0x55, 0x0a, 0x0e, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x5f, + 0x72, 0x61, 0x6e, 0x67, 0x65, 0x18, 0x09, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x2e, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, + 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, + 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, + 0x0d, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x52, 0x61, 0x6e, + 0x67, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, + 0x65, 0x64, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x0a, 0x20, 0x03, 0x28, + 0x09, 0x52, 0x0c, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x4e, + 0x61, 0x6d, 0x65, 0x1a, 0x7a, 0x0a, 0x0e, 0x45, 0x78, 0x74, 0x65, 0x6e, + 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x12, 0x14, 0x0a, + 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, + 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x65, + 0x6e, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x65, 0x6e, + 0x64, 0x12, 0x40, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x67, 0x6f, 0x6f, + 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x61, + 0x6e, 0x67, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, + 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x1a, 0x37, 0x0a, 0x0d, 0x52, + 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x52, 0x61, 0x6e, 0x67, 0x65, + 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x12, 0x10, + 0x0a, 0x03, 0x65, 0x6e, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x03, 0x65, 0x6e, 0x64, 0x22, 0x7c, 0x0a, 0x15, 0x45, 0x78, 0x74, 0x65, + 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x4f, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x58, 0x0a, 0x14, 0x75, 0x6e, 0x69, + 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x5f, 0x6f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0xe7, 0x07, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x55, 0x6e, 0x69, 0x6e, 0x74, + 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, + 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x2a, + 0x09, 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, 0x80, 0x80, 0x02, 0x22, 0x98, + 0x06, 0x0a, 0x14, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x44, 0x65, 0x73, 0x63, + 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, + 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x41, 0x0a, 0x05, 0x6c, + 0x61, 0x62, 0x65, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x2b, + 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x44, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x2e, 0x4c, 0x61, 0x62, 0x65, 0x6c, 0x52, 0x05, 0x6c, 0x61, 0x62, + 0x65, 0x6c, 0x12, 0x3e, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x0e, 0x32, 0x2a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, + 0x69, 0x65, 0x6c, 0x64, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, + 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x79, 0x70, 0x65, + 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x74, 0x79, + 0x70, 0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x08, 0x74, 0x79, 0x70, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12, + 0x1a, 0x0a, 0x08, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x65, 0x65, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x65, 0x78, 0x74, 0x65, 0x6e, + 0x64, 0x65, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x64, 0x65, 0x66, 0x61, 0x75, + 0x6c, 0x74, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x07, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x0c, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x56, + 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x6f, 0x6e, 0x65, 0x6f, + 0x66, 0x5f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x18, 0x09, 0x20, 0x01, 0x28, + 0x05, 0x52, 0x0a, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x49, 0x6e, 0x64, 0x65, + 0x78, 0x12, 0x1b, 0x0a, 0x09, 0x6a, 0x73, 0x6f, 0x6e, 0x5f, 0x6e, 0x61, + 0x6d, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6a, 0x73, + 0x6f, 0x6e, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x37, 0x0a, 0x07, 0x6f, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x22, 0xb6, 0x02, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, + 0x12, 0x0f, 0x0a, 0x0b, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x44, 0x4f, 0x55, + 0x42, 0x4c, 0x45, 0x10, 0x01, 0x12, 0x0e, 0x0a, 0x0a, 0x54, 0x59, 0x50, + 0x45, 0x5f, 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x10, 0x02, 0x12, 0x0e, 0x0a, + 0x0a, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x10, + 0x03, 0x12, 0x0f, 0x0a, 0x0b, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55, 0x49, + 0x4e, 0x54, 0x36, 0x34, 0x10, 0x04, 0x12, 0x0e, 0x0a, 0x0a, 0x54, 0x59, + 0x50, 0x45, 0x5f, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x10, 0x05, 0x12, 0x10, + 0x0a, 0x0c, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x46, 0x49, 0x58, 0x45, 0x44, + 0x36, 0x34, 0x10, 0x06, 0x12, 0x10, 0x0a, 0x0c, 0x54, 0x59, 0x50, 0x45, + 0x5f, 0x46, 0x49, 0x58, 0x45, 0x44, 0x33, 0x32, 0x10, 0x07, 0x12, 0x0d, + 0x0a, 0x09, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x42, 0x4f, 0x4f, 0x4c, 0x10, + 0x08, 0x12, 0x0f, 0x0a, 0x0b, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x53, 0x54, + 0x52, 0x49, 0x4e, 0x47, 0x10, 0x09, 0x12, 0x0e, 0x0a, 0x0a, 0x54, 0x59, + 0x50, 0x45, 0x5f, 0x47, 0x52, 0x4f, 0x55, 0x50, 0x10, 0x0a, 0x12, 0x10, + 0x0a, 0x0c, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x4d, 0x45, 0x53, 0x53, 0x41, + 0x47, 0x45, 0x10, 0x0b, 0x12, 0x0e, 0x0a, 0x0a, 0x54, 0x59, 0x50, 0x45, + 0x5f, 0x42, 0x59, 0x54, 0x45, 0x53, 0x10, 0x0c, 0x12, 0x0f, 0x0a, 0x0b, + 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x10, + 0x0d, 0x12, 0x0d, 0x0a, 0x09, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x45, 0x4e, + 0x55, 0x4d, 0x10, 0x0e, 0x12, 0x11, 0x0a, 0x0d, 0x54, 0x59, 0x50, 0x45, + 0x5f, 0x53, 0x46, 0x49, 0x58, 0x45, 0x44, 0x33, 0x32, 0x10, 0x0f, 0x12, + 0x11, 0x0a, 0x0d, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x53, 0x46, 0x49, 0x58, + 0x45, 0x44, 0x36, 0x34, 0x10, 0x10, 0x12, 0x0f, 0x0a, 0x0b, 0x54, 0x59, + 0x50, 0x45, 0x5f, 0x53, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x10, 0x11, 0x12, + 0x0f, 0x0a, 0x0b, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x53, 0x49, 0x4e, 0x54, + 0x36, 0x34, 0x10, 0x12, 0x22, 0x43, 0x0a, 0x05, 0x4c, 0x61, 0x62, 0x65, + 0x6c, 0x12, 0x12, 0x0a, 0x0e, 0x4c, 0x41, 0x42, 0x45, 0x4c, 0x5f, 0x4f, + 0x50, 0x54, 0x49, 0x4f, 0x4e, 0x41, 0x4c, 0x10, 0x01, 0x12, 0x12, 0x0a, + 0x0e, 0x4c, 0x41, 0x42, 0x45, 0x4c, 0x5f, 0x52, 0x45, 0x51, 0x55, 0x49, + 0x52, 0x45, 0x44, 0x10, 0x02, 0x12, 0x12, 0x0a, 0x0e, 0x4c, 0x41, 0x42, + 0x45, 0x4c, 0x5f, 0x52, 0x45, 0x50, 0x45, 0x41, 0x54, 0x45, 0x44, 0x10, + 0x03, 0x22, 0x63, 0x0a, 0x14, 0x4f, 0x6e, 0x65, 0x6f, 0x66, 0x44, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x37, 0x0a, + 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4f, 0x6e, 0x65, + 0x6f, 0x66, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, 0x6f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0xe3, 0x02, 0x0a, 0x13, 0x45, + 0x6e, 0x75, 0x6d, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, + 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, + 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, + 0x6d, 0x65, 0x12, 0x3f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, + 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x29, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x45, 0x6e, 0x75, 0x6d, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x44, 0x65, 0x73, + 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, + 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x36, 0x0a, 0x07, 0x6f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x12, 0x5d, 0x0a, 0x0e, 0x72, 0x65, 0x73, 0x65, 0x72, + 0x76, 0x65, 0x64, 0x5f, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x18, 0x04, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x36, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, + 0x75, 0x6d, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, + 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x52, 0x65, + 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, + 0x0d, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x52, 0x61, 0x6e, + 0x67, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, + 0x65, 0x64, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x03, 0x28, + 0x09, 0x52, 0x0c, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x4e, + 0x61, 0x6d, 0x65, 0x1a, 0x3b, 0x0a, 0x11, 0x45, 0x6e, 0x75, 0x6d, 0x52, + 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x52, 0x61, 0x6e, 0x67, 0x65, + 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x12, 0x10, + 0x0a, 0x03, 0x65, 0x6e, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x03, 0x65, 0x6e, 0x64, 0x22, 0x83, 0x01, 0x0a, 0x18, 0x45, 0x6e, 0x75, + 0x6d, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, + 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x12, 0x0a, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, + 0x62, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6e, + 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x3b, 0x0a, 0x07, 0x6f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x21, + 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x56, 0x61, 0x6c, + 0x75, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, 0x6f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0xa7, 0x01, 0x0a, 0x16, 0x53, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, + 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x12, 0x0a, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x3e, 0x0a, 0x06, 0x6d, 0x65, 0x74, + 0x68, 0x6f, 0x64, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x26, 0x2e, + 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x44, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x52, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x39, 0x0a, + 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x1f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, + 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x89, 0x02, 0x0a, + 0x15, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x44, 0x65, 0x73, 0x63, 0x72, + 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x12, + 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x69, 0x6e, + 0x70, 0x75, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x09, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x54, 0x79, 0x70, + 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x5f, + 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, + 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x38, + 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x04, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x1e, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, + 0x74, 0x68, 0x6f, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, + 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x30, 0x0a, 0x10, + 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x61, + 0x6d, 0x69, 0x6e, 0x67, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, + 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x0f, 0x63, 0x6c, 0x69, 0x65, 0x6e, + 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x12, 0x30, + 0x0a, 0x10, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x73, 0x74, 0x72, + 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, + 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x0f, 0x73, 0x65, 0x72, + 0x76, 0x65, 0x72, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, + 0x22, 0x92, 0x09, 0x0a, 0x0b, 0x46, 0x69, 0x6c, 0x65, 0x4f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x6a, 0x61, 0x76, 0x61, + 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x0b, 0x6a, 0x61, 0x76, 0x61, 0x50, 0x61, 0x63, 0x6b, + 0x61, 0x67, 0x65, 0x12, 0x30, 0x0a, 0x14, 0x6a, 0x61, 0x76, 0x61, 0x5f, + 0x6f, 0x75, 0x74, 0x65, 0x72, 0x5f, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x6e, + 0x61, 0x6d, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x12, 0x6a, + 0x61, 0x76, 0x61, 0x4f, 0x75, 0x74, 0x65, 0x72, 0x43, 0x6c, 0x61, 0x73, + 0x73, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x35, 0x0a, 0x13, 0x6a, 0x61, 0x76, + 0x61, 0x5f, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x6c, 0x65, 0x5f, 0x66, + 0x69, 0x6c, 0x65, 0x73, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, + 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x11, 0x6a, 0x61, 0x76, 0x61, 0x4d, + 0x75, 0x6c, 0x74, 0x69, 0x70, 0x6c, 0x65, 0x46, 0x69, 0x6c, 0x65, 0x73, + 0x12, 0x44, 0x0a, 0x1d, 0x6a, 0x61, 0x76, 0x61, 0x5f, 0x67, 0x65, 0x6e, + 0x65, 0x72, 0x61, 0x74, 0x65, 0x5f, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x73, + 0x5f, 0x61, 0x6e, 0x64, 0x5f, 0x68, 0x61, 0x73, 0x68, 0x18, 0x14, 0x20, + 0x01, 0x28, 0x08, 0x42, 0x02, 0x18, 0x01, 0x52, 0x19, 0x6a, 0x61, 0x76, + 0x61, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x45, 0x71, 0x75, + 0x61, 0x6c, 0x73, 0x41, 0x6e, 0x64, 0x48, 0x61, 0x73, 0x68, 0x12, 0x3a, + 0x0a, 0x16, 0x6a, 0x61, 0x76, 0x61, 0x5f, 0x73, 0x74, 0x72, 0x69, 0x6e, + 0x67, 0x5f, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x5f, 0x75, 0x74, 0x66, 0x38, + 0x18, 0x1b, 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, + 0x65, 0x52, 0x13, 0x6a, 0x61, 0x76, 0x61, 0x53, 0x74, 0x72, 0x69, 0x6e, + 0x67, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x55, 0x74, 0x66, 0x38, 0x12, 0x53, + 0x0a, 0x0c, 0x6f, 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, 0x5f, 0x66, + 0x6f, 0x72, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x29, 0x2e, 0x67, + 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x73, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, 0x4d, + 0x6f, 0x64, 0x65, 0x3a, 0x05, 0x53, 0x50, 0x45, 0x45, 0x44, 0x52, 0x0b, + 0x6f, 0x70, 0x74, 0x69, 0x6d, 0x69, 0x7a, 0x65, 0x46, 0x6f, 0x72, 0x12, + 0x1d, 0x0a, 0x0a, 0x67, 0x6f, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, + 0x65, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x67, 0x6f, 0x50, + 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x35, 0x0a, 0x13, 0x63, 0x63, + 0x5f, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x5f, 0x73, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x73, 0x18, 0x10, 0x20, 0x01, 0x28, 0x08, 0x3a, + 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x11, 0x63, 0x63, 0x47, 0x65, + 0x6e, 0x65, 0x72, 0x69, 0x63, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x73, 0x12, 0x39, 0x0a, 0x15, 0x6a, 0x61, 0x76, 0x61, 0x5f, 0x67, 0x65, + 0x6e, 0x65, 0x72, 0x69, 0x63, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x73, 0x18, 0x11, 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, 0x66, 0x61, + 0x6c, 0x73, 0x65, 0x52, 0x13, 0x6a, 0x61, 0x76, 0x61, 0x47, 0x65, 0x6e, + 0x65, 0x72, 0x69, 0x63, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, + 0x12, 0x35, 0x0a, 0x13, 0x70, 0x79, 0x5f, 0x67, 0x65, 0x6e, 0x65, 0x72, + 0x69, 0x63, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x18, + 0x12, 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, + 0x52, 0x11, 0x70, 0x79, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x53, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x12, 0x37, 0x0a, 0x14, 0x70, + 0x68, 0x70, 0x5f, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x5f, 0x73, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x18, 0x2a, 0x20, 0x01, 0x28, + 0x08, 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x12, 0x70, 0x68, + 0x70, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x53, 0x65, 0x72, 0x76, + 0x69, 0x63, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x0a, 0x64, 0x65, 0x70, 0x72, + 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x18, 0x17, 0x20, 0x01, 0x28, 0x08, + 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x0a, 0x64, 0x65, 0x70, + 0x72, 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x12, 0x2f, 0x0a, 0x10, 0x63, + 0x63, 0x5f, 0x65, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x61, 0x72, 0x65, + 0x6e, 0x61, 0x73, 0x18, 0x1f, 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, 0x66, + 0x61, 0x6c, 0x73, 0x65, 0x52, 0x0e, 0x63, 0x63, 0x45, 0x6e, 0x61, 0x62, + 0x6c, 0x65, 0x41, 0x72, 0x65, 0x6e, 0x61, 0x73, 0x12, 0x2a, 0x0a, 0x11, + 0x6f, 0x62, 0x6a, 0x63, 0x5f, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x5f, 0x70, + 0x72, 0x65, 0x66, 0x69, 0x78, 0x18, 0x24, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x0f, 0x6f, 0x62, 0x6a, 0x63, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x50, 0x72, + 0x65, 0x66, 0x69, 0x78, 0x12, 0x29, 0x0a, 0x10, 0x63, 0x73, 0x68, 0x61, + 0x72, 0x70, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, + 0x18, 0x25, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x63, 0x73, 0x68, 0x61, + 0x72, 0x70, 0x4e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, + 0x21, 0x0a, 0x0c, 0x73, 0x77, 0x69, 0x66, 0x74, 0x5f, 0x70, 0x72, 0x65, + 0x66, 0x69, 0x78, 0x18, 0x27, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x73, + 0x77, 0x69, 0x66, 0x74, 0x50, 0x72, 0x65, 0x66, 0x69, 0x78, 0x12, 0x28, + 0x0a, 0x10, 0x70, 0x68, 0x70, 0x5f, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x5f, + 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x18, 0x28, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x0e, 0x70, 0x68, 0x70, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x50, 0x72, + 0x65, 0x66, 0x69, 0x78, 0x12, 0x23, 0x0a, 0x0d, 0x70, 0x68, 0x70, 0x5f, + 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x29, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x0c, 0x70, 0x68, 0x70, 0x4e, 0x61, 0x6d, 0x65, + 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, 0x34, 0x0a, 0x16, 0x70, 0x68, 0x70, + 0x5f, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x6e, 0x61, + 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x2c, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x14, 0x70, 0x68, 0x70, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, + 0x74, 0x61, 0x4e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, + 0x21, 0x0a, 0x0c, 0x72, 0x75, 0x62, 0x79, 0x5f, 0x70, 0x61, 0x63, 0x6b, + 0x61, 0x67, 0x65, 0x18, 0x2d, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x72, + 0x75, 0x62, 0x79, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x58, + 0x0a, 0x14, 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, + 0x74, 0x65, 0x64, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0xe7, + 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x55, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, + 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, 0x69, + 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3a, 0x0a, 0x0c, 0x4f, 0x70, 0x74, 0x69, + 0x6d, 0x69, 0x7a, 0x65, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x09, 0x0a, 0x05, + 0x53, 0x50, 0x45, 0x45, 0x44, 0x10, 0x01, 0x12, 0x0d, 0x0a, 0x09, 0x43, + 0x4f, 0x44, 0x45, 0x5f, 0x53, 0x49, 0x5a, 0x45, 0x10, 0x02, 0x12, 0x10, + 0x0a, 0x0c, 0x4c, 0x49, 0x54, 0x45, 0x5f, 0x52, 0x55, 0x4e, 0x54, 0x49, + 0x4d, 0x45, 0x10, 0x03, 0x2a, 0x09, 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, + 0x80, 0x80, 0x02, 0x4a, 0x04, 0x08, 0x26, 0x10, 0x27, 0x22, 0xd1, 0x02, + 0x0a, 0x0e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x4f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x3c, 0x0a, 0x17, 0x6d, 0x65, 0x73, 0x73, + 0x61, 0x67, 0x65, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x77, 0x69, 0x72, 0x65, + 0x5f, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x08, 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x14, 0x6d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x53, 0x65, 0x74, 0x57, 0x69, 0x72, 0x65, + 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x12, 0x4c, 0x0a, 0x1f, 0x6e, 0x6f, + 0x5f, 0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x5f, 0x64, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x5f, 0x61, 0x63, 0x63, + 0x65, 0x73, 0x73, 0x6f, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x3a, + 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x1c, 0x6e, 0x6f, 0x53, 0x74, + 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, + 0x70, 0x74, 0x6f, 0x72, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x6f, 0x72, + 0x12, 0x25, 0x0a, 0x0a, 0x64, 0x65, 0x70, 0x72, 0x65, 0x63, 0x61, 0x74, + 0x65, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, 0x66, 0x61, + 0x6c, 0x73, 0x65, 0x52, 0x0a, 0x64, 0x65, 0x70, 0x72, 0x65, 0x63, 0x61, + 0x74, 0x65, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x6d, 0x61, 0x70, 0x5f, 0x65, + 0x6e, 0x74, 0x72, 0x79, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, + 0x6d, 0x61, 0x70, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x58, 0x0a, 0x14, + 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, + 0x64, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0xe7, 0x07, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x55, 0x6e, + 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, 0x69, 0x6e, 0x74, + 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x2a, 0x09, 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, 0x80, 0x80, + 0x02, 0x4a, 0x04, 0x08, 0x08, 0x10, 0x09, 0x4a, 0x04, 0x08, 0x09, 0x10, + 0x0a, 0x22, 0xe2, 0x03, 0x0a, 0x0c, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x41, 0x0a, 0x05, 0x63, 0x74, + 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x23, 0x2e, + 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x43, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x06, + 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x52, 0x05, 0x63, 0x74, 0x79, 0x70, + 0x65, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x64, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x70, 0x61, 0x63, 0x6b, 0x65, + 0x64, 0x12, 0x47, 0x0a, 0x06, 0x6a, 0x73, 0x74, 0x79, 0x70, 0x65, 0x18, + 0x06, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x2e, 0x4a, 0x53, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x09, 0x4a, 0x53, 0x5f, + 0x4e, 0x4f, 0x52, 0x4d, 0x41, 0x4c, 0x52, 0x06, 0x6a, 0x73, 0x74, 0x79, + 0x70, 0x65, 0x12, 0x19, 0x0a, 0x04, 0x6c, 0x61, 0x7a, 0x79, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, + 0x04, 0x6c, 0x61, 0x7a, 0x79, 0x12, 0x25, 0x0a, 0x0a, 0x64, 0x65, 0x70, + 0x72, 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x08, 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x0a, 0x64, 0x65, + 0x70, 0x72, 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x12, 0x19, 0x0a, 0x04, + 0x77, 0x65, 0x61, 0x6b, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, + 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x04, 0x77, 0x65, 0x61, 0x6b, 0x12, + 0x58, 0x0a, 0x14, 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, + 0x65, 0x74, 0x65, 0x64, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, + 0xe7, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, + 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2e, 0x55, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, + 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, + 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x2f, 0x0a, 0x05, 0x43, 0x54, 0x79, + 0x70, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, + 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x43, 0x4f, 0x52, 0x44, 0x10, 0x01, + 0x12, 0x10, 0x0a, 0x0c, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x5f, 0x50, + 0x49, 0x45, 0x43, 0x45, 0x10, 0x02, 0x22, 0x35, 0x0a, 0x06, 0x4a, 0x53, + 0x54, 0x79, 0x70, 0x65, 0x12, 0x0d, 0x0a, 0x09, 0x4a, 0x53, 0x5f, 0x4e, + 0x4f, 0x52, 0x4d, 0x41, 0x4c, 0x10, 0x00, 0x12, 0x0d, 0x0a, 0x09, 0x4a, + 0x53, 0x5f, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x10, 0x01, 0x12, 0x0d, + 0x0a, 0x09, 0x4a, 0x53, 0x5f, 0x4e, 0x55, 0x4d, 0x42, 0x45, 0x52, 0x10, + 0x02, 0x2a, 0x09, 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, 0x80, 0x80, 0x02, + 0x4a, 0x04, 0x08, 0x04, 0x10, 0x05, 0x22, 0x73, 0x0a, 0x0c, 0x4f, 0x6e, + 0x65, 0x6f, 0x66, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x58, + 0x0a, 0x14, 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, + 0x74, 0x65, 0x64, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0xe7, + 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x55, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, + 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, 0x69, + 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x2a, 0x09, 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, + 0x80, 0x80, 0x02, 0x22, 0xc0, 0x01, 0x0a, 0x0b, 0x45, 0x6e, 0x75, 0x6d, + 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x1f, 0x0a, 0x0b, 0x61, + 0x6c, 0x6c, 0x6f, 0x77, 0x5f, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x61, 0x6c, 0x6c, 0x6f, 0x77, 0x41, + 0x6c, 0x69, 0x61, 0x73, 0x12, 0x25, 0x0a, 0x0a, 0x64, 0x65, 0x70, 0x72, + 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, + 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x0a, 0x64, 0x65, 0x70, + 0x72, 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x12, 0x58, 0x0a, 0x14, 0x75, + 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, + 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0xe7, 0x07, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x55, 0x6e, 0x69, + 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, + 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x2a, 0x09, 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, 0x80, 0x80, 0x02, + 0x4a, 0x04, 0x08, 0x05, 0x10, 0x06, 0x22, 0x9e, 0x01, 0x0a, 0x10, 0x45, + 0x6e, 0x75, 0x6d, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x4f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x12, 0x25, 0x0a, 0x0a, 0x64, 0x65, 0x70, 0x72, 0x65, + 0x63, 0x61, 0x74, 0x65, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x3a, + 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x0a, 0x64, 0x65, 0x70, 0x72, + 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x12, 0x58, 0x0a, 0x14, 0x75, 0x6e, + 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x5f, + 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0xe7, 0x07, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x55, 0x6e, 0x69, 0x6e, + 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, + 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x2a, 0x09, 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, 0x80, 0x80, 0x02, 0x22, + 0x9c, 0x01, 0x0a, 0x0e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x25, 0x0a, 0x0a, 0x64, 0x65, + 0x70, 0x72, 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x18, 0x21, 0x20, 0x01, + 0x28, 0x08, 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, 0x0a, 0x64, + 0x65, 0x70, 0x72, 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x12, 0x58, 0x0a, + 0x14, 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, + 0x65, 0x64, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0xe7, 0x07, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x55, + 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, + 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, 0x69, 0x6e, + 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x2a, 0x09, 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, 0x80, + 0x80, 0x02, 0x22, 0xe0, 0x02, 0x0a, 0x0d, 0x4d, 0x65, 0x74, 0x68, 0x6f, + 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x25, 0x0a, 0x0a, + 0x64, 0x65, 0x70, 0x72, 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x18, 0x21, + 0x20, 0x01, 0x28, 0x08, 0x3a, 0x05, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x52, + 0x0a, 0x64, 0x65, 0x70, 0x72, 0x65, 0x63, 0x61, 0x74, 0x65, 0x64, 0x12, + 0x71, 0x0a, 0x11, 0x69, 0x64, 0x65, 0x6d, 0x70, 0x6f, 0x74, 0x65, 0x6e, + 0x63, 0x79, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x22, 0x20, 0x01, + 0x28, 0x0e, 0x32, 0x2f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x74, + 0x68, 0x6f, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x49, + 0x64, 0x65, 0x6d, 0x70, 0x6f, 0x74, 0x65, 0x6e, 0x63, 0x79, 0x4c, 0x65, + 0x76, 0x65, 0x6c, 0x3a, 0x13, 0x49, 0x44, 0x45, 0x4d, 0x50, 0x4f, 0x54, + 0x45, 0x4e, 0x43, 0x59, 0x5f, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, + 0x52, 0x10, 0x69, 0x64, 0x65, 0x6d, 0x70, 0x6f, 0x74, 0x65, 0x6e, 0x63, + 0x79, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x58, 0x0a, 0x14, 0x75, 0x6e, + 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x5f, + 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0xe7, 0x07, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x55, 0x6e, 0x69, 0x6e, + 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x75, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, + 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x22, 0x50, 0x0a, 0x10, 0x49, 0x64, 0x65, 0x6d, 0x70, 0x6f, 0x74, 0x65, + 0x6e, 0x63, 0x79, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x17, 0x0a, 0x13, + 0x49, 0x44, 0x45, 0x4d, 0x50, 0x4f, 0x54, 0x45, 0x4e, 0x43, 0x59, 0x5f, + 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x13, 0x0a, + 0x0f, 0x4e, 0x4f, 0x5f, 0x53, 0x49, 0x44, 0x45, 0x5f, 0x45, 0x46, 0x46, + 0x45, 0x43, 0x54, 0x53, 0x10, 0x01, 0x12, 0x0e, 0x0a, 0x0a, 0x49, 0x44, + 0x45, 0x4d, 0x50, 0x4f, 0x54, 0x45, 0x4e, 0x54, 0x10, 0x02, 0x2a, 0x09, + 0x08, 0xe8, 0x07, 0x10, 0x80, 0x80, 0x80, 0x80, 0x02, 0x22, 0x9a, 0x03, + 0x0a, 0x13, 0x55, 0x6e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x65, + 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x41, 0x0a, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x2d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x55, 0x6e, 0x69, 0x6e, 0x74, 0x65, + 0x72, 0x70, 0x72, 0x65, 0x74, 0x65, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x50, 0x61, 0x72, 0x74, 0x52, 0x04, + 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x29, 0x0a, 0x10, 0x69, 0x64, 0x65, 0x6e, + 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x69, 0x64, 0x65, 0x6e, + 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, + 0x2c, 0x0a, 0x12, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x76, 0x65, 0x5f, + 0x69, 0x6e, 0x74, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x04, 0x20, + 0x01, 0x28, 0x04, 0x52, 0x10, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x76, + 0x65, 0x49, 0x6e, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x2c, 0x0a, + 0x12, 0x6e, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x5f, 0x69, 0x6e, + 0x74, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x10, 0x6e, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x49, + 0x6e, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x21, 0x0a, 0x0c, 0x64, + 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, + 0x06, 0x20, 0x01, 0x28, 0x01, 0x52, 0x0b, 0x64, 0x6f, 0x75, 0x62, 0x6c, + 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x21, 0x0a, 0x0c, 0x73, 0x74, + 0x72, 0x69, 0x6e, 0x67, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x07, + 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0b, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, + 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x27, 0x0a, 0x0f, 0x61, 0x67, 0x67, + 0x72, 0x65, 0x67, 0x61, 0x74, 0x65, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x61, 0x67, 0x67, 0x72, + 0x65, 0x67, 0x61, 0x74, 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x1a, 0x4a, + 0x0a, 0x08, 0x4e, 0x61, 0x6d, 0x65, 0x50, 0x61, 0x72, 0x74, 0x12, 0x1b, + 0x0a, 0x09, 0x6e, 0x61, 0x6d, 0x65, 0x5f, 0x70, 0x61, 0x72, 0x74, 0x18, + 0x01, 0x20, 0x02, 0x28, 0x09, 0x52, 0x08, 0x6e, 0x61, 0x6d, 0x65, 0x50, + 0x61, 0x72, 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x69, 0x73, 0x5f, 0x65, 0x78, + 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x02, 0x28, + 0x08, 0x52, 0x0b, 0x69, 0x73, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, + 0x6f, 0x6e, 0x22, 0xa7, 0x02, 0x0a, 0x0e, 0x53, 0x6f, 0x75, 0x72, 0x63, + 0x65, 0x43, 0x6f, 0x64, 0x65, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x44, 0x0a, + 0x08, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x28, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x6f, + 0x75, 0x72, 0x63, 0x65, 0x43, 0x6f, 0x64, 0x65, 0x49, 0x6e, 0x66, 0x6f, + 0x2e, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x6c, + 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x1a, 0xce, 0x01, 0x0a, 0x08, + 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x04, + 0x70, 0x61, 0x74, 0x68, 0x18, 0x01, 0x20, 0x03, 0x28, 0x05, 0x42, 0x02, + 0x10, 0x01, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x16, 0x0a, 0x04, + 0x73, 0x70, 0x61, 0x6e, 0x18, 0x02, 0x20, 0x03, 0x28, 0x05, 0x42, 0x02, + 0x10, 0x01, 0x52, 0x04, 0x73, 0x70, 0x61, 0x6e, 0x12, 0x29, 0x0a, 0x10, + 0x6c, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, + 0x65, 0x6e, 0x74, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, + 0x6c, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6d, 0x6d, 0x65, + 0x6e, 0x74, 0x73, 0x12, 0x2b, 0x0a, 0x11, 0x74, 0x72, 0x61, 0x69, 0x6c, + 0x69, 0x6e, 0x67, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, + 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x10, 0x74, 0x72, 0x61, 0x69, + 0x6c, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, + 0x12, 0x3a, 0x0a, 0x19, 0x6c, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x5f, + 0x64, 0x65, 0x74, 0x61, 0x63, 0x68, 0x65, 0x64, 0x5f, 0x63, 0x6f, 0x6d, + 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x06, 0x20, 0x03, 0x28, 0x09, 0x52, + 0x17, 0x6c, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x44, 0x65, 0x74, 0x61, + 0x63, 0x68, 0x65, 0x64, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x73, + 0x22, 0xd1, 0x01, 0x0a, 0x11, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, + 0x65, 0x64, 0x43, 0x6f, 0x64, 0x65, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x4d, + 0x0a, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2d, 0x2e, 0x67, 0x6f, 0x6f, + 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x43, 0x6f, + 0x64, 0x65, 0x49, 0x6e, 0x66, 0x6f, 0x2e, 0x41, 0x6e, 0x6e, 0x6f, 0x74, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, 0x74, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x1a, 0x6d, 0x0a, 0x0a, 0x41, 0x6e, 0x6e, + 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x04, 0x70, + 0x61, 0x74, 0x68, 0x18, 0x01, 0x20, 0x03, 0x28, 0x05, 0x42, 0x02, 0x10, + 0x01, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x1f, 0x0a, 0x0b, 0x73, + 0x6f, 0x75, 0x72, 0x63, 0x65, 0x5f, 0x66, 0x69, 0x6c, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, + 0x46, 0x69, 0x6c, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x62, 0x65, 0x67, 0x69, + 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x62, 0x65, 0x67, + 0x69, 0x6e, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x6e, 0x64, 0x18, 0x04, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x03, 0x65, 0x6e, 0x64, 0x42, 0x8f, 0x01, 0x0a, + 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x10, 0x44, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x73, 0x48, 0x01, 0x5a, 0x3e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x67, 0x6f, 0x2f, 0x64, + 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x3b, 0x64, 0x65, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0xf8, 0x01, 0x01, 0xa2, + 0x02, 0x03, 0x47, 0x50, 0x42, 0xaa, 0x02, 0x1a, 0x47, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x52, 0x65, 0x66, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e +}; +unsigned int descriptor_proto_len = 7582; diff --git a/php/ext/google/protobuf/def.c b/php/ext/google/protobuf/def.c index 238a924789..5df6802489 100644 --- a/php/ext/google/protobuf/def.c +++ b/php/ext/google/protobuf/def.c @@ -28,7 +28,11 @@ // (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 "protobuf.h" +#include "builtin_descriptors.inc" // Forward declare. static void descriptor_init_c_instance(Descriptor* intern TSRMLS_DC); @@ -65,21 +69,6 @@ static void check_upb_status(const upb_status* status, const char* msg) { } } -static void upb_filedef_free(void *r) { - upb_filedef *f = *(upb_filedef **)r; - size_t i; - - for (i = 0; i < upb_filedef_depcount(f); i++) { - upb_filedef_unref(upb_filedef_dep(f, i), f); - } - - upb_inttable_uninit(&f->defs); - upb_inttable_uninit(&f->deps); - upb_gfree((void *)f->name); - upb_gfree((void *)f->package); - upb_gfree(f); -} - // Camel-case the field name and append "Entry" for generated map entry name. // e.g. map foo_map => FooMapEntry static void append_map_entry_name(char *result, const char *field_name, @@ -105,13 +94,6 @@ static void append_map_entry_name(char *result, const char *field_name, strcat(result, "Entry"); } -#define CHECK_UPB(code, msg) \ - do { \ - upb_status status = UPB_STATUS_INIT; \ - code; \ - check_upb_status(&status, msg); \ - } while (0) - // ----------------------------------------------------------------------------- // GPBType // ----------------------------------------------------------------------------- @@ -169,40 +151,12 @@ static void descriptor_free_c(Descriptor *self TSRMLS_DC) { if (self->layout) { free_layout(self->layout); } - if (self->fill_handlers) { - upb_handlers_unref(self->fill_handlers, &self->fill_handlers); - } - if (self->fill_method) { - upb_pbdecodermethod_unref(self->fill_method, &self->fill_method); - } - if (self->json_fill_method) { - upb_json_parsermethod_unref(self->json_fill_method, - &self->json_fill_method); - } - if (self->pb_serialize_handlers) { - upb_handlers_unref(self->pb_serialize_handlers, - &self->pb_serialize_handlers); - } - if (self->json_serialize_handlers) { - upb_handlers_unref(self->json_serialize_handlers, - &self->json_serialize_handlers); - } - if (self->json_serialize_handlers_preserve) { - upb_handlers_unref(self->json_serialize_handlers_preserve, - &self->json_serialize_handlers_preserve); - } } static void descriptor_init_c_instance(Descriptor *desc TSRMLS_DC) { desc->msgdef = NULL; desc->layout = NULL; desc->klass = NULL; - desc->fill_handlers = NULL; - desc->fill_method = NULL; - desc->json_fill_method = NULL; - desc->pb_serialize_handlers = NULL; - desc->json_serialize_handlers = NULL; - desc->json_serialize_handlers_preserve = NULL; } PHP_METHOD(Descriptor, getClass) { @@ -294,7 +248,7 @@ PHP_METHOD(Descriptor, getOneofDecl) { for(upb_msg_oneof_begin(&iter, intern->msgdef), i = 0; !upb_msg_oneof_done(&iter) && i < index; upb_msg_oneof_next(&iter), i++); - upb_oneofdef *oneof = upb_msg_iter_oneof(&iter); + const upb_oneofdef *oneof = upb_msg_iter_oneof(&iter); ZVAL_OBJ(return_value, oneof_descriptor_type->create_object( oneof_descriptor_type TSRMLS_CC)); @@ -479,14 +433,13 @@ PHP_METHOD(FieldDescriptor, isMap) { PHP_METHOD(FieldDescriptor, getEnumType) { FieldDescriptor *intern = UNBOX(FieldDescriptor, getThis()); - const upb_enumdef *enumdef = upb_fielddef_enumsubdef(intern->fielddef); - if (enumdef == NULL) { - char error_msg[100]; - sprintf(error_msg, "Cannot get enum type for non-enum field '%s'", - upb_fielddef_name(intern->fielddef)); - zend_throw_exception(NULL, error_msg, 0 TSRMLS_CC); + if (upb_fielddef_type(intern->fielddef) != UPB_TYPE_ENUM) { + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Cannot get enum type for non-enum field '%s'", + upb_fielddef_name(intern->fielddef)); return; } + const upb_enumdef *enumdef = upb_fielddef_enumsubdef(intern->fielddef); PHP_PROTO_HASHTABLE_VALUE desc = get_def_obj(enumdef); #if PHP_MAJOR_VERSION < 7 @@ -499,14 +452,13 @@ PHP_METHOD(FieldDescriptor, getEnumType) { PHP_METHOD(FieldDescriptor, getMessageType) { FieldDescriptor *intern = UNBOX(FieldDescriptor, getThis()); - const upb_msgdef *msgdef = upb_fielddef_msgsubdef(intern->fielddef); - if (msgdef == NULL) { - char error_msg[100]; - sprintf(error_msg, "Cannot get message type for non-message field '%s'", - upb_fielddef_name(intern->fielddef)); - zend_throw_exception(NULL, error_msg, 0 TSRMLS_CC); + if (upb_fielddef_type(intern->fielddef) != UPB_TYPE_MESSAGE) { + zend_throw_exception_ex( + NULL, 0 TSRMLS_CC, "Cannot get message type for non-message field '%s'", + upb_fielddef_name(intern->fielddef)); return; } + const upb_msgdef *msgdef = upb_fielddef_msgsubdef(intern->fielddef); PHP_PROTO_HASHTABLE_VALUE desc = get_def_obj(msgdef); #if PHP_MAJOR_VERSION < 7 @@ -654,17 +606,24 @@ void init_generated_pool_once(TSRMLS_D) { static void internal_descriptor_pool_init_c_instance( InternalDescriptorPool *pool TSRMLS_DC) { pool->symtab = upb_symtab_new(); - - ALLOC_HASHTABLE(pool->pending_list); - zend_hash_init(pool->pending_list, 1, NULL, ZVAL_PTR_DTOR, 0); + pool->fill_handler_cache = + upb_handlercache_new(add_handlers_for_message, NULL); + pool->pb_serialize_handler_cache = upb_pb_encoder_newcache(); + pool->json_serialize_handler_cache = upb_json_printer_newcache(false); + pool->json_serialize_handler_preserve_cache = upb_json_printer_newcache(true); + pool->fill_method_cache = upb_pbcodecache_new(pool->fill_handler_cache); + pool->json_fill_method_cache = upb_json_codecache_new(); } static void internal_descriptor_pool_free_c( InternalDescriptorPool *pool TSRMLS_DC) { upb_symtab_free(pool->symtab); - - zend_hash_destroy(pool->pending_list); - FREE_HASHTABLE(pool->pending_list); + upb_handlercache_free(pool->fill_handler_cache); + upb_handlercache_free(pool->pb_serialize_handler_cache); + upb_handlercache_free(pool->json_serialize_handler_cache); + upb_handlercache_free(pool->json_serialize_handler_preserve_cache); + upb_pbcodecache_free(pool->fill_method_cache); + upb_json_codecache_free(pool->json_fill_method_cache); } static void descriptor_pool_init_c_instance(DescriptorPool *pool TSRMLS_DC) { @@ -786,47 +745,44 @@ static bool is_reserved(const char *segment, int length) { return result; } -static char* fill_prefix(const char *segment, int length, - const char *prefix_given, - const char *package_name, char *classname) { +static void fill_prefix(const char *segment, int length, + const char *prefix_given, + const char *package_name, + stringsink *classname) { size_t i; if (prefix_given != NULL && strcmp(prefix_given, "") != 0) { - size_t prefix_len = strlen(prefix_given); - memcpy(classname, prefix_given, strlen(prefix_given)); - classname += prefix_len; + stringsink_string(classname, NULL, prefix_given, + strlen(prefix_given), NULL); } else { if (is_reserved(segment, length)) { if (package_name != NULL && strcmp("google.protobuf", package_name) == 0) { - memcpy(classname, "GPB", 3); - classname += 3; + stringsink_string(classname, NULL, "GPB", 3, NULL); } else { - memcpy(classname, "PB", 2); - classname += 2; + stringsink_string(classname, NULL, "PB", 2, NULL); } } } - return classname; } -static char* fill_segment(const char *segment, int length, - char *classname, bool use_camel) { - memcpy(classname, segment, length); +static void fill_segment(const char *segment, int length, + stringsink *classname, bool use_camel) { if (use_camel && (segment[0] < 'A' || segment[0] > 'Z')) { - classname[0] += 'A' - 'a'; + char first = segment[0] + ('A' - 'a'); + stringsink_string(classname, NULL, &first, 1, NULL); + stringsink_string(classname, NULL, segment + 1, length - 1, NULL); + } else { + stringsink_string(classname, NULL, segment, length, NULL); } - return classname + length; } -static char* fill_namespace(const char *package, const char *namespace_given, - char *classname) { - if (namespace_given != NULL) { - size_t namespace_len = strlen(namespace_given); - memcpy(classname, namespace_given, namespace_len); - classname += namespace_len; - *classname = '\\'; - classname++; +static void fill_namespace(const char *package, const char *php_namespace, + stringsink *classname) { + if (php_namespace != NULL) { + stringsink_string(classname, NULL, php_namespace, strlen(php_namespace), + NULL); + stringsink_string(classname, NULL, "\\", 1, NULL); } else if (package != NULL) { int i = 0, j, offset = 0; size_t package_len = strlen(package); @@ -835,29 +791,26 @@ static char* fill_namespace(const char *package, const char *namespace_given, while (j < package_len && package[j] != '.') { j++; } - classname = fill_prefix(package + i, j - i, "", package, classname); - classname = fill_segment(package + i, j - i, classname, true); - classname[0] = '\\'; - classname++; + fill_prefix(package + i, j - i, "", package, classname); + fill_segment(package + i, j - i, classname, true); + stringsink_string(classname, NULL, "\\", 1, NULL); i = j + 1; } } - return classname; } -static char* fill_classname(const char *fullname, - const char *package, - const char *namespace_given, - const char *prefix, char *classname) { +static void fill_classname(const char *fullname, + const char *package, + const char *prefix, + stringsink *classname, + bool use_nested_submsg) { int classname_start = 0; if (package != NULL) { size_t package_len = strlen(package); classname_start = package_len == 0 ? 0 : package_len + 1; } size_t fullname_len = strlen(fullname); - classname = fill_prefix(fullname + classname_start, - fullname_len - classname_start, - prefix, package, classname); + bool is_first_segment = true; int i = classname_start, j; while (i < fullname_len) { @@ -865,140 +818,187 @@ static char* fill_classname(const char *fullname, while (j < fullname_len && fullname[j] != '.') { j++; } - classname = fill_segment(fullname + i, j - i, classname, false); + if (use_nested_submsg || is_first_segment && j == fullname_len) { + fill_prefix(fullname + i, j - i, prefix, package, classname); + } + is_first_segment = false; + fill_segment(fullname + i, j - i, classname, false); if (j != fullname_len) { - *classname = '_'; - classname++; + if (use_nested_submsg) { + stringsink_string(classname, NULL, "\\", 1, NULL); + } else { + stringsink_string(classname, NULL, "_", 1, NULL); + } } i = j + 1; } - return classname; } -static char* fill_qualified_classname(const char *fullname, - const char *package, - const char *namespace_given, - const char *prefix, char *classname) { - classname = fill_namespace(package, namespace_given, classname); - return fill_classname(fullname, package, namespace_given, prefix, classname); +static zend_class_entry *register_class(const upb_filedef *file, + const char *fullname, + PHP_PROTO_HASHTABLE_VALUE desc_php, + bool use_nested_submsg TSRMLS_DC) { + // Prepend '.' to package name to make it absolute. In the 5 additional + // bytes allocated, one for '.', one for trailing 0, and 3 for 'GPB' if + // given message is google.protobuf.Empty. + const char *package = upb_filedef_package(file); + const char *php_namespace = upb_filedef_phpnamespace(file); + const char *prefix = upb_filedef_phpprefix(file); + size_t classname_len = + classname_len_max(fullname, package, php_namespace, prefix); + char* after_package; + zend_class_entry* ret; + stringsink namesink; + stringsink_init(&namesink); + + fill_namespace(package, php_namespace, &namesink); + fill_classname(fullname, package, prefix, &namesink, use_nested_submsg); + stringsink_string(&namesink, NULL, "\0", 1, NULL); + + PHP_PROTO_CE_DECLARE pce; + if (php_proto_zend_lookup_class(namesink.ptr, namesink.len - 1, &pce) == + FAILURE) { + zend_error( + E_ERROR, + "Generated message class %s hasn't been defined (%s, %s, %s, %s)", + namesink.ptr, fullname, package, php_namespace, prefix); + return NULL; + } + ret = PHP_PROTO_CE_UNREF(pce); + add_ce_obj(ret, desc_php); + add_proto_obj(fullname, desc_php); + stringsink_uninit(&namesink); + return ret; } -static void classname_no_prefix(const char *fullname, const char *package_name, - char *class_name) { - size_t i = 0, j; - bool first_char = true, is_reserved = false; - size_t pkg_name_len = package_name == NULL ? 0 : strlen(package_name); - size_t message_name_start = package_name == NULL ? 0 : pkg_name_len + 1; - size_t message_len = (strlen(fullname) - message_name_start); - - // Submessage is concatenated with its containing messages by '_'. - for (j = message_name_start; j < message_name_start + message_len; j++) { - if (fullname[j] == '.') { - class_name[i++] = '_'; - } else { - class_name[i++] = fullname[j]; +bool depends_on_descriptor(const google_protobuf_FileDescriptorProto* file) { + const upb_strview *deps; + upb_strview name = upb_strview_makez("google/protobuf/descriptor.proto"); + size_t i, n; + + deps = google_protobuf_FileDescriptorProto_dependency(file, &n); + for (i = 0; i < n; i++) { + if (upb_strview_eql(deps[i], name)) { + return true; } } + + return false; } -void internal_add_generated_file(const char *data, PHP_PROTO_SIZE data_len, - InternalDescriptorPool *pool TSRMLS_DC) { - upb_filedef **files; - size_t i; +const upb_filedef *parse_and_add_descriptor(const char *data, + PHP_PROTO_SIZE data_len, + InternalDescriptorPool *pool, + upb_arena *arena) { + size_t n; + google_protobuf_FileDescriptorSet *set; + const google_protobuf_FileDescriptorProto* const* files; + const upb_filedef* file; + upb_status status; + + set = google_protobuf_FileDescriptorSet_parse( + data, data_len, arena); + + if (!set) { + zend_error(E_ERROR, "Failed to parse binary descriptor\n"); + return false; + } - CHECK_UPB(files = upb_loaddescriptor(data, data_len, &pool, &status), - "Parse binary descriptors to internal descriptors failed"); + files = google_protobuf_FileDescriptorSet_file(set, &n); - // This method is called only once in each file. - assert(files[0] != NULL); - assert(files[1] == NULL); + if (n != 1) { + zend_error(E_ERROR, "Serialized descriptors should have exactly one file"); + return false; + } - CHECK_UPB(upb_symtab_addfile(pool->symtab, files[0], &status), - "Unable to add file to DescriptorPool"); + // The PHP code generator currently special-cases descriptor.proto. It + // doesn't add it as a dependency even if the proto file actually does + // depend on it. + if (depends_on_descriptor(files[0]) && + upb_symtab_lookupfile(pool->symtab, "google/protobuf/descriptor.proto") == + NULL) { + if (!parse_and_add_descriptor((char *)descriptor_proto, + descriptor_proto_len, pool, arena)) { + return false; + } + } + + upb_status_clear(&status); + file = upb_symtab_addfile(pool->symtab, files[0], &status); + check_upb_status(&status, "Unable to load descriptor"); + return file; +} + +void internal_add_generated_file(const char *data, PHP_PROTO_SIZE data_len, + InternalDescriptorPool *pool, + bool use_nested_submsg TSRMLS_DC) { + int i; + upb_arena *arena; + const upb_filedef* file; + + arena = upb_arena_new(); + file = parse_and_add_descriptor(data, data_len, pool, arena); + upb_arena_free(arena); + if (!file) return; // For each enum/message, we need its PHP class, upb descriptor and its PHP // wrapper. These information are needed later for encoding, decoding and type // checking. However, sometimes we just have one of them. In order to find // them quickly, here, we store the mapping for them. - for (i = 0; i < upb_filedef_defcount(files[0]); i++) { - const upb_def *def = upb_filedef_def(files[0], i); - switch (upb_def_type(def)) { -#define CASE_TYPE(def_type, def_type_lower, desc_type, desc_type_lower) \ - case UPB_DEF_##def_type: { \ - CREATE_HASHTABLE_VALUE(desc, desc_php, desc_type, desc_type_lower##_type); \ - const upb_##def_type_lower *def_type_lower = \ - upb_downcast_##def_type_lower(def); \ - desc->def_type_lower = def_type_lower; \ - add_def_obj(desc->def_type_lower, desc_php); \ - /* Unlike other messages, MapEntry is shared by all map fields and doesn't \ - * have generated PHP class.*/ \ - if (upb_def_type(def) == UPB_DEF_MSG && \ - upb_msgdef_mapentry(upb_downcast_msgdef(def))) { \ - break; \ - } \ - /* Prepend '.' to package name to make it absolute. In the 5 additional \ - * bytes allocated, one for '.', one for trailing 0, and 3 for 'GPB' if \ - * given message is google.protobuf.Empty.*/ \ - const char *fullname = upb_##def_type_lower##_fullname(def_type_lower); \ - const char *package = upb_filedef_package(files[0]); \ - const char *php_namespace = upb_filedef_phpnamespace(files[0]); \ - const char *prefix_given = upb_filedef_phpprefix(files[0]); \ - size_t classname_len = classname_len_max(fullname, package, \ - php_namespace, prefix_given); \ - char *classname = ecalloc(sizeof(char), classname_len); \ - fill_qualified_classname(fullname, package, php_namespace, \ - prefix_given, classname); \ - PHP_PROTO_CE_DECLARE pce; \ - if (php_proto_zend_lookup_class(classname, strlen(classname), &pce) == \ - FAILURE) { \ - zend_error(E_ERROR, "Generated message class %s hasn't been defined", \ - classname); \ - return; \ - } else { \ - desc->klass = PHP_PROTO_CE_UNREF(pce); \ - } \ - add_ce_obj(desc->klass, desc_php); \ - add_proto_obj(upb_##def_type_lower##_fullname(desc->def_type_lower), \ - desc_php); \ - efree(classname); \ - break; \ - } - CASE_TYPE(MSG, msgdef, Descriptor, descriptor) - CASE_TYPE(ENUM, enumdef, EnumDescriptor, enum_descriptor) -#undef CASE_TYPE + for (i = 0; i < upb_filedef_msgcount(file); i++) { + const upb_msgdef *msgdef = upb_filedef_msg(file, i); + CREATE_HASHTABLE_VALUE(desc, desc_php, Descriptor, descriptor_type); + desc->msgdef = msgdef; + desc->pool = pool; + add_def_obj(desc->msgdef, desc_php); + + // Unlike other messages, MapEntry is shared by all map fields and doesn't + // have generated PHP class. + if (upb_msgdef_mapentry(msgdef)) { + continue; + } + + desc->klass = register_class(file, upb_msgdef_fullname(msgdef), desc_php, + use_nested_submsg TSRMLS_CC); - default: - break; + if (desc->klass == NULL) { + return; } + + build_class_from_descriptor(desc_php TSRMLS_CC); } - for (i = 0; i < upb_filedef_defcount(files[0]); i++) { - const upb_def *def = upb_filedef_def(files[0], i); - if (upb_def_type(def) == UPB_DEF_MSG) { - const upb_msgdef *msgdef = upb_downcast_msgdef(def); - PHP_PROTO_HASHTABLE_VALUE desc_php = get_def_obj(msgdef); - build_class_from_descriptor(desc_php TSRMLS_CC); + for (i = 0; i < upb_filedef_enumcount(file); i++) { + const upb_enumdef *enumdef = upb_filedef_enum(file, i); + CREATE_HASHTABLE_VALUE(desc, desc_php, EnumDescriptor, enum_descriptor_type); + desc->enumdef = enumdef; + add_def_obj(desc->enumdef, desc_php); + desc->klass = register_class(file, upb_enumdef_fullname(enumdef), desc_php, + use_nested_submsg TSRMLS_CC); + + if (desc->klass == NULL) { + return; } } - - upb_filedef_unref(files[0], &pool); - upb_gfree(files); } PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile) { char *data = NULL; PHP_PROTO_SIZE data_len; upb_filedef **files; + zend_bool use_nested_submsg = false; size_t i; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", + &data, &data_len, &use_nested_submsg) == FAILURE) { return; } InternalDescriptorPool *pool = UNBOX(InternalDescriptorPool, getThis()); - internal_add_generated_file(data, data_len, pool TSRMLS_CC); + internal_add_generated_file(data, data_len, pool, + use_nested_submsg TSRMLS_CC); } PHP_METHOD(DescriptorPool, getDescriptorByClassName) { diff --git a/php/ext/google/protobuf/encode_decode.c b/php/ext/google/protobuf/encode_decode.c index 899b99f075..31d7d5a8d5 100644 --- a/php/ext/google/protobuf/encode_decode.c +++ b/php/ext/google/protobuf/encode_decode.c @@ -27,28 +27,23 @@ // 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 "protobuf.h" #include "utf8.h" /* stringsink *****************************************************************/ -typedef struct { - upb_byteshandler handler; - upb_bytessink sink; - char *ptr; - size_t len, size; -} stringsink; - - static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) { stringsink *sink = _sink; sink->len = 0; return sink; } -static size_t stringsink_string(void *_sink, const void *hd, const char *ptr, - size_t len, const upb_bufhandle *handle) { +size_t stringsink_string(void *_sink, const void *hd, const char *ptr, + size_t len, const upb_bufhandle *handle) { stringsink *sink = _sink; size_t new_size = sink->size; @@ -84,6 +79,8 @@ void stringsink_init(stringsink *sink) { void stringsink_uninit(stringsink *sink) { free(sink->ptr); } +void stringsink_uninit_opaque(void *sink) { stringsink_uninit(sink); } + /* stackenv *****************************************************************/ // Stack-allocated context during an encode/decode operation. Contains the upb @@ -92,7 +89,8 @@ void stringsink_uninit(stringsink *sink) { free(sink->ptr); } // if any error occurs. #define STACK_ENV_STACKBYTES 4096 typedef struct { - upb_env env; + upb_arena *arena; + upb_status status; const char *php_error_template; char allocbuf[STACK_ENV_STACKBYTES]; } stackenv; @@ -101,37 +99,34 @@ typedef struct { static void stackenv_init(stackenv* se, const char* errmsg); static void stackenv_uninit(stackenv* se); -// Callback invoked by upb if any error occurs during parsing or serialization. -static bool env_error_func(void* ud, const upb_status* status) { - char err_msg[100] = ""; - stackenv* se = ud; - // Free the env -- zend_error will longjmp up the stack past the - // encode/decode function so it would not otherwise have been freed. - stackenv_uninit(se); - - // TODO(teboring): have a way to verify that this is actually a parse error, - // instead of just throwing "parse error" unconditionally. - sprintf(err_msg, se->php_error_template, upb_status_errmsg(status)); - TSRMLS_FETCH(); - zend_throw_exception(NULL, err_msg, 0 TSRMLS_CC); - // Never reached. - return false; -} - static void stackenv_init(stackenv* se, const char* errmsg) { se->php_error_template = errmsg; - upb_env_init2(&se->env, se->allocbuf, sizeof(se->allocbuf), NULL); - upb_env_seterrorfunc(&se->env, env_error_func, se); + se->arena = upb_arena_new(); + upb_status_clear(&se->status); } static void stackenv_uninit(stackenv* se) { - upb_env_uninit(&se->env); + upb_arena_free(se->arena); + + if (!upb_ok(&se->status)) { + // TODO(teboring): have a way to verify that this is actually a parse error, + // instead of just throwing "parse error" unconditionally. + TSRMLS_FETCH(); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, se->php_error_template, + upb_status_errmsg(&se->status)); + } } // ----------------------------------------------------------------------------- // Parsing. // ----------------------------------------------------------------------------- +// TODO(teboring): This shoud be a bit in upb_msgdef +static bool is_wrapper_msg(const upb_msgdef *msg) { + return !strcmp(upb_filedef_name(upb_msgdef_file(msg)), + "google/protobuf/wrappers.proto"); +} + #define DEREF(msg, ofs, type) *(type*)(((uint8_t *)msg) + ofs) // Creates a handlerdata that simply contains the offset for this field. @@ -142,6 +137,11 @@ static const void* newhandlerdata(upb_handlers* h, uint32_t ofs) { return hd_ofs; } +typedef struct { + void* closure; + stringsink sink; +} stringfields_parseframe_t; + typedef size_t (*encodeunknown_handlerfunc)(void* _sink, const void* hd, const char* ptr, size_t len, const upb_bufhandle* handle); @@ -239,46 +239,41 @@ DEFINE_APPEND_HANDLER(int64, int64_t) DEFINE_APPEND_HANDLER(uint64, uint64_t) DEFINE_APPEND_HANDLER(double, double) -// Appends a string to a repeated field. +// Appends a string or 'bytes' string to a repeated field. static void* appendstr_handler(void *closure, const void *hd, size_t size_hint) { - zval* array = (zval*)closure; - TSRMLS_FETCH(); - RepeatedField* intern = UNBOX(RepeatedField, array); + UPB_UNUSED(hd); -#if PHP_MAJOR_VERSION < 7 - zval* str; - MAKE_STD_ZVAL(str); - PHP_PROTO_ZVAL_STRING(str, "", 1); - repeated_field_push_native(intern, &str); - return (void*)str; -#else - zend_string* str = zend_string_init("", 0, 1); - repeated_field_push_native(intern, &str); - return intern; -#endif + stringfields_parseframe_t* frame = + (stringfields_parseframe_t*)malloc(sizeof(stringfields_parseframe_t)); + frame->closure = closure; + stringsink_init(&frame->sink); + + return frame; } -// Appends a 'bytes' string to a repeated field. -static void* appendbytes_handler(void *closure, - const void *hd, - size_t size_hint) { - zval* array = (zval*)closure; +static bool appendstr_end_handler(void *closure, const void *hd) { + stringfields_parseframe_t* frame = closure; + + zval* array = (zval*)frame->closure; TSRMLS_FETCH(); RepeatedField* intern = UNBOX(RepeatedField, array); #if PHP_MAJOR_VERSION < 7 zval* str; MAKE_STD_ZVAL(str); - PHP_PROTO_ZVAL_STRING(str, "", 1); + PHP_PROTO_ZVAL_STRINGL(str, frame->sink.ptr, frame->sink.len, 1); repeated_field_push_native(intern, &str); - return (void*)str; #else - zend_string* str = zend_string_init("", 0, 1); + zend_string* str = zend_string_init(frame->sink.ptr, frame->sink.len, 1); repeated_field_push_native(intern, &str); - return intern; #endif + + stringsink_uninit(&frame->sink); + free(frame); + + return true; } // Handlers that append primitive values to a repeated field. @@ -320,64 +315,76 @@ static void *empty_php_string(zval* value_ptr) { return value_ptr; } #endif +#if PHP_MAJOR_VERSION < 7 +static void *empty_php_string2(zval** value_ptr) { + SEPARATE_ZVAL_IF_NOT_REF(value_ptr); + if (Z_TYPE_PP(value_ptr) == IS_STRING && + !IS_INTERNED(Z_STRVAL_PP(value_ptr))) { + FREE(Z_STRVAL_PP(value_ptr)); + } + ZVAL_EMPTY_STRING(*value_ptr); + return (void*)(*value_ptr); +} +static void new_php_string(zval** value_ptr, const char* str, size_t len) { + SEPARATE_ZVAL_IF_NOT_REF(value_ptr); + if (Z_TYPE_PP(value_ptr) == IS_STRING && + !IS_INTERNED(Z_STRVAL_PP(value_ptr))) { + FREE(Z_STRVAL_PP(value_ptr)); + } + ZVAL_EMPTY_STRING(*value_ptr); + ZVAL_STRINGL(*value_ptr, str, len, 1); +} +#else +static void *empty_php_string2(zval* value_ptr) { + if (Z_TYPE_P(value_ptr) == IS_STRING) { + zend_string_release(Z_STR_P(value_ptr)); + } + ZVAL_EMPTY_STRING(value_ptr); + return value_ptr; +} +static void new_php_string(zval* value_ptr, const char* str, size_t len) { + if (Z_TYPE_P(value_ptr) == IS_STRING) { + zend_string_release(Z_STR_P(value_ptr)); + } + ZVAL_NEW_STR(value_ptr, zend_string_init(str, len, 0)); +} +#endif -// Sets a non-repeated string field in a message. +// Sets a non-repeated string/bytes field in a message. static void* str_handler(void *closure, const void *hd, size_t size_hint) { - MessageHeader* msg = closure; - const size_t *ofs = hd; - return empty_php_string(DEREF(message_data(msg), *ofs, CACHED_VALUE*)); + UPB_UNUSED(hd); + + stringfields_parseframe_t* frame = + (stringfields_parseframe_t*)malloc(sizeof(stringfields_parseframe_t)); + frame->closure = closure; + stringsink_init(&frame->sink); + + return frame; } -// Sets a non-repeated 'bytes' field in a message. -static void* bytes_handler(void *closure, - const void *hd, - size_t size_hint) { - MessageHeader* msg = closure; +static bool str_end_handler(void *closure, const void *hd) { + stringfields_parseframe_t* frame = closure; const size_t *ofs = hd; - return empty_php_string(DEREF(message_data(msg), *ofs, CACHED_VALUE*)); + MessageHeader* msg = (MessageHeader*)frame->closure; + + new_php_string(DEREF(message_data(msg), *ofs, CACHED_VALUE*), + frame->sink.ptr, frame->sink.len); + + stringsink_uninit(&frame->sink); + free(frame); + + return true; } static size_t stringdata_handler(void* closure, const void* hd, const char* str, size_t len, const upb_bufhandle* handle) { - zval* php_str = (zval*)closure; -#if PHP_MAJOR_VERSION < 7 - // Oneof string/bytes fields may have NULL initial value, which doesn't need - // to be freed. - if (Z_TYPE_P(php_str) == IS_STRING && !IS_INTERNED(Z_STRVAL_P(php_str))) { - FREE(Z_STRVAL_P(php_str)); - } - ZVAL_STRINGL(php_str, str, len, 1); -#else - if (Z_TYPE_P(php_str) == IS_STRING) { - zend_string_release(Z_STR_P(php_str)); - } - ZVAL_NEW_STR(php_str, zend_string_init(str, len, 0)); -#endif - return len; + stringfields_parseframe_t* frame = closure; + return stringsink_string(&frame->sink, hd, str, len, handle); } -#if PHP_MAJOR_VERSION >= 7 -static size_t zendstringdata_handler(void* closure, const void* hd, - const char* str, size_t len, - const upb_bufhandle* handle) { - RepeatedField* intern = (RepeatedField*)closure; - - unsigned char memory[NATIVE_SLOT_MAX_SIZE]; - memset(memory, 0, NATIVE_SLOT_MAX_SIZE); - *(zend_string**)memory = zend_string_init(str, len, 0); - - HashTable *ht = PHP_PROTO_HASH_OF(intern->array); - int index = zend_hash_num_elements(ht) - 1; - php_proto_zend_hash_index_update_mem( - ht, index, memory, sizeof(zend_string*), NULL); - - return len; -} -#endif - // Appends a submessage to a repeated field. static void *appendsubmsg_handler(void *closure, const void *hd) { zval* array = (zval*)closure; @@ -420,13 +427,13 @@ static void *submsg_handler(void *closure, const void *hd) { if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(DEREF(message_data(msg), submsgdata->ofs, CACHED_VALUE*))) == IS_NULL) { #if PHP_MAJOR_VERSION < 7 - zval* val = NULL; - MAKE_STD_ZVAL(val); - ZVAL_OBJ(val, subklass->create_object(subklass TSRMLS_CC)); - MessageHeader* intern = UNBOX(MessageHeader, val); + zval val; + ZVAL_OBJ(&val, subklass->create_object(subklass TSRMLS_CC)); + MessageHeader* intern = UNBOX(MessageHeader, &val); custom_data_init(subklass, intern PHP_PROTO_TSRMLS_CC); - php_proto_zval_ptr_dtor(*DEREF(message_data(msg), submsgdata->ofs, zval**)); - *DEREF(message_data(msg), submsgdata->ofs, zval**) = val; + REPLACE_ZVAL_VALUE(DEREF(message_data(msg), submsgdata->ofs, zval**), + &val, 1); + zval_dtor(&val); #else zend_object* obj = subklass->create_object(subklass TSRMLS_CC); ZVAL_OBJ(DEREF(message_data(msg), submsgdata->ofs, zval*), obj); @@ -447,11 +454,6 @@ typedef struct { size_t ofs; upb_fieldtype_t key_field_type; upb_fieldtype_t value_field_type; - - // We know that we can hold this reference because the handlerdata has the - // same lifetime as the upb_handlers struct, and the upb_handlers struct holds - // a reference to the upb_msgdef, which in turn has references to its subdefs. - const upb_def* value_field_subdef; } map_handlerdata_t; // Temporary frame for map parsing: at the beginning of a map entry message, a @@ -656,7 +658,6 @@ static map_handlerdata_t* new_map_handlerdata( value_field = upb_msgdef_itof(mapentry_def, MAP_VALUE_FIELD); assert(value_field != NULL); hd->value_field_type = upb_fielddef_type(value_field); - hd->value_field_subdef = upb_fielddef_subdef(value_field); return hd; } @@ -738,13 +739,38 @@ static void *oneofbytes_handler(void *closure, return empty_php_string(DEREF( message_data(msg), oneofdata->ofs, CACHED_VALUE*)); } +static bool oneofstr_end_handler(void *closure, const void *hd) { + stringfields_parseframe_t* frame = closure; + MessageHeader* msg = (MessageHeader*)frame->closure; + const oneof_handlerdata_t *oneofdata = hd; + + oneof_cleanup(msg, oneofdata); + + DEREF(message_data(msg), oneofdata->case_ofs, uint32_t) = + oneofdata->oneof_case_num; + DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*) = + OBJ_PROP(&msg->std, oneofdata->property_ofs); + + new_php_string(DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*), + frame->sink.ptr, frame->sink.len); + + stringsink_uninit(&frame->sink); + free(frame); + + return true; +} static void *oneofstr_handler(void *closure, const void *hd, size_t size_hint) { - // TODO(teboring): Add it back. - // rb_enc_associate(str, kRubyString8bitEncoding); - return oneofbytes_handler(closure, hd, size_hint); + UPB_UNUSED(hd); + + stringfields_parseframe_t* frame = + (stringfields_parseframe_t*)malloc(sizeof(stringfields_parseframe_t)); + frame->closure = closure; + stringsink_init(&frame->sink); + + return frame; } // Handler for a submessage field in a oneof. @@ -765,9 +791,16 @@ static void* oneofsubmsg_handler(void* closure, const void* hd) { // Create new message. DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*) = OBJ_PROP(&msg->std, oneofdata->property_ofs); - ZVAL_OBJ(CACHED_PTR_TO_ZVAL_PTR( - DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*)), - subklass->create_object(subklass TSRMLS_CC)); +#if PHP_MAJOR_VERSION < 7 + zval val; + ZVAL_OBJ(&val, subklass->create_object(subklass TSRMLS_CC)); + REPLACE_ZVAL_VALUE(DEREF(message_data(msg), oneofdata->ofs, zval**), + &val, 1); + zval_dtor(&val); +#else + zend_object* obj = subklass->create_object(subklass TSRMLS_CC); + ZVAL_OBJ(DEREF(message_data(msg), oneofdata->ofs, zval*), obj); +#endif } DEREF(message_data(msg), oneofdata->case_ofs, uint32_t) = @@ -784,10 +817,9 @@ static void* oneofsubmsg_handler(void* closure, const void* hd) { static void add_handlers_for_repeated_field(upb_handlers *h, const upb_fielddef *f, size_t offset) { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset)); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + attr.handler_data = newhandlerdata(h, offset); upb_handlers_setstartseq(h, f, startseq_handler, &attr); - upb_handlerattr_uninit(&attr); switch (upb_fielddef_type(f)) { @@ -809,22 +841,15 @@ static void add_handlers_for_repeated_field(upb_handlers *h, case UPB_TYPE_STRING: case UPB_TYPE_BYTES: { - bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES; - upb_handlers_setstartstr(h, f, is_bytes ? - appendbytes_handler : appendstr_handler, - NULL); -#if PHP_MAJOR_VERSION < 7 + upb_handlers_setstartstr(h, f, appendstr_handler, NULL); upb_handlers_setstring(h, f, stringdata_handler, NULL); -#else - upb_handlers_setstring(h, f, zendstringdata_handler, NULL); -#endif + upb_handlers_setendstr(h, f, appendstr_end_handler, &attr); break; } case UPB_TYPE_MESSAGE: { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, 0, f)); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + attr.handler_data = newsubmsghandlerdata(h, 0, f); upb_handlers_setstartsubmsg(h, f, appendsubmsg_handler, &attr); - upb_handlerattr_uninit(&attr); break; } } @@ -835,13 +860,12 @@ static void add_handlers_for_singular_field(upb_handlers *h, const upb_fielddef *f, size_t offset) { switch (upb_fielddef_type(f)) { - -#define SET_HANDLER(utype, ltype) \ - case utype: { \ - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; \ - upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset)); \ - upb_handlers_set##ltype(h, f, ltype##_handler, &attr); \ - break; \ +#define SET_HANDLER(utype, ltype) \ + case utype: { \ + upb_handlerattr attr = UPB_HANDLERATTR_INIT; \ + attr.handler_data = newhandlerdata(h, offset); \ + upb_handlers_set##ltype(h, f, ltype##_handler, &attr); \ + break; \ } SET_HANDLER(UPB_TYPE_BOOL, bool); @@ -857,21 +881,17 @@ static void add_handlers_for_singular_field(upb_handlers *h, case UPB_TYPE_STRING: case UPB_TYPE_BYTES: { - bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES; - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset)); - upb_handlers_setstartstr(h, f, - is_bytes ? bytes_handler : str_handler, - &attr); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + attr.handler_data = newhandlerdata(h, offset); + upb_handlers_setstartstr(h, f, str_handler, &attr); upb_handlers_setstring(h, f, stringdata_handler, &attr); - upb_handlerattr_uninit(&attr); + upb_handlers_setendstr(h, f, str_end_handler, &attr); break; } case UPB_TYPE_MESSAGE: { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, offset, f)); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + attr.handler_data = newsubmsghandlerdata(h, offset, f); upb_handlers_setstartsubmsg(h, f, submsg_handler, &attr); - upb_handlerattr_uninit(&attr); break; } } @@ -884,12 +904,11 @@ static void add_handlers_for_mapfield(upb_handlers* h, Descriptor* desc) { const upb_msgdef* map_msgdef = upb_fielddef_msgsubdef(fielddef); map_handlerdata_t* hd = new_map_handlerdata(offset, map_msgdef, desc); - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlers_addcleanup(h, hd, free); - upb_handlerattr_sethandlerdata(&attr, hd); + attr.handler_data = hd; upb_handlers_setstartsubmsg(h, fielddef, startmapentry_handler, &attr); - upb_handlerattr_uninit(&attr); } // Adds handlers to a map-entry msgdef. @@ -898,17 +917,17 @@ static void add_handlers_for_mapentry(const upb_msgdef* msgdef, upb_handlers* h, const upb_fielddef* key_field = map_entry_key(msgdef); const upb_fielddef* value_field = map_entry_value(msgdef); map_handlerdata_t* hd = new_map_handlerdata(0, msgdef, desc); - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlers_addcleanup(h, hd, free); - upb_handlerattr_sethandlerdata(&attr, hd); + attr.handler_data = hd; upb_handlers_setendmsg(h, endmap_handler, &attr); add_handlers_for_singular_field(h, key_field, offsetof(map_parse_frame_data_t, key_storage)); add_handlers_for_singular_field(h, value_field, - offsetof(map_parse_frame_data_t, + offsetof(map_parse_frame_data_t, value_storage)); } @@ -920,10 +939,9 @@ static void add_handlers_for_oneof_field(upb_handlers *h, size_t oneof_case_offset, int property_cache_offset) { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata( - &attr, newoneofhandlerdata(h, offset, oneof_case_offset, - property_cache_offset, m, f)); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + attr.handler_data = newoneofhandlerdata(h, offset, oneof_case_offset, + property_cache_offset, m, f); switch (upb_fielddef_type(f)) { @@ -945,11 +963,9 @@ static void add_handlers_for_oneof_field(upb_handlers *h, case UPB_TYPE_STRING: case UPB_TYPE_BYTES: { - bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES; - upb_handlers_setstartstr(h, f, is_bytes ? - oneofbytes_handler : oneofstr_handler, - &attr); + upb_handlers_setstartstr(h, f, oneofstr_handler, &attr); upb_handlers_setstring(h, f, stringdata_handler, NULL); + upb_handlers_setendstr(h, f, oneofstr_end_handler, &attr); break; } case UPB_TYPE_MESSAGE: { @@ -957,8 +973,6 @@ static void add_handlers_for_oneof_field(upb_handlers *h, break; } } - - upb_handlerattr_uninit(&attr); } static bool add_unknown_handler(void* closure, const void* hd, const char* buf, @@ -979,8 +993,7 @@ static bool add_unknown_handler(void* closure, const void* hd, const char* buf, return true; } -static void add_handlers_for_message(const void* closure, - upb_handlers* h) { +void add_handlers_for_message(const void* closure, upb_handlers* h) { const upb_msgdef* msgdef = upb_handlers_msgdef(h); TSRMLS_FETCH(); Descriptor* desc = @@ -1002,8 +1015,8 @@ static void add_handlers_for_message(const void* closure, desc->layout = create_layout(desc->msgdef); } - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newunknownfieldshandlerdata(h)); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + attr.handler_data = newunknownfieldshandlerdata(h); upb_handlers_setunknown(h, add_unknown_handler, &attr); for (upb_msg_field_begin(&i, desc->msgdef); @@ -1029,75 +1042,53 @@ static void add_handlers_for_message(const void* closure, } } -// Creates upb handlers for populating a message. -static const upb_handlers *new_fill_handlers(Descriptor* desc, - const void* owner) { - // TODO(cfallin, haberman): once upb gets a caching/memoization layer for - // handlers, reuse subdef handlers so that e.g. if we already parse - // B-with-field-of-type-C, we don't have to rebuild the whole hierarchy to - // parse A-with-field-of-type-B-with-field-of-type-C. - return upb_handlers_newfrozen(desc->msgdef, owner, - add_handlers_for_message, NULL); -} - // Constructs the handlers for filling a message's data into an in-memory // object. const upb_handlers* get_fill_handlers(Descriptor* desc) { - if (!desc->fill_handlers) { - desc->fill_handlers = - new_fill_handlers(desc, &desc->fill_handlers); - } - return desc->fill_handlers; -} - -const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor* desc, - const void* owner) { - const upb_handlers* handlers = get_fill_handlers(desc); - upb_pbdecodermethodopts opts; - upb_pbdecodermethodopts_init(&opts, handlers); - - return upb_pbdecodermethod_new(&opts, owner); + return upb_handlercache_get(desc->pool->fill_handler_cache, desc->msgdef); } static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) { - if (desc->fill_method == NULL) { - desc->fill_method = new_fillmsg_decodermethod( - desc, &desc->fill_method); - } - return desc->fill_method; + return upb_pbcodecache_get(desc->pool->fill_method_cache, desc->msgdef); } static const upb_json_parsermethod *msgdef_jsonparsermethod(Descriptor* desc) { - if (desc->json_fill_method == NULL) { - desc->json_fill_method = - upb_json_parsermethod_new(desc->msgdef, &desc->json_fill_method); - } - return desc->json_fill_method; + return upb_json_codecache_get(desc->pool->json_fill_method_cache, desc->msgdef); } // ----------------------------------------------------------------------------- // Serializing. // ----------------------------------------------------------------------------- -static void putmsg(zval* msg, const Descriptor* desc, upb_sink* sink, - int depth TSRMLS_DC); +static void putmsg(zval* msg, const Descriptor* desc, upb_sink sink, + int depth, bool is_json TSRMLS_DC); static void putrawmsg(MessageHeader* msg, const Descriptor* desc, - upb_sink* sink, int depth TSRMLS_DC); - -static void putstr(zval* str, const upb_fielddef* f, upb_sink* sink); + upb_sink sink, int depth, bool is_json, + bool open_msg TSRMLS_DC); +static void putjsonany(MessageHeader* msg, const Descriptor* desc, + upb_sink sink, int depth TSRMLS_DC); +static void putjsonlistvalue( + MessageHeader* msg, const Descriptor* desc, + upb_sink sink, int depth TSRMLS_DC); +static void putjsonstruct( + MessageHeader* msg, const Descriptor* desc, + upb_sink sink, int depth TSRMLS_DC); + +static void putstr(zval* str, const upb_fielddef* f, upb_sink sink, + bool force_default); static void putrawstr(const char* str, int len, const upb_fielddef* f, - upb_sink* sink); + upb_sink sink, bool force_default); -static void putsubmsg(zval* submsg, const upb_fielddef* f, upb_sink* sink, - int depth TSRMLS_DC); +static void putsubmsg(zval* submsg, const upb_fielddef* f, upb_sink sink, + int depth, bool is_json TSRMLS_DC); static void putrawsubmsg(MessageHeader* submsg, const upb_fielddef* f, - upb_sink* sink, int depth TSRMLS_DC); + upb_sink sink, int depth, bool is_json TSRMLS_DC); -static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink, - int depth TSRMLS_DC); -static void putmap(zval* map, const upb_fielddef* f, upb_sink* sink, - int depth TSRMLS_DC); +static void putarray(zval* array, const upb_fielddef* f, upb_sink sink, + int depth, bool is_json TSRMLS_DC); +static void putmap(zval* map, const upb_fielddef* f, upb_sink sink, + int depth, bool is_json TSRMLS_DC); static upb_selector_t getsel(const upb_fielddef* f, upb_handlertype_t type) { upb_selector_t ret; @@ -1106,15 +1097,17 @@ static upb_selector_t getsel(const upb_fielddef* f, upb_handlertype_t type) { return ret; } -static void put_optional_value(const void* memory, int len, const upb_fielddef* f, - int depth, upb_sink* sink TSRMLS_DC) { +static void put_optional_value(const void* memory, int len, + const upb_fielddef* f, + int depth, upb_sink sink, + bool is_json TSRMLS_DC) { assert(upb_fielddef_label(f) == UPB_LABEL_OPTIONAL); switch (upb_fielddef_type(f)) { #define T(upbtypeconst, upbtype, ctype, default_value) \ case upbtypeconst: { \ ctype value = DEREF(memory, 0, ctype); \ - if (value != default_value) { \ + if (is_json || value != default_value) { \ upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); \ upb_sink_put##upbtype(sink, sel, value); \ } \ @@ -1132,7 +1125,7 @@ static void put_optional_value(const void* memory, int len, const upb_fielddef* #undef T case UPB_TYPE_STRING: case UPB_TYPE_BYTES: - putrawstr(memory, len, f, sink); + putrawstr(memory, len, f, sink, is_json); break; case UPB_TYPE_MESSAGE: { #if PHP_MAJOR_VERSION < 7 @@ -1142,7 +1135,7 @@ static void put_optional_value(const void* memory, int len, const upb_fielddef* (MessageHeader*)((char*)(*(zend_object**)memory) - XtOffsetOf(MessageHeader, std)); #endif - putrawsubmsg(submsg, f, sink, depth TSRMLS_CC); + putrawsubmsg(submsg, f, sink, depth, is_json TSRMLS_CC); break; } default: @@ -1180,8 +1173,8 @@ static int raw_value_len(void* memory, int len, const upb_fielddef* f) { } } -static void putmap(zval* map, const upb_fielddef* f, upb_sink* sink, - int depth TSRMLS_DC) { +static void putmap(zval* map, const upb_fielddef* f, upb_sink sink, + int depth, bool is_json TSRMLS_DC) { upb_sink subsink; const upb_fielddef* key_field; const upb_fielddef* value_field; @@ -1203,39 +1196,210 @@ static void putmap(zval* map, const upb_fielddef* f, upb_sink* sink, upb_status status; upb_sink entry_sink; - upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG), + upb_sink_startsubmsg(subsink, getsel(f, UPB_HANDLER_STARTSUBMSG), &entry_sink); - upb_sink_startmsg(&entry_sink); + upb_sink_startmsg(entry_sink); // Serialize key. const char *key = map_iter_key(&it, &len); - put_optional_value(key, len, key_field, depth + 1, &entry_sink TSRMLS_CC); + put_optional_value(key, len, key_field, depth + 1, + entry_sink, is_json TSRMLS_CC); // Serialize value. upb_value value = map_iter_value(&it, &len); put_optional_value(raw_value(upb_value_memory(&value), value_field), raw_value_len(upb_value_memory(&value), len, value_field), - value_field, depth + 1, &entry_sink TSRMLS_CC); + value_field, depth + 1, entry_sink, is_json TSRMLS_CC); - upb_sink_endmsg(&entry_sink, &status); - upb_sink_endsubmsg(&subsink, getsel(f, UPB_HANDLER_ENDSUBMSG)); + upb_sink_endmsg(entry_sink, &status); + upb_sink_endsubmsg(subsink, getsel(f, UPB_HANDLER_ENDSUBMSG)); } upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); } -static void putmsg(zval* msg_php, const Descriptor* desc, upb_sink* sink, - int depth TSRMLS_DC) { +static void putmsg(zval* msg_php, const Descriptor* desc, upb_sink sink, + int depth, bool is_json TSRMLS_DC) { MessageHeader* msg = UNBOX(MessageHeader, msg_php); - putrawmsg(msg, desc, sink, depth TSRMLS_CC); + putrawmsg(msg, desc, sink, depth, is_json, true TSRMLS_CC); +} + +static const upb_handlers* msgdef_json_serialize_handlers( + Descriptor* desc, bool preserve_proto_fieldnames); + +static void putjsonany(MessageHeader* msg, const Descriptor* desc, + upb_sink sink, int depth TSRMLS_DC) { + upb_status status; + const upb_fielddef* type_field = upb_msgdef_itof(desc->msgdef, UPB_ANY_TYPE); + const upb_fielddef* value_field = upb_msgdef_itof(desc->msgdef, UPB_ANY_VALUE); + + uint32_t type_url_offset; + zval* type_url_php_str; + const upb_msgdef *payload_type = NULL; + + upb_sink_startmsg(sink); + + /* Handle type url */ + type_url_offset = desc->layout->fields[upb_fielddef_index(type_field)].offset; + type_url_php_str = CACHED_PTR_TO_ZVAL_PTR( + DEREF(message_data(msg), type_url_offset, CACHED_VALUE*)); + if (Z_STRLEN_P(type_url_php_str) > 0) { + putstr(type_url_php_str, type_field, sink, false); + } + + { + const char* type_url_str = Z_STRVAL_P(type_url_php_str); + size_t type_url_len = Z_STRLEN_P(type_url_php_str); + if (type_url_len <= 20 || + strncmp(type_url_str, "type.googleapis.com/", 20) != 0) { + zend_error(E_ERROR, "Invalid type url: %s", type_url_str); + } + + /* Resolve type url */ + type_url_str += 20; + type_url_len -= 20; + + payload_type = upb_symtab_lookupmsg2( + generated_pool->symtab, type_url_str, type_url_len); + if (payload_type == NULL) { + zend_error(E_ERROR, "Unknown type: %s", type_url_str); + return; + } + } + + { + uint32_t value_offset; + zval* value_php_str; + const char* value_str; + size_t value_len; + + value_offset = desc->layout->fields[upb_fielddef_index(value_field)].offset; + value_php_str = CACHED_PTR_TO_ZVAL_PTR( + DEREF(message_data(msg), value_offset, CACHED_VALUE*)); + value_str = Z_STRVAL_P(value_php_str); + value_len = Z_STRLEN_P(value_php_str); + + if (value_len > 0) { + Descriptor* payload_desc = + UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj((void*)payload_type)); + zend_class_entry* payload_klass = payload_desc->klass; + zval val; + upb_sink subsink; + bool is_wellknown; + + /* Create message of the payload type. */ + ZVAL_OBJ(&val, payload_klass->create_object(payload_klass TSRMLS_CC)); + MessageHeader* intern = UNBOX(MessageHeader, &val); + custom_data_init(payload_klass, intern PHP_PROTO_TSRMLS_CC); + + merge_from_string(value_str, value_len, payload_desc, intern); + + is_wellknown = + upb_msgdef_wellknowntype(payload_desc->msgdef) != + UPB_WELLKNOWN_UNSPECIFIED; + if (is_wellknown) { + upb_sink_startstr(sink, getsel(value_field, UPB_HANDLER_STARTSTR), 0, + &subsink); + } + + subsink.handlers = + msgdef_json_serialize_handlers(payload_desc, true); + subsink.closure = sink.closure; + putrawmsg(intern, payload_desc, subsink, depth, true, + is_wellknown TSRMLS_CC); + + zval_dtor(&val); + } + } + + upb_sink_endmsg(sink, &status); +} + +static void putjsonlistvalue( + MessageHeader* msg, const Descriptor* desc, + upb_sink sink, int depth TSRMLS_DC) { + upb_status status; + upb_sink subsink; + const upb_fielddef* f = upb_msgdef_itof(desc->msgdef, 1); + uint32_t offset = desc->layout->fields[upb_fielddef_index(f)].offset; + zval* array; + RepeatedField* intern; + HashTable *ht; + int size, i; + + upb_sink_startmsg(sink); + + array = CACHED_PTR_TO_ZVAL_PTR( + DEREF(message_data(msg), offset, CACHED_VALUE*)); + intern = UNBOX(RepeatedField, array); + ht = PHP_PROTO_HASH_OF(intern->array); + size = zend_hash_num_elements(ht); + + if (size == 0) { + upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink); + upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); + } else { + putarray(array, f, sink, depth, true TSRMLS_CC); + } + + upb_sink_endmsg(sink, &status); +} + +static void putjsonstruct( + MessageHeader* msg, const Descriptor* desc, + upb_sink sink, int depth TSRMLS_DC) { + upb_status status; + upb_sink subsink; + const upb_fielddef* f = upb_msgdef_itof(desc->msgdef, 1); + uint32_t offset = desc->layout->fields[upb_fielddef_index(f)].offset; + zval* map; + Map* intern; + int size; + + upb_sink_startmsg(sink); + + map = CACHED_PTR_TO_ZVAL_PTR( + DEREF(message_data(msg), offset, CACHED_VALUE*)); + intern = UNBOX(Map, map); + size = upb_strtable_count(&intern->table); + + if (size == 0) { + upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink); + upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); + } else { + putmap(map, f, sink, depth, true TSRMLS_CC); + } + + upb_sink_endmsg(sink, &status); } static void putrawmsg(MessageHeader* msg, const Descriptor* desc, - upb_sink* sink, int depth TSRMLS_DC) { + upb_sink sink, int depth, bool is_json, + bool open_msg TSRMLS_DC) { upb_msg_field_iter i; upb_status status; - upb_sink_startmsg(sink); + if (is_json && + upb_msgdef_wellknowntype(desc->msgdef) == UPB_WELLKNOWN_ANY) { + putjsonany(msg, desc, sink, depth TSRMLS_CC); + return; + } + + if (is_json && + upb_msgdef_wellknowntype(desc->msgdef) == UPB_WELLKNOWN_LISTVALUE) { + putjsonlistvalue(msg, desc, sink, depth TSRMLS_CC); + return; + } + + if (is_json && + upb_msgdef_wellknowntype(desc->msgdef) == UPB_WELLKNOWN_STRUCT) { + putjsonstruct(msg, desc, sink, depth TSRMLS_CC); + return; + } + + if (open_msg) { + upb_sink_startmsg(sink); + } // Protect against cycles (possible because users may freely reassign message // and repeated fields) by imposing a maximum recursion depth. @@ -1268,31 +1432,34 @@ static void putrawmsg(MessageHeader* msg, const Descriptor* desc, zval* map = CACHED_PTR_TO_ZVAL_PTR( DEREF(message_data(msg), offset, CACHED_VALUE*)); if (map != NULL) { - putmap(map, f, sink, depth TSRMLS_CC); + putmap(map, f, sink, depth, is_json TSRMLS_CC); } } else if (upb_fielddef_isseq(f)) { zval* array = CACHED_PTR_TO_ZVAL_PTR( DEREF(message_data(msg), offset, CACHED_VALUE*)); if (array != NULL) { - putarray(array, f, sink, depth TSRMLS_CC); + putarray(array, f, sink, depth, is_json TSRMLS_CC); } } else if (upb_fielddef_isstring(f)) { zval* str = CACHED_PTR_TO_ZVAL_PTR( DEREF(message_data(msg), offset, CACHED_VALUE*)); - if (containing_oneof || Z_STRLEN_P(str) > 0) { - putstr(str, f, sink); + if (containing_oneof || (is_json && is_wrapper_msg(desc->msgdef)) || + Z_STRLEN_P(str) > 0) { + putstr(str, f, sink, is_json && is_wrapper_msg(desc->msgdef)); } } else if (upb_fielddef_issubmsg(f)) { putsubmsg(CACHED_PTR_TO_ZVAL_PTR( DEREF(message_data(msg), offset, CACHED_VALUE*)), - f, sink, depth TSRMLS_CC); + f, sink, depth, is_json TSRMLS_CC); } else { upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); #define T(upbtypeconst, upbtype, ctype, default_value) \ case upbtypeconst: { \ ctype value = DEREF(message_data(msg), offset, ctype); \ - if (containing_oneof || value != default_value) { \ + if (containing_oneof || \ + (is_json && is_wrapper_msg(desc->msgdef)) || \ + value != default_value) { \ upb_sink_put##upbtype(sink, sel, value); \ } \ } break; @@ -1322,10 +1489,13 @@ static void putrawmsg(MessageHeader* msg, const Descriptor* desc, upb_sink_putunknown(sink, unknown->ptr, unknown->len); } - upb_sink_endmsg(sink, &status); + if (open_msg) { + upb_sink_endmsg(sink, &status); + } } -static void putstr(zval* str, const upb_fielddef *f, upb_sink *sink) { +static void putstr(zval* str, const upb_fielddef *f, + upb_sink sink, bool force_default) { upb_sink subsink; if (ZVAL_IS_NULL(str)) return; @@ -1336,7 +1506,7 @@ static void putstr(zval* str, const upb_fielddef *f, upb_sink *sink) { &subsink); // For oneof string field, we may get here with string length is zero. - if (Z_STRLEN_P(str) > 0) { + if (Z_STRLEN_P(str) > 0 || force_default) { // Ensure that the string has the correct encoding. We also check at // field-set time, but the user may have mutated the string object since // then. @@ -1345,7 +1515,7 @@ static void putstr(zval* str, const upb_fielddef *f, upb_sink *sink) { zend_error(E_USER_ERROR, "Given string is not UTF8 encoded."); return; } - upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), Z_STRVAL_P(str), + upb_sink_putstring(subsink, getsel(f, UPB_HANDLER_STRING), Z_STRVAL_P(str), Z_STRLEN_P(str), NULL); } @@ -1353,10 +1523,10 @@ static void putstr(zval* str, const upb_fielddef *f, upb_sink *sink) { } static void putrawstr(const char* str, int len, const upb_fielddef* f, - upb_sink* sink) { + upb_sink sink, bool force_default) { upb_sink subsink; - if (len == 0) return; + if (len == 0 && !force_default) return; // Ensure that the string has the correct encoding. We also check at field-set // time, but the user may have mutated the string object since then. @@ -1367,32 +1537,32 @@ static void putrawstr(const char* str, int len, const upb_fielddef* f, } upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), len, &subsink); - upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), str, len, NULL); + upb_sink_putstring(subsink, getsel(f, UPB_HANDLER_STRING), str, len, NULL); upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR)); } -static void putsubmsg(zval* submsg_php, const upb_fielddef* f, upb_sink* sink, - int depth TSRMLS_DC) { +static void putsubmsg(zval* submsg_php, const upb_fielddef* f, upb_sink sink, + int depth, bool is_json TSRMLS_DC) { if (Z_TYPE_P(submsg_php) == IS_NULL) return; MessageHeader *submsg = UNBOX(MessageHeader, submsg_php); - putrawsubmsg(submsg, f, sink, depth TSRMLS_CC); + putrawsubmsg(submsg, f, sink, depth, is_json TSRMLS_CC); } static void putrawsubmsg(MessageHeader* submsg, const upb_fielddef* f, - upb_sink* sink, int depth TSRMLS_DC) { + upb_sink sink, int depth, bool is_json TSRMLS_DC) { upb_sink subsink; Descriptor* subdesc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(upb_fielddef_msgsubdef(f))); upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink); - putrawmsg(submsg, subdesc, &subsink, depth + 1 TSRMLS_CC); + putrawmsg(submsg, subdesc, subsink, depth + 1, is_json, true TSRMLS_CC); upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG)); } -static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink, - int depth TSRMLS_DC) { +static void putarray(zval* array, const upb_fielddef* f, upb_sink sink, + int depth, bool is_json TSRMLS_DC) { upb_sink subsink; upb_fieldtype_t type = upb_fielddef_type(f); upb_selector_t sel = 0; @@ -1415,7 +1585,7 @@ static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink, switch (type) { #define T(upbtypeconst, upbtype, ctype) \ case upbtypeconst: \ - upb_sink_put##upbtype(&subsink, sel, *((ctype*)memory)); \ + upb_sink_put##upbtype(subsink, sel, *((ctype*)memory)); \ break; T(UPB_TYPE_FLOAT, float, float) @@ -1436,7 +1606,8 @@ static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink, const char* rawstr = ZSTR_VAL(*(zend_string**)memory); int len = ZSTR_LEN(*(zend_string**)memory); #endif - putrawstr(rawstr, len, f, &subsink); + putrawstr(rawstr, len, f, subsink, + is_json && is_wrapper_msg(upb_fielddef_containingtype(f))); break; } case UPB_TYPE_MESSAGE: { @@ -1447,7 +1618,7 @@ static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink, (MessageHeader*)((char*)(Z_OBJ_P((zval*)memory)) - XtOffsetOf(MessageHeader, std)); #endif - putrawsubmsg(submsg, f, &subsink, depth TSRMLS_CC); + putrawsubmsg(submsg, f, subsink, depth, is_json TSRMLS_CC); break; } @@ -1458,29 +1629,18 @@ static void putarray(zval* array, const upb_fielddef* f, upb_sink* sink, } static const upb_handlers* msgdef_pb_serialize_handlers(Descriptor* desc) { - if (desc->pb_serialize_handlers == NULL) { - desc->pb_serialize_handlers = - upb_pb_encoder_newhandlers(desc->msgdef, &desc->pb_serialize_handlers); - } - return desc->pb_serialize_handlers; + return upb_handlercache_get(desc->pool->pb_serialize_handler_cache, + desc->msgdef); } static const upb_handlers* msgdef_json_serialize_handlers( Descriptor* desc, bool preserve_proto_fieldnames) { if (preserve_proto_fieldnames) { - if (desc->json_serialize_handlers == NULL) { - desc->json_serialize_handlers = - upb_json_printer_newhandlers( - desc->msgdef, true, &desc->json_serialize_handlers); - } - return desc->json_serialize_handlers; + return upb_handlercache_get( + desc->pool->json_serialize_handler_preserve_cache, desc->msgdef); } else { - if (desc->json_serialize_handlers_preserve == NULL) { - desc->json_serialize_handlers_preserve = - upb_json_printer_newhandlers( - desc->msgdef, false, &desc->json_serialize_handlers_preserve); - } - return desc->json_serialize_handlers_preserve; + return upb_handlercache_get(desc->pool->json_serialize_handler_cache, + desc->msgdef); } } @@ -1502,9 +1662,9 @@ void serialize_to_string(zval* val, zval* return_value TSRMLS_DC) { upb_pb_encoder* encoder; stackenv_init(&se, "Error occurred during encoding: %s"); - encoder = upb_pb_encoder_create(&se.env, serialize_handlers, &sink.sink); + encoder = upb_pb_encoder_create(se.arena, serialize_handlers, sink.sink); - putmsg(val, desc, upb_pb_encoder_input(encoder), 0 TSRMLS_CC); + putmsg(val, desc, upb_pb_encoder_input(encoder), 0, false TSRMLS_CC); PHP_PROTO_RETVAL_STRINGL(sink.ptr, sink.len, 1); @@ -1517,7 +1677,7 @@ PHP_METHOD(Message, serializeToString) { serialize_to_string(getThis(), return_value TSRMLS_CC); } -void merge_from_string(const char* data, int data_len, const Descriptor* desc, +void merge_from_string(const char* data, int data_len, Descriptor* desc, MessageHeader* msg) { const upb_pbdecodermethod* method = msgdef_decodermethod(desc); const upb_handlers* h = upb_pbdecodermethod_desthandlers(method); @@ -1527,7 +1687,7 @@ void merge_from_string(const char* data, int data_len, const Descriptor* desc, stackenv_init(&se, "Error occurred during parsing: %s"); upb_sink_reset(&sink, h, msg); - decoder = upb_pbdecoder_create(&se.env, method, &sink); + decoder = upb_pbdecoder_create(se.arena, method, sink, &se.status); upb_bufsrc_putbuf(data, data_len, upb_pbdecoder_input(decoder)); stackenv_uninit(&se); @@ -1569,9 +1729,9 @@ PHP_METHOD(Message, serializeToJsonString) { stackenv se; stackenv_init(&se, "Error occurred during encoding: %s"); - printer = upb_json_printer_create(&se.env, serialize_handlers, &sink.sink); + printer = upb_json_printer_create(se.arena, serialize_handlers, sink.sink); - putmsg(getThis(), desc, upb_json_printer_input(printer), 0 TSRMLS_CC); + putmsg(getThis(), desc, upb_json_printer_input(printer), 0, true TSRMLS_CC); PHP_PROTO_RETVAL_STRINGL(sink.ptr, sink.len, 1); @@ -1610,7 +1770,8 @@ PHP_METHOD(Message, mergeFromJsonString) { stackenv_init(&se, "Error occurred during parsing: %s"); upb_sink_reset(&sink, get_fill_handlers(desc), msg); - parser = upb_json_parser_create(&se.env, method, &sink, ignore_json_unknown); + parser = upb_json_parser_create(se.arena, method, generated_pool->symtab, + sink, &se.status, ignore_json_unknown); upb_bufsrc_putbuf(data, data_len, upb_json_parser_input(parser)); stackenv_uninit(&se); @@ -1668,7 +1829,7 @@ static void discard_unknown_fields(MessageHeader* msg) { for (map_begin(map_php, &map_it TSRMLS_CC); !map_done(&map_it); map_next(&map_it)) { upb_value value = map_iter_value(&map_it, &len); - void* memory = raw_value(upb_value_memory(&value), value_field); + const void* memory = raw_value(upb_value_memory(&value), value_field); #if PHP_MAJOR_VERSION < 7 MessageHeader *submsg = UNBOX(MessageHeader, *(zval**)memory); #else diff --git a/php/ext/google/protobuf/map.c b/php/ext/google/protobuf/map.c index c5b500b53e..0ce10190e6 100644 --- a/php/ext/google/protobuf/map.c +++ b/php/ext/google/protobuf/map.c @@ -167,7 +167,8 @@ static void map_begin_internal(Map *map, MapIter *iter) { upb_strtable_begin(&iter->it, &map->table); } -static HashTable *map_field_get_gc(zval *object, CACHED_VALUE **table, int *n) { +static HashTable *map_field_get_gc(zval *object, CACHED_VALUE **table, + int *n TSRMLS_DC) { // TODO(teboring): Unfortunately, zend engine does not support garbage // collection for custom array. We have to use zend engine's native array // instead. @@ -294,7 +295,7 @@ static bool map_field_read_dimension(zval *object, zval *key, int type, } } -static bool map_index_unset(Map *intern, const char* keyval, int length) { +static void map_index_unset(Map *intern, const char* keyval, int length) { upb_value old_value; if (upb_strtable_remove2(&intern->table, keyval, length, &old_value)) { switch (intern->value_type) { diff --git a/php/ext/google/protobuf/message.c b/php/ext/google/protobuf/message.c index 0efe090d52..43561551bd 100644 --- a/php/ext/google/protobuf/message.c +++ b/php/ext/google/protobuf/message.c @@ -29,9 +29,16 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include #include #include +#if PHP_MAJOR_VERSION < 7 +#include +#else +#include +#endif + #include "protobuf.h" #include "utf8.h" @@ -83,7 +90,7 @@ static HashTable* message_get_properties(zval* object TSRMLS_DC); // Define object free method. PHP_PROTO_OBJECT_FREE_START(MessageHeader, message) if (*(void**)intern->data != NULL) { - stringsink_uninit(*(void**)intern->data); + stringsink_uninit_opaque(*(void**)intern->data); FREE(*(void**)intern->data); } FREE(intern->data); @@ -254,6 +261,16 @@ void custom_data_init(const zend_class_entry* ce, &intern->std PHP_PROTO_TSRMLS_CC); } +#define INIT_MESSAGE_WITH_ARRAY \ + { \ + zval* array_wrapper = NULL; \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \ + "|a!", &array_wrapper) == FAILURE) { \ + return; \ + } \ + Message_construct(getThis(), array_wrapper); \ + } + void build_class_from_descriptor( PHP_PROTO_HASHTABLE_VALUE php_descriptor TSRMLS_DC) { Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, php_descriptor); @@ -277,6 +294,57 @@ void build_class_from_descriptor( // PHP Methods // ----------------------------------------------------------------------------- +static bool is_wrapper_msg(const upb_msgdef* m) { + upb_wellknowntype_t type = upb_msgdef_wellknowntype(m); + return type >= UPB_WELLKNOWN_DOUBLEVALUE && + type <= UPB_WELLKNOWN_BOOLVALUE; +} + +static void append_wrapper_message( + zend_class_entry* subklass, RepeatedField* intern, zval* value TSRMLS_DC) { + MessageHeader* submsg; + const upb_fielddef* field; +#if PHP_MAJOR_VERSION < 7 + zval* val = NULL; + MAKE_STD_ZVAL(val); + ZVAL_OBJ(val, subklass->create_object(subklass TSRMLS_CC)); + repeated_field_push_native(intern, &val); + submsg = UNBOX(MessageHeader, val); +#else + zend_object* obj = subklass->create_object(subklass TSRMLS_CC); + repeated_field_push_native(intern, &obj); + submsg = (MessageHeader*)((char*)obj - XtOffsetOf(MessageHeader, std)); +#endif + custom_data_init(subklass, submsg PHP_PROTO_TSRMLS_CC); + + field = upb_msgdef_itof(submsg->descriptor->msgdef, 1); + layout_set(submsg->descriptor->layout, submsg, field, value TSRMLS_CC); +} + +static void set_wrapper_message_as_map_value( + zend_class_entry* subklass, zval* map, zval* key, zval* value TSRMLS_DC) { + MessageHeader* submsg; + const upb_fielddef* field; +#if PHP_MAJOR_VERSION < 7 + zval* val = NULL; + MAKE_STD_ZVAL(val); + ZVAL_OBJ(val, subklass->create_object(subklass TSRMLS_CC)); + map_field_handlers->write_dimension( + map, key, val TSRMLS_CC); + submsg = UNBOX(MessageHeader, val); +#else + zval val; + zend_object* obj = subklass->create_object(subklass TSRMLS_CC); + ZVAL_OBJ(&val, obj); + map_field_handlers->write_dimension(map, key, &val TSRMLS_CC); + submsg = (MessageHeader*)((char*)obj - XtOffsetOf(MessageHeader, std)); +#endif + custom_data_init(subklass, submsg PHP_PROTO_TSRMLS_CC); + + field = upb_msgdef_itof(submsg->descriptor->msgdef, 1); + layout_set(submsg->descriptor->layout, submsg, field, value TSRMLS_CC); +} + void Message_construct(zval* msg, zval* array_wrapper) { TSRMLS_FETCH(); zend_class_entry* ce = Z_OBJCE_P(msg); @@ -302,6 +370,11 @@ void Message_construct(zval* msg, zval* array_wrapper) { zend_hash_move_forward_ex(array, &pointer)) { zend_hash_get_current_key_zval_ex(array, &key, &pointer); field = upb_msgdef_ntofz(intern->descriptor->msgdef, Z_STRVAL_P(&key)); +#if PHP_MAJOR_VERSION >= 7 + if (Z_ISREF_P((CACHED_VALUE*)value)) { + value = Z_REFVAL_P((CACHED_VALUE*)value); + } +#endif if (field == NULL) { zend_error(E_USER_ERROR, "Unknown field: %s", Z_STRVAL_P(&key)); } @@ -314,14 +387,38 @@ void Message_construct(zval* msg, zval* array_wrapper) { HashPosition subpointer; zval subkey; void* memory; + bool is_wrapper = false; + zend_class_entry* subklass = NULL; + const upb_msgdef* mapentry = upb_fielddef_msgsubdef(field); + const upb_fielddef *value_field = upb_msgdef_itof(mapentry, 2); + + if (upb_fielddef_issubmsg(value_field)) { + const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(value_field); + upb_wellknowntype_t type = upb_msgdef_wellknowntype(submsgdef); + is_wrapper = is_wrapper_msg(submsgdef); + + if (is_wrapper) { + PHP_PROTO_HASHTABLE_VALUE subdesc_php = get_def_obj(submsgdef); + Descriptor* subdesc = UNBOX_HASHTABLE_VALUE(Descriptor, subdesc_php); + subklass = subdesc->klass; + } + } + for (zend_hash_internal_pointer_reset_ex(subtable, &subpointer); php_proto_zend_hash_get_current_data_ex(subtable, (void**)&memory, &subpointer) == SUCCESS; zend_hash_move_forward_ex(subtable, &subpointer)) { zend_hash_get_current_key_zval_ex(subtable, &subkey, &subpointer); - map_field_handlers->write_dimension( - submap, &subkey, - CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC); + if (is_wrapper && + Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory)) != IS_OBJECT) { + set_wrapper_message_as_map_value( + subklass, submap, &subkey, + CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC); + } else { + map_field_handlers->write_dimension( + submap, &subkey, + CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC); + } zval_dtor(&subkey); } } else if (upb_fielddef_isseq(field)) { @@ -332,44 +429,90 @@ void Message_construct(zval* msg, zval* array_wrapper) { CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value)); HashPosition subpointer; void* memory; + bool is_wrapper = false; + zend_class_entry* subklass = NULL; + + if (upb_fielddef_issubmsg(field)) { + const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field); + upb_wellknowntype_t type = upb_msgdef_wellknowntype(submsgdef); + is_wrapper = is_wrapper_msg(submsgdef); + + if (is_wrapper) { + PHP_PROTO_HASHTABLE_VALUE subdesc_php = get_def_obj(submsgdef); + Descriptor* subdesc = UNBOX_HASHTABLE_VALUE(Descriptor, subdesc_php); + subklass = subdesc->klass; + } + } + for (zend_hash_internal_pointer_reset_ex(subtable, &subpointer); php_proto_zend_hash_get_current_data_ex(subtable, (void**)&memory, &subpointer) == SUCCESS; zend_hash_move_forward_ex(subtable, &subpointer)) { - repeated_field_handlers->write_dimension( - subarray, NULL, - CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC); + if (is_wrapper && + Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory)) != IS_OBJECT) { + RepeatedField* intern = UNBOX(RepeatedField, subarray); + append_wrapper_message( + subklass, intern, + CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC); + } else { + repeated_field_handlers->write_dimension( + subarray, NULL, + CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC); + } } } else if (upb_fielddef_issubmsg(field)) { const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field); PHP_PROTO_HASHTABLE_VALUE desc_php = get_def_obj(submsgdef); Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, desc_php); - zend_property_info* property_info; - PHP_PROTO_FAKE_SCOPE_BEGIN(Z_OBJCE_P(msg)); + + CACHED_VALUE* cached = NULL; + if (upb_fielddef_containingoneof(field)) { + void* memory = slot_memory(intern->descriptor->layout, + message_data(intern), field); + int property_cache_index = + intern->descriptor->layout->fields[upb_fielddef_index(field)] + .cache_index; + cached = OBJ_PROP(Z_OBJ_P(msg), property_cache_index); + *(CACHED_VALUE**)(memory) = cached; + } else { + zend_property_info* property_info; + PHP_PROTO_FAKE_SCOPE_BEGIN(Z_OBJCE_P(msg)); #if PHP_MAJOR_VERSION < 7 - property_info = - zend_get_property_info(Z_OBJCE_P(msg), &key, true TSRMLS_CC); + property_info = + zend_get_property_info(Z_OBJCE_P(msg), &key, true TSRMLS_CC); #else - property_info = - zend_get_property_info(Z_OBJCE_P(msg), Z_STR_P(&key), true); + property_info = + zend_get_property_info(Z_OBJCE_P(msg), Z_STR_P(&key), true); #endif - PHP_PROTO_FAKE_SCOPE_END; - CACHED_VALUE* cached = OBJ_PROP(Z_OBJ_P(msg), property_info->offset); + PHP_PROTO_FAKE_SCOPE_END; + cached = OBJ_PROP(Z_OBJ_P(msg), property_info->offset); + } #if PHP_MAJOR_VERSION < 7 SEPARATE_ZVAL_IF_NOT_REF(cached); #endif zval* submsg = CACHED_PTR_TO_ZVAL_PTR(cached); ZVAL_OBJ(submsg, desc->klass->create_object(desc->klass TSRMLS_CC)); Message_construct(submsg, NULL); - MessageHeader* from = UNBOX(MessageHeader, - CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value)); MessageHeader* to = UNBOX(MessageHeader, submsg); - if(from->descriptor != to->descriptor) { - zend_error(E_USER_ERROR, "Cannot merge messages with different class."); - return; + const upb_filedef *file = upb_msgdef_file(submsgdef); + if (!strcmp(upb_filedef_name(file), "google/protobuf/wrappers.proto") && + Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value)) != IS_OBJECT) { + const upb_fielddef *value_field = upb_msgdef_itof(submsgdef, 1); + layout_set(to->descriptor->layout, to, + value_field, CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value) + TSRMLS_CC); + } else { + MessageHeader* from = + UNBOX(MessageHeader, + CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value)); + if(from->descriptor != to->descriptor) { + zend_error(E_USER_ERROR, + "Cannot merge messages with different class."); + return; + } + + layout_merge(from->descriptor->layout, from, to TSRMLS_CC); } - - layout_merge(from->descriptor->layout, from, to TSRMLS_CC); } else { message_set_property_internal(msg, &key, CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value) TSRMLS_CC); @@ -382,14 +525,7 @@ void Message_construct(zval* msg, zval* array_wrapper) { // modified. As a result, the first created instance will be a normal zend // object. Here, we manually modify it to our message in such a case. PHP_METHOD(Message, __construct) { - // Init message with array - zval* array_wrapper = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, - "|a!", &array_wrapper) == FAILURE) { - return; - } - - Message_construct(getThis(), array_wrapper); + INIT_MESSAGE_WITH_ARRAY; } PHP_METHOD(Message, clear) { @@ -565,7 +701,8 @@ static void init_file_any(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_any = true; } @@ -605,7 +742,8 @@ static void init_file_api(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_api = true; } @@ -625,7 +763,8 @@ static void init_file_duration(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_duration = true; } @@ -645,7 +784,8 @@ static void init_file_field_mask(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_field_mask = true; } @@ -664,7 +804,8 @@ static void init_file_empty(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_empty = true; } @@ -685,7 +826,8 @@ static void init_file_source_context(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_source_context = true; } @@ -719,7 +861,8 @@ static void init_file_struct(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_struct = true; } @@ -739,7 +882,8 @@ static void init_file_timestamp(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_timestamp = true; } @@ -807,7 +951,8 @@ static void init_file_type(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_type = true; } @@ -834,7 +979,8 @@ static void init_file_wrappers(TSRMLS_D) { char* binary; int binary_len; hex_to_binary(generated_file, &binary, &binary_len); - internal_add_generated_file(binary, binary_len, generated_pool TSRMLS_CC); + internal_add_generated_file(binary, binary_len, + generated_pool, true TSRMLS_CC); FREE(binary); is_inited_file_wrappers = true; } @@ -848,6 +994,8 @@ static void init_file_wrappers(TSRMLS_D) { // ----------------------------------------------------------------------------- static zend_function_entry field_cardinality_methods[] = { + PHP_ME(Field_Cardinality, name, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(Field_Cardinality, value, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) {NULL, NULL, NULL} }; @@ -872,11 +1020,58 @@ PHP_PROTO_INIT_ENUMCLASS_START("Google\\Protobuf\\Field\\Cardinality", #endif PHP_PROTO_INIT_ENUMCLASS_END +PHP_METHOD(Field_Cardinality, name) { + PHP_PROTO_LONG value; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &value) == + FAILURE) { + return; + } + switch (value) { + case 0: + PHP_PROTO_RETURN_STRING("CARDINALITY_UNKNOWN", 1); + case 1: + PHP_PROTO_RETURN_STRING("CARDINALITY_OPTIONAL", 1); + case 2: + PHP_PROTO_RETURN_STRING("CARDINALITY_REQUIRED", 1); + case 3: + PHP_PROTO_RETURN_STRING("CARDINALITY_REPEATED", 1); + default: + zend_throw_exception_ex( + NULL, 0 TSRMLS_CC, + "Enum Google\\Protobuf\\Field_Cardinality has no name " + "defined for value %d.", + value); + } +} + +PHP_METHOD(Field_Cardinality, value) { + char *name = NULL; + PHP_PROTO_SIZE name_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == + FAILURE) { + return; + } + + if (strncmp(name, "CARDINALITY_UNKNOWN", name_len) == 0) RETURN_LONG(0); + if (strncmp(name, "CARDINALITY_OPTIONAL", name_len) == 0) RETURN_LONG(1); + if (strncmp(name, "CARDINALITY_REQUIRED", name_len) == 0) RETURN_LONG(2); + if (strncmp(name, "CARDINALITY_REPEATED", name_len) == 0) RETURN_LONG(3); + + zend_throw_exception_ex( + NULL, 0 TSRMLS_CC, + "Enum Google\\Protobuf\\Field_Cardinality has no value " + "defined for name %s.", + name); +} + // ----------------------------------------------------------------------------- // Field_Kind // ----------------------------------------------------------------------------- static zend_function_entry field_kind_methods[] = { + PHP_ME(Field_Kind, name, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(Field_Kind, value, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) {NULL, NULL, NULL} }; @@ -931,11 +1126,101 @@ PHP_PROTO_INIT_ENUMCLASS_START("Google\\Protobuf\\Field\\Kind", #endif PHP_PROTO_INIT_ENUMCLASS_END +PHP_METHOD(Field_Kind, name) { + PHP_PROTO_LONG value; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &value) == + FAILURE) { + return; + } + switch (value) { + case 0: + PHP_PROTO_RETURN_STRING("TYPE_UNKNOWN", 1); + case 1: + PHP_PROTO_RETURN_STRING("TYPE_DOUBLE", 1); + case 2: + PHP_PROTO_RETURN_STRING("TYPE_FLOAT", 1); + case 3: + PHP_PROTO_RETURN_STRING("TYPE_INT64", 1); + case 4: + PHP_PROTO_RETURN_STRING("TYPE_UINT64", 1); + case 5: + PHP_PROTO_RETURN_STRING("TYPE_INT32", 1); + case 6: + PHP_PROTO_RETURN_STRING("TYPE_FIXED64", 1); + case 7: + PHP_PROTO_RETURN_STRING("TYPE_FIXED32", 1); + case 8: + PHP_PROTO_RETURN_STRING("TYPE_BOOL", 1); + case 9: + PHP_PROTO_RETURN_STRING("TYPE_STRING", 1); + case 10: + PHP_PROTO_RETURN_STRING("TYPE_GROUP", 1); + case 11: + PHP_PROTO_RETURN_STRING("TYPE_MESSAGE", 1); + case 12: + PHP_PROTO_RETURN_STRING("TYPE_BYTES", 1); + case 13: + PHP_PROTO_RETURN_STRING("TYPE_UINT32", 1); + case 14: + PHP_PROTO_RETURN_STRING("TYPE_ENUM", 1); + case 15: + PHP_PROTO_RETURN_STRING("TYPE_SFIXED32", 1); + case 16: + PHP_PROTO_RETURN_STRING("TYPE_SFIXED64", 1); + case 17: + PHP_PROTO_RETURN_STRING("TYPE_SINT32", 1); + case 18: + PHP_PROTO_RETURN_STRING("TYPE_SINT64", 1); + default: + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Enum Google\\Protobuf\\Field_Kind has no name " + "defined for value %d.", + value); + } +} + +PHP_METHOD(Field_Kind, value) { + char *name = NULL; + PHP_PROTO_SIZE name_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == + FAILURE) { + return; + } + + if (strncmp(name, "TYPE_UNKNOWN", name_len) == 0) RETURN_LONG(0); + if (strncmp(name, "TYPE_DOUBLE", name_len) == 0) RETURN_LONG(1); + if (strncmp(name, "TYPE_FLOAT", name_len) == 0) RETURN_LONG(2); + if (strncmp(name, "TYPE_INT64", name_len) == 0) RETURN_LONG(3); + if (strncmp(name, "TYPE_UINT64", name_len) == 0) RETURN_LONG(4); + if (strncmp(name, "TYPE_INT32", name_len) == 0) RETURN_LONG(5); + if (strncmp(name, "TYPE_FIXED64", name_len) == 0) RETURN_LONG(6); + if (strncmp(name, "TYPE_FIXED32", name_len) == 0) RETURN_LONG(7); + if (strncmp(name, "TYPE_BOOL", name_len) == 0) RETURN_LONG(8); + if (strncmp(name, "TYPE_STRING", name_len) == 0) RETURN_LONG(9); + if (strncmp(name, "TYPE_GROUP", name_len) == 0) RETURN_LONG(10); + if (strncmp(name, "TYPE_MESSAGE", name_len) == 0) RETURN_LONG(11); + if (strncmp(name, "TYPE_BYTES", name_len) == 0) RETURN_LONG(12); + if (strncmp(name, "TYPE_UINT32", name_len) == 0) RETURN_LONG(13); + if (strncmp(name, "TYPE_ENUM", name_len) == 0) RETURN_LONG(14); + if (strncmp(name, "TYPE_SFIXED32", name_len) == 0) RETURN_LONG(15); + if (strncmp(name, "TYPE_SFIXED64", name_len) == 0) RETURN_LONG(16); + if (strncmp(name, "TYPE_SINT32", name_len) == 0) RETURN_LONG(17); + if (strncmp(name, "TYPE_SINT64", name_len) == 0) RETURN_LONG(18); + + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Enum Google\\Protobuf\\Field_Kind has no value " + "defined for name %s.", + name); +} + // ----------------------------------------------------------------------------- // NullValue // ----------------------------------------------------------------------------- static zend_function_entry null_value_methods[] = { + PHP_ME(NullValue, name, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(NullValue, value, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) {NULL, NULL, NULL} }; @@ -948,11 +1233,47 @@ PHP_PROTO_INIT_ENUMCLASS_START("Google\\Protobuf\\NullValue", "NULL_VALUE", 10, 0 TSRMLS_CC); PHP_PROTO_INIT_ENUMCLASS_END +PHP_METHOD(NullValue, name) { + PHP_PROTO_LONG value; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &value) == + FAILURE) { + return; + } + switch (value) { + case 0: + PHP_PROTO_RETURN_STRING("NULL_VALUE", 1); + default: + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Enum Google\\Protobuf\\NullValue has no name " + "defined for value %d.", + value); + } +} + +PHP_METHOD(NullValue, value) { + char *name = NULL; + PHP_PROTO_SIZE name_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == + FAILURE) { + return; + } + + if (strncmp(name, "NULL_VALUE", name_len) == 0) RETURN_LONG(0); + + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Enum Google\\Protobuf\\NullValue has no value " + "defined for name %s.", + name); +} + // ----------------------------------------------------------------------------- // Syntax // ----------------------------------------------------------------------------- static zend_function_entry syntax_methods[] = { + PHP_ME(Syntax, name, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(Syntax, value, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) {NULL, NULL, NULL} }; @@ -967,7 +1288,42 @@ PHP_PROTO_INIT_ENUMCLASS_START("Google\\Protobuf\\Syntax", "SYNTAX_PROTO3", 13, 1 TSRMLS_CC); PHP_PROTO_INIT_ENUMCLASS_END +PHP_METHOD(Syntax, name) { + PHP_PROTO_LONG value; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &value) == + FAILURE) { + return; + } + switch (value) { + case 0: + PHP_PROTO_RETURN_STRING("SYNTAX_PROTO2", 1); + case 1: + PHP_PROTO_RETURN_STRING("SYNTAX_PROTO3", 1); + default: + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Enum Google\\Protobuf\\Syntax has no name " + "defined for value %d.", + value); + } +} + +PHP_METHOD(Syntax, value) { + char *name = NULL; + PHP_PROTO_SIZE name_len; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == + FAILURE) { + return; + } + + if (strncmp(name, "SYNTAX_PROTO2", name_len) == 0) RETURN_LONG(0); + if (strncmp(name, "SYNTAX_PROTO3", name_len) == 0) RETURN_LONG(1); + + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Enum Google\\Protobuf\\Syntax has no value " + "defined for name %s.", + name); +} // ----------------------------------------------------------------------------- // Define message @@ -993,7 +1349,6 @@ zend_class_entry* any_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Any", Any, any) - zend_class_implements(any_type TSRMLS_CC, 1, message_type); zend_declare_property_string(any_type, "type_url", strlen("type_url"), "" ,ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_string(any_type, "value", strlen("value"), @@ -1024,7 +1379,7 @@ static void hex_to_binary(const char* hex, char** binary, int* binary_len) { PHP_METHOD(Any, __construct) { init_file_any(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(any_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Any, any, TypeUrl, "type_url") @@ -1183,7 +1538,6 @@ zend_class_entry* duration_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Duration", Duration, duration) - zend_class_implements(duration_type TSRMLS_CC, 1, message_type); zend_declare_property_long(duration_type, "seconds", strlen("seconds"), 0 ,ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_long(duration_type, "nanos", strlen("nanos"), @@ -1193,7 +1547,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Duration, __construct) { init_file_duration(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(duration_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Duration, duration, Seconds, "seconds") @@ -1219,7 +1573,6 @@ zend_class_entry* timestamp_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Timestamp", Timestamp, timestamp) - zend_class_implements(timestamp_type TSRMLS_CC, 1, message_type); zend_declare_property_long(timestamp_type, "seconds", strlen("seconds"), 0 ,ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_long(timestamp_type, "nanos", strlen("nanos"), @@ -1229,7 +1582,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Timestamp, __construct) { init_file_timestamp(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(timestamp_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Timestamp, timestamp, Seconds, "seconds") @@ -1412,7 +1765,6 @@ zend_class_entry* api_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Api", Api, api) - zend_class_implements(api_type TSRMLS_CC, 1, message_type); zend_declare_property_null(api_type, "name", strlen("name"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(api_type, "methods", strlen("methods"), @@ -1432,7 +1784,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Api, __construct) { init_file_api(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(api_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Api, api, Name, "name") @@ -1459,7 +1811,6 @@ zend_class_entry* bool_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\BoolValue", BoolValue, bool_value) - zend_class_implements(bool_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(bool_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1467,7 +1818,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(BoolValue, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(bool_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(BoolValue, bool_value, Value, "value") @@ -1488,7 +1839,6 @@ zend_class_entry* bytes_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\BytesValue", BytesValue, bytes_value) - zend_class_implements(bytes_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(bytes_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1496,7 +1846,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(BytesValue, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(bytes_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(BytesValue, bytes_value, Value, "value") @@ -1517,7 +1867,6 @@ zend_class_entry* double_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\DoubleValue", DoubleValue, double_value) - zend_class_implements(double_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(double_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1525,7 +1874,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(DoubleValue, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(double_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(DoubleValue, double_value, Value, "value") @@ -1554,7 +1903,6 @@ zend_class_entry* enum_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Enum", Enum, enum) - zend_class_implements(enum_type TSRMLS_CC, 1, message_type); zend_declare_property_null(enum_type, "name", strlen("name"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(enum_type, "enumvalue", strlen("enumvalue"), @@ -1570,7 +1918,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Enum, __construct) { init_file_type(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(enum_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Enum, enum, Name, "name") @@ -1599,7 +1947,6 @@ zend_class_entry* enum_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\EnumValue", EnumValue, enum_value) - zend_class_implements(enum_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(enum_value_type, "name", strlen("name"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(enum_value_type, "number", strlen("number"), @@ -1611,7 +1958,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(EnumValue, __construct) { init_file_type(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(enum_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(EnumValue, enum_value, Name, "name") @@ -1634,7 +1981,6 @@ zend_class_entry* field_mask_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\FieldMask", FieldMask, field_mask) - zend_class_implements(field_mask_type TSRMLS_CC, 1, message_type); zend_declare_property_null(field_mask_type, "paths", strlen("paths"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1642,7 +1988,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(FieldMask, __construct) { init_file_field_mask(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(field_mask_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(FieldMask, field_mask, Paths, "paths") @@ -1681,7 +2027,6 @@ zend_class_entry* field_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Field", Field, field) - zend_class_implements(field_type TSRMLS_CC, 1, message_type); zend_declare_property_null(field_type, "kind", strlen("kind"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(field_type, "cardinality", strlen("cardinality"), @@ -1707,7 +2052,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Field, __construct) { init_file_type(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(field_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Field, field, Kind, "kind") @@ -1737,7 +2082,6 @@ zend_class_entry* float_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\FloatValue", FloatValue, float_value) - zend_class_implements(float_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(float_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1745,7 +2089,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(FloatValue, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(float_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(FloatValue, float_value, Value, "value") @@ -1764,13 +2108,12 @@ zend_class_entry* empty_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\GPBEmpty", GPBEmpty, empty) - zend_class_implements(empty_type TSRMLS_CC, 1, message_type); PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(GPBEmpty, __construct) { init_file_empty(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(empty_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } @@ -1790,7 +2133,6 @@ zend_class_entry* int32_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Int32Value", Int32Value, int32_value) - zend_class_implements(int32_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(int32_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1798,7 +2140,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Int32Value, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(int32_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Int32Value, int32_value, Value, "value") @@ -1819,7 +2161,6 @@ zend_class_entry* int64_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Int64Value", Int64Value, int64_value) - zend_class_implements(int64_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(int64_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1827,7 +2168,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Int64Value, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(int64_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Int64Value, int64_value, Value, "value") @@ -1848,7 +2189,6 @@ zend_class_entry* list_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\ListValue", ListValue, list_value) - zend_class_implements(list_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(list_value_type, "values", strlen("values"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1856,7 +2196,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(ListValue, __construct) { init_file_struct(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(list_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(ListValue, list_value, Values, "values") @@ -1889,7 +2229,6 @@ zend_class_entry* method_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Method", Method, method) - zend_class_implements(method_type TSRMLS_CC, 1, message_type); zend_declare_property_null(method_type, "name", strlen("name"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(method_type, "request_type_url", strlen("request_type_url"), @@ -1909,7 +2248,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Method, __construct) { init_file_api(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(method_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Method, method, Name, "name") @@ -1938,7 +2277,6 @@ zend_class_entry* mixin_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Mixin", Mixin, mixin) - zend_class_implements(mixin_type TSRMLS_CC, 1, message_type); zend_declare_property_null(mixin_type, "name", strlen("name"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(mixin_type, "root", strlen("root"), @@ -1948,7 +2286,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Mixin, __construct) { init_file_api(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(mixin_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Mixin, mixin, Name, "name") @@ -1972,7 +2310,6 @@ zend_class_entry* option_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Option", Option, option) - zend_class_implements(option_type TSRMLS_CC, 1, message_type); zend_declare_property_null(option_type, "name", strlen("name"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(option_type, "value", strlen("value"), @@ -1982,7 +2319,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Option, __construct) { init_file_type(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(option_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Option, option, Name, "name") @@ -2004,7 +2341,6 @@ zend_class_entry* source_context_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\SourceContext", SourceContext, source_context) - zend_class_implements(source_context_type TSRMLS_CC, 1, message_type); zend_declare_property_null(source_context_type, "file_name", strlen("file_name"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2012,7 +2348,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(SourceContext, __construct) { init_file_source_context(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(source_context_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(SourceContext, source_context, FileName, "file_name") @@ -2033,7 +2369,6 @@ zend_class_entry* string_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\StringValue", StringValue, string_value) - zend_class_implements(string_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(string_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2041,7 +2376,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(StringValue, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(string_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(StringValue, string_value, Value, "value") @@ -2062,7 +2397,6 @@ zend_class_entry* struct_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Struct", Struct, struct) - zend_class_implements(struct_type TSRMLS_CC, 1, message_type); zend_declare_property_null(struct_type, "fields", strlen("fields"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2070,7 +2404,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Struct, __construct) { init_file_struct(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(struct_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Struct, struct, Fields, "fields") @@ -2101,7 +2435,6 @@ zend_class_entry* type_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Type", Type, type) - zend_class_implements(type_type TSRMLS_CC, 1, message_type); zend_declare_property_null(type_type, "name", strlen("name"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(type_type, "fields", strlen("fields"), @@ -2119,7 +2452,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Type, __construct) { init_file_type(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(type_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(Type, type, Name, "name") @@ -2145,7 +2478,6 @@ zend_class_entry* u_int32_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\UInt32Value", UInt32Value, u_int32_value) - zend_class_implements(u_int32_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(u_int32_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2153,7 +2485,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(UInt32Value, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(u_int32_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(UInt32Value, u_int32_value, Value, "value") @@ -2174,7 +2506,6 @@ zend_class_entry* u_int64_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\UInt64Value", UInt64Value, u_int64_value) - zend_class_implements(u_int64_value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(u_int64_value_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2182,7 +2513,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(UInt64Value, __construct) { init_file_wrappers(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(u_int64_value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_FIELD_ACCESSORS(UInt64Value, u_int64_value, Value, "value") @@ -2214,7 +2545,6 @@ zend_class_entry* value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Value", Value, value) - zend_class_implements(value_type TSRMLS_CC, 1, message_type); zend_declare_property_null(value_type, "kind", strlen("kind"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2222,7 +2552,7 @@ PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Value, __construct) { init_file_struct(TSRMLS_C); MessageHeader* intern = UNBOX(MessageHeader, getThis()); - custom_data_init(value_type, intern PHP_PROTO_TSRMLS_CC); + INIT_MESSAGE_WITH_ARRAY; } PHP_PROTO_ONEOF_FIELD_ACCESSORS(Value, value, NullValue, "null_value") diff --git a/php/ext/google/protobuf/package.xml b/php/ext/google/protobuf/package.xml index 28d713f344..f81e55f1ab 100644 --- a/php/ext/google/protobuf/package.xml +++ b/php/ext/google/protobuf/package.xml @@ -10,37 +10,36 @@ protobuf-opensource@google.com yes - 2018-06-06 - + 2019-06-17 + - 3.6.0 - 3.6.0 + 3.9.0RC1 + 3.9.0 - stable - stable + beta + beta 3-Clause BSD License - -GA release. - + GA release.

- - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + @@ -54,7 +53,7 @@ GA release. protobuf - + @@ -248,5 +247,119 @@ G A release. G A release. + + + 3.6.1 + 3.6.1 + + + stable + stable + + 2018-08-03 + + 3-Clause BSD License + +G A release. + + + + + 3.7.0RC2 + 3.7.0 + + + stable + stable + + 2019-02-1 + + 3-Clause BSD License + GA release. + + + + 3.7.0RC3 + 3.7.0 + + + beta + beta + + 2019-02-22 + + 3-Clause BSD License + GA release. + + + + 3.7.0 + 3.7.0 + + + stable + stable + + 2019-02-28 + + 3-Clause BSD License + GA release. + + + + 3.7.1 + 3.7.1 + + + stable + stable + + 2019-03-25 + + 3-Clause BSD License + GA release. + + + + 3.8.0RC1 + 3.8.0 + + + beta + beta + + 2019-04-23 + + 3-Clause BSD License + GA release. + + + + 3.8.0 + 3.8.0 + + + stable + stable + + 2019-05-21 + + 3-Clause BSD License + GA release. + + + + 3.9.0RC1 + 3.9.0 + + + beta + beta + + 2019-06-17 + + 3-Clause BSD License + GA release. + diff --git a/php/ext/google/protobuf/protobuf.h b/php/ext/google/protobuf/protobuf.h index 9cb36dc202..b2a9b94c82 100644 --- a/php/ext/google/protobuf/protobuf.h +++ b/php/ext/google/protobuf/protobuf.h @@ -37,7 +37,7 @@ #include "upb.h" #define PHP_PROTOBUF_EXTNAME "protobuf" -#define PHP_PROTOBUF_VERSION "3.6.1" +#define PHP_PROTOBUF_VERSION "3.9.0RC1" #define MAX_LENGTH_OF_INT64 20 #define SIZEOF_INT64 8 @@ -137,8 +137,10 @@ const char* class_name = CLASSNAME; \ INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \ LOWWERNAME##_methods); \ - LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \ - LOWWERNAME##_type->create_object = message_create; + LOWWERNAME##_type = zend_register_internal_class_ex( \ + &class_type, message_type, NULL TSRMLS_CC); \ + LOWWERNAME##_type->create_object = message_create; \ + zend_do_inheritance(LOWWERNAME##_type, message_type TSRMLS_CC); #define PHP_PROTO_INIT_SUBMSGCLASS_END \ } @@ -308,8 +310,10 @@ static inline int php_proto_zend_hash_index_update_mem(HashTable* ht, ulong h, static inline int php_proto_zend_hash_update_zval(HashTable* ht, const char* key, uint key_len, zval* pData) { + void* result = NULL; zend_string* internal_key = zend_string_init(key, key_len, 0); - zend_hash_update(ht, internal_key, pData); + result = zend_hash_update(ht, internal_key, pData); + return result != NULL ? SUCCESS : FAILURE; } static inline int php_proto_zend_hash_update_mem(HashTable* ht, const char* key, @@ -403,8 +407,9 @@ static inline int php_proto_zend_hash_get_current_data_ex(HashTable* ht, const char* class_name = CLASSNAME; \ INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \ LOWWERNAME##_methods); \ - LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \ - LOWWERNAME##_type->create_object = message_create; + LOWWERNAME##_type = zend_register_internal_class_ex( \ + &class_type, message_type TSRMLS_CC); \ + zend_do_inheritance(LOWWERNAME##_type, message_type TSRMLS_CC); #define PHP_PROTO_INIT_SUBMSGCLASS_END \ } @@ -679,6 +684,7 @@ void double_value_init(TSRMLS_D); void duration_init(TSRMLS_D); void empty_init(TSRMLS_D); void enum_descriptor_init(TSRMLS_D); +void enum_value_descriptor_init(TSRMLS_D); void enum_init(TSRMLS_D); void enum_value_init(TSRMLS_D); void field_cardinality_init(TSRMLS_D); @@ -708,8 +714,8 @@ void struct_init(TSRMLS_D); void syntax_init(TSRMLS_D); void timestamp_init(TSRMLS_D); void type_init(TSRMLS_D); -void uint32_value_init(TSRMLS_D); -void uint64_value_init(TSRMLS_D); +void u_int32_value_init(TSRMLS_D); +void u_int64_value_init(TSRMLS_D); void util_init(TSRMLS_D); void value_init(TSRMLS_D); @@ -757,15 +763,22 @@ PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName); PHP_PROTO_WRAP_OBJECT_START(InternalDescriptorPool) upb_symtab* symtab; - HashTable* pending_list; + upb_handlercache* fill_handler_cache; + upb_handlercache* pb_serialize_handler_cache; + upb_handlercache* json_serialize_handler_cache; + upb_handlercache* json_serialize_handler_preserve_cache; + upb_pbcodecache* fill_method_cache; + upb_json_codecache* json_fill_method_cache; PHP_PROTO_WRAP_OBJECT_END PHP_METHOD(InternalDescriptorPool, getGeneratedPool); PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile); void internal_add_generated_file(const char* data, PHP_PROTO_SIZE data_len, - InternalDescriptorPool* pool TSRMLS_DC); + InternalDescriptorPool* pool, + bool use_nested_submsg TSRMLS_DC); void init_generated_pool_once(TSRMLS_D); +void add_handlers_for_message(const void* closure, upb_handlers* h); // wrapper of generated pool #if PHP_MAJOR_VERSION < 7 @@ -782,15 +795,10 @@ void internal_descriptor_pool_free(zend_object* object); extern InternalDescriptorPool* generated_pool; // The actual generated pool PHP_PROTO_WRAP_OBJECT_START(Descriptor) + InternalDescriptorPool* pool; const upb_msgdef* msgdef; MessageLayout* layout; zend_class_entry* klass; // begins as NULL - const upb_handlers* fill_handlers; - const upb_pbdecodermethod* fill_method; - const upb_json_parsermethod* json_fill_method; - const upb_handlers* pb_serialize_handlers; - const upb_handlers* json_serialize_handlers; - const upb_handlers* json_serialize_handlers_preserve; PHP_PROTO_WRAP_OBJECT_END PHP_METHOD(Descriptor, getClass); @@ -963,7 +971,7 @@ PHP_METHOD(Message, __construct); const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor *desc, const void *owner); void serialize_to_string(zval* val, zval* return_value TSRMLS_DC); -void merge_from_string(const char* data, int data_len, const Descriptor* desc, +void merge_from_string(const char* data, int data_len, Descriptor* desc, MessageHeader* msg); PHP_METHOD(Message, serializeToString); @@ -1161,7 +1169,7 @@ PHP_METHOD(RepeatedFieldIter, valid); // ----------------------------------------------------------------------------- PHP_PROTO_WRAP_OBJECT_START(Oneof) - upb_oneofdef* oneofdef; + const upb_oneofdef* oneofdef; int index; // Index of field in oneof. -1 if not set. char value[NATIVE_SLOT_MAX_SIZE]; PHP_PROTO_WRAP_OBJECT_END @@ -1300,6 +1308,12 @@ PHP_METHOD(Field, setJsonName); PHP_METHOD(Field, getDefaultValue); PHP_METHOD(Field, setDefaultValue); +PHP_METHOD(Field_Cardinality, name); +PHP_METHOD(Field_Cardinality, value); + +PHP_METHOD(Field_Kind, name); +PHP_METHOD(Field_Kind, value); + PHP_METHOD(FloatValue, __construct); PHP_METHOD(FloatValue, getValue); PHP_METHOD(FloatValue, setValue); @@ -1340,6 +1354,9 @@ PHP_METHOD(Mixin, setName); PHP_METHOD(Mixin, getRoot); PHP_METHOD(Mixin, setRoot); +PHP_METHOD(NullValue, name); +PHP_METHOD(NullValue, value); + PHP_METHOD(Option, __construct); PHP_METHOD(Option, getName); PHP_METHOD(Option, setName); @@ -1358,6 +1375,9 @@ PHP_METHOD(Struct, __construct); PHP_METHOD(Struct, getFields); PHP_METHOD(Struct, setFields); +PHP_METHOD(Syntax, name); +PHP_METHOD(Syntax, value); + PHP_METHOD(Type, __construct); PHP_METHOD(Type, getName); PHP_METHOD(Type, setName); @@ -1433,6 +1453,19 @@ extern zend_class_entry* value_type; upb_fieldtype_t to_fieldtype(upb_descriptortype_t type); const zend_class_entry* field_type_class( const upb_fielddef* field PHP_PROTO_TSRMLS_DC); +void stringsink_uninit_opaque(void *sink); + +typedef struct { + upb_byteshandler handler; + upb_bytessink sink; + char *ptr; + size_t len, size; +} stringsink; + +void stringsink_init(stringsink *sink); +void stringsink_uninit(stringsink *sink); +size_t stringsink_string(void *_sink, const void *hd, const char *ptr, + size_t len, const upb_bufhandle *handle); // ----------------------------------------------------------------------------- // Utilities. diff --git a/php/ext/google/protobuf/storage.c b/php/ext/google/protobuf/storage.c index a60fbe393b..8f717302dc 100644 --- a/php/ext/google/protobuf/storage.c +++ b/php/ext/google/protobuf/storage.c @@ -161,6 +161,11 @@ bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass, bool native_slot_set_by_array(upb_fieldtype_t type, const zend_class_entry* klass, void* memory, zval* value TSRMLS_DC) { +#if PHP_MAJOR_VERSION >= 7 + if (Z_ISREF_P(value)) { + ZVAL_DEREF(value); + } +#endif switch (type) { case UPB_TYPE_STRING: case UPB_TYPE_BYTES: { @@ -180,7 +185,8 @@ bool native_slot_set_by_array(upb_fieldtype_t type, PHP_PROTO_ZVAL_STRINGL(DEREF(memory, zval*), Z_STRVAL_P(value), Z_STRLEN_P(value), 1); #else - *(zend_string**)memory = zend_string_dup(Z_STR_P(value), 0); + *(zend_string**)memory = + zend_string_init(Z_STRVAL_P(value), Z_STRLEN_P(value), 0); #endif break; } @@ -212,6 +218,11 @@ bool native_slot_set_by_array(upb_fieldtype_t type, bool native_slot_set_by_map(upb_fieldtype_t type, const zend_class_entry* klass, void* memory, zval* value TSRMLS_DC) { +#if PHP_MAJOR_VERSION >= 7 + if (Z_ISREF_P(value)) { + ZVAL_DEREF(value); + } +#endif switch (type) { case UPB_TYPE_STRING: case UPB_TYPE_BYTES: { @@ -231,7 +242,8 @@ bool native_slot_set_by_map(upb_fieldtype_t type, const zend_class_entry* klass, PHP_PROTO_ZVAL_STRINGL(DEREF(memory, zval*), Z_STRVAL_P(value), Z_STRLEN_P(value), 1); #else - *(zend_string**)memory = zend_string_dup(Z_STR_P(value), 0); + *(zend_string**)memory = + zend_string_init(Z_STRVAL_P(value), Z_STRLEN_P(value), 0); #endif break; } @@ -541,11 +553,11 @@ const zend_class_entry* field_type_class( const upb_fielddef* field PHP_PROTO_TSRMLS_DC) { if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) { Descriptor* desc = UNBOX_HASHTABLE_VALUE( - Descriptor, get_def_obj(upb_fielddef_subdef(field))); + Descriptor, get_def_obj(upb_fielddef_msgsubdef(field))); return desc->klass; } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) { EnumDescriptor* desc = UNBOX_HASHTABLE_VALUE( - EnumDescriptor, get_def_obj(upb_fielddef_subdef(field))); + EnumDescriptor, get_def_obj(upb_fielddef_enumsubdef(field))); return desc->klass; } return NULL; @@ -730,16 +742,13 @@ MessageLayout* create_layout(const upb_msgdef* msgdef) { } layout->size = off; - layout->msgdef = msgdef; - upb_msgdef_ref(layout->msgdef, &layout->msgdef); return layout; } void free_layout(MessageLayout* layout) { FREE(layout->fields); - upb_msgdef_unref(layout->msgdef, &layout->msgdef); FREE(layout); } diff --git a/php/ext/google/protobuf/type_check.c b/php/ext/google/protobuf/type_check.c index 85f5051eda..e037e638a6 100644 --- a/php/ext/google/protobuf/type_check.c +++ b/php/ext/google/protobuf/type_check.c @@ -29,6 +29,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include #include "protobuf.h" #include "utf8.h" @@ -87,7 +88,7 @@ void util_init(TSRMLS_D) { // Type checking/conversion. // ----------------------------------------------------------------------------- -// This is modified from is_numeric_string in zend_operators.h. The behavior of +// This is modified from is_numeric_string in zend_operators.h. The behavior of // this function is the same as is_numeric_string, except that this takes // int64_t as input instead of long. static zend_uchar convert_numeric_string( @@ -101,7 +102,7 @@ static zend_uchar convert_numeric_string( return IS_NULL; } - while (*str == ' ' || *str == '\t' || *str == '\n' || + while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') { str++; length--; @@ -222,7 +223,7 @@ process_double: type##_t* type##_value) { \ int64_t lval; \ double dval; \ - \ + TSRMLS_FETCH(); \ switch (convert_numeric_string(val, len, &lval, &dval)) { \ case IS_DOUBLE: { \ return convert_double_to_##type(dval, type##_value); \ @@ -231,13 +232,15 @@ process_double: return convert_int64_to_##type(lval, type##_value); \ } \ default: \ - zend_error(E_USER_ERROR, \ - "Given string value cannot be converted to integer."); \ + zend_throw_exception(NULL, \ + "Given string value cannot be converted to integer.", \ + 0 TSRMLS_CC); \ return false; \ } \ } \ \ bool protobuf_convert_to_##type(zval* from, type##_t* to) { \ + TSRMLS_FETCH(); \ switch (Z_TYPE_P(from)) { \ case IS_LONG: { \ return convert_int64_to_##type(Z_LVAL_P(from), to); \ @@ -250,8 +253,9 @@ process_double: to); \ } \ default: { \ - zend_error(E_USER_ERROR, \ - "Given value cannot be converted to integer."); \ + zend_throw_exception(NULL, \ + "Given value cannot be converted to integer.", \ + 0 TSRMLS_CC); \ return false; \ } \ } \ @@ -281,6 +285,7 @@ CONVERT_TO_INTEGER(uint64); int64_t lval; \ double dval; \ \ + TSRMLS_FETCH(); \ switch (convert_numeric_string(val, len, &lval, &dval)) { \ case IS_DOUBLE: { \ *type##_value = (type)dval; \ @@ -291,13 +296,15 @@ CONVERT_TO_INTEGER(uint64); return true; \ } \ default: \ - zend_error(E_USER_ERROR, \ - "Given string value cannot be converted to integer."); \ + zend_throw_exception(NULL, \ + "Given string value cannot be converted to integer.", \ + 0 TSRMLS_CC); \ return false; \ } \ } \ \ bool protobuf_convert_to_##type(zval* from, type* to) { \ + TSRMLS_FETCH(); \ switch (Z_TYPE_P(from)) { \ case IS_LONG: { \ return convert_int64_to_##type(Z_LVAL_P(from), to); \ @@ -310,8 +317,9 @@ CONVERT_TO_INTEGER(uint64); to); \ } \ default: { \ - zend_error(E_USER_ERROR, \ - "Given value cannot be converted to integer."); \ + zend_throw_exception(NULL, \ + "Given value cannot be converted to integer.", \ + 0 TSRMLS_CC); \ return false; \ } \ } \ @@ -324,6 +332,7 @@ CONVERT_TO_FLOAT(double); #undef CONVERT_TO_FLOAT bool protobuf_convert_to_bool(zval* from, int8_t* to) { + TSRMLS_FETCH(); switch (Z_TYPE_P(from)) { #if PHP_MAJOR_VERSION < 7 case IS_BOOL: @@ -354,7 +363,9 @@ bool protobuf_convert_to_bool(zval* from, int8_t* to) { } } break; default: { - zend_error(E_USER_ERROR, "Given value cannot be converted to bool."); + zend_throw_exception( + NULL, "Given value cannot be converted to bool.", + 0 TSRMLS_CC); return false; } } @@ -362,6 +373,12 @@ bool protobuf_convert_to_bool(zval* from, int8_t* to) { } bool protobuf_convert_to_string(zval* from) { +#if PHP_MAJOR_VERSION >= 7 + if (Z_ISREF_P(from)) { + ZVAL_DEREF(from); + } +#endif + TSRMLS_FETCH(); switch (Z_TYPE_P(from)) { case IS_STRING: { return true; @@ -380,7 +397,9 @@ bool protobuf_convert_to_string(zval* from) { return true; } default: - zend_error(E_USER_ERROR, "Given value cannot be converted to string."); + zend_throw_exception( + NULL, "Given value cannot be converted to string.", + 0 TSRMLS_CC); return false; } } @@ -421,8 +440,9 @@ PHP_METHOD(Util, checkMessage) { RETURN_NULL(); } if (!instanceof_function(Z_OBJCE_P(val), klass TSRMLS_CC)) { - zend_error(E_USER_ERROR, "Given value is not an instance of %s.", - klass->name); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Given value is not an instance of %s.", + klass->name); return; } RETURN_ZVAL(val, 1, 0); @@ -465,24 +485,27 @@ void check_repeated_field(const zend_class_entry* klass, PHP_PROTO_LONG type, } else if (Z_TYPE_P(val) == IS_OBJECT) { if (!instanceof_function(Z_OBJCE_P(val), repeated_field_type TSRMLS_CC)) { - zend_error(E_USER_ERROR, "Given value is not an instance of %s.", - repeated_field_type->name); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Given value is not an instance of %s.", + repeated_field_type->name); return; } RepeatedField* intern = UNBOX(RepeatedField, val); if (to_fieldtype(type) != intern->type) { - zend_error(E_USER_ERROR, "Incorrect repeated field type."); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Incorrect repeated field type."); return; } if (klass != NULL && intern->msg_ce != klass) { - zend_error(E_USER_ERROR, - "Expect a repeated field of %s, but %s is given.", klass->name, - intern->msg_ce->name); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Expect a repeated field of %s, but %s is given.", + klass->name, intern->msg_ce->name); return; } RETURN_ZVAL(val, 1, 0); } else { - zend_error(E_USER_ERROR, "Incorrect repeated field type."); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Incorrect repeated field type."); return; } } @@ -538,27 +561,35 @@ void check_map_field(const zend_class_entry* klass, PHP_PROTO_LONG key_type, RETURN_ZVAL(CACHED_TO_ZVAL_PTR(map_field), 1, 1); } else if (Z_TYPE_P(val) == IS_OBJECT) { if (!instanceof_function(Z_OBJCE_P(val), map_field_type TSRMLS_CC)) { - zend_error(E_USER_ERROR, "Given value is not an instance of %s.", - map_field_type->name); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Given value is not an instance of %s.", + map_field_type->name); return; } Map* intern = UNBOX(Map, val); if (to_fieldtype(key_type) != intern->key_type) { - zend_error(E_USER_ERROR, "Incorrect map field key type."); + zend_throw_exception( + NULL, "Incorrect map field key type.", + 0 TSRMLS_CC); return; } if (to_fieldtype(value_type) != intern->value_type) { - zend_error(E_USER_ERROR, "Incorrect map field value type."); + zend_throw_exception( + NULL, "Incorrect map field value type.", + 0 TSRMLS_CC); return; } if (klass != NULL && intern->msg_ce != klass) { - zend_error(E_USER_ERROR, "Expect a map field of %s, but %s is given.", - klass->name, intern->msg_ce->name); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, + "Expect a map field of %s, but %s is given.", + klass->name, intern->msg_ce->name); return; } RETURN_ZVAL(val, 1, 0); } else { - zend_error(E_USER_ERROR, "Incorrect map field type."); + zend_throw_exception( + NULL, "Incorrect map field type.", + 0 TSRMLS_CC); return; } } diff --git a/php/ext/google/protobuf/upb.c b/php/ext/google/protobuf/upb.c index 90c52aa88a..cc5a54ab84 100644 --- a/php/ext/google/protobuf/upb.c +++ b/php/ext/google/protobuf/upb.c @@ -1,5 +1,27 @@ -// Amalgamated source file +/* Amalgamated source file */ #include "upb.h" + +#ifndef UINTPTR_MAX +#error must include stdint.h first +#endif + +#if UINTPTR_MAX == 0xffffffff +#define UPB_SIZE(size32, size64) size32 +#else +#define UPB_SIZE(size32, size64) size64 +#endif + +#define UPB_FIELD_AT(msg, fieldtype, offset) \ + *(fieldtype*)((const char*)(msg) + offset) + +#define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \ + UPB_FIELD_AT(msg, int, case_offset) == case_val \ + ? UPB_FIELD_AT(msg, fieldtype, offset) \ + : default + +#define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \ + UPB_FIELD_AT(msg, int, case_offset) = case_val; \ + UPB_FIELD_AT(msg, fieldtype, offset) = value; /* This file was generated by upbc (the upb compiler) from the input * file: * @@ -11,64 +33,21 @@ #include -struct google_protobuf_FileDescriptorSet { - upb_array* file; -}; - -static const upb_msglayout_msginit_v1 *const google_protobuf_FileDescriptorSet_submsgs[1] = { +static const upb_msglayout *const google_protobuf_FileDescriptorSet_submsgs[1] = { &google_protobuf_FileDescriptorProto_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_FileDescriptorSet__fields[1] = { - {1, offsetof(google_protobuf_FileDescriptorSet, file), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_FileDescriptorSet__fields[1] = { + {1, UPB_SIZE(0, 0), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_FileDescriptorSet_msginit = { +const upb_msglayout google_protobuf_FileDescriptorSet_msginit = { &google_protobuf_FileDescriptorSet_submsgs[0], &google_protobuf_FileDescriptorSet__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_FileDescriptorSet), 1, 0, false, true -}; - -google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_env *env) { - google_protobuf_FileDescriptorSet *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_FileDescriptorSet *msg = google_protobuf_FileDescriptorSet_new(env); - if (upb_decode(buf, msg, &google_protobuf_FileDescriptorSet_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_FileDescriptorSet_serialize(google_protobuf_FileDescriptorSet *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, env, size); -} -const upb_array* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg) { - return msg->file; -} -void google_protobuf_FileDescriptorSet_set_file(google_protobuf_FileDescriptorSet *msg, upb_array* value) { - msg->file = value; -} -struct google_protobuf_FileDescriptorProto { - upb_stringview name; - upb_stringview package; - upb_stringview syntax; - google_protobuf_FileOptions* options; - google_protobuf_SourceCodeInfo* source_code_info; - upb_array* dependency; - upb_array* message_type; - upb_array* enum_type; - upb_array* service; - upb_array* extension; - upb_array* public_dependency; - upb_array* weak_dependency; + UPB_SIZE(4, 8), 1, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_FileDescriptorProto_submsgs[6] = { +static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = { &google_protobuf_DescriptorProto_msginit, &google_protobuf_EnumDescriptorProto_msginit, &google_protobuf_FieldDescriptorProto_msginit, @@ -77,131 +56,28 @@ static const upb_msglayout_msginit_v1 *const google_protobuf_FileDescriptorProto &google_protobuf_SourceCodeInfo_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_FileDescriptorProto__fields[12] = { - {1, offsetof(google_protobuf_FileDescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {2, offsetof(google_protobuf_FileDescriptorProto, package), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {3, offsetof(google_protobuf_FileDescriptorProto, dependency), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 3}, - {4, offsetof(google_protobuf_FileDescriptorProto, message_type), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, - {5, offsetof(google_protobuf_FileDescriptorProto, enum_type), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 1, 11, 3}, - {6, offsetof(google_protobuf_FileDescriptorProto, service), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 4, 11, 3}, - {7, offsetof(google_protobuf_FileDescriptorProto, extension), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 2, 11, 3}, - {8, offsetof(google_protobuf_FileDescriptorProto, options), 3, UPB_NOT_IN_ONEOF, 3, 11, 1}, - {9, offsetof(google_protobuf_FileDescriptorProto, source_code_info), 4, UPB_NOT_IN_ONEOF, 5, 11, 1}, - {10, offsetof(google_protobuf_FileDescriptorProto, public_dependency), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3}, - {11, offsetof(google_protobuf_FileDescriptorProto, weak_dependency), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3}, - {12, offsetof(google_protobuf_FileDescriptorProto, syntax), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, +static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(12, 24), 2, 0, 9, 1}, + {3, UPB_SIZE(36, 72), 0, 0, 9, 3}, + {4, UPB_SIZE(40, 80), 0, 0, 11, 3}, + {5, UPB_SIZE(44, 88), 0, 1, 11, 3}, + {6, UPB_SIZE(48, 96), 0, 4, 11, 3}, + {7, UPB_SIZE(52, 104), 0, 2, 11, 3}, + {8, UPB_SIZE(28, 56), 4, 3, 11, 1}, + {9, UPB_SIZE(32, 64), 5, 5, 11, 1}, + {10, UPB_SIZE(56, 112), 0, 0, 5, 3}, + {11, UPB_SIZE(60, 120), 0, 0, 5, 3}, + {12, UPB_SIZE(20, 40), 3, 0, 9, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_FileDescriptorProto_msginit = { +const upb_msglayout google_protobuf_FileDescriptorProto_msginit = { &google_protobuf_FileDescriptorProto_submsgs[0], &google_protobuf_FileDescriptorProto__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_FileDescriptorProto), 12, 0, false, true -}; - -google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_env *env) { - google_protobuf_FileDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_FileDescriptorProto *msg = google_protobuf_FileDescriptorProto_new(env); - if (upb_decode(buf, msg, &google_protobuf_FileDescriptorProto_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_FileDescriptorProto_serialize(google_protobuf_FileDescriptorProto *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, env, size); -} -upb_stringview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) { - return msg->name; -} -void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_stringview value) { - msg->name = value; -} -upb_stringview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) { - return msg->package; -} -void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_stringview value) { - msg->package = value; -} -const upb_array* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg) { - return msg->dependency; -} -void google_protobuf_FileDescriptorProto_set_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) { - msg->dependency = value; -} -const upb_array* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg) { - return msg->message_type; -} -void google_protobuf_FileDescriptorProto_set_message_type(google_protobuf_FileDescriptorProto *msg, upb_array* value) { - msg->message_type = value; -} -const upb_array* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg) { - return msg->enum_type; -} -void google_protobuf_FileDescriptorProto_set_enum_type(google_protobuf_FileDescriptorProto *msg, upb_array* value) { - msg->enum_type = value; -} -const upb_array* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg) { - return msg->service; -} -void google_protobuf_FileDescriptorProto_set_service(google_protobuf_FileDescriptorProto *msg, upb_array* value) { - msg->service = value; -} -const upb_array* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg) { - return msg->extension; -} -void google_protobuf_FileDescriptorProto_set_extension(google_protobuf_FileDescriptorProto *msg, upb_array* value) { - msg->extension = value; -} -const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg) { - return msg->options; -} -void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value) { - msg->options = value; -} -const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg) { - return msg->source_code_info; -} -void google_protobuf_FileDescriptorProto_set_source_code_info(google_protobuf_FileDescriptorProto *msg, google_protobuf_SourceCodeInfo* value) { - msg->source_code_info = value; -} -const upb_array* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg) { - return msg->public_dependency; -} -void google_protobuf_FileDescriptorProto_set_public_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) { - msg->public_dependency = value; -} -const upb_array* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg) { - return msg->weak_dependency; -} -void google_protobuf_FileDescriptorProto_set_weak_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) { - msg->weak_dependency = value; -} -upb_stringview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) { - return msg->syntax; -} -void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_stringview value) { - msg->syntax = value; -} -struct google_protobuf_DescriptorProto { - upb_stringview name; - google_protobuf_MessageOptions* options; - upb_array* field; - upb_array* nested_type; - upb_array* enum_type; - upb_array* extension_range; - upb_array* extension; - upb_array* oneof_decl; - upb_array* reserved_range; - upb_array* reserved_name; + UPB_SIZE(64, 128), 12, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_DescriptorProto_submsgs[8] = { +static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = { &google_protobuf_DescriptorProto_msginit, &google_protobuf_DescriptorProto_ExtensionRange_msginit, &google_protobuf_DescriptorProto_ReservedRange_msginit, @@ -211,1696 +87,423 @@ static const upb_msglayout_msginit_v1 *const google_protobuf_DescriptorProto_sub &google_protobuf_OneofDescriptorProto_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_DescriptorProto__fields[10] = { - {1, offsetof(google_protobuf_DescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {2, offsetof(google_protobuf_DescriptorProto, field), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 4, 11, 3}, - {3, offsetof(google_protobuf_DescriptorProto, nested_type), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, - {4, offsetof(google_protobuf_DescriptorProto, enum_type), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 3, 11, 3}, - {5, offsetof(google_protobuf_DescriptorProto, extension_range), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 1, 11, 3}, - {6, offsetof(google_protobuf_DescriptorProto, extension), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 4, 11, 3}, - {7, offsetof(google_protobuf_DescriptorProto, options), 1, UPB_NOT_IN_ONEOF, 5, 11, 1}, - {8, offsetof(google_protobuf_DescriptorProto, oneof_decl), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 6, 11, 3}, - {9, offsetof(google_protobuf_DescriptorProto, reserved_range), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 2, 11, 3}, - {10, offsetof(google_protobuf_DescriptorProto, reserved_name), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 3}, +static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(16, 32), 0, 4, 11, 3}, + {3, UPB_SIZE(20, 40), 0, 0, 11, 3}, + {4, UPB_SIZE(24, 48), 0, 3, 11, 3}, + {5, UPB_SIZE(28, 56), 0, 1, 11, 3}, + {6, UPB_SIZE(32, 64), 0, 4, 11, 3}, + {7, UPB_SIZE(12, 24), 2, 5, 11, 1}, + {8, UPB_SIZE(36, 72), 0, 6, 11, 3}, + {9, UPB_SIZE(40, 80), 0, 2, 11, 3}, + {10, UPB_SIZE(44, 88), 0, 0, 9, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_msginit = { +const upb_msglayout google_protobuf_DescriptorProto_msginit = { &google_protobuf_DescriptorProto_submsgs[0], &google_protobuf_DescriptorProto__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_DescriptorProto), 10, 0, false, true -}; - -google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_env *env) { - google_protobuf_DescriptorProto *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_DescriptorProto *msg = google_protobuf_DescriptorProto_new(env); - if (upb_decode(buf, msg, &google_protobuf_DescriptorProto_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_DescriptorProto_serialize(google_protobuf_DescriptorProto *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, env, size); -} -upb_stringview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) { - return msg->name; -} -void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_stringview value) { - msg->name = value; -} -const upb_array* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg) { - return msg->field; -} -void google_protobuf_DescriptorProto_set_field(google_protobuf_DescriptorProto *msg, upb_array* value) { - msg->field = value; -} -const upb_array* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg) { - return msg->nested_type; -} -void google_protobuf_DescriptorProto_set_nested_type(google_protobuf_DescriptorProto *msg, upb_array* value) { - msg->nested_type = value; -} -const upb_array* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg) { - return msg->enum_type; -} -void google_protobuf_DescriptorProto_set_enum_type(google_protobuf_DescriptorProto *msg, upb_array* value) { - msg->enum_type = value; -} -const upb_array* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg) { - return msg->extension_range; -} -void google_protobuf_DescriptorProto_set_extension_range(google_protobuf_DescriptorProto *msg, upb_array* value) { - msg->extension_range = value; -} -const upb_array* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg) { - return msg->extension; -} -void google_protobuf_DescriptorProto_set_extension(google_protobuf_DescriptorProto *msg, upb_array* value) { - msg->extension = value; -} -const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg) { - return msg->options; -} -void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value) { - msg->options = value; -} -const upb_array* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg) { - return msg->oneof_decl; -} -void google_protobuf_DescriptorProto_set_oneof_decl(google_protobuf_DescriptorProto *msg, upb_array* value) { - msg->oneof_decl = value; -} -const upb_array* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg) { - return msg->reserved_range; -} -void google_protobuf_DescriptorProto_set_reserved_range(google_protobuf_DescriptorProto *msg, upb_array* value) { - msg->reserved_range = value; -} -const upb_array* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg) { - return msg->reserved_name; -} -void google_protobuf_DescriptorProto_set_reserved_name(google_protobuf_DescriptorProto *msg, upb_array* value) { - msg->reserved_name = value; -} -struct google_protobuf_DescriptorProto_ExtensionRange { - int32_t start; - int32_t end; - google_protobuf_ExtensionRangeOptions* options; + UPB_SIZE(48, 96), 10, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = { +static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = { &google_protobuf_ExtensionRangeOptions_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_DescriptorProto_ExtensionRange__fields[3] = { - {1, offsetof(google_protobuf_DescriptorProto_ExtensionRange, start), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, - {2, offsetof(google_protobuf_DescriptorProto_ExtensionRange, end), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, - {3, offsetof(google_protobuf_DescriptorProto_ExtensionRange, options), 2, UPB_NOT_IN_ONEOF, 0, 11, 1}, +static const upb_msglayout_field google_protobuf_DescriptorProto_ExtensionRange__fields[3] = { + {1, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {2, UPB_SIZE(8, 8), 2, 0, 5, 1}, + {3, UPB_SIZE(12, 16), 3, 0, 11, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_ExtensionRange_msginit = { +const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = { &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0], &google_protobuf_DescriptorProto_ExtensionRange__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_DescriptorProto_ExtensionRange), 3, 0, false, true -}; - -google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_env *env) { - google_protobuf_DescriptorProto_ExtensionRange *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_DescriptorProto_ExtensionRange *msg = google_protobuf_DescriptorProto_ExtensionRange_new(env); - if (upb_decode(buf, msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_DescriptorProto_ExtensionRange_serialize(google_protobuf_DescriptorProto_ExtensionRange *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, env, size); -} -int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { - return msg->start; -} -void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { - msg->start = value; -} -int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { - return msg->end; -} -void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { - msg->end = value; -} -const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { - return msg->options; -} -void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value) { - msg->options = value; -} -struct google_protobuf_DescriptorProto_ReservedRange { - int32_t start; - int32_t end; + UPB_SIZE(16, 24), 3, false, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_DescriptorProto_ReservedRange__fields[2] = { - {1, offsetof(google_protobuf_DescriptorProto_ReservedRange, start), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, - {2, offsetof(google_protobuf_DescriptorProto_ReservedRange, end), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, +static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = { + {1, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {2, UPB_SIZE(8, 8), 2, 0, 5, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_ReservedRange_msginit = { +const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = { NULL, &google_protobuf_DescriptorProto_ReservedRange__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_DescriptorProto_ReservedRange), 2, 0, false, true -}; - -google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_env *env) { - google_protobuf_DescriptorProto_ReservedRange *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_DescriptorProto_ReservedRange *msg = google_protobuf_DescriptorProto_ReservedRange_new(env); - if (upb_decode(buf, msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_DescriptorProto_ReservedRange_serialize(google_protobuf_DescriptorProto_ReservedRange *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, env, size); -} -int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { - return msg->start; -} -void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { - msg->start = value; -} -int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { - return msg->end; -} -void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { - msg->end = value; -} -struct google_protobuf_ExtensionRangeOptions { - upb_array* uninterpreted_option; + UPB_SIZE(12, 12), 2, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_ExtensionRangeOptions_submsgs[1] = { +static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = { &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_ExtensionRangeOptions__fields[1] = { - {999, offsetof(google_protobuf_ExtensionRangeOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_ExtensionRangeOptions__fields[1] = { + {999, UPB_SIZE(0, 0), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_ExtensionRangeOptions_msginit = { +const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = { &google_protobuf_ExtensionRangeOptions_submsgs[0], &google_protobuf_ExtensionRangeOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_ExtensionRangeOptions), 1, 0, false, true -}; - -google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_env *env) { - google_protobuf_ExtensionRangeOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_ExtensionRangeOptions *msg = google_protobuf_ExtensionRangeOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_ExtensionRangeOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_ExtensionRangeOptions_serialize(google_protobuf_ExtensionRangeOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, env, size); -} -const upb_array* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_ExtensionRangeOptions_set_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_FieldDescriptorProto { - google_protobuf_FieldDescriptorProto_Label label; - google_protobuf_FieldDescriptorProto_Type type; - int32_t number; - int32_t oneof_index; - upb_stringview name; - upb_stringview extendee; - upb_stringview type_name; - upb_stringview default_value; - upb_stringview json_name; - google_protobuf_FieldOptions* options; + UPB_SIZE(4, 8), 1, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_FieldDescriptorProto_submsgs[1] = { +static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = { &google_protobuf_FieldOptions_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_FieldDescriptorProto__fields[10] = { - {1, offsetof(google_protobuf_FieldDescriptorProto, name), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {2, offsetof(google_protobuf_FieldDescriptorProto, extendee), 5, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {3, offsetof(google_protobuf_FieldDescriptorProto, number), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, - {4, offsetof(google_protobuf_FieldDescriptorProto, label), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1}, - {5, offsetof(google_protobuf_FieldDescriptorProto, type), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1}, - {6, offsetof(google_protobuf_FieldDescriptorProto, type_name), 6, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {7, offsetof(google_protobuf_FieldDescriptorProto, default_value), 7, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {8, offsetof(google_protobuf_FieldDescriptorProto, options), 9, UPB_NOT_IN_ONEOF, 0, 11, 1}, - {9, offsetof(google_protobuf_FieldDescriptorProto, oneof_index), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, - {10, offsetof(google_protobuf_FieldDescriptorProto, json_name), 8, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, +static const upb_msglayout_field google_protobuf_FieldDescriptorProto__fields[10] = { + {1, UPB_SIZE(32, 32), 5, 0, 9, 1}, + {2, UPB_SIZE(40, 48), 6, 0, 9, 1}, + {3, UPB_SIZE(24, 24), 3, 0, 5, 1}, + {4, UPB_SIZE(8, 8), 1, 0, 14, 1}, + {5, UPB_SIZE(16, 16), 2, 0, 14, 1}, + {6, UPB_SIZE(48, 64), 7, 0, 9, 1}, + {7, UPB_SIZE(56, 80), 8, 0, 9, 1}, + {8, UPB_SIZE(72, 112), 10, 0, 11, 1}, + {9, UPB_SIZE(28, 28), 4, 0, 5, 1}, + {10, UPB_SIZE(64, 96), 9, 0, 9, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_FieldDescriptorProto_msginit = { +const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = { &google_protobuf_FieldDescriptorProto_submsgs[0], &google_protobuf_FieldDescriptorProto__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_FieldDescriptorProto), 10, 0, false, true -}; - -google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_env *env) { - google_protobuf_FieldDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_FieldDescriptorProto *msg = google_protobuf_FieldDescriptorProto_new(env); - if (upb_decode(buf, msg, &google_protobuf_FieldDescriptorProto_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_FieldDescriptorProto_serialize(google_protobuf_FieldDescriptorProto *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, env, size); -} -upb_stringview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) { - return msg->name; -} -void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { - msg->name = value; -} -upb_stringview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) { - return msg->extendee; -} -void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { - msg->extendee = value; -} -int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) { - return msg->number; -} -void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value) { - msg->number = value; -} -google_protobuf_FieldDescriptorProto_Label google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) { - return msg->label; -} -void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Label value) { - msg->label = value; -} -google_protobuf_FieldDescriptorProto_Type google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) { - return msg->type; -} -void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Type value) { - msg->type = value; -} -upb_stringview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) { - return msg->type_name; -} -void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { - msg->type_name = value; -} -upb_stringview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) { - return msg->default_value; -} -void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { - msg->default_value = value; -} -const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg) { - return msg->options; -} -void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value) { - msg->options = value; -} -int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { - return msg->oneof_index; -} -void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto *msg, int32_t value) { - msg->oneof_index = value; -} -upb_stringview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) { - return msg->json_name; -} -void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { - msg->json_name = value; -} -struct google_protobuf_OneofDescriptorProto { - upb_stringview name; - google_protobuf_OneofOptions* options; + UPB_SIZE(80, 128), 10, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_OneofDescriptorProto_submsgs[1] = { +static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = { &google_protobuf_OneofOptions_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_OneofDescriptorProto__fields[2] = { - {1, offsetof(google_protobuf_OneofDescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {2, offsetof(google_protobuf_OneofDescriptorProto, options), 1, UPB_NOT_IN_ONEOF, 0, 11, 1}, +static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(12, 24), 2, 0, 11, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_OneofDescriptorProto_msginit = { +const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = { &google_protobuf_OneofDescriptorProto_submsgs[0], &google_protobuf_OneofDescriptorProto__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_OneofDescriptorProto), 2, 0, false, true -}; - -google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_env *env) { - google_protobuf_OneofDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_OneofDescriptorProto *msg = google_protobuf_OneofDescriptorProto_new(env); - if (upb_decode(buf, msg, &google_protobuf_OneofDescriptorProto_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_OneofDescriptorProto_serialize(google_protobuf_OneofDescriptorProto *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, env, size); -} -upb_stringview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) { - return msg->name; -} -void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_stringview value) { - msg->name = value; -} -const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg) { - return msg->options; -} -void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value) { - msg->options = value; -} -struct google_protobuf_EnumDescriptorProto { - upb_stringview name; - google_protobuf_EnumOptions* options; - upb_array* value; - upb_array* reserved_range; - upb_array* reserved_name; + UPB_SIZE(16, 32), 2, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_EnumDescriptorProto_submsgs[3] = { +static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = { &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, &google_protobuf_EnumOptions_msginit, &google_protobuf_EnumValueDescriptorProto_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_EnumDescriptorProto__fields[5] = { - {1, offsetof(google_protobuf_EnumDescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {2, offsetof(google_protobuf_EnumDescriptorProto, value), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 2, 11, 3}, - {3, offsetof(google_protobuf_EnumDescriptorProto, options), 1, UPB_NOT_IN_ONEOF, 1, 11, 1}, - {4, offsetof(google_protobuf_EnumDescriptorProto, reserved_range), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, - {5, offsetof(google_protobuf_EnumDescriptorProto, reserved_name), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 3}, +static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(16, 32), 0, 2, 11, 3}, + {3, UPB_SIZE(12, 24), 2, 1, 11, 1}, + {4, UPB_SIZE(20, 40), 0, 0, 11, 3}, + {5, UPB_SIZE(24, 48), 0, 0, 9, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_EnumDescriptorProto_msginit = { +const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = { &google_protobuf_EnumDescriptorProto_submsgs[0], &google_protobuf_EnumDescriptorProto__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_EnumDescriptorProto), 5, 0, false, true -}; - -google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_env *env) { - google_protobuf_EnumDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_EnumDescriptorProto *msg = google_protobuf_EnumDescriptorProto_new(env); - if (upb_decode(buf, msg, &google_protobuf_EnumDescriptorProto_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_EnumDescriptorProto_serialize(google_protobuf_EnumDescriptorProto *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, env, size); -} -upb_stringview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) { - return msg->name; -} -void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_stringview value) { - msg->name = value; -} -const upb_array* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg) { - return msg->value; -} -void google_protobuf_EnumDescriptorProto_set_value(google_protobuf_EnumDescriptorProto *msg, upb_array* value) { - msg->value = value; -} -const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg) { - return msg->options; -} -void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value) { - msg->options = value; -} -const upb_array* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg) { - return msg->reserved_range; -} -void google_protobuf_EnumDescriptorProto_set_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_array* value) { - msg->reserved_range = value; -} -const upb_array* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg) { - return msg->reserved_name; -} -void google_protobuf_EnumDescriptorProto_set_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_array* value) { - msg->reserved_name = value; -} -struct google_protobuf_EnumDescriptorProto_EnumReservedRange { - int32_t start; - int32_t end; + UPB_SIZE(32, 64), 5, false, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = { - {1, offsetof(google_protobuf_EnumDescriptorProto_EnumReservedRange, start), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, - {2, offsetof(google_protobuf_EnumDescriptorProto_EnumReservedRange, end), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, +static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = { + {1, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {2, UPB_SIZE(8, 8), 2, 0, 5, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = { +const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = { NULL, &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_EnumDescriptorProto_EnumReservedRange), 2, 0, false, true -}; - -google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_env *env) { - google_protobuf_EnumDescriptorProto_EnumReservedRange *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_EnumDescriptorProto_EnumReservedRange *msg = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(env); - if (upb_decode(buf, msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, env, size); -} -int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { - return msg->start; -} -void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { - msg->start = value; -} -int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { - return msg->end; -} -void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { - msg->end = value; -} -struct google_protobuf_EnumValueDescriptorProto { - int32_t number; - upb_stringview name; - google_protobuf_EnumValueOptions* options; + UPB_SIZE(12, 12), 2, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = { +static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = { &google_protobuf_EnumValueOptions_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_EnumValueDescriptorProto__fields[3] = { - {1, offsetof(google_protobuf_EnumValueDescriptorProto, name), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {2, offsetof(google_protobuf_EnumValueDescriptorProto, number), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, - {3, offsetof(google_protobuf_EnumValueDescriptorProto, options), 2, UPB_NOT_IN_ONEOF, 0, 11, 1}, +static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = { + {1, UPB_SIZE(8, 8), 2, 0, 9, 1}, + {2, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {3, UPB_SIZE(16, 24), 3, 0, 11, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_EnumValueDescriptorProto_msginit = { +const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = { &google_protobuf_EnumValueDescriptorProto_submsgs[0], &google_protobuf_EnumValueDescriptorProto__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_EnumValueDescriptorProto), 3, 0, false, true -}; - -google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_env *env) { - google_protobuf_EnumValueDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_EnumValueDescriptorProto *msg = google_protobuf_EnumValueDescriptorProto_new(env); - if (upb_decode(buf, msg, &google_protobuf_EnumValueDescriptorProto_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_EnumValueDescriptorProto_serialize(google_protobuf_EnumValueDescriptorProto *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, env, size); -} -upb_stringview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) { - return msg->name; -} -void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_stringview value) { - msg->name = value; -} -int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) { - return msg->number; -} -void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value) { - msg->number = value; -} -const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg) { - return msg->options; -} -void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value) { - msg->options = value; -} -struct google_protobuf_ServiceDescriptorProto { - upb_stringview name; - google_protobuf_ServiceOptions* options; - upb_array* method; + UPB_SIZE(24, 32), 3, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_ServiceDescriptorProto_submsgs[2] = { +static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = { &google_protobuf_MethodDescriptorProto_msginit, &google_protobuf_ServiceOptions_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_ServiceDescriptorProto__fields[3] = { - {1, offsetof(google_protobuf_ServiceDescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {2, offsetof(google_protobuf_ServiceDescriptorProto, method), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, - {3, offsetof(google_protobuf_ServiceDescriptorProto, options), 1, UPB_NOT_IN_ONEOF, 1, 11, 1}, +static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(16, 32), 0, 0, 11, 3}, + {3, UPB_SIZE(12, 24), 2, 1, 11, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_ServiceDescriptorProto_msginit = { +const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = { &google_protobuf_ServiceDescriptorProto_submsgs[0], &google_protobuf_ServiceDescriptorProto__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_ServiceDescriptorProto), 3, 0, false, true -}; - -google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_env *env) { - google_protobuf_ServiceDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_ServiceDescriptorProto *msg = google_protobuf_ServiceDescriptorProto_new(env); - if (upb_decode(buf, msg, &google_protobuf_ServiceDescriptorProto_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_ServiceDescriptorProto_serialize(google_protobuf_ServiceDescriptorProto *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, env, size); -} -upb_stringview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) { - return msg->name; -} -void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_stringview value) { - msg->name = value; -} -const upb_array* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg) { - return msg->method; -} -void google_protobuf_ServiceDescriptorProto_set_method(google_protobuf_ServiceDescriptorProto *msg, upb_array* value) { - msg->method = value; -} -const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg) { - return msg->options; -} -void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value) { - msg->options = value; -} -struct google_protobuf_MethodDescriptorProto { - bool client_streaming; - bool server_streaming; - upb_stringview name; - upb_stringview input_type; - upb_stringview output_type; - google_protobuf_MethodOptions* options; + UPB_SIZE(24, 48), 3, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_MethodDescriptorProto_submsgs[1] = { +static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = { &google_protobuf_MethodOptions_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_MethodDescriptorProto__fields[6] = { - {1, offsetof(google_protobuf_MethodDescriptorProto, name), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {2, offsetof(google_protobuf_MethodDescriptorProto, input_type), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {3, offsetof(google_protobuf_MethodDescriptorProto, output_type), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {4, offsetof(google_protobuf_MethodDescriptorProto, options), 5, UPB_NOT_IN_ONEOF, 0, 11, 1}, - {5, offsetof(google_protobuf_MethodDescriptorProto, client_streaming), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {6, offsetof(google_protobuf_MethodDescriptorProto, server_streaming), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, +static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = { + {1, UPB_SIZE(4, 8), 3, 0, 9, 1}, + {2, UPB_SIZE(12, 24), 4, 0, 9, 1}, + {3, UPB_SIZE(20, 40), 5, 0, 9, 1}, + {4, UPB_SIZE(28, 56), 6, 0, 11, 1}, + {5, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {6, UPB_SIZE(2, 2), 2, 0, 8, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_MethodDescriptorProto_msginit = { +const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = { &google_protobuf_MethodDescriptorProto_submsgs[0], &google_protobuf_MethodDescriptorProto__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_MethodDescriptorProto), 6, 0, false, true + UPB_SIZE(32, 64), 6, false, }; -google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_env *env) { - google_protobuf_MethodDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_MethodDescriptorProto *msg = google_protobuf_MethodDescriptorProto_new(env); - if (upb_decode(buf, msg, &google_protobuf_MethodDescriptorProto_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_MethodDescriptorProto_serialize(google_protobuf_MethodDescriptorProto *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, env, size); -} -upb_stringview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) { - return msg->name; -} -void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) { - msg->name = value; -} -upb_stringview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) { - return msg->input_type; -} -void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) { - msg->input_type = value; -} -upb_stringview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) { - return msg->output_type; -} -void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) { - msg->output_type = value; -} -const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg) { - return msg->options; -} -void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value) { - msg->options = value; -} -bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { - return msg->client_streaming; -} -void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) { - msg->client_streaming = value; -} -bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { - return msg->server_streaming; -} -void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) { - msg->server_streaming = value; -} -struct google_protobuf_FileOptions { - google_protobuf_FileOptions_OptimizeMode optimize_for; - bool java_multiple_files; - bool cc_generic_services; - bool java_generic_services; - bool py_generic_services; - bool java_generate_equals_and_hash; - bool deprecated; - bool java_string_check_utf8; - bool cc_enable_arenas; - bool php_generic_services; - upb_stringview java_package; - upb_stringview java_outer_classname; - upb_stringview go_package; - upb_stringview objc_class_prefix; - upb_stringview csharp_namespace; - upb_stringview swift_prefix; - upb_stringview php_class_prefix; - upb_stringview php_namespace; - upb_array* uninterpreted_option; +static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_FileOptions_submsgs[1] = { - &google_protobuf_UninterpretedOption_msginit, -}; - -static const upb_msglayout_fieldinit_v1 google_protobuf_FileOptions__fields[19] = { - {1, offsetof(google_protobuf_FileOptions, java_package), 10, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {8, offsetof(google_protobuf_FileOptions, java_outer_classname), 11, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {9, offsetof(google_protobuf_FileOptions, optimize_for), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1}, - {10, offsetof(google_protobuf_FileOptions, java_multiple_files), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {11, offsetof(google_protobuf_FileOptions, go_package), 12, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {16, offsetof(google_protobuf_FileOptions, cc_generic_services), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {17, offsetof(google_protobuf_FileOptions, java_generic_services), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {18, offsetof(google_protobuf_FileOptions, py_generic_services), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {20, offsetof(google_protobuf_FileOptions, java_generate_equals_and_hash), 5, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {23, offsetof(google_protobuf_FileOptions, deprecated), 6, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {27, offsetof(google_protobuf_FileOptions, java_string_check_utf8), 7, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {31, offsetof(google_protobuf_FileOptions, cc_enable_arenas), 8, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {36, offsetof(google_protobuf_FileOptions, objc_class_prefix), 13, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {37, offsetof(google_protobuf_FileOptions, csharp_namespace), 14, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {39, offsetof(google_protobuf_FileOptions, swift_prefix), 15, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {40, offsetof(google_protobuf_FileOptions, php_class_prefix), 16, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {41, offsetof(google_protobuf_FileOptions, php_namespace), 17, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {42, offsetof(google_protobuf_FileOptions, php_generic_services), 9, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {999, offsetof(google_protobuf_FileOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, -}; - -const upb_msglayout_msginit_v1 google_protobuf_FileOptions_msginit = { +static const upb_msglayout_field google_protobuf_FileOptions__fields[19] = { + {1, UPB_SIZE(28, 32), 11, 0, 9, 1}, + {8, UPB_SIZE(36, 48), 12, 0, 9, 1}, + {9, UPB_SIZE(8, 8), 1, 0, 14, 1}, + {10, UPB_SIZE(16, 16), 2, 0, 8, 1}, + {11, UPB_SIZE(44, 64), 13, 0, 9, 1}, + {16, UPB_SIZE(17, 17), 3, 0, 8, 1}, + {17, UPB_SIZE(18, 18), 4, 0, 8, 1}, + {18, UPB_SIZE(19, 19), 5, 0, 8, 1}, + {20, UPB_SIZE(20, 20), 6, 0, 8, 1}, + {23, UPB_SIZE(21, 21), 7, 0, 8, 1}, + {27, UPB_SIZE(22, 22), 8, 0, 8, 1}, + {31, UPB_SIZE(23, 23), 9, 0, 8, 1}, + {36, UPB_SIZE(52, 80), 14, 0, 9, 1}, + {37, UPB_SIZE(60, 96), 15, 0, 9, 1}, + {39, UPB_SIZE(68, 112), 16, 0, 9, 1}, + {40, UPB_SIZE(76, 128), 17, 0, 9, 1}, + {41, UPB_SIZE(84, 144), 18, 0, 9, 1}, + {42, UPB_SIZE(24, 24), 10, 0, 8, 1}, + {999, UPB_SIZE(92, 160), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_FileOptions_msginit = { &google_protobuf_FileOptions_submsgs[0], &google_protobuf_FileOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_FileOptions), 19, 0, false, true -}; - -google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_env *env) { - google_protobuf_FileOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_FileOptions *google_protobuf_FileOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_FileOptions *msg = google_protobuf_FileOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_FileOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_FileOptions_serialize(google_protobuf_FileOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_FileOptions_msginit, env, size); -} -upb_stringview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) { - return msg->java_package; -} -void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_stringview value) { - msg->java_package = value; -} -upb_stringview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) { - return msg->java_outer_classname; -} -void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_stringview value) { - msg->java_outer_classname = value; -} -google_protobuf_FileOptions_OptimizeMode google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) { - return msg->optimize_for; -} -void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, google_protobuf_FileOptions_OptimizeMode value) { - msg->optimize_for = value; -} -bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) { - return msg->java_multiple_files; -} -void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value) { - msg->java_multiple_files = value; -} -upb_stringview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) { - return msg->go_package; -} -void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_stringview value) { - msg->go_package = value; -} -bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) { - return msg->cc_generic_services; -} -void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value) { - msg->cc_generic_services = value; -} -bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) { - return msg->java_generic_services; -} -void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value) { - msg->java_generic_services = value; -} -bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) { - return msg->py_generic_services; -} -void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value) { - msg->py_generic_services = value; -} -bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { - return msg->java_generate_equals_and_hash; -} -void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value) { - msg->java_generate_equals_and_hash = value; -} -bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) { - return msg->deprecated; -} -void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value) { - msg->deprecated = value; -} -bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg) { - return msg->java_string_check_utf8; -} -void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value) { - msg->java_string_check_utf8 = value; -} -bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) { - return msg->cc_enable_arenas; -} -void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value) { - msg->cc_enable_arenas = value; -} -upb_stringview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) { - return msg->objc_class_prefix; -} -void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_stringview value) { - msg->objc_class_prefix = value; -} -upb_stringview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) { - return msg->csharp_namespace; -} -void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_stringview value) { - msg->csharp_namespace = value; -} -upb_stringview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) { - return msg->swift_prefix; -} -void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_stringview value) { - msg->swift_prefix = value; -} -upb_stringview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) { - return msg->php_class_prefix; -} -void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_stringview value) { - msg->php_class_prefix = value; -} -upb_stringview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) { - return msg->php_namespace; -} -void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_stringview value) { - msg->php_namespace = value; -} -bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) { - return msg->php_generic_services; -} -void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value) { - msg->php_generic_services = value; -} -const upb_array* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_FileOptions_set_uninterpreted_option(google_protobuf_FileOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_MessageOptions { - bool message_set_wire_format; - bool no_standard_descriptor_accessor; - bool deprecated; - bool map_entry; - upb_array* uninterpreted_option; + UPB_SIZE(96, 176), 19, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_MessageOptions_submsgs[1] = { +static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = { &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_MessageOptions__fields[5] = { - {1, offsetof(google_protobuf_MessageOptions, message_set_wire_format), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {2, offsetof(google_protobuf_MessageOptions, no_standard_descriptor_accessor), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {3, offsetof(google_protobuf_MessageOptions, deprecated), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {7, offsetof(google_protobuf_MessageOptions, map_entry), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {999, offsetof(google_protobuf_MessageOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_MessageOptions__fields[5] = { + {1, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {2, UPB_SIZE(2, 2), 2, 0, 8, 1}, + {3, UPB_SIZE(3, 3), 3, 0, 8, 1}, + {7, UPB_SIZE(4, 4), 4, 0, 8, 1}, + {999, UPB_SIZE(8, 8), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_MessageOptions_msginit = { +const upb_msglayout google_protobuf_MessageOptions_msginit = { &google_protobuf_MessageOptions_submsgs[0], &google_protobuf_MessageOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_MessageOptions), 5, 0, false, true -}; - -google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_env *env) { - google_protobuf_MessageOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_MessageOptions *google_protobuf_MessageOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_MessageOptions *msg = google_protobuf_MessageOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_MessageOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_MessageOptions_serialize(google_protobuf_MessageOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_MessageOptions_msginit, env, size); -} -bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) { - return msg->message_set_wire_format; -} -void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value) { - msg->message_set_wire_format = value; -} -bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { - return msg->no_standard_descriptor_accessor; -} -void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value) { - msg->no_standard_descriptor_accessor = value; -} -bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) { - return msg->deprecated; -} -void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value) { - msg->deprecated = value; -} -bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) { - return msg->map_entry; -} -void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value) { - msg->map_entry = value; -} -const upb_array* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_MessageOptions_set_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_FieldOptions { - google_protobuf_FieldOptions_CType ctype; - google_protobuf_FieldOptions_JSType jstype; - bool packed; - bool deprecated; - bool lazy; - bool weak; - upb_array* uninterpreted_option; + UPB_SIZE(12, 16), 5, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_FieldOptions_submsgs[1] = { +static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = { &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_FieldOptions__fields[7] = { - {1, offsetof(google_protobuf_FieldOptions, ctype), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1}, - {2, offsetof(google_protobuf_FieldOptions, packed), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {3, offsetof(google_protobuf_FieldOptions, deprecated), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {5, offsetof(google_protobuf_FieldOptions, lazy), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {6, offsetof(google_protobuf_FieldOptions, jstype), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1}, - {10, offsetof(google_protobuf_FieldOptions, weak), 5, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {999, offsetof(google_protobuf_FieldOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_FieldOptions__fields[7] = { + {1, UPB_SIZE(8, 8), 1, 0, 14, 1}, + {2, UPB_SIZE(24, 24), 3, 0, 8, 1}, + {3, UPB_SIZE(25, 25), 4, 0, 8, 1}, + {5, UPB_SIZE(26, 26), 5, 0, 8, 1}, + {6, UPB_SIZE(16, 16), 2, 0, 14, 1}, + {10, UPB_SIZE(27, 27), 6, 0, 8, 1}, + {999, UPB_SIZE(28, 32), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_FieldOptions_msginit = { +const upb_msglayout google_protobuf_FieldOptions_msginit = { &google_protobuf_FieldOptions_submsgs[0], &google_protobuf_FieldOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_FieldOptions), 7, 0, false, true -}; - -google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_env *env) { - google_protobuf_FieldOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_FieldOptions *google_protobuf_FieldOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_FieldOptions *msg = google_protobuf_FieldOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_FieldOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_FieldOptions_serialize(google_protobuf_FieldOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_FieldOptions_msginit, env, size); -} -google_protobuf_FieldOptions_CType google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) { - return msg->ctype; -} -void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_CType value) { - msg->ctype = value; -} -bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) { - return msg->packed; -} -void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value) { - msg->packed = value; -} -bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) { - return msg->deprecated; -} -void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value) { - msg->deprecated = value; -} -bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) { - return msg->lazy; -} -void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value) { - msg->lazy = value; -} -google_protobuf_FieldOptions_JSType google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) { - return msg->jstype; -} -void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_JSType value) { - msg->jstype = value; -} -bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) { - return msg->weak; -} -void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value) { - msg->weak = value; -} -const upb_array* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_FieldOptions_set_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_OneofOptions { - upb_array* uninterpreted_option; + UPB_SIZE(32, 40), 7, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_OneofOptions_submsgs[1] = { +static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = { &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_OneofOptions__fields[1] = { - {999, offsetof(google_protobuf_OneofOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_OneofOptions__fields[1] = { + {999, UPB_SIZE(0, 0), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_OneofOptions_msginit = { +const upb_msglayout google_protobuf_OneofOptions_msginit = { &google_protobuf_OneofOptions_submsgs[0], &google_protobuf_OneofOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_OneofOptions), 1, 0, false, true -}; - -google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_env *env) { - google_protobuf_OneofOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_OneofOptions *google_protobuf_OneofOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_OneofOptions *msg = google_protobuf_OneofOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_OneofOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_OneofOptions_serialize(google_protobuf_OneofOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_OneofOptions_msginit, env, size); -} -const upb_array* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_OneofOptions_set_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_EnumOptions { - bool allow_alias; - bool deprecated; - upb_array* uninterpreted_option; + UPB_SIZE(4, 8), 1, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_EnumOptions_submsgs[1] = { +static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = { &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_EnumOptions__fields[3] = { - {2, offsetof(google_protobuf_EnumOptions, allow_alias), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {3, offsetof(google_protobuf_EnumOptions, deprecated), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {999, offsetof(google_protobuf_EnumOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_EnumOptions__fields[3] = { + {2, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {3, UPB_SIZE(2, 2), 2, 0, 8, 1}, + {999, UPB_SIZE(4, 8), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_EnumOptions_msginit = { +const upb_msglayout google_protobuf_EnumOptions_msginit = { &google_protobuf_EnumOptions_submsgs[0], &google_protobuf_EnumOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_EnumOptions), 3, 0, false, true + UPB_SIZE(8, 16), 3, false, }; -google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_env *env) { - google_protobuf_EnumOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_EnumOptions *google_protobuf_EnumOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_EnumOptions *msg = google_protobuf_EnumOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_EnumOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_EnumOptions_serialize(google_protobuf_EnumOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_EnumOptions_msginit, env, size); -} -bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) { - return msg->allow_alias; -} -void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value) { - msg->allow_alias = value; -} -bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) { - return msg->deprecated; -} -void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value) { - msg->deprecated = value; -} -const upb_array* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_EnumOptions_set_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_EnumValueOptions { - bool deprecated; - upb_array* uninterpreted_option; -}; - -static const upb_msglayout_msginit_v1 *const google_protobuf_EnumValueOptions_submsgs[1] = { +static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = { &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_EnumValueOptions__fields[2] = { - {1, offsetof(google_protobuf_EnumValueOptions, deprecated), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {999, offsetof(google_protobuf_EnumValueOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_EnumValueOptions__fields[2] = { + {1, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {999, UPB_SIZE(4, 8), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_EnumValueOptions_msginit = { +const upb_msglayout google_protobuf_EnumValueOptions_msginit = { &google_protobuf_EnumValueOptions_submsgs[0], &google_protobuf_EnumValueOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_EnumValueOptions), 2, 0, false, true + UPB_SIZE(8, 16), 2, false, }; -google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_env *env) { - google_protobuf_EnumValueOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_EnumValueOptions *msg = google_protobuf_EnumValueOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_EnumValueOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_EnumValueOptions_serialize(google_protobuf_EnumValueOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, env, size); -} -bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) { - return msg->deprecated; -} -void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value) { - msg->deprecated = value; -} -const upb_array* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_EnumValueOptions_set_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_ServiceOptions { - bool deprecated; - upb_array* uninterpreted_option; -}; - -static const upb_msglayout_msginit_v1 *const google_protobuf_ServiceOptions_submsgs[1] = { +static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = { &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_ServiceOptions__fields[2] = { - {33, offsetof(google_protobuf_ServiceOptions, deprecated), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {999, offsetof(google_protobuf_ServiceOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_ServiceOptions__fields[2] = { + {33, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {999, UPB_SIZE(4, 8), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_ServiceOptions_msginit = { +const upb_msglayout google_protobuf_ServiceOptions_msginit = { &google_protobuf_ServiceOptions_submsgs[0], &google_protobuf_ServiceOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_ServiceOptions), 2, 0, false, true -}; - -google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_env *env) { - google_protobuf_ServiceOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_ServiceOptions *msg = google_protobuf_ServiceOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_ServiceOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_ServiceOptions_serialize(google_protobuf_ServiceOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, env, size); -} -bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) { - return msg->deprecated; -} -void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value) { - msg->deprecated = value; -} -const upb_array* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_ServiceOptions_set_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_MethodOptions { - google_protobuf_MethodOptions_IdempotencyLevel idempotency_level; - bool deprecated; - upb_array* uninterpreted_option; + UPB_SIZE(8, 16), 2, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_MethodOptions_submsgs[1] = { +static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = { &google_protobuf_UninterpretedOption_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_MethodOptions__fields[3] = { - {33, offsetof(google_protobuf_MethodOptions, deprecated), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1}, - {34, offsetof(google_protobuf_MethodOptions, idempotency_level), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1}, - {999, offsetof(google_protobuf_MethodOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_MethodOptions__fields[3] = { + {33, UPB_SIZE(16, 16), 2, 0, 8, 1}, + {34, UPB_SIZE(8, 8), 1, 0, 14, 1}, + {999, UPB_SIZE(20, 24), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_MethodOptions_msginit = { +const upb_msglayout google_protobuf_MethodOptions_msginit = { &google_protobuf_MethodOptions_submsgs[0], &google_protobuf_MethodOptions__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_MethodOptions), 3, 0, false, true -}; - -google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_env *env) { - google_protobuf_MethodOptions *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_MethodOptions *google_protobuf_MethodOptions_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_MethodOptions *msg = google_protobuf_MethodOptions_new(env); - if (upb_decode(buf, msg, &google_protobuf_MethodOptions_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_MethodOptions_serialize(google_protobuf_MethodOptions *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_MethodOptions_msginit, env, size); -} -bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) { - return msg->deprecated; -} -void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value) { - msg->deprecated = value; -} -google_protobuf_MethodOptions_IdempotencyLevel google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) { - return msg->idempotency_level; -} -void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, google_protobuf_MethodOptions_IdempotencyLevel value) { - msg->idempotency_level = value; -} -const upb_array* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg) { - return msg->uninterpreted_option; -} -void google_protobuf_MethodOptions_set_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_array* value) { - msg->uninterpreted_option = value; -} -struct google_protobuf_UninterpretedOption { - uint64_t positive_int_value; - int64_t negative_int_value; - double double_value; - upb_stringview identifier_value; - upb_stringview string_value; - upb_stringview aggregate_value; - upb_array* name; + UPB_SIZE(24, 32), 3, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_UninterpretedOption_submsgs[1] = { +static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = { &google_protobuf_UninterpretedOption_NamePart_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_UninterpretedOption__fields[7] = { - {2, offsetof(google_protobuf_UninterpretedOption, name), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, - {3, offsetof(google_protobuf_UninterpretedOption, identifier_value), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {4, offsetof(google_protobuf_UninterpretedOption, positive_int_value), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 4, 1}, - {5, offsetof(google_protobuf_UninterpretedOption, negative_int_value), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 3, 1}, - {6, offsetof(google_protobuf_UninterpretedOption, double_value), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 1, 1}, - {7, offsetof(google_protobuf_UninterpretedOption, string_value), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 12, 1}, - {8, offsetof(google_protobuf_UninterpretedOption, aggregate_value), 5, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, +static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7] = { + {2, UPB_SIZE(56, 80), 0, 0, 11, 3}, + {3, UPB_SIZE(32, 32), 4, 0, 9, 1}, + {4, UPB_SIZE(8, 8), 1, 0, 4, 1}, + {5, UPB_SIZE(16, 16), 2, 0, 3, 1}, + {6, UPB_SIZE(24, 24), 3, 0, 1, 1}, + {7, UPB_SIZE(40, 48), 5, 0, 12, 1}, + {8, UPB_SIZE(48, 64), 6, 0, 9, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_UninterpretedOption_msginit = { +const upb_msglayout google_protobuf_UninterpretedOption_msginit = { &google_protobuf_UninterpretedOption_submsgs[0], &google_protobuf_UninterpretedOption__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_UninterpretedOption), 7, 0, false, true -}; - -google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_env *env) { - google_protobuf_UninterpretedOption *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_UninterpretedOption *msg = google_protobuf_UninterpretedOption_new(env); - if (upb_decode(buf, msg, &google_protobuf_UninterpretedOption_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_UninterpretedOption_serialize(google_protobuf_UninterpretedOption *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, env, size); -} -const upb_array* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg) { - return msg->name; -} -void google_protobuf_UninterpretedOption_set_name(google_protobuf_UninterpretedOption *msg, upb_array* value) { - msg->name = value; -} -upb_stringview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) { - return msg->identifier_value; -} -void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) { - msg->identifier_value = value; -} -uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg) { - return msg->positive_int_value; -} -void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value) { - msg->positive_int_value = value; -} -int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg) { - return msg->negative_int_value; -} -void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value) { - msg->negative_int_value = value; -} -double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) { - return msg->double_value; -} -void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value) { - msg->double_value = value; -} -upb_stringview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) { - return msg->string_value; -} -void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) { - msg->string_value = value; -} -upb_stringview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) { - return msg->aggregate_value; -} -void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) { - msg->aggregate_value = value; -} -struct google_protobuf_UninterpretedOption_NamePart { - bool is_extension; - upb_stringview name_part; + UPB_SIZE(64, 96), 7, false, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_UninterpretedOption_NamePart__fields[2] = { - {1, offsetof(google_protobuf_UninterpretedOption_NamePart, name_part), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 2}, - {2, offsetof(google_protobuf_UninterpretedOption_NamePart, is_extension), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 2}, +static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = { + {1, UPB_SIZE(4, 8), 2, 0, 9, 2}, + {2, UPB_SIZE(1, 1), 1, 0, 8, 2}, }; -const upb_msglayout_msginit_v1 google_protobuf_UninterpretedOption_NamePart_msginit = { +const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = { NULL, &google_protobuf_UninterpretedOption_NamePart__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_UninterpretedOption_NamePart), 2, 0, false, true -}; - -google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_env *env) { - google_protobuf_UninterpretedOption_NamePart *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_UninterpretedOption_NamePart *msg = google_protobuf_UninterpretedOption_NamePart_new(env); - if (upb_decode(buf, msg, &google_protobuf_UninterpretedOption_NamePart_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_UninterpretedOption_NamePart_serialize(google_protobuf_UninterpretedOption_NamePart *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, env, size); -} -upb_stringview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { - return msg->name_part; -} -void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_stringview value) { - msg->name_part = value; -} -bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { - return msg->is_extension; -} -void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value) { - msg->is_extension = value; -} -struct google_protobuf_SourceCodeInfo { - upb_array* location; + UPB_SIZE(16, 32), 2, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_SourceCodeInfo_submsgs[1] = { +static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = { &google_protobuf_SourceCodeInfo_Location_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_SourceCodeInfo__fields[1] = { - {1, offsetof(google_protobuf_SourceCodeInfo, location), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_SourceCodeInfo__fields[1] = { + {1, UPB_SIZE(0, 0), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_SourceCodeInfo_msginit = { +const upb_msglayout google_protobuf_SourceCodeInfo_msginit = { &google_protobuf_SourceCodeInfo_submsgs[0], &google_protobuf_SourceCodeInfo__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_SourceCodeInfo), 1, 0, false, true -}; - -google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_env *env) { - google_protobuf_SourceCodeInfo *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_SourceCodeInfo *msg = google_protobuf_SourceCodeInfo_new(env); - if (upb_decode(buf, msg, &google_protobuf_SourceCodeInfo_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_SourceCodeInfo_serialize(google_protobuf_SourceCodeInfo *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, env, size); -} -const upb_array* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg) { - return msg->location; -} -void google_protobuf_SourceCodeInfo_set_location(google_protobuf_SourceCodeInfo *msg, upb_array* value) { - msg->location = value; -} -struct google_protobuf_SourceCodeInfo_Location { - upb_stringview leading_comments; - upb_stringview trailing_comments; - upb_array* path; - upb_array* span; - upb_array* leading_detached_comments; + UPB_SIZE(4, 8), 1, false, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_SourceCodeInfo_Location__fields[5] = { - {1, offsetof(google_protobuf_SourceCodeInfo_Location, path), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3}, - {2, offsetof(google_protobuf_SourceCodeInfo_Location, span), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3}, - {3, offsetof(google_protobuf_SourceCodeInfo_Location, leading_comments), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {4, offsetof(google_protobuf_SourceCodeInfo_Location, trailing_comments), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {6, offsetof(google_protobuf_SourceCodeInfo_Location, leading_detached_comments), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 3}, +static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = { + {1, UPB_SIZE(20, 40), 0, 0, 5, 3}, + {2, UPB_SIZE(24, 48), 0, 0, 5, 3}, + {3, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {4, UPB_SIZE(12, 24), 2, 0, 9, 1}, + {6, UPB_SIZE(28, 56), 0, 0, 9, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_SourceCodeInfo_Location_msginit = { +const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = { NULL, &google_protobuf_SourceCodeInfo_Location__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_SourceCodeInfo_Location), 5, 0, false, true -}; - -google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_env *env) { - google_protobuf_SourceCodeInfo_Location *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_SourceCodeInfo_Location *msg = google_protobuf_SourceCodeInfo_Location_new(env); - if (upb_decode(buf, msg, &google_protobuf_SourceCodeInfo_Location_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_SourceCodeInfo_Location_serialize(google_protobuf_SourceCodeInfo_Location *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, env, size); -} -const upb_array* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg) { - return msg->path; -} -void google_protobuf_SourceCodeInfo_Location_set_path(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) { - msg->path = value; -} -const upb_array* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg) { - return msg->span; -} -void google_protobuf_SourceCodeInfo_Location_set_span(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) { - msg->span = value; -} -upb_stringview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { - return msg->leading_comments; -} -void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_stringview value) { - msg->leading_comments = value; -} -upb_stringview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { - return msg->trailing_comments; -} -void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_stringview value) { - msg->trailing_comments = value; -} -const upb_array* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg) { - return msg->leading_detached_comments; -} -void google_protobuf_SourceCodeInfo_Location_set_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) { - msg->leading_detached_comments = value; -} -struct google_protobuf_GeneratedCodeInfo { - upb_array* annotation; + UPB_SIZE(32, 64), 5, false, }; -static const upb_msglayout_msginit_v1 *const google_protobuf_GeneratedCodeInfo_submsgs[1] = { +static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = { &google_protobuf_GeneratedCodeInfo_Annotation_msginit, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_GeneratedCodeInfo__fields[1] = { - {1, offsetof(google_protobuf_GeneratedCodeInfo, annotation), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3}, +static const upb_msglayout_field google_protobuf_GeneratedCodeInfo__fields[1] = { + {1, UPB_SIZE(0, 0), 0, 0, 11, 3}, }; -const upb_msglayout_msginit_v1 google_protobuf_GeneratedCodeInfo_msginit = { +const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = { &google_protobuf_GeneratedCodeInfo_submsgs[0], &google_protobuf_GeneratedCodeInfo__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_GeneratedCodeInfo), 1, 0, false, true -}; - -google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_env *env) { - google_protobuf_GeneratedCodeInfo *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_GeneratedCodeInfo *msg = google_protobuf_GeneratedCodeInfo_new(env); - if (upb_decode(buf, msg, &google_protobuf_GeneratedCodeInfo_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_GeneratedCodeInfo_serialize(google_protobuf_GeneratedCodeInfo *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, env, size); -} -const upb_array* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg) { - return msg->annotation; -} -void google_protobuf_GeneratedCodeInfo_set_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_array* value) { - msg->annotation = value; -} -struct google_protobuf_GeneratedCodeInfo_Annotation { - int32_t begin; - int32_t end; - upb_stringview source_file; - upb_array* path; + UPB_SIZE(4, 8), 1, false, }; -static const upb_msglayout_fieldinit_v1 google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = { - {1, offsetof(google_protobuf_GeneratedCodeInfo_Annotation, path), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3}, - {2, offsetof(google_protobuf_GeneratedCodeInfo_Annotation, source_file), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1}, - {3, offsetof(google_protobuf_GeneratedCodeInfo_Annotation, begin), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, - {4, offsetof(google_protobuf_GeneratedCodeInfo_Annotation, end), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1}, +static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = { + {1, UPB_SIZE(20, 32), 0, 0, 5, 3}, + {2, UPB_SIZE(12, 16), 3, 0, 9, 1}, + {3, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {4, UPB_SIZE(8, 8), 2, 0, 5, 1}, }; -const upb_msglayout_msginit_v1 google_protobuf_GeneratedCodeInfo_Annotation_msginit = { +const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = { NULL, &google_protobuf_GeneratedCodeInfo_Annotation__fields[0], - NULL, - NULL, /* TODO. default_msg */ - UPB_ALIGNED_SIZEOF(google_protobuf_GeneratedCodeInfo_Annotation), 4, 0, false, true + UPB_SIZE(24, 48), 4, false, }; -google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_env *env) { - google_protobuf_GeneratedCodeInfo_Annotation *msg = upb_env_malloc(env, sizeof(*msg)); - memset(msg, 0, sizeof(*msg)); /* TODO: defaults */ - return msg; -} -google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parsenew(upb_stringview buf, upb_env *env) { - google_protobuf_GeneratedCodeInfo_Annotation *msg = google_protobuf_GeneratedCodeInfo_Annotation_new(env); - if (upb_decode(buf, msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, env)) { - return msg; - } else { - return NULL; - } -} -char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_env *env, size_t *size) { - return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, env, size); -} -const upb_array* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { - return msg->path; -} -void google_protobuf_GeneratedCodeInfo_Annotation_set_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_array* value) { - msg->path = value; -} -upb_stringview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { - return msg->source_file; -} -void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_stringview value) { - msg->source_file = value; -} -int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { - return msg->begin; -} -void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) { - msg->begin = value; -} -int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { - return msg->end; -} -void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) { - msg->end = value; -} +#include + /* Maps descriptor type -> upb field type. */ const uint8_t upb_desctype_to_fieldtype[] = { UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */ @@ -1926,7 +529,6 @@ const uint8_t upb_desctype_to_fieldtype[] = { /* Data pertaining to the parse. */ typedef struct { - upb_env *env; /* Current decoding pointer. Points to the beginning of a field until we * have finished decoding the whole field. */ const char *ptr; @@ -1939,7 +541,7 @@ typedef struct { /* These members are unset for an unknown group frame. */ char *msg; - const upb_msglayout_msginit_v1 *m; + const upb_msglayout *m; } upb_decframe; #define CHK(x) if (!(x)) { return false; } @@ -1948,7 +550,7 @@ static bool upb_skip_unknowngroup(upb_decstate *d, int field_number, const char *limit); static bool upb_decode_message(upb_decstate *d, const char *limit, int group_number, char *msg, - const upb_msglayout_msginit_v1 *l); + const upb_msglayout *l); static bool upb_decode_varint(const char **ptr, const char *limit, uint64_t *val) { @@ -2011,14 +613,14 @@ static int64_t upb_zzdecode_64(uint64_t n) { } static bool upb_decode_string(const char **ptr, const char *limit, - upb_stringview *val) { + upb_strview *val) { uint32_t len; CHK(upb_decode_varint32(ptr, limit, &len) && len < INT32_MAX && limit - *ptr >= (int32_t)len); - *val = upb_stringview_make(*ptr, len); + *val = upb_strview_make(*ptr, len); *ptr += len; return true; } @@ -2029,9 +631,7 @@ static void upb_set32(void *msg, size_t ofs, uint32_t val) { static bool upb_append_unknown(upb_decstate *d, upb_decframe *frame, const char *start) { - UPB_UNUSED(d); - UPB_UNUSED(frame); - UPB_UNUSED(start); + upb_msg_addunknown(frame->msg, start, d->ptr - start); return true; } @@ -2051,7 +651,7 @@ static bool upb_skip_unknownfielddata(upb_decstate *d, upb_decframe *frame, return upb_decode_64bit(&d->ptr, frame->limit, &val); } case UPB_WIRE_TYPE_DELIMITED: { - upb_stringview val; + upb_strview val; return upb_decode_string(&d->ptr, frame->limit, &val); } case UPB_WIRE_TYPE_START_GROUP: @@ -2070,6 +670,7 @@ static bool upb_array_grow(upb_array *arr, size_t elements) { size_t new_bytes; size_t old_bytes; void *new_data; + upb_alloc *alloc = upb_arena_alloc(arr->arena); while (new_size < needed) { new_size *= 2; @@ -2077,7 +678,7 @@ static bool upb_array_grow(upb_array *arr, size_t elements) { old_bytes = arr->len * arr->element_size; new_bytes = new_size * arr->element_size; - new_data = upb_realloc(arr->alloc, arr->data, old_bytes, new_bytes); + new_data = upb_realloc(alloc, arr->data, old_bytes, new_bytes); CHK(new_data); arr->data = new_data; @@ -2099,23 +700,21 @@ static void *upb_array_add(upb_array *arr, size_t elements) { } static upb_array *upb_getarr(upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { UPB_ASSERT(field->label == UPB_LABEL_REPEATED); return *(upb_array**)&frame->msg[field->offset]; } -static upb_array *upb_getorcreatearr(upb_decstate *d, - upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { +static upb_array *upb_getorcreatearr(upb_decframe *frame, + const upb_msglayout_field *field) { upb_array *arr = upb_getarr(frame, field); if (!arr) { - arr = upb_env_malloc(d->env, sizeof(*arr)); + upb_fieldtype_t type = upb_desctype_to_fieldtype[field->descriptortype]; + arr = upb_array_new(type, upb_msg_arena(frame->msg)); if (!arr) { return NULL; } - upb_array_init(arr, upb_desctype_to_fieldtype[field->descriptortype], - upb_arena_alloc(upb_env_arena(d->env))); *(upb_array**)&frame->msg[field->offset] = arr; } @@ -2123,26 +722,25 @@ static upb_array *upb_getorcreatearr(upb_decstate *d, } static void upb_sethasbit(upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { - UPB_ASSERT(field->hasbit != UPB_NO_HASBIT); - frame->msg[field->hasbit / 8] |= (1 << (field->hasbit % 8)); + const upb_msglayout_field *field) { + int32_t hasbit = field->presence; + UPB_ASSERT(field->presence > 0); + frame->msg[hasbit / 8] |= (1 << (hasbit % 8)); } static void upb_setoneofcase(upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { - UPB_ASSERT(field->oneof_index != UPB_NOT_IN_ONEOF); - upb_set32(frame->msg, frame->m->oneofs[field->oneof_index].case_offset, - field->number); + const upb_msglayout_field *field) { + UPB_ASSERT(field->presence < 0); + upb_set32(frame->msg, ~field->presence, field->number); } -static char *upb_decode_prepareslot(upb_decstate *d, - upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { +static char *upb_decode_prepareslot(upb_decframe *frame, + const upb_msglayout_field *field) { char *field_mem = frame->msg + field->offset; upb_array *arr; if (field->label == UPB_LABEL_REPEATED) { - arr = upb_getorcreatearr(d, frame, field); + arr = upb_getorcreatearr(frame, field); field_mem = upb_array_reserve(arr, 1); } @@ -2150,36 +748,32 @@ static char *upb_decode_prepareslot(upb_decstate *d, } static void upb_decode_setpresent(upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { if (field->label == UPB_LABEL_REPEATED) { upb_array *arr = upb_getarr(frame, field); UPB_ASSERT(arr->len < arr->size); arr->len++; - } else if (field->oneof_index != UPB_NOT_IN_ONEOF) { + } else if (field->presence < 0) { upb_setoneofcase(frame, field); - } else if (field->hasbit != UPB_NO_HASBIT) { + } else if (field->presence > 0) { upb_sethasbit(frame, field); } } -static bool upb_decode_submsg(upb_decstate *d, - upb_decframe *frame, +static bool upb_decode_submsg(upb_decstate *d, upb_decframe *frame, const char *limit, - const upb_msglayout_fieldinit_v1 *field, + const upb_msglayout_field *field, int group_number) { - char *submsg_slot = upb_decode_prepareslot(d, frame, field); - char *submsg = *(void**)submsg_slot; - const upb_msglayout_msginit_v1 *subm; + char *submsg_slot = upb_decode_prepareslot(frame, field); + char *submsg = *(void **)submsg_slot; + const upb_msglayout *subm; - UPB_ASSERT(field->submsg_index != UPB_NO_SUBMSG); subm = frame->m->submsgs[field->submsg_index]; UPB_ASSERT(subm); if (!submsg) { - submsg = upb_env_malloc(d->env, upb_msg_sizeof((upb_msglayout *)subm)); + submsg = upb_msg_new(subm, upb_msg_arena(frame->msg)); CHK(submsg); - submsg = upb_msg_init( - submsg, (upb_msglayout*)subm, upb_arena_alloc(upb_env_arena(d->env))); *(void**)submsg_slot = submsg; } @@ -2190,11 +784,11 @@ static bool upb_decode_submsg(upb_decstate *d, static bool upb_decode_varintfield(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { uint64_t val; void *field_mem; - field_mem = upb_decode_prepareslot(d, frame, field); + field_mem = upb_decode_prepareslot(frame, field); CHK(field_mem); CHK(upb_decode_varint(&d->ptr, frame->limit, &val)); @@ -2235,11 +829,11 @@ static bool upb_decode_varintfield(upb_decstate *d, upb_decframe *frame, static bool upb_decode_64bitfield(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { void *field_mem; uint64_t val; - field_mem = upb_decode_prepareslot(d, frame, field); + field_mem = upb_decode_prepareslot(frame, field); CHK(field_mem); CHK(upb_decode_64bit(&d->ptr, frame->limit, &val)); @@ -2259,11 +853,11 @@ static bool upb_decode_64bitfield(upb_decstate *d, upb_decframe *frame, static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { void *field_mem; uint32_t val; - field_mem = upb_decode_prepareslot(d, frame, field); + field_mem = upb_decode_prepareslot(frame, field); CHK(field_mem); CHK(upb_decode_32bit(&d->ptr, frame->limit, &val)); @@ -2281,7 +875,7 @@ static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame, return true; } -static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data, +static bool upb_decode_fixedpacked(upb_array *arr, upb_strview data, int elem_size) { int elements = data.size / elem_size; void *field_mem; @@ -2295,9 +889,9 @@ static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data, static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field, - upb_stringview val) { - upb_array *arr = upb_getorcreatearr(d, frame, field); + const upb_msglayout_field *field, + upb_strview val) { + upb_array *arr = upb_getorcreatearr(frame, field); #define VARINT_CASE(ctype, decode) { \ const char *ptr = val.data; \ @@ -2346,7 +940,7 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, case UPB_DESCRIPTOR_TYPE_SINT64: VARINT_CASE(int64_t, upb_zzdecode_64); case UPB_DESCRIPTOR_TYPE_MESSAGE: { - const upb_msglayout_msginit_v1 *subm; + const upb_msglayout *subm; char *submsg; void *field_mem; @@ -2354,14 +948,11 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, d->ptr -= val.size; /* Create elemente message. */ - UPB_ASSERT(field->submsg_index != UPB_NO_SUBMSG); subm = frame->m->submsgs[field->submsg_index]; UPB_ASSERT(subm); - submsg = upb_env_malloc(d->env, upb_msg_sizeof((upb_msglayout *)subm)); + submsg = upb_msg_new(subm, upb_msg_arena(frame->msg)); CHK(submsg); - submsg = upb_msg_init(submsg, (upb_msglayout*)subm, - upb_arena_alloc(upb_env_arena(d->env))); field_mem = upb_array_add(arr, 1); CHK(field_mem); @@ -2379,8 +970,8 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field) { - upb_stringview val; + const upb_msglayout_field *field) { + upb_strview val; CHK(upb_decode_string(&d->ptr, frame->limit, &val)); @@ -2390,7 +981,7 @@ static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame, switch ((upb_descriptortype_t)field->descriptortype) { case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_BYTES: { - void *field_mem = upb_decode_prepareslot(d, frame, field); + void *field_mem = upb_decode_prepareslot(frame, field); CHK(field_mem); memcpy(field_mem, &val, sizeof(val)); break; @@ -2409,8 +1000,8 @@ static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame, } } -static const upb_msglayout_fieldinit_v1 *upb_find_field( - const upb_msglayout_msginit_v1 *l, uint32_t field_number) { +static const upb_msglayout_field *upb_find_field(const upb_msglayout *l, + uint32_t field_number) { /* Lots of optimization opportunities here. */ int i; for (i = 0; i < l->field_count; i++) { @@ -2426,7 +1017,7 @@ static bool upb_decode_field(upb_decstate *d, upb_decframe *frame) { int field_number; int wire_type; const char *field_start = d->ptr; - const upb_msglayout_fieldinit_v1 *field; + const upb_msglayout_field *field; CHK(upb_decode_tag(&d->ptr, frame->limit, &field_number, &wire_type)); field = upb_find_field(frame->m, field_number); @@ -2453,7 +1044,9 @@ static bool upb_decode_field(upb_decstate *d, upb_decframe *frame) { } } else { CHK(field_number != 0); - return upb_skip_unknownfielddata(d, frame, field_number, wire_type); + CHK(upb_skip_unknownfielddata(d, frame, field_number, wire_type)); + CHK(upb_append_unknown(d, frame, field_start)); + return true; } } @@ -2478,7 +1071,7 @@ static bool upb_skip_unknowngroup(upb_decstate *d, int field_number, static bool upb_decode_message(upb_decstate *d, const char *limit, int group_number, char *msg, - const upb_msglayout_msginit_v1 *l) { + const upb_msglayout *l) { upb_decframe frame; frame.group_number = group_number; frame.limit = limit; @@ -2492,19 +1085,19 @@ static bool upb_decode_message(upb_decstate *d, const char *limit, return true; } -bool upb_decode(upb_stringview buf, void *msg, - const upb_msglayout_msginit_v1 *l, upb_env *env) { +bool upb_decode(const char *buf, size_t size, void *msg, + const upb_msglayout *l) { upb_decstate state; - state.ptr = buf.data; - state.env = env; + state.ptr = buf; - return upb_decode_message(&state, buf.data + buf.size, 0, msg, l); + return upb_decode_message(&state, buf + size, 0, msg, l); } #undef CHK #include +#include #include #include @@ -2513,8 +1106,8 @@ typedef struct { char str[1]; /* Null-terminated string data follows. */ } str_t; -static str_t *newstr(const char *data, size_t len) { - str_t *ret = upb_gmalloc(sizeof(*ret) + len); +static str_t *newstr(upb_alloc *alloc, const char *data, size_t len) { + str_t *ret = upb_malloc(alloc, sizeof(*ret) + len); if (!ret) return NULL; ret->len = len; memcpy(ret->str, data, len); @@ -2522,7 +1115,113 @@ static str_t *newstr(const char *data, size_t len) { return ret; } -static void freestr(str_t *s) { upb_gfree(s); } +struct upb_fielddef { + const upb_filedef *file; + const upb_msgdef *msgdef; + const char *full_name; + union { + int64_t sint; + uint64_t uint; + double dbl; + float flt; + bool boolean; + str_t *str; + } defaultval; + const upb_oneofdef *oneof; + union { + const upb_msgdef *msgdef; + const upb_enumdef *enumdef; + const google_protobuf_FieldDescriptorProto *unresolved; + } sub; + uint32_t number_; + uint32_t index_; + uint32_t selector_base; /* Used to index into a upb::Handlers table. */ + bool is_extension_; + bool lazy_; + bool packed_; + upb_descriptortype_t type_; + upb_label_t label_; +}; + +struct upb_msgdef { + const upb_filedef *file; + const char *full_name; + uint32_t selector_count; + uint32_t submsg_field_count; + + /* Tables for looking up fields by number and name. */ + upb_inttable itof; + upb_strtable ntof; + + const upb_fielddef *fields; + const upb_oneofdef *oneofs; + int field_count; + int oneof_count; + + /* Is this a map-entry message? */ + bool map_entry; + upb_wellknowntype_t well_known_type; + + /* TODO(haberman): proper extension ranges (there can be multiple). */ +}; + +struct upb_enumdef { + const upb_filedef *file; + const char *full_name; + upb_strtable ntoi; + upb_inttable iton; + int32_t defaultval; +}; + +struct upb_oneofdef { + const upb_msgdef *parent; + const char *full_name; + uint32_t index; + upb_strtable ntof; + upb_inttable itof; +}; + +struct upb_filedef { + const char *name; + const char *package; + const char *phpprefix; + const char *phpnamespace; + upb_syntax_t syntax; + + const upb_filedef **deps; + const upb_msgdef *msgs; + const upb_enumdef *enums; + const upb_fielddef *exts; + + int dep_count; + int msg_count; + int enum_count; + int ext_count; +}; + +struct upb_symtab { + upb_arena *arena; + upb_strtable syms; /* full_name -> packed def ptr */ + upb_strtable files; /* file_name -> upb_filedef* */ +}; + +/* Inside a symtab we store tagged pointers to specific def types. */ +typedef enum { + UPB_DEFTYPE_MSG = 0, + UPB_DEFTYPE_ENUM = 1, + UPB_DEFTYPE_FIELD = 2, + UPB_DEFTYPE_ONEOF = 3 +} upb_deftype_t; + +static const void *unpack_def(upb_value v, upb_deftype_t type) { + uintptr_t num = (uintptr_t)upb_value_getconstptr(v); + return (num & 3) == type ? (const void*)(num & ~3) : NULL; +} + +static upb_value pack_def(const void *ptr, upb_deftype_t type) { + uintptr_t num = (uintptr_t)ptr | type; + return upb_value_constptr((const void*)num); +} /* isalpha() etc. from are locale-dependent, which we don't want. */ static bool upb_isbetween(char c, char low, char high) { @@ -2537,7 +1236,9 @@ static bool upb_isalphanum(char c) { return upb_isletter(c) || upb_isbetween(c, '0', '9'); } -static bool upb_isident(const char *str, size_t len, bool full, upb_status *s) { +static bool upb_isident(upb_strview name, bool full, upb_status *s) { + const char *str = name.data; + size_t len = name.size; bool start = true; size_t i; for (i = 0; i < len; i++) { @@ -2567,187 +1268,20 @@ static bool upb_isident(const char *str, size_t len, bool full, upb_status *s) { return !start; } -static bool upb_isoneof(const upb_refcounted *def) { - return def->vtbl == &upb_oneofdef_vtbl; -} - -static bool upb_isfield(const upb_refcounted *def) { - return def->vtbl == &upb_fielddef_vtbl; -} - -static const upb_oneofdef *upb_trygetoneof(const upb_refcounted *def) { - return upb_isoneof(def) ? (const upb_oneofdef*)def : NULL; -} - -static const upb_fielddef *upb_trygetfield(const upb_refcounted *def) { - return upb_isfield(def) ? (const upb_fielddef*)def : NULL; -} - - -/* upb_def ********************************************************************/ - -upb_deftype_t upb_def_type(const upb_def *d) { return d->type; } - -const char *upb_def_fullname(const upb_def *d) { return d->fullname; } - -const char *upb_def_name(const upb_def *d) { +static const char *shortdefname(const char *fullname) { const char *p; - if (d->fullname == NULL) { + if (fullname == NULL) { return NULL; - } else if ((p = strrchr(d->fullname, '.')) == NULL) { + } else if ((p = strrchr(fullname, '.')) == NULL) { /* No '.' in the name, return the full string. */ - return d->fullname; + return fullname; } else { /* Return one past the last '.'. */ return p + 1; } } -bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s) { - UPB_ASSERT(!upb_def_isfrozen(def)); - if (!upb_isident(fullname, strlen(fullname), true, s)) { - return false; - } - - fullname = upb_gstrdup(fullname); - if (!fullname) { - upb_upberr_setoom(s); - return false; - } - - upb_gfree((void*)def->fullname); - def->fullname = fullname; - return true; -} - -const upb_filedef *upb_def_file(const upb_def *d) { return d->file; } - -static bool upb_def_init(upb_def *def, upb_deftype_t type, - const struct upb_refcounted_vtbl *vtbl, - const void *owner) { - if (!upb_refcounted_init(upb_def_upcast_mutable(def), vtbl, owner)) return false; - def->type = type; - def->fullname = NULL; - def->came_from_user = false; - def->file = NULL; - return true; -} - -static void upb_def_uninit(upb_def *def) { - upb_gfree((void*)def->fullname); -} - -static const char *msgdef_name(const upb_msgdef *m) { - const char *name = upb_def_fullname(upb_msgdef_upcast(m)); - return name ? name : "(anonymous)"; -} - -static bool upb_validate_field(upb_fielddef *f, upb_status *s) { - if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) { - upb_status_seterrmsg(s, "fielddef must have name and number set"); - return false; - } - - if (!f->type_is_set_) { - upb_status_seterrmsg(s, "fielddef type was not initialized"); - return false; - } - - if (upb_fielddef_lazy(f) && - upb_fielddef_descriptortype(f) != UPB_DESCRIPTOR_TYPE_MESSAGE) { - upb_status_seterrmsg(s, - "only length-delimited submessage fields may be lazy"); - return false; - } - - if (upb_fielddef_hassubdef(f)) { - const upb_def *subdef; - - if (f->subdef_is_symbolic) { - upb_status_seterrf(s, "field '%s.%s' has not been resolved", - msgdef_name(f->msg.def), upb_fielddef_name(f)); - return false; - } - - subdef = upb_fielddef_subdef(f); - if (subdef == NULL) { - upb_status_seterrf(s, "field %s.%s is missing required subdef", - msgdef_name(f->msg.def), upb_fielddef_name(f)); - return false; - } - - if (!upb_def_isfrozen(subdef) && !subdef->came_from_user) { - upb_status_seterrf(s, - "subdef of field %s.%s is not frozen or being frozen", - msgdef_name(f->msg.def), upb_fielddef_name(f)); - return false; - } - } - - if (upb_fielddef_type(f) == UPB_TYPE_ENUM) { - bool has_default_name = upb_fielddef_enumhasdefaultstr(f); - bool has_default_number = upb_fielddef_enumhasdefaultint32(f); - - /* Previously verified by upb_validate_enumdef(). */ - UPB_ASSERT(upb_enumdef_numvals(upb_fielddef_enumsubdef(f)) > 0); - - /* We've already validated that we have an associated enumdef and that it - * has at least one member, so at least one of these should be true. - * Because if the user didn't set anything, we'll pick up the enum's - * default, but if the user *did* set something we should at least pick up - * the one they set (int32 or string). */ - UPB_ASSERT(has_default_name || has_default_number); - - if (!has_default_name) { - upb_status_seterrf(s, - "enum default for field %s.%s (%d) is not in the enum", - msgdef_name(f->msg.def), upb_fielddef_name(f), - upb_fielddef_defaultint32(f)); - return false; - } - - if (!has_default_number) { - upb_status_seterrf(s, - "enum default for field %s.%s (%s) is not in the enum", - msgdef_name(f->msg.def), upb_fielddef_name(f), - upb_fielddef_defaultstr(f, NULL)); - return false; - } - - /* Lift the effective numeric default into the field's default slot, in case - * we were only getting it "by reference" from the enumdef. */ - upb_fielddef_setdefaultint32(f, upb_fielddef_defaultint32(f)); - } - - /* Ensure that MapEntry submessages only appear as repeated fields, not - * optional/required (singular) fields. */ - if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE && - upb_fielddef_msgsubdef(f) != NULL) { - const upb_msgdef *subdef = upb_fielddef_msgsubdef(f); - if (upb_msgdef_mapentry(subdef) && !upb_fielddef_isseq(f)) { - upb_status_seterrf(s, - "Field %s refers to mapentry message but is not " - "a repeated field", - upb_fielddef_name(f) ? upb_fielddef_name(f) : - "(unnamed)"); - return false; - } - } - - return true; -} - -static bool upb_validate_enumdef(const upb_enumdef *e, upb_status *s) { - if (upb_enumdef_numvals(e) == 0) { - upb_status_seterrf(s, "enum %s has no members (must have at least one)", - upb_enumdef_fullname(e)); - return false; - } - - return true; -} - /* All submessage fields are lower than all other fields. * Secondly, fields are increasing in order. */ uint32_t field_rank(const upb_fielddef *f) { @@ -2783,7 +1317,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { fields = upb_gmalloc(n * sizeof(*fields)); if (!fields) { - upb_upberr_setoom(s); + upb_status_setoom(s); return false; } @@ -2792,11 +1326,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { !upb_msg_field_done(&j); upb_msg_field_next(&j), i++) { upb_fielddef *f = upb_msg_iter_field(&j); - UPB_ASSERT(f->msg.def == m); - if (!upb_validate_field(f, s)) { - upb_gfree(fields); - return false; - } + UPB_ASSERT(f->msgdef == m); if (upb_fielddef_issubmsg(f)) { m->submsg_field_count++; } @@ -2818,7 +1348,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { { /* Verify that all selectors for the message are distinct. */ #define TRY(type) \ - if (upb_handlers_getselector(f, type, &sel)) upb_inttable_insert(&t, sel, v); + if (upb_handlers_getselector(f, type, &sel)) { upb_inttable_insert(&t, sel, v); } upb_inttable t; upb_value v; @@ -2858,7 +1388,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { for(upb_msg_oneof_begin(&k, m), i = 0; !upb_msg_oneof_done(&k); upb_msg_oneof_next(&k), i++) { - upb_oneofdef *o = upb_msg_iter_oneof(&k); + upb_oneofdef *o = (upb_oneofdef*)upb_msg_iter_oneof(&k); o->index = i; } @@ -2866,171 +1396,62 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { return true; } -bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s) { - size_t i; - - /* First perform validation, in two passes so we can check that we have a - * transitive closure without needing to search. */ - for (i = 0; i < n; i++) { - upb_def *def = defs[i]; - if (upb_def_isfrozen(def)) { - /* Could relax this requirement if it's annoying. */ - upb_status_seterrmsg(s, "def is already frozen"); - goto err; - } else if (def->type == UPB_DEF_FIELD) { - upb_status_seterrmsg(s, "standalone fielddefs can not be frozen"); - goto err; - } else { - /* Set now to detect transitive closure in the second pass. */ - def->came_from_user = true; - - if (def->type == UPB_DEF_ENUM && - !upb_validate_enumdef(upb_dyncast_enumdef(def), s)) { - goto err; - } - } - } - - /* Second pass of validation. Also assign selector bases and indexes, and - * compact tables. */ - for (i = 0; i < n; i++) { - upb_def *def = defs[i]; - upb_msgdef *m = upb_dyncast_msgdef_mutable(def); - upb_enumdef *e = upb_dyncast_enumdef_mutable(def); - if (m) { - upb_inttable_compact(&m->itof); - if (!assign_msg_indices(m, s)) { - goto err; - } - } else if (e) { - upb_inttable_compact(&e->iton); - } - } - - return true; - -err: - for (i = 0; i < n; i++) { - upb_def *def = defs[i]; - def->came_from_user = false; +static void assign_msg_wellknowntype(upb_msgdef *m) { + const char *name = upb_msgdef_fullname(m); + if (name == NULL) { + m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED; + return; } - UPB_ASSERT(!(s && upb_ok(s))); - return false; -} - -bool upb_def_freeze(upb_def *const* defs, size_t n, upb_status *s) { - /* Def graph contains FieldDefs between each MessageDef, so double the - * limit. */ - const size_t maxdepth = UPB_MAX_MESSAGE_DEPTH * 2; - - if (!_upb_def_validate(defs, n, s)) { - return false; + if (!strcmp(name, "google.protobuf.Any")) { + m->well_known_type = UPB_WELLKNOWN_ANY; + } else if (!strcmp(name, "google.protobuf.FieldMask")) { + m->well_known_type = UPB_WELLKNOWN_FIELDMASK; + } else if (!strcmp(name, "google.protobuf.Duration")) { + m->well_known_type = UPB_WELLKNOWN_DURATION; + } else if (!strcmp(name, "google.protobuf.Timestamp")) { + m->well_known_type = UPB_WELLKNOWN_TIMESTAMP; + } else if (!strcmp(name, "google.protobuf.DoubleValue")) { + m->well_known_type = UPB_WELLKNOWN_DOUBLEVALUE; + } else if (!strcmp(name, "google.protobuf.FloatValue")) { + m->well_known_type = UPB_WELLKNOWN_FLOATVALUE; + } else if (!strcmp(name, "google.protobuf.Int64Value")) { + m->well_known_type = UPB_WELLKNOWN_INT64VALUE; + } else if (!strcmp(name, "google.protobuf.UInt64Value")) { + m->well_known_type = UPB_WELLKNOWN_UINT64VALUE; + } else if (!strcmp(name, "google.protobuf.Int32Value")) { + m->well_known_type = UPB_WELLKNOWN_INT32VALUE; + } else if (!strcmp(name, "google.protobuf.UInt32Value")) { + m->well_known_type = UPB_WELLKNOWN_UINT32VALUE; + } else if (!strcmp(name, "google.protobuf.BoolValue")) { + m->well_known_type = UPB_WELLKNOWN_BOOLVALUE; + } else if (!strcmp(name, "google.protobuf.StringValue")) { + m->well_known_type = UPB_WELLKNOWN_STRINGVALUE; + } else if (!strcmp(name, "google.protobuf.BytesValue")) { + m->well_known_type = UPB_WELLKNOWN_BYTESVALUE; + } else if (!strcmp(name, "google.protobuf.Value")) { + m->well_known_type = UPB_WELLKNOWN_VALUE; + } else if (!strcmp(name, "google.protobuf.ListValue")) { + m->well_known_type = UPB_WELLKNOWN_LISTVALUE; + } else if (!strcmp(name, "google.protobuf.Struct")) { + m->well_known_type = UPB_WELLKNOWN_STRUCT; + } else { + m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED; } - - - /* Validation all passed; freeze the objects. */ - return upb_refcounted_freeze((upb_refcounted *const*)defs, n, s, maxdepth); } /* upb_enumdef ****************************************************************/ -static void visitenum(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_enumdef *e = (const upb_enumdef*)r; - const upb_def *def = upb_enumdef_upcast(e); - if (upb_def_file(def)) { - visit(r, upb_filedef_upcast(upb_def_file(def)), closure); - } -} - -static void freeenum(upb_refcounted *r) { - upb_enumdef *e = (upb_enumdef*)r; - upb_inttable_iter i; - upb_inttable_begin(&i, &e->iton); - for( ; !upb_inttable_done(&i); upb_inttable_next(&i)) { - /* To clean up the upb_gstrdup() from upb_enumdef_addval(). */ - upb_gfree(upb_value_getcstr(upb_inttable_iter_value(&i))); - } - upb_strtable_uninit(&e->ntoi); - upb_inttable_uninit(&e->iton); - upb_def_uninit(upb_enumdef_upcast_mutable(e)); - upb_gfree(e); -} - -const struct upb_refcounted_vtbl upb_enumdef_vtbl = {&visitenum, &freeenum}; - -upb_enumdef *upb_enumdef_new(const void *owner) { - upb_enumdef *e = upb_gmalloc(sizeof(*e)); - if (!e) return NULL; - - if (!upb_def_init(upb_enumdef_upcast_mutable(e), UPB_DEF_ENUM, - &upb_enumdef_vtbl, owner)) { - goto err2; - } - - if (!upb_strtable_init(&e->ntoi, UPB_CTYPE_INT32)) goto err2; - if (!upb_inttable_init(&e->iton, UPB_CTYPE_CSTR)) goto err1; - return e; - -err1: - upb_strtable_uninit(&e->ntoi); -err2: - upb_gfree(e); - return NULL; -} - -bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status) { - upb_def *d = upb_enumdef_upcast_mutable(e); - return upb_def_freeze(&d, 1, status); -} - const char *upb_enumdef_fullname(const upb_enumdef *e) { - return upb_def_fullname(upb_enumdef_upcast(e)); + return e->full_name; } const char *upb_enumdef_name(const upb_enumdef *e) { - return upb_def_name(upb_enumdef_upcast(e)); + return shortdefname(e->full_name); } -bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname, - upb_status *s) { - return upb_def_setfullname(upb_enumdef_upcast_mutable(e), fullname, s); -} - -bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, - upb_status *status) { - char *name2; - - if (!upb_isident(name, strlen(name), false, status)) { - return false; - } - - if (upb_enumdef_ntoiz(e, name, NULL)) { - upb_status_seterrf(status, "name '%s' is already defined", name); - return false; - } - - if (!upb_strtable_insert(&e->ntoi, name, upb_value_int32(num))) { - upb_status_seterrmsg(status, "out of memory"); - return false; - } - - if (!upb_inttable_lookup(&e->iton, num, NULL)) { - name2 = upb_gstrdup(name); - if (!name2 || !upb_inttable_insert(&e->iton, num, upb_value_cstr(name2))) { - upb_status_seterrmsg(status, "out of memory"); - upb_strtable_remove(&e->ntoi, name, NULL); - return false; - } - } - - if (upb_enumdef_numvals(e) == 1) { - bool ok = upb_enumdef_setdefault(e, num, NULL); - UPB_ASSERT(ok); - } - - return true; +const upb_filedef *upb_enumdef_file(const upb_enumdef *e) { + return e->file; } int32_t upb_enumdef_default(const upb_enumdef *e) { @@ -3038,16 +1459,6 @@ int32_t upb_enumdef_default(const upb_enumdef *e) { return e->defaultval; } -bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s) { - UPB_ASSERT(!upb_enumdef_isfrozen(e)); - if (!upb_enumdef_iton(e, val)) { - upb_status_seterrf(s, "number '%d' is not in the enum.", val); - return false; - } - e->defaultval = val; - return true; -} - int upb_enumdef_numvals(const upb_enumdef *e) { return upb_strtable_count(&e->ntoi); } @@ -3087,139 +1498,46 @@ int32_t upb_enum_iter_number(upb_enum_iter *iter) { /* upb_fielddef ***************************************************************/ -static void upb_fielddef_init_default(upb_fielddef *f); - -static void upb_fielddef_uninit_default(upb_fielddef *f) { - if (f->type_is_set_ && f->default_is_string && f->defaultval.bytes) - freestr(f->defaultval.bytes); -} - -const char *upb_fielddef_fullname(const upb_fielddef *e) { - return upb_def_fullname(upb_fielddef_upcast(e)); -} - -static void visitfield(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_fielddef *f = (const upb_fielddef*)r; - const upb_def *def = upb_fielddef_upcast(f); - if (upb_fielddef_containingtype(f)) { - visit(r, upb_msgdef_upcast2(upb_fielddef_containingtype(f)), closure); - } - if (upb_fielddef_containingoneof(f)) { - visit(r, upb_oneofdef_upcast(upb_fielddef_containingoneof(f)), closure); - } - if (upb_fielddef_subdef(f)) { - visit(r, upb_def_upcast(upb_fielddef_subdef(f)), closure); - } - if (upb_def_file(def)) { - visit(r, upb_filedef_upcast(upb_def_file(def)), closure); - } -} - -static void freefield(upb_refcounted *r) { - upb_fielddef *f = (upb_fielddef*)r; - upb_fielddef_uninit_default(f); - if (f->subdef_is_symbolic) - upb_gfree(f->sub.name); - upb_def_uninit(upb_fielddef_upcast_mutable(f)); - upb_gfree(f); -} - -static const char *enumdefaultstr(const upb_fielddef *f) { - const upb_enumdef *e; - UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM); - e = upb_fielddef_enumsubdef(f); - if (f->default_is_string && f->defaultval.bytes) { - /* Default was explicitly set as a string. */ - str_t *s = f->defaultval.bytes; - return s->str; - } else if (e) { - if (!f->default_is_string) { - /* Default was explicitly set as an integer; look it up in enumdef. */ - const char *name = upb_enumdef_iton(e, f->defaultval.sint); - if (name) { - return name; - } - } else { - /* Default is completely unset; pull enumdef default. */ - if (upb_enumdef_numvals(e) > 0) { - const char *name = upb_enumdef_iton(e, upb_enumdef_default(e)); - UPB_ASSERT(name); - return name; - } - } - } - return NULL; -} - -static bool enumdefaultint32(const upb_fielddef *f, int32_t *val) { - const upb_enumdef *e; - UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM); - e = upb_fielddef_enumsubdef(f); - if (!f->default_is_string) { - /* Default was explicitly set as an integer. */ - *val = f->defaultval.sint; - return true; - } else if (e) { - if (f->defaultval.bytes) { - /* Default was explicitly set as a str; try to lookup corresponding int. */ - str_t *s = f->defaultval.bytes; - if (upb_enumdef_ntoiz(e, s->str, val)) { - return true; - } - } else { - /* Default is unset; try to pull in enumdef default. */ - if (upb_enumdef_numvals(e) > 0) { - *val = upb_enumdef_default(e); - return true; - } - } - } - return false; +const char *upb_fielddef_fullname(const upb_fielddef *f) { + return f->full_name; } -const struct upb_refcounted_vtbl upb_fielddef_vtbl = {visitfield, freefield}; - -upb_fielddef *upb_fielddef_new(const void *o) { - upb_fielddef *f = upb_gmalloc(sizeof(*f)); - if (!f) return NULL; - if (!upb_def_init(upb_fielddef_upcast_mutable(f), UPB_DEF_FIELD, - &upb_fielddef_vtbl, o)) { - upb_gfree(f); - return NULL; +upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) { + switch (f->type_) { + case UPB_DESCRIPTOR_TYPE_DOUBLE: + return UPB_TYPE_DOUBLE; + case UPB_DESCRIPTOR_TYPE_FLOAT: + return UPB_TYPE_FLOAT; + case UPB_DESCRIPTOR_TYPE_INT64: + case UPB_DESCRIPTOR_TYPE_SINT64: + case UPB_DESCRIPTOR_TYPE_SFIXED64: + return UPB_TYPE_INT64; + case UPB_DESCRIPTOR_TYPE_INT32: + case UPB_DESCRIPTOR_TYPE_SFIXED32: + case UPB_DESCRIPTOR_TYPE_SINT32: + return UPB_TYPE_INT32; + case UPB_DESCRIPTOR_TYPE_UINT64: + case UPB_DESCRIPTOR_TYPE_FIXED64: + return UPB_TYPE_UINT64; + case UPB_DESCRIPTOR_TYPE_UINT32: + case UPB_DESCRIPTOR_TYPE_FIXED32: + return UPB_TYPE_UINT32; + case UPB_DESCRIPTOR_TYPE_ENUM: + return UPB_TYPE_ENUM; + case UPB_DESCRIPTOR_TYPE_BOOL: + return UPB_TYPE_BOOL; + case UPB_DESCRIPTOR_TYPE_STRING: + return UPB_TYPE_STRING; + case UPB_DESCRIPTOR_TYPE_BYTES: + return UPB_TYPE_BYTES; + case UPB_DESCRIPTOR_TYPE_GROUP: + case UPB_DESCRIPTOR_TYPE_MESSAGE: + return UPB_TYPE_MESSAGE; } - f->msg.def = NULL; - f->sub.def = NULL; - f->oneof = NULL; - f->subdef_is_symbolic = false; - f->msg_is_symbolic = false; - f->label_ = UPB_LABEL_OPTIONAL; - f->type_ = UPB_TYPE_INT32; - f->number_ = 0; - f->type_is_set_ = false; - f->tagdelim = false; - f->is_extension_ = false; - f->lazy_ = false; - f->packed_ = true; - - /* For the moment we default this to UPB_INTFMT_VARIABLE, since it will work - * with all integer types and is in some since more "default" since the most - * normal-looking proto2 types int32/int64/uint32/uint64 use variable. - * - * Other options to consider: - * - there is no default; users must set this manually (like type). - * - default signed integers to UPB_INTFMT_ZIGZAG, since it's more likely to - * be an optimal default for signed integers. */ - f->intfmt = UPB_INTFMT_VARIABLE; - return f; -} - -bool upb_fielddef_typeisset(const upb_fielddef *f) { - return f->type_is_set_; + UPB_UNREACHABLE(); } -upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) { - UPB_ASSERT(f->type_is_set_); +upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) { return f->type_; } @@ -3231,14 +1549,6 @@ upb_label_t upb_fielddef_label(const upb_fielddef *f) { return f->label_; } -upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f) { - return f->intfmt; -} - -bool upb_fielddef_istagdelim(const upb_fielddef *f) { - return f->tagdelim; -} - uint32_t upb_fielddef_number(const upb_fielddef *f) { return f->number_; } @@ -3256,7 +1566,11 @@ bool upb_fielddef_packed(const upb_fielddef *f) { } const char *upb_fielddef_name(const upb_fielddef *f) { - return upb_def_fullname(upb_fielddef_upcast(f)); + return shortdefname(f->full_name); +} + +uint32_t upb_fielddef_selectorbase(const upb_fielddef *f) { + return f->selector_base; } size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len) { @@ -3299,77 +1613,26 @@ size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len) { } const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) { - return f->msg_is_symbolic ? NULL : f->msg.def; + return f->msgdef; } const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) { return f->oneof; } -upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f) { - return (upb_msgdef*)upb_fielddef_containingtype(f); +static void chkdefaulttype(const upb_fielddef *f, int ctype) { + UPB_UNUSED(f); + UPB_UNUSED(ctype); } -const char *upb_fielddef_containingtypename(upb_fielddef *f) { - return f->msg_is_symbolic ? f->msg.name : NULL; +int64_t upb_fielddef_defaultint64(const upb_fielddef *f) { + chkdefaulttype(f, UPB_TYPE_INT64); + return f->defaultval.sint; } -static void release_containingtype(upb_fielddef *f) { - if (f->msg_is_symbolic) upb_gfree(f->msg.name); -} - -bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name, - upb_status *s) { - char *name_copy; - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - if (upb_fielddef_containingtype(f)) { - upb_status_seterrmsg(s, "field has already been added to a message."); - return false; - } - /* TODO: validate name (upb_isident() doesn't quite work atm because this name - * may have a leading "."). */ - - name_copy = upb_gstrdup(name); - if (!name_copy) { - upb_upberr_setoom(s); - return false; - } - - release_containingtype(f); - f->msg.name = name_copy; - f->msg_is_symbolic = true; - return true; -} - -bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s) { - if (upb_fielddef_containingtype(f) || upb_fielddef_containingoneof(f)) { - upb_status_seterrmsg(s, "Already added to message or oneof"); - return false; - } - return upb_def_setfullname(upb_fielddef_upcast_mutable(f), name, s); -} - -static void chkdefaulttype(const upb_fielddef *f, upb_fieldtype_t type) { - UPB_UNUSED(f); - UPB_UNUSED(type); - UPB_ASSERT(f->type_is_set_ && upb_fielddef_type(f) == type); -} - -int64_t upb_fielddef_defaultint64(const upb_fielddef *f) { - chkdefaulttype(f, UPB_TYPE_INT64); - return f->defaultval.sint; -} - -int32_t upb_fielddef_defaultint32(const upb_fielddef *f) { - if (f->type_is_set_ && upb_fielddef_type(f) == UPB_TYPE_ENUM) { - int32_t val; - bool ok = enumdefaultint32(f, &val); - UPB_ASSERT(ok); - return val; - } else { - chkdefaulttype(f, UPB_TYPE_INT32); - return f->defaultval.sint; - } +int32_t upb_fielddef_defaultint32(const upb_fielddef *f) { + chkdefaulttype(f, UPB_TYPE_INT32); + return f->defaultval.sint; } uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) { @@ -3384,7 +1647,7 @@ uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) { bool upb_fielddef_defaultbool(const upb_fielddef *f) { chkdefaulttype(f, UPB_TYPE_BOOL); - return f->defaultval.uint; + return f->defaultval.boolean; } float upb_fielddef_defaultfloat(const upb_fielddef *f) { @@ -3398,1013 +1661,1038 @@ double upb_fielddef_defaultdouble(const upb_fielddef *f) { } const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) { - UPB_ASSERT(f->type_is_set_); + str_t *str = f->defaultval.str; UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING || upb_fielddef_type(f) == UPB_TYPE_BYTES || upb_fielddef_type(f) == UPB_TYPE_ENUM); - - if (upb_fielddef_type(f) == UPB_TYPE_ENUM) { - const char *ret = enumdefaultstr(f); - UPB_ASSERT(ret); - /* Enum defaults can't have embedded NULLs. */ - if (len) *len = strlen(ret); - return ret; - } - - if (f->default_is_string) { - str_t *str = f->defaultval.bytes; + if (str) { if (len) *len = str->len; return str->str; + } else { + if (len) *len = 0; + return NULL; } +} - return NULL; +const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) { + UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_MESSAGE); + return f->sub.msgdef; } -static void upb_fielddef_init_default(upb_fielddef *f) { - f->default_is_string = false; - switch (upb_fielddef_type(f)) { - case UPB_TYPE_DOUBLE: f->defaultval.dbl = 0; break; - case UPB_TYPE_FLOAT: f->defaultval.flt = 0; break; - case UPB_TYPE_INT32: - case UPB_TYPE_INT64: f->defaultval.sint = 0; break; - case UPB_TYPE_UINT64: - case UPB_TYPE_UINT32: - case UPB_TYPE_BOOL: f->defaultval.uint = 0; break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - f->defaultval.bytes = newstr("", 0); - f->default_is_string = true; - break; - case UPB_TYPE_MESSAGE: break; - case UPB_TYPE_ENUM: - /* This is our special sentinel that indicates "not set" for an enum. */ - f->default_is_string = true; - f->defaultval.bytes = NULL; - break; - } +const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) { + UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_ENUM); + return f->sub.enumdef; } -const upb_def *upb_fielddef_subdef(const upb_fielddef *f) { - return f->subdef_is_symbolic ? NULL : f->sub.def; +bool upb_fielddef_issubmsg(const upb_fielddef *f) { + return upb_fielddef_type(f) == UPB_TYPE_MESSAGE; } -const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) { - const upb_def *def = upb_fielddef_subdef(f); - return def ? upb_dyncast_msgdef(def) : NULL; +bool upb_fielddef_isstring(const upb_fielddef *f) { + return upb_fielddef_type(f) == UPB_TYPE_STRING || + upb_fielddef_type(f) == UPB_TYPE_BYTES; } -const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) { - const upb_def *def = upb_fielddef_subdef(f); - return def ? upb_dyncast_enumdef(def) : NULL; +bool upb_fielddef_isseq(const upb_fielddef *f) { + return upb_fielddef_label(f) == UPB_LABEL_REPEATED; } -upb_def *upb_fielddef_subdef_mutable(upb_fielddef *f) { - return (upb_def*)upb_fielddef_subdef(f); +bool upb_fielddef_isprimitive(const upb_fielddef *f) { + return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f); } -const char *upb_fielddef_subdefname(const upb_fielddef *f) { - if (f->subdef_is_symbolic) { - return f->sub.name; - } else if (f->sub.def) { - return upb_def_fullname(f->sub.def); - } else { - return NULL; - } +bool upb_fielddef_ismap(const upb_fielddef *f) { + return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) && + upb_msgdef_mapentry(upb_fielddef_msgsubdef(f)); } -bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s) { - if (upb_fielddef_containingtype(f)) { - upb_status_seterrmsg( - s, "cannot change field number after adding to a message"); - return false; - } - if (number == 0 || number > UPB_MAX_FIELDNUMBER) { - upb_status_seterrf(s, "invalid field number (%u)", number); - return false; - } - f->number_ = number; - return true; +bool upb_fielddef_hassubdef(const upb_fielddef *f) { + return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM; } -void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - UPB_ASSERT(upb_fielddef_checktype(type)); - upb_fielddef_uninit_default(f); - f->type_ = type; - f->type_is_set_ = true; - upb_fielddef_init_default(f); +bool upb_fielddef_haspresence(const upb_fielddef *f) { + if (upb_fielddef_isseq(f)) return false; + if (upb_fielddef_issubmsg(f)) return true; + return f->file->syntax == UPB_SYNTAX_PROTO2; } -void upb_fielddef_setdescriptortype(upb_fielddef *f, int type) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - switch (type) { - case UPB_DESCRIPTOR_TYPE_DOUBLE: - upb_fielddef_settype(f, UPB_TYPE_DOUBLE); - break; - case UPB_DESCRIPTOR_TYPE_FLOAT: - upb_fielddef_settype(f, UPB_TYPE_FLOAT); - break; - case UPB_DESCRIPTOR_TYPE_INT64: - case UPB_DESCRIPTOR_TYPE_SFIXED64: - case UPB_DESCRIPTOR_TYPE_SINT64: - upb_fielddef_settype(f, UPB_TYPE_INT64); - break; - case UPB_DESCRIPTOR_TYPE_UINT64: - case UPB_DESCRIPTOR_TYPE_FIXED64: - upb_fielddef_settype(f, UPB_TYPE_UINT64); - break; - case UPB_DESCRIPTOR_TYPE_INT32: - case UPB_DESCRIPTOR_TYPE_SFIXED32: - case UPB_DESCRIPTOR_TYPE_SINT32: - upb_fielddef_settype(f, UPB_TYPE_INT32); - break; - case UPB_DESCRIPTOR_TYPE_UINT32: - case UPB_DESCRIPTOR_TYPE_FIXED32: - upb_fielddef_settype(f, UPB_TYPE_UINT32); - break; - case UPB_DESCRIPTOR_TYPE_BOOL: - upb_fielddef_settype(f, UPB_TYPE_BOOL); - break; - case UPB_DESCRIPTOR_TYPE_STRING: - upb_fielddef_settype(f, UPB_TYPE_STRING); - break; - case UPB_DESCRIPTOR_TYPE_BYTES: - upb_fielddef_settype(f, UPB_TYPE_BYTES); - break; - case UPB_DESCRIPTOR_TYPE_GROUP: - case UPB_DESCRIPTOR_TYPE_MESSAGE: - upb_fielddef_settype(f, UPB_TYPE_MESSAGE); - break; - case UPB_DESCRIPTOR_TYPE_ENUM: - upb_fielddef_settype(f, UPB_TYPE_ENUM); - break; - default: UPB_ASSERT(false); - } +static bool between(int32_t x, int32_t low, int32_t high) { + return x >= low && x <= high; +} - if (type == UPB_DESCRIPTOR_TYPE_FIXED64 || - type == UPB_DESCRIPTOR_TYPE_FIXED32 || - type == UPB_DESCRIPTOR_TYPE_SFIXED64 || - type == UPB_DESCRIPTOR_TYPE_SFIXED32) { - upb_fielddef_setintfmt(f, UPB_INTFMT_FIXED); - } else if (type == UPB_DESCRIPTOR_TYPE_SINT64 || - type == UPB_DESCRIPTOR_TYPE_SINT32) { - upb_fielddef_setintfmt(f, UPB_INTFMT_ZIGZAG); - } else { - upb_fielddef_setintfmt(f, UPB_INTFMT_VARIABLE); - } +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); } - upb_fielddef_settagdelim(f, type == UPB_DESCRIPTOR_TYPE_GROUP); +bool upb_fielddef_checkdescriptortype(int32_t type) { + return between(type, 1, 18); } -upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) { - switch (upb_fielddef_type(f)) { - case UPB_TYPE_FLOAT: return UPB_DESCRIPTOR_TYPE_FLOAT; - case UPB_TYPE_DOUBLE: return UPB_DESCRIPTOR_TYPE_DOUBLE; - case UPB_TYPE_BOOL: return UPB_DESCRIPTOR_TYPE_BOOL; - case UPB_TYPE_STRING: return UPB_DESCRIPTOR_TYPE_STRING; - case UPB_TYPE_BYTES: return UPB_DESCRIPTOR_TYPE_BYTES; - case UPB_TYPE_ENUM: return UPB_DESCRIPTOR_TYPE_ENUM; - case UPB_TYPE_INT32: - switch (upb_fielddef_intfmt(f)) { - case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_INT32; - case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_SFIXED32; - case UPB_INTFMT_ZIGZAG: return UPB_DESCRIPTOR_TYPE_SINT32; - } - case UPB_TYPE_INT64: - switch (upb_fielddef_intfmt(f)) { - case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_INT64; - case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_SFIXED64; - case UPB_INTFMT_ZIGZAG: return UPB_DESCRIPTOR_TYPE_SINT64; - } - case UPB_TYPE_UINT32: - switch (upb_fielddef_intfmt(f)) { - case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_UINT32; - case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_FIXED32; - case UPB_INTFMT_ZIGZAG: return -1; - } - case UPB_TYPE_UINT64: - switch (upb_fielddef_intfmt(f)) { - case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_UINT64; - case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_FIXED64; - case UPB_INTFMT_ZIGZAG: return -1; - } - case UPB_TYPE_MESSAGE: - return upb_fielddef_istagdelim(f) ? - UPB_DESCRIPTOR_TYPE_GROUP : UPB_DESCRIPTOR_TYPE_MESSAGE; - } - return 0; +/* upb_msgdef *****************************************************************/ + +const char *upb_msgdef_fullname(const upb_msgdef *m) { + return m->full_name; } -void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - f->is_extension_ = is_extension; +const upb_filedef *upb_msgdef_file(const upb_msgdef *m) { + return m->file; } -void upb_fielddef_setlazy(upb_fielddef *f, bool lazy) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - f->lazy_ = lazy; +const char *upb_msgdef_name(const upb_msgdef *m) { + return shortdefname(m->full_name); } -void upb_fielddef_setpacked(upb_fielddef *f, bool packed) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - f->packed_ = packed; +upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) { + return m->file->syntax; } -void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - UPB_ASSERT(upb_fielddef_checklabel(label)); - f->label_ = label; +size_t upb_msgdef_selectorcount(const upb_msgdef *m) { + return m->selector_count; } -void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - UPB_ASSERT(upb_fielddef_checkintfmt(fmt)); - f->intfmt = fmt; +uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m) { + return m->submsg_field_count; } -void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - f->tagdelim = tag_delim; - f->tagdelim = tag_delim; +const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) { + upb_value val; + return upb_inttable_lookup32(&m->itof, i, &val) ? + upb_value_getconstptr(val) : NULL; } -static bool checksetdefault(upb_fielddef *f, upb_fieldtype_t type) { - if (!f->type_is_set_ || upb_fielddef_isfrozen(f) || - upb_fielddef_type(f) != type) { - UPB_ASSERT(false); - return false; - } - if (f->default_is_string) { - str_t *s = f->defaultval.bytes; - UPB_ASSERT(s || type == UPB_TYPE_ENUM); - if (s) freestr(s); +const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, + size_t len) { + upb_value val; + + if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) { + return NULL; } - f->default_is_string = false; - return true; + + return unpack_def(val, UPB_DEFTYPE_FIELD); } -void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t value) { - if (checksetdefault(f, UPB_TYPE_INT64)) - f->defaultval.sint = value; +const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, + size_t len) { + upb_value val; + + if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) { + return NULL; + } + + return unpack_def(val, UPB_DEFTYPE_ONEOF); } -void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t value) { - if ((upb_fielddef_type(f) == UPB_TYPE_ENUM && - checksetdefault(f, UPB_TYPE_ENUM)) || - checksetdefault(f, UPB_TYPE_INT32)) { - f->defaultval.sint = value; +bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len, + const upb_fielddef **f, const upb_oneofdef **o) { + upb_value val; + + if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) { + return false; } + + *o = unpack_def(val, UPB_DEFTYPE_ONEOF); + *f = unpack_def(val, UPB_DEFTYPE_FIELD); + UPB_ASSERT((*o != NULL) ^ (*f != NULL)); /* Exactly one of the two should be set. */ + return true; } -void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t value) { - if (checksetdefault(f, UPB_TYPE_UINT64)) - f->defaultval.uint = value; +int upb_msgdef_numfields(const upb_msgdef *m) { + /* The number table contains only fields. */ + return upb_inttable_count(&m->itof); } -void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t value) { - if (checksetdefault(f, UPB_TYPE_UINT32)) - f->defaultval.uint = value; +int upb_msgdef_numoneofs(const upb_msgdef *m) { + /* The name table includes oneofs, and the number table does not. */ + return upb_strtable_count(&m->ntof) - upb_inttable_count(&m->itof); } -void upb_fielddef_setdefaultbool(upb_fielddef *f, bool value) { - if (checksetdefault(f, UPB_TYPE_BOOL)) - f->defaultval.uint = value; +bool upb_msgdef_mapentry(const upb_msgdef *m) { + return m->map_entry; } -void upb_fielddef_setdefaultfloat(upb_fielddef *f, float value) { - if (checksetdefault(f, UPB_TYPE_FLOAT)) - f->defaultval.flt = value; +upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m) { + return m->well_known_type; } -void upb_fielddef_setdefaultdouble(upb_fielddef *f, double value) { - if (checksetdefault(f, UPB_TYPE_DOUBLE)) - f->defaultval.dbl = value; +bool upb_msgdef_isnumberwrapper(const upb_msgdef *m) { + upb_wellknowntype_t type = upb_msgdef_wellknowntype(m); + return type >= UPB_WELLKNOWN_DOUBLEVALUE && + type <= UPB_WELLKNOWN_UINT32VALUE; } -bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len, - upb_status *s) { - str_t *str2; - UPB_ASSERT(upb_fielddef_isstring(f) || f->type_ == UPB_TYPE_ENUM); - if (f->type_ == UPB_TYPE_ENUM && !upb_isident(str, len, false, s)) - return false; +void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) { + upb_inttable_begin(iter, &m->itof); +} - if (f->default_is_string) { - str_t *s = f->defaultval.bytes; - UPB_ASSERT(s || f->type_ == UPB_TYPE_ENUM); - if (s) freestr(s); - } else { - UPB_ASSERT(f->type_ == UPB_TYPE_ENUM); - } +void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); } - str2 = newstr(str, len); - f->defaultval.bytes = str2; - f->default_is_string = true; - return true; +bool upb_msg_field_done(const upb_msg_field_iter *iter) { + return upb_inttable_done(iter); } -void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str, - upb_status *s) { - UPB_ASSERT(f->type_is_set_); - upb_fielddef_setdefaultstr(f, str, str ? strlen(str) : 0, s); +upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) { + return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter)); } -bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f) { - int32_t val; - UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM); - return enumdefaultint32(f, &val); +void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) { + upb_inttable_iter_setdone(iter); } -bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f) { - UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM); - return enumdefaultstr(f) != NULL; +bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1, + const upb_msg_field_iter * iter2) { + return upb_inttable_iter_isequal(iter1, iter2); } -static bool upb_subdef_typecheck(upb_fielddef *f, const upb_def *subdef, - upb_status *s) { - if (f->type_ == UPB_TYPE_MESSAGE) { - if (upb_dyncast_msgdef(subdef)) return true; - upb_status_seterrmsg(s, "invalid subdef type for this submessage field"); - return false; - } else if (f->type_ == UPB_TYPE_ENUM) { - if (upb_dyncast_enumdef(subdef)) return true; - upb_status_seterrmsg(s, "invalid subdef type for this enum field"); - return false; - } else { - upb_status_seterrmsg(s, "only message and enum fields can have a subdef"); - return false; +void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) { + upb_strtable_begin(iter, &m->ntof); + /* We need to skip past any initial fields. */ + while (!upb_strtable_done(iter) && + !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)) { + upb_strtable_next(iter); } } -static void release_subdef(upb_fielddef *f) { - if (f->subdef_is_symbolic) { - upb_gfree(f->sub.name); - } else if (f->sub.def) { - upb_unref2(f->sub.def, f); - } +void upb_msg_oneof_next(upb_msg_oneof_iter *iter) { + /* We need to skip past fields to return only oneofs. */ + do { + upb_strtable_next(iter); + } while (!upb_strtable_done(iter) && + !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)); } -bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef, - upb_status *s) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - UPB_ASSERT(upb_fielddef_hassubdef(f)); - if (subdef && !upb_subdef_typecheck(f, subdef, s)) return false; - release_subdef(f); - f->sub.def = subdef; - f->subdef_is_symbolic = false; - if (f->sub.def) upb_ref2(f->sub.def, f); - return true; +bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) { + return upb_strtable_done(iter); } -bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef, - upb_status *s) { - return upb_fielddef_setsubdef(f, upb_msgdef_upcast(subdef), s); +const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) { + return unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF); } -bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef, - upb_status *s) { - return upb_fielddef_setsubdef(f, upb_enumdef_upcast(subdef), s); +void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) { + upb_strtable_iter_setdone(iter); } -bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name, - upb_status *s) { - char *name_copy; - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - if (!upb_fielddef_hassubdef(f)) { - upb_status_seterrmsg(s, "field type does not accept a subdef"); - return false; - } +bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1, + const upb_msg_oneof_iter *iter2) { + return upb_strtable_iter_isequal(iter1, iter2); +} - name_copy = upb_gstrdup(name); - if (!name_copy) { - upb_upberr_setoom(s); - return false; - } +/* upb_oneofdef ***************************************************************/ - /* TODO: validate name (upb_isident() doesn't quite work atm because this name - * may have a leading "."). */ - release_subdef(f); - f->sub.name = name_copy; - f->subdef_is_symbolic = true; - return true; +const char *upb_oneofdef_name(const upb_oneofdef *o) { + return shortdefname(o->full_name); } -bool upb_fielddef_issubmsg(const upb_fielddef *f) { - return upb_fielddef_type(f) == UPB_TYPE_MESSAGE; +const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) { + return o->parent; } -bool upb_fielddef_isstring(const upb_fielddef *f) { - return upb_fielddef_type(f) == UPB_TYPE_STRING || - upb_fielddef_type(f) == UPB_TYPE_BYTES; +int upb_oneofdef_numfields(const upb_oneofdef *o) { + return upb_strtable_count(&o->ntof); } -bool upb_fielddef_isseq(const upb_fielddef *f) { - return upb_fielddef_label(f) == UPB_LABEL_REPEATED; +uint32_t upb_oneofdef_index(const upb_oneofdef *o) { + return o->index; } -bool upb_fielddef_isprimitive(const upb_fielddef *f) { - return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f); +const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, + const char *name, size_t length) { + upb_value val; + return upb_strtable_lookup2(&o->ntof, name, length, &val) ? + upb_value_getptr(val) : NULL; } -bool upb_fielddef_ismap(const upb_fielddef *f) { - return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) && - upb_msgdef_mapentry(upb_fielddef_msgsubdef(f)); +const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) { + upb_value val; + return upb_inttable_lookup32(&o->itof, num, &val) ? + upb_value_getptr(val) : NULL; } -bool upb_fielddef_haspresence(const upb_fielddef *f) { - if (upb_fielddef_isseq(f)) return false; - if (upb_fielddef_issubmsg(f)) return true; - - /* Primitive field: return true unless there is a message that specifies - * presence should not exist. */ - if (f->msg_is_symbolic || !f->msg.def) return true; - return f->msg.def->syntax == UPB_SYNTAX_PROTO2; +void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) { + upb_inttable_begin(iter, &o->itof); } -bool upb_fielddef_hassubdef(const upb_fielddef *f) { - return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM; +void upb_oneof_next(upb_oneof_iter *iter) { + upb_inttable_next(iter); } -static bool between(int32_t x, int32_t low, int32_t high) { - return x >= low && x <= high; +bool upb_oneof_done(upb_oneof_iter *iter) { + return upb_inttable_done(iter); } -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); } +upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) { + return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter)); +} -bool upb_fielddef_checkdescriptortype(int32_t type) { - return between(type, 1, 18); -} - -/* upb_msgdef *****************************************************************/ - -static void visitmsg(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - upb_msg_oneof_iter o; - const upb_msgdef *m = (const upb_msgdef*)r; - const upb_def *def = upb_msgdef_upcast(m); - upb_msg_field_iter i; - for(upb_msg_field_begin(&i, m); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - visit(r, upb_fielddef_upcast2(f), closure); - } - for(upb_msg_oneof_begin(&o, m); - !upb_msg_oneof_done(&o); - upb_msg_oneof_next(&o)) { - upb_oneofdef *f = upb_msg_iter_oneof(&o); - visit(r, upb_oneofdef_upcast(f), closure); - } - if (upb_def_file(def)) { - visit(r, upb_filedef_upcast(upb_def_file(def)), closure); - } -} - -static void freemsg(upb_refcounted *r) { - upb_msgdef *m = (upb_msgdef*)r; - upb_strtable_uninit(&m->ntof); - upb_inttable_uninit(&m->itof); - upb_def_uninit(upb_msgdef_upcast_mutable(m)); - upb_gfree(m); +void upb_oneof_iter_setdone(upb_oneof_iter *iter) { + upb_inttable_iter_setdone(iter); } -const struct upb_refcounted_vtbl upb_msgdef_vtbl = {visitmsg, freemsg}; - -upb_msgdef *upb_msgdef_new(const void *owner) { - upb_msgdef *m = upb_gmalloc(sizeof(*m)); - if (!m) return NULL; +/* Code to build defs from descriptor protos. *********************************/ - if (!upb_def_init(upb_msgdef_upcast_mutable(m), UPB_DEF_MSG, &upb_msgdef_vtbl, - owner)) { - goto err2; - } +/* There is a question of how much validation to do here. It will be difficult + * to perfectly match the amount of validation performed by proto2. But since + * this code is used to directly build defs from Ruby (for example) we do need + * to validate important constraints like uniqueness of names and numbers. */ - if (!upb_inttable_init(&m->itof, UPB_CTYPE_PTR)) goto err2; - if (!upb_strtable_init(&m->ntof, UPB_CTYPE_PTR)) goto err1; - m->map_entry = false; - m->syntax = UPB_SYNTAX_PROTO2; - return m; +#define CHK(x) if (!(x)) { return false; } +#define CHK_OOM(x) if (!(x)) { upb_status_setoom(ctx->status); return false; } -err1: - upb_inttable_uninit(&m->itof); -err2: - upb_gfree(m); - return NULL; -} +typedef struct { + const upb_symtab *symtab; + upb_filedef *file; /* File we are building. */ + upb_alloc *alloc; /* Allocate defs here. */ + upb_alloc *tmp; /* Alloc for addtab and any other tmp data. */ + upb_strtable *addtab; /* full_name -> packed def ptr for new defs. */ + upb_status *status; /* Record errors here. */ +} symtab_addctx; -bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status) { - upb_def *d = upb_msgdef_upcast_mutable(m); - return upb_def_freeze(&d, 1, status); +static char* strviewdup(const symtab_addctx *ctx, upb_strview view) { + return upb_strdup2(view.data, view.size, ctx->alloc); } -const char *upb_msgdef_fullname(const upb_msgdef *m) { - return upb_def_fullname(upb_msgdef_upcast(m)); +static bool streql2(const char *a, size_t n, const char *b) { + return n == strlen(b) && memcmp(a, b, n) == 0; } -const char *upb_msgdef_name(const upb_msgdef *m) { - return upb_def_name(upb_msgdef_upcast(m)); +static bool streql_view(upb_strview view, const char *b) { + return streql2(view.data, view.size, b); } -bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, - upb_status *s) { - return upb_def_setfullname(upb_msgdef_upcast_mutable(m), fullname, s); +static const char *makefullname(const symtab_addctx *ctx, const char *prefix, + upb_strview name) { + if (prefix) { + /* ret = prefix + '.' + name; */ + size_t n = strlen(prefix); + char *ret = upb_malloc(ctx->alloc, n + name.size + 2); + CHK_OOM(ret); + strcpy(ret, prefix); + ret[n] = '.'; + memcpy(&ret[n + 1], name.data, name.size); + ret[n + 1 + name.size] = '\0'; + return ret; + } else { + return strviewdup(ctx, name); + } } -bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax) { - if (syntax != UPB_SYNTAX_PROTO2 && syntax != UPB_SYNTAX_PROTO3) { +static bool symtab_add(const symtab_addctx *ctx, const char *name, + upb_value v) { + upb_value tmp; + if (upb_strtable_lookup(ctx->addtab, name, &tmp) || + upb_strtable_lookup(&ctx->symtab->syms, name, &tmp)) { + upb_status_seterrf(ctx->status, "duplicate symbol '%s'", name); return false; } - m->syntax = syntax; + CHK_OOM(upb_strtable_insert3(ctx->addtab, name, strlen(name), v, ctx->tmp)); return true; } -upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) { - return m->syntax; -} +/* Given a symbol and the base symbol inside which it is defined, find the + * symbol's definition in t. */ +static bool resolvename(const upb_strtable *t, const upb_fielddef *f, + const char *base, upb_strview sym, + upb_deftype_t type, upb_status *status, + const void **def) { + if(sym.size == 0) return NULL; + if(sym.data[0] == '.') { + /* Symbols starting with '.' are absolute, so we do a single lookup. + * Slice to omit the leading '.' */ + upb_value v; + if (!upb_strtable_lookup2(t, sym.data + 1, sym.size - 1, &v)) { + return false; + } -/* Helper: check that the field |f| is safe to add to msgdef |m|. Set an error - * on status |s| and return false if not. */ -static bool check_field_add(const upb_msgdef *m, const upb_fielddef *f, - upb_status *s) { - if (upb_fielddef_containingtype(f) != NULL) { - upb_status_seterrmsg(s, "fielddef already belongs to a message"); - return false; - } else if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) { - upb_status_seterrmsg(s, "field name or number were not set"); - return false; - } else if (upb_msgdef_itof(m, upb_fielddef_number(f))) { - upb_status_seterrmsg(s, "duplicate field number"); - return false; - } else if (upb_strtable_lookup(&m->ntof, upb_fielddef_name(f), NULL)) { - upb_status_seterrmsg(s, "name conflicts with existing field or oneof"); - return false; - } - return true; -} + *def = unpack_def(v, type); -static void add_field(upb_msgdef *m, upb_fielddef *f, const void *ref_donor) { - release_containingtype(f); - f->msg.def = m; - f->msg_is_symbolic = false; - upb_inttable_insert(&m->itof, upb_fielddef_number(f), upb_value_ptr(f)); - upb_strtable_insert(&m->ntof, upb_fielddef_name(f), upb_value_ptr(f)); - upb_ref2(f, m); - upb_ref2(m, f); - if (ref_donor) upb_fielddef_unref(f, ref_donor); -} + if (!*def) { + upb_status_seterrf(status, + "type mismatch when resolving field %s, name %s", + f->full_name, sym.data); + return false; + } -bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor, - upb_status *s) { - /* TODO: extensions need to have a separate namespace, because proto2 allows a - * top-level extension (ie. one not in any package) to have the same name as a - * field from the message. - * - * This also implies that there needs to be a separate lookup-by-name method - * for extensions. It seems desirable for iteration to return both extensions - * and non-extensions though. - * - * We also need to validate that the field number is in an extension range iff - * it is an extension. - * - * This method is idempotent. Check if |f| is already part of this msgdef and - * return immediately if so. */ - if (upb_fielddef_containingtype(f) == m) { - if (ref_donor) upb_fielddef_unref(f, ref_donor); return true; - } - - /* Check constraints for all fields before performing any action. */ - if (!check_field_add(m, f, s)) { - return false; - } else if (upb_fielddef_containingoneof(f) != NULL) { - /* Fields in a oneof can only be added by adding the oneof to the msgdef. */ - upb_status_seterrmsg(s, "fielddef is part of a oneof"); + } else { + /* Remove components from base until we find an entry or run out. + * TODO: This branch is totally broken, but currently not used. */ + (void)base; + UPB_ASSERT(false); return false; } - - /* Constraint checks ok, perform the action. */ - add_field(m, f, ref_donor); - return true; } -bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor, - upb_status *s) { - upb_oneof_iter it; - - /* Check various conditions that would prevent this oneof from being added. */ - if (upb_oneofdef_containingtype(o)) { - upb_status_seterrmsg(s, "oneofdef already belongs to a message"); - return false; - } else if (upb_oneofdef_name(o) == NULL) { - upb_status_seterrmsg(s, "oneofdef name was not set"); - return false; - } else if (upb_strtable_lookup(&m->ntof, upb_oneofdef_name(o), NULL)) { - upb_status_seterrmsg(s, "name conflicts with existing field or oneof"); - return false; - } - - /* Check that all of the oneof's fields do not conflict with names or numbers - * of fields already in the message. */ - for (upb_oneof_begin(&it, o); !upb_oneof_done(&it); upb_oneof_next(&it)) { - const upb_fielddef *f = upb_oneof_iter_field(&it); - if (!check_field_add(m, f, s)) { - return false; +const void *symtab_resolve(const symtab_addctx *ctx, const upb_fielddef *f, + const char *base, upb_strview sym, + upb_deftype_t type) { + const void *ret; + if (!resolvename(ctx->addtab, f, base, sym, type, ctx->status, &ret) && + !resolvename(&ctx->symtab->syms, f, base, sym, type, ctx->status, &ret)) { + if (upb_ok(ctx->status)) { + upb_status_seterrf(ctx->status, "couldn't resolve name '%s'", sym.data); } + return false; } + return ret; +} - /* Everything checks out -- commit now. */ +static bool create_oneofdef( + const symtab_addctx *ctx, upb_msgdef *m, + const google_protobuf_OneofDescriptorProto *oneof_proto) { + upb_oneofdef *o; + upb_strview name = google_protobuf_OneofDescriptorProto_name(oneof_proto); + upb_value v; - /* Add oneof itself first. */ + o = (upb_oneofdef*)&m->oneofs[m->oneof_count++]; o->parent = m; - upb_strtable_insert(&m->ntof, upb_oneofdef_name(o), upb_value_ptr(o)); - upb_ref2(o, m); - upb_ref2(m, o); + o->full_name = makefullname(ctx, m->full_name, name); - /* Add each field of the oneof directly to the msgdef. */ - for (upb_oneof_begin(&it, o); !upb_oneof_done(&it); upb_oneof_next(&it)) { - upb_fielddef *f = upb_oneof_iter_field(&it); - add_field(m, f, NULL); - } + v = pack_def(o, UPB_DEFTYPE_ONEOF); + CHK_OOM(symtab_add(ctx, o->full_name, v)); + CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, v, ctx->alloc)); - if (ref_donor) upb_oneofdef_unref(o, ref_donor); + CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_CONSTPTR, ctx->alloc)); + CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc)); return true; } -const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) { - upb_value val; - return upb_inttable_lookup32(&m->itof, i, &val) ? - upb_value_getptr(val) : NULL; -} - -const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, - size_t len) { - upb_value val; +static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, + upb_fielddef *f) { + char *end; + char nullz[64]; + errno = 0; - if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) { - return NULL; + switch (upb_fielddef_type(f)) { + case UPB_TYPE_INT32: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT32: + case UPB_TYPE_UINT64: + case UPB_TYPE_DOUBLE: + case UPB_TYPE_FLOAT: + /* Standard C number parsing functions expect null-terminated strings. */ + if (len >= sizeof(nullz) - 1) { + return false; + } + memcpy(nullz, str, len); + nullz[len] = '\0'; + str = nullz; + break; + default: + break; } - return upb_trygetfield(upb_value_getptr(val)); + switch (upb_fielddef_type(f)) { + case UPB_TYPE_INT32: { + long val = strtol(str, &end, 0); + CHK(val <= INT32_MAX && val >= INT32_MIN && errno != ERANGE && !*end); + f->defaultval.sint = val; + break; + } + case UPB_TYPE_ENUM: { + const upb_enumdef *e = f->sub.enumdef; + int32_t val; + CHK(upb_enumdef_ntoi(e, str, len, &val)); + f->defaultval.sint = val; + break; + } + case UPB_TYPE_INT64: { + /* XXX: Need to write our own strtoll, since it's not available in c89. */ + long long val = strtol(str, &end, 0); + CHK(val <= INT64_MAX && val >= INT64_MIN && errno != ERANGE && !*end); + f->defaultval.sint = val; + break; + } + case UPB_TYPE_UINT32: { + unsigned long val = strtoul(str, &end, 0); + CHK(val <= UINT32_MAX && errno != ERANGE && !*end); + f->defaultval.uint = val; + break; + } + case UPB_TYPE_UINT64: { + /* XXX: Need to write our own strtoull, since it's not available in c89. */ + unsigned long long val = strtoul(str, &end, 0); + CHK(val <= UINT64_MAX && errno != ERANGE && !*end); + f->defaultval.uint = val; + break; + } + case UPB_TYPE_DOUBLE: { + double val = strtod(str, &end); + CHK(errno != ERANGE && !*end); + f->defaultval.dbl = val; + break; + } + case UPB_TYPE_FLOAT: { + /* XXX: Need to write our own strtof, since it's not available in c89. */ + float val = strtod(str, &end); + CHK(errno != ERANGE && !*end); + f->defaultval.flt = val; + break; + } + case UPB_TYPE_BOOL: { + if (streql2(str, len, "false")) { + f->defaultval.boolean = false; + } else if (streql2(str, len, "true")) { + f->defaultval.boolean = true; + } else { + return false; + } + } + case UPB_TYPE_STRING: + f->defaultval.str = newstr(ctx->alloc, str, len); + break; + case UPB_TYPE_BYTES: + /* XXX: need to interpret the C-escaped value. */ + f->defaultval.str = newstr(ctx->alloc, str, len); + break; + case UPB_TYPE_MESSAGE: + /* Should not have a default value. */ + return false; + } + return true; } -const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, - size_t len) { - upb_value val; - - if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) { - return NULL; +static void set_default_default(const symtab_addctx *ctx, upb_fielddef *f) { + switch (upb_fielddef_type(f)) { + case UPB_TYPE_INT32: + case UPB_TYPE_INT64: + case UPB_TYPE_ENUM: + f->defaultval.sint = 0; + break; + case UPB_TYPE_UINT64: + case UPB_TYPE_UINT32: + f->defaultval.uint = 0; + break; + case UPB_TYPE_DOUBLE: + case UPB_TYPE_FLOAT: + f->defaultval.dbl = 0; + break; + case UPB_TYPE_STRING: + case UPB_TYPE_BYTES: + f->defaultval.str = newstr(ctx->alloc, NULL, 0); + break; + case UPB_TYPE_BOOL: + f->defaultval.boolean = false; + break; + case UPB_TYPE_MESSAGE: + break; } - - return upb_trygetoneof(upb_value_getptr(val)); } -bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len, - const upb_fielddef **f, const upb_oneofdef **o) { - upb_value val; - - if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) { +static bool create_fielddef( + const symtab_addctx *ctx, const char *prefix, upb_msgdef *m, + const google_protobuf_FieldDescriptorProto *field_proto) { + upb_alloc *alloc = ctx->alloc; + upb_fielddef *f; + const google_protobuf_FieldOptions *options; + upb_strview name; + const char *full_name; + const char *shortname; + uint32_t field_number; + + if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) { + upb_status_seterrmsg(ctx->status, "field has no name"); return false; } - *o = upb_trygetoneof(upb_value_getptr(val)); - *f = upb_trygetfield(upb_value_getptr(val)); - UPB_ASSERT((*o != NULL) ^ (*f != NULL)); /* Exactly one of the two should be set. */ - return true; -} + name = google_protobuf_FieldDescriptorProto_name(field_proto); + CHK(upb_isident(name, false, ctx->status)); + full_name = makefullname(ctx, prefix, name); + shortname = shortdefname(full_name); -int upb_msgdef_numfields(const upb_msgdef *m) { - /* The number table contains only fields. */ - return upb_inttable_count(&m->itof); -} + field_number = google_protobuf_FieldDescriptorProto_number(field_proto); -int upb_msgdef_numoneofs(const upb_msgdef *m) { - /* The name table includes oneofs, and the number table does not. */ - return upb_strtable_count(&m->ntof) - upb_inttable_count(&m->itof); -} + if (field_number == 0 || field_number > UPB_MAX_FIELDNUMBER) { + upb_status_seterrf(ctx->status, "invalid field number (%u)", field_number); + return false; + } -void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry) { - UPB_ASSERT(!upb_msgdef_isfrozen(m)); - m->map_entry = map_entry; -} + if (m) { + /* direct message field. */ + upb_value v, packed_v; -bool upb_msgdef_mapentry(const upb_msgdef *m) { - return m->map_entry; -} + f = (upb_fielddef*)&m->fields[m->field_count++]; + f->msgdef = m; + f->is_extension_ = false; -void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) { - upb_inttable_begin(iter, &m->itof); -} + packed_v = pack_def(f, UPB_DEFTYPE_FIELD); + v = upb_value_constptr(f); -void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); } + if (!upb_strtable_insert3(&m->ntof, name.data, name.size, packed_v, alloc)) { + upb_status_seterrf(ctx->status, "duplicate field name (%s)", shortname); + return false; + } -bool upb_msg_field_done(const upb_msg_field_iter *iter) { - return upb_inttable_done(iter); -} + if (!upb_inttable_insert2(&m->itof, field_number, v, alloc)) { + upb_status_seterrf(ctx->status, "duplicate field number (%u)", + field_number); + return false; + } + } else { + /* extension field. */ + f = (upb_fielddef*)&ctx->file->exts[ctx->file->ext_count]; + f->is_extension_ = true; + CHK_OOM(symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_FIELD))); + } -upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) { - return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter)); -} + f->full_name = full_name; + f->file = ctx->file; + f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto); + f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto); + f->number_ = field_number; + f->oneof = NULL; -void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) { - upb_inttable_iter_setdone(iter); -} + /* 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; -void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) { - upb_strtable_begin(iter, &m->ntof); - /* We need to skip past any initial fields. */ - while (!upb_strtable_done(iter) && - !upb_isoneof(upb_value_getptr(upb_strtable_iter_value(iter)))) { - upb_strtable_next(iter); + if (f->label_ == UPB_LABEL_REQUIRED && f->file->syntax == UPB_SYNTAX_PROTO3) { + upb_status_seterrf(ctx->status, "proto3 fields cannot be required (%s)", + f->full_name); + return false; } -} -void upb_msg_oneof_next(upb_msg_oneof_iter *iter) { - /* We need to skip past fields to return only oneofs. */ - do { - upb_strtable_next(iter); - } while (!upb_strtable_done(iter) && - !upb_isoneof(upb_value_getptr(upb_strtable_iter_value(iter)))); -} + if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) { + int oneof_index = + google_protobuf_FieldDescriptorProto_oneof_index(field_proto); + upb_oneofdef *oneof; + upb_value v = upb_value_constptr(f); -bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) { - return upb_strtable_done(iter); -} + if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) { + upb_status_seterrf(ctx->status, + "fields in oneof must have OPTIONAL label (%s)", + f->full_name); + return false; + } -upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) { - return (upb_oneofdef*)upb_value_getptr(upb_strtable_iter_value(iter)); -} + if (!m) { + upb_status_seterrf(ctx->status, + "oneof_index provided for extension field (%s)", + f->full_name); + return false; + } -void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) { - upb_strtable_iter_setdone(iter); -} + if (oneof_index >= m->oneof_count) { + upb_status_seterrf(ctx->status, "oneof_index out of range (%s)", + f->full_name); + return false; + } -/* upb_oneofdef ***************************************************************/ + oneof = (upb_oneofdef*)&m->oneofs[oneof_index]; + f->oneof = oneof; -static void visitoneof(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_oneofdef *o = (const upb_oneofdef*)r; - upb_oneof_iter i; - for (upb_oneof_begin(&i, o); !upb_oneof_done(&i); upb_oneof_next(&i)) { - const upb_fielddef *f = upb_oneof_iter_field(&i); - visit(r, upb_fielddef_upcast2(f), closure); + CHK(upb_inttable_insert2(&oneof->itof, f->number_, v, alloc)); + CHK(upb_strtable_insert3(&oneof->ntof, name.data, name.size, v, alloc)); + } else { + f->oneof = NULL; } - if (o->parent) { - visit(r, upb_msgdef_upcast2(o->parent), closure); + + if (google_protobuf_FieldDescriptorProto_has_options(field_proto)) { + options = google_protobuf_FieldDescriptorProto_options(field_proto); + f->lazy_ = google_protobuf_FieldOptions_lazy(options); + f->packed_ = google_protobuf_FieldOptions_packed(options); + } else { + f->lazy_ = false; + f->packed_ = false; } -} -static void freeoneof(upb_refcounted *r) { - upb_oneofdef *o = (upb_oneofdef*)r; - upb_strtable_uninit(&o->ntof); - upb_inttable_uninit(&o->itof); - upb_gfree((void*)o->name); - upb_gfree(o); + return true; } -const struct upb_refcounted_vtbl upb_oneofdef_vtbl = {visitoneof, freeoneof}; - -upb_oneofdef *upb_oneofdef_new(const void *owner) { - upb_oneofdef *o = upb_gmalloc(sizeof(*o)); - - if (!o) { - return NULL; - } - - o->parent = NULL; - o->name = NULL; +static bool create_enumdef( + const symtab_addctx *ctx, const char *prefix, + const google_protobuf_EnumDescriptorProto *enum_proto) { + upb_enumdef *e; + const google_protobuf_EnumValueDescriptorProto *const *values; + upb_strview name; + size_t i, n; - if (!upb_refcounted_init(upb_oneofdef_upcast_mutable(o), &upb_oneofdef_vtbl, - owner)) { - goto err2; - } + name = google_protobuf_EnumDescriptorProto_name(enum_proto); + CHK(upb_isident(name, false, ctx->status)); - if (!upb_inttable_init(&o->itof, UPB_CTYPE_PTR)) goto err2; - if (!upb_strtable_init(&o->ntof, UPB_CTYPE_PTR)) goto err1; + e = (upb_enumdef*)&ctx->file->enums[ctx->file->enum_count++]; + e->full_name = makefullname(ctx, prefix, name); + CHK_OOM(symtab_add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM))); - return o; + CHK_OOM(upb_strtable_init2(&e->ntoi, UPB_CTYPE_INT32, ctx->alloc)); + CHK_OOM(upb_inttable_init2(&e->iton, UPB_CTYPE_CSTR, ctx->alloc)); -err1: - upb_inttable_uninit(&o->itof); -err2: - upb_gfree(o); - return NULL; -} + e->file = ctx->file; + e->defaultval = 0; -const char *upb_oneofdef_name(const upb_oneofdef *o) { return o->name; } + values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n); -bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s) { - UPB_ASSERT(!upb_oneofdef_isfrozen(o)); - if (upb_oneofdef_containingtype(o)) { - upb_status_seterrmsg(s, "oneof already added to a message"); + if (n == 0) { + upb_status_seterrf(ctx->status, + "enums must contain at least one value (%s)", + e->full_name); return false; } - if (!upb_isident(name, strlen(name), true, s)) { - return false; - } + for (i = 0; i < n; i++) { + const google_protobuf_EnumValueDescriptorProto *value = values[i]; + upb_strview name = google_protobuf_EnumValueDescriptorProto_name(value); + char *name2 = strviewdup(ctx, name); + int32_t num = google_protobuf_EnumValueDescriptorProto_number(value); + upb_value v = upb_value_int32(num); + + if (i == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && num != 0) { + upb_status_seterrf(ctx->status, + "for proto3, the first enum value must be zero (%s)", + e->full_name); + return false; + } - name = upb_gstrdup(name); - if (!name) { - upb_status_seterrmsg(s, "One of memory"); - return false; + if (upb_strtable_lookup(&e->ntoi, name2, NULL)) { + upb_status_seterrf(ctx->status, "duplicate enum label '%s'", name2); + return false; + } + + CHK_OOM(name2) + CHK_OOM( + upb_strtable_insert3(&e->ntoi, name2, strlen(name2), v, ctx->alloc)); + + if (!upb_inttable_lookup(&e->iton, num, NULL)) { + upb_value v = upb_value_cstr(name2); + CHK_OOM(upb_inttable_insert2(&e->iton, num, v, ctx->alloc)); + } } - upb_gfree((void*)o->name); - o->name = name; + upb_inttable_compact2(&e->iton, ctx->alloc); + return true; } -const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) { - return o->parent; -} +static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, + const google_protobuf_DescriptorProto *msg_proto) { + upb_msgdef *m; + const google_protobuf_MessageOptions *options; + const google_protobuf_OneofDescriptorProto *const *oneofs; + const google_protobuf_FieldDescriptorProto *const *fields; + const google_protobuf_EnumDescriptorProto *const *enums; + const google_protobuf_DescriptorProto *const *msgs; + size_t i, n; + upb_strview name; -int upb_oneofdef_numfields(const upb_oneofdef *o) { - return upb_strtable_count(&o->ntof); + name = google_protobuf_DescriptorProto_name(msg_proto); + CHK(upb_isident(name, false, ctx->status)); + + m = (upb_msgdef*)&ctx->file->msgs[ctx->file->msg_count++]; + m->full_name = makefullname(ctx, prefix, name); + CHK_OOM(symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG))); + + CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_CONSTPTR, ctx->alloc)); + CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc)); + + m->file = ctx->file; + m->map_entry = false; + + options = google_protobuf_DescriptorProto_options(msg_proto); + + if (options) { + m->map_entry = google_protobuf_MessageOptions_map_entry(options); + } + + oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n); + m->oneof_count = 0; + m->oneofs = upb_malloc(ctx->alloc, sizeof(*m->oneofs) * n); + for (i = 0; i < n; i++) { + CHK(create_oneofdef(ctx, m, oneofs[i])); + } + + fields = google_protobuf_DescriptorProto_field(msg_proto, &n); + m->field_count = 0; + m->fields = upb_malloc(ctx->alloc, sizeof(*m->fields) * n); + for (i = 0; i < n; i++) { + CHK(create_fielddef(ctx, m->full_name, m, fields[i])); + } + + CHK(assign_msg_indices(m, ctx->status)); + assign_msg_wellknowntype(m); + upb_inttable_compact2(&m->itof, ctx->alloc); + + /* This message is built. Now build nested messages and enums. */ + + enums = google_protobuf_DescriptorProto_enum_type(msg_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_enumdef(ctx, m->full_name, enums[i])); + } + + msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_msgdef(ctx, m->full_name, msgs[i])); + } + + return true; } -uint32_t upb_oneofdef_index(const upb_oneofdef *o) { - return o->index; +typedef struct { + int msg_count; + int enum_count; + int ext_count; +} decl_counts; + +static void count_types_in_msg(const google_protobuf_DescriptorProto *msg_proto, + decl_counts *counts) { + const google_protobuf_DescriptorProto *const *msgs; + size_t i, n; + + counts->msg_count++; + + msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n); + for (i = 0; i < n; i++) { + count_types_in_msg(msgs[i], counts); + } + + google_protobuf_DescriptorProto_enum_type(msg_proto, &n); + counts->enum_count += n; + + google_protobuf_DescriptorProto_extension(msg_proto, &n); + counts->ext_count += n; } -bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f, - const void *ref_donor, - upb_status *s) { - UPB_ASSERT(!upb_oneofdef_isfrozen(o)); - UPB_ASSERT(!o->parent || !upb_msgdef_isfrozen(o->parent)); +static void count_types_in_file( + const google_protobuf_FileDescriptorProto *file_proto, + decl_counts *counts) { + const google_protobuf_DescriptorProto *const *msgs; + size_t i, n; - /* This method is idempotent. Check if |f| is already part of this oneofdef - * and return immediately if so. */ - if (upb_fielddef_containingoneof(f) == o) { - return true; + msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n); + for (i = 0; i < n; i++) { + count_types_in_msg(msgs[i], counts); } - /* The field must have an OPTIONAL label. */ - if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) { - upb_status_seterrmsg(s, "fields in oneof must have OPTIONAL label"); - return false; + google_protobuf_FileDescriptorProto_enum_type(file_proto, &n); + counts->enum_count += n; + + google_protobuf_FileDescriptorProto_extension(file_proto, &n); + counts->ext_count += n; +} + +static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix, + upb_fielddef *f) { + upb_strview name; + const google_protobuf_FieldDescriptorProto *field_proto = f->sub.unresolved; + + if (f->is_extension_) { + if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) { + upb_status_seterrf(ctx->status, + "extension for field '%s' had no extendee", + f->full_name); + return false; + } + + name = google_protobuf_FieldDescriptorProto_extendee(field_proto); + f->msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG); + CHK(f->msgdef); } - /* Check that no field with this name or number exists already in the oneof. - * Also check that the field is not already part of a oneof. */ - if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) { - upb_status_seterrmsg(s, "field name or number were not set"); - return false; - } else if (upb_oneofdef_itof(o, upb_fielddef_number(f)) || - upb_oneofdef_ntofz(o, upb_fielddef_name(f))) { - upb_status_seterrmsg(s, "duplicate field name or number"); - return false; - } else if (upb_fielddef_containingoneof(f) != NULL) { - upb_status_seterrmsg(s, "fielddef already belongs to a oneof"); + if ((upb_fielddef_issubmsg(f) || f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) && + !google_protobuf_FieldDescriptorProto_has_type_name(field_proto)) { + upb_status_seterrf(ctx->status, "field '%s' is missing type name", + f->full_name); return false; } - /* We allow adding a field to the oneof either if the field is not part of a - * msgdef, or if it is and we are also part of the same msgdef. */ - if (o->parent == NULL) { - /* If we're not in a msgdef, the field cannot be either. Otherwise we would - * need to magically add this oneof to a msgdef to remain consistent, which - * is surprising behavior. */ - if (upb_fielddef_containingtype(f) != NULL) { - upb_status_seterrmsg(s, "fielddef already belongs to a message, but " - "oneof does not"); + name = google_protobuf_FieldDescriptorProto_type_name(field_proto); + + if (upb_fielddef_issubmsg(f)) { + f->sub.msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG); + CHK(f->sub.msgdef); + } else if (f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) { + f->sub.enumdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_ENUM); + CHK(f->sub.enumdef); + } + + /* 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_strview defaultval = + google_protobuf_FieldDescriptorProto_default_value(field_proto); + + if (f->file->syntax == UPB_SYNTAX_PROTO3) { + upb_status_seterrf(ctx->status, + "proto3 fields cannot have explicit defaults (%s)", + f->full_name); return false; } - } else { - /* If we're in a msgdef, the user can add fields that either aren't in any - * msgdef (in which case they're added to our msgdef) or already a part of - * our msgdef. */ - if (upb_fielddef_containingtype(f) != NULL && - upb_fielddef_containingtype(f) != o->parent) { - upb_status_seterrmsg(s, "fielddef belongs to a different message " - "than oneof"); + + if (upb_fielddef_issubmsg(f)) { + upb_status_seterrf(ctx->status, + "message fields cannot have explicit defaults (%s)", + f->full_name); return false; } - } - - /* Commit phase. First add the field to our parent msgdef, if any, because - * that may fail; then add the field to our own tables. */ - if (o->parent != NULL && upb_fielddef_containingtype(f) == NULL) { - if (!upb_msgdef_addfield((upb_msgdef*)o->parent, f, NULL, s)) { + if (!parse_default(ctx, defaultval.data, defaultval.size, f)) { + upb_status_seterrf(ctx->status, + "couldn't parse default '" UPB_STRVIEW_FORMAT + "' for field (%s)", + UPB_STRVIEW_ARGS(defaultval), f->full_name); return false; } + } else { + set_default_default(ctx, f); } - release_containingtype(f); - f->oneof = o; - upb_inttable_insert(&o->itof, upb_fielddef_number(f), upb_value_ptr(f)); - upb_strtable_insert(&o->ntof, upb_fielddef_name(f), upb_value_ptr(f)); - upb_ref2(f, o); - upb_ref2(o, f); - if (ref_donor) upb_fielddef_unref(f, ref_donor); - return true; } -const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, - const char *name, size_t length) { - upb_value val; - return upb_strtable_lookup2(&o->ntof, name, length, &val) ? - upb_value_getptr(val) : NULL; -} - -const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) { - upb_value val; - return upb_inttable_lookup32(&o->itof, num, &val) ? - upb_value_getptr(val) : NULL; -} - -void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) { - upb_inttable_begin(iter, &o->itof); -} +static bool build_filedef( + const symtab_addctx *ctx, upb_filedef *file, + const google_protobuf_FileDescriptorProto *file_proto) { + upb_alloc *alloc = ctx->alloc; + const google_protobuf_FileOptions *file_options_proto; + const google_protobuf_DescriptorProto *const *msgs; + const google_protobuf_EnumDescriptorProto *const *enums; + const google_protobuf_FieldDescriptorProto *const *exts; + const upb_strview* strs; + size_t i, n; + decl_counts counts = {0}; -void upb_oneof_next(upb_oneof_iter *iter) { - upb_inttable_next(iter); -} + count_types_in_file(file_proto, &counts); -bool upb_oneof_done(upb_oneof_iter *iter) { - return upb_inttable_done(iter); -} + file->msgs = upb_malloc(alloc, sizeof(*file->msgs) * counts.msg_count); + file->enums = upb_malloc(alloc, sizeof(*file->enums) * counts.enum_count); + file->exts = upb_malloc(alloc, sizeof(*file->exts) * counts.ext_count); -upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) { - return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter)); -} + CHK_OOM(counts.msg_count == 0 || file->msgs); + CHK_OOM(counts.enum_count == 0 || file->enums); + CHK_OOM(counts.ext_count == 0 || file->exts); -void upb_oneof_iter_setdone(upb_oneof_iter *iter) { - upb_inttable_iter_setdone(iter); -} + /* We increment these as defs are added. */ + file->msg_count = 0; + file->enum_count = 0; + file->ext_count = 0; -/* upb_filedef ****************************************************************/ + if (!google_protobuf_FileDescriptorProto_has_name(file_proto)) { + upb_status_seterrmsg(ctx->status, "File has no name"); + return false; + } -static void visitfiledef(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_filedef *f = (const upb_filedef*)r; - size_t i; + file->name = + strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto)); + file->phpprefix = NULL; + file->phpnamespace = NULL; - for(i = 0; i < upb_filedef_defcount(f); i++) { - visit(r, upb_def_upcast(upb_filedef_def(f, i)), closure); + if (google_protobuf_FileDescriptorProto_has_package(file_proto)) { + upb_strview package = + google_protobuf_FileDescriptorProto_package(file_proto); + CHK(upb_isident(package, true, ctx->status)); + file->package = strviewdup(ctx, package); + } else { + file->package = NULL; } -} -static void freefiledef(upb_refcounted *r) { - upb_filedef *f = (upb_filedef*)r; - size_t i; + if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) { + upb_strview syntax = + google_protobuf_FileDescriptorProto_syntax(file_proto); - for(i = 0; i < upb_filedef_depcount(f); i++) { - upb_filedef_unref(upb_filedef_dep(f, i), f); + if (streql_view(syntax, "proto2")) { + file->syntax = UPB_SYNTAX_PROTO2; + } else if (streql_view(syntax, "proto3")) { + file->syntax = UPB_SYNTAX_PROTO3; + } else { + upb_status_seterrf(ctx->status, "Invalid syntax '%s'", syntax); + return false; + } + } else { + file->syntax = UPB_SYNTAX_PROTO2; } - upb_inttable_uninit(&f->defs); - upb_inttable_uninit(&f->deps); - upb_gfree((void*)f->name); - upb_gfree((void*)f->package); - upb_gfree((void*)f->phpprefix); - upb_gfree((void*)f->phpnamespace); - upb_gfree(f); -} + /* Read options. */ + file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto); + if (file_options_proto) { + if (google_protobuf_FileOptions_has_php_class_prefix(file_options_proto)) { + file->phpprefix = strviewdup( + ctx, + google_protobuf_FileOptions_php_class_prefix(file_options_proto)); + } + if (google_protobuf_FileOptions_has_php_namespace(file_options_proto)) { + file->phpnamespace = strviewdup( + ctx, google_protobuf_FileOptions_php_namespace(file_options_proto)); + } + } -const struct upb_refcounted_vtbl upb_filedef_vtbl = {visitfiledef, freefiledef}; + /* Verify dependencies. */ + strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n); + file->deps = upb_malloc(alloc, sizeof(*file->deps) * n) ; + CHK_OOM(n == 0 || file->deps); -upb_filedef *upb_filedef_new(const void *owner) { - upb_filedef *f = upb_gmalloc(sizeof(*f)); + for (i = 0; i < n; i++) { + upb_strview dep_name = strs[i]; + upb_value v; + if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data, + dep_name.size, &v)) { + upb_status_seterrf(ctx->status, + "Depends on file '" UPB_STRVIEW_FORMAT + "', but it has not been loaded", + UPB_STRVIEW_ARGS(dep_name)); + return false; + } + file->deps[i] = upb_value_getconstptr(v); + } - if (!f) { - return NULL; + /* Create messages. */ + msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_msgdef(ctx, file->package, msgs[i])); } - f->package = NULL; - f->name = NULL; - f->phpprefix = NULL; - f->phpnamespace = NULL; - f->syntax = UPB_SYNTAX_PROTO2; + /* Create enums. */ + enums = google_protobuf_FileDescriptorProto_enum_type(file_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_enumdef(ctx, file->package, enums[i])); + } - if (!upb_refcounted_init(upb_filedef_upcast_mutable(f), &upb_filedef_vtbl, - owner)) { - goto err; + /* Create extensions. */ + exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n); + file->exts = upb_malloc(alloc, sizeof(*file->exts) * n); + CHK_OOM(n == 0 || file->exts); + for (i = 0; i < n; i++) { + CHK(create_fielddef(ctx, file->package, NULL, exts[i])); } - if (!upb_inttable_init(&f->defs, UPB_CTYPE_CONSTPTR)) { - goto err; + /* Now that all names are in the table, resolve references. */ + for (i = 0; i < file->ext_count; i++) { + CHK(resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i])); } - if (!upb_inttable_init(&f->deps, UPB_CTYPE_CONSTPTR)) { - goto err2; + for (i = 0; i < file->msg_count; i++) { + const upb_msgdef *m = &file->msgs[i]; + int j; + for (j = 0; j < m->field_count; j++) { + CHK(resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j])); + } } - return f; + return true; + } + +static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, + upb_status *status) { + const upb_filedef *file = ctx->file; + upb_alloc *alloc = upb_arena_alloc(s->arena); + upb_strtable_iter iter; + CHK_OOM(upb_strtable_insert3(&s->files, file->name, strlen(file->name), + upb_value_constptr(file), alloc)); -err2: - upb_inttable_uninit(&f->defs); + upb_strtable_begin(&iter, ctx->addtab); + for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { + const char *key = upb_strtable_iter_key(&iter); + size_t keylen = upb_strtable_iter_keylength(&iter); + upb_value value = upb_strtable_iter_value(&iter); + CHK_OOM(upb_strtable_insert3(&s->syms, key, keylen, value, alloc)); + } -err: - upb_gfree(f); - return NULL; + return true; } +/* upb_filedef ****************************************************************/ + const char *upb_filedef_name(const upb_filedef *f) { return f->name; } @@ -4425,445 +2713,158 @@ upb_syntax_t upb_filedef_syntax(const upb_filedef *f) { return f->syntax; } -size_t upb_filedef_defcount(const upb_filedef *f) { - return upb_inttable_count(&f->defs); -} - -size_t upb_filedef_depcount(const upb_filedef *f) { - return upb_inttable_count(&f->deps); -} - -const upb_def *upb_filedef_def(const upb_filedef *f, size_t i) { - upb_value v; - - if (upb_inttable_lookup32(&f->defs, i, &v)) { - return upb_value_getconstptr(v); - } else { - return NULL; - } -} - -const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i) { - upb_value v; - - if (upb_inttable_lookup32(&f->deps, i, &v)) { - return upb_value_getconstptr(v); - } else { - return NULL; - } -} - -bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s) { - name = upb_gstrdup(name); - if (!name) { - upb_upberr_setoom(s); - return false; - } - upb_gfree((void*)f->name); - f->name = name; - return true; -} - -bool upb_filedef_setpackage(upb_filedef *f, const char *package, - upb_status *s) { - if (!upb_isident(package, strlen(package), true, s)) return false; - package = upb_gstrdup(package); - if (!package) { - upb_upberr_setoom(s); - return false; - } - upb_gfree((void*)f->package); - f->package = package; - return true; +int upb_filedef_msgcount(const upb_filedef *f) { + return f->msg_count; } -bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix, - upb_status *s) { - phpprefix = upb_gstrdup(phpprefix); - if (!phpprefix) { - upb_upberr_setoom(s); - return false; - } - upb_gfree((void*)f->phpprefix); - f->phpprefix = phpprefix; - return true; +int upb_filedef_depcount(const upb_filedef *f) { + return f->dep_count; } -bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace, - upb_status *s) { - phpnamespace = upb_gstrdup(phpnamespace); - if (!phpnamespace) { - upb_upberr_setoom(s); - return false; - } - upb_gfree((void*)f->phpnamespace); - f->phpnamespace = phpnamespace; - return true; +int upb_filedef_enumcount(const upb_filedef *f) { + return f->enum_count; } -bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax, - upb_status *s) { - UPB_UNUSED(s); - if (syntax != UPB_SYNTAX_PROTO2 && - syntax != UPB_SYNTAX_PROTO3) { - upb_status_seterrmsg(s, "Unknown syntax value."); - return false; - } - f->syntax = syntax; - - { - /* Set all messages in this file to match. */ - size_t i; - for (i = 0; i < upb_filedef_defcount(f); i++) { - /* Casting const away is safe since all defs in mutable filedef must - * also be mutable. */ - upb_def *def = (upb_def*)upb_filedef_def(f, i); - - upb_msgdef *m = upb_dyncast_msgdef_mutable(def); - if (m) { - m->syntax = syntax; - } - } - } - - return true; +const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) { + return i < 0 || i >= f->dep_count ? NULL : f->deps[i]; } -bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor, - upb_status *s) { - if (def->file) { - upb_status_seterrmsg(s, "Def is already part of another filedef."); - return false; - } - - if (upb_inttable_push(&f->defs, upb_value_constptr(def))) { - def->file = f; - upb_ref2(def, f); - upb_ref2(f, def); - if (ref_donor) upb_def_unref(def, ref_donor); - if (def->type == UPB_DEF_MSG) { - upb_downcast_msgdef_mutable(def)->syntax = f->syntax; - } - return true; - } else { - upb_upberr_setoom(s); - return false; - } +const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) { + return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i]; } -bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep) { - if (upb_inttable_push(&f->deps, upb_value_constptr(dep))) { - /* Regular ref instead of ref2 because files can't form cycles. */ - upb_filedef_ref(dep, f); - return true; - } else { - return false; - } +const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) { + return i < 0 || i >= f->enum_count ? NULL : &f->enums[i]; } void upb_symtab_free(upb_symtab *s) { - upb_strtable_iter i; - upb_strtable_begin(&i, &s->symtab); - for (; !upb_strtable_done(&i); upb_strtable_next(&i)) { - const upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i)); - upb_def_unref(def, s); - } - upb_strtable_uninit(&s->symtab); + upb_arena_free(s->arena); upb_gfree(s); } upb_symtab *upb_symtab_new() { upb_symtab *s = upb_gmalloc(sizeof(*s)); + upb_alloc *alloc; + if (!s) { return NULL; } - upb_strtable_init(&s->symtab, UPB_CTYPE_PTR); + s->arena = upb_arena_new(); + alloc = upb_arena_alloc(s->arena); + + if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) || + !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) { + upb_arena_free(s->arena); + upb_gfree(s); + s = NULL; + } return s; } -const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym) { +const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) { upb_value v; - upb_def *ret = upb_strtable_lookup(&s->symtab, sym, &v) ? - upb_value_getptr(v) : NULL; - return ret; + return upb_strtable_lookup(&s->syms, sym, &v) ? + unpack_def(v, UPB_DEFTYPE_MSG) : NULL; } -const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) { +const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym, + size_t len) { upb_value v; - upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ? - upb_value_getptr(v) : NULL; - return def ? upb_dyncast_msgdef(def) : NULL; + return upb_strtable_lookup2(&s->syms, sym, len, &v) ? + unpack_def(v, UPB_DEFTYPE_MSG) : NULL; } const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { upb_value v; - upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ? - upb_value_getptr(v) : NULL; - return def ? upb_dyncast_enumdef(def) : NULL; -} - -/* Given a symbol and the base symbol inside which it is defined, find the - * symbol's definition in t. */ -static upb_def *upb_resolvename(const upb_strtable *t, - const char *base, const char *sym) { - if(strlen(sym) == 0) return NULL; - if(sym[0] == '.') { - /* Symbols starting with '.' are absolute, so we do a single lookup. - * Slice to omit the leading '.' */ - upb_value v; - return upb_strtable_lookup(t, sym + 1, &v) ? upb_value_getptr(v) : NULL; - } else { - /* Remove components from base until we find an entry or run out. - * TODO: This branch is totally broken, but currently not used. */ - (void)base; - UPB_ASSERT(false); - return NULL; - } + return upb_strtable_lookup(&s->syms, sym, &v) ? + unpack_def(v, UPB_DEFTYPE_ENUM) : NULL; } -const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, - const char *sym) { - upb_def *ret = upb_resolvename(&s->symtab, base, sym); - return ret; +const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name) { + upb_value v; + return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v) + : NULL; } -/* TODO(haberman): we need a lot more testing of error conditions. */ -static bool symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, - void *ref_donor, upb_refcounted *freeze_also, - upb_status *status) { - size_t i; - size_t add_n; - size_t freeze_n; - upb_strtable_iter iter; - upb_refcounted **add_objs = NULL; - upb_def **add_defs = NULL; - size_t add_objs_size; +const upb_filedef *upb_symtab_addfile( + upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto, + upb_status *status) { + upb_arena *tmparena = upb_arena_new(); upb_strtable addtab; + upb_alloc *alloc = upb_arena_alloc(s->arena); + upb_filedef *file = upb_malloc(alloc, sizeof(*file)); + bool ok; + symtab_addctx ctx; - if (n == 0 && !freeze_also) { - return true; - } + ctx.file = file; + ctx.symtab = s; + ctx.alloc = alloc; + ctx.tmp = upb_arena_alloc(tmparena); + ctx.addtab = &addtab; + ctx.status = status; - if (!upb_strtable_init(&addtab, UPB_CTYPE_PTR)) { - upb_status_seterrmsg(status, "out of memory"); - return false; - } + ok = file && + upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) && + build_filedef(&ctx, file, file_proto) && + upb_symtab_addtotabs(s, &ctx, status); - /* Add new defs to our "add" set. */ - for (i = 0; i < n; i++) { - upb_def *def = defs[i]; - const char *fullname; - upb_fielddef *f; + upb_arena_free(tmparena); + return ok ? file : NULL; +} - if (upb_def_isfrozen(def)) { - upb_status_seterrmsg(status, "added defs must be mutable"); - goto err; - } - UPB_ASSERT(!upb_def_isfrozen(def)); - fullname = upb_def_fullname(def); - if (!fullname) { - upb_status_seterrmsg( - status, "Anonymous defs cannot be added to a symtab"); - goto err; - } +/* Include here since we want most of this file to be stdio-free. */ +#include - f = upb_dyncast_fielddef_mutable(def); +bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) { + /* 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_def_init **deps = init->deps; + google_protobuf_FileDescriptorProto *file; + upb_arena *arena; + upb_status status; - if (f) { - if (!upb_fielddef_containingtypename(f)) { - upb_status_seterrmsg(status, - "Standalone fielddefs must have a containing type " - "(extendee) name set"); - goto err; - } - } else { - if (upb_strtable_lookup(&addtab, fullname, NULL)) { - upb_status_seterrf(status, "Conflicting defs named '%s'", fullname); - goto err; - } - if (upb_strtable_lookup(&s->symtab, fullname, NULL)) { - upb_status_seterrf(status, "Symtab already has a def named '%s'", - fullname); - goto err; - } - if (!upb_strtable_insert(&addtab, fullname, upb_value_ptr(def))) - goto oom_err; - upb_def_donateref(def, ref_donor, s); - } + upb_status_clear(&status); - if (upb_dyncast_fielddef_mutable(def)) { - /* TODO(haberman): allow adding extensions attached to files. */ - upb_status_seterrf(status, "Can't add extensions to symtab.\n"); - goto err; - } + if (upb_strtable_lookup(&s->files, init->filename, NULL)) { + return true; } - /* Now using the table, resolve symbolic references for subdefs. */ - upb_strtable_begin(&iter, &addtab); - for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { - const char *base; - upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter)); - upb_msgdef *m = upb_dyncast_msgdef_mutable(def); - upb_msg_field_iter j; - - if (!m) continue; - /* Type names are resolved relative to the message in which they appear. */ - base = upb_msgdef_fullname(m); - - for(upb_msg_field_begin(&j, m); - !upb_msg_field_done(&j); - upb_msg_field_next(&j)) { - upb_fielddef *f = upb_msg_iter_field(&j); - const char *name = upb_fielddef_subdefname(f); - if (name && !upb_fielddef_subdef(f)) { - /* Try the lookup in the current set of to-be-added defs first. If not - * there, try existing defs. */ - upb_def *subdef = upb_resolvename(&addtab, base, name); - if (subdef == NULL) { - subdef = upb_resolvename(&s->symtab, base, name); - } - if (subdef == NULL) { - upb_status_seterrf( - status, "couldn't resolve name '%s' in message '%s'", name, base); - goto err; - } else if (!upb_fielddef_setsubdef(f, subdef, status)) { - goto err; - } - } - } - } + arena = upb_arena_new(); - /* We need an array of the defs in addtab, for passing to - * upb_refcounted_freeze(). */ - add_objs_size = upb_strtable_count(&addtab); - if (freeze_also) { - add_objs_size++; + for (; *deps; deps++) { + if (!_upb_symtab_loaddefinit(s, *deps)) goto err; } - add_defs = upb_gmalloc(sizeof(void*) * add_objs_size); - if (add_defs == NULL) goto oom_err; - upb_strtable_begin(&iter, &addtab); - for (add_n = 0; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { - add_defs[add_n++] = upb_value_getptr(upb_strtable_iter_value(&iter)); - } + file = google_protobuf_FileDescriptorProto_parse( + init->descriptor.data, init->descriptor.size, arena); - /* Validate defs. */ - if (!_upb_def_validate(add_defs, add_n, status)) { + if (!file) { + upb_status_seterrf( + &status, + "Failed to parse compiled-in descriptor for file '%s'. This should " + "never happen.", + init->filename); goto err; } - /* Cheat a little and give the array a new type. - * This is probably undefined behavior, but this code will be deleted soon. */ - add_objs = (upb_refcounted**)add_defs; + if (!upb_symtab_addfile(s, file, &status)) goto err; - freeze_n = add_n; - if (freeze_also) { - add_objs[freeze_n++] = freeze_also; - } - - if (!upb_refcounted_freeze(add_objs, freeze_n, status, - UPB_MAX_MESSAGE_DEPTH * 2)) { - goto err; - } - - /* This must be delayed until all errors have been detected, since error - * recovery code uses this table to cleanup defs. */ - upb_strtable_uninit(&addtab); - - /* TODO(haberman) we don't properly handle errors after this point (like - * OOM in upb_strtable_insert() below). */ - for (i = 0; i < add_n; i++) { - upb_def *def = (upb_def*)add_objs[i]; - const char *name = upb_def_fullname(def); - bool success; - success = upb_strtable_insert(&s->symtab, name, upb_value_ptr(def)); - UPB_ASSERT(success); - } - upb_gfree(add_defs); + upb_arena_free(arena); return true; -oom_err: - upb_status_seterrmsg(status, "out of memory"); -err: { - /* We need to donate the refs back. */ - upb_strtable_begin(&iter, &addtab); - for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { - upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter)); - upb_def_donateref(def, s, ref_donor); - } - } - upb_strtable_uninit(&addtab); - upb_gfree(add_defs); - UPB_ASSERT(!upb_ok(status)); +err: + fprintf(stderr, "Error loading compiled-in descriptor: %s\n", + upb_status_errmsg(&status)); + upb_arena_free(arena); return false; } -bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, - void *ref_donor, upb_status *status) { - return symtab_add(s, defs, n, ref_donor, NULL, status); -} - -bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status *status) { - size_t n; - size_t i; - upb_def **defs; - bool ret; - - n = upb_filedef_defcount(file); - if (n == 0) { - return true; - } - defs = upb_gmalloc(sizeof(*defs) * n); - - if (defs == NULL) { - upb_status_seterrmsg(status, "Out of memory"); - return false; - } - - for (i = 0; i < n; i++) { - defs[i] = upb_filedef_mutabledef(file, i); - } - - ret = symtab_add(s, defs, n, NULL, upb_filedef_upcast_mutable(file), status); - - upb_gfree(defs); - return ret; -} - -/* Iteration. */ - -static void advance_to_matching(upb_symtab_iter *iter) { - if (iter->type == UPB_DEF_ANY) - return; - - while (!upb_strtable_done(&iter->iter) && - iter->type != upb_symtab_iter_def(iter)->type) { - upb_strtable_next(&iter->iter); - } -} - -void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s, - upb_deftype_t type) { - upb_strtable_begin(&iter->iter, &s->symtab); - iter->type = type; - advance_to_matching(iter); -} - -void upb_symtab_next(upb_symtab_iter *iter) { - upb_strtable_next(&iter->iter); - advance_to_matching(iter); -} - -bool upb_symtab_done(const upb_symtab_iter *iter) { - return upb_strtable_done(&iter->iter); -} - -const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter) { - return upb_value_getptr(upb_strtable_iter_value(&iter->iter)); -} +#undef CHK +#undef CHK_OOM /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */ +#include #define UPB_PB_VARINT_MAX_LEN 10 #define CHK(x) do { if (!(x)) { return false; } } while(0) @@ -4908,7 +2909,7 @@ static uint32_t upb_zzencode_32(int32_t n) { return (n << 1) ^ (n >> 31); } static uint64_t upb_zzencode_64(int64_t n) { return (n << 1) ^ (n >> 63); } typedef struct { - upb_env *env; + upb_alloc *alloc; char *buf, *ptr, *limit; } upb_encstate; @@ -4923,7 +2924,7 @@ static size_t upb_roundup_pow2(size_t bytes) { static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) { size_t old_size = e->limit - e->buf; size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr)); - char *new_buf = upb_env_realloc(e->env, e->buf, old_size, new_size); + char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size); CHK(new_buf); /* We want previous data at the end, realloc() put it at the beginning. */ @@ -4987,17 +2988,17 @@ static bool upb_put_float(upb_encstate *e, float d) { return upb_put_fixed32(e, u32); } -static uint32_t upb_readcase(const char *msg, const upb_msglayout_msginit_v1 *m, - int oneof_index) { +static uint32_t upb_readcase(const char *msg, const upb_msglayout_field *f) { uint32_t ret; - memcpy(&ret, msg + m->oneofs[oneof_index].case_offset, sizeof(ret)); + uint32_t offset = ~f->presence; + memcpy(&ret, msg + offset, sizeof(ret)); return ret; } -static bool upb_readhasbit(const char *msg, - const upb_msglayout_fieldinit_v1 *f) { - UPB_ASSERT(f->hasbit != UPB_NO_HASBIT); - return msg[f->hasbit / 8] & (1 << (f->hasbit % 8)); +static bool upb_readhasbit(const char *msg, const upb_msglayout_field *f) { + uint32_t hasbit = f->presence; + UPB_ASSERT(f->presence > 0); + return msg[hasbit / 8] & (1 << (hasbit % 8)); } static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) { @@ -5011,12 +3012,11 @@ static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr, } bool upb_encode_message(upb_encstate *e, const char *msg, - const upb_msglayout_msginit_v1 *m, - size_t *size); + const upb_msglayout *m, size_t *size); static bool upb_encode_array(upb_encstate *e, const char *field_mem, - const upb_msglayout_msginit_v1 *m, - const upb_msglayout_fieldinit_v1 *f) { + const upb_msglayout *m, + const upb_msglayout_field *f) { const upb_array *arr = *(const upb_array**)field_mem; if (arr == NULL || arr->len == 0) { @@ -5069,8 +3069,8 @@ do { ; } while(0) VARINT_CASE(int64_t, upb_zzencode_64(*ptr)); case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_BYTES: { - upb_stringview *start = arr->data; - upb_stringview *ptr = start + arr->len; + upb_strview *start = arr->data; + upb_strview *ptr = start + arr->len; do { ptr--; CHK(upb_put_bytes(e, ptr->data, ptr->size) && @@ -5082,7 +3082,7 @@ do { ; } while(0) case UPB_DESCRIPTOR_TYPE_GROUP: { void **start = arr->data; void **ptr = start + arr->len; - const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index]; + const upb_msglayout *subm = m->submsgs[f->submsg_index]; do { size_t size; ptr--; @@ -5095,7 +3095,7 @@ do { ; } while(0) case UPB_DESCRIPTOR_TYPE_MESSAGE: { void **start = arr->data; void **ptr = start + arr->len; - const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index]; + const upb_msglayout *subm = m->submsgs[f->submsg_index]; do { size_t size; ptr--; @@ -5115,11 +3115,9 @@ do { ; } while(0) } static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, - const upb_msglayout_msginit_v1 *m, - const upb_msglayout_fieldinit_v1 *f, - bool is_proto3) { - bool skip_zero_value = is_proto3 && f->oneof_index == UPB_NOT_IN_ONEOF; - + const upb_msglayout *m, + const upb_msglayout_field *f, + bool skip_zero_value) { #define CASE(ctype, type, wire_type, encodeval) do { \ ctype val = *(ctype*)field_mem; \ if (skip_zero_value && val == 0) { \ @@ -5156,7 +3154,7 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val)); case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_BYTES: { - upb_stringview view = *(upb_stringview*)field_mem; + upb_strview view = *(upb_strview*)field_mem; if (skip_zero_value && view.size == 0) { return true; } @@ -5166,9 +3164,9 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, } case UPB_DESCRIPTOR_TYPE_GROUP: { size_t size; - void *submsg = *(void**)field_mem; - const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index]; - if (skip_zero_value && submsg == NULL) { + void *submsg = *(void **)field_mem; + const upb_msglayout *subm = m->submsgs[f->submsg_index]; + if (submsg == NULL) { return true; } return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) && @@ -5177,9 +3175,9 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, } case UPB_DESCRIPTOR_TYPE_MESSAGE: { size_t size; - void *submsg = *(void**)field_mem; - const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index]; - if (skip_zero_value && submsg == NULL) { + void *submsg = *(void **)field_mem; + const upb_msglayout *subm = m->submsgs[f->submsg_index]; + if (submsg == NULL) { return true; } return upb_encode_message(e, submsg, subm, &size) && @@ -5191,55 +3189,52 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, UPB_UNREACHABLE(); } -bool upb_encode_hasscalarfield(const char *msg, - const upb_msglayout_msginit_v1 *m, - const upb_msglayout_fieldinit_v1 *f) { - if (f->oneof_index != UPB_NOT_IN_ONEOF) { - return upb_readcase(msg, m, f->oneof_index) == f->number; - } else if (m->is_proto2) { - return upb_readhasbit(msg, f); - } else { - /* For proto3, we'll test for the field being empty later. */ - return true; - } -} - -bool upb_encode_message(upb_encstate* e, const char *msg, - const upb_msglayout_msginit_v1 *m, - size_t *size) { +bool upb_encode_message(upb_encstate *e, const char *msg, + const upb_msglayout *m, size_t *size) { int i; size_t pre_len = e->limit - e->ptr; - - if (msg == NULL) { - return true; - } + const char *unknown; + size_t unknown_size; for (i = m->field_count - 1; i >= 0; i--) { - const upb_msglayout_fieldinit_v1 *f = &m->fields[i]; + const upb_msglayout_field *f = &m->fields[i]; if (f->label == UPB_LABEL_REPEATED) { CHK(upb_encode_array(e, msg + f->offset, m, f)); } else { - if (upb_encode_hasscalarfield(msg, m, f)) { - if (f->oneof_index == UPB_NOT_IN_ONEOF) { - CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, !m->is_proto2)); - } else { - const upb_msglayout_oneofinit_v1 *o = &m->oneofs[f->oneof_index]; - CHK(upb_encode_scalarfield(e, msg + o->data_offset, - m, f, !m->is_proto2)); + bool skip_empty = false; + if (f->presence == 0) { + /* Proto3 presence. */ + skip_empty = true; + } else if (f->presence > 0) { + /* Proto2 presence: hasbit. */ + if (!upb_readhasbit(msg, f)) { + continue; + } + } else { + /* Field is in a oneof. */ + if (upb_readcase(msg, f) != f->number) { + continue; } } + CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty)); } } + unknown = upb_msg_getunknown(msg, &unknown_size); + + if (unknown) { + upb_put_bytes(e, unknown, unknown_size); + } + *size = (e->limit - e->ptr) - pre_len; return true; } -char *upb_encode(const void *msg, const upb_msglayout_msginit_v1 *m, - upb_env *env, size_t *size) { +char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena, + size_t *size) { upb_encstate e; - e.env = env; + e.alloc = upb_arena_alloc(arena); e.buf = NULL; e.limit = NULL; e.ptr = NULL; @@ -5270,8 +3265,17 @@ char *upb_encode(const void *msg, const upb_msglayout_msginit_v1 *m, #include -static void *upb_calloc(size_t size) { - void *mem = upb_gmalloc(size); + +struct upb_handlers { + upb_handlercache *cache; + const upb_msgdef *msg; + const upb_handlers **sub; + const void *top_closure_type; + upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */ +}; + +static void *upb_calloc(upb_arena *arena, size_t size) { + void *mem = upb_malloc(upb_arena_alloc(arena), size); if (mem) { memset(mem, 0, size); } @@ -5282,111 +3286,23 @@ static void *upb_calloc(size_t size) { * UPB_NO_CLOSURE. */ char _upb_noclosure; -static void freehandlers(upb_refcounted *r) { - upb_handlers *h = (upb_handlers*)r; - - upb_inttable_iter i; - upb_inttable_begin(&i, &h->cleanup_); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - void *val = (void*)upb_inttable_iter_key(&i); - upb_value func_val = upb_inttable_iter_value(&i); - upb_handlerfree *func = upb_value_getfptr(func_val); - func(val); - } - - upb_inttable_uninit(&h->cleanup_); - upb_msgdef_unref(h->msg, h); - upb_gfree(h->sub); - upb_gfree(h); -} - -static void visithandlers(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_handlers *h = (const upb_handlers*)r; - upb_msg_field_iter i; - for(upb_msg_field_begin(&i, h->msg); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - const upb_handlers *sub; - if (!upb_fielddef_issubmsg(f)) continue; - sub = upb_handlers_getsubhandlers(h, f); - if (sub) visit(r, upb_handlers_upcast(sub), closure); - } -} - -static const struct upb_refcounted_vtbl vtbl = {visithandlers, freehandlers}; - -typedef struct { - upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ - upb_handlers_callback *callback; - const void *closure; -} dfs_state; - -/* TODO(haberman): discard upb_handlers* objects that do not actually have any - * handlers set and cannot reach any upb_handlers* object that does. This is - * slightly tricky to do correctly. */ -static upb_handlers *newformsg(const upb_msgdef *m, const void *owner, - dfs_state *s) { - upb_msg_field_iter i; - upb_handlers *h = upb_handlers_new(m, owner); - if (!h) return NULL; - if (!upb_inttable_insertptr(&s->tab, m, upb_value_ptr(h))) goto oom; - - s->callback(s->closure, h); - - /* For each submessage field, get or create a handlers object and set it as - * the subhandlers. */ - for(upb_msg_field_begin(&i, m); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - const upb_msgdef *subdef; - upb_value subm_ent; - - if (!upb_fielddef_issubmsg(f)) continue; - - subdef = upb_downcast_msgdef(upb_fielddef_subdef(f)); - if (upb_inttable_lookupptr(&s->tab, subdef, &subm_ent)) { - upb_handlers_setsubhandlers(h, f, upb_value_getptr(subm_ent)); - } else { - upb_handlers *sub_mh = newformsg(subdef, &sub_mh, s); - if (!sub_mh) goto oom; - upb_handlers_setsubhandlers(h, f, sub_mh); - upb_handlers_unref(sub_mh, &sub_mh); - } - } - return h; - -oom: - upb_handlers_unref(h, owner); - return NULL; -} - /* Given a selector for a STARTSUBMSG handler, resolves to a pointer to the * subhandlers for this submessage field. */ #define SUBH(h, selector) (h->sub[selector]) /* The selector for a submessage field is the field index. */ -#define SUBH_F(h, f) SUBH(h, f->index_) +#define SUBH_F(h, f) SUBH(h, upb_fielddef_index(f)) static int32_t trygetsel(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type) { upb_selector_t sel; - UPB_ASSERT(!upb_handlers_isfrozen(h)); - if (upb_handlers_msgdef(h) != upb_fielddef_containingtype(f)) { - upb_status_seterrf( - &h->status_, "type mismatch: field %s does not belong to message %s", - upb_fielddef_name(f), upb_msgdef_fullname(upb_handlers_msgdef(h))); - return -1; - } - if (!upb_handlers_getselector(f, type, &sel)) { - upb_status_seterrf( - &h->status_, - "type mismatch: cannot register handler type %d for field %s", - type, upb_fielddef_name(f)); - return -1; - } + bool ok; + + ok = upb_handlers_getselector(f, type, &sel); + + UPB_ASSERT(upb_handlers_msgdef(h) == upb_fielddef_containingtype(f)); + UPB_ASSERT(ok); + return sel; } @@ -5399,29 +3315,17 @@ static upb_selector_t handlers_getsel(upb_handlers *h, const upb_fielddef *f, static const void **returntype(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type) { - return &h->table[handlers_getsel(h, f, type)].attr.return_closure_type_; + return &h->table[handlers_getsel(h, f, type)].attr.return_closure_type; } static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, upb_handlertype_t type, upb_func *func, - upb_handlerattr *attr) { - upb_handlerattr set_attr = UPB_HANDLERATTR_INITIALIZER; + const upb_handlerattr *attr) { + upb_handlerattr set_attr = UPB_HANDLERATTR_INIT; const void *closure_type; const void **context_closure_type; - UPB_ASSERT(!upb_handlers_isfrozen(h)); - - if (sel < 0) { - upb_status_seterrmsg(&h->status_, - "incorrect handler type for this field."); - return false; - } - - if (h->table[sel].func) { - upb_status_seterrmsg(&h->status_, - "cannot change handler once it has been set."); - return false; - } + UPB_ASSERT(!h->table[sel].func); if (attr) { set_attr = *attr; @@ -5429,7 +3333,7 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, /* Check that the given closure type matches the closure type that has been * established for this context (if any). */ - closure_type = upb_handlerattr_closuretype(&set_attr); + closure_type = set_attr.closure_type; if (type == UPB_HANDLER_STRING) { context_closure_type = returntype(h, f, UPB_HANDLER_STARTSTR); @@ -5443,15 +3347,6 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, if (closure_type && *context_closure_type && closure_type != *context_closure_type) { - /* TODO(haberman): better message for debugging. */ - if (f) { - upb_status_seterrf(&h->status_, - "closure type does not match for field %s", - upb_fielddef_name(f)); - } else { - upb_status_seterrmsg( - &h->status_, "closure type does not match for message-level handler"); - } return false; } @@ -5461,16 +3356,15 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, /* If this is a STARTSEQ or STARTSTR handler, check that the returned pointer * matches any pre-existing expectations about what type is expected. */ if (type == UPB_HANDLER_STARTSEQ || type == UPB_HANDLER_STARTSTR) { - const void *return_type = upb_handlerattr_returnclosuretype(&set_attr); - const void *table_return_type = - upb_handlerattr_returnclosuretype(&h->table[sel].attr); + const void *return_type = set_attr.return_closure_type; + const void *table_return_type = h->table[sel].attr.return_closure_type; if (return_type && table_return_type && return_type != table_return_type) { - upb_status_seterrmsg(&h->status_, "closure return type does not match"); return false; } - if (table_return_type && !return_type) - upb_handlerattr_setreturnclosuretype(&set_attr, table_return_type); + if (table_return_type && !return_type) { + set_attr.return_closure_type = table_return_type; + } } h->table[sel].func = (upb_func*)func; @@ -5496,18 +3390,18 @@ const void *effective_closure_type(upb_handlers *h, const upb_fielddef *f, type != UPB_HANDLER_STARTSEQ && type != UPB_HANDLER_ENDSEQ && h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSEQ)].func) { - ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); + ret = h->table[sel].attr.return_closure_type; } if (type == UPB_HANDLER_STRING && h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSTR)].func) { - ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); + ret = h->table[sel].attr.return_closure_type; } /* The effective type of the submessage; not used yet. * if (type == SUBMESSAGE && * h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSUBMSG)].func) { - * ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); + * ret = h->table[sel].attr.return_closure_type; * } */ return ret; @@ -5527,92 +3421,47 @@ bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type, if (h->table[sel].func) return true; closure_type = effective_closure_type(h, f, type); attr = &h->table[sel].attr; - return_closure_type = upb_handlerattr_returnclosuretype(attr); + return_closure_type = attr->return_closure_type; if (closure_type && return_closure_type && closure_type != return_closure_type) { - upb_status_seterrf(status, - "expected start handler to return sub type for field %f", - upb_fielddef_name(f)); return false; } return true; } -/* Public interface ***********************************************************/ - -upb_handlers *upb_handlers_new(const upb_msgdef *md, const void *owner) { +static upb_handlers *upb_handlers_new(const upb_msgdef *md, + upb_handlercache *cache, + upb_arena *arena) { int extra; upb_handlers *h; - UPB_ASSERT(upb_msgdef_isfrozen(md)); - - extra = sizeof(upb_handlers_tabent) * (md->selector_count - 1); - h = upb_calloc(sizeof(*h) + extra); + extra = sizeof(upb_handlers_tabent) * (upb_msgdef_selectorcount(md) - 1); + h = upb_calloc(arena, sizeof(*h) + extra); if (!h) return NULL; + h->cache = cache; h->msg = md; - upb_msgdef_ref(h->msg, h); - upb_status_clear(&h->status_); - if (md->submsg_field_count > 0) { - h->sub = upb_calloc(md->submsg_field_count * sizeof(*h->sub)); - if (!h->sub) goto oom; + if (upb_msgdef_submsgfieldcount(md) > 0) { + size_t bytes = upb_msgdef_submsgfieldcount(md) * sizeof(*h->sub); + h->sub = upb_calloc(arena, bytes); + if (!h->sub) return NULL; } else { h->sub = 0; } - if (!upb_refcounted_init(upb_handlers_upcast_mutable(h), &vtbl, owner)) - goto oom; - if (!upb_inttable_init(&h->cleanup_, UPB_CTYPE_FPTR)) goto oom; - /* calloc() above initialized all handlers to NULL. */ return h; - -oom: - freehandlers(upb_handlers_upcast_mutable(h)); - return NULL; -} - -const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m, - const void *owner, - upb_handlers_callback *callback, - const void *closure) { - dfs_state state; - upb_handlers *ret; - bool ok; - upb_refcounted *r; - - state.callback = callback; - state.closure = closure; - if (!upb_inttable_init(&state.tab, UPB_CTYPE_PTR)) return NULL; - - ret = newformsg(m, owner, &state); - - upb_inttable_uninit(&state.tab); - if (!ret) return NULL; - - r = upb_handlers_upcast_mutable(ret); - ok = upb_refcounted_freeze(&r, 1, NULL, UPB_MAX_HANDLER_DEPTH); - UPB_ASSERT(ok); - - return ret; -} - -const upb_status *upb_handlers_status(upb_handlers *h) { - UPB_ASSERT(!upb_handlers_isfrozen(h)); - return &h->status_; } -void upb_handlers_clearerr(upb_handlers *h) { - UPB_ASSERT(!upb_handlers_isfrozen(h)); - upb_status_clear(&h->status_); -} +/* Public interface ***********************************************************/ -#define SETTER(name, handlerctype, handlertype) \ - bool upb_handlers_set ## name(upb_handlers *h, const upb_fielddef *f, \ - handlerctype func, upb_handlerattr *attr) { \ - int32_t sel = trygetsel(h, f, handlertype); \ - return doset(h, sel, f, handlertype, (upb_func*)func, attr); \ +#define SETTER(name, handlerctype, handlertype) \ + bool upb_handlers_set##name(upb_handlers *h, const upb_fielddef *f, \ + handlerctype func, \ + const upb_handlerattr *attr) { \ + int32_t sel = trygetsel(h, f, handlertype); \ + return doset(h, sel, f, handlertype, (upb_func *)func, attr); \ } SETTER(int32, upb_int32_handlerfunc*, UPB_HANDLER_INT32) @@ -5633,20 +3482,19 @@ SETTER(endseq, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSEQ) #undef SETTER bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func, - upb_handlerattr *attr) { + const upb_handlerattr *attr) { return doset(h, UPB_UNKNOWN_SELECTOR, NULL, UPB_HANDLER_INT32, (upb_func *)func, attr); } bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func, - upb_handlerattr *attr) { + const upb_handlerattr *attr) { return doset(h, UPB_STARTMSG_SELECTOR, NULL, UPB_HANDLER_INT32, (upb_func *)func, attr); } bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, - upb_handlerattr *attr) { - UPB_ASSERT(!upb_handlers_isfrozen(h)); + const upb_handlerattr *attr) { return doset(h, UPB_ENDMSG_SELECTOR, NULL, UPB_HANDLER_INT32, (upb_func *)func, attr); } @@ -5654,14 +3502,12 @@ bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f, const upb_handlers *sub) { UPB_ASSERT(sub); - UPB_ASSERT(!upb_handlers_isfrozen(h)); UPB_ASSERT(upb_fielddef_issubmsg(f)); if (SUBH_F(h, f)) return false; /* Can't reset. */ - if (upb_msgdef_upcast(upb_handlers_msgdef(sub)) != upb_fielddef_subdef(f)) { + if (upb_handlers_msgdef(sub) != upb_fielddef_msgsubdef(f)) { return false; } SUBH_F(h, f) = sub; - upb_ref2(sub, h); return true; } @@ -5671,9 +3517,18 @@ const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h, return SUBH_F(h, f); } +upb_func *upb_handlers_gethandler(const upb_handlers *h, upb_selector_t s, + const void **handler_data) { + upb_func *ret = (upb_func *)h->table[s].func; + if (ret && handler_data) { + *handler_data = h->table[s].attr.handler_data; + } + return ret; +} + bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t sel, upb_handlerattr *attr) { - if (!upb_handlers_gethandler(h, sel)) + if (!upb_handlers_gethandler(h, sel, NULL)) return false; *attr = h->table[sel].attr; return true; @@ -5688,100 +3543,7 @@ const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; } bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { - bool ok; - if (upb_inttable_lookupptr(&h->cleanup_, p, NULL)) { - return false; - } - ok = upb_inttable_insertptr(&h->cleanup_, p, upb_value_fptr(func)); - UPB_ASSERT(ok); - return true; -} - - -/* "Static" methods ***********************************************************/ - -bool upb_handlers_freeze(upb_handlers *const*handlers, int n, upb_status *s) { - /* TODO: verify we have a transitive closure. */ - int i; - for (i = 0; i < n; i++) { - upb_msg_field_iter j; - upb_handlers *h = handlers[i]; - - if (!upb_ok(&h->status_)) { - upb_status_seterrf(s, "handlers for message %s had error status: %s", - upb_msgdef_fullname(upb_handlers_msgdef(h)), - upb_status_errmsg(&h->status_)); - return false; - } - - /* Check that there are no closure mismatches due to missing Start* handlers - * or subhandlers with different type-level types. */ - for(upb_msg_field_begin(&j, h->msg); - !upb_msg_field_done(&j); - upb_msg_field_next(&j)) { - - const upb_fielddef *f = upb_msg_iter_field(&j); - if (upb_fielddef_isseq(f)) { - if (!checkstart(h, f, UPB_HANDLER_STARTSEQ, s)) - return false; - } - - if (upb_fielddef_isstring(f)) { - if (!checkstart(h, f, UPB_HANDLER_STARTSTR, s)) - return false; - } - - if (upb_fielddef_issubmsg(f)) { - bool hashandler = false; - if (upb_handlers_gethandler( - h, handlers_getsel(h, f, UPB_HANDLER_STARTSUBMSG)) || - upb_handlers_gethandler( - h, handlers_getsel(h, f, UPB_HANDLER_ENDSUBMSG))) { - hashandler = true; - } - - if (upb_fielddef_isseq(f) && - (upb_handlers_gethandler( - h, handlers_getsel(h, f, UPB_HANDLER_STARTSEQ)) || - upb_handlers_gethandler( - h, handlers_getsel(h, f, UPB_HANDLER_ENDSEQ)))) { - hashandler = true; - } - - if (hashandler && !upb_handlers_getsubhandlers(h, f)) { - /* For now we add an empty subhandlers in this case. It makes the - * decoder code generator simpler, because it only has to handle two - * cases (submessage has handlers or not) as opposed to three - * (submessage has handlers in enclosing message but no subhandlers). - * - * This makes parsing less efficient in the case that we want to - * notice a submessage but skip its contents (like if we're testing - * for submessage presence or counting the number of repeated - * submessages). In this case we will end up parsing the submessage - * field by field and throwing away the results for each, instead of - * skipping the whole delimited thing at once. If this is an issue we - * can revisit it, but do remember that this only arises when you have - * handlers (startseq/startsubmsg/endsubmsg/endseq) set for the - * submessage but no subhandlers. The uses cases for this are - * limited. */ - upb_handlers *sub = upb_handlers_new(upb_fielddef_msgsubdef(f), &sub); - upb_handlers_setsubhandlers(h, f, sub); - upb_handlers_unref(sub, &sub); - } - - /* TODO(haberman): check type of submessage. - * This is slightly tricky; also consider whether we should check that - * they match at setsubhandlers time. */ - } - } - } - - if (!upb_refcounted_freeze((upb_refcounted*const*)handlers, n, s, - UPB_MAX_HANDLER_DEPTH)) { - return false; - } - - return true; + return upb_handlercache_addcleanup(h->cache, p, func); } upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) { @@ -5800,6 +3562,7 @@ upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) { bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, upb_selector_t *s) { + uint32_t selector_base = upb_fielddef_selectorbase(f); switch (type) { case UPB_HANDLER_INT32: case UPB_HANDLER_INT64: @@ -5811,38 +3574,38 @@ bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, if (!upb_fielddef_isprimitive(f) || upb_handlers_getprimitivehandlertype(f) != type) return false; - *s = f->selector_base; + *s = selector_base; break; case UPB_HANDLER_STRING: if (upb_fielddef_isstring(f)) { - *s = f->selector_base; + *s = selector_base; } else if (upb_fielddef_lazy(f)) { - *s = f->selector_base + 3; + *s = selector_base + 3; } else { return false; } break; case UPB_HANDLER_STARTSTR: if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) { - *s = f->selector_base + 1; + *s = selector_base + 1; } else { return false; } break; case UPB_HANDLER_ENDSTR: if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) { - *s = f->selector_base + 2; + *s = selector_base + 2; } else { return false; } break; case UPB_HANDLER_STARTSEQ: if (!upb_fielddef_isseq(f)) return false; - *s = f->selector_base - 2; + *s = selector_base - 2; break; case UPB_HANDLER_ENDSEQ: if (!upb_fielddef_isseq(f)) return false; - *s = f->selector_base - 1; + *s = selector_base - 1; break; case UPB_HANDLER_STARTSUBMSG: if (!upb_fielddef_issubmsg(f)) return false; @@ -5850,14 +3613,14 @@ bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, * selector can also be used as an index into the "sub" array of * subhandlers. The indexes for the two into these two tables are the * same, except that in the handler table the static selectors come first. */ - *s = f->index_ + UPB_STATIC_SELECTOR_COUNT; + *s = upb_fielddef_index(f) + UPB_STATIC_SELECTOR_COUNT; break; case UPB_HANDLER_ENDSUBMSG: if (!upb_fielddef_issubmsg(f)) return false; - *s = f->selector_base; + *s = selector_base; break; } - UPB_ASSERT((size_t)*s < upb_fielddef_containingtype(f)->selector_count); + UPB_ASSERT((size_t)*s < upb_msgdef_selectorcount(upb_fielddef_containingtype(f))); return true; } @@ -5880,103 +3643,204 @@ uint32_t upb_handlers_selectorcount(const upb_fielddef *f) { return ret; } +/* upb_handlercache ***********************************************************/ -/* upb_handlerattr ************************************************************/ +struct upb_handlercache { + upb_arena *arena; + upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ + upb_handlers_callback *callback; + const void *closure; +}; -void upb_handlerattr_init(upb_handlerattr *attr) { - upb_handlerattr from = UPB_HANDLERATTR_INITIALIZER; - memcpy(attr, &from, sizeof(*attr)); -} +const upb_handlers *upb_handlercache_get(upb_handlercache *c, + const upb_msgdef *md) { + upb_msg_field_iter i; + upb_value v; + upb_handlers *h; -void upb_handlerattr_uninit(upb_handlerattr *attr) { - UPB_UNUSED(attr); -} + if (upb_inttable_lookupptr(&c->tab, md, &v)) { + return upb_value_getptr(v); + } -bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd) { - attr->handler_data_ = hd; - return true; -} + h = upb_handlers_new(md, c, c->arena); + v = upb_value_ptr(h); -bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type) { - attr->closure_type_ = type; - return true; -} + if (!h) return NULL; + if (!upb_inttable_insertptr(&c->tab, md, v)) return NULL; -const void *upb_handlerattr_closuretype(const upb_handlerattr *attr) { - return attr->closure_type_; -} + c->callback(c->closure, h); -bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr, - const void *type) { - attr->return_closure_type_ = type; - return true; -} + /* For each submessage field, get or create a handlers object and set it as + * the subhandlers. */ + for(upb_msg_field_begin(&i, md); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + upb_fielddef *f = upb_msg_iter_field(&i); -const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr) { - return attr->return_closure_type_; -} + if (upb_fielddef_issubmsg(f)) { + const upb_msgdef *subdef = upb_fielddef_msgsubdef(f); + const upb_handlers *sub_mh = upb_handlercache_get(c, subdef); -bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok) { - attr->alwaysok_ = alwaysok; - return true; -} + if (!sub_mh) return NULL; + + upb_handlers_setsubhandlers(h, f, sub_mh); + } + } -bool upb_handlerattr_alwaysok(const upb_handlerattr *attr) { - return attr->alwaysok_; + return h; } -/* upb_bufhandle **************************************************************/ -size_t upb_bufhandle_objofs(const upb_bufhandle *h) { - return h->objofs_; -} +upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, + const void *closure) { + upb_handlercache *cache = upb_gmalloc(sizeof(*cache)); -/* upb_byteshandler ***********************************************************/ + if (!cache) return NULL; + + cache->arena = upb_arena_new(); + + cache->callback = callback; + cache->closure = closure; + + if (!upb_inttable_init(&cache->tab, UPB_CTYPE_PTR)) goto oom; + + return cache; -void upb_byteshandler_init(upb_byteshandler* h) { - memset(h, 0, sizeof(*h)); +oom: + upb_gfree(cache); + return NULL; } -/* For when we support handlerfree callbacks. */ -void upb_byteshandler_uninit(upb_byteshandler* h) { - UPB_UNUSED(h); +void upb_handlercache_free(upb_handlercache *cache) { + upb_inttable_uninit(&cache->tab); + upb_arena_free(cache->arena); + upb_gfree(cache); +} + +bool upb_handlercache_addcleanup(upb_handlercache *c, void *p, + upb_handlerfree *func) { + return upb_arena_addcleanup(c->arena, p, func); } +/* upb_byteshandler ***********************************************************/ + bool upb_byteshandler_setstartstr(upb_byteshandler *h, upb_startstr_handlerfunc *func, void *d) { h->table[UPB_STARTSTR_SELECTOR].func = (upb_func*)func; - h->table[UPB_STARTSTR_SELECTOR].attr.handler_data_ = d; + h->table[UPB_STARTSTR_SELECTOR].attr.handler_data = d; return true; } bool upb_byteshandler_setstring(upb_byteshandler *h, upb_string_handlerfunc *func, void *d) { h->table[UPB_STRING_SELECTOR].func = (upb_func*)func; - h->table[UPB_STRING_SELECTOR].attr.handler_data_ = d; + h->table[UPB_STRING_SELECTOR].attr.handler_data = d; return true; } bool upb_byteshandler_setendstr(upb_byteshandler *h, upb_endfield_handlerfunc *func, void *d) { h->table[UPB_ENDSTR_SELECTOR].func = (upb_func*)func; - h->table[UPB_ENDSTR_SELECTOR].attr.handler_data_ = d; + h->table[UPB_ENDSTR_SELECTOR].attr.handler_data = d; return true; } +/** Handlers for upb_msg ******************************************************/ -static bool is_power_of_two(size_t val) { - return (val & (val - 1)) == 0; -} - -/* Align up to the given power of 2. */ -static size_t align_up(size_t val, size_t align) { - UPB_ASSERT(is_power_of_two(align)); - return (val + align - 1) & ~(align - 1); -} +typedef struct { + size_t offset; + int32_t hasbit; +} upb_msg_handlerdata; -static size_t div_round_up(size_t n, size_t d) { - return (n + d - 1) / d; -} +/* Fallback implementation if the handler is not specialized by the producer. */ +#define MSG_WRITER(type, ctype) \ + bool upb_msg_set ## type (void *c, const void *hd, ctype val) { \ + uint8_t *m = c; \ + const upb_msg_handlerdata *d = hd; \ + if (d->hasbit > 0) \ + *(uint8_t*)&m[d->hasbit / 8] |= 1 << (d->hasbit % 8); \ + *(ctype*)&m[d->offset] = val; \ + return true; \ + } \ + +MSG_WRITER(double, double) +MSG_WRITER(float, float) +MSG_WRITER(int32, int32_t) +MSG_WRITER(int64, int64_t) +MSG_WRITER(uint32, uint32_t) +MSG_WRITER(uint64, uint64_t) +MSG_WRITER(bool, bool) + +bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f, + size_t offset, int32_t hasbit) { + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + bool ok; + + upb_msg_handlerdata *d = upb_gmalloc(sizeof(*d)); + if (!d) return false; + d->offset = offset; + d->hasbit = hasbit; + + attr.handler_data = d; + attr.alwaysok = true; + upb_handlers_addcleanup(h, d, upb_gfree); + +#define TYPE(u, l) \ + case UPB_TYPE_##u: \ + ok = upb_handlers_set##l(h, f, upb_msg_set##l, &attr); break; + + ok = false; + + switch (upb_fielddef_type(f)) { + TYPE(INT64, int64); + TYPE(INT32, int32); + TYPE(ENUM, int32); + TYPE(UINT64, uint64); + TYPE(UINT32, uint32); + TYPE(DOUBLE, double); + TYPE(FLOAT, float); + TYPE(BOOL, bool); + default: UPB_ASSERT(false); break; + } +#undef TYPE + + return ok; +} + +bool upb_msg_getscalarhandlerdata(const upb_handlers *h, + upb_selector_t s, + upb_fieldtype_t *type, + size_t *offset, + int32_t *hasbit) { + const upb_msg_handlerdata *d; + const void *p; + upb_func *f = upb_handlers_gethandler(h, s, &p); + + if ((upb_int64_handlerfunc*)f == upb_msg_setint64) { + *type = UPB_TYPE_INT64; + } else if ((upb_int32_handlerfunc*)f == upb_msg_setint32) { + *type = UPB_TYPE_INT32; + } else if ((upb_uint64_handlerfunc*)f == upb_msg_setuint64) { + *type = UPB_TYPE_UINT64; + } else if ((upb_uint32_handlerfunc*)f == upb_msg_setuint32) { + *type = UPB_TYPE_UINT32; + } else if ((upb_double_handlerfunc*)f == upb_msg_setdouble) { + *type = UPB_TYPE_DOUBLE; + } else if ((upb_float_handlerfunc*)f == upb_msg_setfloat) { + *type = UPB_TYPE_FLOAT; + } else if ((upb_bool_handlerfunc*)f == upb_msg_setbool) { + *type = UPB_TYPE_BOOL; + } else { + return false; + } + + d = p; + *offset = d->offset; + *hasbit = d->hasbit; + return true; +} + +#include bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) { return type == UPB_TYPE_BOOL || type == UPB_TYPE_INT32 || @@ -5984,9 +3848,6 @@ bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) { type == UPB_TYPE_UINT64 || type == UPB_TYPE_STRING; } -void *upb_array_pack(const upb_array *arr, void *p, size_t *ofs, size_t size); -void *upb_map_pack(const upb_map *map, void *p, size_t *ofs, size_t size); - #define PTR_AT(msg, ofs, type) (type*)((char*)msg + ofs) #define VOIDPTR_AT(msg, ofs) PTR_AT(msg, ofs, void) #define ENCODE_MAX_NESTING 64 @@ -5994,8 +3855,6 @@ void *upb_map_pack(const upb_map *map, void *p, size_t *ofs, size_t size); /** upb_msgval ****************************************************************/ -#define upb_alignof(t) offsetof(struct { char c; t x; }, x) - /* These functions will generate real memcpy() calls on ARM sadly, because * the compiler assumes they might not be aligned. */ @@ -6030,12 +3889,12 @@ static size_t upb_msgval_sizeof(upb_fieldtype_t type) { return sizeof(void*); case UPB_TYPE_BYTES: case UPB_TYPE_STRING: - return sizeof(upb_stringview); + return sizeof(upb_strview); } UPB_UNREACHABLE(); } -static uint8_t upb_msg_fieldsize(const upb_msglayout_fieldinit_v1 *field) { +static uint8_t upb_msg_fieldsize(const upb_msglayout_field *field) { if (field->label == UPB_LABEL_REPEATED) { return sizeof(void*); } else { @@ -6043,14 +3902,6 @@ static uint8_t upb_msg_fieldsize(const upb_msglayout_fieldinit_v1 *field) { } } -static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) { - if (upb_fielddef_isseq(f)) { - return sizeof(void*); - } else { - return upb_msgval_sizeof(upb_fielddef_type(f)); - } -} - /* TODO(haberman): this is broken right now because upb_msgval can contain * a char* / size_t pair, which is too big for a upb_value. To fix this * we'll probably need to dynamically allocate a upb_msgval and store a @@ -6086,8831 +3937,6170 @@ static upb_ctype_t upb_fieldtotabtype(upb_fieldtype_t type) { } } -static upb_msgval upb_msgval_fromdefault(const upb_fielddef *f) { - switch (upb_fielddef_type(f)) { - case UPB_TYPE_FLOAT: - return upb_msgval_float(upb_fielddef_defaultfloat(f)); - case UPB_TYPE_DOUBLE: - return upb_msgval_double(upb_fielddef_defaultdouble(f)); - case UPB_TYPE_BOOL: - return upb_msgval_bool(upb_fielddef_defaultbool(f)); - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: { - size_t len; - const char *ptr = upb_fielddef_defaultstr(f, &len); - return upb_msgval_makestr(ptr, len); - } - case UPB_TYPE_MESSAGE: - return upb_msgval_msg(NULL); - case UPB_TYPE_ENUM: - case UPB_TYPE_INT32: - return upb_msgval_int32(upb_fielddef_defaultint32(f)); - case UPB_TYPE_UINT32: - return upb_msgval_uint32(upb_fielddef_defaultuint32(f)); - case UPB_TYPE_INT64: - return upb_msgval_int64(upb_fielddef_defaultint64(f)); - case UPB_TYPE_UINT64: - return upb_msgval_uint64(upb_fielddef_defaultuint64(f)); - default: - UPB_ASSERT(false); - return upb_msgval_msg(NULL); - } -} +/** upb_msg *******************************************************************/ -/** upb_msglayout *************************************************************/ +/* If we always read/write as a consistent type to each address, this shouldn't + * violate aliasing. + */ +#define DEREF(msg, ofs, type) *PTR_AT(msg, ofs, type) -struct upb_msglayout { - struct upb_msglayout_msginit_v1 data; -}; +/* Internal members of a upb_msg. We can change this without breaking binary + * compatibility. We put these before the user's data. The user's upb_msg* + * points after the upb_msg_internal. */ -static void upb_msglayout_free(upb_msglayout *l) { - upb_gfree(l->data.default_msg); - upb_gfree(l); -} +/* Used when a message is not extendable. */ +typedef struct { + /* TODO(haberman): use pointer tagging so we we are slim when known unknown + * fields are not present. */ + upb_arena *arena; + char *unknown; + size_t unknown_len; + size_t unknown_size; +} upb_msg_internal; -static size_t upb_msglayout_place(upb_msglayout *l, size_t size) { - size_t ret; +/* Used when a message is extendable. */ +typedef struct { + upb_inttable *extdict; + upb_msg_internal base; +} upb_msg_internal_withext; - l->data.size = align_up(l->data.size, size); - ret = l->data.size; - l->data.size += size; - return ret; +static int upb_msg_internalsize(const upb_msglayout *l) { + return sizeof(upb_msg_internal) - l->extendable * sizeof(void *); } -static uint32_t upb_msglayout_offset(const upb_msglayout *l, - const upb_fielddef *f) { - return l->data.fields[upb_fielddef_index(f)].offset; +static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) { + return VOIDPTR_AT(msg, -sizeof(upb_msg_internal)); } -static uint32_t upb_msglayout_hasbit(const upb_msglayout *l, - const upb_fielddef *f) { - return l->data.fields[upb_fielddef_index(f)].hasbit; +static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) { + return VOIDPTR_AT(msg, -sizeof(upb_msg_internal)); } -static bool upb_msglayout_initdefault(upb_msglayout *l, const upb_msgdef *m) { - upb_msg_field_iter it; +static upb_msg_internal_withext *upb_msg_getinternalwithext( + upb_msg *msg, const upb_msglayout *l) { + UPB_ASSERT(l->extendable); + return VOIDPTR_AT(msg, -sizeof(upb_msg_internal_withext)); +} - if (upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2 && l->data.size) { - /* Allocate default message and set default values in it. */ - l->data.default_msg = upb_gmalloc(l->data.size); - if (!l->data.default_msg) { - return false; - } +void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len) { + upb_msg_internal* in = upb_msg_getinternal(msg); + if (len > in->unknown_size - in->unknown_len) { + upb_alloc *alloc = upb_arena_alloc(in->arena); + size_t need = in->unknown_size + len; + size_t newsize = UPB_MAX(in->unknown_size * 2, need); + in->unknown = upb_realloc(alloc, in->unknown, in->unknown_size, newsize); + in->unknown_size = newsize; + } + memcpy(in->unknown + in->unknown_len, data, len); + in->unknown_len += len; +} - memset(l->data.default_msg, 0, l->data.size); +const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) { + const upb_msg_internal* in = upb_msg_getinternal_const(msg); + *len = in->unknown_len; + return in->unknown; +} - for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it); - upb_msg_field_next(&it)) { - const upb_fielddef* f = upb_msg_iter_field(&it); +static const upb_msglayout_field *upb_msg_checkfield(int field_index, + const upb_msglayout *l) { + UPB_ASSERT(field_index >= 0 && field_index < l->field_count); + return &l->fields[field_index]; +} - if (upb_fielddef_containingoneof(f)) { - continue; - } +static bool upb_msg_inoneof(const upb_msglayout_field *field) { + return field->presence < 0; +} - /* TODO(haberman): handle strings. */ - if (!upb_fielddef_isstring(f) && - !upb_fielddef_issubmsg(f) && - !upb_fielddef_isseq(f)) { - upb_msg_set(l->data.default_msg, - upb_fielddef_index(f), - upb_msgval_fromdefault(f), - l); - } - } - } +static uint32_t *upb_msg_oneofcase(const upb_msg *msg, int field_index, + const upb_msglayout *l) { + const upb_msglayout_field *field = upb_msg_checkfield(field_index, l); + UPB_ASSERT(upb_msg_inoneof(field)); + return PTR_AT(msg, ~field->presence, uint32_t); +} - return true; +static size_t upb_msg_sizeof(const upb_msglayout *l) { + return l->size + upb_msg_internalsize(l); } -static bool upb_msglayout_init(const upb_msgdef *m, - upb_msglayout *l, - upb_msgfactory *factory) { - upb_msg_field_iter it; - upb_msg_oneof_iter oit; - size_t hasbit; - size_t submsg_count = 0; - const upb_msglayout_msginit_v1 **submsgs; - upb_msglayout_fieldinit_v1 *fields; - upb_msglayout_oneofinit_v1 *oneofs; +upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a) { + upb_alloc *alloc = upb_arena_alloc(a); + void *mem = upb_malloc(alloc, upb_msg_sizeof(l)); + upb_msg_internal *in; + upb_msg *msg; - for (upb_msg_field_begin(&it, m); - !upb_msg_field_done(&it); - upb_msg_field_next(&it)) { - const upb_fielddef* f = upb_msg_iter_field(&it); - if (upb_fielddef_issubmsg(f)) { - submsg_count++; - } + if (!mem) { + return NULL; } - memset(l, 0, sizeof(*l)); + msg = VOIDPTR_AT(mem, upb_msg_internalsize(l)); - fields = upb_gmalloc(upb_msgdef_numfields(m) * sizeof(*fields)); - submsgs = upb_gmalloc(submsg_count * sizeof(*submsgs)); - oneofs = upb_gmalloc(upb_msgdef_numoneofs(m) * sizeof(*oneofs)); + /* Initialize normal members. */ + memset(msg, 0, l->size); - if ((!fields && upb_msgdef_numfields(m)) || - (!submsgs && submsg_count) || - (!oneofs && upb_msgdef_numoneofs(m))) { - /* OOM. */ - upb_gfree(fields); - upb_gfree(submsgs); - upb_gfree(oneofs); - return false; + /* Initialize internal members. */ + in = upb_msg_getinternal(msg); + in->arena = a; + in->unknown = NULL; + in->unknown_len = 0; + in->unknown_size = 0; + + if (l->extendable) { + upb_msg_getinternalwithext(msg, l)->extdict = NULL; } - l->data.field_count = upb_msgdef_numfields(m); - l->data.oneof_count = upb_msgdef_numoneofs(m); - l->data.fields = fields; - l->data.submsgs = submsgs; - l->data.oneofs = oneofs; - l->data.is_proto2 = (upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2); + return msg; +} - /* Allocate data offsets in three stages: - * - * 1. hasbits. - * 2. regular fields. - * 3. oneof fields. - * - * OPT: There is a lot of room for optimization here to minimize the size. - */ +upb_arena *upb_msg_arena(const upb_msg *msg) { + return upb_msg_getinternal_const(msg)->arena; +} - /* Allocate hasbits and set basic field attributes. */ - submsg_count = 0; - for (upb_msg_field_begin(&it, m), hasbit = 0; - !upb_msg_field_done(&it); - upb_msg_field_next(&it)) { - const upb_fielddef* f = upb_msg_iter_field(&it); - upb_msglayout_fieldinit_v1 *field = &fields[upb_fielddef_index(f)]; +bool upb_msg_has(const upb_msg *msg, + int field_index, + const upb_msglayout *l) { + const upb_msglayout_field *field = upb_msg_checkfield(field_index, l); - field->number = upb_fielddef_number(f); - field->descriptortype = upb_fielddef_descriptortype(f); - field->label = upb_fielddef_label(f); + UPB_ASSERT(field->presence); - if (upb_fielddef_containingoneof(f)) { - field->oneof_index = upb_oneofdef_index(upb_fielddef_containingoneof(f)); - } else { - field->oneof_index = UPB_NOT_IN_ONEOF; - } + if (upb_msg_inoneof(field)) { + /* Oneofs are set when the oneof number is set to this field. */ + return *upb_msg_oneofcase(msg, field_index, l) == field->number; + } else { + /* Other fields are set when their hasbit is set. */ + uint32_t hasbit = field->presence; + return DEREF(msg, hasbit / 8, char) | (1 << (hasbit % 8)); + } +} - if (upb_fielddef_issubmsg(f)) { - const upb_msglayout *sub_layout = - upb_msgfactory_getlayout(factory, upb_fielddef_msgsubdef(f)); - field->submsg_index = submsg_count++; - submsgs[field->submsg_index] = &sub_layout->data; - } else { - field->submsg_index = UPB_NO_SUBMSG; - } +upb_msgval upb_msg_get(const upb_msg *msg, int field_index, + const upb_msglayout *l) { + const upb_msglayout_field *field = upb_msg_checkfield(field_index, l); + int size = upb_msg_fieldsize(field); + return upb_msgval_read(msg, field->offset, size); +} - if (upb_fielddef_haspresence(f) && !upb_fielddef_containingoneof(f)) { - field->hasbit = hasbit++; - } else { - field->hasbit = UPB_NO_HASBIT; - } - } +void upb_msg_set(upb_msg *msg, int field_index, upb_msgval val, + const upb_msglayout *l) { + const upb_msglayout_field *field = upb_msg_checkfield(field_index, l); + int size = upb_msg_fieldsize(field); + upb_msgval_write(msg, field->offset, val, size); +} - /* Account for space used by hasbits. */ - l->data.size = div_round_up(hasbit, 8); - /* Allocate non-oneof fields. */ - for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it); - upb_msg_field_next(&it)) { - const upb_fielddef* f = upb_msg_iter_field(&it); - size_t field_size = upb_msg_fielddefsize(f); - size_t index = upb_fielddef_index(f); +/** upb_array *****************************************************************/ - if (upb_fielddef_containingoneof(f)) { - /* Oneofs are handled separately below. */ - continue; - } +#define DEREF_ARR(arr, i, type) ((type*)arr->data)[i] - fields[index].offset = upb_msglayout_place(l, field_size); +upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a) { + upb_alloc *alloc = upb_arena_alloc(a); + upb_array *ret = upb_malloc(alloc, sizeof(upb_array)); + + if (!ret) { + return NULL; } - /* Allocate oneof fields. Each oneof field consists of a uint32 for the case - * and space for the actual data. */ - for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit); - upb_msg_oneof_next(&oit)) { - const upb_oneofdef* o = upb_msg_iter_oneof(&oit); - upb_oneof_iter fit; - - size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */ - upb_msglayout_oneofinit_v1 *oneof = &oneofs[upb_oneofdef_index(o)]; - size_t field_size = 0; + ret->type = type; + ret->data = NULL; + ret->len = 0; + ret->size = 0; + ret->element_size = upb_msgval_sizeof(type); + ret->arena = a; - /* Calculate field size: the max of all field sizes. */ - for (upb_oneof_begin(&fit, o); - !upb_oneof_done(&fit); - upb_oneof_next(&fit)) { - const upb_fielddef* f = upb_oneof_iter_field(&fit); - field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f)); - } + return ret; +} - /* Align and allocate case offset. */ - oneof->case_offset = upb_msglayout_place(l, case_size); - oneof->data_offset = upb_msglayout_place(l, field_size); - } +size_t upb_array_size(const upb_array *arr) { + return arr->len; +} - /* Size of the entire structure should be a multiple of its greatest - * alignment. TODO: track overall alignment for real? */ - l->data.size = align_up(l->data.size, 8); +upb_fieldtype_t upb_array_type(const upb_array *arr) { + return arr->type; +} - return upb_msglayout_initdefault(l, m); +upb_msgval upb_array_get(const upb_array *arr, size_t i) { + UPB_ASSERT(i < arr->len); + return upb_msgval_read(arr->data, i * arr->element_size, arr->element_size); } +bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) { + UPB_ASSERT(i <= arr->len); + + if (i == arr->len) { + /* Extending the array. */ -/** upb_msgfactory ************************************************************/ + if (i == arr->size) { + /* Need to reallocate. */ + size_t new_size = UPB_MAX(arr->size * 2, 8); + size_t new_bytes = new_size * arr->element_size; + size_t old_bytes = arr->size * arr->element_size; + upb_alloc *alloc = upb_arena_alloc(arr->arena); + upb_msgval *new_data = + upb_realloc(alloc, arr->data, old_bytes, new_bytes); -struct upb_msgfactory { - const upb_symtab *symtab; /* We own a ref. */ - upb_inttable layouts; - upb_inttable mergehandlers; -}; + if (!new_data) { + return false; + } -upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) { - upb_msgfactory *ret = upb_gmalloc(sizeof(*ret)); + arr->data = new_data; + arr->size = new_size; + } - ret->symtab = symtab; - upb_inttable_init(&ret->layouts, UPB_CTYPE_PTR); - upb_inttable_init(&ret->mergehandlers, UPB_CTYPE_CONSTPTR); + arr->len = i + 1; + } - return ret; + upb_msgval_write(arr->data, i * arr->element_size, val, arr->element_size); + return true; } -void upb_msgfactory_free(upb_msgfactory *f) { - upb_inttable_iter i; - upb_inttable_begin(&i, &f->layouts); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_msglayout *l = upb_value_getptr(upb_inttable_iter_value(&i)); - upb_msglayout_free(l); - } - upb_inttable_begin(&i, &f->mergehandlers); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - const upb_handlers *h = upb_value_getconstptr(upb_inttable_iter_value(&i)); - upb_handlers_unref(h, f); - } +/** upb_map *******************************************************************/ - upb_inttable_uninit(&f->layouts); - upb_inttable_uninit(&f->mergehandlers); - upb_gfree(f); -} +struct upb_map { + upb_fieldtype_t key_type; + upb_fieldtype_t val_type; + /* We may want to optimize this to use inttable where possible, for greater + * efficiency and lower memory footprint. */ + upb_strtable strtab; + upb_arena *arena; +}; -const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f) { - return f->symtab; +static void upb_map_tokey(upb_fieldtype_t type, upb_msgval *key, + const char **out_key, size_t *out_len) { + switch (type) { + case UPB_TYPE_STRING: + /* Point to string data of the input key. */ + *out_key = key->str.data; + *out_len = key->str.size; + return; + case UPB_TYPE_BOOL: + case UPB_TYPE_INT32: + case UPB_TYPE_UINT32: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT64: + /* Point to the key itself. XXX: big-endian. */ + *out_key = (const char*)key; + *out_len = upb_msgval_sizeof(type); + return; + case UPB_TYPE_BYTES: + case UPB_TYPE_DOUBLE: + case UPB_TYPE_ENUM: + case UPB_TYPE_FLOAT: + case UPB_TYPE_MESSAGE: + break; /* Cannot be a map key. */ + } + UPB_UNREACHABLE(); } -const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, - const upb_msgdef *m) { - upb_value v; - UPB_ASSERT(upb_symtab_lookupmsg(f->symtab, upb_msgdef_fullname(m)) == m); - UPB_ASSERT(!upb_msgdef_mapentry(m)); - - if (upb_inttable_lookupptr(&f->layouts, m, &v)) { - UPB_ASSERT(upb_value_getptr(v)); - return upb_value_getptr(v); - } else { - /* In case of circular dependency, layout has to be inserted first. */ - upb_msglayout *l = upb_gmalloc(sizeof(*l)); - upb_msgfactory *mutable_f = (void*)f; - upb_inttable_insertptr(&mutable_f->layouts, m, upb_value_ptr(l)); - UPB_ASSERT(l); - if (!upb_msglayout_init(m, l, f)) { - upb_msglayout_free(l); - } - return l; +static upb_msgval upb_map_fromkey(upb_fieldtype_t type, const char *key, + size_t len) { + switch (type) { + case UPB_TYPE_STRING: + return upb_msgval_makestr(key, len); + case UPB_TYPE_BOOL: + case UPB_TYPE_INT32: + case UPB_TYPE_UINT32: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT64: + return upb_msgval_read(key, 0, upb_msgval_sizeof(type)); + case UPB_TYPE_BYTES: + case UPB_TYPE_DOUBLE: + case UPB_TYPE_ENUM: + case UPB_TYPE_FLOAT: + case UPB_TYPE_MESSAGE: + break; /* Cannot be a map key. */ } + UPB_UNREACHABLE(); } -/* Our handlers that we don't expose externally. */ +upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype, + upb_arena *a) { + upb_ctype_t vtabtype = upb_fieldtotabtype(vtype); + upb_alloc *alloc = upb_arena_alloc(a); + upb_map *map = upb_malloc(alloc, sizeof(upb_map)); -void *upb_msg_startstr(void *msg, const void *hd, size_t size_hint) { - uint32_t ofs = (uintptr_t)hd; - upb_alloc *alloc = upb_msg_alloc(msg); - upb_msgval val; - UPB_UNUSED(size_hint); + if (!map) { + return NULL; + } - val = upb_msgval_read(msg, ofs, upb_msgval_sizeof(UPB_TYPE_STRING)); + UPB_ASSERT(upb_fieldtype_mapkeyok(ktype)); + map->key_type = ktype; + map->val_type = vtype; + map->arena = a; - upb_free(alloc, (void*)val.str.data); - val.str.data = NULL; - val.str.size = 0; + if (!upb_strtable_init2(&map->strtab, vtabtype, alloc)) { + return NULL; + } - upb_msgval_write(msg, ofs, val, upb_msgval_sizeof(UPB_TYPE_STRING)); - return msg; + return map; } -size_t upb_msg_str(void *msg, const void *hd, const char *ptr, size_t size, - const upb_bufhandle *handle) { - uint32_t ofs = (uintptr_t)hd; - upb_alloc *alloc = upb_msg_alloc(msg); - upb_msgval val; - size_t newsize; - UPB_UNUSED(handle); - - val = upb_msgval_read(msg, ofs, upb_msgval_sizeof(UPB_TYPE_STRING)); - - newsize = val.str.size + size; - val.str.data = upb_realloc(alloc, (void*)val.str.data, val.str.size, newsize); +size_t upb_map_size(const upb_map *map) { + return upb_strtable_count(&map->strtab); +} - if (!val.str.data) { - return false; - } +upb_fieldtype_t upb_map_keytype(const upb_map *map) { + return map->key_type; +} - memcpy((char*)val.str.data + val.str.size, ptr, size); - val.str.size = newsize; - upb_msgval_write(msg, ofs, val, upb_msgval_sizeof(UPB_TYPE_STRING)); - return size; +upb_fieldtype_t upb_map_valuetype(const upb_map *map) { + return map->val_type; } -static void callback(const void *closure, upb_handlers *h) { - upb_msgfactory *factory = (upb_msgfactory*)closure; - const upb_msgdef *md = upb_handlers_msgdef(h); - const upb_msglayout* layout = upb_msgfactory_getlayout(factory, md); - upb_msg_field_iter i; - UPB_UNUSED(factory); +bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) { + upb_value tabval; + const char *key_str; + size_t key_len; + bool ret; - for(upb_msg_field_begin(&i, md); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - const upb_fielddef *f = upb_msg_iter_field(&i); - size_t offset = upb_msglayout_offset(layout, f); - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, (void*)offset); - - if (upb_fielddef_isseq(f)) { - } else if (upb_fielddef_isstring(f)) { - upb_handlers_setstartstr(h, f, upb_msg_startstr, &attr); - upb_handlers_setstring(h, f, upb_msg_str, &attr); - } else { - upb_msg_setscalarhandler( - h, f, offset, upb_msglayout_hasbit(layout, f)); - } + upb_map_tokey(map->key_type, &key, &key_str, &key_len); + ret = upb_strtable_lookup2(&map->strtab, key_str, key_len, &tabval); + if (ret) { + memcpy(val, &tabval, sizeof(tabval)); } + + return ret; } -const upb_handlers *upb_msgfactory_getmergehandlers(upb_msgfactory *f, - const upb_msgdef *m) { - upb_msgfactory *mutable_f = (void*)f; +bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val, + upb_msgval *removed) { + const char *key_str; + size_t key_len; + upb_value tabval = upb_toval(val); + upb_value removedtabval; + upb_alloc *a = upb_arena_alloc(map->arena); - /* TODO(haberman): properly cache these. */ - const upb_handlers *ret = upb_handlers_newfrozen(m, f, callback, f); - upb_inttable_push(&mutable_f->mergehandlers, upb_value_constptr(ret)); + upb_map_tokey(map->key_type, &key, &key_str, &key_len); - return ret; + /* TODO(haberman): add overwrite operation to minimize number of lookups. */ + if (upb_strtable_lookup2(&map->strtab, key_str, key_len, NULL)) { + upb_strtable_remove3(&map->strtab, key_str, key_len, &removedtabval, a); + memcpy(&removed, &removedtabval, sizeof(removed)); + } + + return upb_strtable_insert3(&map->strtab, key_str, key_len, tabval, a); } -const upb_visitorplan *upb_msgfactory_getvisitorplan(upb_msgfactory *f, - const upb_handlers *h) { - const upb_msgdef *md = upb_handlers_msgdef(h); - return (const upb_visitorplan*)upb_msgfactory_getlayout(f, md); +bool upb_map_del(upb_map *map, upb_msgval key) { + const char *key_str; + size_t key_len; + upb_alloc *a = upb_arena_alloc(map->arena); + + upb_map_tokey(map->key_type, &key, &key_str, &key_len); + return upb_strtable_remove3(&map->strtab, key_str, key_len, NULL, a); } -/** upb_visitor ***************************************************************/ +/** upb_mapiter ***************************************************************/ -struct upb_visitor { - const upb_msglayout *layout; - upb_sink *sink; +struct upb_mapiter { + upb_strtable_iter iter; + upb_fieldtype_t key_type; }; -static upb_selector_t getsel2(const upb_fielddef *f, upb_handlertype_t type) { - upb_selector_t ret; - bool ok = upb_handlers_getselector(f, type, &ret); - UPB_ASSERT(ok); - return ret; +size_t upb_mapiter_sizeof() { + return sizeof(upb_mapiter); } -static bool upb_visitor_hasfield(const upb_msg *msg, const upb_fielddef *f, - const upb_msglayout *layout) { - int field_index = upb_fielddef_index(f); - if (upb_fielddef_isseq(f)) { - return upb_msgval_getarr(upb_msg_get(msg, field_index, layout)) != NULL; - } else if (upb_msgdef_syntax(upb_fielddef_containingtype(f)) == - UPB_SYNTAX_PROTO2) { - return upb_msg_has(msg, field_index, layout); - } else { - upb_msgval val = upb_msg_get(msg, field_index, layout); - switch (upb_fielddef_type(f)) { - case UPB_TYPE_FLOAT: - return upb_msgval_getfloat(val) != 0; - case UPB_TYPE_DOUBLE: - return upb_msgval_getdouble(val) != 0; - case UPB_TYPE_BOOL: - return upb_msgval_getbool(val); - case UPB_TYPE_ENUM: - case UPB_TYPE_INT32: - return upb_msgval_getint32(val) != 0; - case UPB_TYPE_UINT32: - return upb_msgval_getuint32(val) != 0; - case UPB_TYPE_INT64: - return upb_msgval_getint64(val) != 0; - case UPB_TYPE_UINT64: - return upb_msgval_getuint64(val) != 0; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - return upb_msgval_getstr(val).size > 0; - case UPB_TYPE_MESSAGE: - return upb_msgval_getmsg(val) != NULL; - } - UPB_UNREACHABLE(); - } +void upb_mapiter_begin(upb_mapiter *i, const upb_map *map) { + upb_strtable_begin(&i->iter, &map->strtab); + i->key_type = map->key_type; } -static bool upb_visitor_visitmsg2(const upb_msg *msg, - const upb_msglayout *layout, upb_sink *sink, - int depth) { - const upb_msgdef *md = upb_handlers_msgdef(sink->handlers); - upb_msg_field_iter i; - upb_status status; - - upb_sink_startmsg(sink); +upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a) { + upb_mapiter *ret = upb_malloc(a, upb_mapiter_sizeof()); - /* Protect against cycles (possible because users may freely reassign message - * and repeated fields) by imposing a maximum recursion depth. */ - if (depth > ENCODE_MAX_NESTING) { - return false; + if (!ret) { + return NULL; } - for (upb_msg_field_begin(&i, md); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - upb_msgval val; - - if (!upb_visitor_hasfield(msg, f, layout)) { - continue; - } + upb_mapiter_begin(ret, t); + return ret; +} - val = upb_msg_get(msg, upb_fielddef_index(f), layout); - - if (upb_fielddef_isseq(f)) { - const upb_array *arr = upb_msgval_getarr(val); - UPB_ASSERT(arr); - /* TODO: putary(ary, f, sink, depth);*/ - } else if (upb_fielddef_issubmsg(f)) { - const upb_map *map = upb_msgval_getmap(val); - UPB_ASSERT(map); - /* TODO: putmap(map, f, sink, depth);*/ - } else if (upb_fielddef_isstring(f)) { - /* TODO putstr(); */ - } else { - upb_selector_t sel = getsel2(f, upb_handlers_getprimitivehandlertype(f)); - UPB_ASSERT(upb_fielddef_isprimitive(f)); - - switch (upb_fielddef_type(f)) { - case UPB_TYPE_FLOAT: - CHECK_TRUE(upb_sink_putfloat(sink, sel, upb_msgval_getfloat(val))); - break; - case UPB_TYPE_DOUBLE: - CHECK_TRUE(upb_sink_putdouble(sink, sel, upb_msgval_getdouble(val))); - break; - case UPB_TYPE_BOOL: - CHECK_TRUE(upb_sink_putbool(sink, sel, upb_msgval_getbool(val))); - break; - case UPB_TYPE_ENUM: - case UPB_TYPE_INT32: - CHECK_TRUE(upb_sink_putint32(sink, sel, upb_msgval_getint32(val))); - break; - case UPB_TYPE_UINT32: - CHECK_TRUE(upb_sink_putuint32(sink, sel, upb_msgval_getuint32(val))); - break; - case UPB_TYPE_INT64: - CHECK_TRUE(upb_sink_putint64(sink, sel, upb_msgval_getint64(val))); - break; - case UPB_TYPE_UINT64: - CHECK_TRUE(upb_sink_putuint64(sink, sel, upb_msgval_getuint64(val))); - break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - case UPB_TYPE_MESSAGE: - UPB_UNREACHABLE(); - } - } - } - - upb_sink_endmsg(sink, &status); - return true; +void upb_mapiter_free(upb_mapiter *i, upb_alloc *a) { + upb_free(a, i); } -upb_visitor *upb_visitor_create(upb_env *e, const upb_visitorplan *vp, - upb_sink *output) { - upb_visitor *visitor = upb_env_malloc(e, sizeof(*visitor)); - visitor->layout = (const upb_msglayout*)vp; - visitor->sink = output; - return visitor; +void upb_mapiter_next(upb_mapiter *i) { + upb_strtable_next(&i->iter); } -bool upb_visitor_visitmsg(upb_visitor *visitor, const upb_msg *msg) { - return upb_visitor_visitmsg2(msg, visitor->layout, visitor->sink, 0); +bool upb_mapiter_done(const upb_mapiter *i) { + return upb_strtable_done(&i->iter); } - -/** upb_msg *******************************************************************/ - -/* If we always read/write as a consistent type to each address, this shouldn't - * violate aliasing. - */ -#define DEREF(msg, ofs, type) *PTR_AT(msg, ofs, type) - -/* Internal members of a upb_msg. We can change this without breaking binary - * compatibility. We put these before the user's data. The user's upb_msg* - * points after the upb_msg_internal. */ - -/* Used when a message is not extendable. */ -typedef struct { - /* TODO(haberman): add unknown fields. */ - upb_alloc *alloc; -} upb_msg_internal; - -/* Used when a message is extendable. */ -typedef struct { - upb_inttable *extdict; - upb_msg_internal base; -} upb_msg_internal_withext; - -static int upb_msg_internalsize(const upb_msglayout *l) { - return sizeof(upb_msg_internal) - l->data.extendable * sizeof(void*); +upb_msgval upb_mapiter_key(const upb_mapiter *i) { + return upb_map_fromkey(i->key_type, upb_strtable_iter_key(&i->iter), + upb_strtable_iter_keylength(&i->iter)); } -static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) { - return VOIDPTR_AT(msg, -sizeof(upb_msg_internal)); +upb_msgval upb_mapiter_value(const upb_mapiter *i) { + return upb_msgval_fromval(upb_strtable_iter_value(&i->iter)); } -static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) { - return VOIDPTR_AT(msg, -sizeof(upb_msg_internal)); +void upb_mapiter_setdone(upb_mapiter *i) { + upb_strtable_iter_setdone(&i->iter); } -static upb_msg_internal_withext *upb_msg_getinternalwithext( - upb_msg *msg, const upb_msglayout *l) { - UPB_ASSERT(l->data.extendable); - return VOIDPTR_AT(msg, -sizeof(upb_msg_internal_withext)); +bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2) { + return upb_strtable_iter_isequal(&i1->iter, &i2->iter); } -static const upb_msglayout_fieldinit_v1 *upb_msg_checkfield( - int field_index, const upb_msglayout *l) { - UPB_ASSERT(field_index >= 0 && field_index < l->data.field_count); - return &l->data.fields[field_index]; -} -static bool upb_msg_inoneof(const upb_msglayout_fieldinit_v1 *field) { - return field->oneof_index != UPB_NOT_IN_ONEOF; +static bool is_power_of_two(size_t val) { + return (val & (val - 1)) == 0; } -static uint32_t *upb_msg_oneofcase(const upb_msg *msg, int field_index, - const upb_msglayout *l) { - const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l); - UPB_ASSERT(upb_msg_inoneof(field)); - return PTR_AT(msg, l->data.oneofs[field->oneof_index].case_offset, uint32_t); +/* Align up to the given power of 2. */ +static size_t align_up(size_t val, size_t align) { + UPB_ASSERT(is_power_of_two(align)); + return (val + align - 1) & ~(align - 1); } -size_t upb_msg_sizeof(const upb_msglayout *l) { - return l->data.size + upb_msg_internalsize(l); +static size_t div_round_up(size_t n, size_t d) { + return (n + d - 1) / d; } -upb_msg *upb_msg_init(void *mem, const upb_msglayout *l, upb_alloc *a) { - upb_msg *msg = VOIDPTR_AT(mem, upb_msg_internalsize(l)); - - /* Initialize normal members. */ - if (l->data.default_msg) { - memcpy(msg, l->data.default_msg, l->data.size); - } else { - memset(msg, 0, l->data.size); - } - - /* Initialize internal members. */ - upb_msg_getinternal(msg)->alloc = a; - - if (l->data.extendable) { - upb_msg_getinternalwithext(msg, l)->extdict = NULL; +static size_t upb_msgval_sizeof2(upb_fieldtype_t type) { + switch (type) { + case UPB_TYPE_DOUBLE: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT64: + return 8; + case UPB_TYPE_ENUM: + case UPB_TYPE_INT32: + case UPB_TYPE_UINT32: + case UPB_TYPE_FLOAT: + return 4; + case UPB_TYPE_BOOL: + return 1; + case UPB_TYPE_MESSAGE: + return sizeof(void*); + case UPB_TYPE_BYTES: + case UPB_TYPE_STRING: + return sizeof(upb_strview); } - - return msg; + UPB_UNREACHABLE(); } -void *upb_msg_uninit(upb_msg *msg, const upb_msglayout *l) { - if (l->data.extendable) { - upb_inttable *ext_dict = upb_msg_getinternalwithext(msg, l)->extdict; - if (ext_dict) { - upb_inttable_uninit2(ext_dict, upb_msg_alloc(msg)); - upb_free(upb_msg_alloc(msg), ext_dict); - } +static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) { + if (upb_fielddef_isseq(f)) { + return sizeof(void*); + } else { + return upb_msgval_sizeof2(upb_fielddef_type(f)); } - - return VOIDPTR_AT(msg, -upb_msg_internalsize(l)); } -upb_msg *upb_msg_new(const upb_msglayout *l, upb_alloc *a) { - void *mem = upb_malloc(a, upb_msg_sizeof(l)); - return mem ? upb_msg_init(mem, l, a) : NULL; -} -void upb_msg_free(upb_msg *msg, const upb_msglayout *l) { - upb_free(upb_msg_alloc(msg), upb_msg_uninit(msg, l)); -} +/** upb_msglayout *************************************************************/ -upb_alloc *upb_msg_alloc(const upb_msg *msg) { - return upb_msg_getinternal_const(msg)->alloc; +static void upb_msglayout_free(upb_msglayout *l) { + upb_gfree(l); } -bool upb_msg_has(const upb_msg *msg, - int field_index, - const upb_msglayout *l) { - const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l); - - UPB_ASSERT(l->data.is_proto2); +static size_t upb_msglayout_place(upb_msglayout *l, size_t size) { + size_t ret; - if (upb_msg_inoneof(field)) { - /* Oneofs are set when the oneof number is set to this field. */ - return *upb_msg_oneofcase(msg, field_index, l) == field->number; - } else { - /* Other fields are set when their hasbit is set. */ - uint32_t hasbit = l->data.fields[field_index].hasbit; - return DEREF(msg, hasbit / 8, char) | (1 << (hasbit % 8)); - } + l->size = align_up(l->size, size); + ret = l->size; + l->size += size; + return ret; } -upb_msgval upb_msg_get(const upb_msg *msg, int field_index, - const upb_msglayout *l) { - const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l); - int size = upb_msg_fieldsize(field); +static bool upb_msglayout_init(const upb_msgdef *m, + upb_msglayout *l, + upb_msgfactory *factory) { + upb_msg_field_iter it; + upb_msg_oneof_iter oit; + size_t hasbit; + size_t submsg_count = 0; + const upb_msglayout **submsgs; + upb_msglayout_field *fields; - if (upb_msg_inoneof(field)) { - if (*upb_msg_oneofcase(msg, field_index, l) == field->number) { - size_t ofs = l->data.oneofs[field->oneof_index].data_offset; - return upb_msgval_read(msg, ofs, size); - } else { - /* Return default. */ - return upb_msgval_read(l->data.default_msg, field->offset, size); + for (upb_msg_field_begin(&it, m); + !upb_msg_field_done(&it); + upb_msg_field_next(&it)) { + const upb_fielddef* f = upb_msg_iter_field(&it); + if (upb_fielddef_issubmsg(f)) { + submsg_count++; } - } else { - return upb_msgval_read(msg, field->offset, size); } -} -void upb_msg_set(upb_msg *msg, int field_index, upb_msgval val, - const upb_msglayout *l) { - const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l); - int size = upb_msg_fieldsize(field); - - if (upb_msg_inoneof(field)) { - size_t ofs = l->data.oneofs[field->oneof_index].data_offset; - *upb_msg_oneofcase(msg, field_index, l) = field->number; - upb_msgval_write(msg, ofs, val, size); - } else { - upb_msgval_write(msg, field->offset, val, size); - } -} + memset(l, 0, sizeof(*l)); + fields = upb_gmalloc(upb_msgdef_numfields(m) * sizeof(*fields)); + submsgs = upb_gmalloc(submsg_count * sizeof(*submsgs)); -/** upb_array *****************************************************************/ + if ((!fields && upb_msgdef_numfields(m)) || + (!submsgs && submsg_count)) { + /* OOM. */ + upb_gfree(fields); + upb_gfree(submsgs); + return false; + } -#define DEREF_ARR(arr, i, type) ((type*)arr->data)[i] + l->field_count = upb_msgdef_numfields(m); + l->fields = fields; + l->submsgs = submsgs; -size_t upb_array_sizeof(upb_fieldtype_t type) { - UPB_UNUSED(type); - return sizeof(upb_array); -} + /* Allocate data offsets in three stages: + * + * 1. hasbits. + * 2. regular fields. + * 3. oneof fields. + * + * OPT: There is a lot of room for optimization here to minimize the size. + */ -void upb_array_init(upb_array *arr, upb_fieldtype_t type, upb_alloc *alloc) { - arr->type = type; - arr->data = NULL; - arr->len = 0; - arr->size = 0; - arr->element_size = upb_msgval_sizeof(type); - arr->alloc = alloc; -} + /* Allocate hasbits and set basic field attributes. */ + submsg_count = 0; + for (upb_msg_field_begin(&it, m), hasbit = 0; + !upb_msg_field_done(&it); + upb_msg_field_next(&it)) { + const upb_fielddef* f = upb_msg_iter_field(&it); + upb_msglayout_field *field = &fields[upb_fielddef_index(f)]; -void upb_array_uninit(upb_array *arr) { - upb_free(arr->alloc, arr->data); -} + field->number = upb_fielddef_number(f); + field->descriptortype = upb_fielddef_descriptortype(f); + field->label = upb_fielddef_label(f); -upb_array *upb_array_new(upb_fieldtype_t type, upb_alloc *a) { - upb_array *ret = upb_malloc(a, upb_array_sizeof(type)); + if (upb_fielddef_issubmsg(f)) { + const upb_msglayout *sub_layout = + upb_msgfactory_getlayout(factory, upb_fielddef_msgsubdef(f)); + field->submsg_index = submsg_count++; + submsgs[field->submsg_index] = sub_layout; + } - if (ret) { - upb_array_init(ret, type, a); + if (upb_fielddef_haspresence(f) && !upb_fielddef_containingoneof(f)) { + field->presence = (hasbit++); + } else { + field->presence = 0; + } } - return ret; -} - -void upb_array_free(upb_array *arr) { - upb_array_uninit(arr); - upb_free(arr->alloc, arr); -} + /* Account for space used by hasbits. */ + l->size = div_round_up(hasbit, 8); -size_t upb_array_size(const upb_array *arr) { - return arr->len; -} + /* Allocate non-oneof fields. */ + for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it); + upb_msg_field_next(&it)) { + const upb_fielddef* f = upb_msg_iter_field(&it); + size_t field_size = upb_msg_fielddefsize(f); + size_t index = upb_fielddef_index(f); -upb_fieldtype_t upb_array_type(const upb_array *arr) { - return arr->type; -} + if (upb_fielddef_containingoneof(f)) { + /* Oneofs are handled separately below. */ + continue; + } -upb_msgval upb_array_get(const upb_array *arr, size_t i) { - UPB_ASSERT(i < arr->len); - return upb_msgval_read(arr->data, i * arr->element_size, arr->element_size); -} + fields[index].offset = upb_msglayout_place(l, field_size); + } -bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) { - UPB_ASSERT(i <= arr->len); + /* Allocate oneof fields. Each oneof field consists of a uint32 for the case + * and space for the actual data. */ + for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit); + upb_msg_oneof_next(&oit)) { + const upb_oneofdef* o = upb_msg_iter_oneof(&oit); + upb_oneof_iter fit; - if (i == arr->len) { - /* Extending the array. */ + size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */ + size_t field_size = 0; + uint32_t case_offset; + uint32_t data_offset; - if (i == arr->size) { - /* Need to reallocate. */ - size_t new_size = UPB_MAX(arr->size * 2, 8); - size_t new_bytes = new_size * arr->element_size; - size_t old_bytes = arr->size * arr->element_size; - upb_msgval *new_data = - upb_realloc(arr->alloc, arr->data, old_bytes, new_bytes); + /* Calculate field size: the max of all field sizes. */ + for (upb_oneof_begin(&fit, o); + !upb_oneof_done(&fit); + upb_oneof_next(&fit)) { + const upb_fielddef* f = upb_oneof_iter_field(&fit); + field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f)); + } - if (!new_data) { - return false; - } + /* Align and allocate case offset. */ + case_offset = upb_msglayout_place(l, case_size); + data_offset = upb_msglayout_place(l, field_size); - arr->data = new_data; - arr->size = new_size; + for (upb_oneof_begin(&fit, o); + !upb_oneof_done(&fit); + upb_oneof_next(&fit)) { + const upb_fielddef* f = upb_oneof_iter_field(&fit); + fields[upb_fielddef_index(f)].offset = data_offset; + fields[upb_fielddef_index(f)].presence = ~case_offset; } - - arr->len = i + 1; } - upb_msgval_write(arr->data, i * arr->element_size, val, arr->element_size); + /* Size of the entire structure should be a multiple of its greatest + * alignment. TODO: track overall alignment for real? */ + l->size = align_up(l->size, 8); + return true; } -/** upb_map *******************************************************************/ +/** upb_msgfactory ************************************************************/ -struct upb_map { - upb_fieldtype_t key_type; - upb_fieldtype_t val_type; - /* We may want to optimize this to use inttable where possible, for greater - * efficiency and lower memory footprint. */ - upb_strtable strtab; - upb_alloc *alloc; +struct upb_msgfactory { + const upb_symtab *symtab; /* We own a ref. */ + upb_inttable layouts; }; -static void upb_map_tokey(upb_fieldtype_t type, upb_msgval *key, - const char **out_key, size_t *out_len) { - switch (type) { - case UPB_TYPE_STRING: - /* Point to string data of the input key. */ - *out_key = key->str.data; - *out_len = key->str.size; - return; - case UPB_TYPE_BOOL: - case UPB_TYPE_INT32: - case UPB_TYPE_UINT32: - case UPB_TYPE_INT64: - case UPB_TYPE_UINT64: - /* Point to the key itself. XXX: big-endian. */ - *out_key = (const char*)key; - *out_len = upb_msgval_sizeof(type); - return; - case UPB_TYPE_BYTES: - case UPB_TYPE_DOUBLE: - case UPB_TYPE_ENUM: - case UPB_TYPE_FLOAT: - case UPB_TYPE_MESSAGE: - break; /* Cannot be a map key. */ - } - UPB_UNREACHABLE(); -} +upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) { + upb_msgfactory *ret = upb_gmalloc(sizeof(*ret)); -static upb_msgval upb_map_fromkey(upb_fieldtype_t type, const char *key, - size_t len) { - switch (type) { - case UPB_TYPE_STRING: - return upb_msgval_makestr(key, len); - case UPB_TYPE_BOOL: - case UPB_TYPE_INT32: - case UPB_TYPE_UINT32: - case UPB_TYPE_INT64: - case UPB_TYPE_UINT64: - return upb_msgval_read(key, 0, upb_msgval_sizeof(type)); - case UPB_TYPE_BYTES: - case UPB_TYPE_DOUBLE: - case UPB_TYPE_ENUM: - case UPB_TYPE_FLOAT: - case UPB_TYPE_MESSAGE: - break; /* Cannot be a map key. */ - } - UPB_UNREACHABLE(); -} + ret->symtab = symtab; + upb_inttable_init(&ret->layouts, UPB_CTYPE_PTR); -size_t upb_map_sizeof(upb_fieldtype_t ktype, upb_fieldtype_t vtype) { - /* Size does not currently depend on key/value type. */ - UPB_UNUSED(ktype); - UPB_UNUSED(vtype); - return sizeof(upb_map); + return ret; } -bool upb_map_init(upb_map *map, upb_fieldtype_t ktype, upb_fieldtype_t vtype, - upb_alloc *a) { - upb_ctype_t vtabtype = upb_fieldtotabtype(vtype); - UPB_ASSERT(upb_fieldtype_mapkeyok(ktype)); - map->key_type = ktype; - map->val_type = vtype; - map->alloc = a; - - if (!upb_strtable_init2(&map->strtab, vtabtype, a)) { - return false; +void upb_msgfactory_free(upb_msgfactory *f) { + upb_inttable_iter i; + upb_inttable_begin(&i, &f->layouts); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + upb_msglayout *l = upb_value_getptr(upb_inttable_iter_value(&i)); + upb_msglayout_free(l); } - return true; + upb_inttable_uninit(&f->layouts); + upb_gfree(f); } -void upb_map_uninit(upb_map *map) { - upb_strtable_uninit2(&map->strtab, map->alloc); +const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f) { + return f->symtab; } -upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype, - upb_alloc *a) { - upb_map *map = upb_malloc(a, upb_map_sizeof(ktype, vtype)); +const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, + const upb_msgdef *m) { + upb_value v; + UPB_ASSERT(upb_symtab_lookupmsg(f->symtab, upb_msgdef_fullname(m)) == m); + UPB_ASSERT(!upb_msgdef_mapentry(m)); - if (!map) { - return NULL; + if (upb_inttable_lookupptr(&f->layouts, m, &v)) { + UPB_ASSERT(upb_value_getptr(v)); + return upb_value_getptr(v); + } else { + /* In case of circular dependency, layout has to be inserted first. */ + upb_msglayout *l = upb_gmalloc(sizeof(*l)); + upb_msgfactory *mutable_f = (void*)f; + upb_inttable_insertptr(&mutable_f->layouts, m, upb_value_ptr(l)); + UPB_ASSERT(l); + if (!upb_msglayout_init(m, l, f)) { + upb_msglayout_free(l); + } + return l; } +} - if (!upb_map_init(map, ktype, vtype, a)) { - return NULL; - } +#ifndef UINTPTR_MAX +#error must include stdint.h first +#endif - return map; -} +#if UINTPTR_MAX == 0xffffffff +#define UPB_SIZE(size32, size64) size32 +#else +#define UPB_SIZE(size32, size64) size64 +#endif -void upb_map_free(upb_map *map) { - upb_map_uninit(map); - upb_free(map->alloc, map); -} +#define UPB_FIELD_AT(msg, fieldtype, offset) \ + *(fieldtype*)((const char*)(msg) + offset) -size_t upb_map_size(const upb_map *map) { - return upb_strtable_count(&map->strtab); -} +#define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \ + UPB_FIELD_AT(msg, int, case_offset) == case_val \ + ? UPB_FIELD_AT(msg, fieldtype, offset) \ + : default -upb_fieldtype_t upb_map_keytype(const upb_map *map) { - return map->key_type; -} +#define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \ + UPB_FIELD_AT(msg, int, case_offset) = case_val; \ + UPB_FIELD_AT(msg, fieldtype, offset) = value; -upb_fieldtype_t upb_map_valuetype(const upb_map *map) { - return map->val_type; -} +#undef UPB_SIZE +#undef UPB_FIELD_AT +#undef UPB_READ_ONEOF +#undef UPB_WRITE_ONEOF -bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) { - upb_value tabval; - const char *key_str; - size_t key_len; - bool ret; - upb_map_tokey(map->key_type, &key, &key_str, &key_len); - ret = upb_strtable_lookup2(&map->strtab, key_str, key_len, &tabval); +bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink) { + void *subc; + bool ret; + upb_bufhandle handle = UPB_BUFHANDLE_INIT; + handle.buf = buf; + ret = upb_bytessink_start(sink, len, &subc); + if (ret && len != 0) { + ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len); + } if (ret) { - memcpy(val, &tabval, sizeof(tabval)); + ret = upb_bytessink_end(sink); } - return ret; } +/* +** upb_table Implementation +** +** Implementation is heavily inspired by Lua's ltable.c. +*/ -bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val, - upb_msgval *removed) { - const char *key_str; - size_t key_len; - upb_value tabval = upb_toval(val); - upb_value removedtabval; - upb_alloc *a = map->alloc; - - upb_map_tokey(map->key_type, &key, &key_str, &key_len); - /* TODO(haberman): add overwrite operation to minimize number of lookups. */ - if (upb_strtable_lookup2(&map->strtab, key_str, key_len, NULL)) { - upb_strtable_remove3(&map->strtab, key_str, key_len, &removedtabval, a); - memcpy(&removed, &removedtabval, sizeof(removed)); - } +#include - return upb_strtable_insert3(&map->strtab, key_str, key_len, tabval, a); -} +#define UPB_MAXARRSIZE 16 /* 64k. */ -bool upb_map_del(upb_map *map, upb_msgval key) { - const char *key_str; - size_t key_len; - upb_alloc *a = map->alloc; +/* From Chromium. */ +#define ARRAY_SIZE(x) \ + ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) - upb_map_tokey(map->key_type, &key, &key_str, &key_len); - return upb_strtable_remove3(&map->strtab, key_str, key_len, NULL, a); +static void upb_check_alloc(upb_table *t, upb_alloc *a) { + UPB_UNUSED(t); + UPB_UNUSED(a); + UPB_ASSERT_DEBUGVAR(t->alloc == a); } +static const double MAX_LOAD = 0.85; -/** upb_mapiter ***************************************************************/ +/* 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; -struct upb_mapiter { - upb_strtable_iter iter; - upb_fieldtype_t key_type; -}; +bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; } -size_t upb_mapiter_sizeof() { - return sizeof(upb_mapiter); +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); } -void upb_mapiter_begin(upb_mapiter *i, const upb_map *map) { - upb_strtable_begin(&i->iter, &map->strtab); - i->key_type = map->key_type; +char *upb_strdup(const char *s, upb_alloc *a) { + return upb_strdup2(s, strlen(s), a); } -upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a) { - upb_mapiter *ret = upb_malloc(a, upb_mapiter_sizeof()); +char *upb_strdup2(const char *s, size_t len, upb_alloc *a) { + size_t n; + char *p; - if (!ret) { - return NULL; + /* 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_malloc(a, n); + if (p) { + memcpy(p, s, len); + p[len] = 0; } - - upb_mapiter_begin(ret, t); - return ret; + return p; } -void upb_mapiter_free(upb_mapiter *i, upb_alloc *a) { - upb_free(a, i); -} +/* A type to represent the lookup key of either a strtable or an inttable. */ +typedef union { + uintptr_t num; + struct { + const char *str; + size_t len; + } str; +} lookupkey_t; -void upb_mapiter_next(upb_mapiter *i) { - upb_strtable_next(&i->iter); +static lookupkey_t strkey2(const char *str, size_t len) { + lookupkey_t k; + k.str.str = str; + k.str.len = len; + return k; } -bool upb_mapiter_done(const upb_mapiter *i) { - return upb_strtable_done(&i->iter); +static lookupkey_t intkey(uintptr_t key) { + lookupkey_t k; + k.num = key; + return k; } -upb_msgval upb_mapiter_key(const upb_mapiter *i) { - return upb_map_fromkey(i->key_type, upb_strtable_iter_key(&i->iter), - upb_strtable_iter_keylength(&i->iter)); -} +typedef uint32_t hashfunc_t(upb_tabkey key); +typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2); -upb_msgval upb_mapiter_value(const upb_mapiter *i) { - return upb_msgval_fromval(upb_strtable_iter_value(&i->iter)); -} +/* Base table (shared code) ***************************************************/ -void upb_mapiter_setdone(upb_mapiter *i) { - upb_strtable_iter_setdone(&i->iter); +/* For when we need to cast away const. */ +static upb_tabent *mutable_entries(upb_table *t) { + return (upb_tabent*)t->entries; } -bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2) { - return upb_strtable_iter_isequal(&i1->iter, &i2->iter); +static bool isfull(upb_table *t) { + if (upb_table_size(t) == 0) { + return true; + } else { + return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD; + } } +static bool init(upb_table *t, upb_ctype_t ctype, uint8_t size_lg2, + upb_alloc *a) { + size_t bytes; -/** Handlers for upb_msg ******************************************************/ - -typedef struct { - size_t offset; - int32_t hasbit; -} upb_msg_handlerdata; - -/* Fallback implementation if the handler is not specialized by the producer. */ -#define MSG_WRITER(type, ctype) \ - bool upb_msg_set ## type (void *c, const void *hd, ctype val) { \ - uint8_t *m = c; \ - const upb_msg_handlerdata *d = hd; \ - if (d->hasbit > 0) \ - *(uint8_t*)&m[d->hasbit / 8] |= 1 << (d->hasbit % 8); \ - *(ctype*)&m[d->offset] = val; \ - return true; \ - } \ - -MSG_WRITER(double, double) -MSG_WRITER(float, float) -MSG_WRITER(int32, int32_t) -MSG_WRITER(int64, int64_t) -MSG_WRITER(uint32, uint32_t) -MSG_WRITER(uint64, uint64_t) -MSG_WRITER(bool, bool) - -bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f, - size_t offset, int32_t hasbit) { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - bool ok; + t->count = 0; + t->ctype = ctype; + t->size_lg2 = size_lg2; + t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0; +#ifndef NDEBUG + t->alloc = a; +#endif + bytes = upb_table_size(t) * sizeof(upb_tabent); + if (bytes > 0) { + t->entries = upb_malloc(a, bytes); + if (!t->entries) return false; + memset(mutable_entries(t), 0, bytes); + } else { + t->entries = NULL; + } + return true; +} - upb_msg_handlerdata *d = upb_gmalloc(sizeof(*d)); - if (!d) return false; - d->offset = offset; - d->hasbit = hasbit; +static void uninit(upb_table *t, upb_alloc *a) { + upb_check_alloc(t, a); + upb_free(a, mutable_entries(t)); +} - upb_handlerattr_sethandlerdata(&attr, d); - upb_handlerattr_setalwaysok(&attr, true); - upb_handlers_addcleanup(h, d, upb_gfree); +static upb_tabent *emptyent(upb_table *t) { + upb_tabent *e = mutable_entries(t) + upb_table_size(t); + while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); } +} -#define TYPE(u, l) \ - case UPB_TYPE_##u: \ - ok = upb_handlers_set##l(h, f, upb_msg_set##l, &attr); break; +static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) { + return (upb_tabent*)upb_getentry(t, hash); +} - ok = false; +static const upb_tabent *findentry(const upb_table *t, lookupkey_t key, + uint32_t hash, eqlfunc_t *eql) { + const upb_tabent *e; - switch (upb_fielddef_type(f)) { - TYPE(INT64, int64); - TYPE(INT32, int32); - TYPE(ENUM, int32); - TYPE(UINT64, uint64); - TYPE(UINT32, uint32); - TYPE(DOUBLE, double); - TYPE(FLOAT, float); - TYPE(BOOL, bool); - default: UPB_ASSERT(false); break; + if (t->size_lg2 == 0) return NULL; + e = upb_getentry(t, hash); + if (upb_tabent_isempty(e)) return NULL; + while (1) { + if (eql(e->key, key)) return e; + if ((e = e->next) == NULL) return NULL; } -#undef TYPE - - upb_handlerattr_uninit(&attr); - return ok; } -bool upb_msg_getscalarhandlerdata(const upb_handlers *h, - upb_selector_t s, - upb_fieldtype_t *type, - size_t *offset, - int32_t *hasbit) { - const upb_msg_handlerdata *d; - upb_func *f = upb_handlers_gethandler(h, s); +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); +} - if ((upb_int64_handlerfunc*)f == upb_msg_setint64) { - *type = UPB_TYPE_INT64; - } else if ((upb_int32_handlerfunc*)f == upb_msg_setint32) { - *type = UPB_TYPE_INT32; - } else if ((upb_uint64_handlerfunc*)f == upb_msg_setuint64) { - *type = UPB_TYPE_UINT64; - } else if ((upb_uint32_handlerfunc*)f == upb_msg_setuint32) { - *type = UPB_TYPE_UINT32; - } else if ((upb_double_handlerfunc*)f == upb_msg_setdouble) { - *type = UPB_TYPE_DOUBLE; - } else if ((upb_float_handlerfunc*)f == upb_msg_setfloat) { - *type = UPB_TYPE_FLOAT; - } else if ((upb_bool_handlerfunc*)f == upb_msg_setbool) { - *type = UPB_TYPE_BOOL; +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) { + _upb_value_setval(v, e->val.val, t->ctype); + } + return true; } else { return false; } - - d = upb_handlers_gethandlerdata(h, s); - *offset = d->offset; - *hasbit = d->hasbit; - return true; } -/* -** upb::RefCounted Implementation -** -** Our key invariants are: -** 1. reference cycles never span groups -** 2. for ref2(to, from), we increment to's count iff group(from) != group(to) -** -** The previous two are how we avoid leaking cycles. Other important -** invariants are: -** 3. for mutable objects "from" and "to", if there exists a ref2(to, from) -** this implies group(from) == group(to). (In practice, what we implement -** is even stronger; "from" and "to" will share a group if there has *ever* -** been a ref2(to, from), but all that is necessary for correctness is the -** weaker one). -** 4. mutable and immutable objects are never in the same group. -*/ +/* The given key must not already exist in the table. */ +static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey, + upb_value val, uint32_t hash, + hashfunc_t *hashfunc, eqlfunc_t *eql) { + upb_tabent *mainpos_e; + upb_tabent *our_e; -#include + UPB_ASSERT(findentry(t, key, hash, eql) == NULL); + UPB_ASSERT_DEBUGVAR(val.ctype == t->ctype); -static void freeobj(upb_refcounted *o); + t->count++; + mainpos_e = getentry_mutable(t, hash); + our_e = mainpos_e; -const char untracked_val; -const void *UPB_UNTRACKED_REF = &untracked_val; + 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); + /* Head of collider's chain. */ + upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key)); + if (chain == mainpos_e) { + /* Existing ent is in its main posisiton (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 = val.val; + UPB_ASSERT(findentry(t, key, hash, eql) == our_e); +} -/* arch-specific atomic primitives *******************************************/ +static bool rm(upb_table *t, lookupkey_t key, upb_value *val, + upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) { + upb_tabent *chain = getentry_mutable(t, hash); + if (upb_tabent_isempty(chain)) return false; + if (eql(chain->key, key)) { + /* Element to remove is at the head of its chain. */ + t->count--; + if (val) _upb_value_setval(val, chain->val.val, t->ctype); + if (removed) *removed = chain->key; + if (chain->next) { + upb_tabent *move = (upb_tabent*)chain->next; + *chain = *move; + move->key = 0; /* Make the slot empty. */ + } else { + chain->key = 0; /* Make the slot 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, key)) { + chain = (upb_tabent*)chain->next; + } + if (chain->next) { + /* Found element to remove. */ + upb_tabent *rm = (upb_tabent*)chain->next; + t->count--; + if (val) _upb_value_setval(val, chain->next->val.val, t->ctype); + if (removed) *removed = rm->key; + rm->key = 0; /* Make the slot empty. */ + chain->next = rm->next; + return true; + } else { + /* Element to remove is not in the table. */ + return false; + } + } +} -#ifdef UPB_THREAD_UNSAFE /*---------------------------------------------------*/ +static size_t next(const upb_table *t, size_t i) { + do { + if (++i >= upb_table_size(t)) + return SIZE_MAX; + } while(upb_tabent_isempty(&t->entries[i])); -static void atomic_inc(uint32_t *a) { (*a)++; } -static bool atomic_dec(uint32_t *a) { return --(*a) == 0; } + return i; +} -#elif defined(__GNUC__) || defined(__clang__) /*------------------------------*/ +static size_t begin(const upb_table *t) { + return next(t, -1); +} -static void atomic_inc(uint32_t *a) { __sync_fetch_and_add(a, 1); } -static bool atomic_dec(uint32_t *a) { return __sync_sub_and_fetch(a, 1) == 0; } -#elif defined(WIN32) /*-------------------------------------------------------*/ +/* upb_strtable ***************************************************************/ -#include +/* A simple "subclass" of upb_table that only adds a hash function for strings. */ -static void atomic_inc(upb_atomic_t *a) { InterlockedIncrement(&a->val); } -static bool atomic_dec(upb_atomic_t *a) { - return InterlockedDecrement(&a->val) == 0; +static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) { + uint32_t len = (uint32_t) k2.str.len; + char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1); + if (str == NULL) return 0; + memcpy(str, &len, sizeof(uint32_t)); + memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len + 1); + return (uintptr_t)str; } -#else -#error Atomic primitives not defined for your platform/CPU. \ - Implement them or compile with UPB_THREAD_UNSAFE. -#endif - -/* All static objects point to this refcount. - * It is special-cased in ref/unref below. */ -uint32_t static_refcount = -1; +static uint32_t strhash(upb_tabkey key) { + uint32_t len; + char *str = upb_tabstr(key, &len); + return MurmurHash2(str, len, 0); +} -/* We can avoid atomic ops for statically-declared objects. - * This is a minor optimization but nice since we can avoid degrading under - * contention in this case. */ +static bool streql(upb_tabkey k1, lookupkey_t k2) { + uint32_t len; + char *str = upb_tabstr(k1, &len); + return len == k2.str.len && memcmp(str, k2.str.str, len) == 0; +} -static void refgroup(uint32_t *group) { - if (group != &static_refcount) - atomic_inc(group); +bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) { + return init(&t->t, ctype, 2, a); } -static bool unrefgroup(uint32_t *group) { - if (group == &static_refcount) { - return false; - } else { - return atomic_dec(group); - } +void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) { + size_t i; + for (i = 0; i < upb_table_size(&t->t); i++) + upb_free(a, (void*)t->t.entries[i].key); + uninit(&t->t, a); } +bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) { + upb_strtable new_table; + upb_strtable_iter i; -/* Reference tracking (debug only) ********************************************/ + upb_check_alloc(&t->t, a); -#ifdef UPB_DEBUG_REFS - -#ifdef UPB_THREAD_UNSAFE - -static void upb_lock() {} -static void upb_unlock() {} - -#else - -/* User must define functions that lock/unlock a global mutex and link this - * file against them. */ -void upb_lock(); -void upb_unlock(); - -#endif - -/* UPB_DEBUG_REFS mode counts on being able to malloc() memory in some - * code-paths that can normally never fail, like upb_refcounted_ref(). Since - * we have no way to propagage out-of-memory errors back to the user, and since - * these errors can only occur in UPB_DEBUG_REFS mode, we use an allocator that - * immediately aborts on failure (avoiding the global allocator, which might - * inject failures). */ - -#include - -static void *upb_debugrefs_allocfunc(upb_alloc *alloc, void *ptr, - size_t oldsize, size_t size) { - UPB_UNUSED(alloc); - UPB_UNUSED(oldsize); - if (size == 0) { - free(ptr); - return NULL; - } else { - void *ret = realloc(ptr, size); - - if (!ret) { - abort(); - } - - return ret; - } -} - -upb_alloc upb_alloc_debugrefs = {&upb_debugrefs_allocfunc}; - -typedef struct { - int count; /* How many refs there are (duplicates only allowed for ref2). */ - bool is_ref2; -} trackedref; - -static trackedref *trackedref_new(bool is_ref2) { - trackedref *ret = upb_malloc(&upb_alloc_debugrefs, sizeof(*ret)); - ret->count = 1; - ret->is_ref2 = is_ref2; - return ret; -} - -static void track(const upb_refcounted *r, const void *owner, bool ref2) { - upb_value v; - - UPB_ASSERT(owner); - if (owner == UPB_UNTRACKED_REF) return; - - upb_lock(); - if (upb_inttable_lookupptr(r->refs, owner, &v)) { - trackedref *ref = upb_value_getptr(v); - /* Since we allow multiple ref2's for the same to/from pair without - * allocating separate memory for each one, we lose the fine-grained - * tracking behavior we get with regular refs. Since ref2s only happen - * inside upb, we'll accept this limitation until/unless there is a really - * difficult upb-internal bug that can't be figured out without it. */ - UPB_ASSERT(ref2); - UPB_ASSERT(ref->is_ref2); - ref->count++; - } else { - trackedref *ref = trackedref_new(ref2); - upb_inttable_insertptr2(r->refs, owner, upb_value_ptr(ref), - &upb_alloc_debugrefs); - if (ref2) { - /* We know this cast is safe when it is a ref2, because it's coming from - * another refcounted object. */ - const upb_refcounted *from = owner; - UPB_ASSERT(!upb_inttable_lookupptr(from->ref2s, r, NULL)); - upb_inttable_insertptr2(from->ref2s, r, upb_value_ptr(NULL), - &upb_alloc_debugrefs); - } + if (!init(&new_table.t, t->t.ctype, size_lg2, a)) + return false; + upb_strtable_begin(&i, t); + for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) { + upb_strtable_insert3( + &new_table, + upb_strtable_iter_key(&i), + upb_strtable_iter_keylength(&i), + upb_strtable_iter_value(&i), + a); } - upb_unlock(); + upb_strtable_uninit2(t, a); + *t = new_table; + return true; } -static void untrack(const upb_refcounted *r, const void *owner, bool ref2) { - upb_value v; - bool found; - trackedref *ref; +bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len, + upb_value v, upb_alloc *a) { + lookupkey_t key; + upb_tabkey tabkey; + uint32_t hash; - UPB_ASSERT(owner); - if (owner == UPB_UNTRACKED_REF) return; + upb_check_alloc(&t->t, a); - upb_lock(); - found = upb_inttable_lookupptr(r->refs, owner, &v); - /* This assert will fail if an owner attempts to release a ref it didn't have. */ - UPB_ASSERT(found); - ref = upb_value_getptr(v); - UPB_ASSERT(ref->is_ref2 == ref2); - if (--ref->count == 0) { - free(ref); - upb_inttable_removeptr(r->refs, owner, NULL); - if (ref2) { - /* We know this cast is safe when it is a ref2, because it's coming from - * another refcounted object. */ - const upb_refcounted *from = owner; - bool removed = upb_inttable_removeptr(from->ref2s, r, NULL); - UPB_ASSERT(removed); + if (isfull(&t->t)) { + /* Need to resize. New table of double the size, add old elements to it. */ + if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) { + return false; } } - upb_unlock(); -} -static void checkref(const upb_refcounted *r, const void *owner, bool ref2) { - upb_value v; - bool found; - trackedref *ref; + key = strkey2(k, len); + tabkey = strcopy(key, a); + if (tabkey == 0) return false; - upb_lock(); - found = upb_inttable_lookupptr(r->refs, owner, &v); - UPB_ASSERT(found); - ref = upb_value_getptr(v); - UPB_ASSERT(ref->is_ref2 == ref2); - upb_unlock(); + hash = MurmurHash2(key.str.str, key.str.len, 0); + insert(&t->t, key, tabkey, v, hash, &strhash, &streql); + return true; } -/* Populates the given UPB_CTYPE_INT32 inttable with counts of ref2's that - * originate from the given owner. */ -static void getref2s(const upb_refcounted *owner, upb_inttable *tab) { - upb_inttable_iter i; - - upb_lock(); - upb_inttable_begin(&i, owner->ref2s); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_value v; - upb_value count; - trackedref *ref; - bool found; - - upb_refcounted *to = (upb_refcounted*)upb_inttable_iter_key(&i); - - /* To get the count we need to look in the target's table. */ - found = upb_inttable_lookupptr(to->refs, owner, &v); - UPB_ASSERT(found); - ref = upb_value_getptr(v); - count = upb_value_int32(ref->count); - - upb_inttable_insertptr2(tab, to, count, &upb_alloc_debugrefs); - } - upb_unlock(); +bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len, + upb_value *v) { + uint32_t hash = MurmurHash2(key, len, 0); + return lookup(&t->t, strkey2(key, len), v, hash, &streql); } -typedef struct { - upb_inttable ref2; - const upb_refcounted *obj; -} check_state; - -static void visit_check(const upb_refcounted *obj, const upb_refcounted *subobj, - void *closure) { - check_state *s = closure; - upb_inttable *ref2 = &s->ref2; - upb_value v; - bool removed; - int32_t newcount; - - UPB_ASSERT(obj == s->obj); - UPB_ASSERT(subobj); - removed = upb_inttable_removeptr(ref2, subobj, &v); - /* The following assertion will fail if the visit() function visits a subobj - * that it did not have a ref2 on, or visits the same subobj too many times. */ - UPB_ASSERT(removed); - newcount = upb_value_getint32(v) - 1; - if (newcount > 0) { - upb_inttable_insert2(ref2, (uintptr_t)subobj, upb_value_int32(newcount), - &upb_alloc_debugrefs); +bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len, + upb_value *val, upb_alloc *alloc) { + uint32_t hash = MurmurHash2(key, len, 0); + upb_tabkey tabkey; + if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) { + upb_free(alloc, (void*)tabkey); + return true; + } else { + return false; } } -static void visit(const upb_refcounted *r, upb_refcounted_visit *v, - void *closure) { - /* In DEBUG_REFS mode we know what existing ref2 refs there are, so we know - * exactly the set of nodes that visit() should visit. So we verify visit()'s - * correctness here. */ - check_state state; - state.obj = r; - upb_inttable_init2(&state.ref2, UPB_CTYPE_INT32, &upb_alloc_debugrefs); - getref2s(r, &state.ref2); - - /* This should visit any children in the ref2 table. */ - if (r->vtbl->visit) r->vtbl->visit(r, visit_check, &state); +/* Iteration */ - /* This assertion will fail if the visit() function missed any children. */ - UPB_ASSERT(upb_inttable_count(&state.ref2) == 0); - upb_inttable_uninit2(&state.ref2, &upb_alloc_debugrefs); - if (r->vtbl->visit) r->vtbl->visit(r, v, closure); +static const upb_tabent *str_tabent(const upb_strtable_iter *i) { + return &i->t->t.entries[i->index]; } -static void trackinit(upb_refcounted *r) { - r->refs = upb_malloc(&upb_alloc_debugrefs, sizeof(*r->refs)); - r->ref2s = upb_malloc(&upb_alloc_debugrefs, sizeof(*r->ref2s)); - upb_inttable_init2(r->refs, UPB_CTYPE_PTR, &upb_alloc_debugrefs); - upb_inttable_init2(r->ref2s, UPB_CTYPE_PTR, &upb_alloc_debugrefs); +void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) { + i->t = t; + i->index = begin(&t->t); } -static void trackfree(const upb_refcounted *r) { - upb_inttable_uninit2(r->refs, &upb_alloc_debugrefs); - upb_inttable_uninit2(r->ref2s, &upb_alloc_debugrefs); - upb_free(&upb_alloc_debugrefs, r->refs); - upb_free(&upb_alloc_debugrefs, r->ref2s); +void upb_strtable_next(upb_strtable_iter *i) { + i->index = next(&i->t->t, i->index); } -#else - -static void track(const upb_refcounted *r, const void *owner, bool ref2) { - UPB_UNUSED(r); - UPB_UNUSED(owner); - UPB_UNUSED(ref2); +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)); } -static void untrack(const upb_refcounted *r, const void *owner, bool ref2) { - UPB_UNUSED(r); - UPB_UNUSED(owner); - UPB_UNUSED(ref2); +const char *upb_strtable_iter_key(const upb_strtable_iter *i) { + UPB_ASSERT(!upb_strtable_done(i)); + return upb_tabstr(str_tabent(i)->key, NULL); } -static void checkref(const upb_refcounted *r, const void *owner, bool ref2) { - UPB_UNUSED(r); - UPB_UNUSED(owner); - UPB_UNUSED(ref2); +size_t upb_strtable_iter_keylength(const upb_strtable_iter *i) { + uint32_t len; + UPB_ASSERT(!upb_strtable_done(i)); + upb_tabstr(str_tabent(i)->key, &len); + return len; } -static void trackinit(upb_refcounted *r) { - UPB_UNUSED(r); +upb_value upb_strtable_iter_value(const upb_strtable_iter *i) { + UPB_ASSERT(!upb_strtable_done(i)); + return _upb_value_val(str_tabent(i)->val.val, i->t->t.ctype); } -static void trackfree(const upb_refcounted *r) { - UPB_UNUSED(r); +void upb_strtable_iter_setdone(upb_strtable_iter *i) { + i->t = NULL; + i->index = SIZE_MAX; } -static void visit(const upb_refcounted *r, upb_refcounted_visit *v, - void *closure) { - if (r->vtbl->visit) r->vtbl->visit(r, v, closure); +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; } -#endif /* UPB_DEBUG_REFS */ - - -/* freeze() *******************************************************************/ - -/* The freeze() operation is by far the most complicated part of this scheme. - * We compute strongly-connected components and then mutate the graph such that - * we preserve the invariants documented at the top of this file. And we must - * handle out-of-memory errors gracefully (without leaving the graph - * inconsistent), which adds to the fun. */ - -/* The state used by the freeze operation (shared across many functions). */ -typedef struct { - int depth; - int maxdepth; - uint64_t index; - /* Maps upb_refcounted* -> attributes (color, etc). attr layout varies by - * color. */ - upb_inttable objattr; - upb_inttable stack; /* stack of upb_refcounted* for Tarjan's algorithm. */ - upb_inttable groups; /* array of uint32_t*, malloc'd refcounts for new groups */ - upb_status *status; - jmp_buf err; -} tarjan; - -static void release_ref2(const upb_refcounted *obj, - const upb_refcounted *subobj, - void *closure); - -/* Node attributes -----------------------------------------------------------*/ -/* After our analysis phase all nodes will be either GRAY or WHITE. */ +/* upb_inttable ***************************************************************/ -typedef enum { - BLACK = 0, /* Object has not been seen. */ - GRAY, /* Object has been found via a refgroup but may not be reachable. */ - GREEN, /* Object is reachable and is currently on the Tarjan stack. */ - WHITE /* Object is reachable and has been assigned a group (SCC). */ -} color_t; +/* For inttables we use a hybrid structure where small keys are kept in an + * array and large keys are put in the hash table. */ -UPB_NORETURN static void err(tarjan *t) { longjmp(t->err, 1); } -UPB_NORETURN static void oom(tarjan *t) { - upb_status_seterrmsg(t->status, "out of memory"); - err(t); -} +static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); } -static uint64_t trygetattr(const tarjan *t, const upb_refcounted *r) { - upb_value v; - return upb_inttable_lookupptr(&t->objattr, r, &v) ? - upb_value_getuint64(v) : 0; +static bool inteql(upb_tabkey k1, lookupkey_t k2) { + return k1 == k2.num; } -static uint64_t getattr(const tarjan *t, const upb_refcounted *r) { - upb_value v; - bool found = upb_inttable_lookupptr(&t->objattr, r, &v); - UPB_ASSERT(found); - return upb_value_getuint64(v); +static upb_tabval *mutable_array(upb_inttable *t) { + return (upb_tabval*)t->array; } -static void setattr(tarjan *t, const upb_refcounted *r, uint64_t attr) { - upb_inttable_removeptr(&t->objattr, r, NULL); - upb_inttable_insertptr(&t->objattr, r, upb_value_uint64(attr)); +static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) { + if (key < t->array_size) { + return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL; + } else { + upb_tabent *e = + findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql); + return e ? &e->val : NULL; + } } -static color_t color(tarjan *t, const upb_refcounted *r) { - return trygetattr(t, r) & 0x3; /* Color is always stored in the low 2 bits. */ +static const upb_tabval *inttable_val_const(const upb_inttable *t, + uintptr_t key) { + return inttable_val((upb_inttable*)t, key); } -static void set_gray(tarjan *t, const upb_refcounted *r) { - UPB_ASSERT(color(t, r) == BLACK); - setattr(t, r, GRAY); +size_t upb_inttable_count(const upb_inttable *t) { + return t->t.count + t->array_count; } -/* Pushes an obj onto the Tarjan stack and sets it to GREEN. */ -static void push(tarjan *t, const upb_refcounted *r) { - UPB_ASSERT(color(t, r) == BLACK || color(t, r) == GRAY); - /* This defines the attr layout for the GREEN state. "index" and "lowlink" - * get 31 bits, which is plenty (limit of 2B objects frozen at a time). */ - setattr(t, r, GREEN | (t->index << 2) | (t->index << 33)); - if (++t->index == 0x80000000) { - upb_status_seterrmsg(t->status, "too many objects to freeze"); - err(t); +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; + upb_inttable_iter i; + upb_inttable_begin(&i, t); + for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) { + UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL)); + } + UPB_ASSERT(count == upb_inttable_count(t)); } - upb_inttable_push(&t->stack, upb_value_ptr((void*)r)); +#endif } -/* Pops an obj from the Tarjan stack and sets it to WHITE, with a ptr to its - * SCC group. */ -static upb_refcounted *pop(tarjan *t) { - upb_refcounted *r = upb_value_getptr(upb_inttable_pop(&t->stack)); - UPB_ASSERT(color(t, r) == GREEN); - /* This defines the attr layout for nodes in the WHITE state. - * Top of group stack is [group, NULL]; we point at group. */ - setattr(t, r, WHITE | (upb_inttable_count(&t->groups) - 2) << 8); - return r; -} +bool upb_inttable_sizedinit(upb_inttable *t, upb_ctype_t ctype, + size_t asize, int hsize_lg2, upb_alloc *a) { + size_t array_bytes; -static void tarjan_newgroup(tarjan *t) { - uint32_t *group = upb_gmalloc(sizeof(*group)); - if (!group) oom(t); - /* Push group and empty group leader (we'll fill in leader later). */ - if (!upb_inttable_push(&t->groups, upb_value_ptr(group)) || - !upb_inttable_push(&t->groups, upb_value_ptr(NULL))) { - upb_gfree(group); - oom(t); + if (!init(&t->t, ctype, 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; + array_bytes = t->array_size * sizeof(upb_value); + t->array = upb_malloc(a, array_bytes); + if (!t->array) { + uninit(&t->t, a); + return false; } - *group = 0; -} - -static uint32_t idx(tarjan *t, const upb_refcounted *r) { - UPB_ASSERT(color(t, r) == GREEN); - return (getattr(t, r) >> 2) & 0x7FFFFFFF; + memset(mutable_array(t), 0xff, array_bytes); + check(t); + return true; } -static uint32_t lowlink(tarjan *t, const upb_refcounted *r) { - if (color(t, r) == GREEN) { - return getattr(t, r) >> 33; - } else { - return UINT32_MAX; - } +bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) { + return upb_inttable_sizedinit(t, ctype, 0, 4, a); } -static void set_lowlink(tarjan *t, const upb_refcounted *r, uint32_t lowlink) { - UPB_ASSERT(color(t, r) == GREEN); - setattr(t, r, ((uint64_t)lowlink << 33) | (getattr(t, r) & 0x1FFFFFFFF)); +void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) { + uninit(&t->t, a); + upb_free(a, mutable_array(t)); } -static uint32_t *group(tarjan *t, upb_refcounted *r) { - uint64_t groupnum; - upb_value v; - bool found; - - UPB_ASSERT(color(t, r) == WHITE); - groupnum = getattr(t, r) >> 8; - found = upb_inttable_lookup(&t->groups, groupnum, &v); - UPB_ASSERT(found); - return upb_value_getptr(v); -} +bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val, + upb_alloc *a) { + upb_tabval tabval; + tabval.val = val.val; + UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */ -/* If the group leader for this object's group has not previously been set, - * the given object is assigned to be its leader. */ -static upb_refcounted *groupleader(tarjan *t, upb_refcounted *r) { - uint64_t leader_slot; - upb_value v; - bool found; + upb_check_alloc(&t->t, a); - UPB_ASSERT(color(t, r) == WHITE); - leader_slot = (getattr(t, r) >> 8) + 1; - found = upb_inttable_lookup(&t->groups, leader_slot, &v); - UPB_ASSERT(found); - if (upb_value_getptr(v)) { - return upb_value_getptr(v); + if (key < t->array_size) { + UPB_ASSERT(!upb_arrhas(t->array[key])); + t->array_count++; + mutable_array(t)[key].val = val.val; } else { - upb_inttable_remove(&t->groups, leader_slot, NULL); - upb_inttable_insert(&t->groups, leader_slot, upb_value_ptr(r)); - return r; - } -} - + if (isfull(&t->t)) { + /* Need to resize the hash part, but we re-use the array part. */ + size_t i; + upb_table new_table; -/* Tarjan's algorithm --------------------------------------------------------*/ + if (!init(&new_table, t->t.ctype, t->t.size_lg2 + 1, a)) { + return false; + } -/* See: - * http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm */ -static void do_tarjan(const upb_refcounted *obj, tarjan *t); + 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; + upb_value v; -static void tarjan_visit(const upb_refcounted *obj, - const upb_refcounted *subobj, - void *closure) { - tarjan *t = closure; - if (++t->depth > t->maxdepth) { - upb_status_seterrf(t->status, "graph too deep to freeze (%d)", t->maxdepth); - err(t); - } else if (subobj->is_frozen || color(t, subobj) == WHITE) { - /* Do nothing: we don't want to visit or color already-frozen nodes, - * and WHITE nodes have already been assigned a SCC. */ - } else if (color(t, subobj) < GREEN) { - /* Subdef has not yet been visited; recurse on it. */ - do_tarjan(subobj, t); - set_lowlink(t, obj, UPB_MIN(lowlink(t, obj), lowlink(t, subobj))); - } else if (color(t, subobj) == GREEN) { - /* Subdef is in the stack and hence in the current SCC. */ - set_lowlink(t, obj, UPB_MIN(lowlink(t, obj), idx(t, subobj))); - } - --t->depth; -} + _upb_value_setval(&v, e->val.val, t->t.ctype); + hash = upb_inthash(e->key); + insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql); + } -static void do_tarjan(const upb_refcounted *obj, tarjan *t) { - if (color(t, obj) == BLACK) { - /* We haven't seen this object's group; mark the whole group GRAY. */ - const upb_refcounted *o = obj; - do { set_gray(t, o); } while ((o = o->next) != obj); - } + UPB_ASSERT(t->t.count == new_table.count); - push(t, obj); - visit(obj, tarjan_visit, t); - if (lowlink(t, obj) == idx(t, obj)) { - tarjan_newgroup(t); - while (pop(t) != obj) - ; + uninit(&t->t, a); + t->t = new_table; + } + insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql); } + check(t); + return true; } - -/* freeze() ------------------------------------------------------------------*/ - -static void crossref(const upb_refcounted *r, const upb_refcounted *subobj, - void *_t) { - tarjan *t = _t; - UPB_ASSERT(color(t, r) > BLACK); - if (color(t, subobj) > BLACK && r->group != subobj->group) { - /* Previously this ref was not reflected in subobj->group because they - * were in the same group; now that they are split a ref must be taken. */ - refgroup(subobj->group); - } +bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) { + const upb_tabval *table_v = inttable_val_const(t, key); + if (!table_v) return false; + if (v) _upb_value_setval(v, table_v->val, t->t.ctype); + return true; } -static bool freeze(upb_refcounted *const*roots, int n, upb_status *s, - int maxdepth) { - volatile bool ret = false; - int i; - upb_inttable_iter iter; - - /* We run in two passes so that we can allocate all memory before performing - * any mutation of the input -- this allows us to leave the input unchanged - * in the case of memory allocation failure. */ - tarjan t; - t.index = 0; - t.depth = 0; - t.maxdepth = maxdepth; - t.status = s; - if (!upb_inttable_init(&t.objattr, UPB_CTYPE_UINT64)) goto err1; - if (!upb_inttable_init(&t.stack, UPB_CTYPE_PTR)) goto err2; - if (!upb_inttable_init(&t.groups, UPB_CTYPE_PTR)) goto err3; - if (setjmp(t.err) != 0) goto err4; - - - for (i = 0; i < n; i++) { - if (color(&t, roots[i]) < GREEN) { - do_tarjan(roots[i], &t); - } - } - - /* If we've made it this far, no further errors are possible so it's safe to - * mutate the objects without risk of leaving them in an inconsistent state. */ - ret = true; - - /* The transformation that follows requires care. The preconditions are: - * - all objects in attr map are WHITE or GRAY, and are in mutable groups - * (groups of all mutable objs) - * - no ref2(to, from) refs have incremented count(to) if both "to" and - * "from" are in our attr map (this follows from invariants (2) and (3)) */ - - /* Pass 1: we remove WHITE objects from their mutable groups, and add them to - * new groups according to the SCC's we computed. These new groups will - * consist of only frozen objects. None will be immediately collectible, - * because WHITE objects are by definition reachable from one of "roots", - * which the caller must own refs on. */ - upb_inttable_begin(&iter, &t.objattr); - for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) { - upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter); - /* Since removal from a singly-linked list requires access to the object's - * predecessor, we consider obj->next instead of obj for moving. With the - * while() loop we guarantee that we will visit every node's predecessor. - * Proof: - * 1. every node's predecessor is in our attr map. - * 2. though the loop body may change a node's predecessor, it will only - * change it to be the node we are currently operating on, so with a - * while() loop we guarantee ourselves the chance to remove each node. */ - while (color(&t, obj->next) == WHITE && - group(&t, obj->next) != obj->next->group) { - upb_refcounted *leader; - - /* Remove from old group. */ - upb_refcounted *move = obj->next; - if (obj == move) { - /* Removing the last object from a group. */ - UPB_ASSERT(*obj->group == obj->individual_count); - upb_gfree(obj->group); - } else { - obj->next = move->next; - /* This may decrease to zero; we'll collect GRAY objects (if any) that - * remain in the group in the third pass. */ - UPB_ASSERT(*move->group >= move->individual_count); - *move->group -= move->individual_count; - } - - /* Add to new group. */ - leader = groupleader(&t, move); - if (move == leader) { - /* First object added to new group is its leader. */ - move->group = group(&t, move); - move->next = move; - *move->group = move->individual_count; - } else { - /* Group already has at least one object in it. */ - UPB_ASSERT(leader->group == group(&t, move)); - move->group = group(&t, move); - move->next = leader->next; - leader->next = move; - *move->group += move->individual_count; - } - - move->is_frozen = true; - } - } - - /* Pass 2: GRAY and WHITE objects "obj" with ref2(to, obj) references must - * increment count(to) if group(obj) != group(to) (which could now be the - * case if "to" was just frozen). */ - upb_inttable_begin(&iter, &t.objattr); - for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) { - upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter); - visit(obj, crossref, &t); - } +bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) { + upb_tabval *table_v = inttable_val(t, key); + if (!table_v) return false; + table_v->val = val.val; + return true; +} - /* Pass 3: GRAY objects are collected if their group's refcount dropped to - * zero when we removed its white nodes. This can happen if they had only - * been kept alive by virtue of sharing a group with an object that was just - * frozen. - * - * It is important that we do this last, since the GRAY object's free() - * function could call unref2() on just-frozen objects, which will decrement - * refs that were added in pass 2. */ - upb_inttable_begin(&iter, &t.objattr); - for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) { - upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter); - if (obj->group == NULL || *obj->group == 0) { - if (obj->group) { - upb_refcounted *o; - - /* We eagerly free() the group's count (since we can't easily determine - * the group's remaining size it's the easiest way to ensure it gets - * done). */ - upb_gfree(obj->group); - - /* Visit to release ref2's (done in a separate pass since release_ref2 - * depends on o->group being unmodified so it can test merged()). */ - o = obj; - do { visit(o, release_ref2, NULL); } while ((o = o->next) != obj); - - /* Mark "group" fields as NULL so we know to free the objects later in - * this loop, but also don't try to delete the group twice. */ - o = obj; - do { o->group = NULL; } while ((o = o->next) != obj); +bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) { + bool success; + if (key < t->array_size) { + if (upb_arrhas(t->array[key])) { + upb_tabval empty = UPB_TABVALUE_EMPTY_INIT; + t->array_count--; + if (val) { + _upb_value_setval(val, t->array[key].val, t->t.ctype); } - freeobj(obj); + mutable_array(t)[key] = empty; + success = true; + } else { + success = false; } + } else { + success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql); } + check(t); + return success; +} -err4: - if (!ret) { - upb_inttable_begin(&iter, &t.groups); - for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) - upb_gfree(upb_value_getptr(upb_inttable_iter_value(&iter))); - } - upb_inttable_uninit(&t.groups); -err3: - upb_inttable_uninit(&t.stack); -err2: - upb_inttable_uninit(&t.objattr); -err1: - return ret; +bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a) { + upb_check_alloc(&t->t, a); + return upb_inttable_insert2(t, upb_inttable_count(t), val, a); } +upb_value upb_inttable_pop(upb_inttable *t) { + upb_value val; + bool ok = upb_inttable_remove(t, upb_inttable_count(t) - 1, &val); + UPB_ASSERT(ok); + return val; +} -/* Misc internal functions ***************************************************/ +bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val, + upb_alloc *a) { + upb_check_alloc(&t->t, a); + return upb_inttable_insert2(t, (uintptr_t)key, val, a); +} -static bool merged(const upb_refcounted *r, const upb_refcounted *r2) { - return r->group == r2->group; +bool upb_inttable_lookupptr(const upb_inttable *t, const void *key, + upb_value *v) { + return upb_inttable_lookup(t, (uintptr_t)key, v); } -static void merge(upb_refcounted *r, upb_refcounted *from) { - upb_refcounted *base; - upb_refcounted *tmp; +bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) { + return upb_inttable_remove(t, (uintptr_t)key, val); +} - if (merged(r, from)) return; - *r->group += *from->group; - upb_gfree(from->group); - base = from; +void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) { + /* A power-of-two histogram of the table keys. */ + size_t counts[UPB_MAXARRSIZE + 1] = {0}; - /* Set all refcount pointers in the "from" chain to the merged refcount. - * - * TODO(haberman): this linear algorithm can result in an overall O(n^2) bound - * if the user continuously extends a group by one object. Prevent this by - * using one of the techniques in this paper: - * http://bioinfo.ict.ac.cn/~dbu/AlgorithmCourses/Lectures/Union-Find-Tarjan.pdf */ - do { from->group = r->group; } while ((from = from->next) != base); + /* The max key in each bucket. */ + uintptr_t max[UPB_MAXARRSIZE + 1] = {0}; - /* Merge the two circularly linked lists by swapping their next pointers. */ - tmp = r->next; - r->next = base->next; - base->next = tmp; -} + upb_inttable_iter i; + size_t arr_count; + int size_lg2; + upb_inttable new_t; -static void unref(const upb_refcounted *r); + upb_check_alloc(&t->t, a); -static void release_ref2(const upb_refcounted *obj, - const upb_refcounted *subobj, - void *closure) { - UPB_UNUSED(closure); - untrack(subobj, obj, true); - if (!merged(obj, subobj)) { - UPB_ASSERT(subobj->is_frozen); - unref(subobj); + upb_inttable_begin(&i, t); + for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { + uintptr_t key = upb_inttable_iter_key(&i); + int bucket = log2ceil(key); + max[bucket] = UPB_MAX(max[bucket], key); + counts[bucket]++; } -} - -static void unref(const upb_refcounted *r) { - if (unrefgroup(r->group)) { - const upb_refcounted *o; - upb_gfree(r->group); + /* Find the largest power of two that satisfies the MIN_DENSITY + * definition (while actually having some keys). */ + arr_count = upb_inttable_count(t); - /* In two passes, since release_ref2 needs a guarantee that any subobjs - * are alive. */ - o = r; - do { visit(o, release_ref2, NULL); } while((o = o->next) != r); + for (size_lg2 = ARRAY_SIZE(counts) - 1; 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; + } - o = r; - do { - const upb_refcounted *next = o->next; - UPB_ASSERT(o->is_frozen || o->individual_count == 0); - freeobj((upb_refcounted*)o); - o = next; - } while(o != r); + arr_count -= counts[size_lg2]; } -} -static void freeobj(upb_refcounted *o) { - trackfree(o); - o->vtbl->free((upb_refcounted*)o); -} - - -/* Public interface ***********************************************************/ + UPB_ASSERT(arr_count <= upb_inttable_count(t)); -bool upb_refcounted_init(upb_refcounted *r, - const struct upb_refcounted_vtbl *vtbl, - const void *owner) { -#ifndef NDEBUG - /* Endianness check. This is unrelated to upb_refcounted, it's just a - * convenient place to put the check that we can be assured will run for - * basically every program using upb. */ - const int x = 1; -#ifdef UPB_BIG_ENDIAN - UPB_ASSERT(*(char*)&x != 1); -#else - UPB_ASSERT(*(char*)&x == 1); -#endif -#endif + { + /* Insert all elements into new, perfectly-sized table. */ + size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */ + size_t hash_count = upb_inttable_count(t) - arr_count; + size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0; + size_t hashsize_lg2 = log2ceil(hash_size); - r->next = r; - r->vtbl = vtbl; - r->individual_count = 0; - r->is_frozen = false; - r->group = upb_gmalloc(sizeof(*r->group)); - if (!r->group) return false; - *r->group = 0; - trackinit(r); - upb_refcounted_ref(r, owner); - return true; + upb_inttable_sizedinit(&new_t, t->t.ctype, arr_size, hashsize_lg2, a); + upb_inttable_begin(&i, t); + for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { + uintptr_t k = upb_inttable_iter_key(&i); + upb_inttable_insert2(&new_t, k, upb_inttable_iter_value(&i), a); + } + UPB_ASSERT(new_t.array_size == arr_size); + UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2); + } + upb_inttable_uninit2(t, a); + *t = new_t; } -bool upb_refcounted_isfrozen(const upb_refcounted *r) { - return r->is_frozen; +/* Iteration. */ + +static const upb_tabent *int_tabent(const upb_inttable_iter *i) { + UPB_ASSERT(!i->array_part); + return &i->t->t.entries[i->index]; } -void upb_refcounted_ref(const upb_refcounted *r, const void *owner) { - track(r, owner, false); - if (!r->is_frozen) - ((upb_refcounted*)r)->individual_count++; - refgroup(r->group); +static upb_tabval int_arrent(const upb_inttable_iter *i) { + UPB_ASSERT(i->array_part); + return i->t->array[i->index]; } -void upb_refcounted_unref(const upb_refcounted *r, const void *owner) { - untrack(r, owner, false); - if (!r->is_frozen) - ((upb_refcounted*)r)->individual_count--; - unref(r); +void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) { + i->t = t; + i->index = -1; + i->array_part = true; + upb_inttable_next(i); } -void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from) { - UPB_ASSERT(!from->is_frozen); /* Non-const pointer implies this. */ - track(r, from, true); - if (r->is_frozen) { - refgroup(r->group); +void upb_inttable_next(upb_inttable_iter *iter) { + const upb_inttable *t = iter->t; + if (iter->array_part) { + while (++iter->index < t->array_size) { + if (upb_arrhas(int_arrent(iter))) { + return; + } + } + iter->array_part = false; + iter->index = begin(&t->t); } else { - merge((upb_refcounted*)r, from); + iter->index = next(&t->t, iter->index); } } -void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from) { - UPB_ASSERT(!from->is_frozen); /* Non-const pointer implies this. */ - untrack(r, from, true); - if (r->is_frozen) { - unref(r); +bool upb_inttable_done(const upb_inttable_iter *i) { + if (!i->t) return true; + if (i->array_part) { + return i->index >= i->t->array_size || + !upb_arrhas(int_arrent(i)); } else { - UPB_ASSERT(merged(r, from)); + return i->index >= upb_table_size(&i->t->t) || + upb_tabent_isempty(int_tabent(i)); } } -void upb_refcounted_donateref( - const upb_refcounted *r, const void *from, const void *to) { - UPB_ASSERT(from != to); - if (to != NULL) - upb_refcounted_ref(r, to); - if (from != NULL) - upb_refcounted_unref(r, from); +uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) { + UPB_ASSERT(!upb_inttable_done(i)); + return i->array_part ? i->index : int_tabent(i)->key; } -void upb_refcounted_checkref(const upb_refcounted *r, const void *owner) { - checkref(r, owner, false); +upb_value upb_inttable_iter_value(const upb_inttable_iter *i) { + UPB_ASSERT(!upb_inttable_done(i)); + return _upb_value_val( + i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val, + i->t->t.ctype); } -bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s, - int maxdepth) { - int i; - bool ret; - for (i = 0; i < n; i++) { - UPB_ASSERT(!roots[i]->is_frozen); - } - ret = freeze(roots, n, s, maxdepth); - UPB_ASSERT(!s || ret == upb_ok(s)); - return ret; +void upb_inttable_iter_setdone(upb_inttable_iter *i) { + i->t = NULL; + i->index = SIZE_MAX; + i->array_part = false; } - -bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink) { - void *subc; - bool ret; - upb_bufhandle handle; - upb_bufhandle_init(&handle); - upb_bufhandle_setbuf(&handle, buf, 0); - ret = upb_bytessink_start(sink, len, &subc); - if (ret && len != 0) { - ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len); - } - if (ret) { - ret = upb_bytessink_end(sink); - } - upb_bufhandle_uninit(&handle); - return ret; +bool upb_inttable_iter_isequal(const upb_inttable_iter *i1, + const upb_inttable_iter *i2) { + if (upb_inttable_done(i1) && upb_inttable_done(i2)) + return true; + return i1->t == i2->t && i1->index == i2->index && + i1->array_part == i2->array_part; } -struct upb_bufsink { - upb_byteshandler handler; - upb_bytessink sink; - upb_env *env; - char *ptr; - size_t len, size; -}; +#if defined(UPB_UNALIGNED_READS_OK) || defined(__s390x__) +/* ----------------------------------------------------------------------------- + * MurmurHash2, by Austin Appleby (released as public domain). + * Reformatted and C99-ified by Joshua Haberman. + * Note - This code makes a few assumptions about how your machine behaves - + * 1. We can read a 4-byte value from any address without crashing + * 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t + * And it has a few limitations - + * 1. It will not work incrementally. + * 2. It will not produce the same results on little-endian and big-endian + * machines. */ +uint32_t MurmurHash2(const void *key, size_t len, uint32_t seed) { + /* 'm' and 'r' are mixing constants generated offline. + * They're not really 'magic', they just happen to work well. */ + const uint32_t m = 0x5bd1e995; + const int32_t r = 24; -static void *upb_bufsink_start(void *_sink, const void *hd, size_t size_hint) { - upb_bufsink *sink = _sink; - UPB_UNUSED(hd); - UPB_UNUSED(size_hint); - sink->len = 0; - return sink; -} + /* Initialize the hash to a 'random' value */ + uint32_t h = seed ^ len; -static size_t upb_bufsink_string(void *_sink, const void *hd, const char *ptr, - size_t len, const upb_bufhandle *handle) { - upb_bufsink *sink = _sink; - size_t new_size = sink->size; + /* Mix 4 bytes at a time into the hash */ + const uint8_t * data = (const uint8_t *)key; + while(len >= 4) { + uint32_t k = *(uint32_t *)data; - UPB_ASSERT(new_size > 0); - UPB_UNUSED(hd); - UPB_UNUSED(handle); + k *= m; + k ^= k >> r; + k *= m; - while (sink->len + len > new_size) { - new_size *= 2; - } + h *= m; + h ^= k; - if (new_size != sink->size) { - sink->ptr = upb_env_realloc(sink->env, sink->ptr, sink->size, new_size); - sink->size = new_size; + data += 4; + len -= 4; } - memcpy(sink->ptr + sink->len, ptr, len); - sink->len += len; + /* Handle the last few bytes of the input array */ + switch(len) { + case 3: h ^= data[2] << 16; + case 2: h ^= data[1] << 8; + case 1: h ^= data[0]; h *= m; + }; - return len; + /* Do a few final mixes of the hash to ensure the last few + * bytes are well-incorporated. */ + h ^= h >> 13; + h *= m; + h ^= h >> 15; + + return h; } -upb_bufsink *upb_bufsink_new(upb_env *env) { - upb_bufsink *sink = upb_env_malloc(env, sizeof(upb_bufsink)); - upb_byteshandler_init(&sink->handler); - upb_byteshandler_setstartstr(&sink->handler, upb_bufsink_start, NULL); - upb_byteshandler_setstring(&sink->handler, upb_bufsink_string, NULL); +#else /* !UPB_UNALIGNED_READS_OK */ - upb_bytessink_reset(&sink->sink, &sink->handler, sink); +/* ----------------------------------------------------------------------------- + * MurmurHashAligned2, by Austin Appleby + * Same algorithm as MurmurHash2, but only does aligned reads - should be safer + * on certain platforms. + * Performance will be lower than MurmurHash2 */ - sink->env = env; - sink->size = 32; - sink->ptr = upb_env_malloc(env, sink->size); - sink->len = 0; +#define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; } - return sink; -} +uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed) { + const uint32_t m = 0x5bd1e995; + const int32_t r = 24; + const uint8_t * data = (const uint8_t *)key; + uint32_t h = seed ^ len; + uint8_t align = (uintptr_t)data & 3; -void upb_bufsink_free(upb_bufsink *sink) { - upb_env_free(sink->env, sink->ptr); - upb_env_free(sink->env, sink); -} + if(align && (len >= 4)) { + /* Pre-load the temp registers */ + uint32_t t = 0, d = 0; + int32_t sl; + int32_t sr; -upb_bytessink *upb_bufsink_sink(upb_bufsink *sink) { - return &sink->sink; -} + switch(align) { + case 1: t |= data[2] << 16; + case 2: t |= data[1] << 8; + case 3: t |= data[0]; + } -const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len) { - *len = sink->len; - return sink->ptr; -} -/* -** upb_table Implementation -** -** Implementation is heavily inspired by Lua's ltable.c. -*/ + t <<= (8 * align); + data += 4-align; + len -= 4-align; -#include + sl = 8 * (4-align); + sr = 8 * align; -#define UPB_MAXARRSIZE 16 /* 64k. */ + /* Mix */ -/* From Chromium. */ -#define ARRAY_SIZE(x) \ - ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) + while(len >= 4) { + uint32_t k; -static void upb_check_alloc(upb_table *t, upb_alloc *a) { - UPB_UNUSED(t); - UPB_UNUSED(a); - UPB_ASSERT_DEBUGVAR(t->alloc == a); -} + d = *(uint32_t *)data; + t = (t >> sr) | (d << sl); -static const double MAX_LOAD = 0.85; + k = t; -/* 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; + MIX(h,k,m); -bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; } + t = d; -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); -} + data += 4; + len -= 4; + } -char *upb_strdup(const char *s, upb_alloc *a) { - return upb_strdup2(s, strlen(s), a); -} + /* Handle leftover data in temp registers */ -char *upb_strdup2(const char *s, size_t len, upb_alloc *a) { - size_t n; - char *p; + d = 0; - /* 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_malloc(a, n); - if (p) { - memcpy(p, s, len); - p[len] = 0; - } - return p; -} + if(len >= align) { + uint32_t k; -/* A type to represent the lookup key of either a strtable or an inttable. */ -typedef union { - uintptr_t num; - struct { - const char *str; - size_t len; - } str; -} lookupkey_t; + switch(align) { + case 3: d |= data[2] << 16; + case 2: d |= data[1] << 8; + case 1: d |= data[0]; + } -static lookupkey_t strkey2(const char *str, size_t len) { - lookupkey_t k; - k.str.str = str; - k.str.len = len; - return k; -} + k = (t >> sr) | (d << sl); + MIX(h,k,m); -static lookupkey_t intkey(uintptr_t key) { - lookupkey_t k; - k.num = key; - return k; -} + data += align; + len -= align; -typedef uint32_t hashfunc_t(upb_tabkey key); -typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2); + /* ---------- + * Handle tail bytes */ -/* Base table (shared code) ***************************************************/ + switch(len) { + case 3: h ^= data[2] << 16; + case 2: h ^= data[1] << 8; + case 1: h ^= data[0]; h *= m; + }; + } else { + switch(len) { + case 3: d |= data[2] << 16; + case 2: d |= data[1] << 8; + case 1: d |= data[0]; + case 0: h ^= (t >> sr) | (d << sl); h *= m; + } + } -/* For when we need to cast away const. */ -static upb_tabent *mutable_entries(upb_table *t) { - return (upb_tabent*)t->entries; -} + h ^= h >> 13; + h *= m; + h ^= h >> 15; -static bool isfull(upb_table *t) { - if (upb_table_size(t) == 0) { - return true; + return h; } else { - return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD; - } -} + while(len >= 4) { + uint32_t k = *(uint32_t *)data; -static bool init(upb_table *t, upb_ctype_t ctype, uint8_t size_lg2, - upb_alloc *a) { - size_t bytes; + MIX(h,k,m); - t->count = 0; - t->ctype = ctype; - t->size_lg2 = size_lg2; - t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0; -#ifndef NDEBUG - t->alloc = a; -#endif - bytes = upb_table_size(t) * sizeof(upb_tabent); - if (bytes > 0) { - t->entries = upb_malloc(a, bytes); - if (!t->entries) return false; - memset(mutable_entries(t), 0, bytes); - } else { - t->entries = NULL; + data += 4; + len -= 4; + } + + /* ---------- + * Handle tail bytes */ + + switch(len) { + case 3: h ^= data[2] << 16; + case 2: h ^= data[1] << 8; + case 1: h ^= data[0]; h *= m; + }; + + h ^= h >> 13; + h *= m; + h ^= h >> 15; + + return h; } - return true; } +#undef MIX -static void uninit(upb_table *t, upb_alloc *a) { - upb_check_alloc(t, a); - upb_free(a, mutable_entries(t)); -} +#endif /* UPB_UNALIGNED_READS_OK */ -static upb_tabent *emptyent(upb_table *t) { - upb_tabent *e = mutable_entries(t) + upb_table_size(t); - while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); } -} +#include +#include +#include +#include +#include +#include +#include -static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) { - return (upb_tabent*)upb_getentry(t, hash); +/* Guarantee null-termination and provide ellipsis truncation. + * It may be tempting to "optimize" this by initializing these final + * four bytes up-front and then being careful never to overwrite them, + * this is safer and simpler. */ +static void nullz(upb_status *status) { + const char *ellipsis = "..."; + size_t len = strlen(ellipsis); + UPB_ASSERT(sizeof(status->msg) > len); + memcpy(status->msg + sizeof(status->msg) - len, ellipsis, len); } -static const upb_tabent *findentry(const upb_table *t, lookupkey_t key, - uint32_t hash, eqlfunc_t *eql) { - const upb_tabent *e; +/* upb_status *****************************************************************/ - if (t->size_lg2 == 0) return NULL; - e = upb_getentry(t, hash); - if (upb_tabent_isempty(e)) return NULL; - while (1) { - if (eql(e->key, key)) return e; - if ((e = e->next) == NULL) return NULL; - } +void upb_status_clear(upb_status *status) { + if (!status) return; + status->ok = true; + status->msg[0] = '\0'; } -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); +bool upb_ok(const upb_status *status) { return status->ok; } + +const char *upb_status_errmsg(const upb_status *status) { return status->msg; } + +void upb_status_seterrmsg(upb_status *status, const char *msg) { + if (!status) return; + status->ok = false; + strncpy(status->msg, msg, sizeof(status->msg)); + nullz(status); } -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) { - _upb_value_setval(v, e->val.val, t->ctype); - } - return true; - } else { - return false; - } +void upb_status_seterrf(upb_status *status, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + upb_status_vseterrf(status, fmt, args); + va_end(args); } -/* The given key must not already exist in the table. */ -static void insert(upb_table *t, lookupkey_t key, upb_tabkey 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); - UPB_ASSERT_DEBUGVAR(val.ctype == t->ctype); +void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) { + if (!status) return; + status->ok = false; + _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args); + nullz(status); +} - t->count++; - mainpos_e = getentry_mutable(t, hash); - our_e = mainpos_e; +/* upb_alloc ******************************************************************/ - if (upb_tabent_isempty(mainpos_e)) { - /* Our main position is empty; use it. */ - our_e->next = NULL; +static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize, + size_t size) { + UPB_UNUSED(alloc); + UPB_UNUSED(oldsize); + if (size == 0) { + free(ptr); + return NULL; } else { - /* Collision. */ - upb_tabent *new_e = emptyent(t); - /* Head of collider's chain. */ - upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key)); - if (chain == mainpos_e) { - /* Existing ent is in its main posisiton (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; - } + return realloc(ptr, size); } - our_e->key = tabkey; - our_e->val.val = val.val; - UPB_ASSERT(findentry(t, key, hash, eql) == our_e); } -static bool rm(upb_table *t, lookupkey_t key, upb_value *val, - upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) { - upb_tabent *chain = getentry_mutable(t, hash); - if (upb_tabent_isempty(chain)) return false; - if (eql(chain->key, key)) { - /* Element to remove is at the head of its chain. */ - t->count--; - if (val) _upb_value_setval(val, chain->val.val, t->ctype); - if (removed) *removed = chain->key; - if (chain->next) { - upb_tabent *move = (upb_tabent*)chain->next; - *chain = *move; - move->key = 0; /* Make the slot empty. */ - } else { - chain->key = 0; /* Make the slot 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, key)) { - chain = (upb_tabent*)chain->next; - } - if (chain->next) { - /* Found element to remove. */ - upb_tabent *rm = (upb_tabent*)chain->next; - t->count--; - if (val) _upb_value_setval(val, chain->next->val.val, t->ctype); - if (removed) *removed = rm->key; - rm->key = 0; /* Make the slot empty. */ - chain->next = rm->next; - return true; - } else { - /* Element to remove is not in the table. */ - return false; - } - } -} +upb_alloc upb_alloc_global = {&upb_global_allocfunc}; -static size_t next(const upb_table *t, size_t i) { - do { - if (++i >= upb_table_size(t)) - return SIZE_MAX; - } while(upb_tabent_isempty(&t->entries[i])); +/* upb_arena ******************************************************************/ - return i; -} +/* Be conservative and choose 16 in case anyone is using SSE. */ +static const size_t maxalign = 16; -static size_t begin(const upb_table *t) { - return next(t, -1); +static size_t align_up_max(size_t size) { + return ((size + maxalign - 1) / maxalign) * maxalign; } +struct upb_arena { + /* We implement the allocator interface. + * This must be the first member of upb_arena! */ + upb_alloc alloc; -/* upb_strtable ***************************************************************/ + /* Allocator to allocate arena blocks. We are responsible for freeing these + * when we are destroyed. */ + upb_alloc *block_alloc; -/* A simple "subclass" of upb_table that only adds a hash function for strings. */ + size_t bytes_allocated; + size_t next_block_size; + size_t max_block_size; -static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) { - char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1); - if (str == NULL) return 0; - memcpy(str, &k2.str.len, sizeof(uint32_t)); - memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len + 1); - return (uintptr_t)str; -} + /* Linked list of blocks. Points to an arena_block, defined in env.c */ + void *block_head; -static uint32_t strhash(upb_tabkey key) { - uint32_t len; - char *str = upb_tabstr(key, &len); - return MurmurHash2(str, len, 0); -} + /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */ + void *cleanup_head; +}; -static bool streql(upb_tabkey k1, lookupkey_t k2) { - uint32_t len; - char *str = upb_tabstr(k1, &len); - return len == k2.str.len && memcmp(str, k2.str.str, len) == 0; -} +typedef struct mem_block { + struct mem_block *next; + size_t size; + size_t used; + bool owned; + /* Data follows. */ +} mem_block; -bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) { - return init(&t->t, ctype, 2, a); -} +typedef struct cleanup_ent { + struct cleanup_ent *next; + upb_cleanup_func *cleanup; + void *ud; +} cleanup_ent; -void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) { - size_t i; - for (i = 0; i < upb_table_size(&t->t); i++) - upb_free(a, (void*)t->t.entries[i].key); - uninit(&t->t, a); -} +static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size, + bool owned) { + mem_block *block = ptr; -bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) { - upb_strtable new_table; - upb_strtable_iter i; + block->next = a->block_head; + block->size = size; + block->used = align_up_max(sizeof(mem_block)); + block->owned = owned; - upb_check_alloc(&t->t, a); + a->block_head = block; - if (!init(&new_table.t, t->t.ctype, size_lg2, a)) - return false; - upb_strtable_begin(&i, t); - for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) { - upb_strtable_insert3( - &new_table, - upb_strtable_iter_key(&i), - upb_strtable_iter_keylength(&i), - upb_strtable_iter_value(&i), - a); - } - upb_strtable_uninit2(t, a); - *t = new_table; - return true; + /* TODO(haberman): ASAN poison. */ } -bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len, - upb_value v, upb_alloc *a) { - lookupkey_t key; - upb_tabkey tabkey; - uint32_t hash; - - upb_check_alloc(&t->t, a); +static mem_block *upb_arena_allocblock(upb_arena *a, size_t size) { + size_t block_size = UPB_MAX(size, a->next_block_size) + sizeof(mem_block); + mem_block *block = upb_malloc(a->block_alloc, block_size); - if (isfull(&t->t)) { - /* Need to resize. New table of double the size, add old elements to it. */ - if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) { - return false; - } + if (!block) { + return NULL; } - key = strkey2(k, len); - tabkey = strcopy(key, a); - if (tabkey == 0) return false; + upb_arena_addblock(a, block, block_size, true); + a->next_block_size = UPB_MIN(block_size * 2, a->max_block_size); - hash = MurmurHash2(key.str.str, key.str.len, 0); - insert(&t->t, key, tabkey, v, hash, &strhash, &streql); - return true; + return block; } -bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len, - upb_value *v) { - uint32_t hash = MurmurHash2(key, len, 0); - return lookup(&t->t, strkey2(key, len), v, hash, &streql); -} +static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize, + size_t size) { + upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */ + mem_block *block = a->block_head; + void *ret; -bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len, - upb_value *val, upb_alloc *alloc) { - uint32_t hash = MurmurHash2(key, len, 0); - upb_tabkey tabkey; - if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) { - upb_free(alloc, (void*)tabkey); - return true; - } else { - return false; + if (size == 0) { + return NULL; /* We are an arena, don't need individual frees. */ } -} -/* Iteration */ + size = align_up_max(size); -static const upb_tabent *str_tabent(const upb_strtable_iter *i) { - return &i->t->t.entries[i->index]; -} + /* TODO(haberman): special-case if this is a realloc of the last alloc? */ -void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) { - i->t = t; - i->index = begin(&t->t); -} + if (!block || block->size - block->used < size) { + /* Slow path: have to allocate a new block. */ + block = upb_arena_allocblock(a, size); -void upb_strtable_next(upb_strtable_iter *i) { - i->index = next(&i->t->t, i->index); -} + if (!block) { + return NULL; /* Out of memory. */ + } + } -bool upb_strtable_done(const upb_strtable_iter *i) { - return i->index >= upb_table_size(&i->t->t) || - upb_tabent_isempty(str_tabent(i)); -} + ret = (char*)block + block->used; + block->used += size; -const char *upb_strtable_iter_key(const upb_strtable_iter *i) { - UPB_ASSERT(!upb_strtable_done(i)); - return upb_tabstr(str_tabent(i)->key, NULL); -} + if (oldsize > 0) { + memcpy(ret, ptr, oldsize); /* Preserve existing data. */ + } -size_t upb_strtable_iter_keylength(const upb_strtable_iter *i) { - uint32_t len; - UPB_ASSERT(!upb_strtable_done(i)); - upb_tabstr(str_tabent(i)->key, &len); - return len; -} + /* TODO(haberman): ASAN unpoison. */ -upb_value upb_strtable_iter_value(const upb_strtable_iter *i) { - UPB_ASSERT(!upb_strtable_done(i)); - return _upb_value_val(str_tabent(i)->val.val, i->t->t.ctype); + a->bytes_allocated += size; + return ret; } -void upb_strtable_iter_setdone(upb_strtable_iter *i) { - i->index = SIZE_MAX; -} +/* Public Arena API ***********************************************************/ -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; -} +#define upb_alignof(type) offsetof (struct { char c; type member; }, member) +upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { + const size_t first_block_overhead = sizeof(upb_arena) + sizeof(mem_block); + upb_arena *a; + bool owned = false; -/* upb_inttable ***************************************************************/ + /* Round block size down to alignof(*a) since we will allocate the arena + * itself at the end. */ + n &= ~(upb_alignof(upb_arena) - 1); -/* For inttables we use a hybrid structure where small keys are kept in an - * array and large keys are put in the hash table. */ + if (n < first_block_overhead) { + /* We need to malloc the initial block. */ + n = first_block_overhead + 256; + owned = true; + if (!alloc || !(mem = upb_malloc(alloc, n))) { + return NULL; + } + } -static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); } + a = (void*)((char*)mem + n - sizeof(*a)); + n -= sizeof(*a); -static bool inteql(upb_tabkey k1, lookupkey_t k2) { - return k1 == k2.num; -} + a->alloc.func = &upb_arena_doalloc; + a->block_alloc = &upb_alloc_global; + a->bytes_allocated = 0; + a->next_block_size = 256; + a->max_block_size = 16384; + a->cleanup_head = NULL; + a->block_head = NULL; + a->block_alloc = alloc; -static upb_tabval *mutable_array(upb_inttable *t) { - return (upb_tabval*)t->array; -} + upb_arena_addblock(a, mem, n, owned); -static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) { - if (key < t->array_size) { - return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL; - } else { - upb_tabent *e = - findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql); - return e ? &e->val : NULL; - } + return a; } -static const upb_tabval *inttable_val_const(const upb_inttable *t, - uintptr_t key) { - return inttable_val((upb_inttable*)t, key); -} +#undef upb_alignof -size_t upb_inttable_count(const upb_inttable *t) { - return t->t.count + t->array_count; -} +void upb_arena_free(upb_arena *a) { + cleanup_ent *ent = a->cleanup_head; + mem_block *block = a->block_head; -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; - upb_inttable_iter i; - upb_inttable_begin(&i, t); - for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) { - UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL)); - } - UPB_ASSERT(count == upb_inttable_count(t)); + while (ent) { + ent->cleanup(ent->ud); + ent = ent->next; } -#endif -} -bool upb_inttable_sizedinit(upb_inttable *t, upb_ctype_t ctype, - size_t asize, int hsize_lg2, upb_alloc *a) { - size_t array_bytes; + /* Must do this after running cleanup functions, because this will delete + * the memory we store our cleanup entries in! */ + while (block) { + /* Load first since we are deleting block. */ + mem_block *next = block->next; - if (!init(&t->t, ctype, 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; - array_bytes = t->array_size * sizeof(upb_value); - t->array = upb_malloc(a, array_bytes); - if (!t->array) { - uninit(&t->t, a); - return false; + if (block->owned) { + upb_free(a->block_alloc, block); + } + + block = next; } - memset(mutable_array(t), 0xff, array_bytes); - check(t); - return true; } -bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) { - return upb_inttable_sizedinit(t, ctype, 0, 4, a); +bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) { + cleanup_ent *ent = upb_malloc(&a->alloc, sizeof(cleanup_ent)); + if (!ent) { + return false; /* Out of memory. */ + } + + ent->cleanup = func; + ent->ud = ud; + ent->next = a->cleanup_head; + a->cleanup_head = ent; + + return true; } -void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) { - uninit(&t->t, a); - upb_free(a, mutable_array(t)); +size_t upb_arena_bytesallocated(const upb_arena *a) { + return a->bytes_allocated; } +/* +** protobuf decoder bytecode compiler +** +** Code to compile a upb::Handlers into bytecode for decoding a protobuf +** according to that specific schema and destination handlers. +** +** Bytecode definition is in decoder.int.h. +*/ -bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val, - upb_alloc *a) { - upb_tabval tabval; - tabval.val = val.val; - UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */ +#include - upb_check_alloc(&t->t, a); +#ifdef UPB_DUMP_BYTECODE +#include +#endif - if (key < t->array_size) { - UPB_ASSERT(!upb_arrhas(t->array[key])); - t->array_count++; - mutable_array(t)[key].val = val.val; - } 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; +#define MAXLABEL 5 +#define EMPTYLABEL -1 - if (!init(&new_table, t->t.ctype, t->t.size_lg2 + 1, a)) { - return false; - } +/* upb_pbdecodermethod ********************************************************/ - 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; - upb_value v; +static void freemethod(upb_pbdecodermethod *method) { + upb_inttable_uninit(&method->dispatch); + upb_gfree(method); +} - _upb_value_setval(&v, e->val.val, t->t.ctype); - hash = upb_inthash(e->key); - insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql); - } +static upb_pbdecodermethod *newmethod(const upb_handlers *dest_handlers, + mgroup *group) { + upb_pbdecodermethod *ret = upb_gmalloc(sizeof(*ret)); + upb_byteshandler_init(&ret->input_handler_); - UPB_ASSERT(t->t.count == new_table.count); + ret->group = group; + ret->dest_handlers_ = dest_handlers; + upb_inttable_init(&ret->dispatch, UPB_CTYPE_UINT64); - uninit(&t->t, a); - t->t = new_table; - } - insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql); - } - check(t); - return true; + return ret; } -bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) { - const upb_tabval *table_v = inttable_val_const(t, key); - if (!table_v) return false; - if (v) _upb_value_setval(v, table_v->val, t->t.ctype); - return true; +const upb_handlers *upb_pbdecodermethod_desthandlers( + const upb_pbdecodermethod *m) { + return m->dest_handlers_; } -bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) { - upb_tabval *table_v = inttable_val(t, key); - if (!table_v) return false; - table_v->val = val.val; - return true; +const upb_byteshandler *upb_pbdecodermethod_inputhandler( + const upb_pbdecodermethod *m) { + return &m->input_handler_; } -bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) { - bool success; - if (key < t->array_size) { - if (upb_arrhas(t->array[key])) { - upb_tabval empty = UPB_TABVALUE_EMPTY_INIT; - t->array_count--; - if (val) { - _upb_value_setval(val, t->array[key].val, t->t.ctype); - } - mutable_array(t)[key] = empty; - success = true; - } else { - success = false; - } - } else { - success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql); - } - check(t); - return success; +bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m) { + return m->is_native_; } -bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a) { - upb_check_alloc(&t->t, a); - return upb_inttable_insert2(t, upb_inttable_count(t), val, a); -} -upb_value upb_inttable_pop(upb_inttable *t) { - upb_value val; - bool ok = upb_inttable_remove(t, upb_inttable_count(t) - 1, &val); - UPB_ASSERT(ok); - return val; -} +/* mgroup *********************************************************************/ -bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val, - upb_alloc *a) { - upb_check_alloc(&t->t, a); - return upb_inttable_insert2(t, (uintptr_t)key, val, a); -} +static void freegroup(mgroup *g) { + upb_inttable_iter i; -bool upb_inttable_lookupptr(const upb_inttable *t, const void *key, - upb_value *v) { - return upb_inttable_lookup(t, (uintptr_t)key, v); + upb_inttable_begin(&i, &g->methods); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + freemethod(upb_value_getptr(upb_inttable_iter_value(&i))); + } + + upb_inttable_uninit(&g->methods); + upb_gfree(g->bytecode); + upb_gfree(g); } -bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) { - return upb_inttable_remove(t, (uintptr_t)key, val); +mgroup *newgroup() { + mgroup *g = upb_gmalloc(sizeof(*g)); + upb_inttable_init(&g->methods, UPB_CTYPE_PTR); + g->bytecode = NULL; + g->bytecode_end = NULL; + return g; } -void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) { - /* A power-of-two histogram of the table keys. */ - size_t counts[UPB_MAXARRSIZE + 1] = {0}; - /* The max key in each bucket. */ - uintptr_t max[UPB_MAXARRSIZE + 1] = {0}; +/* bytecode compiler **********************************************************/ - upb_inttable_iter i; - size_t arr_count; - int size_lg2; - upb_inttable new_t; - - upb_check_alloc(&t->t, a); - - upb_inttable_begin(&i, t); - for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { - uintptr_t key = upb_inttable_iter_key(&i); - 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). */ - arr_count = upb_inttable_count(t); - - for (size_lg2 = ARRAY_SIZE(counts) - 1; 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; - } +/* Data used only at compilation time. */ +typedef struct { + mgroup *group; - arr_count -= counts[size_lg2]; - } + uint32_t *pc; + int fwd_labels[MAXLABEL]; + int back_labels[MAXLABEL]; - UPB_ASSERT(arr_count <= upb_inttable_count(t)); + /* For fields marked "lazy", parse them lazily or eagerly? */ + bool lazy; +} compiler; - { - /* Insert all elements into new, perfectly-sized table. */ - size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */ - size_t hash_count = upb_inttable_count(t) - arr_count; - size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0; - size_t hashsize_lg2 = log2ceil(hash_size); +static compiler *newcompiler(mgroup *group, bool lazy) { + compiler *ret = upb_gmalloc(sizeof(*ret)); + int i; - upb_inttable_sizedinit(&new_t, t->t.ctype, arr_size, hashsize_lg2, a); - upb_inttable_begin(&i, t); - for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { - uintptr_t k = upb_inttable_iter_key(&i); - upb_inttable_insert2(&new_t, k, upb_inttable_iter_value(&i), a); - } - UPB_ASSERT(new_t.array_size == arr_size); - UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2); + ret->group = group; + ret->lazy = lazy; + for (i = 0; i < MAXLABEL; i++) { + ret->fwd_labels[i] = EMPTYLABEL; + ret->back_labels[i] = EMPTYLABEL; } - upb_inttable_uninit2(t, a); - *t = new_t; + return ret; } -/* Iteration. */ - -static const upb_tabent *int_tabent(const upb_inttable_iter *i) { - UPB_ASSERT(!i->array_part); - return &i->t->t.entries[i->index]; +static void freecompiler(compiler *c) { + upb_gfree(c); } -static upb_tabval int_arrent(const upb_inttable_iter *i) { - UPB_ASSERT(i->array_part); - return i->t->array[i->index]; +const size_t ptr_words = sizeof(void*) / sizeof(uint32_t); + +/* How many words an instruction is. */ +static int instruction_len(uint32_t instr) { + switch (getop(instr)) { + case OP_SETDISPATCH: return 1 + ptr_words; + case OP_TAGN: return 3; + case OP_SETBIGGROUPNUM: return 2; + default: return 1; + } } -void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) { - i->t = t; - i->index = -1; - i->array_part = true; - upb_inttable_next(i); +bool op_has_longofs(int32_t instruction) { + switch (getop(instruction)) { + case OP_CALL: + case OP_BRANCH: + case OP_CHECKDELIM: + return true; + /* The "tag" instructions only have 8 bytes available for the jump target, + * but that is ok because these opcodes only require short jumps. */ + case OP_TAG1: + case OP_TAG2: + case OP_TAGN: + return false; + default: + UPB_ASSERT(false); + return false; + } } -void upb_inttable_next(upb_inttable_iter *iter) { - const upb_inttable *t = iter->t; - if (iter->array_part) { - while (++iter->index < t->array_size) { - if (upb_arrhas(int_arrent(iter))) { - return; - } - } - iter->array_part = false; - iter->index = begin(&t->t); +static int32_t getofs(uint32_t instruction) { + if (op_has_longofs(instruction)) { + return (int32_t)instruction >> 8; } else { - iter->index = next(&t->t, iter->index); + return (int8_t)(instruction >> 8); } } -bool upb_inttable_done(const upb_inttable_iter *i) { - if (i->array_part) { - return i->index >= i->t->array_size || - !upb_arrhas(int_arrent(i)); +static void setofs(uint32_t *instruction, int32_t ofs) { + if (op_has_longofs(*instruction)) { + *instruction = getop(*instruction) | ofs << 8; } else { - return i->index >= upb_table_size(&i->t->t) || - upb_tabent_isempty(int_tabent(i)); + *instruction = (*instruction & ~0xff00) | ((ofs & 0xff) << 8); } + UPB_ASSERT(getofs(*instruction) == ofs); /* Would fail in cases of overflow. */ } -uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) { - UPB_ASSERT(!upb_inttable_done(i)); - return i->array_part ? i->index : int_tabent(i)->key; -} +static uint32_t pcofs(compiler *c) { return c->pc - c->group->bytecode; } -upb_value upb_inttable_iter_value(const upb_inttable_iter *i) { - UPB_ASSERT(!upb_inttable_done(i)); - return _upb_value_val( - i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val, - i->t->t.ctype); -} +/* Defines a local label at the current PC location. All previous forward + * references are updated to point to this location. The location is noted + * for any future backward references. */ +static void label(compiler *c, unsigned int label) { + int val; + uint32_t *codep; -void upb_inttable_iter_setdone(upb_inttable_iter *i) { - i->index = SIZE_MAX; - i->array_part = false; + UPB_ASSERT(label < MAXLABEL); + val = c->fwd_labels[label]; + codep = (val == EMPTYLABEL) ? NULL : c->group->bytecode + val; + while (codep) { + int ofs = getofs(*codep); + setofs(codep, c->pc - codep - instruction_len(*codep)); + codep = ofs ? codep + ofs : NULL; + } + c->fwd_labels[label] = EMPTYLABEL; + c->back_labels[label] = pcofs(c); } -bool upb_inttable_iter_isequal(const upb_inttable_iter *i1, - const upb_inttable_iter *i2) { - if (upb_inttable_done(i1) && upb_inttable_done(i2)) - return true; - return i1->t == i2->t && i1->index == i2->index && - i1->array_part == i2->array_part; +/* Creates a reference to a numbered label; either a forward reference + * (positive arg) or backward reference (negative arg). For forward references + * the value returned now is actually a "next" pointer into a linked list of all + * instructions that use this label and will be patched later when the label is + * defined with label(). + * + * The returned value is the offset that should be written into the instruction. + */ +static int32_t labelref(compiler *c, int label) { + UPB_ASSERT(label < MAXLABEL); + if (label == LABEL_DISPATCH) { + /* No resolving required. */ + return 0; + } else if (label < 0) { + /* Backward local label. Relative to the next instruction. */ + uint32_t from = (c->pc + 1) - c->group->bytecode; + return c->back_labels[-label] - from; + } else { + /* Forward local label: prepend to (possibly-empty) linked list. */ + int *lptr = &c->fwd_labels[label]; + int32_t ret = (*lptr == EMPTYLABEL) ? 0 : *lptr - pcofs(c); + *lptr = pcofs(c); + return ret; + } } -#ifdef UPB_UNALIGNED_READS_OK -/* ----------------------------------------------------------------------------- - * MurmurHash2, by Austin Appleby (released as public domain). - * Reformatted and C99-ified by Joshua Haberman. - * Note - This code makes a few assumptions about how your machine behaves - - * 1. We can read a 4-byte value from any address without crashing - * 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t - * And it has a few limitations - - * 1. It will not work incrementally. - * 2. It will not produce the same results on little-endian and big-endian - * machines. */ -uint32_t MurmurHash2(const void *key, size_t len, uint32_t seed) { - /* 'm' and 'r' are mixing constants generated offline. - * They're not really 'magic', they just happen to work well. */ - const uint32_t m = 0x5bd1e995; - const int32_t r = 24; - - /* Initialize the hash to a 'random' value */ - uint32_t h = seed ^ len; - - /* Mix 4 bytes at a time into the hash */ - const uint8_t * data = (const uint8_t *)key; - while(len >= 4) { - uint32_t k = *(uint32_t *)data; - - k *= m; - k ^= k >> r; - k *= m; - - h *= m; - h ^= k; - - data += 4; - len -= 4; +static void put32(compiler *c, uint32_t v) { + mgroup *g = c->group; + if (c->pc == g->bytecode_end) { + int ofs = pcofs(c); + size_t oldsize = g->bytecode_end - g->bytecode; + size_t newsize = UPB_MAX(oldsize * 2, 64); + /* TODO(haberman): handle OOM. */ + g->bytecode = upb_grealloc(g->bytecode, oldsize * sizeof(uint32_t), + newsize * sizeof(uint32_t)); + g->bytecode_end = g->bytecode + newsize; + c->pc = g->bytecode + ofs; } - - /* Handle the last few bytes of the input array */ - switch(len) { - case 3: h ^= data[2] << 16; - case 2: h ^= data[1] << 8; - case 1: h ^= data[0]; h *= m; - }; - - /* Do a few final mixes of the hash to ensure the last few - * bytes are well-incorporated. */ - h ^= h >> 13; - h *= m; - h ^= h >> 15; - - return h; + *c->pc++ = v; } -#else /* !UPB_UNALIGNED_READS_OK */ +static void putop(compiler *c, int op, ...) { + va_list ap; + va_start(ap, op); -/* ----------------------------------------------------------------------------- - * MurmurHashAligned2, by Austin Appleby - * Same algorithm as MurmurHash2, but only does aligned reads - should be safer - * on certain platforms. - * Performance will be lower than MurmurHash2 */ - -#define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; } - -uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed) { - const uint32_t m = 0x5bd1e995; - const int32_t r = 24; - const uint8_t * data = (const uint8_t *)key; - uint32_t h = seed ^ len; - uint8_t align = (uintptr_t)data & 3; - - if(align && (len >= 4)) { - /* Pre-load the temp registers */ - uint32_t t = 0, d = 0; - int32_t sl; - int32_t sr; - - switch(align) { - case 1: t |= data[2] << 16; - case 2: t |= data[1] << 8; - case 3: t |= data[0]; + switch (op) { + case OP_SETDISPATCH: { + uintptr_t ptr = (uintptr_t)va_arg(ap, void*); + put32(c, OP_SETDISPATCH); + put32(c, ptr); + if (sizeof(uintptr_t) > sizeof(uint32_t)) + put32(c, (uint64_t)ptr >> 32); + break; } - - t <<= (8 * align); - - data += 4-align; - len -= 4-align; - - sl = 8 * (4-align); - sr = 8 * align; - - /* Mix */ - - while(len >= 4) { - uint32_t k; - - d = *(uint32_t *)data; - t = (t >> sr) | (d << sl); - - k = t; - - MIX(h,k,m); - - t = d; - - data += 4; - len -= 4; + case OP_STARTMSG: + case OP_ENDMSG: + case OP_PUSHLENDELIM: + case OP_POP: + case OP_SETDELIM: + case OP_HALT: + case OP_RET: + case OP_DISPATCH: + put32(c, op); + break; + case OP_PARSE_DOUBLE: + case OP_PARSE_FLOAT: + case OP_PARSE_INT64: + case OP_PARSE_UINT64: + case OP_PARSE_INT32: + case OP_PARSE_FIXED64: + case OP_PARSE_FIXED32: + case OP_PARSE_BOOL: + case OP_PARSE_UINT32: + case OP_PARSE_SFIXED32: + case OP_PARSE_SFIXED64: + case OP_PARSE_SINT32: + case OP_PARSE_SINT64: + case OP_STARTSEQ: + case OP_ENDSEQ: + case OP_STARTSUBMSG: + case OP_ENDSUBMSG: + case OP_STARTSTR: + case OP_STRING: + case OP_ENDSTR: + case OP_PUSHTAGDELIM: + put32(c, op | va_arg(ap, upb_selector_t) << 8); + break; + case OP_SETBIGGROUPNUM: + put32(c, op); + put32(c, va_arg(ap, int)); + break; + case OP_CALL: { + const upb_pbdecodermethod *method = va_arg(ap, upb_pbdecodermethod *); + put32(c, op | (method->code_base.ofs - (pcofs(c) + 1)) << 8); + break; } - - /* Handle leftover data in temp registers */ - - d = 0; - - if(len >= align) { - uint32_t k; - - switch(align) { - case 3: d |= data[2] << 16; - case 2: d |= data[1] << 8; - case 1: d |= data[0]; - } - - k = (t >> sr) | (d << sl); - MIX(h,k,m); - - data += align; - len -= align; - - /* ---------- - * Handle tail bytes */ - - switch(len) { - case 3: h ^= data[2] << 16; - case 2: h ^= data[1] << 8; - case 1: h ^= data[0]; h *= m; - }; - } else { - switch(len) { - case 3: d |= data[2] << 16; - case 2: d |= data[1] << 8; - case 1: d |= data[0]; - case 0: h ^= (t >> sr) | (d << sl); h *= m; - } + case OP_CHECKDELIM: + case OP_BRANCH: { + uint32_t instruction = op; + int label = va_arg(ap, int); + setofs(&instruction, labelref(c, label)); + put32(c, instruction); + break; } - - h ^= h >> 13; - h *= m; - h ^= h >> 15; - - return h; - } else { - while(len >= 4) { - uint32_t k = *(uint32_t *)data; - - MIX(h,k,m); - - data += 4; - len -= 4; + case OP_TAG1: + case OP_TAG2: { + int label = va_arg(ap, int); + uint64_t tag = va_arg(ap, uint64_t); + uint32_t instruction = op | (tag << 16); + UPB_ASSERT(tag <= 0xffff); + setofs(&instruction, labelref(c, label)); + put32(c, instruction); + break; + } + case OP_TAGN: { + int label = va_arg(ap, int); + uint64_t tag = va_arg(ap, uint64_t); + uint32_t instruction = op | (upb_value_size(tag) << 16); + setofs(&instruction, labelref(c, label)); + put32(c, instruction); + put32(c, tag); + put32(c, tag >> 32); + break; } - - /* ---------- - * Handle tail bytes */ - - switch(len) { - case 3: h ^= data[2] << 16; - case 2: h ^= data[1] << 8; - case 1: h ^= data[0]; h *= m; - }; - - h ^= h >> 13; - h *= m; - h ^= h >> 15; - - return h; } -} -#undef MIX - -#endif /* UPB_UNALIGNED_READS_OK */ - -#include -#include -#include -#include -#include -#include -#include - -bool upb_dumptostderr(void *closure, const upb_status* status) { - UPB_UNUSED(closure); - fprintf(stderr, "%s\n", upb_status_errmsg(status)); - return false; -} - -/* Guarantee null-termination and provide ellipsis truncation. - * It may be tempting to "optimize" this by initializing these final - * four bytes up-front and then being careful never to overwrite them, - * this is safer and simpler. */ -static void nullz(upb_status *status) { - const char *ellipsis = "..."; - size_t len = strlen(ellipsis); - UPB_ASSERT(sizeof(status->msg) > len); - memcpy(status->msg + sizeof(status->msg) - len, ellipsis, len); -} - -/* upb_upberr *****************************************************************/ - -upb_errorspace upb_upberr = {"upb error"}; - -void upb_upberr_setoom(upb_status *status) { - status->error_space_ = &upb_upberr; - upb_status_seterrmsg(status, "Out of memory"); + va_end(ap); } +#if defined(UPB_DUMP_BYTECODE) -/* upb_status *****************************************************************/ - -void upb_status_clear(upb_status *status) { - if (!status) return; - status->ok_ = true; - status->code_ = 0; - status->msg[0] = '\0'; +const char *upb_pbdecoder_getopname(unsigned int op) { +#define QUOTE(x) #x +#define EXPAND_AND_QUOTE(x) QUOTE(x) +#define OPNAME(x) OP_##x +#define OP(x) case OPNAME(x): return EXPAND_AND_QUOTE(OPNAME(x)); +#define T(x) OP(PARSE_##x) + /* Keep in sync with list in decoder.int.h. */ + switch ((opcode)op) { + T(DOUBLE) T(FLOAT) T(INT64) T(UINT64) T(INT32) T(FIXED64) T(FIXED32) + T(BOOL) T(UINT32) T(SFIXED32) T(SFIXED64) T(SINT32) T(SINT64) + OP(STARTMSG) OP(ENDMSG) OP(STARTSEQ) OP(ENDSEQ) OP(STARTSUBMSG) + OP(ENDSUBMSG) OP(STARTSTR) OP(STRING) OP(ENDSTR) OP(CALL) OP(RET) + OP(PUSHLENDELIM) OP(PUSHTAGDELIM) OP(SETDELIM) OP(CHECKDELIM) + OP(BRANCH) OP(TAG1) OP(TAG2) OP(TAGN) OP(SETDISPATCH) OP(POP) + OP(SETBIGGROUPNUM) OP(DISPATCH) OP(HALT) + } + return ""; +#undef OP +#undef T } -bool upb_ok(const upb_status *status) { return status->ok_; } - -upb_errorspace *upb_status_errspace(const upb_status *status) { - return status->error_space_; -} +#endif -int upb_status_errcode(const upb_status *status) { return status->code_; } +#ifdef UPB_DUMP_BYTECODE -const char *upb_status_errmsg(const upb_status *status) { return status->msg; } +static void dumpbc(uint32_t *p, uint32_t *end, FILE *f) { -void upb_status_seterrmsg(upb_status *status, const char *msg) { - if (!status) return; - status->ok_ = false; - strncpy(status->msg, msg, sizeof(status->msg)); - nullz(status); -} + uint32_t *begin = p; -void upb_status_seterrf(upb_status *status, const char *fmt, ...) { - va_list args; - va_start(args, fmt); - upb_status_vseterrf(status, fmt, args); - va_end(args); + while (p < end) { + fprintf(f, "%p %8tx", p, p - begin); + uint32_t instr = *p++; + uint8_t op = getop(instr); + fprintf(f, " %s", upb_pbdecoder_getopname(op)); + switch ((opcode)op) { + case OP_SETDISPATCH: { + const upb_inttable *dispatch; + memcpy(&dispatch, p, sizeof(void*)); + p += ptr_words; + const upb_pbdecodermethod *method = + (void *)((char *)dispatch - + offsetof(upb_pbdecodermethod, dispatch)); + fprintf(f, " %s", upb_msgdef_fullname( + upb_handlers_msgdef(method->dest_handlers_))); + break; + } + case OP_DISPATCH: + case OP_STARTMSG: + case OP_ENDMSG: + case OP_PUSHLENDELIM: + case OP_POP: + case OP_SETDELIM: + case OP_HALT: + case OP_RET: + break; + case OP_PARSE_DOUBLE: + case OP_PARSE_FLOAT: + case OP_PARSE_INT64: + case OP_PARSE_UINT64: + case OP_PARSE_INT32: + case OP_PARSE_FIXED64: + case OP_PARSE_FIXED32: + case OP_PARSE_BOOL: + case OP_PARSE_UINT32: + case OP_PARSE_SFIXED32: + case OP_PARSE_SFIXED64: + case OP_PARSE_SINT32: + case OP_PARSE_SINT64: + case OP_STARTSEQ: + case OP_ENDSEQ: + case OP_STARTSUBMSG: + case OP_ENDSUBMSG: + case OP_STARTSTR: + case OP_STRING: + case OP_ENDSTR: + case OP_PUSHTAGDELIM: + fprintf(f, " %d", instr >> 8); + break; + case OP_SETBIGGROUPNUM: + fprintf(f, " %d", *p++); + break; + case OP_CHECKDELIM: + case OP_CALL: + case OP_BRANCH: + fprintf(f, " =>0x%tx", p + getofs(instr) - begin); + break; + case OP_TAG1: + case OP_TAG2: { + fprintf(f, " tag:0x%x", instr >> 16); + if (getofs(instr)) { + fprintf(f, " =>0x%tx", p + getofs(instr) - begin); + } + break; + } + case OP_TAGN: { + uint64_t tag = *p++; + tag |= (uint64_t)*p++ << 32; + fprintf(f, " tag:0x%llx", (long long)tag); + fprintf(f, " n:%d", instr >> 16); + if (getofs(instr)) { + fprintf(f, " =>0x%tx", p + getofs(instr) - begin); + } + break; + } + } + fputs("\n", f); + } } -void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) { - if (!status) return; - status->ok_ = false; - _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args); - nullz(status); +#endif + +static uint64_t get_encoded_tag(const upb_fielddef *f, int wire_type) { + uint32_t tag = (upb_fielddef_number(f) << 3) | wire_type; + uint64_t encoded_tag = upb_vencode32(tag); + /* No tag should be greater than 5 bytes. */ + UPB_ASSERT(encoded_tag <= 0xffffffffff); + return encoded_tag; } -void upb_status_copy(upb_status *to, const upb_status *from) { - if (!to) return; - *to = *from; +static void putchecktag(compiler *c, const upb_fielddef *f, + int wire_type, int dest) { + uint64_t tag = get_encoded_tag(f, wire_type); + switch (upb_value_size(tag)) { + case 1: + putop(c, OP_TAG1, dest, tag); + break; + case 2: + putop(c, OP_TAG2, dest, tag); + break; + default: + putop(c, OP_TAGN, dest, tag); + break; + } } +static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) { + upb_selector_t selector; + bool ok = upb_handlers_getselector(f, type, &selector); + UPB_ASSERT(ok); + return selector; +} -/* upb_alloc ******************************************************************/ +/* Takes an existing, primary dispatch table entry and repacks it with a + * different alternate wire type. Called when we are inserting a secondary + * dispatch table entry for an alternate wire type. */ +static uint64_t repack(uint64_t dispatch, int new_wt2) { + uint64_t ofs; + uint8_t wt1; + uint8_t old_wt2; + upb_pbdecoder_unpackdispatch(dispatch, &ofs, &wt1, &old_wt2); + UPB_ASSERT(old_wt2 == NO_WIRE_TYPE); /* wt2 should not be set yet. */ + return upb_pbdecoder_packdispatch(ofs, wt1, new_wt2); +} -static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize, - size_t size) { - UPB_UNUSED(alloc); - UPB_UNUSED(oldsize); - if (size == 0) { - free(ptr); - return NULL; +/* Marks the current bytecode position as the dispatch target for this message, + * field, and wire type. */ +static void dispatchtarget(compiler *c, upb_pbdecodermethod *method, + const upb_fielddef *f, int wire_type) { + /* Offset is relative to msg base. */ + uint64_t ofs = pcofs(c) - method->code_base.ofs; + uint32_t fn = upb_fielddef_number(f); + upb_inttable *d = &method->dispatch; + upb_value v; + if (upb_inttable_remove(d, fn, &v)) { + /* TODO: prioritize based on packed setting in .proto file. */ + uint64_t repacked = repack(upb_value_getuint64(v), wire_type); + upb_inttable_insert(d, fn, upb_value_uint64(repacked)); + upb_inttable_insert(d, fn + UPB_MAX_FIELDNUMBER, upb_value_uint64(ofs)); } else { - return realloc(ptr, size); + uint64_t val = upb_pbdecoder_packdispatch(ofs, wire_type, NO_WIRE_TYPE); + upb_inttable_insert(d, fn, upb_value_uint64(val)); } } -upb_alloc upb_alloc_global = {&upb_global_allocfunc}; - - -/* upb_arena ******************************************************************/ - -/* Be conservative and choose 16 in case anyone is using SSE. */ -static const size_t maxalign = 16; +static void putpush(compiler *c, const upb_fielddef *f) { + if (upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_MESSAGE) { + putop(c, OP_PUSHLENDELIM); + } else { + uint32_t fn = upb_fielddef_number(f); + if (fn >= 1 << 24) { + putop(c, OP_PUSHTAGDELIM, 0); + putop(c, OP_SETBIGGROUPNUM, fn); + } else { + putop(c, OP_PUSHTAGDELIM, fn); + } + } +} -static size_t align_up_max(size_t size) { - return ((size + maxalign - 1) / maxalign) * maxalign; +static upb_pbdecodermethod *find_submethod(const compiler *c, + const upb_pbdecodermethod *method, + const upb_fielddef *f) { + const upb_handlers *sub = + upb_handlers_getsubhandlers(method->dest_handlers_, f); + upb_value v; + return upb_inttable_lookupptr(&c->group->methods, sub, &v) + ? upb_value_getptr(v) + : NULL; } -typedef struct mem_block { - struct mem_block *next; - size_t size; - size_t used; - bool owned; - /* Data follows. */ -} mem_block; +static void putsel(compiler *c, opcode op, upb_selector_t sel, + const upb_handlers *h) { + if (upb_handlers_gethandler(h, sel, NULL)) { + putop(c, op, sel); + } +} -typedef struct cleanup_ent { - struct cleanup_ent *next; - upb_cleanup_func *cleanup; - void *ud; -} cleanup_ent; +/* Puts an opcode to call a callback, but only if a callback actually exists for + * this field and handler type. */ +static void maybeput(compiler *c, opcode op, const upb_handlers *h, + const upb_fielddef *f, upb_handlertype_t type) { + putsel(c, op, getsel(f, type), h); +} -static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size, - bool owned) { - mem_block *block = ptr; +static bool haslazyhandlers(const upb_handlers *h, const upb_fielddef *f) { + if (!upb_fielddef_lazy(f)) + return false; - block->next = a->block_head; - block->size = size; - block->used = align_up_max(sizeof(mem_block)); - block->owned = owned; + return upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STARTSTR), NULL) || + upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STRING), NULL) || + upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_ENDSTR), NULL); +} - a->block_head = block; - /* TODO(haberman): ASAN poison. */ -} +/* bytecode compiler code generation ******************************************/ +/* Symbolic names for our local labels. */ +#define LABEL_LOOPSTART 1 /* Top of a repeated field loop. */ +#define LABEL_LOOPBREAK 2 /* To jump out of a repeated loop */ +#define LABEL_FIELD 3 /* Jump backward to find the most recent field. */ +#define LABEL_ENDMSG 4 /* To reach the OP_ENDMSG instr for this msg. */ -static mem_block *upb_arena_allocblock(upb_arena *a, size_t size) { - size_t block_size = UPB_MAX(size, a->next_block_size) + sizeof(mem_block); - mem_block *block = upb_malloc(a->block_alloc, block_size); +/* Generates bytecode to parse a single non-lazy message field. */ +static void generate_msgfield(compiler *c, const upb_fielddef *f, + upb_pbdecodermethod *method) { + const upb_handlers *h = upb_pbdecodermethod_desthandlers(method); + const upb_pbdecodermethod *sub_m = find_submethod(c, method, f); + int wire_type; - if (!block) { - return NULL; + if (!sub_m) { + /* Don't emit any code for this field at all; it will be parsed as an + * unknown field. + * + * TODO(haberman): we should change this to parse it as a string field + * instead. It will probably be faster, but more importantly, once we + * start vending unknown fields, a field shouldn't be treated as unknown + * just because it doesn't have subhandlers registered. */ + return; } - upb_arena_addblock(a, block, block_size, true); - a->next_block_size = UPB_MIN(block_size * 2, a->max_block_size); - - return block; -} + label(c, LABEL_FIELD); -static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize, - size_t size) { - upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */ - mem_block *block = a->block_head; - void *ret; + wire_type = + (upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_MESSAGE) + ? UPB_WIRE_TYPE_DELIMITED + : UPB_WIRE_TYPE_START_GROUP; - if (size == 0) { - return NULL; /* We are an arena, don't need individual frees. */ - } - - size = align_up_max(size); - - /* TODO(haberman): special-case if this is a realloc of the last alloc? */ - - if (!block || block->size - block->used < size) { - /* Slow path: have to allocate a new block. */ - block = upb_arena_allocblock(a, size); - - if (!block) { - return NULL; /* Out of memory. */ + if (upb_fielddef_isseq(f)) { + putop(c, OP_CHECKDELIM, LABEL_ENDMSG); + putchecktag(c, f, wire_type, LABEL_DISPATCH); + dispatchtarget(c, method, f, wire_type); + putop(c, OP_PUSHTAGDELIM, 0); + putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); + label(c, LABEL_LOOPSTART); + putpush(c, f); + putop(c, OP_STARTSUBMSG, getsel(f, UPB_HANDLER_STARTSUBMSG)); + putop(c, OP_CALL, sub_m); + putop(c, OP_POP); + maybeput(c, OP_ENDSUBMSG, h, f, UPB_HANDLER_ENDSUBMSG); + if (wire_type == UPB_WIRE_TYPE_DELIMITED) { + putop(c, OP_SETDELIM); + } + putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); + putchecktag(c, f, wire_type, LABEL_LOOPBREAK); + putop(c, OP_BRANCH, -LABEL_LOOPSTART); + label(c, LABEL_LOOPBREAK); + putop(c, OP_POP); + maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ); + } else { + putop(c, OP_CHECKDELIM, LABEL_ENDMSG); + putchecktag(c, f, wire_type, LABEL_DISPATCH); + dispatchtarget(c, method, f, wire_type); + putpush(c, f); + putop(c, OP_STARTSUBMSG, getsel(f, UPB_HANDLER_STARTSUBMSG)); + putop(c, OP_CALL, sub_m); + putop(c, OP_POP); + maybeput(c, OP_ENDSUBMSG, h, f, UPB_HANDLER_ENDSUBMSG); + if (wire_type == UPB_WIRE_TYPE_DELIMITED) { + putop(c, OP_SETDELIM); } } +} - ret = (char*)block + block->used; - block->used += size; +/* Generates bytecode to parse a single string or lazy submessage field. */ +static void generate_delimfield(compiler *c, const upb_fielddef *f, + upb_pbdecodermethod *method) { + const upb_handlers *h = upb_pbdecodermethod_desthandlers(method); - if (oldsize > 0) { - memcpy(ret, ptr, oldsize); /* Preserve existing data. */ + label(c, LABEL_FIELD); + if (upb_fielddef_isseq(f)) { + putop(c, OP_CHECKDELIM, LABEL_ENDMSG); + putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH); + dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED); + putop(c, OP_PUSHTAGDELIM, 0); + putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); + label(c, LABEL_LOOPSTART); + putop(c, OP_PUSHLENDELIM); + putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR)); + /* Need to emit even if no handler to skip past the string. */ + putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING)); + maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR); + putop(c, OP_POP); + putop(c, OP_SETDELIM); + putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); + putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_LOOPBREAK); + putop(c, OP_BRANCH, -LABEL_LOOPSTART); + label(c, LABEL_LOOPBREAK); + putop(c, OP_POP); + maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ); + } else { + putop(c, OP_CHECKDELIM, LABEL_ENDMSG); + putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH); + dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED); + putop(c, OP_PUSHLENDELIM); + putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR)); + putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING)); + maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR); + putop(c, OP_POP); + putop(c, OP_SETDELIM); } - - /* TODO(haberman): ASAN unpoison. */ - - a->bytes_allocated += size; - return ret; } -/* Public Arena API ***********************************************************/ +/* Generates bytecode to parse a single primitive field. */ +static void generate_primitivefield(compiler *c, const upb_fielddef *f, + upb_pbdecodermethod *method) { + const upb_handlers *h = upb_pbdecodermethod_desthandlers(method); + upb_descriptortype_t descriptor_type = upb_fielddef_descriptortype(f); + opcode parse_type; + upb_selector_t sel; + int wire_type; -void upb_arena_init(upb_arena *a) { - a->alloc.func = &upb_arena_doalloc; - a->block_alloc = &upb_alloc_global; - a->bytes_allocated = 0; - a->next_block_size = 256; - a->max_block_size = 16384; - a->cleanup_head = NULL; - a->block_head = NULL; -} + label(c, LABEL_FIELD); + + /* From a decoding perspective, ENUM is the same as INT32. */ + if (descriptor_type == UPB_DESCRIPTOR_TYPE_ENUM) + descriptor_type = UPB_DESCRIPTOR_TYPE_INT32; -void upb_arena_init2(upb_arena *a, void *mem, size_t size, upb_alloc *alloc) { - upb_arena_init(a); + parse_type = (opcode)descriptor_type; - if (size > sizeof(mem_block)) { - upb_arena_addblock(a, mem, size, false); - } + /* TODO(haberman): generate packed or non-packed first depending on "packed" + * setting in the fielddef. This will favor (in speed) whichever was + * specified. */ - if (alloc) { - a->block_alloc = alloc; + UPB_ASSERT((int)parse_type >= 0 && parse_type <= OP_MAX); + sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); + wire_type = upb_pb_native_wire_types[upb_fielddef_descriptortype(f)]; + if (upb_fielddef_isseq(f)) { + putop(c, OP_CHECKDELIM, LABEL_ENDMSG); + putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH); + dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED); + putop(c, OP_PUSHLENDELIM); + putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); /* Packed */ + label(c, LABEL_LOOPSTART); + putop(c, parse_type, sel); + putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); + putop(c, OP_BRANCH, -LABEL_LOOPSTART); + dispatchtarget(c, method, f, wire_type); + putop(c, OP_PUSHTAGDELIM, 0); + putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); /* Non-packed */ + label(c, LABEL_LOOPSTART); + putop(c, parse_type, sel); + putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); + putchecktag(c, f, wire_type, LABEL_LOOPBREAK); + putop(c, OP_BRANCH, -LABEL_LOOPSTART); + label(c, LABEL_LOOPBREAK); + putop(c, OP_POP); /* Packed and non-packed join. */ + maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ); + putop(c, OP_SETDELIM); /* Could remove for non-packed by dup ENDSEQ. */ + } else { + putop(c, OP_CHECKDELIM, LABEL_ENDMSG); + putchecktag(c, f, wire_type, LABEL_DISPATCH); + dispatchtarget(c, method, f, wire_type); + putop(c, parse_type, sel); } } -void upb_arena_uninit(upb_arena *a) { - cleanup_ent *ent = a->cleanup_head; - mem_block *block = a->block_head; +/* Adds bytecode for parsing the given message to the given decoderplan, + * while adding all dispatch targets to this message's dispatch table. */ +static void compile_method(compiler *c, upb_pbdecodermethod *method) { + const upb_handlers *h; + const upb_msgdef *md; + uint32_t* start_pc; + upb_msg_field_iter i; + upb_value val; - while (ent) { - ent->cleanup(ent->ud); - ent = ent->next; - } + UPB_ASSERT(method); - /* Must do this after running cleanup functions, because this will delete - * the memory we store our cleanup entries in! */ - while (block) { - mem_block *next = block->next; + /* Clear all entries in the dispatch table. */ + upb_inttable_uninit(&method->dispatch); + upb_inttable_init(&method->dispatch, UPB_CTYPE_UINT64); - if (block->owned) { - upb_free(a->block_alloc, block); + h = upb_pbdecodermethod_desthandlers(method); + md = upb_handlers_msgdef(h); + + method->code_base.ofs = pcofs(c); + putop(c, OP_SETDISPATCH, &method->dispatch); + putsel(c, OP_STARTMSG, UPB_STARTMSG_SELECTOR, h); + label(c, LABEL_FIELD); + start_pc = c->pc; + for(upb_msg_field_begin(&i, md); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + const upb_fielddef *f = upb_msg_iter_field(&i); + upb_fieldtype_t type = upb_fielddef_type(f); + + if (type == UPB_TYPE_MESSAGE && !(haslazyhandlers(h, f) && c->lazy)) { + generate_msgfield(c, f, method); + } else if (type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES || + type == UPB_TYPE_MESSAGE) { + generate_delimfield(c, f, method); + } else { + generate_primitivefield(c, f, method); } + } - block = next; + /* If there were no fields, or if no handlers were defined, we need to + * generate a non-empty loop body so that we can at least dispatch for unknown + * fields and check for the end of the message. */ + if (c->pc == start_pc) { + /* Check for end-of-message. */ + putop(c, OP_CHECKDELIM, LABEL_ENDMSG); + /* Unconditionally dispatch. */ + putop(c, OP_DISPATCH, 0); } - /* Protect against multiple-uninit. */ - a->cleanup_head = NULL; - a->block_head = NULL; -} + /* For now we just loop back to the last field of the message (or if none, + * the DISPATCH opcode for the message). */ + putop(c, OP_BRANCH, -LABEL_FIELD); -bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud) { - cleanup_ent *ent = upb_malloc(&a->alloc, sizeof(cleanup_ent)); - if (!ent) { - return false; /* Out of memory. */ - } + /* Insert both a label and a dispatch table entry for this end-of-msg. */ + label(c, LABEL_ENDMSG); + val = upb_value_uint64(pcofs(c) - method->code_base.ofs); + upb_inttable_insert(&method->dispatch, DISPATCH_ENDMSG, val); - ent->cleanup = func; - ent->ud = ud; - ent->next = a->cleanup_head; - a->cleanup_head = ent; + putsel(c, OP_ENDMSG, UPB_ENDMSG_SELECTOR, h); + putop(c, OP_RET); - return true; + upb_inttable_compact(&method->dispatch); } -size_t upb_arena_bytesallocated(const upb_arena *a) { - return a->bytes_allocated; -} +/* Populate "methods" with new upb_pbdecodermethod objects reachable from "h". + * Returns the method for these handlers. + * + * Generates a new method for every destination handlers reachable from "h". */ +static void find_methods(compiler *c, const upb_handlers *h) { + upb_value v; + upb_msg_field_iter i; + const upb_msgdef *md; + upb_pbdecodermethod *method; + if (upb_inttable_lookupptr(&c->group->methods, h, &v)) + return; -/* Standard error functions ***************************************************/ + method = newmethod(h, c->group); + upb_inttable_insertptr(&c->group->methods, h, upb_value_ptr(method)); -static bool default_err(void *ud, const upb_status *status) { - UPB_UNUSED(ud); - UPB_UNUSED(status); - return false; -} - -static bool write_err_to(void *ud, const upb_status *status) { - upb_status *copy_to = ud; - upb_status_copy(copy_to, status); - return false; + /* Find submethods. */ + md = upb_handlers_msgdef(h); + for(upb_msg_field_begin(&i, md); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + const upb_fielddef *f = upb_msg_iter_field(&i); + const upb_handlers *sub_h; + if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE && + (sub_h = upb_handlers_getsubhandlers(h, f)) != NULL) { + /* We only generate a decoder method for submessages with handlers. + * Others will be parsed as unknown fields. */ + find_methods(c, sub_h); + } + } } +/* (Re-)compile bytecode for all messages in "msgs." + * Overwrites any existing bytecode in "c". */ +static void compile_methods(compiler *c) { + upb_inttable_iter i; -/* upb_env ********************************************************************/ + /* Start over at the beginning of the bytecode. */ + c->pc = c->group->bytecode; -void upb_env_initonly(upb_env *e) { - e->ok_ = true; - e->error_func_ = &default_err; - e->error_ud_ = NULL; + upb_inttable_begin(&i, &c->group->methods); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i)); + compile_method(c, method); + } } -void upb_env_init(upb_env *e) { - upb_arena_init(&e->arena_); - upb_env_initonly(e); -} +static void set_bytecode_handlers(mgroup *g) { + upb_inttable_iter i; + upb_inttable_begin(&i, &g->methods); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + upb_pbdecodermethod *m = upb_value_getptr(upb_inttable_iter_value(&i)); + upb_byteshandler *h = &m->input_handler_; -void upb_env_init2(upb_env *e, void *mem, size_t n, upb_alloc *alloc) { - upb_arena_init2(&e->arena_, mem, n, alloc); - upb_env_initonly(e); -} + m->code_base.ptr = g->bytecode + m->code_base.ofs; -void upb_env_uninit(upb_env *e) { - upb_arena_uninit(&e->arena_); + upb_byteshandler_setstartstr(h, upb_pbdecoder_startbc, m->code_base.ptr); + upb_byteshandler_setstring(h, upb_pbdecoder_decode, g); + upb_byteshandler_setendstr(h, upb_pbdecoder_end, m); + } } -void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud) { - e->error_func_ = func; - e->error_ud_ = ud; -} -void upb_env_reporterrorsto(upb_env *e, upb_status *s) { - e->error_func_ = &write_err_to; - e->error_ud_ = s; -} +/* TODO(haberman): allow this to be constructed for an arbitrary set of dest + * handlers and other mgroups (but verify we have a transitive closure). */ +const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) { + mgroup *g; + compiler *c; -bool upb_env_reporterror(upb_env *e, const upb_status *status) { - e->ok_ = false; - return e->error_func_(e->error_ud_, status); -} + UPB_UNUSED(allowjit); -void *upb_env_malloc(upb_env *e, size_t size) { - return upb_malloc(&e->arena_.alloc, size); -} + g = newgroup(); + c = newcompiler(g, lazy); + find_methods(c, dest); -void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size) { - return upb_realloc(&e->arena_.alloc, ptr, oldsize, size); -} + /* We compile in two passes: + * 1. all messages are assigned relative offsets from the beginning of the + * bytecode (saved in method->code_base). + * 2. forwards OP_CALL instructions can be correctly linked since message + * offsets have been previously assigned. + * + * Could avoid the second pass by linking OP_CALL instructions somehow. */ + compile_methods(c); + compile_methods(c); + g->bytecode_end = c->pc; + freecompiler(c); -void upb_env_free(upb_env *e, void *ptr) { - upb_free(&e->arena_.alloc, ptr); -} +#ifdef UPB_DUMP_BYTECODE + { + FILE *f = fopen("/tmp/upb-bytecode", "w"); + UPB_ASSERT(f); + dumpbc(g->bytecode, g->bytecode_end, stderr); + dumpbc(g->bytecode, g->bytecode_end, f); + fclose(f); -bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud) { - return upb_arena_addcleanup(&e->arena_, func, ud); -} + f = fopen("/tmp/upb-bytecode.bin", "wb"); + UPB_ASSERT(f); + fwrite(g->bytecode, 1, g->bytecode_end - g->bytecode, f); + fclose(f); + } +#endif -size_t upb_env_bytesallocated(const upb_env *e) { - return upb_arena_bytesallocated(&e->arena_); + set_bytecode_handlers(g); + return g; } -/* This file was generated by upbc (the upb compiler) from the input - * file: - * - * upb/descriptor/descriptor.proto - * - * Do not edit -- your changes will be discarded when the file is - * regenerated. */ -static const upb_msgdef msgs[22]; -static const upb_fielddef fields[107]; -static const upb_enumdef enums[5]; -static const upb_tabent strentries[236]; -static const upb_tabent intentries[18]; -static const upb_tabval arrays[187]; +/* upb_pbcodecache ************************************************************/ -#ifdef UPB_DEBUG_REFS -static upb_inttable reftables[268]; -#endif +upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) { + upb_pbcodecache *c = upb_gmalloc(sizeof(*c)); -static const upb_msgdef msgs[22] = { - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto", 41, 8, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[0], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[0]), false, UPB_SYNTAX_PROTO2, &reftables[0], &reftables[1]), - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ExtensionRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[11], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[16]), false, UPB_SYNTAX_PROTO2, &reftables[2], &reftables[3]), - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ReservedRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[14], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[20]), false, UPB_SYNTAX_PROTO2, &reftables[4], &reftables[5]), - UPB_MSGDEF_INIT("google.protobuf.EnumDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[17], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[24]), false, UPB_SYNTAX_PROTO2, &reftables[6], &reftables[7]), - UPB_MSGDEF_INIT("google.protobuf.EnumOptions", 9, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[0], &arrays[21], 4, 2), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[28]), false, UPB_SYNTAX_PROTO2, &reftables[8], &reftables[9]), - UPB_MSGDEF_INIT("google.protobuf.EnumValueDescriptorProto", 9, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[25], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[32]), false, UPB_SYNTAX_PROTO2, &reftables[10], &reftables[11]), - UPB_MSGDEF_INIT("google.protobuf.EnumValueOptions", 8, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[2], &arrays[29], 2, 1), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[36]), false, UPB_SYNTAX_PROTO2, &reftables[12], &reftables[13]), - UPB_MSGDEF_INIT("google.protobuf.FieldDescriptorProto", 24, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[31], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[40]), false, UPB_SYNTAX_PROTO2, &reftables[14], &reftables[15]), - UPB_MSGDEF_INIT("google.protobuf.FieldOptions", 13, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[4], &arrays[42], 11, 6), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[56]), false, UPB_SYNTAX_PROTO2, &reftables[16], &reftables[17]), - UPB_MSGDEF_INIT("google.protobuf.FileDescriptorProto", 43, 6, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[53], 13, 12), UPB_STRTABLE_INIT(12, 15, UPB_CTYPE_PTR, 4, &strentries[72]), false, UPB_SYNTAX_PROTO2, &reftables[18], &reftables[19]), - UPB_MSGDEF_INIT("google.protobuf.FileDescriptorSet", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[66], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[88]), false, UPB_SYNTAX_PROTO2, &reftables[20], &reftables[21]), - UPB_MSGDEF_INIT("google.protobuf.FileOptions", 38, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[6], &arrays[68], 42, 17), UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_PTR, 5, &strentries[92]), false, UPB_SYNTAX_PROTO2, &reftables[22], &reftables[23]), - UPB_MSGDEF_INIT("google.protobuf.MessageOptions", 11, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[8], &arrays[110], 8, 4), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[124]), false, UPB_SYNTAX_PROTO2, &reftables[24], &reftables[25]), - UPB_MSGDEF_INIT("google.protobuf.MethodDescriptorProto", 16, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[118], 7, 6), UPB_STRTABLE_INIT(6, 7, UPB_CTYPE_PTR, 3, &strentries[132]), false, UPB_SYNTAX_PROTO2, &reftables[26], &reftables[27]), - UPB_MSGDEF_INIT("google.protobuf.MethodOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[10], &arrays[125], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[140]), false, UPB_SYNTAX_PROTO2, &reftables[28], &reftables[29]), - UPB_MSGDEF_INIT("google.protobuf.OneofDescriptorProto", 6, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[126], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[144]), false, UPB_SYNTAX_PROTO2, &reftables[30], &reftables[31]), - UPB_MSGDEF_INIT("google.protobuf.ServiceDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[128], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[148]), false, UPB_SYNTAX_PROTO2, &reftables[32], &reftables[33]), - UPB_MSGDEF_INIT("google.protobuf.ServiceOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[14], &arrays[132], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[152]), false, UPB_SYNTAX_PROTO2, &reftables[34], &reftables[35]), - UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[133], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[156]), false, UPB_SYNTAX_PROTO2, &reftables[36], &reftables[37]), - UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo.Location", 20, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[135], 7, 5), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[160]), false, UPB_SYNTAX_PROTO2, &reftables[38], &reftables[39]), - UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption", 19, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[142], 9, 7), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[168]), false, UPB_SYNTAX_PROTO2, &reftables[40], &reftables[41]), - UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption.NamePart", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[151], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[184]), false, UPB_SYNTAX_PROTO2, &reftables[42], &reftables[43]), -}; + if (!c) return NULL; -static const upb_fielddef fields[107] = { - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "aggregate_value", 8, &msgs[20], NULL, 16, 6, {0},&reftables[44], &reftables[45]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "allow_alias", 2, &msgs[4], NULL, 7, 1, {0},&reftables[46], &reftables[47]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_enable_arenas", 31, &msgs[11], NULL, 24, 12, {0},&reftables[48], &reftables[49]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_generic_services", 16, &msgs[11], NULL, 18, 6, {0},&reftables[50], &reftables[51]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "client_streaming", 5, &msgs[13], NULL, 14, 4, {0},&reftables[52], &reftables[53]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "csharp_namespace", 37, &msgs[11], NULL, 28, 14, {0},&reftables[54], &reftables[55]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "ctype", 1, &msgs[8], (const upb_def*)(&enums[2]), 7, 1, {0},&reftables[56], &reftables[57]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "default_value", 7, &msgs[7], NULL, 17, 7, {0},&reftables[58], &reftables[59]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "dependency", 3, &msgs[9], NULL, 31, 8, {0},&reftables[60], &reftables[61]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[8], NULL, 9, 3, {0},&reftables[62], &reftables[63]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 33, &msgs[14], NULL, 7, 1, {0},&reftables[64], &reftables[65]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[12], NULL, 9, 3, {0},&reftables[66], &reftables[67]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 23, &msgs[11], NULL, 22, 10, {0},&reftables[68], &reftables[69]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 1, &msgs[6], NULL, 7, 1, {0},&reftables[70], &reftables[71]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[4], NULL, 8, 2, {0},&reftables[72], &reftables[73]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 33, &msgs[17], NULL, 7, 1, {0},&reftables[74], &reftables[75]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_DOUBLE, 0, false, false, false, false, "double_value", 6, &msgs[20], NULL, 12, 4, {0},&reftables[76], &reftables[77]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[2], NULL, 4, 1, {0},&reftables[78], &reftables[79]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[1], NULL, 4, 1, {0},&reftables[80], &reftables[81]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 5, &msgs[9], (const upb_def*)(&msgs[3]), 14, 1, {0},&reftables[82], &reftables[83]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 4, &msgs[0], (const upb_def*)(&msgs[3]), 19, 2, {0},&reftables[84], &reftables[85]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "extendee", 2, &msgs[7], NULL, 8, 2, {0},&reftables[86], &reftables[87]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 6, &msgs[0], (const upb_def*)(&msgs[7]), 25, 4, {0},&reftables[88], &reftables[89]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 7, &msgs[9], (const upb_def*)(&msgs[7]), 20, 3, {0},&reftables[90], &reftables[91]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension_range", 5, &msgs[0], (const upb_def*)(&msgs[1]), 22, 3, {0},&reftables[92], &reftables[93]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "field", 2, &msgs[0], (const upb_def*)(&msgs[7]), 13, 0, {0},&reftables[94], &reftables[95]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "file", 1, &msgs[10], (const upb_def*)(&msgs[9]), 6, 0, {0},&reftables[96], &reftables[97]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "go_package", 11, &msgs[11], NULL, 15, 5, {0},&reftables[98], &reftables[99]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "identifier_value", 3, &msgs[20], NULL, 7, 1, {0},&reftables[100], &reftables[101]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "input_type", 2, &msgs[13], NULL, 8, 2, {0},&reftables[102], &reftables[103]), - UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_BOOL, 0, false, false, false, false, "is_extension", 2, &msgs[21], NULL, 6, 1, {0},&reftables[104], &reftables[105]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generate_equals_and_hash", 20, &msgs[11], NULL, 21, 9, {0},&reftables[106], &reftables[107]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generic_services", 17, &msgs[11], NULL, 19, 7, {0},&reftables[108], &reftables[109]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_multiple_files", 10, &msgs[11], NULL, 14, 4, {0},&reftables[110], &reftables[111]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_outer_classname", 8, &msgs[11], NULL, 10, 2, {0},&reftables[112], &reftables[113]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_package", 1, &msgs[11], NULL, 7, 1, {0},&reftables[114], &reftables[115]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_string_check_utf8", 27, &msgs[11], NULL, 23, 11, {0},&reftables[116], &reftables[117]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "javanano_use_deprecated_package", 38, &msgs[11], NULL, 31, 15, {0},&reftables[118], &reftables[119]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "json_name", 10, &msgs[7], NULL, 21, 9, {0},&reftables[120], &reftables[121]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "jstype", 6, &msgs[8], (const upb_def*)(&enums[3]), 11, 5, {0},&reftables[122], &reftables[123]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "label", 4, &msgs[7], (const upb_def*)(&enums[0]), 12, 4, {0},&reftables[124], &reftables[125]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "lazy", 5, &msgs[8], NULL, 10, 4, {0},&reftables[126], &reftables[127]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "leading_comments", 3, &msgs[19], NULL, 9, 2, {0},&reftables[128], &reftables[129]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "leading_detached_comments", 6, &msgs[19], NULL, 17, 4, {0},&reftables[130], &reftables[131]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "location", 1, &msgs[18], (const upb_def*)(&msgs[19]), 6, 0, {0},&reftables[132], &reftables[133]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "map_entry", 7, &msgs[12], NULL, 10, 4, {0},&reftables[134], &reftables[135]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "message_set_wire_format", 1, &msgs[12], NULL, 7, 1, {0},&reftables[136], &reftables[137]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "message_type", 4, &msgs[9], (const upb_def*)(&msgs[0]), 11, 0, {0},&reftables[138], &reftables[139]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "method", 2, &msgs[16], (const upb_def*)(&msgs[13]), 7, 0, {0},&reftables[140], &reftables[141]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "name", 2, &msgs[20], (const upb_def*)(&msgs[21]), 6, 0, {0},&reftables[142], &reftables[143]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[5], NULL, 5, 1, {0},&reftables[144], &reftables[145]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[9], NULL, 23, 6, {0},&reftables[146], &reftables[147]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[3], NULL, 9, 2, {0},&reftables[148], &reftables[149]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[16], NULL, 9, 2, {0},&reftables[150], &reftables[151]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[15], NULL, 3, 0, {0},&reftables[152], &reftables[153]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[13], NULL, 5, 1, {0},&reftables[154], &reftables[155]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[7], NULL, 5, 1, {0},&reftables[156], &reftables[157]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[0], NULL, 33, 8, {0},&reftables[158], &reftables[159]), - UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_STRING, 0, false, false, false, false, "name_part", 1, &msgs[21], NULL, 3, 0, {0},&reftables[160], &reftables[161]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT64, UPB_INTFMT_VARIABLE, false, false, false, false, "negative_int_value", 5, &msgs[20], NULL, 11, 3, {0},&reftables[162], &reftables[163]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "nested_type", 3, &msgs[0], (const upb_def*)(&msgs[0]), 16, 1, {0},&reftables[164], &reftables[165]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "no_standard_descriptor_accessor", 2, &msgs[12], NULL, 8, 2, {0},&reftables[166], &reftables[167]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 3, &msgs[7], NULL, 11, 3, {0},&reftables[168], &reftables[169]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 2, &msgs[5], NULL, 8, 2, {0},&reftables[170], &reftables[171]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "objc_class_prefix", 36, &msgs[11], NULL, 25, 13, {0},&reftables[172], &reftables[173]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "oneof_decl", 8, &msgs[0], (const upb_def*)(&msgs[15]), 29, 6, {0},&reftables[174], &reftables[175]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "oneof_index", 9, &msgs[7], NULL, 20, 8, {0},&reftables[176], &reftables[177]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "optimize_for", 9, &msgs[11], (const upb_def*)(&enums[4]), 13, 3, {0},&reftables[178], &reftables[179]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 7, &msgs[0], (const upb_def*)(&msgs[12]), 26, 5, {0},&reftables[180], &reftables[181]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[9], (const upb_def*)(&msgs[11]), 21, 4, {0},&reftables[182], &reftables[183]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[7], (const upb_def*)(&msgs[8]), 4, 0, {0},&reftables[184], &reftables[185]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 4, &msgs[13], (const upb_def*)(&msgs[14]), 4, 0, {0},&reftables[186], &reftables[187]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[16], (const upb_def*)(&msgs[17]), 8, 1, {0},&reftables[188], &reftables[189]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[3], (const upb_def*)(&msgs[4]), 8, 1, {0},&reftables[190], &reftables[191]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[5], (const upb_def*)(&msgs[6]), 4, 0, {0},&reftables[192], &reftables[193]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "output_type", 3, &msgs[13], NULL, 11, 3, {0},&reftables[194], &reftables[195]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "package", 2, &msgs[9], NULL, 26, 7, {0},&reftables[196], &reftables[197]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "packed", 2, &msgs[8], NULL, 8, 2, {0},&reftables[198], &reftables[199]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "path", 1, &msgs[19], NULL, 5, 0, {0},&reftables[200], &reftables[201]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "php_class_prefix", 40, &msgs[11], NULL, 32, 16, {0},&reftables[202], &reftables[203]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "php_namespace", 41, &msgs[11], NULL, 35, 17, {0},&reftables[204], &reftables[205]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_UINT64, UPB_INTFMT_VARIABLE, false, false, false, false, "positive_int_value", 4, &msgs[20], NULL, 10, 2, {0},&reftables[206], &reftables[207]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "public_dependency", 10, &msgs[9], NULL, 36, 9, {0},&reftables[208], &reftables[209]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "py_generic_services", 18, &msgs[11], NULL, 20, 8, {0},&reftables[210], &reftables[211]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "reserved_name", 10, &msgs[0], NULL, 38, 9, {0},&reftables[212], &reftables[213]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "reserved_range", 9, &msgs[0], (const upb_def*)(&msgs[2]), 32, 7, {0},&reftables[214], &reftables[215]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "server_streaming", 6, &msgs[13], NULL, 15, 5, {0},&reftables[216], &reftables[217]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "service", 6, &msgs[9], (const upb_def*)(&msgs[16]), 17, 2, {0},&reftables[218], &reftables[219]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "source_code_info", 9, &msgs[9], (const upb_def*)(&msgs[18]), 22, 5, {0},&reftables[220], &reftables[221]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "span", 2, &msgs[19], NULL, 8, 1, {0},&reftables[222], &reftables[223]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[2], NULL, 3, 0, {0},&reftables[224], &reftables[225]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[1], NULL, 3, 0, {0},&reftables[226], &reftables[227]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BYTES, 0, false, false, false, false, "string_value", 7, &msgs[20], NULL, 13, 5, {0},&reftables[228], &reftables[229]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "syntax", 12, &msgs[9], NULL, 40, 11, {0},&reftables[230], &reftables[231]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "trailing_comments", 4, &msgs[19], NULL, 12, 3, {0},&reftables[232], &reftables[233]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "type", 5, &msgs[7], (const upb_def*)(&enums[1]), 13, 5, {0},&reftables[234], &reftables[235]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "type_name", 6, &msgs[7], NULL, 14, 6, {0},&reftables[236], &reftables[237]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[12], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[238], &reftables[239]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[17], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[240], &reftables[241]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[11], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[242], &reftables[243]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[14], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[244], &reftables[245]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[8], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[246], &reftables[247]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[6], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[248], &reftables[249]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[4], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[250], &reftables[251]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "value", 2, &msgs[3], (const upb_def*)(&msgs[5]), 7, 0, {0},&reftables[252], &reftables[253]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "weak", 10, &msgs[8], NULL, 12, 6, {0},&reftables[254], &reftables[255]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "weak_dependency", 11, &msgs[9], NULL, 39, 10, {0},&reftables[256], &reftables[257]), -}; + c->dest = dest; + c->allow_jit = true; + c->lazy = false; -static const upb_enumdef enums[5] = { - UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Label", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[188]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[154], 4, 3), 0, &reftables[258], &reftables[259]), - UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Type", UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_INT32, 5, &strentries[192]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[158], 19, 18), 0, &reftables[260], &reftables[261]), - UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.CType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[224]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[177], 3, 3), 0, &reftables[262], &reftables[263]), - UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.JSType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[228]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[180], 3, 3), 0, &reftables[264], &reftables[265]), - UPB_ENUMDEF_INIT("google.protobuf.FileOptions.OptimizeMode", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[232]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[183], 4, 3), 0, &reftables[266], &reftables[267]), -}; + c->arena = upb_arena_new(); + if (!upb_inttable_init(&c->groups, UPB_CTYPE_CONSTPTR)) return NULL; -static const upb_tabent strentries[236] = { - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "extension"), UPB_TABVALUE_PTR_INIT(&fields[22]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "reserved_name"), UPB_TABVALUE_PTR_INIT(&fields[84]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[57]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "field"), UPB_TABVALUE_PTR_INIT(&fields[25]), &strentries[12]}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "extension_range"), UPB_TABVALUE_PTR_INIT(&fields[24]), &strentries[14]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "nested_type"), UPB_TABVALUE_PTR_INIT(&fields[60]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "reserved_range"), UPB_TABVALUE_PTR_INIT(&fields[85]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[68]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "oneof_decl"), UPB_TABVALUE_PTR_INIT(&fields[65]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "enum_type"), UPB_TABVALUE_PTR_INIT(&fields[20]), &strentries[13]}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "start"), UPB_TABVALUE_PTR_INIT(&fields[91]), NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "end"), UPB_TABVALUE_PTR_INIT(&fields[18]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "start"), UPB_TABVALUE_PTR_INIT(&fields[90]), NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "end"), UPB_TABVALUE_PTR_INIT(&fields[17]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[104]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[73]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[52]), &strentries[26]}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[103]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[14]), NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "allow_alias"), UPB_TABVALUE_PTR_INIT(&fields[1]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "number"), UPB_TABVALUE_PTR_INIT(&fields[63]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[74]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[50]), &strentries[34]}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[102]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[13]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "oneof_index"), UPB_TABVALUE_PTR_INIT(&fields[66]), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "label"), UPB_TABVALUE_PTR_INIT(&fields[40]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[56]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "number"), UPB_TABVALUE_PTR_INIT(&fields[62]), &strentries[53]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\010", "\000", "\000", "\000", "extendee"), UPB_TABVALUE_PTR_INIT(&fields[21]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "type_name"), UPB_TABVALUE_PTR_INIT(&fields[96]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "json_name"), UPB_TABVALUE_PTR_INIT(&fields[38]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "type"), UPB_TABVALUE_PTR_INIT(&fields[95]), &strentries[50]}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "default_value"), UPB_TABVALUE_PTR_INIT(&fields[7]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[70]), NULL}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[101]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "weak"), UPB_TABVALUE_PTR_INIT(&fields[105]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "packed"), UPB_TABVALUE_PTR_INIT(&fields[77]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "lazy"), UPB_TABVALUE_PTR_INIT(&fields[41]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "ctype"), UPB_TABVALUE_PTR_INIT(&fields[6]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "jstype"), UPB_TABVALUE_PTR_INIT(&fields[39]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[9]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "extension"), UPB_TABVALUE_PTR_INIT(&fields[23]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "weak_dependency"), UPB_TABVALUE_PTR_INIT(&fields[106]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[51]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "service"), UPB_TABVALUE_PTR_INIT(&fields[87]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "source_code_info"), UPB_TABVALUE_PTR_INIT(&fields[88]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "syntax"), UPB_TABVALUE_PTR_INIT(&fields[93]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "dependency"), UPB_TABVALUE_PTR_INIT(&fields[8]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "message_type"), UPB_TABVALUE_PTR_INIT(&fields[47]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "package"), UPB_TABVALUE_PTR_INIT(&fields[76]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[69]), &strentries[86]}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "enum_type"), UPB_TABVALUE_PTR_INIT(&fields[19]), NULL}, - {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "public_dependency"), UPB_TABVALUE_PTR_INIT(&fields[82]), &strentries[85]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "file"), UPB_TABVALUE_PTR_INIT(&fields[26]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "cc_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[3]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "csharp_namespace"), UPB_TABVALUE_PTR_INIT(&fields[5]), &strentries[116]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "go_package"), UPB_TABVALUE_PTR_INIT(&fields[27]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "java_package"), UPB_TABVALUE_PTR_INIT(&fields[35]), &strentries[120]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "java_outer_classname"), UPB_TABVALUE_PTR_INIT(&fields[34]), NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "php_namespace"), UPB_TABVALUE_PTR_INIT(&fields[80]), &strentries[113]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "java_multiple_files"), UPB_TABVALUE_PTR_INIT(&fields[33]), &strentries[117]}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[99]), NULL}, - {UPB_TABKEY_STR("\025", "\000", "\000", "\000", "java_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[32]), &strentries[118]}, - {UPB_TABKEY_STR("\035", "\000", "\000", "\000", "java_generate_equals_and_hash"), UPB_TABVALUE_PTR_INIT(&fields[31]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "php_class_prefix"), UPB_TABVALUE_PTR_INIT(&fields[79]), NULL}, - {UPB_TABKEY_STR("\037", "\000", "\000", "\000", "javanano_use_deprecated_package"), UPB_TABVALUE_PTR_INIT(&fields[37]), &strentries[123]}, - {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "py_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[83]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "optimize_for"), UPB_TABVALUE_PTR_INIT(&fields[67]), NULL}, - {UPB_TABKEY_STR("\026", "\000", "\000", "\000", "java_string_check_utf8"), UPB_TABVALUE_PTR_INIT(&fields[36]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[12]), &strentries[119]}, - {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "objc_class_prefix"), UPB_TABVALUE_PTR_INIT(&fields[64]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "cc_enable_arenas"), UPB_TABVALUE_PTR_INIT(&fields[2]), NULL}, - {UPB_TABKEY_STR("\027", "\000", "\000", "\000", "message_set_wire_format"), UPB_TABVALUE_PTR_INIT(&fields[46]), &strentries[128]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[97]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[11]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "map_entry"), UPB_TABVALUE_PTR_INIT(&fields[45]), NULL}, - {UPB_TABKEY_STR("\037", "\000", "\000", "\000", "no_standard_descriptor_accessor"), UPB_TABVALUE_PTR_INIT(&fields[61]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "client_streaming"), UPB_TABVALUE_PTR_INIT(&fields[4]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "server_streaming"), UPB_TABVALUE_PTR_INIT(&fields[86]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[55]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "input_type"), UPB_TABVALUE_PTR_INIT(&fields[29]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "output_type"), UPB_TABVALUE_PTR_INIT(&fields[75]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[71]), NULL}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[100]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[54]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[72]), &strentries[150]}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "method"), UPB_TABVALUE_PTR_INIT(&fields[48]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[53]), &strentries[149]}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[98]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[15]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\010", "\000", "\000", "\000", "location"), UPB_TABVALUE_PTR_INIT(&fields[44]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "span"), UPB_TABVALUE_PTR_INIT(&fields[89]), &strentries[167]}, - {UPB_TABKEY_STR("\031", "\000", "\000", "\000", "leading_detached_comments"), UPB_TABVALUE_PTR_INIT(&fields[43]), &strentries[165]}, - {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "trailing_comments"), UPB_TABVALUE_PTR_INIT(&fields[94]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "leading_comments"), UPB_TABVALUE_PTR_INIT(&fields[42]), &strentries[164]}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "path"), UPB_TABVALUE_PTR_INIT(&fields[78]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "double_value"), UPB_TABVALUE_PTR_INIT(&fields[16]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[49]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\022", "\000", "\000", "\000", "negative_int_value"), UPB_TABVALUE_PTR_INIT(&fields[59]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "aggregate_value"), UPB_TABVALUE_PTR_INIT(&fields[0]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\022", "\000", "\000", "\000", "positive_int_value"), UPB_TABVALUE_PTR_INIT(&fields[81]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "identifier_value"), UPB_TABVALUE_PTR_INIT(&fields[28]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "string_value"), UPB_TABVALUE_PTR_INIT(&fields[92]), &strentries[182]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "is_extension"), UPB_TABVALUE_PTR_INIT(&fields[30]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "name_part"), UPB_TABVALUE_PTR_INIT(&fields[58]), NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_REQUIRED"), UPB_TABVALUE_INT_INIT(2), &strentries[190]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_REPEATED"), UPB_TABVALUE_INT_INIT(3), NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_OPTIONAL"), UPB_TABVALUE_INT_INIT(1), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_FIXED64"), UPB_TABVALUE_INT_INIT(6), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_STRING"), UPB_TABVALUE_INT_INIT(9), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_FLOAT"), UPB_TABVALUE_INT_INIT(2), &strentries[221]}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_DOUBLE"), UPB_TABVALUE_INT_INIT(1), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_INT32"), UPB_TABVALUE_INT_INIT(5), NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "TYPE_SFIXED32"), UPB_TABVALUE_INT_INIT(15), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_FIXED32"), UPB_TABVALUE_INT_INIT(7), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_MESSAGE"), UPB_TABVALUE_INT_INIT(11), &strentries[222]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_INT64"), UPB_TABVALUE_INT_INIT(3), &strentries[219]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "TYPE_ENUM"), UPB_TABVALUE_INT_INIT(14), NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_UINT32"), UPB_TABVALUE_INT_INIT(13), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_UINT64"), UPB_TABVALUE_INT_INIT(4), &strentries[218]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "TYPE_SFIXED64"), UPB_TABVALUE_INT_INIT(16), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_BYTES"), UPB_TABVALUE_INT_INIT(12), NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_SINT64"), UPB_TABVALUE_INT_INIT(18), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "TYPE_BOOL"), UPB_TABVALUE_INT_INIT(8), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_GROUP"), UPB_TABVALUE_INT_INIT(10), NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_SINT32"), UPB_TABVALUE_INT_INIT(17), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "CORD"), UPB_TABVALUE_INT_INIT(1), NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "STRING"), UPB_TABVALUE_INT_INIT(0), &strentries[225]}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "STRING_PIECE"), UPB_TABVALUE_INT_INIT(2), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_NORMAL"), UPB_TABVALUE_INT_INIT(0), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_NUMBER"), UPB_TABVALUE_INT_INIT(2), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_STRING"), UPB_TABVALUE_INT_INIT(1), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "CODE_SIZE"), UPB_TABVALUE_INT_INIT(2), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "SPEED"), UPB_TABVALUE_INT_INIT(1), &strentries[235]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "LITE_RUNTIME"), UPB_TABVALUE_INT_INIT(3), NULL}, -}; + return c; +} -static const upb_tabent intentries[18] = { - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[103]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[102]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[101]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[99]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[97]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(33), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[100]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(33), UPB_TABVALUE_PTR_INIT(&fields[15]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[98]), NULL}, -}; +void upb_pbcodecache_free(upb_pbcodecache *c) { + size_t i; -static const upb_tabval arrays[187] = { - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[57]), - UPB_TABVALUE_PTR_INIT(&fields[25]), - UPB_TABVALUE_PTR_INIT(&fields[60]), - UPB_TABVALUE_PTR_INIT(&fields[20]), - UPB_TABVALUE_PTR_INIT(&fields[24]), - UPB_TABVALUE_PTR_INIT(&fields[22]), - UPB_TABVALUE_PTR_INIT(&fields[68]), - UPB_TABVALUE_PTR_INIT(&fields[65]), - UPB_TABVALUE_PTR_INIT(&fields[85]), - UPB_TABVALUE_PTR_INIT(&fields[84]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[91]), - UPB_TABVALUE_PTR_INIT(&fields[18]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[90]), - UPB_TABVALUE_PTR_INIT(&fields[17]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[52]), - UPB_TABVALUE_PTR_INIT(&fields[104]), - UPB_TABVALUE_PTR_INIT(&fields[73]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[1]), - UPB_TABVALUE_PTR_INIT(&fields[14]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[50]), - UPB_TABVALUE_PTR_INIT(&fields[63]), - UPB_TABVALUE_PTR_INIT(&fields[74]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[13]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[56]), - UPB_TABVALUE_PTR_INIT(&fields[21]), - UPB_TABVALUE_PTR_INIT(&fields[62]), - UPB_TABVALUE_PTR_INIT(&fields[40]), - UPB_TABVALUE_PTR_INIT(&fields[95]), - UPB_TABVALUE_PTR_INIT(&fields[96]), - UPB_TABVALUE_PTR_INIT(&fields[7]), - UPB_TABVALUE_PTR_INIT(&fields[70]), - UPB_TABVALUE_PTR_INIT(&fields[66]), - UPB_TABVALUE_PTR_INIT(&fields[38]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[6]), - UPB_TABVALUE_PTR_INIT(&fields[77]), - UPB_TABVALUE_PTR_INIT(&fields[9]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[41]), - UPB_TABVALUE_PTR_INIT(&fields[39]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[105]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[51]), - UPB_TABVALUE_PTR_INIT(&fields[76]), - UPB_TABVALUE_PTR_INIT(&fields[8]), - UPB_TABVALUE_PTR_INIT(&fields[47]), - UPB_TABVALUE_PTR_INIT(&fields[19]), - UPB_TABVALUE_PTR_INIT(&fields[87]), - UPB_TABVALUE_PTR_INIT(&fields[23]), - UPB_TABVALUE_PTR_INIT(&fields[69]), - UPB_TABVALUE_PTR_INIT(&fields[88]), - UPB_TABVALUE_PTR_INIT(&fields[82]), - UPB_TABVALUE_PTR_INIT(&fields[106]), - UPB_TABVALUE_PTR_INIT(&fields[93]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[26]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[35]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[34]), - UPB_TABVALUE_PTR_INIT(&fields[67]), - UPB_TABVALUE_PTR_INIT(&fields[33]), - UPB_TABVALUE_PTR_INIT(&fields[27]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[3]), - UPB_TABVALUE_PTR_INIT(&fields[32]), - UPB_TABVALUE_PTR_INIT(&fields[83]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[31]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[12]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[36]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[2]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[64]), - UPB_TABVALUE_PTR_INIT(&fields[5]), - UPB_TABVALUE_PTR_INIT(&fields[37]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[79]), - UPB_TABVALUE_PTR_INIT(&fields[80]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[46]), - UPB_TABVALUE_PTR_INIT(&fields[61]), - UPB_TABVALUE_PTR_INIT(&fields[11]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[45]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[55]), - UPB_TABVALUE_PTR_INIT(&fields[29]), - UPB_TABVALUE_PTR_INIT(&fields[75]), - UPB_TABVALUE_PTR_INIT(&fields[71]), - UPB_TABVALUE_PTR_INIT(&fields[4]), - UPB_TABVALUE_PTR_INIT(&fields[86]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[54]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[53]), - UPB_TABVALUE_PTR_INIT(&fields[48]), - UPB_TABVALUE_PTR_INIT(&fields[72]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[44]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[78]), - UPB_TABVALUE_PTR_INIT(&fields[89]), - UPB_TABVALUE_PTR_INIT(&fields[42]), - UPB_TABVALUE_PTR_INIT(&fields[94]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[43]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[49]), - UPB_TABVALUE_PTR_INIT(&fields[28]), - UPB_TABVALUE_PTR_INIT(&fields[81]), - UPB_TABVALUE_PTR_INIT(&fields[59]), - UPB_TABVALUE_PTR_INIT(&fields[16]), - UPB_TABVALUE_PTR_INIT(&fields[92]), - UPB_TABVALUE_PTR_INIT(&fields[0]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[58]), - UPB_TABVALUE_PTR_INIT(&fields[30]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT("LABEL_OPTIONAL"), - UPB_TABVALUE_PTR_INIT("LABEL_REQUIRED"), - UPB_TABVALUE_PTR_INIT("LABEL_REPEATED"), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT("TYPE_DOUBLE"), - UPB_TABVALUE_PTR_INIT("TYPE_FLOAT"), - UPB_TABVALUE_PTR_INIT("TYPE_INT64"), - UPB_TABVALUE_PTR_INIT("TYPE_UINT64"), - UPB_TABVALUE_PTR_INIT("TYPE_INT32"), - UPB_TABVALUE_PTR_INIT("TYPE_FIXED64"), - UPB_TABVALUE_PTR_INIT("TYPE_FIXED32"), - UPB_TABVALUE_PTR_INIT("TYPE_BOOL"), - UPB_TABVALUE_PTR_INIT("TYPE_STRING"), - UPB_TABVALUE_PTR_INIT("TYPE_GROUP"), - UPB_TABVALUE_PTR_INIT("TYPE_MESSAGE"), - UPB_TABVALUE_PTR_INIT("TYPE_BYTES"), - UPB_TABVALUE_PTR_INIT("TYPE_UINT32"), - UPB_TABVALUE_PTR_INIT("TYPE_ENUM"), - UPB_TABVALUE_PTR_INIT("TYPE_SFIXED32"), - UPB_TABVALUE_PTR_INIT("TYPE_SFIXED64"), - UPB_TABVALUE_PTR_INIT("TYPE_SINT32"), - UPB_TABVALUE_PTR_INIT("TYPE_SINT64"), - UPB_TABVALUE_PTR_INIT("STRING"), - UPB_TABVALUE_PTR_INIT("CORD"), - UPB_TABVALUE_PTR_INIT("STRING_PIECE"), - UPB_TABVALUE_PTR_INIT("JS_NORMAL"), - UPB_TABVALUE_PTR_INIT("JS_STRING"), - UPB_TABVALUE_PTR_INIT("JS_NUMBER"), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT("SPEED"), - UPB_TABVALUE_PTR_INIT("CODE_SIZE"), - UPB_TABVALUE_PTR_INIT("LITE_RUNTIME"), -}; + for (i = 0; i < upb_inttable_count(&c->groups); i++) { + upb_value v; + bool ok = upb_inttable_lookup(&c->groups, i, &v); + UPB_ASSERT(ok); + freegroup((void*)upb_value_getconstptr(v)); + } -#ifdef UPB_DEBUG_REFS -static upb_inttable reftables[268] = { - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), -}; -#endif + upb_inttable_uninit(&c->groups); + upb_arena_free(c->arena); + upb_gfree(c); +} -static const upb_msgdef *refm(const upb_msgdef *m, const void *owner) { - upb_msgdef_ref(m, owner); - return m; +bool upb_pbcodecache_allowjit(const upb_pbcodecache *c) { + return c->allow_jit; } -static const upb_enumdef *refe(const upb_enumdef *e, const void *owner) { - upb_enumdef_ref(e, owner); - return e; +void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) { + UPB_ASSERT(upb_inttable_count(&c->groups) == 0); + c->allow_jit = allow; } -/* Public API. */ -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner) { return refm(&msgs[0], owner); } -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner) { return refm(&msgs[1], owner); } -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner) { return refm(&msgs[2], owner); } -const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner) { return refm(&msgs[3], owner); } -const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner) { return refm(&msgs[4], owner); } -const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner) { return refm(&msgs[5], owner); } -const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner) { return refm(&msgs[6], owner); } -const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner) { return refm(&msgs[7], owner); } -const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner) { return refm(&msgs[8], owner); } -const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner) { return refm(&msgs[9], owner); } -const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner) { return refm(&msgs[10], owner); } -const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner) { return refm(&msgs[11], owner); } -const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner) { return refm(&msgs[12], owner); } -const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner) { return refm(&msgs[13], owner); } -const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner) { return refm(&msgs[14], owner); } -const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner) { return refm(&msgs[15], owner); } -const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner) { return refm(&msgs[16], owner); } -const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner) { return refm(&msgs[17], owner); } -const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner) { return refm(&msgs[18], owner); } -const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner) { return refm(&msgs[19], owner); } -const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner) { return refm(&msgs[20], owner); } -const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner) { return refm(&msgs[21], owner); } - -const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner) { return refe(&enums[0], owner); } -const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner) { return refe(&enums[1], owner); } -const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner) { return refe(&enums[2], owner); } -const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner) { return refe(&enums[3], owner); } -const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner) { return refe(&enums[4], owner); } -/* -** XXX: The routines in this file that consume a string do not currently -** support having the string span buffers. In the future, as upb_sink and -** its buffering/sharing functionality evolve there should be an easy and -** idiomatic way of correctly handling this case. For now, we accept this -** limitation since we currently only parse descriptors from single strings. -*/ +void upb_pbdecodermethodopts_setlazy(upb_pbcodecache *c, bool lazy) { + UPB_ASSERT(upb_inttable_count(&c->groups) == 0); + c->lazy = lazy; +} +const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, + const upb_msgdef *md) { + upb_value v; + bool ok; + const upb_handlers *h; + const mgroup *g; -#include -#include -#include + /* Right now we build a new DecoderMethod every time. + * TODO(haberman): properly cache methods by their true key. */ + h = upb_handlercache_get(c->dest, md); + g = mgroup_new(h, c->allow_jit, c->lazy); + upb_inttable_push(&c->groups, upb_value_constptr(g)); -/* Compares a NULL-terminated string with a non-NULL-terminated string. */ -static bool upb_streq(const char *str, const char *buf, size_t n) { - return strlen(str) == n && memcmp(str, buf, n) == 0; + ok = upb_inttable_lookupptr(&g->methods, h, &v); + UPB_ASSERT(ok); + return upb_value_getptr(v); } +/* +** upb::Decoder (Bytecode Decoder VM) +** +** Bytecode must previously have been generated using the bytecode compiler in +** compile_decoder.c. This decoder then walks through the bytecode op-by-op to +** parse the input. +** +** Decoding is fully resumable; we just keep a pointer to the current bytecode +** instruction and resume from there. A fair amount of the logic here is to +** handle the fact that values can span buffer seams and we have to be able to +** be capable of suspending/resuming from any byte in the stream. This +** sometimes requires keeping a few trailing bytes from the last buffer around +** in the "residual" buffer. +*/ -/* We keep a stack of all the messages scopes we are currently in, as well as - * the top-level file scope. This is necessary to correctly qualify the - * definitions that are contained inside. "name" tracks the name of the - * message or package (a bare name -- not qualified by any enclosing scopes). */ -typedef struct { - char *name; - /* Index of the first def that is under this scope. For msgdefs, the - * msgdef itself is at start-1. */ - int start; - uint32_t oneof_start; - uint32_t oneof_index; -} upb_descreader_frame; - -/* The maximum number of nested declarations that are allowed, ie. - * message Foo { - * message Bar { - * message Baz { - * } - * } - * } - * - * This is a resource limit that affects how big our runtime stack can grow. - * TODO: make this a runtime-settable property of the Reader instance. */ -#define UPB_MAX_MESSAGE_NESTING 64 +#include +#include -struct upb_descreader { - upb_sink sink; - upb_inttable files; - upb_strtable files_by_name; - upb_filedef *file; /* The last file in files. */ - upb_descreader_frame stack[UPB_MAX_MESSAGE_NESTING]; - int stack_len; - upb_inttable oneofs; +#ifdef UPB_DUMP_BYTECODE +#include +#endif - uint32_t number; - char *name; - bool saw_number; - bool saw_name; +#define CHECK_SUSPEND(x) if (!(x)) return upb_pbdecoder_suspend(d); - char *default_string; +/* Error messages that are shared between the bytecode and JIT decoders. */ +const char *kPbDecoderStackOverflow = "Nesting too deep."; +const char *kPbDecoderSubmessageTooLong = + "Submessage end extends past enclosing submessage."; - upb_fielddef *f; -}; +/* Error messages shared within this file. */ +static const char *kUnterminatedVarint = "Unterminated varint."; -static char *upb_gstrndup(const char *buf, size_t n) { - char *ret = upb_gmalloc(n + 1); - if (!ret) return NULL; - memcpy(ret, buf, n); - ret[n] = '\0'; - return ret; -} +/* upb_pbdecoder **************************************************************/ -/* Returns a newly allocated string that joins input strings together, for - * example: - * join("Foo.Bar", "Baz") -> "Foo.Bar.Baz" - * join("", "Baz") -> "Baz" - * Caller owns a ref on the returned string. */ -static char *upb_join(const char *base, const char *name) { - if (!base || strlen(base) == 0) { - return upb_gstrdup(name); - } else { - char *ret = upb_gmalloc(strlen(base) + strlen(name) + 2); - if (!ret) { - return NULL; - } - ret[0] = '\0'; - strcat(ret, base); - strcat(ret, "."); - strcat(ret, name); - return ret; - } -} +static opcode halt = OP_HALT; -/* Qualify the defname for all defs starting with offset "start" with "str". */ -static bool upb_descreader_qualify(upb_filedef *f, char *str, int32_t start) { - size_t i; - for (i = start; i < upb_filedef_defcount(f); i++) { - upb_def *def = upb_filedef_mutabledef(f, i); - char *name = upb_join(str, upb_def_fullname(def)); - if (!name) { - /* Need better logic here; at this point we've qualified some names but - * not others. */ +/* A dummy character we can point to when the user passes us a NULL buffer. + * We need this because in C (NULL + 0) and (NULL - NULL) are undefined + * behavior, which would invalidate functions like curbufleft(). */ +static const char dummy_char; + +/* Whether an op consumes any of the input buffer. */ +static bool consumes_input(opcode op) { + switch (op) { + case OP_SETDISPATCH: + case OP_STARTMSG: + case OP_ENDMSG: + case OP_STARTSEQ: + case OP_ENDSEQ: + case OP_STARTSUBMSG: + case OP_ENDSUBMSG: + case OP_STARTSTR: + case OP_ENDSTR: + case OP_PUSHTAGDELIM: + case OP_POP: + case OP_SETDELIM: + case OP_SETBIGGROUPNUM: + case OP_CHECKDELIM: + case OP_CALL: + case OP_RET: + case OP_BRANCH: return false; - } - upb_def_setfullname(def, name, NULL); - upb_gfree(name); + default: + return true; } - return true; } - -/* upb_descreader ************************************************************/ - -static upb_msgdef *upb_descreader_top(upb_descreader *r) { - int index; - UPB_ASSERT(r->stack_len > 1); - index = r->stack[r->stack_len-1].start - 1; - UPB_ASSERT(index >= 0); - return upb_downcast_msgdef_mutable(upb_filedef_mutabledef(r->file, index)); +static size_t stacksize(upb_pbdecoder *d, size_t entries) { + UPB_UNUSED(d); + return entries * sizeof(upb_pbdecoder_frame); } -static upb_def *upb_descreader_last(upb_descreader *r) { - return upb_filedef_mutabledef(r->file, upb_filedef_defcount(r->file) - 1); -} +static size_t callstacksize(upb_pbdecoder *d, size_t entries) { + UPB_UNUSED(d); + +#ifdef UPB_USE_JIT_X64 + if (d->method_->is_native_) { + /* Each native stack frame needs two pointers, plus we need a few frames for + * the enter/exit trampolines. */ + size_t ret = entries * sizeof(void*) * 2; + ret += sizeof(void*) * 10; + return ret; + } +#endif -/* Start/end handlers for FileDescriptorProto and DescriptorProto (the two - * entities that have names and can contain sub-definitions. */ -void upb_descreader_startcontainer(upb_descreader *r) { - upb_descreader_frame *f = &r->stack[r->stack_len++]; - f->start = upb_filedef_defcount(r->file); - f->oneof_start = upb_inttable_count(&r->oneofs); - f->oneof_index = 0; - f->name = NULL; + return entries * sizeof(uint32_t*); } -bool upb_descreader_endcontainer(upb_descreader *r) { - upb_descreader_frame *f = &r->stack[r->stack_len - 1]; - while (upb_inttable_count(&r->oneofs) > f->oneof_start) { - upb_oneofdef *o = upb_value_getptr(upb_inttable_pop(&r->oneofs)); - bool ok = upb_msgdef_addoneof(upb_descreader_top(r), o, &r->oneofs, NULL); - UPB_ASSERT(ok); - } +static bool in_residual_buf(const upb_pbdecoder *d, const char *p); - if (!upb_descreader_qualify(r->file, f->name, f->start)) { - return false; - } - upb_gfree(f->name); - f->name = NULL; +/* It's unfortunate that we have to micro-manage the compiler with + * UPB_FORCEINLINE and UPB_NOINLINE, especially since this tuning is necessarily + * specific to one hardware configuration. But empirically on a Core i7, + * performance increases 30-50% with these annotations. Every instance where + * these appear, gcc 4.2.1 made the wrong decision and degraded performance in + * benchmarks. */ - r->stack_len--; - return true; +static void seterr(upb_pbdecoder *d, const char *msg) { + upb_status_seterrmsg(d->status, msg); } -void upb_descreader_setscopename(upb_descreader *r, char *str) { - upb_descreader_frame *f = &r->stack[r->stack_len-1]; - upb_gfree(f->name); - f->name = str; +void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg) { + seterr(d, msg); } -static upb_oneofdef *upb_descreader_getoneof(upb_descreader *r, - uint32_t index) { - bool found; - upb_value val; - upb_descreader_frame *f = &r->stack[r->stack_len-1]; - /* DescriptorProto messages can be nested, so we will see the nested messages - * between when we see the FieldDescriptorProto and the OneofDescriptorProto. - * We need to preserve the oneofs in between these two things. */ - index += f->oneof_start; +/* Buffering ******************************************************************/ - while (upb_inttable_count(&r->oneofs) <= index) { - upb_inttable_push(&r->oneofs, upb_value_ptr(upb_oneofdef_new(&r->oneofs))); - } +/* We operate on one buffer at a time, which is either the user's buffer passed + * to our "decode" callback or some residual bytes from the previous buffer. */ - found = upb_inttable_lookup(&r->oneofs, index, &val); - UPB_ASSERT(found); - return upb_value_getptr(val); +/* How many bytes can be safely read from d->ptr without reading past end-of-buf + * or past the current delimited end. */ +static size_t curbufleft(const upb_pbdecoder *d) { + UPB_ASSERT(d->data_end >= d->ptr); + return d->data_end - d->ptr; } -/** Handlers for google.protobuf.FileDescriptorSet. ***************************/ - -static void *fileset_startfile(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - r->file = upb_filedef_new(&r->files); - upb_inttable_push(&r->files, upb_value_ptr(r->file)); - return r; +/* How many bytes are available before end-of-buffer. */ +static size_t bufleft(const upb_pbdecoder *d) { + return d->end - d->ptr; } -/** Handlers for google.protobuf.FileDescriptorProto. *************************/ +/* Overall stream offset of d->ptr. */ +uint64_t offset(const upb_pbdecoder *d) { + return d->bufstart_ofs + (d->ptr - d->buf); +} -static bool file_start(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - upb_descreader_startcontainer(r); - return true; +/* How many bytes are available before the end of this delimited region. */ +size_t delim_remaining(const upb_pbdecoder *d) { + return d->top->end_ofs - offset(d); } -static bool file_end(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - UPB_UNUSED(status); - return upb_descreader_endcontainer(r); +/* Advances d->ptr. */ +static void advance(upb_pbdecoder *d, size_t len) { + UPB_ASSERT(curbufleft(d) >= len); + d->ptr += len; } -static size_t file_onname(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *name; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); +static bool in_buf(const char *p, const char *buf, const char *end) { + return p >= buf && p <= end; +} - name = upb_gstrndup(buf, n); - upb_strtable_insert(&r->files_by_name, name, upb_value_ptr(r->file)); - /* XXX: see comment at the top of the file. */ - ok = upb_filedef_setname(r->file, name, NULL); - upb_gfree(name); - UPB_ASSERT(ok); - return n; +static bool in_residual_buf(const upb_pbdecoder *d, const char *p) { + return in_buf(p, d->residual, d->residual_end); } -static size_t file_onpackage(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *package; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - package = upb_gstrndup(buf, n); - /* XXX: see comment at the top of the file. */ - upb_descreader_setscopename(r, package); - ok = upb_filedef_setpackage(r->file, package, NULL); - UPB_ASSERT(ok); - return n; -} - -static void *file_startphpnamespace(void *closure, const void *hd, - size_t size_hint) { - upb_descreader *r = closure; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(size_hint); - - ok = upb_filedef_setphpnamespace(r->file, "", NULL); - UPB_ASSERT(ok); - return closure; -} - -static size_t file_onphpnamespace(void *closure, const void *hd, - const char *buf, size_t n, - const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *php_namespace; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - php_namespace = upb_gstrndup(buf, n); - ok = upb_filedef_setphpnamespace(r->file, php_namespace, NULL); - upb_gfree(php_namespace); - UPB_ASSERT(ok); - return n; -} - -static size_t file_onphpprefix(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *prefix; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - prefix = upb_gstrndup(buf, n); - ok = upb_filedef_setphpprefix(r->file, prefix, NULL); - upb_gfree(prefix); - UPB_ASSERT(ok); - return n; -} - -static size_t file_onsyntax(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - /* XXX: see comment at the top of the file. */ - if (upb_streq("proto2", buf, n)) { - ok = upb_filedef_setsyntax(r->file, UPB_SYNTAX_PROTO2, NULL); - } else if (upb_streq("proto3", buf, n)) { - ok = upb_filedef_setsyntax(r->file, UPB_SYNTAX_PROTO3, NULL); +/* Calculates the delim_end value, which is affected by both the current buffer + * and the parsing stack, so must be called whenever either is updated. */ +static void set_delim_end(upb_pbdecoder *d) { + size_t delim_ofs = d->top->end_ofs - d->bufstart_ofs; + if (delim_ofs <= (size_t)(d->end - d->buf)) { + d->delim_end = d->buf + delim_ofs; + d->data_end = d->delim_end; } else { - ok = false; + d->data_end = d->end; + d->delim_end = NULL; } - - UPB_ASSERT(ok); - return n; } -static void *file_startmsg(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_msgdef *m = upb_msgdef_new(&m); - bool ok = upb_filedef_addmsg(r->file, m, &m, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; +static void switchtobuf(upb_pbdecoder *d, const char *buf, const char *end) { + d->ptr = buf; + d->buf = buf; + d->end = end; + set_delim_end(d); } -static void *file_startenum(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_enumdef *e = upb_enumdef_new(&e); - bool ok = upb_filedef_addenum(r->file, e, &e, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; +static void advancetobuf(upb_pbdecoder *d, const char *buf, size_t len) { + UPB_ASSERT(curbufleft(d) == 0); + d->bufstart_ofs += (d->end - d->buf); + switchtobuf(d, buf, buf + len); } -static void *file_startext(void *closure, const void *hd) { - upb_descreader *r = closure; - bool ok; - r->f = upb_fielddef_new(r); - ok = upb_filedef_addext(r->file, r->f, r, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; +static void checkpoint(upb_pbdecoder *d) { + /* The assertion here is in the interests of efficiency, not correctness. + * We are trying to ensure that we don't checkpoint() more often than + * necessary. */ + UPB_ASSERT(d->checkpoint != d->ptr); + d->checkpoint = d->ptr; } -static size_t file_ondep(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - upb_value val; - if (upb_strtable_lookup2(&r->files_by_name, buf, n, &val)) { - upb_filedef_adddep(r->file, upb_value_getptr(val)); +/* Skips "bytes" bytes in the stream, which may be more than available. If we + * skip more bytes than are available, we return a long read count to the caller + * indicating how many bytes can be skipped over before passing actual data + * again. Skipped bytes can pass a NULL buffer and the decoder guarantees they + * won't actually be read. + */ +static int32_t skip(upb_pbdecoder *d, size_t bytes) { + UPB_ASSERT(!in_residual_buf(d, d->ptr) || d->size_param == 0); + UPB_ASSERT(d->skip == 0); + if (bytes > delim_remaining(d)) { + seterr(d, "Skipped value extended beyond enclosing submessage."); + return upb_pbdecoder_suspend(d); + } else if (bufleft(d) >= bytes) { + /* Skipped data is all in current buffer, and more is still available. */ + advance(d, bytes); + d->skip = 0; + return DECODE_OK; + } else { + /* Skipped data extends beyond currently available buffers. */ + d->pc = d->last; + d->skip = bytes - curbufleft(d); + d->bufstart_ofs += (d->end - d->buf); + d->residual_end = d->residual; + switchtobuf(d, d->residual, d->residual_end); + return d->size_param + d->skip; } - UPB_UNUSED(hd); - UPB_UNUSED(handle); - return n; } -/** Handlers for google.protobuf.EnumValueDescriptorProto. *********************/ -static bool enumval_startmsg(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - r->saw_number = false; - r->saw_name = false; - return true; -} +/* Resumes the decoder from an initial state or from a previous suspend. */ +int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf, + size_t size, const upb_bufhandle *handle) { + UPB_UNUSED(p); /* Useless; just for the benefit of the JIT. */ -static size_t enumval_onname(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - /* XXX: see comment at the top of the file. */ - upb_gfree(r->name); - r->name = upb_gstrndup(buf, n); - r->saw_name = true; - return n; -} + /* d->skip and d->residual_end could probably elegantly be represented + * as a single variable, to more easily represent this invariant. */ + UPB_ASSERT(!(d->skip && d->residual_end > d->residual)); -static bool enumval_onnumber(void *closure, const void *hd, int32_t val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - r->number = val; - r->saw_number = true; - return true; -} + /* We need to remember the original size_param, so that the value we return + * is relative to it, even if we do some skipping first. */ + d->size_param = size; + d->handle = handle; -static bool enumval_endmsg(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - upb_enumdef *e; - UPB_UNUSED(hd); + /* Have to handle this case specially (ie. not with skip()) because the user + * is allowed to pass a NULL buffer here, which won't allow us to safely + * calculate a d->end or use our normal functions like curbufleft(). */ + if (d->skip && d->skip >= size) { + d->skip -= size; + d->bufstart_ofs += size; + buf = &dummy_char; + size = 0; - if(!r->saw_number || !r->saw_name) { - upb_status_seterrmsg(status, "Enum value missing name or number."); - return false; + /* We can't just return now, because we might need to execute some ops + * like CHECKDELIM, which could call some callbacks and pop the stack. */ } - e = upb_downcast_enumdef_mutable(upb_descreader_last(r)); - upb_enumdef_addval(e, r->name, r->number, status); - upb_gfree(r->name); - r->name = NULL; - return true; -} - -/** Handlers for google.protobuf.EnumDescriptorProto. *************************/ -static bool enum_endmsg(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - upb_enumdef *e; - UPB_UNUSED(hd); + /* We need to pretend that this was the actual buffer param, since some of the + * calculations assume that d->ptr/d->buf is relative to this. */ + d->buf_param = buf; - e = upb_downcast_enumdef_mutable(upb_descreader_last(r)); - if (upb_def_fullname(upb_descreader_last(r)) == NULL) { - upb_status_seterrmsg(status, "Enum had no name."); - return false; - } - if (upb_enumdef_numvals(e) == 0) { - upb_status_seterrmsg(status, "Enum had no values."); - return false; + if (!buf) { + /* NULL buf is ok if its entire span is covered by the "skip" above, but + * by this point we know that "skip" doesn't cover the buffer. */ + seterr(d, "Passed NULL buffer over non-skippable region."); + return upb_pbdecoder_suspend(d); } - return true; -} - -static size_t enum_onname(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *fullname = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - /* XXX: see comment at the top of the file. */ - upb_def_setfullname(upb_descreader_last(r), fullname, NULL); - upb_gfree(fullname); - return n; -} - -/** Handlers for google.protobuf.FieldDescriptorProto *************************/ - -static bool field_startmsg(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - UPB_ASSERT(r->f); - upb_gfree(r->default_string); - r->default_string = NULL; - - /* fielddefs default to packed, but descriptors default to non-packed. */ - upb_fielddef_setpacked(r->f, false); - return true; -} -/* Converts the default value in string "str" into "d". Passes a ref on str. - * Returns true on success. */ -static bool parse_default(char *str, upb_fielddef *f) { - bool success = true; - char *end; - switch (upb_fielddef_type(f)) { - case UPB_TYPE_INT32: { - long val = strtol(str, &end, 0); - if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultint32(f, val); - break; - } - case UPB_TYPE_INT64: { - /* XXX: Need to write our own strtoll, since it's not available in c89. */ - long long val = strtol(str, &end, 0); - if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultint64(f, val); - break; - } - case UPB_TYPE_UINT32: { - unsigned long val = strtoul(str, &end, 0); - if (val > UINT32_MAX || errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultuint32(f, val); - break; - } - case UPB_TYPE_UINT64: { - /* XXX: Need to write our own strtoull, since it's not available in c89. */ - unsigned long long val = strtoul(str, &end, 0); - if (val > UINT64_MAX || errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultuint64(f, val); - break; - } - case UPB_TYPE_DOUBLE: { - double val = strtod(str, &end); - if (errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultdouble(f, val); - break; - } - case UPB_TYPE_FLOAT: { - /* XXX: Need to write our own strtof, since it's not available in c89. */ - float val = strtod(str, &end); - if (errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultfloat(f, val); - break; - } - case UPB_TYPE_BOOL: { - if (strcmp(str, "false") == 0) - upb_fielddef_setdefaultbool(f, false); - else if (strcmp(str, "true") == 0) - upb_fielddef_setdefaultbool(f, true); - else - success = false; - break; - } - default: abort(); + if (d->residual_end > d->residual) { + /* We have residual bytes from the last buffer. */ + UPB_ASSERT(d->ptr == d->residual); + } else { + switchtobuf(d, buf, buf + size); } - return success; -} - -static bool field_endmsg(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - upb_fielddef *f = r->f; - UPB_UNUSED(hd); - /* TODO: verify that all required fields were present. */ - UPB_ASSERT(upb_fielddef_number(f) != 0); - UPB_ASSERT(upb_fielddef_name(f) != NULL); - UPB_ASSERT((upb_fielddef_subdefname(f) != NULL) == upb_fielddef_hassubdef(f)); + d->checkpoint = d->ptr; - if (r->default_string) { - if (upb_fielddef_issubmsg(f)) { - upb_status_seterrmsg(status, "Submessages cannot have defaults."); - return false; - } - if (upb_fielddef_isstring(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM) { - upb_fielddef_setdefaultcstr(f, r->default_string, NULL); - } else { - if (r->default_string && !parse_default(r->default_string, f)) { - /* We don't worry too much about giving a great error message since the - * compiler should have ensured this was correct. */ - upb_status_seterrmsg(status, "Error converting default value."); - return false; - } - } + /* Handle skips that don't cover the whole buffer (as above). */ + if (d->skip) { + size_t skip_bytes = d->skip; + d->skip = 0; + CHECK_RETURN(skip(d, skip_bytes)); + checkpoint(d); } - return true; -} -static bool field_onlazy(void *closure, const void *hd, bool val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); + /* If we're inside an unknown group, continue to parse unknown values. */ + if (d->top->groupnum < 0) { + CHECK_RETURN(upb_pbdecoder_skipunknown(d, -1, 0)); + checkpoint(d); + } - upb_fielddef_setlazy(r->f, val); - return true; + return DECODE_OK; } -static bool field_onpacked(void *closure, const void *hd, bool val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); +/* Suspends the decoder at the last checkpoint, without saving any residual + * bytes. If there are any unconsumed bytes, returns a short byte count. */ +size_t upb_pbdecoder_suspend(upb_pbdecoder *d) { + d->pc = d->last; + if (d->checkpoint == d->residual) { + /* Checkpoint was in residual buf; no user bytes were consumed. */ + d->ptr = d->residual; + return 0; + } else { + size_t ret = d->size_param - (d->end - d->checkpoint); + UPB_ASSERT(!in_residual_buf(d, d->checkpoint)); + UPB_ASSERT(d->buf == d->buf_param || d->buf == &dummy_char); - upb_fielddef_setpacked(r->f, val); - return true; + d->bufstart_ofs += (d->checkpoint - d->buf); + d->residual_end = d->residual; + switchtobuf(d, d->residual, d->residual_end); + return ret; + } } -static bool field_ontype(void *closure, const void *hd, int32_t val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); +/* Suspends the decoder at the last checkpoint, and saves any unconsumed + * bytes in our residual buffer. This is necessary if we need more user + * bytes to form a complete value, which might not be contiguous in the + * user's buffers. Always consumes all user bytes. */ +static size_t suspend_save(upb_pbdecoder *d) { + /* We hit end-of-buffer before we could parse a full value. + * Save any unconsumed bytes (if any) to the residual buffer. */ + d->pc = d->last; - upb_fielddef_setdescriptortype(r->f, val); - return true; -} - -static bool field_onlabel(void *closure, const void *hd, int32_t val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - - upb_fielddef_setlabel(r->f, val); - return true; -} + if (d->checkpoint == d->residual) { + /* Checkpoint was in residual buf; append user byte(s) to residual buf. */ + UPB_ASSERT((d->residual_end - d->residual) + d->size_param <= + sizeof(d->residual)); + if (!in_residual_buf(d, d->ptr)) { + d->bufstart_ofs -= (d->residual_end - d->residual); + } + memcpy(d->residual_end, d->buf_param, d->size_param); + d->residual_end += d->size_param; + } else { + /* Checkpoint was in user buf; old residual bytes not needed. */ + size_t save; + UPB_ASSERT(!in_residual_buf(d, d->checkpoint)); -static bool field_onnumber(void *closure, const void *hd, int32_t val) { - upb_descreader *r = closure; - bool ok; - UPB_UNUSED(hd); + d->ptr = d->checkpoint; + save = curbufleft(d); + UPB_ASSERT(save <= sizeof(d->residual)); + memcpy(d->residual, d->ptr, save); + d->residual_end = d->residual + save; + d->bufstart_ofs = offset(d); + } - ok = upb_fielddef_setnumber(r->f, val, NULL); - UPB_ASSERT(ok); - return true; + switchtobuf(d, d->residual, d->residual_end); + return d->size_param; } -static size_t field_onname(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *name = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - /* XXX: see comment at the top of the file. */ - upb_fielddef_setname(r->f, name, NULL); - upb_gfree(name); - return n; +/* Copies the next "bytes" bytes into "buf" and advances the stream. + * Requires that this many bytes are available in the current buffer. */ +UPB_FORCEINLINE static void consumebytes(upb_pbdecoder *d, void *buf, + size_t bytes) { + UPB_ASSERT(bytes <= curbufleft(d)); + memcpy(buf, d->ptr, bytes); + advance(d, bytes); } -static size_t field_ontypename(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *name = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - /* XXX: see comment at the top of the file. */ - upb_fielddef_setsubdefname(r->f, name, NULL); - upb_gfree(name); - return n; +/* Slow path for getting the next "bytes" bytes, regardless of whether they are + * available in the current buffer or not. Returns a status code as described + * in decoder.int.h. */ +UPB_NOINLINE static int32_t getbytes_slow(upb_pbdecoder *d, void *buf, + size_t bytes) { + const size_t avail = curbufleft(d); + consumebytes(d, buf, avail); + bytes -= avail; + UPB_ASSERT(bytes > 0); + if (in_residual_buf(d, d->ptr)) { + advancetobuf(d, d->buf_param, d->size_param); + } + if (curbufleft(d) >= bytes) { + consumebytes(d, (char *)buf + avail, bytes); + return DECODE_OK; + } else if (d->data_end == d->delim_end) { + seterr(d, "Submessage ended in the middle of a value or group"); + return upb_pbdecoder_suspend(d); + } else { + return suspend_save(d); + } } -static size_t field_onextendee(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *name = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - /* XXX: see comment at the top of the file. */ - upb_fielddef_setcontainingtypename(r->f, name, NULL); - upb_gfree(name); - return n; +/* Gets the next "bytes" bytes, regardless of whether they are available in the + * current buffer or not. Returns a status code as described in decoder.int.h. + */ +UPB_FORCEINLINE static int32_t getbytes(upb_pbdecoder *d, void *buf, + size_t bytes) { + if (curbufleft(d) >= bytes) { + /* Buffer has enough data to satisfy. */ + consumebytes(d, buf, bytes); + return DECODE_OK; + } else { + return getbytes_slow(d, buf, bytes); + } } -static size_t field_ondefaultval(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - /* Have to convert from string to the correct type, but we might not know the - * type yet, so we save it as a string until the end of the field. - * XXX: see comment at the top of the file. */ - upb_gfree(r->default_string); - r->default_string = upb_gstrndup(buf, n); - return n; +UPB_NOINLINE static size_t peekbytes_slow(upb_pbdecoder *d, void *buf, + size_t bytes) { + size_t ret = curbufleft(d); + memcpy(buf, d->ptr, ret); + if (in_residual_buf(d, d->ptr)) { + size_t copy = UPB_MIN(bytes - ret, d->size_param); + memcpy((char *)buf + ret, d->buf_param, copy); + ret += copy; + } + return ret; } -static bool field_ononeofindex(void *closure, const void *hd, int32_t index) { - upb_descreader *r = closure; - upb_oneofdef *o = upb_descreader_getoneof(r, index); - bool ok = upb_oneofdef_addfield(o, r->f, &r->f, NULL); - UPB_UNUSED(hd); - - UPB_ASSERT(ok); - return true; +UPB_FORCEINLINE static size_t peekbytes(upb_pbdecoder *d, void *buf, + size_t bytes) { + if (curbufleft(d) >= bytes) { + memcpy(buf, d->ptr, bytes); + return bytes; + } else { + return peekbytes_slow(d, buf, bytes); + } } -/** Handlers for google.protobuf.OneofDescriptorProto. ************************/ -static size_t oneof_name(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - upb_descreader_frame *f = &r->stack[r->stack_len-1]; - upb_oneofdef *o = upb_descreader_getoneof(r, f->oneof_index++); - char *name_null_terminated = upb_gstrndup(buf, n); - bool ok = upb_oneofdef_setname(o, name_null_terminated, NULL); - UPB_UNUSED(hd); - UPB_UNUSED(handle); +/* Decoding of wire types *****************************************************/ - UPB_ASSERT(ok); - free(name_null_terminated); - return n; +/* Slow path for decoding a varint from the current buffer position. + * Returns a status code as described in decoder.int.h. */ +UPB_NOINLINE int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, + uint64_t *u64) { + uint8_t byte = 0x80; + int bitpos; + *u64 = 0; + for(bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) { + CHECK_RETURN(getbytes(d, &byte, 1)); + *u64 |= (uint64_t)(byte & 0x7F) << bitpos; + } + if(bitpos == 70 && (byte & 0x80)) { + seterr(d, kUnterminatedVarint); + return upb_pbdecoder_suspend(d); + } + return DECODE_OK; } -/** Handlers for google.protobuf.DescriptorProto ******************************/ - -static bool msg_start(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - - upb_descreader_startcontainer(r); - return true; +/* Decodes a varint from the current buffer position. + * Returns a status code as described in decoder.int.h. */ +UPB_FORCEINLINE static int32_t decode_varint(upb_pbdecoder *d, uint64_t *u64) { + if (curbufleft(d) > 0 && !(*d->ptr & 0x80)) { + *u64 = *d->ptr; + advance(d, 1); + return DECODE_OK; + } else if (curbufleft(d) >= 10) { + /* Fast case. */ + upb_decoderet r = upb_vdecode_fast(d->ptr); + if (r.p == NULL) { + seterr(d, kUnterminatedVarint); + return upb_pbdecoder_suspend(d); + } + advance(d, r.p - d->ptr); + *u64 = r.val; + return DECODE_OK; + } else { + /* Slow case -- varint spans buffer seam. */ + return upb_pbdecoder_decode_varint_slow(d, u64); + } } -static bool msg_end(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - upb_msgdef *m = upb_descreader_top(r); - UPB_UNUSED(hd); - - if(!upb_def_fullname(upb_msgdef_upcast_mutable(m))) { - upb_status_seterrmsg(status, "Encountered message with no name."); - return false; +/* Decodes a 32-bit varint from the current buffer position. + * Returns a status code as described in decoder.int.h. */ +UPB_FORCEINLINE static int32_t decode_v32(upb_pbdecoder *d, uint32_t *u32) { + uint64_t u64; + int32_t ret = decode_varint(d, &u64); + if (ret >= 0) return ret; + if (u64 > UINT32_MAX) { + seterr(d, "Unterminated 32-bit varint"); + /* TODO(haberman) guarantee that this function return is >= 0 somehow, + * so we know this path will always be treated as error by our caller. + * Right now the size_t -> int32_t can overflow and produce negative values. + */ + *u32 = 0; + return upb_pbdecoder_suspend(d); } - return upb_descreader_endcontainer(r); + *u32 = u64; + return DECODE_OK; } -static size_t msg_name(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - upb_msgdef *m = upb_descreader_top(r); - /* XXX: see comment at the top of the file. */ - char *name = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - upb_def_setfullname(upb_msgdef_upcast_mutable(m), name, NULL); - upb_descreader_setscopename(r, name); /* Passes ownership of name. */ - return n; +/* Decodes a fixed32 from the current buffer position. + * Returns a status code as described in decoder.int.h. + * TODO: proper byte swapping for big-endian machines. */ +UPB_FORCEINLINE static int32_t decode_fixed32(upb_pbdecoder *d, uint32_t *u32) { + return getbytes(d, u32, 4); } -static void *msg_startmsg(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_msgdef *m = upb_msgdef_new(&m); - bool ok = upb_filedef_addmsg(r->file, m, &m, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; +/* Decodes a fixed64 from the current buffer position. + * Returns a status code as described in decoder.int.h. + * TODO: proper byte swapping for big-endian machines. */ +UPB_FORCEINLINE static int32_t decode_fixed64(upb_pbdecoder *d, uint64_t *u64) { + return getbytes(d, u64, 8); } -static void *msg_startext(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_fielddef *f = upb_fielddef_new(&f); - bool ok = upb_filedef_addext(r->file, f, &f, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; +/* Non-static versions of the above functions. + * These are called by the JIT for fallback paths. */ +int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32) { + return decode_fixed32(d, u32); } -static void *msg_startfield(void *closure, const void *hd) { - upb_descreader *r = closure; - r->f = upb_fielddef_new(&r->f); - /* We can't add the new field to the message until its name/number are - * filled in. */ - UPB_UNUSED(hd); - return r; +int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64) { + return decode_fixed64(d, u64); } -static bool msg_endfield(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_msgdef *m = upb_descreader_top(r); - bool ok; - UPB_UNUSED(hd); +static double as_double(uint64_t n) { double d; memcpy(&d, &n, 8); return d; } +static float as_float(uint32_t n) { float f; memcpy(&f, &n, 4); return f; } - /* Oneof fields are added to the msgdef through their oneof, so don't need to - * be added here. */ - if (upb_fielddef_containingoneof(r->f) == NULL) { - ok = upb_msgdef_addfield(m, r->f, &r->f, NULL); - UPB_ASSERT(ok); +/* Pushes a frame onto the decoder stack. */ +static bool decoder_push(upb_pbdecoder *d, uint64_t end) { + upb_pbdecoder_frame *fr = d->top; + + if (end > fr->end_ofs) { + seterr(d, kPbDecoderSubmessageTooLong); + return false; + } else if (fr == d->limit) { + seterr(d, kPbDecoderStackOverflow); + return false; } - r->f = NULL; + + fr++; + fr->end_ofs = end; + fr->dispatch = NULL; + fr->groupnum = 0; + d->top = fr; return true; } -static bool msg_onmapentry(void *closure, const void *hd, bool mapentry) { - upb_descreader *r = closure; - upb_msgdef *m = upb_descreader_top(r); - UPB_UNUSED(hd); - - upb_msgdef_setmapentry(m, mapentry); - r->f = NULL; +static bool pushtagdelim(upb_pbdecoder *d, uint32_t arg) { + /* While we expect to see an "end" tag (either ENDGROUP or a non-sequence + * field number) prior to hitting any enclosing submessage end, pushing our + * existing delim end prevents us from continuing to parse values from a + * corrupt proto that doesn't give us an END tag in time. */ + if (!decoder_push(d, d->top->end_ofs)) + return false; + d->top->groupnum = arg; return true; } +/* Pops a frame from the decoder stack. */ +static void decoder_pop(upb_pbdecoder *d) { d->top--; } +UPB_NOINLINE int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, + uint64_t expected) { + uint64_t data = 0; + size_t bytes = upb_value_size(expected); + size_t read = peekbytes(d, &data, bytes); + if (read == bytes && data == expected) { + /* Advance past matched bytes. */ + int32_t ok = getbytes(d, &data, read); + UPB_ASSERT(ok < 0); + return DECODE_OK; + } else if (read < bytes && memcmp(&data, &expected, read) == 0) { + return suspend_save(d); + } else { + return DECODE_MISMATCH; + } +} -/** Code to register handlers *************************************************/ +int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum, + uint8_t wire_type) { + if (fieldnum >= 0) + goto have_tag; -#define F(msg, field) upbdefs_google_protobuf_ ## msg ## _f_ ## field(m) - -static void reghandlers(const void *closure, upb_handlers *h) { - const upb_msgdef *m = upb_handlers_msgdef(h); - UPB_UNUSED(closure); - - if (upbdefs_google_protobuf_FileDescriptorSet_is(m)) { - upb_handlers_setstartsubmsg(h, F(FileDescriptorSet, file), - &fileset_startfile, NULL); - } else if (upbdefs_google_protobuf_DescriptorProto_is(m)) { - upb_handlers_setstartmsg(h, &msg_start, NULL); - upb_handlers_setendmsg(h, &msg_end, NULL); - upb_handlers_setstring(h, F(DescriptorProto, name), &msg_name, NULL); - upb_handlers_setstartsubmsg(h, F(DescriptorProto, extension), &msg_startext, - NULL); - upb_handlers_setstartsubmsg(h, F(DescriptorProto, nested_type), - &msg_startmsg, NULL); - upb_handlers_setstartsubmsg(h, F(DescriptorProto, field), - &msg_startfield, NULL); - upb_handlers_setendsubmsg(h, F(DescriptorProto, field), - &msg_endfield, NULL); - upb_handlers_setstartsubmsg(h, F(DescriptorProto, enum_type), - &file_startenum, NULL); - } else if (upbdefs_google_protobuf_FileDescriptorProto_is(m)) { - upb_handlers_setstartmsg(h, &file_start, NULL); - upb_handlers_setendmsg(h, &file_end, NULL); - upb_handlers_setstring(h, F(FileDescriptorProto, name), &file_onname, - NULL); - upb_handlers_setstring(h, F(FileDescriptorProto, package), &file_onpackage, - NULL); - upb_handlers_setstring(h, F(FileDescriptorProto, syntax), &file_onsyntax, - NULL); - upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, message_type), - &file_startmsg, NULL); - upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, enum_type), - &file_startenum, NULL); - upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, extension), - &file_startext, NULL); - upb_handlers_setstring(h, F(FileDescriptorProto, dependency), - &file_ondep, NULL); - } else if (upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)) { - upb_handlers_setstartmsg(h, &enumval_startmsg, NULL); - upb_handlers_setendmsg(h, &enumval_endmsg, NULL); - upb_handlers_setstring(h, F(EnumValueDescriptorProto, name), &enumval_onname, NULL); - upb_handlers_setint32(h, F(EnumValueDescriptorProto, number), &enumval_onnumber, - NULL); - } else if (upbdefs_google_protobuf_EnumDescriptorProto_is(m)) { - upb_handlers_setendmsg(h, &enum_endmsg, NULL); - upb_handlers_setstring(h, F(EnumDescriptorProto, name), &enum_onname, NULL); - } else if (upbdefs_google_protobuf_FieldDescriptorProto_is(m)) { - upb_handlers_setstartmsg(h, &field_startmsg, NULL); - upb_handlers_setendmsg(h, &field_endmsg, NULL); - upb_handlers_setint32(h, F(FieldDescriptorProto, type), &field_ontype, - NULL); - upb_handlers_setint32(h, F(FieldDescriptorProto, label), &field_onlabel, - NULL); - upb_handlers_setint32(h, F(FieldDescriptorProto, number), &field_onnumber, - NULL); - upb_handlers_setstring(h, F(FieldDescriptorProto, name), &field_onname, - NULL); - upb_handlers_setstring(h, F(FieldDescriptorProto, type_name), - &field_ontypename, NULL); - upb_handlers_setstring(h, F(FieldDescriptorProto, extendee), - &field_onextendee, NULL); - upb_handlers_setstring(h, F(FieldDescriptorProto, default_value), - &field_ondefaultval, NULL); - upb_handlers_setint32(h, F(FieldDescriptorProto, oneof_index), - &field_ononeofindex, NULL); - } else if (upbdefs_google_protobuf_OneofDescriptorProto_is(m)) { - upb_handlers_setstring(h, F(OneofDescriptorProto, name), &oneof_name, NULL); - } else if (upbdefs_google_protobuf_FieldOptions_is(m)) { - upb_handlers_setbool(h, F(FieldOptions, lazy), &field_onlazy, NULL); - upb_handlers_setbool(h, F(FieldOptions, packed), &field_onpacked, NULL); - } else if (upbdefs_google_protobuf_MessageOptions_is(m)) { - upb_handlers_setbool(h, F(MessageOptions, map_entry), &msg_onmapentry, NULL); - } else if (upbdefs_google_protobuf_FileOptions_is(m)) { - upb_handlers_setstring(h, F(FileOptions, php_class_prefix), - &file_onphpprefix, NULL); - upb_handlers_setstartstr(h, F(FileOptions, php_namespace), - &file_startphpnamespace, NULL); - upb_handlers_setstring(h, F(FileOptions, php_namespace), - &file_onphpnamespace, NULL); - } - - UPB_ASSERT(upb_ok(upb_handlers_status(h))); -} - -#undef F - -void descreader_cleanup(void *_r) { - upb_descreader *r = _r; - size_t i; - - for (i = 0; i < upb_descreader_filecount(r); i++) { - upb_filedef_unref(upb_descreader_file(r, i), &r->files); - } + while (true) { + uint32_t tag; + CHECK_RETURN(decode_v32(d, &tag)); + wire_type = tag & 0x7; + fieldnum = tag >> 3; - upb_gfree(r->name); - upb_inttable_uninit(&r->files); - upb_strtable_uninit(&r->files_by_name); - upb_inttable_uninit(&r->oneofs); - upb_gfree(r->default_string); - while (r->stack_len > 0) { - upb_descreader_frame *f = &r->stack[--r->stack_len]; - upb_gfree(f->name); - } -} +have_tag: + if (fieldnum == 0) { + seterr(d, "Saw invalid field number (0)"); + return upb_pbdecoder_suspend(d); + } + switch (wire_type) { + case UPB_WIRE_TYPE_32BIT: + CHECK_RETURN(skip(d, 4)); + break; + case UPB_WIRE_TYPE_64BIT: + CHECK_RETURN(skip(d, 8)); + break; + case UPB_WIRE_TYPE_VARINT: { + uint64_t u64; + CHECK_RETURN(decode_varint(d, &u64)); + break; + } + case UPB_WIRE_TYPE_DELIMITED: { + uint32_t len; + CHECK_RETURN(decode_v32(d, &len)); + CHECK_RETURN(skip(d, len)); + break; + } + case UPB_WIRE_TYPE_START_GROUP: + CHECK_SUSPEND(pushtagdelim(d, -fieldnum)); + break; + case UPB_WIRE_TYPE_END_GROUP: + if (fieldnum == -d->top->groupnum) { + decoder_pop(d); + } else if (fieldnum == d->top->groupnum) { + return DECODE_ENDGROUP; + } else { + seterr(d, "Unmatched ENDGROUP tag."); + return upb_pbdecoder_suspend(d); + } + break; + default: + seterr(d, "Invalid wire type"); + return upb_pbdecoder_suspend(d); + } -/* Public API ****************************************************************/ + if (d->top->groupnum >= 0) { + /* TODO: More code needed for handling unknown groups. */ + upb_sink_putunknown(d->top->sink, d->checkpoint, d->ptr - d->checkpoint); + return DECODE_OK; + } -upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h) { - upb_descreader *r = upb_env_malloc(e, sizeof(upb_descreader)); - if (!r || !upb_env_addcleanup(e, descreader_cleanup, r)) { - return NULL; + /* Unknown group -- continue looping over unknown fields. */ + checkpoint(d); } - - upb_inttable_init(&r->files, UPB_CTYPE_PTR); - upb_strtable_init(&r->files_by_name, UPB_CTYPE_PTR); - upb_inttable_init(&r->oneofs, UPB_CTYPE_PTR); - upb_sink_reset(upb_descreader_input(r), h, r); - r->stack_len = 0; - r->name = NULL; - r->default_string = NULL; - - return r; } -size_t upb_descreader_filecount(const upb_descreader *r) { - return upb_inttable_count(&r->files); -} - -upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i) { +static void goto_endmsg(upb_pbdecoder *d) { upb_value v; - if (upb_inttable_lookup(&r->files, i, &v)) { - return upb_value_getptr(v); - } else { - return NULL; - } -} - -upb_sink *upb_descreader_input(upb_descreader *r) { - return &r->sink; -} - -const upb_handlers *upb_descreader_newhandlers(const void *owner) { - const upb_msgdef *m = upbdefs_google_protobuf_FileDescriptorSet_get(&m); - const upb_handlers *h = upb_handlers_newfrozen(m, owner, reghandlers, NULL); - upb_msgdef_unref(m, &m); - return h; -} -/* -** protobuf decoder bytecode compiler -** -** Code to compile a upb::Handlers into bytecode for decoding a protobuf -** according to that specific schema and destination handlers. -** -** Compiling to bytecode is always the first step. If we are using the -** interpreted decoder we leave it as bytecode and interpret that. If we are -** using a JIT decoder we use a code generator to turn the bytecode into native -** code, LLVM IR, etc. -** -** Bytecode definition is in decoder.int.h. -*/ - -#include - -#ifdef UPB_DUMP_BYTECODE -#include -#endif - -#define MAXLABEL 5 -#define EMPTYLABEL -1 - -/* mgroup *********************************************************************/ - -static void freegroup(upb_refcounted *r) { - mgroup *g = (mgroup*)r; - upb_inttable_uninit(&g->methods); -#ifdef UPB_USE_JIT_X64 - upb_pbdecoder_freejit(g); -#endif - upb_gfree(g->bytecode); - upb_gfree(g); -} - -static void visitgroup(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const mgroup *g = (const mgroup*)r; - upb_inttable_iter i; - upb_inttable_begin(&i, &g->methods); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i)); - visit(r, upb_pbdecodermethod_upcast(method), closure); - } -} - -mgroup *newgroup(const void *owner) { - mgroup *g = upb_gmalloc(sizeof(*g)); - static const struct upb_refcounted_vtbl vtbl = {visitgroup, freegroup}; - upb_refcounted_init(mgroup_upcast_mutable(g), &vtbl, owner); - upb_inttable_init(&g->methods, UPB_CTYPE_PTR); - g->bytecode = NULL; - g->bytecode_end = NULL; - return g; + bool found = upb_inttable_lookup32(d->top->dispatch, DISPATCH_ENDMSG, &v); + UPB_ASSERT(found); + d->pc = d->top->base + upb_value_getuint64(v); } +/* Parses a tag and jumps to the corresponding bytecode instruction for this + * field. + * + * If the tag is unknown (or the wire type doesn't match), parses the field as + * unknown. If the tag is a valid ENDGROUP tag, jumps to the bytecode + * instruction for the end of message. */ +static int32_t dispatch(upb_pbdecoder *d) { + upb_inttable *dispatch = d->top->dispatch; + uint32_t tag; + uint8_t wire_type; + uint32_t fieldnum; + upb_value val; + int32_t retval; -/* upb_pbdecodermethod ********************************************************/ - -static void freemethod(upb_refcounted *r) { - upb_pbdecodermethod *method = (upb_pbdecodermethod*)r; + /* Decode tag. */ + CHECK_RETURN(decode_v32(d, &tag)); + wire_type = tag & 0x7; + fieldnum = tag >> 3; - if (method->dest_handlers_) { - upb_handlers_unref(method->dest_handlers_, method); + /* Lookup tag. Because of packed/non-packed compatibility, we have to + * check the wire type against two possibilities. */ + if (fieldnum != DISPATCH_ENDMSG && + upb_inttable_lookup32(dispatch, fieldnum, &val)) { + uint64_t v = upb_value_getuint64(val); + if (wire_type == (v & 0xff)) { + d->pc = d->top->base + (v >> 16); + return DECODE_OK; + } else if (wire_type == ((v >> 8) & 0xff)) { + bool found = + upb_inttable_lookup(dispatch, fieldnum + UPB_MAX_FIELDNUMBER, &val); + UPB_ASSERT(found); + d->pc = d->top->base + upb_value_getuint64(val); + return DECODE_OK; + } } - upb_inttable_uninit(&method->dispatch); - upb_gfree(method); -} - -static void visitmethod(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_pbdecodermethod *m = (const upb_pbdecodermethod*)r; - visit(r, m->group, closure); -} - -static upb_pbdecodermethod *newmethod(const upb_handlers *dest_handlers, - mgroup *group) { - static const struct upb_refcounted_vtbl vtbl = {visitmethod, freemethod}; - upb_pbdecodermethod *ret = upb_gmalloc(sizeof(*ret)); - upb_refcounted_init(upb_pbdecodermethod_upcast_mutable(ret), &vtbl, &ret); - upb_byteshandler_init(&ret->input_handler_); + /* We have some unknown fields (or ENDGROUP) to parse. The DISPATCH or TAG + * bytecode that triggered this is preceded by a CHECKDELIM bytecode which + * we need to back up to, so that when we're done skipping unknown data we + * can re-check the delimited end. */ + d->last--; /* Necessary if we get suspended */ + d->pc = d->last; + UPB_ASSERT(getop(*d->last) == OP_CHECKDELIM); - /* The method references the group and vice-versa, in a circular reference. */ - upb_ref2(ret, group); - upb_ref2(group, ret); - upb_inttable_insertptr(&group->methods, dest_handlers, upb_value_ptr(ret)); - upb_pbdecodermethod_unref(ret, &ret); + /* Unknown field or ENDGROUP. */ + retval = upb_pbdecoder_skipunknown(d, fieldnum, wire_type); - ret->group = mgroup_upcast_mutable(group); - ret->dest_handlers_ = dest_handlers; - ret->is_native_ = false; /* If we JIT, it will update this later. */ - upb_inttable_init(&ret->dispatch, UPB_CTYPE_UINT64); + CHECK_RETURN(retval); - if (ret->dest_handlers_) { - upb_handlers_ref(ret->dest_handlers_, ret); + if (retval == DECODE_ENDGROUP) { + goto_endmsg(d); + return DECODE_OK; } - return ret; -} -const upb_handlers *upb_pbdecodermethod_desthandlers( - const upb_pbdecodermethod *m) { - return m->dest_handlers_; + return DECODE_OK; } -const upb_byteshandler *upb_pbdecodermethod_inputhandler( - const upb_pbdecodermethod *m) { - return &m->input_handler_; +/* Callers know that the stack is more than one deep because the opcodes that + * call this only occur after PUSH operations. */ +upb_pbdecoder_frame *outer_frame(upb_pbdecoder *d) { + UPB_ASSERT(d->top != d->stack); + return d->top - 1; } -bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m) { - return m->is_native_; -} -const upb_pbdecodermethod *upb_pbdecodermethod_new( - const upb_pbdecodermethodopts *opts, const void *owner) { - const upb_pbdecodermethod *ret; - upb_pbcodecache cache; +/* The main decoding loop *****************************************************/ - upb_pbcodecache_init(&cache); - ret = upb_pbcodecache_getdecodermethod(&cache, opts); - upb_pbdecodermethod_ref(ret, owner); - upb_pbcodecache_uninit(&cache); - return ret; -} +/* The main decoder VM function. Uses traditional bytecode dispatch loop with a + * switch() statement. */ +size_t run_decoder_vm(upb_pbdecoder *d, const mgroup *group, + const upb_bufhandle* handle) { +#define VMCASE(op, code) \ + case op: { code; if (consumes_input(op)) checkpoint(d); break; } +#define PRIMITIVE_OP(type, wt, name, convfunc, ctype) \ + VMCASE(OP_PARSE_ ## type, { \ + ctype val; \ + CHECK_RETURN(decode_ ## wt(d, &val)); \ + upb_sink_put ## name(d->top->sink, arg, (convfunc)(val)); \ + }) -/* bytecode compiler **********************************************************/ + while(1) { + int32_t instruction; + opcode op; + uint32_t arg; + int32_t longofs; -/* Data used only at compilation time. */ -typedef struct { - mgroup *group; + d->last = d->pc; + instruction = *d->pc++; + op = getop(instruction); + arg = instruction >> 8; + longofs = arg; + UPB_ASSERT(d->ptr != d->residual_end); + UPB_UNUSED(group); +#ifdef UPB_DUMP_BYTECODE + fprintf(stderr, "s_ofs=%d buf_ofs=%d data_rem=%d buf_rem=%d delim_rem=%d " + "%x %s (%d)\n", + (int)offset(d), + (int)(d->ptr - d->buf), + (int)(d->data_end - d->ptr), + (int)(d->end - d->ptr), + (int)((d->top->end_ofs - d->bufstart_ofs) - (d->ptr - d->buf)), + (int)(d->pc - 1 - group->bytecode), + upb_pbdecoder_getopname(op), + arg); +#endif + switch (op) { + /* Technically, we are losing data if we see a 32-bit varint that is not + * properly sign-extended. We could detect this and error about the data + * loss, but proto2 does not do this, so we pass. */ + PRIMITIVE_OP(INT32, varint, int32, int32_t, uint64_t) + PRIMITIVE_OP(INT64, varint, int64, int64_t, uint64_t) + PRIMITIVE_OP(UINT32, varint, uint32, uint32_t, uint64_t) + PRIMITIVE_OP(UINT64, varint, uint64, uint64_t, uint64_t) + PRIMITIVE_OP(FIXED32, fixed32, uint32, uint32_t, uint32_t) + PRIMITIVE_OP(FIXED64, fixed64, uint64, uint64_t, uint64_t) + PRIMITIVE_OP(SFIXED32, fixed32, int32, int32_t, uint32_t) + PRIMITIVE_OP(SFIXED64, fixed64, int64, int64_t, uint64_t) + PRIMITIVE_OP(BOOL, varint, bool, bool, uint64_t) + PRIMITIVE_OP(DOUBLE, fixed64, double, as_double, uint64_t) + PRIMITIVE_OP(FLOAT, fixed32, float, as_float, uint32_t) + PRIMITIVE_OP(SINT32, varint, int32, upb_zzdec_32, uint64_t) + PRIMITIVE_OP(SINT64, varint, int64, upb_zzdec_64, uint64_t) - uint32_t *pc; - int fwd_labels[MAXLABEL]; - int back_labels[MAXLABEL]; - - /* For fields marked "lazy", parse them lazily or eagerly? */ - bool lazy; -} compiler; - -static compiler *newcompiler(mgroup *group, bool lazy) { - compiler *ret = upb_gmalloc(sizeof(*ret)); - int i; - - ret->group = group; - ret->lazy = lazy; - for (i = 0; i < MAXLABEL; i++) { - ret->fwd_labels[i] = EMPTYLABEL; - ret->back_labels[i] = EMPTYLABEL; - } - return ret; -} - -static void freecompiler(compiler *c) { - upb_gfree(c); -} - -const size_t ptr_words = sizeof(void*) / sizeof(uint32_t); - -/* How many words an instruction is. */ -static int instruction_len(uint32_t instr) { - switch (getop(instr)) { - case OP_SETDISPATCH: return 1 + ptr_words; - case OP_TAGN: return 3; - case OP_SETBIGGROUPNUM: return 2; - default: return 1; - } -} - -bool op_has_longofs(int32_t instruction) { - switch (getop(instruction)) { - case OP_CALL: - case OP_BRANCH: - case OP_CHECKDELIM: - return true; - /* The "tag" instructions only have 8 bytes available for the jump target, - * but that is ok because these opcodes only require short jumps. */ - case OP_TAG1: - case OP_TAG2: - case OP_TAGN: - return false; - default: - UPB_ASSERT(false); - return false; - } -} - -static int32_t getofs(uint32_t instruction) { - if (op_has_longofs(instruction)) { - return (int32_t)instruction >> 8; - } else { - return (int8_t)(instruction >> 8); - } -} - -static void setofs(uint32_t *instruction, int32_t ofs) { - if (op_has_longofs(*instruction)) { - *instruction = getop(*instruction) | ofs << 8; - } else { - *instruction = (*instruction & ~0xff00) | ((ofs & 0xff) << 8); - } - UPB_ASSERT(getofs(*instruction) == ofs); /* Would fail in cases of overflow. */ -} - -static uint32_t pcofs(compiler *c) { return c->pc - c->group->bytecode; } - -/* Defines a local label at the current PC location. All previous forward - * references are updated to point to this location. The location is noted - * for any future backward references. */ -static void label(compiler *c, unsigned int label) { - int val; - uint32_t *codep; - - UPB_ASSERT(label < MAXLABEL); - val = c->fwd_labels[label]; - codep = (val == EMPTYLABEL) ? NULL : c->group->bytecode + val; - while (codep) { - int ofs = getofs(*codep); - setofs(codep, c->pc - codep - instruction_len(*codep)); - codep = ofs ? codep + ofs : NULL; - } - c->fwd_labels[label] = EMPTYLABEL; - c->back_labels[label] = pcofs(c); -} - -/* Creates a reference to a numbered label; either a forward reference - * (positive arg) or backward reference (negative arg). For forward references - * the value returned now is actually a "next" pointer into a linked list of all - * instructions that use this label and will be patched later when the label is - * defined with label(). - * - * The returned value is the offset that should be written into the instruction. - */ -static int32_t labelref(compiler *c, int label) { - UPB_ASSERT(label < MAXLABEL); - if (label == LABEL_DISPATCH) { - /* No resolving required. */ - return 0; - } else if (label < 0) { - /* Backward local label. Relative to the next instruction. */ - uint32_t from = (c->pc + 1) - c->group->bytecode; - return c->back_labels[-label] - from; - } else { - /* Forward local label: prepend to (possibly-empty) linked list. */ - int *lptr = &c->fwd_labels[label]; - int32_t ret = (*lptr == EMPTYLABEL) ? 0 : *lptr - pcofs(c); - *lptr = pcofs(c); - return ret; - } -} - -static void put32(compiler *c, uint32_t v) { - mgroup *g = c->group; - if (c->pc == g->bytecode_end) { - int ofs = pcofs(c); - size_t oldsize = g->bytecode_end - g->bytecode; - size_t newsize = UPB_MAX(oldsize * 2, 64); - /* TODO(haberman): handle OOM. */ - g->bytecode = upb_grealloc(g->bytecode, oldsize * sizeof(uint32_t), - newsize * sizeof(uint32_t)); - g->bytecode_end = g->bytecode + newsize; - c->pc = g->bytecode + ofs; - } - *c->pc++ = v; -} - -static void putop(compiler *c, int op, ...) { - va_list ap; - va_start(ap, op); - - switch (op) { - case OP_SETDISPATCH: { - uintptr_t ptr = (uintptr_t)va_arg(ap, void*); - put32(c, OP_SETDISPATCH); - put32(c, ptr); - if (sizeof(uintptr_t) > sizeof(uint32_t)) - put32(c, (uint64_t)ptr >> 32); - break; - } - case OP_STARTMSG: - case OP_ENDMSG: - case OP_PUSHLENDELIM: - case OP_POP: - case OP_SETDELIM: - case OP_HALT: - case OP_RET: - case OP_DISPATCH: - put32(c, op); - break; - case OP_PARSE_DOUBLE: - case OP_PARSE_FLOAT: - case OP_PARSE_INT64: - case OP_PARSE_UINT64: - case OP_PARSE_INT32: - case OP_PARSE_FIXED64: - case OP_PARSE_FIXED32: - case OP_PARSE_BOOL: - case OP_PARSE_UINT32: - case OP_PARSE_SFIXED32: - case OP_PARSE_SFIXED64: - case OP_PARSE_SINT32: - case OP_PARSE_SINT64: - case OP_STARTSEQ: - case OP_ENDSEQ: - case OP_STARTSUBMSG: - case OP_ENDSUBMSG: - case OP_STARTSTR: - case OP_STRING: - case OP_ENDSTR: - case OP_PUSHTAGDELIM: - put32(c, op | va_arg(ap, upb_selector_t) << 8); - break; - case OP_SETBIGGROUPNUM: - put32(c, op); - put32(c, va_arg(ap, int)); - break; - case OP_CALL: { - const upb_pbdecodermethod *method = va_arg(ap, upb_pbdecodermethod *); - put32(c, op | (method->code_base.ofs - (pcofs(c) + 1)) << 8); - break; - } - case OP_CHECKDELIM: - case OP_BRANCH: { - uint32_t instruction = op; - int label = va_arg(ap, int); - setofs(&instruction, labelref(c, label)); - put32(c, instruction); - break; - } - case OP_TAG1: - case OP_TAG2: { - int label = va_arg(ap, int); - uint64_t tag = va_arg(ap, uint64_t); - uint32_t instruction = op | (tag << 16); - UPB_ASSERT(tag <= 0xffff); - setofs(&instruction, labelref(c, label)); - put32(c, instruction); - break; - } - case OP_TAGN: { - int label = va_arg(ap, int); - uint64_t tag = va_arg(ap, uint64_t); - uint32_t instruction = op | (upb_value_size(tag) << 16); - setofs(&instruction, labelref(c, label)); - put32(c, instruction); - put32(c, tag); - put32(c, tag >> 32); - break; - } - } - - va_end(ap); -} - -#if defined(UPB_USE_JIT_X64) || defined(UPB_DUMP_BYTECODE) - -const char *upb_pbdecoder_getopname(unsigned int op) { -#define QUOTE(x) #x -#define EXPAND_AND_QUOTE(x) QUOTE(x) -#define OPNAME(x) OP_##x -#define OP(x) case OPNAME(x): return EXPAND_AND_QUOTE(OPNAME(x)); -#define T(x) OP(PARSE_##x) - /* Keep in sync with list in decoder.int.h. */ - switch ((opcode)op) { - T(DOUBLE) T(FLOAT) T(INT64) T(UINT64) T(INT32) T(FIXED64) T(FIXED32) - T(BOOL) T(UINT32) T(SFIXED32) T(SFIXED64) T(SINT32) T(SINT64) - OP(STARTMSG) OP(ENDMSG) OP(STARTSEQ) OP(ENDSEQ) OP(STARTSUBMSG) - OP(ENDSUBMSG) OP(STARTSTR) OP(STRING) OP(ENDSTR) OP(CALL) OP(RET) - OP(PUSHLENDELIM) OP(PUSHTAGDELIM) OP(SETDELIM) OP(CHECKDELIM) - OP(BRANCH) OP(TAG1) OP(TAG2) OP(TAGN) OP(SETDISPATCH) OP(POP) - OP(SETBIGGROUPNUM) OP(DISPATCH) OP(HALT) - } - return ""; -#undef OP -#undef T -} - -#endif - -#ifdef UPB_DUMP_BYTECODE - -static void dumpbc(uint32_t *p, uint32_t *end, FILE *f) { - - uint32_t *begin = p; - - while (p < end) { - fprintf(f, "%p %8tx", p, p - begin); - uint32_t instr = *p++; - uint8_t op = getop(instr); - fprintf(f, " %s", upb_pbdecoder_getopname(op)); - switch ((opcode)op) { - case OP_SETDISPATCH: { - const upb_inttable *dispatch; - memcpy(&dispatch, p, sizeof(void*)); - p += ptr_words; - const upb_pbdecodermethod *method = - (void *)((char *)dispatch - - offsetof(upb_pbdecodermethod, dispatch)); - fprintf(f, " %s", upb_msgdef_fullname( - upb_handlers_msgdef(method->dest_handlers_))); - break; - } - case OP_DISPATCH: - case OP_STARTMSG: - case OP_ENDMSG: - case OP_PUSHLENDELIM: - case OP_POP: - case OP_SETDELIM: - case OP_HALT: - case OP_RET: - break; - case OP_PARSE_DOUBLE: - case OP_PARSE_FLOAT: - case OP_PARSE_INT64: - case OP_PARSE_UINT64: - case OP_PARSE_INT32: - case OP_PARSE_FIXED64: - case OP_PARSE_FIXED32: - case OP_PARSE_BOOL: - case OP_PARSE_UINT32: - case OP_PARSE_SFIXED32: - case OP_PARSE_SFIXED64: - case OP_PARSE_SINT32: - case OP_PARSE_SINT64: - case OP_STARTSEQ: - case OP_ENDSEQ: - case OP_STARTSUBMSG: - case OP_ENDSUBMSG: - case OP_STARTSTR: - case OP_STRING: - case OP_ENDSTR: - case OP_PUSHTAGDELIM: - fprintf(f, " %d", instr >> 8); - break; - case OP_SETBIGGROUPNUM: - fprintf(f, " %d", *p++); - break; - case OP_CHECKDELIM: - case OP_CALL: - case OP_BRANCH: - fprintf(f, " =>0x%tx", p + getofs(instr) - begin); - break; - case OP_TAG1: - case OP_TAG2: { - fprintf(f, " tag:0x%x", instr >> 16); - if (getofs(instr)) { - fprintf(f, " =>0x%tx", p + getofs(instr) - begin); - } - break; - } - case OP_TAGN: { - uint64_t tag = *p++; - tag |= (uint64_t)*p++ << 32; - fprintf(f, " tag:0x%llx", (long long)tag); - fprintf(f, " n:%d", instr >> 16); - if (getofs(instr)) { - fprintf(f, " =>0x%tx", p + getofs(instr) - begin); - } - break; - } - } - fputs("\n", f); - } -} - -#endif - -static uint64_t get_encoded_tag(const upb_fielddef *f, int wire_type) { - uint32_t tag = (upb_fielddef_number(f) << 3) | wire_type; - uint64_t encoded_tag = upb_vencode32(tag); - /* No tag should be greater than 5 bytes. */ - UPB_ASSERT(encoded_tag <= 0xffffffffff); - return encoded_tag; -} - -static void putchecktag(compiler *c, const upb_fielddef *f, - int wire_type, int dest) { - uint64_t tag = get_encoded_tag(f, wire_type); - switch (upb_value_size(tag)) { - case 1: - putop(c, OP_TAG1, dest, tag); - break; - case 2: - putop(c, OP_TAG2, dest, tag); - break; - default: - putop(c, OP_TAGN, dest, tag); - break; - } -} - -static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) { - upb_selector_t selector; - bool ok = upb_handlers_getselector(f, type, &selector); - UPB_ASSERT(ok); - return selector; -} - -/* Takes an existing, primary dispatch table entry and repacks it with a - * different alternate wire type. Called when we are inserting a secondary - * dispatch table entry for an alternate wire type. */ -static uint64_t repack(uint64_t dispatch, int new_wt2) { - uint64_t ofs; - uint8_t wt1; - uint8_t old_wt2; - upb_pbdecoder_unpackdispatch(dispatch, &ofs, &wt1, &old_wt2); - UPB_ASSERT(old_wt2 == NO_WIRE_TYPE); /* wt2 should not be set yet. */ - return upb_pbdecoder_packdispatch(ofs, wt1, new_wt2); -} - -/* Marks the current bytecode position as the dispatch target for this message, - * field, and wire type. */ -static void dispatchtarget(compiler *c, upb_pbdecodermethod *method, - const upb_fielddef *f, int wire_type) { - /* Offset is relative to msg base. */ - uint64_t ofs = pcofs(c) - method->code_base.ofs; - uint32_t fn = upb_fielddef_number(f); - upb_inttable *d = &method->dispatch; - upb_value v; - if (upb_inttable_remove(d, fn, &v)) { - /* TODO: prioritize based on packed setting in .proto file. */ - uint64_t repacked = repack(upb_value_getuint64(v), wire_type); - upb_inttable_insert(d, fn, upb_value_uint64(repacked)); - upb_inttable_insert(d, fn + UPB_MAX_FIELDNUMBER, upb_value_uint64(ofs)); - } else { - uint64_t val = upb_pbdecoder_packdispatch(ofs, wire_type, NO_WIRE_TYPE); - upb_inttable_insert(d, fn, upb_value_uint64(val)); - } -} - -static void putpush(compiler *c, const upb_fielddef *f) { - if (upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_MESSAGE) { - putop(c, OP_PUSHLENDELIM); - } else { - uint32_t fn = upb_fielddef_number(f); - if (fn >= 1 << 24) { - putop(c, OP_PUSHTAGDELIM, 0); - putop(c, OP_SETBIGGROUPNUM, fn); - } else { - putop(c, OP_PUSHTAGDELIM, fn); - } - } -} - -static upb_pbdecodermethod *find_submethod(const compiler *c, - const upb_pbdecodermethod *method, - const upb_fielddef *f) { - const upb_handlers *sub = - upb_handlers_getsubhandlers(method->dest_handlers_, f); - upb_value v; - return upb_inttable_lookupptr(&c->group->methods, sub, &v) - ? upb_value_getptr(v) - : NULL; -} - -static void putsel(compiler *c, opcode op, upb_selector_t sel, - const upb_handlers *h) { - if (upb_handlers_gethandler(h, sel)) { - putop(c, op, sel); - } -} - -/* Puts an opcode to call a callback, but only if a callback actually exists for - * this field and handler type. */ -static void maybeput(compiler *c, opcode op, const upb_handlers *h, - const upb_fielddef *f, upb_handlertype_t type) { - putsel(c, op, getsel(f, type), h); -} - -static bool haslazyhandlers(const upb_handlers *h, const upb_fielddef *f) { - if (!upb_fielddef_lazy(f)) - return false; - - return upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STARTSTR)) || - upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STRING)) || - upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_ENDSTR)); -} - - -/* bytecode compiler code generation ******************************************/ - -/* Symbolic names for our local labels. */ -#define LABEL_LOOPSTART 1 /* Top of a repeated field loop. */ -#define LABEL_LOOPBREAK 2 /* To jump out of a repeated loop */ -#define LABEL_FIELD 3 /* Jump backward to find the most recent field. */ -#define LABEL_ENDMSG 4 /* To reach the OP_ENDMSG instr for this msg. */ - -/* Generates bytecode to parse a single non-lazy message field. */ -static void generate_msgfield(compiler *c, const upb_fielddef *f, - upb_pbdecodermethod *method) { - const upb_handlers *h = upb_pbdecodermethod_desthandlers(method); - const upb_pbdecodermethod *sub_m = find_submethod(c, method, f); - int wire_type; - - if (!sub_m) { - /* Don't emit any code for this field at all; it will be parsed as an - * unknown field. - * - * TODO(haberman): we should change this to parse it as a string field - * instead. It will probably be faster, but more importantly, once we - * start vending unknown fields, a field shouldn't be treated as unknown - * just because it doesn't have subhandlers registered. */ - return; - } - - label(c, LABEL_FIELD); - - wire_type = - (upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_MESSAGE) - ? UPB_WIRE_TYPE_DELIMITED - : UPB_WIRE_TYPE_START_GROUP; - - if (upb_fielddef_isseq(f)) { - putop(c, OP_CHECKDELIM, LABEL_ENDMSG); - putchecktag(c, f, wire_type, LABEL_DISPATCH); - dispatchtarget(c, method, f, wire_type); - putop(c, OP_PUSHTAGDELIM, 0); - putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); - label(c, LABEL_LOOPSTART); - putpush(c, f); - putop(c, OP_STARTSUBMSG, getsel(f, UPB_HANDLER_STARTSUBMSG)); - putop(c, OP_CALL, sub_m); - putop(c, OP_POP); - maybeput(c, OP_ENDSUBMSG, h, f, UPB_HANDLER_ENDSUBMSG); - if (wire_type == UPB_WIRE_TYPE_DELIMITED) { - putop(c, OP_SETDELIM); - } - putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); - putchecktag(c, f, wire_type, LABEL_LOOPBREAK); - putop(c, OP_BRANCH, -LABEL_LOOPSTART); - label(c, LABEL_LOOPBREAK); - putop(c, OP_POP); - maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ); - } else { - putop(c, OP_CHECKDELIM, LABEL_ENDMSG); - putchecktag(c, f, wire_type, LABEL_DISPATCH); - dispatchtarget(c, method, f, wire_type); - putpush(c, f); - putop(c, OP_STARTSUBMSG, getsel(f, UPB_HANDLER_STARTSUBMSG)); - putop(c, OP_CALL, sub_m); - putop(c, OP_POP); - maybeput(c, OP_ENDSUBMSG, h, f, UPB_HANDLER_ENDSUBMSG); - if (wire_type == UPB_WIRE_TYPE_DELIMITED) { - putop(c, OP_SETDELIM); - } - } -} - -/* Generates bytecode to parse a single string or lazy submessage field. */ -static void generate_delimfield(compiler *c, const upb_fielddef *f, - upb_pbdecodermethod *method) { - const upb_handlers *h = upb_pbdecodermethod_desthandlers(method); - - label(c, LABEL_FIELD); - if (upb_fielddef_isseq(f)) { - putop(c, OP_CHECKDELIM, LABEL_ENDMSG); - putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH); - dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED); - putop(c, OP_PUSHTAGDELIM, 0); - putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); - label(c, LABEL_LOOPSTART); - putop(c, OP_PUSHLENDELIM); - putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR)); - /* Need to emit even if no handler to skip past the string. */ - putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING)); - putop(c, OP_POP); - maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR); - putop(c, OP_SETDELIM); - putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); - putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_LOOPBREAK); - putop(c, OP_BRANCH, -LABEL_LOOPSTART); - label(c, LABEL_LOOPBREAK); - putop(c, OP_POP); - maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ); - } else { - putop(c, OP_CHECKDELIM, LABEL_ENDMSG); - putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH); - dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED); - putop(c, OP_PUSHLENDELIM); - putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR)); - putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING)); - putop(c, OP_POP); - maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR); - putop(c, OP_SETDELIM); - } -} - -/* Generates bytecode to parse a single primitive field. */ -static void generate_primitivefield(compiler *c, const upb_fielddef *f, - upb_pbdecodermethod *method) { - const upb_handlers *h = upb_pbdecodermethod_desthandlers(method); - upb_descriptortype_t descriptor_type = upb_fielddef_descriptortype(f); - opcode parse_type; - upb_selector_t sel; - int wire_type; - - label(c, LABEL_FIELD); - - /* From a decoding perspective, ENUM is the same as INT32. */ - if (descriptor_type == UPB_DESCRIPTOR_TYPE_ENUM) - descriptor_type = UPB_DESCRIPTOR_TYPE_INT32; - - parse_type = (opcode)descriptor_type; - - /* TODO(haberman): generate packed or non-packed first depending on "packed" - * setting in the fielddef. This will favor (in speed) whichever was - * specified. */ - - UPB_ASSERT((int)parse_type >= 0 && parse_type <= OP_MAX); - sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); - wire_type = upb_pb_native_wire_types[upb_fielddef_descriptortype(f)]; - if (upb_fielddef_isseq(f)) { - putop(c, OP_CHECKDELIM, LABEL_ENDMSG); - putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH); - dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED); - putop(c, OP_PUSHLENDELIM); - putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); /* Packed */ - label(c, LABEL_LOOPSTART); - putop(c, parse_type, sel); - putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); - putop(c, OP_BRANCH, -LABEL_LOOPSTART); - dispatchtarget(c, method, f, wire_type); - putop(c, OP_PUSHTAGDELIM, 0); - putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); /* Non-packed */ - label(c, LABEL_LOOPSTART); - putop(c, parse_type, sel); - putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); - putchecktag(c, f, wire_type, LABEL_LOOPBREAK); - putop(c, OP_BRANCH, -LABEL_LOOPSTART); - label(c, LABEL_LOOPBREAK); - putop(c, OP_POP); /* Packed and non-packed join. */ - maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ); - putop(c, OP_SETDELIM); /* Could remove for non-packed by dup ENDSEQ. */ - } else { - putop(c, OP_CHECKDELIM, LABEL_ENDMSG); - putchecktag(c, f, wire_type, LABEL_DISPATCH); - dispatchtarget(c, method, f, wire_type); - putop(c, parse_type, sel); - } -} - -/* Adds bytecode for parsing the given message to the given decoderplan, - * while adding all dispatch targets to this message's dispatch table. */ -static void compile_method(compiler *c, upb_pbdecodermethod *method) { - const upb_handlers *h; - const upb_msgdef *md; - uint32_t* start_pc; - upb_msg_field_iter i; - upb_value val; - - UPB_ASSERT(method); - - /* Clear all entries in the dispatch table. */ - upb_inttable_uninit(&method->dispatch); - upb_inttable_init(&method->dispatch, UPB_CTYPE_UINT64); - - h = upb_pbdecodermethod_desthandlers(method); - md = upb_handlers_msgdef(h); - - method->code_base.ofs = pcofs(c); - putop(c, OP_SETDISPATCH, &method->dispatch); - putsel(c, OP_STARTMSG, UPB_STARTMSG_SELECTOR, h); - label(c, LABEL_FIELD); - start_pc = c->pc; - for(upb_msg_field_begin(&i, md); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - const upb_fielddef *f = upb_msg_iter_field(&i); - upb_fieldtype_t type = upb_fielddef_type(f); - - if (type == UPB_TYPE_MESSAGE && !(haslazyhandlers(h, f) && c->lazy)) { - generate_msgfield(c, f, method); - } else if (type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES || - type == UPB_TYPE_MESSAGE) { - generate_delimfield(c, f, method); - } else { - generate_primitivefield(c, f, method); - } - } - - /* If there were no fields, or if no handlers were defined, we need to - * generate a non-empty loop body so that we can at least dispatch for unknown - * fields and check for the end of the message. */ - if (c->pc == start_pc) { - /* Check for end-of-message. */ - putop(c, OP_CHECKDELIM, LABEL_ENDMSG); - /* Unconditionally dispatch. */ - putop(c, OP_DISPATCH, 0); - } - - /* For now we just loop back to the last field of the message (or if none, - * the DISPATCH opcode for the message). */ - putop(c, OP_BRANCH, -LABEL_FIELD); - - /* Insert both a label and a dispatch table entry for this end-of-msg. */ - label(c, LABEL_ENDMSG); - val = upb_value_uint64(pcofs(c) - method->code_base.ofs); - upb_inttable_insert(&method->dispatch, DISPATCH_ENDMSG, val); - - putsel(c, OP_ENDMSG, UPB_ENDMSG_SELECTOR, h); - putop(c, OP_RET); - - upb_inttable_compact(&method->dispatch); -} - -/* Populate "methods" with new upb_pbdecodermethod objects reachable from "h". - * Returns the method for these handlers. - * - * Generates a new method for every destination handlers reachable from "h". */ -static void find_methods(compiler *c, const upb_handlers *h) { - upb_value v; - upb_msg_field_iter i; - const upb_msgdef *md; - - if (upb_inttable_lookupptr(&c->group->methods, h, &v)) - return; - newmethod(h, c->group); - - /* Find submethods. */ - md = upb_handlers_msgdef(h); - for(upb_msg_field_begin(&i, md); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - const upb_fielddef *f = upb_msg_iter_field(&i); - const upb_handlers *sub_h; - if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE && - (sub_h = upb_handlers_getsubhandlers(h, f)) != NULL) { - /* We only generate a decoder method for submessages with handlers. - * Others will be parsed as unknown fields. */ - find_methods(c, sub_h); - } - } -} - -/* (Re-)compile bytecode for all messages in "msgs." - * Overwrites any existing bytecode in "c". */ -static void compile_methods(compiler *c) { - upb_inttable_iter i; - - /* Start over at the beginning of the bytecode. */ - c->pc = c->group->bytecode; - - upb_inttable_begin(&i, &c->group->methods); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i)); - compile_method(c, method); - } -} - -static void set_bytecode_handlers(mgroup *g) { - upb_inttable_iter i; - upb_inttable_begin(&i, &g->methods); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_pbdecodermethod *m = upb_value_getptr(upb_inttable_iter_value(&i)); - upb_byteshandler *h = &m->input_handler_; - - m->code_base.ptr = g->bytecode + m->code_base.ofs; - - upb_byteshandler_setstartstr(h, upb_pbdecoder_startbc, m->code_base.ptr); - upb_byteshandler_setstring(h, upb_pbdecoder_decode, g); - upb_byteshandler_setendstr(h, upb_pbdecoder_end, m); - } -} - - -/* JIT setup. *****************************************************************/ - -#ifdef UPB_USE_JIT_X64 - -static void sethandlers(mgroup *g, bool allowjit) { - g->jit_code = NULL; - if (allowjit) { - /* Compile byte-code into machine code, create handlers. */ - upb_pbdecoder_jit(g); - } else { - set_bytecode_handlers(g); - } -} - -#else /* UPB_USE_JIT_X64 */ - -static void sethandlers(mgroup *g, bool allowjit) { - /* No JIT compiled in; use bytecode handlers unconditionally. */ - UPB_UNUSED(allowjit); - set_bytecode_handlers(g); -} - -#endif /* UPB_USE_JIT_X64 */ - - -/* TODO(haberman): allow this to be constructed for an arbitrary set of dest - * handlers and other mgroups (but verify we have a transitive closure). */ -const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy, - const void *owner) { - mgroup *g; - compiler *c; - - UPB_UNUSED(allowjit); - UPB_ASSERT(upb_handlers_isfrozen(dest)); - - g = newgroup(owner); - c = newcompiler(g, lazy); - find_methods(c, dest); - - /* We compile in two passes: - * 1. all messages are assigned relative offsets from the beginning of the - * bytecode (saved in method->code_base). - * 2. forwards OP_CALL instructions can be correctly linked since message - * offsets have been previously assigned. - * - * Could avoid the second pass by linking OP_CALL instructions somehow. */ - compile_methods(c); - compile_methods(c); - g->bytecode_end = c->pc; - freecompiler(c); - -#ifdef UPB_DUMP_BYTECODE - { - FILE *f = fopen("/tmp/upb-bytecode", "w"); - UPB_ASSERT(f); - dumpbc(g->bytecode, g->bytecode_end, stderr); - dumpbc(g->bytecode, g->bytecode_end, f); - fclose(f); - - f = fopen("/tmp/upb-bytecode.bin", "wb"); - UPB_ASSERT(f); - fwrite(g->bytecode, 1, g->bytecode_end - g->bytecode, f); - fclose(f); - } -#endif - - sethandlers(g, allowjit); - return g; -} - - -/* upb_pbcodecache ************************************************************/ - -void upb_pbcodecache_init(upb_pbcodecache *c) { - upb_inttable_init(&c->groups, UPB_CTYPE_CONSTPTR); - c->allow_jit_ = true; -} - -void upb_pbcodecache_uninit(upb_pbcodecache *c) { - upb_inttable_iter i; - upb_inttable_begin(&i, &c->groups); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - const mgroup *group = upb_value_getconstptr(upb_inttable_iter_value(&i)); - mgroup_unref(group, c); - } - upb_inttable_uninit(&c->groups); -} - -bool upb_pbcodecache_allowjit(const upb_pbcodecache *c) { - return c->allow_jit_; -} - -bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) { - if (upb_inttable_count(&c->groups) > 0) - return false; - c->allow_jit_ = allow; - return true; -} - -const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod( - upb_pbcodecache *c, const upb_pbdecodermethodopts *opts) { - upb_value v; - bool ok; - - /* Right now we build a new DecoderMethod every time. - * TODO(haberman): properly cache methods by their true key. */ - const mgroup *g = mgroup_new(opts->handlers, c->allow_jit_, opts->lazy, c); - upb_inttable_push(&c->groups, upb_value_constptr(g)); - - ok = upb_inttable_lookupptr(&g->methods, opts->handlers, &v); - UPB_ASSERT(ok); - return upb_value_getptr(v); -} - - -/* upb_pbdecodermethodopts ****************************************************/ - -void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts, - const upb_handlers *h) { - opts->handlers = h; - opts->lazy = false; -} - -void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy) { - opts->lazy = lazy; -} -/* -** upb::Decoder (Bytecode Decoder VM) -** -** Bytecode must previously have been generated using the bytecode compiler in -** compile_decoder.c. This decoder then walks through the bytecode op-by-op to -** parse the input. -** -** Decoding is fully resumable; we just keep a pointer to the current bytecode -** instruction and resume from there. A fair amount of the logic here is to -** handle the fact that values can span buffer seams and we have to be able to -** be capable of suspending/resuming from any byte in the stream. This -** sometimes requires keeping a few trailing bytes from the last buffer around -** in the "residual" buffer. -*/ - -#include -#include - -#ifdef UPB_DUMP_BYTECODE -#include -#endif - -#define CHECK_SUSPEND(x) if (!(x)) return upb_pbdecoder_suspend(d); - -/* Error messages that are shared between the bytecode and JIT decoders. */ -const char *kPbDecoderStackOverflow = "Nesting too deep."; -const char *kPbDecoderSubmessageTooLong = - "Submessage end extends past enclosing submessage."; - -/* Error messages shared within this file. */ -static const char *kUnterminatedVarint = "Unterminated varint."; - -/* upb_pbdecoder **************************************************************/ - -static opcode halt = OP_HALT; - -/* A dummy character we can point to when the user passes us a NULL buffer. - * We need this because in C (NULL + 0) and (NULL - NULL) are undefined - * behavior, which would invalidate functions like curbufleft(). */ -static const char dummy_char; - -/* Whether an op consumes any of the input buffer. */ -static bool consumes_input(opcode op) { - switch (op) { - case OP_SETDISPATCH: - case OP_STARTMSG: - case OP_ENDMSG: - case OP_STARTSEQ: - case OP_ENDSEQ: - case OP_STARTSUBMSG: - case OP_ENDSUBMSG: - case OP_STARTSTR: - case OP_ENDSTR: - case OP_PUSHTAGDELIM: - case OP_POP: - case OP_SETDELIM: - case OP_SETBIGGROUPNUM: - case OP_CHECKDELIM: - case OP_CALL: - case OP_RET: - case OP_BRANCH: - return false; - default: - return true; - } -} - -static size_t stacksize(upb_pbdecoder *d, size_t entries) { - UPB_UNUSED(d); - return entries * sizeof(upb_pbdecoder_frame); -} - -static size_t callstacksize(upb_pbdecoder *d, size_t entries) { - UPB_UNUSED(d); - -#ifdef UPB_USE_JIT_X64 - if (d->method_->is_native_) { - /* Each native stack frame needs two pointers, plus we need a few frames for - * the enter/exit trampolines. */ - size_t ret = entries * sizeof(void*) * 2; - ret += sizeof(void*) * 10; - return ret; - } -#endif - - return entries * sizeof(uint32_t*); -} - - -static bool in_residual_buf(const upb_pbdecoder *d, const char *p); - -/* It's unfortunate that we have to micro-manage the compiler with - * UPB_FORCEINLINE and UPB_NOINLINE, especially since this tuning is necessarily - * specific to one hardware configuration. But empirically on a Core i7, - * performance increases 30-50% with these annotations. Every instance where - * these appear, gcc 4.2.1 made the wrong decision and degraded performance in - * benchmarks. */ - -static void seterr(upb_pbdecoder *d, const char *msg) { - upb_status status = UPB_STATUS_INIT; - upb_status_seterrmsg(&status, msg); - upb_env_reporterror(d->env, &status); -} - -void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg) { - seterr(d, msg); -} - - -/* Buffering ******************************************************************/ - -/* We operate on one buffer at a time, which is either the user's buffer passed - * to our "decode" callback or some residual bytes from the previous buffer. */ - -/* How many bytes can be safely read from d->ptr without reading past end-of-buf - * or past the current delimited end. */ -static size_t curbufleft(const upb_pbdecoder *d) { - UPB_ASSERT(d->data_end >= d->ptr); - return d->data_end - d->ptr; -} - -/* How many bytes are available before end-of-buffer. */ -static size_t bufleft(const upb_pbdecoder *d) { - return d->end - d->ptr; -} - -/* Overall stream offset of d->ptr. */ -uint64_t offset(const upb_pbdecoder *d) { - return d->bufstart_ofs + (d->ptr - d->buf); -} - -/* How many bytes are available before the end of this delimited region. */ -size_t delim_remaining(const upb_pbdecoder *d) { - return d->top->end_ofs - offset(d); -} - -/* Advances d->ptr. */ -static void advance(upb_pbdecoder *d, size_t len) { - UPB_ASSERT(curbufleft(d) >= len); - d->ptr += len; -} - -static bool in_buf(const char *p, const char *buf, const char *end) { - return p >= buf && p <= end; -} - -static bool in_residual_buf(const upb_pbdecoder *d, const char *p) { - return in_buf(p, d->residual, d->residual_end); -} - -/* Calculates the delim_end value, which is affected by both the current buffer - * and the parsing stack, so must be called whenever either is updated. */ -static void set_delim_end(upb_pbdecoder *d) { - size_t delim_ofs = d->top->end_ofs - d->bufstart_ofs; - if (delim_ofs <= (size_t)(d->end - d->buf)) { - d->delim_end = d->buf + delim_ofs; - d->data_end = d->delim_end; - } else { - d->data_end = d->end; - d->delim_end = NULL; - } -} - -static void switchtobuf(upb_pbdecoder *d, const char *buf, const char *end) { - d->ptr = buf; - d->buf = buf; - d->end = end; - set_delim_end(d); -} - -static void advancetobuf(upb_pbdecoder *d, const char *buf, size_t len) { - UPB_ASSERT(curbufleft(d) == 0); - d->bufstart_ofs += (d->end - d->buf); - switchtobuf(d, buf, buf + len); -} - -static void checkpoint(upb_pbdecoder *d) { - /* The assertion here is in the interests of efficiency, not correctness. - * We are trying to ensure that we don't checkpoint() more often than - * necessary. */ - UPB_ASSERT(d->checkpoint != d->ptr); - d->checkpoint = d->ptr; -} - -/* Skips "bytes" bytes in the stream, which may be more than available. If we - * skip more bytes than are available, we return a long read count to the caller - * indicating how many bytes can be skipped over before passing actual data - * again. Skipped bytes can pass a NULL buffer and the decoder guarantees they - * won't actually be read. - */ -static int32_t skip(upb_pbdecoder *d, size_t bytes) { - UPB_ASSERT(!in_residual_buf(d, d->ptr) || d->size_param == 0); - UPB_ASSERT(d->skip == 0); - if (bytes > delim_remaining(d)) { - seterr(d, "Skipped value extended beyond enclosing submessage."); - return upb_pbdecoder_suspend(d); - } else if (bufleft(d) >= bytes) { - /* Skipped data is all in current buffer, and more is still available. */ - advance(d, bytes); - d->skip = 0; - return DECODE_OK; - } else { - /* Skipped data extends beyond currently available buffers. */ - d->pc = d->last; - d->skip = bytes - curbufleft(d); - d->bufstart_ofs += (d->end - d->buf); - d->residual_end = d->residual; - switchtobuf(d, d->residual, d->residual_end); - return d->size_param + d->skip; - } -} - - -/* Resumes the decoder from an initial state or from a previous suspend. */ -int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf, - size_t size, const upb_bufhandle *handle) { - UPB_UNUSED(p); /* Useless; just for the benefit of the JIT. */ - - /* d->skip and d->residual_end could probably elegantly be represented - * as a single variable, to more easily represent this invariant. */ - UPB_ASSERT(!(d->skip && d->residual_end > d->residual)); - - /* We need to remember the original size_param, so that the value we return - * is relative to it, even if we do some skipping first. */ - d->size_param = size; - d->handle = handle; - - /* Have to handle this case specially (ie. not with skip()) because the user - * is allowed to pass a NULL buffer here, which won't allow us to safely - * calculate a d->end or use our normal functions like curbufleft(). */ - if (d->skip && d->skip >= size) { - d->skip -= size; - d->bufstart_ofs += size; - buf = &dummy_char; - size = 0; - - /* We can't just return now, because we might need to execute some ops - * like CHECKDELIM, which could call some callbacks and pop the stack. */ - } - - /* We need to pretend that this was the actual buffer param, since some of the - * calculations assume that d->ptr/d->buf is relative to this. */ - d->buf_param = buf; - - if (!buf) { - /* NULL buf is ok if its entire span is covered by the "skip" above, but - * by this point we know that "skip" doesn't cover the buffer. */ - seterr(d, "Passed NULL buffer over non-skippable region."); - return upb_pbdecoder_suspend(d); - } - - if (d->residual_end > d->residual) { - /* We have residual bytes from the last buffer. */ - UPB_ASSERT(d->ptr == d->residual); - } else { - switchtobuf(d, buf, buf + size); - } - - d->checkpoint = d->ptr; - - /* Handle skips that don't cover the whole buffer (as above). */ - if (d->skip) { - size_t skip_bytes = d->skip; - d->skip = 0; - CHECK_RETURN(skip(d, skip_bytes)); - checkpoint(d); - } - - /* If we're inside an unknown group, continue to parse unknown values. */ - if (d->top->groupnum < 0) { - CHECK_RETURN(upb_pbdecoder_skipunknown(d, -1, 0)); - checkpoint(d); - } - - return DECODE_OK; -} - -/* Suspends the decoder at the last checkpoint, without saving any residual - * bytes. If there are any unconsumed bytes, returns a short byte count. */ -size_t upb_pbdecoder_suspend(upb_pbdecoder *d) { - d->pc = d->last; - if (d->checkpoint == d->residual) { - /* Checkpoint was in residual buf; no user bytes were consumed. */ - d->ptr = d->residual; - return 0; - } else { - size_t ret = d->size_param - (d->end - d->checkpoint); - UPB_ASSERT(!in_residual_buf(d, d->checkpoint)); - UPB_ASSERT(d->buf == d->buf_param || d->buf == &dummy_char); - - d->bufstart_ofs += (d->checkpoint - d->buf); - d->residual_end = d->residual; - switchtobuf(d, d->residual, d->residual_end); - return ret; - } -} - -/* Suspends the decoder at the last checkpoint, and saves any unconsumed - * bytes in our residual buffer. This is necessary if we need more user - * bytes to form a complete value, which might not be contiguous in the - * user's buffers. Always consumes all user bytes. */ -static size_t suspend_save(upb_pbdecoder *d) { - /* We hit end-of-buffer before we could parse a full value. - * Save any unconsumed bytes (if any) to the residual buffer. */ - d->pc = d->last; - - if (d->checkpoint == d->residual) { - /* Checkpoint was in residual buf; append user byte(s) to residual buf. */ - UPB_ASSERT((d->residual_end - d->residual) + d->size_param <= - sizeof(d->residual)); - if (!in_residual_buf(d, d->ptr)) { - d->bufstart_ofs -= (d->residual_end - d->residual); - } - memcpy(d->residual_end, d->buf_param, d->size_param); - d->residual_end += d->size_param; - } else { - /* Checkpoint was in user buf; old residual bytes not needed. */ - size_t save; - UPB_ASSERT(!in_residual_buf(d, d->checkpoint)); - - d->ptr = d->checkpoint; - save = curbufleft(d); - UPB_ASSERT(save <= sizeof(d->residual)); - memcpy(d->residual, d->ptr, save); - d->residual_end = d->residual + save; - d->bufstart_ofs = offset(d); + VMCASE(OP_SETDISPATCH, + d->top->base = d->pc - 1; + memcpy(&d->top->dispatch, d->pc, sizeof(void*)); + d->pc += sizeof(void*) / sizeof(uint32_t); + ) + VMCASE(OP_STARTMSG, + CHECK_SUSPEND(upb_sink_startmsg(d->top->sink)); + ) + VMCASE(OP_ENDMSG, + CHECK_SUSPEND(upb_sink_endmsg(d->top->sink, d->status)); + ) + VMCASE(OP_STARTSEQ, + upb_pbdecoder_frame *outer = outer_frame(d); + CHECK_SUSPEND(upb_sink_startseq(outer->sink, arg, &d->top->sink)); + ) + VMCASE(OP_ENDSEQ, + CHECK_SUSPEND(upb_sink_endseq(d->top->sink, arg)); + ) + VMCASE(OP_STARTSUBMSG, + upb_pbdecoder_frame *outer = outer_frame(d); + CHECK_SUSPEND(upb_sink_startsubmsg(outer->sink, arg, &d->top->sink)); + ) + VMCASE(OP_ENDSUBMSG, + CHECK_SUSPEND(upb_sink_endsubmsg(d->top->sink, arg)); + ) + VMCASE(OP_STARTSTR, + uint32_t len = delim_remaining(d); + upb_pbdecoder_frame *outer = outer_frame(d); + CHECK_SUSPEND(upb_sink_startstr(outer->sink, arg, len, &d->top->sink)); + if (len == 0) { + d->pc++; /* Skip OP_STRING. */ + } + ) + VMCASE(OP_STRING, + uint32_t len = curbufleft(d); + size_t n = upb_sink_putstring(d->top->sink, arg, d->ptr, len, handle); + if (n > len) { + if (n > delim_remaining(d)) { + seterr(d, "Tried to skip past end of string."); + return upb_pbdecoder_suspend(d); + } else { + int32_t ret = skip(d, n); + /* This shouldn't return DECODE_OK, because n > len. */ + UPB_ASSERT(ret >= 0); + return ret; + } + } + advance(d, n); + if (n < len || d->delim_end == NULL) { + /* We aren't finished with this string yet. */ + d->pc--; /* Repeat OP_STRING. */ + if (n > 0) checkpoint(d); + return upb_pbdecoder_suspend(d); + } + ) + VMCASE(OP_ENDSTR, + CHECK_SUSPEND(upb_sink_endstr(d->top->sink, arg)); + ) + VMCASE(OP_PUSHTAGDELIM, + CHECK_SUSPEND(pushtagdelim(d, arg)); + ) + VMCASE(OP_SETBIGGROUPNUM, + d->top->groupnum = *d->pc++; + ) + VMCASE(OP_POP, + UPB_ASSERT(d->top > d->stack); + decoder_pop(d); + ) + VMCASE(OP_PUSHLENDELIM, + uint32_t len; + CHECK_RETURN(decode_v32(d, &len)); + CHECK_SUSPEND(decoder_push(d, offset(d) + len)); + set_delim_end(d); + ) + VMCASE(OP_SETDELIM, + set_delim_end(d); + ) + VMCASE(OP_CHECKDELIM, + /* We are guaranteed of this assert because we never allow ourselves to + * consume bytes beyond data_end, which covers delim_end when non-NULL. + */ + UPB_ASSERT(!(d->delim_end && d->ptr > d->delim_end)); + if (d->ptr == d->delim_end) + d->pc += longofs; + ) + VMCASE(OP_CALL, + d->callstack[d->call_len++] = d->pc; + d->pc += longofs; + ) + VMCASE(OP_RET, + UPB_ASSERT(d->call_len > 0); + d->pc = d->callstack[--d->call_len]; + ) + VMCASE(OP_BRANCH, + d->pc += longofs; + ) + VMCASE(OP_TAG1, + uint8_t expected; + CHECK_SUSPEND(curbufleft(d) > 0); + expected = (arg >> 8) & 0xff; + if (*d->ptr == expected) { + advance(d, 1); + } else { + int8_t shortofs; + badtag: + shortofs = arg; + if (shortofs == LABEL_DISPATCH) { + CHECK_RETURN(dispatch(d)); + } else { + d->pc += shortofs; + break; /* Avoid checkpoint(). */ + } + } + ) + VMCASE(OP_TAG2, + uint16_t expected; + CHECK_SUSPEND(curbufleft(d) > 0); + expected = (arg >> 8) & 0xffff; + if (curbufleft(d) >= 2) { + uint16_t actual; + memcpy(&actual, d->ptr, 2); + if (expected == actual) { + advance(d, 2); + } else { + goto badtag; + } + } else { + int32_t result = upb_pbdecoder_checktag_slow(d, expected); + if (result == DECODE_MISMATCH) goto badtag; + if (result >= 0) return result; + } + ) + VMCASE(OP_TAGN, { + uint64_t expected; + int32_t result; + memcpy(&expected, d->pc, 8); + d->pc += 2; + result = upb_pbdecoder_checktag_slow(d, expected); + if (result == DECODE_MISMATCH) goto badtag; + if (result >= 0) return result; + }) + VMCASE(OP_DISPATCH, { + CHECK_RETURN(dispatch(d)); + }) + VMCASE(OP_HALT, { + return d->size_param; + }) + } } - - switchtobuf(d, d->residual, d->residual_end); - return d->size_param; -} - -/* Copies the next "bytes" bytes into "buf" and advances the stream. - * Requires that this many bytes are available in the current buffer. */ -UPB_FORCEINLINE static void consumebytes(upb_pbdecoder *d, void *buf, - size_t bytes) { - UPB_ASSERT(bytes <= curbufleft(d)); - memcpy(buf, d->ptr, bytes); - advance(d, bytes); } -/* Slow path for getting the next "bytes" bytes, regardless of whether they are - * available in the current buffer or not. Returns a status code as described - * in decoder.int.h. */ -UPB_NOINLINE static int32_t getbytes_slow(upb_pbdecoder *d, void *buf, - size_t bytes) { - const size_t avail = curbufleft(d); - consumebytes(d, buf, avail); - bytes -= avail; - UPB_ASSERT(bytes > 0); - if (in_residual_buf(d, d->ptr)) { - advancetobuf(d, d->buf_param, d->size_param); - } - if (curbufleft(d) >= bytes) { - consumebytes(d, (char *)buf + avail, bytes); - return DECODE_OK; - } else if (d->data_end == d->delim_end) { - seterr(d, "Submessage ended in the middle of a value or group"); - return upb_pbdecoder_suspend(d); - } else { - return suspend_save(d); - } -} -/* Gets the next "bytes" bytes, regardless of whether they are available in the - * current buffer or not. Returns a status code as described in decoder.int.h. - */ -UPB_FORCEINLINE static int32_t getbytes(upb_pbdecoder *d, void *buf, - size_t bytes) { - if (curbufleft(d) >= bytes) { - /* Buffer has enough data to satisfy. */ - consumebytes(d, buf, bytes); - return DECODE_OK; - } else { - return getbytes_slow(d, buf, bytes); - } -} +/* BytesHandler handlers ******************************************************/ -UPB_NOINLINE static size_t peekbytes_slow(upb_pbdecoder *d, void *buf, - size_t bytes) { - size_t ret = curbufleft(d); - memcpy(buf, d->ptr, ret); - if (in_residual_buf(d, d->ptr)) { - size_t copy = UPB_MIN(bytes - ret, d->size_param); - memcpy((char *)buf + ret, d->buf_param, copy); - ret += copy; - } - return ret; +void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) { + upb_pbdecoder *d = closure; + UPB_UNUSED(size_hint); + d->top->end_ofs = UINT64_MAX; + d->bufstart_ofs = 0; + d->call_len = 1; + d->callstack[0] = &halt; + d->pc = pc; + d->skip = 0; + return d; } -UPB_FORCEINLINE static size_t peekbytes(upb_pbdecoder *d, void *buf, - size_t bytes) { - if (curbufleft(d) >= bytes) { - memcpy(buf, d->ptr, bytes); - return bytes; - } else { - return peekbytes_slow(d, buf, bytes); - } +void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) { + upb_pbdecoder *d = closure; + UPB_UNUSED(hd); + UPB_UNUSED(size_hint); + d->top->end_ofs = UINT64_MAX; + d->bufstart_ofs = 0; + d->call_len = 0; + d->skip = 0; + return d; } +bool upb_pbdecoder_end(void *closure, const void *handler_data) { + upb_pbdecoder *d = closure; + const upb_pbdecodermethod *method = handler_data; + uint64_t end; + char dummy; -/* Decoding of wire types *****************************************************/ + if (d->residual_end > d->residual) { + seterr(d, "Unexpected EOF: decoder still has buffered unparsed data"); + return false; + } -/* Slow path for decoding a varint from the current buffer position. - * Returns a status code as described in decoder.int.h. */ -UPB_NOINLINE int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, - uint64_t *u64) { - uint8_t byte = 0x80; - int bitpos; - *u64 = 0; - for(bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) { - CHECK_RETURN(getbytes(d, &byte, 1)); - *u64 |= (uint64_t)(byte & 0x7F) << bitpos; + if (d->skip) { + seterr(d, "Unexpected EOF inside skipped data"); + return false; } - if(bitpos == 70 && (byte & 0x80)) { - seterr(d, kUnterminatedVarint); - return upb_pbdecoder_suspend(d); + + if (d->top->end_ofs != UINT64_MAX) { + seterr(d, "Unexpected EOF inside delimited string"); + return false; } - return DECODE_OK; -} -/* Decodes a varint from the current buffer position. - * Returns a status code as described in decoder.int.h. */ -UPB_FORCEINLINE static int32_t decode_varint(upb_pbdecoder *d, uint64_t *u64) { - if (curbufleft(d) > 0 && !(*d->ptr & 0x80)) { - *u64 = *d->ptr; - advance(d, 1); - return DECODE_OK; - } else if (curbufleft(d) >= 10) { - /* Fast case. */ - upb_decoderet r = upb_vdecode_fast(d->ptr); - if (r.p == NULL) { - seterr(d, kUnterminatedVarint); - return upb_pbdecoder_suspend(d); + /* The user's end() call indicates that the message ends here. */ + end = offset(d); + d->top->end_ofs = end; + +#ifdef UPB_USE_JIT_X64 + if (method->is_native_) { + const mgroup *group = (const mgroup*)method->group; + if (d->top != d->stack) + d->stack->end_ofs = 0; + group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL); + } else +#endif + { + const uint32_t *p = d->pc; + d->stack->end_ofs = end; + /* Check the previous bytecode, but guard against beginning. */ + if (p != method->code_base.ptr) p--; + if (getop(*p) == OP_CHECKDELIM) { + /* Rewind from OP_TAG* to OP_CHECKDELIM. */ + UPB_ASSERT(getop(*d->pc) == OP_TAG1 || + getop(*d->pc) == OP_TAG2 || + getop(*d->pc) == OP_TAGN || + getop(*d->pc) == OP_DISPATCH); + d->pc = p; } - advance(d, r.p - d->ptr); - *u64 = r.val; - return DECODE_OK; - } else { - /* Slow case -- varint spans buffer seam. */ - return upb_pbdecoder_decode_varint_slow(d, u64); + upb_pbdecoder_decode(closure, handler_data, &dummy, 0, NULL); } -} -/* Decodes a 32-bit varint from the current buffer position. - * Returns a status code as described in decoder.int.h. */ -UPB_FORCEINLINE static int32_t decode_v32(upb_pbdecoder *d, uint32_t *u32) { - uint64_t u64; - int32_t ret = decode_varint(d, &u64); - if (ret >= 0) return ret; - if (u64 > UINT32_MAX) { - seterr(d, "Unterminated 32-bit varint"); - /* TODO(haberman) guarantee that this function return is >= 0 somehow, - * so we know this path will always be treated as error by our caller. - * Right now the size_t -> int32_t can overflow and produce negative values. - */ - *u32 = 0; - return upb_pbdecoder_suspend(d); + if (d->call_len != 0) { + seterr(d, "Unexpected EOF inside submessage or group"); + return false; } - *u32 = u64; - return DECODE_OK; -} -/* Decodes a fixed32 from the current buffer position. - * Returns a status code as described in decoder.int.h. - * TODO: proper byte swapping for big-endian machines. */ -UPB_FORCEINLINE static int32_t decode_fixed32(upb_pbdecoder *d, uint32_t *u32) { - return getbytes(d, u32, 4); + return true; } -/* Decodes a fixed64 from the current buffer position. - * Returns a status code as described in decoder.int.h. - * TODO: proper byte swapping for big-endian machines. */ -UPB_FORCEINLINE static int32_t decode_fixed64(upb_pbdecoder *d, uint64_t *u64) { - return getbytes(d, u64, 8); -} +size_t upb_pbdecoder_decode(void *decoder, const void *group, const char *buf, + size_t size, const upb_bufhandle *handle) { + int32_t result = upb_pbdecoder_resume(decoder, NULL, buf, size, handle); -/* Non-static versions of the above functions. - * These are called by the JIT for fallback paths. */ -int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32) { - return decode_fixed32(d, u32); + if (result == DECODE_ENDGROUP) goto_endmsg(decoder); + CHECK_RETURN(result); + + return run_decoder_vm(decoder, group, handle); } -int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64) { - return decode_fixed64(d, u64); + +/* Public API *****************************************************************/ + +void upb_pbdecoder_reset(upb_pbdecoder *d) { + d->top = d->stack; + d->top->groupnum = 0; + d->ptr = d->residual; + d->buf = d->residual; + d->end = d->residual; + d->residual_end = d->residual; } -static double as_double(uint64_t n) { double d; memcpy(&d, &n, 8); return d; } -static float as_float(uint32_t n) { float f; memcpy(&f, &n, 4); return f; } +upb_pbdecoder *upb_pbdecoder_create(upb_arena *a, const upb_pbdecodermethod *m, + upb_sink sink, upb_status *status) { + const size_t default_max_nesting = 64; +#ifndef NDEBUG + size_t size_before = upb_arena_bytesallocated(a); +#endif -/* Pushes a frame onto the decoder stack. */ -static bool decoder_push(upb_pbdecoder *d, uint64_t end) { - upb_pbdecoder_frame *fr = d->top; + upb_pbdecoder *d = upb_arena_malloc(a, sizeof(upb_pbdecoder)); + if (!d) return NULL; - if (end > fr->end_ofs) { - seterr(d, kPbDecoderSubmessageTooLong); - return false; - } else if (fr == d->limit) { - seterr(d, kPbDecoderStackOverflow); - return false; + d->method_ = m; + d->callstack = upb_arena_malloc(a, callstacksize(d, default_max_nesting)); + d->stack = upb_arena_malloc(a, stacksize(d, default_max_nesting)); + if (!d->stack || !d->callstack) { + return NULL; } - fr++; - fr->end_ofs = end; - fr->dispatch = NULL; - fr->groupnum = 0; - d->top = fr; - return true; + d->arena = a; + d->limit = d->stack + default_max_nesting - 1; + d->stack_size = default_max_nesting; + d->status = status; + + upb_pbdecoder_reset(d); + upb_bytessink_reset(&d->input_, &m->input_handler_, d); + + if (d->method_->dest_handlers_) { + if (sink.handlers != d->method_->dest_handlers_) + return NULL; + } + d->top->sink = sink; + + /* If this fails, increase the value in decoder.h. */ + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(a) - size_before <= + UPB_PB_DECODER_SIZE); + return d; } -static bool pushtagdelim(upb_pbdecoder *d, uint32_t arg) { - /* While we expect to see an "end" tag (either ENDGROUP or a non-sequence - * field number) prior to hitting any enclosing submessage end, pushing our - * existing delim end prevents us from continuing to parse values from a - * corrupt proto that doesn't give us an END tag in time. */ - if (!decoder_push(d, d->top->end_ofs)) - return false; - d->top->groupnum = arg; - return true; +uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d) { + return offset(d); } -/* Pops a frame from the decoder stack. */ -static void decoder_pop(upb_pbdecoder *d) { d->top--; } +const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) { + return d->method_; +} -UPB_NOINLINE int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, - uint64_t expected) { - uint64_t data = 0; - size_t bytes = upb_value_size(expected); - size_t read = peekbytes(d, &data, bytes); - if (read == bytes && data == expected) { - /* Advance past matched bytes. */ - int32_t ok = getbytes(d, &data, read); - UPB_ASSERT(ok < 0); - return DECODE_OK; - } else if (read < bytes && memcmp(&data, &expected, read) == 0) { - return suspend_save(d); - } else { - return DECODE_MISMATCH; - } +upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d) { + return d->input_; } -int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum, - uint8_t wire_type) { - if (fieldnum >= 0) - goto have_tag; +size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) { + return d->stack_size; +} - while (true) { - uint32_t tag; - CHECK_RETURN(decode_v32(d, &tag)); - wire_type = tag & 0x7; - fieldnum = tag >> 3; +bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) { + UPB_ASSERT(d->top >= d->stack); -have_tag: - if (fieldnum == 0) { - seterr(d, "Saw invalid field number (0)"); - return upb_pbdecoder_suspend(d); - } + if (max < (size_t)(d->top - d->stack)) { + /* Can't set a limit smaller than what we are currently at. */ + return false; + } - switch (wire_type) { - case UPB_WIRE_TYPE_32BIT: - CHECK_RETURN(skip(d, 4)); - break; - case UPB_WIRE_TYPE_64BIT: - CHECK_RETURN(skip(d, 8)); - break; - case UPB_WIRE_TYPE_VARINT: { - uint64_t u64; - CHECK_RETURN(decode_varint(d, &u64)); - break; - } - case UPB_WIRE_TYPE_DELIMITED: { - uint32_t len; - CHECK_RETURN(decode_v32(d, &len)); - CHECK_RETURN(skip(d, len)); - break; - } - case UPB_WIRE_TYPE_START_GROUP: - CHECK_SUSPEND(pushtagdelim(d, -fieldnum)); - break; - case UPB_WIRE_TYPE_END_GROUP: - if (fieldnum == -d->top->groupnum) { - decoder_pop(d); - } else if (fieldnum == d->top->groupnum) { - return DECODE_ENDGROUP; - } else { - seterr(d, "Unmatched ENDGROUP tag."); - return upb_pbdecoder_suspend(d); - } - break; - default: - seterr(d, "Invalid wire type"); - return upb_pbdecoder_suspend(d); + if (max > d->stack_size) { + /* Need to reallocate stack and callstack to accommodate. */ + size_t old_size = stacksize(d, d->stack_size); + size_t new_size = stacksize(d, max); + void *p = upb_arena_realloc(d->arena, d->stack, old_size, new_size); + if (!p) { + return false; } + d->stack = p; - if (d->top->groupnum >= 0) { - /* TODO: More code needed for handling unknown groups. */ - upb_sink_putunknown(&d->top->sink, d->checkpoint, d->ptr - d->checkpoint); - return DECODE_OK; + old_size = callstacksize(d, d->stack_size); + new_size = callstacksize(d, max); + p = upb_arena_realloc(d->arena, d->callstack, old_size, new_size); + if (!p) { + return false; } + d->callstack = p; - /* Unknown group -- continue looping over unknown fields. */ - checkpoint(d); + d->stack_size = max; } -} -static void goto_endmsg(upb_pbdecoder *d) { - upb_value v; - bool found = upb_inttable_lookup32(d->top->dispatch, DISPATCH_ENDMSG, &v); - UPB_ASSERT(found); - d->pc = d->top->base + upb_value_getuint64(v); + d->limit = d->stack + max - 1; + return true; } +/* +** upb::Encoder +** +** Since we are implementing pure handlers (ie. without any out-of-band access +** to pre-computed lengths), we have to buffer all submessages before we can +** emit even their first byte. +** +** Not knowing the size of submessages also means we can't write a perfect +** zero-copy implementation, even with buffering. Lengths are stored as +** varints, which means that we don't know how many bytes to reserve for the +** length until we know what the length is. +** +** This leaves us with three main choices: +** +** 1. buffer all submessage data in a temporary buffer, then copy it exactly +** once into the output buffer. +** +** 2. attempt to buffer data directly into the output buffer, estimating how +** many bytes each length will take. When our guesses are wrong, use +** memmove() to grow or shrink the allotted space. +** +** 3. buffer directly into the output buffer, allocating a max length +** ahead-of-time for each submessage length. If we overallocated, we waste +** space, but no memcpy() or memmove() is required. This approach requires +** defining a maximum size for submessages and rejecting submessages that +** exceed that size. +** +** (2) and (3) have the potential to have better performance, but they are more +** complicated and subtle to implement: +** +** (3) requires making an arbitrary choice of the maximum message size; it +** wastes space when submessages are shorter than this and fails +** completely when they are longer. This makes it more finicky and +** requires configuration based on the input. It also makes it impossible +** to perfectly match the output of reference encoders that always use the +** optimal amount of space for each length. +** +** (2) requires guessing the the size upfront, and if multiple lengths are +** guessed wrong the minimum required number of memmove() operations may +** be complicated to compute correctly. Implemented properly, it may have +** a useful amortized or average cost, but more investigation is required +** to determine this and what the optimal algorithm is to achieve it. +** +** (1) makes you always pay for exactly one copy, but its implementation is +** the simplest and its performance is predictable. +** +** So for now, we implement (1) only. If we wish to optimize later, we should +** be able to do it without affecting users. +** +** The strategy is to buffer the segments of data that do *not* depend on +** unknown lengths in one buffer, and keep a separate buffer of segment pointers +** and lengths. When the top-level submessage ends, we can go beginning to end, +** alternating the writing of lengths with memcpy() of the rest of the data. +** At the top level though, no buffering is required. +*/ -/* Parses a tag and jumps to the corresponding bytecode instruction for this - * field. + + +/* The output buffer is divided into segments; a segment is a string of data + * that is "ready to go" -- it does not need any varint lengths inserted into + * the middle. The seams between segments are where varints will be inserted + * once they are known. * - * If the tag is unknown (or the wire type doesn't match), parses the field as - * unknown. If the tag is a valid ENDGROUP tag, jumps to the bytecode - * instruction for the end of message. */ -static int32_t dispatch(upb_pbdecoder *d) { - upb_inttable *dispatch = d->top->dispatch; - uint32_t tag; - uint8_t wire_type; - uint32_t fieldnum; - upb_value val; - int32_t retval; + * We also use the concept of a "run", which is a range of encoded bytes that + * occur at a single submessage level. Every segment contains one or more runs. + * + * A segment can span messages. Consider: + * + * .--Submessage lengths---------. + * | | | + * | V V + * V | |--------------- | |----------------- + * Submessages: | |----------------------------------------------- + * Top-level msg: ------------------------------------------------------------ + * + * Segments: ----- ------------------- ----------------- + * Runs: *---- *--------------*--- *---------------- + * (* marks the start) + * + * Note that the top-level menssage is not in any segment because it does not + * have any length preceding it. + * + * A segment is only interrupted when another length needs to be inserted. So + * observe how the second segment spans both the inner submessage and part of + * the next enclosing message. */ +typedef struct { + uint32_t msglen; /* The length to varint-encode before this segment. */ + uint32_t seglen; /* Length of the segment. */ +} upb_pb_encoder_segment; - /* Decode tag. */ - CHECK_RETURN(decode_v32(d, &tag)); - wire_type = tag & 0x7; - fieldnum = tag >> 3; +struct upb_pb_encoder { + upb_arena *arena; - /* Lookup tag. Because of packed/non-packed compatibility, we have to - * check the wire type against two possibilities. */ - if (fieldnum != DISPATCH_ENDMSG && - upb_inttable_lookup32(dispatch, fieldnum, &val)) { - uint64_t v = upb_value_getuint64(val); - if (wire_type == (v & 0xff)) { - d->pc = d->top->base + (v >> 16); - return DECODE_OK; - } else if (wire_type == ((v >> 8) & 0xff)) { - bool found = - upb_inttable_lookup(dispatch, fieldnum + UPB_MAX_FIELDNUMBER, &val); - UPB_ASSERT(found); - d->pc = d->top->base + upb_value_getuint64(val); - return DECODE_OK; - } - } + /* Our input and output. */ + upb_sink input_; + upb_bytessink output_; + + /* The "subclosure" -- used as the inner closure as part of the bytessink + * protocol. */ + void *subc; + + /* The output buffer and limit, and our current write position. "buf" + * initially points to "initbuf", but is dynamically allocated if we need to + * grow beyond the initial size. */ + char *buf, *ptr, *limit; + + /* The beginning of the current run, or undefined if we are at the top + * level. */ + char *runbegin; + + /* The list of segments we are accumulating. */ + upb_pb_encoder_segment *segbuf, *segptr, *seglimit; - /* We have some unknown fields (or ENDGROUP) to parse. The DISPATCH or TAG - * bytecode that triggered this is preceded by a CHECKDELIM bytecode which - * we need to back up to, so that when we're done skipping unknown data we - * can re-check the delimited end. */ - d->last--; /* Necessary if we get suspended */ - d->pc = d->last; - UPB_ASSERT(getop(*d->last) == OP_CHECKDELIM); + /* The stack of enclosing submessages. Each entry in the stack points to the + * segment where this submessage's length is being accumulated. */ + int *stack, *top, *stacklimit; - /* Unknown field or ENDGROUP. */ - retval = upb_pbdecoder_skipunknown(d, fieldnum, wire_type); + /* Depth of startmsg/endmsg calls. */ + int depth; +}; - CHECK_RETURN(retval); +/* low-level buffering ********************************************************/ - if (retval == DECODE_ENDGROUP) { - goto_endmsg(d); - return DECODE_OK; - } +/* Low-level functions for interacting with the output buffer. */ - return DECODE_OK; +/* TODO(haberman): handle pushback */ +static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) { + size_t n = upb_bytessink_putbuf(e->output_, e->subc, buf, len, NULL); + UPB_ASSERT(n == len); } -/* Callers know that the stack is more than one deep because the opcodes that - * call this only occur after PUSH operations. */ -upb_pbdecoder_frame *outer_frame(upb_pbdecoder *d) { - UPB_ASSERT(d->top != d->stack); - return d->top - 1; +static upb_pb_encoder_segment *top(upb_pb_encoder *e) { + return &e->segbuf[*e->top]; } +/* Call to ensure that at least "bytes" bytes are available for writing at + * e->ptr. Returns false if the bytes could not be allocated. */ +static bool reserve(upb_pb_encoder *e, size_t bytes) { + if ((size_t)(e->limit - e->ptr) < bytes) { + /* Grow buffer. */ + char *new_buf; + size_t needed = bytes + (e->ptr - e->buf); + size_t old_size = e->limit - e->buf; -/* The main decoding loop *****************************************************/ + size_t new_size = old_size; -/* The main decoder VM function. Uses traditional bytecode dispatch loop with a - * switch() statement. */ -size_t run_decoder_vm(upb_pbdecoder *d, const mgroup *group, - const upb_bufhandle* handle) { + while (new_size < needed) { + new_size *= 2; + } -#define VMCASE(op, code) \ - case op: { code; if (consumes_input(op)) checkpoint(d); break; } -#define PRIMITIVE_OP(type, wt, name, convfunc, ctype) \ - VMCASE(OP_PARSE_ ## type, { \ - ctype val; \ - CHECK_RETURN(decode_ ## wt(d, &val)); \ - upb_sink_put ## name(&d->top->sink, arg, (convfunc)(val)); \ - }) + new_buf = upb_arena_realloc(e->arena, e->buf, old_size, new_size); - while(1) { - int32_t instruction; - opcode op; - uint32_t arg; - int32_t longofs; + if (new_buf == NULL) { + return false; + } - d->last = d->pc; - instruction = *d->pc++; - op = getop(instruction); - arg = instruction >> 8; - longofs = arg; - UPB_ASSERT(d->ptr != d->residual_end); - UPB_UNUSED(group); -#ifdef UPB_DUMP_BYTECODE - fprintf(stderr, "s_ofs=%d buf_ofs=%d data_rem=%d buf_rem=%d delim_rem=%d " - "%x %s (%d)\n", - (int)offset(d), - (int)(d->ptr - d->buf), - (int)(d->data_end - d->ptr), - (int)(d->end - d->ptr), - (int)((d->top->end_ofs - d->bufstart_ofs) - (d->ptr - d->buf)), - (int)(d->pc - 1 - group->bytecode), - upb_pbdecoder_getopname(op), - arg); -#endif - switch (op) { - /* Technically, we are losing data if we see a 32-bit varint that is not - * properly sign-extended. We could detect this and error about the data - * loss, but proto2 does not do this, so we pass. */ - PRIMITIVE_OP(INT32, varint, int32, int32_t, uint64_t) - PRIMITIVE_OP(INT64, varint, int64, int64_t, uint64_t) - PRIMITIVE_OP(UINT32, varint, uint32, uint32_t, uint64_t) - PRIMITIVE_OP(UINT64, varint, uint64, uint64_t, uint64_t) - PRIMITIVE_OP(FIXED32, fixed32, uint32, uint32_t, uint32_t) - PRIMITIVE_OP(FIXED64, fixed64, uint64, uint64_t, uint64_t) - PRIMITIVE_OP(SFIXED32, fixed32, int32, int32_t, uint32_t) - PRIMITIVE_OP(SFIXED64, fixed64, int64, int64_t, uint64_t) - PRIMITIVE_OP(BOOL, varint, bool, bool, uint64_t) - PRIMITIVE_OP(DOUBLE, fixed64, double, as_double, uint64_t) - PRIMITIVE_OP(FLOAT, fixed32, float, as_float, uint32_t) - PRIMITIVE_OP(SINT32, varint, int32, upb_zzdec_32, uint64_t) - PRIMITIVE_OP(SINT64, varint, int64, upb_zzdec_64, uint64_t) + e->ptr = new_buf + (e->ptr - e->buf); + e->runbegin = new_buf + (e->runbegin - e->buf); + e->limit = new_buf + new_size; + e->buf = new_buf; + } - VMCASE(OP_SETDISPATCH, - d->top->base = d->pc - 1; - memcpy(&d->top->dispatch, d->pc, sizeof(void*)); - d->pc += sizeof(void*) / sizeof(uint32_t); - ) - VMCASE(OP_STARTMSG, - CHECK_SUSPEND(upb_sink_startmsg(&d->top->sink)); - ) - VMCASE(OP_ENDMSG, - CHECK_SUSPEND(upb_sink_endmsg(&d->top->sink, d->status)); - ) - VMCASE(OP_STARTSEQ, - upb_pbdecoder_frame *outer = outer_frame(d); - CHECK_SUSPEND(upb_sink_startseq(&outer->sink, arg, &d->top->sink)); - ) - VMCASE(OP_ENDSEQ, - CHECK_SUSPEND(upb_sink_endseq(&d->top->sink, arg)); - ) - VMCASE(OP_STARTSUBMSG, - upb_pbdecoder_frame *outer = outer_frame(d); - CHECK_SUSPEND(upb_sink_startsubmsg(&outer->sink, arg, &d->top->sink)); - ) - VMCASE(OP_ENDSUBMSG, - CHECK_SUSPEND(upb_sink_endsubmsg(&d->top->sink, arg)); - ) - VMCASE(OP_STARTSTR, - uint32_t len = delim_remaining(d); - upb_pbdecoder_frame *outer = outer_frame(d); - CHECK_SUSPEND(upb_sink_startstr(&outer->sink, arg, len, &d->top->sink)); - if (len == 0) { - d->pc++; /* Skip OP_STRING. */ - } - ) - VMCASE(OP_STRING, - uint32_t len = curbufleft(d); - size_t n = upb_sink_putstring(&d->top->sink, arg, d->ptr, len, handle); - if (n > len) { - if (n > delim_remaining(d)) { - seterr(d, "Tried to skip past end of string."); - return upb_pbdecoder_suspend(d); - } else { - int32_t ret = skip(d, n); - /* This shouldn't return DECODE_OK, because n > len. */ - UPB_ASSERT(ret >= 0); - return ret; - } - } - advance(d, n); - if (n < len || d->delim_end == NULL) { - /* We aren't finished with this string yet. */ - d->pc--; /* Repeat OP_STRING. */ - if (n > 0) checkpoint(d); - return upb_pbdecoder_suspend(d); - } - ) - VMCASE(OP_ENDSTR, - CHECK_SUSPEND(upb_sink_endstr(&d->top->sink, arg)); - ) - VMCASE(OP_PUSHTAGDELIM, - CHECK_SUSPEND(pushtagdelim(d, arg)); - ) - VMCASE(OP_SETBIGGROUPNUM, - d->top->groupnum = *d->pc++; - ) - VMCASE(OP_POP, - UPB_ASSERT(d->top > d->stack); - decoder_pop(d); - ) - VMCASE(OP_PUSHLENDELIM, - uint32_t len; - CHECK_RETURN(decode_v32(d, &len)); - CHECK_SUSPEND(decoder_push(d, offset(d) + len)); - set_delim_end(d); - ) - VMCASE(OP_SETDELIM, - set_delim_end(d); - ) - VMCASE(OP_CHECKDELIM, - /* We are guaranteed of this assert because we never allow ourselves to - * consume bytes beyond data_end, which covers delim_end when non-NULL. - */ - UPB_ASSERT(!(d->delim_end && d->ptr > d->delim_end)); - if (d->ptr == d->delim_end) - d->pc += longofs; - ) - VMCASE(OP_CALL, - d->callstack[d->call_len++] = d->pc; - d->pc += longofs; - ) - VMCASE(OP_RET, - UPB_ASSERT(d->call_len > 0); - d->pc = d->callstack[--d->call_len]; - ) - VMCASE(OP_BRANCH, - d->pc += longofs; - ) - VMCASE(OP_TAG1, - uint8_t expected; - CHECK_SUSPEND(curbufleft(d) > 0); - expected = (arg >> 8) & 0xff; - if (*d->ptr == expected) { - advance(d, 1); - } else { - int8_t shortofs; - badtag: - shortofs = arg; - if (shortofs == LABEL_DISPATCH) { - CHECK_RETURN(dispatch(d)); - } else { - d->pc += shortofs; - break; /* Avoid checkpoint(). */ - } - } - ) - VMCASE(OP_TAG2, - uint16_t expected; - CHECK_SUSPEND(curbufleft(d) > 0); - expected = (arg >> 8) & 0xffff; - if (curbufleft(d) >= 2) { - uint16_t actual; - memcpy(&actual, d->ptr, 2); - if (expected == actual) { - advance(d, 2); - } else { - goto badtag; - } - } else { - int32_t result = upb_pbdecoder_checktag_slow(d, expected); - if (result == DECODE_MISMATCH) goto badtag; - if (result >= 0) return result; - } - ) - VMCASE(OP_TAGN, { - uint64_t expected; - int32_t result; - memcpy(&expected, d->pc, 8); - d->pc += 2; - result = upb_pbdecoder_checktag_slow(d, expected); - if (result == DECODE_MISMATCH) goto badtag; - if (result >= 0) return result; - }) - VMCASE(OP_DISPATCH, { - CHECK_RETURN(dispatch(d)); - }) - VMCASE(OP_HALT, { - return d->size_param; - }) - } + return true; +} + +/* Call when "bytes" bytes have been writte at e->ptr. The caller *must* have + * previously called reserve() with at least this many bytes. */ +static void encoder_advance(upb_pb_encoder *e, size_t bytes) { + UPB_ASSERT((size_t)(e->limit - e->ptr) >= bytes); + e->ptr += bytes; +} + +/* Call when all of the bytes for a handler have been written. Flushes the + * bytes if possible and necessary, returning false if this failed. */ +static bool commit(upb_pb_encoder *e) { + if (!e->top) { + /* We aren't inside a delimited region. Flush our accumulated bytes to + * the output. + * + * TODO(haberman): in the future we may want to delay flushing for + * efficiency reasons. */ + putbuf(e, e->buf, e->ptr - e->buf); + e->ptr = e->buf; } -} + return true; +} -/* BytesHandler handlers ******************************************************/ +/* Writes the given bytes to the buffer, handling reserve/advance. */ +static bool encode_bytes(upb_pb_encoder *e, const void *data, size_t len) { + if (!reserve(e, len)) { + return false; + } -void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) { - upb_pbdecoder *d = closure; - UPB_UNUSED(size_hint); - d->top->end_ofs = UINT64_MAX; - d->bufstart_ofs = 0; - d->call_len = 1; - d->callstack[0] = &halt; - d->pc = pc; - d->skip = 0; - return d; + memcpy(e->ptr, data, len); + encoder_advance(e, len); + return true; } -void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) { - upb_pbdecoder *d = closure; - UPB_UNUSED(hd); - UPB_UNUSED(size_hint); - d->top->end_ofs = UINT64_MAX; - d->bufstart_ofs = 0; - d->call_len = 0; - d->skip = 0; - return d; +/* Finish the current run by adding the run totals to the segment and message + * length. */ +static void accumulate(upb_pb_encoder *e) { + size_t run_len; + UPB_ASSERT(e->ptr >= e->runbegin); + run_len = e->ptr - e->runbegin; + e->segptr->seglen += run_len; + top(e)->msglen += run_len; + e->runbegin = e->ptr; } -bool upb_pbdecoder_end(void *closure, const void *handler_data) { - upb_pbdecoder *d = closure; - const upb_pbdecodermethod *method = handler_data; - uint64_t end; - char dummy; +/* Call to indicate the start of delimited region for which the full length is + * not yet known. All data will be buffered until the length is known. + * Delimited regions may be nested; their lengths will all be tracked properly. */ +static bool start_delim(upb_pb_encoder *e) { + if (e->top) { + /* We are already buffering, advance to the next segment and push it on the + * stack. */ + accumulate(e); - if (d->residual_end > d->residual) { - seterr(d, "Unexpected EOF: decoder still has buffered unparsed data"); - return false; - } + if (++e->top == e->stacklimit) { + /* TODO(haberman): grow stack? */ + return false; + } - if (d->skip) { - seterr(d, "Unexpected EOF inside skipped data"); - return false; - } + if (++e->segptr == e->seglimit) { + /* Grow segment buffer. */ + size_t old_size = + (e->seglimit - e->segbuf) * sizeof(upb_pb_encoder_segment); + size_t new_size = old_size * 2; + upb_pb_encoder_segment *new_buf = + upb_arena_realloc(e->arena, e->segbuf, old_size, new_size); - if (d->top->end_ofs != UINT64_MAX) { - seterr(d, "Unexpected EOF inside delimited string"); - return false; + if (new_buf == NULL) { + return false; + } + + e->segptr = new_buf + (e->segptr - e->segbuf); + e->seglimit = new_buf + (new_size / sizeof(upb_pb_encoder_segment)); + e->segbuf = new_buf; + } + } else { + /* We were previously at the top level, start buffering. */ + e->segptr = e->segbuf; + e->top = e->stack; + e->runbegin = e->ptr; } - /* The user's end() call indicates that the message ends here. */ - end = offset(d); - d->top->end_ofs = end; + *e->top = e->segptr - e->segbuf; + e->segptr->seglen = 0; + e->segptr->msglen = 0; -#ifdef UPB_USE_JIT_X64 - if (method->is_native_) { - const mgroup *group = (const mgroup*)method->group; - if (d->top != d->stack) - d->stack->end_ofs = 0; - group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL); - } else -#endif - { - const uint32_t *p = d->pc; - d->stack->end_ofs = end; - /* Check the previous bytecode, but guard against beginning. */ - if (p != method->code_base.ptr) p--; - if (getop(*p) == OP_CHECKDELIM) { - /* Rewind from OP_TAG* to OP_CHECKDELIM. */ - UPB_ASSERT(getop(*d->pc) == OP_TAG1 || - getop(*d->pc) == OP_TAG2 || - getop(*d->pc) == OP_TAGN || - getop(*d->pc) == OP_DISPATCH); - d->pc = p; + return true; +} + +/* Call to indicate the end of a delimited region. We now know the length of + * the delimited region. If we are not nested inside any other delimited + * regions, we can now emit all of the buffered data we accumulated. */ +static bool end_delim(upb_pb_encoder *e) { + size_t msglen; + accumulate(e); + msglen = top(e)->msglen; + + if (e->top == e->stack) { + /* All lengths are now available, emit all buffered data. */ + char buf[UPB_PB_VARINT_MAX_LEN]; + upb_pb_encoder_segment *s; + const char *ptr = e->buf; + for (s = e->segbuf; s <= e->segptr; s++) { + size_t lenbytes = upb_vencode64(s->msglen, buf); + putbuf(e, buf, lenbytes); + putbuf(e, ptr, s->seglen); + ptr += s->seglen; } - upb_pbdecoder_decode(closure, handler_data, &dummy, 0, NULL); + + e->ptr = e->buf; + e->top = NULL; + } else { + /* Need to keep buffering; propagate length info into enclosing + * submessages. */ + --e->top; + top(e)->msglen += msglen + upb_varint_size(msglen); } - if (d->call_len != 0) { - seterr(d, "Unexpected EOF inside submessage or group"); + return true; +} + + +/* tag_t **********************************************************************/ + +/* A precomputed (pre-encoded) tag and length. */ + +typedef struct { + uint8_t bytes; + char tag[7]; +} tag_t; + +/* Allocates a new tag for this field, and sets it in these handlerattr. */ +static void new_tag(upb_handlers *h, const upb_fielddef *f, upb_wiretype_t wt, + upb_handlerattr *attr) { + uint32_t n = upb_fielddef_number(f); + + tag_t *tag = upb_gmalloc(sizeof(tag_t)); + tag->bytes = upb_vencode64((n << 3) | wt, tag->tag); + + attr->handler_data = tag; + upb_handlers_addcleanup(h, tag, upb_gfree); +} + +static bool encode_tag(upb_pb_encoder *e, const tag_t *tag) { + return encode_bytes(e, tag->tag, tag->bytes); +} + + +/* encoding of wire types *****************************************************/ + +static bool encode_fixed64(upb_pb_encoder *e, uint64_t val) { + /* TODO(haberman): byte-swap for big endian. */ + return encode_bytes(e, &val, sizeof(uint64_t)); +} + +static bool encode_fixed32(upb_pb_encoder *e, uint32_t val) { + /* TODO(haberman): byte-swap for big endian. */ + return encode_bytes(e, &val, sizeof(uint32_t)); +} + +static bool encode_varint(upb_pb_encoder *e, uint64_t val) { + if (!reserve(e, UPB_PB_VARINT_MAX_LEN)) { return false; } + encoder_advance(e, upb_vencode64(val, e->ptr)); return true; } -size_t upb_pbdecoder_decode(void *decoder, const void *group, const char *buf, - size_t size, const upb_bufhandle *handle) { - int32_t result = upb_pbdecoder_resume(decoder, NULL, buf, size, handle); +static uint64_t dbl2uint64(double d) { + uint64_t ret; + memcpy(&ret, &d, sizeof(uint64_t)); + return ret; +} - if (result == DECODE_ENDGROUP) goto_endmsg(decoder); - CHECK_RETURN(result); +static uint32_t flt2uint32(float d) { + uint32_t ret; + memcpy(&ret, &d, sizeof(uint32_t)); + return ret; +} - return run_decoder_vm(decoder, group, handle); + +/* encoding of proto types ****************************************************/ + +static bool startmsg(void *c, const void *hd) { + upb_pb_encoder *e = c; + UPB_UNUSED(hd); + if (e->depth++ == 0) { + upb_bytessink_start(e->output_, 0, &e->subc); + } + return true; +} + +static bool endmsg(void *c, const void *hd, upb_status *status) { + upb_pb_encoder *e = c; + UPB_UNUSED(hd); + UPB_UNUSED(status); + if (--e->depth == 0) { + upb_bytessink_end(e->output_); + } + return true; +} + +static void *encode_startdelimfield(void *c, const void *hd) { + bool ok = encode_tag(c, hd) && commit(c) && start_delim(c); + return ok ? c : UPB_BREAK; +} + +static bool encode_unknown(void *c, const void *hd, const char *buf, + size_t len) { + UPB_UNUSED(hd); + return encode_bytes(c, buf, len) && commit(c); +} + +static bool encode_enddelimfield(void *c, const void *hd) { + UPB_UNUSED(hd); + return end_delim(c); +} + +static void *encode_startgroup(void *c, const void *hd) { + return (encode_tag(c, hd) && commit(c)) ? c : UPB_BREAK; } +static bool encode_endgroup(void *c, const void *hd) { + return encode_tag(c, hd) && commit(c); +} -/* Public API *****************************************************************/ +static void *encode_startstr(void *c, const void *hd, size_t size_hint) { + UPB_UNUSED(size_hint); + return encode_startdelimfield(c, hd); +} -void upb_pbdecoder_reset(upb_pbdecoder *d) { - d->top = d->stack; - d->top->groupnum = 0; - d->ptr = d->residual; - d->buf = d->residual; - d->end = d->residual; - d->residual_end = d->residual; +static size_t encode_strbuf(void *c, const void *hd, const char *buf, + size_t len, const upb_bufhandle *h) { + UPB_UNUSED(hd); + UPB_UNUSED(h); + return encode_bytes(c, buf, len) ? len : 0; } -upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, - upb_sink *sink) { - const size_t default_max_nesting = 64; -#ifndef NDEBUG - size_t size_before = upb_env_bytesallocated(e); -#endif +#define T(type, ctype, convert, encode) \ + static bool encode_scalar_##type(void *e, const void *hd, ctype val) { \ + return encode_tag(e, hd) && encode(e, (convert)(val)) && commit(e); \ + } \ + static bool encode_packed_##type(void *e, const void *hd, ctype val) { \ + UPB_UNUSED(hd); \ + return encode(e, (convert)(val)); \ + } - upb_pbdecoder *d = upb_env_malloc(e, sizeof(upb_pbdecoder)); - if (!d) return NULL; +T(double, double, dbl2uint64, encode_fixed64) +T(float, float, flt2uint32, encode_fixed32) +T(int64, int64_t, uint64_t, encode_varint) +T(int32, int32_t, int64_t, encode_varint) +T(fixed64, uint64_t, uint64_t, encode_fixed64) +T(fixed32, uint32_t, uint32_t, encode_fixed32) +T(bool, bool, bool, encode_varint) +T(uint32, uint32_t, uint32_t, encode_varint) +T(uint64, uint64_t, uint64_t, encode_varint) +T(enum, int32_t, uint32_t, encode_varint) +T(sfixed32, int32_t, uint32_t, encode_fixed32) +T(sfixed64, int64_t, uint64_t, encode_fixed64) +T(sint32, int32_t, upb_zzenc_32, encode_varint) +T(sint64, int64_t, upb_zzenc_64, encode_varint) - d->method_ = m; - d->callstack = upb_env_malloc(e, callstacksize(d, default_max_nesting)); - d->stack = upb_env_malloc(e, stacksize(d, default_max_nesting)); - if (!d->stack || !d->callstack) { - return NULL; - } +#undef T - d->env = e; - d->limit = d->stack + default_max_nesting - 1; - d->stack_size = default_max_nesting; - d->status = NULL; - upb_pbdecoder_reset(d); - upb_bytessink_reset(&d->input_, &m->input_handler_, d); +/* code to build the handlers *************************************************/ - UPB_ASSERT(sink); - if (d->method_->dest_handlers_) { - if (sink->handlers != d->method_->dest_handlers_) - return NULL; - } - upb_sink_reset(&d->top->sink, sink->handlers, sink->closure); +#include +static void newhandlers_callback(const void *closure, upb_handlers *h) { + const upb_msgdef *m; + upb_msg_field_iter i; - /* If this fails, increase the value in decoder.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= - UPB_PB_DECODER_SIZE); - return d; -} + UPB_UNUSED(closure); -uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d) { - return offset(d); -} + upb_handlers_setstartmsg(h, startmsg, NULL); + upb_handlers_setendmsg(h, endmsg, NULL); + upb_handlers_setunknown(h, encode_unknown, NULL); -const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) { - return d->method_; -} + m = upb_handlers_msgdef(h); + for(upb_msg_field_begin(&i, m); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + const upb_fielddef *f = upb_msg_iter_field(&i); + bool packed = upb_fielddef_isseq(f) && upb_fielddef_isprimitive(f) && + upb_fielddef_packed(f); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + upb_wiretype_t wt = + packed ? UPB_WIRE_TYPE_DELIMITED + : upb_pb_native_wire_types[upb_fielddef_descriptortype(f)]; -upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d) { - return &d->input_; -} + /* Pre-encode the tag for this field. */ + new_tag(h, f, wt, &attr); -size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) { - return d->stack_size; -} + if (packed) { + upb_handlers_setstartseq(h, f, encode_startdelimfield, &attr); + upb_handlers_setendseq(h, f, encode_enddelimfield, &attr); + } -bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) { - UPB_ASSERT(d->top >= d->stack); +#define T(upper, lower, upbtype) \ + case UPB_DESCRIPTOR_TYPE_##upper: \ + if (packed) { \ + upb_handlers_set##upbtype(h, f, encode_packed_##lower, &attr); \ + } else { \ + upb_handlers_set##upbtype(h, f, encode_scalar_##lower, &attr); \ + } \ + break; - if (max < (size_t)(d->top - d->stack)) { - /* Can't set a limit smaller than what we are currently at. */ - return false; - } + switch (upb_fielddef_descriptortype(f)) { + T(DOUBLE, double, double); + T(FLOAT, float, float); + T(INT64, int64, int64); + T(INT32, int32, int32); + T(FIXED64, fixed64, uint64); + T(FIXED32, fixed32, uint32); + T(BOOL, bool, bool); + T(UINT32, uint32, uint32); + T(UINT64, uint64, uint64); + T(ENUM, enum, int32); + T(SFIXED32, sfixed32, int32); + T(SFIXED64, sfixed64, int64); + T(SINT32, sint32, int32); + T(SINT64, sint64, int64); + case UPB_DESCRIPTOR_TYPE_STRING: + case UPB_DESCRIPTOR_TYPE_BYTES: + upb_handlers_setstartstr(h, f, encode_startstr, &attr); + upb_handlers_setendstr(h, f, encode_enddelimfield, &attr); + upb_handlers_setstring(h, f, encode_strbuf, &attr); + break; + case UPB_DESCRIPTOR_TYPE_MESSAGE: + upb_handlers_setstartsubmsg(h, f, encode_startdelimfield, &attr); + upb_handlers_setendsubmsg(h, f, encode_enddelimfield, &attr); + break; + case UPB_DESCRIPTOR_TYPE_GROUP: { + /* Endgroup takes a different tag (wire_type = END_GROUP). */ + upb_handlerattr attr2 = UPB_HANDLERATTR_INIT; + new_tag(h, f, UPB_WIRE_TYPE_END_GROUP, &attr2); - if (max > d->stack_size) { - /* Need to reallocate stack and callstack to accommodate. */ - size_t old_size = stacksize(d, d->stack_size); - size_t new_size = stacksize(d, max); - void *p = upb_env_realloc(d->env, d->stack, old_size, new_size); - if (!p) { - return false; - } - d->stack = p; + upb_handlers_setstartsubmsg(h, f, encode_startgroup, &attr); + upb_handlers_setendsubmsg(h, f, encode_endgroup, &attr2); - old_size = callstacksize(d, d->stack_size); - new_size = callstacksize(d, max); - p = upb_env_realloc(d->env, d->callstack, old_size, new_size); - if (!p) { - return false; + break; + } } - d->callstack = p; - d->stack_size = max; +#undef T } +} - d->limit = d->stack + max - 1; - return true; +void upb_pb_encoder_reset(upb_pb_encoder *e) { + e->segptr = NULL; + e->top = NULL; + e->depth = 0; } -/* -** upb::Encoder -** -** Since we are implementing pure handlers (ie. without any out-of-band access -** to pre-computed lengths), we have to buffer all submessages before we can -** emit even their first byte. -** -** Not knowing the size of submessages also means we can't write a perfect -** zero-copy implementation, even with buffering. Lengths are stored as -** varints, which means that we don't know how many bytes to reserve for the -** length until we know what the length is. -** -** This leaves us with three main choices: -** -** 1. buffer all submessage data in a temporary buffer, then copy it exactly -** once into the output buffer. -** -** 2. attempt to buffer data directly into the output buffer, estimating how -** many bytes each length will take. When our guesses are wrong, use -** memmove() to grow or shrink the allotted space. -** -** 3. buffer directly into the output buffer, allocating a max length -** ahead-of-time for each submessage length. If we overallocated, we waste -** space, but no memcpy() or memmove() is required. This approach requires -** defining a maximum size for submessages and rejecting submessages that -** exceed that size. -** -** (2) and (3) have the potential to have better performance, but they are more -** complicated and subtle to implement: -** -** (3) requires making an arbitrary choice of the maximum message size; it -** wastes space when submessages are shorter than this and fails -** completely when they are longer. This makes it more finicky and -** requires configuration based on the input. It also makes it impossible -** to perfectly match the output of reference encoders that always use the -** optimal amount of space for each length. -** -** (2) requires guessing the the size upfront, and if multiple lengths are -** guessed wrong the minimum required number of memmove() operations may -** be complicated to compute correctly. Implemented properly, it may have -** a useful amortized or average cost, but more investigation is required -** to determine this and what the optimal algorithm is to achieve it. -** -** (1) makes you always pay for exactly one copy, but its implementation is -** the simplest and its performance is predictable. -** -** So for now, we implement (1) only. If we wish to optimize later, we should -** be able to do it without affecting users. -** -** The strategy is to buffer the segments of data that do *not* depend on -** unknown lengths in one buffer, and keep a separate buffer of segment pointers -** and lengths. When the top-level submessage ends, we can go beginning to end, -** alternating the writing of lengths with memcpy() of the rest of the data. -** At the top level though, no buffering is required. -*/ +/* public API *****************************************************************/ -/* The output buffer is divided into segments; a segment is a string of data - * that is "ready to go" -- it does not need any varint lengths inserted into - * the middle. The seams between segments are where varints will be inserted - * once they are known. - * - * We also use the concept of a "run", which is a range of encoded bytes that - * occur at a single submessage level. Every segment contains one or more runs. - * - * A segment can span messages. Consider: - * - * .--Submessage lengths---------. - * | | | - * | V V - * V | |--------------- | |----------------- - * Submessages: | |----------------------------------------------- - * Top-level msg: ------------------------------------------------------------ - * - * Segments: ----- ------------------- ----------------- - * Runs: *---- *--------------*--- *---------------- - * (* marks the start) - * - * Note that the top-level menssage is not in any segment because it does not - * have any length preceding it. - * - * A segment is only interrupted when another length needs to be inserted. So - * observe how the second segment spans both the inner submessage and part of - * the next enclosing message. */ -typedef struct { - uint32_t msglen; /* The length to varint-encode before this segment. */ - uint32_t seglen; /* Length of the segment. */ -} upb_pb_encoder_segment; +upb_handlercache *upb_pb_encoder_newcache() { + return upb_handlercache_new(newhandlers_callback, NULL); +} + +upb_pb_encoder *upb_pb_encoder_create(upb_arena *arena, const upb_handlers *h, + upb_bytessink output) { + const size_t initial_bufsize = 256; + const size_t initial_segbufsize = 16; + /* TODO(haberman): make this configurable. */ + const size_t stack_size = 64; +#ifndef NDEBUG + const size_t size_before = upb_arena_bytesallocated(arena); +#endif -struct upb_pb_encoder { - upb_env *env; + upb_pb_encoder *e = upb_arena_malloc(arena, sizeof(upb_pb_encoder)); + if (!e) return NULL; - /* Our input and output. */ - upb_sink input_; - upb_bytessink *output_; + e->buf = upb_arena_malloc(arena, initial_bufsize); + e->segbuf = upb_arena_malloc(arena, initial_segbufsize * sizeof(*e->segbuf)); + e->stack = upb_arena_malloc(arena, stack_size * sizeof(*e->stack)); - /* The "subclosure" -- used as the inner closure as part of the bytessink - * protocol. */ - void *subc; + if (!e->buf || !e->segbuf || !e->stack) { + return NULL; + } - /* The output buffer and limit, and our current write position. "buf" - * initially points to "initbuf", but is dynamically allocated if we need to - * grow beyond the initial size. */ - char *buf, *ptr, *limit; + e->limit = e->buf + initial_bufsize; + e->seglimit = e->segbuf + initial_segbufsize; + e->stacklimit = e->stack + stack_size; - /* The beginning of the current run, or undefined if we are at the top - * level. */ - char *runbegin; + upb_pb_encoder_reset(e); + upb_sink_reset(&e->input_, h, e); - /* The list of segments we are accumulating. */ - upb_pb_encoder_segment *segbuf, *segptr, *seglimit; + e->arena = arena; + e->output_ = output; + e->subc = output.closure; + e->ptr = e->buf; - /* The stack of enclosing submessages. Each entry in the stack points to the - * segment where this submessage's length is being accumulated. */ - int *stack, *top, *stacklimit; + /* If this fails, increase the value in encoder.h. */ + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <= + UPB_PB_ENCODER_SIZE); + return e; +} - /* Depth of startmsg/endmsg calls. */ - int depth; -}; +upb_sink upb_pb_encoder_input(upb_pb_encoder *e) { return e->input_; } +/* + * upb::pb::TextPrinter + * + * OPT: This is not optimized at all. It uses printf() which parses the format + * string every time, and it allocates memory for every put. + */ -/* low-level buffering ********************************************************/ -/* Low-level functions for interacting with the output buffer. */ +#include +#include +#include +#include +#include +#include -/* TODO(haberman): handle pushback */ -static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) { - size_t n = upb_bytessink_putbuf(e->output_, e->subc, buf, len, NULL); - UPB_ASSERT(n == len); + +struct upb_textprinter { + upb_sink input_; + upb_bytessink output_; + int indent_depth_; + bool single_line_; + void *subc; +}; + +#define CHECK(x) if ((x) < 0) goto err; + +static const char *shortname(const char *longname) { + const char *last = strrchr(longname, '.'); + return last ? last + 1 : longname; } -static upb_pb_encoder_segment *top(upb_pb_encoder *e) { - return &e->segbuf[*e->top]; +static int indent(upb_textprinter *p) { + int i; + if (!p->single_line_) + for (i = 0; i < p->indent_depth_; i++) + upb_bytessink_putbuf(p->output_, p->subc, " ", 2, NULL); + return 0; } -/* Call to ensure that at least "bytes" bytes are available for writing at - * e->ptr. Returns false if the bytes could not be allocated. */ -static bool reserve(upb_pb_encoder *e, size_t bytes) { - if ((size_t)(e->limit - e->ptr) < bytes) { - /* Grow buffer. */ - char *new_buf; - size_t needed = bytes + (e->ptr - e->buf); - size_t old_size = e->limit - e->buf; +static int endfield(upb_textprinter *p) { + const char ch = (p->single_line_ ? ' ' : '\n'); + upb_bytessink_putbuf(p->output_, p->subc, &ch, 1, NULL); + return 0; +} - size_t new_size = old_size; +static int putescaped(upb_textprinter *p, const char *buf, size_t len, + bool preserve_utf8) { + /* Based on CEscapeInternal() from Google's protobuf release. */ + char dstbuf[4096], *dst = dstbuf, *dstend = dstbuf + sizeof(dstbuf); + const char *end = buf + len; - while (new_size < needed) { - new_size *= 2; - } + /* I think hex is prettier and more useful, but proto2 uses octal; should + * investigate whether it can parse hex also. */ + const bool use_hex = false; + bool last_hex_escape = false; /* true if last output char was \xNN */ - new_buf = upb_env_realloc(e->env, e->buf, old_size, new_size); + for (; buf < end; buf++) { + bool is_hex_escape; - if (new_buf == NULL) { - return false; + if (dstend - dst < 4) { + upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); + dst = dstbuf; } - e->ptr = new_buf + (e->ptr - e->buf); - e->runbegin = new_buf + (e->runbegin - e->buf); - e->limit = new_buf + new_size; - e->buf = new_buf; + is_hex_escape = false; + switch (*buf) { + case '\n': *(dst++) = '\\'; *(dst++) = 'n'; break; + case '\r': *(dst++) = '\\'; *(dst++) = 'r'; break; + case '\t': *(dst++) = '\\'; *(dst++) = 't'; break; + case '\"': *(dst++) = '\\'; *(dst++) = '\"'; break; + case '\'': *(dst++) = '\\'; *(dst++) = '\''; break; + case '\\': *(dst++) = '\\'; *(dst++) = '\\'; break; + default: + /* Note that if we emit \xNN and the buf 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 ((!preserve_utf8 || (uint8_t)*buf < 0x80) && + (!isprint(*buf) || (last_hex_escape && isxdigit(*buf)))) { + sprintf(dst, (use_hex ? "\\x%02x" : "\\%03o"), (uint8_t)*buf); + is_hex_escape = use_hex; + dst += 4; + } else { + *(dst++) = *buf; break; + } + } + last_hex_escape = is_hex_escape; } - - return true; -} - -/* Call when "bytes" bytes have been writte at e->ptr. The caller *must* have - * previously called reserve() with at least this many bytes. */ -static void encoder_advance(upb_pb_encoder *e, size_t bytes) { - UPB_ASSERT((size_t)(e->limit - e->ptr) >= bytes); - e->ptr += bytes; + /* Flush remaining data. */ + upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); + return 0; } -/* Call when all of the bytes for a handler have been written. Flushes the - * bytes if possible and necessary, returning false if this failed. */ -static bool commit(upb_pb_encoder *e) { - if (!e->top) { - /* We aren't inside a delimited region. Flush our accumulated bytes to - * the output. - * - * TODO(haberman): in the future we may want to delay flushing for - * efficiency reasons. */ - putbuf(e, e->buf, e->ptr - e->buf); - e->ptr = e->buf; - } +bool putf(upb_textprinter *p, const char *fmt, ...) { + va_list args; + va_list args_copy; + char *str; + int written; + int len; + bool ok; - return true; -} + va_start(args, fmt); -/* Writes the given bytes to the buffer, handling reserve/advance. */ -static bool encode_bytes(upb_pb_encoder *e, const void *data, size_t len) { - if (!reserve(e, len)) { - return false; - } + /* Run once to get the length of the string. */ + _upb_va_copy(args_copy, args); + len = _upb_vsnprintf(NULL, 0, fmt, args_copy); + va_end(args_copy); - memcpy(e->ptr, data, len); - encoder_advance(e, len); - return true; -} + /* + 1 for NULL terminator (vsprintf() requires it even if we don't). */ + str = upb_gmalloc(len + 1); + if (!str) return false; + written = vsprintf(str, fmt, args); + va_end(args); + UPB_ASSERT(written == len); -/* Finish the current run by adding the run totals to the segment and message - * length. */ -static void accumulate(upb_pb_encoder *e) { - size_t run_len; - UPB_ASSERT(e->ptr >= e->runbegin); - run_len = e->ptr - e->runbegin; - e->segptr->seglen += run_len; - top(e)->msglen += run_len; - e->runbegin = e->ptr; + ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL); + upb_gfree(str); + return ok; } -/* Call to indicate the start of delimited region for which the full length is - * not yet known. All data will be buffered until the length is known. - * Delimited regions may be nested; their lengths will all be tracked properly. */ -static bool start_delim(upb_pb_encoder *e) { - if (e->top) { - /* We are already buffering, advance to the next segment and push it on the - * stack. */ - accumulate(e); - - if (++e->top == e->stacklimit) { - /* TODO(haberman): grow stack? */ - return false; - } - if (++e->segptr == e->seglimit) { - /* Grow segment buffer. */ - size_t old_size = - (e->seglimit - e->segbuf) * sizeof(upb_pb_encoder_segment); - size_t new_size = old_size * 2; - upb_pb_encoder_segment *new_buf = - upb_env_realloc(e->env, e->segbuf, old_size, new_size); +/* handlers *******************************************************************/ - if (new_buf == NULL) { - return false; - } +static bool textprinter_startmsg(void *c, const void *hd) { + upb_textprinter *p = c; + UPB_UNUSED(hd); + if (p->indent_depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc); + } + return true; +} - e->segptr = new_buf + (e->segptr - e->segbuf); - e->seglimit = new_buf + (new_size / sizeof(upb_pb_encoder_segment)); - e->segbuf = new_buf; - } - } else { - /* We were previously at the top level, start buffering. */ - e->segptr = e->segbuf; - e->top = e->stack; - e->runbegin = e->ptr; +static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) { + upb_textprinter *p = c; + UPB_UNUSED(hd); + UPB_UNUSED(s); + if (p->indent_depth_ == 0) { + upb_bytessink_end(p->output_); } + return true; +} - *e->top = e->segptr - e->segbuf; - e->segptr->seglen = 0; - e->segptr->msglen = 0; +#define TYPE(name, ctype, fmt) \ + static bool textprinter_put ## name(void *closure, const void *handler_data, \ + ctype val) { \ + upb_textprinter *p = closure; \ + const upb_fielddef *f = handler_data; \ + CHECK(indent(p)); \ + putf(p, "%s: " fmt, upb_fielddef_name(f), val); \ + CHECK(endfield(p)); \ + return true; \ + err: \ + return false; \ +} +static bool textprinter_putbool(void *closure, const void *handler_data, + bool val) { + upb_textprinter *p = closure; + const upb_fielddef *f = handler_data; + CHECK(indent(p)); + putf(p, "%s: %s", upb_fielddef_name(f), val ? "true" : "false"); + CHECK(endfield(p)); return true; +err: + return false; } -/* Call to indicate the end of a delimited region. We now know the length of - * the delimited region. If we are not nested inside any other delimited - * regions, we can now emit all of the buffered data we accumulated. */ -static bool end_delim(upb_pb_encoder *e) { - size_t msglen; - accumulate(e); - msglen = top(e)->msglen; +#define STRINGIFY_HELPER(x) #x +#define STRINGIFY_MACROVAL(x) STRINGIFY_HELPER(x) - if (e->top == e->stack) { - /* All lengths are now available, emit all buffered data. */ - char buf[UPB_PB_VARINT_MAX_LEN]; - upb_pb_encoder_segment *s; - const char *ptr = e->buf; - for (s = e->segbuf; s <= e->segptr; s++) { - size_t lenbytes = upb_vencode64(s->msglen, buf); - putbuf(e, buf, lenbytes); - putbuf(e, ptr, s->seglen); - ptr += s->seglen; - } +TYPE(int32, int32_t, "%" PRId32) +TYPE(int64, int64_t, "%" PRId64) +TYPE(uint32, uint32_t, "%" PRIu32) +TYPE(uint64, uint64_t, "%" PRIu64) +TYPE(float, float, "%." STRINGIFY_MACROVAL(FLT_DIG) "g") +TYPE(double, double, "%." STRINGIFY_MACROVAL(DBL_DIG) "g") - e->ptr = e->buf; - e->top = NULL; +#undef TYPE + +/* Output a symbolic value from the enum if found, else just print as int32. */ +static bool textprinter_putenum(void *closure, const void *handler_data, + int32_t val) { + upb_textprinter *p = closure; + const upb_fielddef *f = handler_data; + const upb_enumdef *enum_def = upb_fielddef_enumsubdef(f); + const char *label = upb_enumdef_iton(enum_def, val); + if (label) { + indent(p); + putf(p, "%s: %s", upb_fielddef_name(f), label); + endfield(p); } else { - /* Need to keep buffering; propagate length info into enclosing - * submessages. */ - --e->top; - top(e)->msglen += msglen + upb_varint_size(msglen); + if (!textprinter_putint32(closure, handler_data, val)) + return false; } - return true; } +static void *textprinter_startstr(void *closure, const void *handler_data, + size_t size_hint) { + upb_textprinter *p = closure; + const upb_fielddef *f = handler_data; + UPB_UNUSED(size_hint); + indent(p); + putf(p, "%s: \"", upb_fielddef_name(f)); + return p; +} -/* tag_t **********************************************************************/ - -/* A precomputed (pre-encoded) tag and length. */ - -typedef struct { - uint8_t bytes; - char tag[7]; -} tag_t; - -/* Allocates a new tag for this field, and sets it in these handlerattr. */ -static void new_tag(upb_handlers *h, const upb_fielddef *f, upb_wiretype_t wt, - upb_handlerattr *attr) { - uint32_t n = upb_fielddef_number(f); +static bool textprinter_endstr(void *closure, const void *handler_data) { + upb_textprinter *p = closure; + UPB_UNUSED(handler_data); + putf(p, "\""); + endfield(p); + return true; +} - tag_t *tag = upb_gmalloc(sizeof(tag_t)); - tag->bytes = upb_vencode64((n << 3) | wt, tag->tag); +static size_t textprinter_putstr(void *closure, const void *hd, const char *buf, + size_t len, const upb_bufhandle *handle) { + upb_textprinter *p = closure; + const upb_fielddef *f = hd; + UPB_UNUSED(handle); + CHECK(putescaped(p, buf, len, upb_fielddef_type(f) == UPB_TYPE_STRING)); + return len; +err: + return 0; +} - upb_handlerattr_init(attr); - upb_handlerattr_sethandlerdata(attr, tag); - upb_handlers_addcleanup(h, tag, upb_gfree); +static void *textprinter_startsubmsg(void *closure, const void *handler_data) { + upb_textprinter *p = closure; + const char *name = handler_data; + CHECK(indent(p)); + putf(p, "%s {%c", name, p->single_line_ ? ' ' : '\n'); + p->indent_depth_++; + return p; +err: + return UPB_BREAK; } -static bool encode_tag(upb_pb_encoder *e, const tag_t *tag) { - return encode_bytes(e, tag->tag, tag->bytes); +static bool textprinter_endsubmsg(void *closure, const void *handler_data) { + upb_textprinter *p = closure; + UPB_UNUSED(handler_data); + p->indent_depth_--; + CHECK(indent(p)); + upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL); + CHECK(endfield(p)); + return true; +err: + return false; } +static void onmreg(const void *c, upb_handlers *h) { + const upb_msgdef *m = upb_handlers_msgdef(h); + upb_msg_field_iter i; + UPB_UNUSED(c); -/* encoding of wire types *****************************************************/ + upb_handlers_setstartmsg(h, textprinter_startmsg, NULL); + upb_handlers_setendmsg(h, textprinter_endmsg, NULL); -static bool encode_fixed64(upb_pb_encoder *e, uint64_t val) { - /* TODO(haberman): byte-swap for big endian. */ - return encode_bytes(e, &val, sizeof(uint64_t)); + for(upb_msg_field_begin(&i, m); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + upb_fielddef *f = upb_msg_iter_field(&i); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + attr.handler_data = f; + switch (upb_fielddef_type(f)) { + case UPB_TYPE_INT32: + upb_handlers_setint32(h, f, textprinter_putint32, &attr); + break; + case UPB_TYPE_INT64: + upb_handlers_setint64(h, f, textprinter_putint64, &attr); + break; + case UPB_TYPE_UINT32: + upb_handlers_setuint32(h, f, textprinter_putuint32, &attr); + break; + case UPB_TYPE_UINT64: + upb_handlers_setuint64(h, f, textprinter_putuint64, &attr); + break; + case UPB_TYPE_FLOAT: + upb_handlers_setfloat(h, f, textprinter_putfloat, &attr); + break; + case UPB_TYPE_DOUBLE: + upb_handlers_setdouble(h, f, textprinter_putdouble, &attr); + break; + case UPB_TYPE_BOOL: + upb_handlers_setbool(h, f, textprinter_putbool, &attr); + break; + case UPB_TYPE_STRING: + case UPB_TYPE_BYTES: + upb_handlers_setstartstr(h, f, textprinter_startstr, &attr); + upb_handlers_setstring(h, f, textprinter_putstr, &attr); + upb_handlers_setendstr(h, f, textprinter_endstr, &attr); + break; + case UPB_TYPE_MESSAGE: { + const char *name = + upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_GROUP + ? shortname(upb_msgdef_fullname(upb_fielddef_msgsubdef(f))) + : upb_fielddef_name(f); + attr.handler_data = name; + upb_handlers_setstartsubmsg(h, f, textprinter_startsubmsg, &attr); + upb_handlers_setendsubmsg(h, f, textprinter_endsubmsg, &attr); + break; + } + case UPB_TYPE_ENUM: + upb_handlers_setint32(h, f, textprinter_putenum, &attr); + break; + } + } } -static bool encode_fixed32(upb_pb_encoder *e, uint32_t val) { - /* TODO(haberman): byte-swap for big endian. */ - return encode_bytes(e, &val, sizeof(uint32_t)); +static void textprinter_reset(upb_textprinter *p, bool single_line) { + p->single_line_ = single_line; + p->indent_depth_ = 0; } -static bool encode_varint(upb_pb_encoder *e, uint64_t val) { - if (!reserve(e, UPB_PB_VARINT_MAX_LEN)) { - return false; - } - encoder_advance(e, upb_vencode64(val, e->ptr)); - return true; -} +/* Public API *****************************************************************/ -static uint64_t dbl2uint64(double d) { - uint64_t ret; - memcpy(&ret, &d, sizeof(uint64_t)); - return ret; -} +upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h, + upb_bytessink output) { + upb_textprinter *p = upb_arena_malloc(arena, sizeof(upb_textprinter)); + if (!p) return NULL; -static uint32_t flt2uint32(float d) { - uint32_t ret; - memcpy(&ret, &d, sizeof(uint32_t)); - return ret; + p->output_ = output; + upb_sink_reset(&p->input_, h, p); + textprinter_reset(p, false); + + return p; } +upb_handlercache *upb_textprinter_newcache() { + return upb_handlercache_new(&onmreg, NULL); +} -/* encoding of proto types ****************************************************/ +upb_sink upb_textprinter_input(upb_textprinter *p) { return p->input_; } -static bool startmsg(void *c, const void *hd) { - upb_pb_encoder *e = c; - UPB_UNUSED(hd); - if (e->depth++ == 0) { - upb_bytessink_start(e->output_, 0, &e->subc); - } - return true; +void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line) { + p->single_line_ = single_line; } -static bool endmsg(void *c, const void *hd, upb_status *status) { - upb_pb_encoder *e = c; - UPB_UNUSED(hd); - UPB_UNUSED(status); - if (--e->depth == 0) { - upb_bytessink_end(e->output_); - } - return true; -} -static void *encode_startdelimfield(void *c, const void *hd) { - bool ok = encode_tag(c, hd) && commit(c) && start_delim(c); - return ok ? c : UPB_BREAK; -} +/* Index is descriptor type. */ +const uint8_t upb_pb_native_wire_types[] = { + UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */ + UPB_WIRE_TYPE_64BIT, /* DOUBLE */ + UPB_WIRE_TYPE_32BIT, /* FLOAT */ + UPB_WIRE_TYPE_VARINT, /* INT64 */ + UPB_WIRE_TYPE_VARINT, /* UINT64 */ + UPB_WIRE_TYPE_VARINT, /* INT32 */ + UPB_WIRE_TYPE_64BIT, /* FIXED64 */ + UPB_WIRE_TYPE_32BIT, /* FIXED32 */ + UPB_WIRE_TYPE_VARINT, /* BOOL */ + UPB_WIRE_TYPE_DELIMITED, /* STRING */ + UPB_WIRE_TYPE_START_GROUP, /* GROUP */ + UPB_WIRE_TYPE_DELIMITED, /* MESSAGE */ + UPB_WIRE_TYPE_DELIMITED, /* BYTES */ + UPB_WIRE_TYPE_VARINT, /* UINT32 */ + UPB_WIRE_TYPE_VARINT, /* ENUM */ + UPB_WIRE_TYPE_32BIT, /* SFIXED32 */ + UPB_WIRE_TYPE_64BIT, /* SFIXED64 */ + UPB_WIRE_TYPE_VARINT, /* SINT32 */ + UPB_WIRE_TYPE_VARINT, /* SINT64 */ +}; + +/* A basic branch-based decoder, uses 32-bit values to get good performance + * on 32-bit architectures (but performs well on 64-bits also). + * This scheme comes from the original Google Protobuf implementation + * (proto2). */ +upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r) { + upb_decoderet err = {NULL, 0}; + const char *p = r.p; + uint32_t low = (uint32_t)r.val; + uint32_t high = 0; + uint32_t b; + b = *(p++); low |= (b & 0x7fU) << 14; if (!(b & 0x80)) goto done; + b = *(p++); low |= (b & 0x7fU) << 21; if (!(b & 0x80)) goto done; + b = *(p++); low |= (b & 0x7fU) << 28; + high = (b & 0x7fU) >> 4; if (!(b & 0x80)) goto done; + b = *(p++); high |= (b & 0x7fU) << 3; if (!(b & 0x80)) goto done; + b = *(p++); high |= (b & 0x7fU) << 10; if (!(b & 0x80)) goto done; + b = *(p++); high |= (b & 0x7fU) << 17; if (!(b & 0x80)) goto done; + b = *(p++); high |= (b & 0x7fU) << 24; if (!(b & 0x80)) goto done; + b = *(p++); high |= (b & 0x7fU) << 31; if (!(b & 0x80)) goto done; + return err; -static bool encode_unknown(void *c, const void *hd, const char *buf, - size_t len) { - UPB_UNUSED(hd); - return encode_bytes(c, buf, len) && commit(c); +done: + r.val = ((uint64_t)high << 32) | low; + r.p = p; + return r; } -static bool encode_enddelimfield(void *c, const void *hd) { - UPB_UNUSED(hd); - return end_delim(c); -} +/* Like the previous, but uses 64-bit values. */ +upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r) { + const char *p = r.p; + uint64_t val = r.val; + uint64_t b; + upb_decoderet err = {NULL, 0}; + b = *(p++); val |= (b & 0x7fU) << 14; if (!(b & 0x80)) goto done; + b = *(p++); val |= (b & 0x7fU) << 21; if (!(b & 0x80)) goto done; + b = *(p++); val |= (b & 0x7fU) << 28; if (!(b & 0x80)) goto done; + b = *(p++); val |= (b & 0x7fU) << 35; if (!(b & 0x80)) goto done; + b = *(p++); val |= (b & 0x7fU) << 42; if (!(b & 0x80)) goto done; + b = *(p++); val |= (b & 0x7fU) << 49; if (!(b & 0x80)) goto done; + b = *(p++); val |= (b & 0x7fU) << 56; if (!(b & 0x80)) goto done; + b = *(p++); val |= (b & 0x7fU) << 63; if (!(b & 0x80)) goto done; + return err; -static void *encode_startgroup(void *c, const void *hd) { - return (encode_tag(c, hd) && commit(c)) ? c : UPB_BREAK; +done: + r.val = val; + r.p = p; + return r; } -static bool encode_endgroup(void *c, const void *hd) { - return encode_tag(c, hd) && commit(c); -} +#line 1 "upb/json/parser.rl" +/* +** upb::json::Parser (upb_json_parser) +** +** A parser that uses the Ragel State Machine Compiler to generate +** the finite automata. +** +** Ragel only natively handles regular languages, but we can manually +** program it a bit to handle context-free languages like JSON, by using +** the "fcall" and "fret" constructs. +** +** This parser can handle the basics, but needs several things to be fleshed +** out: +** +** - handling of unicode escape sequences (including high surrogate pairs). +** - properly check and report errors for unknown fields, stack overflow, +** improper array nesting (or lack of nesting). +** - handling of base64 sequences with padding characters. +** - handling of push-back (non-success returns from sink functions). +** - handling of keys/escape-sequences/etc that span input buffers. +*/ -static void *encode_startstr(void *c, const void *hd, size_t size_hint) { - UPB_UNUSED(size_hint); - return encode_startdelimfield(c, hd); -} +#include +#include +#include +#include +#include +#include +#include +#include -static size_t encode_strbuf(void *c, const void *hd, const char *buf, - size_t len, const upb_bufhandle *h) { - UPB_UNUSED(hd); - UPB_UNUSED(h); - return encode_bytes(c, buf, len) ? len : 0; -} +#include -#define T(type, ctype, convert, encode) \ - static bool encode_scalar_##type(void *e, const void *hd, ctype val) { \ - return encode_tag(e, hd) && encode(e, (convert)(val)) && commit(e); \ - } \ - static bool encode_packed_##type(void *e, const void *hd, ctype val) { \ - UPB_UNUSED(hd); \ - return encode(e, (convert)(val)); \ - } -T(double, double, dbl2uint64, encode_fixed64) -T(float, float, flt2uint32, encode_fixed32) -T(int64, int64_t, uint64_t, encode_varint) -T(int32, int32_t, int64_t, encode_varint) -T(fixed64, uint64_t, uint64_t, encode_fixed64) -T(fixed32, uint32_t, uint32_t, encode_fixed32) -T(bool, bool, bool, encode_varint) -T(uint32, uint32_t, uint32_t, encode_varint) -T(uint64, uint64_t, uint64_t, encode_varint) -T(enum, int32_t, uint32_t, encode_varint) -T(sfixed32, int32_t, uint32_t, encode_fixed32) -T(sfixed64, int64_t, uint64_t, encode_fixed64) -T(sint32, int32_t, upb_zzenc_32, encode_varint) -T(sint64, int64_t, upb_zzenc_64, encode_varint) +#define UPB_JSON_MAX_DEPTH 64 -#undef T +/* Type of value message */ +enum { + VALUE_NULLVALUE = 0, + VALUE_NUMBERVALUE = 1, + VALUE_STRINGVALUE = 2, + VALUE_BOOLVALUE = 3, + VALUE_STRUCTVALUE = 4, + VALUE_LISTVALUE = 5 +}; +/* Forward declare */ +static bool is_top_level(upb_json_parser *p); +static bool is_wellknown_msg(upb_json_parser *p, upb_wellknowntype_t type); +static bool is_wellknown_field(upb_json_parser *p, upb_wellknowntype_t type); -/* code to build the handlers *************************************************/ +static bool is_number_wrapper_object(upb_json_parser *p); +static bool does_number_wrapper_start(upb_json_parser *p); +static bool does_number_wrapper_end(upb_json_parser *p); -static void newhandlers_callback(const void *closure, upb_handlers *h) { - const upb_msgdef *m; - upb_msg_field_iter i; +static bool is_string_wrapper_object(upb_json_parser *p); +static bool does_string_wrapper_start(upb_json_parser *p); +static bool does_string_wrapper_end(upb_json_parser *p); - UPB_UNUSED(closure); +static bool does_fieldmask_start(upb_json_parser *p); +static bool does_fieldmask_end(upb_json_parser *p); +static void start_fieldmask_object(upb_json_parser *p); +static void end_fieldmask_object(upb_json_parser *p); - upb_handlers_setstartmsg(h, startmsg, NULL); - upb_handlers_setendmsg(h, endmsg, NULL); - upb_handlers_setunknown(h, encode_unknown, NULL); +static void start_wrapper_object(upb_json_parser *p); +static void end_wrapper_object(upb_json_parser *p); - m = upb_handlers_msgdef(h); - for(upb_msg_field_begin(&i, m); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - const upb_fielddef *f = upb_msg_iter_field(&i); - bool packed = upb_fielddef_isseq(f) && upb_fielddef_isprimitive(f) && - upb_fielddef_packed(f); - upb_handlerattr attr; - upb_wiretype_t wt = - packed ? UPB_WIRE_TYPE_DELIMITED - : upb_pb_native_wire_types[upb_fielddef_descriptortype(f)]; +static void start_value_object(upb_json_parser *p, int value_type); +static void end_value_object(upb_json_parser *p); - /* Pre-encode the tag for this field. */ - new_tag(h, f, wt, &attr); +static void start_listvalue_object(upb_json_parser *p); +static void end_listvalue_object(upb_json_parser *p); - if (packed) { - upb_handlers_setstartseq(h, f, encode_startdelimfield, &attr); - upb_handlers_setendseq(h, f, encode_enddelimfield, &attr); - } +static void start_structvalue_object(upb_json_parser *p); +static void end_structvalue_object(upb_json_parser *p); -#define T(upper, lower, upbtype) \ - case UPB_DESCRIPTOR_TYPE_##upper: \ - if (packed) { \ - upb_handlers_set##upbtype(h, f, encode_packed_##lower, &attr); \ - } else { \ - upb_handlers_set##upbtype(h, f, encode_scalar_##lower, &attr); \ - } \ - break; +static void start_object(upb_json_parser *p); +static void end_object(upb_json_parser *p); - switch (upb_fielddef_descriptortype(f)) { - T(DOUBLE, double, double); - T(FLOAT, float, float); - T(INT64, int64, int64); - T(INT32, int32, int32); - T(FIXED64, fixed64, uint64); - T(FIXED32, fixed32, uint32); - T(BOOL, bool, bool); - T(UINT32, uint32, uint32); - T(UINT64, uint64, uint64); - T(ENUM, enum, int32); - T(SFIXED32, sfixed32, int32); - T(SFIXED64, sfixed64, int64); - T(SINT32, sint32, int32); - T(SINT64, sint64, int64); - case UPB_DESCRIPTOR_TYPE_STRING: - case UPB_DESCRIPTOR_TYPE_BYTES: - upb_handlers_setstartstr(h, f, encode_startstr, &attr); - upb_handlers_setendstr(h, f, encode_enddelimfield, &attr); - upb_handlers_setstring(h, f, encode_strbuf, &attr); - break; - case UPB_DESCRIPTOR_TYPE_MESSAGE: - upb_handlers_setstartsubmsg(h, f, encode_startdelimfield, &attr); - upb_handlers_setendsubmsg(h, f, encode_enddelimfield, &attr); - break; - case UPB_DESCRIPTOR_TYPE_GROUP: { - /* Endgroup takes a different tag (wire_type = END_GROUP). */ - upb_handlerattr attr2; - new_tag(h, f, UPB_WIRE_TYPE_END_GROUP, &attr2); +static void start_any_object(upb_json_parser *p, const char *ptr); +static bool end_any_object(upb_json_parser *p, const char *ptr); - upb_handlers_setstartsubmsg(h, f, encode_startgroup, &attr); - upb_handlers_setendsubmsg(h, f, encode_endgroup, &attr2); +static bool start_subobject(upb_json_parser *p); +static void end_subobject(upb_json_parser *p); - upb_handlerattr_uninit(&attr2); - break; - } - } +static void start_member(upb_json_parser *p); +static void end_member(upb_json_parser *p); +static bool end_membername(upb_json_parser *p); -#undef T +static void start_any_member(upb_json_parser *p, const char *ptr); +static void end_any_member(upb_json_parser *p, const char *ptr); +static bool end_any_membername(upb_json_parser *p); - upb_handlerattr_uninit(&attr); - } -} +size_t parse(void *closure, const void *hd, const char *buf, size_t size, + const upb_bufhandle *handle); +static bool end(void *closure, const void *hd); -void upb_pb_encoder_reset(upb_pb_encoder *e) { - e->segptr = NULL; - e->top = NULL; - e->depth = 0; -} +static const char eof_ch = 'e'; +/* stringsink */ +typedef struct { + upb_byteshandler handler; + upb_bytessink sink; + char *ptr; + size_t len, size; +} upb_stringsink; -/* public API *****************************************************************/ -const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m, - const void *owner) { - return upb_handlers_newfrozen(m, owner, newhandlers_callback, NULL); +static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) { + upb_stringsink *sink = _sink; + sink->len = 0; + UPB_UNUSED(hd); + UPB_UNUSED(size_hint); + return sink; } -upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output) { - const size_t initial_bufsize = 256; - const size_t initial_segbufsize = 16; - /* TODO(haberman): make this configurable. */ - const size_t stack_size = 64; -#ifndef NDEBUG - const size_t size_before = upb_env_bytesallocated(env); -#endif - - upb_pb_encoder *e = upb_env_malloc(env, sizeof(upb_pb_encoder)); - if (!e) return NULL; +static size_t stringsink_string(void *_sink, const void *hd, const char *ptr, + size_t len, const upb_bufhandle *handle) { + upb_stringsink *sink = _sink; + size_t new_size = sink->size; - e->buf = upb_env_malloc(env, initial_bufsize); - e->segbuf = upb_env_malloc(env, initial_segbufsize * sizeof(*e->segbuf)); - e->stack = upb_env_malloc(env, stack_size * sizeof(*e->stack)); + UPB_UNUSED(hd); + UPB_UNUSED(handle); - if (!e->buf || !e->segbuf || !e->stack) { - return NULL; + while (sink->len + len > new_size) { + new_size *= 2; } - e->limit = e->buf + initial_bufsize; - e->seglimit = e->segbuf + initial_segbufsize; - e->stacklimit = e->stack + stack_size; - - upb_pb_encoder_reset(e); - upb_sink_reset(&e->input_, h, e); - - e->env = env; - e->output_ = output; - e->subc = output->closure; - e->ptr = e->buf; - - /* If this fails, increase the value in encoder.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= - UPB_PB_ENCODER_SIZE); - return e; -} - -upb_sink *upb_pb_encoder_input(upb_pb_encoder *e) { return &e->input_; } + if (new_size != sink->size) { + sink->ptr = realloc(sink->ptr, new_size); + sink->size = new_size; + } + memcpy(sink->ptr + sink->len, ptr, len); + sink->len += len; + return len; +} -upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner, - upb_status *status) { - /* Create handlers. */ - const upb_pbdecodermethod *decoder_m; - const upb_handlers *reader_h = upb_descreader_newhandlers(&reader_h); - upb_env env; - upb_pbdecodermethodopts opts; - upb_pbdecoder *decoder; - upb_descreader *reader; - bool ok; - size_t i; - upb_filedef **ret = NULL; +void upb_stringsink_init(upb_stringsink *sink) { + upb_byteshandler_init(&sink->handler); + upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL); + upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL); - upb_pbdecodermethodopts_init(&opts, reader_h); - decoder_m = upb_pbdecodermethod_new(&opts, &decoder_m); + upb_bytessink_reset(&sink->sink, &sink->handler, sink); - upb_env_init(&env); - upb_env_reporterrorsto(&env, status); + sink->size = 32; + sink->ptr = malloc(sink->size); + sink->len = 0; +} - reader = upb_descreader_create(&env, reader_h); - decoder = upb_pbdecoder_create(&env, decoder_m, upb_descreader_input(reader)); +void upb_stringsink_uninit(upb_stringsink *sink) { free(sink->ptr); } - /* Push input data. */ - ok = upb_bufsrc_putbuf(buf, n, upb_pbdecoder_input(decoder)); +typedef struct { + /* For encoding Any value field in binary format. */ + upb_handlercache *encoder_handlercache; + upb_stringsink stringsink; - if (!ok) { - goto cleanup; - } + /* For decoding Any value field in json format. */ + upb_json_codecache *parser_codecache; + upb_sink sink; + upb_json_parser *parser; - ret = upb_gmalloc(sizeof (*ret) * (upb_descreader_filecount(reader) + 1)); + /* Mark the range of uninterpreted values in json input before type url. */ + const char *before_type_url_start; + const char *before_type_url_end; - if (!ret) { - goto cleanup; - } + /* Mark the range of uninterpreted values in json input after type url. */ + const char *after_type_url_start; +} upb_jsonparser_any_frame; - for (i = 0; i < upb_descreader_filecount(reader); i++) { - ret[i] = upb_descreader_file(reader, i); - upb_filedef_ref(ret[i], owner); - } +typedef struct { + upb_sink sink; - ret[i] = NULL; + /* The current message in which we're parsing, and the field whose value we're + * expecting next. */ + const upb_msgdef *m; + const upb_fielddef *f; -cleanup: - upb_env_uninit(&env); - upb_handlers_unref(reader_h, &reader_h); - upb_pbdecodermethod_unref(decoder_m, &decoder_m); - return ret; -} -/* - * upb::pb::TextPrinter - * - * OPT: This is not optimized at all. It uses printf() which parses the format - * string every time, and it allocates memory for every put. - */ + /* The table mapping json name to fielddef for this message. */ + const upb_strtable *name_table; + /* We are in a repeated-field context. We need this flag to decide whether to + * handle the array as a normal repeated field or a + * google.protobuf.ListValue/google.protobuf.Value. */ + bool is_repeated; -#include -#include -#include -#include -#include -#include + /* We are in a repeated-field context, ready to emit mapentries as + * submessages. This flag alters the start-of-object (open-brace) behavior to + * begin a sequence of mapentry messages rather than a single submessage. */ + bool is_map; + /* We are in a map-entry message context. This flag is set when parsing the + * value field of a single map entry and indicates to all value-field parsers + * (subobjects, strings, numbers, and bools) that the map-entry submessage + * should end as soon as the value is parsed. */ + bool is_mapentry; -struct upb_textprinter { - upb_sink input_; - upb_bytessink *output_; - int indent_depth_; - bool single_line_; - void *subc; -}; + /* If |is_map| or |is_mapentry| is true, |mapfield| refers to the parent + * message's map field that we're currently parsing. This differs from |f| + * because |f| is the field in the *current* message (i.e., the map-entry + * message itself), not the parent's field that leads to this map. */ + const upb_fielddef *mapfield; -#define CHECK(x) if ((x) < 0) goto err; + /* We are in an Any message context. This flag is set when parsing the Any + * message and indicates to all field parsers (subobjects, strings, numbers, + * and bools) that the parsed field should be serialized as binary data or + * cached (type url not found yet). */ + bool is_any; -static const char *shortname(const char *longname) { - const char *last = strrchr(longname, '.'); - return last ? last + 1 : longname; -} + /* The type of packed message in Any. */ + upb_jsonparser_any_frame *any_frame; -static int indent(upb_textprinter *p) { - int i; - if (!p->single_line_) - for (i = 0; i < p->indent_depth_; i++) - upb_bytessink_putbuf(p->output_, p->subc, " ", 2, NULL); - return 0; -} + /* True if the field to be parsed is unknown. */ + bool is_unknown_field; +} upb_jsonparser_frame; -static int endfield(upb_textprinter *p) { - const char ch = (p->single_line_ ? ' ' : '\n'); - upb_bytessink_putbuf(p->output_, p->subc, &ch, 1, NULL); - return 0; +static void init_frame(upb_jsonparser_frame* frame) { + frame->m = NULL; + frame->f = NULL; + frame->name_table = NULL; + frame->is_repeated = false; + frame->is_map = false; + frame->is_mapentry = false; + frame->mapfield = NULL; + frame->is_any = false; + frame->any_frame = NULL; + frame->is_unknown_field = false; } -static int putescaped(upb_textprinter *p, const char *buf, size_t len, - bool preserve_utf8) { - /* Based on CEscapeInternal() from Google's protobuf release. */ - char dstbuf[4096], *dst = dstbuf, *dstend = dstbuf + sizeof(dstbuf); - const char *end = buf + len; +struct upb_json_parser { + upb_arena *arena; + const upb_json_parsermethod *method; + upb_bytessink input_; - /* I think hex is prettier and more useful, but proto2 uses octal; should - * investigate whether it can parse hex also. */ - const bool use_hex = false; - bool last_hex_escape = false; /* true if last output char was \xNN */ + /* Stack to track the JSON scopes we are in. */ + upb_jsonparser_frame stack[UPB_JSON_MAX_DEPTH]; + upb_jsonparser_frame *top; + upb_jsonparser_frame *limit; - for (; buf < end; buf++) { - bool is_hex_escape; + upb_status *status; - if (dstend - dst < 4) { - upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); - dst = dstbuf; - } + /* Ragel's internal parsing stack for the parsing state machine. */ + int current_state; + int parser_stack[UPB_JSON_MAX_DEPTH]; + int parser_top; - is_hex_escape = false; - switch (*buf) { - case '\n': *(dst++) = '\\'; *(dst++) = 'n'; break; - case '\r': *(dst++) = '\\'; *(dst++) = 'r'; break; - case '\t': *(dst++) = '\\'; *(dst++) = 't'; break; - case '\"': *(dst++) = '\\'; *(dst++) = '\"'; break; - case '\'': *(dst++) = '\\'; *(dst++) = '\''; break; - case '\\': *(dst++) = '\\'; *(dst++) = '\\'; break; - default: - /* Note that if we emit \xNN and the buf 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 ((!preserve_utf8 || (uint8_t)*buf < 0x80) && - (!isprint(*buf) || (last_hex_escape && isxdigit(*buf)))) { - sprintf(dst, (use_hex ? "\\x%02x" : "\\%03o"), (uint8_t)*buf); - is_hex_escape = use_hex; - dst += 4; - } else { - *(dst++) = *buf; break; - } - } - last_hex_escape = is_hex_escape; - } - /* Flush remaining data. */ - upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); - return 0; -} + /* The handle for the current buffer. */ + const upb_bufhandle *handle; -bool putf(upb_textprinter *p, const char *fmt, ...) { - va_list args; - va_list args_copy; - char *str; - int written; - int len; - bool ok; + /* Accumulate buffer. See details in parser.rl. */ + const char *accumulated; + size_t accumulated_len; + char *accumulate_buf; + size_t accumulate_buf_size; - va_start(args, fmt); + /* Multi-part text data. See details in parser.rl. */ + int multipart_state; + upb_selector_t string_selector; - /* Run once to get the length of the string. */ - _upb_va_copy(args_copy, args); - len = _upb_vsnprintf(NULL, 0, fmt, args_copy); - va_end(args_copy); + /* Input capture. See details in parser.rl. */ + const char *capture; - /* + 1 for NULL terminator (vsprintf() requires it even if we don't). */ - str = upb_gmalloc(len + 1); - if (!str) return false; - written = vsprintf(str, fmt, args); - va_end(args); - UPB_ASSERT(written == len); + /* Intermediate result of parsing a unicode escape sequence. */ + uint32_t digit; - ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL); - upb_gfree(str); - return ok; -} + /* For resolve type url in Any. */ + const upb_symtab *symtab; + /* Whether to proceed if unknown field is met. */ + bool ignore_json_unknown; -/* handlers *******************************************************************/ + /* Cache for parsing timestamp due to base and zone are handled in different + * handlers. */ + struct tm tm; +}; -static bool textprinter_startmsg(void *c, const void *hd) { - upb_textprinter *p = c; - UPB_UNUSED(hd); - if (p->indent_depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc); - } - return true; +static upb_jsonparser_frame* start_jsonparser_frame(upb_json_parser *p) { + upb_jsonparser_frame *inner; + inner = p->top + 1; + init_frame(inner); + return inner; } -static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) { - upb_textprinter *p = c; - UPB_UNUSED(hd); - UPB_UNUSED(s); - if (p->indent_depth_ == 0) { - upb_bytessink_end(p->output_); - } - return true; -} +struct upb_json_codecache { + upb_arena *arena; + upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ +}; -#define TYPE(name, ctype, fmt) \ - static bool textprinter_put ## name(void *closure, const void *handler_data, \ - ctype val) { \ - upb_textprinter *p = closure; \ - const upb_fielddef *f = handler_data; \ - CHECK(indent(p)); \ - putf(p, "%s: " fmt, upb_fielddef_name(f), val); \ - CHECK(endfield(p)); \ - return true; \ - err: \ - return false; \ -} +struct upb_json_parsermethod { + const upb_json_codecache *cache; + upb_byteshandler input_handler_; -static bool textprinter_putbool(void *closure, const void *handler_data, - bool val) { - upb_textprinter *p = closure; - const upb_fielddef *f = handler_data; - CHECK(indent(p)); - putf(p, "%s: %s", upb_fielddef_name(f), val ? "true" : "false"); - CHECK(endfield(p)); - return true; -err: - return false; -} + /* Maps json_name -> fielddef */ + upb_strtable name_table; +}; -#define STRINGIFY_HELPER(x) #x -#define STRINGIFY_MACROVAL(x) STRINGIFY_HELPER(x) +#define PARSER_CHECK_RETURN(x) if (!(x)) return false -TYPE(int32, int32_t, "%" PRId32) -TYPE(int64, int64_t, "%" PRId64) -TYPE(uint32, uint32_t, "%" PRIu32) -TYPE(uint64, uint64_t, "%" PRIu64) -TYPE(float, float, "%." STRINGIFY_MACROVAL(FLT_DIG) "g") -TYPE(double, double, "%." STRINGIFY_MACROVAL(DBL_DIG) "g") +static upb_jsonparser_any_frame *json_parser_any_frame_new( + upb_json_parser *p) { + upb_jsonparser_any_frame *frame; -#undef TYPE + frame = upb_arena_malloc(p->arena, sizeof(upb_jsonparser_any_frame)); -/* Output a symbolic value from the enum if found, else just print as int32. */ -static bool textprinter_putenum(void *closure, const void *handler_data, - int32_t val) { - upb_textprinter *p = closure; - const upb_fielddef *f = handler_data; - const upb_enumdef *enum_def = upb_downcast_enumdef(upb_fielddef_subdef(f)); - const char *label = upb_enumdef_iton(enum_def, val); - if (label) { - indent(p); - putf(p, "%s: %s", upb_fielddef_name(f), label); - endfield(p); - } else { - if (!textprinter_putint32(closure, handler_data, val)) - return false; - } - return true; -} + frame->encoder_handlercache = upb_pb_encoder_newcache(); + frame->parser_codecache = upb_json_codecache_new(); + frame->parser = NULL; + frame->before_type_url_start = NULL; + frame->before_type_url_end = NULL; + frame->after_type_url_start = NULL; -static void *textprinter_startstr(void *closure, const void *handler_data, - size_t size_hint) { - upb_textprinter *p = closure; - const upb_fielddef *f = handler_data; - UPB_UNUSED(size_hint); - indent(p); - putf(p, "%s: \"", upb_fielddef_name(f)); - return p; -} + upb_stringsink_init(&frame->stringsink); -static bool textprinter_endstr(void *closure, const void *handler_data) { - upb_textprinter *p = closure; - UPB_UNUSED(handler_data); - putf(p, "\""); - endfield(p); - return true; + return frame; } -static size_t textprinter_putstr(void *closure, const void *hd, const char *buf, - size_t len, const upb_bufhandle *handle) { - upb_textprinter *p = closure; - const upb_fielddef *f = hd; - UPB_UNUSED(handle); - CHECK(putescaped(p, buf, len, upb_fielddef_type(f) == UPB_TYPE_STRING)); - return len; -err: - return 0; +static void json_parser_any_frame_set_payload_type( + upb_json_parser *p, + upb_jsonparser_any_frame *frame, + const upb_msgdef *payload_type) { + const upb_handlers *h; + const upb_json_parsermethod *parser_method; + upb_pb_encoder *encoder; + + /* Initialize encoder. */ + h = upb_handlercache_get(frame->encoder_handlercache, payload_type); + encoder = upb_pb_encoder_create(p->arena, h, frame->stringsink.sink); + + /* Initialize parser. */ + parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); + upb_sink_reset(&frame->sink, h, encoder); + frame->parser = + upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, + p->status, p->ignore_json_unknown); } -static void *textprinter_startsubmsg(void *closure, const void *handler_data) { - upb_textprinter *p = closure; - const char *name = handler_data; - CHECK(indent(p)); - putf(p, "%s {%c", name, p->single_line_ ? ' ' : '\n'); - p->indent_depth_++; - return p; -err: - return UPB_BREAK; +static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { + upb_handlercache_free(frame->encoder_handlercache); + upb_json_codecache_free(frame->parser_codecache); + upb_stringsink_uninit(&frame->stringsink); } -static bool textprinter_endsubmsg(void *closure, const void *handler_data) { - upb_textprinter *p = closure; - UPB_UNUSED(handler_data); - p->indent_depth_--; - CHECK(indent(p)); - upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL); - CHECK(endfield(p)); - return true; -err: - return false; +static bool json_parser_any_frame_has_type_url( + upb_jsonparser_any_frame *frame) { + return frame->parser != NULL; } -static void onmreg(const void *c, upb_handlers *h) { - const upb_msgdef *m = upb_handlers_msgdef(h); - upb_msg_field_iter i; - UPB_UNUSED(c); +static bool json_parser_any_frame_has_value_before_type_url( + upb_jsonparser_any_frame *frame) { + return frame->before_type_url_start != frame->before_type_url_end; +} - upb_handlers_setstartmsg(h, textprinter_startmsg, NULL); - upb_handlers_setendmsg(h, textprinter_endmsg, NULL); +static bool json_parser_any_frame_has_value_after_type_url( + upb_jsonparser_any_frame *frame) { + return frame->after_type_url_start != NULL; +} - for(upb_msg_field_begin(&i, m); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, f); - switch (upb_fielddef_type(f)) { - case UPB_TYPE_INT32: - upb_handlers_setint32(h, f, textprinter_putint32, &attr); - break; - case UPB_TYPE_INT64: - upb_handlers_setint64(h, f, textprinter_putint64, &attr); - break; - case UPB_TYPE_UINT32: - upb_handlers_setuint32(h, f, textprinter_putuint32, &attr); - break; - case UPB_TYPE_UINT64: - upb_handlers_setuint64(h, f, textprinter_putuint64, &attr); - break; - case UPB_TYPE_FLOAT: - upb_handlers_setfloat(h, f, textprinter_putfloat, &attr); - break; - case UPB_TYPE_DOUBLE: - upb_handlers_setdouble(h, f, textprinter_putdouble, &attr); - break; - case UPB_TYPE_BOOL: - upb_handlers_setbool(h, f, textprinter_putbool, &attr); - break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - upb_handlers_setstartstr(h, f, textprinter_startstr, &attr); - upb_handlers_setstring(h, f, textprinter_putstr, &attr); - upb_handlers_setendstr(h, f, textprinter_endstr, &attr); - break; - case UPB_TYPE_MESSAGE: { - const char *name = - upb_fielddef_istagdelim(f) - ? shortname(upb_msgdef_fullname(upb_fielddef_msgsubdef(f))) - : upb_fielddef_name(f); - upb_handlerattr_sethandlerdata(&attr, name); - upb_handlers_setstartsubmsg(h, f, textprinter_startsubmsg, &attr); - upb_handlers_setendsubmsg(h, f, textprinter_endsubmsg, &attr); - break; - } - case UPB_TYPE_ENUM: - upb_handlers_setint32(h, f, textprinter_putenum, &attr); - break; - } +static bool json_parser_any_frame_has_value( + upb_jsonparser_any_frame *frame) { + return json_parser_any_frame_has_value_before_type_url(frame) || + json_parser_any_frame_has_value_after_type_url(frame); +} + +static void json_parser_any_frame_set_before_type_url_end( + upb_jsonparser_any_frame *frame, + const char *ptr) { + if (frame->parser == NULL) { + frame->before_type_url_end = ptr; } } -static void textprinter_reset(upb_textprinter *p, bool single_line) { - p->single_line_ = single_line; - p->indent_depth_ = 0; +static void json_parser_any_frame_set_after_type_url_start_once( + upb_jsonparser_any_frame *frame, + const char *ptr) { + if (json_parser_any_frame_has_type_url(frame) && + frame->after_type_url_start == NULL) { + frame->after_type_url_start = ptr; + } } +/* Used to signal that a capture has been suspended. */ +static char suspend_capture; -/* Public API *****************************************************************/ +static upb_selector_t getsel_for_handlertype(upb_json_parser *p, + upb_handlertype_t type) { + upb_selector_t sel; + bool ok = upb_handlers_getselector(p->top->f, type, &sel); + UPB_ASSERT(ok); + return sel; +} -upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output) { - upb_textprinter *p = upb_env_malloc(env, sizeof(upb_textprinter)); - if (!p) return NULL; +static upb_selector_t parser_getsel(upb_json_parser *p) { + return getsel_for_handlertype( + p, upb_handlers_getprimitivehandlertype(p->top->f)); +} - p->output_ = output; - upb_sink_reset(&p->input_, h, p); - textprinter_reset(p, false); +static bool check_stack(upb_json_parser *p) { + if ((p->top + 1) == p->limit) { + upb_status_seterrmsg(p->status, "Nesting too deep"); + return false; + } - return p; + return true; } -const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m, - const void *owner) { - return upb_handlers_newfrozen(m, owner, &onmreg, NULL); -} +static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { + upb_value v; + const upb_json_codecache *cache = p->method->cache; + bool ok; + const upb_json_parsermethod *method; -upb_sink *upb_textprinter_input(upb_textprinter *p) { return &p->input_; } + ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); + UPB_ASSERT(ok); + method = upb_value_getconstptr(v); -void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line) { - p->single_line_ = single_line; + frame->name_table = &method->name_table; } +/* There are GCC/Clang built-ins for overflow checking which we could start + * using if there was any performance benefit to it. */ -/* Index is descriptor type. */ -const uint8_t upb_pb_native_wire_types[] = { - UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */ - UPB_WIRE_TYPE_64BIT, /* DOUBLE */ - UPB_WIRE_TYPE_32BIT, /* FLOAT */ - UPB_WIRE_TYPE_VARINT, /* INT64 */ - UPB_WIRE_TYPE_VARINT, /* UINT64 */ - UPB_WIRE_TYPE_VARINT, /* INT32 */ - UPB_WIRE_TYPE_64BIT, /* FIXED64 */ - UPB_WIRE_TYPE_32BIT, /* FIXED32 */ - UPB_WIRE_TYPE_VARINT, /* BOOL */ - UPB_WIRE_TYPE_DELIMITED, /* STRING */ - UPB_WIRE_TYPE_START_GROUP, /* GROUP */ - UPB_WIRE_TYPE_DELIMITED, /* MESSAGE */ - UPB_WIRE_TYPE_DELIMITED, /* BYTES */ - UPB_WIRE_TYPE_VARINT, /* UINT32 */ - UPB_WIRE_TYPE_VARINT, /* ENUM */ - UPB_WIRE_TYPE_32BIT, /* SFIXED32 */ - UPB_WIRE_TYPE_64BIT, /* SFIXED64 */ - UPB_WIRE_TYPE_VARINT, /* SINT32 */ - UPB_WIRE_TYPE_VARINT, /* SINT64 */ -}; - -/* A basic branch-based decoder, uses 32-bit values to get good performance - * on 32-bit architectures (but performs well on 64-bits also). - * This scheme comes from the original Google Protobuf implementation - * (proto2). */ -upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r) { - upb_decoderet err = {NULL, 0}; - const char *p = r.p; - uint32_t low = (uint32_t)r.val; - uint32_t high = 0; - uint32_t b; - b = *(p++); low |= (b & 0x7fU) << 14; if (!(b & 0x80)) goto done; - b = *(p++); low |= (b & 0x7fU) << 21; if (!(b & 0x80)) goto done; - b = *(p++); low |= (b & 0x7fU) << 28; - high = (b & 0x7fU) >> 4; if (!(b & 0x80)) goto done; - b = *(p++); high |= (b & 0x7fU) << 3; if (!(b & 0x80)) goto done; - b = *(p++); high |= (b & 0x7fU) << 10; if (!(b & 0x80)) goto done; - b = *(p++); high |= (b & 0x7fU) << 17; if (!(b & 0x80)) goto done; - b = *(p++); high |= (b & 0x7fU) << 24; if (!(b & 0x80)) goto done; - b = *(p++); high |= (b & 0x7fU) << 31; if (!(b & 0x80)) goto done; - return err; +static bool checked_add(size_t a, size_t b, size_t *c) { + if (SIZE_MAX - a < b) return false; + *c = a + b; + return true; +} -done: - r.val = ((uint64_t)high << 32) | low; - r.p = p; - return r; +static size_t saturating_multiply(size_t a, size_t b) { + /* size_t is unsigned, so this is defined behavior even on overflow. */ + size_t ret = a * b; + if (b != 0 && ret / b != a) { + ret = SIZE_MAX; + } + return ret; } -/* Like the previous, but uses 64-bit values. */ -upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r) { - const char *p = r.p; - uint64_t val = r.val; - uint64_t b; - upb_decoderet err = {NULL, 0}; - b = *(p++); val |= (b & 0x7fU) << 14; if (!(b & 0x80)) goto done; - b = *(p++); val |= (b & 0x7fU) << 21; if (!(b & 0x80)) goto done; - b = *(p++); val |= (b & 0x7fU) << 28; if (!(b & 0x80)) goto done; - b = *(p++); val |= (b & 0x7fU) << 35; if (!(b & 0x80)) goto done; - b = *(p++); val |= (b & 0x7fU) << 42; if (!(b & 0x80)) goto done; - b = *(p++); val |= (b & 0x7fU) << 49; if (!(b & 0x80)) goto done; - b = *(p++); val |= (b & 0x7fU) << 56; if (!(b & 0x80)) goto done; - b = *(p++); val |= (b & 0x7fU) << 63; if (!(b & 0x80)) goto done; - return err; -done: - r.val = val; - r.p = p; - return r; -} +/* Base64 decoding ************************************************************/ -#line 1 "upb/json/parser.rl" -/* -** upb::json::Parser (upb_json_parser) -** -** A parser that uses the Ragel State Machine Compiler to generate -** the finite automata. -** -** Ragel only natively handles regular languages, but we can manually -** program it a bit to handle context-free languages like JSON, by using -** the "fcall" and "fret" constructs. -** -** This parser can handle the basics, but needs several things to be fleshed -** out: -** -** - handling of unicode escape sequences (including high surrogate pairs). -** - properly check and report errors for unknown fields, stack overflow, -** improper array nesting (or lack of nesting). -** - handling of base64 sequences with padding characters. -** - handling of push-back (non-success returns from sink functions). -** - handling of keys/escape-sequences/etc that span input buffers. -*/ +/* TODO(haberman): make this streaming. */ + +static const signed char b64table[] = { + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -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*/, + 07/*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 +}; -#include -#include -#include -#include -#include -#include +/* Returns the table value sign-extended to 32 bits. Knowing that the upper + * bits will be 1 for unrecognized characters makes it easier to check for + * this error condition later (see below). */ +int32_t b64lookup(unsigned char ch) { return b64table[ch]; } +/* Returns true if the given character is not a valid base64 character or + * padding. */ +bool nonbase64(unsigned char ch) { return b64lookup(ch) == -1 && ch != '='; } -#define UPB_JSON_MAX_DEPTH 64 +static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, + size_t len) { + const char *limit = ptr + len; + for (; ptr < limit; ptr += 4) { + uint32_t val; + char output[3]; -static const char *kDoubleValueFullMessageName = "google.protobuf.DoubleValue"; -static const char *kFloatValueFullMessageName = "google.protobuf.FloatValue"; -static const char *kInt64ValueFullMessageName = "google.protobuf.Int64Value"; -static const char *kUInt64ValueFullMessageName = "google.protobuf.UInt64Value"; -static const char *kInt32ValueFullMessageName = "google.protobuf.Int32Value"; -static const char *kUInt32ValueFullMessageName = "google.protobuf.UInt32Value"; -static const char *kBoolValueFullMessageName = "google.protobuf.BoolValue"; -static const char *kStringValueFullMessageName = "google.protobuf.StringValue"; -static const char *kBytesValueFullMessageName = "google.protobuf.BytesValue"; + if (limit - ptr < 4) { + upb_status_seterrf(p->status, + "Base64 input for bytes field not a multiple of 4: %s", + upb_fielddef_name(p->top->f)); + return false; + } -/* Forward declare */ -static bool is_top_level(upb_json_parser *p); + val = b64lookup(ptr[0]) << 18 | + b64lookup(ptr[1]) << 12 | + b64lookup(ptr[2]) << 6 | + b64lookup(ptr[3]); -static bool is_number_wrapper_object(upb_json_parser *p); -static bool does_number_wrapper_start(upb_json_parser *p); -static bool does_number_wrapper_end(upb_json_parser *p); + /* Test the upper bit; returns true if any of the characters returned -1. */ + if (val & 0x80000000) { + goto otherchar; + } -static bool is_string_wrapper_object(upb_json_parser *p); -static bool does_string_wrapper_start(upb_json_parser *p); -static bool does_string_wrapper_end(upb_json_parser *p); + output[0] = val >> 16; + output[1] = (val >> 8) & 0xff; + output[2] = val & 0xff; + upb_sink_putstring(p->top->sink, sel, output, 3, NULL); + } + return true; -static bool is_boolean_wrapper_object(upb_json_parser *p); -static bool does_boolean_wrapper_start(upb_json_parser *p); -static bool does_boolean_wrapper_end(upb_json_parser *p); +otherchar: + if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) || + nonbase64(ptr[3]) ) { + upb_status_seterrf(p->status, + "Non-base64 characters in bytes field: %s", + upb_fielddef_name(p->top->f)); + return false; + } if (ptr[2] == '=') { + uint32_t val; + char output; -static void start_wrapper_object(upb_json_parser *p); -static void end_wrapper_object(upb_json_parser *p); + /* Last group contains only two input bytes, one output byte. */ + if (ptr[0] == '=' || ptr[1] == '=' || ptr[3] != '=') { + goto badpadding; + } -static bool start_subobject(upb_json_parser *p); -static void end_subobject(upb_json_parser *p); + val = b64lookup(ptr[0]) << 18 | + b64lookup(ptr[1]) << 12; -static const char eof_ch = 'e'; + UPB_ASSERT(!(val & 0x80000000)); + output = val >> 16; + upb_sink_putstring(p->top->sink, sel, &output, 1, NULL); + return true; + } else { + uint32_t val; + char output[2]; -typedef struct { - upb_sink sink; + /* Last group contains only three input bytes, two output bytes. */ + if (ptr[0] == '=' || ptr[1] == '=' || ptr[2] == '=') { + goto badpadding; + } - /* The current message in which we're parsing, and the field whose value we're - * expecting next. */ - const upb_msgdef *m; - const upb_fielddef *f; + val = b64lookup(ptr[0]) << 18 | + b64lookup(ptr[1]) << 12 | + b64lookup(ptr[2]) << 6; - /* The table mapping json name to fielddef for this message. */ - upb_strtable *name_table; + output[0] = val >> 16; + output[1] = (val >> 8) & 0xff; + upb_sink_putstring(p->top->sink, sel, output, 2, NULL); + return true; + } - /* We are in a repeated-field context, ready to emit mapentries as - * submessages. This flag alters the start-of-object (open-brace) behavior to - * begin a sequence of mapentry messages rather than a single submessage. */ - bool is_map; +badpadding: + upb_status_seterrf(p->status, + "Incorrect base64 padding for field: %s (%.*s)", + upb_fielddef_name(p->top->f), + 4, ptr); + return false; +} - /* We are in a map-entry message context. This flag is set when parsing the - * value field of a single map entry and indicates to all value-field parsers - * (subobjects, strings, numbers, and bools) that the map-entry submessage - * should end as soon as the value is parsed. */ - bool is_mapentry; - /* If |is_map| or |is_mapentry| is true, |mapfield| refers to the parent - * message's map field that we're currently parsing. This differs from |f| - * because |f| is the field in the *current* message (i.e., the map-entry - * message itself), not the parent's field that leads to this map. */ - const upb_fielddef *mapfield; -} upb_jsonparser_frame; +/* Accumulate buffer **********************************************************/ -struct upb_json_parser { - upb_env *env; - const upb_json_parsermethod *method; - upb_bytessink input_; +/* Functionality for accumulating a buffer. + * + * Some parts of the parser need an entire value as a contiguous string. For + * example, to look up a member name in a hash table, or to turn a string into + * a number, the relevant library routines need the input string to be in + * contiguous memory, even if the value spanned two or more buffers in the + * input. These routines handle that. + * + * In the common case we can just point to the input buffer to get this + * contiguous string and avoid any actual copy. So we optimistically begin + * this way. But there are a few cases where we must instead copy into a + * separate buffer: + * + * 1. The string was not contiguous in the input (it spanned buffers). + * + * 2. The string included escape sequences that need to be interpreted to get + * the true value in a contiguous buffer. */ - /* Stack to track the JSON scopes we are in. */ - upb_jsonparser_frame stack[UPB_JSON_MAX_DEPTH]; - upb_jsonparser_frame *top; - upb_jsonparser_frame *limit; +static void assert_accumulate_empty(upb_json_parser *p) { + UPB_ASSERT(p->accumulated == NULL); + UPB_ASSERT(p->accumulated_len == 0); +} - upb_status status; +static void accumulate_clear(upb_json_parser *p) { + p->accumulated = NULL; + p->accumulated_len = 0; +} - /* Ragel's internal parsing stack for the parsing state machine. */ - int current_state; - int parser_stack[UPB_JSON_MAX_DEPTH]; - int parser_top; +/* Used internally by accumulate_append(). */ +static bool accumulate_realloc(upb_json_parser *p, size_t need) { + void *mem; + size_t old_size = p->accumulate_buf_size; + size_t new_size = UPB_MAX(old_size, 128); + while (new_size < need) { + new_size = saturating_multiply(new_size, 2); + } - /* The handle for the current buffer. */ - const upb_bufhandle *handle; + mem = upb_arena_realloc(p->arena, p->accumulate_buf, old_size, new_size); + if (!mem) { + upb_status_seterrmsg(p->status, "Out of memory allocating buffer."); + return false; + } - /* Accumulate buffer. See details in parser.rl. */ - const char *accumulated; - size_t accumulated_len; - char *accumulate_buf; - size_t accumulate_buf_size; + p->accumulate_buf = mem; + p->accumulate_buf_size = new_size; + return true; +} - /* Multi-part text data. See details in parser.rl. */ - int multipart_state; - upb_selector_t string_selector; +/* Logically appends the given data to the append buffer. + * If "can_alias" is true, we will try to avoid actually copying, but the buffer + * must be valid until the next accumulate_append() call (if any). */ +static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len, + bool can_alias) { + size_t need; - /* Input capture. See details in parser.rl. */ - const char *capture; + if (!p->accumulated && can_alias) { + p->accumulated = buf; + p->accumulated_len = len; + return true; + } - /* Intermediate result of parsing a unicode escape sequence. */ - uint32_t digit; + if (!checked_add(p->accumulated_len, len, &need)) { + upb_status_seterrmsg(p->status, "Integer overflow."); + return false; + } - /* Whether to proceed if unknown field is met. */ - bool ignore_json_unknown; -}; + if (need > p->accumulate_buf_size && !accumulate_realloc(p, need)) { + return false; + } -struct upb_json_parsermethod { - upb_refcounted base; + if (p->accumulated != p->accumulate_buf) { + memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len); + p->accumulated = p->accumulate_buf; + } - upb_byteshandler input_handler_; + memcpy(p->accumulate_buf + p->accumulated_len, buf, len); + p->accumulated_len += len; + return true; +} - /* Mainly for the purposes of refcounting, so all the fielddefs we point - * to stay alive. */ - const upb_msgdef *msg; +/* Returns a pointer to the data accumulated since the last accumulate_clear() + * call, and writes the length to *len. This with point either to the input + * buffer or a temporary accumulate buffer. */ +static const char *accumulate_getptr(upb_json_parser *p, size_t *len) { + UPB_ASSERT(p->accumulated); + *len = p->accumulated_len; + return p->accumulated; +} - /* Keys are upb_msgdef*, values are upb_strtable (json_name -> fielddef) */ - upb_inttable name_tables; -}; -#define PARSER_CHECK_RETURN(x) if (!(x)) return false +/* Mult-part text data ********************************************************/ -/* Used to signal that a capture has been suspended. */ -static char suspend_capture; +/* When we have text data in the input, it can often come in multiple segments. + * For example, there may be some raw string data followed by an escape + * sequence. The two segments are processed with different logic. Also buffer + * seams in the input can cause multiple segments. + * + * As we see segments, there are two main cases for how we want to process them: + * + * 1. we want to push the captured input directly to string handlers. + * + * 2. we need to accumulate all the parts into a contiguous buffer for further + * processing (field name lookup, string->number conversion, etc). */ -static upb_selector_t getsel_for_handlertype(upb_json_parser *p, - upb_handlertype_t type) { - upb_selector_t sel; - bool ok = upb_handlers_getselector(p->top->f, type, &sel); - UPB_ASSERT(ok); - return sel; +/* This is the set of states for p->multipart_state. */ +enum { + /* We are not currently processing multipart data. */ + MULTIPART_INACTIVE = 0, + + /* We are processing multipart data by accumulating it into a contiguous + * buffer. */ + MULTIPART_ACCUMULATE = 1, + + /* We are processing multipart data by pushing each part directly to the + * current string handlers. */ + MULTIPART_PUSHEAGERLY = 2 +}; + +/* Start a multi-part text value where we accumulate the data for processing at + * the end. */ +static void multipart_startaccum(upb_json_parser *p) { + assert_accumulate_empty(p); + UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE); + p->multipart_state = MULTIPART_ACCUMULATE; } -static upb_selector_t parser_getsel(upb_json_parser *p) { - return getsel_for_handlertype( - p, upb_handlers_getprimitivehandlertype(p->top->f)); +/* Start a multi-part text value where we immediately push text data to a string + * value with the given selector. */ +static void multipart_start(upb_json_parser *p, upb_selector_t sel) { + assert_accumulate_empty(p); + UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE); + p->multipart_state = MULTIPART_PUSHEAGERLY; + p->string_selector = sel; } -static bool check_stack(upb_json_parser *p) { - if ((p->top + 1) == p->limit) { - upb_status_seterrmsg(&p->status, "Nesting too deep"); - upb_env_reporterror(p->env, &p->status); - return false; +static bool multipart_text(upb_json_parser *p, const char *buf, size_t len, + bool can_alias) { + switch (p->multipart_state) { + case MULTIPART_INACTIVE: + upb_status_seterrmsg( + p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); + return false; + + case MULTIPART_ACCUMULATE: + if (!accumulate_append(p, buf, len, can_alias)) { + return false; + } + break; + + case MULTIPART_PUSHEAGERLY: { + const upb_bufhandle *handle = can_alias ? p->handle : NULL; + upb_sink_putstring(p->top->sink, p->string_selector, buf, len, handle); + break; + } } return true; } -static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { - upb_value v; - bool ok = upb_inttable_lookupptr(&p->method->name_tables, frame->m, &v); - UPB_ASSERT(ok); - frame->name_table = upb_value_getptr(v); +/* Note: this invalidates the accumulate buffer! Call only after reading its + * contents. */ +static void multipart_end(upb_json_parser *p) { + UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE); + p->multipart_state = MULTIPART_INACTIVE; + accumulate_clear(p); } -/* There are GCC/Clang built-ins for overflow checking which we could start - * using if there was any performance benefit to it. */ -static bool checked_add(size_t a, size_t b, size_t *c) { - if (SIZE_MAX - a < b) return false; - *c = a + b; - return true; +/* Input capture **************************************************************/ + +/* Functionality for capturing a region of the input as text. Gracefully + * handles the case where a buffer seam occurs in the middle of the captured + * region. */ + +static void capture_begin(upb_json_parser *p, const char *ptr) { + UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE); + UPB_ASSERT(p->capture == NULL); + p->capture = ptr; } -static size_t saturating_multiply(size_t a, size_t b) { - /* size_t is unsigned, so this is defined behavior even on overflow. */ - size_t ret = a * b; - if (b != 0 && ret / b != a) { - ret = SIZE_MAX; +static bool capture_end(upb_json_parser *p, const char *ptr) { + UPB_ASSERT(p->capture); + if (multipart_text(p, p->capture, ptr - p->capture, true)) { + p->capture = NULL; + return true; + } else { + return false; } - return ret; } +/* This is called at the end of each input buffer (ie. when we have hit a + * buffer seam). If we are in the middle of capturing the input, this + * processes the unprocessed capture region. */ +static void capture_suspend(upb_json_parser *p, const char **ptr) { + if (!p->capture) return; -/* Base64 decoding ************************************************************/ - -/* TODO(haberman): make this streaming. */ - -static const signed char b64table[] = { - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -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*/, - 07/*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 -}; - -/* Returns the table value sign-extended to 32 bits. Knowing that the upper - * bits will be 1 for unrecognized characters makes it easier to check for - * this error condition later (see below). */ -int32_t b64lookup(unsigned char ch) { return b64table[ch]; } - -/* Returns true if the given character is not a valid base64 character or - * padding. */ -bool nonbase64(unsigned char ch) { return b64lookup(ch) == -1 && ch != '='; } + if (multipart_text(p, p->capture, *ptr - p->capture, false)) { + /* We use this as a signal that we were in the middle of capturing, and + * that capturing should resume at the beginning of the next buffer. + * + * We can't use *ptr here, because we have no guarantee that this pointer + * will be valid when we resume (if the underlying memory is freed, then + * using the pointer at all, even to compare to NULL, is likely undefined + * behavior). */ + p->capture = &suspend_capture; + } else { + /* Need to back up the pointer to the beginning of the capture, since + * we were not able to actually preserve it. */ + *ptr = p->capture; + } +} -static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, - size_t len) { - const char *limit = ptr + len; - for (; ptr < limit; ptr += 4) { - uint32_t val; - char output[3]; +static void capture_resume(upb_json_parser *p, const char *ptr) { + if (p->capture) { + UPB_ASSERT(p->capture == &suspend_capture); + p->capture = ptr; + } +} - if (limit - ptr < 4) { - upb_status_seterrf(&p->status, - "Base64 input for bytes field not a multiple of 4: %s", - upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); - return false; - } - val = b64lookup(ptr[0]) << 18 | - b64lookup(ptr[1]) << 12 | - b64lookup(ptr[2]) << 6 | - b64lookup(ptr[3]); +/* Callbacks from the parser **************************************************/ - /* Test the upper bit; returns true if any of the characters returned -1. */ - if (val & 0x80000000) { - goto otherchar; - } +/* These are the functions called directly from the parser itself. + * We define these in the same order as their declarations in the parser. */ - output[0] = val >> 16; - output[1] = (val >> 8) & 0xff; - output[2] = val & 0xff; - upb_sink_putstring(&p->top->sink, sel, output, 3, NULL); +static char escape_char(char in) { + switch (in) { + case 'r': return '\r'; + case 't': return '\t'; + case 'n': return '\n'; + case 'f': return '\f'; + case 'b': return '\b'; + case '/': return '/'; + case '"': return '"'; + case '\\': return '\\'; + default: + UPB_ASSERT(0); + return 'x'; } - return true; +} -otherchar: - if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) || - nonbase64(ptr[3]) ) { - upb_status_seterrf(&p->status, - "Non-base64 characters in bytes field: %s", - upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); - return false; - } if (ptr[2] == '=') { - uint32_t val; - char output; +static bool escape(upb_json_parser *p, const char *ptr) { + char ch = escape_char(*ptr); + return multipart_text(p, &ch, 1, false); +} - /* Last group contains only two input bytes, one output byte. */ - if (ptr[0] == '=' || ptr[1] == '=' || ptr[3] != '=') { - goto badpadding; - } +static void start_hex(upb_json_parser *p) { + p->digit = 0; +} - val = b64lookup(ptr[0]) << 18 | - b64lookup(ptr[1]) << 12; +static void hexdigit(upb_json_parser *p, const char *ptr) { + char ch = *ptr; - UPB_ASSERT(!(val & 0x80000000)); - output = val >> 16; - upb_sink_putstring(&p->top->sink, sel, &output, 1, NULL); - return true; - } else { - uint32_t val; - char output[2]; + p->digit <<= 4; - /* Last group contains only three input bytes, two output bytes. */ - if (ptr[0] == '=' || ptr[1] == '=' || ptr[2] == '=') { - goto badpadding; - } + if (ch >= '0' && ch <= '9') { + p->digit += (ch - '0'); + } else if (ch >= 'a' && ch <= 'f') { + p->digit += ((ch - 'a') + 10); + } else { + UPB_ASSERT(ch >= 'A' && ch <= 'F'); + p->digit += ((ch - 'A') + 10); + } +} - val = b64lookup(ptr[0]) << 18 | - b64lookup(ptr[1]) << 12 | - b64lookup(ptr[2]) << 6; +static bool end_hex(upb_json_parser *p) { + uint32_t codepoint = p->digit; - output[0] = val >> 16; - output[1] = (val >> 8) & 0xff; - upb_sink_putstring(&p->top->sink, sel, output, 2, NULL); - return true; + /* emit the codepoint as UTF-8. */ + char utf8[3]; /* support \u0000 -- \uFFFF -- need only three bytes. */ + int length = 0; + if (codepoint <= 0x7F) { + utf8[0] = codepoint; + length = 1; + } else if (codepoint <= 0x07FF) { + utf8[1] = (codepoint & 0x3F) | 0x80; + codepoint >>= 6; + utf8[0] = (codepoint & 0x1F) | 0xC0; + length = 2; + } else /* codepoint <= 0xFFFF */ { + utf8[2] = (codepoint & 0x3F) | 0x80; + codepoint >>= 6; + utf8[1] = (codepoint & 0x3F) | 0x80; + codepoint >>= 6; + utf8[0] = (codepoint & 0x0F) | 0xE0; + length = 3; } + /* TODO(haberman): Handle high surrogates: if codepoint is a high surrogate + * we have to wait for the next escape to get the full code point). */ -badpadding: - upb_status_seterrf(&p->status, - "Incorrect base64 padding for field: %s (%.*s)", - upb_fielddef_name(p->top->f), - 4, ptr); - upb_env_reporterror(p->env, &p->status); - return false; + return multipart_text(p, utf8, length, false); } - -/* Accumulate buffer **********************************************************/ - -/* Functionality for accumulating a buffer. - * - * Some parts of the parser need an entire value as a contiguous string. For - * example, to look up a member name in a hash table, or to turn a string into - * a number, the relevant library routines need the input string to be in - * contiguous memory, even if the value spanned two or more buffers in the - * input. These routines handle that. - * - * In the common case we can just point to the input buffer to get this - * contiguous string and avoid any actual copy. So we optimistically begin - * this way. But there are a few cases where we must instead copy into a - * separate buffer: - * - * 1. The string was not contiguous in the input (it spanned buffers). - * - * 2. The string included escape sequences that need to be interpreted to get - * the true value in a contiguous buffer. */ - -static void assert_accumulate_empty(upb_json_parser *p) { - UPB_ASSERT(p->accumulated == NULL); - UPB_ASSERT(p->accumulated_len == 0); +static void start_text(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); } -static void accumulate_clear(upb_json_parser *p) { - p->accumulated = NULL; - p->accumulated_len = 0; +static bool end_text(upb_json_parser *p, const char *ptr) { + return capture_end(p, ptr); } -/* Used internally by accumulate_append(). */ -static bool accumulate_realloc(upb_json_parser *p, size_t need) { - void *mem; - size_t old_size = p->accumulate_buf_size; - size_t new_size = UPB_MAX(old_size, 128); - while (new_size < need) { - new_size = saturating_multiply(new_size, 2); - } - - mem = upb_env_realloc(p->env, p->accumulate_buf, old_size, new_size); - if (!mem) { - upb_status_seterrmsg(&p->status, "Out of memory allocating buffer."); - upb_env_reporterror(p->env, &p->status); - return false; +static bool start_number(upb_json_parser *p, const char *ptr) { + if (is_top_level(p)) { + if (is_number_wrapper_object(p)) { + start_wrapper_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_NUMBERVALUE); + } else { + return false; + } + } else if (does_number_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) { + return false; + } + start_value_object(p, VALUE_NUMBERVALUE); } - p->accumulate_buf = mem; - p->accumulate_buf_size = new_size; + multipart_startaccum(p); + capture_begin(p, ptr); return true; } -/* Logically appends the given data to the append buffer. - * If "can_alias" is true, we will try to avoid actually copying, but the buffer - * must be valid until the next accumulate_append() call (if any). */ -static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len, - bool can_alias) { - size_t need; +static bool parse_number(upb_json_parser *p, bool is_quoted); - if (!p->accumulated && can_alias) { - p->accumulated = buf; - p->accumulated_len = len; +static bool end_number_nontop(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + + if (p->top->f == NULL) { + multipart_end(p); return true; } - if (!checked_add(p->accumulated_len, len, &need)) { - upb_status_seterrmsg(&p->status, "Integer overflow."); - upb_env_reporterror(p->env, &p->status); + return parse_number(p, false); +} + +static bool end_number(upb_json_parser *p, const char *ptr) { + if (!end_number_nontop(p, ptr)) { return false; } - if (need > p->accumulate_buf_size && !accumulate_realloc(p, need)) { - return false; + if (does_number_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; } - if (p->accumulated != p->accumulate_buf) { - memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len); - p->accumulated = p->accumulate_buf; + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; } - memcpy(p->accumulate_buf + p->accumulated_len, buf, len); - p->accumulated_len += len; return true; } -/* Returns a pointer to the data accumulated since the last accumulate_clear() - * call, and writes the length to *len. This with point either to the input - * buffer or a temporary accumulate buffer. */ -static const char *accumulate_getptr(upb_json_parser *p, size_t *len) { - UPB_ASSERT(p->accumulated); - *len = p->accumulated_len; - return p->accumulated; -} - - -/* Mult-part text data ********************************************************/ - -/* When we have text data in the input, it can often come in multiple segments. - * For example, there may be some raw string data followed by an escape - * sequence. The two segments are processed with different logic. Also buffer - * seams in the input can cause multiple segments. - * - * As we see segments, there are two main cases for how we want to process them: - * - * 1. we want to push the captured input directly to string handlers. - * - * 2. we need to accumulate all the parts into a contiguous buffer for further - * processing (field name lookup, string->number conversion, etc). */ - -/* This is the set of states for p->multipart_state. */ -enum { - /* We are not currently processing multipart data. */ - MULTIPART_INACTIVE = 0, +/* |buf| is NULL-terminated. |buf| itself will never include quotes; + * |is_quoted| tells us whether this text originally appeared inside quotes. */ +static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, + bool is_quoted) { + size_t len = strlen(buf); + const char *bufend = buf + len; + char *end; + upb_fieldtype_t type = upb_fielddef_type(p->top->f); + double val; + double dummy; + double inf = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */ - /* We are processing multipart data by accumulating it into a contiguous - * buffer. */ - MULTIPART_ACCUMULATE = 1, + errno = 0; - /* We are processing multipart data by pushing each part directly to the - * current string handlers. */ - MULTIPART_PUSHEAGERLY = 2 -}; + if (len == 0 || buf[0] == ' ') { + return false; + } -/* Start a multi-part text value where we accumulate the data for processing at - * the end. */ -static void multipart_startaccum(upb_json_parser *p) { - assert_accumulate_empty(p); - UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE); - p->multipart_state = MULTIPART_ACCUMULATE; -} + /* For integer types, first try parsing with integer-specific routines. + * If these succeed, they will be more accurate for int64/uint64 than + * strtod(). + */ + switch (type) { + case UPB_TYPE_ENUM: + case UPB_TYPE_INT32: { + long val = strtol(buf, &end, 0); + if (errno == ERANGE || end != bufend) { + break; + } else if (val > INT32_MAX || val < INT32_MIN) { + return false; + } else { + upb_sink_putint32(p->top->sink, parser_getsel(p), val); + return true; + } + } + case UPB_TYPE_UINT32: { + unsigned long val = strtoul(buf, &end, 0); + if (end != bufend) { + break; + } else if (val > UINT32_MAX || errno == ERANGE) { + return false; + } else { + upb_sink_putuint32(p->top->sink, parser_getsel(p), val); + return true; + } + } + /* XXX: We can't handle [u]int64 properly on 32-bit machines because + * strto[u]ll isn't in C89. */ + case UPB_TYPE_INT64: { + long val = strtol(buf, &end, 0); + if (errno == ERANGE || end != bufend) { + break; + } else { + upb_sink_putint64(p->top->sink, parser_getsel(p), val); + return true; + } + } + case UPB_TYPE_UINT64: { + unsigned long val = strtoul(p->accumulated, &end, 0); + if (end != bufend) { + break; + } else if (errno == ERANGE) { + return false; + } else { + upb_sink_putuint64(p->top->sink, parser_getsel(p), val); + return true; + } + } + default: + break; + } -/* Start a multi-part text value where we immediately push text data to a string - * value with the given selector. */ -static void multipart_start(upb_json_parser *p, upb_selector_t sel) { - assert_accumulate_empty(p); - UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE); - p->multipart_state = MULTIPART_PUSHEAGERLY; - p->string_selector = sel; -} + if (type != UPB_TYPE_DOUBLE && type != UPB_TYPE_FLOAT && is_quoted) { + /* Quoted numbers for integer types are not allowed to be in double form. */ + return false; + } -static bool multipart_text(upb_json_parser *p, const char *buf, size_t len, - bool can_alias) { - switch (p->multipart_state) { - case MULTIPART_INACTIVE: - upb_status_seterrmsg( - &p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); - upb_env_reporterror(p->env, &p->status); + if (len == strlen("Infinity") && strcmp(buf, "Infinity") == 0) { + /* C89 does not have an INFINITY macro. */ + val = inf; + } else if (len == strlen("-Infinity") && strcmp(buf, "-Infinity") == 0) { + val = -inf; + } else { + val = strtod(buf, &end); + if (errno == ERANGE || end != bufend) { return false; + } + } + + switch (type) { +#define CASE(capitaltype, smalltype, ctype, min, max) \ + case UPB_TYPE_ ## capitaltype: { \ + if (modf(val, &dummy) != 0 || val > max || val < min) { \ + return false; \ + } else { \ + upb_sink_put ## smalltype(p->top->sink, parser_getsel(p), \ + (ctype)val); \ + return true; \ + } \ + break; \ + } + case UPB_TYPE_ENUM: + CASE(INT32, int32, int32_t, INT32_MIN, INT32_MAX); + CASE(INT64, int64, int64_t, INT64_MIN, INT64_MAX); + CASE(UINT32, uint32, uint32_t, 0, UINT32_MAX); + CASE(UINT64, uint64, uint64_t, 0, UINT64_MAX); +#undef CASE - case MULTIPART_ACCUMULATE: - if (!accumulate_append(p, buf, len, can_alias)) { + case UPB_TYPE_DOUBLE: + upb_sink_putdouble(p->top->sink, parser_getsel(p), val); + return true; + case UPB_TYPE_FLOAT: + if ((val > FLT_MAX || val < -FLT_MAX) && val != inf && val != -inf) { return false; + } else { + upb_sink_putfloat(p->top->sink, parser_getsel(p), val); + return true; } - break; + default: + return false; + } +} - case MULTIPART_PUSHEAGERLY: { - const upb_bufhandle *handle = can_alias ? p->handle : NULL; - upb_sink_putstring(&p->top->sink, p->string_selector, buf, len, handle); - break; - } +static bool parse_number(upb_json_parser *p, bool is_quoted) { + size_t len; + const char *buf; + + /* strtol() and friends unfortunately do not support specifying the length of + * the input string, so we need to force a copy into a NULL-terminated buffer. */ + if (!multipart_text(p, "\0", 1, false)) { + return false; } - return true; -} + buf = accumulate_getptr(p, &len); -/* Note: this invalidates the accumulate buffer! Call only after reading its - * contents. */ -static void multipart_end(upb_json_parser *p) { - UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE); - p->multipart_state = MULTIPART_INACTIVE; - accumulate_clear(p); + if (parse_number_from_buffer(p, buf, is_quoted)) { + multipart_end(p); + return true; + } else { + upb_status_seterrf(p->status, "error parsing number: %s", buf); + multipart_end(p); + return false; + } } +static bool parser_putbool(upb_json_parser *p, bool val) { + bool ok; -/* Input capture **************************************************************/ + if (p->top->f == NULL) { + return true; + } -/* Functionality for capturing a region of the input as text. Gracefully - * handles the case where a buffer seam occurs in the middle of the captured - * region. */ + if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) { + upb_status_seterrf(p->status, + "Boolean value specified for non-bool field: %s", + upb_fielddef_name(p->top->f)); + return false; + } -static void capture_begin(upb_json_parser *p, const char *ptr) { - UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE); - UPB_ASSERT(p->capture == NULL); - p->capture = ptr; + ok = upb_sink_putbool(p->top->sink, parser_getsel(p), val); + UPB_ASSERT(ok); + + return true; } -static bool capture_end(upb_json_parser *p, const char *ptr) { - UPB_ASSERT(p->capture); - if (multipart_text(p, p->capture, ptr - p->capture, true)) { - p->capture = NULL; +static bool end_bool(upb_json_parser *p, bool val) { + if (is_top_level(p)) { + if (is_wellknown_msg(p, UPB_WELLKNOWN_BOOLVALUE)) { + start_wrapper_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_BOOLVALUE); + } else { + return false; + } + } else if (is_wellknown_field(p, UPB_WELLKNOWN_BOOLVALUE)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) { + return false; + } + start_value_object(p, VALUE_BOOLVALUE); + } + + if (p->top->is_unknown_field) { return true; - } else { - return false; } -} -/* This is called at the end of each input buffer (ie. when we have hit a - * buffer seam). If we are in the middle of capturing the input, this - * processes the unprocessed capture region. */ -static void capture_suspend(upb_json_parser *p, const char **ptr) { - if (!p->capture) return; + if (!parser_putbool(p, val)) { + return false; + } - if (multipart_text(p, p->capture, *ptr - p->capture, false)) { - /* We use this as a signal that we were in the middle of capturing, and - * that capturing should resume at the beginning of the next buffer. - * - * We can't use *ptr here, because we have no guarantee that this pointer - * will be valid when we resume (if the underlying memory is freed, then - * using the pointer at all, even to compare to NULL, is likely undefined - * behavior). */ - p->capture = &suspend_capture; - } else { - /* Need to back up the pointer to the beginning of the capture, since - * we were not able to actually preserve it. */ - *ptr = p->capture; + if (is_wellknown_msg(p, UPB_WELLKNOWN_BOOLVALUE)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; } -} -static void capture_resume(upb_json_parser *p, const char *ptr) { - if (p->capture) { - UPB_ASSERT(p->capture == &suspend_capture); - p->capture = ptr; + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; } + + return true; } +static bool end_null(upb_json_parser *p) { + const char *zero_ptr = "0"; -/* Callbacks from the parser **************************************************/ + if (is_top_level(p)) { + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_NULLVALUE); + } else { + return true; + } + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) { + return false; + } + start_value_object(p, VALUE_NULLVALUE); + } else { + return true; + } -/* These are the functions called directly from the parser itself. - * We define these in the same order as their declarations in the parser. */ + /* Fill null_value field. */ + multipart_startaccum(p); + capture_begin(p, zero_ptr); + capture_end(p, zero_ptr + 1); + parse_number(p, false); -static char escape_char(char in) { - switch (in) { - case 'r': return '\r'; - case 't': return '\t'; - case 'n': return '\n'; - case 'f': return '\f'; - case 'b': return '\b'; - case '/': return '/'; - case '"': return '"'; - case '\\': return '\\'; - default: - UPB_ASSERT(0); - return 'x'; + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); } -} -static bool escape(upb_json_parser *p, const char *ptr) { - char ch = escape_char(*ptr); - return multipart_text(p, &ch, 1, false); + return true; } -static void start_hex(upb_json_parser *p) { - p->digit = 0; +static bool start_any_stringval(upb_json_parser *p) { + multipart_startaccum(p); + return true; } -static void hexdigit(upb_json_parser *p, const char *ptr) { - char ch = *ptr; +static bool start_stringval(upb_json_parser *p) { + if (is_top_level(p)) { + if (is_string_wrapper_object(p)) { + start_wrapper_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) { + start_fieldmask_object(p); + return true; + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) || + is_wellknown_msg(p, UPB_WELLKNOWN_DURATION)) { + start_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_STRINGVALUE); + } else { + return false; + } + } else if (does_string_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_fieldmask_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_fieldmask_object(p); + return true; + } else if (is_wellknown_field(p, UPB_WELLKNOWN_TIMESTAMP) || + is_wellknown_field(p, UPB_WELLKNOWN_DURATION)) { + if (!start_subobject(p)) { + return false; + } + start_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) { + return false; + } + start_value_object(p, VALUE_STRINGVALUE); + } - p->digit <<= 4; + if (p->top->f == NULL) { + multipart_startaccum(p); + return true; + } - if (ch >= '0' && ch <= '9') { - p->digit += (ch - '0'); - } else if (ch >= 'a' && ch <= 'f') { - p->digit += ((ch - 'a') + 10); - } else { - UPB_ASSERT(ch >= 'A' && ch <= 'F'); - p->digit += ((ch - 'A') + 10); + if (p->top->is_any) { + return start_any_stringval(p); } -} -static bool end_hex(upb_json_parser *p) { - uint32_t codepoint = p->digit; + if (upb_fielddef_isstring(p->top->f)) { + upb_jsonparser_frame *inner; + upb_selector_t sel; - /* emit the codepoint as UTF-8. */ - char utf8[3]; /* support \u0000 -- \uFFFF -- need only three bytes. */ - int length = 0; - if (codepoint <= 0x7F) { - utf8[0] = codepoint; - length = 1; - } else if (codepoint <= 0x07FF) { - utf8[1] = (codepoint & 0x3F) | 0x80; - codepoint >>= 6; - utf8[0] = (codepoint & 0x1F) | 0xC0; - length = 2; - } else /* codepoint <= 0xFFFF */ { - utf8[2] = (codepoint & 0x3F) | 0x80; - codepoint >>= 6; - utf8[1] = (codepoint & 0x3F) | 0x80; - codepoint >>= 6; - utf8[0] = (codepoint & 0x0F) | 0xE0; - length = 3; - } - /* TODO(haberman): Handle high surrogates: if codepoint is a high surrogate - * we have to wait for the next escape to get the full code point). */ + if (!check_stack(p)) return false; - return multipart_text(p, utf8, length, false); + /* Start a new parser frame: parser frames correspond one-to-one with + * handler frames, and string events occur in a sub-frame. */ + inner = start_jsonparser_frame(p); + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); + inner->m = p->top->m; + inner->f = p->top->f; + p->top = inner; + + if (upb_fielddef_type(p->top->f) == UPB_TYPE_STRING) { + /* For STRING fields we push data directly to the handlers as it is + * parsed. We don't do this yet for BYTES fields, because our base64 + * decoder is not streaming. + * + * TODO(haberman): make base64 decoding streaming also. */ + multipart_start(p, getsel_for_handlertype(p, UPB_HANDLER_STRING)); + return true; + } else { + multipart_startaccum(p); + return true; + } + } else if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL && + upb_fielddef_type(p->top->f) != UPB_TYPE_MESSAGE) { + /* No need to push a frame -- numeric values in quotes remain in the + * current parser frame. These values must accmulate so we can convert + * them all at once at the end. */ + multipart_startaccum(p); + return true; + } else { + upb_status_seterrf(p->status, + "String specified for bool or submessage field: %s", + upb_fielddef_name(p->top->f)); + return false; + } } -static void start_text(upb_json_parser *p, const char *ptr) { - capture_begin(p, ptr); -} +static bool end_any_stringval(upb_json_parser *p) { + size_t len; + const char *buf = accumulate_getptr(p, &len); -static bool end_text(upb_json_parser *p, const char *ptr) { - return capture_end(p, ptr); -} + /* Set type_url */ + upb_selector_t sel; + upb_jsonparser_frame *inner; + if (!check_stack(p)) return false; + inner = p->top + 1; -static bool start_number(upb_json_parser *p, const char *ptr) { - if (is_top_level(p)) { - if (!is_number_wrapper_object(p)) { - return false; - } - start_wrapper_object(p); - } else if (does_number_wrapper_start(p)) { - if (!start_subobject(p)) { + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); + sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); + upb_sink_putstring(inner->sink, sel, buf, len, NULL); + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(inner->sink, sel); + + multipart_end(p); + + /* Resolve type url */ + if (strncmp(buf, "type.googleapis.com/", 20) == 0 && len > 20) { + const upb_msgdef *payload_type = NULL; + buf += 20; + len -= 20; + + payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len); + if (payload_type == NULL) { + upb_status_seterrf( + p->status, "Cannot find packed type: %.*s\n", (int)len, buf); return false; } - start_wrapper_object(p); - } - multipart_startaccum(p); - capture_begin(p, ptr); - return true; + json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type); + + return true; + } else { + upb_status_seterrf( + p->status, "Invalid type url: %.*s\n", (int)len, buf); + return false; + } } -static bool parse_number(upb_json_parser *p, bool is_quoted); +static bool end_stringval_nontop(upb_json_parser *p) { + bool ok = true; -static bool end_number_nontop(upb_json_parser *p, const char *ptr) { - if (!capture_end(p, ptr)) { - return false; + if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) || + is_wellknown_msg(p, UPB_WELLKNOWN_DURATION)) { + multipart_end(p); + return true; } if (p->top->f == NULL) { @@ -14918,361 +10108,512 @@ static bool end_number_nontop(upb_json_parser *p, const char *ptr) { return true; } - return parse_number(p, false); + if (p->top->is_any) { + return end_any_stringval(p); + } + + switch (upb_fielddef_type(p->top->f)) { + case UPB_TYPE_BYTES: + if (!base64_push(p, getsel_for_handlertype(p, UPB_HANDLER_STRING), + p->accumulated, p->accumulated_len)) { + return false; + } + /* Fall through. */ + + case UPB_TYPE_STRING: { + upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(p->top->sink, sel); + p->top--; + break; + } + + case UPB_TYPE_ENUM: { + /* Resolve enum symbolic name to integer value. */ + const upb_enumdef *enumdef = upb_fielddef_enumsubdef(p->top->f); + + size_t len; + const char *buf = accumulate_getptr(p, &len); + + int32_t int_val = 0; + ok = upb_enumdef_ntoi(enumdef, buf, len, &int_val); + + if (ok) { + upb_selector_t sel = parser_getsel(p); + upb_sink_putint32(p->top->sink, sel, int_val); + } else { + upb_status_seterrf(p->status, "Enum value unknown: '%.*s'", len, buf); + } + + break; + } + + case UPB_TYPE_INT32: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT32: + case UPB_TYPE_UINT64: + case UPB_TYPE_DOUBLE: + case UPB_TYPE_FLOAT: + ok = parse_number(p, true); + break; + + default: + UPB_ASSERT(false); + upb_status_seterrmsg(p->status, "Internal error in JSON decoder"); + ok = false; + break; + } + + multipart_end(p); + + return ok; } -static bool end_number(upb_json_parser *p, const char *ptr) { - if (!end_number_nontop(p, ptr)) { +static bool end_stringval(upb_json_parser *p) { + /* FieldMask's stringvals have been ended when handling them. Only need to + * close FieldMask here.*/ + if (does_fieldmask_end(p)) { + end_fieldmask_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + if (!end_stringval_nontop(p)) { return false; } - if (does_number_wrapper_end(p)) { + if (does_string_wrapper_end(p)) { end_wrapper_object(p); if (!is_top_level(p)) { end_subobject(p); } + return true; + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) || + is_wellknown_msg(p, UPB_WELLKNOWN_DURATION) || + is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) { + end_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; } return true; } -/* |buf| is NULL-terminated. |buf| itself will never include quotes; - * |is_quoted| tells us whether this text originally appeared inside quotes. */ -static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, - bool is_quoted) { - size_t len = strlen(buf); - const char *bufend = buf + len; +static void start_duration_base(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_duration_base(upb_json_parser *p, const char *ptr) { + size_t len; + const char *buf; + char seconds_buf[14]; + char nanos_buf[12]; char *end; - upb_fieldtype_t type = upb_fielddef_type(p->top->f); - double val; - double dummy; - double inf = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */ + int64_t seconds = 0; + int32_t nanos = 0; + double val = 0.0; + const char *seconds_membername = "seconds"; + const char *nanos_membername = "nanos"; + size_t fraction_start; - errno = 0; + if (!capture_end(p, ptr)) { + return false; + } - if (len == 0 || buf[0] == ' ') { + buf = accumulate_getptr(p, &len); + + memset(seconds_buf, 0, 14); + memset(nanos_buf, 0, 12); + + /* Find out base end. The maximus duration is 315576000000, which cannot be + * represented by double without losing precision. Thus, we need to handle + * fraction and base separately. */ + for (fraction_start = 0; fraction_start < len && buf[fraction_start] != '.'; + fraction_start++); + + /* Parse base */ + memcpy(seconds_buf, buf, fraction_start); + seconds = strtol(seconds_buf, &end, 10); + if (errno == ERANGE || end != seconds_buf + fraction_start) { + upb_status_seterrf(p->status, "error parsing duration: %s", + seconds_buf); return false; } - /* For integer types, first try parsing with integer-specific routines. - * If these succeed, they will be more accurate for int64/uint64 than - * strtod(). - */ - switch (type) { - case UPB_TYPE_ENUM: - case UPB_TYPE_INT32: { - long val = strtol(buf, &end, 0); - if (errno == ERANGE || end != bufend) { - break; - } else if (val > INT32_MAX || val < INT32_MIN) { - return false; - } else { - upb_sink_putint32(&p->top->sink, parser_getsel(p), val); - return true; - } - } - case UPB_TYPE_UINT32: { - unsigned long val = strtoul(buf, &end, 0); - if (end != bufend) { - break; - } else if (val > UINT32_MAX || errno == ERANGE) { - return false; - } else { - upb_sink_putuint32(&p->top->sink, parser_getsel(p), val); - return true; - } - } - /* XXX: We can't handle [u]int64 properly on 32-bit machines because - * strto[u]ll isn't in C89. */ - case UPB_TYPE_INT64: { - long val = strtol(buf, &end, 0); - if (errno == ERANGE || end != bufend) { - break; - } else { - upb_sink_putint64(&p->top->sink, parser_getsel(p), val); - return true; - } - } - case UPB_TYPE_UINT64: { - unsigned long val = strtoul(p->accumulated, &end, 0); - if (end != bufend) { - break; - } else if (errno == ERANGE) { - return false; - } else { - upb_sink_putuint64(&p->top->sink, parser_getsel(p), val); - return true; - } - } - default: - break; + if (seconds > 315576000000) { + upb_status_seterrf(p->status, "error parsing duration: " + "maximum acceptable value is " + "315576000000"); + return false; } - if (type != UPB_TYPE_DOUBLE && type != UPB_TYPE_FLOAT && is_quoted) { - /* Quoted numbers for integer types are not allowed to be in double form. */ + if (seconds < -315576000000) { + upb_status_seterrf(p->status, "error parsing duration: " + "minimum acceptable value is " + "-315576000000"); return false; } - if (len == strlen("Infinity") && strcmp(buf, "Infinity") == 0) { - /* C89 does not have an INFINITY macro. */ - val = inf; - } else if (len == strlen("-Infinity") && strcmp(buf, "-Infinity") == 0) { - val = -inf; - } else { - val = strtod(buf, &end); - if (errno == ERANGE || end != bufend) { - return false; - } + /* Parse fraction */ + nanos_buf[0] = '0'; + memcpy(nanos_buf + 1, buf + fraction_start, len - fraction_start); + val = strtod(nanos_buf, &end); + if (errno == ERANGE || end != nanos_buf + len - fraction_start + 1) { + upb_status_seterrf(p->status, "error parsing duration: %s", + nanos_buf); + return false; } - switch (type) { -#define CASE(capitaltype, smalltype, ctype, min, max) \ - case UPB_TYPE_ ## capitaltype: { \ - if (modf(val, &dummy) != 0 || val > max || val < min) { \ - return false; \ - } else { \ - upb_sink_put ## smalltype(&p->top->sink, parser_getsel(p), \ - (ctype)val); \ - return true; \ - } \ - break; \ - } - case UPB_TYPE_ENUM: - CASE(INT32, int32, int32_t, INT32_MIN, INT32_MAX); - CASE(INT64, int64, int64_t, INT64_MIN, INT64_MAX); - CASE(UINT32, uint32, uint32_t, 0, UINT32_MAX); - CASE(UINT64, uint64, uint64_t, 0, UINT64_MAX); -#undef CASE + nanos = val * 1000000000; + if (seconds < 0) nanos = -nanos; - case UPB_TYPE_DOUBLE: - upb_sink_putdouble(&p->top->sink, parser_getsel(p), val); - return true; - case UPB_TYPE_FLOAT: - if ((val > FLT_MAX || val < -FLT_MAX) && val != inf && val != -inf) { - return false; - } else { - upb_sink_putfloat(&p->top->sink, parser_getsel(p), val); - return true; - } - default: - return false; + /* Clean up buffer */ + multipart_end(p); + + /* Set seconds */ + start_member(p); + capture_begin(p, seconds_membername); + capture_end(p, seconds_membername + 7); + end_membername(p); + upb_sink_putint64(p->top->sink, parser_getsel(p), seconds); + end_member(p); + + /* Set nanos */ + start_member(p); + capture_begin(p, nanos_membername); + capture_end(p, nanos_membername + 5); + end_membername(p); + upb_sink_putint32(p->top->sink, parser_getsel(p), nanos); + end_member(p); + + /* Continue previous arena */ + multipart_startaccum(p); + + return true; +} + +static int parse_timestamp_number(upb_json_parser *p) { + size_t len; + const char *buf; + char *end; + int val; + + /* atoi() and friends unfortunately do not support specifying the length of + * the input string, so we need to force a copy into a NULL-terminated buffer. */ + multipart_text(p, "\0", 1, false); + + buf = accumulate_getptr(p, &len); + val = atoi(buf); + multipart_end(p); + multipart_startaccum(p); + + return val; +} + +static void start_year(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_year(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; } + p->tm.tm_year = parse_timestamp_number(p) - 1900; + return true; } -static bool parse_number(upb_json_parser *p, bool is_quoted) { - size_t len; - const char *buf; +static void start_month(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} - /* strtol() and friends unfortunately do not support specifying the length of - * the input string, so we need to force a copy into a NULL-terminated buffer. */ - if (!multipart_text(p, "\0", 1, false)) { +static bool end_month(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { return false; } + p->tm.tm_mon = parse_timestamp_number(p) - 1; + return true; +} - buf = accumulate_getptr(p, &len); +static void start_day(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} - if (parse_number_from_buffer(p, buf, is_quoted)) { - multipart_end(p); - return true; - } else { - upb_status_seterrf(&p->status, "error parsing number: %s", buf); - upb_env_reporterror(p->env, &p->status); - multipart_end(p); +static bool end_day(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { return false; } + p->tm.tm_mday = parse_timestamp_number(p); + return true; } -static bool parser_putbool(upb_json_parser *p, bool val) { - bool ok; +static void start_hour(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} - if (p->top->f == NULL) { - return true; +static bool end_hour(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; } + p->tm.tm_hour = parse_timestamp_number(p); + return true; +} - if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) { - upb_status_seterrf(&p->status, - "Boolean value specified for non-bool field: %s", - upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); +static void start_minute(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_minute(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { return false; } + p->tm.tm_min = parse_timestamp_number(p); + return true; +} - ok = upb_sink_putbool(&p->top->sink, parser_getsel(p), val); - UPB_ASSERT(ok); +static void start_second(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} +static bool end_second(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_sec = parse_timestamp_number(p); return true; } -static bool end_bool(upb_json_parser *p, bool val) { - if (is_top_level(p)) { - if (!is_boolean_wrapper_object(p)) { - return false; - } - start_wrapper_object(p); - } else if (does_boolean_wrapper_start(p)) { - if (!start_subobject(p)) { - return false; - } - start_wrapper_object(p); +static void start_timestamp_base(upb_json_parser *p) { + memset(&p->tm, 0, sizeof(struct tm)); +} + +static void start_timestamp_fraction(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { + size_t len; + const char *buf; + char nanos_buf[12]; + char *end; + double val = 0.0; + int32_t nanos; + const char *nanos_membername = "nanos"; + + memset(nanos_buf, 0, 12); + + if (!capture_end(p, ptr)) { + return false; } - if (!parser_putbool(p, val)) { + buf = accumulate_getptr(p, &len); + + if (len > 10) { + upb_status_seterrf(p->status, + "error parsing timestamp: at most 9-digit fraction."); return false; } - if (does_boolean_wrapper_end(p)) { - end_wrapper_object(p); - if (!is_top_level(p)) { - end_subobject(p); - } + /* Parse nanos */ + nanos_buf[0] = '0'; + memcpy(nanos_buf + 1, buf, len); + val = strtod(nanos_buf, &end); + + if (errno == ERANGE || end != nanos_buf + len + 1) { + upb_status_seterrf(p->status, "error parsing timestamp nanos: %s", + nanos_buf); + return false; } + nanos = val * 1000000000; + + /* Clean up previous environment */ + multipart_end(p); + + /* Set nanos */ + start_member(p); + capture_begin(p, nanos_membername); + capture_end(p, nanos_membername + 5); + end_membername(p); + upb_sink_putint32(p->top->sink, parser_getsel(p), nanos); + end_member(p); + + /* Continue previous environment */ + multipart_startaccum(p); + return true; } -static bool start_stringval(upb_json_parser *p) { - if (is_top_level(p)) { - if (!is_string_wrapper_object(p)) { - return false; - } - start_wrapper_object(p); - } else if (does_string_wrapper_start(p)) { - if (!start_subobject(p)) { - return false; - } - start_wrapper_object(p); - } +static void start_timestamp_zone(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} - if (p->top->f == NULL) { - multipart_startaccum(p); - return true; - } +#define EPOCH_YEAR 1970 +#define TM_YEAR_BASE 1900 - if (upb_fielddef_isstring(p->top->f)) { - upb_jsonparser_frame *inner; - upb_selector_t sel; +static bool isleap(int year) { + return (year % 4) == 0 && (year % 100 != 0 || (year % 400) == 0); +} - if (!check_stack(p)) return false; +const unsigned short int __mon_yday[2][13] = { + /* Normal years. */ + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, + /* Leap years. */ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } +}; - /* Start a new parser frame: parser frames correspond one-to-one with - * handler frames, and string events occur in a sub-frame. */ - inner = p->top + 1; - sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); - inner->m = p->top->m; - inner->f = p->top->f; - inner->name_table = NULL; - inner->is_map = false; - inner->is_mapentry = false; - p->top = inner; +int64_t epoch(int year, int yday, int hour, int min, int sec) { + int64_t years = year - EPOCH_YEAR; - if (upb_fielddef_type(p->top->f) == UPB_TYPE_STRING) { - /* For STRING fields we push data directly to the handlers as it is - * parsed. We don't do this yet for BYTES fields, because our base64 - * decoder is not streaming. - * - * TODO(haberman): make base64 decoding streaming also. */ - multipart_start(p, getsel_for_handlertype(p, UPB_HANDLER_STRING)); - return true; - } else { - multipart_startaccum(p); - return true; - } - } else if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL && - upb_fielddef_type(p->top->f) != UPB_TYPE_MESSAGE) { - /* No need to push a frame -- numeric values in quotes remain in the - * current parser frame. These values must accmulate so we can convert - * them all at once at the end. */ - multipart_startaccum(p); - return true; - } else { - upb_status_seterrf(&p->status, - "String specified for bool or submessage field: %s", - upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); - return false; - } + int64_t leap_days = years / 4 - years / 100 + years / 400; + + int64_t days = years * 365 + yday + leap_days; + int64_t hours = days * 24 + hour; + int64_t mins = hours * 60 + min; + int64_t secs = mins * 60 + sec; + return secs; } -static bool end_stringval_nontop(upb_json_parser *p) { - bool ok = true; - if (p->top->f == NULL) { - multipart_end(p); - return true; - } +static int64_t upb_mktime(const struct tm *tp) { + int sec = tp->tm_sec; + int min = tp->tm_min; + int hour = tp->tm_hour; + int mday = tp->tm_mday; + int mon = tp->tm_mon; + int year = tp->tm_year + TM_YEAR_BASE; - switch (upb_fielddef_type(p->top->f)) { - case UPB_TYPE_BYTES: - if (!base64_push(p, getsel_for_handlertype(p, UPB_HANDLER_STRING), - p->accumulated, p->accumulated_len)) { - return false; - } - /* Fall through. */ + /* Calculate day of year from year, month, and day of month. */ + int mon_yday = ((__mon_yday[isleap(year)][mon]) - 1); + int yday = mon_yday + mday; - case UPB_TYPE_STRING: { - upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - p->top--; - upb_sink_endstr(&p->top->sink, sel); - break; - } + return epoch(year, yday, hour, min, sec); +} - case UPB_TYPE_ENUM: { - /* Resolve enum symbolic name to integer value. */ - const upb_enumdef *enumdef = - (const upb_enumdef*)upb_fielddef_subdef(p->top->f); +static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { + size_t len; + const char *buf; + int hours; + int64_t seconds; + const char *seconds_membername = "seconds"; - size_t len; - const char *buf = accumulate_getptr(p, &len); + if (!capture_end(p, ptr)) { + return false; + } - int32_t int_val = 0; - ok = upb_enumdef_ntoi(enumdef, buf, len, &int_val); + buf = accumulate_getptr(p, &len); - if (ok) { - upb_selector_t sel = parser_getsel(p); - upb_sink_putint32(&p->top->sink, sel, int_val); - } else { - upb_status_seterrf(&p->status, "Enum value unknown: '%.*s'", len, buf); - upb_env_reporterror(p->env, &p->status); - } + if (buf[0] != 'Z') { + if (sscanf(buf + 1, "%2d:00", &hours) != 1) { + upb_status_seterrf(p->status, "error parsing timestamp offset"); + return false; + } - break; + if (buf[0] == '+') { + hours = -hours; } - case UPB_TYPE_INT32: - case UPB_TYPE_INT64: - case UPB_TYPE_UINT32: - case UPB_TYPE_UINT64: - case UPB_TYPE_DOUBLE: - case UPB_TYPE_FLOAT: - ok = parse_number(p, true); - break; + p->tm.tm_hour += hours; + } - default: - UPB_ASSERT(false); - upb_status_seterrmsg(&p->status, "Internal error in JSON decoder"); - upb_env_reporterror(p->env, &p->status); - ok = false; - break; + /* Normalize tm */ + seconds = upb_mktime(&p->tm); + + /* Check timestamp boundary */ + if (seconds < -62135596800) { + upb_status_seterrf(p->status, "error parsing timestamp: " + "minimum acceptable value is " + "0001-01-01T00:00:00Z"); + return false; } + /* Clean up previous environment */ multipart_end(p); - return ok; + /* Set seconds */ + start_member(p); + capture_begin(p, seconds_membername); + capture_end(p, seconds_membername + 7); + end_membername(p); + upb_sink_putint64(p->top->sink, parser_getsel(p), seconds); + end_member(p); + + /* Continue previous environment */ + multipart_startaccum(p); + + return true; } -static bool end_stringval(upb_json_parser *p) { - if (!end_stringval_nontop(p)) { - return false; - } +static void start_fieldmask_path_text(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} - if (does_string_wrapper_end(p)) { - end_wrapper_object(p); - if (!is_top_level(p)) { - end_subobject(p); +static bool end_fieldmask_path_text(upb_json_parser *p, const char *ptr) { + return capture_end(p, ptr); +} + +static bool start_fieldmask_path(upb_json_parser *p) { + upb_jsonparser_frame *inner; + upb_selector_t sel; + + if (!check_stack(p)) return false; + + /* Start a new parser frame: parser frames correspond one-to-one with + * handler frames, and string events occur in a sub-frame. */ + inner = start_jsonparser_frame(p); + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); + inner->m = p->top->m; + inner->f = p->top->f; + p->top = inner; + + multipart_startaccum(p); + return true; +} + +static bool lower_camel_push( + upb_json_parser *p, upb_selector_t sel, const char *ptr, size_t len) { + const char *limit = ptr + len; + bool first = true; + for (;ptr < limit; ptr++) { + if (*ptr >= 'A' && *ptr <= 'Z' && !first) { + char lower = tolower(*ptr); + upb_sink_putstring(p->top->sink, sel, "_", 1, NULL); + upb_sink_putstring(p->top->sink, sel, &lower, 1, NULL); + } else { + upb_sink_putstring(p->top->sink, sel, ptr, 1, NULL); } + first = false; + } + return true; +} + +static bool end_fieldmask_path(upb_json_parser *p) { + upb_selector_t sel; + + if (!lower_camel_push( + p, getsel_for_handlertype(p, UPB_HANDLER_STRING), + p->accumulated, p->accumulated_len)) { + return false; } + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(p->top->sink, sel); + p->top--; + + multipart_end(p); return true; } @@ -15296,8 +10637,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_KEY); if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no key"); return false; } switch (upb_fielddef_type(p->top->f)) { @@ -15320,9 +10660,8 @@ static bool parse_mapentry_key(upb_json_parser *p) { return false; } } else { - upb_status_seterrmsg(&p->status, + upb_status_seterrmsg(p->status, "Map bool key not 'true' or 'false'"); - upb_env_reporterror(p->env, &p->status); return false; } multipart_end(p); @@ -15331,17 +10670,16 @@ static bool parse_mapentry_key(upb_json_parser *p) { case UPB_TYPE_BYTES: { upb_sink subsink; upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, len, &subsink); + upb_sink_startstr(p->top->sink, sel, len, &subsink); sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); - upb_sink_putstring(&subsink, sel, buf, len, NULL); + upb_sink_putstring(subsink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&p->top->sink, sel); + upb_sink_endstr(subsink, sel); multipart_end(p); break; } default: - upb_status_seterrmsg(&p->status, "Invalid field type for map key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid field type for map key"); return false; } @@ -15371,14 +10709,12 @@ static bool handle_mapentry(upb_json_parser *p) { mapfield = p->top->mapfield; mapentrymsg = upb_fielddef_msgsubdef(mapfield); - inner = p->top + 1; + inner = start_jsonparser_frame(p); p->top->f = mapfield; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); - upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink); + upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = mapentrymsg; - inner->name_table = NULL; inner->mapfield = mapfield; - inner->is_map = false; /* Don't set this to true *yet* -- we reuse parsing handlers below to push * the key field value to the sink, and these handlers will pop the frame @@ -15388,7 +10724,7 @@ static bool handle_mapentry(upb_json_parser *p) { p->top = inner; /* send STARTMSG in submsg frame. */ - upb_sink_startmsg(&p->top->sink); + upb_sink_startmsg(p->top->sink); parse_mapentry_key(p); @@ -15397,8 +10733,7 @@ static bool handle_mapentry(upb_json_parser *p) { p->top->is_mapentry = true; /* set up to pop frame after value is parsed. */ p->top->mapfield = mapfield; if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no value"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no value"); return false; } @@ -15409,10 +10744,14 @@ static bool end_membername(upb_json_parser *p) { UPB_ASSERT(!p->top->f); if (!p->top->m) { + p->top->is_unknown_field = true; + multipart_end(p); return true; } - if (p->top->is_map) { + if (p->top->is_any) { + return end_any_membername(p); + } else if (p->top->is_map) { return handle_mapentry(p); } else { size_t len; @@ -15425,54 +10764,72 @@ static bool end_membername(upb_json_parser *p) { return true; } else if (p->ignore_json_unknown) { + p->top->is_unknown_field = true; multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "No such field: %.*s\n", (int)len, buf); return false; } } } +static bool end_any_membername(upb_json_parser *p) { + size_t len; + const char *buf = accumulate_getptr(p, &len); + upb_value v; + + if (len == 5 && strncmp(buf, "@type", len) == 0) { + upb_strtable_lookup2(p->top->name_table, "type_url", 8, &v); + p->top->f = upb_value_getconstptr(v); + multipart_end(p); + return true; + } else { + p->top->is_unknown_field = true; + multipart_end(p); + return true; + } +} + static void end_member(upb_json_parser *p) { /* If we just parsed a map-entry value, end that frame too. */ if (p->top->is_mapentry) { - upb_status s = UPB_STATUS_INIT; upb_selector_t sel; bool ok; const upb_fielddef *mapfield; UPB_ASSERT(p->top > p->stack); /* send ENDMSG on submsg. */ - upb_sink_endmsg(&p->top->sink, &s); + upb_sink_endmsg(p->top->sink, p->status); mapfield = p->top->mapfield; /* send ENDSUBMSG in repeated-field-of-mapentries frame. */ p->top--; ok = upb_handlers_getselector(mapfield, UPB_HANDLER_ENDSUBMSG, &sel); UPB_ASSERT(ok); - upb_sink_endsubmsg(&p->top->sink, sel); + upb_sink_endsubmsg(p->top->sink, sel); } p->top->f = NULL; + p->top->is_unknown_field = false; } -static bool start_subobject(upb_json_parser *p) { - if (is_top_level(p)) { - return true; - } +static void start_any_member(upb_json_parser *p, const char *ptr) { + start_member(p); + json_parser_any_frame_set_after_type_url_start_once(p->top->any_frame, ptr); +} - if (p->top->f == NULL) { +static void end_any_member(upb_json_parser *p, const char *ptr) { + json_parser_any_frame_set_before_type_url_end(p->top->any_frame, ptr); + end_member(p); +} + +static bool start_subobject(upb_json_parser *p) { + if (p->top->is_unknown_field) { upb_jsonparser_frame *inner; if (!check_stack(p)) return false; - inner = p->top + 1; - inner->m = NULL; - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; - p->top = inner; + p->top = start_jsonparser_frame(p); return true; } @@ -15484,15 +10841,12 @@ static bool start_subobject(upb_json_parser *p) { * context. */ if (!check_stack(p)) return false; - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); - upb_sink_startseq(&p->top->sink, sel, &inner->sink); + upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); - inner->name_table = NULL; inner->mapfield = p->top->f; - inner->f = NULL; inner->is_map = true; - inner->is_mapentry = false; p->top = inner; return true; @@ -15504,27 +10858,54 @@ static bool start_subobject(upb_json_parser *p) { * context. */ if (!check_stack(p)) return false; - inner = p->top + 1; - + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); - upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink); + upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); set_name_table(p, inner); - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; p->top = inner; + if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { + p->top->is_any = true; + p->top->any_frame = json_parser_any_frame_new(p); + } else { + p->top->is_any = false; + p->top->any_frame = NULL; + } + return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Object specified for non-message/group field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } +static bool start_subobject_full(upb_json_parser *p) { + if (is_top_level(p)) { + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_STRUCTVALUE); + if (!start_subobject(p)) return false; + start_structvalue_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_STRUCT)) { + start_structvalue_object(p); + } else { + return true; + } + } else if (is_wellknown_field(p, UPB_WELLKNOWN_STRUCT)) { + if (!start_subobject(p)) return false; + start_structvalue_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) return false; + start_value_object(p, VALUE_STRUCTVALUE); + if (!start_subobject(p)) return false; + start_structvalue_object(p); + } + + return start_subobject(p); +} + static void end_subobject(upb_json_parser *p) { if (is_top_level(p)) { return; @@ -15534,14 +10915,32 @@ static void end_subobject(upb_json_parser *p) { upb_selector_t sel; p->top--; sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ); - upb_sink_endseq(&p->top->sink, sel); + upb_sink_endseq(p->top->sink, sel); } else { upb_selector_t sel; bool is_unknown = p->top->m == NULL; p->top--; if (!is_unknown) { sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG); - upb_sink_endsubmsg(&p->top->sink, sel); + upb_sink_endsubmsg(p->top->sink, sel); + } + } +} + +static void end_subobject_full(upb_json_parser *p) { + end_subobject(p); + + if (is_wellknown_msg(p, UPB_WELLKNOWN_STRUCT)) { + end_structvalue_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); } } } @@ -15550,26 +10949,53 @@ static bool start_array(upb_json_parser *p) { upb_jsonparser_frame *inner; upb_selector_t sel; - UPB_ASSERT(p->top->f); + if (is_top_level(p)) { + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_LISTVALUE); + if (!start_subobject(p)) return false; + start_listvalue_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) { + start_listvalue_object(p); + } else { + return false; + } + } else if (is_wellknown_field(p, UPB_WELLKNOWN_LISTVALUE) && + (!upb_fielddef_isseq(p->top->f) || + p->top->is_repeated)) { + if (!start_subobject(p)) return false; + start_listvalue_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE) && + (!upb_fielddef_isseq(p->top->f) || + p->top->is_repeated)) { + if (!start_subobject(p)) return false; + start_value_object(p, VALUE_LISTVALUE); + if (!start_subobject(p)) return false; + start_listvalue_object(p); + } + + if (p->top->is_unknown_field) { + inner = start_jsonparser_frame(p); + inner->is_unknown_field = true; + p->top = inner; + + return true; + } if (!upb_fielddef_isseq(p->top->f)) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Array specified for non-repeated field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } if (!check_stack(p)) return false; - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); - upb_sink_startseq(&p->top->sink, sel, &inner->sink); + upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = p->top->m; - inner->name_table = NULL; inner->f = p->top->f; - inner->is_map = false; - inner->is_mapentry = false; + inner->is_repeated = true; p->top = inner; return true; @@ -15581,110 +11007,313 @@ static void end_array(upb_json_parser *p) { UPB_ASSERT(p->top > p->stack); p->top--; + + if (p->top->is_unknown_field) { + return; + } + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ); - upb_sink_endseq(&p->top->sink, sel); + upb_sink_endseq(p->top->sink, sel); + + if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) { + end_listvalue_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } } static void start_object(upb_json_parser *p) { - if (!p->top->is_map) { - upb_sink_startmsg(&p->top->sink); + if (!p->top->is_map && p->top->m != NULL) { + upb_sink_startmsg(p->top->sink); } } static void end_object(upb_json_parser *p) { - if (!p->top->is_map) { - upb_status status; - upb_status_clear(&status); - upb_sink_endmsg(&p->top->sink, &status); - if (!upb_ok(&status)) { - upb_env_reporterror(p->env, &status); - } + if (!p->top->is_map && p->top->m != NULL) { + upb_sink_endmsg(p->top->sink, p->status); } } -static bool is_double_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kDoubleValueFullMessageName) == 0; +static void start_any_object(upb_json_parser *p, const char *ptr) { + start_object(p); + p->top->any_frame->before_type_url_start = ptr; + p->top->any_frame->before_type_url_end = ptr; +} + +static bool end_any_object(upb_json_parser *p, const char *ptr) { + const char *value_membername = "value"; + bool is_well_known_packed = false; + const char *packed_end = ptr + 1; + upb_selector_t sel; + upb_jsonparser_frame *inner; + + if (json_parser_any_frame_has_value(p->top->any_frame) && + !json_parser_any_frame_has_type_url(p->top->any_frame)) { + upb_status_seterrmsg(p->status, "No valid type url"); + return false; + } + + /* Well known types data is represented as value field. */ + if (upb_msgdef_wellknowntype(p->top->any_frame->parser->top->m) != + UPB_WELLKNOWN_UNSPECIFIED) { + is_well_known_packed = true; + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) { + p->top->any_frame->before_type_url_start = + memchr(p->top->any_frame->before_type_url_start, ':', + p->top->any_frame->before_type_url_end - + p->top->any_frame->before_type_url_start); + if (p->top->any_frame->before_type_url_start == NULL) { + upb_status_seterrmsg(p->status, "invalid data for well known type."); + return false; + } + p->top->any_frame->before_type_url_start++; + } + + if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + p->top->any_frame->after_type_url_start = + memchr(p->top->any_frame->after_type_url_start, ':', + (ptr + 1) - + p->top->any_frame->after_type_url_start); + if (p->top->any_frame->after_type_url_start == NULL) { + upb_status_seterrmsg(p->status, "Invalid data for well known type."); + return false; + } + p->top->any_frame->after_type_url_start++; + packed_end = ptr; + } + } + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, + p->top->any_frame->before_type_url_start, + p->top->any_frame->before_type_url_end - + p->top->any_frame->before_type_url_start, NULL)) { + return false; + } + } else { + if (!is_well_known_packed) { + if (!parse(p->top->any_frame->parser, NULL, "{", 1, NULL)) { + return false; + } + } + } + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame) && + json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, ",", 1, NULL)) { + return false; + } + } + + if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, + p->top->any_frame->after_type_url_start, + packed_end - p->top->any_frame->after_type_url_start, NULL)) { + return false; + } + } else { + if (!is_well_known_packed) { + if (!parse(p->top->any_frame->parser, NULL, "}", 1, NULL)) { + return false; + } + } + } + + if (!end(p->top->any_frame->parser, NULL)) { + return false; + } + + p->top->is_any = false; + + /* Set value */ + start_member(p); + capture_begin(p, value_membername); + capture_end(p, value_membername + 5); + end_membername(p); + + if (!check_stack(p)) return false; + inner = p->top + 1; + + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); + sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); + upb_sink_putstring(inner->sink, sel, p->top->any_frame->stringsink.ptr, + p->top->any_frame->stringsink.len, NULL); + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(inner->sink, sel); + + end_member(p); + + end_object(p); + + /* Deallocate any parse frame. */ + json_parser_any_frame_free(p->top->any_frame); + + return true; } -static bool is_float_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kFloatValueFullMessageName) == 0; +static bool is_string_wrapper(const upb_msgdef *m) { + upb_wellknowntype_t type = upb_msgdef_wellknowntype(m); + return type == UPB_WELLKNOWN_STRINGVALUE || + type == UPB_WELLKNOWN_BYTESVALUE; } -static bool is_int64_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kInt64ValueFullMessageName) == 0; +static bool is_fieldmask(const upb_msgdef *m) { + upb_wellknowntype_t type = upb_msgdef_wellknowntype(m); + return type == UPB_WELLKNOWN_FIELDMASK; } -static bool is_uint64_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kUInt64ValueFullMessageName) == 0; +static void start_fieldmask_object(upb_json_parser *p) { + const char *membername = "paths"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + 5); + end_membername(p); + + start_array(p); } -static bool is_int32_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kInt32ValueFullMessageName) == 0; +static void end_fieldmask_object(upb_json_parser *p) { + end_array(p); + end_member(p); + end_object(p); } -static bool is_uint32_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kUInt32ValueFullMessageName) == 0; +static void start_wrapper_object(upb_json_parser *p) { + const char *membername = "value"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + 5); + end_membername(p); } -static bool is_bool_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kBoolValueFullMessageName) == 0; +static void end_wrapper_object(upb_json_parser *p) { + end_member(p); + end_object(p); } -static bool is_string_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kStringValueFullMessageName) == 0; +static void start_value_object(upb_json_parser *p, int value_type) { + const char *nullmember = "null_value"; + const char *numbermember = "number_value"; + const char *stringmember = "string_value"; + const char *boolmember = "bool_value"; + const char *structmember = "struct_value"; + const char *listmember = "list_value"; + const char *membername = ""; + + switch (value_type) { + case VALUE_NULLVALUE: + membername = nullmember; + break; + case VALUE_NUMBERVALUE: + membername = numbermember; + break; + case VALUE_STRINGVALUE: + membername = stringmember; + break; + case VALUE_BOOLVALUE: + membername = boolmember; + break; + case VALUE_STRUCTVALUE: + membername = structmember; + break; + case VALUE_LISTVALUE: + membername = listmember; + break; + } + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + strlen(membername)); + end_membername(p); } -static bool is_bytes_value(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), kBytesValueFullMessageName) == 0; +static void end_value_object(upb_json_parser *p) { + end_member(p); + end_object(p); } -static bool is_number_wrapper(const upb_msgdef *m) { - return is_double_value(m) || - is_float_value(m) || - is_int64_value(m) || - is_uint64_value(m) || - is_int32_value(m) || - is_uint32_value(m); +static void start_listvalue_object(upb_json_parser *p) { + const char *membername = "values"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + strlen(membername)); + end_membername(p); } -static bool is_string_wrapper(const upb_msgdef *m) { - return is_string_value(m) || - is_bytes_value(m); +static void end_listvalue_object(upb_json_parser *p) { + end_member(p); + end_object(p); } -static void start_wrapper_object(upb_json_parser *p) { - const char *membername = "value"; +static void start_structvalue_object(upb_json_parser *p) { + const char *membername = "fields"; start_object(p); /* Set up context for parsing value */ start_member(p); capture_begin(p, membername); - capture_end(p, membername + 5); + capture_end(p, membername + strlen(membername)); end_membername(p); } -static void end_wrapper_object(upb_json_parser *p) { +static void end_structvalue_object(upb_json_parser *p) { end_member(p); end_object(p); } static bool is_top_level(upb_json_parser *p) { - return p->top == p->stack && p->top->f == NULL; + return p->top == p->stack && p->top->f == NULL && !p->top->is_unknown_field; +} + +static bool is_wellknown_msg(upb_json_parser *p, upb_wellknowntype_t type) { + return p->top->m != NULL && upb_msgdef_wellknowntype(p->top->m) == type; +} + +static bool is_wellknown_field(upb_json_parser *p, upb_wellknowntype_t type) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + (upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(p->top->f)) + == type); } static bool does_number_wrapper_start(upb_json_parser *p) { return p->top->f != NULL && upb_fielddef_issubmsg(p->top->f) && - is_number_wrapper(upb_fielddef_msgsubdef(p->top->f)); + upb_msgdef_isnumberwrapper(upb_fielddef_msgsubdef(p->top->f)); } static bool does_number_wrapper_end(upb_json_parser *p) { - return p->top->m != NULL && is_number_wrapper(p->top->m); + return p->top->m != NULL && upb_msgdef_isnumberwrapper(p->top->m); } static bool is_number_wrapper_object(upb_json_parser *p) { - return p->top->m != NULL && is_number_wrapper(p->top->m); + return p->top->m != NULL && upb_msgdef_isnumberwrapper(p->top->m); } static bool does_string_wrapper_start(upb_json_parser *p) { @@ -15701,18 +11330,14 @@ static bool is_string_wrapper_object(upb_json_parser *p) { return p->top->m != NULL && is_string_wrapper(p->top->m); } -static bool does_boolean_wrapper_start(upb_json_parser *p) { +static bool does_fieldmask_start(upb_json_parser *p) { return p->top->f != NULL && upb_fielddef_issubmsg(p->top->f) && - is_bool_value(upb_fielddef_msgsubdef(p->top->f)); -} - -static bool does_boolean_wrapper_end(upb_json_parser *p) { - return p->top->m != NULL && is_bool_value(p->top->m); + is_fieldmask(upb_fielddef_msgsubdef(p->top->f)); } -static bool is_boolean_wrapper_object(upb_json_parser *p) { - return p->top->m != NULL && is_bool_value(p->top->m); +static bool does_fieldmask_end(upb_json_parser *p) { + return p->top->m != NULL && is_fieldmask(p->top->m); } #define CHECK_RETURN_TOP(x) if (!(x)) goto error @@ -15736,37 +11361,48 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 1589 "upb/json/parser.rl" +#line 2749 "upb/json/parser.rl" -#line 1496 "upb/json/parser.c" +#line 2552 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, - 9, 1, 11, 1, 13, 1, 14, 1, - 15, 1, 16, 1, 17, 1, 18, 1, - 20, 1, 22, 1, 23, 1, 24, 1, - 25, 1, 26, 1, 27, 1, 28, 2, + 9, 1, 11, 1, 12, 1, 13, 1, + 14, 1, 15, 1, 16, 1, 17, 1, + 18, 1, 19, 1, 20, 1, 22, 1, + 23, 1, 24, 1, 35, 1, 37, 1, + 39, 1, 40, 1, 42, 1, 43, 1, + 44, 1, 46, 1, 48, 1, 49, 1, + 50, 1, 51, 1, 53, 1, 54, 2, 4, 9, 2, 5, 6, 2, 7, 3, - 2, 7, 9, 2, 12, 10, 2, 14, - 16, 2, 15, 16, 2, 19, 2, 2, - 20, 28, 2, 21, 10, 2, 23, 28, - 2, 24, 28, 2, 25, 28, 2, 27, - 28, 3, 15, 12, 10 + 2, 7, 9, 2, 21, 26, 2, 25, + 10, 2, 27, 28, 2, 29, 30, 2, + 32, 34, 2, 33, 31, 2, 38, 36, + 2, 40, 42, 2, 45, 2, 2, 46, + 54, 2, 47, 36, 2, 49, 54, 2, + 50, 54, 2, 51, 54, 2, 52, 41, + 2, 53, 54, 3, 32, 34, 35, 4, + 21, 26, 27, 28 }; -static const unsigned char _json_key_offsets[] = { +static const short _json_key_offsets[] = { 0, 0, 12, 13, 18, 23, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, - 38, 43, 48, 49, 53, 58, 63, 68, - 72, 76, 79, 82, 84, 88, 92, 94, - 96, 101, 103, 105, 114, 120, 126, 132, - 138, 140, 149, 150, 150, 150, 155, 160, - 165, 166, 167, 168, 169, 169, 170, 171, - 172, 172, 173, 174, 175, 175, 180, 185, - 186, 190, 195, 200, 205, 209, 209, 212, - 215, 218, 221, 224, 227, 227, 227 + 38, 43, 44, 48, 53, 58, 63, 67, + 71, 74, 77, 79, 83, 87, 89, 91, + 96, 98, 100, 109, 115, 121, 127, 133, + 135, 139, 142, 144, 146, 149, 150, 154, + 156, 158, 160, 162, 163, 165, 167, 168, + 170, 172, 173, 175, 177, 178, 180, 182, + 183, 185, 187, 191, 193, 195, 196, 197, + 198, 199, 201, 206, 208, 210, 212, 221, + 222, 222, 222, 227, 232, 237, 238, 239, + 240, 241, 241, 242, 243, 244, 244, 245, + 246, 247, 247, 252, 253, 257, 262, 267, + 272, 276, 276, 279, 282, 285, 288, 291, + 294, 294, 294, 294, 294, 294 }; static const char _json_trans_keys[] = { @@ -15775,165 +11411,216 @@ static const char _json_trans_keys[] = { 9, 13, 32, 44, 93, 9, 13, 32, 93, 125, 9, 13, 97, 108, 115, 101, 117, 108, 108, 114, 117, 101, 32, 34, - 125, 9, 13, 32, 34, 125, 9, 13, - 34, 32, 58, 9, 13, 32, 93, 125, + 125, 9, 13, 34, 32, 58, 9, 13, + 32, 93, 125, 9, 13, 32, 44, 125, 9, 13, 32, 44, 125, 9, 13, 32, - 44, 125, 9, 13, 32, 34, 9, 13, - 45, 48, 49, 57, 48, 49, 57, 46, - 69, 101, 48, 57, 69, 101, 48, 57, - 43, 45, 48, 57, 48, 57, 48, 57, - 46, 69, 101, 48, 57, 34, 92, 34, - 92, 34, 47, 92, 98, 102, 110, 114, - 116, 117, 48, 57, 65, 70, 97, 102, - 48, 57, 65, 70, 97, 102, 48, 57, - 65, 70, 97, 102, 48, 57, 65, 70, - 97, 102, 34, 92, 34, 45, 91, 102, + 34, 9, 13, 45, 48, 49, 57, 48, + 49, 57, 46, 69, 101, 48, 57, 69, + 101, 48, 57, 43, 45, 48, 57, 48, + 57, 48, 57, 46, 69, 101, 48, 57, + 34, 92, 34, 92, 34, 47, 92, 98, + 102, 110, 114, 116, 117, 48, 57, 65, + 70, 97, 102, 48, 57, 65, 70, 97, + 102, 48, 57, 65, 70, 97, 102, 48, + 57, 65, 70, 97, 102, 34, 92, 45, + 48, 49, 57, 48, 49, 57, 46, 115, + 48, 57, 115, 48, 57, 34, 46, 115, + 48, 57, 48, 57, 48, 57, 48, 57, + 48, 57, 45, 48, 57, 48, 57, 45, + 48, 57, 48, 57, 84, 48, 57, 48, + 57, 58, 48, 57, 48, 57, 58, 48, + 57, 48, 57, 43, 45, 46, 90, 48, + 57, 48, 57, 58, 48, 48, 34, 48, + 57, 43, 45, 90, 48, 57, 34, 44, + 34, 44, 34, 44, 34, 45, 91, 102, 110, 116, 123, 48, 57, 34, 32, 93, 125, 9, 13, 32, 44, 93, 9, 13, 32, 93, 125, 9, 13, 97, 108, 115, 101, 117, 108, 108, 114, 117, 101, 32, - 34, 125, 9, 13, 32, 34, 125, 9, - 13, 34, 32, 58, 9, 13, 32, 93, + 34, 125, 9, 13, 34, 32, 58, 9, + 13, 32, 93, 125, 9, 13, 32, 44, 125, 9, 13, 32, 44, 125, 9, 13, - 32, 44, 125, 9, 13, 32, 34, 9, - 13, 32, 9, 13, 32, 9, 13, 32, + 32, 34, 9, 13, 32, 9, 13, 32, 9, 13, 32, 9, 13, 32, 9, 13, - 32, 9, 13, 0 + 32, 9, 13, 32, 9, 13, 0 }; static const char _json_single_lengths[] = { 0, 8, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 3, 3, 1, 2, 3, 3, 3, 2, - 2, 1, 3, 0, 2, 2, 0, 0, - 3, 2, 2, 9, 0, 0, 0, 0, - 2, 7, 1, 0, 0, 3, 3, 3, - 1, 1, 1, 1, 0, 1, 1, 1, - 0, 1, 1, 1, 0, 3, 3, 1, - 2, 3, 3, 3, 2, 0, 1, 1, - 1, 1, 1, 1, 0, 0, 0 + 3, 1, 2, 3, 3, 3, 2, 2, + 1, 3, 0, 2, 2, 0, 0, 3, + 2, 2, 9, 0, 0, 0, 0, 2, + 2, 1, 2, 0, 1, 1, 2, 0, + 0, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, + 0, 0, 4, 0, 0, 1, 1, 1, + 1, 0, 3, 2, 2, 2, 7, 1, + 0, 0, 3, 3, 3, 1, 1, 1, + 1, 0, 1, 1, 1, 0, 1, 1, + 1, 0, 3, 1, 2, 3, 3, 3, + 2, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0 }; static const char _json_range_lengths[] = { 0, 2, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 0, 1, 1, 1, 1, 1, - 1, 1, 0, 1, 1, 1, 1, 1, - 1, 0, 0, 0, 3, 3, 3, 3, - 0, 1, 0, 0, 0, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 3, 3, 3, 3, 0, + 1, 1, 0, 1, 1, 0, 1, 1, + 1, 1, 1, 0, 1, 1, 0, 1, + 1, 0, 1, 1, 0, 1, 1, 0, + 1, 1, 0, 1, 1, 0, 0, 0, + 0, 1, 1, 0, 0, 0, 1, 0, + 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 1, 0, - 1, 1, 1, 1, 1, 0, 1, 1, - 1, 1, 1, 1, 0, 0, 0 + 0, 0, 1, 0, 1, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0 }; static const short _json_index_offsets[] = { 0, 0, 11, 13, 18, 23, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, - 48, 53, 58, 60, 64, 69, 74, 79, - 83, 87, 90, 94, 96, 100, 104, 106, - 108, 113, 116, 119, 129, 133, 137, 141, - 145, 148, 157, 159, 160, 161, 166, 171, - 176, 178, 180, 182, 184, 185, 187, 189, - 191, 192, 194, 196, 198, 199, 204, 209, - 211, 215, 220, 225, 230, 234, 235, 238, - 241, 244, 247, 250, 253, 254, 255 -}; - -static const char _json_indicies[] = { + 48, 53, 55, 59, 64, 69, 74, 78, + 82, 85, 89, 91, 95, 99, 101, 103, + 108, 111, 114, 124, 128, 132, 136, 140, + 143, 147, 150, 153, 155, 158, 160, 164, + 166, 168, 170, 172, 174, 176, 178, 180, + 182, 184, 186, 188, 190, 192, 194, 196, + 198, 200, 202, 207, 209, 211, 213, 215, + 217, 219, 221, 226, 229, 232, 235, 244, + 246, 247, 248, 253, 258, 263, 265, 267, + 269, 271, 272, 274, 276, 278, 279, 281, + 283, 285, 286, 291, 293, 297, 302, 307, + 312, 316, 317, 320, 323, 326, 329, 332, + 335, 336, 337, 338, 339, 340 +}; + +static const unsigned char _json_indicies[] = { 0, 2, 3, 4, 5, 6, 7, 8, 0, 3, 1, 9, 1, 11, 12, 1, 11, 10, 13, 14, 12, 13, 1, 14, 1, 1, 14, 10, 15, 1, 16, 1, 17, 1, 18, 1, 19, 1, 20, 1, 21, 1, 22, 1, 23, 1, 24, 1, - 25, 26, 27, 25, 1, 28, 29, 30, - 28, 1, 31, 1, 32, 33, 32, 1, - 33, 1, 1, 33, 34, 35, 36, 37, - 35, 1, 38, 39, 30, 38, 1, 39, - 29, 39, 1, 40, 41, 42, 1, 41, - 42, 1, 44, 45, 45, 43, 46, 1, - 45, 45, 46, 43, 47, 47, 48, 1, - 48, 1, 48, 43, 44, 45, 45, 42, - 43, 50, 51, 49, 53, 54, 52, 55, - 55, 55, 55, 55, 55, 55, 55, 56, - 1, 57, 57, 57, 1, 58, 58, 58, - 1, 59, 59, 59, 1, 60, 60, 60, - 1, 62, 63, 61, 64, 65, 66, 67, - 68, 69, 70, 65, 1, 71, 1, 72, - 73, 75, 76, 1, 75, 74, 77, 78, - 76, 77, 1, 78, 1, 1, 78, 74, - 79, 1, 80, 1, 81, 1, 82, 1, - 83, 84, 1, 85, 1, 86, 1, 87, - 88, 1, 89, 1, 90, 1, 91, 92, - 93, 94, 92, 1, 95, 96, 97, 95, - 1, 98, 1, 99, 100, 99, 1, 100, - 1, 1, 100, 101, 102, 103, 104, 102, - 1, 105, 106, 97, 105, 1, 106, 96, - 106, 1, 107, 108, 108, 1, 109, 109, - 1, 110, 110, 1, 111, 111, 1, 112, - 112, 1, 113, 113, 1, 1, 1, 1, - 0 + 25, 26, 27, 25, 1, 28, 1, 29, + 30, 29, 1, 30, 1, 1, 30, 31, + 32, 33, 34, 32, 1, 35, 36, 27, + 35, 1, 36, 26, 36, 1, 37, 38, + 39, 1, 38, 39, 1, 41, 42, 42, + 40, 43, 1, 42, 42, 43, 40, 44, + 44, 45, 1, 45, 1, 45, 40, 41, + 42, 42, 39, 40, 47, 48, 46, 50, + 51, 49, 52, 52, 52, 52, 52, 52, + 52, 52, 53, 1, 54, 54, 54, 1, + 55, 55, 55, 1, 56, 56, 56, 1, + 57, 57, 57, 1, 59, 60, 58, 61, + 62, 63, 1, 64, 65, 1, 66, 67, + 1, 68, 1, 67, 68, 1, 69, 1, + 66, 67, 65, 1, 70, 1, 71, 1, + 72, 1, 73, 1, 74, 1, 75, 1, + 76, 1, 77, 1, 78, 1, 79, 1, + 80, 1, 81, 1, 82, 1, 83, 1, + 84, 1, 85, 1, 86, 1, 87, 1, + 88, 1, 89, 89, 90, 91, 1, 92, + 1, 93, 1, 94, 1, 95, 1, 96, + 1, 97, 1, 98, 1, 99, 99, 100, + 98, 1, 102, 1, 101, 104, 105, 103, + 1, 1, 101, 106, 107, 108, 109, 110, + 111, 112, 107, 1, 113, 1, 114, 115, + 117, 118, 1, 117, 116, 119, 120, 118, + 119, 1, 120, 1, 1, 120, 116, 121, + 1, 122, 1, 123, 1, 124, 1, 125, + 126, 1, 127, 1, 128, 1, 129, 130, + 1, 131, 1, 132, 1, 133, 134, 135, + 136, 134, 1, 137, 1, 138, 139, 138, + 1, 139, 1, 1, 139, 140, 141, 142, + 143, 141, 1, 144, 145, 136, 144, 1, + 145, 135, 145, 1, 146, 147, 147, 1, + 148, 148, 1, 149, 149, 1, 150, 150, + 1, 151, 151, 1, 152, 152, 1, 1, + 1, 1, 1, 1, 1, 0 }; static const char _json_trans_targs[] = { - 1, 0, 2, 71, 3, 6, 10, 13, - 16, 70, 4, 3, 70, 4, 5, 7, - 8, 9, 72, 11, 12, 73, 14, 15, - 74, 17, 18, 75, 17, 18, 75, 19, - 19, 20, 21, 22, 23, 75, 22, 23, - 25, 26, 32, 76, 27, 29, 28, 30, - 31, 34, 77, 35, 34, 77, 35, 33, - 36, 37, 38, 39, 40, 34, 77, 35, - 42, 44, 45, 48, 53, 57, 61, 43, - 78, 78, 46, 45, 43, 46, 47, 49, - 50, 51, 52, 78, 54, 55, 56, 78, - 58, 59, 60, 78, 62, 63, 69, 62, - 63, 69, 64, 64, 65, 66, 67, 68, - 69, 67, 68, 78, 70, 70, 70, 70, - 70, 70 -}; - -static const char _json_trans_actions[] = { - 0, 0, 74, 68, 27, 0, 0, 0, - 41, 33, 17, 0, 29, 0, 0, 0, + 1, 0, 2, 107, 3, 6, 10, 13, + 16, 106, 4, 3, 106, 4, 5, 7, + 8, 9, 108, 11, 12, 109, 14, 15, + 110, 16, 17, 111, 18, 18, 19, 20, + 21, 22, 111, 21, 22, 24, 25, 31, + 112, 26, 28, 27, 29, 30, 33, 113, + 34, 33, 113, 34, 32, 35, 36, 37, + 38, 39, 33, 113, 34, 41, 42, 46, + 42, 46, 43, 45, 44, 114, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 65, + 66, 67, 73, 72, 68, 69, 70, 71, + 72, 115, 74, 67, 72, 76, 116, 76, + 116, 77, 79, 81, 82, 85, 90, 94, + 98, 80, 117, 117, 83, 82, 80, 83, + 84, 86, 87, 88, 89, 117, 91, 92, + 93, 117, 95, 96, 97, 117, 98, 99, + 105, 100, 100, 101, 102, 103, 104, 105, + 103, 104, 117, 106, 106, 106, 106, 106, + 106 +}; + +static const unsigned char _json_trans_actions[] = { + 0, 0, 113, 107, 53, 0, 0, 0, + 125, 59, 45, 0, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 23, 89, 65, 0, 59, 25, 19, - 0, 0, 17, 21, 21, 62, 0, 0, - 0, 0, 0, 3, 0, 0, 0, 0, - 0, 5, 15, 0, 0, 47, 7, 13, - 0, 50, 9, 9, 9, 53, 56, 11, - 74, 68, 27, 0, 0, 0, 41, 33, - 45, 71, 17, 0, 29, 0, 0, 0, - 0, 0, 0, 80, 0, 0, 0, 83, - 0, 0, 0, 77, 23, 89, 65, 0, - 59, 25, 19, 0, 0, 17, 21, 21, - 62, 0, 0, 86, 0, 31, 37, 39, - 35, 43 -}; - -static const char _json_eof_actions[] = { + 0, 0, 101, 51, 47, 0, 0, 45, + 49, 49, 104, 0, 0, 0, 0, 0, + 3, 0, 0, 0, 0, 0, 5, 15, + 0, 0, 71, 7, 13, 0, 74, 9, + 9, 9, 77, 80, 11, 37, 37, 37, + 0, 0, 0, 39, 0, 41, 86, 0, + 0, 0, 17, 19, 0, 21, 23, 0, + 25, 27, 0, 29, 31, 0, 33, 35, + 0, 135, 83, 135, 0, 0, 0, 0, + 0, 92, 0, 89, 89, 98, 43, 0, + 131, 95, 113, 107, 53, 0, 0, 0, + 125, 59, 69, 110, 45, 0, 55, 0, + 0, 0, 0, 0, 0, 119, 0, 0, + 0, 122, 0, 0, 0, 116, 0, 101, + 51, 47, 0, 0, 45, 49, 49, 104, + 0, 0, 128, 0, 57, 63, 65, 61, + 67 +}; + +static const unsigned char _json_eof_actions[] = { + 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, 0, 1, 0, 0, 1, 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, 1, 0, 1, 0, 0, 1, - 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, 31, - 37, 39, 35, 43, 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, 57, 63, 65, 61, 67, + 0, 0, 0, 0, 0, 0 }; static const int json_start = 1; -static const int json_en_number_machine = 24; -static const int json_en_string_machine = 33; -static const int json_en_value_machine = 41; +static const int json_en_number_machine = 23; +static const int json_en_string_machine = 32; +static const int json_en_duration_machine = 40; +static const int json_en_timestamp_machine = 47; +static const int json_en_fieldmask_machine = 75; +static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 1592 "upb/json/parser.rl" +#line 2752 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -15956,7 +11643,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 1712 "upb/json/parser.c" +#line 2830 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -16031,118 +11718,256 @@ _match: switch ( *_acts++ ) { case 1: -#line 1503 "upb/json/parser.rl" +#line 2557 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 1505 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 24; goto _again;} } +#line 2559 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 1509 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { start_text(parser, p); } break; - case 4: -#line 1510 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_text(parser, p)); } + case 4: +#line 2564 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_text(parser, p)); } + break; + case 5: +#line 2570 "upb/json/parser.rl" + { start_hex(parser); } + break; + case 6: +#line 2571 "upb/json/parser.rl" + { hexdigit(parser, p); } + break; + case 7: +#line 2572 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_hex(parser)); } + break; + case 8: +#line 2578 "upb/json/parser.rl" + { CHECK_RETURN_TOP(escape(parser, p)); } + break; + case 9: +#line 2584 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } + break; + case 10: +#line 2589 "upb/json/parser.rl" + { start_year(parser, p); } + break; + case 11: +#line 2590 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_year(parser, p)); } + break; + case 12: +#line 2594 "upb/json/parser.rl" + { start_month(parser, p); } + break; + case 13: +#line 2595 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_month(parser, p)); } + break; + case 14: +#line 2599 "upb/json/parser.rl" + { start_day(parser, p); } + break; + case 15: +#line 2600 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_day(parser, p)); } + break; + case 16: +#line 2604 "upb/json/parser.rl" + { start_hour(parser, p); } + break; + case 17: +#line 2605 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_hour(parser, p)); } + break; + case 18: +#line 2609 "upb/json/parser.rl" + { start_minute(parser, p); } + break; + case 19: +#line 2610 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_minute(parser, p)); } + break; + case 20: +#line 2614 "upb/json/parser.rl" + { start_second(parser, p); } + break; + case 21: +#line 2615 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_second(parser, p)); } + break; + case 22: +#line 2620 "upb/json/parser.rl" + { start_duration_base(parser, p); } + break; + case 23: +#line 2621 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_duration_base(parser, p)); } + break; + case 24: +#line 2623 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } + break; + case 25: +#line 2628 "upb/json/parser.rl" + { start_timestamp_base(parser); } + break; + case 26: +#line 2630 "upb/json/parser.rl" + { start_timestamp_fraction(parser, p); } + break; + case 27: +#line 2631 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } + break; + case 28: +#line 2633 "upb/json/parser.rl" + { start_timestamp_zone(parser, p); } + break; + case 29: +#line 2634 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; - case 5: -#line 1516 "upb/json/parser.rl" - { start_hex(parser); } + case 30: +#line 2636 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; - case 6: -#line 1517 "upb/json/parser.rl" - { hexdigit(parser, p); } + case 31: +#line 2641 "upb/json/parser.rl" + { start_fieldmask_path_text(parser, p); } break; - case 7: -#line 1518 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_hex(parser)); } + case 32: +#line 2642 "upb/json/parser.rl" + { end_fieldmask_path_text(parser, p); } break; - case 8: -#line 1524 "upb/json/parser.rl" - { CHECK_RETURN_TOP(escape(parser, p)); } + case 33: +#line 2647 "upb/json/parser.rl" + { start_fieldmask_path(parser); } break; - case 9: -#line 1530 "upb/json/parser.rl" + case 34: +#line 2648 "upb/json/parser.rl" + { end_fieldmask_path(parser); } + break; + case 35: +#line 2654 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; - case 10: -#line 1533 "upb/json/parser.rl" - { {stack[top++] = cs; cs = 33; goto _again;} } + case 36: +#line 2659 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { + {stack[top++] = cs; cs = 47;goto _again;} + } else if (is_wellknown_msg(parser, UPB_WELLKNOWN_DURATION)) { + {stack[top++] = cs; cs = 40;goto _again;} + } else if (is_wellknown_msg(parser, UPB_WELLKNOWN_FIELDMASK)) { + {stack[top++] = cs; cs = 75;goto _again;} + } else { + {stack[top++] = cs; cs = 32;goto _again;} + } + } break; - case 11: -#line 1535 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 41; goto _again;} } + case 37: +#line 2672 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; - case 12: -#line 1540 "upb/json/parser.rl" - { start_member(parser); } + case 38: +#line 2677 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + start_any_member(parser, p); + } else { + start_member(parser); + } + } break; - case 13: -#line 1541 "upb/json/parser.rl" + case 39: +#line 2684 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; - case 14: -#line 1544 "upb/json/parser.rl" - { end_member(parser); } + case 40: +#line 2687 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + end_any_member(parser, p); + } else { + end_member(parser); + } + } break; - case 15: -#line 1550 "upb/json/parser.rl" - { start_object(parser); } + case 41: +#line 2698 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + start_any_object(parser, p); + } else { + start_object(parser); + } + } break; - case 16: -#line 1553 "upb/json/parser.rl" - { end_object(parser); } + case 42: +#line 2707 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + CHECK_RETURN_TOP(end_any_object(parser, p)); + } else { + end_object(parser); + } + } break; - case 17: -#line 1559 "upb/json/parser.rl" + case 43: +#line 2719 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; - case 18: -#line 1563 "upb/json/parser.rl" + case 44: +#line 2723 "upb/json/parser.rl" { end_array(parser); } break; - case 19: -#line 1568 "upb/json/parser.rl" + case 45: +#line 2728 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; - case 20: -#line 1569 "upb/json/parser.rl" + case 46: +#line 2729 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; - case 21: -#line 1571 "upb/json/parser.rl" + case 47: +#line 2731 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; - case 22: -#line 1572 "upb/json/parser.rl" + case 48: +#line 2732 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; - case 23: -#line 1574 "upb/json/parser.rl" + case 49: +#line 2734 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; - case 24: -#line 1576 "upb/json/parser.rl" + case 50: +#line 2736 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; - case 25: -#line 1578 "upb/json/parser.rl" - { /* null value */ } + case 51: +#line 2738 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_null(parser)); } break; - case 26: -#line 1580 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_subobject(parser)); } + case 52: +#line 2740 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; - case 27: -#line 1581 "upb/json/parser.rl" - { end_subobject(parser); } + case 53: +#line 2741 "upb/json/parser.rl" + { end_subobject_full(parser); } break; - case 28: -#line 1586 "upb/json/parser.rl" + case 54: +#line 2746 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 1898 "upb/json/parser.c" +#line 3154 "upb/json/parser.c" } } @@ -16159,930 +11984,1475 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 1499 "upb/json/parser.rl" - { - p--; {cs = stack[--top]; goto _again;} - } +#line 2555 "upb/json/parser.rl" + { p--; {cs = stack[--top]; if ( p == pe ) + goto _test_eof; +goto _again;} } break; - case 20: -#line 1569 "upb/json/parser.rl" + case 46: +#line 2729 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; - case 23: -#line 1574 "upb/json/parser.rl" + case 49: +#line 2734 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; - case 24: -#line 1576 "upb/json/parser.rl" + case 50: +#line 2736 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; - case 25: -#line 1578 "upb/json/parser.rl" - { /* null value */ } + case 51: +#line 2738 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_null(parser)); } break; - case 27: -#line 1581 "upb/json/parser.rl" - { end_subobject(parser); } + case 53: +#line 2741 "upb/json/parser.rl" + { end_subobject_full(parser); } break; -#line 1940 "upb/json/parser.c" +#line 3196 "upb/json/parser.c" } } } - _out: {} - } + _out: {} + } + +#line 2774 "upb/json/parser.rl" + + if (p != pe) { + upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); + } else { + capture_suspend(parser, &p); + } + +error: + /* Save parsing state back to parser. */ + parser->current_state = cs; + parser->parser_top = top; + + return p - buf; +} + +static bool end(void *closure, const void *hd) { + upb_json_parser *parser = closure; + + /* Prevent compile warning on unused static constants. */ + UPB_UNUSED(json_start); + UPB_UNUSED(json_en_duration_machine); + UPB_UNUSED(json_en_fieldmask_machine); + UPB_UNUSED(json_en_number_machine); + UPB_UNUSED(json_en_string_machine); + UPB_UNUSED(json_en_timestamp_machine); + UPB_UNUSED(json_en_value_machine); + UPB_UNUSED(json_en_main); + + parse(parser, hd, &eof_ch, 0, NULL); + + return parser->current_state >= 106; +} + +static void json_parser_reset(upb_json_parser *p) { + int cs; + int top; + + p->top = p->stack; + init_frame(p->top); + + /* Emit Ragel initialization of the parser. */ + +#line 3247 "upb/json/parser.c" + { + cs = json_start; + top = 0; + } + +#line 2816 "upb/json/parser.rl" + p->current_state = cs; + p->parser_top = top; + accumulate_clear(p); + p->multipart_state = MULTIPART_INACTIVE; + p->capture = NULL; + p->accumulated = NULL; +} + +static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, + const upb_msgdef *md) { + upb_msg_field_iter i; + upb_alloc *alloc = upb_arena_alloc(c->arena); + + upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); + + m->cache = c; + + upb_byteshandler_init(&m->input_handler_); + upb_byteshandler_setstring(&m->input_handler_, parse, m); + upb_byteshandler_setendstr(&m->input_handler_, end, m); + + upb_strtable_init2(&m->name_table, UPB_CTYPE_CONSTPTR, alloc); + + /* Build name_table */ + + for(upb_msg_field_begin(&i, md); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + const upb_fielddef *f = upb_msg_iter_field(&i); + upb_value v = upb_value_constptr(f); + char *buf; + + /* Add an entry for the JSON name. */ + size_t len = upb_fielddef_getjsonname(f, NULL, 0); + buf = upb_malloc(alloc, len); + upb_fielddef_getjsonname(f, buf, len); + upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc); + + if (strcmp(buf, upb_fielddef_name(f)) != 0) { + /* Since the JSON name is different from the regular field name, add an + * entry for the raw name (compliant proto3 JSON parsers must accept + * both). */ + const char *name = upb_fielddef_name(f); + upb_strtable_insert3(&m->name_table, name, strlen(name), v, alloc); + } + } + + return m; +} + +/* Public API *****************************************************************/ + +upb_json_parser *upb_json_parser_create(upb_arena *arena, + const upb_json_parsermethod *method, + const upb_symtab* symtab, + upb_sink output, + upb_status *status, + bool ignore_json_unknown) { +#ifndef NDEBUG + const size_t size_before = upb_arena_bytesallocated(arena); +#endif + upb_json_parser *p = upb_arena_malloc(arena, sizeof(upb_json_parser)); + if (!p) return false; + + p->arena = arena; + p->method = method; + p->status = status; + p->limit = p->stack + UPB_JSON_MAX_DEPTH; + p->accumulate_buf = NULL; + p->accumulate_buf_size = 0; + upb_bytessink_reset(&p->input_, &method->input_handler_, p); + + json_parser_reset(p); + p->top->sink = output; + p->top->m = upb_handlers_msgdef(output.handlers); + if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { + p->top->is_any = true; + p->top->any_frame = json_parser_any_frame_new(p); + } else { + p->top->is_any = false; + p->top->any_frame = NULL; + } + set_name_table(p, p->top); + p->symtab = symtab; + + p->ignore_json_unknown = ignore_json_unknown; + + /* If this fails, uncomment and increase the value in parser.h. */ + /* fprintf(stderr, "%zd\n", upb_arena_bytesallocated(arena) - size_before); */ + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <= + UPB_JSON_PARSER_SIZE); + return p; +} + +upb_bytessink upb_json_parser_input(upb_json_parser *p) { + return p->input_; +} + +const upb_byteshandler *upb_json_parsermethod_inputhandler( + const upb_json_parsermethod *m) { + return &m->input_handler_; +} + +upb_json_codecache *upb_json_codecache_new() { + upb_alloc *alloc; + upb_json_codecache *c; + + c = upb_gmalloc(sizeof(*c)); + + c->arena = upb_arena_new(); + alloc = upb_arena_alloc(c->arena); + + upb_inttable_init2(&c->methods, UPB_CTYPE_CONSTPTR, alloc); + + return c; +} + +void upb_json_codecache_free(upb_json_codecache *c) { + upb_arena_free(c->arena); + upb_gfree(c); +} + +const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, + const upb_msgdef *md) { + upb_json_parsermethod *m; + upb_value v; + upb_msg_field_iter i; + upb_alloc *alloc = upb_arena_alloc(c->arena); + + if (upb_inttable_lookupptr(&c->methods, md, &v)) { + return upb_value_getconstptr(v); + } + + m = parsermethod_new(c, md); + v = upb_value_constptr(m); + + if (!m) return NULL; + if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL; + + /* Populate parser methods for all submessages, so the name tables will + * be available during parsing. */ + for(upb_msg_field_begin(&i, md); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + upb_fielddef *f = upb_msg_iter_field(&i); + + if (upb_fielddef_issubmsg(f)) { + const upb_msgdef *subdef = upb_fielddef_msgsubdef(f); + const upb_json_parsermethod *sub_method = + upb_json_codecache_get(c, subdef); + + if (!sub_method) return NULL; + } + } + + return m; +} +/* +** This currently uses snprintf() to format primitives, and could be optimized +** further. +*/ + + +#include +#include +#include +#include + +struct upb_json_printer { + upb_sink input_; + /* BytesSink closure. */ + void *subc_; + upb_bytessink output_; + + /* We track the depth so that we know when to emit startstr/endstr on the + * output. */ + int depth_; + + /* Have we emitted the first element? This state is necessary to emit commas + * without leaving a trailing comma in arrays/maps. We keep this state per + * frame depth. + * + * Why max_depth * 2? UPB_MAX_HANDLER_DEPTH counts depth as nested messages. + * We count frames (contexts in which we separate elements by commas) as both + * repeated fields and messages (maps), and the worst case is a + * message->repeated field->submessage->repeated field->... nesting. */ + bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2]; + + /* To print timestamp, printer needs to cache its seconds and nanos values + * and convert them when ending timestamp message. See comments of + * printer_sethandlers_timestamp for more detail. */ + int64_t seconds; + int32_t nanos; +}; + +/* StringPiece; a pointer plus a length. */ +typedef struct { + char *ptr; + size_t len; +} strpc; + +void freestrpc(void *ptr) { + strpc *pc = ptr; + upb_gfree(pc->ptr); + upb_gfree(pc); +} + +typedef struct { + bool preserve_fieldnames; +} upb_json_printercache; + +/* Convert fielddef name to JSON name and return as a string piece. */ +strpc *newstrpc(upb_handlers *h, const upb_fielddef *f, + bool preserve_fieldnames) { + /* TODO(haberman): handle malloc failure. */ + strpc *ret = upb_gmalloc(sizeof(*ret)); + if (preserve_fieldnames) { + ret->ptr = upb_gstrdup(upb_fielddef_name(f)); + ret->len = strlen(ret->ptr); + } else { + size_t len; + ret->len = upb_fielddef_getjsonname(f, NULL, 0); + ret->ptr = upb_gmalloc(ret->len); + len = upb_fielddef_getjsonname(f, ret->ptr, ret->len); + UPB_ASSERT(len == ret->len); + ret->len--; /* NULL */ + } + + upb_handlers_addcleanup(h, ret, freestrpc); + return ret; +} -#line 1614 "upb/json/parser.rl" +/* Convert a null-terminated const char* to a string piece. */ +strpc *newstrpc_str(upb_handlers *h, const char * str) { + strpc * ret = upb_gmalloc(sizeof(*ret)); + ret->ptr = upb_gstrdup(str); + ret->len = strlen(str); + upb_handlers_addcleanup(h, ret, freestrpc); + return ret; +} - if (p != pe) { - upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); - upb_env_reporterror(parser->env, &parser->status); - } else { - capture_suspend(parser, &p); - } +/* ------------ JSON string printing: values, maps, arrays ------------------ */ -error: - /* Save parsing state back to parser. */ - parser->current_state = cs; - parser->parser_top = top; +static void print_data( + upb_json_printer *p, const char *buf, unsigned int len) { + /* TODO: Will need to change if we support pushback from the sink. */ + size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL); + UPB_ASSERT(n == len); +} - return p - buf; +static void print_comma(upb_json_printer *p) { + if (!p->first_elem_[p->depth_]) { + print_data(p, ",", 1); + } + p->first_elem_[p->depth_] = false; } -bool end(void *closure, const void *hd) { - upb_json_parser *parser = closure; +/* Helpers that print properly formatted elements to the JSON output stream. */ - /* Prevent compile warning on unused static constants. */ - UPB_UNUSED(json_start); - UPB_UNUSED(json_en_number_machine); - UPB_UNUSED(json_en_string_machine); - UPB_UNUSED(json_en_value_machine); - UPB_UNUSED(json_en_main); +/* Used for escaping control chars in strings. */ +static const char kControlCharLimit = 0x20; - parse(parser, hd, &eof_ch, 0, NULL); +UPB_INLINE bool is_json_escaped(char c) { + /* See RFC 4627. */ + unsigned char uc = (unsigned char)c; + return uc < kControlCharLimit || uc == '"' || uc == '\\'; +} - return parser->current_state >= -#line 1978 "upb/json/parser.c" -70 -#line 1642 "upb/json/parser.rl" -; +UPB_INLINE const char* json_nice_escape(char c) { + switch (c) { + case '"': return "\\\""; + case '\\': return "\\\\"; + case '\b': return "\\b"; + case '\f': return "\\f"; + case '\n': return "\\n"; + case '\r': return "\\r"; + case '\t': return "\\t"; + default: return NULL; + } } -static void json_parser_reset(upb_json_parser *p) { - int cs; - int top; +/* Write a properly escaped string chunk. The surrounding quotes are *not* + * printed; this is so that the caller has the option of emitting the string + * content in chunks. */ +static void putstring(upb_json_printer *p, const char *buf, unsigned int len) { + const char* unescaped_run = NULL; + unsigned int i; + for (i = 0; i < len; i++) { + char c = buf[i]; + /* Handle escaping. */ + if (is_json_escaped(c)) { + /* Use a "nice" escape, like \n, if one exists for this character. */ + const char* escape = json_nice_escape(c); + /* If we don't have a specific 'nice' escape code, use a \uXXXX-style + * escape. */ + char escape_buf[8]; + if (!escape) { + unsigned char byte = (unsigned char)c; + _upb_snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte); + escape = escape_buf; + } - p->top = p->stack; - p->top->f = NULL; - p->top->is_map = false; - p->top->is_mapentry = false; + /* N.B. that we assume that the input encoding is equal to the output + * encoding (both UTF-8 for now), so for chars >= 0x20 and != \, ", we + * can simply pass the bytes through. */ - /* Emit Ragel initialization of the parser. */ - -#line 1995 "upb/json/parser.c" - { - cs = json_start; - top = 0; - } + /* If there's a current run of unescaped chars, print that run first. */ + if (unescaped_run) { + print_data(p, unescaped_run, &buf[i] - unescaped_run); + unescaped_run = NULL; + } + /* Then print the escape code. */ + print_data(p, escape, strlen(escape)); + } else { + /* Add to the current unescaped run of characters. */ + if (unescaped_run == NULL) { + unescaped_run = &buf[i]; + } + } + } -#line 1656 "upb/json/parser.rl" - p->current_state = cs; - p->parser_top = top; - accumulate_clear(p); - p->multipart_state = MULTIPART_INACTIVE; - p->capture = NULL; - p->accumulated = NULL; - upb_status_clear(&p->status); + /* If the string ended in a run of unescaped characters, print that last run. */ + if (unescaped_run) { + print_data(p, unescaped_run, &buf[len] - unescaped_run); + } } -static void visit_json_parsermethod(const upb_refcounted *r, - upb_refcounted_visit *visit, - void *closure) { - const upb_json_parsermethod *method = (upb_json_parsermethod*)r; - visit(r, upb_msgdef_upcast2(method->msg), closure); -} +#define CHKLENGTH(x) if (!(x)) return -1; -static void free_json_parsermethod(upb_refcounted *r) { - upb_json_parsermethod *method = (upb_json_parsermethod*)r; +/* Helpers that format floating point values according to our custom formats. + * Right now we use %.8g and %.17g for float/double, respectively, to match + * proto2::util::JsonFormat's defaults. May want to change this later. */ - upb_inttable_iter i; - upb_inttable_begin(&i, &method->name_tables); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_value val = upb_inttable_iter_value(&i); - upb_strtable *t = upb_value_getptr(val); - upb_strtable_uninit(t); - upb_gfree(t); +const char neginf[] = "\"-Infinity\""; +const char inf[] = "\"Infinity\""; + +static size_t fmt_double(double val, char* buf, size_t length) { + if (val == (1.0 / 0.0)) { + CHKLENGTH(length >= strlen(inf)); + strcpy(buf, inf); + return strlen(inf); + } else if (val == (-1.0 / 0.0)) { + CHKLENGTH(length >= strlen(neginf)); + strcpy(buf, neginf); + return strlen(neginf); + } else { + size_t n = _upb_snprintf(buf, length, "%.17g", val); + CHKLENGTH(n > 0 && n < length); + return n; } +} - upb_inttable_uninit(&method->name_tables); +static size_t fmt_float(float val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "%.8g", val); + CHKLENGTH(n > 0 && n < length); + return n; +} - upb_gfree(r); +static size_t fmt_bool(bool val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "%s", (val ? "true" : "false")); + CHKLENGTH(n > 0 && n < length); + return n; } -static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { - upb_msg_field_iter i; - upb_strtable *t; +static size_t fmt_int64_as_number(long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "%lld", val); + CHKLENGTH(n > 0 && n < length); + return n; +} - /* It would be nice to stack-allocate this, but protobufs do not limit the - * length of fields to any reasonable limit. */ - char *buf = NULL; - size_t len = 0; +static size_t fmt_uint64_as_number( + unsigned long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "%llu", val); + CHKLENGTH(n > 0 && n < length); + return n; +} - if (upb_inttable_lookupptr(&m->name_tables, md, NULL)) { - return; - } +static size_t fmt_int64_as_string(long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%lld\"", val); + CHKLENGTH(n > 0 && n < length); + return n; +} - /* TODO(haberman): handle malloc failure. */ - t = upb_gmalloc(sizeof(*t)); - upb_strtable_init(t, UPB_CTYPE_CONSTPTR); - upb_inttable_insertptr(&m->name_tables, md, upb_value_ptr(t)); +static size_t fmt_uint64_as_string( + unsigned long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%llu\"", val); + CHKLENGTH(n > 0 && n < length); + return n; +} - for(upb_msg_field_begin(&i, md); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - const upb_fielddef *f = upb_msg_iter_field(&i); +/* Print a map key given a field name. Called by scalar field handlers and by + * startseq for repeated fields. */ +static bool putkey(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + const strpc *key = handler_data; + print_comma(p); + print_data(p, "\"", 1); + putstring(p, key->ptr, key->len); + print_data(p, "\":", 2); + return true; +} - /* Add an entry for the JSON name. */ - size_t field_len = upb_fielddef_getjsonname(f, buf, len); - if (field_len > len) { - size_t len2; - buf = upb_grealloc(buf, 0, field_len); - len = field_len; - len2 = upb_fielddef_getjsonname(f, buf, len); - UPB_ASSERT(len == len2); - } - upb_strtable_insert(t, buf, upb_value_constptr(f)); +#define CHKFMT(val) if ((val) == (size_t)-1) return false; +#define CHK(val) if (!(val)) return false; - if (strcmp(buf, upb_fielddef_name(f)) != 0) { - /* Since the JSON name is different from the regular field name, add an - * entry for the raw name (compliant proto3 JSON parsers must accept - * both). */ - upb_strtable_insert(t, upb_fielddef_name(f), upb_value_constptr(f)); - } +#define TYPE_HANDLERS(type, fmt_func) \ + static bool put##type(void *closure, const void *handler_data, type val) { \ + upb_json_printer *p = closure; \ + char data[64]; \ + size_t length = fmt_func(val, data, sizeof(data)); \ + UPB_UNUSED(handler_data); \ + CHKFMT(length); \ + print_data(p, data, length); \ + return true; \ + } \ + static bool scalar_##type(void *closure, const void *handler_data, \ + type val) { \ + CHK(putkey(closure, handler_data)); \ + CHK(put##type(closure, handler_data, val)); \ + return true; \ + } \ + static bool repeated_##type(void *closure, const void *handler_data, \ + type val) { \ + upb_json_printer *p = closure; \ + print_comma(p); \ + CHK(put##type(closure, handler_data, val)); \ + return true; \ + } - if (upb_fielddef_issubmsg(f)) { - add_jsonname_table(m, upb_fielddef_msgsubdef(f)); - } +#define TYPE_HANDLERS_MAPKEY(type, fmt_func) \ + static bool putmapkey_##type(void *closure, const void *handler_data, \ + type val) { \ + upb_json_printer *p = closure; \ + char data[64]; \ + size_t length = fmt_func(val, data, sizeof(data)); \ + UPB_UNUSED(handler_data); \ + print_data(p, "\"", 1); \ + print_data(p, data, length); \ + print_data(p, "\":", 2); \ + return true; \ } - upb_gfree(buf); -} +TYPE_HANDLERS(double, fmt_double) +TYPE_HANDLERS(float, fmt_float) +TYPE_HANDLERS(bool, fmt_bool) +TYPE_HANDLERS(int32_t, fmt_int64_as_number) +TYPE_HANDLERS(uint32_t, fmt_int64_as_number) +TYPE_HANDLERS(int64_t, fmt_int64_as_string) +TYPE_HANDLERS(uint64_t, fmt_uint64_as_string) -/* Public API *****************************************************************/ +/* double and float are not allowed to be map keys. */ +TYPE_HANDLERS_MAPKEY(bool, fmt_bool) +TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64_as_number) -upb_json_parser *upb_json_parser_create(upb_env *env, - const upb_json_parsermethod *method, - upb_sink *output, - bool ignore_json_unknown) { -#ifndef NDEBUG - const size_t size_before = upb_env_bytesallocated(env); -#endif - upb_json_parser *p = upb_env_malloc(env, sizeof(upb_json_parser)); - if (!p) return false; +#undef TYPE_HANDLERS +#undef TYPE_HANDLERS_MAPKEY - p->env = env; - p->method = method; - p->limit = p->stack + UPB_JSON_MAX_DEPTH; - p->accumulate_buf = NULL; - p->accumulate_buf_size = 0; - upb_bytessink_reset(&p->input_, &method->input_handler_, p); +typedef struct { + void *keyname; + const upb_enumdef *enumdef; +} EnumHandlerData; + +static bool scalar_enum(void *closure, const void *handler_data, + int32_t val) { + const EnumHandlerData *hd = handler_data; + upb_json_printer *p = closure; + const char *symbolic_name; - json_parser_reset(p); - upb_sink_reset(&p->top->sink, output->handlers, output->closure); - p->top->m = upb_handlers_msgdef(output->handlers); - set_name_table(p, p->top); + CHK(putkey(closure, hd->keyname)); - p->ignore_json_unknown = ignore_json_unknown; + symbolic_name = upb_enumdef_iton(hd->enumdef, val); + if (symbolic_name) { + print_data(p, "\"", 1); + putstring(p, symbolic_name, strlen(symbolic_name)); + print_data(p, "\"", 1); + } else { + putint32_t(closure, NULL, val); + } - /* If this fails, uncomment and increase the value in parser.h. */ - /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= - UPB_JSON_PARSER_SIZE); - return p; + return true; } -upb_bytessink *upb_json_parser_input(upb_json_parser *p) { - return &p->input_; +static void print_enum_symbolic_name(upb_json_printer *p, + const upb_enumdef *def, + int32_t val) { + const char *symbolic_name = upb_enumdef_iton(def, val); + if (symbolic_name) { + print_data(p, "\"", 1); + putstring(p, symbolic_name, strlen(symbolic_name)); + print_data(p, "\"", 1); + } else { + putint32_t(p, NULL, val); + } } -upb_json_parsermethod *upb_json_parsermethod_new(const upb_msgdef* md, - const void* owner) { - static const struct upb_refcounted_vtbl vtbl = {visit_json_parsermethod, - free_json_parsermethod}; - upb_json_parsermethod *ret = upb_gmalloc(sizeof(*ret)); - upb_refcounted_init(upb_json_parsermethod_upcast_mutable(ret), &vtbl, owner); +static bool repeated_enum(void *closure, const void *handler_data, + int32_t val) { + const EnumHandlerData *hd = handler_data; + upb_json_printer *p = closure; + print_comma(p); - ret->msg = md; - upb_ref2(md, ret); + print_enum_symbolic_name(p, hd->enumdef, val); - upb_byteshandler_init(&ret->input_handler_); - upb_byteshandler_setstring(&ret->input_handler_, parse, ret); - upb_byteshandler_setendstr(&ret->input_handler_, end, ret); + return true; +} - upb_inttable_init(&ret->name_tables, UPB_CTYPE_PTR); +static bool mapvalue_enum(void *closure, const void *handler_data, + int32_t val) { + const EnumHandlerData *hd = handler_data; + upb_json_printer *p = closure; - add_jsonname_table(ret, md); + print_enum_symbolic_name(p, hd->enumdef, val); - return ret; + return true; } -const upb_byteshandler *upb_json_parsermethod_inputhandler( - const upb_json_parsermethod *m) { - return &m->input_handler_; +static void *scalar_startsubmsg(void *closure, const void *handler_data) { + return putkey(closure, handler_data) ? closure : UPB_BREAK; } -/* -** This currently uses snprintf() to format primitives, and could be optimized -** further. -*/ - - -#include -#include - -struct upb_json_printer { - upb_sink input_; - /* BytesSink closure. */ - void *subc_; - upb_bytessink *output_; - /* We track the depth so that we know when to emit startstr/endstr on the - * output. */ - int depth_; +static void *repeated_startsubmsg(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_comma(p); + return closure; +} - /* Have we emitted the first element? This state is necessary to emit commas - * without leaving a trailing comma in arrays/maps. We keep this state per - * frame depth. - * - * Why max_depth * 2? UPB_MAX_HANDLER_DEPTH counts depth as nested messages. - * We count frames (contexts in which we separate elements by commas) as both - * repeated fields and messages (maps), and the worst case is a - * message->repeated field->submessage->repeated field->... nesting. */ - bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2]; -}; +static void start_frame(upb_json_printer *p) { + p->depth_++; + p->first_elem_[p->depth_] = true; + print_data(p, "{", 1); +} -/* StringPiece; a pointer plus a length. */ -typedef struct { - char *ptr; - size_t len; -} strpc; +static void end_frame(upb_json_printer *p) { + print_data(p, "}", 1); + p->depth_--; +} -void freestrpc(void *ptr) { - strpc *pc = ptr; - upb_gfree(pc->ptr); - upb_gfree(pc); +static bool printer_startmsg(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + start_frame(p); + return true; } -/* Convert fielddef name to JSON name and return as a string piece. */ -strpc *newstrpc(upb_handlers *h, const upb_fielddef *f, - bool preserve_fieldnames) { - /* TODO(haberman): handle malloc failure. */ - strpc *ret = upb_gmalloc(sizeof(*ret)); - if (preserve_fieldnames) { - ret->ptr = upb_gstrdup(upb_fielddef_name(f)); - ret->len = strlen(ret->ptr); - } else { - size_t len; - ret->len = upb_fielddef_getjsonname(f, NULL, 0); - ret->ptr = upb_gmalloc(ret->len); - len = upb_fielddef_getjsonname(f, ret->ptr, ret->len); - UPB_ASSERT(len == ret->len); - ret->len--; /* NULL */ +static bool printer_endmsg(void *closure, const void *handler_data, upb_status *s) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(s); + end_frame(p); + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); } + return true; +} - upb_handlers_addcleanup(h, ret, freestrpc); - return ret; +static void *startseq(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + CHK(putkey(closure, handler_data)); + p->depth_++; + p->first_elem_[p->depth_] = true; + print_data(p, "[", 1); + return closure; } -/* ------------ JSON string printing: values, maps, arrays ------------------ */ +static bool endseq(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "]", 1); + p->depth_--; + return true; +} -static void print_data( - upb_json_printer *p, const char *buf, unsigned int len) { - /* TODO: Will need to change if we support pushback from the sink. */ - size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL); - UPB_ASSERT(n == len); +static void *startmap(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + CHK(putkey(closure, handler_data)); + p->depth_++; + p->first_elem_[p->depth_] = true; + print_data(p, "{", 1); + return closure; } -static void print_comma(upb_json_printer *p) { - if (!p->first_elem_[p->depth_]) { - print_data(p, ",", 1); - } - p->first_elem_[p->depth_] = false; +static bool endmap(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "}", 1); + p->depth_--; + return true; } -/* Helpers that print properly formatted elements to the JSON output stream. */ +static size_t putstr(void *closure, const void *handler_data, const char *str, + size_t len, const upb_bufhandle *handle) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(handle); + putstring(p, str, len); + return len; +} -/* Used for escaping control chars in strings. */ -static const char kControlCharLimit = 0x20; +/* This has to Base64 encode the bytes, because JSON has no "bytes" type. */ +static size_t putbytes(void *closure, const void *handler_data, const char *str, + size_t len, const upb_bufhandle *handle) { + upb_json_printer *p = closure; -UPB_INLINE bool is_json_escaped(char c) { - /* See RFC 4627. */ - unsigned char uc = (unsigned char)c; - return uc < kControlCharLimit || uc == '"' || uc == '\\'; -} + /* This is the regular base64, not the "web-safe" version. */ + static const char base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -UPB_INLINE const char* json_nice_escape(char c) { - switch (c) { - case '"': return "\\\""; - case '\\': return "\\\\"; - case '\b': return "\\b"; - case '\f': return "\\f"; - case '\n': return "\\n"; - case '\r': return "\\r"; - case '\t': return "\\t"; - default: return NULL; - } -} + /* Base64-encode. */ + char data[16000]; + const char *limit = data + sizeof(data); + const unsigned char *from = (const unsigned char*)str; + char *to = data; + size_t remaining = len; + size_t bytes; -/* Write a properly escaped string chunk. The surrounding quotes are *not* - * printed; this is so that the caller has the option of emitting the string - * content in chunks. */ -static void putstring(upb_json_printer *p, const char *buf, unsigned int len) { - const char* unescaped_run = NULL; - unsigned int i; - for (i = 0; i < len; i++) { - char c = buf[i]; - /* Handle escaping. */ - if (is_json_escaped(c)) { - /* Use a "nice" escape, like \n, if one exists for this character. */ - const char* escape = json_nice_escape(c); - /* If we don't have a specific 'nice' escape code, use a \uXXXX-style - * escape. */ - char escape_buf[8]; - if (!escape) { - unsigned char byte = (unsigned char)c; - _upb_snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte); - escape = escape_buf; - } + UPB_UNUSED(handler_data); + UPB_UNUSED(handle); - /* N.B. that we assume that the input encoding is equal to the output - * encoding (both UTF-8 for now), so for chars >= 0x20 and != \, ", we - * can simply pass the bytes through. */ + print_data(p, "\"", 1); - /* If there's a current run of unescaped chars, print that run first. */ - if (unescaped_run) { - print_data(p, unescaped_run, &buf[i] - unescaped_run); - unescaped_run = NULL; - } - /* Then print the escape code. */ - print_data(p, escape, strlen(escape)); - } else { - /* Add to the current unescaped run of characters. */ - if (unescaped_run == NULL) { - unescaped_run = &buf[i]; - } + while (remaining > 2) { + if (limit - to < 4) { + bytes = to - data; + putstring(p, data, bytes); + to = data; } + + to[0] = base64[from[0] >> 2]; + to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; + to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)]; + to[3] = base64[from[2] & 0x3f]; + + remaining -= 3; + to += 4; + from += 3; } - /* If the string ended in a run of unescaped characters, print that last run. */ - if (unescaped_run) { - print_data(p, unescaped_run, &buf[len] - unescaped_run); + switch (remaining) { + case 2: + to[0] = base64[from[0] >> 2]; + to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; + to[2] = base64[(from[1] & 0xf) << 2]; + to[3] = '='; + to += 4; + from += 2; + break; + case 1: + to[0] = base64[from[0] >> 2]; + to[1] = base64[((from[0] & 0x3) << 4)]; + to[2] = '='; + to[3] = '='; + to += 4; + from += 1; + break; } + + bytes = to - data; + putstring(p, data, bytes); + print_data(p, "\"", 1); + return len; } -#define CHKLENGTH(x) if (!(x)) return -1; +static void *scalar_startstr(void *closure, const void *handler_data, + size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(size_hint); + CHK(putkey(closure, handler_data)); + print_data(p, "\"", 1); + return p; +} -/* Helpers that format floating point values according to our custom formats. - * Right now we use %.8g and %.17g for float/double, respectively, to match - * proto2::util::JsonFormat's defaults. May want to change this later. */ +static size_t scalar_str(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + CHK(putstr(closure, handler_data, str, len, handle)); + return len; +} -const char neginf[] = "\"-Infinity\""; -const char inf[] = "\"Infinity\""; +static bool scalar_endstr(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "\"", 1); + return true; +} -static size_t fmt_double(double val, char* buf, size_t length) { - if (val == (1.0 / 0.0)) { - CHKLENGTH(length >= strlen(inf)); - strcpy(buf, inf); - return strlen(inf); - } else if (val == (-1.0 / 0.0)) { - CHKLENGTH(length >= strlen(neginf)); - strcpy(buf, neginf); - return strlen(neginf); - } else { - size_t n = _upb_snprintf(buf, length, "%.17g", val); - CHKLENGTH(n > 0 && n < length); - return n; - } +static void *repeated_startstr(void *closure, const void *handler_data, + size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(size_hint); + print_comma(p); + print_data(p, "\"", 1); + return p; } -static size_t fmt_float(float val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%.8g", val); - CHKLENGTH(n > 0 && n < length); - return n; +static size_t repeated_str(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + CHK(putstr(closure, handler_data, str, len, handle)); + return len; } -static size_t fmt_bool(bool val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%s", (val ? "true" : "false")); - CHKLENGTH(n > 0 && n < length); - return n; +static bool repeated_endstr(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "\"", 1); + return true; } -static size_t fmt_int64(long val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%ld", val); - CHKLENGTH(n > 0 && n < length); - return n; +static void *mapkeyval_startstr(void *closure, const void *handler_data, + size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(size_hint); + print_data(p, "\"", 1); + return p; } -static size_t fmt_uint64(unsigned long long val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%llu", val); - CHKLENGTH(n > 0 && n < length); - return n; +static size_t mapkey_str(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + CHK(putstr(closure, handler_data, str, len, handle)); + return len; } -/* Print a map key given a field name. Called by scalar field handlers and by - * startseq for repeated fields. */ -static bool putkey(void *closure, const void *handler_data) { +static bool mapkey_endstr(void *closure, const void *handler_data) { upb_json_printer *p = closure; - const strpc *key = handler_data; - print_comma(p); - print_data(p, "\"", 1); - putstring(p, key->ptr, key->len); + UPB_UNUSED(handler_data); print_data(p, "\":", 2); return true; } -#define CHKFMT(val) if ((val) == (size_t)-1) return false; -#define CHK(val) if (!(val)) return false; +static bool mapvalue_endstr(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "\"", 1); + return true; +} -#define TYPE_HANDLERS(type, fmt_func) \ - static bool put##type(void *closure, const void *handler_data, type val) { \ - upb_json_printer *p = closure; \ - char data[64]; \ - size_t length = fmt_func(val, data, sizeof(data)); \ - UPB_UNUSED(handler_data); \ - CHKFMT(length); \ - print_data(p, data, length); \ - return true; \ - } \ - static bool scalar_##type(void *closure, const void *handler_data, \ - type val) { \ - CHK(putkey(closure, handler_data)); \ - CHK(put##type(closure, handler_data, val)); \ - return true; \ - } \ - static bool repeated_##type(void *closure, const void *handler_data, \ - type val) { \ - upb_json_printer *p = closure; \ - print_comma(p); \ - CHK(put##type(closure, handler_data, val)); \ - return true; \ - } +static size_t scalar_bytes(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + CHK(putkey(closure, handler_data)); + CHK(putbytes(closure, handler_data, str, len, handle)); + return len; +} -#define TYPE_HANDLERS_MAPKEY(type, fmt_func) \ - static bool putmapkey_##type(void *closure, const void *handler_data, \ - type val) { \ - upb_json_printer *p = closure; \ - print_data(p, "\"", 1); \ - CHK(put##type(closure, handler_data, val)); \ - print_data(p, "\":", 2); \ - return true; \ - } +static size_t repeated_bytes(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + upb_json_printer *p = closure; + print_comma(p); + CHK(putbytes(closure, handler_data, str, len, handle)); + return len; +} -TYPE_HANDLERS(double, fmt_double) -TYPE_HANDLERS(float, fmt_float) -TYPE_HANDLERS(bool, fmt_bool) -TYPE_HANDLERS(int32_t, fmt_int64) -TYPE_HANDLERS(uint32_t, fmt_int64) -TYPE_HANDLERS(int64_t, fmt_int64) -TYPE_HANDLERS(uint64_t, fmt_uint64) +static size_t mapkey_bytes(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + upb_json_printer *p = closure; + CHK(putbytes(closure, handler_data, str, len, handle)); + print_data(p, ":", 1); + return len; +} -/* double and float are not allowed to be map keys. */ -TYPE_HANDLERS_MAPKEY(bool, fmt_bool) -TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64) +static void set_enum_hd(upb_handlers *h, + const upb_fielddef *f, + bool preserve_fieldnames, + upb_handlerattr *attr) { + EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData)); + hd->enumdef = upb_fielddef_enumsubdef(f); + hd->keyname = newstrpc(h, f, preserve_fieldnames); + upb_handlers_addcleanup(h, hd, upb_gfree); + attr->handler_data = hd; +} -#undef TYPE_HANDLERS -#undef TYPE_HANDLERS_MAPKEY +/* Set up handlers for a mapentry submessage (i.e., an individual key/value pair + * in a map). + * + * TODO: Handle missing key, missing value, out-of-order key/value, or repeated + * key or value cases properly. The right way to do this is to allocate a + * temporary structure at the start of a mapentry submessage, store key and + * value data in it as key and value handlers are called, and then print the + * key/value pair once at the end of the submessage. If we don't do this, we + * should at least detect the case and throw an error. However, so far all of + * our sources that emit mapentry messages do so canonically (with one key + * field, and then one value field), so this is not a pressing concern at the + * moment. */ +void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames, + upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); -typedef struct { - void *keyname; - const upb_enumdef *enumdef; -} EnumHandlerData; + /* A mapentry message is printed simply as '"key": value'. Rather than + * special-case key and value for every type below, we just handle both + * fields explicitly here. */ + const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY); + const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE); -static bool scalar_enum(void *closure, const void *handler_data, - int32_t val) { - const EnumHandlerData *hd = handler_data; - upb_json_printer *p = closure; - const char *symbolic_name; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; - CHK(putkey(closure, hd->keyname)); + UPB_UNUSED(closure); - symbolic_name = upb_enumdef_iton(hd->enumdef, val); - if (symbolic_name) { - print_data(p, "\"", 1); - putstring(p, symbolic_name, strlen(symbolic_name)); - print_data(p, "\"", 1); - } else { - putint32_t(closure, NULL, val); + switch (upb_fielddef_type(key_field)) { + case UPB_TYPE_INT32: + upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr); + break; + case UPB_TYPE_INT64: + upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr); + break; + case UPB_TYPE_UINT32: + upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr); + break; + case UPB_TYPE_UINT64: + upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr); + break; + case UPB_TYPE_BOOL: + upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr); + break; + case UPB_TYPE_STRING: + upb_handlers_setstartstr(h, key_field, mapkeyval_startstr, &empty_attr); + upb_handlers_setstring(h, key_field, mapkey_str, &empty_attr); + upb_handlers_setendstr(h, key_field, mapkey_endstr, &empty_attr); + break; + case UPB_TYPE_BYTES: + upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr); + break; + default: + UPB_ASSERT(false); + break; } - return true; -} - -static void print_enum_symbolic_name(upb_json_printer *p, - const upb_enumdef *def, - int32_t val) { - const char *symbolic_name = upb_enumdef_iton(def, val); - if (symbolic_name) { - print_data(p, "\"", 1); - putstring(p, symbolic_name, strlen(symbolic_name)); - print_data(p, "\"", 1); - } else { - putint32_t(p, NULL, val); + switch (upb_fielddef_type(value_field)) { + case UPB_TYPE_INT32: + upb_handlers_setint32(h, value_field, putint32_t, &empty_attr); + break; + case UPB_TYPE_INT64: + upb_handlers_setint64(h, value_field, putint64_t, &empty_attr); + break; + case UPB_TYPE_UINT32: + upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr); + break; + case UPB_TYPE_UINT64: + upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr); + break; + case UPB_TYPE_BOOL: + upb_handlers_setbool(h, value_field, putbool, &empty_attr); + break; + case UPB_TYPE_FLOAT: + upb_handlers_setfloat(h, value_field, putfloat, &empty_attr); + break; + case UPB_TYPE_DOUBLE: + upb_handlers_setdouble(h, value_field, putdouble, &empty_attr); + break; + case UPB_TYPE_STRING: + upb_handlers_setstartstr(h, value_field, mapkeyval_startstr, &empty_attr); + upb_handlers_setstring(h, value_field, putstr, &empty_attr); + upb_handlers_setendstr(h, value_field, mapvalue_endstr, &empty_attr); + break; + case UPB_TYPE_BYTES: + upb_handlers_setstring(h, value_field, putbytes, &empty_attr); + break; + case UPB_TYPE_ENUM: { + upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT; + set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr); + upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr); + break; + } + case UPB_TYPE_MESSAGE: + /* No handler necessary -- the submsg handlers will print the message + * as appropriate. */ + break; } } -static bool repeated_enum(void *closure, const void *handler_data, - int32_t val) { - const EnumHandlerData *hd = handler_data; +static bool putseconds(void *closure, const void *handler_data, + int64_t seconds) { upb_json_printer *p = closure; - print_comma(p); - - print_enum_symbolic_name(p, hd->enumdef, val); - + p->seconds = seconds; + UPB_UNUSED(handler_data); return true; } -static bool mapvalue_enum(void *closure, const void *handler_data, - int32_t val) { - const EnumHandlerData *hd = handler_data; +static bool putnanos(void *closure, const void *handler_data, + int32_t nanos) { upb_json_printer *p = closure; - - print_enum_symbolic_name(p, hd->enumdef, val); - + p->nanos = nanos; + UPB_UNUSED(handler_data); return true; } -static void *scalar_startsubmsg(void *closure, const void *handler_data) { - return putkey(closure, handler_data) ? closure : UPB_BREAK; -} - -static void *repeated_startsubmsg(void *closure, const void *handler_data) { +static void *scalar_startstr_nokey(void *closure, const void *handler_data, + size_t size_hint) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - print_comma(p); - return closure; -} - -static void start_frame(upb_json_printer *p) { - p->depth_++; - p->first_elem_[p->depth_] = true; - print_data(p, "{", 1); -} - -static void end_frame(upb_json_printer *p) { - print_data(p, "}", 1); - p->depth_--; + UPB_UNUSED(size_hint); + print_data(p, "\"", 1); + return p; } -static bool printer_startmsg(void *closure, const void *handler_data) { +static size_t putstr_nokey(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); - } - start_frame(p); - return true; + UPB_UNUSED(handle); + print_data(p, "\"", 1); + putstring(p, str, len); + print_data(p, "\"", 1); + return len + 2; } -static bool printer_endmsg(void *closure, const void *handler_data, upb_status *s) { +static void *startseq_nokey(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - UPB_UNUSED(s); - end_frame(p); - if (p->depth_ == 0) { - upb_bytessink_end(p->output_); - } - return true; + p->depth_++; + p->first_elem_[p->depth_] = true; + print_data(p, "[", 1); + return closure; } -static void *startseq(void *closure, const void *handler_data) { +static void *startseq_fieldmask(void *closure, const void *handler_data) { upb_json_printer *p = closure; - CHK(putkey(closure, handler_data)); + UPB_UNUSED(handler_data); p->depth_++; p->first_elem_[p->depth_] = true; - print_data(p, "[", 1); return closure; } -static bool endseq(void *closure, const void *handler_data) { +static bool endseq_fieldmask(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - print_data(p, "]", 1); p->depth_--; return true; } -static void *startmap(void *closure, const void *handler_data) { +static void *repeated_startstr_fieldmask( + void *closure, const void *handler_data, + size_t size_hint) { upb_json_printer *p = closure; - CHK(putkey(closure, handler_data)); + UPB_UNUSED(handler_data); + UPB_UNUSED(size_hint); + print_comma(p); + return p; +} + +static size_t repeated_str_fieldmask( + void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + const char* limit = str + len; + bool upper = false; + size_t result_len = 0; + for (; str < limit; str++) { + if (*str == '_') { + upper = true; + continue; + } + if (upper && *str >= 'a' && *str <= 'z') { + char upper_char = toupper(*str); + CHK(putstr(closure, handler_data, &upper_char, 1, handle)); + } else { + CHK(putstr(closure, handler_data, str, 1, handle)); + } + upper = false; + result_len++; + } + return result_len; +} + +static void *startmap_nokey(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); p->depth_++; p->first_elem_[p->depth_] = true; print_data(p, "{", 1); return closure; } -static bool endmap(void *closure, const void *handler_data) { +static bool putnull(void *closure, const void *handler_data, + int32_t null) { upb_json_printer *p = closure; + print_data(p, "null", 4); UPB_UNUSED(handler_data); - print_data(p, "}", 1); - p->depth_--; + UPB_UNUSED(null); return true; } -static size_t putstr(void *closure, const void *handler_data, const char *str, - size_t len, const upb_bufhandle *handle) { +static bool printer_startdurationmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - UPB_UNUSED(handle); - putstring(p, str, len); - return len; + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + return true; } -/* This has to Base64 encode the bytes, because JSON has no "bytes" type. */ -static size_t putbytes(void *closure, const void *handler_data, const char *str, - size_t len, const upb_bufhandle *handle) { - upb_json_printer *p = closure; +#define UPB_DURATION_MAX_JSON_LEN 23 +#define UPB_DURATION_MAX_NANO_LEN 9 - /* This is the regular base64, not the "web-safe" version. */ - static const char base64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static bool printer_enddurationmsg(void *closure, const void *handler_data, + upb_status *s) { + upb_json_printer *p = closure; + char buffer[UPB_DURATION_MAX_JSON_LEN]; + size_t base_len; + size_t curr; + size_t i; - /* Base64-encode. */ - char data[16000]; - const char *limit = data + sizeof(data); - const unsigned char *from = (const unsigned char*)str; - char *to = data; - size_t remaining = len; - size_t bytes; + memset(buffer, 0, UPB_DURATION_MAX_JSON_LEN); - UPB_UNUSED(handler_data); - UPB_UNUSED(handle); + if (p->seconds < -315576000000) { + upb_status_seterrf(s, "error parsing duration: " + "minimum acceptable value is " + "-315576000000"); + return false; + } - while (remaining > 2) { - /* TODO(haberman): handle encoded lengths > sizeof(data) */ - UPB_ASSERT((limit - to) >= 4); + if (p->seconds > 315576000000) { + upb_status_seterrf(s, "error serializing duration: " + "maximum acceptable value is " + "315576000000"); + return false; + } - to[0] = base64[from[0] >> 2]; - to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; - to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)]; - to[3] = base64[from[2] & 0x3f]; + _upb_snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds); + base_len = strlen(buffer); - remaining -= 3; - to += 4; - from += 3; + if (p->nanos != 0) { + char nanos_buffer[UPB_DURATION_MAX_NANO_LEN + 3]; + _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f", + p->nanos / 1000000000.0); + /* Remove trailing 0. */ + for (i = UPB_DURATION_MAX_NANO_LEN + 2; + nanos_buffer[i] == '0'; i--) { + nanos_buffer[i] = 0; + } + strcpy(buffer + base_len, nanos_buffer + 1); } - switch (remaining) { - case 2: - to[0] = base64[from[0] >> 2]; - to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; - to[2] = base64[(from[1] & 0xf) << 2]; - to[3] = '='; - to += 4; - from += 2; - break; - case 1: - to[0] = base64[from[0] >> 2]; - to[1] = base64[((from[0] & 0x3) << 4)]; - to[2] = '='; - to[3] = '='; - to += 4; - from += 1; - break; - } + curr = strlen(buffer); + strcpy(buffer + curr, "s"); + + p->seconds = 0; + p->nanos = 0; - bytes = to - data; print_data(p, "\"", 1); - putstring(p, data, bytes); + print_data(p, buffer, strlen(buffer)); print_data(p, "\"", 1); - return len; + + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } + + UPB_UNUSED(handler_data); + return true; } -static void *scalar_startstr(void *closure, const void *handler_data, - size_t size_hint) { +static bool printer_starttimestampmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - UPB_UNUSED(size_hint); - CHK(putkey(closure, handler_data)); - print_data(p, "\"", 1); - return p; + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + return true; } -static size_t scalar_str(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - CHK(putstr(closure, handler_data, str, len, handle)); - return len; -} +#define UPB_TIMESTAMP_MAX_JSON_LEN 31 +#define UPB_TIMESTAMP_BEFORE_NANO_LEN 19 +#define UPB_TIMESTAMP_MAX_NANO_LEN 9 + +static bool printer_endtimestampmsg(void *closure, const void *handler_data, + upb_status *s) { + upb_json_printer *p = closure; + char buffer[UPB_TIMESTAMP_MAX_JSON_LEN]; + time_t time = p->seconds; + size_t curr; + size_t i; + size_t year_length = + strftime(buffer, UPB_TIMESTAMP_MAX_JSON_LEN, "%Y", gmtime(&time)); + + if (p->seconds < -62135596800) { + upb_status_seterrf(s, "error parsing timestamp: " + "minimum acceptable value is " + "0001-01-01T00:00:00Z"); + return false; + } + + if (p->seconds > 253402300799) { + upb_status_seterrf(s, "error parsing timestamp: " + "maximum acceptable value is " + "9999-12-31T23:59:59Z"); + return false; + } + + /* strftime doesn't guarantee 4 digits for year. Prepend 0 by ourselves. */ + for (i = 0; i < 4 - year_length; i++) { + buffer[i] = '0'; + } + + strftime(buffer + (4 - year_length), UPB_TIMESTAMP_MAX_JSON_LEN, + "%Y-%m-%dT%H:%M:%S", gmtime(&time)); + if (p->nanos != 0) { + char nanos_buffer[UPB_TIMESTAMP_MAX_NANO_LEN + 3]; + _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f", + p->nanos / 1000000000.0); + /* Remove trailing 0. */ + for (i = UPB_TIMESTAMP_MAX_NANO_LEN + 2; + nanos_buffer[i] == '0'; i--) { + nanos_buffer[i] = 0; + } + strcpy(buffer + UPB_TIMESTAMP_BEFORE_NANO_LEN, nanos_buffer + 1); + } + + curr = strlen(buffer); + strcpy(buffer + curr, "Z"); + + p->seconds = 0; + p->nanos = 0; + + print_data(p, "\"", 1); + print_data(p, buffer, strlen(buffer)); + print_data(p, "\"", 1); + + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } -static bool scalar_endstr(void *closure, const void *handler_data) { - upb_json_printer *p = closure; UPB_UNUSED(handler_data); - print_data(p, "\"", 1); + UPB_UNUSED(s); return true; } -static void *repeated_startstr(void *closure, const void *handler_data, - size_t size_hint) { +static bool printer_startmsg_noframe(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - UPB_UNUSED(size_hint); - print_comma(p); - print_data(p, "\"", 1); - return p; -} - -static size_t repeated_str(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - CHK(putstr(closure, handler_data, str, len, handle)); - return len; + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + return true; } -static bool repeated_endstr(void *closure, const void *handler_data) { +static bool printer_endmsg_noframe( + void *closure, const void *handler_data, upb_status *s) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - print_data(p, "\"", 1); + UPB_UNUSED(s); + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } return true; } -static void *mapkeyval_startstr(void *closure, const void *handler_data, - size_t size_hint) { +static bool printer_startmsg_fieldmask( + void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - UPB_UNUSED(size_hint); + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } print_data(p, "\"", 1); - return p; -} - -static size_t mapkey_str(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - CHK(putstr(closure, handler_data, str, len, handle)); - return len; -} - -static bool mapkey_endstr(void *closure, const void *handler_data) { - upb_json_printer *p = closure; - UPB_UNUSED(handler_data); - print_data(p, "\":", 2); return true; } -static bool mapvalue_endstr(void *closure, const void *handler_data) { +static bool printer_endmsg_fieldmask( + void *closure, const void *handler_data, upb_status *s) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); + UPB_UNUSED(s); print_data(p, "\"", 1); + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } return true; } -static size_t scalar_bytes(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { +static void *scalar_startstr_onlykey( + void *closure, const void *handler_data, size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(size_hint); CHK(putkey(closure, handler_data)); - CHK(putbytes(closure, handler_data, str, len, handle)); - return len; + return p; } -static size_t repeated_bytes(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - upb_json_printer *p = closure; - print_comma(p); - CHK(putbytes(closure, handler_data, str, len, handle)); - return len; +/* Set up handlers for an Any submessage. */ +void printer_sethandlers_any(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + + const upb_fielddef* type_field = upb_msgdef_itof(md, UPB_ANY_TYPE); + const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_ANY_VALUE); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; + + /* type_url's json name is "@type" */ + upb_handlerattr type_name_attr = UPB_HANDLERATTR_INIT; + upb_handlerattr value_name_attr = UPB_HANDLERATTR_INIT; + strpc *type_url_json_name = newstrpc_str(h, "@type"); + strpc *value_json_name = newstrpc_str(h, "value"); + + type_name_attr.handler_data = type_url_json_name; + value_name_attr.handler_data = value_json_name; + + /* Set up handlers. */ + upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg, &empty_attr); + + upb_handlers_setstartstr(h, type_field, scalar_startstr, &type_name_attr); + upb_handlers_setstring(h, type_field, scalar_str, &empty_attr); + upb_handlers_setendstr(h, type_field, scalar_endstr, &empty_attr); + + /* This is not the full and correct JSON encoding for the Any value field. It + * requires further processing by the wrapper code based on the type URL. + */ + upb_handlers_setstartstr(h, value_field, scalar_startstr_onlykey, + &value_name_attr); + + UPB_UNUSED(closure); } -static size_t mapkey_bytes(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - upb_json_printer *p = closure; - CHK(putbytes(closure, handler_data, str, len, handle)); - print_data(p, ":", 1); - return len; +/* Set up handlers for a fieldmask submessage. */ +void printer_sethandlers_fieldmask(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + const upb_fielddef* f = upb_msgdef_itof(md, 1); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; + + upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr); + upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr); + + upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr); + + upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr); + upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr); + + UPB_UNUSED(closure); } -static void set_enum_hd(upb_handlers *h, - const upb_fielddef *f, - bool preserve_fieldnames, - upb_handlerattr *attr) { - EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData)); - hd->enumdef = (const upb_enumdef *)upb_fielddef_subdef(f); - hd->keyname = newstrpc(h, f, preserve_fieldnames); - upb_handlers_addcleanup(h, hd, upb_gfree); - upb_handlerattr_sethandlerdata(attr, hd); +/* Set up handlers for a duration submessage. */ +void printer_sethandlers_duration(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + + const upb_fielddef* seconds_field = + upb_msgdef_itof(md, UPB_DURATION_SECONDS); + const upb_fielddef* nanos_field = + upb_msgdef_itof(md, UPB_DURATION_NANOS); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; + + upb_handlers_setstartmsg(h, printer_startdurationmsg, &empty_attr); + upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr); + upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr); + upb_handlers_setendmsg(h, printer_enddurationmsg, &empty_attr); + + UPB_UNUSED(closure); } -/* Set up handlers for a mapentry submessage (i.e., an individual key/value pair - * in a map). - * - * TODO: Handle missing key, missing value, out-of-order key/value, or repeated - * key or value cases properly. The right way to do this is to allocate a - * temporary structure at the start of a mapentry submessage, store key and - * value data in it as key and value handlers are called, and then print the - * key/value pair once at the end of the submessage. If we don't do this, we - * should at least detect the case and throw an error. However, so far all of - * our sources that emit mapentry messages do so canonically (with one key - * field, and then one value field), so this is not a pressing concern at the - * moment. */ -void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames, - upb_handlers *h) { +/* Set up handlers for a timestamp submessage. Instead of printing fields + * separately, the json representation of timestamp follows RFC 3339 */ +void printer_sethandlers_timestamp(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); - /* A mapentry message is printed simply as '"key": value'. Rather than - * special-case key and value for every type below, we just handle both - * fields explicitly here. */ - const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY); - const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE); + const upb_fielddef* seconds_field = + upb_msgdef_itof(md, UPB_TIMESTAMP_SECONDS); + const upb_fielddef* nanos_field = + upb_msgdef_itof(md, UPB_TIMESTAMP_NANOS); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; + + upb_handlers_setstartmsg(h, printer_starttimestampmsg, &empty_attr); + upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr); + upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr); + upb_handlers_setendmsg(h, printer_endtimestampmsg, &empty_attr); UPB_UNUSED(closure); +} - switch (upb_fielddef_type(key_field)) { - case UPB_TYPE_INT32: - upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr); - break; - case UPB_TYPE_INT64: - upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr); - break; - case UPB_TYPE_UINT32: - upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr); - break; - case UPB_TYPE_UINT64: - upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr); - break; - case UPB_TYPE_BOOL: - upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr); - break; - case UPB_TYPE_STRING: - upb_handlers_setstartstr(h, key_field, mapkeyval_startstr, &empty_attr); - upb_handlers_setstring(h, key_field, mapkey_str, &empty_attr); - upb_handlers_setendstr(h, key_field, mapkey_endstr, &empty_attr); - break; - case UPB_TYPE_BYTES: - upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr); - break; - default: - UPB_ASSERT(false); - break; - } +void printer_sethandlers_value(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + upb_msg_field_iter i; - switch (upb_fielddef_type(value_field)) { - case UPB_TYPE_INT32: - upb_handlers_setint32(h, value_field, putint32_t, &empty_attr); - break; - case UPB_TYPE_INT64: - upb_handlers_setint64(h, value_field, putint64_t, &empty_attr); - break; - case UPB_TYPE_UINT32: - upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr); - break; - case UPB_TYPE_UINT64: - upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr); - break; - case UPB_TYPE_BOOL: - upb_handlers_setbool(h, value_field, putbool, &empty_attr); - break; - case UPB_TYPE_FLOAT: - upb_handlers_setfloat(h, value_field, putfloat, &empty_attr); - break; - case UPB_TYPE_DOUBLE: - upb_handlers_setdouble(h, value_field, putdouble, &empty_attr); - break; - case UPB_TYPE_STRING: - upb_handlers_setstartstr(h, value_field, mapkeyval_startstr, &empty_attr); - upb_handlers_setstring(h, value_field, putstr, &empty_attr); - upb_handlers_setendstr(h, value_field, mapvalue_endstr, &empty_attr); - break; - case UPB_TYPE_BYTES: - upb_handlers_setstring(h, value_field, putbytes, &empty_attr); - break; - case UPB_TYPE_ENUM: { - upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER; - set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr); - upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr); - upb_handlerattr_uninit(&enum_attr); - break; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; + + upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); + + upb_msg_field_begin(&i, md); + for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) { + const upb_fielddef *f = upb_msg_iter_field(&i); + + switch (upb_fielddef_type(f)) { + case UPB_TYPE_ENUM: + upb_handlers_setint32(h, f, putnull, &empty_attr); + break; + case UPB_TYPE_DOUBLE: + upb_handlers_setdouble(h, f, putdouble, &empty_attr); + break; + case UPB_TYPE_STRING: + upb_handlers_setstartstr(h, f, scalar_startstr_nokey, &empty_attr); + upb_handlers_setstring(h, f, scalar_str, &empty_attr); + upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr); + break; + case UPB_TYPE_BOOL: + upb_handlers_setbool(h, f, putbool, &empty_attr); + break; + case UPB_TYPE_MESSAGE: + break; + default: + UPB_ASSERT(false); + break; } - case UPB_TYPE_MESSAGE: - /* No handler necessary -- the submsg handlers will print the message - * as appropriate. */ - break; } - upb_handlerattr_uninit(&empty_attr); + UPB_UNUSED(closure); +} + +#define WRAPPER_SETHANDLERS(wrapper, type, putmethod) \ +void printer_sethandlers_##wrapper(const void *closure, upb_handlers *h) { \ + const upb_msgdef *md = upb_handlers_msgdef(h); \ + const upb_fielddef* f = upb_msgdef_itof(md, 1); \ + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; \ + upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); \ + upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); \ + upb_handlers_set##type(h, f, putmethod, &empty_attr); \ + UPB_UNUSED(closure); \ +} + +WRAPPER_SETHANDLERS(doublevalue, double, putdouble) +WRAPPER_SETHANDLERS(floatvalue, float, putfloat) +WRAPPER_SETHANDLERS(int64value, int64, putint64_t) +WRAPPER_SETHANDLERS(uint64value, uint64, putuint64_t) +WRAPPER_SETHANDLERS(int32value, int32, putint32_t) +WRAPPER_SETHANDLERS(uint32value, uint32, putuint32_t) +WRAPPER_SETHANDLERS(boolvalue, bool, putbool) +WRAPPER_SETHANDLERS(stringvalue, string, putstr_nokey) +WRAPPER_SETHANDLERS(bytesvalue, string, putbytes) + +#undef WRAPPER_SETHANDLERS + +void printer_sethandlers_listvalue(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + const upb_fielddef* f = upb_msgdef_itof(md, 1); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; + + upb_handlers_setstartseq(h, f, startseq_nokey, &empty_attr); + upb_handlers_setendseq(h, f, endseq, &empty_attr); + + upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); + + upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr); + + UPB_UNUSED(closure); +} + +void printer_sethandlers_structvalue(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + const upb_fielddef* f = upb_msgdef_itof(md, 1); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; + + upb_handlers_setstartseq(h, f, startmap_nokey, &empty_attr); + upb_handlers_setendseq(h, f, endmap, &empty_attr); + + upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); + + upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr); + + UPB_UNUSED(closure); } void printer_sethandlers(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); bool is_mapentry = upb_msgdef_mapentry(md); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; upb_msg_field_iter i; - const bool *preserve_fieldnames_ptr = closure; - const bool preserve_fieldnames = *preserve_fieldnames_ptr; + const upb_json_printercache *cache = closure; + const bool preserve_fieldnames = cache->preserve_fieldnames; if (is_mapentry) { /* mapentry messages are sufficiently different that we handle them @@ -17091,6 +13461,48 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { return; } + switch (upb_msgdef_wellknowntype(md)) { + case UPB_WELLKNOWN_UNSPECIFIED: + break; + case UPB_WELLKNOWN_ANY: + printer_sethandlers_any(closure, h); + return; + case UPB_WELLKNOWN_FIELDMASK: + printer_sethandlers_fieldmask(closure, h); + return; + case UPB_WELLKNOWN_DURATION: + printer_sethandlers_duration(closure, h); + return; + case UPB_WELLKNOWN_TIMESTAMP: + printer_sethandlers_timestamp(closure, h); + return; + case UPB_WELLKNOWN_VALUE: + printer_sethandlers_value(closure, h); + return; + case UPB_WELLKNOWN_LISTVALUE: + printer_sethandlers_listvalue(closure, h); + return; + case UPB_WELLKNOWN_STRUCT: + printer_sethandlers_structvalue(closure, h); + return; +#define WRAPPER(wellknowntype, name) \ + case wellknowntype: \ + printer_sethandlers_##name(closure, h); \ + return; \ + + WRAPPER(UPB_WELLKNOWN_DOUBLEVALUE, doublevalue); + WRAPPER(UPB_WELLKNOWN_FLOATVALUE, floatvalue); + WRAPPER(UPB_WELLKNOWN_INT64VALUE, int64value); + WRAPPER(UPB_WELLKNOWN_UINT64VALUE, uint64value); + WRAPPER(UPB_WELLKNOWN_INT32VALUE, int32value); + WRAPPER(UPB_WELLKNOWN_UINT32VALUE, uint32value); + WRAPPER(UPB_WELLKNOWN_BOOLVALUE, boolvalue); + WRAPPER(UPB_WELLKNOWN_STRINGVALUE, stringvalue); + WRAPPER(UPB_WELLKNOWN_BYTESVALUE, bytesvalue); + +#undef WRAPPER + } + upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr); upb_handlers_setendmsg(h, printer_endmsg, &empty_attr); @@ -17107,9 +13519,8 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) { const upb_fielddef *f = upb_msg_iter_field(&i); - upb_handlerattr name_attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&name_attr, - newstrpc(h, f, preserve_fieldnames)); + upb_handlerattr name_attr = UPB_HANDLERATTR_INIT; + name_attr.handler_data = newstrpc(h, f, preserve_fieldnames); if (upb_fielddef_ismap(f)) { upb_handlers_setstartseq(h, f, startmap, &name_attr); @@ -17131,7 +13542,7 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { /* For now, we always emit symbolic names for enums. We may want an * option later to control this behavior, but we will wait for a real * need first. */ - upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT; set_enum_hd(h, f, preserve_fieldnames, &enum_attr); if (upb_fielddef_isseq(f)) { @@ -17140,7 +13551,6 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { upb_handlers_setint32(h, f, scalar_enum, &enum_attr); } - upb_handlerattr_uninit(&enum_attr); break; } case UPB_TYPE_STRING: @@ -17171,11 +13581,8 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { } break; } - - upb_handlerattr_uninit(&name_attr); } - upb_handlerattr_uninit(&empty_attr); #undef TYPE } @@ -17186,32 +13593,42 @@ static void json_printer_reset(upb_json_printer *p) { /* Public API *****************************************************************/ -upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, - upb_bytessink *output) { +upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h, + upb_bytessink output) { #ifndef NDEBUG - size_t size_before = upb_env_bytesallocated(e); + size_t size_before = upb_arena_bytesallocated(a); #endif - upb_json_printer *p = upb_env_malloc(e, sizeof(upb_json_printer)); + upb_json_printer *p = upb_arena_malloc(a, sizeof(upb_json_printer)); if (!p) return NULL; p->output_ = output; json_printer_reset(p); upb_sink_reset(&p->input_, h, p); + p->seconds = 0; + p->nanos = 0; /* If this fails, increase the value in printer.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(a) - size_before <= UPB_JSON_PRINTER_SIZE); return p; } -upb_sink *upb_json_printer_input(upb_json_printer *p) { - return &p->input_; +upb_sink upb_json_printer_input(upb_json_printer *p) { + return p->input_; } -const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, - bool preserve_fieldnames, - const void *owner) { - return upb_handlers_newfrozen( - md, owner, printer_sethandlers, &preserve_fieldnames); +upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) { + upb_json_printercache *cache = upb_gmalloc(sizeof(*cache)); + upb_handlercache *ret = upb_handlercache_new(printer_sethandlers, cache); + + cache->preserve_fieldnames = preserve_proto_fieldnames; + upb_handlercache_addcleanup(ret, cache, upb_gfree); + + return ret; } + +#undef UPB_SIZE +#undef UPB_FIELD_AT +#undef UPB_READ_ONEOF +#undef UPB_WRITE_ONEOF diff --git a/php/ext/google/protobuf/upb.h b/php/ext/google/protobuf/upb.h index 180f4715d9..d8ae1b6d41 100644 --- a/php/ext/google/protobuf/upb.h +++ b/php/ext/google/protobuf/upb.h @@ -1,4 +1,4 @@ -// Amalgamated source file +/* Amalgamated source file */ // php.h intentionally defined NDEBUG. We have to define this macro in order to // be used together with php.h @@ -6,104 +6,51 @@ #define NDEBUG #endif -/* -** upb_decode: parsing into a upb_msg using a upb_msglayout. -*/ +#include +#ifndef UINTPTR_MAX +#error must include stdint.h first +#endif -#ifndef UPB_DECODE_H_ -#define UPB_DECODE_H_ +#if UINTPTR_MAX == 0xffffffff +#define UPB_SIZE(size32, size64) size32 +#else +#define UPB_SIZE(size32, size64) size64 +#endif + +#define UPB_FIELD_AT(msg, fieldtype, offset) \ + *(fieldtype*)((const char*)(msg) + offset) +#define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \ + UPB_FIELD_AT(msg, int, case_offset) == case_val \ + ? UPB_FIELD_AT(msg, fieldtype, offset) \ + : default + +#define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \ + UPB_FIELD_AT(msg, int, case_offset) = case_val; \ + UPB_FIELD_AT(msg, fieldtype, offset) = value; /* ** upb::Message is a representation for protobuf messages. ** ** However it differs from other common representations like ** google::protobuf::Message in one key way: it does not prescribe any ** ownership between messages and submessages, and it relies on the -** client to delete each message/submessage/array/map at the appropriate -** time. +** client to ensure that each submessage/array/map outlives its parent. +** +** All messages, arrays, and maps live in an Arena. If the entire message +** tree is in the same arena, ensuring proper lifetimes is simple. However +** the client can mix arenas as long as they ensure that there are no +** dangling pointers. ** ** A client can access a upb::Message without knowing anything about ** ownership semantics, but to create or mutate a message a user needs ** to implement the memory management themselves. ** -** Currently all messages, arrays, and maps store a upb_alloc* internally. -** Mutating operations use this when they require dynamically-allocated -** memory. We could potentially eliminate this size overhead later by -** letting the user flip a bit on the factory that prevents this from -** being stored. The user would then need to use separate functions where -** the upb_alloc* is passed explicitly. However for handlers to populate -** such structures, they would need a place to store this upb_alloc* during -** parsing; upb_handlers don't currently have a good way to accommodate this. -** ** TODO: UTF-8 checking? **/ #ifndef UPB_MSG_H_ #define UPB_MSG_H_ -/* -** Defs are upb's internal representation of the constructs that can appear -** in a .proto file: -** -** - upb::MessageDef (upb_msgdef): describes a "message" construct. -** - upb::FieldDef (upb_fielddef): describes a message field. -** - upb::FileDef (upb_filedef): describes a .proto file and its defs. -** - upb::EnumDef (upb_enumdef): describes an enum. -** - upb::OneofDef (upb_oneofdef): describes a oneof. -** - upb::Def (upb_def): base class of all the others. -** -** TODO: definitions of services. -** -** Like upb_refcounted objects, defs are mutable only until frozen, and are -** only thread-safe once frozen. -** -** This is a mixed C/C++ interface that offers a full API to both languages. -** See the top-level README for more information. -*/ - -#ifndef UPB_DEF_H_ -#define UPB_DEF_H_ - -/* -** upb::RefCounted (upb_refcounted) -** -** A refcounting scheme that supports circular refs. It accomplishes this by -** partitioning the set of objects into groups such that no cycle spans groups; -** we can then reference-count the group as a whole and ignore refs within the -** group. When objects are mutable, these groups are computed very -** conservatively; we group any objects that have ever had a link between them. -** When objects are frozen, we compute strongly-connected components which -** allows us to be precise and only group objects that are actually cyclic. -** -** This is a mixed C/C++ interface that offers a full API to both languages. -** See the top-level README for more information. -*/ - -#ifndef UPB_REFCOUNTED_H_ -#define UPB_REFCOUNTED_H_ - -/* -** 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 homogenous (all values of the same type). In debug -** mode, we check this on insert and lookup. -*/ - -#ifndef UPB_TABLE_H_ -#define UPB_TABLE_H_ - #include #include /* @@ -120,16 +67,14 @@ #include #include #include +#include #ifdef __cplusplus +#include namespace upb { -class Allocator; class Arena; -class Environment; -class ErrorSpace; class Status; template class InlinedArena; -template class InlinedEnvironment; } #endif @@ -181,128 +126,15 @@ template class InlinedEnvironment; #error Need implementations of [v]snprintf and va_copy #endif - -#if ((defined(__cplusplus) && __cplusplus >= 201103L) || \ - defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11) -#define UPB_CXX11 +#ifdef __cplusplus +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \ + (defined(_MSC_VER) && _MSC_VER >= 1900) +// C++11 is present +#else +#error upb requires C++11 for C++ support #endif - -/* UPB_DISALLOW_COPY_AND_ASSIGN() - * UPB_DISALLOW_POD_OPS() - * - * Declare these in the "private" section of a C++ class to forbid copy/assign - * or all POD ops (construct, destruct, copy, assign) on that class. */ -#ifdef UPB_CXX11 -#include -#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \ - class_name(const class_name&) = delete; \ - void operator=(const class_name&) = delete; -#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \ - class_name() = delete; \ - ~class_name() = delete; \ - UPB_DISALLOW_COPY_AND_ASSIGN(class_name) -#define UPB_ASSERT_STDLAYOUT(type) \ - static_assert(std::is_standard_layout::value, \ - #type " must be standard layout"); -#define UPB_FINAL final -#else /* !defined(UPB_CXX11) */ -#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \ - class_name(const class_name&); \ - void operator=(const class_name&); -#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \ - class_name(); \ - ~class_name(); \ - UPB_DISALLOW_COPY_AND_ASSIGN(class_name) -#define UPB_ASSERT_STDLAYOUT(type) -#define UPB_FINAL #endif -/* UPB_DECLARE_TYPE() - * UPB_DECLARE_DERIVED_TYPE() - * UPB_DECLARE_DERIVED_TYPE2() - * - * Macros for declaring C and C++ types both, including inheritance. - * The inheritance doesn't use real C++ inheritance, to stay compatible with C. - * - * These macros also provide upcasts: - * - in C: types-specific functions (ie. upb_foo_upcast(foo)) - * - in C++: upb::upcast(foo) along with implicit conversions - * - * Downcasts are not provided, but upb/def.h defines downcasts for upb::Def. */ - -#define UPB_C_UPCASTS(ty, base) \ - UPB_INLINE base *ty ## _upcast_mutable(ty *p) { return (base*)p; } \ - UPB_INLINE const base *ty ## _upcast(const ty *p) { return (const base*)p; } - -#define UPB_C_UPCASTS2(ty, base, base2) \ - UPB_C_UPCASTS(ty, base) \ - UPB_INLINE base2 *ty ## _upcast2_mutable(ty *p) { return (base2*)p; } \ - UPB_INLINE const base2 *ty ## _upcast2(const ty *p) { return (const base2*)p; } - -#ifdef __cplusplus - -#define UPB_BEGIN_EXTERN_C extern "C" { -#define UPB_END_EXTERN_C } -#define UPB_PRIVATE_FOR_CPP private: -#define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname; - -#define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \ - UPB_DECLARE_TYPE(cppname, cname) \ - UPB_C_UPCASTS(cname, cbase) \ - namespace upb { \ - template <> \ - class Pointer : public PointerBase { \ - public: \ - explicit Pointer(cppname* ptr) \ - : PointerBase(ptr) {} \ - }; \ - template <> \ - class Pointer \ - : public PointerBase { \ - public: \ - explicit Pointer(const cppname* ptr) \ - : PointerBase(ptr) {} \ - }; \ - } - -#define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, cname, cbase, \ - cbase2) \ - UPB_DECLARE_TYPE(cppname, cname) \ - UPB_C_UPCASTS2(cname, cbase, cbase2) \ - namespace upb { \ - template <> \ - class Pointer : public PointerBase2 { \ - public: \ - explicit Pointer(cppname* ptr) \ - : PointerBase2(ptr) {} \ - }; \ - template <> \ - class Pointer \ - : public PointerBase2 { \ - public: \ - explicit Pointer(const cppname* ptr) \ - : PointerBase2(ptr) {} \ - }; \ - } - -#else /* !defined(__cplusplus) */ - -#define UPB_BEGIN_EXTERN_C -#define UPB_END_EXTERN_C -#define UPB_PRIVATE_FOR_CPP -#define UPB_DECLARE_TYPE(cppname, cname) \ - struct cname; \ - typedef struct cname cname; -#define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \ - UPB_DECLARE_TYPE(cppname, cname) \ - UPB_C_UPCASTS(cname, cbase) -#define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, \ - cname, cbase, cbase2) \ - UPB_DECLARE_TYPE(cppname, cname) \ - UPB_C_UPCASTS2(cname, cbase, cbase2) - -#endif /* defined(__cplusplus) */ - #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y)) #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -326,135 +158,26 @@ template class InlinedEnvironment; #define UPB_UNREACHABLE() do { assert(0); } while(0) #endif -/* Generic function type. */ -typedef void upb_func(); - - -/* C++ Casts ******************************************************************/ - -#ifdef __cplusplus - -namespace upb { - -template class Pointer; - -/* Casts to a subclass. The caller must know that cast is correct; an - * incorrect cast will throw an assertion failure in debug mode. - * - * Example: - * upb::Def* def = GetDef(); - * // Assert-fails if this was not actually a MessageDef. - * upb::MessgeDef* md = upb::down_cast(def); - * - * Note that downcasts are only defined for some types (at the moment you can - * only downcast from a upb::Def to a specific Def type). */ -template To down_cast(From* f); - -/* Casts to a subclass. If the class does not actually match the given To type, - * returns NULL. - * - * Example: - * upb::Def* def = GetDef(); - * // md will be NULL if this was not actually a MessageDef. - * upb::MessgeDef* md = upb::down_cast(def); - * - * Note that dynamic casts are only defined for some types (at the moment you - * can only downcast from a upb::Def to a specific Def type).. */ -template To dyn_cast(From* f); - -/* Casts to any base class, or the type itself (ie. can be a no-op). - * - * Example: - * upb::MessageDef* md = GetDef(); - * // This will fail to compile if this wasn't actually a base class. - * upb::Def* def = upb::upcast(md); - */ -template inline Pointer upcast(T *f) { return Pointer(f); } - -/* Attempt upcast to specific base class. - * - * Example: - * upb::MessageDef* md = GetDef(); - * upb::upcast_to(md)->MethodOnDef(); - */ -template inline T* upcast_to(F *f) { - return static_cast(upcast(f)); -} - -/* PointerBase: implementation detail of upb::upcast(). - * It is implicitly convertable to pointers to the Base class(es). - */ -template -class PointerBase { - public: - explicit PointerBase(T* ptr) : ptr_(ptr) {} - operator T*() { return ptr_; } - operator Base*() { return (Base*)ptr_; } - - private: - T* ptr_; -}; - -template -class PointerBase2 : public PointerBase { - public: - explicit PointerBase2(T* ptr) : PointerBase(ptr) {} - operator Base2*() { return Pointer(*this); } -}; - -} - -#endif - -/* A list of types as they are encoded on-the-wire. */ -typedef enum { - UPB_WIRE_TYPE_VARINT = 0, - UPB_WIRE_TYPE_64BIT = 1, - UPB_WIRE_TYPE_DELIMITED = 2, - UPB_WIRE_TYPE_START_GROUP = 3, - UPB_WIRE_TYPE_END_GROUP = 4, - UPB_WIRE_TYPE_32BIT = 5 -} upb_wiretype_t; - - -/* upb::ErrorSpace ************************************************************/ - -/* A upb::ErrorSpace represents some domain of possible error values. This lets - * upb::Status attach specific error codes to operations, like POSIX/C errno, - * Win32 error codes, etc. Clients who want to know the very specific error - * code can check the error space and then know the type of the integer code. - * - * NOTE: upb::ErrorSpace is currently not used and should be considered - * experimental. It is important primarily in cases where upb is performing - * I/O, but upb doesn't currently have any components that do this. */ - -UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace) - -#ifdef __cplusplus -class upb::ErrorSpace { -#else -struct upb_errorspace { -#endif - const char *name; -}; - - -/* upb::Status ****************************************************************/ +/* upb_status *****************************************************************/ -/* upb::Status represents a success or failure status and error message. +/* upb_status represents a success or failure status and error message. * It owns no resources and allocates no memory, so it should work * even in OOM situations. */ -UPB_DECLARE_TYPE(upb::Status, upb_status) /* The maximum length of an error message before it will get truncated. */ -#define UPB_STATUS_MAX_MESSAGE 128 +#define UPB_STATUS_MAX_MESSAGE 127 + +typedef struct { + bool ok; + char msg[UPB_STATUS_MAX_MESSAGE]; /* Error message; NULL-terminated. */ +} upb_status; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const char *upb_status_errmsg(const upb_status *status); bool upb_ok(const upb_status *status); -upb_errorspace *upb_status_errspace(const upb_status *status); -int upb_status_errcode(const upb_status *status); /* Any of the functions that write to a status object allow status to be NULL, * to support use cases where the function's caller does not care about the @@ -463,88 +186,55 @@ void upb_status_clear(upb_status *status); void upb_status_seterrmsg(upb_status *status, const char *msg); void upb_status_seterrf(upb_status *status, const char *fmt, ...); void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args); -void upb_status_copy(upb_status *to, const upb_status *from); -UPB_END_EXTERN_C +UPB_INLINE void upb_status_setoom(upb_status *status) { + upb_status_seterrmsg(status, "out of memory"); +} #ifdef __cplusplus +} /* extern "C" */ class upb::Status { public: - Status() { upb_status_clear(this); } + Status() { upb_status_clear(&status_); } - /* Returns true if there is no error. */ - bool ok() const { return upb_ok(this); } + upb_status* ptr() { return &status_; } - /* Optional error space and code, useful if the caller wants to - * programmatically check the specific kind of error. */ - ErrorSpace* error_space() { return upb_status_errspace(this); } - int error_code() const { return upb_status_errcode(this); } + /* Returns true if there is no error. */ + bool ok() const { return upb_ok(&status_); } - /* The returned string is invalidated by any other call into the status. */ - const char *error_message() const { return upb_status_errmsg(this); } + /* Guaranteed to be NULL-terminated. */ + const char *error_message() const { return upb_status_errmsg(&status_); } /* The error message will be truncated if it is longer than * UPB_STATUS_MAX_MESSAGE-4. */ - void SetErrorMessage(const char* msg) { upb_status_seterrmsg(this, msg); } - void SetFormattedErrorMessage(const char* fmt, ...) { + void SetErrorMessage(const char *msg) { upb_status_seterrmsg(&status_, msg); } + void SetFormattedErrorMessage(const char *fmt, ...) { va_list args; va_start(args, fmt); - upb_status_vseterrf(this, fmt, args); + upb_status_vseterrf(&status_, fmt, args); va_end(args); } /* Resets the status to a successful state with no message. */ - void Clear() { upb_status_clear(this); } - - void CopyFrom(const Status& other) { upb_status_copy(this, &other); } + void Clear() { upb_status_clear(&status_); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(Status) -#else -struct upb_status { -#endif - bool ok_; - - /* Specific status code defined by some error space (optional). */ - int code_; - upb_errorspace *error_space_; - - /* TODO(haberman): add file/line of error? */ - - /* Error message; NULL-terminated. */ - char msg[UPB_STATUS_MAX_MESSAGE]; + upb_status status_; }; -#define UPB_STATUS_INIT {true, 0, NULL, {0}} - - -/** Built-in error spaces. ****************************************************/ - -/* Errors raised by upb that we want to be able to detect programmatically. */ -typedef enum { - UPB_NOMEM /* Can't reuse ENOMEM because it is POSIX, not ISO C. */ -} upb_errcode_t; - -extern upb_errorspace upb_upberr; - -void upb_upberr_setoom(upb_status *s); - -/* Since errno is defined by standard C, we define an error space for it in - * core upb. Other error spaces should be defined in other, platform-specific - * modules. */ - -extern upb_errorspace upb_errnoerr; - +#endif /* __cplusplus */ -/** upb::Allocator ************************************************************/ +/** upb_alloc *****************************************************************/ -/* A upb::Allocator is a possibly-stateful allocator object. +/* 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. */ -UPB_DECLARE_TYPE(upb::Allocator, upb_alloc) + +struct upb_alloc; +typedef struct upb_alloc upb_alloc; /* A malloc()/free() function. * If "size" is 0 then the function acts like free(), otherwise it acts like @@ -552,19 +242,7 @@ UPB_DECLARE_TYPE(upb::Allocator, upb_alloc) typedef void *upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize, size_t size); -#ifdef __cplusplus - -class upb::Allocator UPB_FINAL { - public: - Allocator() {} - - private: - UPB_DISALLOW_COPY_AND_ASSIGN(Allocator) - - public: -#else struct upb_alloc { -#endif /* __cplusplus */ upb_alloc_func *func; }; @@ -605,212 +283,91 @@ UPB_INLINE void upb_gfree(void *ptr) { upb_free(&upb_alloc_global, ptr); } -/* upb::Arena *****************************************************************/ +/* upb_arena ******************************************************************/ -/* upb::Arena is a specific allocator implementation that uses arena allocation. +/* 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. + * A upb_arena is *not* thread-safe. * * You could write a thread-safe arena allocator that satisfies the - * upb::Allocator interface, but it would not be as efficient for the + * upb_alloc interface, but it would not be as efficient for the * single-threaded case. */ -UPB_DECLARE_TYPE(upb::Arena, upb_arena) typedef void upb_cleanup_func(void *ud); -#define UPB_ARENA_BLOCK_OVERHEAD (sizeof(size_t)*4) +struct upb_arena; +typedef struct upb_arena upb_arena; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif -void upb_arena_init(upb_arena *a); -void upb_arena_init2(upb_arena *a, void *mem, size_t n, upb_alloc *alloc); -void upb_arena_uninit(upb_arena *a); -bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud); +/* Creates an arena from the given initial block (if any -- n may be 0). + * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this + * is a fixed-size arena and cannot grow. */ +upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc); +void upb_arena_free(upb_arena *a); +bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func); size_t upb_arena_bytesallocated(const upb_arena *a); -void upb_arena_setnextblocksize(upb_arena *a, size_t size); -void upb_arena_setmaxblocksize(upb_arena *a, size_t size); + UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } -UPB_END_EXTERN_C +/* Convenience wrappers around upb_alloc functions. */ + +UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) { + return upb_malloc(upb_arena_alloc(a), size); +} + +UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize, + size_t size) { + return upb_realloc(upb_arena_alloc(a), ptr, oldsize, size); +} + +UPB_INLINE upb_arena *upb_arena_new() { + return upb_arena_init(NULL, 0, &upb_alloc_global); +} #ifdef __cplusplus +} /* extern "C" */ class upb::Arena { public: /* A simple arena with no initial memory block and the default allocator. */ - Arena() { upb_arena_init(this); } + Arena() : ptr_(upb_arena_new(), upb_arena_free) {} - /* Constructs an arena with the given initial block which allocates blocks - * with the given allocator. The given allocator must outlive the Arena. - * - * If you pass NULL for the allocator it will default to the global allocator - * upb_alloc_global, and NULL/0 for the initial block will cause there to be - * no initial block. */ - Arena(void *mem, size_t len, Allocator* a) { - upb_arena_init2(this, mem, len, a); - } - - ~Arena() { upb_arena_uninit(this); } - - /* Sets the size of the next block the Arena will request (unless the - * requested allocation is larger). Each block will double in size until the - * max limit is reached. */ - void SetNextBlockSize(size_t size) { upb_arena_setnextblocksize(this, size); } - - /* Sets the maximum block size. No blocks larger than this will be requested - * from the underlying allocator unless individual arena allocations are - * larger. */ - void SetMaxBlockSize(size_t size) { upb_arena_setmaxblocksize(this, size); } + upb_arena* ptr() { return ptr_.get(); } /* Allows this arena to be used as a generic allocator. * * The arena does not need free() calls so when using Arena as an allocator * it is safe to skip them. However they are no-ops so there is no harm in * calling free() either. */ - Allocator* allocator() { return upb_arena_alloc(this); } + upb_alloc *allocator() { return upb_arena_alloc(ptr_.get()); } /* Add a cleanup function to run when the arena is destroyed. * Returns false on out-of-memory. */ - bool AddCleanup(upb_cleanup_func* func, void* ud) { - return upb_arena_addcleanup(this, func, ud); + bool AddCleanup(void *ud, upb_cleanup_func* func) { + return upb_arena_addcleanup(ptr_.get(), ud, func); } /* Total number of bytes that have been allocated. It is undefined what - * Realloc() does to this counter. */ - size_t BytesAllocated() const { - return upb_arena_bytesallocated(this); - } - - private: - UPB_DISALLOW_COPY_AND_ASSIGN(Arena) - -#else -struct upb_arena { -#endif /* __cplusplus */ - /* We implement the allocator interface. - * This must be the first member of upb_arena! */ - upb_alloc alloc; - - /* Allocator to allocate arena blocks. We are responsible for freeing these - * when we are destroyed. */ - upb_alloc *block_alloc; - - size_t bytes_allocated; - size_t next_block_size; - size_t max_block_size; - - /* Linked list of blocks. Points to an arena_block, defined in env.c */ - void *block_head; - - /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */ - void *cleanup_head; - - /* For future expansion, since the size of this struct is exposed to users. */ - void *future1; - void *future2; -}; - - -/* upb::Environment ***********************************************************/ - -/* A upb::Environment provides a means for injecting malloc and an - * error-reporting callback into encoders/decoders. This allows them to be - * independent of nearly all assumptions about their actual environment. - * - * It is also a container for allocating the encoders/decoders themselves that - * insulates clients from knowing their actual size. This provides ABI - * compatibility even if the size of the objects change. And this allows the - * structure definitions to be in the .c files instead of the .h files, making - * the .h files smaller and more readable. - * - * We might want to consider renaming this to "Pipeline" if/when the concept of - * a pipeline element becomes more formalized. */ -UPB_DECLARE_TYPE(upb::Environment, upb_env) - -/* A function that receives an error report from an encoder or decoder. The - * callback can return true to request that the error should be recovered, but - * if the error is not recoverable this has no effect. */ -typedef bool upb_error_func(void *ud, const upb_status *status); - -UPB_BEGIN_EXTERN_C - -void upb_env_init(upb_env *e); -void upb_env_init2(upb_env *e, void *mem, size_t n, upb_alloc *alloc); -void upb_env_uninit(upb_env *e); - -void upb_env_initonly(upb_env *e); - -UPB_INLINE upb_arena *upb_env_arena(upb_env *e) { return (upb_arena*)e; } -bool upb_env_ok(const upb_env *e); -void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud); - -/* Convenience wrappers around the methods of the contained arena. */ -void upb_env_reporterrorsto(upb_env *e, upb_status *s); -bool upb_env_reporterror(upb_env *e, const upb_status *s); -void *upb_env_malloc(upb_env *e, size_t size); -void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size); -void upb_env_free(upb_env *e, void *ptr); -bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud); -size_t upb_env_bytesallocated(const upb_env *e); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -class upb::Environment { - public: - /* The given Arena must outlive this environment. */ - Environment() { upb_env_initonly(this); } - - Environment(void *mem, size_t len, Allocator *a) : arena_(mem, len, a) { - upb_env_initonly(this); - } - - Arena* arena() { return upb_env_arena(this); } - - /* Set a custom error reporting function. */ - void SetErrorFunction(upb_error_func* func, void* ud) { - upb_env_seterrorfunc(this, func, ud); - } - - /* Set the error reporting function to simply copy the status to the given - * status and abort. */ - void ReportErrorsTo(Status* status) { upb_env_reporterrorsto(this, status); } - - /* Returns true if all allocations and AddCleanup() calls have succeeded, - * and no errors were reported with ReportError() (except ones that recovered - * successfully). */ - bool ok() const { return upb_env_ok(this); } - - /* Reports an error to this environment's callback, returning true if - * the caller should try to recover. */ - bool ReportError(const Status* status) { - return upb_env_reporterror(this, status); - } + * Realloc() does to &arena_ counter. */ + size_t BytesAllocated() const { return upb_arena_bytesallocated(ptr_.get()); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(Environment) - -#else -struct upb_env { -#endif /* __cplusplus */ - upb_arena arena_; - upb_error_func *error_func_; - void *error_ud_; - bool ok_; + std::unique_ptr ptr_; }; +#endif /* upb::InlinedArena **********************************************************/ -/* upb::InlinedEnvironment ****************************************************/ -/* upb::InlinedArena and upb::InlinedEnvironment seed their arenas with a - * predefined amount of memory. No heap memory will be allocated until the - * initial block is exceeded. +/* upb::InlinedArena seeds the arenas with a predefined amount of memory. No + * heap memory will be allocated until the initial block is exceeded. * * These types only exist in C++ */ @@ -818,7589 +375,5824 @@ struct upb_env { template class upb::InlinedArena : public upb::Arena { public: - InlinedArena() : Arena(initial_block_, N, NULL) {} - explicit InlinedArena(Allocator* a) : Arena(initial_block_, N, a) {} - - private: - UPB_DISALLOW_COPY_AND_ASSIGN(InlinedArena) + InlinedArena() : ptr_(upb_arena_new(&initial_block_, N, &upb_alloc_global)) {} - char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD]; -}; - -template class upb::InlinedEnvironment : public upb::Environment { - public: - InlinedEnvironment() : Environment(initial_block_, N, NULL) {} - explicit InlinedEnvironment(Allocator *a) - : Environment(initial_block_, N, a) {} + upb_arena* ptr() { return ptr_.get(); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(InlinedEnvironment) + InlinedArena(const InlinedArena*) = delete; + InlinedArena& operator=(const InlinedArena*) = delete; - char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD]; + std::unique_ptr ptr_; + char initial_block_[N]; }; #endif /* __cplusplus */ +/* Constants ******************************************************************/ +/* Generic function type. */ +typedef void upb_func(); -#endif /* UPB_H_ */ - -#ifdef __cplusplus -extern "C" { -#endif - - -/* upb_value ******************************************************************/ +/* A list of types as they are encoded on-the-wire. */ +typedef enum { + UPB_WIRE_TYPE_VARINT = 0, + UPB_WIRE_TYPE_64BIT = 1, + UPB_WIRE_TYPE_DELIMITED = 2, + UPB_WIRE_TYPE_START_GROUP = 3, + UPB_WIRE_TYPE_END_GROUP = 4, + UPB_WIRE_TYPE_32BIT = 5 +} upb_wiretype_t; -/* A tagged union (stored untagged inside the table) so that we can check that - * clients calling table accessors are correctly typed without having to have - * an explosion of accessors. */ +/* 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). */ typedef enum { - UPB_CTYPE_INT32 = 1, - UPB_CTYPE_INT64 = 2, - UPB_CTYPE_UINT32 = 3, - UPB_CTYPE_UINT64 = 4, - UPB_CTYPE_BOOL = 5, - UPB_CTYPE_CSTR = 6, - UPB_CTYPE_PTR = 7, - UPB_CTYPE_CONSTPTR = 8, - UPB_CTYPE_FPTR = 9, - UPB_CTYPE_FLOAT = 10, - UPB_CTYPE_DOUBLE = 11 -} upb_ctype_t; - -typedef struct { - uint64_t val; -#ifndef NDEBUG - /* In debug mode we carry the value type around also so we can check accesses - * to be sure the right member is being read. */ - upb_ctype_t ctype; -#endif -} upb_value; - -#ifdef NDEBUG -#define SET_TYPE(dest, val) UPB_UNUSED(val) -#else -#define SET_TYPE(dest, val) dest = val -#endif + /* Types stored in 1 byte. */ + UPB_TYPE_BOOL = 1, + /* Types stored in 4 bytes. */ + UPB_TYPE_FLOAT = 2, + UPB_TYPE_INT32 = 3, + UPB_TYPE_UINT32 = 4, + UPB_TYPE_ENUM = 5, /* Enum values are int32. */ + /* Types stored as pointers (probably 4 or 8 bytes). */ + UPB_TYPE_STRING = 6, + UPB_TYPE_BYTES = 7, + UPB_TYPE_MESSAGE = 8, + /* Types stored as 8 bytes. */ + UPB_TYPE_DOUBLE = 9, + UPB_TYPE_INT64 = 10, + UPB_TYPE_UINT64 = 11 +} upb_fieldtype_t; -/* Like strdup(), which isn't always available since it's not ANSI C. */ -char *upb_strdup(const char *s, upb_alloc *a); -/* 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_alloc *a); +/* The repeated-ness of each field; this matches descriptor.proto. */ +typedef enum { + UPB_LABEL_OPTIONAL = 1, + UPB_LABEL_REQUIRED = 2, + UPB_LABEL_REPEATED = 3 +} upb_label_t; -UPB_INLINE char *upb_gstrdup(const char *s) { - return upb_strdup(s, &upb_alloc_global); -} +/* Descriptor types, as defined in descriptor.proto. */ +typedef enum { + UPB_DESCRIPTOR_TYPE_DOUBLE = 1, + UPB_DESCRIPTOR_TYPE_FLOAT = 2, + UPB_DESCRIPTOR_TYPE_INT64 = 3, + UPB_DESCRIPTOR_TYPE_UINT64 = 4, + UPB_DESCRIPTOR_TYPE_INT32 = 5, + UPB_DESCRIPTOR_TYPE_FIXED64 = 6, + UPB_DESCRIPTOR_TYPE_FIXED32 = 7, + UPB_DESCRIPTOR_TYPE_BOOL = 8, + UPB_DESCRIPTOR_TYPE_STRING = 9, + UPB_DESCRIPTOR_TYPE_GROUP = 10, + UPB_DESCRIPTOR_TYPE_MESSAGE = 11, + UPB_DESCRIPTOR_TYPE_BYTES = 12, + UPB_DESCRIPTOR_TYPE_UINT32 = 13, + UPB_DESCRIPTOR_TYPE_ENUM = 14, + UPB_DESCRIPTOR_TYPE_SFIXED32 = 15, + UPB_DESCRIPTOR_TYPE_SFIXED64 = 16, + UPB_DESCRIPTOR_TYPE_SINT32 = 17, + UPB_DESCRIPTOR_TYPE_SINT64 = 18 +} upb_descriptortype_t; -UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val, - upb_ctype_t ctype) { - v->val = val; - SET_TYPE(v->ctype, ctype); -} +extern const uint8_t upb_desctype_to_fieldtype[]; -UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) { - upb_value ret; - _upb_value_setval(&ret, val, ctype); - return ret; -} +#endif /* UPB_H_ */ +/* +** structs.int.h: structures definitions that are internal to upb. +*/ -/* 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, proto_type) \ - UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \ - val->val = (converter)cval; \ - SET_TYPE(val->ctype, proto_type); \ - } \ - 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) { \ - UPB_ASSERT_DEBUGVAR(val.ctype == proto_type); \ - return (type_t)(converter)val.val; \ - } +#ifndef UPB_STRUCTS_H_ +#define UPB_STRUCTS_H_ -FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32) -FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64) -FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32) -FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64) -FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL) -FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR) -FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR) -FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR) -FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR) -#undef FUNCS +struct upb_array { + upb_fieldtype_t type; + uint8_t element_size; + void *data; /* Each element is element_size. */ + size_t len; /* Measured in elements. */ + size_t size; /* Measured in elements. */ + upb_arena *arena; +}; -UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) { - memcpy(&val->val, &cval, sizeof(cval)); - SET_TYPE(val->ctype, UPB_CTYPE_FLOAT); -} +#endif /* UPB_STRUCTS_H_ */ -UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) { - memcpy(&val->val, &cval, sizeof(cval)); - SET_TYPE(val->ctype, UPB_CTYPE_DOUBLE); -} -UPB_INLINE upb_value upb_value_float(float cval) { - upb_value ret; - upb_value_setfloat(&ret, cval); - return ret; -} +#ifdef __cplusplus -UPB_INLINE upb_value upb_value_double(double cval) { - upb_value ret; - upb_value_setdouble(&ret, cval); - return ret; +namespace upb { +class Array; +class Map; +class MapIterator; +class MessageLayout; } -#undef SET_TYPE +#endif +/* TODO(haberman): C++ accessors */ -/* upb_tabkey *****************************************************************/ +#ifdef __cplusplus +extern "C" { +#endif -/* Either: - * 1. an actual integer key, or - * 2. a pointer to a string prefixed by its uint32_t length, owned by us. - * - * ...depending on whether this is a string table or an int table. We would - * make this a union of those two types, but C89 doesn't support statically - * initializing a non-first union member. */ -typedef uintptr_t upb_tabkey; +typedef void upb_msg; -#define UPB_TABKEY_NUM(n) n -#define UPB_TABKEY_NONE 0 -/* The preprocessor isn't quite powerful enough to turn the compile-time string - * length into a byte-wise string representation, so code generation needs to - * help it along. - * - * "len1" is the low byte and len4 is the high byte. */ -#ifdef UPB_BIG_ENDIAN -#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ - (uintptr_t)(len4 len3 len2 len1 strval) -#else -#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ - (uintptr_t)(len1 len2 len3 len4 strval) -#endif +struct upb_array; +typedef struct upb_array upb_array; -UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) { - char* mem = (char*)key; - if (len) memcpy(len, mem, sizeof(*len)); - return mem + sizeof(*len); -} +struct upb_map; +typedef struct upb_map upb_map; +struct upb_mapiter; +typedef struct upb_mapiter upb_mapiter; -/* upb_tabval *****************************************************************/ +/** upb_msglayout *************************************************************/ -#ifdef __cplusplus +/* upb_msglayout represents the memory layout of a given upb_msgdef. The + * members are public so generated code can initialize them, but users MUST NOT + * read or write any of its members. */ -/* Status initialization not supported. - * - * This separate definition is necessary because in C++, UINTPTR_MAX isn't - * reliably available. */ typedef struct { - uint64_t val; -} upb_tabval; + uint32_t number; + uint16_t offset; + int16_t presence; /* If >0, hasbit_index+1. If <0, oneof_index+1. */ + uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */ + uint8_t descriptortype; + uint8_t label; +} upb_msglayout_field; -#else +typedef struct upb_msglayout { + const struct upb_msglayout *const* submsgs; + const upb_msglayout_field *fields; + /* Must be aligned to sizeof(void*). Doesn't include internal members like + * unknown fields, extension dict, pointer to msglayout, etc. */ + uint16_t size; + uint16_t field_count; + bool extendable; +} upb_msglayout; -/* C -- supports static initialization, but to support static initialization of - * both integers and points for both 32 and 64 bit targets, it takes a little - * bit of doing. */ +/** upb_strview ************************************************************/ -#if UINTPTR_MAX == 0xffffffffffffffffULL -#define UPB_PTR_IS_64BITS -#elif UINTPTR_MAX != 0xffffffff -#error Could not determine how many bits pointers are. -#endif +typedef struct { + const char *data; + size_t size; +} upb_strview; -typedef union { - /* For static initialization. - * - * Unfortunately this ugliness is necessary -- it is the only way that we can, - * with -std=c89 -pedantic, statically initialize this to either a pointer or - * an integer on 32-bit platforms. */ - struct { -#ifdef UPB_PTR_IS_64BITS - uintptr_t val; -#else - uintptr_t val1; - uintptr_t val2; -#endif - } staticinit; +UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) { + upb_strview ret; + ret.data = data; + ret.size = size; + return ret; +} - /* The normal accessor that we use for everything at runtime. */ - uint64_t val; -} upb_tabval; +UPB_INLINE upb_strview upb_strview_makez(const char *data) { + return upb_strview_make(data, strlen(data)); +} -#ifdef UPB_PTR_IS_64BITS -#define UPB_TABVALUE_INT_INIT(v) {{v}} -#define UPB_TABVALUE_EMPTY_INIT {{-1}} -#else +UPB_INLINE bool upb_strview_eql(upb_strview a, upb_strview b) { + return a.size == b.size && memcmp(a.data, b.data, a.size) == 0; +} -/* 32-bit pointers */ +#define UPB_STRVIEW_INIT(ptr, len) {ptr, len} -#ifdef UPB_BIG_ENDIAN -#define UPB_TABVALUE_INT_INIT(v) {{0, v}} -#define UPB_TABVALUE_EMPTY_INIT {{-1, -1}} -#else -#define UPB_TABVALUE_INT_INIT(v) {{v, 0}} -#define UPB_TABVALUE_EMPTY_INIT {{-1, -1}} -#endif +#define UPB_STRVIEW_FORMAT "%.*s" +#define UPB_STRVIEW_ARGS(view) (int)(view).size, (view).data -#endif +/** upb_msgval ****************************************************************/ -#define UPB_TABVALUE_PTR_INIT(v) UPB_TABVALUE_INT_INIT((uintptr_t)v) +/* A union representing all possible protobuf values. Used for generic get/set + * operations. */ -#undef UPB_PTR_IS_64BITS +typedef union { + bool b; + float flt; + double dbl; + int32_t i32; + int64_t i64; + uint32_t u32; + uint64_t u64; + const upb_map* map; + const upb_msg* msg; + const upb_array* arr; + const void* ptr; + upb_strview str; +} upb_msgval; -#endif /* __cplusplus */ +#define ACCESSORS(name, membername, ctype) \ + UPB_INLINE ctype upb_msgval_get ## name(upb_msgval v) { \ + return v.membername; \ + } \ + UPB_INLINE void upb_msgval_set ## name(upb_msgval *v, ctype cval) { \ + v->membername = cval; \ + } \ + UPB_INLINE upb_msgval upb_msgval_ ## name(ctype v) { \ + upb_msgval ret; \ + ret.membername = v; \ + return ret; \ + } +ACCESSORS(bool, b, bool) +ACCESSORS(float, flt, float) +ACCESSORS(double, dbl, double) +ACCESSORS(int32, i32, int32_t) +ACCESSORS(int64, i64, int64_t) +ACCESSORS(uint32, u32, uint32_t) +ACCESSORS(uint64, u64, uint64_t) +ACCESSORS(map, map, const upb_map*) +ACCESSORS(msg, msg, const upb_msg*) +ACCESSORS(ptr, ptr, const void*) +ACCESSORS(arr, arr, const upb_array*) +ACCESSORS(str, str, upb_strview) -/* upb_table ******************************************************************/ +#undef ACCESSORS -typedef struct _upb_tabent { - upb_tabkey key; - upb_tabval val; +UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) { + return upb_msgval_str(upb_strview_make(data, size)); +} - /* 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; +/** upb_msg *******************************************************************/ -typedef struct { - size_t count; /* Number of entries in the hash part. */ - size_t mask; /* Mask to turn hash value -> bucket. */ - upb_ctype_t ctype; /* Type of all values. */ - uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */ +/* A upb_msg represents a protobuf message. It always corresponds to a specific + * upb_msglayout, which describes how it is laid out in memory. */ - /* Hash table entries. - * Making this const isn't entirely accurate; what we really want is for it to - * have the same const-ness as the table it's inside. But there's no way to - * declare that in C. So we have to make it const so that we can statically - * initialize const hash tables. Then we cast away const when we have to. - */ - const upb_tabent *entries; +/* Creates a new message of the given type/layout in this arena. */ +upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a); -#ifndef NDEBUG - /* This table's allocator. We make the user pass it in to every relevant - * function and only use this to check it in debug mode. We do this solely - * to keep upb_table as small as possible. This might seem slightly paranoid - * but the plan is to use upb_table for all map fields and extension sets in - * a forthcoming message representation, so there could be a lot of these. - * If this turns out to be too annoying later, we can change it (since this - * is an internal-only header file). */ - upb_alloc *alloc; -#endif -} upb_table; +/* Returns the arena for the given message. */ +upb_arena *upb_msg_arena(const upb_msg *msg); -#ifdef NDEBUG -# define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \ - {count, mask, ctype, size_lg2, entries} -#else -# ifdef UPB_DEBUG_REFS -/* At the moment the only mutable tables we statically initialize are debug - * ref tables. */ -# define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \ - {count, mask, ctype, size_lg2, entries, &upb_alloc_debugrefs} -# else -# define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \ - {count, mask, ctype, size_lg2, entries, NULL} -# endif -#endif +void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len); +const char *upb_msg_getunknown(const upb_msg *msg, size_t *len); -typedef struct { - upb_table t; -} upb_strtable; +/* Read-only message API. Can be safely called by anyone. */ -#define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \ - {UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries)} +/* Returns the value associated with this field: + * - for scalar fields (including strings), the value directly. + * - return upb_msg*, or upb_map* for msg/map. + * If the field is unset for these field types, returns NULL. + * + * TODO(haberman): should we let users store cached array/map/msg + * pointers here for fields that are unset? Could be useful for the + * strongly-owned submessage model (ie. generated C API that doesn't use + * arenas). + */ +upb_msgval upb_msg_get(const upb_msg *msg, + int field_index, + const upb_msglayout *l); -#define UPB_EMPTY_STRTABLE_INIT(ctype) \ - UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL) +/* May only be called for fields where upb_fielddef_haspresence(f) == true. */ +bool upb_msg_has(const upb_msg *msg, + int field_index, + const upb_msglayout *l); -typedef struct { - upb_table t; /* For entries that don't fit in the array part. */ - const upb_tabval *array; /* Array part of the table. See const note above. */ - size_t array_size; /* Array part size. */ - size_t array_count; /* Array part number of elements. */ -} upb_inttable; +/* Mutable message API. May only be called by the owner of the message who + * knows its ownership scheme and how to keep it consistent. */ -#define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \ - {UPB_TABLE_INIT(count, mask, ctype, size_lg2, ent), a, asize, acount} +/* Sets the given field to the given value. Does not perform any memory + * management: if you overwrite a pointer to a msg/array/map/string without + * cleaning it up (or using an arena) it will leak. + */ +void upb_msg_set(upb_msg *msg, + int field_index, + upb_msgval val, + const upb_msglayout *l); -#define UPB_EMPTY_INTTABLE_INIT(ctype) \ - UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0) +/* For a primitive field, set it back to its default. For repeated, string, and + * submessage fields set it back to NULL. This could involve releasing some + * internal memory (for example, from an extension dictionary), but it is not + * recursive in any way and will not recover any memory that may be used by + * arrays/maps/strings/msgs that this field may have pointed to. + */ +bool upb_msg_clearfield(upb_msg *msg, + int field_index, + const upb_msglayout *l); -#define UPB_ARRAY_EMPTYENT -1 +/* TODO(haberman): copyfrom()/mergefrom()? */ -UPB_INLINE size_t upb_table_size(const upb_table *t) { - if (t->size_lg2 == 0) - return 0; - else - return 1 << t->size_lg2; -} +/** upb_array *****************************************************************/ -/* Internal-only functions, in .h file only out of necessity. */ -UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) { - return e->key == 0; -} +/* A upb_array stores data for a repeated field. The memory management + * semantics are the same as upb_msg. A upb_array allocates dynamic + * memory internally for the array elements. */ -/* Used by some of the unit tests for generic hashing functionality. */ -uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed); +upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a); +upb_fieldtype_t upb_array_type(const upb_array *arr); -UPB_INLINE uintptr_t upb_intkey(uintptr_t key) { - return key; -} +/* Read-only interface. Safe for anyone to call. */ -UPB_INLINE uint32_t upb_inthash(uintptr_t key) { - return (uint32_t)key; -} +size_t upb_array_size(const upb_array *arr); +upb_msgval upb_array_get(const upb_array *arr, size_t i); -static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) { - return t->entries + (hash & t->mask); +/* Write interface. May only be called by the message's owner who can enforce + * its memory management invariants. */ + +bool upb_array_set(upb_array *arr, size_t i, upb_msgval val); + +/** upb_map *******************************************************************/ + +/* A upb_map stores data for a map field. The memory management semantics are + * the same as upb_msg, with one notable exception. upb_map will internally + * store a copy of all string keys, but *not* any string values or submessages. + * So you must ensure that any string or message values outlive the map, and you + * must delete them manually when they are no longer required. */ + +upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype, + upb_arena *a); + +/* Read-only interface. Safe for anyone to call. */ + +size_t upb_map_size(const upb_map *map); +upb_fieldtype_t upb_map_keytype(const upb_map *map); +upb_fieldtype_t upb_map_valuetype(const upb_map *map); +bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val); + +/* Write interface. May only be called by the message's owner who can enforce + * its memory management invariants. */ + +/* Sets or overwrites an entry in the map. Return value indicates whether + * the operation succeeded or failed with OOM, and also whether an existing + * key was replaced or not. */ +bool upb_map_set(upb_map *map, + upb_msgval key, upb_msgval val, + upb_msgval *valremoved); + +/* Deletes an entry in the map. Returns true if the key was present. */ +bool upb_map_del(upb_map *map, upb_msgval key); + +/** upb_mapiter ***************************************************************/ + +/* For iterating over a map. Map iterators are invalidated by mutations to the + * map, but an invalidated iterator will never return junk or crash the process. + * An invalidated iterator may return entries that were already returned though, + * and if you keep invalidating the iterator during iteration, the program may + * enter an infinite loop. */ + +size_t upb_mapiter_sizeof(); + +void upb_mapiter_begin(upb_mapiter *i, const upb_map *t); +upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a); +void upb_mapiter_free(upb_mapiter *i, upb_alloc *a); +void upb_mapiter_next(upb_mapiter *i); +bool upb_mapiter_done(const upb_mapiter *i); + +upb_msgval upb_mapiter_key(const upb_mapiter *i); +upb_msgval upb_mapiter_value(const upb_mapiter *i); +void upb_mapiter_setdone(upb_mapiter *i); +bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* UPB_MSG_H_ */ +/* This file was generated by upbc (the upb compiler) from the input + * file: + * + * google/protobuf/descriptor.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. */ + +#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ +#define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ + +/* +** Functions for use by generated code. These are not public and users must +** not call them directly. +*/ + +#ifndef UPB_GENERATED_UTIL_H_ +#define UPB_GENERATED_UTIL_H_ + +#include + +#define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs) + +UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs, + size_t *size) { + const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*); + if (arr) { + if (size) *size = arr->len; + return arr->data; + } else { + if (size) *size = 0; + return NULL; + } } -UPB_INLINE bool upb_arrhas(upb_tabval key) { - return key.val != (uint64_t)-1; +UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs, + size_t *size) { + upb_array *arr = *PTR_AT(msg, ofs, upb_array*); + if (arr) { + if (size) *size = arr->len; + return arr->data; + } else { + if (size) *size = 0; + return NULL; + } } -/* Initialize and uninitialize a table, respectively. If memory allocation - * failed, false is returned that the table is uninitialized. */ -bool upb_inttable_init2(upb_inttable *table, upb_ctype_t ctype, upb_alloc *a); -bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype, upb_alloc *a); -void upb_inttable_uninit2(upb_inttable *table, upb_alloc *a); -void upb_strtable_uninit2(upb_strtable *table, upb_alloc *a); +/* TODO(haberman): this is a mess. It will improve when upb_array no longer + * carries reflective state (type, elem_size). */ +UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size, + size_t elem_size, + upb_fieldtype_t type, + upb_arena *arena) { + upb_array *arr = *PTR_AT(msg, ofs, upb_array*); -UPB_INLINE bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype) { - return upb_inttable_init2(table, ctype, &upb_alloc_global); + if (!arr) { + arr = upb_array_new(type, arena); + if (!arr) return NULL; + *PTR_AT(msg, ofs, upb_array*) = arr; + } + + if (size > arr->size) { + size_t new_size = UPB_MAX(arr->size, 4); + size_t old_bytes = arr->size * elem_size; + size_t new_bytes; + while (new_size < size) new_size *= 2; + new_bytes = new_size * elem_size; + arr->data = upb_arena_realloc(arena, arr->data, old_bytes, new_bytes); + if (!arr->data) { + return NULL; + } + arr->size = new_size; + } + + arr->len = size; + return arr->data; +} + +UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs, + size_t elem_size, + upb_fieldtype_t type, + const void *value, + upb_arena *arena) { + upb_array *arr = *PTR_AT(msg, ofs, upb_array*); + size_t i = arr ? arr->len : 0; + void *data = + _upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena); + if (!data) return false; + memcpy(PTR_AT(data, i * elem_size, char), value, elem_size); + return true; } -UPB_INLINE bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype) { - return upb_strtable_init2(table, ctype, &upb_alloc_global); +UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) { + return (*PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0; } -UPB_INLINE void upb_inttable_uninit(upb_inttable *table) { - upb_inttable_uninit2(table, &upb_alloc_global); +UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) { + return (*PTR_AT(msg, idx / 8, char)) |= (1 << (idx % 8)); } -UPB_INLINE void upb_strtable_uninit(upb_strtable *table) { - upb_strtable_uninit2(table, &upb_alloc_global); +UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) { + return (*PTR_AT(msg, idx / 8, char)) &= ~(1 << (idx % 8)); } -/* Returns the number of values in the table. */ -size_t upb_inttable_count(const upb_inttable *t); -UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) { - return t->t.count; +UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) { + return *PTR_AT(msg, case_ofs, int32_t) == num; } -void upb_inttable_packedsize(const upb_inttable *t, size_t *size); -void upb_strtable_packedsize(const upb_strtable *t, size_t *size); -upb_inttable *upb_inttable_pack(const upb_inttable *t, void *p, size_t *ofs, - size_t size); -upb_strtable *upb_strtable_pack(const upb_strtable *t, void *p, size_t *ofs, - size_t size); +#undef PTR_AT -/* Inserts the given key into the hashtable with the given value. The key must - * not already exist in the hash table. For string tables, the key must be - * NULL-terminated, and the table will make an internal copy of the key. - * Inttables must not insert a value of UINTPTR_MAX. - * - * If a table resize was required but memory allocation failed, false is - * returned and the table is unchanged. */ -bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val, - upb_alloc *a); -bool upb_strtable_insert3(upb_strtable *t, const char *key, size_t len, - upb_value val, upb_alloc *a); +#endif /* UPB_GENERATED_UTIL_H_ */ -UPB_INLINE bool upb_inttable_insert(upb_inttable *t, uintptr_t key, - upb_value val) { - return upb_inttable_insert2(t, key, val, &upb_alloc_global); -} -UPB_INLINE bool upb_strtable_insert2(upb_strtable *t, const char *key, - size_t len, upb_value val) { - return upb_strtable_insert3(t, key, len, val, &upb_alloc_global); -} +/* +** upb_decode: parsing into a upb_msg using a upb_msglayout. +*/ -/* For NULL-terminated strings. */ -UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key, - upb_value val) { - return upb_strtable_insert2(t, key, strlen(key), val); -} - -/* 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); -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_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val); -bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len, - upb_value *val, upb_alloc *alloc); - -UPB_INLINE bool upb_strtable_remove2(upb_strtable *t, const char *key, - size_t len, upb_value *val) { - return upb_strtable_remove3(t, key, len, val, &upb_alloc_global); -} - -/* For NULL-terminated strings. */ -UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key, - upb_value *v) { - return upb_strtable_remove2(t, key, strlen(key), v); -} - -/* 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); - -/* Handy routines for treating an inttable like a stack. May not be mixed with - * other insert/remove calls. */ -bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a); -upb_value upb_inttable_pop(upb_inttable *t); - -UPB_INLINE bool upb_inttable_push(upb_inttable *t, upb_value val) { - return upb_inttable_push2(t, val, &upb_alloc_global); -} - -/* Convenience routines for inttables with pointer keys. */ -bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val, - upb_alloc *a); -bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val); -bool upb_inttable_lookupptr( - const upb_inttable *t, const void *key, upb_value *val); - -UPB_INLINE bool upb_inttable_insertptr(upb_inttable *t, const void *key, - upb_value val) { - return upb_inttable_insertptr2(t, key, val, &upb_alloc_global); -} - -/* 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. */ -void upb_inttable_compact2(upb_inttable *t, upb_alloc *a); - -UPB_INLINE void upb_inttable_compact(upb_inttable *t) { - upb_inttable_compact2(t, &upb_alloc_global); -} - -/* A special-case inlinable version of the lookup routine for 32-bit - * integers. */ -UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key, - upb_value *v) { - *v = upb_value_int32(0); /* Silence compiler warnings. */ - if (key < t->array_size) { - upb_tabval arrval = t->array[key]; - if (upb_arrhas(arrval)) { - _upb_value_setval(v, arrval.val, t->t.ctype); - return true; - } else { - return false; - } - } else { - const upb_tabent *e; - if (t->t.entries == NULL) return false; - for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) { - if ((uint32_t)e->key == key) { - _upb_value_setval(v, e->val.val, t->t.ctype); - return true; - } - if (e->next == NULL) return false; - } - } -} - -/* Exposed for testing only. */ -bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a); - -/* Iterators ******************************************************************/ - -/* Iterators for int and 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; - -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); -const char *upb_strtable_iter_key(const upb_strtable_iter *i); -size_t upb_strtable_iter_keylength(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); - - -/* upb_inttable_iter **********************************************************/ - -/* upb_inttable_iter i; - * upb_inttable_begin(&i, t); - * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - * uintptr_t key = upb_inttable_iter_key(&i); - * upb_value val = upb_inttable_iter_value(&i); - * // ... - * } - */ +#ifndef UPB_DECODE_H_ +#define UPB_DECODE_H_ -typedef struct { - const upb_inttable *t; - size_t index; - bool array_part; -} upb_inttable_iter; -void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t); -void upb_inttable_next(upb_inttable_iter *i); -bool upb_inttable_done(const upb_inttable_iter *i); -uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i); -upb_value upb_inttable_iter_value(const upb_inttable_iter *i); -void upb_inttable_iter_setdone(upb_inttable_iter *i); -bool upb_inttable_iter_isequal(const upb_inttable_iter *i1, - const upb_inttable_iter *i2); +#ifdef __cplusplus +extern "C" { +#endif +bool upb_decode(const char *buf, size_t size, upb_msg *msg, + const upb_msglayout *l); #ifdef __cplusplus } /* extern "C" */ #endif -#endif /* UPB_TABLE_H_ */ +#endif /* UPB_DECODE_H_ */ +/* +** upb_encode: parsing into a upb_msg using a upb_msglayout. +*/ -/* Reference tracking will check ref()/unref() operations to make sure the - * ref ownership is correct. Where possible it will also make tools like - * Valgrind attribute ref leaks to the code that took the leaked ref, not - * the code that originally created the object. - * - * Enabling this requires the application to define upb_lock()/upb_unlock() - * functions that acquire/release a global mutex (or #define UPB_THREAD_UNSAFE). - * For this reason we don't enable it by default, even in debug builds. - */ +#ifndef UPB_ENCODE_H_ +#define UPB_ENCODE_H_ -/* #define UPB_DEBUG_REFS */ #ifdef __cplusplus -namespace upb { -class RefCounted; -template class reffed_ptr; -} +extern "C" { #endif -UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted) - -struct upb_refcounted_vtbl; +char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena, + size_t *size); #ifdef __cplusplus - -class upb::RefCounted { - public: - /* Returns true if the given object is frozen. */ - bool IsFrozen() const; - - /* Increases the ref count, the new ref is owned by "owner" which must not - * already own a ref (and should not itself be a refcounted object if the ref - * could possibly be circular; see below). - * Thread-safe iff "this" is frozen. */ - void Ref(const void *owner) const; - - /* Release a ref that was acquired from upb_refcounted_ref() and collects any - * objects it can. */ - void Unref(const void *owner) const; - - /* Moves an existing ref from "from" to "to", without changing the overall - * ref count. DonateRef(foo, NULL, owner) is the same as Ref(foo, owner), - * but "to" may not be NULL. */ - void DonateRef(const void *from, const void *to) const; - - /* Verifies that a ref to the given object is currently held by the given - * owner. Only effective in UPB_DEBUG_REFS builds. */ - void CheckRef(const void *owner) const; - - private: - UPB_DISALLOW_POD_OPS(RefCounted, upb::RefCounted) -#else -struct upb_refcounted { -#endif - /* TODO(haberman): move the actual structure definition to structdefs.int.h. - * The only reason they are here is because inline functions need to see the - * definition of upb_handlers, which needs to see this definition. But we - * can change the upb_handlers inline functions to deal in raw offsets - * instead. - */ - - /* A single reference count shared by all objects in the group. */ - uint32_t *group; - - /* A singly-linked list of all objects in the group. */ - upb_refcounted *next; - - /* Table of function pointers for this type. */ - const struct upb_refcounted_vtbl *vtbl; - - /* Maintained only when mutable, this tracks the number of refs (but not - * ref2's) to this object. *group should be the sum of all individual_count - * in the group. */ - uint32_t individual_count; - - bool is_frozen; - -#ifdef UPB_DEBUG_REFS - upb_inttable *refs; /* Maps owner -> trackedref for incoming refs. */ - upb_inttable *ref2s; /* Set of targets for outgoing ref2s. */ -#endif -}; - -#ifdef UPB_DEBUG_REFS -extern upb_alloc upb_alloc_debugrefs; -#define UPB_REFCOUNT_INIT(vtbl, refs, ref2s) \ - {&static_refcount, NULL, vtbl, 0, true, refs, ref2s} -#else -#define UPB_REFCOUNT_INIT(vtbl, refs, ref2s) \ - {&static_refcount, NULL, vtbl, 0, true} +} /* extern "C" */ #endif -UPB_BEGIN_EXTERN_C - -/* It is better to use tracked refs when possible, for the extra debugging - * capability. But if this is not possible (because you don't have easy access - * to a stable pointer value that is associated with the ref), you can pass - * UPB_UNTRACKED_REF instead. */ -extern const void *UPB_UNTRACKED_REF; - -/* Native C API. */ -bool upb_refcounted_isfrozen(const upb_refcounted *r); -void upb_refcounted_ref(const upb_refcounted *r, const void *owner); -void upb_refcounted_unref(const upb_refcounted *r, const void *owner); -void upb_refcounted_donateref( - const upb_refcounted *r, const void *from, const void *to); -void upb_refcounted_checkref(const upb_refcounted *r, const void *owner); - -#define UPB_REFCOUNTED_CMETHODS(type, upcastfunc) \ - UPB_INLINE bool type ## _isfrozen(const type *v) { \ - return upb_refcounted_isfrozen(upcastfunc(v)); \ - } \ - UPB_INLINE void type ## _ref(const type *v, const void *owner) { \ - upb_refcounted_ref(upcastfunc(v), owner); \ - } \ - UPB_INLINE void type ## _unref(const type *v, const void *owner) { \ - upb_refcounted_unref(upcastfunc(v), owner); \ - } \ - UPB_INLINE void type ## _donateref(const type *v, const void *from, const void *to) { \ - upb_refcounted_donateref(upcastfunc(v), from, to); \ - } \ - UPB_INLINE void type ## _checkref(const type *v, const void *owner) { \ - upb_refcounted_checkref(upcastfunc(v), owner); \ - } - -#define UPB_REFCOUNTED_CPPMETHODS \ - bool IsFrozen() const { \ - return upb::upcast_to(this)->IsFrozen(); \ - } \ - void Ref(const void *owner) const { \ - return upb::upcast_to(this)->Ref(owner); \ - } \ - void Unref(const void *owner) const { \ - return upb::upcast_to(this)->Unref(owner); \ - } \ - void DonateRef(const void *from, const void *to) const { \ - return upb::upcast_to(this)->DonateRef(from, to); \ - } \ - void CheckRef(const void *owner) const { \ - return upb::upcast_to(this)->CheckRef(owner); \ - } - -/* Internal-to-upb Interface **************************************************/ - -typedef void upb_refcounted_visit(const upb_refcounted *r, - const upb_refcounted *subobj, - void *closure); - -struct upb_refcounted_vtbl { - /* Must visit all subobjects that are currently ref'd via upb_refcounted_ref2. - * Must be longjmp()-safe. */ - void (*visit)(const upb_refcounted *r, upb_refcounted_visit *visit, void *c); - - /* Must free the object and release all references to other objects. */ - void (*free)(upb_refcounted *r); -}; - -/* Initializes the refcounted with a single ref for the given owner. Returns - * false if memory could not be allocated. */ -bool upb_refcounted_init(upb_refcounted *r, - const struct upb_refcounted_vtbl *vtbl, - const void *owner); - -/* Adds a ref from one refcounted object to another ("from" must not already - * own a ref). These refs may be circular; cycles will be collected correctly - * (if conservatively). These refs do not need to be freed in from's free() - * function. */ -void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from); - -/* Removes a ref that was acquired from upb_refcounted_ref2(), and collects any - * object it can. This is only necessary when "from" no longer points to "r", - * and not from from's "free" function. */ -void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from); - -#define upb_ref2(r, from) \ - upb_refcounted_ref2((const upb_refcounted*)r, (upb_refcounted*)from) -#define upb_unref2(r, from) \ - upb_refcounted_unref2((const upb_refcounted*)r, (upb_refcounted*)from) - -/* Freezes all mutable object reachable by ref2() refs from the given roots. - * This will split refcounting groups into precise SCC groups, so that - * refcounting of frozen objects can be more aggressive. If memory allocation - * fails, or if more than 2**31 mutable objects are reachable from "roots", or - * if the maximum depth of the graph exceeds "maxdepth", false is returned and - * the objects are unchanged. - * - * After this operation succeeds, the objects are frozen/const, and may not be - * used through non-const pointers. In particular, they may not be passed as - * the second parameter of upb_refcounted_{ref,unref}2(). On the upside, all - * operations on frozen refcounteds are threadsafe, and objects will be freed - * at the precise moment that they become unreachable. - * - * Caller must own refs on each object in the "roots" list. */ -bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s, - int maxdepth); - -/* Shared by all compiled-in refcounted objects. */ -extern uint32_t static_refcount; - -UPB_END_EXTERN_C - +#endif /* UPB_ENCODE_H_ */ #ifdef __cplusplus -/* C++ Wrappers. */ -namespace upb { -inline bool RefCounted::IsFrozen() const { - return upb_refcounted_isfrozen(this); -} -inline void RefCounted::Ref(const void *owner) const { - upb_refcounted_ref(this, owner); -} -inline void RefCounted::Unref(const void *owner) const { - upb_refcounted_unref(this, owner); -} -inline void RefCounted::DonateRef(const void *from, const void *to) const { - upb_refcounted_donateref(this, from, to); -} -inline void RefCounted::CheckRef(const void *owner) const { - upb_refcounted_checkref(this, owner); -} -} /* namespace upb */ +extern "C" { #endif +struct google_protobuf_FileDescriptorSet; +struct google_protobuf_FileDescriptorProto; +struct google_protobuf_DescriptorProto; +struct google_protobuf_DescriptorProto_ExtensionRange; +struct google_protobuf_DescriptorProto_ReservedRange; +struct google_protobuf_ExtensionRangeOptions; +struct google_protobuf_FieldDescriptorProto; +struct google_protobuf_OneofDescriptorProto; +struct google_protobuf_EnumDescriptorProto; +struct google_protobuf_EnumDescriptorProto_EnumReservedRange; +struct google_protobuf_EnumValueDescriptorProto; +struct google_protobuf_ServiceDescriptorProto; +struct google_protobuf_MethodDescriptorProto; +struct google_protobuf_FileOptions; +struct google_protobuf_MessageOptions; +struct google_protobuf_FieldOptions; +struct google_protobuf_OneofOptions; +struct google_protobuf_EnumOptions; +struct google_protobuf_EnumValueOptions; +struct google_protobuf_ServiceOptions; +struct google_protobuf_MethodOptions; +struct google_protobuf_UninterpretedOption; +struct google_protobuf_UninterpretedOption_NamePart; +struct google_protobuf_SourceCodeInfo; +struct google_protobuf_SourceCodeInfo_Location; +struct google_protobuf_GeneratedCodeInfo; +struct google_protobuf_GeneratedCodeInfo_Annotation; +typedef struct google_protobuf_FileDescriptorSet google_protobuf_FileDescriptorSet; +typedef struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto; +typedef struct google_protobuf_DescriptorProto google_protobuf_DescriptorProto; +typedef struct google_protobuf_DescriptorProto_ExtensionRange google_protobuf_DescriptorProto_ExtensionRange; +typedef struct google_protobuf_DescriptorProto_ReservedRange google_protobuf_DescriptorProto_ReservedRange; +typedef struct google_protobuf_ExtensionRangeOptions google_protobuf_ExtensionRangeOptions; +typedef struct google_protobuf_FieldDescriptorProto google_protobuf_FieldDescriptorProto; +typedef struct google_protobuf_OneofDescriptorProto google_protobuf_OneofDescriptorProto; +typedef struct google_protobuf_EnumDescriptorProto google_protobuf_EnumDescriptorProto; +typedef struct google_protobuf_EnumDescriptorProto_EnumReservedRange google_protobuf_EnumDescriptorProto_EnumReservedRange; +typedef struct google_protobuf_EnumValueDescriptorProto google_protobuf_EnumValueDescriptorProto; +typedef struct google_protobuf_ServiceDescriptorProto google_protobuf_ServiceDescriptorProto; +typedef struct google_protobuf_MethodDescriptorProto google_protobuf_MethodDescriptorProto; +typedef struct google_protobuf_FileOptions google_protobuf_FileOptions; +typedef struct google_protobuf_MessageOptions google_protobuf_MessageOptions; +typedef struct google_protobuf_FieldOptions google_protobuf_FieldOptions; +typedef struct google_protobuf_OneofOptions google_protobuf_OneofOptions; +typedef struct google_protobuf_EnumOptions google_protobuf_EnumOptions; +typedef struct google_protobuf_EnumValueOptions google_protobuf_EnumValueOptions; +typedef struct google_protobuf_ServiceOptions google_protobuf_ServiceOptions; +typedef struct google_protobuf_MethodOptions google_protobuf_MethodOptions; +typedef struct google_protobuf_UninterpretedOption google_protobuf_UninterpretedOption; +typedef struct google_protobuf_UninterpretedOption_NamePart google_protobuf_UninterpretedOption_NamePart; +typedef struct google_protobuf_SourceCodeInfo google_protobuf_SourceCodeInfo; +typedef struct google_protobuf_SourceCodeInfo_Location google_protobuf_SourceCodeInfo_Location; +typedef struct google_protobuf_GeneratedCodeInfo google_protobuf_GeneratedCodeInfo; +typedef struct google_protobuf_GeneratedCodeInfo_Annotation google_protobuf_GeneratedCodeInfo_Annotation; +extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit; +extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_DescriptorProto_msginit; +extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit; +extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit; +extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit; +extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit; +extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_FileOptions_msginit; +extern const upb_msglayout google_protobuf_MessageOptions_msginit; +extern const upb_msglayout google_protobuf_FieldOptions_msginit; +extern const upb_msglayout google_protobuf_OneofOptions_msginit; +extern const upb_msglayout google_protobuf_EnumOptions_msginit; +extern const upb_msglayout google_protobuf_EnumValueOptions_msginit; +extern const upb_msglayout google_protobuf_ServiceOptions_msginit; +extern const upb_msglayout google_protobuf_MethodOptions_msginit; +extern const upb_msglayout google_protobuf_UninterpretedOption_msginit; +extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit; +extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit; +extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit; +extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit; +extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit; -/* upb::reffed_ptr ************************************************************/ - -#ifdef __cplusplus - -#include /* For std::swap(). */ - -/* Provides RAII semantics for upb refcounted objects. Each reffed_ptr owns a - * ref on whatever object it points to (if any). */ -template class upb::reffed_ptr { - public: - reffed_ptr() : ptr_(NULL) {} - - /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */ - template - reffed_ptr(U* val, const void* ref_donor = NULL) - : ptr_(upb::upcast(val)) { - if (ref_donor) { - UPB_ASSERT(ptr_); - ptr_->DonateRef(ref_donor, this); - } else if (ptr_) { - ptr_->Ref(this); - } - } - - template - reffed_ptr(const reffed_ptr& other) - : ptr_(upb::upcast(other.get())) { - if (ptr_) ptr_->Ref(this); - } - - reffed_ptr(const reffed_ptr& other) - : ptr_(upb::upcast(other.get())) { - if (ptr_) ptr_->Ref(this); - } - - ~reffed_ptr() { if (ptr_) ptr_->Unref(this); } - - template - reffed_ptr& operator=(const reffed_ptr& other) { - reset(other.get()); - return *this; - } - - reffed_ptr& operator=(const reffed_ptr& other) { - reset(other.get()); - return *this; - } - - /* TODO(haberman): add C++11 move construction/assignment for greater - * efficiency. */ - - void swap(reffed_ptr& other) { - if (ptr_ == other.ptr_) { - return; - } - - if (ptr_) ptr_->DonateRef(this, &other); - if (other.ptr_) other.ptr_->DonateRef(&other, this); - std::swap(ptr_, other.ptr_); - } - - T& operator*() const { - UPB_ASSERT(ptr_); - return *ptr_; - } - - T* operator->() const { - UPB_ASSERT(ptr_); - return ptr_; - } - - T* get() const { return ptr_; } - - /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */ - template - void reset(U* ptr = NULL, const void* ref_donor = NULL) { - reffed_ptr(ptr, ref_donor).swap(*this); - } - - template - reffed_ptr down_cast() { - return reffed_ptr(upb::down_cast(get())); - } - - template - reffed_ptr dyn_cast() { - return reffed_ptr(upb::dyn_cast(get())); - } - - /* Plain release() is unsafe; if we were the only owner, it would leak the - * object. Instead we provide this: */ - T* ReleaseTo(const void* new_owner) { - T* ret = NULL; - ptr_->DonateRef(this, new_owner); - std::swap(ret, ptr_); - return ret; - } - - private: - T* ptr_; -}; - -#endif /* __cplusplus */ - -#endif /* UPB_REFCOUNT_H_ */ - -#ifdef __cplusplus -#include -#include -#include - -namespace upb { -class Def; -class EnumDef; -class FieldDef; -class FileDef; -class MessageDef; -class OneofDef; -class SymbolTable; -} -#endif - -UPB_DECLARE_DERIVED_TYPE(upb::Def, upb::RefCounted, upb_def, upb_refcounted) -UPB_DECLARE_DERIVED_TYPE(upb::OneofDef, upb::RefCounted, upb_oneofdef, - upb_refcounted) -UPB_DECLARE_DERIVED_TYPE(upb::FileDef, upb::RefCounted, upb_filedef, - upb_refcounted) -UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab) - - -/* The maximum message depth that the type graph can have. This is a resource - * limit for the C stack since we sometimes need to recursively traverse the - * graph. Cycles are ok; the traversal will stop when it detects a cycle, but - * we must hit the cycle before the maximum depth is reached. - * - * If having a single static limit is too inflexible, we can add another variant - * of Def::Freeze that allows specifying this as a parameter. */ -#define UPB_MAX_MESSAGE_DEPTH 64 - - -/* upb::Def: base class for top-level defs ***********************************/ - -/* All the different kind of defs that can be defined at the top-level and put - * in a SymbolTable or appear in a FileDef::defs() list. This excludes some - * defs (like oneofs and files). It only includes fields because they can be - * defined as extensions. */ -typedef enum { - UPB_DEF_MSG, - UPB_DEF_FIELD, - UPB_DEF_ENUM, - UPB_DEF_SERVICE, /* Not yet implemented. */ - UPB_DEF_ANY = -1 /* Wildcard for upb_symtab_get*() */ -} upb_deftype_t; - -#ifdef __cplusplus - -/* The base class of all defs. Its base is upb::RefCounted (use upb::upcast() - * to convert). */ -class upb::Def { - public: - typedef upb_deftype_t Type; - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - Type def_type() const; - - /* "fullname" is the def's fully-qualified name (eg. foo.bar.Message). */ - const char *full_name() const; - - /* The final part of a def's name (eg. Message). */ - const char *name() const; - - /* The def must be mutable. Caller retains ownership of fullname. Defs are - * not required to have a name; if a def has no name when it is frozen, it - * will remain an anonymous def. On failure, returns false and details in "s" - * if non-NULL. */ - bool set_full_name(const char* fullname, upb::Status* s); - bool set_full_name(const std::string &fullname, upb::Status* s); - - /* The file in which this def appears. It is not necessary to add a def to a - * file (and consequently the accessor may return NULL). Set this by calling - * file->Add(def). */ - FileDef* file() const; - - /* Freezes the given defs; this validates all constraints and marks the defs - * as frozen (read-only). "defs" may not contain any fielddefs, but fields - * of any msgdefs will be frozen. - * - * Symbolic references to sub-types and enum defaults must have already been - * resolved. Any mutable defs reachable from any of "defs" must also be in - * the list; more formally, "defs" must be a transitive closure of mutable - * defs. - * - * After this operation succeeds, the finalized defs must only be accessed - * through a const pointer! */ - static bool Freeze(Def* const* defs, size_t n, Status* status); - static bool Freeze(const std::vector& defs, Status* status); - - private: - UPB_DISALLOW_POD_OPS(Def, upb::Def) -}; - -#endif /* __cplusplus */ - -UPB_BEGIN_EXTERN_C - -/* Include upb_refcounted methods like upb_def_ref()/upb_def_unref(). */ -UPB_REFCOUNTED_CMETHODS(upb_def, upb_def_upcast) - -upb_deftype_t upb_def_type(const upb_def *d); -const char *upb_def_fullname(const upb_def *d); -const char *upb_def_name(const upb_def *d); -const upb_filedef *upb_def_file(const upb_def *d); -bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s); -bool upb_def_freeze(upb_def *const *defs, size_t n, upb_status *s); - -/* Temporary API: for internal use only. */ -bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s); - -UPB_END_EXTERN_C - - -/* upb::Def casts *************************************************************/ - -#ifdef __cplusplus -#define UPB_CPP_CASTS(cname, cpptype) \ - namespace upb { \ - template <> \ - inline cpptype *down_cast(Def * def) { \ - return upb_downcast_##cname##_mutable(def); \ - } \ - template <> \ - inline cpptype *dyn_cast(Def * def) { \ - return upb_dyncast_##cname##_mutable(def); \ - } \ - template <> \ - inline const cpptype *down_cast( \ - const Def *def) { \ - return upb_downcast_##cname(def); \ - } \ - template <> \ - inline const cpptype *dyn_cast(const Def *def) { \ - return upb_dyncast_##cname(def); \ - } \ - template <> \ - inline const cpptype *down_cast(Def * def) { \ - return upb_downcast_##cname(def); \ - } \ - template <> \ - inline const cpptype *dyn_cast(Def * def) { \ - return upb_dyncast_##cname(def); \ - } \ - } /* namespace upb */ -#else -#define UPB_CPP_CASTS(cname, cpptype) -#endif /* __cplusplus */ - -/* Dynamic casts, for determining if a def is of a particular type at runtime. - * Downcasts, for when some wants to assert that a def is of a particular type. - * These are only checked if we are building debug. */ -#define UPB_DEF_CASTS(lower, upper, cpptype) \ - UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) { \ - if (upb_def_type(def) != UPB_DEF_##upper) return NULL; \ - return (upb_##lower *)def; \ - } \ - UPB_INLINE const upb_##lower *upb_downcast_##lower(const upb_def *def) { \ - UPB_ASSERT(upb_def_type(def) == UPB_DEF_##upper); \ - return (const upb_##lower *)def; \ - } \ - UPB_INLINE upb_##lower *upb_dyncast_##lower##_mutable(upb_def *def) { \ - return (upb_##lower *)upb_dyncast_##lower(def); \ - } \ - UPB_INLINE upb_##lower *upb_downcast_##lower##_mutable(upb_def *def) { \ - return (upb_##lower *)upb_downcast_##lower(def); \ - } \ - UPB_CPP_CASTS(lower, cpptype) - -#define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members) \ - UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, cppmethods, \ - members) \ - UPB_DEF_CASTS(lower, upper, cppname) - -#define UPB_DECLARE_DEF_TYPE(cppname, lower, upper) \ - UPB_DECLARE_DERIVED_TYPE2(cppname, upb::Def, upb::RefCounted, \ - upb_ ## lower, upb_def, upb_refcounted) \ - UPB_DEF_CASTS(lower, upper, cppname) - -UPB_DECLARE_DEF_TYPE(upb::FieldDef, fielddef, FIELD) -UPB_DECLARE_DEF_TYPE(upb::MessageDef, msgdef, MSG) -UPB_DECLARE_DEF_TYPE(upb::EnumDef, enumdef, ENUM) - -#undef UPB_DECLARE_DEF_TYPE -#undef UPB_DEF_CASTS -#undef UPB_CPP_CASTS - - -/* upb::FieldDef **************************************************************/ - -/* 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). */ -typedef enum { - /* Types stored in 1 byte. */ - UPB_TYPE_BOOL = 1, - /* Types stored in 4 bytes. */ - UPB_TYPE_FLOAT = 2, - UPB_TYPE_INT32 = 3, - UPB_TYPE_UINT32 = 4, - UPB_TYPE_ENUM = 5, /* Enum values are int32. */ - /* Types stored as pointers (probably 4 or 8 bytes). */ - UPB_TYPE_STRING = 6, - UPB_TYPE_BYTES = 7, - UPB_TYPE_MESSAGE = 8, - /* Types stored as 8 bytes. */ - UPB_TYPE_DOUBLE = 9, - UPB_TYPE_INT64 = 10, - UPB_TYPE_UINT64 = 11 -} upb_fieldtype_t; - -/* The repeated-ness of each field; this matches descriptor.proto. */ -typedef enum { - UPB_LABEL_OPTIONAL = 1, - UPB_LABEL_REQUIRED = 2, - UPB_LABEL_REPEATED = 3 -} upb_label_t; - -/* How integers should be encoded in serializations that offer multiple - * integer encoding methods. */ -typedef enum { - UPB_INTFMT_VARIABLE = 1, - UPB_INTFMT_FIXED = 2, - UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */ -} upb_intfmt_t; - -/* Descriptor types, as defined in descriptor.proto. */ -typedef enum { - UPB_DESCRIPTOR_TYPE_DOUBLE = 1, - UPB_DESCRIPTOR_TYPE_FLOAT = 2, - UPB_DESCRIPTOR_TYPE_INT64 = 3, - UPB_DESCRIPTOR_TYPE_UINT64 = 4, - UPB_DESCRIPTOR_TYPE_INT32 = 5, - UPB_DESCRIPTOR_TYPE_FIXED64 = 6, - UPB_DESCRIPTOR_TYPE_FIXED32 = 7, - UPB_DESCRIPTOR_TYPE_BOOL = 8, - UPB_DESCRIPTOR_TYPE_STRING = 9, - UPB_DESCRIPTOR_TYPE_GROUP = 10, - UPB_DESCRIPTOR_TYPE_MESSAGE = 11, - UPB_DESCRIPTOR_TYPE_BYTES = 12, - UPB_DESCRIPTOR_TYPE_UINT32 = 13, - UPB_DESCRIPTOR_TYPE_ENUM = 14, - UPB_DESCRIPTOR_TYPE_SFIXED32 = 15, - UPB_DESCRIPTOR_TYPE_SFIXED64 = 16, - UPB_DESCRIPTOR_TYPE_SINT32 = 17, - UPB_DESCRIPTOR_TYPE_SINT64 = 18 -} upb_descriptortype_t; - -typedef enum { - UPB_SYNTAX_PROTO2 = 2, - UPB_SYNTAX_PROTO3 = 3 -} upb_syntax_t; - -/* Maps descriptor type -> upb field type. */ -extern const uint8_t upb_desctype_to_fieldtype[]; - -/* Maximum field number allowed for FieldDefs. This is an inherent limit of the - * protobuf wire format. */ -#define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) - -#ifdef __cplusplus - -/* A upb_fielddef describes a single field in a message. It is most often - * found as a part of a upb_msgdef, but can also stand alone to represent - * an extension. - * - * Its base class is upb::Def (use upb::upcast() to convert). */ -class upb::FieldDef { - public: - typedef upb_fieldtype_t Type; - typedef upb_label_t Label; - typedef upb_intfmt_t IntegerFormat; - typedef upb_descriptortype_t DescriptorType; - - /* These return true if the given value is a valid member of the enumeration. */ - static bool CheckType(int32_t val); - static bool CheckLabel(int32_t val); - static bool CheckDescriptorType(int32_t val); - static bool CheckIntegerFormat(int32_t val); - - /* These convert to the given enumeration; they require that the value is - * valid. */ - static Type ConvertType(int32_t val); - static Label ConvertLabel(int32_t val); - static DescriptorType ConvertDescriptorType(int32_t val); - static IntegerFormat ConvertIntegerFormat(int32_t val); - - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - /* Functionality from upb::Def. */ - const char* full_name() const; - - bool type_is_set() const; /* set_[descriptor_]type() has been called? */ - Type type() const; /* Requires that type_is_set() == true. */ - Label label() const; /* Defaults to UPB_LABEL_OPTIONAL. */ - const char* name() const; /* NULL if uninitialized. */ - uint32_t number() const; /* Returns 0 if uninitialized. */ - bool is_extension() const; - - /* Copies the JSON name for this field into the given buffer. Returns the - * actual size of the JSON name, including the NULL terminator. If the - * return value is 0, the JSON name is unset. If the return value is - * greater than len, the JSON name was truncated. The buffer is always - * NULL-terminated if len > 0. - * - * The JSON name always defaults to a camelCased version of the regular - * name. However if the regular name is unset, the JSON name will be unset - * also. - */ - size_t GetJsonName(char* buf, size_t len) const; - - /* Convenience version of the above function which copies the JSON name - * into the given string, returning false if the name is not set. */ - template - bool GetJsonName(T* str) { - str->resize(GetJsonName(NULL, 0)); - GetJsonName(&(*str)[0], str->size()); - return str->size() > 0; - } - - /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false, - * indicates whether this field should have lazy parsing handlers that yield - * the unparsed string for the submessage. - * - * TODO(haberman): I think we want to move this into a FieldOptions container - * when we add support for custom options (the FieldOptions struct will - * contain both regular FieldOptions like "lazy" *and* custom options). */ - bool lazy() const; - - /* For non-string, non-submessage fields, this indicates whether binary - * protobufs are encoded in packed or non-packed format. - * - * TODO(haberman): see note above about putting options like this into a - * FieldOptions container. */ - bool packed() const; - - /* An integer that can be used as an index into an array of fields for - * whatever message this field belongs to. Guaranteed to be less than - * f->containing_type()->field_count(). May only be accessed once the def has - * been finalized. */ - uint32_t index() const; - - /* The MessageDef to which this field belongs. - * - * If this field has been added to a MessageDef, that message can be retrieved - * directly (this is always the case for frozen FieldDefs). - * - * If the field has not yet been added to a MessageDef, you can set the name - * of the containing type symbolically instead. This is mostly useful for - * extensions, where the extension is declared separately from the message. */ - const MessageDef* containing_type() const; - const char* containing_type_name(); - - /* The OneofDef to which this field belongs, or NULL if this field is not part - * of a oneof. */ - const OneofDef* containing_oneof() const; - - /* The field's type according to the enum in descriptor.proto. This is not - * the same as UPB_TYPE_*, because it distinguishes between (for example) - * INT32 and SINT32, whereas our "type" enum does not. This return of - * descriptor_type() is a function of type(), integer_format(), and - * is_tag_delimited(). Likewise set_descriptor_type() sets all three - * appropriately. */ - DescriptorType descriptor_type() const; - - /* Convenient field type tests. */ - bool IsSubMessage() const; - bool IsString() const; - bool IsSequence() const; - bool IsPrimitive() const; - bool IsMap() const; - - /* Returns whether this field explicitly represents presence. - * - * For proto2 messages: Returns true for any scalar (non-repeated) field. - * For proto3 messages: Returns true for scalar submessage or oneof fields. */ - bool HasPresence() const; - - /* How integers are encoded. Only meaningful for integer types. - * Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes. */ - IntegerFormat integer_format() const; - - /* Whether a submessage field is tag-delimited or not (if false, then - * length-delimited). May only be set when type() == UPB_TYPE_MESSAGE. */ - bool is_tag_delimited() const; - - /* Returns the non-string default value for this fielddef, which may either - * be something the client set explicitly or the "default default" (0 for - * numbers, empty for strings). The field's type indicates the type of the - * returned value, except for enum fields that are still mutable. - * - * Requires that the given function matches the field's current type. */ - int64_t default_int64() const; - int32_t default_int32() const; - uint64_t default_uint64() const; - uint32_t default_uint32() const; - bool default_bool() const; - float default_float() const; - double default_double() const; - - /* The resulting string is always NULL-terminated. If non-NULL, the length - * will be stored in *len. */ - const char *default_string(size_t* len) const; - - /* For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either - * string or int32, and both of these methods will always return true. - * - * For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated. - * Enum defaults are unusual. They can be specified either as string or int32, - * but to be valid the enum must have that value as a member. And if no - * default is specified, the "default default" comes from the EnumDef. - * - * We allow reading the default as either an int32 or a string, but only if - * we have a meaningful value to report. We have a meaningful value if it was - * set explicitly, or if we could get the "default default" from the EnumDef. - * Also if you explicitly set the name and we find the number in the EnumDef */ - bool EnumHasStringDefault() const; - bool EnumHasInt32Default() const; - - /* Submessage and enum fields must reference a "subdef", which is the - * upb::MessageDef or upb::EnumDef that defines their type. Note that when - * the FieldDef is mutable it may not have a subdef *yet*, but this function - * still returns true to indicate that the field's type requires a subdef. */ - bool HasSubDef() const; - - /* Returns the enum or submessage def for this field, if any. The field's - * type must match (ie. you may only call enum_subdef() for fields where - * type() == UPB_TYPE_ENUM). Returns NULL if the subdef has not been set or - * is currently set symbolically. */ - const EnumDef* enum_subdef() const; - const MessageDef* message_subdef() const; - - /* Returns the generic subdef for this field. Requires that HasSubDef() (ie. - * only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields). */ - const Def* subdef() const; - - /* Returns the symbolic name of the subdef. If the subdef is currently set - * unresolved (ie. set symbolically) returns the symbolic name. If it has - * been resolved to a specific subdef, returns the name from that subdef. */ - const char* subdef_name() const; - - /* Setters (non-const methods), only valid for mutable FieldDefs! ***********/ - - bool set_full_name(const char* fullname, upb::Status* s); - bool set_full_name(const std::string& fullname, upb::Status* s); - - /* This may only be called if containing_type() == NULL (ie. the field has not - * been added to a message yet). */ - bool set_containing_type_name(const char *name, Status* status); - bool set_containing_type_name(const std::string& name, Status* status); - - /* Defaults to false. When we freeze, we ensure that this can only be true - * for length-delimited message fields. Prior to freezing this can be true or - * false with no restrictions. */ - void set_lazy(bool lazy); - - /* Defaults to true. Sets whether this field is encoded in packed format. */ - void set_packed(bool packed); - - /* "type" or "descriptor_type" MUST be set explicitly before the fielddef is - * finalized. These setters require that the enum value is valid; if the - * value did not come directly from an enum constant, the caller should - * validate it first with the functions above (CheckFieldType(), etc). */ - void set_type(Type type); - void set_label(Label label); - void set_descriptor_type(DescriptorType type); - void set_is_extension(bool is_extension); - - /* "number" and "name" must be set before the FieldDef is added to a - * MessageDef, and may not be set after that. - * - * "name" is the same as full_name()/set_full_name(), but since fielddefs - * most often use simple, non-qualified names, we provide this accessor - * also. Generally only extensions will want to think of this name as - * fully-qualified. */ - bool set_number(uint32_t number, upb::Status* s); - bool set_name(const char* name, upb::Status* s); - bool set_name(const std::string& name, upb::Status* s); - - /* Sets the JSON name to the given string. */ - /* TODO(haberman): implement. Right now only default json_name (camelCase) - * is supported. */ - bool set_json_name(const char* json_name, upb::Status* s); - bool set_json_name(const std::string& name, upb::Status* s); - - /* Clears the JSON name. This will make it revert to its default, which is - * a camelCased version of the regular field name. */ - void clear_json_name(); - - void set_integer_format(IntegerFormat format); - bool set_tag_delimited(bool tag_delimited, upb::Status* s); - - /* Sets default value for the field. The call must exactly match the type - * of the field. Enum fields may use either setint32 or setstring to set - * the default numerically or symbolically, respectively, but symbolic - * defaults must be resolved before finalizing (see ResolveEnumDefault()). - * - * Changing the type of a field will reset its default. */ - void set_default_int64(int64_t val); - void set_default_int32(int32_t val); - void set_default_uint64(uint64_t val); - void set_default_uint32(uint32_t val); - void set_default_bool(bool val); - void set_default_float(float val); - void set_default_double(double val); - bool set_default_string(const void *str, size_t len, Status *s); - bool set_default_string(const std::string &str, Status *s); - void set_default_cstr(const char *str, Status *s); - - /* Before a fielddef is frozen, its subdef may be set either directly (with a - * upb::Def*) or symbolically. Symbolic refs must be resolved before the - * containing msgdef can be frozen (see upb_resolve() above). upb always - * guarantees that any def reachable from a live def will also be kept alive. - * - * Both methods require that upb_hassubdef(f) (so the type must be set prior - * to calling these methods). Returns false if this is not the case, or if - * the given subdef is not of the correct type. The subdef is reset if the - * field's type is changed. The subdef can be set to NULL to clear it. */ - bool set_subdef(const Def* subdef, Status* s); - bool set_enum_subdef(const EnumDef* subdef, Status* s); - bool set_message_subdef(const MessageDef* subdef, Status* s); - bool set_subdef_name(const char* name, Status* s); - bool set_subdef_name(const std::string &name, Status* s); - - private: - UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef) -}; - -# endif /* defined(__cplusplus) */ - -UPB_BEGIN_EXTERN_C - -/* Native C API. */ -upb_fielddef *upb_fielddef_new(const void *owner); - -/* Include upb_refcounted methods like upb_fielddef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_fielddef, upb_fielddef_upcast2) - -/* Methods from upb_def. */ -const char *upb_fielddef_fullname(const upb_fielddef *f); -bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname, - upb_status *s); - -bool upb_fielddef_typeisset(const upb_fielddef *f); -upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f); -upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f); -upb_label_t upb_fielddef_label(const upb_fielddef *f); -uint32_t upb_fielddef_number(const upb_fielddef *f); -const char *upb_fielddef_name(const upb_fielddef *f); -bool upb_fielddef_isextension(const upb_fielddef *f); -bool upb_fielddef_lazy(const upb_fielddef *f); -bool upb_fielddef_packed(const upb_fielddef *f); -size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len); -const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f); -const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f); -upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f); -const char *upb_fielddef_containingtypename(upb_fielddef *f); -upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f); -uint32_t upb_fielddef_index(const upb_fielddef *f); -bool upb_fielddef_istagdelim(const upb_fielddef *f); -bool upb_fielddef_issubmsg(const upb_fielddef *f); -bool upb_fielddef_isstring(const upb_fielddef *f); -bool upb_fielddef_isseq(const upb_fielddef *f); -bool upb_fielddef_isprimitive(const upb_fielddef *f); -bool upb_fielddef_ismap(const upb_fielddef *f); -bool upb_fielddef_haspresence(const upb_fielddef *f); -int64_t upb_fielddef_defaultint64(const upb_fielddef *f); -int32_t upb_fielddef_defaultint32(const upb_fielddef *f); -uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f); -uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f); -bool upb_fielddef_defaultbool(const upb_fielddef *f); -float upb_fielddef_defaultfloat(const upb_fielddef *f); -double upb_fielddef_defaultdouble(const upb_fielddef *f); -const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len); -bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f); -bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f); -bool upb_fielddef_hassubdef(const upb_fielddef *f); -const upb_def *upb_fielddef_subdef(const upb_fielddef *f); -const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f); -const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); -const char *upb_fielddef_subdefname(const upb_fielddef *f); - -void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type); -void upb_fielddef_setdescriptortype(upb_fielddef *f, int type); -void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label); -bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s); -bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s); -bool upb_fielddef_setjsonname(upb_fielddef *f, const char *name, upb_status *s); -bool upb_fielddef_clearjsonname(upb_fielddef *f); -bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name, - upb_status *s); -void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension); -void upb_fielddef_setlazy(upb_fielddef *f, bool lazy); -void upb_fielddef_setpacked(upb_fielddef *f, bool packed); -void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt); -void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim); -void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t val); -void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t val); -void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t val); -void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t val); -void upb_fielddef_setdefaultbool(upb_fielddef *f, bool val); -void upb_fielddef_setdefaultfloat(upb_fielddef *f, float val); -void upb_fielddef_setdefaultdouble(upb_fielddef *f, double val); -bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len, - upb_status *s); -void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str, - upb_status *s); -bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef, - upb_status *s); -bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef, - upb_status *s); -bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef, - upb_status *s); -bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name, - upb_status *s); - -bool upb_fielddef_checklabel(int32_t label); -bool upb_fielddef_checktype(int32_t type); -bool upb_fielddef_checkdescriptortype(int32_t type); -bool upb_fielddef_checkintfmt(int32_t fmt); - -UPB_END_EXTERN_C - - -/* upb::MessageDef ************************************************************/ - -typedef upb_inttable_iter upb_msg_field_iter; -typedef upb_strtable_iter upb_msg_oneof_iter; - -/* Well-known field tag numbers for map-entry messages. */ -#define UPB_MAPENTRY_KEY 1 -#define UPB_MAPENTRY_VALUE 2 - -#ifdef __cplusplus - -/* Structure that describes a single .proto message type. - * - * Its base class is upb::Def (use upb::upcast() to convert). */ -class upb::MessageDef { - public: - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - /* Functionality from upb::Def. */ - const char* full_name() const; - const char* name() const; - bool set_full_name(const char* fullname, Status* s); - bool set_full_name(const std::string& fullname, Status* s); - - /* Call to freeze this MessageDef. - * WARNING: this will fail if this message has any unfrozen submessages! - * Messages with cycles must be frozen as a batch using upb::Def::Freeze(). */ - bool Freeze(Status* s); - - /* The number of fields that belong to the MessageDef. */ - int field_count() const; - - /* The number of oneofs that belong to the MessageDef. */ - int oneof_count() const; - - /* Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef - * and the fielddefs are mutable. The fielddef's name and number must be - * set, and the message may not already contain any field with this name or - * number, and this fielddef may not be part of another message. In error - * cases false is returned and the msgdef is unchanged. - * - * If the given field is part of a oneof, this call succeeds if and only if - * that oneof is already part of this msgdef. (Note that adding a oneof to a - * msgdef automatically adds all of its fields to the msgdef at the time that - * the oneof is added, so it is usually more idiomatic to add the oneof's - * fields first then add the oneof to the msgdef. This case is supported for - * convenience.) - * - * If |f| is already part of this MessageDef, this method performs no action - * and returns true (success). Thus, this method is idempotent. */ - bool AddField(FieldDef* f, Status* s); - bool AddField(const reffed_ptr& f, Status* s); - - /* Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef, - * oneof, and any fielddefs are mutable, that the fielddefs contained in the - * oneof do not have any name or number conflicts with existing fields in the - * msgdef, and that the oneof's name is unique among all oneofs in the msgdef. - * If the oneof is added successfully, all of its fields will be added - * directly to the msgdef as well. In error cases, false is returned and the - * msgdef is unchanged. */ - bool AddOneof(OneofDef* o, Status* s); - bool AddOneof(const reffed_ptr& o, Status* s); - - upb_syntax_t syntax() const; - - /* Returns false if we don't support this syntax value. */ - bool set_syntax(upb_syntax_t syntax); - - /* Set this to false to indicate that primitive fields should not have - * explicit presence information associated with them. This will affect all - * fields added to this message. Defaults to true. */ - void SetPrimitivesHavePresence(bool have_presence); - - /* These return NULL if the field is not found. */ - FieldDef* FindFieldByNumber(uint32_t number); - FieldDef* FindFieldByName(const char *name, size_t len); - const FieldDef* FindFieldByNumber(uint32_t number) const; - const FieldDef* FindFieldByName(const char* name, size_t len) const; - - - FieldDef* FindFieldByName(const char *name) { - return FindFieldByName(name, strlen(name)); - } - const FieldDef* FindFieldByName(const char *name) const { - return FindFieldByName(name, strlen(name)); - } - - template - FieldDef* FindFieldByName(const T& str) { - return FindFieldByName(str.c_str(), str.size()); - } - template - const FieldDef* FindFieldByName(const T& str) const { - return FindFieldByName(str.c_str(), str.size()); - } - - OneofDef* FindOneofByName(const char* name, size_t len); - const OneofDef* FindOneofByName(const char* name, size_t len) const; - - OneofDef* FindOneofByName(const char* name) { - return FindOneofByName(name, strlen(name)); - } - const OneofDef* FindOneofByName(const char* name) const { - return FindOneofByName(name, strlen(name)); - } - - template - OneofDef* FindOneofByName(const T& str) { - return FindOneofByName(str.c_str(), str.size()); - } - template - const OneofDef* FindOneofByName(const T& str) const { - return FindOneofByName(str.c_str(), str.size()); - } - - /* Is this message a map entry? */ - void setmapentry(bool map_entry); - bool mapentry() const; - - /* Iteration over fields. The order is undefined. */ - class field_iterator - : public std::iterator { - public: - explicit field_iterator(MessageDef* md); - static field_iterator end(MessageDef* md); - - void operator++(); - FieldDef* operator*() const; - bool operator!=(const field_iterator& other) const; - bool operator==(const field_iterator& other) const; - - private: - upb_msg_field_iter iter_; - }; - - class const_field_iterator - : public std::iterator { - public: - explicit const_field_iterator(const MessageDef* md); - static const_field_iterator end(const MessageDef* md); - - void operator++(); - const FieldDef* operator*() const; - bool operator!=(const const_field_iterator& other) const; - bool operator==(const const_field_iterator& other) const; - - private: - upb_msg_field_iter iter_; - }; - - /* Iteration over oneofs. The order is undefined. */ - class oneof_iterator - : public std::iterator { - public: - explicit oneof_iterator(MessageDef* md); - static oneof_iterator end(MessageDef* md); - - void operator++(); - OneofDef* operator*() const; - bool operator!=(const oneof_iterator& other) const; - bool operator==(const oneof_iterator& other) const; - - private: - upb_msg_oneof_iter iter_; - }; - - class const_oneof_iterator - : public std::iterator { - public: - explicit const_oneof_iterator(const MessageDef* md); - static const_oneof_iterator end(const MessageDef* md); - - void operator++(); - const OneofDef* operator*() const; - bool operator!=(const const_oneof_iterator& other) const; - bool operator==(const const_oneof_iterator& other) const; - - private: - upb_msg_oneof_iter iter_; - }; - - class FieldAccessor { - public: - explicit FieldAccessor(MessageDef* msg) : msg_(msg) {} - field_iterator begin() { return msg_->field_begin(); } - field_iterator end() { return msg_->field_end(); } - private: - MessageDef* msg_; - }; - - class ConstFieldAccessor { - public: - explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {} - const_field_iterator begin() { return msg_->field_begin(); } - const_field_iterator end() { return msg_->field_end(); } - private: - const MessageDef* msg_; - }; - - class OneofAccessor { - public: - explicit OneofAccessor(MessageDef* msg) : msg_(msg) {} - oneof_iterator begin() { return msg_->oneof_begin(); } - oneof_iterator end() { return msg_->oneof_end(); } - private: - MessageDef* msg_; - }; - - class ConstOneofAccessor { - public: - explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {} - const_oneof_iterator begin() { return msg_->oneof_begin(); } - const_oneof_iterator end() { return msg_->oneof_end(); } - private: - const MessageDef* msg_; - }; - - field_iterator field_begin(); - field_iterator field_end(); - const_field_iterator field_begin() const; - const_field_iterator field_end() const; - - oneof_iterator oneof_begin(); - oneof_iterator oneof_end(); - const_oneof_iterator oneof_begin() const; - const_oneof_iterator oneof_end() const; - - FieldAccessor fields() { return FieldAccessor(this); } - ConstFieldAccessor fields() const { return ConstFieldAccessor(this); } - OneofAccessor oneofs() { return OneofAccessor(this); } - ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); } - - private: - UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef) -}; - -#endif /* __cplusplus */ - -UPB_BEGIN_EXTERN_C - -/* Returns NULL if memory allocation failed. */ -upb_msgdef *upb_msgdef_new(const void *owner); - -/* Include upb_refcounted methods like upb_msgdef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_msgdef, upb_msgdef_upcast2) - -bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status); - -const char *upb_msgdef_fullname(const upb_msgdef *m); -const char *upb_msgdef_name(const upb_msgdef *m); -int upb_msgdef_numoneofs(const upb_msgdef *m); -upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m); - -bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor, - upb_status *s); -bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor, - upb_status *s); -bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s); -void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry); -bool upb_msgdef_mapentry(const upb_msgdef *m); -bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax); - -/* Field lookup in a couple of different variations: - * - itof = int to field - * - ntof = name to field - * - ntofz = name to field, null-terminated string. */ -const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i); -const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, - size_t len); -int upb_msgdef_numfields(const upb_msgdef *m); - -UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m, - const char *name) { - return upb_msgdef_ntof(m, name, strlen(name)); -} - -UPB_INLINE upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) { - return (upb_fielddef*)upb_msgdef_itof(m, i); -} - -UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m, - const char *name, size_t len) { - return (upb_fielddef *)upb_msgdef_ntof(m, name, len); -} - -/* Oneof lookup: - * - ntoo = name to oneof - * - ntooz = name to oneof, null-terminated string. */ -const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, - size_t len); -int upb_msgdef_numoneofs(const upb_msgdef *m); - -UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m, - const char *name) { - return upb_msgdef_ntoo(m, name, strlen(name)); -} - -UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m, - const char *name, size_t len) { - return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len); -} - -/* 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. */ -bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len, - const upb_fielddef **f, const upb_oneofdef **o); - -UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name, - const upb_fielddef **f, - const upb_oneofdef **o) { - return upb_msgdef_lookupname(m, name, strlen(name), f, o); -} - -/* Iteration over fields and oneofs. For example: - * - * upb_msg_field_iter i; - * for(upb_msg_field_begin(&i, m); - * !upb_msg_field_done(&i); - * upb_msg_field_next(&i)) { - * upb_fielddef *f = upb_msg_iter_field(&i); - * // ... - * } - * - * For C we don't have separate iterators for const and non-const. - * It is the caller's responsibility to cast the upb_fielddef* to - * const if the upb_msgdef* is const. */ -void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m); -void upb_msg_field_next(upb_msg_field_iter *iter); -bool upb_msg_field_done(const upb_msg_field_iter *iter); -upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter); -void upb_msg_field_iter_setdone(upb_msg_field_iter *iter); - -/* Similar to above, we also support iterating through the oneofs in a - * msgdef. */ -void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m); -void upb_msg_oneof_next(upb_msg_oneof_iter *iter); -bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter); -upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter); -void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter); - -UPB_END_EXTERN_C - - -/* upb::EnumDef ***************************************************************/ - -typedef upb_strtable_iter upb_enum_iter; - -#ifdef __cplusplus - -/* Class that represents an enum. Its base class is upb::Def (convert with - * upb::upcast()). */ -class upb::EnumDef { - public: - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - /* Functionality from upb::Def. */ - const char* full_name() const; - const char* name() const; - bool set_full_name(const char* fullname, Status* s); - bool set_full_name(const std::string& fullname, Status* s); - - /* Call to freeze this EnumDef. */ - bool Freeze(Status* s); - - /* The value that is used as the default when no field default is specified. - * If not set explicitly, the first value that was added will be used. - * The default value must be a member of the enum. - * Requires that value_count() > 0. */ - int32_t default_value() const; - - /* Sets the default value. If this value is not valid, returns false and an - * error message in status. */ - bool set_default_value(int32_t val, Status* status); - - /* Returns the number of values currently defined in the enum. Note that - * multiple names can refer to the same number, so this may be greater than - * the total number of unique numbers. */ - int value_count() const; - - /* Adds a single name/number pair to the enum. Fails if this name has - * already been used by another value. */ - bool AddValue(const char* name, int32_t num, Status* status); - bool AddValue(const std::string& name, int32_t num, Status* status); - - /* Lookups from name to integer, returning true if found. */ - bool FindValueByName(const char* name, int32_t* num) const; - - /* Finds the name corresponding to the given number, or NULL if none was - * found. If more than one name corresponds to this number, returns the - * first one that was added. */ - const char* FindValueByNumber(int32_t num) const; - - /* Iteration over name/value pairs. The order is undefined. - * Adding an enum val invalidates any iterators. - * - * TODO: make compatible with range-for, with elements as pairs? */ - class Iterator { - public: - explicit Iterator(const EnumDef*); - - int32_t number(); - const char *name(); - bool Done(); - void Next(); - - private: - upb_enum_iter iter_; - }; - - private: - UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef) -}; - -#endif /* __cplusplus */ - -UPB_BEGIN_EXTERN_C - -/* Native C API. */ -upb_enumdef *upb_enumdef_new(const void *owner); - -/* Include upb_refcounted methods like upb_enumdef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_enumdef, upb_enumdef_upcast2) - -bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status); - -/* From upb_def. */ -const char *upb_enumdef_fullname(const upb_enumdef *e); -const char *upb_enumdef_name(const upb_enumdef *e); -bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname, - upb_status *s); - -int32_t upb_enumdef_default(const upb_enumdef *e); -bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s); -int upb_enumdef_numvals(const upb_enumdef *e); -bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, - upb_status *status); - -/* Enum lookups: - * - ntoi: look up a name with specified length. - * - ntoiz: look up a name provided as a null-terminated string. - * - iton: look up an integer, returning the name as a null-terminated - * string. */ -bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len, - int32_t *num); -UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e, - const char *name, int32_t *num) { - return upb_enumdef_ntoi(e, name, strlen(name), num); -} -const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num); - -/* upb_enum_iter i; - * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) { - * // ... - * } - */ -void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e); -void upb_enum_next(upb_enum_iter *iter); -bool upb_enum_done(upb_enum_iter *iter); -const char *upb_enum_iter_name(upb_enum_iter *iter); -int32_t upb_enum_iter_number(upb_enum_iter *iter); - -UPB_END_EXTERN_C - - -/* upb::OneofDef **************************************************************/ - -typedef upb_inttable_iter upb_oneof_iter; - -#ifdef __cplusplus - -/* Class that represents a oneof. */ -class upb::OneofDef { - public: - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - /* Returns the MessageDef that owns this OneofDef. */ - const MessageDef* containing_type() const; - - /* Returns the name of this oneof. This is the name used to look up the oneof - * by name once added to a message def. */ - const char* name() const; - bool set_name(const char* name, Status* s); - bool set_name(const std::string& name, Status* s); - - /* Returns the number of fields currently defined in the oneof. */ - int field_count() const; - - /* Adds a field to the oneof. The field must not have been added to any other - * oneof or msgdef. If the oneof is not yet part of a msgdef, then when the - * oneof is eventually added to a msgdef, all fields added to the oneof will - * also be added to the msgdef at that time. If the oneof is already part of a - * msgdef, the field must either be a part of that msgdef already, or must not - * be a part of any msgdef; in the latter case, the field is added to the - * msgdef as a part of this operation. - * - * The field may only have an OPTIONAL label, never REQUIRED or REPEATED. - * - * If |f| is already part of this MessageDef, this method performs no action - * and returns true (success). Thus, this method is idempotent. */ - bool AddField(FieldDef* field, Status* s); - bool AddField(const reffed_ptr& field, Status* s); - - /* Looks up by name. */ - const FieldDef* FindFieldByName(const char* name, size_t len) const; - FieldDef* FindFieldByName(const char* name, size_t len); - const FieldDef* FindFieldByName(const char* name) const { - return FindFieldByName(name, strlen(name)); - } - FieldDef* FindFieldByName(const char* name) { - return FindFieldByName(name, strlen(name)); - } - - template - FieldDef* FindFieldByName(const T& str) { - return FindFieldByName(str.c_str(), str.size()); - } - template - const FieldDef* FindFieldByName(const T& str) const { - return FindFieldByName(str.c_str(), str.size()); - } - - /* Looks up by tag number. */ - const FieldDef* FindFieldByNumber(uint32_t num) const; - - /* Iteration over fields. The order is undefined. */ - class iterator : public std::iterator { - public: - explicit iterator(OneofDef* md); - static iterator end(OneofDef* md); - - void operator++(); - FieldDef* operator*() const; - bool operator!=(const iterator& other) const; - bool operator==(const iterator& other) const; - - private: - upb_oneof_iter iter_; - }; - - class const_iterator - : public std::iterator { - public: - explicit const_iterator(const OneofDef* md); - static const_iterator end(const OneofDef* md); - - void operator++(); - const FieldDef* operator*() const; - bool operator!=(const const_iterator& other) const; - bool operator==(const const_iterator& other) const; - - private: - upb_oneof_iter iter_; - }; +/* Enums */ - iterator begin(); - iterator end(); - const_iterator begin() const; - const_iterator end() const; +typedef enum { + google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1, + google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2, + google_protobuf_FieldDescriptorProto_LABEL_REPEATED = 3 +} google_protobuf_FieldDescriptorProto_Label; - private: - UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef) -}; +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; -#endif /* __cplusplus */ +typedef enum { + google_protobuf_FieldOptions_STRING = 0, + google_protobuf_FieldOptions_CORD = 1, + google_protobuf_FieldOptions_STRING_PIECE = 2 +} google_protobuf_FieldOptions_CType; -UPB_BEGIN_EXTERN_C +typedef enum { + google_protobuf_FieldOptions_JS_NORMAL = 0, + google_protobuf_FieldOptions_JS_STRING = 1, + google_protobuf_FieldOptions_JS_NUMBER = 2 +} google_protobuf_FieldOptions_JSType; -/* Native C API. */ -upb_oneofdef *upb_oneofdef_new(const void *owner); +typedef enum { + google_protobuf_FileOptions_SPEED = 1, + google_protobuf_FileOptions_CODE_SIZE = 2, + google_protobuf_FileOptions_LITE_RUNTIME = 3 +} google_protobuf_FileOptions_OptimizeMode; -/* Include upb_refcounted methods like upb_oneofdef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_oneofdef, upb_oneofdef_upcast) +typedef enum { + google_protobuf_MethodOptions_IDEMPOTENCY_UNKNOWN = 0, + google_protobuf_MethodOptions_NO_SIDE_EFFECTS = 1, + google_protobuf_MethodOptions_IDEMPOTENT = 2 +} google_protobuf_MethodOptions_IdempotencyLevel; -const char *upb_oneofdef_name(const upb_oneofdef *o); -const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o); -int upb_oneofdef_numfields(const upb_oneofdef *o); -uint32_t upb_oneofdef_index(const upb_oneofdef *o); -bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s); -bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f, - const void *ref_donor, - upb_status *s); +/* google.protobuf.FileDescriptorSet */ -/* Oneof lookups: - * - ntof: look up a field by name. - * - ntofz: look up a field by name (as a null-terminated string). - * - itof: look up a field by number. */ -const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, - const char *name, size_t length); -UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o, - const char *name) { - return upb_oneofdef_ntof(o, name, strlen(name)); +UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_arena *arena) { + return (google_protobuf_FileDescriptorSet *)upb_msg_new(&google_protobuf_FileDescriptorSet_msginit, 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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_FileDescriptorSet_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, arena, len); } -const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num); - -/* upb_oneof_iter i; - * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) { - * // ... - * } - */ -void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o); -void upb_oneof_next(upb_oneof_iter *iter); -bool upb_oneof_done(upb_oneof_iter *iter); -upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter); -void upb_oneof_iter_setdone(upb_oneof_iter *iter); - -UPB_END_EXTERN_C - - -/* upb::FileDef ***************************************************************/ - -#ifdef __cplusplus - -/* Class that represents a .proto file with some things defined in it. - * - * Many users won't care about FileDefs, but they are necessary if you want to - * read the values of file-level options. */ -class upb::FileDef { - public: - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - /* Get/set name of the file (eg. "foo/bar.proto"). */ - const char* name() const; - bool set_name(const char* name, Status* s); - bool set_name(const std::string& name, Status* s); +UPB_INLINE const google_protobuf_FileDescriptorProto* const* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg, size_t *len) { return (const google_protobuf_FileDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } - /* Package name for definitions inside the file (eg. "foo.bar"). */ - const char* package() const; - bool set_package(const char* package, Status* s); +UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_mutable_file(google_protobuf_FileDescriptorSet *msg, size_t *len) { + return (google_protobuf_FileDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); +} +UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_resize_file(google_protobuf_FileDescriptorSet *msg, size_t len, upb_arena *arena) { + return (google_protobuf_FileDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorSet_add_file(google_protobuf_FileDescriptorSet *msg, upb_arena *arena) { + struct google_protobuf_FileDescriptorProto* sub = (struct google_protobuf_FileDescriptorProto*)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} - /* Sets the php class prefix which is prepended to all php generated classes - * from this .proto. Default is empty. */ - const char* phpprefix() const; - bool set_phpprefix(const char* phpprefix, Status* s); - /* Use this option to change the namespace of php generated classes. Default - * is empty. When this option is empty, the package name will be used for - * determining the namespace. */ - const char* phpnamespace() const; - bool set_phpnamespace(const char* phpnamespace, Status* s); +/* google.protobuf.FileDescriptorProto */ - /* Syntax for the file. Defaults to proto2. */ - upb_syntax_t syntax() const; - void set_syntax(upb_syntax_t syntax); +UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_FileDescriptorProto *)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, 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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_FileDescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, arena, len); +} - /* Get the list of defs from the file. These are returned in the order that - * they were added to the FileDef. */ - int def_count() const; - const Def* def(int index) const; - Def* def(int index); +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_package(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); } +UPB_INLINE upb_strview const* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); } +UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); } +UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); } +UPB_INLINE const google_protobuf_ServiceDescriptorProto* const* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_ServiceDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(48, 96), len); } +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(52, 104), len); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_options(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FileOptions*, UPB_SIZE(28, 56)); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)); } +UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(56, 112), len); } +UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(60, 120), len); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_syntax(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); } - /* Get the list of dependencies from the file. These are returned in the - * order that they were added to the FileDef. */ - int dependency_count() const; - const FileDef* dependency(int index) const; +UPB_INLINE void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value; +} +UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_mutable_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len); +} +UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_resize_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_dependency(google_protobuf_FileDescriptorProto *msg, upb_strview val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(36, 72), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_mutable_message_type(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len); +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_resize_message_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_FileDescriptorProto_add_message_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_mutable_enum_type(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len); +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_resize_enum_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_FileDescriptorProto_add_enum_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(44, 88), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_mutable_service(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (google_protobuf_ServiceDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(48, 96), len); +} +UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_resize_service(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_ServiceDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(48, 96), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_ServiceDescriptorProto* google_protobuf_FileDescriptorProto_add_service(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_ServiceDescriptorProto* sub = (struct google_protobuf_ServiceDescriptorProto*)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(48, 96), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_mutable_extension(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(52, 104), len); +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_resize_extension(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(52, 104), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_FileDescriptorProto_add_extension(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(52, 104), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, google_protobuf_FileOptions*, UPB_SIZE(28, 56)) = 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_msg_new(&google_protobuf_FileOptions_msginit, arena); + if (!sub) return NULL; + 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) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)) = 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_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena); + if (!sub) return NULL; + 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 *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 112), len); +} +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_public_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(56, 112), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_public_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(56, 112), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); +} +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(60, 120), len); +} +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(60, 120), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_weak_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(60, 120), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value; +} - /* Adds defs to this file. The def must not already belong to another - * file. - * - * Note: this does *not* ensure that this def's name is unique in this file! - * Use a SymbolTable if you want to check this property. Especially since - * properly checking uniqueness would require a check across *all* files - * (including dependencies). */ - bool AddDef(Def* def, Status* s); - bool AddMessage(MessageDef* m, Status* s); - bool AddEnum(EnumDef* e, Status* s); - bool AddExtension(FieldDef* f, Status* s); - - /* Adds a dependency of this file. */ - bool AddDependency(const FileDef* file); - - /* Freezes this FileDef and all messages/enums under it. All subdefs must be - * resolved and all messages/enums must validate. Returns true if this - * succeeded. - * - * TODO(haberman): should we care whether the file's dependencies are frozen - * already? */ - bool Freeze(Status* s); - private: - UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef) -}; +/* google.protobuf.DescriptorProto */ -#endif +UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_arena *arena) { + return (google_protobuf_DescriptorProto *)upb_msg_new(&google_protobuf_DescriptorProto_msginit, 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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, arena, len); +} -UPB_BEGIN_EXTERN_C +UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } +UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } +UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } +UPB_INLINE const google_protobuf_DescriptorProto_ExtensionRange* const* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ExtensionRange* const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); } +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(32, 64), len); } +UPB_INLINE bool google_protobuf_DescriptorProto_has_options(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MessageOptions*, UPB_SIZE(12, 24)); } +UPB_INLINE const google_protobuf_OneofDescriptorProto* const* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_OneofDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); } +UPB_INLINE const google_protobuf_DescriptorProto_ReservedRange* const* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); } +UPB_INLINE upb_strview const* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); } -upb_filedef *upb_filedef_new(const void *owner); +UPB_INLINE void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_field(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len); +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_field(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_field(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_mutable_nested_type(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len); +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_resize_nested_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_add_nested_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_mutable_enum_type(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len); +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_resize_enum_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_DescriptorProto_add_enum_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(24, 48), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_mutable_extension_range(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len); +} +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_resize_extension_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_add_extension_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_DescriptorProto_ExtensionRange* sub = (struct google_protobuf_DescriptorProto_ExtensionRange*)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(28, 56), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_extension(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(32, 64), len); +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_extension(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(32, 64), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_extension(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(32, 64), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, google_protobuf_MessageOptions*, UPB_SIZE(12, 24)) = 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_msg_new(&google_protobuf_MessageOptions_msginit, arena); + if (!sub) return NULL; + 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 *len) { + return (google_protobuf_OneofDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len); +} +UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_resize_oneof_decl(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_OneofDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_OneofDescriptorProto* google_protobuf_DescriptorProto_add_oneof_decl(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_OneofDescriptorProto* sub = (struct google_protobuf_OneofDescriptorProto*)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(36, 72), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_mutable_reserved_range(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len); +} +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_resize_reserved_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_add_reserved_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_DescriptorProto_ReservedRange* sub = (struct google_protobuf_DescriptorProto_ReservedRange*)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE upb_strview* google_protobuf_DescriptorProto_mutable_reserved_name(google_protobuf_DescriptorProto *msg, size_t *len) { + return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len); +} +UPB_INLINE upb_strview* google_protobuf_DescriptorProto_resize_reserved_name(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena); +} +UPB_INLINE bool google_protobuf_DescriptorProto_add_reserved_name(google_protobuf_DescriptorProto *msg, upb_strview val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(44, 88), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); +} -/* Include upb_refcounted methods like upb_msgdef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_filedef, upb_filedef_upcast) -const char *upb_filedef_name(const upb_filedef *f); -const char *upb_filedef_package(const upb_filedef *f); -const char *upb_filedef_phpprefix(const upb_filedef *f); -const char *upb_filedef_phpnamespace(const upb_filedef *f); -upb_syntax_t upb_filedef_syntax(const upb_filedef *f); -size_t upb_filedef_defcount(const upb_filedef *f); -size_t upb_filedef_depcount(const upb_filedef *f); -const upb_def *upb_filedef_def(const upb_filedef *f, size_t i); -const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i); +/* google.protobuf.DescriptorProto.ExtensionRange */ -bool upb_filedef_freeze(upb_filedef *f, upb_status *s); -bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s); -bool upb_filedef_setpackage(upb_filedef *f, const char *package, upb_status *s); -bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix, - upb_status *s); -bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace, - upb_status *s); -bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax, upb_status *s); +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_arena *arena) { + return (google_protobuf_DescriptorProto_ExtensionRange *)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, 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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_ExtensionRange_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, arena, len); +} -bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor, - upb_status *s); -bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep); +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)); } -UPB_INLINE bool upb_filedef_addmsg(upb_filedef *f, upb_msgdef *m, - const void *ref_donor, upb_status *s) { - return upb_filedef_adddef(f, upb_msgdef_upcast_mutable(m), ref_donor, s); +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; } - -UPB_INLINE bool upb_filedef_addenum(upb_filedef *f, upb_enumdef *e, - const void *ref_donor, upb_status *s) { - return upb_filedef_adddef(f, upb_enumdef_upcast_mutable(e), ref_donor, s); +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } - -UPB_INLINE bool upb_filedef_addext(upb_filedef *file, upb_fielddef *f, - const void *ref_donor, upb_status *s) { - return upb_filedef_adddef(file, upb_fielddef_upcast_mutable(f), ref_donor, s); +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)) = value; } -UPB_INLINE upb_def *upb_filedef_mutabledef(upb_filedef *f, int i) { - return (upb_def*)upb_filedef_def(f, i); +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_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena); + if (!sub) return NULL; + google_protobuf_DescriptorProto_ExtensionRange_set_options(msg, sub); + } + return sub; } -UPB_END_EXTERN_C -typedef struct { - UPB_PRIVATE_FOR_CPP - upb_strtable_iter iter; - upb_deftype_t type; -} upb_symtab_iter; - -#ifdef __cplusplus +/* google.protobuf.DescriptorProto.ReservedRange */ -/* Non-const methods in upb::SymbolTable are NOT thread-safe. */ -class upb::SymbolTable { - public: - /* Returns a new symbol table with a single ref owned by "owner." - * Returns NULL if memory allocation failed. */ - static SymbolTable* New(); - static void Free(upb::SymbolTable* table); - - /* For all lookup functions, the returned pointer is not owned by the - * caller; it may be invalidated by any non-const call or unref of the - * SymbolTable! To protect against this, take a ref if desired. */ - - /* Freezes the symbol table: prevents further modification of it. - * After the Freeze() operation is successful, the SymbolTable must only be - * accessed via a const pointer. - * - * Unlike with upb::MessageDef/upb::EnumDef/etc, freezing a SymbolTable is not - * a necessary step in using a SymbolTable. If you have no need for it to be - * immutable, there is no need to freeze it ever. However sometimes it is - * useful, and SymbolTables that are statically compiled into the binary are - * always frozen by nature. */ - void Freeze(); - - /* Resolves the given symbol using the rules described in descriptor.proto, - * namely: - * - * If the name starts with a '.', it is fully-qualified. Otherwise, - * C++-like scoping rules are used to find the type (i.e. first the nested - * types within this message are searched, then within the parent, on up - * to the root namespace). - * - * If not found, returns NULL. */ - const Def* Resolve(const char* base, const char* sym) const; +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_arena *arena) { + return (google_protobuf_DescriptorProto_ReservedRange *)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, 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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_ReservedRange_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, arena, len); +} - /* Finds an entry in the symbol table with this exact name. If not found, - * returns NULL. */ - const Def* Lookup(const char *sym) const; - const MessageDef* LookupMessage(const char *sym) const; - const EnumDef* LookupEnum(const char *sym) const; - - /* TODO: introduce a C++ iterator, but make it nice and templated so that if - * you ask for an iterator of MessageDef the iterated elements are strongly - * typed as MessageDef*. */ - - /* Adds the given mutable defs to the symtab, resolving all symbols (including - * enum default values) and finalizing the defs. Only one def per name may be - * in the list, and the defs may not duplicate any name already in the symtab. - * All defs must have a name -- anonymous defs are not allowed. Anonymous - * defs can still be frozen by calling upb_def_freeze() directly. - * - * The entire operation either succeeds or fails. If the operation fails, - * the symtab is unchanged, false is returned, and status indicates the - * error. The caller passes a ref on all defs to the symtab (even if the - * operation fails). - * - * TODO(haberman): currently failure will leave the symtab unchanged, but may - * leave the defs themselves partially resolved. Does this matter? If so we - * could do a prepass that ensures that all symbols are resolvable and bail - * if not, so we don't mutate anything until we know the operation will - * succeed. */ - bool Add(Def*const* defs, size_t n, void* ref_donor, Status* status); +UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } - bool Add(const std::vector& defs, void *owner, Status* status) { - return Add((Def*const*)&defs[0], defs.size(), owner, status); - } +UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; +} +UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; +} - /* Resolves all subdefs for messages in this file and attempts to freeze the - * file. If this succeeds, adds all the symbols to this SymbolTable - * (replacing any existing ones with the same names). */ - bool AddFile(FileDef* file, Status* s); - private: - UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable) -}; +/* google.protobuf.ExtensionRangeOptions */ -#endif /* __cplusplus */ +UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_arena *arena) { + return (google_protobuf_ExtensionRangeOptions *)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, 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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_ExtensionRangeOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, arena, len); +} -UPB_BEGIN_EXTERN_C +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } -/* Native C API. */ +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_mutable_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_resize_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ExtensionRangeOptions_add_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} -upb_symtab *upb_symtab_new(); -void upb_symtab_free(upb_symtab* s); -const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, - const char *sym); -const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym); -const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); -const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); -bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, - void *ref_donor, upb_status *status); -bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status* status); - -/* upb_symtab_iter i; - * for(upb_symtab_begin(&i, s, type); !upb_symtab_done(&i); - * upb_symtab_next(&i)) { - * const upb_def *def = upb_symtab_iter_def(&i); - * // ... - * } - * - * For C we don't have separate iterators for const and non-const. - * It is the caller's responsibility to cast the upb_fielddef* to - * const if the upb_msgdef* is const. */ -void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s, - upb_deftype_t type); -void upb_symtab_next(upb_symtab_iter *iter); -bool upb_symtab_done(const upb_symtab_iter *iter); -const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter); -UPB_END_EXTERN_C +/* google.protobuf.FieldDescriptorProto */ -#ifdef __cplusplus -/* C++ inline wrappers. */ -namespace upb { -inline SymbolTable* SymbolTable::New() { - return upb_symtab_new(); +UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_FieldDescriptorProto *)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); } -inline void SymbolTable::Free(SymbolTable* s) { - upb_symtab_free(s); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_FieldDescriptorProto_msginit)) ? ret : NULL; } -inline const Def *SymbolTable::Resolve(const char *base, - const char *sym) const { - return upb_symtab_resolve(this, base, sym); +UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, arena, len); } -inline const Def* SymbolTable::Lookup(const char *sym) const { - return upb_symtab_lookup(this, sym); + +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_extendee(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_number(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_label(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 7); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_default_value(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 8); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_options(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 10); } +UPB_INLINE const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FieldOptions*, UPB_SIZE(72, 112)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_json_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 9); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)); } + +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value; } -inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const { - return upb_symtab_lookupmsg(this, sym); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 6); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value; } -inline bool SymbolTable::Add( - Def*const* defs, size_t n, void* ref_donor, Status* status) { - return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)) = value; } -inline bool SymbolTable::AddFile(FileDef* file, Status* s) { - return upb_symtab_addfile(this, file, s); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } -} /* namespace upb */ -#endif - -#ifdef __cplusplus - -UPB_INLINE const char* upb_safecstr(const std::string& str) { - UPB_ASSERT(str.size() == std::strlen(str.c_str())); - return str.c_str(); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, int32_t value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)) = value; } - -/* Inline C++ wrappers. */ -namespace upb { - -inline Def::Type Def::def_type() const { return upb_def_type(this); } -inline const char* Def::full_name() const { return upb_def_fullname(this); } -inline const char* Def::name() const { return upb_def_name(this); } -inline bool Def::set_full_name(const char* fullname, Status* s) { - return upb_def_setfullname(this, fullname, s); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 7); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value; } -inline bool Def::set_full_name(const std::string& fullname, Status* s) { - return upb_def_setfullname(this, upb_safecstr(fullname), s); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 8); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)) = value; } -inline bool Def::Freeze(Def* const* defs, size_t n, Status* status) { - return upb_def_freeze(defs, n, status); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value) { + _upb_sethas(msg, 10); + UPB_FIELD_AT(msg, google_protobuf_FieldOptions*, UPB_SIZE(72, 112)) = value; } -inline bool Def::Freeze(const std::vector& defs, Status* status) { - return upb_def_freeze((Def* const*)&defs[0], defs.size(), status); +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_msg_new(&google_protobuf_FieldOptions_msginit, arena); + if (!sub) return NULL; + google_protobuf_FieldDescriptorProto_set_options(msg, sub); + } + return sub; } - -inline bool FieldDef::CheckType(int32_t val) { - return upb_fielddef_checktype(val); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto *msg, int32_t value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)) = value; } -inline bool FieldDef::CheckLabel(int32_t val) { - return upb_fielddef_checklabel(val); +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 9); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)) = value; } -inline bool FieldDef::CheckDescriptorType(int32_t val) { - return upb_fielddef_checkdescriptortype(val); + + +/* google.protobuf.OneofDescriptorProto */ + +UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_OneofDescriptorProto *)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena); } -inline bool FieldDef::CheckIntegerFormat(int32_t val) { - return upb_fielddef_checkintfmt(val); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_OneofDescriptorProto_msginit)) ? ret : NULL; } -inline FieldDef::Type FieldDef::ConvertType(int32_t val) { - UPB_ASSERT(CheckType(val)); - return static_cast(val); +UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, arena, len); } -inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) { - UPB_ASSERT(CheckLabel(val)); - return static_cast(val); + +UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_options(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_OneofOptions*, UPB_SIZE(12, 24)); } + +UPB_INLINE void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; } -inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) { - UPB_ASSERT(CheckDescriptorType(val)); - return static_cast(val); +UPB_INLINE void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, google_protobuf_OneofOptions*, UPB_SIZE(12, 24)) = value; } -inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) { - UPB_ASSERT(CheckIntegerFormat(val)); - return static_cast(val); +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_msg_new(&google_protobuf_OneofOptions_msginit, arena); + if (!sub) return NULL; + google_protobuf_OneofDescriptorProto_set_options(msg, sub); + } + return sub; } -inline reffed_ptr FieldDef::New() { - upb_fielddef *f = upb_fielddef_new(&f); - return reffed_ptr(f, &f); + +/* google.protobuf.EnumDescriptorProto */ + +UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto *)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena); } -inline const char* FieldDef::full_name() const { - return upb_fielddef_fullname(this); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumDescriptorProto_msginit)) ? ret : NULL; } -inline bool FieldDef::set_full_name(const char* fullname, Status* s) { - return upb_fielddef_setfullname(this, fullname, s); +UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, arena, len); } -inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) { - return upb_fielddef_setfullname(this, upb_safecstr(fullname), s); + +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE const google_protobuf_EnumValueDescriptorProto* const* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumValueDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_options(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumOptions*, UPB_SIZE(12, 24)); } +UPB_INLINE const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto_EnumReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } +UPB_INLINE upb_strview const* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } + +UPB_INLINE void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; } -inline bool FieldDef::type_is_set() const { - return upb_fielddef_typeisset(this); +UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_mutable_value(google_protobuf_EnumDescriptorProto *msg, size_t *len) { + return (google_protobuf_EnumValueDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len); } -inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); } -inline FieldDef::DescriptorType FieldDef::descriptor_type() const { - return upb_fielddef_descriptortype(this); +UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_resize_value(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_EnumValueDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline FieldDef::Label FieldDef::label() const { - return upb_fielddef_label(this); +UPB_INLINE struct google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumDescriptorProto_add_value(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_EnumValueDescriptorProto* sub = (struct google_protobuf_EnumValueDescriptorProto*)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline uint32_t FieldDef::number() const { return upb_fielddef_number(this); } -inline const char* FieldDef::name() const { return upb_fielddef_name(this); } -inline bool FieldDef::is_extension() const { - return upb_fielddef_isextension(this); +UPB_INLINE void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, google_protobuf_EnumOptions*, UPB_SIZE(12, 24)) = value; } -inline size_t FieldDef::GetJsonName(char* buf, size_t len) const { - return upb_fielddef_getjsonname(this, buf, len); +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_msg_new(&google_protobuf_EnumOptions_msginit, arena); + if (!sub) return NULL; + google_protobuf_EnumDescriptorProto_set_options(msg, sub); + } + return sub; } -inline bool FieldDef::lazy() const { - return upb_fielddef_lazy(this); +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_mutable_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t *len) { + return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len); } -inline void FieldDef::set_lazy(bool lazy) { - upb_fielddef_setlazy(this, lazy); +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_resize_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline bool FieldDef::packed() const { - return upb_fielddef_packed(this); +UPB_INLINE struct google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_add_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_EnumDescriptorProto_EnumReservedRange* sub = (struct google_protobuf_EnumDescriptorProto_EnumReservedRange*)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline uint32_t FieldDef::index() const { - return upb_fielddef_index(this); +UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_mutable_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t *len) { + return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len); } -inline void FieldDef::set_packed(bool packed) { - upb_fielddef_setpacked(this, packed); +UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_resize_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) { + return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena); } -inline const MessageDef* FieldDef::containing_type() const { - return upb_fielddef_containingtype(this); +UPB_INLINE bool google_protobuf_EnumDescriptorProto_add_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_strview val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(24, 48), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); } -inline const OneofDef* FieldDef::containing_oneof() const { - return upb_fielddef_containingoneof(this); + + +/* google.protobuf.EnumDescriptorProto.EnumReservedRange */ + +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto_EnumReservedRange *)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena); } -inline const char* FieldDef::containing_type_name() { - return upb_fielddef_containingtypename(this); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit)) ? ret : NULL; } -inline bool FieldDef::set_number(uint32_t number, Status* s) { - return upb_fielddef_setnumber(this, number, s); +UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena, len); } -inline bool FieldDef::set_name(const char *name, Status* s) { - return upb_fielddef_setname(this, name, s); + +UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } + +UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; } -inline bool FieldDef::set_name(const std::string& name, Status* s) { - return upb_fielddef_setname(this, upb_safecstr(name), s); +UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } -inline bool FieldDef::set_json_name(const char *name, Status* s) { - return upb_fielddef_setjsonname(this, name, s); + + +/* google.protobuf.EnumValueDescriptorProto */ + +UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_EnumValueDescriptorProto *)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena); } -inline bool FieldDef::set_json_name(const std::string& name, Status* s) { - return upb_fielddef_setjsonname(this, upb_safecstr(name), s); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumValueDescriptorProto_msginit)) ? ret : NULL; } -inline void FieldDef::clear_json_name() { - upb_fielddef_clearjsonname(this); +UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, arena, len); } -inline bool FieldDef::set_containing_type_name(const char *name, Status* s) { - return upb_fielddef_setcontainingtypename(this, name, s); + +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE upb_strview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_number(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_options(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)); } + +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)) = value; } -inline bool FieldDef::set_containing_type_name(const std::string &name, - Status *s) { - return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s); +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; } -inline void FieldDef::set_type(upb_fieldtype_t type) { - upb_fielddef_settype(this, type); +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)) = value; } -inline void FieldDef::set_is_extension(bool is_extension) { - upb_fielddef_setisextension(this, is_extension); +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_msg_new(&google_protobuf_EnumValueOptions_msginit, arena); + if (!sub) return NULL; + google_protobuf_EnumValueDescriptorProto_set_options(msg, sub); + } + return sub; } -inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) { - upb_fielddef_setdescriptortype(this, type); + + +/* google.protobuf.ServiceDescriptorProto */ + +UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_ServiceDescriptorProto *)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena); } -inline void FieldDef::set_label(upb_label_t label) { - upb_fielddef_setlabel(this, label); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_ServiceDescriptorProto_msginit)) ? ret : NULL; } -inline bool FieldDef::IsSubMessage() const { - return upb_fielddef_issubmsg(this); +UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, arena, len); } -inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); } -inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); } -inline bool FieldDef::IsMap() const { return upb_fielddef_ismap(this); } -inline int64_t FieldDef::default_int64() const { - return upb_fielddef_defaultint64(this); + +UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE const google_protobuf_MethodDescriptorProto* const* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg, size_t *len) { return (const google_protobuf_MethodDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } +UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_options(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)); } + +UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; } -inline int32_t FieldDef::default_int32() const { - return upb_fielddef_defaultint32(this); +UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_mutable_method(google_protobuf_ServiceDescriptorProto *msg, size_t *len) { + return (google_protobuf_MethodDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len); } -inline uint64_t FieldDef::default_uint64() const { - return upb_fielddef_defaultuint64(this); +UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_resize_method(google_protobuf_ServiceDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_MethodDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline uint32_t FieldDef::default_uint32() const { - return upb_fielddef_defaultuint32(this); +UPB_INLINE struct google_protobuf_MethodDescriptorProto* google_protobuf_ServiceDescriptorProto_add_method(google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_MethodDescriptorProto* sub = (struct google_protobuf_MethodDescriptorProto*)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline bool FieldDef::default_bool() const { - return upb_fielddef_defaultbool(this); +UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)) = value; } -inline float FieldDef::default_float() const { - return upb_fielddef_defaultfloat(this); +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_msg_new(&google_protobuf_ServiceOptions_msginit, arena); + if (!sub) return NULL; + google_protobuf_ServiceDescriptorProto_set_options(msg, sub); + } + return sub; } -inline double FieldDef::default_double() const { - return upb_fielddef_defaultdouble(this); + + +/* google.protobuf.MethodDescriptorProto */ + +UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_MethodDescriptorProto *)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena); } -inline const char* FieldDef::default_string(size_t* len) const { - return upb_fielddef_defaultstr(this, len); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_MethodDescriptorProto_msginit)) ? ret : NULL; } -inline void FieldDef::set_default_int64(int64_t value) { - upb_fielddef_setdefaultint64(this, value); +UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, arena, len); } -inline void FieldDef::set_default_int32(int32_t value) { - upb_fielddef_setdefaultint32(this, value); + +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_input_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_output_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_options(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MethodOptions*, UPB_SIZE(28, 56)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } + +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; } -inline void FieldDef::set_default_uint64(uint64_t value) { - upb_fielddef_setdefaultuint64(this, value); +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value; } -inline void FieldDef::set_default_uint32(uint32_t value) { - upb_fielddef_setdefaultuint32(this, value); +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value; } -inline void FieldDef::set_default_bool(bool value) { - upb_fielddef_setdefaultbool(this, value); +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value) { + _upb_sethas(msg, 6); + UPB_FIELD_AT(msg, google_protobuf_MethodOptions*, UPB_SIZE(28, 56)) = value; } -inline void FieldDef::set_default_float(float value) { - upb_fielddef_setdefaultfloat(this, 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_msg_new(&google_protobuf_MethodOptions_msginit, arena); + if (!sub) return NULL; + google_protobuf_MethodDescriptorProto_set_options(msg, sub); + } + return sub; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; +} + + +/* google.protobuf.FileOptions */ + +UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_arena *arena) { + return (google_protobuf_FileOptions *)upb_msg_new(&google_protobuf_FileOptions_msginit, 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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_FileOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FileOptions_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 11); } +UPB_INLINE upb_strview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_outer_classname(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 12); } +UPB_INLINE upb_strview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)); } +UPB_INLINE bool google_protobuf_FileOptions_has_optimize_for(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_multiple_files(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_FileOptions_has_go_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 13); } +UPB_INLINE upb_strview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)); } +UPB_INLINE bool google_protobuf_FileOptions_has_cc_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)); } +UPB_INLINE bool google_protobuf_FileOptions_has_py_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)); } +UPB_INLINE bool google_protobuf_FileOptions_has_deprecated(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 7); } +UPB_INLINE bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 8); } +UPB_INLINE bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)); } +UPB_INLINE bool google_protobuf_FileOptions_has_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 9); } +UPB_INLINE bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)); } +UPB_INLINE bool google_protobuf_FileOptions_has_objc_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 14); } +UPB_INLINE upb_strview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)); } +UPB_INLINE bool google_protobuf_FileOptions_has_csharp_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 15); } +UPB_INLINE upb_strview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)); } +UPB_INLINE bool google_protobuf_FileOptions_has_swift_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 16); } +UPB_INLINE upb_strview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 17); } +UPB_INLINE upb_strview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 18); } +UPB_INLINE upb_strview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 10); } +UPB_INLINE bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(92, 160), len); } + +UPB_INLINE void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 11); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 12); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 13); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 6); + UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 7); + UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 8); + UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 9); + UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 14); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 15); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 16); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 17); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 18); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 10); + UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_mutable_uninterpreted_option(google_protobuf_FileOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(92, 160), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_resize_uninterpreted_option(google_protobuf_FileOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(92, 160), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FileOptions_add_uninterpreted_option(google_protobuf_FileOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(92, 160), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} + + +/* google.protobuf.MessageOptions */ + +UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_arena *arena) { + return (google_protobuf_MessageOptions *)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena); } -inline void FieldDef::set_default_double(double value) { - upb_fielddef_setdefaultdouble(this, value); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_MessageOptions_msginit)) ? ret : NULL; } -inline bool FieldDef::set_default_string(const void *str, size_t len, - Status *s) { - return upb_fielddef_setdefaultstr(this, str, len, s); +UPB_INLINE char *google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_MessageOptions_msginit, arena, len); } -inline bool FieldDef::set_default_string(const std::string& str, Status* s) { - return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s); + +UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE bool google_protobuf_MessageOptions_has_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } +UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)); } +UPB_INLINE bool google_protobuf_MessageOptions_has_map_entry(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(8, 8), len); } + +UPB_INLINE void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; } -inline void FieldDef::set_default_cstr(const char* str, Status* s) { - return upb_fielddef_setdefaultcstr(this, str, s); +UPB_INLINE void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; } -inline bool FieldDef::HasSubDef() const { return upb_fielddef_hassubdef(this); } -inline const Def* FieldDef::subdef() const { return upb_fielddef_subdef(this); } -inline const MessageDef *FieldDef::message_subdef() const { - return upb_fielddef_msgsubdef(this); +UPB_INLINE void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)) = value; } -inline const EnumDef *FieldDef::enum_subdef() const { - return upb_fielddef_enumsubdef(this); +UPB_INLINE void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)) = value; } -inline const char* FieldDef::subdef_name() const { - return upb_fielddef_subdefname(this); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_mutable_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(8, 8), len); } -inline bool FieldDef::set_subdef(const Def* subdef, Status* s) { - return upb_fielddef_setsubdef(this, subdef, s); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_resize_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(8, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) { - return upb_fielddef_setenumsubdef(this, subdef, s); +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MessageOptions_add_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(8, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) { - return upb_fielddef_setmsgsubdef(this, subdef, s); + + +/* google.protobuf.FieldOptions */ + +UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_arena *arena) { + return (google_protobuf_FieldOptions *)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena); } -inline bool FieldDef::set_subdef_name(const char* name, Status* s) { - return upb_fielddef_setsubdefname(this, name, s); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_FieldOptions_msginit)) ? ret : NULL; } -inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) { - return upb_fielddef_setsubdefname(this, upb_safecstr(name), s); +UPB_INLINE char *google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FieldOptions_msginit, arena, len); } -inline reffed_ptr MessageDef::New() { - upb_msgdef *m = upb_msgdef_new(&m); - return reffed_ptr(m, &m); +UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_packed(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_deprecated(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_lazy(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_jstype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE int32_t google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_weak(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(28, 32), len); } + +UPB_INLINE void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } -inline const char *MessageDef::full_name() const { - return upb_msgdef_fullname(this); +UPB_INLINE void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value; } -inline const char *MessageDef::name() const { - return upb_msgdef_name(this); +UPB_INLINE void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)) = value; } -inline upb_syntax_t MessageDef::syntax() const { - return upb_msgdef_syntax(this); +UPB_INLINE void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)) = value; } -inline bool MessageDef::set_full_name(const char* fullname, Status* s) { - return upb_msgdef_setfullname(this, fullname, s); +UPB_INLINE void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, int32_t value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)) = value; } -inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) { - return upb_msgdef_setfullname(this, upb_safecstr(fullname), s); +UPB_INLINE void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value) { + _upb_sethas(msg, 6); + UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)) = value; } -inline bool MessageDef::set_syntax(upb_syntax_t syntax) { - return upb_msgdef_setsyntax(this, syntax); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_mutable_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 32), len); } -inline bool MessageDef::Freeze(Status* status) { - return upb_msgdef_freeze(this, status); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_resize_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline int MessageDef::field_count() const { - return upb_msgdef_numfields(this); +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FieldOptions_add_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(28, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline int MessageDef::oneof_count() const { - return upb_msgdef_numoneofs(this); + + +/* google.protobuf.OneofOptions */ + +UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_arena *arena) { + return (google_protobuf_OneofOptions *)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena); } -inline bool MessageDef::AddField(upb_fielddef* f, Status* s) { - return upb_msgdef_addfield(this, f, NULL, s); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_OneofOptions_msginit)) ? ret : NULL; } -inline bool MessageDef::AddField(const reffed_ptr& f, Status* s) { - return upb_msgdef_addfield(this, f.get(), NULL, s); +UPB_INLINE char *google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_OneofOptions_msginit, arena, len); } -inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) { - return upb_msgdef_addoneof(this, o, NULL, s); + +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_mutable_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); } -inline bool MessageDef::AddOneof(const reffed_ptr& o, Status* s) { - return upb_msgdef_addoneof(this, o.get(), NULL, s); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_resize_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) { - return upb_msgdef_itof_mutable(this, number); +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_OneofOptions_add_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) { - return upb_msgdef_ntof_mutable(this, name, len); + + +/* google.protobuf.EnumOptions */ + +UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_arena *arena) { + return (google_protobuf_EnumOptions *)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena); } -inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const { - return upb_msgdef_itof(this, number); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumOptions_msginit)) ? ret : NULL; } -inline const FieldDef *MessageDef::FindFieldByName(const char *name, - size_t len) const { - return upb_msgdef_ntof(this, name, len); +UPB_INLINE char *google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumOptions_msginit, arena, len); } -inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) { - return upb_msgdef_ntoo_mutable(this, name, len); + +UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } + +UPB_INLINE void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; } -inline const OneofDef* MessageDef::FindOneofByName(const char* name, - size_t len) const { - return upb_msgdef_ntoo(this, name, len); +UPB_INLINE void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; } -inline void MessageDef::setmapentry(bool map_entry) { - upb_msgdef_setmapentry(this, map_entry); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_mutable_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len); } -inline bool MessageDef::mapentry() const { - return upb_msgdef_mapentry(this); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_resize_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline MessageDef::field_iterator MessageDef::field_begin() { - return field_iterator(this); +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumOptions_add_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline MessageDef::field_iterator MessageDef::field_end() { - return field_iterator::end(this); + + +/* google.protobuf.EnumValueOptions */ + +UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_arena *arena) { + return (google_protobuf_EnumValueOptions *)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena); } -inline MessageDef::const_field_iterator MessageDef::field_begin() const { - return const_field_iterator(this); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumValueOptions_msginit)) ? ret : NULL; } -inline MessageDef::const_field_iterator MessageDef::field_end() const { - return const_field_iterator::end(this); +UPB_INLINE char *google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, arena, len); } -inline MessageDef::oneof_iterator MessageDef::oneof_begin() { - return oneof_iterator(this); +UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } + +UPB_INLINE void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; } -inline MessageDef::oneof_iterator MessageDef::oneof_end() { - return oneof_iterator::end(this); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_mutable_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len); } -inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const { - return const_oneof_iterator(this); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_resize_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const { - return const_oneof_iterator::end(this); +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumValueOptions_add_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline MessageDef::field_iterator::field_iterator(MessageDef* md) { - upb_msg_field_begin(&iter_, md); -} -inline MessageDef::field_iterator MessageDef::field_iterator::end( - MessageDef* md) { - MessageDef::field_iterator iter(md); - upb_msg_field_iter_setdone(&iter.iter_); - return iter; -} -inline FieldDef* MessageDef::field_iterator::operator*() const { - return upb_msg_iter_field(&iter_); -} -inline void MessageDef::field_iterator::operator++() { - return upb_msg_field_next(&iter_); + +/* google.protobuf.ServiceOptions */ + +UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_arena *arena) { + return (google_protobuf_ServiceOptions *)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena); } -inline bool MessageDef::field_iterator::operator==( - const field_iterator &other) const { - return upb_inttable_iter_isequal(&iter_, &other.iter_); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_ServiceOptions_msginit)) ? ret : NULL; } -inline bool MessageDef::field_iterator::operator!=( - const field_iterator &other) const { - return !(*this == other); +UPB_INLINE char *google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, arena, len); } -inline MessageDef::const_field_iterator::const_field_iterator( - const MessageDef* md) { - upb_msg_field_begin(&iter_, md); -} -inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end( - const MessageDef *md) { - MessageDef::const_field_iterator iter(md); - upb_msg_field_iter_setdone(&iter.iter_); - return iter; -} -inline const FieldDef* MessageDef::const_field_iterator::operator*() const { - return upb_msg_iter_field(&iter_); +UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } + +UPB_INLINE void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; } -inline void MessageDef::const_field_iterator::operator++() { - return upb_msg_field_next(&iter_); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_mutable_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len); } -inline bool MessageDef::const_field_iterator::operator==( - const const_field_iterator &other) const { - return upb_inttable_iter_isequal(&iter_, &other.iter_); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_resize_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline bool MessageDef::const_field_iterator::operator!=( - const const_field_iterator &other) const { - return !(*this == other); +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ServiceOptions_add_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) { - upb_msg_oneof_begin(&iter_, md); -} -inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end( - MessageDef* md) { - MessageDef::oneof_iterator iter(md); - upb_msg_oneof_iter_setdone(&iter.iter_); - return iter; -} -inline OneofDef* MessageDef::oneof_iterator::operator*() const { - return upb_msg_iter_oneof(&iter_); -} -inline void MessageDef::oneof_iterator::operator++() { - return upb_msg_oneof_next(&iter_); + +/* google.protobuf.MethodOptions */ + +UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_arena *arena) { + return (google_protobuf_MethodOptions *)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena); } -inline bool MessageDef::oneof_iterator::operator==( - const oneof_iterator &other) const { - return upb_strtable_iter_isequal(&iter_, &other.iter_); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_MethodOptions_msginit)) ? ret : NULL; } -inline bool MessageDef::oneof_iterator::operator!=( - const oneof_iterator &other) const { - return !(*this == other); +UPB_INLINE char *google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_MethodOptions_msginit, arena, len); } -inline MessageDef::const_oneof_iterator::const_oneof_iterator( - const MessageDef* md) { - upb_msg_oneof_begin(&iter_, md); -} -inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end( - const MessageDef *md) { - MessageDef::const_oneof_iterator iter(md); - upb_msg_oneof_iter_setdone(&iter.iter_); - return iter; +UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_MethodOptions_has_idempotency_level(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(20, 24), len); } + +UPB_INLINE void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value; } -inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const { - return upb_msg_iter_oneof(&iter_); +UPB_INLINE void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } -inline void MessageDef::const_oneof_iterator::operator++() { - return upb_msg_oneof_next(&iter_); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_mutable_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 24), len); } -inline bool MessageDef::const_oneof_iterator::operator==( - const const_oneof_iterator &other) const { - return upb_strtable_iter_isequal(&iter_, &other.iter_); +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_resize_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 24), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline bool MessageDef::const_oneof_iterator::operator!=( - const const_oneof_iterator &other) const { - return !(*this == other); +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MethodOptions_add_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(20, 24), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline reffed_ptr EnumDef::New() { - upb_enumdef *e = upb_enumdef_new(&e); - return reffed_ptr(e, &e); + +/* google.protobuf.UninterpretedOption */ + +UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_arena *arena) { + return (google_protobuf_UninterpretedOption *)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); } -inline const char* EnumDef::full_name() const { - return upb_enumdef_fullname(this); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_UninterpretedOption_msginit)) ? ret : NULL; } -inline const char* EnumDef::name() const { - return upb_enumdef_name(this); +UPB_INLINE char *google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, arena, len); } -inline bool EnumDef::set_full_name(const char* fullname, Status* s) { - return upb_enumdef_setfullname(this, fullname, s); + +UPB_INLINE const google_protobuf_UninterpretedOption_NamePart* const* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg, size_t *len) { return (const google_protobuf_UninterpretedOption_NamePart* const*)_upb_array_accessor(msg, UPB_SIZE(56, 80), len); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE upb_strview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_double_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_string_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE upb_strview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE upb_strview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); } + +UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_mutable_name(google_protobuf_UninterpretedOption *msg, size_t *len) { + return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 80), len); } -inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) { - return upb_enumdef_setfullname(this, upb_safecstr(fullname), s); +UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_resize_name(google_protobuf_UninterpretedOption *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_resize_accessor(msg, UPB_SIZE(56, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline bool EnumDef::Freeze(Status* status) { - return upb_enumdef_freeze(this, status); +UPB_INLINE struct google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_add_name(google_protobuf_UninterpretedOption *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption_NamePart* sub = (struct google_protobuf_UninterpretedOption_NamePart*)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(56, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline int32_t EnumDef::default_value() const { - return upb_enumdef_default(this); +UPB_INLINE void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_strview value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value; } -inline bool EnumDef::set_default_value(int32_t val, Status* status) { - return upb_enumdef_setdefault(this, val, status); +UPB_INLINE void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)) = value; } -inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); } -inline bool EnumDef::AddValue(const char* name, int32_t num, Status* status) { - return upb_enumdef_addval(this, name, num, status); +UPB_INLINE void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)) = value; } -inline bool EnumDef::AddValue(const std::string& name, int32_t num, - Status* status) { - return upb_enumdef_addval(this, upb_safecstr(name), num, status); +UPB_INLINE void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)) = value; } -inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const { - return upb_enumdef_ntoiz(this, name, num); +UPB_INLINE void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_strview value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value; } -inline const char* EnumDef::FindValueByNumber(int32_t num) const { - return upb_enumdef_iton(this, num); +UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_strview value) { + _upb_sethas(msg, 6); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value; } -inline EnumDef::Iterator::Iterator(const EnumDef* e) { - upb_enum_begin(&iter_, e); + +/* google.protobuf.UninterpretedOption.NamePart */ + +UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_arena *arena) { + return (google_protobuf_UninterpretedOption_NamePart *)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena); } -inline int32_t EnumDef::Iterator::number() { - return upb_enum_iter_number(&iter_); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_UninterpretedOption_NamePart_msginit)) ? ret : NULL; } -inline const char* EnumDef::Iterator::name() { - return upb_enum_iter_name(&iter_); +UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, arena, len); } -inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); } -inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); } -inline reffed_ptr OneofDef::New() { - upb_oneofdef *o = upb_oneofdef_new(&o); - return reffed_ptr(o, &o); -} +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE upb_strview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } -inline const MessageDef* OneofDef::containing_type() const { - return upb_oneofdef_containingtype(this); +UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_strview value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; } -inline const char* OneofDef::name() const { - return upb_oneofdef_name(this); +UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; } -inline bool OneofDef::set_name(const char* name, Status* s) { - return upb_oneofdef_setname(this, name, s); + + +/* google.protobuf.SourceCodeInfo */ + +UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_arena *arena) { + return (google_protobuf_SourceCodeInfo *)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena); } -inline bool OneofDef::set_name(const std::string& name, Status* s) { - return upb_oneofdef_setname(this, upb_safecstr(name), s); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_SourceCodeInfo_msginit)) ? ret : NULL; } -inline int OneofDef::field_count() const { - return upb_oneofdef_numfields(this); +UPB_INLINE char *google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, arena, len); } -inline bool OneofDef::AddField(FieldDef* field, Status* s) { - return upb_oneofdef_addfield(this, field, NULL, s); + +UPB_INLINE const google_protobuf_SourceCodeInfo_Location* const* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg, size_t *len) { return (const google_protobuf_SourceCodeInfo_Location* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } + +UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_mutable_location(google_protobuf_SourceCodeInfo *msg, size_t *len) { + return (google_protobuf_SourceCodeInfo_Location**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); } -inline bool OneofDef::AddField(const reffed_ptr& field, Status* s) { - return upb_oneofdef_addfield(this, field.get(), NULL, s); +UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_resize_location(google_protobuf_SourceCodeInfo *msg, size_t len, upb_arena *arena) { + return (google_protobuf_SourceCodeInfo_Location**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline const FieldDef* OneofDef::FindFieldByName(const char* name, - size_t len) const { - return upb_oneofdef_ntof(this, name, len); +UPB_INLINE struct google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_add_location(google_protobuf_SourceCodeInfo *msg, upb_arena *arena) { + struct google_protobuf_SourceCodeInfo_Location* sub = (struct google_protobuf_SourceCodeInfo_Location*)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const { - return upb_oneofdef_itof(this, num); + + +/* google.protobuf.SourceCodeInfo.Location */ + +UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_arena *arena) { + return (google_protobuf_SourceCodeInfo_Location *)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena); } -inline OneofDef::iterator OneofDef::begin() { return iterator(this); } -inline OneofDef::iterator OneofDef::end() { return iterator::end(this); } -inline OneofDef::const_iterator OneofDef::begin() const { - return const_iterator(this); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_SourceCodeInfo_Location_msginit)) ? ret : NULL; } -inline OneofDef::const_iterator OneofDef::end() const { - return const_iterator::end(this); +UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, arena, len); } -inline OneofDef::iterator::iterator(OneofDef* o) { - upb_oneof_begin(&iter_, o); +UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } +UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); } +UPB_INLINE upb_strview const* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); } + +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_path(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len); } -inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) { - OneofDef::iterator iter(o); - upb_oneof_iter_setdone(&iter.iter_); - return iter; +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_path(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); } -inline FieldDef* OneofDef::iterator::operator*() const { - return upb_oneof_iter_field(&iter_); +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_path(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(20, 40), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); } -inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); } -inline bool OneofDef::iterator::operator==(const iterator &other) const { - return upb_inttable_iter_isequal(&iter_, &other.iter_); +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_span(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len); } -inline bool OneofDef::iterator::operator!=(const iterator &other) const { - return !(*this == other); +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_span(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); } - -inline OneofDef::const_iterator::const_iterator(const OneofDef* md) { - upb_oneof_begin(&iter_, md); +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_span(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(24, 48), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); } -inline OneofDef::const_iterator OneofDef::const_iterator::end( - const OneofDef *md) { - OneofDef::const_iterator iter(md); - upb_oneof_iter_setdone(&iter.iter_); - return iter; +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; } -inline const FieldDef* OneofDef::const_iterator::operator*() const { - return upb_msg_iter_field(&iter_); +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value; } -inline void OneofDef::const_iterator::operator++() { - return upb_oneof_next(&iter_); +UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_mutable_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { + return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len); } -inline bool OneofDef::const_iterator::operator==( - const const_iterator &other) const { - return upb_inttable_iter_isequal(&iter_, &other.iter_); +UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_resize_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) { + return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena); } -inline bool OneofDef::const_iterator::operator!=( - const const_iterator &other) const { - return !(*this == other); +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(28, 56), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); } -inline reffed_ptr FileDef::New() { - upb_filedef *f = upb_filedef_new(&f); - return reffed_ptr(f, &f); -} -inline const char* FileDef::name() const { - return upb_filedef_name(this); -} -inline bool FileDef::set_name(const char* name, Status* s) { - return upb_filedef_setname(this, name, s); -} -inline bool FileDef::set_name(const std::string& name, Status* s) { - return upb_filedef_setname(this, upb_safecstr(name), s); -} -inline const char* FileDef::package() const { - return upb_filedef_package(this); -} -inline bool FileDef::set_package(const char* package, Status* s) { - return upb_filedef_setpackage(this, package, s); +/* google.protobuf.GeneratedCodeInfo */ + +UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_arena *arena) { + return (google_protobuf_GeneratedCodeInfo *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_msginit, arena); } -inline const char* FileDef::phpprefix() const { - return upb_filedef_phpprefix(this); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_GeneratedCodeInfo_msginit)) ? ret : NULL; } -inline bool FileDef::set_phpprefix(const char* phpprefix, Status* s) { - return upb_filedef_setphpprefix(this, phpprefix, s); +UPB_INLINE char *google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, arena, len); } -inline const char* FileDef::phpnamespace() const { - return upb_filedef_phpnamespace(this); + +UPB_INLINE const google_protobuf_GeneratedCodeInfo_Annotation* const* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg, size_t *len) { return (const google_protobuf_GeneratedCodeInfo_Annotation* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } + +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_mutable_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t *len) { + return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); } -inline bool FileDef::set_phpnamespace(const char* phpnamespace, Status* s) { - return upb_filedef_setphpnamespace(this, phpnamespace, s); +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_resize_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t len, upb_arena *arena) { + return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); } -inline int FileDef::def_count() const { - return upb_filedef_defcount(this); +UPB_INLINE struct google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_add_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena) { + struct google_protobuf_GeneratedCodeInfo_Annotation* sub = (struct google_protobuf_GeneratedCodeInfo_Annotation*)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; } -inline const Def* FileDef::def(int index) const { - return upb_filedef_def(this, index); + + +/* google.protobuf.GeneratedCodeInfo.Annotation */ + +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_arena *arena) { + return (google_protobuf_GeneratedCodeInfo_Annotation *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena); } -inline Def* FileDef::def(int index) { - return const_cast(upb_filedef_def(this, index)); +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); + return (ret && upb_decode(buf, size, ret, &google_protobuf_GeneratedCodeInfo_Annotation_msginit)) ? ret : NULL; } -inline int FileDef::dependency_count() const { - return upb_filedef_depcount(this); +UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena, len); } -inline const FileDef* FileDef::dependency(int index) const { - return upb_filedef_dep(this, index); + +UPB_INLINE int32_t const* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 32), len); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE upb_strview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } + +UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_mutable_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 32), len); } -inline bool FileDef::AddDef(Def* def, Status* s) { - return upb_filedef_adddef(this, def, NULL, s); +UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_resize_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 32), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); } -inline bool FileDef::AddMessage(MessageDef* m, Status* s) { - return upb_filedef_addmsg(this, m, NULL, s); +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_add_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(20, 32), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); } -inline bool FileDef::AddEnum(EnumDef* e, Status* s) { - return upb_filedef_addenum(this, e, NULL, s); +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_strview value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)) = value; } -inline bool FileDef::AddExtension(FieldDef* f, Status* s) { - return upb_filedef_addext(this, f, NULL, s); +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; } -inline bool FileDef::AddDependency(const FileDef* file) { - return upb_filedef_adddep(this, file); +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } -} /* namespace upb */ + +#ifdef __cplusplus +} /* extern "C" */ #endif -#endif /* UPB_DEF_H_ */ + +#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ */ /* -** upb::Handlers (upb_handlers) +** Defs are upb's internal representation of the constructs that can appear +** in a .proto file: ** -** A upb_handlers is like a virtual table for a upb_msgdef. Each field of the -** message can have associated functions that will be called when we are -** parsing or visiting a stream of data. This is similar to how handlers work -** in SAX (the Simple API for XML). +** - upb::MessageDefPtr (upb_msgdef): describes a "message" construct. +** - upb::FieldDefPtr (upb_fielddef): describes a message field. +** - upb::FileDefPtr (upb_filedef): describes a .proto file and its defs. +** - upb::EnumDefPtr (upb_enumdef): describes an enum. +** - upb::OneofDefPtr (upb_oneofdef): describes a oneof. ** -** The handlers have no idea where the data is coming from, so a single set of -** handlers could be used with two completely different data sources (for -** example, a parser and a visitor over in-memory objects). This decoupling is -** the most important feature of upb, because it allows parsers and serializers -** to be highly reusable. +** TODO: definitions of services. ** ** This is a mixed C/C++ interface that offers a full API to both languages. ** See the top-level README for more information. */ -#ifndef UPB_HANDLERS_H -#define UPB_HANDLERS_H +#ifndef UPB_DEF_H_ +#define UPB_DEF_H_ + +/* +** 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 homogenous (all values of the same type). In debug +** mode, we check this on insert and lookup. +*/ + +#ifndef UPB_TABLE_H_ +#define UPB_TABLE_H_ +#include +#include #ifdef __cplusplus -namespace upb { -class BufferHandle; -class BytesHandler; -class HandlerAttributes; -class Handlers; -template class Handler; -template struct CanonicalType; -} /* namespace upb */ +extern "C" { #endif -UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle) -UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler) -UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr) -UPB_DECLARE_DERIVED_TYPE(upb::Handlers, upb::RefCounted, - upb_handlers, upb_refcounted) -/* The maximum depth that the handler graph can have. This is a resource limit - * for the C stack since we sometimes need to recursively traverse the graph. - * Cycles are ok; the traversal will stop when it detects a cycle, but we must - * hit the cycle before the maximum depth is reached. - * - * If having a single static limit is too inflexible, we can add another variant - * of Handlers::Freeze that allows specifying this as a parameter. */ -#define UPB_MAX_HANDLER_DEPTH 64 +/* upb_value ******************************************************************/ -/* All the different types of handlers that can be registered. - * Only needed for the advanced functions in upb::Handlers. */ +/* A tagged union (stored untagged inside the table) so that we can check that + * clients calling table accessors are correctly typed without having to have + * an explosion of accessors. */ typedef enum { - UPB_HANDLER_INT32, - UPB_HANDLER_INT64, - UPB_HANDLER_UINT32, - UPB_HANDLER_UINT64, - UPB_HANDLER_FLOAT, - UPB_HANDLER_DOUBLE, - UPB_HANDLER_BOOL, - UPB_HANDLER_STARTSTR, - UPB_HANDLER_STRING, - UPB_HANDLER_ENDSTR, - UPB_HANDLER_STARTSUBMSG, - UPB_HANDLER_ENDSUBMSG, - UPB_HANDLER_STARTSEQ, - UPB_HANDLER_ENDSEQ -} upb_handlertype_t; + UPB_CTYPE_INT32 = 1, + UPB_CTYPE_INT64 = 2, + UPB_CTYPE_UINT32 = 3, + UPB_CTYPE_UINT64 = 4, + UPB_CTYPE_BOOL = 5, + UPB_CTYPE_CSTR = 6, + UPB_CTYPE_PTR = 7, + UPB_CTYPE_CONSTPTR = 8, + UPB_CTYPE_FPTR = 9, + UPB_CTYPE_FLOAT = 10, + UPB_CTYPE_DOUBLE = 11 +} upb_ctype_t; -#define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1) +typedef struct { + uint64_t val; +#ifndef NDEBUG + /* In debug mode we carry the value type around also so we can check accesses + * to be sure the right member is being read. */ + upb_ctype_t ctype; +#endif +} upb_value; + +#ifdef NDEBUG +#define SET_TYPE(dest, val) UPB_UNUSED(val) +#else +#define SET_TYPE(dest, val) dest = val +#endif + +/* Like strdup(), which isn't always available since it's not ANSI C. */ +char *upb_strdup(const char *s, upb_alloc *a); +/* 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_alloc *a); + +UPB_INLINE char *upb_gstrdup(const char *s) { + return upb_strdup(s, &upb_alloc_global); +} + +UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val, + upb_ctype_t ctype) { + v->val = val; + SET_TYPE(v->ctype, ctype); +} + +UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) { + upb_value ret; + _upb_value_setval(&ret, val, ctype); + return ret; +} + +/* 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, proto_type) \ + UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \ + val->val = (converter)cval; \ + SET_TYPE(val->ctype, proto_type); \ + } \ + 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) { \ + UPB_ASSERT_DEBUGVAR(val.ctype == proto_type); \ + return (type_t)(converter)val.val; \ + } + +FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32) +FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64) +FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32) +FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64) +FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL) +FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR) +FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR) +FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR) +FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR) + +#undef FUNCS + +UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) { + memcpy(&val->val, &cval, sizeof(cval)); + SET_TYPE(val->ctype, UPB_CTYPE_FLOAT); +} + +UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) { + memcpy(&val->val, &cval, sizeof(cval)); + SET_TYPE(val->ctype, UPB_CTYPE_DOUBLE); +} -#define UPB_BREAK NULL +UPB_INLINE upb_value upb_value_float(float cval) { + upb_value ret; + upb_value_setfloat(&ret, cval); + return ret; +} -/* A convenient definition for when no closure is needed. */ -extern char _upb_noclosure; -#define UPB_NO_CLOSURE &_upb_noclosure +UPB_INLINE upb_value upb_value_double(double cval) { + upb_value ret; + upb_value_setdouble(&ret, cval); + return ret; +} -/* A selector refers to a specific field handler in the Handlers object - * (for example: the STARTSUBMSG handler for field "field15"). */ -typedef int32_t upb_selector_t; +#undef SET_TYPE -UPB_BEGIN_EXTERN_C -/* Forward-declares for C inline accessors. We need to declare these here - * so we can "friend" them in the class declarations in C++. */ -UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h, - upb_selector_t s); -UPB_INLINE const void *upb_handlerattr_handlerdata(const upb_handlerattr *attr); -UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h, - upb_selector_t s); +/* upb_tabkey *****************************************************************/ -UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h); -UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj, - const void *type); -UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf, - size_t ofs); -UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h); -UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h); -UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h); +/* Either: + * 1. an actual integer key, or + * 2. a pointer to a string prefixed by its uint32_t length, owned by us. + * + * ...depending on whether this is a string table or an int table. We would + * make this a union of those two types, but C89 doesn't support statically + * initializing a non-first union member. */ +typedef uintptr_t upb_tabkey; -UPB_END_EXTERN_C +UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) { + char* mem = (char*)key; + if (len) memcpy(len, mem, sizeof(*len)); + return mem + sizeof(*len); +} -/* Static selectors for upb::Handlers. */ -#define UPB_STARTMSG_SELECTOR 0 -#define UPB_ENDMSG_SELECTOR 1 -#define UPB_UNKNOWN_SELECTOR 2 -#define UPB_STATIC_SELECTOR_COUNT 3 +/* upb_tabval *****************************************************************/ -/* Static selectors for upb::BytesHandler. */ -#define UPB_STARTSTR_SELECTOR 0 -#define UPB_STRING_SELECTOR 1 -#define UPB_ENDSTR_SELECTOR 2 +typedef struct { + uint64_t val; +} upb_tabval; -typedef void upb_handlerfree(void *d); +#define UPB_TABVALUE_EMPTY_INIT {-1} -#ifdef __cplusplus -/* A set of attributes that accompanies a handler's function pointer. */ -class upb::HandlerAttributes { - public: - HandlerAttributes(); - ~HandlerAttributes(); - - /* Sets the handler data that will be passed as the second parameter of the - * handler. To free this pointer when the handlers are freed, call - * Handlers::AddCleanup(). */ - bool SetHandlerData(const void *handler_data); - const void* handler_data() const; - - /* Use this to specify the type of the closure. This will be checked against - * all other closure types for handler that use the same closure. - * Registration will fail if this does not match all other non-NULL closure - * types. */ - bool SetClosureType(const void *closure_type); - const void* closure_type() const; - - /* Use this to specify the type of the returned closure. Only used for - * Start*{String,SubMessage,Sequence} handlers. This must match the closure - * type of any handlers that use it (for example, the StringBuf handler must - * match the closure returned from StartString). */ - bool SetReturnClosureType(const void *return_closure_type); - const void* return_closure_type() const; - - /* Set to indicate that the handler always returns "ok" (either "true" or a - * non-NULL closure). This is a hint that can allow code generators to - * generate more efficient code. */ - bool SetAlwaysOk(bool always_ok); - bool always_ok() const; +/* upb_table ******************************************************************/ - private: - friend UPB_INLINE const void * ::upb_handlerattr_handlerdata( - const upb_handlerattr *attr); -#else -struct upb_handlerattr { -#endif - const void *handler_data_; - const void *closure_type_; - const void *return_closure_type_; - bool alwaysok_; -}; +typedef struct _upb_tabent { + upb_tabkey key; + upb_tabval val; -#define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false} + /* 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_func *func; + size_t count; /* Number of entries in the hash part. */ + size_t mask; /* Mask to turn hash value -> bucket. */ + upb_ctype_t ctype; /* Type of all values. */ + uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */ - /* It is wasteful to include the entire attributes here: - * - * * Some of the information is redundant (like storing the closure type - * separately for each handler that must match). - * * Some of the info is only needed prior to freeze() (like closure types). - * * alignment padding wastes a lot of space for alwaysok_. - * - * If/when the size and locality of handlers is an issue, we can optimize this - * not to store the entire attr like this. We do not expose the table's - * layout to allow this optimization in the future. */ - upb_handlerattr attr; -} upb_handlers_tabent; + /* Hash table entries. + * Making this const isn't entirely accurate; what we really want is for it to + * have the same const-ness as the table it's inside. But there's no way to + * declare that in C. So we have to make it const so that we can statically + * initialize const hash tables. Then we cast away const when we have to. + */ + const upb_tabent *entries; -#ifdef __cplusplus +#ifndef NDEBUG + /* This table's allocator. We make the user pass it in to every relevant + * function and only use this to check it in debug mode. We do this solely + * to keep upb_table as small as possible. This might seem slightly paranoid + * but the plan is to use upb_table for all map fields and extension sets in + * a forthcoming message representation, so there could be a lot of these. + * If this turns out to be too annoying later, we can change it (since this + * is an internal-only header file). */ + upb_alloc *alloc; +#endif +} upb_table; -/* Extra information about a buffer that is passed to a StringBuf handler. - * TODO(haberman): allow the handle to be pinned so that it will outlive - * the handler invocation. */ -class upb::BufferHandle { - public: - BufferHandle(); - ~BufferHandle(); +typedef struct { + upb_table t; +} upb_strtable; - /* The beginning of the buffer. This may be different than the pointer - * passed to a StringBuf handler because the handler may receive data - * that is from the middle or end of a larger buffer. */ - const char* buffer() const; +typedef struct { + upb_table t; /* For entries that don't fit in the array part. */ + const upb_tabval *array; /* Array part of the table. See const note above. */ + size_t array_size; /* Array part size. */ + size_t array_count; /* Array part number of elements. */ +} upb_inttable; - /* The offset within the attached object where this buffer begins. Only - * meaningful if there is an attached object. */ - size_t object_offset() const; +#define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \ + {UPB_TABLE_INIT(count, mask, ctype, size_lg2, ent), a, asize, acount} - /* Note that object_offset is the offset of "buf" within the attached - * object. */ - void SetBuffer(const char* buf, size_t object_offset); +#define UPB_EMPTY_INTTABLE_INIT(ctype) \ + UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0) - /* The BufferHandle can have an "attached object", which can be used to - * tunnel through a pointer to the buffer's underlying representation. */ - template - void SetAttachedObject(const T* obj); +#define UPB_ARRAY_EMPTYENT -1 - /* Returns NULL if the attached object is not of this type. */ - template - const T* GetAttachedObject() const; +UPB_INLINE size_t upb_table_size(const upb_table *t) { + if (t->size_lg2 == 0) + return 0; + else + return 1 << t->size_lg2; +} - private: - friend UPB_INLINE void ::upb_bufhandle_init(upb_bufhandle *h); - friend UPB_INLINE void ::upb_bufhandle_setobj(upb_bufhandle *h, - const void *obj, - const void *type); - friend UPB_INLINE void ::upb_bufhandle_setbuf(upb_bufhandle *h, - const char *buf, size_t ofs); - friend UPB_INLINE const void* ::upb_bufhandle_obj(const upb_bufhandle *h); - friend UPB_INLINE const void* ::upb_bufhandle_objtype( - const upb_bufhandle *h); - friend UPB_INLINE const char* ::upb_bufhandle_buf(const upb_bufhandle *h); -#else -struct upb_bufhandle { -#endif - const char *buf_; - const void *obj_; - const void *objtype_; - size_t objofs_; -}; +/* Internal-only functions, in .h file only out of necessity. */ +UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) { + return e->key == 0; +} -#ifdef __cplusplus +/* Used by some of the unit tests for generic hashing functionality. */ +uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed); -/* A upb::Handlers object represents the set of handlers associated with a - * message in the graph of messages. You can think of it as a big virtual - * table with functions corresponding to all the events that can fire while - * parsing or visiting a message of a specific type. - * - * Any handlers that are not set behave as if they had successfully consumed - * the value. Any unset Start* handlers will propagate their closure to the - * inner frame. - * - * The easiest way to create the *Handler objects needed by the Set* methods is - * with the UpbBind() and UpbMakeHandler() macros; see below. */ -class upb::Handlers { - public: - typedef upb_selector_t Selector; - typedef upb_handlertype_t Type; +UPB_INLINE uintptr_t upb_intkey(uintptr_t key) { + return key; +} - typedef Handler StartFieldHandler; - typedef Handler EndFieldHandler; - typedef Handler StartMessageHandler; - typedef Handler EndMessageHandler; - typedef Handler StartStringHandler; - typedef Handler StringHandler; +UPB_INLINE uint32_t upb_inthash(uintptr_t key) { + return (uint32_t)key; +} - template struct ValueHandler { - typedef Handler H; - }; +static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) { + return t->entries + (hash & t->mask); +} - typedef ValueHandler::H Int32Handler; - typedef ValueHandler::H Int64Handler; - typedef ValueHandler::H UInt32Handler; - typedef ValueHandler::H UInt64Handler; - typedef ValueHandler::H FloatHandler; - typedef ValueHandler::H DoubleHandler; - typedef ValueHandler::H BoolHandler; +UPB_INLINE bool upb_arrhas(upb_tabval key) { + return key.val != (uint64_t)-1; +} - /* Any function pointer can be converted to this and converted back to its - * correct type. */ - typedef void GenericFunction(); +/* Initialize and uninitialize a table, respectively. If memory allocation + * failed, false is returned that the table is uninitialized. */ +bool upb_inttable_init2(upb_inttable *table, upb_ctype_t ctype, upb_alloc *a); +bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype, upb_alloc *a); +void upb_inttable_uninit2(upb_inttable *table, upb_alloc *a); +void upb_strtable_uninit2(upb_strtable *table, upb_alloc *a); - typedef void HandlersCallback(const void *closure, upb_handlers *h); +UPB_INLINE bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype) { + return upb_inttable_init2(table, ctype, &upb_alloc_global); +} - /* Returns a new handlers object for the given frozen msgdef. - * Returns NULL if memory allocation failed. */ - static reffed_ptr New(const MessageDef *m); - - /* Convenience function for registering a graph of handlers that mirrors the - * graph of msgdefs for some message. For "m" and all its children a new set - * of handlers will be created and the given callback will be invoked, - * allowing the client to register handlers for this message. Note that any - * subhandlers set by the callback will be overwritten. */ - static reffed_ptr NewFrozen(const MessageDef *m, - HandlersCallback *callback, - const void *closure); - - /* Functionality from upb::RefCounted. */ - UPB_REFCOUNTED_CPPMETHODS - - /* All handler registration functions return bool to indicate success or - * failure; details about failures are stored in this status object. If a - * failure does occur, it must be cleared before the Handlers are frozen, - * otherwise the freeze() operation will fail. The functions may *only* be - * used while the Handlers are mutable. */ - const Status* status(); - void ClearError(); - - /* Call to freeze these Handlers. Requires that any SubHandlers are already - * frozen. For cycles, you must use the static version below and freeze the - * whole graph at once. */ - bool Freeze(Status* s); - - /* Freezes the given set of handlers. You may not freeze a handler without - * also freezing any handlers they point to. */ - static bool Freeze(Handlers*const* handlers, int n, Status* s); - static bool Freeze(const std::vector& handlers, Status* s); +UPB_INLINE bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype) { + return upb_strtable_init2(table, ctype, &upb_alloc_global); +} - /* Returns the msgdef associated with this handlers object. */ - const MessageDef* message_def() const; +UPB_INLINE void upb_inttable_uninit(upb_inttable *table) { + upb_inttable_uninit2(table, &upb_alloc_global); +} - /* Adds the given pointer and function to the list of cleanup functions that - * will be run when these handlers are freed. If this pointer has previously - * been registered, the function returns false and does nothing. */ - bool AddCleanup(void *ptr, upb_handlerfree *cleanup); +UPB_INLINE void upb_strtable_uninit(upb_strtable *table) { + upb_strtable_uninit2(table, &upb_alloc_global); +} - /* Sets the startmsg handler for the message, which is defined as follows: - * - * bool startmsg(MyType* closure) { - * // Called when the message begins. Returns true if processing should - * // continue. - * return true; - * } - */ - bool SetStartMessageHandler(const StartMessageHandler& handler); +/* Returns the number of values in the table. */ +size_t upb_inttable_count(const upb_inttable *t); +UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) { + return t->t.count; +} - /* Sets the endmsg handler for the message, which is defined as follows: - * - * bool endmsg(MyType* closure, upb_status *status) { - * // Called when processing of this message ends, whether in success or - * // failure. "status" indicates the final status of processing, and - * // can also be modified in-place to update the final status. - * } - */ - bool SetEndMessageHandler(const EndMessageHandler& handler); +void upb_inttable_packedsize(const upb_inttable *t, size_t *size); +void upb_strtable_packedsize(const upb_strtable *t, size_t *size); +upb_inttable *upb_inttable_pack(const upb_inttable *t, void *p, size_t *ofs, + size_t size); +upb_strtable *upb_strtable_pack(const upb_strtable *t, void *p, size_t *ofs, + size_t size); - /* Sets the value handler for the given field, which is defined as follows - * (this is for an int32 field; other field types will pass their native - * C/C++ type for "val"): - * - * bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) { - * // Called when the field's value is encountered. "d" contains - * // whatever data was bound to this field when it was registered. - * // Returns true if processing should continue. - * return true; - * } - * - * handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...))); - * - * The value type must exactly match f->type(). - * For example, a handler that takes an int32_t parameter may only be used for - * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM. - * - * Returns false if the handler failed to register; in this case the cleanup - * handler (if any) will be called immediately. - */ - bool SetInt32Handler (const FieldDef* f, const Int32Handler& h); - bool SetInt64Handler (const FieldDef* f, const Int64Handler& h); - bool SetUInt32Handler(const FieldDef* f, const UInt32Handler& h); - bool SetUInt64Handler(const FieldDef* f, const UInt64Handler& h); - bool SetFloatHandler (const FieldDef* f, const FloatHandler& h); - bool SetDoubleHandler(const FieldDef* f, const DoubleHandler& h); - bool SetBoolHandler (const FieldDef* f, const BoolHandler& h); +/* Inserts the given key into the hashtable with the given value. The key must + * not already exist in the hash table. For string tables, the key must be + * NULL-terminated, and the table will make an internal copy of the key. + * Inttables must not insert a value of UINTPTR_MAX. + * + * If a table resize was required but memory allocation failed, false is + * returned and the table is unchanged. */ +bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val, + upb_alloc *a); +bool upb_strtable_insert3(upb_strtable *t, const char *key, size_t len, + upb_value val, upb_alloc *a); - /* Like the previous, but templated on the type on the value (ie. int32). - * This is mostly useful to call from other templates. To call this you must - * specify the template parameter explicitly, ie: - * h->SetValueHandler(f, UpbBind(MyHandler, MyData)); */ - template - bool SetValueHandler( - const FieldDef *f, - const typename ValueHandler::Type>::H& handler); +UPB_INLINE bool upb_inttable_insert(upb_inttable *t, uintptr_t key, + upb_value val) { + return upb_inttable_insert2(t, key, val, &upb_alloc_global); +} - /* Sets handlers for a string field, which are defined as follows: - * - * MySubClosure* startstr(MyClosure* c, const MyHandlerData* d, - * size_t size_hint) { - * // Called when a string value begins. The return value indicates the - * // closure for the string. "size_hint" indicates the size of the - * // string if it is known, however if the string is length-delimited - * // and the end-of-string is not available size_hint will be zero. - * // This case is indistinguishable from the case where the size is - * // known to be zero. - * // - * // TODO(haberman): is it important to distinguish these cases? - * // If we had ssize_t as a type we could make -1 "unknown", but - * // ssize_t is POSIX (not ANSI) and therefore less portable. - * // In practice I suspect it won't be important to distinguish. - * return closure; - * } - * - * size_t str(MyClosure* closure, const MyHandlerData* d, - * const char *str, size_t len) { - * // Called for each buffer of string data; the multiple physical buffers - * // are all part of the same logical string. The return value indicates - * // how many bytes were consumed. If this number is less than "len", - * // this will also indicate that processing should be halted for now, - * // like returning false or UPB_BREAK from any other callback. If - * // number is greater than "len", the excess bytes will be skipped over - * // and not passed to the callback. - * return len; - * } - * - * bool endstr(MyClosure* c, const MyHandlerData* d) { - * // Called when a string value ends. Return value indicates whether - * // processing should continue. - * return true; - * } - */ - bool SetStartStringHandler(const FieldDef* f, const StartStringHandler& h); - bool SetStringHandler(const FieldDef* f, const StringHandler& h); - bool SetEndStringHandler(const FieldDef* f, const EndFieldHandler& h); +UPB_INLINE bool upb_strtable_insert2(upb_strtable *t, const char *key, + size_t len, upb_value val) { + return upb_strtable_insert3(t, key, len, val, &upb_alloc_global); +} - /* Sets the startseq handler, which is defined as follows: - * - * MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) { - * // Called when a sequence (repeated field) begins. The returned - * // pointer indicates the closure for the sequence (or UPB_BREAK - * // to interrupt processing). - * return closure; - * } - * - * h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...))); - * - * Returns "false" if "f" does not belong to this message or is not a - * repeated field. - */ - bool SetStartSequenceHandler(const FieldDef* f, const StartFieldHandler& h); +/* For NULL-terminated strings. */ +UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key, + upb_value val) { + return upb_strtable_insert2(t, key, strlen(key), val); +} - /* Sets the startsubmsg handler for the given field, which is defined as - * follows: - * - * MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) { - * // Called when a submessage begins. The returned pointer indicates the - * // closure for the sequence (or UPB_BREAK to interrupt processing). - * return closure; - * } - * - * h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, - * new MyHandlerData(...))); - * - * Returns "false" if "f" does not belong to this message or is not a - * submessage/group field. - */ - bool SetStartSubMessageHandler(const FieldDef* f, const StartFieldHandler& h); +/* 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); +bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len, + upb_value *v); - /* Sets the endsubmsg handler for the given field, which is defined as - * follows: - * - * bool endsubmsg(MyClosure* c, const MyHandlerData* d) { - * // Called when a submessage ends. Returns true to continue processing. - * return true; - * } - * - * Returns "false" if "f" does not belong to this message or is not a - * submessage/group field. - */ - bool SetEndSubMessageHandler(const FieldDef *f, const EndFieldHandler &h); +/* 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); +} - /* Starts the endsubseq handler for the given field, which is defined as - * follows: - * - * bool endseq(MyClosure* c, const MyHandlerData* d) { - * // Called when a sequence ends. Returns true continue processing. - * return true; - * } - * - * Returns "false" if "f" does not belong to this message or is not a - * repeated field. - */ - bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h); +/* 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); +bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len, + upb_value *val, upb_alloc *alloc); - /* Sets or gets the object that specifies handlers for the given field, which - * must be a submessage or group. Returns NULL if no handlers are set. */ - bool SetSubHandlers(const FieldDef* f, const Handlers* sub); - const Handlers* GetSubHandlers(const FieldDef* f) const; +UPB_INLINE bool upb_strtable_remove2(upb_strtable *t, const char *key, + size_t len, upb_value *val) { + return upb_strtable_remove3(t, key, len, val, &upb_alloc_global); +} - /* Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the - * field. */ - const Handlers* GetSubHandlers(Selector startsubmsg) const; +/* For NULL-terminated strings. */ +UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key, + upb_value *v) { + return upb_strtable_remove2(t, key, strlen(key), v); +} - /* A selector refers to a specific field handler in the Handlers object - * (for example: the STARTSUBMSG handler for field "field15"). - * On success, returns true and stores the selector in "s". - * If the FieldDef or Type are invalid, returns false. - * The returned selector is ONLY valid for Handlers whose MessageDef - * contains this FieldDef. */ - static bool GetSelector(const FieldDef* f, Type type, Selector* s); +/* 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); - /* Given a START selector of any kind, returns the corresponding END selector. */ - static Selector GetEndSelector(Selector start_selector); +/* Handy routines for treating an inttable like a stack. May not be mixed with + * other insert/remove calls. */ +bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a); +upb_value upb_inttable_pop(upb_inttable *t); - /* Returns the function pointer for this handler. It is the client's - * responsibility to cast to the correct function type before calling it. */ - GenericFunction* GetHandler(Selector selector); +UPB_INLINE bool upb_inttable_push(upb_inttable *t, upb_value val) { + return upb_inttable_push2(t, val, &upb_alloc_global); +} - /* Sets the given attributes to the attributes for this selector. */ - bool GetAttributes(Selector selector, HandlerAttributes* attr); +/* Convenience routines for inttables with pointer keys. */ +bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val, + upb_alloc *a); +bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val); +bool upb_inttable_lookupptr( + const upb_inttable *t, const void *key, upb_value *val); - /* Returns the handler data that was registered with this handler. */ - const void* GetHandlerData(Selector selector); +UPB_INLINE bool upb_inttable_insertptr(upb_inttable *t, const void *key, + upb_value val) { + return upb_inttable_insertptr2(t, key, val, &upb_alloc_global); +} - /* Could add any of the following functions as-needed, with some minor - * implementation changes: - * - * const FieldDef* GetFieldDef(Selector selector); - * static bool IsSequence(Selector selector); */ +/* 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. */ +void upb_inttable_compact2(upb_inttable *t, upb_alloc *a); - private: - UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers) +UPB_INLINE void upb_inttable_compact(upb_inttable *t) { + upb_inttable_compact2(t, &upb_alloc_global); +} - friend UPB_INLINE GenericFunction *::upb_handlers_gethandler( - const upb_handlers *h, upb_selector_t s); - friend UPB_INLINE const void *::upb_handlers_gethandlerdata( - const upb_handlers *h, upb_selector_t s); -#else -struct upb_handlers { -#endif - upb_refcounted base; - - const upb_msgdef *msg; - const upb_handlers **sub; - const void *top_closure_type; - upb_inttable cleanup_; - upb_status status_; /* Used only when mutable. */ - upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */ -}; +/* A special-case inlinable version of the lookup routine for 32-bit + * integers. */ +UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key, + upb_value *v) { + *v = upb_value_int32(0); /* Silence compiler warnings. */ + if (key < t->array_size) { + upb_tabval arrval = t->array[key]; + if (upb_arrhas(arrval)) { + _upb_value_setval(v, arrval.val, t->t.ctype); + return true; + } else { + return false; + } + } else { + const upb_tabent *e; + if (t->t.entries == NULL) return false; + for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) { + if ((uint32_t)e->key == key) { + _upb_value_setval(v, e->val.val, t->t.ctype); + return true; + } + if (e->next == NULL) return false; + } + } +} -#ifdef __cplusplus +/* Exposed for testing only. */ +bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a); -namespace upb { +/* Iterators ******************************************************************/ -/* Convenience macros for creating a Handler object that is wrapped with a - * type-safe wrapper function that converts the "void*" parameters/returns - * of the underlying C API into nice C++ function. - * - * Sample usage: - * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) { - * // do stuff ... - * } +/* Iterators for int and string tables. We are subject to some kind of unusual + * design constraints: * - * // Handler that doesn't need any data bound to it. - * void OnValue2(MyClosure* c, int32_t val) { - * // do stuff ... - * } + * 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. * - * // Handler that returns bool so it can return failure if necessary. - * bool OnValue3(MyClosure* c, int32_t val) { - * // do stuff ... - * return ok; - * } + * For C++11 range-based for: + * - iterators must be copyable + * - iterators must be comparable + * - it must be possible to construct an "end" value. * - * // Member function handler. - * class MyClosure { - * public: - * void OnValue(int32_t val) { - * // do stuff ... - * } - * }; + * Iteration order is undefined. * - * // Takes ownership of the MyHandlerData. - * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...))); - * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2)); - * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3)); - * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue)); - */ - -#ifdef UPB_CXX11 - -/* In C++11, the "template" disambiguator can appear even outside templates, - * so all calls can safely use this pair of macros. */ + * 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). */ -#define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc() -/* We have to be careful to only evaluate "d" once. */ -#define UpbBind(f, d) upb::MatchFunc(f).template GetFunc((d)) +/* upb_strtable_iter **********************************************************/ -#else +/* 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); + * // ... + * } + */ -/* Prior to C++11, the "template" disambiguator may only appear inside a - * template, so the regular macro must not use "template" */ +typedef struct { + const upb_strtable *t; + size_t index; +} upb_strtable_iter; -#define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc() +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); +const char *upb_strtable_iter_key(const upb_strtable_iter *i); +size_t upb_strtable_iter_keylength(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); -#define UpbBind(f, d) upb::MatchFunc(f).GetFunc((d)) -#endif /* UPB_CXX11 */ +/* upb_inttable_iter **********************************************************/ -/* This macro must be used in C++98 for calls from inside a template. But we - * define this variant in all cases; code that wants to be compatible with both - * C++98 and C++11 should always use this macro when calling from a template. */ -#define UpbMakeHandlerT(f) upb::MatchFunc(f).template GetFunc() +/* upb_inttable_iter i; + * upb_inttable_begin(&i, t); + * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + * uintptr_t key = upb_inttable_iter_key(&i); + * upb_value val = upb_inttable_iter_value(&i); + * // ... + * } + */ -/* We have to be careful to only evaluate "d" once. */ -#define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc((d)) +typedef struct { + const upb_inttable *t; + size_t index; + bool array_part; +} upb_inttable_iter; -/* Handler: a struct that contains the (handler, data, deleter) tuple that is - * used to register all handlers. Users can Make() these directly but it's - * more convenient to use the UpbMakeHandler/UpbBind macros above. */ -template class Handler { - public: - /* The underlying, handler function signature that upb uses internally. */ - typedef T FuncPtr; +void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t); +void upb_inttable_next(upb_inttable_iter *i); +bool upb_inttable_done(const upb_inttable_iter *i); +uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i); +upb_value upb_inttable_iter_value(const upb_inttable_iter *i); +void upb_inttable_iter_setdone(upb_inttable_iter *i); +bool upb_inttable_iter_isequal(const upb_inttable_iter *i1, + const upb_inttable_iter *i2); - /* Intentionally implicit. */ - template Handler(F func); - ~Handler(); - private: - void AddCleanup(Handlers* h) const { - if (cleanup_func_) { - bool ok = h->AddCleanup(cleanup_data_, cleanup_func_); - UPB_ASSERT(ok); - } - } +#ifdef __cplusplus +} /* extern "C" */ +#endif - UPB_DISALLOW_COPY_AND_ASSIGN(Handler) - friend class Handlers; - FuncPtr handler_; - mutable HandlerAttributes attr_; - mutable bool registered_; - void *cleanup_data_; - upb_handlerfree *cleanup_func_; -}; +#endif /* UPB_TABLE_H_ */ -} /* namespace upb */ +#ifdef __cplusplus +#include +#include +#include +#include -#endif /* __cplusplus */ +namespace upb { +class EnumDefPtr; +class FieldDefPtr; +class FileDefPtr; +class MessageDefPtr; +class OneofDefPtr; +class SymbolTable; +} +#endif -UPB_BEGIN_EXTERN_C +struct upb_enumdef; +typedef struct upb_enumdef upb_enumdef; +struct upb_fielddef; +typedef struct upb_fielddef upb_fielddef; +struct upb_filedef; +typedef struct upb_filedef upb_filedef; +struct upb_msgdef; +typedef struct upb_msgdef upb_msgdef; +struct upb_oneofdef; +typedef struct upb_oneofdef upb_oneofdef; +struct upb_symtab; +typedef struct upb_symtab upb_symtab; -/* Native C API. */ +typedef enum { + UPB_SYNTAX_PROTO2 = 2, + UPB_SYNTAX_PROTO3 = 3 +} upb_syntax_t; -/* Handler function typedefs. */ -typedef bool upb_unknown_handlerfunc(void *c, const void *hd, const char *buf, - size_t n); -typedef bool upb_startmsg_handlerfunc(void *c, const void*); -typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status); -typedef void* upb_startfield_handlerfunc(void *c, const void *hd); -typedef bool upb_endfield_handlerfunc(void *c, const void *hd); -typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val); -typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val); -typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val); -typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val); -typedef bool upb_float_handlerfunc(void *c, const void *hd, float val); -typedef bool upb_double_handlerfunc(void *c, const void *hd, double val); -typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val); -typedef void *upb_startstr_handlerfunc(void *c, const void *hd, - size_t size_hint); -typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf, - size_t n, const upb_bufhandle* handle); +/* 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 merber of these groups are not changed. + */ +typedef enum { + UPB_WELLKNOWN_UNSPECIFIED, + UPB_WELLKNOWN_ANY, + UPB_WELLKNOWN_FIELDMASK, + UPB_WELLKNOWN_DURATION, + UPB_WELLKNOWN_TIMESTAMP, + /* number wrappers */ + UPB_WELLKNOWN_DOUBLEVALUE, + UPB_WELLKNOWN_FLOATVALUE, + UPB_WELLKNOWN_INT64VALUE, + UPB_WELLKNOWN_UINT64VALUE, + UPB_WELLKNOWN_INT32VALUE, + UPB_WELLKNOWN_UINT32VALUE, + /* string wrappers */ + UPB_WELLKNOWN_STRINGVALUE, + UPB_WELLKNOWN_BYTESVALUE, + UPB_WELLKNOWN_BOOLVALUE, + UPB_WELLKNOWN_VALUE, + UPB_WELLKNOWN_LISTVALUE, + UPB_WELLKNOWN_STRUCT +} upb_wellknowntype_t; -/* upb_bufhandle */ -size_t upb_bufhandle_objofs(const upb_bufhandle *h); +/* upb_fielddef ***************************************************************/ -/* upb_handlerattr */ -void upb_handlerattr_init(upb_handlerattr *attr); -void upb_handlerattr_uninit(upb_handlerattr *attr); +/* Maximum field number allowed for FieldDefs. This is an inherent limit of the + * protobuf wire format. */ +#define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) -bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd); -bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type); -const void *upb_handlerattr_closuretype(const upb_handlerattr *attr); -bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr, - const void *type); -const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr); -bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok); -bool upb_handlerattr_alwaysok(const upb_handlerattr *attr); +#ifdef __cplusplus +extern "C" { +#endif -UPB_INLINE const void *upb_handlerattr_handlerdata( - const upb_handlerattr *attr) { - return attr->handler_data_; -} +const char *upb_fielddef_fullname(const upb_fielddef *f); +upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f); +upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f); +upb_label_t upb_fielddef_label(const upb_fielddef *f); +uint32_t upb_fielddef_number(const upb_fielddef *f); +const char *upb_fielddef_name(const upb_fielddef *f); +bool upb_fielddef_isextension(const upb_fielddef *f); +bool upb_fielddef_lazy(const upb_fielddef *f); +bool upb_fielddef_packed(const upb_fielddef *f); +size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len); +const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f); +const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f); +uint32_t upb_fielddef_index(const upb_fielddef *f); +bool upb_fielddef_issubmsg(const upb_fielddef *f); +bool upb_fielddef_isstring(const upb_fielddef *f); +bool upb_fielddef_isseq(const upb_fielddef *f); +bool upb_fielddef_isprimitive(const upb_fielddef *f); +bool upb_fielddef_ismap(const upb_fielddef *f); +int64_t upb_fielddef_defaultint64(const upb_fielddef *f); +int32_t upb_fielddef_defaultint32(const upb_fielddef *f); +uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f); +uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f); +bool upb_fielddef_defaultbool(const upb_fielddef *f); +float upb_fielddef_defaultfloat(const upb_fielddef *f); +double upb_fielddef_defaultdouble(const upb_fielddef *f); +const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len); +bool upb_fielddef_hassubdef(const upb_fielddef *f); +bool upb_fielddef_haspresence(const upb_fielddef *f); +const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f); +const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); -/* upb_handlers */ -typedef void upb_handlers_callback(const void *closure, upb_handlers *h); -upb_handlers *upb_handlers_new(const upb_msgdef *m, - const void *owner); -const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m, - const void *owner, - upb_handlers_callback *callback, - const void *closure); +/* Internal only. */ +uint32_t upb_fielddef_selectorbase(const upb_fielddef *f); -/* Include refcounted methods like upb_handlers_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_handlers, upb_handlers_upcast) +#ifdef __cplusplus +} /* extern "C" */ -const upb_status *upb_handlers_status(upb_handlers *h); -void upb_handlers_clearerr(upb_handlers *h); -const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h); -bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree); -bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func, - upb_handlerattr *attr); +/* A upb_fielddef describes a single field in a message. It is most often + * found as a part of a upb_msgdef, but can also stand alone to represent + * an extension. */ +class upb::FieldDefPtr { + public: + FieldDefPtr() : ptr_(nullptr) {} + explicit FieldDefPtr(const upb_fielddef *ptr) : ptr_(ptr) {} -bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f, - upb_int32_handlerfunc *func, upb_handlerattr *attr); -bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f, - upb_int64_handlerfunc *func, upb_handlerattr *attr); -bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f, - upb_uint32_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f, - upb_uint64_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f, - upb_float_handlerfunc *func, upb_handlerattr *attr); -bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f, - upb_double_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f, - upb_bool_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f, - upb_startstr_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f, - upb_string_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f, - upb_endfield_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f, - upb_startfield_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f, - upb_startfield_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f, - upb_endfield_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f, - upb_endfield_handlerfunc *func, - upb_handlerattr *attr); + const upb_fielddef* ptr() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } -bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f, - const upb_handlers *sub); -const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h, - const upb_fielddef *f); -const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, - upb_selector_t sel); + typedef upb_fieldtype_t Type; + typedef upb_label_t Label; + typedef upb_descriptortype_t DescriptorType; -UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h, - upb_selector_t s) { - return (upb_func *)h->table[s].func; -} + const char* full_name() const { return upb_fielddef_fullname(ptr_); } -bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s, - upb_handlerattr *attr); + Type type() const { return upb_fielddef_type(ptr_); } + Label label() const { return upb_fielddef_label(ptr_); } + const char* name() const { return upb_fielddef_name(ptr_); } + uint32_t number() const { return upb_fielddef_number(ptr_); } + bool is_extension() const { return upb_fielddef_isextension(ptr_); } -UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h, - upb_selector_t s) { - return upb_handlerattr_handlerdata(&h->table[s].attr); -} + /* Copies the JSON name for this field into the given buffer. Returns the + * actual size of the JSON name, including the NULL terminator. If the + * return value is 0, the JSON name is unset. If the return value is + * greater than len, the JSON name was truncated. The buffer is always + * NULL-terminated if len > 0. + * + * The JSON name always defaults to a camelCased version of the regular + * name. However if the regular name is unset, the JSON name will be unset + * also. + */ + size_t GetJsonName(char *buf, size_t len) const { + return upb_fielddef_getjsonname(ptr_, buf, len); + } -#ifdef __cplusplus + /* Convenience version of the above function which copies the JSON name + * into the given string, returning false if the name is not set. */ + template + bool GetJsonName(T* str) { + str->resize(GetJsonName(NULL, 0)); + GetJsonName(&(*str)[0], str->size()); + return str->size() > 0; + } -/* Handler types for single fields. - * Right now we only have one for TYPE_BYTES but ones for other types - * should follow. - * - * These follow the same handlers protocol for fields of a message. */ -class upb::BytesHandler { - public: - BytesHandler(); - ~BytesHandler(); -#else -struct upb_byteshandler { -#endif - upb_handlers_tabent table[3]; -}; + /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false, + * indicates whether this field should have lazy parsing handlers that yield + * the unparsed string for the submessage. + * + * TODO(haberman): I think we want to move this into a FieldOptions container + * when we add support for custom options (the FieldOptions struct will + * contain both regular FieldOptions like "lazy" *and* custom options). */ + bool lazy() const { return upb_fielddef_lazy(ptr_); } -void upb_byteshandler_init(upb_byteshandler *h); + /* For non-string, non-submessage fields, this indicates whether binary + * protobufs are encoded in packed or non-packed format. + * + * TODO(haberman): see note above about putting options like this into a + * FieldOptions container. */ + bool packed() const { return upb_fielddef_packed(ptr_); } -/* Caller must ensure that "d" outlives the handlers. - * TODO(haberman): should this have a "freeze" operation? It's not necessary - * for memory management, but could be useful to force immutability and provide - * a convenient moment to verify that all registration succeeded. */ -bool upb_byteshandler_setstartstr(upb_byteshandler *h, - upb_startstr_handlerfunc *func, void *d); -bool upb_byteshandler_setstring(upb_byteshandler *h, - upb_string_handlerfunc *func, void *d); -bool upb_byteshandler_setendstr(upb_byteshandler *h, - upb_endfield_handlerfunc *func, void *d); + /* An integer that can be used as an index into an array of fields for + * whatever message this field belongs to. Guaranteed to be less than + * f->containing_type()->field_count(). May only be accessed once the def has + * been finalized. */ + uint32_t index() const { return upb_fielddef_index(ptr_); } -/* "Static" methods */ -bool upb_handlers_freeze(upb_handlers *const *handlers, int n, upb_status *s); -upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f); -bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, - upb_selector_t *s); -UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) { - return start + 1; -} + /* The MessageDef to which this field belongs. + * + * If this field has been added to a MessageDef, that message can be retrieved + * directly (this is always the case for frozen FieldDefs). + * + * If the field has not yet been added to a MessageDef, you can set the name + * of the containing type symbolically instead. This is mostly useful for + * extensions, where the extension is declared separately from the message. */ + MessageDefPtr containing_type() const; -/* Internal-only. */ -uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f); -uint32_t upb_handlers_selectorcount(const upb_fielddef *f); + /* The OneofDef to which this field belongs, or NULL if this field is not part + * of a oneof. */ + OneofDefPtr containing_oneof() const; -UPB_END_EXTERN_C + /* The field's type according to the enum in descriptor.proto. This is not + * the same as UPB_TYPE_*, because it distinguishes between (for example) + * INT32 and SINT32, whereas our "type" enum does not. This return of + * descriptor_type() is a function of type(), integer_format(), and + * is_tag_delimited(). */ + DescriptorType descriptor_type() const { + return upb_fielddef_descriptortype(ptr_); + } -/* -** Inline definitions for handlers.h, which are particularly long and a bit -** tricky. -*/ + /* Convenient field type tests. */ + bool IsSubMessage() const { return upb_fielddef_issubmsg(ptr_); } + bool IsString() const { return upb_fielddef_isstring(ptr_); } + bool IsSequence() const { return upb_fielddef_isseq(ptr_); } + bool IsPrimitive() const { return upb_fielddef_isprimitive(ptr_); } + bool IsMap() const { return upb_fielddef_ismap(ptr_); } -#ifndef UPB_HANDLERS_INL_H_ -#define UPB_HANDLERS_INL_H_ + /* Returns the non-string default value for this fielddef, which may either + * be something the client set explicitly or the "default default" (0 for + * numbers, empty for strings). The field's type indicates the type of the + * returned value, except for enum fields that are still mutable. + * + * Requires that the given function matches the field's current type. */ + int64_t default_int64() const { return upb_fielddef_defaultint64(ptr_); } + int32_t default_int32() const { return upb_fielddef_defaultint32(ptr_); } + uint64_t default_uint64() const { return upb_fielddef_defaultuint64(ptr_); } + uint32_t default_uint32() const { return upb_fielddef_defaultuint32(ptr_); } + bool default_bool() const { return upb_fielddef_defaultbool(ptr_); } + float default_float() const { return upb_fielddef_defaultfloat(ptr_); } + double default_double() const { return upb_fielddef_defaultdouble(ptr_); } -#include + /* The resulting string is always NULL-terminated. If non-NULL, the length + * will be stored in *len. */ + const char *default_string(size_t * len) const { + return upb_fielddef_defaultstr(ptr_, len); + } -/* C inline methods. */ + /* Returns the enum or submessage def for this field, if any. The field's + * type must match (ie. you may only call enum_subdef() for fields where + * type() == UPB_TYPE_ENUM). */ + EnumDefPtr enum_subdef() const; + MessageDefPtr message_subdef() const; -/* upb_bufhandle */ -UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h) { - h->obj_ = NULL; - h->objtype_ = NULL; - h->buf_ = NULL; - h->objofs_ = 0; -} -UPB_INLINE void upb_bufhandle_uninit(upb_bufhandle *h) { - UPB_UNUSED(h); -} -UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj, - const void *type) { - h->obj_ = obj; - h->objtype_ = type; -} -UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf, - size_t ofs) { - h->buf_ = buf; - h->objofs_ = ofs; -} -UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h) { - return h->obj_; -} -UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h) { - return h->objtype_; -} -UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h) { - return h->buf_; -} + private: + const upb_fielddef *ptr_; +}; + +#endif /* __cplusplus */ +/* upb_oneofdef ***************************************************************/ #ifdef __cplusplus +extern "C" { +#endif -/* Type detection and typedefs for integer types. - * For platforms where there are multiple 32-bit or 64-bit types, we need to be - * able to enumerate them so we can properly create overloads for all variants. - * - * If any platform existed where there were three integer types with the same - * size, this would have to become more complicated. For example, short, int, - * and long could all be 32-bits. Even more diabolically, short, int, long, - * and long long could all be 64 bits and still be standard-compliant. - * However, few platforms are this strange, and it's unlikely that upb will be - * used on the strangest ones. */ +typedef upb_inttable_iter upb_oneof_iter; -/* Can't count on stdint.h limits like INT32_MAX, because in C++ these are - * only defined when __STDC_LIMIT_MACROS are defined before the *first* include - * of stdint.h. We can't guarantee that someone else didn't include these first - * without defining __STDC_LIMIT_MACROS. */ -#define UPB_INT32_MAX 0x7fffffffLL -#define UPB_INT32_MIN (-UPB_INT32_MAX - 1) -#define UPB_INT64_MAX 0x7fffffffffffffffLL -#define UPB_INT64_MIN (-UPB_INT64_MAX - 1) +const char *upb_oneofdef_name(const upb_oneofdef *o); +const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o); +int upb_oneofdef_numfields(const upb_oneofdef *o); +uint32_t upb_oneofdef_index(const upb_oneofdef *o); -#if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN -#define UPB_INT_IS_32BITS 1 -#endif +/* Oneof lookups: + * - ntof: look up a field by name. + * - ntofz: look up a field by name (as a null-terminated string). + * - itof: look up a field by number. */ +const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, + const char *name, size_t length); +UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o, + const char *name) { + return upb_oneofdef_ntof(o, name, strlen(name)); +} +const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num); -#if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN -#define UPB_LONG_IS_32BITS 1 -#endif +/* upb_oneof_iter i; + * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) { + * // ... + * } + */ +void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o); +void upb_oneof_next(upb_oneof_iter *iter); +bool upb_oneof_done(upb_oneof_iter *iter); +upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter); +void upb_oneof_iter_setdone(upb_oneof_iter *iter); +bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1, + const upb_oneof_iter *iter2); -#if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN -#define UPB_LONG_IS_64BITS 1 -#endif +#ifdef __cplusplus +} /* extern "C" */ -#if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN -#define UPB_LLONG_IS_64BITS 1 -#endif +/* Class that represents a oneof. */ +class upb::OneofDefPtr { + public: + OneofDefPtr() : ptr_(nullptr) {} + explicit OneofDefPtr(const upb_oneofdef *ptr) : ptr_(ptr) {} -/* We use macros instead of typedefs so we can undefine them later and avoid - * leaking them outside this header file. */ -#if UPB_INT_IS_32BITS -#define UPB_INT32_T int -#define UPB_UINT32_T unsigned int + const upb_oneofdef* ptr() const { return ptr_; } + explicit operator bool() { return ptr_ != nullptr; } -#if UPB_LONG_IS_32BITS -#define UPB_TWO_32BIT_TYPES 1 -#define UPB_INT32ALT_T long -#define UPB_UINT32ALT_T unsigned long -#endif /* UPB_LONG_IS_32BITS */ + /* Returns the MessageDef that owns this OneofDef. */ + MessageDefPtr containing_type() const; -#elif UPB_LONG_IS_32BITS /* && !UPB_INT_IS_32BITS */ -#define UPB_INT32_T long -#define UPB_UINT32_T unsigned long -#endif /* UPB_INT_IS_32BITS */ + /* Returns the name of this oneof. This is the name used to look up the oneof + * by name once added to a message def. */ + const char* name() const { return upb_oneofdef_name(ptr_); } + /* Returns the number of fields currently defined in the oneof. */ + int field_count() const { return upb_oneofdef_numfields(ptr_); } -#if UPB_LONG_IS_64BITS -#define UPB_INT64_T long -#define UPB_UINT64_T unsigned long + /* Looks up by name. */ + FieldDefPtr FindFieldByName(const char *name, size_t len) const { + return FieldDefPtr(upb_oneofdef_ntof(ptr_, name, len)); + } + FieldDefPtr FindFieldByName(const char* name) const { + return FieldDefPtr(upb_oneofdef_ntofz(ptr_, name)); + } -#if UPB_LLONG_IS_64BITS -#define UPB_TWO_64BIT_TYPES 1 -#define UPB_INT64ALT_T long long -#define UPB_UINT64ALT_T unsigned long long -#endif /* UPB_LLONG_IS_64BITS */ + template + FieldDefPtr FindFieldByName(const T& str) const { + return FindFieldByName(str.c_str(), str.size()); + } -#elif UPB_LLONG_IS_64BITS /* && !UPB_LONG_IS_64BITS */ -#define UPB_INT64_T long long -#define UPB_UINT64_T unsigned long long -#endif /* UPB_LONG_IS_64BITS */ + /* Looks up by tag number. */ + FieldDefPtr FindFieldByNumber(uint32_t num) const { + return FieldDefPtr(upb_oneofdef_itof(ptr_, num)); + } -#undef UPB_INT32_MAX -#undef UPB_INT32_MIN -#undef UPB_INT64_MAX -#undef UPB_INT64_MIN -#undef UPB_INT_IS_32BITS -#undef UPB_LONG_IS_32BITS -#undef UPB_LONG_IS_64BITS -#undef UPB_LLONG_IS_64BITS + class const_iterator + : public std::iterator { + public: + void operator++() { upb_oneof_next(&iter_); } + FieldDefPtr operator*() const { + return FieldDefPtr(upb_oneof_iter_field(&iter_)); + } -namespace upb { + bool operator!=(const const_iterator& other) const { + return !upb_oneof_iter_isequal(&iter_, &other.iter_); + } -typedef void CleanupFunc(void *ptr); + bool operator==(const const_iterator& other) const { + return upb_oneof_iter_isequal(&iter_, &other.iter_); + } -/* Template to remove "const" from "const T*" and just return "T*". - * - * We define a nonsense default because otherwise it will fail to instantiate as - * a function parameter type even in cases where we don't expect any caller to - * actually match the overload. */ -class CouldntRemoveConst {}; -template struct remove_constptr { typedef CouldntRemoveConst type; }; -template struct remove_constptr { typedef T *type; }; + private: + friend class OneofDefPtr; -/* Template that we use below to remove a template specialization from - * consideration if it matches a specific type. */ -template struct disable_if_same { typedef void Type; }; -template struct disable_if_same {}; + const_iterator() {} + explicit const_iterator(OneofDefPtr o) { + upb_oneof_begin(&iter_, o.ptr()); + } + static const_iterator end() { + const_iterator iter; + upb_oneof_iter_setdone(&iter.iter_); + return iter; + } -template void DeletePointer(void *p) { delete static_cast(p); } + upb_oneof_iter iter_; + }; -template -struct FirstUnlessVoidOrBool { - typedef T1 value; -}; + const_iterator begin() const { return const_iterator(*this); } + const_iterator end() const { return const_iterator::end(); } -template -struct FirstUnlessVoidOrBool { - typedef T2 value; + private: + const upb_oneofdef *ptr_; }; -template -struct FirstUnlessVoidOrBool { - typedef T2 value; -}; +inline upb::OneofDefPtr upb::FieldDefPtr::containing_oneof() const { + return OneofDefPtr(upb_fielddef_containingoneof(ptr_)); +} -template -struct is_same { - static bool value; -}; +#endif /* __cplusplus */ -template -struct is_same { - static bool value; -}; +/* upb_msgdef *****************************************************************/ -template -bool is_same::value = false; +typedef upb_inttable_iter upb_msg_field_iter; +typedef upb_strtable_iter upb_msg_oneof_iter; -template -bool is_same::value = true; +/* Well-known field tag numbers for map-entry messages. */ +#define UPB_MAPENTRY_KEY 1 +#define UPB_MAPENTRY_VALUE 2 -/* FuncInfo *******************************************************************/ +/* Well-known field tag numbers for Any messages. */ +#define UPB_ANY_TYPE 1 +#define UPB_ANY_VALUE 2 -/* Info about the user's original, pre-wrapped function. */ -template -struct FuncInfo { - /* The type of the closure that the function takes (its first param). */ - typedef C Closure; +/* Well-known field tag numbers for timestamp messages. */ +#define UPB_DURATION_SECONDS 1 +#define UPB_DURATION_NANOS 2 - /* The return type. */ - typedef R Return; -}; +/* Well-known field tag numbers for duration messages. */ +#define UPB_TIMESTAMP_SECONDS 1 +#define UPB_TIMESTAMP_NANOS 2 -/* Func ***********************************************************************/ +#ifdef __cplusplus +extern "C" { +#endif -/* Func1, Func2, Func3: Template classes representing a function and its - * signature. - * - * Since the function is a template parameter, calling the function can be - * inlined at compile-time and does not require a function pointer at runtime. - * These functions are not bound to a handler data so have no data or cleanup - * handler. */ -struct UnboundFunc { - CleanupFunc *GetCleanup() { return NULL; } - void *GetData() { return NULL; } -}; +const char *upb_msgdef_fullname(const upb_msgdef *m); +const upb_filedef *upb_msgdef_file(const upb_msgdef *m); +const char *upb_msgdef_name(const upb_msgdef *m); +int upb_msgdef_numoneofs(const upb_msgdef *m); +upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m); +bool upb_msgdef_mapentry(const upb_msgdef *m); +upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m); +bool upb_msgdef_isnumberwrapper(const upb_msgdef *m); +bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax); +const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i); +const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, + size_t len); +const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, + size_t len); +int upb_msgdef_numfields(const upb_msgdef *m); +int upb_msgdef_numoneofs(const upb_msgdef *m); -template -struct Func1 : public UnboundFunc { - typedef R Return; - typedef I FuncInfo; - static R Call(P1 p1) { return F(p1); } -}; +UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m, + const char *name) { + return upb_msgdef_ntoo(m, name, strlen(name)); +} -template -struct Func2 : public UnboundFunc { - typedef R Return; - typedef I FuncInfo; - static R Call(P1 p1, P2 p2) { return F(p1, p2); } -}; +UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m, + const char *name) { + return upb_msgdef_ntof(m, name, strlen(name)); +} -template -struct Func3 : public UnboundFunc { - typedef R Return; - typedef I FuncInfo; - static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); } -}; +/* Internal-only. */ +size_t upb_msgdef_selectorcount(const upb_msgdef *m); +uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m); -template -struct Func4 : public UnboundFunc { - typedef R Return; - typedef I FuncInfo; - static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); } -}; +/* 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. */ +bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len, + const upb_fielddef **f, const upb_oneofdef **o); -template -struct Func5 : public UnboundFunc { - typedef R Return; - typedef I FuncInfo; - static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { - return F(p1, p2, p3, p4, p5); - } -}; +UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name, + const upb_fielddef **f, + const upb_oneofdef **o) { + return upb_msgdef_lookupname(m, name, strlen(name), f, o); +} -/* BoundFunc ******************************************************************/ +/* Iteration over fields and oneofs. For example: + * + * upb_msg_field_iter i; + * for(upb_msg_field_begin(&i, m); + * !upb_msg_field_done(&i); + * upb_msg_field_next(&i)) { + * upb_fielddef *f = upb_msg_iter_field(&i); + * // ... + * } + * + * For C we don't have separate iterators for const and non-const. + * It is the caller's responsibility to cast the upb_fielddef* to + * const if the upb_msgdef* is const. */ +void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m); +void upb_msg_field_next(upb_msg_field_iter *iter); +bool upb_msg_field_done(const upb_msg_field_iter *iter); +upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter); +void upb_msg_field_iter_setdone(upb_msg_field_iter *iter); +bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1, + const upb_msg_field_iter * iter2); -/* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that - * shall be bound to the function's second parameter. - * - * Note that the second parameter is a const pointer, but our stored bound value - * is non-const so we can free it when the handlers are destroyed. */ -template -struct BoundFunc { - typedef typename remove_constptr::type MutableP2; - explicit BoundFunc(MutableP2 data_) : data(data_) {} - CleanupFunc *GetCleanup() { return &DeletePointer; } - MutableP2 GetData() { return data; } - MutableP2 data; -}; +/* Similar to above, we also support iterating through the oneofs in a + * msgdef. */ +void upb_msg_oneof_begin(upb_msg_oneof_iter * iter, const upb_msgdef *m); +void upb_msg_oneof_next(upb_msg_oneof_iter * iter); +bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter); +const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter); +void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter); +bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1, + const upb_msg_oneof_iter *iter2); -template -struct BoundFunc2 : public BoundFunc { - typedef BoundFunc Base; - typedef I FuncInfo; - explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {} -}; +#ifdef __cplusplus +} /* extern "C" */ -template -struct BoundFunc3 : public BoundFunc { - typedef BoundFunc Base; - typedef I FuncInfo; - explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {} -}; +/* Structure that describes a single .proto message type. */ +class upb::MessageDefPtr { + public: + MessageDefPtr() : ptr_(nullptr) {} + explicit MessageDefPtr(const upb_msgdef *ptr) : ptr_(ptr) {} -template -struct BoundFunc4 : public BoundFunc { - typedef BoundFunc Base; - typedef I FuncInfo; - explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {} -}; + const upb_msgdef *ptr() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } -template -struct BoundFunc5 : public BoundFunc { - typedef BoundFunc Base; - typedef I FuncInfo; - explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {} -}; + const char* full_name() const { return upb_msgdef_fullname(ptr_); } + const char* name() const { return upb_msgdef_name(ptr_); } -/* FuncSig ********************************************************************/ + /* The number of fields that belong to the MessageDef. */ + int field_count() const { return upb_msgdef_numfields(ptr_); } -/* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function - * *signature*, but without a specific function attached. - * - * These classes contain member functions that can be invoked with a - * specific function to return a Func/BoundFunc class. */ -template -struct FuncSig1 { - template - Func1 > GetFunc() { - return Func1 >(); - } -}; + /* The number of oneofs that belong to the MessageDef. */ + int oneof_count() const { return upb_msgdef_numoneofs(ptr_); } -template -struct FuncSig2 { - template - Func2 > GetFunc() { - return Func2 >(); - } + upb_syntax_t syntax() const { return upb_msgdef_syntax(ptr_); } - template - BoundFunc2 > GetFunc( - typename remove_constptr::type param2) { - return BoundFunc2 >(param2); + /* These return null pointers if the field is not found. */ + FieldDefPtr FindFieldByNumber(uint32_t number) const { + return FieldDefPtr(upb_msgdef_itof(ptr_, number)); } -}; - -template -struct FuncSig3 { - template - Func3 > GetFunc() { - return Func3 >(); + FieldDefPtr FindFieldByName(const char* name, size_t len) const { + return FieldDefPtr(upb_msgdef_ntof(ptr_, name, len)); + } + FieldDefPtr FindFieldByName(const char *name) const { + return FieldDefPtr(upb_msgdef_ntofz(ptr_, name)); } - template - BoundFunc3 > GetFunc( - typename remove_constptr::type param2) { - return BoundFunc3 >(param2); + template + FieldDefPtr FindFieldByName(const T& str) const { + return FindFieldByName(str.c_str(), str.size()); } -}; -template -struct FuncSig4 { - template - Func4 > GetFunc() { - return Func4 >(); + OneofDefPtr FindOneofByName(const char* name, size_t len) const { + return OneofDefPtr(upb_msgdef_ntoo(ptr_, name, len)); } - template - BoundFunc4 > GetFunc( - typename remove_constptr::type param2) { - return BoundFunc4 >(param2); + OneofDefPtr FindOneofByName(const char *name) const { + return OneofDefPtr(upb_msgdef_ntooz(ptr_, name)); } -}; -template -struct FuncSig5 { - template - Func5 > GetFunc() { - return Func5 >(); + template + OneofDefPtr FindOneofByName(const T &str) const { + return FindOneofByName(str.c_str(), str.size()); } - template - BoundFunc5 > GetFunc( - typename remove_constptr::type param2) { - return BoundFunc5 >(param2); + /* Is this message a map entry? */ + bool mapentry() const { return upb_msgdef_mapentry(ptr_); } + + /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for + * non-well-known message. */ + upb_wellknowntype_t wellknowntype() const { + return upb_msgdef_wellknowntype(ptr_); } -}; -/* Overloaded template function that can construct the appropriate FuncSig* - * class given a function pointer by deducing the template parameters. */ -template -inline FuncSig1 MatchFunc(R (*f)(P1)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return FuncSig1(); -} + /* Whether is a number wrapper. */ + bool isnumberwrapper() const { return upb_msgdef_isnumberwrapper(ptr_); } -template -inline FuncSig2 MatchFunc(R (*f)(P1, P2)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return FuncSig2(); -} + /* Iteration over fields. The order is undefined. */ + class const_field_iterator + : public std::iterator { + public: + void operator++() { upb_msg_field_next(&iter_); } -template -inline FuncSig3 MatchFunc(R (*f)(P1, P2, P3)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return FuncSig3(); -} + FieldDefPtr operator*() const { + return FieldDefPtr(upb_msg_iter_field(&iter_)); + } -template -inline FuncSig4 MatchFunc(R (*f)(P1, P2, P3, P4)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return FuncSig4(); -} + bool operator!=(const const_field_iterator &other) const { + return !upb_msg_field_iter_isequal(&iter_, &other.iter_); + } -template -inline FuncSig5 MatchFunc(R (*f)(P1, P2, P3, P4, P5)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return FuncSig5(); -} + bool operator==(const const_field_iterator &other) const { + return upb_msg_field_iter_isequal(&iter_, &other.iter_); + } -/* MethodSig ******************************************************************/ + private: + friend class MessageDefPtr; -/* CallMethod*: a function template that calls a given method. */ -template -R CallMethod0(C *obj) { - return ((*obj).*F)(); -} + explicit const_field_iterator() {} -template -R CallMethod1(C *obj, P1 arg1) { - return ((*obj).*F)(arg1); -} + explicit const_field_iterator(MessageDefPtr msg) { + upb_msg_field_begin(&iter_, msg.ptr()); + } -template -R CallMethod2(C *obj, P1 arg1, P2 arg2) { - return ((*obj).*F)(arg1, arg2); -} + static const_field_iterator end() { + const_field_iterator iter; + upb_msg_field_iter_setdone(&iter.iter_); + return iter; + } -template -R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) { - return ((*obj).*F)(arg1, arg2, arg3); -} + upb_msg_field_iter iter_; + }; -template -R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) { - return ((*obj).*F)(arg1, arg2, arg3, arg4); -} + /* Iteration over oneofs. The order is undefined. */ + class const_oneof_iterator + : public std::iterator { + public: -/* MethodSig: like FuncSig, but for member functions. - * - * GetFunc() returns a normal FuncN object, so after calling GetFunc() no - * more logic is required to special-case methods. */ -template -struct MethodSig0 { - template - Func1, FuncInfo > GetFunc() { - return Func1, FuncInfo >(); - } -}; + void operator++() { upb_msg_oneof_next(&iter_); } -template -struct MethodSig1 { - template - Func2, FuncInfo > GetFunc() { - return Func2, FuncInfo >(); - } + OneofDefPtr operator*() const { + return OneofDefPtr(upb_msg_iter_oneof(&iter_)); + } + + bool operator!=(const const_oneof_iterator& other) const { + return !upb_msg_oneof_iter_isequal(&iter_, &other.iter_); + } + + bool operator==(const const_oneof_iterator &other) const { + return upb_msg_oneof_iter_isequal(&iter_, &other.iter_); + } + + private: + friend class MessageDefPtr; + + const_oneof_iterator() {} + + explicit const_oneof_iterator(MessageDefPtr msg) { + upb_msg_oneof_begin(&iter_, msg.ptr()); + } + + static const_oneof_iterator end() { + const_oneof_iterator iter; + upb_msg_oneof_iter_setdone(&iter.iter_); + return iter; + } + + upb_msg_oneof_iter iter_; + }; - template - BoundFunc2, FuncInfo > GetFunc( - typename remove_constptr::type param1) { - return BoundFunc2, FuncInfo >( - param1); - } -}; + class ConstFieldAccessor { + public: + explicit ConstFieldAccessor(const upb_msgdef* md) : md_(md) {} + const_field_iterator begin() { return MessageDefPtr(md_).field_begin(); } + const_field_iterator end() { return MessageDefPtr(md_).field_end(); } + private: + const upb_msgdef* md_; + }; -template -struct MethodSig2 { - template - Func3, FuncInfo > - GetFunc() { - return Func3, - FuncInfo >(); - } + class ConstOneofAccessor { + public: + explicit ConstOneofAccessor(const upb_msgdef* md) : md_(md) {} + const_oneof_iterator begin() { return MessageDefPtr(md_).oneof_begin(); } + const_oneof_iterator end() { return MessageDefPtr(md_).oneof_end(); } + private: + const upb_msgdef* md_; + }; - template - BoundFunc3, FuncInfo > - GetFunc(typename remove_constptr::type param1) { - return BoundFunc3, - FuncInfo >(param1); + const_field_iterator field_begin() const { + return const_field_iterator(*this); } -}; -template -struct MethodSig3 { - template - Func4, FuncInfo > - GetFunc() { - return Func4, - FuncInfo >(); - } + const_field_iterator field_end() const { return const_field_iterator::end(); } - template - BoundFunc4, - FuncInfo > - GetFunc(typename remove_constptr::type param1) { - return BoundFunc4, - FuncInfo >(param1); + const_oneof_iterator oneof_begin() const { + return const_oneof_iterator(*this); } -}; -template -struct MethodSig4 { - template - Func5, - FuncInfo > - GetFunc() { - return Func5, - FuncInfo >(); - } + const_oneof_iterator oneof_end() const { return const_oneof_iterator::end(); } - template - BoundFunc5, - FuncInfo > - GetFunc(typename remove_constptr::type param1) { - return BoundFunc5, FuncInfo >( - param1); - } + ConstFieldAccessor fields() const { return ConstFieldAccessor(ptr()); } + ConstOneofAccessor oneofs() const { return ConstOneofAccessor(ptr()); } + + private: + const upb_msgdef* ptr_; }; -template -inline MethodSig0 MatchFunc(R (C::*f)()) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return MethodSig0(); +inline upb::MessageDefPtr upb::FieldDefPtr::message_subdef() const { + return MessageDefPtr(upb_fielddef_msgsubdef(ptr_)); } -template -inline MethodSig1 MatchFunc(R (C::*f)(P1)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return MethodSig1(); +inline upb::MessageDefPtr upb::FieldDefPtr::containing_type() const { + return MessageDefPtr(upb_fielddef_containingtype(ptr_)); } -template -inline MethodSig2 MatchFunc(R (C::*f)(P1, P2)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return MethodSig2(); +inline upb::MessageDefPtr upb::OneofDefPtr::containing_type() const { + return MessageDefPtr(upb_oneofdef_containingtype(ptr_)); } -template -inline MethodSig3 MatchFunc(R (C::*f)(P1, P2, P3)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return MethodSig3(); -} +#endif /* __cplusplus */ -template -inline MethodSig4 MatchFunc(R (C::*f)(P1, P2, P3, P4)) { - UPB_UNUSED(f); /* Only used for template parameter deduction. */ - return MethodSig4(); +/* upb_enumdef ****************************************************************/ + +typedef upb_strtable_iter upb_enum_iter; + +const char *upb_enumdef_fullname(const upb_enumdef *e); +const char *upb_enumdef_name(const upb_enumdef *e); +const upb_filedef *upb_enumdef_file(const upb_enumdef *e); +int32_t upb_enumdef_default(const upb_enumdef *e); +int upb_enumdef_numvals(const upb_enumdef *e); + +/* Enum lookups: + * - ntoi: look up a name with specified length. + * - ntoiz: look up a name provided as a null-terminated string. + * - iton: look up an integer, returning the name as a null-terminated + * string. */ +bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len, + int32_t *num); +UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e, + const char *name, int32_t *num) { + return upb_enumdef_ntoi(e, name, strlen(name), num); } +const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num); -/* MaybeWrapReturn ************************************************************/ +/* upb_enum_iter i; + * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) { + * // ... + * } + */ +void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e); +void upb_enum_next(upb_enum_iter *iter); +bool upb_enum_done(upb_enum_iter *iter); +const char *upb_enum_iter_name(upb_enum_iter *iter); +int32_t upb_enum_iter_number(upb_enum_iter *iter); -/* Template class that attempts to wrap the return value of the function so it - * matches the expected type. There are two main adjustments it may make: - * - * 1. If the function returns void, make it return the expected type and with - * a value that always indicates success. - * 2. If the function returns bool, make it return the expected type with a - * value that indicates success or failure. - * - * The "expected type" for return is: - * 1. void* for start handlers. If the closure parameter has a different type - * we will cast it to void* for the return in the success case. - * 2. size_t for string buffer handlers. - * 3. bool for everything else. */ +#ifdef __cplusplus -/* Template parameters are FuncN type and desired return type. */ -template -struct MaybeWrapReturn; +class upb::EnumDefPtr { + public: + EnumDefPtr() : ptr_(nullptr) {} + explicit EnumDefPtr(const upb_enumdef* ptr) : ptr_(ptr) {} -/* If the return type matches, return the given function unwrapped. */ -template -struct MaybeWrapReturn { - typedef F Func; -}; + const upb_enumdef* ptr() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } -/* Function wrapper that munges the return value from void to (bool)true. */ -template -bool ReturnTrue2(P1 p1, P2 p2) { - F(p1, p2); - return true; -} + const char* full_name() const { return upb_enumdef_fullname(ptr_); } + const char* name() const { return upb_enumdef_name(ptr_); } -template -bool ReturnTrue3(P1 p1, P2 p2, P3 p3) { - F(p1, p2, p3); - return true; -} + /* The value that is used as the default when no field default is specified. + * If not set explicitly, the first value that was added will be used. + * The default value must be a member of the enum. + * Requires that value_count() > 0. */ + int32_t default_value() const { return upb_enumdef_default(ptr_); } -/* Function wrapper that munges the return value from void to (void*)arg1 */ -template -void *ReturnClosure2(P1 p1, P2 p2) { - F(p1, p2); - return p1; -} + /* Returns the number of values currently defined in the enum. Note that + * multiple names can refer to the same number, so this may be greater than + * the total number of unique numbers. */ + int value_count() const { return upb_enumdef_numvals(ptr_); } -template -void *ReturnClosure3(P1 p1, P2 p2, P3 p3) { - F(p1, p2, p3); - return p1; -} + /* Lookups from name to integer, returning true if found. */ + bool FindValueByName(const char *name, int32_t *num) const { + return upb_enumdef_ntoiz(ptr_, name, num); + } -/* Function wrapper that munges the return value from R to void*. */ -template -void *CastReturnToVoidPtr2(P1 p1, P2 p2) { - return F(p1, p2); -} + /* Finds the name corresponding to the given number, or NULL if none was + * found. If more than one name corresponds to this number, returns the + * first one that was added. */ + const char *FindValueByNumber(int32_t num) const { + return upb_enumdef_iton(ptr_, num); + } -template -void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) { - return F(p1, p2, p3); -} + /* Iteration over name/value pairs. The order is undefined. + * Adding an enum val invalidates any iterators. + * + * TODO: make compatible with range-for, with elements as pairs? */ + class Iterator { + public: + explicit Iterator(EnumDefPtr e) { upb_enum_begin(&iter_, e.ptr()); } -/* Function wrapper that munges the return value from bool to void*. */ -template -void *ReturnClosureOrBreak2(P1 p1, P2 p2) { - return F(p1, p2) ? p1 : UPB_BREAK; -} + int32_t number() { return upb_enum_iter_number(&iter_); } + const char *name() { return upb_enum_iter_name(&iter_); } + bool Done() { return upb_enum_done(&iter_); } + void Next() { return upb_enum_next(&iter_); } -template -void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) { - return F(p1, p2, p3) ? p1 : UPB_BREAK; -} + private: + upb_enum_iter iter_; + }; -/* For the string callback, which takes five params, returns the size param. */ -template -size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4, - const BufferHandle *p5) { - F(p1, p2, p3, p4, p5); - return p4; -} + private: + const upb_enumdef *ptr_; +}; -/* For the string callback, which takes five params, returns the size param or - * zero. */ -template -size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4, - const BufferHandle *p5) { - return F(p1, p2, p3, p4, p5) ? p4 : 0; +inline upb::EnumDefPtr upb::FieldDefPtr::enum_subdef() const { + return EnumDefPtr(upb_fielddef_enumsubdef(ptr_)); } -/* If we have a function returning void but want a function returning bool, wrap - * it in a function that returns true. */ -template -struct MaybeWrapReturn, bool> { - typedef Func2, I> Func; -}; +#endif /* __cplusplus */ -template -struct MaybeWrapReturn, bool> { - typedef Func3, I> Func; -}; +/* upb_filedef ****************************************************************/ -/* If our function returns void but we want one returning void*, wrap it in a - * function that returns the first argument. */ -template -struct MaybeWrapReturn, void *> { - typedef Func2, I> Func; -}; +#ifdef __cplusplus +extern "C" { +#endif -template -struct MaybeWrapReturn, void *> { - typedef Func3, I> Func; -}; +const char *upb_filedef_name(const upb_filedef *f); +const char *upb_filedef_package(const upb_filedef *f); +const char *upb_filedef_phpprefix(const upb_filedef *f); +const char *upb_filedef_phpnamespace(const upb_filedef *f); +upb_syntax_t upb_filedef_syntax(const upb_filedef *f); +int upb_filedef_depcount(const upb_filedef *f); +int upb_filedef_msgcount(const upb_filedef *f); +int upb_filedef_enumcount(const upb_filedef *f); +const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i); +const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i); +const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i); + +#ifdef __cplusplus +} /* extern "C" */ + +/* Class that represents a .proto file with some things defined in it. + * + * Many users won't care about FileDefs, but they are necessary if you want to + * read the values of file-level options. */ +class upb::FileDefPtr { + public: + explicit FileDefPtr(const upb_filedef *ptr) : ptr_(ptr) {} + + const upb_filedef* ptr() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } + + /* Get/set name of the file (eg. "foo/bar.proto"). */ + const char* name() const { return upb_filedef_name(ptr_); } -/* If our function returns R* but we want one returning void*, wrap it in a - * function that casts to void*. */ -template -struct MaybeWrapReturn, void *, - typename disable_if_same::Type> { - typedef Func2, I> Func; -}; + /* Package name for definitions inside the file (eg. "foo.bar"). */ + const char* package() const { return upb_filedef_package(ptr_); } -template -struct MaybeWrapReturn, void *, - typename disable_if_same::Type> { - typedef Func3, I> - Func; -}; + /* Sets the php class prefix which is prepended to all php generated classes + * from this .proto. Default is empty. */ + const char* phpprefix() const { return upb_filedef_phpprefix(ptr_); } -/* If our function returns bool but we want one returning void*, wrap it in a - * function that returns either the first param or UPB_BREAK. */ -template -struct MaybeWrapReturn, void *> { - typedef Func2, I> Func; -}; + /* Use this option to change the namespace of php generated classes. Default + * is empty. When this option is empty, the package name will be used for + * determining the namespace. */ + const char* phpnamespace() const { return upb_filedef_phpnamespace(ptr_); } -template -struct MaybeWrapReturn, void *> { - typedef Func3, I> - Func; -}; + /* Syntax for the file. Defaults to proto2. */ + upb_syntax_t syntax() const { return upb_filedef_syntax(ptr_); } -/* If our function returns void but we want one returning size_t, wrap it in a - * function that returns the size argument. */ -template -struct MaybeWrapReturn< - Func5, - size_t> { - typedef Func5, I> Func; -}; + /* Get the list of dependencies from the file. These are returned in the + * order that they were added to the FileDefPtr. */ + int dependency_count() const { return upb_filedef_depcount(ptr_); } + const FileDefPtr dependency(int index) const { + return FileDefPtr(upb_filedef_dep(ptr_, index)); + } -/* If our function returns bool but we want one returning size_t, wrap it in a - * function that returns either 0 or the buf size. */ -template -struct MaybeWrapReturn< - Func5, - size_t> { - typedef Func5, I> Func; + private: + const upb_filedef* ptr_; }; -/* ConvertParams **************************************************************/ - -/* Template class that converts the function parameters if necessary, and - * ignores the HandlerData parameter if appropriate. - * - * Template parameter is the are FuncN function type. */ -template -struct ConvertParams; +#endif /* __cplusplus */ -/* Function that discards the handler data parameter. */ -template -R IgnoreHandlerData2(void *p1, const void *hd) { - UPB_UNUSED(hd); - return F(static_cast(p1)); -} +/* upb_symtab *****************************************************************/ -template -R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) { - UPB_UNUSED(hd); - return F(static_cast(p1), p2); -} +#ifdef __cplusplus +extern "C" { +#endif -template -R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) { - UPB_UNUSED(hd); - return F(static_cast(p1), p2, p3); -} +upb_symtab *upb_symtab_new(); +void upb_symtab_free(upb_symtab* s); +const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); +const upb_msgdef *upb_symtab_lookupmsg2( + const upb_symtab *s, const char *sym, size_t len); +const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); +const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name); +int upb_symtab_filecount(const upb_symtab *s); +const upb_filedef *upb_symtab_addfile( + upb_symtab *s, const google_protobuf_FileDescriptorProto *file, + upb_status *status); -template -R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) { - UPB_UNUSED(hd); - return F(static_cast(p1), p2, p3, p4); -} +/* For generated code only: loads a generated descriptor. */ +typedef struct upb_def_init { + struct upb_def_init **deps; + const char *filename; + upb_strview descriptor; +} upb_def_init; -template -R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2, - size_t p3, const BufferHandle *handle) { - UPB_UNUSED(hd); - UPB_UNUSED(handle); - return F(static_cast(p1), p2, p3); -} +bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init); -/* Function that casts the handler data parameter. */ -template -R CastHandlerData2(void *c, const void *hd) { - return F(static_cast(c), static_cast(hd)); -} +#ifdef __cplusplus +} /* extern "C" */ -template -R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) { - return F(static_cast(c), static_cast(hd), p3); -} +/* Non-const methods in upb::SymbolTable are NOT thread-safe. */ +class upb::SymbolTable { + public: + SymbolTable() : ptr_(upb_symtab_new(), upb_symtab_free) {} + explicit SymbolTable(upb_symtab* s) : ptr_(s, upb_symtab_free) {} -template -R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) { - return F(static_cast(c), static_cast(hd), p3, p4, p5); -} + const upb_symtab* ptr() const { return ptr_.get(); } + upb_symtab* ptr() { return ptr_.get(); } -template -R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3, - size_t p4, const BufferHandle *handle) { - UPB_UNUSED(handle); - return F(static_cast(c), static_cast(hd), p3, p4); -} + /* Finds an entry in the symbol table with this exact name. If not found, + * returns NULL. */ + MessageDefPtr LookupMessage(const char *sym) const { + return MessageDefPtr(upb_symtab_lookupmsg(ptr_.get(), sym)); + } -/* For unbound functions, ignore the handler data. */ -template -struct ConvertParams, T> { - typedef Func2, I> Func; -}; + EnumDefPtr LookupEnum(const char *sym) const { + return EnumDefPtr(upb_symtab_lookupenum(ptr_.get(), sym)); + } -template -struct ConvertParams, - R2 (*)(P1_2, P2_2, P3_2)> { - typedef Func3, I> Func; -}; + FileDefPtr LookupFile(const char *name) const { + return FileDefPtr(upb_symtab_lookupfile(ptr_.get(), name)); + } -/* For StringBuffer only; this ignores both the handler data and the - * BufferHandle. */ -template -struct ConvertParams, T> { - typedef Func5, - I> Func; -}; + /* TODO: iteration? */ -template -struct ConvertParams, T> { - typedef Func5, I> Func; -}; + /* Adds the given serialized FileDescriptorProto to the pool. */ + FileDefPtr AddFile(const google_protobuf_FileDescriptorProto *file_proto, + Status *status) { + return FileDefPtr( + upb_symtab_addfile(ptr_.get(), file_proto, status->ptr())); + } -/* For bound functions, cast the handler data. */ -template -struct ConvertParams, T> { - typedef Func2, I> - Func; + private: + std::unique_ptr ptr_; }; -template -struct ConvertParams, - R2 (*)(P1_2, P2_2, P3_2)> { - typedef Func3, I> Func; -}; +UPB_INLINE const char* upb_safecstr(const std::string& str) { + UPB_ASSERT(str.size() == std::strlen(str.c_str())); + return str.c_str(); +} -/* For StringBuffer only; this ignores the BufferHandle. */ -template -struct ConvertParams, T> { - typedef Func5, - I> Func; -}; +#endif /* __cplusplus */ -template -struct ConvertParams, T> { - typedef Func5, I> Func; -}; +#endif /* UPB_DEF_H_ */ +/* +** upb::Handlers (upb_handlers) +** +** A upb_handlers is like a virtual table for a upb_msgdef. Each field of the +** message can have associated functions that will be called when we are +** parsing or visiting a stream of data. This is similar to how handlers work +** in SAX (the Simple API for XML). +** +** The handlers have no idea where the data is coming from, so a single set of +** handlers could be used with two completely different data sources (for +** example, a parser and a visitor over in-memory objects). This decoupling is +** the most important feature of upb, because it allows parsers and serializers +** to be highly reusable. +** +** This is a mixed C/C++ interface that offers a full API to both languages. +** See the top-level README for more information. +*/ -/* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is - * variant C type. */ -#define TYPE_METHODS(utype, ltype, ctype, vtype) \ - template <> struct CanonicalType { \ - typedef ctype Type; \ - }; \ - template <> \ - inline bool Handlers::SetValueHandler( \ - const FieldDef *f, \ - const Handlers::utype ## Handler& handler) { \ - UPB_ASSERT(!handler.registered_); \ - handler.AddCleanup(this); \ - handler.registered_ = true; \ - return upb_handlers_set##ltype(this, f, handler.handler_, &handler.attr_); \ - } \ +#ifndef UPB_HANDLERS_H +#define UPB_HANDLERS_H -TYPE_METHODS(Double, double, double, double) -TYPE_METHODS(Float, float, float, float) -TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T) -TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T) -TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T) -TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T) -TYPE_METHODS(Bool, bool, bool, bool) -#ifdef UPB_TWO_32BIT_TYPES -TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T) -TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T) +#ifdef __cplusplus +namespace upb { +class HandlersPtr; +class HandlerCache; +template class Handler; +template struct CanonicalType; +} /* namespace upb */ #endif -#ifdef UPB_TWO_64BIT_TYPES -TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T) -TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T) -#endif -#undef TYPE_METHODS -template <> struct CanonicalType { - typedef Status* Type; -}; +/* The maximum depth that the handler graph can have. This is a resource limit + * for the C stack since we sometimes need to recursively traverse the graph. + * Cycles are ok; the traversal will stop when it detects a cycle, but we must + * hit the cycle before the maximum depth is reached. + * + * If having a single static limit is too inflexible, we can add another variant + * of Handlers::Freeze that allows specifying this as a parameter. */ +#define UPB_MAX_HANDLER_DEPTH 64 -/* Type methods that are only one-per-canonical-type and not - * one-per-cvariant. */ - -#define TYPE_METHODS(utype, ctype) \ - inline bool Handlers::Set##utype##Handler(const FieldDef *f, \ - const utype##Handler &h) { \ - return SetValueHandler(f, h); \ - } \ - -TYPE_METHODS(Double, double) -TYPE_METHODS(Float, float) -TYPE_METHODS(UInt64, uint64_t) -TYPE_METHODS(UInt32, uint32_t) -TYPE_METHODS(Int64, int64_t) -TYPE_METHODS(Int32, int32_t) -TYPE_METHODS(Bool, bool) -#undef TYPE_METHODS +/* All the different types of handlers that can be registered. + * Only needed for the advanced functions in upb::Handlers. */ +typedef enum { + UPB_HANDLER_INT32, + UPB_HANDLER_INT64, + UPB_HANDLER_UINT32, + UPB_HANDLER_UINT64, + UPB_HANDLER_FLOAT, + UPB_HANDLER_DOUBLE, + UPB_HANDLER_BOOL, + UPB_HANDLER_STARTSTR, + UPB_HANDLER_STRING, + UPB_HANDLER_ENDSTR, + UPB_HANDLER_STARTSUBMSG, + UPB_HANDLER_ENDSUBMSG, + UPB_HANDLER_STARTSEQ, + UPB_HANDLER_ENDSEQ +} upb_handlertype_t; -template struct ReturnOf; +#define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1) -template -struct ReturnOf { - typedef R Return; -}; +#define UPB_BREAK NULL -template -struct ReturnOf { - typedef R Return; -}; +/* A convenient definition for when no closure is needed. */ +extern char _upb_noclosure; +#define UPB_NO_CLOSURE &_upb_noclosure -template -struct ReturnOf { - typedef R Return; -}; +/* A selector refers to a specific field handler in the Handlers object + * (for example: the STARTSUBMSG handler for field "field15"). */ +typedef int32_t upb_selector_t; -template -struct ReturnOf { - typedef R Return; -}; +/* Static selectors for upb::Handlers. */ +#define UPB_STARTMSG_SELECTOR 0 +#define UPB_ENDMSG_SELECTOR 1 +#define UPB_UNKNOWN_SELECTOR 2 +#define UPB_STATIC_SELECTOR_COUNT 3 +/* Static selectors for upb::BytesHandler. */ +#define UPB_STARTSTR_SELECTOR 0 +#define UPB_STRING_SELECTOR 1 +#define UPB_ENDSTR_SELECTOR 2 + +#ifdef __cplusplus template const void *UniquePtrForType() { static const char ch = 0; return &ch; } +#endif -template -template -inline Handler::Handler(F func) - : registered_(false), - cleanup_data_(func.GetData()), - cleanup_func_(func.GetCleanup()) { - upb_handlerattr_sethandlerdata(&attr_, func.GetData()); - typedef typename ReturnOf::Return Return; - typedef typename ConvertParams::Func ConvertedParamsFunc; - typedef typename MaybeWrapReturn::Func - ReturnWrappedFunc; - handler_ = ReturnWrappedFunc().Call; +/* upb_handlers ************************************************************/ - /* Set attributes based on what templates can statically tell us about the - * user's function. */ +/* Handler attributes, to be registered with the handler itself. */ +typedef struct { + const void *handler_data; + const void *closure_type; + const void *return_closure_type; + bool alwaysok; +} upb_handlerattr; - /* If the original function returns void, then we know that we wrapped it to - * always return ok. */ - bool always_ok = is_same::value; - attr_.SetAlwaysOk(always_ok); +#define UPB_HANDLERATTR_INIT {NULL, NULL, NULL, false} - /* Closure parameter and return type. */ - attr_.SetClosureType(UniquePtrForType()); +/* Bufhandle, data passed along with a buffer to indicate its provenance. */ +typedef struct { + /* The beginning of the buffer. This may be different than the pointer + * passed to a StringBuf handler because the handler may receive data + * that is from the middle or end of a larger buffer. */ + const char *buf; - /* We use the closure type (from the first parameter) if the return type is - * void or bool, since these are the two cases we wrap to return the closure's - * type anyway. - * - * This is all nonsense for non START* handlers, but it doesn't matter because - * in that case the value will be ignored. */ - typedef typename FirstUnlessVoidOrBool::value - EffectiveReturn; - attr_.SetReturnClosureType(UniquePtrForType()); -} + /* The offset within the attached object where this buffer begins. Only + * meaningful if there is an attached object. */ + size_t objofs; -template -inline Handler::~Handler() { - UPB_ASSERT(registered_); -} + /* The attached object (if any) and a pointer representing its type. */ + const void *obj; + const void *objtype; -inline HandlerAttributes::HandlerAttributes() { upb_handlerattr_init(this); } -inline HandlerAttributes::~HandlerAttributes() { upb_handlerattr_uninit(this); } -inline bool HandlerAttributes::SetHandlerData(const void *hd) { - return upb_handlerattr_sethandlerdata(this, hd); -} -inline const void* HandlerAttributes::handler_data() const { - return upb_handlerattr_handlerdata(this); -} -inline bool HandlerAttributes::SetClosureType(const void *type) { - return upb_handlerattr_setclosuretype(this, type); -} -inline const void* HandlerAttributes::closure_type() const { - return upb_handlerattr_closuretype(this); -} -inline bool HandlerAttributes::SetReturnClosureType(const void *type) { - return upb_handlerattr_setreturnclosuretype(this, type); -} -inline const void* HandlerAttributes::return_closure_type() const { - return upb_handlerattr_returnclosuretype(this); -} -inline bool HandlerAttributes::SetAlwaysOk(bool always_ok) { - return upb_handlerattr_setalwaysok(this, always_ok); -} -inline bool HandlerAttributes::always_ok() const { - return upb_handlerattr_alwaysok(this); -} +#ifdef __cplusplus + template + void SetAttachedObject(const T* _obj) { + obj = _obj; + objtype = UniquePtrForType(); + } -inline BufferHandle::BufferHandle() { upb_bufhandle_init(this); } -inline BufferHandle::~BufferHandle() { upb_bufhandle_uninit(this); } -inline const char* BufferHandle::buffer() const { - return upb_bufhandle_buf(this); -} -inline size_t BufferHandle::object_offset() const { - return upb_bufhandle_objofs(this); -} -inline void BufferHandle::SetBuffer(const char* buf, size_t ofs) { - upb_bufhandle_setbuf(this, buf, ofs); -} -template -void BufferHandle::SetAttachedObject(const T* obj) { - upb_bufhandle_setobj(this, obj, UniquePtrForType()); -} -template -const T* BufferHandle::GetAttachedObject() const { - return upb_bufhandle_objtype(this) == UniquePtrForType() - ? static_cast(upb_bufhandle_obj(this)) - : NULL; -} - -inline reffed_ptr Handlers::New(const MessageDef *m) { - upb_handlers *h = upb_handlers_new(m, &h); - return reffed_ptr(h, &h); -} -inline reffed_ptr Handlers::NewFrozen( - const MessageDef *m, upb_handlers_callback *callback, - const void *closure) { - const upb_handlers *h = upb_handlers_newfrozen(m, &h, callback, closure); - return reffed_ptr(h, &h); -} -inline const Status* Handlers::status() { - return upb_handlers_status(this); -} -inline void Handlers::ClearError() { - return upb_handlers_clearerr(this); -} -inline bool Handlers::Freeze(Status *s) { - upb::Handlers* h = this; - return upb_handlers_freeze(&h, 1, s); -} -inline bool Handlers::Freeze(Handlers *const *handlers, int n, Status *s) { - return upb_handlers_freeze(handlers, n, s); -} -inline bool Handlers::Freeze(const std::vector& h, Status* status) { - return upb_handlers_freeze((Handlers* const*)&h[0], h.size(), status); -} -inline const MessageDef *Handlers::message_def() const { - return upb_handlers_msgdef(this); -} -inline bool Handlers::AddCleanup(void *p, upb_handlerfree *func) { - return upb_handlers_addcleanup(this, p, func); -} -inline bool Handlers::SetStartMessageHandler( - const Handlers::StartMessageHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstartmsg(this, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetEndMessageHandler( - const Handlers::EndMessageHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setendmsg(this, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetStartStringHandler(const FieldDef *f, - const StartStringHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstartstr(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetEndStringHandler(const FieldDef *f, - const EndFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setendstr(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetStringHandler(const FieldDef *f, - const StringHandler& handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstring(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetStartSequenceHandler( - const FieldDef *f, const StartFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstartseq(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetStartSubMessageHandler( - const FieldDef *f, const StartFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstartsubmsg(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetEndSubMessageHandler(const FieldDef *f, - const EndFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setendsubmsg(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetEndSequenceHandler(const FieldDef *f, - const EndFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetSubHandlers(const FieldDef *f, const Handlers *sub) { - return upb_handlers_setsubhandlers(this, f, sub); -} -inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const { - return upb_handlers_getsubhandlers(this, f); -} -inline const Handlers *Handlers::GetSubHandlers(Handlers::Selector sel) const { - return upb_handlers_getsubhandlers_sel(this, sel); -} -inline bool Handlers::GetSelector(const FieldDef *f, Handlers::Type type, - Handlers::Selector *s) { - return upb_handlers_getselector(f, type, s); -} -inline Handlers::Selector Handlers::GetEndSelector(Handlers::Selector start) { - return upb_handlers_getendselector(start); -} -inline Handlers::GenericFunction *Handlers::GetHandler( - Handlers::Selector selector) { - return upb_handlers_gethandler(this, selector); -} -inline const void *Handlers::GetHandlerData(Handlers::Selector selector) { - return upb_handlers_gethandlerdata(this, selector); -} - -inline BytesHandler::BytesHandler() { - upb_byteshandler_init(this); -} - -inline BytesHandler::~BytesHandler() {} + template + const T *GetAttachedObject() const { + return objtype == UniquePtrForType() ? static_cast(obj) + : NULL; + } +#endif +} upb_bufhandle; -} /* namespace upb */ +#define UPB_BUFHANDLE_INIT {NULL, 0, NULL, NULL} -#endif /* __cplusplus */ +/* Handler function typedefs. */ +typedef void upb_handlerfree(void *d); +typedef bool upb_unknown_handlerfunc(void *c, const void *hd, const char *buf, + size_t n); +typedef bool upb_startmsg_handlerfunc(void *c, const void*); +typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status); +typedef void* upb_startfield_handlerfunc(void *c, const void *hd); +typedef bool upb_endfield_handlerfunc(void *c, const void *hd); +typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val); +typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val); +typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val); +typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val); +typedef bool upb_float_handlerfunc(void *c, const void *hd, float val); +typedef bool upb_double_handlerfunc(void *c, const void *hd, double val); +typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val); +typedef void *upb_startstr_handlerfunc(void *c, const void *hd, + size_t size_hint); +typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf, + size_t n, const upb_bufhandle* handle); +struct upb_handlers; +typedef struct upb_handlers upb_handlers; -#undef UPB_TWO_32BIT_TYPES -#undef UPB_TWO_64BIT_TYPES -#undef UPB_INT32_T -#undef UPB_UINT32_T -#undef UPB_INT32ALT_T -#undef UPB_UINT32ALT_T -#undef UPB_INT64_T -#undef UPB_UINT64_T -#undef UPB_INT64ALT_T -#undef UPB_UINT64ALT_T +#ifdef __cplusplus +extern "C" { +#endif -#endif /* UPB_HANDLERS_INL_H_ */ +/* Mutating accessors. */ +const upb_status *upb_handlers_status(upb_handlers *h); +void upb_handlers_clearerr(upb_handlers *h); +const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h); +bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree); +bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f, + upb_int32_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f, + upb_int64_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f, + upb_uint32_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f, + upb_uint64_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f, + upb_float_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f, + upb_double_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f, + upb_bool_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f, + upb_startstr_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f, + upb_string_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f, + upb_endfield_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f, + upb_startfield_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f, + upb_startfield_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f, + upb_endfield_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f, + upb_endfield_handlerfunc *func, + const upb_handlerattr *attr); -#endif /* UPB_HANDLERS_H */ -/* -** upb::Sink (upb_sink) -** upb::BytesSink (upb_bytessink) -** -** A upb_sink is an object that binds a upb_handlers object to some runtime -** state. It is the object that can actually receive data via the upb_handlers -** interface. -** -** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or -** thread-safe. You can create as many of them as you want, but each one may -** only be used in a single thread at a time. -** -** If we compare with class-based OOP, a you can think of a upb_def as an -** abstract base class, a upb_handlers as a concrete derived class, and a -** upb_sink as an object (class instance). -*/ +/* Read-only accessors. */ +const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h, + const upb_fielddef *f); +const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, + upb_selector_t sel); +upb_func *upb_handlers_gethandler(const upb_handlers *h, upb_selector_t s, + const void **handler_data); +bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s, + upb_handlerattr *attr); -#ifndef UPB_SINK_H -#define UPB_SINK_H +/* "Static" methods */ +upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f); +bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, + upb_selector_t *s); +UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) { + return start + 1; +} +/* Internal-only. */ +uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f); +uint32_t upb_handlers_selectorcount(const upb_fielddef *f); #ifdef __cplusplus +} /* extern "C" */ + namespace upb { -class BufferSink; -class BufferSource; -class BytesSink; -class Sink; +typedef upb_handlers Handlers; } -#endif - -UPB_DECLARE_TYPE(upb::BufferSink, upb_bufsink) -UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc) -UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink) -UPB_DECLARE_TYPE(upb::Sink, upb_sink) - -#ifdef __cplusplus -/* A upb::Sink is an object that binds a upb::Handlers object to some runtime - * state. It represents an endpoint to which data can be sent. - * - * TODO(haberman): right now all of these functions take selectors. Should they - * take selectorbase instead? - * - * ie. instead of calling: - * sink->StartString(FOO_FIELD_START_STRING, ...) - * a selector base would let you say: - * sink->StartString(FOO_FIELD, ...) - * - * This would make call sites a little nicer and require emitting fewer selector - * definitions in .h files. +/* Convenience macros for creating a Handler object that is wrapped with a + * type-safe wrapper function that converts the "void*" parameters/returns + * of the underlying C API into nice C++ function. * - * But the current scheme has the benefit that you can retrieve a function - * pointer for any handler with handlers->GetHandler(selector), without having - * to have a separate GetHandler() function for each handler type. The JIT - * compiler uses this. To accommodate we'd have to expose a separate - * GetHandler() for every handler type. + * Sample usage: + * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) { + * // do stuff ... + * } * - * Also to ponder: selectors right now are independent of a specific Handlers - * instance. In other words, they allocate a number to every possible handler - * that *could* be registered, without knowing anything about what handlers - * *are* registered. That means that using selectors as table offsets prohibits - * us from compacting the handler table at Freeze() time. If the table is very - * sparse, this could be wasteful. + * // Handler that doesn't need any data bound to it. + * void OnValue2(MyClosure* c, int32_t val) { + * // do stuff ... + * } * - * Having another selector-like thing that is specific to a Handlers instance - * would allow this compacting, but then it would be impossible to write code - * ahead-of-time that can be bound to any Handlers instance at runtime. For - * example, a .proto file parser written as straight C will not know what - * Handlers it will be bound to, so when it calls sink->StartString() what - * selector will it pass? It needs a selector like we have today, that is - * independent of any particular upb::Handlers. + * // Handler that returns bool so it can return failure if necessary. + * bool OnValue3(MyClosure* c, int32_t val) { + * // do stuff ... + * return ok; + * } * - * Is there a way then to allow Handlers table compaction? */ -class upb::Sink { - public: - /* Constructor with no initialization; must be Reset() before use. */ - Sink() {} - - /* Constructs a new sink for the given frozen handlers and closure. - * - * TODO: once the Handlers know the expected closure type, verify that T - * matches it. */ - template Sink(const Handlers* handlers, T* closure); - - /* Resets the value of the sink. */ - template void Reset(const Handlers* handlers, T* closure); + * // Member function handler. + * class MyClosure { + * public: + * void OnValue(int32_t val) { + * // do stuff ... + * } + * }; + * + * // Takes ownership of the MyHandlerData. + * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...))); + * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2)); + * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3)); + * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue)); + */ - /* Returns the top-level object that is bound to this sink. - * - * TODO: once the Handlers know the expected closure type, verify that T - * matches it. */ - template T* GetObject() const; +/* In C++11, the "template" disambiguator can appear even outside templates, + * so all calls can safely use this pair of macros. */ - /* Functions for pushing data into the sink. - * - * These return false if processing should stop (either due to error or just - * to suspend). - * - * These may not be called from within one of the same sink's handlers (in - * other words, handlers are not re-entrant). */ +#define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc() - /* Should be called at the start and end of every message; both the top-level - * message and submessages. This means that submessages should use the - * following sequence: - * sink->StartSubMessage(startsubmsg_selector); - * sink->StartMessage(); - * // ... - * sink->EndMessage(&status); - * sink->EndSubMessage(endsubmsg_selector); */ - bool StartMessage(); - bool EndMessage(Status* status); +/* We have to be careful to only evaluate "d" once. */ +#define UpbBind(f, d) upb::MatchFunc(f).template GetFunc((d)) - /* Putting of individual values. These work for both repeated and - * non-repeated fields, but for repeated fields you must wrap them in - * calls to StartSequence()/EndSequence(). */ - bool PutInt32(Handlers::Selector s, int32_t val); - bool PutInt64(Handlers::Selector s, int64_t val); - bool PutUInt32(Handlers::Selector s, uint32_t val); - bool PutUInt64(Handlers::Selector s, uint64_t val); - bool PutFloat(Handlers::Selector s, float val); - bool PutDouble(Handlers::Selector s, double val); - bool PutBool(Handlers::Selector s, bool val); +/* Handler: a struct that contains the (handler, data, deleter) tuple that is + * used to register all handlers. Users can Make() these directly but it's + * more convenient to use the UpbMakeHandler/UpbBind macros above. */ +template class upb::Handler { + public: + /* The underlying, handler function signature that upb uses internally. */ + typedef T FuncPtr; - /* Putting of string/bytes values. Each string can consist of zero or more - * non-contiguous buffers of data. - * - * For StartString(), the function will write a sink for the string to "sub." - * The sub-sink must be used for any/all PutStringBuffer() calls. */ - bool StartString(Handlers::Selector s, size_t size_hint, Sink* sub); - size_t PutStringBuffer(Handlers::Selector s, const char *buf, size_t len, - const BufferHandle *handle); - bool EndString(Handlers::Selector s); + /* Intentionally implicit. */ + template Handler(F func); + ~Handler() { UPB_ASSERT(registered_); } - /* For submessage fields. - * - * For StartSubMessage(), the function will write a sink for the string to - * "sub." The sub-sink must be used for any/all handlers called within the - * submessage. */ - bool StartSubMessage(Handlers::Selector s, Sink* sub); - bool EndSubMessage(Handlers::Selector s); + void AddCleanup(upb_handlers* h) const; + FuncPtr handler() const { return handler_; } + const upb_handlerattr& attr() const { return attr_; } - /* For repeated fields of any type, the sequence of values must be wrapped in - * these calls. - * - * For StartSequence(), the function will write a sink for the string to - * "sub." The sub-sink must be used for any/all handlers called within the - * sequence. */ - bool StartSequence(Handlers::Selector s, Sink* sub); - bool EndSequence(Handlers::Selector s); + private: + Handler(const Handler&) = delete; + Handler& operator=(const Handler&) = delete; - /* Copy and assign specifically allowed. - * We don't even bother making these members private because so many - * functions need them and this is mainly just a dumb data container anyway. - */ -#else -struct upb_sink { -#endif - const upb_handlers *handlers; - void *closure; + FuncPtr handler_; + mutable upb_handlerattr attr_; + mutable bool registered_; + void *cleanup_data_; + upb_handlerfree *cleanup_func_; }; -#ifdef __cplusplus -class upb::BytesSink { +/* A upb::Handlers object represents the set of handlers associated with a + * message in the graph of messages. You can think of it as a big virtual + * table with functions corresponding to all the events that can fire while + * parsing or visiting a message of a specific type. + * + * Any handlers that are not set behave as if they had successfully consumed + * the value. Any unset Start* handlers will propagate their closure to the + * inner frame. + * + * The easiest way to create the *Handler objects needed by the Set* methods is + * with the UpbBind() and UpbMakeHandler() macros; see below. */ +class upb::HandlersPtr { public: - BytesSink() {} + HandlersPtr(upb_handlers* ptr) : ptr_(ptr) {} - /* Constructs a new sink for the given frozen handlers and closure. - * - * TODO(haberman): once the Handlers know the expected closure type, verify - * that T matches it. */ - template BytesSink(const BytesHandler* handler, T* closure); + upb_handlers* ptr() const { return ptr_; } - /* Resets the value of the sink. */ - template void Reset(const BytesHandler* handler, T* closure); + typedef upb_selector_t Selector; + typedef upb_handlertype_t Type; - bool Start(size_t size_hint, void **subc); - size_t PutBuffer(void *subc, const char *buf, size_t len, - const BufferHandle *handle); - bool End(); -#else -struct upb_bytessink { -#endif - const upb_byteshandler *handler; - void *closure; -}; + typedef Handler StartFieldHandler; + typedef Handler EndFieldHandler; + typedef Handler StartMessageHandler; + typedef Handler + EndMessageHandler; + typedef Handler StartStringHandler; + typedef Handler + StringHandler; -#ifdef __cplusplus + template struct ValueHandler { + typedef Handler H; + }; -/* A class for pushing a flat buffer of data to a BytesSink. - * You can construct an instance of this to get a resumable source, - * or just call the static PutBuffer() to do a non-resumable push all in one - * go. */ -class upb::BufferSource { - public: - BufferSource(); - BufferSource(const char* buf, size_t len, BytesSink* sink); + typedef ValueHandler::H Int32Handler; + typedef ValueHandler::H Int64Handler; + typedef ValueHandler::H UInt32Handler; + typedef ValueHandler::H UInt64Handler; + typedef ValueHandler::H FloatHandler; + typedef ValueHandler::H DoubleHandler; + typedef ValueHandler::H BoolHandler; - /* Returns true if the entire buffer was pushed successfully. Otherwise the - * next call to PutNext() will resume where the previous one left off. - * TODO(haberman): implement this. */ - bool PutNext(); + /* Any function pointer can be converted to this and converted back to its + * correct type. */ + typedef void GenericFunction(); - /* A static version; with this version is it not possible to resume in the - * case of failure or a partially-consumed buffer. */ - static bool PutBuffer(const char* buf, size_t len, BytesSink* sink); + typedef void HandlersCallback(const void *closure, upb_handlers *h); - template static bool PutBuffer(const T& str, BytesSink* sink) { - return PutBuffer(str.c_str(), str.size(), sink); + /* Returns the msgdef associated with this handlers object. */ + MessageDefPtr message_def() const { + return MessageDefPtr(upb_handlers_msgdef(ptr())); } -#else -struct upb_bufsrc { - char dummy; -#endif -}; - -UPB_BEGIN_EXTERN_C - -/* A class for accumulating output string data in a flat buffer. */ -upb_bufsink *upb_bufsink_new(upb_env *env); -void upb_bufsink_free(upb_bufsink *sink); -upb_bytessink *upb_bufsink_sink(upb_bufsink *sink); -const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len); - -/* Inline definitions. */ + /* Adds the given pointer and function to the list of cleanup functions that + * will be run when these handlers are freed. If this pointer has previously + * been registered, the function returns false and does nothing. */ + bool AddCleanup(void *ptr, upb_handlerfree *cleanup) { + return upb_handlers_addcleanup(ptr_, ptr, cleanup); + } -UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h, - void *closure) { - s->handler = h; - s->closure = closure; -} + /* Sets the startmsg handler for the message, which is defined as follows: + * + * bool startmsg(MyType* closure) { + * // Called when the message begins. Returns true if processing should + * // continue. + * return true; + * } + */ + bool SetStartMessageHandler(const StartMessageHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setstartmsg(ptr(), h.handler(), &h.attr()); + } -UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint, - void **subc) { - typedef upb_startstr_handlerfunc func; - func *start; - *subc = s->closure; - if (!s->handler) return true; - start = (func *)s->handler->table[UPB_STARTSTR_SELECTOR].func; + /* Sets the endmsg handler for the message, which is defined as follows: + * + * bool endmsg(MyType* closure, upb_status *status) { + * // Called when processing of this message ends, whether in success or + * // failure. "status" indicates the final status of processing, and + * // can also be modified in-place to update the final status. + * } + */ + bool SetEndMessageHandler(const EndMessageHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setendmsg(ptr(), h.handler(), &h.attr()); + } - if (!start) return true; - *subc = start(s->closure, upb_handlerattr_handlerdata( - &s->handler->table[UPB_STARTSTR_SELECTOR].attr), - size_hint); - return *subc != NULL; -} + /* Sets the value handler for the given field, which is defined as follows + * (this is for an int32 field; other field types will pass their native + * C/C++ type for "val"): + * + * bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) { + * // Called when the field's value is encountered. "d" contains + * // whatever data was bound to this field when it was registered. + * // Returns true if processing should continue. + * return true; + * } + * + * handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...))); + * + * The value type must exactly match f->type(). + * For example, a handler that takes an int32_t parameter may only be used for + * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM. + * + * Returns false if the handler failed to register; in this case the cleanup + * handler (if any) will be called immediately. + */ + bool SetInt32Handler(FieldDefPtr f, const Int32Handler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setint32(ptr(), f.ptr(), h.handler(), &h.attr()); + } -UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc, - const char *buf, size_t size, - const upb_bufhandle* handle) { - typedef upb_string_handlerfunc func; - func *putbuf; - if (!s->handler) return true; - putbuf = (func *)s->handler->table[UPB_STRING_SELECTOR].func; + bool SetInt64Handler (FieldDefPtr f, const Int64Handler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setint64(ptr(), f.ptr(), h.handler(), &h.attr()); + } - if (!putbuf) return true; - return putbuf(subc, upb_handlerattr_handlerdata( - &s->handler->table[UPB_STRING_SELECTOR].attr), - buf, size, handle); -} + bool SetUInt32Handler(FieldDefPtr f, const UInt32Handler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setuint32(ptr(), f.ptr(), h.handler(), &h.attr()); + } -UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) { - typedef upb_endfield_handlerfunc func; - func *end; - if (!s->handler) return true; - end = (func *)s->handler->table[UPB_ENDSTR_SELECTOR].func; + bool SetUInt64Handler(FieldDefPtr f, const UInt64Handler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setuint64(ptr(), f.ptr(), h.handler(), &h.attr()); + } - if (!end) return true; - return end(s->closure, - upb_handlerattr_handlerdata( - &s->handler->table[UPB_ENDSTR_SELECTOR].attr)); -} + bool SetFloatHandler (FieldDefPtr f, const FloatHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setfloat(ptr(), f.ptr(), h.handler(), &h.attr()); + } -bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink); + bool SetDoubleHandler(FieldDefPtr f, const DoubleHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setdouble(ptr(), f.ptr(), h.handler(), &h.attr()); + } -#define PUTVAL(type, ctype) \ - UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \ - ctype val) { \ - typedef upb_##type##_handlerfunc functype; \ - functype *func; \ - const void *hd; \ - if (!s->handlers) return true; \ - func = (functype *)upb_handlers_gethandler(s->handlers, sel); \ - if (!func) return true; \ - hd = upb_handlers_gethandlerdata(s->handlers, sel); \ - return func(s->closure, hd, val); \ + bool SetBoolHandler(FieldDefPtr f, const BoolHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setbool(ptr(), f.ptr(), h.handler(), &h.attr()); } -PUTVAL(int32, int32_t) -PUTVAL(int64, int64_t) -PUTVAL(uint32, uint32_t) -PUTVAL(uint64, uint64_t) -PUTVAL(float, float) -PUTVAL(double, double) -PUTVAL(bool, bool) -#undef PUTVAL + /* Like the previous, but templated on the type on the value (ie. int32). + * This is mostly useful to call from other templates. To call this you must + * specify the template parameter explicitly, ie: + * h->SetValueHandler(f, UpbBind(MyHandler, MyData)); */ + template + bool SetValueHandler( + FieldDefPtr f, + const typename ValueHandler::Type>::H &handler); -UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) { - s->handlers = h; - s->closure = c; -} + /* Sets handlers for a string field, which are defined as follows: + * + * MySubClosure* startstr(MyClosure* c, const MyHandlerData* d, + * size_t size_hint) { + * // Called when a string value begins. The return value indicates the + * // closure for the string. "size_hint" indicates the size of the + * // string if it is known, however if the string is length-delimited + * // and the end-of-string is not available size_hint will be zero. + * // This case is indistinguishable from the case where the size is + * // known to be zero. + * // + * // TODO(haberman): is it important to distinguish these cases? + * // If we had ssize_t as a type we could make -1 "unknown", but + * // ssize_t is POSIX (not ANSI) and therefore less portable. + * // In practice I suspect it won't be important to distinguish. + * return closure; + * } + * + * size_t str(MyClosure* closure, const MyHandlerData* d, + * const char *str, size_t len) { + * // Called for each buffer of string data; the multiple physical buffers + * // are all part of the same logical string. The return value indicates + * // how many bytes were consumed. If this number is less than "len", + * // this will also indicate that processing should be halted for now, + * // like returning false or UPB_BREAK from any other callback. If + * // number is greater than "len", the excess bytes will be skipped over + * // and not passed to the callback. + * return len; + * } + * + * bool endstr(MyClosure* c, const MyHandlerData* d) { + * // Called when a string value ends. Return value indicates whether + * // processing should continue. + * return true; + * } + */ + bool SetStartStringHandler(FieldDefPtr f, const StartStringHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setstartstr(ptr(), f.ptr(), h.handler(), &h.attr()); + } -UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel, - const char *buf, size_t n, - const upb_bufhandle *handle) { - typedef upb_string_handlerfunc func; - func *handler; - const void *hd; - if (!s->handlers) return n; - handler = (func *)upb_handlers_gethandler(s->handlers, sel); + bool SetStringHandler(FieldDefPtr f, const StringHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setstring(ptr(), f.ptr(), h.handler(), &h.attr()); + } - if (!handler) return n; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - return handler(s->closure, hd, buf, n, handle); -} + bool SetEndStringHandler(FieldDefPtr f, const EndFieldHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setendstr(ptr(), f.ptr(), h.handler(), &h.attr()); + } -UPB_INLINE bool upb_sink_putunknown(upb_sink *s, const char *buf, size_t n) { - typedef upb_unknown_handlerfunc func; - func *handler; - const void *hd; - if (!s->handlers) return true; - handler = (func *)upb_handlers_gethandler(s->handlers, UPB_UNKNOWN_SELECTOR); + /* Sets the startseq handler, which is defined as follows: + * + * MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) { + * // Called when a sequence (repeated field) begins. The returned + * // pointer indicates the closure for the sequence (or UPB_BREAK + * // to interrupt processing). + * return closure; + * } + * + * h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...))); + * + * Returns "false" if "f" does not belong to this message or is not a + * repeated field. + */ + bool SetStartSequenceHandler(FieldDefPtr f, const StartFieldHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setstartseq(ptr(), f.ptr(), h.handler(), &h.attr()); + } - if (!handler) return n; - hd = upb_handlers_gethandlerdata(s->handlers, UPB_UNKNOWN_SELECTOR); - return handler(s->closure, hd, buf, n); -} + /* Sets the startsubmsg handler for the given field, which is defined as + * follows: + * + * MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) { + * // Called when a submessage begins. The returned pointer indicates the + * // closure for the sequence (or UPB_BREAK to interrupt processing). + * return closure; + * } + * + * h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, + * new MyHandlerData(...))); + * + * Returns "false" if "f" does not belong to this message or is not a + * submessage/group field. + */ + bool SetStartSubMessageHandler(FieldDefPtr f, const StartFieldHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setstartsubmsg(ptr(), f.ptr(), h.handler(), &h.attr()); + } -UPB_INLINE bool upb_sink_startmsg(upb_sink *s) { - typedef upb_startmsg_handlerfunc func; - func *startmsg; - const void *hd; - if (!s->handlers) return true; - startmsg = (func*)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR); + /* Sets the endsubmsg handler for the given field, which is defined as + * follows: + * + * bool endsubmsg(MyClosure* c, const MyHandlerData* d) { + * // Called when a submessage ends. Returns true to continue processing. + * return true; + * } + * + * Returns "false" if "f" does not belong to this message or is not a + * submessage/group field. + */ + bool SetEndSubMessageHandler(FieldDefPtr f, const EndFieldHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setendsubmsg(ptr(), f.ptr(), h.handler(), &h.attr()); + } - if (!startmsg) return true; - hd = upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR); - return startmsg(s->closure, hd); -} + /* Starts the endsubseq handler for the given field, which is defined as + * follows: + * + * bool endseq(MyClosure* c, const MyHandlerData* d) { + * // Called when a sequence ends. Returns true continue processing. + * return true; + * } + * + * Returns "false" if "f" does not belong to this message or is not a + * repeated field. + */ + bool SetEndSequenceHandler(FieldDefPtr f, const EndFieldHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setendseq(ptr(), f.ptr(), h.handler(), &h.attr()); + } -UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) { - typedef upb_endmsg_handlerfunc func; - func *endmsg; - const void *hd; - if (!s->handlers) return true; - endmsg = (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR); + private: + upb_handlers* ptr_; +}; - if (!endmsg) return true; - hd = upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR); - return endmsg(s->closure, hd, status); -} +#endif /* __cplusplus */ -UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel, - upb_sink *sub) { - typedef upb_startfield_handlerfunc func; - func *startseq; - const void *hd; - sub->closure = s->closure; - sub->handlers = s->handlers; - if (!s->handlers) return true; - startseq = (func*)upb_handlers_gethandler(s->handlers, sel); +/* upb_handlercache ***********************************************************/ - if (!startseq) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - sub->closure = startseq(s->closure, hd); - return sub->closure ? true : false; -} +/* A upb_handlercache lazily builds and caches upb_handlers. You pass it a + * function (with optional closure) that can build handlers for a given + * message on-demand, and the cache maintains a map of msgdef->handlers. */ -UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) { - typedef upb_endfield_handlerfunc func; - func *endseq; - const void *hd; - if (!s->handlers) return true; - endseq = (func*)upb_handlers_gethandler(s->handlers, sel); +#ifdef __cplusplus +extern "C" { +#endif - if (!endseq) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - return endseq(s->closure, hd); -} +struct upb_handlercache; +typedef struct upb_handlercache upb_handlercache; -UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel, - size_t size_hint, upb_sink *sub) { - typedef upb_startstr_handlerfunc func; - func *startstr; - const void *hd; - sub->closure = s->closure; - sub->handlers = s->handlers; - if (!s->handlers) return true; - startstr = (func*)upb_handlers_gethandler(s->handlers, sel); +typedef void upb_handlers_callback(const void *closure, upb_handlers *h); - if (!startstr) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - sub->closure = startstr(s->closure, hd, size_hint); - return sub->closure ? true : false; -} +upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, + const void *closure); +void upb_handlercache_free(upb_handlercache *cache); +const upb_handlers *upb_handlercache_get(upb_handlercache *cache, + const upb_msgdef *md); +bool upb_handlercache_addcleanup(upb_handlercache *h, void *p, + upb_handlerfree *hfree); -UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) { - typedef upb_endfield_handlerfunc func; - func *endstr; - const void *hd; - if (!s->handlers) return true; - endstr = (func*)upb_handlers_gethandler(s->handlers, sel); +#ifdef __cplusplus +} /* extern "C" */ - if (!endstr) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - return endstr(s->closure, hd); -} +class upb::HandlerCache { + public: + HandlerCache(upb_handlers_callback *callback, const void *closure) + : ptr_(upb_handlercache_new(callback, closure), upb_handlercache_free) {} + HandlerCache(HandlerCache&&) = default; + HandlerCache& operator=(HandlerCache&&) = default; + HandlerCache(upb_handlercache* c) : ptr_(c, upb_handlercache_free) {} -UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel, - upb_sink *sub) { - typedef upb_startfield_handlerfunc func; - func *startsubmsg; - const void *hd; - sub->closure = s->closure; - if (!s->handlers) { - sub->handlers = NULL; - return true; - } - sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel); - startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel); + upb_handlercache* ptr() { return ptr_.get(); } - if (!startsubmsg) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - sub->closure = startsubmsg(s->closure, hd); - return sub->closure ? true : false; -} + const upb_handlers *Get(MessageDefPtr md) { + return upb_handlercache_get(ptr_.get(), md.ptr()); + } -UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) { - typedef upb_endfield_handlerfunc func; - func *endsubmsg; - const void *hd; - if (!s->handlers) return true; - endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel); + private: + std::unique_ptr ptr_; +}; - if (!endsubmsg) return s->closure; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - return endsubmsg(s->closure, hd); -} +#endif /* __cplusplus */ -UPB_END_EXTERN_C +/* upb_byteshandler ***********************************************************/ -#ifdef __cplusplus +typedef struct { + upb_func *func; -namespace upb { + /* It is wasteful to include the entire attributes here: + * + * * Some of the information is redundant (like storing the closure type + * separately for each handler that must match). + * * Some of the info is only needed prior to freeze() (like closure types). + * * alignment padding wastes a lot of space for alwaysok_. + * + * If/when the size and locality of handlers is an issue, we can optimize this + * not to store the entire attr like this. We do not expose the table's + * layout to allow this optimization in the future. */ + upb_handlerattr attr; +} upb_handlers_tabent; -template Sink::Sink(const Handlers* handlers, T* closure) { - upb_sink_reset(this, handlers, closure); -} -template -inline void Sink::Reset(const Handlers* handlers, T* closure) { - upb_sink_reset(this, handlers, closure); -} -inline bool Sink::StartMessage() { - return upb_sink_startmsg(this); -} -inline bool Sink::EndMessage(Status* status) { - return upb_sink_endmsg(this, status); -} -inline bool Sink::PutInt32(Handlers::Selector sel, int32_t val) { - return upb_sink_putint32(this, sel, val); -} -inline bool Sink::PutInt64(Handlers::Selector sel, int64_t val) { - return upb_sink_putint64(this, sel, val); -} -inline bool Sink::PutUInt32(Handlers::Selector sel, uint32_t val) { - return upb_sink_putuint32(this, sel, val); -} -inline bool Sink::PutUInt64(Handlers::Selector sel, uint64_t val) { - return upb_sink_putuint64(this, sel, val); -} -inline bool Sink::PutFloat(Handlers::Selector sel, float val) { - return upb_sink_putfloat(this, sel, val); -} -inline bool Sink::PutDouble(Handlers::Selector sel, double val) { - return upb_sink_putdouble(this, sel, val); -} -inline bool Sink::PutBool(Handlers::Selector sel, bool val) { - return upb_sink_putbool(this, sel, val); -} -inline bool Sink::StartString(Handlers::Selector sel, size_t size_hint, - Sink *sub) { - return upb_sink_startstr(this, sel, size_hint, sub); -} -inline size_t Sink::PutStringBuffer(Handlers::Selector sel, const char *buf, - size_t len, const BufferHandle* handle) { - return upb_sink_putstring(this, sel, buf, len, handle); -} -inline bool Sink::EndString(Handlers::Selector sel) { - return upb_sink_endstr(this, sel); -} -inline bool Sink::StartSubMessage(Handlers::Selector sel, Sink* sub) { - return upb_sink_startsubmsg(this, sel, sub); -} -inline bool Sink::EndSubMessage(Handlers::Selector sel) { - return upb_sink_endsubmsg(this, sel); -} -inline bool Sink::StartSequence(Handlers::Selector sel, Sink* sub) { - return upb_sink_startseq(this, sel, sub); -} -inline bool Sink::EndSequence(Handlers::Selector sel) { - return upb_sink_endseq(this, sel); -} +#define UPB_TABENT_INIT {NULL, UPB_HANDLERATTR_INIT} -template -BytesSink::BytesSink(const BytesHandler* handler, T* closure) { - Reset(handler, closure); -} +typedef struct { + upb_handlers_tabent table[3]; +} upb_byteshandler; -template -void BytesSink::Reset(const BytesHandler *handler, T *closure) { - upb_bytessink_reset(this, handler, closure); -} -inline bool BytesSink::Start(size_t size_hint, void **subc) { - return upb_bytessink_start(this, size_hint, subc); -} -inline size_t BytesSink::PutBuffer(void *subc, const char *buf, size_t len, - const BufferHandle *handle) { - return upb_bytessink_putbuf(this, subc, buf, len, handle); -} -inline bool BytesSink::End() { - return upb_bytessink_end(this); -} +#define UPB_BYTESHANDLER_INIT \ + { \ + { UPB_TABENT_INIT, UPB_TABENT_INIT, UPB_TABENT_INIT } \ + } -inline bool BufferSource::PutBuffer(const char *buf, size_t len, - BytesSink *sink) { - return upb_bufsrc_putbuf(buf, len, sink); +UPB_INLINE void upb_byteshandler_init(upb_byteshandler *handler) { + upb_byteshandler init = UPB_BYTESHANDLER_INIT; + *handler = init; } -} /* namespace upb */ +#ifdef __cplusplus +extern "C" { #endif -#endif +/* Caller must ensure that "d" outlives the handlers. */ +bool upb_byteshandler_setstartstr(upb_byteshandler *h, + upb_startstr_handlerfunc *func, void *d); +bool upb_byteshandler_setstring(upb_byteshandler *h, + upb_string_handlerfunc *func, void *d); +bool upb_byteshandler_setendstr(upb_byteshandler *h, + upb_endfield_handlerfunc *func, void *d); #ifdef __cplusplus +} /* extern "C" */ namespace upb { -class Array; -class Map; -class MapIterator; -class MessageFactory; -class MessageLayout; -class Visitor; -class VisitorPlan; +typedef upb_byteshandler BytesHandler; } +#endif +/** Message handlers ******************************************************************/ + +#ifdef __cplusplus +extern "C" { #endif -UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory) -UPB_DECLARE_TYPE(upb::MessageLayout, upb_msglayout) -UPB_DECLARE_TYPE(upb::Array, upb_array) -UPB_DECLARE_TYPE(upb::Map, upb_map) -UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter) -UPB_DECLARE_TYPE(upb::Visitor, upb_visitor) -UPB_DECLARE_TYPE(upb::VisitorPlan, upb_visitorplan) +/* These are the handlers used internally by upb_msgfactory_getmergehandlers(). + * They write scalar data to a known offset from the message pointer. + * + * These would be trivial for anyone to implement themselves, but it's better + * to use these because some JITs will recognize and specialize these instead + * of actually calling the function. */ -/* TODO(haberman): C++ accessors */ +/* Sets a handler for the given primitive field that will write the data at the + * given offset. If hasbit > 0, also sets a hasbit at the given bit offset + * (addressing each byte low to high). */ +bool upb_msg_setscalarhandler(upb_handlers *h, + const upb_fielddef *f, + size_t offset, + int32_t hasbit); + +/* If the given handler is a msghandlers_primitive field, returns true and sets + * *type, *offset and *hasbit. Otherwise returns false. */ +bool upb_msg_getscalarhandlerdata(const upb_handlers *h, + upb_selector_t s, + upb_fieldtype_t *type, + size_t *offset, + int32_t *hasbit); -UPB_BEGIN_EXTERN_C -typedef void upb_msg; +#ifdef __cplusplus +} /* extern "C" */ +#endif -/** upb_msglayout *************************************************************/ +/* +** Inline definitions for handlers.h, which are particularly long and a bit +** tricky. +*/ -/* upb_msglayout represents the memory layout of a given upb_msgdef. You get - * instances of this from a upb_msgfactory, and the factory always owns the - * msglayout. */ +#ifndef UPB_HANDLERS_INL_H_ +#define UPB_HANDLERS_INL_H_ +#include +#include -/** upb_visitor ***************************************************************/ +#ifdef __cplusplus -/* upb_visitor will visit all the fields of a message and its submessages. It - * uses a upb_visitorplan which you can obtain from a upb_msgfactory. */ +/* Type detection and typedefs for integer types. + * For platforms where there are multiple 32-bit or 64-bit types, we need to be + * able to enumerate them so we can properly create overloads for all variants. + * + * If any platform existed where there were three integer types with the same + * size, this would have to become more complicated. For example, short, int, + * and long could all be 32-bits. Even more diabolically, short, int, long, + * and long long could all be 64 bits and still be standard-compliant. + * However, few platforms are this strange, and it's unlikely that upb will be + * used on the strangest ones. */ -upb_visitor *upb_visitor_create(upb_env *e, const upb_visitorplan *vp, - upb_sink *output); -bool upb_visitor_visitmsg(upb_visitor *v, const upb_msg *msg); +/* Can't count on stdint.h limits like INT32_MAX, because in C++ these are + * only defined when __STDC_LIMIT_MACROS are defined before the *first* include + * of stdint.h. We can't guarantee that someone else didn't include these first + * without defining __STDC_LIMIT_MACROS. */ +#define UPB_INT32_MAX 0x7fffffffLL +#define UPB_INT32_MIN (-UPB_INT32_MAX - 1) +#define UPB_INT64_MAX 0x7fffffffffffffffLL +#define UPB_INT64_MIN (-UPB_INT64_MAX - 1) +#if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN +#define UPB_INT_IS_32BITS 1 +#endif -/** upb_msgfactory ************************************************************/ +#if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN +#define UPB_LONG_IS_32BITS 1 +#endif -/* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and - * upb_visitorplan objects. These are the objects necessary to represent, - * populate, and and visit upb_msg objects. - * - * These caches are all populated by upb_msgdef, and lazily created on demand. - */ +#if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN +#define UPB_LONG_IS_64BITS 1 +#endif -/* Creates and destroys a msgfactory, respectively. The messages for this - * msgfactory must come from |symtab| (which should outlive the msgfactory). */ -upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab); -void upb_msgfactory_free(upb_msgfactory *f); +#if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN +#define UPB_LLONG_IS_64BITS 1 +#endif -const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f); +/* We use macros instead of typedefs so we can undefine them later and avoid + * leaking them outside this header file. */ +#if UPB_INT_IS_32BITS +#define UPB_INT32_T int +#define UPB_UINT32_T unsigned int -/* The functions to get cached objects, lazily creating them on demand. These - * all require: - * - * - m is in upb_msgfactory_symtab(f) - * - upb_msgdef_mapentry(m) == false (since map messages can't have layouts). - * - * The returned objects will live for as long as the msgfactory does. - * - * TODO(haberman): consider making this thread-safe and take a const - * upb_msgfactory. */ -const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, - const upb_msgdef *m); -const upb_handlers *upb_msgfactory_getmergehandlers(upb_msgfactory *f, - const upb_msgdef *m); -const upb_visitorplan *upb_msgfactory_getvisitorplan(upb_msgfactory *f, - const upb_handlers *h); +#if UPB_LONG_IS_32BITS +#define UPB_TWO_32BIT_TYPES 1 +#define UPB_INT32ALT_T long +#define UPB_UINT32ALT_T unsigned long +#endif /* UPB_LONG_IS_32BITS */ +#elif UPB_LONG_IS_32BITS /* && !UPB_INT_IS_32BITS */ +#define UPB_INT32_T long +#define UPB_UINT32_T unsigned long +#endif /* UPB_INT_IS_32BITS */ -/** upb_stringview ************************************************************/ -typedef struct { - const char *data; - size_t size; -} upb_stringview; +#if UPB_LONG_IS_64BITS +#define UPB_INT64_T long +#define UPB_UINT64_T unsigned long -UPB_INLINE upb_stringview upb_stringview_make(const char *data, size_t size) { - upb_stringview ret; - ret.data = data; - ret.size = size; - return ret; -} +#if UPB_LLONG_IS_64BITS +#define UPB_TWO_64BIT_TYPES 1 +#define UPB_INT64ALT_T long long +#define UPB_UINT64ALT_T unsigned long long +#endif /* UPB_LLONG_IS_64BITS */ -#define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len} +#elif UPB_LLONG_IS_64BITS /* && !UPB_LONG_IS_64BITS */ +#define UPB_INT64_T long long +#define UPB_UINT64_T unsigned long long +#endif /* UPB_LONG_IS_64BITS */ +#undef UPB_INT32_MAX +#undef UPB_INT32_MIN +#undef UPB_INT64_MAX +#undef UPB_INT64_MIN +#undef UPB_INT_IS_32BITS +#undef UPB_LONG_IS_32BITS +#undef UPB_LONG_IS_64BITS +#undef UPB_LLONG_IS_64BITS -/** upb_msgval ****************************************************************/ -/* A union representing all possible protobuf values. Used for generic get/set - * operations. */ +namespace upb { -typedef union { - bool b; - float flt; - double dbl; - int32_t i32; - int64_t i64; - uint32_t u32; - uint64_t u64; - const upb_map* map; - const upb_msg* msg; - const upb_array* arr; - const void* ptr; - upb_stringview str; -} upb_msgval; +typedef void CleanupFunc(void *ptr); -#define ACCESSORS(name, membername, ctype) \ - UPB_INLINE ctype upb_msgval_get ## name(upb_msgval v) { \ - return v.membername; \ - } \ - UPB_INLINE void upb_msgval_set ## name(upb_msgval *v, ctype cval) { \ - v->membername = cval; \ - } \ - UPB_INLINE upb_msgval upb_msgval_ ## name(ctype v) { \ - upb_msgval ret; \ - ret.membername = v; \ - return ret; \ - } +/* Template to remove "const" from "const T*" and just return "T*". + * + * We define a nonsense default because otherwise it will fail to instantiate as + * a function parameter type even in cases where we don't expect any caller to + * actually match the overload. */ +class CouldntRemoveConst {}; +template struct remove_constptr { typedef CouldntRemoveConst type; }; +template struct remove_constptr { typedef T *type; }; -ACCESSORS(bool, b, bool) -ACCESSORS(float, flt, float) -ACCESSORS(double, dbl, double) -ACCESSORS(int32, i32, int32_t) -ACCESSORS(int64, i64, int64_t) -ACCESSORS(uint32, u32, uint32_t) -ACCESSORS(uint64, u64, uint64_t) -ACCESSORS(map, map, const upb_map*) -ACCESSORS(msg, msg, const upb_msg*) -ACCESSORS(ptr, ptr, const void*) -ACCESSORS(arr, arr, const upb_array*) -ACCESSORS(str, str, upb_stringview) +/* Template that we use below to remove a template specialization from + * consideration if it matches a specific type. */ +template struct disable_if_same { typedef void Type; }; +template struct disable_if_same {}; -#undef ACCESSORS +template void DeletePointer(void *p) { delete static_cast(p); } -UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) { - return upb_msgval_str(upb_stringview_make(data, size)); -} +template +struct FirstUnlessVoidOrBool { + typedef T1 value; +}; +template +struct FirstUnlessVoidOrBool { + typedef T2 value; +}; -/** upb_msg *******************************************************************/ +template +struct FirstUnlessVoidOrBool { + typedef T2 value; +}; -/* A upb_msg represents a protobuf message. It always corresponds to a specific - * upb_msglayout, which describes how it is laid out in memory. - * - * The message will have a fixed size, as returned by upb_msg_sizeof(), which - * will be used to store fixed-length fields. The upb_msg may also allocate - * dynamic memory internally to store data such as: - * - * - extensions - * - unknown fields - */ +template +struct is_same { + static bool value; +}; -/* Returns the size of a message given this layout. */ -size_t upb_msg_sizeof(const upb_msglayout *l); +template +struct is_same { + static bool value; +}; -/* upb_msg_init() / upb_msg_uninit() allow the user to use a pre-allocated - * block of memory as a message. The block's size should be upb_msg_sizeof(). - * upb_msg_uninit() must be called to release internally-allocated memory - * unless the allocator is an arena that does not require freeing. - * - * Please note that upb_msg_init() may return a value that is different than - * |msg|, so you must assign the return value and not cast your memory block - * to upb_msg* directly! - * - * Please note that upb_msg_uninit() does *not* free any submessages, maps, - * or arrays referred to by this message's fields. You must free them manually - * yourself. - * - * upb_msg_uninit returns the original memory block, which may be useful if - * you dynamically allocated it (though upb_msg_new() would normally be more - * appropriate in this case). */ -upb_msg *upb_msg_init(void *msg, const upb_msglayout *l, upb_alloc *a); -void *upb_msg_uninit(upb_msg *msg, const upb_msglayout *l); - -/* Like upb_msg_init() / upb_msg_uninit(), except the message's memory is - * allocated / freed from the given upb_alloc. */ -upb_msg *upb_msg_new(const upb_msglayout *l, upb_alloc *a); -void upb_msg_free(upb_msg *msg, const upb_msglayout *l); - -/* Returns the upb_alloc for the given message. - * TODO(haberman): get rid of this? Not sure we want to be storing this - * for every message. */ -upb_alloc *upb_msg_alloc(const upb_msg *msg); - -/* Packs the tree of messages rooted at "msg" into a single hunk of memory, - * allocated from the given allocator. */ -void *upb_msg_pack(const upb_msg *msg, const upb_msglayout *l, - void *p, size_t *ofs, size_t size); +template +bool is_same::value = false; -/* Read-only message API. Can be safely called by anyone. */ +template +bool is_same::value = true; -/* Returns the value associated with this field: - * - for scalar fields (including strings), the value directly. - * - return upb_msg*, or upb_map* for msg/map. - * If the field is unset for these field types, returns NULL. - * - * TODO(haberman): should we let users store cached array/map/msg - * pointers here for fields that are unset? Could be useful for the - * strongly-owned submessage model (ie. generated C API that doesn't use - * arenas). - */ -upb_msgval upb_msg_get(const upb_msg *msg, - int field_index, - const upb_msglayout *l); +/* FuncInfo *******************************************************************/ -/* May only be called for fields where upb_fielddef_haspresence(f) == true. */ -bool upb_msg_has(const upb_msg *msg, - int field_index, - const upb_msglayout *l); +/* Info about the user's original, pre-wrapped function. */ +template +struct FuncInfo { + /* The type of the closure that the function takes (its first param). */ + typedef C Closure; -/* Mutable message API. May only be called by the owner of the message who - * knows its ownership scheme and how to keep it consistent. */ + /* The return type. */ + typedef R Return; +}; -/* Sets the given field to the given value. Does not perform any memory - * management: if you overwrite a pointer to a msg/array/map/string without - * cleaning it up (or using an arena) it will leak. - */ -void upb_msg_set(upb_msg *msg, - int field_index, - upb_msgval val, - const upb_msglayout *l); +/* Func ***********************************************************************/ -/* For a primitive field, set it back to its default. For repeated, string, and - * submessage fields set it back to NULL. This could involve releasing some - * internal memory (for example, from an extension dictionary), but it is not - * recursive in any way and will not recover any memory that may be used by - * arrays/maps/strings/msgs that this field may have pointed to. - */ -bool upb_msg_clearfield(upb_msg *msg, - int field_index, - const upb_msglayout *l); +/* Func1, Func2, Func3: Template classes representing a function and its + * signature. + * + * Since the function is a template parameter, calling the function can be + * inlined at compile-time and does not require a function pointer at runtime. + * These functions are not bound to a handler data so have no data or cleanup + * handler. */ +struct UnboundFunc { + CleanupFunc *GetCleanup() { return nullptr; } + void *GetData() { return nullptr; } +}; -/* TODO(haberman): copyfrom()/mergefrom()? */ +template +struct Func1 : public UnboundFunc { + typedef R Return; + typedef I FuncInfo; + static R Call(P1 p1) { return F(p1); } +}; +template +struct Func2 : public UnboundFunc { + typedef R Return; + typedef I FuncInfo; + static R Call(P1 p1, P2 p2) { return F(p1, p2); } +}; -/** upb_array *****************************************************************/ +template +struct Func3 : public UnboundFunc { + typedef R Return; + typedef I FuncInfo; + static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); } +}; -/* A upb_array stores data for a repeated field. The memory management - * semantics are the same as upb_msg. A upb_array allocates dynamic - * memory internally for the array elements. */ +template +struct Func4 : public UnboundFunc { + typedef R Return; + typedef I FuncInfo; + static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); } +}; -size_t upb_array_sizeof(upb_fieldtype_t type); -void upb_array_init(upb_array *arr, upb_fieldtype_t type, upb_alloc *a); -void upb_array_uninit(upb_array *arr); -upb_array *upb_array_new(upb_fieldtype_t type, upb_alloc *a); -void upb_array_free(upb_array *arr); +template +struct Func5 : public UnboundFunc { + typedef R Return; + typedef I FuncInfo; + static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { + return F(p1, p2, p3, p4, p5); + } +}; -/* Read-only interface. Safe for anyone to call. */ +/* BoundFunc ******************************************************************/ -size_t upb_array_size(const upb_array *arr); -upb_fieldtype_t upb_array_type(const upb_array *arr); -upb_msgval upb_array_get(const upb_array *arr, size_t i); +/* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that + * shall be bound to the function's second parameter. + * + * Note that the second parameter is a const pointer, but our stored bound value + * is non-const so we can free it when the handlers are destroyed. */ +template +struct BoundFunc { + typedef typename remove_constptr::type MutableP2; + explicit BoundFunc(MutableP2 data_) : data(data_) {} + CleanupFunc *GetCleanup() { return &DeletePointer; } + MutableP2 GetData() { return data; } + MutableP2 data; +}; -/* Write interface. May only be called by the message's owner who can enforce - * its memory management invariants. */ +template +struct BoundFunc2 : public BoundFunc { + typedef BoundFunc Base; + typedef I FuncInfo; + explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {} +}; -bool upb_array_set(upb_array *arr, size_t i, upb_msgval val); +template +struct BoundFunc3 : public BoundFunc { + typedef BoundFunc Base; + typedef I FuncInfo; + explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {} +}; +template +struct BoundFunc4 : public BoundFunc { + typedef BoundFunc Base; + typedef I FuncInfo; + explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {} +}; -/** upb_map *******************************************************************/ +template +struct BoundFunc5 : public BoundFunc { + typedef BoundFunc Base; + typedef I FuncInfo; + explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {} +}; -/* A upb_map stores data for a map field. The memory management semantics are - * the same as upb_msg, with one notable exception. upb_map will internally - * store a copy of all string keys, but *not* any string values or submessages. - * So you must ensure that any string or message values outlive the map, and you - * must delete them manually when they are no longer required. */ +/* FuncSig ********************************************************************/ -size_t upb_map_sizeof(upb_fieldtype_t ktype, upb_fieldtype_t vtype); -bool upb_map_init(upb_map *map, upb_fieldtype_t ktype, upb_fieldtype_t vtype, - upb_alloc *a); -void upb_map_uninit(upb_map *map); -upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype, upb_alloc *a); -void upb_map_free(upb_map *map); +/* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function + * *signature*, but without a specific function attached. + * + * These classes contain member functions that can be invoked with a + * specific function to return a Func/BoundFunc class. */ +template +struct FuncSig1 { + template + Func1 > GetFunc() { + return Func1 >(); + } +}; -/* Read-only interface. Safe for anyone to call. */ +template +struct FuncSig2 { + template + Func2 > GetFunc() { + return Func2 >(); + } -size_t upb_map_size(const upb_map *map); -upb_fieldtype_t upb_map_keytype(const upb_map *map); -upb_fieldtype_t upb_map_valuetype(const upb_map *map); -bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val); + template + BoundFunc2 > GetFunc( + typename remove_constptr::type param2) { + return BoundFunc2 >(param2); + } +}; -/* Write interface. May only be called by the message's owner who can enforce - * its memory management invariants. */ +template +struct FuncSig3 { + template + Func3 > GetFunc() { + return Func3 >(); + } -/* Sets or overwrites an entry in the map. Return value indicates whether - * the operation succeeded or failed with OOM, and also whether an existing - * key was replaced or not. */ -bool upb_map_set(upb_map *map, - upb_msgval key, upb_msgval val, - upb_msgval *valremoved); + template + BoundFunc3 > GetFunc( + typename remove_constptr::type param2) { + return BoundFunc3 >(param2); + } +}; -/* Deletes an entry in the map. Returns true if the key was present. */ -bool upb_map_del(upb_map *map, upb_msgval key); +template +struct FuncSig4 { + template + Func4 > GetFunc() { + return Func4 >(); + } + template + BoundFunc4 > GetFunc( + typename remove_constptr::type param2) { + return BoundFunc4 >(param2); + } +}; -/** upb_mapiter ***************************************************************/ +template +struct FuncSig5 { + template + Func5 > GetFunc() { + return Func5 >(); + } -/* For iterating over a map. Map iterators are invalidated by mutations to the - * map, but an invalidated iterator will never return junk or crash the process. - * An invalidated iterator may return entries that were already returned though, - * and if you keep invalidating the iterator during iteration, the program may - * enter an infinite loop. */ + template + BoundFunc5 > GetFunc( + typename remove_constptr::type param2) { + return BoundFunc5 >(param2); + } +}; -size_t upb_mapiter_sizeof(); +/* Overloaded template function that can construct the appropriate FuncSig* + * class given a function pointer by deducing the template parameters. */ +template +inline FuncSig1 MatchFunc(R (*f)(P1)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return FuncSig1(); +} -void upb_mapiter_begin(upb_mapiter *i, const upb_map *t); -upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a); -void upb_mapiter_free(upb_mapiter *i, upb_alloc *a); -void upb_mapiter_next(upb_mapiter *i); -bool upb_mapiter_done(const upb_mapiter *i); +template +inline FuncSig2 MatchFunc(R (*f)(P1, P2)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return FuncSig2(); +} -upb_msgval upb_mapiter_key(const upb_mapiter *i); -upb_msgval upb_mapiter_value(const upb_mapiter *i); -void upb_mapiter_setdone(upb_mapiter *i); -bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2); +template +inline FuncSig3 MatchFunc(R (*f)(P1, P2, P3)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return FuncSig3(); +} +template +inline FuncSig4 MatchFunc(R (*f)(P1, P2, P3, P4)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return FuncSig4(); +} -/** Handlers ******************************************************************/ +template +inline FuncSig5 MatchFunc(R (*f)(P1, P2, P3, P4, P5)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return FuncSig5(); +} -/* These are the handlers used internally by upb_msgfactory_getmergehandlers(). - * They write scalar data to a known offset from the message pointer. - * - * These would be trivial for anyone to implement themselves, but it's better - * to use these because some JITs will recognize and specialize these instead - * of actually calling the function. */ +/* MethodSig ******************************************************************/ -/* Sets a handler for the given primitive field that will write the data at the - * given offset. If hasbit > 0, also sets a hasbit at the given bit offset - * (addressing each byte low to high). */ -bool upb_msg_setscalarhandler(upb_handlers *h, - const upb_fielddef *f, - size_t offset, - int32_t hasbit); +/* CallMethod*: a function template that calls a given method. */ +template +R CallMethod0(C *obj) { + return ((*obj).*F)(); +} -/* If the given handler is a msghandlers_primitive field, returns true and sets - * *type, *offset and *hasbit. Otherwise returns false. */ -bool upb_msg_getscalarhandlerdata(const upb_handlers *h, - upb_selector_t s, - upb_fieldtype_t *type, - size_t *offset, - int32_t *hasbit); +template +R CallMethod1(C *obj, P1 arg1) { + return ((*obj).*F)(arg1); +} +template +R CallMethod2(C *obj, P1 arg1, P2 arg2) { + return ((*obj).*F)(arg1, arg2); +} -/** Interfaces for generated code *********************************************/ +template +R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) { + return ((*obj).*F)(arg1, arg2, arg3); +} -#define UPB_NOT_IN_ONEOF UINT16_MAX -#define UPB_NO_HASBIT UINT16_MAX -#define UPB_NO_SUBMSG UINT16_MAX +template +R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) { + return ((*obj).*F)(arg1, arg2, arg3, arg4); +} -typedef struct { - uint32_t number; - uint32_t offset; /* If in a oneof, offset of default in default_msg below. */ - uint16_t hasbit; /* UPB_NO_HASBIT if no hasbit. */ - uint16_t oneof_index; /* UPB_NOT_IN_ONEOF if not in a oneof. */ - uint16_t submsg_index; /* UPB_NO_SUBMSG if no submsg. */ - uint8_t descriptortype; - uint8_t label; -} upb_msglayout_fieldinit_v1; +/* MethodSig: like FuncSig, but for member functions. + * + * GetFunc() returns a normal FuncN object, so after calling GetFunc() no + * more logic is required to special-case methods. */ +template +struct MethodSig0 { + template + Func1, FuncInfo > GetFunc() { + return Func1, FuncInfo >(); + } +}; -typedef struct { - uint32_t data_offset; - uint32_t case_offset; -} upb_msglayout_oneofinit_v1; - -typedef struct upb_msglayout_msginit_v1 { - const struct upb_msglayout_msginit_v1 *const* submsgs; - const upb_msglayout_fieldinit_v1 *fields; - const upb_msglayout_oneofinit_v1 *oneofs; - void *default_msg; - /* Must be aligned to sizeof(void*). Doesn't include internal members like - * unknown fields, extension dict, pointer to msglayout, etc. */ - uint32_t size; - uint16_t field_count; - uint16_t oneof_count; - bool extendable; - bool is_proto2; -} upb_msglayout_msginit_v1; +template +struct MethodSig1 { + template + Func2, FuncInfo > GetFunc() { + return Func2, FuncInfo >(); + } -#define UPB_ALIGN_UP_TO(val, align) ((val + (align - 1)) & -align) -#define UPB_ALIGNED_SIZEOF(type) UPB_ALIGN_UP_TO(sizeof(type), sizeof(void*)) + template + BoundFunc2, FuncInfo > GetFunc( + typename remove_constptr::type param1) { + return BoundFunc2, FuncInfo >( + param1); + } +}; -/* Initialize/uninitialize a msglayout from a msginit. If upb uses v1 - * internally, this will not allocate any memory. Should only be used by - * generated code. */ -upb_msglayout *upb_msglayout_frominit_v1( - const upb_msglayout_msginit_v1 *init, upb_alloc *a); -void upb_msglayout_uninit_v1(upb_msglayout *layout, upb_alloc *a); +template +struct MethodSig2 { + template + Func3, FuncInfo > + GetFunc() { + return Func3, + FuncInfo >(); + } -UPB_END_EXTERN_C + template + BoundFunc3, FuncInfo > + GetFunc(typename remove_constptr::type param1) { + return BoundFunc3, + FuncInfo >(param1); + } +}; -#endif /* UPB_MSG_H_ */ +template +struct MethodSig3 { + template + Func4, FuncInfo > + GetFunc() { + return Func4, + FuncInfo >(); + } -UPB_BEGIN_EXTERN_C + template + BoundFunc4, + FuncInfo > + GetFunc(typename remove_constptr::type param1) { + return BoundFunc4, + FuncInfo >(param1); + } +}; -bool upb_decode(upb_stringview buf, void *msg, - const upb_msglayout_msginit_v1 *l, upb_env *env); +template +struct MethodSig4 { + template + Func5, + FuncInfo > + GetFunc() { + return Func5, + FuncInfo >(); + } -UPB_END_EXTERN_C + template + BoundFunc5, + FuncInfo > + GetFunc(typename remove_constptr::type param1) { + return BoundFunc5, FuncInfo >( + param1); + } +}; -#endif /* UPB_DECODE_H_ */ -/* -** upb_encode: parsing into a upb_msg using a upb_msglayout. -*/ +template +inline MethodSig0 MatchFunc(R (C::*f)()) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return MethodSig0(); +} -#ifndef UPB_ENCODE_H_ -#define UPB_ENCODE_H_ +template +inline MethodSig1 MatchFunc(R (C::*f)(P1)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return MethodSig1(); +} +template +inline MethodSig2 MatchFunc(R (C::*f)(P1, P2)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return MethodSig2(); +} -UPB_BEGIN_EXTERN_C +template +inline MethodSig3 MatchFunc(R (C::*f)(P1, P2, P3)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return MethodSig3(); +} -char *upb_encode(const void *msg, const upb_msglayout_msginit_v1 *l, - upb_env *env, size_t *size); +template +inline MethodSig4 MatchFunc(R (C::*f)(P1, P2, P3, P4)) { + UPB_UNUSED(f); /* Only used for template parameter deduction. */ + return MethodSig4(); +} -UPB_END_EXTERN_C +/* MaybeWrapReturn ************************************************************/ -#endif /* UPB_ENCODE_H_ */ -/* This file was generated by upbc (the upb compiler) from the input - * file: +/* Template class that attempts to wrap the return value of the function so it + * matches the expected type. There are two main adjustments it may make: * - * google/protobuf/descriptor.proto + * 1. If the function returns void, make it return the expected type and with + * a value that always indicates success. + * 2. If the function returns bool, make it return the expected type with a + * value that indicates success or failure. * - * Do not edit -- your changes will be discarded when the file is - * regenerated. */ + * The "expected type" for return is: + * 1. void* for start handlers. If the closure parameter has a different type + * we will cast it to void* for the return in the success case. + * 2. size_t for string buffer handlers. + * 3. bool for everything else. */ -#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ -#define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ +/* Template parameters are FuncN type and desired return type. */ +template +struct MaybeWrapReturn; +/* If the return type matches, return the given function unwrapped. */ +template +struct MaybeWrapReturn { + typedef F Func; +}; -UPB_BEGIN_EXTERN_C +/* Function wrapper that munges the return value from void to (bool)true. */ +template +bool ReturnTrue2(P1 p1, P2 p2) { + F(p1, p2); + return true; +} -struct google_protobuf_FileDescriptorSet; -typedef struct google_protobuf_FileDescriptorSet google_protobuf_FileDescriptorSet; -struct google_protobuf_FileDescriptorProto; -typedef struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto; -struct google_protobuf_DescriptorProto; -typedef struct google_protobuf_DescriptorProto google_protobuf_DescriptorProto; -struct google_protobuf_DescriptorProto_ExtensionRange; -typedef struct google_protobuf_DescriptorProto_ExtensionRange google_protobuf_DescriptorProto_ExtensionRange; -struct google_protobuf_DescriptorProto_ReservedRange; -typedef struct google_protobuf_DescriptorProto_ReservedRange google_protobuf_DescriptorProto_ReservedRange; -struct google_protobuf_ExtensionRangeOptions; -typedef struct google_protobuf_ExtensionRangeOptions google_protobuf_ExtensionRangeOptions; -struct google_protobuf_FieldDescriptorProto; -typedef struct google_protobuf_FieldDescriptorProto google_protobuf_FieldDescriptorProto; -struct google_protobuf_OneofDescriptorProto; -typedef struct google_protobuf_OneofDescriptorProto google_protobuf_OneofDescriptorProto; -struct google_protobuf_EnumDescriptorProto; -typedef struct google_protobuf_EnumDescriptorProto google_protobuf_EnumDescriptorProto; -struct google_protobuf_EnumDescriptorProto_EnumReservedRange; -typedef struct google_protobuf_EnumDescriptorProto_EnumReservedRange google_protobuf_EnumDescriptorProto_EnumReservedRange; -struct google_protobuf_EnumValueDescriptorProto; -typedef struct google_protobuf_EnumValueDescriptorProto google_protobuf_EnumValueDescriptorProto; -struct google_protobuf_ServiceDescriptorProto; -typedef struct google_protobuf_ServiceDescriptorProto google_protobuf_ServiceDescriptorProto; -struct google_protobuf_MethodDescriptorProto; -typedef struct google_protobuf_MethodDescriptorProto google_protobuf_MethodDescriptorProto; -struct google_protobuf_FileOptions; -typedef struct google_protobuf_FileOptions google_protobuf_FileOptions; -struct google_protobuf_MessageOptions; -typedef struct google_protobuf_MessageOptions google_protobuf_MessageOptions; -struct google_protobuf_FieldOptions; -typedef struct google_protobuf_FieldOptions google_protobuf_FieldOptions; -struct google_protobuf_OneofOptions; -typedef struct google_protobuf_OneofOptions google_protobuf_OneofOptions; -struct google_protobuf_EnumOptions; -typedef struct google_protobuf_EnumOptions google_protobuf_EnumOptions; -struct google_protobuf_EnumValueOptions; -typedef struct google_protobuf_EnumValueOptions google_protobuf_EnumValueOptions; -struct google_protobuf_ServiceOptions; -typedef struct google_protobuf_ServiceOptions google_protobuf_ServiceOptions; -struct google_protobuf_MethodOptions; -typedef struct google_protobuf_MethodOptions google_protobuf_MethodOptions; -struct google_protobuf_UninterpretedOption; -typedef struct google_protobuf_UninterpretedOption google_protobuf_UninterpretedOption; -struct google_protobuf_UninterpretedOption_NamePart; -typedef struct google_protobuf_UninterpretedOption_NamePart google_protobuf_UninterpretedOption_NamePart; -struct google_protobuf_SourceCodeInfo; -typedef struct google_protobuf_SourceCodeInfo google_protobuf_SourceCodeInfo; -struct google_protobuf_SourceCodeInfo_Location; -typedef struct google_protobuf_SourceCodeInfo_Location google_protobuf_SourceCodeInfo_Location; -struct google_protobuf_GeneratedCodeInfo; -typedef struct google_protobuf_GeneratedCodeInfo google_protobuf_GeneratedCodeInfo; -struct google_protobuf_GeneratedCodeInfo_Annotation; -typedef struct google_protobuf_GeneratedCodeInfo_Annotation google_protobuf_GeneratedCodeInfo_Annotation; -/* Enums */ +template +bool ReturnTrue3(P1 p1, P2 p2, P3 p3) { + F(p1, p2, p3); + return true; +} -typedef enum { - google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1, - google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2, - google_protobuf_FieldDescriptorProto_LABEL_REPEATED = 3 -} google_protobuf_FieldDescriptorProto_Label; +/* Function wrapper that munges the return value from void to (void*)arg1 */ +template +void *ReturnClosure2(P1 p1, P2 p2) { + F(p1, p2); + return p1; +} -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; +template +void *ReturnClosure3(P1 p1, P2 p2, P3 p3) { + F(p1, p2, p3); + return p1; +} -typedef enum { - google_protobuf_FieldOptions_STRING = 0, - google_protobuf_FieldOptions_CORD = 1, - google_protobuf_FieldOptions_STRING_PIECE = 2 -} google_protobuf_FieldOptions_CType; +/* Function wrapper that munges the return value from R to void*. */ +template +void *CastReturnToVoidPtr2(P1 p1, P2 p2) { + return F(p1, p2); +} -typedef enum { - google_protobuf_FieldOptions_JS_NORMAL = 0, - google_protobuf_FieldOptions_JS_STRING = 1, - google_protobuf_FieldOptions_JS_NUMBER = 2 -} google_protobuf_FieldOptions_JSType; +template +void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) { + return F(p1, p2, p3); +} -typedef enum { - google_protobuf_FileOptions_SPEED = 1, - google_protobuf_FileOptions_CODE_SIZE = 2, - google_protobuf_FileOptions_LITE_RUNTIME = 3 -} google_protobuf_FileOptions_OptimizeMode; +/* Function wrapper that munges the return value from bool to void*. */ +template +void *ReturnClosureOrBreak2(P1 p1, P2 p2) { + return F(p1, p2) ? p1 : UPB_BREAK; +} -typedef enum { - google_protobuf_MethodOptions_IDEMPOTENCY_UNKNOWN = 0, - google_protobuf_MethodOptions_NO_SIDE_EFFECTS = 1, - google_protobuf_MethodOptions_IDEMPOTENT = 2 -} google_protobuf_MethodOptions_IdempotencyLevel; +template +void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) { + return F(p1, p2, p3) ? p1 : UPB_BREAK; +} -/* google_protobuf_FileDescriptorSet */ -extern const upb_msglayout_msginit_v1 google_protobuf_FileDescriptorSet_msginit; -google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_env *env); -google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_FileDescriptorSet_serialize(google_protobuf_FileDescriptorSet *msg, upb_env *env, size_t *len); -void google_protobuf_FileDescriptorSet_free(google_protobuf_FileDescriptorSet *msg, upb_env *env); - -/* getters. */ -const upb_array* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg); - -/* setters. */ -void google_protobuf_FileDescriptorSet_set_file(google_protobuf_FileDescriptorSet *msg, upb_array* value); - - -/* google_protobuf_FileDescriptorProto */ -extern const upb_msglayout_msginit_v1 google_protobuf_FileDescriptorProto_msginit; -google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_env *env); -google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_FileDescriptorProto_serialize(google_protobuf_FileDescriptorProto *msg, upb_env *env, size_t *len); -void google_protobuf_FileDescriptorProto_free(google_protobuf_FileDescriptorProto *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg); -upb_stringview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg); -const upb_array* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg); -const upb_array* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg); -const upb_array* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg); -const upb_array* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg); -const upb_array* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg); -const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg); -const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg); -const upb_array* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg); -const upb_array* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg); -upb_stringview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg); - -/* setters. */ -void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_stringview value); -void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_stringview value); -void google_protobuf_FileDescriptorProto_set_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value); -void google_protobuf_FileDescriptorProto_set_message_type(google_protobuf_FileDescriptorProto *msg, upb_array* value); -void google_protobuf_FileDescriptorProto_set_enum_type(google_protobuf_FileDescriptorProto *msg, upb_array* value); -void google_protobuf_FileDescriptorProto_set_service(google_protobuf_FileDescriptorProto *msg, upb_array* value); -void google_protobuf_FileDescriptorProto_set_extension(google_protobuf_FileDescriptorProto *msg, upb_array* value); -void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value); -void google_protobuf_FileDescriptorProto_set_source_code_info(google_protobuf_FileDescriptorProto *msg, google_protobuf_SourceCodeInfo* value); -void google_protobuf_FileDescriptorProto_set_public_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value); -void google_protobuf_FileDescriptorProto_set_weak_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value); -void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_stringview value); - - -/* google_protobuf_DescriptorProto */ -extern const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_msginit; -google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_env *env); -google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_DescriptorProto_serialize(google_protobuf_DescriptorProto *msg, upb_env *env, size_t *len); -void google_protobuf_DescriptorProto_free(google_protobuf_DescriptorProto *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg); -const upb_array* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg); -const upb_array* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg); -const upb_array* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg); -const upb_array* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg); -const upb_array* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg); -const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg); -const upb_array* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg); -const upb_array* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg); -const upb_array* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg); - -/* setters. */ -void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_stringview value); -void google_protobuf_DescriptorProto_set_field(google_protobuf_DescriptorProto *msg, upb_array* value); -void google_protobuf_DescriptorProto_set_nested_type(google_protobuf_DescriptorProto *msg, upb_array* value); -void google_protobuf_DescriptorProto_set_enum_type(google_protobuf_DescriptorProto *msg, upb_array* value); -void google_protobuf_DescriptorProto_set_extension_range(google_protobuf_DescriptorProto *msg, upb_array* value); -void google_protobuf_DescriptorProto_set_extension(google_protobuf_DescriptorProto *msg, upb_array* value); -void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value); -void google_protobuf_DescriptorProto_set_oneof_decl(google_protobuf_DescriptorProto *msg, upb_array* value); -void google_protobuf_DescriptorProto_set_reserved_range(google_protobuf_DescriptorProto *msg, upb_array* value); -void google_protobuf_DescriptorProto_set_reserved_name(google_protobuf_DescriptorProto *msg, upb_array* value); - - -/* google_protobuf_DescriptorProto_ExtensionRange */ -extern const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_ExtensionRange_msginit; -google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_env *env); -google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_DescriptorProto_ExtensionRange_serialize(google_protobuf_DescriptorProto_ExtensionRange *msg, upb_env *env, size_t *len); -void google_protobuf_DescriptorProto_ExtensionRange_free(google_protobuf_DescriptorProto_ExtensionRange *msg, upb_env *env); - -/* getters. */ -int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg); -int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg); -const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg); - -/* setters. */ -void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value); -void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value); -void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value); - - -/* google_protobuf_DescriptorProto_ReservedRange */ -extern const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_ReservedRange_msginit; -google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_env *env); -google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_DescriptorProto_ReservedRange_serialize(google_protobuf_DescriptorProto_ReservedRange *msg, upb_env *env, size_t *len); -void google_protobuf_DescriptorProto_ReservedRange_free(google_protobuf_DescriptorProto_ReservedRange *msg, upb_env *env); - -/* getters. */ -int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg); -int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg); - -/* setters. */ -void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value); -void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value); - - -/* google_protobuf_ExtensionRangeOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_ExtensionRangeOptions_msginit; -google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_env *env); -google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_ExtensionRangeOptions_serialize(google_protobuf_ExtensionRangeOptions *msg, upb_env *env, size_t *len); -void google_protobuf_ExtensionRangeOptions_free(google_protobuf_ExtensionRangeOptions *msg, upb_env *env); - -/* getters. */ -const upb_array* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg); - -/* setters. */ -void google_protobuf_ExtensionRangeOptions_set_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_array* value); - - -/* google_protobuf_FieldDescriptorProto */ -extern const upb_msglayout_msginit_v1 google_protobuf_FieldDescriptorProto_msginit; -google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_env *env); -google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_FieldDescriptorProto_serialize(google_protobuf_FieldDescriptorProto *msg, upb_env *env, size_t *len); -void google_protobuf_FieldDescriptorProto_free(google_protobuf_FieldDescriptorProto *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg); -upb_stringview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg); -int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg); -google_protobuf_FieldDescriptorProto_Label google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg); -google_protobuf_FieldDescriptorProto_Type google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg); -upb_stringview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg); -upb_stringview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg); -const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg); -int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg); -upb_stringview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg); - -/* setters. */ -void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value); -void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_stringview value); -void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value); -void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Label value); -void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Type value); -void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value); -void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_stringview value); -void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value); -void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto *msg, int32_t value); -void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value); - - -/* google_protobuf_OneofDescriptorProto */ -extern const upb_msglayout_msginit_v1 google_protobuf_OneofDescriptorProto_msginit; -google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_env *env); -google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_OneofDescriptorProto_serialize(google_protobuf_OneofDescriptorProto *msg, upb_env *env, size_t *len); -void google_protobuf_OneofDescriptorProto_free(google_protobuf_OneofDescriptorProto *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg); -const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg); - -/* setters. */ -void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_stringview value); -void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value); - - -/* google_protobuf_EnumDescriptorProto */ -extern const upb_msglayout_msginit_v1 google_protobuf_EnumDescriptorProto_msginit; -google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_env *env); -google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_EnumDescriptorProto_serialize(google_protobuf_EnumDescriptorProto *msg, upb_env *env, size_t *len); -void google_protobuf_EnumDescriptorProto_free(google_protobuf_EnumDescriptorProto *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg); -const upb_array* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg); -const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg); -const upb_array* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg); -const upb_array* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg); - -/* setters. */ -void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_stringview value); -void google_protobuf_EnumDescriptorProto_set_value(google_protobuf_EnumDescriptorProto *msg, upb_array* value); -void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value); -void google_protobuf_EnumDescriptorProto_set_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_array* value); -void google_protobuf_EnumDescriptorProto_set_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_array* value); - - -/* google_protobuf_EnumDescriptorProto_EnumReservedRange */ -extern const upb_msglayout_msginit_v1 google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit; -google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_env *env); -google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_env *env, size_t *len); -void google_protobuf_EnumDescriptorProto_EnumReservedRange_free(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_env *env); - -/* getters. */ -int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg); -int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg); - -/* setters. */ -void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value); -void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value); - - -/* google_protobuf_EnumValueDescriptorProto */ -extern const upb_msglayout_msginit_v1 google_protobuf_EnumValueDescriptorProto_msginit; -google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_env *env); -google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_EnumValueDescriptorProto_serialize(google_protobuf_EnumValueDescriptorProto *msg, upb_env *env, size_t *len); -void google_protobuf_EnumValueDescriptorProto_free(google_protobuf_EnumValueDescriptorProto *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg); -int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg); -const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg); - -/* setters. */ -void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_stringview value); -void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value); -void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value); - - -/* google_protobuf_ServiceDescriptorProto */ -extern const upb_msglayout_msginit_v1 google_protobuf_ServiceDescriptorProto_msginit; -google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_env *env); -google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_ServiceDescriptorProto_serialize(google_protobuf_ServiceDescriptorProto *msg, upb_env *env, size_t *len); -void google_protobuf_ServiceDescriptorProto_free(google_protobuf_ServiceDescriptorProto *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg); -const upb_array* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg); -const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg); - -/* setters. */ -void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_stringview value); -void google_protobuf_ServiceDescriptorProto_set_method(google_protobuf_ServiceDescriptorProto *msg, upb_array* value); -void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value); - - -/* google_protobuf_MethodDescriptorProto */ -extern const upb_msglayout_msginit_v1 google_protobuf_MethodDescriptorProto_msginit; -google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_env *env); -google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_MethodDescriptorProto_serialize(google_protobuf_MethodDescriptorProto *msg, upb_env *env, size_t *len); -void google_protobuf_MethodDescriptorProto_free(google_protobuf_MethodDescriptorProto *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg); -upb_stringview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg); -upb_stringview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg); -const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg); -bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg); -bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg); - -/* setters. */ -void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_stringview value); -void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_stringview value); -void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_stringview value); -void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value); -void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value); -void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value); - - -/* google_protobuf_FileOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_FileOptions_msginit; -google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_env *env); -google_protobuf_FileOptions *google_protobuf_FileOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_FileOptions_serialize(google_protobuf_FileOptions *msg, upb_env *env, size_t *len); -void google_protobuf_FileOptions_free(google_protobuf_FileOptions *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg); -upb_stringview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg); -google_protobuf_FileOptions_OptimizeMode google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg); -upb_stringview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg); -upb_stringview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg); -upb_stringview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg); -upb_stringview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg); -upb_stringview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg); -upb_stringview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg); -bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg); -const upb_array* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg); - -/* setters. */ -void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_stringview value); -void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_stringview value); -void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, google_protobuf_FileOptions_OptimizeMode value); -void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_stringview value); -void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_stringview value); -void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_stringview value); -void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_stringview value); -void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_stringview value); -void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_stringview value); -void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value); -void google_protobuf_FileOptions_set_uninterpreted_option(google_protobuf_FileOptions *msg, upb_array* value); - - -/* google_protobuf_MessageOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_MessageOptions_msginit; -google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_env *env); -google_protobuf_MessageOptions *google_protobuf_MessageOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_MessageOptions_serialize(google_protobuf_MessageOptions *msg, upb_env *env, size_t *len); -void google_protobuf_MessageOptions_free(google_protobuf_MessageOptions *msg, upb_env *env); - -/* getters. */ -bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg); -bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg); -bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg); -bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg); -const upb_array* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg); - -/* setters. */ -void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value); -void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value); -void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value); -void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value); -void google_protobuf_MessageOptions_set_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_array* value); - - -/* google_protobuf_FieldOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_FieldOptions_msginit; -google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_env *env); -google_protobuf_FieldOptions *google_protobuf_FieldOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_FieldOptions_serialize(google_protobuf_FieldOptions *msg, upb_env *env, size_t *len); -void google_protobuf_FieldOptions_free(google_protobuf_FieldOptions *msg, upb_env *env); - -/* getters. */ -google_protobuf_FieldOptions_CType google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg); -bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg); -bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg); -bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg); -google_protobuf_FieldOptions_JSType google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg); -bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg); -const upb_array* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg); - -/* setters. */ -void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_CType value); -void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value); -void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value); -void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value); -void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_JSType value); -void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value); -void google_protobuf_FieldOptions_set_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_array* value); - - -/* google_protobuf_OneofOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_OneofOptions_msginit; -google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_env *env); -google_protobuf_OneofOptions *google_protobuf_OneofOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_OneofOptions_serialize(google_protobuf_OneofOptions *msg, upb_env *env, size_t *len); -void google_protobuf_OneofOptions_free(google_protobuf_OneofOptions *msg, upb_env *env); - -/* getters. */ -const upb_array* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg); - -/* setters. */ -void google_protobuf_OneofOptions_set_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_array* value); - - -/* google_protobuf_EnumOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_EnumOptions_msginit; -google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_env *env); -google_protobuf_EnumOptions *google_protobuf_EnumOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_EnumOptions_serialize(google_protobuf_EnumOptions *msg, upb_env *env, size_t *len); -void google_protobuf_EnumOptions_free(google_protobuf_EnumOptions *msg, upb_env *env); - -/* getters. */ -bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg); -bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg); -const upb_array* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg); - -/* setters. */ -void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value); -void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value); -void google_protobuf_EnumOptions_set_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_array* value); - - -/* google_protobuf_EnumValueOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_EnumValueOptions_msginit; -google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_env *env); -google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_EnumValueOptions_serialize(google_protobuf_EnumValueOptions *msg, upb_env *env, size_t *len); -void google_protobuf_EnumValueOptions_free(google_protobuf_EnumValueOptions *msg, upb_env *env); - -/* getters. */ -bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg); -const upb_array* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg); - -/* setters. */ -void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value); -void google_protobuf_EnumValueOptions_set_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_array* value); - - -/* google_protobuf_ServiceOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_ServiceOptions_msginit; -google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_env *env); -google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_ServiceOptions_serialize(google_protobuf_ServiceOptions *msg, upb_env *env, size_t *len); -void google_protobuf_ServiceOptions_free(google_protobuf_ServiceOptions *msg, upb_env *env); - -/* getters. */ -bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg); -const upb_array* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg); - -/* setters. */ -void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value); -void google_protobuf_ServiceOptions_set_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_array* value); - - -/* google_protobuf_MethodOptions */ -extern const upb_msglayout_msginit_v1 google_protobuf_MethodOptions_msginit; -google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_env *env); -google_protobuf_MethodOptions *google_protobuf_MethodOptions_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_MethodOptions_serialize(google_protobuf_MethodOptions *msg, upb_env *env, size_t *len); -void google_protobuf_MethodOptions_free(google_protobuf_MethodOptions *msg, upb_env *env); - -/* getters. */ -bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg); -google_protobuf_MethodOptions_IdempotencyLevel google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg); -const upb_array* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg); - -/* setters. */ -void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value); -void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, google_protobuf_MethodOptions_IdempotencyLevel value); -void google_protobuf_MethodOptions_set_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_array* value); - - -/* google_protobuf_UninterpretedOption */ -extern const upb_msglayout_msginit_v1 google_protobuf_UninterpretedOption_msginit; -google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_env *env); -google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_UninterpretedOption_serialize(google_protobuf_UninterpretedOption *msg, upb_env *env, size_t *len); -void google_protobuf_UninterpretedOption_free(google_protobuf_UninterpretedOption *msg, upb_env *env); - -/* getters. */ -const upb_array* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg); -upb_stringview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg); -uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg); -int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg); -double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg); -upb_stringview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg); -upb_stringview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg); - -/* setters. */ -void google_protobuf_UninterpretedOption_set_name(google_protobuf_UninterpretedOption *msg, upb_array* value); -void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_stringview value); -void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value); -void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value); -void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value); -void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_stringview value); -void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_stringview value); - - -/* google_protobuf_UninterpretedOption_NamePart */ -extern const upb_msglayout_msginit_v1 google_protobuf_UninterpretedOption_NamePart_msginit; -google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_env *env); -google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_UninterpretedOption_NamePart_serialize(google_protobuf_UninterpretedOption_NamePart *msg, upb_env *env, size_t *len); -void google_protobuf_UninterpretedOption_NamePart_free(google_protobuf_UninterpretedOption_NamePart *msg, upb_env *env); - -/* getters. */ -upb_stringview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg); -bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg); - -/* setters. */ -void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_stringview value); -void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value); - - -/* google_protobuf_SourceCodeInfo */ -extern const upb_msglayout_msginit_v1 google_protobuf_SourceCodeInfo_msginit; -google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_env *env); -google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_SourceCodeInfo_serialize(google_protobuf_SourceCodeInfo *msg, upb_env *env, size_t *len); -void google_protobuf_SourceCodeInfo_free(google_protobuf_SourceCodeInfo *msg, upb_env *env); - -/* getters. */ -const upb_array* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg); - -/* setters. */ -void google_protobuf_SourceCodeInfo_set_location(google_protobuf_SourceCodeInfo *msg, upb_array* value); - - -/* google_protobuf_SourceCodeInfo_Location */ -extern const upb_msglayout_msginit_v1 google_protobuf_SourceCodeInfo_Location_msginit; -google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_env *env); -google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_SourceCodeInfo_Location_serialize(google_protobuf_SourceCodeInfo_Location *msg, upb_env *env, size_t *len); -void google_protobuf_SourceCodeInfo_Location_free(google_protobuf_SourceCodeInfo_Location *msg, upb_env *env); - -/* getters. */ -const upb_array* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg); -const upb_array* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg); -upb_stringview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg); -upb_stringview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg); -const upb_array* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg); - -/* setters. */ -void google_protobuf_SourceCodeInfo_Location_set_path(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value); -void google_protobuf_SourceCodeInfo_Location_set_span(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value); -void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_stringview value); -void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_stringview value); -void google_protobuf_SourceCodeInfo_Location_set_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value); - - -/* google_protobuf_GeneratedCodeInfo */ -extern const upb_msglayout_msginit_v1 google_protobuf_GeneratedCodeInfo_msginit; -google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_env *env); -google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_GeneratedCodeInfo_serialize(google_protobuf_GeneratedCodeInfo *msg, upb_env *env, size_t *len); -void google_protobuf_GeneratedCodeInfo_free(google_protobuf_GeneratedCodeInfo *msg, upb_env *env); - -/* getters. */ -const upb_array* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg); - -/* setters. */ -void google_protobuf_GeneratedCodeInfo_set_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_array* value); - - -/* google_protobuf_GeneratedCodeInfo_Annotation */ -extern const upb_msglayout_msginit_v1 google_protobuf_GeneratedCodeInfo_Annotation_msginit; -google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_env *env); -google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parsenew(upb_stringview buf, upb_env *env); -char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_env *env, size_t *len); -void google_protobuf_GeneratedCodeInfo_Annotation_free(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_env *env); - -/* getters. */ -const upb_array* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg); -upb_stringview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg); -int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg); -int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg); - -/* setters. */ -void google_protobuf_GeneratedCodeInfo_Annotation_set_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_array* value); -void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_stringview value); -void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value); -void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value); - - -UPB_END_EXTERN_C +/* For the string callback, which takes five params, returns the size param. */ +template +size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4, + const upb_bufhandle *p5) { + F(p1, p2, p3, p4, p5); + return p4; +} -#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ */ -/* -** structs.int.h: structures definitions that are internal to upb. -*/ +/* For the string callback, which takes five params, returns the size param or + * zero. */ +template +size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4, + const upb_bufhandle *p5) { + return F(p1, p2, p3, p4, p5) ? p4 : 0; +} -#ifndef UPB_STRUCTS_H_ -#define UPB_STRUCTS_H_ +/* If we have a function returning void but want a function returning bool, wrap + * it in a function that returns true. */ +template +struct MaybeWrapReturn, bool> { + typedef Func2, I> Func; +}; -struct upb_array { - upb_fieldtype_t type; - uint8_t element_size; - void *data; /* Each element is element_size. */ - size_t len; /* Measured in elements. */ - size_t size; /* Measured in elements. */ - upb_alloc *alloc; +template +struct MaybeWrapReturn, bool> { + typedef Func3, I> Func; }; -#endif /* UPB_STRUCTS_H_ */ +/* If our function returns void but we want one returning void*, wrap it in a + * function that returns the first argument. */ +template +struct MaybeWrapReturn, void *> { + typedef Func2, I> Func; +}; -/* -** This file contains definitions of structs that should be considered private -** and NOT stable across versions of upb. -** -** The only reason they are declared here and not in .c files is to allow upb -** and the application (if desired) to embed statically-initialized instances -** of structures like defs. -** -** If you include this file, all guarantees of ABI compatibility go out the -** window! Any code that includes this file needs to recompile against the -** exact same version of upb that they are linking against. -** -** You also need to recompile if you change the value of the UPB_DEBUG_REFS -** flag. -*/ +template +struct MaybeWrapReturn, void *> { + typedef Func3, I> Func; +}; +/* If our function returns R* but we want one returning void*, wrap it in a + * function that casts to void*. */ +template +struct MaybeWrapReturn, void *, + typename disable_if_same::Type> { + typedef Func2, I> Func; +}; -#ifndef UPB_STATICINIT_H_ -#define UPB_STATICINIT_H_ +template +struct MaybeWrapReturn, void *, + typename disable_if_same::Type> { + typedef Func3, I> + Func; +}; -#ifdef __cplusplus -/* Because of how we do our typedefs, this header can't be included from C++. */ -#error This file cannot be included from C++ -#endif +/* If our function returns bool but we want one returning void*, wrap it in a + * function that returns either the first param or UPB_BREAK. */ +template +struct MaybeWrapReturn, void *> { + typedef Func2, I> Func; +}; -/* upb_refcounted *************************************************************/ +template +struct MaybeWrapReturn, void *> { + typedef Func3, I> + Func; +}; +/* If our function returns void but we want one returning size_t, wrap it in a + * function that returns the size argument. */ +template +struct MaybeWrapReturn< + Func5, + size_t> { + typedef Func5, I> Func; +}; -/* upb_def ********************************************************************/ +/* If our function returns bool but we want one returning size_t, wrap it in a + * function that returns either 0 or the buf size. */ +template +struct MaybeWrapReturn< + Func5, + size_t> { + typedef Func5, I> Func; +}; -struct upb_def { - upb_refcounted base; +/* ConvertParams **************************************************************/ - const char *fullname; - const upb_filedef* file; - char type; /* A upb_deftype_t (char to save space) */ +/* Template class that converts the function parameters if necessary, and + * ignores the HandlerData parameter if appropriate. + * + * Template parameter is the are FuncN function type. */ +template +struct ConvertParams; - /* Used as a flag during the def's mutable stage. Must be false unless - * it is currently being used by a function on the stack. This allows - * us to easily determine which defs were passed into the function's - * current invocation. */ - bool came_from_user; -}; +/* Function that discards the handler data parameter. */ +template +R IgnoreHandlerData2(void *p1, const void *hd) { + UPB_UNUSED(hd); + return F(static_cast(p1)); +} -#define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \ - { UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false } +template +R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) { + UPB_UNUSED(hd); + return F(static_cast(p1), p2); +} +template +R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) { + UPB_UNUSED(hd); + return F(static_cast(p1), p2, p3); +} -/* upb_fielddef ***************************************************************/ +template +R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) { + UPB_UNUSED(hd); + return F(static_cast(p1), p2, p3, p4); +} -struct upb_fielddef { - upb_def base; +template +R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2, + size_t p3, const upb_bufhandle *handle) { + UPB_UNUSED(hd); + UPB_UNUSED(handle); + return F(static_cast(p1), p2, p3); +} - union { - int64_t sint; - uint64_t uint; - double dbl; - float flt; - void *bytes; - } defaultval; - union { - const upb_msgdef *def; /* If !msg_is_symbolic. */ - char *name; /* If msg_is_symbolic. */ - } msg; - union { - const upb_def *def; /* If !subdef_is_symbolic. */ - char *name; /* If subdef_is_symbolic. */ - } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */ - bool subdef_is_symbolic; - bool msg_is_symbolic; - const upb_oneofdef *oneof; - bool default_is_string; - bool type_is_set_; /* False until type is explicitly set. */ - bool is_extension_; - bool lazy_; - bool packed_; - upb_intfmt_t intfmt; - bool tagdelim; - upb_fieldtype_t type_; - upb_label_t label_; - uint32_t number_; - uint32_t selector_base; /* Used to index into a upb::Handlers table. */ - uint32_t index_; -}; +/* Function that casts the handler data parameter. */ +template +R CastHandlerData2(void *c, const void *hd) { + return F(static_cast(c), static_cast(hd)); +} -extern const struct upb_refcounted_vtbl upb_fielddef_vtbl; +template +R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) { + return F(static_cast(c), static_cast(hd), p3); +} -#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \ - packed, name, num, msgdef, subdef, selector_base, \ - index, defaultval, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s), \ - defaultval, {msgdef}, {subdef}, NULL, false, false, \ - type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \ - lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \ - } +template +R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) { + return F(static_cast(c), static_cast(hd), p3, p4, p5); +} +template +R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3, + size_t p4, const upb_bufhandle *handle) { + UPB_UNUSED(handle); + return F(static_cast(c), static_cast(hd), p3, p4); +} -/* upb_msgdef *****************************************************************/ +/* For unbound functions, ignore the handler data. */ +template +struct ConvertParams, T> { + typedef Func2, I> Func; +}; -struct upb_msgdef { - upb_def base; +template +struct ConvertParams, + R2 (*)(P1_2, P2_2, P3_2)> { + typedef Func3, I> Func; +}; - size_t selector_count; - uint32_t submsg_field_count; +/* For StringBuffer only; this ignores both the handler data and the + * upb_bufhandle. */ +template +struct ConvertParams, T> { + typedef Func5, + I> Func; +}; - /* Tables for looking up fields by number and name. */ - upb_inttable itof; /* int to field */ - upb_strtable ntof; /* name to field/oneof */ +template +struct ConvertParams, T> { + typedef Func5, I> Func; +}; - /* Is this a map-entry message? */ - bool map_entry; +/* For bound functions, cast the handler data. */ +template +struct ConvertParams, T> { + typedef Func2, I> + Func; +}; - /* Whether this message has proto2 or proto3 semantics. */ - upb_syntax_t syntax; +template +struct ConvertParams, + R2 (*)(P1_2, P2_2, P3_2)> { + typedef Func3, I> Func; +}; - /* TODO(haberman): proper extension ranges (there can be multiple). */ +/* For StringBuffer only; this ignores the upb_bufhandle. */ +template +struct ConvertParams, T> { + typedef Func5, I> + Func; }; -extern const struct upb_refcounted_vtbl upb_msgdef_vtbl; +template +struct ConvertParams, T> { + typedef Func5, I> Func; +}; -/* TODO: also support static initialization of the oneofs table. This will be - * needed if we compile in descriptors that contain oneofs. */ -#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \ - map_entry, syntax, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s), \ - selector_count, submsg_field_count, itof, ntof, map_entry, syntax \ +/* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is + * variant C type. */ +#define TYPE_METHODS(utype, ltype, ctype, vtype) \ + template <> \ + struct CanonicalType { \ + typedef ctype Type; \ + }; \ + template <> \ + inline bool HandlersPtr::SetValueHandler( \ + FieldDefPtr f, const HandlersPtr::utype##Handler &handler) { \ + handler.AddCleanup(ptr()); \ + return upb_handlers_set##ltype(ptr(), f.ptr(), handler.handler(), \ + &handler.attr()); \ } +TYPE_METHODS(Double, double, double, double) +TYPE_METHODS(Float, float, float, float) +TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T) +TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T) +TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T) +TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T) +TYPE_METHODS(Bool, bool, bool, bool) -/* upb_enumdef ****************************************************************/ +#ifdef UPB_TWO_32BIT_TYPES +TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T) +TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T) +#endif -struct upb_enumdef { - upb_def base; +#ifdef UPB_TWO_64BIT_TYPES +TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T) +TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T) +#endif +#undef TYPE_METHODS - upb_strtable ntoi; - upb_inttable iton; - int32_t defaultval; +template <> struct CanonicalType { + typedef Status* Type; }; -extern const struct upb_refcounted_vtbl upb_enumdef_vtbl; - -#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \ - { UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi, \ - iton, defaultval } +template struct ReturnOf; +template +struct ReturnOf { + typedef R Return; +}; -/* upb_oneofdef ***************************************************************/ +template +struct ReturnOf { + typedef R Return; +}; -struct upb_oneofdef { - upb_refcounted base; +template +struct ReturnOf { + typedef R Return; +}; - uint32_t index; /* Index within oneofs. */ - const char *name; - upb_strtable ntof; - upb_inttable itof; - const upb_msgdef *parent; +template +struct ReturnOf { + typedef R Return; }; -extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl; -#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \ - { UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof } +template +template +inline Handler::Handler(F func) + : registered_(false), + cleanup_data_(func.GetData()), + cleanup_func_(func.GetCleanup()) { + attr_.handler_data = func.GetData(); + typedef typename ReturnOf::Return Return; + typedef typename ConvertParams::Func ConvertedParamsFunc; + typedef typename MaybeWrapReturn::Func + ReturnWrappedFunc; + handler_ = ReturnWrappedFunc().Call; + /* Set attributes based on what templates can statically tell us about the + * user's function. */ -/* upb_symtab *****************************************************************/ + /* If the original function returns void, then we know that we wrapped it to + * always return ok. */ + bool always_ok = is_same::value; + attr_.alwaysok = always_ok; + + /* Closure parameter and return type. */ + attr_.closure_type = UniquePtrForType(); + + /* We use the closure type (from the first parameter) if the return type is + * void or bool, since these are the two cases we wrap to return the closure's + * type anyway. + * + * This is all nonsense for non START* handlers, but it doesn't matter because + * in that case the value will be ignored. */ + typedef typename FirstUnlessVoidOrBool::value + EffectiveReturn; + attr_.return_closure_type = UniquePtrForType(); +} -struct upb_symtab { - upb_refcounted base; +template +inline void Handler::AddCleanup(upb_handlers* h) const { + UPB_ASSERT(!registered_); + registered_ = true; + if (cleanup_func_) { + bool ok = upb_handlers_addcleanup(h, cleanup_data_, cleanup_func_); + UPB_ASSERT(ok); + } +} - upb_strtable symtab; -}; +} /* namespace upb */ -struct upb_filedef { - upb_refcounted base; +#endif /* __cplusplus */ - const char *name; - const char *package; - const char *phpprefix; - const char *phpnamespace; - upb_syntax_t syntax; - upb_inttable defs; - upb_inttable deps; -}; +#undef UPB_TWO_32BIT_TYPES +#undef UPB_TWO_64BIT_TYPES +#undef UPB_INT32_T +#undef UPB_UINT32_T +#undef UPB_INT32ALT_T +#undef UPB_UINT32ALT_T +#undef UPB_INT64_T +#undef UPB_UINT64_T +#undef UPB_INT64ALT_T +#undef UPB_UINT64ALT_T -extern const struct upb_refcounted_vtbl upb_filedef_vtbl; +#endif /* UPB_HANDLERS_INL_H_ */ -#endif /* UPB_STATICINIT_H_ */ +#endif /* UPB_HANDLERS_H */ /* -** upb::descriptor::Reader (upb_descreader) +** upb::Sink (upb_sink) +** upb::BytesSink (upb_bytessink) +** +** A upb_sink is an object that binds a upb_handlers object to some runtime +** state. It is the object that can actually receive data via the upb_handlers +** interface. ** -** Provides a way of building upb::Defs from data in descriptor.proto format. +** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or +** thread-safe. You can create as many of them as you want, but each one may +** only be used in a single thread at a time. +** +** If we compare with class-based OOP, a you can think of a upb_def as an +** abstract base class, a upb_handlers as a concrete derived class, and a +** upb_sink as an object (class instance). */ -#ifndef UPB_DESCRIPTOR_H -#define UPB_DESCRIPTOR_H +#ifndef UPB_SINK_H +#define UPB_SINK_H #ifdef __cplusplus namespace upb { -namespace descriptor { -class Reader; -} /* namespace descriptor */ -} /* namespace upb */ +class BytesSink; +class Sink; +} #endif -UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader) +/* upb_sink *******************************************************************/ #ifdef __cplusplus +extern "C" { +#endif -/* Class that receives descriptor data according to the descriptor.proto schema - * and use it to build upb::Defs corresponding to that schema. */ -class upb::descriptor::Reader { - public: - /* These handlers must have come from NewHandlers() and must outlive the - * Reader. - * - * TODO: generate the handlers statically (like we do with the - * descriptor.proto defs) so that there is no need to pass this parameter (or - * to build/memory-manage the handlers at runtime at all). Unfortunately this - * is a bit tricky to implement for Handlers, but necessary to simplify this - * interface. */ - static Reader* Create(Environment* env, const Handlers* handlers); - - /* The reader's input; this is where descriptor.proto data should be sent. */ - Sink* input(); +typedef struct { + const upb_handlers *handlers; + void *closure; +} upb_sink; + +#define PUTVAL(type, ctype) \ + UPB_INLINE bool upb_sink_put##type(upb_sink s, upb_selector_t sel, \ + ctype val) { \ + typedef upb_##type##_handlerfunc functype; \ + functype *func; \ + const void *hd; \ + if (!s.handlers) return true; \ + func = (functype *)upb_handlers_gethandler(s.handlers, sel, &hd); \ + if (!func) return true; \ + return func(s.closure, hd, val); \ + } - /* Use to get the FileDefs that have been parsed. */ - size_t file_count() const; - FileDef* file(size_t i) const; +PUTVAL(int32, int32_t) +PUTVAL(int64, int64_t) +PUTVAL(uint32, uint32_t) +PUTVAL(uint64, uint64_t) +PUTVAL(float, float) +PUTVAL(double, double) +PUTVAL(bool, bool) +#undef PUTVAL - /* Builds and returns handlers for the reader, owned by "owner." */ - static Handlers* NewHandlers(const void* owner); +UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) { + s->handlers = h; + s->closure = c; +} - private: - UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader) -}; +UPB_INLINE size_t upb_sink_putstring(upb_sink s, upb_selector_t sel, + const char *buf, size_t n, + const upb_bufhandle *handle) { + typedef upb_string_handlerfunc func; + func *handler; + const void *hd; + if (!s.handlers) return n; + handler = (func *)upb_handlers_gethandler(s.handlers, sel, &hd); -#endif + if (!handler) return n; + return handler(s.closure, hd, buf, n, handle); +} -UPB_BEGIN_EXTERN_C +UPB_INLINE bool upb_sink_putunknown(upb_sink s, const char *buf, size_t n) { + typedef upb_unknown_handlerfunc func; + func *handler; + const void *hd; + if (!s.handlers) return true; + handler = + (func *)upb_handlers_gethandler(s.handlers, UPB_UNKNOWN_SELECTOR, &hd); -/* C API. */ -upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h); -upb_sink *upb_descreader_input(upb_descreader *r); -size_t upb_descreader_filecount(const upb_descreader *r); -upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i); -const upb_handlers *upb_descreader_newhandlers(const void *owner); + if (!handler) return n; + return handler(s.closure, hd, buf, n); +} -UPB_END_EXTERN_C +UPB_INLINE bool upb_sink_startmsg(upb_sink s) { + typedef upb_startmsg_handlerfunc func; + func *startmsg; + const void *hd; + if (!s.handlers) return true; + startmsg = + (func *)upb_handlers_gethandler(s.handlers, UPB_STARTMSG_SELECTOR, &hd); -#ifdef __cplusplus -/* C++ implementation details. ************************************************/ -namespace upb { -namespace descriptor { -inline Reader* Reader::Create(Environment* e, const Handlers *h) { - return upb_descreader_create(e, h); + if (!startmsg) return true; + return startmsg(s.closure, hd); } -inline Sink* Reader::input() { return upb_descreader_input(this); } -inline size_t Reader::file_count() const { - return upb_descreader_filecount(this); + +UPB_INLINE bool upb_sink_endmsg(upb_sink s, upb_status *status) { + typedef upb_endmsg_handlerfunc func; + func *endmsg; + const void *hd; + if (!s.handlers) return true; + endmsg = + (func *)upb_handlers_gethandler(s.handlers, UPB_ENDMSG_SELECTOR, &hd); + + if (!endmsg) return true; + return endmsg(s.closure, hd, status); } -inline FileDef* Reader::file(size_t i) const { - return upb_descreader_file(this, i); + +UPB_INLINE bool upb_sink_startseq(upb_sink s, upb_selector_t sel, + upb_sink *sub) { + typedef upb_startfield_handlerfunc func; + func *startseq; + const void *hd; + sub->closure = s.closure; + sub->handlers = s.handlers; + if (!s.handlers) return true; + startseq = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); + + if (!startseq) return true; + sub->closure = startseq(s.closure, hd); + return sub->closure ? true : false; } -} /* namespace descriptor */ -} /* namespace upb */ -#endif -#endif /* UPB_DESCRIPTOR_H */ -/* This file contains accessors for a set of compiled-in defs. - * Note that unlike Google's protobuf, it does *not* define - * generated classes or any other kind of data structure for - * actually storing protobufs. It only contains *defs* which - * let you reflect over a protobuf *schema*. - */ -/* This file was generated by upbc (the upb compiler) from the input - * file: - * - * upb/descriptor/descriptor.proto - * - * Do not edit -- your changes will be discarded when the file is - * regenerated. */ +UPB_INLINE bool upb_sink_endseq(upb_sink s, upb_selector_t sel) { + typedef upb_endfield_handlerfunc func; + func *endseq; + const void *hd; + if (!s.handlers) return true; + endseq = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); + + if (!endseq) return true; + return endseq(s.closure, hd); +} -#ifndef UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ -#define UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ +UPB_INLINE bool upb_sink_startstr(upb_sink s, upb_selector_t sel, + size_t size_hint, upb_sink *sub) { + typedef upb_startstr_handlerfunc func; + func *startstr; + const void *hd; + sub->closure = s.closure; + sub->handlers = s.handlers; + if (!s.handlers) return true; + startstr = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); + if (!startstr) return true; + sub->closure = startstr(s.closure, hd, size_hint); + return sub->closure ? true : false; +} -UPB_BEGIN_EXTERN_C +UPB_INLINE bool upb_sink_endstr(upb_sink s, upb_selector_t sel) { + typedef upb_endfield_handlerfunc func; + func *endstr; + const void *hd; + if (!s.handlers) return true; + endstr = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); -/* MessageDefs: call these functions to get a ref to a msgdef. */ -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner); + if (!endstr) return true; + return endstr(s.closure, hd); +} -/* EnumDefs: call these functions to get a ref to an enumdef. */ -const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner); -const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner); -const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner); -const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner); -const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner); +UPB_INLINE bool upb_sink_startsubmsg(upb_sink s, upb_selector_t sel, + upb_sink *sub) { + typedef upb_startfield_handlerfunc func; + func *startsubmsg; + const void *hd; + sub->closure = s.closure; + if (!s.handlers) { + sub->handlers = NULL; + return true; + } + sub->handlers = upb_handlers_getsubhandlers_sel(s.handlers, sel); + startsubmsg = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); -/* Functions to test whether this message is of a certain type. */ -UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto") == 0; + if (!startsubmsg) return true; + sub->closure = startsubmsg(s.closure, hd); + return sub->closure ? true : false; } -UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ExtensionRange") == 0; + +UPB_INLINE bool upb_sink_endsubmsg(upb_sink s, upb_selector_t sel) { + typedef upb_endfield_handlerfunc func; + func *endsubmsg; + const void *hd; + if (!s.handlers) return true; + endsubmsg = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); + + if (!endsubmsg) return s.closure; + return endsubmsg(s.closure, hd); } -UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ReservedRange") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_EnumDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_EnumOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_EnumValueDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_EnumValueOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorSet_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorSet") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FileOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_MessageOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.MessageOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_MethodDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_MethodOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_OneofDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.OneofDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_ServiceDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_ServiceOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_Location_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo.Location") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_NamePart_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption.NamePart") == 0; -} - -/* Functions to test whether this enum is of a certain type. */ -UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Label_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Label") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Type_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Type") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_CType_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.CType") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_JSType_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.JSType") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FileOptions_OptimizeMode_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FileOptions.OptimizeMode") == 0; -} - - -/* Functions to get a fielddef from a msgdef reference. */ -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_end(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_start(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_end(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_start(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_enum_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension_range(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_field(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_nested_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_oneof_decl(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_range(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_allow_alias(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_number(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_default_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_extendee(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_json_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_label(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_number(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_oneof_index(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_ctype(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_jstype(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_lazy(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_packed(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_weak(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_enum_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_message_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_public_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_service(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_source_code_info(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_syntax(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 12); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_weak_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 11); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_f_file(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorSet_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_enable_arenas(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 31); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 16); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_csharp_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 37); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 23); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_go_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 11); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generate_equals_and_hash(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 20); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 17); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_multiple_files(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_outer_classname(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_string_check_utf8(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 27); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_javanano_use_deprecated_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 38); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_objc_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 36); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_optimize_for(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 40); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 41); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_py_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 18); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_map_entry(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_message_set_wire_format(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_no_standard_descriptor_accessor(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_client_streaming(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_input_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_output_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_server_streaming(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 33); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_OneofDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_OneofDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_method(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 33); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_detached_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_path(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_span(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_trailing_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_f_location(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_is_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_name_part(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_aggregate_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_double_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_identifier_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_negative_int_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_positive_int_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_string_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 7); } - -UPB_END_EXTERN_C #ifdef __cplusplus +} /* extern "C" */ -namespace upbdefs { -namespace google { -namespace protobuf { - -class DescriptorProto : public ::upb::reffed_ptr { +/* A upb::Sink is an object that binds a upb::Handlers object to some runtime + * state. It represents an endpoint to which data can be sent. + * + * TODO(haberman): right now all of these functions take selectors. Should they + * take selectorbase instead? + * + * ie. instead of calling: + * sink->StartString(FOO_FIELD_START_STRING, ...) + * a selector base would let you say: + * sink->StartString(FOO_FIELD, ...) + * + * This would make call sites a little nicer and require emitting fewer selector + * definitions in .h files. + * + * But the current scheme has the benefit that you can retrieve a function + * pointer for any handler with handlers->GetHandler(selector), without having + * to have a separate GetHandler() function for each handler type. The JIT + * compiler uses this. To accommodate we'd have to expose a separate + * GetHandler() for every handler type. + * + * Also to ponder: selectors right now are independent of a specific Handlers + * instance. In other words, they allocate a number to every possible handler + * that *could* be registered, without knowing anything about what handlers + * *are* registered. That means that using selectors as table offsets prohibits + * us from compacting the handler table at Freeze() time. If the table is very + * sparse, this could be wasteful. + * + * Having another selector-like thing that is specific to a Handlers instance + * would allow this compacting, but then it would be impossible to write code + * ahead-of-time that can be bound to any Handlers instance at runtime. For + * example, a .proto file parser written as straight C will not know what + * Handlers it will be bound to, so when it calls sink->StartString() what + * selector will it pass? It needs a selector like we have today, that is + * independent of any particular upb::Handlers. + * + * Is there a way then to allow Handlers table compaction? */ +class upb::Sink { public: - DescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); + /* Constructor with no initialization; must be Reset() before use. */ + Sink() {} + + Sink(const Sink&) = default; + Sink& operator=(const Sink&) = default; + + Sink(const upb_sink& sink) : sink_(sink) {} + Sink &operator=(const upb_sink &sink) { + sink_ = sink; + return *this; } - static DescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_get(&m); - return DescriptorProto(m, &m); + upb_sink sink() { return sink_; } + + /* Constructs a new sink for the given frozen handlers and closure. + * + * TODO: once the Handlers know the expected closure type, verify that T + * matches it. */ + template Sink(const upb_handlers* handlers, T* closure) { + Reset(handlers, closure); } - class ExtensionRange : public ::upb::reffed_ptr { - public: - ExtensionRange(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); - } + upb_sink* ptr() { return &sink_; } - static ExtensionRange get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(&m); - return ExtensionRange(m, &m); - } - }; + /* Resets the value of the sink. */ + template void Reset(const upb_handlers* handlers, T* closure) { + upb_sink_reset(&sink_, handlers, closure); + } - class ReservedRange : public ::upb::reffed_ptr { - public: - ReservedRange(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); - } + /* Returns the top-level object that is bound to this sink. + * + * TODO: once the Handlers know the expected closure type, verify that T + * matches it. */ + template T* GetObject() const { + return static_cast(sink_.closure); + } - static ReservedRange get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(&m); - return ReservedRange(m, &m); - } - }; -}; + /* Functions for pushing data into the sink. + * + * These return false if processing should stop (either due to error or just + * to suspend). + * + * These may not be called from within one of the same sink's handlers (in + * other words, handlers are not re-entrant). */ -class EnumDescriptorProto : public ::upb::reffed_ptr { - public: - EnumDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); + /* Should be called at the start and end of every message; both the top-level + * message and submessages. This means that submessages should use the + * following sequence: + * sink->StartSubMessage(startsubmsg_selector); + * sink->StartMessage(); + * // ... + * sink->EndMessage(&status); + * sink->EndSubMessage(endsubmsg_selector); */ + bool StartMessage() { return upb_sink_startmsg(sink_); } + bool EndMessage(upb_status *status) { + return upb_sink_endmsg(sink_, status); } - static EnumDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumDescriptorProto_get(&m); - return EnumDescriptorProto(m, &m); + /* Putting of individual values. These work for both repeated and + * non-repeated fields, but for repeated fields you must wrap them in + * calls to StartSequence()/EndSequence(). */ + bool PutInt32(HandlersPtr::Selector s, int32_t val) { + return upb_sink_putint32(sink_, s, val); } -}; -class EnumOptions : public ::upb::reffed_ptr { - public: - EnumOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); + bool PutInt64(HandlersPtr::Selector s, int64_t val) { + return upb_sink_putint64(sink_, s, val); } - static EnumOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumOptions_get(&m); - return EnumOptions(m, &m); + bool PutUInt32(HandlersPtr::Selector s, uint32_t val) { + return upb_sink_putuint32(sink_, s, val); } -}; -class EnumValueDescriptorProto : public ::upb::reffed_ptr { - public: - EnumValueDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); + bool PutUInt64(HandlersPtr::Selector s, uint64_t val) { + return upb_sink_putuint64(sink_, s, val); } - static EnumValueDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumValueDescriptorProto_get(&m); - return EnumValueDescriptorProto(m, &m); + bool PutFloat(HandlersPtr::Selector s, float val) { + return upb_sink_putfloat(sink_, s, val); } -}; -class EnumValueOptions : public ::upb::reffed_ptr { - public: - EnumValueOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); + bool PutDouble(HandlersPtr::Selector s, double val) { + return upb_sink_putdouble(sink_, s, val); } - static EnumValueOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumValueOptions_get(&m); - return EnumValueOptions(m, &m); + bool PutBool(HandlersPtr::Selector s, bool val) { + return upb_sink_putbool(sink_, s, val); } -}; -class FieldDescriptorProto : public ::upb::reffed_ptr { - public: - FieldDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); + /* Putting of string/bytes values. Each string can consist of zero or more + * non-contiguous buffers of data. + * + * For StartString(), the function will write a sink for the string to "sub." + * The sub-sink must be used for any/all PutStringBuffer() calls. */ + bool StartString(HandlersPtr::Selector s, size_t size_hint, Sink* sub) { + upb_sink sub_c; + bool ret = upb_sink_startstr(sink_, s, size_hint, &sub_c); + *sub = sub_c; + return ret; + } + + size_t PutStringBuffer(HandlersPtr::Selector s, const char *buf, size_t len, + const upb_bufhandle *handle) { + return upb_sink_putstring(sink_, s, buf, len, handle); + } + + bool EndString(HandlersPtr::Selector s) { + return upb_sink_endstr(sink_, s); + } + + /* For submessage fields. + * + * For StartSubMessage(), the function will write a sink for the string to + * "sub." The sub-sink must be used for any/all handlers called within the + * submessage. */ + bool StartSubMessage(HandlersPtr::Selector s, Sink* sub) { + upb_sink sub_c; + bool ret = upb_sink_startsubmsg(sink_, s, &sub_c); + *sub = sub_c; + return ret; + } + + bool EndSubMessage(HandlersPtr::Selector s) { + return upb_sink_endsubmsg(sink_, s); + } + + /* For repeated fields of any type, the sequence of values must be wrapped in + * these calls. + * + * For StartSequence(), the function will write a sink for the string to + * "sub." The sub-sink must be used for any/all handlers called within the + * sequence. */ + bool StartSequence(HandlersPtr::Selector s, Sink* sub) { + upb_sink sub_c; + bool ret = upb_sink_startseq(sink_, s, &sub_c); + *sub = sub_c; + return ret; } - static FieldDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FieldDescriptorProto_get(&m); - return FieldDescriptorProto(m, &m); + bool EndSequence(HandlersPtr::Selector s) { + return upb_sink_endseq(sink_, s); } - class Label : public ::upb::reffed_ptr { - public: - Label(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_Label_is(e)); - } - static Label get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Label_get(&e); - return Label(e, &e); - } - }; + /* Copy and assign specifically allowed. + * We don't even bother making these members private because so many + * functions need them and this is mainly just a dumb data container anyway. + */ - class Type : public ::upb::reffed_ptr { - public: - Type(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_Type_is(e)); - } - static Type get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Type_get(&e); - return Type(e, &e); - } - }; + private: + upb_sink sink_; }; -class FieldOptions : public ::upb::reffed_ptr { - public: - FieldOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); - } +#endif /* __cplusplus */ - static FieldOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FieldOptions_get(&m); - return FieldOptions(m, &m); - } +/* upb_bytessink **************************************************************/ - class CType : public ::upb::reffed_ptr { - public: - CType(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_CType_is(e)); - } - static CType get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_CType_get(&e); - return CType(e, &e); - } - }; +typedef struct { + const upb_byteshandler *handler; + void *closure; +} upb_bytessink ; - class JSType : public ::upb::reffed_ptr { - public: - JSType(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_JSType_is(e)); - } - static JSType get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_JSType_get(&e); - return JSType(e, &e); - } - }; -}; +UPB_INLINE void upb_bytessink_reset(upb_bytessink* s, const upb_byteshandler *h, + void *closure) { + s->handler = h; + s->closure = closure; +} -class FileDescriptorProto : public ::upb::reffed_ptr { - public: - FileDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); - } +UPB_INLINE bool upb_bytessink_start(upb_bytessink s, size_t size_hint, + void **subc) { + typedef upb_startstr_handlerfunc func; + func *start; + *subc = s.closure; + if (!s.handler) return true; + start = (func *)s.handler->table[UPB_STARTSTR_SELECTOR].func; - static FileDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorProto_get(&m); - return FileDescriptorProto(m, &m); - } -}; + if (!start) return true; + *subc = start(s.closure, + s.handler->table[UPB_STARTSTR_SELECTOR].attr.handler_data, + size_hint); + return *subc != NULL; +} -class FileDescriptorSet : public ::upb::reffed_ptr { - public: - FileDescriptorSet(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorSet_is(m)); - } +UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink s, void *subc, + const char *buf, size_t size, + const upb_bufhandle* handle) { + typedef upb_string_handlerfunc func; + func *putbuf; + if (!s.handler) return true; + putbuf = (func *)s.handler->table[UPB_STRING_SELECTOR].func; - static FileDescriptorSet get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorSet_get(&m); - return FileDescriptorSet(m, &m); - } -}; + if (!putbuf) return true; + return putbuf(subc, s.handler->table[UPB_STRING_SELECTOR].attr.handler_data, + buf, size, handle); +} -class FileOptions : public ::upb::reffed_ptr { - public: - FileOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); - } +UPB_INLINE bool upb_bytessink_end(upb_bytessink s) { + typedef upb_endfield_handlerfunc func; + func *end; + if (!s.handler) return true; + end = (func *)s.handler->table[UPB_ENDSTR_SELECTOR].func; - static FileOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FileOptions_get(&m); - return FileOptions(m, &m); - } + if (!end) return true; + return end(s.closure, + s.handler->table[UPB_ENDSTR_SELECTOR].attr.handler_data); +} - class OptimizeMode : public ::upb::reffed_ptr { - public: - OptimizeMode(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FileOptions_OptimizeMode_is(e)); - } - static OptimizeMode get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FileOptions_OptimizeMode_get(&e); - return OptimizeMode(e, &e); - } - }; -}; +#ifdef __cplusplus -class MessageOptions : public ::upb::reffed_ptr { +class upb::BytesSink { public: - MessageOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); - } + BytesSink() {} - static MessageOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_MessageOptions_get(&m); - return MessageOptions(m, &m); - } -}; + BytesSink(const BytesSink&) = default; + BytesSink& operator=(const BytesSink&) = default; -class MethodDescriptorProto : public ::upb::reffed_ptr { - public: - MethodDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); + BytesSink(const upb_bytessink& sink) : sink_(sink) {} + BytesSink &operator=(const upb_bytessink &sink) { + sink_ = sink; + return *this; } - static MethodDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_MethodDescriptorProto_get(&m); - return MethodDescriptorProto(m, &m); - } -}; + upb_bytessink sink() { return sink_; } -class MethodOptions : public ::upb::reffed_ptr { - public: - MethodOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); + /* Constructs a new sink for the given frozen handlers and closure. + * + * TODO(haberman): once the Handlers know the expected closure type, verify + * that T matches it. */ + template BytesSink(const upb_byteshandler* handler, T* closure) { + upb_bytessink_reset(sink_, handler, closure); } - static MethodOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_MethodOptions_get(&m); - return MethodOptions(m, &m); + /* Resets the value of the sink. */ + template void Reset(const upb_byteshandler* handler, T* closure) { + upb_bytessink_reset(&sink_, handler, closure); } -}; -class OneofDescriptorProto : public ::upb::reffed_ptr { - public: - OneofDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_OneofDescriptorProto_is(m)); + bool Start(size_t size_hint, void **subc) { + return upb_bytessink_start(sink_, size_hint, subc); } - static OneofDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_OneofDescriptorProto_get(&m); - return OneofDescriptorProto(m, &m); + size_t PutBuffer(void *subc, const char *buf, size_t len, + const upb_bufhandle *handle) { + return upb_bytessink_putbuf(sink_, subc, buf, len, handle); } -}; -class ServiceDescriptorProto : public ::upb::reffed_ptr { - public: - ServiceDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); + bool End() { + return upb_bytessink_end(sink_); } - static ServiceDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_ServiceDescriptorProto_get(&m); - return ServiceDescriptorProto(m, &m); - } + private: + upb_bytessink sink_; }; -class ServiceOptions : public ::upb::reffed_ptr { - public: - ServiceOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); - } +#endif /* __cplusplus */ - static ServiceOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_ServiceOptions_get(&m); - return ServiceOptions(m, &m); - } -}; +/* upb_bufsrc *****************************************************************/ -class SourceCodeInfo : public ::upb::reffed_ptr { - public: - SourceCodeInfo(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_is(m)); - } +#ifdef __cplusplus +extern "C" { +#endif - static SourceCodeInfo get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_get(&m); - return SourceCodeInfo(m, &m); - } +bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink); - class Location : public ::upb::reffed_ptr { - public: - Location(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); - } +#ifdef __cplusplus +} /* extern "C" */ - static Location get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_Location_get(&m); - return Location(m, &m); - } - }; -}; +namespace upb { +template bool PutBuffer(const T& str, BytesSink sink) { + return upb_bufsrc_putbuf(str.data(), str.size(), sink.sink()); +} +} -class UninterpretedOption : public ::upb::reffed_ptr { - public: - UninterpretedOption(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); - } +#endif /* __cplusplus */ - static UninterpretedOption get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_get(&m); - return UninterpretedOption(m, &m); - } +#endif - class NamePart : public ::upb::reffed_ptr { - public: - NamePart(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); - } - static NamePart get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_NamePart_get(&m); - return NamePart(m, &m); - } - }; -}; +#ifndef UPB_MSGFACTORY_H_ +#define UPB_MSGFACTORY_H_ + +/** upb_msgfactory ************************************************************/ -} /* namespace protobuf */ -} /* namespace google */ -} /* namespace upbdefs */ +struct upb_msgfactory; +typedef struct upb_msgfactory upb_msgfactory; -#endif /* __cplusplus */ +#ifdef __cplusplus +extern "C" { +#endif + +/* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and + * upb_visitorplan objects. These are the objects necessary to represent, + * populate, and and visit upb_msg objects. + * + * These caches are all populated by upb_msgdef, and lazily created on demand. + */ + +/* Creates and destroys a msgfactory, respectively. The messages for this + * msgfactory must come from |symtab| (which should outlive the msgfactory). */ +upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab); +void upb_msgfactory_free(upb_msgfactory *f); + +const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f); + +/* The functions to get cached objects, lazily creating them on demand. These + * all require: + * + * - m is in upb_msgfactory_symtab(f) + * - upb_msgdef_mapentry(m) == false (since map messages can't have layouts). + * + * The returned objects will live for as long as the msgfactory does. + * + * TODO(haberman): consider making this thread-safe and take a const + * upb_msgfactory. */ +const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, + const upb_msgdef *m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif -#endif /* UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ */ +#endif /* UPB_MSGFACTORY_H_ */ /* ** Internal-only definitions for the decoder. */ @@ -8430,20 +6222,13 @@ class UninterpretedOption : public ::upb::reffed_ptr { namespace upb { namespace pb { class CodeCache; -class Decoder; -class DecoderMethod; +class DecoderPtr; +class DecoderMethodPtr; class DecoderMethodOptions; } /* namespace pb */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache) -UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder) -UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts) - -UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted, - upb_pbdecodermethod, upb_refcounted) - /* The maximum number of bytes we are required to buffer internally between * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte * varint, less one because we are buffering an incomplete value. @@ -8451,83 +6236,111 @@ UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted, * Should only be used by unit tests. */ #define UPB_DECODER_MAX_RESIDUAL_BYTES 14 -#ifdef __cplusplus +/* upb_pbdecodermethod ********************************************************/ -/* The parameters one uses to construct a DecoderMethod. - * TODO(haberman): move allowjit here? Seems more convenient for users. - * TODO(haberman): move this to be heap allocated for ABI stability. */ -class upb::pb::DecoderMethodOptions { - public: - /* Parameter represents the destination handlers that this method will push - * to. */ - explicit DecoderMethodOptions(const Handlers* dest_handlers); +struct upb_pbdecodermethod; +typedef struct upb_pbdecodermethod upb_pbdecodermethod; - /* Should the decoder push submessages to lazy handlers for fields that have - * them? The caller should set this iff the lazy handlers expect data that is - * in protobuf binary format and the caller wishes to lazy parse it. */ - void set_lazy(bool lazy); -#else -struct upb_pbdecodermethodopts { +#ifdef __cplusplus +extern "C" { #endif - const upb_handlers *handlers; - bool lazy; -}; + +const upb_handlers *upb_pbdecodermethod_desthandlers( + const upb_pbdecodermethod *m); +const upb_byteshandler *upb_pbdecodermethod_inputhandler( + const upb_pbdecodermethod *m); +bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m); #ifdef __cplusplus +} /* extern "C" */ /* Represents the code to parse a protobuf according to a destination * Handlers. */ -class upb::pb::DecoderMethod { +class upb::pb::DecoderMethodPtr { public: - /* Include base methods from upb::ReferenceCounted. */ - UPB_REFCOUNTED_CPPMETHODS + DecoderMethodPtr() : ptr_(nullptr) {} + DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {} + + const upb_pbdecodermethod* ptr() { return ptr_; } /* The destination handlers that are statically bound to this method. * This method is only capable of outputting to a sink that uses these * handlers. */ - const Handlers* dest_handlers() const; + const Handlers *dest_handlers() const { + return upb_pbdecodermethod_desthandlers(ptr_); + } /* The input handlers for this decoder method. */ - const BytesHandler* input_handler() const; + const BytesHandler* input_handler() const { + return upb_pbdecodermethod_inputhandler(ptr_); + } /* Whether this method is native. */ - bool is_native() const; - - /* Convenience method for generating a DecoderMethod without explicitly - * creating a CodeCache. */ - static reffed_ptr New(const DecoderMethodOptions& opts); + bool is_native() const { + return upb_pbdecodermethod_isnative(ptr_); + } private: - UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod) + const upb_pbdecodermethod* ptr_; }; #endif +/* upb_pbdecoder **************************************************************/ + /* Preallocation hint: decoder won't allocate more bytes than this when first * constructed. This hint may be an overestimate for some build configurations. * But if the decoder library is upgraded without recompiling the application, * it may be an underestimate. */ #define UPB_PB_DECODER_SIZE 4416 +struct upb_pbdecoder; +typedef struct upb_pbdecoder upb_pbdecoder; + +#ifdef __cplusplus +extern "C" { +#endif + +upb_pbdecoder *upb_pbdecoder_create(upb_arena *arena, + const upb_pbdecodermethod *method, + upb_sink output, upb_status *status); +const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); +upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d); +uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); +size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); +bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); +void upb_pbdecoder_reset(upb_pbdecoder *d); + #ifdef __cplusplus +} /* extern "C" */ /* A Decoder receives binary protobuf data on its input sink and pushes the * decoded data to its output sink. */ -class upb::pb::Decoder { +class upb::pb::DecoderPtr { public: + DecoderPtr() : ptr_(nullptr) {} + DecoderPtr(upb_pbdecoder* ptr) : ptr_(ptr) {} + + upb_pbdecoder* ptr() { return ptr_; } + /* Constructs a decoder instance for the given method, which must outlive this * decoder. Any errors during parsing will be set on the given status, which * must also outlive this decoder. * * The sink must match the given method. */ - static Decoder* Create(Environment* env, const DecoderMethod* method, - Sink* output); + static DecoderPtr Create(Arena *arena, DecoderMethodPtr method, + upb::Sink output, Status *status) { + return DecoderPtr(upb_pbdecoder_create(arena->ptr(), method.ptr(), + output.sink(), status->ptr())); + } /* Returns the DecoderMethod this decoder is parsing from. */ - const DecoderMethod* method() const; + const DecoderMethodPtr method() const { + return DecoderMethodPtr(upb_pbdecoder_method(ptr_)); + } /* The sink on which this decoder receives input. */ - BytesSink* input(); + BytesSink input() { return BytesSink(upb_pbdecoder_input(ptr())); } /* Returns number of bytes successfully parsed. * @@ -8536,7 +6349,7 @@ class upb::pb::Decoder { * * This value may not be up-to-date when called from inside a parsing * callback. */ - uint64_t BytesParsed() const; + uint64_t BytesParsed() { return upb_pbdecoder_bytesparsed(ptr()); } /* Gets/sets the parsing nexting limit. If the total number of nested * submessages and repeated fields hits this limit, parsing will fail. This @@ -8545,192 +6358,88 @@ class upb::pb::Decoder { * * Setting the limit will fail if the parser is currently suspended at a depth * greater than this, or if memory allocation of the stack fails. */ - size_t max_nesting() const; - bool set_max_nesting(size_t max); + size_t max_nesting() { return upb_pbdecoder_maxnesting(ptr()); } + bool set_max_nesting(size_t max) { return upb_pbdecoder_maxnesting(ptr()); } - void Reset(); + void Reset() { upb_pbdecoder_reset(ptr()); } static const size_t kSize = UPB_PB_DECODER_SIZE; private: - UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder) + upb_pbdecoder *ptr_; }; #endif /* __cplusplus */ -#ifdef __cplusplus - -/* A class for caching protobuf processing code, whether bytecode for the - * interpreted decoder or machine code for the JIT. - * - * This class is not thread-safe. - * - * TODO(haberman): move this to be heap allocated for ABI stability. */ -class upb::pb::CodeCache { - public: - CodeCache(); - ~CodeCache(); - - /* Whether the cache is allowed to generate machine code. Defaults to true. - * There is no real reason to turn it off except for testing or if you are - * having a specific problem with the JIT. - * - * Note that allow_jit = true does not *guarantee* that the code will be JIT - * compiled. If this platform is not supported or the JIT was not compiled - * in, the code may still be interpreted. */ - bool allow_jit() const; - - /* This may only be called when the object is first constructed, and prior to - * any code generation, otherwise returns false and does nothing. */ - bool set_allow_jit(bool allow); +/* upb_pbcodecache ************************************************************/ - /* Returns a DecoderMethod that can push data to the given handlers. - * If a suitable method already exists, it will be returned from the cache. - * - * Specifying the destination handlers here allows the DecoderMethod to be - * statically bound to the destination handlers if possible, which can allow - * more efficient decoding. However the returned method may or may not - * actually be statically bound. But in all cases, the returned method can - * push data to the given handlers. */ - const DecoderMethod *GetDecoderMethod(const DecoderMethodOptions& opts); +/* Lazily builds and caches decoder methods that will push data to the given + * handlers. The destination handlercache must outlive this object. */ - /* If/when someone needs to explicitly create a dynamically-bound - * DecoderMethod*, we can add a method to get it here. */ +struct upb_pbcodecache; +typedef struct upb_pbcodecache upb_pbcodecache; - private: - UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache) -#else -struct upb_pbcodecache { +#ifdef __cplusplus +extern "C" { #endif - bool allow_jit_; - - /* Array of mgroups. */ - upb_inttable groups; -}; - -UPB_BEGIN_EXTERN_C - -upb_pbdecoder *upb_pbdecoder_create(upb_env *e, - const upb_pbdecodermethod *method, - upb_sink *output); -const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); -upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d); -uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); -size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); -bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); -void upb_pbdecoder_reset(upb_pbdecoder *d); - -void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts, - const upb_handlers *h); -void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy); - -/* Include refcounted methods like upb_pbdecodermethod_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_pbdecodermethod, upb_pbdecodermethod_upcast) - -const upb_handlers *upb_pbdecodermethod_desthandlers( - const upb_pbdecodermethod *m); -const upb_byteshandler *upb_pbdecodermethod_inputhandler( - const upb_pbdecodermethod *m); -bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m); -const upb_pbdecodermethod *upb_pbdecodermethod_new( - const upb_pbdecodermethodopts *opts, const void *owner); - -void upb_pbcodecache_init(upb_pbcodecache *c); -void upb_pbcodecache_uninit(upb_pbcodecache *c); +upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest); +void upb_pbcodecache_free(upb_pbcodecache *c); bool upb_pbcodecache_allowjit(const upb_pbcodecache *c); -bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow); -const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod( - upb_pbcodecache *c, const upb_pbdecodermethodopts *opts); - -UPB_END_EXTERN_C +void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow); +void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy); +const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, + const upb_msgdef *md); #ifdef __cplusplus +} /* extern "C" */ -namespace upb { +/* A class for caching protobuf processing code, whether bytecode for the + * interpreted decoder or machine code for the JIT. + * + * This class is not thread-safe. */ +class upb::pb::CodeCache { + public: + CodeCache(upb::HandlerCache *dest) + : ptr_(upb_pbcodecache_new(dest->ptr()), upb_pbcodecache_free) {} + CodeCache(CodeCache&&) = default; + CodeCache& operator=(CodeCache&&) = default; -namespace pb { + upb_pbcodecache* ptr() { return ptr_.get(); } + const upb_pbcodecache* ptr() const { return ptr_.get(); } -/* static */ -inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m, - Sink* sink) { - return upb_pbdecoder_create(env, m, sink); -} -inline const DecoderMethod* Decoder::method() const { - return upb_pbdecoder_method(this); -} -inline BytesSink* Decoder::input() { - return upb_pbdecoder_input(this); -} -inline uint64_t Decoder::BytesParsed() const { - return upb_pbdecoder_bytesparsed(this); -} -inline size_t Decoder::max_nesting() const { - return upb_pbdecoder_maxnesting(this); -} -inline bool Decoder::set_max_nesting(size_t max) { - return upb_pbdecoder_setmaxnesting(this, max); -} -inline void Decoder::Reset() { upb_pbdecoder_reset(this); } + /* Whether the cache is allowed to generate machine code. Defaults to true. + * There is no real reason to turn it off except for testing or if you are + * having a specific problem with the JIT. + * + * Note that allow_jit = true does not *guarantee* that the code will be JIT + * compiled. If this platform is not supported or the JIT was not compiled + * in, the code may still be interpreted. */ + bool allow_jit() const { return upb_pbcodecache_allowjit(ptr()); } -inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) { - upb_pbdecodermethodopts_init(this, h); -} -inline void DecoderMethodOptions::set_lazy(bool lazy) { - upb_pbdecodermethodopts_setlazy(this, lazy); -} + /* This may only be called when the object is first constructed, and prior to + * any code generation. */ + void set_allow_jit(bool allow) { upb_pbcodecache_setallowjit(ptr(), allow); } -inline const Handlers* DecoderMethod::dest_handlers() const { - return upb_pbdecodermethod_desthandlers(this); -} -inline const BytesHandler* DecoderMethod::input_handler() const { - return upb_pbdecodermethod_inputhandler(this); -} -inline bool DecoderMethod::is_native() const { - return upb_pbdecodermethod_isnative(this); -} -/* static */ -inline reffed_ptr DecoderMethod::New( - const DecoderMethodOptions &opts) { - const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m); - return reffed_ptr(m, &m); -} + /* Should the decoder push submessages to lazy handlers for fields that have + * them? The caller should set this iff the lazy handlers expect data that is + * in protobuf binary format and the caller wishes to lazy parse it. */ + void set_lazy(bool lazy) { upb_pbcodecache_setlazy(ptr(), lazy); } -inline CodeCache::CodeCache() { - upb_pbcodecache_init(this); -} -inline CodeCache::~CodeCache() { - upb_pbcodecache_uninit(this); -} -inline bool CodeCache::allow_jit() const { - return upb_pbcodecache_allowjit(this); -} -inline bool CodeCache::set_allow_jit(bool allow) { - return upb_pbcodecache_setallowjit(this, allow); -} -inline const DecoderMethod *CodeCache::GetDecoderMethod( - const DecoderMethodOptions& opts) { - return upb_pbcodecache_getdecodermethod(this, &opts); -} + /* Returns a DecoderMethod that can push data to the given handlers. + * If a suitable method already exists, it will be returned from the cache. */ + const DecoderMethodPtr Get(MessageDefPtr md) { + return DecoderMethodPtr(upb_pbcodecache_get(ptr(), md.ptr())); + } -} /* namespace pb */ -} /* namespace upb */ + private: + std::unique_ptr ptr_; +}; #endif /* __cplusplus */ #endif /* UPB_DECODER_H_ */ -/* C++ names are not actually used since this type isn't exposed to users. */ -#ifdef __cplusplus -namespace upb { -namespace pb { -class MessageGroup; -} /* namespace pb */ -} /* namespace upb */ -#endif -UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted, - mgroup, upb_refcounted) - /* Opcode definitions. The canonical meaning of each opcode is its * implementation in the interpreter (the JIT is written to match this). * @@ -8790,31 +6499,26 @@ typedef enum { #define OP_MAX OP_HALT -UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; } +UPB_INLINE opcode getop(uint32_t instr) { return (opcode)(instr & 0xff); } -/* Method group; represents a set of decoder methods that had their code - * emitted together, and must therefore be freed together. Immutable once - * created. It is possible we may want to expose this to users at some point. - * - * Overall ownership of Decoder objects looks like this: - * - * +----------+ - * | | <---> DecoderMethod - * | method | - * CodeCache ---> | group | <---> DecoderMethod - * | | - * | (mgroup) | <---> DecoderMethod - * +----------+ - */ -struct mgroup { - upb_refcounted base; +struct upb_pbcodecache { + upb_arena *arena; + upb_handlercache *dest; + bool allow_jit; + bool lazy; - /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. We own refs on the - * methods. */ - upb_inttable methods; + /* Array of mgroups. */ + upb_inttable groups; +}; - /* When we add the ability to link to previously existing mgroups, we'll - * need an array of mgroups we reference here, and own refs on them. */ +/* Method group; represents a set of decoder methods that had their code + * emitted together. Immutable once created. */ +typedef struct { + /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. Owned by us. + * + * Ideally this would be on pbcodecache (if we were actually caching code). + * Right now we don't actually cache anything, which is wasteful. */ + upb_inttable methods; /* The bytecode for our methods, if any exists. Owned by us. */ uint32_t *bytecode; @@ -8828,7 +6532,7 @@ struct mgroup { char *debug_info; void *dl; #endif -}; +} mgroup; /* The maximum that any submessages can be nested. Matches proto2's limit. * This specifies the size of the decoder's statically-sized array and therefore @@ -8868,8 +6572,6 @@ typedef struct { } upb_pbdecoder_frame; struct upb_pbdecodermethod { - upb_refcounted base; - /* While compiling, the base is relative in "ofs", after compiling it is * absolute in "ptr". */ union { @@ -8877,14 +6579,8 @@ struct upb_pbdecodermethod { void *ptr; /* Pointer to bytecode or machine code for this method. */ } code_base; - /* The decoder method group to which this method belongs. We own a ref. - * Owning a ref on the entire group is more coarse-grained than is strictly - * necessary; all we truly require is that methods we directly reference - * outlive us, while the group could contain many other messages we don't - * require. But the group represents the messages that were - * allocated+compiled together, so it makes the most sense to free them - * together also. */ - const upb_refcounted *group; + /* The decoder method group to which this method belongs. */ + const mgroup *group; /* Whether this method is native code or bytecode. */ bool is_native_; @@ -8902,7 +6598,7 @@ struct upb_pbdecodermethod { }; struct upb_pbdecoder { - upb_env *env; + upb_arena *arena; /* Our input sink. */ upb_bytessink input_; @@ -8985,7 +6681,6 @@ const char *upb_pbdecoder_getopname(unsigned int op); /* JIT codegen entry point. */ void upb_pbdecoder_jit(mgroup *group); void upb_pbdecoder_freejit(mgroup *group); -UPB_REFCOUNTED_CMETHODS(mgroup, mgroup_upcast) /* A special label that means "do field dispatch for this message and branch to * wherever that takes you." */ @@ -9063,6 +6758,18 @@ extern "C" { * descriptor type (upb_descriptortype_t). */ extern const uint8_t upb_pb_native_wire_types[]; +UPB_INLINE uint64_t byteswap64(uint64_t val) +{ + return ((((val) & 0xff00000000000000ull) >> 56) + | (((val) & 0x00ff000000000000ull) >> 40) + | (((val) & 0x0000ff0000000000ull) >> 24) + | (((val) & 0x000000ff00000000ull) >> 8) + | (((val) & 0x00000000ff000000ull) << 8) + | (((val) & 0x0000000000ff0000ull) << 24) + | (((val) & 0x000000000000ff00ull) << 40) + | (((val) & 0x00000000000000ffull) << 56)); +} + /* Zig-zag encoding/decoding **************************************************/ UPB_INLINE int32_t upb_zzdec_32(uint32_t n) { @@ -9169,6 +6876,9 @@ UPB_INLINE uint64_t upb_vencode32(uint32_t val) { uint64_t ret = 0; UPB_ASSERT(bytes <= 5); memcpy(&ret, buf, bytes); +#ifdef UPB_BIG_ENDIAN + ret = byteswap64(ret); +#endif UPB_ASSERT(ret <= 0xffffffffffU); return ret; } @@ -9196,149 +6906,70 @@ UPB_INLINE uint64_t upb_vencode32(uint32_t val) { #ifdef __cplusplus namespace upb { namespace pb { -class Encoder; +class EncoderPtr; } /* namespace pb */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder) - #define UPB_PBENCODER_MAX_NESTING 100 -/* upb::pb::Encoder ***********************************************************/ +/* upb_pb_encoder *************************************************************/ /* Preallocation hint: decoder won't allocate more bytes than this when first * constructed. This hint may be an overestimate for some build configurations. * But if the decoder library is upgraded without recompiling the application, * it may be an underestimate. */ -#define UPB_PB_ENCODER_SIZE 768 - -#ifdef __cplusplus +#define UPB_PB_ENCODER_SIZE 784 -class upb::pb::Encoder { - public: - /* Creates a new encoder in the given environment. The Handlers must have - * come from NewHandlers() below. */ - static Encoder* Create(Environment* env, const Handlers* handlers, - BytesSink* output); - - /* The input to the encoder. */ - Sink* input(); - - /* Creates a new set of handlers for this MessageDef. */ - static reffed_ptr NewHandlers(const MessageDef* msg); - - static const size_t kSize = UPB_PB_ENCODER_SIZE; - - private: - UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder) -}; - -#endif - -UPB_BEGIN_EXTERN_C - -const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m, - const void *owner); -upb_sink *upb_pb_encoder_input(upb_pb_encoder *p); -upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, - upb_bytessink* output); - -UPB_END_EXTERN_C +struct upb_pb_encoder; +typedef struct upb_pb_encoder upb_pb_encoder; #ifdef __cplusplus - -namespace upb { -namespace pb { -inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers, - BytesSink* output) { - return upb_pb_encoder_create(env, handlers, output); -} -inline Sink* Encoder::input() { - return upb_pb_encoder_input(this); -} -inline reffed_ptr Encoder::NewHandlers( - const upb::MessageDef *md) { - const Handlers* h = upb_pb_encoder_newhandlers(md, &h); - return reffed_ptr(h, &h); -} -} /* namespace pb */ -} /* namespace upb */ - +extern "C" { #endif -#endif /* UPB_ENCODER_H_ */ -/* -** upb's core components like upb_decoder and upb_msg are carefully designed to -** avoid depending on each other for maximum orthogonality. In other words, -** you can use a upb_decoder to decode into *any* kind of structure; upb_msg is -** just one such structure. A upb_msg can be serialized/deserialized into any -** format, protobuf binary format is just one such format. -** -** However, for convenience we provide functions here for doing common -** operations like deserializing protobuf binary format into a upb_msg. The -** compromise is that this file drags in almost all of upb as a dependency, -** which could be undesirable if you're trying to use a trimmed-down build of -** upb. -** -** While these routines are convenient, they do not reuse any encoding/decoding -** state. For example, if a decoder is JIT-based, it will be re-JITted every -** time these functions are called. For this reason, if you are parsing lots -** of data and efficiency is an issue, these may not be the best functions to -** use (though they are useful for prototyping, before optimizing). -*/ - -#ifndef UPB_GLUE_H -#define UPB_GLUE_H +upb_sink upb_pb_encoder_input(upb_pb_encoder *p); +upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h, + upb_bytessink output); -#include +/* Lazily builds and caches handlers that will push encoded data to a bytessink. + * Any msgdef objects used with this object must outlive it. */ +upb_handlercache *upb_pb_encoder_newcache(); #ifdef __cplusplus -#include - -extern "C" { -#endif +} /* extern "C" { */ -/* Loads a binary descriptor and returns a NULL-terminated array of unfrozen - * filedefs. The caller owns the returned array, which must be freed with - * upb_gfree(). */ -upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner, - upb_status *status); +class upb::pb::EncoderPtr { + public: + EncoderPtr(upb_pb_encoder* ptr) : ptr_(ptr) {} -#ifdef __cplusplus -} /* extern "C" */ + upb_pb_encoder* ptr() { return ptr_; } -namespace upb { + /* Creates a new encoder in the given environment. The Handlers must have + * come from NewHandlers() below. */ + static EncoderPtr Create(Arena* arena, const Handlers* handlers, + BytesSink output) { + return EncoderPtr( + upb_pb_encoder_create(arena->ptr(), handlers, output.sink())); + } -inline bool LoadDescriptor(const char* buf, size_t n, Status* status, - std::vector >* files) { - FileDef** parsed_files = upb_loaddescriptor(buf, n, &parsed_files, status); + /* The input to the encoder. */ + upb::Sink input() { return upb_pb_encoder_input(ptr()); } - if (parsed_files) { - FileDef** p = parsed_files; - while (*p) { - files->push_back(reffed_ptr(*p, &parsed_files)); - ++p; - } - free(parsed_files); - return true; - } else { - return false; + /* Creates a new set of handlers for this MessageDef. */ + static HandlerCache NewCache() { + return HandlerCache(upb_pb_encoder_newcache()); } -} -/* Templated so it can accept both string and std::string. */ -template -bool LoadDescriptor(const T& desc, Status* status, - std::vector >* files) { - return LoadDescriptor(desc.c_str(), desc.size(), status, files); -} + static const size_t kSize = UPB_PB_ENCODER_SIZE; -} /* namespace upb */ + private: + upb_pb_encoder* ptr_; +}; -#endif +#endif /* __cplusplus */ -#endif /* UPB_GLUE_H */ +#endif /* UPB_ENCODER_H_ */ /* ** upb::pb::TextPrinter (upb_textprinter) ** @@ -9352,68 +6983,57 @@ bool LoadDescriptor(const T& desc, Status* status, #ifdef __cplusplus namespace upb { namespace pb { -class TextPrinter; +class TextPrinterPtr; } /* namespace pb */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter) +/* upb_textprinter ************************************************************/ + +struct upb_textprinter; +typedef struct upb_textprinter upb_textprinter; + +#ifdef __cplusplus +extern "C" { +#endif + +/* C API. */ +upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h, + upb_bytessink output); +void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); +upb_sink upb_textprinter_input(upb_textprinter *p); +upb_handlercache *upb_textprinter_newcache(); #ifdef __cplusplus +} /* extern "C" */ -class upb::pb::TextPrinter { +class upb::pb::TextPrinterPtr { public: + TextPrinterPtr(upb_textprinter* ptr) : ptr_(ptr) {} + /* The given handlers must have come from NewHandlers(). It must outlive the * TextPrinter. */ - static TextPrinter *Create(Environment *env, const upb::Handlers *handlers, - BytesSink *output); + static TextPrinterPtr Create(Arena *arena, upb::HandlersPtr *handlers, + BytesSink output) { + return TextPrinterPtr( + upb_textprinter_create(arena->ptr(), handlers->ptr(), output.sink())); + } - void SetSingleLineMode(bool single_line); + void SetSingleLineMode(bool single_line) { + upb_textprinter_setsingleline(ptr_, single_line); + } - Sink* input(); + Sink input() { return upb_textprinter_input(ptr_); } /* If handler caching becomes a requirement we can add a code cache as in * decoder.h */ - static reffed_ptr NewHandlers(const MessageDef* md); -}; - -#endif - -UPB_BEGIN_EXTERN_C - -/* C API. */ -upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output); -void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); -upb_sink *upb_textprinter_input(upb_textprinter *p); - -const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m, - const void *owner); - -UPB_END_EXTERN_C - -#ifdef __cplusplus + static HandlerCache NewCache() { + return HandlerCache(upb_textprinter_newcache()); + } -namespace upb { -namespace pb { -inline TextPrinter *TextPrinter::Create(Environment *env, - const upb::Handlers *handlers, - BytesSink *output) { - return upb_textprinter_create(env, handlers, output); -} -inline void TextPrinter::SetSingleLineMode(bool single_line) { - upb_textprinter_setsingleline(this, single_line); -} -inline Sink* TextPrinter::input() { - return upb_textprinter_input(this); -} -inline reffed_ptr TextPrinter::NewHandlers( - const MessageDef *md) { - const Handlers* h = upb_textprinter_newhandlers(md, &h); - return reffed_ptr(h, &h); -} -} /* namespace pb */ -} /* namespace upb */ + private: + upb_textprinter* ptr_; +}; #endif @@ -9432,112 +7052,130 @@ inline reffed_ptr TextPrinter::NewHandlers( #ifdef __cplusplus namespace upb { namespace json { -class Parser; -class ParserMethod; +class CodeCache; +class ParserPtr; +class ParserMethodPtr; } /* namespace json */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser) -UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted, - upb_json_parsermethod, upb_refcounted) +/* upb_json_parsermethod ******************************************************/ -/* upb::json::Parser **********************************************************/ +struct upb_json_parsermethod; +typedef struct upb_json_parsermethod upb_json_parsermethod; -/* Preallocation hint: parser won't allocate more bytes than this when first - * constructed. This hint may be an overestimate for some build configurations. - * But if the parser library is upgraded without recompiling the application, - * it may be an underestimate. */ -#define UPB_JSON_PARSER_SIZE 4112 +#ifdef __cplusplus +extern "C" { +#endif + +const upb_byteshandler* upb_json_parsermethod_inputhandler( + const upb_json_parsermethod* m); #ifdef __cplusplus +} /* extern "C" */ -/* Parses an incoming BytesStream, pushing the results to the destination - * sink. */ -class upb::json::Parser { +class upb::json::ParserMethodPtr { public: - static Parser* Create(Environment* env, const ParserMethod* method, - Sink* output, bool ignore_json_unknown); + ParserMethodPtr() : ptr_(nullptr) {} + ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {} + + const upb_json_parsermethod* ptr() const { return ptr_; } - BytesSink* input(); + const BytesHandler* input_handler() const { + return upb_json_parsermethod_inputhandler(ptr()); + } private: - UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser) + const upb_json_parsermethod* ptr_; }; -class upb::json::ParserMethod { - public: - /* Include base methods from upb::ReferenceCounted. */ - UPB_REFCOUNTED_CPPMETHODS +#endif /* __cplusplus */ - /* Returns handlers for parsing according to the specified schema. */ - static reffed_ptr New(const upb::MessageDef* md); +/* upb_json_parser ************************************************************/ - /* The destination handlers that are statically bound to this method. - * This method is only capable of outputting to a sink that uses these - * handlers. */ - const Handlers* dest_handlers() const; +/* Preallocation hint: parser won't allocate more bytes than this when first + * constructed. This hint may be an overestimate for some build configurations. + * But if the parser library is upgraded without recompiling the application, + * it may be an underestimate. */ +#define UPB_JSON_PARSER_SIZE 5712 - /* The input handlers for this decoder method. */ - const BytesHandler* input_handler() const; +struct upb_json_parser; +typedef struct upb_json_parser upb_json_parser; + +#ifdef __cplusplus +extern "C" { +#endif + +upb_json_parser* upb_json_parser_create(upb_arena* a, + const upb_json_parsermethod* m, + const upb_symtab* symtab, + upb_sink output, + upb_status *status, + bool ignore_json_unknown); +upb_bytessink upb_json_parser_input(upb_json_parser* p); + +#ifdef __cplusplus +} /* extern "C" */ + +/* Parses an incoming BytesStream, pushing the results to the destination + * sink. */ +class upb::json::ParserPtr { + public: + ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {} + + static ParserPtr Create(Arena* arena, ParserMethodPtr method, + SymbolTable* symtab, Sink output, Status* status, + bool ignore_json_unknown) { + upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr; + return ParserPtr(upb_json_parser_create( + arena->ptr(), method.ptr(), symtab_ptr, output.sink(), status->ptr(), + ignore_json_unknown)); + } + + BytesSink input() { return upb_json_parser_input(ptr_); } private: - UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod) + upb_json_parser* ptr_; }; -#endif +#endif /* __cplusplus */ -UPB_BEGIN_EXTERN_C +/* upb_json_codecache *********************************************************/ -upb_json_parser* upb_json_parser_create(upb_env* e, - const upb_json_parsermethod* m, - upb_sink* output, - bool ignore_json_unknown); -upb_bytessink *upb_json_parser_input(upb_json_parser *p); +/* Lazily builds and caches decoder methods that will push data to the given + * handlers. The upb_symtab object(s) must outlive this object. */ -upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md, - const void* owner); -const upb_handlers *upb_json_parsermethod_desthandlers( - const upb_json_parsermethod *m); -const upb_byteshandler *upb_json_parsermethod_inputhandler( - const upb_json_parsermethod *m); +struct upb_json_codecache; +typedef struct upb_json_codecache upb_json_codecache; -/* Include refcounted methods like upb_json_parsermethod_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_json_parsermethod, upb_json_parsermethod_upcast) +#ifdef __cplusplus +extern "C" { +#endif -UPB_END_EXTERN_C +upb_json_codecache *upb_json_codecache_new(); +void upb_json_codecache_free(upb_json_codecache *cache); +const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, + const upb_msgdef* md); #ifdef __cplusplus +} /* extern "C" */ -namespace upb { -namespace json { -inline Parser* Parser::Create(Environment* env, const ParserMethod* method, - Sink* output, bool ignore_json_unknown) { - return upb_json_parser_create(env, method, output, ignore_json_unknown); -} -inline BytesSink* Parser::input() { - return upb_json_parser_input(this); -} +class upb::json::CodeCache { + public: + CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {} -inline const Handlers* ParserMethod::dest_handlers() const { - return upb_json_parsermethod_desthandlers(this); -} -inline const BytesHandler* ParserMethod::input_handler() const { - return upb_json_parsermethod_inputhandler(this); -} -/* static */ -inline reffed_ptr ParserMethod::New( - const MessageDef* md) { - const upb_json_parsermethod *m = upb_json_parsermethod_new(md, &m); - return reffed_ptr(m, &m); -} + /* Returns a DecoderMethod that can push data to the given handlers. + * If a suitable method already exists, it will be returned from the cache. */ + ParserMethodPtr Get(MessageDefPtr md) { + return upb_json_codecache_get(ptr_.get(), md.ptr()); + } -} /* namespace json */ -} /* namespace upb */ + private: + std::unique_ptr ptr_; +}; #endif - #endif /* UPB_JSON_PARSER_H_ */ /* ** upb::json::Printer @@ -9552,74 +7190,66 @@ inline reffed_ptr ParserMethod::New( #ifdef __cplusplus namespace upb { namespace json { -class Printer; +class PrinterPtr; } /* namespace json */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer) +/* upb_json_printer ***********************************************************/ +#define UPB_JSON_PRINTER_SIZE 192 + +struct upb_json_printer; +typedef struct upb_json_printer upb_json_printer; + +#ifdef __cplusplus +extern "C" { +#endif -/* upb::json::Printer *********************************************************/ +/* Native C API. */ +upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h, + upb_bytessink output); +upb_sink upb_json_printer_input(upb_json_printer *p); +const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, + bool preserve_fieldnames, + const void *owner); -#define UPB_JSON_PRINTER_SIZE 176 +/* Lazily builds and caches handlers that will push encoded data to a bytessink. + * Any msgdef objects used with this object must outlive it. */ +upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames); #ifdef __cplusplus +} /* extern "C" */ /* Prints an incoming stream of data to a BytesSink in JSON format. */ -class upb::json::Printer { +class upb::json::PrinterPtr { public: - static Printer* Create(Environment* env, const upb::Handlers* handlers, - BytesSink* output); + PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {} - /* The input to the printer. */ - Sink* input(); + static PrinterPtr Create(Arena *arena, const upb::Handlers *handlers, + BytesSink output) { + return PrinterPtr( + upb_json_printer_create(arena->ptr(), handlers, output.sink())); + } - /* Returns handlers for printing according to the specified schema. - * If preserve_proto_fieldnames is true, the output JSON will use the - * original .proto field names (ie. {"my_field":3}) instead of using - * camelCased names, which is the default: (eg. {"myField":3}). */ - static reffed_ptr NewHandlers(const upb::MessageDef* md, - bool preserve_proto_fieldnames); + /* The input to the printer. */ + Sink input() { return upb_json_printer_input(ptr_); } static const size_t kSize = UPB_JSON_PRINTER_SIZE; + static HandlerCache NewCache(bool preserve_proto_fieldnames) { + return upb_json_printer_newcache(preserve_proto_fieldnames); + } + private: - UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer) + upb_json_printer* ptr_; }; -#endif - -UPB_BEGIN_EXTERN_C - -/* Native C API. */ -upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, - upb_bytessink *output); -upb_sink *upb_json_printer_input(upb_json_printer *p); -const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, - bool preserve_fieldnames, - const void *owner); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -namespace upb { -namespace json { -inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers, - BytesSink* output) { - return upb_json_printer_create(env, handlers, output); -} -inline Sink* Printer::input() { return upb_json_printer_input(this); } -inline reffed_ptr Printer::NewHandlers( - const upb::MessageDef *md, bool preserve_proto_fieldnames) { - const Handlers* h = upb_json_printer_newhandlers( - md, preserve_proto_fieldnames, &h); - return reffed_ptr(h, &h); -} -} /* namespace json */ -} /* namespace upb */ - -#endif +#endif /* __cplusplus */ #endif /* UPB_JSON_TYPED_PRINTER_H_ */ + +#undef UPB_SIZE +#undef UPB_FIELD_AT +#undef UPB_READ_ONEOF +#undef UPB_WRITE_ONEOF diff --git a/php/phpunit.xml b/php/phpunit.xml index 87440bbdbb..769037cf9a 100644 --- a/php/phpunit.xml +++ b/php/phpunit.xml @@ -12,6 +12,7 @@ tests/well_known_test.php tests/descriptors_test.php tests/generated_service_test.php + tests/wrapper_type_setters_test.php diff --git a/php/release.sh b/php/release.sh new file mode 100755 index 0000000000..6b0baac78f --- /dev/null +++ b/php/release.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +set -ex + +# Make sure we are in a protobuf source tree. +[ -f "php/release.sh" ] || { + echo "This script must be ran under root of protobuf source tree." + exit 1 +} + +VERSION=$1 + +git clone https://github.com/protocolbuffers/protobuf-php.git +git clone https://github.com/protocolbuffers/protobuf.git + +# Clean old files +pushd protobuf-php +rm -rf src +popd + +# Checkout the target version +pushd protobuf/php +git checkout -b $VERSION +popd + +# Copy files +pushd protobuf-php +mv ../protobuf/php/src src +mv ../protobuf/composer.json composer.json +sed -i 's|php/src|src|g' composer.json +git add . +git commit -m "$VERSION" +git tag "$VERSION" +popd + +# Clean up +rm -rf protobuf diff --git a/php/src/Google/Protobuf/Any.php b/php/src/Google/Protobuf/Any.php index a992854957..cdbaa255a1 100644 --- a/php/src/Google/Protobuf/Any.php +++ b/php/src/Google/Protobuf/Any.php @@ -252,9 +252,9 @@ class Any extends \Google\Protobuf\Internal\Message * the method will create a message of the targeted type and fill it with * the decoded value in Any. * @return unpacked message - * @throws Exception Type url needs to be type.googleapis.com/fully-qulified. - * @throws Exception Class hasn't been added to descriptor pool. - * @throws Exception cannot decode data in value field. + * @throws \Exception Type url needs to be type.googleapis.com/fully-qulified. + * @throws \Exception Class hasn't been added to descriptor pool. + * @throws \Exception cannot decode data in value field. */ public function unpack() { diff --git a/php/src/Google/Protobuf/Field/Cardinality.php b/php/src/Google/Protobuf/Field/Cardinality.php index c887f6d31d..479dc0bfc2 100644 --- a/php/src/Google/Protobuf/Field/Cardinality.php +++ b/php/src/Google/Protobuf/Field/Cardinality.php @@ -4,6 +4,8 @@ namespace Google\Protobuf\Field; +use UnexpectedValueException; + /** * Whether a field is optional, required, or repeated. * @@ -35,6 +37,32 @@ class Cardinality * Generated from protobuf enum CARDINALITY_REPEATED = 3; */ const CARDINALITY_REPEATED = 3; + + private static $valueToName = [ + self::CARDINALITY_UNKNOWN => 'CARDINALITY_UNKNOWN', + self::CARDINALITY_OPTIONAL => 'CARDINALITY_OPTIONAL', + self::CARDINALITY_REQUIRED => 'CARDINALITY_REQUIRED', + self::CARDINALITY_REPEATED => 'CARDINALITY_REPEATED', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } // Adding a class alias for backwards compatibility with the previous class name. diff --git a/php/src/Google/Protobuf/Field/Kind.php b/php/src/Google/Protobuf/Field/Kind.php index a2bbbdebbf..f30bd2f59b 100644 --- a/php/src/Google/Protobuf/Field/Kind.php +++ b/php/src/Google/Protobuf/Field/Kind.php @@ -4,6 +4,8 @@ namespace Google\Protobuf\Field; +use UnexpectedValueException; + /** * Basic field types. * @@ -125,6 +127,47 @@ class Kind * Generated from protobuf enum TYPE_SINT64 = 18; */ const TYPE_SINT64 = 18; + + private static $valueToName = [ + self::TYPE_UNKNOWN => 'TYPE_UNKNOWN', + self::TYPE_DOUBLE => 'TYPE_DOUBLE', + self::TYPE_FLOAT => 'TYPE_FLOAT', + self::TYPE_INT64 => 'TYPE_INT64', + self::TYPE_UINT64 => 'TYPE_UINT64', + self::TYPE_INT32 => 'TYPE_INT32', + self::TYPE_FIXED64 => 'TYPE_FIXED64', + self::TYPE_FIXED32 => 'TYPE_FIXED32', + self::TYPE_BOOL => 'TYPE_BOOL', + self::TYPE_STRING => 'TYPE_STRING', + self::TYPE_GROUP => 'TYPE_GROUP', + self::TYPE_MESSAGE => 'TYPE_MESSAGE', + self::TYPE_BYTES => 'TYPE_BYTES', + self::TYPE_UINT32 => 'TYPE_UINT32', + self::TYPE_ENUM => 'TYPE_ENUM', + self::TYPE_SFIXED32 => 'TYPE_SFIXED32', + self::TYPE_SFIXED64 => 'TYPE_SFIXED64', + self::TYPE_SINT32 => 'TYPE_SINT32', + self::TYPE_SINT64 => 'TYPE_SINT64', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } // Adding a class alias for backwards compatibility with the previous class name. diff --git a/php/src/Google/Protobuf/Internal/CodedInputStream.php b/php/src/Google/Protobuf/Internal/CodedInputStream.php index b612da5b7b..33f4a456af 100644 --- a/php/src/Google/Protobuf/Internal/CodedInputStream.php +++ b/php/src/Google/Protobuf/Internal/CodedInputStream.php @@ -317,7 +317,7 @@ class CodedInputStream * passed unchanged to the corresponding call to popLimit(). * * @param integer $byte_limit - * @throws Exception Fail to push limit. + * @throws \Exception Fail to push limit. */ public function pushLimit($byte_limit) { diff --git a/php/src/Google/Protobuf/Internal/DescriptorPool.php b/php/src/Google/Protobuf/Internal/DescriptorPool.php index 9b4dcc0115..419bbf4dca 100644 --- a/php/src/Google/Protobuf/Internal/DescriptorPool.php +++ b/php/src/Google/Protobuf/Internal/DescriptorPool.php @@ -55,7 +55,7 @@ class DescriptorPool return self::$pool; } - public function internalAddGeneratedFile($data) + public function internalAddGeneratedFile($data, $use_nested = false) { $files = new FileDescriptorSet(); $files->mergeFromString($data); diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto.php b/php/src/Google/Protobuf/Internal/DescriptorProto.php index 3b215d52ae..2d52831451 100644 --- a/php/src/Google/Protobuf/Internal/DescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/DescriptorProto.php @@ -15,7 +15,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.DescriptorProto */ -class DescriptorProto extends \Google\Protobuf\Internal\Message +final class DescriptorProto extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field optional string name = 1; diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto/ExtensionRange.php b/php/src/Google/Protobuf/Internal/DescriptorProto/ExtensionRange.php index c06a0a6ea9..fb98f7d5a0 100644 --- a/php/src/Google/Protobuf/Internal/DescriptorProto/ExtensionRange.php +++ b/php/src/Google/Protobuf/Internal/DescriptorProto/ExtensionRange.php @@ -13,14 +13,18 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.DescriptorProto.ExtensionRange */ -class ExtensionRange extends \Google\Protobuf\Internal\Message +final class ExtensionRange extends \Google\Protobuf\Internal\Message { /** + * Inclusive. + * * Generated from protobuf field optional int32 start = 1; */ private $start = 0; private $has_start = false; /** + * Exclusive. + * * Generated from protobuf field optional int32 end = 2; */ private $end = 0; @@ -38,7 +42,9 @@ class ExtensionRange extends \Google\Protobuf\Internal\Message * Optional. Data for populating the Message object. * * @type int $start + * Inclusive. * @type int $end + * Exclusive. * @type \Google\Protobuf\Internal\ExtensionRangeOptions $options * } */ @@ -48,6 +54,8 @@ class ExtensionRange extends \Google\Protobuf\Internal\Message } /** + * Inclusive. + * * Generated from protobuf field optional int32 start = 1; * @return int */ @@ -57,6 +65,8 @@ class ExtensionRange extends \Google\Protobuf\Internal\Message } /** + * Inclusive. + * * Generated from protobuf field optional int32 start = 1; * @param int $var * @return $this @@ -76,6 +86,8 @@ class ExtensionRange extends \Google\Protobuf\Internal\Message } /** + * Exclusive. + * * Generated from protobuf field optional int32 end = 2; * @return int */ @@ -85,6 +97,8 @@ class ExtensionRange extends \Google\Protobuf\Internal\Message } /** + * Exclusive. + * * Generated from protobuf field optional int32 end = 2; * @param int $var * @return $this diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto/ReservedRange.php b/php/src/Google/Protobuf/Internal/DescriptorProto/ReservedRange.php index 73c964faa0..fa46419a53 100644 --- a/php/src/Google/Protobuf/Internal/DescriptorProto/ReservedRange.php +++ b/php/src/Google/Protobuf/Internal/DescriptorProto/ReservedRange.php @@ -17,7 +17,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.DescriptorProto.ReservedRange */ -class ReservedRange extends \Google\Protobuf\Internal\Message +final class ReservedRange extends \Google\Protobuf\Internal\Message { /** * Inclusive. diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptor.php b/php/src/Google/Protobuf/Internal/EnumDescriptor.php index 82a427670c..7af4f84012 100644 --- a/php/src/Google/Protobuf/Internal/EnumDescriptor.php +++ b/php/src/Google/Protobuf/Internal/EnumDescriptor.php @@ -39,17 +39,26 @@ class EnumDescriptor public function getValueByNumber($number) { - return $this->value[$number]; + if (isset($this->value[$number])) { + return $this->value[$number]; + } + return null; } public function getValueByName($name) { - return $this->name_to_value[$name]; + if (isset($this->name_to_value[$name])) { + return $this->name_to_value[$name]; + } + return null; } public function getValueDescriptorByIndex($index) { - return $this->value_descriptor[$index]; + if (isset($this->value_descriptor[$index])) { + return $this->value_descriptor[$index]; + } + return null; } public function getValueCount() diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php b/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php index da30fa9906..b7c3a208be 100644 --- a/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php @@ -15,7 +15,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.EnumDescriptorProto */ -class EnumDescriptorProto extends \Google\Protobuf\Internal\Message +final class EnumDescriptorProto extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field optional string name = 1; diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptorProto/EnumReservedRange.php b/php/src/Google/Protobuf/Internal/EnumDescriptorProto/EnumReservedRange.php index e1079585ec..0103a1b301 100644 --- a/php/src/Google/Protobuf/Internal/EnumDescriptorProto/EnumReservedRange.php +++ b/php/src/Google/Protobuf/Internal/EnumDescriptorProto/EnumReservedRange.php @@ -19,7 +19,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.EnumDescriptorProto.EnumReservedRange */ -class EnumReservedRange extends \Google\Protobuf\Internal\Message +final class EnumReservedRange extends \Google\Protobuf\Internal\Message { /** * Inclusive. diff --git a/php/src/Google/Protobuf/Internal/EnumOptions.php b/php/src/Google/Protobuf/Internal/EnumOptions.php index 3d74c81c21..4c73d52740 100644 --- a/php/src/Google/Protobuf/Internal/EnumOptions.php +++ b/php/src/Google/Protobuf/Internal/EnumOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.EnumOptions */ -class EnumOptions extends \Google\Protobuf\Internal\Message +final class EnumOptions extends \Google\Protobuf\Internal\Message { /** * Set this option to true to allow mapping different tag names to the same diff --git a/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php b/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php index 50bda008e8..d5a0a9afb5 100644 --- a/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php @@ -15,7 +15,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.EnumValueDescriptorProto */ -class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message +final class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field optional string name = 1; diff --git a/php/src/Google/Protobuf/Internal/EnumValueOptions.php b/php/src/Google/Protobuf/Internal/EnumValueOptions.php index a267c6d5e2..50ac904ff9 100644 --- a/php/src/Google/Protobuf/Internal/EnumValueOptions.php +++ b/php/src/Google/Protobuf/Internal/EnumValueOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.EnumValueOptions */ -class EnumValueOptions extends \Google\Protobuf\Internal\Message +final class EnumValueOptions extends \Google\Protobuf\Internal\Message { /** * Is this enum value deprecated? diff --git a/php/src/Google/Protobuf/Internal/ExtensionRangeOptions.php b/php/src/Google/Protobuf/Internal/ExtensionRangeOptions.php index 00fbebecaf..97ad2c055f 100644 --- a/php/src/Google/Protobuf/Internal/ExtensionRangeOptions.php +++ b/php/src/Google/Protobuf/Internal/ExtensionRangeOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.ExtensionRangeOptions */ -class ExtensionRangeOptions extends \Google\Protobuf\Internal\Message +final class ExtensionRangeOptions extends \Google\Protobuf\Internal\Message { /** * The parser stores options it doesn't recognize here. See above. diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptor.php b/php/src/Google/Protobuf/Internal/FieldDescriptor.php index 6644a2e053..98b516fecc 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptor.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptor.php @@ -187,6 +187,25 @@ class FieldDescriptor $this->getMessageType()->getClass() === "Google\Protobuf\Timestamp"; } + public function isWrapperType() + { + if ($this->getType() == GPBType::MESSAGE) { + $class = $this->getMessageType()->getClass(); + return in_array($class, [ + "Google\Protobuf\DoubleValue", + "Google\Protobuf\FloatValue", + "Google\Protobuf\Int64Value", + "Google\Protobuf\UInt64Value", + "Google\Protobuf\Int32Value", + "Google\Protobuf\UInt32Value", + "Google\Protobuf\BoolValue", + "Google\Protobuf\StringValue", + "Google\Protobuf\BytesValue", + ]); + } + return false; + } + private static function isTypePackable($field_type) { return ($field_type !== GPBType::STRING && diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php b/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php index e578197515..6f8643c8d7 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php @@ -15,7 +15,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.FieldDescriptorProto */ -class FieldDescriptorProto extends \Google\Protobuf\Internal\Message +final class FieldDescriptorProto extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field optional string name = 1; diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptorProto/Label.php b/php/src/Google/Protobuf/Internal/FieldDescriptorProto/Label.php index b105088e6a..a54b228f1b 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptorProto/Label.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptorProto/Label.php @@ -4,6 +4,8 @@ namespace Google\Protobuf\Internal\FieldDescriptorProto; +use UnexpectedValueException; + /** * Protobuf type google.protobuf.FieldDescriptorProto.Label */ @@ -23,6 +25,32 @@ class Label * Generated from protobuf enum LABEL_REPEATED = 3; */ const LABEL_REPEATED = 3; + + private static $valueToName = [ + self::LABEL_OPTIONAL => 'LABEL_OPTIONAL', + self::LABEL_REQUIRED => 'LABEL_REQUIRED', + self::LABEL_REPEATED => 'LABEL_REPEATED', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } // Adding a class alias for backwards compatibility with the previous class name. diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptorProto/Type.php b/php/src/Google/Protobuf/Internal/FieldDescriptorProto/Type.php index ea228a77fe..6072e9990f 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptorProto/Type.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptorProto/Type.php @@ -4,6 +4,8 @@ namespace Google\Protobuf\Internal\FieldDescriptorProto; +use UnexpectedValueException; + /** * Protobuf type google.protobuf.FieldDescriptorProto.Type */ @@ -103,6 +105,47 @@ class Type * Generated from protobuf enum TYPE_SINT64 = 18; */ const TYPE_SINT64 = 18; + + private static $valueToName = [ + self::TYPE_DOUBLE => 'TYPE_DOUBLE', + self::TYPE_FLOAT => 'TYPE_FLOAT', + self::TYPE_INT64 => 'TYPE_INT64', + self::TYPE_UINT64 => 'TYPE_UINT64', + self::TYPE_INT32 => 'TYPE_INT32', + self::TYPE_FIXED64 => 'TYPE_FIXED64', + self::TYPE_FIXED32 => 'TYPE_FIXED32', + self::TYPE_BOOL => 'TYPE_BOOL', + self::TYPE_STRING => 'TYPE_STRING', + self::TYPE_GROUP => 'TYPE_GROUP', + self::TYPE_MESSAGE => 'TYPE_MESSAGE', + self::TYPE_BYTES => 'TYPE_BYTES', + self::TYPE_UINT32 => 'TYPE_UINT32', + self::TYPE_ENUM => 'TYPE_ENUM', + self::TYPE_SFIXED32 => 'TYPE_SFIXED32', + self::TYPE_SFIXED64 => 'TYPE_SFIXED64', + self::TYPE_SINT32 => 'TYPE_SINT32', + self::TYPE_SINT64 => 'TYPE_SINT64', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } // Adding a class alias for backwards compatibility with the previous class name. diff --git a/php/src/Google/Protobuf/Internal/FieldOptions.php b/php/src/Google/Protobuf/Internal/FieldOptions.php index 751c278d78..47d1951d16 100644 --- a/php/src/Google/Protobuf/Internal/FieldOptions.php +++ b/php/src/Google/Protobuf/Internal/FieldOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.FieldOptions */ -class FieldOptions extends \Google\Protobuf\Internal\Message +final class FieldOptions extends \Google\Protobuf\Internal\Message { /** * The ctype option instructs the C++ code generator to use a different diff --git a/php/src/Google/Protobuf/Internal/FieldOptions/CType.php b/php/src/Google/Protobuf/Internal/FieldOptions/CType.php index 016197acb5..ba9eb4adb8 100644 --- a/php/src/Google/Protobuf/Internal/FieldOptions/CType.php +++ b/php/src/Google/Protobuf/Internal/FieldOptions/CType.php @@ -4,6 +4,8 @@ namespace Google\Protobuf\Internal\FieldOptions; +use UnexpectedValueException; + /** * Protobuf type google.protobuf.FieldOptions.CType */ @@ -23,6 +25,32 @@ class CType * Generated from protobuf enum STRING_PIECE = 2; */ const STRING_PIECE = 2; + + private static $valueToName = [ + self::STRING => 'STRING', + self::CORD => 'CORD', + self::STRING_PIECE => 'STRING_PIECE', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } // Adding a class alias for backwards compatibility with the previous class name. diff --git a/php/src/Google/Protobuf/Internal/FieldOptions/JSType.php b/php/src/Google/Protobuf/Internal/FieldOptions/JSType.php index f7b78a1b31..175a4330bb 100644 --- a/php/src/Google/Protobuf/Internal/FieldOptions/JSType.php +++ b/php/src/Google/Protobuf/Internal/FieldOptions/JSType.php @@ -4,6 +4,8 @@ namespace Google\Protobuf\Internal\FieldOptions; +use UnexpectedValueException; + /** * Protobuf type google.protobuf.FieldOptions.JSType */ @@ -27,6 +29,32 @@ class JSType * Generated from protobuf enum JS_NUMBER = 2; */ const JS_NUMBER = 2; + + private static $valueToName = [ + self::JS_NORMAL => 'JS_NORMAL', + self::JS_STRING => 'JS_STRING', + self::JS_NUMBER => 'JS_NUMBER', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } // Adding a class alias for backwards compatibility with the previous class name. diff --git a/php/src/Google/Protobuf/Internal/FileDescriptorProto.php b/php/src/Google/Protobuf/Internal/FileDescriptorProto.php index cb10aa7939..e95205476c 100644 --- a/php/src/Google/Protobuf/Internal/FileDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/FileDescriptorProto.php @@ -15,7 +15,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.FileDescriptorProto */ -class FileDescriptorProto extends \Google\Protobuf\Internal\Message +final class FileDescriptorProto extends \Google\Protobuf\Internal\Message { /** * file name, relative to root of source tree diff --git a/php/src/Google/Protobuf/Internal/FileDescriptorSet.php b/php/src/Google/Protobuf/Internal/FileDescriptorSet.php index 9907b17d73..c9a38808fb 100644 --- a/php/src/Google/Protobuf/Internal/FileDescriptorSet.php +++ b/php/src/Google/Protobuf/Internal/FileDescriptorSet.php @@ -16,7 +16,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.FileDescriptorSet */ -class FileDescriptorSet extends \Google\Protobuf\Internal\Message +final class FileDescriptorSet extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field repeated .google.protobuf.FileDescriptorProto file = 1; diff --git a/php/src/Google/Protobuf/Internal/FileOptions.php b/php/src/Google/Protobuf/Internal/FileOptions.php index c6b36bbc89..9110005016 100644 --- a/php/src/Google/Protobuf/Internal/FileOptions.php +++ b/php/src/Google/Protobuf/Internal/FileOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.FileOptions */ -class FileOptions extends \Google\Protobuf\Internal\Message +final class FileOptions extends \Google\Protobuf\Internal\Message { /** * Sets the Java package where classes generated from this .proto will be @@ -175,8 +175,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message private $has_php_namespace = false; /** * Use this option to change the namespace of php generated metadata classes. - * Default is empty. When this option is empty, the proto file name will be used - * for determining the namespace. + * Default is empty. When this option is empty, the proto file name will be + * used for determining the namespace. * * Generated from protobuf field optional string php_metadata_namespace = 44; */ @@ -280,8 +280,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message * determining the namespace. * @type string $php_metadata_namespace * Use this option to change the namespace of php generated metadata classes. - * Default is empty. When this option is empty, the proto file name will be used - * for determining the namespace. + * Default is empty. When this option is empty, the proto file name will be + * used for determining the namespace. * @type string $ruby_package * Use this option to change the package of ruby generated classes. Default * is empty. When this option is not set, the package name will be used for @@ -938,8 +938,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message /** * Use this option to change the namespace of php generated metadata classes. - * Default is empty. When this option is empty, the proto file name will be used - * for determining the namespace. + * Default is empty. When this option is empty, the proto file name will be + * used for determining the namespace. * * Generated from protobuf field optional string php_metadata_namespace = 44; * @return string @@ -951,8 +951,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message /** * Use this option to change the namespace of php generated metadata classes. - * Default is empty. When this option is empty, the proto file name will be used - * for determining the namespace. + * Default is empty. When this option is empty, the proto file name will be + * used for determining the namespace. * * Generated from protobuf field optional string php_metadata_namespace = 44; * @param string $var diff --git a/php/src/Google/Protobuf/Internal/FileOptions/OptimizeMode.php b/php/src/Google/Protobuf/Internal/FileOptions/OptimizeMode.php index 3dd60bf6e1..0df27b5335 100644 --- a/php/src/Google/Protobuf/Internal/FileOptions/OptimizeMode.php +++ b/php/src/Google/Protobuf/Internal/FileOptions/OptimizeMode.php @@ -4,6 +4,8 @@ namespace Google\Protobuf\Internal\FileOptions; +use UnexpectedValueException; + /** * Generated classes can be optimized for speed or code size. * @@ -29,6 +31,32 @@ class OptimizeMode * Generated from protobuf enum LITE_RUNTIME = 3; */ const LITE_RUNTIME = 3; + + private static $valueToName = [ + self::SPEED => 'SPEED', + self::CODE_SIZE => 'CODE_SIZE', + self::LITE_RUNTIME => 'LITE_RUNTIME', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } // Adding a class alias for backwards compatibility with the previous class name. diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php index ec0bf6bdbe..7ec3ca2294 100644 --- a/php/src/Google/Protobuf/Internal/GPBUtil.php +++ b/php/src/Google/Protobuf/Internal/GPBUtil.php @@ -438,8 +438,8 @@ class GPBUtil $nanoseconds = intval($nanoseconds); // remove the nanoseconds and preceding period from the timestamp - $date = substr($timestamp, 0, $periodIndex - 1); - $timezone = substr($timestamp, $periodIndex + $nanosecondsLength); + $date = substr($timestamp, 0, $periodIndex); + $timezone = substr($timestamp, $periodIndex + $nanosecondsLength + 1); $timestamp = $date.$timezone; } } @@ -504,22 +504,37 @@ class GPBUtil public static function formatDuration($value) { - if (bccomp($value->getSeconds(), "315576000001") != -1) { - throw new GPBDecodeException("Duration number too large."); + if (bccomp($value->getSeconds(), '315576000001') != -1) { + throw new GPBDecodeException('Duration number too large.'); } - if (bccomp($value->getSeconds(), "-315576000001") != 1) { - throw new GPBDecodeException("Duration number too small."); + if (bccomp($value->getSeconds(), '-315576000001') != 1) { + throw new GPBDecodeException('Duration number too small.'); } - return strval(bcadd($value->getSeconds(), - $value->getNanos() / 1000000000.0, 9)); - } + $nanos = $value->getNanos(); + if ($nanos === 0) { + return (string) $value->getSeconds(); + } + if ($nanos % 1000000 === 0) { + $digits = 3; + } elseif ($nanos % 1000 === 0) { + $digits = 6; + } else { + $digits = 9; + } + + $nanos = bcdiv($nanos, '1000000000', $digits); + return bcadd($value->getSeconds(), $nanos, $digits); + } public static function parseFieldMask($paths_string) { - $path_strings = explode(",", $paths_string); $field_mask = new FieldMask(); + if (strlen($paths_string) === 0) { + return $field_mask; + } + $path_strings = explode(",", $paths_string); $paths = $field_mask->getPaths(); foreach($path_strings as &$path_string) { $field_strings = explode(".", $path_string); diff --git a/php/src/Google/Protobuf/Internal/GPBWire.php b/php/src/Google/Protobuf/Internal/GPBWire.php index e7eec552c8..7f1eab3c65 100644 --- a/php/src/Google/Protobuf/Internal/GPBWire.php +++ b/php/src/Google/Protobuf/Internal/GPBWire.php @@ -446,7 +446,7 @@ class GPBWire if (bccomp($value, 0) < 0 || bccomp($value, "9223372036854775807") > 0) { return 10; - } + } if (bccomp($value, 1 << 7) < 0) { return 1; } @@ -475,7 +475,7 @@ class GPBWire } else { if ($value < 0) { return 10; - } + } if ($value < (1 << 7)) { return 1; } diff --git a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php index f5a65bea46..c99d77afa3 100644 --- a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php +++ b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php @@ -17,7 +17,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.GeneratedCodeInfo */ -class GeneratedCodeInfo extends \Google\Protobuf\Internal\Message +final class GeneratedCodeInfo extends \Google\Protobuf\Internal\Message { /** * An Annotation connects some span of text in generated code to an element diff --git a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo/Annotation.php b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo/Annotation.php index 09f958d251..8cc3cdf457 100644 --- a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo/Annotation.php +++ b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo/Annotation.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.GeneratedCodeInfo.Annotation */ -class Annotation extends \Google\Protobuf\Internal\Message +final class Annotation extends \Google\Protobuf\Internal\Message { /** * Identifies the element in the original source .proto file. This field diff --git a/php/src/Google/Protobuf/Internal/MapField.php b/php/src/Google/Protobuf/Internal/MapField.php index 1b2b0cf0ac..2c66aa0c19 100644 --- a/php/src/Google/Protobuf/Internal/MapField.php +++ b/php/src/Google/Protobuf/Internal/MapField.php @@ -131,8 +131,8 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * * @param object $key The key of the element to be fetched. * @return object The stored element at given key. - * @throws ErrorException Invalid type for index. - * @throws ErrorException Non-existing index. + * @throws \ErrorException Invalid type for index. + * @throws \ErrorException Non-existing index. */ public function offsetGet($key) { @@ -147,9 +147,9 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * @param object $key The key of the element to be fetched. * @param object $value The element to be assigned. * @return void - * @throws ErrorException Invalid type for key. - * @throws ErrorException Invalid type for value. - * @throws ErrorException Non-existing key. + * @throws \ErrorException Invalid type for key. + * @throws \ErrorException Invalid type for value. + * @throws \ErrorException Non-existing key. */ public function offsetSet($key, $value) { @@ -207,7 +207,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * * @param object $key The key of the element to be removed. * @return void - * @throws ErrorException Invalid type for key. + * @throws \ErrorException Invalid type for key. */ public function offsetUnset($key) { @@ -222,7 +222,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable * * @param object $key The key of the element to be removed. * @return bool True if the element at the given key exists. - * @throws ErrorException Invalid type for key. + * @throws \ErrorException Invalid type for key. */ public function offsetExists($key) { diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index 8ff141ca43..1ff2dc9a67 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php @@ -716,7 +716,7 @@ class Message * * @param string $data Binary protobuf data. * @return null. - * @throws Exception Invalid data. + * @throws \Exception Invalid data. */ public function mergeFromString($data) { @@ -734,7 +734,7 @@ class Message * * @param string $data Json protobuf data. * @return null. - * @throws Exception Invalid data. + * @throws \Exception Invalid data. */ public function mergeFromJsonString($data) { @@ -832,6 +832,9 @@ class Message if (is_null($value)) { return $this->defaultValue($field); } + if (is_numeric($value)) { + return strval($value); + } if (!is_string($value)) { throw new GPBDecodeException( "String field only accepts string value"); @@ -973,9 +976,12 @@ class Message * ]); * ``` * + * This method will trigger an error if it is passed data that cannot + * be converted to the correct type. For example, a StringValue field + * must receive data that is either a string or a StringValue object. + * * @param array $array An array containing message properties and values. * @return null. - * @throws Exception Invalid data. */ protected function mergeFromArray(array $array) { @@ -987,10 +993,85 @@ class Message 'Invalid message property: ' . $key); } $setter = $field->getSetter(); + if ($field->isMap()) { + $valueField = $field->getMessageType()->getFieldByName('value'); + if (!is_null($valueField) && $valueField->isWrapperType()) { + self::normalizeArrayElementsToMessageType($value, $valueField->getMessageType()->getClass()); + } + } elseif ($field->isWrapperType()) { + $class = $field->getMessageType()->getClass(); + if ($field->isRepeated()) { + self::normalizeArrayElementsToMessageType($value, $class); + } else { + self::normalizeToMessageType($value, $class); + } + } $this->$setter($value); } } + /** + * Tries to normalize the elements in $value into a provided protobuf + * wrapper type $class. If $value is any type other than array, we do + * not do any conversion, and instead rely on the existing protobuf + * type checking. If $value is an array, we process each element and + * try to convert it to an instance of $class. + * + * @param mixed $value The array of values to normalize. + * @param string $class The expected wrapper class name + */ + private static function normalizeArrayElementsToMessageType(&$value, $class) + { + if (!is_array($value)) { + // In the case that $value is not an array, we do not want to + // attempt any conversion. Note that this includes the cases + // when $value is a RepeatedField of MapField. In those cases, + // we do not need to convert the elements, as they should + // already be the correct types. + return; + } else { + // Normalize each element in the array. + foreach ($value as $key => &$elementValue) { + self::normalizeToMessageType($elementValue, $class); + } + } + } + + /** + * Tries to normalize $value into a provided protobuf wrapper type $class. + * If $value is any type other than an object, we attempt to construct an + * instance of $class and assign $value to it using the setValue method + * shared by all wrapper types. + * + * This method will raise an error if it receives a type that cannot be + * assigned to the wrapper type via setValue. + * + * @param mixed $value The value to normalize. + * @param string $class The expected wrapper class name + */ + private static function normalizeToMessageType(&$value, $class) + { + if (is_null($value) || is_object($value)) { + // This handles the case that $value is an instance of $class. We + // choose not to do any more strict checking here, relying on the + // existing type checking done by GPBUtil. + return; + } else { + // Try to instantiate $class and set the value + try { + $msg = new $class; + $msg->setValue($value); + $value = $msg; + return; + } catch (\Exception $exception) { + trigger_error( + "Error normalizing value to type '$class': " . $exception->getMessage(), + E_USER_ERROR + ); + } + } + } + protected function mergeFromJsonArray($array) { if (is_a($this, "Google\Protobuf\Any")) { @@ -1157,9 +1238,17 @@ class Message public function parseFromJsonStream($input) { $array = json_decode($input->getData(), true, 512, JSON_BIGINT_AS_STRING); + if ($this instanceof \Google\Protobuf\ListValue) { + $array = ["values"=>$array]; + } if (is_null($array)) { - throw new GPBDecodeException( - "Cannot decode json string."); + if ($this instanceof \Google\Protobuf\Value) { + $this->setNullValue(\Google\Protobuf\NullValue::NULL_VALUE); + return; + } else { + throw new GPBDecodeException( + "Cannot decode json string: " . $input->getData()); + } } try { $this->mergeFromJsonArray($array); @@ -1338,6 +1427,24 @@ class Message $timestamp = GPBUtil::formatTimestamp($this); $timestamp = json_encode($timestamp); $output->writeRaw($timestamp, strlen($timestamp)); + } elseif (get_class($this) === 'Google\Protobuf\ListValue') { + $field = $this->desc->getField()[1]; + if (!$this->existField($field)) { + $output->writeRaw("[]", 2); + } else { + if (!$this->serializeFieldToJsonStream($output, $field)) { + return false; + } + } + } elseif (get_class($this) === 'Google\Protobuf\Struct') { + $field = $this->desc->getField()[1]; + if (!$this->existField($field)) { + $output->writeRaw("{}", 2); + } else { + if (!$this->serializeFieldToJsonStream($output, $field)) { + return false; + } + } } else { if (!GPBUtil::hasSpecialJsonMapping($this)) { $output->writeRaw("{", 1); @@ -1799,6 +1906,24 @@ class Message $timestamp = GPBUtil::formatTimestamp($this); $timestamp = json_encode($timestamp); $size += strlen($timestamp); + } elseif (get_class($this) === 'Google\Protobuf\ListValue') { + $field = $this->desc->getField()[1]; + if ($this->existField($field)) { + $field_size = $this->fieldJsonByteSize($field); + $size += $field_size; + } else { + // Size for "[]". + $size += 2; + } + } elseif (get_class($this) === 'Google\Protobuf\Struct') { + $field = $this->desc->getField()[1]; + if ($this->existField($field)) { + $field_size = $this->fieldJsonByteSize($field); + $size += $field_size; + } else { + // Size for "{}". + $size += 2; + } } else { if (!GPBUtil::hasSpecialJsonMapping($this)) { // Size for "{}". diff --git a/php/src/Google/Protobuf/Internal/MessageOptions.php b/php/src/Google/Protobuf/Internal/MessageOptions.php index bf490de686..9032c97e9b 100644 --- a/php/src/Google/Protobuf/Internal/MessageOptions.php +++ b/php/src/Google/Protobuf/Internal/MessageOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.MessageOptions */ -class MessageOptions extends \Google\Protobuf\Internal\Message +final class MessageOptions extends \Google\Protobuf\Internal\Message { /** * Set true to use the old proto1 MessageSet wire format for extensions. @@ -69,7 +69,7 @@ class MessageOptions extends \Google\Protobuf\Internal\Message * repeated MapFieldEntry map_field = 1; * Implementations may choose not to generate the map_entry=true message, but * use a native map in the target language to hold the keys and values. - * The reflection APIs in such implementions still need to work as + * The reflection APIs in such implementations still need to work as * if the field is a repeated message field. * NOTE: Do not set the option in .proto files. Always use the maps syntax * instead. The option should only be implicitly set by the proto compiler @@ -132,7 +132,7 @@ class MessageOptions extends \Google\Protobuf\Internal\Message * repeated MapFieldEntry map_field = 1; * Implementations may choose not to generate the map_entry=true message, but * use a native map in the target language to hold the keys and values. - * The reflection APIs in such implementions still need to work as + * The reflection APIs in such implementations still need to work as * if the field is a repeated message field. * NOTE: Do not set the option in .proto files. Always use the maps syntax * instead. The option should only be implicitly set by the proto compiler @@ -294,7 +294,7 @@ class MessageOptions extends \Google\Protobuf\Internal\Message * repeated MapFieldEntry map_field = 1; * Implementations may choose not to generate the map_entry=true message, but * use a native map in the target language to hold the keys and values. - * The reflection APIs in such implementions still need to work as + * The reflection APIs in such implementations still need to work as * if the field is a repeated message field. * NOTE: Do not set the option in .proto files. Always use the maps syntax * instead. The option should only be implicitly set by the proto compiler @@ -322,7 +322,7 @@ class MessageOptions extends \Google\Protobuf\Internal\Message * repeated MapFieldEntry map_field = 1; * Implementations may choose not to generate the map_entry=true message, but * use a native map in the target language to hold the keys and values. - * The reflection APIs in such implementions still need to work as + * The reflection APIs in such implementations still need to work as * if the field is a repeated message field. * NOTE: Do not set the option in .proto files. Always use the maps syntax * instead. The option should only be implicitly set by the proto compiler diff --git a/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php b/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php index 1bd5dd3e1c..25a2c166fd 100644 --- a/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php @@ -15,7 +15,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.MethodDescriptorProto */ -class MethodDescriptorProto extends \Google\Protobuf\Internal\Message +final class MethodDescriptorProto extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field optional string name = 1; diff --git a/php/src/Google/Protobuf/Internal/MethodOptions.php b/php/src/Google/Protobuf/Internal/MethodOptions.php index a2c729a9bb..a9c093ad5d 100644 --- a/php/src/Google/Protobuf/Internal/MethodOptions.php +++ b/php/src/Google/Protobuf/Internal/MethodOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.MethodOptions */ -class MethodOptions extends \Google\Protobuf\Internal\Message +final class MethodOptions extends \Google\Protobuf\Internal\Message { /** * Is this method deprecated? diff --git a/php/src/Google/Protobuf/Internal/MethodOptions/IdempotencyLevel.php b/php/src/Google/Protobuf/Internal/MethodOptions/IdempotencyLevel.php index dcc30e271c..ce3c062c69 100644 --- a/php/src/Google/Protobuf/Internal/MethodOptions/IdempotencyLevel.php +++ b/php/src/Google/Protobuf/Internal/MethodOptions/IdempotencyLevel.php @@ -4,6 +4,8 @@ namespace Google\Protobuf\Internal\MethodOptions; +use UnexpectedValueException; + /** * Is this method side-effect-free (or safe in HTTP parlance), or idempotent, * or neither? HTTP based RPC implementation may choose GET verb for safe @@ -29,6 +31,32 @@ class IdempotencyLevel * Generated from protobuf enum IDEMPOTENT = 2; */ const IDEMPOTENT = 2; + + private static $valueToName = [ + self::IDEMPOTENCY_UNKNOWN => 'IDEMPOTENCY_UNKNOWN', + self::NO_SIDE_EFFECTS => 'NO_SIDE_EFFECTS', + self::IDEMPOTENT => 'IDEMPOTENT', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } // Adding a class alias for backwards compatibility with the previous class name. diff --git a/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php b/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php index 9ecfe5cbfe..a703fcb702 100644 --- a/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php @@ -15,7 +15,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.OneofDescriptorProto */ -class OneofDescriptorProto extends \Google\Protobuf\Internal\Message +final class OneofDescriptorProto extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field optional string name = 1; diff --git a/php/src/Google/Protobuf/Internal/OneofOptions.php b/php/src/Google/Protobuf/Internal/OneofOptions.php index 46b516f301..749051f31f 100644 --- a/php/src/Google/Protobuf/Internal/OneofOptions.php +++ b/php/src/Google/Protobuf/Internal/OneofOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.OneofOptions */ -class OneofOptions extends \Google\Protobuf\Internal\Message +final class OneofOptions extends \Google\Protobuf\Internal\Message { /** * The parser stores options it doesn't recognize here. See above. diff --git a/php/src/Google/Protobuf/Internal/RepeatedField.php b/php/src/Google/Protobuf/Internal/RepeatedField.php index e9b92d8d29..ec7a3a6d07 100644 --- a/php/src/Google/Protobuf/Internal/RepeatedField.php +++ b/php/src/Google/Protobuf/Internal/RepeatedField.php @@ -118,8 +118,8 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * * @param long $offset The index of the element to be fetched. * @return object The stored element at given index. - * @throws ErrorException Invalid type for index. - * @throws ErrorException Non-existing index. + * @throws \ErrorException Invalid type for index. + * @throws \ErrorException Non-existing index. */ public function offsetGet($offset) { @@ -134,9 +134,9 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * @param long $offset The index of the element to be assigned. * @param object $value The element to be assigned. * @return void - * @throws ErrorException Invalid type for index. - * @throws ErrorException Non-existing index. - * @throws ErrorException Incorrect type of the element. + * @throws \ErrorException Invalid type for index. + * @throws \ErrorException Non-existing index. + * @throws \ErrorException Incorrect type of the element. */ public function offsetSet($offset, $value) { @@ -206,8 +206,8 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * * @param long $offset The index of the element to be removed. * @return void - * @throws ErrorException Invalid type for index. - * @throws ErrorException The element to be removed is not at the end of the + * @throws \ErrorException Invalid type for index. + * @throws \ErrorException The element to be removed is not at the end of the * RepeatedField. */ public function offsetUnset($offset) @@ -229,7 +229,7 @@ class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable * * @param long $offset The index of the element to be removed. * @return bool True if the element at the given offset exists. - * @throws ErrorException Invalid type for index. + * @throws \ErrorException Invalid type for index. */ public function offsetExists($offset) { diff --git a/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php b/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php index 8de7afd0bc..9534f0480e 100644 --- a/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php @@ -15,7 +15,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.ServiceDescriptorProto */ -class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message +final class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field optional string name = 1; diff --git a/php/src/Google/Protobuf/Internal/ServiceOptions.php b/php/src/Google/Protobuf/Internal/ServiceOptions.php index 67162f3764..55bf7a7758 100644 --- a/php/src/Google/Protobuf/Internal/ServiceOptions.php +++ b/php/src/Google/Protobuf/Internal/ServiceOptions.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.ServiceOptions */ -class ServiceOptions extends \Google\Protobuf\Internal\Message +final class ServiceOptions extends \Google\Protobuf\Internal\Message { /** * Is this service deprecated? diff --git a/php/src/Google/Protobuf/Internal/SourceCodeInfo.php b/php/src/Google/Protobuf/Internal/SourceCodeInfo.php index 6e413f77a2..3ddd892262 100644 --- a/php/src/Google/Protobuf/Internal/SourceCodeInfo.php +++ b/php/src/Google/Protobuf/Internal/SourceCodeInfo.php @@ -16,7 +16,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.SourceCodeInfo */ -class SourceCodeInfo extends \Google\Protobuf\Internal\Message +final class SourceCodeInfo extends \Google\Protobuf\Internal\Message { /** * A Location identifies a piece of source code in a .proto file which @@ -54,7 +54,7 @@ class SourceCodeInfo extends \Google\Protobuf\Internal\Message * beginning of the "extend" block and is shared by all extensions within * the block. * - Just because a location's span is a subset of some other location's span - * does not mean that it is a descendent. For example, a "group" defines + * does not mean that it is a descendant. For example, a "group" defines * both a type and a field in a single declaration. Thus, the locations * corresponding to the type and field and their components will overlap. * - Code which tries to interpret locations should probably be designed to @@ -108,7 +108,7 @@ class SourceCodeInfo extends \Google\Protobuf\Internal\Message * beginning of the "extend" block and is shared by all extensions within * the block. * - Just because a location's span is a subset of some other location's span - * does not mean that it is a descendent. For example, a "group" defines + * does not mean that it is a descendant. For example, a "group" defines * both a type and a field in a single declaration. Thus, the locations * corresponding to the type and field and their components will overlap. * - Code which tries to interpret locations should probably be designed to @@ -157,7 +157,7 @@ class SourceCodeInfo extends \Google\Protobuf\Internal\Message * beginning of the "extend" block and is shared by all extensions within * the block. * - Just because a location's span is a subset of some other location's span - * does not mean that it is a descendent. For example, a "group" defines + * does not mean that it is a descendant. For example, a "group" defines * both a type and a field in a single declaration. Thus, the locations * corresponding to the type and field and their components will overlap. * - Code which tries to interpret locations should probably be designed to @@ -208,7 +208,7 @@ class SourceCodeInfo extends \Google\Protobuf\Internal\Message * beginning of the "extend" block and is shared by all extensions within * the block. * - Just because a location's span is a subset of some other location's span - * does not mean that it is a descendent. For example, a "group" defines + * does not mean that it is a descendant. For example, a "group" defines * both a type and a field in a single declaration. Thus, the locations * corresponding to the type and field and their components will overlap. * - Code which tries to interpret locations should probably be designed to diff --git a/php/src/Google/Protobuf/Internal/SourceCodeInfo/Location.php b/php/src/Google/Protobuf/Internal/SourceCodeInfo/Location.php index bad247a11f..470d64c6e2 100644 --- a/php/src/Google/Protobuf/Internal/SourceCodeInfo/Location.php +++ b/php/src/Google/Protobuf/Internal/SourceCodeInfo/Location.php @@ -13,7 +13,7 @@ use Google\Protobuf\Internal\GPBUtil; /** * Generated from protobuf message google.protobuf.SourceCodeInfo.Location */ -class Location extends \Google\Protobuf\Internal\Message +final class Location extends \Google\Protobuf\Internal\Message { /** * Identifies which part of the FileDescriptorProto was defined at this diff --git a/php/src/Google/Protobuf/Internal/UninterpretedOption.php b/php/src/Google/Protobuf/Internal/UninterpretedOption.php index 3b517ec552..39273d62fa 100644 --- a/php/src/Google/Protobuf/Internal/UninterpretedOption.php +++ b/php/src/Google/Protobuf/Internal/UninterpretedOption.php @@ -20,7 +20,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.UninterpretedOption */ -class UninterpretedOption extends \Google\Protobuf\Internal\Message +final class UninterpretedOption extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field repeated .google.protobuf.UninterpretedOption.NamePart name = 2; diff --git a/php/src/Google/Protobuf/Internal/UninterpretedOption/NamePart.php b/php/src/Google/Protobuf/Internal/UninterpretedOption/NamePart.php index 92ee4b44b9..a2f9250f93 100644 --- a/php/src/Google/Protobuf/Internal/UninterpretedOption/NamePart.php +++ b/php/src/Google/Protobuf/Internal/UninterpretedOption/NamePart.php @@ -19,7 +19,7 @@ use Google\Protobuf\Internal\GPBUtil; * * Generated from protobuf message google.protobuf.UninterpretedOption.NamePart */ -class NamePart extends \Google\Protobuf\Internal\Message +final class NamePart extends \Google\Protobuf\Internal\Message { /** * Generated from protobuf field required string name_part = 1; diff --git a/php/src/Google/Protobuf/NullValue.php b/php/src/Google/Protobuf/NullValue.php index 482b80dde5..a72cbb2edb 100644 --- a/php/src/Google/Protobuf/NullValue.php +++ b/php/src/Google/Protobuf/NullValue.php @@ -4,6 +4,8 @@ namespace Google\Protobuf; +use UnexpectedValueException; + /** * `NullValue` is a singleton enumeration to represent the null value for the * `Value` type union. @@ -19,5 +21,28 @@ class NullValue * Generated from protobuf enum NULL_VALUE = 0; */ const NULL_VALUE = 0; + + private static $valueToName = [ + self::NULL_VALUE => 'NULL_VALUE', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } diff --git a/php/src/Google/Protobuf/Syntax.php b/php/src/Google/Protobuf/Syntax.php index 3a52dc9e1c..9812669dac 100644 --- a/php/src/Google/Protobuf/Syntax.php +++ b/php/src/Google/Protobuf/Syntax.php @@ -4,6 +4,8 @@ namespace Google\Protobuf; +use UnexpectedValueException; + /** * The syntax in which a protocol buffer element is defined. * @@ -23,5 +25,29 @@ class Syntax * Generated from protobuf enum SYNTAX_PROTO3 = 1; */ const SYNTAX_PROTO3 = 1; + + private static $valueToName = [ + self::SYNTAX_PROTO2 => 'SYNTAX_PROTO2', + self::SYNTAX_PROTO3 => 'SYNTAX_PROTO3', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } } diff --git a/php/tests/array_test.php b/php/tests/array_test.php index 36a649ed19..b251404083 100644 --- a/php/tests/array_test.php +++ b/php/tests/array_test.php @@ -7,7 +7,7 @@ use Google\Protobuf\Internal\GPBType; use Foo\TestMessage; use Foo\TestMessage\Sub; -class RepeatedFieldTest extends PHPUnit_Framework_TestCase +class RepeatedFieldTest extends \PHPUnit\Framework\TestCase { ######################################################### @@ -529,6 +529,45 @@ class RepeatedFieldTest extends PHPUnit_Framework_TestCase $this->assertSame(3, $arr[2]); } + ######################################################### + # Test reference in array + ######################################################### + + public function testArrayElementIsReferenceInSetters() + { + // Bool elements + $values = [true]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setRepeatedBool($values); + + // Int32 elements + $values = [1]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setRepeatedInt32($values); + + // Double elements + $values = [1.0]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setRepeatedDouble($values); + + // String elements + $values = ['a']; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setRepeatedString($values); + + // Message elements + $m = new TestMessage(); + $subs = [1, 2]; + foreach ($subs as &$sub) { + $sub = new Sub(['a' => $sub]); + } + $m->setRepeatedMessage($subs); + } + ######################################################### # Test memory leak ######################################################### diff --git a/php/tests/compatibility_test.sh b/php/tests/compatibility_test.sh index c4d6325de2..f4af52471b 100755 --- a/php/tests/compatibility_test.sh +++ b/php/tests/compatibility_test.sh @@ -92,7 +92,7 @@ echo "Running compatibility tests between $VERSION_NUMBER and $OLD_VERSION" # Download old test. rm -rf protobuf -git clone https://github.com/google/protobuf.git +git clone https://github.com/protocolbuffers/protobuf.git pushd protobuf git checkout v$OLD_VERSION popd @@ -123,8 +123,12 @@ tests=( array_test.php encode_decode_test.php generated_class_test.php map_field sed -i.bak '/php_implementation_test.php/d' phpunit.xml sed -i.bak '/generated_phpdoc_test.php/d' phpunit.xml sed -i.bak 's/generated_phpdoc_test.php//g' tests/test.sh +sed -i.bak 's/generated_service_test.php//g' tests/test.sh sed -i.bak '/memory_leak_test.php/d' tests/test.sh sed -i.bak '/^ public function testTimestamp()$/,/^ }$/d' tests/well_known_test.php +sed -i.bak 's/PHPUnit_Framework_TestCase/\\PHPUnit\\Framework\\TestCase/g' tests/array_test.php +sed -i.bak 's/PHPUnit_Framework_TestCase/\\PHPUnit\\Framework\\TestCase/g' tests/map_field_test.php +sed -i.bak 's/PHPUnit_Framework_TestCase/\\PHPUnit\\Framework\\TestCase/g' tests/test_base.php for t in "${tests[@]}" do remove_error_test tests/$t diff --git a/php/tests/compile_extension.sh b/php/tests/compile_extension.sh new file mode 100755 index 0000000000..bbd669684c --- /dev/null +++ b/php/tests/compile_extension.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +EXTENSION_PATH=$1 + +pushd $EXTENSION_PATH +make clean || true +set -e +# Add following in configure for debug: --enable-debug CFLAGS='-g -O0' +phpize && ./configure CFLAGS='-g -O0' && make +popd diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index 870dacab52..5b373bb07e 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -5,13 +5,16 @@ require_once('test_util.php'); use Google\Protobuf\RepeatedField; use Google\Protobuf\GPBType; +use Foo\TestAny; use Foo\TestEnum; use Foo\TestMessage; use Foo\TestMessage\Sub; use Foo\TestPackedMessage; use Foo\TestRandomFieldOrder; use Foo\TestUnpackedMessage; +use Google\Protobuf\Any; use Google\Protobuf\DoubleValue; +use Google\Protobuf\FieldMask; use Google\Protobuf\FloatValue; use Google\Protobuf\Int32Value; use Google\Protobuf\UInt32Value; @@ -20,6 +23,10 @@ use Google\Protobuf\UInt64Value; use Google\Protobuf\BoolValue; use Google\Protobuf\StringValue; use Google\Protobuf\BytesValue; +use Google\Protobuf\Value; +use Google\Protobuf\ListValue; +use Google\Protobuf\Struct; +use Google\Protobuf\GPBEmpty; class EncodeDecodeTest extends TestBase { @@ -27,6 +34,7 @@ class EncodeDecodeTest extends TestBase { $m = new TestMessage(); $m->mergeFromJsonString("{\"optionalInt32\":1}"); + $this->assertEquals(1, $m->getOptionalInt32()); } public function testDecodeTopLevelBoolValue() @@ -40,6 +48,13 @@ class EncodeDecodeTest extends TestBase $this->assertEquals(false, $m->getValue()); } + public function testEncodeTopLevelBoolValue() + { + $m = new BoolValue(); + $m->setValue(true); + $this->assertSame("true", $m->serializeToJsonString()); + } + public function testDecodeTopLevelDoubleValue() { $m = new DoubleValue(); @@ -47,6 +62,13 @@ class EncodeDecodeTest extends TestBase $this->assertEquals(1.5, $m->getValue()); } + public function testEncodeTopLevelDoubleValue() + { + $m = new DoubleValue(); + $m->setValue(1.5); + $this->assertSame("1.5", $m->serializeToJsonString()); + } + public function testDecodeTopLevelFloatValue() { $m = new FloatValue(); @@ -54,6 +76,13 @@ class EncodeDecodeTest extends TestBase $this->assertEquals(1.5, $m->getValue()); } + public function testEncodeTopLevelFloatValue() + { + $m = new FloatValue(); + $m->setValue(1.5); + $this->assertSame("1.5", $m->serializeToJsonString()); + } + public function testDecodeTopLevelInt32Value() { $m = new Int32Value(); @@ -61,6 +90,13 @@ class EncodeDecodeTest extends TestBase $this->assertEquals(1, $m->getValue()); } + public function testEncodeTopLevelInt32Value() + { + $m = new Int32Value(); + $m->setValue(1); + $this->assertSame("1", $m->serializeToJsonString()); + } + public function testDecodeTopLevelUInt32Value() { $m = new UInt32Value(); @@ -68,6 +104,13 @@ class EncodeDecodeTest extends TestBase $this->assertEquals(1, $m->getValue()); } + public function testEncodeTopLevelUInt32Value() + { + $m = new UInt32Value(); + $m->setValue(1); + $this->assertSame("1", $m->serializeToJsonString()); + } + public function testDecodeTopLevelInt64Value() { $m = new Int64Value(); @@ -75,6 +118,13 @@ class EncodeDecodeTest extends TestBase $this->assertEquals(1, $m->getValue()); } + # public function testEncodeTopLevelInt64Value() + # { + # $m = new Int64Value(); + # $m->setValue(1); + # $this->assertSame("\"1\"", $m->serializeToJsonString()); + # } + public function testDecodeTopLevelUInt64Value() { $m = new UInt64Value(); @@ -82,6 +132,13 @@ class EncodeDecodeTest extends TestBase $this->assertEquals(1, $m->getValue()); } + # public function testEncodeTopLevelUInt64Value() + # { + # $m = new UInt64Value(); + # $m->setValue(1); + # $this->assertSame("\"1\"", $m->serializeToJsonString()); + # } + public function testDecodeTopLevelStringValue() { $m = new StringValue(); @@ -89,6 +146,13 @@ class EncodeDecodeTest extends TestBase $this->assertSame("a", $m->getValue()); } + public function testEncodeTopLevelStringValue() + { + $m = new StringValue(); + $m->setValue("a"); + $this->assertSame("\"a\"", $m->serializeToJsonString()); + } + public function testDecodeTopLevelBytesValue() { $m = new BytesValue(); @@ -96,6 +160,30 @@ class EncodeDecodeTest extends TestBase $this->assertSame("a", $m->getValue()); } + public function testEncodeTopLevelBytesValue() + { + $m = new BytesValue(); + $m->setValue("a"); + $this->assertSame("\"YQ==\"", $m->serializeToJsonString()); + } + + public function generateRandomString($length = 10) { + $randomString = str_repeat("+", $length); + for ($i = 0; $i < $length; $i++) { + $randomString[$i] = rand(0, 255); + } + return $randomString; + } + + public function testEncodeTopLevelLongBytesValue() + { + $m = new BytesValue(); + $data = $this->generateRandomString(12007); + $m->setValue($data); + $expected = "\"" . base64_encode($data) . "\""; + $this->assertSame(strlen($expected), strlen($m->serializeToJsonString())); + } + public function testEncode() { $from = new TestMessage(); @@ -196,6 +284,40 @@ class EncodeDecodeTest extends TestBase } + public function testJsonEncodeDecodeOneof() + { + $m = new TestMessage(); + + $m->setOneofEnum(TestEnum::ONE); + $data = $m->serializeToJsonString(); + $n = new TestMessage(); + $n->mergeFromJsonString($data); + $this->assertSame("oneof_enum", $n->getMyOneof()); + $this->assertSame(TestEnum::ONE, $n->getOneofEnum()); + + $m->setOneofString("a"); + $data = $m->serializeToJsonString(); + $n = new TestMessage(); + $n->mergeFromJsonString($data); + $this->assertSame("oneof_string", $n->getMyOneof()); + $this->assertSame("a", $n->getOneofString()); + + $m->setOneofBytes("bbbb"); + $data = $m->serializeToJsonString(); + $n = new TestMessage(); + $n->mergeFromJsonString($data); + $this->assertSame("oneof_bytes", $n->getMyOneof()); + $this->assertSame("bbbb", $n->getOneofBytes()); + + $sub_m = new Sub(); + $m->setOneofMessage($sub_m); + $data = $m->serializeToJsonString(); + $n = new TestMessage(); + $n->mergeFromJsonString($data); + $this->assertSame("oneof_message", $n->getMyOneof()); + $this->assertFalse(is_null($n->getOneofMessage())); + } + public function testPackedEncode() { $from = new TestPackedMessage(); @@ -209,6 +331,7 @@ class EncodeDecodeTest extends TestBase $to = new TestPackedMessage(); $to->mergeFromString(TestUtil::getGoldenTestPackedMessage()); TestUtil::assertTestPackedMessage($to); + $this->assertTrue(true); } public function testPackedDecodeUnpacked() @@ -216,6 +339,7 @@ class EncodeDecodeTest extends TestBase $to = new TestPackedMessage(); $to->mergeFromString(TestUtil::getGoldenTestUnpackedMessage()); TestUtil::assertTestPackedMessage($to); + $this->assertTrue(true); } public function testUnpackedEncode() @@ -231,6 +355,7 @@ class EncodeDecodeTest extends TestBase $to = new TestUnpackedMessage(); $to->mergeFromString(TestUtil::getGoldenTestPackedMessage()); TestUtil::assertTestPackedMessage($to); + $this->assertTrue(true); } public function testUnpackedDecodeUnpacked() @@ -238,6 +363,7 @@ class EncodeDecodeTest extends TestBase $to = new TestUnpackedMessage(); $to->mergeFromString(TestUtil::getGoldenTestUnpackedMessage()); TestUtil::assertTestPackedMessage($to); + $this->assertTrue(true); } public function testDecodeInt64() @@ -294,6 +420,7 @@ class EncodeDecodeTest extends TestBase $data = hex2bin('c80501'); $m = new TestMessage(); $m->mergeFromString($data); + $this->assertTrue(true); } public function testEncodeNegativeInt32() @@ -594,6 +721,129 @@ class EncodeDecodeTest extends TestBase $this->assertSame("9a0400", bin2hex($to)); } + public function testJsonUnknown() + { + // Test unknown number + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":1, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown bool + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":true, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown string + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":\"abc\", + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown null + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":null, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown array + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":[], + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown number array + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":[1], + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown bool array + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":[true], + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown string array + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":[\"a\"], + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown null array + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":[null], + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown array array + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":[[]], + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown object array + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":[{}], + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown double value array + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":[1, 2], + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown object + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":{}, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown number object + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":{\"a\":1}, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown bool object + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":{\"a\":true}, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown string object + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":{\"a\":\"a\"}, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown null object + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":{\"a\":null}, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown array object + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":{\"a\":[]}, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown object object + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":{\"a\":{}}, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + + // Test unknown double value object + $m = new TestMessage(); + $m->mergeFromJsonString("{\"unknown\":{\"a\":1, \"b\":1}, + \"optionalInt32\":1}", true); + $this->assertSame(1, $m->getOptionalInt32()); + } + public function testJsonEncode() { $from = new TestMessage(); @@ -603,4 +853,326 @@ class EncodeDecodeTest extends TestBase $to->mergeFromJsonString($data); $this->expectFields($to); } + + public function testDecodeDuration() + { + $m = new Google\Protobuf\Duration(); + $m->mergeFromJsonString("\"1234.5678s\""); + $this->assertEquals(1234, $m->getSeconds()); + $this->assertEquals(567800000, $m->getNanos()); + } + + public function testEncodeDuration() + { + $m = new Google\Protobuf\Duration(); + $m->setSeconds(1234); + $m->setNanos(999999999); + $this->assertEquals("\"1234.999999999s\"", $m->serializeToJsonString()); + } + + public function testDecodeTimestamp() + { + $m = new Google\Protobuf\Timestamp(); + $m->mergeFromJsonString("\"2000-01-01T00:00:00.123456789Z\""); + $this->assertEquals(946684800, $m->getSeconds()); + $this->assertEquals(123456789, $m->getNanos()); + } + + public function testEncodeTimestamp() + { + $m = new Google\Protobuf\Timestamp(); + $m->setSeconds(946684800); + $m->setNanos(123456789); + $this->assertEquals("\"2000-01-01T00:00:00.123456789Z\"", + $m->serializeToJsonString()); + } + + public function testDecodeTopLevelValue() + { + $m = new Value(); + $m->mergeFromJsonString("\"a\""); + $this->assertSame("a", $m->getStringValue()); + + $m = new Value(); + $m->mergeFromJsonString("1.5"); + $this->assertSame(1.5, $m->getNumberValue()); + + $m = new Value(); + $m->mergeFromJsonString("true"); + $this->assertSame(true, $m->getBoolValue()); + + $m = new Value(); + $m->mergeFromJsonString("null"); + $this->assertSame("null_value", $m->getKind()); + + $m = new Value(); + $m->mergeFromJsonString("[1]"); + $this->assertSame("list_value", $m->getKind()); + + $m = new Value(); + $m->mergeFromJsonString("{\"a\":1}"); + $this->assertSame("struct_value", $m->getKind()); + } + + public function testEncodeTopLevelValue() + { + $m = new Value(); + $m->setStringValue("a"); + $this->assertSame("\"a\"", $m->serializeToJsonString()); + + $m = new Value(); + $m->setNumberValue(1.5); + $this->assertSame("1.5", $m->serializeToJsonString()); + + $m = new Value(); + $m->setBoolValue(true); + $this->assertSame("true", $m->serializeToJsonString()); + + $m = new Value(); + $m->setNullValue(0); + $this->assertSame("null", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelListValue() + { + $m = new ListValue(); + $m->mergeFromJsonString("[1]"); + $this->assertSame(1.0, $m->getValues()[0]->getNumberValue()); + } + + public function testEncodeTopLevelListValue() + { + $m = new ListValue(); + $arr = $m->getValues(); + $sub = new Value(); + $sub->setNumberValue(1.5); + $arr[] = $sub; + $this->assertSame("[1.5]", $m->serializeToJsonString()); + } + + public function testEncodeEmptyListValue() + { + $m = new Struct(); + $m->setFields(['test' => (new Value())->setListValue(new ListValue())]); + $this->assertSame('{"test":[]}', $m->serializeToJsonString()); + } + + public function testDecodeTopLevelStruct() + { + $m = new Struct(); + $m->mergeFromJsonString("{\"a\":{\"b\":1}}"); + $this->assertSame(1.0, $m->getFields()["a"] + ->getStructValue() + ->getFields()["b"]->getNumberValue()); + } + + public function testEncodeTopLevelStruct() + { + $m = new Struct(); + $map = $m->getFields(); + $sub = new Value(); + $sub->setNumberValue(1.5); + $map["a"] = $sub; + $this->assertSame("{\"a\":1.5}", $m->serializeToJsonString()); + } + + public function testEncodeEmptyStruct() + { + $m = new Struct(); + $m->setFields(['test' => (new Value())->setStructValue(new Struct())]); + $this->assertSame('{"test":{}}', $m->serializeToJsonString()); + } + + public function testDecodeTopLevelAny() + { + // Make sure packed message has been created at least once. + $packed = new TestMessage(); + + $m1 = new Any(); + $m1->mergeFromJsonString( + "{\"optionalInt32\": 1, " . + "\"@type\":\"type.googleapis.com/foo.TestMessage\"}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m1->getTypeUrl()); + $this->assertSame("0801", bin2hex($m1->getValue())); + + $m2 = new Any(); + $m2->mergeFromJsonString( + "{\"@type\":\"type.googleapis.com/foo.TestMessage\", " . + "\"optionalInt32\": 1}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m2->getTypeUrl()); + $this->assertSame("0801", bin2hex($m2->getValue())); + + $m3 = new Any(); + $m3->mergeFromJsonString( + "{\"optionalInt32\": 1, " . + "\"@type\":\"type.googleapis.com/foo.TestMessage\", " . + "\"optionalInt64\": 2}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m3->getTypeUrl()); + $this->assertSame("08011002", bin2hex($m3->getValue())); + } + + public function testDecodeAny() + { + // Make sure packed message has been created at least once. + $packed = new TestMessage(); + + $m1 = new TestAny(); + $m1->mergeFromJsonString( + "{\"any\": {\"optionalInt32\": 1, " . + "\"@type\":\"type.googleapis.com/foo.TestMessage\"}}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m1->getAny()->getTypeUrl()); + $this->assertSame("0801", bin2hex($m1->getAny()->getValue())); + + $m2 = new TestAny(); + $m2->mergeFromJsonString( + "{\"any\":{\"@type\":\"type.googleapis.com/foo.TestMessage\", " . + "\"optionalInt32\": 1}}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m2->getAny()->getTypeUrl()); + $this->assertSame("0801", bin2hex($m2->getAny()->getValue())); + + $m3 = new TestAny(); + $m3->mergeFromJsonString( + "{\"any\":{\"optionalInt32\": 1, " . + "\"@type\":\"type.googleapis.com/foo.TestMessage\", " . + "\"optionalInt64\": 2}}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m3->getAny()->getTypeUrl()); + $this->assertSame("08011002", bin2hex($m3->getAny()->getValue())); + } + + public function testDecodeAnyWithWellKnownPacked() + { + // Make sure packed message has been created at least once. + $packed = new Int32Value(); + + $m1 = new TestAny(); + $m1->mergeFromJsonString( + "{\"any\":" . + " {\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . + " \"value\":1}}"); + $this->assertSame("type.googleapis.com/google.protobuf.Int32Value", + $m1->getAny()->getTypeUrl()); + $this->assertSame("0801", bin2hex($m1->getAny()->getValue())); + } + + /** + * @expectedException Exception + */ + public function testDecodeAnyWithUnknownPacked() + { + $m = new TestAny(); + $m->mergeFromJsonString( + "{\"any\":" . + " {\"@type\":\"type.googleapis.com/unknown\"," . + " \"value\":1}}"); + } + + public function testEncodeTopLevelAny() + { + // Test a normal message. + $packed = new TestMessage(); + $packed->setOptionalInt32(123); + $packed->setOptionalString("abc"); + + $m = new Any(); + $m->pack($packed); + $expected1 = + "{\"@type\":\"type.googleapis.com/foo.TestMessage\"," . + "\"optional_int32\":123,\"optional_string\":\"abc\"}"; + $expected2 = + "{\"@type\":\"type.googleapis.com/foo.TestMessage\"," . + "\"optionalInt32\":123,\"optionalString\":\"abc\"}"; + $result = $m->serializeToJsonString(); + $this->assertTrue($expected1 === $result || $expected2 === $result); + + // Test a well known message. + $packed = new Int32Value(); + $packed->setValue(123); + + $m = new Any(); + $m->pack($packed); + $this->assertSame( + "{\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . + "\"value\":123}", + $m->serializeToJsonString()); + + // Test an Any message. + $outer = new Any(); + $outer->pack($m); + $this->assertSame( + "{\"@type\":\"type.googleapis.com/google.protobuf.Any\"," . + "\"value\":{\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . + "\"value\":123}}", + $outer->serializeToJsonString()); + + // Test a Timestamp message. + $packed = new Google\Protobuf\Timestamp(); + $packed->setSeconds(946684800); + $packed->setNanos(123456789); + $m = new Any(); + $m->pack($packed); + $this->assertSame( + "{\"@type\":\"type.googleapis.com/google.protobuf.Timestamp\"," . + "\"value\":\"2000-01-01T00:00:00.123456789Z\"}", + $m->serializeToJsonString()); + } + + public function testDecodeTopLevelFieldMask() + { + $m = new TestMessage(); + $m->setMapStringString(['a'=>'abcdefg']); + $data1 = $m->serializeToJsonString(); + $n = new TestMessage(); + $n->mergeFromJsonString($data1); + $data2 = $n->serializeToJsonString(); + $this->assertSame($data1, $data2); + + $m = new FieldMask(); + $m->mergeFromJsonString("\"foo.barBaz,qux\""); + $this->assertSame("foo.bar_baz", $m->getPaths()[0]); + $this->assertSame("qux", $m->getPaths()[1]); + } + + public function testEncodeTopLevelFieldMask() + { + $m = new FieldMask(); + $m->setPaths(["foo.bar_baz", "qux"]); + $this->assertSame("\"foo.barBaz,qux\"", $m->serializeToJsonString()); + } + + public function testDecodeEmptyFieldMask() + { + $m = new FieldMask(); + $m->mergeFromJsonString("\"\""); + $this->assertEquals("", $m->serializeToString()); + } + + public function testJsonDecodeMapWithDefaultValueKey() + { + $m = new TestMessage(); + $m->getMapInt32Int32()[0] = 0; + $this->assertSame("{\"mapInt32Int32\":{\"0\":0}}", + $m->serializeToJsonString()); + + $m = new TestMessage(); + $m->getMapStringString()[""] = ""; + $this->assertSame("{\"mapStringString\":{\"\":\"\"}}", + $m->serializeToJsonString()); + } + + public function testJsonDecodeNumericStringMapKey() + { + $m = new TestMessage(); + $m->getMapStringString()["1"] = "1"; + $data = $m->serializeToJsonString(); + $this->assertSame("{\"mapStringString\":{\"1\":\"1\"}}", $data); + $n = new TestMessage(); + $n->mergeFromJsonString($data); + } + } diff --git a/php/tests/gdb_test.sh b/php/tests/gdb_test.sh index 36fa31bbb2..da5f3f3ac1 100755 --- a/php/tests/gdb_test.sh +++ b/php/tests/gdb_test.sh @@ -11,7 +11,8 @@ php -i | grep "Configuration" # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which # phpunit` --bootstrap autoload.php tmp_test.php # -gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php generated_class_test.php +# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php generated_class_test.php +gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php # # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php # diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php index 8bac4e5eb0..a9ad793be3 100644 --- a/php/tests/generated_class_test.php +++ b/php/tests/generated_class_test.php @@ -232,18 +232,42 @@ class GeneratedClassTest extends TestBase // Set string. $m->setOptionalEnum("1"); $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum()); + + // Test Enum methods + $this->assertEquals('ONE', TestEnum::name(1)); + $this->assertEquals(1, TestEnum::value('ONE')); + } + + /** + * @expectedException UnexpectedValueException + * @expectedExceptionMessage Enum Foo\TestEnum has no name defined for value -1 + */ + public function testInvalidEnumValueThrowsException() + { + TestEnum::name(-1); + } + + /** + * @expectedException UnexpectedValueException + * @expectedExceptionMessage Enum Foo\TestEnum has no value defined for name DOES_NOT_EXIST + */ + public function testInvalidEnumNameThrowsException() + { + TestEnum::value('DOES_NOT_EXIST'); } public function testNestedEnum() { $m = new TestMessage(); $m->setOptionalNestedEnum(NestedEnum::ZERO); + $this->assertTrue(true); } public function testLegacyNestedEnum() { $m = new TestMessage(); $m->setOptionalNestedEnum(\Foo\TestMessage_NestedEnum::ZERO); + $this->assertTrue(true); } public function testLegacyTypehintWithNestedEnums() @@ -383,6 +407,7 @@ class GeneratedClassTest extends TestBase $m = new TestMessage(); $hex = hex2bin("ff"); $m->setOptionalBytes($hex); + $this->assertTrue(true); } ######################################################### @@ -687,6 +712,8 @@ class GeneratedClassTest extends TestBase // test nested messages $sub = new NoNamespaceMessage\NestedMessage(); $n->setNestedMessage($sub); + + $this->assertTrue(true); } public function testEnumWithoutNamespace() @@ -696,6 +723,7 @@ class GeneratedClassTest extends TestBase $repeatedNoNamespaceEnum = $m->getRepeatedNoNamespaceEnum(); $repeatedNoNamespaceEnum[] = NoNameSpaceEnum::VALUE_A; $m->setRepeatedNoNamespaceEnum($repeatedNoNamespaceEnum); + $this->assertTrue(true); } ######################################################### @@ -1240,6 +1268,8 @@ class GeneratedClassTest extends TestBase $m = \Upper_enum_value\NotAllowed::NULL; $m = \Upper_enum_value\NotAllowed::VOID; $m = \Upper_enum_value\NotAllowed::ITERABLE; + + $this->assertTrue(true); } ######################################################### @@ -1275,6 +1305,7 @@ class GeneratedClassTest extends TestBase { $m = new testLowerCaseMessage(); $n = testLowerCaseEnum::VALUE; + $this->assertTrue(true); } ######################################################### @@ -1298,7 +1329,7 @@ class GeneratedClassTest extends TestBase 'optional_double' => 1.6, 'optional_bool' => true, 'optional_string' => 'a', - 'optional_bytes' => 'b', + 'optional_bytes' => 'bbbb', 'optional_enum' => TestEnum::ONE, 'optional_message' => new Sub([ 'a' => 33 @@ -1317,7 +1348,7 @@ class GeneratedClassTest extends TestBase 'repeated_double' => [1.6, 2.6], 'repeated_bool' => [true, false], 'repeated_string' => ['a', 'c'], - 'repeated_bytes' => ['b', 'd'], + 'repeated_bytes' => ['bbbb', 'dddd'], 'repeated_enum' => [TestEnum::ZERO, TestEnum::ONE], 'repeated_message' => [new Sub(['a' => 34]), new Sub(['a' => 35])], @@ -1335,12 +1366,99 @@ class GeneratedClassTest extends TestBase 'map_int32_double' => [1 => 3.6], 'map_bool_bool' => [true => true], 'map_string_string' => ['e' => 'e'], - 'map_int32_bytes' => [1 => 'f'], + 'map_int32_bytes' => [1 => 'ffff'], 'map_int32_enum' => [1 => TestEnum::ONE], 'map_int32_message' => [1 => new Sub(['a' => 36])], ]); TestUtil::assertTestMessage($m); + $this->assertTrue(true); + } + + public function testReferenceInArrayConstructor() + { + $keys = [[ + 'optional_bool' => true, + 'repeated_bool' => [true], + 'map_bool_bool' => [true => true], + 'optional_double' => 1.0, + 'repeated_double' => [1.0], + 'map_int32_double' => [1 => 1.0], + 'optional_int32' => 1, + 'repeated_int32' => [1], + 'map_int32_int32' => [1 => 1], + 'optional_string' => 'a', + 'repeated_string' => ['a'], + 'map_string_string' => ['a' => 'a'], + 'optional_message' => ['a' => 1], + 'repeated_message' => [['a' => 1]], + 'map_int32_message' => [1 => ['a' => 1]], + ]]; + + foreach ($keys as &$key) { + foreach ($key as $id => &$value) { + if ($id === 'repeated_bool') { + foreach ($value as &$element) { + } + } + if ($id === 'map_bool_bool') { + foreach ($value as $mapKey => &$element) { + } + } + if ($id === 'repeated_double') { + foreach ($value as &$element) { + } + } + if ($id === 'map_int32_double') { + foreach ($value as $mapKey => &$element) { + } + } + if ($id === 'repeated_int32') { + foreach ($value as &$element) { + } + } + if ($id === 'map_int32_int32') { + foreach ($value as $mapKey => &$element) { + } + } + if ($id === 'repeated_string') { + foreach ($value as &$element) { + } + } + if ($id === 'map_string_string') { + foreach ($value as $mapKey => &$element) { + } + } + if ($id === 'optional_message') { + $value = new Sub($value); + } + if ($id === 'repeated_message') { + foreach ($value as &$element) { + $element = new Sub($element); + } + } + if ($id === 'map_int32_message') { + foreach ($value as $mapKey => &$element) { + $element = new Sub($element); + } + } + } + $key = new TestMessage($key); + } + } + + public function testOneofMessageInArrayConstructor() + { + $m = new TestMessage([ + 'oneof_message' => new Sub(), + ]); + } + + public function testOneofStringInArrayConstructor() + { + $m = new TestMessage([ + 'oneof_string' => 'abc', + ]); } ######################################################### @@ -1355,4 +1473,35 @@ class GeneratedClassTest extends TestBase TestUtil::setTestMessage($n); $this->assertEquals($m, $n); } + + ######################################################### + # Test reference of value + ######################################################### + + public function testValueIsReference() + { + // Bool element + $values = [true]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setOptionalBool($values[0]); + + // Int32 element + $values = [1]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setOptionalInt32($values[0]); + + // Double element + $values = [1.0]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setOptionalDouble($values[0]); + + // String element + $values = ['a']; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setOptionalString($values[0]); + } } diff --git a/php/tests/map_field_test.php b/php/tests/map_field_test.php index 447bdd9b36..577be681bf 100644 --- a/php/tests/map_field_test.php +++ b/php/tests/map_field_test.php @@ -7,7 +7,7 @@ use Google\Protobuf\Internal\MapField; use Foo\TestMessage; use Foo\TestMessage\Sub; -class MapFieldTest extends PHPUnit_Framework_TestCase { +class MapFieldTest extends \PHPUnit\Framework\TestCase { ######################################################### # Test int32 field. @@ -442,6 +442,43 @@ class MapFieldTest extends PHPUnit_Framework_TestCase { $this->assertSame(3, $i); } + ######################################################### + # Test reference in map + ######################################################### + + public function testMapElementIsReference() + { + // Bool elements + $values = [true => true]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setMapBoolBool($values); + + // Int32 elements + $values = [1 => 1]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setMapInt32Int32($values); + + // Double elements + $values = [1 => 1.0]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setMapInt32Double($values); + + // String elements + $values = ['a' => 'a']; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setMapStringString($values); + + // Message elements + $values = [1 => new Sub()]; + array_walk($values, function (&$value) {}); + $m = new TestMessage(); + $m->setMapInt32Message($values); + } + ######################################################### # Test memory leak ######################################################### diff --git a/php/tests/memory_leak_test.php b/php/tests/memory_leak_test.php index f3bcb963f0..142f467669 100644 --- a/php/tests/memory_leak_test.php +++ b/php/tests/memory_leak_test.php @@ -19,6 +19,7 @@ require_once('generated/Bar/TestLegacyMessage/NestedEnum.php'); require_once('generated/Bar/TestLegacyMessage/NestedMessage.php'); require_once('generated/Foo/PBARRAY.php'); require_once('generated/Foo/PBEmpty.php'); +require_once('generated/Foo/TestAny.php'); require_once('generated/Foo/TestEnum.php'); require_once('generated/Foo/TestIncludeNamespaceMessage.php'); require_once('generated/Foo/TestIncludePrefixMessage.php'); @@ -49,6 +50,7 @@ require_once('test_util.php'); use Google\Protobuf\Internal\RepeatedField; use Google\Protobuf\Internal\GPBType; +use Foo\TestAny; use Foo\TestMessage; use Foo\TestMessage\Sub; @@ -191,3 +193,16 @@ $to = new TestMessage(); TestUtil::setTestMessage($from); $to->mergeFrom($from); TestUtil::assertTestMessage($to); + +// Test decode Any +// Make sure packed message has been created at least once. +$packed = new TestMessage(); + +$m = new TestAny(); +$m->mergeFromJsonString( + "{\"any\":" . + " {\"@type\":\"type.googleapis.com/foo.TestMessage\"," . + " \"optionalInt32\":1}}"); +assert("type.googleapis.com/foo.TestMessage" === + $m->getAny()->getTypeUrl()); +assert("0801" === bin2hex($m->getAny()->getValue())); diff --git a/php/tests/php_implementation_test.php b/php/tests/php_implementation_test.php index 323a36ff09..307b749c49 100644 --- a/php/tests/php_implementation_test.php +++ b/php/tests/php_implementation_test.php @@ -14,8 +14,19 @@ use Google\Protobuf\Internal\GPBType; use Google\Protobuf\Internal\GPBWire; use Google\Protobuf\Internal\CodedOutputStream; +/** + * Please note, this test is only intended to be run without the protobuf C + * extension. + */ class ImplementationTest extends TestBase { + public function setUp() + { + if (extension_loaded('protobuf')) { + $this->markTestSkipped(); + } + } + public function testReadInt32() { $value = null; @@ -504,7 +515,7 @@ class ImplementationTest extends TestBase { $m = new TestMessage(); TestUtil::setTestMessage($m); - $this->assertSame(506, $m->byteSize()); + $this->assertSame(518, $m->byteSize()); } public function testPackedByteSize() diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto index d0109d83a5..715f08b832 100644 --- a/php/tests/proto/test.proto +++ b/php/tests/proto/test.proto @@ -1,5 +1,7 @@ syntax = "proto3"; +import 'google/protobuf/any.proto'; +import 'google/protobuf/struct.proto'; import 'proto/test_include.proto'; import 'proto/test_no_namespace.proto'; import 'proto/test_php_namespace.proto'; @@ -201,3 +203,7 @@ message testLowerCaseMessage { enum testLowerCaseEnum { VALUE = 0; } + +message TestAny { + google.protobuf.Any any = 1; +} diff --git a/php/tests/proto/test_wrapper_type_setters.proto b/php/tests/proto/test_wrapper_type_setters.proto new file mode 100644 index 0000000000..41ca7f3f31 --- /dev/null +++ b/php/tests/proto/test_wrapper_type_setters.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; + +import "google/protobuf/wrappers.proto"; + +package foo; + +message TestWrapperSetters { + google.protobuf.DoubleValue double_value = 1; + google.protobuf.FloatValue float_value = 2; + google.protobuf.Int64Value int64_value = 3; + google.protobuf.UInt64Value uint64_value = 4; + google.protobuf.Int32Value int32_value = 5; + google.protobuf.UInt32Value uint32_value = 6; + google.protobuf.BoolValue bool_value = 7; + google.protobuf.StringValue string_value = 8; + google.protobuf.BytesValue bytes_value = 9; + + oneof wrapped_oneofs { + google.protobuf.DoubleValue double_value_oneof = 10; + google.protobuf.StringValue string_value_oneof = 11; + } + + repeated google.protobuf.StringValue repeated_string_value = 12; + + map map_string_value = 13; +} diff --git a/php/tests/test.sh b/php/tests/test.sh index 700dd295cc..be6e97fe85 100755 --- a/php/tests/test.sh +++ b/php/tests/test.sh @@ -7,14 +7,9 @@ export C_INCLUDE_PATH=/usr/local/php-$VERSION/include/php/main:/usr/local/php-$V export CPLUS_INCLUDE_PATH=/usr/local/php-$VERSION/include/php/main:/usr/local/php-$VERSION/include/php:$CPLUS_INCLUDE_PATH # Compile c extension -pushd ../ext/google/protobuf/ -make clean || true -set -e -# Add following in configure for debug: --enable-debug CFLAGS='-g -O0' -phpize && ./configure CFLAGS='-g -O0' && make -popd - -tests=( array_test.php encode_decode_test.php generated_class_test.php map_field_test.php well_known_test.php descriptors_test.php ) +/bin/bash ./compile_extension.sh ../ext/google/protobuf + +tests=( array_test.php encode_decode_test.php generated_class_test.php map_field_test.php well_known_test.php descriptors_test.php wrapper_type_setters_test.php) for t in "${tests[@]}" do diff --git a/php/tests/test_base.php b/php/tests/test_base.php index 80f603c77a..a4d951bdc5 100644 --- a/php/tests/test_base.php +++ b/php/tests/test_base.php @@ -4,7 +4,7 @@ use Foo\TestMessage; use Foo\TestEnum; use Foo\TestMessage\Sub; -class TestBase extends PHPUnit_Framework_TestCase +class TestBase extends \PHPUnit\Framework\TestCase { public function setFields(TestMessage $m) @@ -28,7 +28,7 @@ class TestBase extends PHPUnit_Framework_TestCase $this->assertSame(1.6, $m->getOptionalDouble()); $this->assertSame(true, $m->getOptionalBool()); $this->assertSame('a', $m->getOptionalString()); - $this->assertSame('b', $m->getOptionalBytes()); + $this->assertSame('bbbb', $m->getOptionalBytes()); $this->assertSame(TestEnum::ONE, $m->getOptionalEnum()); $this->assertSame(33, $m->getOptionalMessage()->getA()); if (PHP_INT_SIZE == 4) { @@ -59,7 +59,7 @@ class TestBase extends PHPUnit_Framework_TestCase $this->assertEquals(1.6, $m->getRepeatedDouble()[0]); $this->assertEquals(true, $m->getRepeatedBool()[0]); $this->assertEquals('a', $m->getRepeatedString()[0]); - $this->assertEquals('b', $m->getRepeatedBytes()[0]); + $this->assertEquals('bbbb', $m->getRepeatedBytes()[0]); $this->assertEquals(34, $m->getRepeatedMessage()[0]->GetA()); $this->assertEquals(-52, $m->getRepeatedInt32()[1]); @@ -76,7 +76,7 @@ class TestBase extends PHPUnit_Framework_TestCase $this->assertEquals(2.6, $m->getRepeatedDouble()[1]); $this->assertEquals(false, $m->getRepeatedBool()[1]); $this->assertEquals('c', $m->getRepeatedString()[1]); - $this->assertEquals('d', $m->getRepeatedBytes()[1]); + $this->assertEquals('dddd', $m->getRepeatedBytes()[1]); $this->assertEquals(35, $m->getRepeatedMessage()[1]->GetA()); if (PHP_INT_SIZE == 4) { @@ -101,7 +101,7 @@ class TestBase extends PHPUnit_Framework_TestCase $this->assertEquals(3.6, $m->getMapInt32Double()[1]); $this->assertEquals(true , $m->getMapBoolBool()[true]); $this->assertEquals('e', $m->getMapStringString()['e']); - $this->assertEquals('f', $m->getMapInt32Bytes()[1]); + $this->assertEquals('ffff', $m->getMapInt32Bytes()[1]); $this->assertEquals(TestEnum::ONE, $m->getMapInt32Enum()[1]); $this->assertEquals(36, $m->getMapInt32Message()[1]->GetA()); } @@ -146,7 +146,7 @@ class TestBase extends PHPUnit_Framework_TestCase $this->assertEquals(1.6, $m->getRepeatedDouble()[0]); $this->assertEquals(true, $m->getRepeatedBool()[0]); $this->assertEquals('a', $m->getRepeatedString()[0]); - $this->assertEquals('b', $m->getRepeatedBytes()[0]); + $this->assertEquals('bbbb', $m->getRepeatedBytes()[0]); $this->assertEquals(TestEnum::ZERO, $m->getRepeatedEnum()[0]); $this->assertEquals(34, $m->getRepeatedMessage()[0]->GetA()); @@ -164,7 +164,7 @@ class TestBase extends PHPUnit_Framework_TestCase $this->assertEquals(2.6, $m->getRepeatedDouble()[1]); $this->assertEquals(false, $m->getRepeatedBool()[1]); $this->assertEquals('c', $m->getRepeatedString()[1]); - $this->assertEquals('d', $m->getRepeatedBytes()[1]); + $this->assertEquals('dddd', $m->getRepeatedBytes()[1]); $this->assertEquals(TestEnum::ONE, $m->getRepeatedEnum()[1]); $this->assertEquals(35, $m->getRepeatedMessage()[1]->GetA()); @@ -338,5 +338,6 @@ class TestBase extends PHPUnit_Framework_TestCase // This test is to avoid the warning of no test by php unit. public function testNone() { + $this->assertTrue(true); } } diff --git a/php/tests/test_util.php b/php/tests/test_util.php index a676d097ed..2c5b59541c 100644 --- a/php/tests/test_util.php +++ b/php/tests/test_util.php @@ -65,7 +65,7 @@ class TestUtil $m->setOptionalDouble(1.6); $m->setOptionalBool(true); $m->setOptionalString('a'); - $m->setOptionalBytes('b'); + $m->setOptionalBytes('bbbb'); $m->setOptionalEnum(TestEnum::ONE); $sub = new Sub(); $m->setOptionalMessage($sub); @@ -85,7 +85,7 @@ class TestUtil self::appendHelper($m, 'RepeatedDouble', 1.6); self::appendHelper($m, 'RepeatedBool', true); self::appendHelper($m, 'RepeatedString', 'a'); - self::appendHelper($m, 'RepeatedBytes', 'b'); + self::appendHelper($m, 'RepeatedBytes', 'bbbb'); self::appendHelper($m, 'RepeatedEnum', TestEnum::ZERO); self::appendHelper($m, 'RepeatedMessage', new Sub()); $m->getRepeatedMessage()[0]->setA(34); @@ -104,7 +104,7 @@ class TestUtil self::appendHelper($m, 'RepeatedDouble', 2.6); self::appendHelper($m, 'RepeatedBool', false); self::appendHelper($m, 'RepeatedString', 'c'); - self::appendHelper($m, 'RepeatedBytes', 'd'); + self::appendHelper($m, 'RepeatedBytes', 'dddd'); self::appendHelper($m, 'RepeatedEnum', TestEnum::ONE); self::appendHelper($m, 'RepeatedMessage', new Sub()); $m->getRepeatedMessage()[1]->SetA(35); @@ -123,7 +123,7 @@ class TestUtil self::kvUpdateHelper($m, 'MapInt32Double', 1, 3.6); self::kvUpdateHelper($m, 'MapBoolBool', true, true); self::kvUpdateHelper($m, 'MapStringString', 'e', 'e'); - self::kvUpdateHelper($m, 'MapInt32Bytes', 1, 'f'); + self::kvUpdateHelper($m, 'MapInt32Bytes', 1, 'ffff'); self::kvUpdateHelper($m, 'MapInt32Enum', 1, TestEnum::ONE); self::kvUpdateHelper($m, 'MapInt32Message', 1, new Sub()); $m->getMapInt32Message()[1]->SetA(36); @@ -234,7 +234,7 @@ class TestUtil assert(1.6 === $m->getOptionalDouble()); assert(true=== $m->getOptionalBool()); assert('a' === $m->getOptionalString()); - assert('b' === $m->getOptionalBytes()); + assert('bbbb' === $m->getOptionalBytes()); assert(TestEnum::ONE === $m->getOptionalEnum()); assert(33 === $m->getOptionalMessage()->getA()); @@ -260,7 +260,7 @@ class TestUtil assert(1.6 === $m->getRepeatedDouble()[0]); assert(true=== $m->getRepeatedBool()[0]); assert('a' === $m->getRepeatedString()[0]); - assert('b' === $m->getRepeatedBytes()[0]); + assert('bbbb' === $m->getRepeatedBytes()[0]); assert(TestEnum::ZERO === $m->getRepeatedEnum()[0]); assert(34 === $m->getRepeatedMessage()[0]->getA()); @@ -286,7 +286,7 @@ class TestUtil assert(2.6 === $m->getRepeatedDouble()[1]); assert(false === $m->getRepeatedBool()[1]); assert('c' === $m->getRepeatedString()[1]); - assert('d' === $m->getRepeatedBytes()[1]); + assert('dddd' === $m->getRepeatedBytes()[1]); assert(TestEnum::ONE === $m->getRepeatedEnum()[1]); assert(35 === $m->getRepeatedMessage()[1]->getA()); @@ -312,7 +312,7 @@ class TestUtil assert(3.6 === $m->getMapInt32Double()[1]); assert(true === $m->getMapBoolBool()[true]); assert('e' === $m->getMapStringString()['e']); - assert('f' === $m->getMapInt32Bytes()[1]); + assert('ffff' === $m->getMapInt32Bytes()[1]); assert(TestEnum::ONE === $m->getMapInt32Enum()[1]); assert(36 === $m->getMapInt32Message()[1]->GetA()); } @@ -334,7 +334,7 @@ class TestUtil "619A9999999999F93F" . "6801" . "720161" . - "7A0162" . + "7A0462626262" . "800101" . "8A01020821" . @@ -366,8 +366,8 @@ class TestUtil "D80200" . "E2020161" . "E2020163" . - "EA020162" . - "EA020164" . + "EA020462626262" . + "EA020464646464" . "F00200" . "F00201" . "FA02020822" . @@ -387,7 +387,7 @@ class TestUtil "92050B080111CDCCCCCCCCCC0C40" . "9A050408011001" . "A205060a0165120165" . - "AA05050801120166" . + "AA05080801120466666666" . "B2050408011001" . "Ba0506080112020824" ); diff --git a/php/tests/well_known_test.php b/php/tests/well_known_test.php index 9f2661fafd..a16e070a45 100644 --- a/php/tests/well_known_test.php +++ b/php/tests/well_known_test.php @@ -42,11 +42,13 @@ class WellKnownTest extends TestBase { public function testEmpty() { $msg = new GPBEmpty(); + $this->assertTrue($msg instanceof \Google\Protobuf\Internal\Message); } public function testImportDescriptorProto() { $msg = new TestImportDescriptorProto(); + $this->assertTrue(true); } public function testAny() @@ -391,4 +393,27 @@ class WellKnownTest extends TestBase { $m->setValue("a"); $this->assertSame("a", $m->getValue()); } + + /** + * @dataProvider enumNameValueConversionDataProvider + */ + public function testEnumNameValueConversion($class) + { + $reflectionClass = new ReflectionClass($class); + $constants = $reflectionClass->getConstants(); + foreach ($constants as $k => $v) { + $this->assertSame($k, $class::name($v)); + $this->assertSame($v, $class::value($k)); + } + } + + public function enumNameValueConversionDataProvider() + { + return [ + ['\Google\Protobuf\Field\Cardinality'], + ['\Google\Protobuf\Field\Kind'], + ['\Google\Protobuf\NullValue'], + ['\Google\Protobuf\Syntax'], + ]; + } } diff --git a/php/tests/wrapper_type_setters_test.php b/php/tests/wrapper_type_setters_test.php new file mode 100644 index 0000000000..ad9f7181dd --- /dev/null +++ b/php/tests/wrapper_type_setters_test.php @@ -0,0 +1,312 @@ + $value]); + + // Set values using new and old setters + $oldSetterMsg->$setter($wrappedValue); + $newSetterMsg->$valueSetter($value); + + // Get expected values old getter + $expectedValue = $oldSetterMsg->$getter(); + + // Check that old getter returns the same value after using the + // new setter + $actualValue = $newSetterMsg->$getter(); + $this->assertEquals($expectedValue, $actualValue); + + // Check that new getter returns the unwrapped value from + // $expectedValue + $actualValueNewGetter = $newSetterMsg->$valueGetter(); + if (is_null($expectedValue)) { + $this->assertNull($actualValueNewGetter); + } else { + $this->assertEquals($expectedValue->getValue(), $actualValueNewGetter); + } + } + } + + public function gettersAndSettersDataProvider() + { + return [ + [TestWrapperSetters::class, DoubleValue::class, "setDoubleValue", "setDoubleValueUnwrapped", "getDoubleValue", "getDoubleValueUnwrapped", [ + [1.1, new DoubleValue(["value" => 1.1])], + [2.2, new DoubleValue(["value" => 2.2])], + [null, null], + [0, new DoubleValue()], + ]], + [TestWrapperSetters::class, FloatValue::class, "setFloatValue", "setFloatValueUnwrapped", "getFloatValue", "getFloatValueUnwrapped", [ + [1.1, new FloatValue(["value" => 1.1])], + [2.2, new FloatValue(["value" => 2.2])], + [null, null], + [0, new FloatValue()], + ]], + [TestWrapperSetters::class, Int64Value::class, "setInt64Value", "setInt64ValueUnwrapped", "getInt64Value", "getInt64ValueUnwrapped", [ + [123, new Int64Value(["value" => 123])], + [-789, new Int64Value(["value" => -789])], + [null, null], + [0, new Int64Value()], + [5.5, new Int64Value(["value" => 5])], // Test conversion from float to int + ]], + [TestWrapperSetters::class, UInt64Value::class, "setUInt64Value", "setUInt64ValueUnwrapped", "getUInt64Value", "getUInt64ValueUnwrapped", [ + [123, new UInt64Value(["value" => 123])], + [789, new UInt64Value(["value" => 789])], + [null, null], + [0, new UInt64Value()], + [5.5, new UInt64Value(["value" => 5])], // Test conversion from float to int + [-7, new UInt64Value(["value" => -7])], // Test conversion from -ve to +ve + ]], + [TestWrapperSetters::class, Int32Value::class, "setInt32Value", "setInt32ValueUnwrapped", "getInt32Value", "getInt32ValueUnwrapped", [ + [123, new Int32Value(["value" => 123])], + [-789, new Int32Value(["value" => -789])], + [null, null], + [0, new Int32Value()], + [5.5, new Int32Value(["value" => 5])], // Test conversion from float to int + ]], + [TestWrapperSetters::class, UInt32Value::class, "setUInt32Value", "setUInt32ValueUnwrapped", "getUInt32Value", "getUInt32ValueUnwrapped", [ + [123, new UInt32Value(["value" => 123])], + [789, new UInt32Value(["value" => 789])], + [null, null], + [0, new UInt32Value()], + [5.5, new UInt32Value(["value" => 5])], // Test conversion from float to int + [-7, new UInt32Value(["value" => -7])], // Test conversion from -ve to +ve + ]], + [TestWrapperSetters::class, BoolValue::class, "setBoolValue", "setBoolValueUnwrapped", "getBoolValue", "getBoolValueUnwrapped", [ + [true, new BoolValue(["value" => true])], + [false, new BoolValue(["value" => false])], + [null, null], + ]], + [TestWrapperSetters::class, StringValue::class, "setStringValue", "setStringValueUnwrapped", "getStringValue", "getStringValueUnwrapped", [ + ["asdf", new StringValue(["value" => "asdf"])], + ["", new StringValue(["value" => ""])], + [null, null], + ["", new StringValue()], + [5, new StringValue(["value" => "5"])], // Test conversion from number to string + [5.5, new StringValue(["value" => "5.5"])], // Test conversion from number to string + [-7, new StringValue(["value" => "-7"])], // Test conversion from number to string + [-7.5, new StringValue(["value" => "-7.5"])], // Test conversion from number to string + ]], + [TestWrapperSetters::class, BytesValue::class, "setBytesValue", "setBytesValueUnwrapped", "getBytesValue", "getBytesValueUnwrapped", [ + ["asdf", new BytesValue(["value" => "asdf"])], + ["", new BytesValue(["value" => ""])], + [null, null], + ["", new BytesValue()], + [5, new BytesValue(["value" => "5"])], // Test conversion from number to bytes + [5.5, new BytesValue(["value" => "5.5"])], // Test conversion from number to bytes + [-7, new BytesValue(["value" => "-7"])], // Test conversion from number to bytes + [-7.5, new BytesValue(["value" => "-7.5"])], // Test conversion from number to bytes + ]], + [TestWrapperSetters::class, DoubleValue::class, "setDoubleValueOneof", "setDoubleValueOneofUnwrapped", "getDoubleValueOneof", "getDoubleValueOneofUnwrapped", [ + [1.1, new DoubleValue(["value" => 1.1])], + [2.2, new DoubleValue(["value" => 2.2])], + [null, null], + [0, new DoubleValue()], + ]],[TestWrapperSetters::class, StringValue::class, "setStringValueOneof", "setStringValueOneofUnwrapped", "getStringValueOneof", "getStringValueOneofUnwrapped", [ + ["asdf", new StringValue(["value" => "asdf"])], + ["", new StringValue(["value" => ""])], + [null, null], + ["", new StringValue()], + [5, new StringValue(["value" => "5"])], // Test conversion from number to string + [5.5, new StringValue(["value" => "5.5"])], // Test conversion from number to string + [-7, new StringValue(["value" => "-7"])], // Test conversion from number to string + [-7.5, new StringValue(["value" => "-7.5"])], // Test conversion from number to string + ]], + ]; + } + + /** + * @dataProvider invalidSettersDataProvider + * @expectedException \Exception + */ + public function testInvalidSetters($class, $setter, $value) + { + (new $class())->$setter($value); + } + + public function invalidSettersDataProvider() + { + return [ + [TestWrapperSetters::class, "setDoubleValueUnwrapped", "abc"], + [TestWrapperSetters::class, "setDoubleValueUnwrapped", []], + [TestWrapperSetters::class, "setDoubleValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setDoubleValueUnwrapped", new DoubleValue()], + + [TestWrapperSetters::class, "setFloatValueUnwrapped", "abc"], + [TestWrapperSetters::class, "setFloatValueUnwrapped", []], + [TestWrapperSetters::class, "setFloatValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setFloatValueUnwrapped", new FloatValue()], + + [TestWrapperSetters::class, "setInt64ValueUnwrapped", "abc"], + [TestWrapperSetters::class, "setInt64ValueUnwrapped", []], + [TestWrapperSetters::class, "setInt64ValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setInt64ValueUnwrapped", new Int64Value()], + + [TestWrapperSetters::class, "setUInt64ValueUnwrapped", "abc"], + [TestWrapperSetters::class, "setUInt64ValueUnwrapped", []], + [TestWrapperSetters::class, "setUInt64ValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setUInt64ValueUnwrapped", new UInt64Value()], + + [TestWrapperSetters::class, "setInt32ValueUnwrapped", "abc"], + [TestWrapperSetters::class, "setInt32ValueUnwrapped", []], + [TestWrapperSetters::class, "setInt32ValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setInt32ValueUnwrapped", new Int32Value()], + + [TestWrapperSetters::class, "setUInt32ValueUnwrapped", "abc"], + [TestWrapperSetters::class, "setUInt32ValueUnwrapped", []], + [TestWrapperSetters::class, "setUInt32ValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setUInt32ValueUnwrapped", new UInt32Value()], + + [TestWrapperSetters::class, "setBoolValueUnwrapped", []], + [TestWrapperSetters::class, "setBoolValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setBoolValueUnwrapped", new BoolValue()], + + [TestWrapperSetters::class, "setStringValueUnwrapped", []], + [TestWrapperSetters::class, "setStringValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setStringValueUnwrapped", new StringValue()], + + [TestWrapperSetters::class, "setBytesValueUnwrapped", []], + [TestWrapperSetters::class, "setBytesValueUnwrapped", new stdClass()], + [TestWrapperSetters::class, "setBytesValueUnwrapped", new BytesValue()], + ]; + } + + /** + * @dataProvider constructorWithWrapperTypeDataProvider + */ + public function testConstructorWithWrapperType($class, $wrapperClass, $wrapperField, $getter, $value) + { + $actualInstance = new $class([$wrapperField => $value]); + $expectedInstance = new $class([$wrapperField => new $wrapperClass(['value' => $value])]); + $this->assertEquals($expectedInstance->$getter()->getValue(), $actualInstance->$getter()->getValue()); + } + + public function constructorWithWrapperTypeDataProvider() + { + return [ + [TestWrapperSetters::class, DoubleValue::class, 'double_value', 'getDoubleValue', 1.1], + [TestWrapperSetters::class, FloatValue::class, 'float_value', 'getFloatValue', 2.2], + [TestWrapperSetters::class, Int64Value::class, 'int64_value', 'getInt64Value', 3], + [TestWrapperSetters::class, UInt64Value::class, 'uint64_value', 'getUInt64Value', 4], + [TestWrapperSetters::class, Int32Value::class, 'int32_value', 'getInt32Value', 5], + [TestWrapperSetters::class, UInt32Value::class, 'uint32_value', 'getUInt32Value', 6], + [TestWrapperSetters::class, BoolValue::class, 'bool_value', 'getBoolValue', true], + [TestWrapperSetters::class, StringValue::class, 'string_value', 'getStringValue', "eight"], + [TestWrapperSetters::class, BytesValue::class, 'bytes_value', 'getBytesValue', "nine"], + ]; + } + + /** + * @dataProvider constructorWithRepeatedWrapperTypeDataProvider + */ + public function testConstructorWithRepeatedWrapperType($wrapperField, $getter, $value) + { + $actualInstance = new TestWrapperSetters([$wrapperField => $value]); + foreach ($actualInstance->$getter() as $key => $actualWrapperValue) { + $actualInnerValue = $actualWrapperValue->getValue(); + $expectedElement = $value[$key]; + if (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\StringValue')) { + $expectedInnerValue = $expectedElement->getValue(); + } else { + $expectedInnerValue = $expectedElement; + } + $this->assertEquals($expectedInnerValue, $actualInnerValue); + } + } + + public function constructorWithRepeatedWrapperTypeDataProvider() + { + $sv7 = new StringValue(['value' => 'seven']); + $sv8 = new StringValue(['value' => 'eight']); + + $testWrapperSetters = new TestWrapperSetters(); + $testWrapperSetters->setRepeatedStringValue([$sv7, $sv8]); + $repeatedField = $testWrapperSetters->getRepeatedStringValue(); + + return [ + ['repeated_string_value', 'getRepeatedStringValue', []], + ['repeated_string_value', 'getRepeatedStringValue', [$sv7]], + ['repeated_string_value', 'getRepeatedStringValue', [$sv7, $sv8]], + ['repeated_string_value', 'getRepeatedStringValue', ['seven']], + ['repeated_string_value', 'getRepeatedStringValue', [7]], + ['repeated_string_value', 'getRepeatedStringValue', [7.7]], + ['repeated_string_value', 'getRepeatedStringValue', ['seven', 'eight']], + ['repeated_string_value', 'getRepeatedStringValue', [$sv7, 'eight']], + ['repeated_string_value', 'getRepeatedStringValue', ['seven', $sv8]], + ['repeated_string_value', 'getRepeatedStringValue', $repeatedField], + ]; + } + + /** + * @dataProvider constructorWithMapWrapperTypeDataProvider + */ + public function testConstructorWithMapWrapperType($wrapperField, $getter, $value) + { + $actualInstance = new TestWrapperSetters([$wrapperField => $value]); + foreach ($actualInstance->$getter() as $key => $actualWrapperValue) { + $actualInnerValue = $actualWrapperValue->getValue(); + $expectedElement = $value[$key]; + if (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\StringValue')) { + $expectedInnerValue = $expectedElement->getValue(); + } elseif (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\Internal\MapEntry')) { + $expectedInnerValue = $expectedElement->getValue()->getValue(); + } else { + $expectedInnerValue = $expectedElement; + } + $this->assertEquals($expectedInnerValue, $actualInnerValue); + } + } + + public function constructorWithMapWrapperTypeDataProvider() + { + $sv7 = new StringValue(['value' => 'seven']); + $sv8 = new StringValue(['value' => 'eight']); + + $testWrapperSetters = new TestWrapperSetters(); + $testWrapperSetters->setMapStringValue(['key' => $sv7, 'key2' => $sv8]); + $mapField = $testWrapperSetters->getMapStringValue(); + + return [ + ['map_string_value', 'getMapStringValue', []], + ['map_string_value', 'getMapStringValue', ['key' => $sv7]], + ['map_string_value', 'getMapStringValue', ['key' => $sv7, 'key2' => $sv8]], + ['map_string_value', 'getMapStringValue', ['key' => 'seven']], + ['map_string_value', 'getMapStringValue', ['key' => 7]], + ['map_string_value', 'getMapStringValue', ['key' => 7.7]], + ['map_string_value', 'getMapStringValue', ['key' => 'seven', 'key2' => 'eight']], + ['map_string_value', 'getMapStringValue', ['key' => $sv7, 'key2' => 'eight']], + ['map_string_value', 'getMapStringValue', ['key' => 'seven', 'key2' => $sv8]], + ['map_string_value', 'getMapStringValue', $mapField], + ]; + } +} diff --git a/protobuf-lite.pc.in b/protobuf-lite.pc.in index 80f1f46195..68a2bb455b 100644 --- a/protobuf-lite.pc.in +++ b/protobuf-lite.pc.in @@ -6,6 +6,6 @@ includedir=@includedir@ Name: Protocol Buffers Description: Google's Data Interchange Format Version: @VERSION@ -Libs: -L${libdir} -lprotobuf-lite @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ +Libs: -L${libdir} -lprotobuf-lite @PTHREAD_LIBS@ Cflags: -I${includedir} @PTHREAD_CFLAGS@ Conflicts: protobuf diff --git a/protobuf.bzl b/protobuf.bzl index 67d61d7ba7..e0653321f8 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -1,174 +1,180 @@ -load("@bazel_skylib//:lib.bzl", "versions") +load("@bazel_skylib//lib:versions.bzl", "versions") def _GetPath(ctx, path): - if ctx.label.workspace_root: - return ctx.label.workspace_root + '/' + path - else: - return path + if ctx.label.workspace_root: + return ctx.label.workspace_root + "/" + path + else: + return path def _IsNewExternal(ctx): - # Bazel 0.4.4 and older have genfiles paths that look like: - # bazel-out/local-fastbuild/genfiles/external/repo/foo - # After the exec root rearrangement, they look like: - # ../repo/bazel-out/local-fastbuild/genfiles/foo - return ctx.label.workspace_root.startswith("../") + # Bazel 0.4.4 and older have genfiles paths that look like: + # bazel-out/local-fastbuild/genfiles/external/repo/foo + # After the exec root rearrangement, they look like: + # ../repo/bazel-out/local-fastbuild/genfiles/foo + return ctx.label.workspace_root.startswith("../") def _GenDir(ctx): - if _IsNewExternal(ctx): - # We are using the fact that Bazel 0.4.4+ provides repository-relative paths - # for ctx.genfiles_dir. - return ctx.genfiles_dir.path + ( - "/" + ctx.attr.includes[0] if ctx.attr.includes and ctx.attr.includes[0] else "") - # This means that we're either in the old version OR the new version in the local repo. - # Either way, appending the source path to the genfiles dir works. - return ctx.var["GENDIR"] + "/" + _SourceDir(ctx) + if _IsNewExternal(ctx): + # We are using the fact that Bazel 0.4.4+ provides repository-relative paths + # for ctx.genfiles_dir. + return ctx.genfiles_dir.path + ( + "/" + ctx.attr.includes[0] if ctx.attr.includes and ctx.attr.includes[0] else "" + ) + + # This means that we're either in the old version OR the new version in the local repo. + # Either way, appending the source path to the genfiles dir works. + return ctx.var["GENDIR"] + "/" + _SourceDir(ctx) def _SourceDir(ctx): - if not ctx.attr.includes: - return ctx.label.workspace_root - if not ctx.attr.includes[0]: - return _GetPath(ctx, ctx.label.package) - if not ctx.label.package: - return _GetPath(ctx, ctx.attr.includes[0]) - return _GetPath(ctx, ctx.label.package + '/' + ctx.attr.includes[0]) - -def _CcHdrs(srcs, use_grpc_plugin=False): - ret = [s[:-len(".proto")] + ".pb.h" for s in srcs] - if use_grpc_plugin: - ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs] - return ret - -def _CcSrcs(srcs, use_grpc_plugin=False): - ret = [s[:-len(".proto")] + ".pb.cc" for s in srcs] - if use_grpc_plugin: - ret += [s[:-len(".proto")] + ".grpc.pb.cc" for s in srcs] - return ret - -def _CcOuts(srcs, use_grpc_plugin=False): - return _CcHdrs(srcs, use_grpc_plugin) + _CcSrcs(srcs, use_grpc_plugin) - -def _PyOuts(srcs, use_grpc_plugin=False): - ret = [s[:-len(".proto")] + "_pb2.py" for s in srcs] - if use_grpc_plugin: - ret += [s[:-len(".proto")] + "_pb2_grpc.py" for s in srcs] - return ret - -def _RelativeOutputPath(path, include, dest=""): - if include == None: - return path - - if not path.startswith(include): - fail("Include path %s isn't part of the path %s." % (include, path)) - - if include and include[-1] != '/': - include = include + '/' - if dest and dest[-1] != '/': - dest = dest + '/' - - path = path[len(include):] - return dest + path + if not ctx.attr.includes: + return ctx.label.workspace_root + if not ctx.attr.includes[0]: + return _GetPath(ctx, ctx.label.package) + if not ctx.label.package: + return _GetPath(ctx, ctx.attr.includes[0]) + return _GetPath(ctx, ctx.label.package + "/" + ctx.attr.includes[0]) + +def _CcHdrs(srcs, use_grpc_plugin = False): + ret = [s[:-len(".proto")] + ".pb.h" for s in srcs] + if use_grpc_plugin: + ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs] + return ret + +def _CcSrcs(srcs, use_grpc_plugin = False): + ret = [s[:-len(".proto")] + ".pb.cc" for s in srcs] + if use_grpc_plugin: + ret += [s[:-len(".proto")] + ".grpc.pb.cc" for s in srcs] + return ret + +def _CcOuts(srcs, use_grpc_plugin = False): + return _CcHdrs(srcs, use_grpc_plugin) + _CcSrcs(srcs, use_grpc_plugin) + +def _PyOuts(srcs, use_grpc_plugin = False): + ret = [s[:-len(".proto")] + "_pb2.py" for s in srcs] + if use_grpc_plugin: + ret += [s[:-len(".proto")] + "_pb2_grpc.py" for s in srcs] + return ret + +def _RelativeOutputPath(path, include, dest = ""): + if include == None: + return path + + if not path.startswith(include): + fail("Include path %s isn't part of the path %s." % (include, path)) + + if include and include[-1] != "/": + include = include + "/" + if dest and dest[-1] != "/": + dest = dest + "/" + + path = path[len(include):] + return dest + path def _proto_gen_impl(ctx): - """General implementation for generating protos""" - srcs = ctx.files.srcs - deps = [] - deps += ctx.files.srcs - source_dir = _SourceDir(ctx) - gen_dir = _GenDir(ctx).rstrip('/') - if source_dir: - import_flags = ["-I" + source_dir, "-I" + gen_dir] - else: - import_flags = ["-I."] - - for dep in ctx.attr.deps: - import_flags += dep.proto.import_flags - deps += dep.proto.deps - - if not ctx.attr.gen_cc and not ctx.attr.gen_py and not ctx.executable.plugin: - return struct( - proto=struct( - srcs=srcs, - import_flags=import_flags, - deps=deps, - ), - ) - - for src in srcs: - args = [] - - in_gen_dir = src.root.path == gen_dir - if in_gen_dir: - import_flags_real = [] - for f in depset(import_flags): - path = f.replace('-I', '') - import_flags_real.append('-I$(realpath -s %s)' % path) - - outs = [] - use_grpc_plugin = (ctx.attr.plugin_language == "grpc" and ctx.attr.plugin) - path_tpl = "$(realpath %s)" if in_gen_dir else "%s" - if ctx.attr.gen_cc: - args += [("--cpp_out=" + path_tpl) % gen_dir] - outs.extend(_CcOuts([src.basename], use_grpc_plugin=use_grpc_plugin)) - if ctx.attr.gen_py: - args += [("--python_out=" + path_tpl) % gen_dir] - outs.extend(_PyOuts([src.basename], use_grpc_plugin=use_grpc_plugin)) - - outs = [ctx.actions.declare_file(out, sibling=src) for out in outs] - inputs = [src] + deps - if ctx.executable.plugin: - plugin = ctx.executable.plugin - lang = ctx.attr.plugin_language - if not lang and plugin.basename.startswith('protoc-gen-'): - lang = plugin.basename[len('protoc-gen-'):] - if not lang: - fail("cannot infer the target language of plugin", "plugin_language") - - outdir = "." if in_gen_dir else gen_dir - - if ctx.attr.plugin_options: - outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir - args += [("--plugin=protoc-gen-%s=" + path_tpl) % (lang, plugin.path)] - args += ["--%s_out=%s" % (lang, outdir)] - inputs += [plugin] - - if not in_gen_dir: - ctx.action( - inputs=inputs, - outputs=outs, - arguments=args + import_flags + [src.path], - executable=ctx.executable.protoc, - mnemonic="ProtoCompile", - use_default_shell_env=True, - ) + """General implementation for generating protos""" + srcs = ctx.files.srcs + deps = [] + deps += ctx.files.srcs + source_dir = _SourceDir(ctx) + gen_dir = _GenDir(ctx).rstrip("/") + if source_dir: + import_flags = ["-I" + source_dir, "-I" + gen_dir] else: - for out in outs: - orig_command = " ".join( - ["$(realpath %s)" % ctx.executable.protoc.path] + args + - import_flags_real + ["-I.", src.basename]) - command = ";".join([ - 'CMD="%s"' % orig_command, - "cd %s" % src.dirname, - "${CMD}", - "cd -", - ]) - generated_out = '/'.join([gen_dir, out.basename]) - if generated_out != out.path: - command += ";mv %s %s" % (generated_out, out.path) - ctx.action( - inputs=inputs + [ctx.executable.protoc], - outputs=[out], - command=command, - mnemonic="ProtoCompile", - use_default_shell_env=True, + import_flags = ["-I."] + + for dep in ctx.attr.deps: + import_flags += dep.proto.import_flags + deps += dep.proto.deps + + if not ctx.attr.gen_cc and not ctx.attr.gen_py and not ctx.executable.plugin: + return struct( + proto = struct( + srcs = srcs, + import_flags = import_flags, + deps = deps, + ), ) - return struct( - proto=struct( - srcs=srcs, - import_flags=import_flags, - deps=deps, - ), - ) + for src in srcs: + args = [] + + in_gen_dir = src.root.path == gen_dir + if in_gen_dir: + import_flags_real = [] + for f in depset(import_flags).to_list(): + path = f.replace("-I", "") + import_flags_real.append("-I$(realpath -s %s)" % path) + + outs = [] + use_grpc_plugin = (ctx.attr.plugin_language == "grpc" and ctx.attr.plugin) + path_tpl = "$(realpath %s)" if in_gen_dir else "%s" + if ctx.attr.gen_cc: + args += [("--cpp_out=" + path_tpl) % gen_dir] + outs.extend(_CcOuts([src.basename], use_grpc_plugin = use_grpc_plugin)) + if ctx.attr.gen_py: + args += [("--python_out=" + path_tpl) % gen_dir] + outs.extend(_PyOuts([src.basename], use_grpc_plugin = use_grpc_plugin)) + + outs = [ctx.actions.declare_file(out, sibling = src) for out in outs] + inputs = [src] + deps + tools = [ctx.executable.protoc] + if ctx.executable.plugin: + plugin = ctx.executable.plugin + lang = ctx.attr.plugin_language + if not lang and plugin.basename.startswith("protoc-gen-"): + lang = plugin.basename[len("protoc-gen-"):] + if not lang: + fail("cannot infer the target language of plugin", "plugin_language") + + outdir = "." if in_gen_dir else gen_dir + + if ctx.attr.plugin_options: + outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir + args += [("--plugin=protoc-gen-%s=" + path_tpl) % (lang, plugin.path)] + args += ["--%s_out=%s" % (lang, outdir)] + tools.append(plugin) + + if not in_gen_dir: + ctx.actions.run( + inputs = inputs, + tools = tools, + outputs = outs, + arguments = args + import_flags + [src.path], + executable = ctx.executable.protoc, + mnemonic = "ProtoCompile", + use_default_shell_env = True, + ) + else: + for out in outs: + orig_command = " ".join( + ["$(realpath %s)" % ctx.executable.protoc.path] + args + + import_flags_real + ["-I.", src.basename], + ) + command = ";".join([ + 'CMD="%s"' % orig_command, + "cd %s" % src.dirname, + "${CMD}", + "cd -", + ]) + generated_out = "/".join([gen_dir, out.basename]) + if generated_out != out.path: + command += ";mv %s %s" % (generated_out, out.path) + ctx.actions.run_shell( + inputs = inputs, + outputs = [out], + command = command, + mnemonic = "ProtoCompile", + tools = tools, + use_default_shell_env = True, + ) + + return struct( + proto = struct( + srcs = srcs, + import_flags = import_flags, + deps = deps, + ), + ) proto_gen = rule( attrs = { @@ -178,7 +184,7 @@ proto_gen = rule( "protoc": attr.label( cfg = "host", executable = True, - single_file = True, + allow_single_file = True, mandatory = True, ), "plugin": attr.label( @@ -218,245 +224,251 @@ Args: def cc_proto_library( name, - srcs=[], - deps=[], - cc_libs=[], - include=None, - protoc="@com_google_protobuf//:protoc", - internal_bootstrap_hack=False, - use_grpc_plugin=False, - default_runtime="@com_google_protobuf//:protobuf", + srcs = [], + deps = [], + cc_libs = [], + include = None, + protoc = "@com_google_protobuf//:protoc", + internal_bootstrap_hack = False, + use_grpc_plugin = False, + default_runtime = "@com_google_protobuf//:protobuf", **kargs): - """Bazel rule to create a C++ protobuf library from proto source files - - NOTE: the rule is only an internal workaround to generate protos. The - interface may change and the rule may be removed when bazel has introduced - the native rule. - - Args: - name: the name of the cc_proto_library. - srcs: the .proto files of the cc_proto_library. - deps: a list of dependency labels; must be cc_proto_library. - cc_libs: a list of other cc_library targets depended by the generated - cc_library. - include: a string indicating the include path of the .proto files. - protoc: the label of the protocol compiler to generate the sources. - internal_bootstrap_hack: a flag indicate the cc_proto_library is used only - for bootstraping. When it is set to True, no files will be generated. - The rule will simply be a provider for .proto files, so that other - cc_proto_library can depend on it. - use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin - when processing the proto files. - default_runtime: the implicitly default runtime which will be depended on by - the generated cc_library target. - **kargs: other keyword arguments that are passed to cc_library. - - """ - - includes = [] - if include != None: - includes = [include] - - if internal_bootstrap_hack: - # For pre-checked-in generated files, we add the internal_bootstrap_hack - # which will skip the codegen action. + """Bazel rule to create a C++ protobuf library from proto source files + + NOTE: the rule is only an internal workaround to generate protos. The + interface may change and the rule may be removed when bazel has introduced + the native rule. + + Args: + name: the name of the cc_proto_library. + srcs: the .proto files of the cc_proto_library. + deps: a list of dependency labels; must be cc_proto_library. + cc_libs: a list of other cc_library targets depended by the generated + cc_library. + include: a string indicating the include path of the .proto files. + protoc: the label of the protocol compiler to generate the sources. + internal_bootstrap_hack: a flag indicate the cc_proto_library is used only + for bootstraping. When it is set to True, no files will be generated. + The rule will simply be a provider for .proto files, so that other + cc_proto_library can depend on it. + use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin + when processing the proto files. + default_runtime: the implicitly default runtime which will be depended on by + the generated cc_library target. + **kargs: other keyword arguments that are passed to cc_library. + + """ + + includes = [] + if include != None: + includes = [include] + + if internal_bootstrap_hack: + # For pre-checked-in generated files, we add the internal_bootstrap_hack + # which will skip the codegen action. + proto_gen( + name = name + "_genproto", + srcs = srcs, + deps = [s + "_genproto" for s in deps], + includes = includes, + protoc = protoc, + visibility = ["//visibility:public"], + ) + + # An empty cc_library to make rule dependency consistent. + native.cc_library( + name = name, + **kargs + ) + return + + grpc_cpp_plugin = None + if use_grpc_plugin: + grpc_cpp_plugin = "//external:grpc_cpp_plugin" + + gen_srcs = _CcSrcs(srcs, use_grpc_plugin) + gen_hdrs = _CcHdrs(srcs, use_grpc_plugin) + outs = gen_srcs + gen_hdrs + proto_gen( - name=name + "_genproto", - srcs=srcs, - deps=[s + "_genproto" for s in deps], - includes=includes, - protoc=protoc, - visibility=["//visibility:public"], + name = name + "_genproto", + srcs = srcs, + deps = [s + "_genproto" for s in deps], + includes = includes, + protoc = protoc, + plugin = grpc_cpp_plugin, + plugin_language = "grpc", + gen_cc = 1, + outs = outs, + visibility = ["//visibility:public"], ) - # An empty cc_library to make rule dependency consistent. + + if default_runtime and not default_runtime in cc_libs: + cc_libs = cc_libs + [default_runtime] + if use_grpc_plugin: + cc_libs = cc_libs + ["//external:grpc_lib"] + native.cc_library( - name=name, - **kargs) - return - - grpc_cpp_plugin = None - if use_grpc_plugin: - grpc_cpp_plugin = "//external:grpc_cpp_plugin" - - gen_srcs = _CcSrcs(srcs, use_grpc_plugin) - gen_hdrs = _CcHdrs(srcs, use_grpc_plugin) - outs = gen_srcs + gen_hdrs - - proto_gen( - name=name + "_genproto", - srcs=srcs, - deps=[s + "_genproto" for s in deps], - includes=includes, - protoc=protoc, - plugin=grpc_cpp_plugin, - plugin_language="grpc", - gen_cc=1, - outs=outs, - visibility=["//visibility:public"], - ) - - if default_runtime and not default_runtime in cc_libs: - cc_libs = cc_libs + [default_runtime] - if use_grpc_plugin: - cc_libs = cc_libs + ["//external:grpc_lib"] - - native.cc_library( - name=name, - srcs=gen_srcs, - hdrs=gen_hdrs, - deps=cc_libs + deps, - includes=includes, - **kargs) + name = name, + srcs = gen_srcs, + hdrs = gen_hdrs, + deps = cc_libs + deps, + includes = includes, + **kargs + ) def internal_gen_well_known_protos_java(srcs): - """Bazel rule to generate the gen_well_known_protos_java genrule - - Args: - srcs: the well known protos - """ - root = Label("%s//protobuf_java" % (native.repository_name())).workspace_root - pkg = native.package_name() + "/" if native.package_name() else "" - if root == "": - include = " -I%ssrc " % pkg - else: - include = " -I%s/%ssrc " % (root, pkg) - native.genrule( - name = "gen_well_known_protos_java", - srcs = srcs, - outs = [ - "wellknown.srcjar", - ], - cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" + - " %s $(SRCS) " % include + - " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar", - tools = [":protoc"], - ) + """Bazel rule to generate the gen_well_known_protos_java genrule + + Args: + srcs: the well known protos + """ + root = Label("%s//protobuf_java" % (native.repository_name())).workspace_root + pkg = native.package_name() + "/" if native.package_name() else "" + if root == "": + include = " -I%ssrc " % pkg + else: + include = " -I%s/%ssrc " % (root, pkg) + native.genrule( + name = "gen_well_known_protos_java", + srcs = srcs, + outs = [ + "wellknown.srcjar", + ], + cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" + + " %s $(SRCS) " % include + + " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar", + tools = [":protoc"], + ) def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs): - """Macro to copy files to a different directory and then create a filegroup. - - This is used by the //:protobuf_python py_proto_library target to work around - an issue caused by Python source files that are part of the same Python - package being in separate directories. - - Args: - srcs: The source files to copy and add to the filegroup. - strip_prefix: Path to the root of the files to copy. - dest: The directory to copy the source files into. - **kwargs: extra arguments that will be passesd to the filegroup. - """ - outs = [_RelativeOutputPath(s, strip_prefix, dest) for s in srcs] - - native.genrule( - name = name + "_genrule", - srcs = srcs, - outs = outs, - cmd = " && ".join( - ["cp $(location %s) $(location %s)" % - (s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs]), - ) - - native.filegroup( - name = name, - srcs = outs, - **kwargs) + """Macro to copy files to a different directory and then create a filegroup. + + This is used by the //:protobuf_python py_proto_library target to work around + an issue caused by Python source files that are part of the same Python + package being in separate directories. + + Args: + srcs: The source files to copy and add to the filegroup. + strip_prefix: Path to the root of the files to copy. + dest: The directory to copy the source files into. + **kwargs: extra arguments that will be passesd to the filegroup. + """ + outs = [_RelativeOutputPath(s, strip_prefix, dest) for s in srcs] + + native.genrule( + name = name + "_genrule", + srcs = srcs, + outs = outs, + cmd = " && ".join( + ["cp $(location %s) $(location %s)" % + (s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs], + ), + ) + + native.filegroup( + name = name, + srcs = outs, + **kwargs + ) def py_proto_library( name, - srcs=[], - deps=[], - py_libs=[], - py_extra_srcs=[], - include=None, - default_runtime="@com_google_protobuf//:protobuf_python", - protoc="@com_google_protobuf//:protoc", - use_grpc_plugin=False, + srcs = [], + deps = [], + py_libs = [], + py_extra_srcs = [], + include = None, + default_runtime = "@com_google_protobuf//:protobuf_python", + protoc = "@com_google_protobuf//:protoc", + use_grpc_plugin = False, **kargs): - """Bazel rule to create a Python protobuf library from proto source files - - NOTE: the rule is only an internal workaround to generate protos. The - interface may change and the rule may be removed when bazel has introduced - the native rule. - - Args: - name: the name of the py_proto_library. - srcs: the .proto files of the py_proto_library. - deps: a list of dependency labels; must be py_proto_library. - py_libs: a list of other py_library targets depended by the generated - py_library. - py_extra_srcs: extra source files that will be added to the output - py_library. This attribute is used for internal bootstrapping. - include: a string indicating the include path of the .proto files. - default_runtime: the implicitly default runtime which will be depended on by - the generated py_library target. - protoc: the label of the protocol compiler to generate the sources. - use_grpc_plugin: a flag to indicate whether to call the Python C++ plugin - when processing the proto files. - **kargs: other keyword arguments that are passed to cc_library. - - """ - outs = _PyOuts(srcs, use_grpc_plugin) - - includes = [] - if include != None: - includes = [include] - - grpc_python_plugin = None - if use_grpc_plugin: - grpc_python_plugin = "//external:grpc_python_plugin" - # Note: Generated grpc code depends on Python grpc module. This dependency - # is not explicitly listed in py_libs. Instead, host system is assumed to - # have grpc installed. - - proto_gen( - name=name + "_genproto", - srcs=srcs, - deps=[s + "_genproto" for s in deps], - includes=includes, - protoc=protoc, - gen_py=1, - outs=outs, - visibility=["//visibility:public"], - plugin=grpc_python_plugin, - plugin_language="grpc" - ) - - if default_runtime and not default_runtime in py_libs + deps: - py_libs = py_libs + [default_runtime] - - native.py_library( - name=name, - srcs=outs+py_extra_srcs, - deps=py_libs+deps, - imports=includes, - **kargs) + """Bazel rule to create a Python protobuf library from proto source files + + NOTE: the rule is only an internal workaround to generate protos. The + interface may change and the rule may be removed when bazel has introduced + the native rule. + + Args: + name: the name of the py_proto_library. + srcs: the .proto files of the py_proto_library. + deps: a list of dependency labels; must be py_proto_library. + py_libs: a list of other py_library targets depended by the generated + py_library. + py_extra_srcs: extra source files that will be added to the output + py_library. This attribute is used for internal bootstrapping. + include: a string indicating the include path of the .proto files. + default_runtime: the implicitly default runtime which will be depended on by + the generated py_library target. + protoc: the label of the protocol compiler to generate the sources. + use_grpc_plugin: a flag to indicate whether to call the Python C++ plugin + when processing the proto files. + **kargs: other keyword arguments that are passed to cc_library. + + """ + outs = _PyOuts(srcs, use_grpc_plugin) + + includes = [] + if include != None: + includes = [include] + + grpc_python_plugin = None + if use_grpc_plugin: + grpc_python_plugin = "//external:grpc_python_plugin" + # Note: Generated grpc code depends on Python grpc module. This dependency + # is not explicitly listed in py_libs. Instead, host system is assumed to + # have grpc installed. -def internal_protobuf_py_tests( - name, - modules=[], - **kargs): - """Bazel rules to create batch tests for protobuf internal. - - Args: - name: the name of the rule. - modules: a list of modules for tests. The macro will create a py_test for - each of the parameter with the source "google/protobuf/%s.py" - kargs: extra parameters that will be passed into the py_test. - - """ - for m in modules: - s = "python/google/protobuf/internal/%s.py" % m - native.py_test( - name="py_%s" % m, - srcs=[s], - main=s, - **kargs) + proto_gen( + name = name + "_genproto", + srcs = srcs, + deps = [s + "_genproto" for s in deps], + includes = includes, + protoc = protoc, + gen_py = 1, + outs = outs, + visibility = ["//visibility:public"], + plugin = grpc_python_plugin, + plugin_language = "grpc", + ) + + if default_runtime and not default_runtime in py_libs + deps: + py_libs = py_libs + [default_runtime] + + native.py_library( + name = name, + srcs = outs + py_extra_srcs, + deps = py_libs + deps, + imports = includes, + **kargs + ) +def internal_protobuf_py_tests( + name, + modules = [], + **kargs): + """Bazel rules to create batch tests for protobuf internal. + + Args: + name: the name of the rule. + modules: a list of modules for tests. The macro will create a py_test for + each of the parameter with the source "google/protobuf/%s.py" + kargs: extra parameters that will be passed into the py_test. + + """ + for m in modules: + s = "python/google/protobuf/internal/%s.py" % m + native.py_test( + name = "py_%s" % m, + srcs = [s], + main = s, + **kargs + ) def check_protobuf_required_bazel_version(): - """For WORKSPACE files, to check the installed version of bazel. + """For WORKSPACE files, to check the installed version of bazel. - This ensures bazel supports our approach to proto_library() depending on a - copied filegroup. (Fixed in bazel 0.5.4) - """ - versions.check(minimum_bazel_version = "0.5.4") + This ensures bazel supports our approach to proto_library() depending on a + copied filegroup. (Fixed in bazel 0.5.4) + """ + versions.check(minimum_bazel_version = "0.5.4") diff --git a/protobuf.pc.in b/protobuf.pc.in index 490149034d..055a9d0563 100644 --- a/protobuf.pc.in +++ b/protobuf.pc.in @@ -6,7 +6,8 @@ includedir=@includedir@ Name: Protocol Buffers Description: Google's Data Interchange Format Version: @VERSION@ -Libs: -L${libdir} -lprotobuf @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ +Libs: -L${libdir} -lprotobuf @PTHREAD_LIBS@ Libs.private: @LIBS@ + Cflags: -I${includedir} @PTHREAD_CFLAGS@ Conflicts: protobuf-lite diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl new file mode 100644 index 0000000000..25fad72537 --- /dev/null +++ b/protobuf_deps.bzl @@ -0,0 +1,15 @@ +"""Load dependencies needed to compile the protobuf library as a 3rd-party consumer.""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def protobuf_deps(): + """Loads common dependencies needed to compile the protobuf library.""" + + if "zlib" not in native.existing_rules(): + http_archive( + name = "zlib", + build_file = "@com_google_protobuf//:third_party/zlib.BUILD", + sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1", + strip_prefix = "zlib-1.2.11", + urls = ["https://zlib.net/zlib-1.2.11.tar.gz"], + ) diff --git a/protoc-artifacts/Dockerfile b/protoc-artifacts/Dockerfile index 95086eae7e..278bc7452d 100644 --- a/protoc-artifacts/Dockerfile +++ b/protoc-artifacts/Dockerfile @@ -33,9 +33,20 @@ RUN wget http://people.centos.org/tru/devtools-2/devtools-2.repo -P /etc/yum.rep bash -c 'echo "enabled=1" >> /etc/yum.repos.d/devtools-2.repo' && \ bash -c "sed -e 's/\$basearch/i386/g' /etc/yum.repos.d/devtools-2.repo > /etc/yum.repos.d/devtools-i386-2.repo" && \ sed -e 's/testing-/testing-i386-/g' -i /etc/yum.repos.d/devtools-i386-2.repo && \ - rpm --rebuilddb && \ - yum install -y devtoolset-2-gcc devtoolset-2-gcc-c++ devtoolset-2-binutils devtoolset-2-libstdc++-devel \ - devtoolset-2-gcc.i686 devtoolset-2-gcc-c++.i686 devtoolset-2-binutils.i686 devtoolset-2-libstdc++-devel.i686 && \ + rpm --rebuilddb + +RUN yum install -y devtoolset-2-gcc \ + devtoolset-2-gcc-c++ \ + devtoolset-2-binutils \ + devtoolset-2-libstdc++-devel \ + devtoolset-2-libatomic-devel \ + libatomic \ + devtoolset-2-gcc.i686 \ + devtoolset-2-gcc-c++.i686 \ + devtoolset-2-binutils.i686 \ + devtoolset-2-libstdc++-devel.i686 \ + devtoolset-2-libatomic-devel.i686 \ + libatomic.i686 && \ yum clean all COPY scl-enable-devtoolset.sh /var/local/ diff --git a/protoc-artifacts/README.md b/protoc-artifacts/README.md index 79fa30b8e9..88f183aff1 100644 --- a/protoc-artifacts/README.md +++ b/protoc-artifacts/README.md @@ -90,7 +90,7 @@ To checkout protobuf (run within the container): ``` $ # Replace v3.5.1 with the version you want -$ wget -O - https://github.com/google/protobuf/archive/v3.5.1.tar.gz | tar xvzp +$ wget -O - https://github.com/protocolbuffers/protobuf/archive/v3.5.1.tar.gz | tar xvzp ``` ### Windows build @@ -121,6 +121,8 @@ target directory layout: protoc.exe + aarch_64 protoc.exe + + ppcle_64 + protoc.exe + osx + x86_64 protoc.exe diff --git a/protoc-artifacts/build-protoc.sh b/protoc-artifacts/build-protoc.sh index b8d2eb8762..54b75a6d8e 100755 --- a/protoc-artifacts/build-protoc.sh +++ b/protoc-artifacts/build-protoc.sh @@ -95,8 +95,8 @@ checkArch () assertEq $format "elf64-little" $LINENO elif [[ "$ARCH" == ppcle_64 ]]; then if [[ $host_machine == ppc64le ]];then - assertEq $format "elf64-powerpcle" $LINENO - else + assertEq $format "elf64-powerpcle" $LINENO + else assertEq $format "elf64-little" $LINENO fi else @@ -144,7 +144,7 @@ checkDependencies () elif [[ "$ARCH" == x86_64 ]]; then white_list="linux-vdso\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-x86-64\.so\.2" elif [[ "$ARCH" == ppcle_64 ]]; then - if [[ $host_machine != ppc64le ]];then + if [[ $host_machine != ppc64le ]];then dump_cmd='objdump -p '"$1"' | grep NEEDED' fi white_list="linux-vdso64\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|libz\.so\.1\|ld64\.so\.2" diff --git a/protoc-artifacts/build-zip.sh b/protoc-artifacts/build-zip.sh index 26b6cc930e..fa26f78bfe 100755 --- a/protoc-artifacts/build-zip.sh +++ b/protoc-artifacts/build-zip.sh @@ -13,8 +13,9 @@ Example: This script will download pre-built protoc or protoc plugin binaries from maven repository and create .zip packages suitable to be included in the github release page. If the target is protoc, well-known type .proto files will also be -included. Each invocation will create 7 zip packages: +included. Each invocation will create 8 zip packages: dist/--win32.zip + dist/--win64.zip dist/--osx-x86_32.zip dist/--osx-x86_64.zip dist/--linux-x86_32.zip @@ -31,6 +32,7 @@ VERSION_NUMBER=$2 # pairs. declare -a FILE_NAMES=( \ win32.zip windows-x86_32.exe \ + win64.zip windows-x86_64.exe \ osx-x86_32.zip osx-x86_32.exe \ osx-x86_64.zip osx-x86_64.exe \ linux-x86_32.zip linux-x86_32.exe \ @@ -82,7 +84,7 @@ copy the contents of the 'include' directory somewhere as well, for example into '/usr/local/include/'. Please refer to our official github site for more installation instructions: - https://github.com/google/protobuf + https://github.com/protocolbuffers/protobuf EOF mkdir -p dist diff --git a/protoc-artifacts/pom.xml b/protoc-artifacts/pom.xml index 55d9e91aaf..bff6f53e76 100644 --- a/protoc-artifacts/pom.xml +++ b/protoc-artifacts/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 com.google @@ -10,7 +8,7 @@ com.google.protobuf protoc - 3.6.1 + 3.9.0-rc-1 pom Protobuf Compiler @@ -27,9 +25,9 @@ - https://github.com/google/protobuf + https://github.com/protocolbuffers/protobuf - scm:git:https://github.com/google/protobuf.git + scm:git:https://github.com/protocolbuffers/protobuf.git @@ -82,6 +80,11 @@ linux-aarch_64 exe + + ${basedir}/target/linux/ppcle_64/protoc.exe + linux-ppcle_64 + exe + @@ -98,7 +101,7 @@ subsequent deployments it should be set to what Sonatype has created, so that all deployments will go to the same repository. --> - + diff --git a/python/README.md b/python/README.md index 3eb78e5629..8f7e031314 100644 --- a/python/README.md +++ b/python/README.md @@ -1,7 +1,7 @@ Protocol Buffers - Google's data interchange format =================================================== -[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python_compatibility.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython_compatibility%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython_cpp%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython_cpp%2Fcontinuous) +[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python_compatibility.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython_compatibility%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-python_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fpython_cpp%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-python_cpp.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fpython_cpp%2Fcontinuous) [![Compat check PyPI](https://python-compatibility-tools.appspot.com/one_badge_image?package=protobuf)](https://python-compatibility-tools.appspot.com/one_badge_target?package=protobuf) Copyright 2008 Google Inc. diff --git a/python/compatibility_tests/v2.5.0/setup.py b/python/compatibility_tests/v2.5.0/setup.py index b41d54d4fe..bf67536733 100755 --- a/python/compatibility_tests/v2.5.0/setup.py +++ b/python/compatibility_tests/v2.5.0/setup.py @@ -59,7 +59,7 @@ if __name__ == '__main__': setup( name='protobuf', description='Protocol Buffers', - download_url='https://github.com/google/protobuf/releases', + download_url='https://github.com/protocolbuffers/protobuf/releases', long_description="Protocol Buffers are Google's data interchange format", url='https://developers.google.com/protocol-buffers/', maintainer='protobuf@googlegroups.com', diff --git a/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/service_reflection_test.py b/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/service_reflection_test.py index e04f8252c0..2f0708d83a 100755 --- a/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/service_reflection_test.py +++ b/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/service_reflection_test.py @@ -80,7 +80,7 @@ class FooUnitTest(unittest.TestCase): self.assertEqual('Method Bar not implemented.', rpc_controller.failure_message) self.assertEqual(None, self.callback_response) - + class MyServiceImpl(unittest_pb2.TestService): def Foo(self, rpc_controller, request, done): self.foo_called = True diff --git a/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/text_format_test.py b/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/text_format_test.py index bc53e256e2..1e6f063cd2 100755 --- a/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/text_format_test.py +++ b/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/text_format_test.py @@ -248,7 +248,7 @@ class TextFormatTest(unittest.TestCase): .replace('e-0','e-').replace('e-0','e-') # Floating point fields are printed with .0 suffix even if they are # actualy integer numbers. - text = re.compile('\.0$', re.MULTILINE).sub('', text) + text = re.compile(r'\.0$', re.MULTILINE).sub('', text) return text def testMergeGolden(self): diff --git a/python/google/protobuf/__init__.py b/python/google/protobuf/__init__.py index 249e18aa68..c35f84d548 100755 --- a/python/google/protobuf/__init__.py +++ b/python/google/protobuf/__init__.py @@ -30,7 +30,7 @@ # Copyright 2007 Google Inc. All Rights Reserved. -__version__ = '3.6.1' +__version__ = '3.9.0rc1' if __name__ != '__main__': try: diff --git a/python/google/protobuf/descriptor.py b/python/google/protobuf/descriptor.py index 8a9ba3da48..2c2a0796b2 100755 --- a/python/google/protobuf/descriptor.py +++ b/python/google/protobuf/descriptor.py @@ -657,7 +657,8 @@ class EnumDescriptor(_NestedDescriptorBase): for value in self.values: value.type = self self.values_by_name = dict((v.name, v) for v in values) - self.values_by_number = dict((v.number, v) for v in values) + # Values are reversed to ensure that the first alias is retained. + self.values_by_number = dict((v.number, v) for v in reversed(values)) def CopyToProto(self, proto): """Copies this to a descriptor_pb2.EnumDescriptorProto. @@ -855,7 +856,7 @@ class FileDescriptor(DescriptorBase): dependencies: List of other FileDescriptors this FileDescriptor depends on. public_dependencies: A list of FileDescriptors, subset of the dependencies above, which were declared as "public". - message_types_by_name: Dict of message names of their descriptors. + message_types_by_name: Dict of message names and their descriptors. enum_types_by_name: Dict of enum names and their descriptors. extensions_by_name: Dict of extension names and their descriptors. services_by_name: Dict of services names and their descriptors. diff --git a/python/google/protobuf/descriptor_database.py b/python/google/protobuf/descriptor_database.py index a7616cbc88..5453f50c80 100644 --- a/python/google/protobuf/descriptor_database.py +++ b/python/google/protobuf/descriptor_database.py @@ -134,7 +134,11 @@ class DescriptorDatabase(object): # descriptor can also be found. The behavior is the same with # protobuf C++. top_level, _, _ = symbol.rpartition('.') - return self._file_desc_protos_by_symbol[top_level] + try: + return self._file_desc_protos_by_symbol[top_level] + except KeyError: + # Raise the original symbol as a KeyError for better diagnostics. + raise KeyError(symbol) def FindFileContainingExtension(self, extendee_name, extension_number): # TODO(jieluo): implement this API. diff --git a/python/google/protobuf/descriptor_pool.py b/python/google/protobuf/descriptor_pool.py index 42f7bcb586..f41dc522c6 100644 --- a/python/google/protobuf/descriptor_pool.py +++ b/python/google/protobuf/descriptor_pool.py @@ -161,15 +161,18 @@ class DescriptorPool(object): if not isinstance(desc, descriptor_type) or ( old_file != file_name): - warn_msg = ('Conflict register for file "' + file_name + - '": ' + desc_name + - ' is already defined in file "' + - old_file + '"') + error_msg = ('Conflict register for file "' + file_name + + '": ' + desc_name + + ' is already defined in file "' + + old_file + '". Please fix the conflict by adding ' + 'package name on the proto file, or use different ' + 'name for the duplication.') if isinstance(desc, descriptor.EnumValueDescriptor): - warn_msg += ('\nNote: enum values appear as ' - 'siblings of the enum type instead of ' - 'children of it.') - warnings.warn(warn_msg, RuntimeWarning) + error_msg += ('\nNote: enum values appear as ' + 'siblings of the enum type instead of ' + 'children of it.') + + raise TypeError(error_msg) return diff --git a/python/google/protobuf/internal/_parameterized.py b/python/google/protobuf/internal/_parameterized.py index f2c0b305b9..38f76c5ae2 100755 --- a/python/google/protobuf/internal/_parameterized.py +++ b/python/google/protobuf/internal/_parameterized.py @@ -145,7 +145,6 @@ be wrapped into a tuple: __author__ = 'tmarek@google.com (Torsten Marek)' -import collections import functools import re import types @@ -157,6 +156,13 @@ import uuid import six +try: + # Since python 3 + import collections.abc as collections_abc +except ImportError: + # Won't work after python 3.8 + import collections as collections_abc + ADDR_RE = re.compile(r'\<([a-zA-Z0-9_\-\.]+) object at 0x[a-fA-F0-9]+\>') _SEPARATOR = uuid.uuid1().hex _FIRST_ARG = object() @@ -174,12 +180,12 @@ def _StrClass(cls): def _NonStringIterable(obj): - return (isinstance(obj, collections.Iterable) and not + return (isinstance(obj, collections_abc.Iterable) and not isinstance(obj, six.string_types)) def _FormatParameterList(testcase_params): - if isinstance(testcase_params, collections.Mapping): + if isinstance(testcase_params, collections_abc.Mapping): return ', '.join('%s=%s' % (argname, _CleanRepr(value)) for argname, value in testcase_params.items()) elif _NonStringIterable(testcase_params): @@ -222,7 +228,7 @@ class _ParameterizedTestIter(object): def MakeBoundParamTest(testcase_params): @functools.wraps(test_method) def BoundParamTest(self): - if isinstance(testcase_params, collections.Mapping): + if isinstance(testcase_params, collections_abc.Mapping): test_method(self, **testcase_params) elif _NonStringIterable(testcase_params): test_method(self, *testcase_params) @@ -263,7 +269,7 @@ def _ModifyClass(class_object, testcases, naming_type): 'Cannot add parameters to %s,' ' which already has parameterized methods.' % (class_object,)) class_object._id_suffix = id_suffix = {} - # We change the size of __dict__ while we iterate over it, + # We change the size of __dict__ while we iterate over it, # which Python 3.x will complain about, so use copy(). for name, obj in class_object.__dict__.copy().items(): if (name.startswith(unittest.TestLoader.testMethodPrefix) @@ -291,7 +297,7 @@ def _ParameterDecorator(naming_type, testcases): if isinstance(obj, type): _ModifyClass( obj, - list(testcases) if not isinstance(testcases, collections.Sequence) + list(testcases) if not isinstance(testcases, collections_abc.Sequence) else testcases, naming_type) return obj diff --git a/python/google/protobuf/internal/api_implementation.cc b/python/google/protobuf/internal/api_implementation.cc index 6db12e8dc6..2a7f41d2d6 100644 --- a/python/google/protobuf/internal/api_implementation.cc +++ b/python/google/protobuf/internal/api_implementation.cc @@ -69,25 +69,24 @@ static const char* kImplVersionName = "api_version"; static const char* kModuleName = "_api_implementation"; static const char kModuleDocstring[] = -"_api_implementation is a module that exposes compile-time constants that\n" -"determine the default API implementation to use for Python proto2.\n" -"\n" -"It complements api_implementation.py by setting defaults using compile-time\n" -"constants defined in C, such that one can set defaults at compilation\n" -"(e.g. with blaze flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2)."; + "_api_implementation is a module that exposes compile-time constants that\n" + "determine the default API implementation to use for Python proto2.\n" + "\n" + "It complements api_implementation.py by setting defaults using " + "compile-time\n" + "constants defined in C, such that one can set defaults at compilation\n" + "(e.g. with blaze flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2)."; #if PY_MAJOR_VERSION >= 3 -static struct PyModuleDef _module = { - PyModuleDef_HEAD_INIT, - kModuleName, - kModuleDocstring, - -1, - NULL, - NULL, - NULL, - NULL, - NULL -}; +static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT, + kModuleName, + kModuleDocstring, + -1, + NULL, + NULL, + NULL, + NULL, + NULL}; #define INITFUNC PyInit__api_implementation #define INITFUNC_ERRORVAL NULL #else @@ -96,32 +95,31 @@ static struct PyModuleDef _module = { #endif extern "C" { - PyMODINIT_FUNC INITFUNC() { +PyMODINIT_FUNC INITFUNC() { #if PY_MAJOR_VERSION >= 3 - PyObject *module = PyModule_Create(&_module); + PyObject* module = PyModule_Create(&_module); #else - PyObject *module = Py_InitModule3( - const_cast(kModuleName), - NULL, - const_cast(kModuleDocstring)); + PyObject* module = Py_InitModule3(const_cast(kModuleName), NULL, + const_cast(kModuleDocstring)); #endif - if (module == NULL) { - return INITFUNC_ERRORVAL; - } + if (module == NULL) { + return INITFUNC_ERRORVAL; + } - // Adds the module variable "api_version". - if (PyModule_AddIntConstant( - module, - const_cast(kImplVersionName), - kImplVersion)) + // Adds the module variable "api_version". + if (PyModule_AddIntConstant(module, const_cast(kImplVersionName), + kImplVersion)) #if PY_MAJOR_VERSION < 3 - return; + return; #else - { Py_DECREF(module); return NULL; } + { + Py_DECREF(module); + return NULL; + } - return module; + return module; #endif - } +} } } // namespace python diff --git a/python/google/protobuf/internal/api_implementation.py b/python/google/protobuf/internal/api_implementation.py index 23cc2c0ab3..e8448e88a6 100755 --- a/python/google/protobuf/internal/api_implementation.py +++ b/python/google/protobuf/internal/api_implementation.py @@ -137,6 +137,12 @@ def Type(): return _implementation_type +def _SetType(implementation_type): + """Never use! Only for protobuf benchmark.""" + global _implementation_type + _implementation_type = implementation_type + + # See comment on 'Type' above. def Version(): return _implementation_version diff --git a/python/google/protobuf/internal/containers.py b/python/google/protobuf/internal/containers.py index 182cac996e..9b968e7bfc 100755 --- a/python/google/protobuf/internal/containers.py +++ b/python/google/protobuf/internal/containers.py @@ -41,12 +41,16 @@ are: __author__ = 'petar@google.com (Petar Petrov)' -import collections import sys +try: + # This fallback applies for all versions of Python before 3.3 + import collections.abc as collections_abc +except ImportError: + import collections as collections_abc if sys.version_info[0] < 3: - # We would use collections.MutableMapping all the time, but in Python 2 it - # doesn't define __slots__. This causes two significant problems: + # We would use collections_abc.MutableMapping all the time, but in Python 2 + # it doesn't define __slots__. This causes two significant problems: # # 1. we can't disallow arbitrary attribute assignment, even if our derived # classes *do* define __slots__. @@ -59,7 +63,7 @@ if sys.version_info[0] < 3: # verbatim, except that: # 1. We declare __slots__. # 2. We don't declare this as a virtual base class. The classes defined - # in collections are the interesting base classes, not us. + # in collections_abc are the interesting base classes, not us. # # Note: deriving from object is critical. It is the only thing that makes # this a true type, allowing us to derive from it in C++ cleanly and making @@ -106,7 +110,7 @@ if sys.version_info[0] < 3: __hash__ = None def __eq__(self, other): - if not isinstance(other, collections.Mapping): + if not isinstance(other, collections_abc.Mapping): return NotImplemented return dict(self.items()) == dict(other.items()) @@ -173,13 +177,13 @@ if sys.version_info[0] < 3: self[key] = default return default - collections.Mapping.register(Mapping) - collections.MutableMapping.register(MutableMapping) + collections_abc.Mapping.register(Mapping) + collections_abc.MutableMapping.register(MutableMapping) else: # In Python 3 we can just use MutableMapping directly, because it defines # __slots__. - MutableMapping = collections.MutableMapping + MutableMapping = collections_abc.MutableMapping class BaseContainer(object): @@ -226,6 +230,8 @@ class BaseContainer(object): kwargs['cmp'] = kwargs.pop('sort_function') self._values.sort(*args, **kwargs) +collections_abc.MutableSequence.register(BaseContainer) + class RepeatedScalarFieldContainer(BaseContainer): @@ -337,8 +343,6 @@ class RepeatedScalarFieldContainer(BaseContainer): # We are presumably comparing against some other sequence type. return other == self._values -collections.MutableSequence.register(BaseContainer) - class RepeatedCompositeFieldContainer(BaseContainer): @@ -376,6 +380,24 @@ class RepeatedCompositeFieldContainer(BaseContainer): self._message_listener.Modified() return new_element + def append(self, value): + """Appends one element by copying the message.""" + new_element = self._message_descriptor._concrete_class() + new_element._SetListener(self._message_listener) + new_element.CopyFrom(value) + self._values.append(new_element) + if not self._message_listener.dirty: + self._message_listener.Modified() + + def insert(self, key, value): + """Inserts the item at the specified position by copying.""" + new_element = self._message_descriptor._concrete_class() + new_element._SetListener(self._message_listener) + new_element.CopyFrom(value) + self._values.insert(key, new_element) + if not self._message_listener.dirty: + self._message_listener.Modified() + def extend(self, elem_seq): """Extends by appending the given sequence of elements of the same type as this one, copying each individual message. diff --git a/python/google/protobuf/internal/decoder.py b/python/google/protobuf/internal/decoder.py index 938f6293db..845d774274 100755 --- a/python/google/protobuf/internal/decoder.py +++ b/python/google/protobuf/internal/decoder.py @@ -81,9 +81,10 @@ we repeatedly read a tag, look up the corresponding decoder, and invoke it. __author__ = 'kenton@google.com (Kenton Varda)' import struct - +import sys import six +_UCS2_MAXUNICODE = 65535 if six.PY3: long = int else: @@ -550,7 +551,8 @@ def StringDecoder(field_number, is_repeated, is_packed, key, new_default, e.reason = '%s in field: %s' % (e, key.full_name) raise - if is_strict_utf8 and six.PY2: + if is_strict_utf8 and six.PY2 and sys.maxunicode > _UCS2_MAXUNICODE: + # Only do the check for python2 ucs4 when is_strict_utf8 enabled if _SURROGATE_PATTERN.search(value): reason = ('String field %s contains invalid UTF-8 data when parsing' 'a protocol buffer: surrogates not allowed. Use' @@ -912,11 +914,11 @@ def _SkipGroup(buffer, pos, end): pos = new_pos -def _DecodeGroup(buffer, pos): - """Decode group. Returns the UnknownFieldSet and new position.""" +def _DecodeUnknownFieldSet(buffer, pos, end_pos=None): + """Decode UnknownFieldSet. Returns the UnknownFieldSet and new position.""" unknown_field_set = containers.UnknownFieldSet() - while 1: + while end_pos is None or pos < end_pos: (tag_bytes, pos) = ReadTag(buffer, pos) (tag, _) = _DecodeVarint(tag_bytes, 0) field_number, wire_type = wire_format.UnpackTag(tag) @@ -943,7 +945,7 @@ def _DecodeUnknownField(buffer, pos, wire_type): data = buffer[pos:pos+size] pos += size elif wire_type == wire_format.WIRETYPE_START_GROUP: - (data, pos) = _DecodeGroup(buffer, pos) + (data, pos) = _DecodeUnknownFieldSet(buffer, pos) elif wire_type == wire_format.WIRETYPE_END_GROUP: return (0, -1) else: diff --git a/python/google/protobuf/internal/descriptor_database_test.py b/python/google/protobuf/internal/descriptor_database_test.py index 97e5315a42..619779a5dd 100644 --- a/python/google/protobuf/internal/descriptor_database_test.py +++ b/python/google/protobuf/internal/descriptor_database_test.py @@ -44,9 +44,11 @@ from google.protobuf import unittest_pb2 from google.protobuf import descriptor_pb2 from google.protobuf.internal import factory_test2_pb2 from google.protobuf.internal import no_package_pb2 +from google.protobuf.internal import testing_refleaks from google.protobuf import descriptor_database +@testing_refleaks.TestCase class DescriptorDatabaseTest(unittest.TestCase): def testAdd(self): @@ -103,9 +105,8 @@ class DescriptorDatabaseTest(unittest.TestCase): self.assertEqual(file_desc_proto2, db.FindFileContainingSymbol( 'protobuf_unittest.TestAllTypes.none_field')) - self.assertRaises(KeyError, - db.FindFileContainingSymbol, - 'protobuf_unittest.NoneMessage') + with self.assertRaisesRegexp(KeyError, r'\'protobuf_unittest\.NoneMessage\''): + db.FindFileContainingSymbol('protobuf_unittest.NoneMessage') def testConflictRegister(self): db = descriptor_database.DescriptorDatabase() @@ -114,19 +115,18 @@ class DescriptorDatabaseTest(unittest.TestCase): db.Add(unittest_fd) conflict_fd = descriptor_pb2.FileDescriptorProto.FromString( unittest_pb2.DESCRIPTOR.serialized_pb) - conflict_fd.name = 'other_file' + conflict_fd.name = 'other_file2' with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter('always') db.Add(conflict_fd) self.assertTrue(len(w)) self.assertIs(w[0].category, RuntimeWarning) - self.assertIn('Conflict register for file "other_file": ', + self.assertIn('Conflict register for file "other_file2": ', str(w[0].message)) self.assertIn('already defined in file ' '"google/protobuf/unittest.proto"', str(w[0].message)) - if __name__ == '__main__': unittest.main() diff --git a/python/google/protobuf/internal/descriptor_pool_test.py b/python/google/protobuf/internal/descriptor_pool_test.py index 1b72b0b9ee..ad1eb653fd 100644 --- a/python/google/protobuf/internal/descriptor_pool_test.py +++ b/python/google/protobuf/internal/descriptor_pool_test.py @@ -36,7 +36,6 @@ __author__ = 'matthewtoia@google.com (Matt Toia)' import copy import os -import warnings try: import unittest2 as unittest #PY26 @@ -55,6 +54,7 @@ from google.protobuf.internal import factory_test2_pb2 from google.protobuf.internal import file_options_test_pb2 from google.protobuf.internal import more_messages_pb2 from google.protobuf.internal import no_package_pb2 +from google.protobuf.internal import testing_refleaks from google.protobuf import descriptor from google.protobuf import descriptor_database from google.protobuf import descriptor_pool @@ -529,41 +529,26 @@ class DescriptorPoolTestBase(object): conflict_fd = copy.deepcopy(unittest_fd) conflict_fd.name = 'other_file' if api_implementation.Type() == 'cpp': - try: - self.pool.Add(unittest_fd) - self.pool.Add(conflict_fd) - except TypeError: pass else: - with warnings.catch_warnings(record=True) as w: - # Cause all warnings to always be triggered. - warnings.simplefilter('always') - pool = copy.deepcopy(self.pool) - # No warnings to add the same descriptors. - file_descriptor = unittest_pb2.DESCRIPTOR - pool.AddDescriptor( - file_descriptor.message_types_by_name['TestAllTypes']) - pool.AddEnumDescriptor( - file_descriptor.enum_types_by_name['ForeignEnum']) - pool.AddServiceDescriptor( - file_descriptor.services_by_name['TestService']) - pool.AddExtensionDescriptor( - file_descriptor.extensions_by_name['optional_int32_extension']) - self.assertEqual(len(w), 0) - # Check warnings for conflict descriptors with the same name. - pool.Add(unittest_fd) - pool.Add(conflict_fd) - pool.FindFileByName(unittest_fd.name) + pool = copy.deepcopy(self.pool) + file_descriptor = unittest_pb2.DESCRIPTOR + pool.AddDescriptor( + file_descriptor.message_types_by_name['TestAllTypes']) + pool.AddEnumDescriptor( + file_descriptor.enum_types_by_name['ForeignEnum']) + pool.AddServiceDescriptor( + file_descriptor.services_by_name['TestService']) + pool.AddExtensionDescriptor( + file_descriptor.extensions_by_name['optional_int32_extension']) + pool.Add(unittest_fd) + pool.Add(conflict_fd) + pool.FindFileByName(unittest_fd.name) + with self.assertRaises(TypeError): pool.FindFileByName(conflict_fd.name) - self.assertTrue(len(w)) - self.assertIs(w[0].category, RuntimeWarning) - self.assertIn('Conflict register for file "other_file": ', - str(w[0].message)) - self.assertIn('already defined in file ' - '"google/protobuf/unittest.proto"', - str(w[0].message)) +@testing_refleaks.TestCase class DefaultDescriptorPoolTest(DescriptorPoolTestBase, unittest.TestCase): def setUp(self): @@ -599,6 +584,7 @@ class DefaultDescriptorPoolTest(DescriptorPoolTestBase, unittest.TestCase): unittest_pb2.DESCRIPTOR.services_by_name['TestService']) +@testing_refleaks.TestCase class CreateDescriptorPoolTest(DescriptorPoolTestBase, unittest.TestCase): def setUp(self): @@ -620,6 +606,7 @@ class CreateDescriptorPoolTest(DescriptorPoolTestBase, unittest.TestCase): no_package_pb2.DESCRIPTOR.serialized_pb)) +@testing_refleaks.TestCase class SecondaryDescriptorFromDescriptorDB(DescriptorPoolTestBase, unittest.TestCase): @@ -628,18 +615,86 @@ class SecondaryDescriptorFromDescriptorDB(DescriptorPoolTestBase, factory_test1_pb2.DESCRIPTOR.serialized_pb) self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString( factory_test2_pb2.DESCRIPTOR.serialized_pb) - db = descriptor_database.DescriptorDatabase() - db.Add(self.factory_test1_fd) - db.Add(self.factory_test2_fd) - db.Add(descriptor_pb2.FileDescriptorProto.FromString( + self.db = descriptor_database.DescriptorDatabase() + self.db.Add(self.factory_test1_fd) + self.db.Add(self.factory_test2_fd) + self.db.Add(descriptor_pb2.FileDescriptorProto.FromString( unittest_import_public_pb2.DESCRIPTOR.serialized_pb)) - db.Add(descriptor_pb2.FileDescriptorProto.FromString( + self.db.Add(descriptor_pb2.FileDescriptorProto.FromString( unittest_import_pb2.DESCRIPTOR.serialized_pb)) - db.Add(descriptor_pb2.FileDescriptorProto.FromString( + self.db.Add(descriptor_pb2.FileDescriptorProto.FromString( unittest_pb2.DESCRIPTOR.serialized_pb)) - db.Add(descriptor_pb2.FileDescriptorProto.FromString( + self.db.Add(descriptor_pb2.FileDescriptorProto.FromString( no_package_pb2.DESCRIPTOR.serialized_pb)) - self.pool = descriptor_pool.DescriptorPool(descriptor_db=db) + self.pool = descriptor_pool.DescriptorPool(descriptor_db=self.db) + + def testErrorCollector(self): + file_proto = descriptor_pb2.FileDescriptorProto() + file_proto.package = 'collector' + file_proto.name = 'error_file' + message_type = file_proto.message_type.add() + message_type.name = 'ErrorMessage' + field = message_type.field.add() + field.number = 1 + field.name = 'nested_message_field' + field.label = descriptor.FieldDescriptor.LABEL_OPTIONAL + field.type = descriptor.FieldDescriptor.TYPE_MESSAGE + field.type_name = 'SubMessage' + oneof = message_type.oneof_decl.add() + oneof.name = 'MyOneof' + enum_type = file_proto.enum_type.add() + enum_type.name = 'MyEnum' + enum_value = enum_type.value.add() + enum_value.name = 'MyEnumValue' + enum_value.number = 0 + self.db.Add(file_proto) + + self.assertRaisesRegexp(KeyError, 'SubMessage', + self.pool.FindMessageTypeByName, + 'collector.ErrorMessage') + self.assertRaisesRegexp(KeyError, 'SubMessage', + self.pool.FindFileByName, 'error_file') + with self.assertRaises(KeyError) as exc: + self.pool.FindFileByName('none_file') + self.assertIn(str(exc.exception), ('\'none_file\'', + '\"Couldn\'t find file none_file\"')) + + # Pure python _ConvertFileProtoToFileDescriptor() method has side effect + # that all the symbols found in the file will load into the pool even the + # file can not build. So when FindMessageTypeByName('ErrorMessage') was + # called the first time, a KeyError will be raised but call the find + # method later will return a descriptor which is not build. + # TODO(jieluo): fix pure python to revert the load if file can not be build + if api_implementation.Type() == 'cpp': + error_msg = ('Invalid proto descriptor for file "error_file":\\n ' + 'collector.ErrorMessage.nested_message_field: "SubMessage" ' + 'is not defined.\\n collector.ErrorMessage.MyOneof: Oneof ' + 'must have at least one field.\\n\'') + with self.assertRaises(KeyError) as exc: + self.pool.FindMessageTypeByName('collector.ErrorMessage') + self.assertEqual(str(exc.exception), '\'Couldn\\\'t build file for ' + 'message collector.ErrorMessage\\n' + error_msg) + + with self.assertRaises(KeyError) as exc: + self.pool.FindFieldByName('collector.ErrorMessage.nested_message_field') + self.assertEqual(str(exc.exception), '\'Couldn\\\'t build file for field' + ' collector.ErrorMessage.nested_message_field\\n' + + error_msg) + + with self.assertRaises(KeyError) as exc: + self.pool.FindEnumTypeByName('collector.MyEnum') + self.assertEqual(str(exc.exception), '\'Couldn\\\'t build file for enum' + ' collector.MyEnum\\n' + error_msg) + + with self.assertRaises(KeyError) as exc: + self.pool.FindFileContainingSymbol('collector.MyEnumValue') + self.assertEqual(str(exc.exception), '\'Couldn\\\'t build file for symbol' + ' collector.MyEnumValue\\n' + error_msg) + + with self.assertRaises(KeyError) as exc: + self.pool.FindOneofByName('collector.ErrorMessage.MyOneof') + self.assertEqual(str(exc.exception), '\'Couldn\\\'t build file for oneof' + ' collector.ErrorMessage.MyOneof\\n' + error_msg) class ProtoFile(object): @@ -813,6 +868,7 @@ class ExtensionField(object): test.assertEqual(file_desc, field_desc.file) +@testing_refleaks.TestCase class AddDescriptorTest(unittest.TestCase): def _TestMessage(self, prefix): diff --git a/python/google/protobuf/internal/descriptor_test.py b/python/google/protobuf/internal/descriptor_test.py index af6bece15b..e753cbabd4 100755 --- a/python/google/protobuf/internal/descriptor_test.py +++ b/python/google/protobuf/internal/descriptor_test.py @@ -192,6 +192,14 @@ class DescriptorTest(unittest.TestCase): self.assertTrue(enum_value_descriptor.has_options) self.assertFalse(other_enum_value_descriptor.has_options) + def testCustomOptionsCopyTo(self): + message_descriptor = (unittest_custom_options_pb2. + TestMessageWithCustomOptions.DESCRIPTOR) + message_proto = descriptor_pb2.DescriptorProto() + message_descriptor.CopyToProto(message_proto) + self.assertEqual(len(message_proto.options.ListFields()), + 2) + def testDifferentCustomOptionTypes(self): kint32min = -2**31 kint64min = -2**63 diff --git a/python/google/protobuf/internal/enum_type_wrapper.py b/python/google/protobuf/internal/enum_type_wrapper.py index 1cffe35295..b4d30e2c94 100644 --- a/python/google/protobuf/internal/enum_type_wrapper.py +++ b/python/google/protobuf/internal/enum_type_wrapper.py @@ -46,7 +46,7 @@ class EnumTypeWrapper(object): def __init__(self, enum_type): """Inits EnumTypeWrapper with an EnumDescriptor.""" self._enum_type = enum_type - self.DESCRIPTOR = enum_type; + self.DESCRIPTOR = enum_type def Name(self, number): """Returns a string containing the name of an enum value.""" @@ -56,7 +56,7 @@ class EnumTypeWrapper(object): self._enum_type.name, number)) def Value(self, name): - """Returns the value coresponding to the given enum name.""" + """Returns the value corresponding to the given enum name.""" if name in self._enum_type.values_by_name: return self._enum_type.values_by_name[name].number raise ValueError('Enum %s has no value defined for name %s' % ( @@ -87,3 +87,9 @@ class EnumTypeWrapper(object): """ return [(value_descriptor.name, value_descriptor.number) for value_descriptor in self._enum_type.values] + + def __getattr__(self, name): + """Returns the value corresponding to the given enum name.""" + if name in self._enum_type.values_by_name: + return self._enum_type.values_by_name[name].number + raise AttributeError diff --git a/python/google/protobuf/internal/extension_dict.py b/python/google/protobuf/internal/extension_dict.py new file mode 100644 index 0000000000..a8ce013d0a --- /dev/null +++ b/python/google/protobuf/internal/extension_dict.py @@ -0,0 +1,206 @@ +# 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. + +"""Contains _ExtensionDict class to represent extensions. +""" + +from google.protobuf.internal import type_checkers +from google.protobuf.descriptor import FieldDescriptor + + +def _VerifyExtensionHandle(message, extension_handle): + """Verify that the given extension handle is valid.""" + + if not isinstance(extension_handle, FieldDescriptor): + raise KeyError('HasExtension() expects an extension handle, got: %s' % + extension_handle) + + if not extension_handle.is_extension: + raise KeyError('"%s" is not an extension.' % extension_handle.full_name) + + if not extension_handle.containing_type: + raise KeyError('"%s" is missing a containing_type.' + % extension_handle.full_name) + + if extension_handle.containing_type is not message.DESCRIPTOR: + raise KeyError('Extension "%s" extends message type "%s", but this ' + 'message is of type "%s".' % + (extension_handle.full_name, + extension_handle.containing_type.full_name, + message.DESCRIPTOR.full_name)) + + +# TODO(robinson): Unify error handling of "unknown extension" crap. +# TODO(robinson): Support iteritems()-style iteration over all +# extensions with the "has" bits turned on? +class _ExtensionDict(object): + + """Dict-like container for Extension fields on proto instances. + + Note that in all cases we expect extension handles to be + FieldDescriptors. + """ + + def __init__(self, extended_message): + """ + Args: + extended_message: Message instance for which we are the Extensions dict. + """ + self._extended_message = extended_message + + def __getitem__(self, extension_handle): + """Returns the current value of the given extension handle.""" + + _VerifyExtensionHandle(self._extended_message, extension_handle) + + result = self._extended_message._fields.get(extension_handle) + if result is not None: + return result + + if extension_handle.label == FieldDescriptor.LABEL_REPEATED: + result = extension_handle._default_constructor(self._extended_message) + elif extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE: + assert getattr(extension_handle.message_type, '_concrete_class', None), ( + 'Uninitialized concrete class found for field %r (message type %r)' + % (extension_handle.full_name, + extension_handle.message_type.full_name)) + result = extension_handle.message_type._concrete_class() + try: + result._SetListener(self._extended_message._listener_for_children) + except ReferenceError: + pass + else: + # Singular scalar -- just return the default without inserting into the + # dict. + return extension_handle.default_value + + # Atomically check if another thread has preempted us and, if not, swap + # in the new object we just created. If someone has preempted us, we + # take that object and discard ours. + # WARNING: We are relying on setdefault() being atomic. This is true + # in CPython but we haven't investigated others. This warning appears + # in several other locations in this file. + result = self._extended_message._fields.setdefault( + extension_handle, result) + + return result + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + + my_fields = self._extended_message.ListFields() + other_fields = other._extended_message.ListFields() + + # Get rid of non-extension fields. + my_fields = [field for field in my_fields if field.is_extension] + other_fields = [field for field in other_fields if field.is_extension] + + return my_fields == other_fields + + def __ne__(self, other): + return not self == other + + def __len__(self): + fields = self._extended_message.ListFields() + # Get rid of non-extension fields. + extension_fields = [field for field in fields if field[0].is_extension] + return len(extension_fields) + + def __hash__(self): + raise TypeError('unhashable object') + + # Note that this is only meaningful for non-repeated, scalar extension + # fields. Note also that we may have to call _Modified() when we do + # successfully set a field this way, to set any necssary "has" bits in the + # ancestors of the extended message. + def __setitem__(self, extension_handle, value): + """If extension_handle specifies a non-repeated, scalar extension + field, sets the value of that field. + """ + + _VerifyExtensionHandle(self._extended_message, extension_handle) + + if (extension_handle.label == FieldDescriptor.LABEL_REPEATED or + extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE): + raise TypeError( + 'Cannot assign to extension "%s" because it is a repeated or ' + 'composite type.' % extension_handle.full_name) + + # It's slightly wasteful to lookup the type checker each time, + # but we expect this to be a vanishingly uncommon case anyway. + type_checker = type_checkers.GetTypeChecker(extension_handle) + # pylint: disable=protected-access + self._extended_message._fields[extension_handle] = ( + type_checker.CheckValue(value)) + self._extended_message._Modified() + + def _FindExtensionByName(self, name): + """Tries to find a known extension with the specified name. + + Args: + name: Extension full name. + + Returns: + Extension field descriptor. + """ + return self._extended_message._extensions_by_name.get(name, None) + + def _FindExtensionByNumber(self, number): + """Tries to find a known extension with the field number. + + Args: + number: Extension field number. + + Returns: + Extension field descriptor. + """ + return self._extended_message._extensions_by_number.get(number, None) + + def __iter__(self): + # Return a generator over the populated extension fields + return (f[0] for f in self._extended_message.ListFields() + if f[0].is_extension) + + def __contains__(self, extension_handle): + _VerifyExtensionHandle(self._extended_message, extension_handle) + + if extension_handle not in self._extended_message._fields: + return False + + if extension_handle.label == FieldDescriptor.LABEL_REPEATED: + return bool(self._extended_message._fields.get(extension_handle)) + + if extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE: + value = self._extended_message._fields.get(extension_handle) + # pylint: disable=protected-access + return value is not None and value._is_present_in_parent + + return True diff --git a/python/google/protobuf/internal/generator_test.py b/python/google/protobuf/internal/generator_test.py index 7f13f9da3d..ab16d175d2 100755 --- a/python/google/protobuf/internal/generator_test.py +++ b/python/google/protobuf/internal/generator_test.py @@ -306,6 +306,16 @@ class GeneratorTest(unittest.TestCase): else: self.assertIsNone(field_desc.containing_oneof) + def testEnumWithDupValue(self): + self.assertEqual('FOO1', + unittest_pb2.TestEnumWithDupValue.Name(unittest_pb2.FOO1)) + self.assertEqual('FOO1', + unittest_pb2.TestEnumWithDupValue.Name(unittest_pb2.FOO2)) + self.assertEqual('BAR1', + unittest_pb2.TestEnumWithDupValue.Name(unittest_pb2.BAR1)) + self.assertEqual('BAR1', + unittest_pb2.TestEnumWithDupValue.Name(unittest_pb2.BAR2)) + class SymbolDatabaseRegistrationTest(unittest.TestCase): """Checks that messages, enums and files are correctly registered.""" diff --git a/python/google/protobuf/internal/json_format_test.py b/python/google/protobuf/internal/json_format_test.py index 7f64940654..939d4961e7 100644 --- a/python/google/protobuf/internal/json_format_test.py +++ b/python/google/protobuf/internal/json_format_test.py @@ -49,10 +49,12 @@ from google.protobuf import field_mask_pb2 from google.protobuf import struct_pb2 from google.protobuf import timestamp_pb2 from google.protobuf import wrappers_pb2 +from google.protobuf import any_test_pb2 from google.protobuf import unittest_mset_pb2 from google.protobuf import unittest_pb2 -from google.protobuf.internal import well_known_types +from google.protobuf import descriptor_pool from google.protobuf import json_format +from google.protobuf.util import json_format_pb2 from google.protobuf.util import json_format_proto3_pb2 @@ -200,6 +202,27 @@ class JsonFormatTest(JsonFormatBase): json_format.ParseDict(message_dict, parsed_message) self.assertEqual(message, parsed_message) + def testExtensionToDictAndBackWithScalar(self): + message = unittest_pb2.TestAllExtensions() + ext1 = unittest_pb2.TestNestedExtension.test + message.Extensions[ext1] = 'data' + message_dict = json_format.MessageToDict( + message + ) + parsed_message = unittest_pb2.TestAllExtensions() + json_format.ParseDict(message_dict, parsed_message) + self.assertEqual(message, parsed_message) + + def testJsonParseDictToAnyDoesNotAlterInput(self): + orig_dict = { + 'int32Value': 20, + '@type': 'type.googleapis.com/proto3.TestMessage' + } + copied_dict = json.loads(json.dumps(orig_dict)) + parsed_message = any_pb2.Any() + json_format.ParseDict(copied_dict, parsed_message) + self.assertEqual(copied_dict, orig_dict) + def testExtensionSerializationDictMatchesProto3Spec(self): """See go/proto3-json-spec for spec. """ @@ -225,6 +248,22 @@ class JsonFormatTest(JsonFormatBase): } self.assertEqual(golden_dict, message_dict) + def testExtensionSerializationDictMatchesProto3SpecMore(self): + """See go/proto3-json-spec for spec. + """ + message = json_format_pb2.TestMessageWithExtension() + ext = json_format_pb2.TestExtension.ext + message.Extensions[ext].value = 'stuff' + message_dict = json_format.MessageToDict( + message + ) + expected_dict = { + '[protobuf_unittest.TestExtension.ext]': { + 'value': u'stuff', + }, + } + self.assertEqual(expected_dict, message_dict) + def testExtensionSerializationJsonMatchesProto3Spec(self): """See go/proto3-json-spec for spec. @@ -458,6 +497,14 @@ class JsonFormatTest(JsonFormatBase): parsed_message = json_format_proto3_pb2.TestFieldMask() self.CheckParseBack(message, parsed_message) + message.value.Clear() + self.assertEqual( + json_format.MessageToJson(message, True), + '{\n' + ' "value": ""\n' + '}') + self.CheckParseBack(message, parsed_message) + def testWrapperMessage(self): message = json_format_proto3_pb2.TestWrapper() message.bool_value.value = False @@ -495,6 +542,8 @@ class JsonFormatTest(JsonFormatBase): message.value['email'] = None message.value.get_or_create_struct('address')['city'] = 'SFO' message.value['address']['house_number'] = 1024 + message.value.get_or_create_struct('empty_struct') + message.value.get_or_create_list('empty_list') struct_list = message.value.get_or_create_list('list') struct_list.extend([6, 'seven', True, False, None]) struct_list.add_struct()['subkey2'] = 9 @@ -509,6 +558,8 @@ class JsonFormatTest(JsonFormatBase): ' "city": "SFO", ' ' "house_number": 1024' ' }, ' + ' "empty_struct": {}, ' + ' "empty_list": [], ' ' "age": 10, ' ' "name": "Jim", ' ' "attend": true, ' @@ -519,6 +570,9 @@ class JsonFormatTest(JsonFormatBase): '}')) parsed_message = json_format_proto3_pb2.TestStruct() self.CheckParseBack(message, parsed_message) + # check for regression; this used to raise + parsed_message.value['empty_struct'] + parsed_message.value['empty_list'] def testValueMessage(self): message = json_format_proto3_pb2.TestValue() @@ -611,6 +665,19 @@ class JsonFormatTest(JsonFormatBase): ' "value": {\n' ' "@type": "type.googleapis.com/proto3.TestMessage"') + def testAnyMessageDescriptorPoolMissingType(self): + packed_message = unittest_pb2.OneString() + packed_message.data = 'string' + message = any_test_pb2.TestAny() + message.any_value.Pack(packed_message) + empty_pool = descriptor_pool.DescriptorPool() + with self.assertRaises(TypeError) as cm: + json_format.MessageToJson(message, True, descriptor_pool=empty_pool) + self.assertEqual( + 'Can not find message descriptor by type_url:' + ' type.googleapis.com/protobuf_unittest.OneString.', + str(cm.exception)) + def testWellKnownInAnyMessage(self): message = any_pb2.Any() int32_value = wrappers_pb2.Int32Value() @@ -744,6 +811,13 @@ class JsonFormatTest(JsonFormatBase): json_format.Parse(text, parsed_message) self.assertTrue(math.isnan(parsed_message.float_value)) + def testParseDoubleToFloat(self): + message = json_format_proto3_pb2.TestMessage() + text = ('{"repeatedFloatValue": [3.4028235e+39, 1.4028235e-39]\n}') + json_format.Parse(text, message) + self.assertEqual(message.repeated_float_value[0], float('inf')) + self.assertAlmostEqual(message.repeated_float_value[1], 1.4028235e-39) + def testParseEmptyText(self): self.CheckError('', r'Failed to load JSON: (Expecting value)|(No JSON).') @@ -879,17 +953,18 @@ class JsonFormatTest(JsonFormatBase): text = '{"value": "10000-01-01T00:00:00.00Z"}' self.assertRaisesRegexp( json_format.ParseError, + 'Failed to parse value field: ' 'time data \'10000-01-01T00:00:00\' does not match' ' format \'%Y-%m-%dT%H:%M:%S\'.', json_format.Parse, text, message) text = '{"value": "1970-01-01T00:00:00.0123456789012Z"}' self.assertRaisesRegexp( - well_known_types.ParseError, + json_format.ParseError, 'nanos 0123456789012 more than 9 fractional digits.', json_format.Parse, text, message) text = '{"value": "1972-01-01T01:00:00.01+08"}' self.assertRaisesRegexp( - well_known_types.ParseError, + json_format.ParseError, (r'Invalid timezone offset value: \+08.'), json_format.Parse, text, message) # Time smaller than minimum time. @@ -999,6 +1074,32 @@ class JsonFormatTest(JsonFormatBase): json_format.ParseDict(js_dict, message) self.assertEqual(expected, message.int32_value) + def testParseDictAnyDescriptorPoolMissingType(self): + # Confirm that ParseDict does not raise ParseError with default pool + js_dict = { + 'any_value': { + '@type': 'type.googleapis.com/proto3.MessageType', + 'value': 1234 + } + } + json_format.ParseDict(js_dict, any_test_pb2.TestAny()) + # Check ParseDict raises ParseError with empty pool + js_dict = { + 'any_value': { + '@type': 'type.googleapis.com/proto3.MessageType', + 'value': 1234 + } + } + with self.assertRaises(json_format.ParseError) as cm: + empty_pool = descriptor_pool.DescriptorPool() + json_format.ParseDict(js_dict, + any_test_pb2.TestAny(), + descriptor_pool=empty_pool) + self.assertEqual( + str(cm.exception), + 'Failed to parse any_value field: Can not find message descriptor by' + ' type_url: type.googleapis.com/proto3.MessageType..') + def testMessageToDict(self): message = json_format_proto3_pb2.TestMessage() message.int32_value = 12345 diff --git a/python/google/protobuf/internal/keywords_test.py b/python/google/protobuf/internal/keywords_test.py new file mode 100755 index 0000000000..87449280a7 --- /dev/null +++ b/python/google/protobuf/internal/keywords_test.py @@ -0,0 +1,105 @@ +#! /usr/bin/env python +# +# 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. + +"""Tests for google.protobuf.internal.keywords.""" + +import unittest + + +from google.protobuf.internal import more_messages_pb2 +from google.protobuf import descriptor_pool + + +class KeywordsConflictTest(unittest.TestCase): + + def setUp(self): + super(KeywordsConflictTest, self).setUp() + self.pool = descriptor_pool.Default() + + def testMessage(self): + message = getattr(more_messages_pb2, 'class')() + message.int_field = 123 + self.assertEqual(message.int_field, 123) + des = self.pool.FindMessageTypeByName('google.protobuf.internal.class') + self.assertEqual(des.name, 'class') + + def testNestedMessage(self): + message = getattr(more_messages_pb2, 'class')() + message.nested_message.field = 234 + self.assertEqual(message.nested_message.field, 234) + des = self.pool.FindMessageTypeByName('google.protobuf.internal.class.try') + self.assertEqual(des.name, 'try') + + def testField(self): + message = getattr(more_messages_pb2, 'class')() + setattr(message, 'if', 123) + setattr(message, 'as', 1) + self.assertEqual(getattr(message, 'if'), 123) + self.assertEqual(getattr(message, 'as'), 1) + + def testEnum(self): + class_ = getattr(more_messages_pb2, 'class') + message = class_() + # Normal enum value. + message.enum_field = more_messages_pb2.default + self.assertEqual(message.enum_field, more_messages_pb2.default) + # Top level enum value. + message.enum_field = getattr(more_messages_pb2, 'else') + self.assertEqual(message.enum_field, 1) + # Nested enum value + message.nested_enum_field = getattr(class_, 'True') + self.assertEqual(message.nested_enum_field, 1) + + def testExtension(self): + message = getattr(more_messages_pb2, 'class')() + # Top level extension + extension1 = getattr(more_messages_pb2, 'continue') + message.Extensions[extension1] = 456 + self.assertEqual(message.Extensions[extension1], 456) + # None top level extension + extension2 = getattr(more_messages_pb2.ExtendClass, 'return') + message.Extensions[extension2] = 789 + self.assertEqual(message.Extensions[extension2], 789) + + def testExtensionForNestedMessage(self): + message = getattr(more_messages_pb2, 'class')() + extension = getattr(more_messages_pb2, 'with') + message.nested_message.Extensions[extension] = 999 + self.assertEqual(message.nested_message.Extensions[extension], 999) + + def TestFullKeywordUsed(self): + message = more_messages_pb2.TestFullKeyword() + message.field2.int_field = 123 + + +if __name__ == '__main__': + unittest.main() diff --git a/python/google/protobuf/internal/message_factory_test.py b/python/google/protobuf/internal/message_factory_test.py index b97e3f6518..24b79ecc9e 100644 --- a/python/google/protobuf/internal/message_factory_test.py +++ b/python/google/protobuf/internal/message_factory_test.py @@ -43,11 +43,13 @@ from google.protobuf import descriptor_pb2 from google.protobuf.internal import api_implementation from google.protobuf.internal import factory_test1_pb2 from google.protobuf.internal import factory_test2_pb2 +from google.protobuf.internal import testing_refleaks from google.protobuf import descriptor_database from google.protobuf import descriptor_pool from google.protobuf import message_factory +@testing_refleaks.TestCase class MessageFactoryTest(unittest.TestCase): def setUp(self): @@ -136,13 +138,14 @@ class MessageFactoryTest(unittest.TestCase): 'google.protobuf.python.internal.Factory2Message.one_more_field') ext2 = msg1.Extensions._FindExtensionByName( 'google.protobuf.python.internal.another_field') + self.assertEqual(0, len(msg1.Extensions)) msg1.Extensions[ext1] = 'test1' msg1.Extensions[ext2] = 'test2' self.assertEqual('test1', msg1.Extensions[ext1]) self.assertEqual('test2', msg1.Extensions[ext2]) self.assertEqual(None, msg1.Extensions._FindExtensionByNumber(12321)) - self.assertRaises(TypeError, len, msg1.Extensions) + self.assertEqual(2, len(msg1.Extensions)) if api_implementation.Type() == 'cpp': self.assertRaises(TypeError, msg1.Extensions._FindExtensionByName, 0) diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py index 1a865398f2..9de3e7a016 100755 --- a/python/google/protobuf/internal/message_test.py +++ b/python/google/protobuf/internal/message_test.py @@ -45,7 +45,6 @@ abstract interface. __author__ = 'gps@google.com (Gregory P. Smith)' -import collections import copy import math import operator @@ -55,6 +54,13 @@ import six import sys import warnings +try: + # Since python 3 + import collections.abc as collections_abc +except ImportError: + # Won't work after python 3.8 + import collections as collections_abc + try: import unittest2 as unittest # PY26 except ImportError: @@ -81,6 +87,7 @@ from google.protobuf.internal import testing_refleaks from google.protobuf import message from google.protobuf.internal import _parameterized +UCS2_MAXUNICODE = 65535 if six.PY3: long = int @@ -99,13 +106,11 @@ def IsNegInf(val): return isinf(val) and (val < 0) -BaseTestCase = testing_refleaks.BaseTestCase - - @_parameterized.named_parameters( ('_proto2', unittest_pb2), ('_proto3', unittest_proto3_arena_pb2)) -class MessageTest(BaseTestCase): +@testing_refleaks.TestCase +class MessageTest(unittest.TestCase): def testBadUtf8String(self, message_module): if api_implementation.Type() != 'python': @@ -138,9 +143,10 @@ class MessageTest(BaseTestCase): def testGoldenPackedMessage(self, message_module): golden_data = test_util.GoldenFileData('golden_packed_fields_message') golden_message = message_module.TestPackedTypes() - golden_message.ParseFromString(golden_data) + parsed_bytes = golden_message.ParseFromString(golden_data) all_set = message_module.TestPackedTypes() test_util.SetAllPackedFields(all_set) + self.assertEqual(parsed_bytes, len(golden_data)) self.assertEqual(all_set, golden_message) self.assertEqual(golden_data, all_set.SerializeToString()) golden_copy = copy.deepcopy(golden_message) @@ -157,15 +163,6 @@ class MessageTest(BaseTestCase): with self.assertRaises(message.DecodeError) as context: msg.FromString(end_tag) self.assertEqual('Unexpected end-group tag.', str(context.exception)) - else: - with warnings.catch_warnings(record=True) as w: - # Cause all warnings to always be triggered. - warnings.simplefilter('always') - msg.FromString(end_tag) - assert len(w) == 1 - assert issubclass(w[-1].category, RuntimeWarning) - self.assertEqual('Unexpected end-group tag: Not all data was converted', - str(w[-1].message)) def testDeterminismParameters(self, message_module): # This message is always deterministically serialized, even if determinism @@ -353,6 +350,27 @@ class MessageTest(BaseTestCase): message.ParseFromString(message.SerializeToString()) self.assertTrue(message.optional_float == -kMostNegExponentOneSigBit) + # Max 4 bytes float value + max_float = float.fromhex('0x1.fffffep+127') + message.optional_float = max_float + self.assertAlmostEqual(message.optional_float, max_float) + serialized_data = message.SerializeToString() + message.ParseFromString(serialized_data) + self.assertAlmostEqual(message.optional_float, max_float) + + # Test set double to float field. + message.optional_float = 3.4028235e+39 + self.assertEqual(message.optional_float, float('inf')) + serialized_data = message.SerializeToString() + message.ParseFromString(serialized_data) + self.assertEqual(message.optional_float, float('inf')) + + message.optional_float = -3.4028235e+39 + self.assertEqual(message.optional_float, float('-inf')) + + message.optional_float = 1.4028235e-39 + self.assertAlmostEqual(message.optional_float, 1.4028235e-39) + def testExtremeDoubleValues(self, message_module): message = message_module.TestAllTypes() @@ -418,6 +436,58 @@ class MessageTest(BaseTestCase): empty.ParseFromString(populated.SerializeToString()) self.assertEqual(str(empty), '') + def testAppendRepeatedCompositeField(self, message_module): + msg = message_module.TestAllTypes() + msg.repeated_nested_message.append( + message_module.TestAllTypes.NestedMessage(bb=1)) + nested = message_module.TestAllTypes.NestedMessage(bb=2) + msg.repeated_nested_message.append(nested) + try: + msg.repeated_nested_message.append(1) + except TypeError: + pass + self.assertEqual(2, len(msg.repeated_nested_message)) + self.assertEqual([1, 2], + [m.bb for m in msg.repeated_nested_message]) + + def testInsertRepeatedCompositeField(self, message_module): + msg = message_module.TestAllTypes() + msg.repeated_nested_message.insert( + -1, message_module.TestAllTypes.NestedMessage(bb=1)) + sub_msg = msg.repeated_nested_message[0] + msg.repeated_nested_message.insert( + 0, message_module.TestAllTypes.NestedMessage(bb=2)) + msg.repeated_nested_message.insert( + 99, message_module.TestAllTypes.NestedMessage(bb=3)) + msg.repeated_nested_message.insert( + -2, message_module.TestAllTypes.NestedMessage(bb=-1)) + msg.repeated_nested_message.insert( + -1000, message_module.TestAllTypes.NestedMessage(bb=-1000)) + try: + msg.repeated_nested_message.insert(1, 999) + except TypeError: + pass + self.assertEqual(5, len(msg.repeated_nested_message)) + self.assertEqual([-1000, 2, -1, 1, 3], + [m.bb for m in msg.repeated_nested_message]) + self.assertEqual(str(msg), + 'repeated_nested_message {\n' + ' bb: -1000\n' + '}\n' + 'repeated_nested_message {\n' + ' bb: 2\n' + '}\n' + 'repeated_nested_message {\n' + ' bb: -1\n' + '}\n' + 'repeated_nested_message {\n' + ' bb: 1\n' + '}\n' + 'repeated_nested_message {\n' + ' bb: 3\n' + '}\n') + self.assertEqual(sub_msg.bb, 1) + def testMergeFromRepeatedField(self, message_module): msg = message_module.TestAllTypes() msg.repeated_int32.append(1) @@ -449,6 +519,30 @@ class MessageTest(BaseTestCase): pass self.assertEqual(len(msg.repeated_nested_message), 0) + def testRepeatedContains(self, message_module): + msg = message_module.TestAllTypes() + msg.repeated_int32.extend([1, 2, 3]) + self.assertIn(2, msg.repeated_int32) + self.assertNotIn(0, msg.repeated_int32) + + msg.repeated_nested_message.add(bb=1) + sub_msg1 = msg.repeated_nested_message[0] + sub_msg2 = message_module.TestAllTypes.NestedMessage(bb=2) + sub_msg3 = message_module.TestAllTypes.NestedMessage(bb=3) + msg.repeated_nested_message.append(sub_msg2) + msg.repeated_nested_message.insert(0, sub_msg3) + self.assertIn(sub_msg1, msg.repeated_nested_message) + self.assertIn(sub_msg2, msg.repeated_nested_message) + self.assertIn(sub_msg3, msg.repeated_nested_message) + + def testRepeatedScalarIterable(self, message_module): + msg = message_module.TestAllTypes() + msg.repeated_int32.extend([1, 2, 3]) + add = 0 + for item in msg.repeated_int32: + add += item + self.assertEqual(add, 6) + def testRepeatedNestedFieldIteration(self, message_module): msg = message_module.TestAllTypes() msg.repeated_nested_message.add(bb=1) @@ -663,9 +757,9 @@ class MessageTest(BaseTestCase): def testRepeatedFieldsAreSequences(self, message_module): m = message_module.TestAllTypes() - self.assertIsInstance(m.repeated_int32, collections.MutableSequence) + self.assertIsInstance(m.repeated_int32, collections_abc.MutableSequence) self.assertIsInstance(m.repeated_nested_message, - collections.MutableSequence) + collections_abc.MutableSequence) def testRepeatedFieldsNotHashable(self, message_module): m = message_module.TestAllTypes() @@ -683,28 +777,31 @@ class MessageTest(BaseTestCase): m1 = message_module.TestAllTypes() m2 = message_module.TestAllTypes() # Cpp extension will lazily create a sub message which is immutable. - self.assertEqual(0, m1.optional_nested_message.bb) + nested = m1.optional_nested_message + self.assertEqual(0, nested.bb) m2.optional_nested_message.bb = 1 # Make sure cmessage pointing to a mutable message after merge instead of # the lazily created message. m1.MergeFrom(m2) - self.assertEqual(1, m1.optional_nested_message.bb) + self.assertEqual(1, nested.bb) # Test more nested sub message. msg1 = message_module.NestedTestAllTypes() msg2 = message_module.NestedTestAllTypes() - self.assertEqual(0, msg1.child.payload.optional_nested_message.bb) + nested = msg1.child.payload.optional_nested_message + self.assertEqual(0, nested.bb) msg2.child.payload.optional_nested_message.bb = 1 msg1.MergeFrom(msg2) - self.assertEqual(1, msg1.child.payload.optional_nested_message.bb) + self.assertEqual(1, nested.bb) # Test repeated field. self.assertEqual(msg1.payload.repeated_nested_message, msg1.payload.repeated_nested_message) - msg2.payload.repeated_nested_message.add().bb = 1 + nested = msg2.payload.repeated_nested_message.add() + nested.bb = 1 msg1.MergeFrom(msg2) self.assertEqual(1, len(msg1.payload.repeated_nested_message)) - self.assertEqual(1, msg1.payload.repeated_nested_message[0].bb) + self.assertEqual(1, nested.bb) def testMergeFromString(self, message_module): m1 = message_module.TestAllTypes() @@ -1180,9 +1277,31 @@ class MessageTest(BaseTestCase): with self.assertRaises(AttributeError): m.repeated_int32 = [] + def testReturningType(self, message_module): + m = message_module.TestAllTypes() + self.assertEqual(float, type(m.optional_float)) + self.assertEqual(float, type(m.optional_double)) + self.assertEqual(bool, type(m.optional_bool)) + m.optional_float = 1 + m.optional_double = 1 + m.optional_bool = 1 + m.repeated_float.append(1) + m.repeated_double.append(1) + m.repeated_bool.append(1) + m.ParseFromString(m.SerializeToString()) + self.assertEqual(float, type(m.optional_float)) + self.assertEqual(float, type(m.optional_double)) + self.assertEqual('1.0', str(m.optional_double)) + self.assertEqual(bool, type(m.optional_bool)) + self.assertEqual(float, type(m.repeated_float[0])) + self.assertEqual(float, type(m.repeated_double[0])) + self.assertEqual(bool, type(m.repeated_bool[0])) + self.assertEqual(True, m.repeated_bool[0]) + # Class to test proto2-only features (required, extensions, etc.) -class Proto2Test(BaseTestCase): +@testing_refleaks.TestCase +class Proto2Test(unittest.TestCase): def testFieldPresence(self): message = unittest_pb2.TestAllTypes() @@ -1216,13 +1335,13 @@ class Proto2Test(BaseTestCase): message.optional_bool = True message.optional_nested_message.bb = 15 - self.assertTrue(message.HasField("optional_int32")) + self.assertTrue(message.HasField(u"optional_int32")) self.assertTrue(message.HasField("optional_bool")) self.assertTrue(message.HasField("optional_nested_message")) # Clearing the fields unsets them and resets their value to default. message.ClearField("optional_int32") - message.ClearField("optional_bool") + message.ClearField(u"optional_bool") message.ClearField("optional_nested_message") self.assertFalse(message.HasField("optional_int32")) @@ -1434,6 +1553,16 @@ class Proto2Test(BaseTestCase): with self.assertRaises(ValueError): unittest_pb2.TestAllTypes(repeated_nested_enum='FOO') + def testPythonicInitWithDict(self): + # Both string/unicode field name keys should work. + kwargs = { + 'optional_int32': 100, + u'optional_fixed32': 200, + } + msg = unittest_pb2.TestAllTypes(**kwargs) + self.assertEqual(100, msg.optional_int32) + self.assertEqual(200, msg.optional_fixed32) + def test_documentation(self): # Also used by the interactive help() function. @@ -1446,7 +1575,8 @@ class Proto2Test(BaseTestCase): # Class to test proto3-only features/behavior (updated field presence & enums) -class Proto3Test(BaseTestCase): +@testing_refleaks.TestCase +class Proto3Test(unittest.TestCase): # Utility method for comparing equality with a map. def assertMapIterEquals(self, map_iter, dict_value): @@ -1595,6 +1725,7 @@ class Proto3Test(BaseTestCase): self.assertIsNone(msg.map_int32_int32.get(5)) self.assertEqual(10, msg.map_int32_int32.get(5, 10)) + self.assertEqual(10, msg.map_int32_int32.get(key=5, default=10)) self.assertIsNone(msg.map_int32_int32.get(5)) msg.map_int32_int32[5] = 15 @@ -1605,6 +1736,7 @@ class Proto3Test(BaseTestCase): self.assertIsNone(msg.map_int32_foreign_message.get(5)) self.assertEqual(10, msg.map_int32_foreign_message.get(5, 10)) + self.assertEqual(10, msg.map_int32_foreign_message.get(key=5, default=10)) submsg = msg.map_int32_foreign_message[5] self.assertIs(submsg, msg.map_int32_foreign_message.get(5)) @@ -1779,6 +1911,13 @@ class Proto3Test(BaseTestCase): old_map_value = msg2.map_int32_foreign_message[222] msg2.MergeFrom(msg) + # Compare with expected message instead of call + # msg2.map_int32_foreign_message[222] to make sure MergeFrom does not + # sync with repeated field and there is no duplicated keys. + expected_msg = map_unittest_pb2.TestMap() + expected_msg.CopyFrom(msg) + expected_msg.map_int64_int64[88] = 99 + self.assertEqual(msg2, expected_msg) self.assertEqual(34, msg2.map_int32_int32[12]) self.assertEqual(78, msg2.map_int32_int32[56]) @@ -1842,9 +1981,13 @@ class Proto3Test(BaseTestCase): self.assertEqual(99, msg2.map_int64_int64[88]) msg2.map_int32_foreign_message.MergeFrom(msg.map_int32_foreign_message) - self.assertEqual(5, msg2.map_int32_foreign_message[111].c) - self.assertEqual(10, msg2.map_int32_foreign_message[222].c) - self.assertFalse(msg2.map_int32_foreign_message[222].HasField('d')) + # Compare with expected message instead of call + # msg.map_int32_foreign_message[222] to make sure MergeFrom does not + # sync with repeated field and no duplicated keys. + expected_msg = map_unittest_pb2.TestMap() + expected_msg.CopyFrom(msg) + expected_msg.map_int64_int64[88] = 99 + self.assertEqual(msg2, expected_msg) # Test when cpp extension cache a map. m1 = map_unittest_pb2.TestMap() @@ -2103,6 +2246,24 @@ class Proto3Test(BaseTestCase): map_int32_foreign_message={3: unittest_pb2.ForeignMessage(c=5)}) self.assertEqual(5, msg.map_int32_foreign_message[3].c) + def testMapScalarFieldConstruction(self): + msg1 = map_unittest_pb2.TestMap() + msg1.map_int32_int32[1] = 42 + msg2 = map_unittest_pb2.TestMap(map_int32_int32=msg1.map_int32_int32) + self.assertEqual(42, msg2.map_int32_int32[1]) + + def testMapMessageFieldConstruction(self): + msg1 = map_unittest_pb2.TestMap() + msg1.map_string_foreign_message['test'].c = 42 + msg2 = map_unittest_pb2.TestMap( + map_string_foreign_message=msg1.map_string_foreign_message) + self.assertEqual(42, msg2.map_string_foreign_message['test'].c) + + def testMapFieldRaisesCorrectError(self): + # Should raise a TypeError when given a non-iterable. + with self.assertRaises(TypeError): + map_unittest_pb2.TestMap(map_string_foreign_message=1) + def testMapValidAfterFieldCleared(self): # Map needs to work even if field is cleared. # For the C++ implementation this tests the correctness of @@ -2189,11 +2350,11 @@ class Proto3Test(BaseTestCase): def testMapsAreMapping(self): msg = map_unittest_pb2.TestMap() - self.assertIsInstance(msg.map_int32_int32, collections.Mapping) - self.assertIsInstance(msg.map_int32_int32, collections.MutableMapping) - self.assertIsInstance(msg.map_int32_foreign_message, collections.Mapping) + self.assertIsInstance(msg.map_int32_int32, collections_abc.Mapping) + self.assertIsInstance(msg.map_int32_int32, collections_abc.MutableMapping) + self.assertIsInstance(msg.map_int32_foreign_message, collections_abc.Mapping) self.assertIsInstance(msg.map_int32_foreign_message, - collections.MutableMapping) + collections_abc.MutableMapping) def testMapsCompare(self): msg = map_unittest_pb2.TestMap() @@ -2210,6 +2371,7 @@ class Proto3Test(BaseTestCase): msg.map_string_foreign_message['foo'].c = 5 self.assertEqual(0, len(msg.FindInitializationErrors())) + @unittest.skipIf(sys.maxunicode == UCS2_MAXUNICODE, 'Skip for ucs2') def testStrictUtf8Check(self): # Test u'\ud801' is rejected at parser in both python2 and python3. serialized = (b'r\x03\xed\xa0\x81') @@ -2259,7 +2421,8 @@ class Proto3Test(BaseTestCase): unittest_proto3_arena_pb2.TestAllTypes( optional_string=u'\ud801\ud801') - @unittest.skipIf(six.PY3, 'Surrogates are rejected at setters in Python3') + @unittest.skipIf(six.PY3 or sys.maxunicode == UCS2_MAXUNICODE, + 'Surrogates are rejected at setters in Python3') def testSurrogatesInPython2(self): # Test optional_string=u'\ud801\udc01'. # surrogate pair is acceptable in python2. @@ -2286,7 +2449,8 @@ class Proto3Test(BaseTestCase): optional_string=u'\ud801\ud801') -class ValidTypeNamesTest(BaseTestCase): +@testing_refleaks.TestCase +class ValidTypeNamesTest(unittest.TestCase): def assertImportFromName(self, msg, base_name): # Parse to extra 'some.name' as a string. @@ -2307,7 +2471,8 @@ class ValidTypeNamesTest(BaseTestCase): self.assertImportFromName(pb.repeated_int32, 'Scalar') self.assertImportFromName(pb.repeated_nested_message, 'Composite') -class PackedFieldTest(BaseTestCase): +@testing_refleaks.TestCase +class PackedFieldTest(unittest.TestCase): def setMessage(self, message): message.repeated_int32.append(1) @@ -2367,7 +2532,8 @@ class PackedFieldTest(BaseTestCase): @unittest.skipIf(api_implementation.Type() != 'cpp' or sys.version_info < (2, 7), 'explicit tests of the C++ implementation for PY27 and above') -class OversizeProtosTest(BaseTestCase): +@testing_refleaks.TestCase +class OversizeProtosTest(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/python/google/protobuf/internal/missing_enum_values.proto b/python/google/protobuf/internal/missing_enum_values.proto index 1850be5bb7..5c0f499dba 100644 --- a/python/google/protobuf/internal/missing_enum_values.proto +++ b/python/google/protobuf/internal/missing_enum_values.proto @@ -30,6 +30,7 @@ syntax = "proto2"; + package google.protobuf.python.internal; message TestEnumValues { diff --git a/python/google/protobuf/internal/more_extensions.proto b/python/google/protobuf/internal/more_extensions.proto index 78f1467361..5038fd2447 100644 --- a/python/google/protobuf/internal/more_extensions.proto +++ b/python/google/protobuf/internal/more_extensions.proto @@ -41,7 +41,9 @@ message TopLevelMessage { message ExtendedMessage { - extensions 1 to max; + optional int32 optional_int32 = 1001; + repeated string repeated_string = 1002; + extensions 1 to 999; } diff --git a/python/google/protobuf/internal/more_messages.proto b/python/google/protobuf/internal/more_messages.proto index 2c6ab9efdf..c5889c481f 100644 --- a/python/google/protobuf/internal/more_messages.proto +++ b/python/google/protobuf/internal/more_messages.proto @@ -30,6 +30,8 @@ // Author: robinson@google.com (Will Robinson) +// LINT: LEGACY_NAMES + syntax = "proto2"; package google.protobuf.internal; @@ -50,3 +52,309 @@ extend OutOfOrderFields { optional uint64 optional_uint64 = 4; optional int64 optional_int64 = 2; } + +enum is { // top level enum keyword + default = 0; + else = 1; // top level enum value keyword +} + +message class { // message keyword + optional int32 int_field = 1; + optional int32 if = 2; // field keyword + optional is as = 3; // enum field keyword + optional is enum_field = 4; + enum for { // nested enum keyword + default = 0; + True = 1; // nested enum value keyword + } + optional for nested_enum_field = 5; + message try { + optional int32 field = 1; + extensions 999 to 9999; + } + optional try nested_message = 6; + extensions 999 to 9999; +} + +extend class { + optional int32 continue = 1001; // top level extension keyword +} + +extend class.try { + optional int32 with = 1001; +} + +message ExtendClass { + extend class { + optional int32 return = 1002; // nested extension keyword + } +} + +message TestFullKeyword { + optional google.protobuf.internal.OutOfOrderFields field1 = 1; + optional google.protobuf.internal.class field2 = 2; +} + +// TODO(jieluo): Add keyword support for service. +// service False { +// rpc Bar(class) returns (class); +// } + +message LotsNestedMessage { + message B0 {} + message B1 {} + message B2 {} + message B3 {} + message B4 {} + message B5 {} + message B6 {} + message B7 {} + message B8 {} + message B9 {} + message B10 {} + message B11 {} + message B12 {} + message B13 {} + message B14 {} + message B15 {} + message B16 {} + message B17 {} + message B18 {} + message B19 {} + message B20 {} + message B21 {} + message B22 {} + message B23 {} + message B24 {} + message B25 {} + message B26 {} + message B27 {} + message B28 {} + message B29 {} + message B30 {} + message B31 {} + message B32 {} + message B33 {} + message B34 {} + message B35 {} + message B36 {} + message B37 {} + message B38 {} + message B39 {} + message B40 {} + message B41 {} + message B42 {} + message B43 {} + message B44 {} + message B45 {} + message B46 {} + message B47 {} + message B48 {} + message B49 {} + message B50 {} + message B51 {} + message B52 {} + message B53 {} + message B54 {} + message B55 {} + message B56 {} + message B57 {} + message B58 {} + message B59 {} + message B60 {} + message B61 {} + message B62 {} + message B63 {} + message B64 {} + message B65 {} + message B66 {} + message B67 {} + message B68 {} + message B69 {} + message B70 {} + message B71 {} + message B72 {} + message B73 {} + message B74 {} + message B75 {} + message B76 {} + message B77 {} + message B78 {} + message B79 {} + message B80 {} + message B81 {} + message B82 {} + message B83 {} + message B84 {} + message B85 {} + message B86 {} + message B87 {} + message B88 {} + message B89 {} + message B90 {} + message B91 {} + message B92 {} + message B93 {} + message B94 {} + message B95 {} + message B96 {} + message B97 {} + message B98 {} + message B99 {} + message B100 {} + message B101 {} + message B102 {} + message B103 {} + message B104 {} + message B105 {} + message B106 {} + message B107 {} + message B108 {} + message B109 {} + message B110 {} + message B111 {} + message B112 {} + message B113 {} + message B114 {} + message B115 {} + message B116 {} + message B117 {} + message B118 {} + message B119 {} + message B120 {} + message B121 {} + message B122 {} + message B123 {} + message B124 {} + message B125 {} + message B126 {} + message B127 {} + message B128 {} + message B129 {} + message B130 {} + message B131 {} + message B132 {} + message B133 {} + message B134 {} + message B135 {} + message B136 {} + message B137 {} + message B138 {} + message B139 {} + message B140 {} + message B141 {} + message B142 {} + message B143 {} + message B144 {} + message B145 {} + message B146 {} + message B147 {} + message B148 {} + message B149 {} + message B150 {} + message B151 {} + message B152 {} + message B153 {} + message B154 {} + message B155 {} + message B156 {} + message B157 {} + message B158 {} + message B159 {} + message B160 {} + message B161 {} + message B162 {} + message B163 {} + message B164 {} + message B165 {} + message B166 {} + message B167 {} + message B168 {} + message B169 {} + message B170 {} + message B171 {} + message B172 {} + message B173 {} + message B174 {} + message B175 {} + message B176 {} + message B177 {} + message B178 {} + message B179 {} + message B180 {} + message B181 {} + message B182 {} + message B183 {} + message B184 {} + message B185 {} + message B186 {} + message B187 {} + message B188 {} + message B189 {} + message B190 {} + message B191 {} + message B192 {} + message B193 {} + message B194 {} + message B195 {} + message B196 {} + message B197 {} + message B198 {} + message B199 {} + message B200 {} + message B201 {} + message B202 {} + message B203 {} + message B204 {} + message B205 {} + message B206 {} + message B207 {} + message B208 {} + message B209 {} + message B210 {} + message B211 {} + message B212 {} + message B213 {} + message B214 {} + message B215 {} + message B216 {} + message B217 {} + message B218 {} + message B219 {} + message B220 {} + message B221 {} + message B222 {} + message B223 {} + message B224 {} + message B225 {} + message B226 {} + message B227 {} + message B228 {} + message B229 {} + message B230 {} + message B231 {} + message B232 {} + message B233 {} + message B234 {} + message B235 {} + message B236 {} + message B237 {} + message B238 {} + message B239 {} + message B240 {} + message B241 {} + message B242 {} + message B243 {} + message B244 {} + message B245 {} + message B246 {} + message B247 {} + message B248 {} + message B249 {} + message B250 {} + message B251 {} + message B252 {} + message B253 {} + message B254 {} + message B255 {} +} diff --git a/python/google/protobuf/internal/python_message.py b/python/google/protobuf/internal/python_message.py index ab5d160f48..803ae84849 100755 --- a/python/google/protobuf/internal/python_message.py +++ b/python/google/protobuf/internal/python_message.py @@ -64,6 +64,7 @@ from google.protobuf.internal import containers from google.protobuf.internal import decoder from google.protobuf.internal import encoder from google.protobuf.internal import enum_type_wrapper +from google.protobuf.internal import extension_dict from google.protobuf.internal import message_listener as message_listener_mod from google.protobuf.internal import type_checkers from google.protobuf.internal import well_known_types @@ -74,7 +75,7 @@ from google.protobuf import text_format _FieldDescriptor = descriptor_mod.FieldDescriptor _AnyFullTypeName = 'google.protobuf.Any' - +_ExtensionDict = extension_dict._ExtensionDict class GeneratedProtocolMessageType(type): @@ -237,28 +238,6 @@ def _PropertyName(proto_field_name): return proto_field_name -def _VerifyExtensionHandle(message, extension_handle): - """Verify that the given extension handle is valid.""" - - if not isinstance(extension_handle, _FieldDescriptor): - raise KeyError('HasExtension() expects an extension handle, got: %s' % - extension_handle) - - if not extension_handle.is_extension: - raise KeyError('"%s" is not an extension.' % extension_handle.full_name) - - if not extension_handle.containing_type: - raise KeyError('"%s" is missing a containing_type.' - % extension_handle.full_name) - - if extension_handle.containing_type is not message.DESCRIPTOR: - raise KeyError('Extension "%s" extends message type "%s", but this ' - 'message is of type "%s".' % - (extension_handle.full_name, - extension_handle.containing_type.full_name, - message.DESCRIPTOR.full_name)) - - def _AddSlots(message_descriptor, dictionary): """Adds a __slots__ entry to dictionary, containing the names of all valid attributes for this message type. @@ -294,7 +273,7 @@ def _IsMapField(field): def _IsMessageMapField(field): - value_type = field.message_type.fields_by_name["value"] + value_type = field.message_type.fields_by_name['value'] return value_type.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE @@ -311,12 +290,12 @@ def _AttachFieldHelpers(cls, field_descriptor): wire_format.IsTypePackable(field_descriptor.type)) if not is_packable: is_packed = False - elif field_descriptor.containing_type.syntax == "proto2": + elif field_descriptor.containing_type.syntax == 'proto2': is_packed = (field_descriptor.has_options and field_descriptor.GetOptions().packed) else: has_packed_false = (field_descriptor.has_options and - field_descriptor.GetOptions().HasField("packed") and + field_descriptor.GetOptions().HasField('packed') and field_descriptor.GetOptions().packed == False) is_packed = not has_packed_false is_map_entry = _IsMapField(field_descriptor) @@ -379,8 +358,8 @@ def _AttachFieldHelpers(cls, field_descriptor): def _AddClassAttributesForNestedExtensions(descriptor, dictionary): - extension_dict = descriptor.extensions_by_name - for extension_name, extension_field in extension_dict.items(): + extensions = descriptor.extensions_by_name + for extension_name, extension_field in extensions.items(): assert extension_name not in dictionary dictionary[extension_name] = extension_field @@ -529,7 +508,7 @@ def _AddInitMethod(message_descriptor, cls): for field_name, field_value in kwargs.items(): field = _GetFieldByName(message_descriptor, field_name) if field is None: - raise TypeError("%s() got an unexpected keyword argument '%s'" % + raise TypeError('%s() got an unexpected keyword argument "%s"' % (message_descriptor.name, field_name)) if field_value is None: # field=None is the same as no field at all. @@ -619,7 +598,7 @@ def _AddPropertiesForField(field, cls): # handle specially here. assert _FieldDescriptor.MAX_CPPTYPE == 10 - constant_name = field.name.upper() + "_FIELD_NUMBER" + constant_name = field.name.upper() + '_FIELD_NUMBER' setattr(cls, constant_name, field.number) if field.label == _FieldDescriptor.LABEL_REPEATED: @@ -698,7 +677,7 @@ def _AddPropertiesForNonRepeatedScalarField(field, cls): type_checker = type_checkers.GetTypeChecker(field) default_value = field.default_value valid_values = set() - is_proto3 = field.containing_type.syntax == "proto3" + is_proto3 = field.containing_type.syntax == 'proto3' def getter(self): # TODO(protobuf-team): This may be broken since there may not be @@ -713,7 +692,11 @@ def _AddPropertiesForNonRepeatedScalarField(field, cls): # pylint: disable=protected-access # Testing the value for truthiness captures all of the proto3 defaults # (0, 0.0, enum 0, and False). - new_value = type_checker.CheckValue(new_value) + try: + new_value = type_checker.CheckValue(new_value) + except TypeError as e: + raise TypeError( + 'Cannot set %s to %.1024r: %s' % (field.full_name, new_value, e)) if clear_when_set_to_default and not new_value: self._fields.pop(field, None) else: @@ -784,9 +767,9 @@ def _AddPropertiesForNonRepeatedCompositeField(field, cls): def _AddPropertiesForExtensions(descriptor, cls): """Adds properties for all fields in this protocol message type.""" - extension_dict = descriptor.extensions_by_name - for extension_name, extension_field in extension_dict.items(): - constant_name = extension_name.upper() + "_FIELD_NUMBER" + extensions = descriptor.extensions_by_name + for extension_name, extension_field in extensions.items(): + constant_name = extension_name.upper() + '_FIELD_NUMBER' setattr(cls, constant_name, extension_field.number) # TODO(amauryfa): Migrate all users of these attributes to functions like @@ -835,14 +818,15 @@ def _AddListFieldsMethod(message_descriptor, cls): cls.ListFields = ListFields -_Proto3HasError = 'Protocol message has no non-repeated submessage field "%s"' -_Proto2HasError = 'Protocol message has no non-repeated field "%s"' +_PROTO3_ERROR_TEMPLATE = \ + 'Protocol message %s has no non-repeated submessage field "%s"' +_PROTO2_ERROR_TEMPLATE = 'Protocol message %s has no non-repeated field "%s"' def _AddHasFieldMethod(message_descriptor, cls): """Helper for _AddMessageMethods().""" is_proto3 = (message_descriptor.syntax == "proto3") - error_msg = _Proto3HasError if is_proto3 else _Proto2HasError + error_msg = _PROTO3_ERROR_TEMPLATE if is_proto3 else _PROTO2_ERROR_TEMPLATE hassable_fields = {} for field in message_descriptor.fields: @@ -863,7 +847,7 @@ def _AddHasFieldMethod(message_descriptor, cls): try: field = hassable_fields[field_name] except KeyError: - raise ValueError(error_msg % field_name) + raise ValueError(error_msg % (message_descriptor.full_name, field_name)) if isinstance(field, descriptor_mod.OneofDescriptor): try: @@ -893,7 +877,7 @@ def _AddClearFieldMethod(message_descriptor, cls): else: return except KeyError: - raise ValueError('Protocol message %s() has no "%s" field.' % + raise ValueError('Protocol message %s has no "%s" field.' % (message_descriptor.name, field_name)) if field in self._fields: @@ -921,7 +905,7 @@ def _AddClearFieldMethod(message_descriptor, cls): def _AddClearExtensionMethod(cls): """Helper for _AddMessageMethods().""" def ClearExtension(self, extension_handle): - _VerifyExtensionHandle(self, extension_handle) + extension_dict._VerifyExtensionHandle(self, extension_handle) # Similar to ClearField(), above. if extension_handle in self._fields: @@ -933,7 +917,7 @@ def _AddClearExtensionMethod(cls): def _AddHasExtensionMethod(cls): """Helper for _AddMessageMethods().""" def HasExtension(self, extension_handle): - _VerifyExtensionHandle(self, extension_handle) + extension_dict._VerifyExtensionHandle(self, extension_handle) if extension_handle.label == _FieldDescriptor.LABEL_REPEATED: raise KeyError('"%s" is repeated.' % extension_handle.full_name) @@ -1003,8 +987,13 @@ def _AddEqualsMethod(message_descriptor, cls): if not self.ListFields() == other.ListFields(): return False - # pylint: disable=protected-access - return self._unknown_field_set == other._unknown_field_set + # TODO(jieluo): Fix UnknownFieldSet to consider MessageSet extensions, + # then use it for the comparison. + unknown_fields = list(self._unknown_fields) + unknown_fields.sort() + other_unknown_fields = list(other._unknown_fields) + other_unknown_fields.sort() + return unknown_fields == other_unknown_fields cls.__eq__ = __eq__ @@ -1273,7 +1262,7 @@ def _AddIsInitializedMethod(message_descriptor, cls): for field, value in self.ListFields(): if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: if field.is_extension: - name = "(%s)" % field.full_name + name = '(%s)' % field.full_name else: name = field.name @@ -1281,7 +1270,7 @@ def _AddIsInitializedMethod(message_descriptor, cls): if _IsMessageMapField(field): for key in value: element = value[key] - prefix = "%s[%s]." % (name, key) + prefix = '%s[%s].' % (name, key) sub_errors = element.FindInitializationErrors() errors += [prefix + error for error in sub_errors] else: @@ -1290,11 +1279,11 @@ def _AddIsInitializedMethod(message_descriptor, cls): elif field.label == _FieldDescriptor.LABEL_REPEATED: for i in range(len(value)): element = value[i] - prefix = "%s[%d]." % (name, i) + prefix = '%s[%d].' % (name, i) sub_errors = element.FindInitializationErrors() errors += [prefix + error for error in sub_errors] else: - prefix = name + "." + prefix = name + '.' sub_errors = value.FindInitializationErrors() errors += [prefix + error for error in sub_errors] @@ -1310,7 +1299,7 @@ def _AddMergeFromMethod(cls): def MergeFrom(self, msg): if not isinstance(msg, cls): raise TypeError( - "Parameter to MergeFrom() must be instance of same class: " + 'Parameter to MergeFrom() must be instance of same class: ' 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__)) assert msg is not self @@ -1400,7 +1389,11 @@ def _DiscardUnknownFields(self): self._unknown_field_set = None # pylint: disable=protected-access for field, value in self.ListFields(): if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: - if field.label == _FieldDescriptor.LABEL_REPEATED: + if _IsMapField(field): + if _IsMessageMapField(field): + for key in value: + value[key].DiscardUnknownFields() + elif field.label == _FieldDescriptor.LABEL_REPEATED: for sub_message in value: sub_message.DiscardUnknownFields() else: @@ -1540,126 +1533,3 @@ class _OneofListener(_Listener): super(_OneofListener, self).Modified() except ReferenceError: pass - - -# TODO(robinson): Move elsewhere? This file is getting pretty ridiculous... -# TODO(robinson): Unify error handling of "unknown extension" crap. -# TODO(robinson): Support iteritems()-style iteration over all -# extensions with the "has" bits turned on? -class _ExtensionDict(object): - - """Dict-like container for supporting an indexable "Extensions" - field on proto instances. - - Note that in all cases we expect extension handles to be - FieldDescriptors. - """ - - def __init__(self, extended_message): - """extended_message: Message instance for which we are the Extensions dict. - """ - - self._extended_message = extended_message - - def __getitem__(self, extension_handle): - """Returns the current value of the given extension handle.""" - - _VerifyExtensionHandle(self._extended_message, extension_handle) - - result = self._extended_message._fields.get(extension_handle) - if result is not None: - return result - - if extension_handle.label == _FieldDescriptor.LABEL_REPEATED: - result = extension_handle._default_constructor(self._extended_message) - elif extension_handle.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: - assert getattr(extension_handle.message_type, '_concrete_class', None), ( - 'Uninitialized concrete class found for field %r (message type %r)' - % (extension_handle.full_name, - extension_handle.message_type.full_name)) - result = extension_handle.message_type._concrete_class() - try: - result._SetListener(self._extended_message._listener_for_children) - except ReferenceError: - pass - else: - # Singular scalar -- just return the default without inserting into the - # dict. - return extension_handle.default_value - - # Atomically check if another thread has preempted us and, if not, swap - # in the new object we just created. If someone has preempted us, we - # take that object and discard ours. - # WARNING: We are relying on setdefault() being atomic. This is true - # in CPython but we haven't investigated others. This warning appears - # in several other locations in this file. - result = self._extended_message._fields.setdefault( - extension_handle, result) - - return result - - def __eq__(self, other): - if not isinstance(other, self.__class__): - return False - - my_fields = self._extended_message.ListFields() - other_fields = other._extended_message.ListFields() - - # Get rid of non-extension fields. - my_fields = [ field for field in my_fields if field.is_extension ] - other_fields = [ field for field in other_fields if field.is_extension ] - - return my_fields == other_fields - - def __ne__(self, other): - return not self == other - - def __hash__(self): - raise TypeError('unhashable object') - - # Note that this is only meaningful for non-repeated, scalar extension - # fields. Note also that we may have to call _Modified() when we do - # successfully set a field this way, to set any necssary "has" bits in the - # ancestors of the extended message. - def __setitem__(self, extension_handle, value): - """If extension_handle specifies a non-repeated, scalar extension - field, sets the value of that field. - """ - - _VerifyExtensionHandle(self._extended_message, extension_handle) - - if (extension_handle.label == _FieldDescriptor.LABEL_REPEATED or - extension_handle.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE): - raise TypeError( - 'Cannot assign to extension "%s" because it is a repeated or ' - 'composite type.' % extension_handle.full_name) - - # It's slightly wasteful to lookup the type checker each time, - # but we expect this to be a vanishingly uncommon case anyway. - type_checker = type_checkers.GetTypeChecker(extension_handle) - # pylint: disable=protected-access - self._extended_message._fields[extension_handle] = ( - type_checker.CheckValue(value)) - self._extended_message._Modified() - - def _FindExtensionByName(self, name): - """Tries to find a known extension with the specified name. - - Args: - name: Extension full name. - - Returns: - Extension field descriptor. - """ - return self._extended_message._extensions_by_name.get(name, None) - - def _FindExtensionByNumber(self, number): - """Tries to find a known extension with the field number. - - Args: - number: Extension field number. - - Returns: - Extension field descriptor. - """ - return self._extended_message._extensions_by_number.get(number, None) diff --git a/python/google/protobuf/internal/python_protobuf.cc b/python/google/protobuf/internal/python_protobuf.cc index f90cc4387e..e823bf228c 100644 --- a/python/google/protobuf/internal/python_protobuf.cc +++ b/python/google/protobuf/internal/python_protobuf.cc @@ -36,12 +36,8 @@ namespace google { namespace protobuf { namespace python { -static const Message* GetCProtoInsidePyProtoStub(PyObject* msg) { - return NULL; -} -static Message* MutableCProtoInsidePyProtoStub(PyObject* msg) { - return NULL; -} +static const Message* GetCProtoInsidePyProtoStub(PyObject* msg) { return NULL; } +static Message* MutableCProtoInsidePyProtoStub(PyObject* msg) { return NULL; } // This is initialized with a default, stub implementation. // If python-google.protobuf.cc is loaded, the function pointer is overridden diff --git a/python/google/protobuf/internal/reflection_test.py b/python/google/protobuf/internal/reflection_test.py index 31ceda2462..d486bb1ea9 100755 --- a/python/google/protobuf/internal/reflection_test.py +++ b/python/google/protobuf/internal/reflection_test.py @@ -68,9 +68,6 @@ if six.PY3: long = int # pylint: disable=redefined-builtin,invalid-name -BaseTestCase = testing_refleaks.BaseTestCase - - class _MiniDecoder(object): """Decodes a stream of values from a string. @@ -116,7 +113,8 @@ class _MiniDecoder(object): return self._pos == len(self._bytes) -class ReflectionTest(BaseTestCase): +@testing_refleaks.TestCase +class ReflectionTest(unittest.TestCase): def assertListsEqual(self, values, others): self.assertEqual(len(values), len(others)) @@ -804,30 +802,64 @@ class ReflectionTest(BaseTestCase): def testEnum_Value(self): self.assertEqual(unittest_pb2.FOREIGN_FOO, unittest_pb2.ForeignEnum.Value('FOREIGN_FOO')) + self.assertEqual(unittest_pb2.FOREIGN_FOO, + unittest_pb2.ForeignEnum.FOREIGN_FOO) + self.assertEqual(unittest_pb2.FOREIGN_BAR, unittest_pb2.ForeignEnum.Value('FOREIGN_BAR')) + self.assertEqual(unittest_pb2.FOREIGN_BAR, + unittest_pb2.ForeignEnum.FOREIGN_BAR) + self.assertEqual(unittest_pb2.FOREIGN_BAZ, unittest_pb2.ForeignEnum.Value('FOREIGN_BAZ')) + self.assertEqual(unittest_pb2.FOREIGN_BAZ, + unittest_pb2.ForeignEnum.FOREIGN_BAZ) + self.assertRaises(ValueError, unittest_pb2.ForeignEnum.Value, 'FO') + with self.assertRaises(AttributeError): + unittest_pb2.ForeignEnum.FO proto = unittest_pb2.TestAllTypes() self.assertEqual(proto.FOO, proto.NestedEnum.Value('FOO')) + self.assertEqual(proto.FOO, + proto.NestedEnum.FOO) + self.assertEqual(proto.FOO, unittest_pb2.TestAllTypes.NestedEnum.Value('FOO')) + self.assertEqual(proto.FOO, + unittest_pb2.TestAllTypes.NestedEnum.FOO) + self.assertEqual(proto.BAR, proto.NestedEnum.Value('BAR')) + self.assertEqual(proto.BAR, + proto.NestedEnum.BAR) + self.assertEqual(proto.BAR, unittest_pb2.TestAllTypes.NestedEnum.Value('BAR')) + self.assertEqual(proto.BAR, + unittest_pb2.TestAllTypes.NestedEnum.BAR) + self.assertEqual(proto.BAZ, proto.NestedEnum.Value('BAZ')) + self.assertEqual(proto.BAZ, + proto.NestedEnum.BAZ) + self.assertEqual(proto.BAZ, unittest_pb2.TestAllTypes.NestedEnum.Value('BAZ')) + self.assertEqual(proto.BAZ, + unittest_pb2.TestAllTypes.NestedEnum.BAZ) + self.assertRaises(ValueError, proto.NestedEnum.Value, 'Foo') + with self.assertRaises(AttributeError): + proto.NestedEnum.Value.Foo + self.assertRaises(ValueError, unittest_pb2.TestAllTypes.NestedEnum.Value, 'Foo') + with self.assertRaises(AttributeError): + unittest_pb2.TestAllTypes.NestedEnum.Value.Foo def testEnum_KeysAndValues(self): self.assertEqual(['FOREIGN_FOO', 'FOREIGN_BAR', 'FOREIGN_BAZ'], @@ -1167,32 +1199,71 @@ class ReflectionTest(BaseTestCase): self.assertEqual(prius.price, new_prius.price) self.assertEqual(prius.owners, new_prius.owners) + def testExtensionIter(self): + extendee_proto = more_extensions_pb2.ExtendedMessage() + + extension_int32 = more_extensions_pb2.optional_int_extension + extendee_proto.Extensions[extension_int32] = 23 + + extension_repeated = more_extensions_pb2.repeated_int_extension + extendee_proto.Extensions[extension_repeated].append(11) + + extension_msg = more_extensions_pb2.optional_message_extension + extendee_proto.Extensions[extension_msg].foreign_message_int = 56 + + # Set some normal fields. + extendee_proto.optional_int32 = 1 + extendee_proto.repeated_string.append('hi') + + expected = (extension_int32, extension_msg, extension_repeated) + count = 0 + for item in extendee_proto.Extensions: + self.assertEqual(item.name, expected[count].name) + self.assertIn(item, extendee_proto.Extensions) + count += 1 + self.assertEqual(count, 3) + + def testExtensionContainsError(self): + extendee_proto = more_extensions_pb2.ExtendedMessage() + self.assertRaises(KeyError, extendee_proto.Extensions.__contains__, 0) + + field = more_extensions_pb2.ExtendedMessage.DESCRIPTOR.fields_by_name[ + 'optional_int32'] + self.assertRaises(KeyError, extendee_proto.Extensions.__contains__, field) + def testTopLevelExtensionsForOptionalScalar(self): extendee_proto = unittest_pb2.TestAllExtensions() extension = unittest_pb2.optional_int32_extension self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) self.assertEqual(0, extendee_proto.Extensions[extension]) # As with normal scalar fields, just doing a read doesn't actually set the # "has" bit. self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) # Actually set the thing. extendee_proto.Extensions[extension] = 23 self.assertEqual(23, extendee_proto.Extensions[extension]) self.assertTrue(extendee_proto.HasExtension(extension)) + self.assertIn(extension, extendee_proto.Extensions) # Ensure that clearing works as well. extendee_proto.ClearExtension(extension) self.assertEqual(0, extendee_proto.Extensions[extension]) self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) def testTopLevelExtensionsForRepeatedScalar(self): extendee_proto = unittest_pb2.TestAllExtensions() extension = unittest_pb2.repeated_string_extension self.assertEqual(0, len(extendee_proto.Extensions[extension])) + self.assertNotIn(extension, extendee_proto.Extensions) extendee_proto.Extensions[extension].append('foo') self.assertEqual(['foo'], extendee_proto.Extensions[extension]) + self.assertIn(extension, extendee_proto.Extensions) string_list = extendee_proto.Extensions[extension] extendee_proto.ClearExtension(extension) self.assertEqual(0, len(extendee_proto.Extensions[extension])) + self.assertNotIn(extension, extendee_proto.Extensions) self.assertTrue(string_list is not extendee_proto.Extensions[extension]) # Shouldn't be allowed to do Extensions[extension] = 'a' self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions, @@ -1202,13 +1273,16 @@ class ReflectionTest(BaseTestCase): extendee_proto = unittest_pb2.TestAllExtensions() extension = unittest_pb2.optional_foreign_message_extension self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) self.assertEqual(0, extendee_proto.Extensions[extension].c) # As with normal (non-extension) fields, merely reading from the # thing shouldn't set the "has" bit. self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) extendee_proto.Extensions[extension].c = 23 self.assertEqual(23, extendee_proto.Extensions[extension].c) self.assertTrue(extendee_proto.HasExtension(extension)) + self.assertIn(extension, extendee_proto.Extensions) # Save a reference here. foreign_message = extendee_proto.Extensions[extension] extendee_proto.ClearExtension(extension) @@ -1219,6 +1293,7 @@ class ReflectionTest(BaseTestCase): self.assertEqual(42, foreign_message.c) self.assertTrue(foreign_message.HasField('c')) self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) # Shouldn't be allowed to do Extensions[extension] = 'a' self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions, extension, 'a') @@ -1246,15 +1321,19 @@ class ReflectionTest(BaseTestCase): # We just test the non-repeated case. self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) required = extendee_proto.Extensions[extension] self.assertEqual(0, required.a) self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) required.a = 23 self.assertEqual(23, extendee_proto.Extensions[extension].a) self.assertTrue(extendee_proto.HasExtension(extension)) + self.assertIn(extension, extendee_proto.Extensions) extendee_proto.ClearExtension(extension) self.assertTrue(required is not extendee_proto.Extensions[extension]) self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) def testRegisteredExtensions(self): pool = unittest_pb2.DESCRIPTOR.pool @@ -1579,6 +1658,8 @@ class ReflectionTest(BaseTestCase): proto1.repeated_int32.append(3) container = copy.deepcopy(proto1.repeated_int32) self.assertEqual([2, 3], container) + container.remove(container[0]) + self.assertEqual([3], container) message1 = proto1.repeated_nested_message.add() message1.bb = 1 @@ -1586,6 +1667,8 @@ class ReflectionTest(BaseTestCase): self.assertEqual(proto1.repeated_nested_message, messages) message1.bb = 2 self.assertNotEqual(proto1.repeated_nested_message, messages) + messages.remove(messages[0]) + self.assertEqual(len(messages), 0) # TODO(anuraag): Implement deepcopy for extension dict @@ -1914,7 +1997,8 @@ class ReflectionTest(BaseTestCase): # into separate TestCase classes. -class TestAllTypesEqualityTest(BaseTestCase): +@testing_refleaks.TestCase +class TestAllTypesEqualityTest(unittest.TestCase): def setUp(self): self.first_proto = unittest_pb2.TestAllTypes() @@ -1930,7 +2014,8 @@ class TestAllTypesEqualityTest(BaseTestCase): self.assertEqual(self.first_proto, self.second_proto) -class FullProtosEqualityTest(BaseTestCase): +@testing_refleaks.TestCase +class FullProtosEqualityTest(unittest.TestCase): """Equality tests using completely-full protos as a starting point.""" @@ -2016,7 +2101,8 @@ class FullProtosEqualityTest(BaseTestCase): self.assertEqual(self.first_proto, self.second_proto) -class ExtensionEqualityTest(BaseTestCase): +@testing_refleaks.TestCase +class ExtensionEqualityTest(unittest.TestCase): def testExtensionEquality(self): first_proto = unittest_pb2.TestAllExtensions() @@ -2049,7 +2135,8 @@ class ExtensionEqualityTest(BaseTestCase): self.assertEqual(first_proto, second_proto) -class MutualRecursionEqualityTest(BaseTestCase): +@testing_refleaks.TestCase +class MutualRecursionEqualityTest(unittest.TestCase): def testEqualityWithMutualRecursion(self): first_proto = unittest_pb2.TestMutualRecursionA() @@ -2061,7 +2148,8 @@ class MutualRecursionEqualityTest(BaseTestCase): self.assertEqual(first_proto, second_proto) -class ByteSizeTest(BaseTestCase): +@testing_refleaks.TestCase +class ByteSizeTest(unittest.TestCase): def setUp(self): self.proto = unittest_pb2.TestAllTypes() @@ -2373,7 +2461,8 @@ class ByteSizeTest(BaseTestCase): # * Handling of empty submessages (with and without "has" # bits set). -class SerializationTest(BaseTestCase): +@testing_refleaks.TestCase +class SerializationTest(unittest.TestCase): def testSerializeEmtpyMessage(self): first_proto = unittest_pb2.TestAllTypes() @@ -2966,7 +3055,8 @@ class SerializationTest(BaseTestCase): self.assertEqual(3, proto.repeated_int32[2]) -class OptionsTest(BaseTestCase): +@testing_refleaks.TestCase +class OptionsTest(unittest.TestCase): def testMessageOptions(self): proto = message_set_extensions_pb2.TestMessageSet() @@ -2993,7 +3083,8 @@ class OptionsTest(BaseTestCase): -class ClassAPITest(BaseTestCase): +@testing_refleaks.TestCase +class ClassAPITest(unittest.TestCase): @unittest.skipIf( api_implementation.Type() == 'cpp' and api_implementation.Version() == 2, diff --git a/python/google/protobuf/internal/test_bad_identifiers.proto b/python/google/protobuf/internal/test_bad_identifiers.proto index c4860ea88a..caf86b5f29 100644 --- a/python/google/protobuf/internal/test_bad_identifiers.proto +++ b/python/google/protobuf/internal/test_bad_identifiers.proto @@ -43,10 +43,10 @@ message TestBadIdentifiers { // Make sure these reasonable extension names don't conflict with internal // variables. extend TestBadIdentifiers { - optional string message = 100 [default="foo"]; - optional string descriptor = 101 [default="bar"]; - optional string reflection = 102 [default="baz"]; - optional string service = 103 [default="qux"]; + optional string message = 100 [default = "foo"]; + optional string descriptor = 101 [default = "bar"]; + optional string reflection = 102 [default = "baz"]; + optional string service = 103 [default = "qux"]; } message AnotherMessage {} diff --git a/python/google/protobuf/internal/testing_refleaks.py b/python/google/protobuf/internal/testing_refleaks.py index 8ce06519bc..e448fceef4 100644 --- a/python/google/protobuf/internal/testing_refleaks.py +++ b/python/google/protobuf/internal/testing_refleaks.py @@ -69,8 +69,8 @@ class LocalTestResult(unittest.TestResult): pass -class ReferenceLeakCheckerTestCase(unittest.TestCase): - """A TestCase which runs tests multiple times, collecting reference counts.""" +class ReferenceLeakCheckerMixin(object): + """A mixin class for TestCase, which checks reference counts.""" NB_RUNS = 3 @@ -81,8 +81,8 @@ class ReferenceLeakCheckerTestCase(unittest.TestCase): self._saved_pickle_registry = copyreg.dispatch_table.copy() # Run the test twice, to warm up the instance attributes. - super(ReferenceLeakCheckerTestCase, self).run(result=result) - super(ReferenceLeakCheckerTestCase, self).run(result=result) + super(ReferenceLeakCheckerMixin, self).run(result=result) + super(ReferenceLeakCheckerMixin, self).run(result=result) oldrefcount = 0 local_result = LocalTestResult(result) @@ -90,7 +90,7 @@ class ReferenceLeakCheckerTestCase(unittest.TestCase): refcount_deltas = [] for _ in range(self.NB_RUNS): oldrefcount = self._getRefcounts() - super(ReferenceLeakCheckerTestCase, self).run(result=local_result) + super(ReferenceLeakCheckerMixin, self).run(result=local_result) newrefcount = self._getRefcounts() refcount_deltas.append(newrefcount - oldrefcount) print(refcount_deltas, self) @@ -112,12 +112,19 @@ class ReferenceLeakCheckerTestCase(unittest.TestCase): if hasattr(sys, 'gettotalrefcount'): - BaseTestCase = ReferenceLeakCheckerTestCase + + def TestCase(test_class): + new_bases = (ReferenceLeakCheckerMixin,) + test_class.__bases__ + new_class = type(test_class)( + test_class.__name__, new_bases, dict(test_class.__dict__)) + return new_class SkipReferenceLeakChecker = unittest.skip else: # When PyDEBUG is not enabled, run the tests normally. - BaseTestCase = unittest.TestCase + + def TestCase(test_class): + return test_class def SkipReferenceLeakChecker(reason): del reason # Don't skip, so don't need a reason. diff --git a/python/google/protobuf/internal/text_format_test.py b/python/google/protobuf/internal/text_format_test.py index c68f42d296..c705dcd768 100755 --- a/python/google/protobuf/internal/text_format_test.py +++ b/python/google/protobuf/internal/text_format_test.py @@ -50,9 +50,11 @@ except ImportError: from google.protobuf import any_pb2 from google.protobuf import any_test_pb2 from google.protobuf import map_unittest_pb2 +from google.protobuf import unittest_custom_options_pb2 from google.protobuf import unittest_mset_pb2 from google.protobuf import unittest_pb2 from google.protobuf import unittest_proto3_arena_pb2 +from google.protobuf import descriptor_pb2 from google.protobuf.internal import any_test_pb2 as test_extend_any from google.protobuf.internal import message_set_extensions_pb2 from google.protobuf.internal import test_util @@ -175,7 +177,10 @@ class TextFormatMessageToStringTests(TextFormatBase): 'repeated_nested_message {\n bb: 32\n}\n' 'repeated_foreign_enum: [FOREIGN_FOO, FOREIGN_BAR, FOREIGN_BAZ]\n') if as_one_line: - expected_ascii = expected_ascii.replace('\n ', '').replace('\n', '') + expected_ascii = expected_ascii.replace('\n', ' ') + expected_ascii = re.sub(r'\s+', ' ', expected_ascii) + expected_ascii = re.sub(r'\s$', '', expected_ascii) + actual_ascii = text_format.MessageToString( message, use_short_repeated_primitives=True, as_one_line=as_one_line) @@ -184,7 +189,7 @@ class TextFormatMessageToStringTests(TextFormatBase): text_format.Parse(actual_ascii, parsed_message) self.assertEqual(parsed_message, message) - def tesPrintShortFormatRepeatedFields(self, message_module, as_one_line): + def testPrintShortFormatRepeatedFields(self, message_module): self.VerifyPrintShortFormatRepeatedFields(message_module, False) self.VerifyPrintShortFormatRepeatedFields(message_module, True) @@ -263,13 +268,6 @@ class TextFormatMessageToStringTests(TextFormatBase): def testPrintFloatFormat(self, message_module): # Check that float_format argument is passed to sub-message formatting. message = message_module.NestedTestAllTypes() - # We use 1.25 as it is a round number in binary. The proto 32-bit float - # will not gain additional imprecise digits as a 64-bit Python float and - # show up in its str. 32-bit 1.2 is noisy when extended to 64-bit: - # >>> struct.unpack('f', struct.pack('f', 1.2))[0] - # 1.2000000476837158 - # >>> struct.unpack('f', struct.pack('f', 1.25))[0] - # 1.25 message.payload.optional_float = 1.25 # Check rounding at 15 significant digits message.payload.optional_double = -.000003456789012345678 @@ -293,6 +291,43 @@ class TextFormatMessageToStringTests(TextFormatBase): self.RemoveRedundantZeros(text_message), 'payload {{ {0} {1} {2} {3} }}'.format(*formatted_fields)) + # 32-bit 1.2 is noisy when extended to 64-bit: + # >>> struct.unpack('f', struct.pack('f', 1.2))[0] + # 1.2000000476837158 + # TODO(jieluo): change to 1.2 with cl/241634942. + message.payload.optional_float = 1.2000000476837158 + formatted_fields = ['optional_float: 1.2', + 'optional_double: -3.45678901234568e-6', + 'repeated_float: -5642', 'repeated_double: 7.89e-5'] + text_message = text_format.MessageToString(message, float_format='.7g', + double_format='.15g') + self.CompareToGoldenText( + self.RemoveRedundantZeros(text_message), + 'payload {{\n {0}\n {1}\n {2}\n {3}\n}}\n'.format( + *formatted_fields)) + + # Test only set float_format affect both float and double fields. + formatted_fields = ['optional_float: 1.2', + 'optional_double: -3.456789e-6', + 'repeated_float: -5642', 'repeated_double: 7.89e-5'] + text_message = text_format.MessageToString(message, float_format='.7g') + self.CompareToGoldenText( + self.RemoveRedundantZeros(text_message), + 'payload {{\n {0}\n {1}\n {2}\n {3}\n}}\n'.format( + *formatted_fields)) + + # Test default float_format has 8 valid digits. + message.payload.optional_float = 1.2345678912 + message.payload.optional_double = 1.2345678912 + formatted_fields = ['optional_float: 1.2345679', + 'optional_double: 1.2345678912', + 'repeated_float: -5642', 'repeated_double: 7.89e-5'] + text_message = text_format.MessageToString(message) + self.CompareToGoldenText( + self.RemoveRedundantZeros(text_message), + 'payload {{\n {0}\n {1}\n {2}\n {3}\n}}\n'.format( + *formatted_fields)) + def testMessageToString(self, message_module): message = message_module.ForeignMessage() message.c = 123 @@ -358,6 +393,63 @@ class TextFormatMessageToStringTests(TextFormatBase): self.assertEqual('0.0', out.getvalue()) out.close() + def testCustomOptions(self, message_module): + message_descriptor = (unittest_custom_options_pb2. + TestMessageWithCustomOptions.DESCRIPTOR) + message_proto = descriptor_pb2.DescriptorProto() + message_descriptor.CopyToProto(message_proto) + expected_text = ( + 'name: "TestMessageWithCustomOptions"\n' + 'field {\n' + ' name: "field1"\n' + ' number: 1\n' + ' label: LABEL_OPTIONAL\n' + ' type: TYPE_STRING\n' + ' options {\n' + ' ctype: CORD\n' + ' [protobuf_unittest.field_opt1]: 8765432109\n' + ' }\n' + '}\n' + 'field {\n' + ' name: "oneof_field"\n' + ' number: 2\n' + ' label: LABEL_OPTIONAL\n' + ' type: TYPE_INT32\n' + ' oneof_index: 0\n' + '}\n' + 'enum_type {\n' + ' name: "AnEnum"\n' + ' value {\n' + ' name: "ANENUM_VAL1"\n' + ' number: 1\n' + ' }\n' + ' value {\n' + ' name: "ANENUM_VAL2"\n' + ' number: 2\n' + ' options {\n' + ' [protobuf_unittest.enum_value_opt1]: 123\n' + ' }\n' + ' }\n' + ' options {\n' + ' [protobuf_unittest.enum_opt1]: -789\n' + ' }\n' + '}\n' + 'options {\n' + ' message_set_wire_format: false\n' + ' [protobuf_unittest.message_opt1]: -56\n' + '}\n' + 'oneof_decl {\n' + ' name: "AnOneof"\n' + ' options {\n' + ' [protobuf_unittest.oneof_opt1]: -99\n' + ' }\n' + '}\n') + self.assertEqual(expected_text, + text_format.MessageToString(message_proto)) + parsed_proto = descriptor_pb2.DescriptorProto() + text_format.Parse(expected_text, parsed_proto) + self.assertEqual(message_proto, parsed_proto) + @_parameterized.parameters(unittest_pb2, unittest_proto3_arena_pb2) class TextFormatMessageToTextBytesTests(TextFormatBase): @@ -435,6 +527,14 @@ class TextFormatParserTests(TextFormatBase): text_format.Parse(text, msg2) self.assertEqual(msg2.optional_string, u'café') + def testParseDoubleToFloat(self, message_module): + message = message_module.TestAllTypes() + text = ('repeated_float: 3.4028235e+39\n' + 'repeated_float: 1.4028235e-39\n') + text_format.Parse(text, message) + self.assertEqual(message.repeated_float[0], float('inf')) + self.assertAlmostEqual(message.repeated_float[1], 1.4028235e-39) + def testParseExotic(self, message_module): message = message_module.TestAllTypes() text = ('repeated_int64: -9223372036854775808\n' @@ -528,6 +628,12 @@ class TextFormatParserTests(TextFormatBase): six.assertRaisesRegex(self, text_format.ParseError, ( r'1:1 : Message type "\w+.TestAllTypes" has no field named ' r'"unknown_field".'), text_format.Parse, text, message) + text = ('optional_int32: 123\n' + 'unknown_field: 8\n' + 'optional_nested_message { bb: 45 }') + text_format.Parse(text, message, allow_unknown_field=True) + self.assertEqual(message.optional_nested_message.bb, 45) + self.assertEqual(message.optional_int32, 123) def testParseBadEnumValue(self, message_module): message = message_module.TestAllTypes() @@ -653,6 +759,24 @@ class TextFormatParserTests(TextFormatBase): self.assertEqual(m.optional_string, self._GOLDEN_UNICODE) self.assertEqual(m.repeated_bytes[0], self._GOLDEN_BYTES) + def testParseDuplicateMessages(self, message_module): + message = message_module.TestAllTypes() + text = ('optional_nested_message { bb: 1 } ' + 'optional_nested_message { bb: 2 }') + six.assertRaisesRegex(self, text_format.ParseError, ( + r'1:59 : Message type "\w+.TestAllTypes" ' + r'should not have multiple "optional_nested_message" fields.'), + text_format.Parse, text, + message) + + def testParseDuplicateScalars(self, message_module): + message = message_module.TestAllTypes() + text = ('optional_int32: 42 ' 'optional_int32: 67') + six.assertRaisesRegex(self, text_format.ParseError, ( + r'1:36 : Message type "\w+.TestAllTypes" should not ' + r'have multiple "optional_int32" fields.'), text_format.Parse, text, + message) + @_parameterized.parameters(unittest_pb2, unittest_proto3_arena_pb2) class TextFormatMergeTests(TextFormatBase): @@ -710,6 +834,39 @@ class OnlyWorksWithProto2RightNowTests(TextFormatBase): self.RemoveRedundantZeros(text_format.MessageToString(message)), 'text_format_unittest_data_oneof_implemented.txt') + def testPrintUnknownFields(self): + message = unittest_pb2.TestAllTypes() + message.optional_int32 = 101 + message.optional_double = 102.0 + message.optional_string = u'hello' + message.optional_bytes = b'103' + message.optionalgroup.a = 104 + message.optional_nested_message.bb = 105 + all_data = message.SerializeToString() + empty_message = unittest_pb2.TestEmptyMessage() + empty_message.ParseFromString(all_data) + self.assertEqual('1: 101\n' + '12: 4636878028842991616\n' + '14: "hello"\n' + '15: "103"\n' + '16 {\n' + ' 17: 104\n' + '}\n' + '18 {\n' + ' 1: 105\n' + '}\n', + text_format.MessageToString(empty_message, + print_unknown_fields=True)) + self.assertEqual('1: 101 ' + '12: 4636878028842991616 ' + '14: "hello" ' + '15: "103" ' + '16 { 17: 104 } ' + '18 { 1: 105 }', + text_format.MessageToString(empty_message, + print_unknown_fields=True, + as_one_line=True)) + def testPrintInIndexOrder(self): message = unittest_pb2.TestFieldOrderings() # Fields are listed in index order instead of field number. @@ -835,6 +992,29 @@ class OnlyWorksWithProto2RightNowTests(TextFormatBase): ' }\n' '}\n') + # In cpp implementation, __str__ calls the cpp implementation of text format. + def testPrintMapUsingCppImplementation(self): + message = map_unittest_pb2.TestMap() + inner_msg = message.map_int32_foreign_message[111] + inner_msg.c = 1 + self.assertEqual( + str(message), + 'map_int32_foreign_message {\n' + ' key: 111\n' + ' value {\n' + ' c: 1\n' + ' }\n' + '}\n') + inner_msg.c = 2 + self.assertEqual( + str(message), + 'map_int32_foreign_message {\n' + ' key: 111\n' + ' value {\n' + ' c: 2\n' + ' }\n' + '}\n') + def testMapOrderEnforcement(self): message = map_unittest_pb2.TestMap() for letter in string.ascii_uppercase[13:26]: @@ -1208,16 +1388,6 @@ class Proto2Tests(TextFormatBase): '"protobuf_unittest.optional_int32_extension" extensions.'), text_format.Parse, text, message) - def testParseDuplicateMessages(self): - message = unittest_pb2.TestAllTypes() - text = ('optional_nested_message { bb: 1 } ' - 'optional_nested_message { bb: 2 }') - six.assertRaisesRegex(self, text_format.ParseError, ( - '1:59 : Message type "protobuf_unittest.TestAllTypes" ' - 'should not have multiple "optional_nested_message" fields.'), - text_format.Parse, text, - message) - def testParseDuplicateExtensionMessages(self): message = unittest_pb2.TestAllExtensions() text = ('[protobuf_unittest.optional_nested_message_extension]: {} ' @@ -1228,14 +1398,6 @@ class Proto2Tests(TextFormatBase): '"protobuf_unittest.optional_nested_message_extension" extensions.'), text_format.Parse, text, message) - def testParseDuplicateScalars(self): - message = unittest_pb2.TestAllTypes() - text = ('optional_int32: 42 ' 'optional_int32: 67') - six.assertRaisesRegex(self, text_format.ParseError, ( - '1:36 : Message type "protobuf_unittest.TestAllTypes" should not ' - 'have multiple "optional_int32" fields.'), text_format.Parse, text, - message) - def testParseGroupNotClosed(self): message = unittest_pb2.TestAllTypes() text = 'RepeatedGroup: <' @@ -1397,6 +1559,24 @@ class Proto3Tests(unittest.TestCase): ' < data: "string" > ' '>') + def testPrintAndParseMessageInvalidAny(self): + packed_message = unittest_pb2.OneString() + packed_message.data = 'string' + message = any_test_pb2.TestAny() + message.any_value.Pack(packed_message) + # Only include string after last '/' in type_url. + message.any_value.type_url = message.any_value.TypeName() + text = text_format.MessageToString(message) + self.assertEqual( + text, 'any_value {\n' + ' type_url: "protobuf_unittest.OneString"\n' + ' value: "\\n\\006string"\n' + '}\n') + + parsed_message = any_test_pb2.TestAny() + text_format.Parse(text, parsed_message) + self.assertEqual(message, parsed_message) + def testUnknownEnums(self): message = unittest_proto3_arena_pb2.TestAllTypes() message2 = unittest_proto3_arena_pb2.TestAllTypes() @@ -1866,5 +2046,88 @@ class PrettyPrinterTest(TextFormatBase): 'repeated_nested_message { My lucky number is 42 } ' 'repeated_nested_message { My lucky number is 99 }')) + out = text_format.TextWriter(False) + text_format.PrintField( + message_module.TestAllTypes.DESCRIPTOR.fields_by_name[ + 'optional_nested_message'], + message.optional_nested_message, + out, + message_formatter=printer) + self.assertEqual( + 'optional_nested_message {\n My lucky number is 1\n}\n', + out.getvalue()) + out.close() + + out = text_format.TextWriter(False) + text_format.PrintFieldValue( + message_module.TestAllTypes.DESCRIPTOR.fields_by_name[ + 'optional_nested_message'], + message.optional_nested_message, + out, + message_formatter=printer) + self.assertEqual( + '{\n My lucky number is 1\n}', + out.getvalue()) + out.close() + + +class WhitespaceTest(TextFormatBase): + + def setUp(self): + self.out = text_format.TextWriter(False) + self.addCleanup(self.out.close) + self.message = unittest_pb2.NestedTestAllTypes() + self.message.child.payload.optional_string = 'value' + self.field = self.message.DESCRIPTOR.fields_by_name['child'] + self.value = self.message.child + + def testMessageToString(self): + self.CompareToGoldenText( + text_format.MessageToString(self.message), + textwrap.dedent("""\ + child { + payload { + optional_string: "value" + } + } + """)) + + def testPrintMessage(self): + text_format.PrintMessage(self.message, self.out) + self.CompareToGoldenText( + self.out.getvalue(), + textwrap.dedent("""\ + child { + payload { + optional_string: "value" + } + } + """)) + + def testPrintField(self): + text_format.PrintField(self.field, self.value, self.out) + self.CompareToGoldenText( + self.out.getvalue(), + textwrap.dedent("""\ + child { + payload { + optional_string: "value" + } + } + """)) + + def testPrintFieldValue(self): + text_format.PrintFieldValue( + self.field, self.value, self.out) + self.CompareToGoldenText( + self.out.getvalue(), + textwrap.dedent("""\ + { + payload { + optional_string: "value" + } + }""")) + + if __name__ == '__main__': unittest.main() diff --git a/python/google/protobuf/internal/type_checkers.py b/python/google/protobuf/internal/type_checkers.py index 0807e7f7d1..ac1fbbf8e1 100755 --- a/python/google/protobuf/internal/type_checkers.py +++ b/python/google/protobuf/internal/type_checkers.py @@ -107,13 +107,18 @@ class TypeChecker(object): message = ('%.1024r has type %s, but expected one of: %s' % (proposed_value, type(proposed_value), self._acceptable_types)) raise TypeError(message) + # Some field types(float, double and bool) accept other types, must + # convert to the correct type in such cases. + if self._acceptable_types: + if self._acceptable_types[0] in (bool, float): + return self._acceptable_types[0](proposed_value) return proposed_value class TypeCheckerWithDefault(TypeChecker): def __init__(self, default_value, *acceptable_types): - TypeChecker.__init__(self, acceptable_types) + TypeChecker.__init__(self, *acceptable_types) self._default_value = default_value def DefaultValue(self): @@ -225,6 +230,41 @@ class Uint64ValueChecker(IntValueChecker): _TYPE = long +# The max 4 bytes float is about 3.4028234663852886e+38 +_FLOAT_MAX = float.fromhex('0x1.fffffep+127') +_FLOAT_MIN = -_FLOAT_MAX +_INF = float('inf') +_NEG_INF = float('-inf') + + +class FloatValueChecker(object): + + """Checker used for float fields. Performs type-check and range check. + + Values exceeding a 32-bit float will be converted to inf/-inf. + """ + + def CheckValue(self, proposed_value): + """Check and convert proposed_value to float.""" + if not isinstance(proposed_value, numbers.Real): + message = ('%.1024r has type %s, but expected one of: numbers.Real' % + (proposed_value, type(proposed_value))) + raise TypeError(message) + converted_value = float(proposed_value) + # This inf rounding matches the C++ proto SafeDoubleToFloat logic. + if converted_value > _FLOAT_MAX: + return _INF + if converted_value < _FLOAT_MIN: + return _NEG_INF + + return converted_value + # TODO(jieluo): convert to 4 bytes float (c style float) at setters: + # return struct.unpack('f', struct.pack('f', converted_value)) + + def DefaultValue(self): + return 0.0 + + # Type-checkers for all scalar CPPTYPEs. _VALUE_CHECKERS = { _FieldDescriptor.CPPTYPE_INT32: Int32ValueChecker(), @@ -232,9 +272,8 @@ _VALUE_CHECKERS = { _FieldDescriptor.CPPTYPE_UINT32: Uint32ValueChecker(), _FieldDescriptor.CPPTYPE_UINT64: Uint64ValueChecker(), _FieldDescriptor.CPPTYPE_DOUBLE: TypeCheckerWithDefault( - 0.0, numbers.Real), - _FieldDescriptor.CPPTYPE_FLOAT: TypeCheckerWithDefault( - 0.0, numbers.Real), + 0.0, float, numbers.Real), + _FieldDescriptor.CPPTYPE_FLOAT: FloatValueChecker(), _FieldDescriptor.CPPTYPE_BOOL: TypeCheckerWithDefault( False, bool, numbers.Integral), _FieldDescriptor.CPPTYPE_STRING: TypeCheckerWithDefault(b'', bytes), diff --git a/python/google/protobuf/internal/unknown_fields_test.py b/python/google/protobuf/internal/unknown_fields_test.py index fceadf718e..bcc4a46d6e 100755 --- a/python/google/protobuf/internal/unknown_fields_test.py +++ b/python/google/protobuf/internal/unknown_fields_test.py @@ -39,6 +39,7 @@ try: import unittest2 as unittest #PY26 except ImportError: import unittest +from google.protobuf import map_unittest_pb2 from google.protobuf import unittest_mset_pb2 from google.protobuf import unittest_pb2 from google.protobuf import unittest_proto3_arena_pb2 @@ -52,10 +53,8 @@ from google.protobuf.internal import type_checkers from google.protobuf import descriptor -BaseTestCase = testing_refleaks.BaseTestCase - - -class UnknownFieldsTest(BaseTestCase): +@testing_refleaks.TestCase +class UnknownFieldsTest(unittest.TestCase): def setUp(self): self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR @@ -138,8 +137,21 @@ class UnknownFieldsTest(BaseTestCase): self.assertEqual( b'', message.repeated_nested_message[0].SerializeToString()) + msg = map_unittest_pb2.TestMap() + msg.map_int32_all_types[1].optional_nested_message.ParseFromString( + other_message.SerializeToString()) + msg.map_string_string['1'] = 'test' + self.assertNotEqual( + b'', + msg.map_int32_all_types[1].optional_nested_message.SerializeToString()) + msg.DiscardUnknownFields() + self.assertEqual( + b'', + msg.map_int32_all_types[1].optional_nested_message.SerializeToString()) + -class UnknownFieldsAccessorsTest(BaseTestCase): +@testing_refleaks.TestCase +class UnknownFieldsAccessorsTest(unittest.TestCase): def setUp(self): self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR @@ -335,7 +347,8 @@ class UnknownFieldsAccessorsTest(BaseTestCase): self.assertEqual(message.SerializeToString(), self.all_fields_data) -class UnknownEnumValuesTest(BaseTestCase): +@testing_refleaks.TestCase +class UnknownEnumValuesTest(unittest.TestCase): def setUp(self): self.descriptor = missing_enum_values_pb2.TestEnumValues.DESCRIPTOR diff --git a/python/google/protobuf/internal/well_known_types.py b/python/google/protobuf/internal/well_known_types.py index 37a65cfab1..7d7fe15036 100644 --- a/python/google/protobuf/internal/well_known_types.py +++ b/python/google/protobuf/internal/well_known_types.py @@ -40,11 +40,18 @@ This files defines well known classes which need extra maintenance including: __author__ = 'jieluo@google.com (Jie Luo)' -import collections +import calendar from datetime import datetime from datetime import timedelta import six +try: + # Since python 3 + import collections.abc as collections_abc +except ImportError: + # Won't work after python 3.8 + import collections as collections_abc + from google.protobuf.descriptor import FieldDescriptor _TIMESTAMPFOMAT = '%Y-%m-%dT%H:%M:%S' @@ -57,17 +64,11 @@ _SECONDS_PER_DAY = 24 * 3600 _DURATION_SECONDS_MAX = 315576000000 -class Error(Exception): - """Top-level module error.""" - - -class ParseError(Error): - """Thrown in case of parsing error.""" - - class Any(object): """Class for Any Message type.""" + __slots__ = () + def Pack(self, msg, type_url_prefix='type.googleapis.com/', deterministic=None): """Packs the specified message into current Any message.""" @@ -92,12 +93,17 @@ class Any(object): def Is(self, descriptor): """Checks if this Any represents the given protobuf type.""" - return self.TypeName() == descriptor.full_name + return '/' in self.type_url and self.TypeName() == descriptor.full_name + + +_EPOCH_DATETIME = datetime.utcfromtimestamp(0) class Timestamp(object): """Class for Timestamp message type.""" + __slots__ = () + def ToJsonString(self): """Converts Timestamp to RFC 3339 date string format. @@ -135,7 +141,7 @@ class Timestamp(object): Example of accepted format: '1972-01-01T10:00:20.021-05:00' Raises: - ParseError: On parsing problems. + ValueError: On parsing problems. """ timezone_offset = value.find('Z') if timezone_offset == -1: @@ -143,7 +149,7 @@ class Timestamp(object): if timezone_offset == -1: timezone_offset = value.rfind('-') if timezone_offset == -1: - raise ParseError( + raise ValueError( 'Failed to parse timestamp: missing valid timezone offset.') time_value = value[0:timezone_offset] # Parse datetime and nanos. @@ -158,7 +164,7 @@ class Timestamp(object): td = date_object - datetime(1970, 1, 1) seconds = td.seconds + td.days * _SECONDS_PER_DAY if len(nano_value) > 9: - raise ParseError( + raise ValueError( 'Failed to parse Timestamp: nanos {0} more than ' '9 fractional digits.'.format(nano_value)) if nano_value: @@ -168,13 +174,13 @@ class Timestamp(object): # Parse timezone offsets. if value[timezone_offset] == 'Z': if len(value) != timezone_offset + 1: - raise ParseError('Failed to parse timestamp: invalid trailing' + raise ValueError('Failed to parse timestamp: invalid trailing' ' data {0}.'.format(value)) else: timezone = value[timezone_offset:] pos = timezone.find(':') if pos == -1: - raise ParseError( + raise ValueError( 'Invalid timezone offset value: {0}.'.format(timezone)) if timezone[0] == '+': seconds -= (int(timezone[1:pos])*60+int(timezone[pos+1:]))*60 @@ -228,19 +234,28 @@ class Timestamp(object): def ToDatetime(self): """Converts Timestamp to datetime.""" - return datetime.utcfromtimestamp( - self.seconds + self.nanos / float(_NANOS_PER_SECOND)) + return _EPOCH_DATETIME + timedelta( + seconds=self.seconds, microseconds=_RoundTowardZero( + self.nanos, _NANOS_PER_MICROSECOND)) def FromDatetime(self, dt): """Converts datetime to Timestamp.""" - td = dt - datetime(1970, 1, 1) - self.seconds = td.seconds + td.days * _SECONDS_PER_DAY - self.nanos = td.microseconds * _NANOS_PER_MICROSECOND + # Using this guide: http://wiki.python.org/moin/WorkingWithTime + # And this conversion guide: http://docs.python.org/library/time.html + + # Turn the date parameter into a tuple (struct_time) that can then be + # manipulated into a long value of seconds. During the conversion from + # struct_time to long, the source date in UTC, and so it follows that the + # correct transformation is calendar.timegm() + self.seconds = calendar.timegm(dt.utctimetuple()) + self.nanos = dt.microsecond * _NANOS_PER_MICROSECOND class Duration(object): """Class for Duration message type.""" + __slots__ = () + def ToJsonString(self): """Converts Duration to string format. @@ -282,10 +297,10 @@ class Duration(object): precision. For example: "1s", "1.01s", "1.0000001s", "-3.100s Raises: - ParseError: On parsing problems. + ValueError: On parsing problems. """ if len(value) < 1 or value[-1] != 's': - raise ParseError( + raise ValueError( 'Duration must end with letter "s": {0}.'.format(value)) try: pos = value.find('.') @@ -301,9 +316,9 @@ class Duration(object): _CheckDurationValid(seconds, nanos) self.seconds = seconds self.nanos = nanos - except ValueError: - raise ParseError( - 'Couldn\'t parse duration: {0}.'.format(value)) + except ValueError as e: + raise ValueError( + 'Couldn\'t parse duration: {0} : {1}.'.format(value, e)) def ToNanoseconds(self): """Converts a Duration to nanoseconds.""" @@ -368,15 +383,15 @@ class Duration(object): def _CheckDurationValid(seconds, nanos): if seconds < -_DURATION_SECONDS_MAX or seconds > _DURATION_SECONDS_MAX: - raise Error( + raise ValueError( 'Duration is not valid: Seconds {0} must be in range ' '[-315576000000, 315576000000].'.format(seconds)) if nanos <= -_NANOS_PER_SECOND or nanos >= _NANOS_PER_SECOND: - raise Error( + raise ValueError( 'Duration is not valid: Nanos {0} must be in range ' '[-999999999, 999999999].'.format(nanos)) if (nanos < 0 and seconds > 0) or (nanos > 0 and seconds < 0): - raise Error( + raise ValueError( 'Duration is not valid: Sign mismatch.') @@ -398,6 +413,8 @@ def _RoundTowardZero(value, divider): class FieldMask(object): """Class for FieldMask message type.""" + __slots__ = () + def ToJsonString(self): """Converts FieldMask to string according to proto3 JSON spec.""" camelcase_paths = [] @@ -408,8 +425,9 @@ class FieldMask(object): def FromJsonString(self, value): """Converts string to FieldMask according to proto3 JSON spec.""" self.Clear() - for path in value.split(','): - self.paths.append(_CamelCaseToSnakeCase(path)) + if value: + for path in value.split(','): + self.paths.append(_CamelCaseToSnakeCase(path)) def IsValidForDescriptor(self, message_descriptor): """Checks whether the FieldMask is valid for Message Descriptor.""" @@ -502,24 +520,26 @@ def _SnakeCaseToCamelCase(path_name): after_underscore = False for c in path_name: if c.isupper(): - raise Error('Fail to print FieldMask to Json string: Path name ' - '{0} must not contain uppercase letters.'.format(path_name)) + raise ValueError( + 'Fail to print FieldMask to Json string: Path name ' + '{0} must not contain uppercase letters.'.format(path_name)) if after_underscore: if c.islower(): result.append(c.upper()) after_underscore = False else: - raise Error('Fail to print FieldMask to Json string: The ' - 'character after a "_" must be a lowercase letter ' - 'in path name {0}.'.format(path_name)) + raise ValueError( + 'Fail to print FieldMask to Json string: The ' + 'character after a "_" must be a lowercase letter ' + 'in path name {0}.'.format(path_name)) elif c == '_': after_underscore = True else: result += c if after_underscore: - raise Error('Fail to print FieldMask to Json string: Trailing "_" ' - 'in path name {0}.'.format(path_name)) + raise ValueError('Fail to print FieldMask to Json string: Trailing "_" ' + 'in path name {0}.'.format(path_name)) return ''.join(result) @@ -528,7 +548,7 @@ def _CamelCaseToSnakeCase(path_name): result = [] for c in path_name: if c == '_': - raise ParseError('Fail to parse FieldMask: Path name ' + raise ValueError('Fail to parse FieldMask: Path name ' '{0} must not contain "_"s.'.format(path_name)) if c.isupper(): result += '_' @@ -551,6 +571,8 @@ class _FieldMaskTree(object): In the tree, each leaf node represents a field path. """ + __slots__ = ('_root',) + def __init__(self, field_mask=None): """Initializes the tree by FieldMask.""" self._root = {} @@ -662,11 +684,7 @@ def _MergeMessage( destination.ClearField(_StrConvert(name)) repeated_source = getattr(source, name) repeated_destination = getattr(destination, name) - if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE: - for item in repeated_source: - repeated_destination.add().MergeFrom(item) - else: - repeated_destination.extend(repeated_source) + repeated_destination.MergeFrom(repeated_source) else: if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE: if replace_message: @@ -679,7 +697,7 @@ def _MergeMessage( def _AddFieldPaths(node, prefix, field_mask): """Adds the field paths descended from node to field_mask.""" - if not node: + if not node and prefix: field_mask.paths.append(prefix) return for name in sorted(node): @@ -735,7 +753,7 @@ def _GetStructValue(struct_value): class Struct(object): """Class for Struct message type.""" - __slots__ = [] + __slots__ = () def __getitem__(self, key): return _GetStructValue(self.fields[key]) @@ -782,12 +800,14 @@ class Struct(object): for key, value in dictionary.items(): _SetStructValue(self.fields[key], value) -collections.MutableMapping.register(Struct) +collections_abc.MutableMapping.register(Struct) class ListValue(object): """Class for ListValue message type.""" + __slots__ = () + def __len__(self): return len(self.values) @@ -826,7 +846,7 @@ class ListValue(object): list_value.Clear() return list_value -collections.MutableSequence.register(ListValue) +collections_abc.MutableSequence.register(ListValue) WKTBASES = { diff --git a/python/google/protobuf/internal/well_known_types_test.py b/python/google/protobuf/internal/well_known_types_test.py index 965940b261..61b41ec523 100644 --- a/python/google/protobuf/internal/well_known_types_test.py +++ b/python/google/protobuf/internal/well_known_types_test.py @@ -34,8 +34,14 @@ __author__ = 'jieluo@google.com (Jie Luo)' -import collections -from datetime import datetime +import datetime + +try: + # Since python 3 + import collections.abc as collections_abc +except ImportError: + # Won't work after python 3.8 + import collections as collections_abc try: import unittest2 as unittest #PY26 @@ -47,6 +53,7 @@ from google.protobuf import duration_pb2 from google.protobuf import field_mask_pb2 from google.protobuf import struct_pb2 from google.protobuf import timestamp_pb2 +from google.protobuf import map_unittest_pb2 from google.protobuf import unittest_pb2 from google.protobuf.internal import any_test_pb2 from google.protobuf.internal import test_util @@ -240,14 +247,42 @@ class TimeUtilTest(TimeUtilTestBase): def testDatetimeConverison(self): message = timestamp_pb2.Timestamp() - dt = datetime(1970, 1, 1) + dt = datetime.datetime(1970, 1, 1) message.FromDatetime(dt) self.assertEqual(dt, message.ToDatetime()) message.FromMilliseconds(1999) - self.assertEqual(datetime(1970, 1, 1, 0, 0, 1, 999000), + self.assertEqual(datetime.datetime(1970, 1, 1, 0, 0, 1, 999000), message.ToDatetime()) + dt = datetime.datetime(2555, 2, 22, 1, 2, 3, 456789) + message.FromDatetime(dt) + self.assertEqual(dt, message.ToDatetime()) + + dt = datetime.datetime.max + message.FromDatetime(dt) + self.assertEqual(dt, message.ToDatetime()) + + def testDatetimeConversionWithTimezone(self): + class TZ(datetime.tzinfo): + + def utcoffset(self, _): + return datetime.timedelta(hours=1) + + def dst(self, _): + return datetime.timedelta(0) + + def tzname(self, _): + return 'UTC+1' + + message1 = timestamp_pb2.Timestamp() + dt = datetime.datetime(1970, 1, 1, 1, tzinfo=TZ()) + message1.FromDatetime(dt) + message2 = timestamp_pb2.Timestamp() + dt = datetime.datetime(1970, 1, 1, 0) + message2.FromDatetime(dt) + self.assertEqual(message1, message2) + def testTimedeltaConversion(self): message = duration_pb2.Duration() message.FromNanoseconds(1999999999) @@ -273,12 +308,12 @@ class TimeUtilTest(TimeUtilTestBase): def testInvalidTimestamp(self): message = timestamp_pb2.Timestamp() self.assertRaisesRegexp( - well_known_types.ParseError, + ValueError, 'Failed to parse timestamp: missing valid timezone offset.', message.FromJsonString, '') self.assertRaisesRegexp( - well_known_types.ParseError, + ValueError, 'Failed to parse timestamp: invalid trailing data ' '1970-01-01T00:00:01Ztrail.', message.FromJsonString, @@ -289,12 +324,12 @@ class TimeUtilTest(TimeUtilTestBase): ' format \'%Y-%m-%dT%H:%M:%S\'', message.FromJsonString, '10000-01-01T00:00:00.00Z') self.assertRaisesRegexp( - well_known_types.ParseError, + ValueError, 'nanos 0123456789012 more than 9 fractional digits.', message.FromJsonString, '1970-01-01T00:00:00.0123456789012Z') self.assertRaisesRegexp( - well_known_types.ParseError, + ValueError, (r'Invalid timezone offset value: \+08.'), message.FromJsonString, '1972-01-01T01:00:00.01+08',) @@ -312,43 +347,43 @@ class TimeUtilTest(TimeUtilTestBase): def testInvalidDuration(self): message = duration_pb2.Duration() self.assertRaisesRegexp( - well_known_types.ParseError, + ValueError, 'Duration must end with letter "s": 1.', message.FromJsonString, '1') self.assertRaisesRegexp( - well_known_types.ParseError, + ValueError, 'Couldn\'t parse duration: 1...2s.', message.FromJsonString, '1...2s') text = '-315576000001.000000000s' self.assertRaisesRegexp( - well_known_types.Error, + ValueError, r'Duration is not valid\: Seconds -315576000001 must be in range' r' \[-315576000000\, 315576000000\].', message.FromJsonString, text) text = '315576000001.000000000s' self.assertRaisesRegexp( - well_known_types.Error, + ValueError, r'Duration is not valid\: Seconds 315576000001 must be in range' r' \[-315576000000\, 315576000000\].', message.FromJsonString, text) message.seconds = -315576000001 message.nanos = 0 self.assertRaisesRegexp( - well_known_types.Error, + ValueError, r'Duration is not valid\: Seconds -315576000001 must be in range' r' \[-315576000000\, 315576000000\].', message.ToJsonString) message.seconds = 0 message.nanos = 999999999 + 1 self.assertRaisesRegexp( - well_known_types.Error, + ValueError, r'Duration is not valid\: Nanos 1000000000 must be in range' r' \[-999999999\, 999999999\].', message.ToJsonString) message.seconds = -1 message.nanos = 1 self.assertRaisesRegexp( - well_known_types.Error, + ValueError, r'Duration is not valid\: Sign mismatch.', message.ToJsonString) @@ -379,6 +414,7 @@ class FieldMaskTest(unittest.TestCase): mask.FromJsonString('') self.assertEqual('', mask.ToJsonString()) + self.assertEqual([], mask.paths) mask.FromJsonString('fooBar') self.assertEqual(['foo_bar'], mask.paths) mask.FromJsonString('fooBar,barQuz') @@ -491,6 +527,8 @@ class FieldMaskTest(unittest.TestCase): mask2.FromJsonString('bar,quz') out_mask.Intersect(mask1, mask2) self.assertEqual('', out_mask.ToJsonString()) + self.assertEqual(len(out_mask.paths), 0) + self.assertEqual(out_mask.paths, []) # Overlap with duplicated paths. mask1.FromJsonString('foo,baz.bb') mask2.FromJsonString('baz.bb,quz') @@ -505,8 +543,17 @@ class FieldMaskTest(unittest.TestCase): mask2.FromJsonString('foo.bar.baz,quz') out_mask.Intersect(mask1, mask2) self.assertEqual('foo.bar.baz', out_mask.ToJsonString()) + # Intersect '' with '' + mask1.Clear() + mask2.Clear() + mask1.paths.append('') + mask2.paths.append('') + self.assertEqual(mask1.paths, ['']) + self.assertEqual('', mask1.ToJsonString()) + out_mask.Intersect(mask1, mask2) + self.assertEqual(out_mask.paths, []) - def testMergeMessage(self): + def testMergeMessageWithoutMapFields(self): # Test merge one field. src = unittest_pb2.TestAllTypes() test_util.SetAllFields(src) @@ -615,6 +662,29 @@ class FieldMaskTest(unittest.TestCase): self.assertTrue(dst.HasField('foo_message')) self.assertFalse(dst.HasField('foo_lazy_message')) + def testMergeMessageWithMapField(self): + empty_map = map_unittest_pb2.TestRecursiveMapMessage() + src_level_2 = map_unittest_pb2.TestRecursiveMapMessage() + src_level_2.a['src level 2'].CopyFrom(empty_map) + src = map_unittest_pb2.TestRecursiveMapMessage() + src.a['common key'].CopyFrom(src_level_2) + src.a['src level 1'].CopyFrom(src_level_2) + + dst_level_2 = map_unittest_pb2.TestRecursiveMapMessage() + dst_level_2.a['dst level 2'].CopyFrom(empty_map) + dst = map_unittest_pb2.TestRecursiveMapMessage() + dst.a['common key'].CopyFrom(dst_level_2) + dst.a['dst level 1'].CopyFrom(empty_map) + + mask = field_mask_pb2.FieldMask() + mask.FromJsonString('a') + mask.MergeMessage(src, dst) + + # map from dst is replaced with map from src. + self.assertEqual(dst.a['common key'], src_level_2) + self.assertEqual(dst.a['src level 1'], src_level_2) + self.assertEqual(dst.a['dst level 1'], empty_map) + def testMergeErrors(self): src = unittest_pb2.TestAllTypes() dst = unittest_pb2.TestAllTypes() @@ -638,7 +708,7 @@ class FieldMaskTest(unittest.TestCase): # No uppercase letter is allowed. self.assertRaisesRegexp( - well_known_types.Error, + ValueError, 'Fail to print FieldMask to Json string: Path name Foo must ' 'not contain uppercase letters.', well_known_types._SnakeCaseToCamelCase, @@ -648,19 +718,19 @@ class FieldMaskTest(unittest.TestCase): # 2. "_" cannot be followed by a digit. # 3. "_" cannot appear as the last character. self.assertRaisesRegexp( - well_known_types.Error, + ValueError, 'Fail to print FieldMask to Json string: The character after a ' '"_" must be a lowercase letter in path name foo__bar.', well_known_types._SnakeCaseToCamelCase, 'foo__bar') self.assertRaisesRegexp( - well_known_types.Error, + ValueError, 'Fail to print FieldMask to Json string: The character after a ' '"_" must be a lowercase letter in path name foo_3bar.', well_known_types._SnakeCaseToCamelCase, 'foo_3bar') self.assertRaisesRegexp( - well_known_types.Error, + ValueError, 'Fail to print FieldMask to Json string: Trailing "_" in path ' 'name foo_bar_.', well_known_types._SnakeCaseToCamelCase, @@ -674,7 +744,7 @@ class FieldMaskTest(unittest.TestCase): self.assertEqual('foo3_bar', well_known_types._CamelCaseToSnakeCase('foo3Bar')) self.assertRaisesRegexp( - well_known_types.ParseError, + ValueError, 'Fail to parse FieldMask: Path name foo_bar must not contain "_"s.', well_known_types._CamelCaseToSnakeCase, 'foo_bar') @@ -684,7 +754,7 @@ class StructTest(unittest.TestCase): def testStruct(self): struct = struct_pb2.Struct() - self.assertIsInstance(struct, collections.Mapping) + self.assertIsInstance(struct, collections_abc.Mapping) self.assertEqual(0, len(struct)) struct_class = struct.__class__ @@ -693,7 +763,7 @@ class StructTest(unittest.TestCase): struct['key3'] = True struct.get_or_create_struct('key4')['subkey'] = 11.0 struct_list = struct.get_or_create_list('key5') - self.assertIsInstance(struct_list, collections.Sequence) + self.assertIsInstance(struct_list, collections_abc.Sequence) struct_list.extend([6, 'seven', True, False, None]) struct_list.add_struct()['subkey2'] = 9 struct['key6'] = {'subkey': {}} @@ -879,6 +949,17 @@ class AnyTest(unittest.TestCase): raise AttributeError('%s should not have Pack method.' % msg_descriptor.full_name) + def testUnpackWithNoSlashInTypeUrl(self): + msg = any_test_pb2.TestAny() + all_types = unittest_pb2.TestAllTypes() + all_descriptor = all_types.DESCRIPTOR + msg.value.Pack(all_types) + # Reset type_url to part of type_url after '/' + msg.value.type_url = msg.value.TypeName() + self.assertFalse(msg.value.Is(all_descriptor)) + unpacked_message = unittest_pb2.TestAllTypes() + self.assertFalse(msg.value.Unpack(unpacked_message)) + def testMessageName(self): # Creates and sets message. submessage = any_test_pb2.TestAny() diff --git a/python/google/protobuf/json_format.py b/python/google/protobuf/json_format.py index ce1db7d7b4..6c3b1cb8cb 100644 --- a/python/google/protobuf/json_format.py +++ b/python/google/protobuf/json_format.py @@ -96,12 +96,14 @@ class ParseError(Error): """Thrown in case of parsing error.""" -def MessageToJson(message, - including_default_value_fields=False, - preserving_proto_field_name=False, - indent=2, - sort_keys=False, - use_integers_for_enums=False): +def MessageToJson( + message, + including_default_value_fields=False, + preserving_proto_field_name=False, + indent=2, + sort_keys=False, + use_integers_for_enums=False, + descriptor_pool=None): """Converts protobuf message to JSON format. Args: @@ -117,20 +119,26 @@ def MessageToJson(message, An indent level of 0 or negative will only insert newlines. sort_keys: If True, then the output will be sorted by field names. use_integers_for_enums: If true, print integers instead of enum names. + descriptor_pool: A Descriptor Pool for resolving types. If None use the + default. Returns: A string containing the JSON formatted protocol buffer message. """ - printer = _Printer(including_default_value_fields, - preserving_proto_field_name, - use_integers_for_enums) + printer = _Printer( + including_default_value_fields, + preserving_proto_field_name, + use_integers_for_enums, + descriptor_pool) return printer.ToJsonString(message, indent, sort_keys) -def MessageToDict(message, - including_default_value_fields=False, - preserving_proto_field_name=False, - use_integers_for_enums=False): +def MessageToDict( + message, + including_default_value_fields=False, + preserving_proto_field_name=False, + use_integers_for_enums=False, + descriptor_pool=None): """Converts protobuf message to a dictionary. When the dictionary is encoded to JSON, it conforms to proto3 JSON spec. @@ -145,13 +153,17 @@ def MessageToDict(message, names as defined in the .proto file. If False, convert the field names to lowerCamelCase. use_integers_for_enums: If true, print integers instead of enum names. + descriptor_pool: A Descriptor Pool for resolving types. If None use the + default. Returns: A dict representation of the protocol buffer message. """ - printer = _Printer(including_default_value_fields, - preserving_proto_field_name, - use_integers_for_enums) + printer = _Printer( + including_default_value_fields, + preserving_proto_field_name, + use_integers_for_enums, + descriptor_pool) # pylint: disable=protected-access return printer._MessageToJsonObject(message) @@ -165,13 +177,16 @@ def _IsMapEntry(field): class _Printer(object): """JSON format printer for protocol message.""" - def __init__(self, - including_default_value_fields=False, - preserving_proto_field_name=False, - use_integers_for_enums=False): + def __init__( + self, + including_default_value_fields=False, + preserving_proto_field_name=False, + use_integers_for_enums=False, + descriptor_pool=None): self.including_default_value_fields = including_default_value_fields self.preserving_proto_field_name = preserving_proto_field_name self.use_integers_for_enums = use_integers_for_enums + self.descriptor_pool = descriptor_pool def ToJsonString(self, message, indent, sort_keys): js = self._MessageToJsonObject(message) @@ -218,12 +233,8 @@ class _Printer(object): js[name] = [self._FieldToJsonObject(field, k) for k in value] elif field.is_extension: - f = field - if (f.containing_type.GetOptions().message_set_wire_format and - f.type == descriptor.FieldDescriptor.TYPE_MESSAGE and - f.label == descriptor.FieldDescriptor.LABEL_OPTIONAL): - f = f.message_type - name = '[%s.%s]' % (f.full_name, name) + full_qualifier = field.full_name[:-len(field.name)] + name = '[%s%s]' % (full_qualifier, name) js[name] = self._FieldToJsonObject(field, value) else: js[name] = self._FieldToJsonObject(field, value) @@ -300,7 +311,7 @@ class _Printer(object): js = OrderedDict() type_url = message.type_url js['@type'] = type_url - sub_message = _CreateMessageFromTypeUrl(type_url) + sub_message = _CreateMessageFromTypeUrl(type_url, self.descriptor_pool) sub_message.ParseFromString(message.value) message_descriptor = sub_message.DESCRIPTOR full_name = message_descriptor.full_name @@ -366,13 +377,13 @@ def _DuplicateChecker(js): return result -def _CreateMessageFromTypeUrl(type_url): - # TODO(jieluo): Should add a way that users can register the type resolver - # instead of the default one. +def _CreateMessageFromTypeUrl(type_url, descriptor_pool): + """Creates a message from a type URL.""" db = symbol_database.Default() + pool = db.pool if descriptor_pool is None else descriptor_pool type_name = type_url.split('/')[-1] try: - message_descriptor = db.pool.FindMessageTypeByName(type_name) + message_descriptor = pool.FindMessageTypeByName(type_name) except KeyError: raise TypeError( 'Can not find message descriptor by type_url: {0}.'.format(type_url)) @@ -380,13 +391,15 @@ def _CreateMessageFromTypeUrl(type_url): return message_class() -def Parse(text, message, ignore_unknown_fields=False): +def Parse(text, message, ignore_unknown_fields=False, descriptor_pool=None): """Parses a JSON representation of a protocol message into a message. Args: text: Message JSON representation. message: A protocol buffer message to merge into. ignore_unknown_fields: If True, do not raise errors for unknown fields. + descriptor_pool: A Descriptor Pool for resolving types. If None use the + default. Returns: The same message passed as argument. @@ -399,21 +412,26 @@ def Parse(text, message, ignore_unknown_fields=False): js = json.loads(text, object_pairs_hook=_DuplicateChecker) except ValueError as e: raise ParseError('Failed to load JSON: {0}.'.format(str(e))) - return ParseDict(js, message, ignore_unknown_fields) + return ParseDict(js, message, ignore_unknown_fields, descriptor_pool) -def ParseDict(js_dict, message, ignore_unknown_fields=False): +def ParseDict(js_dict, + message, + ignore_unknown_fields=False, + descriptor_pool=None): """Parses a JSON dictionary representation into a message. Args: js_dict: Dict representation of a JSON message. message: A protocol buffer message to merge into. ignore_unknown_fields: If True, do not raise errors for unknown fields. + descriptor_pool: A Descriptor Pool for resolving types. If None use the + default. Returns: The same message passed as argument. """ - parser = _Parser(ignore_unknown_fields) + parser = _Parser(ignore_unknown_fields, descriptor_pool) parser.ConvertMessage(js_dict, message) return message @@ -424,9 +442,9 @@ _INT_OR_FLOAT = six.integer_types + (float,) class _Parser(object): """JSON format parser for protocol message.""" - def __init__(self, - ignore_unknown_fields): + def __init__(self, ignore_unknown_fields, descriptor_pool): self.ignore_unknown_fields = ignore_unknown_fields + self.descriptor_pool = descriptor_pool def ConvertMessage(self, value, message): """Convert a JSON object into a message. @@ -471,10 +489,16 @@ class _Parser(object): raise ParseError('Message type {0} does not have extensions'.format( message_descriptor.full_name)) identifier = name[1:-1] # strip [] brackets - identifier = '.'.join(identifier.split('.')[:-1]) # pylint: disable=protected-access field = message.Extensions._FindExtensionByName(identifier) # pylint: enable=protected-access + if not field: + # Try looking for extension by the message type name, dropping the + # field name following the final . separator in full_name. + identifier = '.'.join(identifier.split('.')[:-1]) + # pylint: disable=protected-access + field = message.Extensions._FindExtensionByName(identifier) + # pylint: enable=protected-access if not field: if self.ignore_unknown_fields: continue @@ -542,10 +566,13 @@ class _Parser(object): sub_message.SetInParent() self.ConvertMessage(value, sub_message) else: - setattr(message, field.name, _ConvertScalarFieldValue(value, field)) + if field.is_extension: + message.Extensions[field] = _ConvertScalarFieldValue(value, field) + else: + setattr(message, field.name, _ConvertScalarFieldValue(value, field)) except ParseError as e: if field and field.containing_oneof is None: - raise ParseError('Failed to parse {0} field: {1}'.format(name, e)) + raise ParseError('Failed to parse {0} field: {1}.'.format(name, e)) else: raise ParseError(str(e)) except ValueError as e: @@ -562,7 +589,7 @@ class _Parser(object): except KeyError: raise ParseError('@type is missing when parsing any message.') - sub_message = _CreateMessageFromTypeUrl(type_url) + sub_message = _CreateMessageFromTypeUrl(type_url, self.descriptor_pool) message_descriptor = sub_message.DESCRIPTOR full_name = message_descriptor.full_name if _IsWrapperMessage(message_descriptor): @@ -573,6 +600,7 @@ class _Parser(object): else: del value['@type'] self._ConvertFieldValuePair(value, sub_message) + value['@type'] = type_url # Sets Any message message.value = sub_message.SerializeToString() message.type_url = type_url @@ -581,7 +609,10 @@ class _Parser(object): """Convert a JSON representation into message with FromJsonString.""" # Duration, Timestamp, FieldMask have a FromJsonString method to do the # conversion. Users can also call the method directly. - message.FromJsonString(value) + try: + message.FromJsonString(value) + except ValueError as e: + raise ParseError(e) def _ConvertValueMessage(self, value, message): """Convert a JSON representation into Value message.""" @@ -614,6 +645,9 @@ class _Parser(object): if not isinstance(value, dict): raise ParseError( 'Struct must be in a dict which is {0}.'.format(value)) + # Clear will mark the struct as modified so it will be created even if + # there are no values. + message.Clear() for key in value: self._ConvertValueMessage(value[key], message.fields[key]) return diff --git a/python/google/protobuf/message.py b/python/google/protobuf/message.py index eca2e0a901..35ceccb58e 100755 --- a/python/google/protobuf/message.py +++ b/python/google/protobuf/message.py @@ -172,17 +172,19 @@ class Message(object): we *do* stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information. + + Raises: + message.DecodeError if the input cannot be parsed. """ raise NotImplementedError def ParseFromString(self, serialized): """Parse serialized protocol buffer data into this message. - Like MergeFromString(), except we clear the object first and - do not return the value that MergeFromString returns. + Like MergeFromString(), except we clear the object first. """ self.Clear() - self.MergeFromString(serialized) + return self.MergeFromString(serialized) def SerializeToString(self, **kwargs): """Serializes the protocol message to a binary string. @@ -309,4 +311,9 @@ class Message(object): def __setstate__(self, state): """Support the pickle protocol.""" self.__init__() - self.ParseFromString(state['serialized']) + serialized = state['serialized'] + # On Python 3, using encoding='latin1' is required for unpickling + # protos pickled by Python 2. + if not isinstance(serialized, bytes): + serialized = serialized.encode('latin1') + self.ParseFromString(serialized) diff --git a/python/google/protobuf/proto_api.h b/python/google/protobuf/proto_api.h index 47edf0ea1b..75ee9795ae 100644 --- a/python/google/protobuf/proto_api.h +++ b/python/google/protobuf/proto_api.h @@ -47,6 +47,7 @@ #include +#include #include namespace google { @@ -76,6 +77,11 @@ struct PyProto_API { // pointing to the message, like submessages or repeated containers. // With the current implementation, only empty messages are in this case. virtual Message* GetMutableMessagePointer(PyObject* msg) const = 0; + + // Expose the underlying DescriptorPool and MessageFactory to enable C++ code + // to create Python-compatible message. + virtual const DescriptorPool* GetDefaultDescriptorPool() const = 0; + virtual MessageFactory* GetDefaultMessageFactory() const = 0; }; inline const char* PyProtoAPICapsuleName() { diff --git a/python/google/protobuf/pyext/descriptor.cc b/python/google/protobuf/pyext/descriptor.cc index 3cb16b7438..1637f83a30 100644 --- a/python/google/protobuf/pyext/descriptor.cc +++ b/python/google/protobuf/pyext/descriptor.cc @@ -189,6 +189,21 @@ const FileDescriptor* GetFileDescriptor(const MethodDescriptor* descriptor) { return descriptor->service()->file(); } +bool Reparse( + PyMessageFactory* message_factory, const Message& from, Message* to) { + // Reparse message. + string serialized; + from.SerializeToString(&serialized); + io::CodedInputStream input( + reinterpret_cast(serialized.c_str()), serialized.size()); + input.SetExtensionRegistry(message_factory->pool->pool, + message_factory->message_factory); + bool success = to->ParseFromCodedStream(&input); + if (!success) { + return false; + } + return true; +} // Converts options into a Python protobuf, and cache the result. // // This is a bit tricky because options can contain extension fields defined in @@ -234,6 +249,7 @@ static PyObject* GetOrBuildOptions(const DescriptorClass *descriptor) { } ScopedPyObjectPtr value( PyEval_CallObject(message_class->AsPyObject(), NULL)); + Py_DECREF(message_class); if (value == NULL) { return NULL; } @@ -251,15 +267,8 @@ static PyObject* GetOrBuildOptions(const DescriptorClass *descriptor) { cmsg->message->CopyFrom(options); } else { // Reparse options string! XXX call cmessage::MergeFromString - string serialized; - options.SerializeToString(&serialized); - io::CodedInputStream input( - reinterpret_cast(serialized.c_str()), serialized.size()); - input.SetExtensionRegistry(message_factory->pool->pool, - message_factory->message_factory); - bool success = cmsg->message->MergePartialFromCodedStream(&input); - if (!success) { - PyErr_Format(PyExc_ValueError, "Error parsing Options message"); + if (!Reparse(message_factory, options, cmsg->message)) { + PyErr_Format(PyExc_ValueError, "Error reparsing Options message"); return NULL; } } @@ -290,6 +299,16 @@ static PyObject* CopyToPythonProto(const DescriptorClass *descriptor, DescriptorProtoClass* descriptor_message = static_cast(message->message); descriptor->CopyTo(descriptor_message); + // Custom options might in unknown extensions. Reparse + // the descriptor_message. Can't skip reparse when options unknown + // fields is empty, because they might in sub descriptors' options. + PyMessageFactory* message_factory = + GetDefaultDescriptorPool()->py_message_factory; + if (!Reparse(message_factory, *descriptor_message, descriptor_message)) { + PyErr_Format(PyExc_ValueError, "Error reparsing descriptor message"); + return nullptr; + } + Py_RETURN_NONE; } @@ -345,7 +364,7 @@ PyObject* NewInternedDescriptor(PyTypeObject* type, return it->second; } // Create a new descriptor object - PyBaseDescriptor* py_descriptor = PyObject_New( + PyBaseDescriptor* py_descriptor = PyObject_GC_New( PyBaseDescriptor, type); if (py_descriptor == NULL) { return NULL; @@ -367,17 +386,32 @@ PyObject* NewInternedDescriptor(PyTypeObject* type, Py_INCREF(pool); py_descriptor->pool = pool; + PyObject_GC_Track(py_descriptor); + if (was_created) { *was_created = true; } return reinterpret_cast(py_descriptor); } -static void Dealloc(PyBaseDescriptor* self) { +static void Dealloc(PyObject* pself) { + PyBaseDescriptor* self = reinterpret_cast(pself); // Remove from interned dictionary interned_descriptors->erase(self->descriptor); Py_CLEAR(self->pool); - Py_TYPE(self)->tp_free(reinterpret_cast(self)); + Py_TYPE(self)->tp_free(pself); +} + +static int GcTraverse(PyObject* pself, visitproc visit, void* arg) { + PyBaseDescriptor* self = reinterpret_cast(pself); + Py_VISIT(self->pool); + return 0; +} + +static int GcClear(PyObject* pself) { + PyBaseDescriptor* self = reinterpret_cast(pself); + Py_CLEAR(self->pool); + return 0; } static PyGetSetDef Getters[] = { @@ -385,36 +419,36 @@ static PyGetSetDef Getters[] = { }; PyTypeObject PyBaseDescriptor_Type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - FULL_MODULE_NAME ".DescriptorBase", // tp_name - sizeof(PyBaseDescriptor), // tp_basicsize - 0, // tp_itemsize - (destructor)Dealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - 0, // tp_as_mapping - 0, // tp_hash - 0, // tp_call - 0, // tp_str - 0, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags - "Descriptors base class", // tp_doc - 0, // tp_traverse - 0, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - 0, // tp_methods - 0, // tp_members - Getters, // tp_getset + PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME + ".DescriptorBase", // tp_name + sizeof(PyBaseDescriptor), // tp_basicsize + 0, // tp_itemsize + (destructor)Dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags + "Descriptors base class", // tp_doc + GcTraverse, // tp_traverse + GcClear, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + 0, // tp_methods + 0, // tp_members + Getters, // tp_getset }; } // namespace descriptor @@ -1290,7 +1324,7 @@ static const FileDescriptor* _GetDescriptor(PyFileDescriptor *self) { static void Dealloc(PyFileDescriptor* self) { Py_XDECREF(self->serialized_pb); - descriptor::Dealloc(&self->base); + descriptor::Dealloc(reinterpret_cast(self)); } static PyObject* GetPool(PyFileDescriptor *self, void *closure) { @@ -1418,45 +1452,45 @@ static PyMethodDef Methods[] = { } // namespace file_descriptor PyTypeObject PyFileDescriptor_Type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - FULL_MODULE_NAME ".FileDescriptor", // tp_name - sizeof(PyFileDescriptor), // tp_basicsize - 0, // tp_itemsize - (destructor)file_descriptor::Dealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - 0, // tp_as_mapping - 0, // tp_hash - 0, // tp_call - 0, // tp_str - 0, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags - "A File Descriptor", // tp_doc - 0, // tp_traverse - 0, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - file_descriptor::Methods, // tp_methods - 0, // tp_members - file_descriptor::Getters, // tp_getset - &descriptor::PyBaseDescriptor_Type, // tp_base - 0, // tp_dict - 0, // tp_descr_get - 0, // tp_descr_set - 0, // tp_dictoffset - 0, // tp_init - 0, // tp_alloc - 0, // tp_new - PyObject_Del, // tp_free + PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME + ".FileDescriptor", // tp_name + sizeof(PyFileDescriptor), // tp_basicsize + 0, // tp_itemsize + (destructor)file_descriptor::Dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT, // tp_flags + "A File Descriptor", // tp_doc + 0, // tp_traverse + 0, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + file_descriptor::Methods, // tp_methods + 0, // tp_members + file_descriptor::Getters, // tp_getset + &descriptor::PyBaseDescriptor_Type, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init + 0, // tp_alloc + 0, // tp_new + PyObject_GC_Del, // tp_free }; PyObject* PyFileDescriptor_FromDescriptor( @@ -1846,15 +1880,6 @@ PyObject* PyMethodDescriptor_FromDescriptor( &PyMethodDescriptor_Type, method_descriptor, NULL); } -const MethodDescriptor* PyMethodDescriptor_AsDescriptor(PyObject* obj) { - if (!PyObject_TypeCheck(obj, &PyMethodDescriptor_Type)) { - PyErr_SetString(PyExc_TypeError, "Not a MethodDescriptor"); - return NULL; - } - return reinterpret_cast( - reinterpret_cast(obj)->descriptor); -} - // Add a enum values to a type dictionary. static bool AddEnumValues(PyTypeObject *type, const EnumDescriptor* enum_descriptor) { diff --git a/python/google/protobuf/pyext/descriptor.h b/python/google/protobuf/pyext/descriptor.h index c4dde9e7dc..327ba4b3ce 100644 --- a/python/google/protobuf/pyext/descriptor.h +++ b/python/google/protobuf/pyext/descriptor.h @@ -81,7 +81,6 @@ const FieldDescriptor* PyFieldDescriptor_AsDescriptor(PyObject* obj); const EnumDescriptor* PyEnumDescriptor_AsDescriptor(PyObject* obj); const FileDescriptor* PyFileDescriptor_AsDescriptor(PyObject* obj); const ServiceDescriptor* PyServiceDescriptor_AsDescriptor(PyObject* obj); -const MethodDescriptor* PyMethodDescriptor_AsDescriptor(PyObject* obj); // Returns the raw C++ pointer. const void* PyDescriptor_AsVoidPtr(PyObject* obj); diff --git a/python/google/protobuf/pyext/descriptor_pool.cc b/python/google/protobuf/pyext/descriptor_pool.cc index d0038b10fb..767659204f 100644 --- a/python/google/protobuf/pyext/descriptor_pool.cc +++ b/python/google/protobuf/pyext/descriptor_pool.cc @@ -67,15 +67,48 @@ static std::unordered_map* namespace cdescriptor_pool { +// Collects errors that occur during proto file building to allow them to be +// propagated in the python exception instead of only living in ERROR logs. +class BuildFileErrorCollector : public DescriptorPool::ErrorCollector { + public: + BuildFileErrorCollector() : error_message(""), had_errors_(false) {} + + void AddError(const string& filename, const string& element_name, + const Message* descriptor, ErrorLocation location, + const string& message) override { + // Replicates the logging behavior that happens in the C++ implementation + // when an error collector is not passed in. + if (!had_errors_) { + error_message += + ("Invalid proto descriptor for file \"" + filename + "\":\n"); + had_errors_ = true; + } + // As this only happens on failure and will result in the program not + // running at all, no effort is made to optimize this string manipulation. + error_message += (" " + element_name + ": " + message + "\n"); + } + + void Clear() { + had_errors_ = false; + error_message = ""; + } + + string error_message; + + private: + bool had_errors_; +}; + // Create a Python DescriptorPool object, but does not fill the "pool" // attribute. static PyDescriptorPool* _CreateDescriptorPool() { - PyDescriptorPool* cpool = PyObject_New( + PyDescriptorPool* cpool = PyObject_GC_New( PyDescriptorPool, &PyDescriptorPool_Type); if (cpool == NULL) { return NULL; } + cpool->error_collector = nullptr; cpool->underlay = NULL; cpool->database = NULL; @@ -88,6 +121,8 @@ static PyDescriptorPool* _CreateDescriptorPool() { return NULL; } + PyObject_GC_Track(cpool); + return cpool; } @@ -122,7 +157,8 @@ static PyDescriptorPool* PyDescriptorPool_NewWithDatabase( return NULL; } if (database != NULL) { - cpool->pool = new DescriptorPool(database); + cpool->error_collector = new BuildFileErrorCollector(); + cpool->pool = new DescriptorPool(database, cpool->error_collector); cpool->database = database; } else { cpool->pool = new DescriptorPool(); @@ -165,7 +201,34 @@ static void Dealloc(PyObject* pself) { delete self->descriptor_options; delete self->database; delete self->pool; - Py_TYPE(self)->tp_free(reinterpret_cast(self)); + delete self->error_collector; + Py_TYPE(self)->tp_free(pself); +} + +static int GcTraverse(PyObject* pself, visitproc visit, void* arg) { + PyDescriptorPool* self = reinterpret_cast(pself); + Py_VISIT(self->py_message_factory); + return 0; +} + +static int GcClear(PyObject* pself) { + PyDescriptorPool* self = reinterpret_cast(pself); + Py_CLEAR(self->py_message_factory); + return 0; +} + +PyObject* SetErrorFromCollector(DescriptorPool::ErrorCollector* self, + char* name, char* error_type) { + BuildFileErrorCollector* error_collector = + reinterpret_cast(self); + if (error_collector && !error_collector->error_message.empty()) { + PyErr_Format(PyExc_KeyError, "Couldn't build file for %s %.200s\n%s", + error_type, name, error_collector->error_message.c_str()); + error_collector->Clear(); + return NULL; + } + PyErr_Format(PyExc_KeyError, "Couldn't find %s %.200s", error_type, name); + return NULL; } static PyObject* FindMessageByName(PyObject* self, PyObject* arg) { @@ -180,8 +243,9 @@ static PyObject* FindMessageByName(PyObject* self, PyObject* arg) { string(name, name_size)); if (message_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find message %.200s", name); - return NULL; + return SetErrorFromCollector( + reinterpret_cast(self)->error_collector, name, + "message"); } @@ -198,12 +262,12 @@ static PyObject* FindFileByName(PyObject* self, PyObject* arg) { return NULL; } + PyDescriptorPool* py_pool = reinterpret_cast(self); const FileDescriptor* file_descriptor = - reinterpret_cast(self)->pool->FindFileByName( - string(name, name_size)); + py_pool->pool->FindFileByName(string(name, name_size)); + if (file_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find file %.200s", name); - return NULL; + return SetErrorFromCollector(py_pool->error_collector, name, "file"); } return PyFileDescriptor_FromDescriptor(file_descriptor); } @@ -218,9 +282,7 @@ PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* arg) { const FieldDescriptor* field_descriptor = self->pool->FindFieldByName(string(name, name_size)); if (field_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find field %.200s", - name); - return NULL; + return SetErrorFromCollector(self->error_collector, name, "field"); } @@ -241,8 +303,8 @@ PyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg) { const FieldDescriptor* field_descriptor = self->pool->FindExtensionByName(string(name, name_size)); if (field_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find extension field %.200s", name); - return NULL; + return SetErrorFromCollector(self->error_collector, name, + "extension field"); } @@ -263,8 +325,7 @@ PyObject* FindEnumTypeByName(PyDescriptorPool* self, PyObject* arg) { const EnumDescriptor* enum_descriptor = self->pool->FindEnumTypeByName(string(name, name_size)); if (enum_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find enum %.200s", name); - return NULL; + return SetErrorFromCollector(self->error_collector, name, "enum"); } @@ -285,8 +346,7 @@ PyObject* FindOneofByName(PyDescriptorPool* self, PyObject* arg) { const OneofDescriptor* oneof_descriptor = self->pool->FindOneofByName(string(name, name_size)); if (oneof_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find oneof %.200s", name); - return NULL; + return SetErrorFromCollector(self->error_collector, name, "oneof"); } @@ -308,8 +368,9 @@ static PyObject* FindServiceByName(PyObject* self, PyObject* arg) { reinterpret_cast(self)->pool->FindServiceByName( string(name, name_size)); if (service_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find service %.200s", name); - return NULL; + return SetErrorFromCollector( + reinterpret_cast(self)->error_collector, name, + "service"); } @@ -327,8 +388,9 @@ static PyObject* FindMethodByName(PyObject* self, PyObject* arg) { reinterpret_cast(self)->pool->FindMethodByName( string(name, name_size)); if (method_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name); - return NULL; + return SetErrorFromCollector( + reinterpret_cast(self)->error_collector, name, + "method"); } @@ -346,8 +408,9 @@ static PyObject* FindFileContainingSymbol(PyObject* self, PyObject* arg) { reinterpret_cast(self)->pool->FindFileContainingSymbol( string(name, name_size)); if (file_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find symbol %.200s", name); - return NULL; + return SetErrorFromCollector( + reinterpret_cast(self)->error_collector, name, + "symbol"); } @@ -370,7 +433,16 @@ static PyObject* FindExtensionByNumber(PyObject* self, PyObject* args) { reinterpret_cast(self)->pool->FindExtensionByNumber( descriptor, number); if (extension_descriptor == NULL) { - PyErr_Format(PyExc_KeyError, "Couldn't find extension %d", number); + BuildFileErrorCollector* error_collector = + reinterpret_cast( + reinterpret_cast(self)->error_collector); + if (error_collector && !error_collector->error_message.empty()) { + PyErr_Format(PyExc_KeyError, "Couldn't build file for Extension %.d\n%s", + number, error_collector->error_message.c_str()); + error_collector->Clear(); + return NULL; + } + PyErr_Format(PyExc_KeyError, "Couldn't find Extension %d", number); return NULL; } @@ -497,32 +569,6 @@ static PyObject* AddServiceDescriptor(PyObject* self, PyObject* descriptor) { } // The code below loads new Descriptors from a serialized FileDescriptorProto. - -// Collects errors that occur during proto file building to allow them to be -// propagated in the python exception instead of only living in ERROR logs. -class BuildFileErrorCollector : public DescriptorPool::ErrorCollector { - public: - BuildFileErrorCollector() : error_message(""), had_errors(false) {} - - void AddError(const string& filename, const string& element_name, - const Message* descriptor, ErrorLocation location, - const string& message) { - // Replicates the logging behavior that happens in the C++ implementation - // when an error collector is not passed in. - if (!had_errors) { - error_message += - ("Invalid proto descriptor for file \"" + filename + "\":\n"); - had_errors = true; - } - // As this only happens on failure and will result in the program not - // running at all, no effort is made to optimize this string manipulation. - error_message += (" " + element_name + ": " + message + "\n"); - } - - string error_message; - bool had_errors; -}; - static PyObject* AddSerializedFile(PyObject* pself, PyObject* serialized_pb) { PyDescriptorPool* self = reinterpret_cast(pself); char* message_type; @@ -629,45 +675,45 @@ static PyMethodDef Methods[] = { } // namespace cdescriptor_pool PyTypeObject PyDescriptorPool_Type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - FULL_MODULE_NAME ".DescriptorPool", // tp_name - sizeof(PyDescriptorPool), // tp_basicsize - 0, // tp_itemsize - cdescriptor_pool::Dealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - 0, // tp_as_mapping - 0, // tp_hash - 0, // tp_call - 0, // tp_str - 0, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags - "A Descriptor Pool", // tp_doc - 0, // tp_traverse - 0, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - cdescriptor_pool::Methods, // tp_methods - 0, // tp_members - 0, // tp_getset - 0, // tp_base - 0, // tp_dict - 0, // tp_descr_get - 0, // tp_descr_set - 0, // tp_dictoffset - 0, // tp_init - 0, // tp_alloc - cdescriptor_pool::New, // tp_new - PyObject_Del, // tp_free + PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME + ".DescriptorPool", // tp_name + sizeof(PyDescriptorPool), // tp_basicsize + 0, // tp_itemsize + cdescriptor_pool::Dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags + "A Descriptor Pool", // tp_doc + cdescriptor_pool::GcTraverse, // tp_traverse + cdescriptor_pool::GcClear, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + cdescriptor_pool::Methods, // tp_methods + 0, // tp_members + 0, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init + 0, // tp_alloc + cdescriptor_pool::New, // tp_new + PyObject_GC_Del, // tp_free }; // This is the DescriptorPool which contains all the definitions from the diff --git a/python/google/protobuf/pyext/descriptor_pool.h b/python/google/protobuf/pyext/descriptor_pool.h index 8289daeaa7..2d456f9088 100644 --- a/python/google/protobuf/pyext/descriptor_pool.h +++ b/python/google/protobuf/pyext/descriptor_pool.h @@ -50,8 +50,6 @@ struct CMessageClass; // // There is normally one pool per process. We make it a Python object only // because it contains many Python references. -// TODO(amauryfa): See whether such objects can appear in reference cycles, and -// consider adding support for the cyclic GC. // // "Methods" that interacts with this DescriptorPool are in the cdescriptor_pool // namespace. @@ -61,6 +59,10 @@ typedef struct PyDescriptorPool { // The C++ pool containing Descriptors. DescriptorPool* pool; + // The error collector to store error info. Can be NULL. This pointer is + // owned. + DescriptorPool::ErrorCollector* error_collector; + // The C++ pool acting as an underlay. Can be NULL. // This pointer is not owned and must stay alive. const DescriptorPool* underlay; @@ -86,19 +88,8 @@ extern PyTypeObject PyDescriptorPool_Type; namespace cdescriptor_pool { -// Looks up a message by name. -// Returns a message Descriptor, or NULL if not found. -const Descriptor* FindMessageTypeByName(PyDescriptorPool* self, - const string& name); - // The functions below are also exposed as methods of the DescriptorPool type. -// Looks up a message by name. Returns a PyMessageDescriptor corresponding to -// the field on success, or NULL on failure. -// -// Returns a new reference. -PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* name); - // Looks up a field by name. Returns a PyFieldDescriptor corresponding to // the field on success, or NULL on failure. // diff --git a/python/google/protobuf/pyext/extension_dict.cc b/python/google/protobuf/pyext/extension_dict.cc index b73368eb2e..35b6ec6922 100644 --- a/python/google/protobuf/pyext/extension_dict.cc +++ b/python/google/protobuf/pyext/extension_dict.cc @@ -65,6 +65,72 @@ namespace python { namespace extension_dict { +static Py_ssize_t len(ExtensionDict* self) { + Py_ssize_t size = 0; + std::vector fields; + self->parent->message->GetReflection()->ListFields(*self->parent->message, + &fields); + + for (size_t i = 0; i < fields.size(); ++i) { + if (fields[i]->is_extension()) { + // With C++ descriptors, the field can always be retrieved, but for + // unknown extensions which have not been imported in Python code, there + // is no message class and we cannot retrieve the value. + // ListFields() has the same behavior. + if (fields[i]->message_type() != nullptr && + message_factory::GetMessageClass( + cmessage::GetFactoryForMessage(self->parent), + fields[i]->message_type()) == nullptr) { + PyErr_Clear(); + continue; + } + ++size; + } + } + return size; +} + +struct ExtensionIterator { + PyObject_HEAD; + Py_ssize_t index; + std::vector fields; + + // Owned reference, to keep the FieldDescriptors alive. + ExtensionDict* extension_dict; +}; + +PyObject* GetIter(PyObject* _self) { + ExtensionDict* self = reinterpret_cast(_self); + + ScopedPyObjectPtr obj(PyType_GenericAlloc(&ExtensionIterator_Type, 0)); + if (obj == nullptr) { + return PyErr_Format(PyExc_MemoryError, + "Could not allocate extension iterator"); + } + + ExtensionIterator* iter = reinterpret_cast(obj.get()); + + // Call "placement new" to initialize. So the constructor of + // std::vector<...> fields will be called. + new (iter) ExtensionIterator; + + self->parent->message->GetReflection()->ListFields(*self->parent->message, + &iter->fields); + iter->index = 0; + Py_INCREF(self); + iter->extension_dict = self; + + return obj.release(); +} + +static void DeallocExtensionIterator(PyObject* _self) { + ExtensionIterator* self = reinterpret_cast(_self); + self->fields.clear(); + Py_XDECREF(self->extension_dict); + self->~ExtensionIterator(); + Py_TYPE(_self)->tp_free(_self); +} + PyObject* subscript(ExtensionDict* self, PyObject* key) { const FieldDescriptor* descriptor = cmessage::GetExtensionDescriptor(key); if (descriptor == NULL) { @@ -83,20 +149,19 @@ PyObject* subscript(ExtensionDict* self, PyObject* key) { self->parent->composite_fields->find(descriptor); if (iterator != self->parent->composite_fields->end()) { Py_INCREF(iterator->second); - return iterator->second; + return iterator->second->AsPyObject(); } if (descriptor->label() != FieldDescriptor::LABEL_REPEATED && descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { // TODO(plabatut): consider building the class on the fly! - PyObject* sub_message = cmessage::InternalGetSubMessage( + ContainerBase* sub_message = cmessage::InternalGetSubMessage( self->parent, descriptor); if (sub_message == NULL) { return NULL; } - Py_INCREF(sub_message); (*self->parent->composite_fields)[descriptor] = sub_message; - return sub_message; + return sub_message->AsPyObject(); } if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) { @@ -119,23 +184,21 @@ PyObject* subscript(ExtensionDict* self, PyObject* key) { if (message_class == NULL) { return NULL; } - PyObject* py_container = repeated_composite_container::NewContainer( + ContainerBase* py_container = repeated_composite_container::NewContainer( self->parent, descriptor, message_class); if (py_container == NULL) { return NULL; } - Py_INCREF(py_container); (*self->parent->composite_fields)[descriptor] = py_container; - return py_container; + return py_container->AsPyObject(); } else { - PyObject* py_container = repeated_scalar_container::NewContainer( + ContainerBase* py_container = repeated_scalar_container::NewContainer( self->parent, descriptor); if (py_container == NULL) { return NULL; } - Py_INCREF(py_container); (*self->parent->composite_fields)[descriptor] = py_container; - return py_container; + return py_container->AsPyObject(); } } PyErr_SetString(PyExc_ValueError, "control reached unexpected line"); @@ -211,6 +274,35 @@ PyObject* _FindExtensionByNumber(ExtensionDict* self, PyObject* arg) { return PyFieldDescriptor_FromDescriptor(message_extension); } +static int Contains(PyObject* _self, PyObject* key) { + ExtensionDict* self = reinterpret_cast(_self); + const FieldDescriptor* field_descriptor = + cmessage::GetExtensionDescriptor(key); + if (field_descriptor == nullptr) { + return -1; + } + + if (!field_descriptor->is_extension()) { + PyErr_Format(PyExc_KeyError, "%s is not an extension", + field_descriptor->full_name().c_str()); + return -1; + } + + const Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + if (field_descriptor->is_repeated()) { + if (reflection->FieldSize(*message, field_descriptor) > 0) { + return 1; + } + } else { + if (reflection->HasField(*message, field_descriptor)) { + return 1; + } + } + + return 0; +} + ExtensionDict* NewExtensionDict(CMessage *parent) { ExtensionDict* self = reinterpret_cast( PyType_GenericAlloc(&ExtensionDict_Type, 0)); @@ -223,7 +315,8 @@ ExtensionDict* NewExtensionDict(CMessage *parent) { return self; } -void dealloc(ExtensionDict* self) { +void dealloc(PyObject* pself) { + ExtensionDict* self = reinterpret_cast(pself); Py_CLEAR(self->parent); Py_TYPE(self)->tp_free(reinterpret_cast(self)); } @@ -244,63 +337,138 @@ static PyObject* RichCompare(ExtensionDict* self, PyObject* other, int opid) { Py_RETURN_TRUE; } } +static PySequenceMethods SeqMethods = { + (lenfunc)len, // sq_length + 0, // sq_concat + 0, // sq_repeat + 0, // sq_item + 0, // sq_slice + 0, // sq_ass_item + 0, // sq_ass_slice + (objobjproc)Contains, // sq_contains +}; static PyMappingMethods MpMethods = { - (lenfunc)NULL, /* mp_length */ + (lenfunc)len, /* mp_length */ (binaryfunc)subscript, /* mp_subscript */ (objobjargproc)ass_subscript,/* mp_ass_subscript */ }; #define EDMETHOD(name, args, doc) { #name, (PyCFunction)name, args, doc } static PyMethodDef Methods[] = { - EDMETHOD(_FindExtensionByName, METH_O, - "Finds an extension by name."), - EDMETHOD(_FindExtensionByNumber, METH_O, - "Finds an extension by field number."), - { NULL, NULL } + EDMETHOD(_FindExtensionByName, METH_O, "Finds an extension by name."), + EDMETHOD(_FindExtensionByNumber, METH_O, + "Finds an extension by field number."), + {NULL, NULL}, }; } // namespace extension_dict PyTypeObject ExtensionDict_Type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - FULL_MODULE_NAME ".ExtensionDict", // tp_name - sizeof(ExtensionDict), // tp_basicsize - 0, // tp_itemsize - (destructor)extension_dict::dealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - &extension_dict::MpMethods, // tp_as_mapping - PyObject_HashNotImplemented, // tp_hash - 0, // tp_call - 0, // tp_str - 0, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags - "An extension dict", // tp_doc - 0, // tp_traverse - 0, // tp_clear - (richcmpfunc)extension_dict::RichCompare, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - extension_dict::Methods, // tp_methods - 0, // tp_members - 0, // tp_getset - 0, // tp_base - 0, // tp_dict - 0, // tp_descr_get - 0, // tp_descr_set - 0, // tp_dictoffset - 0, // tp_init + PyVarObject_HEAD_INIT(&PyType_Type, 0) // + FULL_MODULE_NAME ".ExtensionDict", // tp_name + sizeof(ExtensionDict), // tp_basicsize + 0, // tp_itemsize + (destructor)extension_dict::dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + &extension_dict::SeqMethods, // tp_as_sequence + &extension_dict::MpMethods, // tp_as_mapping + PyObject_HashNotImplemented, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT, // tp_flags + "An extension dict", // tp_doc + 0, // tp_traverse + 0, // tp_clear + (richcmpfunc)extension_dict::RichCompare, // tp_richcompare + 0, // tp_weaklistoffset + extension_dict::GetIter, // tp_iter + 0, // tp_iternext + extension_dict::Methods, // tp_methods + 0, // tp_members + 0, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init }; +PyObject* IterNext(PyObject* _self) { + extension_dict::ExtensionIterator* self = + reinterpret_cast(_self); + Py_ssize_t total_size = self->fields.size(); + Py_ssize_t index = self->index; + while (self->index < total_size) { + index = self->index; + ++self->index; + if (self->fields[index]->is_extension()) { + // With C++ descriptors, the field can always be retrieved, but for + // unknown extensions which have not been imported in Python code, there + // is no message class and we cannot retrieve the value. + // ListFields() has the same behavior. + if (self->fields[index]->message_type() != nullptr && + message_factory::GetMessageClass( + cmessage::GetFactoryForMessage(self->extension_dict->parent), + self->fields[index]->message_type()) == nullptr) { + PyErr_Clear(); + continue; + } + + return PyFieldDescriptor_FromDescriptor(self->fields[index]); + } + } + + return nullptr; +} + +PyTypeObject ExtensionIterator_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) // + FULL_MODULE_NAME ".ExtensionIterator", // tp_name + sizeof(extension_dict::ExtensionIterator), // tp_basicsize + 0, // tp_itemsize + extension_dict::DeallocExtensionIterator, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT, // tp_flags + "A scalar map iterator", // tp_doc + 0, // tp_traverse + 0, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + PyObject_SelfIter, // tp_iter + IterNext, // tp_iternext + 0, // tp_methods + 0, // tp_members + 0, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init +}; } // namespace python } // namespace protobuf } // namespace google diff --git a/python/google/protobuf/pyext/extension_dict.h b/python/google/protobuf/pyext/extension_dict.h index a7d6bb7b3e..c9da443161 100644 --- a/python/google/protobuf/pyext/extension_dict.h +++ b/python/google/protobuf/pyext/extension_dict.h @@ -56,6 +56,7 @@ typedef struct ExtensionDict { } ExtensionDict; extern PyTypeObject ExtensionDict_Type; +extern PyTypeObject ExtensionIterator_Type; namespace extension_dict { diff --git a/python/google/protobuf/pyext/map_container.cc b/python/google/protobuf/pyext/map_container.cc index 77c61706ff..9c8727efab 100644 --- a/python/google/protobuf/pyext/map_container.cc +++ b/python/google/protobuf/pyext/map_container.cc @@ -43,6 +43,7 @@ #include #include #include +#include #if PY_MAJOR_VERSION >= 3 #define PyInt_FromLong PyLong_FromLong @@ -62,6 +63,7 @@ class MapReflectionFriend { static Py_ssize_t Length(PyObject* _self); static PyObject* GetIterator(PyObject *_self); static PyObject* IterNext(PyObject* _self); + static PyObject* MergeFrom(PyObject* _self, PyObject* arg); // Methods that differ between the map types. static PyObject* ScalarMapGetItem(PyObject* _self, PyObject* key); @@ -81,34 +83,28 @@ struct MapIterator { // We own a ref on this. MapContainer* container; - // We need to keep a ref on the Message* too, because + // We need to keep a ref on the parent Message too, because // MapIterator::~MapIterator() accesses it. Normally this would be ok because // the ref on container (above) would guarantee outlive semantics. However in - // the case of ClearField(), InitializeAndCopyToParentContainer() resets the - // message pointer (and the owner) to a different message, a copy of the - // original. But our iterator still points to the original, which could now - // get deleted before us. + // the case of ClearField(), the MapContainer points to a different message, + // a copy of the original. But our iterator still points to the original, + // which could now get deleted before us. // // To prevent this, we ensure that the Message will always stay alive as long // as this iterator does. This is solely for the benefit of the MapIterator // destructor -- we should never actually access the iterator in this state // except to delete it. - CMessage::OwnerRef owner; - + CMessage* parent; // The version of the map when we took the iterator to it. // // We store this so that if the map is modified during iteration we can throw // an error. uint64 version; - - // True if the container is empty. We signal this separately to avoid calling - // any of the iteration methods, which are non-const. - bool empty; }; Message* MapContainer::GetMutableMessage() { cmessage::AssureWritable(parent); - return const_cast(message); + return parent->message; } // Consumes a reference on the Python string object. @@ -314,7 +310,7 @@ static MapContainer* GetMap(PyObject* obj) { Py_ssize_t MapReflectionFriend::Length(PyObject* _self) { MapContainer* self = GetMap(_self); - const google::protobuf::Message* message = self->message; + const google::protobuf::Message* message = self->parent->message; return message->GetReflection()->MapSize(*message, self->parent_field_descriptor); } @@ -338,20 +334,19 @@ PyObject* GetEntryClass(PyObject* _self) { return reinterpret_cast(message_class); } -PyObject* MergeFrom(PyObject* _self, PyObject* arg) { +PyObject* MapReflectionFriend::MergeFrom(PyObject* _self, PyObject* arg) { MapContainer* self = GetMap(_self); MapContainer* other_map = GetMap(arg); Message* message = self->GetMutableMessage(); - const Message* other_message = other_map->message; + const Message* other_message = other_map->parent->message; const Reflection* reflection = message->GetReflection(); const Reflection* other_reflection = other_message->GetReflection(); - int count = other_reflection->FieldSize( - *other_message, other_map->parent_field_descriptor); - for (int i = 0 ; i < count; i ++) { - reflection->AddMessage(message, self->parent_field_descriptor)->MergeFrom( - other_reflection->GetRepeatedMessage( - *other_message, other_map->parent_field_descriptor, i)); - } + internal::MapFieldBase* field = reflection->MutableMapData( + message, self->parent_field_descriptor); + const internal::MapFieldBase* other_field = + other_reflection->GetMapData(*other_message, + self->parent_field_descriptor); + field->MergeFrom(*other_field); self->version++; Py_RETURN_NONE; } @@ -359,7 +354,7 @@ PyObject* MergeFrom(PyObject* _self, PyObject* arg) { PyObject* MapReflectionFriend::Contains(PyObject* _self, PyObject* key) { MapContainer* self = GetMap(_self); - const Message* message = self->message; + const Message* message = self->parent->message; const Reflection* reflection = message->GetReflection(); MapKey map_key; @@ -375,71 +370,26 @@ PyObject* MapReflectionFriend::Contains(PyObject* _self, PyObject* key) { } } -// Initializes the underlying Message object of "to" so it becomes a new parent -// map container, and copies all the values from "from" to it. A child map -// container can be released by passing it as both from and to (e.g. making it -// the recipient of the new parent message and copying the values from itself). -// In fact, this is the only supported use at the moment. -static int InitializeAndCopyToParentContainer(MapContainer* from, - MapContainer* to) { - // For now we require from == to, re-evaluate if we want to support deep copy - // as in repeated_scalar_container.cc. - GOOGLE_DCHECK(from == to); - Message* new_message = from->message->New(); - - if (MapReflectionFriend::Length(reinterpret_cast(from)) > 0) { - // A somewhat roundabout way of copying just one field from old_message to - // new_message. This is the best we can do with what Reflection gives us. - Message* mutable_old = from->GetMutableMessage(); - std::vector fields; - fields.push_back(from->parent_field_descriptor); - - // Move the map field into the new message. - mutable_old->GetReflection()->SwapFields(mutable_old, new_message, fields); - - // If/when we support from != to, this will be required also to copy the - // map field back into the existing message: - // mutable_old->MergeFrom(*new_message); - } - - // If from == to this could delete old_message. - to->owner.reset(new_message); - - to->parent = NULL; - to->parent_field_descriptor = from->parent_field_descriptor; - to->message = new_message; - - // Invalidate iterators, since they point to the old copy of the field. - to->version++; - - return 0; -} - -int MapContainer::Release() { - return InitializeAndCopyToParentContainer(this, this); -} - - // ScalarMap /////////////////////////////////////////////////////////////////// -PyObject *NewScalarMapContainer( +MapContainer* NewScalarMapContainer( CMessage* parent, const google::protobuf::FieldDescriptor* parent_field_descriptor) { if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) { return NULL; } - ScopedPyObjectPtr obj(PyType_GenericAlloc(ScalarMapContainer_Type, 0)); - if (obj.get() == NULL) { - return PyErr_Format(PyExc_RuntimeError, - "Could not allocate new container."); + PyObject* obj(PyType_GenericAlloc(ScalarMapContainer_Type, 0)); + if (obj == NULL) { + PyErr_Format(PyExc_RuntimeError, + "Could not allocate new container."); + return NULL; } - MapContainer* self = GetMap(obj.get()); + MapContainer* self = GetMap(obj); - self->message = parent->message; + Py_INCREF(parent); self->parent = parent; self->parent_field_descriptor = parent_field_descriptor; - self->owner = parent->owner; self->version = 0; self->key_field_descriptor = @@ -449,11 +399,12 @@ PyObject *NewScalarMapContainer( if (self->key_field_descriptor == NULL || self->value_field_descriptor == NULL) { - return PyErr_Format(PyExc_KeyError, - "Map entry descriptor did not have key/value fields"); + PyErr_Format(PyExc_KeyError, + "Map entry descriptor did not have key/value fields"); + return NULL; } - return obj.release(); + return self; } PyObject* MapReflectionFriend::ScalarMapGetItem(PyObject* _self, @@ -513,10 +464,13 @@ int MapReflectionFriend::ScalarMapSetItem(PyObject* _self, PyObject* key, } } -static PyObject* ScalarMapGet(PyObject* self, PyObject* args) { +static PyObject* ScalarMapGet(PyObject* self, PyObject* args, + PyObject* kwargs) { + static char* kwlist[] = {"key", "default", nullptr}; PyObject* key; PyObject* default_value = NULL; - if (PyArg_ParseTuple(args, "O|O", &key, &default_value) < 0) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist, &key, + &default_value)) { return NULL; } @@ -571,7 +525,7 @@ PyObject* MapReflectionFriend::ScalarMapToStr(PyObject* _self) { static void ScalarMapDealloc(PyObject* _self) { MapContainer* self = GetMap(_self); - self->owner.reset(); + self->RemoveFromParentCache(); PyTypeObject *type = Py_TYPE(_self); type->tp_free(_self); if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { @@ -581,23 +535,23 @@ static void ScalarMapDealloc(PyObject* _self) { } static PyMethodDef ScalarMapMethods[] = { - { "__contains__", MapReflectionFriend::Contains, METH_O, - "Tests whether a key is a member of the map." }, - { "clear", (PyCFunction)Clear, METH_NOARGS, - "Removes all elements from the map." }, - { "get", ScalarMapGet, METH_VARARGS, - "Gets the value for the given key if present, or otherwise a default" }, - { "GetEntryClass", (PyCFunction)GetEntryClass, METH_NOARGS, - "Return the class used to build Entries of (key, value) pairs." }, - { "MergeFrom", (PyCFunction)MergeFrom, METH_O, - "Merges a map into the current map." }, - /* - { "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS, - "Makes a deep copy of the class." }, - { "__reduce__", (PyCFunction)Reduce, METH_NOARGS, - "Outputs picklable representation of the repeated field." }, - */ - {NULL, NULL}, + {"__contains__", MapReflectionFriend::Contains, METH_O, + "Tests whether a key is a member of the map."}, + {"clear", (PyCFunction)Clear, METH_NOARGS, + "Removes all elements from the map."}, + {"get", (PyCFunction)ScalarMapGet, METH_VARARGS | METH_KEYWORDS, + "Gets the value for the given key if present, or otherwise a default"}, + {"GetEntryClass", (PyCFunction)GetEntryClass, METH_NOARGS, + "Return the class used to build Entries of (key, value) pairs."}, + {"MergeFrom", (PyCFunction)MapReflectionFriend::MergeFrom, METH_O, + "Merges a map into the current map."}, + /* + { "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS, + "Makes a deep copy of the class." }, + { "__reduce__", (PyCFunction)Reduce, METH_NOARGS, + "Outputs picklable representation of the repeated field." }, + */ + {NULL, NULL}, }; PyTypeObject *ScalarMapContainer_Type; @@ -674,36 +628,15 @@ static MessageMapContainer* GetMessageMap(PyObject* obj) { return reinterpret_cast(obj); } -static PyObject* GetCMessage(MessageMapContainer* self, Message* message, - bool insert_message_dict) { +static PyObject* GetCMessage(MessageMapContainer* self, Message* message) { // Get or create the CMessage object corresponding to this message. - ScopedPyObjectPtr key(PyLong_FromVoidPtr(message)); - PyObject* ret = PyDict_GetItem(self->message_dict, key.get()); - - if (ret == NULL) { - CMessage* cmsg = cmessage::NewEmptyMessage(self->message_class); - ret = reinterpret_cast(cmsg); - - if (cmsg == NULL) { - return NULL; - } - cmsg->owner = self->owner; - cmsg->message = message; - cmsg->parent = self->parent; - if (insert_message_dict) { - if (PyDict_SetItem(self->message_dict, key.get(), ret) < 0) { - Py_DECREF(ret); - return NULL; - } - } - } else { - Py_INCREF(ret); - } - - return ret; + return self->parent + ->BuildSubMessageFromPointer(self->parent_field_descriptor, message, + self->message_class) + ->AsPyObject(); } -PyObject* NewMessageMapContainer( +MessageMapContainer* NewMessageMapContainer( CMessage* parent, const google::protobuf::FieldDescriptor* parent_field_descriptor, CMessageClass* message_class) { if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) { @@ -712,16 +645,15 @@ PyObject* NewMessageMapContainer( PyObject* obj = PyType_GenericAlloc(MessageMapContainer_Type, 0); if (obj == NULL) { - return PyErr_Format(PyExc_RuntimeError, - "Could not allocate new container."); + PyErr_SetString(PyExc_RuntimeError, "Could not allocate new container."); + return NULL; } MessageMapContainer* self = GetMessageMap(obj); - self->message = parent->message; + Py_INCREF(parent); self->parent = parent; self->parent_field_descriptor = parent_field_descriptor; - self->owner = parent->owner; self->version = 0; self->key_field_descriptor = @@ -729,23 +661,18 @@ PyObject* NewMessageMapContainer( self->value_field_descriptor = parent_field_descriptor->message_type()->FindFieldByName("value"); - self->message_dict = PyDict_New(); - if (self->message_dict == NULL) { - return PyErr_Format(PyExc_RuntimeError, - "Could not allocate message dict."); - } - Py_INCREF(message_class); self->message_class = message_class; if (self->key_field_descriptor == NULL || self->value_field_descriptor == NULL) { - Py_DECREF(obj); - return PyErr_Format(PyExc_KeyError, - "Map entry descriptor did not have key/value fields"); + Py_DECREF(self); + PyErr_SetString(PyExc_KeyError, + "Map entry descriptor did not have key/value fields"); + return NULL; } - return obj; + return self; } int MapReflectionFriend::MessageMapSetItem(PyObject* _self, PyObject* key, @@ -777,22 +704,14 @@ int MapReflectionFriend::MessageMapSetItem(PyObject* _self, PyObject* key, MapValueRef value; reflection->InsertOrLookupMapValue(message, self->parent_field_descriptor, map_key, &value); - ScopedPyObjectPtr key(PyLong_FromVoidPtr(value.MutableMessageValue())); - - PyObject* cmsg_value = PyDict_GetItem(self->message_dict, key.get()); - if (cmsg_value) { - // Need to keep CMessage stay alive if it is still referenced after - // deletion. Makes a new message and swaps values into CMessage - // instead of just removing. - CMessage* cmsg = reinterpret_cast(cmsg_value); - Message* msg = cmsg->message; - cmsg->owner.reset(msg->New()); - cmsg->message = cmsg->owner.get(); - cmsg->parent = NULL; - msg->GetReflection()->Swap(msg, cmsg->message); - if (PyDict_DelItem(self->message_dict, key.get()) < 0) { - return -1; - } + Message* sub_message = value.MutableMessageValue(); + // If there is a living weak reference to an item, we "Release" it, + // otherwise we just discard the C++ value. + if (CMessage* released = + self->parent->MaybeReleaseSubMessage(sub_message)) { + Message* msg = released->message; + released->message = msg->New(); + msg->GetReflection()->Swap(msg, released->message); } // Delete key from map. @@ -823,7 +742,7 @@ PyObject* MapReflectionFriend::MessageMapGetItem(PyObject* _self, self->version++; } - return GetCMessage(self, value.MutableMessageValue(), true); + return GetCMessage(self, value.MutableMessageValue()); } PyObject* MapReflectionFriend::MessageMapToStr(PyObject* _self) { @@ -846,10 +765,7 @@ PyObject* MapReflectionFriend::MessageMapToStr(PyObject* _self) { if (key == NULL) { return NULL; } - // Do not insert the cmessage to self->message_dict because - // the returned CMessage will not escape this function. - value.reset(GetCMessage( - self, it.MutableValueRef()->MutableMessageValue(), false)); + value.reset(GetCMessage(self, it.MutableValueRef()->MutableMessageValue())); if (value == NULL) { return NULL; } @@ -860,10 +776,12 @@ PyObject* MapReflectionFriend::MessageMapToStr(PyObject* _self) { return PyObject_Repr(dict.get()); } -PyObject* MessageMapGet(PyObject* self, PyObject* args) { +PyObject* MessageMapGet(PyObject* self, PyObject* args, PyObject* kwargs) { + static char* kwlist[] = {"key", "default", nullptr}; PyObject* key; PyObject* default_value = NULL; - if (PyArg_ParseTuple(args, "O|O", &key, &default_value) < 0) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist, &key, + &default_value)) { return NULL; } @@ -886,8 +804,7 @@ PyObject* MessageMapGet(PyObject* self, PyObject* args) { static void MessageMapDealloc(PyObject* _self) { MessageMapContainer* self = GetMessageMap(_self); - self->owner.reset(); - Py_DECREF(self->message_dict); + self->RemoveFromParentCache(); Py_DECREF(self->message_class); PyTypeObject *type = Py_TYPE(_self); type->tp_free(_self); @@ -898,25 +815,25 @@ static void MessageMapDealloc(PyObject* _self) { } static PyMethodDef MessageMapMethods[] = { - { "__contains__", (PyCFunction)MapReflectionFriend::Contains, METH_O, - "Tests whether the map contains this element."}, - { "clear", (PyCFunction)Clear, METH_NOARGS, - "Removes all elements from the map."}, - { "get", MessageMapGet, METH_VARARGS, - "Gets the value for the given key if present, or otherwise a default" }, - { "get_or_create", MapReflectionFriend::MessageMapGetItem, METH_O, - "Alias for getitem, useful to make explicit that the map is mutated." }, - { "GetEntryClass", (PyCFunction)GetEntryClass, METH_NOARGS, - "Return the class used to build Entries of (key, value) pairs." }, - { "MergeFrom", (PyCFunction)MergeFrom, METH_O, - "Merges a map into the current map." }, - /* - { "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS, - "Makes a deep copy of the class." }, - { "__reduce__", (PyCFunction)Reduce, METH_NOARGS, - "Outputs picklable representation of the repeated field." }, - */ - {NULL, NULL}, + {"__contains__", (PyCFunction)MapReflectionFriend::Contains, METH_O, + "Tests whether the map contains this element."}, + {"clear", (PyCFunction)Clear, METH_NOARGS, + "Removes all elements from the map."}, + {"get", (PyCFunction)MessageMapGet, METH_VARARGS | METH_KEYWORDS, + "Gets the value for the given key if present, or otherwise a default"}, + {"get_or_create", MapReflectionFriend::MessageMapGetItem, METH_O, + "Alias for getitem, useful to make explicit that the map is mutated."}, + {"GetEntryClass", (PyCFunction)GetEntryClass, METH_NOARGS, + "Return the class used to build Entries of (key, value) pairs."}, + {"MergeFrom", (PyCFunction)MapReflectionFriend::MergeFrom, METH_O, + "Merges a map into the current map."}, + /* + { "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS, + "Makes a deep copy of the class." }, + { "__reduce__", (PyCFunction)Reduce, METH_NOARGS, + "Outputs picklable representation of the repeated field." }, + */ + {NULL, NULL}, }; PyTypeObject *MessageMapContainer_Type; @@ -1005,7 +922,8 @@ PyObject* MapReflectionFriend::GetIterator(PyObject *_self) { Py_INCREF(self); iter->container = self; iter->version = self->version; - iter->owner = self->owner; + Py_INCREF(self->parent); + iter->parent = self->parent; if (MapReflectionFriend::Length(_self) > 0) { Message* message = self->GetMutableMessage(); @@ -1027,6 +945,10 @@ PyObject* MapReflectionFriend::IterNext(PyObject* _self) { return PyErr_Format(PyExc_RuntimeError, "Map modified during iteration."); } + if (self->parent != self->container->parent) { + return PyErr_Format(PyExc_RuntimeError, + "Map cleared during iteration."); + } if (self->iter.get() == NULL) { return NULL; @@ -1051,8 +973,8 @@ PyObject* MapReflectionFriend::IterNext(PyObject* _self) { static void DeallocMapIterator(PyObject* _self) { MapIterator* self = GetIter(_self); self->iter.reset(); - self->owner.reset(); - Py_XDECREF(self->container); + Py_CLEAR(self->container); + Py_CLEAR(self->parent); Py_TYPE(_self)->tp_free(_self); } diff --git a/python/google/protobuf/pyext/map_container.h b/python/google/protobuf/pyext/map_container.h index 7e77b027c3..2c9b323efa 100644 --- a/python/google/protobuf/pyext/map_container.h +++ b/python/google/protobuf/pyext/map_container.h @@ -50,55 +50,21 @@ struct CMessageClass; // This struct is used directly for ScalarMap, and is the base class of // MessageMapContainer, which is used for MessageMap. -struct MapContainer { - PyObject_HEAD; - - // This is the top-level C++ Message object that owns the whole - // proto tree. Every Python MapContainer holds a - // reference to it in order to keep it alive as long as there's a - // Python object that references any part of the tree. - CMessage::OwnerRef owner; - - // Pointer to the C++ Message that contains this container. The - // MapContainer does not own this pointer. - const Message* message; - +struct MapContainer : public ContainerBase { // Use to get a mutable message when necessary. Message* GetMutableMessage(); - // Weak reference to a parent CMessage object (i.e. may be NULL.) - // - // Used to make sure all ancestors are also mutable when first - // modifying the container. - CMessage* parent; - - // Pointer to the parent's descriptor that describes this - // field. Used together with the parent's message when making a - // default message instance mutable. - // The pointer is owned by the global DescriptorPool. - const FieldDescriptor* parent_field_descriptor; + // Cache some descriptors, used to convert keys and values. const FieldDescriptor* key_field_descriptor; const FieldDescriptor* value_field_descriptor; - // We bump this whenever we perform a mutation, to invalidate existing // iterators. uint64 version; - - // Releases the messages in the container to a new message. - // - // Returns 0 on success, -1 on failure. - int Release(); - - // Set the owner field of self and any children of self. - void SetOwner(const CMessage::OwnerRef& new_owner) { owner = new_owner; } }; struct MessageMapContainer : public MapContainer { // The type used to create new child messages. CMessageClass* message_class; - - // A dict mapping Message* -> CMessage. - PyObject* message_dict; }; bool InitMapContainers(); @@ -109,12 +75,12 @@ extern PyTypeObject MapIterator_Type; // Both map types use the same iterator. // Builds a MapContainer object, from a parent message and a // field descriptor. -extern PyObject* NewScalarMapContainer( +extern MapContainer* NewScalarMapContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor); // Builds a MessageMap object, from a parent message and a // field descriptor. -extern PyObject* NewMessageMapContainer( +extern MessageMapContainer* NewMessageMapContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor, CMessageClass* message_class); diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index 2205f12fdc..3530a9b379 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -47,6 +47,7 @@ #endif #include #include +#include #include #include #include @@ -67,6 +68,9 @@ #include #include #include +#include + +#include #if PY_MAJOR_VERSION >= 3 #define PyInt_AsLong PyLong_AsLong @@ -307,12 +311,25 @@ static PyObject* New(PyTypeObject* type, return result.release(); } -static void Dealloc(CMessageClass *self) { +static void Dealloc(PyObject* pself) { + CMessageClass* self = reinterpret_cast(pself); Py_XDECREF(self->py_message_descriptor); Py_XDECREF(self->py_message_factory); - Py_TYPE(self)->tp_free(reinterpret_cast(self)); + return PyType_Type.tp_dealloc(pself); } +static int GcTraverse(PyObject* pself, visitproc visit, void* arg) { + CMessageClass* self = reinterpret_cast(pself); + Py_VISIT(self->py_message_descriptor); + Py_VISIT(self->py_message_factory); + return PyType_Type.tp_traverse(pself, visit, arg); +} + +static int GcClear(PyObject* pself) { + // It's important to keep the descriptor and factory alive, until the + // C++ message is fully destructed. + return PyType_Type.tp_clear(pself); +} // This function inserts and empty weakref at the end of the list of // subclasses for the main protocol buffer Message class. @@ -325,11 +342,17 @@ static int InsertEmptyWeakref(PyTypeObject *base_type) { // hack addresses. For further background and the fix please see // https://bugs.python.org/issue17936. return 0; +#else +#ifdef Py_DEBUG + // The code below causes all new subclasses to append an entry, which is never + // cleared. This is a small memory leak, which we disable in Py_DEBUG mode + // to have stable refcounting checks. #else PyObject *subclasses = base_type->tp_subclasses; if (subclasses && PyList_CheckExact(subclasses)) { return PyList_Append(subclasses, kEmptyWeakref); } +#endif // !Py_DEBUG return 0; #endif // PY_MAJOR_VERSION >= 3 } @@ -448,44 +471,44 @@ static PyObject* GetAttr(CMessageClass* self, PyObject* name) { } // namespace message_meta static PyTypeObject _CMessageClass_Type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - FULL_MODULE_NAME ".MessageMeta", // tp_name - sizeof(CMessageClass), // tp_basicsize - 0, // tp_itemsize - (destructor)message_meta::Dealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - 0, // tp_as_mapping - 0, // tp_hash - 0, // tp_call - 0, // tp_str - (getattrofunc)message_meta::GetAttr, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags - "The metaclass of ProtocolMessages", // tp_doc - 0, // tp_traverse - 0, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - 0, // tp_methods - 0, // tp_members - message_meta::Getters, // tp_getset - 0, // tp_base - 0, // tp_dict - 0, // tp_descr_get - 0, // tp_descr_set - 0, // tp_dictoffset - 0, // tp_init - 0, // tp_alloc - message_meta::New, // tp_new + PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME + ".MessageMeta", // tp_name + sizeof(CMessageClass), // tp_basicsize + 0, // tp_itemsize + message_meta::Dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + (getattrofunc)message_meta::GetAttr, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, // tp_flags + "The metaclass of ProtocolMessages", // tp_doc + message_meta::GcTraverse, // tp_traverse + message_meta::GcClear, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + 0, // tp_methods + 0, // tp_members + message_meta::Getters, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init + 0, // tp_alloc + message_meta::New, // tp_new }; PyTypeObject* CMessageClass_Type = &_CMessageClass_Type; @@ -509,104 +532,9 @@ static const Descriptor* GetMessageDescriptor(PyTypeObject* cls) { namespace cmessage { int InternalReleaseFieldByDescriptor( CMessage* self, - const FieldDescriptor* field_descriptor, - PyObject* composite_field); + const FieldDescriptor* field_descriptor); } // namespace cmessage -// --------------------------------------------------------------------- -// Visiting the composite children of a CMessage - -struct ChildVisitor { - // Returns 0 on success, -1 on failure. - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - return 0; - } - - // Returns 0 on success, -1 on failure. - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - return 0; - } - - // Returns 0 on success, -1 on failure. - int VisitMapContainer(MapContainer* container) { - return 0; - } - - // Returns 0 on success, -1 on failure. - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - return 0; - } - - // Returns 0 on success, -1 on failure. - int VisitUnknownFieldSet(PyUnknownFields* unknown_field_set) { - return 0; - } -}; - -// Apply a function to a composite field. Does nothing if child is of -// non-composite type. -template -static int VisitCompositeField(const FieldDescriptor* descriptor, - PyObject* child, - Visitor visitor) { - if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) { - if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - if (descriptor->is_map()) { - MapContainer* container = reinterpret_cast(child); - if (visitor.VisitMapContainer(container) == -1) { - return -1; - } - } else { - RepeatedCompositeContainer* container = - reinterpret_cast(child); - if (visitor.VisitRepeatedCompositeContainer(container) == -1) - return -1; - } - } else { - RepeatedScalarContainer* container = - reinterpret_cast(child); - if (visitor.VisitRepeatedScalarContainer(container) == -1) - return -1; - } - } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - CMessage* cmsg = reinterpret_cast(child); - if (visitor.VisitCMessage(cmsg, descriptor) == -1) - return -1; - } - // The ExtensionDict might contain non-composite fields, which we - // skip here. - return 0; -} - -// Visit each composite field and extension field of this CMessage. -// Returns -1 on error and 0 on success. -template -int ForEachCompositeField(CMessage* self, Visitor visitor) { - Py_ssize_t pos = 0; - PyObject* key; - PyObject* field; - - // Visit normal fields. - if (self->composite_fields) { - for (CMessage::CompositeFieldsMap::iterator it = - self->composite_fields->begin(); - it != self->composite_fields->end(); it++) { - const FieldDescriptor* descriptor = it->first; - PyObject* field = it->second; - if (VisitCompositeField(descriptor, field, visitor) == -1) return -1; - } - } - - if (self->unknown_field_set) { - PyUnknownFields* unknown_field_set = - reinterpret_cast(self->unknown_field_set); - visitor.VisitUnknownFieldSet(unknown_field_set); - } - - return 0; -} - // --------------------------------------------------------------------- PyObject* EncodeError_class; @@ -642,7 +570,7 @@ void OutOfRangeError(PyObject* arg) { template bool VerifyIntegerCastAndRange(PyObject* arg, ValueType value) { - if (GOOGLE_PREDICT_FALSE(value == -1 && PyErr_Occurred())) { + if (PROTOBUF_PREDICT_FALSE(value == -1 && PyErr_Occurred())) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { // Replace it with the same ValueError as pure python protos instead of // the default one. @@ -651,7 +579,7 @@ bool VerifyIntegerCastAndRange(PyObject* arg, ValueType value) { } // Otherwise propagate existing error. return false; } - if (GOOGLE_PREDICT_FALSE(!IsValidNumericCast(value))) { + if (PROTOBUF_PREDICT_FALSE(!IsValidNumericCast(value))) { OutOfRangeError(arg); return false; } @@ -663,22 +591,22 @@ bool CheckAndGetInteger(PyObject* arg, T* value) { // The fast path. #if PY_MAJOR_VERSION < 3 // For the typical case, offer a fast path. - if (GOOGLE_PREDICT_TRUE(PyInt_Check(arg))) { + if (PROTOBUF_PREDICT_TRUE(PyInt_Check(arg))) { long int_result = PyInt_AsLong(arg); - if (GOOGLE_PREDICT_TRUE(IsValidNumericCast(int_result))) { + if (PROTOBUF_PREDICT_TRUE(IsValidNumericCast(int_result))) { *value = static_cast(int_result); return true; } else { OutOfRangeError(arg); return false; } - } + } #endif // This effectively defines an integer as "an object that can be cast as // an integer and can be used as an ordinal number". // This definition includes everything that implements numbers.Integral // and shouldn't cast the net too wide. - if (GOOGLE_PREDICT_FALSE(!PyIndex_Check(arg))) { + if (PROTOBUF_PREDICT_FALSE(!PyIndex_Check(arg))) { FormatTypeError(arg, "int, long"); return false; } @@ -695,7 +623,7 @@ bool CheckAndGetInteger(PyObject* arg, T* value) { // Unlike PyLong_AsLongLong, PyLong_AsUnsignedLongLong is very // picky about the exact type. PyObject* casted = PyNumber_Long(arg); - if (GOOGLE_PREDICT_FALSE(casted == nullptr)) { + if (PROTOBUF_PREDICT_FALSE(casted == nullptr)) { // Propagate existing error. return false; } @@ -720,7 +648,7 @@ bool CheckAndGetInteger(PyObject* arg, T* value) { // Valid subclasses of numbers.Integral should have a __long__() method // so fall back to that. PyObject* casted = PyNumber_Long(arg); - if (GOOGLE_PREDICT_FALSE(casted == nullptr)) { + if (PROTOBUF_PREDICT_FALSE(casted == nullptr)) { // Propagate existing error. return false; } @@ -746,7 +674,7 @@ template bool CheckAndGetInteger(PyObject*, uint64*); bool CheckAndGetDouble(PyObject* arg, double* value) { *value = PyFloat_AsDouble(arg); - if (GOOGLE_PREDICT_FALSE(*value == -1 && PyErr_Occurred())) { + if (PROTOBUF_PREDICT_FALSE(*value == -1 && PyErr_Occurred())) { FormatTypeError(arg, "int, long, float"); return false; } @@ -758,7 +686,7 @@ bool CheckAndGetFloat(PyObject* arg, float* value) { if (!CheckAndGetDouble(arg, &double_value)) { return false; } - *value = static_cast(double_value); + *value = io::SafeDoubleToFloat(double_value); return true; } @@ -923,138 +851,78 @@ static int MaybeReleaseOverlappingOneofField( // Non-message fields don't need to be released. return 0; } - if (cmessage->composite_fields) { - CMessage::CompositeFieldsMap::iterator iterator = - cmessage->composite_fields->find(existing_field); - if (iterator != cmessage->composite_fields->end()) { - if (InternalReleaseFieldByDescriptor(cmessage, existing_field, - iterator->second) < 0) { - return -1; - } - Py_DECREF(iterator->second); - cmessage->composite_fields->erase(iterator); - } + if (InternalReleaseFieldByDescriptor(cmessage, existing_field) < 0) { + return -1; } #endif return 0; } -// --------------------------------------------------------------------- -// Making a message writable - -static Message* GetMutableMessage( - CMessage* parent, - const FieldDescriptor* parent_field) { - Message* parent_message = parent->message; - const Reflection* reflection = parent_message->GetReflection(); - if (MaybeReleaseOverlappingOneofField(parent, parent_field) < 0) { - return NULL; - } - return reflection->MutableMessage( - parent_message, parent_field, - GetFactoryForMessage(parent)->message_factory); -} - -struct FixupMessageReference : public ChildVisitor { - // message must outlive this object. - explicit FixupMessageReference(Message* message) : - message_(message) {} - - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - container->message = message_; - return 0; - } - - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - container->message = message_; - return 0; - } - - int VisitMapContainer(MapContainer* container) { - container->message = message_; - return 0; - } - - int VisitUnknownFieldSet(PyUnknownFields* unknown_field_set) { - const Reflection* reflection = message_->GetReflection(); - unknown_field_set->fields = &reflection->GetUnknownFields(*message_); - return 0; - } - - private: - Message* message_; -}; - // After a Merge, visit every sub-message that was read-only, and // eventually update their pointer if the Merge operation modified them. -struct FixupMessageAfterMerge : public FixupMessageReference { - explicit FixupMessageAfterMerge(CMessage* parent) : - FixupMessageReference(parent->message), - parent_cmessage(parent), message(parent->message) {} - - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - if (cmessage->read_only == false) { - return 0; - } - if (message->GetReflection()->HasField(*message, field_descriptor)) { - Message* mutable_message = GetMutableMessage( - parent_cmessage, field_descriptor); - if (mutable_message == NULL) { - return -1; +int FixupMessageAfterMerge(CMessage* self) { + if (!self->composite_fields) { + return 0; + } + for (const auto& item : *self->composite_fields) { + const FieldDescriptor* descriptor = item.first; + if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && + !descriptor->is_repeated()) { + CMessage* cmsg = reinterpret_cast(item.second); + if (cmsg->read_only == false) { + return 0; } - cmessage->message = mutable_message; - cmessage->read_only = false; - if (ForEachCompositeField( - cmessage, FixupMessageAfterMerge(cmessage)) == -1) { - return -1; + Message* message = self->message; + const Reflection* reflection = message->GetReflection(); + if (reflection->HasField(*message, descriptor)) { + // Message used to be read_only, but is no longer. Get the new pointer + // and record it. + Message* mutable_message = + reflection->MutableMessage(message, descriptor, nullptr); + cmsg->message = mutable_message; + cmsg->read_only = false; + if (FixupMessageAfterMerge(cmsg) < 0) { + return -1; + } } } - return 0; } - private: - CMessage* parent_cmessage; - Message* message; -}; + return 0; +} + +// --------------------------------------------------------------------- +// Making a message writable int AssureWritable(CMessage* self) { if (self == NULL || !self->read_only) { return 0; } - if (self->parent == NULL) { - // If parent is NULL but we are trying to modify a read-only message, this - // is a reference to a constant default instance that needs to be replaced - // with a mutable top-level message. - self->message = self->message->New(); - self->owner.reset(self->message); - // Cascade the new owner to eventual children: even if this message is - // empty, some submessages or repeated containers might exist already. - SetOwner(self, self->owner); - } else { - // Otherwise, we need a mutable child message. - if (AssureWritable(self->parent) == -1) - return -1; + // Toplevel messages are always mutable. + GOOGLE_DCHECK(self->parent); - // Make self->message writable. - Message* mutable_message = GetMutableMessage( - self->parent, - self->parent_field_descriptor); - if (mutable_message == NULL) { - return -1; - } - self->message = mutable_message; + if (AssureWritable(self->parent) == -1) + return -1; + + // If this message is part of a oneof, there might be a field to release in + // the parent. + if (MaybeReleaseOverlappingOneofField(self->parent, + self->parent_field_descriptor) < 0) { + return -1; } - self->read_only = false; - // When a CMessage is made writable its Message pointer is updated - // to point to a new mutable Message. When that happens we need to - // update any references to the old, read-only CMessage. There are - // three places such references occur: RepeatedScalarContainer, - // RepeatedCompositeContainer, and MapContainer. - if (ForEachCompositeField(self, FixupMessageReference(self->message)) == -1) + // Make self->message writable. + Message* parent_message = self->parent->message; + const Reflection* reflection = parent_message->GetReflection(); + Message* mutable_message = reflection->MutableMessage( + parent_message, self->parent_field_descriptor, + GetFactoryForMessage(self->parent)->message_factory); + if (mutable_message == NULL) { return -1; + } + self->message = mutable_message; + self->read_only = false; return 0; } @@ -1101,17 +969,16 @@ static PyObject* GetIntegerEnumValue(const FieldDescriptor& descriptor, return value; } -// If cmessage_list is not NULL, this function releases values into the -// container CMessages instead of just removing. Repeated composite container -// needs to do this to make sure CMessages stay alive if they're still -// referenced after deletion. Repeated scalar container doesn't need to worry. -int InternalDeleteRepeatedField( +// Delete a slice from a repeated field. +// The only way to remove items in C++ protos is to delete the last one, +// so we swap items to move the deleted ones at the end, and then strip the +// sequence. +int DeleteRepeatedField( CMessage* self, const FieldDescriptor* field_descriptor, - PyObject* slice, - PyObject* cmessage_list) { - Message* message = self->message; + PyObject* slice) { Py_ssize_t length, from, to, step, slice_length; + Message* message = self->message; const Reflection* reflection = message->GetReflection(); int min, max; length = reflection->FieldSize(*message, field_descriptor); @@ -1161,39 +1028,34 @@ int InternalDeleteRepeatedField( i += step; } + // Swap elements so that items to delete are at the end. to = 0; for (i = 0; i < length; ++i) { if (!to_delete[i]) { if (i != to) { reflection->SwapElements(message, field_descriptor, i, to); - if (cmessage_list != NULL) { - // If a list of cmessages is passed in (i.e. from a repeated - // composite container), swap those as well to correspond to the - // swaps in the underlying message so they're in the right order - // when we start releasing. - PyObject* tmp = PyList_GET_ITEM(cmessage_list, i); - PyList_SET_ITEM(cmessage_list, i, - PyList_GET_ITEM(cmessage_list, to)); - PyList_SET_ITEM(cmessage_list, to, tmp); - } } ++to; } } - while (i > to) { - if (cmessage_list == NULL) { + // Remove items, starting from the end. + for (; length > to; length--) { + if (field_descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { reflection->RemoveLast(message, field_descriptor); + continue; + } + // It seems that RemoveLast() is less efficient for sub-messages, and + // the memory is not completely released. Prefer ReleaseLast(). + Message* sub_message = reflection->ReleaseLast(message, field_descriptor); + // If there is a live weak reference to an item being removed, we "Release" + // it, and it takes ownership of the message. + if (CMessage* released = self->MaybeReleaseSubMessage(sub_message)) { + released->message = sub_message; } else { - CMessage* last_cmessage = reinterpret_cast( - PyList_GET_ITEM(cmessage_list, PyList_GET_SIZE(cmessage_list) - 1)); - repeated_composite_container::ReleaseLastTo( - self, field_descriptor, last_cmessage); - if (PySequence_DelItem(cmessage_list, -1) < 0) { - return -1; - } + // sub_message was not transferred, delete it. + delete sub_message; } - --i; } return 0; @@ -1214,7 +1076,7 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { PyObject* name; PyObject* value; while (PyDict_Next(kwargs, &pos, &name, &value)) { - if (!PyString_Check(name)) { + if (!(PyString_Check(name) || PyUnicode_Check(name))) { PyErr_SetString(PyExc_ValueError, "Field name must be a string"); return -1; } @@ -1239,17 +1101,20 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { const FieldDescriptor* value_descriptor = descriptor->message_type()->FindFieldByName("value"); if (value_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - Py_ssize_t map_pos = 0; - PyObject* map_key; - PyObject* map_value; - while (PyDict_Next(value, &map_pos, &map_key, &map_value)) { - ScopedPyObjectPtr function_return; - function_return.reset(PyObject_GetItem(map.get(), map_key)); - if (function_return.get() == NULL) { + ScopedPyObjectPtr iter(PyObject_GetIter(value)); + if (iter == NULL) { + PyErr_Format(PyExc_TypeError, "Argument %s is not iterable", PyString_AsString(name)); + return -1; + } + ScopedPyObjectPtr next; + while ((next.reset(PyIter_Next(iter.get()))) != NULL) { + ScopedPyObjectPtr source_value(PyObject_GetItem(value, next.get())); + ScopedPyObjectPtr dest_value(PyObject_GetItem(map.get(), next.get())); + if (source_value.get() == NULL || dest_value.get() == NULL) { return -1; } ScopedPyObjectPtr ok(PyObject_CallMethod( - function_return.get(), "MergeFrom", "O", map_value)); + dest_value.get(), "MergeFrom", "O", source_value.get())); if (ok.get() == NULL) { return -1; } @@ -1364,8 +1229,8 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { return 0; } -// Allocates an incomplete Python Message: the caller must fill self->message, -// self->owner and eventually self->parent. +// Allocates an incomplete Python Message: the caller must fill self->message +// and eventually self->parent. CMessage* NewEmptyMessage(CMessageClass* type) { CMessage* self = reinterpret_cast( PyType_GenericAlloc(&type->super.ht_type, 0)); @@ -1373,14 +1238,13 @@ CMessage* NewEmptyMessage(CMessageClass* type) { return NULL; } - // Use "placement new" syntax to initialize the C++ object. - new (&self->owner) CMessage::OwnerRef(NULL); self->message = NULL; self->parent = NULL; self->parent_field_descriptor = NULL; self->read_only = false; self->composite_fields = NULL; + self->child_submessages = NULL; self->unknown_field_set = NULL; @@ -1400,9 +1264,10 @@ static PyObject* New(PyTypeObject* cls, if (message_descriptor == NULL) { return NULL; } - const Message* default_message = type->py_message_factory->message_factory - ->GetPrototype(message_descriptor); - if (default_message == NULL) { + const Message* prototype = + type->py_message_factory->message_factory->GetPrototype( + message_descriptor); + if (prototype == NULL) { PyErr_SetString(PyExc_TypeError, message_descriptor->full_name().c_str()); return NULL; } @@ -1411,8 +1276,8 @@ static PyObject* New(PyTypeObject* cls, if (self == NULL) { return NULL; } - self->message = default_message->New(); - self->owner.reset(self->message); + self->message = prototype->New(); + self->parent = nullptr; // This message owns its data. return reinterpret_cast(self); } @@ -1424,63 +1289,39 @@ static int Init(CMessage* self, PyObject* args, PyObject* kwargs) { // --------------------------------------------------------------------- // Deallocating a CMessage -// -// Deallocating a CMessage requires that we clear any weak references -// from children to the message being deallocated. - -// Clear the weak reference from the child to the parent. -struct ClearWeakReferences : public ChildVisitor { - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - container->parent = NULL; - // The elements in the container have the same parent as the - // container itself, so NULL out that pointer as well. - const Py_ssize_t n = PyList_GET_SIZE(container->child_messages); - for (Py_ssize_t i = 0; i < n; ++i) { - CMessage* child_cmessage = reinterpret_cast( - PyList_GET_ITEM(container->child_messages, i)); - child_cmessage->parent = NULL; - } - return 0; - } - - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - container->parent = NULL; - return 0; - } - - int VisitMapContainer(MapContainer* container) { - container->parent = NULL; - return 0; - } - - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - cmessage->parent = NULL; - return 0; - } -}; static void Dealloc(CMessage* self) { if (self->weakreflist) { PyObject_ClearWeakRefs(reinterpret_cast(self)); } - // Null out all weak references from children to this message. - GOOGLE_CHECK_EQ(0, ForEachCompositeField(self, ClearWeakReferences())); - - if (self->composite_fields) { - for (CMessage::CompositeFieldsMap::iterator it = - self->composite_fields->begin(); - it != self->composite_fields->end(); it++) { - Py_DECREF(it->second); - } - delete self->composite_fields; - } + // At this point all dependent objects have been removed. + GOOGLE_DCHECK(!self->child_submessages || self->child_submessages->empty()); + GOOGLE_DCHECK(!self->composite_fields || self->composite_fields->empty()); + delete self->child_submessages; + delete self->composite_fields; if (self->unknown_field_set) { unknown_fields::Clear( reinterpret_cast(self->unknown_field_set)); - Py_CLEAR(self->unknown_field_set); } - self->owner.~ThreadUnsafeSharedPtr(); + + CMessage* parent = self->parent; + if (!parent) { + // No parent, we own the message. + delete self->message; + } else if (parent->AsPyObject() == Py_None) { + // Message owned externally: Nothing to dealloc + Py_CLEAR(self->parent); + } else { + // Clear this message from its parent's map. + if (self->parent_field_descriptor->is_repeated()) { + if (parent->child_submessages) + parent->child_submessages->erase(self->message); + } else { + if (parent->composite_fields) + parent->composite_fields->erase(self->parent_field_descriptor); + } + Py_CLEAR(self->parent); + } Py_TYPE(self)->tp_free(reinterpret_cast(self)); } @@ -1553,10 +1394,11 @@ const FieldDescriptor* FindFieldWithOneofs( } bool CheckHasPresence(const FieldDescriptor* field_descriptor, bool in_oneof) { + auto message_name = field_descriptor->containing_type()->name(); if (field_descriptor->label() == FieldDescriptor::LABEL_REPEATED) { PyErr_Format(PyExc_ValueError, - "Protocol message has no singular \"%s\" field.", - field_descriptor->name().c_str()); + "Protocol message %s has no singular \"%s\" field.", + message_name.c_str(), field_descriptor->name().c_str()); return false; } @@ -1564,8 +1406,8 @@ bool CheckHasPresence(const FieldDescriptor* field_descriptor, bool in_oneof) { // HasField() for a oneof *itself* isn't supported. if (in_oneof) { PyErr_Format(PyExc_ValueError, - "Can't test oneof field \"%s\" for presence in proto3, use " - "WhichOneof instead.", + "Can't test oneof field \"%s.%s\" for presence in proto3, " + "use WhichOneof instead.", message_name.c_str(), field_descriptor->containing_oneof()->name().c_str()); return false; } @@ -1578,8 +1420,8 @@ bool CheckHasPresence(const FieldDescriptor* field_descriptor, bool in_oneof) { if (field_descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { PyErr_Format( PyExc_ValueError, - "Can't test non-submessage field \"%s\" for presence in proto3.", - field_descriptor->name().c_str()); + "Can't test non-submessage field \"%s.%s\" for presence in proto3.", + message_name.c_str(), field_descriptor->name().c_str()); return false; } } @@ -1607,7 +1449,8 @@ PyObject* HasField(CMessage* self, PyObject* arg) { FindFieldWithOneofs(message, string(field_name, size), &is_in_oneof); if (field_descriptor == NULL) { if (!is_in_oneof) { - PyErr_Format(PyExc_ValueError, "Unknown field %s.", field_name); + PyErr_Format(PyExc_ValueError, "Protocol message %s has no field %s.", + message->GetDescriptor()->name().c_str(), field_name); return NULL; } else { Py_RETURN_FALSE; @@ -1630,17 +1473,8 @@ PyObject* ClearExtension(CMessage* self, PyObject* extension) { if (descriptor == NULL) { return NULL; } - if (self->composite_fields != NULL) { - CMessage::CompositeFieldsMap::iterator iterator = - self->composite_fields->find(descriptor); - if (iterator != self->composite_fields->end()) { - if (InternalReleaseFieldByDescriptor(self, descriptor, - iterator->second) < 0) { - return NULL; - } - Py_DECREF(iterator->second); - self->composite_fields->erase(iterator); - } + if (InternalReleaseFieldByDescriptor(self, descriptor) < 0) { + return NULL; } return ClearFieldByDescriptor(self, descriptor); } @@ -1658,141 +1492,107 @@ PyObject* HasExtension(CMessage* self, PyObject* extension) { // // The Python API's ClearField() and Clear() methods behave // differently than their C++ counterparts. While the C++ versions -// clears the children the Python versions detaches the children, +// clears the children, the Python versions detaches the children, // without touching their content. This impedance mismatch causes // some complexity in the implementation, which is captured in this // section. // -// When a CMessage field is cleared we need to: +// When one or multiple fields are cleared we need to: // -// * Release the Message used as the backing store for the CMessage -// from its parent. +// * Gather all child objects that need to be detached from the message. +// In composite_fields and child_submessages. // -// * Change the owner field of the released CMessage and all of its -// children to point to the newly released Message. +// * Create a new Python message of the same kind. Use SwapFields() to move +// data from the original message. // -// * Clear the weak references from the released CMessage to the -// parent. -// -// When a RepeatedCompositeContainer field is cleared we need to: -// -// * Release all the Message used as the backing store for the -// CMessages stored in the container. -// -// * Change the owner field of all the released CMessage and all of -// their children to point to the newly released Messages. -// -// * Clear the weak references from the released container to the -// parent. +// * Change the parent of all child objects: update their strong reference +// to their parent, and move their presence in composite_fields and +// child_submessages. -class SetOwnerVisitor : public ChildVisitor { - public: - // new_owner must outlive this object. - explicit SetOwnerVisitor(const CMessage::OwnerRef& new_owner) - : new_owner_(new_owner) {} +// --------------------------------------------------------------------- +// Release a composite child of a CMessage - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - repeated_composite_container::SetOwner(container, new_owner_); +static int InternalReparentFields( + CMessage* self, const std::vector& messages_to_release, + const std::vector& containers_to_release) { + if (messages_to_release.empty() && containers_to_release.empty()) { return 0; } - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - repeated_scalar_container::SetOwner(container, new_owner_); - return 0; + // Move all the passed sub_messages to another message. + CMessage* new_message = cmessage::NewEmptyMessage(self->GetMessageClass()); + if (new_message == nullptr) { + return -1; } + new_message->message = self->message->New(); + ScopedPyObjectPtr holder(reinterpret_cast(new_message)); + new_message->child_submessages = new CMessage::SubMessagesMap(); + new_message->composite_fields = new CMessage::CompositeFieldsMap(); + std::set fields_to_swap; - int VisitMapContainer(MapContainer* container) { - container->SetOwner(new_owner_); - return 0; - } + // In case this the removed fields are the last reference to a message, keep + // a reference. + Py_INCREF(self); - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - return SetOwner(cmessage, new_owner_); + for (const auto& to_release : messages_to_release) { + fields_to_swap.insert(to_release->parent_field_descriptor); + // Reparent + Py_INCREF(new_message); + Py_DECREF(to_release->parent); + to_release->parent = new_message; + self->child_submessages->erase(to_release->message); + new_message->child_submessages->emplace(to_release->message, to_release); } - private: - const CMessage::OwnerRef& new_owner_; -}; - -// Change the owner of this CMessage and all its children, recursively. -int SetOwner(CMessage* self, const CMessage::OwnerRef& new_owner) { - self->owner = new_owner; - if (ForEachCompositeField(self, SetOwnerVisitor(new_owner)) == -1) - return -1; - return 0; -} - -// Releases the message specified by 'field' and returns the -// pointer. If the field does not exist a new message is created using -// 'descriptor'. The caller takes ownership of the returned pointer. -Message* ReleaseMessage(CMessage* self, - const Descriptor* descriptor, - const FieldDescriptor* field_descriptor) { - MessageFactory* message_factory = GetFactoryForMessage(self)->message_factory; - Message* released_message = self->message->GetReflection()->ReleaseMessage( - self->message, field_descriptor, message_factory); - // ReleaseMessage will return NULL which differs from - // child_cmessage->message, if the field does not exist. In this case, - // the latter points to the default instance via a const_cast<>, so we - // have to reset it to a new mutable object since we are taking ownership. - if (released_message == NULL) { - const Message* prototype = message_factory->GetPrototype(descriptor); - GOOGLE_DCHECK(prototype != NULL); - released_message = prototype->New(); + for (const auto& to_release : containers_to_release) { + fields_to_swap.insert(to_release->parent_field_descriptor); + Py_INCREF(new_message); + Py_DECREF(to_release->parent); + to_release->parent = new_message; + self->composite_fields->erase(to_release->parent_field_descriptor); + new_message->composite_fields->emplace(to_release->parent_field_descriptor, + to_release); } - return released_message; -} - -int ReleaseSubMessage(CMessage* self, - const FieldDescriptor* field_descriptor, - CMessage* child_cmessage) { - // Release the Message - CMessage::OwnerRef released_message(ReleaseMessage( - self, child_cmessage->message->GetDescriptor(), field_descriptor)); - child_cmessage->message = released_message.get(); - child_cmessage->owner.swap(released_message); - child_cmessage->parent = NULL; - child_cmessage->parent_field_descriptor = NULL; - child_cmessage->read_only = false; - return ForEachCompositeField(child_cmessage, - SetOwnerVisitor(child_cmessage->owner)); -} + self->message->GetReflection()->SwapFields( + self->message, new_message->message, + std::vector(fields_to_swap.begin(), + fields_to_swap.end())); -struct ReleaseChild : public ChildVisitor { - // message must outlive this object. - explicit ReleaseChild(CMessage* parent) : - parent_(parent) {} + // This might delete the Python message completely if all children were moved. + Py_DECREF(self); - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - return repeated_composite_container::Release(container); - } + return 0; +} - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - return repeated_scalar_container::Release(container); +int InternalReleaseFieldByDescriptor( + CMessage* self, + const FieldDescriptor* field_descriptor) { + if (!field_descriptor->is_repeated() && + field_descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { + // Single scalars are not in any cache. + return 0; } - - int VisitMapContainer(MapContainer* container) { - return container->Release(); + std::vector messages_to_release; + std::vector containers_to_release; + if (self->child_submessages && field_descriptor->is_repeated() && + field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { + for (const auto& child_item : *self->child_submessages) { + if (child_item.second->parent_field_descriptor == field_descriptor) { + messages_to_release.push_back(child_item.second); + } + } } - - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - return ReleaseSubMessage(parent_, field_descriptor, cmessage); + if (self->composite_fields) { + CMessage::CompositeFieldsMap::iterator it = + self->composite_fields->find(field_descriptor); + if (it != self->composite_fields->end()) { + containers_to_release.push_back(it->second); + } } - CMessage* parent_; -}; - -int InternalReleaseFieldByDescriptor( - CMessage* self, - const FieldDescriptor* field_descriptor, - PyObject* composite_field) { - return VisitCompositeField( - field_descriptor, - composite_field, - ReleaseChild(self)); + return InternalReparentFields(self, messages_to_release, + containers_to_release); } PyObject* ClearFieldByDescriptor( @@ -1808,13 +1608,16 @@ PyObject* ClearFieldByDescriptor( } PyObject* ClearField(CMessage* self, PyObject* arg) { - if (!PyString_Check(arg)) { + if (!(PyString_Check(arg) || PyUnicode_Check(arg))) { PyErr_SetString(PyExc_TypeError, "field name must be a string"); return NULL; } #if PY_MAJOR_VERSION < 3 - const char* field_name = PyString_AS_STRING(arg); - Py_ssize_t size = PyString_GET_SIZE(arg); + char* field_name; + Py_ssize_t size; + if (PyString_AsStringAndSize(arg, &field_name, &size) < 0) { + return NULL; + } #else Py_ssize_t size; const char* field_name = PyUnicode_AsUTF8AndSize(arg, &size); @@ -1839,37 +1642,35 @@ PyObject* ClearField(CMessage* self, PyObject* arg) { arg = arg_in_oneof.get(); } - if (self->composite_fields) { - CMessage::CompositeFieldsMap::iterator iterator = - self->composite_fields->find(field_descriptor); - if (iterator != self->composite_fields->end()) { - if (InternalReleaseFieldByDescriptor(self, field_descriptor, - iterator->second) < 0) { - return NULL; - } - Py_DECREF(iterator->second); - self->composite_fields->erase(iterator); - } + if (InternalReleaseFieldByDescriptor(self, field_descriptor) < 0) { + return NULL; } return ClearFieldByDescriptor(self, field_descriptor); } PyObject* Clear(CMessage* self) { AssureWritable(self); - if (ForEachCompositeField(self, ReleaseChild(self)) == -1) - return NULL; + // Detach all current fields of this message + std::vector messages_to_release; + std::vector containers_to_release; + if (self->child_submessages) { + for (const auto& item : *self->child_submessages) { + messages_to_release.push_back(item.second); + } + } if (self->composite_fields) { - for (CMessage::CompositeFieldsMap::iterator it = - self->composite_fields->begin(); - it != self->composite_fields->end(); it++) { - Py_DECREF(it->second); + for (const auto& item : *self->composite_fields) { + containers_to_release.push_back(item.second); } - self->composite_fields->clear(); + } + if (InternalReparentFields(self, messages_to_release, containers_to_release) < + 0) { + return NULL; } if (self->unknown_field_set) { unknown_fields::Clear( reinterpret_cast(self->unknown_field_set)); - Py_CLEAR(self->unknown_field_set); + self->unknown_field_set = nullptr; } self->message->Clear(); Py_RETURN_NONE; @@ -2050,7 +1851,7 @@ PyObject* MergeFrom(CMessage* self, PyObject* arg) { self->message->MergeFrom(*other_message->message); // Child message might be lazily created before MergeFrom. Make sure they // are mutable at this point if child messages are really created. - if (ForEachCompositeField(self, FixupMessageAfterMerge(self)) == -1) { + if (FixupMessageAfterMerge(self) < 0) { return NULL; } @@ -2136,13 +1937,14 @@ static PyObject* MergeFromString(CMessage* self, PyObject* arg) { reinterpret_cast(data), data_length); if (allow_oversize_protos) { input.SetTotalBytesLimit(INT_MAX, INT_MAX); + input.SetRecursionLimit(INT_MAX); } PyMessageFactory* factory = GetFactoryForMessage(self); input.SetExtensionRegistry(factory->pool->pool, factory->message_factory); bool success = self->message->MergePartialFromCodedStream(&input); // Child message might be lazily created before MergeFrom. Make sure they // are mutable at this point if child messages are really created. - if (ForEachCompositeField(self, FixupMessageAfterMerge(self)) == -1) { + if (FixupMessageAfterMerge(self) < 0) { return NULL; } @@ -2432,7 +2234,7 @@ PyObject* InternalGetScalar(const Message* message, return result; } -PyObject* InternalGetSubMessage( +CMessage* InternalGetSubMessage( CMessage* self, const FieldDescriptor* field_descriptor) { const Reflection* reflection = self->message->GetReflection(); PyMessageFactory* factory = GetFactoryForMessage(self); @@ -2441,7 +2243,7 @@ PyObject* InternalGetSubMessage( CMessageClass* message_class = message_factory::GetOrCreateMessageClass( factory, field_descriptor->message_type()); - ScopedPyObjectPtr message_class_handler( + ScopedPyObjectPtr message_class_owner( reinterpret_cast(message_class)); if (message_class == NULL) { return NULL; @@ -2452,13 +2254,12 @@ PyObject* InternalGetSubMessage( return NULL; } - cmsg->owner = self->owner; + Py_INCREF(self); cmsg->parent = self; cmsg->parent_field_descriptor = field_descriptor; cmsg->read_only = !reflection->HasField(*self->message, field_descriptor); cmsg->message = const_cast(&sub_message); - - return reinterpret_cast(cmsg); + return cmsg; } int InternalSetNonOneofScalar( @@ -2654,6 +2455,13 @@ PyObject* SetState(CMessage* self, PyObject* state) { if (serialized == NULL) { return NULL; } +#if PY_MAJOR_VERSION >= 3 + // On Python 3, using encoding='latin1' is required for unpickling + // protos pickled by Python 2. + if (!PyBytes_Check(serialized)) { + serialized = PyUnicode_AsEncodedString(serialized, "latin1", NULL); + } +#endif if (ScopedPyObjectPtr(ParseFromString(self, serialized)) == NULL) { return NULL; } @@ -2693,8 +2501,9 @@ static PyObject* GetExtensionDict(CMessage* self, void *closure) { static PyObject* UnknownFieldSet(CMessage* self) { if (self->unknown_field_set == NULL) { self->unknown_field_set = unknown_fields::NewPyUnknownFields(self); + } else { + Py_INCREF(self->unknown_field_set); } - Py_INCREF(self->unknown_field_set); return self->unknown_field_set; } @@ -2779,17 +2588,23 @@ static PyMethodDef Methods[] = { { NULL, NULL} }; -static bool SetCompositeField(CMessage* self, const FieldDescriptor* field, - PyObject* value) { +bool SetCompositeField(CMessage* self, const FieldDescriptor* field, + ContainerBase* value) { if (self->composite_fields == NULL) { self->composite_fields = new CMessage::CompositeFieldsMap(); } - Py_INCREF(value); - Py_XDECREF((*self->composite_fields)[field]); (*self->composite_fields)[field] = value; return true; } +bool SetSubmessage(CMessage* self, CMessage* submessage) { + if (self->child_submessages == NULL) { + self->child_submessages = new CMessage::SubMessagesMap(); + } + (*self->child_submessages)[submessage->message] = submessage; + return true; +} + PyObject* GetAttr(PyObject* pself, PyObject* name) { CMessage* self = reinterpret_cast(pself); PyObject* result = PyObject_GenericGetAttr( @@ -2812,14 +2627,12 @@ PyObject* GetFieldValue(CMessage* self, CMessage::CompositeFieldsMap::iterator it = self->composite_fields->find(field_descriptor); if (it != self->composite_fields->end()) { - PyObject* value = it->second; + ContainerBase* value = it->second; Py_INCREF(value); - return value; + return value->AsPyObject(); } } - const Descriptor* message_descriptor = - (reinterpret_cast(Py_TYPE(self)))->message_descriptor; if (self->message->GetDescriptor() != field_descriptor->containing_type()) { PyErr_Format(PyExc_TypeError, "descriptor to field '%s' doesn't apply to '%s' object", @@ -2828,8 +2641,13 @@ PyObject* GetFieldValue(CMessage* self, return NULL; } + if (!field_descriptor->is_repeated() && + field_descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { + return InternalGetScalar(self->message, field_descriptor); + } + + ContainerBase* py_container = nullptr; if (field_descriptor->is_map()) { - PyObject* py_container = NULL; const Descriptor* entry_type = field_descriptor->message_type(); const FieldDescriptor* value_type = entry_type->FindFieldByName("value"); if (value_type->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { @@ -2843,18 +2661,7 @@ PyObject* GetFieldValue(CMessage* self, } else { py_container = NewScalarMapContainer(self, field_descriptor); } - if (py_container == NULL) { - return NULL; - } - if (!SetCompositeField(self, field_descriptor, py_container)) { - Py_DECREF(py_container); - return NULL; - } - return py_container; - } - - if (field_descriptor->label() == FieldDescriptor::LABEL_REPEATED) { - PyObject* py_container = NULL; + } else if (field_descriptor->is_repeated()) { if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { CMessageClass* message_class = message_factory::GetMessageClass( GetFactoryForMessage(self), field_descriptor->message_type()); @@ -2864,32 +2671,24 @@ PyObject* GetFieldValue(CMessage* self, py_container = repeated_composite_container::NewContainer( self, field_descriptor, message_class); } else { - py_container = repeated_scalar_container::NewContainer( - self, field_descriptor); - } - if (py_container == NULL) { - return NULL; - } - if (!SetCompositeField(self, field_descriptor, py_container)) { - Py_DECREF(py_container); - return NULL; + py_container = + repeated_scalar_container::NewContainer(self, field_descriptor); } - return py_container; + } else if (field_descriptor->cpp_type() == + FieldDescriptor::CPPTYPE_MESSAGE) { + py_container = InternalGetSubMessage(self, field_descriptor); + } else { + PyErr_SetString(PyExc_SystemError, "Should never happen"); } - if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - PyObject* sub_message = InternalGetSubMessage(self, field_descriptor); - if (sub_message == NULL) { - return NULL; - } - if (!SetCompositeField(self, field_descriptor, sub_message)) { - Py_DECREF(sub_message); - return NULL; - } - return sub_message; + if (py_container == NULL) { + return NULL; } - - return InternalGetScalar(self->message, field_descriptor); + if (!SetCompositeField(self, field_descriptor, py_container)) { + Py_DECREF(py_container); + return NULL; + } + return py_container->AsPyObject(); } int SetFieldValue(CMessage* self, const FieldDescriptor* field_descriptor, @@ -2917,8 +2716,82 @@ int SetFieldValue(CMessage* self, const FieldDescriptor* field_descriptor, return InternalSetScalar(self, field_descriptor, value); } } + } // namespace cmessage +// All containers which are not messages: +// - Make a new parent message +// - Copy the field +// - return the field. +PyObject* ContainerBase::DeepCopy() { + CMessage* new_parent = + cmessage::NewEmptyMessage(this->parent->GetMessageClass()); + new_parent->message = this->parent->message->New(); + + // Copy the map field into the new message. + this->parent->message->GetReflection()->SwapFields( + this->parent->message, new_parent->message, + {this->parent_field_descriptor}); + this->parent->message->MergeFrom(*new_parent->message); + + PyObject* result = + cmessage::GetFieldValue(new_parent, this->parent_field_descriptor); + Py_DECREF(new_parent); + return result; +} + +void ContainerBase::RemoveFromParentCache() { + CMessage* parent = this->parent; + if (parent) { + if (parent->composite_fields) + parent->composite_fields->erase(this->parent_field_descriptor); + Py_CLEAR(parent); + } +} + +CMessage* CMessage::BuildSubMessageFromPointer( + const FieldDescriptor* field_descriptor, Message* sub_message, + CMessageClass* message_class) { + if (!this->child_submessages) { + this->child_submessages = new CMessage::SubMessagesMap(); + } + CMessage* cmsg = FindPtrOrNull( + *this->child_submessages, sub_message); + if (cmsg) { + Py_INCREF(cmsg); + } else { + cmsg = cmessage::NewEmptyMessage(message_class); + + if (cmsg == NULL) { + return NULL; + } + cmsg->message = sub_message; + Py_INCREF(this); + cmsg->parent = this; + cmsg->parent_field_descriptor = field_descriptor; + cmessage::SetSubmessage(this, cmsg); + } + return cmsg; +} + +CMessage* CMessage::MaybeReleaseSubMessage(Message* sub_message) { + if (!this->child_submessages) { + return nullptr; + } + CMessage* released = FindPtrOrNull( + *this->child_submessages, sub_message); + if (!released) { + return nullptr; + } + // The target message will now own its content. + Py_CLEAR(released->parent); + released->parent_field_descriptor = nullptr; + released->read_only = false; + // Delete it from the cache. + this->child_submessages->erase(sub_message); + return released; +} + static CMessageClass _CMessage_Type = { { { PyVarObject_HEAD_INIT(&_CMessageClass_Type, 0) FULL_MODULE_NAME ".CMessage", // tp_name @@ -2999,10 +2872,11 @@ Message* PyMessage_GetMutableMessagePointer(PyObject* msg) { PyErr_SetString(PyExc_TypeError, "Not a Message instance"); return NULL; } + CMessage* cmsg = reinterpret_cast(msg); - CMessage* cmsg = reinterpret_cast(msg); - if (cmsg->composite_fields && !cmsg->composite_fields->empty()) { + if ((cmsg->composite_fields && !cmsg->composite_fields->empty()) || + (cmsg->child_submessages && !cmsg->child_submessages->empty())) { // There is currently no way of accurately syncing arbitrary changes to // the underlying C++ message back to the CMessage (e.g. removed repeated // composite containers). We only allow direct mutation of the underlying @@ -3016,6 +2890,34 @@ Message* PyMessage_GetMutableMessagePointer(PyObject* msg) { return cmsg->message; } +PyObject* PyMessage_NewMessageOwnedExternally(Message* message, + PyObject* message_factory) { + if (message_factory) { + PyErr_SetString(PyExc_NotImplementedError, + "Default message_factory=NULL is the only supported value"); + return NULL; + } + if (message->GetReflection()->GetMessageFactory() != + MessageFactory::generated_factory()) { + PyErr_SetString(PyExc_TypeError, + "Message pointer was not created from the default factory"); + return NULL; + } + + CMessageClass* message_class = message_factory::GetOrCreateMessageClass( + GetDefaultDescriptorPool()->py_message_factory, message->GetDescriptor()); + + CMessage* self = cmessage::NewEmptyMessage(message_class); + if (self == NULL) { + return NULL; + } + Py_DECREF(message_class); + self->message = message; + Py_INCREF(Py_None); + self->parent = reinterpret_cast(Py_None); + return self->AsPyObject(); +} + void InitGlobals() { // TODO(gps): Check all return values in this function for NULL and propagate // the error (MemoryError) on up to result in an import failure. These should @@ -3144,6 +3046,11 @@ bool InitProto2MessageModule(PyObject *m) { PyModule_AddObject( m, "ExtensionDict", reinterpret_cast(&ExtensionDict_Type)); + if (PyType_Ready(&ExtensionIterator_Type) < 0) { + return false; + } + PyModule_AddObject(m, "ExtensionIterator", + reinterpret_cast(&ExtensionIterator_Type)); // Expose the DescriptorPool used to hold all descriptors added from generated // pb2.py files. diff --git a/python/google/protobuf/pyext/message.h b/python/google/protobuf/pyext/message.h index cbd422be02..55be297fce 100644 --- a/python/google/protobuf/pyext/message.h +++ b/python/google/protobuf/pyext/message.h @@ -41,7 +41,6 @@ #include #include -#include namespace google { namespace protobuf { @@ -57,39 +56,51 @@ namespace python { struct ExtensionDict; struct PyMessageFactory; +struct CMessageClass; -typedef struct CMessage { - PyObject_HEAD; - - // This is the top-level C++ Message object that owns the whole - // proto tree. Every Python CMessage holds a reference to it in - // order to keep it alive as long as there's a Python object that - // references any part of the tree. +// Most of the complexity of the Message class comes from the "Release" +// behavior: +// +// When a field is cleared, it is only detached from its message. Existing +// references to submessages, to repeated container etc. won't see any change, +// as if the data was effectively managed by these containers. +// +// ExtensionDicts and UnknownFields containers do NOT follow this rule. They +// don't store any data, and always refer to their parent message. - typedef ThreadUnsafeSharedPtr OwnerRef; - OwnerRef owner; +struct ContainerBase { + PyObject_HEAD; - // Weak reference to a parent CMessage object. This is NULL for any top-level - // message and is set for any child message (i.e. a child submessage or a - // part of a repeated composite field). + // Strong reference to a parent message object. For a CMessage there are three + // cases: + // - For a top-level message, this pointer is NULL. + // - For a sub-message, this points to the parent message. + // - For a message managed externally, this is a owned reference to Py_None. // - // Used to make sure all ancestors are also mutable when first modifying - // a child submessage (in other words, turning a default message instance - // into a mutable one). - // - // If a submessage is released (becomes a new top-level message), this field - // MUST be set to NULL. The parent may get deallocated and further attempts - // to use this pointer will result in a crash. + // For all other types: repeated containers, maps, it always point to a + // valid parent CMessage. struct CMessage* parent; - // Pointer to the parent's descriptor that describes this submessage. - // Used together with the parent's message when making a default message - // instance mutable. - // The pointer is owned by the global DescriptorPool. + // If this object belongs to a parent message, describes which field it comes + // from. + // The pointer is owned by the DescriptorPool (which is kept alive + // through the message's Python class) const FieldDescriptor* parent_field_descriptor; - // Pointer to the C++ Message object for this CMessage. The - // CMessage does not own this pointer. + PyObject* AsPyObject() { return reinterpret_cast(this); } + + // The Three methods below are only used by Repeated containers, and Maps. + + // This implementation works for all containers which have a parent. + PyObject* DeepCopy(); + // Delete this container object from its parent. Does not work for messages. + void RemoveFromParentCache(); +}; + +typedef struct CMessage : public ContainerBase { + // Pointer to the C++ Message object for this CMessage. + // - If this object has no parent, we own this pointer. + // - If this object has a parent message, the parent owns this pointer. Message* message; // Indicates this submessage is pointing to a default instance of a message. @@ -97,23 +108,37 @@ typedef struct CMessage { // made writable, at which point this field is set to false. bool read_only; - // A mapping indexed by field, containing CMessage, - // RepeatedCompositeContainer, and RepeatedScalarContainer - // objects. Used as a cache to make sure we don't have to make a - // Python wrapper for the C++ Message objects on every access, or - // deal with the synchronization nightmare that could create. - // Also cache extension fields. - // The FieldDescriptor is owned by the message's pool; PyObject references - // are owned. - typedef std::unordered_map + // A mapping indexed by field, containing weak references to contained objects + // which need to implement the "Release" mechanism: + // direct submessages, RepeatedCompositeContainer, RepeatedScalarContainer + // and MapContainer. + typedef std::unordered_map CompositeFieldsMap; CompositeFieldsMap* composite_fields; + // A mapping containing weak references to indirect child messages, accessed + // through containers: repeated messages, and values of message maps. + // This avoid the creation of similar maps in each of those containers. + typedef std::unordered_map SubMessagesMap; + SubMessagesMap* child_submessages; + // A reference to PyUnknownFields. PyObject* unknown_field_set; // Implements the "weakref" protocol for this object. PyObject* weakreflist; + + // Return a *borrowed* reference to the message class. + CMessageClass* GetMessageClass() { + return reinterpret_cast(Py_TYPE(this)); + } + + // For container containing messages, return a Python object for the given + // pointer to a message. + CMessage* BuildSubMessageFromPointer(const FieldDescriptor* field_descriptor, + Message* sub_message, + CMessageClass* message_class); + CMessage* MaybeReleaseSubMessage(Message* sub_message); } CMessage; // The (meta) type of all Messages classes. @@ -129,12 +154,13 @@ struct CMessageClass { const Descriptor* message_descriptor; // Owned reference, used to keep the pointer above alive. + // This reference must stay alive until all message pointers are destructed. PyObject* py_message_descriptor; // The Python MessageFactory used to create the class. It is needed to resolve // fields descriptors, including extensions fields; its C++ MessageFactory is // used to instantiate submessages. - // We own the reference, because it's important to keep the factory alive. + // This reference must stay alive until all message pointers are destructed. PyMessageFactory* py_message_factory; PyObject* AsPyObject() { @@ -160,21 +186,16 @@ const FieldDescriptor* GetExtensionDescriptor(PyObject* extension); // submessage as the result is cached in composite_fields. // // Corresponds to reflection api method GetMessage. -PyObject* InternalGetSubMessage( +CMessage* InternalGetSubMessage( CMessage* self, const FieldDescriptor* field_descriptor); -// Deletes a range of C++ submessages in a repeated field (following a +// Deletes a range of items in a repeated field (following a // removal in a RepeatedCompositeContainer). // -// Releases messages to the provided cmessage_list if it is not NULL rather -// than just removing them from the underlying proto. This cmessage_list must -// have a CMessage for each underlying submessage. The CMessages referred to -// by slice will be removed from cmessage_list by this function. -// // Corresponds to reflection api method RemoveLast. -int InternalDeleteRepeatedField(CMessage* self, - const FieldDescriptor* field_descriptor, - PyObject* slice, PyObject* cmessage_list); +int DeleteRepeatedField(CMessage* self, + const FieldDescriptor* field_descriptor, + PyObject* slice); // Sets the specified scalar value to the message. int InternalSetScalar(CMessage* self, @@ -192,6 +213,11 @@ int InternalSetNonOneofScalar(Message* message, PyObject* InternalGetScalar(const Message* message, const FieldDescriptor* field_descriptor); +bool SetCompositeField(CMessage* self, const FieldDescriptor* field, + ContainerBase* value); + +bool SetSubmessage(CMessage* self, CMessage* submessage); + // Clears the message, removing all contained data. Extension dictionary and // submessages are released first if there are remaining external references. // @@ -247,11 +273,6 @@ int SetFieldValue(CMessage* self, const FieldDescriptor* field_descriptor, PyObject* FindInitializationErrors(CMessage* self); -// Set the owner field of self and any children of self, recursively. -// Used when self is being released and thus has a new owner (the -// released Message.) -int SetOwner(CMessage* self, const CMessage::OwnerRef& new_owner); - int AssureWritable(CMessage* self); // Returns the message factory for the given message. @@ -343,17 +364,17 @@ extern PyObject* PickleError_class; const Message* PyMessage_GetMessagePointer(PyObject* msg); Message* PyMessage_GetMutableMessagePointer(PyObject* msg); +PyObject* PyMessage_NewMessageOwnedExternally(Message* message, + PyObject* message_factory); bool InitProto2MessageModule(PyObject *m); -#if LANG_CXX11 // These are referenced by repeated_scalar_container, and must // be explicitly instantiated. extern template bool CheckAndGetInteger(PyObject*, int32*); extern template bool CheckAndGetInteger(PyObject*, int64*); extern template bool CheckAndGetInteger(PyObject*, uint32*); extern template bool CheckAndGetInteger(PyObject*, uint64*); -#endif } // namespace python } // namespace protobuf diff --git a/python/google/protobuf/pyext/message_factory.cc b/python/google/protobuf/pyext/message_factory.cc index efaa261701..5fed13b943 100644 --- a/python/google/protobuf/pyext/message_factory.cc +++ b/python/google/protobuf/pyext/message_factory.cc @@ -43,9 +43,11 @@ #error "Python 3.0 - 3.2 are not supported." #endif #define PyString_AsStringAndSize(ob, charpp, sizep) \ - (PyUnicode_Check(ob)? \ - ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \ - PyBytes_AsStringAndSize(ob, (charpp), (sizep))) + (PyUnicode_Check(ob) ? ((*(charpp) = const_cast( \ + PyUnicode_AsUTF8AndSize(ob, (sizep)))) == NULL \ + ? -1 \ + : 0) \ + : PyBytes_AsStringAndSize(ob, (charpp), (sizep))) #endif namespace google { @@ -67,9 +69,7 @@ PyMessageFactory* NewMessageFactory(PyTypeObject* type, PyDescriptorPool* pool) factory->message_factory = message_factory; factory->pool = pool; - // TODO(amauryfa): When the MessageFactory is not created from the - // DescriptorPool this reference should be owned, not borrowed. - // Py_INCREF(pool); + Py_INCREF(pool); factory->classes_by_descriptor = new PyMessageFactory::ClassesByMessageMap(); @@ -105,19 +105,37 @@ PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) { static void Dealloc(PyObject* pself) { PyMessageFactory* self = reinterpret_cast(pself); - // TODO(amauryfa): When the MessageFactory is not created from the - // DescriptorPool this reference should be owned, not borrowed. - // Py_CLEAR(self->pool); typedef PyMessageFactory::ClassesByMessageMap::iterator iterator; for (iterator it = self->classes_by_descriptor->begin(); it != self->classes_by_descriptor->end(); ++it) { - Py_DECREF(it->second); + Py_CLEAR(it->second); } delete self->classes_by_descriptor; delete self->message_factory; + Py_CLEAR(self->pool); Py_TYPE(self)->tp_free(pself); } +static int GcTraverse(PyObject* pself, visitproc visit, void* arg) { + PyMessageFactory* self = reinterpret_cast(pself); + Py_VISIT(self->pool); + for (const auto& desc_and_class : *self->classes_by_descriptor) { + Py_VISIT(desc_and_class.second); + } + return 0; +} + +static int GcClear(PyObject* pself) { + PyMessageFactory* self = reinterpret_cast(pself); + // Here it's important to not clear self->pool, so that the C++ DescriptorPool + // is still alive when self->message_factory is destructed. + for (auto& desc_and_class : *self->classes_by_descriptor) { + Py_CLEAR(desc_and_class.second); + } + + return 0; +} + // Add a message class to our database. int RegisterMessageClass(PyMessageFactory* self, const Descriptor* message_descriptor, @@ -232,44 +250,44 @@ static PyGetSetDef Getters[] = { PyTypeObject PyMessageFactory_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME - ".MessageFactory", // tp_name - sizeof(PyMessageFactory), // tp_basicsize - 0, // tp_itemsize - message_factory::Dealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - 0, // tp_as_mapping - 0, // tp_hash - 0, // tp_call - 0, // tp_str - 0, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags - "A static Message Factory", // tp_doc - 0, // tp_traverse - 0, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - message_factory::Methods, // tp_methods - 0, // tp_members - message_factory::Getters, // tp_getset - 0, // tp_base - 0, // tp_dict - 0, // tp_descr_get - 0, // tp_descr_set - 0, // tp_dictoffset - 0, // tp_init - 0, // tp_alloc - message_factory::New, // tp_new - PyObject_Del, // tp_free + ".MessageFactory", // tp_name + sizeof(PyMessageFactory), // tp_basicsize + 0, // tp_itemsize + message_factory::Dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, // tp_flags + "A static Message Factory", // tp_doc + message_factory::GcTraverse, // tp_traverse + message_factory::GcClear, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + message_factory::Methods, // tp_methods + 0, // tp_members + message_factory::Getters, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init + 0, // tp_alloc + message_factory::New, // tp_new + PyObject_GC_Del, // tp_free }; bool InitMessageFactory() { diff --git a/python/google/protobuf/pyext/message_factory.h b/python/google/protobuf/pyext/message_factory.h index 06444b0a2a..515c29cdb8 100644 --- a/python/google/protobuf/pyext/message_factory.h +++ b/python/google/protobuf/pyext/message_factory.h @@ -57,9 +57,8 @@ struct PyMessageFactory { // The C++ one creates messages, when the Python one creates classes. MessageFactory* message_factory; - // borrowed reference to a Python DescriptorPool. - // TODO(amauryfa): invert the dependency: the MessageFactory owns the - // DescriptorPool, not the opposite. + // Owned reference to a Python DescriptorPool. + // This reference must stay until the message_factory is destructed. PyDescriptorPool* pool; // Make our own mapping to retrieve Python classes from C++ descriptors. diff --git a/python/google/protobuf/pyext/message_module.cc b/python/google/protobuf/pyext/message_module.cc index 8d465eb594..4bb35b3193 100644 --- a/python/google/protobuf/pyext/message_module.cc +++ b/python/google/protobuf/pyext/message_module.cc @@ -30,7 +30,9 @@ #include +#include #include +#include #include #include @@ -45,37 +47,42 @@ struct ApiImplementation : google::protobuf::python::PyProto_API { google::protobuf::Message* GetMutableMessagePointer(PyObject* msg) const override { return google::protobuf::python::PyMessage_GetMutableMessagePointer(msg); } + const google::protobuf::DescriptorPool* GetDefaultDescriptorPool() const override { + return google::protobuf::python::GetDefaultDescriptorPool()->pool; + } + + google::protobuf::MessageFactory* GetDefaultMessageFactory() const override { + return google::protobuf::python::GetDefaultDescriptorPool() + ->py_message_factory->message_factory; + } }; } // namespace static const char module_docstring[] = -"python-proto2 is a module that can be used to enhance proto2 Python API\n" -"performance.\n" -"\n" -"It provides access to the protocol buffers C++ reflection API that\n" -"implements the basic protocol buffer functions."; + "python-proto2 is a module that can be used to enhance proto2 Python API\n" + "performance.\n" + "\n" + "It provides access to the protocol buffers C++ reflection API that\n" + "implements the basic protocol buffer functions."; static PyMethodDef ModuleMethods[] = { - {"SetAllowOversizeProtos", - (PyCFunction)google::protobuf::python::cmessage::SetAllowOversizeProtos, - METH_O, "Enable/disable oversize proto parsing."}, - // DO NOT USE: For migration and testing only. - { NULL, NULL} -}; + {"SetAllowOversizeProtos", + (PyCFunction)google::protobuf::python::cmessage::SetAllowOversizeProtos, METH_O, + "Enable/disable oversize proto parsing."}, + // DO NOT USE: For migration and testing only. + {NULL, NULL}}; #if PY_MAJOR_VERSION >= 3 -static struct PyModuleDef _module = { - PyModuleDef_HEAD_INIT, - "_message", - module_docstring, - -1, - ModuleMethods, /* m_methods */ - NULL, - NULL, - NULL, - NULL -}; +static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT, + "_message", + module_docstring, + -1, + ModuleMethods, /* m_methods */ + NULL, + NULL, + NULL, + NULL}; #define INITFUNC PyInit__message #define INITFUNC_ERRORVAL NULL #else // Python 2 diff --git a/python/google/protobuf/pyext/proto2_api_test.proto b/python/google/protobuf/pyext/proto2_api_test.proto index 18aecfb7d6..1fd78e8402 100644 --- a/python/google/protobuf/pyext/proto2_api_test.proto +++ b/python/google/protobuf/pyext/proto2_api_test.proto @@ -30,10 +30,10 @@ syntax = "proto2"; -import "google/protobuf/internal/cpp/proto1_api_test.proto"; - package google.protobuf.python.internal; +import "google/protobuf/internal/cpp/proto1_api_test.proto"; + message TestNestedProto1APIMessage { optional int32 a = 1; optional TestMessage.NestedMessage b = 2; diff --git a/python/google/protobuf/pyext/repeated_composite_container.cc b/python/google/protobuf/pyext/repeated_composite_container.cc index d6bc3d7b26..4188b5f4a1 100644 --- a/python/google/protobuf/pyext/repeated_composite_container.cc +++ b/python/google/protobuf/pyext/repeated_composite_container.cc @@ -46,6 +46,7 @@ #include #include #include +#include #if PY_MAJOR_VERSION >= 3 #define PyInt_Check PyLong_Check @@ -59,22 +60,6 @@ namespace python { namespace repeated_composite_container { -// TODO(tibell): We might also want to check: -// GOOGLE_CHECK_NOTNULL((self)->owner.get()); -#define GOOGLE_CHECK_ATTACHED(self) \ - do { \ - GOOGLE_CHECK_NOTNULL((self)->message); \ - GOOGLE_CHECK_NOTNULL((self)->parent_field_descriptor); \ - } while (0); - -#define GOOGLE_CHECK_RELEASED(self) \ - do { \ - GOOGLE_CHECK((self)->owner.get() == NULL); \ - GOOGLE_CHECK((self)->message == NULL); \ - GOOGLE_CHECK((self)->parent_field_descriptor == NULL); \ - GOOGLE_CHECK((self)->parent == NULL); \ - } while (0); - // --------------------------------------------------------------------- // len() @@ -82,75 +67,28 @@ static Py_ssize_t Length(PyObject* pself) { RepeatedCompositeContainer* self = reinterpret_cast(pself); - Message* message = self->message; - if (message != NULL) { - return message->GetReflection()->FieldSize(*message, - self->parent_field_descriptor); - } else { - // The container has been released (i.e. by a call to Clear() or - // ClearField() on the parent) and thus there's no message. - return PyList_GET_SIZE(self->child_messages); - } -} - -// Returns 0 if successful; returns -1 and sets an exception if -// unsuccessful. -static int UpdateChildMessages(RepeatedCompositeContainer* self) { - if (self->message == NULL) - return 0; - - // A MergeFrom on a parent message could have caused extra messages to be - // added in the underlying protobuf so add them to our list. They can never - // be removed in such a way so there's no need to worry about that. - Py_ssize_t message_length = Length(reinterpret_cast(self)); - Py_ssize_t child_length = PyList_GET_SIZE(self->child_messages); - Message* message = self->message; - const Reflection* reflection = message->GetReflection(); - for (Py_ssize_t i = child_length; i < message_length; ++i) { - const Message& sub_message = reflection->GetRepeatedMessage( - *(self->message), self->parent_field_descriptor, i); - CMessage* cmsg = cmessage::NewEmptyMessage(self->child_message_class); - ScopedPyObjectPtr py_cmsg(reinterpret_cast(cmsg)); - if (cmsg == NULL) { - return -1; - } - cmsg->owner = self->owner; - cmsg->message = const_cast(&sub_message); - cmsg->parent = self->parent; - if (PyList_Append(self->child_messages, py_cmsg.get()) < 0) { - return -1; - } - } - return 0; + Message* message = self->parent->message; + return message->GetReflection()->FieldSize(*message, + self->parent_field_descriptor); } // --------------------------------------------------------------------- // add() -static PyObject* AddToAttached(RepeatedCompositeContainer* self, - PyObject* args, - PyObject* kwargs) { - GOOGLE_CHECK_ATTACHED(self); - - if (UpdateChildMessages(self) < 0) { - return NULL; - } +PyObject* Add(RepeatedCompositeContainer* self, PyObject* args, + PyObject* kwargs) { if (cmessage::AssureWritable(self->parent) == -1) return NULL; - Message* message = self->message; + Message* message = self->parent->message; Message* sub_message = message->GetReflection()->AddMessage( message, self->parent_field_descriptor, self->child_message_class->py_message_factory->message_factory); - CMessage* cmsg = cmessage::NewEmptyMessage(self->child_message_class); - if (cmsg == NULL) - return NULL; + CMessage* cmsg = self->parent->BuildSubMessageFromPointer( + self->parent_field_descriptor, sub_message, self->child_message_class); - cmsg->owner = self->owner; - cmsg->message = sub_message; - cmsg->parent = self->parent; if (cmessage::InitAttributes(cmsg, args, kwargs) < 0) { message->GetReflection()->RemoveLast( message, self->parent_field_descriptor); @@ -158,43 +96,74 @@ static PyObject* AddToAttached(RepeatedCompositeContainer* self, return NULL; } - PyObject* py_cmsg = reinterpret_cast(cmsg); - if (PyList_Append(self->child_messages, py_cmsg) < 0) { - Py_DECREF(py_cmsg); - return NULL; - } - return py_cmsg; + return cmsg->AsPyObject(); } -static PyObject* AddToReleased(RepeatedCompositeContainer* self, - PyObject* args, - PyObject* kwargs) { - GOOGLE_CHECK_RELEASED(self); +static PyObject* AddMethod(PyObject* self, PyObject* args, PyObject* kwargs) { + return Add(reinterpret_cast(self), args, kwargs); +} - // Create a new Message detached from the rest. - PyObject* py_cmsg = PyEval_CallObjectWithKeywords( - self->child_message_class->AsPyObject(), args, kwargs); - if (py_cmsg == NULL) - return NULL; +// --------------------------------------------------------------------- +// append() - if (PyList_Append(self->child_messages, py_cmsg) < 0) { - Py_DECREF(py_cmsg); - return NULL; +static PyObject* AddMessage(RepeatedCompositeContainer* self, PyObject* value) { + cmessage::AssureWritable(self->parent); + PyObject* py_cmsg; + Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + py_cmsg = Add(self, nullptr, nullptr); + if (py_cmsg == nullptr) return nullptr; + CMessage* cmsg = reinterpret_cast(py_cmsg); + if (ScopedPyObjectPtr(cmessage::MergeFrom(cmsg, value)) == nullptr) { + reflection->RemoveLast( + message, self->parent_field_descriptor); + Py_DECREF(cmsg); + return nullptr; } return py_cmsg; } -PyObject* Add(RepeatedCompositeContainer* self, - PyObject* args, - PyObject* kwargs) { - if (self->message == NULL) - return AddToReleased(self, args, kwargs); - else - return AddToAttached(self, args, kwargs); +static PyObject* AppendMethod(PyObject* pself, PyObject* value) { + RepeatedCompositeContainer* self = + reinterpret_cast(pself); + ScopedPyObjectPtr py_cmsg(AddMessage(self, value)); + if (py_cmsg == nullptr) { + return nullptr; + } + + Py_RETURN_NONE; } -static PyObject* AddMethod(PyObject* self, PyObject* args, PyObject* kwargs) { - return Add(reinterpret_cast(self), args, kwargs); +// --------------------------------------------------------------------- +// insert() +static PyObject* Insert(PyObject* pself, PyObject* args) { + RepeatedCompositeContainer* self = + reinterpret_cast(pself); + + Py_ssize_t index; + PyObject* value; + if (!PyArg_ParseTuple(args, "nO", &index, &value)) { + return nullptr; + } + + ScopedPyObjectPtr py_cmsg(AddMessage(self, value)); + if (py_cmsg == nullptr) { + return nullptr; + } + + // Swap the element to right position. + Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + const FieldDescriptor* field_descriptor = self->parent_field_descriptor; + Py_ssize_t length = reflection->FieldSize(*message, field_descriptor) - 1; + Py_ssize_t end_index = index; + if (end_index < 0) end_index += length; + if (end_index < 0) end_index = 0; + for (Py_ssize_t i = length; i > end_index; i --) { + reflection->SwapElements(message, field_descriptor, i, i - 1); + } + + Py_RETURN_NONE; } // --------------------------------------------------------------------- @@ -202,9 +171,6 @@ static PyObject* AddMethod(PyObject* self, PyObject* args, PyObject* kwargs) { PyObject* Extend(RepeatedCompositeContainer* self, PyObject* value) { cmessage::AssureWritable(self->parent); - if (UpdateChildMessages(self) < 0) { - return NULL; - } ScopedPyObjectPtr iter(PyObject_GetIter(value)); if (iter == NULL) { PyErr_SetString(PyExc_TypeError, "Value must be iterable"); @@ -237,9 +203,6 @@ static PyObject* ExtendMethod(PyObject* self, PyObject* value) { } PyObject* MergeFrom(RepeatedCompositeContainer* self, PyObject* other) { - if (UpdateChildMessages(self) < 0) { - return NULL; - } return Extend(self, other); } @@ -247,13 +210,70 @@ static PyObject* MergeFromMethod(PyObject* self, PyObject* other) { return MergeFrom(reinterpret_cast(self), other); } -PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice) { - if (UpdateChildMessages(self) < 0) { +// This function does not check the bounds. +static PyObject* GetItem(RepeatedCompositeContainer* self, Py_ssize_t index, + Py_ssize_t length = -1) { + if (length == -1) { + Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + length = reflection->FieldSize(*message, self->parent_field_descriptor); + } + if (index < 0 || index >= length) { + PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index); + return NULL; + } + Message* message = self->parent->message; + Message* sub_message = message->GetReflection()->MutableRepeatedMessage( + message, self->parent_field_descriptor, index); + return self->parent + ->BuildSubMessageFromPointer(self->parent_field_descriptor, sub_message, + self->child_message_class) + ->AsPyObject(); +} + +PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* item) { + Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + Py_ssize_t length = + reflection->FieldSize(*message, self->parent_field_descriptor); + + if (PyIndex_Check(item)) { + Py_ssize_t index; + index = PyNumber_AsSsize_t(item, PyExc_IndexError); + if (index == -1 && PyErr_Occurred()) return NULL; + if (index < 0) index += length; + return GetItem(self, index, length); + } else if (PySlice_Check(item)) { + Py_ssize_t from, to, step, slicelength, cur, i; + PyObject* result; + +#if PY_MAJOR_VERSION >= 3 + if (PySlice_GetIndicesEx(item, + length, &from, &to, &step, &slicelength) == -1) { +#else + if (PySlice_GetIndicesEx(reinterpret_cast(item), + length, &from, &to, &step, &slicelength) == -1) { +#endif + return NULL; + } + + if (slicelength <= 0) { + return PyList_New(0); + } else { + result = PyList_New(slicelength); + if (!result) return NULL; + + for (cur = from, i = 0; i < slicelength; cur += step, i++) { + PyList_SET_ITEM(result, i, GetItem(self, cur, length)); + } + + return result; + } + } else { + PyErr_Format(PyExc_TypeError, "indices must be integers, not %.200s", + item->ob_type->tp_name); return NULL; } - // Just forward the call to the subscript-handling function of the - // list containing the child messages. - return PyObject_GetItem(self->child_messages, slice); } static PyObject* SubscriptMethod(PyObject* self, PyObject* slice) { @@ -263,49 +283,13 @@ static PyObject* SubscriptMethod(PyObject* self, PyObject* slice) { int AssignSubscript(RepeatedCompositeContainer* self, PyObject* slice, PyObject* value) { - if (UpdateChildMessages(self) < 0) { - return -1; - } if (value != NULL) { PyErr_SetString(PyExc_TypeError, "does not support assignment"); return -1; } - // Delete from the underlying Message, if any. - if (self->parent != NULL) { - if (cmessage::InternalDeleteRepeatedField(self->parent, - self->parent_field_descriptor, - slice, - self->child_messages) < 0) { - return -1; - } - } else { - Py_ssize_t from; - Py_ssize_t to; - Py_ssize_t step; - Py_ssize_t length = Length(reinterpret_cast(self)); - Py_ssize_t slicelength; - if (PySlice_Check(slice)) { -#if PY_MAJOR_VERSION >= 3 - if (PySlice_GetIndicesEx(slice, - length, &from, &to, &step, &slicelength) == -1) { -#else - if (PySlice_GetIndicesEx(reinterpret_cast(slice), - length, &from, &to, &step, &slicelength) == -1) { -#endif - return -1; - } - return PySequence_DelSlice(self->child_messages, from, to); - } else if (PyInt_Check(slice) || PyLong_Check(slice)) { - from = to = PyLong_AsLong(slice); - if (from < 0) { - from = to = length + from; - } - return PySequence_DelItem(self->child_messages, from); - } - } - - return 0; + return cmessage::DeleteRepeatedField(self->parent, + self->parent_field_descriptor, slice); } static int AssignSubscriptMethod(PyObject* self, PyObject* slice, @@ -317,28 +301,33 @@ static int AssignSubscriptMethod(PyObject* self, PyObject* slice, static PyObject* Remove(PyObject* pself, PyObject* value) { RepeatedCompositeContainer* self = reinterpret_cast(pself); + Py_ssize_t len = Length(reinterpret_cast(self)); - if (UpdateChildMessages(self) < 0) { - return NULL; - } - Py_ssize_t index = PySequence_Index(self->child_messages, value); - if (index == -1) { - return NULL; - } - ScopedPyObjectPtr py_index(PyLong_FromLong(index)); - if (AssignSubscript(self, py_index.get(), NULL) < 0) { - return NULL; + for (Py_ssize_t i = 0; i < len; i++) { + ScopedPyObjectPtr item(GetItem(self, i, len)); + if (item == NULL) { + return NULL; + } + int result = PyObject_RichCompareBool(item.get(), value, Py_EQ); + if (result < 0) { + return NULL; + } + if (result) { + ScopedPyObjectPtr py_index(PyLong_FromSsize_t(i)); + if (AssignSubscript(self, py_index.get(), NULL) < 0) { + return NULL; + } + Py_RETURN_NONE; + } } - Py_RETURN_NONE; + PyErr_SetString(PyExc_ValueError, "Item to delete not in list"); + return NULL; } static PyObject* RichCompare(PyObject* pself, PyObject* other, int opid) { RepeatedCompositeContainer* self = reinterpret_cast(pself); - if (UpdateChildMessages(self) < 0) { - return NULL; - } if (!PyObject_TypeCheck(other, &RepeatedCompositeContainer_Type)) { PyErr_SetString(PyExc_TypeError, "Can only compare repeated composite fields " @@ -384,8 +373,9 @@ static PyObject* ToStr(PyObject* pself) { // --------------------------------------------------------------------- // sort() -static void ReorderAttached(RepeatedCompositeContainer* self) { - Message* message = self->message; +static void ReorderAttached(RepeatedCompositeContainer* self, + PyObject* child_list) { + Message* message = self->parent->message; const Reflection* reflection = message->GetReflection(); const FieldDescriptor* descriptor = self->parent_field_descriptor; const Py_ssize_t length = Length(reinterpret_cast(self)); @@ -398,7 +388,7 @@ static void ReorderAttached(RepeatedCompositeContainer* self) { for (Py_ssize_t i = 0; i < length; ++i) { CMessage* py_cmsg = reinterpret_cast( - PyList_GET_ITEM(self->child_messages, i)); + PyList_GET_ITEM(child_list, i)); reflection->AddAllocatedMessage(message, descriptor, py_cmsg->message); } } @@ -408,14 +398,17 @@ static void ReorderAttached(RepeatedCompositeContainer* self) { static int SortPythonMessages(RepeatedCompositeContainer* self, PyObject* args, PyObject* kwds) { - ScopedPyObjectPtr m(PyObject_GetAttrString(self->child_messages, "sort")); + ScopedPyObjectPtr child_list( + PySequence_List(reinterpret_cast(self))); + if (child_list == NULL) { + return -1; + } + ScopedPyObjectPtr m(PyObject_GetAttrString(child_list.get(), "sort")); if (m == NULL) return -1; if (ScopedPyObjectPtr(PyObject_Call(m.get(), args, kwds)) == NULL) return -1; - if (self->message != NULL) { - ReorderAttached(self); - } + ReorderAttached(self, child_list.get()); return 0; } @@ -435,9 +428,6 @@ static PyObject* Sort(PyObject* pself, PyObject* args, PyObject* kwds) { } } - if (UpdateChildMessages(self) < 0) { - return NULL; - } if (SortPythonMessages(self, args, kwds) < 0) { return NULL; } @@ -449,20 +439,7 @@ static PyObject* Sort(PyObject* pself, PyObject* args, PyObject* kwds) { static PyObject* Item(PyObject* pself, Py_ssize_t index) { RepeatedCompositeContainer* self = reinterpret_cast(pself); - - if (UpdateChildMessages(self) < 0) { - return NULL; - } - Py_ssize_t length = Length(pself); - if (index < 0) { - index = length + index; - } - PyObject* item = PyList_GetItem(self->child_messages, index); - if (item == NULL) { - return NULL; - } - Py_INCREF(item); - return item; + return GetItem(self, index); } static PyObject* Pop(PyObject* pself, PyObject* args) { @@ -473,9 +450,10 @@ static PyObject* Pop(PyObject* pself, PyObject* args) { if (!PyArg_ParseTuple(args, "|n", &index)) { return NULL; } - PyObject* item = Item(pself, index); + Py_ssize_t length = Length(pself); + if (index < 0) index += length; + PyObject* item = GetItem(self, index, length); if (item == NULL) { - PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index); return NULL; } ScopedPyObjectPtr py_index(PyLong_FromSsize_t(index)); @@ -485,106 +463,15 @@ static PyObject* Pop(PyObject* pself, PyObject* args) { return item; } -// Release field of parent message and transfer the ownership to target. -void ReleaseLastTo(CMessage* parent, - const FieldDescriptor* field, - CMessage* target) { - GOOGLE_CHECK(parent != nullptr); - GOOGLE_CHECK(field != nullptr); - GOOGLE_CHECK(target != nullptr); - - CMessage::OwnerRef released_message( - parent->message->GetReflection()->ReleaseLast(parent->message, field)); - // TODO(tibell): Deal with proto1. - - target->parent = NULL; - target->parent_field_descriptor = NULL; - target->message = released_message.get(); - target->read_only = false; - cmessage::SetOwner(target, released_message); -} - -// Called to release a container using -// ClearField('container_field_name') on the parent. -int Release(RepeatedCompositeContainer* self) { - if (UpdateChildMessages(self) < 0) { - PyErr_WriteUnraisable(PyBytes_FromString("Failed to update released " - "messages")); - return -1; - } - - Message* message = self->message; - const FieldDescriptor* field = self->parent_field_descriptor; - - // The reflection API only lets us release the last message in a - // repeated field. Therefore we iterate through the children - // starting with the last one. - const Py_ssize_t size = PyList_GET_SIZE(self->child_messages); - GOOGLE_DCHECK_EQ(size, message->GetReflection()->FieldSize(*message, field)); - for (Py_ssize_t i = size - 1; i >= 0; --i) { - CMessage* child_cmessage = reinterpret_cast( - PyList_GET_ITEM(self->child_messages, i)); - ReleaseLastTo(self->parent, field, child_cmessage); - } - - // Detach from containing message. - self->parent = NULL; - self->parent_field_descriptor = NULL; - self->message = NULL; - self->owner.reset(); - - return 0; -} - PyObject* DeepCopy(PyObject* pself, PyObject* arg) { - RepeatedCompositeContainer* self = - reinterpret_cast(pself); - - ScopedPyObjectPtr cloneObj( - PyType_GenericAlloc(&RepeatedCompositeContainer_Type, 0)); - if (cloneObj == NULL) { - return NULL; - } - RepeatedCompositeContainer* clone = - reinterpret_cast(cloneObj.get()); - - Message* new_message = self->message->New(); - clone->parent = NULL; - clone->parent_field_descriptor = self->parent_field_descriptor; - clone->message = new_message; - clone->owner.reset(new_message); - Py_INCREF(self->child_message_class); - clone->child_message_class = self->child_message_class; - clone->child_messages = PyList_New(0); - - new_message->GetReflection() - ->GetMutableRepeatedFieldRef(new_message, - self->parent_field_descriptor) - .MergeFrom(self->message->GetReflection()->GetRepeatedFieldRef( - *self->message, self->parent_field_descriptor)); - return cloneObj.release(); -} - -int SetOwner(RepeatedCompositeContainer* self, - const CMessage::OwnerRef& new_owner) { - GOOGLE_CHECK_ATTACHED(self); - - self->owner = new_owner; - const Py_ssize_t n = PyList_GET_SIZE(self->child_messages); - for (Py_ssize_t i = 0; i < n; ++i) { - PyObject* msg = PyList_GET_ITEM(self->child_messages, i); - if (cmessage::SetOwner(reinterpret_cast(msg), new_owner) == -1) { - return -1; - } - } - return 0; + return reinterpret_cast(pself)->DeepCopy(); } // The private constructor of RepeatedCompositeContainer objects. -PyObject *NewContainer( +RepeatedCompositeContainer *NewContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor, - CMessageClass* concrete_class) { + CMessageClass* child_message_class) { if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) { return NULL; } @@ -596,27 +483,19 @@ PyObject *NewContainer( return NULL; } - self->message = parent->message; + Py_INCREF(parent); self->parent = parent; self->parent_field_descriptor = parent_field_descriptor; - self->owner = parent->owner; - Py_INCREF(concrete_class); - self->child_message_class = concrete_class; - self->child_messages = PyList_New(0); - - return reinterpret_cast(self); + Py_INCREF(child_message_class); + self->child_message_class = child_message_class; + return self; } static void Dealloc(PyObject* pself) { RepeatedCompositeContainer* self = reinterpret_cast(pself); - - Py_CLEAR(self->child_messages); + self->RemoveFromParentCache(); Py_CLEAR(self->child_message_class); - // TODO(tibell): Do we need to call delete on these objects to make - // sure their destructors are called? - self->owner.reset(); - Py_TYPE(self)->tp_free(pself); } @@ -638,6 +517,10 @@ static PyMethodDef Methods[] = { "Makes a deep copy of the class." }, { "add", (PyCFunction)AddMethod, METH_VARARGS | METH_KEYWORDS, "Adds an object to the repeated container." }, + { "append", AppendMethod, METH_O, + "Appends a message to the end of the repeated container."}, + { "insert", Insert, METH_VARARGS, + "Inserts a message before the specified index." }, { "extend", ExtendMethod, METH_O, "Adds objects to the repeated container." }, { "pop", Pop, METH_VARARGS, diff --git a/python/google/protobuf/pyext/repeated_composite_container.h b/python/google/protobuf/pyext/repeated_composite_container.h index 464699aa83..e241827ef5 100644 --- a/python/google/protobuf/pyext/repeated_composite_container.h +++ b/python/google/protobuf/pyext/repeated_composite_container.h @@ -52,46 +52,11 @@ namespace python { struct CMessageClass; -// A RepeatedCompositeContainer can be in one of two states: attached -// or released. -// -// When in the attached state all modifications to the container are -// done both on the 'message' and on the 'child_messages' -// list. In this state all Messages referred to by the children in -// 'child_messages' are owner by the 'owner'. -// -// When in the released state 'message', 'owner', 'parent', and -// 'parent_field_descriptor' are NULL. -typedef struct RepeatedCompositeContainer { - PyObject_HEAD; - - // This is the top-level C++ Message object that owns the whole - // proto tree. Every Python RepeatedCompositeContainer holds a - // reference to it in order to keep it alive as long as there's a - // Python object that references any part of the tree. - CMessage::OwnerRef owner; - - // Weak reference to parent object. May be NULL. Used to make sure - // the parent is writable before modifying the - // RepeatedCompositeContainer. - CMessage* parent; - - // A descriptor used to modify the underlying 'message'. - // The pointer is owned by the global DescriptorPool. - const FieldDescriptor* parent_field_descriptor; - - // Pointer to the C++ Message that contains this container. The - // RepeatedCompositeContainer does not own this pointer. - // - // If NULL, this message has been released from its parent (by - // calling Clear() or ClearField() on the parent. - Message* message; - +// A RepeatedCompositeContainer always has a parent message. +// The parent message also caches reference to items of the container. +typedef struct RepeatedCompositeContainer : public ContainerBase { // The type used to create new child messages. CMessageClass* child_message_class; - - // A list of child messages. - PyObject* child_messages; } RepeatedCompositeContainer; extern PyTypeObject RepeatedCompositeContainer_Type; @@ -100,7 +65,7 @@ namespace repeated_composite_container { // Builds a RepeatedCompositeContainer object, from a parent message and a // field descriptor. -PyObject *NewContainer( +RepeatedCompositeContainer* NewContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor, CMessageClass *child_message_class); @@ -139,25 +104,6 @@ PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice); int AssignSubscript(RepeatedCompositeContainer* self, PyObject* slice, PyObject* value); - -// Releases the messages in the container to a new message. -// -// Returns 0 on success, -1 on failure. -int Release(RepeatedCompositeContainer* self); - -// Returns 0 on success, -1 on failure. -int SetOwner(RepeatedCompositeContainer* self, - const CMessage::OwnerRef& new_owner); - -// Removes the last element of the repeated message field 'field' on -// the Message 'parent', and transfers the ownership of the released -// Message to 'target'. -// -// Corresponds to reflection api method ReleaseMessage. -void ReleaseLastTo(CMessage* parent, - const FieldDescriptor* field, - CMessage* target); - } // namespace repeated_composite_container } // namespace python } // namespace protobuf diff --git a/python/google/protobuf/pyext/repeated_scalar_container.cc b/python/google/protobuf/pyext/repeated_scalar_container.cc index cdb6426932..07ac602832 100644 --- a/python/google/protobuf/pyext/repeated_scalar_container.cc +++ b/python/google/protobuf/pyext/repeated_scalar_container.cc @@ -63,8 +63,8 @@ namespace repeated_scalar_container { static int InternalAssignRepeatedField( RepeatedScalarContainer* self, PyObject* list) { - self->message->GetReflection()->ClearField(self->message, - self->parent_field_descriptor); + Message* message = self->parent->message; + message->GetReflection()->ClearField(message, self->parent_field_descriptor); for (Py_ssize_t i = 0; i < PyList_GET_SIZE(list); ++i) { PyObject* value = PyList_GET_ITEM(list, i); if (ScopedPyObjectPtr(Append(self, value)) == NULL) { @@ -77,7 +77,7 @@ static int InternalAssignRepeatedField( static Py_ssize_t Len(PyObject* pself) { RepeatedScalarContainer* self = reinterpret_cast(pself); - Message* message = self->message; + Message* message = self->parent->message; return message->GetReflection()->FieldSize(*message, self->parent_field_descriptor); } @@ -87,7 +87,7 @@ static int AssignItem(PyObject* pself, Py_ssize_t index, PyObject* arg) { reinterpret_cast(pself); cmessage::AssureWritable(self->parent); - Message* message = self->message; + Message* message = self->parent->message; const FieldDescriptor* field_descriptor = self->parent_field_descriptor; const Reflection* reflection = message->GetReflection(); @@ -104,8 +104,8 @@ static int AssignItem(PyObject* pself, Py_ssize_t index, PyObject* arg) { if (arg == NULL) { ScopedPyObjectPtr py_index(PyLong_FromLong(index)); - return cmessage::InternalDeleteRepeatedField(self->parent, field_descriptor, - py_index.get(), NULL); + return cmessage::DeleteRepeatedField(self->parent, field_descriptor, + py_index.get()); } if (PySequence_Check(arg) && !(PyBytes_Check(arg) || PyUnicode_Check(arg))) { @@ -192,7 +192,7 @@ static PyObject* Item(PyObject* pself, Py_ssize_t index) { RepeatedScalarContainer* self = reinterpret_cast(pself); - Message* message = self->message; + Message* message = self->parent->message; const FieldDescriptor* field_descriptor = self->parent_field_descriptor; const Reflection* reflection = message->GetReflection(); @@ -342,7 +342,7 @@ static PyObject* Subscript(PyObject* pself, PyObject* slice) { PyObject* Append(RepeatedScalarContainer* self, PyObject* item) { cmessage::AssureWritable(self->parent); - Message* message = self->message; + Message* message = self->parent->message; const FieldDescriptor* field_descriptor = self->parent_field_descriptor; const Reflection* reflection = message->GetReflection(); @@ -436,7 +436,7 @@ static int AssSubscript(PyObject* pself, PyObject* slice, PyObject* value) { bool create_list = false; cmessage::AssureWritable(self->parent); - Message* message = self->message; + Message* message = self->parent->message; const FieldDescriptor* field_descriptor = self->parent_field_descriptor; @@ -466,8 +466,7 @@ static int AssSubscript(PyObject* pself, PyObject* slice, PyObject* value) { } if (value == NULL) { - return cmessage::InternalDeleteRepeatedField( - self->parent, field_descriptor, slice, NULL); + return cmessage::DeleteRepeatedField(self->parent, field_descriptor, slice); } if (!create_list) { @@ -668,7 +667,7 @@ static PyObject* MergeFrom(PyObject* pself, PyObject* arg) { } // The private constructor of RepeatedScalarContainer objects. -PyObject *NewContainer( +RepeatedScalarContainer* NewContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor) { if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) { return NULL; @@ -680,72 +679,20 @@ PyObject *NewContainer( return NULL; } - self->message = parent->message; + Py_INCREF(parent); self->parent = parent; self->parent_field_descriptor = parent_field_descriptor; - self->owner = parent->owner; - - return reinterpret_cast(self); -} - -// Initializes the underlying Message object of "to" so it becomes a new parent -// repeated scalar, and copies all the values from "from" to it. A child scalar -// container can be released by passing it as both from and to (e.g. making it -// the recipient of the new parent message and copying the values from itself). -static int InitializeAndCopyToParentContainer( - RepeatedScalarContainer* from, - RepeatedScalarContainer* to) { - ScopedPyObjectPtr full_slice(PySlice_New(NULL, NULL, NULL)); - if (full_slice == NULL) { - return -1; - } - ScopedPyObjectPtr values( - Subscript(reinterpret_cast(from), full_slice.get())); - if (values == NULL) { - return -1; - } - Message* new_message = from->message->New(); - to->parent = NULL; - to->parent_field_descriptor = from->parent_field_descriptor; - to->message = new_message; - to->owner.reset(new_message); - if (InternalAssignRepeatedField(to, values.get()) < 0) { - return -1; - } - return 0; -} -int Release(RepeatedScalarContainer* self) { - return InitializeAndCopyToParentContainer(self, self); + return self; } PyObject* DeepCopy(PyObject* pself, PyObject* arg) { - RepeatedScalarContainer* self = - reinterpret_cast(pself); - - RepeatedScalarContainer* clone = reinterpret_cast( - PyType_GenericAlloc(&RepeatedScalarContainer_Type, 0)); - if (clone == NULL) { - return NULL; - } - - if (InitializeAndCopyToParentContainer(self, clone) < 0) { - Py_DECREF(clone); - return NULL; - } - return reinterpret_cast(clone); + return reinterpret_cast(pself)->DeepCopy(); } static void Dealloc(PyObject* pself) { - RepeatedScalarContainer* self = - reinterpret_cast(pself); - self->owner.reset(); - Py_TYPE(self)->tp_free(pself); -} - -void SetOwner(RepeatedScalarContainer* self, - const CMessage::OwnerRef& new_owner) { - self->owner = new_owner; + reinterpret_cast(pself)->RemoveFromParentCache(); + Py_TYPE(pself)->tp_free(pself); } static PySequenceMethods SqMethods = { diff --git a/python/google/protobuf/pyext/repeated_scalar_container.h b/python/google/protobuf/pyext/repeated_scalar_container.h index 4dcecbac98..f9f0ea8f31 100644 --- a/python/google/protobuf/pyext/repeated_scalar_container.h +++ b/python/google/protobuf/pyext/repeated_scalar_container.h @@ -45,30 +45,7 @@ namespace google { namespace protobuf { namespace python { -typedef struct RepeatedScalarContainer { - PyObject_HEAD; - - // This is the top-level C++ Message object that owns the whole - // proto tree. Every Python RepeatedScalarContainer holds a - // reference to it in order to keep it alive as long as there's a - // Python object that references any part of the tree. - CMessage::OwnerRef owner; - - // Pointer to the C++ Message that contains this container. The - // RepeatedScalarContainer does not own this pointer. - Message* message; - - // Weak reference to a parent CMessage object (i.e. may be NULL.) - // - // Used to make sure all ancestors are also mutable when first - // modifying the container. - CMessage* parent; - - // Pointer to the parent's descriptor that describes this - // field. Used together with the parent's message when making a - // default message instance mutable. - // The pointer is owned by the global DescriptorPool. - const FieldDescriptor* parent_field_descriptor; +typedef struct RepeatedScalarContainer : public ContainerBase { } RepeatedScalarContainer; extern PyTypeObject RepeatedScalarContainer_Type; @@ -77,7 +54,7 @@ namespace repeated_scalar_container { // Builds a RepeatedScalarContainer object, from a parent message and a // field descriptor. -extern PyObject *NewContainer( +extern RepeatedScalarContainer* NewContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor); // Appends the scalar 'item' to the end of the container 'self'. @@ -86,21 +63,12 @@ extern PyObject *NewContainer( // unsuccessful. PyObject* Append(RepeatedScalarContainer* self, PyObject* item); -// Releases the messages in the container to a new message. -// -// Returns 0 on success, -1 on failure. -int Release(RepeatedScalarContainer* self); - // Appends all the elements in the input iterator to the container. // // Returns None if successful; returns NULL and sets an exception if // unsuccessful. PyObject* Extend(RepeatedScalarContainer* self, PyObject* value); -// Set the owner field of self and any children of self. -void SetOwner(RepeatedScalarContainer* self, - const CMessage::OwnerRef& new_owner); - } // namespace repeated_scalar_container } // namespace python } // namespace protobuf diff --git a/python/google/protobuf/pyext/safe_numerics.h b/python/google/protobuf/pyext/safe_numerics.h index 60112cfa4e..93ae640e8b 100644 --- a/python/google/protobuf/pyext/safe_numerics.h +++ b/python/google/protobuf/pyext/safe_numerics.h @@ -132,10 +132,10 @@ template inline bool IsValidNumericCast(Source source) { typedef std::numeric_limits SourceLimits; typedef std::numeric_limits DestLimits; - GOOGLE_COMPILE_ASSERT(SourceLimits::is_specialized, argument_must_be_numeric); - GOOGLE_COMPILE_ASSERT(SourceLimits::is_integer, argument_must_be_integral); - GOOGLE_COMPILE_ASSERT(DestLimits::is_specialized, result_must_be_numeric); - GOOGLE_COMPILE_ASSERT(DestLimits::is_integer, result_must_be_integral); + static_assert(SourceLimits::is_specialized, "argument must be numeric"); + static_assert(SourceLimits::is_integer, "argument must be integral"); + static_assert(DestLimits::is_specialized, "result must be numeric"); + static_assert(DestLimits::is_integer, "result must be integral"); return IsValidNumericCastImpl< sizeof(Dest) == sizeof(Source), @@ -150,7 +150,7 @@ inline bool IsValidNumericCast(Source source) { // checked_numeric_cast<> is analogous to static_cast<> for numeric types, // except that it CHECKs that the specified numeric conversion will not // overflow or underflow. Floating point arguments are not currently allowed -// (this is COMPILE_ASSERTd), though this could be supported if necessary. +// (this is static_asserted), though this could be supported if necessary. template inline Dest checked_numeric_cast(Source source) { GOOGLE_CHECK(IsValidNumericCast(source)); diff --git a/python/google/protobuf/pyext/scoped_pyobject_ptr.h b/python/google/protobuf/pyext/scoped_pyobject_ptr.h index a2afa7f19d..9faaa4f93b 100644 --- a/python/google/protobuf/pyext/scoped_pyobject_ptr.h +++ b/python/google/protobuf/pyext/scoped_pyobject_ptr.h @@ -73,11 +73,6 @@ class ScopedPythonPtr { return p; } - PyObjectStruct* operator->() const { - assert(ptr_ != NULL); - return ptr_; - } - PyObjectStruct* get() const { return ptr_; } PyObject* as_pyobject() const { return reinterpret_cast(ptr_); } diff --git a/python/google/protobuf/pyext/unknown_fields.cc b/python/google/protobuf/pyext/unknown_fields.cc index 760452f2fe..c3679c0d36 100755 --- a/python/google/protobuf/pyext/unknown_fields.cc +++ b/python/google/protobuf/pyext/unknown_fields.cc @@ -137,6 +137,8 @@ static void Dealloc(PyObject* pself) { if (PyObject_TypeCheck(self->parent, &PyUnknownFields_Type)) { reinterpret_cast( self->parent)->sub_unknown_fields.erase(self); + } else { + reinterpret_cast(self->parent)->unknown_field_set = nullptr; } Py_CLEAR(self->parent); self->~PyUnknownFields(); diff --git a/python/google/protobuf/text_format.py b/python/google/protobuf/text_format.py index 5dd41830c2..9f860741e4 100755 --- a/python/google/protobuf/text_format.py +++ b/python/google/protobuf/text_format.py @@ -51,6 +51,7 @@ if six.PY3: long = int # pylint: disable=redefined-builtin,invalid-name # pylint: disable=g-import-not-at-top +from google.protobuf.internal import decoder from google.protobuf.internal import type_checkers from google.protobuf import descriptor from google.protobuf import text_encoding @@ -64,8 +65,6 @@ _INTEGER_CHECKERS = (type_checkers.Uint32ValueChecker(), type_checkers.Int64ValueChecker()) _FLOAT_INFINITY = re.compile('-?inf(?:inity)?f?$', re.IGNORECASE) _FLOAT_NAN = re.compile('nanf?$', re.IGNORECASE) -_FLOAT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_FLOAT, - descriptor.FieldDescriptor.CPPTYPE_DOUBLE]) _QUOTES = frozenset(("'", '"')) _ANY_FULL_TYPE_NAME = 'google.protobuf.Any' @@ -125,17 +124,19 @@ def MessageToString(message, pointy_brackets=False, use_index_order=False, float_format=None, + double_format=None, use_field_number=False, descriptor_pool=None, indent=0, - message_formatter=None): + message_formatter=None, + print_unknown_fields=False): # type: (...) -> str """Convert protobuf message to text format. - Floating point values can be formatted compactly with 15 digits of + Double values can be formatted compactly with 15 digits of precision (which is the most that IEEE 754 "double" can guarantee) - using float_format='.15g'. To ensure that converting to text and back to a - proto will result in an identical value, float_format='.17g' should be used. + using double_format='.15g'. To ensure that converting to text and back to a + proto will result in an identical value, double_format='.17g' should be used. Args: message: The protocol buffers message. @@ -151,14 +152,20 @@ def MessageToString(message, will be printed at the end of the message and their relative order is determined by the extension number. By default, use the field number order. - float_format: If set, use this to specify floating point number formatting - (per the "Format Specification Mini-Language"); otherwise, str() is used. + float_format: If set, use this to specify float field formatting + (per the "Format Specification Mini-Language"); otherwise, 8 valid digits + is used (default '.8g'). Also affect double field if double_format is + not set but float_format is set. + double_format: If set, use this to specify double field formatting + (per the "Format Specification Mini-Language"); if it is not set but + float_format is set, use float_format. Otherwise, use str() use_field_number: If True, print field numbers instead of names. descriptor_pool: A DescriptorPool used to resolve Any types. - indent: The indent level, in terms of spaces, for pretty print. + indent: The initial indent level, in terms of spaces, for pretty print. message_formatter: A function(message, indent, as_one_line): unicode|None to custom format selected sub-messages (usually based on message type). Use to pretty print parts of the protobuf for easier diffing. + print_unknown_fields: If True, unknown fields will be printed. Returns: A string of the text formatted protocol buffer message. @@ -166,8 +173,10 @@ def MessageToString(message, out = TextWriter(as_utf8) printer = _Printer(out, indent, as_utf8, as_one_line, use_short_repeated_primitives, pointy_brackets, - use_index_order, float_format, use_field_number, - descriptor_pool, message_formatter) + use_index_order, float_format, double_format, + use_field_number, + descriptor_pool, message_formatter, + print_unknown_fields=print_unknown_fields) printer.PrintMessage(message) result = out.getvalue() out.close() @@ -201,13 +210,23 @@ def PrintMessage(message, pointy_brackets=False, use_index_order=False, float_format=None, + double_format=None, use_field_number=False, descriptor_pool=None, - message_formatter=None): - printer = _Printer(out, indent, as_utf8, as_one_line, - use_short_repeated_primitives, pointy_brackets, - use_index_order, float_format, use_field_number, - descriptor_pool, message_formatter) + message_formatter=None, + print_unknown_fields=False): + printer = _Printer( + out=out, indent=indent, as_utf8=as_utf8, + as_one_line=as_one_line, + use_short_repeated_primitives=use_short_repeated_primitives, + pointy_brackets=pointy_brackets, + use_index_order=use_index_order, + float_format=float_format, + double_format=double_format, + use_field_number=use_field_number, + descriptor_pool=descriptor_pool, + message_formatter=message_formatter, + print_unknown_fields=print_unknown_fields) printer.PrintMessage(message) @@ -221,11 +240,15 @@ def PrintField(field, pointy_brackets=False, use_index_order=False, float_format=None, - message_formatter=None): + double_format=None, + message_formatter=None, + print_unknown_fields=False): """Print a single field name/value pair.""" printer = _Printer(out, indent, as_utf8, as_one_line, use_short_repeated_primitives, pointy_brackets, - use_index_order, float_format, message_formatter) + use_index_order, float_format, double_format, + message_formatter=message_formatter, + print_unknown_fields=print_unknown_fields) printer.PrintField(field, value) @@ -239,11 +262,15 @@ def PrintFieldValue(field, pointy_brackets=False, use_index_order=False, float_format=None, - message_formatter=None): + double_format=None, + message_formatter=None, + print_unknown_fields=False): """Print a single field value (not including name).""" printer = _Printer(out, indent, as_utf8, as_one_line, use_short_repeated_primitives, pointy_brackets, - use_index_order, float_format, message_formatter) + use_index_order, float_format, double_format, + message_formatter=message_formatter, + print_unknown_fields=print_unknown_fields) printer.PrintFieldValue(field, value) @@ -272,6 +299,11 @@ def _BuildMessageFromTypeName(type_name, descriptor_pool): return message_type() +# These values must match WireType enum in google/protobuf/wire_format.h. +WIRETYPE_LENGTH_DELIMITED = 2 +WIRETYPE_START_GROUP = 3 + + class _Printer(object): """Text format printer for protocol message.""" @@ -284,19 +316,21 @@ class _Printer(object): pointy_brackets=False, use_index_order=False, float_format=None, + double_format=None, use_field_number=False, descriptor_pool=None, - message_formatter=None): + message_formatter=None, + print_unknown_fields=False): """Initialize the Printer. - Floating point values can be formatted compactly with 15 digits of - precision (which is the most that IEEE 754 "double" can guarantee) - using float_format='.15g'. To ensure that converting to text and back to a - proto will result in an identical value, float_format='.17g' should be used. + Double values can be formatted compactly with 15 digits of precision + (which is the most that IEEE 754 "double" can guarantee) using + double_format='.15g'. To ensure that converting to text and back to a proto + will result in an identical value, double_format='.17g' should be used. Args: out: To record the text format result. - indent: The indent level for pretty print. + indent: The initial indent level for pretty print. as_utf8: Return unescaped Unicode for non-ASCII characters. In Python 3 actual Unicode characters may appear as is in strings. In Python 2 the return value will be valid UTF-8 rather than ASCII. @@ -307,14 +341,19 @@ class _Printer(object): use_index_order: If True, print fields of a proto message using the order defined in source code instead of the field number. By default, use the field number order. - float_format: If set, use this to specify floating point number formatting - (per the "Format Specification Mini-Language"); otherwise, str() is - used. + float_format: If set, use this to specify float field formatting + (per the "Format Specification Mini-Language"); otherwise, 8 valid + digits is used (default '.8g'). Also affect double field if + double_format is not set but float_format is set. + double_format: If set, use this to specify double field formatting + (per the "Format Specification Mini-Language"); if it is not set but + float_format is set, use float_format. Otherwise, str() is used. use_field_number: If True, print field numbers instead of names. descriptor_pool: A DescriptorPool used to resolve Any types. message_formatter: A function(message, indent, as_one_line): unicode|None to custom format selected sub-messages (usually based on message type). Use to pretty print parts of the protobuf for easier diffing. + print_unknown_fields: If True, unknown fields will be printed. """ self.out = out self.indent = indent @@ -324,17 +363,24 @@ class _Printer(object): self.pointy_brackets = pointy_brackets self.use_index_order = use_index_order self.float_format = float_format + if double_format is not None: + self.double_format = double_format + else: + self.double_format = float_format self.use_field_number = use_field_number self.descriptor_pool = descriptor_pool self.message_formatter = message_formatter + self.print_unknown_fields = print_unknown_fields def _TryPrintAsAnyMessage(self, message): """Serializes if message is a google.protobuf.Any field.""" + if '/' not in message.type_url: + return False packed_message = _BuildMessageFromTypeName(message.TypeName(), self.descriptor_pool) if packed_message: packed_message.MergeFromString(message.value) - self.out.write('%s[%s]' % (self.indent * ' ', message.type_url)) + self.out.write('%s[%s] ' % (self.indent * ' ', message.type_url)) self._PrintMessageFieldValue(packed_message) self.out.write(' ' if self.as_one_line else '\n') return True @@ -388,6 +434,64 @@ class _Printer(object): else: self.PrintField(field, value) + if self.print_unknown_fields: + self._PrintUnknownFields(message.UnknownFields()) + + def _PrintUnknownFields(self, unknown_fields): + """Print unknown fields.""" + out = self.out + for field in unknown_fields: + out.write(' ' * self.indent) + out.write(str(field.field_number)) + if field.wire_type == WIRETYPE_START_GROUP: + if self.as_one_line: + out.write(' { ') + else: + out.write(' {\n') + self.indent += 2 + + self._PrintUnknownFields(field.data) + + if self.as_one_line: + out.write('} ') + else: + out.write('}\n') + self.indent -= 2 + elif field.wire_type == WIRETYPE_LENGTH_DELIMITED: + try: + # If this field is parseable as a Message, it is probably + # an embedded message. + # pylint: disable=protected-access + (embedded_unknown_message, pos) = decoder._DecodeUnknownFieldSet( + memoryview(field.data), 0, len(field.data)) + except Exception: # pylint: disable=broad-except + pos = 0 + + if pos == len(field.data): + if self.as_one_line: + out.write(' { ') + else: + out.write(' {\n') + self.indent += 2 + + self._PrintUnknownFields(embedded_unknown_message) + + if self.as_one_line: + out.write('} ') + else: + out.write('}\n') + self.indent -= 2 + else: + # A string or bytes field. self.as_utf8 may not work. + out.write(': \"') + out.write(text_encoding.CEscape(field.data, False)) + out.write('\" ' if self.as_one_line else '\"\n') + else: + # varint, fixed32, fixed64 + out.write(': ') + out.write(str(field.data)) + out.write(' ' if self.as_one_line else '\n') + def _PrintFieldName(self, field): """Print field name.""" out = self.out @@ -413,19 +517,20 @@ class _Printer(object): if field.cpp_type != descriptor.FieldDescriptor.CPPTYPE_MESSAGE: # The colon is optional in this case, but our cross-language golden files # don't include it. - out.write(': ') + out.write(':') def PrintField(self, field, value): """Print a single field name/value pair.""" self._PrintFieldName(field) + self.out.write(' ') self.PrintFieldValue(field, value) self.out.write(' ' if self.as_one_line else '\n') def _PrintShortRepeatedPrimitivesValue(self, field, value): # Note: this is called only when value has at least one element. self._PrintFieldName(field) - self.out.write('[') - for i in xrange(len(value) - 1): + self.out.write(' [') + for i in six.moves.range(len(value) - 1): self.PrintFieldValue(field, value[i]) self.out.write(', ') self.PrintFieldValue(field, value[-1]) @@ -441,11 +546,11 @@ class _Printer(object): closeb = '}' if self.as_one_line: - self.out.write(' %s ' % openb) + self.out.write('%s ' % openb) self.PrintMessage(value) self.out.write(closeb) else: - self.out.write(' %s\n' % openb) + self.out.write('%s\n' % openb) self.indent += 2 self.PrintMessage(value) self.indent -= 2 @@ -487,8 +592,14 @@ class _Printer(object): out.write('true') else: out.write('false') - elif field.cpp_type in _FLOAT_TYPES and self.float_format is not None: - out.write('{1:{0}}'.format(self.float_format, value)) + elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_FLOAT: + if self.float_format is not None: + out.write('{1:{0}}'.format(self.float_format, value)) + else: + out.write(str(float(format(value, '.8g')))) + elif (field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_DOUBLE and + self.double_format is not None): + out.write('{1:{0}}'.format(self.double_format, value)) else: out.write(str(value)) @@ -497,7 +608,8 @@ def Parse(text, message, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): """Parses a text representation of a protocol message into a message. NOTE: for historical reasons this function does not clear the input @@ -523,6 +635,9 @@ def Parse(text, parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. + allow_unknown_field: if True, skip over unknown field and keep + parsing. Avoid to use this option if possible. It may hide some + errors (e.g. spelling error on field name) Returns: The same message passed as argument. @@ -534,14 +649,16 @@ def Parse(text, message, allow_unknown_extension, allow_field_number, - descriptor_pool=descriptor_pool) + descriptor_pool=descriptor_pool, + allow_unknown_field=allow_unknown_field) def Merge(text, message, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): """Parses a text representation of a protocol message into a message. Like Parse(), but allows repeated values for a non-repeated field, and uses @@ -554,6 +671,9 @@ def Merge(text, parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. + allow_unknown_field: if True, skip over unknown field and keep + parsing. Avoid to use this option if possible. It may hide some + errors (e.g. spelling error on field name) Returns: The same message passed as argument. @@ -566,14 +686,16 @@ def Merge(text, message, allow_unknown_extension, allow_field_number, - descriptor_pool=descriptor_pool) + descriptor_pool=descriptor_pool, + allow_unknown_field=allow_unknown_field) def ParseLines(lines, message, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): """Parses a text representation of a protocol message into a message. Args: @@ -583,6 +705,9 @@ def ParseLines(lines, parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. + allow_unknown_field: if True, skip over unknown field and keep + parsing. Avoid to use this option if possible. It may hide some + errors (e.g. spelling error on field name) Returns: The same message passed as argument. @@ -592,7 +717,8 @@ def ParseLines(lines, """ parser = _Parser(allow_unknown_extension, allow_field_number, - descriptor_pool=descriptor_pool) + descriptor_pool=descriptor_pool, + allow_unknown_field=allow_unknown_field) return parser.ParseLines(lines, message) @@ -600,7 +726,8 @@ def MergeLines(lines, message, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): """Parses a text representation of a protocol message into a message. Like ParseLines(), but allows repeated values for a non-repeated field, and @@ -613,6 +740,9 @@ def MergeLines(lines, parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. + allow_unknown_field: if True, skip over unknown field and keep + parsing. Avoid to use this option if possible. It may hide some + errors (e.g. spelling error on field name) Returns: The same message passed as argument. @@ -622,7 +752,8 @@ def MergeLines(lines, """ parser = _Parser(allow_unknown_extension, allow_field_number, - descriptor_pool=descriptor_pool) + descriptor_pool=descriptor_pool, + allow_unknown_field=allow_unknown_field) return parser.MergeLines(lines, message) @@ -632,10 +763,12 @@ class _Parser(object): def __init__(self, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): self.allow_unknown_extension = allow_unknown_extension self.allow_field_number = allow_field_number self.descriptor_pool = descriptor_pool + self.allow_unknown_field = allow_unknown_field def ParseLines(self, lines, message): """Parses a text representation of a protocol message into a message.""" @@ -757,7 +890,7 @@ class _Parser(object): field.message_type.name != name): field = None - if not field: + if not field and not self.allow_unknown_field: raise tokenizer.ParseErrorPreviousToken( 'Message type "%s" has no field named "%s".' % (message_descriptor.full_name, name)) @@ -796,7 +929,7 @@ class _Parser(object): merger(tokenizer, message, field) else: # Proto field is unknown. - assert self.allow_unknown_extension + assert (self.allow_unknown_extension or self.allow_unknown_field) _SkipFieldContents(tokenizer) # For historical reasons, fields may optionally be separated by commas or @@ -932,11 +1065,9 @@ class _Parser(object): else: getattr(message, field.name).append(value) else: - # Proto3 doesn't represent presence so we can't test if multiple scalars - # have occurred. We have to allow them. - can_check_presence = not self._IsProto3Syntax(message) if field.is_extension: - if (not self._allow_multiple_scalars and can_check_presence and + if (not self._allow_multiple_scalars and + not self._IsProto3Syntax(message) and message.HasExtension(field)): raise tokenizer.ParseErrorPreviousToken( 'Message type "%s" should not have multiple "%s" extensions.' % @@ -944,8 +1075,16 @@ class _Parser(object): else: message.Extensions[field] = value else: - if (not self._allow_multiple_scalars and can_check_presence and - message.HasField(field.name)): + duplicate_error = False + if not self._allow_multiple_scalars: + if self._IsProto3Syntax(message): + # Proto3 doesn't represent presence so we try best effort to check + # multiple scalars by compare to default values. + duplicate_error = bool(getattr(message, field.name)) + else: + duplicate_error = message.HasField(field.name) + + if duplicate_error: raise tokenizer.ParseErrorPreviousToken( 'Message type "%s" should not have multiple "%s" fields.' % (message.DESCRIPTOR.full_name, field.name)) diff --git a/python/release.sh b/python/release.sh index a71cc7f52b..8d35640e92 100755 --- a/python/release.sh +++ b/python/release.sh @@ -84,13 +84,13 @@ python setup.py sdist upload -r https://test.pypi.org/legacy/ # Test locally with different python versions. run_install_test ${TESTING_VERSION} python2.7 https://test.pypi.org/simple -run_install_test ${TESTING_VERSION} python3.4 https://test.pypi.org/simple +run_install_test ${TESTING_VERSION} python3 https://test.pypi.org/simple # Deploy egg/wheel packages to testing PyPI and test again. python setup.py bdist_egg bdist_wheel upload -r https://test.pypi.org/legacy/ run_install_test ${TESTING_VERSION} python2.7 https://test.pypi.org/simple -run_install_test ${TESTING_VERSION} python3.4 https://test.pypi.org/simple +run_install_test ${TESTING_VERSION} python3 https://test.pypi.org/simple echo "All install tests have passed using testing PyPI." @@ -108,7 +108,7 @@ if [ $TESTING_ONLY -eq 0 ]; then # include files you may not want in the package. E.g., if you have built # and tested with --cpp_implemenation, bdist_xxx will include the _message.so # file even when you no longer pass the --cpp_implemenation flag. See: - # https://github.com/google/protobuf/issues/3042 + # https://github.com/protocolbuffers/protobuf/issues/3042 python setup.py clean build bdist_egg bdist_wheel upload else # Set the version number back (i.e., remove dev suffix). diff --git a/python/release/wheel/protobuf_optimized_pip.sh b/python/release/wheel/protobuf_optimized_pip.sh index 98306f4c2f..07c2a093bb 100755 --- a/python/release/wheel/protobuf_optimized_pip.sh +++ b/python/release/wheel/protobuf_optimized_pip.sh @@ -39,7 +39,7 @@ PYTHON_VERSIONS=('cp27-cp27mu' 'cp33-cp33m' 'cp34-cp34m' 'cp35-cp35m' 'cp36-cp36 mkdir -p ${DIR} cd ${DIR} -curl -SsL -O https://github.com/google/protobuf/archive/v${PROTOBUF_VERSION}.tar.gz +curl -SsL -O https://github.com/protocolbuffers/protobuf/archive/v${PROTOBUF_VERSION}.tar.gz tar xzf v${PROTOBUF_VERSION}.tar.gz cd $DIR/protobuf-${PROTOBUF_VERSION} diff --git a/python/setup.py b/python/setup.py index 63c2d0e051..9aabbf7aaa 100755 --- a/python/setup.py +++ b/python/setup.py @@ -1,24 +1,22 @@ #! /usr/bin/env python # # See README for usage instructions. +from distutils import util import glob import os +import pkg_resources +import re import subprocess import sys +import sysconfig import platform # We must use setuptools, not distutils, because we need to use the # namespace_packages option for the "google" package. from setuptools import setup, Extension, find_packages +from distutils.command.build_py import build_py as _build_py from distutils.command.clean import clean as _clean - -if sys.version_info[0] == 3: - # Python 3 - from distutils.command.build_py import build_py_2to3 as _build_py -else: - # Python 2 - from distutils.command.build_py import build_py as _build_py from distutils.spawn import find_executable # Find the Protocol Compiler. @@ -94,6 +92,7 @@ def GenerateUnittestProtos(): generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False) generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False) generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False) + generate_proto("../src/google/protobuf/util/json_format.proto", False) generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False) generate_proto("google/protobuf/internal/any_test.proto", False) generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False) @@ -190,6 +189,19 @@ if __name__ == '__main__': if sys.platform == 'darwin': extra_compile_args.append("-Wno-shorten-64-to-32"); + extra_compile_args.append("-Wno-deprecated-register"); + + # https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes + # C++ projects must now migrate to libc++ and are recommended to set a + # deployment target of macOS 10.9 or later, or iOS 7 or later. + if sys.platform == 'darwin': + mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') + if mac_target and (pkg_resources.parse_version(mac_target) < + pkg_resources.parse_version('10.9.0')): + os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9' + os.environ['_PYTHON_HOST_PLATFORM'] = re.sub( + r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1', + util.get_platform()) # https://github.com/Theano/Theano/issues/4926 if sys.platform == 'win32': @@ -239,7 +251,7 @@ if __name__ == '__main__': name='protobuf', version=GetVersion(), description='Protocol Buffers', - download_url='https://github.com/google/protobuf/releases', + download_url='https://github.com/protocolbuffers/protobuf/releases', long_description="Protocol Buffers are Google's data interchange format", url='https://developers.google.com/protocol-buffers/', maintainer='protobuf@googlegroups.com', @@ -252,6 +264,9 @@ if __name__ == '__main__': "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", ], namespace_packages=['google'], packages=find_packages( diff --git a/python/tox.ini b/python/tox.ini index 4eb319d23d..999f8ceeb8 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -4,7 +4,7 @@ envlist = [testenv] usedevelop=true -passenv = +passenv = CC KOKORO_BUILD_ID KOKORO_BUILD_NUMBER setenv = cpp: LD_LIBRARY_PATH={toxinidir}/../src/.libs diff --git a/ruby/.gitignore b/ruby/.gitignore index bd8745dd85..653309818f 100644 --- a/ruby/.gitignore +++ b/ruby/.gitignore @@ -6,3 +6,4 @@ protobuf-jruby.iml target/ pkg/ tmp/ +tests/google/ \ No newline at end of file diff --git a/ruby/README.md b/ruby/README.md index 78e86015d7..42a1ffaad4 100644 --- a/ruby/README.md +++ b/ruby/README.md @@ -9,10 +9,11 @@ install protoc as well to have Ruby code generation functionality. Installation from Gem --------------------- +In Gemfile (Please check a version of Protocol Buffers you needed [RubyGems](https://rubygems.org/gems/google-protobuf)): -When we release a version of Protocol Buffers, we will upload a Gem to -[RubyGems](https://www.rubygems.org/). To use this pre-packaged gem, simply -install it as you would any other gem: + gem 'google-protobuf' + +Or for using this pre-packaged gem, simply install it as you would any other gem: $ gem install [--prerelease] google-protobuf diff --git a/ruby/Rakefile b/ruby/Rakefile index 013bc99a40..ad70e3107c 100644 --- a/ruby/Rakefile +++ b/ruby/Rakefile @@ -70,13 +70,13 @@ else task 'gem:windows' do require 'rake_compiler_dock' - RakeCompilerDock.sh "bundle && IN_DOCKER=true rake cross native gem RUBY_CC_VERSION=2.5.0:2.4.0:2.3.0:2.2.2:2.1.6:2.0.0" + RakeCompilerDock.sh "bundle && IN_DOCKER=true rake cross native gem RUBY_CC_VERSION=2.6.0:2.5.0:2.4.0:2.3.0" end if RUBY_PLATFORM =~ /darwin/ task 'gem:native' do system "rake genproto" - system "rake cross native gem RUBY_CC_VERSION=2.5.0:2.4.0:2.3.0:2.2.2:2.1.6:2.0.0" + system "rake cross native gem RUBY_CC_VERSION=2.6.0:2.5.1:2.4.0:2.3.0" end else task 'gem:native' => [:genproto, 'gem:windows'] @@ -86,20 +86,50 @@ end # Proto for tests. genproto_output << "tests/generated_code.rb" +genproto_output << "tests/generated_code_proto2.rb" genproto_output << "tests/test_import.rb" +genproto_output << "tests/test_import_proto2.rb" genproto_output << "tests/test_ruby_package.rb" +genproto_output << "tests/test_ruby_package_proto2.rb" +genproto_output << "tests/basic_test.rb" +genproto_output << "tests/basic_test_proto2.rb" +genproto_output << "tests/wrappers.rb" file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task| sh "../src/protoc --ruby_out=. tests/generated_code.proto" end +file "tests/generated_code_proto2.rb" => "tests/generated_code_proto2.proto" do |file_task| + sh "../src/protoc --ruby_out=. tests/generated_code_proto2.proto" +end + file "tests/test_import.rb" => "tests/test_import.proto" do |file_task| sh "../src/protoc --ruby_out=. tests/test_import.proto" end +file "tests/test_import_proto2.rb" => "tests/test_import_proto2.proto" do |file_task| + sh "../src/protoc --ruby_out=. tests/test_import_proto2.proto" +end + file "tests/test_ruby_package.rb" => "tests/test_ruby_package.proto" do |file_task| sh "../src/protoc --ruby_out=. tests/test_ruby_package.proto" end +file "tests/test_ruby_package_proto2.rb" => "tests/test_ruby_package_proto2.proto" do |file_task| + sh "../src/protoc --ruby_out=. tests/test_ruby_package_proto2.proto" +end + +file "tests/basic_test.rb" => "tests/basic_test.proto" do |file_task| + sh "../src/protoc -I../src -I. --ruby_out=. tests/basic_test.proto" +end + +file "tests/basic_test_proto2.rb" => "tests/basic_test_proto2.proto" do |file_task| + sh "../src/protoc -I../src -I. --ruby_out=. tests/basic_test_proto2.proto" +end + +file "tests/wrappers.rb" => "../src/google/protobuf/wrappers.proto" do |file_task| + sh "../src/protoc -I../src -I. --ruby_out=tests ../src/google/protobuf/wrappers.proto" +end + task :genproto => genproto_output task :clean do @@ -110,7 +140,7 @@ Gem::PackageTask.new(spec) do |pkg| end Rake::TestTask.new(:test => :build) do |t| - t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb") + t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb") end # gc_test needs to be split out to ensure the generated file hasn't been diff --git a/ruby/compatibility_tests/v3.0.0/tests/basic.rb b/ruby/compatibility_tests/v3.0.0/tests/basic.rb index e2559036d6..b7255a672c 100644 --- a/ruby/compatibility_tests/v3.0.0/tests/basic.rb +++ b/ruby/compatibility_tests/v3.0.0/tests/basic.rb @@ -212,12 +212,12 @@ module BasicTest e = assert_raise ArgumentError do MapMessage.new(:map_string_int32 => "hello") end - assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'." + assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)." e = assert_raise ArgumentError do TestMessage.new(:repeated_uint32 => "hello") end - assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'." + assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)." end def test_type_errors diff --git a/ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb b/ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb index 201fe36b97..4ebb731a83 100644 --- a/ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb +++ b/ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb @@ -19,6 +19,7 @@ class RepeatedFieldTest < Test::Unit::TestCase arr_methods -= [ :indices, :iter_for_each, :iter_for_each_index, :iter_for_each_with_index, :dimensions, :copy_data, :copy_data_simple, :nitems, :iter_for_reverse_each, :indexes, :append, :prepend] + arr_methods -= [:union, :difference, :filter!] arr_methods.each do |method_name| assert m.repeated_string.respond_to?(method_name) == true, "does not respond to #{method_name}" end diff --git a/ruby/ext/google/protobuf_c/defs.c b/ruby/ext/google/protobuf_c/defs.c index 9fe04503e1..49c2ba6fb4 100644 --- a/ruby/ext/google/protobuf_c/defs.c +++ b/ruby/ext/google/protobuf_c/defs.c @@ -122,7 +122,7 @@ void DescriptorPool_register(VALUE module) { module, "DescriptorPool", rb_cObject); rb_define_alloc_func(klass, DescriptorPool_alloc); rb_define_method(klass, "add", DescriptorPool_add, 1); - rb_define_method(klass, "build", DescriptorPool_build, 0); + rb_define_method(klass, "build", DescriptorPool_build, -1); rb_define_method(klass, "lookup", DescriptorPool_lookup, 1); rb_define_singleton_method(klass, "generated_pool", DescriptorPool_generated_pool, 0); @@ -181,7 +181,7 @@ VALUE DescriptorPool_add(VALUE _self, VALUE def) { * Builder#add_enum within the block as appropriate. This is the recommended, * idiomatic way to define new message and enum types. */ -VALUE DescriptorPool_build(VALUE _self) { +VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) { VALUE ctx = rb_class_new_instance(0, NULL, cBuilder); VALUE block = rb_block_proc(); rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); @@ -289,6 +289,7 @@ void Descriptor_register(VALUE module) { VALUE klass = rb_define_class_under( module, "Descriptor", rb_cObject); rb_define_alloc_func(klass, Descriptor_alloc); + rb_define_method(klass, "initialize", Descriptor_initialize, 1); rb_define_method(klass, "each", Descriptor_each, 0); rb_define_method(klass, "lookup", Descriptor_lookup, 1); rb_define_method(klass, "add_field", Descriptor_add_field, 1); @@ -298,11 +299,42 @@ void Descriptor_register(VALUE module) { rb_define_method(klass, "msgclass", Descriptor_msgclass, 0); rb_define_method(klass, "name", Descriptor_name, 0); rb_define_method(klass, "name=", Descriptor_name_set, 1); + rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0); rb_include_module(klass, rb_mEnumerable); rb_gc_register_address(&cDescriptor); cDescriptor = klass; } +/* + * call-seq: + * Descriptor.new(file_descriptor) + * + * Initializes a new descriptor and assigns a file descriptor to it. + */ +VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb) { + DEFINE_SELF(Descriptor, self, _self); + + FileDescriptor* file_descriptor = ruby_to_FileDescriptor(file_descriptor_rb); + + CHECK_UPB( + upb_filedef_addmsg(file_descriptor->filedef, self->msgdef, NULL, &status), + "Failed to associate message to file descriptor."); + add_def_obj(file_descriptor->filedef, file_descriptor_rb); + + return Qnil; +} + +/* + * call-seq: + * Descriptor.file_descriptor + * + * Returns the FileDescriptor object this message belongs to. + */ +VALUE Descriptor_file_descriptor(VALUE _self) { + DEFINE_SELF(Descriptor, self, _self); + return get_def_obj(upb_def_file(self->msgdef)); +} + /* * call-seq: * Descriptor.name => name @@ -470,6 +502,142 @@ VALUE Descriptor_msgclass(VALUE _self) { return self->klass; } +// ----------------------------------------------------------------------------- +// FileDescriptor. +// ----------------------------------------------------------------------------- + +DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor"); + +void FileDescriptor_mark(void* _self) { +} + +void FileDescriptor_free(void* _self) { + FileDescriptor* self = _self; + upb_filedef_unref(self->filedef, &self->filedef); + xfree(self); +} + +/* + * call-seq: + * FileDescriptor.new => file + * + * Returns a new file descriptor. The syntax must be set before it's passed + * to a builder. + */ +VALUE FileDescriptor_alloc(VALUE klass) { + FileDescriptor* self = ALLOC(FileDescriptor); + VALUE ret = TypedData_Wrap_Struct(klass, &_FileDescriptor_type, self); + upb_filedef* filedef = upb_filedef_new(&self->filedef); + self->filedef = filedef; + return ret; +} + +void FileDescriptor_register(VALUE module) { + VALUE klass = rb_define_class_under( + module, "FileDescriptor", rb_cObject); + rb_define_alloc_func(klass, FileDescriptor_alloc); + rb_define_method(klass, "initialize", FileDescriptor_initialize, -1); + rb_define_method(klass, "name", FileDescriptor_name, 0); + rb_define_method(klass, "syntax", FileDescriptor_syntax, 0); + rb_define_method(klass, "syntax=", FileDescriptor_syntax_set, 1); + rb_gc_register_address(&cFileDescriptor); + cFileDescriptor = klass; +} + +/* + * call-seq: + * FileDescriptor.new(name, options = nil) => file + * + * Initializes a new file descriptor with the given file name. + * Also accepts an optional "options" hash, specifying other optional + * metadata about the file. The options hash currently accepts the following + * * "syntax": :proto2 or :proto3 (default: :proto3) + */ +VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self) { + DEFINE_SELF(FileDescriptor, self, _self); + + VALUE name_rb; + VALUE options = Qnil; + rb_scan_args(argc, argv, "11", &name_rb, &options); + + if (name_rb != Qnil) { + Check_Type(name_rb, T_STRING); + const char* name = get_str(name_rb); + CHECK_UPB(upb_filedef_setname(self->filedef, name, &status), + "Error setting file name"); + } + + // Default syntax is proto3. + VALUE syntax = ID2SYM(rb_intern("proto3")); + if (options != Qnil) { + Check_Type(options, T_HASH); + + if (rb_funcall(options, rb_intern("key?"), 1, + ID2SYM(rb_intern("syntax"))) == Qtrue) { + syntax = rb_hash_lookup(options, ID2SYM(rb_intern("syntax"))); + } + } + FileDescriptor_syntax_set(_self, syntax); + + return Qnil; +} + +/* + * call-seq: + * FileDescriptor.name => name + * + * Returns the name of the file. + */ +VALUE FileDescriptor_name(VALUE _self) { + DEFINE_SELF(FileDescriptor, self, _self); + const char* name = upb_filedef_name(self->filedef); + return name == NULL ? Qnil : rb_str_new2(name); +} + +/* + * call-seq: + * FileDescriptor.syntax => syntax + * + * Returns this file descriptors syntax. + * + * Valid syntax versions are: + * :proto2 or :proto3. + */ +VALUE FileDescriptor_syntax(VALUE _self) { + DEFINE_SELF(FileDescriptor, self, _self); + + switch (upb_filedef_syntax(self->filedef)) { + case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3")); + case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2")); + default: return Qnil; + } +} + +/* + * call-seq: + * FileDescriptor.syntax = version + * + * Sets this file descriptor's syntax, can be :proto3 or :proto2. + */ +VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax_rb) { + DEFINE_SELF(FileDescriptor, self, _self); + Check_Type(syntax_rb, T_SYMBOL); + + upb_syntax_t syntax; + if (SYM2ID(syntax_rb) == rb_intern("proto3")) { + syntax = UPB_SYNTAX_PROTO3; + } else if (SYM2ID(syntax_rb) == rb_intern("proto2")) { + syntax = UPB_SYNTAX_PROTO2; + } else { + rb_raise(rb_eArgError, "Expected :proto3 or :proto3, received '%s'", + rb_id2name(SYM2ID(syntax_rb))); + } + + CHECK_UPB(upb_filedef_setsyntax(self->filedef, syntax, &status), + "Error setting file syntax for proto"); + return Qnil; +} + // ----------------------------------------------------------------------------- // FieldDescriptor. // ----------------------------------------------------------------------------- @@ -509,6 +677,8 @@ void FieldDescriptor_register(VALUE module) { rb_define_method(klass, "name=", FieldDescriptor_name_set, 1); rb_define_method(klass, "type", FieldDescriptor_type, 0); rb_define_method(klass, "type=", FieldDescriptor_type_set, 1); + rb_define_method(klass, "default", FieldDescriptor_default, 0); + rb_define_method(klass, "default=", FieldDescriptor_default_set, 1); rb_define_method(klass, "label", FieldDescriptor_label, 0); rb_define_method(klass, "label=", FieldDescriptor_label_set, 1); rb_define_method(klass, "number", FieldDescriptor_number, 0); @@ -516,6 +686,8 @@ void FieldDescriptor_register(VALUE module) { rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0); rb_define_method(klass, "submsg_name=", FieldDescriptor_submsg_name_set, 1); rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0); + rb_define_method(klass, "has?", FieldDescriptor_has, 1); + rb_define_method(klass, "clear", FieldDescriptor_clear, 1); rb_define_method(klass, "get", FieldDescriptor_get, 1); rb_define_method(klass, "set", FieldDescriptor_set, 2); rb_gc_register_address(&cFieldDescriptor); @@ -691,6 +863,71 @@ VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) { return Qnil; } +/* + * call-seq: + * FieldDescriptor.default => default + * + * Returns this field's default, as a Ruby object, or nil if not yet set. + */ +VALUE FieldDescriptor_default(VALUE _self) { + DEFINE_SELF(FieldDescriptor, self, _self); + return layout_get_default(self->fielddef); +} + +/* + * call-seq: + * FieldDescriptor.default = default + * + * Sets this field's default value. Raises an exception when calling with + * proto syntax 3. + */ +VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value) { + DEFINE_SELF(FieldDescriptor, self, _self); + upb_fielddef* mut_def = check_field_notfrozen(self->fielddef); + + switch (upb_fielddef_type(mut_def)) { + case UPB_TYPE_FLOAT: + upb_fielddef_setdefaultfloat(mut_def, NUM2DBL(default_value)); + break; + case UPB_TYPE_DOUBLE: + upb_fielddef_setdefaultdouble(mut_def, NUM2DBL(default_value)); + break; + case UPB_TYPE_BOOL: + if (!RB_TYPE_P(default_value, T_TRUE) && + !RB_TYPE_P(default_value, T_FALSE) && + !RB_TYPE_P(default_value, T_NIL)) { + rb_raise(cTypeError, "Expected boolean for default value."); + } + + upb_fielddef_setdefaultbool(mut_def, RTEST(default_value)); + break; + case UPB_TYPE_ENUM: + case UPB_TYPE_INT32: + upb_fielddef_setdefaultint32(mut_def, NUM2INT(default_value)); + break; + case UPB_TYPE_INT64: + upb_fielddef_setdefaultint64(mut_def, NUM2INT(default_value)); + break; + case UPB_TYPE_UINT32: + upb_fielddef_setdefaultuint32(mut_def, NUM2UINT(default_value)); + break; + case UPB_TYPE_UINT64: + upb_fielddef_setdefaultuint64(mut_def, NUM2UINT(default_value)); + break; + case UPB_TYPE_STRING: + case UPB_TYPE_BYTES: + CHECK_UPB(upb_fielddef_setdefaultcstr(mut_def, StringValuePtr(default_value), + &status), + "Error setting default string"); + break; + default: + rb_raise(rb_eArgError, "Defaults not supported on field %s.%s", + upb_fielddef_fullname(mut_def), upb_fielddef_name(mut_def)); + } + + return Qnil; +} + /* * call-seq: * FieldDescriptor.label => label @@ -859,6 +1096,44 @@ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) { return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef); } +/* + * call-seq: + * FieldDescriptor.has?(message) => boolean + * + * Returns whether the value is set on the given message. Raises an + * exception when calling with proto syntax 3. + */ +VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) { + DEFINE_SELF(FieldDescriptor, self, _self); + MessageHeader* msg; + TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); + if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) { + rb_raise(cTypeError, "has method called on wrong message type"); + } else if (!upb_fielddef_haspresence(self->fielddef)) { + rb_raise(rb_eArgError, "does not track presence"); + } + + return layout_has(msg->descriptor->layout, Message_data(msg), self->fielddef); +} + +/* + * call-seq: + * FieldDescriptor.clear(message) + * + * Clears the field from the message if it's set. + */ +VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) { + DEFINE_SELF(FieldDescriptor, self, _self); + MessageHeader* msg; + TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); + if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) { + rb_raise(cTypeError, "has method called on wrong message type"); + } + + layout_clear(msg->descriptor->layout, Message_data(msg), self->fielddef); + return Qnil; +} + /* * call-seq: * FieldDescriptor.set(message, value) @@ -1029,6 +1304,7 @@ void EnumDescriptor_register(VALUE module) { VALUE klass = rb_define_class_under( module, "EnumDescriptor", rb_cObject); rb_define_alloc_func(klass, EnumDescriptor_alloc); + rb_define_method(klass, "initialize", EnumDescriptor_initialize, 1); rb_define_method(klass, "name", EnumDescriptor_name, 0); rb_define_method(klass, "name=", EnumDescriptor_name_set, 1); rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2); @@ -1036,11 +1312,41 @@ void EnumDescriptor_register(VALUE module) { rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1); rb_define_method(klass, "each", EnumDescriptor_each, 0); rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0); + rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0); rb_include_module(klass, rb_mEnumerable); rb_gc_register_address(&cEnumDescriptor); cEnumDescriptor = klass; } +/* + * call-seq: + * Descriptor.new(file_descriptor) + * + * Initializes a new descriptor and assigns a file descriptor to it. + */ +VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb) { + DEFINE_SELF(EnumDescriptor, self, _self); + FileDescriptor* file_descriptor = ruby_to_FileDescriptor(file_descriptor_rb); + CHECK_UPB( + upb_filedef_addenum(file_descriptor->filedef, self->enumdef, + NULL, &status), + "Failed to associate enum to file descriptor."); + add_def_obj(file_descriptor->filedef, file_descriptor_rb); + + return Qnil; +} + +/* + * call-seq: + * Descriptor.file_descriptor + * + * Returns the FileDescriptor object this enum belongs to. + */ +VALUE EnumDescriptor_file_descriptor(VALUE _self) { + DEFINE_SELF(EnumDescriptor, self, _self); + return get_def_obj(upb_def_file(self->enumdef)); +} + /* * call-seq: * EnumDescriptor.name => name @@ -1223,34 +1529,56 @@ VALUE MessageBuilderContext_initialize(VALUE _self, return Qnil; } -static VALUE msgdef_add_field(VALUE msgdef, +static VALUE msgdef_add_field(VALUE msgdef_rb, const char* label, VALUE name, VALUE type, VALUE number, - VALUE type_class) { - VALUE fielddef = rb_class_new_instance(0, NULL, cFieldDescriptor); + VALUE type_class, + VALUE options) { + VALUE fielddef_rb = rb_class_new_instance(0, NULL, cFieldDescriptor); VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name))); - rb_funcall(fielddef, rb_intern("label="), 1, ID2SYM(rb_intern(label))); - rb_funcall(fielddef, rb_intern("name="), 1, name_str); - rb_funcall(fielddef, rb_intern("type="), 1, type); - rb_funcall(fielddef, rb_intern("number="), 1, number); + rb_funcall(fielddef_rb, rb_intern("label="), 1, ID2SYM(rb_intern(label))); + rb_funcall(fielddef_rb, rb_intern("name="), 1, name_str); + rb_funcall(fielddef_rb, rb_intern("type="), 1, type); + rb_funcall(fielddef_rb, rb_intern("number="), 1, number); if (type_class != Qnil) { - if (TYPE(type_class) != T_STRING) { - rb_raise(rb_eArgError, "Expected string for type class"); - } + Check_Type(type_class, T_STRING); + // Make it an absolute type name by prepending a dot. type_class = rb_str_append(rb_str_new2("."), type_class); - rb_funcall(fielddef, rb_intern("submsg_name="), 1, type_class); + rb_funcall(fielddef_rb, rb_intern("submsg_name="), 1, type_class); } - rb_funcall(msgdef, rb_intern("add_field"), 1, fielddef); - return fielddef; + if (options != Qnil) { + Check_Type(options, T_HASH); + + if (rb_funcall(options, rb_intern("key?"), 1, + ID2SYM(rb_intern("default"))) == Qtrue) { + Descriptor* msgdef = ruby_to_Descriptor(msgdef_rb); + if (upb_msgdef_syntax((upb_msgdef*)msgdef->msgdef) == UPB_SYNTAX_PROTO3) { + rb_raise(rb_eArgError, "Cannot set :default when using proto3 syntax."); + } + + FieldDescriptor* fielddef = ruby_to_FieldDescriptor(fielddef_rb); + if (!upb_fielddef_haspresence((upb_fielddef*)fielddef->fielddef) || + upb_fielddef_issubmsg((upb_fielddef*)fielddef->fielddef)) { + rb_raise(rb_eArgError, "Cannot set :default on this kind of field."); + } + + rb_funcall(fielddef_rb, rb_intern("default="), 1, + rb_hash_lookup(options, ID2SYM(rb_intern("default")))); + } + } + + rb_funcall(msgdef_rb, rb_intern("add_field"), 1, fielddef_rb); + return fielddef_rb; } /* * call-seq: - * MessageBuilderContext.optional(name, type, number, type_class = nil) + * MessageBuilderContext.optional(name, type, number, type_class = nil, + * options = nil) * * Defines a new optional field on this message type with the given type, tag * number, and type class (for message and enum fields). The type must be a Ruby @@ -1259,23 +1587,26 @@ static VALUE msgdef_add_field(VALUE msgdef, */ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) { DEFINE_SELF(MessageBuilderContext, self, _self); - VALUE name, type, number, type_class; + VALUE name, type, number; + VALUE type_class, options = Qnil; - if (argc < 3) { - rb_raise(rb_eArgError, "Expected at least 3 arguments."); + rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options); + + // Allow passing (name, type, number, options) or + // (name, type, number, type_class, options) + if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) { + options = type_class; + type_class = Qnil; } - name = argv[0]; - type = argv[1]; - number = argv[2]; - type_class = (argc > 3) ? argv[3] : Qnil; return msgdef_add_field(self->descriptor, "optional", - name, type, number, type_class); + name, type, number, type_class, options); } /* * call-seq: - * MessageBuilderContext.required(name, type, number, type_class = nil) + * MessageBuilderContext.required(name, type, number, type_class = nil, + * options = nil) * * Defines a new required field on this message type with the given type, tag * number, and type class (for message and enum fields). The type must be a Ruby @@ -1288,18 +1619,20 @@ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) { */ VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) { DEFINE_SELF(MessageBuilderContext, self, _self); - VALUE name, type, number, type_class; + VALUE name, type, number; + VALUE type_class, options = Qnil; - if (argc < 3) { - rb_raise(rb_eArgError, "Expected at least 3 arguments."); + rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options); + + // Allow passing (name, type, number, options) or + // (name, type, number, type_class, options) + if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) { + options = type_class; + type_class = Qnil; } - name = argv[0]; - type = argv[1]; - number = argv[2]; - type_class = (argc > 3) ? argv[3] : Qnil; return msgdef_add_field(self->descriptor, "required", - name, type, number, type_class); + name, type, number, type_class, options); } /* @@ -1324,7 +1657,7 @@ VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) { type_class = (argc > 3) ? argv[3] : Qnil; return msgdef_add_field(self->descriptor, "repeated", - name, type, number, type_class); + name, type, number, type_class, Qnil); } /* @@ -1365,9 +1698,17 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) { "type."); } + Descriptor* descriptor = ruby_to_Descriptor(self->descriptor); + if (upb_msgdef_syntax(descriptor->msgdef) == UPB_SYNTAX_PROTO2) { + rb_raise(rb_eArgError, + "Cannot add a native map field using proto2 syntax."); + } + // Create a new message descriptor for the map entry message, and create a // repeated submessage field here with that type. - mapentry_desc = rb_class_new_instance(0, NULL, cDescriptor); + VALUE file_descriptor_rb = + rb_funcall(self->descriptor, rb_intern("file_descriptor"), 0); + mapentry_desc = rb_class_new_instance(1, &file_descriptor_rb, cDescriptor); mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0); mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_"); mapentry_desc_name = rb_str_cat2(mapentry_desc_name, @@ -1410,8 +1751,8 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) { { // Add the map-entry message type to the current builder, and use the type // to create the map field itself. - Builder* builder_self = ruby_to_Builder(self->builder); - rb_ary_push(builder_self->pending_list, mapentry_desc); + Builder* builder = ruby_to_Builder(self->builder); + rb_ary_push(builder->pending_list, mapentry_desc); } { @@ -1514,7 +1855,8 @@ VALUE OneofBuilderContext_initialize(VALUE _self, /* * call-seq: - * OneofBuilderContext.optional(name, type, number, type_class = nil) + * OneofBuilderContext.optional(name, type, number, type_class = nil, + * default_value = nil) * * Defines a new optional field in this oneof with the given type, tag number, * and type class (for message and enum fields). The type must be a Ruby symbol @@ -1523,18 +1865,13 @@ VALUE OneofBuilderContext_initialize(VALUE _self, */ VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) { DEFINE_SELF(OneofBuilderContext, self, _self); - VALUE name, type, number, type_class; + VALUE name, type, number; + VALUE type_class, options = Qnil; - if (argc < 3) { - rb_raise(rb_eArgError, "Expected at least 3 arguments."); - } - name = argv[0]; - type = argv[1]; - number = argv[2]; - type_class = (argc > 3) ? argv[3] : Qnil; + rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options); return msgdef_add_field(self->descriptor, "optional", - name, type, number, type_class); + name, type, number, type_class, options); } // ----------------------------------------------------------------------------- @@ -1604,6 +1941,112 @@ VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) { return enumdef_add_value(self->enumdesc, name, number); } + +// ----------------------------------------------------------------------------- +// FileBuilderContext. +// ----------------------------------------------------------------------------- + +DEFINE_CLASS(FileBuilderContext, + "Google::Protobuf::Internal::FileBuilderContext"); + +void FileBuilderContext_mark(void* _self) { + FileBuilderContext* self = _self; + rb_gc_mark(self->pending_list); + rb_gc_mark(self->file_descriptor); + rb_gc_mark(self->builder); +} + +void FileBuilderContext_free(void* _self) { + FileBuilderContext* self = _self; + xfree(self); +} + +VALUE FileBuilderContext_alloc(VALUE klass) { + FileBuilderContext* self = ALLOC(FileBuilderContext); + VALUE ret = TypedData_Wrap_Struct(klass, &_FileBuilderContext_type, self); + self->pending_list = Qnil; + self->file_descriptor = Qnil; + self->builder = Qnil; + return ret; +} + +void FileBuilderContext_register(VALUE module) { + VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject); + rb_define_alloc_func(klass, FileBuilderContext_alloc); + rb_define_method(klass, "initialize", FileBuilderContext_initialize, 2); + rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1); + rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1); + rb_gc_register_address(&cFileBuilderContext); + cFileBuilderContext = klass; +} + +/* + * call-seq: + * FileBuilderContext.new(file_descriptor, builder) => context + * + * Create a new file builder context for the given file descriptor and + * builder context. This class is intended to serve as a DSL context to be used + * with #instance_eval. + */ +VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor, + VALUE builder) { + DEFINE_SELF(FileBuilderContext, self, _self); + self->pending_list = rb_ary_new(); + self->file_descriptor = file_descriptor; + self->builder = builder; + return Qnil; +} + +/* + * call-seq: + * FileBuilderContext.add_message(name, &block) + * + * Creates a new, empty descriptor with the given name, and invokes the block in + * the context of a MessageBuilderContext on that descriptor. The block can then + * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated + * methods to define the message fields. + * + * This is the recommended, idiomatic way to build message definitions. + */ +VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) { + DEFINE_SELF(FileBuilderContext, self, _self); + VALUE msgdef = rb_class_new_instance(1, &self->file_descriptor, cDescriptor); + VALUE args[2] = { msgdef, self->builder }; + VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext); + VALUE block = rb_block_proc(); + rb_funcall(msgdef, rb_intern("name="), 1, name); + rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); + rb_ary_push(self->pending_list, msgdef); + return Qnil; +} + +/* + * call-seq: + * FileBuilderContext.add_enum(name, &block) + * + * Creates a new, empty enum descriptor with the given name, and invokes the + * block in the context of an EnumBuilderContext on that descriptor. The block + * can then call EnumBuilderContext#add_value to define the enum values. + * + * This is the recommended, idiomatic way to build enum definitions. + */ +VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) { + DEFINE_SELF(FileBuilderContext, self, _self); + VALUE enumdef = + rb_class_new_instance(1, &self->file_descriptor, cEnumDescriptor); + VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext); + VALUE block = rb_block_proc(); + rb_funcall(enumdef, rb_intern("name="), 1, name); + rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); + rb_ary_push(self->pending_list, enumdef); + return Qnil; +} + +VALUE FileBuilderContext_pending_descriptors(VALUE _self) { + DEFINE_SELF(FileBuilderContext, self, _self); + return self->pending_list; +} + // ----------------------------------------------------------------------------- // Builder. // ----------------------------------------------------------------------------- @@ -1613,6 +2056,7 @@ DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder"); void Builder_mark(void* _self) { Builder* self = _self; rb_gc_mark(self->pending_list); + rb_gc_mark(self->default_file_descriptor); } void Builder_free(void* _self) { @@ -1635,15 +2079,17 @@ VALUE Builder_alloc(VALUE klass) { klass, &_Builder_type, self); self->pending_list = Qnil; self->defs = NULL; + self->default_file_descriptor = Qnil; return ret; } void Builder_register(VALUE module) { VALUE klass = rb_define_class_under(module, "Builder", rb_cObject); rb_define_alloc_func(klass, Builder_alloc); + rb_define_method(klass, "initialize", Builder_initialize, 0); + rb_define_method(klass, "add_file", Builder_add_file, -1); rb_define_method(klass, "add_message", Builder_add_message, 1); rb_define_method(klass, "add_enum", Builder_add_enum, 1); - rb_define_method(klass, "initialize", Builder_initialize, 0); rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1); rb_gc_register_address(&cBuilder); cBuilder = klass; @@ -1651,13 +2097,40 @@ void Builder_register(VALUE module) { /* * call-seq: - * Builder.new(d) => builder + * Builder.new * - * Create a new message builder. + * Initializes a new builder. */ VALUE Builder_initialize(VALUE _self) { DEFINE_SELF(Builder, self, _self); self->pending_list = rb_ary_new(); + VALUE file_name = Qnil; + self->default_file_descriptor = + rb_class_new_instance(1, &file_name, cFileDescriptor); + return Qnil; +} + +/* + * call-seq: + * Builder.add_file(name, options = nil, &block) + * + * Creates a new, file descriptor with the given name and options and invokes + * the block in the context of a FileBuilderContext on that descriptor. The + * block can then call FileBuilderContext#add_message or + * FileBuilderContext#add_enum to define new messages or enums, respectively. + * + * This is the recommended, idiomatic way to build file descriptors. + */ +VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) { + DEFINE_SELF(Builder, self, _self); + VALUE file_descriptor = rb_class_new_instance(argc, argv, cFileDescriptor); + VALUE args[2] = { file_descriptor, _self }; + VALUE ctx = rb_class_new_instance(2, args, cFileBuilderContext); + VALUE block = rb_block_proc(); + rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); + + rb_ary_concat(self->pending_list, + FileBuilderContext_pending_descriptors(ctx)); return Qnil; } @@ -1665,16 +2138,17 @@ VALUE Builder_initialize(VALUE _self) { * call-seq: * Builder.add_message(name, &block) * - * Creates a new, empty descriptor with the given name, and invokes the block in - * the context of a MessageBuilderContext on that descriptor. The block can then - * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated - * methods to define the message fields. + * Old and deprecated way to create a new descriptor. + * See FileBuilderContext.add_message for the recommended way. * - * This is the recommended, idiomatic way to build message definitions. + * Exists for backwards compatibility to allow building descriptor pool for + * files generated by protoc which don't add messages within "add_file" block. + * Descriptors created this way get assigned to a default empty FileDescriptor. */ VALUE Builder_add_message(VALUE _self, VALUE name) { DEFINE_SELF(Builder, self, _self); - VALUE msgdef = rb_class_new_instance(0, NULL, cDescriptor); + VALUE msgdef = + rb_class_new_instance(1, &self->default_file_descriptor, cDescriptor); VALUE args[2] = { msgdef, _self }; VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext); VALUE block = rb_block_proc(); @@ -1688,15 +2162,18 @@ VALUE Builder_add_message(VALUE _self, VALUE name) { * call-seq: * Builder.add_enum(name, &block) * - * Creates a new, empty enum descriptor with the given name, and invokes the - * block in the context of an EnumBuilderContext on that descriptor. The block - * can then call EnumBuilderContext#add_value to define the enum values. + * Old and deprecated way to create a new enum descriptor. + * See FileBuilderContext.add_enum for the recommended way. * - * This is the recommended, idiomatic way to build enum definitions. + * Exists for backwards compatibility to allow building descriptor pool for + * files generated by protoc which don't add enums within "add_file" block. + * Enum descriptors created this way get assigned to a default empty + * FileDescriptor. */ VALUE Builder_add_enum(VALUE _self, VALUE name) { DEFINE_SELF(Builder, self, _self); - VALUE enumdef = rb_class_new_instance(0, NULL, cEnumDescriptor); + VALUE enumdef = + rb_class_new_instance(1, &self->default_file_descriptor, cEnumDescriptor); VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext); VALUE block = rb_block_proc(); rb_funcall(enumdef, rb_intern("name="), 1, name); @@ -1705,7 +2182,7 @@ VALUE Builder_add_enum(VALUE _self, VALUE name) { return Qnil; } -static void validate_msgdef(const upb_msgdef* msgdef) { +static void proto3_validate_msgdef(const upb_msgdef* msgdef) { // Verify that no required fields exist. proto3 does not support these. upb_msg_field_iter it; for (upb_msg_field_begin(&it, msgdef); @@ -1718,7 +2195,7 @@ static void validate_msgdef(const upb_msgdef* msgdef) { } } -static void validate_enumdef(const upb_enumdef* enumdef) { +static void proto3_validate_enumdef(const upb_enumdef* enumdef) { // Verify that an entry exists with integer value 0. (This is the default // value.) const char* lookup = upb_enumdef_iton(enumdef, 0); @@ -1753,10 +2230,16 @@ VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) { VALUE def_rb = rb_ary_entry(self->pending_list, i); if (CLASS_OF(def_rb) == cDescriptor) { self->defs[i] = (upb_def*)ruby_to_Descriptor(def_rb)->msgdef; - validate_msgdef((const upb_msgdef*)self->defs[i]); + + if (upb_filedef_syntax(upb_def_file(self->defs[i])) == UPB_SYNTAX_PROTO3) { + proto3_validate_msgdef((const upb_msgdef*)self->defs[i]); + } } else if (CLASS_OF(def_rb) == cEnumDescriptor) { self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef; - validate_enumdef((const upb_enumdef*)self->defs[i]); + + if (upb_filedef_syntax(upb_def_file(self->defs[i])) == UPB_SYNTAX_PROTO3) { + proto3_validate_enumdef((const upb_enumdef*)self->defs[i]); + } } } diff --git a/ruby/ext/google/protobuf_c/encode_decode.c b/ruby/ext/google/protobuf_c/encode_decode.c index d584205137..40952c6b1b 100644 --- a/ruby/ext/google/protobuf_c/encode_decode.c +++ b/ruby/ext/google/protobuf_c/encode_decode.c @@ -100,24 +100,34 @@ void stringsink_uninit(stringsink *sink) { #define DEREF(msg, ofs, type) *(type*)(((uint8_t *)msg) + ofs) -// Creates a handlerdata that simply contains the offset for this field. -static const void* newhandlerdata(upb_handlers* h, uint32_t ofs) { - size_t* hd_ofs = ALLOC(size_t); - *hd_ofs = ofs; - upb_handlers_addcleanup(h, hd_ofs, xfree); - return hd_ofs; +typedef struct { + size_t ofs; + int32_t hasbit; +} field_handlerdata_t; + +// Creates a handlerdata that contains the offset and the hasbit for the field +static const void* newhandlerdata(upb_handlers* h, uint32_t ofs, int32_t hasbit) { + field_handlerdata_t *hd = ALLOC(field_handlerdata_t); + hd->ofs = ofs; + hd->hasbit = hasbit; + upb_handlers_addcleanup(h, hd, xfree); + return hd; } typedef struct { size_t ofs; + int32_t hasbit; const upb_msgdef *md; } submsg_handlerdata_t; // Creates a handlerdata that contains offset and submessage type information. -static const void *newsubmsghandlerdata(upb_handlers* h, uint32_t ofs, +static const void *newsubmsghandlerdata(upb_handlers* h, + uint32_t ofs, + int32_t hasbit, const upb_fielddef* f) { submsg_handlerdata_t *hd = ALLOC(submsg_handlerdata_t); hd->ofs = ofs; + hd->hasbit = hasbit; hd->md = upb_fielddef_msgsubdef(f); upb_handlers_addcleanup(h, hd, xfree); return hd; @@ -189,6 +199,13 @@ static void* appendstr_handler(void *closure, return (void*)str; } +static void set_hasbit(void *closure, int32_t hasbit) { + if (hasbit > 0) { + uint8_t* storage = closure; + storage[hasbit/8] |= 1 << (hasbit % 8); + } +} + // Appends a 'bytes' string to a repeated field. static void* appendbytes_handler(void *closure, const void *hd, @@ -205,10 +222,12 @@ static void* str_handler(void *closure, const void *hd, size_t size_hint) { MessageHeader* msg = closure; - const size_t *ofs = hd; + const field_handlerdata_t *fieldhandler = hd; + VALUE str = rb_str_new2(""); rb_enc_associate(str, kRubyStringUtf8Encoding); - DEREF(msg, *ofs, VALUE) = str; + DEREF(msg, fieldhandler->ofs, VALUE) = str; + set_hasbit(closure, fieldhandler->hasbit); return (void*)str; } @@ -217,10 +236,12 @@ static void* bytes_handler(void *closure, const void *hd, size_t size_hint) { MessageHeader* msg = closure; - const size_t *ofs = hd; + const field_handlerdata_t *fieldhandler = hd; + VALUE str = rb_str_new2(""); rb_enc_associate(str, kRubyString8bitEncoding); - DEREF(msg, *ofs, VALUE) = str; + DEREF(msg, fieldhandler->ofs, VALUE) = str; + set_hasbit(closure, fieldhandler->hasbit); return (void*)str; } @@ -233,18 +254,13 @@ static size_t stringdata_handler(void* closure, const void* hd, } static bool stringdata_end_handler(void* closure, const void* hd) { - MessageHeader* msg = closure; - const size_t *ofs = hd; - VALUE rb_str = DEREF(msg, *ofs, VALUE); + VALUE rb_str = closure; rb_obj_freeze(rb_str); return true; } static bool appendstring_end_handler(void* closure, const void* hd) { - VALUE ary = (VALUE)closure; - int size = RepeatedField_size(ary); - VALUE* last = RepeatedField_index_native(ary, size - 1); - VALUE rb_str = *last; + VALUE rb_str = closure; rb_obj_freeze(rb_str); return true; } @@ -280,8 +296,11 @@ static void *submsg_handler(void *closure, const void *hd) { rb_class_new_instance(0, NULL, subklass); } + set_hasbit(closure, submsgdata->hasbit); + submsg_rb = DEREF(msg, submsgdata->ofs, VALUE); TypedData_Get_Struct(submsg_rb, MessageHeader, &Message_type, submsg); + return submsg; } @@ -370,6 +389,9 @@ static bool endmap_handler(void *closure, const void *hd, upb_status* s) { if (mapdata->value_field_type == UPB_TYPE_MESSAGE || mapdata->value_field_type == UPB_TYPE_ENUM) { value_field_typeclass = get_def_obj(mapdata->value_field_subdef); + if (mapdata->value_field_type == UPB_TYPE_ENUM) { + value_field_typeclass = EnumDescriptor_enummodule(value_field_typeclass); + } } value = native_slot_get( @@ -457,9 +479,8 @@ static void *oneofbytes_handler(void *closure, } static bool oneofstring_end_handler(void* closure, const void* hd) { - MessageHeader* msg = closure; - const oneof_handlerdata_t *oneofdata = hd; - rb_obj_freeze(DEREF(msg, oneofdata->ofs, VALUE)); + VALUE rb_str = rb_str_new2(""); + rb_obj_freeze(rb_str); return true; } @@ -500,7 +521,7 @@ static void add_handlers_for_repeated_field(upb_handlers *h, const upb_fielddef *f, size_t offset) { upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset)); + upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset, -1)); upb_handlers_setstartseq(h, f, startseq_handler, &attr); upb_handlerattr_uninit(&attr); @@ -534,7 +555,7 @@ static void add_handlers_for_repeated_field(upb_handlers *h, } case UPB_TYPE_MESSAGE: { upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, 0, f)); + upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, 0, -1, f)); upb_handlers_setstartsubmsg(h, f, appendsubmsg_handler, &attr); upb_handlerattr_uninit(&attr); break; @@ -545,7 +566,15 @@ static void add_handlers_for_repeated_field(upb_handlers *h, // Set up handlers for a singular field. static void add_handlers_for_singular_field(upb_handlers *h, const upb_fielddef *f, - size_t offset) { + size_t offset, + size_t hasbit_off) { + // The offset we pass to UPB points to the start of the Message, + // rather than the start of where our data is stored. + int32_t hasbit = -1; + if (hasbit_off != MESSAGE_FIELD_NO_HASBIT) { + hasbit = hasbit_off + sizeof(MessageHeader) * 8; + } + switch (upb_fielddef_type(f)) { case UPB_TYPE_BOOL: case UPB_TYPE_INT32: @@ -555,13 +584,13 @@ static void add_handlers_for_singular_field(upb_handlers *h, case UPB_TYPE_INT64: case UPB_TYPE_UINT64: case UPB_TYPE_DOUBLE: - upb_msg_setscalarhandler(h, f, offset, -1); + upb_msg_setscalarhandler(h, f, offset, hasbit); break; case UPB_TYPE_STRING: case UPB_TYPE_BYTES: { bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES; upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset)); + upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset, hasbit)); upb_handlers_setstartstr(h, f, is_bytes ? bytes_handler : str_handler, &attr); @@ -572,7 +601,9 @@ static void add_handlers_for_singular_field(upb_handlers *h, } case UPB_TYPE_MESSAGE: { upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, offset, f)); + upb_handlerattr_sethandlerdata(&attr, + newsubmsghandlerdata(h, offset, + hasbit, f)); upb_handlers_setstartsubmsg(h, f, submsg_handler, &attr); upb_handlerattr_uninit(&attr); break; @@ -610,10 +641,12 @@ static void add_handlers_for_mapentry(const upb_msgdef* msgdef, add_handlers_for_singular_field( h, key_field, - offsetof(map_parse_frame_t, key_storage)); + offsetof(map_parse_frame_t, key_storage), + MESSAGE_FIELD_NO_HASBIT); add_handlers_for_singular_field( h, value_field, - offsetof(map_parse_frame_t, value_storage)); + offsetof(map_parse_frame_t, value_storage), + MESSAGE_FIELD_NO_HASBIT); } // Set up handlers for a oneof field. @@ -718,7 +751,8 @@ static void add_handlers_for_message(const void *closure, upb_handlers *h) { } else if (upb_fielddef_isseq(f)) { add_handlers_for_repeated_field(h, f, offset); } else { - add_handlers_for_singular_field(h, f, offset); + add_handlers_for_singular_field( + h, f, offset, desc->layout->fields[upb_fielddef_index(f)].hasbit); } } } @@ -854,19 +888,38 @@ VALUE Message_decode(VALUE klass, VALUE data) { /* * call-seq: - * MessageClass.decode_json(data) => message + * MessageClass.decode_json(data, options = {}) => message * * Decodes the given data (as a string containing bytes in protocol buffers wire * format) under the interpretration given by this message class's definition * and returns a message object with the corresponding field values. + * + * @param options [Hash] options for the decoder + * ignore_unknown_fields: set true to ignore unknown fields (default is to raise an error) */ -VALUE Message_decode_json(VALUE klass, VALUE data) { +VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) { VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned); Descriptor* desc = ruby_to_Descriptor(descriptor); VALUE msgklass = Descriptor_msgclass(descriptor); VALUE msg_rb; + VALUE data = argv[0]; + VALUE ignore_unknown_fields = Qfalse; MessageHeader* msg; + if (argc < 1 || argc > 2) { + rb_raise(rb_eArgError, "Expected 1 or 2 arguments."); + } + + if (argc == 2) { + VALUE hash_args = argv[1]; + if (TYPE(hash_args) != T_HASH) { + rb_raise(rb_eArgError, "Expected hash arguments."); + } + + ignore_unknown_fields = rb_hash_lookup2( + hash_args, ID2SYM(rb_intern("ignore_unknown_fields")), Qfalse); + } + if (TYPE(data) != T_STRING) { rb_raise(rb_eArgError, "Expected string for JSON data."); } @@ -882,10 +935,12 @@ VALUE Message_decode_json(VALUE klass, VALUE data) { stackenv se; upb_sink sink; upb_json_parser* parser; + DescriptorPool* pool = ruby_to_DescriptorPool(generated_pool); stackenv_init(&se, "Error occurred during parsing: %s"); upb_sink_reset(&sink, get_fill_handlers(desc), msg); - parser = upb_json_parser_create(&se.env, method, &sink); + parser = upb_json_parser_create(&se.env, method, pool->symtab, + &sink, ignore_unknown_fields); upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data), upb_json_parser_input(parser)); @@ -901,13 +956,9 @@ VALUE Message_decode_json(VALUE klass, VALUE data) { /* msgvisitor *****************************************************************/ -// TODO: If/when we support proto2 semantics in addition to the current proto3 -// semantics, which means that we have true field presence, we will want to -// modify msgvisitor so that it emits all present fields rather than all -// non-default-value fields. - static void putmsg(VALUE msg, const Descriptor* desc, - upb_sink *sink, int depth, bool emit_defaults); + upb_sink *sink, int depth, bool emit_defaults, + bool is_json, bool open_msg); static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) { upb_selector_t ret; @@ -939,7 +990,7 @@ static void putstr(VALUE str, const upb_fielddef *f, upb_sink *sink) { } static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink *sink, - int depth, bool emit_defaults) { + int depth, bool emit_defaults, bool is_json) { upb_sink subsink; VALUE descriptor; Descriptor* subdesc; @@ -950,18 +1001,19 @@ static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink *sink, subdesc = ruby_to_Descriptor(descriptor); upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink); - putmsg(submsg, subdesc, &subsink, depth + 1, emit_defaults); + putmsg(submsg, subdesc, &subsink, depth + 1, emit_defaults, is_json, true); upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG)); } static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink, - int depth, bool emit_defaults) { + int depth, bool emit_defaults, bool is_json) { upb_sink subsink; upb_fieldtype_t type = upb_fielddef_type(f); upb_selector_t sel = 0; int size; if (ary == Qnil) return; + if (!emit_defaults && NUM2INT(RepeatedField_length(ary)) == 0) return; size = NUM2INT(RepeatedField_length(ary)); if (size == 0 && !emit_defaults) return; @@ -994,7 +1046,8 @@ static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink, putstr(*((VALUE *)memory), f, &subsink); break; case UPB_TYPE_MESSAGE: - putsubmsg(*((VALUE *)memory), f, &subsink, depth, emit_defaults); + putsubmsg(*((VALUE *)memory), f, &subsink, depth, + emit_defaults, is_json); break; #undef T @@ -1009,7 +1062,13 @@ static void put_ruby_value(VALUE value, VALUE type_class, int depth, upb_sink *sink, - bool emit_defaults) { + bool emit_defaults, + bool is_json) { + if (depth > ENCODE_MAX_NESTING) { + rb_raise(rb_eRuntimeError, + "Maximum recursion depth exceeded during encoding."); + } + upb_selector_t sel = 0; if (upb_fielddef_isprimitive(f)) { sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); @@ -1049,12 +1108,12 @@ static void put_ruby_value(VALUE value, putstr(value, f, sink); break; case UPB_TYPE_MESSAGE: - putsubmsg(value, f, sink, depth, emit_defaults); + putsubmsg(value, f, sink, depth, emit_defaults, is_json); } } static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink, - int depth, bool emit_defaults) { + int depth, bool emit_defaults, bool is_json) { Map* self; upb_sink subsink; const upb_fielddef* key_field; @@ -1062,6 +1121,8 @@ static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink, Map_iter it; if (map == Qnil) return; + if (!emit_defaults && Map_length(map) == 0) return; + self = ruby_to_Map(map); upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink); @@ -1080,9 +1141,10 @@ static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink, &entry_sink); upb_sink_startmsg(&entry_sink); - put_ruby_value(key, key_field, Qnil, depth + 1, &entry_sink, emit_defaults); + put_ruby_value(key, key_field, Qnil, depth + 1, &entry_sink, + emit_defaults, is_json); put_ruby_value(value, value_field, self->value_type_class, depth + 1, - &entry_sink, emit_defaults); + &entry_sink, emit_defaults, is_json); upb_sink_endmsg(&entry_sink, &status); upb_sink_endsubmsg(&subsink, getsel(f, UPB_HANDLER_ENDSUBMSG)); @@ -1091,13 +1153,143 @@ static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink, upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); } +static const upb_handlers* msgdef_json_serialize_handlers( + Descriptor* desc, bool preserve_proto_fieldnames); + +static void putjsonany(VALUE msg_rb, const Descriptor* desc, + upb_sink* sink, int depth, bool emit_defaults) { + upb_status status; + MessageHeader* msg = NULL; + const upb_fielddef* type_field = upb_msgdef_itof(desc->msgdef, UPB_ANY_TYPE); + const upb_fielddef* value_field = upb_msgdef_itof(desc->msgdef, UPB_ANY_VALUE); + + size_t type_url_offset; + VALUE type_url_str_rb; + const upb_msgdef *payload_type = NULL; + + TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); + + upb_sink_startmsg(sink); + + /* Handle type url */ + type_url_offset = desc->layout->fields[upb_fielddef_index(type_field)].offset; + type_url_str_rb = DEREF(Message_data(msg), type_url_offset, VALUE); + if (RSTRING_LEN(type_url_str_rb) > 0) { + putstr(type_url_str_rb, type_field, sink); + } + + { + const char* type_url_str = RSTRING_PTR(type_url_str_rb); + size_t type_url_len = RSTRING_LEN(type_url_str_rb); + DescriptorPool* pool = ruby_to_DescriptorPool(generated_pool); + + if (type_url_len <= 20 || + strncmp(type_url_str, "type.googleapis.com/", 20) != 0) { + rb_raise(rb_eRuntimeError, "Invalid type url: %s", type_url_str); + return; + } + + /* Resolve type url */ + type_url_str += 20; + type_url_len -= 20; + + payload_type = upb_symtab_lookupmsg2( + pool->symtab, type_url_str, type_url_len); + if (payload_type == NULL) { + rb_raise(rb_eRuntimeError, "Unknown type: %s", type_url_str); + return; + } + } + + { + uint32_t value_offset; + VALUE value_str_rb; + const char* value_str; + size_t value_len; + + value_offset = desc->layout->fields[upb_fielddef_index(value_field)].offset; + value_str_rb = DEREF(Message_data(msg), value_offset, VALUE); + value_str = RSTRING_PTR(value_str_rb); + value_len = RSTRING_LEN(value_str_rb); + + if (value_len > 0) { + VALUE payload_desc_rb = get_def_obj(payload_type); + Descriptor* payload_desc = ruby_to_Descriptor(payload_desc_rb); + VALUE payload_class = Descriptor_msgclass(payload_desc_rb); + upb_sink subsink; + bool is_wellknown; + + VALUE payload_msg_rb = Message_decode(payload_class, value_str_rb); + + is_wellknown = + upb_msgdef_wellknowntype(payload_desc->msgdef) != + UPB_WELLKNOWN_UNSPECIFIED; + if (is_wellknown) { + upb_sink_startstr(sink, getsel(value_field, UPB_HANDLER_STARTSTR), 0, + &subsink); + } + + subsink.handlers = + msgdef_json_serialize_handlers(payload_desc, true); + subsink.closure = sink->closure; + putmsg(payload_msg_rb, payload_desc, &subsink, depth, emit_defaults, true, + is_wellknown); + } + } + + upb_sink_endmsg(sink, &status); +} + +static void putjsonlistvalue( + VALUE msg_rb, const Descriptor* desc, + upb_sink* sink, int depth, bool emit_defaults) { + upb_status status; + upb_sink subsink; + MessageHeader* msg = NULL; + const upb_fielddef* f = upb_msgdef_itof(desc->msgdef, 1); + uint32_t offset = + desc->layout->fields[upb_fielddef_index(f)].offset + + sizeof(MessageHeader); + VALUE ary; + + TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); + + upb_sink_startmsg(sink); + + ary = DEREF(msg, offset, VALUE); + + if (ary == Qnil || RepeatedField_size(ary) == 0) { + upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink); + upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); + } else { + putary(ary, f, sink, depth, emit_defaults, true); + } + + upb_sink_endmsg(sink, &status); +} + static void putmsg(VALUE msg_rb, const Descriptor* desc, - upb_sink *sink, int depth, bool emit_defaults) { + upb_sink *sink, int depth, bool emit_defaults, + bool is_json, bool open_msg) { MessageHeader* msg; upb_msg_field_iter i; upb_status status; - upb_sink_startmsg(sink); + if (is_json && + upb_msgdef_wellknowntype(desc->msgdef) == UPB_WELLKNOWN_ANY) { + putjsonany(msg_rb, desc, sink, depth, emit_defaults); + return; + } + + if (is_json && + upb_msgdef_wellknowntype(desc->msgdef) == UPB_WELLKNOWN_LISTVALUE) { + putjsonlistvalue(msg_rb, desc, sink, depth, emit_defaults); + return; + } + + if (open_msg) { + upb_sink_startmsg(sink); + } // Protect against cycles (possible because users may freely reassign message // and repeated fields) by imposing a maximum recursion depth. @@ -1142,30 +1334,45 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, if (is_map_field(f)) { VALUE map = DEREF(msg, offset, VALUE); if (map != Qnil || emit_defaults) { - putmap(map, f, sink, depth, emit_defaults); + putmap(map, f, sink, depth, emit_defaults, is_json); } } else if (upb_fielddef_isseq(f)) { VALUE ary = DEREF(msg, offset, VALUE); if (ary != Qnil) { - putary(ary, f, sink, depth, emit_defaults); + putary(ary, f, sink, depth, emit_defaults, is_json); } } else if (upb_fielddef_isstring(f)) { VALUE str = DEREF(msg, offset, VALUE); - if (is_matching_oneof || emit_defaults || RSTRING_LEN(str) > 0) { + bool is_default = false; + + if (upb_msgdef_syntax(desc->msgdef) == UPB_SYNTAX_PROTO2) { + is_default = layout_has(desc->layout, Message_data(msg), f) == Qfalse; + } else if (upb_msgdef_syntax(desc->msgdef) == UPB_SYNTAX_PROTO3) { + is_default = RSTRING_LEN(str) == 0; + } + + if (is_matching_oneof || emit_defaults || !is_default) { putstr(str, f, sink); } } else if (upb_fielddef_issubmsg(f)) { - putsubmsg(DEREF(msg, offset, VALUE), f, sink, depth, emit_defaults); + putsubmsg(DEREF(msg, offset, VALUE), f, sink, depth, + emit_defaults, is_json); } else { upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); -#define T(upbtypeconst, upbtype, ctype, default_value) \ - case upbtypeconst: { \ - ctype value = DEREF(msg, offset, ctype); \ - if (is_matching_oneof || emit_defaults || value != default_value) { \ - upb_sink_put##upbtype(sink, sel, value); \ - } \ - } \ +#define T(upbtypeconst, upbtype, ctype, default_value) \ + case upbtypeconst: { \ + ctype value = DEREF(msg, offset, ctype); \ + bool is_default = false; \ + if (upb_fielddef_haspresence(f)) { \ + is_default = layout_has(desc->layout, Message_data(msg), f) == Qfalse; \ + } else if (upb_msgdef_syntax(desc->msgdef) == UPB_SYNTAX_PROTO3) { \ + is_default = default_value == value; \ + } \ + if (is_matching_oneof || emit_defaults || !is_default) { \ + upb_sink_put##upbtype(sink, sel, value); \ + } \ + } \ break; switch (upb_fielddef_type(f)) { @@ -1193,7 +1400,9 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, upb_sink_putunknown(sink, unknown->ptr, unknown->len); } - upb_sink_endmsg(sink, &status); + if (open_msg) { + upb_sink_endmsg(sink, &status); + } } static const upb_handlers* msgdef_pb_serialize_handlers(Descriptor* desc) { @@ -1248,7 +1457,7 @@ VALUE Message_encode(VALUE klass, VALUE msg_rb) { stackenv_init(&se, "Error occurred during encoding: %s"); encoder = upb_pb_encoder_create(&se.env, serialize_handlers, &sink.sink); - putmsg(msg_rb, desc, upb_pb_encoder_input(encoder), 0, false); + putmsg(msg_rb, desc, upb_pb_encoder_input(encoder), 0, false, false, true); ret = rb_str_new(sink.ptr, sink.len); @@ -1261,9 +1470,12 @@ VALUE Message_encode(VALUE klass, VALUE msg_rb) { /* * call-seq: - * MessageClass.encode_json(msg) => json_string + * MessageClass.encode_json(msg, options = {}) => json_string * * Encodes the given message object into its serialized JSON representation. + * @param options [Hash] options for the decoder + * preserve_proto_fieldnames: set true to use original fieldnames (default is to camelCase) + * emit_defaults: set true to emit 0/false values (default is to omit them) */ VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) { VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned); @@ -1303,7 +1515,8 @@ VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) { stackenv_init(&se, "Error occurred during encoding: %s"); printer = upb_json_printer_create(&se.env, serialize_handlers, &sink.sink); - putmsg(msg_rb, desc, upb_json_printer_input(printer), 0, RTEST(emit_defaults)); + putmsg(msg_rb, desc, upb_json_printer_input(printer), 0, + RTEST(emit_defaults), true, true); ret = rb_enc_str_new(sink.ptr, sink.len, rb_utf8_encoding()); diff --git a/ruby/ext/google/protobuf_c/extconf.rb b/ruby/ext/google/protobuf_c/extconf.rb index cc097e1185..04eaa4bad9 100644 --- a/ruby/ext/google/protobuf_c/extconf.rb +++ b/ruby/ext/google/protobuf_c/extconf.rb @@ -2,7 +2,11 @@ require 'mkmf' -unless RUBY_PLATFORM =~ /mswin|mingw/ +if RUBY_PLATFORM =~ /darwin/ || RUBY_PLATFORM =~ /linux/ + # XOPEN_SOURCE needed for strptime: + # https://stackoverflow.com/questions/35234152/strptime-giving-implicit-declaration-and-undefined-reference + $CFLAGS += " -std=c99 -O3 -DNDEBUG -D_XOPEN_SOURCE=700" +else $CFLAGS += " -std=c99 -O3 -DNDEBUG" end diff --git a/ruby/ext/google/protobuf_c/map.c b/ruby/ext/google/protobuf_c/map.c index 8c2f6424c3..cf1d6e3771 100644 --- a/ruby/ext/google/protobuf_c/map.c +++ b/ruby/ext/google/protobuf_c/map.c @@ -82,7 +82,7 @@ static VALUE table_key(Map* self, VALUE key, case UPB_TYPE_INT64: case UPB_TYPE_UINT32: case UPB_TYPE_UINT64: - native_slot_set(self->key_type, Qnil, buf, key); + native_slot_set("", self->key_type, Qnil, buf, key); *out_key = buf; *out_length = native_slot_size(self->key_type); break; @@ -386,6 +386,8 @@ VALUE Map_index(VALUE _self, VALUE key) { * was just inserted. */ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) { + rb_check_frozen(_self); + Map* self = ruby_to_Map(_self); char keybuf[TABLE_KEY_BUF_LENGTH]; @@ -396,7 +398,7 @@ VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) { key = table_key(self, key, keybuf, &keyval, &length); mem = value_memory(&v); - native_slot_set(self->value_type, self->value_type_class, mem, value); + native_slot_set("", self->value_type, self->value_type_class, mem, value); // Replace any existing value by issuing a 'remove' operation first. upb_strtable_remove2(&self->table, keyval, length, NULL); @@ -438,6 +440,8 @@ VALUE Map_has_key(VALUE _self, VALUE key) { * nil if none was present. Throws an exception if the key is of the wrong type. */ VALUE Map_delete(VALUE _self, VALUE key) { + rb_check_frozen(_self); + Map* self = ruby_to_Map(_self); char keybuf[TABLE_KEY_BUF_LENGTH]; @@ -461,6 +465,8 @@ VALUE Map_delete(VALUE _self, VALUE key) { * Removes all entries from the map. */ VALUE Map_clear(VALUE _self) { + rb_check_frozen(_self); + Map* self = ruby_to_Map(_self); // Uninit and reinit the table -- this is faster than iterating and doing a @@ -841,7 +847,6 @@ void Map_register(VALUE module) { rb_define_method(klass, "dup", Map_dup, 0); rb_define_method(klass, "==", Map_eq, 1); rb_define_method(klass, "hash", Map_hash, 0); - rb_define_method(klass, "to_hash", Map_to_h, 0); rb_define_method(klass, "to_h", Map_to_h, 0); rb_define_method(klass, "inspect", Map_inspect, 0); rb_define_method(klass, "merge", Map_merge, 1); diff --git a/ruby/ext/google/protobuf_c/message.c b/ruby/ext/google/protobuf_c/message.c index 721c111275..2f02c35445 100644 --- a/ruby/ext/google/protobuf_c/message.c +++ b/ruby/ext/google/protobuf_c/message.c @@ -79,7 +79,7 @@ VALUE Message_alloc(VALUE klass) { return ret; } -static VALUE which_oneof_field(MessageHeader* self, const upb_oneofdef* o) { +static const upb_fielddef* which_oneof_field(MessageHeader* self, const upb_oneofdef* o) { upb_oneof_iter it; size_t case_ofs; uint32_t oneof_case; @@ -88,7 +88,7 @@ static VALUE which_oneof_field(MessageHeader* self, const upb_oneofdef* o) { // If no fields in the oneof, always nil. if (upb_oneofdef_numfields(o) == 0) { - return Qnil; + return NULL; } // Grab the first field in the oneof so we can get its layout info to find the // oneof_case field. @@ -103,22 +103,165 @@ static VALUE which_oneof_field(MessageHeader* self, const upb_oneofdef* o) { oneof_case = *((uint32_t*)((char*)Message_data(self) + case_ofs)); if (oneof_case == ONEOF_CASE_NONE) { - return Qnil; + return NULL; } // oneof_case is a field index, so find that field. f = upb_oneofdef_itof(o, oneof_case); assert(f != NULL); - return ID2SYM(rb_intern(upb_fielddef_name(f))); + return f; +} + +enum { + METHOD_UNKNOWN = 0, + METHOD_GETTER = 1, + METHOD_SETTER = 2, + METHOD_CLEAR = 3, + METHOD_PRESENCE = 4, + METHOD_ENUM_GETTER = 5, + METHOD_WRAPPER_GETTER = 6, + METHOD_WRAPPER_SETTER = 7 +}; + +// Check if the field is a well known wrapper type +static bool is_wrapper_type_field(const upb_fielddef* field) { + char* field_type_name = rb_class2name(field_type_class(field)); + + return strcmp(field_type_name, "Google::Protobuf::DoubleValue") == 0 || + strcmp(field_type_name, "Google::Protobuf::FloatValue") == 0 || + strcmp(field_type_name, "Google::Protobuf::Int32Value") == 0 || + strcmp(field_type_name, "Google::Protobuf::Int64Value") == 0 || + strcmp(field_type_name, "Google::Protobuf::UInt32Value") == 0 || + strcmp(field_type_name, "Google::Protobuf::UInt64Value") == 0 || + strcmp(field_type_name, "Google::Protobuf::BoolValue") == 0 || + strcmp(field_type_name, "Google::Protobuf::StringValue") == 0 || + strcmp(field_type_name, "Google::Protobuf::BytesValue") == 0; +} + +// Get a new Ruby wrapper type and set the initial value +static VALUE ruby_wrapper_type(const upb_fielddef* field, const VALUE* value) { + if (is_wrapper_type_field(field) && value != Qnil) { + VALUE hash = rb_hash_new(); + rb_hash_aset(hash, rb_str_new2("value"), value); + VALUE args[1] = { hash }; + return rb_class_new_instance(1, args, field_type_class(field)); + } + return Qnil; +} + +static int extract_method_call(VALUE method_name, MessageHeader* self, + const upb_fielddef **f, const upb_oneofdef **o) { + Check_Type(method_name, T_SYMBOL); + + VALUE method_str = rb_id2str(SYM2ID(method_name)); + char* name = RSTRING_PTR(method_str); + size_t name_len = RSTRING_LEN(method_str); + int accessor_type; + const upb_oneofdef* test_o; + const upb_fielddef* test_f; + + if (name[name_len - 1] == '=') { + accessor_type = METHOD_SETTER; + name_len--; + // We want to ensure if the proto has something named clear_foo or has_foo?, + // we don't strip the prefix. + } else if (strncmp("clear_", name, 6) == 0 && + !upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len, + &test_f, &test_o)) { + accessor_type = METHOD_CLEAR; + name = name + 6; + name_len = name_len - 6; + } else if (strncmp("has_", name, 4) == 0 && name[name_len - 1] == '?' && + !upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len, + &test_f, &test_o)) { + accessor_type = METHOD_PRESENCE; + name = name + 4; + name_len = name_len - 5; + } else { + accessor_type = METHOD_GETTER; + } + + bool has_field = upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len, + &test_f, &test_o); + + // Look for wrapper type accessor of the form _as_value + if (!has_field && + (accessor_type == METHOD_GETTER || accessor_type == METHOD_SETTER) && + name_len > 9 && strncmp(name + name_len - 9, "_as_value", 9) == 0) { + // Find the field name + char wrapper_field_name[name_len - 8]; + strncpy(wrapper_field_name, name, name_len - 9); + wrapper_field_name[name_len - 7] = '\0'; + + // Check if field exists and is a wrapper type + const upb_oneofdef* test_o_wrapper; + const upb_fielddef* test_f_wrapper; + if (upb_msgdef_lookupname(self->descriptor->msgdef, wrapper_field_name, name_len - 9, + &test_f_wrapper, &test_o_wrapper) && + upb_fielddef_type(test_f_wrapper) == UPB_TYPE_MESSAGE && + is_wrapper_type_field(test_f_wrapper)) { + // It does exist! + has_field = true; + if (accessor_type == METHOD_SETTER) { + accessor_type = METHOD_WRAPPER_SETTER; + } else { + accessor_type = METHOD_WRAPPER_GETTER; + } + test_o = test_o_wrapper; + test_f = test_f_wrapper; + } + } + + // Look for enum accessor of the form _const + if (!has_field && accessor_type == METHOD_GETTER && + name_len > 6 && strncmp(name + name_len - 6, "_const", 6) == 0) { + + // Find enum field name + char enum_name[name_len - 5]; + strncpy(enum_name, name, name_len - 6); + enum_name[name_len - 4] = '\0'; + + // Check if enum field exists + const upb_oneofdef* test_o_enum; + const upb_fielddef* test_f_enum; + if (upb_msgdef_lookupname(self->descriptor->msgdef, enum_name, name_len - 6, + &test_f_enum, &test_o_enum) && + upb_fielddef_type(test_f_enum) == UPB_TYPE_ENUM) { + // It does exist! + has_field = true; + accessor_type = METHOD_ENUM_GETTER; + test_o = test_o_enum; + test_f = test_f_enum; + } + } + + // Verify the name corresponds to a oneof or field in this message. + if (!has_field) { + return METHOD_UNKNOWN; + } + + // Method calls like 'has_foo?' are not allowed if field "foo" does not have + // a hasbit (e.g. repeated fields or non-message type fields for proto3 + // syntax). + if (accessor_type == METHOD_PRESENCE && test_f != NULL && + !upb_fielddef_haspresence(test_f)) { + return METHOD_UNKNOWN; + } + + *o = test_o; + *f = test_f; + return accessor_type; } /* * call-seq: * Message.method_missing(*args) * - * Provides accessors and setters for message fields according to their field - * names. For any field whose name does not conflict with a built-in method, an + * Provides accessors and setters and methods to clear and check for presence of + * message fields according to their field names. + * + * For any field whose name does not conflict with a built-in method, an * accessor is provided with the same name as the field, and a setter is * provided with the name of the field plus the '=' suffix. Thus, given a * message instance 'msg' with field 'foo', the following code is valid: @@ -129,13 +272,17 @@ static VALUE which_oneof_field(MessageHeader* self, const upb_oneofdef* o) { * This method also provides read-only accessors for oneofs. If a oneof exists * with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to * the name of the field in that oneof that is currently set, or nil if none. + * + * It also provides methods of the form 'clear_fieldname' to clear the value + * of the field 'fieldname'. For basic data types, this will set the default + * value of the field. + * + * Additionally, it provides methods of the form 'has_fieldname?', which returns + * true if the field 'fieldname' is set in the message object, else false. For + * 'proto3' syntax, calling this for a basic type field will result in an error. */ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) { MessageHeader* self; - VALUE method_name, method_str; - char* name; - size_t name_len; - bool setter; const upb_oneofdef* o; const upb_fielddef* f; @@ -143,54 +290,84 @@ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) { if (argc < 1) { rb_raise(rb_eArgError, "Expected method name as first argument."); } - method_name = argv[0]; - if (!SYMBOL_P(method_name)) { - rb_raise(rb_eArgError, "Expected symbol as method name."); - } - method_str = rb_id2str(SYM2ID(method_name)); - name = RSTRING_PTR(method_str); - name_len = RSTRING_LEN(method_str); - setter = false; - - // Setters have names that end in '='. - if (name[name_len - 1] == '=') { - setter = true; - name_len--; - } - // See if this name corresponds to either a oneof or field in this message. - if (!upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len, &f, - &o)) { + int accessor_type = extract_method_call(argv[0], self, &f, &o); + if (accessor_type == METHOD_UNKNOWN || (o == NULL && f == NULL) ) { return rb_call_super(argc, argv); + } else if (accessor_type == METHOD_SETTER || accessor_type == METHOD_WRAPPER_SETTER) { + if (argc != 2) { + rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc); + } + rb_check_frozen(_self); + } else if (argc != 1) { + rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc); } + // Return which of the oneof fields are set if (o != NULL) { - // This is a oneof -- return which field inside the oneof is set. - if (setter) { + if (accessor_type == METHOD_SETTER) { rb_raise(rb_eRuntimeError, "Oneof accessors are read-only."); } - return which_oneof_field(self, o); - } else { - // This is a field -- get or set the field's value. - assert(f); - if (setter) { - if (argc < 2) { - rb_raise(rb_eArgError, "No value provided to setter."); + + const upb_fielddef* oneof_field = which_oneof_field(self, o); + if (accessor_type == METHOD_PRESENCE) { + return oneof_field == NULL ? Qfalse : Qtrue; + } else if (accessor_type == METHOD_CLEAR) { + if (oneof_field != NULL) { + layout_clear(self->descriptor->layout, Message_data(self), oneof_field); } - layout_set(self->descriptor->layout, Message_data(self), f, argv[1]); return Qnil; } else { - return layout_get(self->descriptor->layout, Message_data(self), f); + // METHOD_ACCESSOR + return oneof_field == NULL ? Qnil : + ID2SYM(rb_intern(upb_fielddef_name(oneof_field))); + } + // Otherwise we're operating on a single proto field + } else if (accessor_type == METHOD_SETTER) { + layout_set(self->descriptor->layout, Message_data(self), f, argv[1]); + return Qnil; + } else if (accessor_type == METHOD_CLEAR) { + layout_clear(self->descriptor->layout, Message_data(self), f); + return Qnil; + } else if (accessor_type == METHOD_PRESENCE) { + return layout_has(self->descriptor->layout, Message_data(self), f); + } else if (accessor_type == METHOD_WRAPPER_GETTER) { + VALUE value = layout_get(self->descriptor->layout, Message_data(self), f); + if (value != Qnil) { + value = rb_funcall(value, rb_intern("value"), 0); + } + return value; + } else if (accessor_type == METHOD_WRAPPER_SETTER) { + VALUE wrapper = ruby_wrapper_type(f, argv[1]); + layout_set(self->descriptor->layout, Message_data(self), f, wrapper); + return Qnil; + } else if (accessor_type == METHOD_ENUM_GETTER) { + VALUE enum_type = field_type_class(f); + VALUE method = rb_intern("const_get"); + VALUE raw_value = layout_get(self->descriptor->layout, Message_data(self), f); + + // Map repeated fields to a new type with ints + if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) { + int array_size = FIX2INT(rb_funcall(raw_value, rb_intern("length"), 0)); + VALUE array_args[1] = { ID2SYM(rb_intern("int64")) }; + VALUE array = rb_class_new_instance(1, array_args, CLASS_OF(raw_value)); + for (int i = 0; i < array_size; i++) { + VALUE entry = rb_funcall(enum_type, method, 1, rb_funcall(raw_value, + rb_intern("at"), 1, INT2NUM(i))); + rb_funcall(array, rb_intern("push"), 1, entry); + } + return array; } + // Convert the value for singular fields + return rb_funcall(enum_type, method, 1, raw_value); + } else { + return layout_get(self->descriptor->layout, Message_data(self), f); } } + VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) { MessageHeader* self; - VALUE method_name, method_str; - char* name; - size_t name_len; - bool setter; const upb_oneofdef* o; const upb_fielddef* f; @@ -198,30 +375,15 @@ VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) { if (argc < 1) { rb_raise(rb_eArgError, "Expected method name as first argument."); } - method_name = argv[0]; - if (!SYMBOL_P(method_name)) { - rb_raise(rb_eArgError, "Expected symbol as method name."); - } - method_str = rb_id2str(SYM2ID(method_name)); - name = RSTRING_PTR(method_str); - name_len = RSTRING_LEN(method_str); - setter = false; - - // Setters have names that end in '='. - if (name[name_len - 1] == '=') { - setter = true; - name_len--; - } - // See if this name corresponds to either a oneof or field in this message. - if (!upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len, &f, - &o)) { + int accessor_type = extract_method_call(argv[0], self, &f, &o); + if (accessor_type == METHOD_UNKNOWN) { return rb_call_super(argc, argv); + } else if (o != NULL) { + return accessor_type == METHOD_SETTER ? Qfalse : Qtrue; + } else { + return Qtrue; } - if (o != NULL) { - return setter ? Qfalse : Qtrue; - } - return Qtrue; } VALUE create_submsg_from_hash(const upb_fielddef *f, VALUE hash) { @@ -265,7 +427,8 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { if (TYPE(val) != T_HASH) { rb_raise(rb_eArgError, - "Expected Hash object as initializer value for map field '%s'.", name); + "Expected Hash object as initializer value for map field '%s' (given %s).", + name, rb_class2name(CLASS_OF(val))); } map = layout_get(self->descriptor->layout, Message_data(self), f); Map_merge_into_self(map, val); @@ -274,7 +437,8 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { if (TYPE(val) != T_ARRAY) { rb_raise(rb_eArgError, - "Expected array as initializer value for repeated field '%s'.", name); + "Expected array as initializer value for repeated field '%s' (given %s).", + name, rb_class2name(CLASS_OF(val))); } ary = layout_get(self->descriptor->layout, Message_data(self), f); for (int i = 0; i < RARRAY_LEN(val); i++) { @@ -444,13 +608,25 @@ VALUE Message_to_h(VALUE _self) { !upb_msg_field_done(&it); upb_msg_field_next(&it)) { const upb_fielddef* field = upb_msg_iter_field(&it); + + // For proto2, do not include fields which are not set. + if (upb_msgdef_syntax(self->descriptor->msgdef) == UPB_SYNTAX_PROTO2 && + field_contains_hasbit(self->descriptor->layout, field) && + !layout_has(self->descriptor->layout, Message_data(self), field)) { + continue; + } + VALUE msg_value = layout_get(self->descriptor->layout, Message_data(self), field); VALUE msg_key = ID2SYM(rb_intern(upb_fielddef_name(field))); - if (upb_fielddef_ismap(field)) { + if (is_map_field(field)) { msg_value = Map_to_h(msg_value); } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { msg_value = RepeatedField_to_ary(msg_value); + if (upb_msgdef_syntax(self->descriptor->msgdef) == UPB_SYNTAX_PROTO2 && + RARRAY_LEN(msg_value) == 0) { + continue; + } if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) { for (int i = 0; i < RARRAY_LEN(msg_value); i++) { @@ -458,6 +634,7 @@ VALUE Message_to_h(VALUE _self) { rb_ary_store(msg_value, i, Message_to_h(elem)); } } + } else if (msg_value != Qnil && upb_fielddef_type(field) == UPB_TYPE_MESSAGE) { msg_value = Message_to_h(msg_value); @@ -557,15 +734,16 @@ VALUE build_class_from_descriptor(Descriptor* desc) { // Also define #clone so that we don't inherit Object#clone. rb_define_method(klass, "clone", Message_dup, 0); rb_define_method(klass, "==", Message_eq, 1); + rb_define_method(klass, "eql?", Message_eq, 1); rb_define_method(klass, "hash", Message_hash, 0); rb_define_method(klass, "to_h", Message_to_h, 0); - rb_define_method(klass, "to_hash", Message_to_h, 0); rb_define_method(klass, "inspect", Message_inspect, 0); + rb_define_method(klass, "to_s", Message_inspect, 0); rb_define_method(klass, "[]", Message_index, 1); rb_define_method(klass, "[]=", Message_index_set, 2); rb_define_singleton_method(klass, "decode", Message_decode, 1); rb_define_singleton_method(klass, "encode", Message_encode, 1); - rb_define_singleton_method(klass, "decode_json", Message_decode_json, 1); + rb_define_singleton_method(klass, "decode_json", Message_decode_json, -1); rb_define_singleton_method(klass, "encode_json", Message_encode_json, -1); rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0); @@ -635,10 +813,9 @@ VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) { const char* name = upb_enum_iter_name(&it); int32_t value = upb_enum_iter_number(&it); if (name[0] < 'A' || name[0] > 'Z') { - rb_raise(cTypeError, - "Enum value '%s' does not start with an uppercase letter " - "as is required for Ruby constants.", - name); + rb_warn("Enum value '%s' does not start with an uppercase letter " + "as is required for Ruby constants.", + name); } rb_define_const(mod, name, INT2NUM(value)); } diff --git a/ruby/ext/google/protobuf_c/protobuf.c b/ruby/ext/google/protobuf_c/protobuf.c index fe6bb40666..fd964c781e 100644 --- a/ruby/ext/google/protobuf_c/protobuf.c +++ b/ruby/ext/google/protobuf_c/protobuf.c @@ -91,12 +91,14 @@ void Init_protobuf_c() { descriptor_instancevar_interned = rb_intern(kDescriptorInstanceVar); DescriptorPool_register(protobuf); Descriptor_register(protobuf); + FileDescriptor_register(protobuf); FieldDescriptor_register(protobuf); OneofDescriptor_register(protobuf); EnumDescriptor_register(protobuf); MessageBuilderContext_register(internal); OneofBuilderContext_register(internal); EnumBuilderContext_register(internal); + FileBuilderContext_register(internal); Builder_register(internal); RepeatedField_register(protobuf); Map_register(protobuf); diff --git a/ruby/ext/google/protobuf_c/protobuf.h b/ruby/ext/google/protobuf_c/protobuf.h index 3e5c052061..8731eeb47b 100644 --- a/ruby/ext/google/protobuf_c/protobuf.h +++ b/ruby/ext/google/protobuf_c/protobuf.h @@ -40,6 +40,7 @@ // Forward decls. struct DescriptorPool; struct Descriptor; +struct FileDescriptor; struct FieldDescriptor; struct EnumDescriptor; struct MessageLayout; @@ -47,10 +48,12 @@ struct MessageField; struct MessageHeader; struct MessageBuilderContext; struct EnumBuilderContext; +struct FileBuilderContext; struct Builder; typedef struct DescriptorPool DescriptorPool; typedef struct Descriptor Descriptor; +typedef struct FileDescriptor FileDescriptor; typedef struct FieldDescriptor FieldDescriptor; typedef struct OneofDescriptor OneofDescriptor; typedef struct EnumDescriptor EnumDescriptor; @@ -60,6 +63,7 @@ typedef struct MessageHeader MessageHeader; typedef struct MessageBuilderContext MessageBuilderContext; typedef struct OneofBuilderContext OneofBuilderContext; typedef struct EnumBuilderContext EnumBuilderContext; +typedef struct FileBuilderContext FileBuilderContext; typedef struct Builder Builder; /* @@ -118,6 +122,10 @@ struct Descriptor { const upb_handlers* json_serialize_handlers_preserve; }; +struct FileDescriptor { + const upb_filedef* filedef; +}; + struct FieldDescriptor { const upb_fielddef* fielddef; }; @@ -145,18 +153,27 @@ struct EnumBuilderContext { VALUE enumdesc; }; +struct FileBuilderContext { + VALUE pending_list; + VALUE file_descriptor; + VALUE builder; +}; + struct Builder { VALUE pending_list; + VALUE default_file_descriptor; upb_def** defs; // used only while finalizing }; extern VALUE cDescriptorPool; extern VALUE cDescriptor; +extern VALUE cFileDescriptor; extern VALUE cFieldDescriptor; extern VALUE cEnumDescriptor; extern VALUE cMessageBuilderContext; extern VALUE cOneofBuilderContext; extern VALUE cEnumBuilderContext; +extern VALUE cFileBuilderContext; extern VALUE cBuilder; extern VALUE cError; @@ -175,15 +192,18 @@ VALUE DescriptorPool_alloc(VALUE klass); void DescriptorPool_register(VALUE module); DescriptorPool* ruby_to_DescriptorPool(VALUE value); VALUE DescriptorPool_add(VALUE _self, VALUE def); -VALUE DescriptorPool_build(VALUE _self); +VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self); VALUE DescriptorPool_lookup(VALUE _self, VALUE name); VALUE DescriptorPool_generated_pool(VALUE _self); +extern VALUE generated_pool; + void Descriptor_mark(void* _self); void Descriptor_free(void* _self); VALUE Descriptor_alloc(VALUE klass); void Descriptor_register(VALUE module); Descriptor* ruby_to_Descriptor(VALUE value); +VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb); VALUE Descriptor_name(VALUE _self); VALUE Descriptor_name_set(VALUE _self, VALUE str); VALUE Descriptor_each(VALUE _self); @@ -193,8 +213,19 @@ VALUE Descriptor_add_oneof(VALUE _self, VALUE obj); VALUE Descriptor_each_oneof(VALUE _self); VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name); VALUE Descriptor_msgclass(VALUE _self); +VALUE Descriptor_file_descriptor(VALUE _self); extern const rb_data_type_t _Descriptor_type; +void FileDescriptor_mark(void* _self); +void FileDescriptor_free(void* _self); +VALUE FileDescriptor_alloc(VALUE klass); +void FileDescriptor_register(VALUE module); +FileDescriptor* ruby_to_FileDescriptor(VALUE value); +VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self); +VALUE FileDescriptor_name(VALUE _self); +VALUE FileDescriptor_syntax(VALUE _self); +VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax); + void FieldDescriptor_mark(void* _self); void FieldDescriptor_free(void* _self); VALUE FieldDescriptor_alloc(VALUE klass); @@ -204,6 +235,8 @@ VALUE FieldDescriptor_name(VALUE _self); VALUE FieldDescriptor_name_set(VALUE _self, VALUE str); VALUE FieldDescriptor_type(VALUE _self); VALUE FieldDescriptor_type_set(VALUE _self, VALUE type); +VALUE FieldDescriptor_default(VALUE _self); +VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value); VALUE FieldDescriptor_label(VALUE _self); VALUE FieldDescriptor_label_set(VALUE _self, VALUE label); VALUE FieldDescriptor_number(VALUE _self); @@ -211,6 +244,8 @@ VALUE FieldDescriptor_number_set(VALUE _self, VALUE number); VALUE FieldDescriptor_submsg_name(VALUE _self); VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value); VALUE FieldDescriptor_subtype(VALUE _self); +VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb); +VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb); VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb); VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value); upb_fieldtype_t ruby_to_fieldtype(VALUE type); @@ -231,6 +266,8 @@ void EnumDescriptor_free(void* _self); VALUE EnumDescriptor_alloc(VALUE klass); void EnumDescriptor_register(VALUE module); EnumDescriptor* ruby_to_EnumDescriptor(VALUE value); +VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb); +VALUE EnumDescriptor_file_descriptor(VALUE _self); VALUE EnumDescriptor_name(VALUE _self); VALUE EnumDescriptor_name_set(VALUE _self, VALUE str); VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number); @@ -272,12 +309,23 @@ EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value); VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdesc); VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number); +void FileBuilderContext_mark(void* _self); +void FileBuilderContext_free(void* _self); +VALUE FileBuilderContext_alloc(VALUE klass); +void FileBuilderContext_register(VALUE module); +VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor, + VALUE builder); +VALUE FileBuilderContext_add_message(VALUE _self, VALUE name); +VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name); +VALUE FileBuilderContext_pending_descriptors(VALUE _self); + void Builder_mark(void* _self); void Builder_free(void* _self); VALUE Builder_alloc(VALUE klass); void Builder_register(VALUE module); Builder* ruby_to_Builder(VALUE value); VALUE Builder_initialize(VALUE _self); +VALUE Builder_add_file(int argc, VALUE *argv, VALUE _self); VALUE Builder_add_message(VALUE _self, VALUE name); VALUE Builder_add_enum(VALUE _self, VALUE name); VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb); @@ -289,14 +337,16 @@ VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb); #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t) size_t native_slot_size(upb_fieldtype_t type); -void native_slot_set(upb_fieldtype_t type, +void native_slot_set(const char* name, + upb_fieldtype_t type, VALUE type_class, void* memory, VALUE value); // Atomically (with respect to Ruby VM calls) either update the value and set a // oneof case, or do neither. If |case_memory| is null, then no case value is // set. -void native_slot_set_value_and_case(upb_fieldtype_t type, +void native_slot_set_value_and_case(const char* name, + upb_fieldtype_t type, VALUE type_class, void* memory, VALUE value, @@ -312,7 +362,7 @@ void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from); bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2); VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value); -void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE value); +void native_slot_check_int_range_precision(const char* name, upb_fieldtype_t type, VALUE value); extern rb_encoding* kRubyStringUtf8Encoding; extern rb_encoding* kRubyStringASCIIEncoding; @@ -443,10 +493,12 @@ VALUE Map_iter_value(Map_iter* iter); // ----------------------------------------------------------------------------- #define MESSAGE_FIELD_NO_CASE ((size_t)-1) +#define MESSAGE_FIELD_NO_HASBIT ((size_t)-1) struct MessageField { size_t offset; size_t case_offset; // for oneofs, a uint32. Else, MESSAGE_FIELD_NO_CASE. + size_t hasbit; }; struct MessageLayout { @@ -457,6 +509,9 @@ struct MessageLayout { MessageLayout* create_layout(const upb_msgdef* msgdef); void free_layout(MessageLayout* layout); +bool field_contains_hasbit(MessageLayout* layout, + const upb_fielddef* field); +VALUE layout_get_default(const upb_fielddef* field); VALUE layout_get(MessageLayout* layout, const void* storage, const upb_fielddef* field); @@ -464,6 +519,12 @@ void layout_set(MessageLayout* layout, void* storage, const upb_fielddef* field, VALUE val); +VALUE layout_has(MessageLayout* layout, + const void* storage, + const upb_fielddef* field); +void layout_clear(MessageLayout* layout, + const void* storage, + const upb_fielddef* field); void layout_init(MessageLayout* layout, void* storage); void layout_mark(MessageLayout* layout, void* storage); void layout_dup(MessageLayout* layout, void* to, void* from); @@ -513,7 +574,7 @@ VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value); VALUE Message_descriptor(VALUE klass); VALUE Message_decode(VALUE klass, VALUE data); VALUE Message_encode(VALUE klass, VALUE msg_rb); -VALUE Message_decode_json(VALUE klass, VALUE data); +VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass); VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass); VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb); diff --git a/ruby/ext/google/protobuf_c/repeated_field.c b/ruby/ext/google/protobuf_c/repeated_field.c index c6620ee611..29e829ef2c 100644 --- a/ruby/ext/google/protobuf_c/repeated_field.c +++ b/ruby/ext/google/protobuf_c/repeated_field.c @@ -178,7 +178,7 @@ VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) { } memory = RepeatedField_memoryat(self, index, element_size); - native_slot_set(field_type, field_type_class, memory, val); + native_slot_set("", field_type, field_type_class, memory, val); return Qnil; } @@ -217,12 +217,18 @@ VALUE RepeatedField_push(VALUE _self, VALUE val) { RepeatedField_reserve(self, self->size + 1); memory = (void *) (((uint8_t *)self->elements) + self->size * element_size); - native_slot_set(field_type, self->field_type_class, memory, val); + native_slot_set("", field_type, self->field_type_class, memory, val); // native_slot_set may raise an error; bump size only after set. self->size++; return _self; } +VALUE RepeatedField_push_vararg(VALUE _self, VALUE args) { + for (int i = 0; i < RARRAY_LEN(args); i++) { + RepeatedField_push(_self, rb_ary_entry(args, i)); + } + return _self; +} // Used by parsing handlers. void RepeatedField_push_native(VALUE _self, void* data) { @@ -635,7 +641,7 @@ void RepeatedField_register(VALUE module) { rb_define_method(klass, "[]", RepeatedField_index, -1); rb_define_method(klass, "at", RepeatedField_index, -1); rb_define_method(klass, "[]=", RepeatedField_index_set, 2); - rb_define_method(klass, "push", RepeatedField_push, 1); + rb_define_method(klass, "push", RepeatedField_push_vararg, -2); rb_define_method(klass, "<<", RepeatedField_push, 1); rb_define_private_method(klass, "pop_one", RepeatedField_pop_one, 0); rb_define_method(klass, "replace", RepeatedField_replace, 1); diff --git a/ruby/ext/google/protobuf_c/storage.c b/ruby/ext/google/protobuf_c/storage.c index 163b2f8172..ba4f831b9a 100644 --- a/ruby/ext/google/protobuf_c/storage.c +++ b/ruby/ext/google/protobuf_c/storage.c @@ -38,6 +38,8 @@ // Ruby <-> native slot management. // ----------------------------------------------------------------------------- +#define CHARPTR_AT(msg, ofs) ((char*)msg + ofs) +#define DEREF_OFFSET(msg, ofs, type) *(type*)CHARPTR_AT(msg, ofs) #define DEREF(memory, type) *(type*)(memory) size_t native_slot_size(upb_fieldtype_t type) { @@ -57,46 +59,16 @@ size_t native_slot_size(upb_fieldtype_t type) { } } -static VALUE value_from_default(const upb_fielddef *field) { - switch (upb_fielddef_type(field)) { - case UPB_TYPE_FLOAT: return DBL2NUM(upb_fielddef_defaultfloat(field)); - case UPB_TYPE_DOUBLE: return DBL2NUM(upb_fielddef_defaultdouble(field)); - case UPB_TYPE_BOOL: - return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse; - case UPB_TYPE_MESSAGE: return Qnil; - case UPB_TYPE_ENUM: { - const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field); - int32_t num = upb_fielddef_defaultint32(field); - const char *label = upb_enumdef_iton(enumdef, num); - if (label) { - return ID2SYM(rb_intern(label)); - } else { - return INT2NUM(num); - } - } - case UPB_TYPE_INT32: return INT2NUM(upb_fielddef_defaultint32(field)); - case UPB_TYPE_INT64: return LL2NUM(upb_fielddef_defaultint64(field));; - case UPB_TYPE_UINT32: return UINT2NUM(upb_fielddef_defaultuint32(field)); - case UPB_TYPE_UINT64: return ULL2NUM(upb_fielddef_defaultuint64(field)); - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: { - size_t size; - const char *str = upb_fielddef_defaultstr(field, &size); - return rb_str_new(str, size); - } - default: return Qnil; - } -} - static bool is_ruby_num(VALUE value) { return (TYPE(value) == T_FLOAT || TYPE(value) == T_FIXNUM || TYPE(value) == T_BIGNUM); } -void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE val) { +void native_slot_check_int_range_precision(const char* name, upb_fieldtype_t type, VALUE val) { if (!is_ruby_num(val)) { - rb_raise(cTypeError, "Expected number type for integral field."); + rb_raise(cTypeError, "Expected number type for integral field '%s' (given %s).", + name, rb_class2name(CLASS_OF(val))); } // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper @@ -106,13 +78,15 @@ void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE val) { double dbl_val = NUM2DBL(val); if (floor(dbl_val) != dbl_val) { rb_raise(rb_eRangeError, - "Non-integral floating point value assigned to integer field."); + "Non-integral floating point value assigned to integer field '%s' (given %s).", + name, rb_class2name(CLASS_OF(val))); } } if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) { if (NUM2DBL(val) < 0) { rb_raise(rb_eRangeError, - "Assigning negative value to unsigned integer field."); + "Assigning negative value to unsigned integer field '%s' (given %s).", + name, rb_class2name(CLASS_OF(val))); } } } @@ -137,12 +111,14 @@ VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value) { return value; } -void native_slot_set(upb_fieldtype_t type, VALUE type_class, +void native_slot_set(const char* name, + upb_fieldtype_t type, VALUE type_class, void* memory, VALUE value) { - native_slot_set_value_and_case(type, type_class, memory, value, NULL, 0); + native_slot_set_value_and_case(name, type, type_class, memory, value, NULL, 0); } -void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, +void native_slot_set_value_and_case(const char* name, + upb_fieldtype_t type, VALUE type_class, void* memory, VALUE value, uint32_t* case_memory, uint32_t case_number) { @@ -153,13 +129,15 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, switch (type) { case UPB_TYPE_FLOAT: if (!is_ruby_num(value)) { - rb_raise(cTypeError, "Expected number type for float field."); + rb_raise(cTypeError, "Expected number type for float field '%s' (given %s).", + name, rb_class2name(CLASS_OF(value))); } DEREF(memory, float) = NUM2DBL(value); break; case UPB_TYPE_DOUBLE: if (!is_ruby_num(value)) { - rb_raise(cTypeError, "Expected number type for double field."); + rb_raise(cTypeError, "Expected number type for double field '%s' (given %s).", + name, rb_class2name(CLASS_OF(value))); } DEREF(memory, double) = NUM2DBL(value); break; @@ -170,7 +148,8 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, } else if (value == Qfalse) { val = 0; } else { - rb_raise(cTypeError, "Invalid argument for boolean field."); + rb_raise(cTypeError, "Invalid argument for boolean field '%s' (given %s).", + name, rb_class2name(CLASS_OF(value))); } DEREF(memory, int8_t) = val; break; @@ -179,7 +158,8 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, if (CLASS_OF(value) == rb_cSymbol) { value = rb_funcall(value, rb_intern("to_s"), 0); } else if (CLASS_OF(value) != rb_cString) { - rb_raise(cTypeError, "Invalid argument for string field."); + rb_raise(cTypeError, "Invalid argument for string field '%s' (given %s).", + name, rb_class2name(CLASS_OF(value))); } DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value); @@ -187,7 +167,8 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, case UPB_TYPE_BYTES: { if (CLASS_OF(value) != rb_cString) { - rb_raise(cTypeError, "Invalid argument for string field."); + rb_raise(cTypeError, "Invalid argument for bytes field '%s' (given %s).", + name, rb_class2name(CLASS_OF(value))); } DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value); @@ -197,9 +178,39 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, if (CLASS_OF(value) == CLASS_OF(Qnil)) { value = Qnil; } else if (CLASS_OF(value) != type_class) { - rb_raise(cTypeError, - "Invalid type %s to assign to submessage field.", - rb_class2name(CLASS_OF(value))); + // check for possible implicit conversions + VALUE converted_value = NULL; + char* field_type_name = rb_class2name(type_class); + + if (strcmp(field_type_name, "Google::Protobuf::Timestamp") == 0 && + rb_obj_is_kind_of(value, rb_cTime)) { + // Time -> Google::Protobuf::Timestamp + VALUE hash = rb_hash_new(); + rb_hash_aset(hash, rb_str_new2("seconds"), rb_funcall(value, rb_intern("to_i"), 0)); + rb_hash_aset(hash, rb_str_new2("nanos"), rb_funcall(value, rb_intern("nsec"), 0)); + VALUE args[1] = { hash }; + converted_value = rb_class_new_instance(1, args, type_class); + } else if (strcmp(field_type_name, "Google::Protobuf::Duration") == 0 && + rb_obj_is_kind_of(value, rb_cNumeric)) { + // Numeric -> Google::Protobuf::Duration + VALUE hash = rb_hash_new(); + rb_hash_aset(hash, rb_str_new2("seconds"), rb_funcall(value, rb_intern("to_i"), 0)); + VALUE n_value = rb_funcall(value, rb_intern("remainder"), 1, INT2NUM(1)); + n_value = rb_funcall(n_value, rb_intern("*"), 1, INT2NUM(1000000000)); + n_value = rb_funcall(n_value, rb_intern("round"), 0); + rb_hash_aset(hash, rb_str_new2("nanos"), n_value); + VALUE args[1] = { hash }; + converted_value = rb_class_new_instance(1, args, type_class); + } + + // raise if no suitable conversaion could be found + if (converted_value == NULL) { + rb_raise(cTypeError, + "Invalid type %s to assign to submessage field '%s'.", + rb_class2name(CLASS_OF(value)), name); + } else { + value = converted_value; + } } DEREF(memory, VALUE) = value; break; @@ -210,18 +221,18 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, value = rb_funcall(value, rb_intern("to_sym"), 0); } else if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) { rb_raise(cTypeError, - "Expected number or symbol type for enum field."); + "Expected number or symbol type for enum field '%s'.", name); } if (TYPE(value) == T_SYMBOL) { // Ensure that the given symbol exists in the enum module. VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value); if (lookup == Qnil) { - rb_raise(rb_eRangeError, "Unknown symbol value for enum field."); + rb_raise(rb_eRangeError, "Unknown symbol value for enum field '%s'.", name); } else { int_val = NUM2INT(lookup); } } else { - native_slot_check_int_range_precision(UPB_TYPE_INT32, value); + native_slot_check_int_range_precision(name, UPB_TYPE_INT32, value); int_val = NUM2INT(value); } DEREF(memory, int32_t) = int_val; @@ -231,7 +242,7 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, case UPB_TYPE_INT64: case UPB_TYPE_UINT32: case UPB_TYPE_UINT64: - native_slot_check_int_range_precision(type, value); + native_slot_check_int_range_precision(name, type, value); switch (type) { case UPB_TYPE_INT32: DEREF(memory, int32_t) = NUM2INT(value); @@ -404,7 +415,12 @@ const upb_msgdef *map_entry_msgdef(const upb_fielddef* field) { } bool is_map_field(const upb_fielddef *field) { - return tryget_map_entry_msgdef(field) != NULL; + const upb_msgdef* subdef = tryget_map_entry_msgdef(field); + if (subdef == NULL) return false; + + // Map fields are a proto3 feature. + // If we're using proto2 syntax we need to fallback to the repeated field. + return upb_msgdef_syntax(subdef) == UPB_SYNTAX_PROTO3; } const upb_fielddef* map_field_key(const upb_fielddef* field) { @@ -433,6 +449,12 @@ const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) { // Memory layout management. // ----------------------------------------------------------------------------- +bool field_contains_hasbit(MessageLayout* layout, + const upb_fielddef* field) { + return layout->fields[upb_fielddef_index(field)].hasbit != + MESSAGE_FIELD_NO_HASBIT; +} + static size_t align_up_to(size_t offset, size_t granularity) { // Granularity must be a power of two. return (offset + granularity - 1) & ~(granularity - 1); @@ -447,6 +469,23 @@ MessageLayout* create_layout(const upb_msgdef* msgdef) { layout->fields = ALLOC_N(MessageField, nfields); + size_t hasbit = 0; + for (upb_msg_field_begin(&it, msgdef); + !upb_msg_field_done(&it); + upb_msg_field_next(&it)) { + const upb_fielddef* field = upb_msg_iter_field(&it); + if (upb_fielddef_haspresence(field)) { + layout->fields[upb_fielddef_index(field)].hasbit = hasbit++; + } else { + layout->fields[upb_fielddef_index(field)].hasbit = + MESSAGE_FIELD_NO_HASBIT; + } + } + + if (hasbit != 0) { + off += (hasbit + 8 - 1) / 8; + } + for (upb_msg_field_begin(&it, msgdef); !upb_msg_field_done(&it); upb_msg_field_next(&it)) { @@ -569,6 +608,137 @@ static uint32_t* slot_oneof_case(MessageLayout* layout, layout->fields[upb_fielddef_index(field)].case_offset); } +static void slot_set_hasbit(MessageLayout* layout, + const void* storage, + const upb_fielddef* field) { + size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit; + assert(hasbit != MESSAGE_FIELD_NO_HASBIT); + + ((uint8_t*)storage)[hasbit / 8] |= 1 << (hasbit % 8); +} + +static void slot_clear_hasbit(MessageLayout* layout, + const void* storage, + const upb_fielddef* field) { + size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit; + assert(hasbit != MESSAGE_FIELD_NO_HASBIT); + ((uint8_t*)storage)[hasbit / 8] &= ~(1 << (hasbit % 8)); +} + +static bool slot_is_hasbit_set(MessageLayout* layout, + const void* storage, + const upb_fielddef* field) { + size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit; + if (hasbit == MESSAGE_FIELD_NO_HASBIT) { + return false; + } + + return DEREF_OFFSET( + (uint8_t*)storage, hasbit / 8, char) & (1 << (hasbit % 8)); +} + +VALUE layout_has(MessageLayout* layout, + const void* storage, + const upb_fielddef* field) { + assert(field_contains_hasbit(layout, field)); + return slot_is_hasbit_set(layout, storage, field) ? Qtrue : Qfalse; +} + +void layout_clear(MessageLayout* layout, + const void* storage, + const upb_fielddef* field) { + void* memory = slot_memory(layout, storage, field); + uint32_t* oneof_case = slot_oneof_case(layout, storage, field); + + if (field_contains_hasbit(layout, field)) { + slot_clear_hasbit(layout, storage, field); + } + + if (upb_fielddef_containingoneof(field)) { + memset(memory, 0, NATIVE_SLOT_MAX_SIZE); + *oneof_case = ONEOF_CASE_NONE; + } else if (is_map_field(field)) { + VALUE map = Qnil; + + const upb_fielddef* key_field = map_field_key(field); + const upb_fielddef* value_field = map_field_value(field); + VALUE type_class = field_type_class(value_field); + + if (type_class != Qnil) { + VALUE args[3] = { + fieldtype_to_ruby(upb_fielddef_type(key_field)), + fieldtype_to_ruby(upb_fielddef_type(value_field)), + type_class, + }; + map = rb_class_new_instance(3, args, cMap); + } else { + VALUE args[2] = { + fieldtype_to_ruby(upb_fielddef_type(key_field)), + fieldtype_to_ruby(upb_fielddef_type(value_field)), + }; + map = rb_class_new_instance(2, args, cMap); + } + + DEREF(memory, VALUE) = map; + } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { + VALUE ary = Qnil; + + VALUE type_class = field_type_class(field); + + if (type_class != Qnil) { + VALUE args[2] = { + fieldtype_to_ruby(upb_fielddef_type(field)), + type_class, + }; + ary = rb_class_new_instance(2, args, cRepeatedField); + } else { + VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) }; + ary = rb_class_new_instance(1, args, cRepeatedField); + } + + DEREF(memory, VALUE) = ary; + } else { + native_slot_set(upb_fielddef_name(field), + upb_fielddef_type(field), field_type_class(field), + memory, layout_get_default(field)); + } +} + +VALUE layout_get_default(const upb_fielddef *field) { + switch (upb_fielddef_type(field)) { + case UPB_TYPE_FLOAT: return DBL2NUM(upb_fielddef_defaultfloat(field)); + case UPB_TYPE_DOUBLE: return DBL2NUM(upb_fielddef_defaultdouble(field)); + case UPB_TYPE_BOOL: + return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse; + case UPB_TYPE_MESSAGE: return Qnil; + case UPB_TYPE_ENUM: { + const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field); + int32_t num = upb_fielddef_defaultint32(field); + const char *label = upb_enumdef_iton(enumdef, num); + if (label) { + return ID2SYM(rb_intern(label)); + } else { + return INT2NUM(num); + } + } + case UPB_TYPE_INT32: return INT2NUM(upb_fielddef_defaultint32(field)); + case UPB_TYPE_INT64: return LL2NUM(upb_fielddef_defaultint64(field));; + case UPB_TYPE_UINT32: return UINT2NUM(upb_fielddef_defaultuint32(field)); + case UPB_TYPE_UINT64: return ULL2NUM(upb_fielddef_defaultuint64(field)); + case UPB_TYPE_STRING: + case UPB_TYPE_BYTES: { + size_t size; + const char *str = upb_fielddef_defaultstr(field, &size); + VALUE str_rb = rb_str_new(str, size); + + rb_enc_associate(str_rb, (upb_fielddef_type(field) == UPB_TYPE_BYTES) ? + kRubyString8bitEncoding : kRubyStringUtf8Encoding); + rb_obj_freeze(str_rb); + return str_rb; + } + default: return Qnil; + } +} VALUE layout_get(MessageLayout* layout, const void* storage, @@ -576,15 +746,24 @@ VALUE layout_get(MessageLayout* layout, void* memory = slot_memory(layout, storage, field); uint32_t* oneof_case = slot_oneof_case(layout, storage, field); + bool field_set; + if (field_contains_hasbit(layout, field)) { + field_set = slot_is_hasbit_set(layout, storage, field); + } else { + field_set = true; + } + if (upb_fielddef_containingoneof(field)) { if (*oneof_case != upb_fielddef_number(field)) { - return value_from_default(field); + return layout_get_default(field); } return native_slot_get(upb_fielddef_type(field), field_type_class(field), memory); } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { return *((VALUE *)memory); + } else if (!field_set) { + return layout_get_default(field); } else { return native_slot_get(upb_fielddef_type(field), field_type_class(field), @@ -678,6 +857,7 @@ void layout_set(MessageLayout* layout, // use native_slot_set_value_and_case(), which ensures that both the value // and case number are altered atomically (w.r.t. the Ruby VM). native_slot_set_value_and_case( + upb_fielddef_name(field), upb_fielddef_type(field), field_type_class(field), memory, val, oneof_case, upb_fielddef_number(field)); @@ -689,67 +869,25 @@ void layout_set(MessageLayout* layout, check_repeated_field_type(val, field); DEREF(memory, VALUE) = val; } else { - native_slot_set(upb_fielddef_type(field), field_type_class(field), + native_slot_set(upb_fielddef_name(field), + upb_fielddef_type(field), field_type_class(field), memory, val); } + + if (layout->fields[upb_fielddef_index(field)].hasbit != + MESSAGE_FIELD_NO_HASBIT) { + slot_set_hasbit(layout, storage, field); + } } void layout_init(MessageLayout* layout, void* storage) { + upb_msg_field_iter it; for (upb_msg_field_begin(&it, layout->msgdef); !upb_msg_field_done(&it); upb_msg_field_next(&it)) { - const upb_fielddef* field = upb_msg_iter_field(&it); - void* memory = slot_memory(layout, storage, field); - uint32_t* oneof_case = slot_oneof_case(layout, storage, field); - - if (upb_fielddef_containingoneof(field)) { - memset(memory, 0, NATIVE_SLOT_MAX_SIZE); - *oneof_case = ONEOF_CASE_NONE; - } else if (is_map_field(field)) { - VALUE map = Qnil; - - const upb_fielddef* key_field = map_field_key(field); - const upb_fielddef* value_field = map_field_value(field); - VALUE type_class = field_type_class(value_field); - - if (type_class != Qnil) { - VALUE args[3] = { - fieldtype_to_ruby(upb_fielddef_type(key_field)), - fieldtype_to_ruby(upb_fielddef_type(value_field)), - type_class, - }; - map = rb_class_new_instance(3, args, cMap); - } else { - VALUE args[2] = { - fieldtype_to_ruby(upb_fielddef_type(key_field)), - fieldtype_to_ruby(upb_fielddef_type(value_field)), - }; - map = rb_class_new_instance(2, args, cMap); - } - - DEREF(memory, VALUE) = map; - } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { - VALUE ary = Qnil; - - VALUE type_class = field_type_class(field); - - if (type_class != Qnil) { - VALUE args[2] = { - fieldtype_to_ruby(upb_fielddef_type(field)), - type_class, - }; - ary = rb_class_new_instance(2, args, cRepeatedField); - } else { - VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) }; - ary = rb_class_new_instance(1, args, cRepeatedField); - } - - DEREF(memory, VALUE) = ary; - } else { - native_slot_init(upb_fielddef_type(field), memory); - } + layout_clear(layout, storage, upb_msg_iter_field(&it)); } } @@ -796,6 +934,11 @@ void layout_dup(MessageLayout* layout, void* to, void* from) { } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE)); } else { + if (field_contains_hasbit(layout, field)) { + if (!slot_is_hasbit_set(layout, from, field)) continue; + slot_set_hasbit(layout, to, field); + } + native_slot_dup(upb_fielddef_type(field), to_memory, from_memory); } } @@ -825,6 +968,11 @@ void layout_deep_copy(MessageLayout* layout, void* to, void* from) { DEREF(to_memory, VALUE) = RepeatedField_deep_copy(DEREF(from_memory, VALUE)); } else { + if (field_contains_hasbit(layout, field)) { + if (!slot_is_hasbit_set(layout, from, field)) continue; + slot_set_hasbit(layout, to, field); + } + native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory); } } @@ -861,8 +1009,10 @@ VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) { return Qfalse; } } else { - if (!native_slot_eq(upb_fielddef_type(field), - msg1_memory, msg2_memory)) { + if (slot_is_hasbit_set(layout, msg1, field) != + slot_is_hasbit_set(layout, msg2, field) || + !native_slot_eq(upb_fielddef_type(field), + msg1_memory, msg2_memory)) { return Qfalse; } } diff --git a/ruby/ext/google/protobuf_c/upb.c b/ruby/ext/google/protobuf_c/upb.c index c02a5ce4a4..748205083d 100644 --- a/ruby/ext/google/protobuf_c/upb.c +++ b/ruby/ext/google/protobuf_c/upb.c @@ -1,9 +1,507 @@ -// Amalgamated source file +/* Amalgamated source file */ +#define _XOPEN_SOURCE 700 #include "upb.h" +#if UINTPTR_MAX == 0xffffffff +#define UPB_SIZE(size32, size64) size32 +#else +#define UPB_SIZE(size32, size64) size64 +#endif + +#define UPB_FIELD_AT(msg, fieldtype, offset) \ + *(fieldtype*)((const char*)(msg) + offset) + +#define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \ + UPB_FIELD_AT(msg, int, case_offset) == case_val \ + ? UPB_FIELD_AT(msg, fieldtype, offset) \ + : default + +#define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \ + UPB_FIELD_AT(msg, int, case_offset) = case_val; \ + UPB_FIELD_AT(msg, fieldtype, offset) = value; +/* This file was generated by upbc (the upb compiler) from the input + * file: + * + * google/protobuf/descriptor.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. */ + +#include + + +static const upb_msglayout *const google_protobuf_FileDescriptorSet_submsgs[1] = { + &google_protobuf_FileDescriptorProto_msginit, +}; + +static const upb_msglayout_field google_protobuf_FileDescriptorSet__fields[1] = { + {1, UPB_SIZE(0, 0), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_FileDescriptorSet_msginit = { + &google_protobuf_FileDescriptorSet_submsgs[0], + &google_protobuf_FileDescriptorSet__fields[0], + UPB_SIZE(4, 8), 1, false, +}; + +static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = { + &google_protobuf_DescriptorProto_msginit, + &google_protobuf_EnumDescriptorProto_msginit, + &google_protobuf_FieldDescriptorProto_msginit, + &google_protobuf_FileOptions_msginit, + &google_protobuf_ServiceDescriptorProto_msginit, + &google_protobuf_SourceCodeInfo_msginit, +}; + +static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(12, 24), 2, 0, 9, 1}, + {3, UPB_SIZE(36, 72), 0, 0, 9, 3}, + {4, UPB_SIZE(40, 80), 0, 0, 11, 3}, + {5, UPB_SIZE(44, 88), 0, 1, 11, 3}, + {6, UPB_SIZE(48, 96), 0, 4, 11, 3}, + {7, UPB_SIZE(52, 104), 0, 2, 11, 3}, + {8, UPB_SIZE(28, 56), 4, 3, 11, 1}, + {9, UPB_SIZE(32, 64), 5, 5, 11, 1}, + {10, UPB_SIZE(56, 112), 0, 0, 5, 3}, + {11, UPB_SIZE(60, 120), 0, 0, 5, 3}, + {12, UPB_SIZE(20, 40), 3, 0, 9, 1}, +}; + +const upb_msglayout google_protobuf_FileDescriptorProto_msginit = { + &google_protobuf_FileDescriptorProto_submsgs[0], + &google_protobuf_FileDescriptorProto__fields[0], + UPB_SIZE(64, 128), 12, false, +}; + +static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = { + &google_protobuf_DescriptorProto_msginit, + &google_protobuf_DescriptorProto_ExtensionRange_msginit, + &google_protobuf_DescriptorProto_ReservedRange_msginit, + &google_protobuf_EnumDescriptorProto_msginit, + &google_protobuf_FieldDescriptorProto_msginit, + &google_protobuf_MessageOptions_msginit, + &google_protobuf_OneofDescriptorProto_msginit, +}; + +static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(16, 32), 0, 4, 11, 3}, + {3, UPB_SIZE(20, 40), 0, 0, 11, 3}, + {4, UPB_SIZE(24, 48), 0, 3, 11, 3}, + {5, UPB_SIZE(28, 56), 0, 1, 11, 3}, + {6, UPB_SIZE(32, 64), 0, 4, 11, 3}, + {7, UPB_SIZE(12, 24), 2, 5, 11, 1}, + {8, UPB_SIZE(36, 72), 0, 6, 11, 3}, + {9, UPB_SIZE(40, 80), 0, 2, 11, 3}, + {10, UPB_SIZE(44, 88), 0, 0, 9, 3}, +}; + +const upb_msglayout google_protobuf_DescriptorProto_msginit = { + &google_protobuf_DescriptorProto_submsgs[0], + &google_protobuf_DescriptorProto__fields[0], + UPB_SIZE(48, 96), 10, false, +}; + +static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = { + &google_protobuf_ExtensionRangeOptions_msginit, +}; + +static const upb_msglayout_field google_protobuf_DescriptorProto_ExtensionRange__fields[3] = { + {1, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {2, UPB_SIZE(8, 8), 2, 0, 5, 1}, + {3, UPB_SIZE(12, 16), 3, 0, 11, 1}, +}; + +const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = { + &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0], + &google_protobuf_DescriptorProto_ExtensionRange__fields[0], + UPB_SIZE(16, 24), 3, false, +}; + +static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = { + {1, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {2, UPB_SIZE(8, 8), 2, 0, 5, 1}, +}; + +const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = { + NULL, + &google_protobuf_DescriptorProto_ReservedRange__fields[0], + UPB_SIZE(12, 12), 2, false, +}; + +static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_ExtensionRangeOptions__fields[1] = { + {999, UPB_SIZE(0, 0), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = { + &google_protobuf_ExtensionRangeOptions_submsgs[0], + &google_protobuf_ExtensionRangeOptions__fields[0], + UPB_SIZE(4, 8), 1, false, +}; + +static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = { + &google_protobuf_FieldOptions_msginit, +}; + +static const upb_msglayout_field google_protobuf_FieldDescriptorProto__fields[10] = { + {1, UPB_SIZE(32, 32), 5, 0, 9, 1}, + {2, UPB_SIZE(40, 48), 6, 0, 9, 1}, + {3, UPB_SIZE(24, 24), 3, 0, 5, 1}, + {4, UPB_SIZE(8, 8), 1, 0, 14, 1}, + {5, UPB_SIZE(16, 16), 2, 0, 14, 1}, + {6, UPB_SIZE(48, 64), 7, 0, 9, 1}, + {7, UPB_SIZE(56, 80), 8, 0, 9, 1}, + {8, UPB_SIZE(72, 112), 10, 0, 11, 1}, + {9, UPB_SIZE(28, 28), 4, 0, 5, 1}, + {10, UPB_SIZE(64, 96), 9, 0, 9, 1}, +}; + +const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = { + &google_protobuf_FieldDescriptorProto_submsgs[0], + &google_protobuf_FieldDescriptorProto__fields[0], + UPB_SIZE(80, 128), 10, false, +}; + +static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = { + &google_protobuf_OneofOptions_msginit, +}; + +static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(12, 24), 2, 0, 11, 1}, +}; + +const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = { + &google_protobuf_OneofDescriptorProto_submsgs[0], + &google_protobuf_OneofDescriptorProto__fields[0], + UPB_SIZE(16, 32), 2, false, +}; + +static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = { + &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, + &google_protobuf_EnumOptions_msginit, + &google_protobuf_EnumValueDescriptorProto_msginit, +}; + +static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(16, 32), 0, 2, 11, 3}, + {3, UPB_SIZE(12, 24), 2, 1, 11, 1}, + {4, UPB_SIZE(20, 40), 0, 0, 11, 3}, + {5, UPB_SIZE(24, 48), 0, 0, 9, 3}, +}; + +const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = { + &google_protobuf_EnumDescriptorProto_submsgs[0], + &google_protobuf_EnumDescriptorProto__fields[0], + UPB_SIZE(32, 64), 5, false, +}; + +static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = { + {1, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {2, UPB_SIZE(8, 8), 2, 0, 5, 1}, +}; + +const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = { + NULL, + &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0], + UPB_SIZE(12, 12), 2, false, +}; + +static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = { + &google_protobuf_EnumValueOptions_msginit, +}; + +static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = { + {1, UPB_SIZE(8, 8), 2, 0, 9, 1}, + {2, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {3, UPB_SIZE(16, 24), 3, 0, 11, 1}, +}; + +const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = { + &google_protobuf_EnumValueDescriptorProto_submsgs[0], + &google_protobuf_EnumValueDescriptorProto__fields[0], + UPB_SIZE(24, 32), 3, false, +}; + +static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = { + &google_protobuf_MethodDescriptorProto_msginit, + &google_protobuf_ServiceOptions_msginit, +}; + +static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = { + {1, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {2, UPB_SIZE(16, 32), 0, 0, 11, 3}, + {3, UPB_SIZE(12, 24), 2, 1, 11, 1}, +}; + +const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = { + &google_protobuf_ServiceDescriptorProto_submsgs[0], + &google_protobuf_ServiceDescriptorProto__fields[0], + UPB_SIZE(24, 48), 3, false, +}; + +static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = { + &google_protobuf_MethodOptions_msginit, +}; + +static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = { + {1, UPB_SIZE(4, 8), 3, 0, 9, 1}, + {2, UPB_SIZE(12, 24), 4, 0, 9, 1}, + {3, UPB_SIZE(20, 40), 5, 0, 9, 1}, + {4, UPB_SIZE(28, 56), 6, 0, 11, 1}, + {5, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {6, UPB_SIZE(2, 2), 2, 0, 8, 1}, +}; + +const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = { + &google_protobuf_MethodDescriptorProto_submsgs[0], + &google_protobuf_MethodDescriptorProto__fields[0], + UPB_SIZE(32, 64), 6, false, +}; + +static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_FileOptions__fields[19] = { + {1, UPB_SIZE(28, 32), 11, 0, 9, 1}, + {8, UPB_SIZE(36, 48), 12, 0, 9, 1}, + {9, UPB_SIZE(8, 8), 1, 0, 14, 1}, + {10, UPB_SIZE(16, 16), 2, 0, 8, 1}, + {11, UPB_SIZE(44, 64), 13, 0, 9, 1}, + {16, UPB_SIZE(17, 17), 3, 0, 8, 1}, + {17, UPB_SIZE(18, 18), 4, 0, 8, 1}, + {18, UPB_SIZE(19, 19), 5, 0, 8, 1}, + {20, UPB_SIZE(20, 20), 6, 0, 8, 1}, + {23, UPB_SIZE(21, 21), 7, 0, 8, 1}, + {27, UPB_SIZE(22, 22), 8, 0, 8, 1}, + {31, UPB_SIZE(23, 23), 9, 0, 8, 1}, + {36, UPB_SIZE(52, 80), 14, 0, 9, 1}, + {37, UPB_SIZE(60, 96), 15, 0, 9, 1}, + {39, UPB_SIZE(68, 112), 16, 0, 9, 1}, + {40, UPB_SIZE(76, 128), 17, 0, 9, 1}, + {41, UPB_SIZE(84, 144), 18, 0, 9, 1}, + {42, UPB_SIZE(24, 24), 10, 0, 8, 1}, + {999, UPB_SIZE(92, 160), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_FileOptions_msginit = { + &google_protobuf_FileOptions_submsgs[0], + &google_protobuf_FileOptions__fields[0], + UPB_SIZE(96, 176), 19, false, +}; + +static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_MessageOptions__fields[5] = { + {1, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {2, UPB_SIZE(2, 2), 2, 0, 8, 1}, + {3, UPB_SIZE(3, 3), 3, 0, 8, 1}, + {7, UPB_SIZE(4, 4), 4, 0, 8, 1}, + {999, UPB_SIZE(8, 8), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_MessageOptions_msginit = { + &google_protobuf_MessageOptions_submsgs[0], + &google_protobuf_MessageOptions__fields[0], + UPB_SIZE(12, 16), 5, false, +}; + +static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_FieldOptions__fields[7] = { + {1, UPB_SIZE(8, 8), 1, 0, 14, 1}, + {2, UPB_SIZE(24, 24), 3, 0, 8, 1}, + {3, UPB_SIZE(25, 25), 4, 0, 8, 1}, + {5, UPB_SIZE(26, 26), 5, 0, 8, 1}, + {6, UPB_SIZE(16, 16), 2, 0, 14, 1}, + {10, UPB_SIZE(27, 27), 6, 0, 8, 1}, + {999, UPB_SIZE(28, 32), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_FieldOptions_msginit = { + &google_protobuf_FieldOptions_submsgs[0], + &google_protobuf_FieldOptions__fields[0], + UPB_SIZE(32, 40), 7, false, +}; + +static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_OneofOptions__fields[1] = { + {999, UPB_SIZE(0, 0), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_OneofOptions_msginit = { + &google_protobuf_OneofOptions_submsgs[0], + &google_protobuf_OneofOptions__fields[0], + UPB_SIZE(4, 8), 1, false, +}; + +static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_EnumOptions__fields[3] = { + {2, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {3, UPB_SIZE(2, 2), 2, 0, 8, 1}, + {999, UPB_SIZE(4, 8), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_EnumOptions_msginit = { + &google_protobuf_EnumOptions_submsgs[0], + &google_protobuf_EnumOptions__fields[0], + UPB_SIZE(8, 16), 3, false, +}; + +static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_EnumValueOptions__fields[2] = { + {1, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {999, UPB_SIZE(4, 8), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_EnumValueOptions_msginit = { + &google_protobuf_EnumValueOptions_submsgs[0], + &google_protobuf_EnumValueOptions__fields[0], + UPB_SIZE(8, 16), 2, false, +}; + +static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_ServiceOptions__fields[2] = { + {33, UPB_SIZE(1, 1), 1, 0, 8, 1}, + {999, UPB_SIZE(4, 8), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_ServiceOptions_msginit = { + &google_protobuf_ServiceOptions_submsgs[0], + &google_protobuf_ServiceOptions__fields[0], + UPB_SIZE(8, 16), 2, false, +}; + +static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = { + &google_protobuf_UninterpretedOption_msginit, +}; + +static const upb_msglayout_field google_protobuf_MethodOptions__fields[3] = { + {33, UPB_SIZE(16, 16), 2, 0, 8, 1}, + {34, UPB_SIZE(8, 8), 1, 0, 14, 1}, + {999, UPB_SIZE(20, 24), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_MethodOptions_msginit = { + &google_protobuf_MethodOptions_submsgs[0], + &google_protobuf_MethodOptions__fields[0], + UPB_SIZE(24, 32), 3, false, +}; + +static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = { + &google_protobuf_UninterpretedOption_NamePart_msginit, +}; + +static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7] = { + {2, UPB_SIZE(56, 80), 0, 0, 11, 3}, + {3, UPB_SIZE(32, 32), 4, 0, 9, 1}, + {4, UPB_SIZE(8, 8), 1, 0, 4, 1}, + {5, UPB_SIZE(16, 16), 2, 0, 3, 1}, + {6, UPB_SIZE(24, 24), 3, 0, 1, 1}, + {7, UPB_SIZE(40, 48), 5, 0, 12, 1}, + {8, UPB_SIZE(48, 64), 6, 0, 9, 1}, +}; + +const upb_msglayout google_protobuf_UninterpretedOption_msginit = { + &google_protobuf_UninterpretedOption_submsgs[0], + &google_protobuf_UninterpretedOption__fields[0], + UPB_SIZE(64, 96), 7, false, +}; + +static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = { + {1, UPB_SIZE(4, 8), 2, 0, 9, 2}, + {2, UPB_SIZE(1, 1), 1, 0, 8, 2}, +}; + +const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = { + NULL, + &google_protobuf_UninterpretedOption_NamePart__fields[0], + UPB_SIZE(16, 32), 2, false, +}; + +static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = { + &google_protobuf_SourceCodeInfo_Location_msginit, +}; + +static const upb_msglayout_field google_protobuf_SourceCodeInfo__fields[1] = { + {1, UPB_SIZE(0, 0), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_SourceCodeInfo_msginit = { + &google_protobuf_SourceCodeInfo_submsgs[0], + &google_protobuf_SourceCodeInfo__fields[0], + UPB_SIZE(4, 8), 1, false, +}; + +static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = { + {1, UPB_SIZE(20, 40), 0, 0, 5, 3}, + {2, UPB_SIZE(24, 48), 0, 0, 5, 3}, + {3, UPB_SIZE(4, 8), 1, 0, 9, 1}, + {4, UPB_SIZE(12, 24), 2, 0, 9, 1}, + {6, UPB_SIZE(28, 56), 0, 0, 9, 3}, +}; + +const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = { + NULL, + &google_protobuf_SourceCodeInfo_Location__fields[0], + UPB_SIZE(32, 64), 5, false, +}; + +static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = { + &google_protobuf_GeneratedCodeInfo_Annotation_msginit, +}; + +static const upb_msglayout_field google_protobuf_GeneratedCodeInfo__fields[1] = { + {1, UPB_SIZE(0, 0), 0, 0, 11, 3}, +}; + +const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = { + &google_protobuf_GeneratedCodeInfo_submsgs[0], + &google_protobuf_GeneratedCodeInfo__fields[0], + UPB_SIZE(4, 8), 1, false, +}; + +static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = { + {1, UPB_SIZE(20, 32), 0, 0, 5, 3}, + {2, UPB_SIZE(12, 16), 3, 0, 9, 1}, + {3, UPB_SIZE(4, 4), 1, 0, 5, 1}, + {4, UPB_SIZE(8, 8), 2, 0, 5, 1}, +}; + +const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = { + NULL, + &google_protobuf_GeneratedCodeInfo_Annotation__fields[0], + UPB_SIZE(24, 48), 4, false, +}; + + + /* Maps descriptor type -> upb field type. */ -static const uint8_t upb_desctype_to_fieldtype[] = { +const uint8_t upb_desctype_to_fieldtype[] = { UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */ UPB_TYPE_DOUBLE, /* DOUBLE */ UPB_TYPE_FLOAT, /* FLOAT */ @@ -27,7 +525,6 @@ static const uint8_t upb_desctype_to_fieldtype[] = { /* Data pertaining to the parse. */ typedef struct { - upb_env *env; /* Current decoding pointer. Points to the beginning of a field until we * have finished decoding the whole field. */ const char *ptr; @@ -40,7 +537,7 @@ typedef struct { /* These members are unset for an unknown group frame. */ char *msg; - const upb_msglayout_msginit_v1 *m; + const upb_msglayout *m; } upb_decframe; #define CHK(x) if (!(x)) { return false; } @@ -49,7 +546,7 @@ static bool upb_skip_unknowngroup(upb_decstate *d, int field_number, const char *limit); static bool upb_decode_message(upb_decstate *d, const char *limit, int group_number, char *msg, - const upb_msglayout_msginit_v1 *l); + const upb_msglayout *l); static bool upb_decode_varint(const char **ptr, const char *limit, uint64_t *val) { @@ -112,14 +609,14 @@ static int64_t upb_zzdecode_64(uint64_t n) { } static bool upb_decode_string(const char **ptr, const char *limit, - upb_stringview *val) { + upb_strview *val) { uint32_t len; CHK(upb_decode_varint32(ptr, limit, &len) && len < INT32_MAX && limit - *ptr >= (int32_t)len); - *val = upb_stringview_make(*ptr, len); + *val = upb_strview_make(*ptr, len); *ptr += len; return true; } @@ -130,9 +627,7 @@ static void upb_set32(void *msg, size_t ofs, uint32_t val) { static bool upb_append_unknown(upb_decstate *d, upb_decframe *frame, const char *start) { - UPB_UNUSED(d); - UPB_UNUSED(frame); - UPB_UNUSED(start); + upb_msg_addunknown(frame->msg, start, d->ptr - start); return true; } @@ -152,7 +647,7 @@ static bool upb_skip_unknownfielddata(upb_decstate *d, upb_decframe *frame, return upb_decode_64bit(&d->ptr, frame->limit, &val); } case UPB_WIRE_TYPE_DELIMITED: { - upb_stringview val; + upb_strview val; return upb_decode_string(&d->ptr, frame->limit, &val); } case UPB_WIRE_TYPE_START_GROUP: @@ -171,6 +666,7 @@ static bool upb_array_grow(upb_array *arr, size_t elements) { size_t new_bytes; size_t old_bytes; void *new_data; + upb_alloc *alloc = upb_arena_alloc(arr->arena); while (new_size < needed) { new_size *= 2; @@ -178,7 +674,7 @@ static bool upb_array_grow(upb_array *arr, size_t elements) { old_bytes = arr->len * arr->element_size; new_bytes = new_size * arr->element_size; - new_data = upb_realloc(arr->alloc, arr->data, old_bytes, new_bytes); + new_data = upb_realloc(alloc, arr->data, old_bytes, new_bytes); CHK(new_data); arr->data = new_data; @@ -200,23 +696,21 @@ static void *upb_array_add(upb_array *arr, size_t elements) { } static upb_array *upb_getarr(upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { UPB_ASSERT(field->label == UPB_LABEL_REPEATED); return *(upb_array**)&frame->msg[field->offset]; } -static upb_array *upb_getorcreatearr(upb_decstate *d, - upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { +static upb_array *upb_getorcreatearr(upb_decframe *frame, + const upb_msglayout_field *field) { upb_array *arr = upb_getarr(frame, field); if (!arr) { - arr = upb_env_malloc(d->env, sizeof(*arr)); + upb_fieldtype_t type = upb_desctype_to_fieldtype[field->descriptortype]; + arr = upb_array_new(type, upb_msg_arena(frame->msg)); if (!arr) { return NULL; } - upb_array_init(arr, upb_desctype_to_fieldtype[field->type], - upb_arena_alloc(upb_env_arena(d->env))); *(upb_array**)&frame->msg[field->offset] = arr; } @@ -224,26 +718,25 @@ static upb_array *upb_getorcreatearr(upb_decstate *d, } static void upb_sethasbit(upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { - UPB_ASSERT(field->hasbit != UPB_NO_HASBIT); - frame->msg[field->hasbit / 8] |= (1 << (field->hasbit % 8)); + const upb_msglayout_field *field) { + int32_t hasbit = field->presence; + UPB_ASSERT(field->presence > 0); + frame->msg[hasbit / 8] |= (1 << (hasbit % 8)); } static void upb_setoneofcase(upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { - UPB_ASSERT(field->oneof_index != UPB_NOT_IN_ONEOF); - upb_set32(frame->msg, frame->m->oneofs[field->oneof_index].case_offset, - field->number); + const upb_msglayout_field *field) { + UPB_ASSERT(field->presence < 0); + upb_set32(frame->msg, ~field->presence, field->number); } -static char *upb_decode_prepareslot(upb_decstate *d, - upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { +static char *upb_decode_prepareslot(upb_decframe *frame, + const upb_msglayout_field *field) { char *field_mem = frame->msg + field->offset; upb_array *arr; if (field->label == UPB_LABEL_REPEATED) { - arr = upb_getorcreatearr(d, frame, field); + arr = upb_getorcreatearr(frame, field); field_mem = upb_array_reserve(arr, 1); } @@ -251,36 +744,33 @@ static char *upb_decode_prepareslot(upb_decstate *d, } static void upb_decode_setpresent(upb_decframe *frame, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { if (field->label == UPB_LABEL_REPEATED) { upb_array *arr = upb_getarr(frame, field); UPB_ASSERT(arr->len < arr->size); arr->len++; - } else if (field->oneof_index != UPB_NOT_IN_ONEOF) { + } else if (field->presence < 0) { upb_setoneofcase(frame, field); - } else if (field->hasbit != UPB_NO_HASBIT) { + } else if (field->presence > 0) { upb_sethasbit(frame, field); } } -static bool upb_decode_submsg(upb_decstate *d, - upb_decframe *frame, +static bool upb_decode_submsg(upb_decstate *d, upb_decframe *frame, const char *limit, - const upb_msglayout_fieldinit_v1 *field, + const upb_msglayout_field *field, int group_number) { - char *submsg = *(void**)&frame->msg[field->offset]; - const upb_msglayout_msginit_v1 *subm; + char *submsg_slot = upb_decode_prepareslot(frame, field); + char *submsg = *(void **)submsg_slot; + const upb_msglayout *subm; - UPB_ASSERT(field->submsg_index != UPB_NO_SUBMSG); subm = frame->m->submsgs[field->submsg_index]; UPB_ASSERT(subm); if (!submsg) { - submsg = upb_env_malloc(d->env, upb_msg_sizeof((upb_msglayout *)subm)); + submsg = upb_msg_new(subm, upb_msg_arena(frame->msg)); CHK(submsg); - submsg = upb_msg_init( - submsg, (upb_msglayout*)subm, upb_arena_alloc(upb_env_arena(d->env))); - *(void**)&frame->msg[field->offset] = submsg; + *(void**)submsg_slot = submsg; } upb_decode_message(d, limit, group_number, submsg, subm); @@ -290,15 +780,15 @@ static bool upb_decode_submsg(upb_decstate *d, static bool upb_decode_varintfield(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { uint64_t val; void *field_mem; - field_mem = upb_decode_prepareslot(d, frame, field); + field_mem = upb_decode_prepareslot(frame, field); CHK(field_mem); CHK(upb_decode_varint(&d->ptr, frame->limit, &val)); - switch ((upb_descriptortype_t)field->type) { + switch ((upb_descriptortype_t)field->descriptortype) { case UPB_DESCRIPTOR_TYPE_INT64: case UPB_DESCRIPTOR_TYPE_UINT64: memcpy(field_mem, &val, sizeof(val)); @@ -335,15 +825,15 @@ static bool upb_decode_varintfield(upb_decstate *d, upb_decframe *frame, static bool upb_decode_64bitfield(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { void *field_mem; uint64_t val; - field_mem = upb_decode_prepareslot(d, frame, field); + field_mem = upb_decode_prepareslot(frame, field); CHK(field_mem); CHK(upb_decode_64bit(&d->ptr, frame->limit, &val)); - switch ((upb_descriptortype_t)field->type) { + switch ((upb_descriptortype_t)field->descriptortype) { case UPB_DESCRIPTOR_TYPE_DOUBLE: case UPB_DESCRIPTOR_TYPE_FIXED64: case UPB_DESCRIPTOR_TYPE_SFIXED64: @@ -359,15 +849,15 @@ static bool upb_decode_64bitfield(upb_decstate *d, upb_decframe *frame, static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field) { + const upb_msglayout_field *field) { void *field_mem; uint32_t val; - field_mem = upb_decode_prepareslot(d, frame, field); + field_mem = upb_decode_prepareslot(frame, field); CHK(field_mem); CHK(upb_decode_32bit(&d->ptr, frame->limit, &val)); - switch ((upb_descriptortype_t)field->type) { + switch ((upb_descriptortype_t)field->descriptortype) { case UPB_DESCRIPTOR_TYPE_FLOAT: case UPB_DESCRIPTOR_TYPE_FIXED32: case UPB_DESCRIPTOR_TYPE_SFIXED32: @@ -381,7 +871,7 @@ static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame, return true; } -static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data, +static bool upb_decode_fixedpacked(upb_array *arr, upb_strview data, int elem_size) { int elements = data.size / elem_size; void *field_mem; @@ -395,9 +885,9 @@ static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data, static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field, - upb_stringview val) { - upb_array *arr = upb_getorcreatearr(d, frame, field); + const upb_msglayout_field *field, + upb_strview val) { + upb_array *arr = upb_getorcreatearr(frame, field); #define VARINT_CASE(ctype, decode) { \ const char *ptr = val.data; \ @@ -415,7 +905,7 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, return true; \ } - switch ((upb_descriptortype_t)field->type) { + switch ((upb_descriptortype_t)field->descriptortype) { case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_BYTES: { void *field_mem = upb_array_add(arr, 1); @@ -445,9 +935,28 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, VARINT_CASE(int32_t, upb_zzdecode_32); case UPB_DESCRIPTOR_TYPE_SINT64: VARINT_CASE(int64_t, upb_zzdecode_64); - case UPB_DESCRIPTOR_TYPE_MESSAGE: + case UPB_DESCRIPTOR_TYPE_MESSAGE: { + const upb_msglayout *subm; + char *submsg; + void *field_mem; + CHK(val.size <= (size_t)(frame->limit - val.data)); - return upb_decode_submsg(d, frame, val.data + val.size, field, 0); + d->ptr -= val.size; + + /* Create elemente message. */ + subm = frame->m->submsgs[field->submsg_index]; + UPB_ASSERT(subm); + + submsg = upb_msg_new(subm, upb_msg_arena(frame->msg)); + CHK(submsg); + + field_mem = upb_array_add(arr, 1); + CHK(field_mem); + *(void**)field_mem = submsg; + + return upb_decode_message( + d, val.data + val.size, frame->group_number, submsg, subm); + } case UPB_DESCRIPTOR_TYPE_GROUP: return upb_append_unknown(d, frame, field_start); } @@ -457,24 +966,25 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame, const char *field_start, - const upb_msglayout_fieldinit_v1 *field) { - upb_stringview val; + const upb_msglayout_field *field) { + upb_strview val; CHK(upb_decode_string(&d->ptr, frame->limit, &val)); if (field->label == UPB_LABEL_REPEATED) { return upb_decode_toarray(d, frame, field_start, field, val); } else { - switch ((upb_descriptortype_t)field->type) { + switch ((upb_descriptortype_t)field->descriptortype) { case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_BYTES: { - void *field_mem = upb_decode_prepareslot(d, frame, field); + void *field_mem = upb_decode_prepareslot(frame, field); CHK(field_mem); memcpy(field_mem, &val, sizeof(val)); break; } case UPB_DESCRIPTOR_TYPE_MESSAGE: CHK(val.size <= (size_t)(frame->limit - val.data)); + d->ptr -= val.size; CHK(upb_decode_submsg(d, frame, val.data + val.size, field, 0)); break; default: @@ -486,8 +996,8 @@ static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame, } } -static const upb_msglayout_fieldinit_v1 *upb_find_field( - const upb_msglayout_msginit_v1 *l, uint32_t field_number) { +static const upb_msglayout_field *upb_find_field(const upb_msglayout *l, + uint32_t field_number) { /* Lots of optimization opportunities here. */ int i; for (i = 0; i < l->field_count; i++) { @@ -503,7 +1013,7 @@ static bool upb_decode_field(upb_decstate *d, upb_decframe *frame) { int field_number; int wire_type; const char *field_start = d->ptr; - const upb_msglayout_fieldinit_v1 *field; + const upb_msglayout_field *field; CHK(upb_decode_tag(&d->ptr, frame->limit, &field_number, &wire_type)); field = upb_find_field(frame->m, field_number); @@ -519,7 +1029,7 @@ static bool upb_decode_field(upb_decstate *d, upb_decframe *frame) { case UPB_WIRE_TYPE_DELIMITED: return upb_decode_delimitedfield(d, frame, field_start, field); case UPB_WIRE_TYPE_START_GROUP: - CHK(field->type == UPB_DESCRIPTOR_TYPE_GROUP); + CHK(field->descriptortype == UPB_DESCRIPTOR_TYPE_GROUP); return upb_decode_submsg(d, frame, frame->limit, field, field_number); case UPB_WIRE_TYPE_END_GROUP: CHK(frame->group_number == field_number) @@ -530,7 +1040,9 @@ static bool upb_decode_field(upb_decstate *d, upb_decframe *frame) { } } else { CHK(field_number != 0); - return upb_skip_unknownfielddata(d, frame, field_number, wire_type); + CHK(upb_skip_unknownfielddata(d, frame, field_number, wire_type)); + CHK(upb_append_unknown(d, frame, field_start)); + return true; } } @@ -555,7 +1067,7 @@ static bool upb_skip_unknowngroup(upb_decstate *d, int field_number, static bool upb_decode_message(upb_decstate *d, const char *limit, int group_number, char *msg, - const upb_msglayout_msginit_v1 *l) { + const upb_msglayout *l) { upb_decframe frame; frame.group_number = group_number; frame.limit = limit; @@ -569,11 +1081,9 @@ static bool upb_decode_message(upb_decstate *d, const char *limit, return true; } -bool upb_decode(upb_stringview buf, void *msg, - const upb_msglayout_msginit_v1 *l, upb_env *env) { +bool upb_decode(upb_strview buf, void *msg, const upb_msglayout *l) { upb_decstate state; state.ptr = buf.data; - state.env = env; return upb_decode_message(&state, buf.data + buf.size, 0, msg, l); } @@ -943,6 +1453,49 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { return true; } +static void assign_msg_wellknowntype(upb_msgdef *m) { + const char *name = upb_msgdef_fullname(m); + if (name == NULL) { + m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED; + return; + } + if (!strcmp(name, "google.protobuf.Any")) { + m->well_known_type = UPB_WELLKNOWN_ANY; + } else if (!strcmp(name, "google.protobuf.FieldMask")) { + m->well_known_type = UPB_WELLKNOWN_FIELDMASK; + } else if (!strcmp(name, "google.protobuf.Duration")) { + m->well_known_type = UPB_WELLKNOWN_DURATION; + } else if (!strcmp(name, "google.protobuf.Timestamp")) { + m->well_known_type = UPB_WELLKNOWN_TIMESTAMP; + } else if (!strcmp(name, "google.protobuf.DoubleValue")) { + m->well_known_type = UPB_WELLKNOWN_DOUBLEVALUE; + } else if (!strcmp(name, "google.protobuf.FloatValue")) { + m->well_known_type = UPB_WELLKNOWN_FLOATVALUE; + } else if (!strcmp(name, "google.protobuf.Int64Value")) { + m->well_known_type = UPB_WELLKNOWN_INT64VALUE; + } else if (!strcmp(name, "google.protobuf.UInt64Value")) { + m->well_known_type = UPB_WELLKNOWN_UINT64VALUE; + } else if (!strcmp(name, "google.protobuf.Int32Value")) { + m->well_known_type = UPB_WELLKNOWN_INT32VALUE; + } else if (!strcmp(name, "google.protobuf.UInt32Value")) { + m->well_known_type = UPB_WELLKNOWN_UINT32VALUE; + } else if (!strcmp(name, "google.protobuf.BoolValue")) { + m->well_known_type = UPB_WELLKNOWN_BOOLVALUE; + } else if (!strcmp(name, "google.protobuf.StringValue")) { + m->well_known_type = UPB_WELLKNOWN_STRINGVALUE; + } else if (!strcmp(name, "google.protobuf.BytesValue")) { + m->well_known_type = UPB_WELLKNOWN_BYTESVALUE; + } else if (!strcmp(name, "google.protobuf.Value")) { + m->well_known_type = UPB_WELLKNOWN_VALUE; + } else if (!strcmp(name, "google.protobuf.ListValue")) { + m->well_known_type = UPB_WELLKNOWN_LISTVALUE; + } else if (!strcmp(name, "google.protobuf.Struct")) { + m->well_known_type = UPB_WELLKNOWN_STRUCT; + } else { + m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED; + } +} + bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s) { size_t i; @@ -979,6 +1532,8 @@ bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s) { if (!assign_msg_indices(m, s)) { goto err; } + assign_msg_wellknowntype(m); + /* m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED; */ } else if (e) { upb_inttable_compact(&e->iton); } @@ -1198,6 +1753,8 @@ static void freefield(upb_refcounted *r) { upb_fielddef_uninit_default(f); if (f->subdef_is_symbolic) upb_gfree(f->sub.name); + if (f->msg_is_symbolic) + upb_gfree(f->msg.name); upb_def_uninit(upb_fielddef_upcast_mutable(f)); upb_gfree(f); } @@ -1555,7 +2112,7 @@ bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s) { s, "cannot change field number after adding to a message"); return false; } - if (number == 0 || number > UPB_MAX_FIELDNUMBER) { + if (number == 0) { upb_status_seterrf(s, "invalid field number (%u)", number); return false; } @@ -2171,6 +2728,16 @@ bool upb_msgdef_mapentry(const upb_msgdef *m) { return m->map_entry; } +upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m) { + return m->well_known_type; +} + +bool upb_msgdef_isnumberwrapper(const upb_msgdef *m) { + upb_wellknowntype_t type = upb_msgdef_wellknowntype(m); + return type >= UPB_WELLKNOWN_DOUBLEVALUE && + type <= UPB_WELLKNOWN_UINT32VALUE; +} + void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) { upb_inttable_begin(iter, &m->itof); } @@ -2673,6 +3240,14 @@ const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) { return def ? upb_dyncast_msgdef(def) : NULL; } +const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym, + size_t len) { + upb_value v; + upb_def *def = upb_strtable_lookup2(&s->symtab, sym, len, &v) ? + upb_value_getptr(v) : NULL; + return def ? upb_dyncast_msgdef(def) : NULL; +} + const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { upb_value v; upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ? @@ -2985,7 +3560,7 @@ static uint32_t upb_zzencode_32(int32_t n) { return (n << 1) ^ (n >> 31); } static uint64_t upb_zzencode_64(int64_t n) { return (n << 1) ^ (n >> 63); } typedef struct { - upb_env *env; + upb_alloc *alloc; char *buf, *ptr, *limit; } upb_encstate; @@ -3000,11 +3575,11 @@ static size_t upb_roundup_pow2(size_t bytes) { static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) { size_t old_size = e->limit - e->buf; size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr)); - char *new_buf = upb_env_realloc(e->env, e->buf, old_size, new_size); + char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size); CHK(new_buf); /* We want previous data at the end, realloc() put it at the beginning. */ - memmove(e->limit - old_size, e->buf, old_size); + memmove(new_buf + new_size - old_size, e->buf, old_size); e->ptr = new_buf + new_size - (e->limit - e->ptr); e->limit = new_buf + new_size; @@ -3064,17 +3639,17 @@ static bool upb_put_float(upb_encstate *e, float d) { return upb_put_fixed32(e, u32); } -static uint32_t upb_readcase(const char *msg, const upb_msglayout_msginit_v1 *m, - int oneof_index) { +static uint32_t upb_readcase(const char *msg, const upb_msglayout_field *f) { uint32_t ret; - memcpy(&ret, msg + m->oneofs[oneof_index].case_offset, sizeof(ret)); + uint32_t offset = ~f->presence; + memcpy(&ret, msg + offset, sizeof(ret)); return ret; } -static bool upb_readhasbit(const char *msg, - const upb_msglayout_fieldinit_v1 *f) { - UPB_ASSERT(f->hasbit != UPB_NO_HASBIT); - return msg[f->hasbit / 8] & (1 << (f->hasbit % 8)); +static bool upb_readhasbit(const char *msg, const upb_msglayout_field *f) { + uint32_t hasbit = f->presence; + UPB_ASSERT(f->presence > 0); + return msg[hasbit / 8] & (1 << (hasbit % 8)); } static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) { @@ -3088,19 +3663,18 @@ static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr, } bool upb_encode_message(upb_encstate *e, const char *msg, - const upb_msglayout_msginit_v1 *m, - size_t *size); + const upb_msglayout *m, size_t *size); static bool upb_encode_array(upb_encstate *e, const char *field_mem, - const upb_msglayout_msginit_v1 *m, - const upb_msglayout_fieldinit_v1 *f) { + const upb_msglayout *m, + const upb_msglayout_field *f) { const upb_array *arr = *(const upb_array**)field_mem; if (arr == NULL || arr->len == 0) { return true; } - UPB_ASSERT(arr->type == upb_desctype_to_fieldtype2[f->type]); + UPB_ASSERT(arr->type == upb_desctype_to_fieldtype2[f->descriptortype]); #define VARINT_CASE(ctype, encode) { \ ctype *start = arr->data; \ @@ -3115,7 +3689,7 @@ static bool upb_encode_array(upb_encstate *e, const char *field_mem, break; \ do { ; } while(0) - switch (f->type) { + switch (f->descriptortype) { case UPB_DESCRIPTOR_TYPE_DOUBLE: CHK(upb_put_fixedarray(e, arr, sizeof(double))); break; @@ -3134,9 +3708,10 @@ do { ; } while(0) case UPB_DESCRIPTOR_TYPE_UINT64: VARINT_CASE(uint64_t, *ptr); case UPB_DESCRIPTOR_TYPE_UINT32: + VARINT_CASE(uint32_t, *ptr); case UPB_DESCRIPTOR_TYPE_INT32: case UPB_DESCRIPTOR_TYPE_ENUM: - VARINT_CASE(uint32_t, *ptr); + VARINT_CASE(int32_t, (int64_t)*ptr); case UPB_DESCRIPTOR_TYPE_BOOL: VARINT_CASE(bool, *ptr); case UPB_DESCRIPTOR_TYPE_SINT32: @@ -3145,8 +3720,8 @@ do { ; } while(0) VARINT_CASE(int64_t, upb_zzencode_64(*ptr)); case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_BYTES: { - upb_stringview *start = arr->data; - upb_stringview *ptr = start + arr->len; + upb_strview *start = arr->data; + upb_strview *ptr = start + arr->len; do { ptr--; CHK(upb_put_bytes(e, ptr->data, ptr->size) && @@ -3158,7 +3733,7 @@ do { ; } while(0) case UPB_DESCRIPTOR_TYPE_GROUP: { void **start = arr->data; void **ptr = start + arr->len; - const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index]; + const upb_msglayout *subm = m->submsgs[f->submsg_index]; do { size_t size; ptr--; @@ -3171,7 +3746,7 @@ do { ; } while(0) case UPB_DESCRIPTOR_TYPE_MESSAGE: { void **start = arr->data; void **ptr = start + arr->len; - const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index]; + const upb_msglayout *subm = m->submsgs[f->submsg_index]; do { size_t size; ptr--; @@ -3191,11 +3766,9 @@ do { ; } while(0) } static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, - const upb_msglayout_msginit_v1 *m, - const upb_msglayout_fieldinit_v1 *f, - bool is_proto3) { - bool skip_zero_value = is_proto3 && f->oneof_index == UPB_NOT_IN_ONEOF; - + const upb_msglayout *m, + const upb_msglayout_field *f, + bool skip_zero_value) { #define CASE(ctype, type, wire_type, encodeval) do { \ ctype val = *(ctype*)field_mem; \ if (skip_zero_value && val == 0) { \ @@ -3205,7 +3778,7 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, upb_put_tag(e, f->number, wire_type); \ } while(0) - switch (f->type) { + switch (f->descriptortype) { case UPB_DESCRIPTOR_TYPE_DOUBLE: CASE(double, double, UPB_WIRE_TYPE_64BIT, val); case UPB_DESCRIPTOR_TYPE_FLOAT: @@ -3214,9 +3787,10 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, case UPB_DESCRIPTOR_TYPE_UINT64: CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val); case UPB_DESCRIPTOR_TYPE_UINT32: + CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val); case UPB_DESCRIPTOR_TYPE_INT32: case UPB_DESCRIPTOR_TYPE_ENUM: - CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val); + CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val); case UPB_DESCRIPTOR_TYPE_SFIXED64: case UPB_DESCRIPTOR_TYPE_FIXED64: CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val); @@ -3231,7 +3805,7 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val)); case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_BYTES: { - upb_stringview view = *(upb_stringview*)field_mem; + upb_strview view = *(upb_strview*)field_mem; if (skip_zero_value && view.size == 0) { return true; } @@ -3241,9 +3815,9 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, } case UPB_DESCRIPTOR_TYPE_GROUP: { size_t size; - void *submsg = *(void**)field_mem; - const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index]; - if (skip_zero_value && submsg == NULL) { + void *submsg = *(void **)field_mem; + const upb_msglayout *subm = m->submsgs[f->submsg_index]; + if (submsg == NULL) { return true; } return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) && @@ -3252,9 +3826,9 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, } case UPB_DESCRIPTOR_TYPE_MESSAGE: { size_t size; - void *submsg = *(void**)field_mem; - const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index]; - if (skip_zero_value && submsg == NULL) { + void *submsg = *(void **)field_mem; + const upb_msglayout *subm = m->submsgs[f->submsg_index]; + if (submsg == NULL) { return true; } return upb_encode_message(e, submsg, subm, &size) && @@ -3266,49 +3840,52 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem, UPB_UNREACHABLE(); } -bool upb_encode_hasscalarfield(const char *msg, - const upb_msglayout_msginit_v1 *m, - const upb_msglayout_fieldinit_v1 *f) { - if (f->oneof_index != UPB_NOT_IN_ONEOF) { - return upb_readcase(msg, m, f->oneof_index) == f->number; - } else if (m->is_proto2) { - return upb_readhasbit(msg, f); - } else { - /* For proto3, we'll test for the field being empty later. */ - return true; - } -} - -bool upb_encode_message(upb_encstate* e, const char *msg, - const upb_msglayout_msginit_v1 *m, - size_t *size) { +bool upb_encode_message(upb_encstate *e, const char *msg, + const upb_msglayout *m, size_t *size) { int i; - char *buf_end = e->ptr; - - if (msg == NULL) { - return true; - } + size_t pre_len = e->limit - e->ptr; + const char *unknown; + size_t unknown_size; for (i = m->field_count - 1; i >= 0; i--) { - const upb_msglayout_fieldinit_v1 *f = &m->fields[i]; + const upb_msglayout_field *f = &m->fields[i]; if (f->label == UPB_LABEL_REPEATED) { CHK(upb_encode_array(e, msg + f->offset, m, f)); } else { - if (upb_encode_hasscalarfield(msg, m, f)) { - CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, !m->is_proto2)); + bool skip_empty = false; + if (f->presence == 0) { + /* Proto3 presence. */ + skip_empty = true; + } else if (f->presence > 0) { + /* Proto2 presence: hasbit. */ + if (!upb_readhasbit(msg, f)) { + continue; + } + } else { + /* Field is in a oneof. */ + if (upb_readcase(msg, f) != f->number) { + continue; + } } + CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty)); } } - *size = buf_end - e->ptr; + unknown = upb_msg_getunknown(msg, &unknown_size); + + if (unknown) { + upb_put_bytes(e, unknown, unknown_size); + } + + *size = (e->limit - e->ptr) - pre_len; return true; } -char *upb_encode(const void *msg, const upb_msglayout_msginit_v1 *m, - upb_env *env, size_t *size) { +char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena, + size_t *size) { upb_encstate e; - e.env = env; + e.alloc = upb_arena_alloc(arena); e.buf = NULL; e.limit = NULL; e.ptr = NULL; @@ -4033,28 +4610,107 @@ bool upb_byteshandler_setendstr(upb_byteshandler *h, } -static bool is_power_of_two(size_t val) { - return (val & (val - 1)) == 0; -} +/** Handlers for upb_msg ******************************************************/ -/* Align up to the given power of 2. */ -static size_t align_up(size_t val, size_t align) { - UPB_ASSERT(is_power_of_two(align)); - return (val + align - 1) & ~(align - 1); -} +typedef struct { + size_t offset; + int32_t hasbit; +} upb_msg_handlerdata; -static size_t div_round_up(size_t n, size_t d) { - return (n + d - 1) / d; -} +/* Fallback implementation if the handler is not specialized by the producer. */ +#define MSG_WRITER(type, ctype) \ + bool upb_msg_set ## type (void *c, const void *hd, ctype val) { \ + uint8_t *m = c; \ + const upb_msg_handlerdata *d = hd; \ + if (d->hasbit > 0) \ + *(uint8_t*)&m[d->hasbit / 8] |= 1 << (d->hasbit % 8); \ + *(ctype*)&m[d->offset] = val; \ + return true; \ + } \ -bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) { - return type == UPB_TYPE_BOOL || type == UPB_TYPE_INT32 || - type == UPB_TYPE_UINT32 || type == UPB_TYPE_INT64 || - type == UPB_TYPE_UINT64 || type == UPB_TYPE_STRING; -} +MSG_WRITER(double, double) +MSG_WRITER(float, float) +MSG_WRITER(int32, int32_t) +MSG_WRITER(int64, int64_t) +MSG_WRITER(uint32, uint32_t) +MSG_WRITER(uint64, uint64_t) +MSG_WRITER(bool, bool) -void *upb_array_pack(const upb_array *arr, void *p, size_t *ofs, size_t size); -void *upb_map_pack(const upb_map *map, void *p, size_t *ofs, size_t size); +bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f, + size_t offset, int32_t hasbit) { + upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; + bool ok; + + upb_msg_handlerdata *d = upb_gmalloc(sizeof(*d)); + if (!d) return false; + d->offset = offset; + d->hasbit = hasbit; + + upb_handlerattr_sethandlerdata(&attr, d); + upb_handlerattr_setalwaysok(&attr, true); + upb_handlers_addcleanup(h, d, upb_gfree); + +#define TYPE(u, l) \ + case UPB_TYPE_##u: \ + ok = upb_handlers_set##l(h, f, upb_msg_set##l, &attr); break; + + ok = false; + + switch (upb_fielddef_type(f)) { + TYPE(INT64, int64); + TYPE(INT32, int32); + TYPE(ENUM, int32); + TYPE(UINT64, uint64); + TYPE(UINT32, uint32); + TYPE(DOUBLE, double); + TYPE(FLOAT, float); + TYPE(BOOL, bool); + default: UPB_ASSERT(false); break; + } +#undef TYPE + + upb_handlerattr_uninit(&attr); + return ok; +} + +bool upb_msg_getscalarhandlerdata(const upb_handlers *h, + upb_selector_t s, + upb_fieldtype_t *type, + size_t *offset, + int32_t *hasbit) { + const upb_msg_handlerdata *d; + upb_func *f = upb_handlers_gethandler(h, s); + + if ((upb_int64_handlerfunc*)f == upb_msg_setint64) { + *type = UPB_TYPE_INT64; + } else if ((upb_int32_handlerfunc*)f == upb_msg_setint32) { + *type = UPB_TYPE_INT32; + } else if ((upb_uint64_handlerfunc*)f == upb_msg_setuint64) { + *type = UPB_TYPE_UINT64; + } else if ((upb_uint32_handlerfunc*)f == upb_msg_setuint32) { + *type = UPB_TYPE_UINT32; + } else if ((upb_double_handlerfunc*)f == upb_msg_setdouble) { + *type = UPB_TYPE_DOUBLE; + } else if ((upb_float_handlerfunc*)f == upb_msg_setfloat) { + *type = UPB_TYPE_FLOAT; + } else if ((upb_bool_handlerfunc*)f == upb_msg_setbool) { + *type = UPB_TYPE_BOOL; + } else { + return false; + } + + d = upb_handlers_gethandlerdata(h, s); + *offset = d->offset; + *hasbit = d->hasbit; + return true; +} + + +bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) { + return type == UPB_TYPE_BOOL || type == UPB_TYPE_INT32 || + type == UPB_TYPE_UINT32 || type == UPB_TYPE_INT64 || + type == UPB_TYPE_UINT64 || type == UPB_TYPE_STRING; +} #define PTR_AT(msg, ofs, type) (type*)((char*)msg + ofs) #define VOIDPTR_AT(msg, ofs) PTR_AT(msg, ofs, void) @@ -4095,28 +4751,20 @@ static size_t upb_msgval_sizeof(upb_fieldtype_t type) { return 4; case UPB_TYPE_BOOL: return 1; - case UPB_TYPE_BYTES: case UPB_TYPE_MESSAGE: return sizeof(void*); + case UPB_TYPE_BYTES: case UPB_TYPE_STRING: - return sizeof(upb_stringview); + return sizeof(upb_strview); } UPB_UNREACHABLE(); } -static uint8_t upb_msg_fieldsize(const upb_msglayout_fieldinit_v1 *field) { +static uint8_t upb_msg_fieldsize(const upb_msglayout_field *field) { if (field->label == UPB_LABEL_REPEATED) { return sizeof(void*); } else { - return upb_msgval_sizeof(field->type); - } -} - -static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) { - if (upb_fielddef_isseq(f)) { - return sizeof(void*); - } else { - return upb_msgval_sizeof(upb_fielddef_type(f)); + return upb_msgval_sizeof(upb_desctype_to_fieldtype[field->descriptortype]); } } @@ -4155,1186 +4803,811 @@ static upb_ctype_t upb_fieldtotabtype(upb_fieldtype_t type) { } } -static upb_msgval upb_msgval_fromdefault(const upb_fielddef *f) { - switch (upb_fielddef_type(f)) { - case UPB_TYPE_FLOAT: - return upb_msgval_float(upb_fielddef_defaultfloat(f)); - case UPB_TYPE_DOUBLE: - return upb_msgval_double(upb_fielddef_defaultdouble(f)); - case UPB_TYPE_BOOL: - return upb_msgval_bool(upb_fielddef_defaultbool(f)); - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: { - size_t len; - const char *ptr = upb_fielddef_defaultstr(f, &len); - return upb_msgval_makestr(ptr, len); - } - case UPB_TYPE_MESSAGE: - return upb_msgval_msg(NULL); - case UPB_TYPE_ENUM: - case UPB_TYPE_INT32: - return upb_msgval_int32(upb_fielddef_defaultint32(f)); - case UPB_TYPE_UINT32: - return upb_msgval_uint32(upb_fielddef_defaultuint32(f)); - case UPB_TYPE_INT64: - return upb_msgval_int64(upb_fielddef_defaultint64(f)); - case UPB_TYPE_UINT64: - return upb_msgval_uint64(upb_fielddef_defaultuint64(f)); - default: - UPB_ASSERT(false); - return upb_msgval_msg(NULL); - } -} +/** upb_msg *******************************************************************/ -/** upb_msglayout *************************************************************/ +/* If we always read/write as a consistent type to each address, this shouldn't + * violate aliasing. + */ +#define DEREF(msg, ofs, type) *PTR_AT(msg, ofs, type) -struct upb_msglayout { - struct upb_msglayout_msginit_v1 data; -}; +/* Internal members of a upb_msg. We can change this without breaking binary + * compatibility. We put these before the user's data. The user's upb_msg* + * points after the upb_msg_internal. */ -static void upb_msglayout_free(upb_msglayout *l) { - upb_gfree(l->data.default_msg); - upb_gfree(l); -} +/* Used when a message is not extendable. */ +typedef struct { + /* TODO(haberman): use pointer tagging so we we are slim when known unknown + * fields are not present. */ + upb_arena *arena; + char *unknown; + size_t unknown_len; + size_t unknown_size; +} upb_msg_internal; -static size_t upb_msglayout_place(upb_msglayout *l, size_t size) { - size_t ret; +/* Used when a message is extendable. */ +typedef struct { + upb_inttable *extdict; + upb_msg_internal base; +} upb_msg_internal_withext; - l->data.size = align_up(l->data.size, size); - ret = l->data.size; - l->data.size += size; - return ret; +static int upb_msg_internalsize(const upb_msglayout *l) { + return sizeof(upb_msg_internal) - l->extendable * sizeof(void *); } -static uint32_t upb_msglayout_offset(const upb_msglayout *l, - const upb_fielddef *f) { - return l->data.fields[upb_fielddef_index(f)].offset; +static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) { + return VOIDPTR_AT(msg, -sizeof(upb_msg_internal)); } -static uint32_t upb_msglayout_hasbit(const upb_msglayout *l, - const upb_fielddef *f) { - return l->data.fields[upb_fielddef_index(f)].hasbit; +static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) { + return VOIDPTR_AT(msg, -sizeof(upb_msg_internal)); } -static bool upb_msglayout_initdefault(upb_msglayout *l, const upb_msgdef *m) { - upb_msg_field_iter it; +static upb_msg_internal_withext *upb_msg_getinternalwithext( + upb_msg *msg, const upb_msglayout *l) { + UPB_ASSERT(l->extendable); + return VOIDPTR_AT(msg, -sizeof(upb_msg_internal_withext)); +} - if (upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2 && l->data.size) { - /* Allocate default message and set default values in it. */ - l->data.default_msg = upb_gmalloc(l->data.size); - if (!l->data.default_msg) { - return false; - } +void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len) { + upb_msg_internal* in = upb_msg_getinternal(msg); + if (len > in->unknown_size - in->unknown_len) { + upb_alloc *alloc = upb_arena_alloc(in->arena); + size_t need = in->unknown_size + len; + size_t newsize = UPB_MAX(in->unknown_size * 2, need); + in->unknown = upb_realloc(alloc, in->unknown, in->unknown_size, newsize); + in->unknown_size = newsize; + } + memcpy(in->unknown + in->unknown_len, data, len); + in->unknown_len += len; +} - memset(l->data.default_msg, 0, l->data.size); +const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) { + const upb_msg_internal* in = upb_msg_getinternal_const(msg); + *len = in->unknown_len; + return in->unknown; +} - for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it); - upb_msg_field_next(&it)) { - const upb_fielddef* f = upb_msg_iter_field(&it); +static const upb_msglayout_field *upb_msg_checkfield(int field_index, + const upb_msglayout *l) { + UPB_ASSERT(field_index >= 0 && field_index < l->field_count); + return &l->fields[field_index]; +} - if (upb_fielddef_containingoneof(f)) { - continue; - } +static bool upb_msg_inoneof(const upb_msglayout_field *field) { + return field->presence < 0; +} - /* TODO(haberman): handle strings. */ - if (!upb_fielddef_isstring(f) && - !upb_fielddef_issubmsg(f) && - !upb_fielddef_isseq(f)) { - upb_msg_set(l->data.default_msg, - upb_fielddef_index(f), - upb_msgval_fromdefault(f), - l); - } - } - } +static uint32_t *upb_msg_oneofcase(const upb_msg *msg, int field_index, + const upb_msglayout *l) { + const upb_msglayout_field *field = upb_msg_checkfield(field_index, l); + UPB_ASSERT(upb_msg_inoneof(field)); + return PTR_AT(msg, ~field->presence, uint32_t); +} - return true; +static size_t upb_msg_sizeof(const upb_msglayout *l) { + return l->size + upb_msg_internalsize(l); } -static upb_msglayout *upb_msglayout_new(const upb_msgdef *m) { - upb_msg_field_iter it; - upb_msg_oneof_iter oit; - upb_msglayout *l; - size_t hasbit; - size_t submsg_count = 0; - const upb_msglayout_msginit_v1 **submsgs; - upb_msglayout_fieldinit_v1 *fields; - upb_msglayout_oneofinit_v1 *oneofs; +upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a) { + upb_alloc *alloc = upb_arena_alloc(a); + void *mem = upb_malloc(alloc, upb_msg_sizeof(l)); + upb_msg_internal *in; + upb_msg *msg; - for (upb_msg_field_begin(&it, m); - !upb_msg_field_done(&it); - upb_msg_field_next(&it)) { - const upb_fielddef* f = upb_msg_iter_field(&it); - if (upb_fielddef_issubmsg(f)) { - submsg_count++; - } + if (!mem) { + return NULL; } - l = upb_gmalloc(sizeof(*l)); - if (!l) return NULL; + msg = VOIDPTR_AT(mem, upb_msg_internalsize(l)); - memset(l, 0, sizeof(*l)); + /* Initialize normal members. */ + memset(msg, 0, l->size); - fields = upb_gmalloc(upb_msgdef_numfields(m) * sizeof(*fields)); - submsgs = upb_gmalloc(submsg_count * sizeof(*submsgs)); - oneofs = upb_gmalloc(upb_msgdef_numoneofs(m) * sizeof(*oneofs)); + /* Initialize internal members. */ + in = upb_msg_getinternal(msg); + in->arena = a; + in->unknown = NULL; + in->unknown_len = 0; + in->unknown_size = 0; - if ((!fields && upb_msgdef_numfields(m)) || - (!submsgs && submsg_count) || - (!oneofs && upb_msgdef_numoneofs(m))) { - /* OOM. */ - upb_gfree(l); - upb_gfree(fields); - upb_gfree(submsgs); - upb_gfree(oneofs); - return NULL; + if (l->extendable) { + upb_msg_getinternalwithext(msg, l)->extdict = NULL; } - l->data.field_count = upb_msgdef_numfields(m); - l->data.oneof_count = upb_msgdef_numoneofs(m); - l->data.fields = fields; - l->data.submsgs = submsgs; - l->data.oneofs = oneofs; - l->data.is_proto2 = (upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2); - - /* Allocate data offsets in three stages: - * - * 1. hasbits. - * 2. regular fields. - * 3. oneof fields. - * - * OPT: There is a lot of room for optimization here to minimize the size. - */ + return msg; +} - /* Allocate hasbits and set basic field attributes. */ - for (upb_msg_field_begin(&it, m), hasbit = 0; - !upb_msg_field_done(&it); - upb_msg_field_next(&it)) { - const upb_fielddef* f = upb_msg_iter_field(&it); - upb_msglayout_fieldinit_v1 *field = &fields[upb_fielddef_index(f)]; +upb_arena *upb_msg_arena(const upb_msg *msg) { + return upb_msg_getinternal_const(msg)->arena; +} - field->number = upb_fielddef_number(f); - field->type = upb_fielddef_type(f); - field->label = upb_fielddef_label(f); +bool upb_msg_has(const upb_msg *msg, + int field_index, + const upb_msglayout *l) { + const upb_msglayout_field *field = upb_msg_checkfield(field_index, l); - if (upb_fielddef_containingoneof(f)) { - field->oneof_index = upb_oneofdef_index(upb_fielddef_containingoneof(f)); - } else { - field->oneof_index = UPB_NOT_IN_ONEOF; - } + UPB_ASSERT(field->presence); - if (upb_fielddef_haspresence(f) && !upb_fielddef_containingoneof(f)) { - field->hasbit = hasbit++; - } + if (upb_msg_inoneof(field)) { + /* Oneofs are set when the oneof number is set to this field. */ + return *upb_msg_oneofcase(msg, field_index, l) == field->number; + } else { + /* Other fields are set when their hasbit is set. */ + uint32_t hasbit = field->presence; + return DEREF(msg, hasbit / 8, char) | (1 << (hasbit % 8)); } +} - /* Account for space used by hasbits. */ - l->data.size = div_round_up(hasbit, 8); - - /* Allocate non-oneof fields. */ - for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it); - upb_msg_field_next(&it)) { - const upb_fielddef* f = upb_msg_iter_field(&it); - size_t field_size = upb_msg_fielddefsize(f); - size_t index = upb_fielddef_index(f); +upb_msgval upb_msg_get(const upb_msg *msg, int field_index, + const upb_msglayout *l) { + const upb_msglayout_field *field = upb_msg_checkfield(field_index, l); + int size = upb_msg_fieldsize(field); + return upb_msgval_read(msg, field->offset, size); +} - if (upb_fielddef_containingoneof(f)) { - /* Oneofs are handled separately below. */ - continue; - } +void upb_msg_set(upb_msg *msg, int field_index, upb_msgval val, + const upb_msglayout *l) { + const upb_msglayout_field *field = upb_msg_checkfield(field_index, l); + int size = upb_msg_fieldsize(field); + upb_msgval_write(msg, field->offset, val, size); +} - fields[index].offset = upb_msglayout_place(l, field_size); - } - /* Allocate oneof fields. Each oneof field consists of a uint32 for the case - * and space for the actual data. */ - for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit); - upb_msg_oneof_next(&oit)) { - const upb_oneofdef* o = upb_msg_iter_oneof(&oit); - upb_oneof_iter fit; +/** upb_array *****************************************************************/ - size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */ - upb_msglayout_oneofinit_v1 *oneof = &oneofs[upb_oneofdef_index(o)]; - size_t field_size = 0; +#define DEREF_ARR(arr, i, type) ((type*)arr->data)[i] - /* Calculate field size: the max of all field sizes. */ - for (upb_oneof_begin(&fit, o); - !upb_oneof_done(&fit); - upb_oneof_next(&fit)) { - const upb_fielddef* f = upb_oneof_iter_field(&fit); - field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f)); - } +upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a) { + upb_alloc *alloc = upb_arena_alloc(a); + upb_array *ret = upb_malloc(alloc, sizeof(upb_array)); - /* Align and allocate case offset. */ - oneof->case_offset = upb_msglayout_place(l, case_size); - oneof->data_offset = upb_msglayout_place(l, field_size); + if (!ret) { + return NULL; } - /* Size of the entire structure should be a multiple of its greatest - * alignment. TODO: track overall alignment for real? */ - l->data.size = align_up(l->data.size, 8); + ret->type = type; + ret->data = NULL; + ret->len = 0; + ret->size = 0; + ret->element_size = upb_msgval_sizeof(type); + ret->arena = a; - if (upb_msglayout_initdefault(l, m)) { - return l; - } else { - upb_msglayout_free(l); - return NULL; - } + return ret; } +size_t upb_array_size(const upb_array *arr) { + return arr->len; +} -/** upb_msgfactory ************************************************************/ - -struct upb_msgfactory { - const upb_symtab *symtab; /* We own a ref. */ - upb_inttable layouts; - upb_inttable mergehandlers; -}; - -upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) { - upb_msgfactory *ret = upb_gmalloc(sizeof(*ret)); - - ret->symtab = symtab; - upb_inttable_init(&ret->layouts, UPB_CTYPE_PTR); - upb_inttable_init(&ret->mergehandlers, UPB_CTYPE_CONSTPTR); +upb_fieldtype_t upb_array_type(const upb_array *arr) { + return arr->type; +} - return ret; +upb_msgval upb_array_get(const upb_array *arr, size_t i) { + UPB_ASSERT(i < arr->len); + return upb_msgval_read(arr->data, i * arr->element_size, arr->element_size); } -void upb_msgfactory_free(upb_msgfactory *f) { - upb_inttable_iter i; - upb_inttable_begin(&i, &f->layouts); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_msglayout *l = upb_value_getptr(upb_inttable_iter_value(&i)); - upb_msglayout_free(l); - } +bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) { + UPB_ASSERT(i <= arr->len); - upb_inttable_begin(&i, &f->mergehandlers); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - const upb_handlers *h = upb_value_getconstptr(upb_inttable_iter_value(&i)); - upb_handlers_unref(h, f); - } + if (i == arr->len) { + /* Extending the array. */ - upb_inttable_uninit(&f->layouts); - upb_inttable_uninit(&f->mergehandlers); - upb_gfree(f); -} + if (i == arr->size) { + /* Need to reallocate. */ + size_t new_size = UPB_MAX(arr->size * 2, 8); + size_t new_bytes = new_size * arr->element_size; + size_t old_bytes = arr->size * arr->element_size; + upb_alloc *alloc = upb_arena_alloc(arr->arena); + upb_msgval *new_data = + upb_realloc(alloc, arr->data, old_bytes, new_bytes); -const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f) { - return f->symtab; -} + if (!new_data) { + return false; + } -const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, - const upb_msgdef *m) { - upb_value v; - UPB_ASSERT(upb_symtab_lookupmsg(f->symtab, upb_msgdef_fullname(m)) == m); - UPB_ASSERT(!upb_msgdef_mapentry(m)); + arr->data = new_data; + arr->size = new_size; + } - if (upb_inttable_lookupptr(&f->layouts, m, &v)) { - UPB_ASSERT(upb_value_getptr(v)); - return upb_value_getptr(v); - } else { - upb_msgfactory *mutable_f = (void*)f; - upb_msglayout *l = upb_msglayout_new(m); - upb_inttable_insertptr(&mutable_f->layouts, m, upb_value_ptr(l)); - UPB_ASSERT(l); - return l; + arr->len = i + 1; } + + upb_msgval_write(arr->data, i * arr->element_size, val, arr->element_size); + return true; } -/* Our handlers that we don't expose externally. */ -void *upb_msg_startstr(void *msg, const void *hd, size_t size_hint) { - uint32_t ofs = (uintptr_t)hd; - upb_alloc *alloc = upb_msg_alloc(msg); - upb_msgval val; - UPB_UNUSED(size_hint); +/** upb_map *******************************************************************/ - val = upb_msgval_read(msg, ofs, upb_msgval_sizeof(UPB_TYPE_STRING)); +struct upb_map { + upb_fieldtype_t key_type; + upb_fieldtype_t val_type; + /* We may want to optimize this to use inttable where possible, for greater + * efficiency and lower memory footprint. */ + upb_strtable strtab; + upb_arena *arena; +}; - upb_free(alloc, (void*)val.str.data); - val.str.data = NULL; - val.str.size = 0; +static void upb_map_tokey(upb_fieldtype_t type, upb_msgval *key, + const char **out_key, size_t *out_len) { + switch (type) { + case UPB_TYPE_STRING: + /* Point to string data of the input key. */ + *out_key = key->str.data; + *out_len = key->str.size; + return; + case UPB_TYPE_BOOL: + case UPB_TYPE_INT32: + case UPB_TYPE_UINT32: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT64: + /* Point to the key itself. XXX: big-endian. */ + *out_key = (const char*)key; + *out_len = upb_msgval_sizeof(type); + return; + case UPB_TYPE_BYTES: + case UPB_TYPE_DOUBLE: + case UPB_TYPE_ENUM: + case UPB_TYPE_FLOAT: + case UPB_TYPE_MESSAGE: + break; /* Cannot be a map key. */ + } + UPB_UNREACHABLE(); +} - upb_msgval_write(msg, ofs, val, upb_msgval_sizeof(UPB_TYPE_STRING)); - return msg; +static upb_msgval upb_map_fromkey(upb_fieldtype_t type, const char *key, + size_t len) { + switch (type) { + case UPB_TYPE_STRING: + return upb_msgval_makestr(key, len); + case UPB_TYPE_BOOL: + case UPB_TYPE_INT32: + case UPB_TYPE_UINT32: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT64: + return upb_msgval_read(key, 0, upb_msgval_sizeof(type)); + case UPB_TYPE_BYTES: + case UPB_TYPE_DOUBLE: + case UPB_TYPE_ENUM: + case UPB_TYPE_FLOAT: + case UPB_TYPE_MESSAGE: + break; /* Cannot be a map key. */ + } + UPB_UNREACHABLE(); } -size_t upb_msg_str(void *msg, const void *hd, const char *ptr, size_t size, - const upb_bufhandle *handle) { - uint32_t ofs = (uintptr_t)hd; - upb_alloc *alloc = upb_msg_alloc(msg); - upb_msgval val; - size_t newsize; - UPB_UNUSED(handle); +upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype, + upb_arena *a) { + upb_ctype_t vtabtype = upb_fieldtotabtype(vtype); + upb_alloc *alloc = upb_arena_alloc(a); + upb_map *map = upb_malloc(alloc, sizeof(upb_map)); - val = upb_msgval_read(msg, ofs, upb_msgval_sizeof(UPB_TYPE_STRING)); + if (!map) { + return NULL; + } - newsize = val.str.size + size; - val.str.data = upb_realloc(alloc, (void*)val.str.data, val.str.size, newsize); + UPB_ASSERT(upb_fieldtype_mapkeyok(ktype)); + map->key_type = ktype; + map->val_type = vtype; + map->arena = a; - if (!val.str.data) { - return false; + if (!upb_strtable_init2(&map->strtab, vtabtype, alloc)) { + return NULL; } - memcpy((char*)val.str.data + val.str.size, ptr, size); - val.str.size = newsize; - upb_msgval_write(msg, ofs, val, upb_msgval_sizeof(UPB_TYPE_STRING)); - return size; + return map; } -static void callback(const void *closure, upb_handlers *h) { - upb_msgfactory *factory = (upb_msgfactory*)closure; - const upb_msgdef *md = upb_handlers_msgdef(h); - const upb_msglayout* layout = upb_msgfactory_getlayout(factory, md); - upb_msg_field_iter i; - UPB_UNUSED(factory); +size_t upb_map_size(const upb_map *map) { + return upb_strtable_count(&map->strtab); +} - for(upb_msg_field_begin(&i, md); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - const upb_fielddef *f = upb_msg_iter_field(&i); - size_t offset = upb_msglayout_offset(layout, f); - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, (void*)offset); +upb_fieldtype_t upb_map_keytype(const upb_map *map) { + return map->key_type; +} - if (upb_fielddef_isseq(f)) { - } else if (upb_fielddef_isstring(f)) { - upb_handlers_setstartstr(h, f, upb_msg_startstr, &attr); - upb_handlers_setstring(h, f, upb_msg_str, &attr); - } else { - upb_msg_setscalarhandler( - h, f, offset, upb_msglayout_hasbit(layout, f)); - } - } +upb_fieldtype_t upb_map_valuetype(const upb_map *map) { + return map->val_type; } -const upb_handlers *upb_msgfactory_getmergehandlers(upb_msgfactory *f, - const upb_msgdef *m) { - upb_msgfactory *mutable_f = (void*)f; +bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) { + upb_value tabval; + const char *key_str; + size_t key_len; + bool ret; - /* TODO(haberman): properly cache these. */ - const upb_handlers *ret = upb_handlers_newfrozen(m, f, callback, f); - upb_inttable_push(&mutable_f->mergehandlers, upb_value_constptr(ret)); + upb_map_tokey(map->key_type, &key, &key_str, &key_len); + ret = upb_strtable_lookup2(&map->strtab, key_str, key_len, &tabval); + if (ret) { + memcpy(val, &tabval, sizeof(tabval)); + } return ret; } -const upb_visitorplan *upb_msgfactory_getvisitorplan(upb_msgfactory *f, - const upb_handlers *h) { - const upb_msgdef *md = upb_handlers_msgdef(h); - return (const upb_visitorplan*)upb_msgfactory_getlayout(f, md); -} - +bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val, + upb_msgval *removed) { + const char *key_str; + size_t key_len; + upb_value tabval = upb_toval(val); + upb_value removedtabval; + upb_alloc *a = upb_arena_alloc(map->arena); -/** upb_visitor ***************************************************************/ + upb_map_tokey(map->key_type, &key, &key_str, &key_len); -struct upb_visitor { - const upb_msglayout *layout; - upb_sink *sink; -}; + /* TODO(haberman): add overwrite operation to minimize number of lookups. */ + if (upb_strtable_lookup2(&map->strtab, key_str, key_len, NULL)) { + upb_strtable_remove3(&map->strtab, key_str, key_len, &removedtabval, a); + memcpy(&removed, &removedtabval, sizeof(removed)); + } -static upb_selector_t getsel2(const upb_fielddef *f, upb_handlertype_t type) { - upb_selector_t ret; - bool ok = upb_handlers_getselector(f, type, &ret); - UPB_ASSERT(ok); - return ret; + return upb_strtable_insert3(&map->strtab, key_str, key_len, tabval, a); } -static bool upb_visitor_hasfield(const upb_msg *msg, const upb_fielddef *f, - const upb_msglayout *layout) { - int field_index = upb_fielddef_index(f); - if (upb_fielddef_isseq(f)) { - return upb_msgval_getarr(upb_msg_get(msg, field_index, layout)) != NULL; - } else if (upb_msgdef_syntax(upb_fielddef_containingtype(f)) == - UPB_SYNTAX_PROTO2) { - return upb_msg_has(msg, field_index, layout); - } else { - upb_msgval val = upb_msg_get(msg, field_index, layout); - switch (upb_fielddef_type(f)) { - case UPB_TYPE_FLOAT: - return upb_msgval_getfloat(val) != 0; - case UPB_TYPE_DOUBLE: - return upb_msgval_getdouble(val) != 0; - case UPB_TYPE_BOOL: - return upb_msgval_getbool(val); - case UPB_TYPE_ENUM: - case UPB_TYPE_INT32: - return upb_msgval_getint32(val) != 0; - case UPB_TYPE_UINT32: - return upb_msgval_getuint32(val) != 0; - case UPB_TYPE_INT64: - return upb_msgval_getint64(val) != 0; - case UPB_TYPE_UINT64: - return upb_msgval_getuint64(val) != 0; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - return upb_msgval_getstr(val).size > 0; - case UPB_TYPE_MESSAGE: - return upb_msgval_getmsg(val) != NULL; - } - UPB_UNREACHABLE(); - } +bool upb_map_del(upb_map *map, upb_msgval key) { + const char *key_str; + size_t key_len; + upb_alloc *a = upb_arena_alloc(map->arena); + + upb_map_tokey(map->key_type, &key, &key_str, &key_len); + return upb_strtable_remove3(&map->strtab, key_str, key_len, NULL, a); } -static bool upb_visitor_visitmsg2(const upb_msg *msg, - const upb_msglayout *layout, upb_sink *sink, - int depth) { - const upb_msgdef *md = upb_handlers_msgdef(sink->handlers); - upb_msg_field_iter i; - upb_status status; - upb_sink_startmsg(sink); +/** upb_mapiter ***************************************************************/ - /* Protect against cycles (possible because users may freely reassign message - * and repeated fields) by imposing a maximum recursion depth. */ - if (depth > ENCODE_MAX_NESTING) { - return false; - } +struct upb_mapiter { + upb_strtable_iter iter; + upb_fieldtype_t key_type; +}; - for (upb_msg_field_begin(&i, md); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - upb_msgval val; +size_t upb_mapiter_sizeof() { + return sizeof(upb_mapiter); +} - if (!upb_visitor_hasfield(msg, f, layout)) { - continue; - } +void upb_mapiter_begin(upb_mapiter *i, const upb_map *map) { + upb_strtable_begin(&i->iter, &map->strtab); + i->key_type = map->key_type; +} - val = upb_msg_get(msg, upb_fielddef_index(f), layout); +upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a) { + upb_mapiter *ret = upb_malloc(a, upb_mapiter_sizeof()); - if (upb_fielddef_isseq(f)) { - const upb_array *arr = upb_msgval_getarr(val); - UPB_ASSERT(arr); - /* TODO: putary(ary, f, sink, depth);*/ - } else if (upb_fielddef_issubmsg(f)) { - const upb_map *map = upb_msgval_getmap(val); - UPB_ASSERT(map); - /* TODO: putmap(map, f, sink, depth);*/ - } else if (upb_fielddef_isstring(f)) { - /* TODO putstr(); */ - } else { - upb_selector_t sel = getsel2(f, upb_handlers_getprimitivehandlertype(f)); - UPB_ASSERT(upb_fielddef_isprimitive(f)); - - switch (upb_fielddef_type(f)) { - case UPB_TYPE_FLOAT: - CHECK_TRUE(upb_sink_putfloat(sink, sel, upb_msgval_getfloat(val))); - break; - case UPB_TYPE_DOUBLE: - CHECK_TRUE(upb_sink_putdouble(sink, sel, upb_msgval_getdouble(val))); - break; - case UPB_TYPE_BOOL: - CHECK_TRUE(upb_sink_putbool(sink, sel, upb_msgval_getbool(val))); - break; - case UPB_TYPE_ENUM: - case UPB_TYPE_INT32: - CHECK_TRUE(upb_sink_putint32(sink, sel, upb_msgval_getint32(val))); - break; - case UPB_TYPE_UINT32: - CHECK_TRUE(upb_sink_putuint32(sink, sel, upb_msgval_getuint32(val))); - break; - case UPB_TYPE_INT64: - CHECK_TRUE(upb_sink_putint64(sink, sel, upb_msgval_getint64(val))); - break; - case UPB_TYPE_UINT64: - CHECK_TRUE(upb_sink_putuint64(sink, sel, upb_msgval_getuint64(val))); - break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - case UPB_TYPE_MESSAGE: - UPB_UNREACHABLE(); - } - } + if (!ret) { + return NULL; } - upb_sink_endmsg(sink, &status); - return true; + upb_mapiter_begin(ret, t); + return ret; } -upb_visitor *upb_visitor_create(upb_env *e, const upb_visitorplan *vp, - upb_sink *output) { - upb_visitor *visitor = upb_env_malloc(e, sizeof(*visitor)); - visitor->layout = (const upb_msglayout*)vp; - visitor->sink = output; - return visitor; +void upb_mapiter_free(upb_mapiter *i, upb_alloc *a) { + upb_free(a, i); } -bool upb_visitor_visitmsg(upb_visitor *visitor, const upb_msg *msg) { - return upb_visitor_visitmsg2(msg, visitor->layout, visitor->sink, 0); +void upb_mapiter_next(upb_mapiter *i) { + upb_strtable_next(&i->iter); } +bool upb_mapiter_done(const upb_mapiter *i) { + return upb_strtable_done(&i->iter); +} -/** upb_msg *******************************************************************/ - -/* If we always read/write as a consistent type to each address, this shouldn't - * violate aliasing. - */ -#define DEREF(msg, ofs, type) *PTR_AT(msg, ofs, type) - -/* Internal members of a upb_msg. We can change this without breaking binary - * compatibility. We put these before the user's data. The user's upb_msg* - * points after the upb_msg_internal. */ - -/* Used when a message is not extendable. */ -typedef struct { - /* TODO(haberman): add unknown fields. */ - upb_alloc *alloc; -} upb_msg_internal; - -/* Used when a message is extendable. */ -typedef struct { - upb_inttable *extdict; - upb_msg_internal base; -} upb_msg_internal_withext; - -static int upb_msg_internalsize(const upb_msglayout *l) { - return sizeof(upb_msg_internal) - l->data.extendable * sizeof(void*); +upb_msgval upb_mapiter_key(const upb_mapiter *i) { + return upb_map_fromkey(i->key_type, upb_strtable_iter_key(&i->iter), + upb_strtable_iter_keylength(&i->iter)); } -static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) { - return VOIDPTR_AT(msg, -sizeof(upb_msg_internal)); +upb_msgval upb_mapiter_value(const upb_mapiter *i) { + return upb_msgval_fromval(upb_strtable_iter_value(&i->iter)); } -static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) { - return VOIDPTR_AT(msg, -sizeof(upb_msg_internal)); +void upb_mapiter_setdone(upb_mapiter *i) { + upb_strtable_iter_setdone(&i->iter); } -static upb_msg_internal_withext *upb_msg_getinternalwithext( - upb_msg *msg, const upb_msglayout *l) { - UPB_ASSERT(l->data.extendable); - return VOIDPTR_AT(msg, -sizeof(upb_msg_internal_withext)); +bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2) { + return upb_strtable_iter_isequal(&i1->iter, &i2->iter); } -static const upb_msglayout_fieldinit_v1 *upb_msg_checkfield( - int field_index, const upb_msglayout *l) { - UPB_ASSERT(field_index >= 0 && field_index < l->data.field_count); - return &l->data.fields[field_index]; -} -static bool upb_msg_inoneof(const upb_msglayout_fieldinit_v1 *field) { - return field->oneof_index != UPB_NOT_IN_ONEOF; +static bool is_power_of_two(size_t val) { + return (val & (val - 1)) == 0; } -static uint32_t *upb_msg_oneofcase(const upb_msg *msg, int field_index, - const upb_msglayout *l) { - const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l); - UPB_ASSERT(upb_msg_inoneof(field)); - return PTR_AT(msg, l->data.oneofs[field->oneof_index].case_offset, uint32_t); +/* Align up to the given power of 2. */ +static size_t align_up(size_t val, size_t align) { + UPB_ASSERT(is_power_of_two(align)); + return (val + align - 1) & ~(align - 1); } -size_t upb_msg_sizeof(const upb_msglayout *l) { - return l->data.size + upb_msg_internalsize(l); +static size_t div_round_up(size_t n, size_t d) { + return (n + d - 1) / d; } -upb_msg *upb_msg_init(void *mem, const upb_msglayout *l, upb_alloc *a) { - upb_msg *msg = VOIDPTR_AT(mem, upb_msg_internalsize(l)); +static size_t upb_msgval_sizeof2(upb_fieldtype_t type) { + switch (type) { + case UPB_TYPE_DOUBLE: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT64: + return 8; + case UPB_TYPE_ENUM: + case UPB_TYPE_INT32: + case UPB_TYPE_UINT32: + case UPB_TYPE_FLOAT: + return 4; + case UPB_TYPE_BOOL: + return 1; + case UPB_TYPE_MESSAGE: + return sizeof(void*); + case UPB_TYPE_BYTES: + case UPB_TYPE_STRING: + return sizeof(upb_strview); + } + UPB_UNREACHABLE(); +} - /* Initialize normal members. */ - if (l->data.default_msg) { - memcpy(msg, l->data.default_msg, l->data.size); +static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) { + if (upb_fielddef_isseq(f)) { + return sizeof(void*); } else { - memset(msg, 0, l->data.size); + return upb_msgval_sizeof2(upb_fielddef_type(f)); } +} - /* Initialize internal members. */ - upb_msg_getinternal(msg)->alloc = a; - if (l->data.extendable) { - upb_msg_getinternalwithext(msg, l)->extdict = NULL; - } +/** upb_msglayout *************************************************************/ - return msg; +static void upb_msglayout_free(upb_msglayout *l) { + upb_gfree(l); +} + +static size_t upb_msglayout_place(upb_msglayout *l, size_t size) { + size_t ret; + + l->size = align_up(l->size, size); + ret = l->size; + l->size += size; + return ret; } -void *upb_msg_uninit(upb_msg *msg, const upb_msglayout *l) { - if (l->data.extendable) { - upb_inttable *ext_dict = upb_msg_getinternalwithext(msg, l)->extdict; - if (ext_dict) { - upb_inttable_uninit2(ext_dict, upb_msg_alloc(msg)); - upb_free(upb_msg_alloc(msg), ext_dict); +static bool upb_msglayout_init(const upb_msgdef *m, + upb_msglayout *l, + upb_msgfactory *factory) { + upb_msg_field_iter it; + upb_msg_oneof_iter oit; + size_t hasbit; + size_t submsg_count = 0; + const upb_msglayout **submsgs; + upb_msglayout_field *fields; + + for (upb_msg_field_begin(&it, m); + !upb_msg_field_done(&it); + upb_msg_field_next(&it)) { + const upb_fielddef* f = upb_msg_iter_field(&it); + if (upb_fielddef_issubmsg(f)) { + submsg_count++; } } - return VOIDPTR_AT(msg, -upb_msg_internalsize(l)); -} + memset(l, 0, sizeof(*l)); -upb_msg *upb_msg_new(const upb_msglayout *l, upb_alloc *a) { - void *mem = upb_malloc(a, upb_msg_sizeof(l)); - return mem ? upb_msg_init(mem, l, a) : NULL; -} + fields = upb_gmalloc(upb_msgdef_numfields(m) * sizeof(*fields)); + submsgs = upb_gmalloc(submsg_count * sizeof(*submsgs)); -void upb_msg_free(upb_msg *msg, const upb_msglayout *l) { - upb_free(upb_msg_alloc(msg), upb_msg_uninit(msg, l)); -} + if ((!fields && upb_msgdef_numfields(m)) || + (!submsgs && submsg_count)) { + /* OOM. */ + upb_gfree(fields); + upb_gfree(submsgs); + return false; + } -upb_alloc *upb_msg_alloc(const upb_msg *msg) { - return upb_msg_getinternal_const(msg)->alloc; -} + l->field_count = upb_msgdef_numfields(m); + l->fields = fields; + l->submsgs = submsgs; -bool upb_msg_has(const upb_msg *msg, - int field_index, - const upb_msglayout *l) { - const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l); + /* Allocate data offsets in three stages: + * + * 1. hasbits. + * 2. regular fields. + * 3. oneof fields. + * + * OPT: There is a lot of room for optimization here to minimize the size. + */ - UPB_ASSERT(l->data.is_proto2); + /* Allocate hasbits and set basic field attributes. */ + submsg_count = 0; + for (upb_msg_field_begin(&it, m), hasbit = 0; + !upb_msg_field_done(&it); + upb_msg_field_next(&it)) { + const upb_fielddef* f = upb_msg_iter_field(&it); + upb_msglayout_field *field = &fields[upb_fielddef_index(f)]; - if (upb_msg_inoneof(field)) { - /* Oneofs are set when the oneof number is set to this field. */ - return *upb_msg_oneofcase(msg, field_index, l) == field->number; - } else { - /* Other fields are set when their hasbit is set. */ - uint32_t hasbit = l->data.fields[field_index].hasbit; - return DEREF(msg, hasbit / 8, char) | (1 << (hasbit % 8)); - } -} + field->number = upb_fielddef_number(f); + field->descriptortype = upb_fielddef_descriptortype(f); + field->label = upb_fielddef_label(f); -upb_msgval upb_msg_get(const upb_msg *msg, int field_index, - const upb_msglayout *l) { - const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l); - int size = upb_msg_fieldsize(field); + if (upb_fielddef_issubmsg(f)) { + const upb_msglayout *sub_layout = + upb_msgfactory_getlayout(factory, upb_fielddef_msgsubdef(f)); + field->submsg_index = submsg_count++; + submsgs[field->submsg_index] = sub_layout; + } - if (upb_msg_inoneof(field)) { - if (*upb_msg_oneofcase(msg, field_index, l) == field->number) { - size_t ofs = l->data.oneofs[field->oneof_index].data_offset; - return upb_msgval_read(msg, ofs, size); + if (upb_fielddef_haspresence(f) && !upb_fielddef_containingoneof(f)) { + field->presence = (hasbit++); } else { - /* Return default. */ - return upb_msgval_read(l->data.default_msg, field->offset, size); + field->presence = 0; } - } else { - return upb_msgval_read(msg, field->offset, size); } -} - -void upb_msg_set(upb_msg *msg, int field_index, upb_msgval val, - const upb_msglayout *l) { - const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l); - int size = upb_msg_fieldsize(field); - if (upb_msg_inoneof(field)) { - size_t ofs = l->data.oneofs[field->oneof_index].data_offset; - *upb_msg_oneofcase(msg, field_index, l) = field->number; - upb_msgval_write(msg, ofs, val, size); - } else { - upb_msgval_write(msg, field->offset, val, size); - } -} + /* Account for space used by hasbits. */ + l->size = div_round_up(hasbit, 8); + /* Allocate non-oneof fields. */ + for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it); + upb_msg_field_next(&it)) { + const upb_fielddef* f = upb_msg_iter_field(&it); + size_t field_size = upb_msg_fielddefsize(f); + size_t index = upb_fielddef_index(f); -/** upb_array *****************************************************************/ + if (upb_fielddef_containingoneof(f)) { + /* Oneofs are handled separately below. */ + continue; + } -#define DEREF_ARR(arr, i, type) ((type*)arr->data)[i] + fields[index].offset = upb_msglayout_place(l, field_size); + } -size_t upb_array_sizeof(upb_fieldtype_t type) { - UPB_UNUSED(type); - return sizeof(upb_array); -} + /* Allocate oneof fields. Each oneof field consists of a uint32 for the case + * and space for the actual data. */ + for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit); + upb_msg_oneof_next(&oit)) { + const upb_oneofdef* o = upb_msg_iter_oneof(&oit); + upb_oneof_iter fit; -void upb_array_init(upb_array *arr, upb_fieldtype_t type, upb_alloc *alloc) { - arr->type = type; - arr->data = NULL; - arr->len = 0; - arr->size = 0; - arr->element_size = upb_msgval_sizeof(type); - arr->alloc = alloc; -} + size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */ + size_t field_size = 0; + uint32_t case_offset; + uint32_t data_offset; -void upb_array_uninit(upb_array *arr) { - upb_free(arr->alloc, arr->data); -} + /* Calculate field size: the max of all field sizes. */ + for (upb_oneof_begin(&fit, o); + !upb_oneof_done(&fit); + upb_oneof_next(&fit)) { + const upb_fielddef* f = upb_oneof_iter_field(&fit); + field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f)); + } -upb_array *upb_array_new(upb_fieldtype_t type, upb_alloc *a) { - upb_array *ret = upb_malloc(a, upb_array_sizeof(type)); + /* Align and allocate case offset. */ + case_offset = upb_msglayout_place(l, case_size); + data_offset = upb_msglayout_place(l, field_size); - if (ret) { - upb_array_init(ret, type, a); + for (upb_oneof_begin(&fit, o); + !upb_oneof_done(&fit); + upb_oneof_next(&fit)) { + const upb_fielddef* f = upb_oneof_iter_field(&fit); + fields[upb_fielddef_index(f)].offset = data_offset; + fields[upb_fielddef_index(f)].presence = ~case_offset; + } } - return ret; -} - -void upb_array_free(upb_array *arr) { - upb_array_uninit(arr); - upb_free(arr->alloc, arr); -} + /* Size of the entire structure should be a multiple of its greatest + * alignment. TODO: track overall alignment for real? */ + l->size = align_up(l->size, 8); -size_t upb_array_size(const upb_array *arr) { - return arr->len; + return true; } -upb_fieldtype_t upb_array_type(const upb_array *arr) { - return arr->type; -} -upb_msgval upb_array_get(const upb_array *arr, size_t i) { - UPB_ASSERT(i < arr->len); - return upb_msgval_read(arr->data, i * arr->element_size, arr->element_size); -} +/** upb_msgfactory ************************************************************/ -bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) { - UPB_ASSERT(i <= arr->len); +struct upb_msgfactory { + const upb_symtab *symtab; /* We own a ref. */ + upb_inttable layouts; + upb_inttable mergehandlers; +}; - if (i == arr->len) { - /* Extending the array. */ +upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) { + upb_msgfactory *ret = upb_gmalloc(sizeof(*ret)); - if (i == arr->size) { - /* Need to reallocate. */ - size_t new_size = UPB_MAX(arr->size * 2, 8); - size_t new_bytes = new_size * arr->element_size; - size_t old_bytes = arr->size * arr->element_size; - upb_msgval *new_data = - upb_realloc(arr->alloc, arr->data, old_bytes, new_bytes); + ret->symtab = symtab; + upb_inttable_init(&ret->layouts, UPB_CTYPE_PTR); + upb_inttable_init(&ret->mergehandlers, UPB_CTYPE_CONSTPTR); - if (!new_data) { - return false; - } + return ret; +} - arr->data = new_data; - arr->size = new_size; - } +void upb_msgfactory_free(upb_msgfactory *f) { + upb_inttable_iter i; + upb_inttable_begin(&i, &f->layouts); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + upb_msglayout *l = upb_value_getptr(upb_inttable_iter_value(&i)); + upb_msglayout_free(l); + } - arr->len = i + 1; + upb_inttable_begin(&i, &f->mergehandlers); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + const upb_handlers *h = upb_value_getconstptr(upb_inttable_iter_value(&i)); + upb_handlers_unref(h, f); } - upb_msgval_write(arr->data, i * arr->element_size, val, arr->element_size); - return true; + upb_inttable_uninit(&f->layouts); + upb_inttable_uninit(&f->mergehandlers); + upb_gfree(f); } +const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f) { + return f->symtab; +} -/** upb_map *******************************************************************/ - -struct upb_map { - upb_fieldtype_t key_type; - upb_fieldtype_t val_type; - /* We may want to optimize this to use inttable where possible, for greater - * efficiency and lower memory footprint. */ - upb_strtable strtab; - upb_alloc *alloc; -}; +const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, + const upb_msgdef *m) { + upb_value v; + UPB_ASSERT(upb_symtab_lookupmsg(f->symtab, upb_msgdef_fullname(m)) == m); + UPB_ASSERT(!upb_msgdef_mapentry(m)); -static void upb_map_tokey(upb_fieldtype_t type, upb_msgval *key, - const char **out_key, size_t *out_len) { - switch (type) { - case UPB_TYPE_STRING: - /* Point to string data of the input key. */ - *out_key = key->str.data; - *out_len = key->str.size; - return; - case UPB_TYPE_BOOL: - case UPB_TYPE_INT32: - case UPB_TYPE_UINT32: - case UPB_TYPE_INT64: - case UPB_TYPE_UINT64: - /* Point to the key itself. XXX: big-endian. */ - *out_key = (const char*)key; - *out_len = upb_msgval_sizeof(type); - return; - case UPB_TYPE_BYTES: - case UPB_TYPE_DOUBLE: - case UPB_TYPE_ENUM: - case UPB_TYPE_FLOAT: - case UPB_TYPE_MESSAGE: - break; /* Cannot be a map key. */ + if (upb_inttable_lookupptr(&f->layouts, m, &v)) { + UPB_ASSERT(upb_value_getptr(v)); + return upb_value_getptr(v); + } else { + /* In case of circular dependency, layout has to be inserted first. */ + upb_msglayout *l = upb_gmalloc(sizeof(*l)); + upb_msgfactory *mutable_f = (void*)f; + upb_inttable_insertptr(&mutable_f->layouts, m, upb_value_ptr(l)); + UPB_ASSERT(l); + if (!upb_msglayout_init(m, l, f)) { + upb_msglayout_free(l); + } + return l; } - UPB_UNREACHABLE(); } -static upb_msgval upb_map_fromkey(upb_fieldtype_t type, const char *key, - size_t len) { - switch (type) { - case UPB_TYPE_STRING: - return upb_msgval_makestr(key, len); - case UPB_TYPE_BOOL: - case UPB_TYPE_INT32: - case UPB_TYPE_UINT32: - case UPB_TYPE_INT64: - case UPB_TYPE_UINT64: - return upb_msgval_read(key, 0, upb_msgval_sizeof(type)); - case UPB_TYPE_BYTES: - case UPB_TYPE_DOUBLE: - case UPB_TYPE_ENUM: - case UPB_TYPE_FLOAT: - case UPB_TYPE_MESSAGE: - break; /* Cannot be a map key. */ - } - UPB_UNREACHABLE(); -} +#if UINTPTR_MAX == 0xffffffff +#define UPB_SIZE(size32, size64) size32 +#else +#define UPB_SIZE(size32, size64) size64 +#endif -size_t upb_map_sizeof(upb_fieldtype_t ktype, upb_fieldtype_t vtype) { - /* Size does not currently depend on key/value type. */ - UPB_UNUSED(ktype); - UPB_UNUSED(vtype); - return sizeof(upb_map); -} +#define UPB_FIELD_AT(msg, fieldtype, offset) \ + *(fieldtype*)((const char*)(msg) + offset) -bool upb_map_init(upb_map *map, upb_fieldtype_t ktype, upb_fieldtype_t vtype, - upb_alloc *a) { - upb_ctype_t vtabtype = upb_fieldtotabtype(vtype); - UPB_ASSERT(upb_fieldtype_mapkeyok(ktype)); - map->key_type = ktype; - map->val_type = vtype; - map->alloc = a; +#define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \ + UPB_FIELD_AT(msg, int, case_offset) == case_val \ + ? UPB_FIELD_AT(msg, fieldtype, offset) \ + : default - if (!upb_strtable_init2(&map->strtab, vtabtype, a)) { - return false; - } +#define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \ + UPB_FIELD_AT(msg, int, case_offset) = case_val; \ + UPB_FIELD_AT(msg, fieldtype, offset) = value; - return true; -} +#undef UPB_SIZE +#undef UPB_FIELD_AT +#undef UPB_READ_ONEOF +#undef UPB_WRITE_ONEOF +/* +** upb::RefCounted Implementation +** +** Our key invariants are: +** 1. reference cycles never span groups +** 2. for ref2(to, from), we increment to's count iff group(from) != group(to) +** +** The previous two are how we avoid leaking cycles. Other important +** invariants are: +** 3. for mutable objects "from" and "to", if there exists a ref2(to, from) +** this implies group(from) == group(to). (In practice, what we implement +** is even stronger; "from" and "to" will share a group if there has *ever* +** been a ref2(to, from), but all that is necessary for correctness is the +** weaker one). +** 4. mutable and immutable objects are never in the same group. +*/ -void upb_map_uninit(upb_map *map) { - upb_strtable_uninit2(&map->strtab, map->alloc); -} -upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype, - upb_alloc *a) { - upb_map *map = upb_malloc(a, upb_map_sizeof(ktype, vtype)); +#include - if (!map) { - return NULL; - } +static void freeobj(upb_refcounted *o); - if (!upb_map_init(map, ktype, vtype, a)) { - return NULL; - } +const char untracked_val; +const void *UPB_UNTRACKED_REF = &untracked_val; - return map; -} +/* arch-specific atomic primitives *******************************************/ -void upb_map_free(upb_map *map) { - upb_map_uninit(map); - upb_free(map->alloc, map); -} +#ifdef UPB_THREAD_UNSAFE /*---------------------------------------------------*/ -size_t upb_map_size(const upb_map *map) { - return upb_strtable_count(&map->strtab); -} +static void atomic_inc(uint32_t *a) { (*a)++; } +static bool atomic_dec(uint32_t *a) { return --(*a) == 0; } -upb_fieldtype_t upb_map_keytype(const upb_map *map) { - return map->key_type; -} +#elif defined(__GNUC__) || defined(__clang__) /*------------------------------*/ -upb_fieldtype_t upb_map_valuetype(const upb_map *map) { - return map->val_type; -} +static void atomic_inc(uint32_t *a) { __sync_fetch_and_add(a, 1); } +static bool atomic_dec(uint32_t *a) { return __sync_sub_and_fetch(a, 1) == 0; } -bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) { - upb_value tabval; - const char *key_str; - size_t key_len; - bool ret; +#elif defined(WIN32) /*-------------------------------------------------------*/ - upb_map_tokey(map->key_type, &key, &key_str, &key_len); - ret = upb_strtable_lookup2(&map->strtab, key_str, key_len, &tabval); - if (ret) { - memcpy(val, &tabval, sizeof(tabval)); - } +#include - return ret; +static void atomic_inc(upb_atomic_t *a) { InterlockedIncrement(&a->val); } +static bool atomic_dec(upb_atomic_t *a) { + return InterlockedDecrement(&a->val) == 0; } -bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val, - upb_msgval *removed) { - const char *key_str; - size_t key_len; - upb_value tabval = upb_toval(val); - upb_value removedtabval; - upb_alloc *a = map->alloc; +#else +#error Atomic primitives not defined for your platform/CPU. \ + Implement them or compile with UPB_THREAD_UNSAFE. +#endif - upb_map_tokey(map->key_type, &key, &key_str, &key_len); +/* All static objects point to this refcount. + * It is special-cased in ref/unref below. */ +uint32_t static_refcount = -1; - /* TODO(haberman): add overwrite operation to minimize number of lookups. */ - if (upb_strtable_lookup2(&map->strtab, key_str, key_len, NULL)) { - upb_strtable_remove3(&map->strtab, key_str, key_len, &removedtabval, a); - memcpy(&removed, &removedtabval, sizeof(removed)); - } +/* We can avoid atomic ops for statically-declared objects. + * This is a minor optimization but nice since we can avoid degrading under + * contention in this case. */ - return upb_strtable_insert3(&map->strtab, key_str, key_len, tabval, a); +static void refgroup(uint32_t *group) { + if (group != &static_refcount) + atomic_inc(group); } -bool upb_map_del(upb_map *map, upb_msgval key) { - const char *key_str; - size_t key_len; - upb_alloc *a = map->alloc; - - upb_map_tokey(map->key_type, &key, &key_str, &key_len); - return upb_strtable_remove3(&map->strtab, key_str, key_len, NULL, a); +static bool unrefgroup(uint32_t *group) { + if (group == &static_refcount) { + return false; + } else { + return atomic_dec(group); + } } -/** upb_mapiter ***************************************************************/ - -struct upb_mapiter { - upb_strtable_iter iter; - upb_fieldtype_t key_type; -}; - -size_t upb_mapiter_sizeof() { - return sizeof(upb_mapiter); -} +/* Reference tracking (debug only) ********************************************/ -void upb_mapiter_begin(upb_mapiter *i, const upb_map *map) { - upb_strtable_begin(&i->iter, &map->strtab); - i->key_type = map->key_type; -} +#ifdef UPB_DEBUG_REFS -upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a) { - upb_mapiter *ret = upb_malloc(a, upb_mapiter_sizeof()); +#ifdef UPB_THREAD_UNSAFE - if (!ret) { - return NULL; - } +static void upb_lock() {} +static void upb_unlock() {} - upb_mapiter_begin(ret, t); - return ret; -} +#else -void upb_mapiter_free(upb_mapiter *i, upb_alloc *a) { - upb_free(a, i); -} +/* User must define functions that lock/unlock a global mutex and link this + * file against them. */ +void upb_lock(); +void upb_unlock(); -void upb_mapiter_next(upb_mapiter *i) { - upb_strtable_next(&i->iter); -} +#endif -bool upb_mapiter_done(const upb_mapiter *i) { - return upb_strtable_done(&i->iter); -} +/* UPB_DEBUG_REFS mode counts on being able to malloc() memory in some + * code-paths that can normally never fail, like upb_refcounted_ref(). Since + * we have no way to propagage out-of-memory errors back to the user, and since + * these errors can only occur in UPB_DEBUG_REFS mode, we use an allocator that + * immediately aborts on failure (avoiding the global allocator, which might + * inject failures). */ -upb_msgval upb_mapiter_key(const upb_mapiter *i) { - return upb_map_fromkey(i->key_type, upb_strtable_iter_key(&i->iter), - upb_strtable_iter_keylength(&i->iter)); -} +#include -upb_msgval upb_mapiter_value(const upb_mapiter *i) { - return upb_msgval_fromval(upb_strtable_iter_value(&i->iter)); -} +static void *upb_debugrefs_allocfunc(upb_alloc *alloc, void *ptr, + size_t oldsize, size_t size) { + UPB_UNUSED(alloc); + UPB_UNUSED(oldsize); + if (size == 0) { + free(ptr); + return NULL; + } else { + void *ret = realloc(ptr, size); -void upb_mapiter_setdone(upb_mapiter *i) { - upb_strtable_iter_setdone(&i->iter); -} + if (!ret) { + abort(); + } -bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2) { - return upb_strtable_iter_isequal(&i1->iter, &i2->iter); + return ret; + } } - -/** Handlers for upb_msg ******************************************************/ - -typedef struct { - size_t offset; - int32_t hasbit; -} upb_msg_handlerdata; - -/* Fallback implementation if the handler is not specialized by the producer. */ -#define MSG_WRITER(type, ctype) \ - bool upb_msg_set ## type (void *c, const void *hd, ctype val) { \ - uint8_t *m = c; \ - const upb_msg_handlerdata *d = hd; \ - if (d->hasbit > 0) \ - *(uint8_t*)&m[d->hasbit / 8] |= 1 << (d->hasbit % 8); \ - *(ctype*)&m[d->offset] = val; \ - return true; \ - } \ - -MSG_WRITER(double, double) -MSG_WRITER(float, float) -MSG_WRITER(int32, int32_t) -MSG_WRITER(int64, int64_t) -MSG_WRITER(uint32, uint32_t) -MSG_WRITER(uint64, uint64_t) -MSG_WRITER(bool, bool) - -bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f, - size_t offset, int32_t hasbit) { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - bool ok; - - upb_msg_handlerdata *d = upb_gmalloc(sizeof(*d)); - if (!d) return false; - d->offset = offset; - d->hasbit = hasbit; - - upb_handlerattr_sethandlerdata(&attr, d); - upb_handlerattr_setalwaysok(&attr, true); - upb_handlers_addcleanup(h, d, upb_gfree); - -#define TYPE(u, l) \ - case UPB_TYPE_##u: \ - ok = upb_handlers_set##l(h, f, upb_msg_set##l, &attr); break; - - ok = false; - - switch (upb_fielddef_type(f)) { - TYPE(INT64, int64); - TYPE(INT32, int32); - TYPE(ENUM, int32); - TYPE(UINT64, uint64); - TYPE(UINT32, uint32); - TYPE(DOUBLE, double); - TYPE(FLOAT, float); - TYPE(BOOL, bool); - default: UPB_ASSERT(false); break; - } -#undef TYPE - - upb_handlerattr_uninit(&attr); - return ok; -} - -bool upb_msg_getscalarhandlerdata(const upb_handlers *h, - upb_selector_t s, - upb_fieldtype_t *type, - size_t *offset, - int32_t *hasbit) { - const upb_msg_handlerdata *d; - upb_func *f = upb_handlers_gethandler(h, s); - - if ((upb_int64_handlerfunc*)f == upb_msg_setint64) { - *type = UPB_TYPE_INT64; - } else if ((upb_int32_handlerfunc*)f == upb_msg_setint32) { - *type = UPB_TYPE_INT32; - } else if ((upb_uint64_handlerfunc*)f == upb_msg_setuint64) { - *type = UPB_TYPE_UINT64; - } else if ((upb_uint32_handlerfunc*)f == upb_msg_setuint32) { - *type = UPB_TYPE_UINT32; - } else if ((upb_double_handlerfunc*)f == upb_msg_setdouble) { - *type = UPB_TYPE_DOUBLE; - } else if ((upb_float_handlerfunc*)f == upb_msg_setfloat) { - *type = UPB_TYPE_FLOAT; - } else if ((upb_bool_handlerfunc*)f == upb_msg_setbool) { - *type = UPB_TYPE_BOOL; - } else { - return false; - } - - d = upb_handlers_gethandlerdata(h, s); - *offset = d->offset; - *hasbit = d->hasbit; - return true; -} -/* -** upb::RefCounted Implementation -** -** Our key invariants are: -** 1. reference cycles never span groups -** 2. for ref2(to, from), we increment to's count iff group(from) != group(to) -** -** The previous two are how we avoid leaking cycles. Other important -** invariants are: -** 3. for mutable objects "from" and "to", if there exists a ref2(to, from) -** this implies group(from) == group(to). (In practice, what we implement -** is even stronger; "from" and "to" will share a group if there has *ever* -** been a ref2(to, from), but all that is necessary for correctness is the -** weaker one). -** 4. mutable and immutable objects are never in the same group. -*/ - - -#include - -static void freeobj(upb_refcounted *o); - -const char untracked_val; -const void *UPB_UNTRACKED_REF = &untracked_val; - -/* arch-specific atomic primitives *******************************************/ - -#ifdef UPB_THREAD_UNSAFE /*---------------------------------------------------*/ - -static void atomic_inc(uint32_t *a) { (*a)++; } -static bool atomic_dec(uint32_t *a) { return --(*a) == 0; } - -#elif defined(__GNUC__) || defined(__clang__) /*------------------------------*/ - -static void atomic_inc(uint32_t *a) { __sync_fetch_and_add(a, 1); } -static bool atomic_dec(uint32_t *a) { return __sync_sub_and_fetch(a, 1) == 0; } - -#elif defined(WIN32) /*-------------------------------------------------------*/ - -#include - -static void atomic_inc(upb_atomic_t *a) { InterlockedIncrement(&a->val); } -static bool atomic_dec(upb_atomic_t *a) { - return InterlockedDecrement(&a->val) == 0; -} - -#else -#error Atomic primitives not defined for your platform/CPU. \ - Implement them or compile with UPB_THREAD_UNSAFE. -#endif - -/* All static objects point to this refcount. - * It is special-cased in ref/unref below. */ -uint32_t static_refcount = -1; - -/* We can avoid atomic ops for statically-declared objects. - * This is a minor optimization but nice since we can avoid degrading under - * contention in this case. */ - -static void refgroup(uint32_t *group) { - if (group != &static_refcount) - atomic_inc(group); -} - -static bool unrefgroup(uint32_t *group) { - if (group == &static_refcount) { - return false; - } else { - return atomic_dec(group); - } -} - - -/* Reference tracking (debug only) ********************************************/ - -#ifdef UPB_DEBUG_REFS - -#ifdef UPB_THREAD_UNSAFE - -static void upb_lock() {} -static void upb_unlock() {} - -#else - -/* User must define functions that lock/unlock a global mutex and link this - * file against them. */ -void upb_lock(); -void upb_unlock(); - -#endif - -/* UPB_DEBUG_REFS mode counts on being able to malloc() memory in some - * code-paths that can normally never fail, like upb_refcounted_ref(). Since - * we have no way to propagage out-of-memory errors back to the user, and since - * these errors can only occur in UPB_DEBUG_REFS mode, we use an allocator that - * immediately aborts on failure (avoiding the global allocator, which might - * inject failures). */ - -#include - -static void *upb_debugrefs_allocfunc(upb_alloc *alloc, void *ptr, - size_t oldsize, size_t size) { - UPB_UNUSED(alloc); - UPB_UNUSED(oldsize); - if (size == 0) { - free(ptr); - return NULL; - } else { - void *ret = realloc(ptr, size); - - if (!ret) { - abort(); - } - - return ret; - } -} - -upb_alloc upb_alloc_debugrefs = {&upb_debugrefs_allocfunc}; +upb_alloc upb_alloc_debugrefs = {&upb_debugrefs_allocfunc}; typedef struct { int count; /* How many refs there are (duplicates only allowed for ref2). */ @@ -6425,9 +6698,10 @@ static size_t begin(const upb_table *t) { /* A simple "subclass" of upb_table that only adds a hash function for strings. */ static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) { + uint32_t len = (uint32_t) k2.str.len; char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1); if (str == NULL) return 0; - memcpy(str, &k2.str.len, sizeof(uint32_t)); + memcpy(str, &len, sizeof(uint32_t)); memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len + 1); return (uintptr_t)str; } @@ -7393,7 +7667,6 @@ size_t upb_env_bytesallocated(const upb_env *e) { * Do not edit -- your changes will be discarded when the file is * regenerated. */ - static const upb_msgdef msgs[22]; static const upb_fielddef fields[107]; static const upb_enumdef enums[5]; @@ -7406,28 +7679,28 @@ static upb_inttable reftables[268]; #endif static const upb_msgdef msgs[22] = { - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto", 41, 8, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[0], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[0]), false, UPB_SYNTAX_PROTO2, &reftables[0], &reftables[1]), - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ExtensionRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[11], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[16]), false, UPB_SYNTAX_PROTO2, &reftables[2], &reftables[3]), - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ReservedRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[14], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[20]), false, UPB_SYNTAX_PROTO2, &reftables[4], &reftables[5]), - UPB_MSGDEF_INIT("google.protobuf.EnumDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[17], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[24]), false, UPB_SYNTAX_PROTO2, &reftables[6], &reftables[7]), - UPB_MSGDEF_INIT("google.protobuf.EnumOptions", 9, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[0], &arrays[21], 4, 2), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[28]), false, UPB_SYNTAX_PROTO2, &reftables[8], &reftables[9]), - UPB_MSGDEF_INIT("google.protobuf.EnumValueDescriptorProto", 9, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[25], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[32]), false, UPB_SYNTAX_PROTO2, &reftables[10], &reftables[11]), - UPB_MSGDEF_INIT("google.protobuf.EnumValueOptions", 8, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[2], &arrays[29], 2, 1), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[36]), false, UPB_SYNTAX_PROTO2, &reftables[12], &reftables[13]), - UPB_MSGDEF_INIT("google.protobuf.FieldDescriptorProto", 24, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[31], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[40]), false, UPB_SYNTAX_PROTO2, &reftables[14], &reftables[15]), - UPB_MSGDEF_INIT("google.protobuf.FieldOptions", 13, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[4], &arrays[42], 11, 6), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[56]), false, UPB_SYNTAX_PROTO2, &reftables[16], &reftables[17]), - UPB_MSGDEF_INIT("google.protobuf.FileDescriptorProto", 43, 6, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[53], 13, 12), UPB_STRTABLE_INIT(12, 15, UPB_CTYPE_PTR, 4, &strentries[72]), false, UPB_SYNTAX_PROTO2, &reftables[18], &reftables[19]), - UPB_MSGDEF_INIT("google.protobuf.FileDescriptorSet", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[66], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[88]), false, UPB_SYNTAX_PROTO2, &reftables[20], &reftables[21]), - UPB_MSGDEF_INIT("google.protobuf.FileOptions", 38, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[6], &arrays[68], 42, 17), UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_PTR, 5, &strentries[92]), false, UPB_SYNTAX_PROTO2, &reftables[22], &reftables[23]), - UPB_MSGDEF_INIT("google.protobuf.MessageOptions", 11, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[8], &arrays[110], 8, 4), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[124]), false, UPB_SYNTAX_PROTO2, &reftables[24], &reftables[25]), - UPB_MSGDEF_INIT("google.protobuf.MethodDescriptorProto", 16, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[118], 7, 6), UPB_STRTABLE_INIT(6, 7, UPB_CTYPE_PTR, 3, &strentries[132]), false, UPB_SYNTAX_PROTO2, &reftables[26], &reftables[27]), - UPB_MSGDEF_INIT("google.protobuf.MethodOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[10], &arrays[125], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[140]), false, UPB_SYNTAX_PROTO2, &reftables[28], &reftables[29]), - UPB_MSGDEF_INIT("google.protobuf.OneofDescriptorProto", 6, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[126], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[144]), false, UPB_SYNTAX_PROTO2, &reftables[30], &reftables[31]), - UPB_MSGDEF_INIT("google.protobuf.ServiceDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[128], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[148]), false, UPB_SYNTAX_PROTO2, &reftables[32], &reftables[33]), - UPB_MSGDEF_INIT("google.protobuf.ServiceOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[14], &arrays[132], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[152]), false, UPB_SYNTAX_PROTO2, &reftables[34], &reftables[35]), - UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[133], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[156]), false, UPB_SYNTAX_PROTO2, &reftables[36], &reftables[37]), - UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo.Location", 20, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[135], 7, 5), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[160]), false, UPB_SYNTAX_PROTO2, &reftables[38], &reftables[39]), - UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption", 19, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[142], 9, 7), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[168]), false, UPB_SYNTAX_PROTO2, &reftables[40], &reftables[41]), - UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption.NamePart", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[151], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[184]), false, UPB_SYNTAX_PROTO2, &reftables[42], &reftables[43]), + UPB_MSGDEF_INIT("google.protobuf.DescriptorProto", 41, 8, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[0], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[0]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[0], &reftables[1]), + UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ExtensionRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[11], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[16]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[2], &reftables[3]), + UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ReservedRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[14], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[20]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[4], &reftables[5]), + UPB_MSGDEF_INIT("google.protobuf.EnumDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[17], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[24]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[6], &reftables[7]), + UPB_MSGDEF_INIT("google.protobuf.EnumOptions", 9, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[0], &arrays[21], 4, 2), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[28]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[8], &reftables[9]), + UPB_MSGDEF_INIT("google.protobuf.EnumValueDescriptorProto", 9, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[25], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[32]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[10], &reftables[11]), + UPB_MSGDEF_INIT("google.protobuf.EnumValueOptions", 8, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[2], &arrays[29], 2, 1), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[36]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[12], &reftables[13]), + UPB_MSGDEF_INIT("google.protobuf.FieldDescriptorProto", 24, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[31], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[40]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[14], &reftables[15]), + UPB_MSGDEF_INIT("google.protobuf.FieldOptions", 13, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[4], &arrays[42], 11, 6), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[56]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[16], &reftables[17]), + UPB_MSGDEF_INIT("google.protobuf.FileDescriptorProto", 43, 6, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[53], 13, 12), UPB_STRTABLE_INIT(12, 15, UPB_CTYPE_PTR, 4, &strentries[72]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[18], &reftables[19]), + UPB_MSGDEF_INIT("google.protobuf.FileDescriptorSet", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[66], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[88]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[20], &reftables[21]), + UPB_MSGDEF_INIT("google.protobuf.FileOptions", 38, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[6], &arrays[68], 42, 17), UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_PTR, 5, &strentries[92]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[22], &reftables[23]), + UPB_MSGDEF_INIT("google.protobuf.MessageOptions", 11, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[8], &arrays[110], 8, 4), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[124]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[24], &reftables[25]), + UPB_MSGDEF_INIT("google.protobuf.MethodDescriptorProto", 16, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[118], 7, 6), UPB_STRTABLE_INIT(6, 7, UPB_CTYPE_PTR, 3, &strentries[132]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[26], &reftables[27]), + UPB_MSGDEF_INIT("google.protobuf.MethodOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[10], &arrays[125], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[140]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[28], &reftables[29]), + UPB_MSGDEF_INIT("google.protobuf.OneofDescriptorProto", 6, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[126], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[144]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[30], &reftables[31]), + UPB_MSGDEF_INIT("google.protobuf.ServiceDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[128], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[148]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[32], &reftables[33]), + UPB_MSGDEF_INIT("google.protobuf.ServiceOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[14], &arrays[132], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[152]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[34], &reftables[35]), + UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[133], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[156]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[36], &reftables[37]), + UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo.Location", 20, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[135], 7, 5), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[160]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[38], &reftables[39]), + UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption", 19, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[142], 9, 7), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[168]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[40], &reftables[41]), + UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption.NamePart", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[151], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[184]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[42], &reftables[43]), }; static const upb_fielddef fields[107] = { @@ -8629,14 +8902,21 @@ static void *file_startenum(void *closure, const void *hd) { static void *file_startext(void *closure, const void *hd) { upb_descreader *r = closure; - bool ok; r->f = upb_fielddef_new(r); - ok = upb_filedef_addext(r->file, r->f, r, NULL); UPB_UNUSED(hd); - UPB_ASSERT(ok); return r; } +static bool file_endext(void *closure, const void *hd) { + /* The current symtab code can't handle extensions, so we just discard + * them for now. */ + upb_descreader *r = closure; + upb_fielddef_unref(r->f, r); + UPB_UNUSED(hd); + r->f = NULL; + return true; +} + static size_t file_ondep(void *closure, const void *hd, const char *buf, size_t n, const upb_bufhandle *handle) { upb_descreader *r = closure; @@ -8995,6 +9275,7 @@ static size_t msg_name(void *closure, const void *hd, const char *buf, upb_def_setfullname(upb_msgdef_upcast_mutable(m), name, NULL); upb_descreader_setscopename(r, name); /* Passes ownership of name. */ + return n; } @@ -9009,13 +9290,21 @@ static void *msg_startmsg(void *closure, const void *hd) { static void *msg_startext(void *closure, const void *hd) { upb_descreader *r = closure; - upb_fielddef *f = upb_fielddef_new(&f); - bool ok = upb_filedef_addext(r->file, f, &f, NULL); + r->f = upb_fielddef_new(r); UPB_UNUSED(hd); - UPB_ASSERT(ok); return r; } +static bool msg_endext(void *closure, const void *hd) { + /* The current symtab code can't handle extensions, so we just discard + * them for now. */ + upb_descreader *r = closure; + upb_fielddef_unref(r->f, r); + UPB_UNUSED(hd); + r->f = NULL; + return true; +} + static void *msg_startfield(void *closure, const void *hd) { upb_descreader *r = closure; r->f = upb_fielddef_new(&r->f); @@ -9070,6 +9359,8 @@ static void reghandlers(const void *closure, upb_handlers *h) { upb_handlers_setstring(h, F(DescriptorProto, name), &msg_name, NULL); upb_handlers_setstartsubmsg(h, F(DescriptorProto, extension), &msg_startext, NULL); + upb_handlers_setendsubmsg(h, F(DescriptorProto, extension), &msg_endext, + NULL); upb_handlers_setstartsubmsg(h, F(DescriptorProto, nested_type), &msg_startmsg, NULL); upb_handlers_setstartsubmsg(h, F(DescriptorProto, field), @@ -9093,6 +9384,8 @@ static void reghandlers(const void *closure, upb_handlers *h) { &file_startenum, NULL); upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, extension), &file_startext, NULL); + upb_handlers_setendsubmsg(h, F(FileDescriptorProto, extension), + &file_endext, NULL); upb_handlers_setstring(h, F(FileDescriptorProto, dependency), &file_ondep, NULL); } else if (upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)) { @@ -9872,8 +10165,8 @@ static void generate_delimfield(compiler *c, const upb_fielddef *f, putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR)); /* Need to emit even if no handler to skip past the string. */ putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING)); - putop(c, OP_POP); maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR); + putop(c, OP_POP); putop(c, OP_SETDELIM); putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK); putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_LOOPBREAK); @@ -9888,8 +10181,8 @@ static void generate_delimfield(compiler *c, const upb_fielddef *f, putop(c, OP_PUSHLENDELIM); putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR)); putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING)); - putop(c, OP_POP); maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR); + putop(c, OP_POP); putop(c, OP_SETDELIM); } } @@ -12331,35 +12624,181 @@ done: ** - handling of keys/escape-sequences/etc that span input buffers. */ +#include #include #include #include #include +#include #include #include +#include + #define UPB_JSON_MAX_DEPTH 64 -typedef struct { - upb_sink sink; +/* Type of value message */ +enum { + VALUE_NULLVALUE = 0, + VALUE_NUMBERVALUE = 1, + VALUE_STRINGVALUE = 2, + VALUE_BOOLVALUE = 3, + VALUE_STRUCTVALUE = 4, + VALUE_LISTVALUE = 5 +}; - /* The current message in which we're parsing, and the field whose value we're - * expecting next. */ - const upb_msgdef *m; - const upb_fielddef *f; +/* Forward declare */ +static bool is_top_level(upb_json_parser *p); +static bool is_wellknown_msg(upb_json_parser *p, upb_wellknowntype_t type); +static bool is_wellknown_field(upb_json_parser *p, upb_wellknowntype_t type); - /* The table mapping json name to fielddef for this message. */ - upb_strtable *name_table; +static bool is_number_wrapper_object(upb_json_parser *p); +static bool does_number_wrapper_start(upb_json_parser *p); +static bool does_number_wrapper_end(upb_json_parser *p); - /* We are in a repeated-field context, ready to emit mapentries as - * submessages. This flag alters the start-of-object (open-brace) behavior to - * begin a sequence of mapentry messages rather than a single submessage. */ - bool is_map; +static bool is_string_wrapper_object(upb_json_parser *p); +static bool does_string_wrapper_start(upb_json_parser *p); +static bool does_string_wrapper_end(upb_json_parser *p); - /* We are in a map-entry message context. This flag is set when parsing the - * value field of a single map entry and indicates to all value-field parsers - * (subobjects, strings, numbers, and bools) that the map-entry submessage +static bool is_fieldmask_object(upb_json_parser *p); +static bool does_fieldmask_start(upb_json_parser *p); +static bool does_fieldmask_end(upb_json_parser *p); +static void start_fieldmask_object(upb_json_parser *p); +static void end_fieldmask_object(upb_json_parser *p); + +static void start_wrapper_object(upb_json_parser *p); +static void end_wrapper_object(upb_json_parser *p); + +static void start_value_object(upb_json_parser *p, int value_type); +static void end_value_object(upb_json_parser *p); + +static void start_listvalue_object(upb_json_parser *p); +static void end_listvalue_object(upb_json_parser *p); + +static void start_structvalue_object(upb_json_parser *p); +static void end_structvalue_object(upb_json_parser *p); + +static void start_object(upb_json_parser *p); +static void end_object(upb_json_parser *p); + +static void start_any_object(upb_json_parser *p, const char *ptr); +static bool end_any_object(upb_json_parser *p, const char *ptr); + +static bool start_subobject(upb_json_parser *p); +static void end_subobject(upb_json_parser *p); + +static void start_member(upb_json_parser *p); +static void end_member(upb_json_parser *p); +static bool end_membername(upb_json_parser *p); + +static void start_any_member(upb_json_parser *p, const char *ptr); +static void end_any_member(upb_json_parser *p, const char *ptr); +static bool end_any_membername(upb_json_parser *p); + +size_t parse(void *closure, const void *hd, const char *buf, size_t size, + const upb_bufhandle *handle); +static bool end(void *closure, const void *hd); + +static const char eof_ch = 'e'; + +/* stringsink */ +typedef struct { + upb_byteshandler handler; + upb_bytessink sink; + char *ptr; + size_t len, size; +} upb_stringsink; + + +static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) { + upb_stringsink *sink = _sink; + sink->len = 0; + UPB_UNUSED(hd); + UPB_UNUSED(size_hint); + return sink; +} + +static size_t stringsink_string(void *_sink, const void *hd, const char *ptr, + size_t len, const upb_bufhandle *handle) { + upb_stringsink *sink = _sink; + size_t new_size = sink->size; + + UPB_UNUSED(hd); + UPB_UNUSED(handle); + + while (sink->len + len > new_size) { + new_size *= 2; + } + + if (new_size != sink->size) { + sink->ptr = realloc(sink->ptr, new_size); + sink->size = new_size; + } + + memcpy(sink->ptr + sink->len, ptr, len); + sink->len += len; + + return len; +} + +void upb_stringsink_init(upb_stringsink *sink) { + upb_byteshandler_init(&sink->handler); + upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL); + upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL); + + upb_bytessink_reset(&sink->sink, &sink->handler, sink); + + sink->size = 32; + sink->ptr = malloc(sink->size); + sink->len = 0; +} + +void upb_stringsink_uninit(upb_stringsink *sink) { free(sink->ptr); } + +typedef struct { + /* For encoding Any value field in binary format. */ + const upb_handlers *encoder_handlers; + upb_pb_encoder *encoder; + upb_stringsink stringsink; + + /* For decoding Any value field in json format. */ + upb_json_parsermethod *parser_method; + upb_json_parser* parser; + upb_sink sink; + + /* Mark the range of uninterpreted values in json input before type url. */ + const char *before_type_url_start; + const char *before_type_url_end; + + /* Mark the range of uninterpreted values in json input after type url. */ + const char *after_type_url_start; +} upb_jsonparser_any_frame; + +typedef struct { + upb_sink sink; + + /* The current message in which we're parsing, and the field whose value we're + * expecting next. */ + const upb_msgdef *m; + const upb_fielddef *f; + + /* The table mapping json name to fielddef for this message. */ + upb_strtable *name_table; + + /* We are in a repeated-field context. We need this flag to decide whether to + * handle the array as a normal repeated field or a + * google.protobuf.ListValue/google.protobuf.Value. */ + bool is_repeated; + + /* We are in a repeated-field context, ready to emit mapentries as + * submessages. This flag alters the start-of-object (open-brace) behavior to + * begin a sequence of mapentry messages rather than a single submessage. */ + bool is_map; + + /* We are in a map-entry message context. This flag is set when parsing the + * value field of a single map entry and indicates to all value-field parsers + * (subobjects, strings, numbers, and bools) that the map-entry submessage * should end as soon as the value is parsed. */ bool is_mapentry; @@ -12368,8 +12807,33 @@ typedef struct { * because |f| is the field in the *current* message (i.e., the map-entry * message itself), not the parent's field that leads to this map. */ const upb_fielddef *mapfield; + + /* We are in an Any message context. This flag is set when parsing the Any + * message and indicates to all field parsers (subobjects, strings, numbers, + * and bools) that the parsed field should be serialized as binary data or + * cached (type url not found yet). */ + bool is_any; + + /* The type of packed message in Any. */ + upb_jsonparser_any_frame *any_frame; + + /* True if the field to be parsed is unknown. */ + bool is_unknown_field; } upb_jsonparser_frame; +static void init_frame(upb_jsonparser_frame* frame) { + frame->m = NULL; + frame->f = NULL; + frame->name_table = NULL; + frame->is_repeated = false; + frame->is_map = false; + frame->is_mapentry = false; + frame->mapfield = NULL; + frame->is_any = false; + frame->any_frame = NULL; + frame->is_unknown_field = false; +} + struct upb_json_parser { upb_env *env; const upb_json_parsermethod *method; @@ -12405,8 +12869,25 @@ struct upb_json_parser { /* Intermediate result of parsing a unicode escape sequence. */ uint32_t digit; + + /* For resolve type url in Any. */ + const upb_symtab *symtab; + + /* Whether to proceed if unknown field is met. */ + bool ignore_json_unknown; + + /* Cache for parsing timestamp due to base and zone are handled in different + * handlers. */ + struct tm tm; }; +static upb_jsonparser_frame* start_jsonparser_frame(upb_json_parser *p) { + upb_jsonparser_frame *inner; + inner = p->top + 1; + init_frame(inner); + return inner; +} + struct upb_json_parsermethod { upb_refcounted base; @@ -12422,6 +12903,84 @@ struct upb_json_parsermethod { #define PARSER_CHECK_RETURN(x) if (!(x)) return false +static void json_parser_any_frame_reset(upb_jsonparser_any_frame *frame) { + frame->encoder_handlers = NULL; + frame->encoder = NULL; + frame->parser_method = NULL; + frame->parser = NULL; + frame->before_type_url_start = NULL; + frame->before_type_url_end = NULL; + frame->after_type_url_start = NULL; +} + +static void json_parser_any_frame_set_payload_type( + upb_json_parser *p, + upb_jsonparser_any_frame *frame, + const upb_msgdef *payload_type) { + /* Initialize encoder. */ + frame->encoder_handlers = + upb_pb_encoder_newhandlers(payload_type, &frame->encoder_handlers); + upb_stringsink_init(&frame->stringsink); + frame->encoder = + upb_pb_encoder_create( + p->env, frame->encoder_handlers, + &frame->stringsink.sink); + + /* Initialize parser. */ + frame->parser_method = + upb_json_parsermethod_new(payload_type, &frame->parser_method); + upb_sink_reset(&frame->sink, frame->encoder_handlers, frame->encoder); + frame->parser = + upb_json_parser_create(p->env, frame->parser_method, p->symtab, + &frame->sink, p->ignore_json_unknown); +} + +static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { + upb_handlers_unref(frame->encoder_handlers, + &frame->encoder_handlers); + upb_json_parsermethod_unref(frame->parser_method, + &frame->parser_method); + upb_stringsink_uninit(&frame->stringsink); +} + +static bool json_parser_any_frame_has_type_url( + upb_jsonparser_any_frame *frame) { + return frame->encoder != NULL; +} + +static bool json_parser_any_frame_has_value_before_type_url( + upb_jsonparser_any_frame *frame) { + return frame->before_type_url_start != frame->before_type_url_end; +} + +static bool json_parser_any_frame_has_value_after_type_url( + upb_jsonparser_any_frame *frame) { + return frame->after_type_url_start != NULL; +} + +static bool json_parser_any_frame_has_value( + upb_jsonparser_any_frame *frame) { + return json_parser_any_frame_has_value_before_type_url(frame) || + json_parser_any_frame_has_value_after_type_url(frame); +} + +static void json_parser_any_frame_set_before_type_url_end( + upb_jsonparser_any_frame *frame, + const char *ptr) { + if (frame->encoder == NULL) { + frame->before_type_url_end = ptr; + } +} + +static void json_parser_any_frame_set_after_type_url_start_once( + upb_jsonparser_any_frame *frame, + const char *ptr) { + if (json_parser_any_frame_has_type_url(frame) && + frame->after_type_url_start == NULL) { + frame->after_type_url_start = ptr; + } +} + /* Used to signal that a capture has been suspended. */ static char suspend_capture; @@ -12913,21 +13472,71 @@ static bool end_text(upb_json_parser *p, const char *ptr) { return capture_end(p, ptr); } -static void start_number(upb_json_parser *p, const char *ptr) { +static bool start_number(upb_json_parser *p, const char *ptr) { + if (is_top_level(p)) { + if (is_number_wrapper_object(p)) { + start_wrapper_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_NUMBERVALUE); + } else { + return false; + } + } else if (does_number_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) { + return false; + } + start_value_object(p, VALUE_NUMBERVALUE); + } + multipart_startaccum(p); capture_begin(p, ptr); + return true; } static bool parse_number(upb_json_parser *p, bool is_quoted); -static bool end_number(upb_json_parser *p, const char *ptr) { +static bool end_number_nontop(upb_json_parser *p, const char *ptr) { if (!capture_end(p, ptr)) { return false; } + if (p->top->f == NULL) { + multipart_end(p); + return true; + } + return parse_number(p, false); } +static bool end_number(upb_json_parser *p, const char *ptr) { + if (!end_number_nontop(p, ptr)) { + return false; + } + + if (does_number_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + return true; +} + /* |buf| is NULL-terminated. |buf| itself will never include quotes; * |is_quoted| tells us whether this text originally appeared inside quotes. */ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, @@ -13077,6 +13686,10 @@ static bool parse_number(upb_json_parser *p, bool is_quoted) { static bool parser_putbool(upb_json_parser *p, bool val) { bool ok; + if (p->top->f == NULL) { + return true; + } + if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) { upb_status_seterrf(&p->status, "Boolean value specified for non-bool field: %s", @@ -13091,8 +13704,138 @@ static bool parser_putbool(upb_json_parser *p, bool val) { return true; } +static bool end_bool(upb_json_parser *p, bool val) { + if (is_top_level(p)) { + if (is_wellknown_msg(p, UPB_WELLKNOWN_BOOLVALUE)) { + start_wrapper_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_BOOLVALUE); + } else { + return false; + } + } else if (is_wellknown_field(p, UPB_WELLKNOWN_BOOLVALUE)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) { + return false; + } + start_value_object(p, VALUE_BOOLVALUE); + } + + if (p->top->is_unknown_field) { + return true; + } + + if (!parser_putbool(p, val)) { + return false; + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_BOOLVALUE)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + return true; +} + +static bool end_null(upb_json_parser *p) { + const char *zero_ptr = "0"; + + if (is_top_level(p)) { + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_NULLVALUE); + } else { + return true; + } + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) { + return false; + } + start_value_object(p, VALUE_NULLVALUE); + } else { + return true; + } + + /* Fill null_value field. */ + multipart_startaccum(p); + capture_begin(p, zero_ptr); + capture_end(p, zero_ptr + 1); + parse_number(p, false); + + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + + return true; +} + +static bool start_any_stringval(upb_json_parser *p) { + multipart_startaccum(p); + return true; +} + static bool start_stringval(upb_json_parser *p) { - UPB_ASSERT(p->top->f); + if (is_top_level(p)) { + if (is_string_wrapper_object(p)) { + start_wrapper_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) { + start_fieldmask_object(p); + return true; + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) || + is_wellknown_msg(p, UPB_WELLKNOWN_DURATION)) { + start_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_STRINGVALUE); + } else { + return false; + } + } else if (does_string_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_fieldmask_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_fieldmask_object(p); + return true; + } else if (is_wellknown_field(p, UPB_WELLKNOWN_TIMESTAMP) || + is_wellknown_field(p, UPB_WELLKNOWN_DURATION)) { + if (!start_subobject(p)) { + return false; + } + start_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) { + return false; + } + start_value_object(p, VALUE_STRINGVALUE); + } + + if (p->top->f == NULL) { + multipart_startaccum(p); + return true; + } + + if (p->top->is_any) { + return start_any_stringval(p); + } if (upb_fielddef_isstring(p->top->f)) { upb_jsonparser_frame *inner; @@ -13102,14 +13845,11 @@ static bool start_stringval(upb_json_parser *p) { /* Start a new parser frame: parser frames correspond one-to-one with * handler frames, and string events occur in a sub-frame. */ - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; - inner->name_table = NULL; - inner->is_map = false; - inner->is_mapentry = false; p->top = inner; if (upb_fielddef_type(p->top->f) == UPB_TYPE_STRING) { @@ -13140,9 +13880,68 @@ static bool start_stringval(upb_json_parser *p) { } } -static bool end_stringval(upb_json_parser *p) { +static bool end_any_stringval(upb_json_parser *p) { + size_t len; + const char *buf = accumulate_getptr(p, &len); + + /* Set type_url */ + upb_selector_t sel; + upb_jsonparser_frame *inner; + if (!check_stack(p)) return false; + inner = p->top + 1; + + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); + upb_sink_putstring(&inner->sink, sel, buf, len, NULL); + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(&inner->sink, sel); + + multipart_end(p); + + /* Resolve type url */ + if (strncmp(buf, "type.googleapis.com/", 20) == 0 && len > 20) { + const upb_msgdef *payload_type = NULL; + buf += 20; + len -= 20; + + payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len); + if (payload_type == NULL) { + upb_status_seterrf( + &p->status, "Cannot find packed type: %.*s\n", (int)len, buf); + upb_env_reporterror(p->env, &p->status); + return false; + } + + json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type); + + return true; + } else { + upb_status_seterrf( + &p->status, "Invalid type url: %.*s\n", (int)len, buf); + upb_env_reporterror(p->env, &p->status); + return false; + } +} + +static bool end_stringval_nontop(upb_json_parser *p) { bool ok = true; + if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) || + is_wellknown_msg(p, UPB_WELLKNOWN_DURATION)) { + multipart_end(p); + return true; + } + + if (p->top->f == NULL) { + multipart_end(p); + return true; + } + + if (p->top->is_any) { + return end_any_stringval(p); + } + switch (upb_fielddef_type(p->top->f)) { case UPB_TYPE_BYTES: if (!base64_push(p, getsel_for_handlertype(p, UPB_HANDLER_STRING), @@ -13153,8 +13952,8 @@ static bool end_stringval(upb_json_parser *p) { case UPB_TYPE_STRING: { upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - p->top--; upb_sink_endstr(&p->top->sink, sel); + p->top--; break; } @@ -13202,8 +14001,465 @@ static bool end_stringval(upb_json_parser *p) { return ok; } -static void start_member(upb_json_parser *p) { - UPB_ASSERT(!p->top->f); +static bool end_stringval(upb_json_parser *p) { + /* FieldMask's stringvals have been ended when handling them. Only need to + * close FieldMask here.*/ + if (does_fieldmask_end(p)) { + end_fieldmask_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + if (!end_stringval_nontop(p)) { + return false; + } + + if (does_string_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) || + is_wellknown_msg(p, UPB_WELLKNOWN_DURATION) || + is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) { + end_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + return true; + } + + return true; +} + +static void start_duration_base(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_duration_base(upb_json_parser *p, const char *ptr) { + size_t len; + const char *buf; + char seconds_buf[14]; + char nanos_buf[12]; + char *end; + int64_t seconds = 0; + int32_t nanos = 0; + double val = 0.0; + const char *seconds_membername = "seconds"; + const char *nanos_membername = "nanos"; + size_t fraction_start; + + if (!capture_end(p, ptr)) { + return false; + } + + buf = accumulate_getptr(p, &len); + + memset(seconds_buf, 0, 14); + memset(nanos_buf, 0, 12); + + /* Find out base end. The maximus duration is 315576000000, which cannot be + * represented by double without losing precision. Thus, we need to handle + * fraction and base separately. */ + for (fraction_start = 0; fraction_start < len && buf[fraction_start] != '.'; + fraction_start++); + + /* Parse base */ + memcpy(seconds_buf, buf, fraction_start); + seconds = strtol(seconds_buf, &end, 10); + if (errno == ERANGE || end != seconds_buf + fraction_start) { + upb_status_seterrf(&p->status, "error parsing duration: %s", + seconds_buf); + upb_env_reporterror(p->env, &p->status); + return false; + } + + if (seconds > 315576000000) { + upb_status_seterrf(&p->status, "error parsing duration: " + "maximum acceptable value is " + "315576000000"); + upb_env_reporterror(p->env, &p->status); + return false; + } + + if (seconds < -315576000000) { + upb_status_seterrf(&p->status, "error parsing duration: " + "minimum acceptable value is " + "-315576000000"); + upb_env_reporterror(p->env, &p->status); + return false; + } + + /* Parse fraction */ + nanos_buf[0] = '0'; + memcpy(nanos_buf + 1, buf + fraction_start, len - fraction_start); + val = strtod(nanos_buf, &end); + if (errno == ERANGE || end != nanos_buf + len - fraction_start + 1) { + upb_status_seterrf(&p->status, "error parsing duration: %s", + nanos_buf); + upb_env_reporterror(p->env, &p->status); + return false; + } + + nanos = val * 1000000000; + if (seconds < 0) nanos = -nanos; + + /* Clean up buffer */ + multipart_end(p); + + /* Set seconds */ + start_member(p); + capture_begin(p, seconds_membername); + capture_end(p, seconds_membername + 7); + end_membername(p); + upb_sink_putint64(&p->top->sink, parser_getsel(p), seconds); + end_member(p); + + /* Set nanos */ + start_member(p); + capture_begin(p, nanos_membername); + capture_end(p, nanos_membername + 5); + end_membername(p); + upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); + end_member(p); + + /* Continue previous environment */ + multipart_startaccum(p); + + return true; +} + +static int parse_timestamp_number(upb_json_parser *p) { + size_t len; + const char *buf; + char *end; + int val; + + /* atoi() and friends unfortunately do not support specifying the length of + * the input string, so we need to force a copy into a NULL-terminated buffer. */ + multipart_text(p, "\0", 1, false); + + buf = accumulate_getptr(p, &len); + val = atoi(buf); + multipart_end(p); + multipart_startaccum(p); + + return val; +} + +static void start_year(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_year(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_year = parse_timestamp_number(p) - 1900; + return true; +} + +static void start_month(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_month(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_mon = parse_timestamp_number(p) - 1; + return true; +} + +static void start_day(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_day(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_mday = parse_timestamp_number(p); + return true; +} + +static void start_hour(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_hour(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_hour = parse_timestamp_number(p); + return true; +} + +static void start_minute(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_minute(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_min = parse_timestamp_number(p); + return true; +} + +static void start_second(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_second(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_sec = parse_timestamp_number(p); + return true; +} + +static void start_timestamp_base(upb_json_parser *p) { + memset(&p->tm, 0, sizeof(struct tm)); +} + +static void start_timestamp_fraction(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { + size_t len; + const char *buf; + char nanos_buf[12]; + char *end; + double val = 0.0; + int32_t nanos; + const char *nanos_membername = "nanos"; + + memset(nanos_buf, 0, 12); + + if (!capture_end(p, ptr)) { + return false; + } + + buf = accumulate_getptr(p, &len); + + if (len > 10) { + upb_status_seterrf(&p->status, + "error parsing timestamp: at most 9-digit fraction."); + upb_env_reporterror(p->env, &p->status); + return false; + } + + /* Parse nanos */ + nanos_buf[0] = '0'; + memcpy(nanos_buf + 1, buf, len); + val = strtod(nanos_buf, &end); + + if (errno == ERANGE || end != nanos_buf + len + 1) { + upb_status_seterrf(&p->status, "error parsing timestamp nanos: %s", + nanos_buf); + upb_env_reporterror(p->env, &p->status); + return false; + } + + nanos = val * 1000000000; + + /* Clean up previous environment */ + multipart_end(p); + + /* Set nanos */ + start_member(p); + capture_begin(p, nanos_membername); + capture_end(p, nanos_membername + 5); + end_membername(p); + upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); + end_member(p); + + /* Continue previous environment */ + multipart_startaccum(p); + + return true; +} + +static void start_timestamp_zone(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +#define EPOCH_YEAR 1970 +#define TM_YEAR_BASE 1900 + +static bool isleap(int year) { + return (year % 4) == 0 && (year % 100 != 0 || (year % 400) == 0); +} + +const unsigned short int __mon_yday[2][13] = { + /* Normal years. */ + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, + /* Leap years. */ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } +}; + +int64_t epoch(int year, int yday, int hour, int min, int sec) { + int64_t years = year - EPOCH_YEAR; + + int64_t leap_days = years / 4 - years / 100 + years / 400; + + int64_t days = years * 365 + yday + leap_days; + int64_t hours = days * 24 + hour; + int64_t mins = hours * 60 + min; + int64_t secs = mins * 60 + sec; + return secs; +} + +static int64_t upb_mktime(const struct tm *tp) { + int sec = tp->tm_sec; + int min = tp->tm_min; + int hour = tp->tm_hour; + int mday = tp->tm_mday; + int mon = tp->tm_mon; + int year = tp->tm_year + TM_YEAR_BASE; + + /* Calculate day of year from year, month, and day of month. */ + int mon_yday = ((__mon_yday[isleap(year)][mon]) - 1); + int yday = mon_yday + mday; + + return epoch(year, yday, hour, min, sec); +} + +static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { + size_t len; + const char *buf; + int hours = 0; + int64_t seconds; + const char *seconds_membername = "seconds"; + + if (!capture_end(p, ptr)) { + return false; + } + + buf = accumulate_getptr(p, &len); + + if (buf[0] != 'Z') { + if (sscanf(buf + 1, "%2d:00", &hours) != 1) { + upb_status_seterrf(&p->status, "error parsing timestamp offset"); + upb_env_reporterror(p->env, &p->status); + return false; + } + + if (buf[0] == '+') { + hours = -hours; + } + } + + /* Normalize tm */ + seconds = upb_mktime(&p->tm); + seconds += 3600 * hours; + + /* Check timestamp boundary */ + if (seconds < -62135596800) { + upb_status_seterrf(&p->status, "error parsing timestamp: " + "minimum acceptable value is " + "0001-01-01T00:00:00Z"); + upb_env_reporterror(p->env, &p->status); + return false; + } + + /* Clean up previous environment */ + multipart_end(p); + + /* Set seconds */ + start_member(p); + capture_begin(p, seconds_membername); + capture_end(p, seconds_membername + 7); + end_membername(p); + upb_sink_putint64(&p->top->sink, parser_getsel(p), seconds); + end_member(p); + + /* Continue previous environment */ + multipart_startaccum(p); + + return true; +} + +static void start_fieldmask_path_text(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_fieldmask_path_text(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } +} + +static bool start_fieldmask_path(upb_json_parser *p) { + upb_jsonparser_frame *inner; + upb_selector_t sel; + + if (!check_stack(p)) return false; + + /* Start a new parser frame: parser frames correspond one-to-one with + * handler frames, and string events occur in a sub-frame. */ + inner = start_jsonparser_frame(p); + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + inner->m = p->top->m; + inner->f = p->top->f; + p->top = inner; + + multipart_startaccum(p); + return true; +} + +static bool lower_camel_push( + upb_json_parser *p, upb_selector_t sel, const char *ptr, size_t len) { + const char *limit = ptr + len; + bool first = true; + for (;ptr < limit; ptr++) { + if (*ptr >= 'A' && *ptr <= 'Z' && !first) { + char lower = tolower(*ptr); + upb_sink_putstring(&p->top->sink, sel, "_", 1, NULL); + upb_sink_putstring(&p->top->sink, sel, &lower, 1, NULL); + } else { + upb_sink_putstring(&p->top->sink, sel, ptr, 1, NULL); + } + first = false; + } + return true; +} + +static bool end_fieldmask_path(upb_json_parser *p) { + upb_selector_t sel; + + if (!lower_camel_push( + p, getsel_for_handlertype(p, UPB_HANDLER_STRING), + p->accumulated, p->accumulated_len)) { + return false; + } + + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(&p->top->sink, sel); + p->top--; + + multipart_end(p); + return true; +} + +static void start_member(upb_json_parser *p) { + UPB_ASSERT(!p->top->f); multipart_startaccum(p); } @@ -13261,7 +14517,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); upb_sink_putstring(&subsink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&p->top->sink, sel); + upb_sink_endstr(&subsink, sel); multipart_end(p); break; } @@ -13297,14 +14553,12 @@ static bool handle_mapentry(upb_json_parser *p) { mapfield = p->top->mapfield; mapentrymsg = upb_fielddef_msgsubdef(mapfield); - inner = p->top + 1; + inner = start_jsonparser_frame(p); p->top->f = mapfield; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink); inner->m = mapentrymsg; - inner->name_table = NULL; inner->mapfield = mapfield; - inner->is_map = false; /* Don't set this to true *yet* -- we reuse parsing handlers below to push * the key field value to the sink, and these handlers will pop the frame @@ -13334,7 +14588,15 @@ static bool handle_mapentry(upb_json_parser *p) { static bool end_membername(upb_json_parser *p) { UPB_ASSERT(!p->top->f); - if (p->top->is_map) { + if (!p->top->m) { + p->top->is_unknown_field = true; + multipart_end(p); + return true; + } + + if (p->top->is_any) { + return end_any_membername(p); + } else if (p->top->is_map) { return handle_mapentry(p); } else { size_t len; @@ -13345,10 +14607,12 @@ static bool end_membername(upb_json_parser *p) { p->top->f = upb_value_getconstptr(v); multipart_end(p); + return true; + } else if (p->ignore_json_unknown) { + p->top->is_unknown_field = true; + multipart_end(p); return true; } else { - /* TODO(haberman): Ignore unknown fields if requested/configured to do - * so. */ upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf); upb_env_reporterror(p->env, &p->status); return false; @@ -13356,6 +14620,23 @@ static bool end_membername(upb_json_parser *p) { } } +static bool end_any_membername(upb_json_parser *p) { + size_t len; + const char *buf = accumulate_getptr(p, &len); + upb_value v; + + if (len == 5 && strncmp(buf, "@type", len) == 0) { + upb_strtable_lookup2(p->top->name_table, "type_url", 8, &v); + p->top->f = upb_value_getconstptr(v); + multipart_end(p); + return true; + } else { + p->top->is_unknown_field = true; + multipart_end(p); + return true; + } +} + static void end_member(upb_json_parser *p) { /* If we just parsed a map-entry value, end that frame too. */ if (p->top->is_mapentry) { @@ -13377,10 +14658,27 @@ static void end_member(upb_json_parser *p) { } p->top->f = NULL; + p->top->is_unknown_field = false; +} + +static void start_any_member(upb_json_parser *p, const char *ptr) { + start_member(p); + json_parser_any_frame_set_after_type_url_start_once(p->top->any_frame, ptr); +} + +static void end_any_member(upb_json_parser *p, const char *ptr) { + json_parser_any_frame_set_before_type_url_end(p->top->any_frame, ptr); + end_member(p); } static bool start_subobject(upb_json_parser *p) { - UPB_ASSERT(p->top->f); + if (p->top->is_unknown_field) { + upb_jsonparser_frame *inner; + if (!check_stack(p)) return false; + + p->top = start_jsonparser_frame(p); + return true; + } if (upb_fielddef_ismap(p->top->f)) { upb_jsonparser_frame *inner; @@ -13390,15 +14688,12 @@ static bool start_subobject(upb_json_parser *p) { * context. */ if (!check_stack(p)) return false; - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); upb_sink_startseq(&p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); - inner->name_table = NULL; inner->mapfield = p->top->f; - inner->f = NULL; inner->is_map = true; - inner->is_mapentry = false; p->top = inner; return true; @@ -13410,17 +14705,23 @@ static bool start_subobject(upb_json_parser *p) { * context. */ if (!check_stack(p)) return false; - inner = p->top + 1; - + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); set_name_table(p, inner); - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; p->top = inner; + if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { + p->top->is_any = true; + p->top->any_frame = + upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + json_parser_any_frame_reset(p->top->any_frame); + } else { + p->top->is_any = false; + p->top->any_frame = NULL; + } + return true; } else { upb_status_seterrf(&p->status, @@ -13431,7 +14732,35 @@ static bool start_subobject(upb_json_parser *p) { } } +static bool start_subobject_full(upb_json_parser *p) { + if (is_top_level(p)) { + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_STRUCTVALUE); + if (!start_subobject(p)) return false; + start_structvalue_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_STRUCT)) { + start_structvalue_object(p); + } else { + return true; + } + } else if (is_wellknown_field(p, UPB_WELLKNOWN_STRUCT)) { + if (!start_subobject(p)) return false; + start_structvalue_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + if (!start_subobject(p)) return false; + start_value_object(p, VALUE_STRUCTVALUE); + if (!start_subobject(p)) return false; + start_structvalue_object(p); + } + + return start_subobject(p); +} + static void end_subobject(upb_json_parser *p) { + if (is_top_level(p)) { + return; + } + if (p->top->is_map) { upb_selector_t sel; p->top--; @@ -13439,9 +14768,30 @@ static void end_subobject(upb_json_parser *p) { upb_sink_endseq(&p->top->sink, sel); } else { upb_selector_t sel; + bool is_unknown = p->top->m == NULL; p->top--; - sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG); - upb_sink_endsubmsg(&p->top->sink, sel); + if (!is_unknown) { + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG); + upb_sink_endsubmsg(&p->top->sink, sel); + } + } +} + +static void end_subobject_full(upb_json_parser *p) { + end_subobject(p); + + if (is_wellknown_msg(p, UPB_WELLKNOWN_STRUCT)) { + end_structvalue_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } } } @@ -13449,7 +14799,37 @@ static bool start_array(upb_json_parser *p) { upb_jsonparser_frame *inner; upb_selector_t sel; - UPB_ASSERT(p->top->f); + if (is_top_level(p)) { + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + start_value_object(p, VALUE_LISTVALUE); + if (!start_subobject(p)) return false; + start_listvalue_object(p); + } else if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) { + start_listvalue_object(p); + } else { + return false; + } + } else if (is_wellknown_field(p, UPB_WELLKNOWN_LISTVALUE) && + (!upb_fielddef_isseq(p->top->f) || + p->top->is_repeated)) { + if (!start_subobject(p)) return false; + start_listvalue_object(p); + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE) && + (!upb_fielddef_isseq(p->top->f) || + p->top->is_repeated)) { + if (!start_subobject(p)) return false; + start_value_object(p, VALUE_LISTVALUE); + if (!start_subobject(p)) return false; + start_listvalue_object(p); + } + + if (p->top->is_unknown_field) { + inner = start_jsonparser_frame(p); + inner->is_unknown_field = true; + p->top = inner; + + return true; + } if (!upb_fielddef_isseq(p->top->f)) { upb_status_seterrf(&p->status, @@ -13461,14 +14841,12 @@ static bool start_array(upb_json_parser *p) { if (!check_stack(p)) return false; - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); upb_sink_startseq(&p->top->sink, sel, &inner->sink); inner->m = p->top->m; - inner->name_table = NULL; inner->f = p->top->f; - inner->is_map = false; - inner->is_mapentry = false; + inner->is_repeated = true; p->top = inner; return true; @@ -13480,18 +14858,37 @@ static void end_array(upb_json_parser *p) { UPB_ASSERT(p->top > p->stack); p->top--; + + if (p->top->is_unknown_field) { + return; + } + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ); upb_sink_endseq(&p->top->sink, sel); + + if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) { + end_listvalue_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) { + end_value_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } } static void start_object(upb_json_parser *p) { - if (!p->top->is_map) { + if (!p->top->is_map && p->top->m != NULL) { upb_sink_startmsg(&p->top->sink); } } static void end_object(upb_json_parser *p) { - if (!p->top->is_map) { + if (!p->top->is_map && p->top->m != NULL) { upb_status status; upb_status_clear(&status); upb_sink_endmsg(&p->top->sink, &status); @@ -13501,6 +14898,311 @@ static void end_object(upb_json_parser *p) { } } +static void start_any_object(upb_json_parser *p, const char *ptr) { + start_object(p); + p->top->any_frame->before_type_url_start = ptr; + p->top->any_frame->before_type_url_end = ptr; +} + +static bool end_any_object(upb_json_parser *p, const char *ptr) { + const char *value_membername = "value"; + bool is_well_known_packed = false; + const char *packed_end = ptr + 1; + upb_selector_t sel; + upb_jsonparser_frame *inner; + + if (json_parser_any_frame_has_value(p->top->any_frame) && + !json_parser_any_frame_has_type_url(p->top->any_frame)) { + upb_status_seterrmsg(&p->status, "No valid type url"); + upb_env_reporterror(p->env, &p->status); + return false; + } + + /* Well known types data is represented as value field. */ + if (upb_msgdef_wellknowntype(p->top->any_frame->parser->top->m) != + UPB_WELLKNOWN_UNSPECIFIED) { + is_well_known_packed = true; + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) { + p->top->any_frame->before_type_url_start = + memchr(p->top->any_frame->before_type_url_start, ':', + p->top->any_frame->before_type_url_end - + p->top->any_frame->before_type_url_start); + if (p->top->any_frame->before_type_url_start == NULL) { + upb_status_seterrmsg(&p->status, "invalid data for well known type."); + upb_env_reporterror(p->env, &p->status); + return false; + } + p->top->any_frame->before_type_url_start++; + } + + if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + p->top->any_frame->after_type_url_start = + memchr(p->top->any_frame->after_type_url_start, ':', + (ptr + 1) - + p->top->any_frame->after_type_url_start); + if (p->top->any_frame->after_type_url_start == NULL) { + upb_status_seterrmsg(&p->status, "Invalid data for well known type."); + upb_env_reporterror(p->env, &p->status); + return false; + } + p->top->any_frame->after_type_url_start++; + packed_end = ptr; + } + } + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, + p->top->any_frame->before_type_url_start, + p->top->any_frame->before_type_url_end - + p->top->any_frame->before_type_url_start, NULL)) { + return false; + } + } else { + if (!is_well_known_packed) { + if (!parse(p->top->any_frame->parser, NULL, "{", 1, NULL)) { + return false; + } + } + } + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame) && + json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, ",", 1, NULL)) { + return false; + } + } + + if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, + p->top->any_frame->after_type_url_start, + packed_end - p->top->any_frame->after_type_url_start, NULL)) { + return false; + } + } else { + if (!is_well_known_packed) { + if (!parse(p->top->any_frame->parser, NULL, "}", 1, NULL)) { + return false; + } + } + } + + if (!end(p->top->any_frame->parser, NULL)) { + return false; + } + + p->top->is_any = false; + + /* Set value */ + start_member(p); + capture_begin(p, value_membername); + capture_end(p, value_membername + 5); + end_membername(p); + + if (!check_stack(p)) return false; + inner = p->top + 1; + + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); + upb_sink_putstring(&inner->sink, sel, p->top->any_frame->stringsink.ptr, + p->top->any_frame->stringsink.len, NULL); + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(&inner->sink, sel); + + end_member(p); + + end_object(p); + + /* Deallocate any parse frame. */ + json_parser_any_frame_free(p->top->any_frame); + upb_env_free(p->env, p->top->any_frame); + + return true; +} + +static bool is_string_wrapper(const upb_msgdef *m) { + upb_wellknowntype_t type = upb_msgdef_wellknowntype(m); + return type == UPB_WELLKNOWN_STRINGVALUE || + type == UPB_WELLKNOWN_BYTESVALUE; +} + +static bool is_fieldmask(const upb_msgdef *m) { + upb_wellknowntype_t type = upb_msgdef_wellknowntype(m); + return type == UPB_WELLKNOWN_FIELDMASK; +} + +static void start_fieldmask_object(upb_json_parser *p) { + const char *membername = "paths"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + 5); + end_membername(p); + + start_array(p); +} + +static void end_fieldmask_object(upb_json_parser *p) { + end_array(p); + end_member(p); + end_object(p); +} + +static void start_wrapper_object(upb_json_parser *p) { + const char *membername = "value"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + 5); + end_membername(p); +} + +static void end_wrapper_object(upb_json_parser *p) { + end_member(p); + end_object(p); +} + +static void start_value_object(upb_json_parser *p, int value_type) { + const char *nullmember = "null_value"; + const char *numbermember = "number_value"; + const char *stringmember = "string_value"; + const char *boolmember = "bool_value"; + const char *structmember = "struct_value"; + const char *listmember = "list_value"; + const char *membername = ""; + + switch (value_type) { + case VALUE_NULLVALUE: + membername = nullmember; + break; + case VALUE_NUMBERVALUE: + membername = numbermember; + break; + case VALUE_STRINGVALUE: + membername = stringmember; + break; + case VALUE_BOOLVALUE: + membername = boolmember; + break; + case VALUE_STRUCTVALUE: + membername = structmember; + break; + case VALUE_LISTVALUE: + membername = listmember; + break; + } + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + strlen(membername)); + end_membername(p); +} + +static void end_value_object(upb_json_parser *p) { + end_member(p); + end_object(p); +} + +static void start_listvalue_object(upb_json_parser *p) { + const char *membername = "values"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + strlen(membername)); + end_membername(p); +} + +static void end_listvalue_object(upb_json_parser *p) { + end_member(p); + end_object(p); +} + +static void start_structvalue_object(upb_json_parser *p) { + const char *membername = "fields"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + strlen(membername)); + end_membername(p); +} + +static void end_structvalue_object(upb_json_parser *p) { + end_member(p); + end_object(p); +} + +static bool is_top_level(upb_json_parser *p) { + return p->top == p->stack && p->top->f == NULL && !p->top->is_unknown_field; +} + +static bool is_wellknown_msg(upb_json_parser *p, upb_wellknowntype_t type) { + return p->top->m != NULL && upb_msgdef_wellknowntype(p->top->m) == type; +} + +static bool is_wellknown_field(upb_json_parser *p, upb_wellknowntype_t type) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + (upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(p->top->f)) + == type); +} + +static bool does_number_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + upb_msgdef_isnumberwrapper(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_number_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && upb_msgdef_isnumberwrapper(p->top->m); +} + +static bool is_number_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && upb_msgdef_isnumberwrapper(p->top->m); +} + +static bool does_string_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_string_wrapper(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_string_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_string_wrapper(p->top->m); +} + +static bool is_string_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_string_wrapper(p->top->m); +} + +static bool does_fieldmask_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_fieldmask(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_fieldmask_end(upb_json_parser *p) { + return p->top->m != NULL && is_fieldmask(p->top->m); +} + +static bool is_fieldmask_object(upb_json_parser *p) { + return p->top->m != NULL && is_fieldmask(p->top->m); +} #define CHECK_RETURN_TOP(x) if (!(x)) goto error @@ -13523,160 +15225,266 @@ static void end_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 1310 "upb/json/parser.rl" +#line 2789 "upb/json/parser.rl" -#line 1222 "upb/json/parser.c" +#line 2592 "upb/json/parser.c" static const char _json_actions[] = { - 0, 1, 0, 1, 2, 1, 3, 1, - 5, 1, 6, 1, 7, 1, 8, 1, - 10, 1, 12, 1, 13, 1, 14, 1, - 15, 1, 16, 1, 17, 1, 21, 1, - 25, 1, 27, 2, 3, 8, 2, 4, - 5, 2, 6, 2, 2, 6, 8, 2, - 11, 9, 2, 13, 15, 2, 14, 15, - 2, 18, 1, 2, 19, 27, 2, 20, - 9, 2, 22, 27, 2, 23, 27, 2, - 24, 27, 2, 26, 27, 3, 14, 11, - 9 + 0, 1, 0, 1, 1, 1, 3, 1, + 4, 1, 6, 1, 7, 1, 8, 1, + 9, 1, 11, 1, 12, 1, 13, 1, + 14, 1, 15, 1, 16, 1, 17, 1, + 18, 1, 19, 1, 20, 1, 22, 1, + 23, 1, 24, 1, 35, 1, 37, 1, + 39, 1, 40, 1, 42, 1, 43, 1, + 44, 1, 46, 1, 48, 1, 49, 1, + 50, 1, 51, 1, 53, 1, 54, 2, + 4, 9, 2, 5, 6, 2, 7, 3, + 2, 7, 9, 2, 21, 26, 2, 25, + 10, 2, 27, 28, 2, 29, 30, 2, + 32, 34, 2, 33, 31, 2, 38, 36, + 2, 40, 42, 2, 45, 2, 2, 46, + 54, 2, 47, 36, 2, 49, 54, 2, + 50, 54, 2, 51, 54, 2, 52, 41, + 2, 53, 54, 3, 32, 34, 35, 4, + 21, 26, 27, 28 }; -static const unsigned char _json_key_offsets[] = { - 0, 0, 4, 9, 14, 15, 19, 24, - 29, 34, 38, 42, 45, 48, 50, 54, - 58, 60, 62, 67, 69, 71, 80, 86, - 92, 98, 104, 106, 115, 116, 116, 116, - 121, 126, 131, 132, 133, 134, 135, 135, - 136, 137, 138, 138, 139, 140, 141, 141, - 146, 151, 152, 156, 161, 166, 171, 175, - 175, 178, 178, 178 +static const short _json_key_offsets[] = { + 0, 0, 12, 13, 18, 23, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, + 38, 43, 44, 48, 53, 58, 63, 67, + 71, 74, 77, 79, 83, 87, 89, 91, + 96, 98, 100, 109, 115, 121, 127, 133, + 135, 139, 142, 144, 146, 149, 150, 154, + 156, 158, 160, 162, 163, 165, 167, 168, + 170, 172, 173, 175, 177, 178, 180, 182, + 183, 185, 187, 191, 193, 195, 196, 197, + 198, 199, 201, 206, 208, 210, 212, 221, + 222, 222, 222, 227, 232, 237, 238, 239, + 240, 241, 241, 242, 243, 244, 244, 245, + 246, 247, 247, 252, 253, 257, 262, 267, + 272, 276, 276, 279, 282, 285, 288, 291, + 294, 294, 294, 294, 294, 294 }; static const char _json_trans_keys[] = { - 32, 123, 9, 13, 32, 34, 125, 9, - 13, 32, 34, 125, 9, 13, 34, 32, - 58, 9, 13, 32, 93, 125, 9, 13, - 32, 44, 125, 9, 13, 32, 44, 125, - 9, 13, 32, 34, 9, 13, 45, 48, - 49, 57, 48, 49, 57, 46, 69, 101, - 48, 57, 69, 101, 48, 57, 43, 45, - 48, 57, 48, 57, 48, 57, 46, 69, - 101, 48, 57, 34, 92, 34, 92, 34, - 47, 92, 98, 102, 110, 114, 116, 117, - 48, 57, 65, 70, 97, 102, 48, 57, - 65, 70, 97, 102, 48, 57, 65, 70, - 97, 102, 48, 57, 65, 70, 97, 102, - 34, 92, 34, 45, 91, 102, 110, 116, - 123, 48, 57, 34, 32, 93, 125, 9, - 13, 32, 44, 93, 9, 13, 32, 93, - 125, 9, 13, 97, 108, 115, 101, 117, - 108, 108, 114, 117, 101, 32, 34, 125, - 9, 13, 32, 34, 125, 9, 13, 34, - 32, 58, 9, 13, 32, 93, 125, 9, - 13, 32, 44, 125, 9, 13, 32, 44, - 125, 9, 13, 32, 34, 9, 13, 32, - 9, 13, 0 + 32, 34, 45, 91, 102, 110, 116, 123, + 9, 13, 48, 57, 34, 32, 93, 125, + 9, 13, 32, 44, 93, 9, 13, 32, + 93, 125, 9, 13, 97, 108, 115, 101, + 117, 108, 108, 114, 117, 101, 32, 34, + 125, 9, 13, 34, 32, 58, 9, 13, + 32, 93, 125, 9, 13, 32, 44, 125, + 9, 13, 32, 44, 125, 9, 13, 32, + 34, 9, 13, 45, 48, 49, 57, 48, + 49, 57, 46, 69, 101, 48, 57, 69, + 101, 48, 57, 43, 45, 48, 57, 48, + 57, 48, 57, 46, 69, 101, 48, 57, + 34, 92, 34, 92, 34, 47, 92, 98, + 102, 110, 114, 116, 117, 48, 57, 65, + 70, 97, 102, 48, 57, 65, 70, 97, + 102, 48, 57, 65, 70, 97, 102, 48, + 57, 65, 70, 97, 102, 34, 92, 45, + 48, 49, 57, 48, 49, 57, 46, 115, + 48, 57, 115, 48, 57, 34, 46, 115, + 48, 57, 48, 57, 48, 57, 48, 57, + 48, 57, 45, 48, 57, 48, 57, 45, + 48, 57, 48, 57, 84, 48, 57, 48, + 57, 58, 48, 57, 48, 57, 58, 48, + 57, 48, 57, 43, 45, 46, 90, 48, + 57, 48, 57, 58, 48, 48, 34, 48, + 57, 43, 45, 90, 48, 57, 34, 44, + 34, 44, 34, 44, 34, 45, 91, 102, + 110, 116, 123, 48, 57, 34, 32, 93, + 125, 9, 13, 32, 44, 93, 9, 13, + 32, 93, 125, 9, 13, 97, 108, 115, + 101, 117, 108, 108, 114, 117, 101, 32, + 34, 125, 9, 13, 34, 32, 58, 9, + 13, 32, 93, 125, 9, 13, 32, 44, + 125, 9, 13, 32, 44, 125, 9, 13, + 32, 34, 9, 13, 32, 9, 13, 32, + 9, 13, 32, 9, 13, 32, 9, 13, + 32, 9, 13, 32, 9, 13, 0 }; static const char _json_single_lengths[] = { - 0, 2, 3, 3, 1, 2, 3, 3, - 3, 2, 2, 1, 3, 0, 2, 2, - 0, 0, 3, 2, 2, 9, 0, 0, - 0, 0, 2, 7, 1, 0, 0, 3, - 3, 3, 1, 1, 1, 1, 0, 1, - 1, 1, 0, 1, 1, 1, 0, 3, - 3, 1, 2, 3, 3, 3, 2, 0, - 1, 0, 0, 0 + 0, 8, 1, 3, 3, 3, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 3, 1, 2, 3, 3, 3, 2, 2, + 1, 3, 0, 2, 2, 0, 0, 3, + 2, 2, 9, 0, 0, 0, 0, 2, + 2, 1, 2, 0, 1, 1, 2, 0, + 0, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, + 0, 0, 4, 0, 0, 1, 1, 1, + 1, 0, 3, 2, 2, 2, 7, 1, + 0, 0, 3, 3, 3, 1, 1, 1, + 1, 0, 1, 1, 1, 0, 1, 1, + 1, 0, 3, 1, 2, 3, 3, 3, + 2, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0 }; static const char _json_range_lengths[] = { - 0, 1, 1, 1, 0, 1, 1, 1, - 1, 1, 1, 1, 0, 1, 1, 1, - 1, 1, 1, 0, 0, 0, 3, 3, - 3, 3, 0, 1, 0, 0, 0, 1, - 1, 1, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1, - 1, 0, 1, 1, 1, 1, 1, 0, - 1, 0, 0, 0 + 0, 2, 0, 1, 1, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 3, 3, 3, 3, 0, + 1, 1, 0, 1, 1, 0, 1, 1, + 1, 1, 1, 0, 1, 1, 0, 1, + 1, 0, 1, 1, 0, 1, 1, 0, + 1, 1, 0, 1, 1, 0, 0, 0, + 0, 1, 1, 0, 0, 0, 1, 0, + 0, 0, 1, 1, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 1, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0 }; static const short _json_index_offsets[] = { - 0, 0, 4, 9, 14, 16, 20, 25, - 30, 35, 39, 43, 46, 50, 52, 56, - 60, 62, 64, 69, 72, 75, 85, 89, - 93, 97, 101, 104, 113, 115, 116, 117, - 122, 127, 132, 134, 136, 138, 140, 141, - 143, 145, 147, 148, 150, 152, 154, 155, - 160, 165, 167, 171, 176, 181, 186, 190, - 191, 194, 195, 196 + 0, 0, 11, 13, 18, 23, 28, 30, + 32, 34, 36, 38, 40, 42, 44, 46, + 48, 53, 55, 59, 64, 69, 74, 78, + 82, 85, 89, 91, 95, 99, 101, 103, + 108, 111, 114, 124, 128, 132, 136, 140, + 143, 147, 150, 153, 155, 158, 160, 164, + 166, 168, 170, 172, 174, 176, 178, 180, + 182, 184, 186, 188, 190, 192, 194, 196, + 198, 200, 202, 207, 209, 211, 213, 215, + 217, 219, 221, 226, 229, 232, 235, 244, + 246, 247, 248, 253, 258, 263, 265, 267, + 269, 271, 272, 274, 276, 278, 279, 281, + 283, 285, 286, 291, 293, 297, 302, 307, + 312, 316, 317, 320, 323, 326, 329, 332, + 335, 336, 337, 338, 339, 340 }; -static const char _json_indicies[] = { - 0, 2, 0, 1, 3, 4, 5, 3, - 1, 6, 7, 8, 6, 1, 9, 1, - 10, 11, 10, 1, 11, 1, 1, 11, - 12, 13, 14, 15, 13, 1, 16, 17, - 8, 16, 1, 17, 7, 17, 1, 18, - 19, 20, 1, 19, 20, 1, 22, 23, - 23, 21, 24, 1, 23, 23, 24, 21, - 25, 25, 26, 1, 26, 1, 26, 21, - 22, 23, 23, 20, 21, 28, 29, 27, - 31, 32, 30, 33, 33, 33, 33, 33, - 33, 33, 33, 34, 1, 35, 35, 35, - 1, 36, 36, 36, 1, 37, 37, 37, - 1, 38, 38, 38, 1, 40, 41, 39, - 42, 43, 44, 45, 46, 47, 48, 43, - 1, 49, 1, 50, 51, 53, 54, 1, - 53, 52, 55, 56, 54, 55, 1, 56, - 1, 1, 56, 52, 57, 1, 58, 1, - 59, 1, 60, 1, 61, 62, 1, 63, - 1, 64, 1, 65, 66, 1, 67, 1, - 68, 1, 69, 70, 71, 72, 70, 1, - 73, 74, 75, 73, 1, 76, 1, 77, - 78, 77, 1, 78, 1, 1, 78, 79, - 80, 81, 82, 80, 1, 83, 84, 75, - 83, 1, 84, 74, 84, 1, 85, 86, - 86, 1, 1, 1, 1, 0 +static const unsigned char _json_indicies[] = { + 0, 2, 3, 4, 5, 6, 7, 8, + 0, 3, 1, 9, 1, 11, 12, 1, + 11, 10, 13, 14, 12, 13, 1, 14, + 1, 1, 14, 10, 15, 1, 16, 1, + 17, 1, 18, 1, 19, 1, 20, 1, + 21, 1, 22, 1, 23, 1, 24, 1, + 25, 26, 27, 25, 1, 28, 1, 29, + 30, 29, 1, 30, 1, 1, 30, 31, + 32, 33, 34, 32, 1, 35, 36, 27, + 35, 1, 36, 26, 36, 1, 37, 38, + 39, 1, 38, 39, 1, 41, 42, 42, + 40, 43, 1, 42, 42, 43, 40, 44, + 44, 45, 1, 45, 1, 45, 40, 41, + 42, 42, 39, 40, 47, 48, 46, 50, + 51, 49, 52, 52, 52, 52, 52, 52, + 52, 52, 53, 1, 54, 54, 54, 1, + 55, 55, 55, 1, 56, 56, 56, 1, + 57, 57, 57, 1, 59, 60, 58, 61, + 62, 63, 1, 64, 65, 1, 66, 67, + 1, 68, 1, 67, 68, 1, 69, 1, + 66, 67, 65, 1, 70, 1, 71, 1, + 72, 1, 73, 1, 74, 1, 75, 1, + 76, 1, 77, 1, 78, 1, 79, 1, + 80, 1, 81, 1, 82, 1, 83, 1, + 84, 1, 85, 1, 86, 1, 87, 1, + 88, 1, 89, 89, 90, 91, 1, 92, + 1, 93, 1, 94, 1, 95, 1, 96, + 1, 97, 1, 98, 1, 99, 99, 100, + 98, 1, 102, 1, 101, 104, 105, 103, + 1, 1, 101, 106, 107, 108, 109, 110, + 111, 112, 107, 1, 113, 1, 114, 115, + 117, 118, 1, 117, 116, 119, 120, 118, + 119, 1, 120, 1, 1, 120, 116, 121, + 1, 122, 1, 123, 1, 124, 1, 125, + 126, 1, 127, 1, 128, 1, 129, 130, + 1, 131, 1, 132, 1, 133, 134, 135, + 136, 134, 1, 137, 1, 138, 139, 138, + 1, 139, 1, 1, 139, 140, 141, 142, + 143, 141, 1, 144, 145, 136, 144, 1, + 145, 135, 145, 1, 146, 147, 147, 1, + 148, 148, 1, 149, 149, 1, 150, 150, + 1, 151, 151, 1, 152, 152, 1, 1, + 1, 1, 1, 1, 1, 0 }; static const char _json_trans_targs[] = { - 1, 0, 2, 3, 4, 56, 3, 4, - 56, 5, 5, 6, 7, 8, 9, 56, - 8, 9, 11, 12, 18, 57, 13, 15, - 14, 16, 17, 20, 58, 21, 20, 58, - 21, 19, 22, 23, 24, 25, 26, 20, - 58, 21, 28, 30, 31, 34, 39, 43, - 47, 29, 59, 59, 32, 31, 29, 32, - 33, 35, 36, 37, 38, 59, 40, 41, - 42, 59, 44, 45, 46, 59, 48, 49, - 55, 48, 49, 55, 50, 50, 51, 52, - 53, 54, 55, 53, 54, 59, 56 + 1, 0, 2, 107, 3, 6, 10, 13, + 16, 106, 4, 3, 106, 4, 5, 7, + 8, 9, 108, 11, 12, 109, 14, 15, + 110, 16, 17, 111, 18, 18, 19, 20, + 21, 22, 111, 21, 22, 24, 25, 31, + 112, 26, 28, 27, 29, 30, 33, 113, + 34, 33, 113, 34, 32, 35, 36, 37, + 38, 39, 33, 113, 34, 41, 42, 46, + 42, 46, 43, 45, 44, 114, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 65, + 66, 67, 73, 72, 68, 69, 70, 71, + 72, 115, 74, 67, 72, 76, 116, 76, + 116, 77, 79, 81, 82, 85, 90, 94, + 98, 80, 117, 117, 83, 82, 80, 83, + 84, 86, 87, 88, 89, 117, 91, 92, + 93, 117, 95, 96, 97, 117, 98, 99, + 105, 100, 100, 101, 102, 103, 104, 105, + 103, 104, 117, 106, 106, 106, 106, 106, + 106 }; -static const char _json_trans_actions[] = { - 0, 0, 0, 21, 77, 53, 0, 47, - 23, 17, 0, 0, 15, 19, 19, 50, - 0, 0, 0, 0, 0, 1, 0, 0, - 0, 0, 0, 3, 13, 0, 0, 35, - 5, 11, 0, 38, 7, 7, 7, 41, - 44, 9, 62, 56, 25, 0, 0, 0, - 31, 29, 33, 59, 15, 0, 27, 0, - 0, 0, 0, 0, 0, 68, 0, 0, - 0, 71, 0, 0, 0, 65, 21, 77, - 53, 0, 47, 23, 17, 0, 0, 15, - 19, 19, 50, 0, 0, 74, 0 +static const unsigned char _json_trans_actions[] = { + 0, 0, 113, 107, 53, 0, 0, 0, + 125, 59, 45, 0, 55, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 101, 51, 47, 0, 0, 45, + 49, 49, 104, 0, 0, 0, 0, 0, + 3, 0, 0, 0, 0, 0, 5, 15, + 0, 0, 71, 7, 13, 0, 74, 9, + 9, 9, 77, 80, 11, 37, 37, 37, + 0, 0, 0, 39, 0, 41, 86, 0, + 0, 0, 17, 19, 0, 21, 23, 0, + 25, 27, 0, 29, 31, 0, 33, 35, + 0, 135, 83, 135, 0, 0, 0, 0, + 0, 92, 0, 89, 89, 98, 43, 0, + 131, 95, 113, 107, 53, 0, 0, 0, + 125, 59, 69, 110, 45, 0, 55, 0, + 0, 0, 0, 0, 0, 119, 0, 0, + 0, 122, 0, 0, 0, 116, 0, 101, + 51, 47, 0, 0, 45, 49, 49, 104, + 0, 0, 128, 0, 57, 63, 65, 61, + 67 +}; + +static const unsigned char _json_eof_actions[] = { + 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, 0, 1, 0, 0, 1, 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, 57, 63, 65, 61, 67, + 0, 0, 0, 0, 0, 0 }; static const int json_start = 1; -static const int json_en_number_machine = 10; -static const int json_en_string_machine = 19; -static const int json_en_value_machine = 27; +static const int json_en_number_machine = 23; +static const int json_en_string_machine = 32; +static const int json_en_duration_machine = 40; +static const int json_en_timestamp_machine = 47; +static const int json_en_fieldmask_machine = 75; +static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 1313 "upb/json/parser.rl" +#line 2792 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -13689,6 +15497,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, const char *p = buf; const char *pe = buf + size; + const char *eof = &eof_ch; parser->handle = handle; @@ -13698,7 +15507,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 1393 "upb/json/parser.c" +#line 2870 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -13772,159 +15581,339 @@ _match: { switch ( *_acts++ ) { - case 0: -#line 1225 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } - break; case 1: -#line 1226 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 10; goto _again;} } +#line 2597 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 1230 "upb/json/parser.rl" - { start_text(parser, p); } +#line 2599 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 1231 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_text(parser, p)); } +#line 2603 "upb/json/parser.rl" + { start_text(parser, p); } break; case 4: -#line 1237 "upb/json/parser.rl" - { start_hex(parser); } +#line 2604 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 1238 "upb/json/parser.rl" - { hexdigit(parser, p); } +#line 2610 "upb/json/parser.rl" + { start_hex(parser); } break; case 6: -#line 1239 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_hex(parser)); } +#line 2611 "upb/json/parser.rl" + { hexdigit(parser, p); } break; case 7: -#line 1245 "upb/json/parser.rl" - { CHECK_RETURN_TOP(escape(parser, p)); } +#line 2612 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 1251 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } +#line 2618 "upb/json/parser.rl" + { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 1254 "upb/json/parser.rl" - { {stack[top++] = cs; cs = 19; goto _again;} } +#line 2624 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 1256 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 27; goto _again;} } +#line 2629 "upb/json/parser.rl" + { start_year(parser, p); } break; case 11: -#line 1261 "upb/json/parser.rl" - { start_member(parser); } +#line 2630 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_year(parser, p)); } break; case 12: -#line 1262 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_membername(parser)); } +#line 2634 "upb/json/parser.rl" + { start_month(parser, p); } break; case 13: -#line 1265 "upb/json/parser.rl" - { end_member(parser); } +#line 2635 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_month(parser, p)); } break; case 14: -#line 1271 "upb/json/parser.rl" - { start_object(parser); } +#line 2639 "upb/json/parser.rl" + { start_day(parser, p); } break; case 15: -#line 1274 "upb/json/parser.rl" - { end_object(parser); } +#line 2640 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_day(parser, p)); } break; case 16: -#line 1280 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_array(parser)); } +#line 2644 "upb/json/parser.rl" + { start_hour(parser, p); } break; case 17: -#line 1284 "upb/json/parser.rl" - { end_array(parser); } +#line 2645 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_hour(parser, p)); } break; case 18: -#line 1289 "upb/json/parser.rl" - { start_number(parser, p); } +#line 2649 "upb/json/parser.rl" + { start_minute(parser, p); } break; case 19: -#line 1290 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_number(parser, p)); } +#line 2650 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_minute(parser, p)); } break; case 20: -#line 1292 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_stringval(parser)); } +#line 2654 "upb/json/parser.rl" + { start_second(parser, p); } break; case 21: -#line 1293 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_stringval(parser)); } +#line 2655 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_second(parser, p)); } break; case 22: -#line 1295 "upb/json/parser.rl" - { CHECK_RETURN_TOP(parser_putbool(parser, true)); } +#line 2660 "upb/json/parser.rl" + { start_duration_base(parser, p); } break; case 23: -#line 1297 "upb/json/parser.rl" - { CHECK_RETURN_TOP(parser_putbool(parser, false)); } +#line 2661 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 24: -#line 1299 "upb/json/parser.rl" - { /* null value */ } +#line 2663 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 1301 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_subobject(parser)); } +#line 2668 "upb/json/parser.rl" + { start_timestamp_base(parser); } break; case 26: -#line 1302 "upb/json/parser.rl" - { end_subobject(parser); } +#line 2670 "upb/json/parser.rl" + { start_timestamp_fraction(parser, p); } break; case 27: -#line 1307 "upb/json/parser.rl" +#line 2671 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } + break; + case 28: +#line 2673 "upb/json/parser.rl" + { start_timestamp_zone(parser, p); } + break; + case 29: +#line 2674 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } + break; + case 30: +#line 2676 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 1579 "upb/json/parser.c" - } - } - -_again: - if ( cs == 0 ) - goto _out; - if ( ++p != pe ) - goto _resume; - _test_eof: {} - _out: {} - } - -#line 1334 "upb/json/parser.rl" - - if (p != pe) { - upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); - upb_env_reporterror(parser->env, &parser->status); - } else { - capture_suspend(parser, &p); - } - -error: - /* Save parsing state back to parser. */ - parser->current_state = cs; - parser->parser_top = top; - - return p - buf; -} - -bool end(void *closure, const void *hd) { - UPB_UNUSED(closure); - UPB_UNUSED(hd); - - /* Prevent compile warning on unused static constants. */ - UPB_UNUSED(json_start); - UPB_UNUSED(json_en_number_machine); - UPB_UNUSED(json_en_string_machine); - UPB_UNUSED(json_en_value_machine); - UPB_UNUSED(json_en_main); - return true; + case 31: +#line 2681 "upb/json/parser.rl" + { start_fieldmask_path_text(parser, p); } + break; + case 32: +#line 2682 "upb/json/parser.rl" + { end_fieldmask_path_text(parser, p); } + break; + case 33: +#line 2687 "upb/json/parser.rl" + { start_fieldmask_path(parser); } + break; + case 34: +#line 2688 "upb/json/parser.rl" + { end_fieldmask_path(parser); } + break; + case 35: +#line 2694 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } + break; + case 36: +#line 2699 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { + {stack[top++] = cs; cs = 47;goto _again;} + } else if (is_wellknown_msg(parser, UPB_WELLKNOWN_DURATION)) { + {stack[top++] = cs; cs = 40;goto _again;} + } else if (is_wellknown_msg(parser, UPB_WELLKNOWN_FIELDMASK)) { + {stack[top++] = cs; cs = 75;goto _again;} + } else { + {stack[top++] = cs; cs = 32;goto _again;} + } + } + break; + case 37: +#line 2712 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 78;goto _again;} } + break; + case 38: +#line 2717 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + start_any_member(parser, p); + } else { + start_member(parser); + } + } + break; + case 39: +#line 2724 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_membername(parser)); } + break; + case 40: +#line 2727 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + end_any_member(parser, p); + } else { + end_member(parser); + } + } + break; + case 41: +#line 2738 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + start_any_object(parser, p); + } else { + start_object(parser); + } + } + break; + case 42: +#line 2747 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + CHECK_RETURN_TOP(end_any_object(parser, p)); + } else { + end_object(parser); + } + } + break; + case 43: +#line 2759 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_array(parser)); } + break; + case 44: +#line 2763 "upb/json/parser.rl" + { end_array(parser); } + break; + case 45: +#line 2768 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_number(parser, p)); } + break; + case 46: +#line 2769 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_number(parser, p)); } + break; + case 47: +#line 2771 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_stringval(parser)); } + break; + case 48: +#line 2772 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_stringval(parser)); } + break; + case 49: +#line 2774 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, true)); } + break; + case 50: +#line 2776 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, false)); } + break; + case 51: +#line 2778 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_null(parser)); } + break; + case 52: +#line 2780 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_subobject_full(parser)); } + break; + case 53: +#line 2781 "upb/json/parser.rl" + { end_subobject_full(parser); } + break; + case 54: +#line 2786 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } + break; +#line 3194 "upb/json/parser.c" + } + } + +_again: + if ( cs == 0 ) + goto _out; + if ( ++p != pe ) + goto _resume; + _test_eof: {} + if ( p == eof ) + { + const char *__acts = _json_actions + _json_eof_actions[cs]; + unsigned int __nacts = (unsigned int) *__acts++; + while ( __nacts-- > 0 ) { + switch ( *__acts++ ) { + case 0: +#line 2595 "upb/json/parser.rl" + { p--; {cs = stack[--top]; if ( p == pe ) + goto _test_eof; +goto _again;} } + break; + case 46: +#line 2769 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_number(parser, p)); } + break; + case 49: +#line 2774 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, true)); } + break; + case 50: +#line 2776 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, false)); } + break; + case 51: +#line 2778 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_null(parser)); } + break; + case 53: +#line 2781 "upb/json/parser.rl" + { end_subobject_full(parser); } + break; +#line 3236 "upb/json/parser.c" + } + } + } + + _out: {} + } + +#line 2814 "upb/json/parser.rl" + + if (p != pe) { + upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); + upb_env_reporterror(parser->env, &parser->status); + } else { + capture_suspend(parser, &p); + } + +error: + /* Save parsing state back to parser. */ + parser->current_state = cs; + parser->parser_top = top; + + return p - buf; +} + +static bool end(void *closure, const void *hd) { + upb_json_parser *parser = closure; + + /* Prevent compile warning on unused static constants. */ + UPB_UNUSED(json_start); + UPB_UNUSED(json_en_duration_machine); + UPB_UNUSED(json_en_fieldmask_machine); + UPB_UNUSED(json_en_number_machine); + UPB_UNUSED(json_en_string_machine); + UPB_UNUSED(json_en_timestamp_machine); + UPB_UNUSED(json_en_value_machine); + UPB_UNUSED(json_en_main); + + parse(parser, hd, &eof_ch, 0, NULL); + + return parser->current_state >= 106; } static void json_parser_reset(upb_json_parser *p) { @@ -13932,19 +15921,17 @@ static void json_parser_reset(upb_json_parser *p) { int top; p->top = p->stack; - p->top->f = NULL; - p->top->is_map = false; - p->top->is_mapentry = false; + init_frame(p->top); /* Emit Ragel initialization of the parser. */ -#line 1633 "upb/json/parser.c" +#line 3288 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 1374 "upb/json/parser.rl" +#line 2857 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -14031,7 +16018,9 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { upb_json_parser *upb_json_parser_create(upb_env *env, const upb_json_parsermethod *method, - upb_sink *output) { + const upb_symtab* symtab, + upb_sink *output, + bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_env_bytesallocated(env); #endif @@ -14048,7 +16037,19 @@ upb_json_parser *upb_json_parser_create(upb_env *env, json_parser_reset(p); upb_sink_reset(&p->top->sink, output->handlers, output->closure); p->top->m = upb_handlers_msgdef(output->handlers); + if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { + p->top->is_any = true; + p->top->any_frame = + upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + json_parser_any_frame_reset(p->top->any_frame); + } else { + p->top->is_any = false; + p->top->any_frame = NULL; + } set_name_table(p, p->top); + p->symtab = symtab; + + p->ignore_json_unknown = ignore_json_unknown; /* If this fails, uncomment and increase the value in parser.h. */ /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ @@ -14094,6 +16095,7 @@ const upb_byteshandler *upb_json_parsermethod_inputhandler( #include #include +#include struct upb_json_printer { upb_sink input_; @@ -14114,6 +16116,12 @@ struct upb_json_printer { * repeated fields and messages (maps), and the worst case is a * message->repeated field->submessage->repeated field->... nesting. */ bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2]; + + /* To print timestamp, printer needs to cache its seconds and nanos values + * and convert them when ending timestamp message. See comments of + * printer_sethandlers_timestamp for more detail. */ + int64_t seconds; + int32_t nanos; }; /* StringPiece; a pointer plus a length. */ @@ -14149,6 +16157,15 @@ strpc *newstrpc(upb_handlers *h, const upb_fielddef *f, return ret; } +/* Convert a null-terminated const char* to a string piece. */ +strpc *newstrpc_str(upb_handlers *h, const char * str) { + strpc * ret = upb_gmalloc(sizeof(*ret)); + ret->ptr = upb_gstrdup(str); + ret->len = strlen(str); + upb_handlers_addcleanup(h, ret, freestrpc); + return ret; +} + /* ------------ JSON string printing: values, maps, arrays ------------------ */ static void print_data( @@ -14272,18 +16289,32 @@ static size_t fmt_bool(bool val, char* buf, size_t length) { return n; } -static size_t fmt_int64(long val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%ld", val); +static size_t fmt_int64_as_number(long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "%lld", val); CHKLENGTH(n > 0 && n < length); return n; } -static size_t fmt_uint64(unsigned long long val, char* buf, size_t length) { +static size_t fmt_uint64_as_number( + unsigned long long val, char* buf, size_t length) { size_t n = _upb_snprintf(buf, length, "%llu", val); CHKLENGTH(n > 0 && n < length); return n; } +static size_t fmt_int64_as_string(long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%lld\"", val); + CHKLENGTH(n > 0 && n < length); + return n; +} + +static size_t fmt_uint64_as_string( + unsigned long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%llu\"", val); + CHKLENGTH(n > 0 && n < length); + return n; +} + /* Print a map key given a field name. Called by scalar field handlers and by * startseq for repeated fields. */ static bool putkey(void *closure, const void *handler_data) { @@ -14327,8 +16358,11 @@ static bool putkey(void *closure, const void *handler_data) { static bool putmapkey_##type(void *closure, const void *handler_data, \ type val) { \ upb_json_printer *p = closure; \ + char data[64]; \ + size_t length = fmt_func(val, data, sizeof(data)); \ + UPB_UNUSED(handler_data); \ print_data(p, "\"", 1); \ - CHK(put##type(closure, handler_data, val)); \ + print_data(p, data, length); \ print_data(p, "\":", 2); \ return true; \ } @@ -14336,17 +16370,17 @@ static bool putkey(void *closure, const void *handler_data) { TYPE_HANDLERS(double, fmt_double) TYPE_HANDLERS(float, fmt_float) TYPE_HANDLERS(bool, fmt_bool) -TYPE_HANDLERS(int32_t, fmt_int64) -TYPE_HANDLERS(uint32_t, fmt_int64) -TYPE_HANDLERS(int64_t, fmt_int64) -TYPE_HANDLERS(uint64_t, fmt_uint64) +TYPE_HANDLERS(int32_t, fmt_int64_as_number) +TYPE_HANDLERS(uint32_t, fmt_int64_as_number) +TYPE_HANDLERS(int64_t, fmt_int64_as_string) +TYPE_HANDLERS(uint64_t, fmt_uint64_as_string) /* double and float are not allowed to be map keys. */ TYPE_HANDLERS_MAPKEY(bool, fmt_bool) -TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64) +TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64_as_number) #undef TYPE_HANDLERS #undef TYPE_HANDLERS_MAPKEY @@ -14373,403 +16407,904 @@ static bool scalar_enum(void *closure, const void *handler_data, putint32_t(closure, NULL, val); } - return true; -} - -static void print_enum_symbolic_name(upb_json_printer *p, - const upb_enumdef *def, - int32_t val) { - const char *symbolic_name = upb_enumdef_iton(def, val); - if (symbolic_name) { - print_data(p, "\"", 1); - putstring(p, symbolic_name, strlen(symbolic_name)); - print_data(p, "\"", 1); - } else { - putint32_t(p, NULL, val); + return true; +} + +static void print_enum_symbolic_name(upb_json_printer *p, + const upb_enumdef *def, + int32_t val) { + const char *symbolic_name = upb_enumdef_iton(def, val); + if (symbolic_name) { + print_data(p, "\"", 1); + putstring(p, symbolic_name, strlen(symbolic_name)); + print_data(p, "\"", 1); + } else { + putint32_t(p, NULL, val); + } +} + +static bool repeated_enum(void *closure, const void *handler_data, + int32_t val) { + const EnumHandlerData *hd = handler_data; + upb_json_printer *p = closure; + print_comma(p); + + print_enum_symbolic_name(p, hd->enumdef, val); + + return true; +} + +static bool mapvalue_enum(void *closure, const void *handler_data, + int32_t val) { + const EnumHandlerData *hd = handler_data; + upb_json_printer *p = closure; + + print_enum_symbolic_name(p, hd->enumdef, val); + + return true; +} + +static void *scalar_startsubmsg(void *closure, const void *handler_data) { + return putkey(closure, handler_data) ? closure : UPB_BREAK; +} + +static void *repeated_startsubmsg(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_comma(p); + return closure; +} + +static void start_frame(upb_json_printer *p) { + p->depth_++; + p->first_elem_[p->depth_] = true; + print_data(p, "{", 1); +} + +static void end_frame(upb_json_printer *p) { + print_data(p, "}", 1); + p->depth_--; +} + +static bool printer_startmsg(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + start_frame(p); + return true; +} + +static bool printer_endmsg(void *closure, const void *handler_data, upb_status *s) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(s); + end_frame(p); + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } + return true; +} + +static void *startseq(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + CHK(putkey(closure, handler_data)); + p->depth_++; + p->first_elem_[p->depth_] = true; + print_data(p, "[", 1); + return closure; +} + +static bool endseq(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "]", 1); + p->depth_--; + return true; +} + +static void *startmap(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + CHK(putkey(closure, handler_data)); + p->depth_++; + p->first_elem_[p->depth_] = true; + print_data(p, "{", 1); + return closure; +} + +static bool endmap(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "}", 1); + p->depth_--; + return true; +} + +static size_t putstr(void *closure, const void *handler_data, const char *str, + size_t len, const upb_bufhandle *handle) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(handle); + putstring(p, str, len); + return len; +} + +/* This has to Base64 encode the bytes, because JSON has no "bytes" type. */ +static size_t putbytes(void *closure, const void *handler_data, const char *str, + size_t len, const upb_bufhandle *handle) { + upb_json_printer *p = closure; + + /* This is the regular base64, not the "web-safe" version. */ + static const char base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + /* Base64-encode. */ + char data[16000]; + const char *limit = data + sizeof(data); + const unsigned char *from = (const unsigned char*)str; + char *to = data; + size_t remaining = len; + size_t bytes; + + UPB_UNUSED(handler_data); + UPB_UNUSED(handle); + + print_data(p, "\"", 1); + + while (remaining > 2) { + if (limit - to < 4) { + bytes = to - data; + putstring(p, data, bytes); + to = data; + } + + to[0] = base64[from[0] >> 2]; + to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; + to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)]; + to[3] = base64[from[2] & 0x3f]; + + remaining -= 3; + to += 4; + from += 3; + } + + switch (remaining) { + case 2: + to[0] = base64[from[0] >> 2]; + to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; + to[2] = base64[(from[1] & 0xf) << 2]; + to[3] = '='; + to += 4; + from += 2; + break; + case 1: + to[0] = base64[from[0] >> 2]; + to[1] = base64[((from[0] & 0x3) << 4)]; + to[2] = '='; + to[3] = '='; + to += 4; + from += 1; + break; + } + + bytes = to - data; + putstring(p, data, bytes); + print_data(p, "\"", 1); + return len; +} + +static void *scalar_startstr(void *closure, const void *handler_data, + size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(size_hint); + CHK(putkey(closure, handler_data)); + print_data(p, "\"", 1); + return p; +} + +static size_t scalar_str(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + CHK(putstr(closure, handler_data, str, len, handle)); + return len; +} + +static bool scalar_endstr(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "\"", 1); + return true; +} + +static void *repeated_startstr(void *closure, const void *handler_data, + size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(size_hint); + print_comma(p); + print_data(p, "\"", 1); + return p; +} + +static size_t repeated_str(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + CHK(putstr(closure, handler_data, str, len, handle)); + return len; +} + +static bool repeated_endstr(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "\"", 1); + return true; +} + +static void *mapkeyval_startstr(void *closure, const void *handler_data, + size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(size_hint); + print_data(p, "\"", 1); + return p; +} + +static size_t mapkey_str(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + CHK(putstr(closure, handler_data, str, len, handle)); + return len; +} + +static bool mapkey_endstr(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "\":", 2); + return true; +} + +static bool mapvalue_endstr(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + print_data(p, "\"", 1); + return true; +} + +static size_t scalar_bytes(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + CHK(putkey(closure, handler_data)); + CHK(putbytes(closure, handler_data, str, len, handle)); + return len; +} + +static size_t repeated_bytes(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + upb_json_printer *p = closure; + print_comma(p); + CHK(putbytes(closure, handler_data, str, len, handle)); + return len; +} + +static size_t mapkey_bytes(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + upb_json_printer *p = closure; + CHK(putbytes(closure, handler_data, str, len, handle)); + print_data(p, ":", 1); + return len; +} + +static void set_enum_hd(upb_handlers *h, + const upb_fielddef *f, + bool preserve_fieldnames, + upb_handlerattr *attr) { + EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData)); + hd->enumdef = (const upb_enumdef *)upb_fielddef_subdef(f); + hd->keyname = newstrpc(h, f, preserve_fieldnames); + upb_handlers_addcleanup(h, hd, upb_gfree); + upb_handlerattr_sethandlerdata(attr, hd); +} + +/* Set up handlers for a mapentry submessage (i.e., an individual key/value pair + * in a map). + * + * TODO: Handle missing key, missing value, out-of-order key/value, or repeated + * key or value cases properly. The right way to do this is to allocate a + * temporary structure at the start of a mapentry submessage, store key and + * value data in it as key and value handlers are called, and then print the + * key/value pair once at the end of the submessage. If we don't do this, we + * should at least detect the case and throw an error. However, so far all of + * our sources that emit mapentry messages do so canonically (with one key + * field, and then one value field), so this is not a pressing concern at the + * moment. */ +void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames, + upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + + /* A mapentry message is printed simply as '"key": value'. Rather than + * special-case key and value for every type below, we just handle both + * fields explicitly here. */ + const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY); + const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + + UPB_UNUSED(closure); + + switch (upb_fielddef_type(key_field)) { + case UPB_TYPE_INT32: + upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr); + break; + case UPB_TYPE_INT64: + upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr); + break; + case UPB_TYPE_UINT32: + upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr); + break; + case UPB_TYPE_UINT64: + upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr); + break; + case UPB_TYPE_BOOL: + upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr); + break; + case UPB_TYPE_STRING: + upb_handlers_setstartstr(h, key_field, mapkeyval_startstr, &empty_attr); + upb_handlers_setstring(h, key_field, mapkey_str, &empty_attr); + upb_handlers_setendstr(h, key_field, mapkey_endstr, &empty_attr); + break; + case UPB_TYPE_BYTES: + upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr); + break; + default: + UPB_ASSERT(false); + break; + } + + switch (upb_fielddef_type(value_field)) { + case UPB_TYPE_INT32: + upb_handlers_setint32(h, value_field, putint32_t, &empty_attr); + break; + case UPB_TYPE_INT64: + upb_handlers_setint64(h, value_field, putint64_t, &empty_attr); + break; + case UPB_TYPE_UINT32: + upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr); + break; + case UPB_TYPE_UINT64: + upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr); + break; + case UPB_TYPE_BOOL: + upb_handlers_setbool(h, value_field, putbool, &empty_attr); + break; + case UPB_TYPE_FLOAT: + upb_handlers_setfloat(h, value_field, putfloat, &empty_attr); + break; + case UPB_TYPE_DOUBLE: + upb_handlers_setdouble(h, value_field, putdouble, &empty_attr); + break; + case UPB_TYPE_STRING: + upb_handlers_setstartstr(h, value_field, mapkeyval_startstr, &empty_attr); + upb_handlers_setstring(h, value_field, putstr, &empty_attr); + upb_handlers_setendstr(h, value_field, mapvalue_endstr, &empty_attr); + break; + case UPB_TYPE_BYTES: + upb_handlers_setstring(h, value_field, putbytes, &empty_attr); + break; + case UPB_TYPE_ENUM: { + upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER; + set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr); + upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr); + upb_handlerattr_uninit(&enum_attr); + break; + } + case UPB_TYPE_MESSAGE: + /* No handler necessary -- the submsg handlers will print the message + * as appropriate. */ + break; } + + upb_handlerattr_uninit(&empty_attr); } -static bool repeated_enum(void *closure, const void *handler_data, - int32_t val) { - const EnumHandlerData *hd = handler_data; +static bool putseconds(void *closure, const void *handler_data, + int64_t seconds) { upb_json_printer *p = closure; - print_comma(p); - - print_enum_symbolic_name(p, hd->enumdef, val); - + p->seconds = seconds; + UPB_UNUSED(handler_data); return true; } -static bool mapvalue_enum(void *closure, const void *handler_data, - int32_t val) { - const EnumHandlerData *hd = handler_data; +static bool putnanos(void *closure, const void *handler_data, + int32_t nanos) { upb_json_printer *p = closure; - - print_enum_symbolic_name(p, hd->enumdef, val); - + p->nanos = nanos; + UPB_UNUSED(handler_data); return true; } -static void *scalar_startsubmsg(void *closure, const void *handler_data) { - return putkey(closure, handler_data) ? closure : UPB_BREAK; +static void *scalar_startstr_nokey(void *closure, const void *handler_data, + size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(size_hint); + print_data(p, "\"", 1); + return p; } -static void *repeated_startsubmsg(void *closure, const void *handler_data) { +static size_t putstr_nokey(void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - print_comma(p); - return closure; + UPB_UNUSED(handle); + print_data(p, "\"", 1); + putstring(p, str, len); + print_data(p, "\"", 1); + return len + 2; } -static void start_frame(upb_json_printer *p) { +static void *startseq_nokey(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); p->depth_++; p->first_elem_[p->depth_] = true; - print_data(p, "{", 1); + print_data(p, "[", 1); + return closure; } -static void end_frame(upb_json_printer *p) { - print_data(p, "}", 1); - p->depth_--; +static void *startseq_fieldmask(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + p->depth_++; + p->first_elem_[p->depth_] = true; + return closure; } -static bool printer_startmsg(void *closure, const void *handler_data) { +static bool endseq_fieldmask(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); - } - start_frame(p); + p->depth_--; return true; } -static bool printer_endmsg(void *closure, const void *handler_data, upb_status *s) { +static void *repeated_startstr_fieldmask( + void *closure, const void *handler_data, + size_t size_hint) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - UPB_UNUSED(s); - end_frame(p); - if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + UPB_UNUSED(size_hint); + print_comma(p); + return p; +} + +static size_t repeated_str_fieldmask( + void *closure, const void *handler_data, + const char *str, size_t len, + const upb_bufhandle *handle) { + const char* limit = str + len; + bool upper = false; + size_t result_len = 0; + for (; str < limit; str++) { + if (*str == '_') { + upper = true; + continue; + } + if (upper && *str >= 'a' && *str <= 'z') { + char upper_char = toupper(*str); + CHK(putstr(closure, handler_data, &upper_char, 1, handle)); + } else { + CHK(putstr(closure, handler_data, str, 1, handle)); + } + upper = false; + result_len++; } - return true; + return result_len; } -static void *startseq(void *closure, const void *handler_data) { +static void *startmap_nokey(void *closure, const void *handler_data) { upb_json_printer *p = closure; - CHK(putkey(closure, handler_data)); + UPB_UNUSED(handler_data); p->depth_++; p->first_elem_[p->depth_] = true; - print_data(p, "[", 1); + print_data(p, "{", 1); return closure; } -static bool endseq(void *closure, const void *handler_data) { +static bool putnull(void *closure, const void *handler_data, + int32_t null) { upb_json_printer *p = closure; + print_data(p, "null", 4); UPB_UNUSED(handler_data); - print_data(p, "]", 1); - p->depth_--; + UPB_UNUSED(null); return true; } -static void *startmap(void *closure, const void *handler_data) { +static bool printer_startdurationmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; - CHK(putkey(closure, handler_data)); - p->depth_++; - p->first_elem_[p->depth_] = true; - print_data(p, "{", 1); - return closure; + UPB_UNUSED(handler_data); + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + return true; } -static bool endmap(void *closure, const void *handler_data) { +#define UPB_DURATION_MAX_JSON_LEN 23 +#define UPB_DURATION_MAX_NANO_LEN 9 + +static bool printer_enddurationmsg(void *closure, const void *handler_data, + upb_status *s) { upb_json_printer *p = closure; + char buffer[UPB_DURATION_MAX_JSON_LEN]; + size_t base_len; + size_t curr; + size_t i; + + memset(buffer, 0, UPB_DURATION_MAX_JSON_LEN); + + if (p->seconds < -315576000000) { + upb_status_seterrf(s, "error parsing duration: " + "minimum acceptable value is " + "-315576000000"); + return false; + } + + if (p->seconds > 315576000000) { + upb_status_seterrf(s, "error serializing duration: " + "maximum acceptable value is " + "315576000000"); + return false; + } + + _upb_snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds); + base_len = strlen(buffer); + + if (p->nanos != 0) { + char nanos_buffer[UPB_DURATION_MAX_NANO_LEN + 3]; + _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f", + p->nanos / 1000000000.0); + /* Remove trailing 0. */ + for (i = UPB_DURATION_MAX_NANO_LEN + 2; + nanos_buffer[i] == '0'; i--) { + nanos_buffer[i] = 0; + } + strcpy(buffer + base_len, nanos_buffer + 1); + } + + curr = strlen(buffer); + strcpy(buffer + curr, "s"); + + p->seconds = 0; + p->nanos = 0; + + print_data(p, "\"", 1); + print_data(p, buffer, strlen(buffer)); + print_data(p, "\"", 1); + + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } + UPB_UNUSED(handler_data); - print_data(p, "}", 1); - p->depth_--; return true; } -static size_t putstr(void *closure, const void *handler_data, const char *str, - size_t len, const upb_bufhandle *handle) { +static bool printer_starttimestampmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - UPB_UNUSED(handle); - putstring(p, str, len); - return len; + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + return true; } -/* This has to Base64 encode the bytes, because JSON has no "bytes" type. */ -static size_t putbytes(void *closure, const void *handler_data, const char *str, - size_t len, const upb_bufhandle *handle) { - upb_json_printer *p = closure; - - /* This is the regular base64, not the "web-safe" version. */ - static const char base64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - /* Base64-encode. */ - char data[16000]; - const char *limit = data + sizeof(data); - const unsigned char *from = (const unsigned char*)str; - char *to = data; - size_t remaining = len; - size_t bytes; +#define UPB_TIMESTAMP_MAX_JSON_LEN 31 +#define UPB_TIMESTAMP_BEFORE_NANO_LEN 19 +#define UPB_TIMESTAMP_MAX_NANO_LEN 9 - UPB_UNUSED(handler_data); - UPB_UNUSED(handle); +static bool printer_endtimestampmsg(void *closure, const void *handler_data, + upb_status *s) { + upb_json_printer *p = closure; + char buffer[UPB_TIMESTAMP_MAX_JSON_LEN]; + time_t time = p->seconds; + size_t curr; + size_t i; + size_t year_length = + strftime(buffer, UPB_TIMESTAMP_MAX_JSON_LEN, "%Y", gmtime(&time)); - while (remaining > 2) { - /* TODO(haberman): handle encoded lengths > sizeof(data) */ - UPB_ASSERT((limit - to) >= 4); + if (p->seconds < -62135596800) { + upb_status_seterrf(s, "error parsing timestamp: " + "minimum acceptable value is " + "0001-01-01T00:00:00Z"); + return false; + } - to[0] = base64[from[0] >> 2]; - to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; - to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)]; - to[3] = base64[from[2] & 0x3f]; + if (p->seconds > 253402300799) { + upb_status_seterrf(s, "error parsing timestamp: " + "maximum acceptable value is " + "9999-12-31T23:59:59Z"); + return false; + } - remaining -= 3; - to += 4; - from += 3; + /* strftime doesn't guarantee 4 digits for year. Prepend 0 by ourselves. */ + for (i = 0; i < 4 - year_length; i++) { + buffer[i] = '0'; } - switch (remaining) { - case 2: - to[0] = base64[from[0] >> 2]; - to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; - to[2] = base64[(from[1] & 0xf) << 2]; - to[3] = '='; - to += 4; - from += 2; - break; - case 1: - to[0] = base64[from[0] >> 2]; - to[1] = base64[((from[0] & 0x3) << 4)]; - to[2] = '='; - to[3] = '='; - to += 4; - from += 1; - break; + strftime(buffer + (4 - year_length), UPB_TIMESTAMP_MAX_JSON_LEN, + "%Y-%m-%dT%H:%M:%S", gmtime(&time)); + if (p->nanos != 0) { + char nanos_buffer[UPB_TIMESTAMP_MAX_NANO_LEN + 3]; + _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f", + p->nanos / 1000000000.0); + /* Remove trailing 0. */ + for (i = UPB_TIMESTAMP_MAX_NANO_LEN + 2; + nanos_buffer[i] == '0'; i--) { + nanos_buffer[i] = 0; + } + strcpy(buffer + UPB_TIMESTAMP_BEFORE_NANO_LEN, nanos_buffer + 1); } - bytes = to - data; + curr = strlen(buffer); + strcpy(buffer + curr, "Z"); + + p->seconds = 0; + p->nanos = 0; + print_data(p, "\"", 1); - putstring(p, data, bytes); + print_data(p, buffer, strlen(buffer)); print_data(p, "\"", 1); - return len; -} -static void *scalar_startstr(void *closure, const void *handler_data, - size_t size_hint) { - upb_json_printer *p = closure; + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } + UPB_UNUSED(handler_data); - UPB_UNUSED(size_hint); - CHK(putkey(closure, handler_data)); - print_data(p, "\"", 1); - return p; + UPB_UNUSED(s); + return true; } -static size_t scalar_str(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - CHK(putstr(closure, handler_data, str, len, handle)); - return len; +static bool printer_startmsg_noframe(void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + return true; } -static bool scalar_endstr(void *closure, const void *handler_data) { +static bool printer_endmsg_noframe( + void *closure, const void *handler_data, upb_status *s) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - print_data(p, "\"", 1); + UPB_UNUSED(s); + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } return true; } -static void *repeated_startstr(void *closure, const void *handler_data, - size_t size_hint) { +static bool printer_startmsg_fieldmask( + void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); - UPB_UNUSED(size_hint); - print_comma(p); + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } print_data(p, "\"", 1); - return p; -} - -static size_t repeated_str(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - CHK(putstr(closure, handler_data, str, len, handle)); - return len; + return true; } -static bool repeated_endstr(void *closure, const void *handler_data) { +static bool printer_endmsg_fieldmask( + void *closure, const void *handler_data, upb_status *s) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); + UPB_UNUSED(s); print_data(p, "\"", 1); + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } return true; } -static void *mapkeyval_startstr(void *closure, const void *handler_data, - size_t size_hint) { +static void *scalar_startstr_onlykey( + void *closure, const void *handler_data, size_t size_hint) { upb_json_printer *p = closure; - UPB_UNUSED(handler_data); UPB_UNUSED(size_hint); - print_data(p, "\"", 1); + CHK(putkey(closure, handler_data)); return p; } -static size_t mapkey_str(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - CHK(putstr(closure, handler_data, str, len, handle)); - return len; -} +/* Set up handlers for an Any submessage. */ +void printer_sethandlers_any(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); -static bool mapkey_endstr(void *closure, const void *handler_data) { - upb_json_printer *p = closure; - UPB_UNUSED(handler_data); - print_data(p, "\":", 2); - return true; -} + const upb_fielddef* type_field = upb_msgdef_itof(md, UPB_ANY_TYPE); + const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_ANY_VALUE); -static bool mapvalue_endstr(void *closure, const void *handler_data) { - upb_json_printer *p = closure; - UPB_UNUSED(handler_data); - print_data(p, "\"", 1); - return true; -} + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; -static size_t scalar_bytes(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - CHK(putkey(closure, handler_data)); - CHK(putbytes(closure, handler_data, str, len, handle)); - return len; -} + /* type_url's json name is "@type" */ + upb_handlerattr type_name_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr value_name_attr = UPB_HANDLERATTR_INITIALIZER; + strpc *type_url_json_name = newstrpc_str(h, "@type"); + strpc *value_json_name = newstrpc_str(h, "value"); -static size_t repeated_bytes(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - upb_json_printer *p = closure; - print_comma(p); - CHK(putbytes(closure, handler_data, str, len, handle)); - return len; + upb_handlerattr_sethandlerdata(&type_name_attr, type_url_json_name); + upb_handlerattr_sethandlerdata(&value_name_attr, value_json_name); + + /* Set up handlers. */ + upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg, &empty_attr); + + upb_handlers_setstartstr(h, type_field, scalar_startstr, &type_name_attr); + upb_handlers_setstring(h, type_field, scalar_str, &empty_attr); + upb_handlers_setendstr(h, type_field, scalar_endstr, &empty_attr); + + /* This is not the full and correct JSON encoding for the Any value field. It + * requires further processing by the wrapper code based on the type URL. + */ + upb_handlers_setstartstr(h, value_field, scalar_startstr_onlykey, + &value_name_attr); + + UPB_UNUSED(closure); } -static size_t mapkey_bytes(void *closure, const void *handler_data, - const char *str, size_t len, - const upb_bufhandle *handle) { - upb_json_printer *p = closure; - CHK(putbytes(closure, handler_data, str, len, handle)); - print_data(p, ":", 1); - return len; +/* Set up handlers for a fieldmask submessage. */ +void printer_sethandlers_fieldmask(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + const upb_fielddef* f = upb_msgdef_itof(md, 1); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + + upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr); + upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr); + + upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr); + + upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr); + upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr); + + UPB_UNUSED(closure); } -static void set_enum_hd(upb_handlers *h, - const upb_fielddef *f, - bool preserve_fieldnames, - upb_handlerattr *attr) { - EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData)); - hd->enumdef = (const upb_enumdef *)upb_fielddef_subdef(f); - hd->keyname = newstrpc(h, f, preserve_fieldnames); - upb_handlers_addcleanup(h, hd, upb_gfree); - upb_handlerattr_sethandlerdata(attr, hd); +/* Set up handlers for a duration submessage. */ +void printer_sethandlers_duration(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + + const upb_fielddef* seconds_field = + upb_msgdef_itof(md, UPB_DURATION_SECONDS); + const upb_fielddef* nanos_field = + upb_msgdef_itof(md, UPB_DURATION_NANOS); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + + upb_handlers_setstartmsg(h, printer_startdurationmsg, &empty_attr); + upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr); + upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr); + upb_handlers_setendmsg(h, printer_enddurationmsg, &empty_attr); + + UPB_UNUSED(closure); } -/* Set up handlers for a mapentry submessage (i.e., an individual key/value pair - * in a map). - * - * TODO: Handle missing key, missing value, out-of-order key/value, or repeated - * key or value cases properly. The right way to do this is to allocate a - * temporary structure at the start of a mapentry submessage, store key and - * value data in it as key and value handlers are called, and then print the - * key/value pair once at the end of the submessage. If we don't do this, we - * should at least detect the case and throw an error. However, so far all of - * our sources that emit mapentry messages do so canonically (with one key - * field, and then one value field), so this is not a pressing concern at the - * moment. */ -void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames, - upb_handlers *h) { +/* Set up handlers for a timestamp submessage. Instead of printing fields + * separately, the json representation of timestamp follows RFC 3339 */ +void printer_sethandlers_timestamp(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); - /* A mapentry message is printed simply as '"key": value'. Rather than - * special-case key and value for every type below, we just handle both - * fields explicitly here. */ - const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY); - const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE); + const upb_fielddef* seconds_field = + upb_msgdef_itof(md, UPB_TIMESTAMP_SECONDS); + const upb_fielddef* nanos_field = + upb_msgdef_itof(md, UPB_TIMESTAMP_NANOS); upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlers_setstartmsg(h, printer_starttimestampmsg, &empty_attr); + upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr); + upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr); + upb_handlers_setendmsg(h, printer_endtimestampmsg, &empty_attr); + UPB_UNUSED(closure); +} - switch (upb_fielddef_type(key_field)) { - case UPB_TYPE_INT32: - upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr); - break; - case UPB_TYPE_INT64: - upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr); - break; - case UPB_TYPE_UINT32: - upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr); - break; - case UPB_TYPE_UINT64: - upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr); - break; - case UPB_TYPE_BOOL: - upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr); - break; - case UPB_TYPE_STRING: - upb_handlers_setstartstr(h, key_field, mapkeyval_startstr, &empty_attr); - upb_handlers_setstring(h, key_field, mapkey_str, &empty_attr); - upb_handlers_setendstr(h, key_field, mapkey_endstr, &empty_attr); - break; - case UPB_TYPE_BYTES: - upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr); - break; - default: - UPB_ASSERT(false); - break; - } +void printer_sethandlers_value(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + upb_msg_field_iter i; - switch (upb_fielddef_type(value_field)) { - case UPB_TYPE_INT32: - upb_handlers_setint32(h, value_field, putint32_t, &empty_attr); - break; - case UPB_TYPE_INT64: - upb_handlers_setint64(h, value_field, putint64_t, &empty_attr); - break; - case UPB_TYPE_UINT32: - upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr); - break; - case UPB_TYPE_UINT64: - upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr); - break; - case UPB_TYPE_BOOL: - upb_handlers_setbool(h, value_field, putbool, &empty_attr); - break; - case UPB_TYPE_FLOAT: - upb_handlers_setfloat(h, value_field, putfloat, &empty_attr); - break; - case UPB_TYPE_DOUBLE: - upb_handlers_setdouble(h, value_field, putdouble, &empty_attr); - break; - case UPB_TYPE_STRING: - upb_handlers_setstartstr(h, value_field, mapkeyval_startstr, &empty_attr); - upb_handlers_setstring(h, value_field, putstr, &empty_attr); - upb_handlers_setendstr(h, value_field, mapvalue_endstr, &empty_attr); - break; - case UPB_TYPE_BYTES: - upb_handlers_setstring(h, value_field, putbytes, &empty_attr); - break; - case UPB_TYPE_ENUM: { - upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER; - set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr); - upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr); - upb_handlerattr_uninit(&enum_attr); - break; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + + upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); + + upb_msg_field_begin(&i, md); + for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) { + const upb_fielddef *f = upb_msg_iter_field(&i); + + switch (upb_fielddef_type(f)) { + case UPB_TYPE_ENUM: + upb_handlers_setint32(h, f, putnull, &empty_attr); + break; + case UPB_TYPE_DOUBLE: + upb_handlers_setdouble(h, f, putdouble, &empty_attr); + break; + case UPB_TYPE_STRING: + upb_handlers_setstartstr(h, f, scalar_startstr_nokey, &empty_attr); + upb_handlers_setstring(h, f, scalar_str, &empty_attr); + upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr); + break; + case UPB_TYPE_BOOL: + upb_handlers_setbool(h, f, putbool, &empty_attr); + break; + case UPB_TYPE_MESSAGE: + break; + default: + UPB_ASSERT(false); + break; } - case UPB_TYPE_MESSAGE: - /* No handler necessary -- the submsg handlers will print the message - * as appropriate. */ - break; } - upb_handlerattr_uninit(&empty_attr); + UPB_UNUSED(closure); +} + +#define WRAPPER_SETHANDLERS(wrapper, type, putmethod) \ +void printer_sethandlers_##wrapper(const void *closure, upb_handlers *h) { \ + const upb_msgdef *md = upb_handlers_msgdef(h); \ + const upb_fielddef* f = upb_msgdef_itof(md, 1); \ + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; \ + upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); \ + upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); \ + upb_handlers_set##type(h, f, putmethod, &empty_attr); \ + UPB_UNUSED(closure); \ +} + +WRAPPER_SETHANDLERS(doublevalue, double, putdouble) +WRAPPER_SETHANDLERS(floatvalue, float, putfloat) +WRAPPER_SETHANDLERS(int64value, int64, putint64_t) +WRAPPER_SETHANDLERS(uint64value, uint64, putuint64_t) +WRAPPER_SETHANDLERS(int32value, int32, putint32_t) +WRAPPER_SETHANDLERS(uint32value, uint32, putuint32_t) +WRAPPER_SETHANDLERS(boolvalue, bool, putbool) +WRAPPER_SETHANDLERS(stringvalue, string, putstr_nokey) +WRAPPER_SETHANDLERS(bytesvalue, string, putbytes) + +#undef WRAPPER_SETHANDLERS + +void printer_sethandlers_listvalue(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + const upb_fielddef* f = upb_msgdef_itof(md, 1); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + + upb_handlers_setstartseq(h, f, startseq_nokey, &empty_attr); + upb_handlers_setendseq(h, f, endseq, &empty_attr); + + upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); + + upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr); + + UPB_UNUSED(closure); +} + +void printer_sethandlers_structvalue(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + const upb_fielddef* f = upb_msgdef_itof(md, 1); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + + upb_handlers_setstartseq(h, f, startmap_nokey, &empty_attr); + upb_handlers_setendseq(h, f, endmap, &empty_attr); + + upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); + + upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr); + + UPB_UNUSED(closure); } void printer_sethandlers(const void *closure, upb_handlers *h) { @@ -14787,6 +17322,48 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { return; } + switch (upb_msgdef_wellknowntype(md)) { + case UPB_WELLKNOWN_UNSPECIFIED: + break; + case UPB_WELLKNOWN_ANY: + printer_sethandlers_any(closure, h); + return; + case UPB_WELLKNOWN_FIELDMASK: + printer_sethandlers_fieldmask(closure, h); + return; + case UPB_WELLKNOWN_DURATION: + printer_sethandlers_duration(closure, h); + return; + case UPB_WELLKNOWN_TIMESTAMP: + printer_sethandlers_timestamp(closure, h); + return; + case UPB_WELLKNOWN_VALUE: + printer_sethandlers_value(closure, h); + return; + case UPB_WELLKNOWN_LISTVALUE: + printer_sethandlers_listvalue(closure, h); + return; + case UPB_WELLKNOWN_STRUCT: + printer_sethandlers_structvalue(closure, h); + return; +#define WRAPPER(wellknowntype, name) \ + case wellknowntype: \ + printer_sethandlers_##name(closure, h); \ + return; \ + + WRAPPER(UPB_WELLKNOWN_DOUBLEVALUE, doublevalue); + WRAPPER(UPB_WELLKNOWN_FLOATVALUE, floatvalue); + WRAPPER(UPB_WELLKNOWN_INT64VALUE, int64value); + WRAPPER(UPB_WELLKNOWN_UINT64VALUE, uint64value); + WRAPPER(UPB_WELLKNOWN_INT32VALUE, int32value); + WRAPPER(UPB_WELLKNOWN_UINT32VALUE, uint32value); + WRAPPER(UPB_WELLKNOWN_BOOLVALUE, boolvalue); + WRAPPER(UPB_WELLKNOWN_STRINGVALUE, stringvalue); + WRAPPER(UPB_WELLKNOWN_BYTESVALUE, bytesvalue); + +#undef WRAPPER + } + upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr); upb_handlers_setendmsg(h, printer_endmsg, &empty_attr); @@ -14894,6 +17471,8 @@ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, p->output_ = output; json_printer_reset(p); upb_sink_reset(&p->input_, h, p); + p->seconds = 0; + p->nanos = 0; /* If this fails, increase the value in printer.h. */ UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= @@ -14911,3 +17490,8 @@ const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, return upb_handlers_newfrozen( md, owner, printer_sethandlers, &preserve_fieldnames); } + +#undef UPB_SIZE +#undef UPB_FIELD_AT +#undef UPB_READ_ONEOF +#undef UPB_WRITE_ONEOF diff --git a/ruby/ext/google/protobuf_c/upb.h b/ruby/ext/google/protobuf_c/upb.h index f441c89c83..bb6a903c10 100644 --- a/ruby/ext/google/protobuf_c/upb.h +++ b/ruby/ext/google/protobuf_c/upb.h @@ -1,4 +1,110 @@ -// Amalgamated source file +/* Amalgamated source file */ + +#if UINTPTR_MAX == 0xffffffff +#define UPB_SIZE(size32, size64) size32 +#else +#define UPB_SIZE(size32, size64) size64 +#endif + +#define UPB_FIELD_AT(msg, fieldtype, offset) \ + *(fieldtype*)((const char*)(msg) + offset) + +#define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \ + UPB_FIELD_AT(msg, int, case_offset) == case_val \ + ? UPB_FIELD_AT(msg, fieldtype, offset) \ + : default + +#define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \ + UPB_FIELD_AT(msg, int, case_offset) = case_val; \ + UPB_FIELD_AT(msg, fieldtype, offset) = value; +/* +** upb::Message is a representation for protobuf messages. +** +** However it differs from other common representations like +** google::protobuf::Message in one key way: it does not prescribe any +** ownership between messages and submessages, and it relies on the +** client to ensure that each submessage/array/map outlives its parent. +** +** All messages, arrays, and maps live in an Arena. If the entire message +** tree is in the same arena, ensuring proper lifetimes is simple. However +** the client can mix arenas as long as they ensure that there are no +** dangling pointers. +** +** A client can access a upb::Message without knowing anything about +** ownership semantics, but to create or mutate a message a user needs +** to implement the memory management themselves. +** +** TODO: UTF-8 checking? +**/ + +#ifndef UPB_MSG_H_ +#define UPB_MSG_H_ + +/* +** Defs are upb's internal representation of the constructs that can appear +** in a .proto file: +** +** - upb::MessageDef (upb_msgdef): describes a "message" construct. +** - upb::FieldDef (upb_fielddef): describes a message field. +** - upb::FileDef (upb_filedef): describes a .proto file and its defs. +** - upb::EnumDef (upb_enumdef): describes an enum. +** - upb::OneofDef (upb_oneofdef): describes a oneof. +** - upb::Def (upb_def): base class of all the others. +** +** TODO: definitions of services. +** +** Like upb_refcounted objects, defs are mutable only until frozen, and are +** only thread-safe once frozen. +** +** This is a mixed C/C++ interface that offers a full API to both languages. +** See the top-level README for more information. +*/ + +#ifndef UPB_DEF_H_ +#define UPB_DEF_H_ + +/* +** upb::RefCounted (upb_refcounted) +** +** A refcounting scheme that supports circular refs. It accomplishes this by +** partitioning the set of objects into groups such that no cycle spans groups; +** we can then reference-count the group as a whole and ignore refs within the +** group. When objects are mutable, these groups are computed very +** conservatively; we group any objects that have ever had a link between them. +** When objects are frozen, we compute strongly-connected components which +** allows us to be precise and only group objects that are actually cyclic. +** +** This is a mixed C/C++ interface that offers a full API to both languages. +** See the top-level README for more information. +*/ + +#ifndef UPB_REFCOUNTED_H_ +#define UPB_REFCOUNTED_H_ + +/* +** 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 homogenous (all values of the same type). In debug +** mode, we check this on insert and lookup. +*/ + +#ifndef UPB_TABLE_H_ +#define UPB_TABLE_H_ + +#include +#include /* ** This file contains shared definitions that are widely used across upb. ** @@ -737,106 +843,6 @@ template class upb::InlinedEnvironment : public upb::Environment { #endif /* UPB_H_ */ -/* -** upb_decode: parsing into a upb_msg using a upb_msglayout. -*/ - -#ifndef UPB_DECODE_H_ -#define UPB_DECODE_H_ - -/* -** upb::Message is a representation for protobuf messages. -** -** However it differs from other common representations like -** google::protobuf::Message in one key way: it does not prescribe any -** ownership between messages and submessages, and it relies on the -** client to delete each message/submessage/array/map at the appropriate -** time. -** -** A client can access a upb::Message without knowing anything about -** ownership semantics, but to create or mutate a message a user needs -** to implement the memory management themselves. -** -** Currently all messages, arrays, and maps store a upb_alloc* internally. -** Mutating operations use this when they require dynamically-allocated -** memory. We could potentially eliminate this size overhead later by -** letting the user flip a bit on the factory that prevents this from -** being stored. The user would then need to use separate functions where -** the upb_alloc* is passed explicitly. However for handlers to populate -** such structures, they would need a place to store this upb_alloc* during -** parsing; upb_handlers don't currently have a good way to accommodate this. -** -** TODO: UTF-8 checking? -**/ - -#ifndef UPB_MSG_H_ -#define UPB_MSG_H_ - -/* -** Defs are upb's internal representation of the constructs that can appear -** in a .proto file: -** -** - upb::MessageDef (upb_msgdef): describes a "message" construct. -** - upb::FieldDef (upb_fielddef): describes a message field. -** - upb::FileDef (upb_filedef): describes a .proto file and its defs. -** - upb::EnumDef (upb_enumdef): describes an enum. -** - upb::OneofDef (upb_oneofdef): describes a oneof. -** - upb::Def (upb_def): base class of all the others. -** -** TODO: definitions of services. -** -** Like upb_refcounted objects, defs are mutable only until frozen, and are -** only thread-safe once frozen. -** -** This is a mixed C/C++ interface that offers a full API to both languages. -** See the top-level README for more information. -*/ - -#ifndef UPB_DEF_H_ -#define UPB_DEF_H_ - -/* -** upb::RefCounted (upb_refcounted) -** -** A refcounting scheme that supports circular refs. It accomplishes this by -** partitioning the set of objects into groups such that no cycle spans groups; -** we can then reference-count the group as a whole and ignore refs within the -** group. When objects are mutable, these groups are computed very -** conservatively; we group any objects that have ever had a link between them. -** When objects are frozen, we compute strongly-connected components which -** allows us to be precise and only group objects that are actually cyclic. -** -** This is a mixed C/C++ interface that offers a full API to both languages. -** See the top-level README for more information. -*/ - -#ifndef UPB_REFCOUNTED_H_ -#define UPB_REFCOUNTED_H_ - -/* -** 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 homogenous (all values of the same type). In debug -** mode, we check this on insert and lookup. -*/ - -#ifndef UPB_TABLE_H_ -#define UPB_TABLE_H_ - -#include -#include #ifdef __cplusplus extern "C" { @@ -1834,9 +1840,24 @@ class upb::Def { private: UPB_DISALLOW_POD_OPS(Def, upb::Def) +#else +struct upb_def { + upb_refcounted base; + + const char *fullname; + const upb_filedef* file; + char type; /* A upb_deftype_t (char to save space) */ + + /* Used as a flag during the def's mutable stage. Must be false unless + * it is currently being used by a function on the stack. This allows + * us to easily determine which defs were passed into the function's + * current invocation. */ + bool came_from_user; +#endif }; -#endif /* __cplusplus */ +#define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \ + { UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false } UPB_BEGIN_EXTERN_C @@ -1995,6 +2016,36 @@ typedef enum { UPB_SYNTAX_PROTO3 = 3 } upb_syntax_t; +/* 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 merber of these groups are not changed. + */ +typedef enum { + UPB_WELLKNOWN_UNSPECIFIED, + UPB_WELLKNOWN_ANY, + UPB_WELLKNOWN_FIELDMASK, + UPB_WELLKNOWN_DURATION, + UPB_WELLKNOWN_TIMESTAMP, + /* number wrappers */ + UPB_WELLKNOWN_DOUBLEVALUE, + UPB_WELLKNOWN_FLOATVALUE, + UPB_WELLKNOWN_INT64VALUE, + UPB_WELLKNOWN_UINT64VALUE, + UPB_WELLKNOWN_INT32VALUE, + UPB_WELLKNOWN_UINT32VALUE, + /* string wrappers */ + UPB_WELLKNOWN_STRINGVALUE, + UPB_WELLKNOWN_BYTESVALUE, + UPB_WELLKNOWN_BOOLVALUE, + UPB_WELLKNOWN_VALUE, + UPB_WELLKNOWN_LISTVALUE, + UPB_WELLKNOWN_STRUCT +} upb_wellknowntype_t; + + +/* Maps descriptor type -> upb field type. */ +extern const uint8_t upb_desctype_to_fieldtype[]; + /* Maximum field number allowed for FieldDefs. This is an inherent limit of the * protobuf wire format. */ #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) @@ -2269,12 +2320,57 @@ class upb::FieldDef { private: UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef) -}; +#else +struct upb_fielddef { + upb_def base; + union { + int64_t sint; + uint64_t uint; + double dbl; + float flt; + void *bytes; + } defaultval; + union { + const upb_msgdef *def; /* If !msg_is_symbolic. */ + char *name; /* If msg_is_symbolic. */ + } msg; + union { + const upb_def *def; /* If !subdef_is_symbolic. */ + char *name; /* If subdef_is_symbolic. */ + } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */ + bool subdef_is_symbolic; + bool msg_is_symbolic; + const upb_oneofdef *oneof; + bool default_is_string; + bool type_is_set_; /* False until type is explicitly set. */ + bool is_extension_; + bool lazy_; + bool packed_; + upb_intfmt_t intfmt; + bool tagdelim; + upb_fieldtype_t type_; + upb_label_t label_; + uint32_t number_; + uint32_t selector_base; /* Used to index into a upb::Handlers table. */ + uint32_t index_; # endif /* defined(__cplusplus) */ +}; UPB_BEGIN_EXTERN_C +extern const struct upb_refcounted_vtbl upb_fielddef_vtbl; + +#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \ + packed, name, num, msgdef, subdef, selector_base, \ + index, defaultval, refs, ref2s) \ + { \ + UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s), \ + defaultval, {msgdef}, {subdef}, NULL, false, false, \ + type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \ + lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \ + } + /* Native C API. */ upb_fielddef *upb_fielddef_new(const void *owner); @@ -2376,6 +2472,18 @@ typedef upb_strtable_iter upb_msg_oneof_iter; #define UPB_MAPENTRY_KEY 1 #define UPB_MAPENTRY_VALUE 2 +/* Well-known field tag numbers for Any messages. */ +#define UPB_ANY_TYPE 1 +#define UPB_ANY_VALUE 2 + +/* Well-known field tag numbers for timestamp messages. */ +#define UPB_DURATION_SECONDS 1 +#define UPB_DURATION_NANOS 2 + +/* Well-known field tag numbers for duration messages. */ +#define UPB_TIMESTAMP_SECONDS 1 +#define UPB_TIMESTAMP_NANOS 2 + #ifdef __cplusplus /* Structure that describes a single .proto message type. @@ -2490,6 +2598,13 @@ class upb::MessageDef { void setmapentry(bool map_entry); bool mapentry() const; + /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for + * non-well-known message. */ + upb_wellknowntype_t wellknowntype() const; + + /* Whether is a number wrapper. */ + bool isnumberwrapper() const; + /* Iteration over fields. The order is undefined. */ class field_iterator : public std::iterator { @@ -2605,12 +2720,45 @@ class upb::MessageDef { private: UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef) -}; +#else +struct upb_msgdef { + upb_def base; -#endif /* __cplusplus */ + size_t selector_count; + uint32_t submsg_field_count; + + /* Tables for looking up fields by number and name. */ + upb_inttable itof; /* int to field */ + upb_strtable ntof; /* name to field/oneof */ + + /* Is this a map-entry message? */ + bool map_entry; + + /* Whether this message has proto2 or proto3 semantics. */ + upb_syntax_t syntax; + + /* Type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for + * non-well-known message. */ + upb_wellknowntype_t well_known_type; + + /* TODO(haberman): proper extension ranges (there can be multiple). */ +#endif /* __cplusplus */ +}; UPB_BEGIN_EXTERN_C +extern const struct upb_refcounted_vtbl upb_msgdef_vtbl; + +/* TODO: also support static initialization of the oneofs table. This will be + * needed if we compile in descriptors that contain oneofs. */ +#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \ + map_entry, syntax, well_known_type, refs, ref2s) \ + { \ + UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s), \ + selector_count, submsg_field_count, itof, ntof, map_entry, syntax, \ + well_known_type \ + } + /* Returns NULL if memory allocation failed. */ upb_msgdef *upb_msgdef_new(const void *owner); @@ -2631,6 +2779,8 @@ bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor, bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s); void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry); bool upb_msgdef_mapentry(const upb_msgdef *m); +upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m); +bool upb_msgdef_isnumberwrapper(const upb_msgdef *m); bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax); /* Field lookup in a couple of different variations: @@ -2787,12 +2937,24 @@ class upb::EnumDef { private: UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef) -}; +#else +struct upb_enumdef { + upb_def base; + upb_strtable ntoi; + upb_inttable iton; + int32_t defaultval; #endif /* __cplusplus */ +}; UPB_BEGIN_EXTERN_C +extern const struct upb_refcounted_vtbl upb_enumdef_vtbl; + +#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \ + { UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi, \ + iton, defaultval } + /* Native C API. */ upb_enumdef *upb_enumdef_new(const void *owner); @@ -2941,12 +3103,25 @@ class upb::OneofDef { private: UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef) -}; +#else +struct upb_oneofdef { + upb_refcounted base; + uint32_t index; /* Index within oneofs. */ + const char *name; + upb_strtable ntof; + upb_inttable itof; + const upb_msgdef *parent; #endif /* __cplusplus */ +}; UPB_BEGIN_EXTERN_C +extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl; + +#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \ + { UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof } + /* Native C API. */ upb_oneofdef *upb_oneofdef_new(const void *owner); @@ -3065,12 +3240,25 @@ class upb::FileDef { private: UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef) -}; +#else +struct upb_filedef { + upb_refcounted base; + + const char *name; + const char *package; + const char *phpprefix; + const char *phpnamespace; + upb_syntax_t syntax; + upb_inttable defs; + upb_inttable deps; #endif +}; UPB_BEGIN_EXTERN_C +extern const struct upb_refcounted_vtbl upb_filedef_vtbl; + upb_filedef *upb_filedef_new(const void *owner); /* Include upb_refcounted methods like upb_msgdef_ref(). */ @@ -3200,9 +3388,13 @@ class upb::SymbolTable { private: UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable) -}; +#else +struct upb_symtab { + upb_refcounted base; + upb_strtable symtab; #endif /* __cplusplus */ +}; UPB_BEGIN_EXTERN_C @@ -3214,6 +3406,8 @@ const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, const char *sym); const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym); const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); +const upb_msgdef *upb_symtab_lookupmsg2( + const upb_symtab *s, const char *sym, size_t len); const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, void *ref_donor, upb_status *status); @@ -3570,6 +3764,12 @@ inline void MessageDef::setmapentry(bool map_entry) { inline bool MessageDef::mapentry() const { return upb_msgdef_mapentry(this); } +inline upb_wellknowntype_t MessageDef::wellknowntype() const { + return upb_msgdef_wellknowntype(this); +} +inline bool MessageDef::isnumberwrapper() const { + return upb_msgdef_isnumberwrapper(this); +} inline MessageDef::field_iterator MessageDef::field_begin() { return field_iterator(this); } @@ -4697,6 +4897,34 @@ UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) { uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f); uint32_t upb_handlers_selectorcount(const upb_fielddef *f); + +/** Message handlers ******************************************************************/ + +/* These are the handlers used internally by upb_msgfactory_getmergehandlers(). + * They write scalar data to a known offset from the message pointer. + * + * These would be trivial for anyone to implement themselves, but it's better + * to use these because some JITs will recognize and specialize these instead + * of actually calling the function. */ + +/* Sets a handler for the given primitive field that will write the data at the + * given offset. If hasbit > 0, also sets a hasbit at the given bit offset + * (addressing each byte low to high). */ +bool upb_msg_setscalarhandler(upb_handlers *h, + const upb_fielddef *f, + size_t offset, + int32_t hasbit); + +/* If the given handler is a msghandlers_primitive field, returns true and sets + * *type, *offset and *hasbit. Otherwise returns false. */ +bool upb_msg_getscalarhandlerdata(const upb_handlers *h, + upb_selector_t s, + upb_fieldtype_t *type, + size_t *offset, + int32_t *hasbit); + + + UPB_END_EXTERN_C /* @@ -6381,21 +6609,16 @@ namespace upb { class Array; class Map; class MapIterator; -class MessageFactory; class MessageLayout; -class Visitor; -class VisitorPlan; } #endif -UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory) -UPB_DECLARE_TYPE(upb::MessageLayout, upb_msglayout) -UPB_DECLARE_TYPE(upb::Array, upb_array) UPB_DECLARE_TYPE(upb::Map, upb_map) UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter) -UPB_DECLARE_TYPE(upb::Visitor, upb_visitor) -UPB_DECLARE_TYPE(upb::VisitorPlan, upb_visitorplan) + +struct upb_array; +typedef struct upb_array upb_array; /* TODO(haberman): C++ accessors */ @@ -6406,70 +6629,45 @@ typedef void upb_msg; /** upb_msglayout *************************************************************/ -/* upb_msglayout represents the memory layout of a given upb_msgdef. You get - * instances of this from a upb_msgfactory, and the factory always owns the - * msglayout. */ - - -/** upb_visitor ***************************************************************/ - -/* upb_visitor will visit all the fields of a message and its submessages. It - * uses a upb_visitorplan which you can obtain from a upb_msgfactory. */ - -upb_visitor *upb_visitor_create(upb_env *e, const upb_visitorplan *vp, - upb_sink *output); -bool upb_visitor_visitmsg(upb_visitor *v, const upb_msg *msg); +/* upb_msglayout represents the memory layout of a given upb_msgdef. The + * members are public so generated code can initialize them, but users MUST NOT + * read or write any of its members. */ +typedef struct { + uint32_t number; + uint16_t offset; + int16_t presence; /* If >0, hasbit_index+1. If <0, oneof_index+1. */ + uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */ + uint8_t descriptortype; + uint8_t label; +} upb_msglayout_field; -/** upb_msgfactory ************************************************************/ - -/* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and - * upb_visitorplan objects. These are the objects necessary to represent, - * populate, and and visit upb_msg objects. - * - * These caches are all populated by upb_msgdef, and lazily created on demand. - */ - -/* Creates and destroys a msgfactory, respectively. The messages for this - * msgfactory must come from |symtab| (which should outlive the msgfactory). */ -upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab); -void upb_msgfactory_free(upb_msgfactory *f); - -const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f); - -/* The functions to get cached objects, lazily creating them on demand. These - * all require: - * - * - m is in upb_msgfactory_symtab(f) - * - upb_msgdef_mapentry(m) == false (since map messages can't have layouts). - * - * The returned objects will live for as long as the msgfactory does. - * - * TODO(haberman): consider making this thread-safe and take a const - * upb_msgfactory. */ -const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, - const upb_msgdef *m); -const upb_handlers *upb_msgfactory_getmergehandlers(upb_msgfactory *f, - const upb_msgdef *m); -const upb_visitorplan *upb_msgfactory_getvisitorplan(upb_msgfactory *f, - const upb_handlers *h); +typedef struct upb_msglayout { + const struct upb_msglayout *const* submsgs; + const upb_msglayout_field *fields; + /* Must be aligned to sizeof(void*). Doesn't include internal members like + * unknown fields, extension dict, pointer to msglayout, etc. */ + uint16_t size; + uint16_t field_count; + bool extendable; +} upb_msglayout; -/** upb_stringview ************************************************************/ +/** upb_strview ************************************************************/ typedef struct { const char *data; size_t size; -} upb_stringview; +} upb_strview; -UPB_INLINE upb_stringview upb_stringview_make(const char *data, size_t size) { - upb_stringview ret; +UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) { + upb_strview ret; ret.data = data; ret.size = size; return ret; } -#define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len} +#define UPB_STRVIEW_INIT(ptr, len) {ptr, len} /** upb_msgval ****************************************************************/ @@ -6489,7 +6687,7 @@ typedef union { const upb_msg* msg; const upb_array* arr; const void* ptr; - upb_stringview str; + upb_strview str; } upb_msgval; #define ACCESSORS(name, membername, ctype) \ @@ -6516,64 +6714,28 @@ ACCESSORS(map, map, const upb_map*) ACCESSORS(msg, msg, const upb_msg*) ACCESSORS(ptr, ptr, const void*) ACCESSORS(arr, arr, const upb_array*) -ACCESSORS(str, str, upb_stringview) +ACCESSORS(str, str, upb_strview) #undef ACCESSORS UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) { - return upb_msgval_str(upb_stringview_make(data, size)); + return upb_msgval_str(upb_strview_make(data, size)); } /** upb_msg *******************************************************************/ /* A upb_msg represents a protobuf message. It always corresponds to a specific - * upb_msglayout, which describes how it is laid out in memory. - * - * The message will have a fixed size, as returned by upb_msg_sizeof(), which - * will be used to store fixed-length fields. The upb_msg may also allocate - * dynamic memory internally to store data such as: - * - * - extensions - * - unknown fields - */ + * upb_msglayout, which describes how it is laid out in memory. */ -/* Returns the size of a message given this layout. */ -size_t upb_msg_sizeof(const upb_msglayout *l); +/* Creates a new message of the given type/layout in this arena. */ +upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a); -/* upb_msg_init() / upb_msg_uninit() allow the user to use a pre-allocated - * block of memory as a message. The block's size should be upb_msg_sizeof(). - * upb_msg_uninit() must be called to release internally-allocated memory - * unless the allocator is an arena that does not require freeing. - * - * Please note that upb_msg_init() may return a value that is different than - * |msg|, so you must assign the return value and not cast your memory block - * to upb_msg* directly! - * - * Please note that upb_msg_uninit() does *not* free any submessages, maps, - * or arrays referred to by this message's fields. You must free them manually - * yourself. - * - * upb_msg_uninit returns the original memory block, which may be useful if - * you dynamically allocated it (though upb_msg_new() would normally be more - * appropriate in this case). */ -upb_msg *upb_msg_init(void *msg, const upb_msglayout *l, upb_alloc *a); -void *upb_msg_uninit(upb_msg *msg, const upb_msglayout *l); - -/* Like upb_msg_init() / upb_msg_uninit(), except the message's memory is - * allocated / freed from the given upb_alloc. */ -upb_msg *upb_msg_new(const upb_msglayout *l, upb_alloc *a); -void upb_msg_free(upb_msg *msg, const upb_msglayout *l); - -/* Returns the upb_alloc for the given message. - * TODO(haberman): get rid of this? Not sure we want to be storing this - * for every message. */ -upb_alloc *upb_msg_alloc(const upb_msg *msg); - -/* Packs the tree of messages rooted at "msg" into a single hunk of memory, - * allocated from the given allocator. */ -void *upb_msg_pack(const upb_msg *msg, const upb_msglayout *l, - void *p, size_t *ofs, size_t size); +/* Returns the arena for the given message. */ +upb_arena *upb_msg_arena(const upb_msg *msg); + +void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len); +const char *upb_msg_getunknown(const upb_msg *msg, size_t *len); /* Read-only message API. Can be safely called by anyone. */ @@ -6627,16 +6789,12 @@ bool upb_msg_clearfield(upb_msg *msg, * semantics are the same as upb_msg. A upb_array allocates dynamic * memory internally for the array elements. */ -size_t upb_array_sizeof(upb_fieldtype_t type); -void upb_array_init(upb_array *arr, upb_fieldtype_t type, upb_alloc *a); -void upb_array_uninit(upb_array *arr); -upb_array *upb_array_new(upb_fieldtype_t type, upb_alloc *a); -void upb_array_free(upb_array *arr); +upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a); +upb_fieldtype_t upb_array_type(const upb_array *arr); /* Read-only interface. Safe for anyone to call. */ size_t upb_array_size(const upb_array *arr); -upb_fieldtype_t upb_array_type(const upb_array *arr); upb_msgval upb_array_get(const upb_array *arr, size_t i); /* Write interface. May only be called by the message's owner who can enforce @@ -6653,12 +6811,8 @@ bool upb_array_set(upb_array *arr, size_t i, upb_msgval val); * So you must ensure that any string or message values outlive the map, and you * must delete them manually when they are no longer required. */ -size_t upb_map_sizeof(upb_fieldtype_t ktype, upb_fieldtype_t vtype); -bool upb_map_init(upb_map *map, upb_fieldtype_t ktype, upb_fieldtype_t vtype, - upb_alloc *a); -void upb_map_uninit(upb_map *map); -upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype, upb_alloc *a); -void upb_map_free(upb_map *map); +upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype, + upb_arena *a); /* Read-only interface. Safe for anyone to call. */ @@ -6702,90 +6856,29 @@ upb_msgval upb_mapiter_value(const upb_mapiter *i); void upb_mapiter_setdone(upb_mapiter *i); bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2); - -/** Handlers ******************************************************************/ - -/* These are the handlers used internally by upb_msgfactory_getmergehandlers(). - * They write scalar data to a known offset from the message pointer. - * - * These would be trivial for anyone to implement themselves, but it's better - * to use these because some JITs will recognize and specialize these instead - * of actually calling the function. */ - -/* Sets a handler for the given primitive field that will write the data at the - * given offset. If hasbit > 0, also sets a hasbit at the given bit offset - * (addressing each byte low to high). */ -bool upb_msg_setscalarhandler(upb_handlers *h, - const upb_fielddef *f, - size_t offset, - int32_t hasbit); - -/* If the given handler is a msghandlers_primitive field, returns true and sets - * *type, *offset and *hasbit. Otherwise returns false. */ -bool upb_msg_getscalarhandlerdata(const upb_handlers *h, - upb_selector_t s, - upb_fieldtype_t *type, - size_t *offset, - int32_t *hasbit); - - -/** Interfaces for generated code *********************************************/ - -#define UPB_NOT_IN_ONEOF UINT16_MAX -#define UPB_NO_HASBIT UINT16_MAX -#define UPB_NO_SUBMSG UINT16_MAX - -typedef struct { - uint32_t number; - uint32_t offset; /* If in a oneof, offset of default in default_msg below. */ - uint16_t hasbit; /* UPB_NO_HASBIT if no hasbit. */ - uint16_t oneof_index; /* UPB_NOT_IN_ONEOF if not in a oneof. */ - uint16_t submsg_index; /* UPB_NO_SUBMSG if no submsg. */ - uint8_t type; - uint8_t label; -} upb_msglayout_fieldinit_v1; - -typedef struct { - uint32_t data_offset; - uint32_t case_offset; -} upb_msglayout_oneofinit_v1; - -typedef struct upb_msglayout_msginit_v1 { - const struct upb_msglayout_msginit_v1 *const* submsgs; - const upb_msglayout_fieldinit_v1 *fields; - const upb_msglayout_oneofinit_v1 *oneofs; - void *default_msg; - /* Must be aligned to sizeof(void*). Doesn't include internal members like - * unknown fields, extension dict, pointer to msglayout, etc. */ - uint32_t size; - uint16_t field_count; - uint16_t oneof_count; - bool extendable; - bool is_proto2; -} upb_msglayout_msginit_v1; - -#define UPB_ALIGN_UP_TO(val, align) ((val + (align - 1)) & -align) -#define UPB_ALIGNED_SIZEOF(type) UPB_ALIGN_UP_TO(sizeof(type), sizeof(void*)) - -/* Initialize/uninitialize a msglayout from a msginit. If upb uses v1 - * internally, this will not allocate any memory. Should only be used by - * generated code. */ -upb_msglayout *upb_msglayout_frominit_v1( - const upb_msglayout_msginit_v1 *init, upb_alloc *a); -void upb_msglayout_uninit_v1(upb_msglayout *layout, upb_alloc *a); - UPB_END_EXTERN_C #endif /* UPB_MSG_H_ */ +/* This file was generated by upbc (the upb compiler) from the input + * file: + * + * google/protobuf/descriptor.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. */ -UPB_BEGIN_EXTERN_C +#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ +#define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ -bool upb_decode(upb_stringview buf, void *msg, - const upb_msglayout_msginit_v1 *l, upb_env *env); +/* +** Functions for use by generated code. These are not public and users must +** not call them directly. +*/ -UPB_END_EXTERN_C +#ifndef UPB_GENERATED_UTIL_H_ +#define UPB_GENERATED_UTIL_H_ -#endif /* UPB_DECODE_H_ */ +#include /* ** structs.int.h: structures definitions that are internal to upb. */ @@ -6793,223 +6886,1843 @@ UPB_END_EXTERN_C #ifndef UPB_STRUCTS_H_ #define UPB_STRUCTS_H_ + struct upb_array { upb_fieldtype_t type; uint8_t element_size; void *data; /* Each element is element_size. */ size_t len; /* Measured in elements. */ size_t size; /* Measured in elements. */ - upb_alloc *alloc; + upb_arena *arena; }; #endif /* UPB_STRUCTS_H_ */ -/* -** This file contains definitions of structs that should be considered private -** and NOT stable across versions of upb. -** -** The only reason they are declared here and not in .c files is to allow upb -** and the application (if desired) to embed statically-initialized instances -** of structures like defs. -** -** If you include this file, all guarantees of ABI compatibility go out the -** window! Any code that includes this file needs to recompile against the -** exact same version of upb that they are linking against. -** -** You also need to recompile if you change the value of the UPB_DEBUG_REFS -** flag. -*/ +#define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs) -#ifndef UPB_STATICINIT_H_ -#define UPB_STATICINIT_H_ +UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs, + size_t *size) { + const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*); + if (arr) { + if (size) *size = arr->size; + return arr->data; + } else { + if (size) *size = 0; + return NULL; + } +} -#ifdef __cplusplus -/* Because of how we do our typedefs, this header can't be included from C++. */ -#error This file cannot be included from C++ -#endif +UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs, + size_t *size) { + upb_array *arr = *PTR_AT(msg, ofs, upb_array*); + if (arr) { + if (size) *size = arr->size; + return arr->data; + } else { + if (size) *size = 0; + return NULL; + } +} -/* upb_refcounted *************************************************************/ +/* TODO(haberman): this is a mess. It will improve when upb_array no longer + * carries reflective state (type, elem_size). */ +UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size, + size_t elem_size, + upb_fieldtype_t type, + upb_arena *arena) { + upb_array *arr = *PTR_AT(msg, ofs, upb_array*); + if (!arr) { + arr = upb_array_new(type, arena); + if (!arr) return NULL; + *PTR_AT(msg, ofs, upb_array*) = arr; + } -/* upb_def ********************************************************************/ + if (size > arr->size) { + size_t new_size = UPB_MAX(arr->size, 4); + size_t old_bytes = arr->size * elem_size; + size_t new_bytes; + upb_alloc *alloc = upb_arena_alloc(arr->arena); + while (new_size < size) new_size *= 2; + new_bytes = new_size * elem_size; + arr->data = upb_realloc(alloc, arr->data, old_bytes, new_bytes); + if (!arr->data) { + return NULL; + } + arr->size = new_size; + } -struct upb_def { - upb_refcounted base; + arr->len = size; + return arr->data; +} + +UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs, + size_t elem_size, + upb_fieldtype_t type, + const void *value, + upb_arena *arena) { + upb_array *arr = *PTR_AT(msg, ofs, upb_array*); + size_t i = arr ? arr->len : 0; + void *data = + _upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena); + if (!data) return false; + memcpy(PTR_AT(data, i * elem_size, char), value, elem_size); + return true; +} - const char *fullname; - const upb_filedef* file; - char type; /* A upb_deftype_t (char to save space) */ +UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) { + return (*PTR_AT(msg, idx / 8, const char) & (idx % 8)) != 0; +} - /* Used as a flag during the def's mutable stage. Must be false unless - * it is currently being used by a function on the stack. This allows - * us to easily determine which defs were passed into the function's - * current invocation. */ - bool came_from_user; -}; +UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) { + return (*PTR_AT(msg, idx / 8, char)) |= (1 << (idx % 8)); +} -#define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \ - { UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false } +UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) { + return (*PTR_AT(msg, idx / 8, char)) &= ~(1 << (idx % 8)); +} +UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) { + return *PTR_AT(msg, case_ofs, int32_t) == num; +} -/* upb_fielddef ***************************************************************/ +#undef PTR_AT -struct upb_fielddef { - upb_def base; +#endif /* UPB_GENERATED_UTIL_H_ */ - union { - int64_t sint; - uint64_t uint; - double dbl; - float flt; - void *bytes; - } defaultval; - union { - const upb_msgdef *def; /* If !msg_is_symbolic. */ - char *name; /* If msg_is_symbolic. */ - } msg; - union { - const upb_def *def; /* If !subdef_is_symbolic. */ - char *name; /* If subdef_is_symbolic. */ - } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */ - bool subdef_is_symbolic; - bool msg_is_symbolic; - const upb_oneofdef *oneof; - bool default_is_string; - bool type_is_set_; /* False until type is explicitly set. */ - bool is_extension_; - bool lazy_; - bool packed_; - upb_intfmt_t intfmt; - bool tagdelim; - upb_fieldtype_t type_; - upb_label_t label_; - uint32_t number_; - uint32_t selector_base; /* Used to index into a upb::Handlers table. */ - uint32_t index_; -}; -extern const struct upb_refcounted_vtbl upb_fielddef_vtbl; +/* +** upb_decode: parsing into a upb_msg using a upb_msglayout. +*/ + +#ifndef UPB_DECODE_H_ +#define UPB_DECODE_H_ + + +UPB_BEGIN_EXTERN_C + +bool upb_decode(upb_strview buf, upb_msg *msg, const upb_msglayout *l); + +UPB_END_EXTERN_C + +#endif /* UPB_DECODE_H_ */ +/* +** upb_encode: parsing into a upb_msg using a upb_msglayout. +*/ + +#ifndef UPB_ENCODE_H_ +#define UPB_ENCODE_H_ + + +UPB_BEGIN_EXTERN_C + +char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena, + size_t *size); + +UPB_END_EXTERN_C + +#endif /* UPB_ENCODE_H_ */ +UPB_BEGIN_EXTERN_C + +struct google_protobuf_FileDescriptorSet; +struct google_protobuf_FileDescriptorProto; +struct google_protobuf_DescriptorProto; +struct google_protobuf_DescriptorProto_ExtensionRange; +struct google_protobuf_DescriptorProto_ReservedRange; +struct google_protobuf_ExtensionRangeOptions; +struct google_protobuf_FieldDescriptorProto; +struct google_protobuf_OneofDescriptorProto; +struct google_protobuf_EnumDescriptorProto; +struct google_protobuf_EnumDescriptorProto_EnumReservedRange; +struct google_protobuf_EnumValueDescriptorProto; +struct google_protobuf_ServiceDescriptorProto; +struct google_protobuf_MethodDescriptorProto; +struct google_protobuf_FileOptions; +struct google_protobuf_MessageOptions; +struct google_protobuf_FieldOptions; +struct google_protobuf_OneofOptions; +struct google_protobuf_EnumOptions; +struct google_protobuf_EnumValueOptions; +struct google_protobuf_ServiceOptions; +struct google_protobuf_MethodOptions; +struct google_protobuf_UninterpretedOption; +struct google_protobuf_UninterpretedOption_NamePart; +struct google_protobuf_SourceCodeInfo; +struct google_protobuf_SourceCodeInfo_Location; +struct google_protobuf_GeneratedCodeInfo; +struct google_protobuf_GeneratedCodeInfo_Annotation; +typedef struct google_protobuf_FileDescriptorSet google_protobuf_FileDescriptorSet; +typedef struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto; +typedef struct google_protobuf_DescriptorProto google_protobuf_DescriptorProto; +typedef struct google_protobuf_DescriptorProto_ExtensionRange google_protobuf_DescriptorProto_ExtensionRange; +typedef struct google_protobuf_DescriptorProto_ReservedRange google_protobuf_DescriptorProto_ReservedRange; +typedef struct google_protobuf_ExtensionRangeOptions google_protobuf_ExtensionRangeOptions; +typedef struct google_protobuf_FieldDescriptorProto google_protobuf_FieldDescriptorProto; +typedef struct google_protobuf_OneofDescriptorProto google_protobuf_OneofDescriptorProto; +typedef struct google_protobuf_EnumDescriptorProto google_protobuf_EnumDescriptorProto; +typedef struct google_protobuf_EnumDescriptorProto_EnumReservedRange google_protobuf_EnumDescriptorProto_EnumReservedRange; +typedef struct google_protobuf_EnumValueDescriptorProto google_protobuf_EnumValueDescriptorProto; +typedef struct google_protobuf_ServiceDescriptorProto google_protobuf_ServiceDescriptorProto; +typedef struct google_protobuf_MethodDescriptorProto google_protobuf_MethodDescriptorProto; +typedef struct google_protobuf_FileOptions google_protobuf_FileOptions; +typedef struct google_protobuf_MessageOptions google_protobuf_MessageOptions; +typedef struct google_protobuf_FieldOptions google_protobuf_FieldOptions; +typedef struct google_protobuf_OneofOptions google_protobuf_OneofOptions; +typedef struct google_protobuf_EnumOptions google_protobuf_EnumOptions; +typedef struct google_protobuf_EnumValueOptions google_protobuf_EnumValueOptions; +typedef struct google_protobuf_ServiceOptions google_protobuf_ServiceOptions; +typedef struct google_protobuf_MethodOptions google_protobuf_MethodOptions; +typedef struct google_protobuf_UninterpretedOption google_protobuf_UninterpretedOption; +typedef struct google_protobuf_UninterpretedOption_NamePart google_protobuf_UninterpretedOption_NamePart; +typedef struct google_protobuf_SourceCodeInfo google_protobuf_SourceCodeInfo; +typedef struct google_protobuf_SourceCodeInfo_Location google_protobuf_SourceCodeInfo_Location; +typedef struct google_protobuf_GeneratedCodeInfo google_protobuf_GeneratedCodeInfo; +typedef struct google_protobuf_GeneratedCodeInfo_Annotation google_protobuf_GeneratedCodeInfo_Annotation; +extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit; +extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_DescriptorProto_msginit; +extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit; +extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit; +extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit; +extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit; +extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit; +extern const upb_msglayout google_protobuf_FileOptions_msginit; +extern const upb_msglayout google_protobuf_MessageOptions_msginit; +extern const upb_msglayout google_protobuf_FieldOptions_msginit; +extern const upb_msglayout google_protobuf_OneofOptions_msginit; +extern const upb_msglayout google_protobuf_EnumOptions_msginit; +extern const upb_msglayout google_protobuf_EnumValueOptions_msginit; +extern const upb_msglayout google_protobuf_ServiceOptions_msginit; +extern const upb_msglayout google_protobuf_MethodOptions_msginit; +extern const upb_msglayout google_protobuf_UninterpretedOption_msginit; +extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit; +extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit; +extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit; +extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit; +extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit; + +/* Enums */ + +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_FileOptions_SPEED = 1, + google_protobuf_FileOptions_CODE_SIZE = 2, + google_protobuf_FileOptions_LITE_RUNTIME = 3 +} google_protobuf_FileOptions_OptimizeMode; + +typedef enum { + google_protobuf_MethodOptions_IDEMPOTENCY_UNKNOWN = 0, + google_protobuf_MethodOptions_NO_SIDE_EFFECTS = 1, + google_protobuf_MethodOptions_IDEMPOTENT = 2 +} google_protobuf_MethodOptions_IdempotencyLevel; + +/* google.protobuf.FileDescriptorSet */ + +UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_arena *arena) { + return (google_protobuf_FileDescriptorSet *)upb_msg_new(&google_protobuf_FileDescriptorSet_msginit, arena); +} +UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_FileDescriptorSet *ret = google_protobuf_FileDescriptorSet_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_FileDescriptorSet_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, arena, len); +} + +UPB_INLINE const google_protobuf_FileDescriptorProto* const* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg, size_t *len) { return (const google_protobuf_FileDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } + +UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_mutable_file(google_protobuf_FileDescriptorSet *msg, size_t *len) { + return (google_protobuf_FileDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); +} +UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_resize_file(google_protobuf_FileDescriptorSet *msg, size_t len, upb_arena *arena) { + return (google_protobuf_FileDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorSet_add_file(google_protobuf_FileDescriptorSet *msg, upb_arena *arena) { + struct google_protobuf_FileDescriptorProto* sub = (struct google_protobuf_FileDescriptorProto*)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} + + +/* google.protobuf.FileDescriptorProto */ + +UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_FileDescriptorProto *)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena); +} +UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_FileDescriptorProto *ret = google_protobuf_FileDescriptorProto_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_FileDescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_package(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); } +UPB_INLINE upb_strview const* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); } +UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); } +UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); } +UPB_INLINE const google_protobuf_ServiceDescriptorProto* const* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_ServiceDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(48, 96), len); } +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(52, 104), len); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_options(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FileOptions*, UPB_SIZE(28, 56)); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)); } +UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(56, 112), len); } +UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(60, 120), len); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_syntax(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); } + +UPB_INLINE void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value; +} +UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_mutable_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len); +} +UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_resize_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_dependency(google_protobuf_FileDescriptorProto *msg, upb_strview val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(36, 72), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_mutable_message_type(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len); +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_resize_message_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_FileDescriptorProto_add_message_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_mutable_enum_type(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len); +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_resize_enum_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_FileDescriptorProto_add_enum_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(44, 88), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_mutable_service(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (google_protobuf_ServiceDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(48, 96), len); +} +UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_resize_service(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_ServiceDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(48, 96), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_ServiceDescriptorProto* google_protobuf_FileDescriptorProto_add_service(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_ServiceDescriptorProto* sub = (struct google_protobuf_ServiceDescriptorProto*)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(48, 96), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_mutable_extension(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(52, 104), len); +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_resize_extension(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(52, 104), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_FileDescriptorProto_add_extension(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(52, 104), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, google_protobuf_FileOptions*, UPB_SIZE(28, 56)) = 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_msg_new(&google_protobuf_FileOptions_msginit, arena); + if (!sub) return NULL; + 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) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)) = 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_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena); + if (!sub) return NULL; + 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 *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 112), len); +} +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_public_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(56, 112), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_public_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(56, 112), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); +} +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(60, 120), len); +} +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(60, 120), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_weak_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(60, 120), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value; +} + + +/* google.protobuf.DescriptorProto */ + +UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_arena *arena) { + return (google_protobuf_DescriptorProto *)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena); +} +UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_DescriptorProto *ret = google_protobuf_DescriptorProto_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE upb_strview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } +UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } +UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } +UPB_INLINE const google_protobuf_DescriptorProto_ExtensionRange* const* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ExtensionRange* const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); } +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(32, 64), len); } +UPB_INLINE bool google_protobuf_DescriptorProto_has_options(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MessageOptions*, UPB_SIZE(12, 24)); } +UPB_INLINE const google_protobuf_OneofDescriptorProto* const* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_OneofDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); } +UPB_INLINE const google_protobuf_DescriptorProto_ReservedRange* const* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); } +UPB_INLINE upb_strview const* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); } + +UPB_INLINE void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_field(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len); +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_field(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_field(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_mutable_nested_type(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len); +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_resize_nested_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_add_nested_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_mutable_enum_type(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len); +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_resize_enum_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_DescriptorProto_add_enum_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(24, 48), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_mutable_extension_range(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len); +} +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_resize_extension_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_add_extension_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_DescriptorProto_ExtensionRange* sub = (struct google_protobuf_DescriptorProto_ExtensionRange*)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(28, 56), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_extension(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(32, 64), len); +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_extension(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(32, 64), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_extension(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(32, 64), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, google_protobuf_MessageOptions*, UPB_SIZE(12, 24)) = 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_msg_new(&google_protobuf_MessageOptions_msginit, arena); + if (!sub) return NULL; + 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 *len) { + return (google_protobuf_OneofDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len); +} +UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_resize_oneof_decl(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_OneofDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_OneofDescriptorProto* google_protobuf_DescriptorProto_add_oneof_decl(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_OneofDescriptorProto* sub = (struct google_protobuf_OneofDescriptorProto*)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(36, 72), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_mutable_reserved_range(google_protobuf_DescriptorProto *msg, size_t *len) { + return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len); +} +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_resize_reserved_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_add_reserved_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_DescriptorProto_ReservedRange* sub = (struct google_protobuf_DescriptorProto_ReservedRange*)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE upb_strview* google_protobuf_DescriptorProto_mutable_reserved_name(google_protobuf_DescriptorProto *msg, size_t *len) { + return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len); +} +UPB_INLINE upb_strview* google_protobuf_DescriptorProto_resize_reserved_name(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) { + return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena); +} +UPB_INLINE bool google_protobuf_DescriptorProto_add_reserved_name(google_protobuf_DescriptorProto *msg, upb_strview val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(44, 88), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); +} + + +/* google.protobuf.DescriptorProto.ExtensionRange */ + +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_arena *arena) { + return (google_protobuf_DescriptorProto_ExtensionRange *)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena); +} +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_DescriptorProto_ExtensionRange *ret = google_protobuf_DescriptorProto_ExtensionRange_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_ExtensionRange_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)); } + +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; +} +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; +} +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)) = 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_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena); + if (!sub) return NULL; + 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_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena); +} +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_DescriptorProto_ReservedRange *ret = google_protobuf_DescriptorProto_ReservedRange_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_ReservedRange_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } + +UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; +} +UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; +} + + +/* google.protobuf.ExtensionRangeOptions */ + +UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_arena *arena) { + return (google_protobuf_ExtensionRangeOptions *)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena); +} +UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_ExtensionRangeOptions *ret = google_protobuf_ExtensionRangeOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_ExtensionRangeOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, arena, len); +} + +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_mutable_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_resize_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ExtensionRangeOptions_add_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} + + +/* google.protobuf.FieldDescriptorProto */ + +UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_FieldDescriptorProto *)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); +} +UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_FieldDescriptorProto *ret = google_protobuf_FieldDescriptorProto_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_FieldDescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_extendee(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_number(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_label(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE google_protobuf_FieldDescriptorProto_Label google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Label, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE google_protobuf_FieldDescriptorProto_Type google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Type, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_default_value(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 7); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_options(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 9); } +UPB_INLINE const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FieldOptions*, UPB_SIZE(72, 112)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_json_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 8); } +UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)); } + +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)) = value; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Label value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Label, UPB_SIZE(8, 8)) = value; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Type value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Type, UPB_SIZE(16, 16)) = value; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 6); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 7); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)) = value; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value) { + _upb_sethas(msg, 9); + UPB_FIELD_AT(msg, google_protobuf_FieldOptions*, UPB_SIZE(72, 112)) = 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_msg_new(&google_protobuf_FieldOptions_msginit, arena); + if (!sub) return NULL; + 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) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)) = value; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 8); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)) = value; +} + + +/* google.protobuf.OneofDescriptorProto */ + +UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_OneofDescriptorProto *)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena); +} +UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_OneofDescriptorProto *ret = google_protobuf_OneofDescriptorProto_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_OneofDescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE upb_strview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_options(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_OneofOptions*, UPB_SIZE(12, 24)); } + +UPB_INLINE void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, google_protobuf_OneofOptions*, UPB_SIZE(12, 24)) = 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_msg_new(&google_protobuf_OneofOptions_msginit, arena); + if (!sub) return NULL; + 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_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena); +} +UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_EnumDescriptorProto *ret = google_protobuf_EnumDescriptorProto_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_EnumDescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE upb_strview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE const google_protobuf_EnumValueDescriptorProto* const* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumValueDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_options(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumOptions*, UPB_SIZE(12, 24)); } +UPB_INLINE const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto_EnumReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } +UPB_INLINE upb_strview const* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } + +UPB_INLINE void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_mutable_value(google_protobuf_EnumDescriptorProto *msg, size_t *len) { + return (google_protobuf_EnumValueDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len); +} +UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_resize_value(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_EnumValueDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumDescriptorProto_add_value(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_EnumValueDescriptorProto* sub = (struct google_protobuf_EnumValueDescriptorProto*)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, google_protobuf_EnumOptions*, UPB_SIZE(12, 24)) = 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_msg_new(&google_protobuf_EnumOptions_msginit, arena); + if (!sub) return NULL; + 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 *len) { + return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len); +} +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_resize_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_add_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_EnumDescriptorProto_EnumReservedRange* sub = (struct google_protobuf_EnumDescriptorProto_EnumReservedRange*)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_mutable_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t *len) { + return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len); +} +UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_resize_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) { + return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena); +} +UPB_INLINE bool google_protobuf_EnumDescriptorProto_add_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_strview val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(24, 48), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); +} + + +/* google.protobuf.EnumDescriptorProto.EnumReservedRange */ + +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_arena *arena) { + return (google_protobuf_EnumDescriptorProto_EnumReservedRange *)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena); +} +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_EnumDescriptorProto_EnumReservedRange *ret = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } + +UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; +} + + +/* google.protobuf.EnumValueDescriptorProto */ + +UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_arena *arena) { + return (google_protobuf_EnumValueDescriptorProto *)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena); +} +UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_EnumValueDescriptorProto *ret = google_protobuf_EnumValueDescriptorProto_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_EnumValueDescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_number(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_options(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)); } + +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)) = value; +} +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; +} +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)) = 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_msg_new(&google_protobuf_EnumValueOptions_msginit, arena); + if (!sub) return NULL; + 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_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena); +} +UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_ServiceDescriptorProto *ret = google_protobuf_ServiceDescriptorProto_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_ServiceDescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE upb_strview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE const google_protobuf_MethodDescriptorProto* const* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg, size_t *len) { return (const google_protobuf_MethodDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } +UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_options(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)); } + +UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_mutable_method(google_protobuf_ServiceDescriptorProto *msg, size_t *len) { + return (google_protobuf_MethodDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len); +} +UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_resize_method(google_protobuf_ServiceDescriptorProto *msg, size_t len, upb_arena *arena) { + return (google_protobuf_MethodDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_MethodDescriptorProto* google_protobuf_ServiceDescriptorProto_add_method(google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena) { + struct google_protobuf_MethodDescriptorProto* sub = (struct google_protobuf_MethodDescriptorProto*)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)) = 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_msg_new(&google_protobuf_ServiceOptions_msginit, arena); + if (!sub) return NULL; + 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_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena); +} +UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_MethodDescriptorProto *ret = google_protobuf_MethodDescriptorProto_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_MethodDescriptorProto_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_input_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_output_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_options(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MethodOptions*, UPB_SIZE(28, 56)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } + +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, google_protobuf_MethodOptions*, UPB_SIZE(28, 56)) = 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_msg_new(&google_protobuf_MethodOptions_msginit, arena); + if (!sub) return NULL; + google_protobuf_MethodDescriptorProto_set_options(msg, sub); + } + return sub; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; +} + + +/* google.protobuf.FileOptions */ + +UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_arena *arena) { + return (google_protobuf_FileOptions *)upb_msg_new(&google_protobuf_FileOptions_msginit, arena); +} +UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_FileOptions *ret = google_protobuf_FileOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_FileOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FileOptions_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 10); } +UPB_INLINE upb_strview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_outer_classname(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 11); } +UPB_INLINE upb_strview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)); } +UPB_INLINE bool google_protobuf_FileOptions_has_optimize_for(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE google_protobuf_FileOptions_OptimizeMode google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FileOptions_OptimizeMode, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_multiple_files(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_FileOptions_has_go_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 12); } +UPB_INLINE upb_strview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)); } +UPB_INLINE bool google_protobuf_FileOptions_has_cc_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)); } +UPB_INLINE bool google_protobuf_FileOptions_has_py_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)); } +UPB_INLINE bool google_protobuf_FileOptions_has_deprecated(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 7); } +UPB_INLINE bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)); } +UPB_INLINE bool google_protobuf_FileOptions_has_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 8); } +UPB_INLINE bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)); } +UPB_INLINE bool google_protobuf_FileOptions_has_objc_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 13); } +UPB_INLINE upb_strview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)); } +UPB_INLINE bool google_protobuf_FileOptions_has_csharp_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 14); } +UPB_INLINE upb_strview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)); } +UPB_INLINE bool google_protobuf_FileOptions_has_swift_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 15); } +UPB_INLINE upb_strview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 16); } +UPB_INLINE upb_strview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 17); } +UPB_INLINE upb_strview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 9); } +UPB_INLINE bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(92, 160), len); } + +UPB_INLINE void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 10); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 11); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, google_protobuf_FileOptions_OptimizeMode value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, google_protobuf_FileOptions_OptimizeMode, UPB_SIZE(8, 8)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 12); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 6); + UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 7); + UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 8); + UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 13); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 14); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 15); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 16); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_strview value) { + _upb_sethas(msg, 17); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)) = value; +} +UPB_INLINE void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value) { + _upb_sethas(msg, 9); + UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_mutable_uninterpreted_option(google_protobuf_FileOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(92, 160), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_resize_uninterpreted_option(google_protobuf_FileOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(92, 160), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FileOptions_add_uninterpreted_option(google_protobuf_FileOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(92, 160), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} + + +/* google.protobuf.MessageOptions */ + +UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_arena *arena) { + return (google_protobuf_MessageOptions *)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena); +} +UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_MessageOptions *ret = google_protobuf_MessageOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_MessageOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_MessageOptions_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE bool google_protobuf_MessageOptions_has_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } +UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)); } +UPB_INLINE bool google_protobuf_MessageOptions_has_map_entry(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(8, 8), len); } + +UPB_INLINE void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; +} +UPB_INLINE void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; +} +UPB_INLINE void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)) = value; +} +UPB_INLINE void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)) = value; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_mutable_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(8, 8), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_resize_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(8, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MessageOptions_add_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(8, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} + + +/* google.protobuf.FieldOptions */ + +UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_arena *arena) { + return (google_protobuf_FieldOptions *)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena); +} +UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_FieldOptions *ret = google_protobuf_FieldOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_FieldOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_FieldOptions_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE google_protobuf_FieldOptions_CType google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldOptions_CType, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_packed(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_deprecated(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_lazy(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_jstype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE google_protobuf_FieldOptions_JSType google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldOptions_JSType, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_FieldOptions_has_weak(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(28, 32), len); } + +UPB_INLINE void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_CType value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, google_protobuf_FieldOptions_CType, UPB_SIZE(8, 8)) = value; +} +UPB_INLINE void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value; +} +UPB_INLINE void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)) = value; +} +UPB_INLINE void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)) = value; +} +UPB_INLINE void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_JSType value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, google_protobuf_FieldOptions_JSType, UPB_SIZE(16, 16)) = value; +} +UPB_INLINE void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)) = value; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_mutable_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 32), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_resize_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FieldOptions_add_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(28, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} + + +/* google.protobuf.OneofOptions */ + +UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_arena *arena) { + return (google_protobuf_OneofOptions *)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena); +} +UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_OneofOptions *ret = google_protobuf_OneofOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_OneofOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_OneofOptions_msginit, arena, len); +} + +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_mutable_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_resize_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_OneofOptions_add_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} + + +/* google.protobuf.EnumOptions */ + +UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_arena *arena) { + return (google_protobuf_EnumOptions *)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena); +} +UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_EnumOptions *ret = google_protobuf_EnumOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_EnumOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumOptions_msginit, arena, len); +} + +UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } + +UPB_INLINE void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; +} +UPB_INLINE void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_mutable_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_resize_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumOptions_add_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} + -#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \ - packed, name, num, msgdef, subdef, selector_base, \ - index, defaultval, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s), \ - defaultval, {msgdef}, {subdef}, NULL, false, false, \ - type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \ - lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \ - } +/* google.protobuf.EnumValueOptions */ +UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_arena *arena) { + return (google_protobuf_EnumValueOptions *)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena); +} +UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_EnumValueOptions *ret = google_protobuf_EnumValueOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_EnumValueOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, arena, len); +} -/* upb_msgdef *****************************************************************/ +UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } -struct upb_msgdef { - upb_def base; +UPB_INLINE void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_mutable_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_resize_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumValueOptions_add_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} - size_t selector_count; - uint32_t submsg_field_count; - /* Tables for looking up fields by number and name. */ - upb_inttable itof; /* int to field */ - upb_strtable ntof; /* name to field/oneof */ +/* google.protobuf.ServiceOptions */ - /* Is this a map-entry message? */ - bool map_entry; +UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_arena *arena) { + return (google_protobuf_ServiceOptions *)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena); +} +UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_ServiceOptions *ret = google_protobuf_ServiceOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_ServiceOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, arena, len); +} - /* Whether this message has proto2 or proto3 semantics. */ - upb_syntax_t syntax; +UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } - /* TODO(haberman): proper extension ranges (there can be multiple). */ -}; +UPB_INLINE void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_mutable_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_resize_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ServiceOptions_add_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} -extern const struct upb_refcounted_vtbl upb_msgdef_vtbl; -/* TODO: also support static initialization of the oneofs table. This will be - * needed if we compile in descriptors that contain oneofs. */ -#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \ - map_entry, syntax, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s), \ - selector_count, submsg_field_count, itof, ntof, map_entry, syntax \ - } +/* google.protobuf.MethodOptions */ +UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_arena *arena) { + return (google_protobuf_MethodOptions *)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena); +} +UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_MethodOptions *ret = google_protobuf_MethodOptions_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_MethodOptions_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_MethodOptions_msginit, arena, len); +} -/* upb_enumdef ****************************************************************/ +UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_MethodOptions_has_idempotency_level(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE google_protobuf_MethodOptions_IdempotencyLevel google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_MethodOptions_IdempotencyLevel, UPB_SIZE(8, 8)); } +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(20, 24), len); } -struct upb_enumdef { - upb_def base; +UPB_INLINE void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value; +} +UPB_INLINE void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, google_protobuf_MethodOptions_IdempotencyLevel value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, google_protobuf_MethodOptions_IdempotencyLevel, UPB_SIZE(8, 8)) = value; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_mutable_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t *len) { + return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 24), len); +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_resize_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 24), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MethodOptions_add_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(20, 24), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} - upb_strtable ntoi; - upb_inttable iton; - int32_t defaultval; -}; -extern const struct upb_refcounted_vtbl upb_enumdef_vtbl; +/* google.protobuf.UninterpretedOption */ -#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \ - { UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi, \ - iton, defaultval } +UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_arena *arena) { + return (google_protobuf_UninterpretedOption *)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); +} +UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_UninterpretedOption *ret = google_protobuf_UninterpretedOption_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_UninterpretedOption_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, arena, len); +} +UPB_INLINE const google_protobuf_UninterpretedOption_NamePart* const* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg, size_t *len) { return (const google_protobuf_UninterpretedOption_NamePart* const*)_upb_array_accessor(msg, UPB_SIZE(56, 80), len); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE upb_strview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_double_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_string_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE upb_strview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE upb_strview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); } -/* upb_oneofdef ***************************************************************/ +UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_mutable_name(google_protobuf_UninterpretedOption *msg, size_t *len) { + return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 80), len); +} +UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_resize_name(google_protobuf_UninterpretedOption *msg, size_t len, upb_arena *arena) { + return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_resize_accessor(msg, UPB_SIZE(56, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_add_name(google_protobuf_UninterpretedOption *msg, upb_arena *arena) { + struct google_protobuf_UninterpretedOption_NamePart* sub = (struct google_protobuf_UninterpretedOption_NamePart*)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(56, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_strview value) { + _upb_sethas(msg, 3); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value; +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)) = value; +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)) = value; +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)) = value; +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_strview value) { + _upb_sethas(msg, 4); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value; +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_strview value) { + _upb_sethas(msg, 5); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value; +} -struct upb_oneofdef { - upb_refcounted base; - uint32_t index; /* Index within oneofs. */ - const char *name; - upb_strtable ntof; - upb_inttable itof; - const upb_msgdef *parent; -}; +/* google.protobuf.UninterpretedOption.NamePart */ -extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl; +UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_arena *arena) { + return (google_protobuf_UninterpretedOption_NamePart *)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena); +} +UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_UninterpretedOption_NamePart *ret = google_protobuf_UninterpretedOption_NamePart_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_UninterpretedOption_NamePart_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, arena, len); +} -#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \ - { UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof } +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } +UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; +} -/* upb_symtab *****************************************************************/ -struct upb_symtab { - upb_refcounted base; +/* google.protobuf.SourceCodeInfo */ - upb_strtable symtab; -}; +UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_arena *arena) { + return (google_protobuf_SourceCodeInfo *)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena); +} +UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_SourceCodeInfo *ret = google_protobuf_SourceCodeInfo_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_SourceCodeInfo_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, arena, len); +} -struct upb_filedef { - upb_refcounted base; +UPB_INLINE const google_protobuf_SourceCodeInfo_Location* const* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg, size_t *len) { return (const google_protobuf_SourceCodeInfo_Location* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } - const char *name; - const char *package; - const char *phpprefix; - const char *phpnamespace; - upb_syntax_t syntax; +UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_mutable_location(google_protobuf_SourceCodeInfo *msg, size_t *len) { + return (google_protobuf_SourceCodeInfo_Location**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); +} +UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_resize_location(google_protobuf_SourceCodeInfo *msg, size_t len, upb_arena *arena) { + return (google_protobuf_SourceCodeInfo_Location**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_add_location(google_protobuf_SourceCodeInfo *msg, upb_arena *arena) { + struct google_protobuf_SourceCodeInfo_Location* sub = (struct google_protobuf_SourceCodeInfo_Location*)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + return sub; +} - upb_inttable defs; - upb_inttable deps; -}; -extern const struct upb_refcounted_vtbl upb_filedef_vtbl; +/* google.protobuf.SourceCodeInfo.Location */ -#endif /* UPB_STATICINIT_H_ */ -/* -** upb_encode: parsing into a upb_msg using a upb_msglayout. -*/ +UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_arena *arena) { + return (google_protobuf_SourceCodeInfo_Location *)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena); +} +UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_SourceCodeInfo_Location *ret = google_protobuf_SourceCodeInfo_Location_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_SourceCodeInfo_Location_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, arena, len); +} -#ifndef UPB_ENCODE_H_ -#define UPB_ENCODE_H_ +UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } +UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); } +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); } +UPB_INLINE upb_strview const* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); } + +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_path(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len); +} +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_path(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); +} +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_path(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(20, 40), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); +} +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_span(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len); +} +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_span(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); +} +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_span(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(24, 48), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value; +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value; +} +UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_mutable_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { + return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len); +} +UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_resize_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) { + return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena); +} +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(28, 56), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); +} -UPB_BEGIN_EXTERN_C +/* google.protobuf.GeneratedCodeInfo */ + +UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_arena *arena) { + return (google_protobuf_GeneratedCodeInfo *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_msginit, arena); +} +UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_GeneratedCodeInfo *ret = google_protobuf_GeneratedCodeInfo_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_GeneratedCodeInfo_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, arena, len); +} + +UPB_INLINE const google_protobuf_GeneratedCodeInfo_Annotation* const* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg, size_t *len) { return (const google_protobuf_GeneratedCodeInfo_Annotation* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } + +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_mutable_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t *len) { + return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); +} +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_resize_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t len, upb_arena *arena) { + return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena); +} +UPB_INLINE struct google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_add_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena) { + struct google_protobuf_GeneratedCodeInfo_Annotation* sub = (struct google_protobuf_GeneratedCodeInfo_Annotation*)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena); + bool ok = _upb_array_append_accessor( + msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena); + if (!ok) return NULL; + 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_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena); +} +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parsenew(upb_strview buf, upb_arena *arena) { + google_protobuf_GeneratedCodeInfo_Annotation *ret = google_protobuf_GeneratedCodeInfo_Annotation_new(arena); + return (ret && upb_decode(buf, ret, &google_protobuf_GeneratedCodeInfo_Annotation_msginit)) ? ret : NULL; +} +UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_arena *arena, size_t *len) { + return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena, len); +} + +UPB_INLINE int32_t const* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 32), len); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE upb_strview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } + +UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_mutable_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { + return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 32), len); +} +UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_resize_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t len, upb_arena *arena) { + return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 32), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena); +} +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_add_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t val, upb_arena *arena) { + return _upb_array_append_accessor( + msg, UPB_SIZE(20, 32), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena); +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_strview value) { + _upb_sethas(msg, 2); + UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)) = value; +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) { + _upb_sethas(msg, 0); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) { + _upb_sethas(msg, 1); + UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; +} -char *upb_encode(const void *msg, const upb_msglayout_msginit_v1 *l, - upb_env *env, size_t *size); UPB_END_EXTERN_C -#endif /* UPB_ENCODE_H_ */ + +#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ */ + + +#ifndef UPB_MSGFACTORY_H_ +#define UPB_MSGFACTORY_H_ + +#ifdef __cplusplus +namespace upb { +class MessageFactory; +} +#endif + +UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory) + +/** upb_msgfactory ************************************************************/ + +/* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and + * upb_visitorplan objects. These are the objects necessary to represent, + * populate, and and visit upb_msg objects. + * + * These caches are all populated by upb_msgdef, and lazily created on demand. + */ + +/* Creates and destroys a msgfactory, respectively. The messages for this + * msgfactory must come from |symtab| (which should outlive the msgfactory). */ +upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab); +void upb_msgfactory_free(upb_msgfactory *f); + +const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f); + +/* The functions to get cached objects, lazily creating them on demand. These + * all require: + * + * - m is in upb_msgfactory_symtab(f) + * - upb_msgdef_mapentry(m) == false (since map messages can't have layouts). + * + * The returned objects will live for as long as the msgfactory does. + * + * TODO(haberman): consider making this thread-safe and take a const + * upb_msgfactory. */ +const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, + const upb_msgdef *m); + + +#endif /* UPB_MSGFACTORY_H_ */ /* ** upb::descriptor::Reader (upb_descreader) ** @@ -7112,53 +8825,6 @@ inline FileDef* Reader::file(size_t i) const { UPB_BEGIN_EXTERN_C -/* Enums */ - -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_FileOptions_SPEED = 1, - google_protobuf_FileOptions_CODE_SIZE = 2, - google_protobuf_FileOptions_LITE_RUNTIME = 3 -} google_protobuf_FileOptions_OptimizeMode; - /* MessageDefs: call these functions to get a ref to a msgdef. */ const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner); const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner); @@ -8065,14 +9731,8 @@ inline const DecoderMethod *CodeCache::GetDecoderMethod( #endif /* UPB_DECODER_H_ */ -/* C++ names are not actually used since this type isn't exposed to users. */ -#ifdef __cplusplus -namespace upb { -namespace pb { -class MessageGroup; -} /* namespace pb */ -} /* namespace upb */ -#endif +#ifndef __cplusplus + UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted, mgroup, upb_refcounted) @@ -8135,7 +9795,7 @@ typedef enum { #define OP_MAX OP_HALT -UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; } +UPB_INLINE opcode getop(uint32_t instr) { return (opcode)(instr & 0xff); } /* Method group; represents a set of decoder methods that had their code * emitted together, and must therefore be freed together. Immutable once @@ -8382,6 +10042,8 @@ UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs, #define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; } +#endif /* __cplusplus */ + #endif /* UPB_DECODER_INT_H_ */ /* ** A number of routines for varint manipulation (we keep them all around to @@ -8793,7 +10455,7 @@ UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted, * constructed. This hint may be an overestimate for some build configurations. * But if the parser library is upgraded without recompiling the application, * it may be an underestimate. */ -#define UPB_JSON_PARSER_SIZE 4112 +#define UPB_JSON_PARSER_SIZE 5712 #ifdef __cplusplus @@ -8802,7 +10464,8 @@ UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted, class upb::json::Parser { public: static Parser* Create(Environment* env, const ParserMethod* method, - Sink* output); + const SymbolTable* symtab, + Sink* output, bool ignore_json_unknown); BytesSink* input(); @@ -8836,7 +10499,9 @@ UPB_BEGIN_EXTERN_C upb_json_parser* upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m, - upb_sink* output); + const upb_symtab* symtab, + upb_sink* output, + bool ignore_json_unknown); upb_bytessink *upb_json_parser_input(upb_json_parser *p); upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md, @@ -8856,8 +10521,10 @@ UPB_END_EXTERN_C namespace upb { namespace json { inline Parser* Parser::Create(Environment* env, const ParserMethod* method, - Sink* output) { - return upb_json_parser_create(env, method, output); + const SymbolTable* symtab, + Sink* output, bool ignore_json_unknown) { + return upb_json_parser_create( + env, method, symtab, output, ignore_json_unknown); } inline BytesSink* Parser::input() { return upb_json_parser_input(this); @@ -8906,7 +10573,7 @@ UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer) /* upb::json::Printer *********************************************************/ -#define UPB_JSON_PRINTER_SIZE 176 +#define UPB_JSON_PRINTER_SIZE 192 #ifdef __cplusplus @@ -8967,3 +10634,8 @@ inline reffed_ptr Printer::NewHandlers( #endif #endif /* UPB_JSON_TYPED_PRINTER_H_ */ + +#undef UPB_SIZE +#undef UPB_FIELD_AT +#undef UPB_READ_ONEOF +#undef UPB_WRITE_ONEOF diff --git a/ruby/ext/google/protobuf_c/wrap_memcpy.c b/ruby/ext/google/protobuf_c/wrap_memcpy.c index 394a52f9f6..18c036780c 100644 --- a/ruby/ext/google/protobuf_c/wrap_memcpy.c +++ b/ruby/ext/google/protobuf_c/wrap_memcpy.c @@ -33,7 +33,7 @@ // On x86-64 Linux with glibc, we link against the 2.2.5 version of memcpy so // that we avoid depending on the 2.14 version of the symbol. This way, // distributions that are using pre-2.14 versions of glibc can successfully use -// the gem we distribute (https://github.com/google/protobuf/issues/2783). +// the gem we distribute (https://github.com/protocolbuffers/protobuf/issues/2783). // // This wrapper is enabled by passing the linker flags -Wl,-wrap,memcpy in // extconf.rb. diff --git a/ruby/google-protobuf.gemspec b/ruby/google-protobuf.gemspec index 00a7ff3674..b904005255 100644 --- a/ruby/google-protobuf.gemspec +++ b/ruby/google-protobuf.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = "google-protobuf" - s.version = "3.6.1" + s.version = "3.9.0.rc.1" s.licenses = ["BSD-3-Clause"] s.summary = "Protocol Buffers" s.description = "Protocol Buffers are Google's data interchange format." @@ -20,6 +20,7 @@ Gem::Specification.new do |s| s.test_files = ["tests/basic.rb", "tests/stress.rb", "tests/generated_code_test.rb"] + s.required_ruby_version = '>= 2.3' s.add_development_dependency "rake-compiler", "~> 0.9.5" s.add_development_dependency "test-unit", '~> 3.0', '>= 3.0.9' s.add_development_dependency "rubygems-tasks", "~> 0.2.4" diff --git a/ruby/lib/google/protobuf.rb b/ruby/lib/google/protobuf.rb index e20a584e80..464982e303 100644 --- a/ruby/lib/google/protobuf.rb +++ b/ruby/lib/google/protobuf.rb @@ -69,8 +69,8 @@ module Google klass.decode(proto) end - def self.decode_json(klass, json) - klass.decode_json(json) + def self.decode_json(klass, json, options = {}) + klass.decode_json(json, options) end end diff --git a/ruby/lib/google/protobuf/repeated_field.rb b/ruby/lib/google/protobuf/repeated_field.rb index 2dae1e65aa..bc9699fdfd 100644 --- a/ruby/lib/google/protobuf/repeated_field.rb +++ b/ruby/lib/google/protobuf/repeated_field.rb @@ -79,7 +79,7 @@ module Google def first(n=nil) - n ? self[0..n] : self[0] + n ? self[0...n] : self[0] end diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb index 2ee65bc267..37f8d5b675 100644 --- a/ruby/lib/google/protobuf/well_known_types.rb +++ b/ruby/lib/google/protobuf/well_known_types.rb @@ -72,8 +72,14 @@ module Google end Timestamp.class_eval do - def to_time - Time.at(self.to_f) + if RUBY_VERSION < "2.5" + def to_time + Time.at(self.to_f) + end + else + def to_time + Time.at(seconds, nanos, :nanosecond) + end end def from_time(time) diff --git a/ruby/pom.xml b/ruby/pom.xml index adf6ff207d..acd2453ba3 100644 --- a/ruby/pom.xml +++ b/ruby/pom.xml @@ -27,9 +27,9 @@ - https://github.com/google/protobuf + https://github.com/protocolbuffers/protobuf - scm:git:https://github.com/google/protobuf.git + scm:git:https://github.com/protocolbuffers/protobuf.git diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java index 3adaa2a8bd..89738624b8 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java @@ -338,7 +338,7 @@ public class RubyMap extends RubyObject { return newMap; } - @JRubyMethod(name = {"to_h", "to_hash"}) + @JRubyMethod(name = "to_h") public RubyHash toHash(ThreadContext context) { return RubyHash.newHash(context.runtime, table, context.runtime.getNil()); } diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java index c3a0d81c14..774db65a12 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java @@ -368,7 +368,7 @@ public class RubyMessage extends RubyObject { return ret; } - @JRubyMethod(name = {"to_h", "to_hash"}) + @JRubyMethod(name = "to_h") public IRubyObject toHash(ThreadContext context) { Ruby runtime = context.runtime; RubyHash ret = RubyHash.newHash(runtime); diff --git a/ruby/src/main/java/com/google/protobuf/jruby/SentinelOuterClass.java b/ruby/src/main/java/com/google/protobuf/jruby/SentinelOuterClass.java index 54f2c729a2..b3f23c5d35 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/SentinelOuterClass.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/SentinelOuterClass.java @@ -204,7 +204,7 @@ public final class SentinelOuterClass { if (ref instanceof java.lang.String) { return (java.lang.String) ref; } else { - com.google.protobuf.ByteString bs = + com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; java.lang.String s = bs.toStringUtf8(); if (bs.isValidUtf8()) { @@ -220,7 +220,7 @@ public final class SentinelOuterClass { getDefaultStringBytes() { java.lang.Object ref = defaultString_; if (ref instanceof java.lang.String) { - com.google.protobuf.ByteString b = + com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (java.lang.String) ref); defaultString_ = b; @@ -432,7 +432,7 @@ public final class SentinelOuterClass { * optional int32 default_int32 = 1; */ public Builder setDefaultInt32(int value) { - + defaultInt32_ = value; onChanged(); return this; @@ -441,7 +441,7 @@ public final class SentinelOuterClass { * optional int32 default_int32 = 1; */ public Builder clearDefaultInt32() { - + defaultInt32_ = 0; onChanged(); return this; @@ -458,7 +458,7 @@ public final class SentinelOuterClass { * optional int64 default_int64 = 2; */ public Builder setDefaultInt64(long value) { - + defaultInt64_ = value; onChanged(); return this; @@ -467,7 +467,7 @@ public final class SentinelOuterClass { * optional int64 default_int64 = 2; */ public Builder clearDefaultInt64() { - + defaultInt64_ = 0L; onChanged(); return this; @@ -484,7 +484,7 @@ public final class SentinelOuterClass { * optional uint32 default_unit32 = 3; */ public Builder setDefaultUnit32(int value) { - + defaultUnit32_ = value; onChanged(); return this; @@ -493,7 +493,7 @@ public final class SentinelOuterClass { * optional uint32 default_unit32 = 3; */ public Builder clearDefaultUnit32() { - + defaultUnit32_ = 0; onChanged(); return this; @@ -510,7 +510,7 @@ public final class SentinelOuterClass { * optional uint64 default_uint64 = 4; */ public Builder setDefaultUint64(long value) { - + defaultUint64_ = value; onChanged(); return this; @@ -519,7 +519,7 @@ public final class SentinelOuterClass { * optional uint64 default_uint64 = 4; */ public Builder clearDefaultUint64() { - + defaultUint64_ = 0L; onChanged(); return this; @@ -550,7 +550,7 @@ public final class SentinelOuterClass { getDefaultStringBytes() { java.lang.Object ref = defaultString_; if (ref instanceof String) { - com.google.protobuf.ByteString b = + com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (java.lang.String) ref); defaultString_ = b; @@ -567,7 +567,7 @@ public final class SentinelOuterClass { if (value == null) { throw new NullPointerException(); } - + defaultString_ = value; onChanged(); return this; @@ -576,7 +576,7 @@ public final class SentinelOuterClass { * optional string default_string = 5; */ public Builder clearDefaultString() { - + defaultString_ = getDefaultInstance().getDefaultString(); onChanged(); return this; @@ -589,7 +589,7 @@ public final class SentinelOuterClass { if (value == null) { throw new NullPointerException(); } - + defaultString_ = value; onChanged(); return this; @@ -606,7 +606,7 @@ public final class SentinelOuterClass { * optional bool default_bool = 6; */ public Builder setDefaultBool(boolean value) { - + defaultBool_ = value; onChanged(); return this; @@ -615,7 +615,7 @@ public final class SentinelOuterClass { * optional bool default_bool = 6; */ public Builder clearDefaultBool() { - + defaultBool_ = false; onChanged(); return this; @@ -632,7 +632,7 @@ public final class SentinelOuterClass { * optional float default_float = 7; */ public Builder setDefaultFloat(float value) { - + defaultFloat_ = value; onChanged(); return this; @@ -641,7 +641,7 @@ public final class SentinelOuterClass { * optional float default_float = 7; */ public Builder clearDefaultFloat() { - + defaultFloat_ = 0F; onChanged(); return this; @@ -658,7 +658,7 @@ public final class SentinelOuterClass { * optional double default_double = 8; */ public Builder setDefaultDouble(double value) { - + defaultDouble_ = value; onChanged(); return this; @@ -667,7 +667,7 @@ public final class SentinelOuterClass { * optional double default_double = 8; */ public Builder clearDefaultDouble() { - + defaultDouble_ = 0D; onChanged(); return this; @@ -687,7 +687,7 @@ public final class SentinelOuterClass { if (value == null) { throw new NullPointerException(); } - + defaultBytes_ = value; onChanged(); return this; @@ -696,7 +696,7 @@ public final class SentinelOuterClass { * optional bytes default_bytes = 9; */ public Builder clearDefaultBytes() { - + defaultBytes_ = getDefaultInstance().getDefaultBytes(); onChanged(); return this; diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index f174402b07..9ec738ba61 100644 --- a/ruby/tests/basic.rb +++ b/ruby/tests/basic.rb @@ -1,5 +1,10 @@ #!/usr/bin/ruby +# basic_test_pb.rb is in the same directory as this test. +$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) + +require 'basic_test_pb' +require 'common_tests' require 'google/protobuf' require 'json' require 'test/unit' @@ -9,696 +14,152 @@ require 'test/unit' module BasicTest pool = Google::Protobuf::DescriptorPool.new pool.build do - add_message "Foo" do - optional :bar, :message, 1, "Bar" - repeated :baz, :message, 2, "Baz" - end - - add_message "Bar" do - optional :msg, :string, 1 - end - - add_message "Baz" do - optional :msg, :string, 1 - end - - add_message "TestMessage" do - optional :optional_int32, :int32, 1 - optional :optional_int64, :int64, 2 - optional :optional_uint32, :uint32, 3 - optional :optional_uint64, :uint64, 4 - optional :optional_bool, :bool, 5 - optional :optional_float, :float, 6 - optional :optional_double, :double, 7 - optional :optional_string, :string, 8 - optional :optional_bytes, :bytes, 9 - optional :optional_msg, :message, 10, "TestMessage2" - optional :optional_enum, :enum, 11, "TestEnum" - - repeated :repeated_int32, :int32, 12 - repeated :repeated_int64, :int64, 13 - repeated :repeated_uint32, :uint32, 14 - repeated :repeated_uint64, :uint64, 15 - repeated :repeated_bool, :bool, 16 - repeated :repeated_float, :float, 17 - repeated :repeated_double, :double, 18 - repeated :repeated_string, :string, 19 - repeated :repeated_bytes, :bytes, 20 - repeated :repeated_msg, :message, 21, "TestMessage2" - repeated :repeated_enum, :enum, 22, "TestEnum" - end - add_message "TestMessage2" do - optional :foo, :int32, 1 - end - - add_message "TestEmbeddedMessageParent" do - optional :child_msg, :message, 1, "TestEmbeddedMessageChild" - optional :number, :int32, 2 - - repeated :repeated_msg, :message, 3, "TestEmbeddedMessageChild" - repeated :repeated_number, :int32, 4 - end - add_message "TestEmbeddedMessageChild" do - optional :sub_child, :message, 1, "TestMessage" - end - - add_message "Recursive1" do - optional :foo, :message, 1, "Recursive2" - end - add_message "Recursive2" do - optional :foo, :message, 1, "Recursive1" - end - - add_enum "TestEnum" do - value :Default, 0 - value :A, 1 - value :B, 2 - value :C, 3 - end - add_message "BadFieldNames" do optional :dup, :int32, 1 optional :class, :int32, 2 optional :"a.b", :int32, 3 end - - add_message "MapMessage" do - map :map_string_int32, :string, :int32, 1 - map :map_string_msg, :string, :message, 2, "TestMessage2" - end - add_message "MapMessageWireEquiv" do - repeated :map_string_int32, :message, 1, "MapMessageWireEquiv_entry1" - repeated :map_string_msg, :message, 2, "MapMessageWireEquiv_entry2" - end - add_message "MapMessageWireEquiv_entry1" do - optional :key, :string, 1 - optional :value, :int32, 2 - end - add_message "MapMessageWireEquiv_entry2" do - optional :key, :string, 1 - optional :value, :message, 2, "TestMessage2" - end - - add_message "OneofMessage" do - oneof :my_oneof do - optional :a, :string, 1 - optional :b, :int32, 2 - optional :c, :message, 3, "TestMessage2" - optional :d, :enum, 4, "TestEnum" - end - end - - add_message "repro.Outer" do - map :items, :int32, :message, 1, "repro.Inner" - end - - add_message "repro.Inner" do - end end - - Outer = pool.lookup("repro.Outer").msgclass - Inner = pool.lookup("repro.Inner").msgclass - Foo = pool.lookup("Foo").msgclass - Bar = pool.lookup("Bar").msgclass - Baz = pool.lookup("Baz").msgclass - TestMessage = pool.lookup("TestMessage").msgclass - TestMessage2 = pool.lookup("TestMessage2").msgclass - TestEmbeddedMessageParent = pool.lookup("TestEmbeddedMessageParent").msgclass - TestEmbeddedMessageChild = pool.lookup("TestEmbeddedMessageChild").msgclass - Recursive1 = pool.lookup("Recursive1").msgclass - Recursive2 = pool.lookup("Recursive2").msgclass - TestEnum = pool.lookup("TestEnum").enummodule BadFieldNames = pool.lookup("BadFieldNames").msgclass - MapMessage = pool.lookup("MapMessage").msgclass - MapMessageWireEquiv = pool.lookup("MapMessageWireEquiv").msgclass - MapMessageWireEquiv_entry1 = - pool.lookup("MapMessageWireEquiv_entry1").msgclass - MapMessageWireEquiv_entry2 = - pool.lookup("MapMessageWireEquiv_entry2").msgclass - OneofMessage = pool.lookup("OneofMessage").msgclass # ------------ test cases --------------- class MessageContainerTest < Test::Unit::TestCase - - def test_defaults - m = TestMessage.new - assert m.optional_int32 == 0 - assert m.optional_int64 == 0 - assert m.optional_uint32 == 0 - assert m.optional_uint64 == 0 - assert m.optional_bool == false - assert m.optional_float == 0.0 - assert m.optional_double == 0.0 - assert m.optional_string == "" - assert m.optional_bytes == "" - assert m.optional_msg == nil - assert m.optional_enum == :Default - end - - def test_setters - m = TestMessage.new - m.optional_int32 = -42 - assert m.optional_int32 == -42 - m.optional_int64 = -0x1_0000_0000 - assert m.optional_int64 == -0x1_0000_0000 - m.optional_uint32 = 0x9000_0000 - assert m.optional_uint32 == 0x9000_0000 - m.optional_uint64 = 0x9000_0000_0000_0000 - assert m.optional_uint64 == 0x9000_0000_0000_0000 - m.optional_bool = true - assert m.optional_bool == true - m.optional_float = 0.5 - assert m.optional_float == 0.5 - m.optional_double = 0.5 - m.optional_string = "hello" - assert m.optional_string == "hello" - m.optional_string = :hello - assert m.optional_string == "hello" - m.optional_bytes = "world".encode!('ASCII-8BIT') - assert m.optional_bytes == "world" - m.optional_msg = TestMessage2.new(:foo => 42) - assert m.optional_msg == TestMessage2.new(:foo => 42) - m.optional_msg = nil - assert m.optional_msg == nil - m.optional_enum = :C - assert m.optional_enum == :C - m.optional_enum = 'C' - assert m.optional_enum == :C - end - - def test_ctor_args - m = TestMessage.new(:optional_int32 => -42, - :optional_msg => TestMessage2.new, - :optional_enum => :C, - :repeated_string => ["hello", "there", "world"]) - assert m.optional_int32 == -42 - assert m.optional_msg.class == TestMessage2 - assert m.repeated_string.length == 3 - assert m.optional_enum == :C - assert m.repeated_string[0] == "hello" - assert m.repeated_string[1] == "there" - assert m.repeated_string[2] == "world" - end - - def test_ctor_string_symbol_args - m = TestMessage.new(:optional_enum => 'C', :repeated_enum => ['A', 'B']) - assert_equal :C, m.optional_enum - assert_equal [:A, :B], m.repeated_enum - - m = TestMessage.new(:optional_string => :foo, :repeated_string => [:foo, :bar]) - assert_equal 'foo', m.optional_string - assert_equal ['foo', 'bar'], m.repeated_string - end - - def test_ctor_nil_args - m = TestMessage.new(:optional_enum => nil, :optional_int32 => nil, :optional_string => nil, :optional_msg => nil) - - assert_equal :Default, m.optional_enum - assert_equal 0, m.optional_int32 - assert_equal "", m.optional_string - assert_nil m.optional_msg - end - - def test_embeddedmsg_hash_init - m = TestEmbeddedMessageParent.new(:child_msg => {sub_child: {optional_int32: 1}}, - :number => 2, - :repeated_msg => [{sub_child: {optional_int32: 3}}], - :repeated_number => [10, 20, 30]) - - assert_equal 2, m.number - assert_equal [10, 20, 30], m.repeated_number - - assert_not_nil m.child_msg - assert_not_nil m.child_msg.sub_child - assert_equal m.child_msg.sub_child.optional_int32, 1 - - assert_not_nil m.repeated_msg - assert_equal 1, m.repeated_msg.length - assert_equal 3, m.repeated_msg.first.sub_child.optional_int32 - end - - def test_inspect - m = TestMessage.new(:optional_int32 => -42, - :optional_enum => :A, - :optional_msg => TestMessage2.new, - :repeated_string => ["hello", "there", "world"]) - expected = ', optional_enum: :A, repeated_int32: [], repeated_int64: [], repeated_uint32: [], repeated_uint64: [], repeated_bool: [], repeated_float: [], repeated_double: [], repeated_string: ["hello", "there", "world"], repeated_bytes: [], repeated_msg: [], repeated_enum: []>' - assert_equal expected, m.inspect - end - - def test_hash - m1 = TestMessage.new(:optional_int32 => 42) - m2 = TestMessage.new(:optional_int32 => 102, repeated_string: ['please', 'work', 'ok?']) - m3 = TestMessage.new(:optional_int32 => 102, repeated_string: ['please', 'work', 'ok?']) - assert m1.hash != 0 - assert m2.hash != 0 - assert m3.hash != 0 - # relying on the randomness here -- if hash function changes and we are - # unlucky enough to get a collision, then change the values above. - assert m1.hash != m2.hash - assert_equal m2.hash, m3.hash - end - - def test_unknown_field_errors - e = assert_raise NoMethodError do - TestMessage.new.hello - end - assert_match(/hello/, e.message) - - e = assert_raise NoMethodError do - TestMessage.new.hello = "world" - end - assert_match(/hello/, e.message) - end - - def test_initialization_map_errors - e = assert_raise ArgumentError do - TestMessage.new(:hello => "world") - end - assert_match(/hello/, e.message) - - e = assert_raise ArgumentError do - MapMessage.new(:map_string_int32 => "hello") - end - assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'." - - e = assert_raise ArgumentError do - TestMessage.new(:repeated_uint32 => "hello") - end - assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'." + # Required by CommonTests module to resolve proto3 proto classes used in tests. + def proto_module + ::BasicTest end + include CommonTests - def test_type_errors + def test_has_field m = TestMessage.new - e = assert_raise Google::Protobuf::TypeError do - m.optional_int32 = "hello" - end - - # Google::Protobuf::TypeError should inherit from TypeError for backwards compatibility - # TODO: This can be removed when we can safely migrate to Google::Protobuf::TypeError - assert_true e.is_a?(::TypeError) - - assert_raise Google::Protobuf::TypeError do - m.optional_string = 42 - end - assert_raise Google::Protobuf::TypeError do - m.optional_string = nil - end - assert_raise Google::Protobuf::TypeError do - m.optional_bool = 42 - end - assert_raise Google::Protobuf::TypeError do - m.optional_msg = TestMessage.new # expects TestMessage2 + assert !m.has_optional_msg? + m.optional_msg = TestMessage2.new + assert m.has_optional_msg? + assert TestMessage.descriptor.lookup('optional_msg').has?(m) + + m = OneofMessage.new + assert !m.has_my_oneof? + m.a = "foo" + assert m.has_my_oneof? + assert_raise NoMethodError do + m.has_a? end - - assert_raise Google::Protobuf::TypeError do - m.repeated_int32 = [] # needs RepeatedField - end - - assert_raise Google::Protobuf::TypeError do - m.repeated_int32.push "hello" - end - - assert_raise Google::Protobuf::TypeError do - m.repeated_msg.push TestMessage.new + assert_raise ArgumentError do + OneofMessage.descriptor.lookup('a').has?(m) end - end - def test_string_encoding m = TestMessage.new - - # Assigning a normal (ASCII or UTF8) string to a bytes field, or - # ASCII-8BIT to a string field will convert to the proper encoding. - m.optional_bytes = "Test string ASCII".encode!('ASCII') - assert m.optional_bytes.frozen? - assert_equal Encoding::ASCII_8BIT, m.optional_bytes.encoding - assert_equal "Test string ASCII", m.optional_bytes - - assert_raise Encoding::UndefinedConversionError do - m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8') - end - - assert_raise Encoding::UndefinedConversionError do - m.optional_string = ["FFFF"].pack('H*') - end - - # "Ordinary" use case. - m.optional_bytes = ["FFFF"].pack('H*') - m.optional_string = "\u0100" - - # strings are immutable so we can't do this, but serialize should catch it. - m.optional_string = "asdf".encode!('UTF-8') - # Ruby 2.5 changed to raise FrozenError. However, assert_raise don't - # accept subclass. Don't specify type here. - assert_raise do - m.optional_string.encode!('ASCII-8BIT') - end - end - - def test_rptfield_int32 - l = Google::Protobuf::RepeatedField.new(:int32) - assert l.count == 0 - l = Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3]) - assert l.count == 3 - assert_equal [1, 2, 3], l - assert_equal l, [1, 2, 3] - l.push 4 - assert l == [1, 2, 3, 4] - dst_list = [] - l.each { |val| dst_list.push val } - assert dst_list == [1, 2, 3, 4] - assert l.to_a == [1, 2, 3, 4] - assert l[0] == 1 - assert l[3] == 4 - l[0] = 5 - assert l == [5, 2, 3, 4] - - l2 = l.dup - assert l == l2 - assert l.object_id != l2.object_id - l2.push 6 - assert l.count == 4 - assert l2.count == 5 - - assert l.inspect == '[5, 2, 3, 4]' - - l.concat([7, 8, 9]) - assert l == [5, 2, 3, 4, 7, 8, 9] - assert l.pop == 9 - assert l == [5, 2, 3, 4, 7, 8] - - assert_raise Google::Protobuf::TypeError do - m = TestMessage.new - l.push m + assert_raise NoMethodError do + m.has_optional_int32? end - - m = TestMessage.new - m.repeated_int32 = l - assert m.repeated_int32 == [5, 2, 3, 4, 7, 8] - assert m.repeated_int32.object_id == l.object_id - l.push 42 - assert m.repeated_int32.pop == 42 - - l3 = l + l.dup - assert l3.count == l.count * 2 - l.count.times do |i| - assert l3[i] == l[i] - assert l3[l.count + i] == l[i] + assert_raise ArgumentError do + TestMessage.descriptor.lookup('optional_int32').has?(m) end - l.clear - assert l.count == 0 - l += [1, 2, 3, 4] - l.replace([5, 6, 7, 8]) - assert l == [5, 6, 7, 8] - - l4 = Google::Protobuf::RepeatedField.new(:int32) - l4[5] = 42 - assert l4 == [0, 0, 0, 0, 0, 42] - - l4 << 100 - assert l4 == [0, 0, 0, 0, 0, 42, 100] - l4 << 101 << 102 - assert l4 == [0, 0, 0, 0, 0, 42, 100, 101, 102] - end - - def test_parent_rptfield - #make sure we set the RepeatedField and can add to it - m = TestMessage.new - assert m.repeated_string == [] - m.repeated_string << 'ok' - m.repeated_string.push('ok2') - assert m.repeated_string == ['ok', 'ok2'] - m.repeated_string += ['ok3'] - assert m.repeated_string == ['ok', 'ok2', 'ok3'] - end - - def test_rptfield_msg - l = Google::Protobuf::RepeatedField.new(:message, TestMessage) - l.push TestMessage.new - assert l.count == 1 - assert_raise Google::Protobuf::TypeError do - l.push TestMessage2.new + assert_raise NoMethodError do + m.has_optional_string? end - assert_raise Google::Protobuf::TypeError do - l.push 42 + assert_raise ArgumentError do + TestMessage.descriptor.lookup('optional_string').has?(m) end - l2 = l.dup - assert l2[0] == l[0] - assert l2[0].object_id == l[0].object_id - - l2 = Google::Protobuf.deep_copy(l) - assert l2[0] == l[0] - assert l2[0].object_id != l[0].object_id - - l3 = l + l2 - assert l3.count == 2 - assert l3[0] == l[0] - assert l3[1] == l2[0] - l3[0].optional_int32 = 1000 - assert l[0].optional_int32 == 1000 - - new_msg = TestMessage.new(:optional_int32 => 200) - l4 = l + [new_msg] - assert l4.count == 2 - new_msg.optional_int32 = 1000 - assert l4[1].optional_int32 == 1000 - end - - def test_rptfield_enum - l = Google::Protobuf::RepeatedField.new(:enum, TestEnum) - l.push :A - l.push :B - l.push :C - assert l.count == 3 - assert_raise RangeError do - l.push :D - end - assert l[0] == :A - - l.push 4 - assert l[3] == 4 - end - - def test_rptfield_initialize - assert_raise ArgumentError do - l = Google::Protobuf::RepeatedField.new + assert_raise NoMethodError do + m.has_optional_bool? end assert_raise ArgumentError do - l = Google::Protobuf::RepeatedField.new(:message) + TestMessage.descriptor.lookup('optional_bool').has?(m) end - assert_raise ArgumentError do - l = Google::Protobuf::RepeatedField.new([1, 2, 3]) + + assert_raise NoMethodError do + m.has_repeated_msg? end assert_raise ArgumentError do - l = Google::Protobuf::RepeatedField.new(:message, [TestMessage2.new]) + TestMessage.descriptor.lookup('repeated_msg').has?(m) end end - def test_rptfield_array_ducktyping - l = Google::Protobuf::RepeatedField.new(:int32) - length_methods = %w(count length size) - length_methods.each do |lm| - assert l.send(lm) == 0 - end - # out of bounds returns a nil - assert l[0] == nil - assert l[1] == nil - assert l[-1] == nil - l.push 4 - length_methods.each do |lm| - assert l.send(lm) == 1 - end - assert l[0] == 4 - assert l[1] == nil - assert l[-1] == 4 - assert l[-2] == nil - - l.push 2 - length_methods.each do |lm| - assert l.send(lm) == 2 - end - assert l[0] == 4 - assert l[1] == 2 - assert l[2] == nil - assert l[-1] == 2 - assert l[-2] == 4 - assert l[-3] == nil + def test_set_clear_defaults + m = TestMessage.new - #adding out of scope will backfill with empty objects - end + m.optional_int32 = -42 + assert_equal -42, m.optional_int32 + m.clear_optional_int32 + assert_equal 0, m.optional_int32 - def test_map_basic - # allowed key types: - # :int32, :int64, :uint32, :uint64, :bool, :string, :bytes. + m.optional_int32 = 50 + assert_equal 50, m.optional_int32 + TestMessage.descriptor.lookup('optional_int32').clear(m) + assert_equal 0, m.optional_int32 - m = Google::Protobuf::Map.new(:string, :int32) - m["asdf"] = 1 - assert m["asdf"] == 1 - m["jkl;"] = 42 - assert m == { "jkl;" => 42, "asdf" => 1 } - assert m.has_key?("asdf") - assert !m.has_key?("qwerty") - assert m.length == 2 + m.optional_string = "foo bar" + assert_equal "foo bar", m.optional_string + m.clear_optional_string + assert_equal "", m.optional_string - m2 = m.dup - assert_equal m, m2 - assert m.hash != 0 - assert_equal m.hash, m2.hash + m.optional_string = "foo" + assert_equal "foo", m.optional_string + TestMessage.descriptor.lookup('optional_string').clear(m) + assert_equal "", m.optional_string - collected = {} - m.each { |k,v| collected[v] = k } - assert collected == { 42 => "jkl;", 1 => "asdf" } + m.optional_msg = TestMessage2.new(:foo => 42) + assert_equal TestMessage2.new(:foo => 42), m.optional_msg + assert m.has_optional_msg? + m.clear_optional_msg + assert_equal nil, m.optional_msg + assert !m.has_optional_msg? - assert m.delete("asdf") == 1 - assert !m.has_key?("asdf") - assert m["asdf"] == nil - assert !m.has_key?("asdf") + m.optional_msg = TestMessage2.new(:foo => 42) + assert_equal TestMessage2.new(:foo => 42), m.optional_msg + TestMessage.descriptor.lookup('optional_msg').clear(m) + assert_equal nil, m.optional_msg - # We only assert on inspect value when there is one map entry because the - # order in which elements appear is unspecified (depends on the internal - # hash function). We don't want a brittle test. - assert m.inspect == "{\"jkl;\"=>42}" + m.repeated_int32.push(1) + assert_equal [1], m.repeated_int32 + m.clear_repeated_int32 + assert_equal [], m.repeated_int32 - assert m.keys == ["jkl;"] - assert m.values == [42] + m.repeated_int32.push(1) + assert_equal [1], m.repeated_int32 + TestMessage.descriptor.lookup('repeated_int32').clear(m) + assert_equal [], m.repeated_int32 - m.clear - assert m.length == 0 - assert m == {} + m = OneofMessage.new + m.a = "foo" + assert_equal "foo", m.a + assert m.has_my_oneof? + m.clear_a + assert !m.has_my_oneof? - assert_raise TypeError do - m[1] = 1 - end - assert_raise RangeError do - m["asdf"] = 0x1_0000_0000 - end - end + m.a = "foobar" + assert m.has_my_oneof? + m.clear_my_oneof + assert !m.has_my_oneof? - def test_map_ctor - m = Google::Protobuf::Map.new(:string, :int32, - {"a" => 1, "b" => 2, "c" => 3}) - assert m == {"a" => 1, "c" => 3, "b" => 2} + m.a = "bar" + assert_equal "bar", m.a + assert m.has_my_oneof? + OneofMessage.descriptor.lookup('a').clear(m) + assert !m.has_my_oneof? end - def test_map_keytypes - m = Google::Protobuf::Map.new(:int32, :int32) - m[1] = 42 - m[-1] = 42 - assert_raise RangeError do - m[0x8000_0000] = 1 - end - assert_raise Google::Protobuf::TypeError do - m["asdf"] = 1 - end - - m = Google::Protobuf::Map.new(:int64, :int32) - m[0x1000_0000_0000_0000] = 1 - assert_raise RangeError do - m[0x1_0000_0000_0000_0000] = 1 - end - assert_raise Google::Protobuf::TypeError do - m["asdf"] = 1 - end - - m = Google::Protobuf::Map.new(:uint32, :int32) - m[0x8000_0000] = 1 - assert_raise RangeError do - m[0x1_0000_0000] = 1 - end - assert_raise RangeError do - m[-1] = 1 - end - - m = Google::Protobuf::Map.new(:uint64, :int32) - m[0x8000_0000_0000_0000] = 1 - assert_raise RangeError do - m[0x1_0000_0000_0000_0000] = 1 - end - assert_raise RangeError do - m[-1] = 1 - end - - m = Google::Protobuf::Map.new(:bool, :int32) - m[true] = 1 - m[false] = 2 - assert_raise Google::Protobuf::TypeError do - m[1] = 1 - end - assert_raise Google::Protobuf::TypeError do - m["asdf"] = 1 - end - - m = Google::Protobuf::Map.new(:string, :int32) - m["asdf"] = 1 - assert_raise TypeError do - m[1] = 1 - end - assert_raise Encoding::UndefinedConversionError do - bytestring = ["FFFF"].pack("H*") - m[bytestring] = 1 - end - m = Google::Protobuf::Map.new(:bytes, :int32) - bytestring = ["FFFF"].pack("H*") - m[bytestring] = 1 - # Allowed -- we will automatically convert to ASCII-8BIT. - m["asdf"] = 1 - assert_raise TypeError do - m[1] = 1 + def test_initialization_map_errors + e = assert_raise ArgumentError do + TestMessage.new(:hello => "world") end - end + assert_match(/hello/, e.message) - def test_map_msg_enum_valuetypes - m = Google::Protobuf::Map.new(:string, :message, TestMessage) - m["asdf"] = TestMessage.new - assert_raise Google::Protobuf::TypeError do - m["jkl;"] = TestMessage2.new + e = assert_raise ArgumentError do + MapMessage.new(:map_string_int32 => "hello") end + assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)." - m = Google::Protobuf::Map.new( - :string, :message, TestMessage, - { "a" => TestMessage.new(:optional_int32 => 42), - "b" => TestMessage.new(:optional_int32 => 84) }) - assert m.length == 2 - assert m.values.map{|msg| msg.optional_int32}.sort == [42, 84] - - m = Google::Protobuf::Map.new(:string, :enum, TestEnum, - { "x" => :A, "y" => :B, "z" => :C }) - assert m.length == 3 - assert m["z"] == :C - m["z"] = 2 - assert m["z"] == :B - m["z"] = 4 - assert m["z"] == 4 - assert_raise RangeError do - m["z"] = :Z - end - assert_raise RangeError do - m["z"] = "z" + e = assert_raise ArgumentError do + TestMessage.new(:repeated_uint32 => "hello") end - end - - def test_map_dup_deep_copy - m = Google::Protobuf::Map.new( - :string, :message, TestMessage, - { "a" => TestMessage.new(:optional_int32 => 42), - "b" => TestMessage.new(:optional_int32 => 84) }) - - m2 = m.dup - assert m == m2 - assert m.object_id != m2.object_id - assert m["a"].object_id == m2["a"].object_id - assert m["b"].object_id == m2["b"].object_id - - m2 = Google::Protobuf.deep_copy(m) - assert m == m2 - assert m.object_id != m2.object_id - assert m["a"].object_id != m2["a"].object_id - assert m["b"].object_id != m2["b"].object_id + assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)." end def test_map_field @@ -709,10 +170,12 @@ module BasicTest m = MapMessage.new( :map_string_int32 => {"a" => 1, "b" => 2}, :map_string_msg => {"a" => TestMessage2.new(:foo => 1), - "b" => TestMessage2.new(:foo => 2)}) + "b" => TestMessage2.new(:foo => 2)}, + :map_string_enum => {"a" => :A, "b" => :B}) assert m.map_string_int32.keys.sort == ["a", "b"] assert m.map_string_int32["a"] == 1 assert m.map_string_msg["b"].foo == 2 + assert m.map_string_enum["a"] == :A m.map_string_int32["c"] = 3 assert m.map_string_int32["c"] == 3 @@ -741,6 +204,16 @@ module BasicTest end end + def test_map_inspect + m = MapMessage.new( + :map_string_int32 => {"a" => 1, "b" => 2}, + :map_string_msg => {"a" => TestMessage2.new(:foo => 1), + "b" => TestMessage2.new(:foo => 2)}, + :map_string_enum => {"a" => :A, "b" => :B}) + expected = "2, \"a\"=>1}, map_string_msg: {\"b\"=>, \"a\"=>}, map_string_enum: {\"b\"=>:B, \"a\"=>:A}>" + assert_equal expected, m.inspect + end + def test_map_corruption # This pattern led to a crash in a previous version of upb/protobuf. m = MapMessage.new(map_string_int32: { "aaa" => 1 }) @@ -767,7 +240,8 @@ module BasicTest m = MapMessage.new( :map_string_int32 => {"a" => 1, "b" => 2}, :map_string_msg => {"a" => TestMessage2.new(:foo => 1), - "b" => TestMessage2.new(:foo => 2)}) + "b" => TestMessage2.new(:foo => 2)}, + :map_string_enum => {"a" => :A, "b" => :B}) m2 = MapMessage.decode(MapMessage.encode(m)) assert m == m2 @@ -784,209 +258,17 @@ module BasicTest "b" => TestMessage2.new(:foo => 2)} end - def test_oneof_descriptors - d = OneofMessage.descriptor - o = d.lookup_oneof("my_oneof") - assert o != nil - assert o.class == Google::Protobuf::OneofDescriptor - assert o.name == "my_oneof" - oneof_count = 0 - d.each_oneof{ |oneof| - oneof_count += 1 - assert oneof == o - } - assert oneof_count == 1 - assert o.count == 4 - field_names = o.map{|f| f.name}.sort - assert field_names == ["a", "b", "c", "d"] - end - - def test_oneof - d = OneofMessage.new - assert d.a == "" - assert d.b == 0 - assert d.c == nil - assert d.d == :Default - assert d.my_oneof == nil - - d.a = "hi" - assert d.a == "hi" - assert d.b == 0 - assert d.c == nil - assert d.d == :Default - assert d.my_oneof == :a - - d.b = 42 - assert d.a == "" - assert d.b == 42 - assert d.c == nil - assert d.d == :Default - assert d.my_oneof == :b - - d.c = TestMessage2.new(:foo => 100) - assert d.a == "" - assert d.b == 0 - assert d.c.foo == 100 - assert d.d == :Default - assert d.my_oneof == :c - - d.d = :C - assert d.a == "" - assert d.b == 0 - assert d.c == nil - assert d.d == :C - assert d.my_oneof == :d - - d2 = OneofMessage.decode(OneofMessage.encode(d)) - assert d2 == d - - encoded_field_a = OneofMessage.encode(OneofMessage.new(:a => "string")) - encoded_field_b = OneofMessage.encode(OneofMessage.new(:b => 1000)) - encoded_field_c = OneofMessage.encode( - OneofMessage.new(:c => TestMessage2.new(:foo => 1))) - encoded_field_d = OneofMessage.encode(OneofMessage.new(:d => :B)) - - d3 = OneofMessage.decode( - encoded_field_c + encoded_field_a + encoded_field_d) - assert d3.a == "" - assert d3.b == 0 - assert d3.c == nil - assert d3.d == :B + def test_protobuf_decode_json_ignore_unknown_fields + m = TestMessage.decode_json({ + optional_string: "foo", + not_in_message: "some_value" + }.to_json, { ignore_unknown_fields: true }) - d4 = OneofMessage.decode( - encoded_field_c + encoded_field_a + encoded_field_d + - encoded_field_c) - assert d4.a == "" - assert d4.b == 0 - assert d4.c.foo == 1 - assert d4.d == :Default - - d5 = OneofMessage.new(:a => "hello") - assert d5.a == "hello" - d5.a = nil - assert d5.a == "" - assert OneofMessage.encode(d5) == '' - assert d5.my_oneof == nil - end - - def test_enum_field - m = TestMessage.new - assert m.optional_enum == :Default - m.optional_enum = :A - assert m.optional_enum == :A - assert_raise RangeError do - m.optional_enum = :ASDF + assert_equal m.optional_string, "foo" + e = assert_raise Google::Protobuf::ParseError do + TestMessage.decode_json({ not_in_message: "some_value" }.to_json) end - m.optional_enum = 1 - assert m.optional_enum == :A - m.optional_enum = 100 - assert m.optional_enum == 100 - end - - def test_dup - m = TestMessage.new - m.optional_string = "hello" - m.optional_int32 = 42 - tm1 = TestMessage2.new(:foo => 100) - tm2 = TestMessage2.new(:foo => 200) - m.repeated_msg.push tm1 - assert m.repeated_msg[-1] == tm1 - m.repeated_msg.push tm2 - assert m.repeated_msg[-1] == tm2 - m2 = m.dup - assert m == m2 - m.optional_int32 += 1 - assert m != m2 - assert m.repeated_msg[0] == m2.repeated_msg[0] - assert m.repeated_msg[0].object_id == m2.repeated_msg[0].object_id - end - - def test_deep_copy - m = TestMessage.new(:optional_int32 => 42, - :repeated_msg => [TestMessage2.new(:foo => 100)]) - m2 = Google::Protobuf.deep_copy(m) - assert m == m2 - assert m.repeated_msg == m2.repeated_msg - assert m.repeated_msg.object_id != m2.repeated_msg.object_id - assert m.repeated_msg[0].object_id != m2.repeated_msg[0].object_id - end - - def test_eq - m = TestMessage.new(:optional_int32 => 42, - :repeated_int32 => [1, 2, 3]) - m2 = TestMessage.new(:optional_int32 => 43, - :repeated_int32 => [1, 2, 3]) - assert m != m2 - end - - def test_enum_lookup - assert TestEnum::A == 1 - assert TestEnum::B == 2 - assert TestEnum::C == 3 - - assert TestEnum::lookup(1) == :A - assert TestEnum::lookup(2) == :B - assert TestEnum::lookup(3) == :C - - assert TestEnum::resolve(:A) == 1 - assert TestEnum::resolve(:B) == 2 - assert TestEnum::resolve(:C) == 3 - end - - def test_parse_serialize - m = TestMessage.new(:optional_int32 => 42, - :optional_string => "hello world", - :optional_enum => :B, - :repeated_string => ["a", "b", "c"], - :repeated_int32 => [42, 43, 44], - :repeated_enum => [:A, :B, :C, 100], - :repeated_msg => [TestMessage2.new(:foo => 1), - TestMessage2.new(:foo => 2)]) - data = TestMessage.encode m - m2 = TestMessage.decode data - assert m == m2 - - data = Google::Protobuf.encode m - m2 = Google::Protobuf.decode(TestMessage, data) - assert m == m2 - end - - def test_encode_decode_helpers - m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) - assert_equal 'foo', m.optional_string - assert_equal ['bar1', 'bar2'], m.repeated_string - - json = m.to_json - m2 = TestMessage.decode_json(json) - assert_equal 'foo', m2.optional_string - assert_equal ['bar1', 'bar2'], m2.repeated_string - if RUBY_PLATFORM != "java" - assert m2.optional_string.frozen? - assert m2.repeated_string[0].frozen? - end - - proto = m.to_proto - m2 = TestMessage.decode(proto) - assert_equal 'foo', m2.optional_string - assert_equal ['bar1', 'bar2'], m2.repeated_string - end - - def test_protobuf_encode_decode_helpers - m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) - encoded_msg = Google::Protobuf.encode(m) - assert_equal m.to_proto, encoded_msg - - decoded_msg = Google::Protobuf.decode(TestMessage, encoded_msg) - assert_equal TestMessage.decode(m.to_proto), decoded_msg - end - - def test_protobuf_encode_decode_json_helpers - m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) - encoded_msg = Google::Protobuf.encode_json(m) - assert_equal m.to_json, encoded_msg - - decoded_msg = Google::Protobuf.decode_json(TestMessage, encoded_msg) - assert_equal TestMessage.decode_json(m.to_json), decoded_msg + assert_match(/No such field: not_in_message/, e.message) end def test_to_h @@ -1020,396 +302,41 @@ module BasicTest m = MapMessage.new( :map_string_int32 => {"a" => 1, "b" => 2}, :map_string_msg => {"a" => TestMessage2.new(:foo => 1), - "b" => TestMessage2.new(:foo => 2)}) + "b" => TestMessage2.new(:foo => 2)}, + :map_string_enum => {"a" => :A, "b" => :B}) expected_result = { :map_string_int32 => {"a" => 1, "b" => 2}, - :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}} + :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}, + :map_string_enum => {"a" => :A, "b" => :B} } assert_equal expected_result, m.to_h end - def test_def_errors - s = Google::Protobuf::DescriptorPool.new - assert_raise Google::Protobuf::TypeError do - s.build do - # enum with no default (integer value 0) - add_enum "MyEnum" do - value :A, 1 - end - end - end - assert_raise Google::Protobuf::TypeError do - s.build do - # message with required field (unsupported in proto3) - add_message "MyMessage" do - required :foo, :int32, 1 - end - end - end - end - - def test_corecursive - # just be sure that we can instantiate types with corecursive field-type - # references. - m = Recursive1.new(:foo => Recursive2.new(:foo => Recursive1.new)) - assert Recursive1.descriptor.lookup("foo").subtype == - Recursive2.descriptor - assert Recursive2.descriptor.lookup("foo").subtype == - Recursive1.descriptor - - serialized = Recursive1.encode(m) - m2 = Recursive1.decode(serialized) - assert m == m2 - end - - def test_serialize_cycle - m = Recursive1.new(:foo => Recursive2.new) - m.foo.foo = m - assert_raise RuntimeError do - serialized = Recursive1.encode(m) - end - end - - def test_bad_field_names - m = BadFieldNames.new(:dup => 1, :class => 2) - m2 = m.dup - assert m == m2 - assert m['dup'] == 1 - assert m['class'] == 2 - m['dup'] = 3 - assert m['dup'] == 3 - m['a.b'] = 4 - assert m['a.b'] == 4 - end - - def test_int_ranges - m = TestMessage.new - - m.optional_int32 = 0 - m.optional_int32 = -0x8000_0000 - m.optional_int32 = +0x7fff_ffff - m.optional_int32 = 1.0 - m.optional_int32 = -1.0 - m.optional_int32 = 2e9 - assert_raise RangeError do - m.optional_int32 = -0x8000_0001 - end - assert_raise RangeError do - m.optional_int32 = +0x8000_0000 - end - assert_raise RangeError do - m.optional_int32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum - end - assert_raise RangeError do - m.optional_int32 = 1e12 - end - assert_raise RangeError do - m.optional_int32 = 1.5 - end - - m.optional_uint32 = 0 - m.optional_uint32 = +0xffff_ffff - m.optional_uint32 = 1.0 - m.optional_uint32 = 4e9 - assert_raise RangeError do - m.optional_uint32 = -1 - end - assert_raise RangeError do - m.optional_uint32 = -1.5 - end - assert_raise RangeError do - m.optional_uint32 = -1.5e12 - end - assert_raise RangeError do - m.optional_uint32 = -0x1000_0000_0000_0000 - end - assert_raise RangeError do - m.optional_uint32 = +0x1_0000_0000 - end - assert_raise RangeError do - m.optional_uint32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum - end - assert_raise RangeError do - m.optional_uint32 = 1e12 - end - assert_raise RangeError do - m.optional_uint32 = 1.5 - end - - m.optional_int64 = 0 - m.optional_int64 = -0x8000_0000_0000_0000 - m.optional_int64 = +0x7fff_ffff_ffff_ffff - m.optional_int64 = 1.0 - m.optional_int64 = -1.0 - m.optional_int64 = 8e18 - m.optional_int64 = -8e18 - assert_raise RangeError do - m.optional_int64 = -0x8000_0000_0000_0001 - end - assert_raise RangeError do - m.optional_int64 = +0x8000_0000_0000_0000 - end - assert_raise RangeError do - m.optional_int64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum - end - assert_raise RangeError do - m.optional_int64 = 1e50 - end - assert_raise RangeError do - m.optional_int64 = 1.5 - end - - m.optional_uint64 = 0 - m.optional_uint64 = +0xffff_ffff_ffff_ffff - m.optional_uint64 = 1.0 - m.optional_uint64 = 16e18 - assert_raise RangeError do - m.optional_uint64 = -1 - end - assert_raise RangeError do - m.optional_uint64 = -1.5 - end - assert_raise RangeError do - m.optional_uint64 = -1.5e12 - end - assert_raise RangeError do - m.optional_uint64 = -0x1_0000_0000_0000_0000 - end - assert_raise RangeError do - m.optional_uint64 = +0x1_0000_0000_0000_0000 - end - assert_raise RangeError do - m.optional_uint64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum - end - assert_raise RangeError do - m.optional_uint64 = 1e50 - end - assert_raise RangeError do - m.optional_uint64 = 1.5 - end - end - - def test_stress_test - m = TestMessage.new - m.optional_int32 = 42 - m.optional_int64 = 0x100000000 - m.optional_string = "hello world" - 10.times do m.repeated_msg.push TestMessage2.new(:foo => 42) end - 10.times do m.repeated_string.push "hello world" end - - data = TestMessage.encode(m) - - l = 0 - 10_000.times do - m = TestMessage.decode(data) - data_new = TestMessage.encode(m) - assert data_new == data - data = data_new - end - end - - def test_reflection - m = TestMessage.new(:optional_int32 => 1234) - msgdef = m.class.descriptor - assert msgdef.class == Google::Protobuf::Descriptor - assert msgdef.any? {|field| field.name == "optional_int32"} - optional_int32 = msgdef.lookup "optional_int32" - assert optional_int32.class == Google::Protobuf::FieldDescriptor - assert optional_int32 != nil - assert optional_int32.name == "optional_int32" - assert optional_int32.type == :int32 - optional_int32.set(m, 5678) - assert m.optional_int32 == 5678 - m.optional_int32 = 1000 - assert optional_int32.get(m) == 1000 - - optional_msg = msgdef.lookup "optional_msg" - assert optional_msg.subtype == TestMessage2.descriptor - - optional_msg.set(m, optional_msg.subtype.msgclass.new) - - assert msgdef.msgclass == TestMessage - - optional_enum = msgdef.lookup "optional_enum" - assert optional_enum.subtype == TestEnum.descriptor - assert optional_enum.subtype.class == Google::Protobuf::EnumDescriptor - optional_enum.subtype.each do |k, v| - # set with integer, check resolution to symbolic name - optional_enum.set(m, v) - assert optional_enum.get(m) == k - end - end - - def test_json - # TODO: Fix JSON in JRuby version. - return if RUBY_PLATFORM == "java" - m = TestMessage.new(:optional_int32 => 1234, - :optional_int64 => -0x1_0000_0000, - :optional_uint32 => 0x8000_0000, - :optional_uint64 => 0xffff_ffff_ffff_ffff, - :optional_bool => true, - :optional_float => 1.0, - :optional_double => -1e100, - :optional_string => "Test string", - :optional_bytes => ["FFFFFFFF"].pack('H*'), - :optional_msg => TestMessage2.new(:foo => 42), - :repeated_int32 => [1, 2, 3, 4], - :repeated_string => ["a", "b", "c"], - :repeated_bool => [true, false, true, false], - :repeated_msg => [TestMessage2.new(:foo => 1), - TestMessage2.new(:foo => 2)]) - - json_text = TestMessage.encode_json(m) - m2 = TestMessage.decode_json(json_text) - puts m.inspect - puts m2.inspect - assert m == m2 - - # Crash case from GitHub issue 283. - bar = Bar.new(msg: "bar") - baz1 = Baz.new(msg: "baz") - baz2 = Baz.new(msg: "quux") - Foo.encode_json(Foo.new) - Foo.encode_json(Foo.new(bar: bar)) - Foo.encode_json(Foo.new(bar: bar, baz: [baz1, baz2])) - end - - def test_json_empty - assert TestMessage.encode_json(TestMessage.new) == '{}' - end - - def test_json_emit_defaults - # TODO: Fix JSON in JRuby version. - return if RUBY_PLATFORM == "java" - m = TestMessage.new - - expected = { - optionalInt32: 0, - optionalInt64: 0, - optionalUint32: 0, - optionalUint64: 0, - optionalBool: false, - optionalFloat: 0, - optionalDouble: 0, - optionalString: "", - optionalBytes: "", - optionalEnum: "Default", - repeatedInt32: [], - repeatedInt64: [], - repeatedUint32: [], - repeatedUint64: [], - repeatedBool: [], - repeatedFloat: [], - repeatedDouble: [], - repeatedString: [], - repeatedBytes: [], - repeatedMsg: [], - repeatedEnum: [] - } - - actual = TestMessage.encode_json(m, :emit_defaults => true) - - assert JSON.parse(actual, :symbolize_names => true) == expected - end - - def test_json_emit_defaults_submsg - # TODO: Fix JSON in JRuby version. - return if RUBY_PLATFORM == "java" - m = TestMessage.new(optional_msg: TestMessage2.new) - - expected = { - optionalInt32: 0, - optionalInt64: 0, - optionalUint32: 0, - optionalUint64: 0, - optionalBool: false, - optionalFloat: 0, - optionalDouble: 0, - optionalString: "", - optionalBytes: "", - optionalMsg: {foo: 0}, - optionalEnum: "Default", - repeatedInt32: [], - repeatedInt64: [], - repeatedUint32: [], - repeatedUint64: [], - repeatedBool: [], - repeatedFloat: [], - repeatedDouble: [], - repeatedString: [], - repeatedBytes: [], - repeatedMsg: [], - repeatedEnum: [] - } - - actual = TestMessage.encode_json(m, :emit_defaults => true) - - assert JSON.parse(actual, :symbolize_names => true) == expected - end - - def test_json_emit_defaults_repeated_submsg - # TODO: Fix JSON in JRuby version. - return if RUBY_PLATFORM == "java" - m = TestMessage.new(repeated_msg: [TestMessage2.new]) - - expected = { - optionalInt32: 0, - optionalInt64: 0, - optionalUint32: 0, - optionalUint64: 0, - optionalBool: false, - optionalFloat: 0, - optionalDouble: 0, - optionalString: "", - optionalBytes: "", - optionalEnum: "Default", - repeatedInt32: [], - repeatedInt64: [], - repeatedUint32: [], - repeatedUint64: [], - repeatedBool: [], - repeatedFloat: [], - repeatedDouble: [], - repeatedString: [], - repeatedBytes: [], - repeatedMsg: [{foo: 0}], - repeatedEnum: [] - } - - actual = TestMessage.encode_json(m, :emit_defaults => true) - - assert JSON.parse(actual, :symbolize_names => true) == expected - end - def test_json_maps # TODO: Fix JSON in JRuby version. return if RUBY_PLATFORM == "java" m = MapMessage.new(:map_string_int32 => {"a" => 1}) - expected = {mapStringInt32: {a: 1}, mapStringMsg: {}} - expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}} - assert JSON.parse(MapMessage.encode_json(m), :symbolize_names => true) == expected + expected = {mapStringInt32: {a: 1}, mapStringMsg: {}, mapStringEnum: {}} + expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}, map_string_enum: {}} + assert_equal JSON.parse(MapMessage.encode_json(m), :symbolize_names => true), expected json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true) - assert JSON.parse(json, :symbolize_names => true) == expected_preserve + assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve m2 = MapMessage.decode_json(MapMessage.encode_json(m)) - assert m == m2 + assert_equal m, m2 end def test_json_maps_emit_defaults_submsg # TODO: Fix JSON in JRuby version. return if RUBY_PLATFORM == "java" m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new}) - expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}} + expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}} actual = MapMessage.encode_json(m, :emit_defaults => true) - assert JSON.parse(actual, :symbolize_names => true) == expected - end - - def test_comparison_with_arbitrary_object - assert MapMessage.new != nil + assert_equal JSON.parse(actual, :symbolize_names => true), expected end def test_respond_to @@ -1419,5 +346,42 @@ module BasicTest assert msg.respond_to?(:map_string_int32) assert !msg.respond_to?(:bacon) end + + def test_file_descriptor + file_descriptor = TestMessage.descriptor.file_descriptor + assert nil != file_descriptor + assert_equal "tests/basic_test.proto", file_descriptor.name + assert_equal :proto3, file_descriptor.syntax + + file_descriptor = TestEnum.descriptor.file_descriptor + assert nil != file_descriptor + assert_equal "tests/basic_test.proto", file_descriptor.name + assert_equal :proto3, file_descriptor.syntax + + file_descriptor = BadFieldNames.descriptor.file_descriptor + assert nil != file_descriptor + assert_equal nil, file_descriptor.name + assert_equal :proto3, file_descriptor.syntax + end + + # Ruby 2.5 changed to raise FrozenError instead of RuntimeError + FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError + + def test_map_freeze + m = proto_module::MapMessage.new + m.map_string_int32['a'] = 5 + m.map_string_msg['b'] = proto_module::TestMessage2.new + + m.map_string_int32.freeze + m.map_string_msg.freeze + + assert m.map_string_int32.frozen? + assert m.map_string_msg.frozen? + + assert_raise(FrozenErrorType) { m.map_string_int32['foo'] = 1 } + assert_raise(FrozenErrorType) { m.map_string_msg['bar'] = proto_module::TestMessage2.new } + assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') } + assert_raise(FrozenErrorType) { m.map_string_int32.clear } + end end end diff --git a/ruby/tests/basic_proto2.rb b/ruby/tests/basic_proto2.rb new file mode 100644 index 0000000000..53d6a70d2d --- /dev/null +++ b/ruby/tests/basic_proto2.rb @@ -0,0 +1,265 @@ +#!/usr/bin/ruby + +# basic_test_pb.rb is in the same directory as this test. +$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) + +require 'basic_test_proto2_pb' +require 'common_tests' +require 'google/protobuf' +require 'json' +require 'test/unit' + +# ------------- generated code -------------- + +module BasicTestProto2 + pool = Google::Protobuf::DescriptorPool.new + pool.build do + add_file "test_proto2.proto", syntax: :proto2 do + add_message "BadFieldNames" do + optional :dup, :int32, 1 + optional :class, :int32, 2 + optional :"a.b", :int32, 3 + end + end + end + + BadFieldNames = pool.lookup("BadFieldNames").msgclass + +# ------------ test cases --------------- + + class MessageContainerTest < Test::Unit::TestCase + # Required by CommonTests module to resolve proto2 proto classes used in tests. + def proto_module + ::BasicTestProto2 + end + include CommonTests + + def test_has_field + m = TestMessage.new + assert !m.has_optional_int32? + assert !TestMessage.descriptor.lookup('optional_int32').has?(m) + assert !m.has_optional_int64? + assert !TestMessage.descriptor.lookup('optional_int64').has?(m) + assert !m.has_optional_uint32? + assert !TestMessage.descriptor.lookup('optional_uint32').has?(m) + assert !m.has_optional_uint64? + assert !TestMessage.descriptor.lookup('optional_uint64').has?(m) + assert !m.has_optional_bool? + assert !TestMessage.descriptor.lookup('optional_bool').has?(m) + assert !m.has_optional_float? + assert !TestMessage.descriptor.lookup('optional_float').has?(m) + assert !m.has_optional_double? + assert !TestMessage.descriptor.lookup('optional_double').has?(m) + assert !m.has_optional_string? + assert !TestMessage.descriptor.lookup('optional_string').has?(m) + assert !m.has_optional_bytes? + assert !TestMessage.descriptor.lookup('optional_bytes').has?(m) + assert !m.has_optional_enum? + assert !TestMessage.descriptor.lookup('optional_enum').has?(m) + + m = TestMessage.new(:optional_int32 => nil) + assert !m.has_optional_int32? + + assert_raise NoMethodError do + m.has_repeated_msg? + end + assert_raise ArgumentError do + TestMessage.descriptor.lookup('repeated_msg').has?(m) + end + + m.optional_msg = TestMessage2.new + assert m.has_optional_msg? + assert TestMessage.descriptor.lookup('optional_msg').has?(m) + + m = OneofMessage.new + assert !m.has_my_oneof? + m.a = "foo" + assert m.has_a? + assert OneofMessage.descriptor.lookup('a').has?(m) + assert_equal "foo", m.a + assert m.has_my_oneof? + assert !m.has_b? + assert !OneofMessage.descriptor.lookup('b').has?(m) + assert !m.has_c? + assert !OneofMessage.descriptor.lookup('c').has?(m) + assert !m.has_d? + assert !OneofMessage.descriptor.lookup('d').has?(m) + + m = OneofMessage.new + m.b = 100 + assert m.has_b? + assert_equal 100, m.b + assert m.has_my_oneof? + assert !m.has_a? + assert !m.has_c? + assert !m.has_d? + + m = OneofMessage.new + m.c = TestMessage2.new + assert m.has_c? + assert_equal TestMessage2.new, m.c + assert m.has_my_oneof? + assert !m.has_a? + assert !m.has_b? + assert !m.has_d? + + m = OneofMessage.new + m.d = :A + assert m.has_d? + assert_equal :A, m.d + assert m.has_my_oneof? + assert !m.has_a? + assert !m.has_b? + assert !m.has_c? + end + + def test_defined_defaults + m = TestMessageDefaults.new + assert_equal 1, m.optional_int32 + assert_equal 2, m.optional_int64 + assert_equal 3, m.optional_uint32 + assert_equal 4, m.optional_uint64 + assert_equal true, m.optional_bool + assert_equal 6.0, m.optional_float + assert_equal 7.0, m.optional_double + assert_equal "Default Str", m.optional_string + assert_equal "\xCF\xA5s\xBD\xBA\xE6fubar".force_encoding("ASCII-8BIT"), m.optional_bytes + assert_equal :B2, m.optional_enum + + assert !m.has_optional_int32? + assert !m.has_optional_int64? + assert !m.has_optional_uint32? + assert !m.has_optional_uint64? + assert !m.has_optional_bool? + assert !m.has_optional_float? + assert !m.has_optional_double? + assert !m.has_optional_string? + assert !m.has_optional_bytes? + assert !m.has_optional_enum? + end + + def test_set_clear_defaults + m = TestMessageDefaults.new + + m.optional_int32 = -42 + assert_equal -42, m.optional_int32 + assert m.has_optional_int32? + m.clear_optional_int32 + assert_equal 1, m.optional_int32 + assert !m.has_optional_int32? + + m.optional_string = "foo bar" + assert_equal "foo bar", m.optional_string + assert m.has_optional_string? + m.clear_optional_string + assert_equal "Default Str", m.optional_string + assert !m.has_optional_string? + + m.optional_msg = TestMessage2.new(:foo => 42) + assert_equal TestMessage2.new(:foo => 42), m.optional_msg + assert m.has_optional_msg? + + m.clear_optional_msg + assert_equal nil, m.optional_msg + assert !m.has_optional_msg? + + m.optional_msg = TestMessage2.new(:foo => 42) + assert_equal TestMessage2.new(:foo => 42), m.optional_msg + assert TestMessageDefaults.descriptor.lookup('optional_msg').has?(m) + + TestMessageDefaults.descriptor.lookup('optional_msg').clear(m) + assert_equal nil, m.optional_msg + assert !TestMessageDefaults.descriptor.lookup('optional_msg').has?(m) + + m = TestMessage.new + m.repeated_int32.push(1) + assert_equal [1], m.repeated_int32 + m.clear_repeated_int32 + assert_equal [], m.repeated_int32 + + m = OneofMessage.new + m.a = "foo" + assert_equal "foo", m.a + assert m.has_a? + m.clear_a + assert !m.has_a? + + m = OneofMessage.new + m.a = "foobar" + assert m.has_my_oneof? + m.clear_my_oneof + assert !m.has_my_oneof? + + m = OneofMessage.new + m.a = "bar" + assert_equal "bar", m.a + assert m.has_my_oneof? + OneofMessage.descriptor.lookup('a').clear(m) + assert !m.has_my_oneof? + end + + def test_initialization_map_errors + e = assert_raise ArgumentError do + TestMessage.new(:hello => "world") + end + assert_match(/hello/, e.message) + + e = assert_raise ArgumentError do + TestMessage.new(:repeated_uint32 => "hello") + end + assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)." + end + + + def test_to_h + m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + expected_result = { + :optional_bool=>true, + :optional_double=>-10.100001, + :optional_string=>"foo", + :repeated_string=>["bar1", "bar2"], + } + assert_equal expected_result, m.to_h + + m = OneofMessage.new(:a => "foo") + expected_result = {:a => "foo"} + assert_equal expected_result, m.to_h + end + + def test_map_keyword_disabled + pool = Google::Protobuf::DescriptorPool.new + + e = assert_raise ArgumentError do + pool.build do + add_file 'test_file.proto', syntax: :proto2 do + add_message "MapMessage" do + map :map_string_int32, :string, :int32, 1 + map :map_string_msg, :string, :message, 2, "TestMessage2" + end + end + end + end + + assert_match(/Cannot add a native map/, e.message) + end + + def test_respond_to + # This test fails with JRuby 1.7.23, likely because of an old JRuby bug. + return if RUBY_PLATFORM == "java" + msg = TestMessage.new + assert !msg.respond_to?(:bacon) + end + + def test_file_descriptor + file_descriptor = TestMessage.descriptor.file_descriptor + assert nil != file_descriptor + assert_equal "tests/basic_test_proto2.proto", file_descriptor.name + assert_equal :proto2, file_descriptor.syntax + + file_descriptor = TestEnum.descriptor.file_descriptor + assert nil != file_descriptor + assert_equal "tests/basic_test_proto2.proto", file_descriptor.name + assert_equal :proto2, file_descriptor.syntax + end + end +end diff --git a/ruby/tests/basic_test.proto b/ruby/tests/basic_test.proto new file mode 100644 index 0000000000..e5811dc881 --- /dev/null +++ b/ruby/tests/basic_test.proto @@ -0,0 +1,155 @@ +syntax = "proto3"; + +package basic_test; + +import "google/protobuf/wrappers.proto"; +import "google/protobuf/timestamp.proto"; +import "google/protobuf/duration.proto"; +import "google/protobuf/struct.proto"; + +message Foo { + Bar bar = 1; + repeated Baz baz = 2; +} + +message Bar { + string msg = 1; +} + +message Baz { + string msg = 1; +} + +message TestMessage { + int32 optional_int32 = 1; + int64 optional_int64 = 2; + uint32 optional_uint32 = 3; + uint64 optional_uint64 = 4; + bool optional_bool = 5; + float optional_float = 6; + double optional_double = 7; + string optional_string = 8; + bytes optional_bytes = 9; + TestMessage2 optional_msg = 10; + TestEnum optional_enum = 11; + + repeated int32 repeated_int32 = 12; + repeated int64 repeated_int64 = 13; + repeated uint32 repeated_uint32 = 14; + repeated uint64 repeated_uint64 = 15; + repeated bool repeated_bool = 16; + repeated float repeated_float = 17; + repeated double repeated_double = 18; + repeated string repeated_string = 19; + repeated bytes repeated_bytes = 20; + repeated TestMessage2 repeated_msg = 21; + repeated TestEnum repeated_enum = 22; +} + +message TestMessage2 { + int32 foo = 1; +} + +enum TestEnum { + Default = 0; + A = 1; + B = 2; + C = 3; +} + +message TestEmbeddedMessageParent { + TestEmbeddedMessageChild child_msg = 1; + int32 number = 2; + + repeated TestEmbeddedMessageChild repeated_msg = 3; + repeated int32 repeated_number = 4; +} + +message TestEmbeddedMessageChild { + TestMessage sub_child = 1; +} + +message Recursive1 { + Recursive2 foo = 1; +} + +message Recursive2 { + Recursive1 foo = 1; +} + +message MapMessage { + map map_string_int32 = 1; + map map_string_msg = 2; + map map_string_enum = 3; +} + +message MapMessageWireEquiv { + repeated MapMessageWireEquiv_entry1 map_string_int32 = 1; + repeated MapMessageWireEquiv_entry2 map_string_msg = 2; +} + +message MapMessageWireEquiv_entry1 { + string key = 1; + int32 value = 2; +} + +message MapMessageWireEquiv_entry2 { + string key = 1; + TestMessage2 value = 2; +} + +message OneofMessage { + oneof my_oneof { + string a = 1; + int32 b = 2; + TestMessage2 c = 3; + TestEnum d = 4; + } +} + +message Outer { + map items = 1; +} + +message Inner { +} + +message Wrapper { + google.protobuf.DoubleValue double = 1; + google.protobuf.FloatValue float = 2; + google.protobuf.Int32Value int32 = 3; + google.protobuf.Int64Value int64 = 4; + google.protobuf.UInt32Value uint32 = 5; + google.protobuf.UInt64Value uint64 = 6; + google.protobuf.BoolValue bool = 7; + google.protobuf.StringValue string = 8; + google.protobuf.BytesValue bytes = 9; + string real_string = 100; + oneof a_oneof { + string oneof_string = 10; + } +} + +message TimeMessage { + google.protobuf.Timestamp timestamp = 1; + google.protobuf.Duration duration = 2; +} + +message Enumer { + TestEnum optional_enum = 1; + repeated TestEnum repeated_enum = 2; + string a_const = 3; + oneof a_oneof { + string str = 10; + TestEnum const = 11; + } +} + +message MyRepeatedStruct { + repeated MyStruct structs = 1; +} + +message MyStruct { + string string = 1; + google.protobuf.Struct struct = 2; +} diff --git a/ruby/tests/basic_test_proto2.proto b/ruby/tests/basic_test_proto2.proto new file mode 100644 index 0000000000..e54ed3185a --- /dev/null +++ b/ruby/tests/basic_test_proto2.proto @@ -0,0 +1,162 @@ +syntax = "proto2"; + +package basic_test_proto2; + +import "google/protobuf/wrappers.proto"; +import "google/protobuf/timestamp.proto"; +import "google/protobuf/duration.proto"; +import "google/protobuf/struct.proto"; + +message Foo { + optional Bar bar = 1; + repeated Baz baz = 2; +} + +message Bar { + optional string msg = 1; +} + +message Baz { + optional string msg = 1; +} + +message TestMessage { + optional int32 optional_int32 = 1; + optional int64 optional_int64 = 2; + optional uint32 optional_uint32 = 3; + optional uint64 optional_uint64 = 4; + optional bool optional_bool = 5; + optional float optional_float = 6; + optional double optional_double = 7; + optional string optional_string = 8; + optional bytes optional_bytes = 9; + optional TestMessage2 optional_msg = 10; + optional TestEnum optional_enum = 11; + + repeated int32 repeated_int32 = 12; + repeated int64 repeated_int64 = 13; + repeated uint32 repeated_uint32 = 14; + repeated uint64 repeated_uint64 = 15; + repeated bool repeated_bool = 16; + repeated float repeated_float = 17; + repeated double repeated_double = 18; + repeated string repeated_string = 19; + repeated bytes repeated_bytes = 20; + repeated TestMessage2 repeated_msg = 21; + repeated TestEnum repeated_enum = 22; +} + +message TestMessage2 { + optional int32 foo = 1; +} + +message TestMessageDefaults { + optional int32 optional_int32 = 1 [default = 1]; + optional int64 optional_int64 = 2 [default = 2]; + optional uint32 optional_uint32 = 3 [default = 3]; + optional uint64 optional_uint64 = 4 [default = 4]; + optional bool optional_bool = 5 [default = true]; + optional float optional_float = 6 [default = 6]; + optional double optional_double = 7 [default = 7]; + optional string optional_string = 8 [default = "Default Str"]; + optional bytes optional_bytes = 9 [default = "\xCF\xA5s\xBD\xBA\xE6fubar"]; + optional TestMessage2 optional_msg = 10; + optional TestNonZeroEnum optional_enum = 11 [default = B2]; +} + +enum TestEnum { + Default = 0; + A = 1; + B = 2; + C = 3; +} + +enum TestNonZeroEnum { + A2 = 1; + B2 = 2; + C2 = 3; +} + +message TestEmbeddedMessageParent { + optional TestEmbeddedMessageChild child_msg = 1; + optional int32 number = 2; + + repeated TestEmbeddedMessageChild repeated_msg = 3; + repeated int32 repeated_number = 4; +} + +message TestEmbeddedMessageChild { + optional TestMessage sub_child = 1; +} + +message Recursive1 { + optional Recursive2 foo = 1; +} + +message Recursive2 { + optional Recursive1 foo = 1; +} + +message MapMessageWireEquiv { + repeated MapMessageWireEquiv_entry1 map_string_int32 = 1; + repeated MapMessageWireEquiv_entry2 map_string_msg = 2; +} + +message MapMessageWireEquiv_entry1 { + optional string key = 1; + optional int32 value = 2; +} + +message MapMessageWireEquiv_entry2 { + optional string key = 1; + optional TestMessage2 value = 2; +} + +message OneofMessage { + oneof my_oneof { + string a = 1; + int32 b = 2; + TestMessage2 c = 3; + TestEnum d = 4; + } +} + +message Wrapper { + optional google.protobuf.DoubleValue double = 1; + optional google.protobuf.FloatValue float = 2; + optional google.protobuf.Int32Value int32 = 3; + optional google.protobuf.Int64Value int64 = 4; + optional google.protobuf.UInt32Value uint32 = 5; + optional google.protobuf.UInt64Value uint64 = 6; + optional google.protobuf.BoolValue bool = 7; + optional google.protobuf.StringValue string = 8; + optional google.protobuf.BytesValue bytes = 9; + optional string real_string = 100; + oneof a_oneof { + string oneof_string = 10; + } +} + +message TimeMessage { + optional google.protobuf.Timestamp timestamp = 1; + optional google.protobuf.Duration duration = 2; +} + +message Enumer { + optional TestEnum optional_enum = 11; + repeated TestEnum repeated_enum = 22; + optional string a_const = 3; + oneof a_oneof { + string str = 100; + TestEnum const = 101; + } +} + +message MyRepeatedStruct { + repeated MyStruct structs = 1; +} + +message MyStruct { + optional string string = 1; + optional google.protobuf.Struct struct = 2; +} diff --git a/ruby/tests/common_tests.rb b/ruby/tests/common_tests.rb new file mode 100644 index 0000000000..fa1e2ff7d9 --- /dev/null +++ b/ruby/tests/common_tests.rb @@ -0,0 +1,1551 @@ +require 'google/protobuf/wrappers_pb.rb' + +# Defines tests which are common between proto2 and proto3 syntax. +# +# Requires that the proto messages are exactly the same in proto2 and proto3 syntax +# and that the including class should define a 'proto_module' method which returns +# the enclosing module of the proto message classes. + +require 'bigdecimal' + +module CommonTests + # Ruby 2.5 changed to raise FrozenError instead of RuntimeError + FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError + + def test_defaults + m = proto_module::TestMessage.new + assert m.optional_int32 == 0 + assert m.optional_int64 == 0 + assert m.optional_uint32 == 0 + assert m.optional_uint64 == 0 + assert m.optional_bool == false + assert m.optional_float == 0.0 + assert m.optional_double == 0.0 + assert m.optional_string == "" + assert m.optional_bytes == "" + assert m.optional_msg == nil + assert m.optional_enum == :Default + end + + def test_setters + m = proto_module::TestMessage.new + m.optional_int32 = -42 + assert m.optional_int32 == -42 + m.optional_int64 = -0x1_0000_0000 + assert m.optional_int64 == -0x1_0000_0000 + m.optional_uint32 = 0x9000_0000 + assert m.optional_uint32 == 0x9000_0000 + m.optional_uint64 = 0x9000_0000_0000_0000 + assert m.optional_uint64 == 0x9000_0000_0000_0000 + m.optional_bool = true + assert m.optional_bool == true + m.optional_float = 0.5 + assert m.optional_float == 0.5 + m.optional_double = 0.5 + assert m.optional_double == 0.5 + m.optional_string = "hello" + assert m.optional_string == "hello" + m.optional_string = :hello + assert m.optional_string == "hello" + m.optional_bytes = "world".encode!('ASCII-8BIT') + assert m.optional_bytes == "world" + m.optional_msg = proto_module::TestMessage2.new(:foo => 42) + assert m.optional_msg == proto_module::TestMessage2.new(:foo => 42) + m.optional_msg = nil + assert m.optional_msg == nil + m.optional_enum = :C + assert m.optional_enum == :C + m.optional_enum = 'C' + assert m.optional_enum == :C + end + + def test_ctor_args + m = proto_module::TestMessage.new(:optional_int32 => -42, + :optional_msg => proto_module::TestMessage2.new, + :optional_enum => :C, + :repeated_string => ["hello", "there", "world"]) + assert m.optional_int32 == -42 + assert m.optional_msg.class == proto_module::TestMessage2 + assert m.repeated_string.length == 3 + assert m.optional_enum == :C + assert m.repeated_string[0] == "hello" + assert m.repeated_string[1] == "there" + assert m.repeated_string[2] == "world" + end + + def test_ctor_string_symbol_args + m = proto_module::TestMessage.new(:optional_enum => 'C', :repeated_enum => ['A', 'B']) + assert_equal :C, m.optional_enum + assert_equal [:A, :B], m.repeated_enum + + m = proto_module::TestMessage.new(:optional_string => :foo, :repeated_string => [:foo, :bar]) + assert_equal 'foo', m.optional_string + assert_equal ['foo', 'bar'], m.repeated_string + end + + def test_ctor_nil_args + m = proto_module::TestMessage.new(:optional_enum => nil, :optional_int32 => nil, :optional_string => nil, :optional_msg => nil) + + assert_equal :Default, m.optional_enum + assert_equal 0, m.optional_int32 + assert_equal "", m.optional_string + assert_nil m.optional_msg + end + + def test_embeddedmsg_hash_init + m = proto_module::TestEmbeddedMessageParent.new( + :child_msg => {sub_child: {optional_int32: 1}}, + :number => 2, + :repeated_msg => [{sub_child: {optional_int32: 3}}], + :repeated_number => [10, 20, 30]) + + assert_equal 2, m.number + assert_equal [10, 20, 30], m.repeated_number + + assert_not_nil m.child_msg + assert_not_nil m.child_msg.sub_child + assert_equal m.child_msg.sub_child.optional_int32, 1 + + assert_not_nil m.repeated_msg + assert_equal 1, m.repeated_msg.length + assert_equal 3, m.repeated_msg.first.sub_child.optional_int32 + end + + def test_inspect_eq_to_s + m = proto_module::TestMessage.new( + :optional_int32 => -42, + :optional_enum => :A, + :optional_msg => proto_module::TestMessage2.new, + :repeated_string => ["hello", "there", "world"]) + expected = "<#{proto_module}::TestMessage: optional_int32: -42, optional_int64: 0, optional_uint32: 0, optional_uint64: 0, optional_bool: false, optional_float: 0.0, optional_double: 0.0, optional_string: \"\", optional_bytes: \"\", optional_msg: <#{proto_module}::TestMessage2: foo: 0>, optional_enum: :A, repeated_int32: [], repeated_int64: [], repeated_uint32: [], repeated_uint64: [], repeated_bool: [], repeated_float: [], repeated_double: [], repeated_string: [\"hello\", \"there\", \"world\"], repeated_bytes: [], repeated_msg: [], repeated_enum: []>" + assert_equal expected, m.inspect + assert_equal expected, m.to_s + + m = proto_module::OneofMessage.new(:b => -42) + expected = "<#{proto_module}::OneofMessage: a: \"\", b: -42, c: nil, d: :Default>" + assert_equal expected, m.inspect + assert_equal expected, m.to_s + end + + def test_hash + m1 = proto_module::TestMessage.new(:optional_int32 => 42) + m2 = proto_module::TestMessage.new(:optional_int32 => 102, repeated_string: ['please', 'work', 'ok?']) + m3 = proto_module::TestMessage.new(:optional_int32 => 102, repeated_string: ['please', 'work', 'ok?']) + assert m1.hash != 0 + assert m2.hash != 0 + assert m3.hash != 0 + # relying on the randomness here -- if hash function changes and we are + # unlucky enough to get a collision, then change the values above. + assert m1.hash != m2.hash + assert_equal m2.hash, m3.hash + end + + def test_unknown_field_errors + e = assert_raise NoMethodError do + proto_module::TestMessage.new.hello + end + assert_match(/hello/, e.message) + + e = assert_raise NoMethodError do + proto_module::TestMessage.new.hello = "world" + end + assert_match(/hello/, e.message) + end + + def test_type_errors + m = proto_module::TestMessage.new + e = assert_raise Google::Protobuf::TypeError do + m.optional_int32 = "hello" + end + + # Google::Protobuf::TypeError should inherit from TypeError for backwards compatibility + # TODO: This can be removed when we can safely migrate to Google::Protobuf::TypeError + assert e.is_a?(::TypeError) + + assert_raise Google::Protobuf::TypeError do + m.optional_string = 42 + end + assert_raise Google::Protobuf::TypeError do + m.optional_string = nil + end + assert_raise Google::Protobuf::TypeError do + m.optional_bool = 42 + end + assert_raise Google::Protobuf::TypeError do + m.optional_msg = proto_module::TestMessage.new # expects TestMessage2 + end + + assert_raise Google::Protobuf::TypeError do + m.repeated_int32 = [] # needs RepeatedField + end + + assert_raise Google::Protobuf::TypeError do + m.repeated_int32.push "hello" + end + + assert_raise Google::Protobuf::TypeError do + m.repeated_msg.push proto_module::TestMessage.new + end + end + + def test_string_encoding + m = proto_module::TestMessage.new + + # Assigning a normal (ASCII or UTF8) string to a bytes field, or + # ASCII-8BIT to a string field will convert to the proper encoding. + m.optional_bytes = "Test string ASCII".encode!('ASCII') + assert m.optional_bytes.frozen? + assert_equal Encoding::ASCII_8BIT, m.optional_bytes.encoding + assert_equal "Test string ASCII", m.optional_bytes + + assert_raise Encoding::UndefinedConversionError do + m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8') + end + + assert_raise Encoding::UndefinedConversionError do + m.optional_string = ["FFFF"].pack('H*') + end + + # "Ordinary" use case. + m.optional_bytes = ["FFFF"].pack('H*') + m.optional_string = "\u0100" + + # strings are immutable so we can't do this, but serialize should catch it. + m.optional_string = "asdf".encode!('UTF-8') + assert_raise(FrozenErrorType) { m.optional_string.encode!('ASCII-8BIT') } + end + + def test_rptfield_int32 + l = Google::Protobuf::RepeatedField.new(:int32) + assert l.count == 0 + l = Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3]) + assert l.count == 3 + assert_equal [1, 2, 3], l + assert_equal l, [1, 2, 3] + l.push 4 + assert l == [1, 2, 3, 4] + dst_list = [] + l.each { |val| dst_list.push val } + assert dst_list == [1, 2, 3, 4] + assert l.to_a == [1, 2, 3, 4] + assert l[0] == 1 + assert l[3] == 4 + l[0] = 5 + assert l == [5, 2, 3, 4] + + l2 = l.dup + assert l == l2 + assert l.object_id != l2.object_id + l2.push 6 + assert l.count == 4 + assert l2.count == 5 + + assert l.inspect == '[5, 2, 3, 4]' + + l.concat([7, 8, 9]) + assert l == [5, 2, 3, 4, 7, 8, 9] + assert l.pop == 9 + assert l == [5, 2, 3, 4, 7, 8] + + assert_raise Google::Protobuf::TypeError do + m = proto_module::TestMessage.new + l.push m + end + + m = proto_module::TestMessage.new + m.repeated_int32 = l + assert m.repeated_int32 == [5, 2, 3, 4, 7, 8] + assert m.repeated_int32.object_id == l.object_id + l.push 42 + assert m.repeated_int32.pop == 42 + + l3 = l + l.dup + assert l3.count == l.count * 2 + l.count.times do |i| + assert l3[i] == l[i] + assert l3[l.count + i] == l[i] + end + + l.clear + assert l.count == 0 + l += [1, 2, 3, 4] + l.replace([5, 6, 7, 8]) + assert l == [5, 6, 7, 8] + + l4 = Google::Protobuf::RepeatedField.new(:int32) + l4[5] = 42 + assert l4 == [0, 0, 0, 0, 0, 42] + + l4 << 100 + assert l4 == [0, 0, 0, 0, 0, 42, 100] + l4 << 101 << 102 + assert l4 == [0, 0, 0, 0, 0, 42, 100, 101, 102] + end + + def test_parent_rptfield + #make sure we set the RepeatedField and can add to it + m = proto_module::TestMessage.new + assert m.repeated_string == [] + m.repeated_string << 'ok' + m.repeated_string.push('ok2') + assert m.repeated_string == ['ok', 'ok2'] + m.repeated_string += ['ok3'] + assert m.repeated_string == ['ok', 'ok2', 'ok3'] + end + + def test_rptfield_msg + l = Google::Protobuf::RepeatedField.new(:message, proto_module::TestMessage) + l.push proto_module::TestMessage.new + assert l.count == 1 + assert_raise Google::Protobuf::TypeError do + l.push proto_module::TestMessage2.new + end + assert_raise Google::Protobuf::TypeError do + l.push 42 + end + + l2 = l.dup + assert l2[0] == l[0] + assert l2[0].object_id == l[0].object_id + + l2 = Google::Protobuf.deep_copy(l) + assert l2[0] == l[0] + assert l2[0].object_id != l[0].object_id + + l3 = l + l2 + assert l3.count == 2 + assert l3[0] == l[0] + assert l3[1] == l2[0] + l3[0].optional_int32 = 1000 + assert l[0].optional_int32 == 1000 + + new_msg = proto_module::TestMessage.new(:optional_int32 => 200) + l4 = l + [new_msg] + assert l4.count == 2 + new_msg.optional_int32 = 1000 + assert l4[1].optional_int32 == 1000 + end + + def test_rptfield_enum + l = Google::Protobuf::RepeatedField.new(:enum, proto_module::TestEnum) + l.push :A + l.push :B + l.push :C + assert l.count == 3 + assert_raise RangeError do + l.push :D + end + assert l[0] == :A + + l.push 4 + assert l[3] == 4 + end + + def test_rptfield_initialize + assert_raise ArgumentError do + Google::Protobuf::RepeatedField.new + end + assert_raise ArgumentError do + Google::Protobuf::RepeatedField.new(:message) + end + assert_raise ArgumentError do + Google::Protobuf::RepeatedField.new([1, 2, 3]) + end + assert_raise ArgumentError do + Google::Protobuf::RepeatedField.new(:message, [proto_module::TestMessage2.new]) + end + end + + def test_rptfield_array_ducktyping + l = Google::Protobuf::RepeatedField.new(:int32) + length_methods = %w(count length size) + length_methods.each do |lm| + assert l.send(lm) == 0 + end + # out of bounds returns a nil + assert l[0] == nil + assert l[1] == nil + assert l[-1] == nil + l.push 4 + length_methods.each do |lm| + assert l.send(lm) == 1 + end + assert l[0] == 4 + assert l[1] == nil + assert l[-1] == 4 + assert l[-2] == nil + + l.push 2 + length_methods.each do |lm| + assert l.send(lm) == 2 + end + assert l[0] == 4 + assert l[1] == 2 + assert l[2] == nil + assert l[-1] == 2 + assert l[-2] == 4 + assert l[-3] == nil + + #adding out of scope will backfill with empty objects + end + + def test_map_basic + # allowed key types: + # :int32, :int64, :uint32, :uint64, :bool, :string, :bytes. + + m = Google::Protobuf::Map.new(:string, :int32) + m["asdf"] = 1 + assert m["asdf"] == 1 + m["jkl;"] = 42 + assert m == { "jkl;" => 42, "asdf" => 1 } + assert m.has_key?("asdf") + assert !m.has_key?("qwerty") + assert m.length == 2 + + m2 = m.dup + assert_equal m, m2 + assert m.hash != 0 + assert_equal m.hash, m2.hash + + collected = {} + m.each { |k,v| collected[v] = k } + assert collected == { 42 => "jkl;", 1 => "asdf" } + + assert m.delete("asdf") == 1 + assert !m.has_key?("asdf") + assert m["asdf"] == nil + assert !m.has_key?("asdf") + + # We only assert on inspect value when there is one map entry because the + # order in which elements appear is unspecified (depends on the internal + # hash function). We don't want a brittle test. + assert m.inspect == "{\"jkl;\"=>42}" + + assert m.keys == ["jkl;"] + assert m.values == [42] + + m.clear + assert m.length == 0 + assert m == {} + + assert_raise TypeError do + m[1] = 1 + end + assert_raise RangeError do + m["asdf"] = 0x1_0000_0000 + end + end + + def test_map_ctor + m = Google::Protobuf::Map.new(:string, :int32, + {"a" => 1, "b" => 2, "c" => 3}) + assert m == {"a" => 1, "c" => 3, "b" => 2} + end + + def test_map_keytypes + m = Google::Protobuf::Map.new(:int32, :int32) + m[1] = 42 + m[-1] = 42 + assert_raise RangeError do + m[0x8000_0000] = 1 + end + assert_raise Google::Protobuf::TypeError do + m["asdf"] = 1 + end + + m = Google::Protobuf::Map.new(:int64, :int32) + m[0x1000_0000_0000_0000] = 1 + assert_raise RangeError do + m[0x1_0000_0000_0000_0000] = 1 + end + assert_raise Google::Protobuf::TypeError do + m["asdf"] = 1 + end + + m = Google::Protobuf::Map.new(:uint32, :int32) + m[0x8000_0000] = 1 + assert_raise RangeError do + m[0x1_0000_0000] = 1 + end + assert_raise RangeError do + m[-1] = 1 + end + + m = Google::Protobuf::Map.new(:uint64, :int32) + m[0x8000_0000_0000_0000] = 1 + assert_raise RangeError do + m[0x1_0000_0000_0000_0000] = 1 + end + assert_raise RangeError do + m[-1] = 1 + end + + m = Google::Protobuf::Map.new(:bool, :int32) + m[true] = 1 + m[false] = 2 + assert_raise Google::Protobuf::TypeError do + m[1] = 1 + end + assert_raise Google::Protobuf::TypeError do + m["asdf"] = 1 + end + + m = Google::Protobuf::Map.new(:string, :int32) + m["asdf"] = 1 + assert_raise TypeError do + m[1] = 1 + end + assert_raise Encoding::UndefinedConversionError do + bytestring = ["FFFF"].pack("H*") + m[bytestring] = 1 + end + + m = Google::Protobuf::Map.new(:bytes, :int32) + bytestring = ["FFFF"].pack("H*") + m[bytestring] = 1 + # Allowed -- we will automatically convert to ASCII-8BIT. + m["asdf"] = 1 + assert_raise TypeError do + m[1] = 1 + end + end + + def test_map_msg_enum_valuetypes + m = Google::Protobuf::Map.new(:string, :message, proto_module::TestMessage) + m["asdf"] = proto_module::TestMessage.new + assert_raise Google::Protobuf::TypeError do + m["jkl;"] = proto_module::TestMessage2.new + end + + m = Google::Protobuf::Map.new( + :string, :message, proto_module::TestMessage, + { "a" => proto_module::TestMessage.new(:optional_int32 => 42), + "b" => proto_module::TestMessage.new(:optional_int32 => 84) }) + assert m.length == 2 + assert m.values.map{|msg| msg.optional_int32}.sort == [42, 84] + + m = Google::Protobuf::Map.new(:string, :enum, proto_module::TestEnum, + { "x" => :A, "y" => :B, "z" => :C }) + assert m.length == 3 + assert m["z"] == :C + m["z"] = 2 + assert m["z"] == :B + m["z"] = 4 + assert m["z"] == 4 + assert_raise RangeError do + m["z"] = :Z + end + assert_raise RangeError do + m["z"] = "z" + end + end + + def test_map_dup_deep_copy + m = Google::Protobuf::Map.new( + :string, :message, proto_module::TestMessage, + { "a" => proto_module::TestMessage.new(:optional_int32 => 42), + "b" => proto_module::TestMessage.new(:optional_int32 => 84) }) + + m2 = m.dup + assert m == m2 + assert m.object_id != m2.object_id + assert m["a"].object_id == m2["a"].object_id + assert m["b"].object_id == m2["b"].object_id + + m2 = Google::Protobuf.deep_copy(m) + assert m == m2 + assert m.object_id != m2.object_id + assert m["a"].object_id != m2["a"].object_id + assert m["b"].object_id != m2["b"].object_id + end + + def test_oneof_descriptors + d = proto_module::OneofMessage.descriptor + o = d.lookup_oneof("my_oneof") + assert o != nil + assert o.class == Google::Protobuf::OneofDescriptor + assert o.name == "my_oneof" + oneof_count = 0 + d.each_oneof{ |oneof| + oneof_count += 1 + assert oneof == o + } + assert oneof_count == 1 + assert o.count == 4 + field_names = o.map{|f| f.name}.sort + assert field_names == ["a", "b", "c", "d"] + end + + def test_oneof + d = proto_module::OneofMessage.new + assert d.a == "" + assert d.b == 0 + assert d.c == nil + assert d.d == :Default + assert d.my_oneof == nil + + d.a = "hi" + assert d.a == "hi" + assert d.b == 0 + assert d.c == nil + assert d.d == :Default + assert d.my_oneof == :a + + d.b = 42 + assert d.a == "" + assert d.b == 42 + assert d.c == nil + assert d.d == :Default + assert d.my_oneof == :b + + d.c = proto_module::TestMessage2.new(:foo => 100) + assert d.a == "" + assert d.b == 0 + assert d.c.foo == 100 + assert d.d == :Default + assert d.my_oneof == :c + + d.d = :C + assert d.a == "" + assert d.b == 0 + assert d.c == nil + assert d.d == :C + assert d.my_oneof == :d + + d2 = proto_module::OneofMessage.decode(proto_module::OneofMessage.encode(d)) + assert d2 == d + + encoded_field_a = proto_module::OneofMessage.encode(proto_module::OneofMessage.new(:a => "string")) + encoded_field_b = proto_module::OneofMessage.encode(proto_module::OneofMessage.new(:b => 1000)) + encoded_field_c = proto_module::OneofMessage.encode( + proto_module::OneofMessage.new(:c => proto_module::TestMessage2.new(:foo => 1))) + encoded_field_d = proto_module::OneofMessage.encode(proto_module::OneofMessage.new(:d => :B)) + + d3 = proto_module::OneofMessage.decode( + encoded_field_c + encoded_field_a + encoded_field_b + encoded_field_d) + assert d3.a == "" + assert d3.b == 0 + assert d3.c == nil + assert d3.d == :B + + d4 = proto_module::OneofMessage.decode( + encoded_field_c + encoded_field_a + encoded_field_b + encoded_field_d + + encoded_field_c) + assert d4.a == "" + assert d4.b == 0 + assert d4.c.foo == 1 + assert d4.d == :Default + + d5 = proto_module::OneofMessage.new(:a => "hello") + assert d5.a == "hello" + d5.a = nil + assert d5.a == "" + assert proto_module::OneofMessage.encode(d5) == '' + assert d5.my_oneof == nil + end + + def test_enum_field + m = proto_module::TestMessage.new + assert m.optional_enum == :Default + m.optional_enum = :A + assert m.optional_enum == :A + assert_raise RangeError do + m.optional_enum = :ASDF + end + m.optional_enum = 1 + assert m.optional_enum == :A + m.optional_enum = 100 + assert m.optional_enum == 100 + end + + def test_dup + m = proto_module::TestMessage.new + m.optional_string = "hello" + m.optional_int32 = 42 + tm1 = proto_module::TestMessage2.new(:foo => 100) + tm2 = proto_module::TestMessage2.new(:foo => 200) + m.repeated_msg.push tm1 + assert m.repeated_msg[-1] == tm1 + m.repeated_msg.push tm2 + assert m.repeated_msg[-1] == tm2 + m2 = m.dup + assert m == m2 + m.optional_int32 += 1 + assert m != m2 + assert m.repeated_msg[0] == m2.repeated_msg[0] + assert m.repeated_msg[0].object_id == m2.repeated_msg[0].object_id + end + + def test_deep_copy + m = proto_module::TestMessage.new(:optional_int32 => 42, + :repeated_msg => [proto_module::TestMessage2.new(:foo => 100)]) + m2 = Google::Protobuf.deep_copy(m) + assert m == m2 + assert m.repeated_msg == m2.repeated_msg + assert m.repeated_msg.object_id != m2.repeated_msg.object_id + assert m.repeated_msg[0].object_id != m2.repeated_msg[0].object_id + end + + def test_eq + m = proto_module::TestMessage.new(:optional_int32 => 42, + :repeated_int32 => [1, 2, 3]) + m2 = proto_module::TestMessage.new(:optional_int32 => 43, + :repeated_int32 => [1, 2, 3]) + assert m != m2 + end + + def test_enum_lookup + assert proto_module::TestEnum::A == 1 + assert proto_module::TestEnum::B == 2 + assert proto_module::TestEnum::C == 3 + + assert proto_module::TestEnum::lookup(1) == :A + assert proto_module::TestEnum::lookup(2) == :B + assert proto_module::TestEnum::lookup(3) == :C + + assert proto_module::TestEnum::resolve(:A) == 1 + assert proto_module::TestEnum::resolve(:B) == 2 + assert proto_module::TestEnum::resolve(:C) == 3 + end + + def test_enum_const_get_helpers + m = proto_module::TestMessage.new + assert_equal proto_module::TestEnum::Default, m.optional_enum_const + assert_equal proto_module::TestEnum.const_get(:Default), m.optional_enum_const + + m = proto_module::TestMessage.new({optional_enum: proto_module::TestEnum::A}) + assert_equal proto_module::TestEnum::A, m.optional_enum_const + assert_equal proto_module::TestEnum.const_get(:A), m.optional_enum_const + + m = proto_module::TestMessage.new({optional_enum: proto_module::TestEnum::B}) + assert_equal proto_module::TestEnum::B, m.optional_enum_const + assert_equal proto_module::TestEnum.const_get(:B), m.optional_enum_const + + m = proto_module::TestMessage.new({optional_enum: proto_module::TestEnum::C}) + assert_equal proto_module::TestEnum::C, m.optional_enum_const + assert_equal proto_module::TestEnum.const_get(:C), m.optional_enum_const + + m = proto_module::TestMessage2.new({foo: 2}) + assert_equal 2, m.foo + assert_raise(NoMethodError) { m.foo_ } + assert_raise(NoMethodError) { m.foo_X } + assert_raise(NoMethodError) { m.foo_XX } + assert_raise(NoMethodError) { m.foo_XXX } + assert_raise(NoMethodError) { m.foo_XXXX } + assert_raise(NoMethodError) { m.foo_XXXXX } + assert_raise(NoMethodError) { m.foo_XXXXXX } + + m = proto_module::Enumer.new({optional_enum: :B}) + assert_equal :B, m.optional_enum + assert_raise(NoMethodError) { m.optional_enum_ } + assert_raise(NoMethodError) { m.optional_enum_X } + assert_raise(NoMethodError) { m.optional_enum_XX } + assert_raise(NoMethodError) { m.optional_enum_XXX } + assert_raise(NoMethodError) { m.optional_enum_XXXX } + assert_raise(NoMethodError) { m.optional_enum_XXXXX } + assert_raise(NoMethodError) { m.optional_enum_XXXXXX } + end + + def test_enum_getter + m = proto_module::Enumer.new(:optional_enum => :B, :repeated_enum => [:A, :C]) + + assert_equal :B, m.optional_enum + assert_equal 2, m.optional_enum_const + assert_equal proto_module::TestEnum::B, m.optional_enum_const + assert_equal [:A, :C], m.repeated_enum + assert_equal [1, 3], m.repeated_enum_const + assert_equal [proto_module::TestEnum::A, proto_module::TestEnum::C], m.repeated_enum_const + end + + def test_enum_getter_oneof + m = proto_module::Enumer.new(:const => :C) + + assert_equal :C, m.const + assert_equal 3, m.const_const + assert_equal proto_module::TestEnum::C, m.const_const + end + + def test_enum_getter_only_enums + m = proto_module::Enumer.new(:optional_enum => :B, :a_const => 'thing') + + assert_equal 'thing', m.a_const + assert_equal :B, m.optional_enum + + assert_raise(NoMethodError) { m.a } + assert_raise(NoMethodError) { m.a_const_const } + end + + def test_repeated_push + m = proto_module::TestMessage.new + + m.repeated_string += ['one'] + m.repeated_string += %w[two three] + assert_equal %w[one two three], m.repeated_string + + m.repeated_string.push *['four', 'five'] + assert_equal %w[one two three four five], m.repeated_string + + m.repeated_string.push 'six', 'seven' + assert_equal %w[one two three four five six seven], m.repeated_string + + m = proto_module::TestMessage.new + + m.repeated_msg += [proto_module::TestMessage2.new(:foo => 1), proto_module::TestMessage2.new(:foo => 2)] + m.repeated_msg += [proto_module::TestMessage2.new(:foo => 3)] + m.repeated_msg.push proto_module::TestMessage2.new(:foo => 4), proto_module::TestMessage2.new(:foo => 5) + assert_equal [1, 2, 3, 4, 5], m.repeated_msg.map {|x| x.foo} + end + + def test_parse_serialize + m = proto_module::TestMessage.new(:optional_int32 => 42, + :optional_string => "hello world", + :optional_enum => :B, + :repeated_string => ["a", "b", "c"], + :repeated_int32 => [42, 43, 44], + :repeated_enum => [:A, :B, :C, 100], + :repeated_msg => [proto_module::TestMessage2.new(:foo => 1), + proto_module::TestMessage2.new(:foo => 2)]) + data = proto_module::TestMessage.encode m + m2 = proto_module::TestMessage.decode data + assert m == m2 + + data = Google::Protobuf.encode m + m2 = Google::Protobuf.decode(proto_module::TestMessage, data) + assert m == m2 + end + + def test_encode_decode_helpers + m = proto_module::TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + assert_equal 'foo', m.optional_string + assert_equal ['bar1', 'bar2'], m.repeated_string + + json = m.to_json + m2 = proto_module::TestMessage.decode_json(json) + assert_equal 'foo', m2.optional_string + assert_equal ['bar1', 'bar2'], m2.repeated_string + if RUBY_PLATFORM != "java" + assert m2.optional_string.frozen? + assert m2.repeated_string[0].frozen? + end + + proto = m.to_proto + m2 = proto_module::TestMessage.decode(proto) + assert_equal 'foo', m2.optional_string + assert_equal ['bar1', 'bar2'], m2.repeated_string + end + + def test_protobuf_encode_decode_helpers + m = proto_module::TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + encoded_msg = Google::Protobuf.encode(m) + assert_equal m.to_proto, encoded_msg + + decoded_msg = Google::Protobuf.decode(proto_module::TestMessage, encoded_msg) + assert_equal proto_module::TestMessage.decode(m.to_proto), decoded_msg + end + + def test_protobuf_encode_decode_json_helpers + m = proto_module::TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + encoded_msg = Google::Protobuf.encode_json(m) + assert_equal m.to_json, encoded_msg + + decoded_msg = Google::Protobuf.decode_json(proto_module::TestMessage, encoded_msg) + assert_equal proto_module::TestMessage.decode_json(m.to_json), decoded_msg + end + + def test_def_errors + s = Google::Protobuf::DescriptorPool.new + assert_raise Google::Protobuf::TypeError do + s.build do + # enum with no default (integer value 0) + add_enum "MyEnum" do + value :A, 1 + end + end + end + assert_raise Google::Protobuf::TypeError do + s.build do + # message with required field (unsupported in proto3) + add_message "MyMessage" do + required :foo, :int32, 1 + end + end + end + end + + def test_corecursive + # just be sure that we can instantiate types with corecursive field-type + # references. + m = proto_module::Recursive1.new(:foo => proto_module::Recursive2.new(:foo => proto_module::Recursive1.new)) + assert proto_module::Recursive1.descriptor.lookup("foo").subtype == + proto_module::Recursive2.descriptor + assert proto_module::Recursive2.descriptor.lookup("foo").subtype == + proto_module::Recursive1.descriptor + + serialized = proto_module::Recursive1.encode(m) + m2 = proto_module::Recursive1.decode(serialized) + assert m == m2 + end + + def test_serialize_cycle + m = proto_module::Recursive1.new(:foo => proto_module::Recursive2.new) + m.foo.foo = m + assert_raise RuntimeError do + proto_module::Recursive1.encode(m) + end + end + + def test_bad_field_names + m = proto_module::BadFieldNames.new(:dup => 1, :class => 2) + m2 = m.dup + assert m == m2 + assert m['dup'] == 1 + assert m['class'] == 2 + m['dup'] = 3 + assert m['dup'] == 3 + m['a.b'] = 4 + assert m['a.b'] == 4 + end + + def test_int_ranges + m = proto_module::TestMessage.new + + m.optional_int32 = 0 + m.optional_int32 = -0x8000_0000 + m.optional_int32 = +0x7fff_ffff + m.optional_int32 = 1.0 + m.optional_int32 = -1.0 + m.optional_int32 = 2e9 + assert_raise RangeError do + m.optional_int32 = -0x8000_0001 + end + assert_raise RangeError do + m.optional_int32 = +0x8000_0000 + end + assert_raise RangeError do + m.optional_int32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum + end + assert_raise RangeError do + m.optional_int32 = 1e12 + end + assert_raise RangeError do + m.optional_int32 = 1.5 + end + + m.optional_uint32 = 0 + m.optional_uint32 = +0xffff_ffff + m.optional_uint32 = 1.0 + m.optional_uint32 = 4e9 + assert_raise RangeError do + m.optional_uint32 = -1 + end + assert_raise RangeError do + m.optional_uint32 = -1.5 + end + assert_raise RangeError do + m.optional_uint32 = -1.5e12 + end + assert_raise RangeError do + m.optional_uint32 = -0x1000_0000_0000_0000 + end + assert_raise RangeError do + m.optional_uint32 = +0x1_0000_0000 + end + assert_raise RangeError do + m.optional_uint32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum + end + assert_raise RangeError do + m.optional_uint32 = 1e12 + end + assert_raise RangeError do + m.optional_uint32 = 1.5 + end + + m.optional_int64 = 0 + m.optional_int64 = -0x8000_0000_0000_0000 + m.optional_int64 = +0x7fff_ffff_ffff_ffff + m.optional_int64 = 1.0 + m.optional_int64 = -1.0 + m.optional_int64 = 8e18 + m.optional_int64 = -8e18 + assert_raise RangeError do + m.optional_int64 = -0x8000_0000_0000_0001 + end + assert_raise RangeError do + m.optional_int64 = +0x8000_0000_0000_0000 + end + assert_raise RangeError do + m.optional_int64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum + end + assert_raise RangeError do + m.optional_int64 = 1e50 + end + assert_raise RangeError do + m.optional_int64 = 1.5 + end + + m.optional_uint64 = 0 + m.optional_uint64 = +0xffff_ffff_ffff_ffff + m.optional_uint64 = 1.0 + m.optional_uint64 = 16e18 + assert_raise RangeError do + m.optional_uint64 = -1 + end + assert_raise RangeError do + m.optional_uint64 = -1.5 + end + assert_raise RangeError do + m.optional_uint64 = -1.5e12 + end + assert_raise RangeError do + m.optional_uint64 = -0x1_0000_0000_0000_0000 + end + assert_raise RangeError do + m.optional_uint64 = +0x1_0000_0000_0000_0000 + end + assert_raise RangeError do + m.optional_uint64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum + end + assert_raise RangeError do + m.optional_uint64 = 1e50 + end + assert_raise RangeError do + m.optional_uint64 = 1.5 + end + end + + def test_stress_test + m = proto_module::TestMessage.new + m.optional_int32 = 42 + m.optional_int64 = 0x100000000 + m.optional_string = "hello world" + 10.times do m.repeated_msg.push proto_module::TestMessage2.new(:foo => 42) end + 10.times do m.repeated_string.push "hello world" end + + data = proto_module::TestMessage.encode(m) + + 10_000.times do + m = proto_module::TestMessage.decode(data) + data_new = proto_module::TestMessage.encode(m) + assert data_new == data + data = data_new + end + end + + def test_reflection + m = proto_module::TestMessage.new(:optional_int32 => 1234) + msgdef = m.class.descriptor + assert msgdef.class == Google::Protobuf::Descriptor + assert msgdef.any? {|field| field.name == "optional_int32"} + optional_int32 = msgdef.lookup "optional_int32" + assert optional_int32.class == Google::Protobuf::FieldDescriptor + assert optional_int32 != nil + assert optional_int32.name == "optional_int32" + assert optional_int32.type == :int32 + optional_int32.set(m, 5678) + assert m.optional_int32 == 5678 + m.optional_int32 = 1000 + assert optional_int32.get(m) == 1000 + + optional_msg = msgdef.lookup "optional_msg" + assert optional_msg.subtype == proto_module::TestMessage2.descriptor + + optional_msg.set(m, optional_msg.subtype.msgclass.new) + + assert msgdef.msgclass == proto_module::TestMessage + + optional_enum = msgdef.lookup "optional_enum" + assert optional_enum.subtype == proto_module::TestEnum.descriptor + assert optional_enum.subtype.class == Google::Protobuf::EnumDescriptor + optional_enum.subtype.each do |k, v| + # set with integer, check resolution to symbolic name + optional_enum.set(m, v) + assert optional_enum.get(m) == k + end + end + + def test_json + # TODO: Fix JSON in JRuby version. + return if RUBY_PLATFORM == "java" + m = proto_module::TestMessage.new(:optional_int32 => 1234, + :optional_int64 => -0x1_0000_0000, + :optional_uint32 => 0x8000_0000, + :optional_uint64 => 0xffff_ffff_ffff_ffff, + :optional_bool => true, + :optional_float => 1.0, + :optional_double => -1e100, + :optional_string => "Test string", + :optional_bytes => ["FFFFFFFF"].pack('H*'), + :optional_msg => proto_module::TestMessage2.new(:foo => 42), + :repeated_int32 => [1, 2, 3, 4], + :repeated_string => ["a", "b", "c"], + :repeated_bool => [true, false, true, false], + :repeated_msg => [proto_module::TestMessage2.new(:foo => 1), + proto_module::TestMessage2.new(:foo => 2)]) + + json_text = proto_module::TestMessage.encode_json(m) + m2 = proto_module::TestMessage.decode_json(json_text) + puts m.inspect + puts m2.inspect + assert m == m2 + + # Crash case from GitHub issue 283. + bar = proto_module::Bar.new(msg: "bar") + baz1 = proto_module::Baz.new(msg: "baz") + baz2 = proto_module::Baz.new(msg: "quux") + proto_module::Foo.encode_json(proto_module::Foo.new) + proto_module::Foo.encode_json(proto_module::Foo.new(bar: bar)) + proto_module::Foo.encode_json(proto_module::Foo.new(bar: bar, baz: [baz1, baz2])) + end + + def test_json_empty + assert proto_module::TestMessage.encode_json(proto_module::TestMessage.new) == '{}' + end + + def test_json_emit_defaults + # TODO: Fix JSON in JRuby version. + return if RUBY_PLATFORM == "java" + m = proto_module::TestMessage.new + + expected = { + optionalInt32: 0, + optionalInt64: "0", + optionalUint32: 0, + optionalUint64: "0", + optionalBool: false, + optionalFloat: 0, + optionalDouble: 0, + optionalString: "", + optionalBytes: "", + optionalEnum: "Default", + repeatedInt32: [], + repeatedInt64: [], + repeatedUint32: [], + repeatedUint64: [], + repeatedBool: [], + repeatedFloat: [], + repeatedDouble: [], + repeatedString: [], + repeatedBytes: [], + repeatedMsg: [], + repeatedEnum: [] + } + + actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true) + + assert JSON.parse(actual, :symbolize_names => true) == expected + end + + def test_json_emit_defaults_submsg + # TODO: Fix JSON in JRuby version. + return if RUBY_PLATFORM == "java" + m = proto_module::TestMessage.new(optional_msg: proto_module::TestMessage2.new) + + expected = { + optionalInt32: 0, + optionalInt64: "0", + optionalUint32: 0, + optionalUint64: "0", + optionalBool: false, + optionalFloat: 0, + optionalDouble: 0, + optionalString: "", + optionalBytes: "", + optionalMsg: {foo: 0}, + optionalEnum: "Default", + repeatedInt32: [], + repeatedInt64: [], + repeatedUint32: [], + repeatedUint64: [], + repeatedBool: [], + repeatedFloat: [], + repeatedDouble: [], + repeatedString: [], + repeatedBytes: [], + repeatedMsg: [], + repeatedEnum: [] + } + + actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true) + + assert JSON.parse(actual, :symbolize_names => true) == expected + end + + def test_json_emit_defaults_repeated_submsg + # TODO: Fix JSON in JRuby version. + return if RUBY_PLATFORM == "java" + m = proto_module::TestMessage.new(repeated_msg: [proto_module::TestMessage2.new]) + + expected = { + optionalInt32: 0, + optionalInt64: "0", + optionalUint32: 0, + optionalUint64: "0", + optionalBool: false, + optionalFloat: 0, + optionalDouble: 0, + optionalString: "", + optionalBytes: "", + optionalEnum: "Default", + repeatedInt32: [], + repeatedInt64: [], + repeatedUint32: [], + repeatedUint64: [], + repeatedBool: [], + repeatedFloat: [], + repeatedDouble: [], + repeatedString: [], + repeatedBytes: [], + repeatedMsg: [{foo: 0}], + repeatedEnum: [] + } + + actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true) + + assert JSON.parse(actual, :symbolize_names => true) == expected + end + + def value_from_ruby(value) + ret = Google::Protobuf::Value.new + case value + when String + ret.string_value = value + when Google::Protobuf::Struct + ret.struct_value = value + when Hash + ret.struct_value = struct_from_ruby(value) + when Google::Protobuf::ListValue + ret.list_value = value + when Array + ret.list_value = list_from_ruby(value) + else + @log.error "Unknown type: #{value.class}" + raise Google::Protobuf::Error, "Unknown type: #{value.class}" + end + ret + end + + def list_from_ruby(arr) + ret = Google::Protobuf::ListValue.new + arr.each do |v| + ret.values << value_from_ruby(v) + end + ret + end + + def struct_from_ruby(hash) + ret = Google::Protobuf::Struct.new + hash.each do |k, v| + ret.fields[k] ||= value_from_ruby(v) + end + ret + end + + def test_deep_json + # will not overflow + json = '{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":'\ + '{"a":{"a":{"a":{"a":{}}}}}}}}}}}}}}}}' + + struct = struct_from_ruby(JSON.parse(json)) + assert_equal json, struct.to_json + + encoded = proto_module::MyRepeatedStruct.encode( + proto_module::MyRepeatedStruct.new(structs: [proto_module::MyStruct.new(struct: struct)])) + assert_equal json, proto_module::MyRepeatedStruct.decode(encoded).structs[0].struct.to_json + + # will overflow + json = '{"a":{"a":{"a":[{"a":{"a":[{"a":[{"a":{"a":[{"a":[{"a":'\ + '{"a":[{"a":[{"a":{"a":{"a":[{"a":"a"}]}}}]}]}}]}]}}]}]}}]}}}' + + struct = struct_from_ruby(JSON.parse(json)) + assert_equal json, struct.to_json + + assert_raise(RuntimeError, "Maximum recursion depth exceeded during encoding") do + proto_module::MyRepeatedStruct.encode( + proto_module::MyRepeatedStruct.new(structs: [proto_module::MyStruct.new(struct: struct)])) + end + end + + def test_comparison_with_arbitrary_object + assert proto_module::TestMessage.new != nil + end + + def test_wrapper_getters + m = proto_module::Wrapper.new( + double: Google::Protobuf::DoubleValue.new(value: 2.0), + float: Google::Protobuf::FloatValue.new(value: 4.0), + int32: Google::Protobuf::Int32Value.new(value: 3), + int64: Google::Protobuf::Int64Value.new(value: 4), + uint32: Google::Protobuf::UInt32Value.new(value: 5), + uint64: Google::Protobuf::UInt64Value.new(value: 6), + bool: Google::Protobuf::BoolValue.new(value: true), + string: Google::Protobuf::StringValue.new(value: 'str'), + bytes: Google::Protobuf::BytesValue.new(value: 'fun'), + real_string: '100' + ) + + assert_equal 2.0, m.double_as_value + assert_equal 2.0, m.double.value + assert_equal 4.0, m.float_as_value + assert_equal 4.0, m.float.value + assert_equal 3, m.int32_as_value + assert_equal 3, m.int32.value + assert_equal 4, m.int64_as_value + assert_equal 4, m.int64.value + assert_equal 5, m.uint32_as_value + assert_equal 5, m.uint32.value + assert_equal 6, m.uint64_as_value + assert_equal 6, m.uint64.value + assert_equal true, m.bool_as_value + assert_equal true, m.bool.value + assert_equal 'str', m.string_as_value + assert_equal 'str', m.string.value + assert_equal 'fun', m.bytes_as_value + assert_equal 'fun', m.bytes.value + end + + def test_wrapper_setters_as_value + m = proto_module::Wrapper.new + + m.double_as_value = 4.8 + assert_equal 4.8, m.double_as_value + assert_equal Google::Protobuf::DoubleValue.new(value: 4.8), m.double + m.float_as_value = 2.4 + assert_in_delta 2.4, m.float_as_value + assert_in_delta Google::Protobuf::FloatValue.new(value: 2.4).value, m.float.value + m.int32_as_value = 5 + assert_equal 5, m.int32_as_value + assert_equal Google::Protobuf::Int32Value.new(value: 5), m.int32 + m.int64_as_value = 15 + assert_equal 15, m.int64_as_value + assert_equal Google::Protobuf::Int64Value.new(value: 15), m.int64 + m.uint32_as_value = 50 + assert_equal 50, m.uint32_as_value + assert_equal Google::Protobuf::UInt32Value.new(value: 50), m.uint32 + m.uint64_as_value = 500 + assert_equal 500, m.uint64_as_value + assert_equal Google::Protobuf::UInt64Value.new(value: 500), m.uint64 + m.bool_as_value = false + assert_equal false, m.bool_as_value + assert_equal Google::Protobuf::BoolValue.new(value: false), m.bool + m.string_as_value = 'xy' + assert_equal 'xy', m.string_as_value + assert_equal Google::Protobuf::StringValue.new(value: 'xy'), m.string + m.bytes_as_value = '123' + assert_equal '123', m.bytes_as_value + assert_equal Google::Protobuf::BytesValue.new(value: '123'), m.bytes + + m.double_as_value = nil + assert_nil m.double + assert_nil m.double_as_value + m.float_as_value = nil + assert_nil m.float + assert_nil m.float_as_value + m.int32_as_value = nil + assert_nil m.int32 + assert_nil m.int32_as_value + m.int64_as_value = nil + assert_nil m.int64 + assert_nil m.int64_as_value + m.uint32_as_value = nil + assert_nil m.uint32 + assert_nil m.uint32_as_value + m.uint64_as_value = nil + assert_nil m.uint64 + assert_nil m.uint64_as_value + m.bool_as_value = nil + assert_nil m.bool + assert_nil m.bool_as_value + m.string_as_value = nil + assert_nil m.string + assert_nil m.string_as_value + m.bytes_as_value = nil + assert_nil m.bytes + assert_nil m.bytes_as_value + end + + def test_wrapper_setters + m = proto_module::Wrapper.new + + m.double = Google::Protobuf::DoubleValue.new(value: 4.8) + assert_equal 4.8, m.double_as_value + assert_equal Google::Protobuf::DoubleValue.new(value: 4.8), m.double + m.float = Google::Protobuf::FloatValue.new(value: 2.4) + assert_in_delta 2.4, m.float_as_value + assert_in_delta Google::Protobuf::FloatValue.new(value: 2.4).value, m.float.value + m.int32 = Google::Protobuf::Int32Value.new(value: 5) + assert_equal 5, m.int32_as_value + assert_equal Google::Protobuf::Int32Value.new(value: 5), m.int32 + m.int64 = Google::Protobuf::Int64Value.new(value: 15) + assert_equal 15, m.int64_as_value + assert_equal Google::Protobuf::Int64Value.new(value: 15), m.int64 + m.uint32 = Google::Protobuf::UInt32Value.new(value: 50) + assert_equal 50, m.uint32_as_value + assert_equal Google::Protobuf::UInt32Value.new(value: 50), m.uint32 + m.uint64 = Google::Protobuf::UInt64Value.new(value: 500) + assert_equal 500, m.uint64_as_value + assert_equal Google::Protobuf::UInt64Value.new(value: 500), m.uint64 + m.bool = Google::Protobuf::BoolValue.new(value: false) + assert_equal false, m.bool_as_value + assert_equal Google::Protobuf::BoolValue.new(value: false), m.bool + m.string = Google::Protobuf::StringValue.new(value: 'xy') + assert_equal 'xy', m.string_as_value + assert_equal Google::Protobuf::StringValue.new(value: 'xy'), m.string + m.bytes = Google::Protobuf::BytesValue.new(value: '123') + assert_equal '123', m.bytes_as_value + assert_equal Google::Protobuf::BytesValue.new(value: '123'), m.bytes + + m.double = nil + assert_nil m.double + assert_nil m.double_as_value + m.float = nil + assert_nil m.float + assert_nil m.float_as_value + m.int32 = nil + assert_nil m.int32 + assert_nil m.int32_as_value + m.int64 = nil + assert_nil m.int64 + assert_nil m.int64_as_value + m.uint32 = nil + assert_nil m.uint32 + assert_nil m.uint32_as_value + m.uint64 = nil + assert_nil m.uint64 + assert_nil m.uint64_as_value + m.bool = nil + assert_nil m.bool + assert_nil m.bool_as_value + m.string = nil + assert_nil m.string + assert_nil m.string_as_value + m.bytes = nil + assert_nil m.bytes + assert_nil m.bytes_as_value + end + + def test_wrappers_only + m = proto_module::Wrapper.new(real_string: 'hi', oneof_string: 'there') + + assert_raise(NoMethodError) { m.real_string_as_value } + assert_raise(NoMethodError) { m.as_value } + assert_raise(NoMethodError) { m._as_value } + assert_raise(NoMethodError) { m.oneof_string_as_value } + + m = proto_module::Wrapper.new + m.string_as_value = 'you' + assert_equal 'you', m.string.value + assert_equal 'you', m.string_as_value + assert_raise(NoMethodError) { m.string_ } + assert_raise(NoMethodError) { m.string_X } + assert_raise(NoMethodError) { m.string_XX } + assert_raise(NoMethodError) { m.string_XXX } + assert_raise(NoMethodError) { m.string_XXXX } + assert_raise(NoMethodError) { m.string_XXXXX } + assert_raise(NoMethodError) { m.string_XXXXXX } + assert_raise(NoMethodError) { m.string_XXXXXXX } + assert_raise(NoMethodError) { m.string_XXXXXXXX } + assert_raise(NoMethodError) { m.string_XXXXXXXXX } + assert_raise(NoMethodError) { m.string_XXXXXXXXXX } + end + + def test_converts_time + m = proto_module::TimeMessage.new + + m.timestamp = Google::Protobuf::Timestamp.new(seconds: 5, nanos: 6) + assert_kind_of Google::Protobuf::Timestamp, m.timestamp + assert_equal 5, m.timestamp.seconds + assert_equal 6, m.timestamp.nanos + + m.timestamp = Time.at(9466, 123456.789) + assert_equal Google::Protobuf::Timestamp.new(seconds: 9466, nanos: 123456789), m.timestamp + + m = proto_module::TimeMessage.new(timestamp: Time.at(1)) + assert_equal Google::Protobuf::Timestamp.new(seconds: 1, nanos: 0), m.timestamp + + assert_raise(Google::Protobuf::TypeError) { m.timestamp = 2 } + assert_raise(Google::Protobuf::TypeError) { m.timestamp = 2.4 } + assert_raise(Google::Protobuf::TypeError) { m.timestamp = '4' } + assert_raise(Google::Protobuf::TypeError) { m.timestamp = proto_module::TimeMessage.new } + end + + def test_converts_duration + m = proto_module::TimeMessage.new + + m.duration = Google::Protobuf::Duration.new(seconds: 2, nanos: 22) + assert_kind_of Google::Protobuf::Duration, m.duration + assert_equal 2, m.duration.seconds + assert_equal 22, m.duration.nanos + + m.duration = 10.5 + assert_equal Google::Protobuf::Duration.new(seconds: 10, nanos: 500_000_000), m.duration + + m.duration = 200 + assert_equal Google::Protobuf::Duration.new(seconds: 200, nanos: 0), m.duration + + m.duration = Rational(3, 2) + assert_equal Google::Protobuf::Duration.new(seconds: 1, nanos: 500_000_000), m.duration + + m.duration = BigDecimal.new("5") + assert_equal Google::Protobuf::Duration.new(seconds: 5, nanos: 0), m.duration + + m = proto_module::TimeMessage.new(duration: 1.1) + assert_equal Google::Protobuf::Duration.new(seconds: 1, nanos: 100_000_000), m.duration + + assert_raise(Google::Protobuf::TypeError) { m.duration = '2' } + assert_raise(Google::Protobuf::TypeError) { m.duration = proto_module::TimeMessage.new } + end + + def test_freeze + m = proto_module::TestMessage.new + m.optional_int32 = 10 + m.freeze + + frozen_error = assert_raise(FrozenErrorType) { m.optional_int32 = 20 } + assert_equal "can't modify frozen #{proto_module}::TestMessage", frozen_error.message + assert_equal 10, m.optional_int32 + assert_equal true, m.frozen? + + assert_raise(FrozenErrorType) { m.optional_int64 = 2 } + assert_raise(FrozenErrorType) { m.optional_uint32 = 3 } + assert_raise(FrozenErrorType) { m.optional_uint64 = 4 } + assert_raise(FrozenErrorType) { m.optional_bool = true } + assert_raise(FrozenErrorType) { m.optional_float = 6.0 } + assert_raise(FrozenErrorType) { m.optional_double = 7.0 } + assert_raise(FrozenErrorType) { m.optional_string = '8' } + assert_raise(FrozenErrorType) { m.optional_bytes = nil } + assert_raise(FrozenErrorType) { m.optional_msg = proto_module::TestMessage2.new } + assert_raise(FrozenErrorType) { m.optional_enum = :A } + assert_raise(FrozenErrorType) { m.repeated_int32 = 1 } + assert_raise(FrozenErrorType) { m.repeated_int64 = 2 } + assert_raise(FrozenErrorType) { m.repeated_uint32 = 3 } + assert_raise(FrozenErrorType) { m.repeated_uint64 = 4 } + assert_raise(FrozenErrorType) { m.repeated_bool = true } + assert_raise(FrozenErrorType) { m.repeated_float = 6.0 } + assert_raise(FrozenErrorType) { m.repeated_double = 7.0 } + assert_raise(FrozenErrorType) { m.repeated_string = '8' } + assert_raise(FrozenErrorType) { m.repeated_bytes = nil } + assert_raise(FrozenErrorType) { m.repeated_msg = proto_module::TestMessage2.new } + assert_raise(FrozenErrorType) { m.repeated_enum = :A } + end + + def test_eq + m1 = proto_module::TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + m2 = proto_module::TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + + h = {} + h[m1] = :yes + + assert m1 == m2 + assert m1.eql?(m2) + assert m1.hash == m2.hash + assert h[m1] == :yes + assert h[m2] == :yes + + m1.optional_int32 = 2 + + assert m1 != m2 + assert !m1.eql?(m2) + assert m1.hash != m2.hash + assert_nil h[m2] + end +end diff --git a/ruby/tests/encode_decode_test.rb b/ruby/tests/encode_decode_test.rb index f8b991cdb8..d3cebab215 100644 --- a/ruby/tests/encode_decode_test.rb +++ b/ruby/tests/encode_decode_test.rb @@ -22,8 +22,8 @@ class EncodeDecodeTest < Test::Unit::TestCase # Test discard unknown for singular message field. unknown_msg = A::B::C::TestUnknown.new( - :optional_unknown => - A::B::C::TestUnknown.new(:unknown_field => 1)) + :optional_unknown => + A::B::C::TestUnknown.new(:unknown_field => 1)) from = A::B::C::TestUnknown.encode(unknown_msg) m = A::B::C::TestMessage.decode(from) Google::Protobuf.discard_unknown(m) @@ -32,8 +32,8 @@ class EncodeDecodeTest < Test::Unit::TestCase # Test discard unknown for repeated message field. unknown_msg = A::B::C::TestUnknown.new( - :repeated_unknown => - [A::B::C::TestUnknown.new(:unknown_field => 1)]) + :repeated_unknown => + [A::B::C::TestUnknown.new(:unknown_field => 1)]) from = A::B::C::TestUnknown.encode(unknown_msg) m = A::B::C::TestMessage.decode(from) Google::Protobuf.discard_unknown(m) @@ -42,8 +42,8 @@ class EncodeDecodeTest < Test::Unit::TestCase # Test discard unknown for map value message field. unknown_msg = A::B::C::TestUnknown.new( - :map_unknown => - {"" => A::B::C::TestUnknown.new(:unknown_field => 1)}) + :map_unknown => + {"" => A::B::C::TestUnknown.new(:unknown_field => 1)}) from = A::B::C::TestUnknown.encode(unknown_msg) m = A::B::C::TestMessage.decode(from) Google::Protobuf.discard_unknown(m) @@ -52,8 +52,8 @@ class EncodeDecodeTest < Test::Unit::TestCase # Test discard unknown for oneof message field. unknown_msg = A::B::C::TestUnknown.new( - :oneof_unknown => - A::B::C::TestUnknown.new(:unknown_field => 1)) + :oneof_unknown => + A::B::C::TestUnknown.new(:unknown_field => 1)) from = A::B::C::TestUnknown.encode(unknown_msg) m = A::B::C::TestMessage.decode(from) Google::Protobuf.discard_unknown(m) @@ -86,7 +86,7 @@ class EncodeDecodeTest < Test::Unit::TestCase end def test_encode_wrong_msg - e = assert_raise ::ArgumentError do + assert_raise ::ArgumentError do m = A::B::C::TestMessage.new( :optional_int32 => 1, ) diff --git a/ruby/tests/gc_test.rb b/ruby/tests/gc_test.rb index f3470ccaa5..55b96289e8 100644 --- a/ruby/tests/gc_test.rb +++ b/ruby/tests/gc_test.rb @@ -6,12 +6,13 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) old_gc = GC.stress GC.stress = 0x01 | 0x04 require 'generated_code_pb' +require 'generated_code_proto2_pb' GC.stress = old_gc require 'test/unit' class GCTest < Test::Unit::TestCase - def get_msg + def get_msg_proto3 A::B::C::TestMessage.new( :optional_int32 => 1, :optional_int64 => 1, @@ -46,12 +47,55 @@ class GCTest < Test::Unit::TestCase :map_string_bool => {"a" => true}, ) end + + def get_msg_proto2 + A::B::Proto2::TestMessage.new( + :optional_int32 => 1, + :optional_int64 => 1, + :optional_uint32 => 1, + :optional_uint64 => 1, + :optional_bool => true, + :optional_double => 1.0, + :optional_float => 1.0, + :optional_string => "a", + :optional_bytes => "b", + :optional_enum => A::B::Proto2::TestEnum::A, + :optional_msg => A::B::Proto2::TestMessage.new(), + :repeated_int32 => [1], + :repeated_int64 => [1], + :repeated_uint32 => [1], + :repeated_uint64 => [1], + :repeated_bool => [true], + :repeated_double => [1.0], + :repeated_float => [1.0], + :repeated_string => ["a"], + :repeated_bytes => ["b"], + :repeated_enum => [A::B::Proto2::TestEnum::A], + :repeated_msg => [A::B::Proto2::TestMessage.new()], + :required_int32 => 1, + :required_int64 => 1, + :required_uint32 => 1, + :required_uint64 => 1, + :required_bool => true, + :required_double => 1.0, + :required_float => 1.0, + :required_string => "a", + :required_bytes => "b", + :required_enum => A::B::Proto2::TestEnum::A, + :required_msg => A::B::Proto2::TestMessage.new(), + ) + end + def test_generated_msg old_gc = GC.stress GC.stress = 0x01 | 0x04 - from = get_msg + from = get_msg_proto3 data = A::B::C::TestMessage.encode(from) to = A::B::C::TestMessage.decode(data) + + from = get_msg_proto2 + data = A::B::Proto2::TestMessage.encode(from) + to = A::B::Proto2::TestMessage.decode(data) GC.stress = old_gc puts "passed" end diff --git a/ruby/tests/generated_code.proto b/ruby/tests/generated_code.proto index 3b934bd691..e8116697d7 100644 --- a/ruby/tests/generated_code.proto +++ b/ruby/tests/generated_code.proto @@ -69,6 +69,11 @@ enum TestEnum { C = 3; } +message testLowercaseNested { + message lowercase{} +} + + message TestUnknown { TestUnknown optional_unknown = 11; repeated TestUnknown repeated_unknown = 31; diff --git a/ruby/tests/generated_code_proto2.proto b/ruby/tests/generated_code_proto2.proto new file mode 100644 index 0000000000..1e957219fa --- /dev/null +++ b/ruby/tests/generated_code_proto2.proto @@ -0,0 +1,80 @@ +syntax = "proto2"; + +package a.b.proto2; + +message TestMessage { + optional int32 optional_int32 = 1; + optional int64 optional_int64 = 2; + optional uint32 optional_uint32 = 3; + optional uint64 optional_uint64 = 4; + optional bool optional_bool = 5; + optional double optional_double = 6; + optional float optional_float = 7; + optional string optional_string = 8; + optional bytes optional_bytes = 9; + optional TestEnum optional_enum = 10; + optional TestMessage optional_msg = 11; + + repeated int32 repeated_int32 = 21; + repeated int64 repeated_int64 = 22; + repeated uint32 repeated_uint32 = 23; + repeated uint64 repeated_uint64 = 24; + repeated bool repeated_bool = 25; + repeated double repeated_double = 26; + repeated float repeated_float = 27; + repeated string repeated_string = 28; + repeated bytes repeated_bytes = 29; + repeated TestEnum repeated_enum = 30; + repeated TestMessage repeated_msg = 31; + + required int32 required_int32 = 41; + required int64 required_int64 = 42; + required uint32 required_uint32 = 43; + required uint64 required_uint64 = 44; + required bool required_bool = 45; + required double required_double = 46; + required float required_float = 47; + required string required_string = 48; + required bytes required_bytes = 49; + required TestEnum required_enum = 50; + required TestMessage required_msg = 51; + + oneof my_oneof { + int32 oneof_int32 = 61; + int64 oneof_int64 = 62; + uint32 oneof_uint32 = 63; + uint64 oneof_uint64 = 64; + bool oneof_bool = 65; + double oneof_double = 66; + float oneof_float = 67; + string oneof_string = 68; + bytes oneof_bytes = 69; + TestEnum oneof_enum = 70; + TestMessage oneof_msg = 71; + } + + message NestedMessage { + optional int32 foo = 1; + } + + optional NestedMessage nested_message = 80; + + // Reserved for non-existing field test. + // int32 non_exist = 89; +} + +enum TestEnum { + Default = 0; + A = 1; + B = 2; + C = 3; +} + +message TestUnknown { + optional TestUnknown optional_unknown = 11; + repeated TestUnknown repeated_unknown = 31; + oneof my_oneof { + TestUnknown oneof_unknown = 51; + } + optional int32 unknown_field = 89; +} diff --git a/ruby/tests/generated_code_proto2_test.rb b/ruby/tests/generated_code_proto2_test.rb new file mode 100644 index 0000000000..302f945155 --- /dev/null +++ b/ruby/tests/generated_code_proto2_test.rb @@ -0,0 +1,21 @@ +#!/usr/bin/ruby + +# generated_code.rb is in the same directory as this test. +$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) + +require 'generated_code_proto2_pb' +require 'test_import_proto2_pb' +require 'test_ruby_package_proto2_pb' +require 'test/unit' + +class GeneratedCodeProto2Test < Test::Unit::TestCase + def test_generated_msg + # just test that we can instantiate the message. The purpose of this test + # is to ensure that the output of the code generator is valid Ruby and + # successfully creates message definitions and classes, not to test every + # aspect of the extension (basic.rb is for that). + A::B::Proto2::TestMessage.new + FooBar::Proto2::TestImportedMessage.new + A::B::Proto2::TestRubyPackageMessage.new + end +end diff --git a/ruby/tests/generated_code_test.rb b/ruby/tests/generated_code_test.rb index 431d681bb3..aed1cf8ebc 100644 --- a/ruby/tests/generated_code_test.rb +++ b/ruby/tests/generated_code_test.rb @@ -14,8 +14,10 @@ class GeneratedCodeTest < Test::Unit::TestCase # is to ensure that the output of the code generator is valid Ruby and # successfully creates message definitions and classes, not to test every # aspect of the extension (basic.rb is for that). - m = A::B::C::TestMessage.new() - m2 = FooBar::TestImportedMessage.new() - m3 = A::B::TestRubyPackageMessage.new() + A::B::C::TestMessage.new + A::B::C::TestMessage::NestedMessage.new + A::B::C::TestLowercaseNested::Lowercase.new + FooBar::TestImportedMessage.new + A::B::TestRubyPackageMessage.new end end diff --git a/ruby/tests/repeated_field_test.rb b/ruby/tests/repeated_field_test.rb index a4f3aab815..ced9de8381 100644 --- a/ruby/tests/repeated_field_test.rb +++ b/ruby/tests/repeated_field_test.rb @@ -19,6 +19,7 @@ class RepeatedFieldTest < Test::Unit::TestCase arr_methods -= [ :indices, :iter_for_each, :iter_for_each_index, :iter_for_each_with_index, :dimensions, :copy_data, :copy_data_simple, :nitems, :iter_for_reverse_each, :indexes, :append, :prepend] + arr_methods -= [:union, :difference, :filter!] arr_methods.each do |method_name| assert m.repeated_string.respond_to?(method_name) == true, "does not respond to #{method_name}" end @@ -28,7 +29,10 @@ class RepeatedFieldTest < Test::Unit::TestCase m = TestMessage.new repeated_field_names(TestMessage).each do |field_name| assert_nil m.send(field_name).first + assert_equal [], m.send(field_name).first(0) + assert_equal [], m.send(field_name).first(1) end + fill_test_msg(m) assert_equal -10, m.repeated_int32.first assert_equal -1_000_000, m.repeated_int64.first @@ -41,6 +45,11 @@ class RepeatedFieldTest < Test::Unit::TestCase assert_equal "bar".encode!('ASCII-8BIT'), m.repeated_bytes.first assert_equal TestMessage2.new(:foo => 1), m.repeated_msg.first assert_equal :A, m.repeated_enum.first + + assert_equal [], m.repeated_int32.first(0) + assert_equal [-10], m.repeated_int32.first(1) + assert_equal [-10, -11], m.repeated_int32.first(2) + assert_equal [-10, -11], m.repeated_int32.first(3) end @@ -212,7 +221,7 @@ class RepeatedFieldTest < Test::Unit::TestCase def test_push m = TestMessage.new - reference_arr = %w(foo bar baz) + reference_arr = %w[foo bar baz] m.repeated_string += reference_arr.clone check_self_modifying_method(m.repeated_string, reference_arr) do |arr| @@ -221,10 +230,9 @@ class RepeatedFieldTest < Test::Unit::TestCase check_self_modifying_method(m.repeated_string, reference_arr) do |arr| arr << 'fizz' end - #TODO: push should support multiple - # check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - # arr.push('fizz', 'buzz') - # end + check_self_modifying_method(m.repeated_string, reference_arr) do |arr| + arr.push('fizz', 'buzz') + end end def test_clear diff --git a/ruby/tests/test_import_proto2.proto b/ruby/tests/test_import_proto2.proto new file mode 100644 index 0000000000..8d2ef375ef --- /dev/null +++ b/ruby/tests/test_import_proto2.proto @@ -0,0 +1,5 @@ +syntax = "proto2"; + +package foo_bar.proto2; + +message TestImportedMessage {} diff --git a/ruby/tests/test_ruby_package.proto b/ruby/tests/test_ruby_package.proto index b872562028..54b7aca50a 100644 --- a/ruby/tests/test_ruby_package.proto +++ b/ruby/tests/test_ruby_package.proto @@ -2,6 +2,6 @@ syntax = "proto3"; package foo_bar; -option ruby_package = "A.B"; +option ruby_package = "A::B"; message TestRubyPackageMessage {} diff --git a/ruby/tests/test_ruby_package_proto2.proto b/ruby/tests/test_ruby_package_proto2.proto new file mode 100644 index 0000000000..c55bde435b --- /dev/null +++ b/ruby/tests/test_ruby_package_proto2.proto @@ -0,0 +1,7 @@ +syntax = "proto2"; + +package foo_bar_proto2; + +option ruby_package = "A::B::Proto2"; + +message TestRubyPackageMessage {} diff --git a/ruby/tests/type_errors.rb b/ruby/tests/type_errors.rb new file mode 100644 index 0000000000..6f6eb06178 --- /dev/null +++ b/ruby/tests/type_errors.rb @@ -0,0 +1,176 @@ +#!/usr/bin/ruby + +# generated_code.rb is in the same directory as this test. +$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) + +require 'test/unit' +require 'google/protobuf/well_known_types' +require 'generated_code_pb' + +class TestTypeErrors < Test::Unit::TestCase + # Ruby 2.4 unified Fixnum with Integer + IntegerType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4') ? Fixnum : Integer + + def test_bad_string + check_error Google::Protobuf::TypeError, + "Invalid argument for string field 'optional_string' (given #{IntegerType.name})." do + A::B::C::TestMessage.new(optional_string: 4) + end + check_error Google::Protobuf::TypeError, + "Invalid argument for string field 'oneof_string' (given #{IntegerType.name})." do + A::B::C::TestMessage.new(oneof_string: 4) + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_string' (given String)." do + A::B::C::TestMessage.new(repeated_string: '4') + end + end + + def test_bad_float + check_error Google::Protobuf::TypeError, + "Expected number type for float field 'optional_float' (given TrueClass)." do + A::B::C::TestMessage.new(optional_float: true) + end + check_error Google::Protobuf::TypeError, + "Expected number type for float field 'oneof_float' (given TrueClass)." do + A::B::C::TestMessage.new(oneof_float: true) + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_float' (given String)." do + A::B::C::TestMessage.new(repeated_float: 'true') + end + end + + def test_bad_double + check_error Google::Protobuf::TypeError, + "Expected number type for double field 'optional_double' (given Symbol)." do + A::B::C::TestMessage.new(optional_double: :double) + end + check_error Google::Protobuf::TypeError, + "Expected number type for double field 'oneof_double' (given Symbol)." do + A::B::C::TestMessage.new(oneof_double: :double) + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_double' (given FalseClass)." do + A::B::C::TestMessage.new(repeated_double: false) + end + end + + def test_bad_bool + check_error Google::Protobuf::TypeError, + "Invalid argument for boolean field 'optional_bool' (given Float)." do + A::B::C::TestMessage.new(optional_bool: 4.4) + end + check_error Google::Protobuf::TypeError, + "Invalid argument for boolean field 'oneof_bool' (given Float)." do + A::B::C::TestMessage.new(oneof_bool: 4.4) + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_bool' (given String)." do + A::B::C::TestMessage.new(repeated_bool: 'hi') + end + end + + def test_bad_int + check_error Google::Protobuf::TypeError, + "Expected number type for integral field 'optional_int32' (given String)." do + A::B::C::TestMessage.new(optional_int32: 'hi') + end + check_error RangeError, + "Non-integral floating point value assigned to integer field 'optional_int64' (given Float)." do + A::B::C::TestMessage.new(optional_int64: 2.4) + end + check_error Google::Protobuf::TypeError, + "Expected number type for integral field 'optional_uint32' (given Symbol)." do + A::B::C::TestMessage.new(optional_uint32: :thing) + end + check_error Google::Protobuf::TypeError, + "Expected number type for integral field 'optional_uint64' (given FalseClass)." do + A::B::C::TestMessage.new(optional_uint64: false) + end + check_error Google::Protobuf::TypeError, + "Expected number type for integral field 'oneof_int32' (given Symbol)." do + A::B::C::TestMessage.new(oneof_int32: :hi) + end + check_error RangeError, + "Non-integral floating point value assigned to integer field 'oneof_int64' (given Float)." do + A::B::C::TestMessage.new(oneof_int64: 2.4) + end + check_error Google::Protobuf::TypeError, + "Expected number type for integral field 'oneof_uint32' (given String)." do + A::B::C::TestMessage.new(oneof_uint32: 'x') + end + check_error RangeError, + "Non-integral floating point value assigned to integer field 'oneof_uint64' (given Float)." do + A::B::C::TestMessage.new(oneof_uint64: 1.1) + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_int32' (given Symbol)." do + A::B::C::TestMessage.new(repeated_int32: :hi) + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_int64' (given Float)." do + A::B::C::TestMessage.new(repeated_int64: 2.4) + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_uint32' (given String)." do + A::B::C::TestMessage.new(repeated_uint32: 'x') + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_uint64' (given Float)." do + A::B::C::TestMessage.new(repeated_uint64: 1.1) + end + end + + def test_bad_enum + check_error RangeError, + "Unknown symbol value for enum field 'optional_enum'." do + A::B::C::TestMessage.new(optional_enum: 'enum') + end + check_error RangeError, + "Unknown symbol value for enum field 'oneof_enum'." do + A::B::C::TestMessage.new(oneof_enum: '') + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_enum' (given String)." do + A::B::C::TestMessage.new(repeated_enum: '') + end + end + + def test_bad_bytes + check_error Google::Protobuf::TypeError, + "Invalid argument for bytes field 'optional_bytes' (given Float)." do + A::B::C::TestMessage.new(optional_bytes: 22.22) + end + check_error Google::Protobuf::TypeError, + "Invalid argument for bytes field 'oneof_bytes' (given Symbol)." do + A::B::C::TestMessage.new(oneof_bytes: :T22) + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_bytes' (given Symbol)." do + A::B::C::TestMessage.new(repeated_bytes: :T22) + end + end + + def test_bad_msg + check_error Google::Protobuf::TypeError, + "Invalid type #{IntegerType.name} to assign to submessage field 'optional_msg'." do + A::B::C::TestMessage.new(optional_msg: 2) + end + check_error Google::Protobuf::TypeError, + "Invalid type String to assign to submessage field 'oneof_msg'." do + A::B::C::TestMessage.new(oneof_msg: '2') + end + check_error ArgumentError, + "Expected array as initializer value for repeated field 'repeated_msg' (given String)." do + A::B::C::TestMessage.new(repeated_msg: '2') + end + end + + def check_error(type, message) + err = assert_raises type do + yield + end + assert_equal message, err.message + end +end diff --git a/ruby/travis-test.sh b/ruby/travis-test.sh index cbe7cd983c..cb53338f72 100755 --- a/ruby/travis-test.sh +++ b/ruby/travis-test.sh @@ -5,6 +5,17 @@ set -e test_version() { version=$1 + + # TODO(teboring): timestamp parsing is incorrect only on mac due to mktime. + if [[ $(uname -s) == Linux ]] + then + RUBY_CONFORMANCE=test_ruby + elif [[ $(uname -s) == Darwin ]] + then + # TODO(teboring): timestamp parsing is incorrect only on mac due to mktime. + RUBY_CONFORMANCE=test_ruby_mac + fi + if [ "$version" == "jruby-1.7" ] ; then # No conformance tests yet -- JRuby is too broken to run them. bash --login -c \ @@ -13,15 +24,29 @@ test_version() { git clean -f && \ gem install bundler && bundle && \ rake test" - else + elif [ "$version" == "ruby-2.6.0" ] ; then bash --login -c \ "rvm install $version && rvm use $version && \ which ruby && \ git clean -f && \ - gem install bundler && bundle && \ + gem install bundler -v 1.17.3 && bundle && \ + rake test && + rake gc_test && + cd ../conformance && make ${RUBY_CONFORMANCE} && + cd ../ruby/compatibility_tests/v3.0.0 && + cp -R ../../lib lib && ./test.sh" + else + # Recent versions of OSX have deprecated OpenSSL, so we have to explicitly + # provide a path to the OpenSSL directory installed via Homebrew. + bash --login -c \ + "rvm install $version --with-openssl-dir=`brew --prefix openssl` && \ + rvm use $version && \ + which ruby && \ + git clean -f && \ + gem install bundler -v 1.17.3 && bundle && \ rake test && rake gc_test && - cd ../conformance && make test_ruby && + cd ../conformance && make ${RUBY_CONFORMANCE} && cd ../ruby/compatibility_tests/v3.0.0 && ./test.sh" fi } diff --git a/src/Makefile.am b/src/Makefile.am index 1aabf97207..dc5070389d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,6 +18,8 @@ else PTHREAD_DEF = endif +PROTOBUF_VERSION = 20:0:0 + if GCC # Turn on all warnings except for sign comparison (we ignore sign comparison # in Google so our code base have tons of such warnings). @@ -70,14 +72,15 @@ nobase_include_HEADERS = \ google/protobuf/stubs/hash.h \ google/protobuf/stubs/logging.h \ google/protobuf/stubs/macros.h \ + google/protobuf/stubs/map_util.h \ google/protobuf/stubs/mutex.h \ google/protobuf/stubs/once.h \ google/protobuf/stubs/platform_macros.h \ google/protobuf/stubs/port.h \ - google/protobuf/stubs/singleton.h \ google/protobuf/stubs/status.h \ google/protobuf/stubs/stl_util.h \ google/protobuf/stubs/stringpiece.h \ + google/protobuf/stubs/strutil.h \ google/protobuf/stubs/template_util.h \ google/protobuf/any.pb.h \ google/protobuf/api.pb.h \ @@ -92,6 +95,7 @@ nobase_include_HEADERS = \ google/protobuf/dynamic_message.h \ google/protobuf/empty.pb.h \ google/protobuf/extension_set.h \ + google/protobuf/extension_set_inl.h \ google/protobuf/field_mask.pb.h \ google/protobuf/generated_enum_reflection.h \ google/protobuf/generated_enum_util.h \ @@ -112,6 +116,7 @@ nobase_include_HEADERS = \ google/protobuf/message_lite.h \ google/protobuf/metadata.h \ google/protobuf/metadata_lite.h \ + google/protobuf/parse_context.h \ google/protobuf/port.h \ google/protobuf/port_def.inc \ google/protobuf/port_undef.inc \ @@ -127,7 +132,6 @@ nobase_include_HEADERS = \ google/protobuf/unknown_field_set.h \ google/protobuf/wire_format.h \ google/protobuf/wire_format_lite.h \ - google/protobuf/wire_format_lite_inl.h \ google/protobuf/wrappers.pb.h \ google/protobuf/io/coded_stream.h \ $(GZHEADERS) \ @@ -166,8 +170,8 @@ nobase_include_HEADERS = \ lib_LTLIBRARIES = libprotobuf-lite.la libprotobuf.la libprotoc.la -libprotobuf_lite_la_LIBADD = $(PTHREAD_LIBS) -libprotobuf_lite_la_LDFLAGS = -version-info 17:0:0 -export-dynamic -no-undefined +libprotobuf_lite_la_LIBADD = $(PTHREAD_LIBS) $(LIBATOMIC_LIBS) +libprotobuf_lite_la_LDFLAGS = -version-info $(PROTOBUF_VERSION) -export-dynamic -no-undefined if HAVE_LD_VERSION_SCRIPT libprotobuf_lite_la_LDFLAGS += -Wl,--version-script=$(srcdir)/libprotobuf-lite.map EXTRA_libprotobuf_lite_la_DEPENDENCIES = libprotobuf-lite.map @@ -179,8 +183,8 @@ libprotobuf_lite_la_SOURCES = \ google/protobuf/stubs/hash.h \ google/protobuf/stubs/int128.cc \ google/protobuf/stubs/int128.h \ - google/protobuf/stubs/io_win32.cc \ - google/protobuf/stubs/io_win32.h \ + google/protobuf/io/io_win32.cc \ + google/protobuf/io/io_win32.h \ google/protobuf/stubs/map_util.h \ google/protobuf/stubs/mathutil.h \ google/protobuf/stubs/status.cc \ @@ -194,26 +198,29 @@ libprotobuf_lite_la_SOURCES = \ google/protobuf/stubs/stringprintf.h \ google/protobuf/stubs/structurally_valid.cc \ google/protobuf/stubs/strutil.cc \ - google/protobuf/stubs/strutil.h \ google/protobuf/stubs/time.cc \ google/protobuf/stubs/time.h \ + google/protobuf/any_lite.cc \ google/protobuf/arena.cc \ - google/protobuf/arenastring.cc \ google/protobuf/extension_set.cc \ + google/protobuf/generated_enum_util.cc \ google/protobuf/generated_message_util.cc \ google/protobuf/generated_message_table_driven_lite.h \ google/protobuf/generated_message_table_driven_lite.cc \ google/protobuf/implicit_weak_message.cc \ google/protobuf/message_lite.cc \ + google/protobuf/parse_context.cc \ google/protobuf/repeated_field.cc \ google/protobuf/wire_format_lite.cc \ google/protobuf/io/coded_stream.cc \ google/protobuf/io/coded_stream_inl.h \ + google/protobuf/io/strtod.cc \ google/protobuf/io/zero_copy_stream.cc \ + google/protobuf/io/zero_copy_stream_impl.cc \ google/protobuf/io/zero_copy_stream_impl_lite.cc -libprotobuf_la_LIBADD = $(PTHREAD_LIBS) -libprotobuf_la_LDFLAGS = -version-info 17:0:0 -export-dynamic -no-undefined +libprotobuf_la_LIBADD = $(PTHREAD_LIBS) $(LIBATOMIC_LIBS) +libprotobuf_la_LDFLAGS = -version-info $(PROTOBUF_VERSION) -export-dynamic -no-undefined if HAVE_LD_VERSION_SCRIPT libprotobuf_la_LDFLAGS += -Wl,--version-script=$(srcdir)/libprotobuf.map EXTRA_libprotobuf_la_DEPENDENCIES = libprotobuf.map @@ -253,9 +260,7 @@ libprotobuf_la_SOURCES = \ google/protobuf/wrappers.pb.cc \ google/protobuf/io/gzip_stream.cc \ google/protobuf/io/printer.cc \ - google/protobuf/io/strtod.cc \ google/protobuf/io/tokenizer.cc \ - google/protobuf/io/zero_copy_stream_impl.cc \ google/protobuf/compiler/importer.cc \ google/protobuf/compiler/parser.cc \ google/protobuf/util/delimited_message_util.cc \ @@ -304,7 +309,7 @@ libprotobuf_la_SOURCES = \ nodist_libprotobuf_la_SOURCES = $(nodist_libprotobuf_lite_la_SOURCES) libprotoc_la_LIBADD = $(PTHREAD_LIBS) libprotobuf.la -libprotoc_la_LDFLAGS = -version-info 17:0:0 -export-dynamic -no-undefined +libprotoc_la_LDFLAGS = -version-info $(PROTOBUF_VERSION) -export-dynamic -no-undefined if HAVE_LD_VERSION_SCRIPT libprotoc_la_LDFLAGS += -Wl,--version-script=$(srcdir)/libprotoc.map EXTRA_libprotoc_la_DEPENDENCIES = libprotoc.map @@ -314,7 +319,6 @@ libprotoc_la_SOURCES = \ google/protobuf/compiler/command_line_interface.cc \ google/protobuf/compiler/plugin.cc \ google/protobuf/compiler/plugin.pb.cc \ - google/protobuf/compiler/scc.cc \ google/protobuf/compiler/scc.h \ google/protobuf/compiler/subprocess.cc \ google/protobuf/compiler/subprocess.h \ @@ -426,6 +430,7 @@ libprotoc_la_SOURCES = \ google/protobuf/compiler/objectivec/objectivec_message.h \ google/protobuf/compiler/objectivec/objectivec_message_field.cc \ google/protobuf/compiler/objectivec/objectivec_message_field.h \ + google/protobuf/compiler/objectivec/objectivec_nsobject_methods.h \ google/protobuf/compiler/objectivec/objectivec_oneof.cc \ google/protobuf/compiler/objectivec/objectivec_oneof.h \ google/protobuf/compiler/objectivec/objectivec_primitive_field.cc \ @@ -527,7 +532,6 @@ protoc_inputs = \ EXTRA_DIST = \ $(protoc_inputs) \ - $(js_well_known_types_sources) \ solaris/libstdc++.la \ google/protobuf/test_messages_proto3.proto \ google/protobuf/test_messages_proto2.proto \ @@ -551,6 +555,14 @@ EXTRA_DIST = \ google/protobuf/util/package_info.h \ google/protobuf/compiler/ruby/ruby_generated_code.proto \ google/protobuf/compiler/ruby/ruby_generated_code_pb.rb \ + google/protobuf/compiler/ruby/ruby_generated_code_proto2.proto \ + google/protobuf/compiler/ruby/ruby_generated_code_proto2_pb.rb \ + google/protobuf/compiler/ruby/ruby_generated_pkg_explicit.proto \ + google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_legacy.proto \ + google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_legacy_pb.rb \ + google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_pb.rb \ + google/protobuf/compiler/ruby/ruby_generated_pkg_implicit.proto \ + google/protobuf/compiler/ruby/ruby_generated_pkg_implicit_pb.rb \ google/protobuf/compiler/package_info.h \ google/protobuf/compiler/zip_output_unittest.sh \ libprotobuf-lite.map \ @@ -687,7 +699,7 @@ $(protoc_outputs): unittest_proto_middleman COMMON_TEST_SOURCES = \ google/protobuf/arena_test_util.cc \ google/protobuf/arena_test_util.h \ - google/protobuf/map_test_util.cc \ + google/protobuf/map_test_util.inc \ google/protobuf/map_test_util.h \ google/protobuf/map_test_util_impl.h \ google/protobuf/test_util.cc \ @@ -720,7 +732,7 @@ protobuf_test_SOURCES = \ google/protobuf/stubs/bytestream_unittest.cc \ google/protobuf/stubs/common_unittest.cc \ google/protobuf/stubs/int128_unittest.cc \ - google/protobuf/stubs/io_win32_unittest.cc \ + google/protobuf/io/io_win32_unittest.cc \ google/protobuf/stubs/statusor_test.cc \ google/protobuf/stubs/status_test.cc \ google/protobuf/stubs/stringpiece_unittest.cc \ @@ -884,7 +896,7 @@ no_warning_test.cc: no_warning_test_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la no_warning_test_CXXFLAGS = $(PTHREAD_CFLAGS) $(PTHREAD_DEF) $(ZLIB_DEF) \ - -Wall -Wextra -Werror -Wno-unused-parameter -Og + -Wall -Wextra -Werror -Wno-unused-parameter nodist_no_warning_test_SOURCES = no_warning_test.cc $(protoc_outputs) TESTS = protobuf-test protobuf-lazy-descriptor-test protobuf-lite-test \ diff --git a/src/README.md b/src/README.md index e38e59bfb8..007deb328e 100644 --- a/src/README.md +++ b/src/README.md @@ -29,7 +29,7 @@ install them before proceeding. To get the source, download one of the release .tar.gz or .zip packages in the release page: - https://github.com/google/protobuf/releases/latest + https://github.com/protocolbuffers/protobuf/releases/latest For example: if you only need C++, download `protobuf-cpp-[VERSION].tar.gz`; if you need C++ and Java, download `protobuf-java-[VERSION].tar.gz` (every package @@ -40,20 +40,22 @@ You can also get the source by "git clone" our git repository. Make sure you have also cloned the submodules and generated the configure script (skip this if you are using a release .tar.gz or .zip package): - $ git clone https://github.com/google/protobuf.git - $ cd protobuf - $ git submodule update --init --recursive - $ ./autogen.sh - +```shell + git clone https://github.com/protocolbuffers/protobuf.git + cd protobuf + git submodule update --init --recursive + ./autogen.sh +``` To build and install the C++ Protocol Buffer runtime and the Protocol Buffer compiler (protoc) execute the following: - $ ./configure - $ make - $ make check - $ sudo make install - $ sudo ldconfig # refresh shared library cache. - +```shell + ./configure + make + make check + sudo make install + sudo ldconfig # refresh shared library cache. +``` If "make check" fails, you can still install, but it is likely that some features of this library will not work correctly on your system. Proceed at your own risk. @@ -83,10 +85,12 @@ Protocol Buffers integrates with pkg-config to manage this. If you have pkg-config installed, then you can invoke it to get a list of flags like so: + pkg-config --cflags protobuf # print compiler flags pkg-config --libs protobuf # print linker flags pkg-config --cflags --libs protobuf # print both + For example: c++ my_program.cc my_proto.pb.cc `pkg-config --cflags --libs protobuf` @@ -97,6 +101,7 @@ not pass the correct set of flags to correctly link against libprotobuf. If the package in question uses autoconf, you can often fix the problem by invoking its configure script like: + configure CXXFLAGS="$(pkg-config --cflags protobuf)" \ LIBS="$(pkg-config --libs protobuf)" @@ -118,15 +123,15 @@ of "protobuf" in these examples. For a Mac system, Unix tools are not available by default. You will first need to install Xcode from the Mac AppStore and then run the following command from a terminal: - - $ sudo xcode-select --install - +```shell + sudo xcode-select --install +```shell To install Unix tools, you can install "port" following the instructions at https://www.macports.org . This will reside in /opt/local/bin/port for most Mac installations. - - $ sudo /opt/local/bin/port install autoconf automake libtool - +```shell + sudo /opt/local/bin/port install autoconf automake libtool +``` Then follow the Unix instructions above. **Note for cross-compiling** @@ -184,7 +189,7 @@ C++ Installation - Windows If you only need the protoc binary, you can download it from the release page: - https://github.com/google/protobuf/releases/latest + https://github.com/protocolbuffers/protobuf/releases/latest In the downloads section, download the zip file protoc-$VERSION-win32.zip. It contains the protoc binary as well as public proto files of protobuf diff --git a/src/google/protobuf/any.cc b/src/google/protobuf/any.cc index b94529e685..a79214b750 100644 --- a/src/google/protobuf/any.cc +++ b/src/google/protobuf/any.cc @@ -30,92 +30,50 @@ #include +#include +#include #include +#include +#include namespace google { namespace protobuf { namespace internal { -namespace { -string GetTypeUrl(const Descriptor* message, - const string& type_url_prefix) { - if (!type_url_prefix.empty() && - type_url_prefix[type_url_prefix.size() - 1] == '/') { - return type_url_prefix + message->full_name(); - } else { - return type_url_prefix + "/" + message->full_name(); - } -} -} // namespace - -const char kAnyFullTypeName[] = "google.protobuf.Any"; -const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/"; -const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/"; - -AnyMetadata::AnyMetadata(UrlType* type_url, ValueType* value) - : type_url_(type_url), value_(value) { -} - void AnyMetadata::PackFrom(const Message& message) { PackFrom(message, kTypeGoogleApisComPrefix); } void AnyMetadata::PackFrom(const Message& message, - const string& type_url_prefix) { - type_url_->SetNoArena(&::google::protobuf::internal::GetEmptyString(), - GetTypeUrl(message.GetDescriptor(), type_url_prefix)); + const std::string& type_url_prefix) { + type_url_->SetNoArena( + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString(), + GetTypeUrl(message.GetDescriptor()->full_name(), type_url_prefix)); message.SerializeToString(value_->MutableNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited())); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited())); } bool AnyMetadata::UnpackTo(Message* message) const { - if (!InternalIs(message->GetDescriptor())) { + if (!InternalIs(message->GetDescriptor()->full_name())) { return false; } return message->ParseFromString(value_->GetNoArena()); } -bool AnyMetadata::InternalIs(const Descriptor* descriptor) const { - const string type_url = type_url_->GetNoArena(); - string full_name; - if (!ParseAnyTypeUrl(type_url, &full_name)) { - return false; - } - return full_name == descriptor->full_name(); -} - -bool ParseAnyTypeUrl(const string& type_url, string* url_prefix, - string* full_type_name) { - size_t pos = type_url.find_last_of("/"); - if (pos == string::npos || pos + 1 == type_url.size()) { - return false; - } - if (url_prefix) { - *url_prefix = type_url.substr(0, pos + 1); - } - *full_type_name = type_url.substr(pos + 1); - return true; -} - -bool ParseAnyTypeUrl(const string& type_url, string* full_type_name) { - return ParseAnyTypeUrl(type_url, NULL, full_type_name); -} - - bool GetAnyFieldDescriptors(const Message& message, const FieldDescriptor** type_url_field, const FieldDescriptor** value_field) { - const Descriptor* descriptor = message.GetDescriptor(); - if (descriptor->full_name() != kAnyFullTypeName) { - return false; - } - *type_url_field = descriptor->FindFieldByNumber(1); - *value_field = descriptor->FindFieldByNumber(2); - return (*type_url_field != NULL && - (*type_url_field)->type() == FieldDescriptor::TYPE_STRING && - *value_field != NULL && - (*value_field)->type() == FieldDescriptor::TYPE_BYTES); + const Descriptor* descriptor = message.GetDescriptor(); + if (descriptor->full_name() != kAnyFullTypeName) { + return false; + } + *type_url_field = descriptor->FindFieldByNumber(1); + *value_field = descriptor->FindFieldByNumber(2); + return (*type_url_field != NULL && + (*type_url_field)->type() == FieldDescriptor::TYPE_STRING && + *value_field != NULL && + (*value_field)->type() == FieldDescriptor::TYPE_BYTES); } } // namespace internal diff --git a/src/google/protobuf/any.h b/src/google/protobuf/any.h index 61dc7176a4..d7d43966e4 100644 --- a/src/google/protobuf/any.h +++ b/src/google/protobuf/any.h @@ -34,16 +34,28 @@ #include #include -#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 LIBPROTOBUF_EXPORT AnyMetadata { +class PROTOBUF_EXPORT AnyMetadata { typedef ArenaStringPtr UrlType; typedef ArenaStringPtr ValueType; public: @@ -52,31 +64,52 @@ class LIBPROTOBUF_EXPORT AnyMetadata { // 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 up "/". // For example, both PackFrom(message, "type.googleapis.com") and // PackFrom(message, "type.googleapis.com/") yield the same result type // URL: "type.googleapis.com/". - void PackFrom(const Message& message, const string& type_url_prefix); + template + void PackFrom(const T& message, StringPiece type_url_prefix) { + InternalPackFrom(message, type_url_prefix, T::FullMessageName()); + } + + void PackFrom(const Message& message, const std::string& 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 consdiered matching if its full name matches the full name after // the last "/" in the type URL. - template + template bool Is() const { - return InternalIs(T::default_instance().GetDescriptor()); + return InternalIs(T::FullMessageName()); } private: - bool InternalIs(const Descriptor* message) const; + 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_; @@ -84,10 +117,6 @@ class LIBPROTOBUF_EXPORT AnyMetadata { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AnyMetadata); }; -extern const char kAnyFullTypeName[]; // "google.protobuf.Any". -extern const char kTypeGoogleApisComPrefix[]; // "type.googleapis.com/". -extern const char kTypeGoogleProdComPrefix[]; // "type.googleprod.com/". - // 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 "/" @@ -95,15 +124,15 @@ extern const char kTypeGoogleProdComPrefix[]; // "type.googleprod.com/". // // NOTE: this function is available publicly as: // google::protobuf::Any() // static method on the generated message type. -bool ParseAnyTypeUrl(const string& type_url, string* full_type_name); +bool ParseAnyTypeUrl(const std::string& 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(const string& type_url, string* url_prefix, - string* full_type_name); +bool ParseAnyTypeUrl(const std::string& 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. @@ -115,4 +144,6 @@ bool GetAnyFieldDescriptors(const Message& message, } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_ANY_H__ diff --git a/src/google/protobuf/any.pb.cc b/src/google/protobuf/any.pb.cc index 7b10893ca1..bd382d1b6d 100644 --- a/src/google/protobuf/any.pb.cc +++ b/src/google/protobuf/any.pb.cc @@ -5,162 +5,140 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_OPEN class AnyDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Any_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsAny_google_2fprotobuf_2fany_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_Any_google_2fprotobuf_2fany_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_Any_default_instance_; - new (ptr) ::google::protobuf::Any(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Any_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Any(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::Any::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Any::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_Any_google_2fprotobuf_2fany_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsAny_google_2fprotobuf_2fany_2eproto}, {}}; - -void InitDefaults_google_2fprotobuf_2fany_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_Any_google_2fprotobuf_2fany_2eproto.base); -} +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Any_google_2fprotobuf_2fany_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_Any_google_2fprotobuf_2fany_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2fany_2eproto[1]; -constexpr ::google::protobuf::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fany_2eproto = nullptr; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fany_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fany_2eproto[1]; +static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fany_2eproto = nullptr; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fany_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fany_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2fany_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Any, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Any, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Any, type_url_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Any, value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Any, type_url_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Any, value_), }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, -1, sizeof(::google::protobuf::Any)}, +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, -1, sizeof(PROTOBUF_NAMESPACE_ID::Any)}, }; -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_Any_default_instance_), +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Any_default_instance_), }; -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2fany_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2fany_2eproto, "google/protobuf/any.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2fany_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2fany_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2fany_2eproto, file_level_service_descriptors_google_2fprotobuf_2fany_2eproto, -}; - -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fany_2eproto = { - false, InitDefaults_google_2fprotobuf_2fany_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2fany_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n\031google/protobuf/any.proto\022\017google.prot" "obuf\"&\n\003Any\022\020\n\010type_url\030\001 \001(\t\022\r\n\005value\030\002" " \001(\014Bo\n\023com.google.protobufB\010AnyProtoP\001Z" "%github.com/golang/protobuf/ptypes/any\242\002" "\003GPB\252\002\036Google.Protobuf.WellKnownTypesb\006p" "roto3" -, - "google/protobuf/any.proto", &assign_descriptors_table_google_2fprotobuf_2fany_2eproto, 205, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2fany_2eproto_deps[1] = { +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2fany_2eproto_sccs[1] = { + &scc_info_Any_google_2fprotobuf_2fany_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fany_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2fany_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fany_2eproto = { + &descriptor_table_google_2fprotobuf_2fany_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2fany_2eproto, "google/protobuf/any.proto", 205, + &descriptor_table_google_2fprotobuf_2fany_2eproto_once, descriptor_table_google_2fprotobuf_2fany_2eproto_sccs, descriptor_table_google_2fprotobuf_2fany_2eproto_deps, 1, 0, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2fany_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2fany_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2fany_2eproto, file_level_service_descriptors_google_2fprotobuf_2fany_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2fany_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fany_2eproto, deps, 0); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2fany_2eproto = []() { AddDescriptors_google_2fprotobuf_2fany_2eproto(); return true; }(); -namespace google { -namespace protobuf { +static bool dynamic_init_dummy_google_2fprotobuf_2fany_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fany_2eproto), true); +PROTOBUF_NAMESPACE_OPEN // =================================================================== void Any::InitAsDefaultInstance() { } -void Any::PackFrom(const ::google::protobuf::Message& message) { +void Any::PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message& message) { _any_metadata_.PackFrom(message); } -void Any::PackFrom(const ::google::protobuf::Message& message, - const ::std::string& type_url_prefix) { +void Any::PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message& message, + const std::string& type_url_prefix) { _any_metadata_.PackFrom(message, type_url_prefix); } -bool Any::UnpackTo(::google::protobuf::Message* message) const { +bool Any::UnpackTo(::PROTOBUF_NAMESPACE_ID::Message* message) const { return _any_metadata_.UnpackTo(message); } -bool Any::ParseAnyTypeUrl(const string& type_url, - string* full_type_name) { - return ::google::protobuf::internal::ParseAnyTypeUrl(type_url, - full_type_name); -} bool Any::GetAnyFieldDescriptors( - const ::google::protobuf::Message& message, - const ::google::protobuf::FieldDescriptor** type_url_field, - const ::google::protobuf::FieldDescriptor** value_field) { - return ::google::protobuf::internal::GetAnyFieldDescriptors( + const ::PROTOBUF_NAMESPACE_ID::Message& message, + const ::PROTOBUF_NAMESPACE_ID::FieldDescriptor** type_url_field, + const ::PROTOBUF_NAMESPACE_ID::FieldDescriptor** value_field) { + return ::PROTOBUF_NAMESPACE_ID::internal::GetAnyFieldDescriptors( message, type_url_field, value_field); } +bool Any::ParseAnyTypeUrl(const string& type_url, + std::string* full_type_name) { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseAnyTypeUrl(type_url, + full_type_name); +} -class Any::HasBitSetters { +class Any::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Any::kTypeUrlFieldNumber; -const int Any::kValueFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Any::Any() - : ::google::protobuf::Message(), _internal_metadata_(NULL), _any_metadata_(&type_url_, &value_) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr), _any_metadata_(&type_url_, &value_) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Any) } Any::Any(const Any& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _any_metadata_(&type_url_, &value_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - type_url_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.type_url().size() > 0) { - type_url_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.type_url_); + type_url_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.type_url().empty()) { + type_url_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.type_url_); } - value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.value().size() > 0) { - value_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.value_); + value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.value().empty()) { + value_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.value_); } // @@protoc_insertion_point(copy_constructor:google.protobuf.Any) } void Any::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_Any_google_2fprotobuf_2fany_2eproto.base); - type_url_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Any_google_2fprotobuf_2fany_2eproto.base); + type_url_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } Any::~Any() { @@ -169,118 +147,90 @@ Any::~Any() { } void Any::SharedDtor() { - type_url_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + type_url_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + value_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void Any::SetCachedSize(int size) const { _cached_size_.Set(size); } const Any& Any::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_Any_google_2fprotobuf_2fany_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_Any_google_2fprotobuf_2fany_2eproto.base); return *internal_default_instance(); } void Any::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Any) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; - type_url_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + type_url_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + value_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _internal_metadata_.Clear(); } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Any::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Any::_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) { - case 0: goto error; // string type_url = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Any.type_url"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_type_url(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_type_url(), ptr, ctx, "google.protobuf.Any.type_url"); + CHK_(ptr); + } else goto handle_unusual; + continue; // bytes value = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::internal::StringParser; - ::std::string* str = msg->mutable_value(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(mutable_value(), ptr, ctx); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Any::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Any) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // string type_url = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_type_url())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->type_url().data(), static_cast(this->type_url().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Any.type_url")); } else { goto handle_unusual; @@ -290,8 +240,8 @@ bool Any::MergePartialFromCodedStream( // bytes value = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadBytes( input, this->mutable_value())); } else { goto handle_unusual; @@ -304,7 +254,7 @@ bool Any::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -320,63 +270,31 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Any::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Any) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // string type_url = 1; - if (this->type_url().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->type_url().data(), static_cast(this->type_url().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Any.type_url"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->type_url(), output); - } - - // bytes value = 2; - if (this->value().size() > 0) { - ::google::protobuf::internal::WireFormatLite::WriteBytesMaybeAliased( - 2, this->value(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Any) -} - -::google::protobuf::uint8* Any::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Any::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Any) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // string type_url = 1; if (this->type_url().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->type_url().data(), static_cast(this->type_url().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Any.type_url"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->type_url(), target); } // bytes value = 2; if (this->value().size() > 0) { - target = - ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + target = stream->WriteBytesMaybeAliased( 2, this->value(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Any) return target; @@ -386,43 +304,42 @@ size_t Any::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Any) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // string type_url = 1; if (this->type_url().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->type_url()); } // bytes value = 2; if (this->value().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::BytesSize( this->value()); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Any::MergeFrom(const ::google::protobuf::Message& from) { +void Any::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Any) GOOGLE_DCHECK_NE(&from, this); const Any* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Any) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Any) MergeFrom(*source); @@ -433,20 +350,20 @@ void Any::MergeFrom(const Any& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Any) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; if (from.type_url().size() > 0) { - type_url_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.type_url_); + type_url_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.type_url_); } if (from.value().size() > 0) { - value_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.value_); + value_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.value_); } } -void Any::CopyFrom(const ::google::protobuf::Message& from) { +void Any::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Any) if (&from == this) return; Clear(); @@ -464,34 +381,27 @@ bool Any::IsInitialized() const { return true; } -void Any::Swap(Any* other) { - if (other == this) return; - InternalSwap(other); -} void Any::InternalSwap(Any* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); - type_url_.Swap(&other->type_url_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + type_url_.Swap(&other->type_url_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - value_.Swap(&other->value_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + value_.Swap(&other->value_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -::google::protobuf::Metadata Any::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fany_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fany_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Any::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Any* Arena::CreateMaybeMessage< ::google::protobuf::Any >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::Any >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Any* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Any >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::Any >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/any.pb.h b/src/google/protobuf/any.pb.h index a5ad5827f2..7430dce819 100644 --- a/src/google/protobuf/any.pb.h +++ b/src/google/protobuf/any.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/any.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2fany_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2fany_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fany_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fany_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,6 +27,7 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export @@ -33,51 +35,54 @@ #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fany_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fany_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fany_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[1] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fany_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] + 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2fany_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fany_2eproto; +PROTOBUF_NAMESPACE_OPEN class Any; class AnyDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern AnyDefaultTypeInternal _Any_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Any* Arena::CreateMaybeMessage<::google::protobuf::Any>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { +PROTOBUF_EXPORT extern AnyDefaultTypeInternal _Any_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Any* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN // =================================================================== -class LIBPROTOBUF_EXPORT Any : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Any) */ { +class PROTOBUF_EXPORT Any : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Any) */ { public: Any(); virtual ~Any(); Any(const Any& from); - - inline Any& operator=(const Any& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Any(Any&& from) noexcept : Any() { *this = ::std::move(from); } + inline Any& operator=(const Any& from) { + CopyFrom(from); + return *this; + } inline Any& operator=(Any&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -86,9 +91,15 @@ class LIBPROTOBUF_EXPORT Any : public ::google::protobuf::Message /* @@protoc_in } return *this; } - #endif - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Any& default_instance(); @@ -102,111 +113,120 @@ class LIBPROTOBUF_EXPORT Any : public ::google::protobuf::Message /* @@protoc_in // implements Any ----------------------------------------------- - void PackFrom(const ::google::protobuf::Message& message); - void PackFrom(const ::google::protobuf::Message& message, - const ::std::string& type_url_prefix); - bool UnpackTo(::google::protobuf::Message* message) const; + void PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message& message); + void PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message& message, + const std::string& type_url_prefix); + bool UnpackTo(::PROTOBUF_NAMESPACE_ID::Message* message) const; + static bool GetAnyFieldDescriptors( + const ::PROTOBUF_NAMESPACE_ID::Message& message, + const ::PROTOBUF_NAMESPACE_ID::FieldDescriptor** type_url_field, + const ::PROTOBUF_NAMESPACE_ID::FieldDescriptor** value_field); template bool Is() const { return _any_metadata_.Is(); } static bool ParseAnyTypeUrl(const string& type_url, - string* full_type_name); - static bool GetAnyFieldDescriptors( - const ::google::protobuf::Message& message, - const ::google::protobuf::FieldDescriptor** type_url_field, - const ::google::protobuf::FieldDescriptor** value_field); - void Swap(Any* other); + std::string* full_type_name); friend void swap(Any& a, Any& b) { a.Swap(&b); } + inline void Swap(Any* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Any* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Any* New(::google::protobuf::Arena* arena) const final { + Any* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Any& from); void MergeFrom(const Any& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Any* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Any"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fany_2eproto); + return ::descriptor_table_google_2fprotobuf_2fany_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kTypeUrlFieldNumber = 1, + kValueFieldNumber = 2, + }; // string type_url = 1; void clear_type_url(); - static const int kTypeUrlFieldNumber = 1; - const ::std::string& type_url() const; - void set_type_url(const ::std::string& value); - #if LANG_CXX11 - void set_type_url(::std::string&& value); - #endif + const std::string& type_url() const; + void set_type_url(const std::string& value); + void set_type_url(std::string&& value); void set_type_url(const char* value); void set_type_url(const char* value, size_t size); - ::std::string* mutable_type_url(); - ::std::string* release_type_url(); - void set_allocated_type_url(::std::string* type_url); + std::string* mutable_type_url(); + std::string* release_type_url(); + void set_allocated_type_url(std::string* type_url); // bytes value = 2; void clear_value(); - static const int kValueFieldNumber = 2; - const ::std::string& value() const; - void set_value(const ::std::string& value); - #if LANG_CXX11 - void set_value(::std::string&& value); - #endif + const std::string& value() const; + void set_value(const std::string& value); + void set_value(std::string&& value); void set_value(const char* value); void set_value(const void* value, size_t size); - ::std::string* mutable_value(); - ::std::string* release_value(); - void set_allocated_value(::std::string* value); + std::string* mutable_value(); + std::string* release_value(); + void set_allocated_value(std::string* value); // @@protoc_insertion_point(class_scope:google.protobuf.Any) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::internal::ArenaStringPtr type_url_; - ::google::protobuf::internal::ArenaStringPtr value_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::internal::AnyMetadata _any_metadata_; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr type_url_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr value_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata _any_metadata_; friend struct ::TableStruct_google_2fprotobuf_2fany_2eproto; }; // =================================================================== @@ -222,107 +242,103 @@ class LIBPROTOBUF_EXPORT Any : public ::google::protobuf::Message /* @@protoc_in // string type_url = 1; inline void Any::clear_type_url() { - type_url_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + type_url_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Any::type_url() const { +inline const std::string& Any::type_url() const { // @@protoc_insertion_point(field_get:google.protobuf.Any.type_url) return type_url_.GetNoArena(); } -inline void Any::set_type_url(const ::std::string& value) { +inline void Any::set_type_url(const std::string& value) { - type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Any.type_url) } -#if LANG_CXX11 -inline void Any::set_type_url(::std::string&& value) { +inline void Any::set_type_url(std::string&& value) { type_url_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Any.type_url) } -#endif inline void Any::set_type_url(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Any.type_url) } inline void Any::set_type_url(const char* value, size_t size) { - type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Any.type_url) } -inline ::std::string* Any::mutable_type_url() { +inline std::string* Any::mutable_type_url() { // @@protoc_insertion_point(field_mutable:google.protobuf.Any.type_url) - return type_url_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return type_url_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Any::release_type_url() { +inline std::string* Any::release_type_url() { // @@protoc_insertion_point(field_release:google.protobuf.Any.type_url) - return type_url_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return type_url_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Any::set_allocated_type_url(::std::string* type_url) { - if (type_url != NULL) { +inline void Any::set_allocated_type_url(std::string* type_url) { + if (type_url != nullptr) { } else { } - type_url_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), type_url); + type_url_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), type_url); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Any.type_url) } // bytes value = 2; inline void Any::clear_value() { - value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + value_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Any::value() const { +inline const std::string& Any::value() const { // @@protoc_insertion_point(field_get:google.protobuf.Any.value) return value_.GetNoArena(); } -inline void Any::set_value(const ::std::string& value) { +inline void Any::set_value(const std::string& value) { - value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + value_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Any.value) } -#if LANG_CXX11 -inline void Any::set_value(::std::string&& value) { +inline void Any::set_value(std::string&& value) { value_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Any.value) } -#endif inline void Any::set_value(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + value_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Any.value) } inline void Any::set_value(const void* value, size_t size) { - value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + value_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Any.value) } -inline ::std::string* Any::mutable_value() { +inline std::string* Any::mutable_value() { // @@protoc_insertion_point(field_mutable:google.protobuf.Any.value) - return value_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return value_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Any::release_value() { +inline std::string* Any::release_value() { // @@protoc_insertion_point(field_release:google.protobuf.Any.value) - return value_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return value_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Any::set_allocated_value(::std::string* value) { - if (value != NULL) { +inline void Any::set_allocated_value(std::string* value) { + if (value != nullptr) { } else { } - value_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + value_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Any.value) } @@ -332,10 +348,9 @@ inline void Any::set_allocated_value(::std::string* value) { // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2fany_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fany_2eproto diff --git a/src/google/protobuf/any.proto b/src/google/protobuf/any.proto index 4932942558..c9be854167 100644 --- a/src/google/protobuf/any.proto +++ b/src/google/protobuf/any.proto @@ -121,7 +121,8 @@ option objc_class_prefix = "GPB"; // message Any { // A URL/resource name that uniquely identifies the type of the serialized - // protocol buffer message. The last segment of the URL's path must represent + // protocol buffer message. This string must contain at least + // one "/" character. The last segment of the URL's path must represent // the fully qualified name of the type (as in // `path/google.protobuf.Duration`). The name should be in a canonical form // (e.g., leading "." is not accepted). diff --git a/src/google/protobuf/any_lite.cc b/src/google/protobuf/any_lite.cc new file mode 100644 index 0000000000..7403ed3fda --- /dev/null +++ b/src/google/protobuf/any_lite.cc @@ -0,0 +1,123 @@ +// 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 internal { + +std::string GetTypeUrl(StringPiece message_name, + StringPiece type_url_prefix) { + if (!type_url_prefix.empty() && + type_url_prefix[type_url_prefix.size() - 1] == '/') { + return StrCat(type_url_prefix, message_name); + } else { + return StrCat(type_url_prefix, "/", message_name); + } +} + +const char kAnyFullTypeName[] = "google.protobuf.Any"; +const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/"; +const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/"; + +AnyMetadata::AnyMetadata(UrlType* type_url, ValueType* value) + : type_url_(type_url), value_(value) {} + +void AnyMetadata::InternalPackFrom(const MessageLite& message, + StringPiece type_url_prefix, + StringPiece type_name) { + type_url_->SetNoArena(&::google::protobuf::internal::GetEmptyString(), + GetTypeUrl(type_name, type_url_prefix)); + message.SerializeToString(value_->MutableNoArena( + &::google::protobuf::internal::GetEmptyStringAlreadyInited())); +} + +bool AnyMetadata::InternalUnpackTo(StringPiece type_name, + MessageLite* message) const { + if (!InternalIs(type_name)) { + return false; + } + return message->ParseFromString(value_->GetNoArena()); +} + +namespace { + +// The type URL could be stored in either an ArenaStringPtr or a +// StringPieceField, so we provide these helpers to get a string_view from +// either type. We use a template function as a way to avoid depending on +// StringPieceField. + +template +StringPiece Get(const T* ptr) { + return ptr->Get(); +} + +template <> +// NOLINTNEXTLINE: clang-diagnostic-unused-function +StringPiece Get(const ArenaStringPtr* ptr) { + return ptr->GetNoArena(); +} + +} // namespace + +bool AnyMetadata::InternalIs(StringPiece type_name) const { + StringPiece type_url = Get(type_url_); + return type_url.size() >= type_name.size() + 1 && + type_url[type_url.size() - type_name.size() - 1] == '/' && + HasSuffixString(type_url, type_name); +} + +bool ParseAnyTypeUrl(const std::string& type_url, std::string* url_prefix, + std::string* full_type_name) { + size_t pos = type_url.find_last_of("/"); + if (pos == std::string::npos || pos + 1 == type_url.size()) { + return false; + } + if (url_prefix) { + *url_prefix = type_url.substr(0, pos + 1); + } + *full_type_name = type_url.substr(pos + 1); + return true; +} + +bool ParseAnyTypeUrl(const std::string& type_url, std::string* full_type_name) { + return ParseAnyTypeUrl(type_url, nullptr, full_type_name); +} + +} // namespace internal +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/any_test.cc b/src/google/protobuf/any_test.cc index 514ac510df..4fbf0b630e 100644 --- a/src/google/protobuf/any_test.cc +++ b/src/google/protobuf/any_test.cc @@ -29,9 +29,11 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include #include + namespace google { namespace protobuf { namespace { @@ -42,14 +44,26 @@ TEST(AnyTest, TestPackAndUnpack) { protobuf_unittest::TestAny message; message.mutable_any_value()->PackFrom(submessage); - string data = message.SerializeAsString(); + std::string data = message.SerializeAsString(); ASSERT_TRUE(message.ParseFromString(data)); EXPECT_TRUE(message.has_any_value()); + submessage.Clear(); ASSERT_TRUE(message.any_value().UnpackTo(&submessage)); EXPECT_EQ(12345, submessage.int32_value()); } +TEST(AnyTest, TestUnpackWithTypeMismatch) { + protobuf_unittest::TestAny payload; + payload.set_int32_value(13); + google::protobuf::Any any; + any.PackFrom(payload); + + // Attempt to unpack into the wrong type. + protobuf_unittest::TestAllTypes dest; + EXPECT_FALSE(any.UnpackTo(&dest)); +} + TEST(AnyTest, TestPackAndUnpackAny) { // We can pack a Any message inside another Any message. protobuf_unittest::TestAny submessage; @@ -59,30 +73,30 @@ TEST(AnyTest, TestPackAndUnpackAny) { protobuf_unittest::TestAny message; message.mutable_any_value()->PackFrom(any); - string data = message.SerializeAsString(); + std::string data = message.SerializeAsString(); ASSERT_TRUE(message.ParseFromString(data)); EXPECT_TRUE(message.has_any_value()); + any.Clear(); + submessage.Clear(); ASSERT_TRUE(message.any_value().UnpackTo(&any)); ASSERT_TRUE(any.UnpackTo(&submessage)); EXPECT_EQ(12345, submessage.int32_value()); } -TEST(AnyType, TestPackWithCustomTypeUrl) { +TEST(AnyTest, TestPackWithCustomTypeUrl) { protobuf_unittest::TestAny submessage; submessage.set_int32_value(12345); google::protobuf::Any any; // Pack with a custom type URL prefix. any.PackFrom(submessage, "type.myservice.com"); - EXPECT_EQ("type.myservice.com/" + submessage.GetDescriptor()->full_name(), - any.type_url()); + EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url()); // Pack with a custom type URL prefix ending with '/'. any.PackFrom(submessage, "type.myservice.com/"); - EXPECT_EQ("type.myservice.com/" + submessage.GetDescriptor()->full_name(), - any.type_url()); + EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url()); // Pack with an empty type URL prefix. any.PackFrom(submessage, ""); - EXPECT_EQ("/" + submessage.GetDescriptor()->full_name(), any.type_url()); + EXPECT_EQ("/protobuf_unittest.TestAny", any.type_url()); // Test unpacking the type. submessage.Clear(); @@ -104,6 +118,15 @@ TEST(AnyTest, TestIs) { ASSERT_TRUE(message.ParseFromString(message.SerializeAsString())); EXPECT_FALSE(message.any_value().Is()); EXPECT_TRUE(message.any_value().Is()); + + any.set_type_url("/protobuf_unittest.TestAny"); + EXPECT_TRUE(any.Is()); + // The type URL must contain at least one "/". + any.set_type_url("protobuf_unittest.TestAny"); + EXPECT_FALSE(any.Is()); + // The type name after the slash must be fully qualified. + any.set_type_url("/TestAny"); + EXPECT_FALSE(any.Is()); } TEST(AnyTest, MoveConstructor) { @@ -117,6 +140,7 @@ TEST(AnyTest, MoveConstructor) { google::protobuf::Any dst(std::move(src)); EXPECT_EQ(type_url, dst.type_url().data()); + payload.Clear(); ASSERT_TRUE(dst.UnpackTo(&payload)); EXPECT_EQ(12345, payload.int32_value()); } @@ -133,6 +157,7 @@ TEST(AnyTest, MoveAssignment) { google::protobuf::Any dst; dst = std::move(src); EXPECT_EQ(type_url, dst.type_url().data()); + payload.Clear(); ASSERT_TRUE(dst.UnpackTo(&payload)); EXPECT_EQ(12345, payload.int32_value()); } @@ -141,4 +166,3 @@ TEST(AnyTest, MoveAssignment) { } // namespace } // namespace protobuf } // namespace google - diff --git a/src/google/protobuf/api.pb.cc b/src/google/protobuf/api.pb.cc index ac2a0e898e..123026facf 100644 --- a/src/google/protobuf/api.pb.cc +++ b/src/google/protobuf/api.pb.cc @@ -5,151 +5,130 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fapi_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_Mixin_google_2fprotobuf_2fapi_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fapi_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_Method_google_2fprotobuf_2fapi_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fsource_5fcontext_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftype_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_Option_google_2fprotobuf_2ftype_2eproto; -namespace google { -namespace protobuf { +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fapi_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_Method_google_2fprotobuf_2fapi_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fapi_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Mixin_google_2fprotobuf_2fapi_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftype_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_Option_google_2fprotobuf_2ftype_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fsource_5fcontext_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto; +PROTOBUF_NAMESPACE_OPEN class ApiDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Api_default_instance_; class MethodDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Method_default_instance_; class MixinDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Mixin_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsApi_google_2fprotobuf_2fapi_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_Api_google_2fprotobuf_2fapi_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_Api_default_instance_; - new (ptr) ::google::protobuf::Api(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Api_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Api(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::Api::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Api::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<4> scc_info_Api_google_2fprotobuf_2fapi_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 4, InitDefaultsApi_google_2fprotobuf_2fapi_2eproto}, { +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<4> scc_info_Api_google_2fprotobuf_2fapi_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 4, InitDefaultsscc_info_Api_google_2fprotobuf_2fapi_2eproto}, { &scc_info_Method_google_2fprotobuf_2fapi_2eproto.base, &scc_info_Option_google_2fprotobuf_2ftype_2eproto.base, &scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base, &scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base,}}; -static void InitDefaultsMethod_google_2fprotobuf_2fapi_2eproto() { +static void InitDefaultsscc_info_Method_google_2fprotobuf_2fapi_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_Method_default_instance_; - new (ptr) ::google::protobuf::Method(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Method_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Method(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::Method::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Method::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_Method_google_2fprotobuf_2fapi_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsMethod_google_2fprotobuf_2fapi_2eproto}, { +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_Method_google_2fprotobuf_2fapi_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_Method_google_2fprotobuf_2fapi_2eproto}, { &scc_info_Option_google_2fprotobuf_2ftype_2eproto.base,}}; -static void InitDefaultsMixin_google_2fprotobuf_2fapi_2eproto() { +static void InitDefaultsscc_info_Mixin_google_2fprotobuf_2fapi_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_Mixin_default_instance_; - new (ptr) ::google::protobuf::Mixin(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Mixin_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Mixin(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::Mixin::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Mixin::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_Mixin_google_2fprotobuf_2fapi_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsMixin_google_2fprotobuf_2fapi_2eproto}, {}}; - -void InitDefaults_google_2fprotobuf_2fapi_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_Api_google_2fprotobuf_2fapi_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_Method_google_2fprotobuf_2fapi_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base); -} +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Mixin_google_2fprotobuf_2fapi_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_Mixin_google_2fprotobuf_2fapi_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2fapi_2eproto[3]; -constexpr ::google::protobuf::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fapi_2eproto = nullptr; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fapi_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fapi_2eproto[3]; +static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fapi_2eproto = nullptr; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fapi_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fapi_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2fapi_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Api, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Api, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Api, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Api, methods_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Api, options_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Api, version_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Api, source_context_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Api, mixins_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Api, syntax_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Api, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Api, methods_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Api, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Api, version_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Api, source_context_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Api, mixins_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Api, syntax_), ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Method, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Method, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Method, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Method, request_type_url_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Method, request_streaming_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Method, response_type_url_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Method, response_streaming_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Method, options_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Method, syntax_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Method, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Method, request_type_url_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Method, request_streaming_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Method, response_type_url_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Method, response_streaming_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Method, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Method, syntax_), ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Mixin, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Mixin, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Mixin, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Mixin, root_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Mixin, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Mixin, root_), }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, -1, sizeof(::google::protobuf::Api)}, - { 12, -1, sizeof(::google::protobuf::Method)}, - { 24, -1, sizeof(::google::protobuf::Mixin)}, +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, -1, sizeof(PROTOBUF_NAMESPACE_ID::Api)}, + { 12, -1, sizeof(PROTOBUF_NAMESPACE_ID::Method)}, + { 24, -1, sizeof(PROTOBUF_NAMESPACE_ID::Mixin)}, }; -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_Api_default_instance_), - reinterpret_cast(&::google::protobuf::_Method_default_instance_), - reinterpret_cast(&::google::protobuf::_Mixin_default_instance_), -}; - -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2fapi_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2fapi_2eproto, "google/protobuf/api.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2fapi_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2fapi_2eproto, 3, file_level_enum_descriptors_google_2fprotobuf_2fapi_2eproto, file_level_service_descriptors_google_2fprotobuf_2fapi_2eproto, +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Api_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Method_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Mixin_default_instance_), }; -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fapi_2eproto = { - false, InitDefaults_google_2fprotobuf_2fapi_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2fapi_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n\031google/protobuf/api.proto\022\017google.prot" "obuf\032$google/protobuf/source_context.pro" "to\032\032google/protobuf/type.proto\"\201\002\n\003Api\022\014" @@ -169,92 +148,86 @@ static ::google::protobuf::Message const * const file_default_instances[] = { "otobufB\010ApiProtoP\001Z+google.golang.org/ge" "nproto/protobuf/api;api\242\002\003GPB\252\002\036Google.P" "rotobuf.WellKnownTypesb\006proto3" -, - "google/protobuf/api.proto", &assign_descriptors_table_google_2fprotobuf_2fapi_2eproto, 750, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2fapi_2eproto_deps[2] = { + &::descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto, + &::descriptor_table_google_2fprotobuf_2ftype_2eproto, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2fapi_2eproto_sccs[3] = { + &scc_info_Api_google_2fprotobuf_2fapi_2eproto.base, + &scc_info_Method_google_2fprotobuf_2fapi_2eproto.base, + &scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fapi_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2fapi_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fapi_2eproto = { + &descriptor_table_google_2fprotobuf_2fapi_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2fapi_2eproto, "google/protobuf/api.proto", 750, + &descriptor_table_google_2fprotobuf_2fapi_2eproto_once, descriptor_table_google_2fprotobuf_2fapi_2eproto_sccs, descriptor_table_google_2fprotobuf_2fapi_2eproto_deps, 3, 2, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2fapi_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2fapi_2eproto, 3, file_level_enum_descriptors_google_2fprotobuf_2fapi_2eproto, file_level_service_descriptors_google_2fprotobuf_2fapi_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2fapi_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[2] = - { - ::AddDescriptors_google_2fprotobuf_2fsource_5fcontext_2eproto, - ::AddDescriptors_google_2fprotobuf_2ftype_2eproto, - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fapi_2eproto, deps, 2); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2fapi_2eproto = []() { AddDescriptors_google_2fprotobuf_2fapi_2eproto(); return true; }(); -namespace google { -namespace protobuf { +static bool dynamic_init_dummy_google_2fprotobuf_2fapi_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fapi_2eproto), true); +PROTOBUF_NAMESPACE_OPEN // =================================================================== void Api::InitAsDefaultInstance() { - ::google::protobuf::_Api_default_instance_._instance.get_mutable()->source_context_ = const_cast< ::google::protobuf::SourceContext*>( - ::google::protobuf::SourceContext::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_Api_default_instance_._instance.get_mutable()->source_context_ = const_cast< PROTOBUF_NAMESPACE_ID::SourceContext*>( + PROTOBUF_NAMESPACE_ID::SourceContext::internal_default_instance()); } -class Api::HasBitSetters { +class Api::_Internal { public: - static const ::google::protobuf::SourceContext& source_context(const Api* msg); + static const PROTOBUF_NAMESPACE_ID::SourceContext& source_context(const Api* msg); }; -const ::google::protobuf::SourceContext& -Api::HasBitSetters::source_context(const Api* msg) { +const PROTOBUF_NAMESPACE_ID::SourceContext& +Api::_Internal::source_context(const Api* msg) { return *msg->source_context_; } void Api::clear_options() { options_.Clear(); } void Api::clear_source_context() { - if (GetArenaNoVirtual() == NULL && source_context_ != NULL) { + if (GetArenaNoVirtual() == nullptr && source_context_ != nullptr) { delete source_context_; } - source_context_ = NULL; + source_context_ = nullptr; } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Api::kNameFieldNumber; -const int Api::kMethodsFieldNumber; -const int Api::kOptionsFieldNumber; -const int Api::kVersionFieldNumber; -const int Api::kSourceContextFieldNumber; -const int Api::kMixinsFieldNumber; -const int Api::kSyntaxFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Api::Api() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Api) } Api::Api(const Api& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), methods_(from.methods_), options_(from.options_), mixins_(from.mixins_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.name().size() > 0) { - name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.name().empty()) { + name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); } - version_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.version().size() > 0) { - version_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.version_); + version_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.version().empty()) { + version_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.version_); } if (from.has_source_context()) { - source_context_ = new ::google::protobuf::SourceContext(*from.source_context_); + source_context_ = new PROTOBUF_NAMESPACE_ID::SourceContext(*from.source_context_); } else { - source_context_ = NULL; + source_context_ = nullptr; } syntax_ = from.syntax_; // @@protoc_insertion_point(copy_constructor:google.protobuf.Api) } void Api::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_Api_google_2fprotobuf_2fapi_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - version_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Api_google_2fprotobuf_2fapi_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + version_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&source_context_, 0, static_cast( reinterpret_cast(&syntax_) - reinterpret_cast(&source_context_)) + sizeof(syntax_)); @@ -266,8 +239,8 @@ Api::~Api() { } void Api::SharedDtor() { - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - version_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + version_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete source_context_; } @@ -275,190 +248,141 @@ void Api::SetCachedSize(int size) const { _cached_size_.Set(size); } const Api& Api::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_Api_google_2fprotobuf_2fapi_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_Api_google_2fprotobuf_2fapi_2eproto.base); return *internal_default_instance(); } void Api::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Api) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; methods_.Clear(); options_.Clear(); mixins_.Clear(); - name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - version_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (GetArenaNoVirtual() == NULL && source_context_ != NULL) { + name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + version_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (GetArenaNoVirtual() == nullptr && source_context_ != nullptr) { delete source_context_; } - source_context_ = NULL; + source_context_ = nullptr; syntax_ = 0; _internal_metadata_.Clear(); } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Api::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Api::_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) { - case 0: goto error; // string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Api.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Api.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.Method methods = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::Method::_InternalParse; - object = msg->add_methods(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 18 && (ptr += 1)); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_methods(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 18); + } else goto handle_unusual; + continue; // repeated .google.protobuf.Option options = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::Option::_InternalParse; - object = msg->add_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 26 && (ptr += 1)); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_options(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 26); + } else goto handle_unusual; + continue; // string version = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Api.version"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_version(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_version(), ptr, ctx, "google.protobuf.Api.version"); + CHK_(ptr); + } else goto handle_unusual; + continue; // .google.protobuf.SourceContext source_context = 5; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::SourceContext::_InternalParse; - object = msg->mutable_source_context(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 42)) { + ptr = ctx->ParseMessage(mutable_source_context(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.Mixin mixins = 6; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 50) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::Mixin::_InternalParse; - object = msg->add_mixins(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 50 && (ptr += 1)); - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 50)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_mixins(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 50); + } else goto handle_unusual; + continue; // .google.protobuf.Syntax syntax = 7; - case 7: { - if (static_cast<::google::protobuf::uint8>(tag) != 56) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::Syntax value = static_cast<::google::protobuf::Syntax>(val); - msg->set_syntax(value); - break; - } + case 7: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 56)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + set_syntax(static_cast(val)); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Api::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Api) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Api.name")); } else { goto handle_unusual; @@ -468,8 +392,8 @@ bool Api::MergePartialFromCodedStream( // repeated .google.protobuf.Method methods = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_methods())); } else { goto handle_unusual; @@ -479,8 +403,8 @@ bool Api::MergePartialFromCodedStream( // repeated .google.protobuf.Option options = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_options())); } else { goto handle_unusual; @@ -490,12 +414,12 @@ bool Api::MergePartialFromCodedStream( // string version = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (34 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_version())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->version().data(), static_cast(this->version().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Api.version")); } else { goto handle_unusual; @@ -505,8 +429,8 @@ bool Api::MergePartialFromCodedStream( // .google.protobuf.SourceContext source_context = 5; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (42 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (42 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_source_context())); } else { goto handle_unusual; @@ -516,8 +440,8 @@ bool Api::MergePartialFromCodedStream( // repeated .google.protobuf.Mixin mixins = 6; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (50 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (50 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_mixins())); } else { goto handle_unusual; @@ -527,12 +451,12 @@ bool Api::MergePartialFromCodedStream( // .google.protobuf.Syntax syntax = 7; case 7: { - if (static_cast< ::google::protobuf::uint8>(tag) == (56 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (56 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - set_syntax(static_cast< ::google::protobuf::Syntax >(value)); + set_syntax(static_cast< PROTOBUF_NAMESPACE_ID::Syntax >(value)); } else { goto handle_unusual; } @@ -544,7 +468,7 @@ bool Api::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -560,147 +484,74 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Api::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Api) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // string name = 1; - if (this->name().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Api.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // repeated .google.protobuf.Method methods = 2; - for (unsigned int i = 0, - n = static_cast(this->methods_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 2, - this->methods(static_cast(i)), - output); - } - - // repeated .google.protobuf.Option options = 3; - for (unsigned int i = 0, - n = static_cast(this->options_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 3, - this->options(static_cast(i)), - output); - } - - // string version = 4; - if (this->version().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->version().data(), static_cast(this->version().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Api.version"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 4, this->version(), output); - } - - // .google.protobuf.SourceContext source_context = 5; - if (this->has_source_context()) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 5, HasBitSetters::source_context(this), output); - } - - // repeated .google.protobuf.Mixin mixins = 6; - for (unsigned int i = 0, - n = static_cast(this->mixins_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 6, - this->mixins(static_cast(i)), - output); - } - - // .google.protobuf.Syntax syntax = 7; - if (this->syntax() != 0) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 7, this->syntax(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Api) -} - -::google::protobuf::uint8* Api::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Api::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Api) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // string name = 1; if (this->name().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Api.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // repeated .google.protobuf.Method methods = 2; - for (unsigned int i = 0, - n = static_cast(this->methods_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 2, this->methods(static_cast(i)), deterministic, target); + for (auto it = this->methods().pointer_begin(), + end = this->methods().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(2, **it, target, stream); } // repeated .google.protobuf.Option options = 3; - for (unsigned int i = 0, - n = static_cast(this->options_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 3, this->options(static_cast(i)), deterministic, target); + for (auto it = this->options().pointer_begin(), + end = this->options().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(3, **it, target, stream); } // string version = 4; if (this->version().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->version().data(), static_cast(this->version().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Api.version"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 4, this->version(), target); } // .google.protobuf.SourceContext source_context = 5; if (this->has_source_context()) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 5, HasBitSetters::source_context(this), deterministic, target); + 5, _Internal::source_context(this), target, stream); } // repeated .google.protobuf.Mixin mixins = 6; - for (unsigned int i = 0, - n = static_cast(this->mixins_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 6, this->mixins(static_cast(i)), deterministic, target); + for (auto it = this->mixins().pointer_begin(), + end = this->mixins().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(6, **it, target, stream); } // .google.protobuf.Syntax syntax = 7; if (this->syntax() != 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 7, this->syntax(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Api) return target; @@ -710,12 +561,7 @@ size_t Api::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Api) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -725,7 +571,7 @@ size_t Api::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->methods(static_cast(i))); } } @@ -736,7 +582,7 @@ size_t Api::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->options(static_cast(i))); } } @@ -747,7 +593,7 @@ size_t Api::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->mixins(static_cast(i))); } } @@ -755,44 +601,48 @@ size_t Api::ByteSizeLong() const { // string name = 1; if (this->name().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // string version = 4; if (this->version().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->version()); } // .google.protobuf.SourceContext source_context = 5; if (this->has_source_context()) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *source_context_); } // .google.protobuf.Syntax syntax = 7; if (this->syntax() != 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->syntax()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->syntax()); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Api::MergeFrom(const ::google::protobuf::Message& from) { +void Api::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Api) GOOGLE_DCHECK_NE(&from, this); const Api* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Api) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Api) MergeFrom(*source); @@ -803,7 +653,7 @@ void Api::MergeFrom(const Api& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Api) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; methods_.MergeFrom(from.methods_); @@ -811,21 +661,21 @@ void Api::MergeFrom(const Api& from) { mixins_.MergeFrom(from.mixins_); if (from.name().size() > 0) { - name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_); + name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); } if (from.version().size() > 0) { - version_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.version_); + version_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.version_); } if (from.has_source_context()) { - mutable_source_context()->::google::protobuf::SourceContext::MergeFrom(from.source_context()); + mutable_source_context()->PROTOBUF_NAMESPACE_ID::SourceContext::MergeFrom(from.source_context()); } if (from.syntax() != 0) { set_syntax(from.syntax()); } } -void Api::CopyFrom(const ::google::protobuf::Message& from) { +void Api::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Api) if (&from == this) return; Clear(); @@ -843,27 +693,22 @@ bool Api::IsInitialized() const { return true; } -void Api::Swap(Api* other) { - if (other == this) return; - InternalSwap(other); -} void Api::InternalSwap(Api* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); CastToBase(&methods_)->InternalSwap(CastToBase(&other->methods_)); CastToBase(&options_)->InternalSwap(CastToBase(&other->options_)); CastToBase(&mixins_)->InternalSwap(CastToBase(&other->mixins_)); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - version_.Swap(&other->version_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + version_.Swap(&other->version_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(source_context_, other->source_context_); swap(syntax_, other->syntax_); } -::google::protobuf::Metadata Api::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fapi_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fapi_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Api::GetMetadata() const { + return GetMetadataStatic(); } @@ -871,44 +716,34 @@ void Api::InternalSwap(Api* other) { void Method::InitAsDefaultInstance() { } -class Method::HasBitSetters { +class Method::_Internal { public: }; void Method::clear_options() { options_.Clear(); } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Method::kNameFieldNumber; -const int Method::kRequestTypeUrlFieldNumber; -const int Method::kRequestStreamingFieldNumber; -const int Method::kResponseTypeUrlFieldNumber; -const int Method::kResponseStreamingFieldNumber; -const int Method::kOptionsFieldNumber; -const int Method::kSyntaxFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Method::Method() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Method) } Method::Method(const Method& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), options_(from.options_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.name().size() > 0) { - name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.name().empty()) { + name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); } - request_type_url_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.request_type_url().size() > 0) { - request_type_url_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.request_type_url_); + request_type_url_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.request_type_url().empty()) { + request_type_url_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.request_type_url_); } - response_type_url_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.response_type_url().size() > 0) { - response_type_url_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.response_type_url_); + response_type_url_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.response_type_url().empty()) { + response_type_url_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.response_type_url_); } ::memcpy(&request_streaming_, &from.request_streaming_, static_cast(reinterpret_cast(&syntax_) - @@ -917,11 +752,10 @@ Method::Method(const Method& from) } void Method::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_Method_google_2fprotobuf_2fapi_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - request_type_url_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - response_type_url_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Method_google_2fprotobuf_2fapi_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + request_type_url_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + response_type_url_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&request_streaming_, 0, static_cast( reinterpret_cast(&syntax_) - reinterpret_cast(&request_streaming_)) + sizeof(syntax_)); @@ -933,30 +767,30 @@ Method::~Method() { } void Method::SharedDtor() { - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - request_type_url_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - response_type_url_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + request_type_url_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + response_type_url_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void Method::SetCachedSize(int size) const { _cached_size_.Set(size); } const Method& Method::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_Method_google_2fprotobuf_2fapi_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_Method_google_2fprotobuf_2fapi_2eproto.base); return *internal_default_instance(); } void Method::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Method) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; options_.Clear(); - name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - request_type_url_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - response_type_url_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + request_type_url_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + response_type_url_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&request_streaming_, 0, static_cast( reinterpret_cast(&syntax_) - reinterpret_cast(&request_streaming_)) + sizeof(syntax_)); @@ -964,156 +798,106 @@ void Method::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Method::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Method::_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) { - case 0: goto error; // string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Method.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Method.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // string request_type_url = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Method.request_type_url"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_request_type_url(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_request_type_url(), ptr, ctx, "google.protobuf.Method.request_type_url"); + CHK_(ptr); + } else goto handle_unusual; + continue; // bool request_streaming = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_request_streaming(value); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { + request_streaming_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // string response_type_url = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Method.response_type_url"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_response_type_url(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_response_type_url(), ptr, ctx, "google.protobuf.Method.response_type_url"); + CHK_(ptr); + } else goto handle_unusual; + continue; // bool response_streaming = 5; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 40) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_response_streaming(value); - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 40)) { + response_streaming_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.Option options = 6; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 50) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::Option::_InternalParse; - object = msg->add_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 50 && (ptr += 1)); - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 50)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_options(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 50); + } else goto handle_unusual; + continue; // .google.protobuf.Syntax syntax = 7; - case 7: { - if (static_cast<::google::protobuf::uint8>(tag) != 56) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::Syntax value = static_cast<::google::protobuf::Syntax>(val); - msg->set_syntax(value); - break; - } + case 7: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 56)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + set_syntax(static_cast(val)); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Method::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Method) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Method.name")); } else { goto handle_unusual; @@ -1123,12 +907,12 @@ bool Method::MergePartialFromCodedStream( // string request_type_url = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_request_type_url())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->request_type_url().data(), static_cast(this->request_type_url().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Method.request_type_url")); } else { goto handle_unusual; @@ -1138,10 +922,10 @@ bool Method::MergePartialFromCodedStream( // bool request_streaming = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &request_streaming_))); } else { goto handle_unusual; @@ -1151,12 +935,12 @@ bool Method::MergePartialFromCodedStream( // string response_type_url = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (34 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_response_type_url())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->response_type_url().data(), static_cast(this->response_type_url().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Method.response_type_url")); } else { goto handle_unusual; @@ -1166,10 +950,10 @@ bool Method::MergePartialFromCodedStream( // bool response_streaming = 5; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (40 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (40 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &response_streaming_))); } else { goto handle_unusual; @@ -1179,8 +963,8 @@ bool Method::MergePartialFromCodedStream( // repeated .google.protobuf.Option options = 6; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (50 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (50 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_options())); } else { goto handle_unusual; @@ -1190,12 +974,12 @@ bool Method::MergePartialFromCodedStream( // .google.protobuf.Syntax syntax = 7; case 7: { - if (static_cast< ::google::protobuf::uint8>(tag) == (56 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (56 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - set_syntax(static_cast< ::google::protobuf::Syntax >(value)); + set_syntax(static_cast< PROTOBUF_NAMESPACE_ID::Syntax >(value)); } else { goto handle_unusual; } @@ -1207,7 +991,7 @@ bool Method::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -1223,141 +1007,72 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Method::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Method) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // string name = 1; - if (this->name().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Method.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // string request_type_url = 2; - if (this->request_type_url().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->request_type_url().data(), static_cast(this->request_type_url().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Method.request_type_url"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 2, this->request_type_url(), output); - } - - // bool request_streaming = 3; - if (this->request_streaming() != 0) { - ::google::protobuf::internal::WireFormatLite::WriteBool(3, this->request_streaming(), output); - } - - // string response_type_url = 4; - if (this->response_type_url().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->response_type_url().data(), static_cast(this->response_type_url().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Method.response_type_url"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 4, this->response_type_url(), output); - } - - // bool response_streaming = 5; - if (this->response_streaming() != 0) { - ::google::protobuf::internal::WireFormatLite::WriteBool(5, this->response_streaming(), output); - } - - // repeated .google.protobuf.Option options = 6; - for (unsigned int i = 0, - n = static_cast(this->options_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 6, - this->options(static_cast(i)), - output); - } - - // .google.protobuf.Syntax syntax = 7; - if (this->syntax() != 0) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 7, this->syntax(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Method) -} - -::google::protobuf::uint8* Method::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Method::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Method) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // string name = 1; if (this->name().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Method.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // string request_type_url = 2; if (this->request_type_url().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->request_type_url().data(), static_cast(this->request_type_url().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Method.request_type_url"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 2, this->request_type_url(), target); } // bool request_streaming = 3; if (this->request_streaming() != 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(3, this->request_streaming(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(3, this->request_streaming(), target); } // string response_type_url = 4; if (this->response_type_url().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->response_type_url().data(), static_cast(this->response_type_url().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Method.response_type_url"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 4, this->response_type_url(), target); } // bool response_streaming = 5; if (this->response_streaming() != 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(5, this->response_streaming(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(5, this->response_streaming(), target); } // repeated .google.protobuf.Option options = 6; - for (unsigned int i = 0, - n = static_cast(this->options_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 6, this->options(static_cast(i)), deterministic, target); + for (auto it = this->options().pointer_begin(), + end = this->options().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(6, **it, target, stream); } // .google.protobuf.Syntax syntax = 7; if (this->syntax() != 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 7, this->syntax(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Method) return target; @@ -1367,12 +1082,7 @@ size_t Method::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Method) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1382,7 +1092,7 @@ size_t Method::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->options(static_cast(i))); } } @@ -1390,21 +1100,21 @@ size_t Method::ByteSizeLong() const { // string name = 1; if (this->name().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // string request_type_url = 2; if (this->request_type_url().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->request_type_url()); } // string response_type_url = 4; if (this->response_type_url().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->response_type_url()); } @@ -1421,23 +1131,27 @@ size_t Method::ByteSizeLong() const { // .google.protobuf.Syntax syntax = 7; if (this->syntax() != 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->syntax()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->syntax()); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Method::MergeFrom(const ::google::protobuf::Message& from) { +void Method::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Method) GOOGLE_DCHECK_NE(&from, this); const Method* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Method) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Method) MergeFrom(*source); @@ -1448,21 +1162,21 @@ void Method::MergeFrom(const Method& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Method) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; options_.MergeFrom(from.options_); if (from.name().size() > 0) { - name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_); + name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); } if (from.request_type_url().size() > 0) { - request_type_url_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.request_type_url_); + request_type_url_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.request_type_url_); } if (from.response_type_url().size() > 0) { - response_type_url_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.response_type_url_); + response_type_url_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.response_type_url_); } if (from.request_streaming() != 0) { set_request_streaming(from.request_streaming()); @@ -1475,7 +1189,7 @@ void Method::MergeFrom(const Method& from) { } } -void Method::CopyFrom(const ::google::protobuf::Message& from) { +void Method::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Method) if (&from == this) return; Clear(); @@ -1493,28 +1207,23 @@ bool Method::IsInitialized() const { return true; } -void Method::Swap(Method* other) { - if (other == this) return; - InternalSwap(other); -} void Method::InternalSwap(Method* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); CastToBase(&options_)->InternalSwap(CastToBase(&other->options_)); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - request_type_url_.Swap(&other->request_type_url_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + request_type_url_.Swap(&other->request_type_url_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - response_type_url_.Swap(&other->response_type_url_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + response_type_url_.Swap(&other->response_type_url_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(request_streaming_, other->request_streaming_); swap(response_streaming_, other->response_streaming_); swap(syntax_, other->syntax_); } -::google::protobuf::Metadata Method::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fapi_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fapi_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Method::GetMetadata() const { + return GetMetadataStatic(); } @@ -1522,40 +1231,34 @@ void Method::InternalSwap(Method* other) { void Mixin::InitAsDefaultInstance() { } -class Mixin::HasBitSetters { +class Mixin::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Mixin::kNameFieldNumber; -const int Mixin::kRootFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Mixin::Mixin() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Mixin) } Mixin::Mixin(const Mixin& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.name().size() > 0) { - name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.name().empty()) { + name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); } - root_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.root().size() > 0) { - root_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.root_); + root_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.root().empty()) { + root_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.root_); } // @@protoc_insertion_point(copy_constructor:google.protobuf.Mixin) } void Mixin::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - root_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + root_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } Mixin::~Mixin() { @@ -1564,119 +1267,90 @@ Mixin::~Mixin() { } void Mixin::SharedDtor() { - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - root_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + root_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void Mixin::SetCachedSize(int size) const { _cached_size_.Set(size); } const Mixin& Mixin::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base); return *internal_default_instance(); } void Mixin::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Mixin) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; - name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - root_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + root_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _internal_metadata_.Clear(); } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Mixin::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Mixin::_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) { - case 0: goto error; // string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Mixin.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Mixin.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // string root = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Mixin.root"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_root(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_root(), ptr, ctx, "google.protobuf.Mixin.root"); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Mixin::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Mixin) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Mixin.name")); } else { goto handle_unusual; @@ -1686,12 +1360,12 @@ bool Mixin::MergePartialFromCodedStream( // string root = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_root())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->root().data(), static_cast(this->root().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Mixin.root")); } else { goto handle_unusual; @@ -1704,7 +1378,7 @@ bool Mixin::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -1720,71 +1394,35 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Mixin::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Mixin) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // string name = 1; - if (this->name().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Mixin.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // string root = 2; - if (this->root().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->root().data(), static_cast(this->root().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Mixin.root"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 2, this->root(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Mixin) -} - -::google::protobuf::uint8* Mixin::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Mixin::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Mixin) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // string name = 1; if (this->name().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Mixin.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // string root = 2; if (this->root().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->root().data(), static_cast(this->root().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Mixin.root"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 2, this->root(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Mixin) return target; @@ -1794,43 +1432,42 @@ size_t Mixin::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Mixin) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // string name = 1; if (this->name().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // string root = 2; if (this->root().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->root()); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Mixin::MergeFrom(const ::google::protobuf::Message& from) { +void Mixin::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Mixin) GOOGLE_DCHECK_NE(&from, this); const Mixin* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Mixin) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Mixin) MergeFrom(*source); @@ -1841,20 +1478,20 @@ void Mixin::MergeFrom(const Mixin& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Mixin) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; if (from.name().size() > 0) { - name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_); + name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); } if (from.root().size() > 0) { - root_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.root_); + root_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.root_); } } -void Mixin::CopyFrom(const ::google::protobuf::Message& from) { +void Mixin::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Mixin) if (&from == this) return; Clear(); @@ -1872,40 +1509,33 @@ bool Mixin::IsInitialized() const { return true; } -void Mixin::Swap(Mixin* other) { - if (other == this) return; - InternalSwap(other); -} void Mixin::InternalSwap(Mixin* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - root_.Swap(&other->root_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + root_.Swap(&other->root_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -::google::protobuf::Metadata Mixin::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fapi_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fapi_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Mixin::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Api* Arena::CreateMaybeMessage< ::google::protobuf::Api >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::Api >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Api* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Api >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::Api >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Method* Arena::CreateMaybeMessage< ::google::protobuf::Method >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::Method >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Method* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Method >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::Method >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Mixin* Arena::CreateMaybeMessage< ::google::protobuf::Mixin >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::Mixin >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Mixin* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Mixin >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::Mixin >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/api.pb.h b/src/google/protobuf/api.pb.h index 5af8aaf237..836641f81c 100644 --- a/src/google/protobuf/api.pb.h +++ b/src/google/protobuf/api.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/api.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2fapi_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2fapi_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fapi_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fapi_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,6 +27,7 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export @@ -34,59 +36,62 @@ #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fapi_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fapi_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fapi_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[3] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fapi_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2fapi_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fapi_2eproto; +PROTOBUF_NAMESPACE_OPEN class Api; class ApiDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern ApiDefaultTypeInternal _Api_default_instance_; +PROTOBUF_EXPORT extern ApiDefaultTypeInternal _Api_default_instance_; class Method; class MethodDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern MethodDefaultTypeInternal _Method_default_instance_; +PROTOBUF_EXPORT extern MethodDefaultTypeInternal _Method_default_instance_; class Mixin; class MixinDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern MixinDefaultTypeInternal _Mixin_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Api* Arena::CreateMaybeMessage<::google::protobuf::Api>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Method* Arena::CreateMaybeMessage<::google::protobuf::Method>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Mixin* Arena::CreateMaybeMessage<::google::protobuf::Mixin>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { +PROTOBUF_EXPORT extern MixinDefaultTypeInternal _Mixin_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Api* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Method* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Mixin* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN // =================================================================== -class LIBPROTOBUF_EXPORT Api : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Api) */ { +class PROTOBUF_EXPORT Api : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Api) */ { public: Api(); virtual ~Api(); Api(const Api& from); - - inline Api& operator=(const Api& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Api(Api&& from) noexcept : Api() { *this = ::std::move(from); } + inline Api& operator=(const Api& from) { + CopyFrom(from); + return *this; + } inline Api& operator=(Api&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -95,9 +100,15 @@ class LIBPROTOBUF_EXPORT Api : public ::google::protobuf::Message /* @@protoc_in } return *this; } - #endif - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Api& default_instance(); @@ -109,174 +120,182 @@ class LIBPROTOBUF_EXPORT Api : public ::google::protobuf::Message /* @@protoc_in static constexpr int kIndexInFileMessages = 0; - void Swap(Api* other); friend void swap(Api& a, Api& b) { a.Swap(&b); } + inline void Swap(Api* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Api* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Api* New(::google::protobuf::Arena* arena) const final { + Api* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Api& from); void MergeFrom(const Api& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Api* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Api"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fapi_2eproto); + return ::descriptor_table_google_2fprotobuf_2fapi_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kMethodsFieldNumber = 2, + kOptionsFieldNumber = 3, + kMixinsFieldNumber = 6, + kNameFieldNumber = 1, + kVersionFieldNumber = 4, + kSourceContextFieldNumber = 5, + kSyntaxFieldNumber = 7, + }; // repeated .google.protobuf.Method methods = 2; int methods_size() const; void clear_methods(); - static const int kMethodsFieldNumber = 2; - ::google::protobuf::Method* mutable_methods(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Method >* + PROTOBUF_NAMESPACE_ID::Method* mutable_methods(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Method >* mutable_methods(); - const ::google::protobuf::Method& methods(int index) const; - ::google::protobuf::Method* add_methods(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Method >& + const PROTOBUF_NAMESPACE_ID::Method& methods(int index) const; + PROTOBUF_NAMESPACE_ID::Method* add_methods(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Method >& methods() const; // repeated .google.protobuf.Option options = 3; int options_size() const; void clear_options(); - static const int kOptionsFieldNumber = 3; - ::google::protobuf::Option* mutable_options(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option >* + PROTOBUF_NAMESPACE_ID::Option* mutable_options(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option >* mutable_options(); - const ::google::protobuf::Option& options(int index) const; - ::google::protobuf::Option* add_options(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option >& + const PROTOBUF_NAMESPACE_ID::Option& options(int index) const; + PROTOBUF_NAMESPACE_ID::Option* add_options(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option >& options() const; // repeated .google.protobuf.Mixin mixins = 6; int mixins_size() const; void clear_mixins(); - static const int kMixinsFieldNumber = 6; - ::google::protobuf::Mixin* mutable_mixins(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Mixin >* + PROTOBUF_NAMESPACE_ID::Mixin* mutable_mixins(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Mixin >* mutable_mixins(); - const ::google::protobuf::Mixin& mixins(int index) const; - ::google::protobuf::Mixin* add_mixins(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Mixin >& + const PROTOBUF_NAMESPACE_ID::Mixin& mixins(int index) const; + PROTOBUF_NAMESPACE_ID::Mixin* add_mixins(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Mixin >& mixins() const; // string name = 1; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); // string version = 4; void clear_version(); - static const int kVersionFieldNumber = 4; - const ::std::string& version() const; - void set_version(const ::std::string& value); - #if LANG_CXX11 - void set_version(::std::string&& value); - #endif + const std::string& version() const; + void set_version(const std::string& value); + void set_version(std::string&& value); void set_version(const char* value); void set_version(const char* value, size_t size); - ::std::string* mutable_version(); - ::std::string* release_version(); - void set_allocated_version(::std::string* version); + std::string* mutable_version(); + std::string* release_version(); + void set_allocated_version(std::string* version); // .google.protobuf.SourceContext source_context = 5; bool has_source_context() const; void clear_source_context(); - static const int kSourceContextFieldNumber = 5; - const ::google::protobuf::SourceContext& source_context() const; - ::google::protobuf::SourceContext* release_source_context(); - ::google::protobuf::SourceContext* mutable_source_context(); - void set_allocated_source_context(::google::protobuf::SourceContext* source_context); + const PROTOBUF_NAMESPACE_ID::SourceContext& source_context() const; + PROTOBUF_NAMESPACE_ID::SourceContext* release_source_context(); + PROTOBUF_NAMESPACE_ID::SourceContext* mutable_source_context(); + void set_allocated_source_context(PROTOBUF_NAMESPACE_ID::SourceContext* source_context); // .google.protobuf.Syntax syntax = 7; void clear_syntax(); - static const int kSyntaxFieldNumber = 7; - ::google::protobuf::Syntax syntax() const; - void set_syntax(::google::protobuf::Syntax value); + PROTOBUF_NAMESPACE_ID::Syntax syntax() const; + void set_syntax(PROTOBUF_NAMESPACE_ID::Syntax value); // @@protoc_insertion_point(class_scope:google.protobuf.Api) private: - class HasBitSetters; - - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Method > methods_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option > options_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Mixin > mixins_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::internal::ArenaStringPtr version_; - ::google::protobuf::SourceContext* source_context_; + class _Internal; + + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Method > methods_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option > options_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Mixin > mixins_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr version_; + PROTOBUF_NAMESPACE_ID::SourceContext* source_context_; int syntax_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2fapi_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT Method : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Method) */ { +class PROTOBUF_EXPORT Method : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Method) */ { public: Method(); virtual ~Method(); Method(const Method& from); - - inline Method& operator=(const Method& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Method(Method&& from) noexcept : Method() { *this = ::std::move(from); } + inline Method& operator=(const Method& from) { + CopyFrom(from); + return *this; + } inline Method& operator=(Method&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -285,9 +304,15 @@ class LIBPROTOBUF_EXPORT Method : public ::google::protobuf::Message /* @@protoc } return *this; } - #endif - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Method& default_instance(); @@ -299,167 +324,173 @@ class LIBPROTOBUF_EXPORT Method : public ::google::protobuf::Message /* @@protoc static constexpr int kIndexInFileMessages = 1; - void Swap(Method* other); friend void swap(Method& a, Method& b) { a.Swap(&b); } + inline void Swap(Method* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Method* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Method* New(::google::protobuf::Arena* arena) const final { + Method* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Method& from); void MergeFrom(const Method& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Method* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Method"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fapi_2eproto); + return ::descriptor_table_google_2fprotobuf_2fapi_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kOptionsFieldNumber = 6, + kNameFieldNumber = 1, + kRequestTypeUrlFieldNumber = 2, + kResponseTypeUrlFieldNumber = 4, + kRequestStreamingFieldNumber = 3, + kResponseStreamingFieldNumber = 5, + kSyntaxFieldNumber = 7, + }; // repeated .google.protobuf.Option options = 6; int options_size() const; void clear_options(); - static const int kOptionsFieldNumber = 6; - ::google::protobuf::Option* mutable_options(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option >* + PROTOBUF_NAMESPACE_ID::Option* mutable_options(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option >* mutable_options(); - const ::google::protobuf::Option& options(int index) const; - ::google::protobuf::Option* add_options(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option >& + const PROTOBUF_NAMESPACE_ID::Option& options(int index) const; + PROTOBUF_NAMESPACE_ID::Option* add_options(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option >& options() const; // string name = 1; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); // string request_type_url = 2; void clear_request_type_url(); - static const int kRequestTypeUrlFieldNumber = 2; - const ::std::string& request_type_url() const; - void set_request_type_url(const ::std::string& value); - #if LANG_CXX11 - void set_request_type_url(::std::string&& value); - #endif + const std::string& request_type_url() const; + void set_request_type_url(const std::string& value); + void set_request_type_url(std::string&& value); void set_request_type_url(const char* value); void set_request_type_url(const char* value, size_t size); - ::std::string* mutable_request_type_url(); - ::std::string* release_request_type_url(); - void set_allocated_request_type_url(::std::string* request_type_url); + std::string* mutable_request_type_url(); + std::string* release_request_type_url(); + void set_allocated_request_type_url(std::string* request_type_url); // string response_type_url = 4; void clear_response_type_url(); - static const int kResponseTypeUrlFieldNumber = 4; - const ::std::string& response_type_url() const; - void set_response_type_url(const ::std::string& value); - #if LANG_CXX11 - void set_response_type_url(::std::string&& value); - #endif + const std::string& response_type_url() const; + void set_response_type_url(const std::string& value); + void set_response_type_url(std::string&& value); void set_response_type_url(const char* value); void set_response_type_url(const char* value, size_t size); - ::std::string* mutable_response_type_url(); - ::std::string* release_response_type_url(); - void set_allocated_response_type_url(::std::string* response_type_url); + std::string* mutable_response_type_url(); + std::string* release_response_type_url(); + void set_allocated_response_type_url(std::string* response_type_url); // bool request_streaming = 3; void clear_request_streaming(); - static const int kRequestStreamingFieldNumber = 3; bool request_streaming() const; void set_request_streaming(bool value); // bool response_streaming = 5; void clear_response_streaming(); - static const int kResponseStreamingFieldNumber = 5; bool response_streaming() const; void set_response_streaming(bool value); // .google.protobuf.Syntax syntax = 7; void clear_syntax(); - static const int kSyntaxFieldNumber = 7; - ::google::protobuf::Syntax syntax() const; - void set_syntax(::google::protobuf::Syntax value); + PROTOBUF_NAMESPACE_ID::Syntax syntax() const; + void set_syntax(PROTOBUF_NAMESPACE_ID::Syntax value); // @@protoc_insertion_point(class_scope:google.protobuf.Method) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option > options_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::internal::ArenaStringPtr request_type_url_; - ::google::protobuf::internal::ArenaStringPtr response_type_url_; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option > options_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr request_type_url_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr response_type_url_; bool request_streaming_; bool response_streaming_; int syntax_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2fapi_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT Mixin : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Mixin) */ { +class PROTOBUF_EXPORT Mixin : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Mixin) */ { public: Mixin(); virtual ~Mixin(); Mixin(const Mixin& from); - - inline Mixin& operator=(const Mixin& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Mixin(Mixin&& from) noexcept : Mixin() { *this = ::std::move(from); } + inline Mixin& operator=(const Mixin& from) { + CopyFrom(from); + return *this; + } inline Mixin& operator=(Mixin&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -468,9 +499,15 @@ class LIBPROTOBUF_EXPORT Mixin : public ::google::protobuf::Message /* @@protoc_ } return *this; } - #endif - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Mixin& default_instance(); @@ -482,97 +519,106 @@ class LIBPROTOBUF_EXPORT Mixin : public ::google::protobuf::Message /* @@protoc_ static constexpr int kIndexInFileMessages = 2; - void Swap(Mixin* other); friend void swap(Mixin& a, Mixin& b) { a.Swap(&b); } + inline void Swap(Mixin* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Mixin* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Mixin* New(::google::protobuf::Arena* arena) const final { + Mixin* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Mixin& from); void MergeFrom(const Mixin& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Mixin* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Mixin"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fapi_2eproto); + return ::descriptor_table_google_2fprotobuf_2fapi_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kNameFieldNumber = 1, + kRootFieldNumber = 2, + }; // string name = 1; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); // string root = 2; void clear_root(); - static const int kRootFieldNumber = 2; - const ::std::string& root() const; - void set_root(const ::std::string& value); - #if LANG_CXX11 - void set_root(::std::string&& value); - #endif + const std::string& root() const; + void set_root(const std::string& value); + void set_root(std::string&& value); void set_root(const char* value); void set_root(const char* value, size_t size); - ::std::string* mutable_root(); - ::std::string* release_root(); - void set_allocated_root(::std::string* root); + std::string* mutable_root(); + std::string* release_root(); + void set_allocated_root(std::string* root); // @@protoc_insertion_point(class_scope:google.protobuf.Mixin) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::internal::ArenaStringPtr root_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr root_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2fapi_2eproto; }; // =================================================================== @@ -588,54 +634,52 @@ class LIBPROTOBUF_EXPORT Mixin : public ::google::protobuf::Message /* @@protoc_ // string name = 1; inline void Api::clear_name() { - name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Api::name() const { +inline const std::string& Api::name() const { // @@protoc_insertion_point(field_get:google.protobuf.Api.name) return name_.GetNoArena(); } -inline void Api::set_name(const ::std::string& value) { +inline void Api::set_name(const std::string& value) { - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Api.name) } -#if LANG_CXX11 -inline void Api::set_name(::std::string&& value) { +inline void Api::set_name(std::string&& value) { name_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Api.name) } -#endif inline void Api::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Api.name) } inline void Api::set_name(const char* value, size_t size) { - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Api.name) } -inline ::std::string* Api::mutable_name() { +inline std::string* Api::mutable_name() { // @@protoc_insertion_point(field_mutable:google.protobuf.Api.name) - return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Api::release_name() { +inline std::string* Api::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.Api.name) - return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Api::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void Api::set_allocated_name(std::string* name) { + if (name != nullptr) { } else { } - name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name); + name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Api.name) } @@ -646,24 +690,24 @@ inline int Api::methods_size() const { inline void Api::clear_methods() { methods_.Clear(); } -inline ::google::protobuf::Method* Api::mutable_methods(int index) { +inline PROTOBUF_NAMESPACE_ID::Method* Api::mutable_methods(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.Api.methods) return methods_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::Method >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Method >* Api::mutable_methods() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.Api.methods) return &methods_; } -inline const ::google::protobuf::Method& Api::methods(int index) const { +inline const PROTOBUF_NAMESPACE_ID::Method& Api::methods(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.Api.methods) return methods_.Get(index); } -inline ::google::protobuf::Method* Api::add_methods() { +inline PROTOBUF_NAMESPACE_ID::Method* Api::add_methods() { // @@protoc_insertion_point(field_add:google.protobuf.Api.methods) return methods_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Method >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Method >& Api::methods() const { // @@protoc_insertion_point(field_list:google.protobuf.Api.methods) return methods_; @@ -673,24 +717,24 @@ Api::methods() const { inline int Api::options_size() const { return options_.size(); } -inline ::google::protobuf::Option* Api::mutable_options(int index) { +inline PROTOBUF_NAMESPACE_ID::Option* Api::mutable_options(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.Api.options) return options_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option >* Api::mutable_options() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.Api.options) return &options_; } -inline const ::google::protobuf::Option& Api::options(int index) const { +inline const PROTOBUF_NAMESPACE_ID::Option& Api::options(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.Api.options) return options_.Get(index); } -inline ::google::protobuf::Option* Api::add_options() { +inline PROTOBUF_NAMESPACE_ID::Option* Api::add_options() { // @@protoc_insertion_point(field_add:google.protobuf.Api.options) return options_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option >& Api::options() const { // @@protoc_insertion_point(field_list:google.protobuf.Api.options) return options_; @@ -698,92 +742,90 @@ Api::options() const { // string version = 4; inline void Api::clear_version() { - version_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + version_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Api::version() const { +inline const std::string& Api::version() const { // @@protoc_insertion_point(field_get:google.protobuf.Api.version) return version_.GetNoArena(); } -inline void Api::set_version(const ::std::string& value) { +inline void Api::set_version(const std::string& value) { - version_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + version_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Api.version) } -#if LANG_CXX11 -inline void Api::set_version(::std::string&& value) { +inline void Api::set_version(std::string&& value) { version_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Api.version) } -#endif inline void Api::set_version(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - version_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + version_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Api.version) } inline void Api::set_version(const char* value, size_t size) { - version_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + version_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Api.version) } -inline ::std::string* Api::mutable_version() { +inline std::string* Api::mutable_version() { // @@protoc_insertion_point(field_mutable:google.protobuf.Api.version) - return version_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return version_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Api::release_version() { +inline std::string* Api::release_version() { // @@protoc_insertion_point(field_release:google.protobuf.Api.version) - return version_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return version_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Api::set_allocated_version(::std::string* version) { - if (version != NULL) { +inline void Api::set_allocated_version(std::string* version) { + if (version != nullptr) { } else { } - version_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), version); + version_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), version); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Api.version) } // .google.protobuf.SourceContext source_context = 5; inline bool Api::has_source_context() const { - return this != internal_default_instance() && source_context_ != NULL; + return this != internal_default_instance() && source_context_ != nullptr; } -inline const ::google::protobuf::SourceContext& Api::source_context() const { - const ::google::protobuf::SourceContext* p = source_context_; +inline const PROTOBUF_NAMESPACE_ID::SourceContext& Api::source_context() const { + const PROTOBUF_NAMESPACE_ID::SourceContext* p = source_context_; // @@protoc_insertion_point(field_get:google.protobuf.Api.source_context) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_SourceContext_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_SourceContext_default_instance_); } -inline ::google::protobuf::SourceContext* Api::release_source_context() { +inline PROTOBUF_NAMESPACE_ID::SourceContext* Api::release_source_context() { // @@protoc_insertion_point(field_release:google.protobuf.Api.source_context) - ::google::protobuf::SourceContext* temp = source_context_; - source_context_ = NULL; + PROTOBUF_NAMESPACE_ID::SourceContext* temp = source_context_; + source_context_ = nullptr; return temp; } -inline ::google::protobuf::SourceContext* Api::mutable_source_context() { +inline PROTOBUF_NAMESPACE_ID::SourceContext* Api::mutable_source_context() { - if (source_context_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::SourceContext>(GetArenaNoVirtual()); + if (source_context_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); source_context_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.Api.source_context) return source_context_; } -inline void Api::set_allocated_source_context(::google::protobuf::SourceContext* source_context) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { - delete reinterpret_cast< ::google::protobuf::MessageLite*>(source_context_); +inline void Api::set_allocated_source_context(PROTOBUF_NAMESPACE_ID::SourceContext* source_context) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { + delete reinterpret_cast< ::PROTOBUF_NAMESPACE_ID::MessageLite*>(source_context_); } if (source_context) { - ::google::protobuf::Arena* submessage_arena = NULL; + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = nullptr; if (message_arena != submessage_arena) { - source_context = ::google::protobuf::internal::GetOwnedMessage( + source_context = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, source_context, submessage_arena); } @@ -801,24 +843,24 @@ inline int Api::mixins_size() const { inline void Api::clear_mixins() { mixins_.Clear(); } -inline ::google::protobuf::Mixin* Api::mutable_mixins(int index) { +inline PROTOBUF_NAMESPACE_ID::Mixin* Api::mutable_mixins(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.Api.mixins) return mixins_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::Mixin >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Mixin >* Api::mutable_mixins() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.Api.mixins) return &mixins_; } -inline const ::google::protobuf::Mixin& Api::mixins(int index) const { +inline const PROTOBUF_NAMESPACE_ID::Mixin& Api::mixins(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.Api.mixins) return mixins_.Get(index); } -inline ::google::protobuf::Mixin* Api::add_mixins() { +inline PROTOBUF_NAMESPACE_ID::Mixin* Api::add_mixins() { // @@protoc_insertion_point(field_add:google.protobuf.Api.mixins) return mixins_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Mixin >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Mixin >& Api::mixins() const { // @@protoc_insertion_point(field_list:google.protobuf.Api.mixins) return mixins_; @@ -828,11 +870,11 @@ Api::mixins() const { inline void Api::clear_syntax() { syntax_ = 0; } -inline ::google::protobuf::Syntax Api::syntax() const { +inline PROTOBUF_NAMESPACE_ID::Syntax Api::syntax() const { // @@protoc_insertion_point(field_get:google.protobuf.Api.syntax) - return static_cast< ::google::protobuf::Syntax >(syntax_); + return static_cast< PROTOBUF_NAMESPACE_ID::Syntax >(syntax_); } -inline void Api::set_syntax(::google::protobuf::Syntax value) { +inline void Api::set_syntax(PROTOBUF_NAMESPACE_ID::Syntax value) { syntax_ = value; // @@protoc_insertion_point(field_set:google.protobuf.Api.syntax) @@ -844,107 +886,103 @@ inline void Api::set_syntax(::google::protobuf::Syntax value) { // string name = 1; inline void Method::clear_name() { - name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Method::name() const { +inline const std::string& Method::name() const { // @@protoc_insertion_point(field_get:google.protobuf.Method.name) return name_.GetNoArena(); } -inline void Method::set_name(const ::std::string& value) { +inline void Method::set_name(const std::string& value) { - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Method.name) } -#if LANG_CXX11 -inline void Method::set_name(::std::string&& value) { +inline void Method::set_name(std::string&& value) { name_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Method.name) } -#endif inline void Method::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Method.name) } inline void Method::set_name(const char* value, size_t size) { - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Method.name) } -inline ::std::string* Method::mutable_name() { +inline std::string* Method::mutable_name() { // @@protoc_insertion_point(field_mutable:google.protobuf.Method.name) - return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Method::release_name() { +inline std::string* Method::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.Method.name) - return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Method::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void Method::set_allocated_name(std::string* name) { + if (name != nullptr) { } else { } - name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name); + name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Method.name) } // string request_type_url = 2; inline void Method::clear_request_type_url() { - request_type_url_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + request_type_url_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Method::request_type_url() const { +inline const std::string& Method::request_type_url() const { // @@protoc_insertion_point(field_get:google.protobuf.Method.request_type_url) return request_type_url_.GetNoArena(); } -inline void Method::set_request_type_url(const ::std::string& value) { +inline void Method::set_request_type_url(const std::string& value) { - request_type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + request_type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Method.request_type_url) } -#if LANG_CXX11 -inline void Method::set_request_type_url(::std::string&& value) { +inline void Method::set_request_type_url(std::string&& value) { request_type_url_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Method.request_type_url) } -#endif inline void Method::set_request_type_url(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - request_type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + request_type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Method.request_type_url) } inline void Method::set_request_type_url(const char* value, size_t size) { - request_type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + request_type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Method.request_type_url) } -inline ::std::string* Method::mutable_request_type_url() { +inline std::string* Method::mutable_request_type_url() { // @@protoc_insertion_point(field_mutable:google.protobuf.Method.request_type_url) - return request_type_url_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return request_type_url_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Method::release_request_type_url() { +inline std::string* Method::release_request_type_url() { // @@protoc_insertion_point(field_release:google.protobuf.Method.request_type_url) - return request_type_url_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return request_type_url_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Method::set_allocated_request_type_url(::std::string* request_type_url) { - if (request_type_url != NULL) { +inline void Method::set_allocated_request_type_url(std::string* request_type_url) { + if (request_type_url != nullptr) { } else { } - request_type_url_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), request_type_url); + request_type_url_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), request_type_url); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Method.request_type_url) } @@ -964,54 +1002,52 @@ inline void Method::set_request_streaming(bool value) { // string response_type_url = 4; inline void Method::clear_response_type_url() { - response_type_url_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + response_type_url_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Method::response_type_url() const { +inline const std::string& Method::response_type_url() const { // @@protoc_insertion_point(field_get:google.protobuf.Method.response_type_url) return response_type_url_.GetNoArena(); } -inline void Method::set_response_type_url(const ::std::string& value) { +inline void Method::set_response_type_url(const std::string& value) { - response_type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + response_type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Method.response_type_url) } -#if LANG_CXX11 -inline void Method::set_response_type_url(::std::string&& value) { +inline void Method::set_response_type_url(std::string&& value) { response_type_url_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Method.response_type_url) } -#endif inline void Method::set_response_type_url(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - response_type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + response_type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Method.response_type_url) } inline void Method::set_response_type_url(const char* value, size_t size) { - response_type_url_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + response_type_url_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Method.response_type_url) } -inline ::std::string* Method::mutable_response_type_url() { +inline std::string* Method::mutable_response_type_url() { // @@protoc_insertion_point(field_mutable:google.protobuf.Method.response_type_url) - return response_type_url_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return response_type_url_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Method::release_response_type_url() { +inline std::string* Method::release_response_type_url() { // @@protoc_insertion_point(field_release:google.protobuf.Method.response_type_url) - return response_type_url_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return response_type_url_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Method::set_allocated_response_type_url(::std::string* response_type_url) { - if (response_type_url != NULL) { +inline void Method::set_allocated_response_type_url(std::string* response_type_url) { + if (response_type_url != nullptr) { } else { } - response_type_url_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), response_type_url); + response_type_url_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), response_type_url); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Method.response_type_url) } @@ -1033,24 +1069,24 @@ inline void Method::set_response_streaming(bool value) { inline int Method::options_size() const { return options_.size(); } -inline ::google::protobuf::Option* Method::mutable_options(int index) { +inline PROTOBUF_NAMESPACE_ID::Option* Method::mutable_options(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.Method.options) return options_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option >* Method::mutable_options() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.Method.options) return &options_; } -inline const ::google::protobuf::Option& Method::options(int index) const { +inline const PROTOBUF_NAMESPACE_ID::Option& Method::options(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.Method.options) return options_.Get(index); } -inline ::google::protobuf::Option* Method::add_options() { +inline PROTOBUF_NAMESPACE_ID::Option* Method::add_options() { // @@protoc_insertion_point(field_add:google.protobuf.Method.options) return options_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Option >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Option >& Method::options() const { // @@protoc_insertion_point(field_list:google.protobuf.Method.options) return options_; @@ -1060,11 +1096,11 @@ Method::options() const { inline void Method::clear_syntax() { syntax_ = 0; } -inline ::google::protobuf::Syntax Method::syntax() const { +inline PROTOBUF_NAMESPACE_ID::Syntax Method::syntax() const { // @@protoc_insertion_point(field_get:google.protobuf.Method.syntax) - return static_cast< ::google::protobuf::Syntax >(syntax_); + return static_cast< PROTOBUF_NAMESPACE_ID::Syntax >(syntax_); } -inline void Method::set_syntax(::google::protobuf::Syntax value) { +inline void Method::set_syntax(PROTOBUF_NAMESPACE_ID::Syntax value) { syntax_ = value; // @@protoc_insertion_point(field_set:google.protobuf.Method.syntax) @@ -1076,107 +1112,103 @@ inline void Method::set_syntax(::google::protobuf::Syntax value) { // string name = 1; inline void Mixin::clear_name() { - name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Mixin::name() const { +inline const std::string& Mixin::name() const { // @@protoc_insertion_point(field_get:google.protobuf.Mixin.name) return name_.GetNoArena(); } -inline void Mixin::set_name(const ::std::string& value) { +inline void Mixin::set_name(const std::string& value) { - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Mixin.name) } -#if LANG_CXX11 -inline void Mixin::set_name(::std::string&& value) { +inline void Mixin::set_name(std::string&& value) { name_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Mixin.name) } -#endif inline void Mixin::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Mixin.name) } inline void Mixin::set_name(const char* value, size_t size) { - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Mixin.name) } -inline ::std::string* Mixin::mutable_name() { +inline std::string* Mixin::mutable_name() { // @@protoc_insertion_point(field_mutable:google.protobuf.Mixin.name) - return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Mixin::release_name() { +inline std::string* Mixin::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.Mixin.name) - return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Mixin::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void Mixin::set_allocated_name(std::string* name) { + if (name != nullptr) { } else { } - name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name); + name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Mixin.name) } // string root = 2; inline void Mixin::clear_root() { - root_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + root_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& Mixin::root() const { +inline const std::string& Mixin::root() const { // @@protoc_insertion_point(field_get:google.protobuf.Mixin.root) return root_.GetNoArena(); } -inline void Mixin::set_root(const ::std::string& value) { +inline void Mixin::set_root(const std::string& value) { - root_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + root_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.Mixin.root) } -#if LANG_CXX11 -inline void Mixin::set_root(::std::string&& value) { +inline void Mixin::set_root(std::string&& value) { root_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Mixin.root) } -#endif inline void Mixin::set_root(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - root_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + root_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.Mixin.root) } inline void Mixin::set_root(const char* value, size_t size) { - root_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + root_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Mixin.root) } -inline ::std::string* Mixin::mutable_root() { +inline std::string* Mixin::mutable_root() { // @@protoc_insertion_point(field_mutable:google.protobuf.Mixin.root) - return root_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return root_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Mixin::release_root() { +inline std::string* Mixin::release_root() { // @@protoc_insertion_point(field_release:google.protobuf.Mixin.root) - return root_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return root_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Mixin::set_allocated_root(::std::string* root) { - if (root != NULL) { +inline void Mixin::set_allocated_root(std::string* root) { + if (root != nullptr) { } else { } - root_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), root); + root_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), root); // @@protoc_insertion_point(field_set_allocated:google.protobuf.Mixin.root) } @@ -1190,10 +1222,9 @@ inline void Mixin::set_allocated_root(::std::string* root) { // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2fapi_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fapi_2eproto diff --git a/src/google/protobuf/arena.cc b/src/google/protobuf/arena.cc index 1d81c322d5..0171c56168 100644 --- a/src/google/protobuf/arena.cc +++ b/src/google/protobuf/arena.cc @@ -31,8 +31,10 @@ #include #include +#include #include +#include #ifdef ADDRESS_SANITIZER #include @@ -48,7 +50,7 @@ namespace protobuf { namespace internal { -std::atomic ArenaImpl::lifecycle_id_generator_; +std::atomic ArenaImpl::lifecycle_id_generator_; #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) ArenaImpl::ThreadCache& ArenaImpl::thread_cache() { static internal::ThreadLocalStorage* thread_cache_ = @@ -57,7 +59,7 @@ ArenaImpl::ThreadCache& ArenaImpl::thread_cache() { } #elif defined(PROTOBUF_USE_DLLS) ArenaImpl::ThreadCache& ArenaImpl::thread_cache() { - static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_ = { -1, NULL }; + static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_ = {-1, NULL}; return thread_cache_; } #else @@ -125,7 +127,7 @@ ArenaImpl::Block* ArenaImpl::NewBlock(Block* last_block, size_t min_bytes) { ArenaImpl::Block::Block(size_t size, Block* next) : next_(next), pos_(kBlockHeaderSize), size_(size) {} -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE void ArenaImpl::SerialArena::AddCleanupFallback(void* elem, void (*cleanup)(void*)) { size_t size = cleanup_ ? cleanup_->size * 2 : kMinCleanupListElements; @@ -142,10 +144,10 @@ void ArenaImpl::SerialArena::AddCleanupFallback(void* elem, AddCleanup(elem, cleanup); } -GOOGLE_PROTOBUF_ATTRIBUTE_FUNC_ALIGN(32) +PROTOBUF_FUNC_ALIGN(32) void* ArenaImpl::AllocateAligned(size_t n) { SerialArena* arena; - if (GOOGLE_PREDICT_TRUE(GetSerialArenaFast(&arena))) { + if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { return arena->AllocateAligned(n); } else { return AllocateAlignedFallback(n); @@ -155,7 +157,7 @@ void* ArenaImpl::AllocateAligned(size_t n) { void* ArenaImpl::AllocateAlignedAndAddCleanup(size_t n, void (*cleanup)(void*)) { SerialArena* arena; - if (GOOGLE_PREDICT_TRUE(GetSerialArenaFast(&arena))) { + if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { return arena->AllocateAlignedAndAddCleanup(n, cleanup); } else { return AllocateAlignedAndAddCleanupFallback(n, cleanup); @@ -164,36 +166,36 @@ void* ArenaImpl::AllocateAlignedAndAddCleanup(size_t n, void ArenaImpl::AddCleanup(void* elem, void (*cleanup)(void*)) { SerialArena* arena; - if (GOOGLE_PREDICT_TRUE(GetSerialArenaFast(&arena))) { + if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { arena->AddCleanup(elem, cleanup); } else { return AddCleanupFallback(elem, cleanup); } } -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE void* ArenaImpl::AllocateAlignedFallback(size_t n) { return GetSerialArena()->AllocateAligned(n); } -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE void* ArenaImpl::AllocateAlignedAndAddCleanupFallback(size_t n, void (*cleanup)(void*)) { return GetSerialArena()->AllocateAlignedAndAddCleanup(n, cleanup); } -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE void ArenaImpl::AddCleanupFallback(void* elem, void (*cleanup)(void*)) { GetSerialArena()->AddCleanup(elem, cleanup); } -inline GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool -ArenaImpl::GetSerialArenaFast(ArenaImpl::SerialArena** arena) { +inline PROTOBUF_ALWAYS_INLINE bool ArenaImpl::GetSerialArenaFast( + ArenaImpl::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 (GOOGLE_PREDICT_TRUE(tc->last_lifecycle_id_seen == lifecycle_id_)) { + if (PROTOBUF_PREDICT_TRUE(tc->last_lifecycle_id_seen == lifecycle_id_)) { *arena = tc->last_serial_arena; return true; } @@ -201,7 +203,7 @@ ArenaImpl::GetSerialArenaFast(ArenaImpl::SerialArena** arena) { // Check whether we own the last accessed SerialArena on this arena. This // fast path optimizes the case where a single thread uses multiple arenas. SerialArena* serial = hint_.load(std::memory_order_acquire); - if (GOOGLE_PREDICT_TRUE(serial != NULL && serial->owner() == tc)) { + if (PROTOBUF_PREDICT_TRUE(serial != NULL && serial->owner() == tc)) { *arena = serial; return true; } @@ -211,14 +213,14 @@ ArenaImpl::GetSerialArenaFast(ArenaImpl::SerialArena** arena) { ArenaImpl::SerialArena* ArenaImpl::GetSerialArena() { SerialArena* arena; - if (GOOGLE_PREDICT_TRUE(GetSerialArenaFast(&arena))) { + if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { return arena; } else { return GetSerialArenaFallback(&thread_cache()); } } -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE void* ArenaImpl::SerialArena::AllocateAlignedFallback(size_t n) { // Sync back to current's pos. head_->set_pos(head_->size() - (limit_ - ptr_)); @@ -241,7 +243,7 @@ uint64 ArenaImpl::SpaceAllocated() const { uint64 ArenaImpl::SpaceUsed() const { SerialArena* serial = threads_.load(std::memory_order_acquire); uint64 space_used = 0; - for ( ; serial; serial = serial->next()) { + for (; serial; serial = serial->next()) { space_used += serial->SpaceUsed(); } return space_used; @@ -285,7 +287,7 @@ uint64 ArenaImpl::SerialArena::Free(ArenaImpl::SerialArena* serial, // We have to be careful in this function, since we will be freeing the Block // that contains this SerialArena. Be careful about accessing |serial|. - for (Block* b = serial->head_; b; ) { + for (Block* b = serial->head_; b;) { // This is inside the block we are freeing, so we need to read it now. Block* next_block = b->next(); space_allocated += (b->size()); @@ -311,7 +313,7 @@ void ArenaImpl::CleanupList() { // properly synchronize Reset() or the destructor will throw a TSAN warning. SerialArena* serial = threads_.load(std::memory_order_relaxed); - for ( ; serial; serial = serial->next()) { + for (; serial; serial = serial->next()) { serial->CleanupList(); } } @@ -323,24 +325,22 @@ void ArenaImpl::SerialArena::CleanupList() { } void ArenaImpl::SerialArena::CleanupListFallback() { - // Cleanup newest chunk: ptrs give us length. + // 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]; - CleanupNode* node = cleanup_ptr_; - for (size_t i = 0; i < n; i++) { - --node; - node->cleanup(node->elem); - } - - // Cleanup older chunks, which are known to be full. - CleanupChunk* list = cleanup_->next; - while (list) { - size_t n = list->size; - CleanupNode* node = &list->nodes[list->size]; - for (size_t i = 0; i < n; i++) { - --node; - node->cleanup(node->elem); + 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; } } @@ -362,11 +362,11 @@ ArenaImpl::SerialArena* ArenaImpl::SerialArena::New(Block* b, void* owner, return serial; } -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE ArenaImpl::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()) { + for (; serial; serial = serial->next()) { if (serial->owner() == me) { break; } diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h index 53099ff616..d38aed7d1f 100644 --- a/src/google/protobuf/arena.h +++ b/src/google/protobuf/arena.h @@ -34,6 +34,8 @@ #define GOOGLE_PROTOBUF_ARENA_H__ #include +#include +#include #ifdef max #undef max // Visual Studio defines this macro #endif @@ -48,9 +50,9 @@ using type_info = ::type_info; #include #endif -#include #include #include +#include #include @@ -70,8 +72,8 @@ struct ArenaOptions; // defined below namespace google { namespace protobuf { -class Arena; // defined below -class Message; // defined in message.h +class Arena; // defined below +class Message; // defined in message.h class MessageLite; namespace arena_metrics { @@ -82,8 +84,8 @@ void EnableArenaMetrics(ArenaOptions* options); namespace internal { -struct ArenaStringPtr; // defined in arenastring.h -class LazyField; // defined in lazy_field.h +struct ArenaStringPtr; // defined in arenastring.h +class LazyField; // defined in lazy_field.h template class GenericTypeHandler; // defined in repeated_field.h @@ -156,13 +158,14 @@ struct ArenaOptions { private: // Hooks for adding external functionality such as user-specific metrics // collection, specific debugging abilities, etc. - // Init hook may return a pointer to a cookie to be stored in the arena. - // reset and destruction hooks will then be called with the same cookie - // pointer. This allows us to save an external object per arena instance and - // use it on the other hooks (Note: It is just as legal for init to return - // NULL and not use the cookie feature). - // on_arena_reset and on_arena_destruction also receive the space used in - // the arena just before the reset. + // Init hook (if set) will always be called at Arena init time. Init hook may + // return a pointer to a cookie to be stored in the arena. Reset and + // destruction hooks will then be called with the same cookie pointer. This + // allows us to save an external object per arena instance and use it on the + // other hooks (Note: If init hook returns NULL, the other hooks will NOT be + // called on this arena instance). + // on_arena_reset and on_arena_destruction also receive the space used in the + // arena just before the reset. void* (*on_arena_init)(Arena* arena); void (*on_arena_reset)(Arena* arena, void* cookie, uint64 space_used); void (*on_arena_destruction)(Arena* arena, void* cookie, uint64 space_used); @@ -187,7 +190,7 @@ struct ArenaOptions { // Support for non-RTTI environments. (The metrics hooks API uses type // information.) -#if GOOGLE_PROTOBUF_RTTI +#if PROTOBUF_RTTI #define RTTI_TYPE_ID(type) (&typeid(type)) #else #define RTTI_TYPE_ID(type) (NULL) @@ -242,10 +245,7 @@ struct ArenaOptions { // 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. -// -// Do NOT subclass Arena. This class will be marked as final when C++11 is -// enabled. -class LIBPROTOBUF_EXPORT Arena { +class PROTOBUF_EXPORT alignas(8) Arena final { public: // Arena constructor taking custom options. See ArenaOptions below for // descriptions of the options available. @@ -295,8 +295,7 @@ class LIBPROTOBUF_EXPORT Arena { // This function also accepts any type T that satisfies the arena message // allocation protocol, documented above. template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* CreateMessage( - Arena* arena, Args&&... args) { + 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"); @@ -322,8 +321,7 @@ class LIBPROTOBUF_EXPORT Arena { // if the object were allocated on the heap (except that the underlying memory // is obtained from the arena). template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* Create(Arena* arena, - Args&&... args) { + PROTOBUF_ALWAYS_INLINE static T* Create(Arena* arena, Args&&... args) { return CreateNoMessage(arena, is_arena_constructable(), std::forward(args)...); } @@ -335,8 +333,8 @@ class LIBPROTOBUF_EXPORT Arena { // (when compiled as C++11) that T is trivially default-constructible and // trivially destructible. template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* CreateArray( - Arena* arena, size_t num_elements) { + 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, @@ -363,8 +361,7 @@ class LIBPROTOBUF_EXPORT Arena { // // Combines SpaceAllocated and SpaceUsed. Returns a pair of // . - GOOGLE_PROTOBUF_DEPRECATED_MSG( - "Please use SpaceAllocated() and SpaceUsed()") + PROTOBUF_DEPRECATED_MSG("Please use SpaceAllocated() and SpaceUsed()") std::pair SpaceAllocatedAndUsed() const { return std::make_pair(SpaceAllocated(), SpaceUsed()); } @@ -374,7 +371,7 @@ class LIBPROTOBUF_EXPORT Arena { // 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. - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE uint64 Reset() { + PROTOBUF_NOINLINE uint64 Reset() { // Call the reset hook if (on_arena_reset_ != NULL) { on_arena_reset_(this, hooks_cookie_, impl_.SpaceAllocated()); @@ -385,7 +382,7 @@ class LIBPROTOBUF_EXPORT Arena { // Adds |object| to a list of heap-allocated objects to be freed with |delete| // when the arena is destroyed or reset. template - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE void Own(T* object) { + PROTOBUF_NOINLINE void Own(T* object) { OwnInternal(object, std::is_convertible()); } @@ -395,7 +392,7 @@ class LIBPROTOBUF_EXPORT Arena { // normally only used for objects that are placement-newed into // arena-allocated memory. template - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE void OwnDestructor(T* object) { + PROTOBUF_NOINLINE void OwnDestructor(T* object) { if (object != NULL) { impl_.AddCleanup(object, &internal::arena_destruct_object); } @@ -405,19 +402,18 @@ class LIBPROTOBUF_EXPORT Arena { // 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. - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE void OwnCustomDestructor( - void* object, void (*destruct)(void*)) { + 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. This differs from value->GetArena() in that the - // latter is a virtual call, while this method is a templated call that - // resolves at compile-time. + // 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 - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static Arena* GetArena( - const T* value) { - return GetArenaInternal(value, is_arena_constructable()); + PROTOBUF_ALWAYS_INLINE static Arena* GetArena(const T* value) { + return GetArenaInternal(value); } template @@ -444,6 +440,19 @@ class LIBPROTOBUF_EXPORT Arena { 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)...); @@ -473,9 +482,12 @@ class LIBPROTOBUF_EXPORT Arena { }; private: + template + struct has_get_arena : InternalHelper::has_get_arena {}; + template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* CreateMessageInternal( - Arena* arena, Args&&... args) { + 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"); @@ -490,8 +502,7 @@ class LIBPROTOBUF_EXPORT Arena { // slightly different. When the arena pointer is nullptr, it calls T() // instead of T(nullptr). template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* CreateMessageInternal( - Arena* arena) { + PROTOBUF_ALWAYS_INLINE static T* CreateMessageInternal(Arena* arena) { static_assert( InternalHelper::is_arena_constructable::value, "CreateMessage can only construct types that are ArenaConstructable"); @@ -503,8 +514,8 @@ class LIBPROTOBUF_EXPORT Arena { } template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* CreateInternal( - Arena* arena, Args&&... args) { + PROTOBUF_ALWAYS_INLINE static T* CreateInternal(Arena* arena, + Args&&... args) { if (arena == NULL) { return new T(std::forward(args)...); } else { @@ -516,7 +527,7 @@ class LIBPROTOBUF_EXPORT Arena { void CallDestructorHooks(); void OnArenaAllocation(const std::type_info* allocated_type, size_t n) const; inline void AllocHook(const std::type_info* allocated_type, size_t n) const { - if (GOOGLE_PREDICT_FALSE(hooks_cookie_ != NULL)) { + if (PROTOBUF_PREDICT_FALSE(hooks_cookie_ != NULL)) { OnArenaAllocation(allocated_type, n); } } @@ -525,8 +536,7 @@ class LIBPROTOBUF_EXPORT Arena { // allocated type info when the hooks are in place in ArenaOptions and // the cookie is not null. template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void* AllocateInternal( - bool skip_explicit_ownership) { + PROTOBUF_ALWAYS_INLINE void* AllocateInternal(bool skip_explicit_ownership) { const size_t n = internal::AlignUpTo8(sizeof(T)); AllocHook(RTTI_TYPE_ID(T), n); // Monitor allocation if needed. @@ -544,27 +554,29 @@ class LIBPROTOBUF_EXPORT Arena { // user code. These are used only internally from LazyField and Repeated // fields, since they are designed to work in all mode combinations. template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static Msg* DoCreateMaybeMessage( - Arena* arena, std::true_type, Args&&... args) { + PROTOBUF_ALWAYS_INLINE static Msg* DoCreateMaybeMessage(Arena* arena, + std::true_type, + Args&&... args) { return CreateMessageInternal(arena, std::forward(args)...); } template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* DoCreateMaybeMessage( - Arena* arena, std::false_type, Args&&... args) { + PROTOBUF_ALWAYS_INLINE static T* DoCreateMaybeMessage(Arena* arena, + std::false_type, + Args&&... args) { return CreateInternal(arena, std::forward(args)...); } template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* CreateMaybeMessage( - Arena* arena, Args&&... args) { + PROTOBUF_ALWAYS_INLINE static T* CreateMaybeMessage(Arena* arena, + Args&&... args) { return DoCreateMaybeMessage(arena, is_arena_constructable(), std::forward(args)...); } template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* CreateNoMessage( - Arena* arena, std::true_type, Args&&... args) { + 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. @@ -572,8 +584,9 @@ class LIBPROTOBUF_EXPORT Arena { } template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static T* CreateNoMessage( - Arena* arena, std::false_type, Args&&... args) { + 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. @@ -583,8 +596,7 @@ class LIBPROTOBUF_EXPORT Arena { // Just allocate the required size for the given type assuming the // type has a trivial constructor. template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE T* CreateInternalRawArray( - size_t num_elements) { + 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."; const size_t n = internal::AlignUpTo8(sizeof(T) * num_elements); @@ -594,13 +606,13 @@ class LIBPROTOBUF_EXPORT Arena { } template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE T* DoCreate( - bool skip_explicit_ownership, Args&&... args) { + PROTOBUF_ALWAYS_INLINE T* DoCreate(bool skip_explicit_ownership, + Args&&... args) { return new (AllocateInternal(skip_explicit_ownership)) T(std::forward(args)...); } template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE T* DoCreateMessage(Args&&... args) { + PROTOBUF_ALWAYS_INLINE T* DoCreateMessage(Args&&... args) { return InternalHelper::Construct( AllocateInternal(InternalHelper::is_destructor_skippable::value), this, std::forward(args)...); @@ -644,15 +656,13 @@ class LIBPROTOBUF_EXPORT Arena { // all template instantiations to one for generic Message reduces code size, // using the virtual destructor instead. template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void OwnInternal(T* object, - std::true_type) { + PROTOBUF_ALWAYS_INLINE void OwnInternal(T* object, std::true_type) { if (object != NULL) { impl_.AddCleanup(object, &internal::arena_delete_object); } } template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void OwnInternal(T* object, - std::false_type) { + PROTOBUF_ALWAYS_INLINE void OwnInternal(T* object, std::false_type) { if (object != NULL) { impl_.AddCleanup(object, &internal::arena_delete_object); } @@ -661,16 +671,25 @@ class LIBPROTOBUF_EXPORT Arena { // Implementation for GetArena(). Only message objects with // InternalArenaConstructable_ tags can be associated with an arena, and such // objects must implement a GetArenaNoVirtual() method. - template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static Arena* GetArenaInternal( - const T* value, std::true_type) { + template ::value, int>::type = 0> + PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) { return InternalHelper::GetArena(value); } - - template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE static Arena* GetArenaInternal( - const T* /* value */, std::false_type) { - return NULL; + 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. diff --git a/src/google/protobuf/arena_impl.h b/src/google/protobuf/arena_impl.h index 0b71819d29..0beec171f7 100644 --- a/src/google/protobuf/arena_impl.h +++ b/src/google/protobuf/arena_impl.h @@ -52,16 +52,18 @@ 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) & -8; + return (n + 7) & static_cast(-8); } +using LifecycleId = int64_t; + // 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 LIBPROTOBUF_EXPORT ArenaImpl { +class PROTOBUF_EXPORT ArenaImpl { public: struct Options { size_t start_block_size; @@ -73,12 +75,12 @@ class LIBPROTOBUF_EXPORT ArenaImpl { template explicit Options(const O& options) - : start_block_size(options.start_block_size), - max_block_size(options.max_block_size), - initial_block(options.initial_block), - initial_block_size(options.initial_block_size), - block_alloc(options.block_alloc), - block_dealloc(options.block_dealloc) {} + : start_block_size(options.start_block_size), + max_block_size(options.max_block_size), + initial_block(options.initial_block), + initial_block_size(options.initial_block_size), + block_alloc(options.block_alloc), + block_dealloc(options.block_dealloc) {} }; template @@ -137,7 +139,7 @@ class LIBPROTOBUF_EXPORT ArenaImpl { class Block; // A thread-unsafe Arena that can only be used within its owning thread. - class LIBPROTOBUF_EXPORT SerialArena { + class PROTOBUF_EXPORT SerialArena { public: // 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 @@ -157,7 +159,7 @@ class LIBPROTOBUF_EXPORT ArenaImpl { void* AllocateAligned(size_t n) { GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. GOOGLE_DCHECK_GE(limit_, ptr_); - if (GOOGLE_PREDICT_FALSE(static_cast(limit_ - ptr_) < n)) { + if (PROTOBUF_PREDICT_FALSE(static_cast(limit_ - ptr_) < n)) { return AllocateAlignedFallback(n); } void* ret = ptr_; @@ -169,7 +171,7 @@ class LIBPROTOBUF_EXPORT ArenaImpl { } void AddCleanup(void* elem, void (*cleanup)(void*)) { - if (GOOGLE_PREDICT_FALSE(cleanup_ptr_ == cleanup_limit_)) { + if (PROTOBUF_PREDICT_FALSE(cleanup_ptr_ == cleanup_limit_)) { AddCleanupFallback(elem, cleanup); return; } @@ -193,11 +195,11 @@ class LIBPROTOBUF_EXPORT ArenaImpl { void AddCleanupFallback(void* elem, void (*cleanup)(void*)); void CleanupListFallback(); - 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. + 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 @@ -212,7 +214,7 @@ class LIBPROTOBUF_EXPORT ArenaImpl { // Blocks are variable length malloc-ed objects. The following structure // describes the common header for all blocks. - class LIBPROTOBUF_EXPORT Block { + class PROTOBUF_EXPORT Block { public: Block(size_t size, Block* next); @@ -227,7 +229,7 @@ class LIBPROTOBUF_EXPORT ArenaImpl { void set_pos(size_t pos) { pos_ = pos; } private: - Block* next_; // Next block for this thread. + Block* next_; // Next block for this thread. size_t pos_; size_t size_; // data follows @@ -243,10 +245,10 @@ class LIBPROTOBUF_EXPORT ArenaImpl { // The ThreadCache is considered valid as long as this matches the // lifecycle_id of the arena being used. - int64 last_lifecycle_id_seen; + LifecycleId last_lifecycle_id_seen; SerialArena* last_serial_arena; }; - static std::atomic lifecycle_id_generator_; + static std::atomic lifecycle_id_generator_; #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) // Android ndk does not support GOOGLE_THREAD_LOCAL keyword so we use a custom thread // local storage class we implemented. @@ -279,21 +281,20 @@ class LIBPROTOBUF_EXPORT ArenaImpl { hint_.store(serial, std::memory_order_release); } - std::atomic threads_; // Pointer to a linked list of SerialArena. std::atomic hint_; // Fast thread-local block access std::atomic space_allocated_; // Total size of all allocated blocks. - Block *initial_block_; // If non-NULL, points to the block that came from - // user data. + Block* initial_block_; // If non-NULL, points to the block that came from + // user data. Block* NewBlock(Block* last_block, size_t min_bytes); SerialArena* GetSerialArena(); bool GetSerialArenaFast(SerialArena** arena); SerialArena* GetSerialArenaFallback(void* me); - int64 lifecycle_id_; // Unique for each arena. Changes on Reset(). + LifecycleId lifecycle_id_; // Unique for each arena. Changes on Reset(). Options options_; @@ -306,8 +307,10 @@ class LIBPROTOBUF_EXPORT ArenaImpl { 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 const size_t kBlockHeaderSize = (sizeof(Block) + 7) & -8; - static const size_t kSerialArenaSize = (sizeof(SerialArena) + 7) & -8; + static const size_t kBlockHeaderSize = + (sizeof(Block) + 7) & static_cast(-8); + static const size_t kSerialArenaSize = + (sizeof(SerialArena) + 7) & static_cast(-8); static_assert(kBlockHeaderSize % 8 == 0, "kBlockHeaderSize must be a multiple of 8."); static_assert(kSerialArenaSize % 8 == 0, diff --git a/src/google/protobuf/arena_test_util.cc b/src/google/protobuf/arena_test_util.cc index df9c5bd6b4..da32960ce1 100644 --- a/src/google/protobuf/arena_test_util.cc +++ b/src/google/protobuf/arena_test_util.cc @@ -28,9 +28,9 @@ // (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 #define EXPECT_EQ GOOGLE_CHECK_EQ diff --git a/src/google/protobuf/arena_test_util.h b/src/google/protobuf/arena_test_util.h index 9c821b90d7..84df34989c 100644 --- a/src/google/protobuf/arena_test_util.h +++ b/src/google/protobuf/arena_test_util.h @@ -33,6 +33,8 @@ #include #include +#include +#include #include namespace google { @@ -41,7 +43,15 @@ namespace protobuf { template void TestParseCorruptedString(const T& message) { int success_count = 0; - string s = message.SerializeAsString(); + std::string s; + { + // Map order is not deterministic. To make the test deterministic we want + // to serialize the proto deterministically. + io::StringOutputStream output(&s); + io::CodedOutputStream out(&output); + out.SetSerializationDeterministic(true); + message.SerializePartialToCodedStream(&out); + } const int kMaxIters = 900; const int stride = s.size() <= kMaxIters ? 1 : s.size() / kMaxIters; const int start = stride == 1 || use_arena ? 0 : (stride + 1) / 2; @@ -68,10 +78,9 @@ namespace internal { class NoHeapChecker { public: - NoHeapChecker() { - capture_alloc.Hook(); - } + NoHeapChecker() { capture_alloc.Hook(); } ~NoHeapChecker(); + private: class NewDeleteCapture { public: diff --git a/src/google/protobuf/arena_unittest.cc b/src/google/protobuf/arena_unittest.cc index 92e5c84d2c..7e5b115633 100644 --- a/src/google/protobuf/arena_unittest.cc +++ b/src/google/protobuf/arena_unittest.cc @@ -55,14 +55,15 @@ #include #include #include - +#include using proto2_arena_unittest::ArenaMessage; -using protobuf_unittest::TestAllTypes; using protobuf_unittest::TestAllExtensions; -using protobuf_unittest::TestOneof2; +using protobuf_unittest::TestAllTypes; using protobuf_unittest::TestEmptyMessage; +using protobuf_unittest::TestOneof2; +using protobuf_unittest_no_arena::TestNoArenaMessage; namespace google { namespace protobuf { @@ -70,12 +71,8 @@ namespace protobuf { class Notifier { public: Notifier() : count_(0) {} - void Notify() { - count_++; - } - int GetCount() { - return count_; - } + void Notify() { count_++; } + int GetCount() { return count_; } private: int count_; @@ -84,14 +81,13 @@ class Notifier { class SimpleDataType { public: SimpleDataType() : notifier_(NULL) {} - void SetNotifier(Notifier* notifier) { - notifier_ = notifier; - } + void SetNotifier(Notifier* notifier) { notifier_ = notifier; } virtual ~SimpleDataType() { if (notifier_ != NULL) { notifier_->Notify(); } }; + private: Notifier* notifier_; }; @@ -112,14 +108,14 @@ class PleaseDontCopyMe { // A class that takes four different types as constructor arguments. class MustBeConstructedWithOneThroughFour { public: - MustBeConstructedWithOneThroughFour( - int one, const char* two, const string& three, - const PleaseDontCopyMe* four) + MustBeConstructedWithOneThroughFour(int one, const char* two, + const std::string& three, + const PleaseDontCopyMe* four) : one_(one), two_(two), three_(three), four_(four) {} int one_; const char* const two_; - string three_; + std::string three_; const PleaseDontCopyMe* four_; private: @@ -129,21 +125,29 @@ class MustBeConstructedWithOneThroughFour { // A class that takes eight different types as constructor arguments. class MustBeConstructedWithOneThroughEight { public: - MustBeConstructedWithOneThroughEight( - int one, const char* two, const string& three, - const PleaseDontCopyMe* four, int five, const char* six, - const string& seven, const string& eight) - : one_(one), two_(two), three_(three), four_(four), five_(five), - six_(six), seven_(seven), eight_(eight) {} + MustBeConstructedWithOneThroughEight(int one, const char* two, + const std::string& three, + const PleaseDontCopyMe* four, int five, + const char* six, + const std::string& seven, + const std::string& eight) + : one_(one), + two_(two), + three_(three), + four_(four), + five_(five), + six_(six), + seven_(seven), + eight_(eight) {} int one_; const char* const two_; - string three_; + std::string three_; const PleaseDontCopyMe* four_; int five_; const char* const six_; - string seven_; - string eight_; + std::string seven_; + std::string eight_; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MustBeConstructedWithOneThroughEight); @@ -152,16 +156,14 @@ class MustBeConstructedWithOneThroughEight { TEST(ArenaTest, ArenaConstructable) { EXPECT_TRUE(Arena::is_arena_constructable::type::value); EXPECT_TRUE(Arena::is_arena_constructable::type::value); - EXPECT_FALSE(Arena::is_arena_constructable< - protobuf_unittest_no_arena::TestNoArenaMessage>::type::value); + EXPECT_FALSE(Arena::is_arena_constructable::type::value); EXPECT_FALSE(Arena::is_arena_constructable::type::value); } TEST(ArenaTest, DestructorSkippable) { EXPECT_TRUE(Arena::is_destructor_skippable::type::value); EXPECT_TRUE(Arena::is_destructor_skippable::type::value); - EXPECT_FALSE(Arena::is_destructor_skippable< - protobuf_unittest_no_arena::TestNoArenaMessage>::type::value); + EXPECT_FALSE(Arena::is_destructor_skippable::type::value); EXPECT_FALSE(Arena::is_destructor_skippable::type::value); } @@ -171,12 +173,12 @@ TEST(ArenaTest, BasicCreate) { EXPECT_TRUE(Arena::Create(&arena) != NULL); EXPECT_TRUE(Arena::Create(&arena) != NULL); EXPECT_TRUE(Arena::Create(&arena) != NULL); - EXPECT_TRUE(Arena::Create(&arena) != NULL); + EXPECT_TRUE(Arena::Create(&arena) != NULL); arena.Own(new int32); arena.Own(new int64); arena.Own(new float); arena.Own(new double); - arena.Own(new string); + arena.Own(new std::string); arena.Own(NULL); Notifier notifier; SimpleDataType* data = Arena::Create(&arena); @@ -190,8 +192,8 @@ TEST(ArenaTest, BasicCreate) { TEST(ArenaTest, CreateAndConstCopy) { Arena arena; - const string s("foo"); - const string* s_copy = Arena::Create(&arena, s); + const std::string s("foo"); + const std::string* s_copy = Arena::Create(&arena, s); EXPECT_TRUE(s_copy != NULL); EXPECT_EQ("foo", s); EXPECT_EQ("foo", *s_copy); @@ -199,8 +201,8 @@ TEST(ArenaTest, CreateAndConstCopy) { TEST(ArenaTest, CreateAndNonConstCopy) { Arena arena; - string s("foo"); - const string* s_copy = Arena::Create(&arena, s); + std::string s("foo"); + const std::string* s_copy = Arena::Create(&arena, s); EXPECT_TRUE(s_copy != NULL); EXPECT_EQ("foo", s); EXPECT_EQ("foo", *s_copy); @@ -208,8 +210,8 @@ TEST(ArenaTest, CreateAndNonConstCopy) { TEST(ArenaTest, CreateAndMove) { Arena arena; - string s("foo"); - const string* s_move = Arena::Create(&arena, std::move(s)); + std::string s("foo"); + const std::string* s_move = Arena::Create(&arena, std::move(s)); EXPECT_TRUE(s_move != NULL); EXPECT_TRUE(s.empty()); // NOLINT EXPECT_EQ("foo", *s_move); @@ -217,11 +219,11 @@ TEST(ArenaTest, CreateAndMove) { TEST(ArenaTest, CreateWithFourConstructorArguments) { Arena arena; - const string three("3"); + const std::string three("3"); const PleaseDontCopyMe four(4); const MustBeConstructedWithOneThroughFour* new_object = - Arena::Create( - &arena, 1, "2", three, &four); + Arena::Create(&arena, 1, "2", three, + &four); EXPECT_TRUE(new_object != NULL); ASSERT_EQ(1, new_object->one_); ASSERT_STREQ("2", new_object->two_); @@ -231,10 +233,10 @@ TEST(ArenaTest, CreateWithFourConstructorArguments) { TEST(ArenaTest, CreateWithEightConstructorArguments) { Arena arena; - const string three("3"); + const std::string three("3"); const PleaseDontCopyMe four(4); - const string seven("7"); - const string eight("8"); + const std::string seven("7"); + const std::string eight("8"); const MustBeConstructedWithOneThroughEight* new_object = Arena::Create( &arena, 1, "2", three, &four, 5, "6", seven, eight); @@ -251,14 +253,14 @@ TEST(ArenaTest, CreateWithEightConstructorArguments) { class PleaseMoveMe { public: - explicit PleaseMoveMe(const string& value) : value_(value) {} + explicit PleaseMoveMe(const std::string& value) : value_(value) {} PleaseMoveMe(PleaseMoveMe&&) = default; PleaseMoveMe(const PleaseMoveMe&) = delete; - const string& value() const { return value_; } + const std::string& value() const { return value_; } private: - string value_; + std::string value_; }; TEST(ArenaTest, CreateWithMoveArguments) { @@ -411,7 +413,7 @@ TEST(ArenaTest, ReflectionSwapFields) { reflection->SwapFields(arena1_message, arena2_message, fields); EXPECT_EQ(&arena1, arena1_message->GetArena()); EXPECT_EQ(&arena2, arena2_message->GetArena()); - string output; + std::string output; arena1_message->SerializeToString(&output); EXPECT_EQ(0, output.size()); TestUtil::ExpectAllFieldsSet(*arena2_message); @@ -455,13 +457,13 @@ TEST(ArenaTest, ReflectionSwapFields) { TEST(ArenaTest, SetAllocatedMessage) { Arena arena; - TestAllTypes *arena_message = Arena::CreateMessage(&arena); + TestAllTypes* arena_message = Arena::CreateMessage(&arena); TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage; nested->set_bb(118); arena_message->set_allocated_optional_nested_message(nested); EXPECT_EQ(118, arena_message->optional_nested_message().bb()); - protobuf_unittest_no_arena::TestNoArenaMessage no_arena_message; + TestNoArenaMessage no_arena_message; EXPECT_FALSE(no_arena_message.has_arena_message()); no_arena_message.set_allocated_arena_message(NULL); EXPECT_FALSE(no_arena_message.has_arena_message()); @@ -485,7 +487,7 @@ TEST(ArenaTest, ReleaseMessage) { TEST(ArenaTest, SetAllocatedString) { Arena arena; TestAllTypes* arena_message = Arena::CreateMessage(&arena); - string* allocated_str = new string("hello"); + std::string* allocated_str = new std::string("hello"); arena_message->set_allocated_optional_string(allocated_str); EXPECT_EQ("hello", arena_message->optional_string()); } @@ -494,7 +496,7 @@ TEST(ArenaTest, ReleaseString) { Arena arena; TestAllTypes* arena_message = Arena::CreateMessage(&arena); arena_message->set_optional_string("hello"); - std::unique_ptr released_str( + std::unique_ptr released_str( arena_message->release_optional_string()); EXPECT_EQ("hello", *released_str); @@ -510,7 +512,7 @@ TEST(ArenaTest, SwapBetweenArenasWithAllFieldsSet) { TestAllTypes* arena2_message = Arena::CreateMessage(&arena2); TestUtil::SetAllFields(arena2_message); arena2_message->Swap(arena1_message); - string output; + std::string output; arena2_message->SerializeToString(&output); EXPECT_EQ(0, output.size()); } @@ -548,7 +550,7 @@ TEST(ArenaTest, SwapBetweenArenasUsingReflection) { TestUtil::SetAllFields(arena2_message); const Reflection* r = arena2_message->GetReflection(); r->Swap(arena1_message, arena2_message); - string output; + std::string output; arena2_message->SerializeToString(&output); EXPECT_EQ(0, output.size()); } @@ -571,7 +573,7 @@ TEST(ArenaTest, SwapBetweenArenaAndNonArenaUsingReflection) { TEST(ArenaTest, ReleaseFromArenaMessageMakesCopy) { TestAllTypes::NestedMessage* nested_msg = NULL; - string* nested_string = NULL; + std::string* nested_string = NULL; { Arena arena; TestAllTypes* arena_message = Arena::CreateMessage(&arena); @@ -586,7 +588,7 @@ TEST(ArenaTest, ReleaseFromArenaMessageMakesCopy) { delete nested_string; } -#if GOOGLE_PROTOBUF_RTTI +#if PROTOBUF_RTTI TEST(ArenaTest, ReleaseFromArenaMessageUsingReflectionMakesCopy) { TestAllTypes::NestedMessage* nested_msg = NULL; // Note: no string: reflection API only supports releasing submessages. @@ -603,7 +605,7 @@ TEST(ArenaTest, ReleaseFromArenaMessageUsingReflectionMakesCopy) { EXPECT_EQ(42, nested_msg->bb()); delete nested_msg; } -#endif // GOOGLE_PROTOBUF_RTTI +#endif // PROTOBUF_RTTI TEST(ArenaTest, SetAllocatedAcrossArenas) { Arena arena1; @@ -613,8 +615,7 @@ TEST(ArenaTest, SetAllocatedAcrossArenas) { heap_submessage->set_bb(42); arena1_message->set_allocated_optional_nested_message(heap_submessage); // Should keep same object and add to arena's Own()-list. - EXPECT_EQ(heap_submessage, - arena1_message->mutable_optional_nested_message()); + EXPECT_EQ(heap_submessage, arena1_message->mutable_optional_nested_message()); { Arena arena2; TestAllTypes::NestedMessage* arena2_submessage = @@ -630,8 +631,7 @@ TEST(ArenaTest, SetAllocatedAcrossArenas) { arena1_submessage->set_bb(42); TestAllTypes* heap_message = new TestAllTypes; heap_message->set_allocated_optional_nested_message(arena1_submessage); - EXPECT_NE(arena1_submessage, - heap_message->mutable_optional_nested_message()); + EXPECT_NE(arena1_submessage, heap_message->mutable_optional_nested_message()); delete heap_message; } @@ -641,15 +641,14 @@ TEST(ArenaTest, SetAllocatedAcrossArenasWithReflection) { TestAllTypes* arena1_message = Arena::CreateMessage(&arena1); const Reflection* r = arena1_message->GetReflection(); const Descriptor* d = arena1_message->GetDescriptor(); - const FieldDescriptor* msg_field = d->FindFieldByName( - "optional_nested_message"); + const FieldDescriptor* msg_field = + d->FindFieldByName("optional_nested_message"); TestAllTypes::NestedMessage* heap_submessage = new TestAllTypes::NestedMessage(); heap_submessage->set_bb(42); r->SetAllocatedMessage(arena1_message, heap_submessage, msg_field); // Should keep same object and add to arena's Own()-list. - EXPECT_EQ(heap_submessage, - arena1_message->mutable_optional_nested_message()); + EXPECT_EQ(heap_submessage, arena1_message->mutable_optional_nested_message()); { Arena arena2; TestAllTypes::NestedMessage* arena2_submessage = @@ -665,8 +664,7 @@ TEST(ArenaTest, SetAllocatedAcrossArenasWithReflection) { arena1_submessage->set_bb(42); TestAllTypes* heap_message = new TestAllTypes; r->SetAllocatedMessage(heap_message, arena1_submessage, msg_field); - EXPECT_NE(arena1_submessage, - heap_message->mutable_optional_nested_message()); + EXPECT_NE(arena1_submessage, heap_message->mutable_optional_nested_message()); delete heap_message; } @@ -721,11 +719,10 @@ TEST(ArenaTest, AddAllocatedToRepeatedField) { TestAllTypes::NestedMessage* heap_submessage = new TestAllTypes::NestedMessage(); heap_submessage->set_bb(42); - arena1_message->mutable_repeated_nested_message()-> - AddAllocated(heap_submessage); + arena1_message->mutable_repeated_nested_message()->AddAllocated( + heap_submessage); // Should not copy object -- will use arena_->Own(). - EXPECT_EQ(heap_submessage, - &arena1_message->repeated_nested_message(i)); + EXPECT_EQ(heap_submessage, &arena1_message->repeated_nested_message(i)); EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb()); } @@ -736,11 +733,10 @@ TEST(ArenaTest, AddAllocatedToRepeatedField) { TestAllTypes::NestedMessage* arena2_submessage = Arena::CreateMessage(&arena2); arena2_submessage->set_bb(42); - arena1_message->mutable_repeated_nested_message()-> - AddAllocated(arena2_submessage); + arena1_message->mutable_repeated_nested_message()->AddAllocated( + arena2_submessage); // Should copy object. - EXPECT_NE(arena2_submessage, - &arena1_message->repeated_nested_message(i)); + EXPECT_NE(arena2_submessage, &arena1_message->repeated_nested_message(i)); EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb()); } @@ -751,11 +747,10 @@ TEST(ArenaTest, AddAllocatedToRepeatedField) { TestAllTypes::NestedMessage* arena2_submessage = Arena::CreateMessage(&arena2); arena2_submessage->set_bb(42); - heap_message->mutable_repeated_nested_message()-> - AddAllocated(arena2_submessage); + heap_message->mutable_repeated_nested_message()->AddAllocated( + arena2_submessage); // Should copy object. - EXPECT_NE(arena2_submessage, - &heap_message->repeated_nested_message(i)); + EXPECT_NE(arena2_submessage, &heap_message->repeated_nested_message(i)); EXPECT_EQ(42, heap_message->repeated_nested_message(i).bb()); } delete heap_message; @@ -763,9 +758,8 @@ TEST(ArenaTest, AddAllocatedToRepeatedField) { // Heap-arena case for strings (which are not arena-allocated). arena1_message->Clear(); for (int i = 0; i < 10; i++) { - string* s = new string("Test"); - arena1_message->mutable_repeated_string()-> - AddAllocated(s); + std::string* s = new std::string("Test"); + arena1_message->mutable_repeated_string()->AddAllocated(s); // Should not copy. EXPECT_EQ(s, &arena1_message->repeated_string(i)); EXPECT_EQ("Test", arena1_message->repeated_string(i)); @@ -778,16 +772,14 @@ TEST(ArenaTest, AddAllocatedToRepeatedFieldViaReflection) { TestAllTypes* arena1_message = Arena::CreateMessage(&arena1); const Reflection* r = arena1_message->GetReflection(); const Descriptor* d = arena1_message->GetDescriptor(); - const FieldDescriptor* fd = - d->FindFieldByName("repeated_nested_message"); + const FieldDescriptor* fd = d->FindFieldByName("repeated_nested_message"); for (int i = 0; i < 10; i++) { TestAllTypes::NestedMessage* heap_submessage = new TestAllTypes::NestedMessage; heap_submessage->set_bb(42); r->AddAllocatedMessage(arena1_message, fd, heap_submessage); // Should not copy object -- will use arena_->Own(). - EXPECT_EQ(heap_submessage, - &arena1_message->repeated_nested_message(i)); + EXPECT_EQ(heap_submessage, &arena1_message->repeated_nested_message(i)); EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb()); } @@ -800,8 +792,7 @@ TEST(ArenaTest, AddAllocatedToRepeatedFieldViaReflection) { arena2_submessage->set_bb(42); r->AddAllocatedMessage(arena1_message, fd, arena2_submessage); // Should copy object. - EXPECT_NE(arena2_submessage, - &arena1_message->repeated_nested_message(i)); + EXPECT_NE(arena2_submessage, &arena1_message->repeated_nested_message(i)); EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb()); } @@ -814,8 +805,7 @@ TEST(ArenaTest, AddAllocatedToRepeatedFieldViaReflection) { arena2_submessage->set_bb(42); r->AddAllocatedMessage(heap_message, fd, arena2_submessage); // Should copy object. - EXPECT_NE(arena2_submessage, - &heap_message->repeated_nested_message(i)); + EXPECT_NE(arena2_submessage, &heap_message->repeated_nested_message(i)); EXPECT_EQ(42, heap_message->repeated_nested_message(i).bb()); } delete heap_message; @@ -834,9 +824,9 @@ TEST(ArenaTest, ReleaseLastRepeatedField) { } for (int i = 0; i < 10; i++) { - const TestAllTypes::NestedMessage *orig_submessage = + const TestAllTypes::NestedMessage* orig_submessage = &arena_message->repeated_nested_message(10 - 1 - i); // last element - TestAllTypes::NestedMessage *released = + TestAllTypes::NestedMessage* released = arena_message->mutable_repeated_nested_message()->ReleaseLast(); EXPECT_NE(released, orig_submessage); EXPECT_EQ(42, released->bb()); @@ -852,29 +842,31 @@ TEST(ArenaTest, ReleaseLastRepeatedField) { } for (int i = 0; i < 10; i++) { - const TestAllTypes::NestedMessage *orig_submessage = + const TestAllTypes::NestedMessage* orig_submessage = &arena_message->repeated_nested_message(10 - 1 - i); // last element - TestAllTypes::NestedMessage *released = - arena_message->mutable_repeated_nested_message()-> - UnsafeArenaReleaseLast(); + TestAllTypes::NestedMessage* released = + arena_message->mutable_repeated_nested_message() + ->UnsafeArenaReleaseLast(); EXPECT_EQ(released, orig_submessage); EXPECT_EQ(42, released->bb()); // no delete -- |released| is on the arena. } - // Test string case as well. ReleaseLast() in this case must copy the string, - // even though it was originally heap-allocated and its pointer was simply - // appended to the repeated field's internal vector, because the string was - // placed on the arena's destructor list and cannot be removed from that list - // (so the arena permanently owns the original instance). + // Test string case as well. ReleaseLast() in this case must copy the + // string, even though it was originally heap-allocated and its pointer + // was simply appended to the repeated field's internal vector, because the + // string was placed on the arena's destructor list and cannot be removed + // from that list (so the arena permanently owns the original instance). arena_message->Clear(); for (int i = 0; i < 10; i++) { - string* s = new string("Test"); + std::string* s = new std::string("Test"); arena_message->mutable_repeated_string()->AddAllocated(s); } for (int i = 0; i < 10; i++) { - const string* orig_element = &arena_message->repeated_string(10 - 1 - i); - string* released = arena_message->mutable_repeated_string()->ReleaseLast(); + const std::string* orig_element = + &arena_message->repeated_string(10 - 1 - i); + std::string* released = + arena_message->mutable_repeated_string()->ReleaseLast(); EXPECT_NE(released, orig_element); EXPECT_EQ("Test", *released); delete released; @@ -889,7 +881,7 @@ TEST(ArenaTest, UnsafeArenaReleaseAdd) { Arena arena; TestAllTypes* message1 = Arena::CreateMessage(&arena); TestAllTypes* message2 = Arena::CreateMessage(&arena); - string* arena_string = Arena::Create(&arena); + std::string* arena_string = Arena::Create(&arena); *arena_string = kContent; message1->unsafe_arena_set_allocated_optional_string(arena_string); @@ -902,7 +894,7 @@ TEST(ArenaTest, UnsafeArenaAddAllocated) { Arena arena; TestAllTypes* message = Arena::CreateMessage(&arena); for (int i = 0; i < 10; i++) { - string* arena_string = Arena::Create(&arena); + std::string* arena_string = Arena::Create(&arena); message->mutable_repeated_string()->UnsafeArenaAddAllocated(arena_string); EXPECT_EQ(arena_string, message->mutable_repeated_string(i)); } @@ -912,7 +904,7 @@ TEST(ArenaTest, UnsafeArenaRelease) { Arena arena; TestAllTypes* message = Arena::CreateMessage(&arena); - string* s = new string("test string"); + std::string* s = new std::string("test string"); message->unsafe_arena_set_allocated_optional_string(s); EXPECT_TRUE(message->has_optional_string()); EXPECT_EQ("test string", message->optional_string()); @@ -920,7 +912,7 @@ TEST(ArenaTest, UnsafeArenaRelease) { EXPECT_FALSE(message->has_optional_string()); delete s; - s = new string("test string"); + s = new std::string("test string"); message->unsafe_arena_set_allocated_oneof_string(s); EXPECT_TRUE(message->has_oneof_string()); EXPECT_EQ("test string", message->oneof_string()); @@ -934,9 +926,9 @@ TEST(ArenaTest, OneofMerge) { TestAllTypes* message0 = Arena::CreateMessage(&arena); TestAllTypes* message1 = Arena::CreateMessage(&arena); - message0->unsafe_arena_set_allocated_oneof_string(new string("x")); + message0->unsafe_arena_set_allocated_oneof_string(new std::string("x")); ASSERT_TRUE(message0->has_oneof_string()); - message1->unsafe_arena_set_allocated_oneof_string(new string("y")); + message1->unsafe_arena_set_allocated_oneof_string(new std::string("y")); ASSERT_TRUE(message1->has_oneof_string()); EXPECT_EQ("x", message0->oneof_string()); EXPECT_EQ("y", message1->oneof_string()); @@ -953,12 +945,10 @@ TEST(ArenaTest, ArenaOneofReflection) { const Descriptor* desc = message->GetDescriptor(); const Reflection* refl = message->GetReflection(); - const FieldDescriptor* string_field = desc->FindFieldByName( - "oneof_string"); - const FieldDescriptor* msg_field = desc->FindFieldByName( - "oneof_nested_message"); - const OneofDescriptor* oneof = desc->FindOneofByName( - "oneof_field"); + const FieldDescriptor* string_field = desc->FindFieldByName("oneof_string"); + const FieldDescriptor* msg_field = + desc->FindFieldByName("oneof_nested_message"); + const OneofDescriptor* oneof = desc->FindOneofByName("oneof_field"); refl->SetString(message, string_field, "Test value"); EXPECT_TRUE(refl->HasOneof(*message, oneof)); @@ -1005,8 +995,8 @@ void TestSwapRepeatedField(Arena* arena1, Arena* arena2) { field1.Swap(&field2); EXPECT_EQ(5, field1.size()); EXPECT_EQ(10, field2.size()); - EXPECT_TRUE(string("field1") == field2.Get(0).optional_string()); - EXPECT_TRUE(string("field2") == field1.Get(0).optional_string()); + EXPECT_TRUE(std::string("field1") == field2.Get(0).optional_string()); + EXPECT_TRUE(std::string("field2") == field1.Get(0).optional_string()); // Ensure that fields retained their original order: for (int i = 0; i < field1.size(); i++) { EXPECT_EQ(i, field1.Get(i).optional_int32()); @@ -1042,12 +1032,12 @@ TEST(ArenaTest, ExtensionsOnArena) { // Ensure no leaks. TestAllExtensions* message_ext = Arena::CreateMessage(&arena); - message_ext->SetExtension( - protobuf_unittest::optional_int32_extension, 42); - message_ext->SetExtension( - protobuf_unittest::optional_string_extension, string("test")); - message_ext->MutableExtension( - protobuf_unittest::optional_nested_message_extension)->set_bb(42); + message_ext->SetExtension(protobuf_unittest::optional_int32_extension, 42); + message_ext->SetExtension(protobuf_unittest::optional_string_extension, + std::string("test")); + message_ext + ->MutableExtension(protobuf_unittest::optional_nested_message_extension) + ->set_bb(42); } TEST(ArenaTest, RepeatedFieldOnArena) { @@ -1110,7 +1100,7 @@ TEST(ArenaTest, RepeatedFieldOnArena) { // code that may allocate messages or repeated fields of messages on an arena. { RepeatedPtrField* repeated_ptr_on_arena = - Arena::CreateMessage< RepeatedPtrField >(&arena); + Arena::CreateMessage >(&arena); for (int i = 0; i < 10; i++) { // Add some elements and let the leak-checker ensure that everything is // freed. @@ -1118,7 +1108,7 @@ TEST(ArenaTest, RepeatedFieldOnArena) { } RepeatedField* repeated_int_on_arena = - Arena::CreateMessage< RepeatedField >(&arena); + Arena::CreateMessage >(&arena); for (int i = 0; i < 100; i++) { repeated_int_on_arena->Add(i); } @@ -1129,7 +1119,7 @@ TEST(ArenaTest, RepeatedFieldOnArena) { } -#if GOOGLE_PROTOBUF_RTTI +#if PROTOBUF_RTTI TEST(ArenaTest, MutableMessageReflection) { Arena arena; TestAllTypes* message = Arena::CreateMessage(&arena); @@ -1145,7 +1135,8 @@ TEST(ArenaTest, MutableMessageReflection) { EXPECT_EQ(submessage_expected, submessage); EXPECT_EQ(&arena, submessage->GetArena()); - const FieldDescriptor* oneof_field = d->FindFieldByName("oneof_nested_message"); + const FieldDescriptor* oneof_field = + d->FindFieldByName("oneof_nested_message"); submessage = static_cast( r->MutableMessage(message, oneof_field)); submessage_expected = message->mutable_oneof_nested_message(); @@ -1153,11 +1144,11 @@ TEST(ArenaTest, MutableMessageReflection) { EXPECT_EQ(submessage_expected, submessage); EXPECT_EQ(&arena, submessage->GetArena()); } -#endif // GOOGLE_PROTOBUF_RTTI +#endif // PROTOBUF_RTTI void FillArenaAwareFields(TestAllTypes* message) { - string test_string = "hello world"; + std::string test_string = "hello world"; message->set_optional_int32(42); message->set_optional_string(test_string); message->set_optional_bytes(test_string); @@ -1199,7 +1190,7 @@ TEST(ArenaTest, ParseCorruptedString) { TestParseCorruptedString(message); } -#if GOOGLE_PROTOBUF_RTTI +#if PROTOBUF_RTTI // Test construction on an arena via generic MessageLite interface. We should be // able to successfully deserialize on the arena without incurring heap // allocations, i.e., everything should still be arena-allocation-aware. @@ -1213,7 +1204,7 @@ TEST(ArenaTest, MessageLiteOnArena) { TestAllTypes initial_message; FillArenaAwareFields(&initial_message); - string serialized; + std::string serialized; initial_message.SerializeToString(&serialized); { @@ -1228,7 +1219,7 @@ TEST(ArenaTest, MessageLiteOnArena) { arena.Reset(); } -#endif // GOOGLE_PROTOBUF_RTTI +#endif // PROTOBUF_RTTI // RepeatedField should support non-POD types, and invoke constructors and @@ -1236,14 +1227,14 @@ TEST(ArenaTest, MessageLiteOnArena) { // (even if this was not its original intent). TEST(ArenaTest, RepeatedFieldWithNonPODType) { { - RepeatedField field_on_heap; + RepeatedField field_on_heap; for (int i = 0; i < 100; i++) { *field_on_heap.Add() = "test string long enough to exceed inline buffer"; } } { Arena arena; - RepeatedField field_on_arena(&arena); + RepeatedField field_on_arena(&arena); for (int i = 0; i < 100; i++) { *field_on_arena.Add() = "test string long enough to exceed inline buffer"; } @@ -1324,6 +1315,12 @@ TEST(ArenaTest, GetArenaShouldReturnTheArenaForArenaAllocatedMessages) { const ArenaMessage* const_pointer_to_message = message; EXPECT_EQ(&arena, Arena::GetArena(message)); EXPECT_EQ(&arena, Arena::GetArena(const_pointer_to_message)); + + // Test that the Message* / MessageLite* specialization SFINAE works. + const Message* const_pointer_to_message_type = message; + EXPECT_EQ(&arena, Arena::GetArena(const_pointer_to_message_type)); + const MessageLite* const_pointer_to_message_lite_type = message; + EXPECT_EQ(&arena, Arena::GetArena(const_pointer_to_message_lite_type)); } TEST(ArenaTest, GetArenaShouldReturnNullForNonArenaAllocatedMessages) { @@ -1333,6 +1330,34 @@ TEST(ArenaTest, GetArenaShouldReturnNullForNonArenaAllocatedMessages) { EXPECT_EQ(NULL, Arena::GetArena(const_pointer_to_message)); } +TEST(ArenaTest, GetArenaShouldReturnNullForNonArenaCompatibleTypes) { + TestNoArenaMessage message; + const TestNoArenaMessage* const_pointer_to_message = &message; + EXPECT_EQ(nullptr, Arena::GetArena(&message)); + EXPECT_EQ(nullptr, Arena::GetArena(const_pointer_to_message)); + + // Test that GetArena returns nullptr for types that have a GetArena method + // that doesn't return Arena*. + struct { + int GetArena() const { return 0; } + } has_get_arena_method_wrong_return_type; + EXPECT_EQ(nullptr, Arena::GetArena(&has_get_arena_method_wrong_return_type)); + + // Test that GetArena returns nullptr for types that have a GetArena alias. + struct { + using GetArena = Arena*; + GetArena unused; + } has_get_arena_alias; + EXPECT_EQ(nullptr, Arena::GetArena(&has_get_arena_alias)); + + // Test that GetArena returns nullptr for types that have a GetArena data + // member. + struct { + Arena GetArena; + } has_get_arena_data_member; + EXPECT_EQ(nullptr, Arena::GetArena(&has_get_arena_data_member)); +} + TEST(ArenaTest, AddCleanup) { Arena arena; for (int i = 0; i < 100; i++) { @@ -1345,7 +1370,7 @@ TEST(ArenaTest, UnsafeSetAllocatedOnArena) { TestAllTypes* message = Arena::CreateMessage(&arena); EXPECT_FALSE(message->has_optional_string()); - string owned_string = "test with long enough content to heap-allocate"; + std::string owned_string = "test with long enough content to heap-allocate"; message->unsafe_arena_set_allocated_optional_string(&owned_string); EXPECT_TRUE(message->has_optional_string()); diff --git a/src/google/protobuf/arenastring.h b/src/google/protobuf/arenastring.h index 168fc972b8..99e26b8752 100644 --- a/src/google/protobuf/arenastring.h +++ b/src/google/protobuf/arenastring.h @@ -32,20 +32,28 @@ #define GOOGLE_PROTOBUF_ARENASTRING_H__ #include +#include -#include +#include #include #include -#include -#include +#include +#include + +#include + +#ifdef SWIG +#error "You cannot SWIG proto headers" +#endif + // This is the implementation of arena string fields written for the open-source // release. The ArenaStringPtr struct below is an internal implementation class // and *should not be used* by user code. It is used to collect string // operations together into one place and abstract away the underlying // string-field pointer representation, so that (for example) an alternate -// implementation that knew more about ::std::string's internals could integrate more -// closely with the arena allocator. +// implementation that knew more about ::std::string's internals could integrate +// more closely with the arena allocator. namespace google { namespace protobuf { @@ -63,9 +71,9 @@ class TaggedPtr { uintptr_t ptr_; }; -struct LIBPROTOBUF_EXPORT ArenaStringPtr { +struct PROTOBUF_EXPORT ArenaStringPtr { inline void Set(const ::std::string* default_value, - const ::std::string& value, ::google::protobuf::Arena* arena) { + const ::std::string& value, Arena* arena) { if (ptr_ == default_value) { CreateInstance(arena, &value); } else { @@ -74,8 +82,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { } inline void SetLite(const ::std::string* default_value, - const ::std::string& value, - ::google::protobuf::Arena* arena) { + const ::std::string& value, Arena* arena) { Set(default_value, value, arena); } @@ -83,7 +90,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { inline const ::std::string& Get() const { return *ptr_; } inline ::std::string* Mutable(const ::std::string* default_value, - ::google::protobuf::Arena* arena) { + Arena* arena) { if (ptr_ == default_value) { CreateInstance(arena, default_value); } @@ -95,7 +102,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { // retains ownership. Clears this field back to NULL state. Used to implement // release_() methods on generated classes. inline ::std::string* Release(const ::std::string* default_value, - ::google::protobuf::Arena* arena) { + Arena* arena) { if (ptr_ == default_value) { return NULL; } @@ -103,8 +110,8 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { } // Similar to Release, but ptr_ cannot be the default_value. - inline ::std::string* ReleaseNonDefault( - const ::std::string* default_value, ::google::protobuf::Arena* arena) { + inline ::std::string* ReleaseNonDefault(const ::std::string* default_value, + Arena* arena) { GOOGLE_DCHECK(!IsDefault(default_value)); ::std::string* released = NULL; if (arena != NULL) { @@ -114,22 +121,22 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { } else { released = ptr_; } - ptr_ = const_cast< ::std::string* >(default_value); + ptr_ = const_cast< ::std::string*>(default_value); return released; } - // UnsafeArenaRelease returns a ::std::string*, but it may be arena-owned (i.e. - // have its destructor already registered) if arena != NULL. If the field was - // not set, this returns NULL. This method clears this field back to NULL - // state. Used to implement unsafe_arena_release_() methods on + // UnsafeArenaRelease returns a ::std::string*, but it may be arena-owned + // (i.e. have its destructor already registered) if arena != NULL. If the + // field was not set, this returns NULL. This method clears this field back to + // NULL state. Used to implement unsafe_arena_release_() methods on // generated classes. inline ::std::string* UnsafeArenaRelease(const ::std::string* default_value, - ::google::protobuf::Arena* /* arena */) { + Arena* /* arena */) { if (ptr_ == default_value) { return NULL; } ::std::string* released = ptr_; - ptr_ = const_cast< ::std::string* >(default_value); + ptr_ = const_cast< ::std::string*>(default_value); return released; } @@ -137,7 +144,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { // destructor is registered with the arena. Used to implement // set_allocated_ in generated classes. inline void SetAllocated(const ::std::string* default_value, - ::std::string* value, ::google::protobuf::Arena* arena) { + ::std::string* value, Arena* arena) { if (arena == NULL && ptr_ != default_value) { Destroy(default_value, arena); } @@ -147,7 +154,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { arena->Own(value); } } else { - ptr_ = const_cast< ::std::string* >(default_value); + ptr_ = const_cast< ::std::string*>(default_value); } } @@ -157,22 +164,23 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { // to implement unsafe_arena_set_allocated_ in generated classes. inline void UnsafeArenaSetAllocated(const ::std::string* default_value, ::std::string* value, - ::google::protobuf::Arena* /* arena */) { + Arena* /* arena */) { if (value != NULL) { ptr_ = value; } else { - ptr_ = const_cast< ::std::string* >(default_value); + ptr_ = const_cast< ::std::string*>(default_value); } } // 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. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void Swap(ArenaStringPtr* other) { + PROTOBUF_ALWAYS_INLINE void Swap(ArenaStringPtr* other) { std::swap(ptr_, other->ptr_); } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void Swap( - ArenaStringPtr* other, const ::std::string* default_value, Arena* arena) { + PROTOBUF_ALWAYS_INLINE void 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 // string instances themselves. This invalidates previously taken const @@ -192,12 +200,13 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { this_ptr->swap(*other_ptr); #else std::swap(ptr_, other->ptr_); + (void)default_value; + (void)arena; #endif } // Frees storage (if not on an arena). - inline void Destroy(const ::std::string* default_value, - ::google::protobuf::Arena* arena) { + inline void Destroy(const ::std::string* default_value, Arena* arena) { if (arena == NULL && ptr_ != default_value) { delete ptr_; } @@ -208,7 +217,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { // the user) will always be the empty string. Assumes that |default_value| // is an empty string. inline void ClearToEmpty(const ::std::string* default_value, - ::google::protobuf::Arena* /* arena */) { + Arena* /* arena */) { if (ptr_ == default_value) { // Already set to default (which is empty) -- do nothing. } else { @@ -218,18 +227,14 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { // Clears content, assuming that the current value is not the empty string // default. - inline void ClearNonDefaultToEmpty() { - ptr_->clear(); - } - inline void ClearNonDefaultToEmptyNoArena() { - ptr_->clear(); - } + inline void ClearNonDefaultToEmpty() { ptr_->clear(); } + inline void ClearNonDefaultToEmptyNoArena() { ptr_->clear(); } // Clears content, but keeps allocated 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|. inline void ClearToDefault(const ::std::string* default_value, - ::google::protobuf::Arena* /* arena */) { + Arena* /* arena */) { if (ptr_ == default_value) { // Already set to default -- do nothing. } else { @@ -248,7 +253,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { inline void UnsafeSetDefault(const ::std::string* default_value) { // Casting away 'const' is safe here: accessors ensure that ptr_ is only // returned as a const if it is equal to default_value. - ptr_ = const_cast< ::std::string* >(default_value); + ptr_ = const_cast< ::std::string*>(default_value); } // The 'NoArena' variants of methods below assume arena == NULL and are @@ -267,7 +272,6 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { } } -#if LANG_CXX11 void SetNoArena(const ::std::string* default_value, ::std::string&& value) { if (IsDefault(default_value)) { ptr_ = new ::std::string(std::move(value)); @@ -275,9 +279,9 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { *ptr_ = std::move(value); } } -#endif - void AssignWithDefault(const ::std::string* default_value, ArenaStringPtr value); + void AssignWithDefault(const ::std::string* default_value, + ArenaStringPtr value); inline const ::std::string& GetNoArena() const { return *ptr_; } @@ -300,11 +304,10 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { const ::std::string* default_value) { GOOGLE_DCHECK(!IsDefault(default_value)); ::std::string* released = ptr_; - ptr_ = const_cast< ::std::string* >(default_value); + ptr_ = const_cast< ::std::string*>(default_value); return released; } - inline void SetAllocatedNoArena(const ::std::string* default_value, ::std::string* value) { if (ptr_ != default_value) { @@ -313,7 +316,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { if (value != NULL) { ptr_ = value; } else { - ptr_ = const_cast< ::std::string* >(default_value); + ptr_ = const_cast< ::std::string*>(default_value); } } @@ -344,9 +347,7 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { // raw pointer from the shared parse routine (in the non-arenas case). The // parse routine does the string allocation in order to save code size in the // generated parsing code. - inline ::std::string** UnsafeRawStringPointer() { - return &ptr_; - } + inline ::std::string** UnsafeRawStringPointer() { return &ptr_; } inline bool IsDefault(const ::std::string* default_value) const { return ptr_ == default_value; @@ -364,14 +365,13 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { private: ::std::string* ptr_; - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE - void CreateInstance(::google::protobuf::Arena* arena, - const ::std::string* initial_value) { + PROTOBUF_NOINLINE + void CreateInstance(Arena* arena, const ::std::string* initial_value) { GOOGLE_DCHECK(initial_value != NULL); // uses "new ::std::string" when arena is nullptr - ptr_ = Arena::Create< ::std::string >(arena, *initial_value); + ptr_ = Arena::Create< ::std::string>(arena, *initial_value); } - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE + PROTOBUF_NOINLINE void CreateInstanceNoArena(const ::std::string* initial_value) { GOOGLE_DCHECK(initial_value != NULL); ptr_ = new ::std::string(*initial_value); @@ -381,13 +381,11 @@ struct LIBPROTOBUF_EXPORT ArenaStringPtr { } // namespace internal } // namespace protobuf - - namespace protobuf { namespace internal { -inline void ArenaStringPtr::AssignWithDefault(const ::std::string* default_value, - ArenaStringPtr value) { +inline void ArenaStringPtr::AssignWithDefault( + const ::std::string* default_value, ArenaStringPtr value) { const ::std::string* me = *UnsafeRawStringPointer(); const ::std::string* other = *value.UnsafeRawStringPointer(); // If the pointers are the same then do nothing. @@ -398,6 +396,9 @@ inline void ArenaStringPtr::AssignWithDefault(const ::std::string* default_value } // namespace internal } // namespace protobuf - } // namespace google + + +#include + #endif // GOOGLE_PROTOBUF_ARENASTRING_H__ diff --git a/src/google/protobuf/arenastring_unittest.cc b/src/google/protobuf/arenastring_unittest.cc index c5f89a7007..70a3f76722 100644 --- a/src/google/protobuf/arenastring_unittest.cc +++ b/src/google/protobuf/arenastring_unittest.cc @@ -40,10 +40,9 @@ #include #include -#include #include #include - +#include namespace google { @@ -52,85 +51,83 @@ namespace protobuf { using internal::ArenaStringPtr; -static string WrapString(const char* value) { - return value; -} +static std::string WrapString(const char* value) { return value; } // Test ArenaStringPtr with arena == NULL. TEST(ArenaStringPtrTest, ArenaStringPtrOnHeap) { ArenaStringPtr field; - ::std::string default_value = "default"; + std::string default_value = "default"; field.UnsafeSetDefault(&default_value); - EXPECT_EQ(string("default"), field.Get()); + EXPECT_EQ(std::string("default"), field.Get()); field.Set(&default_value, WrapString("Test short"), NULL); - EXPECT_EQ(string("Test short"), field.Get()); + EXPECT_EQ(std::string("Test short"), field.Get()); field.Set(&default_value, WrapString("Test long long long long value"), NULL); - EXPECT_EQ(string("Test long long long long value"), field.Get()); - field.Set(&default_value, string(""), NULL); + EXPECT_EQ(std::string("Test long long long long value"), field.Get()); + field.Set(&default_value, std::string(""), NULL); field.Destroy(&default_value, NULL); ArenaStringPtr field2; field2.UnsafeSetDefault(&default_value); - ::std::string* mut = field2.Mutable(&default_value, NULL); + std::string* mut = field2.Mutable(&default_value, NULL); EXPECT_EQ(mut, field2.Mutable(&default_value, NULL)); EXPECT_EQ(mut, &field2.Get()); EXPECT_NE(&default_value, mut); - EXPECT_EQ(string("default"), *mut); + EXPECT_EQ(std::string("default"), *mut); *mut = "Test long long long long value"; // ensure string allocates storage - EXPECT_EQ(string("Test long long long long value"), field2.Get()); + EXPECT_EQ(std::string("Test long long long long value"), field2.Get()); field2.Destroy(&default_value, NULL); } TEST(ArenaStringPtrTest, ArenaStringPtrOnArena) { Arena arena; ArenaStringPtr field; - ::std::string default_value = "default"; + std::string default_value = "default"; field.UnsafeSetDefault(&default_value); - EXPECT_EQ(string("default"), field.Get()); + EXPECT_EQ(std::string("default"), field.Get()); field.Set(&default_value, WrapString("Test short"), &arena); - EXPECT_EQ(string("Test short"), field.Get()); + EXPECT_EQ(std::string("Test short"), field.Get()); field.Set(&default_value, WrapString("Test long long long long value"), &arena); - EXPECT_EQ(string("Test long long long long value"), field.Get()); - field.Set(&default_value, string(""), &arena); + EXPECT_EQ(std::string("Test long long long long value"), field.Get()); + field.Set(&default_value, std::string(""), &arena); field.Destroy(&default_value, &arena); ArenaStringPtr field2; field2.UnsafeSetDefault(&default_value); - ::std::string* mut = field2.Mutable(&default_value, &arena); + std::string* mut = field2.Mutable(&default_value, &arena); EXPECT_EQ(mut, field2.Mutable(&default_value, &arena)); EXPECT_EQ(mut, &field2.Get()); EXPECT_NE(&default_value, mut); - EXPECT_EQ(string("default"), *mut); + EXPECT_EQ(std::string("default"), *mut); *mut = "Test long long long long value"; // ensure string allocates storage - EXPECT_EQ(string("Test long long long long value"), field2.Get()); + EXPECT_EQ(std::string("Test long long long long value"), field2.Get()); field2.Destroy(&default_value, &arena); } TEST(ArenaStringPtrTest, ArenaStringPtrOnArenaNoSSO) { Arena arena; ArenaStringPtr field; - ::std::string default_value = "default"; + std::string default_value = "default"; field.UnsafeSetDefault(&default_value); - EXPECT_EQ(string("default"), field.Get()); + EXPECT_EQ(std::string("default"), field.Get()); // Avoid triggering the SSO optimization by setting the string to something // larger than the internal buffer. field.Set(&default_value, WrapString("Test long long long long value"), &arena); - EXPECT_EQ(string("Test long long long long value"), field.Get()); - field.Set(&default_value, string(""), &arena); + EXPECT_EQ(std::string("Test long long long long value"), field.Get()); + field.Set(&default_value, std::string(""), &arena); field.Destroy(&default_value, &arena); ArenaStringPtr field2; field2.UnsafeSetDefault(&default_value); - ::std::string* mut = field2.Mutable(&default_value, &arena); + std::string* mut = field2.Mutable(&default_value, &arena); EXPECT_EQ(mut, field2.Mutable(&default_value, &arena)); EXPECT_EQ(mut, &field2.Get()); EXPECT_NE(&default_value, mut); - EXPECT_EQ(string("default"), *mut); + EXPECT_EQ(std::string("default"), *mut); *mut = "Test long long long long value"; // ensure string allocates storage - EXPECT_EQ(string("Test long long long long value"), field2.Get()); + EXPECT_EQ(std::string("Test long long long long value"), field2.Get()); field2.Destroy(&default_value, &arena); } diff --git a/src/google/protobuf/compiler/annotation_test_util.cc b/src/google/protobuf/compiler/annotation_test_util.cc index a0530b9a69..d33f29fb94 100644 --- a/src/google/protobuf/compiler/annotation_test_util.cc +++ b/src/google/protobuf/compiler/annotation_test_util.cc @@ -57,8 +57,9 @@ class DescriptorCapturingGenerator : public CodeGenerator { explicit DescriptorCapturingGenerator(FileDescriptorProto* file) : file_(file) {} - virtual bool Generate(const FileDescriptor* file, const string& parameter, - GeneratorContext* context, string* error) const { + virtual bool Generate(const FileDescriptor* file, + const std::string& parameter, GeneratorContext* context, + std::string* error) const { file->CopyTo(file_); return true; } @@ -68,21 +69,21 @@ class DescriptorCapturingGenerator : public CodeGenerator { }; } // namespace -void AddFile(const string& filename, const string& data) { +void AddFile(const std::string& filename, const std::string& data) { GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/" + filename, data, true)); } -bool RunProtoCompiler(const string& filename, - const string& plugin_specific_args, +bool RunProtoCompiler(const std::string& filename, + const std::string& plugin_specific_args, CommandLineInterface* cli, FileDescriptorProto* file) { cli->SetInputsAreProtoPathRelative(true); DescriptorCapturingGenerator capturing_generator(file); cli->RegisterGenerator("--capture_out", &capturing_generator, ""); - string proto_path = "-I" + TestTempDir(); - string capture_out = "--capture_out=" + TestTempDir(); + std::string proto_path = "-I" + TestTempDir(); + std::string capture_out = "--capture_out=" + TestTempDir(); const char* argv[] = {"protoc", proto_path.c_str(), plugin_specific_args.c_str(), capture_out.c_str(), @@ -91,15 +92,15 @@ bool RunProtoCompiler(const string& filename, return cli->Run(5, argv) == 0; } -bool DecodeMetadata(const string& path, GeneratedCodeInfo* info) { - string data; +bool DecodeMetadata(const std::string& path, GeneratedCodeInfo* info) { + std::string data; GOOGLE_CHECK_OK(File::GetContents(path, &data, true)); io::ArrayInputStream input(data.data(), data.size()); return info->ParseFromZeroCopyStream(&input); } void FindAnnotationsOnPath( - const GeneratedCodeInfo& info, const string& source_file, + const GeneratedCodeInfo& info, const std::string& source_file, const std::vector& path, std::vector* annotations) { for (int i = 0; i < info.annotation_size(); ++i) { @@ -121,7 +122,7 @@ void FindAnnotationsOnPath( } const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( - const GeneratedCodeInfo& info, const string& source_file, + const GeneratedCodeInfo& info, const std::string& source_file, const std::vector& path) { std::vector annotations; FindAnnotationsOnPath(info, source_file, path, &annotations); @@ -132,9 +133,9 @@ const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( } bool AtLeastOneAnnotationMatchesSubstring( - const string& file_content, + const std::string& file_content, const std::vector& annotations, - const string& expected_text) { + const std::string& expected_text) { for (std::vector::const_iterator i = annotations.begin(), e = annotations.end(); @@ -152,9 +153,9 @@ bool AtLeastOneAnnotationMatchesSubstring( return false; } -bool AnnotationMatchesSubstring(const string& file_content, +bool AnnotationMatchesSubstring(const std::string& file_content, const GeneratedCodeInfo::Annotation* annotation, - const string& expected_text) { + const std::string& expected_text) { std::vector annotations; annotations.push_back(annotation); return AtLeastOneAnnotationMatchesSubstring(file_content, annotations, diff --git a/src/google/protobuf/compiler/annotation_test_util.h b/src/google/protobuf/compiler/annotation_test_util.h index 34c3f6a979..7c1319182e 100644 --- a/src/google/protobuf/compiler/annotation_test_util.h +++ b/src/google/protobuf/compiler/annotation_test_util.h @@ -49,15 +49,16 @@ namespace annotation_test_util { // file_content = content of Foo.java // file_info = parsed content of Foo.java.pb.meta struct ExpectedOutput { - string file_path; - string file_content; + std::string file_path; + std::string file_content; GeneratedCodeInfo file_info; - explicit ExpectedOutput(const string& file_path) : file_path(file_path) {} + explicit ExpectedOutput(const std::string& file_path) + : file_path(file_path) {} }; // Creates a file with name `filename` and content `data` in temp test // directory. -void AddFile(const string& filename, const string& data); +void AddFile(const std::string& filename, const std::string& data); // Runs proto compiler. Captures proto file structrue in FileDescriptorProto. // Files will be generated in TestTempDir() folder. Callers of this @@ -70,17 +71,17 @@ void AddFile(const string& filename, const string& data); // annotation_unittest.cc for an example of how to initialize it. // file: output parameter, will be set to the descriptor of the proto file // specified in filename. -bool RunProtoCompiler(const string& filename, - const string& plugin_specific_args, +bool RunProtoCompiler(const std::string& filename, + const std::string& plugin_specific_args, CommandLineInterface* cli, FileDescriptorProto* file); -bool DecodeMetadata(const string& path, GeneratedCodeInfo* info); +bool DecodeMetadata(const std::string& path, GeneratedCodeInfo* info); // Finds all of the Annotations for a given source file and path. // See Location.path in http://google3/net/proto2/proto/descriptor.proto for // explanation of what path vector is. void FindAnnotationsOnPath( - const GeneratedCodeInfo& info, const string& source_file, + const GeneratedCodeInfo& info, const std::string& source_file, const std::vector& path, std::vector* annotations); @@ -90,21 +91,21 @@ void FindAnnotationsOnPath( // http://google3/net/proto2/proto/descriptor.proto for explanation of what path // vector is. const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( - const GeneratedCodeInfo& info, const string& source_file, + const GeneratedCodeInfo& info, const std::string& source_file, const std::vector& path); // Returns true if at least one of the provided annotations covers a given // substring in file_content. bool AtLeastOneAnnotationMatchesSubstring( - const string& file_content, + const std::string& file_content, const std::vector& annotations, - const string& expected_text); + const std::string& expected_text); // Returns true if the provided annotation covers a given substring in // file_content. -bool AnnotationMatchesSubstring(const string& file_content, +bool AnnotationMatchesSubstring(const std::string& file_content, const GeneratedCodeInfo::Annotation* annotation, - const string& expected_text); + const std::string& expected_text); } // namespace annotation_test_util } // namespace compiler diff --git a/src/google/protobuf/compiler/code_generator.cc b/src/google/protobuf/compiler/code_generator.cc index aaabd9142d..428ec46983 100644 --- a/src/google/protobuf/compiler/code_generator.cc +++ b/src/google/protobuf/compiler/code_generator.cc @@ -46,11 +46,10 @@ namespace compiler { CodeGenerator::~CodeGenerator() {} -bool CodeGenerator::GenerateAll( - const std::vector& files, - const string& parameter, - GeneratorContext* generator_context, - string* error) const { +bool CodeGenerator::GenerateAll(const std::vector& files, + const std::string& parameter, + GeneratorContext* generator_context, + std::string* error) const { // Default implemenation is just to call the per file method, and prefix any // error string with the file to provide context. bool succeeded = true; @@ -58,8 +57,9 @@ bool CodeGenerator::GenerateAll( const FileDescriptor* file = files[i]; succeeded = Generate(file, parameter, generator_context, error); if (!succeeded && error && error->empty()) { - *error = "Code generator returned false but provided no error " - "description."; + *error = + "Code generator returned false but provided no error " + "description."; } if (error && !error->empty()) { *error = file->name() + ": " + *error; @@ -74,13 +74,13 @@ bool CodeGenerator::GenerateAll( GeneratorContext::~GeneratorContext() {} -io::ZeroCopyOutputStream* -GeneratorContext::OpenForAppend(const string& filename) { +io::ZeroCopyOutputStream* GeneratorContext::OpenForAppend( + const std::string& filename) { return NULL; } io::ZeroCopyOutputStream* GeneratorContext::OpenForInsert( - const string& filename, const string& insertion_point) { + const std::string& filename, const std::string& insertion_point) { GOOGLE_LOG(FATAL) << "This GeneratorContext does not support insertion."; return NULL; // make compiler happy } @@ -98,14 +98,15 @@ void GeneratorContext::GetCompilerVersion(Version* version) const { } // Parses a set of comma-delimited name/value pairs. -void ParseGeneratorParameter(const string& text, - std::vector >* output) { - std::vector parts = Split(text, ",", true); +void ParseGeneratorParameter( + const std::string& text, + std::vector >* output) { + std::vector parts = Split(text, ",", true); for (int i = 0; i < parts.size(); i++) { - string::size_type equals_pos = parts[i].find_first_of('='); - std::pair value; - if (equals_pos == string::npos) { + std::string::size_type equals_pos = parts[i].find_first_of('='); + std::pair value; + if (equals_pos == std::string::npos) { value.first = parts[i]; value.second = ""; } else { diff --git a/src/google/protobuf/compiler/code_generator.h b/src/google/protobuf/compiler/code_generator.h index a1cd6113e6..528dc76428 100644 --- a/src/google/protobuf/compiler/code_generator.h +++ b/src/google/protobuf/compiler/code_generator.h @@ -38,15 +38,19 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ #define GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ -#include #include -#include #include +#include +#include + +#include namespace google { namespace protobuf { -namespace io { class ZeroCopyOutputStream; } +namespace io { +class ZeroCopyOutputStream; +} class FileDescriptor; namespace compiler { @@ -61,7 +65,7 @@ class GeneratorContext; // The abstract interface to a class which generates code implementing a // particular proto file in a particular language. A number of these may // be registered with CommandLineInterface to support various languages. -class LIBPROTOC_EXPORT CodeGenerator { +class PROTOC_EXPORT CodeGenerator { public: inline CodeGenerator() {} virtual ~CodeGenerator(); @@ -78,9 +82,9 @@ class LIBPROTOC_EXPORT CodeGenerator { // Returns true if successful. Otherwise, sets *error to a description of // the problem (e.g. "invalid parameter") and returns false. virtual bool Generate(const FileDescriptor* file, - const string& parameter, + const std::string& parameter, GeneratorContext* generator_context, - string* error) const = 0; + std::string* error) const = 0; // Generates code for all given proto files. // @@ -94,9 +98,9 @@ class LIBPROTOC_EXPORT CodeGenerator { // Returns true if successful. Otherwise, sets *error to a description of // the problem (e.g. "invalid parameter") and returns false. virtual bool GenerateAll(const std::vector& files, - const string& parameter, + const std::string& parameter, GeneratorContext* generator_context, - string* error) const; + std::string* error) const; // This is no longer used, but this class is part of the opensource protobuf // library, so it has to remain to keep vtables the same for the current @@ -112,7 +116,7 @@ class LIBPROTOC_EXPORT CodeGenerator { // abstract interface represents the directory to which the CodeGenerator is // to write and other information about the context in which the Generator // runs. -class LIBPROTOC_EXPORT GeneratorContext { +class PROTOC_EXPORT GeneratorContext { public: inline GeneratorContext() { } @@ -128,10 +132,10 @@ class LIBPROTOC_EXPORT GeneratorContext { // generate the files "foo/bar.pb.h" and "foo/bar.pb.cc"; note that // "foo/" is included in these filenames. The filename is not allowed to // contain "." or ".." components. - virtual io::ZeroCopyOutputStream* Open(const string& filename) = 0; + virtual io::ZeroCopyOutputStream* Open(const std::string& filename) = 0; // Similar to Open() but the output will be appended to the file if exists - virtual io::ZeroCopyOutputStream* OpenForAppend(const string& filename); + virtual io::ZeroCopyOutputStream* OpenForAppend(const std::string& filename); // Creates a ZeroCopyOutputStream which will insert code into the given file // at the given insertion point. See plugin.proto (plugin.pb.h) for more @@ -140,7 +144,7 @@ class LIBPROTOC_EXPORT GeneratorContext { // // WARNING: This feature is currently EXPERIMENTAL and is subject to change. virtual io::ZeroCopyOutputStream* OpenForInsert( - const string& filename, const string& insertion_point); + const std::string& filename, const std::string& insertion_point); // Returns a vector of FileDescriptors for all the files being compiled // in this run. Useful for languages, such as Go, that treat files @@ -166,11 +170,13 @@ typedef GeneratorContext OutputDirectory; // "foo=bar,baz,qux=corge" // parses to the pairs: // ("foo", "bar"), ("baz", ""), ("qux", "corge") -LIBPROTOC_EXPORT void ParseGeneratorParameter( - const string&, std::vector >*); +PROTOC_EXPORT void ParseGeneratorParameter( + const std::string&, std::vector >*); } // namespace compiler } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index e5fd845978..5b4fc1383a 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -34,7 +34,6 @@ #include - #include #include @@ -45,17 +44,17 @@ #ifdef minor #undef minor #endif -#include #include +#include #ifndef _MSC_VER #include #endif +#include #include #include #include -#include -#include //For PATH_MAX +#include //For PATH_MAX #include @@ -71,6 +70,7 @@ #include #include #include +#include #include #include #include @@ -79,11 +79,14 @@ #include #include #include + + #include #include -#include +#include + namespace google { namespace protobuf { namespace compiler { @@ -92,7 +95,7 @@ namespace compiler { #ifdef _O_BINARY #define O_BINARY _O_BINARY #else -#define O_BINARY 0 // If this isn't defined, the platform doesn't need it. +#define O_BINARY 0 // If this isn't defined, the platform doesn't need it. #endif #endif @@ -100,12 +103,12 @@ namespace { #if defined(_WIN32) // DO NOT include , instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::mkdir; -using google::protobuf::internal::win32::open; -using google::protobuf::internal::win32::setmode; -using google::protobuf::internal::win32::write; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::mkdir; +using google::protobuf::io::win32::open; +using google::protobuf::io::win32::setmode; +using google::protobuf::io::win32::write; #endif static const char* kDefaultDirectDependenciesViolationMsg = @@ -114,12 +117,10 @@ static const char* kDefaultDirectDependenciesViolationMsg = // Returns true if the text looks like a Windows-style absolute path, starting // with a drive letter. Example: "C:\foo". TODO(kenton): Share this with // copy in importer.cc? -static bool IsWindowsAbsolutePath(const string& text) { +static bool IsWindowsAbsolutePath(const std::string& text) { #if defined(_WIN32) || defined(__CYGWIN__) - return text.size() >= 3 && text[1] == ':' && - isalpha(text[0]) && - (text[2] == '/' || text[2] == '\\') && - text.find_last_of(':') == 1; + return text.size() >= 3 && text[1] == ':' && isalpha(text[0]) && + (text[2] == '/' || text[2] == '\\') && text.find_last_of(':') == 1; #else return false; #endif @@ -145,13 +146,13 @@ void SetFdToBinaryMode(int fd) { // (Text and binary are the same on non-Windows platforms.) } -void AddTrailingSlash(string* path) { +void AddTrailingSlash(std::string* path) { if (!path->empty() && path->at(path->size() - 1) != '/') { path->push_back('/'); } } -bool VerifyDirectoryExists(const string& path) { +bool VerifyDirectoryExists(const std::string& path) { if (path.empty()) return true; if (access(path.c_str(), F_OK) == -1) { @@ -166,10 +167,13 @@ bool VerifyDirectoryExists(const string& path) { // parent if necessary, and so on. The full file name is actually // (prefix + filename), but we assume |prefix| already exists and only create // directories listed in |filename|. -bool TryCreateParentDirectory(const string& prefix, const string& filename) { +bool TryCreateParentDirectory(const std::string& prefix, + const std::string& filename) { // Recursively create parent directories to the output file. - std::vector parts = Split(filename, "/", true); - string path_so_far = prefix; + // On Windows, both '/' and '\' are valid path separators. + std::vector parts = + Split(filename, "/\\", true); + std::string path_so_far = prefix; for (int i = 0; i < parts.size() - 1; i++) { path_so_far += parts[i]; if (mkdir(path_so_far.c_str(), 0777) != 0) { @@ -186,11 +190,11 @@ bool TryCreateParentDirectory(const string& prefix, const string& filename) { } // Get the absolute path of this protoc binary. -bool GetProtocAbsolutePath(string* path) { +bool GetProtocAbsolutePath(std::string* path) { #ifdef _WIN32 char buffer[MAX_PATH]; int len = GetModuleFileNameA(NULL, buffer, MAX_PATH); -#elif __APPLE__ +#elif defined(__APPLE__) char buffer[PATH_MAX]; int len = 0; @@ -214,51 +218,55 @@ bool GetProtocAbsolutePath(string* path) { // Whether a path is where google/protobuf/descriptor.proto and other well-known // type protos are installed. -bool IsInstalledProtoPath(const string& path) { +bool IsInstalledProtoPath(const std::string& path) { // Checking the descriptor.proto file should be good enough. - string file_path = path + "/google/protobuf/descriptor.proto"; + std::string file_path = path + "/google/protobuf/descriptor.proto"; return access(file_path.c_str(), F_OK) != -1; } // Add the paths where google/protobuf/descriptor.proto and other well-known // type protos are installed. -void AddDefaultProtoPaths(std::vector >* paths) { +void AddDefaultProtoPaths( + std::vector >* paths) { // TODO(xiaofeng): The code currently only checks relative paths of where // the protoc binary is installed. We probably should make it handle more // cases than that. - string path; + std::string path; if (!GetProtocAbsolutePath(&path)) { return; } // Strip the binary name. size_t pos = path.find_last_of("/\\"); - if (pos == string::npos || pos == 0) { + if (pos == std::string::npos || pos == 0) { return; } path = path.substr(0, pos); // Check the binary's directory. if (IsInstalledProtoPath(path)) { - paths->push_back(std::pair("", path)); + paths->push_back(std::pair("", path)); return; } // Check if there is an include subdirectory. if (IsInstalledProtoPath(path + "/include")) { - paths->push_back(std::pair("", path + "/include")); + paths->push_back( + std::pair("", path + "/include")); return; } // Check if the upper level directory has an "include" subdirectory. pos = path.find_last_of("/\\"); - if (pos == string::npos || pos == 0) { + if (pos == std::string::npos || pos == 0) { return; } path = path.substr(0, pos); if (IsInstalledProtoPath(path + "/include")) { - paths->push_back(std::pair("", path + "/include")); + paths->push_back( + std::pair("", path + "/include")); return; } } -string PluginName(const string& plugin_prefix, const string& directive) { +string PluginName(const std::string& plugin_prefix, + const std::string& directive) { // Assuming the directive starts with "--" and ends with "_out" or "_opt", // strip the "--" and "_out/_opt" and add the plugin prefix. return plugin_prefix + "gen-" + directive.substr(2, directive.size() - 6); @@ -272,60 +280,53 @@ class CommandLineInterface::ErrorPrinter public io::ErrorCollector, public DescriptorPool::ErrorCollector { public: - ErrorPrinter(ErrorFormat format, DiskSourceTree *tree = NULL) - : format_(format), tree_(tree), found_errors_(false) {} + ErrorPrinter(ErrorFormat format, DiskSourceTree* tree = NULL) + : format_(format), tree_(tree), found_errors_(false) {} ~ErrorPrinter() {} // implements MultiFileErrorCollector ------------------------------ - void AddError(const string& filename, int line, int column, - const string& message) { + void AddError(const std::string& filename, int line, int column, + const std::string& message) { found_errors_ = true; AddErrorOrWarning(filename, line, column, message, "error", std::cerr); } - void AddWarning(const string& filename, int line, int column, - const string& message) { + void AddWarning(const std::string& filename, int line, int column, + const std::string& message) { AddErrorOrWarning(filename, line, column, message, "warning", std::clog); } // implements io::ErrorCollector ----------------------------------- - void AddError(int line, int column, const string& message) { + void AddError(int line, int column, const std::string& message) { AddError("input", line, column, message); } - void AddWarning(int line, int column, const string& message) { + void AddWarning(int line, int column, const std::string& message) { AddErrorOrWarning("input", line, column, message, "warning", std::clog); } // implements DescriptorPool::ErrorCollector------------------------- - void AddError( - const string& filename, - const string& element_name, - const Message* descriptor, - ErrorLocation location, - const string& message) { + void AddError(const std::string& filename, const std::string& element_name, + const Message* descriptor, ErrorLocation location, + const std::string& message) { AddErrorOrWarning(filename, -1, -1, message, "error", std::cerr); } - void AddWarning( - const string& filename, - const string& element_name, - const Message* descriptor, - ErrorLocation location, - const string& message) { + void AddWarning(const std::string& filename, const std::string& element_name, + const Message* descriptor, ErrorLocation location, + const std::string& message) { AddErrorOrWarning(filename, -1, -1, message, "warning", std::clog); } bool FoundErrors() const { return found_errors_; } private: - void AddErrorOrWarning(const string& filename, int line, int column, - const string& message, const string& type, + void AddErrorOrWarning(const std::string& filename, int line, int column, + const std::string& message, const std::string& type, std::ostream& out) { // Print full path when running under MSVS - string dfile; - if (format_ == CommandLineInterface::ERROR_FORMAT_MSVS && - tree_ != NULL && + std::string dfile; + if (format_ == CommandLineInterface::ERROR_FORMAT_MSVS && tree_ != NULL && tree_->VirtualFileToDiskFile(filename, &dfile)) { out << dfile; } else { @@ -341,8 +342,8 @@ class CommandLineInterface::ErrorPrinter out << ":" << (line + 1) << ":" << (column + 1); break; case CommandLineInterface::ERROR_FORMAT_MSVS: - out << "(" << (line + 1) << ") : " - << type << " in column=" << (column + 1); + out << "(" << (line + 1) << ") : " << type + << " in column=" << (column + 1); break; } } @@ -355,7 +356,7 @@ class CommandLineInterface::ErrorPrinter } const ErrorFormat format_; - DiskSourceTree *tree_; + DiskSourceTree* tree_; bool found_errors_; }; @@ -366,28 +367,27 @@ class CommandLineInterface::ErrorPrinter class CommandLineInterface::GeneratorContextImpl : public GeneratorContext { public: GeneratorContextImpl(const std::vector& parsed_files); - ~GeneratorContextImpl(); // Write all files in the directory to disk at the given output location, // which must end in a '/'. - bool WriteAllToDisk(const string& prefix); + bool WriteAllToDisk(const std::string& prefix); // Write the contents of this directory to a ZIP-format archive with the // given name. - bool WriteAllToZip(const string& filename); + bool WriteAllToZip(const std::string& filename); // Add a boilerplate META-INF/MANIFEST.MF file as required by the Java JAR // format, unless one has already been written. void AddJarManifest(); // Get name of all output files. - void GetOutputFilenames(std::vector* output_filenames); + void GetOutputFilenames(std::vector* output_filenames); // implements GeneratorContext -------------------------------------- - io::ZeroCopyOutputStream* Open(const string& filename); - io::ZeroCopyOutputStream* OpenForAppend(const string& filename); - io::ZeroCopyOutputStream* OpenForInsert( - const string& filename, const string& insertion_point); + io::ZeroCopyOutputStream* Open(const std::string& filename); + io::ZeroCopyOutputStream* OpenForAppend(const std::string& filename); + io::ZeroCopyOutputStream* OpenForInsert(const std::string& filename, + const std::string& insertion_point); void ListParsedFiles(std::vector* output) { *output = parsed_files_; } @@ -397,7 +397,7 @@ class CommandLineInterface::GeneratorContextImpl : public GeneratorContext { // map instead of unordered_map so that files are written in order (good when // writing zips). - std::map files_; + std::map files_; const std::vector& parsed_files_; bool had_error_; }; @@ -405,16 +405,17 @@ class CommandLineInterface::GeneratorContextImpl : public GeneratorContext { class CommandLineInterface::MemoryOutputStream : public io::ZeroCopyOutputStream { public: - MemoryOutputStream(GeneratorContextImpl* directory, const string& filename, - bool append_mode); - MemoryOutputStream(GeneratorContextImpl* directory, const string& filename, - const string& insertion_point); + MemoryOutputStream(GeneratorContextImpl* directory, + const std::string& filename, bool append_mode); + MemoryOutputStream(GeneratorContextImpl* directory, + const std::string& filename, + const std::string& insertion_point); virtual ~MemoryOutputStream(); // implements ZeroCopyOutputStream --------------------------------- virtual bool Next(void** data, int* size) { return inner_->Next(data, size); } - virtual void BackUp(int count) { inner_->BackUp(count); } - virtual int64 ByteCount() const { return inner_->ByteCount(); } + virtual void BackUp(int count) { inner_->BackUp(count); } + virtual int64 ByteCount() const { return inner_->ByteCount(); } private: // Checks to see if "filename_.meta" exists in directory_; if so, fixes the @@ -426,11 +427,11 @@ class CommandLineInterface::MemoryOutputStream // Where to insert the string when it's done. GeneratorContextImpl* directory_; - string filename_; - string insertion_point_; + std::string filename_; + std::string insertion_point_; // The string we're building. - string data_; + std::string data_; // Whether we should append the output stream to the existing file. bool append_mode_; @@ -443,15 +444,10 @@ class CommandLineInterface::MemoryOutputStream CommandLineInterface::GeneratorContextImpl::GeneratorContextImpl( const std::vector& parsed_files) - : parsed_files_(parsed_files), - had_error_(false) {} - -CommandLineInterface::GeneratorContextImpl::~GeneratorContextImpl() { - STLDeleteValues(&files_); -} + : parsed_files_(parsed_files), had_error_(false) {} bool CommandLineInterface::GeneratorContextImpl::WriteAllToDisk( - const string& prefix) { + const std::string& prefix) { if (had_error_) { return false; } @@ -460,22 +456,21 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToDisk( return false; } - for (std::map::const_iterator iter = files_.begin(); - iter != files_.end(); ++iter) { - const string& relative_filename = iter->first; - const char* data = iter->second->data(); - int size = iter->second->size(); + for (const auto& pair : files_) { + const std::string& relative_filename = pair.first; + const char* data = pair.second.data(); + int size = pair.second.size(); if (!TryCreateParentDirectory(prefix, relative_filename)) { return false; } - string filename = prefix + relative_filename; + std::string filename = prefix + relative_filename; // Create the output file. int file_descriptor; do { file_descriptor = - open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); + open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); } while (file_descriptor < 0 && errno == EINTR); if (file_descriptor < 0) { @@ -526,7 +521,7 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToDisk( } bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip( - const string& filename) { + const std::string& filename) { if (had_error_) { return false; } @@ -535,7 +530,7 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip( int file_descriptor; do { file_descriptor = - open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); + open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); } while (file_descriptor < 0 && errno == EINTR); if (file_descriptor < 0) { @@ -548,9 +543,8 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip( io::FileOutputStream stream(file_descriptor); ZipWriter zip_writer(&stream); - for (std::map::const_iterator iter = files_.begin(); - iter != files_.end(); ++iter) { - zip_writer.Write(iter->first, *iter->second); + for (const auto& pair : files_) { + zip_writer.Write(pair.first, pair.second); } zip_writer.WriteDirectory(); @@ -567,72 +561,69 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip( } void CommandLineInterface::GeneratorContextImpl::AddJarManifest() { - string** map_slot = &files_["META-INF/MANIFEST.MF"]; - if (*map_slot == NULL) { - *map_slot = new string( + auto pair = files_.insert({"META-INF/MANIFEST.MF", ""}); + if (pair.second) { + pair.first->second = "Manifest-Version: 1.0\n" "Created-By: 1.6.0 (protoc)\n" - "\n"); + "\n"; } } void CommandLineInterface::GeneratorContextImpl::GetOutputFilenames( - std::vector* output_filenames) { - for (std::map::iterator iter = files_.begin(); - iter != files_.end(); ++iter) { - output_filenames->push_back(iter->first); + std::vector* output_filenames) { + for (const auto& pair : files_) { + output_filenames->push_back(pair.first); } } io::ZeroCopyOutputStream* CommandLineInterface::GeneratorContextImpl::Open( - const string& filename) { + const std::string& filename) { return new MemoryOutputStream(this, filename, false); } io::ZeroCopyOutputStream* CommandLineInterface::GeneratorContextImpl::OpenForAppend( - const string& filename) { + const std::string& filename) { return new MemoryOutputStream(this, filename, true); } io::ZeroCopyOutputStream* CommandLineInterface::GeneratorContextImpl::OpenForInsert( - const string& filename, const string& insertion_point) { + const std::string& filename, const std::string& insertion_point) { return new MemoryOutputStream(this, filename, insertion_point); } // ------------------------------------------------------------------- CommandLineInterface::MemoryOutputStream::MemoryOutputStream( - GeneratorContextImpl* directory, const string& filename, bool append_mode) + GeneratorContextImpl* directory, const std::string& filename, + bool append_mode) : directory_(directory), filename_(filename), append_mode_(append_mode), - inner_(new io::StringOutputStream(&data_)) { -} + inner_(new io::StringOutputStream(&data_)) {} CommandLineInterface::MemoryOutputStream::MemoryOutputStream( - GeneratorContextImpl* directory, const string& filename, - const string& insertion_point) + GeneratorContextImpl* directory, const std::string& filename, + const std::string& insertion_point) : directory_(directory), filename_(filename), insertion_point_(insertion_point), - inner_(new io::StringOutputStream(&data_)) { -} + inner_(new io::StringOutputStream(&data_)) {} void CommandLineInterface::MemoryOutputStream::UpdateMetadata( size_t insertion_offset, size_t insertion_length) { - std::map::iterator meta_file = - directory_->files_.find(filename_ + ".meta"); - if (meta_file == directory_->files_.end() || !meta_file->second) { + auto it = directory_->files_.find(filename_ + ".meta"); + if (it == directory_->files_.end()) { // No metadata was recorded for this file. return; } - string* encoded_data = meta_file->second; + std::string& encoded_data = it->second; GeneratedCodeInfo metadata; bool is_text_format = false; - if (!metadata.ParseFromString(*encoded_data)) { - if (!TextFormat::ParseFromString(*encoded_data, &metadata)) { + if (!metadata.ParseFromString(encoded_data)) { + if (!TextFormat::ParseFromString(encoded_data, &metadata)) { // The metadata is invalid. std::cerr << filename_ << ".meta: Could not parse metadata as wire or text format." @@ -652,9 +643,9 @@ void CommandLineInterface::MemoryOutputStream::UpdateMetadata( } } if (is_text_format) { - TextFormat::PrintToString(metadata, encoded_data); + TextFormat::PrintToString(metadata, &encoded_data); } else { - metadata.SerializeToString(encoded_data); + metadata.SerializeToString(&encoded_data); } } @@ -663,13 +654,15 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { inner_.reset(); // Insert into the directory. - string** map_slot = &directory_->files_[filename_]; + auto pair = directory_->files_.insert({filename_, ""}); + auto it = pair.first; + bool already_present = !pair.second; if (insertion_point_.empty()) { // This was just a regular Open(). - if (*map_slot != NULL) { + if (already_present) { if (append_mode_) { - (*map_slot)->append(data_); + it->second.append(data_); } else { std::cerr << filename_ << ": Tried to write the same file twice." << std::endl; @@ -678,8 +671,7 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { return; } - *map_slot = new string; - (*map_slot)->swap(data_); + it->second.swap(data_); } else { // This was an OpenForInsert(). @@ -689,21 +681,21 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { } // Find the file we are going to insert into. - if (*map_slot == NULL) { + if (!already_present) { std::cerr << filename_ << ": Tried to insert into file that doesn't exist." << std::endl; directory_->had_error_ = true; return; } - string* target = *map_slot; + std::string* target = &it->second; // Find the insertion point. - string magic_string = strings::Substitute( - "@@protoc_insertion_point($0)", insertion_point_); - string::size_type pos = target->find(magic_string); + std::string magic_string = + strings::Substitute("@@protoc_insertion_point($0)", insertion_point_); + std::string::size_type pos = target->find(magic_string); - if (pos == string::npos) { + if (pos == std::string::npos) { std::cerr << filename_ << ": insertion point \"" << insertion_point_ << "\" not found." << std::endl; directory_->had_error_ = true; @@ -720,7 +712,7 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { // intentional because it means that multiple insertions at the same point // will end up in the expected order in the final output. pos = target->find_last_of('\n', pos); - if (pos == string::npos) { + if (pos == std::string::npos) { // Insertion point is on the first line. pos = 0; } else { @@ -730,7 +722,8 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { } // Extract indent. - string indent_(*target, pos, target->find_first_not_of(" \t", pos) - pos); + std::string indent_(*target, pos, + target->find_first_not_of(" \t", pos) - pos); if (indent_.empty()) { // No indent. This makes things easier. @@ -748,7 +741,7 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { UpdateMetadata(pos, data_.size() + indent_size); // Now copy in the data. - string::size_type data_pos = 0; + std::string::size_type data_pos = 0; char* target_ptr = ::google::protobuf::string_as_array(target) + pos; while (data_pos < data_.size()) { // Copy indent. @@ -758,7 +751,7 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { // Copy line from data_. // We already guaranteed that data_ ends with a newline (above), so this // search can't fail. - string::size_type line_length = + std::string::size_type line_length = data_.find_first_of('\n', data_pos) + 1 - data_pos; memcpy(target_ptr, data_.data() + data_pos, line_length); target_ptr += line_length; @@ -788,12 +781,13 @@ CommandLineInterface::CommandLineInterface() kDefaultDirectDependenciesViolationMsg), imports_in_descriptor_set_(false), source_info_in_descriptor_set_(false), - disallow_services_(false) {} + disallow_services_(false) { +} CommandLineInterface::~CommandLineInterface() {} -void CommandLineInterface::RegisterGenerator(const string& flag_name, +void CommandLineInterface::RegisterGenerator(const std::string& flag_name, CodeGenerator* generator, - const string& help_text) { + const std::string& help_text) { GeneratorInfo info; info.flag_name = flag_name; info.generator = generator; @@ -801,10 +795,9 @@ void CommandLineInterface::RegisterGenerator(const string& flag_name, generators_by_flag_name_[flag_name] = info; } -void CommandLineInterface::RegisterGenerator(const string& flag_name, - const string& option_flag_name, - CodeGenerator* generator, - const string& help_text) { +void CommandLineInterface::RegisterGenerator( + const std::string& flag_name, const std::string& option_flag_name, + CodeGenerator* generator, const std::string& help_text) { GeneratorInfo info; info.flag_name = flag_name; info.option_flag_name = option_flag_name; @@ -814,7 +807,7 @@ void CommandLineInterface::RegisterGenerator(const string& flag_name, generators_by_option_name_[option_flag_name] = info; } -void CommandLineInterface::AllowPlugins(const string& exe_name_prefix) { +void CommandLineInterface::AllowPlugins(const std::string& exe_name_prefix) { plugin_prefix_ = exe_name_prefix; } @@ -830,35 +823,51 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { } std::vector parsed_files; - // null unless descriptor_set_in_names_.empty() std::unique_ptr disk_source_tree; std::unique_ptr error_collector; std::unique_ptr descriptor_pool; - std::unique_ptr descriptor_database; - if (descriptor_set_in_names_.empty()) { + std::unique_ptr descriptor_set_in_database; + std::unique_ptr source_tree_database; + + // Any --descriptor_set_in FileDescriptorSet objects will be used as a + // fallback to input_files on command line, so create that db first. + if (!descriptor_set_in_names_.empty()) { + descriptor_set_in_database.reset(new SimpleDescriptorDatabase()); + if (!PopulateSimpleDescriptorDatabase(descriptor_set_in_database.get())) { + return 1; + } + } + + if (proto_path_.empty()) { + // If there are no --proto_path flags, then just look in the specified + // --descriptor_set_in files. But first, verify that the input files are + // there. + if (!VerifyInputFilesInDescriptors(descriptor_set_in_database.get())) { + return 1; + } + + error_collector.reset(new ErrorPrinter(error_format_)); + descriptor_pool.reset(new DescriptorPool(descriptor_set_in_database.get(), + error_collector.get())); + } else { disk_source_tree.reset(new DiskSourceTree()); - if (!InitializeDiskSourceTree(disk_source_tree.get())) { + if (!InitializeDiskSourceTree(disk_source_tree.get(), + descriptor_set_in_database.get())) { return 1; } + error_collector.reset( new ErrorPrinter(error_format_, disk_source_tree.get())); - SourceTreeDescriptorDatabase* database = - new SourceTreeDescriptorDatabase(disk_source_tree.get()); - database->RecordErrorsTo(error_collector.get()); - descriptor_database.reset(database); - descriptor_pool.reset(new DescriptorPool( - descriptor_database.get(), database->GetValidationErrorCollector())); - } else { - error_collector.reset(new ErrorPrinter(error_format_)); + source_tree_database.reset(new SourceTreeDescriptorDatabase( + disk_source_tree.get(), descriptor_set_in_database.get())); + source_tree_database->RecordErrorsTo(error_collector.get()); - SimpleDescriptorDatabase* database = new SimpleDescriptorDatabase(); - descriptor_database.reset(database); - if (!PopulateSimpleDescriptorDatabase(database)) { - return 1; - } - descriptor_pool.reset(new DescriptorPool(database, error_collector.get())); + descriptor_pool.reset(new DescriptorPool( + source_tree_database.get(), + source_tree_database->GetValidationErrorCollector())); } + descriptor_pool->EnforceWeakDependencies(true); if (!ParseInputFiles(descriptor_pool.get(), &parsed_files)) { return 1; @@ -873,33 +882,32 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { // Generate output. if (mode_ == MODE_COMPILE) { for (int i = 0; i < output_directives_.size(); i++) { - string output_location = output_directives_[i].output_location; + std::string output_location = output_directives_[i].output_location; if (!HasSuffixString(output_location, ".zip") && !HasSuffixString(output_location, ".jar")) { AddTrailingSlash(&output_location); } - GeneratorContextImpl** map_slot = &output_directories[output_location]; - if (*map_slot == NULL) { + auto& generator = output_directories[output_location]; + + if (!generator) { // First time we've seen this output location. - *map_slot = new GeneratorContextImpl(parsed_files); + generator.reset(new GeneratorContextImpl(parsed_files)); } - if (!GenerateOutput(parsed_files, output_directives_[i], *map_slot)) { - STLDeleteValues(&output_directories); + if (!GenerateOutput(parsed_files, output_directives_[i], + generator.get())) { return 1; } } } // Write all output to disk. - for (GeneratorContextMap::iterator iter = output_directories.begin(); - iter != output_directories.end(); ++iter) { - const string& location = iter->first; - GeneratorContextImpl* directory = iter->second; + for (const auto& pair : output_directories) { + const std::string& location = pair.first; + GeneratorContextImpl* directory = pair.second.get(); if (HasSuffixString(location, "/")) { if (!directory->WriteAllToDisk(location)) { - STLDeleteValues(&output_directories); return 1; } } else { @@ -908,7 +916,6 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { } if (!directory->WriteAllToZip(location)) { - STLDeleteValues(&output_directories); return 1; } } @@ -922,8 +929,6 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { } } - STLDeleteValues(&output_directories); - if (!descriptor_set_out_name_.empty()) { if (!WriteDescriptorSet(parsed_files)) { return 1; @@ -965,10 +970,10 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { break; case PRINT_NONE: GOOGLE_LOG(ERROR) << "If the code reaches here, it usually means a bug of " - "flag parsing in the CommandLineInterface."; + "flag parsing in the CommandLineInterface."; return 1; - // Do not add a default case. + // Do not add a default case. } } @@ -976,7 +981,7 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { } bool CommandLineInterface::InitializeDiskSourceTree( - DiskSourceTree* source_tree) { + DiskSourceTree* source_tree, DescriptorDatabase* fallback_database) { AddDefaultProtoPaths(&proto_path_); // Set up the source tree. @@ -985,7 +990,7 @@ bool CommandLineInterface::InitializeDiskSourceTree( } // Map input files to virtual paths if possible. - if (!MakeInputsBeProtoPathRelative(source_tree)) { + if (!MakeInputsBeProtoPathRelative(source_tree, fallback_database)) { return false; } @@ -1000,16 +1005,15 @@ bool CommandLineInterface::PopulateSimpleDescriptorDatabase( fd = open(descriptor_set_in_names_[i].c_str(), O_RDONLY | O_BINARY); } while (fd < 0 && errno == EINTR); if (fd < 0) { - std::cerr << descriptor_set_in_names_[i] << ": " - << strerror(ENOENT) << std::endl; + std::cerr << descriptor_set_in_names_[i] << ": " << strerror(ENOENT) + << std::endl; return false; } FileDescriptorSet file_descriptor_set; bool parsed = file_descriptor_set.ParseFromFileDescriptor(fd); if (close(fd) != 0) { - std::cerr << descriptor_set_in_names_[i] << ": close: " - << strerror(errno) + std::cerr << descriptor_set_in_names_[i] << ": close: " << strerror(errno) << std::endl; return false; } @@ -1035,6 +1039,27 @@ bool CommandLineInterface::PopulateSimpleDescriptorDatabase( return true; } +bool CommandLineInterface::VerifyInputFilesInDescriptors( + DescriptorDatabase* database) { + for (const auto& input_file : input_files_) { + FileDescriptorProto file_descriptor; + if (!database->FindFileByName(input_file, &file_descriptor)) { + std::cerr << input_file << ": " << strerror(ENOENT) << std::endl; + return false; + } + + // Enforce --disallow_services. + if (disallow_services_ && file_descriptor.service_size() > 0) { + std::cerr << file_descriptor.name() + << ": This file contains services, but " + "--disallow_services was used." + << std::endl; + return false; + } + } + return true; +} + bool CommandLineInterface::ParseInputFiles( DescriptorPool* descriptor_pool, std::vector* parsed_files) { @@ -1047,17 +1072,16 @@ bool CommandLineInterface::ParseInputFiles( descriptor_pool->FindFileByName(input_file); descriptor_pool->ClearUnusedImportTrackFiles(); if (parsed_file == NULL) { - if (!descriptor_set_in_names_.empty()) { - std::cerr << input_file << ": " << strerror(ENOENT) << std::endl; - } return false; } parsed_files->push_back(parsed_file); // Enforce --disallow_services. if (disallow_services_ && parsed_file->service_count() > 0) { - std::cerr << parsed_file->name() << ": This file contains services, but " - "--disallow_services was used." << std::endl; + std::cerr << parsed_file->name() + << ": This file contains services, but " + "--disallow_services was used." + << std::endl; return false; } @@ -1107,40 +1131,53 @@ void CommandLineInterface::Clear() { } bool CommandLineInterface::MakeProtoProtoPathRelative( - DiskSourceTree* source_tree, string* proto) { + DiskSourceTree* source_tree, std::string* proto, + DescriptorDatabase* fallback_database) { + // If it's in the fallback db, don't report non-existent file errors. + FileDescriptorProto fallback_file; + bool in_fallback_database = + fallback_database != nullptr && + fallback_database->FindFileByName(*proto, &fallback_file); + // If the input file path is not a physical file path, it must be a virtual // path. if (access(proto->c_str(), F_OK) < 0) { - string disk_file; - if (source_tree->VirtualFileToDiskFile(*proto, &disk_file)) { + std::string disk_file; + if (source_tree->VirtualFileToDiskFile(*proto, &disk_file) || + in_fallback_database) { return true; } else { std::cerr << *proto << ": " << strerror(ENOENT) << std::endl; return false; } } - string virtual_file, shadowing_disk_file; - switch (source_tree->DiskFileToVirtualFile( - *proto, &virtual_file, &shadowing_disk_file)) { + + std::string virtual_file, shadowing_disk_file; + switch (source_tree->DiskFileToVirtualFile(*proto, &virtual_file, + &shadowing_disk_file)) { case DiskSourceTree::SUCCESS: *proto = virtual_file; break; case DiskSourceTree::SHADOWED: - std::cerr << *proto - << ": Input is shadowed in the --proto_path by \"" + std::cerr << *proto << ": Input is shadowed in the --proto_path by \"" << shadowing_disk_file << "\". Either use the latter file as your input or reorder " "the --proto_path so that the former file's location " - "comes first." << std::endl; + "comes first." + << std::endl; return false; case DiskSourceTree::CANNOT_OPEN: + if (in_fallback_database) { + return true; + } std::cerr << *proto << ": " << strerror(errno) << std::endl; return false; case DiskSourceTree::NO_MAPPING: { // Try to interpret the path as a virtual path. - string disk_file; - if (source_tree->VirtualFileToDiskFile(*proto, &disk_file)) { - return true; + std::string disk_file; + if (source_tree->VirtualFileToDiskFile(*proto, &disk_file) || + in_fallback_database) { + return true; } else { // The input file path can't be mapped to any --proto_path and it also // can't be interpreted as a virtual path. @@ -1162,9 +1199,10 @@ bool CommandLineInterface::MakeProtoProtoPathRelative( } bool CommandLineInterface::MakeInputsBeProtoPathRelative( - DiskSourceTree* source_tree) { + DiskSourceTree* source_tree, DescriptorDatabase* fallback_database) { for (auto& input_file : input_files_) { - if (!MakeProtoProtoPathRelative(source_tree, &input_file)) { + if (!MakeProtoProtoPathRelative(source_tree, &input_file, + fallback_database)) { return false; } } @@ -1173,15 +1211,15 @@ bool CommandLineInterface::MakeInputsBeProtoPathRelative( } -bool CommandLineInterface::ExpandArgumentFile(const string& file, - std::vector* arguments) { +bool CommandLineInterface::ExpandArgumentFile( + const std::string& file, std::vector* arguments) { // The argument file is searched in the working directory only. We don't // use the proto import path here. std::ifstream file_stream(file.c_str()); if (!file_stream.is_open()) { return false; } - string argument; + std::string argument; // We don't support any kind of shell expansion right now. while (std::getline(file_stream, argument)) { arguments->push_back(argument); @@ -1189,11 +1227,11 @@ bool CommandLineInterface::ExpandArgumentFile(const string& file, return true; } -CommandLineInterface::ParseArgumentStatus -CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { +CommandLineInterface::ParseArgumentStatus CommandLineInterface::ParseArguments( + int argc, const char* const argv[]) { executable_name_ = argv[0]; - std::vector arguments; + std::vector arguments; for (int i = 1; i < argc; ++i) { if (argv[i][0] == '@') { if (!ExpandArgumentFile(argv[i] + 1, &arguments)) { @@ -1214,7 +1252,7 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { // Iterate through all arguments and parse them. for (int i = 0; i < arguments.size(); ++i) { - string name, value; + std::string name, value; if (ParseArgument(arguments[i].c_str(), &name, &value)) { // Returned true => Use the next argument as the flag value. @@ -1232,22 +1270,23 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { } ParseArgumentStatus status = InterpretArgument(name, value); - if (status != PARSE_ARGUMENT_DONE_AND_CONTINUE) - return status; + if (status != PARSE_ARGUMENT_DONE_AND_CONTINUE) return status; } // Make sure each plugin option has a matching plugin output. bool foundUnknownPluginOption = false; - for (std::map::const_iterator i = plugin_parameters_.begin(); + for (std::map::const_iterator i = + plugin_parameters_.begin(); i != plugin_parameters_.end(); ++i) { if (plugins_.find(i->first) != plugins_.end()) { continue; } bool foundImplicitPlugin = false; - for (std::vector::const_iterator j = output_directives_.begin(); + for (std::vector::const_iterator j = + output_directives_.begin(); j != output_directives_.end(); ++j) { if (j->generator == NULL) { - string plugin_name = PluginName(plugin_prefix_ , j->name); + std::string plugin_name = PluginName(plugin_prefix_, j->name); if (plugin_name == i->first) { foundImplicitPlugin = true; break; @@ -1266,15 +1305,16 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { return PARSE_ARGUMENT_FAIL; } - // If no --proto_path was given, use the current working directory. - if (proto_path_.empty()) { + // The --proto_path & --descriptor_set_in flags both specify places to look + // for proto files. If neither were given, use the current working directory. + if (proto_path_.empty() && descriptor_set_in_names_.empty()) { // Don't use make_pair as the old/default standard library on Solaris // doesn't support it without explicit template parameters, which are // incompatible with C++0x's make_pair. - proto_path_.push_back(std::pair("", ".")); + proto_path_.push_back(std::pair("", ".")); } - // Check some errror cases. + // Check some error cases. bool decoding_raw = (mode_ == MODE_DECODE) && codec_type_.empty(); if (decoding_raw && !input_files_.empty()) { std::cerr << "When using --decode_raw, no input files should be given." @@ -1302,18 +1342,20 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { } if (imports_in_descriptor_set_ && descriptor_set_out_name_.empty()) { std::cerr << "--include_imports only makes sense when combined with " - "--descriptor_set_out." << std::endl; + "--descriptor_set_out." + << std::endl; } if (source_info_in_descriptor_set_ && descriptor_set_out_name_.empty()) { std::cerr << "--include_source_info only makes sense when combined with " - "--descriptor_set_out." << std::endl; + "--descriptor_set_out." + << std::endl; } return PARSE_ARGUMENT_DONE_AND_CONTINUE; } -bool CommandLineInterface::ParseArgument(const char* arg, - string* name, string* value) { +bool CommandLineInterface::ParseArgument(const char* arg, std::string* name, + std::string* value) { bool parsed_value = false; if (arg[0] != '-') { @@ -1326,7 +1368,7 @@ bool CommandLineInterface::ParseArgument(const char* arg, // value. const char* equals_pos = strchr(arg, '='); if (equals_pos != NULL) { - *name = string(arg, equals_pos - arg); + *name = std::string(arg, equals_pos - arg); *value = equals_pos + 1; parsed_value = true; } else { @@ -1342,7 +1384,7 @@ bool CommandLineInterface::ParseArgument(const char* arg, *value = arg; parsed_value = true; } else { - *name = string(arg, 2); + *name = std::string(arg, 2); *value = arg + 2; parsed_value = !value->empty(); } @@ -1356,12 +1398,9 @@ bool CommandLineInterface::ParseArgument(const char* arg, return false; } - if (*name == "-h" || *name == "--help" || - *name == "--disallow_services" || - *name == "--include_imports" || - *name == "--include_source_info" || - *name == "--version" || - *name == "--decode_raw" || + if (*name == "-h" || *name == "--help" || *name == "--disallow_services" || + *name == "--include_imports" || *name == "--include_source_info" || + *name == "--version" || *name == "--decode_raw" || *name == "--print_free_field_numbers") { // HACK: These are the only flags that don't take a value. // They probably should not be hard-coded like this but for now it's @@ -1374,17 +1413,19 @@ bool CommandLineInterface::ParseArgument(const char* arg, } CommandLineInterface::ParseArgumentStatus -CommandLineInterface::InterpretArgument(const string& name, - const string& value) { +CommandLineInterface::InterpretArgument(const std::string& name, + const std::string& value) { if (name.empty()) { // Not a flag. Just a filename. if (value.empty()) { std::cerr << "You seem to have passed an empty string as one of the " - "arguments to " << executable_name_ + "arguments to " + << executable_name_ << ". This is actually " "sort of hard to do. Congrats. Unfortunately it is not valid " - "input so the program is going to die now." << std::endl; + "input so the program is going to die now." + << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1416,26 +1457,19 @@ CommandLineInterface::InterpretArgument(const string& name, #endif // _WIN32 } else if (name == "-I" || name == "--proto_path") { - if (!descriptor_set_in_names_.empty()) { - std::cerr << "Only one of " << name - << " and --descriptor_set_in can be specified." - << std::endl; - return PARSE_ARGUMENT_FAIL; - } - // Java's -classpath (and some other languages) delimits path components // with colons. Let's accept that syntax too just to make things more // intuitive. - std::vector parts = Split( + std::vector parts = Split( value, CommandLineInterface::kPathSeparator, true); for (int i = 0; i < parts.size(); i++) { - string virtual_path; - string disk_path; + std::string virtual_path; + std::string disk_path; - string::size_type equals_pos = parts[i].find_first_of('='); - if (equals_pos == string::npos) { + std::string::size_type equals_pos = parts[i].find_first_of('='); + if (equals_pos == std::string::npos) { virtual_path = ""; disk_path = parts[i]; } else { @@ -1446,7 +1480,8 @@ CommandLineInterface::InterpretArgument(const string& name, if (disk_path.empty()) { std::cerr << "--proto_path passed empty directory name. (Use \".\" for " - "current directory.)" << std::endl; + "current directory.)" + << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1465,20 +1500,23 @@ CommandLineInterface::InterpretArgument(const string& name, // Don't use make_pair as the old/default standard library on Solaris // doesn't support it without explicit template parameters, which are // incompatible with C++0x's make_pair. - proto_path_.push_back(std::pair(virtual_path, disk_path)); + proto_path_.push_back( + std::pair(virtual_path, disk_path)); } } else if (name == "--direct_dependencies") { if (direct_dependencies_explicitly_set_) { - std::cerr << name << " may only be passed once. To specify multiple " - "direct dependencies, pass them all as a single " - "parameter separated by ':'." + std::cerr << name + << " may only be passed once. To specify multiple " + "direct dependencies, pass them all as a single " + "parameter separated by ':'." << std::endl; return PARSE_ARGUMENT_FAIL; } direct_dependencies_explicitly_set_ = true; - std::vector direct = Split(value, ":", true); + std::vector direct = + Split(value, ":", true); GOOGLE_DCHECK(direct_dependencies_.empty()); direct_dependencies_.insert(direct.begin(), direct.end()); @@ -1487,23 +1525,17 @@ CommandLineInterface::InterpretArgument(const string& name, } else if (name == "--descriptor_set_in") { if (!descriptor_set_in_names_.empty()) { - std::cerr << name << " may only be passed once. To specify multiple " - "descriptor sets, pass them all as a single " - "parameter separated by '" - << CommandLineInterface::kPathSeparator << "'." - << std::endl; + std::cerr << name + << " may only be passed once. To specify multiple " + "descriptor sets, pass them all as a single " + "parameter separated by '" + << CommandLineInterface::kPathSeparator << "'." << std::endl; return PARSE_ARGUMENT_FAIL; } if (value.empty()) { std::cerr << name << " requires a non-empty value." << std::endl; return PARSE_ARGUMENT_FAIL; } - if (!proto_path_.empty()) { - std::cerr << "Only one of " << name - << " and --proto_path can be specified." - << std::endl; - return PARSE_ARGUMENT_FAIL; - } if (!dependency_out_name_.empty()) { std::cerr << name << " cannot be used with --dependency_out." << std::endl; @@ -1526,7 +1558,8 @@ CommandLineInterface::InterpretArgument(const string& name, if (mode_ != MODE_COMPILE) { std::cerr << "Cannot use --encode or --decode and generate descriptors at the " - "same time." << std::endl; + "same time." + << std::endl; return PARSE_ARGUMENT_FAIL; } descriptor_set_out_name_ = value; @@ -1569,9 +1602,8 @@ CommandLineInterface::InterpretArgument(const string& name, if (!version_info_.empty()) { std::cout << version_info_ << std::endl; } - std::cout << "libprotoc " - << protobuf::internal::VersionString(GOOGLE_PROTOBUF_VERSION) - << std::endl; + std::cout << "libprotoc " << internal::VersionString(PROTOBUF_VERSION) + << std::endl; return PARSE_ARGUMENT_DONE_AND_EXIT; // Exit without running compiler. } else if (name == "--disallow_services") { @@ -1623,14 +1655,14 @@ CommandLineInterface::InterpretArgument(const string& name, return PARSE_ARGUMENT_FAIL; } - string plugin_name; - string path; + std::string plugin_name; + std::string path; - string::size_type equals_pos = value.find_first_of('='); - if (equals_pos == string::npos) { + std::string::size_type equals_pos = value.find_first_of('='); + if (equals_pos == std::string::npos) { // Use the basename of the file. - string::size_type slash_pos = value.find_last_of('/'); - if (slash_pos == string::npos) { + std::string::size_type slash_pos = value.find_last_of('/'); + if (slash_pos == std::string::npos) { plugin_name = value; } else { plugin_name = value.substr(slash_pos + 1); @@ -1667,13 +1699,14 @@ CommandLineInterface::InterpretArgument(const string& name, // Check if it's a generator option flag. generator_info = FindOrNull(generators_by_option_name_, name); if (generator_info != NULL) { - string* parameters = &generator_parameters_[generator_info->flag_name]; + std::string* parameters = + &generator_parameters_[generator_info->flag_name]; if (!parameters->empty()) { parameters->append(","); } parameters->append(value); } else if (HasPrefixString(name, "--") && HasSuffixString(name, "_opt")) { - string* parameters = + std::string* parameters = &plugin_parameters_[PluginName(plugin_prefix_, name)]; if (!parameters->empty()) { parameters->append(","); @@ -1687,7 +1720,8 @@ CommandLineInterface::InterpretArgument(const string& name, // It's an output flag. Add it to the output directives. if (mode_ != MODE_COMPILE) { std::cerr << "Cannot use --encode, --decode or print .proto info and " - "generate code at the same time." << std::endl; + "generate code at the same time." + << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1702,8 +1736,8 @@ CommandLineInterface::InterpretArgument(const string& name, // Split value at ':' to separate the generator parameter from the // filename. However, avoid doing this if the colon is part of a valid // Windows-style absolute path. - string::size_type colon_pos = value.find_first_of(':'); - if (colon_pos == string::npos || IsWindowsAbsolutePath(value)) { + std::string::size_type colon_pos = value.find_first_of(':'); + if (colon_pos == std::string::npos || IsWindowsAbsolutePath(value)) { directive.output_location = value; } else { directive.parameter = value.substr(0, colon_pos); @@ -1719,70 +1753,124 @@ CommandLineInterface::InterpretArgument(const string& name, void CommandLineInterface::PrintHelpText() { // Sorry for indentation here; line wrapping would be uglier. - std::cout << -"Usage: " << executable_name_ << " [OPTION] PROTO_FILES\n" -"Parse PROTO_FILES and generate output based on the options given:\n" -" -IPATH, --proto_path=PATH Specify the directory in which to search for\n" -" imports. May be specified multiple times;\n" -" directories will be searched in order. If not\n" -" given, the current working directory is used.\n" -" --version Show version info and exit.\n" -" -h, --help Show this text and exit.\n" -" --encode=MESSAGE_TYPE Read a text-format message of the given type\n" -" from standard input and write it in binary\n" -" to standard output. The message type must\n" -" be defined in PROTO_FILES or their imports.\n" -" --decode=MESSAGE_TYPE Read a binary message of the given type from\n" -" standard input and write it in text format\n" -" to standard output. The message type must\n" -" be defined in PROTO_FILES or their imports.\n" -" --decode_raw Read an arbitrary protocol message from\n" -" standard input and write the raw tag/value\n" -" pairs in text format to standard output. No\n" -" PROTO_FILES should be given when using this\n" -" flag.\n" -" --descriptor_set_in=FILES Specifies a delimited list of FILES\n" -" each containing a FileDescriptorSet (a\n" -" protocol buffer defined in descriptor.proto).\n" -" The FileDescriptor for each of the PROTO_FILES\n" -" provided will be loaded from these\n" -" FileDescriptorSets. If a FileDescriptor\n" -" appears multiple times, the first occurrence\n" -" will be used.\n" -" -oFILE, Writes a FileDescriptorSet (a protocol buffer,\n" -" --descriptor_set_out=FILE defined in descriptor.proto) containing all of\n" -" the input files to FILE.\n" -" --include_imports When using --descriptor_set_out, also include\n" -" all dependencies of the input files in the\n" -" set, so that the set is self-contained.\n" -" --include_source_info When using --descriptor_set_out, do not strip\n" -" SourceCodeInfo from the FileDescriptorProto.\n" -" This results in vastly larger descriptors that\n" -" include information about the original\n" -" location of each decl in the source file as\n" -" well as surrounding comments.\n" -" --dependency_out=FILE Write a dependency output file in the format\n" -" expected by make. This writes the transitive\n" -" set of input file paths to FILE\n" -" --error_format=FORMAT Set the format in which to print errors.\n" -" FORMAT may be 'gcc' (the default) or 'msvs'\n" -" (Microsoft Visual Studio format).\n" -" --print_free_field_numbers Print the free field numbers of the messages\n" -" defined in the given proto files. Groups share\n" -" the same field number space with the parent \n" -" message. Extension ranges are counted as \n" -" occupied fields numbers.\n" + std::cout + << + "Usage: " << executable_name_ + << " [OPTION] PROTO_FILES\n" + "Parse PROTO_FILES and generate output based on the options given:\n" + " -IPATH, --proto_path=PATH Specify the directory in which to " + "search for\n" + " imports. May be specified multiple " + "times;\n" + " directories will be searched in order. " + " If not\n" + " given, the current working directory " + "is used.\n" + " If not found in any of the these " + "directories,\n" + " the --descriptor_set_in descriptors " + "will be\n" + " checked for required proto file.\n" + " --version Show version info and exit.\n" + " -h, --help Show this text and exit.\n" + " --encode=MESSAGE_TYPE Read a text-format message of the " + "given type\n" + " from standard input and write it in " + "binary\n" + " to standard output. The message type " + "must\n" + " be defined in PROTO_FILES or their " + "imports.\n" + " --decode=MESSAGE_TYPE Read a binary message of the given " + "type from\n" + " standard input and write it in text " + "format\n" + " to standard output. The message type " + "must\n" + " be defined in PROTO_FILES or their " + "imports.\n" + " --decode_raw Read an arbitrary protocol message " + "from\n" + " standard input and write the raw " + "tag/value\n" + " pairs in text format to standard " + "output. No\n" + " PROTO_FILES should be given when using " + "this\n" + " flag.\n" + " --descriptor_set_in=FILES Specifies a delimited list of FILES\n" + " each containing a FileDescriptorSet " + "(a\n" + " protocol buffer defined in " + "descriptor.proto).\n" + " The FileDescriptor for each of the " + "PROTO_FILES\n" + " provided will be loaded from these\n" + " FileDescriptorSets. If a " + "FileDescriptor\n" + " appears multiple times, the first " + "occurrence\n" + " will be used.\n" + " -oFILE, Writes a FileDescriptorSet (a protocol " + "buffer,\n" + " --descriptor_set_out=FILE defined in descriptor.proto) " + "containing all of\n" + " the input files to FILE.\n" + " --include_imports When using --descriptor_set_out, also " + "include\n" + " all dependencies of the input files in " + "the\n" + " set, so that the set is " + "self-contained.\n" + " --include_source_info When using --descriptor_set_out, do " + "not strip\n" + " SourceCodeInfo from the " + "FileDescriptorProto.\n" + " This results in vastly larger " + "descriptors that\n" + " include information about the " + "original\n" + " location of each decl in the source " + "file as\n" + " well as surrounding comments.\n" + " --dependency_out=FILE Write a dependency output file in the " + "format\n" + " expected by make. This writes the " + "transitive\n" + " set of input file paths to FILE\n" + " --error_format=FORMAT Set the format in which to print " + "errors.\n" + " FORMAT may be 'gcc' (the default) or " + "'msvs'\n" + " (Microsoft Visual Studio format).\n" + " --print_free_field_numbers Print the free field numbers of the " + "messages\n" + " defined in the given proto files. " + "Groups share\n" + " the same field number space with the " + "parent \n" + " message. Extension ranges are counted " + "as \n" + " occupied fields numbers.\n" << std::endl; if (!plugin_prefix_.empty()) { - std::cout << -" --plugin=EXECUTABLE Specifies a plugin executable to use.\n" -" Normally, protoc searches the PATH for\n" -" plugins, but you may specify additional\n" -" executables not in the path using this flag.\n" -" Additionally, EXECUTABLE may be of the form\n" -" NAME=PATH, in which case the given plugin name\n" -" is mapped to the given executable even if\n" -" the executable's own name differs." << std::endl; + std::cout + << " --plugin=EXECUTABLE Specifies a plugin executable to " + "use.\n" + " Normally, protoc searches the PATH " + "for\n" + " plugins, but you may specify " + "additional\n" + " executables not in the path using " + "this flag.\n" + " Additionally, EXECUTABLE may be of " + "the form\n" + " NAME=PATH, in which case the given " + "plugin name\n" + " is mapped to the given executable " + "even if\n" + " the executable's own name differs." + << std::endl; } for (GeneratorMap::iterator iter = generators_by_flag_name_.begin(); @@ -1791,23 +1879,34 @@ void CommandLineInterface::PrintHelpText() { // but fixing this nicely (e.g. splitting on spaces) is probably more // trouble than it's worth. std::cout << " " << iter->first << "=OUT_DIR " - << string(19 - iter->first.size(), ' ') // Spaces for alignment. + << std::string(19 - iter->first.size(), + ' ') // Spaces for alignment. << iter->second.help_text << std::endl; } - std::cerr << -" @ Read options and filenames from file. If a\n" -" relative file path is specified, the file\n" -" will be searched in the working directory.\n" -" The --proto_path option will not affect how\n" -" this argument file is searched. Content of\n" -" the file will be expanded in the position of\n" -" @ as in the argument list. Note\n" -" that shell expansion is not applied to the\n" -" content of the file (i.e., you cannot use\n" -" quotes, wildcards, escapes, commands, etc.).\n" -" Each line corresponds to a single argument,\n" -" even if it contains spaces." - << std::endl; + std::cout << " @ Read options and filenames from " + "file. If a\n" + " relative file path is specified, " + "the file\n" + " will be searched in the working " + "directory.\n" + " The --proto_path option will not " + "affect how\n" + " this argument file is searched. " + "Content of\n" + " the file will be expanded in the " + "position of\n" + " @ as in the argument " + "list. Note\n" + " that shell expansion is not " + "applied to the\n" + " content of the file (i.e., you " + "cannot use\n" + " quotes, wildcards, escapes, " + "commands, etc.).\n" + " Each line corresponds to a " + "single argument,\n" + " even if it contains spaces." + << std::endl; } bool CommandLineInterface::GenerateOutput( @@ -1815,38 +1914,37 @@ bool CommandLineInterface::GenerateOutput( const OutputDirective& output_directive, GeneratorContext* generator_context) { // Call the generator. - string error; + std::string error; if (output_directive.generator == NULL) { // This is a plugin. GOOGLE_CHECK(HasPrefixString(output_directive.name, "--") && HasSuffixString(output_directive.name, "_out")) << "Bad name for plugin generator: " << output_directive.name; - string plugin_name = PluginName(plugin_prefix_ , output_directive.name); - string parameters = output_directive.parameter; + std::string plugin_name = PluginName(plugin_prefix_, output_directive.name); + std::string parameters = output_directive.parameter; if (!plugin_parameters_[plugin_name].empty()) { if (!parameters.empty()) { parameters.append(","); } parameters.append(plugin_parameters_[plugin_name]); } - if (!GeneratePluginOutput(parsed_files, plugin_name, - parameters, + if (!GeneratePluginOutput(parsed_files, plugin_name, parameters, generator_context, &error)) { std::cerr << output_directive.name << ": " << error << std::endl; return false; } } else { // Regular generator. - string parameters = output_directive.parameter; + std::string parameters = output_directive.parameter; if (!generator_parameters_[output_directive.name].empty()) { if (!parameters.empty()) { parameters.append(","); } parameters.append(generator_parameters_[output_directive.name]); } - if (!output_directive.generator->GenerateAll( - parsed_files, parameters, generator_context, &error)) { + if (!output_directive.generator->GenerateAll(parsed_files, parameters, + generator_context, &error)) { // Generator returned an error. std::cerr << output_directive.name << ": " << error << std::endl; return false; @@ -1864,22 +1962,18 @@ bool CommandLineInterface::GenerateDependencyManifestFile( std::set already_seen; for (int i = 0; i < parsed_files.size(); i++) { - GetTransitiveDependencies(parsed_files[i], - false, - false, - &already_seen, + GetTransitiveDependencies(parsed_files[i], false, false, &already_seen, file_set.mutable_file()); } - std::vector output_filenames; - for (GeneratorContextMap::const_iterator iter = output_directories.begin(); - iter != output_directories.end(); ++iter) { - const string& location = iter->first; - GeneratorContextImpl* directory = iter->second; - std::vector relative_output_filenames; + std::vector output_filenames; + for (const auto& pair : output_directories) { + const std::string& location = pair.first; + GeneratorContextImpl* directory = pair.second.get(); + std::vector relative_output_filenames; directory->GetOutputFilenames(&relative_output_filenames); for (int i = 0; i < relative_output_filenames.size(); i++) { - string output_filename = location + relative_output_filenames[i]; + std::string output_filename = location + relative_output_filenames[i]; if (output_filename.compare(0, 2, "./") == 0) { output_filename = output_filename.substr(2); } @@ -1912,8 +2006,8 @@ bool CommandLineInterface::GenerateDependencyManifestFile( for (int i = 0; i < file_set.file_size(); i++) { const FileDescriptorProto& file = file_set.file(i); - const string& virtual_file = file.name(); - string disk_file; + const std::string& virtual_file = file.name(); + std::string disk_file; if (source_tree && source_tree->VirtualFileToDiskFile(virtual_file, &disk_file)) { printer.Print(" $disk_file$", "disk_file", disk_file); @@ -1930,13 +2024,11 @@ bool CommandLineInterface::GenerateDependencyManifestFile( bool CommandLineInterface::GeneratePluginOutput( const std::vector& parsed_files, - const string& plugin_name, - const string& parameter, - GeneratorContext* generator_context, - string* error) { + const std::string& plugin_name, const std::string& parameter, + GeneratorContext* generator_context, std::string* error) { CodeGeneratorRequest request; CodeGeneratorResponse response; - string processed_parameter = parameter; + std::string processed_parameter = parameter; // Build the request. @@ -1956,10 +2048,10 @@ bool CommandLineInterface::GeneratePluginOutput( google::protobuf::compiler::Version* version = request.mutable_compiler_version(); - version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000); - version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000); - version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000); - version->set_suffix(GOOGLE_PROTOBUF_VERSION_SUFFIX); + version->set_major(PROTOBUF_VERSION / 1000000); + version->set_minor(PROTOBUF_VERSION / 1000 % 1000); + version->set_patch(PROTOBUF_VERSION % 1000); + version->set_suffix(PROTOBUF_VERSION_SUFFIX); // Invoke the plugin. Subprocess subprocess; @@ -1970,7 +2062,7 @@ bool CommandLineInterface::GeneratePluginOutput( subprocess.Start(plugin_name, Subprocess::SEARCH_PATH); } - string communicate_error; + std::string communicate_error; if (!subprocess.Communicate(request, &response, &communicate_error)) { *error = strings::Substitute("$0: $1", plugin_name, communicate_error); return false; @@ -1983,7 +2075,7 @@ bool CommandLineInterface::GeneratePluginOutput( const CodeGeneratorResponse::File& output_file = response.file(i); if (!output_file.insertion_point().empty()) { - string filename = output_file.name(); + std::string filename = output_file.name(); // Open a file for insert. // We reset current_output to NULL first so that the old file is closed // before the new one is opened. @@ -1998,8 +2090,9 @@ bool CommandLineInterface::GeneratePluginOutput( current_output.reset(generator_context->Open(output_file.name())); } else if (current_output == NULL) { *error = strings::Substitute( - "$0: First file chunk returned by plugin did not specify a file name.", - plugin_name); + "$0: First file chunk returned by plugin did not specify a file " + "name.", + plugin_name); return false; } @@ -2108,8 +2201,8 @@ bool CommandLineInterface::WriteDescriptorSet( for (int i = 0; i < parsed_files.size(); i++) { GetTransitiveDependencies(parsed_files[i], true, // Include json_name - source_info_in_descriptor_set_, - &already_seen, file_set.mutable_file()); + source_info_in_descriptor_set_, &already_seen, + file_set.mutable_file()); } int fd; @@ -2140,8 +2233,7 @@ bool CommandLineInterface::WriteDescriptorSet( } void CommandLineInterface::GetTransitiveDependencies( - const FileDescriptor* file, - bool include_json_name, + const FileDescriptor* file, bool include_json_name, bool include_source_code_info, std::set* already_seen, RepeatedPtrField* output) { @@ -2152,10 +2244,8 @@ void CommandLineInterface::GetTransitiveDependencies( // Add all dependencies. for (int i = 0; i < file->dependency_count(); i++) { - GetTransitiveDependencies(file->dependency(i), - include_json_name, - include_source_code_info, - already_seen, output); + GetTransitiveDependencies(file->dependency(i), include_json_name, + include_source_code_info, already_seen, output); } // Add this file. @@ -2237,9 +2327,9 @@ void GatherOccupiedFieldRanges( // Utility function for PrintFreeFieldNumbers. // Actually prints the formatted free field numbers for given message name and // occupied ranges. -void FormatFreeFieldNumbers(const string& name, +void FormatFreeFieldNumbers(const std::string& name, const std::set& ranges) { - string output; + std::string output; StringAppendF(&output, "%-35s free:", name.c_str()); int next_free_number = 1; for (std::set::const_iterator i = ranges.begin(); @@ -2267,8 +2357,7 @@ void FormatFreeFieldNumbers(const string& name, } // namespace -void CommandLineInterface::PrintFreeFieldNumbers( - const Descriptor* descriptor) { +void CommandLineInterface::PrintFreeFieldNumbers(const Descriptor* descriptor) { std::set ranges; std::vector nested_messages; GatherOccupiedFieldRanges(descriptor, &ranges, &nested_messages); @@ -2280,7 +2369,6 @@ void CommandLineInterface::PrintFreeFieldNumbers( } - } // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/command_line_interface.h b/src/google/protobuf/compiler/command_line_interface.h index 8dba994961..49d7259436 100644 --- a/src/google/protobuf/compiler/command_line_interface.h +++ b/src/google/protobuf/compiler/command_line_interface.h @@ -39,6 +39,7 @@ #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ #include +#include #include #include #include @@ -46,17 +47,20 @@ #include #include +#include namespace google { namespace protobuf { -class Descriptor; // descriptor.h -class DescriptorPool; // descriptor.h -class FileDescriptor; // descriptor.h -class FileDescriptorSet; // descriptor.h -class FileDescriptorProto; // descriptor.pb.h -template class RepeatedPtrField; // repeated_field.h -class SimpleDescriptorDatabase; // descriptor_database.h +class Descriptor; // descriptor.h +class DescriptorDatabase; // descriptor_database.h +class DescriptorPool; // descriptor.h +class FileDescriptor; // descriptor.h +class FileDescriptorSet; // descriptor.h +class FileDescriptorProto; // descriptor.pb.h +template +class RepeatedPtrField; // repeated_field.h +class SimpleDescriptorDatabase; // descriptor_database.h namespace compiler { @@ -102,7 +106,7 @@ class DiskSourceTree; // importer.h // relative virtual path, the physical file path takes precendence. // // For a full description of the command-line syntax, invoke it with --help. -class LIBPROTOC_EXPORT CommandLineInterface { +class PROTOC_EXPORT CommandLineInterface { public: static const char* const kPathSeparator; @@ -125,9 +129,8 @@ class LIBPROTOC_EXPORT CommandLineInterface { // protoc --foo_out=enable_bar:outdir // The text before the colon is passed to CodeGenerator::Generate() as the // "parameter". - void RegisterGenerator(const string& flag_name, - CodeGenerator* generator, - const string& help_text); + void RegisterGenerator(const std::string& flag_name, CodeGenerator* generator, + const std::string& help_text); // Register a code generator for a language. // Besides flag_name you can specify another option_flag_name that could be @@ -137,10 +140,10 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Then you could invoke the compiler with a command like: // protoc --foo_out=enable_bar:outdir --foo_opt=enable_baz // This will pass "enable_bar,enable_baz" as the parameter to the generator. - void RegisterGenerator(const string& flag_name, - const string& option_flag_name, + void RegisterGenerator(const std::string& flag_name, + const std::string& option_flag_name, CodeGenerator* generator, - const string& help_text); + const std::string& help_text); // Enables "plugins". In this mode, if a command-line flag ends with "_out" // but does not match any registered generator, the compiler will attempt to @@ -178,7 +181,7 @@ class LIBPROTOC_EXPORT CommandLineInterface { // protoc --plug_out=enable_bar:outdir --plug_opt=enable_baz // This will pass "enable_bar,enable_baz" as the parameter to the plugin. // - void AllowPlugins(const string& exe_name_prefix); + void AllowPlugins(const std::string& exe_name_prefix); // Run the Protocol Compiler with the given command-line parameters. // Returns the error code which should be returned by main(). @@ -196,9 +199,7 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Provides some text which will be printed when the --version flag is // used. The version of libprotoc will also be printed on the next line // after this text. - void SetVersionInfo(const string& text) { - version_info_ = text; - } + void SetVersionInfo(const std::string& text) { version_info_ = text; } private: @@ -207,19 +208,22 @@ class LIBPROTOC_EXPORT CommandLineInterface { class ErrorPrinter; class GeneratorContextImpl; class MemoryOutputStream; - typedef std::unordered_map GeneratorContextMap; + typedef std::unordered_map> + GeneratorContextMap; // Clear state from previous Run(). void Clear(); - // Remaps the proto file so that it is relative to one of the ddirectories + // Remaps the proto file so that it is relative to one of the directories // in proto_path_. Returns false if an error occurred. - bool MakeProtoProtoPathRelative(DiskSourceTree* source_tree, string* proto); + bool MakeProtoProtoPathRelative(DiskSourceTree* source_tree, + std::string* proto, + DescriptorDatabase* fallback_database); // Remaps each file in input_files_ so that it is relative to one of the // directories in proto_path_. Returns false if an error occurred. - bool MakeInputsBeProtoPathRelative( - DiskSourceTree* source_tree); + bool MakeInputsBeProtoPathRelative(DiskSourceTree* source_tree, + DescriptorDatabase* fallback_database); // Return status for ParseArguments() and InterpretArgument(). @@ -234,7 +238,8 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Read an argument file and append the file's content to the list of // arguments. Return false if the file cannot be read. - bool ExpandArgumentFile(const string& file, std::vector* arguments); + bool ExpandArgumentFile(const std::string& file, + std::vector* arguments); // Parses a command-line argument into a name/value pair. Returns // true if the next argument in the argv should be used as the value, @@ -247,17 +252,21 @@ class LIBPROTOC_EXPORT CommandLineInterface { // name = "--cpp_out", value = "src/foo.pb2.cc" // "foo.proto" -> // name = "", value = "foo.proto" - bool ParseArgument(const char* arg, string* name, string* value); + bool ParseArgument(const char* arg, std::string* name, std::string* value); // Interprets arguments parsed with ParseArgument. - ParseArgumentStatus InterpretArgument(const string& name, - const string& value); + ParseArgumentStatus InterpretArgument(const std::string& name, + const std::string& value); // Print the --help text to stderr. void PrintHelpText(); // Loads proto_path_ into the provided source_tree. - bool InitializeDiskSourceTree(DiskSourceTree* source_tree); + bool InitializeDiskSourceTree(DiskSourceTree* source_tree, + DescriptorDatabase* fallback_database); + + // Verify that all the input files exist in the given database. + bool VerifyInputFilesInDescriptors(DescriptorDatabase* fallback_database); // Loads descriptor_set_in into the provided database bool PopulateSimpleDescriptorDatabase(SimpleDescriptorDatabase* database); @@ -273,8 +282,8 @@ class LIBPROTOC_EXPORT CommandLineInterface { GeneratorContext* generator_context); bool GeneratePluginOutput( const std::vector& parsed_files, - const string& plugin_name, const string& parameter, - GeneratorContext* generator_context, string* error); + const std::string& plugin_name, const std::string& parameter, + GeneratorContext* generator_context, std::string* error); // Implements --encode and --decode. bool EncodeOrDecode(const DescriptorPool* pool); @@ -299,8 +308,7 @@ class LIBPROTOC_EXPORT CommandLineInterface { // If include_json_name is true, populate the json_name field of // FieldDescriptorProto for all fields. static void GetTransitiveDependencies( - const FileDescriptor* file, - bool include_json_name, + const FileDescriptor* file, bool include_json_name, bool include_source_code_info, std::set* already_seen, RepeatedPtrField* output); @@ -324,36 +332,36 @@ class LIBPROTOC_EXPORT CommandLineInterface { // ----------------------------------------------------------------- // The name of the executable as invoked (i.e. argv[0]). - string executable_name_; + std::string executable_name_; // Version info set with SetVersionInfo(). - string version_info_; + std::string version_info_; // Registered generators. struct GeneratorInfo { - string flag_name; - string option_flag_name; + std::string flag_name; + std::string option_flag_name; CodeGenerator* generator; - string help_text; + std::string help_text; }; - typedef std::map GeneratorMap; + typedef std::map GeneratorMap; GeneratorMap generators_by_flag_name_; GeneratorMap generators_by_option_name_; // A map from generator names to the parameters specified using the option // flag. For example, if the user invokes the compiler with: // protoc --foo_out=outputdir --foo_opt=enable_bar ... // Then there will be an entry ("--foo_out", "enable_bar") in this map. - std::map generator_parameters_; + std::map generator_parameters_; // Similar to generator_parameters_, but stores the parameters for plugins. - std::map plugin_parameters_; + std::map plugin_parameters_; // See AllowPlugins(). If this is empty, plugins aren't allowed. - string plugin_prefix_; + std::string plugin_prefix_; // Maps specific plugin names to files. When executing a plugin, this map // is searched first to find the plugin executable. If not found here, the // PATH (or other OS-specific search strategy) is searched. - std::map plugins_; + std::map plugins_; // Stuff parsed from command line. enum Mode { @@ -366,57 +374,57 @@ class LIBPROTOC_EXPORT CommandLineInterface { Mode mode_; enum PrintMode { - PRINT_NONE, // Not in MODE_PRINT - PRINT_FREE_FIELDS, // --print_free_fields + PRINT_NONE, // Not in MODE_PRINT + PRINT_FREE_FIELDS, // --print_free_fields }; PrintMode print_mode_; enum ErrorFormat { - ERROR_FORMAT_GCC, // GCC error output format (default). - ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). + ERROR_FORMAT_GCC, // GCC error output format (default). + ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). }; ErrorFormat error_format_; - std::vector > - proto_path_; // Search path for proto files. - std::vector input_files_; // Names of the input proto files. + std::vector > + proto_path_; // Search path for proto files. + std::vector input_files_; // Names of the input proto files. // Names of proto files which are allowed to be imported. Used by build // systems to enforce depend-on-what-you-import. - std::set direct_dependencies_; + std::set direct_dependencies_; bool direct_dependencies_explicitly_set_; // If there's a violation of depend-on-what-you-import, this string will be // presented to the user. "%s" will be replaced with the violating import. - string direct_dependencies_violation_msg_; + std::string direct_dependencies_violation_msg_; // output_directives_ lists all the files we are supposed to output and what // generator to use for each. struct OutputDirective { - string name; // E.g. "--foo_out" - CodeGenerator* generator; // NULL for plugins - string parameter; - string output_location; + std::string name; // E.g. "--foo_out" + CodeGenerator* generator; // NULL for plugins + std::string parameter; + std::string output_location; }; std::vector output_directives_; // When using --encode or --decode, this names the type we are encoding or // decoding. (Empty string indicates --decode_raw.) - string codec_type_; + std::string codec_type_; // If --descriptor_set_in was given, these are filenames containing // parsed FileDescriptorSets to be used for loading protos. Otherwise, empty. - std::vector descriptor_set_in_names_; + std::vector descriptor_set_in_names_; // If --descriptor_set_out was given, this is the filename to which the // FileDescriptorSet should be written. Otherwise, empty. - string descriptor_set_out_name_; + std::string descriptor_set_out_name_; // If --dependency_out was given, this is the path to the file where the // dependency file will be written. Otherwise, empty. - string dependency_out_name_; + std::string dependency_out_name_; // True if --include_imports was given, meaning that we should // write all transitive dependencies to the DescriptorSet. Otherwise, only @@ -437,4 +445,6 @@ class LIBPROTOC_EXPORT CommandLineInterface { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index 4e6f4af558..6ea25652d5 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -62,7 +63,6 @@ #include #include -#include namespace google { namespace protobuf { @@ -71,12 +71,12 @@ namespace compiler { #if defined(_WIN32) // DO NOT include , instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::dup; -using google::protobuf::internal::win32::dup2; -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::open; -using google::protobuf::internal::win32::write; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::dup; +using google::protobuf::io::win32::dup2; +using google::protobuf::io::win32::open; +using google::protobuf::io::win32::write; #endif // Disable the whole test when we use tcmalloc for "draconian" heap checks, in @@ -86,7 +86,7 @@ using google::protobuf::internal::win32::write; namespace { -bool FileExists(const string& path) { +bool FileExists(const std::string& path) { return File::Exists(path); } @@ -98,8 +98,8 @@ class CommandLineInterfaceTest : public testing::Test { // Runs the CommandLineInterface with the given command line. The // command is automatically split on spaces, and the string "$tmpdir" // is replaced with TestTempDir(). - void Run(const string& command); - void RunWithArgs(std::vector args); + void Run(const std::string& command); + void RunWithArgs(std::vector args); // ----------------------------------------------------------------- // Methods to set up the test (called before Run()). @@ -112,17 +112,17 @@ class CommandLineInterfaceTest : public testing::Test { // Create a temp file within temp_directory_ with the given name. // The containing directory is also created if necessary. - void CreateTempFile(const string& name, const string& contents); + void CreateTempFile(const std::string& name, const std::string& contents); // Create a subdirectory within temp_directory_. - void CreateTempDir(const string& name); + void CreateTempDir(const std::string& name); #ifdef PROTOBUF_OPENSOURCE // Change working directory to temp directory. void SwitchToTempDirectory() { File::ChangeWorkingDirectory(temp_directory_); } -#else // !PROTOBUF_OPENSOURCE +#else // !PROTOBUF_OPENSOURCE // TODO(teboring): Figure out how to change and get working directory in // google3. #endif // !PROTOBUF_OPENSOURCE @@ -137,27 +137,25 @@ class CommandLineInterfaceTest : public testing::Test { // Checks that Run() returned non-zero and the stderr output is exactly // the text given. expected_test may contain references to "$tmpdir", // which will be replaced by the temporary directory path. - void ExpectErrorText(const string& expected_text); + void ExpectErrorText(const std::string& expected_text); // Checks that Run() returned non-zero and the stderr contains the given // substring. - void ExpectErrorSubstring(const string& expected_substring); - - // Like ExpectErrorSubstring, but checks that Run() returned zero. - void ExpectErrorSubstringWithZeroReturnCode( - const string& expected_substring); + void ExpectErrorSubstring(const std::string& expected_substring); // Checks that the captured stdout is the same as the expected_text. - void ExpectCapturedStdout(const string& expected_text); + void ExpectCapturedStdout(const std::string& expected_text); // Checks that Run() returned zero and the stdout contains the given // substring. void ExpectCapturedStdoutSubstringWithZeroReturnCode( - const string& expected_substring); + const std::string& expected_substring); +#if defined(_WIN32) && !defined(__CYGWIN__) // Returns true if ExpectErrorSubstring(expected_substring) would pass, but // does not fail otherwise. - bool HasAlternateErrorSubstring(const string& expected_substring); + bool HasAlternateErrorSubstring(const std::string& expected_substring); +#endif // _WIN32 && !__CYGWIN__ // Checks that MockCodeGenerator::Generate() was called in the given // context (or the generator in test_plugin.cc, which produces the same @@ -168,37 +166,40 @@ class CommandLineInterfaceTest : public testing::Test { // generate given these inputs. message_name is the name of the first // message that appeared in the proto file; this is just to make extra // sure that the correct file was parsed. - void ExpectGenerated(const string& generator_name, - const string& parameter, - const string& proto_name, - const string& message_name); - void ExpectGenerated(const string& generator_name, - const string& parameter, - const string& proto_name, - const string& message_name, - const string& output_directory); - void ExpectGeneratedWithMultipleInputs(const string& generator_name, - const string& all_proto_names, - const string& proto_name, - const string& message_name); - void ExpectGeneratedWithInsertions(const string& generator_name, - const string& parameter, - const string& insertions, - const string& proto_name, - const string& message_name); - void CheckGeneratedAnnotations(const string& name, const string& file); - - void ExpectNullCodeGeneratorCalled(const string& parameter); - - - void ReadDescriptorSet(const string& filename, + void ExpectGenerated(const std::string& generator_name, + const std::string& parameter, + const std::string& proto_name, + const std::string& message_name); + void ExpectGenerated(const std::string& generator_name, + const std::string& parameter, + const std::string& proto_name, + const std::string& message_name, + const std::string& output_directory); + void ExpectGeneratedWithMultipleInputs(const std::string& generator_name, + const std::string& all_proto_names, + const std::string& proto_name, + const std::string& message_name); + void ExpectGeneratedWithInsertions(const std::string& generator_name, + const std::string& parameter, + const std::string& insertions, + const std::string& proto_name, + const std::string& message_name); + void CheckGeneratedAnnotations(const std::string& name, + const std::string& file); + +#if defined(_WIN32) + void ExpectNullCodeGeneratorCalled(const std::string& parameter); +#endif // _WIN32 + + + void ReadDescriptorSet(const std::string& filename, FileDescriptorSet* descriptor_set); - void WriteDescriptorSet(const string& filename, + void WriteDescriptorSet(const std::string& filename, const FileDescriptorSet* descriptor_set); - void ExpectFileContent(const string& filename, - const string& content); + void ExpectFileContent(const std::string& filename, + const std::string& content); private: // The object we are testing. @@ -211,16 +212,16 @@ class CommandLineInterfaceTest : public testing::Test { // protection against accidentally deleting user files (since we recursively // delete this directory during the test). This is the full path of that // directory. - string temp_directory_; + std::string temp_directory_; // The result of Run(). int return_code_; // The captured stderr output. - string error_text_; + std::string error_text_; // The captured stdout. - string captured_stdout_; + std::string captured_stdout_; // Pointers which need to be deleted later. std::vector mock_generators_to_delete_; @@ -234,13 +235,11 @@ class CommandLineInterfaceTest::NullCodeGenerator : public CodeGenerator { ~NullCodeGenerator() {} mutable bool called_; - mutable string parameter_; + mutable std::string parameter_; // implements CodeGenerator ---------------------------------------- - bool Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const { + bool Generate(const FileDescriptor* file, const std::string& parameter, + GeneratorContext* context, std::string* error) const { called_ = true; parameter_ = parameter; return true; @@ -292,34 +291,36 @@ void CommandLineInterfaceTest::TearDown() { mock_generators_to_delete_.clear(); } -void CommandLineInterfaceTest::Run(const string& command) { +void CommandLineInterfaceTest::Run(const std::string& command) { RunWithArgs(Split(command, " ", true)); } -void CommandLineInterfaceTest::RunWithArgs(std::vector args) { +void CommandLineInterfaceTest::RunWithArgs(std::vector args) { if (!disallow_plugins_) { cli_.AllowPlugins("prefix-"); -#ifndef GOOGLE_THIRD_PARTY_PROTOBUF - string plugin_path; + std::string plugin_path; #ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH; #else const char* possible_paths[] = { - // When building with shared libraries, libtool hides the real executable - // in .libs and puts a fake wrapper in the current directory. - // Unfortunately, due to an apparent bug on Cygwin/MinGW, if one program - // wrapped in this way (e.g. protobuf-tests.exe) tries to execute another - // program wrapped in this way (e.g. test_plugin.exe), the latter fails - // with error code 127 and no explanation message. Presumably the problem - // is that the wrapper for protobuf-tests.exe set some environment - // variables that confuse the wrapper for test_plugin.exe. Luckily, it - // turns out that if we simply invoke the wrapped test_plugin.exe - // directly, it works -- I guess the environment variables set by the - // protobuf-tests.exe wrapper happen to be correct for it too. So we do - // that. - ".libs/test_plugin.exe", // Win32 w/autotool (Cygwin / MinGW) - "test_plugin.exe", // Other Win32 (MSVC) - "test_plugin", // Unix + // When building with shared libraries, libtool hides the real + // executable + // in .libs and puts a fake wrapper in the current directory. + // Unfortunately, due to an apparent bug on Cygwin/MinGW, if one program + // wrapped in this way (e.g. protobuf-tests.exe) tries to execute + // another + // program wrapped in this way (e.g. test_plugin.exe), the latter fails + // with error code 127 and no explanation message. Presumably the + // problem + // is that the wrapper for protobuf-tests.exe set some environment + // variables that confuse the wrapper for test_plugin.exe. Luckily, it + // turns out that if we simply invoke the wrapped test_plugin.exe + // directly, it works -- I guess the environment variables set by the + // protobuf-tests.exe wrapper happen to be correct for it too. So we do + // that. + ".libs/test_plugin.exe", // Win32 w/autotool (Cygwin / MinGW) + "test_plugin.exe", // Other Win32 (MSVC) + "test_plugin", // Unix }; for (int i = 0; i < GOOGLE_ARRAYSIZE(possible_paths); i++) { if (access(possible_paths[i], F_OK) == 0) { @@ -330,11 +331,6 @@ void CommandLineInterfaceTest::RunWithArgs(std::vector args) { #endif if (plugin_path.empty()) { -#else - string plugin_path = "third_party/protobuf/test_plugin"; - - if (access(plugin_path.c_str(), F_OK) != 0) { -#endif // GOOGLE_THIRD_PARTY_PROTOBUF GOOGLE_LOG(ERROR) << "Plugin executable not found. Plugin tests are likely to fail."; } else { @@ -342,7 +338,7 @@ void CommandLineInterfaceTest::RunWithArgs(std::vector args) { } } - std::unique_ptr argv(new const char* [args.size()]); + std::unique_ptr argv(new const char*[args.size()]); for (int i = 0; i < args.size(); i++) { args[i] = StringReplace(args[i], "$tmpdir", temp_directory_, true); @@ -367,13 +363,12 @@ void CommandLineInterfaceTest::RunWithArgs(std::vector args) { // ------------------------------------------------------------------- -void CommandLineInterfaceTest::CreateTempFile( - const string& name, - const string& contents) { +void CommandLineInterfaceTest::CreateTempFile(const std::string& name, + const std::string& contents) { // Create parent directory, if necessary. - string::size_type slash_pos = name.find_last_of('/'); - if (slash_pos != string::npos) { - string dir = name.substr(0, slash_pos); + std::string::size_type slash_pos = name.find_last_of('/'); + if (slash_pos != std::string::npos) { + std::string dir = name.substr(0, slash_pos); if (!FileExists(temp_directory_ + "/" + dir)) { GOOGLE_CHECK_OK(File::RecursivelyCreateDir(temp_directory_ + "/" + dir, 0777)); @@ -381,13 +376,13 @@ void CommandLineInterfaceTest::CreateTempFile( } // Write file. - string full_name = temp_directory_ + "/" + name; + std::string full_name = temp_directory_ + "/" + name; GOOGLE_CHECK_OK(File::SetContents( full_name, StringReplace(contents, "$tmpdir", temp_directory_, true), true)); } -void CommandLineInterfaceTest::CreateTempDir(const string& name) { +void CommandLineInterfaceTest::CreateTempDir(const std::string& name) { GOOGLE_CHECK_OK(File::RecursivelyCreateDir(temp_directory_ + "/" + name, 0777)); } @@ -399,89 +394,78 @@ void CommandLineInterfaceTest::ExpectNoErrors() { EXPECT_EQ("", error_text_); } -void CommandLineInterfaceTest::ExpectErrorText(const string& expected_text) { +void CommandLineInterfaceTest::ExpectErrorText( + const std::string& expected_text) { EXPECT_NE(0, return_code_); EXPECT_EQ(StringReplace(expected_text, "$tmpdir", temp_directory_, true), error_text_); } void CommandLineInterfaceTest::ExpectErrorSubstring( - const string& expected_substring) { + const std::string& expected_substring) { EXPECT_NE(0, return_code_); EXPECT_PRED_FORMAT2(testing::IsSubstring, expected_substring, error_text_); } -void CommandLineInterfaceTest::ExpectErrorSubstringWithZeroReturnCode( - const string& expected_substring) { - EXPECT_EQ(0, return_code_); - EXPECT_PRED_FORMAT2(testing::IsSubstring, expected_substring, error_text_); -} - +#if defined(_WIN32) && !defined(__CYGWIN__) bool CommandLineInterfaceTest::HasAlternateErrorSubstring( - const string& expected_substring) { + const std::string& expected_substring) { EXPECT_NE(0, return_code_); - return error_text_.find(expected_substring) != string::npos; + return error_text_.find(expected_substring) != std::string::npos; } +#endif // _WIN32 && !__CYGWIN__ void CommandLineInterfaceTest::ExpectGenerated( - const string& generator_name, - const string& parameter, - const string& proto_name, - const string& message_name) { - MockCodeGenerator::ExpectGenerated( - generator_name, parameter, "", proto_name, message_name, proto_name, - temp_directory_); + const std::string& generator_name, const std::string& parameter, + const std::string& proto_name, const std::string& message_name) { + MockCodeGenerator::ExpectGenerated(generator_name, parameter, "", proto_name, + message_name, proto_name, temp_directory_); } void CommandLineInterfaceTest::ExpectGenerated( - const string& generator_name, - const string& parameter, - const string& proto_name, - const string& message_name, - const string& output_directory) { - MockCodeGenerator::ExpectGenerated( - generator_name, parameter, "", proto_name, message_name, proto_name, - temp_directory_ + "/" + output_directory); + const std::string& generator_name, const std::string& parameter, + const std::string& proto_name, const std::string& message_name, + const std::string& output_directory) { + MockCodeGenerator::ExpectGenerated(generator_name, parameter, "", proto_name, + message_name, proto_name, + temp_directory_ + "/" + output_directory); } void CommandLineInterfaceTest::ExpectGeneratedWithMultipleInputs( - const string& generator_name, - const string& all_proto_names, - const string& proto_name, - const string& message_name) { - MockCodeGenerator::ExpectGenerated( - generator_name, "", "", proto_name, message_name, - all_proto_names, - temp_directory_); + const std::string& generator_name, const std::string& all_proto_names, + const std::string& proto_name, const std::string& message_name) { + MockCodeGenerator::ExpectGenerated(generator_name, "", "", proto_name, + message_name, all_proto_names, + temp_directory_); } void CommandLineInterfaceTest::ExpectGeneratedWithInsertions( - const string& generator_name, - const string& parameter, - const string& insertions, - const string& proto_name, - const string& message_name) { - MockCodeGenerator::ExpectGenerated( - generator_name, parameter, insertions, proto_name, message_name, - proto_name, temp_directory_); -} - -void CommandLineInterfaceTest::CheckGeneratedAnnotations(const string& name, - const string& file) { + const std::string& generator_name, const std::string& parameter, + const std::string& insertions, const std::string& proto_name, + const std::string& message_name) { + MockCodeGenerator::ExpectGenerated(generator_name, parameter, insertions, + proto_name, message_name, proto_name, + temp_directory_); +} + +void CommandLineInterfaceTest::CheckGeneratedAnnotations( + const std::string& name, const std::string& file) { MockCodeGenerator::CheckGeneratedAnnotations(name, file, temp_directory_); } +#if defined(_WIN32) void CommandLineInterfaceTest::ExpectNullCodeGeneratorCalled( - const string& parameter) { + const std::string& parameter) { EXPECT_TRUE(null_generator_->called_); EXPECT_EQ(parameter, null_generator_->parameter_); } +#endif // _WIN32 void CommandLineInterfaceTest::ReadDescriptorSet( - const string& filename, FileDescriptorSet* descriptor_set) { - string path = temp_directory_ + "/" + filename; - string file_contents; + const std::string& filename, FileDescriptorSet* descriptor_set) { + std::string path = temp_directory_ + "/" + filename; + std::string file_contents; GOOGLE_CHECK_OK(File::GetContents(path, &file_contents, true)); if (!descriptor_set->ParseFromString(file_contents)) { @@ -490,28 +474,28 @@ void CommandLineInterfaceTest::ReadDescriptorSet( } void CommandLineInterfaceTest::WriteDescriptorSet( - const string& filename, const FileDescriptorSet* descriptor_set) { - string binary_proto; + const std::string& filename, const FileDescriptorSet* descriptor_set) { + std::string binary_proto; GOOGLE_CHECK(descriptor_set->SerializeToString(&binary_proto)); CreateTempFile(filename, binary_proto); } void CommandLineInterfaceTest::ExpectCapturedStdout( - const string& expected_text) { + const std::string& expected_text) { EXPECT_EQ(expected_text, captured_stdout_); } void CommandLineInterfaceTest::ExpectCapturedStdoutSubstringWithZeroReturnCode( - const string& expected_substring) { + const std::string& expected_substring) { EXPECT_EQ(0, return_code_); - EXPECT_PRED_FORMAT2( - testing::IsSubstring, expected_substring, captured_stdout_); + EXPECT_PRED_FORMAT2(testing::IsSubstring, expected_substring, + captured_stdout_); } -void CommandLineInterfaceTest::ExpectFileContent( - const string& filename, const string& content) { - string path = temp_directory_ + "/" + filename; - string file_contents; +void CommandLineInterfaceTest::ExpectFileContent(const std::string& filename, + const std::string& content) { + std::string path = temp_directory_ + "/" + filename; + std::string file_contents; GOOGLE_CHECK_OK(File::GetContents(path, &file_contents, true)); EXPECT_EQ(StringReplace(content, "$tmpdir", temp_directory_, true), @@ -524,8 +508,8 @@ TEST_F(CommandLineInterfaceTest, BasicOutput) { // Test that the common case works. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -554,8 +538,8 @@ TEST_F(CommandLineInterfaceTest, BasicPlugin) { // Test that basic plugins work. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --plug_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -585,8 +569,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorAndPlugin) { // Invoke a generator and a plugin at the same time. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -618,11 +602,11 @@ TEST_F(CommandLineInterfaceTest, MultipleInputs) { // Test parsing multiple input files. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " "--proto_path=$tmpdir foo.proto bar.proto"); @@ -670,17 +654,17 @@ TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport) { // Test parsing multiple input files with an import of a separate file. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"baz.proto\";\n" - "message Bar {\n" - " optional Baz a = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"baz.proto\";\n" + "message Bar {\n" + " optional Baz a = 1;\n" + "}\n"); CreateTempFile("baz.proto", - "syntax = \"proto2\";\n" - "message Baz {}\n"); + "syntax = \"proto2\";\n" + "message Baz {}\n"); Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " "--proto_path=$tmpdir foo.proto bar.proto"); @@ -735,9 +719,8 @@ TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport_DescriptorSetIn) { Run(strings::Substitute( "protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir " "--descriptor_set_in=$0 foo.proto bar.proto", - string("$tmpdir/foo_and_bar.bin") + - CommandLineInterface::kPathSeparator + - "$tmpdir/baz_and_bat.bin")); + std::string("$tmpdir/foo_and_bar.bin") + + CommandLineInterface::kPathSeparator + "$tmpdir/baz_and_bat.bin")); ExpectNoErrors(); ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto", @@ -752,9 +735,8 @@ TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport_DescriptorSetIn) { Run(strings::Substitute( "protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir " "--descriptor_set_in=$0 baz.proto bat.proto", - string("$tmpdir/foo_and_bar.bin") + - CommandLineInterface::kPathSeparator + - "$tmpdir/baz_and_bat.bin")); + std::string("$tmpdir/foo_and_bar.bin") + + CommandLineInterface::kPathSeparator + "$tmpdir/baz_and_bat.bin")); ExpectNoErrors(); ExpectGeneratedWithMultipleInputs("test_generator", "baz.proto,bat.proto", @@ -811,9 +793,8 @@ TEST_F(CommandLineInterfaceTest, Run(strings::Substitute( "protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir " "--descriptor_set_in=$0 bar.proto", - string("$tmpdir/foo_and_bar.bin") + - CommandLineInterface::kPathSeparator + - "$tmpdir/foo_and_baz.bin")); + std::string("$tmpdir/foo_and_bar.bin") + + CommandLineInterface::kPathSeparator + "$tmpdir/foo_and_baz.bin")); ExpectNoErrors(); ExpectGenerated("test_generator", "", "bar.proto", "Bar"); @@ -859,8 +840,8 @@ TEST_F(CommandLineInterfaceTest, CreateDirectory) { // Test that when we output to a sub-directory, it is created. CreateTempFile("bar/baz/foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempDir("out"); CreateTempDir("plugout"); @@ -876,8 +857,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorParameters) { // Test that generator parameters are correctly parsed from the command line. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=TestParameter:$tmpdir " "--plug_out=TestPluginParameter:$tmpdir " @@ -893,8 +874,8 @@ TEST_F(CommandLineInterfaceTest, ExtraGeneratorParameters) { // correctly passed to the code generator. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); // Create the "a" and "b" sub-directories. CreateTempDir("a"); CreateTempDir("b"); @@ -908,10 +889,10 @@ TEST_F(CommandLineInterfaceTest, ExtraGeneratorParameters) { "--proto_path=$tmpdir foo.proto"); ExpectNoErrors(); - ExpectGenerated( - "test_generator", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a"); - ExpectGenerated( - "test_generator", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b"); + ExpectGenerated("test_generator", "bar,foo1,foo2,foo3", "foo.proto", "Foo", + "a"); + ExpectGenerated("test_generator", "baz,foo1,foo2,foo3", "foo.proto", "Foo", + "b"); } TEST_F(CommandLineInterfaceTest, ExtraPluginParameters) { @@ -919,8 +900,8 @@ TEST_F(CommandLineInterfaceTest, ExtraPluginParameters) { // correctly passed to the code generator. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); // Create the "a" and "b" sub-directories. CreateTempDir("a"); CreateTempDir("b"); @@ -934,16 +915,14 @@ TEST_F(CommandLineInterfaceTest, ExtraPluginParameters) { "--proto_path=$tmpdir foo.proto"); ExpectNoErrors(); - ExpectGenerated( - "test_plugin", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a"); - ExpectGenerated( - "test_plugin", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b"); + ExpectGenerated("test_plugin", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a"); + ExpectGenerated("test_plugin", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b"); } TEST_F(CommandLineInterfaceTest, UnrecognizedExtraParameters) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--unknown_plug_a_opt=Foo " @@ -966,8 +945,8 @@ TEST_F(CommandLineInterfaceTest, ExtraPluginParametersForOutParameters) { // directives appear, they both are reported. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--xyz_opt=foo=bar --xyz_out=$tmpdir " @@ -982,8 +961,8 @@ TEST_F(CommandLineInterfaceTest, Insert) { // Test running a generator that inserts code into another's output. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler " "--test_out=TestParameter:$tmpdir " @@ -993,12 +972,12 @@ TEST_F(CommandLineInterfaceTest, Insert) { "--proto_path=$tmpdir foo.proto"); ExpectNoErrors(); - ExpectGeneratedWithInsertions( - "test_generator", "TestParameter", "test_generator,test_plugin", - "foo.proto", "Foo"); - ExpectGeneratedWithInsertions( - "test_plugin", "TestPluginParameter", "test_generator,test_plugin", - "foo.proto", "Foo"); + ExpectGeneratedWithInsertions("test_generator", "TestParameter", + "test_generator,test_plugin", "foo.proto", + "Foo"); + ExpectGeneratedWithInsertions("test_plugin", "TestPluginParameter", + "test_generator,test_plugin", "foo.proto", + "Foo"); } TEST_F(CommandLineInterfaceTest, InsertWithAnnotationFixup) { @@ -1025,8 +1004,7 @@ TEST_F(CommandLineInterfaceTest, InsertWithAnnotationFixup) { TEST_F(CommandLineInterfaceTest, WindowsOutputPath) { // Test that the output path can be a Windows-style path. - CreateTempFile("foo.proto", - "syntax = \"proto2\";\n"); + CreateTempFile("foo.proto", "syntax = \"proto2\";\n"); Run("protocol_compiler --null_out=C:\\ " "--proto_path=$tmpdir foo.proto"); @@ -1038,8 +1016,7 @@ TEST_F(CommandLineInterfaceTest, WindowsOutputPath) { TEST_F(CommandLineInterfaceTest, WindowsOutputPathAndParameter) { // Test that we can have a windows-style output path and a parameter. - CreateTempFile("foo.proto", - "syntax = \"proto2\";\n"); + CreateTempFile("foo.proto", "syntax = \"proto2\";\n"); Run("protocol_compiler --null_out=bar:C:\\ " "--proto_path=$tmpdir foo.proto"); @@ -1053,8 +1030,8 @@ TEST_F(CommandLineInterfaceTest, TrailingBackslash) { // doesn't work on their system. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir\\ " "--proto_path=$tmpdir\\ foo.proto"); @@ -1074,14 +1051,14 @@ TEST_F(CommandLineInterfaceTest, PathLookup) { // Test that specifying multiple directories in the proto search path works. CreateTempFile("b/bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); CreateTempFile("a/foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n" - "message Foo {\n" - " optional Bar a = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n" + "message Foo {\n" + " optional Bar a = 1;\n" + "}\n"); CreateTempFile("b/foo.proto", "this should not be parsed\n"); Run("protocol_compiler --test_out=$tmpdir " @@ -1095,21 +1072,20 @@ TEST_F(CommandLineInterfaceTest, ColonDelimitedPath) { // Same as PathLookup, but we provide the proto_path in a single flag. CreateTempFile("b/bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); CreateTempFile("a/foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n" - "message Foo {\n" - " optional Bar a = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n" + "message Foo {\n" + " optional Bar a = 1;\n" + "}\n"); CreateTempFile("b/foo.proto", "this should not be parsed\n"); Run(strings::Substitute( "protocol_compiler --test_out=$$tmpdir --proto_path=$0 foo.proto", - string("$tmpdir/a") + - CommandLineInterface::kPathSeparator + - "$tmpdir/b")); + std::string("$tmpdir/a") + CommandLineInterface::kPathSeparator + + "$tmpdir/b")); ExpectNoErrors(); ExpectGenerated("test_generator", "", "foo.proto", "Foo"); @@ -1119,8 +1095,8 @@ TEST_F(CommandLineInterfaceTest, NonRootMapping) { // Test setting up a search path mapping a directory to a non-root location. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=bar=$tmpdir bar/foo.proto"); @@ -1134,8 +1110,8 @@ TEST_F(CommandLineInterfaceTest, PathWithEqualsSign) { CreateTempDir("with=sign"); CreateTempFile("with=sign/foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir/with=sign foo.proto"); @@ -1149,8 +1125,8 @@ TEST_F(CommandLineInterfaceTest, MultipleGenerators) { // each with a different output directory. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); // Create the "a" and "b" sub-directories. CreateTempDir("a"); CreateTempDir("b"); @@ -1170,8 +1146,8 @@ TEST_F(CommandLineInterfaceTest, DisallowServicesNoServices) { // services. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --disallow_services --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1184,9 +1160,9 @@ TEST_F(CommandLineInterfaceTest, DisallowServicesHasService) { // Test that --disallow_services produces an error when there are services. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n" - "service Bar {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n" + "service Bar {}\n"); Run("protocol_compiler --disallow_services --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1198,9 +1174,9 @@ TEST_F(CommandLineInterfaceTest, AllowServicesHasService) { // Test that services work fine as long as --disallow_services is not used. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n" - "service Bar {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n" + "service Bar {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1282,8 +1258,7 @@ TEST_F(CommandLineInterfaceTest, DirectDependencies_NoViolation_MultiImports) { } TEST_F(CommandLineInterfaceTest, DirectDependencies_ProvidedMultipleTimes) { - CreateTempFile("foo.proto", - "syntax = \"proto2\";\n"); + CreateTempFile("foo.proto", "syntax = \"proto2\";\n"); Run("protocol_compiler --test_out=$tmpdir --proto_path=$tmpdir " "--direct_dependencies=bar.proto --direct_dependencies=bla.proto " @@ -1304,7 +1279,7 @@ TEST_F(CommandLineInterfaceTest, DirectDependencies_CustomErrorMessage) { "syntax = \"proto2\";\n" "message Bar { optional string text = 1; }"); - std::vector commands; + std::vector commands; commands.push_back("protocol_compiler"); commands.push_back("--test_out=$tmpdir"); commands.push_back("--proto_path=$tmpdir"); @@ -1320,8 +1295,8 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputs) { // Test that we can accept working-directory-relative input files. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir $tmpdir/foo.proto"); @@ -1332,14 +1307,14 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputs) { TEST_F(CommandLineInterfaceTest, WriteDescriptorSet) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--proto_path=$tmpdir bar.proto"); @@ -1361,20 +1336,20 @@ TEST_F(CommandLineInterfaceTest, WriteDescriptorSet) { TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithDuplicates) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); CreateTempFile("baz.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Baz {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Baz {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--proto_path=$tmpdir bar.proto foo.proto bar.proto baz.proto"); @@ -1400,14 +1375,14 @@ TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithDuplicates) { TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithSourceInfo) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--include_source_info --proto_path=$tmpdir bar.proto"); @@ -1425,14 +1400,14 @@ TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithSourceInfo) { TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSet) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--include_imports --proto_path=$tmpdir bar.proto"); @@ -1456,14 +1431,14 @@ TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSet) { TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSetWithSourceInfo) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--include_imports --include_source_info --proto_path=$tmpdir bar.proto"); @@ -1490,14 +1465,14 @@ TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSetWithSourceInfo) { #else TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileGivenTwoInputs) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --dependency_out=$tmpdir/manifest " "--test_out=$tmpdir --proto_path=$tmpdir bar.proto foo.proto"); @@ -1509,16 +1484,16 @@ TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileGivenTwoInputs) { #ifdef PROTOBUF_OPENSOURCE TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFile) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); - string current_working_directory = getcwd(NULL, 0); + std::string current_working_directory = getcwd(NULL, 0); SwitchToTempDirectory(); Run("protocol_compiler --dependency_out=manifest --test_out=. " @@ -1539,14 +1514,14 @@ TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFile) { TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileForAbsolutePath) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --dependency_out=$tmpdir/manifest " "--test_out=$tmpdir --proto_path=$tmpdir bar.proto"); @@ -1563,11 +1538,11 @@ TEST_F(CommandLineInterfaceTest, TestArgumentFile) { // Test parsing multiple input files using an argument file. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); CreateTempFile("arguments.txt", "--test_out=$tmpdir\n" "--plug_out=$tmpdir\n" @@ -1596,14 +1571,14 @@ TEST_F(CommandLineInterfaceTest, ParseErrors) { // Test that parse errors are reported. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); ExpectErrorText( - "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); + "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); } TEST_F(CommandLineInterfaceTest, ParseErrors_DescriptorSetIn) { @@ -1613,8 +1588,7 @@ TEST_F(CommandLineInterfaceTest, ParseErrors_DescriptorSetIn) { Run("protocol_compiler --test_out=$tmpdir " "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); - ExpectErrorText( - "$tmpdir/foo.bin: Unable to parse.\n"); + ExpectErrorText("$tmpdir/foo.bin: Unable to parse.\n"); } TEST_F(CommandLineInterfaceTest, ParseErrorsMultipleFiles) { @@ -1624,37 +1598,38 @@ TEST_F(CommandLineInterfaceTest, ParseErrorsMultipleFiles) { // two ways: Directly and through baz.proto. bar.proto's errors should // only be reported once. CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); CreateTempFile("baz.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n"); CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n" - "import \"baz.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n" + "import \"baz.proto\";\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); ExpectErrorText( - "bar.proto:2:1: Expected top-level statement (e.g. \"message\").\n" - "baz.proto: Import \"bar.proto\" was not found or had errors.\n" - "foo.proto: Import \"bar.proto\" was not found or had errors.\n" - "foo.proto: Import \"baz.proto\" was not found or had errors.\n"); + "bar.proto:2:1: Expected top-level statement (e.g. \"message\").\n" + "baz.proto:2:1: Import \"bar.proto\" was not found or had errors.\n" + "foo.proto:2:1: Import \"bar.proto\" was not found or had errors.\n" + "foo.proto:3:1: Import \"baz.proto\" was not found or had errors.\n"); } TEST_F(CommandLineInterfaceTest, RecursiveImportFails) { // Create a proto file that imports itself. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); ExpectErrorSubstring( - "foo.proto: File recursively imports itself: foo.proto -> foo.proto\n"); + "foo.proto:2:1: File recursively imports itself: " + "foo.proto -> foo.proto\n"); } TEST_F(CommandLineInterfaceTest, InputNotFoundError) { @@ -1672,8 +1647,7 @@ TEST_F(CommandLineInterfaceTest, InputNotFoundError_DescriptorSetIn) { Run("protocol_compiler --test_out=$tmpdir " "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); - ExpectErrorText( - "$tmpdir/foo.bin: No such file or directory\n"); + ExpectErrorText("$tmpdir/foo.bin: No such file or directory\n"); } TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotFoundError) { @@ -1683,8 +1657,7 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotFoundError) { Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir $tmpdir/foo.proto"); - ExpectErrorText( - "$tmpdir/foo.proto: No such file or directory\n"); + ExpectErrorText("$tmpdir/foo.proto: No such file or directory\n"); } TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotMappedError) { @@ -1692,8 +1665,8 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotMappedError) { // mapped to a virtual path. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); // Create a directory called "bar" so that we can point --proto_path at it. CreateTempFile("bar/dummy", ""); @@ -1702,7 +1675,7 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotMappedError) { "--proto_path=$tmpdir/bar $tmpdir/foo.proto"); ExpectErrorText( - "$tmpdir/foo.proto: File does not reside within any path " + "$tmpdir/foo.proto: File does not reside within any path " "specified using --proto_path (or -I). You must specify a " "--proto_path which encompasses this file. Note that the " "proto_path must be an exact prefix of the .proto file " @@ -1721,8 +1694,7 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotFoundAndNotMappedError) { Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir/bar $tmpdir/foo.proto"); - ExpectErrorText( - "$tmpdir/foo.proto: No such file or directory\n"); + ExpectErrorText("$tmpdir/foo.proto: No such file or directory\n"); } TEST_F(CommandLineInterfaceTest, CwdRelativeInputShadowedError) { @@ -1730,21 +1702,21 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputShadowedError) { // by another file in the virtual path. CreateTempFile("foo/foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar/foo.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir/foo --proto_path=$tmpdir/bar " "$tmpdir/bar/foo.proto"); ExpectErrorText( - "$tmpdir/bar/foo.proto: Input is shadowed in the --proto_path " - "by \"$tmpdir/foo/foo.proto\". Either use the latter " - "file as your input or reorder the --proto_path so that the " - "former file's location comes first.\n"); + "$tmpdir/bar/foo.proto: Input is shadowed in the --proto_path " + "by \"$tmpdir/foo/foo.proto\". Either use the latter " + "file as your input or reorder the --proto_path so that the " + "former file's location comes first.\n"); } TEST_F(CommandLineInterfaceTest, ProtoPathNotFoundError) { @@ -1761,13 +1733,49 @@ TEST_F(CommandLineInterfaceTest, ProtoPathNotFoundError) { TEST_F(CommandLineInterfaceTest, ProtoPathAndDescriptorSetIn) { Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir --descriptor_set_in=$tmpdir/foo.bin foo.proto"); - ExpectErrorText( - "Only one of --descriptor_set_in and --proto_path can be specified.\n"); + ExpectErrorText("$tmpdir/foo.bin: No such file or directory\n"); Run("protocol_compiler --test_out=$tmpdir " "--descriptor_set_in=$tmpdir/foo.bin --proto_path=$tmpdir foo.proto"); - ExpectErrorText( - "Only one of --proto_path and --descriptor_set_in can be specified.\n"); + ExpectErrorText("$tmpdir/foo.bin: No such file or directory\n"); +} + +TEST_F(CommandLineInterfaceTest, ProtoPathAndDescriptorSetIn_CompileFiles) { + // Test what happens if a proto is in a --descriptor_set_in and also exists + // on disk. + FileDescriptorSet file_descriptor_set; + + // NOTE: This file desc SHOULD be different from the one created as a temp + // to make it easier to test that the file was output instead of the + // contents of the --descriptor_set_in file. + FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("foo.proto"); + file_descriptor_proto->add_message_type()->set_name("Foo"); + + WriteDescriptorSet("foo.bin", &file_descriptor_set); + + CreateTempFile("foo.proto", + "syntax = \"proto2\";\n" + "message FooBar { required string foo_message = 1; }\n"); + + Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " + "--descriptor_set_in=$tmpdir/foo.bin " + "--include_source_info " + "--proto_path=$tmpdir foo.proto"); + + ExpectNoErrors(); + + FileDescriptorSet descriptor_set; + ReadDescriptorSet("descriptor_set", &descriptor_set); + + EXPECT_EQ(1, descriptor_set.file_size()); + EXPECT_EQ("foo.proto", descriptor_set.file(0).name()); + // Descriptor set SHOULD have source code info. + EXPECT_TRUE(descriptor_set.file(0).has_source_code_info()); + + EXPECT_EQ("FooBar", descriptor_set.file(0).message_type(0).name()); + EXPECT_EQ("foo_message", + descriptor_set.file(0).message_type(0).field(0).name()); } TEST_F(CommandLineInterfaceTest, ProtoPathAndDependencyOut) { @@ -1795,8 +1803,8 @@ TEST_F(CommandLineInterfaceTest, MissingInputError) { TEST_F(CommandLineInterfaceTest, MissingOutputError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --proto_path=$tmpdir foo.proto"); @@ -1805,10 +1813,10 @@ TEST_F(CommandLineInterfaceTest, MissingOutputError) { TEST_F(CommandLineInterfaceTest, OutputWriteError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); - string output_file = + std::string output_file = MockCodeGenerator::GetOutputFileName("test_generator", "foo.proto"); // Create a directory blocking our output location. @@ -1834,10 +1842,10 @@ TEST_F(CommandLineInterfaceTest, OutputWriteError) { TEST_F(CommandLineInterfaceTest, PluginOutputWriteError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); - string output_file = + std::string output_file = MockCodeGenerator::GetOutputFileName("test_plugin", "foo.proto"); // Create a directory blocking our output location. @@ -1858,8 +1866,8 @@ TEST_F(CommandLineInterfaceTest, PluginOutputWriteError) { TEST_F(CommandLineInterfaceTest, OutputDirectoryNotFoundError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir/nosuchdir " "--proto_path=$tmpdir foo.proto"); @@ -1869,8 +1877,8 @@ TEST_F(CommandLineInterfaceTest, OutputDirectoryNotFoundError) { TEST_F(CommandLineInterfaceTest, PluginOutputDirectoryNotFoundError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --plug_out=$tmpdir/nosuchdir " "--proto_path=$tmpdir foo.proto"); @@ -1880,8 +1888,8 @@ TEST_F(CommandLineInterfaceTest, PluginOutputDirectoryNotFoundError) { TEST_F(CommandLineInterfaceTest, OutputDirectoryIsFileError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir/foo.proto " "--proto_path=$tmpdir foo.proto"); @@ -1898,8 +1906,8 @@ TEST_F(CommandLineInterfaceTest, OutputDirectoryIsFileError) { TEST_F(CommandLineInterfaceTest, GeneratorError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_Error {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Error {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1912,8 +1920,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginError) { // Test a generator plugin that returns an error. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_Error {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Error {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1926,8 +1934,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginFail) { // Test a generator plugin that exits with an error code. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_Exit {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Exit {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1941,8 +1949,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginCrash) { // Test a generator plugin that crashes. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_Abort {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Abort {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1956,15 +1964,14 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginCrash) { "--plug_out: prefix-gen-plug: Plugin failed with status code"); #else // Don't depend on the exact signal number. - ExpectErrorSubstring( - "--plug_out: prefix-gen-plug: Plugin killed by signal"); + ExpectErrorSubstring("--plug_out: prefix-gen-plug: Plugin killed by signal"); #endif } TEST_F(CommandLineInterfaceTest, PluginReceivesSourceCodeInfo) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_HasSourceCodeInfo {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_HasSourceCodeInfo {}\n"); Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto"); @@ -1974,10 +1981,10 @@ TEST_F(CommandLineInterfaceTest, PluginReceivesSourceCodeInfo) { TEST_F(CommandLineInterfaceTest, PluginReceivesJsonName) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_HasJsonName {\n" - " optional int32 value = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_HasJsonName {\n" + " optional int32 value = 1;\n" + "}\n"); Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto"); @@ -1986,25 +1993,24 @@ TEST_F(CommandLineInterfaceTest, PluginReceivesJsonName) { TEST_F(CommandLineInterfaceTest, PluginReceivesCompilerVersion) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_ShowVersionNumber {\n" - " optional int32 value = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_ShowVersionNumber {\n" + " optional int32 value = 1;\n" + "}\n"); Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto"); - ExpectErrorSubstring( - StringPrintf("Saw compiler_version: %d %s", - GOOGLE_PROTOBUF_VERSION, - GOOGLE_PROTOBUF_VERSION_SUFFIX)); + ExpectErrorSubstring(StringPrintf("Saw compiler_version: %d %s", + GOOGLE_PROTOBUF_VERSION, + GOOGLE_PROTOBUF_VERSION_SUFFIX)); } TEST_F(CommandLineInterfaceTest, GeneratorPluginNotFound) { // Test what happens if the plugin isn't found. CreateTempFile("error.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --badplug_out=TestParameter:$tmpdir " "--plugin=prefix-gen-badplug=no_such_file " @@ -2012,11 +2018,14 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginNotFound) { #ifdef _WIN32 ExpectErrorSubstring("--badplug_out: prefix-gen-badplug: " + - Subprocess::Win32ErrorMessage(ERROR_FILE_NOT_FOUND)); + Subprocess::Win32ErrorMessage(ERROR_FILE_NOT_FOUND)); #else // Error written to stdout by child process after exec() fails. + ExpectErrorSubstring("no_such_file: program not found or is not executable"); + ExpectErrorSubstring( - "no_such_file: program not found or is not executable"); + "Please specify a program using absolute path or make sure " + "the program is available in your PATH system variable"); // Error written by parent process when child fails. ExpectErrorSubstring( @@ -2028,8 +2037,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginNotAllowed) { // Test what happens if plugins aren't allowed. CreateTempFile("error.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); DisallowPlugins(); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " @@ -2053,28 +2062,28 @@ TEST_F(CommandLineInterfaceTest, GccFormatErrors) { // that it can be set explicitly). CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir --error_format=gcc foo.proto"); ExpectErrorText( - "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); + "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); } TEST_F(CommandLineInterfaceTest, MsvsFormatErrors) { // Test --error_format=msvs CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir --error_format=msvs foo.proto"); ExpectErrorText( - "$tmpdir/foo.proto(2) : error in column=1: Expected top-level statement " + "$tmpdir/foo.proto(2) : error in column=1: Expected top-level statement " "(e.g. \"message\").\n"); } @@ -2082,14 +2091,13 @@ TEST_F(CommandLineInterfaceTest, InvalidErrorFormat) { // Test --error_format=msvs CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir --error_format=invalid foo.proto"); - ExpectErrorText( - "Unknown error format: invalid\n"); + ExpectErrorText("Unknown error format: invalid\n"); } // ------------------------------------------------------------------- @@ -2099,8 +2107,8 @@ TEST_F(CommandLineInterfaceTest, ParseSingleCharacterFlag) { // Test that a single-character flag works. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler -t$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -2113,8 +2121,8 @@ TEST_F(CommandLineInterfaceTest, ParseSpaceDelimitedValue) { // Test that separating the flag value with a space works. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out $tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -2128,8 +2136,8 @@ TEST_F(CommandLineInterfaceTest, ParseSingleCharacterSpaceDelimitedValue) { // single-character flags. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler -t $tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -2156,38 +2164,35 @@ TEST_F(CommandLineInterfaceTest, MissingValueAtEndError) { } TEST_F(CommandLineInterfaceTest, PrintFreeFieldNumbers) { - CreateTempFile( - "foo.proto", - "syntax = \"proto2\";\n" - "package foo;\n" - "message Foo {\n" - " optional int32 a = 2;\n" - " optional string b = 4;\n" - " optional string c = 5;\n" - " optional int64 d = 8;\n" - " optional double e = 10;\n" - "}\n"); - CreateTempFile( - "bar.proto", - "syntax = \"proto2\";\n" - "message Bar {\n" - " optional int32 a = 2;\n" - " extensions 4 to 5;\n" - " optional int64 d = 8;\n" - " extensions 10;\n" - "}\n"); - CreateTempFile( - "baz.proto", - "syntax = \"proto2\";\n" - "message Baz {\n" - " optional int32 a = 2;\n" - " optional int64 d = 8;\n" - " extensions 15 to max;\n" // unordered. - " extensions 13;\n" - " extensions 10 to 12;\n" - " extensions 5;\n" - " extensions 4;\n" - "}\n"); + CreateTempFile("foo.proto", + "syntax = \"proto2\";\n" + "package foo;\n" + "message Foo {\n" + " optional int32 a = 2;\n" + " optional string b = 4;\n" + " optional string c = 5;\n" + " optional int64 d = 8;\n" + " optional double e = 10;\n" + "}\n"); + CreateTempFile("bar.proto", + "syntax = \"proto2\";\n" + "message Bar {\n" + " optional int32 a = 2;\n" + " extensions 4 to 5;\n" + " optional int64 d = 8;\n" + " extensions 10;\n" + "}\n"); + CreateTempFile("baz.proto", + "syntax = \"proto2\";\n" + "message Baz {\n" + " optional int32 a = 2;\n" + " optional int64 d = 8;\n" + " extensions 15 to max;\n" // unordered. + " extensions 13;\n" + " extensions 10 to 12;\n" + " extensions 5;\n" + " extensions 4;\n" + "}\n"); CreateTempFile( "quz.proto", "syntax = \"proto2\";\n" @@ -2232,10 +2237,7 @@ TEST_F(CommandLineInterfaceTest, PrintFreeFieldNumbers) { // platforms that don't have a Bourne-compatible shell available (especially // Windows/MSVC). -enum EncodeDecodeTestMode { - PROTO_PATH, - DESCRIPTOR_SET_IN -}; +enum EncodeDecodeTestMode { PROTO_PATH, DESCRIPTOR_SET_IN }; class EncodeDecodeTest : public testing::TestWithParam { protected: @@ -2249,13 +2251,13 @@ class EncodeDecodeTest : public testing::TestWithParam { close(duped_stdin_); } - void RedirectStdinFromText(const string& input) { - string filename = TestTempDir() + "/test_stdin"; + void RedirectStdinFromText(const std::string& input) { + std::string filename = TestTempDir() + "/test_stdin"; GOOGLE_CHECK_OK(File::SetContents(filename, input, true)); GOOGLE_CHECK(RedirectStdinFromFile(filename)); } - bool RedirectStdinFromFile(const string& filename) { + bool RedirectStdinFromFile(const std::string& filename) { int fd = open(filename.c_str(), O_RDONLY); if (fd < 0) return false; dup2(fd, STDIN_FILENO); @@ -2264,8 +2266,8 @@ class EncodeDecodeTest : public testing::TestWithParam { } // Remove '\r' characters from text. - string StripCR(const string& text) { - string result; + std::string StripCR(const std::string& text) { + std::string result; for (int i = 0; i < text.size(); i++) { if (text[i] != '\r') { @@ -2279,8 +2281,8 @@ class EncodeDecodeTest : public testing::TestWithParam { enum Type { TEXT, BINARY }; enum ReturnCode { SUCCESS, ERROR }; - bool Run(const string& command) { - std::vector args; + bool Run(const std::string& command) { + std::vector args; args.push_back("protoc"); SplitStringUsing(command, " ", &args); switch (GetParam()) { @@ -2295,7 +2297,7 @@ class EncodeDecodeTest : public testing::TestWithParam { ADD_FAILURE() << "unexpected EncodeDecodeTestMode: " << GetParam(); } - std::unique_ptr argv(new const char* [args.size()]); + std::unique_ptr argv(new const char*[args.size()]); for (int i = 0; i < args.size(); i++) { argv[i] = args[i].c_str(); } @@ -2313,8 +2315,8 @@ class EncodeDecodeTest : public testing::TestWithParam { return result == 0; } - void ExpectStdoutMatchesBinaryFile(const string& filename) { - string expected_output; + void ExpectStdoutMatchesBinaryFile(const std::string& filename) { + std::string expected_output; GOOGLE_CHECK_OK(File::GetContents(filename, &expected_output, true)); // Don't use EXPECT_EQ because we don't want to print raw binary data to @@ -2322,18 +2324,18 @@ class EncodeDecodeTest : public testing::TestWithParam { EXPECT_TRUE(captured_stdout_ == expected_output); } - void ExpectStdoutMatchesTextFile(const string& filename) { - string expected_output; + void ExpectStdoutMatchesTextFile(const std::string& filename) { + std::string expected_output; GOOGLE_CHECK_OK(File::GetContents(filename, &expected_output, true)); ExpectStdoutMatchesText(expected_output); } - void ExpectStdoutMatchesText(const string& expected_text) { + void ExpectStdoutMatchesText(const std::string& expected_text) { EXPECT_EQ(StripCR(expected_text), StripCR(captured_stdout_)); } - void ExpectStderrMatchesText(const string& expected_text) { + void ExpectStderrMatchesText(const std::string& expected_text) { EXPECT_EQ(StripCR(expected_text), StripCR(captured_stderr_)); } @@ -2348,24 +2350,21 @@ class EncodeDecodeTest : public testing::TestWithParam { protobuf_unittest_import::ImportMessage import_message; import_message.descriptor()->file()->CopyTo(file_descriptor_set.add_file()); - protobuf_unittest_import::PublicImportMessage public_import_message; public_import_message.descriptor()->file()->CopyTo( file_descriptor_set.add_file()); GOOGLE_DCHECK(file_descriptor_set.IsInitialized()); - string binary_proto; + std::string binary_proto; GOOGLE_CHECK(file_descriptor_set.SerializeToString(&binary_proto)); - GOOGLE_CHECK_OK(File::SetContents( - unittest_proto_descriptor_set_filename_, - binary_proto, - true)); + GOOGLE_CHECK_OK(File::SetContents(unittest_proto_descriptor_set_filename_, + binary_proto, true)); } int duped_stdin_; - string captured_stdout_; - string captured_stderr_; - string unittest_proto_descriptor_set_filename_; + std::string captured_stdout_; + std::string captured_stderr_; + std::string unittest_proto_descriptor_set_filename_; }; TEST_P(EncodeDecodeTest, Encode) { @@ -2399,20 +2398,21 @@ TEST_P(EncodeDecodeTest, Partial) { " --encode=protobuf_unittest.TestRequired")); ExpectStdoutMatchesText(""); ExpectStderrMatchesText( - "warning: Input message is missing required fields: a, b, c\n"); + "warning: Input message is missing required fields: a, b, c\n"); } TEST_P(EncodeDecodeTest, DecodeRaw) { protobuf_unittest::TestAllTypes message; message.set_optional_int32(123); message.set_optional_string("foo"); - string data; + std::string data; message.SerializeToString(&data); RedirectStdinFromText(data); EXPECT_TRUE(Run("--decode_raw")); - ExpectStdoutMatchesText("1: 123\n" - "14: \"foo\"\n"); + ExpectStdoutMatchesText( + "1: 123\n" + "14: \"foo\"\n"); ExpectStderrMatchesText(""); } @@ -2433,9 +2433,8 @@ TEST_P(EncodeDecodeTest, ProtoParseError) { "net/proto2/internal/no_such_file.proto: No such file or directory\n"); } -INSTANTIATE_TEST_CASE_P(FileDescriptorSetSource, - EncodeDecodeTest, - testing::Values(PROTO_PATH, DESCRIPTOR_SET_IN)); +INSTANTIATE_TEST_SUITE_P(FileDescriptorSetSource, EncodeDecodeTest, + testing::Values(PROTO_PATH, DESCRIPTOR_SET_IN)); } // anonymous namespace #endif // !GOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN diff --git a/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc index 4c135649f9..ef5be0365c 100644 --- a/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -71,34 +72,40 @@ class MockErrorCollector : public MultiFileErrorCollector { MockErrorCollector() {} ~MockErrorCollector() {} - string text_; + std::string text_; // implements ErrorCollector --------------------------------------- - void AddError(const string& filename, int line, int column, - const string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", - filename, line, column, message); + void AddError(const std::string& filename, int line, int column, + const std::string& message) { + strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line, + column, message); } }; class MockGeneratorContext : public GeneratorContext { public: - MockGeneratorContext() {} - ~MockGeneratorContext() { STLDeleteValues(&files_); } - - void ExpectFileMatches(const string& virtual_filename, - const string& physical_filename) { - string* expected_contents = FindPtrOrNull(files_, virtual_filename); - ASSERT_TRUE(expected_contents != NULL) - << "Generator failed to generate file: " << virtual_filename; - - string actual_contents; + void ExpectFileMatches(const std::string& virtual_filename, + const std::string& physical_filename) { + auto it = files_.find(virtual_filename); + ASSERT_TRUE(it != files_.end()) + << "Generator failed to generate file: " << virtual_filename; + + std::string expected_contents = *it->second; + std::string actual_contents; GOOGLE_CHECK_OK( File::GetContents(TestUtil::TestSourceDir() + "/" + physical_filename, &actual_contents, true)) << physical_filename; CleanStringLineEndings(&actual_contents, false); - EXPECT_EQ(*expected_contents, actual_contents) + +#ifdef WRITE_FILES // Define to debug mismatched files. + GOOGLE_CHECK_OK(File::SetContents("/tmp/expected.cc", expected_contents, + true)); + GOOGLE_CHECK_OK( + File::SetContents("/tmp/actual.cc", actual_contents, true)); +#endif + + ASSERT_EQ(expected_contents, actual_contents) << physical_filename << " needs to be regenerated. Please run " "generate_descriptor_proto.sh. " @@ -107,21 +114,19 @@ class MockGeneratorContext : public GeneratorContext { // implements GeneratorContext -------------------------------------- - virtual io::ZeroCopyOutputStream* Open(const string& filename) { - string** map_slot = &files_[filename]; - delete *map_slot; - *map_slot = new string; - - return new io::StringOutputStream(*map_slot); + virtual io::ZeroCopyOutputStream* Open(const std::string& filename) { + auto& map_slot = files_[filename]; + map_slot.reset(new std::string); + return new io::StringOutputStream(map_slot.get()); } private: - std::map files_; + std::map> files_; }; -const char kDescriptorParameter[] = "dllexport_decl=LIBPROTOBUF_EXPORT"; -const char kPluginParameter[] = "dllexport_decl=LIBPROTOC_EXPORT"; -const char kNormalParameter[] = ""; +const char kDescriptorParameter[] = "dllexport_decl=PROTOBUF_EXPORT"; +const char kPluginParameter[] = "dllexport_decl=PROTOC_EXPORT"; + const char* test_protos[][2] = { {"google/protobuf/descriptor", kDescriptorParameter}, @@ -131,12 +136,16 @@ const char* test_protos[][2] = { TEST(BootstrapTest, GeneratedFilesMatch) { // We need a mapping from the actual file to virtual and actual path // of the data to compare to. - std::map vpath_map; - std::map rpath_map; - rpath_map["third_party/protobuf/src/google/protobuf/test_messages_proto2"] = - "net/proto2/z_generated_example/test_messages_proto2"; - rpath_map["third_party/protobuf/src/google/protobuf/test_messages_proto3"] = - "net/proto2/z_generated_example/test_messages_proto3"; + std::map vpath_map; + std::map rpath_map; + rpath_map + ["third_party/protobuf_legacy_opensource/src/google/protobuf/" + "test_messages_proto2"] = + "net/proto2/z_generated_example/test_messages_proto2"; + rpath_map + ["third_party/protobuf_legacy_opensource/src/google/protobuf/" + "test_messages_proto3"] = + "net/proto2/z_generated_example/test_messages_proto3"; rpath_map["net/proto2/internal/proto2_weak"] = "net/proto2/z_generated_example/proto2_weak"; @@ -147,18 +156,22 @@ TEST(BootstrapTest, GeneratedFilesMatch) { MockErrorCollector error_collector; Importer importer(&source_tree, &error_collector); const FileDescriptor* file = - importer.Import(file_parameter[0] + string(".proto")); + importer.Import(file_parameter[0] + std::string(".proto")); ASSERT_TRUE(file != nullptr) << "Can't import file " << file_parameter[0] + string(".proto") << "\n"; EXPECT_EQ("", error_collector.text_); CppGenerator generator; MockGeneratorContext context; - string error; +#ifdef GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE + generator.set_opensource_runtime(true); + generator.set_runtime_include_base(GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE); +#endif + std::string error; ASSERT_TRUE(generator.Generate(file, file_parameter[1], &context, &error)); - string vpath = + std::string vpath = FindWithDefault(vpath_map, file_parameter[0], file_parameter[0]); - string rpath = + std::string rpath = FindWithDefault(rpath_map, file_parameter[0], file_parameter[0]); context.ExpectFileMatches(vpath + ".pb.cc", rpath + ".pb.cc"); context.ExpectFileMatches(vpath + ".pb.h", rpath + ".pb.h"); diff --git a/src/google/protobuf/compiler/cpp/cpp_enum.cc b/src/google/protobuf/compiler/cpp/cpp_enum.cc index 39fe9e685a..366fcb66ec 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum.cc @@ -57,10 +57,21 @@ bool ShouldGenerateArraySize(const EnumDescriptor* descriptor) { } return max_value != kint32max; } + +// Returns the number of unique numeric enum values. This is less than +// descriptor->value_count() when there are aliased values. +int CountUniqueValues(const EnumDescriptor* descriptor) { + std::set values; + for (int i = 0; i < descriptor->value_count(); ++i) { + values.insert(descriptor->value(i)->number()); + } + return values.size(); +} + } // namespace EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor, - const std::map& vars, + const std::map& vars, const Options& options) : descriptor_(descriptor), classname_(ClassName(descriptor, false)), @@ -68,11 +79,10 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor, generate_array_size_(ShouldGenerateArraySize(descriptor)), variables_(vars) { variables_["classname"] = classname_; - variables_["classtype"] = QualifiedClassName(descriptor_); + variables_["classtype"] = QualifiedClassName(descriptor_, options); variables_["short_name"] = descriptor_->name(); - variables_["enumbase"] = options_.proto_h ? " : int" : ""; variables_["nested_name"] = descriptor_->name(); - variables_["constexpr"] = options_.proto_h ? "constexpr" : ""; + variables_["resolved_name"] = ResolveKeyword(descriptor_->name()); variables_["prefix"] = (descriptor_->containing_type() == NULL) ? "" : classname_ + "_"; } @@ -81,7 +91,7 @@ EnumGenerator::~EnumGenerator() {} void EnumGenerator::GenerateDefinition(io::Printer* printer) { Formatter format(printer, variables_); - format("enum ${1$$classname$$}$$enumbase$ {\n", descriptor_); + format("enum ${1$$classname$$}$ : int {\n", descriptor_); format.Indent(); const EnumValueDescriptor* min_value = descriptor_->value(0); @@ -111,7 +121,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { } } - if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { + if (descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) { // For new enum semantics: generate min and max sentinel values equal to // INT32_MIN and INT32_MAX if (descriptor_->value_count() > 0) format(",\n"); @@ -127,15 +137,15 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { format( "$dllexport_decl $bool $classname$_IsValid(int value);\n" - "const $classname$ ${1$$prefix$$short_name$_MIN$}$ = " + "constexpr $classname$ ${1$$prefix$$short_name$_MIN$}$ = " "$prefix$$2$;\n" - "const $classname$ ${1$$prefix$$short_name$_MAX$}$ = " + "constexpr $classname$ ${1$$prefix$$short_name$_MAX$}$ = " "$prefix$$3$;\n", descriptor_, EnumValueName(min_value), EnumValueName(max_value)); if (generate_array_size_) { format( - "const int ${1$$prefix$$short_name$_ARRAYSIZE$}$ = " + "constexpr int ${1$$prefix$$short_name$_ARRAYSIZE$}$ = " "$prefix$$short_name$_MAX + 1;\n\n", descriptor_); } @@ -144,35 +154,42 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { format( "$dllexport_decl $const ::$proto_ns$::EnumDescriptor* " "$classname$_descriptor();\n"); - // The _Name and _Parse methods - if (options_.opensource_runtime) { - // TODO(haberman): consider removing this in favor of the stricter - // version below. Would this break our compatibility guarantees? - format( - "inline const $string$& $classname$_Name($classname$ value) {\n" - " return ::$proto_ns$::internal::NameOfEnum(\n" - " $classname$_descriptor(), value);\n" - "}\n"); - } else { - // Support a stricter, type-checked enum-to-string method that - // statically checks whether the parameter is the exact enum type or is - // an integral type. - format( - "template\n" - "inline const $string$& $classname$_Name(T enum_t_value) {\n" - " static_assert(::std::is_same::value ||\n" - " ::std::is_integral::value,\n" - " \"Incorrect type passed to function $classname$_Name.\");\n" - " return ::$proto_ns$::internal::NameOfEnum(\n" - " $classname$_descriptor(), enum_t_value);\n" - "}\n"); - } + } + + // The _Name and _Parse functions. The lite implementation is table-based, so + // we make sure to keep the tables hidden in the .cc file. + if (!HasDescriptorMethods(descriptor_->file(), options_)) { + format("const std::string& $classname$_Name($classname$ value);\n"); + } + // The _Name() function accepts the enum type itself but also any integral + // type. + format( + "template\n" + "inline const std::string& $classname$_Name(T enum_t_value) {\n" + " static_assert(::std::is_same::value ||\n" + " ::std::is_integral::value,\n" + " \"Incorrect type passed to function $classname$_Name.\");\n"); + if (HasDescriptorMethods(descriptor_->file(), options_)) { + format( + " return ::$proto_ns$::internal::NameOfEnum(\n" + " $classname$_descriptor(), enum_t_value);\n"); + } else { + format( + " return $classname$_Name(static_cast<$classname$>(enum_t_value));\n"); + } + format("}\n"); + + if (HasDescriptorMethods(descriptor_->file(), options_)) { format( "inline bool $classname$_Parse(\n" - " const $string$& name, $classname$* value) {\n" + " const std::string& name, $classname$* value) {\n" " return ::$proto_ns$::internal::ParseNamedEnum<$classname$>(\n" " $classname$_descriptor(), name, value);\n" "}\n"); + } else { + format( + "bool $classname$_Parse(\n" + " const std::string& name, $classname$* value);\n"); } } @@ -193,13 +210,13 @@ void EnumGenerator::GenerateGetEnumDescriptorSpecializations( void EnumGenerator::GenerateSymbolImports(io::Printer* printer) const { Formatter format(printer, variables_); - format("typedef $classname$ $nested_name$;\n"); + format("typedef $classname$ $resolved_name$;\n"); for (int j = 0; j < descriptor_->value_count(); j++) { - string deprecated_attr = DeprecatedAttribute( + std::string deprecated_attr = DeprecatedAttribute( options_, descriptor_->value(j)->options().deprecated()); format( - "$1$static $constexpr $const $nested_name$ ${2$$3$$}$ =\n" + "$1$static constexpr $resolved_name$ ${2$$3$$}$ =\n" " $classname$_$3$;\n", deprecated_attr, descriptor_->value(j), EnumValueName(descriptor_->value(j))); @@ -209,14 +226,14 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) const { "static inline bool $nested_name$_IsValid(int value) {\n" " return $classname$_IsValid(value);\n" "}\n" - "static const $nested_name$ ${1$$nested_name$_MIN$}$ =\n" + "static constexpr $resolved_name$ ${1$$nested_name$_MIN$}$ =\n" " $classname$_$nested_name$_MIN;\n" - "static const $nested_name$ ${1$$nested_name$_MAX$}$ =\n" + "static constexpr $resolved_name$ ${1$$nested_name$_MAX$}$ =\n" " $classname$_$nested_name$_MAX;\n", descriptor_); if (generate_array_size_) { format( - "static const int ${1$$nested_name$_ARRAYSIZE$}$ =\n" + "static constexpr int ${1$$nested_name$_ARRAYSIZE$}$ =\n" " $classname$_$nested_name$_ARRAYSIZE;\n", descriptor_); } @@ -227,34 +244,21 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) const { "$nested_name$_descriptor() {\n" " return $classname$_descriptor();\n" "}\n"); - if (options_.opensource_runtime) { - // TODO(haberman): consider removing this in favor of the stricter - // version below. Would this break our compatibility guarantees? - format( - "static inline const $string$& " - "$nested_name$_Name($nested_name$ value) {" - "\n" - " return $classname$_Name(value);\n" - "}\n"); - } else { - // Support a stricter, type-checked enum-to-string method that - // statically checks whether the parameter is the exact enum type or is - // an integral type. - format( - "template\n" - "static inline const $string$& $nested_name$_Name(T enum_t_value) {\n" - " static_assert(::std::is_same::value ||\n" - " ::std::is_integral::value,\n" - " \"Incorrect type passed to function $nested_name$_Name.\");\n" - " return $classname$_Name(enum_t_value);\n" - "}\n"); - } - format( - "static inline bool $nested_name$_Parse(const $string$& name,\n" - " $nested_name$* value) {\n" - " return $classname$_Parse(name, value);\n" - "}\n"); } + + format( + "template\n" + "static inline const std::string& $nested_name$_Name(T enum_t_value) {\n" + " static_assert(::std::is_same::value ||\n" + " ::std::is_integral::value,\n" + " \"Incorrect type passed to function $nested_name$_Name.\");\n" + " return $classname$_Name(enum_t_value);\n" + "}\n"); + format( + "static inline bool $nested_name$_Parse(const std::string& name,\n" + " $resolved_name$* value) {\n" + " return $classname$_Parse(name, value);\n" + "}\n"); } void EnumGenerator::GenerateMethods(int idx, io::Printer* printer) { @@ -262,7 +266,7 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* printer) { if (HasDescriptorMethods(descriptor_->file(), options_)) { format( "const ::$proto_ns$::EnumDescriptor* $classname$_descriptor() {\n" - " ::$proto_ns$::internal::AssignDescriptors(&$assign_desc_table$);\n" + " ::$proto_ns$::internal::AssignDescriptors(&$desc_table$);\n" " return $file_level_enum_descriptors$[$1$];\n" "}\n", idx); @@ -295,27 +299,128 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* printer) { "}\n" "\n"); + if (!HasDescriptorMethods(descriptor_->file(), options_)) { + // In lite mode (where descriptors are unavailable), we generate separate + // tables for mapping between enum names and numbers. The _entries table + // contains the bulk of the data and is sorted by name, while + // _entries_by_number is sorted by number and just contains pointers into + // _entries. The two tables allow mapping from name to number and number to + // name, both in time logarithmic in the number of enum entries. This could + // probably be made faster, but for now the tables are intended to be simple + // and compact. + // + // Enums with allow_alias = true support multiple entries with the same + // numerical value. In cases where there are multiple names for the same + // number, we treat the first name appearing in the .proto file as the + // canonical one. + std::map name_to_number; + std::map number_to_canonical_name; + for (int i = 0; i < descriptor_->value_count(); i++) { + const EnumValueDescriptor* value = descriptor_->value(i); + name_to_number.emplace(value->name(), value->number()); + // The same number may appear with multiple names, so we use emplace() to + // let the first name win. + number_to_canonical_name.emplace(value->number(), value->name()); + } + + format( + "static ::$proto_ns$::internal::ExplicitlyConstructed " + "$classname$_strings[$1$] = {};\n\n", + CountUniqueValues(descriptor_)); + + // We concatenate all the names for a given enum into one big string + // literal. If instead we store an array of string literals, the linker + // seems to put all enum strings for a given .proto file in the same + // section, which hinders its ability to strip out unused strings. + format("static const char $classname$_names[] ="); + for (const auto& p : name_to_number) { + format("\n \"$1$\"", p.first); + } + format(";\n\n"); + + format( + "static const ::$proto_ns$::internal::EnumEntry $classname$_entries[] " + "= {\n"); + int i = 0; + std::map number_to_index; + int data_index = 0; + for (const auto& p : name_to_number) { + format(" { {$classname$_names + $1$, $2$}, $3$ },\n", data_index, + p.first.size(), p.second); + if (number_to_canonical_name[p.second] == p.first) { + number_to_index.emplace(p.second, i); + } + ++i; + data_index += p.first.size(); + } + + format( + "};\n" + "\n" + "static const int $classname$_entries_by_number[] = {\n"); + for (const auto& p : number_to_index) { + format(" $1$, // $2$ -> $3$\n", p.second, p.first, + number_to_canonical_name[p.first]); + } + format( + "};\n" + "\n"); + + format( + "const std::string& $classname$_Name(\n" + " $classname$ value) {\n" + " static const bool dummy =\n" + " ::$proto_ns$::internal::InitializeEnumStrings(\n" + " $classname$_entries,\n" + " $classname$_entries_by_number,\n" + " $1$, $classname$_strings);\n" + " (void) dummy;\n" + " int idx = ::$proto_ns$::internal::LookUpEnumName(\n" + " $classname$_entries,\n" + " $classname$_entries_by_number,\n" + " $1$, value);\n" + " return idx == -1 ? ::$proto_ns$::internal::GetEmptyString() :\n" + " $classname$_strings[idx].get();\n" + "}\n", + CountUniqueValues(descriptor_)); + format( + "bool $classname$_Parse(\n" + " const std::string& name, $classname$* value) {\n" + " int int_value;\n" + " bool success = ::$proto_ns$::internal::LookUpEnumValue(\n" + " $classname$_entries, $1$, name, &int_value);\n" + " if (success) {\n" + " *value = static_cast<$classname$>(int_value);\n" + " }\n" + " return success;\n" + "}\n", + descriptor_->value_count()); + } + if (descriptor_->containing_type() != NULL) { - string parent = ClassName(descriptor_->containing_type(), false); - // We need to "define" the static constants which were declared in the - // header, to give the linker a place to put them. Or at least the C++ - // standard says we have to. MSVC actually insists that we do _not_ define - // them again in the .cc file, prior to VC++ 2015. - format("#if !defined(_MSC_VER) || _MSC_VER >= 1900\n"); + std::string parent = ClassName(descriptor_->containing_type(), false); + // Before C++17, we must define the static constants which were + // declared in the header, to give the linker a place to put them. + // But pre-2015 MSVC++ insists that we not. + format( + "#if (__cplusplus < 201703) && " + "(!defined(_MSC_VER) || _MSC_VER >= 1900)\n"); for (int i = 0; i < descriptor_->value_count(); i++) { - format("$constexpr $const $classname$ $1$::$2$;\n", parent, + format("constexpr $classname$ $1$::$2$;\n", parent, EnumValueName(descriptor_->value(i))); } format( - "const $classname$ $1$::$nested_name$_MIN;\n" - "const $classname$ $1$::$nested_name$_MAX;\n", + "constexpr $classname$ $1$::$nested_name$_MIN;\n" + "constexpr $classname$ $1$::$nested_name$_MAX;\n", parent); if (generate_array_size_) { - format("const int $1$::$nested_name$_ARRAYSIZE;\n", parent); + format("constexpr int $1$::$nested_name$_ARRAYSIZE;\n", parent); } - format("#endif // !defined(_MSC_VER) || _MSC_VER >= 1900\n"); + format( + "#endif // (__cplusplus < 201703) && " + "(!defined(_MSC_VER) || _MSC_VER >= 1900)\n"); } } diff --git a/src/google/protobuf/compiler/cpp/cpp_enum.h b/src/google/protobuf/compiler/cpp/cpp_enum.h index 55e6b83548..3687f04c27 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum.h +++ b/src/google/protobuf/compiler/cpp/cpp_enum.h @@ -43,9 +43,9 @@ namespace google { namespace protobuf { - namespace io { - class Printer; // printer.h - } +namespace io { +class Printer; // printer.h +} } // namespace protobuf } // namespace google @@ -58,7 +58,8 @@ class EnumGenerator { public: // See generator.cc for the meaning of dllexport_decl. EnumGenerator(const EnumDescriptor* descriptor, - const std::map& vars, const Options& options); + const std::map& vars, + const Options& options); ~EnumGenerator(); // Generate header code defining the enum. This code should be placed @@ -85,12 +86,12 @@ class EnumGenerator { private: const EnumDescriptor* descriptor_; - const string classname_; + const std::string classname_; const Options& options_; // whether to generate the *_ARRAYSIZE constant. const bool generate_array_size_; - std::map variables_; + std::map variables_; friend class FileGenerator; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumGenerator); diff --git a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc index a406f2eec8..9fdde41207 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc @@ -46,11 +46,11 @@ namespace cpp { namespace { void SetEnumVariables(const FieldDescriptor* descriptor, - std::map* variables, + std::map* variables, const Options& options) { SetCommonFieldVariables(descriptor, variables, options); const EnumValueDescriptor* default_value = descriptor->default_value_enum(); - (*variables)["type"] = ClassName(descriptor->enum_type(), true); + (*variables)["type"] = QualifiedClassName(descriptor->enum_type(), options); (*variables)["default"] = Int32ToString(default_value->number()); (*variables)["full_name"] = descriptor->full_name(); } @@ -86,11 +86,13 @@ void EnumFieldGenerator::GenerateInlineAccessorDefinitions( Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return static_cast< $type$ >($name$_);\n" "}\n" - "inline void $classname$::set_$name$($type$ value) {\n"); - if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { + "inline void $classname$::set_$name$($type$ value) {\n" + "$annotate_accessor$"); + if (!HasPreservingUnknownEnumSemantics(descriptor_)) { format(" assert($type$_IsValid(value));\n"); } format( @@ -134,7 +136,7 @@ void EnumFieldGenerator::GenerateMergeFromCodedStream( "DO_((::$proto_ns$::internal::WireFormatLite::ReadPrimitive<\n" " int, ::$proto_ns$::internal::WireFormatLite::TYPE_ENUM>(\n" " input, &value)));\n"); - if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { + if (HasPreservingUnknownEnumSemantics(descriptor_)) { format("set_$name$(static_cast< $type$ >(value));\n"); } else { format( @@ -157,18 +159,11 @@ void EnumFieldGenerator::GenerateMergeFromCodedStream( } } -void EnumFieldGenerator::GenerateSerializeWithCachedSizes( - io::Printer* printer) const { - Formatter format(printer, variables_); - format( - "::$proto_ns$::internal::WireFormatLite::WriteEnum(\n" - " $number$, this->$name$(), output);\n"); -} - void EnumFieldGenerator::GenerateSerializeWithCachedSizesToArray( io::Printer* printer) const { Formatter format(printer, variables_); format( + "stream->EnsureSpace(&target);\n" "target = ::$proto_ns$::internal::WireFormatLite::WriteEnumToArray(\n" " $number$, this->$name$(), target);\n"); } @@ -195,14 +190,16 @@ void EnumOneofFieldGenerator::GenerateInlineAccessorDefinitions( Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return static_cast< $type$ >($field_member$);\n" " }\n" " return static_cast< $type$ >($default$);\n" "}\n" - "inline void $classname$::set_$name$($type$ value) {\n"); - if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { + "inline void $classname$::set_$name$($type$ value) {\n" + "$annotate_accessor$"); + if (!HasPreservingUnknownEnumSemantics(descriptor_)) { format(" assert($type$_IsValid(value));\n"); } format( @@ -269,19 +266,22 @@ void RepeatedEnumFieldGenerator::GenerateInlineAccessorDefinitions( Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$(int index) const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return static_cast< $type$ >($name$_.Get(index));\n" "}\n" - "inline void $classname$::set_$name$(int index, $type$ value) {\n"); - if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { + "inline void $classname$::set_$name$(int index, $type$ value) {\n" + "$annotate_accessor$"); + if (!HasPreservingUnknownEnumSemantics(descriptor_)) { format(" assert($type$_IsValid(value));\n"); } format( " $name$_.Set(index, value);\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" - "inline void $classname$::add_$name$($type$ value) {\n"); - if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { + "inline void $classname$::add_$name$($type$ value) {\n" + "$annotate_accessor$"); + if (!HasPreservingUnknownEnumSemantics(descriptor_)) { format(" assert($type$_IsValid(value));\n"); } format( @@ -290,11 +290,13 @@ void RepeatedEnumFieldGenerator::GenerateInlineAccessorDefinitions( "}\n" "inline const ::$proto_ns$::RepeatedField&\n" "$classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_list:$full_name$)\n" " return $name$_;\n" "}\n" "inline ::$proto_ns$::RepeatedField*\n" "$classname$::mutable_$name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" " return &$name$_;\n" "}\n"); @@ -332,7 +334,7 @@ void RepeatedEnumFieldGenerator::GenerateMergeFromCodedStream( "DO_((::$proto_ns$::internal::WireFormatLite::ReadPrimitive<\n" " int, ::$proto_ns$::internal::WireFormatLite::TYPE_ENUM>(\n" " input, &value)));\n"); - if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { + if (HasPreservingUnknownEnumSemantics(descriptor_)) { format("add_$name$(static_cast< $type$ >(value));\n"); } else { format( @@ -359,14 +361,14 @@ void RepeatedEnumFieldGenerator::GenerateMergeFromCodedStreamWithPacking( Formatter format(printer, variables_); if (!descriptor_->is_packed()) { // This path is rarely executed, so we use a non-inlined implementation. - if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { + if (HasPreservingUnknownEnumSemantics(descriptor_)) { format( "DO_((::$proto_ns$::internal::" "WireFormatLite::ReadPackedEnumPreserveUnknowns(\n" " input,\n" " $number$,\n" - " NULL,\n" - " NULL,\n" + " nullptr,\n" + " nullptr,\n" " this->mutable_$name$())));\n"); } else if (UseUnknownFieldSet(descriptor_->file(), options_)) { format( @@ -398,7 +400,7 @@ void RepeatedEnumFieldGenerator::GenerateMergeFromCodedStreamWithPacking( " DO_((::$proto_ns$::internal::WireFormatLite::ReadPrimitive<\n" " int, ::$proto_ns$::internal::WireFormatLite::TYPE_ENUM>(\n" " input, &value)));\n"); - if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { + if (HasPreservingUnknownEnumSemantics(descriptor_)) { format(" add_$name$(static_cast< $type$ >(value));\n"); } else { format( @@ -423,58 +425,27 @@ void RepeatedEnumFieldGenerator::GenerateMergeFromCodedStreamWithPacking( } } -void RepeatedEnumFieldGenerator::GenerateSerializeWithCachedSizes( - io::Printer* printer) const { - Formatter format(printer, variables_); - if (descriptor_->is_packed()) { - // Write the tag and the size. - format( - "if (this->$name$_size() > 0) {\n" - " ::$proto_ns$::internal::WireFormatLite::WriteTag(\n" - " $number$,\n" - " " - "::$proto_ns$::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n" - " output);\n" - " output->WriteVarint32(_$name$_cached_byte_size_.load(\n" - " std::memory_order_relaxed));\n" - "}\n"); - } - format("for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); - if (descriptor_->is_packed()) { - format( - " ::$proto_ns$::internal::WireFormatLite::WriteEnumNoTag(\n" - " this->$name$(i), output);\n"); - } else { - format( - " ::$proto_ns$::internal::WireFormatLite::WriteEnum(\n" - " $number$, this->$name$(i), output);\n"); - } - format("}\n"); -} - void RepeatedEnumFieldGenerator::GenerateSerializeWithCachedSizesToArray( io::Printer* printer) const { Formatter format(printer, variables_); if (descriptor_->is_packed()) { // Write the tag and the size. format( - "if (this->$name$_size() > 0) {\n" - " target = ::$proto_ns$::internal::WireFormatLite::WriteTagToArray(\n" - " $number$,\n" - " " - "::$proto_ns$::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n" - " target);\n" - " target = ::$proto_ns$::io::CodedOutputStream::WriteVarint32ToArray(" - " _$name$_cached_byte_size_.load(std::memory_order_relaxed),\n" - " target);\n" - " target = " - "::$proto_ns$::internal::WireFormatLite::WriteEnumNoTagToArray(\n" - " this->$name$_, target);\n" + "{\n" + " int byte_size = " + "_$name$_cached_byte_size_.load(std::memory_order_relaxed);\n" + " if (byte_size > 0) {\n" + " target = stream->WriteEnumPacked(\n" + " $number$, $name$_, byte_size, target);\n" + " }\n" "}\n"); } else { format( - "target = ::$proto_ns$::internal::WireFormatLite::WriteEnumToArray(\n" - " $number$, this->$name$_, target);\n"); + "for (const auto& x : this->$name$()) {\n" + " stream->EnsureSpace(&target);\n" + " target = ::$proto_ns$::internal::WireFormatLite::WriteEnumToArray(\n" + " $number$, x, target);\n" + "}\n"); } } diff --git a/src/google/protobuf/compiler/cpp/cpp_enum_field.h b/src/google/protobuf/compiler/cpp/cpp_enum_field.h index b43d402b4c..7226a7afb6 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_enum_field.h @@ -59,7 +59,6 @@ class EnumFieldGenerator : public FieldGenerator { void GenerateConstructorCode(io::Printer* printer) const; void GenerateCopyConstructorCode(io::Printer* printer) const; void GenerateMergeFromCodedStream(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; @@ -100,7 +99,6 @@ class RepeatedEnumFieldGenerator : public FieldGenerator { void GenerateCopyConstructorCode(io::Printer* printer) const {} void GenerateMergeFromCodedStream(io::Printer* printer) const; void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_extension.cc b/src/google/protobuf/compiler/cpp/cpp_extension.cc index 25bcc333dd..97be5e6426 100644 --- a/src/google/protobuf/compiler/cpp/cpp_extension.cc +++ b/src/google/protobuf/compiler/cpp/cpp_extension.cc @@ -40,6 +40,7 @@ #include + namespace google { namespace protobuf { namespace compiler { @@ -50,7 +51,7 @@ namespace { // Returns the fully-qualified class name of the message that this field // extends. This function is used in the Google-internal code to handle some // legacy cases. -string ExtendeeClassName(const FieldDescriptor* descriptor) { +std::string ExtendeeClassName(const FieldDescriptor* descriptor) { const Descriptor* extendee = descriptor->containing_type(); return ClassName(extendee, true); } @@ -59,8 +60,7 @@ string ExtendeeClassName(const FieldDescriptor* descriptor) { ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor, const Options& options) - : descriptor_(descriptor), - options_(options) { + : descriptor_(descriptor), options_(options) { // Construct type_traits_. if (descriptor_->is_repeated()) { type_traits_ = "Repeated"; @@ -91,19 +91,19 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor, SetCommonVars(options, &variables_); variables_["extendee"] = ExtendeeClassName(descriptor_); variables_["type_traits"] = type_traits_; - string name = descriptor_->name(); - variables_["name"] = name; + std::string name = descriptor_->name(); + variables_["name"] = ResolveKeyword(name); variables_["constant_name"] = FieldConstantName(descriptor_); variables_["field_type"] = - SimpleItoa(static_cast(descriptor_->type())); + StrCat(static_cast(descriptor_->type())); variables_["packed"] = descriptor_->options().packed() ? "true" : "false"; - string scope = + std::string scope = IsScoped() ? ClassName(descriptor_->extension_scope(), false) + "::" : ""; variables_["scope"] = scope; - string scoped_name = scope + name; + std::string scoped_name = scope + ResolveKeyword(name); variables_["scoped_name"] = scoped_name; - variables_["number"] = SimpleItoa(descriptor_->number()); + variables_["number"] = StrCat(descriptor_->number()); } ExtensionGenerator::~ExtensionGenerator() {} @@ -118,7 +118,7 @@ void ExtensionGenerator::GenerateDeclaration(io::Printer* printer) const { // If this is a class member, it needs to be declared "static". Otherwise, // it needs to be "extern". In the latter case, it also needs the DLL // export/import specifier. - string qualifier; + std::string qualifier; if (!IsScoped()) { qualifier = "extern"; if (!options_.dllexport_decl.empty()) { @@ -132,13 +132,23 @@ void ExtensionGenerator::GenerateDeclaration(io::Printer* printer) const { "static const int $constant_name$ = $number$;\n" "$1$ ::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n" " ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$ >\n" - " $name$;\n", - qualifier); + " ${2$$name$$}$;\n", + qualifier, descriptor_); } void ExtensionGenerator::GenerateDefinition(io::Printer* printer) { + // If we are building for lite with implicit weak fields, we want to skip over + // any custom options (i.e. extensions of messages from descriptor.proto). + // This prevents the creation of any unnecessary linker references to the + // descriptor messages. + if (options_.lite_implicit_weak_fields && + descriptor_->containing_type()->file()->name() == + "net/proto2/proto/descriptor.proto") { + return; + } + Formatter format(printer, variables_); - string default_str; + std::string default_str; // If this is a class member, it needs to be declared in its class scope. if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) { // We need to declare a global string which will contain the default value. @@ -147,7 +157,7 @@ void ExtensionGenerator::GenerateDefinition(io::Printer* printer) { // replace :: with _ in the name and declare it as a global. default_str = StringReplace(variables_["scoped_name"], "::", "_", true) + "_default"; - format("const ::std::string $1$($2$);\n", default_str, + format("const std::string $1$($2$);\n", default_str, DefaultValue(options_, descriptor_)); } else { default_str = DefaultValue(options_, descriptor_); diff --git a/src/google/protobuf/compiler/cpp/cpp_extension.h b/src/google/protobuf/compiler/cpp/cpp_extension.h index c316f5dabc..72413f6b28 100644 --- a/src/google/protobuf/compiler/cpp/cpp_extension.h +++ b/src/google/protobuf/compiler/cpp/cpp_extension.h @@ -75,10 +75,10 @@ class ExtensionGenerator { private: const FieldDescriptor* descriptor_; - string type_traits_; + std::string type_traits_; Options options_; - std::map variables_; + std::map variables_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionGenerator); }; diff --git a/src/google/protobuf/compiler/cpp/cpp_field.cc b/src/google/protobuf/compiler/cpp/cpp_field.cc index 582da4ae09..72d6fa27a6 100644 --- a/src/google/protobuf/compiler/cpp/cpp_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_field.cc @@ -57,18 +57,18 @@ namespace cpp { using internal::WireFormat; void SetCommonFieldVariables(const FieldDescriptor* descriptor, - std::map* variables, + std::map* variables, const Options& options) { SetCommonVars(options, variables); - (*variables)["ns"] = Namespace(descriptor); + (*variables)["ns"] = Namespace(descriptor, options); (*variables)["name"] = FieldName(descriptor); - (*variables)["index"] = SimpleItoa(descriptor->index()); - (*variables)["number"] = SimpleItoa(descriptor->number()); + (*variables)["index"] = StrCat(descriptor->index()); + (*variables)["number"] = StrCat(descriptor->number()); (*variables)["classname"] = ClassName(FieldScope(descriptor), false); (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type()); (*variables)["field_member"] = FieldName(descriptor) + "_"; - (*variables)["tag_size"] = SimpleItoa( + (*variables)["tag_size"] = StrCat( WireFormat::TagSize(descriptor->number(), descriptor->type())); (*variables)["deprecated_attr"] = DeprecatedAttribute(options, descriptor->options().deprecated()); @@ -77,10 +77,11 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, (*variables)["clear_hasbit"] = ""; if (HasFieldPresence(descriptor->file())) { (*variables)["set_hasbit_io"] = - "HasBitSetters::set_has_" + FieldName(descriptor) + "(this);"; + "_Internal::set_has_" + FieldName(descriptor) + "(&_has_bits_);"; } else { (*variables)["set_hasbit_io"] = ""; } + (*variables)["annotate_accessor"] = ""; // These variables are placeholders to pick out the beginning and ends of // identifiers for annotations (when doing so with existing variables would @@ -102,9 +103,10 @@ void FieldGenerator::SetHasBitIndex(int32 has_bit_index) { strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;"); } -void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor, - std::map* variables) { - const string prefix = descriptor->containing_oneof()->name() + "_."; +void SetCommonOneofFieldVariables( + const FieldDescriptor* descriptor, + std::map* variables) { + const std::string prefix = descriptor->containing_oneof()->name() + "_."; (*variables)["oneof_name"] = descriptor->containing_oneof()->name(); (*variables)["field_member"] = StrCat(prefix, (*variables)["name"], "_"); @@ -112,8 +114,8 @@ void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor, FieldGenerator::~FieldGenerator() {} -void FieldGenerator:: -GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { +void FieldGenerator::GenerateMergeFromCodedStreamWithPacking( + io::Printer* printer) const { // Reaching here indicates a bug. Cases are: // - This FieldGenerator should support packing, but this method should be // overridden. @@ -121,15 +123,12 @@ GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { // never have been called. GOOGLE_LOG(FATAL) << "GenerateMergeFromCodedStreamWithPacking() " << "called on field generator that does not support packing."; - } FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor, const Options& options, MessageSCCAnalyzer* scc_analyzer) - : descriptor_(descriptor), - options_(options), - field_generators_(descriptor->field_count()) { + : descriptor_(descriptor), field_generators_(descriptor->field_count()) { // Construct all the FieldGenerators. for (int i = 0; i < descriptor->field_count(); i++) { field_generators_[i].reset( diff --git a/src/google/protobuf/compiler/cpp/cpp_field.h b/src/google/protobuf/compiler/cpp/cpp_field.h index 4561b33e85..470899ec65 100644 --- a/src/google/protobuf/compiler/cpp/cpp_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_field.h @@ -61,11 +61,12 @@ namespace cpp { // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size', // 'deprecation']. void SetCommonFieldVariables(const FieldDescriptor* descriptor, - std::map* variables, + std::map* variables, const Options& options); -void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor, - std::map* variables); +void SetCommonOneofFieldVariables( + const FieldDescriptor* descriptor, + std::map* variables); class FieldGenerator { public: @@ -73,7 +74,8 @@ class FieldGenerator { const Options& options) : descriptor_(descriptor), options_(options) {} virtual ~FieldGenerator(); - + virtual void GenerateSerializeWithCachedSizes( + io::Printer* printer) const final{}; // Generate lines of code declaring members fields of the message class // needed to represent this field. These are placed inside the message // class. @@ -179,10 +181,6 @@ class FieldGenerator { virtual void GenerateMergeFromCodedStreamWithPacking( io::Printer* printer) const; - // Generate lines to serialize this field, which are placed within the - // message's SerializeWithCachedSizes() method. - virtual void GenerateSerializeWithCachedSizes(io::Printer* printer) const = 0; - // Generate lines to serialize this field directly to the array "target", // which are placed within the message's SerializeWithCachedSizesToArray() // method. This must also advance "target" past the written bytes. @@ -203,7 +201,7 @@ class FieldGenerator { protected: const FieldDescriptor* descriptor_; const Options& options_; - std::map variables_; + std::map variables_; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator); @@ -226,7 +224,6 @@ class FieldGeneratorMap { private: const Descriptor* descriptor_; - const Options& options_; std::vector> field_generators_; static FieldGenerator* MakeGoogleInternalGenerator( diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc index 414da2f8ad..3db64b4f4c 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.cc +++ b/src/google/protobuf/compiler/cpp/cpp_file.cc @@ -33,9 +33,11 @@ // Sanjay Ghemawat, Jeff Dean, and others. #include + #include #include #include +#include #include #include @@ -50,6 +52,7 @@ #include + #include namespace google { @@ -57,24 +60,54 @@ namespace protobuf { namespace compiler { namespace cpp { +namespace { + +// When we forward-declare things, we want to create a sorted order so our +// output is deterministic and minimizes namespace changes. +template +std::string GetSortKey(const T& val) { + return val.full_name(); +} + +template <> +std::string GetSortKey(const FileDescriptor& val) { + return val.name(); +} + +template <> +std::string GetSortKey(const SCC& val) { + return val.GetRepresentative()->full_name(); +} + +template +bool CompareSortKeys(const T* a, const T* b) { + return GetSortKey(*a) < GetSortKey(*b); +} + +template +std::vector Sorted(const std::unordered_set& vals) { + std::vector sorted(vals.begin(), vals.end()); + std::sort(sorted.begin(), sorted.end(), CompareSortKeys); + return sorted; +} + +} // namespace + FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options) : file_(file), options_(options), scc_analyzer_(options) { // These variables are the same on a file level SetCommonVars(options, &variables_); variables_["dllexport_decl"] = options.dllexport_decl; variables_["tablename"] = UniqueName("TableStruct", file_, options_); - variables_["assign_desc_table"] = - UniqueName("assign_descriptors_table", file_, options_); variables_["file_level_metadata"] = UniqueName("file_level_metadata", file_, options_); + variables_["desc_table"] = DescriptorTableName(file_, options_); variables_["file_level_enum_descriptors"] = UniqueName("file_level_enum_descriptors", file_, options_); variables_["file_level_service_descriptors"] = UniqueName("file_level_service_descriptors", file_, options_); - variables_["add_descriptors"] = UniqueName("AddDescriptors", file_, options_); variables_["filename"] = file_->name(); - variables_["package_ns"] = Namespace(file_); - variables_["init_defaults"] = UniqueName("InitDefaults", file_, options_); + variables_["package_ns"] = Namespace(file_, options); std::vector msgs = FlattenMessagesInFile(file); for (int i = 0; i < msgs.size(); i++) { @@ -106,6 +139,13 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options) for (int i = 0; i < file->weak_dependency_count(); ++i) { weak_deps_.insert(file->weak_dependency(i)); } + for (int i = 0; i < message_generators_.size(); i++) { + if (IsSCCRepresentative(message_generators_[i]->descriptor_)) { + sccs_.push_back(GetSCC(message_generators_[i]->descriptor_)); + } + } + + std::sort(sccs_.begin(), sccs_.end(), CompareSortKeys); } FileGenerator::~FileGenerator() = default; @@ -119,11 +159,11 @@ void FileGenerator::GenerateMacroUndefs(io::Printer* printer) { file_->name() != "google/protobuf/compiler/plugin.proto") { return; } - std::vector names_to_undef; + std::vector names_to_undef; std::vector fields; ListAllFields(file_, &fields); for (int i = 0; i < fields.size(); i++) { - const string& name = fields[i]->name(); + const std::string& name = fields[i]->name(); static const char* kMacroNames[] = {"major", "minor"}; for (int i = 0; i < GOOGLE_ARRAYSIZE(kMacroNames); ++i) { if (name == kMacroNames[i]) { @@ -146,33 +186,24 @@ void FileGenerator::GenerateHeader(io::Printer* printer) { // port_def.inc must be included after all other includes. IncludeFile("net/proto2/public/port_def.inc", printer); - format("#define $1$$ dllexport_decl$\n", - UniqueName("PROTOBUF_INTERNAL_EXPORT", file_, options_)); + format("#define $1$$ dllexport_decl$\n", FileDllExport(file_, options_)); GenerateMacroUndefs(printer); - if (IsProto2MessageSetFile(file_, options_)) { - // Proto2 MessageSet overrides GetMapper() so we forward declare TagMapper - // to avoid inclusion of "tagmapper.h". - format("class TagMapper;\n"); - } - if (!options_.opensource_runtime) { - // EmbeddedMessageHolder is a proxy class to provide access into arena - // constructors for proto1 message objects. - // See net/proto/proto_arena_internal.h - format( - "namespace proto {\n" - "namespace internal {\n" - "template struct EmbeddedMessageHolder;\n" - "} // namespace internal\n" - "} // namespace proto\n"); - } + // For Any support with lite protos, we need to friend AnyMetadata, so we + // forward-declare it here. + format( + "PROTOBUF_NAMESPACE_OPEN\n" + "namespace internal {\n" + "class AnyMetadata;\n" + "} // namespace internal\n" + "PROTOBUF_NAMESPACE_CLOSE\n"); GenerateGlobalStateFunctionDeclarations(printer); GenerateForwardDeclarations(printer); { - NamespaceOpener ns(Namespace(file_), format); + NamespaceOpener ns(Namespace(file_, options_), format); format("\n"); @@ -214,14 +245,13 @@ void FileGenerator::GenerateHeader(io::Printer* printer) { } void FileGenerator::GenerateProtoHeader(io::Printer* printer, - const string& info_path) { + const std::string& info_path) { Formatter format(printer, variables_); if (!options_.proto_h) { return; } - string filename_identifier = FilenameIdentifier(file_->name()); - GenerateTopHeaderGuard(printer, filename_identifier); + GenerateTopHeaderGuard(printer, false); if (!options_.opensource_runtime) { format( @@ -240,14 +270,7 @@ void FileGenerator::GenerateProtoHeader(io::Printer* printer, for (int i = 0; i < file_->public_dependency_count(); i++) { const FileDescriptor* dep = file_->public_dependency(i); - const char* extension = ".proto.h"; - // The proto1 compiler only generates .pb.h files, so even if we are - // running in proto-h mode, we can only use the .pb.h. - if (IsProto1(dep, options_)) { - extension = ".pb.h"; - } - string dependency = StripProto(dep->name()) + extension; - format("#include \"$1$\"\n", dependency); + format("#include \"$1$.proto.h\"\n", StripProto(dep->name())); } format("// @@protoc_insertion_point(includes)\n"); @@ -256,18 +279,16 @@ void FileGenerator::GenerateProtoHeader(io::Printer* printer, GenerateHeader(printer); - GenerateBottomHeaderGuard(printer, filename_identifier); + GenerateBottomHeaderGuard(printer, false); } void FileGenerator::GeneratePBHeader(io::Printer* printer, - const string& info_path) { + const std::string& info_path) { Formatter format(printer, variables_); - string filename_identifier = - FilenameIdentifier(file_->name() + (options_.proto_h ? ".pb.h" : "")); - GenerateTopHeaderGuard(printer, filename_identifier); + GenerateTopHeaderGuard(printer, true); if (options_.proto_h) { - string target_basename = StripProto(file_->name()); + std::string target_basename = StripProto(file_->name()); if (!options_.opensource_runtime) { GetBootstrapBasename(options_, target_basename, &target_basename); } @@ -292,7 +313,7 @@ void FileGenerator::GeneratePBHeader(io::Printer* printer, GenerateHeader(printer); } else { { - NamespaceOpener ns(Namespace(file_), format); + NamespaceOpener ns(Namespace(file_, options_), format); format( "\n" "// @@protoc_insertion_point(namespace_scope)\n"); @@ -303,28 +324,26 @@ void FileGenerator::GeneratePBHeader(io::Printer* printer, "\n"); } - GenerateBottomHeaderGuard(printer, filename_identifier); + GenerateBottomHeaderGuard(printer, true); } -void FileGenerator::DoIncludeFile(const string& google3_name, bool do_export, - io::Printer* printer) { +void FileGenerator::DoIncludeFile(const std::string& google3_name, + bool do_export, io::Printer* printer) { Formatter format(printer, variables_); - const string prefix = "net/proto2/"; + const std::string prefix = "net/proto2/"; GOOGLE_CHECK(google3_name.find(prefix) == 0) << google3_name; if (options_.opensource_runtime) { - string path = google3_name.substr(prefix.size()); + std::string path = google3_name.substr(prefix.size()); path = StringReplace(path, "internal/", "", false); path = StringReplace(path, "proto/", "", false); path = StringReplace(path, "public/", "", false); - if (options_.opensource_include_paths) { + if (options_.runtime_include_base.empty()) { format("#include ", path); } else { - format( - "#include " - "\"third_party/protobuf/testing/extracted/src/google/protobuf/$1$\"", - path); + format("#include \"$1$google/protobuf/$2$\"", + options_.runtime_include_base, path); } } else { format("#include \"$1$\"", google3_name); @@ -337,23 +356,23 @@ void FileGenerator::DoIncludeFile(const string& google3_name, bool do_export, format("\n"); } -string FileGenerator::CreateHeaderInclude(const string& basename, - const FileDescriptor* file) { +std::string FileGenerator::CreateHeaderInclude(const std::string& basename, + const FileDescriptor* file) { bool use_system_include = false; - string name = basename; + std::string name = basename; if (options_.opensource_runtime) { if (IsWellKnownMessage(file)) { - if (options_.opensource_include_paths) { + if (options_.runtime_include_base.empty()) { use_system_include = true; } else { - name = "third_party/protobuf/testing/extracted/src/" + basename; + name = options_.runtime_include_base + basename; } } } - string left = "\""; - string right = "\""; + std::string left = "\""; + std::string right = "\""; if (use_system_include) { left = "<"; right = ">"; @@ -363,7 +382,7 @@ string FileGenerator::CreateHeaderInclude(const string& basename, void FileGenerator::GenerateSourceIncludes(io::Printer* printer) { Formatter format(printer, variables_); - string target_basename = StripProto(file_->name()); + std::string target_basename = StripProto(file_->name()); if (!options_.opensource_runtime) { GetBootstrapBasename(options_, target_basename, &target_basename); } @@ -378,14 +397,10 @@ void FileGenerator::GenerateSourceIncludes(io::Printer* printer) { "\n", CreateHeaderInclude(target_basename, file_)); - if (options_.opensource_runtime) { - DoIncludeFile("net/proto2/public/stubs/common.h", false, printer); - } - IncludeFile("net/proto2/io/public/coded_stream.h", printer); // TODO(gerbens) This is to include parse_context.h, we need a better way IncludeFile("net/proto2/public/extension_set.h", printer); - IncludeFile("net/proto2/public/wire_format_lite_inl.h", printer); + IncludeFile("net/proto2/public/wire_format_lite.h", printer); // Unknown fields implementation in lite mode uses StringOutputStream if (!UseUnknownFieldSet(file_, options_) && !message_generators_.empty()) { @@ -408,37 +423,16 @@ void FileGenerator::GenerateSourceIncludes(io::Printer* printer) { // Use the smaller .proto.h files. for (int i = 0; i < file_->dependency_count(); i++) { const FileDescriptor* dep = file_->dependency(i); - const char* extension = ".proto.h"; - string basename = StripProto(dep->name()); // Do not import weak deps. if (!options_.opensource_runtime && IsDepWeak(dep)) continue; - // The proto1 compiler only generates .pb.h files, so even if we are - // running in proto-h mode, we can only use the .pb.h. - if (IsProto1(dep, options_)) { - extension = ".pb.h"; - } + std::string basename = StripProto(dep->name()); if (IsBootstrapProto(options_, file_)) { GetBootstrapBasename(options_, basename, &basename); } - string dependency = basename + extension; - format("#include \"$1$\"\n", dependency); + format("#include \"$1$.proto.h\"\n", basename); } } - // TODO(gerbens) Remove this when all code in google is using the same - // proto library. This is a temporary hack to force build errors if - // the proto library is compiled with GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS - // and is also linking internal proto2. This is to prevent regressions while - // we work cleaning up the code base. After this is completed and we have - // one proto lib all code uses this should be removed. - if (options_.opensource_runtime) { - format( - "// This is a temporary google only hack\n" - "#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS\n" - "#include \"third_party/protobuf/version.h\"\n" - "#endif\n"); - } - format("// @@protoc_insertion_point(includes)\n"); IncludeFile("net/proto2/public/port_def.inc", printer); } @@ -447,72 +441,99 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* printer) { Formatter format(printer, variables_); format( - "class $1$DefaultTypeInternal {\n" + "class $1$ {\n" " public:\n" - " ::$proto_ns$::internal::ExplicitlyConstructed<$1$> _instance;\n", + " ::$proto_ns$::internal::ExplicitlyConstructed<$2$> _instance;\n", + DefaultInstanceType(message_generators_[idx]->descriptor_, options_), message_generators_[idx]->classname_); format.Indent(); message_generators_[idx]->GenerateExtraDefaultFields(printer); format.Outdent(); - format("} _$1$_default_instance_;\n", message_generators_[idx]->classname_); + format("} $1$;\n", + DefaultInstanceName(message_generators_[idx]->descriptor_, options_)); } -// Generates weak symbol declarations for types that are to be considered weakly -// referenced. -void FileGenerator::GenerateInternalForwardDeclarations( - const std::vector& fields, const Options& options, - MessageSCCAnalyzer* scc_analyzer, io::Printer* printer) { - Formatter format(printer); - // To ensure determinism and minimize the number of namespace statements, - // we output the forward declarations sorted on namespace and type / function - // name. - std::set global_namespace_decls; - // weak defaults - std::set > messages; - for (int i = 0; i < fields.size(); ++i) { - const FieldDescriptor* field = fields[i]; - const Descriptor* msg = field->message_type(); - if (msg == nullptr) continue; - bool is_weak = IsImplicitWeakField(field, options, scc_analyzer); - if (field->options().weak()) { - GOOGLE_CHECK(!options_.opensource_runtime); - is_weak = true; - } - string weak_attr; - if (is_weak) { - global_namespace_decls.insert( - "void " + UniqueName("AddDescriptors", msg, options_) + - "() __attribute__((weak))"); - messages.insert(std::make_pair(Namespace(msg), ClassName(msg))); - weak_attr = " __attribute__((weak))"; - } - string dllexport = UniqueName("PROTOBUF_INTERNAL_EXPORT", msg, options_); - if (IsProto1(msg->file(), options_) || IsWeak(field, options_)) { - dllexport = ""; +// A list of things defined in one .pb.cc file that we need to reference from +// another .pb.cc file. +struct FileGenerator::CrossFileReferences { + // Populated if we are referencing from messages or files. + std::unordered_set strong_sccs; + std::unordered_set weak_sccs; + std::unordered_set weak_default_instances; + + // Only if we are referencing from files. + std::unordered_set strong_reflection_files; + std::unordered_set weak_reflection_files; +}; + +void FileGenerator::GetCrossFileReferencesForField(const FieldDescriptor* field, + CrossFileReferences* refs) { + const Descriptor* msg = field->message_type(); + if (msg == nullptr) return; + const SCC* scc = GetSCC(msg); + + if (IsImplicitWeakField(field, options_, &scc_analyzer_) || + IsWeak(field, options_)) { + refs->weak_sccs.insert(scc); + refs->weak_default_instances.insert(msg); + } else { + refs->strong_sccs.insert(scc); + // We don't need to declare default instances, because it is declared in the + // .proto.h file we imported. + } +} + +void FileGenerator::GetCrossFileReferencesForFile(const FileDescriptor* file, + CrossFileReferences* refs) { + ForEachField(file, [this, refs](const FieldDescriptor* field) { + GetCrossFileReferencesForField(field, refs); + }); + + if (!HasDescriptorMethods(file, options_)) return; + + for (int i = 0; i < file->dependency_count(); i++) { + const FileDescriptor* dep = file->dependency(i); + if (IsDepWeak(dep)) { + refs->weak_reflection_files.insert(dep); + } else { + refs->strong_reflection_files.insert(dep); } - auto scc = scc_analyzer->GetSCC(msg); - string repr = - UniqueName(ClassName(scc->GetRepresentative()), msg, options_); - global_namespace_decls.insert( - "extern " + dllexport + weak_attr + " ::" + ProtobufNamespace(options) + - "::internal::SCCInfo<" + SimpleItoa(scc->children.size()) + - "> scc_info_" + repr); } +} - format("\n"); +// Generates references to variables defined in other files. +void FileGenerator::GenerateInternalForwardDeclarations( + const CrossFileReferences& refs, io::Printer* printer) { + Formatter format(printer, variables_); + + for (auto scc : Sorted(refs.strong_sccs)) { + format("extern $1$ ::$proto_ns$::internal::SCCInfo<$2$> $3$;\n", + FileDllExport(scc->GetFile(), options_), scc->children.size(), + SccInfoSymbol(scc, options_)); + } - for (const string& decl : global_namespace_decls) { - format("$1$;\n", decl); + for (auto scc : Sorted(refs.weak_sccs)) { + format( + "extern __attribute__((weak)) ::$proto_ns$::internal::SCCInfo<$1$> " + "$2$;\n", + scc->children.size(), SccInfoSymbol(scc, options_)); + } + + { + NamespaceOpener ns(format); + for (auto instance : Sorted(refs.weak_default_instances)) { + ns.ChangeTo(Namespace(instance, options_)); + format("extern __attribute__((weak)) $1$ $2$;\n", + DefaultInstanceType(instance, options_), + DefaultInstanceName(instance, options_)); + } } - // Weak external fields - NamespaceOpener ns(format); - for (const auto& pair : messages) { - ns.ChangeTo(pair.first); + for (auto file : Sorted(refs.weak_reflection_files)) { format( - "extern __attribute__((weak)) $1$DefaultTypeInternal " - "_$1$_default_instance_;\n", - pair.second); + "extern __attribute__((weak)) const " + "::$proto_ns$::internal::DescriptorTable $1$;\n", + DescriptorTableName(file, options_)); } } @@ -523,21 +544,22 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* printer) { // Generate weak declarations. We do this for the whole strongly-connected // component (SCC), because we have a single InitDefaults* function for the // SCC. - std::vector fields; + CrossFileReferences refs; for (const Descriptor* message : scc_analyzer_.GetSCC(message_generators_[idx]->descriptor_) ->descriptors) { - ListAllFields(message, &fields); + ForEachField(message, [this, &refs](const FieldDescriptor* field) { + GetCrossFileReferencesForField(field, &refs); + }); } - GenerateInternalForwardDeclarations(fields, options_, &scc_analyzer_, - printer); + GenerateInternalForwardDeclarations(refs, printer); if (IsSCCRepresentative(message_generators_[idx]->descriptor_)) { GenerateInitForSCC(GetSCC(message_generators_[idx]->descriptor_), printer); } { // package namespace - NamespaceOpener ns(Namespace(file_), format); + NamespaceOpener ns(Namespace(file_, options_), format); // Define default instances GenerateSourceDefaultInstance(idx, printer); @@ -579,7 +601,7 @@ void FileGenerator::GenerateGlobalSource(io::Printer* printer) { } } - NamespaceOpener ns(Namespace(file_), format); + NamespaceOpener ns(Namespace(file_, options_), format); // Generate enums. for (int i = 0; i < enum_generators_.size(); i++) { @@ -605,13 +627,12 @@ void FileGenerator::GenerateGlobalSource(io::Printer* printer) { void FileGenerator::GenerateSource(io::Printer* printer) { Formatter format(printer, variables_); GenerateSourceIncludes(printer); - std::vector fields; - ListAllFields(file_, &fields); - GenerateInternalForwardDeclarations(fields, options_, &scc_analyzer_, - printer); + CrossFileReferences refs; + GetCrossFileReferencesForFile(file_, &refs); + GenerateInternalForwardDeclarations(refs, printer); { - NamespaceOpener ns(Namespace(file_), format); + NamespaceOpener ns(Namespace(file_, options_), format); // Define default instances for (int i = 0; i < message_generators_.size(); i++) { @@ -627,27 +648,11 @@ void FileGenerator::GenerateSource(io::Printer* printer) { GenerateTables(printer); // Now generate the InitDefaults for each SCC. - for (int i = 0; i < message_generators_.size(); i++) { - if (IsSCCRepresentative(message_generators_[i]->descriptor_)) { - GenerateInitForSCC(GetSCC(message_generators_[i]->descriptor_), - printer); - } + for (auto scc : sccs_) { + GenerateInitForSCC(scc, printer); } if (HasDescriptorMethods(file_, options_)) { - // TODO(gerbens) This is for proto1 interoperability. Remove when proto1 - // is gone. - format("void $init_defaults$() {\n"); - for (int i = 0; i < message_generators_.size(); i++) { - if (!IsSCCRepresentative(message_generators_[i]->descriptor_)) continue; - string scc_name = - UniqueName(ClassName(message_generators_[i]->descriptor_), - message_generators_[i]->descriptor_, options_); - format(" ::$proto_ns$::internal::InitSCC(&scc_info_$1$.base);\n", - scc_name); - } - format("}\n\n"); - // Define the code to initialize reflection. This code uses a global // constructor to register reflection data with the runtime pre-main. GenerateReflectionInitializationCode(printer); @@ -655,7 +660,7 @@ void FileGenerator::GenerateSource(io::Printer* printer) { } { - NamespaceOpener ns(Namespace(file_), format); + NamespaceOpener ns(Namespace(file_, options_), format); // Actually implement the protos @@ -702,47 +707,42 @@ void FileGenerator::GenerateSource(io::Printer* printer) { format( "\n" "// @@protoc_insertion_point(global_scope)\n"); + + IncludeFile("net/proto2/public/port_undef.inc", printer); } void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { Formatter format(printer, variables_); - // AddDescriptors() is a file-level procedure which adds the encoded - // FileDescriptorProto for this .proto file to the global DescriptorPool for - // generated files (DescriptorPool::generated_pool()). It ordinarily runs at - // static initialization time, but is not used at all in LITE_RUNTIME mode. - // - // Its sibling, AssignDescriptors(), actually pulls the compiled - // FileDescriptor from the DescriptorPool and uses it to populate all of - // the global variables which store pointers to the descriptor objects. - // It also constructs the reflection objects. It is called the first time - // anyone calls descriptor() or GetReflection() on one of the types defined - // in the file. - if (!message_generators_.empty()) { - format("::$proto_ns$::Metadata $file_level_metadata$[$1$];\n", + format("static ::$proto_ns$::Metadata $file_level_metadata$[$1$];\n", message_generators_.size()); } else { format( + "static " "constexpr ::$proto_ns$::Metadata* $file_level_metadata$ = nullptr;\n"); } if (!enum_generators_.empty()) { format( + "static " "const ::$proto_ns$::EnumDescriptor* " "$file_level_enum_descriptors$[$1$];\n", enum_generators_.size()); } else { format( + "static " "constexpr ::$proto_ns$::EnumDescriptor const** " "$file_level_enum_descriptors$ = nullptr;\n"); } if (HasGenericServices(file_, options_) && file_->service_count() > 0) { format( + "static " "const ::$proto_ns$::ServiceDescriptor* " "$file_level_service_descriptors$[$1$];\n", file_->service_count()); } else { format( + "static " "constexpr ::$proto_ns$::ServiceDescriptor const** " "$file_level_service_descriptors$ = nullptr;\n"); } @@ -751,7 +751,7 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { format( "\n" "const $uint32$ $tablename$::offsets[] " - "$GOOGLE_PROTOBUF$_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); + "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n"); format.Indent(); std::vector > pairs; pairs.reserve(message_generators_.size()); @@ -762,7 +762,7 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { format( "};\n" "static const ::$proto_ns$::internal::MigrationSchema schemas[] " - "$GOOGLE_PROTOBUF$_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); + "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n"); format.Indent(); { int offset = 0; @@ -783,8 +783,8 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { format( "reinterpret_cast(&$1$::_$2$_default_instance_),\n", - Namespace(descriptor), // 1 - ClassName(descriptor)); // 2 + Namespace(descriptor, options_), // 1 + ClassName(descriptor)); // 2 } format.Outdent(); format( @@ -796,87 +796,119 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { // MSVC doesn't like empty arrays, so we add a dummy. "const $uint32$ $tablename$::offsets[1] = {};\n" "static constexpr ::$proto_ns$::internal::MigrationSchema* schemas = " - "NULL;" + "nullptr;" "\n" "static constexpr ::$proto_ns$::Message* const* " - "file_default_instances = NULL;\n" + "file_default_instances = nullptr;\n" "\n"); } // --------------------------------------------------------------- - // protobuf_AssignDescriptorsOnce(): The first time it is called, calls - // AssignDescriptors(). All later times, waits for the first call to - // complete and then returns. - format( - "::$proto_ns$::internal::AssignDescriptorsTable $assign_desc_table$ = " - "{\n" - " {}, $add_descriptors$, \"$filename$\", schemas,\n" - " file_default_instances, $tablename$::offsets,\n" - " $file_level_metadata$, $1$, $file_level_enum_descriptors$, " - "$file_level_service_descriptors$,\n" - "};\n" - "\n", - message_generators_.size()); - - // Now generate the AddDescriptors() function. - format( - "::$proto_ns$::internal::DescriptorTable $1$ = {\n" - " false, $init_defaults$, \n", - UniqueName("descriptor_table", file_, options_)); - format.Indent(); - // Embed the descriptor. We simply serialize the entire - // FileDescriptorProto - // and embed it as a string literal, which is parsed and built into real - // descriptors at initialization time. + // FileDescriptorProto/ and embed it as a string literal, which is parsed and + // built into real descriptors at initialization time. + const std::string protodef_name = + UniqueName("descriptor_table_protodef", file_, options_); + format("const char $1$[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) =\n", + protodef_name); + format.Indent(); FileDescriptorProto file_proto; file_->CopyTo(&file_proto); - string file_data; + std::string file_data; file_proto.SerializeToString(&file_data); { - // Only write 40 bytes per line. - static const int kBytesPerLine = 40; - for (int i = 0; i < file_data.size(); i += kBytesPerLine) { - format( - "\"$1$\"\n", - EscapeTrigraphs(CEscape(file_data.substr(i, kBytesPerLine)))); + if (file_data.size() > 65535) { + // Workaround for MSVC: "Error C1091: compiler limit: string exceeds + // 65535 bytes in length". Declare a static array of chars rather than + // use a string literal. Only write 25 bytes per line. + static const int kBytesPerLine = 25; + format("{ "); + for (int i = 0; i < file_data.size();) { + for (int j = 0; j < kBytesPerLine && i < file_data.size(); ++i, ++j) { + format("'$1$', ", CEscape(file_data.substr(i, 1))); + } + format("\n"); + } + format("'\\0' }"); // null-terminate + } else { + // Only write 40 bytes per line. + static const int kBytesPerLine = 40; + for (int i = 0; i < file_data.size(); i += kBytesPerLine) { + format( + "\"$1$\"\n", + EscapeTrigraphs(CEscape(file_data.substr(i, kBytesPerLine)))); + } } + format(";\n"); } - format.Outdent(); - const int num_deps = file_->dependency_count(); + + CrossFileReferences refs; + GetCrossFileReferencesForFile(file_, &refs); + int num_deps = + refs.strong_reflection_files.size() + refs.weak_reflection_files.size(); + + // Build array of DescriptorTable deps. format( - ",\n \"$filename$\", &$assign_desc_table$, $1$,\n" - "};\n\n" - "void $add_descriptors$() {\n" - " static constexpr ::$proto_ns$::internal::InitFunc deps[$2$] =\n" - " {\n", - file_data.size(), std::max(num_deps, 1)); - for (int i = 0; i < num_deps; i++) { - const FileDescriptor* dependency = file_->dependency(i); - format(" ::$1$,\n", UniqueName("AddDescriptors", dependency, options_)); + "static const ::$proto_ns$::internal::DescriptorTable*const " + "$desc_table$_deps[$1$] = {\n", + std::max(num_deps, 1)); + + for (auto dep : Sorted(refs.strong_reflection_files)) { + format(" &::$1$,\n", DescriptorTableName(dep, options_)); } + for (auto dep : Sorted(refs.weak_reflection_files)) { + format(" &::$1$,\n", DescriptorTableName(dep, options_)); + } + + format("};\n"); + + // Build array of SCCs from this file. format( - " };\n" - " ::$proto_ns$::internal::AddDescriptors(&$1$, deps, $2$);\n" - "}\n\n", - UniqueName("descriptor_table", file_, options_), // 1 - num_deps); // 2 + "static ::$proto_ns$::internal::SCCInfoBase*const " + "$desc_table$_sccs[$1$] = {\n", + std::max(sccs_.size(), 1)); + + for (auto scc : sccs_) { + format(" &$1$.base,\n", SccInfoSymbol(scc, options_)); + } + + format("};\n"); + + // The DescriptorTable itself. format( - "// Force running AddDescriptors() at dynamic initialization time.\n" - "static bool $1$ = []() { $add_descriptors$(); return true; }();\n", - UniqueName("dynamic_init_dummy", file_, options_)); + "static ::$proto_ns$::internal::once_flag $desc_table$_once;\n" + "static bool $desc_table$_initialized = false;\n" + "const ::$proto_ns$::internal::DescriptorTable $desc_table$ = {\n" + " &$desc_table$_initialized, $1$, \"$filename$\", $2$,\n" + " &$desc_table$_once, $desc_table$_sccs, $desc_table$_deps, $3$, $4$,\n" + " schemas, file_default_instances, $tablename$::offsets,\n" + " $file_level_metadata$, $5$, $file_level_enum_descriptors$, " + "$file_level_service_descriptors$,\n" + "};\n\n", + protodef_name, file_data.size(), sccs_.size(), num_deps, + message_generators_.size()); + + // For descriptor.proto we want to avoid doing any dynamic initialization, + // because in some situations that would otherwise pull in a lot of + // unnecessary code that can't be stripped by --gc-sections. Descriptor + // initialization will still be performed lazily when it's needed. + if (file_->name() != "net/proto2/proto/descriptor.proto") { + format( + "// Force running AddDescriptors() at dynamic initialization time.\n" + "static bool $1$ = (" + " ::$proto_ns$::internal::AddDescriptors(&$desc_table$), true);\n", + UniqueName("dynamic_init_dummy", file_, options_)); + } } void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) { Formatter format(printer, variables_); - const string scc_name = ClassName(scc->GetRepresentative()); // We use static and not anonymous namespace because symbol names are // substantially shorter. - format("static void InitDefaults$1$() {\n", - UniqueName(scc_name, scc->GetRepresentative(), options_)); + format("static void InitDefaults$1$() {\n", SccInfoSymbol(scc, options_)); if (options_.opensource_runtime) { format(" GOOGLE_PROTOBUF_VERIFY_VERSION;\n\n"); @@ -884,15 +916,6 @@ void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) { format.Indent(); - if (!options_.opensource_runtime) { - for (int i = 0; i < scc->children.size(); i++) { - const SCC* child_scc = scc->children[i]; - const FileDescriptor* dependency = child_scc->GetRepresentative()->file(); - if (!IsProto1(dependency, options_)) continue; - format("$1$();\n", UniqueName("InitDefaults", dependency, options_)); - } - } - // First construct all the necessary default instances. for (int i = 0; i < message_generators_.size(); i++) { if (scc_analyzer_.GetSCC(message_generators_[i]->descriptor_) != scc) { @@ -903,14 +926,17 @@ void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) { message_generators_[i]->GenerateFieldDefaultInstances(printer); format( "{\n" - " void* ptr = &$1$::_$2$_default_instance_;\n" - " new (ptr) $1$::$2$();\n", - Namespace(message_generators_[i]->descriptor_), // 1 - ClassName(message_generators_[i]->descriptor_)); // 2 + " void* ptr = &$1$;\n" + " new (ptr) $2$();\n", + QualifiedDefaultInstanceName(message_generators_[i]->descriptor_, + options_), + QualifiedClassName(message_generators_[i]->descriptor_, options_)); if (options_.opensource_runtime && !IsMapEntryMessage(message_generators_[i]->descriptor_)) { format( - " ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);\n"); + " " + "::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr);" + "\n"); } format("}\n"); } @@ -923,29 +949,20 @@ void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) { continue; } format("$1$::InitAsDefaultInstance();\n", - QualifiedClassName(message_generators_[i]->descriptor_)); + QualifiedClassName(message_generators_[i]->descriptor_, options_)); } format.Outdent(); format("}\n\n"); format( - "$dllexport_decl $::$proto_ns$::internal::SCCInfo<$1$> " - "scc_info_$2$ =\n" + "$dllexport_decl $::$proto_ns$::internal::SCCInfo<$1$> $2$ =\n" " " "{{ATOMIC_VAR_INIT(::$proto_ns$::internal::SCCInfoBase::kUninitialized), " "$1$, InitDefaults$2$}, {", scc->children.size(), // 1 - UniqueName(ClassName(scc->GetRepresentative()), scc->GetRepresentative(), - options_)); + SccInfoSymbol(scc, options_)); for (const SCC* child : scc->children) { - auto repr = child->GetRepresentative(); - if (IsProto1(repr->file(), options_)) { - GOOGLE_CHECK(!options_.opensource_runtime); - format("\n nullptr,"); - continue; - } - format("\n &scc_info_$1$.base,", - UniqueName(ClassName(repr), repr, options_)); + format("\n &$1$.base,", SccInfoSymbol(child, options_)); } format("}};\n\n"); } @@ -958,7 +975,7 @@ void FileGenerator::GenerateTables(io::Printer* printer) { format( "PROTOBUF_CONSTEXPR_VAR ::$proto_ns$::internal::ParseTableField\n" " const $tablename$::entries[] " - "$GOOGLE_PROTOBUF$_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); + "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n"); format.Indent(); std::vector entries; @@ -981,7 +998,7 @@ void FileGenerator::GenerateTables(io::Printer* printer) { "PROTOBUF_CONSTEXPR_VAR " "::$proto_ns$::internal::AuxillaryParseTableField\n" " const $tablename$::aux[] " - "$GOOGLE_PROTOBUF$_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); + "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n"); format.Indent(); std::vector aux_entries; @@ -1001,7 +1018,7 @@ void FileGenerator::GenerateTables(io::Printer* printer) { "};\n" "PROTOBUF_CONSTEXPR_VAR ::$proto_ns$::internal::ParseTable const\n" " $tablename$::schema[] " - "$GOOGLE_PROTOBUF$_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); + "PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {\n"); format.Indent(); size_t offset = 0; @@ -1013,7 +1030,7 @@ void FileGenerator::GenerateTables(io::Printer* printer) { } if (message_generators_.empty()) { - format("{ NULL, NULL, 0, -1, -1, false },\n"); + format("{ nullptr, nullptr, 0, -1, -1, false },\n"); } format.Outdent(); @@ -1067,7 +1084,7 @@ class FileGenerator::ForwardDeclarations { void Print(const Formatter& format, const Options& options) const { for (const auto& p : enums_) { - const string& enumname = p.first; + const std::string& enumname = p.first; const EnumDescriptor* enum_desc = p.second; format( "enum ${1$$2$$}$ : int;\n" @@ -1075,32 +1092,33 @@ class FileGenerator::ForwardDeclarations { enum_desc, enumname); } for (const auto& p : classes_) { - const string& classname = p.first; + const std::string& classname = p.first; const Descriptor* class_desc = p.second; format( "class ${1$$2$$}$;\n" - "class $2$DefaultTypeInternal;\n" - "$dllexport_decl $extern " - "$2$DefaultTypeInternal _$2$_default_instance_;\n", - class_desc, classname); + "class $3$;\n" + "$dllexport_decl $extern $3$ $4$;\n", + class_desc, classname, DefaultInstanceType(class_desc, options), + DefaultInstanceName(class_desc, options)); if (options.lite_implicit_weak_fields) { format("void $1$_ReferenceStrong();\n", classname); } } } - void PrintTopLevelDecl(const Formatter& format) const { + void PrintTopLevelDecl(const Formatter& format, + const Options& options) const { for (const auto& pair : classes_) { format( "template<> $dllexport_decl $" "$1$* Arena::CreateMaybeMessage<$1$>(Arena*);\n", - QualifiedClassName(pair.second)); + QualifiedClassName(pair.second, options)); } } private: - std::map classes_; - std::map enums_; + std::map classes_; + std::map enums_; }; static void PublicImportDFS(const FileDescriptor* fd, @@ -1135,42 +1153,46 @@ void FileGenerator::GenerateForwardDeclarations(io::Printer* printer) { std::unordered_set public_set; PublicImportDFS(file_, &public_set); - std::map decls; + std::map decls; for (int i = 0; i < classes.size(); i++) { const Descriptor* d = classes[i]; - if (d && !public_set.count(d->file())) decls[Namespace(d)].AddMessage(d); + if (d && !public_set.count(d->file())) + decls[Namespace(d, options_)].AddMessage(d); } for (int i = 0; i < enums.size(); i++) { const EnumDescriptor* d = enums[i]; - if (d && !public_set.count(d->file())) decls[Namespace(d)].AddEnum(d); + if (d && !public_set.count(d->file())) + decls[Namespace(d, options_)].AddEnum(d); } - NamespaceOpener ns(format); - for (const auto& pair : decls) { - ns.ChangeTo(pair.first); - pair.second.Print(format, options_); + { + NamespaceOpener ns(format); + for (const auto& pair : decls) { + ns.ChangeTo(pair.first); + pair.second.Print(format, options_); + } } - ns.ChangeTo(variables_["proto_ns"]); + format("PROTOBUF_NAMESPACE_OPEN\n"); for (const auto& pair : decls) { - pair.second.PrintTopLevelDecl(format); + pair.second.PrintTopLevelDecl(format, options_); } + format("PROTOBUF_NAMESPACE_CLOSE\n"); } -void FileGenerator::GenerateTopHeaderGuard(io::Printer* printer, - const string& filename_identifier) { +void FileGenerator::GenerateTopHeaderGuard(io::Printer* printer, bool pb_h) { Formatter format(printer, variables_); // Generate top of header. format( "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" "// source: $filename$\n" "\n" - "#ifndef PROTOBUF_INCLUDED_$1$\n" - "#define PROTOBUF_INCLUDED_$1$\n" + "#ifndef $1$\n" + "#define $1$\n" "\n" "#include \n" "#include \n", - filename_identifier); + IncludeGuard(file_, pb_h, options_)); if (!options_.opensource_runtime && !enum_generators_.empty()) { // Add header to provide std::is_integral for safe Enum_Name() function. format("#include \n"); @@ -1178,10 +1200,10 @@ void FileGenerator::GenerateTopHeaderGuard(io::Printer* printer, format("\n"); } -void FileGenerator::GenerateBottomHeaderGuard( - io::Printer* printer, const string& filename_identifier) { +void FileGenerator::GenerateBottomHeaderGuard(io::Printer* printer, bool pb_h) { Formatter format(printer, variables_); - format("#endif // PROTOBUF_INCLUDED_$1$\n", filename_identifier); + format("#endif // $GOOGLE_PROTOBUF$_INCLUDED_$1$\n", + IncludeGuard(file_, pb_h, options_)); } void FileGenerator::GenerateLibraryIncludes(io::Printer* printer) { @@ -1199,25 +1221,25 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* printer) { } if (options_.opensource_runtime) { - DoIncludeFile("net/proto2/public/stubs/common.h", false, printer); - // Verify the protobuf library header version is compatible with the protoc // version before going any further. + IncludeFile("net/proto2/public/port_def.inc", printer); format( - "#if GOOGLE_PROTOBUF_VERSION < $1$\n" + "#if PROTOBUF_VERSION < $1$\n" "#error This file was generated by a newer version of protoc which is\n" "#error incompatible with your Protocol Buffer headers. Please update\n" "#error your headers.\n" "#endif\n" - "#if $2$ < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION\n" + "#if $2$ < PROTOBUF_MIN_PROTOC_VERSION\n" "#error This file was generated by an older version of protoc which " "is\n" "#error incompatible with your Protocol Buffer headers. Please\n" "#error regenerate this file with a newer version of protoc.\n" "#endif\n" "\n", - GOOGLE_PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC, // 1 - GOOGLE_PROTOBUF_VERSION); // 2 + PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC, // 1 + PROTOBUF_VERSION); // 2 + IncludeFile("net/proto2/public/port_undef.inc", printer); } // OK, it's now safe to #include other files. @@ -1230,6 +1252,7 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* printer) { if (HasDescriptorMethods(file_, options_)) { IncludeFile("net/proto2/public/metadata.h", printer); + IncludeFile("net/proto2/public/generated_message_reflection.h", printer); } else { IncludeFile("net/proto2/public/metadata_lite.h", printer); } @@ -1287,13 +1310,13 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* printer) { IncludeFile("net/proto2/public/unknown_field_set.h", printer); } - if (IsAnyMessage(file_)) { + if (IsAnyMessage(file_, options_)) { IncludeFile("net/proto2/internal/any.h", printer); } } void FileGenerator::GenerateMetadataPragma(io::Printer* printer, - const string& info_path) { + const std::string& info_path) { Formatter format(printer, variables_); if (!info_path.empty() && !options_.annotation_pragma_name.empty() && !options_.annotation_guard_name.empty()) { @@ -1310,7 +1333,7 @@ void FileGenerator::GenerateMetadataPragma(io::Printer* printer, void FileGenerator::GenerateDependencyIncludes(io::Printer* printer) { Formatter format(printer, variables_); for (int i = 0; i < file_->dependency_count(); i++) { - string basename = StripProto(file_->dependency(i)->name()); + std::string basename = StripProto(file_->dependency(i)->name()); // Do not import weak deps. if (IsDepWeak(file_->dependency(i))) continue; @@ -1327,8 +1350,8 @@ void FileGenerator::GenerateDependencyIncludes(io::Printer* printer) { void FileGenerator::GenerateGlobalStateFunctionDeclarations( io::Printer* printer) { Formatter format(printer, variables_); - // Forward-declare the AddDescriptors, InitDefaults because these are called - // by .pb.cc files depending on this file. + // Forward-declare the DescriptorTable because this is referenced by .pb.cc + // files depending on this file. // // The TableStruct is also outputted in weak_message_field.cc, because the // weak fields must refer to table struct but cannot include the header. @@ -1341,11 +1364,11 @@ void FileGenerator::GenerateGlobalStateFunctionDeclarations( // These tables describe how to serialize and parse messages. Used // for table driven code. " static const ::$proto_ns$::internal::ParseTableField entries[]\n" - " $GOOGLE_PROTOBUF$_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold);\n" + " PROTOBUF_SECTION_VARIABLE(protodesc_cold);\n" " static const ::$proto_ns$::internal::AuxillaryParseTableField aux[]\n" - " $GOOGLE_PROTOBUF$_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold);\n" + " PROTOBUF_SECTION_VARIABLE(protodesc_cold);\n" " static const ::$proto_ns$::internal::ParseTable schema[$1$]\n" - " $GOOGLE_PROTOBUF$_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold);\n" + " PROTOBUF_SECTION_VARIABLE(protodesc_cold);\n" " static const ::$proto_ns$::internal::FieldMetadata field_metadata[];\n" " static const ::$proto_ns$::internal::SerializationTable " "serialization_table[];\n" @@ -1353,12 +1376,9 @@ void FileGenerator::GenerateGlobalStateFunctionDeclarations( "};\n", std::max(size_t(1), message_generators_.size())); if (HasDescriptorMethods(file_, options_)) { - format("void $dllexport_decl $$add_descriptors$();\n"); - if (!options_.opensource_runtime) { - // TODO(gerbens) This is for proto1 interoperability. Remove when proto1 - // is gone. - format("void $dllexport_decl $$init_defaults$();\n"); - } + format( + "extern $dllexport_decl $const ::$proto_ns$::internal::DescriptorTable " + "$desc_table$;\n"); } } diff --git a/src/google/protobuf/compiler/cpp/cpp_file.h b/src/google/protobuf/compiler/cpp/cpp_file.h index 9dfdf50fd0..33734c5418 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.h +++ b/src/google/protobuf/compiler/cpp/cpp_file.h @@ -76,10 +76,10 @@ class FileGenerator { // info_path, if non-empty, should be the path (relative to printer's // output) to the metadata file describing this proto header. - void GenerateProtoHeader(io::Printer* printer, const string& info_path); + void GenerateProtoHeader(io::Printer* printer, const std::string& info_path); // info_path, if non-empty, should be the path (relative to printer's // output) to the metadata file describing this PB header. - void GeneratePBHeader(io::Printer* printer, const string& info_path); + void GeneratePBHeader(io::Printer* printer, const std::string& info_path); void GenerateSource(io::Printer* printer); int NumMessages() const { return message_generators_.size(); } @@ -90,21 +90,26 @@ class FileGenerator { private: // Internal type used by GenerateForwardDeclarations (defined in file.cc). class ForwardDeclarations; + struct CrossFileReferences; - void IncludeFile(const string& google3_name, io::Printer* printer) { + void IncludeFile(const std::string& google3_name, io::Printer* printer) { DoIncludeFile(google3_name, false, printer); } - void IncludeFileAndExport(const string& google3_name, io::Printer* printer) { + void IncludeFileAndExport(const std::string& google3_name, + io::Printer* printer) { DoIncludeFile(google3_name, true, printer); } - void DoIncludeFile(const string& google3_name, bool do_export, + void DoIncludeFile(const std::string& google3_name, bool do_export, io::Printer* printer); - string CreateHeaderInclude(const string& basename, - const FileDescriptor* file); - void GenerateInternalForwardDeclarations( - const std::vector& fields, const Options& options, - MessageSCCAnalyzer* scc_analyzer, io::Printer* printer); + std::string CreateHeaderInclude(const std::string& basename, + const FileDescriptor* file); + void GetCrossFileReferencesForField(const FieldDescriptor* field, + CrossFileReferences* refs); + void GetCrossFileReferencesForFile(const FileDescriptor* file, + CrossFileReferences* refs); + void GenerateInternalForwardDeclarations(const CrossFileReferences& refs, + io::Printer* printer); void GenerateSourceIncludes(io::Printer* printer); void GenerateSourceDefaultInstance(int idx, io::Printer* printer); @@ -116,10 +121,8 @@ class FileGenerator { void GenerateForwardDeclarations(io::Printer* printer); // Generates top or bottom of a header file. - void GenerateTopHeaderGuard(io::Printer* printer, - const string& filename_identifier); - void GenerateBottomHeaderGuard(io::Printer* printer, - const string& filename_identifier); + void GenerateTopHeaderGuard(io::Printer* printer, bool pb_h); + void GenerateBottomHeaderGuard(io::Printer* printer, bool pb_h); // Generates #include directives. void GenerateLibraryIncludes(io::Printer* printer); @@ -127,7 +130,8 @@ class FileGenerator { // Generate a pragma to pull in metadata using the given info_path (if // non-empty). info_path should be relative to printer's output. - void GenerateMetadataPragma(io::Printer* printer, const string& info_path); + void GenerateMetadataPragma(io::Printer* printer, + const std::string& info_path); // Generates a couple of different pieces before definitions: void GenerateGlobalStateFunctionDeclarations(io::Printer* printer); @@ -163,9 +167,7 @@ class FileGenerator { const Descriptor* GetSCCRepresentative(const Descriptor* d) { return GetSCC(d)->GetRepresentative(); } - const SCC* GetSCC(const Descriptor* d) { - return scc_analyzer_.GetSCC(d); - } + const SCC* GetSCC(const Descriptor* d) { return scc_analyzer_.GetSCC(d); } bool IsDepWeak(const FileDescriptor* dep) const { if (weak_deps_.count(dep) != 0) { @@ -176,13 +178,14 @@ class FileGenerator { } std::set weak_deps_; + std::vector sccs_; const FileDescriptor* file_; const Options options_; MessageSCCAnalyzer scc_analyzer_; - std::map variables_; + std::map variables_; // Contains the post-order walk of all the messages (and child messages) in // this file. If you need a pre-order walk just reverse iterate. diff --git a/src/google/protobuf/compiler/cpp/cpp_generator.cc b/src/google/protobuf/compiler/cpp/cpp_generator.cc index 79f773ee49..7de07652ba 100644 --- a/src/google/protobuf/compiler/cpp/cpp_generator.cc +++ b/src/google/protobuf/compiler/cpp/cpp_generator.cc @@ -34,9 +34,9 @@ #include -#include #include #include +#include #include #include @@ -46,6 +46,7 @@ #include + namespace google { namespace protobuf { namespace compiler { @@ -55,10 +56,10 @@ CppGenerator::CppGenerator() {} CppGenerator::~CppGenerator() {} bool CppGenerator::Generate(const FileDescriptor* file, - const string& parameter, + const std::string& parameter, GeneratorContext* generator_context, - string* error) const { - std::vector > options; + std::string* error) const { + std::vector > options; ParseGeneratorParameter(parameter, &options); // ----------------------------------------------------------------- @@ -78,19 +79,8 @@ bool CppGenerator::Generate(const FileDescriptor* file, // Options file_options; - switch (runtime_) { - case Runtime::kGoogle3: - file_options.opensource_runtime = false; - break; - case Runtime::kOpensource: - file_options.opensource_runtime = true; - file_options.opensource_include_paths = true; - break; - case Runtime::kOpensourceGoogle3: - file_options.opensource_runtime = true; - file_options.opensource_include_paths = false; - break; - } + file_options.opensource_runtime = opensource_runtime_; + file_options.runtime_include_base = runtime_include_base_; for (int i = 0; i < options.size(); i++) { if (options[i].first == "dllexport_decl") { @@ -103,13 +93,16 @@ bool CppGenerator::Generate(const FileDescriptor* file, file_options.annotation_pragma_name = options[i].second; } else if (options[i].first == "annotation_guard_name") { file_options.annotation_guard_name = options[i].second; + } else if (options[i].first == "speed") { + file_options.enforce_mode = EnforceOptimizeMode::kSpeed; } else if (options[i].first == "lite") { - file_options.enforce_lite = true; + file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime; } else if (options[i].first == "lite_implicit_weak_fields") { + file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime; file_options.lite_implicit_weak_fields = true; if (!options[i].second.empty()) { - file_options.num_cc_files = strto32(options[i].second.c_str(), - NULL, 10); + file_options.num_cc_files = + strto32(options[i].second.c_str(), NULL, 10); } } else if (options[i].first == "table_driven_parsing") { file_options.table_driven_parsing = true; @@ -132,7 +125,7 @@ bool CppGenerator::Generate(const FileDescriptor* file, // ----------------------------------------------------------------- - string basename = StripProto(file->name()); + std::string basename = StripProto(file->name()); if (MaybeBootstrap(file_options, generator_context, file_options.bootstrap, &basename)) { @@ -148,10 +141,10 @@ bool CppGenerator::Generate(const FileDescriptor* file, GeneratedCodeInfo annotations; io::AnnotationProtoCollector annotation_collector( &annotations); - string info_path = basename + ".proto.h.meta"; - io::Printer printer(output.get(), '$', file_options.annotate_headers - ? &annotation_collector - : NULL); + std::string info_path = basename + ".proto.h.meta"; + io::Printer printer( + output.get(), '$', + file_options.annotate_headers ? &annotation_collector : NULL); file_generator.GenerateProtoHeader( &printer, file_options.annotate_headers ? info_path : ""); if (file_options.annotate_headers) { @@ -167,10 +160,10 @@ bool CppGenerator::Generate(const FileDescriptor* file, GeneratedCodeInfo annotations; io::AnnotationProtoCollector annotation_collector( &annotations); - string info_path = basename + ".pb.h.meta"; - io::Printer printer(output.get(), '$', file_options.annotate_headers - ? &annotation_collector - : NULL); + std::string info_path = basename + ".pb.h.meta"; + io::Printer printer( + output.get(), '$', + file_options.annotate_headers ? &annotation_collector : NULL); file_generator.GeneratePBHeader( &printer, file_options.annotate_headers ? info_path : ""); if (file_options.annotate_headers) { @@ -183,7 +176,8 @@ bool CppGenerator::Generate(const FileDescriptor* file, // Generate cc file(s). if (UsingImplicitWeakFields(file, file_options)) { { - // This is the global .cc file, containing enum/services/tables/reflection + // This is the global .cc file, containing + // enum/services/tables/reflection std::unique_ptr output( generator_context->Open(basename + ".pb.cc")); io::Printer printer(output.get(), '$'); @@ -192,19 +186,18 @@ bool CppGenerator::Generate(const FileDescriptor* file, int num_cc_files = file_generator.NumMessages(); - // If we're using implicit weak fields then we allow the user to optionally - // specify how many files to generate, not counting the global pb.cc file. - // If we have more files than messages, then some files will be generated as - // empty placeholders. + // If we're using implicit weak fields then we allow the user to + // optionally specify how many files to generate, not counting the global + // pb.cc file. If we have more files than messages, then some files will + // be generated as empty placeholders. if (file_options.num_cc_files > 0) { GOOGLE_CHECK_LE(file_generator.NumMessages(), file_options.num_cc_files) << "There must be at least as many numbered .cc files as messages."; num_cc_files = file_options.num_cc_files; } for (int i = 0; i < num_cc_files; i++) { - // TODO(gerbens) Agree on naming scheme. - std::unique_ptr output(generator_context->Open( - basename + ".out/" + SimpleItoa(i) + ".cc")); + std::unique_ptr output( + generator_context->Open(StrCat(basename, ".out/", i, ".cc"))); io::Printer printer(output.get(), '$'); if (i < file_generator.NumMessages()) { file_generator.GenerateSourceForMessage(i, &printer); diff --git a/src/google/protobuf/compiler/cpp/cpp_generator.h b/src/google/protobuf/compiler/cpp/cpp_generator.h index 06d3c36f4f..dafc6e60e0 100644 --- a/src/google/protobuf/compiler/cpp/cpp_generator.h +++ b/src/google/protobuf/compiler/cpp/cpp_generator.h @@ -40,6 +40,8 @@ #include #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -49,12 +51,12 @@ namespace cpp { // header. If you create your own protocol compiler binary and you want // it to support C++ output, you can do so by registering an instance of this // CodeGenerator with the CommandLineInterface in your main() function. -class LIBPROTOC_EXPORT CppGenerator : public CodeGenerator { +class PROTOC_EXPORT CppGenerator : public CodeGenerator { public: CppGenerator(); ~CppGenerator(); - enum class LIBPROTOC_EXPORT Runtime { + enum class Runtime { kGoogle3, // Use the internal google3 runtime. kOpensource, // Use the open-source runtime. @@ -64,18 +66,26 @@ class LIBPROTOC_EXPORT CppGenerator : public CodeGenerator { kOpensourceGoogle3 }; - void set_runtime(Runtime runtime) { - runtime_ = runtime; + void set_opensource_runtime(bool opensource) { + opensource_runtime_ = opensource; + } + + // If set to a non-empty string, generated code will do: + // #include "/google/protobuf/message.h" + // instead of: + // #include + // This has no effect if opensource_runtime = false. + void set_runtime_include_base(const std::string& base) { + runtime_include_base_ = base; } // implements CodeGenerator ---------------------------------------- - bool Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* generator_context, - string* error) const; + bool Generate(const FileDescriptor* file, const std::string& parameter, + GeneratorContext* generator_context, std::string* error) const; private: - Runtime runtime_ = Runtime::kOpensource; + bool opensource_runtime_ = true; + std::string runtime_include_base_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CppGenerator); }; @@ -84,4 +94,6 @@ class LIBPROTOC_EXPORT CppGenerator : public CodeGenerator { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_CPP_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.cc b/src/google/protobuf/compiler/cpp/cpp_helpers.cc index bc0a9264ad..624bbd37a2 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.cc +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.cc @@ -32,15 +32,19 @@ // 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 @@ -52,6 +56,7 @@ #include +#include namespace google { namespace protobuf { @@ -62,54 +67,105 @@ namespace { static const char kAnyMessageName[] = "Any"; static const char kAnyProtoFile[] = "google/protobuf/any.proto"; -static const char kGoogleProtobufPrefix[] = "google/protobuf/"; -string DotsToUnderscores(const string& name) { - return StringReplace(name, ".", "_", true); -} - -string DotsToColons(const string& name) { +std::string DotsToColons(const std::string& name) { return StringReplace(name, ".", "::", true); } -const char* const kKeywordList[] = { - "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor", - "bool", "break", "case", "catch", "char", "class", "compl", "const", - "constexpr", "const_cast", "continue", "decltype", "default", "delete", "do", - "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", - "false", "float", "for", "friend", "goto", "if", "inline", "int", "long", - "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", - "operator", "or", "or_eq", "private", "protected", "public", "register", - "reinterpret_cast", "return", "short", "signed", "sizeof", "static", - "static_assert", "static_cast", "struct", "switch", "template", "this", - "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", - "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", - "while", "xor", "xor_eq" -}; - -std::unordered_set MakeKeywordsMap() { - std::unordered_set result; - for (int i = 0; i < GOOGLE_ARRAYSIZE(kKeywordList); i++) { - result.insert(kKeywordList[i]); +static const char* const kKeywordList[] = { // + "NULL", + "alignas", + "alignof", + "and", + "and_eq", + "asm", + "auto", + "bitand", + "bitor", + "bool", + "break", + "case", + "catch", + "char", + "class", + "compl", + "const", + "constexpr", + "const_cast", + "continue", + "decltype", + "default", + "delete", + "do", + "double", + "dynamic_cast", + "else", + "enum", + "explicit", + "export", + "extern", + "false", + "float", + "for", + "friend", + "goto", + "if", + "inline", + "int", + "long", + "mutable", + "namespace", + "new", + "noexcept", + "not", + "not_eq", + "nullptr", + "operator", + "or", + "or_eq", + "private", + "protected", + "public", + "register", + "reinterpret_cast", + "return", + "short", + "signed", + "sizeof", + "static", + "static_assert", + "static_cast", + "struct", + "switch", + "template", + "this", + "thread_local", + "throw", + "true", + "try", + "typedef", + "typeid", + "typename", + "union", + "unsigned", + "using", + "virtual", + "void", + "volatile", + "wchar_t", + "while", + "xor", + "xor_eq"}; + +static std::unordered_set* MakeKeywordsMap() { + auto* result = new std::unordered_set(); + for (const auto keyword : kKeywordList) { + result->emplace(keyword); } return result; } -std::unordered_set kKeywords = MakeKeywordsMap(); - -// Returns whether the provided descriptor has an extension. This includes its -// nested types. -bool HasExtension(const Descriptor* descriptor) { - if (descriptor->extension_count() > 0) { - return true; - } - for (int i = 0; i < descriptor->nested_type_count(); ++i) { - if (HasExtension(descriptor->nested_type(i))) { - return true; - } - } - return false; -} +static std::unordered_set& kKeywords = *MakeKeywordsMap(); // Encode [0..63] as 'A'-'Z', 'a'-'z', '0'-'9', '_' char Base63Char(int value) { @@ -126,8 +182,8 @@ char Base63Char(int value) { // Given a c identifier has 63 legal characters we can't implement base64 // encoding. So we return the k least significant "digits" in base 63. template -string Base63(I n, int k) { - string res; +std::string Base63(I n, int k) { + std::string res; while (k-- > 0) { res += Base63Char(static_cast(n % 63)); n /= 63; @@ -135,27 +191,23 @@ string Base63(I n, int k) { return res; } -string IntTypeName(const Options& options, const string& type) { +std::string IntTypeName(const Options& options, const std::string& type) { if (options.opensource_runtime) { - return "::google::protobuf::" + type; + return "::PROTOBUF_NAMESPACE_ID::" + type; } else { return "::" + type; } } -string StringTypeName(const Options& options) { - return options.opensource_runtime ? "::std::string" : "::std::string"; -} - -void SetIntVar(const Options& options, const string& type, - std::map* variables) { +void SetIntVar(const Options& options, const std::string& type, + std::map* variables) { (*variables)[type] = IntTypeName(options, type); } } // namespace void SetCommonVars(const Options& options, - std::map* variables) { + std::map* variables) { (*variables)["proto_ns"] = ProtobufNamespace(options); // Warning: there is some clever naming/splitting here to avoid extract script @@ -171,21 +223,29 @@ void SetCommonVars(const Options& options, // split them. It might not strictly matter since we don't generate google3 // code in open-source. But it's good to prevent surprising things from // happening. - (*variables)["GOOGLE_PROTOBUF"] = "GOOGLE3" "_PROTOBUF"; - (*variables)["CHK"] = "CH" "ECK"; - (*variables)["DCHK"] = "DCH" "ECK"; - } - + (*variables)["GOOGLE_PROTOBUF"] = + "GOOGLE3" + "_PROTOBUF"; + (*variables)["CHK"] = + "CH" + "ECK"; + (*variables)["DCHK"] = + "DCH" + "ECK"; + } + + SetIntVar(options, "int8", variables); SetIntVar(options, "uint8", variables); SetIntVar(options, "uint32", variables); SetIntVar(options, "uint64", variables); SetIntVar(options, "int32", variables); SetIntVar(options, "int64", variables); - (*variables)["string"] = StringTypeName(options); + (*variables)["string"] = "std::string"; } -string UnderscoresToCamelCase(const string& input, bool cap_next_letter) { - string result; +std::string UnderscoresToCamelCase(const std::string& input, + bool cap_next_letter) { + std::string result; // Note: I distrust ctype.h due to locales. for (int i = 0; i < input.size(); i++) { if ('a' <= input[i] && input[i] <= 'z') { @@ -210,9 +270,9 @@ string UnderscoresToCamelCase(const string& input, bool cap_next_letter) { } const char kThickSeparator[] = - "// ===================================================================\n"; + "// ===================================================================\n"; const char kThinSeparator[] = - "// -------------------------------------------------------------------\n"; + "// -------------------------------------------------------------------\n"; bool CanInitializeByZeroing(const FieldDescriptor* field) { if (field->is_repeated() || field->is_extension()) return false; @@ -238,61 +298,117 @@ bool CanInitializeByZeroing(const FieldDescriptor* field) { } } -string ClassName(const Descriptor* descriptor) { +std::string ClassName(const Descriptor* descriptor) { const Descriptor* parent = descriptor->containing_type(); - string res; + std::string res; if (parent) res += ClassName(parent) + "_"; res += descriptor->name(); if (IsMapEntryMessage(descriptor)) res += "_DoNotUse"; - return res; + return ResolveKeyword(res); } -string ClassName(const EnumDescriptor* enum_descriptor) { - if (enum_descriptor->containing_type() == NULL) { - return enum_descriptor->name(); +std::string ClassName(const EnumDescriptor* enum_descriptor) { + if (enum_descriptor->containing_type() == nullptr) { + return ResolveKeyword(enum_descriptor->name()); } else { return ClassName(enum_descriptor->containing_type()) + "_" + enum_descriptor->name(); } } -string QualifiedClassName(const Descriptor* d) { - return Namespace(d) + "::" + ClassName(d); +std::string QualifiedClassName(const Descriptor* d, const Options& options) { + return QualifiedFileLevelSymbol(d->file(), ClassName(d), options); +} + +std::string QualifiedClassName(const EnumDescriptor* d, + const Options& options) { + return QualifiedFileLevelSymbol(d->file(), ClassName(d), options); } -string QualifiedClassName(const EnumDescriptor* d) { - return Namespace(d) + "::" + ClassName(d); +std::string QualifiedClassName(const Descriptor* d) { + return QualifiedClassName(d, Options()); } -string Namespace(const string& package) { +std::string QualifiedClassName(const EnumDescriptor* d) { + return QualifiedClassName(d, Options()); +} + +std::string Namespace(const std::string& package) { if (package.empty()) return ""; return "::" + DotsToColons(package); } -string Namespace(const Descriptor* d) { return Namespace(d->file()); } +std::string Namespace(const FileDescriptor* d, const Options& options) { + std::string ret = Namespace(d->package()); + if (IsWellKnownMessage(d) && options.opensource_runtime) { + // Written with string concatenation to prevent rewriting of + // ::google::protobuf. + ret = StringReplace(ret, + "::google::" + "protobuf", + "PROTOBUF_NAMESPACE_ID", false); + } + return ret; +} + +std::string Namespace(const Descriptor* d, const Options& options) { + return Namespace(d->file(), options); +} -string Namespace(const FieldDescriptor* d) { return Namespace(d->file()); } +std::string Namespace(const FieldDescriptor* d, const Options& options) { + return Namespace(d->file(), options); +} + +std::string Namespace(const EnumDescriptor* d, const Options& options) { + return Namespace(d->file(), options); +} + +std::string DefaultInstanceType(const Descriptor* descriptor, + const Options& options) { + return ClassName(descriptor) + "DefaultTypeInternal"; +} + +std::string DefaultInstanceName(const Descriptor* descriptor, + const Options& options) { + return "_" + ClassName(descriptor, false) + "_default_instance_"; +} + +std::string QualifiedDefaultInstanceName(const Descriptor* descriptor, + const Options& options) { + return QualifiedFileLevelSymbol( + descriptor->file(), DefaultInstanceName(descriptor, options), options); +} -string Namespace(const EnumDescriptor* d) { return Namespace(d->file()); } +std::string DescriptorTableName(const FileDescriptor* file, + const Options& options) { + return UniqueName("descriptor_table", file, options); +} -string DefaultInstanceName(const Descriptor* descriptor) { - string prefix = descriptor->file()->package().empty() ? "" : "::"; - return prefix + DotsToColons(descriptor->file()->package()) + "::_" + - ClassName(descriptor, false) + "_default_instance_"; +std::string FileDllExport(const FileDescriptor* file, const Options& options) { + return UniqueName("PROTOBUF_INTERNAL_EXPORT", file, options); } -string ReferenceFunctionName(const Descriptor* descriptor) { - return QualifiedClassName(descriptor) + "_ReferenceStrong"; +std::string ReferenceFunctionName(const Descriptor* descriptor, + const Options& options) { + return QualifiedClassName(descriptor, options) + "_ReferenceStrong"; } -string SuperClassName(const Descriptor* descriptor, const Options& options) { +std::string SuperClassName(const Descriptor* descriptor, + const Options& options) { return "::" + ProtobufNamespace(options) + (HasDescriptorMethods(descriptor->file(), options) ? "::Message" : "::MessageLite"); } -string FieldName(const FieldDescriptor* field) { - string result = field->name(); +std::string ResolveKeyword(const string& name) { + if (kKeywords.count(name) > 0) { + return name + "_"; + } + return name; +} + +std::string FieldName(const FieldDescriptor* field) { + std::string result = field->name(); LowerString(&result); if (kKeywords.count(result) > 0) { result.append("_"); @@ -300,8 +416,8 @@ string FieldName(const FieldDescriptor* field) { return result; } -string EnumValueName(const EnumValueDescriptor* enum_value) { - string result = enum_value->name(); +std::string EnumValueName(const EnumValueDescriptor* enum_value) { + std::string result = enum_value->name(); if (kKeywords.count(result) > 0) { result.append("_"); } @@ -309,7 +425,7 @@ string EnumValueName(const EnumValueDescriptor* enum_value) { } int EstimateAlignmentSize(const FieldDescriptor* field) { - if (field == NULL) return 0; + if (field == nullptr) return 0; if (field->is_repeated()) return 8; switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_BOOL: @@ -332,29 +448,30 @@ int EstimateAlignmentSize(const FieldDescriptor* field) { return -1; // Make compiler happy. } -string FieldConstantName(const FieldDescriptor *field) { - string field_name = UnderscoresToCamelCase(field->name(), true); - string result = "k" + field_name + "FieldNumber"; +std::string FieldConstantName(const FieldDescriptor* field) { + std::string field_name = UnderscoresToCamelCase(field->name(), true); + std::string result = "k" + field_name + "FieldNumber"; if (!field->is_extension() && field->containing_type()->FindFieldByCamelcaseName( - field->camelcase_name()) != field) { + field->camelcase_name()) != field) { // This field's camelcase name is not unique. As a hack, add the field // number to the constant name. This makes the constant rather useless, // but what can we do? - result += "_" + SimpleItoa(field->number()); + result += "_" + StrCat(field->number()); } return result; } -string FieldMessageTypeName(const FieldDescriptor* field) { +std::string FieldMessageTypeName(const FieldDescriptor* field, + const Options& options) { // Note: The Google-internal version of Protocol Buffers uses this function // as a hook point for hacks to support legacy code. - return ClassName(field->message_type(), true); + return QualifiedClassName(field->message_type(), options); } -string StripProto(const string& filename) { +std::string StripProto(const std::string& filename) { if (HasSuffixString(filename, ".protodevel")) { return StripSuffixString(filename, ".protodevel"); } else { @@ -364,27 +481,37 @@ string StripProto(const string& filename) { const char* PrimitiveTypeName(FieldDescriptor::CppType type) { switch (type) { - case FieldDescriptor::CPPTYPE_INT32 : return "::google::protobuf::int32"; - case FieldDescriptor::CPPTYPE_INT64 : return "::google::protobuf::int64"; - case FieldDescriptor::CPPTYPE_UINT32 : return "::google::protobuf::uint32"; - case FieldDescriptor::CPPTYPE_UINT64 : return "::google::protobuf::uint64"; - case FieldDescriptor::CPPTYPE_DOUBLE : return "double"; - case FieldDescriptor::CPPTYPE_FLOAT : return "float"; - case FieldDescriptor::CPPTYPE_BOOL : return "bool"; - case FieldDescriptor::CPPTYPE_ENUM : return "int"; - case FieldDescriptor::CPPTYPE_STRING : return "::std::string"; - case FieldDescriptor::CPPTYPE_MESSAGE: return NULL; + case FieldDescriptor::CPPTYPE_INT32: + return "::google::protobuf::int32"; + case FieldDescriptor::CPPTYPE_INT64: + return "::google::protobuf::int64"; + case FieldDescriptor::CPPTYPE_UINT32: + return "::google::protobuf::uint32"; + case FieldDescriptor::CPPTYPE_UINT64: + return "::google::protobuf::uint64"; + case FieldDescriptor::CPPTYPE_DOUBLE: + return "double"; + case FieldDescriptor::CPPTYPE_FLOAT: + return "float"; + case FieldDescriptor::CPPTYPE_BOOL: + return "bool"; + case FieldDescriptor::CPPTYPE_ENUM: + return "int"; + case FieldDescriptor::CPPTYPE_STRING: + return "std::string"; + case FieldDescriptor::CPPTYPE_MESSAGE: + return nullptr; - // No default because we want the compiler to complain if any new - // CppTypes are added. + // No default because we want the compiler to complain if any new + // CppTypes are added. } GOOGLE_LOG(FATAL) << "Can't get here."; - return NULL; + return nullptr; } -string PrimitiveTypeName(const Options& options, - FieldDescriptor::CppType type) { +std::string PrimitiveTypeName(const Options& options, + FieldDescriptor::CppType type) { switch (type) { case FieldDescriptor::CPPTYPE_INT32: return IntTypeName(options, "int32"); @@ -403,7 +530,7 @@ string PrimitiveTypeName(const Options& options, case FieldDescriptor::CPPTYPE_ENUM: return "int"; case FieldDescriptor::CPPTYPE_STRING: - return StringTypeName(options); + return "std::string"; case FieldDescriptor::CPPTYPE_MESSAGE: return ""; @@ -417,59 +544,76 @@ string PrimitiveTypeName(const Options& options, const char* DeclaredTypeMethodName(FieldDescriptor::Type type) { switch (type) { - case FieldDescriptor::TYPE_INT32 : return "Int32"; - case FieldDescriptor::TYPE_INT64 : return "Int64"; - case FieldDescriptor::TYPE_UINT32 : return "UInt32"; - case FieldDescriptor::TYPE_UINT64 : return "UInt64"; - case FieldDescriptor::TYPE_SINT32 : return "SInt32"; - case FieldDescriptor::TYPE_SINT64 : return "SInt64"; - case FieldDescriptor::TYPE_FIXED32 : return "Fixed32"; - case FieldDescriptor::TYPE_FIXED64 : return "Fixed64"; - case FieldDescriptor::TYPE_SFIXED32: return "SFixed32"; - case FieldDescriptor::TYPE_SFIXED64: return "SFixed64"; - case FieldDescriptor::TYPE_FLOAT : return "Float"; - case FieldDescriptor::TYPE_DOUBLE : return "Double"; - - case FieldDescriptor::TYPE_BOOL : return "Bool"; - case FieldDescriptor::TYPE_ENUM : return "Enum"; - - case FieldDescriptor::TYPE_STRING : return "String"; - case FieldDescriptor::TYPE_BYTES : return "Bytes"; - case FieldDescriptor::TYPE_GROUP : return "Group"; - case FieldDescriptor::TYPE_MESSAGE : return "Message"; - - // No default because we want the compiler to complain if any new - // types are added. + case FieldDescriptor::TYPE_INT32: + return "Int32"; + case FieldDescriptor::TYPE_INT64: + return "Int64"; + case FieldDescriptor::TYPE_UINT32: + return "UInt32"; + case FieldDescriptor::TYPE_UINT64: + return "UInt64"; + case FieldDescriptor::TYPE_SINT32: + return "SInt32"; + case FieldDescriptor::TYPE_SINT64: + return "SInt64"; + case FieldDescriptor::TYPE_FIXED32: + return "Fixed32"; + case FieldDescriptor::TYPE_FIXED64: + return "Fixed64"; + case FieldDescriptor::TYPE_SFIXED32: + return "SFixed32"; + case FieldDescriptor::TYPE_SFIXED64: + return "SFixed64"; + case FieldDescriptor::TYPE_FLOAT: + return "Float"; + case FieldDescriptor::TYPE_DOUBLE: + return "Double"; + + case FieldDescriptor::TYPE_BOOL: + return "Bool"; + case FieldDescriptor::TYPE_ENUM: + return "Enum"; + + case FieldDescriptor::TYPE_STRING: + return "String"; + case FieldDescriptor::TYPE_BYTES: + return "Bytes"; + case FieldDescriptor::TYPE_GROUP: + return "Group"; + case FieldDescriptor::TYPE_MESSAGE: + return "Message"; + + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; return ""; } -string Int32ToString(int number) { +std::string Int32ToString(int number) { if (number == kint32min) { // This needs to be special-cased, see explanation here: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661 - return SimpleItoa(number + 1) + " - 1"; + return StrCat(number + 1, " - 1"); } else { - return SimpleItoa(number); + return StrCat(number); } } -string Int64ToString(const string& macro_prefix, int64 number) { +std::string Int64ToString(const std::string& macro_prefix, int64 number) { if (number == kint64min) { // This needs to be special-cased, see explanation here: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661 - return macro_prefix + "_LONGLONG(" + SimpleItoa(number + 1) + - ") - 1"; + return StrCat(macro_prefix, "_LONGLONG(", number + 1, ") - 1"); } - return macro_prefix + "_LONGLONG(" + SimpleItoa(number) + ")"; + return StrCat(macro_prefix, "_LONGLONG(", number, ")"); } -string UInt64ToString(const string& macro_prefix, uint64 number) { - return macro_prefix + "_ULONGLONG(" + SimpleItoa(number) + ")"; +std::string UInt64ToString(const std::string& macro_prefix, uint64 number) { + return StrCat(macro_prefix, "_ULONGLONG(", number, ")"); } -string DefaultValue(const FieldDescriptor* field) { +std::string DefaultValue(const FieldDescriptor* field) { switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_INT64: return Int64ToString("GG", field->default_value_int64()); @@ -480,17 +624,16 @@ string DefaultValue(const FieldDescriptor* field) { } } -string DefaultValue(const Options& options, const FieldDescriptor* field) { +std::string DefaultValue(const Options& options, const FieldDescriptor* field) { switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: return Int32ToString(field->default_value_int32()); case FieldDescriptor::CPPTYPE_UINT32: - return SimpleItoa(field->default_value_uint32()) + "u"; + return StrCat(field->default_value_uint32()) + "u"; case FieldDescriptor::CPPTYPE_INT64: - return Int64ToString(MacroPrefix(options), field->default_value_int64()); + return Int64ToString("PROTOBUF", field->default_value_int64()); case FieldDescriptor::CPPTYPE_UINT64: - return UInt64ToString(MacroPrefix(options), - field->default_value_uint64()); + return UInt64ToString("PROTOBUF", field->default_value_uint64()); case FieldDescriptor::CPPTYPE_DOUBLE: { double value = field->default_value_double(); if (value == std::numeric_limits::infinity()) { @@ -503,41 +646,39 @@ string DefaultValue(const Options& options, const FieldDescriptor* field) { return SimpleDtoa(value); } } - case FieldDescriptor::CPPTYPE_FLOAT: - { - float value = field->default_value_float(); - if (value == std::numeric_limits::infinity()) { - return "std::numeric_limits::infinity()"; - } else if (value == -std::numeric_limits::infinity()) { - return "-std::numeric_limits::infinity()"; - } else if (value != value) { - return "std::numeric_limits::quiet_NaN()"; - } else { - string float_value = SimpleFtoa(value); - // If floating point value contains a period (.) or an exponent - // (either E or e), then append suffix 'f' to make it a float - // literal. - if (float_value.find_first_of(".eE") != string::npos) { - float_value.push_back('f'); - } - return float_value; + case FieldDescriptor::CPPTYPE_FLOAT: { + float value = field->default_value_float(); + if (value == std::numeric_limits::infinity()) { + return "std::numeric_limits::infinity()"; + } else if (value == -std::numeric_limits::infinity()) { + return "-std::numeric_limits::infinity()"; + } else if (value != value) { + return "std::numeric_limits::quiet_NaN()"; + } else { + std::string float_value = SimpleFtoa(value); + // If floating point value contains a period (.) or an exponent + // (either E or e), then append suffix 'f' to make it a float + // literal. + if (float_value.find_first_of(".eE") != string::npos) { + float_value.push_back('f'); } + return float_value; } + } case FieldDescriptor::CPPTYPE_BOOL: return field->default_value_bool() ? "true" : "false"; case FieldDescriptor::CPPTYPE_ENUM: // Lazy: Generate a static_cast because we don't have a helper function // that constructs the full name of an enum value. return strings::Substitute( - "static_cast< $0 >($1)", - ClassName(field->enum_type(), true), + "static_cast< $0 >($1)", ClassName(field->enum_type(), true), Int32ToString(field->default_value_enum()->number())); case FieldDescriptor::CPPTYPE_STRING: - return "\"" + EscapeTrigraphs( - CEscape(field->default_value_string())) + - "\""; + return "\"" + + EscapeTrigraphs(CEscape(field->default_value_string())) + + "\""; case FieldDescriptor::CPPTYPE_MESSAGE: - return "*" + FieldMessageTypeName(field) + + return "*" + FieldMessageTypeName(field, options) + "::internal_default_instance()"; } // Can't actually get here; make compiler happy. (We could add a default @@ -548,8 +689,8 @@ string DefaultValue(const Options& options, const FieldDescriptor* field) { } // Convert a file name into a valid identifier. -string FilenameIdentifier(const string& filename) { - string result; +std::string FilenameIdentifier(const std::string& filename) { + std::string result; for (int i = 0; i < filename.size(); i++) { if (ascii_isalnum(filename[i])) { result.push_back(filename[i]); @@ -562,32 +703,34 @@ string FilenameIdentifier(const string& filename) { return result; } -string UniqueName(const string& name, const string& filename, +string UniqueName(const std::string& name, const std::string& filename, const Options& options) { return name + "_" + FilenameIdentifier(filename); } // Return the qualified C++ name for a file level symbol. -string QualifiedFileLevelSymbol(const string& package, const string& name) { - if (package.empty()) { +std::string QualifiedFileLevelSymbol(const FileDescriptor* file, + const std::string& name, + const Options& options) { + if (file->package().empty()) { return StrCat("::", name); } - return StrCat("::", DotsToColons(package), "::", name); + return StrCat(Namespace(file, options), "::", name); } // Escape C++ trigraphs by escaping question marks to \? -string EscapeTrigraphs(const string& to_escape) { +std::string EscapeTrigraphs(const std::string& to_escape) { return StringReplace(to_escape, "?", "\\?", true); } // Escaped function name to eliminate naming conflict. -string SafeFunctionName(const Descriptor* descriptor, - const FieldDescriptor* field, - const string& prefix) { +std::string SafeFunctionName(const Descriptor* descriptor, + const FieldDescriptor* field, + const std::string& prefix) { // Do not use FieldName() since it will escape keywords. - string name = field->name(); + std::string name = field->name(); LowerString(&name); - string function_name = prefix + name; + std::string function_name = prefix + name; if (descriptor->FindFieldByName(function_name)) { // Single underscore will also make it conflicting with the private data // member. We use double underscore to escape function names. @@ -600,6 +743,25 @@ string SafeFunctionName(const Descriptor* descriptor, return function_name; } +bool IsStringInlined(const FieldDescriptor* descriptor, + const Options& options) { + if (options.opensource_runtime) return false; + + // TODO(ckennelly): Handle inlining for any.proto. + if (IsAnyMessage(descriptor->containing_type(), options)) return false; + if (descriptor->containing_type()->options().map_entry()) return false; + + // Limit to proto2, as we rely on has bits to distinguish field presence for + // release_$name$. On proto3, we cannot use the address of the string + // instance when the field has been inlined. + if (!HasFieldPresence(descriptor->file())) return false; + + if (options.access_info_map) { + if (descriptor->is_required()) return true; + } + return false; +} + static bool HasLazyFields(const Descriptor* descriptor, const Options& options) { for (int field_idx = 0; field_idx < descriptor->field_count(); field_idx++) { @@ -788,23 +950,37 @@ FieldOptions::CType EffectiveStringCType(const FieldDescriptor* field, } } -bool IsAnyMessage(const FileDescriptor* descriptor) { +bool IsAnyMessage(const FileDescriptor* descriptor, const Options& options) { return descriptor->name() == kAnyProtoFile; } -bool IsAnyMessage(const Descriptor* descriptor) { +bool IsAnyMessage(const Descriptor* descriptor, const Options& options) { return descriptor->name() == kAnyMessageName && - descriptor->file()->name() == kAnyProtoFile; -} - -bool IsWellKnownMessage(const FileDescriptor* descriptor) { - return !descriptor->name().compare(0, 16, kGoogleProtobufPrefix); + IsAnyMessage(descriptor->file(), options); +} + +bool IsWellKnownMessage(const FileDescriptor* file) { + static const std::unordered_set well_known_files{ + "google/protobuf/any.proto", + "google/protobuf/api.proto", + "google/protobuf/compiler/plugin.proto", + "google/protobuf/descriptor.proto", + "google/protobuf/duration.proto", + "google/protobuf/empty.proto", + "google/protobuf/field_mask.proto", + "google/protobuf/source_context.proto", + "google/protobuf/struct.proto", + "google/protobuf/timestamp.proto", + "google/protobuf/type.proto", + "google/protobuf/wrappers.proto", + }; + return well_known_files.find(file->name()) != well_known_files.end(); } enum Utf8CheckMode { STRICT = 0, // Parsing will fail if non UTF-8 data is in string fields. VERIFY = 1, // Only log an error but parsing will succeed. - NONE = 2, // No UTF-8 check. + NONE = 2, // No UTF-8 check. }; static bool FieldEnforceUtf8(const FieldDescriptor* field, @@ -832,7 +1008,8 @@ static Utf8CheckMode GetUtf8CheckMode(const FieldDescriptor* field, } } -string GetUtf8Suffix(const FieldDescriptor* field, const Options& options) { +std::string GetUtf8Suffix(const FieldDescriptor* field, + const Options& options) { switch (GetUtf8CheckMode(field, options)) { case STRICT: return "UTF8"; @@ -949,8 +1126,10 @@ bool IsImplicitWeakField(const FieldDescriptor* field, const Options& options, return UsingImplicitWeakFields(field->file(), options) && field->type() == FieldDescriptor::TYPE_MESSAGE && !field->is_required() && !field->is_map() && - field->containing_oneof() == NULL && + field->containing_oneof() == nullptr && !IsWellKnownMessage(field->message_type()->file()) && + field->message_type()->file()->name() != + "net/proto2/proto/descriptor.proto" && // We do not support implicit weak fields between messages in the same // strongly-connected component. scc_analyzer->GetSCC(field->containing_type()) != @@ -1056,13 +1235,13 @@ void ListAllTypesForServices(const FileDescriptor* fd, } } -bool GetBootstrapBasename(const Options& options, const string& basename, - string* bootstrap_basename) { +bool GetBootstrapBasename(const Options& options, const std::string& basename, + std::string* bootstrap_basename) { if (options.opensource_runtime) { return false; } - std::unordered_map bootstrap_mapping{ + std::unordered_map bootstrap_mapping{ {"net/proto2/proto/descriptor", "net/proto2/internal/descriptor"}, {"net/proto2/compiler/proto/plugin", @@ -1081,13 +1260,13 @@ bool GetBootstrapBasename(const Options& options, const string& basename, } bool IsBootstrapProto(const Options& options, const FileDescriptor* file) { - string my_name = StripProto(file->name()); + std::string my_name = StripProto(file->name()); return GetBootstrapBasename(options, my_name, &my_name); } bool MaybeBootstrap(const Options& options, GeneratorContext* generator_context, - bool bootstrap_flag, string* basename) { - string bootstrap_basename; + bool bootstrap_flag, std::string* basename) { + std::string bootstrap_basename; if (!GetBootstrapBasename(options, *basename, &bootstrap_basename)) { return false; } @@ -1097,7 +1276,7 @@ bool MaybeBootstrap(const Options& options, GeneratorContext* generator_context, *basename = bootstrap_basename; return false; } else { - string forward_to_basename = bootstrap_basename; + std::string forward_to_basename = bootstrap_basename; // Generate forwarding headers and empty .pb.cc. { @@ -1109,8 +1288,8 @@ bool MaybeBootstrap(const Options& options, GeneratorContext* generator_context, "#define PROTOBUF_INCLUDED_$filename_identifier$_FORWARD_PB_H\n" "#include \"$forward_to_basename$.pb.h\" // IWYU pragma: export\n" "#endif // PROTOBUF_INCLUDED_$filename_identifier$_FORWARD_PB_H\n", - "forward_to_basename", forward_to_basename, - "filename_identifier", FilenameIdentifier(*basename)); + "forward_to_basename", forward_to_basename, "filename_identifier", + FilenameIdentifier(*basename)); if (!options.opensource_runtime) { // HACK HACK HACK, tech debt from the deeps of proto1 and SWIG @@ -1136,8 +1315,8 @@ bool MaybeBootstrap(const Options& options, GeneratorContext* generator_context, "export\n" "#endif // " "PROTOBUF_INCLUDED_$filename_identifier$_FORWARD_PROTO_H\n", - "forward_to_basename", forward_to_basename, - "filename_identifier", FilenameIdentifier(*basename)); + "forward_to_basename", forward_to_basename, "filename_identifier", + FilenameIdentifier(*basename)); } { @@ -1162,436 +1341,500 @@ bool MaybeBootstrap(const Options& options, GeneratorContext* generator_context, } } -bool ShouldRepeat(const FieldDescriptor* descriptor, - internal::WireFormatLite::WireType wiretype) { - return descriptor->is_repeated() && - (!descriptor->is_packable() || - wiretype != internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED); -} - -void GenerateCaseBody(internal::WireFormatLite::WireType wiretype, - const FieldDescriptor* field, const Options& options, - MessageSCCAnalyzer* scc_analyzer, - const Formatter& format) { - using internal::WireFormat; - using internal::WireFormatLite; - - if (ShouldRepeat(field, wiretype)) { - format("do {\n"); - format.Indent(); - } - switch (wiretype) { - case WireFormatLite::WIRETYPE_VARINT: { - format( - "$uint64$ val;\n" - "ptr = Varint::Parse64(ptr, &val);\n" - "if (!ptr) goto error;\n"); - string type = PrimitiveTypeName(options, field->cpp_type()); - if (field->type() == FieldDescriptor::TYPE_SINT32 || - field->type() == FieldDescriptor::TYPE_SINT64) { - int size = EstimateAlignmentSize(field) * 8; - format( - "$1$ value = " - "::$proto_ns$::internal::WireFormatLite::ZigZagDecode$2$(val);\n", - type, size); - } else if (field->type() == FieldDescriptor::TYPE_ENUM && - !IsProto1(field->file(), options)) { - if (!HasPreservingUnknownEnumSemantics(field->file())) { - format( - "if (!$1$_IsValid(val)) {\n" - " ::$proto_ns$::internal::WriteVarint($2$, val, " - "msg->mutable_unknown_fields());\n" - " break;\n" - "}\n", - QualifiedClassName(field->enum_type()), field->number()); +class ParseLoopGenerator { + public: + ParseLoopGenerator(int num_hasbits, const Options& options, + MessageSCCAnalyzer* scc_analyzer, io::Printer* printer) + : scc_analyzer_(scc_analyzer), + options_(options), + format_(printer), + num_hasbits_(num_hasbits) {} + + void GenerateParserLoop(const Descriptor* descriptor) { + format_.Set("classname", ClassName(descriptor)); + format_.Set("p_ns", "::" + ProtobufNamespace(options_)); + format_.Set("pi_ns", + StrCat("::", ProtobufNamespace(options_), "::internal")); + format_.Set("GOOGLE_PROTOBUF", MacroPrefix(options_)); + std::map vars; + SetCommonVars(options_, &vars); + format_.AddMap(vars); + + std::vector ordered_fields; + for (auto field : FieldRange(descriptor)) { + ordered_fields.push_back(field); + } + std::sort(ordered_fields.begin(), ordered_fields.end(), + [](const FieldDescriptor* a, const FieldDescriptor* b) { + return a->number() < b->number(); + }); + + format_( + "const char* $classname$::_InternalParse(const char* ptr, " + "$pi_ns$::ParseContext* ctx) {\n" + "#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure\n"); + format_.Indent(); + int hasbits_size = 0; + if (HasFieldPresence(descriptor->file())) { + hasbits_size = (num_hasbits_ + 31) / 32; + } + // For now only optimize small hasbits. + if (hasbits_size != 1) hasbits_size = 0; + if (hasbits_size) { + format_("_Internal::HasBits has_bits{};\n"); + format_.Set("has_bits", "has_bits"); + } else { + format_.Set("has_bits", "_has_bits_"); + } + + if (descriptor->file()->options().cc_enable_arenas()) { + format_("$p_ns$::Arena* arena = GetArenaNoVirtual(); (void)arena;\n"); + } + GenerateParseLoop(descriptor, ordered_fields); + format_.Outdent(); + format_("success:\n"); + if (hasbits_size) format_(" _has_bits_.Or(has_bits);\n"); + format_( + " return ptr;\n" + "failure:\n" + " ptr = nullptr;\n" + " goto success;\n" + "#undef CHK_\n" + "}\n"); + } + + private: + MessageSCCAnalyzer* scc_analyzer_; + const Options& options_; + Formatter format_; + int num_hasbits_; + + using WireFormat = internal::WireFormat; + using WireFormatLite = internal::WireFormatLite; + + void GenerateArenaString(const FieldDescriptor* field, + const std::string& utf8, std::string field_name) { + if (!field_name.empty()) { + format_("static const char kFieldName[] = $1$;\n", + field_name.substr(2)); // remove ", " + field_name = ", kFieldName"; + } + if (HasFieldPresence(field->file())) { + format_("_Internal::set_has_$1$(&$has_bits$);\n", FieldName(field)); + } + string default_string = + field->default_value_string().empty() + ? "::" + ProtobufNamespace(options_) + + "::internal::GetEmptyStringAlreadyInited()" + : QualifiedClassName(field->containing_type(), options_) + + "::" + MakeDefaultName(field) + ".get()"; + format_( + "if (arena != nullptr) {\n" + " ptr = $pi_ns$::InlineCopyIntoArenaString$1$(&$2$_, ptr, ctx, " + " arena$3$);\n" + "} else {\n" + " ptr = " + "$pi_ns$::InlineGreedyStringParser$1$($2$_.MutableNoArenaNoDefault(&$4$" + "), ptr, ctx$3$);" + "\n}\n", + utf8, FieldName(field), field_name, default_string); + } + + void GenerateStrings(const FieldDescriptor* field, bool check_utf8) { + std::string utf8; + std::string field_name; + if (check_utf8) { + utf8 = GetUtf8Suffix(field, options_); + if (!utf8.empty()) { + field_name = ", nullptr"; + if (HasDescriptorMethods(field->file(), options_)) { + field_name = StrCat(", \"", field->full_name(), "\""); } - format("$1$ value = static_cast<$1$>(val);\n", - QualifiedClassName(field->enum_type())); - } else { - format("$1$ value = val;\n", type); - } - if (field->is_repeated()) { - format("msg->add_$1$(value);\n", FieldName(field)); - } else { - format("msg->set_$1$(value);\n", FieldName(field)); } - break; } - case WireFormatLite::WIRETYPE_FIXED64: { - string type = PrimitiveTypeName(options, field->cpp_type()); - format( - "$1$ val;\n" - "::std::memcpy(&val, ptr, 8);\n" - "ptr += 8;\n", - type); - if (field->is_repeated()) { - format("msg->add_$1$(val);\n", FieldName(field)); - } else { - format("msg->set_$1$(val);\n", FieldName(field)); - } - break; + FieldOptions::CType ctype = FieldOptions::STRING; + if (!options_.opensource_runtime) { + // Open source doesn't support other ctypes; + ctype = field->options().ctype(); } - case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: { - format( - "ptr = Varint::Parse32Inline(ptr, &size);\n" - "if (!ptr) goto error;\n"); - if (!IsProto1(field->file(), options) && field->is_packable()) { - if (!HasPreservingUnknownEnumSemantics(field->file()) && - field->type() == FieldDescriptor::TYPE_ENUM) { - format( - "ctx->extra_parse_data().SetEnumValidator($1$_IsValid, " - "msg->mutable_unknown_fields(), $2$);\n" - "parser_till_end = " - "::$proto_ns$::internal::PackedValidEnumParser$3$;\n" - "object = msg->mutable_$4$();\n", - QualifiedClassName(field->enum_type()), field->number(), - UseUnknownFieldSet(field->file(), options) ? "" : "Lite", - FieldName(field)); - } else { - format( - "parser_till_end = ::$proto_ns$::internal::Packed$1$Parser;\n" - "object = msg->mutable_$2$();\n", - DeclaredTypeMethodName(field->type()), FieldName(field)); - } - } else { - auto field_type = field->type(); - if (IsProto1(field->file(), options)) { - if (field->is_packable()) { - // Sigh ... packed fields endup as a string in proto1 - field_type = FieldDescriptor::TYPE_BYTES; - } - if (field_type == FieldDescriptor::TYPE_STRING) { - // In proto1 strings are treated as bytes - field_type = FieldDescriptor::TYPE_BYTES; - } - } - string utf8 = ""; - switch (field_type) { - case FieldDescriptor::TYPE_STRING: - utf8 = GetUtf8Suffix(field, options); - if (!utf8.empty()) { - string name = "nullptr"; - if (HasDescriptorMethods(field->file(), options)) { - name = field->full_name(); - } - format("ctx->extra_parse_data().SetFieldName(\"$1$\");\n", name); + if (field->file()->options().cc_enable_arenas() && !field->is_repeated() && + !options_.opensource_runtime && + GetOptimizeFor(field->file(), options_) != FileOptions::LITE_RUNTIME && + // For now only use arena string for strings with empty defaults. + field->default_value_string().empty() && + !IsStringInlined(field, options_) && + field->containing_oneof() == nullptr && ctype == FieldOptions::STRING) { + GenerateArenaString(field, utf8, field_name); + return; + } + std::string name; + switch (ctype) { + case FieldOptions::STRING: + name = "GreedyStringParser" + utf8; + break; + case FieldOptions::CORD: + name = "CordParser" + utf8; + break; + case FieldOptions::STRING_PIECE: + name = "StringPieceParser" + utf8; + break; + } + format_("ptr = $pi_ns$::Inline$1$($2$_$3$(), ptr, ctx$4$);\n", name, + field->is_repeated() && !field->is_packable() ? "add" : "mutable", + FieldName(field), field_name); + } + + void GenerateLengthDelim(const FieldDescriptor* field) { + if (field->is_packable()) { + std::string enum_validator; + if (field->type() == FieldDescriptor::TYPE_ENUM && + !HasPreservingUnknownEnumSemantics(field)) { + enum_validator = + StrCat(", ", QualifiedClassName(field->enum_type(), options_), + "_IsValid, &_internal_metadata_, ", field->number()); + } + format_("ptr = $pi_ns$::Packed$1$Parser(mutable_$2$(), ptr, ctx$3$);\n", + DeclaredTypeMethodName(field->type()), FieldName(field), + enum_validator); + } else { + auto field_type = field->type(); + switch (field_type) { + case FieldDescriptor::TYPE_STRING: + GenerateStrings(field, true /* utf8 */); + break; + case FieldDescriptor::TYPE_BYTES: + GenerateStrings(field, false /* utf8 */); + break; + case FieldDescriptor::TYPE_MESSAGE: { + if (field->is_map()) { + const FieldDescriptor* val = + field->message_type()->FindFieldByName("value"); + GOOGLE_CHECK(val); + if (HasFieldPresence(field->file()) && + val->type() == FieldDescriptor::TYPE_ENUM) { + format_( + "auto object = ::$proto_ns$::internal::InitEnumParseWrapper(" + "&$1$_, $2$_IsValid, $3$, &_internal_metadata_);\n" + "ptr = ctx->ParseMessage(&object, ptr);\n", + FieldName(field), QualifiedClassName(val->enum_type()), + field->number()); + } else { + format_("ptr = ctx->ParseMessage(&$1$_, ptr);\n", + FieldName(field)); } - [[clang::fallthrough]]; - case FieldDescriptor::TYPE_BYTES: { - if (field->options().ctype() == FieldOptions::STRING || - (IsProto1(field->file(), options) && - field->options().ctype() == FieldOptions::STRING_PIECE)) { - format( - "parser_till_end = ::$proto_ns$::internal::StringParser$1$;\n" - "$string$* str = msg->$2$_$3$();\n" - "str->clear();\n", - utf8, - field->is_repeated() && !field->is_map() && - !field->is_packable() - ? "add" - : "mutable", - FieldName(field)); - } else if (field->options().ctype() == FieldOptions::CORD) { - string cord_parser = "CordParser" + utf8; - format( - "parser_till_end = ::$proto_ns$::internal::$1$;\n" - "auto* str = msg->$2$_$3$();\n" - "str->Clear();\n", - cord_parser, - field->is_repeated() && !field->is_map() ? "add" : "mutable", + } else if (IsLazy(field, options_)) { + if (field->containing_oneof() != nullptr) { + format_( + "if (!has_$1$()) {\n" + " clear_$1$();\n" + " $2$_.$1$_ = ::$proto_ns$::Arena::CreateMessage<\n" + " $pi_ns$::LazyField>(" + "GetArenaNoVirtual());\n" + " set_has_$1$();\n" + "}\n" + "ptr = ctx->ParseMessage($2$_.$1$_, ptr);\n", + FieldName(field), field->containing_oneof()->name()); + } else if (HasFieldPresence(field->file())) { + format_( + "_Internal::set_has_$1$(&$has_bits$);\n" + "ptr = ctx->ParseMessage(&$1$_, ptr);\n", FieldName(field)); - } else if (field->options().ctype() == FieldOptions::STRING_PIECE) { - format( - "parser_till_end = " - "::$proto_ns$::internal::StringPieceParser$1$;\n" - "::$proto_ns$::internal::StringPieceField* str = " - "msg->$2$_$3$();\n" - "str->Clear();\n", - utf8, - field->is_repeated() && !field->is_map() ? "add" : "mutable", + } else { + format_("ptr = ctx->ParseMessage(&$1$_, ptr);\n", + FieldName(field)); + } + } else if (IsImplicitWeakField(field, options_, scc_analyzer_)) { + if (!field->is_repeated()) { + format_( + "ptr = ctx->ParseMessage(_Internal::mutable_$1$(this), " + "ptr);\n", FieldName(field)); + } else { + format_( + "ptr = ctx->ParseMessage(" + "CastToBase(&$1$_)->AddWeak(reinterpret_cast(&$2$::_$3$_default_instance_)), " + "ptr);\n", + FieldName(field), Namespace(field->message_type(), options_), + ClassName(field->message_type())); } - format("object = str;\n"); - break; + } else if (IsWeak(field, options_)) { + format_( + "ptr = ctx->ParseMessage(_weak_field_map_.MutableMessage($1$," + " _$classname$_default_instance_.$2$_), ptr);\n", + field->number(), FieldName(field)); + } else { + format_("ptr = ctx->ParseMessage($1$_$2$(), ptr);\n", + field->is_repeated() ? "add" : "mutable", FieldName(field)); + } + break; + } + default: + GOOGLE_LOG(FATAL) << "Illegal combination for length delimited wiretype " + << " filed type is " << field->type(); + } + } + } + + // Convert a 1 or 2 byte varint into the equivalent value upon a direct load. + static uint32 SmallVarintValue(uint32 x) { + GOOGLE_DCHECK(x < 128 * 128); + if (x >= 128) x += (x & 0xFF80) + 128; + return x; + } + + static bool ShouldRepeat(const FieldDescriptor* descriptor, + internal::WireFormatLite::WireType wiretype) { + constexpr int kMaxTwoByteFieldNumber = 16 * 128; + return descriptor->number() < kMaxTwoByteFieldNumber && + descriptor->is_repeated() && + (!descriptor->is_packable() || + wiretype != internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED); + } + + void GenerateFieldBody(internal::WireFormatLite::WireType wiretype, + const FieldDescriptor* field) { + uint32 tag = WireFormatLite::MakeTag(field->number(), wiretype); + switch (wiretype) { + case WireFormatLite::WIRETYPE_VARINT: { + std::string type = PrimitiveTypeName(options_, field->cpp_type()); + std::string prefix = field->is_repeated() ? "add" : "set"; + if (field->type() == FieldDescriptor::TYPE_ENUM) { + format_( + "$uint64$ val = $pi_ns$::ReadVarint(&ptr);\n" + "CHK_(ptr);\n"); + if (!HasPreservingUnknownEnumSemantics(field)) { + format_("if (PROTOBUF_PREDICT_TRUE($1$_IsValid(val))) {\n", + QualifiedClassName(field->enum_type(), options_)); + format_.Indent(); } - case FieldDescriptor::TYPE_MESSAGE: { - GOOGLE_CHECK(field->message_type()); - if (IsImplicitWeakField(field, options, scc_analyzer)) { - if (!field->is_repeated()) { - format("object = HasBitSetters::mutable_$1$(msg);\n", - FieldName(field)); - } else { - format( - "object = " - "CastToBase(&msg->$1$_)->AddWeak(reinterpret_cast(&$2$::_$3$_default_instance_));\n", - FieldName(field), Namespace(field->message_type()), - ClassName(field->message_type())); - } - format( - "parser_till_end = static_cast<::$proto_ns$::MessageLite*>(" - "object)->_ParseFunc();\n"); - break; - } else if (IsWeak(field, options)) { - if (IsProto1(field->file(), options)) { - format("object = msg->internal_mutable_$1$();\n", - FieldName(field)); - } else { - format( - "object = msg->_weak_field_map_.MutableMessage($1$, " - "_$classname$_default_instance_.$2$_);\n", - field->number(), FieldName(field)); - } - format( - "parser_till_end = static_cast<::$proto_ns$::MessageLite*>(" - "object)->_ParseFunc();\n"); - break; + format_("$1$_$2$(static_cast<$3$>(val));\n", prefix, FieldName(field), + QualifiedClassName(field->enum_type(), options_)); + if (!HasPreservingUnknownEnumSemantics(field)) { + format_.Outdent(); + format_( + "} else {\n" + " $pi_ns$::WriteVarint($1$, val, mutable_unknown_fields());\n" + "}\n", + field->number()); + } + } else { + int size = field->type() == FieldDescriptor::TYPE_SINT32 ? 32 : 64; + std::string zigzag; + if ((field->type() == FieldDescriptor::TYPE_SINT32 || + field->type() == FieldDescriptor::TYPE_SINT64)) { + zigzag = StrCat("ZigZag", size); + } + if (field->is_repeated() || field->containing_oneof()) { + string prefix = field->is_repeated() ? "add" : "set"; + format_( + "$1$_$2$($pi_ns$::ReadVarint$3$(&ptr));\n" + "CHK_(ptr);\n", + prefix, FieldName(field), zigzag); + } else { + if (HasFieldPresence(field->file())) { + format_("_Internal::set_has_$1$(&$has_bits$);\n", + FieldName(field)); } - format( - "parser_till_end = $1$::_InternalParse;\n" - "object = msg->$2$_$3$();\n", - QualifiedClassName(field->message_type()), - field->is_repeated() && !field->is_map() ? "add" : "mutable", - FieldName(field)); - break; + format_( + "$1$_ = $pi_ns$::ReadVarint$2$(&ptr);\n" + "CHK_(ptr);\n", + FieldName(field), zigzag); } - default: - GOOGLE_LOG(FATAL) << "Illegal combination for length delimited wiretype " - << " filed type is " << field->type(); } + break; } - format( - "if (size > end - ptr) goto len_delim_till_end;\n" - "auto newend = ptr + size;\n" - "if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) " - "goto error;\n" - "ptr = newend;\n"); - break; - } - case WireFormatLite::WIRETYPE_START_GROUP: { - format( - "parser_till_end = $1$::_InternalParse;\n" - "object = msg->$2$_$3$();\n" - "if (!ctx->PrepareGroup(tag, &depth)) goto error;\n" - "ptr = parser_till_end(ptr, end, object, ctx);\n" - "if (!ptr) goto error;\n" - "if (ctx->GroupContinues(depth)) goto group_continues;\n", - QualifiedClassName(field->message_type()), - field->is_repeated() ? "add" : "mutable", FieldName(field)); - break; - } - case WireFormatLite::WIRETYPE_END_GROUP: { - GOOGLE_LOG(FATAL) << "Can't have end group field\n"; - break; - } - case WireFormatLite::WIRETYPE_FIXED32: { - string type = PrimitiveTypeName(options, field->cpp_type()); - format( - "$1$ val;\n" - "std::memcpy(&val, ptr, 4);\n" - "ptr += 4;\n", - type); - if (field->is_repeated()) { - format("msg->add_$1$(val);\n", FieldName(field)); - } else { - format("msg->set_$1$(val);\n", FieldName(field)); + case WireFormatLite::WIRETYPE_FIXED32: + case WireFormatLite::WIRETYPE_FIXED64: { + std::string type = PrimitiveTypeName(options_, field->cpp_type()); + if (field->is_repeated() || field->containing_oneof()) { + string prefix = field->is_repeated() ? "add" : "set"; + format_( + "$1$_$2$($pi_ns$::UnalignedLoad<$3$>(ptr));\n" + "ptr += sizeof($3$);\n", + prefix, FieldName(field), type); + } else { + if (HasFieldPresence(field->file())) { + format_("_Internal::set_has_$1$(&$has_bits$);\n", FieldName(field)); + } + format_( + "$1$_ = $pi_ns$::UnalignedLoad<$2$>(ptr);\n" + "ptr += sizeof($2$);\n", + FieldName(field), type); + } + break; } - break; + case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: { + GenerateLengthDelim(field); + format_("CHK_(ptr);\n"); + break; + } + case WireFormatLite::WIRETYPE_START_GROUP: { + format_( + "ptr = ctx->ParseGroup($1$_$2$(), ptr, $3$);\n" + "CHK_(ptr);\n", + field->is_repeated() ? "add" : "mutable", FieldName(field), tag); + break; + } + case WireFormatLite::WIRETYPE_END_GROUP: { + GOOGLE_LOG(FATAL) << "Can't have end group field\n"; + break; + } + } // switch (wire_type) + } + + // Returns the tag for this field and in case of repeated packable fields, + // sets a fallback tag in fallback_tag_ptr. + static uint32 ExpectedTag(const FieldDescriptor* field, + uint32* fallback_tag_ptr) { + uint32 expected_tag; + if (field->is_packable()) { + auto expected_wiretype = WireFormat::WireTypeForFieldType(field->type()); + expected_tag = + WireFormatLite::MakeTag(field->number(), expected_wiretype); + GOOGLE_CHECK(expected_wiretype != WireFormatLite::WIRETYPE_LENGTH_DELIMITED); + auto fallback_wiretype = WireFormatLite::WIRETYPE_LENGTH_DELIMITED; + uint32 fallback_tag = + WireFormatLite::MakeTag(field->number(), fallback_wiretype); + + if (field->is_packed()) std::swap(expected_tag, fallback_tag); + *fallback_tag_ptr = fallback_tag; + } else { + auto expected_wiretype = WireFormat::WireTypeForField(field); + expected_tag = + WireFormatLite::MakeTag(field->number(), expected_wiretype); } - } // switch (wire_type) - - if (ShouldRepeat(field, wiretype)) { - format("if (ptr >= end) break;\n"); - uint32 x = field->number() * 8 + wiretype; - uint64 y = 0; - int cnt = 0; - do { - y += static_cast((x & 0x7F) + (x >= 128 ? 128 : 0)) - << (cnt++ * 8); - x >>= 7; - } while (x); - uint64 mask = (1ull << (cnt * 8)) - 1; - format.Outdent(); - format( - "} while((*reinterpret_cast(ptr) & $1$) == $2$ && " - "(ptr += $3$));\n", - mask, y, cnt); - } - format("break;\n"); -} - -void GenerateCaseBody(const FieldDescriptor* field, const Options& options, - MessageSCCAnalyzer* scc_analyzer, - const Formatter& format) { - using internal::WireFormat; - using internal::WireFormatLite; - - if (!IsProto1(field->file(), options) && field->is_packable()) { - auto expected_wiretype = WireFormat::WireTypeForFieldType(field->type()); - GOOGLE_CHECK(expected_wiretype != WireFormatLite::WIRETYPE_LENGTH_DELIMITED); - uint32 expected_tag = - WireFormatLite::MakeTag(field->number(), expected_wiretype); - auto fallback_wiretype = WireFormatLite::WIRETYPE_LENGTH_DELIMITED; - uint32 fallback_tag = - WireFormatLite::MakeTag(field->number(), fallback_wiretype); - - if (field->is_packed()) { - std::swap(expected_tag, fallback_tag); - std::swap(expected_wiretype, fallback_wiretype); + return expected_tag; + } + + void GenerateParseLoop( + const Descriptor* descriptor, + const std::vector& ordered_fields) { + format_( + "while (!ctx->Done(&ptr)) {\n" + " $uint32$ tag;\n" + " ptr = $pi_ns$::ReadTag(ptr, &tag);\n" + " CHK_(ptr);\n" + " switch (tag >> 3) {\n"); + + format_.Indent(); + format_.Indent(); + + for (const auto* field : ordered_fields) { + // Print the field's (or oneof's) proto-syntax definition as a comment. + // We don't want to print group bodies so we cut off after the first + // line. + std::string def; + { + DebugStringOptions options; + options.elide_group_body = true; + options.elide_oneof_body = true; + def = field->DebugStringWithOptions(options); + def = def.substr(0, def.find_first_of('\n')); + } + format_( + "// $1$\n" + "case $2$:\n", + def, field->number()); + format_.Indent(); + uint32 fallback_tag = 0; + uint32 expected_tag = ExpectedTag(field, &fallback_tag); + format_( + "if (PROTOBUF_PREDICT_TRUE(static_cast<$uint8$>(tag) == $1$)) {\n", + expected_tag & 0xFF); + format_.Indent(); + auto wiretype = WireFormatLite::GetTagWireType(expected_tag); + uint32 tag = WireFormatLite::MakeTag(field->number(), wiretype); + int tag_size = io::CodedOutputStream::VarintSize32(tag); + bool is_repeat = ShouldRepeat(field, wiretype); + if (is_repeat) { + format_( + "ptr -= $1$;\n" + "do {\n" + " ptr += $1$;\n", + tag_size); + format_.Indent(); + } + GenerateFieldBody(wiretype, field); + if (is_repeat) { + string type = tag_size == 2 ? "uint16" : "uint8"; + format_.Outdent(); + format_( + " if (!ctx->DataAvailable(ptr)) break;\n" + "} while ($pi_ns$::UnalignedLoad<$1$>(ptr) == $2$);\n", + IntTypeName(options_, type), SmallVarintValue(tag)); + } + format_.Outdent(); + if (fallback_tag) { + format_("} else if (static_cast<$uint8$>(tag) == $1$) {\n", + fallback_tag & 0xFF); + format_.Indent(); + GenerateFieldBody(WireFormatLite::GetTagWireType(fallback_tag), field); + format_.Outdent(); + } + format_.Outdent(); + format_( + " } else goto handle_unusual;\n" + " continue;\n"); + } // for loop over ordered fields + + // Default case + format_("default: {\n"); + if (!ordered_fields.empty()) format_("handle_unusual:\n"); + format_( + " if ((tag & 7) == 4 || tag == 0) {\n" + " ctx->SetLastTag(tag);\n" + " goto success;\n" + " }\n"); + if (IsMapEntryMessage(descriptor)) { + format_(" continue;\n"); + } else { + if (descriptor->extension_range_count() > 0) { + format_("if ("); + for (int i = 0; i < descriptor->extension_range_count(); i++) { + const Descriptor::ExtensionRange* range = + descriptor->extension_range(i); + if (i > 0) format_(" ||\n "); + + uint32 start_tag = WireFormatLite::MakeTag( + range->start, static_cast(0)); + uint32 end_tag = WireFormatLite::MakeTag( + range->end, static_cast(0)); + + if (range->end > FieldDescriptor::kMaxNumber) { + format_("($1$u <= tag)", start_tag); + } else { + format_("($1$u <= tag && tag < $2$u)", start_tag, end_tag); + } + } + format_(") {\n"); + format_( + " ptr = _extensions_.ParseField(tag, ptr,\n" + " internal_default_instance(), &_internal_metadata_, ctx);\n" + " CHK_(ptr != nullptr);\n" + " continue;\n" + "}\n"); + } + format_( + " ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx);\n" + " CHK_(ptr != nullptr);\n" + " continue;\n"); } - - format("if (static_cast<$uint8$>(tag) == $1$) {\n", expected_tag & 0xFF); - format.Indent(); - GenerateCaseBody(expected_wiretype, field, options, scc_analyzer, format); - format.Outdent(); - format( - "} else if (static_cast<$uint8$>(tag) != $1$) goto handle_unusual;\n", - fallback_tag & 0xFF); - GenerateCaseBody(fallback_wiretype, field, options, scc_analyzer, format); - } else { - auto wiretype = WireFormat::WireTypeForField(field); - format("if (static_cast<$uint8$>(tag) != $1$) goto handle_unusual;\n", - WireFormat::MakeTag(field) & 0xFF); - GenerateCaseBody(wiretype, field, options, scc_analyzer, format); + format_("}\n"); // default case + format_.Outdent(); + format_.Outdent(); + format_( + " } // switch\n" + "} // while\n"); } -} +}; -void GenerateParserLoop(const Descriptor* descriptor, const Options& options, +void GenerateParserLoop(const Descriptor* descriptor, int num_hasbits, + const Options& options, MessageSCCAnalyzer* scc_analyzer, io::Printer* printer) { - using internal::WireFormat; - using internal::WireFormatLite; - - Formatter format(printer); - format.Set("classname", ClassName(descriptor)); - format.Set("proto_ns", ProtobufNamespace(options)); - std::map vars; - SetCommonVars(options, &vars); - format.AddMap(vars); - - std::vector ordered_fields; - for (auto field : FieldRange(descriptor)) { - ordered_fields.push_back(field); - } - std::sort(ordered_fields.begin(), ordered_fields.end(), - [](const FieldDescriptor* a, const FieldDescriptor* b) { - return a->number() < b->number(); - }); - - format( - "const char* $classname$::_InternalParse(const char* begin, const char* " - "end, void* object,\n" - " ::$proto_ns$::internal::ParseContext* ctx) {\n" - " auto msg = static_cast<$classname$*>(object);\n" - " $uint32$ size; (void)size;\n" - " int depth; (void)depth;\n" - " ::$proto_ns$::internal::ParseFunc parser_till_end; " - "(void)parser_till_end;\n" - " auto ptr = begin;\n" - " while (ptr < end) {\n" - " $uint32$ tag;\n" - " ptr = Varint::Parse32Inline(ptr, &tag);\n" - " if (!ptr) goto error;\n" - " switch (tag >> 3) {\n" - " case 0: goto error;\n"); - - format.Indent(); - format.Indent(); - format.Indent(); - - for (const auto* field : ordered_fields) { - if (IsProto1(descriptor->file(), options)) { - if (field->number() >= (1 << 14)) continue; - } - // Print the field's (or oneof's) proto-syntax definition as a comment. - // We don't want to print group bodies so we cut off after the first - // line. - string def; - { - DebugStringOptions options; - options.elide_group_body = true; - options.elide_oneof_body = true; - def = field->DebugStringWithOptions(options); - def = def.substr(0, def.find_first_of('\n')); - } - format( - "// $1$\n" - "case $2$: {\n", - def, field->number()); - format.Indent(); - GenerateCaseBody(field, options, scc_analyzer, format); - format.Outdent(); - format("}\n"); // case - } // for fields - format( - "default: {\n" - "handle_unusual: (void)&&handle_unusual;\n" - " if ((tag & 7) == 4) {\n" - " if (!ctx->ValidEndGroup(tag)) goto error;\n" - " return ptr;\n" - " }\n"); - if (IsMapEntryMessage(descriptor)) { - format( - " break;\n" - "}\n"); - } else { - if (descriptor->extension_range_count() > 0) { - format("if ("); - for (int i = 0; i < descriptor->extension_range_count(); i++) { - const Descriptor::ExtensionRange* range = - descriptor->extension_range(i); - if (i > 0) format(" ||\n "); - - uint32 start_tag = WireFormatLite::MakeTag( - range->start, static_cast(0)); - uint32 end_tag = WireFormatLite::MakeTag( - range->end, static_cast(0)); - - if (range->end > FieldDescriptor::kMaxNumber) { - format("($1$u <= tag)", start_tag); - } else { - format("($1$u <= tag && tag < $2$u)", start_tag, end_tag); - } - } - format(") {\n"); - format( - " auto res = msg->_extensions_.ParseField(tag, {_InternalParse, " - "msg}, ptr, end,\n" - " internal_default_instance(), &msg->_internal_metadata_, " - "ctx);\n" - " ptr = res.first;\n" - " if (res.second) return ptr;\n" - " continue;\n" - "}\n"); - } - format( - " auto res = UnknownFieldParse(tag, {_InternalParse, msg},\n" - " ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), " - "ctx);\n" - " ptr = res.first;\n" - " if (res.second) return ptr;\n" - "}\n"); // default case - } - format.Outdent(); - format.Outdent(); - format.Outdent(); - format( - " } // switch\n" - " } // while\n" - " return ptr;\n" - "error:\n" - " return nullptr;\n" - "len_delim_till_end: (void)&&len_delim_till_end;\n" - " return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},\n" - " {parser_till_end, object}, size);\n" - "group_continues: (void)&&group_continues;\n" - " $DCHK$(ptr >= end);\n" - // Group crossed end and must be continued. Either this a parse failure - // or we need to resume on the next chunk and thus save the state. - " ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, " - "depth);\n" - " return ptr;\n" - "}\n"); + ParseLoopGenerator generator(num_hasbits, options, scc_analyzer, printer); + generator.GenerateParserLoop(descriptor); } } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.h b/src/google/protobuf/compiler/cpp/cpp_helpers.h index c01329fa2a..fd518e0780 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.h +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.h @@ -35,6 +35,7 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__ #define GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__ +#include #include #include #include @@ -48,6 +49,7 @@ #include #include + #include namespace google { @@ -55,19 +57,17 @@ namespace protobuf { namespace compiler { namespace cpp { -inline string ProtobufNamespace(const Options& options) { - return options.opensource_runtime ? "google::protobuf" : "proto2"; +inline std::string ProtobufNamespace(const Options& options) { + return "PROTOBUF_NAMESPACE_ID"; } -inline string MacroPrefix(const Options& options) { +inline std::string MacroPrefix(const Options& options) { return options.opensource_runtime ? "GOOGLE_PROTOBUF" : "GOOGLE_PROTOBUF"; } -inline string DeprecatedAttribute(const Options& options, bool deprecated) { - if (!deprecated) { - return ""; - } - return MacroPrefix(options) + "_DEPRECATED "; +inline std::string DeprecatedAttribute(const Options& options, + bool deprecated) { + return deprecated ? "PROTOBUF_DEPRECATED " : ""; } // Commonly-used separator comments. Thick is a line of '=', thin is a line @@ -75,39 +75,35 @@ inline string DeprecatedAttribute(const Options& options, bool deprecated) { extern const char kThickSeparator[]; extern const char kThinSeparator[]; -inline bool IsProto1(const FileDescriptor* file, const Options& options) { - return false; -} - -void SetCommonVars(const Options& options, std::map* variables); +void SetCommonVars(const Options& options, + std::map* variables); -bool GetBootstrapBasename(const Options& options, const string& basename, - string* bootstrap_basename); +bool GetBootstrapBasename(const Options& options, const std::string& basename, + std::string* bootstrap_basename); bool MaybeBootstrap(const Options& options, GeneratorContext* generator_context, - bool bootstrap_flag, string* basename); + bool bootstrap_flag, std::string* basename); bool IsBootstrapProto(const Options& options, const FileDescriptor* file); // Name space of the proto file. This namespace is such that the string // "::some_name" is the correct fully qualified namespace. // This means if the package is empty the namespace is "", and otherwise // the namespace is "::foo::bar::...::baz" without trailing semi-colons. -string Namespace(const string& package); -inline string Namespace(const FileDescriptor* d) { - return Namespace(d->package()); -} - -string Namespace(const Descriptor* d); -string Namespace(const FieldDescriptor* d); -string Namespace(const EnumDescriptor* d); +std::string Namespace(const FileDescriptor* d, const Options& options); +std::string Namespace(const Descriptor* d, const Options& options); +std::string Namespace(const FieldDescriptor* d, const Options& options); +std::string Namespace(const EnumDescriptor* d, const Options& options); // Returns true if it's safe to reset "field" to zero. bool CanInitializeByZeroing(const FieldDescriptor* field); -string ClassName(const Descriptor* descriptor); -string ClassName(const EnumDescriptor* enum_descriptor); +std::string ClassName(const Descriptor* descriptor); +std::string ClassName(const EnumDescriptor* enum_descriptor); + +std::string QualifiedClassName(const Descriptor* d, const Options& options); +std::string QualifiedClassName(const EnumDescriptor* d, const Options& options); -string QualifiedClassName(const Descriptor* d); -string QualifiedClassName(const EnumDescriptor* d); +std::string QualifiedClassName(const Descriptor* d); +std::string QualifiedClassName(const EnumDescriptor* d); // DEPRECATED just use ClassName or QualifiedClassName, a boolean is very // unreadable at the callsite. @@ -119,33 +115,57 @@ string QualifiedClassName(const EnumDescriptor* d); // ::foo::bar::Baz_Qux // While the non-qualified version would be: // Baz_Qux -inline string ClassName(const Descriptor* descriptor, bool qualified) { - return qualified ? QualifiedClassName(descriptor) : ClassName(descriptor); +inline std::string ClassName(const Descriptor* descriptor, bool qualified) { + return qualified ? QualifiedClassName(descriptor, Options()) + : ClassName(descriptor); } -inline string ClassName(const EnumDescriptor* descriptor, bool qualified) { - return qualified ? QualifiedClassName(descriptor) : ClassName(descriptor); +inline std::string ClassName(const EnumDescriptor* descriptor, bool qualified) { + return qualified ? QualifiedClassName(descriptor, Options()) + : ClassName(descriptor); } +// Type name of default instance. +std::string DefaultInstanceType(const Descriptor* descriptor, + const Options& options); + +// Non-qualified name of the default_instance of this message. +std::string DefaultInstanceName(const Descriptor* descriptor, + const Options& options); + // Fully qualified name of the default_instance of this message. -string DefaultInstanceName(const Descriptor* descriptor); +std::string QualifiedDefaultInstanceName(const Descriptor* descriptor, + const Options& options); + +// DescriptorTable variable name. +std::string DescriptorTableName(const FileDescriptor* file, + const Options& options); + +// When declaring symbol externs from another file, this macro will supply the +// dllexport needed for the target file, if any. +std::string FileDllExport(const FileDescriptor* file, const Options& options); // Returns the name of a no-op function that we can call to introduce a linker // dependency on the given message type. This is used to implement implicit weak // fields. -string ReferenceFunctionName(const Descriptor* descriptor); +std::string ReferenceFunctionName(const Descriptor* descriptor, + const Options& options); // Name of the base class: google::protobuf::Message or google::protobuf::MessageLite. -string SuperClassName(const Descriptor* descriptor, const Options& options); +std::string SuperClassName(const Descriptor* descriptor, + const Options& options); + +// Adds an underscore if necessary to prevent conflicting with a keyword. +std::string ResolveKeyword(const string& name); // Get the (unqualified) name that should be used for this field in C++ code. // The name is coerced to lower-case to emulate proto1 behavior. People // should be using lowercase-with-underscores style for proto field names // anyway, so normally this just returns field->name(). -string FieldName(const FieldDescriptor* field); +std::string FieldName(const FieldDescriptor* field); // Get the sanitized name that should be used for the given enum in C++ code. -string EnumValueName(const EnumValueDescriptor* enum_value); +std::string EnumValueName(const EnumValueDescriptor* enum_value); // Returns an estimate of the compiler's alignment for the field. This // can't guarantee to be correct because the generated code could be compiled on @@ -155,63 +175,66 @@ int EstimateAlignmentSize(const FieldDescriptor* field); // Get the unqualified name that should be used for a field's field // number constant. -string FieldConstantName(const FieldDescriptor *field); +std::string FieldConstantName(const FieldDescriptor* field); // Returns the scope where the field was defined (for extensions, this is // different from the message type to which the field applies). inline const Descriptor* FieldScope(const FieldDescriptor* field) { - return field->is_extension() ? - field->extension_scope() : field->containing_type(); + return field->is_extension() ? field->extension_scope() + : field->containing_type(); } // Returns the fully-qualified type name field->message_type(). Usually this // is just ClassName(field->message_type(), true); -string FieldMessageTypeName(const FieldDescriptor* field); +std::string FieldMessageTypeName(const FieldDescriptor* field, + const Options& options); // Strips ".proto" or ".protodevel" from the end of a filename. -LIBPROTOC_EXPORT string StripProto(const string& filename); +PROTOC_EXPORT std::string StripProto(const std::string& filename); // Get the C++ type name for a primitive type (e.g. "double", "::google::protobuf::int32", etc.). const char* PrimitiveTypeName(FieldDescriptor::CppType type); -string PrimitiveTypeName(const Options& options, FieldDescriptor::CppType type); +std::string PrimitiveTypeName(const Options& options, + FieldDescriptor::CppType type); // Get the declared type name in CamelCase format, as is used e.g. for the // methods of WireFormat. For example, TYPE_INT32 becomes "Int32". const char* DeclaredTypeMethodName(FieldDescriptor::Type type); // Return the code that evaluates to the number when compiled. -string Int32ToString(int number); +std::string Int32ToString(int number); // Return the code that evaluates to the number when compiled. -string Int64ToString(const Options& options, int64 number); +std::string Int64ToString(const Options& options, int64 number); // Get code that evaluates to the field's default value. -string DefaultValue(const Options& options, const FieldDescriptor* field); +std::string DefaultValue(const Options& options, const FieldDescriptor* field); // Compatibility function for callers outside proto2. -string DefaultValue(const FieldDescriptor* field); +std::string DefaultValue(const FieldDescriptor* field); // Convert a file name into a valid identifier. -string FilenameIdentifier(const string& filename); +std::string FilenameIdentifier(const std::string& filename); // For each .proto file generates a unique name. To prevent collisions of // symbols in the global namespace -string UniqueName(const string& name, const string& filename, - const Options& options); -inline string UniqueName(const string& name, const FileDescriptor* d, - const Options& options) { +std::string UniqueName(const std::string& name, const std::string& filename, + const Options& options); +inline std::string UniqueName(const std::string& name, const FileDescriptor* d, + const Options& options) { return UniqueName(name, d->name(), options); } -inline string UniqueName(const string& name, const Descriptor* d, - const Options& options) { +inline std::string UniqueName(const std::string& name, const Descriptor* d, + const Options& options) { return UniqueName(name, d->file(), options); } -inline string UniqueName(const string& name, const EnumDescriptor* d, - const Options& options) { +inline std::string UniqueName(const std::string& name, const EnumDescriptor* d, + const Options& options) { return UniqueName(name, d->file(), options); } -inline string UniqueName(const string& name, const ServiceDescriptor* d, - const Options& options) { +inline std::string UniqueName(const std::string& name, + const ServiceDescriptor* d, + const Options& options) { return UniqueName(name, d->file(), options); } @@ -222,32 +245,38 @@ inline Options InternalRuntimeOptions() { options.opensource_runtime = false; return options; } -inline string UniqueName(const string& name, const string& filename) { +inline std::string UniqueName(const std::string& name, + const std::string& filename) { return UniqueName(name, filename, InternalRuntimeOptions()); } -inline string UniqueName(const string& name, const FileDescriptor* d) { +inline std::string UniqueName(const std::string& name, + const FileDescriptor* d) { return UniqueName(name, d->name(), InternalRuntimeOptions()); } -inline string UniqueName(const string& name, const Descriptor* d) { +inline std::string UniqueName(const std::string& name, const Descriptor* d) { return UniqueName(name, d->file(), InternalRuntimeOptions()); } -inline string UniqueName(const string& name, const EnumDescriptor* d) { +inline std::string UniqueName(const std::string& name, + const EnumDescriptor* d) { return UniqueName(name, d->file(), InternalRuntimeOptions()); } -inline string UniqueName(const string& name, const ServiceDescriptor* d) { +inline std::string UniqueName(const std::string& name, + const ServiceDescriptor* d) { return UniqueName(name, d->file(), InternalRuntimeOptions()); } // Return the qualified C++ name for a file level symbol. -string QualifiedFileLevelSymbol(const string& package, const string& name); +std::string QualifiedFileLevelSymbol(const FileDescriptor* file, + const std::string& name, + const Options& options); // Escape C++ trigraphs by escaping question marks to \? -string EscapeTrigraphs(const string& to_escape); +std::string EscapeTrigraphs(const std::string& to_escape); // Escaped function name to eliminate naming conflict. -string SafeFunctionName(const Descriptor* descriptor, - const FieldDescriptor* field, - const string& prefix); +std::string SafeFunctionName(const Descriptor* descriptor, + const FieldDescriptor* field, + const std::string& prefix); // Returns true if generated messages have public unknown fields accessors inline bool PublicUnknownFieldsAccessors(const Descriptor* message) { @@ -273,6 +302,8 @@ inline bool IsWeak(const FieldDescriptor* field, const Options& options) { return false; } +bool IsStringInlined(const FieldDescriptor* descriptor, const Options& options); + // For a string field, returns the effective ctype. If the actual ctype is // not supported, returns the default of STRING. FieldOptions::CType EffectiveStringCType(const FieldDescriptor* field, @@ -344,17 +375,11 @@ inline bool HasGenericServices(const FileDescriptor* file, file->options().cc_generic_services(); } -// Should we generate a separate, super-optimized code path for serializing to -// flat arrays? We don't do this in Lite mode because we'd rather reduce code -// size. -inline bool HasFastArraySerialization(const FileDescriptor* file, - const Options& options) { - return GetOptimizeFor(file, options) == FileOptions::SPEED; -} - inline bool IsProto2MessageSet(const Descriptor* descriptor, const Options& options) { return !options.opensource_runtime && + options.enforce_mode != EnforceOptimizeMode::kLiteRuntime && + !options.lite_implicit_weak_fields && descriptor->options().message_set_wire_format() && descriptor->full_name() == "google.protobuf.bridge.MessageSet"; } @@ -362,6 +387,8 @@ inline bool IsProto2MessageSet(const Descriptor* descriptor, inline bool IsProto2MessageSetFile(const FileDescriptor* file, const Options& options) { return !options.opensource_runtime && + options.enforce_mode != EnforceOptimizeMode::kLiteRuntime && + !options.lite_implicit_weak_fields && file->name() == "net/proto2/bridge/proto/message_set.proto"; } @@ -372,7 +399,8 @@ inline bool IsMapEntryMessage(const Descriptor* descriptor) { // Returns true if the field's CPPTYPE is string or message. bool IsStringOrMessage(const FieldDescriptor* field); -string UnderscoresToCamelCase(const string& input, bool cap_next_letter); +std::string UnderscoresToCamelCase(const std::string& input, + bool cap_next_letter); inline bool HasFieldPresence(const FileDescriptor* file) { return file->syntax() != FileDescriptor::SYNTAX_PROTO3; @@ -380,8 +408,8 @@ inline bool HasFieldPresence(const FileDescriptor* file) { // Returns true if 'enum' semantics are such that unknown values are preserved // in the enum field itself, rather than going to the UnknownFieldSet. -inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) { - return file->syntax() == FileDescriptor::SYNTAX_PROTO3; +inline bool HasPreservingUnknownEnumSemantics(const FieldDescriptor* field) { + return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3; } inline bool SupportsArenas(const FileDescriptor* file) { @@ -401,25 +429,55 @@ inline bool IsCrossFileMessage(const FieldDescriptor* field) { field->message_type()->file() != field->file(); } -inline string MessageCreateFunction(const Descriptor* d) { +inline std::string MessageCreateFunction(const Descriptor* d) { return SupportsArenas(d) ? "CreateMessage" : "Create"; } -inline string MakeDefaultName(const FieldDescriptor* field) { +inline std::string MakeDefaultName(const FieldDescriptor* field) { return "_i_give_permission_to_break_this_code_default_" + FieldName(field) + "_"; } -bool IsAnyMessage(const FileDescriptor* descriptor); -bool IsAnyMessage(const Descriptor* descriptor); +bool IsAnyMessage(const FileDescriptor* descriptor, const Options& options); +bool IsAnyMessage(const Descriptor* descriptor, const Options& options); bool IsWellKnownMessage(const FileDescriptor* descriptor); +inline std::string IncludeGuard(const FileDescriptor* file, bool pb_h, + const Options& options) { + // If we are generating a .pb.h file and the proto_h option is enabled, then + // the .pb.h gets an extra suffix. + std::string filename_identifier = FilenameIdentifier( + file->name() + (pb_h && options.proto_h ? ".pb.h" : "")); + + if (IsWellKnownMessage(file)) { + // For well-known messages we need third_party/protobuf and net/proto2 to + // have distinct include guards, because some source files include both and + // both need to be defined (the third_party copies will be in the + // google::protobuf_opensource namespace). + return MacroPrefix(options) + "_INCLUDED_" + filename_identifier; + } else { + // Ideally this case would use distinct include guards for opensource and + // google3 protos also. (The behavior of "first #included wins" is not + // ideal). But unfortunately some legacy code includes both and depends on + // the identical include guards to avoid compile errors. + // + // We should clean this up so that this case can be removed. + return "GOOGLE_PROTOBUF_INCLUDED_" + filename_identifier; + } +} + inline FileOptions_OptimizeMode GetOptimizeFor(const FileDescriptor* file, const Options& options) { - return options.enforce_lite - ? FileOptions::LITE_RUNTIME - : file->options().optimize_for(); + switch (options.enforce_mode) { + case EnforceOptimizeMode::kSpeed: + return FileOptions::SPEED; + case EnforceOptimizeMode::kLiteRuntime: + return FileOptions::LITE_RUNTIME; + case EnforceOptimizeMode::kNoEnforcement: + default: + return file->options().optimize_for(); + } } // This orders the messages in a .pb.cc as it's outputted by file.cc @@ -455,7 +513,7 @@ struct MessageAnalysis { // quadratic performance, if we do this per message we would get O(V*(V+E)). // Logically this is just only used in message.cc, but in the header for // FileGenerator to help share it. -class LIBPROTOC_EXPORT MessageSCCAnalyzer { +class PROTOC_EXPORT MessageSCCAnalyzer { public: explicit MessageSCCAnalyzer(const Options& options) : options_(options) {} @@ -470,15 +528,55 @@ class LIBPROTOC_EXPORT MessageSCCAnalyzer { } private: - SCCAnalyzer analyzer_; + struct DepsGenerator { + std::vector operator()(const Descriptor* desc) const { + std::vector deps; + for (int i = 0; i < desc->field_count(); i++) { + if (desc->field(i)->message_type()) { + deps.push_back(desc->field(i)->message_type()); + } + } + return deps; + } + }; + SCCAnalyzer analyzer_; Options options_; std::map analysis_cache_; }; +inline std::string SccInfoSymbol(const SCC* scc, const Options& options) { + return UniqueName("scc_info_" + ClassName(scc->GetRepresentative()), + scc->GetRepresentative(), options); +} + void ListAllFields(const Descriptor* d, std::vector* fields); void ListAllFields(const FileDescriptor* d, std::vector* fields); + +template +void ForEachField(const Descriptor* d, T&& func) { + for (int i = 0; i < d->nested_type_count(); i++) { + ForEachField(d->nested_type(i), std::forward(func)); + } + for (int i = 0; i < d->extension_count(); i++) { + func(d->extension(i)); + } + for (int i = 0; i < d->field_count(); i++) { + func(d->field(i)); + } +} + +template +void ForEachField(const FileDescriptor* d, T&& func) { + for (int i = 0; i < d->message_type_count(); i++) { + ForEachField(d->message_type(i), std::forward(func)); + } + for (int i = 0; i < d->extension_count(); i++) { + func(d->extension(i)); + } +} + void ListAllTypesForServices(const FileDescriptor* fd, std::vector* types); @@ -528,18 +626,19 @@ bool IsImplicitWeakField(const FieldDescriptor* field, const Options& options, // "__declspec(export) void fun();" // // which is convenient to prevent double, leading or trailing spaces. -class LIBPROTOC_EXPORT Formatter { +class PROTOC_EXPORT Formatter { public: explicit Formatter(io::Printer* printer) : printer_(printer) {} - Formatter(io::Printer* printer, const std::map& vars) + Formatter(io::Printer* printer, + const std::map& vars) : printer_(printer), vars_(vars) {} template - void Set(const string& key, const T& value) { + void Set(const std::string& key, const T& value) { vars_[key] = ToString(value); } - void AddMap(const std::map& vars) { + void AddMap(const std::map& vars) { for (const auto& keyval : vars) vars_[keyval.first] = keyval.second; } @@ -552,7 +651,7 @@ class LIBPROTOC_EXPORT Formatter { void Outdent() const { printer_->Outdent(); } io::Printer* printer() const { return printer_; } - class LIBPROTOC_EXPORT SaveState { + class PROTOC_EXPORT SaveState { public: explicit SaveState(Formatter* format) : format_(format), vars_(format->vars_) {} @@ -560,28 +659,31 @@ class LIBPROTOC_EXPORT Formatter { private: Formatter* format_; - std::map vars_; + std::map vars_; }; private: io::Printer* printer_; - std::map vars_; + std::map vars_; // Convenience overloads to accept different types as arguments. - static string ToString(const string& s) { return s; } + static std::string ToString(const std::string& s) { return s; } template ::value>::type> - static string ToString(I x) { - return SimpleItoa(x); + static std::string ToString(I x) { + return StrCat(x); } - static string ToString(strings::Hex x) { return StrCat(x); } - static string ToString(const FieldDescriptor* d) { return Payload(d); } - static string ToString(const Descriptor* d) { return Payload(d); } - static string ToString(const EnumDescriptor* d) { return Payload(d); } - static string ToString(const EnumValueDescriptor* d) { return Payload(d); } + static std::string ToString(strings::Hex x) { return StrCat(x); } + static std::string ToString(const FieldDescriptor* d) { return Payload(d); } + static std::string ToString(const Descriptor* d) { return Payload(d); } + static std::string ToString(const EnumDescriptor* d) { return Payload(d); } + static std::string ToString(const EnumValueDescriptor* d) { + return Payload(d); + } + static std::string ToString(const OneofDescriptor* d) { return Payload(d); } template - static string Payload(const Descriptor* descriptor) { + static std::string Payload(const Descriptor* descriptor) { std::vector path; descriptor->GetLocationPath(&path); GeneratedCodeInfo::Annotation annotation; @@ -593,18 +695,18 @@ class LIBPROTOC_EXPORT Formatter { } }; -class LIBPROTOC_EXPORT NamespaceOpener { +class PROTOC_EXPORT NamespaceOpener { public: explicit NamespaceOpener(const Formatter& format) : printer_(format.printer()) {} - NamespaceOpener(const string& name, const Formatter& format) + NamespaceOpener(const std::string& name, const Formatter& format) : NamespaceOpener(format) { ChangeTo(name); } ~NamespaceOpener() { ChangeTo(""); } - void ChangeTo(const string& name) { - std::vector new_stack_ = + void ChangeTo(const std::string& name) { + std::vector new_stack_ = Split(name, "::", true); int len = std::min(name_stack_.size(), new_stack_.size()); int common_idx = 0; @@ -613,20 +715,28 @@ class LIBPROTOC_EXPORT NamespaceOpener { common_idx++; } for (int i = name_stack_.size() - 1; i >= common_idx; i--) { - printer_->Print("} // namespace $ns$\n", "ns", name_stack_[i]); + if (name_stack_[i] == "PROTOBUF_NAMESPACE_ID") { + printer_->Print("PROTOBUF_NAMESPACE_CLOSE\n"); + } else { + printer_->Print("} // namespace $ns$\n", "ns", name_stack_[i]); + } } name_stack_.swap(new_stack_); for (int i = common_idx; i < name_stack_.size(); i++) { - printer_->Print("namespace $ns$ {\n", "ns", name_stack_[i]); + if (name_stack_[i] == "PROTOBUF_NAMESPACE_ID") { + printer_->Print("PROTOBUF_NAMESPACE_OPEN\n"); + } else { + printer_->Print("namespace $ns$ {\n", "ns", name_stack_[i]); + } } } private: io::Printer* printer_; - std::vector name_stack_; + std::vector name_stack_; }; -string GetUtf8Suffix(const FieldDescriptor* field, const Options& options); +std::string GetUtf8Suffix(const FieldDescriptor* field, const Options& options); void GenerateUtf8CheckCodeForString(const FieldDescriptor* field, const Options& options, bool for_parse, const char* parameters, @@ -700,16 +810,15 @@ struct OneOfRangeImpl { }; Iterator begin() const { return {0, descriptor}; } - Iterator end() const { - return {descriptor->oneof_decl_count(), descriptor}; - } + Iterator end() const { return {descriptor->oneof_decl_count(), descriptor}; } const Descriptor* descriptor; }; inline OneOfRangeImpl OneOfRange(const Descriptor* desc) { return {desc}; } -void GenerateParserLoop(const Descriptor* descriptor, const Options& options, +void GenerateParserLoop(const Descriptor* descriptor, int num_hasbits, + const Options& options, MessageSCCAnalyzer* scc_analyzer, io::Printer* printer); } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.cc b/src/google/protobuf/compiler/cpp/cpp_map_field.cc index 4ab407d2a2..0c4634bb6b 100644 --- a/src/google/protobuf/compiler/cpp/cpp_map_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_map_field.cc @@ -46,15 +46,10 @@ bool IsProto3Field(const FieldDescriptor* field_descriptor) { } void SetMessageVariables(const FieldDescriptor* descriptor, - std::map* variables, + std::map* variables, const Options& options) { SetCommonFieldVariables(descriptor, variables, options); (*variables)["type"] = ClassName(descriptor->message_type(), false); - (*variables)["stream_writer"] = - (*variables)["declared_type"] + - (HasFastArraySerialization(descriptor->message_type()->file(), options) - ? "MaybeToArray" - : ""); (*variables)["full_name"] = descriptor->full_name(); const FieldDescriptor* key = @@ -64,25 +59,21 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["key_cpp"] = PrimitiveTypeName(options, key->cpp_type()); switch (val->cpp_type()) { case FieldDescriptor::CPPTYPE_MESSAGE: - (*variables)["val_cpp"] = FieldMessageTypeName(val); - (*variables)["wrapper"] = "EntryWrapper"; + (*variables)["val_cpp"] = FieldMessageTypeName(val, options); break; case FieldDescriptor::CPPTYPE_ENUM: (*variables)["val_cpp"] = ClassName(val->enum_type(), true); - (*variables)["wrapper"] = "EnumEntryWrapper"; break; default: (*variables)["val_cpp"] = PrimitiveTypeName(options, val->cpp_type()); - (*variables)["wrapper"] = "EntryWrapper"; } (*variables)["key_wire_type"] = "TYPE_" + ToUpper(DeclaredTypeMethodName(key->type())); (*variables)["val_wire_type"] = "TYPE_" + ToUpper(DeclaredTypeMethodName(val->type())); (*variables)["map_classname"] = ClassName(descriptor->message_type(), false); - (*variables)["number"] = SimpleItoa(descriptor->number()); - (*variables)["tag"] = - SimpleItoa(internal::WireFormat::MakeTag(descriptor)); + (*variables)["number"] = StrCat(descriptor->number()); + (*variables)["tag"] = StrCat(internal::WireFormat::MakeTag(descriptor)); if (HasDescriptorMethods(descriptor->file(), options)) { (*variables)["lite"] = ""; @@ -90,8 +81,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["lite"] = "Lite"; } - if (!IsProto3Field(descriptor) && - val->type() == FieldDescriptor::TYPE_ENUM) { + if (!IsProto3Field(descriptor) && val->type() == FieldDescriptor::TYPE_ENUM) { const EnumValueDescriptor* default_value = val->default_value_enum(); (*variables)["default_enum_value"] = Int32ToString(default_value->number()); } else { @@ -107,8 +97,7 @@ MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor, MapFieldGenerator::~MapFieldGenerator() {} -void MapFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void MapFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const { Formatter format(printer, variables_); format( "::$proto_ns$::internal::MapField$lite$<\n" @@ -119,8 +108,8 @@ GeneratePrivateMembers(io::Printer* printer) const { " $default_enum_value$ > $name$_;\n"); } -void MapFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void MapFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$const ::$proto_ns$::Map< $key_cpp$, $val_cpp$ >&\n" @@ -130,101 +119,95 @@ GenerateAccessorDeclarations(io::Printer* printer) const { descriptor_); } -void MapFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void MapFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline const ::$proto_ns$::Map< $key_cpp$, $val_cpp$ >&\n" "$classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_map:$full_name$)\n" " return $name$_.GetMap();\n" "}\n" "inline ::$proto_ns$::Map< $key_cpp$, $val_cpp$ >*\n" "$classname$::mutable_$name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_mutable_map:$full_name$)\n" " return $name$_.MutableMap();\n" "}\n"); } -void MapFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void MapFieldGenerator::GenerateClearingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.Clear();\n"); } -void MapFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void MapFieldGenerator::GenerateMergingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.MergeFrom(from.$name$_);\n"); } -void MapFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void MapFieldGenerator::GenerateSwappingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.Swap(&other->$name$_);\n"); } -void MapFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void MapFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { GenerateConstructorCode(printer); GenerateMergingCode(printer); } -void MapFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void MapFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); const FieldDescriptor* key_field = descriptor_->message_type()->FindFieldByName("key"); const FieldDescriptor* value_field = descriptor_->message_type()->FindFieldByName("value"); - bool using_entry = false; - string key; - string value; + std::string key; + std::string value; + format( + "$map_classname$::Parser< ::$proto_ns$::internal::MapField$lite$<\n" + " $map_classname$,\n" + " $key_cpp$, $val_cpp$,\n" + " ::$proto_ns$::internal::WireFormatLite::$key_wire_type$,\n" + " ::$proto_ns$::internal::WireFormatLite::$val_wire_type$,\n" + " $default_enum_value$ >,\n" + " ::$proto_ns$::Map< $key_cpp$, $val_cpp$ > >" + " parser(&$name$_);\n"); if (IsProto3Field(descriptor_) || value_field->type() != FieldDescriptor::TYPE_ENUM) { format( - "$map_classname$::Parser< ::$proto_ns$::internal::MapField$lite$<\n" - " $map_classname$,\n" - " $key_cpp$, $val_cpp$,\n" - " ::$proto_ns$::internal::WireFormatLite::$key_wire_type$,\n" - " ::$proto_ns$::internal::WireFormatLite::$val_wire_type$,\n" - " $default_enum_value$ >,\n" - " ::$proto_ns$::Map< $key_cpp$, $val_cpp$ > >" - " parser(&$name$_);\n" "DO_(::$proto_ns$::internal::WireFormatLite::ReadMessageNoVirtual(\n" " input, &parser));\n"); key = "parser.key()"; value = "parser.value()"; } else { - using_entry = true; key = "entry->key()"; value = "entry->value()"; - format("::std::unique_ptr<$map_classname$> entry($name$_.NewEntry());\n"); + format("auto entry = parser.NewEntry();\n"); format( - "{\n" - " ::std::string data;\n" - " DO_(::$proto_ns$::internal::WireFormatLite::ReadString(input, " + "std::string data;\n" + "DO_(::$proto_ns$::internal::WireFormatLite::ReadString(input, " "&data));\n" - " DO_(entry->ParseFromString(data));\n" - " if ($val_cpp$_IsValid(*entry->mutable_value())) {\n" - " (*mutable_$name$())[entry->key()] =\n" - " static_cast< $val_cpp$ >(*entry->mutable_value());\n" - " } else {\n"); + "DO_(entry->ParseFromString(data));\n" + "if ($val_cpp$_IsValid(*entry->mutable_value())) {\n" + " (*mutable_$name$())[entry->key()] =\n" + " static_cast< $val_cpp$ >(*entry->mutable_value());\n" + "} else {\n"); if (HasDescriptorMethods(descriptor_->file(), options_)) { format( - " mutable_unknown_fields()" + " mutable_unknown_fields()" "->AddLengthDelimited($number$, data);\n"); } else { format( - " unknown_fields_stream.WriteVarint32($tag$u);\n" - " unknown_fields_stream.WriteVarint32(\n" - " static_cast< ::google::protobuf::uint32>(data.size()));\n" - " unknown_fields_stream.WriteString(data);\n"); + " unknown_fields_stream.WriteVarint32($tag$u);\n" + " unknown_fields_stream.WriteVarint32(\n" + " static_cast< ::google::protobuf::uint32>(data.size()));\n" + " unknown_fields_stream.WriteString(data);\n"); } - - format( - " }\n" - "}\n"); + format("}\n"); } if (key_field->type() == FieldDescriptor::TYPE_STRING) { @@ -242,19 +225,12 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { .data(), format); } - - // If entry is allocated by arena, its desctructor should be avoided. - if (using_entry && SupportsArenas(descriptor_)) { - format("if (entry->GetArena() != NULL) entry.release();\n"); - } } -static void GenerateSerializationLoop(const Formatter& format, - bool supports_arenas, bool string_key, - bool string_value, bool to_array, +static void GenerateSerializationLoop(const Formatter& format, bool string_key, + bool string_value, bool is_deterministic) { - format("::std::unique_ptr<$map_classname$> entry;\n"); - string ptr; + std::string ptr; if (is_deterministic) { format("for (size_type i = 0; i < n; i++) {\n"); ptr = string_key ? "items[static_cast(i)]" @@ -268,26 +244,10 @@ static void GenerateSerializationLoop(const Formatter& format, } format.Indent(); - format("entry.reset($name$_.New$wrapper$($1$->first, $1$->second));\n", ptr); - if (to_array) { - format( - "target = ::$proto_ns$::internal::WireFormatLite::InternalWrite" - "$declared_type$NoVirtualToArray($number$, *entry, deterministic, " - "target);\n"); - } else { - format( - "::$proto_ns$::internal::WireFormatLite::Write$stream_writer$($number$," - " " - "*entry, output);\n"); - } - - // If entry is allocated by arena, its desctructor should be avoided. - if (supports_arenas) { - format( - "if (entry->GetArena() != NULL) {\n" - " entry.release();\n" - "}\n"); - } + format( + "target = $map_classname$::Funcs::InternalSerialize($number$, " + "$1$->first, $1$->second, target, stream);\n", + ptr); if (string_key || string_value) { // ptr is either an actual pointer or an iterator, either way we can @@ -299,18 +259,8 @@ static void GenerateSerializationLoop(const Formatter& format, format("}\n"); } -void MapFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { - GenerateSerializeWithCachedSizes(printer, false); -} - -void MapFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { - GenerateSerializeWithCachedSizes(printer, true); -} - -void MapFieldGenerator::GenerateSerializeWithCachedSizes(io::Printer* printer, - bool to_array) const { +void MapFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); format("if (!this->$name$().empty()) {\n"); format.Indent(); @@ -362,7 +312,7 @@ void MapFieldGenerator::GenerateSerializeWithCachedSizes(io::Printer* printer, format( "\n" - "if ($1$ &&\n" + "if (stream->IsSerializationDeterministic() &&\n" " this->$name$().size() > 1) {\n" " ::std::unique_ptr items(\n" " new SortItem[this->$name$().size()]);\n" @@ -374,57 +324,30 @@ void MapFieldGenerator::GenerateSerializeWithCachedSizes(io::Printer* printer, " it != this->$name$().end(); ++it, ++n) {\n" " items[static_cast(n)] = SortItem(&*it);\n" " }\n" - " ::std::sort(&items[0], &items[static_cast(n)], Less());\n", - to_array ? "deterministic" : "output->IsSerializationDeterministic()"); + " ::std::sort(&items[0], &items[static_cast(n)], Less());\n"); format.Indent(); - GenerateSerializationLoop(format, SupportsArenas(descriptor_), string_key, - string_value, to_array, true); + GenerateSerializationLoop(format, string_key, string_value, true); format.Outdent(); format("} else {\n"); format.Indent(); - GenerateSerializationLoop(format, SupportsArenas(descriptor_), string_key, - string_value, to_array, false); + GenerateSerializationLoop(format, string_key, string_value, false); format.Outdent(); format("}\n"); format.Outdent(); format("}\n"); } -void MapFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void MapFieldGenerator::GenerateByteSize(io::Printer* printer) const { Formatter format(printer, variables_); format( "total_size += $tag_size$ *\n" " ::$proto_ns$::internal::FromIntSize(this->$name$_size());\n" - "{\n" - " ::std::unique_ptr<$map_classname$> entry;\n" - " for (::$proto_ns$::Map< $key_cpp$, $val_cpp$ >::const_iterator\n" - " it = this->$name$().begin();\n" - " it != this->$name$().end(); ++it) {\n"); - - // If entry is allocated by arena, its desctructor should be avoided. - if (SupportsArenas(descriptor_)) { - format( - " if (entry.get() != NULL && entry->GetArena() != NULL) {\n" - " entry.release();\n" - " }\n"); - } - - format( - " entry.reset($name$_.New$wrapper$(it->first, it->second));\n" - " total_size += ::$proto_ns$::internal::WireFormatLite::\n" - " $declared_type$SizeNoVirtual(*entry);\n" - " }\n"); - - // If entry is allocated by arena, its desctructor should be avoided. - if (SupportsArenas(descriptor_)) { - format( - " if (entry.get() != NULL && entry->GetArena() != NULL) {\n" - " entry.release();\n" - " }\n"); - } - - format("}\n"); + "for (::$proto_ns$::Map< $key_cpp$, $val_cpp$ >::const_iterator\n" + " it = this->$name$().begin();\n" + " it != this->$name$().end(); ++it) {\n" + " total_size += $map_classname$::Funcs::ByteSizeLong(it->first, " + "it->second);\n" + "}\n"); } } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.h b/src/google/protobuf/compiler/cpp/cpp_map_field.h index 95eecc07ee..45a70c43f3 100644 --- a/src/google/protobuf/compiler/cpp/cpp_map_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_map_field.h @@ -56,15 +56,10 @@ class MapFieldGenerator : public FieldGenerator { void GenerateConstructorCode(io::Printer* printer) const {} void GenerateCopyConstructorCode(io::Printer* printer) const; void GenerateMergeFromCodedStream(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; private: - // A helper for GenerateSerializeWithCachedSizes{,ToArray}. - void GenerateSerializeWithCachedSizes(io::Printer* printer, - bool to_array) const; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapFieldGenerator); }; diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc index e219d78432..8dba16379f 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message.cc @@ -78,7 +78,7 @@ void PrintFieldComment(const Formatter& format, const T* field) { DebugStringOptions options; options.elide_group_body = true; options.elide_oneof_body = true; - string def = field->DebugStringWithOptions(options); + std::string def = field->DebugStringWithOptions(options); format("// $1$\n", def.substr(0, def.find_first_of('\n'))); } @@ -91,7 +91,7 @@ void PrintPresenceCheck(const Formatter& format, const FieldDescriptor* field, *cached_has_bit_index = (has_bit_index / 32); format("cached_has_bits = _has_bits_[$1$];\n", *cached_has_bit_index); } - const string mask = + const std::string mask = StrCat(strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); format("if (cached_has_bits & 0x$1$u) {\n", mask); } else { @@ -154,8 +154,7 @@ bool CanConstructByZeroing(const FieldDescriptor* field, // Non-repeated, non-lazy message fields are simply raw pointers, so we can // use memset to initialize these in SharedCtor. We cannot use this in // Clear, as we need to potentially delete the existing value. - ret = ret || (!field->is_repeated() && - !IsLazy(field, options) && + ret = ret || (!field->is_repeated() && !IsLazy(field, options) && field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE); return ret; } @@ -164,7 +163,8 @@ bool CanConstructByZeroing(const FieldDescriptor* field, // considered non-default (will be sent over the wire), for message types // without true field presence. Should only be called if // !HasFieldPresence(message_descriptor). -bool EmitFieldNonDefaultCondition(io::Printer* printer, const string& prefix, +bool EmitFieldNonDefaultCondition(io::Printer* printer, + const std::string& prefix, const FieldDescriptor* field) { Formatter format(printer); format.Set("prefix", prefix); @@ -177,6 +177,10 @@ bool EmitFieldNonDefaultCondition(io::Printer* printer, const string& prefix, } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { // Message fields still have has_$name$() methods. format("if ($prefix$has_$name$()) {\n"); + } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_DOUBLE || + field->cpp_type() == FieldDescriptor::CPPTYPE_FLOAT) { + // Handle float comparison to prevent -Wfloat-equal warnings + format("if (!($prefix$$name$() <= 0 && $prefix$$name$() >= 0)) {\n"); } else { format("if ($prefix$$name$() != 0) {\n"); } @@ -203,15 +207,15 @@ bool HasHasMethod(const FieldDescriptor* field) { // Collects map entry message type information. void CollectMapInfo(const Options& options, const Descriptor* descriptor, - std::map* variables) { + std::map* variables) { GOOGLE_CHECK(IsMapEntryMessage(descriptor)); - std::map& vars = *variables; + std::map& vars = *variables; const FieldDescriptor* key = descriptor->FindFieldByName("key"); const FieldDescriptor* val = descriptor->FindFieldByName("value"); vars["key_cpp"] = PrimitiveTypeName(options, key->cpp_type()); switch (val->cpp_type()) { case FieldDescriptor::CPPTYPE_MESSAGE: - vars["val_cpp"] = FieldMessageTypeName(val); + vars["val_cpp"] = FieldMessageTypeName(val, options); break; case FieldDescriptor::CPPTYPE_ENUM: vars["val_cpp"] = ClassName(val->enum_type(), true); @@ -244,42 +248,37 @@ bool HasPrivateHasMethod(const FieldDescriptor* field) { // TODO(ckennelly): Cull these exclusions if/when these protos do not have // their methods overriden by subclasses. +bool ShouldMarkClassAsFinal(const Descriptor* descriptor, + const Options& options) { + return false; +} + bool ShouldMarkClearAsFinal(const Descriptor* descriptor, const Options& options) { - static std::set exclusions{ + static std::set exclusions{ }; - const string name = ClassName(descriptor, true); + const std::string name = ClassName(descriptor, true); return exclusions.find(name) == exclusions.end() || options.opensource_runtime; } bool ShouldMarkIsInitializedAsFinal(const Descriptor* descriptor, const Options& options) { - static std::set exclusions{ - }; - - const string name = ClassName(descriptor, true); - return exclusions.find(name) == exclusions.end() || - options.opensource_runtime; -} - -bool ShouldMarkMergePartialAsFinal(const Descriptor* descriptor, - const Options& options) { - static std::set exclusions{ + static std::set exclusions{ }; - const string name = ClassName(descriptor, true); + const std::string name = ClassName(descriptor, true); return exclusions.find(name) == exclusions.end() || options.opensource_runtime; } bool ShouldMarkNewAsFinal(const Descriptor* descriptor, const Options& options) { - static std::set exclusions{ + static std::set exclusions{ }; - const string name = ClassName(descriptor, true); + const std::string name = ClassName(descriptor, true); return exclusions.find(name) == exclusions.end() || options.opensource_runtime; } @@ -337,8 +336,8 @@ bool TableDrivenParsingEnabled(const Descriptor* descriptor, void SetUnknkownFieldsVariable(const Descriptor* descriptor, const Options& options, - std::map* variables) { - string proto_ns = ProtobufNamespace(options); + std::map* variables) { + std::string proto_ns = ProtobufNamespace(options); if (UseUnknownFieldSet(descriptor->file(), options)) { (*variables)["unknown_fields_type"] = "::" + proto_ns + "::UnknownFieldSet"; } else { @@ -431,10 +430,10 @@ class MatchRepeatedAndHasByteAndZeroInits : public MatchRepeatedAndHasByte { // Collects neighboring fields based on a given criteria (equivalent predicate). template -std::vector > CollectFields( +std::vector> CollectFields( const std::vector& fields, const Predicate& equivalent) { - std::vector > chunks; + std::vector> chunks; if (fields.empty()) { return chunks; } @@ -503,8 +502,8 @@ class ColdChunkSkipper { // May open an external if check for a batch of cold fields. "from" is the // prefix to _has_bits_ to allow MergeFrom to use "from._has_bits_". // Otherwise, it should be "". - void OnStartChunk(int chunk, int cached_has_bit_index, const string& from, - io::Printer* printer); + void OnStartChunk(int chunk, int cached_has_bit_index, + const std::string& from, io::Printer* printer); bool OnEndChunk(int chunk, io::Printer* printer); private: @@ -514,11 +513,11 @@ class ColdChunkSkipper { return has_bit_indices_[chunks_[chunk][offset]->index()] / 32; } - const std::vector >& chunks_; + const std::vector>& chunks_; const std::vector& has_bit_indices_; const AccessInfoMap* access_info_map_; const double cold_threshold_; - std::map variables_; + std::map variables_; int limit_chunk_ = -1; bool has_field_presence_; }; @@ -526,10 +525,16 @@ class ColdChunkSkipper { // Tuning parameters for ColdChunkSkipper. const double kColdRatio = 0.005; -bool ColdChunkSkipper::IsColdChunk(int chunk) { return false; } +bool ColdChunkSkipper::IsColdChunk(int chunk) { + // Mark this variable as used until it is actually used + (void)cold_threshold_; + return false; +} + void ColdChunkSkipper::OnStartChunk(int chunk, int cached_has_bit_index, - const string& from, io::Printer* printer) { + const std::string& from, + io::Printer* printer) { Formatter format(printer, variables_); if (!access_info_map_ || !has_field_presence_) { return; @@ -554,7 +559,7 @@ void ColdChunkSkipper::OnStartChunk(int chunk, int cached_has_bit_index, } // Emit has_bit check for each has_bit_dword index. - format("if ($GOOGLE_PROTOBUF$_PREDICT_FALSE("); + format("if (PROTOBUF_PREDICT_FALSE("); int first_word = HasbitWord(chunk, 0); while (chunk < limit_chunk_) { uint32 mask = 0; @@ -597,11 +602,10 @@ bool ColdChunkSkipper::OnEndChunk(int chunk, io::Printer* printer) { // =================================================================== -MessageGenerator::MessageGenerator(const Descriptor* descriptor, - const std::map& vars, - int index_in_file_messages, - const Options& options, - MessageSCCAnalyzer* scc_analyzer) +MessageGenerator::MessageGenerator( + const Descriptor* descriptor, + const std::map& vars, int index_in_file_messages, + const Options& options, MessageSCCAnalyzer* scc_analyzer) : descriptor_(descriptor), index_in_file_messages_(index_in_file_messages), classname_(ClassName(descriptor, false)), @@ -617,10 +621,9 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor, // Variables that apply to this class variables_["classname"] = classname_; - variables_["classtype"] = QualifiedClassName(descriptor_); - string scc_name = - ClassName(scc_analyzer_->GetSCC(descriptor_)->GetRepresentative()); - variables_["scc_name"] = UniqueName(scc_name, descriptor_, options_); + variables_["classtype"] = QualifiedClassName(descriptor_, options); + variables_["scc_info"] = + SccInfoSymbol(scc_analyzer_->GetSCC(descriptor_), options_); variables_["full_name"] = descriptor_->full_name(); variables_["superclass"] = SuperClassName(descriptor_, options_); @@ -711,12 +714,24 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* printer) { ordered_fields.push_back(field); } + if (!ordered_fields.empty()) { + format("enum : int {\n"); + for (auto field : ordered_fields) { + Formatter::SaveState save(&format); + + std::map vars; + SetCommonFieldVariables(field, &vars, options_); + format.AddMap(vars); + format(" ${1$$2$$}$ = $number$,\n", field, FieldConstantName(field)); + } + format("};\n"); + } for (auto field : ordered_fields) { PrintFieldComment(format, field); Formatter::SaveState save(&format); - std::map vars; + std::map vars; SetCommonFieldVariables(field, &vars, options_); format.AddMap(vars); @@ -732,10 +747,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* printer) { field); } - format( - "$deprecated_attr$void ${1$clear_$name$$}$();\n" - "$deprecated_attr$static const int ${1$$2$$}$ = $number$;\n", - field, FieldConstantName(field)); + format("$deprecated_attr$void ${1$clear_$name$$}$();\n", field); // Generate type-specific accessor declarations. field_generators_.get(field).GenerateAccessorDeclarations(printer); @@ -762,8 +774,9 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* printer) { format.Set("oneof_name", oneof->name()); format.Set("camel_oneof_name", UnderscoresToCamelCase(oneof->name(), true)); format( - "void clear_$oneof_name$();\n" - "$camel_oneof_name$Case $oneof_name$_case() const;\n"); + "void ${1$clear_$oneof_name$$}$();\n" + "$camel_oneof_name$Case $oneof_name$_case() const;\n", + oneof); } } @@ -772,6 +785,7 @@ void MessageGenerator::GenerateSingularFieldHasBits( if (field->options().weak()) { format( "inline bool $classname$::has_$name$() const {\n" + "$annotate_accessor$" " return _weak_field_map_.Has($number$);\n" "}\n"); return; @@ -787,6 +801,7 @@ void MessageGenerator::GenerateSingularFieldHasBits( strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); format( "inline bool $classname$::has_$name$() const {\n" + "$annotate_accessor$" " return (_has_bits_[$has_array_index$] & 0x$has_mask$u) != 0;\n" "}\n"); } else { @@ -795,12 +810,15 @@ void MessageGenerator::GenerateSingularFieldHasBits( if (IsLazy(field, options_)) { format( "inline bool $classname$::has_$name$() const {\n" + "$annotate_accessor$" " return !$name$_.IsCleared();\n" "}\n"); } else { format( "inline bool $classname$::has_$name$() const {\n" - " return this != internal_default_instance() && $name$_ != NULL;\n" + "$annotate_accessor$" + " return this != internal_default_instance() " + "&& $name$_ != nullptr;\n" "}\n"); } } @@ -833,21 +851,24 @@ void MessageGenerator::GenerateOneofMemberHasBits(const FieldDescriptor* field, // _oneof_case_[index] against a constant everywhere). format( "inline bool $classname$::has_$name$() const {\n" + "$annotate_accessor$" " return $oneof_name$_case() == k$field_name$;\n" "}\n" "inline void $classname$::set_has_$name$() {\n" + "$annotate_accessor$" " _oneof_case_[$oneof_index$] = k$field_name$;\n" "}\n"); } void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field, - bool is_inline, - Formatter format) { + bool is_inline, Formatter format) { // Generate clear_$name$(). if (is_inline) { format("inline "); } - format("void $classname$::clear_$name$() {\n"); + format( + "void $classname$::clear_$name$() {\n" + "$annotate_accessor$"); format.Indent(); @@ -886,7 +907,7 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* printer) { for (auto field : FieldRange(descriptor_)) { PrintFieldComment(format, field); - std::map vars; + std::map vars; SetCommonFieldVariables(field, &vars, options_); Formatter::SaveState saver(&format); @@ -896,13 +917,14 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* printer) { if (field->is_repeated()) { format( "inline int $classname$::$name$_size() const {\n" + "$annotate_accessor$" " return $name$_.size();\n" "}\n"); } else if (field->containing_oneof()) { format.Set("field_name", UnderscoresToCamelCase(field->name(), true)); format.Set("oneof_name", field->containing_oneof()->name()); format.Set("oneof_index", - SimpleItoa(field->containing_oneof()->index())); + StrCat(field->containing_oneof()->index())); GenerateOneofMemberHasBits(field, format); } else { // Singular field. @@ -925,8 +947,11 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* printer) { void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { Formatter format(printer, variables_); + format.Set("class_final", + ShouldMarkClassAsFinal(descriptor_, options_) ? "final" : ""); + if (IsMapEntryMessage(descriptor_)) { - std::map vars; + std::map vars; CollectMapInfo(options_, descriptor_, &vars); vars["lite"] = HasDescriptorMethods(descriptor_->file(), options_) ? "" : "Lite"; @@ -939,10 +964,6 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { " ::$proto_ns$::internal::WireFormatLite::$val_wire_type$,\n" " $default_enum_value$ > {\n" "public:\n" - "#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n" - " static const char* _InternalParse(const char* begin, const char* " - "end, void* object, ::$proto_ns$::internal::ParseContext* ctx);\n" - "#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n" " typedef ::$proto_ns$::internal::MapEntry$lite$<$classname$, \n" " $key_cpp$, $val_cpp$,\n" " ::$proto_ns$::internal::WireFormatLite::$key_wire_type$,\n" @@ -954,11 +975,72 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { " static const $classname$* internal_default_instance() { return " "reinterpret_cast(&_$classname$_default_instance_); }\n"); + std::string suffix = GetUtf8Suffix(descriptor_->field(0), options_); + if (descriptor_->field(0)->type() == FieldDescriptor::TYPE_STRING && + !suffix.empty()) { + if (suffix == "UTF8") { + format( + " static bool ValidateKey(std::string* s) {\n" + " return ::$proto_ns$::internal::WireFormatLite::" + "VerifyUtf8String(s->data(), s->size(), " + "::$proto_ns$::internal::WireFormatLite::PARSE, \"$1$\");\n" + " }\n", + descriptor_->field(0)->full_name()); + } else { + GOOGLE_CHECK(suffix == "UTF8Verify"); + format( + " static bool ValidateKey(std::string* s) {\n" + "#ifndef NDEBUG\n" + " ::$proto_ns$::internal::WireFormatLite::VerifyUtf8String(\n" + " s->data(), s->size(), ::$proto_ns$::internal::" + "WireFormatLite::PARSE, \"$1$\");\n" + "#endif\n" + " return true;\n" + " }\n", + descriptor_->field(0)->full_name()); + } + } else { + format(" static bool ValidateKey(void*) { return true; }\n"); + } + if (descriptor_->field(1)->type() == FieldDescriptor::TYPE_STRING && + !suffix.empty()) { + if (suffix == "UTF8") { + format( + " static bool ValidateValue(std::string* s) {\n" + " return ::$proto_ns$::internal::WireFormatLite::" + "VerifyUtf8String(s->data(), s->size(), " + "::$proto_ns$::internal::WireFormatLite::PARSE, \"$1$\");\n" + " }\n", + descriptor_->field(1)->full_name()); + } else { + GOOGLE_CHECK(suffix == "UTF8Verify"); + format( + " static bool ValidateValue(std::string* s) {\n" + "#ifndef NDEBUG\n" + " ::$proto_ns$::internal::WireFormatLite::VerifyUtf8String(\n" + " s->data(), s->size(), ::$proto_ns$::internal::" + "WireFormatLite::PARSE, \"$1$\");\n" + "#endif\n" + " return true;\n" + " }\n", + descriptor_->field(1)->full_name()); + } + } else { + format(" static bool ValidateValue(void*) { return true; }\n"); + } if (HasDescriptorMethods(descriptor_->file(), options_)) { format( " void MergeFrom(const ::$proto_ns$::Message& other) final;\n" - " ::$proto_ns$::Metadata GetMetadata() const;\n" - "};\n"); + " ::$proto_ns$::Metadata GetMetadata() const final;\n" + " private:\n" + " static ::$proto_ns$::Metadata GetMetadataStatic() {\n" + " ::$proto_ns$::internal::AssignDescriptors(&::$desc_table$);\n" + " return ::$desc_table$.file_level_metadata[$1$];\n" + " }\n" + "\n" + " public:\n" + "};\n", + index_in_file_messages_); } else { format("};\n"); } @@ -966,9 +1048,9 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { } format( - "class $dllexport_decl $${1$$classname$$}$ : public $superclass$ " - "/* @@protoc_insertion_point(class_definition:$full_name$) */ " - "{\n", + "class $dllexport_decl $${1$$classname$$}$$ class_final$ :\n" + " public $superclass$ /* @@protoc_insertion_point(" + "class_definition:$full_name$) */ {\n", descriptor_); format(" public:\n"); format.Indent(); @@ -978,28 +1060,15 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "virtual ~$classname$();\n" "\n" "$classname$(const $classname$& from);\n" - "\n" - "inline $classname$& operator=(const $classname$& from) {\n" - " CopyFrom(from);\n" - " return *this;\n" - "}\n"); - - if (options_.table_driven_serialization) { - format( - "private:\n" - "const void* InternalGetTable() const;\n" - "public:\n" - "\n"); - } - - // Generate move constructor and move assignment operator. - format( - "#if LANG_CXX11\n" "$classname$($classname$&& from) noexcept\n" " : $classname$() {\n" " *this = ::std::move(from);\n" "}\n" "\n" + "inline $classname$& operator=(const $classname$& from) {\n" + " CopyFrom(from);\n" + " return *this;\n" + "}\n" "inline $classname$& operator=($classname$&& from) noexcept {\n" " if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {\n" " if (this != &from) InternalSwap(&from);\n" @@ -1008,9 +1077,17 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { " }\n" " return *this;\n" "}\n" - "#endif\n"); + "\n"); - std::map vars; + if (options_.table_driven_serialization) { + format( + "private:\n" + "const void* InternalGetTable() const;\n" + "public:\n" + "\n"); + } + + std::map vars; SetUnknkownFieldsVariable(descriptor_, options_, &vars); format.AddMap(vars); if (PublicUnknownFieldsAccessors(descriptor_)) { @@ -1044,7 +1121,25 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { !descriptor_->options().no_standard_descriptor_accessor()) { format( "static const ::$proto_ns$::Descriptor* descriptor() {\n" - " return default_instance().GetDescriptor();\n" + " return GetDescriptor();\n" + "}\n"); + } + + if (HasDescriptorMethods(descriptor_->file(), options_)) { + // These shadow non-static methods of the same names in Message. We + // redefine them here because calls directly on the generated class can be + // statically analyzed -- we know what descriptor types are being requested. + // It also avoids a vtable dispatch. + // + // We would eventually like to eliminate the methods in Message, and having + // this separate also lets us track calls to the base class methods + // separately. + format( + "static const ::$proto_ns$::Descriptor* GetDescriptor() {\n" + " return GetMetadataStatic().descriptor;\n" + "}\n" + "static const ::$proto_ns$::Reflection* GetReflection() {\n" + " return GetMetadataStatic().reflection;\n" "}\n"); } @@ -1058,7 +1153,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { format("enum $1$Case {\n", UnderscoresToCamelCase(oneof->name(), true)); format.Indent(); for (auto field : FieldRange(oneof)) { - string oneof_enum_case_field_name = + std::string oneof_enum_case_field_name = UnderscoresToCamelCase(field->name(), true); format("k$1$ = $2$,\n", oneof_enum_case_field_name, // 1 field->number()); // 2 @@ -1079,44 +1174,84 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "}\n" "static constexpr int kIndexInFileMessages =\n" " $1$;\n" - "\n", index_in_file_messages_); + "\n", + index_in_file_messages_); - if (SupportsArenas(descriptor_)) { - format("void UnsafeArenaSwap($classname$* other);\n"); - } - - if (IsAnyMessage(descriptor_)) { + if (IsAnyMessage(descriptor_, options_)) { format( "// implements Any -----------------------------------------------\n" - "\n" - "void PackFrom(const ::$proto_ns$::Message& message);\n" - "void PackFrom(const ::$proto_ns$::Message& message,\n" - " const $string$& type_url_prefix);\n" - "bool UnpackTo(::$proto_ns$::Message* message) const;\n" + "\n"); + if (HasDescriptorMethods(descriptor_->file(), options_)) { + format( + "void PackFrom(const ::$proto_ns$::Message& message);\n" + "void PackFrom(const ::$proto_ns$::Message& message,\n" + " const std::string& type_url_prefix);\n" + "bool UnpackTo(::$proto_ns$::Message* message) const;\n" + "static bool GetAnyFieldDescriptors(\n" + " const ::$proto_ns$::Message& message,\n" + " const ::$proto_ns$::FieldDescriptor** type_url_field,\n" + " const ::$proto_ns$::FieldDescriptor** value_field);\n"); + } else { + format( + "template \n" + "void PackFrom(const T& message) {\n" + " _any_metadata_.PackFrom(message);\n" + "}\n" + "template \n" + "void PackFrom(const T& message,\n" + " const std::string& type_url_prefix) {\n" + " _any_metadata_.PackFrom(message, type_url_prefix);" + "}\n" + "template \n" + "bool UnpackTo(T* message) const {\n" + " return _any_metadata_.UnpackTo(message);\n" + "}\n"); + } + format( "template bool Is() const {\n" " return _any_metadata_.Is();\n" "}\n" "static bool ParseAnyTypeUrl(const string& type_url,\n" - " string* full_type_name);\n" - "static bool GetAnyFieldDescriptors(\n" - " const ::$proto_ns$::Message& message,\n" - " const ::$proto_ns$::FieldDescriptor** type_url_field,\n" - " const ::$proto_ns$::FieldDescriptor** value_field);\n"); + " std::string* full_type_name);\n"); } format.Set("new_final", ShouldMarkNewAsFinal(descriptor_, options_) ? "final" : ""); format( - "void Swap($classname$* other);\n" "friend void swap($classname$& a, $classname$& b) {\n" " a.Swap(&b);\n" - "}\n" + "}\n"); + + if (SupportsArenas(descriptor_)) { + format( + "inline void Swap($classname$* other) {\n" + " if (other == this) return;\n" + " if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {\n" + " InternalSwap(other);\n" + " } else {\n" + " ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);\n" + " }\n" + "}\n" + "void UnsafeArenaSwap($classname$* other) {\n" + " if (other == this) return;\n" + " $DCHK$(GetArenaNoVirtual() == other->GetArenaNoVirtual());\n" + " InternalSwap(other);\n" + "}\n"); + } else { + format( + "inline void Swap($classname$* other) {\n" + " if (other == this) return;\n" + " InternalSwap(other);\n" + "}\n"); + } + + format( "\n" "// implements Message ----------------------------------------------\n" "\n" "inline $classname$* New() const$ new_final$ {\n" - " return CreateMaybeMessage<$classname$>(NULL);\n" + " return CreateMaybeMessage<$classname$>(nullptr);\n" "}\n" "\n" "$classname$* New(::$proto_ns$::Arena* arena) const$ new_final$ {\n" @@ -1145,62 +1280,49 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { format.Set( "is_initialized_final", ShouldMarkIsInitializedAsFinal(descriptor_, options_) ? "final" : ""); - format.Set( - "merge_partial_final", - ShouldMarkMergePartialAsFinal(descriptor_, options_) ? "final" : ""); format( "void CopyFrom(const $classname$& from);\n" "void MergeFrom(const $classname$& from);\n" - "void Clear()$ clear_final$;\n" + "PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear()$ clear_final$;\n" "bool IsInitialized() const$ is_initialized_final$;\n" "\n" "size_t ByteSizeLong() const final;\n" "#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n" - "static const char* _InternalParse(const char* begin, const char* end, " - "void* object, ::$proto_ns$::internal::ParseContext* ctx);\n" - "::$proto_ns$::internal::ParseFunc _ParseFunc() const final { return " - "_InternalParse; }\n" + "const char* _InternalParse(const char* ptr, " + "::$proto_ns$::internal::ParseContext* ctx) final;\n" "#else\n" "bool MergePartialFromCodedStream(\n" - " ::$proto_ns$::io::CodedInputStream* input)$ " - "merge_partial_final$;\n" - "#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"); - if (descriptor_->options().message_set_wire_format()) { - format( - "#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n" - "static const char* InternalParseMessageSetItem(const char* begin, " - "const char* end, void* object, " - "::$proto_ns$::internal::ParseContext* " - "ctx);\n" - "#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"); - } + " ::$proto_ns$::io::CodedInputStream* input) final;\n" + "#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n" + "$uint8$* InternalSerializeWithCachedSizesToArray(\n" + " $uint8$* target, ::$proto_ns$::io::EpsCopyOutputStream* stream) " + "const final;\n"); - if (!options_.table_driven_serialization || - descriptor_->options().message_set_wire_format()) { - format( - "void SerializeWithCachedSizes(\n" - " ::$proto_ns$::io::CodedOutputStream* output) const final;\n"); - } // DiscardUnknownFields() is implemented in message.cc using reflections. We // need to implement this function in generated code for messages. if (!UseUnknownFieldSet(descriptor_->file(), options_)) { format("void DiscardUnknownFields()$ full_final$;\n"); } - if (HasFastArraySerialization(descriptor_->file(), options_)) { - format( - "$uint8$* InternalSerializeWithCachedSizesToArray(\n" - " bool deterministic, $uint8$* target) const final;\n"); - } } format( "int GetCachedSize() const final { return _cached_size_.Get(); }" "\n\nprivate:\n" - "void SharedCtor();\n" - "void SharedDtor();\n" + "inline void SharedCtor();\n" + "inline void SharedDtor();\n" "void SetCachedSize(int size) const$ full_final$;\n" "void InternalSwap($classname$* other);\n"); + + format( + // Friend AnyMetadata so that it can call this FullMessageName() method. + "friend class ::$proto_ns$::internal::AnyMetadata;\n" + "static $1$ FullMessageName() {\n" + " return \"$full_name$\";\n" + "}\n", + options_.opensource_runtime ? "::PROTOBUF_NAMESPACE_ID::StringPiece" + : "::StringPiece"); + if (SupportsArenas(descriptor_)) { format( // TODO(gerbens) Make this private! Currently people are deriving from @@ -1226,10 +1348,10 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { format( "private:\n" "inline ::$proto_ns$::Arena* GetArenaNoVirtual() const {\n" - " return NULL;\n" + " return nullptr;\n" "}\n" "inline void* MaybeArenaPtr() const {\n" - " return NULL;\n" + " return nullptr;\n" "}\n"); } @@ -1240,10 +1362,17 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { if (HasDescriptorMethods(descriptor_->file(), options_)) { format( "::$proto_ns$::Metadata GetMetadata() const final;\n" + "private:\n" + "static ::$proto_ns$::Metadata GetMetadataStatic() {\n" + " ::$proto_ns$::internal::AssignDescriptors(&::$desc_table$);\n" + " return ::$desc_table$.file_level_metadata[kIndexInFileMessages];\n" + "}\n" + "\n" + "public:\n" "\n"); } else { format( - "$string$ GetTypeName() const final;\n" + "std::string GetTypeName() const final;\n" "\n"); } @@ -1256,7 +1385,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { const Descriptor* nested_type = descriptor_->nested_type(i); if (!IsMapEntryMessage(nested_type)) { format.Set("nested_full_name", ClassName(nested_type, false)); - format.Set("nested_name", nested_type->name()); + format.Set("nested_name", ResolveKeyword(nested_type->name())); format("typedef ${1$$nested_full_name$$}$ ${1$$nested_name$$}$;\n", nested_type); } @@ -1293,7 +1422,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { format(" private:\n"); format.Indent(); // TODO(seongkim): Remove hack to track field access and remove this class. - format("class HasBitSetters;\n"); + format("class _Internal;\n"); for (auto field : FieldRange(descriptor_)) { @@ -1325,15 +1454,15 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { // output will be determined later. bool need_to_emit_cached_size = true; - const string cached_size_decl = + const std::string cached_size_decl = "mutable ::$proto_ns$::internal::CachedSize _cached_size_;\n"; const size_t sizeof_has_bits = HasBitsSize(); - const string has_bits_decl = + const std::string has_bits_decl = sizeof_has_bits == 0 ? "" - : "::$proto_ns$::internal::HasBits<" + - SimpleItoa(sizeof_has_bits / 4) + "> _has_bits_;\n"; + : StrCat("::$proto_ns$::internal::HasBits<", + sizeof_has_bits / 4, "> _has_bits_;\n"); // To minimize padding, data members are divided into three sections: // (1) members assumed to align to 8 bytes @@ -1360,11 +1489,6 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { } if (SupportsArenas(descriptor_)) { - if (!options_.opensource_runtime) { - format( - "template friend struct " - "::proto::internal::EmbeddedMessageHolder;\n"); - } format( "template friend class " "::$proto_ns$::Arena::InternalHelper;\n" @@ -1393,7 +1517,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { // For each oneof generate a union for (auto oneof : OneOfRange(descriptor_)) { - string camel_oneof_name = UnderscoresToCamelCase(oneof->name(), true); + std::string camel_oneof_name = UnderscoresToCamelCase(oneof->name(), true); format( "union $1$Union {\n" // explicit empty constructor is needed when union contains @@ -1430,7 +1554,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { format("::$proto_ns$::internal::WeakFieldMap _weak_field_map_;\n"); } // Generate _any_metadata_ for the Any type. - if (IsAnyMessage(descriptor_)) { + if (IsAnyMessage(descriptor_, options_)) { format("::$proto_ns$::internal::AnyMetadata _any_metadata_;\n"); } @@ -1441,7 +1565,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { format.Outdent(); format("};"); GOOGLE_DCHECK(!need_to_emit_cached_size); -} +} // NOLINT(readability/fn_size) void MessageGenerator::GenerateInlineMethods(io::Printer* printer) { if (IsMapEntryMessage(descriptor_)) return; @@ -1455,10 +1579,11 @@ void MessageGenerator::GenerateInlineMethods(io::Printer* printer) { format.Set("oneof_index", oneof->index()); format( "inline $classname$::$camel_oneof_name$Case $classname$::" - "$oneof_name$_case() const {\n" + "${1$$oneof_name$_case$}$() const {\n" " return $classname$::$camel_oneof_name$Case(" "_oneof_case_[$oneof_index$]);\n" - "}\n"); + "}\n", + oneof); } } @@ -1490,7 +1615,7 @@ bool MessageGenerator::GenerateParseTable(io::Printer* printer, size_t offset, Formatter format(printer, variables_); if (!table_driven_) { - format("{ NULL, NULL, 0, -1, -1, -1, -1, NULL, false },\n"); + format("{ nullptr, nullptr, 0, -1, -1, -1, -1, nullptr, false },\n"); return false; } @@ -1514,23 +1639,17 @@ bool MessageGenerator::GenerateParseTable(io::Printer* printer, size_t offset, // If we don't have field presence, then _has_bits_ does not exist. format("-1,\n"); } else { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET(\n" - " $classtype$, _has_bits_),\n"); + format("PROTOBUF_FIELD_OFFSET($classtype$, _has_bits_),\n"); } if (descriptor_->oneof_decl_count() > 0) { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET(\n" - " $classtype$, _oneof_case_),\n"); + format("PROTOBUF_FIELD_OFFSET($classtype$, _oneof_case_),\n"); } else { format("-1, // no _oneof_case_\n"); } if (descriptor_->extension_range_count() > 0) { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_extensions_),\n"); + format("PROTOBUF_FIELD_OFFSET($classtype$, _extensions_),\n"); } else { format("-1, // no _extensions_\n"); } @@ -1538,8 +1657,7 @@ bool MessageGenerator::GenerateParseTable(io::Printer* printer, size_t offset, // TODO(ckennelly): Consolidate this with the calculation for // AuxillaryParseTableField. format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET(\n" - " $classtype$, _internal_metadata_),\n" + "PROTOBUF_FIELD_OFFSET($classtype$, _internal_metadata_),\n" "&$package_ns$::_$classname$_default_instance_,\n"); if (UseUnknownFieldSet(descriptor_->file(), options_)) { @@ -1628,32 +1746,27 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { uint32 tag = internal::WireFormatLite::MakeTag( field->number(), WireFormat::WireTypeForFieldType(field->type())); - std::map vars; - vars["classtype"] = QualifiedClassName(descriptor_); + std::map vars; + vars["classtype"] = QualifiedClassName(descriptor_, options_); vars["field_name"] = FieldName(field); - vars["tag"] = SimpleItoa(tag); - vars["hasbit"] = SimpleItoa(i); - vars["type"] = - SimpleItoa(CalcFieldNum(generator, field, options_)); - vars["ptr"] = "NULL"; + vars["tag"] = StrCat(tag); + vars["hasbit"] = StrCat(i); + vars["type"] = StrCat(CalcFieldNum(generator, field, options_)); + vars["ptr"] = "nullptr"; if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { GOOGLE_CHECK(!IsMapEntryMessage(field->message_type())); - if (!IsProto1(field->message_type()->file(), options_)) { - vars["ptr"] = - "::" + - UniqueName("TableStruct", field->message_type(), options_) + - "::serialization_table + " + - SimpleItoa( - FindMessageIndexInFile(field->message_type())); - } + vars["ptr"] = + "::" + UniqueName("TableStruct", field->message_type(), options_) + + "::serialization_table + " + + StrCat(FindMessageIndexInFile(field->message_type())); } Formatter::SaveState saver(&format); format.AddMap(vars); format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET(" + "{PROTOBUF_FIELD_OFFSET(" "::$proto_ns$::internal::MapEntryHelper<$classtype$::" "SuperType>, $field_name$_), $tag$," - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET(" + "PROTOBUF_FIELD_OFFSET(" "::$proto_ns$::internal::MapEntryHelper<$classtype$::" "SuperType>, _has_bits_) * 8 + $hasbit$, $type$, " "$ptr$},\n"); @@ -1661,9 +1774,10 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { return 2; } format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_cached_size_), 0, 0, 0, NULL},\n"); + "{PROTOBUF_FIELD_OFFSET($classtype$, _cached_size_)," + " 0, 0, 0, nullptr},\n"); std::vector sorted_extensions; + sorted_extensions.reserve(descriptor_->extension_range_count()); for (int i = 0; i < descriptor_->extension_range_count(); ++i) { sorted_extensions.push_back(descriptor_->extension_range(i)); } @@ -1677,8 +1791,7 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { const Descriptor::ExtensionRange* range = sorted_extensions[extension_idx]; format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_extensions_), " + "{PROTOBUF_FIELD_OFFSET($classtype$, _extensions_), " "$1$, $2$, ::$proto_ns$::internal::FieldMetadata::kSpecial, " "reinterpret_cast(::$proto_ns$::internal::ExtensionSerializer)},\n", @@ -1694,17 +1807,16 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED); } - string classfieldname = FieldName(field); + std::string classfieldname = FieldName(field); if (field->containing_oneof()) { classfieldname = field->containing_oneof()->name(); } format.Set("field_name", classfieldname); - string ptr = "NULL"; + std::string ptr = "nullptr"; if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { if (IsMapEntryMessage(field->message_type())) { format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($" - "classtype$, $field_name$_), $1$, $2$, " + "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), $1$, $2$, " "::$proto_ns$::internal::FieldMetadata::kSpecial, " "reinterpret_cast(static_cast< " "::$proto_ns$::internal::SpecialSerializer>(" @@ -1713,16 +1825,15 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { "$3$>::MapFieldType, " "$tablename$::serialization_table>))},\n", tag, FindMessageIndexInFile(field->message_type()), - QualifiedClassName(field->message_type())); + QualifiedClassName(field->message_type(), options_)); continue; - } else if (!IsProto1(field->message_type()->file(), options_) && - !field->message_type()->options().message_set_wire_format()) { - // Proto1 or message_set don't have the usual table and we need to + } else if (!field->message_type()->options().message_set_wire_format()) { + // message_set doesn't have the usual table and we need to // dispatch to generated serializer, hence ptr stays zero. ptr = "::" + UniqueName("TableStruct", field->message_type(), options_) + "::serialization_table + " + - SimpleItoa(FindMessageIndexInFile(field->message_type())); + StrCat(FindMessageIndexInFile(field->message_type())); } } @@ -1745,7 +1856,7 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { if (field->options().weak()) { // TODO(gerbens) merge weak fields into ranges format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET(" + "{PROTOBUF_FIELD_OFFSET(" "$classtype$, _weak_field_map_), $1$, $1$, " "::$proto_ns$::internal::FieldMetadata::kSpecial, " "reinterpret_castcontaining_oneof()->index()); format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "$field_name$_), " - "$1$, $GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_oneof_case_) + $oneofoffset$, $2$, $3$},\n", + "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), $1$," + " PROTOBUF_FIELD_OFFSET($classtype$, _oneof_case_) + " + "$oneofoffset$, $2$, $3$},\n", tag, type, ptr); } else if (HasFieldPresence(descriptor_->file()) && has_bit_indices_[field->index()] != -1) { format.Set("hasbitsoffset", has_bit_indices_[field->index()]); format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "$field_name$_), " - "$1$, $GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_has_bits_) * 8 + $hasbitsoffset$, $2$, $3$},\n", + "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), " + "$1$, PROTOBUF_FIELD_OFFSET($classtype$, _has_bits_) * 8 + " + "$hasbitsoffset$, $2$, $3$},\n", tag, type, ptr); } else { format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "$field_name$_), " + "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), " "$1$, ~0u, $2$, $3$},\n", tag, type, ptr); } } int num_field_metadata = 1 + sorted.size() + sorted_extensions.size(); num_field_metadata++; - string serializer = UseUnknownFieldSet(descriptor_->file(), options_) - ? "UnknownFieldSetSerializer" - : "UnknownFieldSerializerLite"; + std::string serializer = UseUnknownFieldSet(descriptor_->file(), options_) + ? "UnknownFieldSetSerializer" + : "UnknownFieldSerializerLite"; format( - "{$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_internal_metadata_), 0, ~0u, " + "{PROTOBUF_FIELD_OFFSET($classtype$, _internal_metadata_), 0, ~0u, " "::$proto_ns$::internal::FieldMetadata::kSpecial, reinterpret_cast(::$proto_ns$::internal::$1$)},\n", serializer); @@ -1811,12 +1918,11 @@ void MessageGenerator::GenerateDefaultInstanceInitializer( for (auto field : FieldRange(descriptor_)) { Formatter::SaveState saver(&format); - if (!field->is_repeated() && - !IsLazy(field, options_) && + if (!field->is_repeated() && !IsLazy(field, options_) && field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && (field->containing_oneof() == NULL || HasDescriptorMethods(descriptor_->file(), options_))) { - string name; + std::string name; if (field->containing_oneof() || field->options().weak()) { name = "_" + classname_ + "_default_instance_."; } else { @@ -1826,24 +1932,21 @@ void MessageGenerator::GenerateDefaultInstanceInitializer( name += FieldName(field); format.Set("name", name); if (IsWeak(field, options_)) { - const FileDescriptor* dependency = field->message_type()->file(); - string default_instance = QualifiedFileLevelSymbol( - dependency->package(), - "_" + ClassName(field->message_type()) + "_default_instance_"); format( "$package_ns$::$name$_ = reinterpret_cast(&$1$);\n" - "if ($package_ns$::$name$_ == NULL) {\n" + "if ($package_ns$::$name$_ == nullptr) {\n" " $package_ns$::$name$_ = " "::$proto_ns$::Empty::internal_default_instance();\n" "}\n", - default_instance); // 1 + QualifiedDefaultInstanceName(field->message_type(), + options_)); // 1 continue; } format( "$package_ns$::$name$_ = const_cast< $1$*>(\n" " $1$::internal_default_instance());\n", - FieldMessageTypeName(field)); + FieldMessageTypeName(field, options_)); } else if (field->containing_oneof() && HasDescriptorMethods(descriptor_->file(), options_)) { field_generators_.get(field).GenerateConstructorCode(printer); @@ -1864,11 +1967,8 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { if (HasDescriptorMethods(descriptor_->file(), options_)) { format( "::$proto_ns$::Metadata $classname$::GetMetadata() const {\n" - " " - "::$proto_ns$::internal::AssignDescriptors(&::$assign_desc_table$);\n" - " return ::$file_level_metadata$[$1$];\n" - "}\n", - index_in_file_messages_); + " return GetMetadataStatic();\n" + "}\n"); format( "void $classname$::MergeFrom(\n" " const ::$proto_ns$::Message& other) {\n" @@ -1876,14 +1976,6 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { "}\n" "\n"); } - // TODO(gerbens) make maps parse :( - format( - "#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n" - "const char* $classname$::_InternalParse(const char* begin, const " - "char* end, void* object, ::$proto_ns$::internal::ParseContext* ctx) { " - "return end; }\n" - "#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"); - format("\n"); return; } @@ -1895,54 +1987,57 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { format.Outdent(); format("}\n"); - if (IsAnyMessage(descriptor_)) { + if (IsAnyMessage(descriptor_, options_)) { + if (HasDescriptorMethods(descriptor_->file(), options_)) { + format( + "void $classname$::PackFrom(const ::$proto_ns$::Message& message) {\n" + " _any_metadata_.PackFrom(message);\n" + "}\n" + "\n" + "void $classname$::PackFrom(const ::$proto_ns$::Message& message,\n" + " const std::string& type_url_prefix) {\n" + " _any_metadata_.PackFrom(message, type_url_prefix);\n" + "}\n" + "\n" + "bool $classname$::UnpackTo(::$proto_ns$::Message* message) const {\n" + " return _any_metadata_.UnpackTo(message);\n" + "}\n" + "bool $classname$::GetAnyFieldDescriptors(\n" + " const ::$proto_ns$::Message& message,\n" + " const ::$proto_ns$::FieldDescriptor** type_url_field,\n" + " const ::$proto_ns$::FieldDescriptor** value_field) {\n" + " return ::$proto_ns$::internal::GetAnyFieldDescriptors(\n" + " message, type_url_field, value_field);\n" + "}\n"); + } format( - "void $classname$::PackFrom(const ::$proto_ns$::Message& message) {\n" - " _any_metadata_.PackFrom(message);\n" - "}\n" - "\n" - "void $classname$::PackFrom(const ::$proto_ns$::Message& message,\n" - " const $string$& type_url_prefix) {\n" - " _any_metadata_.PackFrom(message, type_url_prefix);\n" - "}\n" - "\n" - "bool $classname$::UnpackTo(::$proto_ns$::Message* message) const {\n" - " return _any_metadata_.UnpackTo(message);\n" - "}\n" "bool $classname$::ParseAnyTypeUrl(const string& type_url,\n" - " string* full_type_name) {\n" + " std::string* full_type_name) {\n" " return ::$proto_ns$::internal::ParseAnyTypeUrl(type_url,\n" " full_type_name);\n" "}\n" - "bool $classname$::GetAnyFieldDescriptors(\n" - " const ::$proto_ns$::Message& message,\n" - " const ::$proto_ns$::FieldDescriptor** type_url_field,\n" - " const ::$proto_ns$::FieldDescriptor** value_field) {\n" - " return ::$proto_ns$::internal::GetAnyFieldDescriptors(\n" - " message, type_url_field, value_field);\n" - "}\n" "\n"); } format( - "class $classname$::HasBitSetters {\n" + "class $classname$::_Internal {\n" " public:\n"); format.Indent(); + if (HasFieldPresence(descriptor_->file()) && HasBitsSize() != 0) { + format( + "using HasBits = decltype(std::declval<$classname$>()._has_bits_);\n"); + } for (auto field : FieldRange(descriptor_)) { field_generators_.get(field).GenerateInternalAccessorDeclarations(printer); if (HasFieldPresence(descriptor_->file()) && !field->is_repeated() && !field->options().weak() && !field->containing_oneof()) { int has_bit_index = has_bit_indices_[field->index()]; GOOGLE_CHECK_GE(has_bit_index, 0); - - format.Set("has_array_index", has_bit_index / 32); - format.Set("has_mask", - strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); format( - "static void set_has_$1$($classname$* msg) {\n" - " msg->_has_bits_[$has_array_index$] |= 0x$has_mask$u;\n" + "static void set_has_$1$(HasBits* has_bits) {\n" + " (*has_bits)[$2$] |= $3$u;\n" "}\n", - FieldName(field)); + FieldName(field), has_bit_index / 32, (1u << (has_bit_index % 32))); } } format.Outdent(); @@ -1956,7 +2051,7 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { field_generators_.get(field).GenerateNonInlineAccessorDefinitions(printer); if (IsCrossFileMaybeMap(field)) { Formatter::SaveState saver(&format); - std::map vars; + std::map vars; SetCommonFieldVariables(field, &vars, options_); if (field->containing_oneof()) { SetCommonOneofFieldVariables(field, &vars); @@ -1966,15 +2061,6 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { } } - // Generate field number constants. - format("#if !defined(_MSC_VER) || _MSC_VER >= 1900\n"); - for (auto field : FieldRange(descriptor_)) { - format("const int $classname$::$1$;\n", FieldConstantName(field)); - } - format( - "#endif // !defined(_MSC_VER) || _MSC_VER >= 1900\n" - "\n"); - GenerateStructors(printer); format("\n"); @@ -1990,14 +2076,9 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { GenerateMergeFromCodedStream(printer); format("\n"); - GenerateSerializeWithCachedSizes(printer); + GenerateSerializeWithCachedSizesToArray(printer); format("\n"); - if (HasFastArraySerialization(descriptor_->file(), options_)) { - GenerateSerializeWithCachedSizesToArray(printer); - format("\n"); - } - GenerateByteSize(printer); format("\n"); @@ -2025,13 +2106,12 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { if (HasDescriptorMethods(descriptor_->file(), options_)) { format( "::$proto_ns$::Metadata $classname$::GetMetadata() const {\n" - " ::$proto_ns$::internal::AssignDescriptors(&::$assign_desc_table$);\n" - " return ::$file_level_metadata$[kIndexInFileMessages];\n" + " return GetMetadataStatic();\n" "}\n" "\n"); } else { format( - "$string$ $classname$::GetTypeName() const {\n" + "std::string $classname$::GetTypeName() const {\n" " return \"$full_name$\";\n" "}\n" "\n"); @@ -2120,26 +2200,24 @@ size_t MessageGenerator::GenerateParseOffsets(io::Printer* printer) { const unsigned char tag_size = WireFormat::TagSize(field->number(), field->type()); - std::map vars; + std::map vars; if (field->containing_oneof() != NULL) { vars["name"] = field->containing_oneof()->name(); - vars["presence"] = - SimpleItoa(field->containing_oneof()->index()); + vars["presence"] = StrCat(field->containing_oneof()->index()); } else { vars["name"] = FieldName(field); - vars["presence"] = SimpleItoa(has_bit_indices_[field->index()]); + vars["presence"] = StrCat(has_bit_indices_[field->index()]); } - vars["nwtype"] = SimpleItoa(normal_wiretype); - vars["pwtype"] = SimpleItoa(packed_wiretype); - vars["ptype"] = SimpleItoa(processing_type); - vars["tag_size"] = SimpleItoa(tag_size); + vars["nwtype"] = StrCat(normal_wiretype); + vars["pwtype"] = StrCat(packed_wiretype); + vars["ptype"] = StrCat(processing_type); + vars["tag_size"] = StrCat(tag_size); format.AddMap(vars); format( "{\n" - " $GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET(\n" - " $classtype$, $name$_),\n" + " PROTOBUF_FIELD_OFFSET($classtype$, $name$_),\n" " static_cast<$uint32$>($presence$),\n" " $nwtype$, $pwtype$, $ptype$, $tag_size$\n" "},\n"); @@ -2168,16 +2246,22 @@ size_t MessageGenerator::GenerateParseAuxTable(io::Printer* printer) { format("::$proto_ns$::internal::AuxillaryParseTableField(),\n"); } - std::map vars; + std::map vars; SetCommonFieldVariables(field, &vars, options_); format.AddMap(vars); switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_ENUM: - format( - "{::$proto_ns$::internal::AuxillaryParseTableField::enum_aux{" - "$1$_IsValid}},\n", - ClassName(field->enum_type(), true)); + if (HasPreservingUnknownEnumSemantics(field)) { + format( + "{::$proto_ns$::internal::AuxillaryParseTableField::enum_aux{" + "nullptr}},\n"); + } else { + format( + "{::$proto_ns$::internal::AuxillaryParseTableField::enum_aux{" + "$1$_IsValid}},\n", + ClassName(field->enum_type(), true)); + } last_field_number++; break; case FieldDescriptor::CPPTYPE_MESSAGE: { @@ -2185,27 +2269,29 @@ size_t MessageGenerator::GenerateParseAuxTable(io::Printer* printer) { format( "{::$proto_ns$::internal::AuxillaryParseTableField::map_" "aux{&::$proto_ns$::internal::ParseMap<$1$>}},\n", - QualifiedClassName(field->message_type())); + QualifiedClassName(field->message_type(), options_)); last_field_number++; break; } format.Set("field_classname", ClassName(field->message_type(), false)); - format.Set("ns", Namespace(field->message_type())); + format.Set("default_instance", QualifiedDefaultInstanceName( + field->message_type(), options_)); format( "{::$proto_ns$::internal::AuxillaryParseTableField::message_aux{\n" - " &$ns$::_$field_classname$_default_instance_}},\n"); + " &$default_instance$}},\n"); last_field_number++; break; } case FieldDescriptor::CPPTYPE_STRING: { - string default_val; + std::string default_val; switch (EffectiveStringCType(field, options_)) { case FieldOptions::STRING: default_val = field->default_value_string().empty() ? "&::" + variables_["proto_ns"] + "::internal::fixed_address_empty_string" - : "&" + Namespace(field) + " ::" + classname_ + + : "&" + + QualifiedClassName(descriptor_, options_) + "::" + MakeDefaultName(field); break; case FieldOptions::CORD: @@ -2236,33 +2322,23 @@ std::pair MessageGenerator::GenerateOffsets( Formatter format(printer, variables_); if (HasFieldPresence(descriptor_->file()) || IsMapEntryMessage(descriptor_)) { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_has_bits_),\n"); + format("PROTOBUF_FIELD_OFFSET($classtype$, _has_bits_),\n"); } else { format("~0u, // no _has_bits_\n"); } - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_internal_metadata_),\n"); + format("PROTOBUF_FIELD_OFFSET($classtype$, _internal_metadata_),\n"); if (descriptor_->extension_range_count() > 0) { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, " - "_extensions_),\n"); + format("PROTOBUF_FIELD_OFFSET($classtype$, _extensions_),\n"); } else { format("~0u, // no _extensions_\n"); } if (descriptor_->oneof_decl_count() > 0) { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET(" - "$classtype$, _oneof_case_[0]),\n"); + format("PROTOBUF_FIELD_OFFSET($classtype$, _oneof_case_[0]),\n"); } else { format("~0u, // no _oneof_case_\n"); } if (num_weak_fields_ > 0) { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$," - " _weak_field_map_),\n"); + format("PROTOBUF_FIELD_OFFSET($classtype$, _weak_field_map_),\n"); } else { format("~0u, // no _weak_field_map_\n"); } @@ -2275,9 +2351,7 @@ std::pair MessageGenerator::GenerateOffsets( format("offsetof($classtype$DefaultTypeInternal, $1$_)", FieldName(field)); } else { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, $1$_)", - FieldName(field)); + format("PROTOBUF_FIELD_OFFSET($classtype$, $1$_)", FieldName(field)); } uint32 tag = field_generators_.get(field).CalculateFieldTag(); @@ -2289,9 +2363,7 @@ std::pair MessageGenerator::GenerateOffsets( } for (auto oneof : OneOfRange(descriptor_)) { - format( - "$GOOGLE_PROTOBUF$_GENERATED_MESSAGE_FIELD_OFFSET($classtype$, $1$_),\n", - oneof->name()); + format("PROTOBUF_FIELD_OFFSET($classtype$, $1$_),\n", oneof->name()); } if (IsMapEntryMessage(descriptor_)) { @@ -2302,9 +2374,8 @@ std::pair MessageGenerator::GenerateOffsets( } else if (HasFieldPresence(descriptor_->file())) { entries += has_bit_indices_.size(); for (int i = 0; i < has_bit_indices_.size(); i++) { - const string index = has_bit_indices_[i] >= 0 - ? SimpleItoa(has_bit_indices_[i]) - : "~0u"; + const std::string index = + has_bit_indices_[i] >= 0 ? StrCat(has_bit_indices_[i]) : "~0u"; format("$1$,\n", index); } } @@ -2316,12 +2387,9 @@ void MessageGenerator::GenerateSharedConstructorCode(io::Printer* printer) { Formatter format(printer, variables_); format("void $classname$::SharedCtor() {\n"); - if (scc_analyzer_ - ->GetSCCAnalysis(scc_analyzer_->GetSCC(descriptor_)) + if (scc_analyzer_->GetSCCAnalysis(scc_analyzer_->GetSCC(descriptor_)) .constructor_requires_initialization) { - format( - " ::$proto_ns$::internal::InitSCC(\n" - " &scc_info_$scc_name$.base);\n"); + format(" ::$proto_ns$::internal::InitSCC(&$scc_info$.base);\n"); } format.Indent(); @@ -2343,7 +2411,7 @@ void MessageGenerator::GenerateSharedDestructorCode(io::Printer* printer) { format("void $classname$::SharedDtor() {\n"); format.Indent(); if (SupportsArenas(descriptor_)) { - format("$DCHK$(GetArenaNoVirtual() == NULL);\n"); + format("$DCHK$(GetArenaNoVirtual() == nullptr);\n"); } // Write the destructors for each field except oneof members. // optimized_order_ does not contain oneof fields. @@ -2419,7 +2487,7 @@ void MessageGenerator::GenerateArenaDestructorCode(io::Printer* printer) { format( "inline void $classname$::RegisterArenaDtor(::$proto_ns$::Arena* " "arena) {\n" - " if (arena != NULL) {\n" + " if (arena != nullptr) {\n" " arena->OwnCustomDestructor(this, &$classname$::ArenaDtor);\n" " }\n" "}\n"); @@ -2454,7 +2522,7 @@ void MessageGenerator::GenerateConstructorBody(io::Printer* printer, } } - string pod_template; + std::string pod_template; if (copy_constructor) { pod_template = "::memcpy(&$first$_, &from.$first$_,\n" @@ -2480,8 +2548,8 @@ void MessageGenerator::GenerateConstructorBody(io::Printer* printer, if (it != runs.end() && it->second > 1) { // Use a memset, then skip run_length fields. const size_t run_length = it->second; - const string first_field_name = FieldName(field); - const string last_field_name = + const std::string first_field_name = FieldName(field); + const std::string last_field_name = FieldName(optimized_order_[i + run_length - 1]); format.Set("first", first_field_name); @@ -2504,9 +2572,9 @@ void MessageGenerator::GenerateConstructorBody(io::Printer* printer, void MessageGenerator::GenerateStructors(io::Printer* printer) { Formatter format(printer, variables_); - string superclass; + std::string superclass; superclass = SuperClassName(descriptor_, options_); - string initializer_with_arena = superclass + "()"; + std::string initializer_with_arena = superclass + "()"; if (descriptor_->extension_range_count() > 0) { initializer_with_arena += ",\n _extensions_(arena)"; @@ -2523,19 +2591,20 @@ void MessageGenerator::GenerateStructors(io::Printer* printer) { } if (has_arena_constructor) { initializer_with_arena += - string(",\n ") + FieldName(field) + string("_(arena)"); + std::string(",\n ") + FieldName(field) + std::string("_(arena)"); } } - if (IsAnyMessage(descriptor_)) { + if (IsAnyMessage(descriptor_, options_)) { initializer_with_arena += ",\n _any_metadata_(&type_url_, &value_)"; } if (num_weak_fields_ > 0) { initializer_with_arena += ", _weak_field_map_(arena)"; } - string initializer_null = superclass + "(), _internal_metadata_(NULL)"; - if (IsAnyMessage(descriptor_)) { + std::string initializer_null = + superclass + "(), _internal_metadata_(nullptr)"; + if (IsAnyMessage(descriptor_, options_)) { initializer_null += ", _any_metadata_(&type_url_, &value_)"; } if (num_weak_fields_ > 0) { @@ -2579,7 +2648,7 @@ void MessageGenerator::GenerateStructors(io::Printer* printer) { format.Indent(); format.Indent(); format.Indent(); - format(",\n_internal_metadata_(NULL)"); + format(",\n_internal_metadata_(nullptr)"); if (HasFieldPresence(descriptor_->file())) { if (!IsProto2MessageSet(descriptor_, options_)) { @@ -2599,7 +2668,7 @@ void MessageGenerator::GenerateStructors(io::Printer* printer) { format(",\n$1$_(from.$1$_)", FieldName(field)); } - if (IsAnyMessage(descriptor_)) { + if (IsAnyMessage(descriptor_, options_)) { format(",\n_any_metadata_(&type_url_, &value_)"); } if (num_weak_fields_ > 0) { @@ -2677,7 +2746,7 @@ void MessageGenerator::GenerateStructors(io::Printer* printer) { format( "const $classname$& $classname$::default_instance() {\n" " " - "::$proto_ns$::internal::InitSCC(&::scc_info_$scc_name$.base)" + "::$proto_ns$::internal::InitSCC(&::$scc_info$.base)" ";\n" " return *internal_default_instance();\n" "}\n\n"); @@ -2687,28 +2756,13 @@ void MessageGenerator::GenerateSourceInProto2Namespace(io::Printer* printer) { Formatter format(printer, variables_); format( "template<> " - "$GOOGLE_PROTOBUF$_ATTRIBUTE_NOINLINE " + "PROTOBUF_NOINLINE " "$classtype$* Arena::CreateMaybeMessage< $classtype$ >(Arena* arena) {\n" " return Arena::$1$Internal< $classtype$ >(arena);\n" "}\n", MessageCreateFunction(descriptor_)); } -bool MessageGenerator::MaybeGenerateOptionalFieldCondition( - io::Printer* printer, const FieldDescriptor* field, - int expected_has_bits_index) { - Formatter format(printer, variables_); - int has_bit_index = has_bit_indices_[field->index()]; - if (!field->options().weak() && - expected_has_bits_index == has_bit_index / 32) { - const string mask = - StrCat(strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); - format("if (cached_has_bits & 0x$1$u) {\n", mask); - return true; - } - return false; -} - void MessageGenerator::GenerateClear(io::Printer* printer) { Formatter format(printer, variables_); // Performance tuning parameters @@ -2744,14 +2798,14 @@ void MessageGenerator::GenerateClear(io::Printer* printer) { unconditional_budget -= EstimateAlignmentSize(field); } - std::vector > chunks_frag = CollectFields( + std::vector> chunks_frag = CollectFields( optimized_order_, MatchRepeatedAndHasByteAndZeroInits( &has_bit_indices_, HasFieldPresence(descriptor_->file()))); // Merge next non-zero initializable chunk if it has the same has_byte index // and not meeting unconditional clear condition. - std::vector > chunks; + std::vector> chunks; if (!HasFieldPresence(descriptor_->file())) { // Don't bother with merging without has_bit field. chunks = chunks_frag; @@ -2845,8 +2899,8 @@ void MessageGenerator::GenerateClear(io::Printer* printer) { field_generators_.get(chunk[memset_run_start]); generator.GenerateMessageClearingCode(printer); } else { - const string first_field_name = FieldName(chunk[memset_run_start]); - const string last_field_name = FieldName(chunk[memset_run_end]); + const std::string first_field_name = FieldName(chunk[memset_run_start]); + const std::string last_field_name = FieldName(chunk[memset_run_end]); format( "::memset(&$1$_, 0, static_cast(\n" @@ -2965,40 +3019,7 @@ void MessageGenerator::GenerateOneofClear(io::Printer* printer) { void MessageGenerator::GenerateSwap(io::Printer* printer) { Formatter format(printer, variables_); - if (SupportsArenas(descriptor_)) { - // Generate the Swap member function. This is a lightweight wrapper around - // UnsafeArenaSwap() / MergeFrom() with temporaries, depending on the memory - // ownership situation: swapping across arenas or between an arena and a - // heap requires copying. - format( - "void $classname$::Swap($classname$* other) {\n" - " if (other == this) return;\n" - " if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {\n" - " InternalSwap(other);\n" - " } else {\n" - " $classname$* temp = New(GetArenaNoVirtual());\n" - " temp->MergeFrom(*other);\n" - " other->CopyFrom(*this);\n" - " InternalSwap(temp);\n" - " if (GetArenaNoVirtual() == NULL) {\n" - " delete temp;\n" - " }\n" - " }\n" - "}\n" - "void $classname$::UnsafeArenaSwap($classname$* other) {\n" - " if (other == this) return;\n" - " $DCHK$(GetArenaNoVirtual() == other->GetArenaNoVirtual());\n" - " InternalSwap(other);\n" - "}\n"); - } else { - format( - "void $classname$::Swap($classname$* other) {\n" - " if (other == this) return;\n" - " InternalSwap(other);\n" - "}\n"); - } - // Generate the UnsafeArenaSwap member function. format("void $classname$::InternalSwap($classname$* other) {\n"); format.Indent(); format("using std::swap;\n"); @@ -3028,8 +3049,7 @@ void MessageGenerator::GenerateSwap(io::Printer* printer) { } for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - format( - "swap(_oneof_case_[$1$], other->_oneof_case_[$1$]);\n", i); + format("swap(_oneof_case_[$1$], other->_oneof_case_[$1$]);\n", i); } if (num_weak_fields_) { @@ -3063,7 +3083,7 @@ void MessageGenerator::GenerateMergeFrom(io::Printer* printer) { "const $classname$* source =\n" " ::$proto_ns$::DynamicCastToGenerated<$classname$>(\n" " &from);\n" - "if (source == NULL) {\n" + "if (source == nullptr) {\n" "// @@protoc_insertion_point(generalized_merge_from_cast_fail:" "$full_name$)\n" " ::$proto_ns$::internal::ReflectionOps::Merge(from, this);\n" @@ -3080,7 +3100,8 @@ void MessageGenerator::GenerateMergeFrom(io::Printer* printer) { format( "void $classname$::CheckTypeAndMergeFrom(\n" " const ::$proto_ns$::MessageLite& from) {\n" - " MergeFrom(*::google::protobuf::down_cast(&from));\n" + " MergeFrom(*::$proto_ns$::internal::DownCast(\n" + " &from));\n" "}\n" "\n"); } @@ -3167,7 +3188,7 @@ void MessageGenerator::GenerateMergeFrom(io::Printer* printer) { int has_bit_index = has_bit_indices_[field->index()]; if (!field->options().weak() && cached_has_bit_index == has_bit_index / 32) { - const string mask = StrCat( + const std::string mask = StrCat( strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); format("if (cached_has_bits & 0x$1$u) {\n", mask); @@ -3324,27 +3345,17 @@ void MessageGenerator::GenerateCopyFrom(io::Printer* printer) { } void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { - std::map vars = variables_; + std::map vars = variables_; SetUnknkownFieldsVariable(descriptor_, options_, &vars); Formatter format(printer, vars); if (descriptor_->options().message_set_wire_format()) { // Special-case MessageSet. format( "#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n" - "const char* $classname$::_InternalParse(const char* begin, const " - "char* end, void* object,\n" + "const char* $classname$::_InternalParse(const char* ptr,\n" " ::$proto_ns$::internal::ParseContext* ctx) {\n" - " return ::$proto_ns$::internal::ParseMessageSet(begin, end, " - "static_cast<$classname$*>(object), ctx);\n" - "}\n" - "const char* $classname$::InternalParseMessageSetItem(const char* " - "begin, const char* end, void* object,\n" - " ::$proto_ns$::internal::ParseContext* ctx) {\n" - " auto msg = static_cast<$classname$*>(object);\n" - " return " - "msg->_extensions_.ParseMessageSetItem({InternalParseMessageSetItem, " - "msg}, begin, end, internal_default_instance(), " - "&msg->_internal_metadata_, ctx);\n" + " return _extensions_.ParseMessageSet(ptr, \n" + " internal_default_instance(), &_internal_metadata_, ctx);\n" "}\n" "#else\n" "bool $classname$::MergePartialFromCodedStream(\n" @@ -3356,7 +3367,8 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { return; } format("#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"); - GenerateParserLoop(descriptor_, options_, scc_analyzer_, printer); + GenerateParserLoop(descriptor_, max_has_bit_index_, options_, scc_analyzer_, + printer); format("#else // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"); std::vector ordered_fields = SortFieldsByNumber(descriptor_); @@ -3368,7 +3380,7 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { if (table_driven_) { format.Indent(); - const string lite = + const std::string lite = UseUnknownFieldSet(descriptor_->file(), options_) ? "" : "Lite"; format( @@ -3396,7 +3408,7 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { format( "#define DO_(EXPRESSION) if " - "(!$GOOGLE_PROTOBUF$_PREDICT_TRUE(EXPRESSION)) goto failure\n" + "(!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure\n" " $uint32$ tag;\n"); if (!UseUnknownFieldSet(descriptor_->file(), options_)) { @@ -3508,9 +3520,8 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { // Emit code to parse the common, expected case. // MSVC is warning about truncating constant in the static_cast so // we truncate the tag explicitly. - format( - "if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", - WireFormat::MakeTag(field)); + format("if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", + WireFormat::MakeTag(field)); format.Indent(); if (field->is_packed()) { @@ -3526,9 +3537,8 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { WireFormat::WireTypeForFieldType(field->type()); const uint32 tag = internal::WireFormatLite::MakeTag(field->number(), wiretype); - format( - "} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", - tag); + format("} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", + tag); format.Indent(); field_generator.GenerateMergeFromCodedStream(printer); @@ -3538,9 +3548,8 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED; const uint32 tag = internal::WireFormatLite::MakeTag(field->number(), wiretype); - format( - "} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", - tag); + format("} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", + tag); format.Indent(); field_generator.GenerateMergeFromCodedStreamWithPacking(printer); format.Outdent(); @@ -3661,12 +3670,11 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { } void MessageGenerator::GenerateSerializeOneofFields( - io::Printer* printer, const std::vector& fields, - bool to_array) { + io::Printer* printer, const std::vector& fields) { Formatter format(printer, variables_); GOOGLE_CHECK(!fields.empty()); if (fields.size() == 1) { - GenerateSerializeOneField(printer, fields[0], to_array, -1); + GenerateSerializeOneField(printer, fields[0], -1); return; } // We have multiple mutually exclusive choices. Emit a switch statement. @@ -3676,12 +3684,8 @@ void MessageGenerator::GenerateSerializeOneofFields( for (auto field : fields) { format("case k$1$:\n", UnderscoresToCamelCase(field->name(), true)); format.Indent(); - if (to_array) { - field_generators_.get(field).GenerateSerializeWithCachedSizesToArray( - printer); - } else { - field_generators_.get(field).GenerateSerializeWithCachedSizes(printer); - } + field_generators_.get(field).GenerateSerializeWithCachedSizesToArray( + printer); format("break;\n"); format.Outdent(); } @@ -3694,7 +3698,6 @@ void MessageGenerator::GenerateSerializeOneofFields( void MessageGenerator::GenerateSerializeOneField(io::Printer* printer, const FieldDescriptor* field, - bool to_array, int cached_has_bits_index) { Formatter format(printer, variables_); if (!field->options().weak()) { @@ -3708,7 +3711,7 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* printer, // Attempt to use the state of cached_has_bits, if possible. int has_bit_index = has_bit_indices_[field->index()]; if (cached_has_bits_index == has_bit_index / 32) { - const string mask = + const std::string mask = StrCat(strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); format("if (cached_has_bits & 0x$1$u) {\n", mask); @@ -3722,12 +3725,7 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* printer, have_enclosing_if = EmitFieldNonDefaultCondition(printer, "this->", field); } - if (to_array) { - field_generators_.get(field).GenerateSerializeWithCachedSizesToArray( - printer); - } else { - field_generators_.get(field).GenerateSerializeWithCachedSizes(printer); - } + field_generators_.get(field).GenerateSerializeWithCachedSizesToArray(printer); if (have_enclosing_if) { format.Outdent(); @@ -3737,57 +3735,15 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* printer, } void MessageGenerator::GenerateSerializeOneExtensionRange( - io::Printer* printer, const Descriptor::ExtensionRange* range, - bool to_array) { - std::map vars; - vars["start"] = SimpleItoa(range->start); - vars["end"] = SimpleItoa(range->end); + io::Printer* printer, const Descriptor::ExtensionRange* range) { + std::map vars = variables_; + vars["start"] = StrCat(range->start); + vars["end"] = StrCat(range->end); Formatter format(printer, vars); format("// Extension range [$start$, $end$)\n"); - if (to_array) { - format( - "target = _extensions_.InternalSerializeWithCachedSizesToArray(\n" - " $start$, $end$, deterministic, target);\n\n"); - } else { - format( - "_extensions_.SerializeWithCachedSizes($start$, $end$, output);\n" - "\n"); - } -} - -void MessageGenerator::GenerateSerializeWithCachedSizes(io::Printer* printer) { - Formatter format(printer, variables_); - if (descriptor_->options().message_set_wire_format()) { - // Special-case MessageSet. - format( - "void $classname$::SerializeWithCachedSizes(\n" - " ::$proto_ns$::io::CodedOutputStream* output) const {\n" - " _extensions_.SerializeMessageSetWithCachedSizes(output);\n"); - std::map vars; - SetUnknkownFieldsVariable(descriptor_, options_, &vars); - format.AddMap(vars); - format( - " " - "::$proto_ns$::internal::SerializeUnknownMessageSetItems(\n" - " $unknown_fields$, output);\n"); - format("}\n"); - return; - } - if (options_.table_driven_serialization) return; - format( - "void $classname$::SerializeWithCachedSizes(\n" - " ::$proto_ns$::io::CodedOutputStream* output) const {\n"); - format.Indent(); - - format("// @@protoc_insertion_point(serialize_start:$full_name$)\n"); - - GenerateSerializeWithCachedSizesBody(printer, false); - - format("// @@protoc_insertion_point(serialize_end:$full_name$)\n"); - - format.Outdent(); - format("}\n"); + "target = _extensions_.InternalSerializeWithCachedSizesToArray(\n" + " $start$, $end$, target, stream);\n\n"); } void MessageGenerator::GenerateSerializeWithCachedSizesToArray( @@ -3797,17 +3753,17 @@ void MessageGenerator::GenerateSerializeWithCachedSizesToArray( // Special-case MessageSet. format( "$uint8$* $classname$::InternalSerializeWithCachedSizesToArray(\n" - " bool deterministic, $uint8$* target) const {\n" + " $uint8$* target, ::$proto_ns$::io::EpsCopyOutputStream* stream) " + "const {\n" " target = _extensions_." - "InternalSerializeMessageSetWithCachedSizesToArray(\n" - " deterministic, target);\n"); - std::map vars; + "InternalSerializeMessageSetWithCachedSizesToArray(target, stream);\n"); + std::map vars; SetUnknkownFieldsVariable(descriptor_, options_, &vars); format.AddMap(vars); format( - " target = ::$proto_ns$::internal::\n" - " SerializeUnknownMessageSetItemsToArray(\n" - " $unknown_fields$, target);\n"); + " target = ::$proto_ns$::internal::" + "InternalSerializeUnknownMessageSetItemsToArray(\n" + " $unknown_fields$, target, stream);\n"); format( " return target;\n" "}\n"); @@ -3816,13 +3772,13 @@ void MessageGenerator::GenerateSerializeWithCachedSizesToArray( format( "$uint8$* $classname$::InternalSerializeWithCachedSizesToArray(\n" - " bool deterministic, $uint8$* target) const {\n"); + " $uint8$* target, ::$proto_ns$::io::EpsCopyOutputStream* stream) " + "const {\n"); format.Indent(); - format("(void)deterministic; // Unused\n"); format("// @@protoc_insertion_point(serialize_to_array_start:$full_name$)\n"); - GenerateSerializeWithCachedSizesBody(printer, true); + GenerateSerializeWithCachedSizesBody(printer); format("// @@protoc_insertion_point(serialize_to_array_end:$full_name$)\n"); @@ -3833,7 +3789,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesToArray( } void MessageGenerator::GenerateSerializeWithCachedSizesBody( - io::Printer* printer, bool to_array) { + io::Printer* printer) { Formatter format(printer, variables_); // If there are multiple fields in a row from the same oneof then we // coalesce them and emit a switch statement. This is more efficient @@ -3842,11 +3798,9 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody( // compiler's emitted code might check has_y() even when has_x() is true. class LazySerializerEmitter { public: - LazySerializerEmitter(MessageGenerator* mg, io::Printer* printer, - bool to_array) + LazySerializerEmitter(MessageGenerator* mg, io::Printer* printer) : mg_(mg), format_(printer), - to_array_(to_array), eager_(!HasFieldPresence(mg->descriptor_->file())), cached_has_bit_index_(-1) {} @@ -3876,7 +3830,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody( } } - mg_->GenerateSerializeOneField(format_.printer(), field, to_array_, + mg_->GenerateSerializeOneField(format_.printer(), field, cached_has_bit_index_); } else { v_.push_back(field); @@ -3885,7 +3839,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody( void Flush() { if (!v_.empty()) { - mg_->GenerateSerializeOneofFields(format_.printer(), v_, to_array_); + mg_->GenerateSerializeOneofFields(format_.printer(), v_); v_.clear(); } } @@ -3900,7 +3854,6 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody( MessageGenerator* mg_; Formatter format_; - const bool to_array_; const bool eager_; std::vector v_; @@ -3914,6 +3867,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody( SortFieldsByNumber(descriptor_); std::vector sorted_extensions; + sorted_extensions.reserve(descriptor_->extension_range_count()); for (int i = 0; i < descriptor_->extension_range_count(); ++i) { sorted_extensions.push_back(descriptor_->extension_range(i)); } @@ -3931,7 +3885,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody( // Merge the fields and the extension ranges, both sorted by field number. { - LazySerializerEmitter e(this, printer, to_array); + LazySerializerEmitter e(this, printer); const FieldDescriptor* last_weak_field = nullptr; int i, j; for (i = 0, j = 0; @@ -3956,8 +3910,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody( last_weak_field = nullptr; } e.Flush(); - GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++], - to_array); + GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); } } if (last_weak_field != nullptr) { @@ -3965,30 +3918,24 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody( } } - std::map vars; + std::map vars; SetUnknkownFieldsVariable(descriptor_, options_, &vars); format.AddMap(vars); + format("if (PROTOBUF_PREDICT_FALSE($have_unknown_fields$)) {\n"); + format.Indent(); if (UseUnknownFieldSet(descriptor_->file(), options_)) { - format("if ($have_unknown_fields$) {\n"); - format.Indent(); - if (to_array) { - format( - "target = " - "::$proto_ns$::internal::WireFormat::SerializeUnknownFieldsToArray(\n" - " $unknown_fields$, target);\n"); - } else { - format( - "::$proto_ns$::internal::WireFormat::SerializeUnknownFields(\n" - " $unknown_fields$, output);\n"); - } - format.Outdent(); - - format("}\n"); + format( + "target = " + "::$proto_ns$::internal::WireFormat::" + "InternalSerializeUnknownFieldsToArray(\n" + " $unknown_fields$, target, stream);\n"); } else { format( - "output->WriteRaw($unknown_fields$.data(),\n" - " static_cast($unknown_fields$.size()));\n"); + "target = stream->WriteRaw($unknown_fields$.data(),\n" + " static_cast($unknown_fields$.size()), target);\n"); } + format.Outdent(); + format("}\n"); } std::vector MessageGenerator::RequiredFieldsBitMask() const { @@ -4010,18 +3957,19 @@ std::vector MessageGenerator::RequiredFieldsBitMask() const { // "for all i, (_has_bits_[i] & masks[i]) == masks[i]" // masks is allowed to be shorter than _has_bits_, but at least one element of // masks must be non-zero. -static string ConditionalToCheckBitmasks(const std::vector& masks) { - std::vector parts; +static std::string ConditionalToCheckBitmasks( + const std::vector& masks) { + std::vector parts; for (int i = 0; i < masks.size(); i++) { if (masks[i] == 0) continue; - string m = StrCat("0x", strings::Hex(masks[i], strings::ZERO_PAD_8)); + std::string m = StrCat("0x", strings::Hex(masks[i], strings::ZERO_PAD_8)); // Each xor evaluates to 0 if the expected bits are present. parts.push_back( StrCat("((_has_bits_[", i, "] & ", m, ") ^ ", m, ")")); } GOOGLE_CHECK(!parts.empty()); // If we have multiple parts, each expected to be 0, then bitwise-or them. - string result = + std::string result = parts.size() == 1 ? parts[0] : StrCat("(", Join(parts, "\n | "), ")"); @@ -4033,7 +3981,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* printer) { if (descriptor_->options().message_set_wire_format()) { // Special-case MessageSet. - std::map vars; + std::map vars; SetUnknkownFieldsVariable(descriptor_, options_, &vars); format.AddMap(vars); format( @@ -4095,21 +4043,9 @@ void MessageGenerator::GenerateByteSize(io::Printer* printer) { "\n"); } - std::map vars; + std::map vars; SetUnknkownFieldsVariable(descriptor_, options_, &vars); format.AddMap(vars); - if (UseUnknownFieldSet(descriptor_->file(), options_)) { - format( - "if ($have_unknown_fields$) {\n" - " total_size +=\n" - " ::$proto_ns$::internal::WireFormat::ComputeUnknownFieldsSize(\n" - " $unknown_fields$);\n" - "}\n"); - } else { - format( - "total_size += $unknown_fields$.size();\n" - "\n"); - } // Handle required fields (if any). We expect all of them to be // present, so emit one conditional that checks for that. If they are all @@ -4146,7 +4082,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* printer) { } } - std::vector > chunks = CollectFields( + std::vector> chunks = CollectFields( optimized_order_, MatchRepeatedAndHasByteAndRequired( &has_bit_indices_, HasFieldPresence(descriptor_->file()))); @@ -4288,6 +4224,19 @@ void MessageGenerator::GenerateByteSize(io::Printer* printer) { format("total_size += _weak_field_map_.ByteSizeLong();\n"); } + format("if (PROTOBUF_PREDICT_FALSE($have_unknown_fields$)) {\n"); + if (UseUnknownFieldSet(descriptor_->file(), options_)) { + // We go out of our way to put the computation of the uncommon path of + // unknown fields in tail position. This allows for better code generation + // of this function for simple protos. + format( + " return ::$proto_ns$::internal::ComputeUnknownFieldsSize(\n" + " _internal_metadata_, total_size, &_cached_size_);\n"); + } else { + format(" total_size += $unknown_fields$.size();\n"); + } + format("}\n"); + // We update _cached_size_ even though this is a const method. Because // const methods might be called concurrently this needs to be atomic // operations or the program is undefined. In practice, since any concurrent diff --git a/src/google/protobuf/compiler/cpp/cpp_message.h b/src/google/protobuf/compiler/cpp/cpp_message.h index 170a70cd08..bc4febf5c5 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.h +++ b/src/google/protobuf/compiler/cpp/cpp_message.h @@ -63,7 +63,7 @@ class MessageGenerator { public: // See generator.cc for the meaning of dllexport_decl. MessageGenerator(const Descriptor* descriptor, - const std::map& vars, + const std::map& vars, int index_in_file_messages, const Options& options, MessageSCCAnalyzer* scc_analyzer); ~MessageGenerator(); @@ -135,21 +135,13 @@ class MessageGenerator { // Generate the arena-specific destructor code. void GenerateArenaDestructorCode(io::Printer* printer); - // Helper for GenerateClear and others. Optionally emits a condition that - // assumes the existence of the cached_has_bits variable, and returns true - // if the condition was printed. - bool MaybeGenerateOptionalFieldCondition(io::Printer* printer, - const FieldDescriptor* field, - int expected_has_bits_index); - // Generate standard Message methods. void GenerateClear(io::Printer* printer); void GenerateOneofClear(io::Printer* printer); void GenerateMergeFromCodedStream(io::Printer* printer); void GenerateSerializeWithCachedSizes(io::Printer* printer); void GenerateSerializeWithCachedSizesToArray(io::Printer* printer); - void GenerateSerializeWithCachedSizesBody(io::Printer* printer, - bool to_array); + void GenerateSerializeWithCachedSizesBody(io::Printer* printer); void GenerateByteSize(io::Printer* printer); void GenerateMergeFrom(io::Printer* printer); void GenerateCopyFrom(io::Printer* printer); @@ -162,16 +154,14 @@ class MessageGenerator { // cached_has_bits = _has_bits_[cached_has_bit_index] // for cached_has_bit_index >= 0 void GenerateSerializeOneField(io::Printer* printer, - const FieldDescriptor* field, bool unbounded, + const FieldDescriptor* field, int cached_has_bits_index); // Generate a switch statement to serialize 2+ fields from the same oneof. // Or, if fields.size() == 1, just call GenerateSerializeOneField(). void GenerateSerializeOneofFields( - io::Printer* printer, const std::vector& fields, - bool to_array); + io::Printer* printer, const std::vector& fields); void GenerateSerializeOneExtensionRange( - io::Printer* printer, const Descriptor::ExtensionRange* range, - bool unbounded); + io::Printer* printer, const Descriptor::ExtensionRange* range); // Generates has_foo() functions and variables for singular field has-bits. void GenerateSingularFieldHasBits(const FieldDescriptor* field, @@ -194,7 +184,7 @@ class MessageGenerator { const Descriptor* descriptor_; int index_in_file_messages_; - string classname_; + std::string classname_; Options options_; FieldGeneratorMap field_generators_; // optimized_order_ is the order we layout the message's fields in the @@ -216,7 +206,7 @@ class MessageGenerator { MessageSCCAnalyzer* scc_analyzer_; - std::map variables_; + std::map variables_; friend class FileGenerator; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); diff --git a/src/google/protobuf/compiler/cpp/cpp_message_field.cc b/src/google/protobuf/compiler/cpp/cpp_message_field.cc index afb272730d..7d621e08a6 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message_field.cc @@ -44,22 +44,6 @@ namespace compiler { namespace cpp { namespace { - -// When we are generating code for implicit weak fields, we need to insert some -// additional casts. These functions return the casted expression if -// implicit_weak_field is true but otherwise return the original expression. -// Ordinarily a static_cast is enough to cast google::protobuf::MessageLite* to a class -// deriving from it, but we need a reinterpret_cast in cases where the generated -// message is forward-declared but its full definition is not visible. -string StaticCast(const string& type, const string& expression, - bool implicit_weak_field) { - if (implicit_weak_field) { - return "static_cast< " + type + " >(" + expression + ")"; - } else { - return expression; - } -} - string ReinterpretCast(const string& type, const string& expression, bool implicit_weak_field) { if (implicit_weak_field) { @@ -71,27 +55,22 @@ string ReinterpretCast(const string& type, const string& expression, void SetMessageVariables(const FieldDescriptor* descriptor, const Options& options, bool implicit_weak, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, variables, options); - (*variables)["type"] = FieldMessageTypeName(descriptor); + (*variables)["type"] = FieldMessageTypeName(descriptor, options); (*variables)["casted_member"] = ReinterpretCast( (*variables)["type"] + "*", (*variables)["name"] + "_", implicit_weak); (*variables)["type_default_instance"] = - DefaultInstanceName(descriptor->message_type()); + QualifiedDefaultInstanceName(descriptor->message_type(), options); (*variables)["type_reference_function"] = implicit_weak - ? (" " + ReferenceFunctionName(descriptor->message_type()) + "();\n") + ? (" " + ReferenceFunctionName(descriptor->message_type(), options) + + "();\n") : ""; - (*variables)["stream_writer"] = - (*variables)["declared_type"] + - (HasFastArraySerialization(descriptor->message_type()->file(), options) - ? "MaybeToArray" - : ""); // NOTE: Escaped here to unblock proto1->proto2 migration. // TODO(liujisi): Extend this to apply for other conflicting methods. (*variables)["release_name"] = - SafeFunctionName(descriptor->containing_type(), - descriptor, "release_"); + SafeFunctionName(descriptor->containing_type(), descriptor, "release_"); (*variables)["full_name"] = descriptor->full_name(); } @@ -110,8 +89,7 @@ MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor, MessageFieldGenerator::~MessageFieldGenerator() {} -void MessageFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void MessageFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format("::$proto_ns$::MessageLite* $name$_;\n"); @@ -120,8 +98,8 @@ GeneratePrivateMembers(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void MessageFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$const $type$& ${1$$name$$}$() const;\n" @@ -147,9 +125,10 @@ void MessageFieldGenerator::GenerateNonInlineAccessorDefinitions( format( "void $classname$::unsafe_arena_set_allocated_$name$(\n" " $type$* $name$) {\n" + "$annotate_accessor$" // If we're not on an arena, free whatever we were holding before. // (If we are on arena, we can just forget the earlier pointer.) - " if (GetArenaNoVirtual() == NULL) {\n" + " if (GetArenaNoVirtual() == nullptr) {\n" " delete $name$_;\n" " }\n" " $name$_ = $name$;\n" @@ -164,51 +143,55 @@ void MessageFieldGenerator::GenerateNonInlineAccessorDefinitions( } } -void MessageFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void MessageFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline const $type$& $classname$::$name$() const {\n" + "$annotate_accessor$" " const $type$* p = $casted_member$;\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" - " return p != NULL ? *p : *reinterpret_cast(\n" + " return p != nullptr ? *p : *reinterpret_cast(\n" " &$type_default_instance$);\n" "}\n"); format( "inline $type$* $classname$::$release_name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_release:$full_name$)\n" "$type_reference_function$" " $clear_hasbit$\n" " $type$* temp = $casted_member$;\n"); if (SupportsArenas(descriptor_)) { format( - " if (GetArenaNoVirtual() != NULL) {\n" + " if (GetArenaNoVirtual() != nullptr) {\n" " temp = ::$proto_ns$::internal::DuplicateIfNonNull(temp);\n" " }\n"); } format( - " $name$_ = NULL;\n" + " $name$_ = nullptr;\n" " return temp;\n" "}\n"); if (SupportsArenas(descriptor_)) { format( "inline $type$* $classname$::unsafe_arena_release_$name$() {\n" + "$annotate_accessor$" " // " "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" "$type_reference_function$" " $clear_hasbit$\n" " $type$* temp = $casted_member$;\n" - " $name$_ = NULL;\n" + " $name$_ = nullptr;\n" " return temp;\n" "}\n"); } format( "inline $type$* $classname$::mutable_$name$() {\n" + "$annotate_accessor$" " $set_hasbit$\n" - " if ($name$_ == NULL) {\n" + " if ($name$_ == nullptr) {\n" " auto* p = CreateMaybeMessage<$type$>(GetArenaNoVirtual());\n"); if (implicit_weak_field_) { format(" $name$_ = reinterpret_cast<::$proto_ns$::MessageLite*>(p);\n"); @@ -225,8 +208,9 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { // the slow fallback function. format( "inline void $classname$::set_allocated_$name$($type$* $name$) {\n" + "$annotate_accessor$" " ::$proto_ns$::Arena* message_arena = GetArenaNoVirtual();\n"); - format(" if (message_arena == NULL) {\n"); + format(" if (message_arena == nullptr) {\n"); if (IsCrossFileMessage(descriptor_)) { format( " delete reinterpret_cast< ::$proto_ns$::MessageLite*>($name$_);\n"); @@ -245,7 +229,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " " "reinterpret_cast<::$proto_ns$::MessageLite*>($name$)->GetArena();\n"); } else if (!SupportsArenas(descriptor_->message_type())) { - format(" ::$proto_ns$::Arena* submessage_arena = NULL;\n"); + format(" ::$proto_ns$::Arena* submessage_arena = nullptr;\n"); } else { format( " ::$proto_ns$::Arena* submessage_arena =\n" @@ -270,8 +254,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void MessageFieldGenerator:: -GenerateInternalAccessorDeclarations(io::Printer* printer) const { +void MessageFieldGenerator::GenerateInternalAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( @@ -284,9 +268,9 @@ GenerateInternalAccessorDeclarations(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateInternalAccessorDefinitions(io::Printer* printer) const { - // In theory, these accessors could be inline in HasBitSetters. However, in +void MessageFieldGenerator::GenerateInternalAccessorDefinitions( + io::Printer* printer) const { + // In theory, these accessors could be inline in _Internal. However, in // practice, the linker is then not able to throw them out making implicit // weak dependencies not work at all. Formatter format(printer, variables_); @@ -295,11 +279,11 @@ GenerateInternalAccessorDefinitions(io::Printer* printer) const { // MergePartialFromCodedStream, and their purpose is to provide access to // the field without creating a strong dependency on the message type. format( - "const ::$proto_ns$::MessageLite& $classname$::HasBitSetters::$name$(\n" + "const ::$proto_ns$::MessageLite& $classname$::_Internal::$name$(\n" " const $classname$* msg) {\n" - " if (msg->$name$_ != NULL) {\n" + " if (msg->$name$_ != nullptr) {\n" " return *msg->$name$_;\n" - " } else if (&$type_default_instance$ != NULL) {\n" + " } else if (&$type_default_instance$ != nullptr) {\n" " return *reinterpret_cast(\n" " &$type_default_instance$);\n" " } else {\n" @@ -310,13 +294,13 @@ GenerateInternalAccessorDefinitions(io::Printer* printer) const { if (SupportsArenas(descriptor_)) { format( "::$proto_ns$::MessageLite*\n" - "$classname$::HasBitSetters::mutable_$name$($classname$* msg) {\n"); + "$classname$::_Internal::mutable_$name$($classname$* msg) {\n"); if (HasFieldPresence(descriptor_->file())) { format(" msg->$set_hasbit$\n"); } format( - " if (msg->$name$_ == NULL) {\n" - " if (&$type_default_instance$ == NULL) {\n" + " if (msg->$name$_ == nullptr) {\n" + " if (&$type_default_instance$ == nullptr) {\n" " msg->$name$_ = ::$proto_ns$::Arena::CreateMessage<\n" " ::$proto_ns$::internal::ImplicitWeakMessage>(\n" " msg->GetArenaNoVirtual());\n" @@ -332,13 +316,13 @@ GenerateInternalAccessorDefinitions(io::Printer* printer) const { } else { format( "::$proto_ns$::MessageLite*\n" - "$classname$::HasBitSetters::mutable_$name$($classname$* msg) {\n"); + "$classname$::_Internal::mutable_$name$($classname$* msg) {\n"); if (HasFieldPresence(descriptor_->file())) { format(" msg->$set_hasbit$\n"); } format( - " if (msg->$name$_ == NULL) {\n" - " if (&$type_default_instance$ == NULL) {\n" + " if (msg->$name$_ == nullptr) {\n" + " if (&$type_default_instance$ == nullptr) {\n" " msg->$name$_ = " "new ::$proto_ns$::internal::ImplicitWeakMessage;\n" " } else {\n" @@ -356,66 +340,62 @@ GenerateInternalAccessorDefinitions(io::Printer* printer) const { // message fields under serialize. format( "const $type$&\n" - "$classname$::HasBitSetters::$name$(const $classname$* msg) {\n" + "$classname$::_Internal::$name$(const $classname$* msg) {\n" " return *msg->$field_member$;\n" "}\n"); } } -void MessageFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateClearingCode(io::Printer* printer) const { Formatter format(printer, variables_); if (!HasFieldPresence(descriptor_->file())) { // If we don't have has-bits, message presence is indicated only by ptr != // NULL. Thus on clear, we need to delete the object. format( - "if (GetArenaNoVirtual() == NULL && $name$_ != NULL) {\n" + "if (GetArenaNoVirtual() == nullptr && $name$_ != nullptr) {\n" " delete $name$_;\n" "}\n" - "$name$_ = NULL;\n"); + "$name$_ = nullptr;\n"); } else { - format("if ($name$_ != NULL) $name$_->Clear();\n"); + format("if ($name$_ != nullptr) $name$_->Clear();\n"); } } -void MessageFieldGenerator:: -GenerateMessageClearingCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateMessageClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (!HasFieldPresence(descriptor_->file())) { // If we don't have has-bits, message presence is indicated only by ptr != // NULL. Thus on clear, we need to delete the object. format( - "if (GetArenaNoVirtual() == NULL && $name$_ != NULL) {\n" + "if (GetArenaNoVirtual() == nullptr && $name$_ != nullptr) {\n" " delete $name$_;\n" "}\n" - "$name$_ = NULL;\n"); + "$name$_ = nullptr;\n"); } else { format( - "$DCHK$($name$_ != NULL);\n" + "$DCHK$($name$_ != nullptr);\n" "$name$_->Clear();\n"); } } -void MessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( - "HasBitSetters::mutable_$name$(this)->CheckTypeAndMergeFrom(\n" - " HasBitSetters::$name$(&from));\n"); + "_Internal::mutable_$name$(this)->CheckTypeAndMergeFrom(\n" + " _Internal::$name$(&from));\n"); } else { format("mutable_$name$()->$type$::MergeFrom(from.$name$());\n"); } } -void MessageFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateSwappingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("swap($name$_, other->$name$_);\n"); } -void MessageFieldGenerator:: -GenerateDestructorCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateDestructorCode(io::Printer* printer) const { Formatter format(printer, variables_); if (options_.opensource_runtime) { // TODO(gerbens) Remove this when we don't need to destruct default @@ -428,30 +408,30 @@ GenerateDestructorCode(io::Printer* printer) const { format("delete $name$_;\n"); } -void MessageFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); - format("$name$_ = NULL;\n"); + format("$name$_ = nullptr;\n"); } -void MessageFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format( "if (from.has_$name$()) {\n" " $name$_ = new $type$(*from.$name$_);\n" "} else {\n" - " $name$_ = NULL;\n" + " $name$_ = nullptr;\n" "}\n"); } -void MessageFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void MessageFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( "DO_(::$proto_ns$::internal::WireFormatLite::ReadMessage(\n" - " input, HasBitSetters::mutable_$name$(this)));\n"); + " input, _Internal::mutable_$name$(this)));\n"); } else if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { format( "DO_(::$proto_ns$::internal::WireFormatLite::ReadMessage(\n" @@ -463,25 +443,17 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { - Formatter format(printer, variables_); - format( - "::$proto_ns$::internal::WireFormatLite::Write$stream_writer$(\n" - " $number$, HasBitSetters::$name$(this), output);\n"); -} - -void MessageFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void MessageFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); format( + "stream->EnsureSpace(&target);\n" "target = ::$proto_ns$::internal::WireFormatLite::\n" " InternalWrite$declared_type$ToArray(\n" - " $number$, HasBitSetters::$name$(this), deterministic, target);\n"); + " $number$, _Internal::$name$(this), target, stream);\n"); } -void MessageFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void MessageFieldGenerator::GenerateByteSize(io::Printer* printer) const { Formatter format(printer, variables_); format( "total_size += $tag_size$ +\n" @@ -505,6 +477,7 @@ void MessageOneofFieldGenerator::GenerateNonInlineAccessorDefinitions( Formatter format(printer, variables_); format( "void $classname$::set_allocated_$name$($type$* $name$) {\n" + "$annotate_accessor$" " ::$proto_ns$::Arena* message_arena = GetArenaNoVirtual();\n" " clear_$oneof_name$();\n" " if ($name$) {\n"); @@ -517,7 +490,7 @@ void MessageOneofFieldGenerator::GenerateNonInlineAccessorDefinitions( " " "reinterpret_cast<::$proto_ns$::MessageLite*>($name$)->GetArena();\n"); } else if (!SupportsArenas(descriptor_->message_type())) { - format(" ::$proto_ns$::Arena* submessage_arena = NULL;\n"); + format(" ::$proto_ns$::Arena* submessage_arena = nullptr;\n"); } else { format( " ::$proto_ns$::Arena* submessage_arena =\n" @@ -535,31 +508,33 @@ void MessageOneofFieldGenerator::GenerateNonInlineAccessorDefinitions( "}\n"); } -void MessageOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$* $classname$::$release_name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_release:$full_name$)\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" " $type$* temp = $field_member$;\n"); if (SupportsArenas(descriptor_)) { format( - " if (GetArenaNoVirtual() != NULL) {\n" + " if (GetArenaNoVirtual() != nullptr) {\n" " temp = ::$proto_ns$::internal::DuplicateIfNonNull(temp);\n" " }\n"); } format( - " $field_member$ = NULL;\n" + " $field_member$ = nullptr;\n" " return temp;\n" " } else {\n" - " return NULL;\n" + " return nullptr;\n" " }\n" "}\n"); format( "inline const $type$& $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return has_$name$()\n" " ? *$field_member$\n" @@ -569,19 +544,21 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (SupportsArenas(descriptor_)) { format( "inline $type$* $classname$::unsafe_arena_release_$name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_unsafe_arena_release" ":$full_name$)\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" " $type$* temp = $field_member$;\n" - " $field_member$ = NULL;\n" + " $field_member$ = nullptr;\n" " return temp;\n" " } else {\n" - " return NULL;\n" + " return nullptr;\n" " }\n" "}\n" "inline void $classname$::unsafe_arena_set_allocated_$name$" "($type$* $name$) {\n" + "$annotate_accessor$" // We rely on the oneof clear method to free the earlier contents of // this oneof. We can directly use the pointer we're given to set the // new value. @@ -597,6 +574,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { format( "inline $type$* $classname$::mutable_$name$() {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -608,12 +586,12 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void MessageOneofFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { format( - "if (GetArenaNoVirtual() == NULL) {\n" + "if (GetArenaNoVirtual() == nullptr) {\n" " delete $field_member$;\n" "}\n"); } else { @@ -621,24 +599,24 @@ GenerateClearingCode(io::Printer* printer) const { } } -void MessageOneofFieldGenerator:: -GenerateMessageClearingCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateMessageClearingCode( + io::Printer* printer) const { GenerateClearingCode(printer); } -void MessageOneofFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { // Don't print any swapping code. Swapping the union will swap this field. } -void MessageOneofFieldGenerator:: -GenerateDestructorCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateDestructorCode( + io::Printer* printer) const { // We inherit from MessageFieldGenerator, so we need to override the default // behavior. } -void MessageOneofFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { // Don't print any constructor code. The field is in a union. We allocate // space only when this field is used. } @@ -656,14 +634,14 @@ RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator( RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {} -void RepeatedMessageFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GeneratePrivateMembers( + io::Printer* printer) const { Formatter format(printer, variables_); format("::$proto_ns$::RepeatedPtrField< $type$ > $name$_;\n"); } -void RepeatedMessageFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$$type$* ${1$mutable_$name$$}$(int index);\n" @@ -676,11 +654,12 @@ GenerateAccessorDeclarations(io::Printer* printer) const { descriptor_); } -void RepeatedMessageFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$* $classname$::mutable_$name$(int index) {\n" + "$annotate_accessor$" // TODO(dlj): move insertion points " // @@protoc_insertion_point(field_mutable:$full_name$)\n" "$type_reference_function$" @@ -688,6 +667,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n" "inline ::$proto_ns$::RepeatedPtrField< $type$ >*\n" "$classname$::mutable_$name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" "$type_reference_function$" " return &$name$_;\n" @@ -696,6 +676,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (options_.safe_boundary_check) { format( "inline const $type$& $classname$::$name$(int index) const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.InternalCheckedGet(index,\n" " *reinterpret_cast(&$type_default_instance$));\n" @@ -703,6 +684,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { } else { format( "inline const $type$& $classname$::$name$(int index) const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" "$type_reference_function$" " return $name$_.Get(index);\n" @@ -711,6 +693,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { format( "inline $type$* $classname$::add_$name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_add:$full_name$)\n" " return $name$_.Add();\n" "}\n"); @@ -718,14 +701,15 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { format( "inline const ::$proto_ns$::RepeatedPtrField< $type$ >&\n" "$classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_list:$full_name$)\n" "$type_reference_function$" " return $name$_;\n" "}\n"); } -void RepeatedMessageFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( @@ -736,8 +720,8 @@ GenerateClearingCode(io::Printer* printer) const { } } -void RepeatedMessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( @@ -749,19 +733,19 @@ GenerateMergingCode(io::Printer* printer) const { } } -void RepeatedMessageFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("CastToBase(&$name$_)->InternalSwap(CastToBase(&other->$name$_));\n"); } -void RepeatedMessageFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { // Not needed for repeated fields. } -void RepeatedMessageFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { if (implicit_weak_field_) { @@ -783,42 +767,36 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } -void RepeatedMessageFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); - format( - "for (unsigned int i = 0,\n" - " n = static_cast(this->$name$_size()); i < n; i++) {\n" - " ::$proto_ns$::internal::WireFormatLite::Write$stream_writer$(\n" - " $number$,\n"); if (implicit_weak_field_) { format( + "for (unsigned int i = 0,\n" + " n = static_cast(this->$name$_size()); i < n; i++) " + "{\n" + " stream->EnsureSpace(&target);\n" + " target = ::$proto_ns$::internal::WireFormatLite::\n" + " InternalWrite$declared_type$ToArray(\n" + " $number$,\n" " CastToBase($name$_).Get<" "::$proto_ns$::internal::ImplicitWeakTypeHandler<$type$>>(" - "static_cast(i)),\n"); + "static_cast(i)), target, stream);\n" + "}\n"); } else { - format(" this->$name$(static_cast(i)),\n"); + format( + "for (auto it = this->$name$().pointer_begin(),\n" + " end = this->$name$().pointer_end(); it < end; ++it) {\n" + " stream->EnsureSpace(&target);\n" + " target = ::$proto_ns$::internal::WireFormatLite::\n" + " InternalWrite$declared_type$ToArray($number$, **it, target, " + "stream);\n" + "}\n"); } - format( - " output);\n" - "}\n"); } -void RepeatedMessageFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { - Formatter format(printer, variables_); - format( - "for (unsigned int i = 0,\n" - " n = static_cast(this->$name$_size()); i < n; i++) {\n" - " target = ::$proto_ns$::internal::WireFormatLite::\n" - " InternalWrite$declared_type$ToArray(\n" - " $number$, this->$name$(static_cast(i)), deterministic, " - "target);\n" - "}\n"); -} - -void RepeatedMessageFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateByteSize( + io::Printer* printer) const { Formatter format(printer, variables_); format( "{\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_message_field.h b/src/google/protobuf/compiler/cpp/cpp_message_field.h index eed8e4f5b4..88b01e05d7 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_message_field.h @@ -67,7 +67,6 @@ class MessageFieldGenerator : public FieldGenerator { void GenerateConstructorCode(io::Printer* printer) const; void GenerateCopyConstructorCode(io::Printer* printer) const; void GenerateMergeFromCodedStream(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; @@ -118,7 +117,6 @@ class RepeatedMessageFieldGenerator : public FieldGenerator { void GenerateConstructorCode(io::Printer* printer) const; void GenerateCopyConstructorCode(io::Printer* printer) const {} void GenerateMergeFromCodedStream(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_options.h b/src/google/protobuf/compiler/cpp/cpp_options.h index 6a364debee..d8fe3a7aef 100644 --- a/src/google/protobuf/compiler/cpp/cpp_options.h +++ b/src/google/protobuf/compiler/cpp/cpp_options.h @@ -35,7 +35,6 @@ #include -#include namespace google { namespace protobuf { namespace compiler { @@ -43,23 +42,30 @@ class AccessInfoMap; namespace cpp { +enum class EnforceOptimizeMode { + kNoEnforcement, // Use the runtime specified by the file specific options. + kSpeed, // This is the full runtime. + kLiteRuntime, +}; + // Generator options (see generator.cc for a description of each): struct Options { - string dllexport_decl; + std::string dllexport_decl; bool safe_boundary_check = false; bool proto_h = false; bool transitive_pb_h = true; bool annotate_headers = false; - bool enforce_lite = false; + EnforceOptimizeMode enforce_mode = EnforceOptimizeMode::kNoEnforcement; bool table_driven_parsing = false; bool table_driven_serialization = false; bool lite_implicit_weak_fields = false; bool bootstrap = false; bool opensource_runtime = false; - bool opensource_include_paths = false; + bool annotate_accessor = false; + std::string runtime_include_base; int num_cc_files = 0; - string annotation_pragma_name; - string annotation_guard_name; + std::string annotation_pragma_name; + std::string annotation_guard_name; const AccessInfoMap* access_info_map = nullptr; }; @@ -68,5 +74,4 @@ struct Options { } // namespace protobuf } // namespace google - #endif // GOOGLE_PROTOBUF_COMPILER_CPP_OPTIONS_H__ diff --git a/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc index 4f8a23ed32..10e468b0b9 100644 --- a/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc @@ -38,8 +38,8 @@ #include #include -#include #include +#include #include #include @@ -58,9 +58,8 @@ class TestGenerator : public CodeGenerator { ~TestGenerator() {} virtual bool Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const { + const std::string& parameter, GeneratorContext* context, + std::string* error) const { TryInsert("test.pb.h", "includes", context); TryInsert("test.pb.h", "namespace_scope", context); TryInsert("test.pb.h", "global_scope", context); @@ -129,7 +128,8 @@ class TestGenerator : public CodeGenerator { // Check field accessors for a message inside oneof{}: TryInsert("test.pb.h", "field_get:foo.Bar.oneOfMessage", context); TryInsert("test.pb.h", "field_mutable:foo.Bar.oneOfMessage", context); - TryInsert("test.pb.cc", "field_set_allocated:foo.Bar.oneOfMessage", context); + TryInsert("test.pb.cc", "field_set_allocated:foo.Bar.oneOfMessage", + context); // Check field accessors for an optional enum: TryInsert("test.pb.h", "field_get:foo.Bar.optEnum", context); @@ -167,7 +167,8 @@ class TestGenerator : public CodeGenerator { return true; } - void TryInsert(const string& filename, const string& insertion_point, + void TryInsert(const std::string& filename, + const std::string& insertion_point, GeneratorContext* context) const { std::unique_ptr output( context->OpenForInsert(filename, insertion_point)); @@ -227,17 +228,12 @@ TEST(CppPluginTest, PluginTest) { cli.RegisterGenerator("--cpp_out", &cpp_generator, ""); cli.RegisterGenerator("--test_out", &test_generator, ""); - string proto_path = "-I" + TestTempDir(); - string cpp_out = "--cpp_out=" + TestTempDir(); - string test_out = "--test_out=" + TestTempDir(); - - const char* argv[] = { - "protoc", - proto_path.c_str(), - cpp_out.c_str(), - test_out.c_str(), - "test.proto" - }; + std::string proto_path = "-I" + TestTempDir(); + std::string cpp_out = "--cpp_out=" + TestTempDir(); + std::string test_out = "--test_out=" + TestTempDir(); + + const char* argv[] = {"protoc", proto_path.c_str(), cpp_out.c_str(), + test_out.c_str(), "test.proto"}; EXPECT_EQ(0, cli.Run(5, argv)); } diff --git a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc index 2f6a050455..62dcdcc7a1 100644 --- a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc @@ -38,6 +38,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -51,45 +52,62 @@ namespace { // returns -1. int FixedSize(FieldDescriptor::Type type) { switch (type) { - case FieldDescriptor::TYPE_INT32 : return -1; - case FieldDescriptor::TYPE_INT64 : return -1; - case FieldDescriptor::TYPE_UINT32 : return -1; - case FieldDescriptor::TYPE_UINT64 : return -1; - case FieldDescriptor::TYPE_SINT32 : return -1; - case FieldDescriptor::TYPE_SINT64 : return -1; - case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size; - case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size; - case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size; - case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size; - case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize; - case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize; - - case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize; - case FieldDescriptor::TYPE_ENUM : return -1; - - case FieldDescriptor::TYPE_STRING : return -1; - case FieldDescriptor::TYPE_BYTES : return -1; - case FieldDescriptor::TYPE_GROUP : return -1; - case FieldDescriptor::TYPE_MESSAGE : return -1; - - // No default because we want the compiler to complain if any new - // types are added. + case FieldDescriptor::TYPE_INT32: + return -1; + case FieldDescriptor::TYPE_INT64: + return -1; + case FieldDescriptor::TYPE_UINT32: + return -1; + case FieldDescriptor::TYPE_UINT64: + return -1; + case FieldDescriptor::TYPE_SINT32: + return -1; + case FieldDescriptor::TYPE_SINT64: + return -1; + case FieldDescriptor::TYPE_FIXED32: + return WireFormatLite::kFixed32Size; + case FieldDescriptor::TYPE_FIXED64: + return WireFormatLite::kFixed64Size; + case FieldDescriptor::TYPE_SFIXED32: + return WireFormatLite::kSFixed32Size; + case FieldDescriptor::TYPE_SFIXED64: + return WireFormatLite::kSFixed64Size; + case FieldDescriptor::TYPE_FLOAT: + return WireFormatLite::kFloatSize; + case FieldDescriptor::TYPE_DOUBLE: + return WireFormatLite::kDoubleSize; + + case FieldDescriptor::TYPE_BOOL: + return WireFormatLite::kBoolSize; + case FieldDescriptor::TYPE_ENUM: + return -1; + + case FieldDescriptor::TYPE_STRING: + return -1; + case FieldDescriptor::TYPE_BYTES: + return -1; + case FieldDescriptor::TYPE_GROUP: + return -1; + case FieldDescriptor::TYPE_MESSAGE: + return -1; + + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; return -1; } void SetPrimitiveVariables(const FieldDescriptor* descriptor, - std::map* variables, + std::map* variables, const Options& options) { SetCommonFieldVariables(descriptor, variables, options); (*variables)["type"] = PrimitiveTypeName(options, descriptor->cpp_type()); (*variables)["default"] = DefaultValue(options, descriptor); - (*variables)["tag"] = - SimpleItoa(internal::WireFormat::MakeTag(descriptor)); + (*variables)["tag"] = StrCat(internal::WireFormat::MakeTag(descriptor)); int fixed_size = FixedSize(descriptor->type()); if (fixed_size != -1) { - (*variables)["fixed_size"] = SimpleItoa(fixed_size); + (*variables)["fixed_size"] = StrCat(fixed_size); } (*variables)["wire_format_field_type"] = FieldDescriptorProto_Type_Name( static_cast(descriptor->type())); @@ -108,14 +126,14 @@ PrimitiveFieldGenerator::PrimitiveFieldGenerator( PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {} -void PrimitiveFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void PrimitiveFieldGenerator::GeneratePrivateMembers( + io::Printer* printer) const { Formatter format(printer, variables_); format("$type$ $name$_;\n"); } -void PrimitiveFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$$type$ ${1$$name$$}$() const;\n" @@ -123,53 +141,52 @@ GenerateAccessorDeclarations(io::Printer* printer) const { descriptor_); } -void PrimitiveFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_;\n" "}\n" "inline void $classname$::set_$name$($type$ value) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_ = value;\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n"); } -void PrimitiveFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateClearingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_ = $default$;\n"); } -void PrimitiveFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("set_$name$(from.$name$());\n"); } -void PrimitiveFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateSwappingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("swap($name$_, other->$name$_);\n"); } -void PrimitiveFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_ = $default$;\n"); } -void PrimitiveFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_ = from.$name$_;\n"); } -void PrimitiveFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$set_hasbit_io$\n" @@ -179,25 +196,17 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { " input, &$name$_)));\n"); } -void PrimitiveFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { - Formatter format(printer, variables_); - format( - "::$proto_ns$::internal::WireFormatLite::Write$declared_type$(" - "$number$, this->$name$(), output);\n"); -} - -void PrimitiveFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); format( + "stream->EnsureSpace(&target);\n" "target = " "::$proto_ns$::internal::WireFormatLite::Write$declared_type$ToArray(" "$number$, this->$name$(), target);\n"); } -void PrimitiveFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateByteSize(io::Printer* printer) const { Formatter format(printer, variables_); int fixed_size = FixedSize(descriptor_->type()); if (fixed_size == -1) { @@ -212,20 +221,20 @@ GenerateByteSize(io::Printer* printer) const { // =================================================================== -PrimitiveOneofFieldGenerator:: -PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, - const Options& options) - : PrimitiveFieldGenerator(descriptor, options) { +PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator( + const FieldDescriptor* descriptor, const Options& options) + : PrimitiveFieldGenerator(descriptor, options) { SetCommonOneofFieldVariables(descriptor, &variables_); } PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {} -void PrimitiveOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return $field_member$;\n" @@ -233,6 +242,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " return $default$;\n" "}\n" "inline void $classname$::set_$name$($type$ value) {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -242,25 +252,25 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void PrimitiveOneofFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$field_member$ = $default$;\n"); } -void PrimitiveOneofFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { // Don't print any swapping code. Swapping the union will swap this field. } -void PrimitiveOneofFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$ns$::_$classname$_default_instance_.$name$_ = $default$;\n"); } -void PrimitiveOneofFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); format( "clear_$oneof_name$();\n" @@ -289,8 +299,8 @@ RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator( RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {} -void RepeatedPrimitiveFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GeneratePrivateMembers( + io::Printer* printer) const { Formatter format(printer, variables_); format("::$proto_ns$::RepeatedField< $type$ > $name$_;\n"); if (descriptor_->is_packed() && @@ -299,8 +309,8 @@ GeneratePrivateMembers(io::Printer* printer) const { } } -void RepeatedPrimitiveFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$$type$ ${1$$name$$}$(int index) const;\n" @@ -313,65 +323,70 @@ GenerateAccessorDeclarations(io::Printer* printer) const { descriptor_); } -void RepeatedPrimitiveFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$(int index) const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.Get(index);\n" "}\n" "inline void $classname$::set_$name$(int index, $type$ value) {\n" + "$annotate_accessor$" " $name$_.Set(index, value);\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "inline void $classname$::add_$name$($type$ value) {\n" + "$annotate_accessor$" " $name$_.Add(value);\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" "}\n" "inline const ::$proto_ns$::RepeatedField< $type$ >&\n" "$classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_list:$full_name$)\n" " return $name$_;\n" "}\n" "inline ::$proto_ns$::RepeatedField< $type$ >*\n" "$classname$::mutable_$name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" " return &$name$_;\n" "}\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.Clear();\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.MergeFrom(from.$name$_);\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.InternalSwap(&other->$name$_);\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { // Not needed for repeated fields. } -void RepeatedPrimitiveFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.CopyFrom(from.$name$_);\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); format( "DO_((::$proto_ns$::internal::WireFormatLite::$repeated_reader$<\n" @@ -380,8 +395,8 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { " $tag_size$, $tag$u, input, this->mutable_$name$())));\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateMergeFromCodedStreamWithPacking( + io::Printer* printer) const { Formatter format(printer, variables_); format( "DO_((::$proto_ns$::internal::WireFormatLite::$packed_reader$<\n" @@ -390,77 +405,38 @@ GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { " input, this->mutable_$name$())));\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); - bool array_written = false; if (descriptor_->is_packed()) { - // Write the tag and the size. - format( - "if (this->$name$_size() > 0) {\n" - " ::$proto_ns$::internal::WireFormatLite::WriteTag(" - "$number$, " - "::$proto_ns$::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, " - "output);\n" - " output->WriteVarint32(_$name$_cached_byte_size_.load(\n" - " std::memory_order_relaxed));\n"); - if (FixedSize(descriptor_->type()) > 0) { - // TODO(ckennelly): Use RepeatedField::unsafe_data() via - // WireFormatLite to access the contents of this->$name$_ to save a branch - // here. format( - " " - "::$proto_ns$::internal::WireFormatLite::Write$declared_type$Array(\n" - " this->$name$().data(), this->$name$_size(), output);\n"); - array_written = true; // Wrote array all at once - } - format("}\n"); - } - if (!array_written) { - format("for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); - if (descriptor_->is_packed()) { - format( - " " - "::$proto_ns$::internal::WireFormatLite::Write$declared_type$NoTag(\n" - " this->$name$(i), output);\n"); + "if (this->$name$_size() > 0) {\n" + " target = stream->WriteFixedPacked($number$, $name$_, target);\n" + "}\n"); } else { format( - " ::$proto_ns$::internal::WireFormatLite::Write$declared_type$(\n" - " $number$, this->$name$(i), output);\n"); + "{\n" + " int byte_size = " + "_$name$_cached_byte_size_.load(std::memory_order_relaxed);\n" + " if (byte_size > 0) {\n" + " target = stream->Write$declared_type$Packed(\n" + " $number$, $name$_, byte_size, target);\n" + " }\n" + "}\n"); } - format("}\n"); - } -} - -void RepeatedPrimitiveFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { - Formatter format(printer, variables_); - if (descriptor_->is_packed()) { - // Write the tag and the size. - format( - "if (this->$name$_size() > 0) {\n" - " target = ::$proto_ns$::internal::WireFormatLite::WriteTagToArray(\n" - " $number$,\n" - " " - "::$proto_ns$::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n" - " target);\n" - " target = " - "::$proto_ns$::io::CodedOutputStream::WriteVarint32ToArray(\n" - " _$name$_cached_byte_size_.load(std::memory_order_relaxed),\n" - " target);\n" - " target = ::$proto_ns$::internal::WireFormatLite::\n" - " Write$declared_type$NoTagToArray(this->$name$_, target);\n" - "}\n"); } else { format( - "target = ::$proto_ns$::internal::WireFormatLite::\n" - " Write$declared_type$ToArray($number$, this->$name$_, target);\n"); + "for (const auto& x : this->$name$()) {\n" + " stream->EnsureSpace(&target);\n" + " target = ::$proto_ns$::internal::WireFormatLite::" + "Write$declared_type$ToArray($number$, x, target);\n" + "}\n"); } } -void RepeatedPrimitiveFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateByteSize( + io::Printer* printer) const { Formatter format(printer, variables_); format("{\n"); format.Indent(); diff --git a/src/google/protobuf/compiler/cpp/cpp_primitive_field.h b/src/google/protobuf/compiler/cpp/cpp_primitive_field.h index 77389aa0bd..18b67ad47a 100644 --- a/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_primitive_field.h @@ -60,7 +60,6 @@ class PrimitiveFieldGenerator : public FieldGenerator { void GenerateConstructorCode(io::Printer* printer) const; void GenerateCopyConstructorCode(io::Printer* printer) const; void GenerateMergeFromCodedStream(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; @@ -102,7 +101,6 @@ class RepeatedPrimitiveFieldGenerator : public FieldGenerator { void GenerateCopyConstructorCode(io::Printer* printer) const; void GenerateMergeFromCodedStream(io::Printer* printer) const; void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_service.cc b/src/google/protobuf/compiler/cpp/cpp_service.cc index 677b73746e..994e653e84 100644 --- a/src/google/protobuf/compiler/cpp/cpp_service.cc +++ b/src/google/protobuf/compiler/cpp/cpp_service.cc @@ -44,18 +44,20 @@ namespace cpp { namespace { -void InitMethodVariables(const MethodDescriptor* method, Formatter* format) { +void InitMethodVariables(const MethodDescriptor* method, const Options& options, + Formatter* format) { format->Set("name", method->name()); - format->Set("input_type", QualifiedClassName(method->input_type())); - format->Set("output_type", QualifiedClassName(method->output_type())); + format->Set("input_type", QualifiedClassName(method->input_type(), options)); + format->Set("output_type", + QualifiedClassName(method->output_type(), options)); } } // namespace -ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor, - const std::map& vars, - const Options& options) - : descriptor_(descriptor), vars_(vars) { +ServiceGenerator::ServiceGenerator( + const ServiceDescriptor* descriptor, + const std::map& vars, const Options& options) + : descriptor_(descriptor), vars_(vars), options_(options) { vars_["classname"] = descriptor_->name(); vars_["full_name"] = descriptor_->full_name(); } @@ -153,7 +155,7 @@ void ServiceGenerator::GenerateMethodSignatures(VirtualOrNon virtual_or_non, for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); Formatter format(printer, vars_); - InitMethodVariables(method, &format); + InitMethodVariables(method, options_, &format); format.Set("virtual", virtual_or_non == VIRTUAL ? "virtual " : ""); format( "$virtual$void $name$(::$proto_ns$::RpcController* controller,\n" @@ -165,14 +167,6 @@ void ServiceGenerator::GenerateMethodSignatures(VirtualOrNon virtual_or_non, // =================================================================== -void ServiceGenerator::GenerateDescriptorInitializer(io::Printer* printer, - int index) { - Formatter format(printer, vars_); - format("$classname$_descriptor_ = file->service($1$);\n", index); -} - -// =================================================================== - void ServiceGenerator::GenerateImplementation(io::Printer* printer) { Formatter format(printer, vars_); format( @@ -180,7 +174,7 @@ void ServiceGenerator::GenerateImplementation(io::Printer* printer) { "\n" "const ::$proto_ns$::ServiceDescriptor* $classname$::descriptor() {\n" " " - "::$proto_ns$::internal::AssignDescriptors(&$assign_desc_table$);\n" + "::$proto_ns$::internal::AssignDescriptors(&$desc_table$);\n" " return $file_level_service_descriptors$[$1$];\n" "}\n" "\n" @@ -219,7 +213,7 @@ void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) { for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); Formatter format(printer, vars_); - InitMethodVariables(method, &format); + InitMethodVariables(method, options_, &format); format( "void $classname$::$name$(::$proto_ns$::RpcController* controller,\n" " const $input_type$*,\n" @@ -248,15 +242,17 @@ void ServiceGenerator::GenerateCallMethod(io::Printer* printer) { for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); Formatter format(printer, vars_); - InitMethodVariables(method, &format); + InitMethodVariables(method, options_, &format); // Note: down_cast does not work here because it only works on pointers, // not references. format( " case $1$:\n" " $name$(controller,\n" - " ::google::protobuf::down_cast(request),\n" - " ::google::protobuf::down_cast< $output_type$*>(response),\n" + " ::$proto_ns$::internal::DownCast(\n" + " request),\n" + " ::$proto_ns$::internal::DownCast<$output_type$*>(\n" + " response),\n" " done);\n" " break;\n", i); @@ -293,7 +289,7 @@ void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which, format( " case $1$:\n" " return $2$::default_instance();\n", - i, QualifiedClassName(type)); + i, QualifiedClassName(type, options_)); } format( @@ -311,7 +307,7 @@ void ServiceGenerator::GenerateStubMethods(io::Printer* printer) { for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); Formatter format(printer, vars_); - InitMethodVariables(method, &format); + InitMethodVariables(method, options_, &format); format( "void $classname$_Stub::$name$(::$proto_ns$::RpcController* " "controller,\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_service.h b/src/google/protobuf/compiler/cpp/cpp_service.h index 3acbe637c5..63c7ca44f8 100644 --- a/src/google/protobuf/compiler/cpp/cpp_service.h +++ b/src/google/protobuf/compiler/cpp/cpp_service.h @@ -57,7 +57,7 @@ class ServiceGenerator { public: // See generator.cc for the meaning of dllexport_decl. explicit ServiceGenerator(const ServiceDescriptor* descriptor, - const std::map& vars, + const std::map& vars, const Options& options); ~ServiceGenerator(); @@ -69,10 +69,6 @@ class ServiceGenerator { // Source file stuff. - // Generate code that initializes the global variable storing the service's - // descriptor. - void GenerateDescriptorInitializer(io::Printer* printer, int index); - // Generate implementations of everything declared by // GenerateDeclarations(). void GenerateImplementation(io::Printer* printer); @@ -109,7 +105,8 @@ class ServiceGenerator { void GenerateStubMethods(io::Printer* printer); const ServiceDescriptor* descriptor_; - std::map vars_; + std::map vars_; + const Options& options_; int index_in_metadata_; diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.cc b/src/google/protobuf/compiler/cpp/cpp_string_field.cc index ecfabe644c..455ac6ed3f 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.cc @@ -39,6 +39,7 @@ #include + namespace google { namespace protobuf { namespace compiler { @@ -47,28 +48,27 @@ namespace cpp { namespace { void SetStringVariables(const FieldDescriptor* descriptor, - std::map* variables, + std::map* variables, const Options& options) { SetCommonFieldVariables(descriptor, variables, options); (*variables)["default"] = DefaultValue(options, descriptor); (*variables)["default_length"] = - SimpleItoa(descriptor->default_value_string().length()); - string default_variable_string = MakeDefaultName(descriptor); + StrCat(descriptor->default_value_string().length()); + std::string default_variable_string = MakeDefaultName(descriptor); (*variables)["default_variable_name"] = default_variable_string; (*variables)["default_variable"] = descriptor->default_value_string().empty() ? "&::" + (*variables)["proto_ns"] + "::internal::GetEmptyStringAlreadyInited()" - : "&" + Namespace(descriptor) + "::" + (*variables)["classname"] + + : "&" + QualifiedClassName(descriptor->containing_type(), options) + "::" + default_variable_string + ".get()"; (*variables)["pointer_type"] = descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char"; - (*variables)["null_check"] = (*variables)["DCHK"] + "(value != NULL);\n"; + (*variables)["null_check"] = (*variables)["DCHK"] + "(value != nullptr);\n"; // NOTE: Escaped here to unblock proto1->proto2 migration. // TODO(liujisi): Extend this to apply for other conflicting methods. (*variables)["release_name"] = - SafeFunctionName(descriptor->containing_type(), - descriptor, "release_"); + SafeFunctionName(descriptor->containing_type(), descriptor, "release_"); (*variables)["full_name"] = descriptor->full_name(); if (options.opensource_runtime) { @@ -89,28 +89,13 @@ StringFieldGenerator::StringFieldGenerator(const FieldDescriptor* descriptor, const Options& options) : FieldGenerator(descriptor, options), lite_(!HasDescriptorMethods(descriptor->file(), options)), - inlined_(false) { - - // TODO(ckennelly): Handle inlining for any.proto. - if (IsAnyMessage(descriptor_->containing_type())) { - inlined_ = false; - } - if (descriptor_->containing_type()->options().map_entry()) { - inlined_ = false; - } - - // Limit to proto2, as we rely on has bits to distinguish field presence for - // release_$name$. On proto3, we cannot use the address of the string - // instance when the field has been inlined. - inlined_ = inlined_ && HasFieldPresence(descriptor_->file()); - + inlined_(IsStringInlined(descriptor, options)) { SetStringVariables(descriptor, &variables_, options); } StringFieldGenerator::~StringFieldGenerator() {} -void StringFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void StringFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const { Formatter format(printer, variables_); if (inlined_) { format("::$proto_ns$::internal::InlinedStringField $name$_;\n"); @@ -131,22 +116,21 @@ GeneratePrivateMembers(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateStaticMembers(io::Printer* printer) const { +void StringFieldGenerator::GenerateStaticMembers(io::Printer* printer) const { Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { // We make the default instance public, so it can be initialized by // non-friend code. format( "public:\n" - "static ::$proto_ns$::internal::ExplicitlyConstructed<$string$>" + "static ::$proto_ns$::internal::ExplicitlyConstructed" " $default_variable_name$;\n" "private:\n"); } } -void StringFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void StringFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); // If we're using StringFieldGenerator for a field with a ctype, it's // because that ctype isn't actually implemented. In particular, this is @@ -177,28 +161,24 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } format( - "$deprecated_attr$const $string$& ${1$$name$$}$() const;\n" - "$deprecated_attr$void ${1$set_$name$$}$(const $string$& value);\n" - "#if LANG_CXX11\n" - "$deprecated_attr$void ${1$set_$name$$}$($string$&& value);\n" - "#endif\n" + "$deprecated_attr$const std::string& ${1$$name$$}$() const;\n" + "$deprecated_attr$void ${1$set_$name$$}$(const std::string& value);\n" + "$deprecated_attr$void ${1$set_$name$$}$(std::string&& value);\n" "$deprecated_attr$void ${1$set_$name$$}$(const char* value);\n", descriptor_); if (!options_.opensource_runtime) { format( - "$deprecated_attr$void ${1$set_$name$$}$(::StringPiece value);\n" - "#ifdef HAS_GLOBAL_STRING\n" - "$deprecated_attr$void ${1$set_$name$$}$(const ::std::string& value);\n" - "#endif\n", + "$deprecated_attr$void ${1$set_$name$$}$(::StringPiece value);\n", descriptor_); } format( "$deprecated_attr$void ${1$set_$name$$}$(const $pointer_type$* " "value, size_t size)" ";\n" - "$deprecated_attr$$string$* ${1$mutable_$name$$}$();\n" - "$deprecated_attr$$string$* ${1$$release_name$$}$();\n" - "$deprecated_attr$void ${1$set_allocated_$name$$}$($string$* $name$);\n", + "$deprecated_attr$std::string* ${1$mutable_$name$$}$();\n" + "$deprecated_attr$std::string* ${1$$release_name$$}$();\n" + "$deprecated_attr$void ${1$set_allocated_$name$$}$(std::string* " + "$name$);\n", descriptor_); if (options_.opensource_runtime) { if (SupportsArenas(descriptor_)) { @@ -207,13 +187,13 @@ GenerateAccessorDeclarations(io::Printer* printer) const { "for\"\n" "\" string fields are deprecated and will be removed in a\"\n" "\" future release.\")\n" - "$string$* ${1$unsafe_arena_release_$name$$}$();\n" + "std::string* ${1$unsafe_arena_release_$name$$}$();\n" "$GOOGLE_PROTOBUF$_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors " "for\"\n" "\" string fields are deprecated and will be removed in a\"\n" "\" future release.\")\n" "void ${1$unsafe_arena_set_allocated_$name$$}$(\n" - " $string$* $name$);\n", + " std::string* $name$);\n", descriptor_); } } @@ -225,29 +205,31 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void StringFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { format( - "inline const $string$& $classname$::$name$() const {\n" + "inline const std::string& $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.Get();\n" "}\n" - "inline void $classname$::set_$name$(const $string$& value) {\n" + "inline void $classname$::set_$name$(const std::string& value) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_.Set$lite$($default_variable$, value, GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" - "#if LANG_CXX11\n" - "inline void $classname$::set_$name$($string$&& value) {\n" + "inline void $classname$::set_$name$(std::string&& value) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_.Set$lite$(\n" " $default_variable$, ::std::move(value), GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n" "}\n" - "#endif\n" "inline void $classname$::set_$name$(const char* value) {\n" + "$annotate_accessor$" " $null_check$" " $set_hasbit$\n" " $name$_.Set$lite$($default_variable$, $string_piece$(value),\n" @@ -257,43 +239,38 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (!options_.opensource_runtime) { format( "inline void $classname$::set_$name$(::StringPiece value) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_.Set$lite$($default_variable$, value, " "GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" - "}\n" - "#ifdef HAS_GLOBAL_STRING\n" - "inline void $classname$::set_$name$(const ::std::string& value) {\n" - " $set_hasbit$\n" - " $name$_.Set$lite$($default_variable$, " - "::StringPiece(value.data(),\n" - " value.size()), GetArenaNoVirtual());\n" - " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" - "}\n" - "#endif\n"); + "}\n"); } format( "inline " "void $classname$::set_$name$(const $pointer_type$* value,\n" " size_t size) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_.Set$lite$($default_variable$, $string_piece$(\n" " reinterpret_cast(value), size), " "GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline $string$* $classname$::mutable_$name$() {\n" + "inline std::string* $classname$::mutable_$name$() {\n" + "$annotate_accessor$" " $set_hasbit$\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n" "}\n" - "inline $string$* $classname$::$release_name$() {\n" + "inline std::string* $classname$::$release_name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_release:$full_name$)\n"); if (HasFieldPresence(descriptor_->file())) { format( " if (!has_$name$()) {\n" - " return NULL;\n" + " return nullptr;\n" " }\n" " $clear_hasbit$\n" " return $name$_.ReleaseNonDefault(" @@ -307,8 +284,9 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { format( "}\n" - "inline void $classname$::set_allocated_$name$($string$* $name$) {\n" - " if ($name$ != NULL) {\n" + "inline void $classname$::set_allocated_$name$(std::string* $name$) {\n" + "$annotate_accessor$" + " if ($name$ != nullptr) {\n" " $set_hasbit$\n" " } else {\n" " $clear_hasbit$\n" @@ -319,18 +297,20 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); if (options_.opensource_runtime) { format( - "inline $string$* $classname$::unsafe_arena_release_$name$() {\n" + "inline std::string* $classname$::unsafe_arena_release_$name$() {\n" + "$annotate_accessor$" " // " "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" - " $DCHK$(GetArenaNoVirtual() != NULL);\n" + " $DCHK$(GetArenaNoVirtual() != nullptr);\n" " $clear_hasbit$\n" " return $name$_.UnsafeArenaRelease($default_variable$,\n" " GetArenaNoVirtual());\n" "}\n" "inline void $classname$::unsafe_arena_set_allocated_$name$(\n" - " $string$* $name$) {\n" - " $DCHK$(GetArenaNoVirtual() != NULL);\n" - " if ($name$ != NULL) {\n" + "$annotate_accessor$" + " std::string* $name$) {\n" + " $DCHK$(GetArenaNoVirtual() != nullptr);\n" + " if ($name$ != nullptr) {\n" " $set_hasbit$\n" " } else {\n" " $clear_hasbit$\n" @@ -344,24 +324,26 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { } else { // No-arena case. format( - "inline const $string$& $classname$::$name$() const {\n" + "inline const std::string& $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.GetNoArena();\n" "}\n" - "inline void $classname$::set_$name$(const $string$& value) {\n" + "inline void $classname$::set_$name$(const std::string& value) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_.SetNoArena($default_variable$, value);\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" - "#if LANG_CXX11\n" - "inline void $classname$::set_$name$($string$&& value) {\n" + "inline void $classname$::set_$name$(std::string&& value) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_.SetNoArena(\n" " $default_variable$, ::std::move(value));\n" " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n" "}\n" - "#endif\n" "inline void $classname$::set_$name$(const char* value) {\n" + "$annotate_accessor$" " $null_check$" " $set_hasbit$\n" " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n" @@ -370,40 +352,36 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (!options_.opensource_runtime) { format( "inline void $classname$::set_$name$(::StringPiece value) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_.SetNoArena($default_variable$, value);\n" " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" - "}\n" - "#ifdef HAS_GLOBAL_STRING\n" - "inline void $classname$::set_$name$(const ::std::string& value) {\n" - " $set_hasbit$\n" - " $name$_.SetNoArena($default_variable$,\n" - " ::StringPiece(value.data(), value.size()));\n" - " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" - "}\n" - "#endif\n"); + "}\n"); } format( "inline " "void $classname$::set_$name$(const $pointer_type$* value, " "size_t size) {\n" + "$annotate_accessor$" " $set_hasbit$\n" " $name$_.SetNoArena($default_variable$,\n" " $string_piece$(reinterpret_cast(value), size));\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline $string$* $classname$::mutable_$name$() {\n" + "inline std::string* $classname$::mutable_$name$() {\n" + "$annotate_accessor$" " $set_hasbit$\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.MutableNoArena($default_variable$);\n" "}\n" - "inline $string$* $classname$::$release_name$() {\n" + "inline std::string* $classname$::$release_name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_release:$full_name$)\n"); if (HasFieldPresence(descriptor_->file())) { format( " if (!has_$name$()) {\n" - " return NULL;\n" + " return nullptr;\n" " }\n" " $clear_hasbit$\n" " return $name$_.ReleaseNonDefaultNoArena($default_variable$);\n"); @@ -415,8 +393,9 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { format( "}\n" - "inline void $classname$::set_allocated_$name$($string$* $name$) {\n" - " if ($name$ != NULL) {\n" + "inline void $classname$::set_allocated_$name$(std::string* $name$) {\n" + "$annotate_accessor$" + " if ($name$ != nullptr) {\n" " $set_hasbit$\n" " } else {\n" " $clear_hasbit$\n" @@ -427,19 +406,18 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { +void StringFieldGenerator::GenerateNonInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { // Initialized in GenerateDefaultInstanceAllocator. format( - "::$proto_ns$::internal::ExplicitlyConstructed<$string$> " + "::$proto_ns$::internal::ExplicitlyConstructed " "$classname$::$default_variable_name$;\n"); } } -void StringFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateClearingCode(io::Printer* printer) const { Formatter format(printer, variables_); // Two-dimension specialization here: supporting arenas or not, and default // value is the empty string or not. Complexity here ensures the minimal @@ -462,8 +440,8 @@ GenerateClearingCode(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateMessageClearingCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateMessageClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); // Two-dimension specialization here: supporting arenas, field presence, or // not, and default value is the empty string or not. Complexity here ensures @@ -473,8 +451,7 @@ GenerateMessageClearingCode(io::Printer* printer) const { // If we have field presence, then the Clear() method of the protocol buffer // will have checked that this field is set. If so, we can avoid redundant // checks against default_variable. - const bool must_be_present = - HasFieldPresence(descriptor_->file()); + const bool must_be_present = HasFieldPresence(descriptor_->file()); if (inlined_ && must_be_present) { // Calling mutable_$name$() gives us a string reference and sets the has bit @@ -519,8 +496,7 @@ GenerateMessageClearingCode(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateMergingCode(io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) { // TODO(gpike): improve this @@ -532,8 +508,7 @@ GenerateMergingCode(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateSwappingCode(io::Printer* printer) const { Formatter format(printer, variables_); if (inlined_) { format("$name$_.Swap(&other->$name$_);\n"); @@ -544,8 +519,7 @@ GenerateSwappingCode(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateConstructorCode(io::Printer* printer) const { Formatter format(printer, variables_); // TODO(ckennelly): Construct non-empty strings as part of the initializer // list. @@ -557,15 +531,15 @@ GenerateConstructorCode(io::Printer* printer) const { format("$name$_.UnsafeSetDefault($default_variable$);\n"); } -void StringFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); GenerateConstructorCode(printer); if (HasFieldPresence(descriptor_->file())) { format("if (from.has_$name$()) {\n"); } else { - format("if (from.$name$().size() > 0) {\n"); + format("if (!from.$name$().empty()) {\n"); } format.Indent(); @@ -583,8 +557,7 @@ GenerateCopyConstructorCode(io::Printer* printer) const { format("}\n"); } -void StringFieldGenerator:: -GenerateDestructorCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateDestructorCode(io::Printer* printer) const { Formatter format(printer, variables_); if (inlined_) { // The destructor is automatically invoked. @@ -605,34 +578,34 @@ bool StringFieldGenerator::GenerateArenaDestructorCode( return true; } -void StringFieldGenerator:: -GenerateDefaultInstanceAllocator(io::Printer* printer) const { +void StringFieldGenerator::GenerateDefaultInstanceAllocator( + io::Printer* printer) const { Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { format( "$ns$::$classname$::$default_variable_name$.DefaultConstruct();\n" "*$ns$::$classname$::$default_variable_name$.get_mutable() = " - "$string$($default$, $default_length$);\n" + "std::string($default$, $default_length$);\n" "::$proto_ns$::internal::OnShutdownDestroyString(\n" " $ns$::$classname$::$default_variable_name$.get_mutable());\n"); } } -void StringFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void StringFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); // The google3 version of proto2 has ArenaStrings and parses into them - // directly, but for the open-source release, we always parse into ::std::string + // directly, but for the open-source release, we always parse into std::string // instances. Note that for lite, we do similarly to the open source release - // and use ::std::string, not ArenaString. + // and use std::string, not ArenaString. if (!options_.opensource_runtime && !inlined_ && SupportsArenas(descriptor_) && !lite_) { // If arena != NULL, the current string is either an ArenaString (no - // destructor necessary) or a materialized ::std::string (and is on the Arena's - // destructor list). No call to ArenaStringPtr::Destroy is needed. + // destructor necessary) or a materialized std::string (and is on the + // Arena's destructor list). No call to ArenaStringPtr::Destroy is needed. format( - "if (arena != NULL) {\n" - " ::$proto_ns$::internal::TaggedPtr<$string$> str =\n" + "if (arena != nullptr) {\n" + " ::$proto_ns$::internal::TaggedPtr str =\n" " ::$proto_ns$::internal::ReadArenaString(input, arena);\n" " DO_(!str.IsNull());\n" " $set_hasbit_io$\n" @@ -655,28 +628,12 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } -bool StringFieldGenerator:: -MergeFromCodedStreamNeedsArena() const { +bool StringFieldGenerator::MergeFromCodedStreamNeedsArena() const { return !lite_ && !inlined_ && !options_.opensource_runtime; } -void StringFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { - Formatter format(printer, variables_); - if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { - GenerateUtf8CheckCodeForString( - descriptor_, options_, false, - "this->$name$().data(), static_cast(this->$name$().length()),\n", - format); - } - format( - "::$proto_ns$::internal::WireFormatLite::Write$declared_type$" - "MaybeAliased(\n" - " $number$, this->$name$(), output);\n"); -} - -void StringFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void StringFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( @@ -685,13 +642,11 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { format); } format( - "target =\n" - " ::$proto_ns$::internal::WireFormatLite::Write$declared_type$ToArray(\n" + "target = stream->Write$declared_type$MaybeAliased(\n" " $number$, this->$name$(), target);\n"); } -void StringFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void StringFieldGenerator::GenerateByteSize(io::Printer* printer) const { Formatter format(printer, variables_); format( "total_size += $tag_size$ +\n" @@ -715,19 +670,21 @@ StringOneofFieldGenerator::StringOneofFieldGenerator( StringOneofFieldGenerator::~StringOneofFieldGenerator() {} -void StringOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { format( - "inline const $string$& $classname$::$name$() const {\n" + "inline const std::string& $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return $field_member$.Get();\n" " }\n" " return *$default_variable$;\n" "}\n" - "inline void $classname$::set_$name$(const $string$& value) {\n" + "inline void $classname$::set_$name$(const std::string& value) {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -737,8 +694,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" - "#if LANG_CXX11\n" - "inline void $classname$::set_$name$($string$&& value) {\n" + "inline void $classname$::set_$name$(std::string&& value) {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -749,8 +706,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $default_variable$, ::std::move(value), GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n" "}\n" - "#endif\n" "inline void $classname$::set_$name$(const char* value) {\n" + "$annotate_accessor$" " $null_check$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -764,6 +721,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (!options_.opensource_runtime) { format( "inline void $classname$::set_$name$(::StringPiece value) {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -772,25 +730,13 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $field_member$.Set$lite$($default_variable$, value,\n" " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" - "}\n" - "#ifdef HAS_GLOBAL_STRING\n" - "inline void $classname$::set_$name$(const ::std::string& value) {\n" - " if (!has_$name$()) {\n" - " clear_$oneof_name$();\n" - " set_has_$name$();\n" - " $field_member$.UnsafeSetDefault($default_variable$);\n" - " }\n" - " $field_member$.Set$lite$($default_variable$,\n" - " ::StringPiece(value.data(), value.size()), " - "GetArenaNoVirtual());\n" - " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" - "}\n" - "#endif\n"); + "}\n"); } format( "inline " "void $classname$::set_$name$(const $pointer_type$* value,\n" " size_t size) {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -802,7 +748,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline $string$* $classname$::mutable_$name$() {\n" + "inline std::string* $classname$::mutable_$name$() {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -812,21 +759,23 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" "}\n" - "inline $string$* $classname$::$release_name$() {\n" + "inline std::string* $classname$::$release_name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_release:$full_name$)\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" " return $field_member$.Release($default_variable$,\n" " GetArenaNoVirtual());\n" " } else {\n" - " return NULL;\n" + " return nullptr;\n" " }\n" "}\n" - "inline void $classname$::set_allocated_$name$($string$* $name$) {\n" + "inline void $classname$::set_allocated_$name$(std::string* $name$) {\n" + "$annotate_accessor$" " if (has_$oneof_name$()) {\n" " clear_$oneof_name$();\n" " }\n" - " if ($name$ != NULL) {\n" + " if ($name$ != nullptr) {\n" " set_has_$name$();\n" " $field_member$.UnsafeSetDefault($name$);\n" " }\n" @@ -834,21 +783,23 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); if (options_.opensource_runtime) { format( - "inline $string$* $classname$::unsafe_arena_release_$name$() {\n" + "inline std::string* $classname$::unsafe_arena_release_$name$() {\n" + "$annotate_accessor$" " // " "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" - " $DCHK$(GetArenaNoVirtual() != NULL);\n" + " $DCHK$(GetArenaNoVirtual() != nullptr);\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" " return $field_member$.UnsafeArenaRelease(\n" " $default_variable$, GetArenaNoVirtual());\n" " } else {\n" - " return NULL;\n" + " return nullptr;\n" " }\n" "}\n" "inline void $classname$::unsafe_arena_set_allocated_$name$(" - "$string$* $name$) {\n" - " $DCHK$(GetArenaNoVirtual() != NULL);\n" + "std::string* $name$) {\n" + "$annotate_accessor$" + " $DCHK$(GetArenaNoVirtual() != nullptr);\n" " if (!has_$name$()) {\n" " $field_member$.UnsafeSetDefault($default_variable$);\n" " }\n" @@ -865,14 +816,16 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { } else { // No-arena case. format( - "inline const $string$& $classname$::$name$() const {\n" + "inline const std::string& $classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return $field_member$.GetNoArena();\n" " }\n" " return *$default_variable$;\n" "}\n" - "inline void $classname$::set_$name$(const $string$& value) {\n" + "inline void $classname$::set_$name$(const std::string& value) {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -882,8 +835,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $field_member$.SetNoArena($default_variable$, value);\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" - "#if LANG_CXX11\n" - "inline void $classname$::set_$name$($string$&& value) {\n" + "inline void $classname$::set_$name$(std::string&& value) {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -893,8 +846,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $field_member$.SetNoArena($default_variable$, ::std::move(value));\n" " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n" "}\n" - "#endif\n" "inline void $classname$::set_$name$(const char* value) {\n" + "$annotate_accessor$" " $null_check$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -908,6 +861,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (!options_.opensource_runtime) { format( "inline void $classname$::set_$name$(::StringPiece value) {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -915,24 +869,13 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " }\n" " $field_member$.SetNoArena($default_variable$, value);\n" " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" - "}\n" - "#ifdef HAS_GLOBAL_STRING\n" - "inline void $classname$::set_$name$(const ::std::string& value) {\n" - " if (!has_$name$()) {\n" - " clear_$oneof_name$();\n" - " set_has_$name$();\n" - " $field_member$.UnsafeSetDefault($default_variable$);\n" - " }\n" - " $field_member$.SetNoArena($default_variable$,\n" - " ::StringPiece(value.data(), value.size()));\n" - " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" - "}\n" - "#endif\n"); + "}\n"); } format( "inline " "void $classname$::set_$name$(const $pointer_type$* value, size_t " "size) {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -942,7 +885,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " reinterpret_cast(value), size));\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline $string$* $classname$::mutable_$name$() {\n" + "inline std::string* $classname$::mutable_$name$() {\n" + "$annotate_accessor$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -951,20 +895,22 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $field_member$.MutableNoArena($default_variable$);\n" "}\n" - "inline $string$* $classname$::$release_name$() {\n" + "inline std::string* $classname$::$release_name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_release:$full_name$)\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" " return $field_member$.ReleaseNoArena($default_variable$);\n" " } else {\n" - " return NULL;\n" + " return nullptr;\n" " }\n" "}\n" - "inline void $classname$::set_allocated_$name$($string$* $name$) {\n" + "inline void $classname$::set_allocated_$name$(std::string* $name$) {\n" + "$annotate_accessor$" " if (has_$oneof_name$()) {\n" " clear_$oneof_name$();\n" " }\n" - " if ($name$ != NULL) {\n" + " if ($name$ != nullptr) {\n" " set_has_$name$();\n" " $field_member$.UnsafeSetDefault($name$);\n" " }\n" @@ -973,8 +919,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { } } -void StringOneofFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { format( @@ -985,26 +931,26 @@ GenerateClearingCode(io::Printer* printer) const { } } -void StringOneofFieldGenerator:: -GenerateMessageClearingCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateMessageClearingCode( + io::Printer* printer) const { return GenerateClearingCode(printer); } -void StringOneofFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { // Don't print any swapping code. Swapping the union will swap this field. } -void StringOneofFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$ns$::_$classname$_default_instance_.$name$_.UnsafeSetDefault(\n" " $default_variable$);\n"); } -void StringOneofFieldGenerator:: -GenerateDestructorCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateDestructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format( "if (has_$name$()) {\n" @@ -1012,22 +958,22 @@ GenerateDestructorCode(io::Printer* printer) const { "}\n"); } -void StringOneofFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); // See above: ArenaString is not included in the open-source release. if (!options_.opensource_runtime && SupportsArenas(descriptor_) && !lite_) { // If has_$name$(), then the current string is either an ArenaString (no - // destructor necessary) or a materialized ::std::string (and is on the Arena's - // destructor list). No call to ArenaStringPtr::Destroy is needed. + // destructor necessary) or a materialized std::string (and is on the + // Arena's destructor list). No call to ArenaStringPtr::Destroy is needed. format( - "if (arena != NULL) {\n" + "if (arena != nullptr) {\n" " clear_$oneof_name$();\n" " if (!has_$name$()) {\n" " $field_member$.UnsafeSetDefault($default_variable$);\n" " set_has_$name$();\n" " }\n" - " ::$proto_ns$::internal::TaggedPtr<$string$> new_value =\n" + " ::$proto_ns$::internal::TaggedPtr new_value =\n" " ::$proto_ns$::internal::ReadArenaString(input, arena);\n" " DO_(!new_value.IsNull());\n" " $field_member$.UnsafeSetTaggedPointer(new_value);\n" @@ -1049,7 +995,6 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } - // =================================================================== RepeatedStringFieldGenerator::RepeatedStringFieldGenerator( @@ -1060,14 +1005,14 @@ RepeatedStringFieldGenerator::RepeatedStringFieldGenerator( RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {} -void RepeatedStringFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GeneratePrivateMembers( + io::Printer* printer) const { Formatter format(printer, variables_); - format("::$proto_ns$::RepeatedPtrField<$string$> $name$_;\n"); + format("::$proto_ns$::RepeatedPtrField $name$_;\n"); } -void RepeatedStringFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); // See comment above about unknown ctypes. bool unknown_ctype = descriptor_->options().ctype() != @@ -1082,52 +1027,42 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } format( - "$deprecated_attr$const $string$& ${1$$name$$}$(int index) const;\n" - "$deprecated_attr$$string$* ${1$mutable_$name$$}$(int index);\n" + "$deprecated_attr$const std::string& ${1$$name$$}$(int index) const;\n" + "$deprecated_attr$std::string* ${1$mutable_$name$$}$(int index);\n" "$deprecated_attr$void ${1$set_$name$$}$(int index, const " - "$string$& value);\n" - "#if LANG_CXX11\n" - "$deprecated_attr$void ${1$set_$name$$}$(int index, $string$&& value);\n" - "#endif\n" + "std::string& value);\n" + "$deprecated_attr$void ${1$set_$name$$}$(int index, std::string&& " + "value);\n" "$deprecated_attr$void ${1$set_$name$$}$(int index, const " "char* value);\n", descriptor_); if (!options_.opensource_runtime) { format( "$deprecated_attr$void ${1$set_$name$$}$(int index, " - "StringPiece value);\n" - "#ifdef HAS_GLOBAL_STRING\n" - "$deprecated_attr$void ${1$set_$name$$}$(int index, const " - "::std::string& value);\n" - "#endif\n", + "StringPiece value);\n", descriptor_); } format( "$deprecated_attr$void ${1$set_$name$$}$(" "int index, const $pointer_type$* value, size_t size);\n" - "$deprecated_attr$$string$* ${1$add_$name$$}$();\n" - "$deprecated_attr$void ${1$add_$name$$}$(const $string$& value);\n" - "#if LANG_CXX11\n" - "$deprecated_attr$void ${1$add_$name$$}$($string$&& value);\n" - "#endif\n" + "$deprecated_attr$std::string* ${1$add_$name$$}$();\n" + "$deprecated_attr$void ${1$add_$name$$}$(const std::string& value);\n" + "$deprecated_attr$void ${1$add_$name$$}$(std::string&& value);\n" "$deprecated_attr$void ${1$add_$name$$}$(const char* value);\n", descriptor_); if (!options_.opensource_runtime) { format( - "$deprecated_attr$void ${1$add_$name$$}$(StringPiece value);\n" - "#ifdef HAS_GLOBAL_STRING\n" - "$deprecated_attr$void ${1$add_$name$$}$(const ::std::string& value);\n" - "#endif\n", + "$deprecated_attr$void ${1$add_$name$$}$(StringPiece value);\n", descriptor_); } format( "$deprecated_attr$void ${1$add_$name$$}$(const $pointer_type$* " "value, size_t size)" ";\n" - "$deprecated_attr$const ::$proto_ns$::RepeatedPtrField<$string$>& " + "$deprecated_attr$const ::$proto_ns$::RepeatedPtrField& " "${1$$name$$}$() " "const;\n" - "$deprecated_attr$::$proto_ns$::RepeatedPtrField<$string$>* " + "$deprecated_attr$::$proto_ns$::RepeatedPtrField* " "${1$mutable_$name$$}$()" ";\n", descriptor_); @@ -1139,40 +1074,45 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } } -void RepeatedStringFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); if (options_.safe_boundary_check) { format( - "inline const $string$& $classname$::$name$(int index) const {\n" + "inline const std::string& $classname$::$name$(int index) const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.InternalCheckedGet(\n" " index, ::$proto_ns$::internal::GetEmptyStringAlreadyInited());\n" "}\n"); } else { format( - "inline const $string$& $classname$::$name$(int index) const {\n" + "inline const std::string& $classname$::$name$(int index) const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.Get(index);\n" "}\n"); } format( - "inline $string$* $classname$::mutable_$name$(int index) {\n" + "inline std::string* $classname$::mutable_$name$(int index) {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.Mutable(index);\n" "}\n" - "inline void $classname$::set_$name$(int index, const $string$& value) " + "inline void $classname$::set_$name$(int index, const std::string& " + "value) " "{\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_set:$full_name$)\n" " $name$_.Mutable(index)->assign(value);\n" "}\n" - "#if LANG_CXX11\n" - "inline void $classname$::set_$name$(int index, $string$&& value) {\n" + "inline void $classname$::set_$name$(int index, std::string&& value) {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_set:$full_name$)\n" " $name$_.Mutable(index)->assign(std::move(value));\n" "}\n" - "#endif\n" "inline void $classname$::set_$name$(int index, const char* value) {\n" + "$annotate_accessor$" " $null_check$" " $name$_.Mutable(index)->assign(value);\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" @@ -1181,40 +1121,37 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { format( "inline void " "$classname$::set_$name$(int index, StringPiece value) {\n" + "$annotate_accessor$" " $name$_.Mutable(index)->assign(value.data(), value.size());\n" " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" - "}\n" - "#ifdef HAS_GLOBAL_STRING\n" - "inline void " - "$classname$::set_$name$(int index, const std::string& value) {\n" - " $name$_.Mutable(index)->assign(value.data(), value.size());\n" - " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" - "}\n" - "#endif\n"); + "}\n"); } format( "inline void " "$classname$::set_$name$" "(int index, const $pointer_type$* value, size_t size) {\n" + "$annotate_accessor$" " $name$_.Mutable(index)->assign(\n" " reinterpret_cast(value), size);\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline $string$* $classname$::add_$name$() {\n" + "inline std::string* $classname$::add_$name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_add_mutable:$full_name$)\n" " return $name$_.Add();\n" "}\n" - "inline void $classname$::add_$name$(const $string$& value) {\n" + "inline void $classname$::add_$name$(const std::string& value) {\n" + "$annotate_accessor$" " $name$_.Add()->assign(value);\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" "}\n" - "#if LANG_CXX11\n" - "inline void $classname$::add_$name$($string$&& value) {\n" + "inline void $classname$::add_$name$(std::string&& value) {\n" + "$annotate_accessor$" " $name$_.Add(std::move(value));\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" "}\n" - "#endif\n" "inline void $classname$::add_$name$(const char* value) {\n" + "$annotate_accessor$" " $null_check$" " $name$_.Add()->assign(value);\n" " // @@protoc_insertion_point(field_add_char:$full_name$)\n" @@ -1222,65 +1159,63 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (!options_.opensource_runtime) { format( "inline void $classname$::add_$name$(StringPiece value) {\n" + "$annotate_accessor$" " $name$_.Add()->assign(value.data(), value.size());\n" " // @@protoc_insertion_point(field_add_string_piece:$full_name$)\n" - "}\n" - "#ifdef HAS_GLOBAL_STRING\n" - "inline void $classname$::add_$name$(const ::std::string& value) {\n" - " $name$_.Add()->assign(value.data(), value.size());\n" - " // @@protoc_insertion_point(field_add_std_string:$full_name$)\n" - "}\n" - "#endif\n"); + "}\n"); } format( "inline void " "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n" + "$annotate_accessor$" " $name$_.Add()->assign(reinterpret_cast(value), size);\n" " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n" "}\n" - "inline const ::$proto_ns$::RepeatedPtrField<$string$>&\n" + "inline const ::$proto_ns$::RepeatedPtrField&\n" "$classname$::$name$() const {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_list:$full_name$)\n" " return $name$_;\n" "}\n" - "inline ::$proto_ns$::RepeatedPtrField<$string$>*\n" + "inline ::$proto_ns$::RepeatedPtrField*\n" "$classname$::mutable_$name$() {\n" + "$annotate_accessor$" " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" " return &$name$_;\n" "}\n"); } -void RepeatedStringFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.Clear();\n"); } -void RepeatedStringFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.MergeFrom(from.$name$_);\n"); } -void RepeatedStringFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.InternalSwap(CastToBase(&other->$name$_));\n"); } -void RepeatedStringFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { // Not needed for repeated fields. } -void RepeatedStringFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.CopyFrom(from.$name$_);"); } -void RepeatedStringFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); format( "DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n" @@ -1294,44 +1229,28 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } -void RepeatedStringFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); - format("for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); - format.Indent(); - if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { - GenerateUtf8CheckCodeForString( - descriptor_, options_, false, - "this->$name$(i).data(), static_cast(this->$name$(i).length()),\n", - format); - } - format.Outdent(); format( - " ::$proto_ns$::internal::WireFormatLite::Write$declared_type$(\n" - " $number$, this->$name$(i), output);\n" - "}\n"); -} - -void RepeatedStringFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { - Formatter format(printer, variables_); - format("for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); + "for (auto it = this->$name$().pointer_begin(),\n" + " end = this->$name$().pointer_end(); it < end; ++it) {\n" + " const auto& s = **it;\n"); + // format("for (const std::string& s : this->$name$()) {\n"); format.Indent(); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { - GenerateUtf8CheckCodeForString( - descriptor_, options_, false, - "this->$name$(i).data(), static_cast(this->$name$(i).length()),\n", - format); + GenerateUtf8CheckCodeForString(descriptor_, options_, false, + "s.data(), static_cast(s.length()),\n", + format); } format.Outdent(); format( - " target = ::$proto_ns$::internal::WireFormatLite::\n" - " Write$declared_type$ToArray($number$, this->$name$(i), target);\n" + " target = stream->Write$declared_type$($number$, s, target);\n" "}\n"); } -void RepeatedStringFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateByteSize( + io::Printer* printer) const { Formatter format(printer, variables_); format( "total_size += $tag_size$ *\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.h b/src/google/protobuf/compiler/cpp/cpp_string_field.h index 3a1de55167..b01c9a04b2 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.h @@ -66,7 +66,6 @@ class StringFieldGenerator : public FieldGenerator { bool GenerateArenaDestructorCode(io::Printer* printer) const; void GenerateDefaultInstanceAllocator(io::Printer* printer) const; void GenerateMergeFromCodedStream(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; uint32 CalculateFieldTag() const; @@ -120,7 +119,6 @@ class RepeatedStringFieldGenerator : public FieldGenerator { void GenerateConstructorCode(io::Printer* printer) const; void GenerateCopyConstructorCode(io::Printer* printer) const; void GenerateMergeFromCodedStream(io::Printer* printer) const; - void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; void GenerateByteSize(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto b/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto index 7fe9875978..479710821f 100644 --- a/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto +++ b/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto @@ -35,11 +35,13 @@ // This file tests that various identifiers work as field and type names even // though the same identifiers are used internally by the C++ code generator. +// LINT: LEGACY_NAMES + syntax = "proto2"; // Some generic_services option(s) added automatically. // See: http://go/proto2-generic-services-default -option cc_generic_services = true; // auto-added +option cc_generic_services = true; // auto-added // We don't put this in a package within proto2 because we need to make sure // that the generated code doesn't depend on being in the proto2 namespace. @@ -55,17 +57,29 @@ message TestConflictingSymbolNames { optional int32 output = 2; optional string length = 3; repeated int32 i = 4; - repeated string new_element = 5 [ctype=STRING_PIECE]; + repeated string new_element = 5 [ctype = STRING_PIECE]; optional int32 total_size = 6; optional int32 tag = 7; enum TestEnum { FOO = 0; } - message Data1 { repeated int32 data = 1; } - message Data2 { repeated TestEnum data = 1; } - message Data3 { repeated string data = 1; } - message Data4 { repeated Data4 data = 1; } - message Data5 { repeated string data = 1 [ctype=STRING_PIECE]; } - message Data6 { repeated string data = 1 [ctype=CORD]; } + message Data1 { + repeated int32 data = 1; + } + message Data2 { + repeated TestEnum data = 1; + } + message Data3 { + repeated string data = 1; + } + message Data4 { + repeated Data4 data = 1; + } + message Data5 { + repeated string data = 1 [ctype = STRING_PIECE]; + } + message Data6 { + repeated string data = 1 [ctype = CORD]; + } optional int32 source = 8; optional int32 value = 9; @@ -91,10 +105,10 @@ message TestConflictingSymbolNames { optional uint32 reflection = 27; message Cord {} - optional string some_cord = 28 [ctype=CORD]; + optional string some_cord = 28 [ctype = CORD]; message StringPiece {} - optional string some_string_piece = 29 [ctype=STRING_PIECE]; + optional string some_string_piece = 29 [ctype = STRING_PIECE]; // Some keywords. optional uint32 int = 30; @@ -125,14 +139,15 @@ message TestConflictingSymbolNames { extensions 1000 to max; // NO_PROTO3 } -message TestConflictingSymbolNamesExtension { // NO_PROTO3 - extend TestConflictingSymbolNames { // NO_PROTO3 - repeated int32 repeated_int32_ext = 20423638 [packed=true]; // NO_PROTO3 - } // NO_PROTO3 -} // NO_PROTO3 +message TestConflictingSymbolNamesExtension { // NO_PROTO3 + extend TestConflictingSymbolNames { // NO_PROTO3 + repeated int32 repeated_int32_ext = 20423638 [packed = true]; // NO_PROTO3 + } // NO_PROTO3 +} // NO_PROTO3 message TestConflictingEnumNames { // NO_PROTO3 - enum NestedConflictingEnum { // NO_PROTO3 + enum while { // NO_PROTO3 + default = 0; // NO_PROTO3 and = 1; // NO_PROTO3 class = 2; // NO_PROTO3 int = 3; // NO_PROTO3 @@ -140,10 +155,11 @@ message TestConflictingEnumNames { // NO_PROTO3 XOR = 5; // NO_PROTO3 } // NO_PROTO3 - optional NestedConflictingEnum conflicting_enum = 1; // NO_PROTO3 + optional while conflicting_enum = 1; // NO_PROTO3 } // NO_PROTO3 -enum ConflictingEnum { // NO_PROTO3 +enum bool { // NO_PROTO3 + default = 0; // NO_PROTO3 NOT_EQ = 1; // NO_PROTO3 volatile = 2; // NO_PROTO3 return = 3; // NO_PROTO3 @@ -151,6 +167,14 @@ enum ConflictingEnum { // NO_PROTO3 message DummyMessage {} +message NULL { + optional int32 int = 1; +} + +extend TestConflictingSymbolNames { // NO_PROTO3 + optional int32 void = 314253; // NO_PROTO3 +} // NO_PROTO3 + // Message names that could conflict. message Shutdown {} message TableStruct {} diff --git a/src/google/protobuf/compiler/cpp/cpp_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_unittest.cc index 2cf030eb2c..74310a7fd7 100644 --- a/src/google/protobuf/compiler/cpp/cpp_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_unittest.cc @@ -47,8 +47,8 @@ #include #include -#include #include +#include #include @@ -96,25 +96,37 @@ TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingSymbolNames) { // parameter names. typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessage; message.AddExtension(ExtensionMessage::repeated_int32_ext, 123); - EXPECT_EQ(123, - message.GetExtension(ExtensionMessage::repeated_int32_ext, 0)); + EXPECT_EQ(123, message.GetExtension(ExtensionMessage::repeated_int32_ext, 0)); } TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingEnumNames) { protobuf_unittest::TestConflictingEnumNames message; - message.set_conflicting_enum(protobuf_unittest::TestConflictingEnumNames_NestedConflictingEnum_and_); + message.set_conflicting_enum( + protobuf_unittest::TestConflictingEnumNames_while_and_); EXPECT_EQ(1, message.conflicting_enum()); - message.set_conflicting_enum(protobuf_unittest::TestConflictingEnumNames_NestedConflictingEnum_XOR); + message.set_conflicting_enum( + protobuf_unittest::TestConflictingEnumNames_while_XOR); EXPECT_EQ(5, message.conflicting_enum()); - - protobuf_unittest::ConflictingEnum conflicting_enum; + protobuf_unittest::bool_ conflicting_enum; conflicting_enum = protobuf_unittest::NOT_EQ; EXPECT_EQ(1, conflicting_enum); conflicting_enum = protobuf_unittest::return_; EXPECT_EQ(3, conflicting_enum); } +TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingMessageNames) { + protobuf_unittest::NULL_ message; + message.set_int_(123); + EXPECT_EQ(message.int_(), 123); +} + +TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingExtension) { + protobuf_unittest::TestConflictingSymbolNames message; + message.SetExtension(protobuf_unittest::void_, 123); + EXPECT_EQ(123, message.GetExtension(protobuf_unittest::void_)); +} + } // namespace cpp_unittest } // namespace cpp } // namespace compiler diff --git a/src/google/protobuf/compiler/cpp/cpp_unittest.inc b/src/google/protobuf/compiler/cpp/cpp_unittest.inc index 0604731f09..0b18662ed2 100644 --- a/src/google/protobuf/compiler/cpp/cpp_unittest.inc +++ b/src/google/protobuf/compiler/cpp/cpp_unittest.inc @@ -50,7 +50,7 @@ #include #include - +#include #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) // We exclude this large proto from cmake build because it's too large for // visual studio to compile (report internal errors). @@ -78,6 +78,8 @@ #include #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -92,11 +94,11 @@ class MockErrorCollector : public MultiFileErrorCollector { MockErrorCollector() {} ~MockErrorCollector() {} - string text_; + std::string text_; // implements ErrorCollector --------------------------------------- - void AddError(const string& filename, int line, int column, - const string& message) { + void AddError(const std::string& filename, int line, int column, + const std::string& message) { strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line, column, message); } @@ -140,7 +142,7 @@ TEST(GENERATED_DESCRIPTOR_TEST_NAME, IdenticalDescriptors) { // limit for string literal size TEST(GENERATED_DESCRIPTOR_TEST_NAME, EnormousDescriptor) { const Descriptor* generated_descriptor = - TestEnormousDescriptor::descriptor(); + ::protobuf_unittest::TestEnormousDescriptor::descriptor(); EXPECT_TRUE(generated_descriptor != NULL); } @@ -203,7 +205,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, ExtremeSmallIntegerDefault) { const UNITTEST::TestExtremeDefaultValues& extreme_default = UNITTEST::TestExtremeDefaultValues::default_instance(); EXPECT_EQ(~0x7fffffff, kint32min); - EXPECT_EQ(GOOGLE_LONGLONG(~0x7fffffffffffffff), kint64min); + EXPECT_EQ(PROTOBUF_LONGLONG(~0x7fffffffffffffff), kint64min); EXPECT_EQ(kint32min, extreme_default.really_small_int32()); EXPECT_EQ(kint64min, extreme_default.really_small_int64()); } @@ -239,8 +241,8 @@ TEST(GENERATED_MESSAGE_TEST_NAME, MutableStringDefault) { TEST(GENERATED_MESSAGE_TEST_NAME, StringDefaults) { UNITTEST::TestExtremeDefaultValues message; // Check if '\000' can be used in default string value. - EXPECT_EQ(string("hel\000lo", 6), message.string_with_zero()); - EXPECT_EQ(string("wor\000ld", 6), message.bytes_with_zero()); + EXPECT_EQ(std::string("hel\000lo", 6), message.string_with_zero()); + EXPECT_EQ(std::string("wor\000ld", 6), message.bytes_with_zero()); } TEST(GENERATED_MESSAGE_TEST_NAME, ReleaseString) { @@ -254,7 +256,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, ReleaseString) { message.set_default_string("blah"); EXPECT_TRUE(message.has_default_string()); - std::unique_ptr str(message.release_default_string()); + std::unique_ptr str(message.release_default_string()); EXPECT_FALSE(message.has_default_string()); ASSERT_TRUE(str != NULL); EXPECT_EQ("blah", *str); @@ -288,7 +290,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, SetAllocatedString) { UNITTEST::TestAllTypes message; EXPECT_FALSE(message.has_optional_string()); - const string kHello("hello"); + const std::string kHello("hello"); message.set_optional_string(kHello); EXPECT_TRUE(message.has_optional_string()); @@ -296,7 +298,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, SetAllocatedString) { EXPECT_FALSE(message.has_optional_string()); EXPECT_EQ("", message.optional_string()); - message.set_allocated_optional_string(new string(kHello)); + message.set_allocated_optional_string(new std::string(kHello)); EXPECT_TRUE(message.has_optional_string()); EXPECT_EQ(kHello, message.optional_string()); } @@ -400,70 +402,68 @@ TEST(GENERATED_MESSAGE_TEST_NAME, StringCharStarLength) { EXPECT_EQ("wx", message.repeated_string(0)); } -#if LANG_CXX11 TEST(GENERATED_MESSAGE_TEST_NAME, StringMove) { // Verify that we trigger the move behavior on a scalar setter. protobuf_unittest_no_arena::TestAllTypes message; { - string tmp(32, 'a'); + std::string tmp(32, 'a'); const char* old_data = tmp.data(); message.set_optional_string(std::move(tmp)); const char* new_data = message.optional_string().data(); EXPECT_EQ(old_data, new_data); - EXPECT_EQ(string(32, 'a'), message.optional_string()); + EXPECT_EQ(std::string(32, 'a'), message.optional_string()); - string tmp2(32, 'b'); + std::string tmp2(32, 'b'); old_data = tmp2.data(); message.set_optional_string(std::move(tmp2)); new_data = message.optional_string().data(); EXPECT_EQ(old_data, new_data); - EXPECT_EQ(string(32, 'b'), message.optional_string()); + EXPECT_EQ(std::string(32, 'b'), message.optional_string()); } // Verify that we trigger the move behavior on a oneof setter. { - string tmp(32, 'a'); + std::string tmp(32, 'a'); const char* old_data = tmp.data(); message.set_oneof_string(std::move(tmp)); const char* new_data = message.oneof_string().data(); EXPECT_EQ(old_data, new_data); - EXPECT_EQ(string(32, 'a'), message.oneof_string()); + EXPECT_EQ(std::string(32, 'a'), message.oneof_string()); - string tmp2(32, 'b'); + std::string tmp2(32, 'b'); old_data = tmp2.data(); message.set_oneof_string(std::move(tmp2)); new_data = message.oneof_string().data(); EXPECT_EQ(old_data, new_data); - EXPECT_EQ(string(32, 'b'), message.oneof_string()); + EXPECT_EQ(std::string(32, 'b'), message.oneof_string()); } // Verify that we trigger the move behavior on a repeated setter. { - string tmp(32, 'a'); + std::string tmp(32, 'a'); const char* old_data = tmp.data(); message.add_repeated_string(std::move(tmp)); const char* new_data = message.repeated_string(0).data(); EXPECT_EQ(old_data, new_data); - EXPECT_EQ(string(32, 'a'), message.repeated_string(0)); + EXPECT_EQ(std::string(32, 'a'), message.repeated_string(0)); - string tmp2(32, 'b'); + std::string tmp2(32, 'b'); old_data = tmp2.data(); message.set_repeated_string(0, std::move(tmp2)); new_data = message.repeated_string(0).data(); EXPECT_EQ(old_data, new_data); - EXPECT_EQ(string(32, 'b'), message.repeated_string(0)); + EXPECT_EQ(std::string(32, 'b'), message.repeated_string(0)); } } -#endif TEST(GENERATED_MESSAGE_TEST_NAME, CopyFrom) { @@ -656,7 +656,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, CopyAssignmentOperator) { TestUtil::ExpectAllFieldsSet(message2); } -#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || GOOGLE_PROTOBUF_RTTI +#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || PROTOBUF_RTTI TEST(GENERATED_MESSAGE_TEST_NAME, UpcastCopyFrom) { // Test the CopyFrom method that takes in the generic const Message& // parameter. @@ -664,7 +664,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, UpcastCopyFrom) { TestUtil::SetAllFields(&message1); - const Message* source = ::google::protobuf::implicit_cast(&message1); + const Message* source = implicit_cast(&message1); message2.CopyFrom(*source); TestUtil::ExpectAllFieldsSet(message2); @@ -725,7 +725,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, NonEmptyMergeFrom) { // Test the generated SerializeWithCachedSizesToArray(), TEST(GENERATED_MESSAGE_TEST_NAME, SerializationToArray) { UNITTEST::TestAllTypes message1, message2; - string data; + std::string data; TestUtil::SetAllFields(&message1); int size = message1.ByteSizeLong(); data.resize(size); @@ -739,7 +739,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, SerializationToArray) { TEST(GENERATED_MESSAGE_TEST_NAME, PackedFieldsSerializationToArray) { UNITTEST::TestPackedTypes packed_message1, packed_message2; - string packed_data; + std::string packed_data; TestUtil::SetPackedFields(&packed_message1); int packed_size = packed_message1.ByteSizeLong(); packed_data.resize(packed_size); @@ -756,7 +756,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, SerializationToStream) { UNITTEST::TestAllTypes message1, message2; TestUtil::SetAllFields(&message1); int size = message1.ByteSizeLong(); - string data; + std::string data; data.resize(size); { // Allow the output stream to buffer only one byte at a time. @@ -775,7 +775,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, PackedFieldsSerializationToStream) { UNITTEST::TestPackedTypes message1, message2; TestUtil::SetPackedFields(&message1); int size = message1.ByteSizeLong(); - string data; + std::string data; data.resize(size); { // Allow the output stream to buffer only one byte at a time. @@ -842,7 +842,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, ForeignNested) { TEST(GENERATED_MESSAGE_TEST_NAME, ReallyLargeTagNumber) { // Test that really large tag numbers don't break anything. UNITTEST::TestReallyLargeTagNumber message1, message2; - string data; + std::string data; // For the most part, if this compiles and runs then we're probably good. // (The most likely cause for failure would be if something were attempting @@ -925,7 +925,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, TestEmbedOptimizedForSize) { UNITTEST::TestEmbedOptimizedForSize message, message2; message.mutable_optional_message()->set_i(1); message.add_repeated_message()->mutable_msg()->set_c(2); - string data; + std::string data; message.SerializeToString(&data); ASSERT_TRUE(message2.ParseFromString(data)); EXPECT_EQ(1, message2.optional_message().i()); @@ -954,7 +954,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, TestSpaceUsed) { // Setting a string to a value larger than the string object itself should // increase SpaceUsedLong(), because it cannot store the value internally. - message1.set_optional_string(string(sizeof(string) + 1, 'x')); + message1.set_optional_string(std::string(sizeof(std::string) + 1, 'x')); int min_expected_increase = message1.optional_string().capacity(); EXPECT_LE(empty_message_size + min_expected_increase, message1.SpaceUsedLong()); @@ -984,14 +984,14 @@ TEST(GENERATED_MESSAGE_TEST_NAME, TestOneofSpaceUsed) { // Setting a string in oneof to a small value should only increase // SpaceUsedLong() by the size of a string object. message1.set_foo_string("abc"); - EXPECT_LE(empty_message_size + sizeof(string), message1.SpaceUsedLong()); + EXPECT_LE(empty_message_size + sizeof(std::string), message1.SpaceUsedLong()); // Setting a string in oneof to a value larger than the string object itself // should increase SpaceUsedLong(), because it cannot store the value // internally. - message1.set_foo_string(string(sizeof(string) + 1, 'x')); - int min_expected_increase = message1.foo_string().capacity() + - sizeof(string); + message1.set_foo_string(std::string(sizeof(std::string) + 1, 'x')); + int min_expected_increase = + message1.foo_string().capacity() + sizeof(std::string); EXPECT_LE(empty_message_size + min_expected_increase, message1.SpaceUsedLong()); @@ -1036,10 +1036,10 @@ TEST(GENERATED_MESSAGE_TEST_NAME, ExtensionConstantValues) { } TEST(GENERATED_MESSAGE_TEST_NAME, ParseFromTruncated) { - const string long_string = string(128, 'q'); + const std::string long_string = std::string(128, 'q'); FileDescriptorProto p; p.add_extension()->set_name(long_string); - const string msg = p.SerializeAsString(); + const std::string msg = p.SerializeAsString(); int successful_count = 0; for (int i = 0; i <= msg.size(); i++) { if (p.ParseFromArray(msg.c_str(), i)) { @@ -1141,7 +1141,7 @@ TEST(GENERATED_ENUM_TEST_NAME, MinAndMax) { #ifndef PROTOBUF_TEST_NO_DESCRIPTORS TEST(GENERATED_ENUM_TEST_NAME, Name) { - // "Names" in the presence of dup values are a bit arbitrary. + // "Names" in the presence of dup values map to the first alias. EXPECT_EQ("FOO1", UNITTEST::TestEnumWithDupValue_Name(UNITTEST::FOO1)); EXPECT_EQ("FOO1", UNITTEST::TestEnumWithDupValue_Name(UNITTEST::FOO2)); @@ -1242,7 +1242,7 @@ class GENERATED_SERVICE_TEST_NAME : public testing::Test { // --------------------------------------------------------------- bool called_; - string method_; + std::string method_; RpcController* controller_; const Message* request_; Message* response_; @@ -1302,14 +1302,14 @@ class GENERATED_SERVICE_TEST_NAME : public testing::Test { ADD_FAILURE() << "Failed() not expected during this test."; return false; } - string ErrorText() const { + std::string ErrorText() const { ADD_FAILURE() << "ErrorText() not expected during this test."; return ""; } void StartCancel() { ADD_FAILURE() << "StartCancel() not expected during this test."; } - void SetFailed(const string& reason) { + void SetFailed(const std::string& reason) { ADD_FAILURE() << "SetFailed() not expected during this test."; } bool IsCanceled() const { @@ -1469,7 +1469,7 @@ TEST_F(GENERATED_SERVICE_TEST_NAME, NotImplemented) { public: ExpectUnimplementedController() : called_(false) {} - void SetFailed(const string& reason) { + void SetFailed(const std::string& reason) { EXPECT_FALSE(called_); called_ = true; EXPECT_EQ("Method Foo() not implemented.", reason); @@ -1619,7 +1619,7 @@ TEST_F(OneofTest, SetString) { message.clear_foo_string(); EXPECT_FALSE(message.has_foo_string()); - message.set_foo_string(string("bar")); + message.set_foo_string(std::string("bar")); EXPECT_TRUE(message.has_foo_string()); EXPECT_EQ(message.foo_string(), "bar"); message.clear_foo_string(); @@ -1655,7 +1655,7 @@ TEST_F(OneofTest, ReleaseString) { message.set_foo_string("blah"); EXPECT_TRUE(message.has_foo_string()); - std::unique_ptr str(message.release_foo_string()); + std::unique_ptr str(message.release_foo_string()); EXPECT_FALSE(message.has_foo_string()); ASSERT_TRUE(str != NULL); EXPECT_EQ("blah", *str); @@ -1669,7 +1669,7 @@ TEST_F(OneofTest, SetAllocatedString) { UNITTEST::TestOneof2 message; EXPECT_FALSE(message.has_foo_string()); - const string kHello("hello"); + const std::string kHello("hello"); message.set_foo_string(kHello); EXPECT_TRUE(message.has_foo_string()); @@ -1677,7 +1677,7 @@ TEST_F(OneofTest, SetAllocatedString) { EXPECT_FALSE(message.has_foo_string()); EXPECT_EQ("", message.foo_string()); - message.set_allocated_foo_string(new string(kHello)); + message.set_allocated_foo_string(new std::string(kHello)); EXPECT_TRUE(message.has_foo_string()); EXPECT_EQ(kHello, message.foo_string()); } @@ -1871,7 +1871,7 @@ TEST_F(OneofTest, UpcastCopyFrom) { message1.mutable_foogroup()->set_a(123); EXPECT_TRUE(message1.has_foogroup()); - const Message* source = ::google::protobuf::implicit_cast(&message1); + const Message* source = implicit_cast(&message1); message2.CopyFrom(*source); EXPECT_TRUE(message2.has_foogroup()); @@ -1886,21 +1886,21 @@ TEST_F(OneofTest, SerializationToArray) { // Primitive type { UNITTEST::TestOneof2 message1, message2; - string data; - message1.set_foo_int(123); - int size = message1.ByteSizeLong(); - data.resize(size); - uint8* start = reinterpret_cast(::google::protobuf::string_as_array(&data)); - uint8* end = message1.SerializeWithCachedSizesToArray(start); - EXPECT_EQ(size, end - start); - EXPECT_TRUE(message2.ParseFromString(data)); - EXPECT_EQ(message2.foo_int(), 123); +std::string data; +message1.set_foo_int(123); +int size = message1.ByteSizeLong(); +data.resize(size); +uint8* start = reinterpret_cast(::google::protobuf::string_as_array(&data)); +uint8* end = message1.SerializeWithCachedSizesToArray(start); +EXPECT_EQ(size, end - start); +EXPECT_TRUE(message2.ParseFromString(data)); +EXPECT_EQ(message2.foo_int(), 123); } // String { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.set_foo_string("foo"); int size = message1.ByteSizeLong(); data.resize(size); @@ -1915,7 +1915,7 @@ TEST_F(OneofTest, SerializationToArray) { // Bytes { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.set_foo_bytes("qux"); int size = message1.ByteSizeLong(); data.resize(size); @@ -1929,7 +1929,7 @@ TEST_F(OneofTest, SerializationToArray) { // Enum { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.set_foo_enum(UNITTEST::TestOneof2::FOO); int size = message1.ByteSizeLong(); data.resize(size); @@ -1943,7 +1943,7 @@ TEST_F(OneofTest, SerializationToArray) { // Message { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.mutable_foo_message()->set_qux_int(234); int size = message1.ByteSizeLong(); data.resize(size); @@ -1957,7 +1957,7 @@ TEST_F(OneofTest, SerializationToArray) { // Group { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.mutable_foogroup()->set_a(345); int size = message1.ByteSizeLong(); data.resize(size); @@ -1979,19 +1979,19 @@ TEST_F(OneofTest, SerializationToStream) { // Primitive type { UNITTEST::TestOneof2 message1, message2; - string data; - message1.set_foo_int(123); - int size = message1.ByteSizeLong(); - data.resize(size); +std::string data; +message1.set_foo_int(123); +int size = message1.ByteSizeLong(); +data.resize(size); - { - // Allow the output stream to buffer only one byte at a time. - io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); - io::CodedOutputStream output_stream(&array_stream); - message1.SerializeWithCachedSizes(&output_stream); - EXPECT_FALSE(output_stream.HadError()); - EXPECT_EQ(size, output_stream.ByteCount()); - } +{ + // Allow the output stream to buffer only one byte at a time. + io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); + io::CodedOutputStream output_stream(&array_stream); + message1.SerializeWithCachedSizes(&output_stream); + EXPECT_FALSE(output_stream.HadError()); + EXPECT_EQ(size, output_stream.ByteCount()); +} EXPECT_TRUE(message2.ParseFromString(data)); EXPECT_EQ(message2.foo_int(), 123); @@ -2000,7 +2000,7 @@ TEST_F(OneofTest, SerializationToStream) { // String { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.set_foo_string("foo"); int size = message1.ByteSizeLong(); data.resize(size); @@ -2022,7 +2022,7 @@ TEST_F(OneofTest, SerializationToStream) { // Bytes { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.set_foo_bytes("qux"); int size = message1.ByteSizeLong(); data.resize(size); @@ -2043,7 +2043,7 @@ TEST_F(OneofTest, SerializationToStream) { // Enum { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.set_foo_enum(UNITTEST::TestOneof2::FOO); int size = message1.ByteSizeLong(); data.resize(size); @@ -2064,7 +2064,7 @@ TEST_F(OneofTest, SerializationToStream) { // Message { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.mutable_foo_message()->set_qux_int(234); int size = message1.ByteSizeLong(); data.resize(size); @@ -2085,7 +2085,7 @@ TEST_F(OneofTest, SerializationToStream) { // Group { UNITTEST::TestOneof2 message1, message2; - string data; + std::string data; message1.mutable_foogroup()->set_a(345); int size = message1.ByteSizeLong(); data.resize(size); @@ -2151,11 +2151,12 @@ TEST(HELPERS_TEST_NAME, TestSCC) { UNITTEST::TestMutualRecursionA a; MessageSCCAnalyzer scc_analyzer((Options())); const SCC* scc = scc_analyzer.GetSCC(a.GetDescriptor()); - std::vector names; + std::vector names; + names.reserve(scc->descriptors.size()); for (int i = 0; i < scc->descriptors.size(); i++) { names.push_back(scc->descriptors[i]->full_name()); } - string package = a.GetDescriptor()->file()->package(); + std::string package = a.GetDescriptor()->file()->package(); ASSERT_EQ(names.size(), 4); std::sort(names.begin(), names.end()); EXPECT_EQ(names[0], package + ".TestMutualRecursionA"); @@ -2223,16 +2224,19 @@ namespace cpp_unittest { TEST_F(GENERATED_SERVICE_TEST_NAME, NoGenericServices) { // Verify that non-services in unittest_no_generic_services.proto were // generated. - no_generic_services_test::TestMessage message; + ::protobuf_unittest::no_generic_services_test::TestMessage message; message.set_a(1); - message.SetExtension(no_generic_services_test::test_extension, 123); - no_generic_services_test::TestEnum e = no_generic_services_test::FOO; + message.SetExtension( + ::protobuf_unittest::no_generic_services_test::test_extension, 123); + ::protobuf_unittest::no_generic_services_test::TestEnum e = + ::protobuf_unittest::no_generic_services_test::FOO; EXPECT_EQ(e, 1); // Verify that a ServiceDescriptor is generated for the service even if the // class itself is not. const FileDescriptor* file = - no_generic_services_test::TestMessage::descriptor()->file(); + ::google::protobuf::unittest::no_generic_services_test::TestMessage::descriptor() + ->file(); ASSERT_EQ(1, file->service_count()); EXPECT_EQ("TestService", file->service(0)->name()); @@ -2267,3 +2271,5 @@ TEST(DESCRIPTOR_INIT_TEST_NAME, Initialized) { } // namespace compiler } // namespace protobuf } // namespace google + +#include diff --git a/src/google/protobuf/compiler/cpp/metadata_test.cc b/src/google/protobuf/compiler/cpp/metadata_test.cc index 77636fe982..045c6a3d29 100644 --- a/src/google/protobuf/compiler/cpp/metadata_test.cc +++ b/src/google/protobuf/compiler/cpp/metadata_test.cc @@ -56,27 +56,26 @@ class CppMetadataTest : public ::testing::Test { // code from the previously added file with name `filename`. Returns true on // success. If pb_h is non-null, expects a .pb.h and a .pb.h.meta (copied to // pb_h and pb_h_info respecfively); similarly for proto_h and proto_h_info. - bool CaptureMetadata(const string& filename, FileDescriptorProto* file, - string* pb_h, GeneratedCodeInfo* pb_h_info, - string* proto_h, GeneratedCodeInfo* proto_h_info, - string* pb_cc) { + bool CaptureMetadata(const std::string& filename, FileDescriptorProto* file, + std::string* pb_h, GeneratedCodeInfo* pb_h_info, + std::string* proto_h, GeneratedCodeInfo* proto_h_info, + std::string* pb_cc) { CommandLineInterface cli; CppGenerator cpp_generator; cli.RegisterGenerator("--cpp_out", &cpp_generator, ""); - string cpp_out = + std::string cpp_out = "--cpp_out=annotate_headers=true," "annotation_pragma_name=pragma_name," "annotation_guard_name=guard_name:" + TestTempDir(); - const bool result = - atu::RunProtoCompiler(filename, cpp_out, &cli, file); + const bool result = atu::RunProtoCompiler(filename, cpp_out, &cli, file); if (!result) { return result; } - string output_base = TestTempDir() + "/" + StripProto(filename); + std::string output_base = TestTempDir() + "/" + StripProto(filename); if (pb_cc != NULL) { GOOGLE_CHECK_OK( @@ -112,7 +111,7 @@ const char kSmallTestFile[] = TEST_F(CppMetadataTest, CapturesEnumNames) { FileDescriptorProto file; GeneratedCodeInfo info; - string pb_h; + std::string pb_h; atu::AddFile("test.proto", kSmallTestFile); EXPECT_TRUE( CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); @@ -129,19 +128,19 @@ TEST_F(CppMetadataTest, CapturesEnumNames) { TEST_F(CppMetadataTest, AddsPragma) { FileDescriptorProto file; GeneratedCodeInfo info; - string pb_h; + std::string pb_h; atu::AddFile("test.proto", kSmallTestFile); EXPECT_TRUE( CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); EXPECT_TRUE(pb_h.find("#ifdef guard_name") != string::npos); EXPECT_TRUE(pb_h.find("#pragma pragma_name \"test.pb.h.meta\"") != - string::npos); + std::string::npos); } TEST_F(CppMetadataTest, CapturesMessageNames) { FileDescriptorProto file; GeneratedCodeInfo info; - string pb_h; + std::string pb_h; atu::AddFile("test.proto", kSmallTestFile); EXPECT_TRUE( CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); diff --git a/src/google/protobuf/compiler/csharp/csharp_bootstrap_unittest.cc b/src/google/protobuf/compiler/csharp/csharp_bootstrap_unittest.cc index 33c5619843..978fdf02b6 100644 --- a/src/google/protobuf/compiler/csharp/csharp_bootstrap_unittest.cc +++ b/src/google/protobuf/compiler/csharp/csharp_bootstrap_unittest.cc @@ -77,23 +77,19 @@ class MockErrorCollector : public MultiFileErrorCollector { class MockGeneratorContext : public GeneratorContext { public: - MockGeneratorContext() {} - ~MockGeneratorContext() { - STLDeleteValues(&files_); - } - void ExpectFileMatches(const string& virtual_filename, const string& physical_filename) { - string* expected_contents = FindPtrOrNull(files_, virtual_filename); - ASSERT_TRUE(expected_contents != NULL) + auto it = files_.find(virtual_filename); + ASSERT_TRUE(it != files_.end()) << "Generator failed to generate file: " << virtual_filename; + string expected_contents = *it->second; string actual_contents; GOOGLE_CHECK_OK( File::GetContentsAsText(TestSourceDir() + "/" + physical_filename, &actual_contents, true)) << "Unable to get " << physical_filename; - EXPECT_TRUE(actual_contents == *expected_contents) + EXPECT_TRUE(actual_contents == expected_contents) << physical_filename << " needs to be regenerated. Please run " "generate_descriptor_proto.sh. Then add this file " "to your CL."; @@ -102,15 +98,13 @@ class MockGeneratorContext : public GeneratorContext { // implements GeneratorContext -------------------------------------- virtual io::ZeroCopyOutputStream* Open(const string& filename) { - string** map_slot = &files_[filename]; - delete *map_slot; - *map_slot = new string; - - return new io::StringOutputStream(*map_slot); + auto& map_slot = files_[filename]; + map_slot.reset(new std::string); + return new io::StringOutputStream(map_slot.get()); } private: - std::map files_; + std::map> files_; }; class GenerateAndTest { diff --git a/src/google/protobuf/compiler/csharp/csharp_doc_comment.cc b/src/google/protobuf/compiler/csharp/csharp_doc_comment.cc index a21dc0a495..fcc0aec0ac 100644 --- a/src/google/protobuf/compiler/csharp/csharp_doc_comment.cc +++ b/src/google/protobuf/compiler/csharp/csharp_doc_comment.cc @@ -56,7 +56,8 @@ void WriteDocCommentBodyImpl(io::Printer* printer, SourceLocation location) { // node of a summary element, not part of an attribute. comments = StringReplace(comments, "&", "&", true); comments = StringReplace(comments, "<", "<", true); - std::vector lines = Split(comments, "\n", false /* skip_empty */); + std::vector lines; + SplitStringAllowEmpty(comments, "\n", &lines); // TODO: We really should work out which part to put in the summary and which to put in the remarks... // but that needs to be part of a bigger effort to understand the markdown better anyway. printer->Print("/// \n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_enum.cc b/src/google/protobuf/compiler/csharp/csharp_enum.cc index 32c719907c..2baefd84ed 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum.cc +++ b/src/google/protobuf/compiler/csharp/csharp_enum.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -80,12 +79,12 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Print("[pbr::OriginalName(\"$original_name$\", PreferredAlias = false)] $name$ = $number$,\n", "original_name", original_name, "name", name, - "number", SimpleItoa(number)); + "number", StrCat(number)); } else { printer->Print("[pbr::OriginalName(\"$original_name$\")] $name$ = $number$,\n", "original_name", original_name, "name", name, - "number", SimpleItoa(number)); + "number", StrCat(number)); } } printer->Outdent(); diff --git a/src/google/protobuf/compiler/csharp/csharp_enum.h b/src/google/protobuf/compiler/csharp/csharp_enum.h index 5170d839b9..e409c2e4b2 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum.h +++ b/src/google/protobuf/compiler/csharp/csharp_enum.h @@ -48,12 +48,13 @@ class EnumGenerator : public SourceGeneratorBase { EnumGenerator(const EnumDescriptor* descriptor, const Options* options); ~EnumGenerator(); + EnumGenerator(const EnumGenerator&) = delete; + EnumGenerator& operator=(const EnumGenerator&) = delete; + void Generate(io::Printer* printer); private: const EnumDescriptor* descriptor_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_enum_field.cc b/src/google/protobuf/compiler/csharp/csharp_enum_field.cc index 9ceffa8cab..186fa27e5b 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_enum_field.cc @@ -31,12 +31,12 @@ #include #include -#include #include #include #include #include +#include #include #include #include @@ -47,8 +47,8 @@ namespace compiler { namespace csharp { EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, const Options *options) - : PrimitiveFieldGenerator(descriptor, fieldOrdinal, options) { + int presenceIndex, const Options *options) + : PrimitiveFieldGenerator(descriptor, presenceIndex, options) { } EnumFieldGenerator::~EnumFieldGenerator() { @@ -56,7 +56,7 @@ EnumFieldGenerator::~EnumFieldGenerator() { void EnumFieldGenerator::GenerateParsingCode(io::Printer* printer) { printer->Print(variables_, - "$name$_ = ($type_name$) input.ReadEnum();\n"); + "$property_name$ = ($type_name$) input.ReadEnum();\n"); } void EnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) { @@ -76,14 +76,25 @@ void EnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { } void EnumFieldGenerator::GenerateCodecCode(io::Printer* printer) { - printer->Print( - variables_, - "pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x)"); + printer->Print( + variables_, + "pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x, $default_value$)"); +} + +void EnumFieldGenerator::GenerateExtensionCode(io::Printer* printer) { + WritePropertyDocComment(printer, descriptor_); + AddDeprecatedFlag(printer); + printer->Print( + variables_, + "$access_level$ static readonly pb::Extension<$extended_type$, $type_name$> $property_name$ =\n" + " new pb::Extension<$extended_type$, $type_name$>($number$, "); + GenerateCodecCode(printer); + printer->Print(");\n"); } EnumOneofFieldGenerator::EnumOneofFieldGenerator( - const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options) - : PrimitiveOneofFieldGenerator(descriptor, fieldOrdinal, options) { + const FieldDescriptor* descriptor, int presenceIndex, const Options *options) + : PrimitiveOneofFieldGenerator(descriptor, presenceIndex, options) { } EnumOneofFieldGenerator::~EnumOneofFieldGenerator() { diff --git a/src/google/protobuf/compiler/csharp/csharp_enum_field.h b/src/google/protobuf/compiler/csharp/csharp_enum_field.h index 631632bcba..9f1a2ea777 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_enum_field.h @@ -44,33 +44,34 @@ namespace csharp { class EnumFieldGenerator : public PrimitiveFieldGenerator { public: EnumFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~EnumFieldGenerator(); + EnumFieldGenerator(const EnumFieldGenerator&) = delete; + EnumFieldGenerator& operator=(const EnumFieldGenerator&) = delete; + virtual void GenerateCodecCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator); + virtual void GenerateExtensionCode(io::Printer* printer); }; class EnumOneofFieldGenerator : public PrimitiveOneofFieldGenerator { public: EnumOneofFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~EnumOneofFieldGenerator(); + EnumOneofFieldGenerator(const EnumOneofFieldGenerator&) = delete; + EnumOneofFieldGenerator& operator=(const EnumOneofFieldGenerator&) = delete; + virtual void GenerateMergingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumOneofFieldGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.cc b/src/google/protobuf/compiler/csharp/csharp_field_base.cc index 7e737e47f9..9be7b70e0b 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.cc +++ b/src/google/protobuf/compiler/csharp/csharp_field_base.cc @@ -28,17 +28,16 @@ // (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 -#include #include #include @@ -57,52 +56,90 @@ void FieldGeneratorBase::SetCommonFieldVariables( // repeated fields varies by wire format. The wire format is encoded in the bottom 3 bits, which // never effects the tag size. int tag_size = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type()); + int part_tag_size = tag_size; + if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { + part_tag_size /= 2; + } uint tag = internal::WireFormat::MakeTag(descriptor_); uint8 tag_array[5]; io::CodedOutputStream::WriteTagToArray(tag, tag_array); - string tag_bytes = SimpleItoa(tag_array[0]); - for (int i = 1; i < tag_size; i++) { - tag_bytes += ", " + SimpleItoa(tag_array[i]); + string tag_bytes = StrCat(tag_array[0]); + for (int i = 1; i < part_tag_size; i++) { + tag_bytes += ", " + StrCat(tag_array[i]); } - (*variables)["access_level"] = "public"; - (*variables)["tag"] = SimpleItoa(tag); - (*variables)["tag_size"] = SimpleItoa(tag_size); + (*variables)["tag"] = StrCat(tag); + (*variables)["tag_size"] = StrCat(tag_size); (*variables)["tag_bytes"] = tag_bytes; + if (descriptor_->type() == FieldDescriptor::Type::TYPE_GROUP) { + tag = internal::WireFormatLite::MakeTag( + descriptor_->number(), + internal::WireFormatLite::WIRETYPE_END_GROUP); + io::CodedOutputStream::WriteTagToArray(tag, tag_array); + tag_bytes = StrCat(tag_array[0]); + for (int i = 1; i < part_tag_size; i++) { + tag_bytes += ", " + StrCat(tag_array[i]); + } + + variables_["end_tag"] = StrCat(tag); + variables_["end_tag_bytes"] = tag_bytes; + } + + (*variables)["access_level"] = "public"; + (*variables)["property_name"] = property_name(); (*variables)["type_name"] = type_name(); + (*variables)["extended_type"] = GetClassName(descriptor_->containing_type()); (*variables)["name"] = name(); (*variables)["descriptor_name"] = descriptor_->name(); (*variables)["default_value"] = default_value(); - if (has_default_value()) { + (*variables)["capitalized_type_name"] = capitalized_type_name(); + (*variables)["number"] = number(); + if (has_default_value() && !IsProto2(descriptor_->file())) { (*variables)["name_def_message"] = (*variables)["name"] + "_ = " + (*variables)["default_value"]; } else { (*variables)["name_def_message"] = (*variables)["name"] + "_"; } - (*variables)["capitalized_type_name"] = capitalized_type_name(); - (*variables)["number"] = number(); - (*variables)["has_property_check"] = - (*variables)["property_name"] + " != " + (*variables)["default_value"]; - (*variables)["other_has_property_check"] = "other." + - (*variables)["property_name"] + " != " + (*variables)["default_value"]; + if (IsProto2(descriptor_->file())) { + (*variables)["has_property_check"] = "Has" + (*variables)["property_name"]; + (*variables)["other_has_property_check"] = "other.Has" + (*variables)["property_name"]; + (*variables)["has_not_property_check"] = "!" + (*variables)["has_property_check"]; + (*variables)["other_has_not_property_check"] = "!" + (*variables)["other_has_property_check"]; + if (presenceIndex_ != -1) { + string hasBitsNumber = StrCat(presenceIndex_ / 32); + string hasBitsMask = StrCat(1 << (presenceIndex_ % 32)); + (*variables)["has_field_check"] = "(_hasBits" + hasBitsNumber + " & " + hasBitsMask + ") != 0"; + (*variables)["set_has_field"] = "_hasBits" + hasBitsNumber + " |= " + hasBitsMask; + (*variables)["clear_has_field"] = "_hasBits" + hasBitsNumber + " &= ~" + hasBitsMask; + } + } else { + (*variables)["has_property_check"] = + (*variables)["property_name"] + " != " + (*variables)["default_value"]; + (*variables)["other_has_property_check"] = "other." + + (*variables)["property_name"] + " != " + (*variables)["default_value"]; + } } void FieldGeneratorBase::SetCommonOneofFieldVariables( std::map* variables) { (*variables)["oneof_name"] = oneof_name(); - (*variables)["has_property_check"] = - oneof_name() + "Case_ == " + oneof_property_name() + - "OneofCase." + property_name(); + if (IsProto2(descriptor_->file())) { + (*variables)["has_property_check"] = "Has" + property_name(); + } else { + (*variables)["has_property_check"] = + oneof_name() + "Case_ == " + oneof_property_name() + + "OneofCase." + property_name(); + } (*variables)["oneof_property_name"] = oneof_property_name(); } FieldGeneratorBase::FieldGeneratorBase(const FieldDescriptor* descriptor, - int fieldOrdinal, const Options* options) + int presenceIndex, const Options* options) : SourceGeneratorBase(descriptor->file(), options), descriptor_(descriptor), - fieldOrdinal_(fieldOrdinal) { + presenceIndex_(presenceIndex) { SetCommonFieldVariables(&variables_); } @@ -119,6 +156,11 @@ void FieldGeneratorBase::GenerateCodecCode(io::Printer* printer) { // Could fail if we get called here though... } +void FieldGeneratorBase::GenerateExtensionCode(io::Printer* printer) { + // No-op: only message fields, enum fields, primitives, + // and repeated fields need this default is to not generate any code +} + void FieldGeneratorBase::AddDeprecatedFlag(io::Printer* printer) { if (descriptor_->options().deprecated()) { printer->Print("[global::System.ObsoleteAttribute]\n"); @@ -251,36 +293,6 @@ bool FieldGeneratorBase::has_default_value() { } } -bool FieldGeneratorBase::is_nullable_type() { - switch (descriptor_->type()) { - case FieldDescriptor::TYPE_ENUM: - case FieldDescriptor::TYPE_DOUBLE: - case FieldDescriptor::TYPE_FLOAT: - case FieldDescriptor::TYPE_INT64: - case FieldDescriptor::TYPE_UINT64: - case FieldDescriptor::TYPE_INT32: - case FieldDescriptor::TYPE_FIXED64: - case FieldDescriptor::TYPE_FIXED32: - case FieldDescriptor::TYPE_BOOL: - case FieldDescriptor::TYPE_UINT32: - case FieldDescriptor::TYPE_SFIXED32: - case FieldDescriptor::TYPE_SFIXED64: - case FieldDescriptor::TYPE_SINT32: - case FieldDescriptor::TYPE_SINT64: - return false; - - case FieldDescriptor::TYPE_MESSAGE: - case FieldDescriptor::TYPE_GROUP: - case FieldDescriptor::TYPE_STRING: - case FieldDescriptor::TYPE_BYTES: - return true; - - default: - GOOGLE_LOG(FATAL)<< "Unknown field type."; - return true; - } -} - bool AllPrintableAscii(const std::string& text) { for(int i = 0; i < text.size(); i++) { if (text[i] < 0x20 || text[i] > 0x7e) { @@ -290,14 +302,18 @@ bool AllPrintableAscii(const std::string& text) { return true; } -std::string FieldGeneratorBase::GetStringDefaultValueInternal() { - // No other default values needed for proto3... - return "\"\""; +std::string FieldGeneratorBase::GetStringDefaultValueInternal(const FieldDescriptor* descriptor) { + if (descriptor->default_value_string().empty()) + return "\"\""; + else + return "global::System.Encoding.UTF8.GetString(global::System.Convert.FromBase64String(\" +" + StringToBase64(descriptor->default_value_string()) + " +\"))"; } -std::string FieldGeneratorBase::GetBytesDefaultValueInternal() { - // No other default values needed for proto3... - return "pb::ByteString.Empty"; +std::string FieldGeneratorBase::GetBytesDefaultValueInternal(const FieldDescriptor* descriptor) { + if (descriptor->default_value_string().empty()) + return "pb::ByteString.Empty"; + else + return "pb::ByteString.FromBase64(\"" + StringToBase64(descriptor->default_value_string()) + "\")"; } std::string FieldGeneratorBase::default_value() { @@ -307,9 +323,8 @@ std::string FieldGeneratorBase::default_value() { std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) { switch (descriptor->type()) { case FieldDescriptor::TYPE_ENUM: - // All proto3 enums have a default value of 0, and there's an implicit conversion from the constant 0 to - // any C# enum. This means we don't need to work out what we actually mapped the enum value name to. - return "0"; + return GetClassName(descriptor->default_value_enum()->type()) + "." + + GetEnumValueName(descriptor->default_value_enum()->type()->name(), descriptor->default_value_enum()->name()); case FieldDescriptor::TYPE_MESSAGE: case FieldDescriptor::TYPE_GROUP: if (IsWrapperType(descriptor)) { @@ -324,10 +339,10 @@ std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) return "double.PositiveInfinity"; } else if (value == -std::numeric_limits::infinity()) { return "double.NegativeInfinity"; - } else if (MathLimits::IsNaN(value)) { + } else if (std::isnan(value)) { return "double.NaN"; } - return SimpleDtoa(value) + "D"; + return StrCat(value) + "D"; } case FieldDescriptor::TYPE_FLOAT: { float value = descriptor->default_value_float(); @@ -335,21 +350,21 @@ std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) return "float.PositiveInfinity"; } else if (value == -std::numeric_limits::infinity()) { return "float.NegativeInfinity"; - } else if (MathLimits::IsNaN(value)) { + } else if (std::isnan(value)) { return "float.NaN"; } - return SimpleFtoa(value) + "F"; + return StrCat(value) + "F"; } case FieldDescriptor::TYPE_INT64: - return SimpleItoa(descriptor->default_value_int64()) + "L"; + return StrCat(descriptor->default_value_int64()) + "L"; case FieldDescriptor::TYPE_UINT64: - return SimpleItoa(descriptor->default_value_uint64()) + "UL"; + return StrCat(descriptor->default_value_uint64()) + "UL"; case FieldDescriptor::TYPE_INT32: - return SimpleItoa(descriptor->default_value_int32()); + return StrCat(descriptor->default_value_int32()); case FieldDescriptor::TYPE_FIXED64: - return SimpleItoa(descriptor->default_value_uint64()) + "UL"; + return StrCat(descriptor->default_value_uint64()) + "UL"; case FieldDescriptor::TYPE_FIXED32: - return SimpleItoa(descriptor->default_value_uint32()); + return StrCat(descriptor->default_value_uint32()); case FieldDescriptor::TYPE_BOOL: if (descriptor->default_value_bool()) { return "true"; @@ -357,19 +372,19 @@ std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) return "false"; } case FieldDescriptor::TYPE_STRING: - return GetStringDefaultValueInternal(); + return GetStringDefaultValueInternal(descriptor); case FieldDescriptor::TYPE_BYTES: - return GetBytesDefaultValueInternal(); + return GetBytesDefaultValueInternal(descriptor); case FieldDescriptor::TYPE_UINT32: - return SimpleItoa(descriptor->default_value_uint32()); + return StrCat(descriptor->default_value_uint32()); case FieldDescriptor::TYPE_SFIXED32: - return SimpleItoa(descriptor->default_value_int32()); + return StrCat(descriptor->default_value_int32()); case FieldDescriptor::TYPE_SFIXED64: - return SimpleItoa(descriptor->default_value_int64()) + "L"; + return StrCat(descriptor->default_value_int64()) + "L"; case FieldDescriptor::TYPE_SINT32: - return SimpleItoa(descriptor->default_value_int32()); + return StrCat(descriptor->default_value_int32()); case FieldDescriptor::TYPE_SINT64: - return SimpleItoa(descriptor->default_value_int64()) + "L"; + return StrCat(descriptor->default_value_int64()) + "L"; default: GOOGLE_LOG(FATAL)<< "Unknown field type."; return ""; @@ -377,7 +392,7 @@ std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) } std::string FieldGeneratorBase::number() { - return SimpleItoa(descriptor_->number()); + return StrCat(descriptor_->number()); } std::string FieldGeneratorBase::capitalized_type_name() { diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.h b/src/google/protobuf/compiler/csharp/csharp_field_base.h index 62c255176a..594461da78 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.h +++ b/src/google/protobuf/compiler/csharp/csharp_field_base.h @@ -47,13 +47,17 @@ namespace csharp { class FieldGeneratorBase : public SourceGeneratorBase { public: FieldGeneratorBase(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options* options); ~FieldGeneratorBase(); + FieldGeneratorBase(const FieldGeneratorBase&) = delete; + FieldGeneratorBase& operator=(const FieldGeneratorBase&) = delete; + virtual void GenerateCloningCode(io::Printer* printer) = 0; virtual void GenerateFreezingCode(io::Printer* printer); virtual void GenerateCodecCode(io::Printer* printer); + virtual void GenerateExtensionCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer) = 0; virtual void GenerateMergingCode(io::Printer* printer) = 0; virtual void GenerateParsingCode(io::Printer* printer) = 0; @@ -67,7 +71,7 @@ class FieldGeneratorBase : public SourceGeneratorBase { protected: const FieldDescriptor* descriptor_; - const int fieldOrdinal_; + const int presenceIndex_; std::map variables_; void AddDeprecatedFlag(io::Printer* printer); @@ -84,7 +88,6 @@ class FieldGeneratorBase : public SourceGeneratorBase { std::string type_name(); std::string type_name(const FieldDescriptor* descriptor); bool has_default_value(); - bool is_nullable_type(); std::string default_value(); std::string default_value(const FieldDescriptor* descriptor); std::string number(); @@ -92,10 +95,8 @@ class FieldGeneratorBase : public SourceGeneratorBase { private: void SetCommonFieldVariables(std::map* variables); - std::string GetStringDefaultValueInternal(); - std::string GetBytesDefaultValueInternal(); - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorBase); + std::string GetStringDefaultValueInternal(const FieldDescriptor* descriptor); + std::string GetBytesDefaultValueInternal(const FieldDescriptor* descriptor); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_generator.cc b/src/google/protobuf/compiler/csharp/csharp_generator.cc index 0c93fc2929..2b353b3413 100644 --- a/src/google/protobuf/compiler/csharp/csharp_generator.cc +++ b/src/google/protobuf/compiler/csharp/csharp_generator.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -49,8 +48,7 @@ namespace protobuf { namespace compiler { namespace csharp { -void GenerateFile(const google::protobuf::FileDescriptor* file, - io::Printer* printer, +void GenerateFile(const FileDescriptor* file, io::Printer* printer, const Options* options) { ReflectionClassGenerator reflectionClassGenerator(file, options); reflectionClassGenerator.Generate(printer); @@ -81,6 +79,8 @@ bool Generator::Generate( cli_options.base_namespace_specified = true; } else if (options[i].first == "internal_access") { cli_options.internal_access = true; + } else if (options[i].first == "serializable") { + cli_options.serializable = true; } else { *error = "Unknown generator option: " + options[i].first; return false; diff --git a/src/google/protobuf/compiler/csharp/csharp_generator.h b/src/google/protobuf/compiler/csharp/csharp_generator.h index c8b1952913..da72e0e776 100644 --- a/src/google/protobuf/compiler/csharp/csharp_generator.h +++ b/src/google/protobuf/compiler/csharp/csharp_generator.h @@ -37,6 +37,8 @@ #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -46,9 +48,8 @@ namespace csharp { // header. If you create your own protocol compiler binary and you want // it to support C# output, you can do so by registering an instance of this // CodeGenerator with the CommandLineInterface in your main() function. -class LIBPROTOC_EXPORT Generator - : public google::protobuf::compiler::CodeGenerator { -public: +class PROTOC_EXPORT Generator : public CodeGenerator { + public: virtual bool Generate( const FileDescriptor* file, const string& parameter, @@ -61,4 +62,6 @@ public: } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.cc b/src/google/protobuf/compiler/csharp/csharp_helpers.cc index 04b610744a..f0684a6f7f 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.cc +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.cc @@ -33,9 +33,9 @@ // Sanjay Ghemawat, Jeff Dean, and others. #include -#include #include #include +#include #include #include @@ -43,7 +43,6 @@ #include #include #include -#include #include #include @@ -134,6 +133,12 @@ std::string GetReflectionClassUnqualifiedName(const FileDescriptor* descriptor) return GetFileNameBase(descriptor) + "Reflection"; } +std::string GetExtensionClassUnqualifiedName(const FileDescriptor* descriptor) { + // TODO: Detect collisions with existing messages, + // and append an underscore if necessary. + return GetFileNameBase(descriptor) + "Extensions"; +} + // TODO(jtattermusch): can we reuse a utility function? std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter, @@ -197,7 +202,7 @@ std::string ShoutyToPascalCase(const std::string& input) { char current = input[i]; if (!ascii_isalnum(current)) { previous = current; - continue; + continue; } if (!ascii_isalnum(previous)) { result += ascii_toupper(current); @@ -227,7 +232,7 @@ std::string TryRemovePrefix(const std::string& prefix, const std::string& value) prefix_to_match += ascii_tolower(prefix[i]); } } - + // This keeps track of how much of value we've consumed size_t prefix_index, value_index; for (prefix_index = 0, value_index = 0; @@ -277,6 +282,19 @@ std::string GetEnumValueName(const std::string& enum_name, const std::string& en return result; } +uint GetGroupEndTag(const Descriptor* descriptor) { + const Descriptor* containing_type = descriptor->containing_type(); + if (containing_type == NULL) { + return 0; + } + const FieldDescriptor* field = containing_type->FindFieldByName(descriptor->name()); + if (field != NULL && field->type() == FieldDescriptor::Type::TYPE_GROUP) { + return internal::WireFormatLite::MakeTag(field->number(), internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED); + } else { + return 0; + } +} + std::string ToCSharpName(const std::string& name, const FileDescriptor* file) { std::string result = GetFileNamespace(file); if (result != "") { @@ -303,6 +321,15 @@ std::string GetReflectionClassName(const FileDescriptor* descriptor) { return "global::" + result; } +std::string GetFullExtensionName(const FieldDescriptor* descriptor) { + if (descriptor->extension_scope()) { + return GetClassName(descriptor->extension_scope()) + ".Extensions." + GetPropertyName(descriptor); + } + else { + return GetExtensionClassUnqualifiedName(descriptor->file()) + "." + GetPropertyName(descriptor); + } +} + std::string GetClassName(const Descriptor* descriptor) { return ToCSharpName(descriptor->full_name(), descriptor->file()); } @@ -341,12 +368,10 @@ std::string GetPropertyName(const FieldDescriptor* descriptor) { return property_name; } -std::string GetOutputFile( - const google::protobuf::FileDescriptor* descriptor, - const std::string file_extension, - const bool generate_directories, - const std::string base_namespace, - string* error) { +std::string GetOutputFile(const FileDescriptor* descriptor, + const std::string file_extension, + const bool generate_directories, + const std::string base_namespace, string* error) { string relative_filename = GetFileNameBase(descriptor) + file_extension; if (!generate_directories) { return relative_filename; @@ -452,55 +477,89 @@ std::string FileDescriptorToBase64(const FileDescriptor* descriptor) { } FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options* options) { switch (descriptor->type()) { case FieldDescriptor::TYPE_GROUP: case FieldDescriptor::TYPE_MESSAGE: if (descriptor->is_repeated()) { if (descriptor->is_map()) { - return new MapFieldGenerator(descriptor, fieldOrdinal, options); + return new MapFieldGenerator(descriptor, presenceIndex, options); } else { - return new RepeatedMessageFieldGenerator(descriptor, fieldOrdinal, options); + return new RepeatedMessageFieldGenerator(descriptor, presenceIndex, options); } } else { if (IsWrapperType(descriptor)) { if (descriptor->containing_oneof()) { - return new WrapperOneofFieldGenerator(descriptor, fieldOrdinal, options); + return new WrapperOneofFieldGenerator(descriptor, presenceIndex, options); } else { - return new WrapperFieldGenerator(descriptor, fieldOrdinal, options); + return new WrapperFieldGenerator(descriptor, presenceIndex, options); } } else { if (descriptor->containing_oneof()) { - return new MessageOneofFieldGenerator(descriptor, fieldOrdinal, options); + return new MessageOneofFieldGenerator(descriptor, presenceIndex, options); } else { - return new MessageFieldGenerator(descriptor, fieldOrdinal, options); + return new MessageFieldGenerator(descriptor, presenceIndex, options); } } } case FieldDescriptor::TYPE_ENUM: if (descriptor->is_repeated()) { - return new RepeatedEnumFieldGenerator(descriptor, fieldOrdinal, options); + return new RepeatedEnumFieldGenerator(descriptor, presenceIndex, options); } else { if (descriptor->containing_oneof()) { - return new EnumOneofFieldGenerator(descriptor, fieldOrdinal, options); + return new EnumOneofFieldGenerator(descriptor, presenceIndex, options); } else { - return new EnumFieldGenerator(descriptor, fieldOrdinal, options); + return new EnumFieldGenerator(descriptor, presenceIndex, options); } } default: if (descriptor->is_repeated()) { - return new RepeatedPrimitiveFieldGenerator(descriptor, fieldOrdinal, options); + return new RepeatedPrimitiveFieldGenerator(descriptor, presenceIndex, options); } else { if (descriptor->containing_oneof()) { - return new PrimitiveOneofFieldGenerator(descriptor, fieldOrdinal, options); + return new PrimitiveOneofFieldGenerator(descriptor, presenceIndex, options); } else { - return new PrimitiveFieldGenerator(descriptor, fieldOrdinal, options); + return new PrimitiveFieldGenerator(descriptor, presenceIndex, options); } } } } +bool IsNullable(const FieldDescriptor* descriptor) { + if (descriptor->is_repeated()) { + return true; + } + + switch (descriptor->type()) { + case FieldDescriptor::TYPE_ENUM: + case FieldDescriptor::TYPE_DOUBLE: + case FieldDescriptor::TYPE_FLOAT: + case FieldDescriptor::TYPE_INT64: + case FieldDescriptor::TYPE_UINT64: + case FieldDescriptor::TYPE_INT32: + case FieldDescriptor::TYPE_FIXED64: + case FieldDescriptor::TYPE_FIXED32: + case FieldDescriptor::TYPE_BOOL: + case FieldDescriptor::TYPE_UINT32: + case FieldDescriptor::TYPE_SFIXED32: + case FieldDescriptor::TYPE_SFIXED64: + case FieldDescriptor::TYPE_SINT32: + case FieldDescriptor::TYPE_SINT64: + return false; + + case FieldDescriptor::TYPE_MESSAGE: + case FieldDescriptor::TYPE_GROUP: + case FieldDescriptor::TYPE_STRING: + case FieldDescriptor::TYPE_BYTES: + return true; + + default: + GOOGLE_LOG(FATAL) << "Unknown field type."; + return true; + } +} + } // namespace csharp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.h b/src/google/protobuf/compiler/csharp/csharp_helpers.h index 8dd265bcf3..df18348131 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.h +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.h @@ -42,6 +42,8 @@ #include #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -73,6 +75,8 @@ std::string StripDotProto(const std::string& proto_file); // Gets unqualified name of the reflection class std::string GetReflectionClassUnqualifiedName(const FileDescriptor* descriptor); +// Gets unqualified name of the extension class +std::string GetExtensionClassUnqualifiedName(const FileDescriptor* descriptor); std::string GetClassName(const EnumDescriptor* descriptor); @@ -96,7 +100,8 @@ std::string UnderscoresToPascalCase(const std::string& input); // Note that we wouldn't normally want to export this (we're not expecting // it to be used outside libprotoc itself) but this exposes it for testing. -std::string LIBPROTOC_EXPORT GetEnumValueName(const std::string& enum_name, const std::string& enum_value_name); +std::string PROTOC_EXPORT GetEnumValueName(const std::string& enum_name, + const std::string& enum_value_name); // TODO(jtattermusch): perhaps we could move this to strutil std::string StringToBase64(const std::string& input); @@ -104,15 +109,22 @@ std::string StringToBase64(const std::string& input); std::string FileDescriptorToBase64(const FileDescriptor* descriptor); FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options* options); +std::string GetFullExtensionName(const FieldDescriptor* descriptor); + +bool IsNullable(const FieldDescriptor* descriptor); + // Determines whether the given message is a map entry message, // i.e. one implicitly created by protoc due to a map field. inline bool IsMapEntryMessage(const Descriptor* descriptor) { return descriptor->options().map_entry(); } +// Checks if this descriptor is for a group and gets its end tag or 0 if it's not a group +uint GetGroupEndTag(const Descriptor* descriptor); + // Determines whether we're generating code for the proto representation of // descriptors etc, for use in the runtime. This is the only type which is // allowed to use proto2 syntax, and it generates internal classes. @@ -141,8 +153,15 @@ inline bool IsWrapperType(const FieldDescriptor* descriptor) { descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto"; } +inline bool IsProto2(const FileDescriptor* descriptor) { + return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO2; +} + } // namespace csharp } // namespace compiler } // namespace protobuf } // namespace google + +#include + #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__ diff --git a/src/google/protobuf/compiler/csharp/csharp_map_field.cc b/src/google/protobuf/compiler/csharp/csharp_map_field.cc index d58514ce4a..f3f09ea454 100644 --- a/src/google/protobuf/compiler/csharp/csharp_map_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_map_field.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -48,15 +47,15 @@ namespace compiler { namespace csharp { MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options* options) - : FieldGeneratorBase(descriptor, fieldOrdinal, options) { + : FieldGeneratorBase(descriptor, presenceIndex, options) { } MapFieldGenerator::~MapFieldGenerator() { } -void MapFieldGenerator::GenerateMembers(io::Printer* printer) { +void MapFieldGenerator::GenerateMembers(io::Printer* printer) { const FieldDescriptor* key_descriptor = descriptor_->message_type()->FindFieldByName("key"); const FieldDescriptor* value_descriptor = diff --git a/src/google/protobuf/compiler/csharp/csharp_map_field.h b/src/google/protobuf/compiler/csharp/csharp_map_field.h index 84a33a0367..b920b9f225 100644 --- a/src/google/protobuf/compiler/csharp/csharp_map_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_map_field.h @@ -44,10 +44,13 @@ namespace csharp { class MapFieldGenerator : public FieldGeneratorBase { public: MapFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options* options); ~MapFieldGenerator(); + MapFieldGenerator(const MapFieldGenerator&) = delete; + MapFieldGenerator& operator=(const MapFieldGenerator&) = delete; + virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateFreezingCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer); @@ -59,9 +62,6 @@ class MapFieldGenerator : public FieldGeneratorBase { virtual void WriteHash(io::Printer* printer); virtual void WriteEquals(io::Printer* printer); virtual void WriteToString(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapFieldGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index 8a4307f157..6ebd89f02f 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -33,7 +33,6 @@ #include #include -#include #include #include #include @@ -42,6 +41,7 @@ #include #include +#include #include #include #include @@ -61,20 +61,29 @@ bool CompareFieldNumbers(const FieldDescriptor* d1, const FieldDescriptor* d2) { MessageGenerator::MessageGenerator(const Descriptor* descriptor, const Options* options) : SourceGeneratorBase(descriptor->file(), options), - descriptor_(descriptor) { - - // sorted field names - for (int i = 0; i < descriptor_->field_count(); i++) { - field_names_.push_back(descriptor_->field(i)->name()); - } - std::sort(field_names_.begin(), field_names_.end()); - + descriptor_(descriptor), + has_bit_field_count_(0), + end_tag_(GetGroupEndTag(descriptor)), + has_extension_ranges_(descriptor->extension_range_count() > 0) { // fields by number for (int i = 0; i < descriptor_->field_count(); i++) { fields_by_number_.push_back(descriptor_->field(i)); } std::sort(fields_by_number_.begin(), fields_by_number_.end(), CompareFieldNumbers); + + if (IsProto2(descriptor_->file())) { + int primitiveCount = 0; + for (int i = 0; i < descriptor_->field_count(); i++) { + const FieldDescriptor* field = descriptor_->field(i); + if (!IsNullable(field)) { + primitiveCount++; + if (has_bit_field_count_ == 0 || (primitiveCount % 32) == 0) { + has_bit_field_count_++; + } + } + } + } } MessageGenerator::~MessageGenerator() { @@ -88,10 +97,6 @@ std::string MessageGenerator::full_class_name() { return GetClassName(descriptor_); } -const std::vector& MessageGenerator::field_names() { - return field_names_; -} - const std::vector& MessageGenerator::fields_by_number() { return fields_by_number_; } @@ -102,6 +107,12 @@ void MessageGenerator::AddDeprecatedFlag(io::Printer* printer) { } } +void MessageGenerator::AddSerializableAttribute(io::Printer* printer) { + if (this->options()->serializable) { + printer->Print("[global::System.SerializableAttribute]\n"); + } +} + void MessageGenerator::Generate(io::Printer* printer) { std::map vars; vars["class_name"] = class_name(); @@ -109,10 +120,19 @@ void MessageGenerator::Generate(io::Printer* printer) { WriteMessageDocComment(printer, descriptor_); AddDeprecatedFlag(printer); + AddSerializableAttribute(printer); printer->Print( vars, - "$access_level$ sealed partial class $class_name$ : pb::IMessage<$class_name$> {\n"); + "$access_level$ sealed partial class $class_name$ : "); + + if (has_extension_ranges_) { + printer->Print(vars, "pb::IExtendableMessage<$class_name$>"); + } + else { + printer->Print(vars, "pb::IMessage<$class_name$>"); + } + printer->Print(" {\n"); printer->Indent(); // All static fields and properties @@ -123,6 +143,20 @@ void MessageGenerator::Generate(io::Printer* printer) { printer->Print( "private pb::UnknownFieldSet _unknownFields;\n"); + if (has_extension_ranges_) { + if (IsDescriptorProto(descriptor_->file())) { + printer->Print(vars, "internal pb::ExtensionSet<$class_name$> _extensions;\n"); // CustomOptions compatibility + } else { + printer->Print(vars, "private pb::ExtensionSet<$class_name$> _extensions;\n"); + } + } + + for (int i = 0; i < has_bit_field_count_; i++) { + // don't use arrays since all arrays are heap allocated, saving allocations + // use ints instead of bytes since bytes lack bitwise operators, saving casts + printer->Print("private int _hasBits$i$;\n", "i", StrCat(i)); + } + WriteGeneratedCodeAttributes(printer); printer->Print( @@ -132,10 +166,10 @@ void MessageGenerator::Generate(io::Printer* printer) { // Access the message descriptor via the relevant file descriptor or containing message descriptor. if (!descriptor_->containing_type()) { vars["descriptor_accessor"] = GetReflectionClassName(descriptor_->file()) - + ".Descriptor.MessageTypes[" + SimpleItoa(descriptor_->index()) + "]"; + + ".Descriptor.MessageTypes[" + StrCat(descriptor_->index()) + "]"; } else { vars["descriptor_accessor"] = GetClassName(descriptor_->containing_type()) - + ".Descriptor.NestedTypes[" + SimpleItoa(descriptor_->index()) + "]"; + + ".Descriptor.NestedTypes[" + StrCat(descriptor_->index()) + "]"; } WriteGeneratedCodeAttributes(printer); @@ -152,12 +186,6 @@ void MessageGenerator::Generate(io::Printer* printer) { " get { return Descriptor; }\n" "}\n" "\n"); - // CustomOptions property, only for options messages - if (IsDescriptorOptionMessage(descriptor_)) { - printer->Print( - "internal CustomOptions CustomOptions{ get; private set; } = CustomOptions.Empty;\n" - "\n"); - } // Parameterless constructor and partial OnConstruction method. WriteGeneratedCodeAttributes(printer); @@ -181,7 +209,7 @@ void MessageGenerator::Generate(io::Printer* printer) { "public const int $field_constant_name$ = $index$;\n", "field_name", fieldDescriptor->name(), "field_constant_name", GetFieldConstantName(fieldDescriptor), - "index", SimpleItoa(fieldDescriptor->number())); + "index", StrCat(fieldDescriptor->number())); std::unique_ptr generator( CreateFieldGeneratorInternal(fieldDescriptor)); generator->GenerateMembers(printer); @@ -204,7 +232,7 @@ void MessageGenerator::Generate(io::Printer* printer) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); printer->Print("$field_property_name$ = $index$,\n", "field_property_name", GetPropertyName(field), - "index", SimpleItoa(field->number())); + "index", StrCat(field->number())); } printer->Outdent(); printer->Print("}\n"); @@ -233,6 +261,32 @@ void MessageGenerator::Generate(io::Printer* printer) { GenerateMessageSerializationMethods(printer); GenerateMergingMethods(printer); + if (has_extension_ranges_) { + printer->Print( + vars, + "public TValue GetExtension(pb::Extension<$class_name$, TValue> extension) {\n" + " return pb::ExtensionSet.Get(ref _extensions, extension);\n" + "}\n" + "public pbc::RepeatedField GetExtension(pb::RepeatedExtension<$class_name$, TValue> extension) {\n" + " return pb::ExtensionSet.Get(ref _extensions, extension);\n" + "}\n" + "public pbc::RepeatedField GetOrRegisterExtension(pb::RepeatedExtension<$class_name$, TValue> extension) {\n" + " return pb::ExtensionSet.GetOrRegister(ref _extensions, extension);\n" + "}\n" + "public void SetExtension(pb::Extension<$class_name$, TValue> extension, TValue value) {\n" + " pb::ExtensionSet.Set(ref _extensions, extension, value);\n" + "}\n" + "public bool HasExtension(pb::Extension<$class_name$, TValue> extension) {\n" + " return pb::ExtensionSet.Has(ref _extensions, extension);\n" + "}\n" + "public void ClearExtension(pb::Extension<$class_name$, TValue> extension) {\n" + " pb::ExtensionSet.Clear(ref _extensions, extension);\n" + "}\n" + "public void ClearExtension(pb::RepeatedExtension<$class_name$, TValue> extension) {\n" + " pb::ExtensionSet.Clear(ref _extensions, extension);\n" + "}\n\n"); + } + // Nested messages and enums if (HasNestedGeneratedTypes()) { printer->Print( @@ -260,6 +314,26 @@ void MessageGenerator::Generate(io::Printer* printer) { "\n"); } + if (descriptor_->extension_count() > 0) { + printer->Print( + vars, + "#region Extensions\n" + "/// Container for extensions for other messages declared in the $class_name$ message type.\n"); + WriteGeneratedCodeAttributes(printer); + printer->Print("internal static partial class Extensions {\n"); + printer->Indent(); + for (int i = 0; i < descriptor_->extension_count(); i++) { + std::unique_ptr generator( + CreateFieldGeneratorInternal(descriptor_->extension(i))); + generator->GenerateExtensionCode(printer); + } + printer->Outdent(); + printer->Print( + "}\n" + "#endregion\n" + "\n"); + } + printer->Outdent(); printer->Print("}\n"); printer->Print("\n"); @@ -288,6 +362,9 @@ void MessageGenerator::GenerateCloningCode(io::Printer* printer) { vars, "public $class_name$($class_name$ other) : this() {\n"); printer->Indent(); + for (int i = 0; i < has_bit_field_count_; i++) { + printer->Print("_hasBits$i$ = other._hasBits$i$;\n", "i", StrCat(i)); + } // Clone non-oneof fields first for (int i = 0; i < descriptor_->field_count(); i++) { if (!descriptor_->field(i)->containing_oneof()) { @@ -321,6 +398,10 @@ void MessageGenerator::GenerateCloningCode(io::Printer* printer) { // Clone unknown fields printer->Print( "_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);\n"); + if (has_extension_ranges_) { + printer->Print( + "_extensions = pb::ExtensionSet.Clone(other._extensions);\n"); + } printer->Outdent(); printer->Print("}\n\n"); @@ -367,6 +448,12 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) { printer->Print("if ($property_name$Case != other.$property_name$Case) return false;\n", "property_name", UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true)); } + if (has_extension_ranges_) { + printer->Print( + "if (!Equals(_extensions, other._extensions)) {\n" + " return false;\n" + "}\n"); + } printer->Outdent(); printer->Print( " return Equals(_unknownFields, other._unknownFields);\n" @@ -388,6 +475,12 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) { printer->Print("hash ^= (int) $name$Case_;\n", "name", UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), false)); } + if (has_extension_ranges_) { + printer->Print( + "if (_extensions != null) {\n" + " hash ^= _extensions.GetHashCode();\n" + "}\n"); + } printer->Print( "if (_unknownFields != null) {\n" " hash ^= _unknownFields.GetHashCode();\n" @@ -416,6 +509,14 @@ void MessageGenerator::GenerateMessageSerializationMethods(io::Printer* printer) generator->GenerateSerializationCode(printer); } + if (has_extension_ranges_) { + // Serialize extensions + printer->Print( + "if (_extensions != null) {\n" + " _extensions.WriteTo(output);\n" + "}\n"); + } + // Serialize unknown fields printer->Print( "if (_unknownFields != null) {\n" @@ -438,6 +539,13 @@ void MessageGenerator::GenerateMessageSerializationMethods(io::Printer* printer) generator->GenerateSerializedSizeCode(printer); } + if (has_extension_ranges_) { + printer->Print( + "if (_extensions != null) {\n" + " size += _extensions.CalculateSize();\n" + "}\n"); + } + printer->Print( "if (_unknownFields != null) {\n" " size += _unknownFields.CalculateSize();\n" @@ -466,7 +574,7 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer) { "}\n"); // Merge non-oneof fields for (int i = 0; i < descriptor_->field_count(); i++) { - if (!descriptor_->field(i)->containing_oneof()) { + if (!descriptor_->field(i)->containing_oneof()) { std::unique_ptr generator( CreateFieldGeneratorInternal(descriptor_->field(i))); generator->GenerateMergingCode(printer); @@ -493,6 +601,11 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer) { printer->Outdent(); printer->Print("}\n\n"); } + // Merge extensions + if (has_extension_ranges_) { + printer->Print("pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions);\n"); + } + // Merge unknown fields. printer->Print( "_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);\n"); @@ -510,11 +623,18 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer) { " switch(tag) {\n"); printer->Indent(); printer->Indent(); - // Option messages need to store unknown fields so that options can be parsed later. - if (IsDescriptorOptionMessage(descriptor_)) { + if (end_tag_ != 0) { + printer->Print( + "$end_tag$:\n" + " return;\n", + "end_tag", StrCat(end_tag_)); + } + if (has_extension_ranges_) { printer->Print( "default:\n" - " CustomOptions = CustomOptions.ReadOrSkipUnknownField(input);\n" + " if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, input)) {\n" + " _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);\n" + " }\n" " break;\n"); } else { printer->Print( @@ -536,13 +656,13 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer) { printer->Print( "case $packed_tag$:\n", "packed_tag", - SimpleItoa( + StrCat( internal::WireFormatLite::MakeTag( field->number(), internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED))); } - printer->Print("case $tag$: {\n", "tag", SimpleItoa(tag)); + printer->Print("case $tag$: {\n", "tag", StrCat(tag)); printer->Indent(); std::unique_ptr generator( CreateFieldGeneratorInternal(field)); @@ -559,19 +679,29 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer) { printer->Print("}\n\n"); // method } -int MessageGenerator::GetFieldOrdinal(const FieldDescriptor* descriptor) { - for (int i = 0; i < field_names().size(); i++) { - if (field_names()[i] == descriptor->name()) { - return i; +// it's a waste of space to track presence for all values, so we only track them if they're not nullable +int MessageGenerator::GetPresenceIndex(const FieldDescriptor* descriptor) { + if (IsNullable(descriptor) || !IsProto2(descriptor_->file())) { + return -1; + } + + int index = 0; + for (int i = 0; i < fields_by_number().size(); i++) { + const FieldDescriptor* field = fields_by_number()[i]; + if (field == descriptor) { + return index; + } + if (!IsNullable(field)) { + index++; } } - GOOGLE_LOG(DFATAL)<< "Could not find ordinal for field " << descriptor->name(); + GOOGLE_LOG(DFATAL)<< "Could not find presence index for field " << descriptor->name(); return -1; } FieldGeneratorBase* MessageGenerator::CreateFieldGeneratorInternal( const FieldDescriptor* descriptor) { - return CreateFieldGenerator(descriptor, GetFieldOrdinal(descriptor), this->options()); + return CreateFieldGenerator(descriptor, GetPresenceIndex(descriptor), this->options()); } } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_message.h b/src/google/protobuf/compiler/csharp/csharp_message.h index e7f3b4d009..5642dc8812 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.h +++ b/src/google/protobuf/compiler/csharp/csharp_message.h @@ -50,6 +50,9 @@ class MessageGenerator : public SourceGeneratorBase { MessageGenerator(const Descriptor* descriptor, const Options* options); ~MessageGenerator(); + MessageGenerator(const MessageGenerator&) = delete; + MessageGenerator& operator=(const MessageGenerator&) = delete; + void GenerateCloningCode(io::Printer* printer); void GenerateFreezingCode(io::Printer* printer); void GenerateFrameworkMethods(io::Printer* printer); @@ -57,30 +60,28 @@ class MessageGenerator : public SourceGeneratorBase { private: const Descriptor* descriptor_; - std::vector field_names_; std::vector fields_by_number_; + int has_bit_field_count_; + uint end_tag_; + bool has_extension_ranges_; void GenerateMessageSerializationMethods(io::Printer* printer); void GenerateMergingMethods(io::Printer* printer); - int GetFieldOrdinal(const FieldDescriptor* descriptor); + int GetPresenceIndex(const FieldDescriptor* descriptor); FieldGeneratorBase* CreateFieldGeneratorInternal( const FieldDescriptor* descriptor); bool HasNestedGeneratedTypes(); void AddDeprecatedFlag(io::Printer* printer); - + void AddSerializableAttribute(io::Printer* printer); + std::string class_name(); std::string full_class_name(); - // field names sorted alphabetically - const std::vector& field_names(); - // field descriptors sorted by number const std::vector& fields_by_number(); - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_message_field.cc index cf1b4dbfea..4125798312 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message_field.cc @@ -31,12 +31,13 @@ #include #include -#include #include #include #include #include #include +#include +#include #include #include @@ -49,11 +50,13 @@ namespace compiler { namespace csharp { MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options) - : FieldGeneratorBase(descriptor, fieldOrdinal, options) { - variables_["has_property_check"] = name() + "_ != null"; - variables_["has_not_property_check"] = name() + "_ == null"; + : FieldGeneratorBase(descriptor, presenceIndex, options) { + if (!IsProto2(descriptor_->file())) { + variables_["has_property_check"] = name() + "_ != null"; + variables_["has_not_property_check"] = name() + "_ == null"; + } } MessageFieldGenerator::~MessageFieldGenerator() { @@ -74,6 +77,26 @@ void MessageFieldGenerator::GenerateMembers(io::Printer* printer) { " $name$_ = value;\n" " }\n" "}\n"); + if (IsProto2(descriptor_->file())) { + printer->Print( + variables_, + "/// Gets whether the $descriptor_name$ field is set\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ bool Has$property_name$ {\n" + " get { return $name$_ != null; }\n" + "}\n"); + printer->Print( + variables_, + "/// Clears the value of the $descriptor_name$ field\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ void Clear$property_name$() {\n" + " $name$_ = null;\n" + "}\n"); + } } void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) { @@ -81,7 +104,7 @@ void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) { variables_, "if (other.$has_property_check$) {\n" " if ($has_not_property_check$) {\n" - " $name$_ = new $type_name$();\n" + " $property_name$ = new $type_name$();\n" " }\n" " $property_name$.MergeFrom(other.$property_name$);\n" "}\n"); @@ -91,27 +114,48 @@ void MessageFieldGenerator::GenerateParsingCode(io::Printer* printer) { printer->Print( variables_, "if ($has_not_property_check$) {\n" - " $name$_ = new $type_name$();\n" - "}\n" - // TODO(jonskeet): Do we really need merging behaviour like this? - "input.ReadMessage($name$_);\n"); // No need to support TYPE_GROUP... + " $property_name$ = new $type_name$();\n" + "}\n"); + if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) { + printer->Print(variables_, "input.ReadMessage($property_name$);\n"); + } else { + printer->Print(variables_, "input.ReadGroup($property_name$);\n"); + } } void MessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) { - printer->Print( - variables_, - "if ($has_property_check$) {\n" - " output.WriteRawTag($tag_bytes$);\n" - " output.WriteMessage($property_name$);\n" - "}\n"); + if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) { + printer->Print( + variables_, + "if ($has_property_check$) {\n" + " output.WriteRawTag($tag_bytes$);\n" + " output.WriteMessage($property_name$);\n" + "}\n"); + } else { + printer->Print( + variables_, + "if ($has_property_check$) {\n" + " output.WriteRawTag($tag_bytes$);\n" + " output.WriteGroup($property_name$);\n" + " output.WriteRawTag($end_tag_bytes$);\n" + "}\n"); + } } void MessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { - printer->Print( - variables_, - "if ($has_property_check$) {\n" - " size += $tag_size$ + pb::CodedOutputStream.ComputeMessageSize($property_name$);\n" - "}\n"); + if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) { + printer->Print( + variables_, + "if ($has_property_check$) {\n" + " size += $tag_size$ + pb::CodedOutputStream.ComputeMessageSize($property_name$);\n" + "}\n"); + } else { + printer->Print( + variables_, + "if ($has_property_check$) {\n" + " size += $tag_size$ + pb::CodedOutputStream.ComputeGroupSize($property_name$);\n" + "}\n"); + } } void MessageFieldGenerator::WriteHash(io::Printer* printer) { @@ -130,7 +174,16 @@ void MessageFieldGenerator::WriteToString(io::Printer* printer) { variables_, "PrintField(\"$field_name$\", has$property_name$, $name$_, writer);\n"); } - +void MessageFieldGenerator::GenerateExtensionCode(io::Printer* printer) { + WritePropertyDocComment(printer, descriptor_); + AddDeprecatedFlag(printer); + printer->Print( + variables_, + "$access_level$ static readonly pb::Extension<$extended_type$, $type_name$> $property_name$ =\n" + " new pb::Extension<$extended_type$, $type_name$>($number$, "); + GenerateCodecCode(printer); + printer->Print(");\n"); +} void MessageFieldGenerator::GenerateCloningCode(io::Printer* printer) { printer->Print(variables_, "$name$_ = other.$has_property_check$ ? other.$name$_.Clone() : null;\n"); @@ -140,16 +193,22 @@ void MessageFieldGenerator::GenerateFreezingCode(io::Printer* printer) { } void MessageFieldGenerator::GenerateCodecCode(io::Printer* printer) { - printer->Print( - variables_, - "pb::FieldCodec.ForMessage($tag$, $type_name$.Parser)"); + if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) { + printer->Print( + variables_, + "pb::FieldCodec.ForMessage($tag$, $type_name$.Parser)"); + } else { + printer->Print( + variables_, + "pb::FieldCodec.ForGroup($tag$, $end_tag$, $type_name$.Parser)"); + } } MessageOneofFieldGenerator::MessageOneofFieldGenerator( const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options) - : MessageFieldGenerator(descriptor, fieldOrdinal, options) { + : MessageFieldGenerator(descriptor, presenceIndex, options) { SetCommonOneofFieldVariables(&variables_); } @@ -169,10 +228,32 @@ void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) { " $oneof_name$Case_ = value == null ? $oneof_property_name$OneofCase.None : $oneof_property_name$OneofCase.$property_name$;\n" " }\n" "}\n"); + if (IsProto2(descriptor_->file())) { + printer->Print( + variables_, + "/// Gets whether the \"$descriptor_name$\" field is set\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ bool Has$property_name$ {\n" + " get { return $oneof_name$Case_ == $oneof_property_name$OneofCase.$property_name$; }\n" + "}\n"); + printer->Print( + variables_, + "/// Clears the value of the oneof if it's currently set to \"$descriptor_name$\" \n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ void Clear$property_name$() {\n" + " if ($has_property_check$) {\n" + " Clear$oneof_property_name$();\n" + " }\n" + "}\n"); + } } void MessageOneofFieldGenerator::GenerateMergingCode(io::Printer* printer) { - printer->Print(variables_, + printer->Print(variables_, "if ($property_name$ == null) {\n" " $property_name$ = new $type_name$();\n" "}\n" @@ -186,9 +267,13 @@ void MessageOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) { "$type_name$ subBuilder = new $type_name$();\n" "if ($has_property_check$) {\n" " subBuilder.MergeFrom($property_name$);\n" - "}\n" - "input.ReadMessage(subBuilder);\n" // No support of TYPE_GROUP - "$property_name$ = subBuilder;\n"); + "}\n"); + if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) { + printer->Print("input.ReadMessage(subBuilder);\n"); + } else { + printer->Print("input.ReadGroup(subBuilder);\n"); + } + printer->Print(variables_, "$property_name$ = subBuilder;\n"); } void MessageOneofFieldGenerator::WriteToString(io::Printer* printer) { diff --git a/src/google/protobuf/compiler/csharp/csharp_message_field.h b/src/google/protobuf/compiler/csharp/csharp_message_field.h index c41ee88a76..2463d912ba 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_message_field.h @@ -44,10 +44,13 @@ namespace csharp { class MessageFieldGenerator : public FieldGeneratorBase { public: MessageFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~MessageFieldGenerator(); + MessageFieldGenerator(const MessageFieldGenerator&) = delete; + MessageFieldGenerator& operator=(const MessageFieldGenerator&) = delete; + virtual void GenerateCodecCode(io::Printer* printer); virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateFreezingCode(io::Printer* printer); @@ -56,30 +59,29 @@ class MessageFieldGenerator : public FieldGeneratorBase { virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); + virtual void GenerateExtensionCode(io::Printer* printer); virtual void WriteHash(io::Printer* printer); virtual void WriteEquals(io::Printer* printer); virtual void WriteToString(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator); }; class MessageOneofFieldGenerator : public MessageFieldGenerator { public: MessageOneofFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~MessageOneofFieldGenerator(); + MessageOneofFieldGenerator(const MessageOneofFieldGenerator&) = delete; + MessageOneofFieldGenerator& operator=(const MessageOneofFieldGenerator&) = + delete; + virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMergingCode(io::Printer* printer); virtual void WriteToString(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageOneofFieldGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_names.h b/src/google/protobuf/compiler/csharp/csharp_names.h index 21758f2882..0c5ade919f 100644 --- a/src/google/protobuf/compiler/csharp/csharp_names.h +++ b/src/google/protobuf/compiler/csharp/csharp_names.h @@ -41,6 +41,8 @@ #include #include +#include + namespace google { namespace protobuf { @@ -57,14 +59,14 @@ namespace csharp { // // Returns: // The namespace to use for given file descriptor. -string LIBPROTOC_EXPORT GetFileNamespace(const FileDescriptor* descriptor); +string PROTOC_EXPORT GetFileNamespace(const FileDescriptor* descriptor); // Requires: // descriptor != NULL // // Returns: // The fully-qualified C# class name. -string LIBPROTOC_EXPORT GetClassName(const Descriptor* descriptor); +string PROTOC_EXPORT GetClassName(const Descriptor* descriptor); // Requires: // descriptor != NULL @@ -73,7 +75,7 @@ string LIBPROTOC_EXPORT GetClassName(const Descriptor* descriptor); // The fully-qualified name of the C# class that provides // access to the file descriptor. Proto compiler generates // such class for each .proto file processed. -string LIBPROTOC_EXPORT GetReflectionClassName(const FileDescriptor* descriptor); +string PROTOC_EXPORT GetReflectionClassName(const FileDescriptor* descriptor); // Generates output file name for given file descriptor. If generate_directories // is true, the output file will be put under directory corresponding to file's @@ -89,16 +91,16 @@ string LIBPROTOC_EXPORT GetReflectionClassName(const FileDescriptor* descriptor) // The file name to use as output file for given file descriptor. In case // of failure, this function will return empty string and error parameter // will contain the error message. -string LIBPROTOC_EXPORT GetOutputFile( - const google::protobuf::FileDescriptor* descriptor, - const string file_extension, - const bool generate_directories, - const string base_namespace, - string* error); +string PROTOC_EXPORT GetOutputFile(const FileDescriptor* descriptor, + const string file_extension, + const bool generate_directories, + const string base_namespace, string* error); } // namespace csharp } // namespace compiler } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_NAMES_H__ diff --git a/src/google/protobuf/compiler/csharp/csharp_options.h b/src/google/protobuf/compiler/csharp/csharp_options.h index 426fb3b50f..42ff6d8662 100644 --- a/src/google/protobuf/compiler/csharp/csharp_options.h +++ b/src/google/protobuf/compiler/csharp/csharp_options.h @@ -33,7 +33,6 @@ #include -#include namespace google { namespace protobuf { namespace compiler { @@ -45,10 +44,11 @@ struct Options { file_extension(".cs"), base_namespace(""), base_namespace_specified(false), - internal_access(false) { + internal_access(false), + serializable(false) { } // Extension of the generated file. Defaults to ".cs" - string file_extension; + std::string file_extension; // Base namespace to use to create directory hierarchy. Defaults to "". // This option allows the simple creation of a conventional C# file layout, // where directories are created relative to a project-specific base @@ -59,7 +59,7 @@ struct Options { // // If no base namespace is specified, all files are generated in the // --csharp_out directory, with no subdirectories created automatically. - string base_namespace; + std::string base_namespace; // Whether the base namespace has been explicitly specified by the user. // This is required as the base namespace can be explicitly set to the empty // string, meaning "create a full directory hierarchy, starting from the first @@ -68,12 +68,14 @@ struct Options { // Whether the generated classes should have accessibility level of "internal". // Defaults to false that generates "public" classes. bool internal_access; + // Whether the generated classes should have a global::System.Serializable attribute added + // Defaults to false + bool serializable; }; } // namespace csharp } // namespace compiler } // namespace protobuf - - } // namespace google + #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_OPTIONS_H__ diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc index c3003e3d8d..eb7f70dda6 100644 --- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -49,12 +48,12 @@ namespace compiler { namespace csharp { PrimitiveFieldGenerator::PrimitiveFieldGenerator( - const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options) - : FieldGeneratorBase(descriptor, fieldOrdinal, options) { + const FieldDescriptor* descriptor, int presenceIndex, const Options *options) + : FieldGeneratorBase(descriptor, presenceIndex, options) { // TODO(jonskeet): Make this cleaner... is_value_type = descriptor->type() != FieldDescriptor::TYPE_STRING && descriptor->type() != FieldDescriptor::TYPE_BYTES; - if (!is_value_type) { + if (!is_value_type && !IsProto2(descriptor_->file())) { variables_["has_property_check"] = variables_["property_name"] + ".Length != 0"; variables_["other_has_property_check"] = "other." + variables_["property_name"] + ".Length != 0"; } @@ -67,16 +66,44 @@ void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { // TODO(jonskeet): Work out whether we want to prevent the fields from ever being // null, or whether we just handle it, in the cases of bytes and string. // (Basically, should null-handling code be in the getter or the setter?) + if (IsProto2(descriptor_->file())) { + printer->Print( + variables_, + "private readonly static $type_name$ $property_name$DefaultValue = $default_value$;\n\n"); + } + printer->Print( variables_, "private $type_name$ $name_def_message$;\n"); + WritePropertyDocComment(printer, descriptor_); AddPublicMemberAttributes(printer); - printer->Print( - variables_, - "$access_level$ $type_name$ $property_name$ {\n" - " get { return $name$_; }\n" - " set {\n"); + if (IsProto2(descriptor_->file())) { + if (presenceIndex_ == -1) { + printer->Print( + variables_, + "$access_level$ $type_name$ $property_name$ {\n" + " get { return $name$_ ?? $property_name$DefaultValue; }\n" + " set {\n"); + } else { + printer->Print( + variables_, + "$access_level$ $type_name$ $property_name$ {\n" + " get { if ($has_field_check$) { return $name$_; } else { return $property_name$DefaultValue; } }\n" + " set {\n"); + } + } else { + printer->Print( + variables_, + "$access_level$ $type_name$ $property_name$ {\n" + " get { return $name$_; }\n" + " set {\n"); + } + if (presenceIndex_ != -1) { + printer->Print( + variables_, + " $set_has_field$;\n"); + } if (is_value_type) { printer->Print( variables_, @@ -89,6 +116,36 @@ void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { printer->Print( " }\n" "}\n"); + if (IsProto2(descriptor_->file())) { + printer->Print(variables_, "/// Gets whether the \"$descriptor_name$\" field is set\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ bool Has$property_name$ {\n" + " get { return "); + if (IsNullable(descriptor_)) { + printer->Print( + variables_, + "$name$_ != null; }\n}\n"); + } else { + printer->Print( + variables_, + "$has_field_check$; }\n}\n"); + } + } + if (IsProto2(descriptor_->file())) { + printer->Print(variables_, "/// Clears the value of the \"$descriptor_name$\" field\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ void Clear$property_name$() {\n"); + if (IsNullable(descriptor_)) { + printer->Print(variables_, " $name$_ = null;\n"); + } else { + printer->Print(variables_, " $clear_has_field$;\n"); + } + printer->Print("}\n"); + } } void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) { @@ -129,7 +186,7 @@ void PrimitiveFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { } else { printer->Print( "size += $tag_size$ + $fixed_size$;\n", - "fixed_size", SimpleItoa(fixedSize), + "fixed_size", StrCat(fixedSize), "tag_size", variables_["tag_size"]); } printer->Outdent(); @@ -168,12 +225,23 @@ void PrimitiveFieldGenerator::GenerateCloningCode(io::Printer* printer) { void PrimitiveFieldGenerator::GenerateCodecCode(io::Printer* printer) { printer->Print( variables_, - "pb::FieldCodec.For$capitalized_type_name$($tag$)"); + "pb::FieldCodec.For$capitalized_type_name$($tag$, $default_value$)"); +} + +void PrimitiveFieldGenerator::GenerateExtensionCode(io::Printer* printer) { + WritePropertyDocComment(printer, descriptor_); + AddDeprecatedFlag(printer); + printer->Print( + variables_, + "$access_level$ static readonly pb::Extension<$extended_type$, $type_name$> $property_name$ =\n" + " new pb::Extension<$extended_type$, $type_name$>($number$, "); + GenerateCodecCode(printer); + printer->Print(");\n"); } PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator( - const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options) - : PrimitiveFieldGenerator(descriptor, fieldOrdinal, options) { + const FieldDescriptor* descriptor, int presenceIndex, const Options *options) + : PrimitiveFieldGenerator(descriptor, presenceIndex, options) { SetCommonOneofFieldVariables(&variables_); } @@ -188,20 +256,42 @@ void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) { "$access_level$ $type_name$ $property_name$ {\n" " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n" " set {\n"); - if (is_value_type) { - printer->Print( - variables_, - " $oneof_name$_ = value;\n"); - } else { - printer->Print( - variables_, - " $oneof_name$_ = pb::ProtoPreconditions.CheckNotNull(value, \"value\");\n"); - } + if (is_value_type) { printer->Print( variables_, - " $oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n" + " $oneof_name$_ = value;\n"); + } else { + printer->Print( + variables_, + " $oneof_name$_ = pb::ProtoPreconditions.CheckNotNull(value, \"value\");\n"); + } + printer->Print( + variables_, + " $oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n" + " }\n" + "}\n"); + if (IsProto2(descriptor_->file())) { + printer->Print( + variables_, + "/// Gets whether the \"$descriptor_name$\" field is set\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ bool Has$property_name$ {\n" + " get { return $oneof_name$Case_ == $oneof_property_name$OneofCase.$property_name$; }\n" + "}\n"); + printer->Print( + variables_, + "/// Clears the value of the oneof if it's currently set to \"$descriptor_name$\" \n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ void Clear$property_name$() {\n" + " if ($has_property_check$) {\n" + " Clear$oneof_property_name$();\n" " }\n" "}\n"); + } } void PrimitiveOneofFieldGenerator::GenerateMergingCode(io::Printer* printer) { diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.h b/src/google/protobuf/compiler/csharp/csharp_primitive_field.h index ca7b8b3da6..5edcc42b7b 100644 --- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.h @@ -46,10 +46,13 @@ struct Options; class PrimitiveFieldGenerator : public FieldGeneratorBase { public: PrimitiveFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~PrimitiveFieldGenerator(); + PrimitiveFieldGenerator(const PrimitiveFieldGenerator&) = delete; + PrimitiveFieldGenerator& operator=(const PrimitiveFieldGenerator&) = delete; + virtual void GenerateCodecCode(io::Printer* printer); virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer); @@ -57,6 +60,7 @@ class PrimitiveFieldGenerator : public FieldGeneratorBase { virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); + virtual void GenerateExtensionCode(io::Printer* printer); virtual void WriteHash(io::Printer* printer); virtual void WriteEquals(io::Printer* printer); @@ -64,26 +68,24 @@ class PrimitiveFieldGenerator : public FieldGeneratorBase { protected: bool is_value_type; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator); }; class PrimitiveOneofFieldGenerator : public PrimitiveFieldGenerator { public: PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~PrimitiveOneofFieldGenerator(); + PrimitiveOneofFieldGenerator(const PrimitiveOneofFieldGenerator&) = delete; + PrimitiveOneofFieldGenerator& operator=(const PrimitiveOneofFieldGenerator&) = + delete; + virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMergingCode(io::Printer* printer); virtual void WriteToString(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveOneofFieldGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc index 5ddd616e10..38a36024f9 100644 --- a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc +++ b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -41,6 +40,7 @@ #include #include +#include #include #include #include @@ -57,6 +57,7 @@ ReflectionClassGenerator::ReflectionClassGenerator(const FileDescriptor* file, file_(file) { namespace_ = GetFileNamespace(file); reflectionClassname_ = GetReflectionClassUnqualifiedName(file); + extensionClassname_ = GetExtensionClassUnqualifiedName(file); } ReflectionClassGenerator::~ReflectionClassGenerator() { @@ -70,6 +71,25 @@ void ReflectionClassGenerator::Generate(io::Printer* printer) { printer->Outdent(); printer->Print("}\n"); + if (file_->extension_count() > 0) { + printer->Print( + "/// Holder for extension identifiers generated from the top level of $file_name$\n" + "internal static partial class $class_name$ {\n", + "access_level", class_access_level(), + "class_name", extensionClassname_, + "file_name", file_->name()); + printer->Indent(); + for (int i = 0; i < file_->extension_count(); i++) { + std::unique_ptr generator( + CreateFieldGenerator(file_->extension(i), -1, this->options())); + generator->GenerateExtensionCode(printer); + } + printer->Outdent(); + printer->Print( + "}\n" + "\n"); + } + // write children: Enums if (file_->enum_type_count() > 0) { printer->Print("#region Enums\n"); @@ -170,17 +190,10 @@ void ReflectionClassGenerator::WriteDescriptor(io::Printer* printer) { "descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,\n"); printer->Print(" new pbr::FileDescriptor[] { "); for (int i = 0; i < file_->dependency_count(); i++) { - // descriptor.proto is special: we don't allow access to the generated code, but there's - // a separately-exposed property to get at the file descriptor, specifically to allow this - // kind of dependency. - if (IsDescriptorProto(file_->dependency(i))) { - printer->Print("pbr::FileDescriptor.DescriptorProtoFileDescriptor, "); - } else { printer->Print( "$full_reflection_class_name$.Descriptor, ", "full_reflection_class_name", GetReflectionClassName(file_->dependency(i))); - } } printer->Print("},\n" " new pbr::GeneratedClrTypeInfo("); @@ -194,6 +207,16 @@ void ReflectionClassGenerator::WriteDescriptor(io::Printer* printer) { } else { printer->Print("null, "); + } + if (file_->extension_count() > 0) { + std::vector extensions; + for (int i = 0; i < file_->extension_count(); i++) { + extensions.push_back(GetFullExtensionName(file_->extension(i))); + } + printer->Print("new pb::Extension[] { $extensions$ }, ", "extensions", JoinStrings(extensions, ", ")); + } + else { + printer->Print("null, "); } if (file_->message_type_count() > 0) { printer->Print("new pbr::GeneratedClrTypeInfo[] {\n"); @@ -234,7 +257,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript } // Generated message type printer->Print("new pbr::GeneratedClrTypeInfo(typeof($type_name$), $type_name$.Parser, ", "type_name", GetClassName(descriptor)); - + // Fields if (descriptor->field_count() > 0) { std::vector fields; @@ -271,9 +294,21 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript printer->Print("null, "); } + // Extensions + if (descriptor->extension_count() > 0) { + std::vector extensions; + for (int i = 0; i < descriptor->extension_count(); i++) { + extensions.push_back(GetFullExtensionName(descriptor->extension(i))); + } + printer->Print("new pb::Extension[] { $extensions$ }, ", "extensions", JoinStrings(extensions, ", ")); + } + else { + printer->Print("null, "); + } + // Nested types if (descriptor->nested_type_count() > 0) { - // Need to specify array type explicitly here, as all elements may be null. + // Need to specify array type explicitly here, as all elements may be null. printer->Print("new pbr::GeneratedClrTypeInfo[] { "); for (int i = 0; i < descriptor->nested_type_count(); i++) { WriteGeneratedCodeInfo(descriptor->nested_type(i), printer, i == descriptor->nested_type_count() - 1); diff --git a/src/google/protobuf/compiler/csharp/csharp_reflection_class.h b/src/google/protobuf/compiler/csharp/csharp_reflection_class.h index 3291d65b06..9554727766 100644 --- a/src/google/protobuf/compiler/csharp/csharp_reflection_class.h +++ b/src/google/protobuf/compiler/csharp/csharp_reflection_class.h @@ -48,6 +48,9 @@ class ReflectionClassGenerator : public SourceGeneratorBase { ReflectionClassGenerator(const FileDescriptor* file, const Options* options); ~ReflectionClassGenerator(); + ReflectionClassGenerator(const ReflectionClassGenerator&) = delete; + ReflectionClassGenerator& operator=(const ReflectionClassGenerator&) = delete; + void Generate(io::Printer* printer); private: @@ -55,14 +58,13 @@ class ReflectionClassGenerator : public SourceGeneratorBase { std::string namespace_; std::string reflectionClassname_; + std::string extensionClassname_; void WriteIntroduction(io::Printer* printer); void WriteDescriptor(io::Printer* printer); void WriteGeneratedCodeInfo(const Descriptor* descriptor, io::Printer* printer, bool last); - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionClassGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc index 683c4b0b7a..73309a7edd 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -48,8 +47,8 @@ namespace compiler { namespace csharp { RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator( - const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options) - : FieldGeneratorBase(descriptor, fieldOrdinal, options) { + const FieldDescriptor* descriptor, int presenceIndex, const Options *options) + : FieldGeneratorBase(descriptor, presenceIndex, options) { } RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() { @@ -90,7 +89,7 @@ void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) "$name$_.WriteTo(output, _repeated_$name$_codec);\n"); } -void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { +void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { printer->Print( variables_, "size += $name$_.CalculateSize(_repeated_$name$_codec);\n"); @@ -118,6 +117,16 @@ void RepeatedEnumFieldGenerator::GenerateCloningCode(io::Printer* printer) { "$name$_ = other.$name$_.Clone();\n"); } +void RepeatedEnumFieldGenerator::GenerateExtensionCode(io::Printer* printer) { + WritePropertyDocComment(printer, descriptor_); + AddDeprecatedFlag(printer); + printer->Print( + variables_, + "$access_level$ static readonly pb::RepeatedExtension<$extended_type$, $type_name$> $property_name$ =\n" + " new pb::RepeatedExtension<$extended_type$, $type_name$>($number$, " + "pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x));\n"); +} + void RepeatedEnumFieldGenerator::GenerateFreezingCode(io::Printer* printer) { } diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.h b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.h index 819b583262..c7a632a17d 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.h @@ -41,15 +41,19 @@ namespace protobuf { namespace compiler { namespace csharp { -// TODO(jonskeet): Refactor repeated field support; all the implementations are *really* similar. We -// should probably have a RepeatedFieldGeneratorBase. +// TODO(jonskeet): Refactor repeated field support; all the implementations are +// *really* similar. We should probably have a RepeatedFieldGeneratorBase. class RepeatedEnumFieldGenerator : public FieldGeneratorBase { public: RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~RepeatedEnumFieldGenerator(); + RepeatedEnumFieldGenerator(const RepeatedEnumFieldGenerator&) = delete; + RepeatedEnumFieldGenerator& operator=(const RepeatedEnumFieldGenerator&) = + delete; + virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateFreezingCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer); @@ -57,13 +61,11 @@ class RepeatedEnumFieldGenerator : public FieldGeneratorBase { virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); + virtual void GenerateExtensionCode(io::Printer* printer); virtual void WriteHash(io::Printer* printer); virtual void WriteEquals(io::Printer* printer); virtual void WriteToString(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedEnumFieldGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc index 90af569c47..4b4b37de1b 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -49,8 +48,8 @@ namespace compiler { namespace csharp { RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator( - const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options) - : FieldGeneratorBase(descriptor, fieldOrdinal, options) { + const FieldDescriptor* descriptor, int presenceIndex, const Options *options) + : FieldGeneratorBase(descriptor, presenceIndex, options) { } RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() { @@ -67,11 +66,11 @@ void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) { // function, but it doesn't seem worth it for just this. if (IsWrapperType(descriptor_)) { std::unique_ptr single_generator( - new WrapperFieldGenerator(descriptor_, fieldOrdinal_, this->options())); + new WrapperFieldGenerator(descriptor_, presenceIndex_, this->options())); single_generator->GenerateCodecCode(printer); } else { std::unique_ptr single_generator( - new MessageFieldGenerator(descriptor_, fieldOrdinal_, this->options())); + new MessageFieldGenerator(descriptor_, presenceIndex_, this->options())); single_generator->GenerateCodecCode(printer); } printer->Print(";\n"); @@ -138,6 +137,25 @@ void RepeatedMessageFieldGenerator::GenerateCloningCode(io::Printer* printer) { void RepeatedMessageFieldGenerator::GenerateFreezingCode(io::Printer* printer) { } +void RepeatedMessageFieldGenerator::GenerateExtensionCode(io::Printer* printer) { + WritePropertyDocComment(printer, descriptor_); + AddDeprecatedFlag(printer); + printer->Print( + variables_, + "$access_level$ static readonly pb::RepeatedExtension<$extended_type$, $type_name$> $property_name$ =\n" + " new pb::RepeatedExtension<$extended_type$, $type_name$>($number$, "); + if (IsWrapperType(descriptor_)) { + std::unique_ptr single_generator( + new WrapperFieldGenerator(descriptor_, -1, this->options())); + single_generator->GenerateCodecCode(printer); + } else { + std::unique_ptr single_generator( + new MessageFieldGenerator(descriptor_, -1, this->options())); + single_generator->GenerateCodecCode(printer); + } + printer->Print(");\n"); +} + } // namespace csharp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.h b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.h index 6e33648b6c..74f6874df2 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.h @@ -46,10 +46,14 @@ struct Options; class RepeatedMessageFieldGenerator : public FieldGeneratorBase { public: RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~RepeatedMessageFieldGenerator(); + RepeatedMessageFieldGenerator(const RepeatedMessageFieldGenerator&) = delete; + RepeatedMessageFieldGenerator& operator=( + const RepeatedMessageFieldGenerator&) = delete; + virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateFreezingCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer); @@ -57,13 +61,11 @@ class RepeatedMessageFieldGenerator : public FieldGeneratorBase { virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); + virtual void GenerateExtensionCode(io::Printer* printer); virtual void WriteHash(io::Printer* printer); virtual void WriteEquals(io::Printer* printer); virtual void WriteToString(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedMessageFieldGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc index cd91506ff1..c1444ea124 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -48,8 +47,8 @@ namespace compiler { namespace csharp { RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator( - const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options) - : FieldGeneratorBase(descriptor, fieldOrdinal, options) { + const FieldDescriptor* descriptor, int presenceIndex, const Options *options) + : FieldGeneratorBase(descriptor, presenceIndex, options) { } RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() { @@ -119,6 +118,15 @@ void RepeatedPrimitiveFieldGenerator::GenerateCloningCode(io::Printer* printer) void RepeatedPrimitiveFieldGenerator::GenerateFreezingCode(io::Printer* printer) { } +void RepeatedPrimitiveFieldGenerator::GenerateExtensionCode(io::Printer* printer) { + WritePropertyDocComment(printer, descriptor_); + AddDeprecatedFlag(printer); + printer->Print( + variables_, + "$access_level$ static readonly pb::RepeatedExtension<$extended_type$, $type_name$> $property_name$ =\n" + " new pb::RepeatedExtension<$extended_type$, $type_name$>($number$, pb::FieldCodec.For$capitalized_type_name$($tag$));\n"); +} + } // namespace csharp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h index a59348a95f..2a3be48168 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h @@ -43,9 +43,13 @@ namespace csharp { class RepeatedPrimitiveFieldGenerator : public FieldGeneratorBase { public: - RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options); + RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, + int presenceIndex, const Options* options); ~RepeatedPrimitiveFieldGenerator(); + RepeatedPrimitiveFieldGenerator(const RepeatedPrimitiveFieldGenerator&) = delete; + RepeatedPrimitiveFieldGenerator& operator=(const RepeatedPrimitiveFieldGenerator&) = delete; + virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateFreezingCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer); @@ -53,13 +57,11 @@ class RepeatedPrimitiveFieldGenerator : public FieldGeneratorBase { virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); + virtual void GenerateExtensionCode(io::Printer* printer); virtual void WriteHash(io::Printer* printer); virtual void WriteEquals(io::Printer* printer); virtual void WriteToString(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPrimitiveFieldGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc b/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc index 1fda7ddf31..68209cc825 100644 --- a/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc +++ b/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include diff --git a/src/google/protobuf/compiler/csharp/csharp_source_generator_base.h b/src/google/protobuf/compiler/csharp/csharp_source_generator_base.h index 790fb1be77..695c4225d3 100644 --- a/src/google/protobuf/compiler/csharp/csharp_source_generator_base.h +++ b/src/google/protobuf/compiler/csharp/csharp_source_generator_base.h @@ -48,6 +48,9 @@ class SourceGeneratorBase { SourceGeneratorBase(const FileDescriptor* descriptor, const Options* options); virtual ~SourceGeneratorBase(); + SourceGeneratorBase(const SourceGeneratorBase&) = delete; + SourceGeneratorBase& operator=(const SourceGeneratorBase&) = delete; + std::string class_access_level(); const Options* options(); @@ -58,8 +61,6 @@ class SourceGeneratorBase { private: const FileDescriptor* descriptor_; const Options *options_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceGeneratorBase); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc b/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc index 047edf7345..add20ab9fe 100644 --- a/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -48,8 +47,8 @@ namespace compiler { namespace csharp { WrapperFieldGenerator::WrapperFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, const Options *options) - : FieldGeneratorBase(descriptor, fieldOrdinal, options) { + int presenceIndex, const Options *options) + : FieldGeneratorBase(descriptor, presenceIndex, options) { variables_["has_property_check"] = name() + "_ != null"; variables_["has_not_property_check"] = name() + "_ == null"; const FieldDescriptor* wrapped_field = descriptor->message_type()->field(0); @@ -81,7 +80,27 @@ void WrapperFieldGenerator::GenerateMembers(io::Printer* printer) { " set {\n" " $name$_ = value;\n" " }\n" - "}\n"); + "}\n\n"); + if (IsProto2(descriptor_->file())) { + printer->Print( + variables_, + "/// Gets whether the $descriptor_name$ field is set\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ bool Has$property_name$ {\n" + " get { return $name$_ != null; }\n" + "}\n\n"); + printer->Print( + variables_, + "/// Clears the value of the $descriptor_name$ field\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ void Clear$property_name$() {\n" + " $name$_ = null;\n" + "}\n"); + } } void WrapperFieldGenerator::GenerateMergingCode(io::Printer* printer) { @@ -162,9 +181,20 @@ void WrapperFieldGenerator::GenerateCodecCode(io::Printer* printer) { } } +void WrapperFieldGenerator::GenerateExtensionCode(io::Printer* printer) { + WritePropertyDocComment(printer, descriptor_); + AddDeprecatedFlag(printer); + printer->Print( + variables_, + "$access_level$ static readonly pb::Extension<$extended_type$, $type_name$> $property_name$ =\n" + " new pb::Extension<$extended_type$, $type_name$>($number$, "); + GenerateCodecCode(printer); + printer->Print(");\n"); +} + WrapperOneofFieldGenerator::WrapperOneofFieldGenerator( - const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options) - : WrapperFieldGenerator(descriptor, fieldOrdinal, options) { + const FieldDescriptor* descriptor, int presenceIndex, const Options *options) + : WrapperFieldGenerator(descriptor, presenceIndex, options) { SetCommonOneofFieldVariables(&variables_); } @@ -189,6 +219,28 @@ void WrapperOneofFieldGenerator::GenerateMembers(io::Printer* printer) { " $oneof_name$Case_ = value == null ? $oneof_property_name$OneofCase.None : $oneof_property_name$OneofCase.$property_name$;\n" " }\n" "}\n"); + if (IsProto2(descriptor_->file())) { + printer->Print( + variables_, + "/// Gets whether the \"$descriptor_name$\" field is set\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ bool Has$property_name$ {\n" + " get { return $oneof_name$Case_ == $oneof_property_name$OneofCase.$property_name$; }\n" + "}\n"); + printer->Print( + variables_, + "/// Clears the value of the oneof if it's currently set to \"$descriptor_name$\" \n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ void Clear$property_name$() {\n" + " if ($has_property_check$) {\n" + " Clear$oneof_property_name$();\n" + " }\n" + "}\n"); + } } void WrapperOneofFieldGenerator::GenerateMergingCode(io::Printer* printer) { diff --git a/src/google/protobuf/compiler/csharp/csharp_wrapper_field.h b/src/google/protobuf/compiler/csharp/csharp_wrapper_field.h index 452531fbfa..394e27de59 100644 --- a/src/google/protobuf/compiler/csharp/csharp_wrapper_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_wrapper_field.h @@ -46,10 +46,13 @@ struct Options; class WrapperFieldGenerator : public FieldGeneratorBase { public: WrapperFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~WrapperFieldGenerator(); + WrapperFieldGenerator(const WrapperFieldGenerator&) = delete; + WrapperFieldGenerator& operator=(const WrapperFieldGenerator&) = delete; + virtual void GenerateCodecCode(io::Printer* printer); virtual void GenerateCloningCode(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer); @@ -57,6 +60,7 @@ class WrapperFieldGenerator : public FieldGeneratorBase { virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); + virtual void GenerateExtensionCode(io::Printer* printer); virtual void WriteHash(io::Printer* printer); virtual void WriteEquals(io::Printer* printer); @@ -64,24 +68,23 @@ class WrapperFieldGenerator : public FieldGeneratorBase { private: bool is_value_type; // True for int32 etc; false for bytes and string - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WrapperFieldGenerator); }; class WrapperOneofFieldGenerator : public WrapperFieldGenerator { public: WrapperOneofFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options); ~WrapperOneofFieldGenerator(); + WrapperOneofFieldGenerator(const WrapperOneofFieldGenerator&) = delete; + WrapperOneofFieldGenerator& operator=(const WrapperOneofFieldGenerator&) = delete; + virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMergingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WrapperOneofFieldGenerator); }; } // namespace csharp diff --git a/src/google/protobuf/compiler/importer.cc b/src/google/protobuf/compiler/importer.cc index 58498aaa34..715d999e7e 100644 --- a/src/google/protobuf/compiler/importer.cc +++ b/src/google/protobuf/compiler/importer.cc @@ -37,10 +37,10 @@ #else #include #endif -#include -#include -#include #include +#include +#include +#include #include #include @@ -48,12 +48,12 @@ #include #include +#include #include #include #include -#include #ifdef _WIN32 #include @@ -66,19 +66,17 @@ namespace compiler { #ifdef _WIN32 // DO NOT include , instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::open; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::open; #endif // Returns true if the text looks like a Windows-style absolute path, starting // with a drive letter. Example: "C:\foo". TODO(kenton): Share this with // copy in command_line_interface.cc? -static bool IsWindowsAbsolutePath(const string& text) { +static bool IsWindowsAbsolutePath(const std::string& text) { #if defined(_WIN32) || defined(__CYGWIN__) - return text.size() >= 3 && text[1] == ':' && - isalpha(text[0]) && - (text[2] == '/' || text[2] == '\\') && - text.find_last_of(':') == 1; + return text.size() >= 3 && text[1] == ':' && isalpha(text[0]) && + (text[2] == '/' || text[2] == '\\') && text.find_last_of(':') == 1; #else return false; #endif @@ -93,17 +91,17 @@ MultiFileErrorCollector::~MultiFileErrorCollector() {} class SourceTreeDescriptorDatabase::SingleFileErrorCollector : public io::ErrorCollector { public: - SingleFileErrorCollector(const string& filename, + SingleFileErrorCollector(const std::string& filename, MultiFileErrorCollector* multi_file_error_collector) - : filename_(filename), - multi_file_error_collector_(multi_file_error_collector), - had_errors_(false) {} + : filename_(filename), + multi_file_error_collector_(multi_file_error_collector), + had_errors_(false) {} ~SingleFileErrorCollector() {} bool had_errors() { return had_errors_; } // implements ErrorCollector --------------------------------------- - void AddError(int line, int column, const string& message) override { + void AddError(int line, int column, const std::string& message) override { if (multi_file_error_collector_ != NULL) { multi_file_error_collector_->AddError(filename_, line, column, message); } @@ -111,7 +109,7 @@ class SourceTreeDescriptorDatabase::SingleFileErrorCollector } private: - string filename_; + std::string filename_; MultiFileErrorCollector* multi_file_error_collector_; bool had_errors_; }; @@ -120,17 +118,30 @@ class SourceTreeDescriptorDatabase::SingleFileErrorCollector SourceTreeDescriptorDatabase::SourceTreeDescriptorDatabase( SourceTree* source_tree) - : source_tree_(source_tree), - error_collector_(NULL), - using_validation_error_collector_(false), - validation_error_collector_(this) {} + : source_tree_(source_tree), + fallback_database_(nullptr), + error_collector_(nullptr), + using_validation_error_collector_(false), + validation_error_collector_(this) {} + +SourceTreeDescriptorDatabase::SourceTreeDescriptorDatabase( + SourceTree* source_tree, DescriptorDatabase* fallback_database) + : source_tree_(source_tree), + fallback_database_(fallback_database), + error_collector_(nullptr), + using_validation_error_collector_(false), + validation_error_collector_(this) {} SourceTreeDescriptorDatabase::~SourceTreeDescriptorDatabase() {} -bool SourceTreeDescriptorDatabase::FindFileByName( - const string& filename, FileDescriptorProto* output) { +bool SourceTreeDescriptorDatabase::FindFileByName(const std::string& filename, + FileDescriptorProto* output) { std::unique_ptr input(source_tree_->Open(filename)); if (input == NULL) { + if (fallback_database_ != nullptr && + fallback_database_->FindFileByName(filename, output)) { + return true; + } if (error_collector_ != NULL) { error_collector_->AddError(filename, -1, 0, source_tree_->GetLastErrorMessage()); @@ -152,17 +163,16 @@ bool SourceTreeDescriptorDatabase::FindFileByName( // Parse it. output->set_name(filename); - return parser.Parse(&tokenizer, output) && - !file_error_collector.had_errors(); + return parser.Parse(&tokenizer, output) && !file_error_collector.had_errors(); } bool SourceTreeDescriptorDatabase::FindFileContainingSymbol( - const string& symbol_name, FileDescriptorProto* output) { + const std::string& symbol_name, FileDescriptorProto* output) { return false; } bool SourceTreeDescriptorDatabase::FindFileContainingExtension( - const string& containing_type, int field_number, + const std::string& containing_type, int field_number, FileDescriptorProto* output) { return false; } @@ -170,35 +180,41 @@ bool SourceTreeDescriptorDatabase::FindFileContainingExtension( // ------------------------------------------------------------------- SourceTreeDescriptorDatabase::ValidationErrorCollector:: -ValidationErrorCollector(SourceTreeDescriptorDatabase* owner) - : owner_(owner) {} + ValidationErrorCollector(SourceTreeDescriptorDatabase* owner) + : owner_(owner) {} SourceTreeDescriptorDatabase::ValidationErrorCollector:: -~ValidationErrorCollector() {} + ~ValidationErrorCollector() {} void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddError( - const string& filename, - const string& element_name, - const Message* descriptor, - ErrorLocation location, - const string& message) { + const std::string& filename, const std::string& element_name, + const Message* descriptor, ErrorLocation location, + const std::string& message) { if (owner_->error_collector_ == NULL) return; int line, column; - owner_->source_locations_.Find(descriptor, location, &line, &column); + if (location == DescriptorPool::ErrorCollector::IMPORT) { + owner_->source_locations_.FindImport(descriptor, element_name, &line, + &column); + } else { + owner_->source_locations_.Find(descriptor, location, &line, &column); + } owner_->error_collector_->AddError(filename, line, column, message); } void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddWarning( - const string& filename, - const string& element_name, - const Message* descriptor, - ErrorLocation location, - const string& message) { + const std::string& filename, const std::string& element_name, + const Message* descriptor, ErrorLocation location, + const std::string& message) { if (owner_->error_collector_ == NULL) return; int line, column; - owner_->source_locations_.Find(descriptor, location, &line, &column); + if (location == DescriptorPool::ErrorCollector::IMPORT) { + owner_->source_locations_.FindImport(descriptor, element_name, &line, + &column); + } else { + owner_->source_locations_.Find(descriptor, location, &line, &column); + } owner_->error_collector_->AddWarning(filename, line, column, message); } @@ -206,19 +222,19 @@ void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddWarning( Importer::Importer(SourceTree* source_tree, MultiFileErrorCollector* error_collector) - : database_(source_tree), - pool_(&database_, database_.GetValidationErrorCollector()) { + : database_(source_tree), + pool_(&database_, database_.GetValidationErrorCollector()) { pool_.EnforceWeakDependencies(true); database_.RecordErrorsTo(error_collector); } Importer::~Importer() {} -const FileDescriptor* Importer::Import(const string& filename) { +const FileDescriptor* Importer::Import(const std::string& filename) { return pool_.FindFileByName(filename); } -void Importer::AddUnusedImportTrackFile(const string& file_name) { +void Importer::AddUnusedImportTrackFile(const std::string& file_name) { pool_.AddUnusedImportTrackFile(file_name); } @@ -231,15 +247,13 @@ void Importer::ClearUnusedImportTrackFiles() { SourceTree::~SourceTree() {} -string SourceTree::GetLastErrorMessage() { - return "File not found."; -} +std::string SourceTree::GetLastErrorMessage() { return "File not found."; } DiskSourceTree::DiskSourceTree() {} DiskSourceTree::~DiskSourceTree() {} -static inline char LastChar(const string& str) { +static inline char LastChar(const std::string& str) { return str[str.size() - 1]; } @@ -262,7 +276,7 @@ static inline char LastChar(const string& str) { // then if foo/bar is a symbolic link, foo/bar/baz.proto will canonicalize // to a path which does not appear to be under foo, and thus the compiler // will complain that baz.proto is not inside the --proto_path. -static string CanonicalizePath(string path) { +static std::string CanonicalizePath(std::string path) { #ifdef _WIN32 // The Win32 API accepts forward slashes as a path delimiter even though // backslashes are standard. Let's avoid confusion and use only forward @@ -275,8 +289,8 @@ static string CanonicalizePath(string path) { } #endif - std::vector canonical_parts; - std::vector parts = Split( + std::vector canonical_parts; + std::vector parts = Split( path, "/", true); // Note: Removes empty parts. for (int i = 0; i < parts.size(); i++) { if (parts[i] == ".") { @@ -285,20 +299,20 @@ static string CanonicalizePath(string path) { canonical_parts.push_back(parts[i]); } } - string result = Join(canonical_parts, "/"); + std::string result = Join(canonical_parts, "/"); if (!path.empty() && path[0] == '/') { // Restore leading slash. result = '/' + result; } - if (!path.empty() && LastChar(path) == '/' && - !result.empty() && LastChar(result) != '/') { + if (!path.empty() && LastChar(path) == '/' && !result.empty() && + LastChar(result) != '/') { // Restore trailing slash. result += '/'; } return result; } -static inline bool ContainsParentReference(const string& path) { +static inline bool ContainsParentReference(const std::string& path) { return path == ".." || HasPrefixString(path, "../") || HasSuffixString(path, "/..") || path.find("/../") != string::npos; } @@ -320,10 +334,9 @@ static inline bool ContainsParentReference(const string& path) { // assert(!ApplyMapping("foo/bar", "baz", "qux", &result)); // assert(!ApplyMapping("foo/bar", "baz", "qux", &result)); // assert(!ApplyMapping("foobar", "foo", "baz", &result)); -static bool ApplyMapping(const string& filename, - const string& old_prefix, - const string& new_prefix, - string* result) { +static bool ApplyMapping(const std::string& filename, + const std::string& old_prefix, + const std::string& new_prefix, std::string* result) { if (old_prefix.empty()) { // old_prefix matches any relative path. if (ContainsParentReference(filename)) { @@ -359,7 +372,7 @@ static bool ApplyMapping(const string& filename, if (after_prefix_start != -1) { // Yep. So the prefixes are directories and the filename is a file // inside them. - string after_prefix = filename.substr(after_prefix_start); + std::string after_prefix = filename.substr(after_prefix_start); if (ContainsParentReference(after_prefix)) { // We do not allow the file name to use "..". return false; @@ -375,18 +388,17 @@ static bool ApplyMapping(const string& filename, return false; } -void DiskSourceTree::MapPath(const string& virtual_path, - const string& disk_path) { +void DiskSourceTree::MapPath(const std::string& virtual_path, + const std::string& disk_path) { mappings_.push_back(Mapping(virtual_path, CanonicalizePath(disk_path))); } DiskSourceTree::DiskFileToVirtualFileResult -DiskSourceTree::DiskFileToVirtualFile( - const string& disk_file, - string* virtual_file, - string* shadowing_disk_file) { +DiskSourceTree::DiskFileToVirtualFile(const std::string& disk_file, + std::string* virtual_file, + std::string* shadowing_disk_file) { int mapping_index = -1; - string canonical_disk_file = CanonicalizePath(disk_file); + std::string canonical_disk_file = CanonicalizePath(disk_file); for (int i = 0; i < mappings_.size(); i++) { // Apply the mapping in reverse. @@ -426,36 +438,36 @@ DiskSourceTree::DiskFileToVirtualFile( return SUCCESS; } -bool DiskSourceTree::VirtualFileToDiskFile(const string& virtual_file, - string* disk_file) { +bool DiskSourceTree::VirtualFileToDiskFile(const std::string& virtual_file, + std::string* disk_file) { std::unique_ptr stream( OpenVirtualFile(virtual_file, disk_file)); return stream != NULL; } -io::ZeroCopyInputStream* DiskSourceTree::Open(const string& filename) { +io::ZeroCopyInputStream* DiskSourceTree::Open(const std::string& filename) { return OpenVirtualFile(filename, NULL); } -string DiskSourceTree::GetLastErrorMessage() { +std::string DiskSourceTree::GetLastErrorMessage() { return last_error_message_; } io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile( - const string& virtual_file, - string* disk_file) { + const std::string& virtual_file, std::string* disk_file) { if (virtual_file != CanonicalizePath(virtual_file) || ContainsParentReference(virtual_file)) { // We do not allow importing of paths containing things like ".." or // consecutive slashes since the compiler expects files to be uniquely // identified by file name. - last_error_message_ = "Backslashes, consecutive slashes, \".\", or \"..\" " - "are not allowed in the virtual path"; + last_error_message_ = + "Backslashes, consecutive slashes, \".\", or \"..\" " + "are not allowed in the virtual path"; return NULL; } for (int i = 0; i < mappings_.size(); i++) { - string temp_disk_file; + std::string temp_disk_file; if (ApplyMapping(virtual_file, mappings_[i].virtual_path, mappings_[i].disk_path, &temp_disk_file)) { io::ZeroCopyInputStream* stream = OpenDiskFile(temp_disk_file); @@ -468,8 +480,8 @@ io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile( if (errno == EACCES) { // The file exists but is not readable. - last_error_message_ = "Read access is denied for file: " + - temp_disk_file; + last_error_message_ = + "Read access is denied for file: " + temp_disk_file; return NULL; } } @@ -479,7 +491,7 @@ io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile( } io::ZeroCopyInputStream* DiskSourceTree::OpenDiskFile( - const string& filename) { + const std::string& filename) { int file_descriptor; do { file_descriptor = open(filename.c_str(), O_RDONLY); diff --git a/src/google/protobuf/compiler/importer.h b/src/google/protobuf/compiler/importer.h index 5f27907845..1a0b47ae2f 100644 --- a/src/google/protobuf/compiler/importer.h +++ b/src/google/protobuf/compiler/importer.h @@ -37,18 +37,22 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__ #define GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__ -#include -#include #include +#include #include +#include +#include #include #include -#include + +#include namespace google { namespace protobuf { -namespace io { class ZeroCopyInputStream; } +namespace io { +class ZeroCopyInputStream; +} namespace compiler { @@ -73,9 +77,14 @@ class DiskSourceTree; // // Note: This class does not implement FindFileContainingSymbol() or // FindFileContainingExtension(); these will always return false. -class LIBPROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabase { +class PROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabase { public: SourceTreeDescriptorDatabase(SourceTree* source_tree); + + // If non-NULL, fallback_database will be checked if a file doesn't exist in + // the specified source_tree. + SourceTreeDescriptorDatabase(SourceTree* source_tree, + DescriptorDatabase* fallback_database); ~SourceTreeDescriptorDatabase(); // Instructs the SourceTreeDescriptorDatabase to report any parse errors @@ -96,11 +105,11 @@ class LIBPROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabas } // implements DescriptorDatabase ----------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) override; - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) override; - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) override; @@ -108,21 +117,24 @@ class LIBPROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabas class SingleFileErrorCollector; SourceTree* source_tree_; + DescriptorDatabase* fallback_database_; MultiFileErrorCollector* error_collector_; - class LIBPROTOBUF_EXPORT ValidationErrorCollector : public DescriptorPool::ErrorCollector { + class PROTOBUF_EXPORT ValidationErrorCollector + : public DescriptorPool::ErrorCollector { public: ValidationErrorCollector(SourceTreeDescriptorDatabase* owner); ~ValidationErrorCollector(); // implements ErrorCollector --------------------------------------- - void AddError(const string& filename, const string& element_name, + void AddError(const std::string& filename, const std::string& element_name, const Message* descriptor, ErrorLocation location, - const string& message) override; + const std::string& message) override; - void AddWarning(const string& filename, const string& element_name, - const Message* descriptor, ErrorLocation location, - const string& message) override; + void AddWarning(const std::string& filename, + const std::string& element_name, const Message* descriptor, + ErrorLocation location, + const std::string& message) override; private: SourceTreeDescriptorDatabase* owner_; @@ -142,10 +154,9 @@ class LIBPROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabas // You may find that SourceTreeDescriptorDatabase is more flexible. // // TODO(kenton): I feel like this class is not well-named. -class LIBPROTOBUF_EXPORT Importer { +class PROTOBUF_EXPORT Importer { public: - Importer(SourceTree* source_tree, - MultiFileErrorCollector* error_collector); + Importer(SourceTree* source_tree, MultiFileErrorCollector* error_collector); ~Importer(); // Import the given file and build a FileDescriptor representing it. If @@ -161,15 +172,13 @@ class LIBPROTOBUF_EXPORT Importer { // you want to see errors for the same files repeatedly, you can use a // separate Importer object to import each one (but use the same // DescriptorPool so that they can be cross-linked). - const FileDescriptor* Import(const string& filename); + const FileDescriptor* Import(const std::string& filename); // The DescriptorPool in which all imported FileDescriptors and their // contents are stored. - inline const DescriptorPool* pool() const { - return &pool_; - } + inline const DescriptorPool* pool() const { return &pool_; } - void AddUnusedImportTrackFile(const string& file_name); + void AddUnusedImportTrackFile(const std::string& file_name); void ClearUnusedImportTrackFiles(); @@ -182,18 +191,18 @@ class LIBPROTOBUF_EXPORT Importer { // If the importer encounters problems while trying to import the proto files, // it reports them to a MultiFileErrorCollector. -class LIBPROTOBUF_EXPORT MultiFileErrorCollector { +class PROTOBUF_EXPORT MultiFileErrorCollector { public: inline MultiFileErrorCollector() {} virtual ~MultiFileErrorCollector(); // Line and column numbers are zero-based. A line number of -1 indicates // an error with the entire file (e.g. "not found"). - virtual void AddError(const string& filename, int line, int column, - const string& message) = 0; + virtual void AddError(const std::string& filename, int line, int column, + const std::string& message) = 0; - virtual void AddWarning(const string& filename, int line, int column, - const string& message) {} + virtual void AddWarning(const std::string& filename, int line, int column, + const std::string& message) {} private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MultiFileErrorCollector); @@ -203,7 +212,7 @@ class LIBPROTOBUF_EXPORT MultiFileErrorCollector { // Used by the default implementation of Importer to resolve import statements // Most users will probably want to use the DiskSourceTree implementation, // below. -class LIBPROTOBUF_EXPORT SourceTree { +class PROTOBUF_EXPORT SourceTree { public: inline SourceTree() {} virtual ~SourceTree(); @@ -212,14 +221,14 @@ class LIBPROTOBUF_EXPORT SourceTree { // found. The caller takes ownership of the returned object. The filename // must be a path relative to the root of the source tree and must not // contain "." or ".." components. - virtual io::ZeroCopyInputStream* Open(const string& filename) = 0; + virtual io::ZeroCopyInputStream* Open(const std::string& filename) = 0; // If Open() returns NULL, calling this method immediately will return an // description of the error. // Subclasses should implement this method and return a meaningful value for // better error reporting. // TODO(xiaofeng): change this to a pure virtual function. - virtual string GetLastErrorMessage(); + virtual std::string GetLastErrorMessage(); private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceTree); @@ -228,7 +237,7 @@ class LIBPROTOBUF_EXPORT SourceTree { // An implementation of SourceTree which loads files from locations on disk. // Multiple mappings can be set up to map locations in the DiskSourceTree to // locations in the physical filesystem. -class LIBPROTOBUF_EXPORT DiskSourceTree : public SourceTree { +class PROTOBUF_EXPORT DiskSourceTree : public SourceTree { public: DiskSourceTree(); ~DiskSourceTree(); @@ -249,7 +258,7 @@ class LIBPROTOBUF_EXPORT DiskSourceTree : public SourceTree { // // disk_path may be an absolute path or relative to the current directory, // just like a path you'd pass to open(). - void MapPath(const string& virtual_path, const string& disk_path); + void MapPath(const std::string& virtual_path, const std::string& disk_path); // Return type for DiskFileToVirtualFile(). enum DiskFileToVirtualFileResult { @@ -279,40 +288,40 @@ class LIBPROTOBUF_EXPORT DiskSourceTree : public SourceTree { // it is not useful. // * NO_MAPPING: Indicates that no mapping was found which contains this // file. - DiskFileToVirtualFileResult - DiskFileToVirtualFile(const string& disk_file, - string* virtual_file, - string* shadowing_disk_file); + DiskFileToVirtualFileResult DiskFileToVirtualFile( + const std::string& disk_file, std::string* virtual_file, + std::string* shadowing_disk_file); // Given a virtual path, find the path to the file on disk. // Return true and update disk_file with the on-disk path if the file exists. // Return false and leave disk_file untouched if the file doesn't exist. - bool VirtualFileToDiskFile(const string& virtual_file, string* disk_file); + bool VirtualFileToDiskFile(const std::string& virtual_file, + std::string* disk_file); // implements SourceTree ------------------------------------------- - io::ZeroCopyInputStream* Open(const string& filename) override; + io::ZeroCopyInputStream* Open(const std::string& filename) override; - string GetLastErrorMessage() override; + std::string GetLastErrorMessage() override; private: struct Mapping { - string virtual_path; - string disk_path; + std::string virtual_path; + std::string disk_path; - inline Mapping(const string& virtual_path_param, - const string& disk_path_param) - : virtual_path(virtual_path_param), disk_path(disk_path_param) {} + inline Mapping(const std::string& virtual_path_param, + const std::string& disk_path_param) + : virtual_path(virtual_path_param), disk_path(disk_path_param) {} }; std::vector mappings_; - string last_error_message_; + std::string last_error_message_; // Like Open(), but returns the on-disk path in disk_file if disk_file is // non-NULL and the file could be successfully opened. - io::ZeroCopyInputStream* OpenVirtualFile(const string& virtual_file, - string* disk_file); + io::ZeroCopyInputStream* OpenVirtualFile(const std::string& virtual_file, + std::string* disk_file); // Like Open() but given the actual on-disk path. - io::ZeroCopyInputStream* OpenDiskFile(const string& filename); + io::ZeroCopyInputStream* OpenDiskFile(const std::string& filename); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DiskSourceTree); }; @@ -321,4 +330,6 @@ class LIBPROTOBUF_EXPORT DiskSourceTree : public SourceTree { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__ diff --git a/src/google/protobuf/compiler/importer_unittest.cc b/src/google/protobuf/compiler/importer_unittest.cc index 5108809df7..82ffc0fc2d 100644 --- a/src/google/protobuf/compiler/importer_unittest.cc +++ b/src/google/protobuf/compiler/importer_unittest.cc @@ -56,7 +56,7 @@ namespace compiler { namespace { -bool FileExists(const string& path) { +bool FileExists(const std::string& path) { return File::Exists(path); } @@ -68,20 +68,20 @@ class MockErrorCollector : public MultiFileErrorCollector { MockErrorCollector() {} ~MockErrorCollector() {} - string text_; - string warning_text_; + std::string text_; + std::string warning_text_; // implements ErrorCollector --------------------------------------- - void AddError(const string& filename, int line, int column, - const string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", - filename, line, column, message); + void AddError(const std::string& filename, int line, int column, + const std::string& message) { + strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line, + column, message); } - void AddWarning(const string& filename, int line, int column, - const string& message) { - strings::SubstituteAndAppend(&warning_text_, "$0:$1:$2: $3\n", - filename, line, column, message); + void AddWarning(const std::string& filename, int line, int column, + const std::string& message) { + strings::SubstituteAndAppend(&warning_text_, "$0:$1:$2: $3\n", filename, + line, column, message); } }; @@ -93,12 +93,12 @@ class MockSourceTree : public SourceTree { MockSourceTree() {} ~MockSourceTree() {} - void AddFile(const string& name, const char* contents) { + void AddFile(const std::string& name, const char* contents) { files_[name] = contents; } // implements SourceTree ------------------------------------------- - io::ZeroCopyInputStream* Open(const string& filename) { + io::ZeroCopyInputStream* Open(const std::string& filename) { const char* contents = FindPtrOrNull(files_, filename); if (contents == NULL) { return NULL; @@ -107,28 +107,24 @@ class MockSourceTree : public SourceTree { } } - string GetLastErrorMessage() { - return "File not found."; - } + std::string GetLastErrorMessage() { return "File not found."; } private: - std::unordered_map files_; + std::unordered_map files_; }; // =================================================================== class ImporterTest : public testing::Test { protected: - ImporterTest() - : importer_(&source_tree_, &error_collector_) {} + ImporterTest() : importer_(&source_tree_, &error_collector_) {} - void AddFile(const string& filename, const char* text) { + void AddFile(const std::string& filename, const char* text) { source_tree_.AddFile(filename, text); } // Return the collected error text - string error() const { return error_collector_.text_; } - string warning() const { return error_collector_.warning_text_; } + std::string warning() const { return error_collector_.warning_text_; } MockErrorCollector error_collector_; MockSourceTree source_tree_; @@ -138,8 +134,8 @@ class ImporterTest : public testing::Test { TEST_F(ImporterTest, Import) { // Test normal importing. AddFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); const FileDescriptor* file = importer_.Import("foo.proto"); EXPECT_EQ("", error_collector_.text_); @@ -155,14 +151,14 @@ TEST_F(ImporterTest, Import) { TEST_F(ImporterTest, ImportNested) { // Test that importing a file which imports another file works. AddFile("foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n" - "message Foo {\n" - " optional Bar bar = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n" + "message Foo {\n" + " optional Bar bar = 1;\n" + "}\n"); AddFile("bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); // Note that both files are actually parsed by the first call to Import() // here, since foo.proto imports bar.proto. The second call just returns @@ -192,42 +188,73 @@ TEST_F(ImporterTest, ImportNested) { TEST_F(ImporterTest, FileNotFound) { // Error: Parsing a file that doesn't exist. EXPECT_TRUE(importer_.Import("foo.proto") == NULL); - EXPECT_EQ( - "foo.proto:-1:0: File not found.\n", - error_collector_.text_); + EXPECT_EQ("foo.proto:-1:0: File not found.\n", error_collector_.text_); } TEST_F(ImporterTest, ImportNotFound) { // Error: Importing a file that doesn't exist. AddFile("foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n"); EXPECT_TRUE(importer_.Import("foo.proto") == NULL); EXPECT_EQ( - "bar.proto:-1:0: File not found.\n" - "foo.proto:-1:0: Import \"bar.proto\" was not found or had errors.\n", - error_collector_.text_); + "bar.proto:-1:0: File not found.\n" + "foo.proto:1:0: Import \"bar.proto\" was not found or had errors.\n", + error_collector_.text_); } TEST_F(ImporterTest, RecursiveImport) { // Error: Recursive import. AddFile("recursive1.proto", - "syntax = \"proto2\";\n" - "import \"recursive2.proto\";\n"); + "syntax = \"proto2\";\n" + "\n" + "import \"recursive2.proto\";\n"); AddFile("recursive2.proto", - "syntax = \"proto2\";\n" - "import \"recursive1.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"recursive1.proto\";\n"); EXPECT_TRUE(importer_.Import("recursive1.proto") == NULL); EXPECT_EQ( - "recursive1.proto:-1:0: File recursively imports itself: recursive1.proto " + "recursive1.proto:2:0: File recursively imports itself: " + "recursive1.proto " "-> recursive2.proto -> recursive1.proto\n" - "recursive2.proto:-1:0: Import \"recursive1.proto\" was not found " + "recursive2.proto:1:0: Import \"recursive1.proto\" was not found " "or had errors.\n" - "recursive1.proto:-1:0: Import \"recursive2.proto\" was not found " + "recursive1.proto:2:0: Import \"recursive2.proto\" was not found " "or had errors.\n", - error_collector_.text_); + error_collector_.text_); +} + +TEST_F(ImporterTest, RecursiveImportSelf) { + // Error: Recursive import. + AddFile("recursive.proto", + "syntax = \"proto2\";\n" + "\n" + "import \"recursive.proto\";\n"); + + EXPECT_TRUE(importer_.Import("recursive.proto") == nullptr); + EXPECT_EQ( + "recursive.proto:2:0: File recursively imports itself: " + "recursive.proto -> recursive.proto\n", + error_collector_.text_); +} + +TEST_F(ImporterTest, LiteRuntimeImport) { + // Error: Recursive import. + AddFile("bar.proto", + "syntax = \"proto2\";\n" + "option optimize_for = LITE_RUNTIME;\n"); + AddFile("foo.proto", + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n"); + + EXPECT_TRUE(importer_.Import("foo.proto") == nullptr); + EXPECT_EQ( + "foo.proto:1:0: Files that do not use optimize_for = LITE_RUNTIME " + "cannot import files which do use this option. This file is not " + "lite, but it imports \"bar.proto\" which is.\n", + error_collector_.text_); } @@ -255,22 +282,22 @@ class DiskSourceTreeTest : public testing::Test { } } - void AddFile(const string& filename, const char* contents) { + void AddFile(const std::string& filename, const char* contents) { GOOGLE_CHECK_OK(File::SetContents(filename, contents, true)); } - void AddSubdir(const string& dirname) { + void AddSubdir(const std::string& dirname) { GOOGLE_CHECK_OK(File::CreateDir(dirname, 0777)); } - void ExpectFileContents(const string& filename, + void ExpectFileContents(const std::string& filename, const char* expected_contents) { std::unique_ptr input(source_tree_.Open(filename)); ASSERT_FALSE(input == NULL); // Read all the data from the file. - string file_contents; + std::string file_contents; const void* data; int size; while (input->Next(&data, &size)) { @@ -280,8 +307,8 @@ class DiskSourceTreeTest : public testing::Test { EXPECT_EQ(expected_contents, file_contents); } - void ExpectCannotOpenFile(const string& filename, - const string& error_message) { + void ExpectCannotOpenFile(const std::string& filename, + const std::string& error_message) { std::unique_ptr input(source_tree_.Open(filename)); EXPECT_TRUE(input == NULL); EXPECT_EQ(error_message, source_tree_.GetLastErrorMessage()); @@ -290,7 +317,7 @@ class DiskSourceTreeTest : public testing::Test { DiskSourceTree source_tree_; // Paths of two on-disk directories to use during the test. - std::vector dirnames_; + std::vector dirnames_; }; TEST_F(DiskSourceTreeTest, MapRoot) { @@ -394,27 +421,27 @@ TEST_F(DiskSourceTreeTest, DiskFileToVirtualFile) { source_tree_.MapPath("bar", dirnames_[0]); source_tree_.MapPath("bar", dirnames_[1]); - string virtual_file; - string shadowing_disk_file; + std::string virtual_file; + std::string shadowing_disk_file; EXPECT_EQ(DiskSourceTree::NO_MAPPING, - source_tree_.DiskFileToVirtualFile( - "/foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("/foo", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ(DiskSourceTree::SHADOWED, - source_tree_.DiskFileToVirtualFile( - dirnames_[1] + "/foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile( + dirnames_[1] + "/foo", &virtual_file, &shadowing_disk_file)); EXPECT_EQ("bar/foo", virtual_file); EXPECT_EQ(dirnames_[0] + "/foo", shadowing_disk_file); EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - dirnames_[1] + "/baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile( + dirnames_[1] + "/baz", &virtual_file, &shadowing_disk_file)); EXPECT_EQ("bar/baz", virtual_file); EXPECT_EQ(DiskSourceTree::SUCCESS, - source_tree_.DiskFileToVirtualFile( - dirnames_[0] + "/foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile( + dirnames_[0] + "/foo", &virtual_file, &shadowing_disk_file)); EXPECT_EQ("bar/foo", virtual_file); } @@ -428,60 +455,60 @@ TEST_F(DiskSourceTreeTest, DiskFileToVirtualFileCanonicalization) { source_tree_.MapPath("", "/qux"); source_tree_.MapPath("dir5", "/quux/"); - string virtual_file; - string shadowing_disk_file; + std::string virtual_file; + std::string shadowing_disk_file; // "../.." should not be considered to be under "..". EXPECT_EQ(DiskSourceTree::NO_MAPPING, - source_tree_.DiskFileToVirtualFile( - "../../baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("../../baz", &virtual_file, + &shadowing_disk_file)); // "/foo" is not mapped (it should not be misintepreted as being under "."). EXPECT_EQ(DiskSourceTree::NO_MAPPING, - source_tree_.DiskFileToVirtualFile( - "/foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("/foo", &virtual_file, + &shadowing_disk_file)); #ifdef WIN32 // "C:\foo" is not mapped (it should not be misintepreted as being under "."). EXPECT_EQ(DiskSourceTree::NO_MAPPING, - source_tree_.DiskFileToVirtualFile( - "C:\\foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("C:\\foo", &virtual_file, + &shadowing_disk_file)); #endif // WIN32 // But "../baz" should be. EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "../baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("../baz", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir1/baz", virtual_file); // "../../foo/baz" is under "../../foo". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "../../foo/baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("../../foo/baz", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir2/baz", virtual_file); // "foo/./bar/baz" is under "./foo/bar/.". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "foo/bar/baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("foo/bar/baz", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir3/baz", virtual_file); // "bar" is under ".". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "bar", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("bar", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir4/bar", virtual_file); // "/qux/baz" is under "/qux". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "/qux/baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("/qux/baz", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("baz", virtual_file); // "/quux/bar" is under "/quux". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "/quux/bar", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("/quux/bar", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir5/bar", virtual_file); } @@ -495,14 +522,14 @@ TEST_F(DiskSourceTreeTest, VirtualFileToDiskFile) { source_tree_.MapPath("bar", dirnames_[1]); // Existent files, shadowed and non-shadowed case. - string disk_file; + std::string disk_file; EXPECT_TRUE(source_tree_.VirtualFileToDiskFile("bar/foo", &disk_file)); EXPECT_EQ(dirnames_[0] + "/foo", disk_file); EXPECT_TRUE(source_tree_.VirtualFileToDiskFile("bar/quux", &disk_file)); EXPECT_EQ(dirnames_[1] + "/quux", disk_file); // Nonexistent file in existent directory and vice versa. - string not_touched = "not touched"; + std::string not_touched = "not touched"; EXPECT_FALSE(source_tree_.VirtualFileToDiskFile("bar/baz", ¬_touched)); EXPECT_EQ("not touched", not_touched); EXPECT_FALSE(source_tree_.VirtualFileToDiskFile("baz/foo", ¬_touched)); diff --git a/src/google/protobuf/compiler/java/java_context.cc b/src/google/protobuf/compiler/java/java_context.cc index 2528c2d13f..778d3ba1e3 100644 --- a/src/google/protobuf/compiler/java/java_context.cc +++ b/src/google/protobuf/compiler/java/java_context.cc @@ -35,6 +35,7 @@ #include #include #include + #include namespace google { @@ -47,8 +48,7 @@ Context::Context(const FileDescriptor* file, const Options& options) InitializeFieldGeneratorInfo(file); } -Context::~Context() { -} +Context::~Context() {} ClassNameResolver* Context::GetNameResolver() const { return name_resolver_.get(); @@ -58,9 +58,9 @@ namespace { // Whether two fields have conflicting accessors (assuming name1 and name2 // are different). name1 and name2 are field1 and field2's camel-case name // respectively. -bool IsConflicting(const FieldDescriptor* field1, const string& name1, - const FieldDescriptor* field2, const string& name2, - string* info) { +bool IsConflicting(const FieldDescriptor* field1, const std::string& name1, + const FieldDescriptor* field2, const std::string& name2, + std::string* info) { if (field1->is_repeated()) { if (field2->is_repeated()) { // Both fields are repeated. @@ -128,13 +128,13 @@ void Context::InitializeFieldGeneratorInfoForFields( // Find out all fields that conflict with some other field in the same // message. std::vector is_conflict(fields.size()); - std::vector conflict_reason(fields.size()); + std::vector conflict_reason(fields.size()); for (int i = 0; i < fields.size(); ++i) { const FieldDescriptor* field = fields[i]; - const string& name = UnderscoresToCapitalizedCamelCase(field); + const std::string& name = UnderscoresToCapitalizedCamelCase(field); for (int j = i + 1; j < fields.size(); ++j) { const FieldDescriptor* other = fields[j]; - const string& other_name = UnderscoresToCapitalizedCamelCase(other); + const std::string& other_name = UnderscoresToCapitalizedCamelCase(other); if (name == other_name) { is_conflict[i] = is_conflict[j] = true; conflict_reason[i] = conflict_reason[j] = @@ -159,8 +159,8 @@ void Context::InitializeFieldGeneratorInfoForFields( // For fields conflicting with some other fields, we append the field // number to their field names in generated code to avoid conflicts. if (is_conflict[i]) { - info.name += SimpleItoa(field->number()); - info.capitalized_name += SimpleItoa(field->number()); + info.name += StrCat(field->number()); + info.capitalized_name += StrCat(field->number()); info.disambiguated_reason = conflict_reason[i]; } field_generator_info_map_[field] = info; diff --git a/src/google/protobuf/compiler/java/java_context.h b/src/google/protobuf/compiler/java/java_context.h index fd41a27054..3fa6af1778 100644 --- a/src/google/protobuf/compiler/java/java_context.h +++ b/src/google/protobuf/compiler/java/java_context.h @@ -40,16 +40,16 @@ namespace google { namespace protobuf { - class FileDescriptor; - class FieldDescriptor; - class OneofDescriptor; - class Descriptor; - class EnumDescriptor; - namespace compiler { - namespace java { - class ClassNameResolver; // name_resolver.h - } - } +class FileDescriptor; +class FieldDescriptor; +class OneofDescriptor; +class Descriptor; +class EnumDescriptor; +namespace compiler { +namespace java { +class ClassNameResolver; // name_resolver.h +} +} // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/java/java_doc_comment.cc b/src/google/protobuf/compiler/java/java_doc_comment.cc index 59c04ad412..2aee232354 100644 --- a/src/google/protobuf/compiler/java/java_doc_comment.cc +++ b/src/google/protobuf/compiler/java/java_doc_comment.cc @@ -44,13 +44,13 @@ namespace protobuf { namespace compiler { namespace java { -string EscapeJavadoc(const string& input) { - string result; +std::string EscapeJavadoc(const std::string& input) { + std::string result; result.reserve(input.size() * 2); char prev = '*'; - for (string::size_type i = 0; i < input.size(); i++) { + for (std::string::size_type i = 0; i < input.size(); i++) { char c = input[i]; switch (c) { case '*': @@ -102,10 +102,11 @@ string EscapeJavadoc(const string& input) { return result; } -static void WriteDocCommentBodyForLocation( - io::Printer* printer, const SourceLocation& location) { - string comments = location.leading_comments.empty() ? - location.trailing_comments : location.leading_comments; +static void WriteDocCommentBodyForLocation(io::Printer* printer, + const SourceLocation& location) { + std::string comments = location.leading_comments.empty() + ? location.trailing_comments + : location.leading_comments; if (!comments.empty()) { // TODO(kenton): Ideally we should parse the comment text as Markdown and // write it back as HTML, but this requires a Markdown parser. For now @@ -115,7 +116,7 @@ static void WriteDocCommentBodyForLocation( // HTML-escape them so that they don't accidentally close the doc comment. comments = EscapeJavadoc(comments); - std::vector lines = Split(comments, "\n"); + std::vector lines = Split(comments, "\n"); while (!lines.empty() && lines.back().empty()) { lines.pop_back(); } @@ -138,19 +139,19 @@ static void WriteDocCommentBodyForLocation( } template -static void WriteDocCommentBody( - io::Printer* printer, const DescriptorType* descriptor) { +static void WriteDocCommentBody(io::Printer* printer, + const DescriptorType* descriptor) { SourceLocation location; if (descriptor->GetSourceLocation(&location)) { WriteDocCommentBodyForLocation(printer, location); } } -static string FirstLineOf(const string& value) { - string result = value; +static std::string FirstLineOf(const std::string& value) { + std::string result = value; - string::size_type pos = result.find_first_of('\n'); - if (pos != string::npos) { + std::string::size_type pos = result.find_first_of('\n'); + if (pos != std::string::npos) { result.erase(pos); } @@ -166,35 +167,203 @@ void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) { printer->Print("/**\n"); WriteDocCommentBody(printer, message); printer->Print( - " * Protobuf type {@code $fullname$}\n" - " */\n", - "fullname", EscapeJavadoc(message->full_name())); + " * Protobuf type {@code $fullname$}\n" + " */\n", + "fullname", EscapeJavadoc(message->full_name())); } void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field) { - // In theory we should have slightly different comments for setters, getters, - // etc., but in practice everyone already knows the difference between these - // so it's redundant information. - // We start the comment with the main body based on the comments from the - // .proto file (if present). We then end with the field declaration, e.g.: + // .proto file (if present). We then continue with the field declaration, e.g.: // optional string foo = 5; + // And then we end with the javadoc tags if applicable. // If the field is a group, the debug string might end with {. printer->Print("/**\n"); WriteDocCommentBody(printer, field); - printer->Print( - " * $def$\n", - "def", EscapeJavadoc(FirstLineOf(field->DebugString()))); + printer->Print(" * $def$\n", "def", + EscapeJavadoc(FirstLineOf(field->DebugString()))); + printer->Print(" */\n"); +} + +void WriteFieldAccessorDocComment(io::Printer* printer, + const FieldDescriptor* field, + const FieldAccessorType type, + const bool builder) { + printer->Print("/**\n"); + WriteDocCommentBody(printer, field); + printer->Print(" * $def$\n", "def", + EscapeJavadoc(FirstLineOf(field->DebugString()))); + switch (type) { + case HAZZER: + printer->Print(" * @return Whether the $name$ field is set.\n", "name", + field->camelcase_name()); + break; + case GETTER: + printer->Print(" * @return The $name$.\n", "name", + field->camelcase_name()); + break; + case SETTER: + printer->Print(" * @param value The $name$ to set.\n", "name", + field->camelcase_name()); + break; + case CLEARER: + // Print nothing + break; + // Repeated + case LIST_COUNT: + printer->Print(" * @return The number of $name$(s).\n", "name", + field->camelcase_name()); + break; + case LIST_GETTER: + printer->Print(" * @return A list containing the $name$(s).\n", "name", + field->camelcase_name()); + break; + case LIST_INDEXED_GETTER: + printer->Print(" * @param index The index of the element to return.\n"); + printer->Print(" * @return The $name$(s) at the given index.\n", "name", + field->camelcase_name()); + break; + case LIST_INDEXED_SETTER: + printer->Print(" * @param index The index to set the value at.\n"); + printer->Print(" * @param value The $name$ to set.\n", "name", + field->camelcase_name()); + break; + case LIST_ADDER: + printer->Print(" * @param value The $name$ to add.\n", "name", + field->camelcase_name()); + break; + case LIST_MULTI_ADDER: + printer->Print(" * @param values The $name$(s) to add.\n", "name", + field->camelcase_name()); + break; + } + if (builder) { + printer->Print(" * @return This builder for chaining.\n"); + } + printer->Print(" */\n"); +} + +void WriteFieldEnumValueAccessorDocComment(io::Printer* printer, + const FieldDescriptor* field, + const FieldAccessorType type, + const bool builder) { + printer->Print("/**\n"); + WriteDocCommentBody(printer, field); + printer->Print(" * $def$\n", "def", + EscapeJavadoc(FirstLineOf(field->DebugString()))); + switch (type) { + case HAZZER: + // Should never happen + break; + case GETTER: + printer->Print(" * @return The enum value for $name$.\n", "name", + field->camelcase_name()); + break; + case SETTER: + printer->Print(" * @param value The enum value for $name$ to set.\n", + "name", field->camelcase_name()); + break; + case CLEARER: + // Print nothing + break; + // Repeated + case LIST_COUNT: + // Should never happen + break; + case LIST_GETTER: + printer->Print(" * @return A list containing the enum values for " + "$name$(s).\n", "name", field->camelcase_name()); + break; + case LIST_INDEXED_GETTER: + printer->Print(" * @param index The index of the value to return.\n"); + printer->Print(" * @return The enum value of the $name$ at the given " + "index.\n", "name", field->camelcase_name()); + break; + case LIST_INDEXED_SETTER: + printer->Print(" * @param index The index to set the value at.\n"); + printer->Print(" * @param value The enum value of the $name$ to set.\n", + "name", field->camelcase_name()); + break; + case LIST_ADDER: + printer->Print(" * @param value The enum value of the $name$ to add.\n", + "name", field->camelcase_name()); + break; + case LIST_MULTI_ADDER: + printer->Print(" * @param values The enum values of the $name$(s) to " + "add.\n", "name", field->camelcase_name()); + break; + } + if (builder) { + printer->Print(" * @return This builder for chaining.\n"); + } + printer->Print(" */\n"); +} + +void WriteFieldStringBytesAccessorDocComment(io::Printer* printer, + const FieldDescriptor* field, + const FieldAccessorType type, + const bool builder) { + printer->Print("/**\n"); + WriteDocCommentBody(printer, field); + printer->Print(" * $def$\n", "def", + EscapeJavadoc(FirstLineOf(field->DebugString()))); + switch (type) { + case HAZZER: + // Should never happen + break; + case GETTER: + printer->Print(" * @return The bytes for $name$.\n", "name", + field->camelcase_name()); + break; + case SETTER: + printer->Print(" * @param value The bytes for $name$ to set.\n", + "name", field->camelcase_name()); + break; + case CLEARER: + // Print nothing + break; + // Repeated + case LIST_COUNT: + // Should never happen + break; + case LIST_GETTER: + printer->Print(" * @return A list containing the bytes for $name$(s).\n", + "name", field->camelcase_name()); + break; + case LIST_INDEXED_GETTER: + printer->Print(" * @param index The index of the value to return.\n"); + printer->Print(" * @return The bytes of the $name$ at the given index.\n", + "name", field->camelcase_name()); + break; + case LIST_INDEXED_SETTER: + printer->Print(" * @param index The index to set the value at.\n"); + printer->Print(" * @param value The bytes of the $name$ to set.\n", + "name", field->camelcase_name()); + break; + case LIST_ADDER: + printer->Print(" * @param value The bytes of the $name$ to add.\n", + "name", field->camelcase_name()); + break; + case LIST_MULTI_ADDER: + printer->Print(" * @param values The bytes of the $name$(s) to add.\n", + "name", field->camelcase_name()); + break; + } + if (builder) { + printer->Print(" * @return This builder for chaining.\n"); + } printer->Print(" */\n"); } +// Enum + void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_) { printer->Print("/**\n"); WriteDocCommentBody(printer, enum_); printer->Print( - " * Protobuf enum {@code $fullname$}\n" - " */\n", - "fullname", EscapeJavadoc(enum_->full_name())); + " * Protobuf enum {@code $fullname$}\n" + " */\n", + "fullname", EscapeJavadoc(enum_->full_name())); } void WriteEnumValueDocComment(io::Printer* printer, @@ -202,9 +371,9 @@ void WriteEnumValueDocComment(io::Printer* printer, printer->Print("/**\n"); WriteDocCommentBody(printer, value); printer->Print( - " * $def$\n" - " */\n", - "def", EscapeJavadoc(FirstLineOf(value->DebugString()))); + " * $def$\n" + " */\n", + "def", EscapeJavadoc(FirstLineOf(value->DebugString()))); } void WriteServiceDocComment(io::Printer* printer, @@ -212,9 +381,9 @@ void WriteServiceDocComment(io::Printer* printer, printer->Print("/**\n"); WriteDocCommentBody(printer, service); printer->Print( - " * Protobuf service {@code $fullname$}\n" - " */\n", - "fullname", EscapeJavadoc(service->full_name())); + " * Protobuf service {@code $fullname$}\n" + " */\n", + "fullname", EscapeJavadoc(service->full_name())); } void WriteMethodDocComment(io::Printer* printer, @@ -222,9 +391,9 @@ void WriteMethodDocComment(io::Printer* printer, printer->Print("/**\n"); WriteDocCommentBody(printer, method); printer->Print( - " * $def$\n" - " */\n", - "def", EscapeJavadoc(FirstLineOf(method->DebugString()))); + " * $def$\n" + " */\n", + "def", EscapeJavadoc(FirstLineOf(method->DebugString()))); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_doc_comment.h b/src/google/protobuf/compiler/java/java_doc_comment.h index 112c3bc3c1..a5ef09690c 100644 --- a/src/google/protobuf/compiler/java/java_doc_comment.h +++ b/src/google/protobuf/compiler/java/java_doc_comment.h @@ -37,6 +37,8 @@ #include +#include + namespace google { namespace protobuf { namespace io { @@ -50,8 +52,36 @@ namespace protobuf { namespace compiler { namespace java { +enum FieldAccessorType { + + HAZZER, + GETTER, + SETTER, + CLEARER, + // Repeated + LIST_COUNT, + LIST_GETTER, + LIST_INDEXED_GETTER, + LIST_INDEXED_SETTER, + LIST_ADDER, + LIST_MULTI_ADDER + +}; + void WriteMessageDocComment(io::Printer* printer, const Descriptor* message); void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field); +void WriteFieldAccessorDocComment(io::Printer* printer, + const FieldDescriptor* field, + const FieldAccessorType type, + const bool builder = false); +void WriteFieldEnumValueAccessorDocComment(io::Printer* printer, + const FieldDescriptor* field, + const FieldAccessorType type, + const bool builder = false); +void WriteFieldStringBytesAccessorDocComment(io::Printer* printer, + const FieldDescriptor* field, + const FieldAccessorType type, + const bool builder = false); void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_); void WriteEnumValueDocComment(io::Printer* printer, const EnumValueDescriptor* value); @@ -61,11 +91,13 @@ void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method); // Exposed for testing only. -LIBPROTOC_EXPORT string EscapeJavadoc(const string& input); +PROTOC_EXPORT std::string EscapeJavadoc(const std::string& input); } // namespace java } // namespace compiler } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_DOC_COMMENT_H__ diff --git a/src/google/protobuf/compiler/java/java_enum.cc b/src/google/protobuf/compiler/java/java_enum.cc index 1ca7ba958b..bf911be4b9 100644 --- a/src/google/protobuf/compiler/java/java_enum.cc +++ b/src/google/protobuf/compiler/java/java_enum.cc @@ -44,21 +44,22 @@ #include #include + namespace google { namespace protobuf { namespace compiler { namespace java { EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor, - bool immutable_api, - Context* context) - : descriptor_(descriptor), immutable_api_(immutable_api), - context_(context), - name_resolver_(context->GetNameResolver()) { + bool immutable_api, Context* context) + : descriptor_(descriptor), + immutable_api_(immutable_api), + context_(context), + name_resolver_(context->GetNameResolver()) { for (int i = 0; i < descriptor_->value_count(); i++) { const EnumValueDescriptor* value = descriptor_->value(i); const EnumValueDescriptor* canonical_value = - descriptor_->FindValueByNumber(value->number()); + descriptor_->FindValueByNumber(value->number()); if (value == canonical_value) { canonical_values_.push_back(value); @@ -84,7 +85,7 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Indent(); bool ordinal_is_index = true; - string index_text = "ordinal()"; + std::string index_text = "ordinal()"; for (int i = 0; i < canonical_values_.size(); i++) { if (canonical_values_[i]->index() != i) { ordinal_is_index = false; @@ -94,20 +95,18 @@ void EnumGenerator::Generate(io::Printer* printer) { } for (int i = 0; i < canonical_values_.size(); i++) { - std::map vars; + std::map vars; vars["name"] = canonical_values_[i]->name(); - vars["index"] = SimpleItoa(canonical_values_[i]->index()); - vars["number"] = SimpleItoa(canonical_values_[i]->number()); + vars["index"] = StrCat(canonical_values_[i]->index()); + vars["number"] = StrCat(canonical_values_[i]->number()); WriteEnumValueDocComment(printer, canonical_values_[i]); if (canonical_values_[i]->options().deprecated()) { printer->Print("@java.lang.Deprecated\n"); } if (ordinal_is_index) { - printer->Print(vars, - "$name$($number$),\n"); + printer->Print(vars, "$name$($number$),\n"); } else { - printer->Print(vars, - "$name$($index$, $number$),\n"); + printer->Print(vars, "$name$($index$, $number$),\n"); } printer->Annotate("name", canonical_values_[i]); } @@ -122,31 +121,31 @@ void EnumGenerator::Generate(io::Printer* printer) { } printer->Print( - ";\n" - "\n"); + ";\n" + "\n"); // ----------------------------------------------------------------- for (int i = 0; i < aliases_.size(); i++) { - std::map vars; + std::map vars; vars["classname"] = descriptor_->name(); vars["name"] = aliases_[i].value->name(); vars["canonical_name"] = aliases_[i].canonical_value->name(); WriteEnumValueDocComment(printer, aliases_[i].value); - printer->Print(vars, - "public static final $classname$ $name$ = $canonical_name$;\n"); + printer->Print( + vars, "public static final $classname$ $name$ = $canonical_name$;\n"); printer->Annotate("name", aliases_[i].value); } for (int i = 0; i < descriptor_->value_count(); i++) { - std::map vars; + std::map vars; vars["name"] = descriptor_->value(i)->name(); - vars["number"] = SimpleItoa(descriptor_->value(i)->number()); + vars["number"] = StrCat(descriptor_->value(i)->number()); vars["{"] = ""; vars["}"] = ""; WriteEnumValueDocComment(printer, descriptor_->value(i)); printer->Print(vars, - "public static final int ${$$name$_VALUE$}$ = $number$;\n"); + "public static final int ${$$name$_VALUE$}$ = $number$;\n"); printer->Annotate("{", "}", descriptor_->value(i)); } printer->Print("\n"); @@ -154,84 +153,86 @@ void EnumGenerator::Generate(io::Printer* printer) { // ----------------------------------------------------------------- printer->Print( - "\n" - "public final int getNumber() {\n"); + "\n" + "public final int getNumber() {\n"); if (SupportUnknownEnumValue(descriptor_->file())) { if (ordinal_is_index) { printer->Print( - " if (this == UNRECOGNIZED) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Can't get the number of an unknown enum value.\");\n" - " }\n"); + " if (this == UNRECOGNIZED) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Can't get the number of an unknown enum value.\");\n" + " }\n"); } else { printer->Print( - " if (index == -1) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Can't get the number of an unknown enum value.\");\n" - " }\n"); + " if (index == -1) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Can't get the number of an unknown enum value.\");\n" + " }\n"); } } printer->Print( - " return value;\n" - "}\n" - "\n" - "/**\n" - " * @deprecated Use {@link #forNumber(int)} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public static $classname$ valueOf(int value) {\n" - " return forNumber(value);\n" - "}\n" - "\n" - "public static $classname$ forNumber(int value) {\n" - " switch (value) {\n", - "classname", descriptor_->name()); + " return value;\n" + "}\n" + "\n" + "/**\n" + " * @param value The number of the enum to look for.\n" + " * @return The enum associated with the given number.\n" + " * @deprecated Use {@link #forNumber(int)} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public static $classname$ valueOf(int value) {\n" + " return forNumber(value);\n" + "}\n" + "\n" + "public static $classname$ forNumber(int value) {\n" + " switch (value) {\n", + "classname", descriptor_->name()); printer->Indent(); printer->Indent(); for (int i = 0; i < canonical_values_.size(); i++) { printer->Print("case $number$: return $name$;\n", "name", canonical_values_[i]->name(), "number", - SimpleItoa(canonical_values_[i]->number())); + StrCat(canonical_values_[i]->number())); } printer->Outdent(); printer->Outdent(); printer->Print( - " default: return null;\n" - " }\n" - "}\n" - "\n" - "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n" - " internalGetValueMap() {\n" - " return internalValueMap;\n" - "}\n" - "private static final com.google.protobuf.Internal.EnumLiteMap<\n" - " $classname$> internalValueMap =\n" - " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n" - " public $classname$ findValueByNumber(int number) {\n" - " return $classname$.forNumber(number);\n" - " }\n" - " };\n" - "\n", - "classname", descriptor_->name()); + " default: return null;\n" + " }\n" + "}\n" + "\n" + "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n" + " internalGetValueMap() {\n" + " return internalValueMap;\n" + "}\n" + "private static final com.google.protobuf.Internal.EnumLiteMap<\n" + " $classname$> internalValueMap =\n" + " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n" + " public $classname$ findValueByNumber(int number) {\n" + " return $classname$.forNumber(number);\n" + " }\n" + " };\n" + "\n", + "classname", descriptor_->name()); // ----------------------------------------------------------------- // Reflection if (HasDescriptorMethods(descriptor_, context_->EnforceLite())) { printer->Print( - "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n" - " getValueDescriptor() {\n" - " return getDescriptor().getValues().get($index_text$);\n" - "}\n" - "public final com.google.protobuf.Descriptors.EnumDescriptor\n" - " getDescriptorForType() {\n" - " return getDescriptor();\n" - "}\n" - "public static final com.google.protobuf.Descriptors.EnumDescriptor\n" - " getDescriptor() {\n", - "index_text", index_text); + "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n" + " getValueDescriptor() {\n" + " return getDescriptor().getValues().get($index_text$);\n" + "}\n" + "public final com.google.protobuf.Descriptors.EnumDescriptor\n" + " getDescriptorForType() {\n" + " return getDescriptor();\n" + "}\n" + "public static final com.google.protobuf.Descriptors.EnumDescriptor\n" + " getDescriptor() {\n", + "index_text", index_text); // TODO(kenton): Cache statically? Note that we can't access descriptors // at module init time because it wouldn't work with descriptor.proto, but @@ -244,7 +245,7 @@ void EnumGenerator::Generate(io::Printer* printer) { " return $file$.getDescriptor().getEnumTypes().get($index$);\n", "file", name_resolver_->GetClassName(descriptor_->file(), immutable_api_), - "index", SimpleItoa(descriptor_->index())); + "index", StrCat(descriptor_->index())); } else { printer->Print( " return $parent$.$descriptor$.getEnumTypes().get($index$);\n", @@ -257,14 +258,14 @@ void EnumGenerator::Generate(io::Printer* printer) { .no_standard_descriptor_accessor() ? "getDefaultInstance().getDescriptorForType()" : "getDescriptor()", - "index", SimpleItoa(descriptor_->index())); + "index", StrCat(descriptor_->index())); } printer->Print( - "}\n" - "\n" - "private static final $classname$[] VALUES = ", - "classname", descriptor_->name()); + "}\n" + "\n" + "private static final $classname$[] VALUES = ", + "classname", descriptor_->name()); if (CanUseEnumValues()) { // If the constants we are going to output are exactly the ones we @@ -274,11 +275,10 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Print("values();\n"); } else { printer->Print( - "{\n" - " "); + "{\n" + " "); for (int i = 0; i < descriptor_->value_count(); i++) { - printer->Print("$name$, ", - "name", descriptor_->value(i)->name()); + printer->Print("$name$, ", "name", descriptor_->value(i)->name()); } printer->Print( "\n" @@ -286,24 +286,24 @@ void EnumGenerator::Generate(io::Printer* printer) { } printer->Print( - "\n" - "public static $classname$ valueOf(\n" - " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n" - " if (desc.getType() != getDescriptor()) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"EnumValueDescriptor is not for this type.\");\n" - " }\n", - "classname", descriptor_->name()); + "\n" + "public static $classname$ valueOf(\n" + " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n" + " if (desc.getType() != getDescriptor()) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"EnumValueDescriptor is not for this type.\");\n" + " }\n", + "classname", descriptor_->name()); if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print( - " if (desc.getIndex() == -1) {\n" - " return UNRECOGNIZED;\n" - " }\n"); + " if (desc.getIndex() == -1) {\n" + " return UNRECOGNIZED;\n" + " }\n"); } printer->Print( - " return VALUES[desc.getIndex()];\n" - "}\n" - "\n"); + " return VALUES[desc.getIndex()];\n" + "}\n" + "\n"); if (!ordinal_is_index) { printer->Print("private final int index;\n"); @@ -312,30 +312,27 @@ void EnumGenerator::Generate(io::Printer* printer) { // ----------------------------------------------------------------- - printer->Print( - "private final int value;\n\n"); + printer->Print("private final int value;\n\n"); if (ordinal_is_index) { - printer->Print( - "private $classname$(int value) {\n", - "classname", descriptor_->name()); + printer->Print("private $classname$(int value) {\n", "classname", + descriptor_->name()); } else { - printer->Print( - "private $classname$(int index, int value) {\n", - "classname", descriptor_->name()); + printer->Print("private $classname$(int index, int value) {\n", "classname", + descriptor_->name()); } if (HasDescriptorMethods(descriptor_, context_->EnforceLite()) && !ordinal_is_index) { printer->Print(" this.index = index;\n"); } printer->Print( - " this.value = value;\n" - "}\n"); + " this.value = value;\n" + "}\n"); printer->Print( - "\n" - "// @@protoc_insertion_point(enum_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(enum_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n\n"); diff --git a/src/google/protobuf/compiler/java/java_enum.h b/src/google/protobuf/compiler/java/java_enum.h index d650924c98..0a2c363b53 100644 --- a/src/google/protobuf/compiler/java/java_enum.h +++ b/src/google/protobuf/compiler/java/java_enum.h @@ -42,13 +42,13 @@ namespace google { namespace protobuf { namespace compiler { - namespace java { - class Context; // context.h - class ClassNameResolver; // name_resolver.h - } -} +namespace java { +class Context; // context.h +class ClassNameResolver; // name_resolver.h +} // namespace java +} // namespace compiler namespace io { - class Printer; // printer.h +class Printer; // printer.h } } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/java/java_enum_field.cc b/src/google/protobuf/compiler/java/java_enum_field.cc index 9d0bcd2cec..5ae51ffe67 100644 --- a/src/google/protobuf/compiler/java/java_enum_field.cc +++ b/src/google/protobuf/compiler/java/java_enum_field.cc @@ -46,6 +46,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -53,12 +54,10 @@ namespace java { namespace { -void SetEnumVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - const FieldGeneratorInfo* info, +void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, + int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); (*variables)["type"] = @@ -67,15 +66,15 @@ void SetEnumVariables(const FieldDescriptor* descriptor, name_resolver->GetMutableClassName(descriptor->enum_type()); (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver); (*variables)["default_number"] = - SimpleItoa(descriptor->default_value_enum()->number()); - (*variables)["tag"] = SimpleItoa( + StrCat(descriptor->default_value_enum()->number()); + (*variables)["tag"] = StrCat( static_cast(internal::WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = SimpleItoa( + (*variables)["tag_size"] = StrCat( internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor))); // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["on_changed"] = "onChanged();"; // Use deprecated valueOf() method to be compatible with old generated code // for v2.5.0/v2.6.1. @@ -103,8 +102,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor, (*variables)["clear_has_field_bit_builder"] = ""; (*variables)["is_field_present_message"] = - (*variables)["name"] + "_ != " + - (*variables)["default"] + ".getNumber()"; + (*variables)["name"] + "_ != " + (*variables)["default"] + + ".getNumber()"; } // For repated builders, one bit is used for whether the array is immutable. @@ -135,460 +134,467 @@ void SetEnumVariables(const FieldDescriptor* descriptor, // =================================================================== -ImmutableEnumFieldGenerator:: -ImmutableEnumFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), - name_resolver_(context->GetNameResolver()) { +ImmutableEnumFieldGenerator::ImmutableEnumFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetEnumVariables(descriptor, messageBitIndex, builderBitIndex, - context->GetFieldGeneratorInfo(descriptor), - name_resolver_, &variables_); + context->GetFieldGeneratorInfo(descriptor), name_resolver_, + &variables_); } ImmutableEnumFieldGenerator::~ImmutableEnumFieldGenerator() {} int ImmutableEnumFieldGenerator::GetNumBitsForMessage() const { - return 1; + return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } int ImmutableEnumFieldGenerator::GetNumBitsForBuilder() const { - return 1; + return GetNumBitsForMessage(); } -void ImmutableEnumFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Value();\n"); + "$deprecation$int get$capitalized_name$Value();\n"); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutableEnumFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private int $name$_;\n"); +void ImmutableEnumFieldGenerator::GenerateMembers(io::Printer* printer) const { + printer->Print(variables_, "private int $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return $name$_;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$($name$_);\n" - " return result == null ? $unknown$ : result;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$($name$_);\n" + " return result == null ? $unknown$ : result;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print(variables_, - "private int $name$_ = $default_number$;\n"); +void ImmutableEnumFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print(variables_, "private int $name$_ = $default_number$;\n"); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_builder$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_builder$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return $name$_;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder " - "${$set$capitalized_name$Value$}$(int value) {\n" - " $name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" + " $name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$($name$_);\n" - " return result == null ? $unknown$ : result;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$($name$_);\n" + " return result == null ? $unknown$ : result;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $set_has_field_bit_builder$\n" - " $name$_ = value.getNumber();\n" - " $on_changed$\n" - " return this;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $clear_has_field_bit_builder$\n" - " $name$_ = $default_number$;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $set_has_field_bit_builder$\n" + " $name$_ = value.getNumber();\n" + " $on_changed$\n" + " return this;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $clear_has_field_bit_builder$\n" + " $name$_ = $default_number$;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { // noop for enums } -void ImmutableEnumFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $default_number$;\n"); } -void ImmutableEnumFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $default_number$;\n" - "$clear_has_field_bit_builder$\n"); + "$name$_ = $default_number$;\n" + "$clear_has_field_bit_builder$\n"); } -void ImmutableEnumFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, - "if (other.has$capitalized_name$()) {\n" - " set$capitalized_name$(other.get$capitalized_name$());\n" - "}\n"); + "if (other.has$capitalized_name$()) {\n" + " set$capitalized_name$(other.get$capitalized_name$());\n" + "}\n"); } else if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "if (other.$name$_ != $default_number$) {\n" - " set$capitalized_name$Value(other.get$capitalized_name$Value());\n" - "}\n"); + printer->Print( + variables_, + "if (other.$name$_ != $default_number$) {\n" + " set$capitalized_name$Value(other.get$capitalized_name$Value());\n" + "}\n"); } else { GOOGLE_LOG(FATAL) << "Can't reach here."; } } -void ImmutableEnumFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, - "if ($get_has_field_bit_from_local$) {\n" - " $set_has_field_bit_to_local$;\n" - "}\n"); + "if ($get_has_field_bit_from_local$) {\n" + " $set_has_field_bit_to_local$;\n" + "}\n"); } - printer->Print(variables_, - "result.$name$_ = $name$_;\n"); + printer->Print(variables_, "result.$name$_ = $name$_;\n"); } -void ImmutableEnumFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$set_has_field_bit_message$\n" - "$name$_ = rawValue;\n"); + "int rawValue = input.readEnum();\n" + "$set_has_field_bit_message$\n" + "$name$_ = rawValue;\n"); } else { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - " @SuppressWarnings(\"deprecation\")\n" - "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n" - " unknownFields.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $set_has_field_bit_message$\n" - " $name$_ = rawValue;\n" - "}\n"); + "int rawValue = input.readEnum();\n" + " @SuppressWarnings(\"deprecation\")\n" + "$type$ value = $type$.$for_number$(rawValue);\n" + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" + "} else {\n" + " $set_has_field_bit_message$\n" + " $name$_ = rawValue;\n" + "}\n"); } } -void ImmutableEnumFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // noop for enums } -void ImmutableEnumFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.writeEnum($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " output.writeEnum($number$, $name$_);\n" + "}\n"); } -void ImmutableEnumFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSize($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .computeEnumSize($number$, $name$_);\n" + "}\n"); } -void ImmutableEnumFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && $name$_ == other.$name$_;\n"); +void ImmutableEnumFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print(variables_, "if ($name$_ != other.$name$_) return false;\n"); } -void ImmutableEnumFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateHashCode(io::Printer* printer) const { printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + $name$_;\n"); + "hash = (37 * hash) + $constant_name$;\n" + "hash = (53 * hash) + $name$_;\n"); } -string ImmutableEnumFieldGenerator::GetBoxedType() const { +std::string ImmutableEnumFieldGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); } // =================================================================== -ImmutableEnumOneofFieldGenerator:: -ImmutableEnumOneofFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : ImmutableEnumFieldGenerator( - descriptor, messageBitIndex, builderBitIndex, context) { +ImmutableEnumOneofFieldGenerator::ImmutableEnumOneofFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : ImmutableEnumFieldGenerator(descriptor, messageBitIndex, builderBitIndex, + context) { const OneofGeneratorInfo* info = context->GetOneofGeneratorInfo(descriptor->containing_oneof()); SetCommonOneofVariables(descriptor, info, &variables_); } -ImmutableEnumOneofFieldGenerator:: -~ImmutableEnumOneofFieldGenerator() {} +ImmutableEnumOneofFieldGenerator::~ImmutableEnumOneofFieldGenerator() {} -void ImmutableEnumOneofFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return (java.lang.Integer) $oneof_name$_;\n" - " }\n" - " return $default_number$;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return (java.lang.Integer) $oneof_name$_;\n" + " }\n" + " return $default_number$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$(\n" - " (java.lang.Integer) $oneof_name$_);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " return $default$;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$(\n" + " (java.lang.Integer) $oneof_name$_);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumOneofFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ((java.lang.Integer) $oneof_name$_).intValue();\n" - " }\n" - " return $default_number$;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ((java.lang.Integer) $oneof_name$_).intValue();\n" + " }\n" + " return $default_number$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder " - "${$set$capitalized_name$Value$}$(int value) {\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$(\n" - " (java.lang.Integer) $oneof_name$_);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " return $default$;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$(\n" + " (java.lang.Integer) $oneof_name$_);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value.getNumber();\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value.getNumber();\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " $on_changed$\n" - " }\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " $on_changed$\n" + " }\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumOneofFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " result.$oneof_name$_ = $oneof_name$_;\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " result.$oneof_name$_ = $oneof_name$_;\n" + "}\n"); } -void ImmutableEnumOneofFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "set$capitalized_name$Value(other.get$capitalized_name$Value());\n"); + printer->Print( + variables_, + "set$capitalized_name$Value(other.get$capitalized_name$Value());\n"); } else { printer->Print(variables_, - "set$capitalized_name$(other.get$capitalized_name$());\n"); + "set$capitalized_name$(other.get$capitalized_name$());\n"); } } -void ImmutableEnumOneofFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = rawValue;\n"); + "int rawValue = input.readEnum();\n" + "$set_oneof_case_message$;\n" + "$oneof_name$_ = rawValue;\n"); } else { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "@SuppressWarnings(\"deprecation\")\n" - "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n" - " unknownFields.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = rawValue;\n" - "}\n"); + "int rawValue = input.readEnum();\n" + "@SuppressWarnings(\"deprecation\")\n" + "$type$ value = $type$.$for_number$(rawValue);\n" + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" + "} else {\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = rawValue;\n" + "}\n"); } } -void ImmutableEnumOneofFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.writeEnum($number$, ((java.lang.Integer) $oneof_name$_));\n" - "}\n"); +void ImmutableEnumOneofFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($has_oneof_case_message$) {\n" + " output.writeEnum($number$, ((java.lang.Integer) $oneof_name$_));\n" + "}\n"); } -void ImmutableEnumOneofFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSize($number$, ((java.lang.Integer) $oneof_name$_));\n" - "}\n"); +void ImmutableEnumOneofFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($has_oneof_case_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .computeEnumSize($number$, ((java.lang.Integer) $oneof_name$_));\n" + "}\n"); } -void ImmutableEnumOneofFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "result = result && get$capitalized_name$Value()\n" - " == other.get$capitalized_name$Value();\n"); + printer->Print( + variables_, + "if (get$capitalized_name$Value()\n" + " != other.get$capitalized_name$Value()) return false;\n"); } else { - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); + printer->Print( + variables_, + "if (!get$capitalized_name$()\n" + " .equals(other.get$capitalized_name$())) return false;\n"); } } -void ImmutableEnumOneofFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateHashCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$Value();\n"); + "hash = (37 * hash) + $constant_name$;\n" + "hash = (53 * hash) + get$capitalized_name$Value();\n"); } else { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$().getNumber();\n"); + printer->Print( + variables_, + "hash = (37 * hash) + $constant_name$;\n" + "hash = (53 * hash) + get$capitalized_name$().getNumber();\n"); } } // =================================================================== -RepeatedImmutableEnumFieldGenerator:: -RepeatedImmutableEnumFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +RepeatedImmutableEnumFieldGenerator::RepeatedImmutableEnumFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetEnumVariables(descriptor, messageBitIndex, builderBitIndex, - context->GetFieldGeneratorInfo(descriptor), - name_resolver_, &variables_); + context->GetFieldGeneratorInfo(descriptor), name_resolver_, + &variables_); } RepeatedImmutableEnumFieldGenerator::~RepeatedImmutableEnumFieldGenerator() {} @@ -601,401 +607,427 @@ int RepeatedImmutableEnumFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void RepeatedImmutableEnumFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"); - WriteFieldDocComment(printer, descriptor_); +void RepeatedImmutableEnumFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print( + variables_, + "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$int get$capitalized_name$Count();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$java.util.List\n" - "get$capitalized_name$ValueList();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$java.util.List\n" + "get$capitalized_name$ValueList();\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Value(int index);\n"); + "$deprecation$int get$capitalized_name$Value(int index);\n"); } } -void RepeatedImmutableEnumFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private java.util.List $name$_;\n" - "private static final com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$> $name$_converter_ =\n" - " new com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$>() {\n" - " public $type$ convert(java.lang.Integer from) {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$(from);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " };\n"); +void RepeatedImmutableEnumFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print( + variables_, + "private java.util.List $name$_;\n" + "private static final " + "com.google.protobuf.Internal.ListAdapter.Converter<\n" + " java.lang.Integer, $type$> $name$_converter_ =\n" + " new com.google.protobuf.Internal.ListAdapter.Converter<\n" + " java.lang.Integer, $type$>() {\n" + " public $type$ convert(java.lang.Integer from) {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$(from);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " };\n"); PrintExtraFieldInfo(variables_, printer); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return new com.google.protobuf.Internal.ListAdapter<\n" - " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print( + variables_, + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return new com.google.protobuf.Internal.ListAdapter<\n" + " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_converter_.convert($name$_.get(index));\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_converter_.convert($name$_.get(index));\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$public java.util.List\n" - "${$get$capitalized_name$ValueList$}$() {\n" - " return $name$_;\n" - "}\n"); + "$deprecation$public java.util.List\n" + "${$get$capitalized_name$ValueList$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public int " + "${$get$capitalized_name$Value$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (descriptor_->is_packed()) { - printer->Print(variables_, - "private int $name$MemoizedSerializedSize;\n"); + printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n"); } } -void RepeatedImmutableEnumFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print(variables_, - // One field is the list and the other field keeps track of whether the - // list is immutable. If it's immutable, the invariant is that it must - // either an instance of Collections.emptyList() or it's an ArrayList - // wrapped in a Collections.unmodifiableList() wrapper and nobody else has - // a refererence to the underlying ArrayList. This invariant allows us to - // share instances of lists between protocol buffers avoiding expensive - // memory allocations. Note, immutable is a strong guarantee here -- not - // just that the list cannot be modified via the reference but that the - // list can never be modified. - "private java.util.List $name$_ =\n" - " java.util.Collections.emptyList();\n" - - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$get_mutable_bit_builder$) {\n" - " $name$_ = new java.util.ArrayList($name$_);\n" - " $set_mutable_bit_builder$;\n" - " }\n" - "}\n"); - - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - // Note: We return an unmodifiable list because otherwise the caller - // could hold on to the returned list and modify it after the message - // has been built, thus mutating the message which is supposed to be - // immutable. - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return new com.google.protobuf.Internal.ListAdapter<\n" - " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" - "}\n"); +void RepeatedImmutableEnumFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print( + variables_, + // One field is the list and the other field keeps track of whether the + // list is immutable. If it's immutable, the invariant is that it must + // either an instance of Collections.emptyList() or it's an ArrayList + // wrapped in a Collections.unmodifiableList() wrapper and nobody else has + // a refererence to the underlying ArrayList. This invariant allows us to + // share instances of lists between protocol buffers avoiding expensive + // memory allocations. Note, immutable is a strong guarantee here -- not + // just that the list cannot be modified via the reference but that the + // list can never be modified. + "private java.util.List $name$_ =\n" + " java.util.Collections.emptyList();\n" + + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$get_mutable_bit_builder$) {\n" + " $name$_ = new java.util.ArrayList($name$_);\n" + " $set_mutable_bit_builder$;\n" + " }\n" + "}\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print( + variables_, + // Note: We return an unmodifiable list because otherwise the caller + // could hold on to the returned list and modify it after the message + // has been built, thus mutating the message which is supposed to be + // immutable. + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return new com.google.protobuf.Internal.ListAdapter<\n" + " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_converter_.convert($name$_.get(index));\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_converter_.convert($name$_.get(index));\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value.getNumber());\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value.getNumber());\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value.getNumber());\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value.getNumber());\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " for ($type$ value : values) {\n" - " $name$_.add(value.getNumber());\n" - " }\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " for ($type$ value : values) {\n" + " $name$_.add(value.getNumber());\n" + " }\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $name$_ = java.util.Collections.emptyList();\n" - " $clear_mutable_bit_builder$;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $name$_ = java.util.Collections.emptyList();\n" + " $clear_mutable_bit_builder$;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$public java.util.List\n" - "${$get$capitalized_name$ValueList$}$() {\n" - " return java.util.Collections.unmodifiableList($name$_);\n" - "}\n"); + "$deprecation$public java.util.List\n" + "${$get$capitalized_name$ValueList$}$() {\n" + " return java.util.Collections.unmodifiableList($name$_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public int " + "${$get$capitalized_name$Value$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" - " int index, int value) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" + " int index, int value) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder " - "${$add$capitalized_name$Value$}$(int value) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$Value$}$(int value) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " for (int value : values) {\n" - " $name$_.add(value);\n" - " }\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_MULTI_ADDER, /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " for (int value : values) {\n" + " $name$_.add(value);\n" + " }\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } void RepeatedImmutableEnumFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for enums } -void RepeatedImmutableEnumFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = java.util.Collections.emptyList();\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = java.util.Collections.emptyList();\n" - "$clear_mutable_bit_builder$;\n"); + "$name$_ = java.util.Collections.emptyList();\n" + "$clear_mutable_bit_builder$;\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // The code below does two optimizations: // 1. If the other list is empty, there's nothing to do. This ensures we // don't allocate a new array if we already have an immutable one. // 2. If the other list is non-empty and our current list is empty, we can // reuse the other list which is guaranteed to be immutable. printer->Print(variables_, - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$_.isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " } else {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addAll(other.$name$_);\n" - " }\n" - " $on_changed$\n" - "}\n"); + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$_.isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " } else {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addAll(other.$name$_);\n" + " }\n" + " $on_changed$\n" + "}\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { // The code below ensures that the result has an immutable list. If our // list is immutable, we can just reuse it. If not, we make it immutable. - printer->Print(variables_, - "if ($get_mutable_bit_builder$) {\n" - " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" - " $clear_mutable_bit_builder$;\n" - "}\n" - "result.$name$_ = $name$_;\n"); + printer->Print( + variables_, + "if ($get_mutable_bit_builder$) {\n" + " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" + " $clear_mutable_bit_builder$;\n" + "}\n" + "result.$name$_ = $name$_;\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { // Read and store the enum if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = new java.util.ArrayList();\n" - " $set_mutable_bit_parser$;\n" - "}\n" - "$name$_.add(rawValue);\n"); + "int rawValue = input.readEnum();\n" + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = new java.util.ArrayList();\n" + " $set_mutable_bit_parser$;\n" + "}\n" + "$name$_.add(rawValue);\n"); } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "@SuppressWarnings(\"deprecation\")\n" - "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n" - " unknownFields.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " if (!$get_mutable_bit_parser$) {\n" - " $name$_ = new java.util.ArrayList();\n" - " $set_mutable_bit_parser$;\n" - " }\n" - " $name$_.add(rawValue);\n" - "}\n"); + printer->Print( + variables_, + "int rawValue = input.readEnum();\n" + "@SuppressWarnings(\"deprecation\")\n" + "$type$ value = $type$.$for_number$(rawValue);\n" + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" + "} else {\n" + " if (!$get_mutable_bit_parser$) {\n" + " $name$_ = new java.util.ArrayList();\n" + " $set_mutable_bit_parser$;\n" + " }\n" + " $name$_.add(rawValue);\n" + "}\n"); } } -void RepeatedImmutableEnumFieldGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateParsingCodeFromPacked( + io::Printer* printer) const { // Wrap GenerateParsingCode's contents with a while loop. printer->Print(variables_, - "int length = input.readRawVarint32();\n" - "int oldLimit = input.pushLimit(length);\n" - "while(input.getBytesUntilLimit() > 0) {\n"); + "int length = input.readRawVarint32();\n" + "int oldLimit = input.pushLimit(length);\n" + "while(input.getBytesUntilLimit() > 0) {\n"); printer->Indent(); GenerateParsingCode(printer); printer->Outdent(); printer->Print(variables_, - "}\n" - "input.popLimit(oldLimit);\n"); + "}\n" + "input.popLimit(oldLimit);\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($get_mutable_bit_parser$) {\n" - " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" - "}\n"); +void RepeatedImmutableEnumFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($get_mutable_bit_parser$) {\n" + " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" + "}\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { if (descriptor_->is_packed()) { printer->Print(variables_, - "if (get$capitalized_name$List().size() > 0) {\n" - " output.writeUInt32NoTag($tag$);\n" - " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" - "}\n" - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeEnumNoTag($name$_.get(i));\n" - "}\n"); + "if (get$capitalized_name$List().size() > 0) {\n" + " output.writeUInt32NoTag($tag$);\n" + " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" + "}\n" + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.writeEnumNoTag($name$_.get(i));\n" + "}\n"); } else { printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeEnum($number$, $name$_.get(i));\n" - "}\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.writeEnum($number$, $name$_.get(i));\n" + "}\n"); } } -void RepeatedImmutableEnumFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); + "{\n" + " int dataSize = 0;\n"); printer->Indent(); printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSizeNoTag($name$_.get(i));\n" - "}\n"); - printer->Print( - "size += dataSize;\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " dataSize += com.google.protobuf.CodedOutputStream\n" + " .computeEnumSizeNoTag($name$_.get(i));\n" + "}\n"); + printer->Print("size += dataSize;\n"); if (descriptor_->is_packed()) { printer->Print(variables_, - "if (!get$capitalized_name$List().isEmpty()) {" - " size += $tag_size$;\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeUInt32SizeNoTag(dataSize);\n" - "}"); + "if (!get$capitalized_name$List().isEmpty()) {" + " size += $tag_size$;\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .computeUInt32SizeNoTag(dataSize);\n" + "}"); } else { - printer->Print(variables_, - "size += $tag_size$ * $name$_.size();\n"); + printer->Print(variables_, "size += $tag_size$ * $name$_.size();\n"); } // cache the data size for packed fields. if (descriptor_->is_packed()) { - printer->Print(variables_, - "$name$MemoizedSerializedSize = dataSize;\n"); + printer->Print(variables_, "$name$MemoizedSerializedSize = dataSize;\n"); } printer->Outdent(); printer->Print("}\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { printer->Print(variables_, - "result = result && $name$_.equals(other.$name$_);\n"); + "if (!$name$_.equals(other.$name$_)) return false;\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateHashCode( + io::Printer* printer) const { printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + $name$_.hashCode();\n" - "}\n"); + "if (get$capitalized_name$Count() > 0) {\n" + " hash = (37 * hash) + $constant_name$;\n" + " hash = (53 * hash) + $name$_.hashCode();\n" + "}\n"); } -string RepeatedImmutableEnumFieldGenerator::GetBoxedType() const { +std::string RepeatedImmutableEnumFieldGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); } diff --git a/src/google/protobuf/compiler/java/java_enum_field.h b/src/google/protobuf/compiler/java/java_enum_field.h index 842da59270..95c7db578f 100644 --- a/src/google/protobuf/compiler/java/java_enum_field.h +++ b/src/google/protobuf/compiler/java/java_enum_field.h @@ -81,14 +81,11 @@ class ImmutableEnumFieldGenerator : public ImmutableFieldGenerator { void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - const int builderBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; private: @@ -142,14 +139,11 @@ class RepeatedImmutableEnumFieldGenerator : public ImmutableFieldGenerator { void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; private: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - const int builderBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutableEnumFieldGenerator); diff --git a/src/google/protobuf/compiler/java/java_enum_field_lite.cc b/src/google/protobuf/compiler/java/java_enum_field_lite.cc index 353a2f6e57..b3b4c3c842 100644 --- a/src/google/protobuf/compiler/java/java_enum_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_enum_field_lite.cc @@ -46,19 +46,25 @@ #include #include + namespace google { namespace protobuf { namespace compiler { namespace java { namespace { +bool EnableExperimentalRuntimeForLite() { +#ifdef PROTOBUF_EXPERIMENT + return PROTOBUF_EXPERIMENT; +#else // PROTOBUF_EXPERIMENT + return false; +#endif // !PROTOBUF_EXPERIMENT +} -void SetEnumVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - const FieldGeneratorInfo* info, +void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, + int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); (*variables)["type"] = @@ -67,15 +73,15 @@ void SetEnumVariables(const FieldDescriptor* descriptor, name_resolver->GetMutableClassName(descriptor->enum_type()); (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver); (*variables)["default_number"] = - SimpleItoa(descriptor->default_value_enum()->number()); - (*variables)["tag"] = SimpleItoa( + StrCat(descriptor->default_value_enum()->number()); + (*variables)["tag"] = StrCat( static_cast(internal::WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = SimpleItoa( + (*variables)["tag_size"] = StrCat( internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor))); // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (SupportFieldPresence(descriptor->file())) { @@ -94,8 +100,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor, (*variables)["clear_has_field_bit_message"] = ""; (*variables)["is_field_present_message"] = - (*variables)["name"] + "_ != " + - (*variables)["default"] + ".getNumber()"; + (*variables)["name"] + "_ != " + (*variables)["default"] + + ".getNumber()"; } // For repeated builders, the underlying list tracks mutability state. @@ -117,12 +123,12 @@ void SetEnumVariables(const FieldDescriptor* descriptor, // =================================================================== -ImmutableEnumFieldLiteGenerator:: -ImmutableEnumFieldLiteGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - context_(context), name_resolver_(context->GetNameResolver()) { +ImmutableEnumFieldLiteGenerator::ImmutableEnumFieldLiteGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { SetEnumVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); @@ -131,227 +137,170 @@ ImmutableEnumFieldLiteGenerator(const FieldDescriptor* descriptor, ImmutableEnumFieldLiteGenerator::~ImmutableEnumFieldLiteGenerator() {} int ImmutableEnumFieldLiteGenerator::GetNumBitsForMessage() const { - return 1; + return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } -void ImmutableEnumFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Value();\n"); + "$deprecation$int get$capitalized_name$Value();\n"); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutableEnumFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private int $name$_;\n"); +void ImmutableEnumFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private int $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return $name$_;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " $type$ result = $type$.forNumber($name$_);\n" - " return result == null ? $unknown$ : result;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " $type$ result = $type$.forNumber($name$_);\n" + " return result == null ? $unknown$ : result;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Generate private setters for the builder to proxy into. if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER); printer->Print(variables_, - "private void set$capitalized_name$Value(int value) {\n" - " $set_has_field_bit_message$" - " $name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$Value(int value) {\n" + " $set_has_field_bit_message$" + " $name$_ = value;\n" + "}\n"); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $set_has_field_bit_message$\n" - " $name$_ = value.getNumber();\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); + "private void set$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $set_has_field_bit_message$\n" + " $name$_ = value.getNumber();\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $clear_has_field_bit_message$\n" - " $name$_ = $default_number$;\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " $clear_has_field_bit_message$\n" + " $name$_ = $default_number$;\n" + "}\n"); } -void ImmutableEnumFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableEnumFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return instance.get$capitalized_name$Value();\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return instance.get$capitalized_name$Value();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder " - "${$set$capitalized_name$Value$}$(int value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Value(value);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Value(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for enums -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutableEnumFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { if (!IsDefaultValueJavaDefault(descriptor_)) { printer->Print(variables_, "$name$_ = $default_number$;\n"); } } -void ImmutableEnumFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { +void ImmutableEnumFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "$name$_ = visitor.visitInt(has$capitalized_name$(), $name$_,\n" - " other.has$capitalized_name$(), other.$name$_);\n"); - } else if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "$name$_ = visitor.visitInt($name$_ != $default_number$, $name$_," - " other.$name$_ != $default_number$, other.$name$_);\n"); - } else { - GOOGLE_LOG(FATAL) << "Can't reach here."; + WriteIntToUtf16CharSequence(messageBitIndex_, output); } -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - // noop for scalars -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$set_has_field_bit_message$\n" - "$name$_ = rawValue;\n"); - } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n" - " super.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $set_has_field_bit_message$\n" - " $name$_ = rawValue;\n" - "}\n"); + printer->Print(variables_, "\"$name$_\",\n"); + if (SupportFieldPresence(descriptor_->file())) { + PrintEnumVerifierLogic(printer, descriptor_, variables_, + /*var_name=*/"$type$", + /*terminating_string=*/",\n", + /*enforce_lite=*/context_->EnforceLite()); } } -void ImmutableEnumFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // noop for enums -} - - -void ImmutableEnumFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.writeEnum($number$, $name$_);\n" - "}\n"); -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSize($number$, $name$_);\n" - "}\n"); -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && $name$_ == other.$name$_;\n"); -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + $name$_;\n"); -} - -string ImmutableEnumFieldLiteGenerator::GetBoxedType() const { +std::string ImmutableEnumFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); } @@ -365,196 +314,148 @@ ImmutableEnumOneofFieldLiteGenerator::ImmutableEnumOneofFieldLiteGenerator( SetCommonOneofVariables(descriptor, info, &variables_); } -ImmutableEnumOneofFieldLiteGenerator:: -~ImmutableEnumOneofFieldLiteGenerator() {} +ImmutableEnumOneofFieldLiteGenerator::~ImmutableEnumOneofFieldLiteGenerator() {} -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableEnumOneofFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return (java.lang.Integer) $oneof_name$_;\n" - " }\n" - " return $default_number$;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return (java.lang.Integer) $oneof_name$_;\n" + " }\n" + " return $default_number$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $type$ result = $type$.forNumber((java.lang.Integer) $oneof_name$_);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " return $default$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $type$ result = $type$.forNumber((java.lang.Integer) " + "$oneof_name$_);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Generate private setters for the builder to proxy into. if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER); printer->Print(variables_, - "private void set$capitalized_name$Value(int value) {\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$Value(int value) {\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + "}\n"); + } + WriteFieldAccessorDocComment(printer, descriptor_, SETTER); + printer->Print(variables_, + "private void set$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value.getNumber();\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); + printer->Print(variables_, + "private void clear$capitalized_name$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " }\n" + "}\n"); +} + +void ImmutableEnumOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output); + if (SupportFieldPresence(descriptor_->file())) { + PrintEnumVerifierLogic(printer, descriptor_, variables_, + /*var_name=*/"$type$", + /*terminating_string=*/",\n", + /*enforce_lite=*/context_->EnforceLite()); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value.getNumber();\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " }\n" - "}\n"); } - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableEnumOneofFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return instance.get$capitalized_name$Value();\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return instance.get$capitalized_name$Value();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder " - "${$set$capitalized_name$Value$}$(int value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Value(value);\n" - " return this;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Value(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$oneof_name$_ = visitor.visitOneofInt(\n" - " $has_oneof_case_message$, $oneof_name$_, other.$oneof_name$_);\n"); -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = rawValue;\n"); - } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n" - " super.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = rawValue;\n" + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" "}\n"); - } -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.writeEnum($number$, ((java.lang.Integer) $oneof_name$_));\n" - "}\n"); -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSize($number$, ((java.lang.Integer) $oneof_name$_));\n" - "}\n"); -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "result = result && get$capitalized_name$Value()\n" - " == other.get$capitalized_name$Value();\n"); - } else { - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); - } -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$Value();\n"); - } else { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$().getNumber();\n"); - } + printer->Annotate("{", "}", descriptor_); } // =================================================================== @@ -564,7 +465,6 @@ RepeatedImmutableEnumFieldLiteGenerator:: int messageBitIndex, Context* context) : descriptor_(descriptor), - messageBitIndex_(messageBitIndex), context_(context), name_resolver_(context->GetNameResolver()) { SetEnumVariables(descriptor, messageBitIndex, 0, @@ -573,432 +473,316 @@ RepeatedImmutableEnumFieldLiteGenerator:: } RepeatedImmutableEnumFieldLiteGenerator:: -~RepeatedImmutableEnumFieldLiteGenerator() {} + ~RepeatedImmutableEnumFieldLiteGenerator() {} int RepeatedImmutableEnumFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"); - WriteFieldDocComment(printer, descriptor_); +void RepeatedImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print( + variables_, + "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$int get$capitalized_name$Count();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$java.util.List\n" - "get$capitalized_name$ValueList();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$java.util.List\n" + "get$capitalized_name$ValueList();\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Value(int index);\n"); + "$deprecation$int get$capitalized_name$Value(int index);\n"); } } -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private com.google.protobuf.Internal.IntList $name$_;\n" - "private static final com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$> $name$_converter_ =\n" - " new com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$>() {\n" - " @java.lang.Override\n" - " public $type$ convert(java.lang.Integer from) {\n" - " $type$ result = $type$.forNumber(from);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " };\n"); +void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print( + variables_, + "private com.google.protobuf.Internal.IntList $name$_;\n" + "private static final " + "com.google.protobuf.Internal.ListAdapter.Converter<\n" + " java.lang.Integer, $type$> $name$_converter_ =\n" + " new com.google.protobuf.Internal.ListAdapter.Converter<\n" + " java.lang.Integer, $type$>() {\n" + " @java.lang.Override\n" + " public $type$ convert(java.lang.Integer from) {\n" + " $type$ result = $type$.forNumber(from);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " };\n"); PrintExtraFieldInfo(variables_, printer); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return new com.google.protobuf.Internal.ListAdapter<\n" - " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return new com.google.protobuf.Internal.ListAdapter<\n" + " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_converter_.convert($name$_.getInt(index));\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_converter_.convert($name$_.getInt(index));\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List\n" - "${$get$capitalized_name$ValueList$}$() {\n" - " return $name$_;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List\n" + "${$get$capitalized_name$ValueList$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" - " return $name$_.getInt(index);\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public int " + "${$get$capitalized_name$Value$}$(int index) {\n" + " return $name$_.getInt(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - if (descriptor_->is_packed() && + if (!EnableExperimentalRuntimeForLite() && descriptor_->is_packed() && context_->HasGeneratedMethods(descriptor_->containing_type())) { - printer->Print(variables_, - "private int $name$MemoizedSerializedSize;\n"); + printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n"); } // Generate private setters for the builder to proxy into. - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - " }\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, $type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.setInt(index, value.getNumber());\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void add$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addInt(value.getNumber());\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void addAll$capitalized_name$(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " for ($type$ value : values) {\n" - " $name$_.addInt(value.getNumber());\n" - " }\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $name$_ = emptyIntList();\n" - "}\n"); - - if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void set$capitalized_name$Value(\n" - " int index, int value) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.setInt(index, value);\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void add$capitalized_name$Value(int value) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addInt(value);\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void addAll$capitalized_name$Value(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " for (int value : values) {\n" - " $name$_.addInt(value);\n" + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$is_mutable$) {\n" + " $name$_ =\n" + " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" " }\n" "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER); + printer->Print(variables_, + "private void set$capitalized_name$(\n" + " int index, $type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.setInt(index, value.getNumber());\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER); + printer->Print(variables_, + "private void add$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addInt(value.getNumber());\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER); + printer->Print(variables_, + "private void addAll$capitalized_name$(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " for ($type$ value : values) {\n" + " $name$_.addInt(value.getNumber());\n" + " }\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); + printer->Print(variables_, + "private void clear$capitalized_name$() {\n" + " $name$_ = emptyIntList();\n" + "}\n"); + + if (SupportUnknownEnumValue(descriptor_->file())) { + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER); + printer->Print(variables_, + "private void set$capitalized_name$Value(\n" + " int index, int value) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.setInt(index, value);\n" + "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_ADDER); + printer->Print(variables_, + "private void add$capitalized_name$Value(int value) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addInt(value);\n" + "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_MULTI_ADDER); + printer->Print(variables_, + "private void addAll$capitalized_name$Value(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " for (int value : values) {\n" + " $name$_.addInt(value);\n" + " }\n" + "}\n"); } } +void RepeatedImmutableEnumFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + printer->Print(variables_, "\"$name$_\",\n"); + if (SupportFieldPresence(descriptor_->file())) { + PrintEnumVerifierLogic(printer, descriptor_, variables_, + /*var_name=*/"$type$", + /*terminating_string=*/",\n", + /*enforce_lite=*/context_->EnforceLite()); + } +} -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); +void RepeatedImmutableEnumFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return instance.get$capitalized_name$List();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return instance.get$capitalized_name$List();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Count();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Count();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return instance.get$capitalized_name$(index);\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return instance.get$capitalized_name$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$(values);" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$(values);" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List\n" - "${$get$capitalized_name$ValueList$}$() {\n" - " return java.util.Collections.unmodifiableList(\n" - " instance.get$capitalized_name$ValueList());\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public java.util.List\n" + "${$get$capitalized_name$ValueList$}$() {\n" + " return java.util.Collections.unmodifiableList(\n" + " instance.get$capitalized_name$ValueList());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" - " return instance.get$capitalized_name$Value(index);\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public int " + "${$get$capitalized_name$Value$}$(int index) {\n" + " return instance.get$capitalized_name$Value(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" - " int index, int value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Value(index, value);\n" - " return this;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_INDEXED_SETTER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" + " int index, int value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Value(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder " - "${$add$capitalized_name$Value$}$(int value) {\n" - " instance.add$capitalized_name$Value(value);\n" - " return this;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$add$capitalized_name$Value$}$(int value) {\n" + " instance.add$capitalized_name$Value(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" - " java.lang.Iterable values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$Value(values);\n" - " return this;\n" - "}\n"); + WriteFieldEnumValueAccessorDocComment(printer, descriptor_, + LIST_MULTI_ADDER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" + " java.lang.Iterable values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$Value(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for enums -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = emptyIntList();\n"); } -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_= visitor.visitIntList($name$_, other.$name$_);\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - // Read and store the enum - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n"); - - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "$name$_.addInt(input.readEnum());\n"); - } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n" - // We store the unknown value in unknown fields. - " super.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $name$_.addInt(rawValue);\n" - "}\n"); - } -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n"); - - printer->Print(variables_, - "int length = input.readRawVarint32();\n" - "int oldLimit = input.pushLimit(length);\n" - "while(input.getBytesUntilLimit() > 0) {\n"); - printer->Indent(); - - // Read and store the enum - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "$name$_.addInt(input.readEnum());\n"); - } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n" - // We store the unknown value in unknown fields. - " super.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $name$_.addInt(rawValue);\n" - "}\n"); - } - - printer->Outdent(); - printer->Print(variables_, - "}\n" - "input.popLimit(oldLimit);\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_mutable$) {\n" - " $name$_.makeImmutable();\n" - "}\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - if (descriptor_->is_packed()) { - printer->Print(variables_, - "if (get$capitalized_name$List().size() > 0) {\n" - " output.writeUInt32NoTag($tag$);\n" - " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" - "}\n" - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeEnumNoTag($name$_.getInt(i));\n" - "}\n"); - } else { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeEnum($number$, $name$_.getInt(i));\n" - "}\n"); - } -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); - printer->Indent(); - - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSizeNoTag($name$_.getInt(i));\n" - "}\n"); - printer->Print( - "size += dataSize;\n"); - if (descriptor_->is_packed()) { - printer->Print(variables_, - "if (!get$capitalized_name$List().isEmpty()) {" - " size += $tag_size$;\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeUInt32SizeNoTag(dataSize);\n" - "}"); - } else { - printer->Print(variables_, - "size += $tag_size$ * $name$_.size();\n"); - } - - // cache the data size for packed fields. - if (descriptor_->is_packed()) { - printer->Print(variables_, - "$name$MemoizedSerializedSize = dataSize;\n"); - } - - printer->Outdent(); - printer->Print("}\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && $name$_.equals(other.$name$_);\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + $name$_.hashCode();\n" - "}\n"); -} - -string RepeatedImmutableEnumFieldLiteGenerator::GetBoxedType() const { +std::string RepeatedImmutableEnumFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); } diff --git a/src/google/protobuf/compiler/java/java_enum_field_lite.h b/src/google/protobuf/compiler/java/java_enum_field_lite.h index 6bda426fb2..b5e9807728 100644 --- a/src/google/protobuf/compiler/java/java_enum_field_lite.h +++ b/src/google/protobuf/compiler/java/java_enum_field_lite.h @@ -69,21 +69,14 @@ class ImmutableEnumFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - - string GetBoxedType() const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; + + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; + std::map variables_; const int messageBitIndex_; Context* context_; ClassNameResolver* name_resolver_; @@ -101,12 +94,8 @@ class ImmutableEnumOneofFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableEnumOneofFieldLiteGenerator); @@ -125,23 +114,14 @@ class RepeatedImmutableEnumFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingCodeFromPacked(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - - string GetBoxedType() const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; + + std::string GetBoxedType() const; private: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; + std::map variables_; Context* context_; ClassNameResolver* name_resolver_; diff --git a/src/google/protobuf/compiler/java/java_enum_lite.cc b/src/google/protobuf/compiler/java/java_enum_lite.cc index deebe523c6..f8e0653d23 100644 --- a/src/google/protobuf/compiler/java/java_enum_lite.cc +++ b/src/google/protobuf/compiler/java/java_enum_lite.cc @@ -43,6 +43,7 @@ #include #include #include + #include namespace google { @@ -59,7 +60,7 @@ EnumLiteGenerator::EnumLiteGenerator(const EnumDescriptor* descriptor, for (int i = 0; i < descriptor_->value_count(); i++) { const EnumValueDescriptor* value = descriptor_->value(i); const EnumValueDescriptor* canonical_value = - descriptor_->FindValueByNumber(value->number()); + descriptor_->FindValueByNumber(value->number()); if (value == canonical_value) { canonical_values_.push_back(value); @@ -85,15 +86,14 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { printer->Indent(); for (int i = 0; i < canonical_values_.size(); i++) { - std::map vars; + std::map vars; vars["name"] = canonical_values_[i]->name(); - vars["number"] = SimpleItoa(canonical_values_[i]->number()); + vars["number"] = StrCat(canonical_values_[i]->number()); WriteEnumValueDocComment(printer, canonical_values_[i]); if (canonical_values_[i]->options().deprecated()) { printer->Print("@java.lang.Deprecated\n"); } - printer->Print(vars, - "$name$($number$),\n"); + printer->Print(vars, "$name$($number$),\n"); printer->Annotate("name", canonical_values_[i]); } @@ -103,31 +103,31 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { } printer->Print( - ";\n" - "\n"); + ";\n" + "\n"); // ----------------------------------------------------------------- for (int i = 0; i < aliases_.size(); i++) { - std::map vars; + std::map vars; vars["classname"] = descriptor_->name(); vars["name"] = aliases_[i].value->name(); vars["canonical_name"] = aliases_[i].canonical_value->name(); WriteEnumValueDocComment(printer, aliases_[i].value); - printer->Print(vars, - "public static final $classname$ $name$ = $canonical_name$;\n"); + printer->Print( + vars, "public static final $classname$ $name$ = $canonical_name$;\n"); printer->Annotate("name", aliases_[i].value); } for (int i = 0; i < descriptor_->value_count(); i++) { - std::map vars; + std::map vars; vars["name"] = descriptor_->value(i)->name(); - vars["number"] = SimpleItoa(descriptor_->value(i)->number()); + vars["number"] = StrCat(descriptor_->value(i)->number()); vars["{"] = ""; vars["}"] = ""; WriteEnumValueDocComment(printer, descriptor_->value(i)); printer->Print(vars, - "public static final int ${$$name$_VALUE$}$ = $number$;\n"); + "public static final int ${$$name$_VALUE$}$ = $number$;\n"); printer->Annotate("{", "}", descriptor_->value(i)); } printer->Print("\n"); @@ -150,6 +150,8 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { "}\n" "\n" "/**\n" + " * @param value The number of the enum to look for.\n" + " * @return The enum associated with the given number.\n" " * @deprecated Use {@link #forNumber(int)} instead.\n" " */\n" "@java.lang.Deprecated\n" @@ -166,7 +168,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { for (int i = 0; i < canonical_values_.size(); i++) { printer->Print("case $number$: return $name$;\n", "name", canonical_values_[i]->name(), "number", - SimpleItoa(canonical_values_[i]->number())); + StrCat(canonical_values_[i]->number())); } printer->Outdent(); @@ -207,34 +209,22 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { "classname", descriptor_->name()); printer->Print( - "private final int value;\n\n" - "private $classname$(int value) {\n", - "classname", descriptor_->name()); + "private final int value;\n\n" + "private $classname$(int value) {\n", + "classname", descriptor_->name()); printer->Print( - " this.value = value;\n" - "}\n"); + " this.value = value;\n" + "}\n"); printer->Print( - "\n" - "// @@protoc_insertion_point(enum_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(enum_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n\n"); } -bool EnumLiteGenerator::CanUseEnumValues() { - if (canonical_values_.size() != descriptor_->value_count()) { - return false; - } - for (int i = 0; i < descriptor_->value_count(); i++) { - if (descriptor_->value(i)->name() != canonical_values_[i]->name()) { - return false; - } - } - return true; -} - } // namespace java } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/java/java_enum_lite.h b/src/google/protobuf/compiler/java/java_enum_lite.h index 9e20e6fb71..50f3fe7b1a 100644 --- a/src/google/protobuf/compiler/java/java_enum_lite.h +++ b/src/google/protobuf/compiler/java/java_enum_lite.h @@ -42,13 +42,13 @@ namespace google { namespace protobuf { namespace compiler { - namespace java { - class Context; // context.h - class ClassNameResolver; // name_resolver.h - } -} +namespace java { +class Context; // context.h +class ClassNameResolver; // name_resolver.h +} // namespace java +} // namespace compiler namespace io { - class Printer; // printer.h +class Printer; // printer.h } } // namespace protobuf } // namespace google @@ -87,8 +87,6 @@ class EnumLiteGenerator { Context* context_; ClassNameResolver* name_resolver_; - bool CanUseEnumValues(); - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumLiteGenerator); }; diff --git a/src/google/protobuf/compiler/java/java_extension.cc b/src/google/protobuf/compiler/java/java_extension.cc index 1ee9771046..3ad64e061b 100644 --- a/src/google/protobuf/compiler/java/java_extension.cc +++ b/src/google/protobuf/compiler/java/java_extension.cc @@ -41,6 +41,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -48,11 +49,10 @@ namespace java { ImmutableExtensionGenerator::ImmutableExtensionGenerator( const FieldDescriptor* descriptor, Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()) { + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { if (descriptor_->extension_scope() != NULL) { - scope_ = name_resolver_->GetImmutableClassName( - descriptor_->extension_scope()); + scope_ = + name_resolver_->GetImmutableClassName(descriptor_->extension_scope()); } else { scope_ = name_resolver_->GetImmutableClassName(descriptor_->file()); } @@ -62,34 +62,36 @@ ImmutableExtensionGenerator::~ImmutableExtensionGenerator() {} // Initializes the vars referenced in the generated code templates. void ExtensionGenerator::InitTemplateVars( - const FieldDescriptor* descriptor, const string& scope, bool immutable, - ClassNameResolver* name_resolver, std::map* vars_pointer) { - std::map &vars = *vars_pointer; + const FieldDescriptor* descriptor, const std::string& scope, bool immutable, + ClassNameResolver* name_resolver, + std::map* vars_pointer) { + std::map& vars = *vars_pointer; vars["scope"] = scope; - vars["name"] = UnderscoresToCamelCase(descriptor); + vars["name"] = UnderscoresToCamelCaseCheckReserved(descriptor); vars["containing_type"] = name_resolver->GetClassName(descriptor->containing_type(), immutable); - vars["number"] = SimpleItoa(descriptor->number()); + vars["number"] = StrCat(descriptor->number()); vars["constant_name"] = FieldConstantName(descriptor); - vars["index"] = SimpleItoa(descriptor->index()); - vars["default"] = descriptor->is_repeated() ? - "" : DefaultValue(descriptor, immutable, name_resolver); + vars["index"] = StrCat(descriptor->index()); + vars["default"] = descriptor->is_repeated() + ? "" + : DefaultValue(descriptor, immutable, name_resolver); vars["type_constant"] = FieldTypeName(GetType(descriptor)); vars["packed"] = descriptor->is_packed() ? "true" : "false"; vars["enum_map"] = "null"; vars["prototype"] = "null"; JavaType java_type = GetJavaType(descriptor); - string singular_type; + std::string singular_type; switch (java_type) { case JAVATYPE_MESSAGE: - singular_type = name_resolver->GetClassName(descriptor->message_type(), - immutable); + singular_type = + name_resolver->GetClassName(descriptor->message_type(), immutable); vars["prototype"] = singular_type + ".getDefaultInstance()"; break; case JAVATYPE_ENUM: - singular_type = name_resolver->GetClassName(descriptor->enum_type(), - immutable); + singular_type = + name_resolver->GetClassName(descriptor->enum_type(), immutable); vars["enum_map"] = singular_type + ".internalGetValueMap()"; break; case JAVATYPE_STRING: @@ -102,18 +104,18 @@ void ExtensionGenerator::InitTemplateVars( singular_type = BoxedPrimitiveTypeName(java_type); break; } - vars["type"] = descriptor->is_repeated() ? - "java.util.List<" + singular_type + ">" : singular_type; + vars["type"] = descriptor->is_repeated() + ? "java.util.List<" + singular_type + ">" + : singular_type; vars["singular_type"] = singular_type; } void ImmutableExtensionGenerator::Generate(io::Printer* printer) { - std::map vars; + std::map vars; const bool kUseImmutableNames = true; InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_, &vars); - printer->Print(vars, - "public static final int $constant_name$ = $number$;\n"); + printer->Print(vars, "public static final int $constant_name$ = $number$;\n"); WriteFieldDocComment(printer, descriptor_); if (descriptor_->extension_scope() == NULL) { @@ -151,8 +153,8 @@ int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode( // Only applies to non-nested extensions. printer->Print( "$name$.internalInit(descriptor.getExtensions().get($index$));\n", - "name", UnderscoresToCamelCase(descriptor_), "index", - SimpleItoa(descriptor_->index())); + "name", UnderscoresToCamelCaseCheckReserved(descriptor_), "index", + StrCat(descriptor_->index())); bytecode_estimate += 21; } return bytecode_estimate; @@ -160,10 +162,8 @@ int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode( int ImmutableExtensionGenerator::GenerateRegistrationCode( io::Printer* printer) { - printer->Print( - "registry.add($scope$.$name$);\n", - "scope", scope_, - "name", UnderscoresToCamelCase(descriptor_)); + printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name", + UnderscoresToCamelCaseCheckReserved(descriptor_)); return 7; } diff --git a/src/google/protobuf/compiler/java/java_extension.h b/src/google/protobuf/compiler/java/java_extension.h index 7a58373f35..ab87890771 100644 --- a/src/google/protobuf/compiler/java/java_extension.h +++ b/src/google/protobuf/compiler/java/java_extension.h @@ -79,10 +79,10 @@ class ExtensionGenerator { virtual int GenerateRegistrationCode(io::Printer* printer) = 0; protected: - static void InitTemplateVars(const FieldDescriptor* descriptor, - const string& scope, bool immutable, - ClassNameResolver* name_resolver, - std::map* vars_pointer); + static void InitTemplateVars( + const FieldDescriptor* descriptor, const std::string& scope, + bool immutable, ClassNameResolver* name_resolver, + std::map* vars_pointer); private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionGenerator); @@ -100,9 +100,8 @@ class ImmutableExtensionGenerator : public ExtensionGenerator { protected: const FieldDescriptor* descriptor_; - Context* context_; ClassNameResolver* name_resolver_; - string scope_; + std::string scope_; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableExtensionGenerator); diff --git a/src/google/protobuf/compiler/java/java_extension_lite.cc b/src/google/protobuf/compiler/java/java_extension_lite.cc index 70ce8e7d1f..71bf4e23f3 100644 --- a/src/google/protobuf/compiler/java/java_extension_lite.cc +++ b/src/google/protobuf/compiler/java/java_extension_lite.cc @@ -44,11 +44,10 @@ namespace java { ImmutableExtensionLiteGenerator::ImmutableExtensionLiteGenerator( const FieldDescriptor* descriptor, Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()) { + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { if (descriptor_->extension_scope() != NULL) { - scope_ = name_resolver_->GetImmutableClassName( - descriptor_->extension_scope()); + scope_ = + name_resolver_->GetImmutableClassName(descriptor_->extension_scope()); } else { scope_ = name_resolver_->GetImmutableClassName(descriptor_->file()); } @@ -57,12 +56,11 @@ ImmutableExtensionLiteGenerator::ImmutableExtensionLiteGenerator( ImmutableExtensionLiteGenerator::~ImmutableExtensionLiteGenerator() {} void ImmutableExtensionLiteGenerator::Generate(io::Printer* printer) { - std::map vars; + std::map vars; const bool kUseImmutableNames = true; InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_, &vars); - printer->Print(vars, - "public static final int $constant_name$ = $number$;\n"); + printer->Print(vars, "public static final int $constant_name$ = $number$;\n"); WriteFieldDocComment(printer, descriptor_); if (descriptor_->is_repeated()) { @@ -106,10 +104,8 @@ int ImmutableExtensionLiteGenerator::GenerateNonNestedInitializationCode( int ImmutableExtensionLiteGenerator::GenerateRegistrationCode( io::Printer* printer) { - printer->Print( - "registry.add($scope$.$name$);\n", - "scope", scope_, - "name", UnderscoresToCamelCase(descriptor_)); + printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name", + UnderscoresToCamelCaseCheckReserved(descriptor_)); return 7; } diff --git a/src/google/protobuf/compiler/java/java_extension_lite.h b/src/google/protobuf/compiler/java/java_extension_lite.h index 34716f9d84..410781dd33 100644 --- a/src/google/protobuf/compiler/java/java_extension_lite.h +++ b/src/google/protobuf/compiler/java/java_extension_lite.h @@ -61,9 +61,8 @@ class ImmutableExtensionLiteGenerator : public ExtensionGenerator { private: const FieldDescriptor* descriptor_; - Context* context_; ClassNameResolver* name_resolver_; - string scope_; + std::string scope_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableExtensionLiteGenerator); }; diff --git a/src/google/protobuf/compiler/java/java_field.cc b/src/google/protobuf/compiler/java/java_field.cc index fec20a7f35..18a86bad04 100644 --- a/src/google/protobuf/compiler/java/java_field.cc +++ b/src/google/protobuf/compiler/java/java_field.cc @@ -55,6 +55,7 @@ #include + namespace google { namespace protobuf { namespace compiler { @@ -62,15 +63,16 @@ namespace java { namespace { -ImmutableFieldGenerator* MakeImmutableGenerator( - const FieldDescriptor* field, int messageBitIndex, int builderBitIndex, - Context* context) { +ImmutableFieldGenerator* MakeImmutableGenerator(const FieldDescriptor* field, + int messageBitIndex, + int builderBitIndex, + Context* context) { if (field->is_repeated()) { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: if (IsMapEntry(field->message_type())) { - return new ImmutableMapFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableMapFieldGenerator(field, messageBitIndex, + builderBitIndex, context); } else { return new RepeatedImmutableMessageFieldGenerator( field, messageBitIndex, builderBitIndex, context); @@ -92,8 +94,8 @@ ImmutableFieldGenerator* MakeImmutableGenerator( return new ImmutableMessageOneofFieldGenerator( field, messageBitIndex, builderBitIndex, context); case JAVATYPE_ENUM: - return new ImmutableEnumOneofFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableEnumOneofFieldGenerator(field, messageBitIndex, + builderBitIndex, context); case JAVATYPE_STRING: return new ImmutableStringOneofFieldGenerator( field, messageBitIndex, builderBitIndex, context); @@ -104,17 +106,17 @@ ImmutableFieldGenerator* MakeImmutableGenerator( } else { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: - return new ImmutableMessageFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableMessageFieldGenerator(field, messageBitIndex, + builderBitIndex, context); case JAVATYPE_ENUM: - return new ImmutableEnumFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableEnumFieldGenerator(field, messageBitIndex, + builderBitIndex, context); case JAVATYPE_STRING: - return new ImmutableStringFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableStringFieldGenerator(field, messageBitIndex, + builderBitIndex, context); default: - return new ImmutablePrimitiveFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutablePrimitiveFieldGenerator(field, messageBitIndex, + builderBitIndex, context); } } } @@ -192,18 +194,13 @@ static inline void ReportUnexpectedPackedFieldsCall(io::Printer* printer) { ImmutableFieldGenerator::~ImmutableFieldGenerator() {} -void ImmutableFieldGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { +void ImmutableFieldGenerator::GenerateParsingCodeFromPacked( + io::Printer* printer) const { ReportUnexpectedPackedFieldsCall(printer); } ImmutableFieldLiteGenerator::~ImmutableFieldLiteGenerator() {} -void ImmutableFieldLiteGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { - ReportUnexpectedPackedFieldsCall(printer); -} - // =================================================================== template <> @@ -223,7 +220,7 @@ FieldGeneratorMap::FieldGeneratorMap( } } -template<> +template <> FieldGeneratorMap::~FieldGeneratorMap() {} template <> @@ -241,20 +238,20 @@ FieldGeneratorMap::FieldGeneratorMap( } } -template<> +template <> FieldGeneratorMap::~FieldGeneratorMap() {} void SetCommonFieldVariables(const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, - std::map* variables) { + std::map* variables) { (*variables)["field_name"] = descriptor->name(); (*variables)["name"] = info->name; (*variables)["classname"] = descriptor->containing_type()->name(); (*variables)["capitalized_name"] = info->capitalized_name; (*variables)["disambiguated_reason"] = info->disambiguated_reason; (*variables)["constant_name"] = FieldConstantName(descriptor); - (*variables)["number"] = SimpleItoa(descriptor->number()); + (*variables)["number"] = StrCat(descriptor->number()); // These variables are placeholders to pick out the beginning and ends of // identifiers for annotations (when doing so with existing variables would // be ambiguous or impossible). They should never be set to anything but the @@ -265,22 +262,22 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, void SetCommonOneofVariables(const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, - std::map* variables) { + std::map* variables) { (*variables)["oneof_name"] = info->name; (*variables)["oneof_capitalized_name"] = info->capitalized_name; (*variables)["oneof_index"] = - SimpleItoa(descriptor->containing_oneof()->index()); + StrCat(descriptor->containing_oneof()->index()); + (*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor); (*variables)["set_oneof_case_message"] = - info->name + "Case_ = " + SimpleItoa(descriptor->number()); - (*variables)["clear_oneof_case_message"] = info->name + - "Case_ = 0"; + info->name + "Case_ = " + StrCat(descriptor->number()); + (*variables)["clear_oneof_case_message"] = info->name + "Case_ = 0"; (*variables)["has_oneof_case_message"] = - info->name + "Case_ == " + SimpleItoa(descriptor->number()); + info->name + "Case_ == " + StrCat(descriptor->number()); } -void PrintExtraFieldInfo(const std::map& variables, +void PrintExtraFieldInfo(const std::map& variables, io::Printer* printer) { - const std::map::const_iterator it = + const std::map::const_iterator it = variables.find("disambiguated_reason"); if (it != variables.end() && !it->second.empty()) { printer->Print( diff --git a/src/google/protobuf/compiler/java/java_field.h b/src/google/protobuf/compiler/java/java_field.h index 881427d78c..9d04dc8455 100644 --- a/src/google/protobuf/compiler/java/java_field.h +++ b/src/google/protobuf/compiler/java/java_field.h @@ -39,9 +39,9 @@ #include #include +#include #include #include -#include namespace google { namespace protobuf { @@ -87,7 +87,7 @@ class ImmutableFieldGenerator { virtual void GenerateEqualsCode(io::Printer* printer) const = 0; virtual void GenerateHashCode(io::Printer* printer) const = 0; - virtual string GetBoxedType() const = 0; + virtual std::string GetBoxedType() const = 0; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldGenerator); @@ -103,22 +103,10 @@ class ImmutableFieldLiteGenerator { virtual void GenerateMembers(io::Printer* printer) const = 0; virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; virtual void GenerateInitializationCode(io::Printer* printer) const = 0; - virtual void GenerateVisitCode(io::Printer* printer) const = 0; - virtual void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) - const = 0; - virtual void GenerateParsingCode(io::Printer* printer) const = 0; - virtual void GenerateParsingCodeFromPacked(io::Printer* printer) const; - virtual void GenerateParsingDoneCode(io::Printer* printer) const = 0; - virtual void GenerateSerializationCode(io::Printer* printer) const = 0; - virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0; - virtual void GenerateFieldBuilderInitializationCode(io::Printer* printer) - const = 0; + virtual void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const = 0; - virtual void GenerateEqualsCode(io::Printer* printer) const = 0; - virtual void GenerateHashCode(io::Printer* printer) const = 0; - - - virtual string GetBoxedType() const = 0; + virtual std::string GetBoxedType() const = 0; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldLiteGenerator); @@ -126,38 +114,34 @@ class ImmutableFieldLiteGenerator { // Convenience class which constructs FieldGenerators for a Descriptor. -template +template class FieldGeneratorMap { public: - explicit FieldGeneratorMap(const Descriptor* descriptor, - Context* context); + explicit FieldGeneratorMap(const Descriptor* descriptor, Context* context); ~FieldGeneratorMap(); const FieldGeneratorType& get(const FieldDescriptor* field) const; private: const Descriptor* descriptor_; - Context* context_; - ClassNameResolver* name_resolver_; std::vector> field_generators_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); }; -template -inline const FieldGeneratorType& -FieldGeneratorMap::get(const FieldDescriptor* field) const { +template +inline const FieldGeneratorType& FieldGeneratorMap::get( + const FieldDescriptor* field) const { GOOGLE_CHECK_EQ(field->containing_type(), descriptor_); return *field_generators_[field->index()]; } // Instantiate template for mutable and immutable maps. -template<> -FieldGeneratorMap:: -FieldGeneratorMap(const Descriptor* descriptor, - Context* context); +template <> +FieldGeneratorMap::FieldGeneratorMap( + const Descriptor* descriptor, Context* context); -template<> +template <> FieldGeneratorMap::~FieldGeneratorMap(); @@ -171,29 +155,29 @@ FieldGeneratorMap::~FieldGeneratorMap(); // Field information used in FieldGeneartors. struct FieldGeneratorInfo { - string name; - string capitalized_name; - string disambiguated_reason; + std::string name; + std::string capitalized_name; + std::string disambiguated_reason; }; // Oneof information used in OneofFieldGenerators. struct OneofGeneratorInfo { - string name; - string capitalized_name; + std::string name; + std::string capitalized_name; }; // Set some common variables used in variable FieldGenerators. void SetCommonFieldVariables(const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, - std::map* variables); + std::map* variables); // Set some common oneof variables used in OneofFieldGenerators. void SetCommonOneofVariables(const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, - std::map* variables); + std::map* variables); // Print useful comments before a field's accessors. -void PrintExtraFieldInfo(const std::map& variables, +void PrintExtraFieldInfo(const std::map& variables, io::Printer* printer); } // namespace java diff --git a/src/google/protobuf/compiler/java/java_file.cc b/src/google/protobuf/compiler/java/java_file.cc index bb48a3be44..5bc847c369 100644 --- a/src/google/protobuf/compiler/java/java_file.cc +++ b/src/google/protobuf/compiler/java/java_file.cc @@ -54,6 +54,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -62,26 +63,26 @@ namespace java { namespace { struct FieldDescriptorCompare { - bool operator ()(const FieldDescriptor* f1, const FieldDescriptor* f2) const { - if(f1 == NULL) { + bool operator()(const FieldDescriptor* f1, const FieldDescriptor* f2) const { + if (f1 == NULL) { return false; } - if(f2 == NULL) { + if (f2 == NULL) { return true; } return f1->full_name() < f2->full_name(); } }; -typedef std::set FieldDescriptorSet; +typedef std::set + FieldDescriptorSet; // Recursively searches the given message to collect extensions. // Returns true if all the extensions can be recognized. The extensions will be // appended in to the extensions parameter. // Returns false when there are unknown fields, in which case the data in the // extensions output parameter is not reliable and should be discarded. -bool CollectExtensions(const Message& message, - FieldDescriptorSet* extensions) { +bool CollectExtensions(const Message& message, FieldDescriptorSet* extensions) { const Reflection* reflection = message.GetReflection(); // There are unknown fields that could be extensions, thus this call fails. @@ -98,7 +99,7 @@ bool CollectExtensions(const Message& message, int size = reflection->FieldSize(message, fields[i]); for (int j = 0; j < size; j++) { const Message& sub_message = - reflection->GetRepeatedMessage(message, fields[i], j); + reflection->GetRepeatedMessage(message, fields[i], j); if (!CollectExtensions(sub_message, extensions)) return false; } } else { @@ -119,7 +120,7 @@ bool CollectExtensions(const Message& message, void CollectExtensions(const FileDescriptorProto& file_proto, const DescriptorPool& alternate_pool, FieldDescriptorSet* extensions, - const string& file_data) { + const std::string& file_data) { if (!CollectExtensions(file_proto, extensions)) { // There are unknown fields in the file_proto, which are probably // extensions. We need to parse the data into a dynamic message based on the @@ -159,11 +160,9 @@ void CollectExtensions(const FileDescriptorProto& file_proto, // extend our methods by simply chaining them to another method // with a tail call. This inserts the sequence call-next-method, // end this one, begin-next-method as needed. -void MaybeRestartJavaMethod(io::Printer* printer, - int *bytecode_estimate, - int *method_num, - const char *chain_statement, - const char *method_decl) { +void MaybeRestartJavaMethod(io::Printer* printer, int* bytecode_estimate, + int* method_num, const char* chain_statement, + const char* method_decl) { // The goal here is to stay under 64K bytes of jvm bytecode/method, // since otherwise we hit a hardcoded limit in the jvm and javac will // then fail with the error "code too large". This limit lets our @@ -172,11 +171,10 @@ void MaybeRestartJavaMethod(io::Printer* printer, if ((*bytecode_estimate) > bytesPerMethod) { ++(*method_num); - printer->Print(chain_statement, "method_num", - SimpleItoa(*method_num)); + printer->Print(chain_statement, "method_num", StrCat(*method_num)); printer->Outdent(); printer->Print("}\n"); - printer->Print(method_decl, "method_num", SimpleItoa(*method_num)); + printer->Print(method_decl, "method_num", StrCat(*method_num)); printer->Indent(); *bytecode_estimate = 0; } @@ -194,8 +192,7 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options, options_(options), immutable_api_(immutable_api) { classname_ = name_resolver_->GetFileClassName(file, immutable_api); - generator_factory_.reset( - new ImmutableGeneratorFactory(context_.get())); + generator_factory_.reset(new ImmutableGeneratorFactory(context_.get())); for (int i = 0; i < file_->message_type_count(); ++i) { message_generators_[i].reset( generator_factory_->NewMessageGenerator(file_->message_type(i))); @@ -208,31 +205,48 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options, FileGenerator::~FileGenerator() {} -bool FileGenerator::Validate(string* error) { +bool FileGenerator::Validate(std::string* error) { // Check that no class name matches the file's class name. This is a common // problem that leads to Java compile errors that can be hard to understand. // It's especially bad when using the java_multiple_files, since we would // end up overwriting the outer class with one of the inner ones. - if (name_resolver_->HasConflictingClassName(file_, classname_)) { + if (name_resolver_->HasConflictingClassName(file_, classname_, + NameEquality::EXACT_EQUAL)) { error->assign(file_->name()); error->append( - ": Cannot generate Java output because the file's outer class name, \""); + ": Cannot generate Java output because the file's outer class name, " + "\""); error->append(classname_); error->append( - "\", matches the name of one of the types declared inside it. " - "Please either rename the type or use the java_outer_classname " - "option to specify a different outer class name for the .proto file."); + "\", matches the name of one of the types declared inside it. " + "Please either rename the type or use the java_outer_classname " + "option to specify a different outer class name for the .proto file."); return false; } + // Similar to the check above, but ignore the case this time. This is not a + // problem on Linux, but will lead to Java compile errors on Windows / Mac + // because filenames are case-insensitive on those platforms. + if (name_resolver_->HasConflictingClassName( + file_, classname_, NameEquality::EQUAL_IGNORE_CASE)) { + GOOGLE_LOG(WARNING) + << file_->name() << ": The file's outer class name, \"" << classname_ + << "\", matches the name of one of the types declared inside it when " + << "case is ignored. This can cause compilation issues on Windows / " + << "MacOS. Please either rename the type or use the " + << "java_outer_classname option to specify a different outer class " + << "name for the .proto file to be safe."; + } + // Print a warning if optimize_for = LITE_RUNTIME is used. if (file_->options().optimize_for() == FileOptions::LITE_RUNTIME) { GOOGLE_LOG(WARNING) << "The optimize_for = LITE_RUNTIME option is no longer supported by " - << "protobuf Java code generator and may generate broken code. It " - << "will be ignored by protoc in the future and protoc will always " + << "protobuf Java code generator and is ignored--protoc will always " << "generate full runtime code for Java. To use Java Lite runtime, " << "users should use the Java Lite plugin instead. See:\n" - << " https://github.com/google/protobuf/blob/master/java/lite.md"; + << " " + "https://github.com/protocolbuffers/protobuf/blob/master/java/" + "lite.md"; } return true; } @@ -241,33 +255,32 @@ void FileGenerator::Generate(io::Printer* printer) { // We don't import anything because we refer to all classes by their // fully-qualified names in the generated source. printer->Print( - "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" - "// source: $filename$\n" - "\n", - "filename", file_->name()); + "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" + "// source: $filename$\n" + "\n", + "filename", file_->name()); if (!java_package_.empty()) { printer->Print( - "package $package$;\n" - "\n", - "package", java_package_); + "package $package$;\n" + "\n", + "package", java_package_); } PrintGeneratedAnnotation( printer, '$', options_.annotate_code ? classname_ + ".java.pb.meta" : ""); printer->Print( "$deprecation$public final class $classname$ {\n" " private $ctor$() {}\n", - "deprecation", file_->options().deprecated() ? - "@java.lang.Deprecated " : "", - "classname", classname_, - "ctor", classname_); + "deprecation", + file_->options().deprecated() ? "@java.lang.Deprecated " : "", + "classname", classname_, "ctor", classname_); printer->Annotate("classname", file_->name()); printer->Indent(); // ----------------------------------------------------------------- printer->Print( - "public static void registerAllExtensions(\n" - " com.google.protobuf.ExtensionRegistryLite registry) {\n"); + "public static void registerAllExtensions(\n" + " com.google.protobuf.ExtensionRegistryLite registry) {\n"); printer->Indent(); @@ -280,8 +293,7 @@ void FileGenerator::Generate(io::Printer* printer) { } printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); if (HasDescriptorMethods(file_, context_->EnforceLite())) { // Overload registerAllExtensions for the non-lite usage to // redundantly maintain the original signature (this is @@ -289,12 +301,12 @@ void FileGenerator::Generate(io::Printer* printer) { // ExtensionRegistry in the non-lite usage). Intent is // to remove this in the future. printer->Print( - "\n" - "public static void registerAllExtensions(\n" - " com.google.protobuf.ExtensionRegistry registry) {\n" - " registerAllExtensions(\n" - " (com.google.protobuf.ExtensionRegistryLite) registry);\n" - "}\n"); + "\n" + "public static void registerAllExtensions(\n" + " com.google.protobuf.ExtensionRegistry registry) {\n" + " registerAllExtensions(\n" + " (com.google.protobuf.ExtensionRegistryLite) registry);\n" + "}\n"); } // ----------------------------------------------------------------- @@ -346,29 +358,27 @@ void FileGenerator::Generate(io::Printer* printer) { GenerateDescriptorInitializationCodeForMutable(printer); } } else { - printer->Print( - "static {\n"); + printer->Print("static {\n"); printer->Indent(); int bytecode_estimate = 0; int method_num = 0; for (int i = 0; i < file_->message_type_count(); i++) { - bytecode_estimate += message_generators_[i]->GenerateStaticVariableInitializers(printer); + bytecode_estimate += + message_generators_[i]->GenerateStaticVariableInitializers(printer); MaybeRestartJavaMethod( - printer, - &bytecode_estimate, &method_num, - "_clinit_autosplit_$method_num$();\n", - "private static void _clinit_autosplit_$method_num$() {\n"); + printer, &bytecode_estimate, &method_num, + "_clinit_autosplit_$method_num$();\n", + "private static void _clinit_autosplit_$method_num$() {\n"); } printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } printer->Print( - "\n" - "// @@protoc_insertion_point(outer_class_scope)\n"); + "\n" + "// @@protoc_insertion_point(outer_class_scope)\n"); printer->Outdent(); printer->Print("}\n"); @@ -377,15 +387,15 @@ void FileGenerator::Generate(io::Printer* printer) { void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( io::Printer* printer) { printer->Print( - "public static com.google.protobuf.Descriptors.FileDescriptor\n" - " getDescriptor() {\n" - " return descriptor;\n" - "}\n" - "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n" - " descriptor;\n" - "static {\n", - // TODO(dweis): Mark this as final. - "final", ""); + "public static com.google.protobuf.Descriptors.FileDescriptor\n" + " getDescriptor() {\n" + " return descriptor;\n" + "}\n" + "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n" + " descriptor;\n" + "static {\n", + // TODO(dweis): Mark this as final. + "final", ""); printer->Indent(); SharedCodeGenerator shared_code_generator(file_, options_); @@ -395,20 +405,20 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( int method_num = 0; for (int i = 0; i < file_->message_type_count(); i++) { - bytecode_estimate += message_generators_[i]->GenerateStaticVariableInitializers(printer); + bytecode_estimate += + message_generators_[i]->GenerateStaticVariableInitializers(printer); MaybeRestartJavaMethod( - printer, - &bytecode_estimate, &method_num, - "_clinit_autosplit_dinit_$method_num$();\n", - "private static void _clinit_autosplit_dinit_$method_num$() {\n"); + printer, &bytecode_estimate, &method_num, + "_clinit_autosplit_dinit_$method_num$();\n", + "private static void _clinit_autosplit_dinit_$method_num$() {\n"); } for (int i = 0; i < file_->extension_count(); i++) { - bytecode_estimate += extension_generators_[i]->GenerateNonNestedInitializationCode(printer); + bytecode_estimate += + extension_generators_[i]->GenerateNonNestedInitializationCode(printer); MaybeRestartJavaMethod( - printer, - &bytecode_estimate, &method_num, - "_clinit_autosplit_dinit_$method_num$();\n", - "private static void _clinit_autosplit_dinit_$method_num$() {\n"); + printer, &bytecode_estimate, &method_num, + "_clinit_autosplit_dinit_$method_num$();\n", + "private static void _clinit_autosplit_dinit_$method_num$() {\n"); } // Proto compiler builds a DescriptorPool, which holds all the descriptors to @@ -428,7 +438,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( // reflections to find all extension fields FileDescriptorProto file_proto; file_->CopyTo(&file_proto); - string file_data; + std::string file_data; file_proto.SerializeToString(&file_data); FieldDescriptorSet extensions; CollectExtensions(file_proto, *file_->pool(), &extensions, file_data); @@ -437,56 +447,54 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( // Must construct an ExtensionRegistry containing all existing extensions // and use it to parse the descriptor data again to recognize extensions. printer->Print( - "com.google.protobuf.ExtensionRegistry registry =\n" - " com.google.protobuf.ExtensionRegistry.newInstance();\n"); + "com.google.protobuf.ExtensionRegistry registry =\n" + " com.google.protobuf.ExtensionRegistry.newInstance();\n"); FieldDescriptorSet::iterator it; for (it = extensions.begin(); it != extensions.end(); it++) { std::unique_ptr generator( generator_factory_->NewExtensionGenerator(*it)); bytecode_estimate += generator->GenerateRegistrationCode(printer); MaybeRestartJavaMethod( - printer, - &bytecode_estimate, &method_num, - "_clinit_autosplit_dinit_$method_num$(registry);\n", - "private static void _clinit_autosplit_dinit_$method_num$(\n" - " com.google.protobuf.ExtensionRegistry registry) {\n"); + printer, &bytecode_estimate, &method_num, + "_clinit_autosplit_dinit_$method_num$(registry);\n", + "private static void _clinit_autosplit_dinit_$method_num$(\n" + " com.google.protobuf.ExtensionRegistry registry) {\n"); } printer->Print( - "com.google.protobuf.Descriptors.FileDescriptor\n" - " .internalUpdateFileDescriptor(descriptor, registry);\n"); + "com.google.protobuf.Descriptors.FileDescriptor\n" + " .internalUpdateFileDescriptor(descriptor, registry);\n"); } // Force descriptor initialization of all dependencies. for (int i = 0; i < file_->dependency_count(); i++) { if (ShouldIncludeDependency(file_->dependency(i), true)) { - string dependency = + std::string dependency = name_resolver_->GetImmutableClassName(file_->dependency(i)); - printer->Print( - "$dependency$.getDescriptor();\n", - "dependency", dependency); + printer->Print("$dependency$.getDescriptor();\n", "dependency", + dependency); } } printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } -void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* printer) { +void FileGenerator::GenerateDescriptorInitializationCodeForMutable( + io::Printer* printer) { printer->Print( - "public static com.google.protobuf.Descriptors.FileDescriptor\n" - " getDescriptor() {\n" - " return descriptor;\n" - "}\n" - "private static final com.google.protobuf.Descriptors.FileDescriptor\n" - " descriptor;\n" - "static {\n"); + "public static com.google.protobuf.Descriptors.FileDescriptor\n" + " getDescriptor() {\n" + " return descriptor;\n" + "}\n" + "private static final com.google.protobuf.Descriptors.FileDescriptor\n" + " descriptor;\n" + "static {\n"); printer->Indent(); printer->Print( - "descriptor = $immutable_package$.$descriptor_classname$.descriptor;\n", - "immutable_package", FileJavaPackage(file_, true), - "descriptor_classname", name_resolver_->GetDescriptorClassName(file_)); + "descriptor = $immutable_package$.$descriptor_classname$.descriptor;\n", + "immutable_package", FileJavaPackage(file_, true), "descriptor_classname", + name_resolver_->GetDescriptorClassName(file_)); for (int i = 0; i < file_->message_type_count(); i++) { message_generators_[i]->GenerateStaticVariableInitializers(printer); @@ -499,7 +507,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* // custom options are only represented with immutable messages. FileDescriptorProto file_proto; file_->CopyTo(&file_proto); - string file_data; + std::string file_data; file_proto.SerializeToString(&file_data); FieldDescriptorSet extensions; CollectExtensions(file_proto, *file_->pool(), &extensions, file_data); @@ -513,7 +521,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* // we want the mutable code to be independent from the immutable code // at compile time. It is required to implement dual-compile for // mutable and immutable API in blaze. - " java.lang.Class immutableClass = java.lang.Class.forName(\n" + " java.lang.Class immutableClass = java.lang.Class.forName(\n" " \"$immutable_classname$\");\n" "} catch (java.lang.ClassNotFoundException e) {\n", "immutable_classname", name_resolver_->GetImmutableClassName(file_)); @@ -528,7 +536,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* FieldDescriptorSet::iterator it; for (it = extensions.begin(); it != extensions.end(); it++) { const FieldDescriptor* field = *it; - string scope; + std::string scope; if (field->extension_scope() != NULL) { scope = name_resolver_->GetMutableClassName(field->extension_scope()) + ".getDescriptor()"; @@ -546,13 +554,11 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* " $scope$.getExtensions().get($index$),\n" " (com.google.protobuf.Message) defaultExtensionInstance);\n" "}\n", - "scope", scope, "index", SimpleItoa(field->index()), - "class", + "scope", scope, "index", StrCat(field->index()), "class", name_resolver_->GetImmutableClassName(field->message_type())); } else { printer->Print("registry.add($scope$.getExtensions().get($index$));\n", - "scope", scope, "index", - SimpleItoa(field->index())); + "scope", scope, "index", StrCat(field->index())); } } printer->Print( @@ -566,32 +572,29 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* // Force descriptor initialization of all dependencies. for (int i = 0; i < file_->dependency_count(); i++) { if (ShouldIncludeDependency(file_->dependency(i), false)) { - string dependency = name_resolver_->GetMutableClassName( - file_->dependency(i)); - printer->Print( - "$dependency$.getDescriptor();\n", - "dependency", dependency); + std::string dependency = + name_resolver_->GetMutableClassName(file_->dependency(i)); + printer->Print("$dependency$.getDescriptor();\n", "dependency", + dependency); } } printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } template -static void GenerateSibling(const string& package_dir, - const string& java_package, - const DescriptorClass* descriptor, - GeneratorContext* context, - std::vector* file_list, bool annotate_code, - std::vector* annotation_list, - const string& name_suffix, - GeneratorClass* generator, - void (GeneratorClass::*pfn)(io::Printer* printer)) { - string filename = package_dir + descriptor->name() + name_suffix + ".java"; +static void GenerateSibling( + const std::string& package_dir, const std::string& java_package, + const DescriptorClass* descriptor, GeneratorContext* context, + std::vector* file_list, bool annotate_code, + std::vector* annotation_list, const std::string& name_suffix, + GeneratorClass* generator, + void (GeneratorClass::*pfn)(io::Printer* printer)) { + std::string filename = + package_dir + descriptor->name() + name_suffix + ".java"; file_list->push_back(filename); - string info_full_path = filename + ".pb.meta"; + std::string info_full_path = filename + ".pb.meta"; GeneratedCodeInfo annotations; io::AnnotationProtoCollector annotation_collector( &annotations); @@ -601,15 +604,15 @@ static void GenerateSibling(const string& package_dir, annotate_code ? &annotation_collector : NULL); printer.Print( - "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" - "// source: $filename$\n" - "\n", - "filename", descriptor->file()->name()); + "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" + "// source: $filename$\n" + "\n", + "filename", descriptor->file()->name()); if (!java_package.empty()) { printer.Print( - "package $package$;\n" - "\n", - "package", java_package); + "package $package$;\n" + "\n", + "package", java_package); } (generator->*pfn)(&printer); @@ -622,10 +625,10 @@ static void GenerateSibling(const string& package_dir, } } -void FileGenerator::GenerateSiblings(const string& package_dir, - GeneratorContext* context, - std::vector* file_list, - std::vector* annotation_list) { +void FileGenerator::GenerateSiblings( + const std::string& package_dir, GeneratorContext* context, + std::vector* file_list, + std::vector* annotation_list) { if (MultipleJavaFiles(file_, immutable_api_)) { for (int i = 0; i < file_->enum_type_count(); i++) { if (HasDescriptorMethods(file_, context_->EnforceLite())) { @@ -669,8 +672,8 @@ void FileGenerator::GenerateSiblings(const string& package_dir, } } -bool FileGenerator::ShouldIncludeDependency( - const FileDescriptor* descriptor, bool immutable_api) { +bool FileGenerator::ShouldIncludeDependency(const FileDescriptor* descriptor, + bool immutable_api) { return true; } diff --git a/src/google/protobuf/compiler/java/java_file.h b/src/google/protobuf/compiler/java/java_file.h index 4158d48b99..bb3e4a5765 100644 --- a/src/google/protobuf/compiler/java/java_file.h +++ b/src/google/protobuf/compiler/java/java_file.h @@ -43,20 +43,20 @@ namespace google { namespace protobuf { -class FileDescriptor; // descriptor.h +class FileDescriptor; // descriptor.h namespace io { - class Printer; // printer.h +class Printer; // printer.h } namespace compiler { - class GeneratorContext; // code_generator.h - namespace java { - class Context; // context.h - class MessageGenerator; // message.h - class GeneratorFactory; // generator_factory.h - class ExtensionGenerator; // extension.h - class ClassNameResolver; // name_resolver.h - } -} +class GeneratorContext; // code_generator.h +namespace java { +class Context; // context.h +class MessageGenerator; // message.h +class GeneratorFactory; // generator_factory.h +class ExtensionGenerator; // extension.h +class ClassNameResolver; // name_resolver.h +} // namespace java +} // namespace compiler } // namespace protobuf } // namespace google @@ -74,20 +74,20 @@ class FileGenerator { // Checks for problems that would otherwise lead to cryptic compile errors. // Returns true if there are no problems, or writes an error description to // the given string and returns false otherwise. - bool Validate(string* error); + bool Validate(std::string* error); void Generate(io::Printer* printer); // If we aren't putting everything into one file, this will write all the // files other than the outer file (i.e. one for each message, enum, and // service type). - void GenerateSiblings(const string& package_dir, + void GenerateSiblings(const std::string& package_dir, GeneratorContext* generator_context, - std::vector* file_list, - std::vector* annotation_list); + std::vector* file_list, + std::vector* annotation_list); - const string& java_package() { return java_package_; } - const string& classname() { return classname_; } + const std::string& java_package() { return java_package_; } + const std::string& classname() { return classname_; } private: void GenerateDescriptorInitializationCodeForImmutable(io::Printer* printer); @@ -97,8 +97,8 @@ class FileGenerator { bool immutable_api_); const FileDescriptor* file_; - string java_package_; - string classname_; + std::string java_package_; + std::string classname_; std::vector> message_generators_; std::vector> extension_generators_; diff --git a/src/google/protobuf/compiler/java/java_generator.cc b/src/google/protobuf/compiler/java/java_generator.cc index fd2591dabe..5045cfdc66 100644 --- a/src/google/protobuf/compiler/java/java_generator.cc +++ b/src/google/protobuf/compiler/java/java_generator.cc @@ -59,14 +59,13 @@ JavaGenerator::JavaGenerator() {} JavaGenerator::~JavaGenerator() {} bool JavaGenerator::Generate(const FileDescriptor* file, - const string& parameter, + const std::string& parameter, GeneratorContext* context, - string* error) const { + std::string* error) const { // ----------------------------------------------------------------- // parse generator options - - std::vector > options; + std::vector > options; ParseGeneratorParameter(parameter, &options); Options file_options; @@ -79,6 +78,10 @@ bool JavaGenerator::Generate(const FileDescriptor* file, file_options.generate_mutable_code = true; } else if (options[i].first == "shared") { file_options.generate_shared_code = true; + } else if (options[i].first == "lite") { + // Note: Java Lite does not guarantee API/ABI stability. We may choose to + // break existing API in order to boost performance / reduce code size. + file_options.enforce_lite = true; } else if (options[i].first == "annotate_code") { file_options.annotate_code = true; } else if (options[i].first == "annotation_list_file") { @@ -105,8 +108,8 @@ bool JavaGenerator::Generate(const FileDescriptor* file, // ----------------------------------------------------------------- - std::vector all_files; - std::vector all_annotations; + std::vector all_files; + std::vector all_annotations; std::vector file_generators; @@ -131,13 +134,13 @@ bool JavaGenerator::Generate(const FileDescriptor* file, for (int i = 0; i < file_generators.size(); ++i) { FileGenerator* file_generator = file_generators[i]; - string package_dir = JavaPackageToDir(file_generator->java_package()); + std::string package_dir = JavaPackageToDir(file_generator->java_package()); - string java_filename = package_dir; + std::string java_filename = package_dir; java_filename += file_generator->classname(); java_filename += ".java"; all_files.push_back(java_filename); - string info_full_path = java_filename + ".pb.meta"; + std::string info_full_path = java_filename + ".pb.meta"; if (file_options.annotate_code) { all_annotations.push_back(info_full_path); } @@ -148,9 +151,9 @@ bool JavaGenerator::Generate(const FileDescriptor* file, GeneratedCodeInfo annotations; io::AnnotationProtoCollector annotation_collector( &annotations); - io::Printer printer(output.get(), '$', file_options.annotate_code - ? &annotation_collector - : NULL); + io::Printer printer( + output.get(), '$', + file_options.annotate_code ? &annotation_collector : NULL); file_generator->Generate(&printer); diff --git a/src/google/protobuf/compiler/java/java_generator.h b/src/google/protobuf/compiler/java/java_generator.h index 6eefdd8431..be63ac49e2 100644 --- a/src/google/protobuf/compiler/java/java_generator.h +++ b/src/google/protobuf/compiler/java/java_generator.h @@ -40,6 +40,8 @@ #include #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -49,16 +51,14 @@ namespace java { // own protocol compiler binary and you want it to support Java output, you // can do so by registering an instance of this CodeGenerator with the // CommandLineInterface in your main() function. -class LIBPROTOC_EXPORT JavaGenerator : public CodeGenerator { +class PROTOC_EXPORT JavaGenerator : public CodeGenerator { public: JavaGenerator(); ~JavaGenerator(); // implements CodeGenerator ---------------------------------------- - bool Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const; + bool Generate(const FileDescriptor* file, const std::string& parameter, + GeneratorContext* context, std::string* error) const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(JavaGenerator); @@ -69,4 +69,6 @@ class LIBPROTOC_EXPORT JavaGenerator : public CodeGenerator { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/java/java_generator_factory.cc b/src/google/protobuf/compiler/java/java_generator_factory.cc index 3218b41076..3a62adb103 100644 --- a/src/google/protobuf/compiler/java/java_generator_factory.cc +++ b/src/google/protobuf/compiler/java/java_generator_factory.cc @@ -52,9 +52,8 @@ GeneratorFactory::~GeneratorFactory() {} // =================================================================== -ImmutableGeneratorFactory::ImmutableGeneratorFactory( - Context* context) : context_(context) { -} +ImmutableGeneratorFactory::ImmutableGeneratorFactory(Context* context) + : context_(context) {} ImmutableGeneratorFactory::~ImmutableGeneratorFactory() {} MessageGenerator* ImmutableGeneratorFactory::NewMessageGenerator( diff --git a/src/google/protobuf/compiler/java/java_generator_factory.h b/src/google/protobuf/compiler/java/java_generator_factory.h index 505cd3ac17..16688a56d0 100644 --- a/src/google/protobuf/compiler/java/java_generator_factory.h +++ b/src/google/protobuf/compiler/java/java_generator_factory.h @@ -37,17 +37,17 @@ namespace google { namespace protobuf { -class FieldDescriptor; // descriptor.h -class Descriptor; // descriptor.h -class ServiceDescriptor; // descriptor.h +class FieldDescriptor; // descriptor.h +class Descriptor; // descriptor.h +class ServiceDescriptor; // descriptor.h namespace compiler { - namespace java { - class MessageGenerator; // message.h - class ExtensionGenerator; // extension.h - class ServiceGenerator; // service.h - class Context; // context.h - } -} +namespace java { +class MessageGenerator; // message.h +class ExtensionGenerator; // extension.h +class ServiceGenerator; // service.h +class Context; // context.h +} // namespace java +} // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/java/java_helpers.cc b/src/google/protobuf/compiler/java/java_helpers.cc index dbd1604a60..ac0e9cd3db 100644 --- a/src/google/protobuf/compiler/java/java_helpers.cc +++ b/src/google/protobuf/compiler/java/java_helpers.cc @@ -46,6 +46,7 @@ #include + #include // for hash namespace google { @@ -57,9 +58,9 @@ using internal::WireFormat; using internal::WireFormatLite; const char kThickSeparator[] = - "// ===================================================================\n"; + "// ===================================================================\n"; const char kThinSeparator[] = - "// -------------------------------------------------------------------\n"; + "// -------------------------------------------------------------------\n"; namespace { @@ -69,15 +70,28 @@ const char* kDefaultPackage = ""; // Using them will cause the compiler to generate accessors whose names are // colliding with methods defined in base classes. const char* kForbiddenWordList[] = { - // message base class: - "cached_size", "serialized_size", - // java.lang.Object: - "class", + // message base class: + "cached_size", + "serialized_size", + // java.lang.Object: + "class", }; -const int kDefaultLookUpStartFieldNumber = 40; - -bool IsForbidden(const string& field_name) { +const std::unordered_set* kReservedNames = + new std::unordered_set({ + "abstract", "assert", "boolean", "break", "byte", + "case", "catch", "char", "class", "const", + "continue", "default", "do", "double", "else", + "enum", "extends", "final", "finally", "float", + "for", "goto", "if", "implements", "import", + "instanceof", "int", "interface", "long", "native", + "new", "package", "private", "protected", "public", + "return", "short", "static", "strictfp", "super", + "switch", "synchronized", "this", "throw", "throws", + "transient", "try", "void", "volatile", "while", + }); + +bool IsForbidden(const std::string& field_name) { for (int i = 0; i < GOOGLE_ARRAYSIZE(kForbiddenWordList); ++i) { if (field_name == kForbiddenWordList[i]) { return true; @@ -86,8 +100,8 @@ bool IsForbidden(const string& field_name) { return false; } -string FieldName(const FieldDescriptor* field) { - string field_name; +std::string FieldName(const FieldDescriptor* field) { + std::string field_name; // Groups are hacky: The name of the field is just the lower-cased name // of the group type. In Java, though, we would like to retain the original // capitalization of the type name. @@ -105,28 +119,14 @@ string FieldName(const FieldDescriptor* field) { } -// Judge whether should use table or use look up. -// Copied from com.google.protobuf.SchemaUtil.shouldUseTableSwitch -bool ShouldUseTable(int lo, int hi, int number_of_fields) { - if (hi < kDefaultLookUpStartFieldNumber) { - return true; - } - int64 table_space_cost = (static_cast(hi) - lo + 1); // words - int64 table_time_cost = 3; // comparisons - int64 lookup_space_cost = 3 + 2 * static_cast(number_of_fields); - int64 lookup_time_cost = 3 + number_of_fields; - return table_space_cost + 3 * table_time_cost <= - lookup_space_cost + 3 * lookup_time_cost; -} - } // namespace void PrintGeneratedAnnotation(io::Printer* printer, char delimiter, - const string& annotation_file) { + const std::string& annotation_file) { if (annotation_file.empty()) { return; } - string ptemplate = + std::string ptemplate = "@javax.annotation.Generated(value=\"protoc\", comments=\"annotations:"; ptemplate.push_back(delimiter); ptemplate.append("annotation_file"); @@ -137,29 +137,29 @@ void PrintGeneratedAnnotation(io::Printer* printer, char delimiter, void PrintEnumVerifierLogic(io::Printer* printer, const FieldDescriptor* descriptor, - const std::map& variables, + const std::map& variables, const char* var_name, - const char* terminating_string, - bool enforce_lite) { + const char* terminating_string, bool enforce_lite) { std::string enum_verifier_string = - (descriptor->enum_type()->file()->options().optimize_for() == - FileOptions::LITE_RUNTIME) || enforce_lite - ? StrCat(var_name, ".internalGetVerifier()") - : StrCat( - "new com.google.protobuf.Internal.EnumVerifier() {\n" - " @java.lang.Override\n" - " public boolean isInRange(int number) {\n" - " return ", var_name, ".forNumber(number) != null;\n" - " }\n" - " }" - ); + enforce_lite ? StrCat(var_name, ".internalGetVerifier()") + : StrCat( + "new com.google.protobuf.Internal.EnumVerifier() {\n" + " @java.lang.Override\n" + " public boolean isInRange(int number) {\n" + " return ", + var_name, + ".forNumber(number) != null;\n" + " }\n" + " }"); printer->Print( variables, StrCat(enum_verifier_string, terminating_string).c_str()); } -string UnderscoresToCamelCase(const string& input, bool cap_next_letter) { - string result; +std::string UnderscoresToCamelCase(const std::string& input, + bool cap_next_letter) { + GOOGLE_CHECK(!input.empty()); + std::string result; // Note: I distrust ctype.h due to locales. for (int i = 0; i < input.size(); i++) { if ('a' <= input[i] && input[i] <= 'z') { @@ -193,35 +193,43 @@ string UnderscoresToCamelCase(const string& input, bool cap_next_letter) { return result; } -string UnderscoresToCamelCase(const FieldDescriptor* field) { +std::string UnderscoresToCamelCase(const FieldDescriptor* field) { return UnderscoresToCamelCase(FieldName(field), false); } -string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field) { +std::string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field) { return UnderscoresToCamelCase(FieldName(field), true); } -string CapitalizedFieldName(const FieldDescriptor* field) { +std::string CapitalizedFieldName(const FieldDescriptor* field) { return UnderscoresToCapitalizedCamelCase(field); } -string UnderscoresToCamelCase(const MethodDescriptor* method) { +std::string UnderscoresToCamelCase(const MethodDescriptor* method) { return UnderscoresToCamelCase(method->name(), false); } -string UniqueFileScopeIdentifier(const Descriptor* descriptor) { +std::string UnderscoresToCamelCaseCheckReserved(const FieldDescriptor* field) { + std::string name = UnderscoresToCamelCase(field); + if (kReservedNames->find(name) != kReservedNames->end()) { + return name + "_"; + } + return name; +} + +std::string UniqueFileScopeIdentifier(const Descriptor* descriptor) { return "static_" + StringReplace(descriptor->full_name(), ".", "_", true); } -string CamelCaseFieldName(const FieldDescriptor* field) { - string fieldName = UnderscoresToCamelCase(field); +std::string CamelCaseFieldName(const FieldDescriptor* field) { + std::string fieldName = UnderscoresToCamelCase(field); if ('0' <= fieldName[0] && fieldName[0] <= '9') { return '_' + fieldName; } return fieldName; } -string StripProto(const string& filename) { +std::string StripProto(const std::string& filename) { if (HasSuffixString(filename, ".protodevel")) { return StripSuffixString(filename, ".protodevel"); } else { @@ -229,13 +237,13 @@ string StripProto(const string& filename) { } } -string FileClassName(const FileDescriptor* file, bool immutable) { +std::string FileClassName(const FileDescriptor* file, bool immutable) { ClassNameResolver name_resolver; return name_resolver.GetFileClassName(file, immutable); } -string FileJavaPackage(const FileDescriptor* file, bool immutable) { - string result; +std::string FileJavaPackage(const FileDescriptor* file, bool immutable) { + std::string result; if (file->options().has_java_package()) { result = file->options().java_package(); @@ -250,82 +258,58 @@ string FileJavaPackage(const FileDescriptor* file, bool immutable) { return result; } -string FileJavaPackage(const FileDescriptor* file) { +std::string FileJavaPackage(const FileDescriptor* file) { return FileJavaPackage(file, true /* immutable */); } -string JavaPackageToDir(string package_name) { - string package_dir = - StringReplace(package_name, ".", "/", true); +std::string JavaPackageToDir(std::string package_name) { + std::string package_dir = StringReplace(package_name, ".", "/", true); if (!package_dir.empty()) package_dir += "/"; return package_dir; } -// TODO(xiaofeng): This function is only kept for it's publicly referenced. -// It should be removed after mutable API up-integration. -string ToJavaName(const string& full_name, - const FileDescriptor* file) { - string result; - if (file->options().java_multiple_files()) { - result = FileJavaPackage(file); - } else { - result = ClassName(file); - } - if (!result.empty()) { - result += '.'; - } - if (file->package().empty()) { - result += full_name; - } else { - // Strip the proto package from full_name since we've replaced it with - // the Java package. - result += full_name.substr(file->package().size() + 1); - } - return result; -} - -string ClassName(const Descriptor* descriptor) { +std::string ClassName(const Descriptor* descriptor) { ClassNameResolver name_resolver; return name_resolver.GetClassName(descriptor, true); } -string ClassName(const EnumDescriptor* descriptor) { +std::string ClassName(const EnumDescriptor* descriptor) { ClassNameResolver name_resolver; return name_resolver.GetClassName(descriptor, true); } -string ClassName(const ServiceDescriptor* descriptor) { +std::string ClassName(const ServiceDescriptor* descriptor) { ClassNameResolver name_resolver; return name_resolver.GetClassName(descriptor, true); } -string ClassName(const FileDescriptor* descriptor) { +std::string ClassName(const FileDescriptor* descriptor) { ClassNameResolver name_resolver; return name_resolver.GetClassName(descriptor, true); } -string ExtraMessageInterfaces(const Descriptor* descriptor) { - string interfaces = "// @@protoc_insertion_point(message_implements:" - + descriptor->full_name() + ")"; +std::string ExtraMessageInterfaces(const Descriptor* descriptor) { + std::string interfaces = "// @@protoc_insertion_point(message_implements:" + + descriptor->full_name() + ")"; return interfaces; } -string ExtraBuilderInterfaces(const Descriptor* descriptor) { - string interfaces = "// @@protoc_insertion_point(builder_implements:" - + descriptor->full_name() + ")"; +std::string ExtraBuilderInterfaces(const Descriptor* descriptor) { + std::string interfaces = "// @@protoc_insertion_point(builder_implements:" + + descriptor->full_name() + ")"; return interfaces; } -string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor) { - string interfaces = "// @@protoc_insertion_point(interface_extends:" - + descriptor->full_name() + ")"; +std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor) { + std::string interfaces = "// @@protoc_insertion_point(interface_extends:" + + descriptor->full_name() + ")"; return interfaces; } -string FieldConstantName(const FieldDescriptor *field) { - string name = field->name() + "_FIELD_NUMBER"; +std::string FieldConstantName(const FieldDescriptor* field) { + std::string name = field->name() + "_FIELD_NUMBER"; UpperString(&name); return name; } @@ -372,8 +356,8 @@ JavaType GetJavaType(const FieldDescriptor* field) { case FieldDescriptor::TYPE_MESSAGE: return JAVATYPE_MESSAGE; - // No default because we want the compiler to complain if any new - // types are added. + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -382,18 +366,27 @@ JavaType GetJavaType(const FieldDescriptor* field) { const char* PrimitiveTypeName(JavaType type) { switch (type) { - case JAVATYPE_INT : return "int"; - case JAVATYPE_LONG : return "long"; - case JAVATYPE_FLOAT : return "float"; - case JAVATYPE_DOUBLE : return "double"; - case JAVATYPE_BOOLEAN: return "boolean"; - case JAVATYPE_STRING : return "java.lang.String"; - case JAVATYPE_BYTES : return "com.google.protobuf.ByteString"; - case JAVATYPE_ENUM : return NULL; - case JAVATYPE_MESSAGE: return NULL; - - // No default because we want the compiler to complain if any new - // JavaTypes are added. + case JAVATYPE_INT: + return "int"; + case JAVATYPE_LONG: + return "long"; + case JAVATYPE_FLOAT: + return "float"; + case JAVATYPE_DOUBLE: + return "double"; + case JAVATYPE_BOOLEAN: + return "boolean"; + case JAVATYPE_STRING: + return "java.lang.String"; + case JAVATYPE_BYTES: + return "com.google.protobuf.ByteString"; + case JAVATYPE_ENUM: + return NULL; + case JAVATYPE_MESSAGE: + return NULL; + + // No default because we want the compiler to complain if any new + // JavaTypes are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -406,18 +399,27 @@ const char* PrimitiveTypeName(const FieldDescriptor* descriptor) { const char* BoxedPrimitiveTypeName(JavaType type) { switch (type) { - case JAVATYPE_INT : return "java.lang.Integer"; - case JAVATYPE_LONG : return "java.lang.Long"; - case JAVATYPE_FLOAT : return "java.lang.Float"; - case JAVATYPE_DOUBLE : return "java.lang.Double"; - case JAVATYPE_BOOLEAN: return "java.lang.Boolean"; - case JAVATYPE_STRING : return "java.lang.String"; - case JAVATYPE_BYTES : return "com.google.protobuf.ByteString"; - case JAVATYPE_ENUM : return NULL; - case JAVATYPE_MESSAGE: return NULL; - - // No default because we want the compiler to complain if any new - // JavaTypes are added. + case JAVATYPE_INT: + return "java.lang.Integer"; + case JAVATYPE_LONG: + return "java.lang.Long"; + case JAVATYPE_FLOAT: + return "java.lang.Float"; + case JAVATYPE_DOUBLE: + return "java.lang.Double"; + case JAVATYPE_BOOLEAN: + return "java.lang.Boolean"; + case JAVATYPE_STRING: + return "java.lang.String"; + case JAVATYPE_BYTES: + return "com.google.protobuf.ByteString"; + case JAVATYPE_ENUM: + return NULL; + case JAVATYPE_MESSAGE: + return NULL; + + // No default because we want the compiler to complain if any new + // JavaTypes are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -428,37 +430,66 @@ const char* BoxedPrimitiveTypeName(const FieldDescriptor* descriptor) { return BoxedPrimitiveTypeName(GetJavaType(descriptor)); } +std::string GetOneofStoredType(const FieldDescriptor* field) { + const JavaType javaType = GetJavaType(field); + switch (javaType) { + case JAVATYPE_ENUM: + return "java.lang.Integer"; + case JAVATYPE_MESSAGE: + return ClassName(field->message_type()); + default: + return BoxedPrimitiveTypeName(javaType); + } +} const char* FieldTypeName(FieldDescriptor::Type field_type) { switch (field_type) { - case FieldDescriptor::TYPE_INT32 : return "INT32"; - case FieldDescriptor::TYPE_UINT32 : return "UINT32"; - case FieldDescriptor::TYPE_SINT32 : return "SINT32"; - case FieldDescriptor::TYPE_FIXED32 : return "FIXED32"; - case FieldDescriptor::TYPE_SFIXED32: return "SFIXED32"; - case FieldDescriptor::TYPE_INT64 : return "INT64"; - case FieldDescriptor::TYPE_UINT64 : return "UINT64"; - case FieldDescriptor::TYPE_SINT64 : return "SINT64"; - case FieldDescriptor::TYPE_FIXED64 : return "FIXED64"; - case FieldDescriptor::TYPE_SFIXED64: return "SFIXED64"; - case FieldDescriptor::TYPE_FLOAT : return "FLOAT"; - case FieldDescriptor::TYPE_DOUBLE : return "DOUBLE"; - case FieldDescriptor::TYPE_BOOL : return "BOOL"; - case FieldDescriptor::TYPE_STRING : return "STRING"; - case FieldDescriptor::TYPE_BYTES : return "BYTES"; - case FieldDescriptor::TYPE_ENUM : return "ENUM"; - case FieldDescriptor::TYPE_GROUP : return "GROUP"; - case FieldDescriptor::TYPE_MESSAGE : return "MESSAGE"; - - // No default because we want the compiler to complain if any new - // types are added. + case FieldDescriptor::TYPE_INT32: + return "INT32"; + case FieldDescriptor::TYPE_UINT32: + return "UINT32"; + case FieldDescriptor::TYPE_SINT32: + return "SINT32"; + case FieldDescriptor::TYPE_FIXED32: + return "FIXED32"; + case FieldDescriptor::TYPE_SFIXED32: + return "SFIXED32"; + case FieldDescriptor::TYPE_INT64: + return "INT64"; + case FieldDescriptor::TYPE_UINT64: + return "UINT64"; + case FieldDescriptor::TYPE_SINT64: + return "SINT64"; + case FieldDescriptor::TYPE_FIXED64: + return "FIXED64"; + case FieldDescriptor::TYPE_SFIXED64: + return "SFIXED64"; + case FieldDescriptor::TYPE_FLOAT: + return "FLOAT"; + case FieldDescriptor::TYPE_DOUBLE: + return "DOUBLE"; + case FieldDescriptor::TYPE_BOOL: + return "BOOL"; + case FieldDescriptor::TYPE_STRING: + return "STRING"; + case FieldDescriptor::TYPE_BYTES: + return "BYTES"; + case FieldDescriptor::TYPE_ENUM: + return "ENUM"; + case FieldDescriptor::TYPE_GROUP: + return "GROUP"; + case FieldDescriptor::TYPE_MESSAGE: + return "MESSAGE"; + + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; return NULL; } -bool AllAscii(const string& text) { +bool AllAscii(const std::string& text) { for (int i = 0; i < text.size(); i++) { if ((text[i] & 0x80) != 0) { return false; @@ -467,22 +498,20 @@ bool AllAscii(const string& text) { return true; } -string DefaultValue(const FieldDescriptor* field, bool immutable, - ClassNameResolver* name_resolver) { +std::string DefaultValue(const FieldDescriptor* field, bool immutable, + ClassNameResolver* name_resolver) { // Switch on CppType since we need to know which default_value_* method // of FieldDescriptor to call. switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: - return SimpleItoa(field->default_value_int32()); + return StrCat(field->default_value_int32()); case FieldDescriptor::CPPTYPE_UINT32: // Need to print as a signed int since Java has no unsigned. - return SimpleItoa( - static_cast(field->default_value_uint32())); + return StrCat(static_cast(field->default_value_uint32())); case FieldDescriptor::CPPTYPE_INT64: - return SimpleItoa(field->default_value_int64()) + "L"; + return StrCat(field->default_value_int64()) + "L"; case FieldDescriptor::CPPTYPE_UINT64: - return SimpleItoa( - static_cast(field->default_value_uint64())) + + return StrCat(static_cast(field->default_value_uint64())) + "L"; case FieldDescriptor::CPPTYPE_DOUBLE: { double value = field->default_value_double(); @@ -515,8 +544,8 @@ string DefaultValue(const FieldDescriptor* field, bool immutable, if (field->has_default_value()) { // See comments in Internal.java for gory details. return strings::Substitute( - "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")", - CEscape(field->default_value_string())); + "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")", + CEscape(field->default_value_string())); } else { return "com.google.protobuf.ByteString.EMPTY"; } @@ -534,14 +563,14 @@ string DefaultValue(const FieldDescriptor* field, bool immutable, case FieldDescriptor::CPPTYPE_ENUM: return name_resolver->GetClassName(field->enum_type(), immutable) + "." + - field->default_value_enum()->name(); + field->default_value_enum()->name(); case FieldDescriptor::CPPTYPE_MESSAGE: return name_resolver->GetClassName(field->message_type(), immutable) + - ".getDefaultInstance()"; + ".getDefaultInstance()"; - // No default because we want the compiler to complain if any new - // types are added. + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -572,8 +601,8 @@ bool IsDefaultValueJavaDefault(const FieldDescriptor* field) { case FieldDescriptor::CPPTYPE_MESSAGE: return false; - // No default because we want the compiler to complain if any new - // types are added. + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -586,123 +615,108 @@ bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field) { } const char* bit_masks[] = { - "0x00000001", - "0x00000002", - "0x00000004", - "0x00000008", - "0x00000010", - "0x00000020", - "0x00000040", - "0x00000080", - - "0x00000100", - "0x00000200", - "0x00000400", - "0x00000800", - "0x00001000", - "0x00002000", - "0x00004000", - "0x00008000", - - "0x00010000", - "0x00020000", - "0x00040000", - "0x00080000", - "0x00100000", - "0x00200000", - "0x00400000", - "0x00800000", - - "0x01000000", - "0x02000000", - "0x04000000", - "0x08000000", - "0x10000000", - "0x20000000", - "0x40000000", - "0x80000000", + "0x00000001", "0x00000002", "0x00000004", "0x00000008", + "0x00000010", "0x00000020", "0x00000040", "0x00000080", + + "0x00000100", "0x00000200", "0x00000400", "0x00000800", + "0x00001000", "0x00002000", "0x00004000", "0x00008000", + + "0x00010000", "0x00020000", "0x00040000", "0x00080000", + "0x00100000", "0x00200000", "0x00400000", "0x00800000", + + "0x01000000", "0x02000000", "0x04000000", "0x08000000", + "0x10000000", "0x20000000", "0x40000000", "0x80000000", }; -string GetBitFieldName(int index) { - string varName = "bitField"; - varName += SimpleItoa(index); +std::string GetBitFieldName(int index) { + std::string varName = "bitField"; + varName += StrCat(index); varName += "_"; return varName; } -string GetBitFieldNameForBit(int bitIndex) { +std::string GetBitFieldNameForBit(int bitIndex) { return GetBitFieldName(bitIndex / 32); } namespace { -string GenerateGetBitInternal(const string& prefix, int bitIndex) { - string varName = prefix + GetBitFieldNameForBit(bitIndex); +std::string GenerateGetBitInternal(const std::string& prefix, int bitIndex) { + std::string varName = prefix + GetBitFieldNameForBit(bitIndex); int bitInVarIndex = bitIndex % 32; - string mask = bit_masks[bitInVarIndex]; - string result = "((" + varName + " & " + mask + ") == " + mask + ")"; + std::string mask = bit_masks[bitInVarIndex]; + std::string result = "((" + varName + " & " + mask + ") != 0)"; return result; } -string GenerateSetBitInternal(const string& prefix, int bitIndex) { - string varName = prefix + GetBitFieldNameForBit(bitIndex); +std::string GenerateSetBitInternal(const std::string& prefix, int bitIndex) { + std::string varName = prefix + GetBitFieldNameForBit(bitIndex); int bitInVarIndex = bitIndex % 32; - string mask = bit_masks[bitInVarIndex]; - string result = varName + " |= " + mask; + std::string mask = bit_masks[bitInVarIndex]; + std::string result = varName + " |= " + mask; return result; } } // namespace -string GenerateGetBit(int bitIndex) { +std::string GenerateGetBit(int bitIndex) { return GenerateGetBitInternal("", bitIndex); } -string GenerateSetBit(int bitIndex) { +std::string GenerateSetBit(int bitIndex) { return GenerateSetBitInternal("", bitIndex); } -string GenerateClearBit(int bitIndex) { - string varName = GetBitFieldNameForBit(bitIndex); +std::string GenerateClearBit(int bitIndex) { + std::string varName = GetBitFieldNameForBit(bitIndex); int bitInVarIndex = bitIndex % 32; - string mask = bit_masks[bitInVarIndex]; - string result = varName + " = (" + varName + " & ~" + mask + ")"; + std::string mask = bit_masks[bitInVarIndex]; + std::string result = varName + " = (" + varName + " & ~" + mask + ")"; return result; } -string GenerateGetBitFromLocal(int bitIndex) { +std::string GenerateGetBitFromLocal(int bitIndex) { return GenerateGetBitInternal("from_", bitIndex); } -string GenerateSetBitToLocal(int bitIndex) { +std::string GenerateSetBitToLocal(int bitIndex) { return GenerateSetBitInternal("to_", bitIndex); } -string GenerateGetBitMutableLocal(int bitIndex) { +std::string GenerateGetBitMutableLocal(int bitIndex) { return GenerateGetBitInternal("mutable_", bitIndex); } -string GenerateSetBitMutableLocal(int bitIndex) { +std::string GenerateSetBitMutableLocal(int bitIndex) { return GenerateSetBitInternal("mutable_", bitIndex); } bool IsReferenceType(JavaType type) { switch (type) { - case JAVATYPE_INT : return false; - case JAVATYPE_LONG : return false; - case JAVATYPE_FLOAT : return false; - case JAVATYPE_DOUBLE : return false; - case JAVATYPE_BOOLEAN: return false; - case JAVATYPE_STRING : return true; - case JAVATYPE_BYTES : return true; - case JAVATYPE_ENUM : return true; - case JAVATYPE_MESSAGE: return true; + case JAVATYPE_INT: + return false; + case JAVATYPE_LONG: + return false; + case JAVATYPE_FLOAT: + return false; + case JAVATYPE_DOUBLE: + return false; + case JAVATYPE_BOOLEAN: + return false; + case JAVATYPE_STRING: + return true; + case JAVATYPE_BYTES: + return true; + case JAVATYPE_ENUM: + return true; + case JAVATYPE_MESSAGE: + return true; - // No default because we want the compiler to complain if any new - // JavaTypes are added. + // No default because we want the compiler to complain if any new + // JavaTypes are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -711,29 +725,46 @@ bool IsReferenceType(JavaType type) { const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable) { switch (GetType(field)) { - case FieldDescriptor::TYPE_INT32 : return "Int32"; - case FieldDescriptor::TYPE_UINT32 : return "UInt32"; - case FieldDescriptor::TYPE_SINT32 : return "SInt32"; - case FieldDescriptor::TYPE_FIXED32 : return "Fixed32"; - case FieldDescriptor::TYPE_SFIXED32: return "SFixed32"; - case FieldDescriptor::TYPE_INT64 : return "Int64"; - case FieldDescriptor::TYPE_UINT64 : return "UInt64"; - case FieldDescriptor::TYPE_SINT64 : return "SInt64"; - case FieldDescriptor::TYPE_FIXED64 : return "Fixed64"; - case FieldDescriptor::TYPE_SFIXED64: return "SFixed64"; - case FieldDescriptor::TYPE_FLOAT : return "Float"; - case FieldDescriptor::TYPE_DOUBLE : return "Double"; - case FieldDescriptor::TYPE_BOOL : return "Bool"; - case FieldDescriptor::TYPE_STRING : return "String"; - case FieldDescriptor::TYPE_BYTES : { + case FieldDescriptor::TYPE_INT32: + return "Int32"; + case FieldDescriptor::TYPE_UINT32: + return "UInt32"; + case FieldDescriptor::TYPE_SINT32: + return "SInt32"; + case FieldDescriptor::TYPE_FIXED32: + return "Fixed32"; + case FieldDescriptor::TYPE_SFIXED32: + return "SFixed32"; + case FieldDescriptor::TYPE_INT64: + return "Int64"; + case FieldDescriptor::TYPE_UINT64: + return "UInt64"; + case FieldDescriptor::TYPE_SINT64: + return "SInt64"; + case FieldDescriptor::TYPE_FIXED64: + return "Fixed64"; + case FieldDescriptor::TYPE_SFIXED64: + return "SFixed64"; + case FieldDescriptor::TYPE_FLOAT: + return "Float"; + case FieldDescriptor::TYPE_DOUBLE: + return "Double"; + case FieldDescriptor::TYPE_BOOL: + return "Bool"; + case FieldDescriptor::TYPE_STRING: + return "String"; + case FieldDescriptor::TYPE_BYTES: { return "Bytes"; } - case FieldDescriptor::TYPE_ENUM : return "Enum"; - case FieldDescriptor::TYPE_GROUP : return "Group"; - case FieldDescriptor::TYPE_MESSAGE : return "Message"; + case FieldDescriptor::TYPE_ENUM: + return "Enum"; + case FieldDescriptor::TYPE_GROUP: + return "Group"; + case FieldDescriptor::TYPE_MESSAGE: + return "Message"; - // No default because we want the compiler to complain if any new - // types are added. + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -744,29 +775,47 @@ const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable) { // returns -1. int FixedSize(FieldDescriptor::Type type) { switch (type) { - case FieldDescriptor::TYPE_INT32 : return -1; - case FieldDescriptor::TYPE_INT64 : return -1; - case FieldDescriptor::TYPE_UINT32 : return -1; - case FieldDescriptor::TYPE_UINT64 : return -1; - case FieldDescriptor::TYPE_SINT32 : return -1; - case FieldDescriptor::TYPE_SINT64 : return -1; - case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size; - case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size; - case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size; - case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size; - case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize; - case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize; - - case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize; - case FieldDescriptor::TYPE_ENUM : return -1; - - case FieldDescriptor::TYPE_STRING : return -1; - case FieldDescriptor::TYPE_BYTES : return -1; - case FieldDescriptor::TYPE_GROUP : return -1; - case FieldDescriptor::TYPE_MESSAGE : return -1; - - // No default because we want the compiler to complain if any new - // types are added. + case FieldDescriptor::TYPE_INT32: + return -1; + case FieldDescriptor::TYPE_INT64: + return -1; + case FieldDescriptor::TYPE_UINT32: + return -1; + case FieldDescriptor::TYPE_UINT64: + return -1; + case FieldDescriptor::TYPE_SINT32: + return -1; + case FieldDescriptor::TYPE_SINT64: + return -1; + case FieldDescriptor::TYPE_FIXED32: + return WireFormatLite::kFixed32Size; + case FieldDescriptor::TYPE_FIXED64: + return WireFormatLite::kFixed64Size; + case FieldDescriptor::TYPE_SFIXED32: + return WireFormatLite::kSFixed32Size; + case FieldDescriptor::TYPE_SFIXED64: + return WireFormatLite::kSFixed64Size; + case FieldDescriptor::TYPE_FLOAT: + return WireFormatLite::kFloatSize; + case FieldDescriptor::TYPE_DOUBLE: + return WireFormatLite::kDoubleSize; + + case FieldDescriptor::TYPE_BOOL: + return WireFormatLite::kBoolSize; + case FieldDescriptor::TYPE_ENUM: + return -1; + + case FieldDescriptor::TYPE_STRING: + return -1; + case FieldDescriptor::TYPE_BYTES: + return -1; + case FieldDescriptor::TYPE_GROUP: + return -1; + case FieldDescriptor::TYPE_MESSAGE: + return -1; + + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; return -1; @@ -776,7 +825,7 @@ int FixedSize(FieldDescriptor::Type type) { // and return it. The caller should delete the returned array. const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) { const FieldDescriptor** fields = - new const FieldDescriptor*[descriptor->field_count()]; + new const FieldDescriptor*[descriptor->field_count()]; for (int i = 0; i < descriptor->field_count(); i++) { fields[i] = descriptor->field(i); } @@ -948,7 +997,7 @@ int GetExperimentalJavaFieldType(const FieldDescriptor* field) { } // Escape a UTF-16 character to be embedded in a Java string. -void EscapeUtf16ToString(uint16 code, string* output) { +void EscapeUtf16ToString(uint16 code, std::string* output) { if (code == '\t') { output->append("\\t"); } else if (code == '\b') { diff --git a/src/google/protobuf/compiler/java/java_helpers.h b/src/google/protobuf/compiler/java/java_helpers.h index 375bd67d26..b06ab208f4 100644 --- a/src/google/protobuf/compiler/java/java_helpers.h +++ b/src/google/protobuf/compiler/java/java_helpers.h @@ -60,89 +60,84 @@ extern const char kThinSeparator[]; // annotation_file should be generated from the filename of the source file // being annotated (which in turn must be a Java identifier plus ".java"). void PrintGeneratedAnnotation(io::Printer* printer, char delimiter = '$', - const string& annotation_file = ""); + const std::string& annotation_file = ""); // If a GeneratedMessageLite contains non-lite enums, then its verifier // must be instantiated inline, rather than retrieved from the enum class. void PrintEnumVerifierLogic(io::Printer* printer, const FieldDescriptor* descriptor, - const std::map& variables, + const std::map& variables, const char* var_name, - const char* terminating_string, - bool enforce_lite); + const char* terminating_string, bool enforce_lite); // Converts a name to camel-case. If cap_first_letter is true, capitalize the // first letter. -string UnderscoresToCamelCase(const string& name, bool cap_first_letter); +std::string UnderscoresToCamelCase(const std::string& name, + bool cap_first_letter); // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes // "fooBarBaz" or "FooBarBaz", respectively. -string UnderscoresToCamelCase(const FieldDescriptor* field); -string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); +std::string UnderscoresToCamelCase(const FieldDescriptor* field); +std::string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); // Similar, but for method names. (Typically, this merely has the effect // of lower-casing the first letter of the name.) -string UnderscoresToCamelCase(const MethodDescriptor* method); +std::string UnderscoresToCamelCase(const MethodDescriptor* method); + +// Same as UnderscoresToCamelCase, but checks for reserved keywords +std::string UnderscoresToCamelCaseCheckReserved(const FieldDescriptor* field); // Similar to UnderscoresToCamelCase, but guarentees that the result is a // complete Java identifier by adding a _ if needed. -string CamelCaseFieldName(const FieldDescriptor* field); +std::string CamelCaseFieldName(const FieldDescriptor* field); // Get an identifier that uniquely identifies this type within the file. // This is used to declare static variables related to this type at the // outermost file scope. -string UniqueFileScopeIdentifier(const Descriptor* descriptor); +std::string UniqueFileScopeIdentifier(const Descriptor* descriptor); // Strips ".proto" or ".protodevel" from the end of a filename. -string StripProto(const string& filename); +std::string StripProto(const std::string& filename); // Gets the unqualified class name for the file. For each .proto file, there // will be one Java class containing all the immutable messages and another // Java class containing all the mutable messages. // TODO(xiaofeng): remove the default value after updating client code. -string FileClassName(const FileDescriptor* file, bool immutable = true); +std::string FileClassName(const FileDescriptor* file, bool immutable = true); // Returns the file's Java package name. -string FileJavaPackage(const FileDescriptor* file); -string FileJavaPackage(const FileDescriptor* file, bool immutable); +std::string FileJavaPackage(const FileDescriptor* file); +std::string FileJavaPackage(const FileDescriptor* file, bool immutable); // Returns output directory for the given package name. -string JavaPackageToDir(string package_name); - -// Converts the given fully-qualified name in the proto namespace to its -// fully-qualified name in the Java namespace, given that it is in the given -// file. -// TODO(xiaofeng): this method is deprecated and should be removed in the -// future. -string ToJavaName(const string& full_name, - const FileDescriptor* file); +std::string JavaPackageToDir(std::string package_name); // TODO(xiaofeng): the following methods are kept for they are exposed // publicly in //net/proto2/compiler/java/public/names.h. They return // immutable names only and should be removed after mutable API is // integrated into google3. -string ClassName(const Descriptor* descriptor); -string ClassName(const EnumDescriptor* descriptor); -string ClassName(const ServiceDescriptor* descriptor); -string ClassName(const FileDescriptor* descriptor); +std::string ClassName(const Descriptor* descriptor); +std::string ClassName(const EnumDescriptor* descriptor); +std::string ClassName(const ServiceDescriptor* descriptor); +std::string ClassName(const FileDescriptor* descriptor); // Comma-separate list of option-specified interfaces implemented by the // Message, to follow the "implements" declaration of the Message definition. -string ExtraMessageInterfaces(const Descriptor* descriptor); +std::string ExtraMessageInterfaces(const Descriptor* descriptor); // Comma-separate list of option-specified interfaces implemented by the // MutableMessage, to follow the "implements" declaration of the MutableMessage // definition. -string ExtraMutableMessageInterfaces(const Descriptor* descriptor); +std::string ExtraMutableMessageInterfaces(const Descriptor* descriptor); // Comma-separate list of option-specified interfaces implemented by the // Builder, to follow the "implements" declaration of the Builder definition. -string ExtraBuilderInterfaces(const Descriptor* descriptor); +std::string ExtraBuilderInterfaces(const Descriptor* descriptor); // Comma-separate list of option-specified interfaces extended by the // MessageOrBuilder, to follow the "extends" declaration of the // MessageOrBuilder definition. -string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor); +std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor); // Get the unqualified Java class name for mutable messages. i.e. without // package or outer classnames. -inline string ShortMutableJavaClassName(const Descriptor* descriptor) { +inline std::string ShortMutableJavaClassName(const Descriptor* descriptor) { return descriptor->name(); } @@ -154,10 +149,14 @@ inline bool IsDescriptorProto(const Descriptor* descriptor) { descriptor->file()->name() == "google/protobuf/descriptor.proto"; } +// Returns the stored type string used by the experimental runtime for oneof +// fields. +std::string GetOneofStoredType(const FieldDescriptor* field); + // Whether we should generate multiple java files for messages. -inline bool MultipleJavaFiles( - const FileDescriptor* descriptor, bool immutable) { +inline bool MultipleJavaFiles(const FileDescriptor* descriptor, + bool immutable) { return descriptor->options().java_multiple_files(); } @@ -179,14 +178,15 @@ inline bool IsOwnFile(const ServiceDescriptor* descriptor, bool immutable) { // annotation data for that descriptor. `suffix` is usually empty, but may // (e.g.) be "OrBuilder" for some generated interfaces. template -string AnnotationFileName(const Descriptor* descriptor, const string& suffix) { +std::string AnnotationFileName(const Descriptor* descriptor, + const std::string& suffix) { return descriptor->name() + suffix + ".java.pb.meta"; } template void MaybePrintGeneratedAnnotation(Context* context, io::Printer* printer, Descriptor* descriptor, bool immutable, - const string& suffix = "") { + const std::string& suffix = "") { if (context->options().annotate_code && IsOwnFile(descriptor, immutable)) { PrintGeneratedAnnotation(printer, '$', AnnotationFileName(descriptor, suffix)); @@ -195,7 +195,7 @@ void MaybePrintGeneratedAnnotation(Context* context, io::Printer* printer, // Get the unqualified name that should be used for a field's field // number constant. -string FieldConstantName(const FieldDescriptor *field); +std::string FieldConstantName(const FieldDescriptor* field); // Returns the type of the FieldDescriptor. // This does nothing interesting for the open source release, but is used for @@ -229,10 +229,10 @@ const char* BoxedPrimitiveTypeName(JavaType type); const char* FieldTypeName(const FieldDescriptor::Type field_type); class ClassNameResolver; -string DefaultValue(const FieldDescriptor* field, bool immutable, - ClassNameResolver* name_resolver); -inline string ImmutableDefaultValue(const FieldDescriptor* field, - ClassNameResolver* name_resolver) { +std::string DefaultValue(const FieldDescriptor* field, bool immutable, + ClassNameResolver* name_resolver); +inline std::string ImmutableDefaultValue(const FieldDescriptor* field, + ClassNameResolver* name_resolver) { return DefaultValue(field, true, name_resolver); } bool IsDefaultValueJavaDefault(const FieldDescriptor* field); @@ -241,24 +241,19 @@ bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field); // Does this message class have descriptor and reflection methods? inline bool HasDescriptorMethods(const Descriptor* descriptor, bool enforce_lite) { - return !enforce_lite && - descriptor->file()->options().optimize_for() != - FileOptions::LITE_RUNTIME; + return !enforce_lite; } inline bool HasDescriptorMethods(const EnumDescriptor* descriptor, bool enforce_lite) { - return !enforce_lite && - descriptor->file()->options().optimize_for() != - FileOptions::LITE_RUNTIME; + return !enforce_lite; } inline bool HasDescriptorMethods(const FileDescriptor* descriptor, bool enforce_lite) { - return !enforce_lite && - descriptor->options().optimize_for() != FileOptions::LITE_RUNTIME; + return !enforce_lite; } // Should we generate generic services for this file? -inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) { +inline bool HasGenericServices(const FileDescriptor* file, bool enforce_lite) { return file->service_count() > 0 && HasDescriptorMethods(file, enforce_lite) && file->options().java_generic_services(); @@ -267,50 +262,50 @@ inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) { // Methods for shared bitfields. // Gets the name of the shared bitfield for the given index. -string GetBitFieldName(int index); +std::string GetBitFieldName(int index); // Gets the name of the shared bitfield for the given bit index. // Effectively, GetBitFieldName(bitIndex / 32) -string GetBitFieldNameForBit(int bitIndex); +std::string GetBitFieldNameForBit(int bitIndex); // Generates the java code for the expression that returns the boolean value // of the bit of the shared bitfields for the given bit index. // Example: "((bitField1_ & 0x04) == 0x04)" -string GenerateGetBit(int bitIndex); +std::string GenerateGetBit(int bitIndex); // Generates the java code for the expression that sets the bit of the shared // bitfields for the given bit index. // Example: "bitField1_ = (bitField1_ | 0x04)" -string GenerateSetBit(int bitIndex); +std::string GenerateSetBit(int bitIndex); // Generates the java code for the expression that clears the bit of the shared // bitfields for the given bit index. // Example: "bitField1_ = (bitField1_ & ~0x04)" -string GenerateClearBit(int bitIndex); +std::string GenerateClearBit(int bitIndex); // Does the same as GenerateGetBit but operates on the bit field on a local // variable. This is used by the builder to copy the value in the builder to // the message. // Example: "((from_bitField1_ & 0x04) == 0x04)" -string GenerateGetBitFromLocal(int bitIndex); +std::string GenerateGetBitFromLocal(int bitIndex); // Does the same as GenerateSetBit but operates on the bit field on a local // variable. This is used by the builder to copy the value in the builder to // the message. // Example: "to_bitField1_ = (to_bitField1_ | 0x04)" -string GenerateSetBitToLocal(int bitIndex); +std::string GenerateSetBitToLocal(int bitIndex); // Does the same as GenerateGetBit but operates on the bit field on a local // variable. This is used by the parsing constructor to record if a repeated // field is mutable. // Example: "((mutable_bitField1_ & 0x04) == 0x04)" -string GenerateGetBitMutableLocal(int bitIndex); +std::string GenerateGetBitMutableLocal(int bitIndex); // Does the same as GenerateSetBit but operates on the bit field on a local // variable. This is used by the parsing constructor to record if a repeated // field is mutable. // Example: "mutable_bitField1_ = (mutable_bitField1_ | 0x04)" -string GenerateSetBitMutableLocal(int bitIndex); +std::string GenerateSetBitMutableLocal(int bitIndex); // Returns whether the JavaType is a reference type. bool IsReferenceType(JavaType type); @@ -395,10 +390,10 @@ inline bool IsWrappersProtoFile(const FileDescriptor* descriptor) { inline bool CheckUtf8(const FieldDescriptor* descriptor) { return descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 || - descriptor->file()->options().java_string_check_utf8(); + descriptor->file()->options().java_string_check_utf8(); } -inline string GeneratedCodeVersionSuffix() { +inline std::string GeneratedCodeVersionSuffix() { return "V3"; } @@ -410,7 +405,7 @@ inline void WriteIntToUtf16CharSequence(int value, } // Escape a UTF-16 character so it can be embedded in a Java string literal. -void EscapeUtf16ToString(uint16 code, string* output); +void EscapeUtf16ToString(uint16 code, std::string* output); // Only the lowest two bytes of the return value are used. The lowest byte // is the integer value of a j/c/g/protobuf/FieldType enum. For the other diff --git a/src/google/protobuf/compiler/java/java_map_field.cc b/src/google/protobuf/compiler/java/java_map_field.cc index b22a21993f..5353745879 100644 --- a/src/google/protobuf/compiler/java/java_map_field.cc +++ b/src/google/protobuf/compiler/java/java_map_field.cc @@ -57,9 +57,8 @@ const FieldDescriptor* ValueField(const FieldDescriptor* descriptor) { return message->FindFieldByName("value"); } -string TypeName(const FieldDescriptor* field, - ClassNameResolver* name_resolver, - bool boxed) { +std::string TypeName(const FieldDescriptor* field, + ClassNameResolver* name_resolver, bool boxed) { if (GetJavaType(field) == JAVATYPE_MESSAGE) { return name_resolver->GetImmutableClassName(field->message_type()); } else if (GetJavaType(field) == JAVATYPE_ENUM) { @@ -70,17 +69,15 @@ string TypeName(const FieldDescriptor* field, } } -string WireType(const FieldDescriptor* field) { +std::string WireType(const FieldDescriptor* field) { return "com.google.protobuf.WireFormat.FieldType." + - string(FieldTypeName(field->type())); + std::string(FieldTypeName(field->type())); } -void SetMessageVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - const FieldGeneratorInfo* info, +void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, + int builderBitIndex, const FieldGeneratorInfo* info, Context* context, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); ClassNameResolver* name_resolver = context->GetNameResolver(); @@ -92,17 +89,21 @@ void SetMessageVariables(const FieldDescriptor* descriptor, const JavaType valueJavaType = GetJavaType(value); (*variables)["key_type"] = TypeName(key, name_resolver, false); - string boxed_key_type = TypeName(key, name_resolver, true); + std::string boxed_key_type = TypeName(key, name_resolver, true); (*variables)["boxed_key_type"] = boxed_key_type; // Used for calling the serialization function. (*variables)["short_key_type"] = boxed_key_type.substr(boxed_key_type.rfind('.') + 1); (*variables)["key_wire_type"] = WireType(key); (*variables)["key_default_value"] = DefaultValue(key, true, name_resolver); - (*variables)["key_null_check"] = IsReferenceType(keyJavaType) ? - "if (key == null) { throw new java.lang.NullPointerException(); }" : ""; - (*variables)["value_null_check"] = IsReferenceType(valueJavaType) ? - "if (value == null) { throw new java.lang.NullPointerException(); }" : ""; + (*variables)["key_null_check"] = + IsReferenceType(keyJavaType) + ? "if (key == null) { throw new java.lang.NullPointerException(); }" + : ""; + (*variables)["value_null_check"] = + IsReferenceType(valueJavaType) + ? "if (value == null) { throw new java.lang.NullPointerException(); }" + : ""; if (valueJavaType == JAVATYPE_ENUM) { // We store enums as Integers internally. (*variables)["value_type"] = "int"; @@ -133,8 +134,8 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"]; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["on_changed"] = "onChanged();"; // For repeated fields, one bit is used for whether the array is immutable @@ -144,62 +145,51 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["set_mutable_bit_parser"] = GenerateSetBitMutableLocal(builderBitIndex); - (*variables)["default_entry"] = (*variables)["capitalized_name"] + - "DefaultEntryHolder.defaultEntry"; + (*variables)["default_entry"] = + (*variables)["capitalized_name"] + "DefaultEntryHolder.defaultEntry"; (*variables)["map_field_parameter"] = (*variables)["default_entry"]; (*variables)["descriptor"] = - name_resolver->GetImmutableClassName(descriptor->file()) + - ".internal_" + UniqueFileScopeIdentifier(descriptor->message_type()) + - "_descriptor, "; + name_resolver->GetImmutableClassName(descriptor->file()) + ".internal_" + + UniqueFileScopeIdentifier(descriptor->message_type()) + "_descriptor, "; (*variables)["ver"] = GeneratedCodeVersionSuffix(); } } // namespace -ImmutableMapFieldGenerator:: -ImmutableMapFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { +ImmutableMapFieldGenerator::ImmutableMapFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetMessageVariables(descriptor, messageBitIndex, builderBitIndex, - context->GetFieldGeneratorInfo(descriptor), - context, &variables_); + context->GetFieldGeneratorInfo(descriptor), context, + &variables_); } -ImmutableMapFieldGenerator:: -~ImmutableMapFieldGenerator() {} +ImmutableMapFieldGenerator::~ImmutableMapFieldGenerator() {} -int ImmutableMapFieldGenerator::GetNumBitsForMessage() const { - return 0; -} +int ImmutableMapFieldGenerator::GetNumBitsForMessage() const { return 0; } -int ImmutableMapFieldGenerator::GetNumBitsForBuilder() const { - return 1; -} +int ImmutableMapFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void ImmutableMapFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); + printer->Print(variables_, + "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$();\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -231,63 +221,55 @@ GenerateInterfaceMembers(io::Printer* printer) const { "${$get$capitalized_name$Value$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$ValueMap$}$();\n"); + printer->Print(variables_, + "$deprecation$java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$ValueMap$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" - " $key_type$ key,\n" - " $value_type$ defaultValue);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" + " $key_type$ key,\n" + " $value_type$ defaultValue);\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$$}$();\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$Map$}$();\n"); + printer->Print(variables_, + "$deprecation$java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$Map$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" - " $key_type$ key,\n" - " $value_type$ defaultValue);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" + " $key_type$ key,\n" + " $value_type$ defaultValue);\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); } } -void ImmutableMapFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateMembers(io::Printer* printer) const { printer->Print( variables_, "private static final class $capitalized_name$DefaultEntryHolder {\n" @@ -301,18 +283,17 @@ GenerateMembers(io::Printer* printer) const { " $value_wire_type$,\n" " $value_default_value$);\n" "}\n"); - printer->Print( - variables_, - "private com.google.protobuf.MapField<\n" - " $type_parameters$> $name$_;\n" - "private com.google.protobuf.MapField<$type_parameters$>\n" - "internalGet$capitalized_name$() {\n" - " if ($name$_ == null) {\n" - " return com.google.protobuf.MapField.emptyMapField(\n" - " $map_field_parameter$);\n" - " }\n" - " return $name$_;\n" - "}\n"); + printer->Print(variables_, + "private com.google.protobuf.MapField<\n" + " $type_parameters$> $name$_;\n" + "private com.google.protobuf.MapField<$type_parameters$>\n" + "internalGet$capitalized_name$() {\n" + " if ($name$_ == null) {\n" + " return com.google.protobuf.MapField.emptyMapField(\n" + " $map_field_parameter$);\n" + " }\n" + " return $name$_;\n" + "}\n"); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( variables_, @@ -336,53 +317,50 @@ GenerateMembers(io::Printer* printer) const { GenerateMapGetters(printer); } -void ImmutableMapFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print( - variables_, - "private com.google.protobuf.MapField<\n" - " $type_parameters$> $name$_;\n" - "private com.google.protobuf.MapField<$type_parameters$>\n" - "internalGet$capitalized_name$() {\n" - " if ($name$_ == null) {\n" - " return com.google.protobuf.MapField.emptyMapField(\n" - " $map_field_parameter$);\n" - " }\n" - " return $name$_;\n" - "}\n" - "private com.google.protobuf.MapField<$type_parameters$>\n" - "internalGetMutable$capitalized_name$() {\n" - " $on_changed$;\n" - " if ($name$_ == null) {\n" - " $name$_ = com.google.protobuf.MapField.newMapField(\n" - " $map_field_parameter$);\n" - " }\n" - " if (!$name$_.isMutable()) {\n" - " $name$_ = $name$_.copy();\n" - " }\n" - " return $name$_;\n" - "}\n"); +void ImmutableMapFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print(variables_, + "private com.google.protobuf.MapField<\n" + " $type_parameters$> $name$_;\n" + "private com.google.protobuf.MapField<$type_parameters$>\n" + "internalGet$capitalized_name$() {\n" + " if ($name$_ == null) {\n" + " return com.google.protobuf.MapField.emptyMapField(\n" + " $map_field_parameter$);\n" + " }\n" + " return $name$_;\n" + "}\n" + "private com.google.protobuf.MapField<$type_parameters$>\n" + "internalGetMutable$capitalized_name$() {\n" + " $on_changed$;\n" + " if ($name$_ == null) {\n" + " $name$_ = com.google.protobuf.MapField.newMapField(\n" + " $map_field_parameter$);\n" + " }\n" + " if (!$name$_.isMutable()) {\n" + " $name$_ = $name$_.copy();\n" + " }\n" + " return $name$_;\n" + "}\n"); GenerateMapGetters(printer); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$clear$capitalized_name$$}$() {\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .clear();\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$clear$capitalized_name$$}$() {\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .clear();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$remove$capitalized_name$$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .remove(key);\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$remove$capitalized_name$$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .remove(key);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( @@ -468,41 +446,38 @@ GenerateBuilderMembers(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$" - "public Builder ${$put$capitalized_name$$}$(\n" - " $key_type$ key,\n" - " $value_type$ value) {\n" - " $key_null_check$\n" - " $value_null_check$\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .put(key, value);\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$" + "public Builder ${$put$capitalized_name$$}$(\n" + " $key_type$ key,\n" + " $value_type$ value) {\n" + " $key_null_check$\n" + " $value_null_check$\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .put(key, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$putAll$capitalized_name$$}$(\n" - " java.util.Map<$type_parameters$> values) {\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .putAll(values);\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$putAll$capitalized_name$$}$(\n" + " java.util.Map<$type_parameters$> values) {\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .putAll(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } -void ImmutableMapFieldGenerator:: -GenerateMapGetters(io::Printer* printer) const { - printer->Print( - variables_, - "$deprecation$\n" - "public int ${$get$capitalized_name$Count$}$() {\n" - " return internalGet$capitalized_name$().getMap().size();\n" - "}\n"); +void ImmutableMapFieldGenerator::GenerateMapGetters( + io::Printer* printer) const { + printer->Print(variables_, + "$deprecation$\n" + "public int ${$get$capitalized_name$Count$}$() {\n" + " return internalGet$capitalized_name$().getMap().size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -515,26 +490,24 @@ GenerateMapGetters(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$Map$}$() {\n" - " return internalGetAdapted$capitalized_name$Map(\n" - " internalGet$capitalized_name$().getMap());" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$Map$}$() {\n" + " return internalGetAdapted$capitalized_name$Map(\n" + " internalGet$capitalized_name$().getMap());" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -617,25 +590,23 @@ GenerateMapGetters(io::Printer* printer) const { printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$Map$}$() {\n" - " return internalGet$capitalized_name$().getMap();\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" + " return internalGet$capitalized_name$().getMap();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -651,65 +622,60 @@ GenerateMapGetters(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " java.util.Map<$type_parameters$> map =\n" - " internalGet$capitalized_name$().getMap();\n" - " if (!map.containsKey(key)) {\n" - " throw new java.lang.IllegalArgumentException();\n" - " }\n" - " return map.get(key);\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " java.util.Map<$type_parameters$> map =\n" + " internalGet$capitalized_name$().getMap();\n" + " if (!map.containsKey(key)) {\n" + " throw new java.lang.IllegalArgumentException();\n" + " }\n" + " return map.get(key);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } -void ImmutableMapFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { // Nothing to initialize. } -void ImmutableMapFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { // Nothing to initialize. } -void ImmutableMapFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { - printer->Print( - variables_, - "internalGetMutable$capitalized_name$().clear();\n"); +void ImmutableMapFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { + printer->Print(variables_, + "internalGetMutable$capitalized_name$().clear();\n"); } -void ImmutableMapFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { - printer->Print( - variables_, - "internalGetMutable$capitalized_name$().mergeFrom(\n" - " other.internalGet$capitalized_name$());\n"); +void ImmutableMapFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { + printer->Print(variables_, + "internalGetMutable$capitalized_name$().mergeFrom(\n" + " other.internalGet$capitalized_name$());\n"); } -void ImmutableMapFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - printer->Print( - variables_, - "result.$name$_ = internalGet$capitalized_name$();\n" - "result.$name$_.makeImmutable();\n"); +void ImmutableMapFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { + printer->Print(variables_, + "result.$name$_ = internalGet$capitalized_name$();\n" + "result.$name$_.makeImmutable();\n"); } -void ImmutableMapFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print( - variables_, - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = com.google.protobuf.MapField.newMapField(\n" - " $map_field_parameter$);\n" - " $set_mutable_bit_parser$;\n" - "}\n"); +void ImmutableMapFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { + printer->Print(variables_, + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = com.google.protobuf.MapField.newMapField(\n" + " $map_field_parameter$);\n" + " $set_mutable_bit_parser$;\n" + "}\n"); if (!SupportUnknownEnumValue(descriptor_->file()) && GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( @@ -736,25 +702,24 @@ GenerateParsingCode(io::Printer* printer) const { } } -void ImmutableMapFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // Nothing to do here. } -void ImmutableMapFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print( - variables_, - "com.google.protobuf.GeneratedMessage$ver$\n" - " .serialize$short_key_type$MapTo(\n" - " output,\n" - " internalGet$capitalized_name$(),\n" - " $default_entry$,\n" - " $number$);\n"); +void ImmutableMapFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { + printer->Print(variables_, + "com.google.protobuf.GeneratedMessage$ver$\n" + " .serialize$short_key_type$MapTo(\n" + " output,\n" + " internalGet$capitalized_name$(),\n" + " $default_entry$,\n" + " $number$);\n"); } -void ImmutableMapFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print( variables_, "for (java.util.Map.Entry<$type_parameters$> entry\n" @@ -769,16 +734,14 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "}\n"); } -void ImmutableMapFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print( - variables_, - "result = result && internalGet$capitalized_name$().equals(\n" - " other.internalGet$capitalized_name$());\n"); +void ImmutableMapFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print(variables_, + "if (!internalGet$capitalized_name$().equals(\n" + " other.internalGet$capitalized_name$())) return false;\n"); } -void ImmutableMapFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateHashCode(io::Printer* printer) const { printer->Print( variables_, "if (!internalGet$capitalized_name$().getMap().isEmpty()) {\n" @@ -787,7 +750,7 @@ GenerateHashCode(io::Printer* printer) const { "}\n"); } -string ImmutableMapFieldGenerator::GetBoxedType() const { +std::string ImmutableMapFieldGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } diff --git a/src/google/protobuf/compiler/java/java_map_field.h b/src/google/protobuf/compiler/java/java_map_field.h index 90c90eb2d6..2ff1f7673e 100644 --- a/src/google/protobuf/compiler/java/java_map_field.h +++ b/src/google/protobuf/compiler/java/java_map_field.h @@ -40,9 +40,9 @@ namespace java { class ImmutableMapFieldGenerator : public ImmutableFieldGenerator { public: - explicit ImmutableMapFieldGenerator( - const FieldDescriptor* descriptor, int messageBitIndex, - int builderBitIndex, Context* context); + explicit ImmutableMapFieldGenerator(const FieldDescriptor* descriptor, + int messageBitIndex, int builderBitIndex, + Context* context); ~ImmutableMapFieldGenerator(); // implements ImmutableFieldGenerator --------------------------------------- @@ -63,11 +63,11 @@ class ImmutableMapFieldGenerator : public ImmutableFieldGenerator { void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; private: const FieldDescriptor* descriptor_; - std::map variables_; + std::map variables_; ClassNameResolver* name_resolver_; void GenerateMapGetters(io::Printer* printer) const; }; diff --git a/src/google/protobuf/compiler/java/java_map_field_lite.cc b/src/google/protobuf/compiler/java/java_map_field_lite.cc index 569c38d789..9055774844 100644 --- a/src/google/protobuf/compiler/java/java_map_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_map_field_lite.cc @@ -57,9 +57,8 @@ const FieldDescriptor* ValueField(const FieldDescriptor* descriptor) { return message->FindFieldByName("value"); } -string TypeName(const FieldDescriptor* field, - ClassNameResolver* name_resolver, - bool boxed) { +std::string TypeName(const FieldDescriptor* field, + ClassNameResolver* name_resolver, bool boxed) { if (GetJavaType(field) == JAVATYPE_MESSAGE) { return name_resolver->GetImmutableClassName(field->message_type()); } else if (GetJavaType(field) == JAVATYPE_ENUM) { @@ -70,17 +69,15 @@ string TypeName(const FieldDescriptor* field, } } -string WireType(const FieldDescriptor* field) { +std::string WireType(const FieldDescriptor* field) { return "com.google.protobuf.WireFormat.FieldType." + - string(FieldTypeName(field->type())); + std::string(FieldTypeName(field->type())); } -void SetMessageVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - const FieldGeneratorInfo* info, +void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, + int builderBitIndex, const FieldGeneratorInfo* info, Context* context, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); ClassNameResolver* name_resolver = context->GetNameResolver(); @@ -95,10 +92,14 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["boxed_key_type"] = TypeName(key, name_resolver, true); (*variables)["key_wire_type"] = WireType(key); (*variables)["key_default_value"] = DefaultValue(key, true, name_resolver); - (*variables)["key_null_check"] = IsReferenceType(keyJavaType) ? - "if (key == null) { throw new java.lang.NullPointerException(); }" : ""; - (*variables)["value_null_check"] = IsReferenceType(valueJavaType) ? - "if (value == null) { throw new java.lang.NullPointerException(); }" : ""; + (*variables)["key_null_check"] = + IsReferenceType(keyJavaType) + ? "if (key == null) { throw new java.lang.NullPointerException(); }" + : ""; + (*variables)["value_null_check"] = + IsReferenceType(valueJavaType) + ? "if (value == null) { throw new java.lang.NullPointerException(); }" + : ""; if (GetJavaType(value) == JAVATYPE_ENUM) { // We store enums as Integers internally. @@ -130,55 +131,48 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"]; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - (*variables)["default_entry"] = (*variables)["capitalized_name"] + - "DefaultEntryHolder.defaultEntry"; + (*variables)["default_entry"] = + (*variables)["capitalized_name"] + "DefaultEntryHolder.defaultEntry"; } } // namespace -ImmutableMapFieldLiteGenerator:: -ImmutableMapFieldLiteGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()){ +ImmutableMapFieldLiteGenerator::ImmutableMapFieldLiteGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, Context* context) + : descriptor_(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()) { SetMessageVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), context, &variables_); } -ImmutableMapFieldLiteGenerator:: -~ImmutableMapFieldLiteGenerator() {} +ImmutableMapFieldLiteGenerator::~ImmutableMapFieldLiteGenerator() {} -int ImmutableMapFieldLiteGenerator::GetNumBitsForMessage() const { - return 0; -} +int ImmutableMapFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void ImmutableMapFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableMapFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); + printer->Print(variables_, + "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$();\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -210,63 +204,56 @@ GenerateInterfaceMembers(io::Printer* printer) const { "${$get$capitalized_name$Value$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$ValueMap$}$();\n"); + printer->Print(variables_, + "$deprecation$java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$ValueMap$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" - " $key_type$ key,\n" - " $value_type$ defaultValue);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" + " $key_type$ key,\n" + " $value_type$ defaultValue);\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$$}$();\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$Map$}$();\n"); + printer->Print(variables_, + "$deprecation$java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$Map$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" - " $key_type$ key,\n" - " $value_type$ defaultValue);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" + " $key_type$ key,\n" + " $value_type$ defaultValue);\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); } } -void ImmutableMapFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMapFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { printer->Print( variables_, "private static final class $capitalized_name$DefaultEntryHolder {\n" @@ -279,40 +266,37 @@ GenerateMembers(io::Printer* printer) const { " $value_wire_type$,\n" " $value_default_value$);\n" "}\n"); - printer->Print( - variables_, - "private com.google.protobuf.MapFieldLite<\n" - " $type_parameters$> $name$_ =\n" - " com.google.protobuf.MapFieldLite.emptyMapField();\n" - "private com.google.protobuf.MapFieldLite<$type_parameters$>\n" - "internalGet$capitalized_name$() {\n" - " return $name$_;\n" - "}\n" - "private com.google.protobuf.MapFieldLite<$type_parameters$>\n" - "internalGetMutable$capitalized_name$() {\n" - " if (!$name$_.isMutable()) {\n" - " $name$_ = $name$_.mutableCopy();\n" - " }\n" - " return $name$_;\n" - "}\n"); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public int ${$get$capitalized_name$Count$}$() {\n" - " return internalGet$capitalized_name$().size();\n" - "}\n"); + printer->Print(variables_, + "private com.google.protobuf.MapFieldLite<\n" + " $type_parameters$> $name$_ =\n" + " com.google.protobuf.MapFieldLite.emptyMapField();\n" + "private com.google.protobuf.MapFieldLite<$type_parameters$>\n" + "internalGet$capitalized_name$() {\n" + " return $name$_;\n" + "}\n" + "private com.google.protobuf.MapFieldLite<$type_parameters$>\n" + "internalGetMutable$capitalized_name$() {\n" + " if (!$name$_.isMutable()) {\n" + " $name$_ = $name$_.mutableCopy();\n" + " }\n" + " return $name$_;\n" + "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public int ${$get$capitalized_name$Count$}$() {\n" + " return internalGet$capitalized_name$().size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public boolean ${$contains$capitalized_name$$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " return internalGet$capitalized_name$().containsKey(key);\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public boolean ${$contains$capitalized_name$$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " return internalGet$capitalized_name$().containsKey(key);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( @@ -323,16 +307,15 @@ GenerateMembers(io::Printer* printer) const { " com.google.protobuf.Internal.MapAdapter.newEnumConverter(\n" " $value_enum_type$.internalGetValueMap(),\n" " $unrecognized_value$);\n"); - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -436,28 +419,26 @@ GenerateMembers(io::Printer* printer) const { printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Override\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Override\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$Map$}$() {\n" - " return java.util.Collections.unmodifiableMap(\n" - " internalGet$capitalized_name$());\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" + " return java.util.Collections.unmodifiableMap(\n" + " internalGet$capitalized_name$());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -474,20 +455,19 @@ GenerateMembers(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " java.util.Map<$type_parameters$> map =\n" - " internalGet$capitalized_name$();\n" - " if (!map.containsKey(key)) {\n" - " throw new java.lang.IllegalArgumentException();\n" - " }\n" - " return map.get(key);\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " java.util.Map<$type_parameters$> map =\n" + " internalGet$capitalized_name$();\n" + " if (!map.containsKey(key)) {\n" + " throw new java.lang.IllegalArgumentException();\n" + " }\n" + " return map.get(key);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } @@ -514,25 +494,39 @@ GenerateMembers(io::Printer* printer) const { } } else { WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "private java.util.Map<$type_parameters$>\n" - "getMutable$capitalized_name$Map() {\n" - " return internalGetMutable$capitalized_name$();\n" - "}\n"); + printer->Print(variables_, + "private java.util.Map<$type_parameters$>\n" + "getMutable$capitalized_name$Map() {\n" + " return internalGetMutable$capitalized_name$();\n" + "}\n"); } } +void ImmutableMapFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + printer->Print(variables_, + "\"$name$_\",\n" + "$default_entry$,\n"); + if (SupportFieldPresence(descriptor_->file()) && + GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { + PrintEnumVerifierLogic(printer, ValueField(descriptor_), variables_, + /*var_name=*/"$value_enum_type$", + /*terminating_string=*/",\n", + /*enforce_lite=*/context_->EnforceLite()); + } +} -void ImmutableMapFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Map().size();\n" - "}\n"); +void ImmutableMapFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Map().size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -545,49 +539,45 @@ GenerateBuilderMembers(io::Printer* printer) const { " return instance.get$capitalized_name$Map().containsKey(key);\n" "}\n"); printer->Annotate("{", "}", descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.getMutable$capitalized_name$Map().clear();\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.getMutable$capitalized_name$Map().clear();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$remove$capitalized_name$$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " copyOnWrite();\n" - " instance.getMutable$capitalized_name$Map().remove(key);\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$remove$capitalized_name$$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " copyOnWrite();\n" + " instance.getMutable$capitalized_name$Map().remove(key);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$Map$}$() {\n" - " return java.util.Collections.unmodifiableMap(\n" - " instance.get$capitalized_name$Map());\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$Map$}$() {\n" + " return java.util.Collections.unmodifiableMap(\n" + " instance.get$capitalized_name$Map());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -722,28 +712,26 @@ GenerateBuilderMembers(io::Printer* printer) const { printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Override\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Override\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$Map$}$() {\n" - " return java.util.Collections.unmodifiableMap(\n" - " instance.get$capitalized_name$Map());\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" + " return java.util.Collections.unmodifiableMap(\n" + " instance.get$capitalized_name$Map());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -760,20 +748,19 @@ GenerateBuilderMembers(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " java.util.Map<$type_parameters$> map =\n" - " instance.get$capitalized_name$Map();\n" - " if (!map.containsKey(key)) {\n" - " throw new java.lang.IllegalArgumentException();\n" - " }\n" - " return map.get(key);\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " java.util.Map<$type_parameters$> map =\n" + " instance.get$capitalized_name$Map();\n" + " if (!map.containsKey(key)) {\n" + " throw new java.lang.IllegalArgumentException();\n" + " }\n" + " return map.get(key);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -803,104 +790,12 @@ GenerateBuilderMembers(io::Printer* printer) const { } } -void ImmutableMapFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableMapFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { // Nothing to initialize. } -void ImmutableMapFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - // Nothing to initialize. -} - -void ImmutableMapFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print( - variables_, - "$name$_ = visitor.visitMap(\n" - " $name$_, other.internalGet$capitalized_name$());\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print( - variables_, - "if (!$name$_.isMutable()) {\n" - " $name$_ = $name$_.mutableCopy();\n" - "}\n"); - if (!SupportUnknownEnumValue(descriptor_->file()) && - GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "com.google.protobuf.ByteString bytes = input.readBytes();\n" - "java.util.Map.Entry<$type_parameters$> $name$__ =\n" - " $default_entry$.parseEntry(bytes, extensionRegistry);\n"); - printer->Print( - variables_, - "if ($value_enum_type$.forNumber($name$__.getValue()) == null) {\n" - " super.mergeLengthDelimitedField($number$, bytes);\n" - "} else {\n" - " $name$_.put($name$__);\n" - "}\n"); - } else { - printer->Print( - variables_, - "$default_entry$.parseInto($name$_, input, extensionRegistry);"); - } -} - -void ImmutableMapFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // Nothing to do here. -} - -void ImmutableMapFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print( - variables_, - "for (java.util.Map.Entry<$type_parameters$> entry\n" - " : internalGet$capitalized_name$().entrySet()) {\n" - " $default_entry$.serializeTo(\n" - " output, $number$, entry.getKey(), entry.getValue());\n" - "}\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print( - variables_, - "for (java.util.Map.Entry<$type_parameters$> entry\n" - " : internalGet$capitalized_name$().entrySet()) {\n" - " size += $default_entry$.computeMessageSize(\n" - " $number$, entry.getKey(), entry.getValue());\n" - "}\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print( - variables_, - "result = result && internalGet$capitalized_name$().equals(\n" - " other.internalGet$capitalized_name$());\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print( - variables_, - "if (!internalGet$capitalized_name$().isEmpty()) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + internalGet$capitalized_name$().hashCode();\n" - "}\n"); -} - -string ImmutableMapFieldLiteGenerator::GetBoxedType() const { +std::string ImmutableMapFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } diff --git a/src/google/protobuf/compiler/java/java_map_field_lite.h b/src/google/protobuf/compiler/java/java_map_field_lite.h index 710617ae63..49cbf6cca7 100644 --- a/src/google/protobuf/compiler/java/java_map_field_lite.h +++ b/src/google/protobuf/compiler/java/java_map_field_lite.h @@ -51,22 +51,14 @@ class ImmutableMapFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; - - string GetBoxedType() const; + std::string GetBoxedType() const; private: const FieldDescriptor* descriptor_; - std::map variables_; + std::map variables_; Context* context_; ClassNameResolver* name_resolver_; }; diff --git a/src/google/protobuf/compiler/java/java_message.cc b/src/google/protobuf/compiler/java/java_message.cc index 151422310a..63bbe02016 100644 --- a/src/google/protobuf/compiler/java/java_message.cc +++ b/src/google/protobuf/compiler/java/java_message.cc @@ -56,6 +56,7 @@ #include + namespace google { namespace protobuf { namespace compiler { @@ -65,13 +66,8 @@ using internal::WireFormat; using internal::WireFormatLite; namespace { -bool GenerateHasBits(const Descriptor* descriptor) { - return SupportFieldPresence(descriptor->file()) || - HasRepeatedFields(descriptor); -} - -string MapValueImmutableClassdName(const Descriptor* descriptor, - ClassNameResolver* name_resolver) { +std::string MapValueImmutableClassdName(const Descriptor* descriptor, + ClassNameResolver* name_resolver) { const FieldDescriptor* value_field = descriptor->FindFieldByName("value"); GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, value_field->type()); return name_resolver->GetImmutableClassName(value_field->message_type()); @@ -81,16 +77,17 @@ string MapValueImmutableClassdName(const Descriptor* descriptor, // =================================================================== MessageGenerator::MessageGenerator(const Descriptor* descriptor) - : descriptor_(descriptor) {} + : descriptor_(descriptor) {} MessageGenerator::~MessageGenerator() {} // =================================================================== ImmutableMessageGenerator::ImmutableMessageGenerator( const Descriptor* descriptor, Context* context) - : MessageGenerator(descriptor), context_(context), - name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + : MessageGenerator(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()), + field_generators_(descriptor, context_) { GOOGLE_CHECK(HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A non-lite message generator is used to " "generate lite messages."; @@ -107,13 +104,12 @@ void ImmutableMessageGenerator::GenerateStaticVariables( // the outermost class in the file. This way, they will be initialized in // a deterministic order. - std::map vars; + std::map vars; vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); - vars["index"] = SimpleItoa(descriptor_->index()); + vars["index"] = StrCat(descriptor_->index()); vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_); if (descriptor_->containing_type() != NULL) { - vars["parent"] = UniqueFileScopeIdentifier( - descriptor_->containing_type()); + vars["parent"] = UniqueFileScopeIdentifier(descriptor_->containing_type()); } if (MultipleJavaFiles(descriptor_->file(), /* immutable = */ true)) { // We can only make these package-private since the classes that use them @@ -129,12 +125,13 @@ void ImmutableMessageGenerator::GenerateStaticVariables( } // The descriptor for this type. - printer->Print(vars, - // TODO(teboring): final needs to be added back. The way to fix it is to - // generate methods that can construct the types, and then still declare the - // types, and then init them in clinit with the new method calls. - "$private$static $final$com.google.protobuf.Descriptors.Descriptor\n" - " internal_$identifier$_descriptor;\n"); + printer->Print( + vars, + // TODO(teboring): final needs to be added back. The way to fix it is to + // generate methods that can construct the types, and then still declare + // the types, and then init them in clinit with the new method calls. + "$private$static $final$com.google.protobuf.Descriptors.Descriptor\n" + " internal_$identifier$_descriptor;\n"); *bytecode_estimate += 30; // And the FieldAccessorTable. @@ -151,25 +148,25 @@ void ImmutableMessageGenerator::GenerateStaticVariables( int ImmutableMessageGenerator::GenerateStaticVariableInitializers( io::Printer* printer) { int bytecode_estimate = 0; - std::map vars; + std::map vars; vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); - vars["index"] = SimpleItoa(descriptor_->index()); + vars["index"] = StrCat(descriptor_->index()); vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_); if (descriptor_->containing_type() != NULL) { - vars["parent"] = UniqueFileScopeIdentifier( - descriptor_->containing_type()); + vars["parent"] = UniqueFileScopeIdentifier(descriptor_->containing_type()); } // The descriptor for this type. if (descriptor_->containing_type() == NULL) { printer->Print(vars, - "internal_$identifier$_descriptor =\n" - " getDescriptor().getMessageTypes().get($index$);\n"); + "internal_$identifier$_descriptor =\n" + " getDescriptor().getMessageTypes().get($index$);\n"); bytecode_estimate += 30; } else { - printer->Print(vars, - "internal_$identifier$_descriptor =\n" - " internal_$parent$_descriptor.getNestedTypes().get($index$);\n"); + printer->Print( + vars, + "internal_$identifier$_descriptor =\n" + " internal_$parent$_descriptor.getNestedTypes().get($index$);\n"); bytecode_estimate += 30; } @@ -186,9 +183,9 @@ int ImmutableMessageGenerator::GenerateStaticVariableInitializers( return bytecode_estimate; } -void ImmutableMessageGenerator:: -GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate) { - std::map vars; +void ImmutableMessageGenerator::GenerateFieldAccessorTable( + io::Printer* printer, int* bytecode_estimate) { + std::map vars; vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); if (MultipleJavaFiles(descriptor_->file(), /* immutable = */ true)) { // We can only make these package-private since the classes that use them @@ -203,41 +200,38 @@ GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate) { vars["final"] = ""; } vars["ver"] = GeneratedCodeVersionSuffix(); - printer->Print(vars, - "$private$static $final$\n" - " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" - " internal_$identifier$_fieldAccessorTable;\n"); + printer->Print( + vars, + "$private$static $final$\n" + " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" + " internal_$identifier$_fieldAccessorTable;\n"); // 6 bytes per field and oneof - *bytecode_estimate += 10 + 6 * descriptor_->field_count() - + 6 * descriptor_->oneof_decl_count(); + *bytecode_estimate += + 10 + 6 * descriptor_->field_count() + 6 * descriptor_->oneof_decl_count(); } -int ImmutableMessageGenerator:: -GenerateFieldAccessorTableInitializer(io::Printer* printer) { +int ImmutableMessageGenerator::GenerateFieldAccessorTableInitializer( + io::Printer* printer) { int bytecode_estimate = 10; printer->Print( - "internal_$identifier$_fieldAccessorTable = new\n" - " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable(\n" - " internal_$identifier$_descriptor,\n" - " new java.lang.String[] { ", - "identifier", UniqueFileScopeIdentifier(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "internal_$identifier$_fieldAccessorTable = new\n" + " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable(\n" + " internal_$identifier$_descriptor,\n" + " new java.lang.String[] { ", + "identifier", UniqueFileScopeIdentifier(descriptor_), "ver", + GeneratedCodeVersionSuffix()); for (int i = 0; i < descriptor_->field_count(); i++) { const FieldDescriptor* field = descriptor_->field(i); const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); bytecode_estimate += 6; - printer->Print( - "\"$field_name$\", ", - "field_name", info->capitalized_name); + printer->Print("\"$field_name$\", ", "field_name", info->capitalized_name); } for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { const OneofDescriptor* oneof = descriptor_->oneof_decl(i); const OneofGeneratorInfo* info = context_->GetOneofGeneratorInfo(oneof); bytecode_estimate += 6; - printer->Print( - "\"$oneof_name$\", ", - "oneof_name", info->capitalized_name); + printer->Print("\"$oneof_name$\", ", "oneof_name", info->capitalized_name); } printer->Print("});\n"); return bytecode_estimate; @@ -254,42 +248,40 @@ void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) { " $extra_interfaces$\n" " com.google.protobuf.GeneratedMessage$ver$.\n" " ExtendableMessageOrBuilder<$classname$> {\n", - "deprecation", descriptor_->options().deprecated() ? - "@java.lang.Deprecated " : "", + "deprecation", + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), - "classname", descriptor_->name(), - "{", "", "}", "", "ver", GeneratedCodeVersionSuffix()); + "classname", descriptor_->name(), "{", "", "}", "", "ver", + GeneratedCodeVersionSuffix()); } else { printer->Print( "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n" " $extra_interfaces$\n" " com.google.protobuf.MessageOrBuilder {\n", - "deprecation", descriptor_->options().deprecated() ? - "@java.lang.Deprecated " : "", + "deprecation", + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), - "classname", descriptor_->name(), - "{", "", "}", ""); + "classname", descriptor_->name(), "{", "", "}", ""); } printer->Annotate("{", "}", descriptor_); printer->Indent(); - for (int i = 0; i < descriptor_->field_count(); i++) { - printer->Print("\n"); - field_generators_.get(descriptor_->field(i)) - .GenerateInterfaceMembers(printer); - } - for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - printer->Print( - "\n" - "public $classname$.$oneof_capitalized_name$Case " - "get$oneof_capitalized_name$Case();\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name, - "classname", - context_->GetNameResolver()->GetImmutableClassName( - descriptor_)); - } + for (int i = 0; i < descriptor_->field_count(); i++) { + printer->Print("\n"); + field_generators_.get(descriptor_->field(i)) + .GenerateInterfaceMembers(printer); + } + for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { + printer->Print( + "\n" + "public $classname$.$oneof_capitalized_name$Case " + "get$oneof_capitalized_name$Case();\n", + "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name, + "classname", + context_->GetNameResolver()->GetImmutableClassName(descriptor_)); + } printer->Outdent(); printer->Print("}\n"); @@ -300,20 +292,20 @@ void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) { void ImmutableMessageGenerator::Generate(io::Printer* printer) { bool is_own_file = IsOwnFile(descriptor_, /* immutable = */ true); - std::map variables; + std::map variables; variables["static"] = is_own_file ? " " : " static "; variables["classname"] = descriptor_->name(); variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_); variables["ver"] = GeneratedCodeVersionSuffix(); - variables["deprecation"] = descriptor_->options().deprecated() - ? "@java.lang.Deprecated " : ""; + variables["deprecation"] = + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""; WriteMessageDocComment(printer, descriptor_); MaybePrintGeneratedAnnotation(context_, printer, descriptor_, /* immutable = */ true); // The builder_type stores the super type name of the nested Builder class. - string builder_type; + std::string builder_type; if (descriptor_->extension_range_count() > 0) { printer->Print( variables, @@ -335,15 +327,14 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { "$deprecation$public $static$final class $classname$ extends\n"); printer->Annotate("classname", descriptor_); printer->Print(variables, - " com.google.protobuf.GeneratedMessage$ver$ implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n"); - builder_type = strings::Substitute( - "com.google.protobuf.GeneratedMessage$0.Builder", - GeneratedCodeVersionSuffix()); + " com.google.protobuf.GeneratedMessage$ver$ implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n"); + builder_type = + strings::Substitute("com.google.protobuf.GeneratedMessage$0.Builder", + GeneratedCodeVersionSuffix()); } - printer->Print( - "private static final long serialVersionUID = 0L;\n"); + printer->Print("private static final long serialVersionUID = 0L;\n"); printer->Indent(); // Using builder_type, instead of Builder, prevents the Builder class from @@ -351,29 +342,34 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { // This optimizes the PermGen space usage for clients that do not modify // messages. printer->Print( - "// Use $classname$.newBuilder() to construct.\n" - "private $classname$($buildertype$ builder) {\n" - " super(builder);\n" - "}\n", - "classname", descriptor_->name(), - "buildertype", builder_type); - printer->Print( - "private $classname$() {\n", - "classname", descriptor_->name()); + "// Use $classname$.newBuilder() to construct.\n" + "private $classname$($buildertype$ builder) {\n" + " super(builder);\n" + "}\n", + "classname", descriptor_->name(), "buildertype", builder_type); + printer->Print("private $classname$() {\n", "classname", descriptor_->name()); printer->Indent(); GenerateInitializers(printer); printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); + printer->Print(variables, + "@java.lang.Override\n" + "@SuppressWarnings({\"unused\"})\n" + "protected java.lang.Object newInstance(\n" + " UnusedPrivateParameter unused) {\n" + " return new $classname$();\n" + "}\n" + "\n"); printer->Print( - "@java.lang.Override\n" - "public final com.google.protobuf.UnknownFieldSet\n" - "getUnknownFields() {\n" - " return this.unknownFields;\n" - "}\n"); + "@java.lang.Override\n" + "public final com.google.protobuf.UnknownFieldSet\n" + "getUnknownFields() {\n" + " return this.unknownFields;\n" + "}\n"); if (context_->HasGeneratedMethods(descriptor_)) { GenerateParsingConstructor(printer); @@ -383,50 +379,47 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { // Nested types for (int i = 0; i < descriptor_->enum_type_count(); i++) { - EnumGenerator(descriptor_->enum_type(i), true, context_) - .Generate(printer); + EnumGenerator(descriptor_->enum_type(i), true, context_).Generate(printer); } for (int i = 0; i < descriptor_->nested_type_count(); i++) { // Don't generate Java classes for map entry messages. if (IsMapEntry(descriptor_->nested_type(i))) continue; - ImmutableMessageGenerator messageGenerator( - descriptor_->nested_type(i), context_); + ImmutableMessageGenerator messageGenerator(descriptor_->nested_type(i), + context_); messageGenerator.GenerateInterface(printer); messageGenerator.Generate(printer); } - if (GenerateHasBits(descriptor_)) { - // Integers for bit fields. - int totalBits = 0; - for (int i = 0; i < descriptor_->field_count(); i++) { - totalBits += field_generators_.get(descriptor_->field(i)) - .GetNumBitsForMessage(); - } - int totalInts = (totalBits + 31) / 32; - for (int i = 0; i < totalInts; i++) { - printer->Print("private int $bit_field_name$;\n", - "bit_field_name", GetBitFieldName(i)); - } + // Integers for bit fields. + int totalBits = 0; + for (int i = 0; i < descriptor_->field_count(); i++) { + totalBits += + field_generators_.get(descriptor_->field(i)).GetNumBitsForMessage(); + } + int totalInts = (totalBits + 31) / 32; + for (int i = 0; i < totalInts; i++) { + printer->Print("private int $bit_field_name$;\n", "bit_field_name", + GetBitFieldName(i)); } // oneof - std::map vars; + std::map vars; for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - vars["oneof_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name; - vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name; - vars["oneof_index"] = - SimpleItoa(descriptor_->oneof_decl(i)->index()); + vars["oneof_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name; + vars["oneof_capitalized_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name; + vars["oneof_index"] = StrCat(descriptor_->oneof_decl(i)->index()); // oneofCase_ and oneof_ printer->Print(vars, - "private int $oneof_name$Case_ = 0;\n" - "private java.lang.Object $oneof_name$_;\n"); + "private int $oneof_name$Case_ = 0;\n" + "private java.lang.Object $oneof_name$_;\n"); // OneofCase enum printer->Print(vars, - "public enum $oneof_capitalized_name$Case\n" - " implements com.google.protobuf.Internal.EnumLite {\n"); + "public enum $oneof_capitalized_name$Case\n" + " implements com.google.protobuf.Internal.EnumLite {\n"); printer->Indent(); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); @@ -434,53 +427,54 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { "$deprecation$$field_name$($field_number$),\n", "deprecation", field->options().deprecated() ? "@java.lang.Deprecated " : "", "field_name", ToUpper(field->name()), "field_number", - SimpleItoa(field->number())); + StrCat(field->number())); } - printer->Print( - "$cap_oneof_name$_NOT_SET(0);\n", - "cap_oneof_name", - ToUpper(vars["oneof_name"])); + printer->Print("$cap_oneof_name$_NOT_SET(0);\n", "cap_oneof_name", + ToUpper(vars["oneof_name"])); printer->Print(vars, - "private final int value;\n" - "private $oneof_capitalized_name$Case(int value) {\n" - " this.value = value;\n" - "}\n"); - printer->Print(vars, - "/**\n" - " * @deprecated Use {@link #forNumber(int)} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public static $oneof_capitalized_name$Case valueOf(int value) {\n" - " return forNumber(value);\n" - "}\n" - "\n" - "public static $oneof_capitalized_name$Case forNumber(int value) {\n" - " switch (value) {\n"); + "private final int value;\n" + "private $oneof_capitalized_name$Case(int value) {\n" + " this.value = value;\n" + "}\n"); + printer->Print( + vars, + "/**\n" + " * @param value The number of the enum to look for.\n" + " * @return The enum associated with the given number.\n" + " * @deprecated Use {@link #forNumber(int)} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public static $oneof_capitalized_name$Case valueOf(int value) {\n" + " return forNumber(value);\n" + "}\n" + "\n" + "public static $oneof_capitalized_name$Case forNumber(int value) {\n" + " switch (value) {\n"); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); printer->Print(" case $field_number$: return $field_name$;\n", - "field_number", SimpleItoa(field->number()), + "field_number", StrCat(field->number()), "field_name", ToUpper(field->name())); } printer->Print( - " case 0: return $cap_oneof_name$_NOT_SET;\n" - " default: return null;\n" - " }\n" - "}\n" - "public int getNumber() {\n" - " return this.value;\n" - "}\n", - "cap_oneof_name", ToUpper(vars["oneof_name"])); + " case 0: return $cap_oneof_name$_NOT_SET;\n" + " default: return null;\n" + " }\n" + "}\n" + "public int getNumber() {\n" + " return this.value;\n" + "}\n", + "cap_oneof_name", ToUpper(vars["oneof_name"])); printer->Outdent(); printer->Print("};\n\n"); // oneofCase() printer->Print(vars, - "public $oneof_capitalized_name$Case\n" - "get$oneof_capitalized_name$Case() {\n" - " return $oneof_capitalized_name$Case.forNumber(\n" - " $oneof_name$Case_);\n" - "}\n" - "\n"); + "public $oneof_capitalized_name$Case\n" + "get$oneof_capitalized_name$Case() {\n" + " return $oneof_capitalized_name$Case.forNumber(\n" + " $oneof_name$Case_);\n" + "}\n" + "\n"); } if (IsAnyMessage(descriptor_)) { @@ -491,8 +485,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->field_count(); i++) { printer->Print("public static final int $constant_name$ = $number$;\n", "constant_name", FieldConstantName(descriptor_->field(i)), - "number", - SimpleItoa(descriptor_->field(i)->number())); + "number", StrCat(descriptor_->field(i)->number())); field_generators_.get(descriptor_->field(i)).GenerateMembers(printer); printer->Print("\n"); } @@ -508,22 +501,21 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { GenerateBuilder(printer); printer->Print( - "\n" - "// @@protoc_insertion_point(class_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); - + "\n" + "// @@protoc_insertion_point(class_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); // Carefully initialize the default instance in such a way that it doesn't // conflict with other initialization. + printer->Print("private static final $classname$ DEFAULT_INSTANCE;\n", + "classname", + name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "private static final $classname$ DEFAULT_INSTANCE;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - printer->Print( - "static {\n" - " DEFAULT_INSTANCE = new $classname$();\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "static {\n" + " DEFAULT_INSTANCE = new $classname$();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( "public static $classname$ getDefaultInstance() {\n" @@ -546,12 +538,12 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { GenerateParser(printer); printer->Print( - "@java.lang.Override\n" - "public $classname$ getDefaultInstanceForType() {\n" - " return DEFAULT_INSTANCE;\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public $classname$ getDefaultInstanceForType() {\n" + " return DEFAULT_INSTANCE;\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); // Extensions must be declared after the DEFAULT_INSTANCE is initialized // because the DEFAULT_INSTANCE is used by the extension to lazily retrieve @@ -565,12 +557,11 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { printer->Print("}\n\n"); } - // =================================================================== -void ImmutableMessageGenerator:: -GenerateMessageSerializationMethods(io::Printer* printer) { - std::unique_ptr sorted_fields( +void ImmutableMessageGenerator::GenerateMessageSerializationMethods( + io::Printer* printer) { + std::unique_ptr sorted_fields( SortFieldsByNumber(descriptor_)); std::vector sorted_extensions; @@ -580,9 +571,9 @@ GenerateMessageSerializationMethods(io::Printer* printer) { std::sort(sorted_extensions.begin(), sorted_extensions.end(), ExtensionRangeOrdering()); printer->Print( - "@java.lang.Override\n" - "public void writeTo(com.google.protobuf.CodedOutputStream output)\n" - " throws java.io.IOException {\n"); + "@java.lang.Override\n" + "public void writeTo(com.google.protobuf.CodedOutputStream output)\n" + " throws java.io.IOException {\n"); printer->Indent(); if (HasPackedFields(descriptor_)) { @@ -647,8 +638,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) { printer->Print("size = 0;\n"); for (int i = 0; i < descriptor_->field_count(); i++) { - field_generators_.get(sorted_fields[i]) - .GenerateSerializedSizeCode(printer); + field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer); } if (descriptor_->extension_range_count() > 0) { @@ -660,8 +650,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) { } if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "size += unknownFields.getSerializedSizeAsMessageSet();\n"); + printer->Print("size += unknownFields.getSerializedSizeAsMessageSet();\n"); } else { printer->Print("size += unknownFields.getSerializedSize();\n"); } @@ -672,88 +661,89 @@ GenerateMessageSerializationMethods(io::Printer* printer) { printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); } -void ImmutableMessageGenerator:: -GenerateParseFromMethods(io::Printer* printer) { +void ImmutableMessageGenerator::GenerateParseFromMethods(io::Printer* printer) { // Note: These are separate from GenerateMessageSerializationMethods() // because they need to be generated even for messages that are optimized // for code size. printer->Print( - "public static $classname$ parseFrom(\n" - " java.nio.ByteBuffer data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " java.nio.ByteBuffer data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.ByteString data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.ByteString data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(byte[] data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " byte[] data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(java.io.InputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseWithIOException(PARSER, input);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " java.io.InputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseWithIOException(PARSER, input, extensionRegistry);\n" - "}\n" - "public static $classname$ parseDelimitedFrom(java.io.InputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseDelimitedWithIOException(PARSER, input);\n" - "}\n" - "public static $classname$ parseDelimitedFrom(\n" - " java.io.InputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseDelimitedWithIOException(PARSER, input, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.CodedInputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseWithIOException(PARSER, input);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.CodedInputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseWithIOException(PARSER, input, extensionRegistry);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "public static $classname$ parseFrom(\n" + " java.nio.ByteBuffer data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " java.nio.ByteBuffer data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.ByteString data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.ByteString data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(byte[] data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " byte[] data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(java.io.InputStream input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseWithIOException(PARSER, input);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " java.io.InputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseWithIOException(PARSER, input, extensionRegistry);\n" + "}\n" + "public static $classname$ parseDelimitedFrom(java.io.InputStream " + "input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseDelimitedWithIOException(PARSER, input);\n" + "}\n" + "public static $classname$ parseDelimitedFrom(\n" + " java.io.InputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseDelimitedWithIOException(PARSER, input, " + "extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.CodedInputStream input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseWithIOException(PARSER, input);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.CodedInputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseWithIOException(PARSER, input, extensionRegistry);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } void ImmutableMessageGenerator::GenerateSerializeOneField( @@ -764,7 +754,7 @@ void ImmutableMessageGenerator::GenerateSerializeOneField( void ImmutableMessageGenerator::GenerateSerializeOneExtensionRange( io::Printer* printer, const Descriptor::ExtensionRange* range) { printer->Print("extensionWriter.writeUntil($end$, output);\n", "end", - SimpleItoa(range->end)); + StrCat(range->end)); } // =================================================================== @@ -772,48 +762,48 @@ void ImmutableMessageGenerator::GenerateSerializeOneExtensionRange( void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) { // LITE_RUNTIME implements this at the GeneratedMessageLite level. printer->Print( - "@java.lang.Override\n" - "public Builder newBuilderForType() { return newBuilder(); }\n"); + "@java.lang.Override\n" + "public Builder newBuilderForType() { return newBuilder(); }\n"); printer->Print( - "public static Builder newBuilder() {\n" - " return DEFAULT_INSTANCE.toBuilder();\n" - "}\n" - "public static Builder newBuilder($classname$ prototype) {\n" - " return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n" - "}\n" - "@java.lang.Override\n" - "public Builder toBuilder() {\n" - " return this == DEFAULT_INSTANCE\n" - " ? new Builder() : new Builder().mergeFrom(this);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public static Builder newBuilder() {\n" + " return DEFAULT_INSTANCE.toBuilder();\n" + "}\n" + "public static Builder newBuilder($classname$ prototype) {\n" + " return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n" + "}\n" + "@java.lang.Override\n" + "public Builder toBuilder() {\n" + " return this == DEFAULT_INSTANCE\n" + " ? new Builder() : new Builder().mergeFrom(this);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "@java.lang.Override\n" - "protected Builder newBuilderForType(\n" - " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n" - " Builder builder = new Builder(parent);\n" - " return builder;\n" - "}\n", - "ver", GeneratedCodeVersionSuffix()); + "@java.lang.Override\n" + "protected Builder newBuilderForType(\n" + " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n" + " Builder builder = new Builder(parent);\n" + " return builder;\n" + "}\n", + "ver", GeneratedCodeVersionSuffix()); MessageBuilderGenerator builderGenerator(descriptor_, context_); builderGenerator.Generate(printer); } -void ImmutableMessageGenerator:: -GenerateDescriptorMethods(io::Printer* printer) { +void ImmutableMessageGenerator::GenerateDescriptorMethods( + io::Printer* printer) { if (!descriptor_->options().no_standard_descriptor_accessor()) { printer->Print( - "public static final com.google.protobuf.Descriptors.Descriptor\n" - " getDescriptor() {\n" - " return $fileclass$.internal_$identifier$_descriptor;\n" - "}\n" - "\n", - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_)); + "public static final com.google.protobuf.Descriptors.Descriptor\n" + " getDescriptor() {\n" + " return $fileclass$.internal_$identifier$_descriptor;\n" + "}\n" + "\n", + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_)); } std::vector map_fields; for (int i = 0; i < descriptor_->field_count(); i++) { @@ -825,11 +815,11 @@ GenerateDescriptorMethods(io::Printer* printer) { } if (!map_fields.empty()) { printer->Print( - "@SuppressWarnings({\"rawtypes\"})\n" - "@java.lang.Override\n" - "protected com.google.protobuf.MapField internalGetMapField(\n" - " int number) {\n" - " switch (number) {\n"); + "@SuppressWarnings({\"rawtypes\"})\n" + "@java.lang.Override\n" + "protected com.google.protobuf.MapField internalGetMapField(\n" + " int number) {\n" + " switch (number) {\n"); printer->Indent(); printer->Indent(); for (int i = 0; i < map_fields.size(); ++i) { @@ -838,7 +828,7 @@ GenerateDescriptorMethods(io::Printer* printer) { printer->Print( "case $number$:\n" " return internalGet$capitalized_name$();\n", - "number", SimpleItoa(field->number()), "capitalized_name", + "number", StrCat(field->number()), "capitalized_name", info->capitalized_name); } printer->Print( @@ -852,40 +842,38 @@ GenerateDescriptorMethods(io::Printer* printer) { "}\n"); } printer->Print( - "@java.lang.Override\n" - "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" - " internalGetFieldAccessorTable() {\n" - " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" - " .ensureFieldAccessorsInitialized(\n" - " $classname$.class, $classname$.Builder.class);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "@java.lang.Override\n" + "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" + " internalGetFieldAccessorTable() {\n" + " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" + " .ensureFieldAccessorsInitialized(\n" + " $classname$.class, $classname$.Builder.class);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } // =================================================================== -void ImmutableMessageGenerator::GenerateIsInitialized( - io::Printer* printer) { +void ImmutableMessageGenerator::GenerateIsInitialized(io::Printer* printer) { // Memoizes whether the protocol buffer is fully initialized (has all // required fields). -1 means not yet computed. 0 means false and 1 means // true. + printer->Print("private byte memoizedIsInitialized = -1;\n"); printer->Print( - "private byte memoizedIsInitialized = -1;\n"); - printer->Print( - "@java.lang.Override\n" - "public final boolean isInitialized() {\n"); + "@java.lang.Override\n" + "public final boolean isInitialized() {\n"); printer->Indent(); // Don't directly compare to -1 to avoid an Android x86 JIT bug. printer->Print( - "byte isInitialized = memoizedIsInitialized;\n" - "if (isInitialized == 1) return true;\n" - "if (isInitialized == 0) return false;\n" - "\n"); + "byte isInitialized = memoizedIsInitialized;\n" + "if (isInitialized == 1) return true;\n" + "if (isInitialized == 0) return false;\n" + "\n"); // Check that all required fields in this message are set. // TODO(kenton): We can optimize this when we switch to putting all the @@ -896,11 +884,11 @@ void ImmutableMessageGenerator::GenerateIsInitialized( if (field->is_required()) { printer->Print( - "if (!has$name$()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - "}\n", - "name", info->capitalized_name); + "if (!has$name$()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + "}\n", + "name", info->capitalized_name); } } @@ -913,13 +901,13 @@ void ImmutableMessageGenerator::GenerateIsInitialized( switch (field->label()) { case FieldDescriptor::LABEL_REQUIRED: printer->Print( - "if (!get$name$().isInitialized()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); + "if (!get$name$().isInitialized()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + "}\n", + "type", + name_resolver_->GetImmutableClassName(field->message_type()), + "name", info->capitalized_name); break; case FieldDescriptor::LABEL_OPTIONAL: if (!SupportFieldPresence(descriptor_->file()) && @@ -929,43 +917,43 @@ void ImmutableMessageGenerator::GenerateIsInitialized( context_->GetOneofGeneratorInfo(oneof); printer->Print("if ($oneof_name$Case_ == $field_number$) {\n", "oneof_name", oneof_info->name, "field_number", - SimpleItoa(field->number())); + StrCat(field->number())); } else { - printer->Print( - "if (has$name$()) {\n", - "name", info->capitalized_name); + printer->Print("if (has$name$()) {\n", "name", + info->capitalized_name); } printer->Print( - " if (!get$name$().isInitialized()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - " }\n" - "}\n", - "name", info->capitalized_name); - break; - case FieldDescriptor::LABEL_REPEATED: - if (IsMapEntry(field->message_type())) { - printer->Print( - "for ($type$ item : get$name$Map().values()) {\n" - " if (!item.isInitialized()) {\n" + " if (!get$name$().isInitialized()) {\n" " memoizedIsInitialized = 0;\n" " return false;\n" " }\n" "}\n", - "type", MapValueImmutableClassdName(field->message_type(), - name_resolver_), "name", info->capitalized_name); + break; + case FieldDescriptor::LABEL_REPEATED: + if (IsMapEntry(field->message_type())) { + printer->Print( + "for ($type$ item : get$name$Map().values()) {\n" + " if (!item.isInitialized()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + " }\n" + "}\n", + "type", + MapValueImmutableClassdName(field->message_type(), + name_resolver_), + "name", info->capitalized_name); } else { printer->Print( - "for (int i = 0; i < get$name$Count(); i++) {\n" - " if (!get$name$(i).isInitialized()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - " }\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); + "for (int i = 0; i < get$name$Count(); i++) {\n" + " if (!get$name$(i).isInitialized()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + " }\n" + "}\n", + "type", + name_resolver_->GetImmutableClassName(field->message_type()), + "name", info->capitalized_name); } break; } @@ -974,21 +962,20 @@ void ImmutableMessageGenerator::GenerateIsInitialized( if (descriptor_->extension_range_count() > 0) { printer->Print( - "if (!extensionsAreInitialized()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - "}\n"); + "if (!extensionsAreInitialized()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + "}\n"); } printer->Outdent(); - printer->Print( - " memoizedIsInitialized = 1;\n"); + printer->Print(" memoizedIsInitialized = 1;\n"); printer->Print( - " return true;\n" - "}\n" - "\n"); + " return true;\n" + "}\n" + "\n"); } // =================================================================== @@ -1002,28 +989,27 @@ bool CheckHasBitsForEqualsAndHashCode(const FieldDescriptor* field) { return true; } return GetJavaType(field) == JAVATYPE_MESSAGE && - field->containing_oneof() == NULL; + field->containing_oneof() == NULL; } } // namespace -void ImmutableMessageGenerator:: -GenerateEqualsAndHashCode(io::Printer* printer) { +void ImmutableMessageGenerator::GenerateEqualsAndHashCode( + io::Printer* printer) { printer->Print( - "@java.lang.Override\n" - "public boolean equals(final java.lang.Object obj) {\n"); + "@java.lang.Override\n" + "public boolean equals(final java.lang.Object obj) {\n"); printer->Indent(); printer->Print( - "if (obj == this) {\n" - " return true;\n" - "}\n" - "if (!(obj instanceof $classname$)) {\n" - " return super.equals(obj);\n" - "}\n" - "$classname$ other = ($classname$) obj;\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - - printer->Print("boolean result = true;\n"); + "if (obj == this) {\n" + " return true;\n" + "}\n" + "if (!(obj instanceof $classname$)) {\n" + " return super.equals(obj);\n" + "}\n" + "$classname$ other = ($classname$) obj;\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); + for (int i = 0; i < descriptor_->field_count(); i++) { const FieldDescriptor* field = descriptor_->field(i); if (field->containing_oneof() == NULL) { @@ -1031,16 +1017,15 @@ GenerateEqualsAndHashCode(io::Printer* printer) { bool check_has_bits = CheckHasBitsForEqualsAndHashCode(field); if (check_has_bits) { printer->Print( - "result = result && (has$name$() == other.has$name$());\n" - "if (has$name$()) {\n", - "name", info->capitalized_name); + "if (has$name$() != other.has$name$()) return false;\n" + "if (has$name$()) {\n", + "name", info->capitalized_name); printer->Indent(); } field_generators_.get(field).GenerateEqualsCode(printer); if (check_has_bits) { printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } } } @@ -1048,30 +1033,27 @@ GenerateEqualsAndHashCode(io::Printer* printer) { // Compare oneofs. for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { printer->Print( - "result = result && get$oneof_capitalized_name$Case().equals(\n" - " other.get$oneof_capitalized_name$Case());\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name); + "if (!get$oneof_capitalized_name$Case().equals(" + "other.get$oneof_capitalized_name$Case())) return false;\n", + "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name); printer->Print( - "if (!result) return false;\n" - "switch ($oneof_name$Case_) {\n", - "oneof_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); + "switch ($oneof_name$Case_) {\n", "oneof_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name); printer->Indent(); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); printer->Print("case $field_number$:\n", "field_number", - SimpleItoa(field->number())); + StrCat(field->number())); printer->Indent(); field_generators_.get(field).GenerateEqualsCode(printer); printer->Print("break;\n"); printer->Outdent(); } printer->Print( - "case 0:\n" - "default:\n"); + "case 0:\n" + "default:\n"); printer->Outdent(); printer->Print("}\n"); } @@ -1080,32 +1062,29 @@ GenerateEqualsAndHashCode(io::Printer* printer) { // false for non-canonical ordering when running in LITE_RUNTIME but it's // the best we can do. printer->Print( - "result = result && unknownFields.equals(other.unknownFields);\n"); + "if (!unknownFields.equals(other.unknownFields)) return false;\n"); if (descriptor_->extension_range_count() > 0) { printer->Print( - "result = result &&\n" - " getExtensionFields().equals(other.getExtensionFields());\n"); + "if (!getExtensionFields().equals(other.getExtensionFields()))\n" + " return false;\n"); } - printer->Print( - "return result;\n"); + printer->Print("return true;\n"); printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); printer->Print( - "@java.lang.Override\n" - "public int hashCode() {\n"); + "@java.lang.Override\n" + "public int hashCode() {\n"); printer->Indent(); - printer->Print( - "if (memoizedHashCode != 0) {\n"); + printer->Print("if (memoizedHashCode != 0) {\n"); printer->Indent(); - printer->Print( - "return memoizedHashCode;\n"); + printer->Print("return memoizedHashCode;\n"); printer->Outdent(); printer->Print( - "}\n" - "int hash = 41;\n"); + "}\n" + "int hash = 41;\n"); // If we output a getDescriptor() method, use that as it is more efficient. if (descriptor_->options().no_standard_descriptor_accessor()) { @@ -1121,9 +1100,7 @@ GenerateEqualsAndHashCode(io::Printer* printer) { const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); bool check_has_bits = CheckHasBitsForEqualsAndHashCode(field); if (check_has_bits) { - printer->Print( - "if (has$name$()) {\n", - "name", info->capitalized_name); + printer->Print("if (has$name$()) {\n", "name", info->capitalized_name); printer->Indent(); } field_generators_.get(field).GenerateHashCode(printer); @@ -1137,62 +1114,58 @@ GenerateEqualsAndHashCode(io::Printer* printer) { // hashCode oneofs. for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { printer->Print( - "switch ($oneof_name$Case_) {\n", - "oneof_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); + "switch ($oneof_name$Case_) {\n", "oneof_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name); printer->Indent(); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); printer->Print("case $field_number$:\n", "field_number", - SimpleItoa(field->number())); + StrCat(field->number())); printer->Indent(); field_generators_.get(field).GenerateHashCode(printer); printer->Print("break;\n"); printer->Outdent(); } printer->Print( - "case 0:\n" - "default:\n"); + "case 0:\n" + "default:\n"); printer->Outdent(); printer->Print("}\n"); } if (descriptor_->extension_range_count() > 0) { - printer->Print( - "hash = hashFields(hash, getExtensionFields());\n"); + printer->Print("hash = hashFields(hash, getExtensionFields());\n"); } + printer->Print("hash = (29 * hash) + unknownFields.hashCode();\n"); printer->Print( - "hash = (29 * hash) + unknownFields.hashCode();\n"); - printer->Print( - "memoizedHashCode = hash;\n" - "return hash;\n"); + "memoizedHashCode = hash;\n" + "return hash;\n"); printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); } // =================================================================== -void ImmutableMessageGenerator:: -GenerateExtensionRegistrationCode(io::Printer* printer) { +void ImmutableMessageGenerator::GenerateExtensionRegistrationCode( + io::Printer* printer) { for (int i = 0; i < descriptor_->extension_count(); i++) { ImmutableExtensionGenerator(descriptor_->extension(i), context_) - .GenerateRegistrationCode(printer); + .GenerateRegistrationCode(printer); } for (int i = 0; i < descriptor_->nested_type_count(); i++) { ImmutableMessageGenerator(descriptor_->nested_type(i), context_) - .GenerateExtensionRegistrationCode(printer); + .GenerateExtensionRegistrationCode(printer); } } // =================================================================== -void ImmutableMessageGenerator:: -GenerateParsingConstructor(io::Printer* printer) { - std::unique_ptr sorted_fields( +void ImmutableMessageGenerator::GenerateParsingConstructor( + io::Printer* printer) { + std::unique_ptr sorted_fields( SortFieldsByNumber(descriptor_)); printer->Print( @@ -1219,26 +1192,25 @@ GenerateParsingConstructor(io::Printer* printer) { } int totalBuilderInts = (totalBuilderBits + 31) / 32; for (int i = 0; i < totalBuilderInts; i++) { - printer->Print("int mutable_$bit_field_name$ = 0;\n", - "bit_field_name", GetBitFieldName(i)); + printer->Print("int mutable_$bit_field_name$ = 0;\n", "bit_field_name", + GetBitFieldName(i)); } printer->Print( "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n" " com.google.protobuf.UnknownFieldSet.newBuilder();\n"); - printer->Print( - "try {\n"); + printer->Print("try {\n"); printer->Indent(); printer->Print( - "boolean done = false;\n" - "while (!done) {\n"); + "boolean done = false;\n" + "while (!done) {\n"); printer->Indent(); printer->Print( - "int tag = input.readTag();\n" - "switch (tag) {\n"); + "int tag = input.readTag();\n" + "switch (tag) {\n"); printer->Indent(); printer->Print( @@ -1248,35 +1220,35 @@ GenerateParsingConstructor(io::Printer* printer) { for (int i = 0; i < descriptor_->field_count(); i++) { const FieldDescriptor* field = sorted_fields[i]; - uint32 tag = WireFormatLite::MakeTag(field->number(), - WireFormat::WireTypeForFieldType(field->type())); + uint32 tag = WireFormatLite::MakeTag( + field->number(), WireFormat::WireTypeForFieldType(field->type())); printer->Print("case $tag$: {\n", "tag", - SimpleItoa(static_cast(tag))); + StrCat(static_cast(tag))); printer->Indent(); field_generators_.get(field).GenerateParsingCode(printer); printer->Outdent(); printer->Print( - " break;\n" - "}\n"); + " break;\n" + "}\n"); if (field->is_packable()) { // To make packed = true wire compatible, we generate parsing code from a // packed version of this field regardless of field->options().packed(). - uint32 packed_tag = WireFormatLite::MakeTag(field->number(), - WireFormatLite::WIRETYPE_LENGTH_DELIMITED); + uint32 packed_tag = WireFormatLite::MakeTag( + field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED); printer->Print("case $tag$: {\n", "tag", - SimpleItoa(static_cast(packed_tag))); + StrCat(static_cast(packed_tag))); printer->Indent(); field_generators_.get(field).GenerateParsingCodeFromPacked(printer); printer->Outdent(); printer->Print( - " break;\n" - "}\n"); + " break;\n" + "}\n"); } } @@ -1292,8 +1264,8 @@ GenerateParsingConstructor(io::Printer* printer) { printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" // switch (tag) - "}\n"); // while (!done) + " }\n" // switch (tag) + "}\n"); // while (!done) printer->Outdent(); printer->Print( @@ -1315,13 +1287,12 @@ GenerateParsingConstructor(io::Printer* printer) { printer->Print("this.unknownFields = unknownFields.build();\n"); // Make extensions immutable. - printer->Print( - "makeExtensionsImmutable();\n"); + printer->Print("makeExtensionsImmutable();\n"); printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" // finally + " }\n" // finally "}\n"); } @@ -1364,8 +1335,7 @@ void ImmutableMessageGenerator::GenerateParser(io::Printer* printer) { "return builder.buildPartial();\n"); printer->Outdent(); } - printer->Print( - "}\n"); + printer->Print("}\n"); printer->Outdent(); printer->Print( "};\n" @@ -1397,72 +1367,76 @@ void ImmutableMessageGenerator::GenerateInitializers(io::Printer* printer) { void ImmutableMessageGenerator::GenerateAnyMethods(io::Printer* printer) { printer->Print( - "private static String getTypeUrl(\n" - " java.lang.String typeUrlPrefix,\n" - " com.google.protobuf.Descriptors.Descriptor descriptor) {\n" - " return typeUrlPrefix.endsWith(\"/\")\n" - " ? typeUrlPrefix + descriptor.getFullName()\n" - " : typeUrlPrefix + \"/\" + descriptor.getFullName();\n" - "}\n" - "\n" - "private static String getTypeNameFromTypeUrl(\n" - " java.lang.String typeUrl) {\n" - " int pos = typeUrl.lastIndexOf('/');\n" - " return pos == -1 ? \"\" : typeUrl.substring(pos + 1);\n" - "}\n" - "\n" - "public static Any pack(\n" - " T message) {\n" - " return Any.newBuilder()\n" - " .setTypeUrl(getTypeUrl(\"type.googleapis.com\",\n" - " message.getDescriptorForType()))\n" - " .setValue(message.toByteString())\n" - " .build();\n" - "}\n" - "\n" - "/**\n" - " * Packs a message using the given type URL prefix. The type URL will\n" - " * be constructed by concatenating the message type's full name to the\n" - " * prefix with an optional \"/\" separator if the prefix doesn't end\n" - " * with \"/\" already.\n" - " */\n" - "public static Any pack(\n" - " T message, java.lang.String typeUrlPrefix) {\n" - " return Any.newBuilder()\n" - " .setTypeUrl(getTypeUrl(typeUrlPrefix,\n" - " message.getDescriptorForType()))\n" - " .setValue(message.toByteString())\n" - " .build();\n" - "}\n" - "\n" - "public boolean is(\n" - " java.lang.Class clazz) {\n" - " T defaultInstance =\n" - " com.google.protobuf.Internal.getDefaultInstance(clazz);\n" - " return getTypeNameFromTypeUrl(getTypeUrl()).equals(\n" - " defaultInstance.getDescriptorForType().getFullName());\n" - "}\n" - "\n" - "private volatile com.google.protobuf.Message cachedUnpackValue;\n" - "\n" - "@java.lang.SuppressWarnings(\"unchecked\")\n" - "public T unpack(\n" - " java.lang.Class clazz)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " if (!is(clazz)) {\n" - " throw new com.google.protobuf.InvalidProtocolBufferException(\n" - " \"Type of the Any message does not match the given class.\");\n" - " }\n" - " if (cachedUnpackValue != null) {\n" - " return (T) cachedUnpackValue;\n" - " }\n" - " T defaultInstance =\n" - " com.google.protobuf.Internal.getDefaultInstance(clazz);\n" - " T result = (T) defaultInstance.getParserForType()\n" - " .parseFrom(getValue());\n" - " cachedUnpackValue = result;\n" - " return result;\n" - "}\n"); + "private static String getTypeUrl(\n" + " java.lang.String typeUrlPrefix,\n" + " com.google.protobuf.Descriptors.Descriptor descriptor) {\n" + " return typeUrlPrefix.endsWith(\"/\")\n" + " ? typeUrlPrefix + descriptor.getFullName()\n" + " : typeUrlPrefix + \"/\" + descriptor.getFullName();\n" + "}\n" + "\n" + "private static String getTypeNameFromTypeUrl(\n" + " java.lang.String typeUrl) {\n" + " int pos = typeUrl.lastIndexOf('/');\n" + " return pos == -1 ? \"\" : typeUrl.substring(pos + 1);\n" + "}\n" + "\n" + "public static Any pack(\n" + " T message) {\n" + " return Any.newBuilder()\n" + " .setTypeUrl(getTypeUrl(\"type.googleapis.com\",\n" + " message.getDescriptorForType()))\n" + " .setValue(message.toByteString())\n" + " .build();\n" + "}\n" + "\n" + "/**\n" + " * Packs a message using the given type URL prefix. The type URL will\n" + " * be constructed by concatenating the message type's full name to the\n" + " * prefix with an optional \"/\" separator if the prefix doesn't end\n" + " * with \"/\" already.\n" + " */\n" + "public static Any pack(\n" + " T message, java.lang.String typeUrlPrefix) {\n" + " return Any.newBuilder()\n" + " .setTypeUrl(getTypeUrl(typeUrlPrefix,\n" + " message.getDescriptorForType()))\n" + " .setValue(message.toByteString())\n" + " .build();\n" + "}\n" + "\n" + "public boolean is(\n" + " java.lang.Class clazz) {\n" + " T defaultInstance =\n" + " com.google.protobuf.Internal.getDefaultInstance(clazz);\n" + " return getTypeNameFromTypeUrl(getTypeUrl()).equals(\n" + " defaultInstance.getDescriptorForType().getFullName());\n" + "}\n" + "\n" + "private volatile com.google.protobuf.Message cachedUnpackValue;\n" + "\n" + "@java.lang.SuppressWarnings(\"unchecked\")\n" + "public T unpack(\n" + " java.lang.Class clazz)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " boolean invalidClazz = false;\n" + " if (cachedUnpackValue != null) {\n" + " if (cachedUnpackValue.getClass() == clazz) {\n" + " return (T) cachedUnpackValue;\n" + " }\n" + " invalidClazz = true;\n" + " }\n" + " if (invalidClazz || !is(clazz)) {\n" + " throw new com.google.protobuf.InvalidProtocolBufferException(\n" + " \"Type of the Any message does not match the given class.\");\n" + " }\n" + " T defaultInstance =\n" + " com.google.protobuf.Internal.getDefaultInstance(clazz);\n" + " T result = (T) defaultInstance.getParserForType()\n" + " .parseFrom(getValue());\n" + " cachedUnpackValue = result;\n" + " return result;\n" + "}\n"); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_message.h b/src/google/protobuf/compiler/java/java_message.h index ac0c765994..de2f359a41 100644 --- a/src/google/protobuf/compiler/java/java_message.h +++ b/src/google/protobuf/compiler/java/java_message.h @@ -35,8 +35,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__ -#include #include +#include #include namespace google { @@ -101,14 +101,13 @@ class ImmutableMessageGenerator : public MessageGenerator { virtual void Generate(io::Printer* printer); virtual void GenerateInterface(io::Printer* printer); virtual void GenerateExtensionRegistrationCode(io::Printer* printer); - virtual void GenerateStaticVariables( - io::Printer* printer, int* bytecode_estimate); + virtual void GenerateStaticVariables(io::Printer* printer, + int* bytecode_estimate); // Returns an estimate of the number of bytes the printed code will compile to virtual int GenerateStaticVariableInitializers(io::Printer* printer); private: - void GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate); // Returns an estimate of the number of bytes the printed code will compile to diff --git a/src/google/protobuf/compiler/java/java_message_builder.cc b/src/google/protobuf/compiler/java/java_message_builder.cc index 0b9db22254..aa00a66f4c 100644 --- a/src/google/protobuf/compiler/java/java_message_builder.cc +++ b/src/google/protobuf/compiler/java/java_message_builder.cc @@ -54,30 +54,27 @@ #include + namespace google { namespace protobuf { namespace compiler { namespace java { namespace { -bool GenerateHasBits(const Descriptor* descriptor) { - return SupportFieldPresence(descriptor->file()) || - HasRepeatedFields(descriptor); -} - -string MapValueImmutableClassdName(const Descriptor* descriptor, - ClassNameResolver* name_resolver) { +std::string MapValueImmutableClassdName(const Descriptor* descriptor, + ClassNameResolver* name_resolver) { const FieldDescriptor* value_field = descriptor->FindFieldByName("value"); GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, value_field->type()); return name_resolver->GetImmutableClassName(value_field->message_type()); } } // namespace -MessageBuilderGenerator::MessageBuilderGenerator( - const Descriptor* descriptor, Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { +MessageBuilderGenerator::MessageBuilderGenerator(const Descriptor* descriptor, + Context* context) + : descriptor_(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()), + field_generators_(descriptor, context_) { GOOGLE_CHECK(HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A non-lite message generator is used to " "generate lite messages."; @@ -85,28 +82,28 @@ MessageBuilderGenerator::MessageBuilderGenerator( MessageBuilderGenerator::~MessageBuilderGenerator() {} -void MessageBuilderGenerator:: -Generate(io::Printer* printer) { +void MessageBuilderGenerator::Generate(io::Printer* printer) { WriteMessageDocComment(printer, descriptor_); if (descriptor_->extension_range_count() > 0) { printer->Print( - "public static final class Builder extends\n" - " com.google.protobuf.GeneratedMessage$ver$.ExtendableBuilder<\n" - " $classname$, Builder> implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "extra_interfaces", ExtraBuilderInterfaces(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "public static final class Builder extends\n" + " com.google.protobuf.GeneratedMessage$ver$.ExtendableBuilder<\n" + " $classname$, Builder> implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } else { printer->Print( - "public static final class Builder extends\n" - " com.google.protobuf.GeneratedMessage$ver$.Builder implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "extra_interfaces", ExtraBuilderInterfaces(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "public static final class Builder extends\n" + " com.google.protobuf.GeneratedMessage$ver$.Builder " + "implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } printer->Indent(); @@ -119,79 +116,77 @@ Generate(io::Printer* printer) { } // oneof - std::map vars; + std::map vars; for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - vars["oneof_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name; - vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name; - vars["oneof_index"] = - SimpleItoa(descriptor_->oneof_decl(i)->index()); + vars["oneof_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name; + vars["oneof_capitalized_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name; + vars["oneof_index"] = StrCat(descriptor_->oneof_decl(i)->index()); // oneofCase_ and oneof_ printer->Print(vars, - "private int $oneof_name$Case_ = 0;\n" - "private java.lang.Object $oneof_name$_;\n"); + "private int $oneof_name$Case_ = 0;\n" + "private java.lang.Object $oneof_name$_;\n"); // oneofCase() and clearOneof() printer->Print(vars, - "public $oneof_capitalized_name$Case\n" - " get$oneof_capitalized_name$Case() {\n" - " return $oneof_capitalized_name$Case.forNumber(\n" - " $oneof_name$Case_);\n" - "}\n" - "\n" - "public Builder clear$oneof_capitalized_name$() {\n" - " $oneof_name$Case_ = 0;\n" - " $oneof_name$_ = null;\n"); + "public $oneof_capitalized_name$Case\n" + " get$oneof_capitalized_name$Case() {\n" + " return $oneof_capitalized_name$Case.forNumber(\n" + " $oneof_name$Case_);\n" + "}\n" + "\n" + "public Builder clear$oneof_capitalized_name$() {\n" + " $oneof_name$Case_ = 0;\n" + " $oneof_name$_ = null;\n"); printer->Print(" onChanged();\n"); printer->Print( - " return this;\n" - "}\n" - "\n"); + " return this;\n" + "}\n" + "\n"); } - if (GenerateHasBits(descriptor_)) { - // Integers for bit fields. - int totalBits = 0; - for (int i = 0; i < descriptor_->field_count(); i++) { - totalBits += field_generators_.get(descriptor_->field(i)) - .GetNumBitsForBuilder(); - } - int totalInts = (totalBits + 31) / 32; - for (int i = 0; i < totalInts; i++) { - printer->Print("private int $bit_field_name$;\n", - "bit_field_name", GetBitFieldName(i)); - } + // Integers for bit fields. + int totalBits = 0; + for (int i = 0; i < descriptor_->field_count(); i++) { + totalBits += + field_generators_.get(descriptor_->field(i)).GetNumBitsForBuilder(); + } + int totalInts = (totalBits + 31) / 32; + for (int i = 0; i < totalInts; i++) { + printer->Print("private int $bit_field_name$;\n", "bit_field_name", + GetBitFieldName(i)); } for (int i = 0; i < descriptor_->field_count(); i++) { printer->Print("\n"); field_generators_.get(descriptor_->field(i)) - .GenerateBuilderMembers(printer); + .GenerateBuilderMembers(printer); } - // Override methods declared in GeneratedMessage to return the concrete - // generated type so callsites won't depend on GeneratedMessage. This - // is needed to keep binary compatibility when we change generated code - // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release - // we changed all generated code to subclass GeneratedMessageV3). + // Override methods declared in GeneratedMessage to return the concrete + // generated type so callsites won't depend on GeneratedMessage. This + // is needed to keep binary compatibility when we change generated code + // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release + // we changed all generated code to subclass GeneratedMessageV3). printer->Print( - "@java.lang.Override\n" - "public final Builder setUnknownFields(\n" - " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" - " return super.setUnknownFields(unknownFields);\n" - "}\n" - "\n" - "@java.lang.Override\n" - "public final Builder mergeUnknownFields(\n" - " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" - " return super.mergeUnknownFields(unknownFields);\n" - "}\n" - "\n"); + "@java.lang.Override\n" + "public final Builder setUnknownFields(\n" + " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" + " return super.setUnknownFields(unknownFields);\n" + "}\n" + "\n" + "@java.lang.Override\n" + "public final Builder mergeUnknownFields(\n" + " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" + " return super.mergeUnknownFields(unknownFields);\n" + "}\n" + "\n"); printer->Print( - "\n" - "// @@protoc_insertion_point(builder_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(builder_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n"); @@ -199,17 +194,16 @@ Generate(io::Printer* printer) { // =================================================================== -void MessageBuilderGenerator:: -GenerateDescriptorMethods(io::Printer* printer) { +void MessageBuilderGenerator::GenerateDescriptorMethods(io::Printer* printer) { if (!descriptor_->options().no_standard_descriptor_accessor()) { printer->Print( - "public static final com.google.protobuf.Descriptors.Descriptor\n" - " getDescriptor() {\n" - " return $fileclass$.internal_$identifier$_descriptor;\n" - "}\n" - "\n", - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_)); + "public static final com.google.protobuf.Descriptors.Descriptor\n" + " getDescriptor() {\n" + " return $fileclass$.internal_$identifier$_descriptor;\n" + "}\n" + "\n", + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_)); } std::vector map_fields; for (int i = 0; i < descriptor_->field_count(); i++) { @@ -221,10 +215,10 @@ GenerateDescriptorMethods(io::Printer* printer) { } if (!map_fields.empty()) { printer->Print( - "@SuppressWarnings({\"rawtypes\"})\n" - "protected com.google.protobuf.MapField internalGetMapField(\n" - " int number) {\n" - " switch (number) {\n"); + "@SuppressWarnings({\"rawtypes\"})\n" + "protected com.google.protobuf.MapField internalGetMapField(\n" + " int number) {\n" + " switch (number) {\n"); printer->Indent(); printer->Indent(); for (int i = 0; i < map_fields.size(); ++i) { @@ -233,7 +227,7 @@ GenerateDescriptorMethods(io::Printer* printer) { printer->Print( "case $number$:\n" " return internalGet$capitalized_name$();\n", - "number", SimpleItoa(field->number()), "capitalized_name", + "number", StrCat(field->number()), "capitalized_name", info->capitalized_name); } printer->Print( @@ -246,20 +240,19 @@ GenerateDescriptorMethods(io::Printer* printer) { " }\n" "}\n"); printer->Print( - "@SuppressWarnings({\"rawtypes\"})\n" - "protected com.google.protobuf.MapField internalGetMutableMapField(\n" - " int number) {\n" - " switch (number) {\n"); + "@SuppressWarnings({\"rawtypes\"})\n" + "protected com.google.protobuf.MapField internalGetMutableMapField(\n" + " int number) {\n" + " switch (number) {\n"); printer->Indent(); printer->Indent(); for (int i = 0; i < map_fields.size(); ++i) { const FieldDescriptor* field = map_fields[i]; - const FieldGeneratorInfo* info = - context_->GetFieldGeneratorInfo(field); + const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); printer->Print( "case $number$:\n" " return internalGetMutable$capitalized_name$();\n", - "number", SimpleItoa(field->number()), "capitalized_name", + "number", StrCat(field->number()), "capitalized_name", info->capitalized_name); } printer->Print( @@ -273,24 +266,24 @@ GenerateDescriptorMethods(io::Printer* printer) { "}\n"); } printer->Print( - "@java.lang.Override\n" - "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" - " internalGetFieldAccessorTable() {\n" - " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" - " .ensureFieldAccessorsInitialized(\n" - " $classname$.class, $classname$.Builder.class);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "@java.lang.Override\n" + "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" + " internalGetFieldAccessorTable() {\n" + " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" + " .ensureFieldAccessorsInitialized(\n" + " $classname$.class, $classname$.Builder.class);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } // =================================================================== -void MessageBuilderGenerator:: -GenerateCommonBuilderMethods(io::Printer* printer) { +void MessageBuilderGenerator::GenerateCommonBuilderMethods( + io::Printer* printer) { printer->Print( "// Construct using $classname$.newBuilder()\n" "private Builder() {\n" @@ -300,19 +293,19 @@ GenerateCommonBuilderMethods(io::Printer* printer) { "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "private Builder(\n" - " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n" - " super(parent);\n" - " maybeForceBuilderInitialization();\n" - "}\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "private Builder(\n" + " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n" + " super(parent);\n" + " maybeForceBuilderInitialization();\n" + "}\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), "ver", + GeneratedCodeVersionSuffix()); printer->Print( - "private void maybeForceBuilderInitialization() {\n" - " if (com.google.protobuf.GeneratedMessage$ver$\n" - " .alwaysUseFieldBuilders) {\n", - "ver", GeneratedCodeVersionSuffix()); + "private void maybeForceBuilderInitialization() {\n" + " if (com.google.protobuf.GeneratedMessage$ver$\n" + " .alwaysUseFieldBuilders) {\n", + "ver", GeneratedCodeVersionSuffix()); printer->Indent(); printer->Indent(); @@ -326,13 +319,13 @@ GenerateCommonBuilderMethods(io::Printer* printer) { printer->Outdent(); printer->Print( - " }\n" - "}\n"); + " }\n" + "}\n"); printer->Print( - "@java.lang.Override\n" - "public Builder clear() {\n" - " super.clear();\n"); + "@java.lang.Override\n" + "public Builder clear() {\n" + " super.clear();\n"); printer->Indent(); @@ -345,55 +338,55 @@ GenerateCommonBuilderMethods(io::Printer* printer) { for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { printer->Print( - "$oneof_name$Case_ = 0;\n" - "$oneof_name$_ = null;\n", - "oneof_name", context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); + "$oneof_name$Case_ = 0;\n" + "$oneof_name$_ = null;\n", + "oneof_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name); } printer->Outdent(); printer->Print( - " return this;\n" - "}\n" - "\n"); + " return this;\n" + "}\n" + "\n"); printer->Print( - "@java.lang.Override\n" - "public com.google.protobuf.Descriptors.Descriptor\n" - " getDescriptorForType() {\n" - " return $fileclass$.internal_$identifier$_descriptor;\n" - "}\n" - "\n", - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_)); + "@java.lang.Override\n" + "public com.google.protobuf.Descriptors.Descriptor\n" + " getDescriptorForType() {\n" + " return $fileclass$.internal_$identifier$_descriptor;\n" + "}\n" + "\n", + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_)); // LITE runtime implements this in GeneratedMessageLite. printer->Print( - "@java.lang.Override\n" - "public $classname$ getDefaultInstanceForType() {\n" - " return $classname$.getDefaultInstance();\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public $classname$ getDefaultInstanceForType() {\n" + " return $classname$.getDefaultInstance();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "@java.lang.Override\n" - "public $classname$ build() {\n" - " $classname$ result = buildPartial();\n" - " if (!result.isInitialized()) {\n" - " throw newUninitializedMessageException(result);\n" - " }\n" - " return result;\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public $classname$ build() {\n" + " $classname$ result = buildPartial();\n" + " if (!result.isInitialized()) {\n" + " throw newUninitializedMessageException(result);\n" + " }\n" + " return result;\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "@java.lang.Override\n" - "public $classname$ buildPartial() {\n" - " $classname$ result = new $classname$(this);\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public $classname$ buildPartial() {\n" + " $classname$ result = new $classname$(this);\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Indent(); @@ -408,19 +401,17 @@ GenerateCommonBuilderMethods(io::Printer* printer) { int totalBuilderInts = (totalBuilderBits + 31) / 32; int totalMessageInts = (totalMessageBits + 31) / 32; - if (GenerateHasBits(descriptor_)) { - // Local vars for from and to bit fields to avoid accessing the builder and - // message over and over for these fields. Seems to provide a slight - // perforamance improvement in micro benchmark and this is also what proto1 - // code does. - for (int i = 0; i < totalBuilderInts; i++) { - printer->Print("int from_$bit_field_name$ = $bit_field_name$;\n", - "bit_field_name", GetBitFieldName(i)); - } - for (int i = 0; i < totalMessageInts; i++) { - printer->Print("int to_$bit_field_name$ = 0;\n", - "bit_field_name", GetBitFieldName(i)); - } + // Local vars for from and to bit fields to avoid accessing the builder and + // message over and over for these fields. Seems to provide a slight + // perforamance improvement in micro benchmark and this is also what proto1 + // code does. + for (int i = 0; i < totalBuilderInts; i++) { + printer->Print("int from_$bit_field_name$ = $bit_field_name$;\n", + "bit_field_name", GetBitFieldName(i)); + } + for (int i = 0; i < totalMessageInts; i++) { + printer->Print("int to_$bit_field_name$ = 0;\n", "bit_field_name", + GetBitFieldName(i)); } // Output generation code for each field. @@ -428,30 +419,27 @@ GenerateCommonBuilderMethods(io::Printer* printer) { field_generators_.get(descriptor_->field(i)).GenerateBuildingCode(printer); } - if (GenerateHasBits(descriptor_)) { - // Copy the bit field results to the generated message - for (int i = 0; i < totalMessageInts; i++) { - printer->Print("result.$bit_field_name$ = to_$bit_field_name$;\n", - "bit_field_name", GetBitFieldName(i)); - } + // Copy the bit field results to the generated message + for (int i = 0; i < totalMessageInts; i++) { + printer->Print("result.$bit_field_name$ = to_$bit_field_name$;\n", + "bit_field_name", GetBitFieldName(i)); } for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - printer->Print("result.$oneof_name$Case_ = $oneof_name$Case_;\n", - "oneof_name", context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); + printer->Print( + "result.$oneof_name$Case_ = $oneof_name$Case_;\n", "oneof_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name); } printer->Outdent(); - printer->Print( - " onBuilt();\n"); + printer->Print(" onBuilt();\n"); printer->Print( - " return result;\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + " return result;\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); // Override methods declared in GeneratedMessage to return the concrete // generated type so callsites won't depend on GeneratedMessage. This @@ -459,190 +447,181 @@ GenerateCommonBuilderMethods(io::Printer* printer) { // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release // we changed all generated code to subclass GeneratedMessageV3). printer->Print( - "@java.lang.Override\n" - "public Builder clone() {\n" - " return (Builder) super.clone();\n" - "}\n" - "@java.lang.Override\n" - "public Builder setField(\n" - " com.google.protobuf.Descriptors.FieldDescriptor field,\n" - " java.lang.Object value) {\n" - " return (Builder) super.setField(field, value);\n" - "}\n" - "@java.lang.Override\n" - "public Builder clearField(\n" - " com.google.protobuf.Descriptors.FieldDescriptor field) {\n" - " return (Builder) super.clearField(field);\n" - "}\n" - "@java.lang.Override\n" - "public Builder clearOneof(\n" - " com.google.protobuf.Descriptors.OneofDescriptor oneof) {\n" - " return (Builder) super.clearOneof(oneof);\n" - "}\n" - "@java.lang.Override\n" - "public Builder setRepeatedField(\n" - " com.google.protobuf.Descriptors.FieldDescriptor field,\n" - " int index, java.lang.Object value) {\n" - " return (Builder) super.setRepeatedField(field, index, value);\n" - "}\n" - "@java.lang.Override\n" - "public Builder addRepeatedField(\n" - " com.google.protobuf.Descriptors.FieldDescriptor field,\n" - " java.lang.Object value) {\n" - " return (Builder) super.addRepeatedField(field, value);\n" - "}\n"); - - if (descriptor_->extension_range_count() > 0) { - printer->Print( "@java.lang.Override\n" - "public Builder setExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, Type> extension,\n" - " Type value) {\n" - " return (Builder) super.setExtension(extension, value);\n" + "public Builder clone() {\n" + " return super.clone();\n" "}\n" "@java.lang.Override\n" - "public Builder setExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, java.util.List> extension,\n" - " int index, Type value) {\n" - " return (Builder) super.setExtension(extension, index, value);\n" + "public Builder setField(\n" + " com.google.protobuf.Descriptors.FieldDescriptor field,\n" + " java.lang.Object value) {\n" + " return super.setField(field, value);\n" "}\n" "@java.lang.Override\n" - "public Builder addExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, java.util.List> extension,\n" - " Type value) {\n" - " return (Builder) super.addExtension(extension, value);\n" + "public Builder clearField(\n" + " com.google.protobuf.Descriptors.FieldDescriptor field) {\n" + " return super.clearField(field);\n" "}\n" "@java.lang.Override\n" - "public Builder clearExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, ?> extension) {\n" - " return (Builder) super.clearExtension(extension);\n" - "}\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public Builder clearOneof(\n" + " com.google.protobuf.Descriptors.OneofDescriptor oneof) {\n" + " return super.clearOneof(oneof);\n" + "}\n" + "@java.lang.Override\n" + "public Builder setRepeatedField(\n" + " com.google.protobuf.Descriptors.FieldDescriptor field,\n" + " int index, java.lang.Object value) {\n" + " return super.setRepeatedField(field, index, value);\n" + "}\n" + "@java.lang.Override\n" + "public Builder addRepeatedField(\n" + " com.google.protobuf.Descriptors.FieldDescriptor field,\n" + " java.lang.Object value) {\n" + " return super.addRepeatedField(field, value);\n" + "}\n"); + + if (descriptor_->extension_range_count() > 0) { + printer->Print( + "@java.lang.Override\n" + "public Builder setExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, Type> extension,\n" + " Type value) {\n" + " return super.setExtension(extension, value);\n" + "}\n" + "@java.lang.Override\n" + "public Builder setExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, java.util.List> extension,\n" + " int index, Type value) {\n" + " return super.setExtension(extension, index, value);\n" + "}\n" + "@java.lang.Override\n" + "public Builder addExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, java.util.List> extension,\n" + " Type value) {\n" + " return super.addExtension(extension, value);\n" + "}\n" + "@java.lang.Override\n" + "public Builder clearExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, ?> extension) {\n" + " return super.clearExtension(extension);\n" + "}\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } // ----------------------------------------------------------------- if (context_->HasGeneratedMethods(descriptor_)) { printer->Print( - "@java.lang.Override\n" - "public Builder mergeFrom(com.google.protobuf.Message other) {\n" - " if (other instanceof $classname$) {\n" - " return mergeFrom(($classname$)other);\n" - " } else {\n" - " super.mergeFrom(other);\n" - " return this;\n" - " }\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public Builder mergeFrom(com.google.protobuf.Message other) {\n" + " if (other instanceof $classname$) {\n" + " return mergeFrom(($classname$)other);\n" + " } else {\n" + " super.mergeFrom(other);\n" + " return this;\n" + " }\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "public Builder mergeFrom($classname$ other) {\n" - // Optimization: If other is the default instance, we know none of its - // fields are set so we can skip the merge. - " if (other == $classname$.getDefaultInstance()) return this;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public Builder mergeFrom($classname$ other) {\n" + // Optimization: If other is the default instance, we know none of its + // fields are set so we can skip the merge. + " if (other == $classname$.getDefaultInstance()) return this;\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Indent(); for (int i = 0; i < descriptor_->field_count(); i++) { if (!descriptor_->field(i)->containing_oneof()) { - field_generators_.get( - descriptor_->field(i)).GenerateMergingCode(printer); + field_generators_.get(descriptor_->field(i)) + .GenerateMergingCode(printer); } } // Merge oneof fields. for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) { - printer->Print( - "switch (other.get$oneof_capitalized_name$Case()) {\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name); + printer->Print("switch (other.get$oneof_capitalized_name$Case()) {\n", + "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name); printer->Indent(); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); - printer->Print( - "case $field_name$: {\n", - "field_name", - ToUpper(field->name())); + printer->Print("case $field_name$: {\n", "field_name", + ToUpper(field->name())); printer->Indent(); field_generators_.get(field).GenerateMergingCode(printer); - printer->Print( - "break;\n"); + printer->Print("break;\n"); printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } printer->Print( - "case $cap_oneof_name$_NOT_SET: {\n" - " break;\n" - "}\n", - "cap_oneof_name", - ToUpper(context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name)); + "case $cap_oneof_name$_NOT_SET: {\n" + " break;\n" + "}\n", + "cap_oneof_name", + ToUpper( + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->name)); printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } printer->Outdent(); // if message type has extensions if (descriptor_->extension_range_count() > 0) { - printer->Print( - " this.mergeExtensionFields(other);\n"); + printer->Print(" this.mergeExtensionFields(other);\n"); } - printer->Print( - " this.mergeUnknownFields(other.unknownFields);\n"); + printer->Print(" this.mergeUnknownFields(other.unknownFields);\n"); - printer->Print( - " onChanged();\n"); + printer->Print(" onChanged();\n"); printer->Print( - " return this;\n" - "}\n" - "\n"); + " return this;\n" + "}\n" + "\n"); } } // =================================================================== -void MessageBuilderGenerator:: -GenerateBuilderParsingMethods(io::Printer* printer) { +void MessageBuilderGenerator::GenerateBuilderParsingMethods( + io::Printer* printer) { printer->Print( - "@java.lang.Override\n" - "public Builder mergeFrom(\n" - " com.google.protobuf.CodedInputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " $classname$ parsedMessage = null;\n" - " try {\n" - " parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);\n" - " } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" - " parsedMessage = ($classname$) e.getUnfinishedMessage();\n" - " throw e.unwrapIOException();\n" - " } finally {\n" - " if (parsedMessage != null) {\n" - " mergeFrom(parsedMessage);\n" - " }\n" - " }\n" - " return this;\n" - "}\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public Builder mergeFrom(\n" + " com.google.protobuf.CodedInputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " $classname$ parsedMessage = null;\n" + " try {\n" + " parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);\n" + " } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" + " parsedMessage = ($classname$) e.getUnfinishedMessage();\n" + " throw e.unwrapIOException();\n" + " } finally {\n" + " if (parsedMessage != null) {\n" + " mergeFrom(parsedMessage);\n" + " }\n" + " }\n" + " return this;\n" + "}\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } // =================================================================== -void MessageBuilderGenerator::GenerateIsInitialized( - io::Printer* printer) { +void MessageBuilderGenerator::GenerateIsInitialized(io::Printer* printer) { printer->Print( - "@java.lang.Override\n" - "public final boolean isInitialized() {\n"); + "@java.lang.Override\n" + "public final boolean isInitialized() {\n"); printer->Indent(); // Check that all required fields in this message are set. @@ -654,10 +633,10 @@ void MessageBuilderGenerator::GenerateIsInitialized( if (field->is_required()) { printer->Print( - "if (!has$name$()) {\n" - " return false;\n" - "}\n", - "name", info->capitalized_name); + "if (!has$name$()) {\n" + " return false;\n" + "}\n", + "name", info->capitalized_name); } } @@ -670,12 +649,12 @@ void MessageBuilderGenerator::GenerateIsInitialized( switch (field->label()) { case FieldDescriptor::LABEL_REQUIRED: printer->Print( - "if (!get$name$().isInitialized()) {\n" - " return false;\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); + "if (!get$name$().isInitialized()) {\n" + " return false;\n" + "}\n", + "type", + name_resolver_->GetImmutableClassName(field->message_type()), + "name", info->capitalized_name); break; case FieldDescriptor::LABEL_OPTIONAL: if (!SupportFieldPresence(descriptor_->file()) && @@ -685,40 +664,40 @@ void MessageBuilderGenerator::GenerateIsInitialized( context_->GetOneofGeneratorInfo(oneof); printer->Print("if ($oneof_name$Case_ == $field_number$) {\n", "oneof_name", oneof_info->name, "field_number", - SimpleItoa(field->number())); + StrCat(field->number())); } else { - printer->Print( - "if (has$name$()) {\n", - "name", info->capitalized_name); + printer->Print("if (has$name$()) {\n", "name", + info->capitalized_name); } printer->Print( - " if (!get$name$().isInitialized()) {\n" - " return false;\n" - " }\n" - "}\n", - "name", info->capitalized_name); - break; - case FieldDescriptor::LABEL_REPEATED: - if (IsMapEntry(field->message_type())) { - printer->Print( - "for ($type$ item : get$name$Map().values()) {\n" - " if (!item.isInitialized()) {\n" + " if (!get$name$().isInitialized()) {\n" " return false;\n" " }\n" "}\n", - "type", MapValueImmutableClassdName(field->message_type(), - name_resolver_), "name", info->capitalized_name); + break; + case FieldDescriptor::LABEL_REPEATED: + if (IsMapEntry(field->message_type())) { + printer->Print( + "for ($type$ item : get$name$Map().values()) {\n" + " if (!item.isInitialized()) {\n" + " return false;\n" + " }\n" + "}\n", + "type", + MapValueImmutableClassdName(field->message_type(), + name_resolver_), + "name", info->capitalized_name); } else { printer->Print( - "for (int i = 0; i < get$name$Count(); i++) {\n" - " if (!get$name$(i).isInitialized()) {\n" - " return false;\n" - " }\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); + "for (int i = 0; i < get$name$Count(); i++) {\n" + " if (!get$name$(i).isInitialized()) {\n" + " return false;\n" + " }\n" + "}\n", + "type", + name_resolver_->GetImmutableClassName(field->message_type()), + "name", info->capitalized_name); } break; } @@ -727,17 +706,17 @@ void MessageBuilderGenerator::GenerateIsInitialized( if (descriptor_->extension_range_count() > 0) { printer->Print( - "if (!extensionsAreInitialized()) {\n" - " return false;\n" - "}\n"); + "if (!extensionsAreInitialized()) {\n" + " return false;\n" + "}\n"); } printer->Outdent(); printer->Print( - " return true;\n" - "}\n" - "\n"); + " return true;\n" + "}\n" + "\n"); } // =================================================================== diff --git a/src/google/protobuf/compiler/java/java_message_builder.h b/src/google/protobuf/compiler/java/java_message_builder.h index d67a9c9d4f..a31d0badf9 100644 --- a/src/google/protobuf/compiler/java/java_message_builder.h +++ b/src/google/protobuf/compiler/java/java_message_builder.h @@ -35,8 +35,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_H__ -#include #include +#include #include namespace google { diff --git a/src/google/protobuf/compiler/java/java_message_builder_lite.cc b/src/google/protobuf/compiler/java/java_message_builder_lite.cc index b9a12d4ad9..ae34499131 100644 --- a/src/google/protobuf/compiler/java/java_message_builder_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_builder_lite.cc @@ -53,23 +53,18 @@ #include #include + namespace google { namespace protobuf { namespace compiler { namespace java { -namespace { -bool GenerateHasBits(const Descriptor* descriptor) { - return SupportFieldPresence(descriptor->file()) || - HasRepeatedFields(descriptor); -} -} // namespace - MessageBuilderLiteGenerator::MessageBuilderLiteGenerator( const Descriptor* descriptor, Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + : descriptor_(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()), + field_generators_(descriptor, context_) { GOOGLE_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A lite message generator is used to " "generate non-lite messages."; @@ -77,59 +72,57 @@ MessageBuilderLiteGenerator::MessageBuilderLiteGenerator( MessageBuilderLiteGenerator::~MessageBuilderLiteGenerator() {} -void MessageBuilderLiteGenerator:: -Generate(io::Printer* printer) { +void MessageBuilderLiteGenerator::Generate(io::Printer* printer) { WriteMessageDocComment(printer, descriptor_); printer->Print( - "public static final class Builder extends\n" - " com.google.protobuf.GeneratedMessageLite.$extendible$Builder<\n" - " $classname$, Builder> implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "extra_interfaces", ExtraBuilderInterfaces(descriptor_), - "extendible", - descriptor_->extension_range_count() > 0 ? "Extendable" : ""); + "public static final class Builder extends\n" + " com.google.protobuf.GeneratedMessageLite.$extendible$Builder<\n" + " $classname$, Builder> implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "extendible", + descriptor_->extension_range_count() > 0 ? "Extendable" : ""); printer->Indent(); GenerateCommonBuilderMethods(printer); // oneof - std::map vars; + std::map vars; for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - vars["oneof_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name; - vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name; - vars["oneof_index"] = - SimpleItoa(descriptor_->oneof_decl(i)->index()); + vars["oneof_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name; + vars["oneof_capitalized_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name; + vars["oneof_index"] = StrCat(descriptor_->oneof_decl(i)->index()); // oneofCase() and clearOneof() printer->Print(vars, - "@java.lang.Override\n" - "public $oneof_capitalized_name$Case\n" - " get$oneof_capitalized_name$Case() {\n" - " return instance.get$oneof_capitalized_name$Case();\n" - "}\n" - "\n" - "public Builder clear$oneof_capitalized_name$() {\n" - " copyOnWrite();\n" - " instance.clear$oneof_capitalized_name$();\n" - " return this;\n" - "}\n" - "\n"); + "@java.lang.Override\n" + "public $oneof_capitalized_name$Case\n" + " get$oneof_capitalized_name$Case() {\n" + " return instance.get$oneof_capitalized_name$Case();\n" + "}\n" + "\n" + "public Builder clear$oneof_capitalized_name$() {\n" + " copyOnWrite();\n" + " instance.clear$oneof_capitalized_name$();\n" + " return this;\n" + "}\n" + "\n"); } for (int i = 0; i < descriptor_->field_count(); i++) { printer->Print("\n"); field_generators_.get(descriptor_->field(i)) - .GenerateBuilderMembers(printer); + .GenerateBuilderMembers(printer); } printer->Print( - "\n" - "// @@protoc_insertion_point(builder_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(builder_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n"); @@ -137,15 +130,15 @@ Generate(io::Printer* printer) { // =================================================================== -void MessageBuilderLiteGenerator:: -GenerateCommonBuilderMethods(io::Printer* printer) { +void MessageBuilderLiteGenerator::GenerateCommonBuilderMethods( + io::Printer* printer) { printer->Print( - "// Construct using $classname$.newBuilder()\n" - "private Builder() {\n" - " super(DEFAULT_INSTANCE);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "// Construct using $classname$.newBuilder()\n" + "private Builder() {\n" + " super(DEFAULT_INSTANCE);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } // =================================================================== diff --git a/src/google/protobuf/compiler/java/java_message_builder_lite.h b/src/google/protobuf/compiler/java/java_message_builder_lite.h index 079d86a925..e0a48dc1f7 100644 --- a/src/google/protobuf/compiler/java/java_message_builder_lite.h +++ b/src/google/protobuf/compiler/java/java_message_builder_lite.h @@ -35,8 +35,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_LITE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_LITE_H__ -#include #include +#include #include namespace google { diff --git a/src/google/protobuf/compiler/java/java_message_field.cc b/src/google/protobuf/compiler/java/java_message_field.cc index 92425d064e..273faecedb 100644 --- a/src/google/protobuf/compiler/java/java_message_field.cc +++ b/src/google/protobuf/compiler/java/java_message_field.cc @@ -36,9 +36,9 @@ #include #include -#include #include #include +#include #include #include #include @@ -51,12 +51,10 @@ namespace java { namespace { -void SetMessageVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - const FieldGeneratorInfo* info, +void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, + int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); (*variables)["type"] = @@ -64,17 +62,17 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["mutable_type"] = name_resolver->GetMutableClassName(descriptor->message_type()); (*variables)["group_or_message"] = - (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? - "Group" : "Message"; + (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? "Group" + : "Message"; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["on_changed"] = "onChanged();"; (*variables)["ver"] = GeneratedCodeVersionSuffix(); (*variables)["get_parser"] = - ExposePublicParser(descriptor->message_type()->file()) - ? "PARSER" : "parser()"; + ExposePublicParser(descriptor->message_type()->file()) ? "PARSER" + : "parser()"; if (SupportFieldPresence(descriptor->file())) { // For singular messages and builders, one bit is used for the hasField bit. @@ -121,101 +119,99 @@ void SetMessageVariables(const FieldDescriptor* descriptor, // =================================================================== -ImmutableMessageFieldGenerator:: -ImmutableMessageFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { - SetMessageVariables(descriptor, messageBitIndex, builderBitIndex, - context->GetFieldGeneratorInfo(descriptor), - name_resolver_, &variables_); +ImmutableMessageFieldGenerator::ImmutableMessageFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { + SetMessageVariables(descriptor, messageBitIndex, builderBitIndex, + context->GetFieldGeneratorInfo(descriptor), + name_resolver_, &variables_); } ImmutableMessageFieldGenerator::~ImmutableMessageFieldGenerator() {} int ImmutableMessageFieldGenerator::GetNumBitsForMessage() const { - return 1; + return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } int ImmutableMessageFieldGenerator::GetNumBitsForBuilder() const { - return 1; + return GetNumBitsForMessage(); } -void ImmutableMessageFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { // TODO(jonp): In the future, consider having a method specific to the // interface so that builders can choose dynamically to either return a // message or a nested builder, so that asking for the interface doesn't // cause a message to ever be built. - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print(variables_, "$deprecation$boolean has$capitalized_name$();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder();\n"); + printer->Print( + variables_, + "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder();\n"); } -void ImmutableMessageFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $type$ $name$_;\n"); +void ImmutableMessageFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } else { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $name$_ != null;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $name$_ != null;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " return get$capitalized_name$();\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " return get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } void ImmutableMessageFieldGenerator::PrintNestedBuilderCondition( - io::Printer* printer, - const char* regular_case, + io::Printer* printer, const char* regular_case, const char* nested_builder_case) const { printer->Print(variables_, "if ($name$Builder_ == null) {\n"); printer->Indent(); @@ -229,10 +225,8 @@ void ImmutableMessageFieldGenerator::PrintNestedBuilderCondition( } void ImmutableMessageFieldGenerator::PrintNestedBuilderFunction( - io::Printer* printer, - const char* method_prototype, - const char* regular_case, - const char* nested_builder_case, + io::Printer* printer, const char* method_prototype, + const char* regular_case, const char* nested_builder_case, const char* trailing_code) const { printer->Print(variables_, method_prototype); printer->Annotate("{", "}", descriptor_); @@ -246,8 +240,8 @@ void ImmutableMessageFieldGenerator::PrintNestedBuilderFunction( printer->Print("}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // When using nested-builders, the code initially works just like the // non-nested builder case. It only creates a nested builder lazily on // demand and then forever delegates to it after creation. @@ -257,190 +251,190 @@ GenerateBuilderMembers(io::Printer* printer) const { printer->Print(variables_, "private $type$ $name$_;\n"); printer->Print(variables_, - // If this builder is non-null, it is used and the other fields are - // ignored. - "private com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;" - "\n"); + // If this builder is non-null, it is used and the other fields + // are ignored. + "private com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;" + "\n"); // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". // boolean hasField() - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); if (support_field_presence) { - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_builder$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_builder$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } else { - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $name$Builder_ != null || $name$_ != null;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $name$Builder_ != null || $name$_ != null;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } // Field getField() - WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ ${$get$capitalized_name$$}$()", - "return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n", - "return $name$Builder_.getMessage();\n", - NULL); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + PrintNestedBuilderFunction( + printer, "$deprecation$public $type$ ${$get$capitalized_name$$}$()", + "return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n", + "return $name$Builder_.getMessage();\n", NULL); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "$name$_ = value;\n" - "$on_changed$\n", + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "$name$_ = value;\n" + "$on_changed$\n", - "$name$Builder_.setMessage(value);\n", + "$name$Builder_.setMessage(value);\n", - "$set_has_field_bit_builder$\n" - "return this;\n"); + "$set_has_field_bit_builder$\n" + "return this;\n"); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " $type$.Builder builderForValue)", - "$name$_ = builderForValue.build();\n" - "$on_changed$\n", + "$name$_ = builderForValue.build();\n" + "$on_changed$\n", - "$name$Builder_.setMessage(builderForValue.build());\n", + "$name$Builder_.setMessage(builderForValue.build());\n", - "$set_has_field_bit_builder$\n" - "return this;\n"); + "$set_has_field_bit_builder$\n" + "return this;\n"); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", - - support_field_presence - ? "if ($get_has_field_bit_builder$ &&\n" - " $name$_ != null &&\n" - " $name$_ != $type$.getDefaultInstance()) {\n" - " $name$_ =\n" - " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" - "} else {\n" - " $name$_ = value;\n" - "}\n" - "$on_changed$\n" - : "if ($name$_ != null) {\n" - " $name$_ =\n" - " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" - "} else {\n" - " $name$_ = value;\n" - "}\n" - "$on_changed$\n", - - "$name$Builder_.mergeFrom(value);\n", - - "$set_has_field_bit_builder$\n" - "return this;\n"); + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", + + support_field_presence + ? "if ($get_has_field_bit_builder$ &&\n" + " $name$_ != null &&\n" + " $name$_ != $type$.getDefaultInstance()) {\n" + " $name$_ =\n" + " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" + "} else {\n" + " $name$_ = value;\n" + "}\n" + "$on_changed$\n" + : "if ($name$_ != null) {\n" + " $name$_ =\n" + " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" + "} else {\n" + " $name$_ = value;\n" + "}\n" + "$on_changed$\n", + + "$name$Builder_.mergeFrom(value);\n", + + "$set_has_field_bit_builder$\n" + "return this;\n"); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$clear$capitalized_name$$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public Builder ${$clear$capitalized_name$$}$()", - "$name$_ = null;\n" - "$on_changed$\n", + "$name$_ = null;\n" + "$on_changed$\n", - support_field_presence - ? "$name$Builder_.clear();\n" - : "$name$_ = null;\n" - "$name$Builder_ = null;\n", + support_field_presence ? "$name$Builder_.clear();\n" + : "$name$_ = null;\n" + "$name$Builder_ = null;\n", - "$clear_has_field_bit_builder$\n" - "return this;\n"); + "$clear_has_field_bit_builder$\n" + "return this;\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder " - "${$get$capitalized_name$Builder$}$() {\n" - " $set_has_field_bit_builder$\n" - " $on_changed$\n" - " return get$capitalized_name$FieldBuilder().getBuilder();\n" - "}\n"); + "$deprecation$public $type$.Builder " + "${$get$capitalized_name$Builder$}$() {\n" + " $set_has_field_bit_builder$\n" + " $on_changed$\n" + " return get$capitalized_name$FieldBuilder().getBuilder();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " if ($name$Builder_ != null) {\n" - " return $name$Builder_.getMessageOrBuilder();\n" - " } else {\n" - " return $name$_ == null ?\n" - " $type$.getDefaultInstance() : $name$_;\n" - " }\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " if ($name$Builder_ != null) {\n" + " return $name$Builder_.getMessageOrBuilder();\n" + " } else {\n" + " return $name$_ == null ?\n" + " $type$.getDefaultInstance() : $name$_;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> \n" - " get$capitalized_name$FieldBuilder() {\n" - " if ($name$Builder_ == null) {\n" - " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder>(\n" - " get$capitalized_name$(),\n" - " getParentForChildren(),\n" - " isClean());\n" - " $name$_ = null;\n" - " }\n" - " return $name$Builder_;\n" - "}\n"); + printer->Print( + variables_, + "private com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> \n" + " get$capitalized_name$FieldBuilder() {\n" + " if ($name$Builder_ == null) {\n" + " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder>(\n" + " get$capitalized_name$(),\n" + " getParentForChildren(),\n" + " isClean());\n" + " $name$_ = null;\n" + " }\n" + " return $name$Builder_;\n" + "}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "get$capitalized_name$FieldBuilder();\n"); + printer->Print(variables_, "get$capitalized_name$FieldBuilder();\n"); } } +void ImmutableMessageFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const {} -void ImmutableMessageFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const {} - -void ImmutableMessageFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - PrintNestedBuilderCondition(printer, - "$name$_ = null;\n", + PrintNestedBuilderCondition(printer, "$name$_ = null;\n", - "$name$Builder_.clear();\n"); + "$name$Builder_.clear();\n"); printer->Print(variables_, "$clear_has_field_bit_builder$\n"); } else { - PrintNestedBuilderCondition(printer, - "$name$_ = null;\n", + PrintNestedBuilderCondition(printer, "$name$_ = null;\n", - "$name$_ = null;\n" - "$name$Builder_ = null;\n"); + "$name$_ = null;\n" + "$name$Builder_ = null;\n"); } } -void ImmutableMessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { printer->Print(variables_, - "if (other.has$capitalized_name$()) {\n" - " merge$capitalized_name$(other.get$capitalized_name$());\n" - "}\n"); + "if (other.has$capitalized_name$()) {\n" + " merge$capitalized_name$(other.get$capitalized_name$());\n" + "}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, "if ($get_has_field_bit_from_local$) {\n"); printer->Indent(); @@ -456,357 +450,359 @@ GenerateBuildingCode(io::Printer* printer) const { } } -void ImmutableMessageFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "$type$.Builder subBuilder = null;\n" - "if ($is_field_present_message$) {\n" - " subBuilder = $name$_.toBuilder();\n" - "}\n"); + "$type$.Builder subBuilder = null;\n" + "if ($is_field_present_message$) {\n" + " subBuilder = $name$_.toBuilder();\n" + "}\n"); if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { printer->Print(variables_, - "$name$_ = input.readGroup($number$, $type$.$get_parser$,\n" - " extensionRegistry);\n"); + "$name$_ = input.readGroup($number$, $type$.$get_parser$,\n" + " extensionRegistry);\n"); } else { printer->Print(variables_, - "$name$_ = input.readMessage($type$.$get_parser$, extensionRegistry);\n"); + "$name$_ = input.readMessage($type$.$get_parser$, " + "extensionRegistry);\n"); } printer->Print(variables_, - "if (subBuilder != null) {\n" - " subBuilder.mergeFrom($name$_);\n" - " $name$_ = subBuilder.buildPartial();\n" - "}\n" - "$set_has_field_bit_message$\n"); + "if (subBuilder != null) {\n" + " subBuilder.mergeFrom($name$_);\n" + " $name$_ = subBuilder.buildPartial();\n" + "}\n" + "$set_has_field_bit_message$\n"); } -void ImmutableMessageFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // noop for messages. } -void ImmutableMessageFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.write$group_or_message$($number$, get$capitalized_name$());\n" - "}\n"); +void ImmutableMessageFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($is_field_present_message$) {\n" + " output.write$group_or_message$($number$, get$capitalized_name$());\n" + "}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, get$capitalized_name$());\n" - "}\n"); +void ImmutableMessageFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($is_field_present_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$group_or_message$Size($number$, get$capitalized_name$());\n" + "}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); + "if (!get$capitalized_name$()\n" + " .equals(other.get$capitalized_name$())) return false;\n"); } -void ImmutableMessageFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateHashCode( + io::Printer* printer) const { printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); + "hash = (37 * hash) + $constant_name$;\n" + "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); } -string ImmutableMessageFieldGenerator::GetBoxedType() const { +std::string ImmutableMessageFieldGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } // =================================================================== -ImmutableMessageOneofFieldGenerator:: -ImmutableMessageOneofFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : ImmutableMessageFieldGenerator( - descriptor, messageBitIndex, builderBitIndex, context) { +ImmutableMessageOneofFieldGenerator::ImmutableMessageOneofFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : ImmutableMessageFieldGenerator(descriptor, messageBitIndex, + builderBitIndex, context) { const OneofGeneratorInfo* info = context->GetOneofGeneratorInfo(descriptor->containing_oneof()); SetCommonOneofVariables(descriptor, info, &variables_); } -ImmutableMessageOneofFieldGenerator:: -~ImmutableMessageOneofFieldGenerator() {} +ImmutableMessageOneofFieldGenerator::~ImmutableMessageOneofFieldGenerator() {} -void ImmutableMessageOneofFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMessageOneofFieldGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - " }\n" - " return $type$.getDefaultInstance();\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + " }\n" + " return $type$.getDefaultInstance();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - " }\n" - " return $type$.getDefaultInstance();\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + " }\n" + " return $type$.getDefaultInstance();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableMessageOneofFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableMessageOneofFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // When using nested-builders, the code initially works just like the // non-nested builder case. It only creates a nested builder lazily on // demand and then forever delegates to it after creation. printer->Print(variables_, - // If this builder is non-null, it is used and the other fields are - // ignored. - "private com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;" - "\n"); + // If this builder is non-null, it is used and the other fields + // are ignored. + "private com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;" + "\n"); // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". // boolean hasField() - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field getField() - WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ ${$get$capitalized_name$$}$()", + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + PrintNestedBuilderFunction( + printer, "$deprecation$public $type$ ${$get$capitalized_name$$}$()", - "if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - "}\n" - "return $type$.getDefaultInstance();\n", + "if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + "}\n" + "return $type$.getDefaultInstance();\n", - "if ($has_oneof_case_message$) {\n" - " return $name$Builder_.getMessage();\n" - "}\n" - "return $type$.getDefaultInstance();\n", + "if ($has_oneof_case_message$) {\n" + " return $name$Builder_.getMessage();\n" + "}\n" + "return $type$.getDefaultInstance();\n", - NULL); + NULL); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "$oneof_name$_ = value;\n" - "$on_changed$\n", + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "$oneof_name$_ = value;\n" + "$on_changed$\n", - "$name$Builder_.setMessage(value);\n", + "$name$Builder_.setMessage(value);\n", - "$set_oneof_case_message$;\n" - "return this;\n"); + "$set_oneof_case_message$;\n" + "return this;\n"); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " $type$.Builder builderForValue)", - "$oneof_name$_ = builderForValue.build();\n" - "$on_changed$\n", + "$oneof_name$_ = builderForValue.build();\n" + "$on_changed$\n", - "$name$Builder_.setMessage(builderForValue.build());\n", + "$name$Builder_.setMessage(builderForValue.build());\n", - "$set_oneof_case_message$;\n" - "return this;\n"); + "$set_oneof_case_message$;\n" + "return this;\n"); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", - - "if ($has_oneof_case_message$ &&\n" - " $oneof_name$_ != $type$.getDefaultInstance()) {\n" - " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n" - " .mergeFrom(value).buildPartial();\n" - "} else {\n" - " $oneof_name$_ = value;\n" - "}\n" - "$on_changed$\n", - - "if ($has_oneof_case_message$) {\n" - " $name$Builder_.mergeFrom(value);\n" - "}\n" - "$name$Builder_.setMessage(value);\n", - - "$set_oneof_case_message$;\n" - "return this;\n"); + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", + + "if ($has_oneof_case_message$ &&\n" + " $oneof_name$_ != $type$.getDefaultInstance()) {\n" + " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n" + " .mergeFrom(value).buildPartial();\n" + "} else {\n" + " $oneof_name$_ = value;\n" + "}\n" + "$on_changed$\n", + + "if ($has_oneof_case_message$) {\n" + " $name$Builder_.mergeFrom(value);\n" + "}\n" + "$name$Builder_.setMessage(value);\n", + + "$set_oneof_case_message$;\n" + "return this;\n"); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$clear$capitalized_name$$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public Builder ${$clear$capitalized_name$$}$()", - "if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " $on_changed$\n" - "}\n", + "if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " $on_changed$\n" + "}\n", - "if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - "}\n" - "$name$Builder_.clear();\n", + "if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + "}\n" + "$name$Builder_.clear();\n", - "return this;\n"); + "return this;\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder " - "${$get$capitalized_name$Builder$}$() {\n" - " return get$capitalized_name$FieldBuilder().getBuilder();\n" - "}\n"); + "$deprecation$public $type$.Builder " + "${$get$capitalized_name$Builder$}$() {\n" + " return get$capitalized_name$FieldBuilder().getBuilder();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " if (($has_oneof_case_message$) && ($name$Builder_ != null)) {\n" - " return $name$Builder_.getMessageOrBuilder();\n" - " } else {\n" - " if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - " }\n" - " return $type$.getDefaultInstance();\n" - " }\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " if (($has_oneof_case_message$) && ($name$Builder_ != null)) {\n" + " return $name$Builder_.getMessageOrBuilder();\n" + " } else {\n" + " if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + " }\n" + " return $type$.getDefaultInstance();\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> \n" - " ${$get$capitalized_name$FieldBuilder$}$() {\n" - " if ($name$Builder_ == null) {\n" - " if (!($has_oneof_case_message$)) {\n" - " $oneof_name$_ = $type$.getDefaultInstance();\n" - " }\n" - " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder>(\n" - " ($type$) $oneof_name$_,\n" - " getParentForChildren(),\n" - " isClean());\n" - " $oneof_name$_ = null;\n" - " }\n" - " $set_oneof_case_message$;\n" - " $on_changed$;\n" - " return $name$Builder_;\n" - "}\n"); + printer->Print( + variables_, + "private com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> \n" + " ${$get$capitalized_name$FieldBuilder$}$() {\n" + " if ($name$Builder_ == null) {\n" + " if (!($has_oneof_case_message$)) {\n" + " $oneof_name$_ = $type$.getDefaultInstance();\n" + " }\n" + " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder>(\n" + " ($type$) $oneof_name$_,\n" + " getParentForChildren(),\n" + " isClean());\n" + " $oneof_name$_ = null;\n" + " }\n" + " $set_oneof_case_message$;\n" + " $on_changed$;\n" + " return $name$Builder_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableMessageOneofFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n"); +void ImmutableMessageOneofFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { + printer->Print(variables_, "if ($has_oneof_case_message$) {\n"); printer->Indent(); - PrintNestedBuilderCondition(printer, - "result.$oneof_name$_ = $oneof_name$_;\n", + PrintNestedBuilderCondition( + printer, "result.$oneof_name$_ = $oneof_name$_;\n", - "result.$oneof_name$_ = $name$Builder_.build();\n"); + "result.$oneof_name$_ = $name$Builder_.build();\n"); printer->Outdent(); printer->Print("}\n"); } -void ImmutableMessageOneofFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableMessageOneofFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { printer->Print(variables_, - "merge$capitalized_name$(other.get$capitalized_name$());\n"); + "merge$capitalized_name$(other.get$capitalized_name$());\n"); } -void ImmutableMessageOneofFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableMessageOneofFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "$type$.Builder subBuilder = null;\n" - "if ($has_oneof_case_message$) {\n" - " subBuilder = (($type$) $oneof_name$_).toBuilder();\n" - "}\n"); + "$type$.Builder subBuilder = null;\n" + "if ($has_oneof_case_message$) {\n" + " subBuilder = (($type$) $oneof_name$_).toBuilder();\n" + "}\n"); if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$oneof_name$_ = input.readGroup($number$, $type$.$get_parser$,\n" - " extensionRegistry);\n"); + printer->Print( + variables_, + "$oneof_name$_ = input.readGroup($number$, $type$.$get_parser$,\n" + " extensionRegistry);\n"); } else { - printer->Print(variables_, - "$oneof_name$_ =\n" - " input.readMessage($type$.$get_parser$, extensionRegistry);\n"); + printer->Print( + variables_, + "$oneof_name$_ =\n" + " input.readMessage($type$.$get_parser$, extensionRegistry);\n"); } printer->Print(variables_, - "if (subBuilder != null) {\n" - " subBuilder.mergeFrom(($type$) $oneof_name$_);\n" - " $oneof_name$_ = subBuilder.buildPartial();\n" - "}\n"); - printer->Print(variables_, - "$set_oneof_case_message$;\n"); + "if (subBuilder != null) {\n" + " subBuilder.mergeFrom(($type$) $oneof_name$_);\n" + " $oneof_name$_ = subBuilder.buildPartial();\n" + "}\n"); + printer->Print(variables_, "$set_oneof_case_message$;\n"); } -void ImmutableMessageOneofFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.write$group_or_message$($number$, ($type$) $oneof_name$_);\n" - "}\n"); +void ImmutableMessageOneofFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($has_oneof_case_message$) {\n" + " output.write$group_or_message$($number$, ($type$) $oneof_name$_);\n" + "}\n"); } -void ImmutableMessageOneofFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, ($type$) $oneof_name$_);\n" - "}\n"); +void ImmutableMessageOneofFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($has_oneof_case_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$group_or_message$Size($number$, ($type$) $oneof_name$_);\n" + "}\n"); } // =================================================================== -RepeatedImmutableMessageFieldGenerator:: -RepeatedImmutableMessageFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +RepeatedImmutableMessageFieldGenerator::RepeatedImmutableMessageFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetMessageVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); } RepeatedImmutableMessageFieldGenerator:: -~RepeatedImmutableMessageFieldGenerator() {} + ~RepeatedImmutableMessageFieldGenerator() {} int RepeatedImmutableMessageFieldGenerator::GetNumBitsForMessage() const { return 0; @@ -816,78 +812,79 @@ int RepeatedImmutableMessageFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void RepeatedImmutableMessageFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { // TODO(jonp): In the future, consider having methods specific to the // interface so that builders can choose dynamically to either return a // message or a nested builder, so that asking for the interface doesn't // cause a message to ever be built. WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<$type$> \n" - " get$capitalized_name$List();\n"); + "$deprecation$java.util.List<$type$> \n" + " get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List \n" - " get$capitalized_name$OrBuilderList();\n"); + "$deprecation$java.util.List \n" + " get$capitalized_name$OrBuilderList();\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder(\n" - " int index);\n"); + printer->Print( + variables_, + "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder(\n" + " int index);\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private java.util.List<$type$> $name$_;\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private java.util.List<$type$> $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List \n" - " ${$get$capitalized_name$OrBuilderList$}$() {\n" - " return $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List \n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$(\n" - " int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" + " int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - } void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderCondition( - io::Printer* printer, - const char* regular_case, + io::Printer* printer, const char* regular_case, const char* nested_builder_case) const { printer->Print(variables_, "if ($name$Builder_ == null) {\n"); printer->Indent(); @@ -901,10 +898,8 @@ void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderCondition( } void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderFunction( - io::Printer* printer, - const char* method_prototype, - const char* regular_case, - const char* nested_builder_case, + io::Printer* printer, const char* method_prototype, + const char* regular_case, const char* nested_builder_case, const char* trailing_code) const { printer->Print(variables_, method_prototype); printer->Annotate("{", "}", descriptor_); @@ -918,409 +913,432 @@ void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderFunction( printer->Print("}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // When using nested-builders, the code initially works just like the // non-nested builder case. It only creates a nested builder lazily on // demand and then forever delegates to it after creation. - printer->Print(variables_, - // Used when the builder is null. - // One field is the list and the other field keeps track of whether the - // list is immutable. If it's immutable, the invariant is that it must - // either an instance of Collections.emptyList() or it's an ArrayList - // wrapped in a Collections.unmodifiableList() wrapper and nobody else has - // a refererence to the underlying ArrayList. This invariant allows us to - // share instances of lists between protocol buffers avoiding expensive - // memory allocations. Note, immutable is a strong guarantee here -- not - // just that the list cannot be modified via the reference but that the - // list can never be modified. - "private java.util.List<$type$> $name$_ =\n" - " java.util.Collections.emptyList();\n" - - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$get_mutable_bit_builder$) {\n" - " $name$_ = new java.util.ArrayList<$type$>($name$_);\n" - " $set_mutable_bit_builder$;\n" - " }\n" - "}\n" - "\n"); + printer->Print( + variables_, + // Used when the builder is null. + // One field is the list and the other field keeps track of whether the + // list is immutable. If it's immutable, the invariant is that it must + // either an instance of Collections.emptyList() or it's an ArrayList + // wrapped in a Collections.unmodifiableList() wrapper and nobody else has + // a refererence to the underlying ArrayList. This invariant allows us to + // share instances of lists between protocol buffers avoiding expensive + // memory allocations. Note, immutable is a strong guarantee here -- not + // just that the list cannot be modified via the reference but that the + // list can never be modified. + "private java.util.List<$type$> $name$_ =\n" + " java.util.Collections.emptyList();\n" + + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$get_mutable_bit_builder$) {\n" + " $name$_ = new java.util.ArrayList<$type$>($name$_);\n" + " $set_mutable_bit_builder$;\n" + " }\n" + "}\n" + "\n"); - printer->Print(variables_, - // If this builder is non-null, it is used and the other fields are - // ignored. - "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;\n" - "\n"); + printer->Print( + variables_, + // If this builder is non-null, it is used and the other fields are + // ignored. + "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;\n" + "\n"); // The comments above the methods below are based on a hypothetical // repeated field of type "Field" called "RepeatedField". // List getRepeatedFieldList() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$()", + PrintNestedBuilderFunction( + printer, + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$()", - "return java.util.Collections.unmodifiableList($name$_);\n", - "return $name$Builder_.getMessageList();\n", + "return java.util.Collections.unmodifiableList($name$_);\n", + "return $name$Builder_.getMessageList();\n", - NULL); + NULL); // int getRepeatedFieldCount() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public int ${$get$capitalized_name$Count$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public int ${$get$capitalized_name$Count$}$()", - "return $name$_.size();\n", - "return $name$Builder_.getCount();\n", + "return $name$_.size();\n", "return $name$Builder_.getCount();\n", - NULL); + NULL); // Field getRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index)", - "return $name$_.get(index);\n", + "return $name$_.get(index);\n", - "return $name$Builder_.getMessage(index);\n", + "return $name$Builder_.getMessage(index);\n", - NULL); + NULL); // Builder setRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "ensure$capitalized_name$IsMutable();\n" - "$name$_.set(index, value);\n" - "$on_changed$\n", - "$name$Builder_.setMessage(index, value);\n", - "return this;\n"); + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value)", + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "ensure$capitalized_name$IsMutable();\n" + "$name$_.set(index, value);\n" + "$on_changed$\n", + "$name$Builder_.setMessage(index, value);\n", "return this;\n"); // Builder setRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$.Builder builderForValue)", - "ensure$capitalized_name$IsMutable();\n" - "$name$_.set(index, builderForValue.build());\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "$name$_.set(index, builderForValue.build());\n" + "$on_changed$\n", - "$name$Builder_.setMessage(index, builderForValue.build());\n", + "$name$Builder_.setMessage(index, builderForValue.build());\n", - "return this;\n"); + "return this;\n"); // Builder addRepeatedField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "ensure$capitalized_name$IsMutable();\n" - "$name$_.add(value);\n" + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "ensure$capitalized_name$IsMutable();\n" + "$name$_.add(value);\n" - "$on_changed$\n", + "$on_changed$\n", - "$name$Builder_.addMessage(value);\n", + "$name$Builder_.addMessage(value);\n", - "return this;\n"); + "return this;\n"); // Builder addRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " int index, $type$ value)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " int index, $type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "ensure$capitalized_name$IsMutable();\n" - "$name$_.add(index, value);\n" - "$on_changed$\n", + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "ensure$capitalized_name$IsMutable();\n" + "$name$_.add(index, value);\n" + "$on_changed$\n", - "$name$Builder_.addMessage(index, value);\n", + "$name$Builder_.addMessage(index, value);\n", - "return this;\n"); + "return this;\n"); // Builder addRepeatedField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " $type$.Builder builderForValue)", - "ensure$capitalized_name$IsMutable();\n" - "$name$_.add(builderForValue.build());\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "$name$_.add(builderForValue.build());\n" + "$on_changed$\n", - "$name$Builder_.addMessage(builderForValue.build());\n", + "$name$Builder_.addMessage(builderForValue.build());\n", - "return this;\n"); + "return this;\n"); // Builder addRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " int index, $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " int index, $type$.Builder builderForValue)", - "ensure$capitalized_name$IsMutable();\n" - "$name$_.add(index, builderForValue.build());\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "$name$_.add(index, builderForValue.build());\n" + "$on_changed$\n", - "$name$Builder_.addMessage(index, builderForValue.build());\n", + "$name$Builder_.addMessage(index, builderForValue.build());\n", - "return this;\n"); + "return this;\n"); // Builder addAllRepeatedField(Iterable values) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable values)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable values)", - "ensure$capitalized_name$IsMutable();\n" - "com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" - " values, $name$_);\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" + " values, $name$_);\n" + "$on_changed$\n", - "$name$Builder_.addAllMessages(values);\n", + "$name$Builder_.addAllMessages(values);\n", - "return this;\n"); + "return this;\n"); // Builder clearAllRepeatedField() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$clear$capitalized_name$$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public Builder ${$clear$capitalized_name$$}$()", - "$name$_ = java.util.Collections.emptyList();\n" - "$clear_mutable_bit_builder$;\n" - "$on_changed$\n", + "$name$_ = java.util.Collections.emptyList();\n" + "$clear_mutable_bit_builder$;\n" + "$on_changed$\n", - "$name$Builder_.clear();\n", + "$name$Builder_.clear();\n", - "return this;\n"); + "return this;\n"); // Builder removeRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$remove$capitalized_name$$}$(int index)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$remove$capitalized_name$$}$(int index)", - "ensure$capitalized_name$IsMutable();\n" - "$name$_.remove(index);\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "$name$_.remove(index);\n" + "$on_changed$\n", - "$name$Builder_.remove(index);\n", + "$name$Builder_.remove(index);\n", - "return this;\n"); + "return this;\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$.Builder ${$get$capitalized_name$Builder$}$(\n" - " int index) {\n" - " return get$capitalized_name$FieldBuilder().getBuilder(index);\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$.Builder ${$get$capitalized_name$Builder$}$(\n" + " int index) {\n" + " return get$capitalized_name$FieldBuilder().getBuilder(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$(\n" - " int index) {\n" - " if ($name$Builder_ == null) {\n" - " return $name$_.get(index);" - " } else {\n" - " return $name$Builder_.getMessageOrBuilder(index);\n" - " }\n" - "}\n"); + printer->Print(variables_, + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" + " int index) {\n" + " if ($name$Builder_ == null) {\n" + " return $name$_.get(index);" + " } else {\n" + " return $name$Builder_.getMessageOrBuilder(index);\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List \n" - " ${$get$capitalized_name$OrBuilderList$}$() {\n" - " if ($name$Builder_ != null) {\n" - " return $name$Builder_.getMessageOrBuilderList();\n" - " } else {\n" - " return java.util.Collections.unmodifiableList($name$_);\n" - " }\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List \n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" + " if ($name$Builder_ != null) {\n" + " return $name$Builder_.getMessageOrBuilderList();\n" + " } else {\n" + " return java.util.Collections.unmodifiableList($name$_);\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$.Builder " - "${$add$capitalized_name$Builder$}$() {\n" - " return get$capitalized_name$FieldBuilder().addBuilder(\n" - " $type$.getDefaultInstance());\n" - "}\n"); + printer->Print(variables_, + "$deprecation$public $type$.Builder " + "${$add$capitalized_name$Builder$}$() {\n" + " return get$capitalized_name$FieldBuilder().addBuilder(\n" + " $type$.getDefaultInstance());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$.Builder ${$add$capitalized_name$Builder$}$(\n" - " int index) {\n" - " return get$capitalized_name$FieldBuilder().addBuilder(\n" - " index, $type$.getDefaultInstance());\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$.Builder ${$add$capitalized_name$Builder$}$(\n" + " int index) {\n" + " return get$capitalized_name$FieldBuilder().addBuilder(\n" + " index, $type$.getDefaultInstance());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<$type$.Builder> \n" - " ${$get$capitalized_name$BuilderList$}$() {\n" - " return get$capitalized_name$FieldBuilder().getBuilderList();\n" - "}\n" - "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> \n" - " get$capitalized_name$FieldBuilder() {\n" - " if ($name$Builder_ == null) {\n" - " $name$Builder_ = new com.google.protobuf.RepeatedFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder>(\n" - " $name$_,\n" - " $get_mutable_bit_builder$,\n" - " getParentForChildren(),\n" - " isClean());\n" - " $name$_ = null;\n" - " }\n" - " return $name$Builder_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List<$type$.Builder> \n" + " ${$get$capitalized_name$BuilderList$}$() {\n" + " return get$capitalized_name$FieldBuilder().getBuilderList();\n" + "}\n" + "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> \n" + " get$capitalized_name$FieldBuilder() {\n" + " if ($name$Builder_ == null) {\n" + " $name$Builder_ = new " + "com.google.protobuf.RepeatedFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder>(\n" + " $name$_,\n" + " $get_mutable_bit_builder$,\n" + " getParentForChildren(),\n" + " isClean());\n" + " $name$_ = null;\n" + " }\n" + " return $name$Builder_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } void RepeatedImmutableMessageFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - printer->Print(variables_, - "get$capitalized_name$FieldBuilder();\n"); + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + printer->Print(variables_, "get$capitalized_name$FieldBuilder();\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = java.util.Collections.emptyList();\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { PrintNestedBuilderCondition(printer, - "$name$_ = java.util.Collections.emptyList();\n" - "$clear_mutable_bit_builder$;\n", + "$name$_ = java.util.Collections.emptyList();\n" + "$clear_mutable_bit_builder$;\n", - "$name$Builder_.clear();\n"); + "$name$Builder_.clear();\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // The code below does two optimizations (non-nested builder case): // 1. If the other list is empty, there's nothing to do. This ensures we // don't allocate a new array if we already have an immutable one. // 2. If the other list is non-empty and our current list is empty, we can // reuse the other list which is guaranteed to be immutable. - PrintNestedBuilderCondition(printer, - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$_.isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " } else {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addAll(other.$name$_);\n" - " }\n" - " $on_changed$\n" - "}\n", - - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$Builder_.isEmpty()) {\n" - " $name$Builder_.dispose();\n" - " $name$Builder_ = null;\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " $name$Builder_ = \n" - " com.google.protobuf.GeneratedMessage$ver$.alwaysUseFieldBuilders ?\n" - " get$capitalized_name$FieldBuilder() : null;\n" - " } else {\n" - " $name$Builder_.addAllMessages(other.$name$_);\n" - " }\n" - "}\n"); + PrintNestedBuilderCondition( + printer, + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$_.isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " } else {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addAll(other.$name$_);\n" + " }\n" + " $on_changed$\n" + "}\n", + + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$Builder_.isEmpty()) {\n" + " $name$Builder_.dispose();\n" + " $name$Builder_ = null;\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " $name$Builder_ = \n" + " com.google.protobuf.GeneratedMessage$ver$.alwaysUseFieldBuilders " + "?\n" + " get$capitalized_name$FieldBuilder() : null;\n" + " } else {\n" + " $name$Builder_.addAllMessages(other.$name$_);\n" + " }\n" + "}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { // The code below (non-nested builder case) ensures that the result has an // immutable list. If our list is immutable, we can just reuse it. If not, // we make it immutable. - PrintNestedBuilderCondition(printer, - "if ($get_mutable_bit_builder$) {\n" - " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" - " $clear_mutable_bit_builder$;\n" - "}\n" - "result.$name$_ = $name$_;\n", - - "result.$name$_ = $name$Builder_.build();\n"); + PrintNestedBuilderCondition( + printer, + "if ($get_mutable_bit_builder$) {\n" + " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" + " $clear_mutable_bit_builder$;\n" + "}\n" + "result.$name$_ = $name$_;\n", + + "result.$name$_ = $name$Builder_.build();\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = new java.util.ArrayList<$type$>();\n" - " $set_mutable_bit_parser$;\n" - "}\n"); + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = new java.util.ArrayList<$type$>();\n" + " $set_mutable_bit_parser$;\n" + "}\n"); if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$name$_.add(input.readGroup($number$, $type$.$get_parser$,\n" - " extensionRegistry));\n"); + printer->Print( + variables_, + "$name$_.add(input.readGroup($number$, $type$.$get_parser$,\n" + " extensionRegistry));\n"); } else { - printer->Print(variables_, - "$name$_.add(\n" - " input.readMessage($type$.$get_parser$, extensionRegistry));\n"); + printer->Print( + variables_, + "$name$_.add(\n" + " input.readMessage($type$.$get_parser$, extensionRegistry));\n"); } } -void RepeatedImmutableMessageFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($get_mutable_bit_parser$) {\n" - " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" - "}\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($get_mutable_bit_parser$) {\n" + " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" + "}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$group_or_message$($number$, $name$_.get(i));\n" - "}\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.write$group_or_message$($number$, $name$_.get(i));\n" + "}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, $name$_.get(i));\n" - "}\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { + printer->Print( + variables_, + "for (int i = 0; i < $name$_.size(); i++) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$group_or_message$Size($number$, $name$_.get(i));\n" + "}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (!get$capitalized_name$List()\n" + " .equals(other.get$capitalized_name$List())) return false;\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (get$capitalized_name$Count() > 0) {\n" + " hash = (37 * hash) + $constant_name$;\n" + " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" + "}\n"); } -string RepeatedImmutableMessageFieldGenerator::GetBoxedType() const { +std::string RepeatedImmutableMessageFieldGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } diff --git a/src/google/protobuf/compiler/java/java_message_field.h b/src/google/protobuf/compiler/java/java_message_field.h index d70d8270e2..36fa49208c 100644 --- a/src/google/protobuf/compiler/java/java_message_field.h +++ b/src/google/protobuf/compiler/java/java_message_field.h @@ -82,14 +82,11 @@ class ImmutableMessageFieldGenerator : public ImmutableFieldGenerator { void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - const int builderBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; void PrintNestedBuilderCondition(io::Printer* printer, @@ -150,14 +147,11 @@ class RepeatedImmutableMessageFieldGenerator : public ImmutableFieldGenerator { void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - const int builderBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; void PrintNestedBuilderCondition(io::Printer* printer, diff --git a/src/google/protobuf/compiler/java/java_message_field_lite.cc b/src/google/protobuf/compiler/java/java_message_field_lite.cc index 7bf6426e81..1a055fea12 100644 --- a/src/google/protobuf/compiler/java/java_message_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_field_lite.cc @@ -36,14 +36,15 @@ #include #include -#include #include #include +#include #include #include #include #include + namespace google { namespace protobuf { namespace compiler { @@ -51,12 +52,10 @@ namespace java { namespace { -void SetMessageVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - const FieldGeneratorInfo* info, +void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, + int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); (*variables)["type"] = @@ -64,12 +63,12 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["mutable_type"] = name_resolver->GetMutableClassName(descriptor->message_type()); (*variables)["group_or_message"] = - (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? - "Group" : "Message"; + (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? "Group" + : "Message"; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (SupportFieldPresence(descriptor->file())) { @@ -108,7 +107,6 @@ ImmutableMessageFieldLiteGenerator::ImmutableMessageFieldLiteGenerator( const FieldDescriptor* descriptor, int messageBitIndex, Context* context) : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - context_(context), name_resolver_(context->GetNameResolver()) { SetMessageVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), @@ -118,263 +116,190 @@ ImmutableMessageFieldLiteGenerator::ImmutableMessageFieldLiteGenerator( ImmutableMessageFieldLiteGenerator::~ImmutableMessageFieldLiteGenerator() {} int ImmutableMessageFieldLiteGenerator::GetNumBitsForMessage() const { - return 1; + return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } -void ImmutableMessageFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); +void ImmutableMessageFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print(variables_, "$deprecation$boolean has$capitalized_name$();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutableMessageFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMessageFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { - printer->Print(variables_, - "private $type$ $name$_;\n"); + printer->Print(variables_, "private $type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } else { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $name$_ != null;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $name$_ != null;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $name$_ = value;\n" - " $set_has_field_bit_message$\n" - " }\n"); + "private void set$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $name$_ = value;\n" + " $set_has_field_bit_message$\n" + " }\n"); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " $type$.Builder builderForValue) {\n" - " $name$_ = builderForValue.build();\n" - " $set_has_field_bit_message$\n" - "}\n"); + "private void set$capitalized_name$(\n" + " $type$.Builder builderForValue) {\n" + " $name$_ = builderForValue.build();\n" + " $set_has_field_bit_message$\n" + "}\n"); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.SuppressWarnings({\"ReferenceEquality\"})\n" - "private void merge$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " if ($name$_ != null &&\n" - " $name$_ != $type$.getDefaultInstance()) {\n" - " $name$_ =\n" - " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" - " } else {\n" - " $name$_ = value;\n" - " }\n" - " $set_has_field_bit_message$\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.SuppressWarnings({\"ReferenceEquality\"})\n" + "private void merge$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " if ($name$_ != null &&\n" + " $name$_ != $type$.getDefaultInstance()) {\n" + " $name$_ =\n" + " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" + " } else {\n" + " $name$_ = value;\n" + " }\n" + " $set_has_field_bit_message$\n" + "}\n"); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {" - " $name$_ = null;\n" - " $clear_has_field_bit_message$\n" - "}\n"); + "private void clear$capitalized_name$() {" + " $name$_ = null;\n" + " $clear_has_field_bit_message$\n" + "}\n"); } -void ImmutableMessageFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableMessageFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". // boolean hasField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field getField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - " }\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + " }\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$merge$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.merge$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$merge$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.merge$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableMessageFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + +void ImmutableMessageFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "get$capitalized_name$FieldBuilder();\n"); + WriteIntToUtf16CharSequence(messageBitIndex_, output); } + printer->Print(variables_, "\"$name$_\",\n"); } +void ImmutableMessageFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const {} -void ImmutableMessageFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const {} - -void ImmutableMessageFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_ = visitor.visitMessage($name$_, other.$name$_);\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - // noop for scalars -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - // TODO(dweis): Update this code to avoid the builder allocation and instead - // only allocate a submessage that isn't made immutable. Rely on the top - // message calling makeImmutable once done to actually traverse the tree and - // finalize state. This will avoid: - // - transitive builder allocations - // - the extra transitive iteration for streamed fields - // - reallocations for copying repeated fields - printer->Print(variables_, - "$type$.Builder subBuilder = null;\n" - "if ($is_field_present_message$) {\n" - " subBuilder = $name$_.toBuilder();\n" - "}\n"); - - if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$name$_ = input.readGroup($number$, $type$.parser(),\n" - " extensionRegistry);\n"); - } else { - printer->Print(variables_, - "$name$_ = input.readMessage($type$.parser(), extensionRegistry);\n"); - } - - printer->Print(variables_, - "if (subBuilder != null) {\n" - " subBuilder.mergeFrom($name$_);\n" - " $name$_ = subBuilder.buildPartial();\n" - "}\n" - "$set_has_field_bit_message$\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // noop for messages. -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.write$group_or_message$($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); -} - -string ImmutableMessageFieldLiteGenerator::GetBoxedType() const { +std::string ImmutableMessageFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } @@ -391,591 +316,488 @@ ImmutableMessageOneofFieldLiteGenerator:: } ImmutableMessageOneofFieldLiteGenerator:: -~ImmutableMessageOneofFieldLiteGenerator() {} + ~ImmutableMessageOneofFieldLiteGenerator() {} -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMessageOneofFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - " }\n" - " return $type$.getDefaultInstance();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + " }\n" + " return $type$.getDefaultInstance();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $oneof_name$_ = value;\n" - " $set_oneof_case_message$;\n" - "}\n"); + "private void set$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $oneof_name$_ = value;\n" + " $set_oneof_case_message$;\n" + "}\n"); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " $type$.Builder builderForValue) {\n" - " $oneof_name$_ = builderForValue.build();\n" - " $set_oneof_case_message$;\n" - "}\n"); + "private void set$capitalized_name$(\n" + " $type$.Builder builderForValue) {\n" + " $oneof_name$_ = builderForValue.build();\n" + " $set_oneof_case_message$;\n" + "}\n"); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void merge$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " if ($has_oneof_case_message$ &&\n" - " $oneof_name$_ != $type$.getDefaultInstance()) {\n" - " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n" - " .mergeFrom(value).buildPartial();\n" - " } else {\n" - " $oneof_name$_ = value;\n" - " }\n" - " $set_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "private void merge$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " if ($has_oneof_case_message$ &&\n" + " $oneof_name$_ != $type$.getDefaultInstance()) {\n" + " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n" + " .mergeFrom(value).buildPartial();\n" + " } else {\n" + " $oneof_name$_ = value;\n" + " }\n" + " $set_oneof_case_message$;\n" + "}\n"); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " }\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " }\n" + "}\n"); } +void ImmutableMessageOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output); + printer->Print(variables_, "$oneof_stored_type$.class,\n"); +} -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableMessageOneofFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". // boolean hasField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field getField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$merge$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.merge$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$merge$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.merge$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$oneof_name$_ = visitor.visitOneofMessage(\n" - " $has_oneof_case_message$,\n" - " $oneof_name$_,\n" - " other.$oneof_name$_);\n"); -} - -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print(variables_, - "$type$.Builder subBuilder = null;\n" - "if ($has_oneof_case_message$) {\n" - " subBuilder = (($type$) $oneof_name$_).toBuilder();\n" - "}\n"); - - if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$oneof_name$_ = input.readGroup($number$, $type$.parser(),\n" - " extensionRegistry);\n"); - } else { - printer->Print(variables_, - "$oneof_name$_ =\n" - " input.readMessage($type$.parser(), extensionRegistry);\n"); - } - - printer->Print(variables_, - "if (subBuilder != null) {\n" - " subBuilder.mergeFrom(($type$) $oneof_name$_);\n" - " $oneof_name$_ = subBuilder.buildPartial();\n" - "}\n"); - printer->Print(variables_, - "$set_oneof_case_message$;\n"); -} - -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.write$group_or_message$($number$, ($type$) $oneof_name$_);\n" - "}\n"); -} - -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, ($type$) $oneof_name$_);\n" - "}\n"); -} - // =================================================================== RepeatedImmutableMessageFieldLiteGenerator:: RepeatedImmutableMessageFieldLiteGenerator( const FieldDescriptor* descriptor, int messageBitIndex, Context* context) - : descriptor_(descriptor), - messageBitIndex_(messageBitIndex), - context_(context), - name_resolver_(context->GetNameResolver()) { + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetMessageVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); } RepeatedImmutableMessageFieldLiteGenerator:: -~RepeatedImmutableMessageFieldLiteGenerator() {} + ~RepeatedImmutableMessageFieldLiteGenerator() {} int RepeatedImmutableMessageFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutableMessageFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { // TODO(jonp): In the future, consider having methods specific to the // interface so that builders can choose dynamically to either return a // message or a nested builder, so that asking for the interface doesn't // cause a message to ever be built. WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<$type$> \n" - " get$capitalized_name$List();\n"); + "$deprecation$java.util.List<$type$> \n" + " get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private com.google.protobuf.Internal.ProtobufList<$type$> $name$_;\n"); +void RepeatedImmutableMessageFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print( + variables_, + "private com.google.protobuf.Internal.ProtobufList<$type$> $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List \n" - " ${$get$capitalized_name$OrBuilderList$}$() {\n" - " return $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List \n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$(\n" - " int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" + " int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - " }\n" - "}\n" - "\n"); + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$is_mutable$) {\n" + " $name$_ =\n" + " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" + " }\n" + "}\n" + "\n"); // Builder setRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, $type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value);\n" - "}\n"); + "private void set$capitalized_name$(\n" + " int index, $type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value);\n" + "}\n"); // Builder setRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, $type$.Builder builderForValue) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, builderForValue.build());\n" - "}\n"); + "private void set$capitalized_name$(\n" + " int index, $type$.Builder builderForValue) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, builderForValue.build());\n" + "}\n"); // Builder addRepeatedField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - "}\n"); + "private void add$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + "}\n"); // Builder addRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$(\n" - " int index, $type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(index, value);\n" - "}\n"); + "private void add$capitalized_name$(\n" + " int index, $type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(index, value);\n" + "}\n"); // Builder addRepeatedField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$(\n" - " $type$.Builder builderForValue) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(builderForValue.build());\n" - "}\n"); + "private void add$capitalized_name$(\n" + " $type$.Builder builderForValue) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(builderForValue.build());\n" + "}\n"); // Builder addRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$(\n" - " int index, $type$.Builder builderForValue) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(index, builderForValue.build());\n" - "}\n"); + "private void add$capitalized_name$(\n" + " int index, $type$.Builder builderForValue) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(index, builderForValue.build());\n" + "}\n"); // Builder addAllRepeatedField(Iterable values) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void addAll$capitalized_name$(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.addAll(\n" - " values, $name$_);\n" - "}\n"); + "private void addAll$capitalized_name$(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.addAll(\n" + " values, $name$_);\n" + "}\n"); // Builder clearAllRepeatedField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $name$_ = emptyProtobufList();\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " $name$_ = emptyProtobufList();\n" + "}\n"); // Builder removeRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void remove$capitalized_name$(int index) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.remove(index);\n" - "}\n"); + "private void remove$capitalized_name$(int index) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.remove(index);\n" + "}\n"); } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutableMessageFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // The comments above the methods below are based on a hypothetical // repeated field of type "Field" called "RepeatedField". // List getRepeatedFieldList() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return java.util.Collections.unmodifiableList(\n" - " instance.get$capitalized_name$List());\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return java.util.Collections.unmodifiableList(\n" + " instance.get$capitalized_name$List());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // int getRepeatedFieldCount() WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Count();\n" - "}"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Count();\n" + "}"); printer->Annotate("{", "}", descriptor_); // Field getRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return instance.get$capitalized_name$(index);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return instance.get$capitalized_name$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder setRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder setRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " int index, $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(index, builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " int index, $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(index, builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addAllRepeatedField(Iterable values) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$(values);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder clearAllRepeatedField() WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder removeRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$remove$capitalized_name$$}$(int index) {\n" - " copyOnWrite();\n" - " instance.remove$capitalized_name$(index);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$remove$capitalized_name$$}$(int index) {\n" + " copyOnWrite();\n" + " instance.remove$capitalized_name$(index);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - printer->Print(variables_, - "get$capitalized_name$FieldBuilder();\n"); -} - - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_ = emptyProtobufList();\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_= visitor.visitList($name$_, other.$name$_);\n"); -} -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); printer->Print(variables_, - "$name$_.makeImmutable();\n"); + "\"$name$_\",\n" + "$type$.class,\n"); } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n"); - - if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$name$_.add(input.readGroup($number$, $type$.parser(),\n" - " extensionRegistry));\n"); - } else { - printer->Print(variables_, - "$name$_.add(\n" - " input.readMessage($type$.parser(), extensionRegistry));\n"); - } -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_mutable$) {\n" - " $name$_.makeImmutable();\n" - "}\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$group_or_message$($number$, $name$_.get(i));\n" - "}\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, $name$_.get(i));\n" - "}\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutableMessageFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { + printer->Print(variables_, "$name$_ = emptyProtobufList();\n"); } -string RepeatedImmutableMessageFieldLiteGenerator::GetBoxedType() const { +std::string RepeatedImmutableMessageFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } diff --git a/src/google/protobuf/compiler/java/java_message_field_lite.h b/src/google/protobuf/compiler/java/java_message_field_lite.h index c9eb30b836..c0a9b37f4f 100644 --- a/src/google/protobuf/compiler/java/java_message_field_lite.h +++ b/src/google/protobuf/compiler/java/java_message_field_lite.h @@ -69,23 +69,15 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - - string GetBoxedType() const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; + + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; + std::map variables_; const int messageBitIndex_; - Context* context_; ClassNameResolver* name_resolver_; private: @@ -102,11 +94,8 @@ class ImmutableMessageOneofFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageOneofFieldLiteGenerator); @@ -125,24 +114,14 @@ class RepeatedImmutableMessageFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; - - string GetBoxedType() const; + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; private: diff --git a/src/google/protobuf/compiler/java/java_message_lite.cc b/src/google/protobuf/compiler/java/java_message_lite.cc index 85a7453d17..c356f15310 100644 --- a/src/google/protobuf/compiler/java/java_message_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_lite.cc @@ -56,6 +56,7 @@ #include + namespace google { namespace protobuf { namespace compiler { @@ -64,34 +65,13 @@ namespace java { using internal::WireFormat; using internal::WireFormatLite; -namespace { -bool EnableExperimentalRuntimeForLite() { -#ifdef PROTOBUF_EXPERIMENT - return PROTOBUF_EXPERIMENT; -#else // PROTOBUF_EXPERIMENT - return false; -#endif // !PROTOBUF_EXPERIMENT -} - -bool GenerateHasBits(const Descriptor* descriptor) { - return SupportFieldPresence(descriptor->file()) || - HasRepeatedFields(descriptor); -} - -string MapValueImmutableClassdName(const Descriptor* descriptor, - ClassNameResolver* name_resolver) { - const FieldDescriptor* value_field = descriptor->FindFieldByName("value"); - GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, value_field->type()); - return name_resolver->GetImmutableClassName(value_field->message_type()); -} -} // namespace - // =================================================================== ImmutableMessageLiteGenerator::ImmutableMessageLiteGenerator( const Descriptor* descriptor, Context* context) - : MessageGenerator(descriptor), context_(context), - name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + : MessageGenerator(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()), + field_generators_(descriptor, context_) { GOOGLE_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A lite message generator is used to " "generate non-lite messages."; @@ -134,41 +114,39 @@ void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) { " com.google.protobuf.GeneratedMessageLite.\n" " ExtendableMessageOrBuilder<\n" " $classname$, $classname$.Builder> {\n", - "deprecation", descriptor_->options().deprecated() ? - "@java.lang.Deprecated " : "", + "deprecation", + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), - "classname", descriptor_->name(), - "{", "", "}", ""); + "classname", descriptor_->name(), "{", "", "}", ""); } else { printer->Print( "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n" " $extra_interfaces$\n" " com.google.protobuf.MessageLiteOrBuilder {\n", - "deprecation", descriptor_->options().deprecated() ? - "@java.lang.Deprecated " : "", + "deprecation", + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), - "classname", descriptor_->name(), - "{", "", "}", ""); + "classname", descriptor_->name(), "{", "", "}", ""); } printer->Annotate("{", "}", descriptor_); printer->Indent(); - for (int i = 0; i < descriptor_->field_count(); i++) { - printer->Print("\n"); - field_generators_.get(descriptor_->field(i)) - .GenerateInterfaceMembers(printer); - } - for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - printer->Print( - "\n" - "public $classname$.$oneof_capitalized_name$Case " - "get$oneof_capitalized_name$Case();\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name, - "classname", - context_->GetNameResolver()->GetImmutableClassName(descriptor_)); - } + for (int i = 0; i < descriptor_->field_count(); i++) { + printer->Print("\n"); + field_generators_.get(descriptor_->field(i)) + .GenerateInterfaceMembers(printer); + } + for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { + printer->Print( + "\n" + "public $classname$.$oneof_capitalized_name$Case " + "get$oneof_capitalized_name$Case();\n", + "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name, + "classname", + context_->GetNameResolver()->GetImmutableClassName(descriptor_)); + } printer->Outdent(); printer->Print("}\n"); @@ -179,12 +157,12 @@ void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) { void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { bool is_own_file = IsOwnFile(descriptor_, /* immutable = */ true); - std::map variables; + std::map variables; variables["static"] = is_own_file ? " " : " static "; variables["classname"] = descriptor_->name(); variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_); - variables["deprecation"] = descriptor_->options().deprecated() - ? "@java.lang.Deprecated " : ""; + variables["deprecation"] = + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""; WriteMessageDocComment(printer, descriptor_); MaybePrintGeneratedAnnotation(context_, printer, descriptor_, @@ -192,19 +170,21 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { // The builder_type stores the super type name of the nested Builder class. - string builder_type; + std::string builder_type; if (descriptor_->extension_range_count() > 0) { - printer->Print(variables, - "$deprecation$public $static$final class $classname$ extends\n" - " com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n" - " $classname$, $classname$.Builder> implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n"); + printer->Print( + variables, + "$deprecation$public $static$final class $classname$ extends\n" + " com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n" + " $classname$, $classname$.Builder> implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n"); builder_type = strings::Substitute( "com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<$0, ?>", name_resolver_->GetImmutableClassName(descriptor_)); } else { - printer->Print(variables, + printer->Print( + variables, "$deprecation$public $static$final class $classname$ extends\n" " com.google.protobuf.GeneratedMessageLite<\n" " $classname$, $classname$.Builder> implements\n" @@ -226,114 +206,109 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->nested_type_count(); i++) { // Don't generate Java classes for map entry messages. if (IsMapEntry(descriptor_->nested_type(i))) continue; - ImmutableMessageLiteGenerator messageGenerator( - descriptor_->nested_type(i), context_); + ImmutableMessageLiteGenerator messageGenerator(descriptor_->nested_type(i), + context_); messageGenerator.GenerateInterface(printer); messageGenerator.Generate(printer); } - if (GenerateHasBits(descriptor_)) { - // Integers for bit fields. - int totalBits = 0; - for (int i = 0; i < descriptor_->field_count(); i++) { - totalBits += field_generators_.get(descriptor_->field(i)) - .GetNumBitsForMessage(); - } - int totalInts = (totalBits + 31) / 32; - for (int i = 0; i < totalInts; i++) { - printer->Print("private int $bit_field_name$;\n", - "bit_field_name", GetBitFieldName(i)); - } + // Integers for bit fields. + int totalBits = 0; + for (int i = 0; i < descriptor_->field_count(); i++) { + totalBits += + field_generators_.get(descriptor_->field(i)).GetNumBitsForMessage(); + } + int totalInts = (totalBits + 31) / 32; + for (int i = 0; i < totalInts; i++) { + printer->Print("private int $bit_field_name$;\n", "bit_field_name", + GetBitFieldName(i)); } // oneof - std::map vars; + std::map vars; for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { const OneofDescriptor* oneof = descriptor_->oneof_decl(i); vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name; - vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( - oneof)->capitalized_name; - vars["oneof_index"] = SimpleItoa(oneof->index()); + vars["oneof_capitalized_name"] = + context_->GetOneofGeneratorInfo(oneof)->capitalized_name; + vars["oneof_index"] = StrCat(oneof->index()); // oneofCase_ and oneof_ printer->Print(vars, - "private int $oneof_name$Case_ = 0;\n" - "private java.lang.Object $oneof_name$_;\n"); + "private int $oneof_name$Case_ = 0;\n" + "private java.lang.Object $oneof_name$_;\n"); // OneofCase enum printer->Print(vars, - "public enum $oneof_capitalized_name$Case\n" - " implements com.google.protobuf.Internal.EnumLite {\n"); + "public enum $oneof_capitalized_name$Case\n" + " implements com.google.protobuf.Internal.EnumLite {\n"); printer->Indent(); for (int j = 0; j < oneof->field_count(); j++) { const FieldDescriptor* field = oneof->field(j); printer->Print("$field_name$($field_number$),\n", "field_name", ToUpper(field->name()), "field_number", - SimpleItoa(field->number())); + StrCat(field->number())); } - printer->Print( - "$cap_oneof_name$_NOT_SET(0);\n", - "cap_oneof_name", - ToUpper(vars["oneof_name"])); + printer->Print("$cap_oneof_name$_NOT_SET(0);\n", "cap_oneof_name", + ToUpper(vars["oneof_name"])); printer->Print(vars, - "private final int value;\n" - "private $oneof_capitalized_name$Case(int value) {\n" - " this.value = value;\n" - "}\n"); - printer->Print(vars, - "/**\n" - " * @deprecated Use {@link #forNumber(int)} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public static $oneof_capitalized_name$Case valueOf(int value) {\n" - " return forNumber(value);\n" - "}\n" - "\n" - "public static $oneof_capitalized_name$Case forNumber(int value) {\n" - " switch (value) {\n"); + "private final int value;\n" + "private $oneof_capitalized_name$Case(int value) {\n" + " this.value = value;\n" + "}\n"); + printer->Print( + vars, + "/**\n" + " * @deprecated Use {@link #forNumber(int)} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public static $oneof_capitalized_name$Case valueOf(int value) {\n" + " return forNumber(value);\n" + "}\n" + "\n" + "public static $oneof_capitalized_name$Case forNumber(int value) {\n" + " switch (value) {\n"); for (int j = 0; j < oneof->field_count(); j++) { const FieldDescriptor* field = oneof->field(j); printer->Print(" case $field_number$: return $field_name$;\n", - "field_number", SimpleItoa(field->number()), + "field_number", StrCat(field->number()), "field_name", ToUpper(field->name())); } printer->Print( - " case 0: return $cap_oneof_name$_NOT_SET;\n" - " default: return null;\n" - " }\n" - "}\n" - "@java.lang.Override\n" - "public int getNumber() {\n" - " return this.value;\n" - "}\n", - "cap_oneof_name", ToUpper(vars["oneof_name"])); + " case 0: return $cap_oneof_name$_NOT_SET;\n" + " default: return null;\n" + " }\n" + "}\n" + "@java.lang.Override\n" + "public int getNumber() {\n" + " return this.value;\n" + "}\n", + "cap_oneof_name", ToUpper(vars["oneof_name"])); printer->Outdent(); printer->Print("};\n\n"); // oneofCase() printer->Print(vars, - "@java.lang.Override\n" - "public $oneof_capitalized_name$Case\n" - "get$oneof_capitalized_name$Case() {\n" - " return $oneof_capitalized_name$Case.forNumber(\n" - " $oneof_name$Case_);\n" - "}\n" - "\n" - "private void clear$oneof_capitalized_name$() {\n" - " $oneof_name$Case_ = 0;\n" - " $oneof_name$_ = null;\n" - "}\n" - "\n"); + "@java.lang.Override\n" + "public $oneof_capitalized_name$Case\n" + "get$oneof_capitalized_name$Case() {\n" + " return $oneof_capitalized_name$Case.forNumber(\n" + " $oneof_name$Case_);\n" + "}\n" + "\n" + "private void clear$oneof_capitalized_name$() {\n" + " $oneof_name$Case_ = 0;\n" + " $oneof_name$_ = null;\n" + "}\n" + "\n"); } // Fields for (int i = 0; i < descriptor_->field_count(); i++) { printer->Print("public static final int $constant_name$ = $number$;\n", "constant_name", FieldConstantName(descriptor_->field(i)), - "number", - SimpleItoa(descriptor_->field(i)->number())); + "number", StrCat(descriptor_->field(i)->number())); field_generators_.get(descriptor_->field(i)).GenerateMembers(printer); printer->Print("\n"); } - GenerateMessageSerializationMethods(printer); GenerateParseFromMethods(printer); GenerateBuilder(printer); @@ -341,8 +316,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { // Memoizes whether the protocol buffer is fully initialized (has all // required fields). 0 means false, 1 means true, and all other values // mean not yet computed. - printer->Print( - "private byte memoizedIsInitialized = 2;\n"); + printer->Print("private byte memoizedIsInitialized = 2;\n"); } printer->Print( @@ -360,74 +334,48 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { printer->Indent(); printer->Indent(); - printer->Print( - "case NEW_BUILDER: {\n"); + printer->Print("case NEW_BUILDER: {\n"); printer->Indent(); GenerateDynamicMethodNewBuilder(printer); printer->Outdent(); - if (!EnableExperimentalRuntimeForLite()) { - printer->Print( + printer->Print( "}\n" - "case IS_INITIALIZED: {\n"); - printer->Indent(); - GenerateDynamicMethodIsInitialized(printer); - printer->Outdent(); - - printer->Print("}\n"); - - printer->Print( - "case MAKE_IMMUTABLE: {\n"); - - printer->Indent(); - GenerateDynamicMethodMakeImmutable(printer); - printer->Outdent(); - - printer->Print( - "}\n" - "case VISIT: {\n"); - - printer->Indent(); - GenerateDynamicMethodVisit(printer); - printer->Outdent(); - - printer->Print( - "}\n" - "case MERGE_FROM_STREAM: {\n"); - - printer->Indent(); - GenerateDynamicMethodMergeFromStream(printer); - printer->Outdent(); - } + "case BUILD_MESSAGE_INFO: {\n"); + printer->Indent(); + GenerateDynamicMethodNewBuildMessageInfo(printer); + printer->Outdent(); printer->Print( - "}\n" - "// fall through\n" - "case GET_DEFAULT_INSTANCE: {\n" - " return DEFAULT_INSTANCE;\n" - "}\n" - "case GET_PARSER: {\n" - // Generally one would use the lazy initialization holder pattern for - // manipulating static fields but that has exceptional cost on Android as - // it will generate an extra class for every message. Instead, use the - // double-check locking pattern which works just as well. - // - // The "parser" temporary mirrors the "PARSER" field to eliminate a read - // at the final return statement. - " com.google.protobuf.Parser<$classname$> parser = PARSER;\n" - " if (parser == null) {\n" - " synchronized ($classname$.class) {\n" - " parser = PARSER;\n" - " if (parser == null) {\n" - " parser = new DefaultInstanceBasedParser(DEFAULT_INSTANCE);\n" - " PARSER = parser;\n" - " }\n" - " }\n" - " }\n" - " return parser;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "}\n" + "// fall through\n" + "case GET_DEFAULT_INSTANCE: {\n" + " return DEFAULT_INSTANCE;\n" + "}\n" + "case GET_PARSER: {\n" + // Generally one would use the lazy initialization holder pattern for + // manipulating static fields but that has exceptional cost on Android as + // it will generate an extra class for every message. Instead, use the + // double-check locking pattern which works just as well. + // + // The "parser" temporary mirrors the "PARSER" field to eliminate a read + // at the final return statement. + " com.google.protobuf.Parser<$classname$> parser = PARSER;\n" + " if (parser == null) {\n" + " synchronized ($classname$.class) {\n" + " parser = PARSER;\n" + " if (parser == null) {\n" + " parser =\n" + " new DefaultInstanceBasedParser<$classname$>(\n" + " DEFAULT_INSTANCE);\n" + " PARSER = parser;\n" + " }\n" + " }\n" + " }\n" + " return parser;\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Outdent(); @@ -454,43 +402,37 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { printer->Outdent(); printer->Print( - " }\n" - " throw new UnsupportedOperationException();\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + " }\n" + " throw new UnsupportedOperationException();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "\n" - "// @@protoc_insertion_point(class_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); - + "\n" + "// @@protoc_insertion_point(class_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); // Carefully initialize the default instance in such a way that it doesn't // conflict with other initialization. - printer->Print( - "private static final $classname$ DEFAULT_INSTANCE;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - - printer->Print( - "static {\n" - " // New instances are implicitly immutable so no need to make\n" - " // immutable.\n" - " DEFAULT_INSTANCE = new $classname$();\n" - "}\n" - "\n", - "classname", descriptor_->name()); - if (EnableExperimentalRuntimeForLite()) { - // Register the default instance in a map. This map will be used by - // experimental runtime to lookup default instance given a class instance - // without using Java reflection. - printer->Print( - "static {\n" - " com.google.protobuf.GeneratedMessageLite.registerDefaultInstance(\n" - " $classname$.class, DEFAULT_INSTANCE);\n" - "}\n", - "classname", descriptor_->name()); - } + printer->Print("private static final $classname$ DEFAULT_INSTANCE;\n", + "classname", + name_resolver_->GetImmutableClassName(descriptor_)); + + printer->Print( + "static {\n" + " $classname$ defaultInstance = new $classname$();\n" + " // New instances are implicitly immutable so no need to make\n" + " // immutable.\n" + " DEFAULT_INSTANCE = defaultInstance;\n" + // Register the default instance in a map. This map will be used by + // experimental runtime to lookup default instance given a class instance + // without using Java reflection. + " com.google.protobuf.GeneratedMessageLite.registerDefaultInstance(\n" + " $classname$.class, defaultInstance);\n" + "}\n" + "\n", + "classname", descriptor_->name()); printer->Print( "public static $classname$ getDefaultInstance() {\n" @@ -520,230 +462,208 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { .Generate(printer); } - printer->Outdent(); printer->Print("}\n\n"); } +void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo( + io::Printer* printer) { + printer->Indent(); -// =================================================================== + // Collect field info into a sequence of UTF-16 chars. It will be embedded + // as a Java string in the generated code. + std::vector chars; -void ImmutableMessageLiteGenerator:: -GenerateMessageSerializationMethods(io::Printer* printer) { - if (EnableExperimentalRuntimeForLite()) { - return; + int flags = 0; + if (SupportFieldPresence(descriptor_->file())) { + flags |= 0x1; } - - std::unique_ptr sorted_fields( - SortFieldsByNumber(descriptor_)); - - std::vector sorted_extensions; - for (int i = 0; i < descriptor_->extension_range_count(); ++i) { - sorted_extensions.push_back(descriptor_->extension_range(i)); + if (descriptor_->options().message_set_wire_format()) { + flags |= 0x2; } - std::sort(sorted_extensions.begin(), sorted_extensions.end(), - ExtensionRangeOrdering()); + WriteIntToUtf16CharSequence(flags, &chars); + WriteIntToUtf16CharSequence(descriptor_->field_count(), &chars); - printer->Print( - "@java.lang.Override\n" - "public void writeTo(com.google.protobuf.CodedOutputStream output)\n" - " throws java.io.IOException {\n"); - printer->Indent(); - if (HasPackedFields(descriptor_)) { - // writeTo(CodedOutputStream output) might be invoked without - // getSerializedSize() ever being called, but we need the memoized - // sizes in case this message has packed fields. Rather than emit checks - // for each packed field, just call getSerializedSize() up front. In most - // cases, getSerializedSize() will have already been called anyway by one - // of the wrapper writeTo() methods, making this call cheap. - printer->Print("getSerializedSize();\n"); - } + if (descriptor_->field_count() == 0) { + printer->Print("java.lang.Object[] objects = null;"); + } else { + // A single array of all fields (including oneof, oneofCase, hasBits). + printer->Print("java.lang.Object[] objects = new java.lang.Object[] {\n"); + printer->Indent(); - if (descriptor_->extension_range_count() > 0) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "com.google.protobuf.GeneratedMessageLite\n" - " .ExtendableMessage<$classname$, $classname$.Builder>\n" - " .ExtensionWriter extensionWriter =\n" - " newMessageSetExtensionWriter();\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - } else { + // Record the number of oneofs. + WriteIntToUtf16CharSequence(descriptor_->oneof_decl_count(), &chars); + for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { + const OneofDescriptor* oneof = descriptor_->oneof_decl(i); printer->Print( - "com.google.protobuf.GeneratedMessageLite\n" - " .ExtendableMessage<$classname$, $classname$.Builder>\n" - " .ExtensionWriter extensionWriter =\n" - " newExtensionWriter();\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "\"$oneof_name$_\",\n" + "\"$oneof_name$Case_\",\n", + "oneof_name", context_->GetOneofGeneratorInfo(oneof)->name); } - } - // Merge the fields and the extension ranges, both sorted by field number. - for (int i = 0, j = 0; - i < descriptor_->field_count() || j < sorted_extensions.size();) { - if (i == descriptor_->field_count()) { - GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); - } else if (j == sorted_extensions.size()) { - GenerateSerializeOneField(printer, sorted_fields[i++]); - } else if (sorted_fields[i]->number() < sorted_extensions[j]->start) { - GenerateSerializeOneField(printer, sorted_fields[i++]); - } else { - GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); + // Integers for bit fields. + int total_bits = 0; + for (int i = 0; i < descriptor_->field_count(); i++) { + total_bits += + field_generators_.get(descriptor_->field(i)).GetNumBitsForMessage(); } - } + int total_ints = (total_bits + 31) / 32; + for (int i = 0; i < total_ints; i++) { + printer->Print("\"$bit_field_name$\",\n", "bit_field_name", + GetBitFieldName(i)); + } + WriteIntToUtf16CharSequence(total_ints, &chars); - if (descriptor_->options().message_set_wire_format()) { - printer->Print("unknownFields.writeAsMessageSetTo(output);\n"); - } else { - printer->Print("unknownFields.writeTo(output);\n"); - } + int map_count = 0; + int repeated_count = 0; + std::unique_ptr sorted_fields( + SortFieldsByNumber(descriptor_)); + for (int i = 0; i < descriptor_->field_count(); i++) { + const FieldDescriptor* field = sorted_fields[i]; + if (field->is_map()) { + map_count++; + } else if (field->is_repeated()) { + repeated_count++; + } + } - printer->Outdent(); - printer->Print( - "}\n" - "\n" - "@java.lang.Override\n" - "public int getSerializedSize() {\n" - " int size = memoizedSerializedSize;\n" - " if (size != -1) return size;\n" - "\n"); - printer->Indent(); - printer->Print( - "size = 0;\n"); + WriteIntToUtf16CharSequence(sorted_fields[0]->number(), &chars); + WriteIntToUtf16CharSequence( + sorted_fields[descriptor_->field_count() - 1]->number(), &chars); + WriteIntToUtf16CharSequence(descriptor_->field_count(), &chars); + WriteIntToUtf16CharSequence(map_count, &chars); + WriteIntToUtf16CharSequence(repeated_count, &chars); - for (int i = 0; i < descriptor_->field_count(); i++) { - field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer); - } + std::vector fields_for_is_initialized_check; + for (int i = 0; i < descriptor_->field_count(); i++) { + if (descriptor_->field(i)->is_required() || + (GetJavaType(descriptor_->field(i)) == JAVATYPE_MESSAGE && + HasRequiredFields(descriptor_->field(i)->message_type()))) { + fields_for_is_initialized_check.push_back(descriptor_->field(i)); + } + } + WriteIntToUtf16CharSequence(fields_for_is_initialized_check.size(), &chars); - if (descriptor_->extension_range_count() > 0) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print("size += extensionsSerializedSizeAsMessageSet();\n"); - } else { - printer->Print("size += extensionsSerializedSize();\n"); + for (int i = 0; i < descriptor_->field_count(); i++) { + const FieldDescriptor* field = sorted_fields[i]; + field_generators_.get(field).GenerateFieldInfo(printer, &chars); } + printer->Outdent(); + printer->Print("};\n"); } - if (descriptor_->options().message_set_wire_format()) { - printer->Print("size += unknownFields.getSerializedSizeAsMessageSet();\n"); - } else { - printer->Print("size += unknownFields.getSerializedSize();\n"); + printer->Print("java.lang.String info =\n"); + std::string line; + for (size_t i = 0; i < chars.size(); i++) { + uint16 code = chars[i]; + EscapeUtf16ToString(code, &line); + if (line.size() >= 80) { + printer->Print(" \"$string$\" +\n", "string", line); + line.clear(); + } } + printer->Print(" \"$string$\";\n", "string", line); - printer->Print( - "memoizedSerializedSize = size;\n" - "return size;\n"); - + printer->Print("return newMessageInfo(DEFAULT_INSTANCE, info, objects);\n"); printer->Outdent(); - printer->Print( - "}\n" - "\n"); -} - -void ImmutableMessageLiteGenerator:: -GenerateParseFromMethods(io::Printer* printer) { - // Note: These are separate from GenerateMessageSerializationMethods() - // because they need to be generated even for messages that are optimized - // for code size. - printer->Print( - "public static $classname$ parseFrom(\n" - " java.nio.ByteBuffer data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " java.nio.ByteBuffer data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.ByteString data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.ByteString data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(byte[] data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " byte[] data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(java.io.InputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, input);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " java.io.InputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, input, extensionRegistry);\n" - "}\n" - "public static $classname$ parseDelimitedFrom(java.io.InputStream input)\n" - " throws java.io.IOException {\n" - " return parseDelimitedFrom(DEFAULT_INSTANCE, input);\n" - "}\n" - "public static $classname$ parseDelimitedFrom(\n" - " java.io.InputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return parseDelimitedFrom(DEFAULT_INSTANCE, input, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.CodedInputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, input);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.CodedInputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, input, extensionRegistry);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); } -void ImmutableMessageLiteGenerator::GenerateSerializeOneField( - io::Printer* printer, const FieldDescriptor* field) { - field_generators_.get(field).GenerateSerializationCode(printer); -} +// =================================================================== -void ImmutableMessageLiteGenerator::GenerateSerializeOneExtensionRange( - io::Printer* printer, const Descriptor::ExtensionRange* range) { - printer->Print("extensionWriter.writeUntil($end$, output);\n", "end", - SimpleItoa(range->end)); +void ImmutableMessageLiteGenerator::GenerateParseFromMethods( + io::Printer* printer) { + printer->Print( + "public static $classname$ parseFrom(\n" + " java.nio.ByteBuffer data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " java.nio.ByteBuffer data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.ByteString data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.ByteString data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(byte[] data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " byte[] data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(java.io.InputStream input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, input);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " java.io.InputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, input, extensionRegistry);\n" + "}\n" + "public static $classname$ parseDelimitedFrom(java.io.InputStream " + "input)\n" + " throws java.io.IOException {\n" + " return parseDelimitedFrom(DEFAULT_INSTANCE, input);\n" + "}\n" + "public static $classname$ parseDelimitedFrom(\n" + " java.io.InputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return parseDelimitedFrom(DEFAULT_INSTANCE, input, " + "extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.CodedInputStream input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, input);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.CodedInputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, input, extensionRegistry);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } // =================================================================== void ImmutableMessageLiteGenerator::GenerateBuilder(io::Printer* printer) { printer->Print( - "public static Builder newBuilder() {\n" - " return (Builder) DEFAULT_INSTANCE.createBuilder();\n" - "}\n" - "public static Builder newBuilder($classname$ prototype) {\n" - " return (Builder) DEFAULT_INSTANCE.createBuilder(prototype);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public static Builder newBuilder() {\n" + " return (Builder) DEFAULT_INSTANCE.createBuilder();\n" + "}\n" + "public static Builder newBuilder($classname$ prototype) {\n" + " return (Builder) DEFAULT_INSTANCE.createBuilder(prototype);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); MessageBuilderLiteGenerator builderGenerator(descriptor_, context_); builderGenerator.Generate(printer); @@ -751,386 +671,43 @@ void ImmutableMessageLiteGenerator::GenerateBuilder(io::Printer* printer) { // =================================================================== -void ImmutableMessageLiteGenerator::GenerateDynamicMethodIsInitialized( - io::Printer* printer) { - // Returns null for false, DEFAULT_INSTANCE for true. - if (!HasRequiredFields(descriptor_)) { - printer->Print("return DEFAULT_INSTANCE;\n"); - return; - } - - // TODO(xiaofeng): Remove this when b/64445758 is fixed. We don't need to - // check memoizedIsInitialized here because the caller does that already, - // but right now proguard proto shrinker asserts on the bytecode layout of - // this code so it can't be removed until proguard is updated. - printer->Print( - "byte isInitialized = memoizedIsInitialized;\n" - "if (isInitialized == 1) return DEFAULT_INSTANCE;\n" - "if (isInitialized == 0) return null;\n" - "\n" - "boolean shouldMemoize = ((Boolean) arg0).booleanValue();\n"); - - // Check that all required fields in this message are set. - // TODO(kenton): We can optimize this when we switch to putting all the - // "has" fields into a single bitfield. - for (int i = 0; i < descriptor_->field_count(); i++) { - const FieldDescriptor* field = descriptor_->field(i); - const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); - - if (field->is_required()) { - printer->Print( - "if (!has$name$()) {\n" - " return null;\n" - "}\n", - "name", info->capitalized_name); - } - } - - // Now check that all embedded messages are initialized. - for (int i = 0; i < descriptor_->field_count(); i++) { - const FieldDescriptor* field = descriptor_->field(i); - const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); - if (GetJavaType(field) == JAVATYPE_MESSAGE && - HasRequiredFields(field->message_type())) { - switch (field->label()) { - case FieldDescriptor::LABEL_REQUIRED: - printer->Print( - "if (!get$name$().isInitialized()) {\n" - " return null;\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); - break; - case FieldDescriptor::LABEL_OPTIONAL: - if (!SupportFieldPresence(descriptor_->file()) && - field->containing_oneof() != NULL) { - const OneofDescriptor* oneof = field->containing_oneof(); - const OneofGeneratorInfo* oneof_info = - context_->GetOneofGeneratorInfo(oneof); - printer->Print("if ($oneof_name$Case_ == $field_number$) {\n", - "oneof_name", oneof_info->name, "field_number", - SimpleItoa(field->number())); - } else { - printer->Print( - "if (has$name$()) {\n", - "name", info->capitalized_name); - } - printer->Print( - " if (!get$name$().isInitialized()) {\n" - " return null;\n" - " }\n" - "}\n", - "name", info->capitalized_name); - break; - case FieldDescriptor::LABEL_REPEATED: - if (IsMapEntry(field->message_type())) { - printer->Print( - "for ($type$ item : get$name$Map().values()) {\n" - " if (!item.isInitialized()) {\n" - " return null;\n" - " }\n" - "}\n", - "type", MapValueImmutableClassdName(field->message_type(), - name_resolver_), - "name", info->capitalized_name); - } else { - printer->Print( - "for (int i = 0; i < get$name$Count(); i++) {\n" - " if (!get$name$(i).isInitialized()) {\n" - " return null;\n" - " }\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); - } - break; - } - } - } - - if (descriptor_->extension_range_count() > 0) { - printer->Print( - "if (!extensionsAreInitialized()) {\n" - " return null;\n" - "}\n"); - } - - printer->Print( - "return DEFAULT_INSTANCE;\n" - "\n"); -} - -// =================================================================== - -void ImmutableMessageLiteGenerator::GenerateDynamicMethodMakeImmutable( - io::Printer* printer) { - - // Output generation code for each field. - for (int i = 0; i < descriptor_->field_count(); i++) { - field_generators_.get(descriptor_->field(i)) - .GenerateDynamicMethodMakeImmutableCode(printer); - } - - printer->Print( - "return null;\n"); -} - -// =================================================================== - void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuilder( io::Printer* printer) { - printer->Print( - "return new Builder();\n"); -} - -// =================================================================== - -void ImmutableMessageLiteGenerator::GenerateDynamicMethodVisit( - io::Printer* printer) { - printer->Print( - "Visitor visitor = (Visitor) arg0;\n" - "$classname$ other = ($classname$) arg1;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - - for (int i = 0; i < descriptor_->field_count(); i++) { - if (!descriptor_->field(i)->containing_oneof()) { - field_generators_.get( - descriptor_->field(i)).GenerateVisitCode(printer); - } - } - - // Merge oneof fields. - for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) { - printer->Print( - "switch (other.get$oneof_capitalized_name$Case()) {\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name); - printer->Indent(); - for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { - const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); - printer->Print( - "case $field_name$: {\n", - "field_name", - ToUpper(field->name())); - printer->Indent(); - field_generators_.get(field).GenerateVisitCode(printer); - printer->Print( - "break;\n"); - printer->Outdent(); - printer->Print( - "}\n"); - } - printer->Print( - "case $cap_oneof_name$_NOT_SET: {\n" - " visitor.visitOneofNotSet($oneof_name$Case_ != 0);\n" - " break;\n" - "}\n", - "cap_oneof_name", - ToUpper(context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name), - "oneof_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); - printer->Outdent(); - printer->Print( - "}\n"); - } - - printer->Print( - "if (visitor == com.google.protobuf.GeneratedMessageLite.MergeFromVisitor\n" - " .INSTANCE) {\n"); - printer->Indent(); - for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) { - const OneofDescriptor* field = descriptor_->oneof_decl(i); - printer->Print( - "if (other.$oneof_name$Case_ != 0) {\n" - " $oneof_name$Case_ = other.$oneof_name$Case_;\n" - "}\n", - "oneof_name", context_->GetOneofGeneratorInfo(field)->name); - } - - if (GenerateHasBits(descriptor_)) { - // Integers for bit fields. - int totalBits = 0; - for (int i = 0; i < descriptor_->field_count(); i++) { - totalBits += field_generators_.get(descriptor_->field(i)) - .GetNumBitsForMessage(); - } - int totalInts = (totalBits + 31) / 32; - - for (int i = 0; i < totalInts; i++) { - printer->Print( - "$bit_field_name$ |= other.$bit_field_name$;\n", - "bit_field_name", GetBitFieldName(i)); - } - } - printer->Outdent(); - printer->Print( - "}\n"); - - - printer->Print( - "return this;\n"); + printer->Print("return new Builder();\n"); } // =================================================================== -void ImmutableMessageLiteGenerator::GenerateDynamicMethodMergeFromStream( +void ImmutableMessageLiteGenerator::GenerateExtensionRegistrationCode( io::Printer* printer) { - printer->Print( - "com.google.protobuf.CodedInputStream input =\n" - " (com.google.protobuf.CodedInputStream) arg0;\n" - "com.google.protobuf.ExtensionRegistryLite extensionRegistry =\n" - " (com.google.protobuf.ExtensionRegistryLite) arg1;\n" - "if (extensionRegistry == null) {\n" - " throw new java.lang.NullPointerException();\n" - "}\n"); - printer->Print( - "try {\n"); - printer->Indent(); - printer->Print( - "boolean done = false;\n" - "while (!done) {\n"); - printer->Indent(); - - printer->Print( - "int tag = input.readTag();\n" - "switch (tag) {\n"); - printer->Indent(); - - printer->Print( - "case 0:\n" // zero signals EOF / limit reached - " done = true;\n" - " break;\n"); - - std::unique_ptr sorted_fields( - SortFieldsByNumber(descriptor_)); - for (int i = 0; i < descriptor_->field_count(); i++) { - const FieldDescriptor* field = sorted_fields[i]; - uint32 tag = WireFormatLite::MakeTag( - field->number(), WireFormat::WireTypeForFieldType(field->type())); - - printer->Print("case $tag$: {\n", "tag", - SimpleItoa(static_cast(tag))); - printer->Indent(); - - field_generators_.get(field).GenerateParsingCode(printer); - - printer->Outdent(); - printer->Print( - " break;\n" - "}\n"); - - if (field->is_packable()) { - // To make packed = true wire compatible, we generate parsing code from - // a packed version of this field regardless of - // field->options().packed(). - uint32 packed_tag = WireFormatLite::MakeTag( - field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED); - printer->Print("case $tag$: {\n", "tag", - SimpleItoa(static_cast(packed_tag))); - printer->Indent(); - - field_generators_.get(field).GenerateParsingCodeFromPacked(printer); - - printer->Outdent(); - printer->Print( - " break;\n" - "}\n"); - } - } - - if (descriptor_->extension_range_count() > 0) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "default: {\n" - " if (!parseUnknownFieldAsMessageSet(\n" - " getDefaultInstanceForType(), input, extensionRegistry,\n" - " tag)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } else { - printer->Print( - "default: {\n" - " if (!parseUnknownField(getDefaultInstanceForType(),\n" - " input, extensionRegistry, tag)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } - } else { - printer->Print( - "default: {\n" - " if (!parseUnknownField(tag, input)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } - - printer->Outdent(); - printer->Outdent(); - printer->Print( - " }\n" // switch (tag) - "}\n"); // while (!done) - - printer->Outdent(); - printer->Print( - "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" - " throw new RuntimeException(e.setUnfinishedMessage(this));\n" - "} catch (java.io.IOException e) {\n" - " throw new RuntimeException(\n" - " new com.google.protobuf.InvalidProtocolBufferException(\n" - " e.getMessage()).setUnfinishedMessage(this));\n" - "} finally {\n"); - printer->Indent(); - - printer->Outdent(); - printer->Print( - "}\n"); // finally -} - -// =================================================================== - -void ImmutableMessageLiteGenerator:: -GenerateExtensionRegistrationCode(io::Printer* printer) { for (int i = 0; i < descriptor_->extension_count(); i++) { ImmutableExtensionLiteGenerator(descriptor_->extension(i), context_) - .GenerateRegistrationCode(printer); + .GenerateRegistrationCode(printer); } for (int i = 0; i < descriptor_->nested_type_count(); i++) { ImmutableMessageLiteGenerator(descriptor_->nested_type(i), context_) - .GenerateExtensionRegistrationCode(printer); + .GenerateExtensionRegistrationCode(printer); } } // =================================================================== -void ImmutableMessageLiteGenerator:: -GenerateConstructor(io::Printer* printer) { - printer->Print( - "private $classname$() {\n", - "classname", descriptor_->name()); +void ImmutableMessageLiteGenerator::GenerateConstructor(io::Printer* printer) { + printer->Print("private $classname$() {\n", "classname", descriptor_->name()); printer->Indent(); // Initialize all fields to default. GenerateInitializers(printer); printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } // =================================================================== void ImmutableMessageLiteGenerator::GenerateParser(io::Printer* printer) { printer->Print( - "private static volatile com.google.protobuf.Parser<$classname$> PARSER;\n" + "private static volatile com.google.protobuf.Parser<$classname$> " + "PARSER;\n" "\n" "public static com.google.protobuf.Parser<$classname$> parser() {\n" " return DEFAULT_INSTANCE.getParserForType();\n" diff --git a/src/google/protobuf/compiler/java/java_message_lite.h b/src/google/protobuf/compiler/java/java_message_lite.h index d38c54a7f9..5290b1e641 100644 --- a/src/google/protobuf/compiler/java/java_message_lite.h +++ b/src/google/protobuf/compiler/java/java_message_lite.h @@ -35,8 +35,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_LITE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_LITE_H__ -#include #include +#include #include #include @@ -53,29 +53,19 @@ class ImmutableMessageLiteGenerator : public MessageGenerator { virtual void Generate(io::Printer* printer); virtual void GenerateInterface(io::Printer* printer); virtual void GenerateExtensionRegistrationCode(io::Printer* printer); - virtual void GenerateStaticVariables( - io::Printer* printer, int* bytecode_estimate); + virtual void GenerateStaticVariables(io::Printer* printer, + int* bytecode_estimate); virtual int GenerateStaticVariableInitializers(io::Printer* printer); private: - - void GenerateMessageSerializationMethods(io::Printer* printer); void GenerateParseFromMethods(io::Printer* printer); - void GenerateSerializeOneField(io::Printer* printer, - const FieldDescriptor* field); - void GenerateSerializeOneExtensionRange( - io::Printer* printer, const Descriptor::ExtensionRange* range); void GenerateBuilder(io::Printer* printer); - void GenerateDynamicMethodIsInitialized(io::Printer* printer); - void GenerateDynamicMethodMakeImmutable(io::Printer* printer); - void GenerateDynamicMethodVisit(io::Printer* printer); - void GenerateDynamicMethodMergeFromStream(io::Printer* printer); void GenerateDynamicMethodNewBuilder(io::Printer* printer); void GenerateInitializers(io::Printer* printer); - void GenerateEqualsAndHashCode(io::Printer* printer); void GenerateParser(io::Printer* printer); void GenerateConstructor(io::Printer* printer); + void GenerateDynamicMethodNewBuildMessageInfo(io::Printer* printer); Context* context_; ClassNameResolver* name_resolver_; diff --git a/src/google/protobuf/compiler/java/java_name_resolver.cc b/src/google/protobuf/compiler/java/java_name_resolver.cc index 1673b4ee76..d07c628e50 100644 --- a/src/google/protobuf/compiler/java/java_name_resolver.cc +++ b/src/google/protobuf/compiler/java/java_name_resolver.cc @@ -37,6 +37,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -52,8 +53,8 @@ const char* kOuterClassNameSuffix = "OuterClass"; // Full name : foo.Bar.Baz // Package name: foo // After strip : Bar.Baz -string StripPackageName(const string& full_name, - const FileDescriptor* file) { +std::string StripPackageName(const std::string& full_name, + const FileDescriptor* file) { if (file->package().empty()) { return full_name; } else { @@ -63,46 +64,61 @@ string StripPackageName(const string& full_name, } // Get the name of a message's Java class without package name prefix. -string ClassNameWithoutPackage(const Descriptor* descriptor, - bool immutable) { - return StripPackageName(descriptor->full_name(), - descriptor->file()); +std::string ClassNameWithoutPackage(const Descriptor* descriptor, + bool immutable) { + return StripPackageName(descriptor->full_name(), descriptor->file()); } // Get the name of an enum's Java class without package name prefix. -string ClassNameWithoutPackage(const EnumDescriptor* descriptor, - bool immutable) { +std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor, + bool immutable) { // Doesn't append "Mutable" for enum type's name. const Descriptor* message_descriptor = descriptor->containing_type(); if (message_descriptor == NULL) { return descriptor->name(); } else { - return ClassNameWithoutPackage(message_descriptor, immutable) + - "." + descriptor->name(); + return ClassNameWithoutPackage(message_descriptor, immutable) + "." + + descriptor->name(); } } // Get the name of a service's Java class without package name prefix. -string ClassNameWithoutPackage(const ServiceDescriptor* descriptor, - bool immutable) { - string full_name = StripPackageName(descriptor->full_name(), - descriptor->file()); +std::string ClassNameWithoutPackage(const ServiceDescriptor* descriptor, + bool immutable) { + std::string full_name = + StripPackageName(descriptor->full_name(), descriptor->file()); // We don't allow nested service definitions. - GOOGLE_CHECK(full_name.find('.') == string::npos); + GOOGLE_CHECK(full_name.find('.') == std::string::npos); return full_name; } +// Return true if a and b are equals (case insensitive). +NameEquality CheckNameEquality(const string& a, const string& b) { + if (ToUpper(a) == ToUpper(b)) { + if (a == b) { + return NameEquality::EXACT_EQUAL; + } + return NameEquality::EQUAL_IGNORE_CASE; + } + return NameEquality::NO_MATCH; +} + // Check whether a given message or its nested types has the given class name. bool MessageHasConflictingClassName(const Descriptor* message, - const string& classname) { - if (message->name() == classname) return true; + const std::string& classname, + NameEquality equality_mode) { + if (CheckNameEquality(message->name(), classname) == equality_mode) { + return true; + } for (int i = 0; i < message->nested_type_count(); ++i) { - if (MessageHasConflictingClassName(message->nested_type(i), classname)) { + if (MessageHasConflictingClassName(message->nested_type(i), classname, + equality_mode)) { return true; } } for (int i = 0; i < message->enum_type_count(); ++i) { - if (message->enum_type(i)->name() == classname) { + if (CheckNameEquality(message->enum_type(i)->name(), classname) == + equality_mode) { return true; } } @@ -111,17 +127,15 @@ bool MessageHasConflictingClassName(const Descriptor* message, } // namespace -ClassNameResolver::ClassNameResolver() { -} +ClassNameResolver::ClassNameResolver() {} -ClassNameResolver::~ClassNameResolver() { -} +ClassNameResolver::~ClassNameResolver() {} -string ClassNameResolver::GetFileDefaultImmutableClassName( +std::string ClassNameResolver::GetFileDefaultImmutableClassName( const FileDescriptor* file) { - string basename; - string::size_type last_slash = file->name().find_last_of('/'); - if (last_slash == string::npos) { + std::string basename; + std::string::size_type last_slash = file->name().find_last_of('/'); + if (last_slash == std::string::npos) { basename = file->name(); } else { basename = file->name().substr(last_slash + 1); @@ -129,15 +143,16 @@ string ClassNameResolver::GetFileDefaultImmutableClassName( return UnderscoresToCamelCase(StripProto(basename), true); } -string ClassNameResolver::GetFileImmutableClassName( +std::string ClassNameResolver::GetFileImmutableClassName( const FileDescriptor* file) { - string& class_name = file_immutable_outer_class_names_[file]; + std::string& class_name = file_immutable_outer_class_names_[file]; if (class_name.empty()) { if (file->options().has_java_outer_classname()) { class_name = file->options().java_outer_classname(); } else { class_name = GetFileDefaultImmutableClassName(file); - if (HasConflictingClassName(file, class_name)) { + if (HasConflictingClassName(file, class_name, + NameEquality::EXACT_EQUAL)) { class_name += kOuterClassNameSuffix; } } @@ -145,8 +160,8 @@ string ClassNameResolver::GetFileImmutableClassName( return class_name; } -string ClassNameResolver::GetFileClassName(const FileDescriptor* file, - bool immutable) { +std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file, + bool immutable) { if (immutable) { return GetFileImmutableClassName(file); } else { @@ -156,34 +171,38 @@ string ClassNameResolver::GetFileClassName(const FileDescriptor* file, // Check whether there is any type defined in the proto file that has // the given class name. -bool ClassNameResolver::HasConflictingClassName( - const FileDescriptor* file, const string& classname) { +bool ClassNameResolver::HasConflictingClassName(const FileDescriptor* file, + const std::string& classname, + NameEquality equality_mode) { for (int i = 0; i < file->enum_type_count(); i++) { - if (file->enum_type(i)->name() == classname) { + if (CheckNameEquality(file->enum_type(i)->name(), classname) == + equality_mode) { return true; } } for (int i = 0; i < file->service_count(); i++) { - if (file->service(i)->name() == classname) { + if (CheckNameEquality(file->service(i)->name(), classname) == + equality_mode) { return true; } } for (int i = 0; i < file->message_type_count(); i++) { - if (MessageHasConflictingClassName(file->message_type(i), classname)) { + if (MessageHasConflictingClassName(file->message_type(i), classname, + equality_mode)) { return true; } } return false; } -string ClassNameResolver::GetDescriptorClassName( +std::string ClassNameResolver::GetDescriptorClassName( const FileDescriptor* descriptor) { return GetFileImmutableClassName(descriptor); } -string ClassNameResolver::GetClassName(const FileDescriptor* descriptor, - bool immutable) { - string result = FileJavaPackage(descriptor, immutable); +std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor, + bool immutable) { + std::string result = FileJavaPackage(descriptor, immutable); if (!result.empty()) result += '.'; result += GetFileClassName(descriptor, immutable); return result; @@ -191,11 +210,10 @@ string ClassNameResolver::GetClassName(const FileDescriptor* descriptor, // Get the full name of a Java class by prepending the Java package name // or outer class name. -string ClassNameResolver::GetClassFullName(const string& name_without_package, - const FileDescriptor* file, - bool immutable, - bool multiple_files) { - string result; +std::string ClassNameResolver::GetClassFullName( + const std::string& name_without_package, const FileDescriptor* file, + bool immutable, bool multiple_files) { + std::string result; if (multiple_files) { result = FileJavaPackage(file, immutable); } else { @@ -208,33 +226,32 @@ string ClassNameResolver::GetClassFullName(const string& name_without_package, return result; } -string ClassNameResolver::GetClassName(const Descriptor* descriptor, - bool immutable) { +std::string ClassNameResolver::GetClassName(const Descriptor* descriptor, + bool immutable) { return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable), descriptor->file(), immutable, MultipleJavaFiles(descriptor->file(), immutable)); } -string ClassNameResolver::GetClassName(const EnumDescriptor* descriptor, - bool immutable) { +std::string ClassNameResolver::GetClassName(const EnumDescriptor* descriptor, + bool immutable) { return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable), descriptor->file(), immutable, MultipleJavaFiles(descriptor->file(), immutable)); } -string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor, - bool immutable) { +std::string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor, + bool immutable) { return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable), descriptor->file(), immutable, MultipleJavaFiles(descriptor->file(), immutable)); } // Get the Java Class style full name of a message. -string ClassNameResolver::GetJavaClassFullName( - const string& name_without_package, - const FileDescriptor* file, +std::string ClassNameResolver::GetJavaClassFullName( + const std::string& name_without_package, const FileDescriptor* file, bool immutable) { - string result; + std::string result; if (MultipleJavaFiles(file, immutable)) { result = FileJavaPackage(file, immutable); if (!result.empty()) result += '.'; @@ -246,25 +263,22 @@ string ClassNameResolver::GetJavaClassFullName( return result; } -string ClassNameResolver::GetExtensionIdentifierName( +std::string ClassNameResolver::GetExtensionIdentifierName( const FieldDescriptor* descriptor, bool immutable) { return GetClassName(descriptor->containing_type(), immutable) + "." + descriptor->name(); } - -string ClassNameResolver::GetJavaImmutableClassName( +std::string ClassNameResolver::GetJavaImmutableClassName( const Descriptor* descriptor) { - return GetJavaClassFullName( - ClassNameWithoutPackage(descriptor, true), - descriptor->file(), true); + return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true), + descriptor->file(), true); } -string ClassNameResolver::GetJavaImmutableClassName( +std::string ClassNameResolver::GetJavaImmutableClassName( const EnumDescriptor* descriptor) { - return GetJavaClassFullName( - ClassNameWithoutPackage(descriptor, true), - descriptor->file(), true); + return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true), + descriptor->file(), true); } diff --git a/src/google/protobuf/compiler/java/java_name_resolver.h b/src/google/protobuf/compiler/java/java_name_resolver.h index 90684da26d..89bcb59741 100644 --- a/src/google/protobuf/compiler/java/java_name_resolver.h +++ b/src/google/protobuf/compiler/java/java_name_resolver.h @@ -47,6 +47,9 @@ class ServiceDescriptor; namespace compiler { namespace java { +// Indicates how closely the two class names match. +enum NameEquality { NO_MATCH, EXACT_EQUAL, EQUAL_IGNORE_CASE }; + // Used to get the Java class related names for a given descriptor. It caches // the results to avoid redundant calculation across multiple name queries. // Thread-safety note: This class is *not* thread-safe. @@ -56,63 +59,62 @@ class ClassNameResolver { ~ClassNameResolver(); // Gets the unqualified outer class name for the file. - string GetFileClassName(const FileDescriptor* file, bool immutable); + std::string GetFileClassName(const FileDescriptor* file, bool immutable); // Gets the unqualified immutable outer class name of a file. - string GetFileImmutableClassName(const FileDescriptor* file); + std::string GetFileImmutableClassName(const FileDescriptor* file); // Gets the unqualified default immutable outer class name of a file // (converted from the proto file's name). - string GetFileDefaultImmutableClassName(const FileDescriptor* file); + std::string GetFileDefaultImmutableClassName(const FileDescriptor* file); // Check whether there is any type defined in the proto file that has // the given class name. bool HasConflictingClassName(const FileDescriptor* file, - const string& classname); + const std::string& classname, + NameEquality equality_mode); // Gets the name of the outer class that holds descriptor information. // Descriptors are shared between immutable messages and mutable messages. // Since both of them are generated optionally, the descriptors need to be // put in another common place. - string GetDescriptorClassName(const FileDescriptor* file); + std::string GetDescriptorClassName(const FileDescriptor* file); // Gets the fully-qualified class name corresponding to the given descriptor. - string GetClassName(const Descriptor* descriptor, bool immutable); - string GetClassName(const EnumDescriptor* descriptor, bool immutable); - string GetClassName(const ServiceDescriptor* descriptor, bool immutable); - string GetClassName(const FileDescriptor* descriptor, bool immutable); + std::string GetClassName(const Descriptor* descriptor, bool immutable); + std::string GetClassName(const EnumDescriptor* descriptor, bool immutable); + std::string GetClassName(const ServiceDescriptor* descriptor, bool immutable); + std::string GetClassName(const FileDescriptor* descriptor, bool immutable); - template - string GetImmutableClassName(const DescriptorType* descriptor) { + template + std::string GetImmutableClassName(const DescriptorType* descriptor) { return GetClassName(descriptor, true); } - template - string GetMutableClassName(const DescriptorType* descriptor) { + template + std::string GetMutableClassName(const DescriptorType* descriptor) { return GetClassName(descriptor, false); } // Gets the fully qualified name of an extension identifier. - string GetExtensionIdentifierName(const FieldDescriptor* descriptor, - bool immutable); + std::string GetExtensionIdentifierName(const FieldDescriptor* descriptor, + bool immutable); // Gets the fully qualified name for generated classes in Java convention. // Nested classes will be separated using '$' instead of '.' // For example: // com.package.OuterClass$OuterMessage$InnerMessage - string GetJavaImmutableClassName(const Descriptor* descriptor); - string GetJavaImmutableClassName(const EnumDescriptor* descriptor); + std::string GetJavaImmutableClassName(const Descriptor* descriptor); + std::string GetJavaImmutableClassName(const EnumDescriptor* descriptor); private: // Get the full name of a Java class by prepending the Java package name // or outer class name. - string GetClassFullName(const string& name_without_package, - const FileDescriptor* file, - bool immutable, - bool multiple_files); + std::string GetClassFullName(const std::string& name_without_package, + const FileDescriptor* file, bool immutable, + bool multiple_files); // Get the Java Class style full name of a message. - string GetJavaClassFullName( - const string& name_without_package, - const FileDescriptor* file, - bool immutable); + std::string GetJavaClassFullName(const std::string& name_without_package, + const FileDescriptor* file, bool immutable); // Caches the result to provide better performance. - std::map file_immutable_outer_class_names_; + std::map + file_immutable_outer_class_names_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ClassNameResolver); }; diff --git a/src/google/protobuf/compiler/java/java_names.h b/src/google/protobuf/compiler/java/java_names.h index 39e8c51f8c..a8efbb49bd 100644 --- a/src/google/protobuf/compiler/java/java_names.h +++ b/src/google/protobuf/compiler/java/java_names.h @@ -57,41 +57,41 @@ namespace java { // // Returns: // The fully-qualified Java class name. -string ClassName(const Descriptor* descriptor); +std::string ClassName(const Descriptor* descriptor); // Requires: // descriptor != NULL // // Returns: // The fully-qualified Java class name. -string ClassName(const EnumDescriptor* descriptor); +std::string ClassName(const EnumDescriptor* descriptor); // Requires: // descriptor != NULL // // Returns: // The fully-qualified Java class name. -string ClassName(const FileDescriptor* descriptor); +std::string ClassName(const FileDescriptor* descriptor); // Requires: // descriptor != NULL // // Returns: // The fully-qualified Java class name. -string ClassName(const ServiceDescriptor* descriptor); +std::string ClassName(const ServiceDescriptor* descriptor); // Requires: // descriptor != NULL // // Returns: // Java package name. -string FileJavaPackage(const FileDescriptor* descriptor); +std::string FileJavaPackage(const FileDescriptor* descriptor); // Requires: // descriptor != NULL // Returns: // Captialized camel case name field name. -string CapitalizedFieldName(const FieldDescriptor* descriptor); +std::string CapitalizedFieldName(const FieldDescriptor* descriptor); // Requires: // descriptor != NULL diff --git a/src/google/protobuf/compiler/java/java_plugin_unittest.cc b/src/google/protobuf/compiler/java/java_plugin_unittest.cc index 644d0685a3..744b2c8be7 100644 --- a/src/google/protobuf/compiler/java/java_plugin_unittest.cc +++ b/src/google/protobuf/compiler/java/java_plugin_unittest.cc @@ -38,8 +38,8 @@ #include #include -#include #include +#include #include #include @@ -58,10 +58,9 @@ class TestGenerator : public CodeGenerator { ~TestGenerator() {} virtual bool Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const { - string filename = "Test.java"; + const std::string& parameter, GeneratorContext* context, + std::string* error) const { + std::string filename = "Test.java"; TryInsert(filename, "outer_class_scope", context); TryInsert(filename, "class_scope:foo.Bar", context); TryInsert(filename, "class_scope:foo.Bar.Baz", context); @@ -71,7 +70,8 @@ class TestGenerator : public CodeGenerator { return true; } - void TryInsert(const string& filename, const string& insertion_point, + void TryInsert(const std::string& filename, + const std::string& insertion_point, GeneratorContext* context) const { std::unique_ptr output( context->OpenForInsert(filename, insertion_point)); @@ -103,17 +103,12 @@ TEST(JavaPluginTest, PluginTest) { cli.RegisterGenerator("--java_out", &java_generator, ""); cli.RegisterGenerator("--test_out", &test_generator, ""); - string proto_path = "-I" + TestTempDir(); - string java_out = "--java_out=" + TestTempDir(); - string test_out = "--test_out=" + TestTempDir(); + std::string proto_path = "-I" + TestTempDir(); + std::string java_out = "--java_out=" + TestTempDir(); + std::string test_out = "--test_out=" + TestTempDir(); - const char* argv[] = { - "protoc", - proto_path.c_str(), - java_out.c_str(), - test_out.c_str(), - "test.proto" - }; + const char* argv[] = {"protoc", proto_path.c_str(), java_out.c_str(), + test_out.c_str(), "test.proto"}; EXPECT_EQ(0, cli.Run(5, argv)); } diff --git a/src/google/protobuf/compiler/java/java_primitive_field.cc b/src/google/protobuf/compiler/java/java_primitive_field.cc index c98c8b3a16..8b51e792df 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field.cc +++ b/src/google/protobuf/compiler/java/java_primitive_field.cc @@ -46,6 +46,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -56,20 +57,11 @@ using internal::WireFormatLite; namespace { -bool EnableJavaPrimitiveExperiment() { -#ifdef PROTOBUF_JAVA_PRIMITIVE_EXPERIMENT - return PROTOBUF_JAVA_PRIMITIVE_EXPERIMENT; -#else // PROTOBUF_JAVA_PRIMITIVE_EXPERIMENT - return false; -#endif // !PROTOBUF_JAVA_PRIMITIVE_EXPERIMENT -} - void SetPrimitiveVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, + int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); JavaType javaType = GetJavaType(descriptor); @@ -77,13 +69,10 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, (*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType); (*variables)["field_type"] = (*variables)["type"]; - if (EnableJavaPrimitiveExperiment() && - (javaType == JAVATYPE_BOOLEAN || - javaType == JAVATYPE_DOUBLE || - javaType == JAVATYPE_FLOAT || - javaType == JAVATYPE_INT || - javaType == JAVATYPE_LONG)) { - string capitalized_type = UnderscoresToCamelCase( + if (javaType == JAVATYPE_BOOLEAN || javaType == JAVATYPE_DOUBLE || + javaType == JAVATYPE_FLOAT || javaType == JAVATYPE_INT || + javaType == JAVATYPE_LONG) { + std::string capitalized_type = UnderscoresToCamelCase( PrimitiveTypeName(javaType), /*cap_first_letter=*/true); (*variables)["field_list_type"] = "com.google.protobuf.Internal." + capitalized_type + "List"; @@ -117,13 +106,15 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, } (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver); - (*variables)["default_init"] = IsDefaultValueJavaDefault(descriptor) ? - "" : ("= " + ImmutableDefaultValue(descriptor, name_resolver)); + (*variables)["default_init"] = + IsDefaultValueJavaDefault(descriptor) + ? "" + : ("= " + ImmutableDefaultValue(descriptor, name_resolver)); (*variables)["capitalized_type"] = GetCapitalizedType(descriptor, /* immutable = */ true); (*variables)["tag"] = - SimpleItoa(static_cast(WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = SimpleItoa( + StrCat(static_cast(WireFormat::MakeTag(descriptor))); + (*variables)["tag_size"] = StrCat( WireFormat::TagSize(descriptor->number(), GetType(descriptor))); if (IsReferenceType(GetJavaType(descriptor))) { (*variables)["null_check"] = @@ -135,11 +126,11 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, } // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; int fixed_size = FixedSize(GetType(descriptor)); if (fixed_size != -1) { - (*variables)["fixed_size"] = SimpleItoa(fixed_size); + (*variables)["fixed_size"] = StrCat(fixed_size); } (*variables)["on_changed"] = "onChanged();"; @@ -193,14 +184,10 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, // =================================================================== -ImmutablePrimitiveFieldGenerator:: -ImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +ImmutablePrimitiveFieldGenerator::ImmutablePrimitiveFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); @@ -209,136 +196,139 @@ ImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, ImmutablePrimitiveFieldGenerator::~ImmutablePrimitiveFieldGenerator() {} int ImmutablePrimitiveFieldGenerator::GetNumBitsForMessage() const { - return 1; + return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } int ImmutablePrimitiveFieldGenerator::GetNumBitsForBuilder() const { - return 1; + return GetNumBitsForMessage(); } -void ImmutablePrimitiveFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $field_type$ $name$_;\n"); +void ImmutablePrimitiveFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $field_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutablePrimitiveFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $field_type$ $name$_ $default_init$;\n"); +void ImmutablePrimitiveFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $field_type$ $name$_ $default_init$;\n"); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_builder$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_builder$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - "$null_check$" - " $set_has_field_bit_builder$\n" - " $name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + "$null_check$" + " $set_has_field_bit_builder$\n" + " $name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $clear_has_field_bit_builder$\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $clear_has_field_bit_builder$\n"); printer->Annotate("{", "}", descriptor_); JavaType type = GetJavaType(descriptor_); if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) { // The default value is not a simple literal so we want to avoid executing // it multiple times. Instead, get the default out of the default instance. - printer->Print(variables_, - " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); + printer->Print( + variables_, + " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); } else { - printer->Print(variables_, - " $name$_ = $default$;\n"); + printer->Print(variables_, " $name$_ = $default$;\n"); } printer->Print(variables_, - " $on_changed$\n" - " return this;\n" - "}\n"); + " $on_changed$\n" + " return this;\n" + "}\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { // noop for primitives } -void ImmutablePrimitiveFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { if (!IsDefaultValueJavaDefault(descriptor_)) { printer->Print(variables_, "$name$_ = $default$;\n"); } } -void ImmutablePrimitiveFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $default$;\n" - "$clear_has_field_bit_builder$\n"); + "$name$_ = $default$;\n" + "$clear_has_field_bit_builder$\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, - "if (other.has$capitalized_name$()) {\n" - " set$capitalized_name$(other.get$capitalized_name$());\n" - "}\n"); + "if (other.has$capitalized_name$()) {\n" + " set$capitalized_name$(other.get$capitalized_name$());\n" + "}\n"); } else { printer->Print(variables_, - "if (other.get$capitalized_name$() != $default$) {\n" - " set$capitalized_name$(other.get$capitalized_name$());\n" - "}\n"); + "if (other.get$capitalized_name$() != $default$) {\n" + " set$capitalized_name$(other.get$capitalized_name$());\n" + "}\n"); } } -void ImmutablePrimitiveFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { if (IsDefaultValueJavaDefault(descriptor_)) { printer->Print(variables_, @@ -358,67 +348,68 @@ GenerateBuildingCode(io::Printer* printer) const { } } -void ImmutablePrimitiveFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "$set_has_field_bit_message$\n" - "$name$_ = input.read$capitalized_type$();\n"); + "$set_has_field_bit_message$\n" + "$name$_ = input.read$capitalized_type$();\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // noop for primitives. } -void ImmutablePrimitiveFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.write$capitalized_type$($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " output.write$capitalized_type$($number$, $name$_);\n" + "}\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$capitalized_type$Size($number$, $name$_);\n" + "}\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { switch (GetJavaType(descriptor_)) { case JAVATYPE_INT: case JAVATYPE_LONG: case JAVATYPE_BOOLEAN: printer->Print(variables_, - "result = result && (get$capitalized_name$()\n" - " == other.get$capitalized_name$());\n"); + "if (get$capitalized_name$()\n" + " != other.get$capitalized_name$()) return false;\n"); break; case JAVATYPE_FLOAT: - printer->Print(variables_, - "result = result && (\n" - " java.lang.Float.floatToIntBits(get$capitalized_name$())\n" - " == java.lang.Float.floatToIntBits(\n" - " other.get$capitalized_name$()));\n"); + printer->Print( + variables_, + "if (java.lang.Float.floatToIntBits(get$capitalized_name$())\n" + " != java.lang.Float.floatToIntBits(\n" + " other.get$capitalized_name$())) return false;\n"); break; case JAVATYPE_DOUBLE: - printer->Print(variables_, - "result = result && (\n" - " java.lang.Double.doubleToLongBits(get$capitalized_name$())\n" - " == java.lang.Double.doubleToLongBits(\n" - " other.get$capitalized_name$()));\n"); + printer->Print( + variables_, + "if (java.lang.Double.doubleToLongBits(get$capitalized_name$())\n" + " != java.lang.Double.doubleToLongBits(\n" + " other.get$capitalized_name$())) return false;\n"); break; case JAVATYPE_STRING: case JAVATYPE_BYTES: - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); + printer->Print( + variables_, + "if (!get$capitalized_name$()\n" + " .equals(other.get$capitalized_name$())) return false;\n"); break; case JAVATYPE_ENUM: @@ -429,44 +420,47 @@ GenerateEqualsCode(io::Printer* printer) const { } } -void ImmutablePrimitiveFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n"); +void ImmutablePrimitiveFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print(variables_, "hash = (37 * hash) + $constant_name$;\n"); switch (GetJavaType(descriptor_)) { case JAVATYPE_INT: printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$();\n"); + "hash = (53 * hash) + get$capitalized_name$();\n"); break; case JAVATYPE_LONG: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" - " get$capitalized_name$());\n"); + printer->Print( + variables_, + "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" + " get$capitalized_name$());\n"); break; case JAVATYPE_BOOLEAN: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(\n" - " get$capitalized_name$());\n"); + printer->Print( + variables_, + "hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(\n" + " get$capitalized_name$());\n"); break; case JAVATYPE_FLOAT: printer->Print(variables_, - "hash = (53 * hash) + java.lang.Float.floatToIntBits(\n" - " get$capitalized_name$());\n"); + "hash = (53 * hash) + java.lang.Float.floatToIntBits(\n" + " get$capitalized_name$());\n"); break; case JAVATYPE_DOUBLE: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" - " java.lang.Double.doubleToLongBits(get$capitalized_name$()));\n"); + printer->Print( + variables_, + "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" + " java.lang.Double.doubleToLongBits(get$capitalized_name$()));\n"); break; case JAVATYPE_STRING: case JAVATYPE_BYTES: - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); + printer->Print( + variables_, + "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); break; case JAVATYPE_ENUM: @@ -477,171 +471,170 @@ GenerateHashCode(io::Printer* printer) const { } } -string ImmutablePrimitiveFieldGenerator::GetBoxedType() const { +std::string ImmutablePrimitiveFieldGenerator::GetBoxedType() const { return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); } // =================================================================== -ImmutablePrimitiveOneofFieldGenerator:: -ImmutablePrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : ImmutablePrimitiveFieldGenerator( - descriptor, messageBitIndex, builderBitIndex, context) { +ImmutablePrimitiveOneofFieldGenerator::ImmutablePrimitiveOneofFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : ImmutablePrimitiveFieldGenerator(descriptor, messageBitIndex, + builderBitIndex, context) { const OneofGeneratorInfo* info = context->GetOneofGeneratorInfo(descriptor->containing_oneof()); SetCommonOneofVariables(descriptor, info, &variables_); } ImmutablePrimitiveOneofFieldGenerator:: -~ImmutablePrimitiveOneofFieldGenerator() {} + ~ImmutablePrimitiveOneofFieldGenerator() {} -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($boxed_type$) $oneof_name$_;\n" - " }\n" - " return $default$;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($boxed_type$) $oneof_name$_;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($boxed_type$) $oneof_name$_;\n" - " }\n" - " return $default$;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($boxed_type$) $oneof_name$_;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - "$null_check$" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + "$null_check$" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " $on_changed$\n" - " }\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " $on_changed$\n" + " }\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " result.$oneof_name$_ = $oneof_name$_;\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " result.$oneof_name$_ = $oneof_name$_;\n" + "}\n"); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { printer->Print(variables_, - "set$capitalized_name$(other.get$capitalized_name$());\n"); + "set$capitalized_name$(other.get$capitalized_name$());\n"); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "$set_oneof_case_message$;\n" - "$oneof_name$_ = input.read$capitalized_type$();\n"); + "$set_oneof_case_message$;\n" + "$oneof_name$_ = input.read$capitalized_type$();\n"); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.write$capitalized_type$(\n"); + "if ($has_oneof_case_message$) {\n" + " output.write$capitalized_type$(\n"); // $type$ and $boxed_type$ is the same for bytes fields so we don't need to // do redundant casts. if (GetJavaType(descriptor_) == JAVATYPE_BYTES) { - printer->Print(variables_, - " $number$, ($type$) $oneof_name$_);\n"); + printer->Print(variables_, " $number$, ($type$) $oneof_name$_);\n"); } else { - printer->Print(variables_, - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); + printer->Print( + variables_, + " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); } - printer->Print( - "}\n"); + printer->Print("}\n"); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size(\n"); + "if ($has_oneof_case_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$capitalized_type$Size(\n"); // $type$ and $boxed_type$ is the same for bytes fields so we don't need to // do redundant casts. if (GetJavaType(descriptor_) == JAVATYPE_BYTES) { - printer->Print(variables_, - " $number$, ($type$) $oneof_name$_);\n"); + printer->Print(variables_, " $number$, ($type$) $oneof_name$_);\n"); } else { - printer->Print(variables_, - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); + printer->Print( + variables_, + " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); } - printer->Print( - "}\n"); + printer->Print("}\n"); } // =================================================================== RepeatedImmutablePrimitiveFieldGenerator:: -RepeatedImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { + RepeatedImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, + int messageBitIndex, + int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); } RepeatedImmutablePrimitiveFieldGenerator:: -~RepeatedImmutablePrimitiveFieldGenerator() {} + ~RepeatedImmutablePrimitiveFieldGenerator() {} int RepeatedImmutablePrimitiveFieldGenerator::GetNumBitsForMessage() const { return 0; @@ -651,53 +644,54 @@ int RepeatedImmutablePrimitiveFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$java.util.List<$boxed_type$> get$capitalized_name$List();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$java.util.List<$boxed_type$> " + "get$capitalized_name$List();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$int get$capitalized_name$Count();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); } - -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $field_list_type$ $name$_;\n"); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $field_list_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$public java.util.List<$boxed_type$>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "$deprecation$public java.util.List<$boxed_type$>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $repeated_get$(index);\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $repeated_get$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (descriptor_->is_packed()) { printer->Print(variables_, - "private int $name$MemoizedSerializedSize = -1;\n"); + "private int $name$MemoizedSerializedSize = -1;\n"); } } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // One field is the list and the bit field keeps track of whether the // list is immutable. If it's immutable, the invariant is that it must // either an instance of Collections.emptyList() or it's an ArrayList @@ -708,247 +702,261 @@ GenerateBuilderMembers(io::Printer* printer) const { // just that the list cannot be modified via the reference but that the // list can never be modified. printer->Print(variables_, - "private $field_list_type$ $name$_ = $empty_list$;\n"); + "private $field_list_type$ $name$_ = $empty_list$;\n"); printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$get_mutable_bit_builder$) {\n" - " $name$_ = $mutable_copy_list$;\n" - " $set_mutable_bit_builder$;\n" - " }\n" - "}\n"); - - // Note: We return an unmodifiable list because otherwise the caller - // could hold on to the returned list and modify it after the message - // has been built, thus mutating the message which is supposed to be - // immutable. - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<$boxed_type$>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $get_mutable_bit_builder$ ?\n" - " java.util.Collections.unmodifiableList($name$_) : $name$_;\n" - "}\n"); + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$get_mutable_bit_builder$) {\n" + " $name$_ = $mutable_copy_list$;\n" + " $set_mutable_bit_builder$;\n" + " }\n" + "}\n"); + + // Note: We return an unmodifiable list because otherwise the caller + // could hold on to the returned list and modify it after the message + // has been built, thus mutating the message which is supposed to be + // immutable. + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print( + variables_, + "$deprecation$public java.util.List<$boxed_type$>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $get_mutable_bit_builder$ ?\n" + " java.util.Collections.unmodifiableList($name$_) : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $repeated_get$(index);\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $repeated_get$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $repeated_set$(index, value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $repeated_set$(index, value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $repeated_add$(value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $repeated_add$(value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" - " values, $name$_);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" + " values, $name$_);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $name$_ = $empty_list$;\n" - " $clear_mutable_bit_builder$;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $name$_ = $empty_list$;\n" + " $clear_mutable_bit_builder$;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for primitives } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $empty_list$;\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $empty_list$;\n" - "$clear_mutable_bit_builder$;\n"); + "$name$_ = $empty_list$;\n" + "$clear_mutable_bit_builder$;\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // The code below does two optimizations: // 1. If the other list is empty, there's nothing to do. This ensures we // don't allocate a new array if we already have an immutable one. // 2. If the other list is non-empty and our current list is empty, we can // reuse the other list which is guaranteed to be immutable. printer->Print(variables_, - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$_.isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " } else {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addAll(other.$name$_);\n" - " }\n" - " $on_changed$\n" - "}\n"); + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$_.isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " } else {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addAll(other.$name$_);\n" + " }\n" + " $on_changed$\n" + "}\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { // The code below ensures that the result has an immutable list. If our // list is immutable, we can just reuse it. If not, we make it immutable. printer->Print(variables_, - "if ($get_mutable_bit_builder$) {\n" - " $name_make_immutable$;\n" - " $clear_mutable_bit_builder$;\n" - "}\n" - "result.$name$_ = $name$_;\n"); + "if ($get_mutable_bit_builder$) {\n" + " $name_make_immutable$;\n" + " $clear_mutable_bit_builder$;\n" + "}\n" + "result.$name$_ = $name$_;\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = $create_list$;\n" - " $set_mutable_bit_parser$;\n" - "}\n" - "$repeated_add$(input.read$capitalized_type$());\n"); + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = $create_list$;\n" + " $set_mutable_bit_parser$;\n" + "}\n" + "$repeated_add$(input.read$capitalized_type$());\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { - printer->Print(variables_, - "int length = input.readRawVarint32();\n" - "int limit = input.pushLimit(length);\n" - "if (!$get_mutable_bit_parser$ && input.getBytesUntilLimit() > 0) {\n" - " $name$_ = $create_list$;\n" - " $set_mutable_bit_parser$;\n" - "}\n" - "while (input.getBytesUntilLimit() > 0) {\n" - " $repeated_add$(input.read$capitalized_type$());\n" - "}\n" - "input.popLimit(limit);\n"); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateParsingCodeFromPacked( + io::Printer* printer) const { + printer->Print( + variables_, + "int length = input.readRawVarint32();\n" + "int limit = input.pushLimit(length);\n" + "if (!$get_mutable_bit_parser$ && input.getBytesUntilLimit() > 0) {\n" + " $name$_ = $create_list$;\n" + " $set_mutable_bit_parser$;\n" + "}\n" + "while (input.getBytesUntilLimit() > 0) {\n" + " $repeated_add$(input.read$capitalized_type$());\n" + "}\n" + "input.popLimit(limit);\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($get_mutable_bit_parser$) {\n" - " $name_make_immutable$; // C\n" - "}\n"); + "if ($get_mutable_bit_parser$) {\n" + " $name_make_immutable$; // C\n" + "}\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { if (descriptor_->is_packed()) { // We invoke getSerializedSize in writeTo for messages that have packed // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. // That makes it safe to rely on the memoized size here. printer->Print(variables_, - "if (get$capitalized_name$List().size() > 0) {\n" - " output.writeUInt32NoTag($tag$);\n" - " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" - "}\n" - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$capitalized_type$NoTag($repeated_get$(i));\n" - "}\n"); + "if (get$capitalized_name$List().size() > 0) {\n" + " output.writeUInt32NoTag($tag$);\n" + " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" + "}\n" + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.write$capitalized_type$NoTag($repeated_get$(i));\n" + "}\n"); } else { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$capitalized_type$($number$, $repeated_get$(i));\n" - "}\n"); + printer->Print( + variables_, + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.write$capitalized_type$($number$, $repeated_get$(i));\n" + "}\n"); } } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); + "{\n" + " int dataSize = 0;\n"); printer->Indent(); if (FixedSize(GetType(descriptor_)) == -1) { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$SizeNoTag($repeated_get$(i));\n" - "}\n"); + printer->Print( + variables_, + "for (int i = 0; i < $name$_.size(); i++) {\n" + " dataSize += com.google.protobuf.CodedOutputStream\n" + " .compute$capitalized_type$SizeNoTag($repeated_get$(i));\n" + "}\n"); } else { - printer->Print(variables_, - "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n"); + printer->Print( + variables_, + "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n"); } - printer->Print( - "size += dataSize;\n"); + printer->Print("size += dataSize;\n"); if (descriptor_->is_packed()) { printer->Print(variables_, - "if (!get$capitalized_name$List().isEmpty()) {\n" - " size += $tag_size$;\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeInt32SizeNoTag(dataSize);\n" - "}\n"); + "if (!get$capitalized_name$List().isEmpty()) {\n" + " size += $tag_size$;\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .computeInt32SizeNoTag(dataSize);\n" + "}\n"); } else { - printer->Print(variables_, - "size += $tag_size$ * get$capitalized_name$List().size();\n"); + printer->Print( + variables_, + "size += $tag_size$ * get$capitalized_name$List().size();\n"); } // cache the data size for packed fields. if (descriptor_->is_packed()) { - printer->Print(variables_, - "$name$MemoizedSerializedSize = dataSize;\n"); + printer->Print(variables_, "$name$MemoizedSerializedSize = dataSize;\n"); } printer->Outdent(); printer->Print("}\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (!get$capitalized_name$List()\n" + " .equals(other.get$capitalized_name$List())) return false;\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (get$capitalized_name$Count() > 0) {\n" + " hash = (37 * hash) + $constant_name$;\n" + " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" + "}\n"); } -string RepeatedImmutablePrimitiveFieldGenerator::GetBoxedType() const { +std::string RepeatedImmutablePrimitiveFieldGenerator::GetBoxedType() const { return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); } diff --git a/src/google/protobuf/compiler/java/java_primitive_field.h b/src/google/protobuf/compiler/java/java_primitive_field.h index a13680575e..db20750e26 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field.h +++ b/src/google/protobuf/compiler/java/java_primitive_field.h @@ -82,14 +82,11 @@ class ImmutablePrimitiveFieldGenerator : public ImmutableFieldGenerator { void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - const int builderBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; private: @@ -143,14 +140,11 @@ class RepeatedImmutablePrimitiveFieldGenerator void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; private: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - const int builderBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutablePrimitiveFieldGenerator); diff --git a/src/google/protobuf/compiler/java/java_primitive_field_lite.cc b/src/google/protobuf/compiler/java/java_primitive_field_lite.cc index 034a0865b0..833be233bc 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_primitive_field_lite.cc @@ -46,6 +46,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -55,13 +56,19 @@ using internal::WireFormat; using internal::WireFormatLite; namespace { +bool EnableExperimentalRuntimeForLite() { +#ifdef PROTOBUF_EXPERIMENT + return PROTOBUF_EXPERIMENT; +#else // PROTOBUF_EXPERIMENT + return false; +#endif // !PROTOBUF_EXPERIMENT +} void SetPrimitiveVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, + int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); JavaType javaType = GetJavaType(descriptor); (*variables)["type"] = PrimitiveTypeName(javaType); @@ -71,13 +78,13 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, (*variables)["capitalized_type"] = GetCapitalizedType(descriptor, /* immutable = */ true); (*variables)["tag"] = - SimpleItoa(static_cast(WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = SimpleItoa( + StrCat(static_cast(WireFormat::MakeTag(descriptor))); + (*variables)["tag_size"] = StrCat( WireFormat::TagSize(descriptor->number(), GetType(descriptor))); (*variables)["required"] = descriptor->is_required() ? "true" : "false"; - string capitalized_type = UnderscoresToCamelCase(PrimitiveTypeName(javaType), - true /* cap_next_letter */); + std::string capitalized_type = UnderscoresToCamelCase( + PrimitiveTypeName(javaType), true /* cap_next_letter */); switch (javaType) { case JAVATYPE_INT: case JAVATYPE_LONG: @@ -127,11 +134,11 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, } // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; int fixed_size = FixedSize(GetType(descriptor)); if (fixed_size != -1) { - (*variables)["fixed_size"] = SimpleItoa(fixed_size); + (*variables)["fixed_size"] = StrCat(fixed_size); } if (SupportFieldPresence(descriptor->file())) { @@ -176,7 +183,6 @@ ImmutablePrimitiveFieldLiteGenerator::ImmutablePrimitiveFieldLiteGenerator( const FieldDescriptor* descriptor, int messageBitIndex, Context* context) : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - context_(context), name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), @@ -186,23 +192,22 @@ ImmutablePrimitiveFieldLiteGenerator::ImmutablePrimitiveFieldLiteGenerator( ImmutablePrimitiveFieldLiteGenerator::~ImmutablePrimitiveFieldLiteGenerator() {} int ImmutablePrimitiveFieldLiteGenerator::GetNumBitsForMessage() const { - return 1; + return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutablePrimitiveFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutablePrimitiveFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { if (IsByteStringWithCustomDefaultValue(descriptor_)) { // allocate this once statically since we know ByteStrings are immutable // values that can be reused. @@ -210,100 +215,109 @@ GenerateMembers(io::Printer* printer) const { variables_, "private static final $field_type$ $bytes_default$ = $default$;\n"); } - printer->Print(variables_, - "private $field_type$ $name$_;\n"); + printer->Print(variables_, "private $field_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - "$null_check$" - " $set_has_field_bit_message$\n" - " $name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$($type$ value) {\n" + "$null_check$" + " $set_has_field_bit_message$\n" + " $name$_ = value;\n" + "}\n"); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $clear_has_field_bit_message$\n"); + "private void clear$capitalized_name$() {\n" + " $clear_has_field_bit_message$\n"); JavaType type = GetJavaType(descriptor_); if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) { // The default value is not a simple literal so we want to avoid executing // it multiple times. Instead, get the default out of the default instance. - printer->Print(variables_, - " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); + printer->Print( + variables_, + " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); } else { - printer->Print(variables_, - " $name$_ = $default$;\n"); + printer->Print(variables_, " $name$_ = $default$;\n"); } - printer->Print(variables_, - "}\n"); + printer->Print(variables_, "}\n"); } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for primitives +void ImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + if (SupportFieldPresence(descriptor_->file())) { + WriteIntToUtf16CharSequence(messageBitIndex_, output); + } + printer->Print(variables_, "\"$name$_\",\n"); } - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { if (IsByteStringWithCustomDefaultValue(descriptor_)) { printer->Print(variables_, "$name$_ = $bytes_default$;\n"); } else if (!IsDefaultValueJavaDefault(descriptor_)) { @@ -311,155 +325,7 @@ GenerateInitializationCode(io::Printer* printer) const { } } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { - // noop for primitives -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "$name$_ = visitor.visit$visit_type$(\n" - " has$capitalized_name$(), $name$_,\n" - " other.has$capitalized_name$(), other.$name$_);\n"); - } else { - printer->Print(variables_, - "$name$_ = visitor.visit$visit_type$($name$_ != $default$, $name$_,\n" - " other.$name$_ != $default$, other.$name$_);\n"); - } -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - // noop for primitives -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - // noop for scalars -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print(variables_, - "$set_has_field_bit_message$\n" - "$name$_ = input.read$capitalized_type$();\n"); -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // noop for primitives. -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.write$capitalized_type$($number$, $name$_);\n" - "}\n"); -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size($number$, $name$_);\n" - "}\n"); -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - switch (GetJavaType(descriptor_)) { - case JAVATYPE_INT: - case JAVATYPE_LONG: - case JAVATYPE_BOOLEAN: - printer->Print(variables_, - "result = result && (get$capitalized_name$()\n" - " == other.get$capitalized_name$());\n"); - break; - - case JAVATYPE_FLOAT: - printer->Print(variables_, - "result = result && (\n" - " java.lang.Float.floatToIntBits(get$capitalized_name$())\n" - " == java.lang.Float.floatToIntBits(\n" - " other.get$capitalized_name$()));\n"); - break; - - case JAVATYPE_DOUBLE: - printer->Print(variables_, - "result = result && (\n" - " java.lang.Double.doubleToLongBits(get$capitalized_name$())\n" - " == java.lang.Double.doubleToLongBits(\n" - " other.get$capitalized_name$()));\n"); - break; - - case JAVATYPE_STRING: - case JAVATYPE_BYTES: - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); - break; - - case JAVATYPE_ENUM: - case JAVATYPE_MESSAGE: - default: - GOOGLE_LOG(FATAL) << "Can't get here."; - break; - } -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n"); - switch (GetJavaType(descriptor_)) { - case JAVATYPE_INT: - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$();\n"); - break; - - case JAVATYPE_LONG: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" - " get$capitalized_name$());\n"); - break; - - case JAVATYPE_BOOLEAN: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(\n" - " get$capitalized_name$());\n"); - break; - - case JAVATYPE_FLOAT: - printer->Print(variables_, - "hash = (53 * hash) + java.lang.Float.floatToIntBits(\n" - " get$capitalized_name$());\n"); - break; - - case JAVATYPE_DOUBLE: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" - " java.lang.Double.doubleToLongBits(get$capitalized_name$()));\n"); - break; - - case JAVATYPE_STRING: - case JAVATYPE_BYTES: - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); - break; - - case JAVATYPE_ENUM: - case JAVATYPE_MESSAGE: - default: - GOOGLE_LOG(FATAL) << "Can't get here."; - break; - } -} - -string ImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { +std::string ImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); } @@ -477,128 +343,103 @@ ImmutablePrimitiveOneofFieldLiteGenerator:: } ImmutablePrimitiveOneofFieldLiteGenerator:: -~ImmutablePrimitiveOneofFieldLiteGenerator() {} + ~ImmutablePrimitiveOneofFieldLiteGenerator() {} -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($boxed_type$) $oneof_name$_;\n" - " }\n" - " return $default$;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($boxed_type$) $oneof_name$_;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - "$null_check$" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$($type$ value) {\n" + "$null_check$" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + "}\n"); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " }\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " }\n" + "}\n"); } +void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output); +} -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - // noop for primitives -} - -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$oneof_name$_ = visitor.visitOneof$visit_type$(\n" - " $has_oneof_case_message$, $oneof_name$_, other.$oneof_name$_);\n"); -} - -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print(variables_, - "$set_oneof_case_message$;\n" - "$oneof_name$_ = input.read$capitalized_type$();\n"); -} - -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.write$capitalized_type$(\n" - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n" - "}\n"); -} - -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size(\n" - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n" - "}\n"); -} - // =================================================================== RepeatedImmutablePrimitiveFieldLiteGenerator:: @@ -606,7 +447,6 @@ RepeatedImmutablePrimitiveFieldLiteGenerator:: const FieldDescriptor* descriptor, int messageBitIndex, Context* context) : descriptor_(descriptor), - messageBitIndex_(messageBitIndex), context_(context), name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, 0, @@ -615,325 +455,183 @@ RepeatedImmutablePrimitiveFieldLiteGenerator:: } RepeatedImmutablePrimitiveFieldLiteGenerator:: -~RepeatedImmutablePrimitiveFieldLiteGenerator() {} + ~RepeatedImmutablePrimitiveFieldLiteGenerator() {} int RepeatedImmutablePrimitiveFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$java.util.List<$boxed_type$> get$capitalized_name$List();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$java.util.List<$boxed_type$> " + "get$capitalized_name$List();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$int get$capitalized_name$Count();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); } - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $field_list_type$ $name$_;\n"); +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $field_list_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$boxed_type$>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$boxed_type$>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $repeated_get$(index);\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $repeated_get$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - if (descriptor_->is_packed() && + if (!EnableExperimentalRuntimeForLite() && descriptor_->is_packed() && context_->HasGeneratedMethods(descriptor_->containing_type())) { printer->Print(variables_, - "private int $name$MemoizedSerializedSize = -1;\n"); + "private int $name$MemoizedSerializedSize = -1;\n"); } - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - " }\n" - "}\n"); - - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, $type$ value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $repeated_set$(index, value);\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void add$capitalized_name$($type$ value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $repeated_add$(value);\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void addAll$capitalized_name$(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.addAll(\n" - " values, $name$_);\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $name$_ = $empty_list$;\n" - "}\n"); -} + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$is_mutable$) {\n" + " $name$_ =\n" + " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" + " }\n" + "}\n"); -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$boxed_type$>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return java.util.Collections.unmodifiableList(\n" - " instance.get$capitalized_name$List());\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Count();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER); + printer->Print(variables_, + "private void set$capitalized_name$(\n" + " int index, $type$ value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $repeated_set$(index, value);\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER); + printer->Print(variables_, + "private void add$capitalized_name$($type$ value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $repeated_add$(value);\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER); + printer->Print(variables_, + "private void addAll$capitalized_name$(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.addAll(\n" + " values, $name$_);\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); + printer->Print(variables_, + "private void clear$capitalized_name$() {\n" + " $name$_ = $empty_list$;\n" + "}\n"); +} + +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public java.util.List<$boxed_type$>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return java.util.Collections.unmodifiableList(\n" + " instance.get$capitalized_name$List());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return instance.get$capitalized_name$(index);\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Count();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return instance.get$capitalized_name$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$(values);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for primitives -} - - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_ = $empty_list$;\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { - // noop for primitives -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_= visitor.$visit_type_list$($name$_, other.$name$_);\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - // noop for primitives -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - // TODO(dweis): Scan the input buffer to count, then initialize - // appropriately. - // TODO(dweis): Scan the input buffer to count and ensure capacity. - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n" - "$repeated_add$(input.read$capitalized_type$());\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { - printer->Print(variables_, - "int length = input.readRawVarint32();\n" - "int limit = input.pushLimit(length);\n" - "if (!$is_mutable$ && input.getBytesUntilLimit() > 0) {\n"); - - int fixed_size = FixedSize(GetType(descriptor_)); - if (fixed_size == -1) { - // TODO(dweis): Scan the input buffer to count, then initialize - // appropriately. - printer->Print(variables_, - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"); - } else { - printer->Print(variables_, - " final int currentSize = $name$_.size();\n" - " $name$_ = $name$_.mutableCopyWithCapacity(\n" - " currentSize + (length/$fixed_size$));\n"); - } - - // TODO(dweis): Scan the input buffer to count and ensure capacity. - printer->Print(variables_, - "}\n" - "while (input.getBytesUntilLimit() > 0) {\n" - " $repeated_add$(input.read$capitalized_type$());\n" - "}\n" - "input.popLimit(limit);\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_mutable$) {\n" - " $make_name_unmodifiable$;\n" - "}\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - if (descriptor_->is_packed()) { - // We invoke getSerializedSize in writeTo for messages that have packed - // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. - // That makes it safe to rely on the memoized size here. - printer->Print(variables_, - "if (get$capitalized_name$List().size() > 0) {\n" - " output.writeUInt32NoTag($tag$);\n" - " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" - "}\n" - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$capitalized_type$NoTag($repeated_get$(i));\n" - "}\n"); - } else { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$capitalized_type$($number$, $repeated_get$(i));\n" - "}\n"); - } -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); - printer->Indent(); - - if (FixedSize(GetType(descriptor_)) == -1) { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$SizeNoTag($repeated_get$(i));\n" - "}\n"); - } else { - printer->Print(variables_, - "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n"); - } - + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); printer->Print( - "size += dataSize;\n"); - - if (descriptor_->is_packed()) { - printer->Print(variables_, - "if (!get$capitalized_name$List().isEmpty()) {\n" - " size += $tag_size$;\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeInt32SizeNoTag(dataSize);\n" + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" "}\n"); - } else { - printer->Print(variables_, - "size += $tag_size$ * get$capitalized_name$List().size();\n"); - } - - // cache the data size for packed fields. - if (descriptor_->is_packed()) { - printer->Print(variables_, - "$name$MemoizedSerializedSize = dataSize;\n"); - } - - printer->Outdent(); - printer->Print("}\n"); + printer->Annotate("{", "}", descriptor_); } -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + printer->Print(variables_, "\"$name$_\",\n"); } -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { + printer->Print(variables_, "$name$_ = $empty_list$;\n"); } -string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { +std::string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); } diff --git a/src/google/protobuf/compiler/java/java_primitive_field_lite.h b/src/google/protobuf/compiler/java/java_primitive_field_lite.h index c12e458c1f..5867cee746 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field_lite.h +++ b/src/google/protobuf/compiler/java/java_primitive_field_lite.h @@ -69,26 +69,15 @@ class ImmutablePrimitiveFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateBuilderClearCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateBuildingCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; - - string GetBoxedType() const; + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; + std::map variables_; const int messageBitIndex_; - Context* context_; ClassNameResolver* name_resolver_; private: @@ -105,12 +94,9 @@ class ImmutablePrimitiveOneofFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; - void GenerateBuildingCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutablePrimitiveOneofFieldLiteGenerator); @@ -129,26 +115,14 @@ class RepeatedImmutablePrimitiveFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateBuilderClearCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateBuildingCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingCodeFromPacked(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - - - string GetBoxedType() const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; + + std::string GetBoxedType() const; private: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; + std::map variables_; Context* context_; ClassNameResolver* name_resolver_; diff --git a/src/google/protobuf/compiler/java/java_service.cc b/src/google/protobuf/compiler/java/java_service.cc index 53a9017de4..17d8e707c0 100644 --- a/src/google/protobuf/compiler/java/java_service.cc +++ b/src/google/protobuf/compiler/java/java_service.cc @@ -41,21 +41,23 @@ #include #include + namespace google { namespace protobuf { namespace compiler { namespace java { ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor) - : descriptor_(descriptor) {} + : descriptor_(descriptor) {} ServiceGenerator::~ServiceGenerator() {} // =================================================================== ImmutableServiceGenerator::ImmutableServiceGenerator( const ServiceDescriptor* descriptor, Context* context) - : ServiceGenerator(descriptor), context_(context), - name_resolver_(context->GetNameResolver()) {} + : ServiceGenerator(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()) {} ImmutableServiceGenerator::~ImmutableServiceGenerator() {} @@ -65,15 +67,13 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) { MaybePrintGeneratedAnnotation(context_, printer, descriptor_, /* immutable = */ true); printer->Print( - "public $static$ abstract class $classname$\n" - " implements com.google.protobuf.Service {\n", - "static", is_own_file ? "" : "static", - "classname", descriptor_->name()); + "public $static$ abstract class $classname$\n" + " implements com.google.protobuf.Service {\n", + "static", is_own_file ? "" : "static", "classname", descriptor_->name()); printer->Indent(); - printer->Print( - "protected $classname$() {}\n\n", - "classname", descriptor_->name()); + printer->Print("protected $classname$() {}\n\n", "classname", + descriptor_->name()); GenerateInterface(printer); @@ -90,7 +90,7 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) { " return $file$.getDescriptor().getServices().get($index$);\n" "}\n", "file", name_resolver_->GetImmutableClassName(descriptor_->file()), - "index", SimpleItoa(descriptor_->index())); + "index", StrCat(descriptor_->index())); GenerateGetDescriptorForType(printer); // Generate more stuff. @@ -102,9 +102,9 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) { // Add an insertion point. printer->Print( - "\n" - "// @@protoc_insertion_point(class_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(class_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n\n"); @@ -113,10 +113,10 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) { void ImmutableServiceGenerator::GenerateGetDescriptorForType( io::Printer* printer) { printer->Print( - "public final com.google.protobuf.Descriptors.ServiceDescriptor\n" - " getDescriptorForType() {\n" - " return getDescriptor();\n" - "}\n"); + "public final com.google.protobuf.Descriptors.ServiceDescriptor\n" + " getDescriptorForType() {\n" + " return getDescriptor();\n" + "}\n"); } void ImmutableServiceGenerator::GenerateInterface(io::Printer* printer) { @@ -130,10 +130,10 @@ void ImmutableServiceGenerator::GenerateInterface(io::Printer* printer) { void ImmutableServiceGenerator::GenerateNewReflectiveServiceMethod( io::Printer* printer) { printer->Print( - "public static com.google.protobuf.Service newReflectiveService(\n" - " final Interface impl) {\n" - " return new $classname$() {\n", - "classname", descriptor_->name()); + "public static com.google.protobuf.Service newReflectiveService(\n" + " final Interface impl) {\n" + " return new $classname$() {\n", + "classname", descriptor_->name()); printer->Indent(); printer->Indent(); @@ -142,10 +142,10 @@ void ImmutableServiceGenerator::GenerateNewReflectiveServiceMethod( printer->Print("@java.lang.Override\n"); GenerateMethodSignature(printer, method, IS_CONCRETE); printer->Print( - " {\n" - " impl.$method$(controller, request, done);\n" - "}\n\n", - "method", UnderscoresToCamelCase(method)); + " {\n" + " impl.$method$(controller, request, done);\n" + "}\n\n", + "method", UnderscoresToCamelCase(method)); } printer->Outdent(); @@ -157,9 +157,9 @@ void ImmutableServiceGenerator::GenerateNewReflectiveServiceMethod( void ImmutableServiceGenerator::GenerateNewReflectiveBlockingServiceMethod( io::Printer* printer) { printer->Print( - "public static com.google.protobuf.BlockingService\n" - " newReflectiveBlockingService(final BlockingInterface impl) {\n" - " return new com.google.protobuf.BlockingService() {\n"); + "public static com.google.protobuf.BlockingService\n" + " newReflectiveBlockingService(final BlockingInterface impl) {\n" + " return new com.google.protobuf.BlockingService() {\n"); printer->Indent(); printer->Indent(); @@ -184,167 +184,170 @@ void ImmutableServiceGenerator::GenerateAbstractMethods(io::Printer* printer) { } } -string ImmutableServiceGenerator::GetOutput(const MethodDescriptor* method) { +std::string ImmutableServiceGenerator::GetOutput( + const MethodDescriptor* method) { return name_resolver_->GetImmutableClassName(method->output_type()); } void ImmutableServiceGenerator::GenerateCallMethod(io::Printer* printer) { printer->Print( - "\n" - "public final void callMethod(\n" - " com.google.protobuf.Descriptors.MethodDescriptor method,\n" - " com.google.protobuf.RpcController controller,\n" - " com.google.protobuf.Message request,\n" - " com.google.protobuf.RpcCallback<\n" - " com.google.protobuf.Message> done) {\n" - " if (method.getService() != getDescriptor()) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Service.callMethod() given method descriptor for wrong \" +\n" - " \"service type.\");\n" - " }\n" - " switch(method.getIndex()) {\n"); + "\n" + "public final void callMethod(\n" + " com.google.protobuf.Descriptors.MethodDescriptor method,\n" + " com.google.protobuf.RpcController controller,\n" + " com.google.protobuf.Message request,\n" + " com.google.protobuf.RpcCallback<\n" + " com.google.protobuf.Message> done) {\n" + " if (method.getService() != getDescriptor()) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Service.callMethod() given method descriptor for wrong \" +\n" + " \"service type.\");\n" + " }\n" + " switch(method.getIndex()) {\n"); printer->Indent(); printer->Indent(); for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); - std::map vars; - vars["index"] = SimpleItoa(i); + std::map vars; + vars["index"] = StrCat(i); vars["method"] = UnderscoresToCamelCase(method); - vars["input"] = name_resolver_->GetImmutableClassName( - method->input_type()); + vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); - printer->Print(vars, - "case $index$:\n" - " this.$method$(controller, ($input$)request,\n" - " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n" - " done));\n" - " return;\n"); + printer->Print( + vars, + "case $index$:\n" + " this.$method$(controller, ($input$)request,\n" + " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n" + " done));\n" + " return;\n"); } printer->Print( - "default:\n" - " throw new java.lang.AssertionError(\"Can't get here.\");\n"); + "default:\n" + " throw new java.lang.AssertionError(\"Can't get here.\");\n"); printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" - "}\n" - "\n"); + " }\n" + "}\n" + "\n"); } void ImmutableServiceGenerator::GenerateCallBlockingMethod( io::Printer* printer) { printer->Print( - "\n" - "public final com.google.protobuf.Message callBlockingMethod(\n" - " com.google.protobuf.Descriptors.MethodDescriptor method,\n" - " com.google.protobuf.RpcController controller,\n" - " com.google.protobuf.Message request)\n" - " throws com.google.protobuf.ServiceException {\n" - " if (method.getService() != getDescriptor()) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Service.callBlockingMethod() given method descriptor for \" +\n" - " \"wrong service type.\");\n" - " }\n" - " switch(method.getIndex()) {\n"); + "\n" + "public final com.google.protobuf.Message callBlockingMethod(\n" + " com.google.protobuf.Descriptors.MethodDescriptor method,\n" + " com.google.protobuf.RpcController controller,\n" + " com.google.protobuf.Message request)\n" + " throws com.google.protobuf.ServiceException {\n" + " if (method.getService() != getDescriptor()) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Service.callBlockingMethod() given method descriptor for \" +\n" + " \"wrong service type.\");\n" + " }\n" + " switch(method.getIndex()) {\n"); printer->Indent(); printer->Indent(); for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); - std::map vars; - vars["index"] = SimpleItoa(i); + std::map vars; + vars["index"] = StrCat(i); vars["method"] = UnderscoresToCamelCase(method); - vars["input"] = name_resolver_->GetImmutableClassName( - method->input_type()); + vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); printer->Print(vars, - "case $index$:\n" - " return impl.$method$(controller, ($input$)request);\n"); + "case $index$:\n" + " return impl.$method$(controller, ($input$)request);\n"); } printer->Print( - "default:\n" - " throw new java.lang.AssertionError(\"Can't get here.\");\n"); + "default:\n" + " throw new java.lang.AssertionError(\"Can't get here.\");\n"); printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" - "}\n" - "\n"); + " }\n" + "}\n" + "\n"); } void ImmutableServiceGenerator::GenerateGetPrototype(RequestOrResponse which, - io::Printer* printer) { + io::Printer* printer) { /* * TODO(cpovirk): The exception message says "Service.foo" when it may be * "BlockingService.foo." Consider fixing. */ printer->Print( - "public final com.google.protobuf.Message\n" - " get$request_or_response$Prototype(\n" - " com.google.protobuf.Descriptors.MethodDescriptor method) {\n" - " if (method.getService() != getDescriptor()) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Service.get$request_or_response$Prototype() given method \" +\n" - " \"descriptor for wrong service type.\");\n" - " }\n" - " switch(method.getIndex()) {\n", - "request_or_response", (which == REQUEST) ? "Request" : "Response"); + "public final com.google.protobuf.Message\n" + " get$request_or_response$Prototype(\n" + " com.google.protobuf.Descriptors.MethodDescriptor method) {\n" + " if (method.getService() != getDescriptor()) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Service.get$request_or_response$Prototype() given method \" +\n" + " \"descriptor for wrong service type.\");\n" + " }\n" + " switch(method.getIndex()) {\n", + "request_or_response", (which == REQUEST) ? "Request" : "Response"); printer->Indent(); printer->Indent(); for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); - std::map vars; - vars["index"] = SimpleItoa(i); - vars["type"] = name_resolver_->GetImmutableClassName( - (which == REQUEST) ? method->input_type() : method->output_type()); + std::map vars; + vars["index"] = StrCat(i); + vars["type"] = + (which == REQUEST) + ? name_resolver_->GetImmutableClassName(method->input_type()) + : GetOutput(method); printer->Print(vars, - "case $index$:\n" - " return $type$.getDefaultInstance();\n"); + "case $index$:\n" + " return $type$.getDefaultInstance();\n"); } printer->Print( - "default:\n" - " throw new java.lang.AssertionError(\"Can't get here.\");\n"); + "default:\n" + " throw new java.lang.AssertionError(\"Can't get here.\");\n"); printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" - "}\n" - "\n"); + " }\n" + "}\n" + "\n"); } void ImmutableServiceGenerator::GenerateStub(io::Printer* printer) { printer->Print( - "public static Stub newStub(\n" - " com.google.protobuf.RpcChannel channel) {\n" - " return new Stub(channel);\n" - "}\n" - "\n" - "public static final class Stub extends $classname$ implements Interface {" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public static Stub newStub(\n" + " com.google.protobuf.RpcChannel channel) {\n" + " return new Stub(channel);\n" + "}\n" + "\n" + "public static final class Stub extends $classname$ implements Interface " + "{" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Indent(); printer->Print( - "private Stub(com.google.protobuf.RpcChannel channel) {\n" - " this.channel = channel;\n" - "}\n" - "\n" - "private final com.google.protobuf.RpcChannel channel;\n" - "\n" - "public com.google.protobuf.RpcChannel getChannel() {\n" - " return channel;\n" - "}\n"); + "private Stub(com.google.protobuf.RpcChannel channel) {\n" + " this.channel = channel;\n" + "}\n" + "\n" + "private final com.google.protobuf.RpcChannel channel;\n" + "\n" + "public com.google.protobuf.RpcChannel getChannel() {\n" + " return channel;\n" + "}\n"); for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); @@ -353,19 +356,19 @@ void ImmutableServiceGenerator::GenerateStub(io::Printer* printer) { printer->Print(" {\n"); printer->Indent(); - std::map vars; - vars["index"] = SimpleItoa(i); + std::map vars; + vars["index"] = StrCat(i); vars["output"] = GetOutput(method); printer->Print(vars, - "channel.callMethod(\n" - " getDescriptor().getMethods().get($index$),\n" - " controller,\n" - " request,\n" - " $output$.getDefaultInstance(),\n" - " com.google.protobuf.RpcUtil.generalizeCallback(\n" - " done,\n" - " $output$.class,\n" - " $output$.getDefaultInstance()));\n"); + "channel.callMethod(\n" + " getDescriptor().getMethods().get($index$),\n" + " controller,\n" + " request,\n" + " $output$.getDefaultInstance(),\n" + " com.google.protobuf.RpcUtil.generalizeCallback(\n" + " done,\n" + " $output$.class,\n" + " $output$.getDefaultInstance()));\n"); printer->Outdent(); printer->Print("}\n"); @@ -373,20 +376,19 @@ void ImmutableServiceGenerator::GenerateStub(io::Printer* printer) { printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); } void ImmutableServiceGenerator::GenerateBlockingStub(io::Printer* printer) { printer->Print( - "public static BlockingInterface newBlockingStub(\n" - " com.google.protobuf.BlockingRpcChannel channel) {\n" - " return new BlockingStub(channel);\n" - "}\n" - "\n"); + "public static BlockingInterface newBlockingStub(\n" + " com.google.protobuf.BlockingRpcChannel channel) {\n" + " return new BlockingStub(channel);\n" + "}\n" + "\n"); - printer->Print( - "public interface BlockingInterface {"); + printer->Print("public interface BlockingInterface {"); printer->Indent(); for (int i = 0; i < descriptor_->method_count(); i++) { @@ -397,19 +399,20 @@ void ImmutableServiceGenerator::GenerateBlockingStub(io::Printer* printer) { printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); printer->Print( - "private static final class BlockingStub implements BlockingInterface {\n"); + "private static final class BlockingStub implements BlockingInterface " + "{\n"); printer->Indent(); printer->Print( - "private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) {\n" - " this.channel = channel;\n" - "}\n" - "\n" - "private final com.google.protobuf.BlockingRpcChannel channel;\n"); + "private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) {\n" + " this.channel = channel;\n" + "}\n" + "\n" + "private final com.google.protobuf.BlockingRpcChannel channel;\n"); for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); @@ -417,54 +420,53 @@ void ImmutableServiceGenerator::GenerateBlockingStub(io::Printer* printer) { printer->Print(" {\n"); printer->Indent(); - std::map vars; - vars["index"] = SimpleItoa(i); + std::map vars; + vars["index"] = StrCat(i); vars["output"] = GetOutput(method); printer->Print(vars, - "return ($output$) channel.callBlockingMethod(\n" - " getDescriptor().getMethods().get($index$),\n" - " controller,\n" - " request,\n" - " $output$.getDefaultInstance());\n"); + "return ($output$) channel.callBlockingMethod(\n" + " getDescriptor().getMethods().get($index$),\n" + " controller,\n" + " request,\n" + " $output$.getDefaultInstance());\n"); printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); } printer->Outdent(); printer->Print("}\n"); } -void ImmutableServiceGenerator::GenerateMethodSignature(io::Printer* printer, - const MethodDescriptor* method, - IsAbstract is_abstract) { - std::map vars; +void ImmutableServiceGenerator::GenerateMethodSignature( + io::Printer* printer, const MethodDescriptor* method, + IsAbstract is_abstract) { + std::map vars; vars["name"] = UnderscoresToCamelCase(method); vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); vars["abstract"] = (is_abstract == IS_ABSTRACT) ? "abstract" : ""; printer->Print(vars, - "public $abstract$ void $name$(\n" - " com.google.protobuf.RpcController controller,\n" - " $input$ request,\n" - " com.google.protobuf.RpcCallback<$output$> done)"); + "public $abstract$ void $name$(\n" + " com.google.protobuf.RpcController controller,\n" + " $input$ request,\n" + " com.google.protobuf.RpcCallback<$output$> done)"); } void ImmutableServiceGenerator::GenerateBlockingMethodSignature( - io::Printer* printer, - const MethodDescriptor* method) { - std::map vars; + io::Printer* printer, const MethodDescriptor* method) { + std::map vars; vars["method"] = UnderscoresToCamelCase(method); vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); printer->Print(vars, - "\n" - "public $output$ $method$(\n" - " com.google.protobuf.RpcController controller,\n" - " $input$ request)\n" - " throws com.google.protobuf.ServiceException"); + "\n" + "public $output$ $method$(\n" + " com.google.protobuf.RpcController controller,\n" + " $input$ request)\n" + " throws com.google.protobuf.ServiceException"); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_service.h b/src/google/protobuf/compiler/java/java_service.h index f34cd798ff..1c82c16c1c 100644 --- a/src/google/protobuf/compiler/java/java_service.h +++ b/src/google/protobuf/compiler/java/java_service.h @@ -83,7 +83,6 @@ class ImmutableServiceGenerator : public ServiceGenerator { virtual void Generate(io::Printer* printer); private: - // Generate the getDescriptorForType() method. void GenerateGetDescriptorForType(io::Printer* printer); @@ -125,7 +124,7 @@ class ImmutableServiceGenerator : public ServiceGenerator { const MethodDescriptor* method); // Return the output type of the method. - string GetOutput(const MethodDescriptor* method); + std::string GetOutput(const MethodDescriptor* method); Context* context_; ClassNameResolver* name_resolver_; diff --git a/src/google/protobuf/compiler/java/java_shared_code_generator.cc b/src/google/protobuf/compiler/java/java_shared_code_generator.cc index 0cec20b97b..f673e68301 100644 --- a/src/google/protobuf/compiler/java/java_shared_code_generator.cc +++ b/src/google/protobuf/compiler/java/java_shared_code_generator.cc @@ -52,19 +52,18 @@ SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file, const Options& options) : name_resolver_(new ClassNameResolver), file_(file), options_(options) {} -SharedCodeGenerator::~SharedCodeGenerator() { -} +SharedCodeGenerator::~SharedCodeGenerator() {} -void SharedCodeGenerator::Generate(GeneratorContext* context, - std::vector* file_list, - std::vector* annotation_file_list) { - string java_package = FileJavaPackage(file_); - string package_dir = JavaPackageToDir(java_package); +void SharedCodeGenerator::Generate( + GeneratorContext* context, std::vector* file_list, + std::vector* annotation_file_list) { + std::string java_package = FileJavaPackage(file_); + std::string package_dir = JavaPackageToDir(java_package); if (HasDescriptorMethods(file_, options_.enforce_lite)) { // Generate descriptors. - string classname = name_resolver_->GetDescriptorClassName(file_); - string filename = package_dir + classname + ".java"; + std::string classname = name_resolver_->GetDescriptorClassName(file_); + std::string filename = package_dir + classname + ".java"; file_list->push_back(filename); std::unique_ptr output(context->Open(filename)); GeneratedCodeInfo annotations; @@ -73,8 +72,8 @@ void SharedCodeGenerator::Generate(GeneratorContext* context, std::unique_ptr printer( new io::Printer(output.get(), '$', options_.annotate_code ? &annotation_collector : NULL)); - string info_relative_path = classname + ".java.pb.meta"; - string info_full_path = filename + ".pb.meta"; + std::string info_relative_path = classname + ".java.pb.meta"; + std::string info_full_path = filename + ".pb.meta"; printer->Print( "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" "// source: $filename$\n" @@ -82,9 +81,9 @@ void SharedCodeGenerator::Generate(GeneratorContext* context, "filename", file_->name()); if (!java_package.empty()) { printer->Print( - "package $package$;\n" - "\n", - "package", java_package); + "package $package$;\n" + "\n", + "package", java_package); } PrintGeneratedAnnotation(printer.get(), '$', options_.annotate_code ? info_relative_path : ""); @@ -101,8 +100,8 @@ void SharedCodeGenerator::Generate(GeneratorContext* context, printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" - "}\n"); + " }\n" + "}\n"); if (options_.annotate_code) { std::unique_ptr info_output( @@ -130,11 +129,10 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { FileDescriptorProto file_proto; file_->CopyTo(&file_proto); - string file_data; + std::string file_data; file_proto.SerializeToString(&file_data); - printer->Print( - "java.lang.String[] descriptorData = {\n"); + printer->Print("java.lang.String[] descriptorData = {\n"); printer->Indent(); // Limit the number of bytes per line. @@ -152,40 +150,22 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { printer->Print(" +\n"); } } - printer->Print("\"$data$\"", - "data", CEscape(file_data.substr(i, kBytesPerLine))); + printer->Print("\"$data$\"", "data", + CEscape(file_data.substr(i, kBytesPerLine))); } printer->Outdent(); printer->Print("\n};\n"); - // ----------------------------------------------------------------- - // Create the InternalDescriptorAssigner. - - printer->Print( - "com.google.protobuf.Descriptors.FileDescriptor." - "InternalDescriptorAssigner assigner =\n" - " new com.google.protobuf.Descriptors.FileDescriptor." - " InternalDescriptorAssigner() {\n" - " public com.google.protobuf.ExtensionRegistry assignDescriptors(\n" - " com.google.protobuf.Descriptors.FileDescriptor root) {\n" - " descriptor = root;\n" - // Custom options will be handled when immutable messages' outer class is - // loaded. Here we just return null and let custom options be unknown - // fields. - " return null;\n" - " }\n" - " };\n"); - // ----------------------------------------------------------------- // Find out all dependencies. - std::vector > dependencies; + std::vector > dependencies; for (int i = 0; i < file_->dependency_count(); i++) { - string filename = file_->dependency(i)->name(); - string package = FileJavaPackage(file_->dependency(i)); - string classname = name_resolver_->GetDescriptorClassName( - file_->dependency(i)); - string full_name; + std::string filename = file_->dependency(i)->name(); + std::string package = FileJavaPackage(file_->dependency(i)); + std::string classname = + name_resolver_->GetDescriptorClassName(file_->dependency(i)); + std::string full_name; if (package.empty()) { full_name = classname; } else { @@ -197,20 +177,18 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { // ----------------------------------------------------------------- // Invoke internalBuildGeneratedFileFrom() to build the file. printer->Print( - "com.google.protobuf.Descriptors.FileDescriptor\n" - " .internalBuildGeneratedFileFrom(descriptorData,\n"); + "descriptor = com.google.protobuf.Descriptors.FileDescriptor\n" + " .internalBuildGeneratedFileFrom(descriptorData,\n"); printer->Print( - " new com.google.protobuf.Descriptors.FileDescriptor[] {\n"); + " new com.google.protobuf.Descriptors.FileDescriptor[] {\n"); for (int i = 0; i < dependencies.size(); i++) { - const string& dependency = dependencies[i].second; - printer->Print( - " $dependency$.getDescriptor(),\n", - "dependency", dependency); + const std::string& dependency = dependencies[i].second; + printer->Print(" $dependency$.getDescriptor(),\n", "dependency", + dependency); } - printer->Print( - " }, assigner);\n"); + printer->Print(" });\n"); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_shared_code_generator.h b/src/google/protobuf/compiler/java/java_shared_code_generator.h index 1eb6feafdd..7c79abe2e1 100644 --- a/src/google/protobuf/compiler/java/java_shared_code_generator.h +++ b/src/google/protobuf/compiler/java/java_shared_code_generator.h @@ -70,8 +70,8 @@ class SharedCodeGenerator { ~SharedCodeGenerator(); void Generate(GeneratorContext* generator_context, - std::vector* file_list, - std::vector* annotation_file_list); + std::vector* file_list, + std::vector* annotation_file_list); void GenerateDescriptors(io::Printer* printer); @@ -82,7 +82,6 @@ class SharedCodeGenerator { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SharedCodeGenerator); }; - } // namespace java } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/java/java_string_field.cc b/src/google/protobuf/compiler/java/java_string_field.cc index 2b6e938180..2a828816cd 100644 --- a/src/google/protobuf/compiler/java/java_string_field.cc +++ b/src/google/protobuf/compiler/java/java_string_field.cc @@ -47,6 +47,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -58,11 +59,10 @@ using internal::WireFormatLite; namespace { void SetPrimitiveVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, + int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); (*variables)["empty_list"] = "com.google.protobuf.LazyStringArrayList.EMPTY"; @@ -72,24 +72,23 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, "= " + ImmutableDefaultValue(descriptor, name_resolver); (*variables)["capitalized_type"] = "String"; (*variables)["tag"] = - SimpleItoa(static_cast(WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = SimpleItoa( + StrCat(static_cast(WireFormat::MakeTag(descriptor))); + (*variables)["tag_size"] = StrCat( WireFormat::TagSize(descriptor->number(), GetType(descriptor))); (*variables)["null_check"] = " if (value == null) {\n" " throw new NullPointerException();\n" " }\n"; - (*variables)["writeString"] = - "com.google.protobuf.GeneratedMessage" + GeneratedCodeVersionSuffix() + - ".writeString"; - (*variables)["computeStringSize"] = - "com.google.protobuf.GeneratedMessage" + GeneratedCodeVersionSuffix() + - ".computeStringSize"; + (*variables)["writeString"] = "com.google.protobuf.GeneratedMessage" + + GeneratedCodeVersionSuffix() + ".writeString"; + (*variables)["computeStringSize"] = "com.google.protobuf.GeneratedMessage" + + GeneratedCodeVersionSuffix() + + ".computeStringSize"; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["on_changed"] = "onChanged();"; if (SupportFieldPresence(descriptor->file())) { @@ -137,14 +136,10 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, // =================================================================== -ImmutableStringFieldGenerator:: -ImmutableStringFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +ImmutableStringFieldGenerator::ImmutableStringFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); @@ -153,11 +148,11 @@ ImmutableStringFieldGenerator(const FieldDescriptor* descriptor, ImmutableStringFieldGenerator::~ImmutableStringFieldGenerator() {} int ImmutableStringFieldGenerator::GetNumBitsForMessage() const { - return 1; + return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } int ImmutableStringFieldGenerator::GetNumBitsForBuilder() const { - return 1; + return GetNumBitsForMessage(); } // A note about how strings are handled. This code used to just store a String @@ -192,534 +187,537 @@ int ImmutableStringFieldGenerator::GetNumBitsForBuilder() const { // For single fields, the logic for this is done inside the generated code. For // repeated fields, the logic is done in LazyStringArrayList and // UnmodifiableLazyStringList. -void ImmutableStringFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$java.lang.String get$capitalized_name$();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$java.lang.String get$capitalized_name$();\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes();\n"); + "$deprecation$com.google.protobuf.ByteString\n" + " get$capitalized_name$Bytes();\n"); } -void ImmutableStringFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private volatile java.lang.Object $name$_;\n"); +void ImmutableStringFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private volatile java.lang.Object $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.Object ref = $name$_;\n" - " if (ref instanceof java.lang.String) {\n" - " return (java.lang.String) ref;\n" - " } else {\n" - " com.google.protobuf.ByteString bs = \n" - " (com.google.protobuf.ByteString) ref;\n" + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.Object ref = $name$_;\n" + " if (ref instanceof java.lang.String) {\n" + " return (java.lang.String) ref;\n" + " } else {\n" + " com.google.protobuf.ByteString bs = \n" + " (com.google.protobuf.ByteString) ref;\n" " java.lang.String s = bs.toStringUtf8();\n"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " $name$_ = s;\n"); + printer->Print(variables_, " $name$_ = s;\n"); } else { printer->Print(variables_, - " if (bs.isValidUtf8()) {\n" - " $name$_ = s;\n" - " }\n"); + " if (bs.isValidUtf8()) {\n" + " $name$_ = s;\n" + " }\n"); } printer->Print(variables_, - " return s;\n" - " }\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.Object ref = $name$_;\n" - " if (ref instanceof java.lang.String) {\n" - " com.google.protobuf.ByteString b = \n" - " com.google.protobuf.ByteString.copyFromUtf8(\n" - " (java.lang.String) ref);\n" - " $name$_ = b;\n" - " return b;\n" - " } else {\n" - " return (com.google.protobuf.ByteString) ref;\n" - " }\n" - "}\n"); + " return s;\n" + " }\n" + "}\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.Object ref = $name$_;\n" + " if (ref instanceof java.lang.String) {\n" + " com.google.protobuf.ByteString b = \n" + " com.google.protobuf.ByteString.copyFromUtf8(\n" + " (java.lang.String) ref);\n" + " $name$_ = b;\n" + " return b;\n" + " } else {\n" + " return (com.google.protobuf.ByteString) ref;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableStringFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { printer->Print(variables_, - "private java.lang.Object $name$_ $default_init$;\n"); + "private java.lang.Object $name$_ $default_init$;\n"); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_builder$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_builder$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.Object ref = $name$_;\n" - " if (!(ref instanceof java.lang.String)) {\n" - " com.google.protobuf.ByteString bs =\n" - " (com.google.protobuf.ByteString) ref;\n" - " java.lang.String s = bs.toStringUtf8();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.Object ref = $name$_;\n" + " if (!(ref instanceof java.lang.String)) {\n" + " com.google.protobuf.ByteString bs =\n" + " (com.google.protobuf.ByteString) ref;\n" + " java.lang.String s = bs.toStringUtf8();\n"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " $name$_ = s;\n"); + printer->Print(variables_, " $name$_ = s;\n"); } else { printer->Print(variables_, - " if (bs.isValidUtf8()) {\n" - " $name$_ = s;\n" - " }\n"); + " if (bs.isValidUtf8()) {\n" + " $name$_ = s;\n" + " }\n"); } printer->Print(variables_, - " return s;\n" - " } else {\n" - " return (java.lang.String) ref;\n" - " }\n" - "}\n"); - - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.Object ref = $name$_;\n" - " if (ref instanceof String) {\n" - " com.google.protobuf.ByteString b = \n" - " com.google.protobuf.ByteString.copyFromUtf8(\n" - " (java.lang.String) ref);\n" - " $name$_ = b;\n" - " return b;\n" - " } else {\n" - " return (com.google.protobuf.ByteString) ref;\n" - " }\n" - "}\n"); + " return s;\n" + " } else {\n" + " return (java.lang.String) ref;\n" + " }\n" + "}\n"); + + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.Object ref = $name$_;\n" + " if (ref instanceof String) {\n" + " com.google.protobuf.ByteString b = \n" + " com.google.protobuf.ByteString.copyFromUtf8(\n" + " (java.lang.String) ref);\n" + " $name$_ = b;\n" + " return b;\n" + " } else {\n" + " return (com.google.protobuf.ByteString) ref;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - "$null_check$" - " $set_has_field_bit_builder$\n" - " $name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + "$null_check$" + " $set_has_field_bit_builder$\n" + " $name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $clear_has_field_bit_builder$\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $clear_has_field_bit_builder$\n"); printer->Annotate("{", "}", descriptor_); // The default value is not a simple literal so we want to avoid executing // it multiple times. Instead, get the default out of the default instance. printer->Print(variables_, - " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); + " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); printer->Print(variables_, - " $on_changed$\n" - " return this;\n" - "}\n"); + " $on_changed$\n" + " return this;\n" + "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " $set_has_field_bit_builder$\n" - " $name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + " $set_has_field_bit_builder$\n" + " $name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); } -void ImmutableStringFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { // noop for primitives } -void ImmutableStringFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $default$;\n"); } -void ImmutableStringFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $default$;\n" - "$clear_has_field_bit_builder$\n"); + "$name$_ = $default$;\n" + "$clear_has_field_bit_builder$\n"); } -void ImmutableStringFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { // Allow a slight breach of abstraction here in order to avoid forcing // all string fields to Strings when copying fields from a Message. printer->Print(variables_, - "if (other.has$capitalized_name$()) {\n" - " $set_has_field_bit_builder$\n" - " $name$_ = other.$name$_;\n" - " $on_changed$\n" - "}\n"); + "if (other.has$capitalized_name$()) {\n" + " $set_has_field_bit_builder$\n" + " $name$_ = other.$name$_;\n" + " $on_changed$\n" + "}\n"); } else { printer->Print(variables_, - "if (!other.get$capitalized_name$().isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $on_changed$\n" - "}\n"); + "if (!other.get$capitalized_name$().isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $on_changed$\n" + "}\n"); } } -void ImmutableStringFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, - "if ($get_has_field_bit_from_local$) {\n" - " $set_has_field_bit_to_local$;\n" - "}\n"); + "if ($get_has_field_bit_from_local$) {\n" + " $set_has_field_bit_to_local$;\n" + "}\n"); } - printer->Print(variables_, - "result.$name$_ = $name$_;\n"); + printer->Print(variables_, "result.$name$_ = $name$_;\n"); } -void ImmutableStringFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n" - "$set_has_field_bit_message$\n" - "$name$_ = s;\n"); + "java.lang.String s = input.readStringRequireUtf8();\n" + "$set_has_field_bit_message$\n" + "$name$_ = s;\n"); } else { printer->Print(variables_, - "com.google.protobuf.ByteString bs = input.readBytes();\n" - "$set_has_field_bit_message$\n" - "$name$_ = bs;\n"); + "com.google.protobuf.ByteString bs = input.readBytes();\n" + "$set_has_field_bit_message$\n" + "$name$_ = bs;\n"); } } -void ImmutableStringFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // noop for strings. } -void ImmutableStringFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " $writeString$(output, $number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " $writeString$(output, $number$, $name$_);\n" + "}\n"); } -void ImmutableStringFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += $computeStringSize$($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " size += $computeStringSize$($number$, $name$_);\n" + "}\n"); } -void ImmutableStringFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); + "if (!get$capitalized_name$()\n" + " .equals(other.get$capitalized_name$())) return false;\n"); } -void ImmutableStringFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print(variables_, "hash = (37 * hash) + $constant_name$;\n"); printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n"); - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); + "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); } -string ImmutableStringFieldGenerator::GetBoxedType() const { +std::string ImmutableStringFieldGenerator::GetBoxedType() const { return "java.lang.String"; } // =================================================================== -ImmutableStringOneofFieldGenerator:: -ImmutableStringOneofFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : ImmutableStringFieldGenerator( - descriptor, messageBitIndex, builderBitIndex, context) { +ImmutableStringOneofFieldGenerator::ImmutableStringOneofFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : ImmutableStringFieldGenerator(descriptor, messageBitIndex, + builderBitIndex, context) { const OneofGeneratorInfo* info = context->GetOneofGeneratorInfo(descriptor->containing_oneof()); SetCommonOneofVariables(descriptor, info, &variables_); } -ImmutableStringOneofFieldGenerator:: -~ImmutableStringOneofFieldGenerator() {} +ImmutableStringOneofFieldGenerator::~ImmutableStringOneofFieldGenerator() {} -void ImmutableStringOneofFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.Object ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = $oneof_name$_;\n" - " }\n" - " if (ref instanceof java.lang.String) {\n" - " return (java.lang.String) ref;\n" - " } else {\n" - " com.google.protobuf.ByteString bs = \n" - " (com.google.protobuf.ByteString) ref;\n" - " java.lang.String s = bs.toStringUtf8();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.Object ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = $oneof_name$_;\n" + " }\n" + " if (ref instanceof java.lang.String) {\n" + " return (java.lang.String) ref;\n" + " } else {\n" + " com.google.protobuf.ByteString bs = \n" + " (com.google.protobuf.ByteString) ref;\n" + " java.lang.String s = bs.toStringUtf8();\n"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, - " if ($has_oneof_case_message$) {\n" - " $oneof_name$_ = s;\n" - " }\n"); + " if ($has_oneof_case_message$) {\n" + " $oneof_name$_ = s;\n" + " }\n"); } else { printer->Print(variables_, - " if (bs.isValidUtf8() && ($has_oneof_case_message$)) {\n" - " $oneof_name$_ = s;\n" - " }\n"); + " if (bs.isValidUtf8() && ($has_oneof_case_message$)) {\n" + " $oneof_name$_ = s;\n" + " }\n"); } printer->Print(variables_, - " return s;\n" - " }\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - - printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.Object ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = $oneof_name$_;\n" - " }\n" - " if (ref instanceof java.lang.String) {\n" - " com.google.protobuf.ByteString b = \n" - " com.google.protobuf.ByteString.copyFromUtf8(\n" - " (java.lang.String) ref);\n" - " if ($has_oneof_case_message$) {\n" - " $oneof_name$_ = b;\n" - " }\n" - " return b;\n" - " } else {\n" - " return (com.google.protobuf.ByteString) ref;\n" - " }\n" - "}\n"); + " return s;\n" + " }\n" + "}\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); + + printer->Print(variables_, + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.Object ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = $oneof_name$_;\n" + " }\n" + " if (ref instanceof java.lang.String) {\n" + " com.google.protobuf.ByteString b = \n" + " com.google.protobuf.ByteString.copyFromUtf8(\n" + " (java.lang.String) ref);\n" + " if ($has_oneof_case_message$) {\n" + " $oneof_name$_ = b;\n" + " }\n" + " return b;\n" + " } else {\n" + " return (com.google.protobuf.ByteString) ref;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableStringOneofFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.Object ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = $oneof_name$_;\n" - " }\n" - " if (!(ref instanceof java.lang.String)) {\n" - " com.google.protobuf.ByteString bs =\n" - " (com.google.protobuf.ByteString) ref;\n" - " java.lang.String s = bs.toStringUtf8();\n" - " if ($has_oneof_case_message$) {\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.Object ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = $oneof_name$_;\n" + " }\n" + " if (!(ref instanceof java.lang.String)) {\n" + " com.google.protobuf.ByteString bs =\n" + " (com.google.protobuf.ByteString) ref;\n" + " java.lang.String s = bs.toStringUtf8();\n" + " if ($has_oneof_case_message$) {\n"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " $oneof_name$_ = s;\n"); + printer->Print(variables_, " $oneof_name$_ = s;\n"); } else { printer->Print(variables_, - " if (bs.isValidUtf8()) {\n" - " $oneof_name$_ = s;\n" - " }\n"); + " if (bs.isValidUtf8()) {\n" + " $oneof_name$_ = s;\n" + " }\n"); } printer->Print(variables_, - " }\n" - " return s;\n" - " } else {\n" - " return (java.lang.String) ref;\n" - " }\n" - "}\n"); - - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.Object ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = $oneof_name$_;\n" - " }\n" - " if (ref instanceof String) {\n" - " com.google.protobuf.ByteString b = \n" - " com.google.protobuf.ByteString.copyFromUtf8(\n" - " (java.lang.String) ref);\n" - " if ($has_oneof_case_message$) {\n" - " $oneof_name$_ = b;\n" - " }\n" - " return b;\n" - " } else {\n" - " return (com.google.protobuf.ByteString) ref;\n" - " }\n" - "}\n"); + " }\n" + " return s;\n" + " } else {\n" + " return (java.lang.String) ref;\n" + " }\n" + "}\n"); + + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.Object ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = $oneof_name$_;\n" + " }\n" + " if (ref instanceof String) {\n" + " com.google.protobuf.ByteString b = \n" + " com.google.protobuf.ByteString.copyFromUtf8(\n" + " (java.lang.String) ref);\n" + " if ($has_oneof_case_message$) {\n" + " $oneof_name$_ = b;\n" + " }\n" + " return b;\n" + " } else {\n" + " return (com.google.protobuf.ByteString) ref;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - "$null_check$" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + "$null_check$" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " $on_changed$\n" - " }\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " $on_changed$\n" + " }\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); } -void ImmutableStringOneofFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // Allow a slight breach of abstraction here in order to avoid forcing // all string fields to Strings when copying fields from a Message. printer->Print(variables_, - "$set_oneof_case_message$;\n" - "$oneof_name$_ = other.$oneof_name$_;\n" - "$on_changed$\n"); + "$set_oneof_case_message$;\n" + "$oneof_name$_ = other.$oneof_name$_;\n" + "$on_changed$\n"); } -void ImmutableStringOneofFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " result.$oneof_name$_ = $oneof_name$_;\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " result.$oneof_name$_ = $oneof_name$_;\n" + "}\n"); } -void ImmutableStringOneofFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = s;\n"); + "java.lang.String s = input.readStringRequireUtf8();\n" + "$set_oneof_case_message$;\n" + "$oneof_name$_ = s;\n"); } else { printer->Print(variables_, - "com.google.protobuf.ByteString bs = input.readBytes();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = bs;\n"); + "com.google.protobuf.ByteString bs = input.readBytes();\n" + "$set_oneof_case_message$;\n" + "$oneof_name$_ = bs;\n"); } } -void ImmutableStringOneofFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " $writeString$(output, $number$, $oneof_name$_);\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " $writeString$(output, $number$, $oneof_name$_);\n" + "}\n"); } -void ImmutableStringOneofFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += $computeStringSize$($number$, $oneof_name$_);\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " size += $computeStringSize$($number$, $oneof_name$_);\n" + "}\n"); } // =================================================================== -RepeatedImmutableStringFieldGenerator:: -RepeatedImmutableStringFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +RepeatedImmutableStringFieldGenerator::RepeatedImmutableStringFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); } RepeatedImmutableStringFieldGenerator:: -~RepeatedImmutableStringFieldGenerator() {} + ~RepeatedImmutableStringFieldGenerator() {} int RepeatedImmutableStringFieldGenerator::GetNumBitsForMessage() const { return 0; @@ -729,67 +727,71 @@ int RepeatedImmutableStringFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void RepeatedImmutableStringFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - // NOTE: the same method in the implementation class actually returns - // com.google.protobuf.ProtocolStringList (a subclass of List). It's - // changed between protobuf 2.5.0 release and protobuf 2.6.1 release. - // To retain binary compatibility with both 2.5.0 and 2.6.1 generated - // code, we make this interface method return List so both methods - // with different return types exist in the compiled byte code. - "$deprecation$java.util.List\n" - " get$capitalized_name$List();\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$java.lang.String get$capitalized_name$(int index);\n"); - WriteFieldDocComment(printer, descriptor_); +void RepeatedImmutableStringFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); + printer->Print( + variables_, + // NOTE: the same method in the implementation class actually returns + // com.google.protobuf.ProtocolStringList (a subclass of List). It's + // changed between protobuf 2.5.0 release and protobuf 2.6.1 release. + // To retain binary compatibility with both 2.5.0 and 2.6.1 generated + // code, we make this interface method return List so both methods + // with different return types exist in the compiled byte code. + "$deprecation$java.util.List\n" + " get$capitalized_name$List();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print(variables_, + "$deprecation$int get$capitalized_name$Count();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "$deprecation$java.lang.String get$capitalized_name$(int index);\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes(int index);\n"); + "$deprecation$com.google.protobuf.ByteString\n" + " get$capitalized_name$Bytes(int index);\n"); } - -void RepeatedImmutableStringFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateMembers( + io::Printer* printer) const { printer->Print(variables_, - "private com.google.protobuf.LazyStringList $name$_;\n"); + "private com.google.protobuf.LazyStringList $name$_;\n"); PrintExtraFieldInfo(variables_, printer); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ProtocolStringList\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "$deprecation$public com.google.protobuf.ProtocolStringList\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$public java.lang.String " - "${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$(int index) {\n" - " return $name$_.getByteString(index);\n" - "}\n"); + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" + " return $name$_.getByteString(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void RepeatedImmutableStringFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // One field is the list and the bit field keeps track of whether the // list is immutable. If it's immutable, the invariant is that it must // either an instance of Collections.emptyList() or it's an ArrayList @@ -799,237 +801,245 @@ GenerateBuilderMembers(io::Printer* printer) const { // memory allocations. Note, immutable is a strong guarantee here -- not // just that the list cannot be modified via the reference but that the // list can never be modified. - printer->Print(variables_, - "private com.google.protobuf.LazyStringList $name$_ = $empty_list$;\n"); + printer->Print( + variables_, + "private com.google.protobuf.LazyStringList $name$_ = $empty_list$;\n"); + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$get_mutable_bit_builder$) {\n" + " $name$_ = new com.google.protobuf.LazyStringArrayList($name$_);\n" + " $set_mutable_bit_builder$;\n" + " }\n" + "}\n"); + + // Note: We return an unmodifiable list because otherwise the caller + // could hold on to the returned list and modify it after the message + // has been built, thus mutating the message which is supposed to be + // immutable. + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$get_mutable_bit_builder$) {\n" - " $name$_ = new com.google.protobuf.LazyStringArrayList($name$_);\n" - " $set_mutable_bit_builder$;\n" - " }\n" - "}\n"); - - // Note: We return an unmodifiable list because otherwise the caller - // could hold on to the returned list and modify it after the message - // has been built, thus mutating the message which is supposed to be - // immutable. - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public com.google.protobuf.ProtocolStringList\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $name$_.getUnmodifiableView();\n" - "}\n"); + "$deprecation$public com.google.protobuf.ProtocolStringList\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $name$_.getUnmodifiableView();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$public java.lang.String " - "${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, LIST_ADDER); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$(int index) {\n" - " return $name$_.getByteString(index);\n" - "}\n"); + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" + " return $name$_.getByteString(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, java.lang.String value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, java.lang.String value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" - " values, $name$_);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" + " values, $name$_);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $name$_ = $empty_list$;\n" - " $clear_mutable_bit_builder$;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $name$_ = $empty_list$;\n" + " $clear_mutable_bit_builder$;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); } void RepeatedImmutableStringFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for primitives } -void RepeatedImmutableStringFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $empty_list$;\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $empty_list$;\n" - "$clear_mutable_bit_builder$;\n"); + "$name$_ = $empty_list$;\n" + "$clear_mutable_bit_builder$;\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // The code below does two optimizations: // 1. If the other list is empty, there's nothing to do. This ensures we // don't allocate a new array if we already have an immutable one. // 2. If the other list is non-empty and our current list is empty, we can // reuse the other list which is guaranteed to be immutable. printer->Print(variables_, - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$_.isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " } else {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addAll(other.$name$_);\n" - " }\n" - " $on_changed$\n" - "}\n"); + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$_.isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " } else {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addAll(other.$name$_);\n" + " }\n" + " $on_changed$\n" + "}\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { // The code below ensures that the result has an immutable list. If our // list is immutable, we can just reuse it. If not, we make it immutable. printer->Print(variables_, - "if ($get_mutable_bit_builder$) {\n" - " $name$_ = $name$_.getUnmodifiableView();\n" - " $clear_mutable_bit_builder$;\n" - "}\n" - "result.$name$_ = $name$_;\n"); + "if ($get_mutable_bit_builder$) {\n" + " $name$_ = $name$_.getUnmodifiableView();\n" + " $clear_mutable_bit_builder$;\n" + "}\n" + "result.$name$_ = $name$_;\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n"); + "java.lang.String s = input.readStringRequireUtf8();\n"); } else { printer->Print(variables_, - "com.google.protobuf.ByteString bs = input.readBytes();\n"); + "com.google.protobuf.ByteString bs = input.readBytes();\n"); } printer->Print(variables_, - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = new com.google.protobuf.LazyStringArrayList();\n" - " $set_mutable_bit_parser$;\n" - "}\n"); + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = new com.google.protobuf.LazyStringArrayList();\n" + " $set_mutable_bit_parser$;\n" + "}\n"); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - "$name$_.add(s);\n"); + printer->Print(variables_, "$name$_.add(s);\n"); } else { - printer->Print(variables_, - "$name$_.add(bs);\n"); + printer->Print(variables_, "$name$_.add(bs);\n"); } } -void RepeatedImmutableStringFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($get_mutable_bit_parser$) {\n" - " $name$_ = $name$_.getUnmodifiableView();\n" - "}\n"); + "if ($get_mutable_bit_parser$) {\n" + " $name$_ = $name$_.getUnmodifiableView();\n" + "}\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " $writeString$(output, $number$, $name$_.getRaw(i));\n" - "}\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " $writeString$(output, $number$, $name$_.getRaw(i));\n" + "}\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); + "{\n" + " int dataSize = 0;\n"); printer->Indent(); printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += computeStringSizeNoTag($name$_.getRaw(i));\n" - "}\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " dataSize += computeStringSizeNoTag($name$_.getRaw(i));\n" + "}\n"); - printer->Print( - "size += dataSize;\n"); + printer->Print("size += dataSize;\n"); printer->Print(variables_, - "size += $tag_size$ * get$capitalized_name$List().size();\n"); + "size += $tag_size$ * get$capitalized_name$List().size();\n"); printer->Outdent(); printer->Print("}\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); +void RepeatedImmutableStringFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (!get$capitalized_name$List()\n" + " .equals(other.get$capitalized_name$List())) return false;\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutableStringFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (get$capitalized_name$Count() > 0) {\n" + " hash = (37 * hash) + $constant_name$;\n" + " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" + "}\n"); } -string RepeatedImmutableStringFieldGenerator::GetBoxedType() const { +std::string RepeatedImmutableStringFieldGenerator::GetBoxedType() const { return "String"; } diff --git a/src/google/protobuf/compiler/java/java_string_field.h b/src/google/protobuf/compiler/java/java_string_field.h index 7f9fa0ed6e..1c00ae81c2 100644 --- a/src/google/protobuf/compiler/java/java_string_field.h +++ b/src/google/protobuf/compiler/java/java_string_field.h @@ -82,14 +82,11 @@ class ImmutableStringFieldGenerator : public ImmutableFieldGenerator { void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - const int builderBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; private: @@ -141,14 +138,11 @@ class RepeatedImmutableStringFieldGenerator : public ImmutableFieldGenerator { void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - string GetBoxedType() const; + std::string GetBoxedType() const; private: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - const int builderBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutableStringFieldGenerator); diff --git a/src/google/protobuf/compiler/java/java_string_field_lite.cc b/src/google/protobuf/compiler/java/java_string_field_lite.cc index 5ce3151791..1e856f253d 100644 --- a/src/google/protobuf/compiler/java/java_string_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_string_field_lite.cc @@ -47,6 +47,7 @@ #include #include + namespace google { namespace protobuf { namespace compiler { @@ -58,11 +59,10 @@ using internal::WireFormatLite; namespace { void SetPrimitiveVariables(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, + int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* variables) { SetCommonFieldVariables(descriptor, info, variables); (*variables)["empty_list"] = @@ -73,8 +73,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, "= " + ImmutableDefaultValue(descriptor, name_resolver); (*variables)["capitalized_type"] = "java.lang.String"; (*variables)["tag"] = - SimpleItoa(static_cast(WireFormat::MakeTag(descriptor))); - (*variables)["tag_size"] = SimpleItoa( + StrCat(static_cast(WireFormat::MakeTag(descriptor))); + (*variables)["tag_size"] = StrCat( WireFormat::TagSize(descriptor->number(), GetType(descriptor))); (*variables)["null_check"] = " if (value == null) {\n" @@ -83,8 +83,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (SupportFieldPresence(descriptor->file())) { @@ -123,7 +123,6 @@ ImmutableStringFieldLiteGenerator::ImmutableStringFieldLiteGenerator( const FieldDescriptor* descriptor, int messageBitIndex, Context* context) : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - context_(context), name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), @@ -133,7 +132,7 @@ ImmutableStringFieldLiteGenerator::ImmutableStringFieldLiteGenerator( ImmutableStringFieldLiteGenerator::~ImmutableStringFieldLiteGenerator() {} int ImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const { - return 1; + return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } // A note about how strings are handled. In the SPEED and CODE_SIZE runtimes, @@ -161,237 +160,170 @@ int ImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const { // TODO(dweis): Consider dropping all of the *Bytes() methods. They really // shouldn't be necessary or used on devices. -void ImmutableStringFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableStringFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$java.lang.String get$capitalized_name$();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$java.lang.String get$capitalized_name$();\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "$deprecation$com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes();\n"); + "$deprecation$com.google.protobuf.ByteString\n" + " get$capitalized_name$Bytes();\n"); } -void ImmutableStringFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private java.lang.String $name$_;\n"); +void ImmutableStringFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private java.lang.String $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " return $name$_;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " return com.google.protobuf.ByteString.copyFromUtf8($name$_);\n" - "}\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " return com.google.protobuf.ByteString.copyFromUtf8($name$_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void set$capitalized_name$(\n" - " java.lang.String value) {\n" - "$null_check$" - " $set_has_field_bit_message$\n" - " $name$_ = value;\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $clear_has_field_bit_message$\n" - // The default value is not a simple literal so we want to avoid executing - // it multiple times. Instead, get the default out of the default instance. - " $name$_ = getDefaultInstance().get$capitalized_name$();\n" - "}\n"); - - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void set$capitalized_name$Bytes(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER); + printer->Print(variables_, + "private void set$capitalized_name$(\n" + " java.lang.String value) {\n" + "$null_check$" + " $set_has_field_bit_message$\n" + " $name$_ = value;\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); + printer->Print(variables_, + "private void clear$capitalized_name$() {\n" + " $clear_has_field_bit_message$\n" + // The default value is not a simple literal so we want to + // avoid executing it multiple times. Instead, get the default + // out of the default instance. + " $name$_ = getDefaultInstance().get$capitalized_name$();\n" + "}\n"); + + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER); + printer->Print(variables_, + "private void set$capitalized_name$Bytes(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " $set_has_field_bit_message$\n" - " $name$_ = value.toStringUtf8();\n" - "}\n"); + " $set_has_field_bit_message$\n" + " $name$_ = value.toStringUtf8();\n" + "}\n"); } -void ImmutableStringFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableStringFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " return instance.get$capitalized_name$Bytes();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " return instance.get$capitalized_name$Bytes();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Bytes(value);\n" - " return this;\n" - "}\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Bytes(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableStringFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for strings -} - - -void ImmutableStringFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_ = $default$;\n"); -} - -void ImmutableStringFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { +void ImmutableStringFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "$name$_ = visitor.visitString(\n" - " has$capitalized_name$(), $name$_,\n" - " other.has$capitalized_name$(), other.$name$_);\n"); - } else { - printer->Print(variables_, - "$name$_ = visitor.visitString(!$name$_.isEmpty(), $name$_,\n" - " !other.$name$_.isEmpty(), other.$name$_);\n"); + WriteIntToUtf16CharSequence(messageBitIndex_, output); } + printer->Print(variables_, "\"$name$_\",\n"); } -void ImmutableStringFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - // noop for scalars -} - -void ImmutableStringFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n" - "$set_has_field_bit_message$\n" - "$name$_ = s;\n"); - } else { - // Lite runtime should attempt to reduce allocations by attempting to - // construct the string directly from the input stream buffer. This avoids - // spurious intermediary ByteString allocations, cutting overall allocations - // in half. - printer->Print(variables_, - "java.lang.String s = input.readString();\n" - "$set_has_field_bit_message$\n" - "$name$_ = s;\n"); - } -} - -void ImmutableStringFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // noop for strings -} - -void ImmutableStringFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by serializing the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.writeString($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableStringFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by computing on the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeStringSize($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableStringFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); -} - -void ImmutableStringFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n"); - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); +void ImmutableStringFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { + printer->Print(variables_, "$name$_ = $default$;\n"); } -string ImmutableStringFieldLiteGenerator::GetBoxedType() const { +std::string ImmutableStringFieldLiteGenerator::GetBoxedType() const { return "java.lang.String"; } @@ -406,512 +338,393 @@ ImmutableStringOneofFieldLiteGenerator::ImmutableStringOneofFieldLiteGenerator( } ImmutableStringOneofFieldLiteGenerator:: -~ImmutableStringOneofFieldLiteGenerator() {} + ~ImmutableStringOneofFieldLiteGenerator() {} -void ImmutableStringOneofFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableStringOneofFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.String ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = (java.lang.String) $oneof_name$_;\n" - " }\n" - " return ref;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.String ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = (java.lang.String) $oneof_name$_;\n" + " }\n" + " return ref;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.String ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = (java.lang.String) $oneof_name$_;\n" - " }\n" - " return com.google.protobuf.ByteString.copyFromUtf8(ref);\n" - "}\n"); + + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.String ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = (java.lang.String) $oneof_name$_;\n" + " }\n" + " return com.google.protobuf.ByteString.copyFromUtf8(ref);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER); printer->Print(variables_, - "private void ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - "$null_check$" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - "}\n"); + "private void ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + "$null_check$" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void ${$clear$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " }\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); + printer->Print(variables_, + "private void ${$clear$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER); printer->Print(variables_, - "private void ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + "private void ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value.toStringUtf8();\n" - "}\n"); + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value.toStringUtf8();\n" + "}\n"); } +void ImmutableStringOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output); +} -void ImmutableStringOneofFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableStringOneofFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, HAZZER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, GETTER); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " return instance.get$capitalized_name$Bytes();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " return instance.get$capitalized_name$Bytes();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Bytes(value);\n" - " return this;\n" - "}\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Bytes(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableStringOneofFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$oneof_name$_ = visitor.visitOneofString(\n" - " $has_oneof_case_message$, $oneof_name$_, other.$oneof_name$_);\n"); -} - -void ImmutableStringOneofFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = s;\n"); - } else { - // Lite runtime should attempt to reduce allocations by attempting to - // construct the string directly from the input stream buffer. This avoids - // spurious intermediary ByteString allocations, cutting overall allocations - // in half. - printer->Print(variables_, - "java.lang.String s = input.readString();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = s;\n"); - } -} - -void ImmutableStringOneofFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by serializing the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.writeString($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableStringOneofFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by computing on the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeStringSize($number$, get$capitalized_name$());\n" - "}\n"); -} - // =================================================================== RepeatedImmutableStringFieldLiteGenerator:: RepeatedImmutableStringFieldLiteGenerator(const FieldDescriptor* descriptor, int messageBitIndex, Context* context) - : descriptor_(descriptor), - messageBitIndex_(messageBitIndex), - context_(context), - name_resolver_(context->GetNameResolver()) { + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); } RepeatedImmutableStringFieldLiteGenerator:: -~RepeatedImmutableStringFieldLiteGenerator() {} + ~RepeatedImmutableStringFieldLiteGenerator() {} int RepeatedImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$java.util.List\n" - " get$capitalized_name$List();\n"); - WriteFieldDocComment(printer, descriptor_); +void RepeatedImmutableStringFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$java.util.List\n" + " get$capitalized_name$List();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); printer->Print(variables_, - "$deprecation$java.lang.String get$capitalized_name$(int index);\n"); - WriteFieldDocComment(printer, descriptor_); + "$deprecation$int get$capitalized_name$Count();\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); + printer->Print( + variables_, + "$deprecation$java.lang.String get$capitalized_name$(int index);\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "$deprecation$com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes(int index);\n"); + "$deprecation$com.google.protobuf.ByteString\n" + " get$capitalized_name$Bytes(int index);\n"); } - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private com.google.protobuf.Internal.ProtobufList " - "$name$_;\n"); +void RepeatedImmutableStringFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print( + variables_, + "private com.google.protobuf.Internal.ProtobufList " + "$name$_;\n"); PrintExtraFieldInfo(variables_, printer); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List " - "${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List " + "${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String " - "${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$(int index) {\n" - " return com.google.protobuf.ByteString.copyFromUtf8(\n" - " $name$_.get(index));\n" - "}\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" + " return com.google.protobuf.ByteString.copyFromUtf8(\n" + " $name$_.get(index));\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - " }\n" - "}\n"); - - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, java.lang.String value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value);\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void add$capitalized_name$(\n" - " java.lang.String value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void addAll$capitalized_name$(\n" - " java.lang.Iterable values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.addAll(\n" - " values, $name$_);\n" - "}\n"); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $name$_ = $empty_list$;\n" - "}\n"); - - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void add$capitalized_name$Bytes(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$is_mutable$) {\n" + " $name$_ =\n" + " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" + " }\n" + "}\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER); + printer->Print(variables_, + "private void set$capitalized_name$(\n" + " int index, java.lang.String value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value);\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER); + printer->Print(variables_, + "private void add$capitalized_name$(\n" + " java.lang.String value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER); + printer->Print(variables_, + "private void addAll$capitalized_name$(\n" + " java.lang.Iterable values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.addAll(\n" + " values, $name$_);\n" + "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER); + printer->Print(variables_, + "private void clear$capitalized_name$() {\n" + " $name$_ = $empty_list$;\n" + "}\n"); + + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, LIST_ADDER); + printer->Print(variables_, + "private void add$capitalized_name$Bytes(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value.toStringUtf8());\n" - "}\n"); + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value.toStringUtf8());\n" + "}\n"); } -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_); +void RepeatedImmutableStringFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List\n" - " ${$get$capitalized_name$List$}$() {\n" - " return java.util.Collections.unmodifiableList(\n" - " instance.get$capitalized_name$List());\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List\n" + " ${$get$capitalized_name$List$}$() {\n" + " return java.util.Collections.unmodifiableList(\n" + " instance.get$capitalized_name$List());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Count();\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Count();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String " - "${$get$capitalized_name$$}$(int index) {\n" - " return instance.get$capitalized_name$(index);\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" + " return instance.get$capitalized_name$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$(int index) {\n" - " return instance.get$capitalized_name$Bytes(index);\n" - "}\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, + LIST_INDEXED_GETTER); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" + " return instance.get$capitalized_name$Bytes(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, java.lang.String value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, java.lang.String value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$(values);\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, + /* builder */ true); + printer->Print(variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$Bytes(value);\n" - " return this;\n" - "}\n"); + WriteFieldStringBytesAccessorDocComment(printer, descriptor_, LIST_ADDER, + /* builder */ true); + printer->Print( + variables_, + "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$Bytes(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for strings +void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + printer->Print(variables_, "\"$name$_\",\n"); } - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $empty_list$;\n"); } -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_= visitor.visitList($name$_, other.$name$_);\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n"); - } else { - // Lite runtime should attempt to reduce allocations by attempting to - // construct the string directly from the input stream buffer. This avoids - // spurious intermediary ByteString allocations, cutting overall allocations - // in half. - printer->Print(variables_, - "java.lang.String s = input.readString();\n"); - } - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n"); - printer->Print(variables_, - "$name$_.add(s);\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_mutable$) {\n" - " $name$_.makeImmutable();\n" - "}\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by serializing the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeString($number$, $name$_.get(i));\n" - "}\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by computing on the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); - printer->Indent(); - - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .computeStringSizeNoTag($name$_.get(i));\n" - "}\n"); - - printer->Print( - "size += dataSize;\n"); - - - printer->Print(variables_, - "size += $tag_size$ * get$capitalized_name$List().size();\n"); - - printer->Outdent(); - printer->Print("}\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); -} - -string RepeatedImmutableStringFieldLiteGenerator::GetBoxedType() const { +std::string RepeatedImmutableStringFieldLiteGenerator::GetBoxedType() const { return "java.lang.String"; } diff --git a/src/google/protobuf/compiler/java/java_string_field_lite.h b/src/google/protobuf/compiler/java/java_string_field_lite.h index 684d3b005b..b67135cd81 100644 --- a/src/google/protobuf/compiler/java/java_string_field_lite.h +++ b/src/google/protobuf/compiler/java/java_string_field_lite.h @@ -70,24 +70,15 @@ class ImmutableStringFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; - - string GetBoxedType() const; + std::string GetBoxedType() const; protected: const FieldDescriptor* descriptor_; - std::map variables_; + std::map variables_; const int messageBitIndex_; - Context* context_; ClassNameResolver* name_resolver_; private: @@ -104,11 +95,8 @@ class ImmutableStringOneofFieldLiteGenerator private: void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableStringOneofFieldLiteGenerator); }; @@ -126,24 +114,14 @@ class RepeatedImmutableStringFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const; - - string GetBoxedType() const; + std::string GetBoxedType() const; private: const FieldDescriptor* descriptor_; - std::map variables_; - const int messageBitIndex_; - Context* context_; + std::map variables_; ClassNameResolver* name_resolver_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutableStringFieldLiteGenerator); diff --git a/src/google/protobuf/compiler/js/js_generator.cc b/src/google/protobuf/compiler/js/js_generator.cc index 5779d5e2af..fa7e98ef5c 100644 --- a/src/google/protobuf/compiler/js/js_generator.cc +++ b/src/google/protobuf/compiler/js/js_generator.cc @@ -43,6 +43,8 @@ #include #include #include + +#include #include #include #include @@ -58,64 +60,18 @@ namespace js { // Sorted list of JavaScript keywords. These cannot be used as names. If they // appear, we prefix them with "pb_". const char* kKeyword[] = { - "abstract", - "boolean", - "break", - "byte", - "case", - "catch", - "char", - "class", - "const", - "continue", - "debugger", - "default", - "delete", - "do", - "double", - "else", - "enum", - "export", - "extends", - "false", - "final", - "finally", - "float", - "for", - "function", - "goto", - "if", - "implements", - "import", - "in", - "instanceof", - "int", - "interface", - "long", - "native", - "new", - "null", - "package", - "private", - "protected", - "public", - "return", - "short", - "static", - "super", - "switch", - "synchronized", - "this", - "throw", - "throws", - "transient", - "try", - "typeof", - "var", - "void", - "volatile", - "while", - "with", + "abstract", "boolean", "break", "byte", "case", + "catch", "char", "class", "const", "continue", + "debugger", "default", "delete", "do", "double", + "else", "enum", "export", "extends", "false", + "final", "finally", "float", "for", "function", + "goto", "if", "implements", "import", "in", + "instanceof", "int", "interface", "long", "native", + "new", "null", "package", "private", "protected", + "public", "return", "short", "static", "super", + "switch", "synchronized", "this", "throw", "throws", + "transient", "try", "typeof", "var", "void", + "volatile", "while", "with", }; static const int kNumKeyword = sizeof(kKeyword) / sizeof(char*); @@ -132,7 +88,7 @@ enum BytesMode { BYTES_U8, // Explicitly coerce to Uint8Array where needed. }; -bool IsReserved(const string& ident) { +bool IsReserved(const std::string& ident) { for (int i = 0; i < kNumKeyword; i++) { if (ident == kKeyword[i]) { return true; @@ -148,21 +104,29 @@ bool StrEndsWith(StringPiece sp, StringPiece x) { // Returns a copy of |filename| with any trailing ".protodevel" or ".proto // suffix stripped. // TODO(haberman): Unify with copy in compiler/cpp/internal/helpers.cc. -string StripProto(const string& filename) { +std::string StripProto(const std::string& filename) { const char* suffix = StrEndsWith(filename, ".protodevel") ? ".protodevel" : ".proto"; return StripSuffixString(filename, suffix); } +std::string GetSnakeFilename(const std::string& filename) { + std::string snake_name = filename; + ReplaceCharacters(&snake_name, "/", '_'); + return snake_name; +} + // Given a filename like foo/bar/baz.proto, returns the corresponding JavaScript // file foo/bar/baz.js. -string GetJSFilename(const GeneratorOptions& options, const string& filename) { +std::string GetJSFilename(const GeneratorOptions& options, + const std::string& filename) { return StripProto(filename) + options.GetFileNameExtension(); } // Given a filename like foo/bar/baz.proto, returns the root directory // path ../../ -string GetRootPath(const string& from_filename, const string& to_filename) { +string GetRootPath(const std::string& from_filename, + const std::string& to_filename) { if (to_filename.find("google/protobuf") == 0) { // Well-known types (.proto files in the google/protobuf directory) are // assumed to come from the 'google-protobuf' npm package. We may want to @@ -175,7 +139,7 @@ string GetRootPath(const string& from_filename, const string& to_filename) { if (slashes == 0) { return "./"; } - string result = ""; + std::string result = ""; for (size_t i = 0; i < slashes; i++) { result += "../"; } @@ -184,7 +148,7 @@ string GetRootPath(const string& from_filename, const string& to_filename) { // Returns the alias we assign to the module of the given .proto filename // when importing. -string ModuleAlias(const string& filename) { +std::string ModuleAlias(const std::string& filename) { // This scheme could technically cause problems if a file includes any 2 of: // foo/bar_baz.proto // foo_bar_baz.proto @@ -192,17 +156,17 @@ string ModuleAlias(const string& filename) { // // We'll worry about this problem if/when we actually see it. This name isn't // exposed to users so we can change it later if we need to. - string basename = StripProto(filename); + std::string basename = StripProto(filename); ReplaceCharacters(&basename, "-", '$'); ReplaceCharacters(&basename, "/", '_'); ReplaceCharacters(&basename, ".", '_'); return basename + "_pb"; } -// Returns the fully normalized JavaScript path for the given +// Returns the fully normalized JavaScript namespace for the given // file descriptor's package. -string GetFilePath(const GeneratorOptions& options, - const FileDescriptor* file) { +std::string GetNamespace(const GeneratorOptions& options, + const FileDescriptor* file) { if (!options.namespace_prefix.empty()) { return options.namespace_prefix; } else if (!file->package().empty()) { @@ -216,12 +180,12 @@ string GetFilePath(const GeneratorOptions& options, // nesting, for example ".OuterMessage.InnerMessage", or returns empty if // descriptor is null. This function does not handle namespacing, only message // nesting. -string GetNestedMessageName(const Descriptor* descriptor) { +std::string GetNestedMessageName(const Descriptor* descriptor) { if (descriptor == NULL) { return ""; } - string result = StripPrefixString(descriptor->full_name(), - descriptor->file()->package()); + std::string result = StripPrefixString( + descriptor->full_name(), descriptor->file()->package()); // Add a leading dot if one is not already present. if (!result.empty() && result[0] != '.') { result = "." + result; @@ -231,11 +195,11 @@ string GetNestedMessageName(const Descriptor* descriptor) { // Returns the path prefix for a message or enumeration that // lives under the given file and containing type. -string GetPrefix(const GeneratorOptions& options, - const FileDescriptor* file_descriptor, - const Descriptor* containing_type) { - string prefix = GetFilePath(options, file_descriptor) + - GetNestedMessageName(containing_type); +std::string GetPrefix(const GeneratorOptions& options, + const FileDescriptor* file_descriptor, + const Descriptor* containing_type) { + std::string prefix = GetNamespace(options, file_descriptor) + + GetNestedMessageName(containing_type); if (!prefix.empty()) { prefix += "."; } @@ -244,38 +208,36 @@ string GetPrefix(const GeneratorOptions& options, // Returns the fully normalized JavaScript path prefix for the given // message descriptor. -string GetMessagePathPrefix(const GeneratorOptions& options, - const Descriptor* descriptor) { - return GetPrefix( - options, descriptor->file(), - descriptor->containing_type()); +std::string GetMessagePathPrefix(const GeneratorOptions& options, + const Descriptor* descriptor) { + return GetPrefix(options, descriptor->file(), descriptor->containing_type()); } // Returns the fully normalized JavaScript path for the given // message descriptor. -string GetMessagePath(const GeneratorOptions& options, - const Descriptor* descriptor) { +std::string GetMessagePath(const GeneratorOptions& options, + const Descriptor* descriptor) { return GetMessagePathPrefix(options, descriptor) + descriptor->name(); } // Returns the fully normalized JavaScript path prefix for the given // enumeration descriptor. -string GetEnumPathPrefix(const GeneratorOptions& options, - const EnumDescriptor* enum_descriptor) { +std::string GetEnumPathPrefix(const GeneratorOptions& options, + const EnumDescriptor* enum_descriptor) { return GetPrefix(options, enum_descriptor->file(), - enum_descriptor->containing_type()); + enum_descriptor->containing_type()); } // Returns the fully normalized JavaScript path for the given // enumeration descriptor. -string GetEnumPath(const GeneratorOptions& options, - const EnumDescriptor* enum_descriptor) { +std::string GetEnumPath(const GeneratorOptions& options, + const EnumDescriptor* enum_descriptor) { return GetEnumPathPrefix(options, enum_descriptor) + enum_descriptor->name(); } -string MaybeCrossFileRef(const GeneratorOptions& options, - const FileDescriptor* from_file, - const Descriptor* to_message) { +std::string MaybeCrossFileRef(const GeneratorOptions& options, + const FileDescriptor* from_file, + const Descriptor* to_message) { if ((options.import_style == GeneratorOptions::kImportCommonJs || options.import_style == GeneratorOptions::kImportCommonJsStrict) && from_file != to_message->file()) { @@ -290,8 +252,8 @@ string MaybeCrossFileRef(const GeneratorOptions& options, } } -string SubmessageTypeRef(const GeneratorOptions& options, - const FieldDescriptor* field) { +std::string SubmessageTypeRef(const GeneratorOptions& options, + const FieldDescriptor* field) { GOOGLE_CHECK(field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE); return MaybeCrossFileRef(options, field->file(), field->message_type()); } @@ -313,9 +275,9 @@ char ToLowerASCII(char c) { } } -std::vector ParseLowerUnderscore(const string& input) { - std::vector words; - string running = ""; +std::vector ParseLowerUnderscore(const std::string& input) { + std::vector words; + std::string running = ""; for (int i = 0; i < input.size(); i++) { if (input[i] == '_') { if (!running.empty()) { @@ -332,9 +294,9 @@ std::vector ParseLowerUnderscore(const string& input) { return words; } -std::vector ParseUpperCamel(const string& input) { - std::vector words; - string running = ""; +std::vector ParseUpperCamel(const std::string& input) { + std::vector words; + std::string running = ""; for (int i = 0; i < input.size(); i++) { if (input[i] >= 'A' && input[i] <= 'Z' && !running.empty()) { words.push_back(running); @@ -348,10 +310,10 @@ std::vector ParseUpperCamel(const string& input) { return words; } -string ToLowerCamel(const std::vector& words) { - string result; +std::string ToLowerCamel(const std::vector& words) { + std::string result; for (int i = 0; i < words.size(); i++) { - string word = words[i]; + std::string word = words[i]; if (i == 0 && (word[0] >= 'A' && word[0] <= 'Z')) { word[0] = (word[0] - 'A') + 'a'; } else if (i != 0 && (word[0] >= 'a' && word[0] <= 'z')) { @@ -362,10 +324,10 @@ string ToLowerCamel(const std::vector& words) { return result; } -string ToUpperCamel(const std::vector& words) { - string result; +std::string ToUpperCamel(const std::vector& words) { + std::string result; for (int i = 0; i < words.size(); i++) { - string word = words[i]; + std::string word = words[i]; if (word[0] >= 'a' && word[0] <= 'z') { word[0] = (word[0] - 'a') + 'A'; } @@ -377,8 +339,8 @@ string ToUpperCamel(const std::vector& words) { // Based on code from descriptor.cc (Thanks Kenton!) // Uppercases the entire string, turning ValueName into // VALUENAME. -string ToEnumCase(const string& input) { - string result; +std::string ToEnumCase(const std::string& input) { + std::string result; result.reserve(input.size()); for (int i = 0; i < input.size(); i++) { @@ -392,8 +354,8 @@ string ToEnumCase(const string& input) { return result; } -string ToFileName(const string& input) { - string result; +std::string ToLower(const std::string& input) { + std::string result; result.reserve(input.size()); for (int i = 0; i < input.size(); i++) { @@ -407,33 +369,81 @@ string ToFileName(const string& input) { return result; } -// When we're generating one output file per type name, this is the filename +// When we're generating one output file per SCC, this is the filename // that top-level extensions should go in. -string GetExtensionFileName(const GeneratorOptions& options, - const FileDescriptor* file) { - return options.output_dir + "/" + ToFileName(GetFilePath(options, file)) + +// e.g. one proto file (test.proto): +// package a; +// extends Foo { +// ... +// } +// If "with_filename" equals true, the extension filename will be +// "proto.a_test_extensions.js", otherwise will be "proto.a.js" +std::string GetExtensionFileName(const GeneratorOptions& options, + const FileDescriptor* file, + bool with_filename) { + std::string snake_name = StripProto(GetSnakeFilename(file->name())); + return options.output_dir + "/" + ToLower(GetNamespace(options, file)) + + (with_filename ? ("_" + snake_name + "_extensions") : "") + options.GetFileNameExtension(); } - -// When we're generating one output file per type name, this is the filename -// that a top-level message should go in. -string GetMessageFileName(const GeneratorOptions& options, - const Descriptor* desc) { - return options.output_dir + "/" + ToFileName(desc->name()) + +// When we're generating one output file per SCC, this is the filename +// that all messages in the SCC should go in. +// If with_package equals true, filename will have package prefix, +// If the filename length is longer than 200, the filename will be the +// SCC's proto filename with suffix "_long_sccs_(index)" (if with_package equals +// true it still has package prefix) +std::string GetMessagesFileName(const GeneratorOptions& options, const SCC* scc, + bool with_package) { + static std::map* long_name_dict = + new std::map(); + std::string package_base = + with_package + ? ToLower(GetNamespace(options, scc->GetRepresentative()->file()) + + "_") + : ""; + std::string filename_base = ""; + std::vector all_message_names; + for (auto one_desc : scc->descriptors) { + if (one_desc->containing_type() == nullptr) { + all_message_names.push_back(ToLower(one_desc->name())); + } + } + sort(all_message_names.begin(), all_message_names.end()); + for (auto one_message : all_message_names) { + if (!filename_base.empty()) { + filename_base += "_"; + } + filename_base += one_message; + } + if (filename_base.size() + package_base.size() > 200) { + if ((*long_name_dict).find(scc->GetRepresentative()) == + (*long_name_dict).end()) { + std::string snake_name = StripProto( + GetSnakeFilename(scc->GetRepresentative()->file()->name())); + (*long_name_dict)[scc->GetRepresentative()] = + StrCat(snake_name, "_long_sccs_", + static_cast((*long_name_dict).size())); + } + filename_base = (*long_name_dict)[scc->GetRepresentative()]; + } + return options.output_dir + "/" + package_base + filename_base + options.GetFileNameExtension(); } // When we're generating one output file per type name, this is the filename -// that a top-level message should go in. -string GetEnumFileName(const GeneratorOptions& options, - const EnumDescriptor* desc) { - return options.output_dir + "/" + ToFileName(desc->name()) + - options.GetFileNameExtension(); +// that a top-level enum should go in. +// If with_package equals true, filename will have package prefix. +std::string GetEnumFileName(const GeneratorOptions& options, + const EnumDescriptor* desc, bool with_package) { + return options.output_dir + "/" + + (with_package ? ToLower(GetNamespace(options, desc->file()) + "_") + : "") + + ToLower(desc->name()) + options.GetFileNameExtension(); } // Returns the message/response ID, if set. -string GetMessageId(const Descriptor* desc) { - return string(); +std::string GetMessageId(const Descriptor* desc) { + return std::string(); } bool IgnoreExtensionField(const FieldDescriptor* field) { @@ -455,9 +465,7 @@ bool IgnoreField(const FieldDescriptor* field) { // Do we ignore this message type? -bool IgnoreMessage(const Descriptor* d) { - return d->options().map_entry(); -} +bool IgnoreMessage(const Descriptor* d) { return d->options().map_entry(); } // Does JSPB ignore this entire oneof? True only if all fields are ignored. bool IgnoreOneof(const OneofDescriptor* oneof) { @@ -469,17 +477,17 @@ bool IgnoreOneof(const OneofDescriptor* oneof) { return true; } -string JSIdent(const GeneratorOptions& options, const FieldDescriptor* field, - bool is_upper_camel, bool is_map, bool drop_list) { - string result; +std::string JSIdent(const GeneratorOptions& options, + const FieldDescriptor* field, bool is_upper_camel, + bool is_map, bool drop_list) { + std::string result; if (field->type() == FieldDescriptor::TYPE_GROUP) { - result = is_upper_camel ? - ToUpperCamel(ParseUpperCamel(field->message_type()->name())) : - ToLowerCamel(ParseUpperCamel(field->message_type()->name())); + result = is_upper_camel + ? ToUpperCamel(ParseUpperCamel(field->message_type()->name())) + : ToLowerCamel(ParseUpperCamel(field->message_type()->name())); } else { - result = is_upper_camel ? - ToUpperCamel(ParseLowerUnderscore(field->name())) : - ToLowerCamel(ParseLowerUnderscore(field->name())); + result = is_upper_camel ? ToUpperCamel(ParseLowerUnderscore(field->name())) + : ToLowerCamel(ParseLowerUnderscore(field->name())); } if (is_map || field->is_map()) { // JSPB-style or proto3-style map. @@ -491,19 +499,19 @@ string JSIdent(const GeneratorOptions& options, const FieldDescriptor* field, return result; } -string JSObjectFieldName(const GeneratorOptions& options, - const FieldDescriptor* field) { - string name = JSIdent(options, field, - /* is_upper_camel = */ false, - /* is_map = */ false, - /* drop_list = */ false); +std::string JSObjectFieldName(const GeneratorOptions& options, + const FieldDescriptor* field) { + std::string name = JSIdent(options, field, + /* is_upper_camel = */ false, + /* is_map = */ false, + /* drop_list = */ false); if (IsReserved(name)) { name = "pb_" + name; } return name; } -string JSByteGetterSuffix(BytesMode bytes_mode) { +std::string JSByteGetterSuffix(BytesMode bytes_mode) { switch (bytes_mode) { case BYTES_DEFAULT: return ""; @@ -519,15 +527,15 @@ string JSByteGetterSuffix(BytesMode bytes_mode) { // Returns the field name as a capitalized portion of a getter/setter method // name, e.g. MyField for .getMyField(). -string JSGetterName(const GeneratorOptions& options, - const FieldDescriptor* field, - BytesMode bytes_mode = BYTES_DEFAULT, - bool drop_list = false) { - string name = JSIdent(options, field, - /* is_upper_camel = */ true, - /* is_map = */ false, drop_list); +std::string JSGetterName(const GeneratorOptions& options, + const FieldDescriptor* field, + BytesMode bytes_mode = BYTES_DEFAULT, + bool drop_list = false) { + std::string name = JSIdent(options, field, + /* is_upper_camel = */ true, + /* is_map = */ false, drop_list); if (field->type() == FieldDescriptor::TYPE_BYTES) { - string suffix = JSByteGetterSuffix(bytes_mode); + std::string suffix = JSByteGetterSuffix(bytes_mode); if (!suffix.empty()) { name += "_as" + suffix; } @@ -540,14 +548,13 @@ string JSGetterName(const GeneratorOptions& options, } - -string JSOneofName(const OneofDescriptor* oneof) { +std::string JSOneofName(const OneofDescriptor* oneof) { return ToUpperCamel(ParseLowerUnderscore(oneof->name())); } // Returns the index corresponding to this field in the JSPB array (underlying // data storage array). -string JSFieldIndex(const FieldDescriptor* field) { +std::string JSFieldIndex(const FieldDescriptor* field) { // Determine whether this field is a member of a group. Group fields are a bit // wonky: their "containing type" is a message type created just for the // group, and that type's parent type has a field with the group-message type @@ -560,15 +567,14 @@ string JSFieldIndex(const FieldDescriptor* field) { for (int i = 0; i < parent_type->field_count(); i++) { if (parent_type->field(i)->type() == FieldDescriptor::TYPE_GROUP && parent_type->field(i)->message_type() == containing_type) { - return SimpleItoa(field->number() - - parent_type->field(i)->number()); + return StrCat(field->number() - parent_type->field(i)->number()); } } } - return SimpleItoa(field->number()); + return StrCat(field->number()); } -string JSOneofIndex(const OneofDescriptor* oneof) { +std::string JSOneofIndex(const OneofDescriptor* oneof) { int index = -1; for (int i = 0; i < oneof->containing_type()->oneof_decl_count(); i++) { const OneofDescriptor* o = oneof->containing_type()->oneof_decl(i); @@ -584,7 +590,7 @@ string JSOneofIndex(const OneofDescriptor* oneof) { break; } } - return SimpleItoa(index); + return StrCat(index); } // Decodes a codepoint in \x0000 -- \xFFFF. @@ -613,13 +619,15 @@ uint16 DecodeUTF8Codepoint(uint8* bytes, size_t* length) { *length = expected; switch (expected) { - case 1: return bytes[0]; - case 2: return ((bytes[0] & 0x1F) << 6) | - ((bytes[1] & 0x3F) << 0); - case 3: return ((bytes[0] & 0x0F) << 12) | - ((bytes[1] & 0x3F) << 6) | - ((bytes[2] & 0x3F) << 0); - default: return 0; + case 1: + return bytes[0]; + case 2: + return ((bytes[0] & 0x1F) << 6) | ((bytes[1] & 0x3F) << 0); + case 3: + return ((bytes[0] & 0x0F) << 12) | ((bytes[1] & 0x3F) << 6) | + ((bytes[2] & 0x3F) << 0); + default: + return 0; } } @@ -627,8 +635,8 @@ uint16 DecodeUTF8Codepoint(uint8* bytes, size_t* length) { // JavaScript. The input data should be a UTF-8 encoded C++ string of chars. // Returns false if |out| was truncated because |in| contained invalid UTF-8 or // codepoints outside the BMP. -// TODO(lukestebbing): Support codepoints outside the BMP. -bool EscapeJSString(const string& in, string* out) { +// TODO(b/115551870): Support codepoints outside the BMP. +bool EscapeJSString(const std::string& in, std::string* out) { size_t decoded = 0; for (size_t i = 0; i < in.size(); i += decoded) { uint16 codepoint = 0; @@ -646,20 +654,44 @@ bool EscapeJSString(const string& in, string* out) { decoded = have_bytes; switch (codepoint) { - case '\'': *out += "\\x27"; break; - case '"': *out += "\\x22"; break; - case '<': *out += "\\x3c"; break; - case '=': *out += "\\x3d"; break; - case '>': *out += "\\x3e"; break; - case '&': *out += "\\x26"; break; - case '\b': *out += "\\b"; break; - case '\t': *out += "\\t"; break; - case '\n': *out += "\\n"; break; - case '\f': *out += "\\f"; break; - case '\r': *out += "\\r"; break; - case '\\': *out += "\\\\"; break; + case '\'': + *out += "\\x27"; + break; + case '"': + *out += "\\x22"; + break; + case '<': + *out += "\\x3c"; + break; + case '=': + *out += "\\x3d"; + break; + case '>': + *out += "\\x3e"; + break; + case '&': + *out += "\\x26"; + break; + case '\b': + *out += "\\b"; + break; + case '\t': + *out += "\\t"; + break; + case '\n': + *out += "\\n"; + break; + case '\f': + *out += "\\f"; + break; + case '\r': + *out += "\\r"; + break; + case '\\': + *out += "\\\\"; + break; default: - // TODO(lukestebbing): Once we're supporting codepoints outside the BMP, + // TODO(b/115551870): Once we're supporting codepoints outside the BMP, // use a single Unicode codepoint escape if the output language is // ECMAScript 2015 or above. Otherwise, use a surrogate pair. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#String_literals @@ -676,24 +708,23 @@ bool EscapeJSString(const string& in, string* out) { return true; } -string EscapeBase64(const string& in) { +std::string EscapeBase64(const std::string& in) { static const char* kAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - string result; + std::string result; for (size_t i = 0; i < in.size(); i += 3) { - int value = (in[i] << 16) | - (((i + 1) < in.size()) ? (in[i + 1] << 8) : 0) | - (((i + 2) < in.size()) ? (in[i + 2] << 0) : 0); + int value = (in[i] << 16) | (((i + 1) < in.size()) ? (in[i + 1] << 8) : 0) | + (((i + 2) < in.size()) ? (in[i + 2] << 0) : 0); result += kAlphabet[(value >> 18) & 0x3f]; result += kAlphabet[(value >> 12) & 0x3f]; if ((i + 1) < in.size()) { - result += kAlphabet[(value >> 6) & 0x3f]; + result += kAlphabet[(value >> 6) & 0x3f]; } else { result += '='; } if ((i + 2) < in.size()) { - result += kAlphabet[(value >> 0) & 0x3f]; + result += kAlphabet[(value >> 0) & 0x3f]; } else { result += '='; } @@ -705,7 +736,7 @@ string EscapeBase64(const string& in) { // Post-process the result of SimpleFtoa/SimpleDtoa to *exactly* match the // original codegen's formatting (which is just .toString() on java.lang.Double // or java.lang.Float). -string PostProcessFloat(string result) { +std::string PostProcessFloat(std::string result) { // If inf, -inf or nan, replace with +Infinity, -Infinity or NaN. if (result == "inf") { return "Infinity"; @@ -719,13 +750,13 @@ string PostProcessFloat(string result) { // ensure that the mantissa (portion prior to the "e") has at least one // fractional digit (after the decimal point), and (iii) strip any unnecessary // leading zeroes and/or '+' signs from the exponent. - string::size_type exp_pos = result.find('e'); - if (exp_pos != string::npos) { - string mantissa = result.substr(0, exp_pos); - string exponent = result.substr(exp_pos + 1); + std::string::size_type exp_pos = result.find('e'); + if (exp_pos != std::string::npos) { + std::string mantissa = result.substr(0, exp_pos); + std::string exponent = result.substr(exp_pos + 1); // Add ".0" to mantissa if no fractional part exists. - if (mantissa.find('.') == string::npos) { + if (mantissa.find('.') == std::string::npos) { mantissa += ".0"; } @@ -748,20 +779,20 @@ string PostProcessFloat(string result) { // Otherwise, this is an ordinary decimal number. Append ".0" if result has no // decimal/fractional part in order to match output of original codegen. - if (result.find('.') == string::npos) { + if (result.find('.') == std::string::npos) { result += ".0"; } return result; } -string FloatToString(float value) { - string result = SimpleFtoa(value); +std::string FloatToString(float value) { + std::string result = SimpleFtoa(value); return PostProcessFloat(result); } -string DoubleToString(double value) { - string result = SimpleDtoa(value); +std::string DoubleToString(double value) { + std::string result = SimpleDtoa(value); return PostProcessFloat(result); } @@ -779,34 +810,37 @@ bool IsIntegralFieldWithStringJSType(const FieldDescriptor* field) { } } -string MaybeNumberString(const FieldDescriptor* field, const string& orig) { +std::string MaybeNumberString(const FieldDescriptor* field, + const std::string& orig) { return IsIntegralFieldWithStringJSType(field) ? ("\"" + orig + "\"") : orig; } -string JSFieldDefault(const FieldDescriptor* field) { +std::string JSFieldDefault(const FieldDescriptor* field) { if (field->is_repeated()) { return "[]"; } switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: - return MaybeNumberString( - field, SimpleItoa(field->default_value_int32())); + return MaybeNumberString(field, + StrCat(field->default_value_int32())); case FieldDescriptor::CPPTYPE_UINT32: // The original codegen is in Java, and Java protobufs store unsigned // integer values as signed integer values. In order to exactly match the // output, we need to reinterpret as base-2 signed. Ugh. - return MaybeNumberString(field, SimpleItoa(static_cast( - field->default_value_uint32()))); - case FieldDescriptor::CPPTYPE_INT64: return MaybeNumberString( - field, SimpleItoa(field->default_value_int64())); + field, + StrCat(static_cast(field->default_value_uint32()))); + case FieldDescriptor::CPPTYPE_INT64: + return MaybeNumberString(field, + StrCat(field->default_value_int64())); case FieldDescriptor::CPPTYPE_UINT64: // See above note for uint32 -- reinterpreting as signed. - return MaybeNumberString(field, SimpleItoa(static_cast( - field->default_value_uint64()))); + return MaybeNumberString( + field, + StrCat(static_cast(field->default_value_uint64()))); case FieldDescriptor::CPPTYPE_ENUM: - return SimpleItoa(field->default_value_enum()->number()); + return StrCat(field->default_value_enum()->number()); case FieldDescriptor::CPPTYPE_BOOL: return field->default_value_bool() ? "true" : "false"; case FieldDescriptor::CPPTYPE_FLOAT: @@ -815,10 +849,10 @@ string JSFieldDefault(const FieldDescriptor* field) { return DoubleToString(field->default_value_double()); case FieldDescriptor::CPPTYPE_STRING: if (field->type() == FieldDescriptor::TYPE_STRING) { - string out; + std::string out; bool is_valid = EscapeJSString(field->default_value_string(), &out); if (!is_valid) { - // TODO(lukestebbing): Decide whether this should be a hard error. + // TODO(b/115551870): Decide whether this should be a hard error. GOOGLE_LOG(WARNING) << "The default value for field " << field->full_name() << " was truncated since it contained invalid UTF-8 or" " codepoints outside the basic multilingual plane."; @@ -834,8 +868,8 @@ string JSFieldDefault(const FieldDescriptor* field) { return ""; } -string ProtoTypeName(const GeneratorOptions& options, - const FieldDescriptor* field) { +std::string ProtoTypeName(const GeneratorOptions& options, + const FieldDescriptor* field) { switch (field->type()) { case FieldDescriptor::TYPE_BOOL: return "bool"; @@ -878,13 +912,13 @@ string ProtoTypeName(const GeneratorOptions& options, } } -string JSIntegerTypeName(const FieldDescriptor* field) { +std::string JSIntegerTypeName(const FieldDescriptor* field) { return IsIntegralFieldWithStringJSType(field) ? "string" : "number"; } -string JSStringTypeName(const GeneratorOptions& options, - const FieldDescriptor* field, - BytesMode bytes_mode) { +std::string JSStringTypeName(const GeneratorOptions& options, + const FieldDescriptor* field, + BytesMode bytes_mode) { if (field->type() == FieldDescriptor::TYPE_BYTES) { switch (bytes_mode) { case BYTES_DEFAULT: @@ -900,9 +934,8 @@ string JSStringTypeName(const GeneratorOptions& options, return "string"; } -string JSTypeName(const GeneratorOptions& options, - const FieldDescriptor* field, - BytesMode bytes_mode) { +std::string JSTypeName(const GeneratorOptions& options, + const FieldDescriptor* field, BytesMode bytes_mode) { switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_BOOL: return "boolean"; @@ -936,8 +969,8 @@ bool UseBrokenPresenceSemantics(const GeneratorOptions& options, } // Returns true for fields that return "null" from accessors when they are -// unset. This should normally only be true for non-repeated submessages, but -// we have legacy users who relied on old behavior where accessors behaved this +// unset. This should normally only be true for non-repeated submessages, but we +// have legacy users who relied on old behavior where accessors behaved this // way. bool ReturnsNullWhenUnset(const GeneratorOptions& options, const FieldDescriptor* field) { @@ -996,19 +1029,18 @@ bool SetterAcceptsNull(const GeneratorOptions& options, // Returns types which are known to by non-nullable by default. // The style guide requires that we omit "!" in this case. -bool IsPrimitive(const string& type) { +bool IsPrimitive(const std::string& type) { return type == "undefined" || type == "string" || type == "number" || type == "boolean"; } -string JSFieldTypeAnnotation(const GeneratorOptions& options, - const FieldDescriptor* field, - bool is_setter_argument, - bool force_present, - bool singular_if_not_packed, - BytesMode bytes_mode = BYTES_DEFAULT, - bool force_singular = false) { - string jstype = JSTypeName(options, field, bytes_mode); +std::string JSFieldTypeAnnotation(const GeneratorOptions& options, + const FieldDescriptor* field, + bool is_setter_argument, bool force_present, + bool singular_if_not_packed, + BytesMode bytes_mode = BYTES_DEFAULT, + bool force_singular = false) { + std::string jstype = JSTypeName(options, field, bytes_mode); if (!force_singular && field->is_repeated() && (field->is_packed() || !singular_if_not_packed)) { @@ -1051,17 +1083,17 @@ string JSFieldTypeAnnotation(const GeneratorOptions& options, return jstype; } -string JSBinaryReaderMethodType(const FieldDescriptor* field) { - string name = field->type_name(); +std::string JSBinaryReaderMethodType(const FieldDescriptor* field) { + std::string name = field->type_name(); if (name[0] >= 'a' && name[0] <= 'z') { name[0] = (name[0] - 'a') + 'A'; } return IsIntegralFieldWithStringJSType(field) ? (name + "String") : name; } -string JSBinaryReadWriteMethodName(const FieldDescriptor* field, - bool is_writer) { - string name = JSBinaryReaderMethodType(field); +std::string JSBinaryReadWriteMethodName(const FieldDescriptor* field, + bool is_writer) { + std::string name = JSBinaryReaderMethodType(field); if (field->is_packed()) { name = "Packed" + name; } else if (is_writer && field->is_repeated()) { @@ -1070,23 +1102,27 @@ string JSBinaryReadWriteMethodName(const FieldDescriptor* field, return name; } -string JSBinaryReaderMethodName(const GeneratorOptions& options, - const FieldDescriptor* field) { +std::string JSBinaryReaderMethodName(const GeneratorOptions& options, + const FieldDescriptor* field) { return "jspb.BinaryReader.prototype.read" + JSBinaryReadWriteMethodName(field, /* is_writer = */ false); } -string JSBinaryWriterMethodName(const GeneratorOptions& options, - const FieldDescriptor* field) { +std::string JSBinaryWriterMethodName(const GeneratorOptions& options, + const FieldDescriptor* field) { + if (field->containing_type() && + field->containing_type()->options().message_set_wire_format()) { + return "jspb.BinaryWriter.prototype.writeMessageSet"; + } return "jspb.BinaryWriter.prototype.write" + JSBinaryReadWriteMethodName(field, /* is_writer = */ true); } -string JSReturnClause(const FieldDescriptor* desc) { +std::string JSReturnClause(const FieldDescriptor* desc) { return ""; } -string JSTypeTag(const FieldDescriptor* desc) { +std::string JSTypeTag(const FieldDescriptor* desc) { switch (desc->type()) { case FieldDescriptor::TYPE_DOUBLE: case FieldDescriptor::TYPE_FLOAT: @@ -1120,8 +1156,8 @@ string JSTypeTag(const FieldDescriptor* desc) { return ""; } -string JSReturnDoc(const GeneratorOptions& options, - const FieldDescriptor* desc) { +std::string JSReturnDoc(const GeneratorOptions& options, + const FieldDescriptor* desc) { return ""; } @@ -1137,8 +1173,8 @@ bool HasRepeatedFields(const GeneratorOptions& options, static const char* kRepeatedFieldArrayName = ".repeatedFields_"; -string RepeatedFieldsArrayName(const GeneratorOptions& options, - const Descriptor* desc) { +std::string RepeatedFieldsArrayName(const GeneratorOptions& options, + const Descriptor* desc) { return HasRepeatedFields(options, desc) ? (GetMessagePath(options, desc) + kRepeatedFieldArrayName) : "null"; @@ -1155,16 +1191,16 @@ bool HasOneofFields(const Descriptor* desc) { static const char* kOneofGroupArrayName = ".oneofGroups_"; -string OneofFieldsArrayName(const GeneratorOptions& options, - const Descriptor* desc) { +std::string OneofFieldsArrayName(const GeneratorOptions& options, + const Descriptor* desc) { return HasOneofFields(desc) ? (GetMessagePath(options, desc) + kOneofGroupArrayName) : "null"; } -string RepeatedFieldNumberList(const GeneratorOptions& options, - const Descriptor* desc) { - std::vector numbers; +std::string RepeatedFieldNumberList(const GeneratorOptions& options, + const Descriptor* desc) { + std::vector numbers; for (int i = 0; i < desc->field_count(); i++) { if (desc->field(i)->is_repeated() && !desc->field(i)->is_map()) { numbers.push_back(JSFieldIndex(desc->field(i))); @@ -1173,16 +1209,16 @@ string RepeatedFieldNumberList(const GeneratorOptions& options, return "[" + Join(numbers, ",") + "]"; } -string OneofGroupList(const Descriptor* desc) { +std::string OneofGroupList(const Descriptor* desc) { // List of arrays (one per oneof), each of which is a list of field indices - std::vector oneof_entries; + std::vector oneof_entries; for (int i = 0; i < desc->oneof_decl_count(); i++) { const OneofDescriptor* oneof = desc->oneof_decl(i); if (IgnoreOneof(oneof)) { continue; } - std::vector oneof_fields; + std::vector oneof_fields; for (int j = 0; j < oneof->field_count(); j++) { if (IgnoreField(oneof->field(j))) { continue; @@ -1194,21 +1230,22 @@ string OneofGroupList(const Descriptor* desc) { return "[" + Join(oneof_entries, ",") + "]"; } -string JSOneofArray(const GeneratorOptions& options, - const FieldDescriptor* field) { +std::string JSOneofArray(const GeneratorOptions& options, + const FieldDescriptor* field) { return OneofFieldsArrayName(options, field->containing_type()) + "[" + - JSOneofIndex(field->containing_oneof()) + "]"; + JSOneofIndex(field->containing_oneof()) + "]"; } -string RelativeTypeName(const FieldDescriptor* field) { +std::string RelativeTypeName(const FieldDescriptor* field) { assert(field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM || field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE); // For a field with an enum or message type, compute a name relative to the // path name of the message type containing this field. - string package = field->file()->package(); - string containing_type = field->containing_type()->full_name() + "."; - string type = (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) ? - field->enum_type()->full_name() : field->message_type()->full_name(); + std::string package = field->file()->package(); + std::string containing_type = field->containing_type()->full_name() + "."; + std::string type = (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) + ? field->enum_type()->full_name() + : field->message_type()->full_name(); // |prefix| is advanced as we find separators '.' past the common package // prefix that yield common prefixes in the containing type's name and this @@ -1226,9 +1263,9 @@ string RelativeTypeName(const FieldDescriptor* field) { return type.substr(prefix); } -string JSExtensionsObjectName(const GeneratorOptions& options, - const FileDescriptor* from_file, - const Descriptor* desc) { +std::string JSExtensionsObjectName(const GeneratorOptions& options, + const FileDescriptor* from_file, + const Descriptor* desc) { if (desc->full_name() == "google.protobuf.bridge.MessageSet") { // TODO(haberman): fix this for the kImportCommonJs case. return "jspb.Message.messageSetExtensions"; @@ -1250,28 +1287,27 @@ const FieldDescriptor* MapFieldValue(const FieldDescriptor* field) { return field->message_type()->FindFieldByNumber(kMapValueField); } -string FieldDefinition(const GeneratorOptions& options, - const FieldDescriptor* field) { +std::string FieldDefinition(const GeneratorOptions& options, + const FieldDescriptor* field) { if (field->is_map()) { const FieldDescriptor* key_field = MapFieldKey(field); const FieldDescriptor* value_field = MapFieldValue(field); - string key_type = ProtoTypeName(options, key_field); - string value_type; + std::string key_type = ProtoTypeName(options, key_field); + std::string value_type; if (value_field->type() == FieldDescriptor::TYPE_ENUM || value_field->type() == FieldDescriptor::TYPE_MESSAGE) { value_type = RelativeTypeName(value_field); } else { value_type = ProtoTypeName(options, value_field); } - return StringPrintf("map<%s, %s> %s = %d;", - key_type.c_str(), - value_type.c_str(), - field->name().c_str(), + return StringPrintf("map<%s, %s> %s = %d;", key_type.c_str(), + value_type.c_str(), field->name().c_str(), field->number()); } else { - string qualifier = field->is_repeated() ? "repeated" : - (field->is_optional() ? "optional" : "required"); - string type, name; + std::string qualifier = + field->is_repeated() ? "repeated" + : (field->is_optional() ? "optional" : "required"); + std::string type, name; if (field->type() == FieldDescriptor::TYPE_ENUM || field->type() == FieldDescriptor::TYPE_MESSAGE) { type = RelativeTypeName(field); @@ -1283,23 +1319,13 @@ string FieldDefinition(const GeneratorOptions& options, type = ProtoTypeName(options, field); name = field->name(); } - return StringPrintf("%s %s %s = %d;", - qualifier.c_str(), - type.c_str(), - name.c_str(), - field->number()); + return StringPrintf("%s %s %s = %d;", qualifier.c_str(), type.c_str(), + name.c_str(), field->number()); } } -string FieldComments(const FieldDescriptor* field, BytesMode bytes_mode) { - string comments; - if (field->cpp_type() == FieldDescriptor::CPPTYPE_BOOL) { - comments += - " * Note that Boolean fields may be set to 0/1 when serialized from " - "a Java server.\n" - " * You should avoid comparisons like {@code val === true/false} in " - "those cases.\n"; - } +std::string FieldComments(const FieldDescriptor* field, BytesMode bytes_mode) { + std::string comments; if (field->type() == FieldDescriptor::TYPE_BYTES && bytes_mode == BYTES_U8) { comments += " * Note that Uint8Array is not supported on all browsers.\n" @@ -1309,9 +1335,7 @@ string FieldComments(const FieldDescriptor* field, BytesMode bytes_mode) { } bool ShouldGenerateExtension(const FieldDescriptor* field) { - return - field->is_extension() && - !IgnoreField(field); + return field->is_extension() && !IgnoreField(field); } bool HasExtensions(const Descriptor* desc) { @@ -1371,7 +1395,7 @@ bool IsExtendable(const Descriptor* desc) { // Returns the max index in the underlying data storage array beyond which the // extension object is used. -string GetPivot(const Descriptor* desc) { +std::string GetPivot(const Descriptor* desc) { static const int kDefaultPivot = 500; // Find the max field number @@ -1385,11 +1409,11 @@ string GetPivot(const Descriptor* desc) { int pivot = -1; if (IsExtendable(desc) || (max_field_number >= kDefaultPivot)) { - pivot = ((max_field_number + 1) < kDefaultPivot) ? - (max_field_number + 1) : kDefaultPivot; + pivot = ((max_field_number + 1) < kDefaultPivot) ? (max_field_number + 1) + : kDefaultPivot; } - return SimpleItoa(pivot); + return StrCat(pivot); } // Whether this field represents presence. For fields with presence, we @@ -1413,37 +1437,56 @@ bool HasFieldPresence(const GeneratorOptions& options, } // We use this to implement the semantics that same file can be generated -// multiple times, but the last one wins. We never actually write the files, -// but we keep a set of which descriptors were the final one for a given -// filename. +// multiple times, but only the last one keep the short name. Others all use +// long name with extra information to distinguish (For message and enum, the +// extra information is package name, for file level extension, the extra +// information is proto's filename). +// We never actually write the files, but we keep a set of which descriptors +// were the final one for a given filename. class FileDeduplicator { public: explicit FileDeduplicator(const GeneratorOptions& options) : error_on_conflict_(options.error_on_name_conflict) {} - bool AddFile(const string& filename, const void* desc, string* error) { - if (descs_by_filename_.find(filename) != descs_by_filename_.end()) { + // params: + // filenames: a pair of {short filename, full filename} + // (short filename don't have extra information, full filename + // contains extra information) + // desc: The Descriptor or SCC pointer or EnumDescriptor. + // error: The returned error information. + bool AddFile(const std::pair filenames, + const void* desc, std::string* error) { + if (descs_by_shortname_.find(filenames.first) != + descs_by_shortname_.end()) { if (error_on_conflict_) { - *error = "Name conflict: file name " + filename + + *error = "Name conflict: file name " + filenames.first + " would be generated by two descriptors"; return false; } - allowed_descs_.erase(descs_by_filename_[filename]); + // Change old pointer's actual name to full name. + auto short_name_desc = descs_by_shortname_[filenames.first]; + allowed_descs_actual_name_[short_name_desc] = + allowed_descs_full_name_[short_name_desc]; } + descs_by_shortname_[filenames.first] = desc; + allowed_descs_actual_name_[desc] = filenames.first; + allowed_descs_full_name_[desc] = filenames.second; - descs_by_filename_[filename] = desc; - allowed_descs_.insert(desc); return true; } - void GetAllowedSet(std::set* allowed_set) { - *allowed_set = allowed_descs_; + void GetAllowedMap(std::map* allowed_set) { + *allowed_set = allowed_descs_actual_name_; } private: bool error_on_conflict_; - std::map descs_by_filename_; - std::set allowed_descs_; + // The map that restores all the descs that are using short name as filename. + std::map descs_by_shortname_; + // The final actual filename map. + std::map allowed_descs_actual_name_; + // The full name map. + std::map allowed_descs_full_name_; }; void DepthFirstSearch(const FileDescriptor* file, @@ -1504,25 +1547,58 @@ void GenerateJspbFileOrder(const std::vector& input, // by choosing the last descriptor that writes each filename and permitting // only those to generate code. -bool GenerateJspbAllowedSet(const GeneratorOptions& options, +struct DepsGenerator { + std::vector operator()(const Descriptor* desc) const { + std::vector deps; + auto maybe_add = [&](const Descriptor* d) { + if (d) deps.push_back(d); + }; + for (int i = 0; i < desc->field_count(); i++) { + if (!IgnoreField(desc->field(i))) { + maybe_add(desc->field(i)->message_type()); + } + } + for (int i = 0; i < desc->extension_count(); i++) { + maybe_add(desc->extension(i)->message_type()); + maybe_add(desc->extension(i)->containing_type()); + } + for (int i = 0; i < desc->nested_type_count(); i++) { + maybe_add(desc->nested_type(i)); + } + maybe_add(desc->containing_type()); + + return deps; + } +}; + +bool GenerateJspbAllowedMap(const GeneratorOptions& options, const std::vector& files, - std::set* allowed_set, - string* error) { + std::map* allowed_set, + SCCAnalyzer* analyzer, + std::string* error) { std::vector files_ordered; GenerateJspbFileOrder(files, &files_ordered); // Choose the last descriptor for each filename. FileDeduplicator dedup(options); + std::set added; for (int i = 0; i < files_ordered.size(); i++) { for (int j = 0; j < files_ordered[i]->message_type_count(); j++) { const Descriptor* desc = files_ordered[i]->message_type(j); - if (!dedup.AddFile(GetMessageFileName(options, desc), desc, error)) { + if (added.insert(analyzer->GetSCC(desc)).second && + !dedup.AddFile( + std::make_pair( + GetMessagesFileName(options, analyzer->GetSCC(desc), false), + GetMessagesFileName(options, analyzer->GetSCC(desc), true)), + analyzer->GetSCC(desc), error)) { return false; } } for (int j = 0; j < files_ordered[i]->enum_type_count(); j++) { const EnumDescriptor* desc = files_ordered[i]->enum_type(j); - if (!dedup.AddFile(GetEnumFileName(options, desc), desc, error)) { + if (!dedup.AddFile(std::make_pair(GetEnumFileName(options, desc, false), + GetEnumFileName(options, desc, true)), + desc, error)) { return false; } } @@ -1537,14 +1613,17 @@ bool GenerateJspbAllowedSet(const GeneratorOptions& options, } if (has_extension) { - if (!dedup.AddFile(GetExtensionFileName(options, files_ordered[i]), - files_ordered[i], error)) { + if (!dedup.AddFile( + std::make_pair( + GetExtensionFileName(options, files_ordered[i], false), + GetExtensionFileName(options, files_ordered[i], true)), + files_ordered[i], error)) { return false; } } } - dedup.GetAllowedSet(allowed_set); + dedup.GetAllowedMap(allowed_set); return true; } @@ -1554,9 +1633,9 @@ bool GenerateJspbAllowedSet(const GeneratorOptions& options, void EmbedCodeAnnotations(const GeneratedCodeInfo& annotations, io::Printer* printer) { // Serialize annotations proto into base64 string. - string meta_content; + std::string meta_content; annotations.SerializeToString(&meta_content); - string meta_64; + std::string meta_64; Base64Escape(meta_content, &meta_64); // Print base64 encoded annotations at the end of output file in @@ -1565,27 +1644,36 @@ void EmbedCodeAnnotations(const GeneratedCodeInfo& annotations, printer->Print("\n// $encoded_proto$\n", "encoded_proto", meta_64); } +bool IsWellKnownTypeFile(const FileDescriptor* file) { + return HasPrefixString(file->name(), "google/protobuf/"); +} + } // anonymous namespace void Generator::GenerateHeader(const GeneratorOptions& options, + const FileDescriptor* file, io::Printer* printer) const { - printer->Print("/**\n" - " * @fileoverview\n" - " * @enhanceable\n" - " * @suppress {messageConventions} JS Compiler reports an " - "error if a variable or\n" - " * field starts with 'MSG_' and isn't a translatable " - "message.\n" - " * @public\n" - " */\n" - "// GENERATED CODE -- DO NOT EDIT!\n" - "\n"); + if (file != nullptr) { + printer->Print("// source: $filename$\n", "filename", file->name()); + } + printer->Print( + "/**\n" + " * @fileoverview\n" + " * @enhanceable\n" + " * @suppress {messageConventions} JS Compiler reports an " + "error if a variable or\n" + " * field starts with 'MSG_' and isn't a translatable " + "message.\n" + " * @public\n" + " */\n" + "// GENERATED CODE -- DO NOT EDIT!\n" + "\n"); } void Generator::FindProvidesForFile(const GeneratorOptions& options, io::Printer* printer, const FileDescriptor* file, - std::set* provided) const { + std::set* provided) const { for (int i = 0; i < file->message_type_count(); i++) { FindProvidesForMessage(options, printer, file->message_type(i), provided); } @@ -1597,7 +1685,7 @@ void Generator::FindProvidesForFile(const GeneratorOptions& options, void Generator::FindProvides(const GeneratorOptions& options, io::Printer* printer, const std::vector& files, - std::set* provided) const { + std::set* provided) const { for (int i = 0; i < files.size(); i++) { FindProvidesForFile(options, printer, files[i], provided); } @@ -1605,41 +1693,60 @@ void Generator::FindProvides(const GeneratorOptions& options, printer->Print("\n"); } -void Generator::FindProvidesForMessage( - const GeneratorOptions& options, - io::Printer* printer, - const Descriptor* desc, - std::set* provided) const { +void FindProvidesForOneOfEnum(const GeneratorOptions& options, + const OneofDescriptor* oneof, + std::set* provided) { + std::string name = GetMessagePath(options, oneof->containing_type()) + "." + + JSOneofName(oneof) + "Case"; + provided->insert(name); +} + +void FindProvidesForOneOfEnums(const GeneratorOptions& options, + io::Printer* printer, const Descriptor* desc, + std::set* provided) { + if (HasOneofFields(desc)) { + for (int i = 0; i < desc->oneof_decl_count(); i++) { + if (IgnoreOneof(desc->oneof_decl(i))) { + continue; + } + FindProvidesForOneOfEnum(options, desc->oneof_decl(i), provided); + } + } +} + +void Generator::FindProvidesForMessage(const GeneratorOptions& options, + io::Printer* printer, + const Descriptor* desc, + std::set* provided) const { if (IgnoreMessage(desc)) { return; } - string name = GetMessagePath(options, desc); + std::string name = GetMessagePath(options, desc); provided->insert(name); for (int i = 0; i < desc->enum_type_count(); i++) { - FindProvidesForEnum(options, printer, desc->enum_type(i), - provided); + FindProvidesForEnum(options, printer, desc->enum_type(i), provided); } + + FindProvidesForOneOfEnums(options, printer, desc, provided); + for (int i = 0; i < desc->nested_type_count(); i++) { - FindProvidesForMessage(options, printer, desc->nested_type(i), - provided); + FindProvidesForMessage(options, printer, desc->nested_type(i), provided); } } - void Generator::FindProvidesForEnum(const GeneratorOptions& options, io::Printer* printer, const EnumDescriptor* enumdesc, - std::set* provided) const { - string name = GetEnumPath(options, enumdesc); + std::set* provided) const { + std::string name = GetEnumPath(options, enumdesc); provided->insert(name); } void Generator::FindProvidesForFields( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const std::vector& fields, - std::set* provided) const { + std::set* provided) const { for (int i = 0; i < fields.size(); i++) { const FieldDescriptor* field = fields[i]; @@ -1647,16 +1754,16 @@ void Generator::FindProvidesForFields( continue; } - string name = GetFilePath(options, field->file()) + "." + - JSObjectFieldName(options, field); + std::string name = GetNamespace(options, field->file()) + "." + + JSObjectFieldName(options, field); provided->insert(name); } } void Generator::GenerateProvides(const GeneratorOptions& options, io::Printer* printer, - std::set* provided) const { - for (std::set::iterator it = provided->begin(); + std::set* provided) const { + for (std::set::iterator it = provided->begin(); it != provided->end(); ++it) { if (options.import_style == GeneratorOptions::kImportClosure) { printer->Print("goog.provide('$name$');\n", "name", *it); @@ -1671,7 +1778,7 @@ void Generator::GenerateProvides(const GeneratorOptions& options, // Do not use global scope in strict mode if (options.import_style == GeneratorOptions::kImportCommonJsStrict) { - string namespaceObject = *it; + std::string namespaceObject = *it; // Remove "proto." from the namespace object GOOGLE_CHECK_EQ(0, namespaceObject.compare(0, 6, "proto.")); namespaceObject.erase(0, 6); @@ -1685,30 +1792,37 @@ void Generator::GenerateProvides(const GeneratorOptions& options, } } -void Generator::GenerateRequiresForMessage(const GeneratorOptions& options, - io::Printer* printer, - const Descriptor* desc, - std::set* provided) const { - std::set required; - std::set forwards; +void Generator::GenerateRequiresForSCC(const GeneratorOptions& options, + io::Printer* printer, const SCC* scc, + std::set* provided) const { + std::set required; + std::set forwards; bool have_message = false; - FindRequiresForMessage(options, desc, - &required, &forwards, &have_message); + bool has_extension = false; + bool has_map = false; + for (auto desc : scc->descriptors) { + if (desc->containing_type() == nullptr) { + FindRequiresForMessage(options, desc, &required, &forwards, + &have_message); + has_extension = (has_extension || HasExtensions(desc)); + has_map = (has_map || HasMap(options, desc)); + } + } GenerateRequiresImpl(options, printer, &required, &forwards, provided, /* require_jspb = */ have_message, - /* require_extension = */ HasExtensions(desc), - /* require_map = */ HasMap(options, desc)); + /* require_extension = */ has_extension, + /* require_map = */ has_map); } void Generator::GenerateRequiresForLibrary( const GeneratorOptions& options, io::Printer* printer, const std::vector& files, - std::set* provided) const { + std::set* provided) const { GOOGLE_CHECK_EQ(options.import_style, GeneratorOptions::kImportClosure); // For Closure imports we need to import every message type individually. - std::set required; - std::set forwards; + std::set required; + std::set forwards; bool have_extensions = false; bool have_map = false; bool have_message = false; @@ -1736,7 +1850,7 @@ void Generator::GenerateRequiresForLibrary( continue; } if (extension->containing_type()->full_name() != - "google.protobuf.bridge.MessageSet") { + "google.protobuf.bridge.MessageSet") { required.insert(GetMessagePath(options, extension->containing_type())); } FindRequiresForField(options, extension, &required, &forwards); @@ -1753,9 +1867,9 @@ void Generator::GenerateRequiresForLibrary( void Generator::GenerateRequiresForExtensions( const GeneratorOptions& options, io::Printer* printer, const std::vector& fields, - std::set* provided) const { - std::set required; - std::set forwards; + std::set* provided) const { + std::set required; + std::set forwards; for (int i = 0; i < fields.size(); i++) { const FieldDescriptor* field = fields[i]; if (IgnoreField(field)) { @@ -1772,9 +1886,9 @@ void Generator::GenerateRequiresForExtensions( void Generator::GenerateRequiresImpl(const GeneratorOptions& options, io::Printer* printer, - std::set* required, - std::set* forwards, - std::set* provided, + std::set* required, + std::set* forwards, + std::set* provided, bool require_jspb, bool require_extension, bool require_map) const { if (require_jspb) { @@ -1790,13 +1904,12 @@ void Generator::GenerateRequiresImpl(const GeneratorOptions& options, required->insert("jspb.Map"); } - std::set::iterator it; + std::set::iterator it; for (it = required->begin(); it != required->end(); ++it) { if (provided->find(*it) != provided->end()) { continue; } - printer->Print("goog.require('$name$');\n", - "name", *it); + printer->Print("goog.require('$name$');\n", "name", *it); } printer->Print("\n"); @@ -1805,8 +1918,7 @@ void Generator::GenerateRequiresImpl(const GeneratorOptions& options, if (provided->find(*it) != provided->end()) { continue; } - printer->Print("goog.forwardDeclare('$name$');\n", - "name", *it); + printer->Print("goog.forwardDeclare('$name$');\n", "name", *it); } } @@ -1814,13 +1926,11 @@ bool NamespaceOnly(const Descriptor* desc) { return false; } -void Generator::FindRequiresForMessage( - const GeneratorOptions& options, - const Descriptor* desc, - std::set* required, - std::set* forwards, - bool* have_message) const { - +void Generator::FindRequiresForMessage(const GeneratorOptions& options, + const Descriptor* desc, + std::set* required, + std::set* forwards, + bool* have_message) const { if (!NamespaceOnly(desc)) { *have_message = true; @@ -1849,32 +1959,31 @@ void Generator::FindRequiresForMessage( void Generator::FindRequiresForField(const GeneratorOptions& options, const FieldDescriptor* field, - std::set* required, - std::set* forwards) const { - if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM && - // N.B.: file-level extensions with enum type do *not* create - // dependencies, as per original codegen. - !(field->is_extension() && field->extension_scope() == NULL)) { - if (options.add_require_for_enums) { - required->insert(GetEnumPath(options, field->enum_type())); - } else { - forwards->insert(GetEnumPath(options, field->enum_type())); - } - } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - if (!IgnoreMessage(field->message_type())) { - required->insert(GetMessagePath(options, field->message_type())); - } + std::set* required, + std::set* forwards) const { + if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM && + // N.B.: file-level extensions with enum type do *not* create + // dependencies, as per original codegen. + !(field->is_extension() && field->extension_scope() == nullptr)) { + if (options.add_require_for_enums) { + required->insert(GetEnumPath(options, field->enum_type())); + } else { + forwards->insert(GetEnumPath(options, field->enum_type())); + } + } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { + if (!IgnoreMessage(field->message_type())) { + required->insert(GetMessagePath(options, field->message_type())); } + } } -void Generator::FindRequiresForExtension(const GeneratorOptions& options, - const FieldDescriptor* field, - std::set* required, - std::set* forwards) const { - if (field->containing_type()->full_name() != "google.protobuf.bridge.MessageSet") { - required->insert(GetMessagePath(options, field->containing_type())); - } - FindRequiresForField(options, field, required, forwards); +void Generator::FindRequiresForExtension( + const GeneratorOptions& options, const FieldDescriptor* field, + std::set* required, std::set* forwards) const { + if (field->containing_type()->full_name() != "google.protobuf.bridge.MessageSet") { + required->insert(GetMessagePath(options, field->containing_type())); + } + FindRequiresForField(options, field, required, forwards); } void Generator::GenerateTestOnly(const GeneratorOptions& options, @@ -1888,6 +1997,10 @@ void Generator::GenerateTestOnly(const GeneratorOptions& options, void Generator::GenerateClassesAndEnums(const GeneratorOptions& options, io::Printer* printer, const FileDescriptor* file) const { + for (int i = 0; i < file->message_type_count(); i++) { + GenerateClassConstructorAndDeclareExtensionFieldInfo(options, printer, + file->message_type(i)); + } for (int i = 0; i < file->message_type_count(); i++) { GenerateClass(options, printer, file->message_type(i)); } @@ -1905,7 +2018,6 @@ void Generator::GenerateClass(const GeneratorOptions& options, if (!NamespaceOnly(desc)) { printer->Print("\n"); - GenerateClassConstructor(options, printer, desc); GenerateClassFieldInfo(options, printer, desc); @@ -1931,9 +2043,6 @@ void Generator::GenerateClass(const GeneratorOptions& options, if (!NamespaceOnly(desc)) { GenerateClassRegistration(options, printer, desc); GenerateClassFields(options, printer, desc); - if (IsExtendable(desc) && desc->full_name() != "google.protobuf.bridge.MessageSet") { - GenerateClassExtensionFieldInfo(options, printer, desc); - } if (options.import_style != GeneratorOptions::kImportClosure) { for (int i = 0; i < desc->extension_count(); i++) { @@ -1962,28 +2071,51 @@ void Generator::GenerateClassConstructor(const GeneratorOptions& options, " * @constructor\n" " */\n" "$classprefix$$classname$ = function(opt_data) {\n", - "classprefix", GetMessagePathPrefix(options, desc), - "classname", desc->name()); + "classprefix", GetMessagePathPrefix(options, desc), "classname", + desc->name()); printer->Annotate("classname", desc); - string message_id = GetMessageId(desc); + std::string message_id = GetMessageId(desc); printer->Print( " jspb.Message.initialize(this, opt_data, $messageId$, $pivot$, " "$rptfields$, $oneoffields$);\n", - "messageId", !message_id.empty() ? - ("'" + message_id + "'") : - (IsResponse(desc) ? "''" : "0"), - "pivot", GetPivot(desc), - "rptfields", RepeatedFieldsArrayName(options, desc), - "oneoffields", OneofFieldsArrayName(options, desc)); + "messageId", + !message_id.empty() ? ("'" + message_id + "'") + : (IsResponse(desc) ? "''" : "0"), + "pivot", GetPivot(desc), "rptfields", + RepeatedFieldsArrayName(options, desc), "oneoffields", + OneofFieldsArrayName(options, desc)); printer->Print( "};\n" "goog.inherits($classname$, jspb.Message);\n" "if (goog.DEBUG && !COMPILED) {\n" + // displayName overrides Function.prototype.displayName + // http://google3/javascript/externs/es3.js?l=511 + " /**\n" + " * @public\n" + " * @override\n" + " */\n" " $classname$.displayName = '$classname$';\n" "}\n", "classname", GetMessagePath(options, desc)); } +void Generator::GenerateClassConstructorAndDeclareExtensionFieldInfo( + const GeneratorOptions& options, io::Printer* printer, + const Descriptor* desc) const { + if (!NamespaceOnly(desc)) { + GenerateClassConstructor(options, printer, desc); + if (IsExtendable(desc) && desc->full_name() != "google.protobuf.bridge.MessageSet") { + GenerateClassExtensionFieldInfo(options, printer, desc); + } + } + for (int i = 0; i < desc->nested_type_count(); i++) { + if (!IgnoreMessage(desc->nested_type(i))) { + GenerateClassConstructorAndDeclareExtensionFieldInfo( + options, printer, desc->nested_type(i)); + } + } +} + void Generator::GenerateClassFieldInfo(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const { @@ -1996,9 +2128,9 @@ void Generator::GenerateClassFieldInfo(const GeneratorOptions& options, " */\n" "$classname$$rptfieldarray$ = $rptfields$;\n" "\n", - "classname", GetMessagePath(options, desc), - "rptfieldarray", kRepeatedFieldArrayName, - "rptfields", RepeatedFieldNumberList(options, desc)); + "classname", GetMessagePath(options, desc), "rptfieldarray", + kRepeatedFieldArrayName, "rptfields", + RepeatedFieldNumberList(options, desc)); } if (HasOneofFields(desc)) { @@ -2017,9 +2149,8 @@ void Generator::GenerateClassFieldInfo(const GeneratorOptions& options, " */\n" "$classname$$oneofgrouparray$ = $oneofgroups$;\n" "\n", - "classname", GetMessagePath(options, desc), - "oneofgrouparray", kOneofGroupArrayName, - "oneofgroups", OneofGroupList(desc)); + "classname", GetMessagePath(options, desc), "oneofgrouparray", + kOneofGroupArrayName, "oneofgroups", OneofGroupList(desc)); for (int i = 0; i < desc->oneof_decl_count(); i++) { if (IgnoreOneof(desc->oneof_decl(i))) { @@ -2041,8 +2172,7 @@ void Generator::GenerateClassXid(const GeneratorOptions& options, } void Generator::GenerateOneofCaseDefinition( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const OneofDescriptor* oneof) const { printer->Print( "/**\n" @@ -2050,9 +2180,8 @@ void Generator::GenerateOneofCaseDefinition( " */\n" "$classname$.$oneof$Case = {\n" " $upcase$_NOT_SET: 0", - "classname", GetMessagePath(options, oneof->containing_type()), - "oneof", JSOneofName(oneof), - "upcase", ToEnumCase(oneof->name())); + "classname", GetMessagePath(options, oneof->containing_type()), "oneof", + JSOneofName(oneof), "upcase", ToEnumCase(oneof->name())); for (int i = 0; i < oneof->field_count(); i++) { if (IgnoreField(oneof->field(i))) { @@ -2062,8 +2191,8 @@ void Generator::GenerateOneofCaseDefinition( printer->Print( ",\n" " $upcase$: $number$", - "upcase", ToEnumCase(oneof->field(i)->name()), - "number", JSFieldIndex(oneof->field(i))); + "upcase", ToEnumCase(oneof->field(i)->name()), "number", + JSFieldIndex(oneof->field(i))); printer->Annotate("upcase", oneof->field(i)); } @@ -2079,9 +2208,8 @@ void Generator::GenerateOneofCaseDefinition( "computeOneofCase(this, $class$.oneofGroups_[$oneofindex$]));\n" "};\n" "\n", - "class", GetMessagePath(options, oneof->containing_type()), - "oneof", JSOneofName(oneof), - "oneofindex", JSOneofIndex(oneof)); + "class", GetMessagePath(options, oneof->containing_type()), "oneof", + JSOneofName(oneof), "oneofindex", JSOneofIndex(oneof)); } void Generator::GenerateClassToObject(const GeneratorOptions& options, @@ -2092,17 +2220,17 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options, "\n" "if (jspb.Message.GENERATE_TO_OBJECT) {\n" "/**\n" - " * Creates an object representation of this proto suitable for use in " - "Soy templates.\n" + " * Creates an object representation of this proto.\n" " * Field names that are reserved in JavaScript and will be renamed to " "pb_name.\n" + " * Optional fields that are not set will be set to undefined.\n" " * To access a reserved field use, foo.pb_, eg, foo.pb_default.\n" " * For the list of reserved names please see:\n" - " * com.google.apps.jspb.JsClassTemplate.JS_RESERVED_WORDS.\n" - " * @param {boolean=} opt_includeInstance Whether to include the JSPB " - "instance\n" - " * for transitional soy proto support: http://goto/soy-param-" - "migration\n" + " * net/proto2/compiler/js/internal/generator.cc#kKeyword.\n" + " * @param {boolean=} opt_includeInstance Deprecated. whether to include " + "the\n" + " * JSPB instance for transitional soy proto support:\n" + " * http://goto/soy-param-migration\n" " * @return {!Object}\n" " */\n" "$classname$.prototype.toObject = function(opt_includeInstance) {\n" @@ -2112,9 +2240,9 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options, "\n" "/**\n" " * Static version of the {@see toObject} method.\n" - " * @param {boolean|undefined} includeInstance Whether to include the " - "JSPB\n" - " * instance for transitional soy proto support:\n" + " * @param {boolean|undefined} includeInstance Deprecated. Whether to " + "include\n" + " * the JSPB instance for transitional soy proto support:\n" " * http://goto/soy-param-migration\n" " * @param {!$classname$} msg The msg instance to transform.\n" " * @return {!Object}\n" @@ -2170,62 +2298,64 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options, } void Generator::GenerateFieldValueExpression(io::Printer* printer, - const char *obj_reference, + const char* obj_reference, const FieldDescriptor* field, bool use_default) const { - bool is_float_or_double = + const bool is_float_or_double = field->cpp_type() == FieldDescriptor::CPPTYPE_FLOAT || field->cpp_type() == FieldDescriptor::CPPTYPE_DOUBLE; - if (use_default) { - if (is_float_or_double) { - // Coerce "Nan" and "Infinity" to actual float values. - // - // This will change null to 0, but that doesn't matter since we're getting - // with a default. - printer->Print("+"); - } - + const bool is_boolean = field->cpp_type() == FieldDescriptor::CPPTYPE_BOOL; + + const string with_default = use_default ? "WithDefault" : ""; + const string default_arg = + use_default ? StrCat(", ", JSFieldDefault(field)) : ""; + const string cardinality = field->is_repeated() ? "Repeated" : ""; + string type = ""; + if (is_float_or_double) { + type = "FloatingPoint"; + } + if (is_boolean) { + type = "Boolean"; + } + + // Prints the appropriate function, among: + // - getField + // - getBooleanField + // - getFloatingPointField => Replaced by getOptionalFloatingPointField to + // preserve backward compatibility. + // - getFieldWithDefault + // - getBooleanFieldWithDefault + // - getFloatingPointFieldWithDefault + // - getRepeatedField + // - getRepeatedBooleanField + // - getRepeatedFloatingPointField + if (is_float_or_double && !field->is_repeated() && !use_default) { printer->Print( - "jspb.Message.getFieldWithDefault($obj$, $index$, $default$)", - "obj", obj_reference, - "index", JSFieldIndex(field), - "default", JSFieldDefault(field)); + "jspb.Message.getOptionalFloatingPointField($obj$, " + "$index$$default$)", + "obj", obj_reference, "index", JSFieldIndex(field), "default", + default_arg); } else { - if (is_float_or_double) { - if (field->is_required()) { - // Use "+" to convert all fields to numeric (including null). - printer->Print( - "+jspb.Message.getField($obj$, $index$)", - "index", JSFieldIndex(field), - "obj", obj_reference); - } else { - // Converts "NaN" and "Infinity" while preserving null. - printer->Print( - "jspb.Message.get$cardinality$FloatingPointField($obj$, $index$)", - "cardinality", field->is_repeated() ? "Repeated" : "Optional", - "index", JSFieldIndex(field), - "obj", obj_reference); - } - } else { - printer->Print("jspb.Message.get$cardinality$Field($obj$, $index$)", - "cardinality", field->is_repeated() ? "Repeated" : "", - "index", JSFieldIndex(field), - "obj", obj_reference); - } + printer->Print( + "jspb.Message.get$cardinality$$type$Field$with_default$($obj$, " + "$index$$default$)", + "cardinality", cardinality, "type", type, "with_default", with_default, + "obj", obj_reference, "index", JSFieldIndex(field), "default", + default_arg); } } void Generator::GenerateClassFieldToObject(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { - printer->Print("$fieldname$: ", - "fieldname", JSObjectFieldName(options, field)); + printer->Print("$fieldname$: ", "fieldname", + JSObjectFieldName(options, field)); if (field->is_map()) { const FieldDescriptor* value_field = MapFieldValue(field); // If the map values are of a message type, we must provide their static // toObject() method; otherwise we pass undefined for that argument. - string value_to_object; + std::string value_to_object; if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { value_to_object = GetMessagePath(options, value_field->message_type()) + ".toObject"; @@ -2240,21 +2370,23 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options, // Message field. if (field->is_repeated()) { { - printer->Print("jspb.Message.toObjectList(msg.get$getter$(),\n" - " $type$.toObject, includeInstance)", - "getter", JSGetterName(options, field), - "type", SubmessageTypeRef(options, field)); + printer->Print( + "jspb.Message.toObjectList(msg.get$getter$(),\n" + " $type$.toObject, includeInstance)", + "getter", JSGetterName(options, field), "type", + SubmessageTypeRef(options, field)); } } else { - printer->Print("(f = msg.get$getter$()) && " - "$type$.toObject(includeInstance, f)", - "getter", JSGetterName(options, field), - "type", SubmessageTypeRef(options, field)); + printer->Print( + "(f = msg.get$getter$()) && " + "$type$.toObject(includeInstance, f)", + "getter", JSGetterName(options, field), "type", + SubmessageTypeRef(options, field)); } } else if (field->type() == FieldDescriptor::TYPE_BYTES) { // For bytes fields we want to always return the B64 data. - printer->Print("msg.get$getter$()", - "getter", JSGetterName(options, field, BYTES_B64)); + printer->Print("msg.get$getter$()", "getter", + JSGetterName(options, field, BYTES_B64)); } else { bool use_default = field->has_default_value(); @@ -2273,19 +2405,61 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options, // We are migrating the accessors to return defaults instead of null, but // it may take longer to migrate toObject (or we might not want to do it at // all). So we want to generate independent code. + // The accessor for unset optional values without default should return + // null. Those are converted to undefined in the generated object. + if (!use_default) { + printer->Print("(f = "); + } GenerateFieldValueExpression(printer, "msg", field, use_default); + if (!use_default) { + printer->Print(") == null ? undefined : f"); + } + } +} + +void Generator::GenerateObjectTypedef(const GeneratorOptions& options, + io::Printer* printer, + const Descriptor* desc) const { + // TODO(b/122687752): Consider renaming nested messages called ObjectFormat + // to prevent collisions. + const std::string type_name = GetMessagePath(options, desc) + ".ObjectFormat"; + + printer->Print( + "/**\n" + " * The raw object form of $messageName$ as accepted by the `fromObject` " + "method.\n" + " * @record\n" + " */\n" + "$typeName$ = function() {\n", + "messageName", desc->name(), "typeName", type_name); + + for (int i = 0; i < desc->field_count(); i++) { + if (i > 0) { + printer->Print("\n"); + } + printer->Print( + " /** @type {$fieldType$|undefined} */\n" + " this.$fieldName$;\n", + "fieldName", JSObjectFieldName(options, desc->field(i)), + // TODO(b/121097361): Add type checking for field values. + "fieldType", "?"); } + + printer->Print("};\n\n"); } void Generator::GenerateClassFromObject(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const { + printer->Print("if (jspb.Message.GENERATE_FROM_OBJECT) {\n\n"); + + GenerateObjectTypedef(options, printer, desc); + printer->Print( - "if (jspb.Message.GENERATE_FROM_OBJECT) {\n" "/**\n" " * Loads data from an object into a new instance of this proto.\n" - " * @param {!Object} obj The object representation of this proto to\n" - " * load the data from.\n" + " * @param {!$classname$.ObjectFormat} obj\n" + " * The object representation of this proto to load the data from.\n" " * @return {!$classname$}\n" " */\n" "$classname$.fromObject = function(obj) {\n" @@ -2294,18 +2468,19 @@ void Generator::GenerateClassFromObject(const GeneratorOptions& options, for (int i = 0; i < desc->field_count(); i++) { const FieldDescriptor* field = desc->field(i); - GenerateClassFieldFromObject(options, printer, field); + if (!IgnoreField(field)) { + GenerateClassFieldFromObject(options, printer, field); + } } printer->Print( " return msg;\n" "};\n" - "}\n"); + "}\n\n"); } void Generator::GenerateClassFieldFromObject( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { if (field->is_map()) { const FieldDescriptor* value_field = MapFieldValue(field); @@ -2313,51 +2488,49 @@ void Generator::GenerateClassFieldFromObject( // Since the map values are of message type, we have to do some extra work // to recursively call fromObject() on them before setting the map field. printer->Print( - " goog.isDef(obj.$name$) && jspb.Message.setWrapperField(\n" + " obj.$name$ && jspb.Message.setWrapperField(\n" " msg, $index$, jspb.Map.fromObject(obj.$name$, $fieldclass$, " "$fieldclass$.fromObject));\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field), - "fieldclass", GetMessagePath(options, value_field->message_type())); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field), "fieldclass", + GetMessagePath(options, value_field->message_type())); } else { // `msg` is a newly-constructed message object that has not yet built any // map containers wrapping underlying arrays, so we can simply directly // set the array here without fear of a stale wrapper. printer->Print( - " goog.isDef(obj.$name$) && " + " obj.$name$ && " "jspb.Message.setField(msg, $index$, obj.$name$);\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field)); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field)); } } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { // Message field (singular or repeated) if (field->is_repeated()) { { printer->Print( - " goog.isDef(obj.$name$) && " + " obj.$name$ && " "jspb.Message.setRepeatedWrapperField(\n" - " msg, $index$, goog.array.map(obj.$name$, function(i) {\n" - " return $fieldclass$.fromObject(i);\n" - " }));\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field), - "fieldclass", SubmessageTypeRef(options, field)); + " msg, $index$, obj.$name$.map(\n" + " $fieldclass$.fromObject));\n", + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field), "fieldclass", + SubmessageTypeRef(options, field)); } } else { printer->Print( - " goog.isDef(obj.$name$) && jspb.Message.setWrapperField(\n" + " obj.$name$ && jspb.Message.setWrapperField(\n" " msg, $index$, $fieldclass$.fromObject(obj.$name$));\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field), - "fieldclass", SubmessageTypeRef(options, field)); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field), "fieldclass", SubmessageTypeRef(options, field)); } } else { // Simple (primitive) field. printer->Print( - " goog.isDef(obj.$name$) && jspb.Message.setField(msg, $index$, " + " obj.$name$ != null && jspb.Message.setField(msg, $index$, " "obj.$name$);\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field)); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field)); } } @@ -2384,15 +2557,13 @@ void Generator::GenerateClassFields(const GeneratorOptions& options, } } -void GenerateBytesWrapper(const GeneratorOptions& options, - io::Printer* printer, - const FieldDescriptor* field, - BytesMode bytes_mode) { - string type = JSFieldTypeAnnotation( - options, field, - /* is_setter_argument = */ false, - /* force_present = */ false, - /* singular_if_not_packed = */ false, bytes_mode); +void GenerateBytesWrapper(const GeneratorOptions& options, io::Printer* printer, + const FieldDescriptor* field, BytesMode bytes_mode) { + std::string type = + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ false, + /* singular_if_not_packed = */ false, bytes_mode); printer->Print( "/**\n" " * $fielddef$\n" @@ -2406,14 +2577,13 @@ void GenerateBytesWrapper(const GeneratorOptions& options, "};\n" "\n" "\n", - "fielddef", FieldDefinition(options, field), - "comment", FieldComments(field, bytes_mode), - "type", type, - "class", GetMessagePath(options, field->containing_type()), - "name", JSGetterName(options, field, bytes_mode), - "list", field->is_repeated() ? "List" : "", - "suffix", JSByteGetterSuffix(bytes_mode), - "defname", JSGetterName(options, field, BYTES_DEFAULT)); + "fielddef", FieldDefinition(options, field), "comment", + FieldComments(field, bytes_mode), "type", type, "class", + GetMessagePath(options, field->containing_type()), "name", + JSGetterName(options, field, bytes_mode), "list", + field->is_repeated() ? "List" : "", "suffix", + JSByteGetterSuffix(bytes_mode), "defname", + JSGetterName(options, field, BYTES_DEFAULT)); } void Generator::GenerateClassField(const GeneratorOptions& options, @@ -2423,18 +2593,16 @@ void Generator::GenerateClassField(const GeneratorOptions& options, const FieldDescriptor* key_field = MapFieldKey(field); const FieldDescriptor* value_field = MapFieldValue(field); // Map field: special handling to instantiate the map object on demand. - string key_type = - JSFieldTypeAnnotation( - options, key_field, - /* is_setter_argument = */ false, - /* force_present = */ true, - /* singular_if_not_packed = */ false); - string value_type = - JSFieldTypeAnnotation( - options, value_field, - /* is_setter_argument = */ false, - /* force_present = */ true, - /* singular_if_not_packed = */ false); + std::string key_type = + JSFieldTypeAnnotation(options, key_field, + /* is_setter_argument = */ false, + /* force_present = */ true, + /* singular_if_not_packed = */ false); + std::string value_type = + JSFieldTypeAnnotation(options, value_field, + /* is_setter_argument = */ false, + /* force_present = */ true, + /* singular_if_not_packed = */ false); printer->Print( "/**\n" @@ -2443,15 +2611,13 @@ void Generator::GenerateClassField(const GeneratorOptions& options, " * empty, instead returning `undefined`\n" " * @return {!jspb.Map<$keytype$,$valuetype$>}\n" " */\n", - "fielddef", FieldDefinition(options, field), - "keytype", key_type, + "fielddef", FieldDefinition(options, field), "keytype", key_type, "valuetype", value_type); printer->Print( "$class$.prototype.$gettername$ = function(opt_noLazyCreate) {\n" " return /** @type {!jspb.Map<$keytype$,$valuetype$>} */ (\n", "class", GetMessagePath(options, field->containing_type()), - "gettername", "get" + JSGetterName(options, field), - "keytype", key_type, + "gettername", "get" + JSGetterName(options, field), "keytype", key_type, "valuetype", value_type); printer->Annotate("gettername", field); printer->Print( @@ -2464,12 +2630,12 @@ void Generator::GenerateClassField(const GeneratorOptions& options, " $messageType$", "messageType", GetMessagePath(options, value_field->message_type())); } else { - printer->Print(",\n" + printer->Print( + ",\n" " null"); } - printer->Print( - "));\n"); + printer->Print("));\n"); printer->Print( "};\n" @@ -2485,12 +2651,12 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "$comment$" " * @return {$type$}\n" " */\n", - "fielddef", FieldDefinition(options, field), - "comment", FieldComments(field, BYTES_DEFAULT), - "type", JSFieldTypeAnnotation(options, field, - /* is_setter_argument = */ false, - /* force_present = */ false, - /* singular_if_not_packed = */ false)); + "fielddef", FieldDefinition(options, field), "comment", + FieldComments(field, BYTES_DEFAULT), "type", + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ false, + /* singular_if_not_packed = */ false)); printer->Print( "$class$.prototype.$gettername$ = function() {\n" " return /** @type{$type$} */ (\n" @@ -2500,16 +2666,15 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "\n" "\n", "class", GetMessagePath(options, field->containing_type()), - "gettername", "get" + JSGetterName(options, field), - "type", JSFieldTypeAnnotation(options, field, - /* is_setter_argument = */ false, - /* force_present = */ false, - /* singular_if_not_packed = */ false), - "rpt", (field->is_repeated() ? "Repeated" : ""), - "index", JSFieldIndex(field), - "wrapperclass", SubmessageTypeRef(options, field), - "required", (field->label() == FieldDescriptor::LABEL_REQUIRED ? - ", 1" : "")); + "gettername", "get" + JSGetterName(options, field), "type", + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ false, + /* singular_if_not_packed = */ false), + "rpt", (field->is_repeated() ? "Repeated" : ""), "index", + JSFieldIndex(field), "wrapperclass", SubmessageTypeRef(options, field), + "required", + (field->label() == FieldDescriptor::LABEL_REQUIRED ? ", 1" : "")); printer->Annotate("gettername", field); printer->Print( "/** @param {$optionaltype$} value$returndoc$ */\n" @@ -2520,11 +2685,11 @@ void Generator::GenerateClassField(const GeneratorOptions& options, /* is_setter_argument = */ true, /* force_present = */ false, /* singular_if_not_packed = */ false), - "returndoc", JSReturnDoc(options, field), - "class", GetMessagePath(options, field->containing_type()), - "settername", "set" + JSGetterName(options, field), - "oneoftag", (field->containing_oneof() ? "Oneof" : ""), - "repeatedtag", (field->is_repeated() ? "Repeated" : "")); + "returndoc", JSReturnDoc(options, field), "class", + GetMessagePath(options, field->containing_type()), "settername", + "set" + JSGetterName(options, field), "oneoftag", + (field->containing_oneof() ? "Oneof" : ""), "repeatedtag", + (field->is_repeated() ? "Repeated" : "")); printer->Annotate("settername", field); printer->Print( @@ -2532,9 +2697,9 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "};\n" "\n" "\n", - "index", JSFieldIndex(field), - "oneofgroup", (field->containing_oneof() ? - (", " + JSOneofArray(options, field)) : ""), + "index", JSFieldIndex(field), "oneofgroup", + (field->containing_oneof() ? (", " + JSOneofArray(options, field)) + : ""), "returnvalue", JSReturnClause(field)); if (field->is_repeated()) { @@ -2551,14 +2716,15 @@ void Generator::GenerateClassField(const GeneratorOptions& options, // at this point we "lie" to non-binary users and tell the return // type is always base64 string, pending a LSC to migrate to typed getters. BytesMode bytes_mode = - field->type() == FieldDescriptor::TYPE_BYTES && !options.binary ? - BYTES_B64 : BYTES_DEFAULT; - string typed_annotation = JSFieldTypeAnnotation( - options, field, - /* is_setter_argument = */ false, - /* force_present = */ false, - /* singular_if_not_packed = */ false, - /* bytes_mode = */ bytes_mode); + field->type() == FieldDescriptor::TYPE_BYTES && !options.binary + ? BYTES_B64 + : BYTES_DEFAULT; + std::string typed_annotation = + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ false, + /* singular_if_not_packed = */ false, + /* bytes_mode = */ bytes_mode); if (untyped) { printer->Print( "/**\n" @@ -2571,24 +2737,20 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "$comment$" " * @return {$type$}\n" " */\n", - "fielddef", FieldDefinition(options, field), - "comment", FieldComments(field, bytes_mode), - "type", typed_annotation); + "fielddef", FieldDefinition(options, field), "comment", + FieldComments(field, bytes_mode), "type", typed_annotation); } - printer->Print( - "$class$.prototype.$gettername$ = function() {\n", - "class", GetMessagePath(options, field->containing_type()), - "gettername", "get" + JSGetterName(options, field)); + printer->Print("$class$.prototype.$gettername$ = function() {\n", "class", + GetMessagePath(options, field->containing_type()), + "gettername", "get" + JSGetterName(options, field)); printer->Annotate("gettername", field); if (untyped) { - printer->Print( - " return "); + printer->Print(" return "); } else { - printer->Print( - " return /** @type {$type$} */ (", - "type", typed_annotation); + printer->Print(" return /** @type {$type$} */ (", "type", + typed_annotation); } bool use_default = !ReturnsNullWhenUnset(options, field); @@ -2634,11 +2796,10 @@ void Generator::GenerateClassField(const GeneratorOptions& options, } else { printer->Print( "/** @param {$optionaltype$} value$returndoc$ */\n", "optionaltype", - JSFieldTypeAnnotation( - options, field, - /* is_setter_argument = */ true, - /* force_present = */ false, - /* singular_if_not_packed = */ false), + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ true, + /* force_present = */ false, + /* singular_if_not_packed = */ false), "returndoc", JSReturnDoc(options, field)); } @@ -2691,7 +2852,6 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "returndoc", JSReturnDoc(options, field)); } - if (field->is_repeated()) { GenerateRepeatedPrimitiveHelperMethods(options, printer, field, untyped); } @@ -2700,46 +2860,69 @@ void Generator::GenerateClassField(const GeneratorOptions& options, // Generate clearFoo() method for map fields, repeated fields, and other // fields with presence. if (field->is_map()) { + // clang-format off printer->Print( + "/**\n" + " * Clears values from the map. The map will be non-null." + "$returndoc$\n" + " */\n" "$class$.prototype.$clearername$ = function() {\n" " this.$gettername$().clear();$returnvalue$\n" "};\n" "\n" "\n", + "returndoc", JSReturnDoc(options, field), "class", GetMessagePath(options, field->containing_type()), "clearername", "clear" + JSGetterName(options, field), "gettername", "get" + JSGetterName(options, field), "returnvalue", JSReturnClause(field)); + // clang-format on printer->Annotate("clearername", field); } else if (field->is_repeated() || (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && !field->is_required())) { // Fields where we can delegate to the regular setter. + // clang-format off printer->Print( + "/**\n" + " * $jsdoc$$returndoc$\n" + " */\n" "$class$.prototype.$clearername$ = function() {\n" " this.$settername$($clearedvalue$);$returnvalue$\n" "};\n" "\n" "\n", + "jsdoc", field->is_repeated() + ? "Clears the list making it empty but non-null." + : "Clears the message field making it undefined.", + "returndoc", JSReturnDoc(options, field), "class", GetMessagePath(options, field->containing_type()), "clearername", "clear" + JSGetterName(options, field), "settername", "set" + JSGetterName(options, field), "clearedvalue", (field->is_repeated() ? "[]" : "undefined"), "returnvalue", JSReturnClause(field)); + // clang-format on printer->Annotate("clearername", field); } else if (HasFieldPresence(options, field)) { // Fields where we can't delegate to the regular setter because it doesn't // accept "undefined" as an argument. + // clang-format off printer->Print( + "/**\n" + " * Clears the field making it undefined.$returndoc$\n" + " */\n" "$class$.prototype.$clearername$ = function() {\n" " jspb.Message.set$maybeoneof$Field(this, " "$index$$maybeoneofgroup$, ", + "returndoc", JSReturnDoc(options, field), "class", GetMessagePath(options, field->containing_type()), "clearername", "clear" + JSGetterName(options, field), "maybeoneof", (field->containing_oneof() ? "Oneof" : ""), - "maybeoneofgroup", (field->containing_oneof() ? - (", " + JSOneofArray(options, field)) : ""), + "maybeoneofgroup", (field->containing_oneof() + ? (", " + JSOneofArray(options, field)) + : ""), "index", JSFieldIndex(field)); + // clang-format on printer->Annotate("clearername", field); printer->Print( "$clearedvalue$);$returnvalue$\n" @@ -2761,9 +2944,8 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "};\n" "\n" "\n", - "class", GetMessagePath(options, field->containing_type()), - "hasername", "has" + JSGetterName(options, field), - "index", JSFieldIndex(field)); + "class", GetMessagePath(options, field->containing_type()), "hasername", + "has" + JSGetterName(options, field), "index", JSFieldIndex(field)); printer->Annotate("hasername", field); } } @@ -2816,14 +2998,15 @@ void Generator::GenerateRepeatedMessageHelperMethods( " * @param {number=} opt_index\n" " * @return {!$optionaltype$}\n" " */\n" - "$class$.prototype.add$name$ = function(opt_value, opt_index) {\n" + "$class$.prototype.$addername$ = function(opt_value, opt_index) {\n" " return jspb.Message.addTo$repeatedtag$WrapperField(", - "optionaltype", JSTypeName(options, field, BYTES_DEFAULT), - "class", GetMessagePath(options, field->containing_type()), - "name", JSGetterName(options, field, BYTES_DEFAULT, - /* drop_list = */ true), + "optionaltype", JSTypeName(options, field, BYTES_DEFAULT), "class", + GetMessagePath(options, field->containing_type()), "addername", + "add" + JSGetterName(options, field, BYTES_DEFAULT, + /* drop_list = */ true), "repeatedtag", (field->is_repeated() ? "Repeated" : "")); + printer->Annotate("addername", field); printer->Print( "this, $index$$oneofgroup$, opt_value, $ctor$, opt_index);\n" "};\n" @@ -2910,38 +3093,40 @@ void Generator::GenerateClassDeserializeBinary(const GeneratorOptions& options, " * @return {!$class$}\n" " */\n" "$class$.deserializeBinaryFromReader = function(msg, reader) {\n" - " while (reader.nextField()) {\n" - " if (reader.isEndGroup()) {\n" - " break;\n" - " }\n" - " var field = reader.getFieldNumber();\n" - " switch (field) {\n", + " while (reader.nextField()) {\n", "class", GetMessagePath(options, desc)); + printer->Print( + " if (reader.isEndGroup()) {\n" + " break;\n" + " }\n" + " var field = reader.getFieldNumber();\n" + " switch (field) {\n"); - for (int i = 0; i < desc->field_count(); i++) { - if (!IgnoreField(desc->field(i))) { - GenerateClassDeserializeBinaryField(options, printer, desc->field(i)); + for (int i = 0; i < desc->field_count(); i++) { + if (!IgnoreField(desc->field(i))) { + GenerateClassDeserializeBinaryField(options, printer, desc->field(i)); + } } - } - printer->Print( - " default:\n"); - if (IsExtendable(desc)) { - printer->Print( - " jspb.Message.readBinaryExtension(msg, reader, $extobj$Binary,\n" - " $class$.prototype.getExtension,\n" - " $class$.prototype.setExtension);\n" - " break;\n", - "extobj", JSExtensionsObjectName(options, desc->file(), desc), - "class", GetMessagePath(options, desc)); - } else { - printer->Print( - " reader.skipField();\n" - " break;\n"); - } + printer->Print(" default:\n"); + if (IsExtendable(desc)) { + printer->Print( + " jspb.Message.readBinaryExtension(msg, reader,\n" + " $extobj$Binary,\n" + " $class$.prototype.getExtension,\n" + " $class$.prototype.setExtension);\n" + " break;\n" + " }\n", + "extobj", JSExtensionsObjectName(options, desc->file(), desc), + "class", GetMessagePath(options, desc)); + } else { + printer->Print( + " reader.skipField();\n" + " break;\n" + " }\n"); + } printer->Print( - " }\n" " }\n" " return msg;\n" "};\n" @@ -2950,11 +3135,9 @@ void Generator::GenerateClassDeserializeBinary(const GeneratorOptions& options, } void Generator::GenerateClassDeserializeBinaryField( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { - printer->Print(" case $num$:\n", "num", - SimpleItoa(field->number())); + printer->Print(" case $num$:\n", "num", StrCat(field->number())); if (field->is_map()) { const FieldDescriptor* key_field = MapFieldKey(field); @@ -2964,20 +3147,20 @@ void Generator::GenerateClassDeserializeBinaryField( " reader.readMessage(value, function(message, reader) {\n", "name", JSGetterName(options, field)); - printer->Print(" jspb.Map.deserializeBinary(message, reader, " - "$keyReaderFn$, $valueReaderFn$", - "keyReaderFn", JSBinaryReaderMethodName(options, key_field), - "valueReaderFn", JSBinaryReaderMethodName(options, value_field)); + printer->Print( + " jspb.Map.deserializeBinary(message, reader, " + "$keyReaderFn$, $valueReaderFn$", + "keyReaderFn", JSBinaryReaderMethodName(options, key_field), + "valueReaderFn", JSBinaryReaderMethodName(options, value_field)); if (value_field->type() == FieldDescriptor::TYPE_MESSAGE) { printer->Print(", $messageType$.deserializeBinaryFromReader", - "messageType", GetMessagePath(options, value_field->message_type())); + "messageType", + GetMessagePath(options, value_field->message_type())); } else { printer->Print(", null"); } - printer->Print(", $defaultKey$", - "defaultKey", JSFieldDefault(key_field) - ); + printer->Print(", $defaultKey$", "defaultKey", JSFieldDefault(key_field)); printer->Print(");\n"); printer->Print(" });\n"); } else { @@ -2990,15 +3173,15 @@ void Generator::GenerateClassDeserializeBinaryField( (field->type() == FieldDescriptor::TYPE_GROUP) ? "Group" : "Message", "grpfield", (field->type() == FieldDescriptor::TYPE_GROUP) - ? (SimpleItoa(field->number()) + ", ") + ? (StrCat(field->number()) + ", ") : ""); } else { printer->Print( " var value = /** @type {$fieldtype$} */ " "(reader.read$reader$());\n", - "fieldtype", JSFieldTypeAnnotation(options, field, false, true, - /* singular_if_not_packed */ true, - BYTES_U8), + "fieldtype", + JSFieldTypeAnnotation(options, field, false, true, + /* singular_if_not_packed */ true, BYTES_U8), "reader", JSBinaryReadWriteMethodName(field, /* is_writer = */ false)); } @@ -3011,9 +3194,8 @@ void Generator::GenerateClassDeserializeBinaryField( // Singular fields, and packed repeated fields, receive a |value| either // as the field's value or as the array of all the field's values; set // this as the field's value directly. - printer->Print( - " msg.set$name$(value);\n", - "name", JSGetterName(options, field)); + printer->Print(" msg.set$name$(value);\n", "name", + JSGetterName(options, field)); } } @@ -3057,8 +3239,8 @@ void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options, printer->Print( " jspb.Message.serializeBinaryExtensions(message, writer,\n" " $extobj$Binary, $class$.prototype.getExtension);\n", - "extobj", JSExtensionsObjectName(options, desc->file(), desc), - "class", GetMessagePath(options, desc)); + "extobj", JSExtensionsObjectName(options, desc->file(), desc), "class", + GetMessagePath(options, desc)); } printer->Print( @@ -3068,26 +3250,24 @@ void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options, } void Generator::GenerateClassSerializeBinaryField( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { if (HasFieldPresence(options, field) && field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { - string typed_annotation = JSFieldTypeAnnotation( - options, field, - /* is_setter_argument = */ false, - /* force_present = */ false, - /* singular_if_not_packed = */ false, - /* bytes_mode = */ BYTES_DEFAULT); + std::string typed_annotation = + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ false, + /* singular_if_not_packed = */ false, + /* bytes_mode = */ BYTES_DEFAULT); printer->Print( " f = /** @type {$type$} */ " "(jspb.Message.getField(message, $index$));\n", - "index", JSFieldIndex(field), - "type", typed_annotation); + "index", JSFieldIndex(field), "type", typed_annotation); } else { printer->Print( - " f = message.get$name$($nolazy$);\n", - "name", JSGetterName(options, field, BYTES_U8), + " f = message.get$name$($nolazy$);\n", "name", + JSGetterName(options, field, BYTES_U8), // No lazy creation for maps containers -- fastpath the empty case. "nolazy", field->is_map() ? "true" : ""); } @@ -3095,15 +3275,12 @@ void Generator::GenerateClassSerializeBinaryField( // Print an `if (condition)` statement that evaluates to true if the field // goes on the wire. if (field->is_map()) { - printer->Print( - " if (f && f.getLength() > 0) {\n"); + printer->Print(" if (f && f.getLength() > 0) {\n"); } else if (field->is_repeated()) { - printer->Print( - " if (f.length > 0) {\n"); + printer->Print(" if (f.length > 0) {\n"); } else { if (HasFieldPresence(options, field)) { - printer->Print( - " if (f != null) {\n"); + printer->Print(" if (f != null) {\n"); } else { // No field presence: serialize onto the wire only if value is // non-default. Defaults are documented here: @@ -3128,16 +3305,13 @@ void Generator::GenerateClassSerializeBinaryField( case FieldDescriptor::CPPTYPE_ENUM: case FieldDescriptor::CPPTYPE_FLOAT: case FieldDescriptor::CPPTYPE_DOUBLE: - printer->Print( - " if (f !== 0.0) {\n"); + printer->Print(" if (f !== 0.0) {\n"); break; case FieldDescriptor::CPPTYPE_BOOL: - printer->Print( - " if (f) {\n"); + printer->Print(" if (f) {\n"); break; case FieldDescriptor::CPPTYPE_STRING: - printer->Print( - " if (f.length > 0) {\n"); + printer->Print(" if (f.length > 0) {\n"); break; default: assert(false); @@ -3153,13 +3327,13 @@ void Generator::GenerateClassSerializeBinaryField( printer->Print( " f.serializeBinary($index$, writer, " "$keyWriterFn$, $valueWriterFn$", - "index", SimpleItoa(field->number()), "keyWriterFn", + "index", StrCat(field->number()), "keyWriterFn", JSBinaryWriterMethodName(options, key_field), "valueWriterFn", JSBinaryWriterMethodName(options, value_field)); if (value_field->type() == FieldDescriptor::TYPE_MESSAGE) { - printer->Print(", $messageType$.serializeBinaryToWriter", - "messageType", GetMessagePath(options, value_field->message_type())); + printer->Print(", $messageType$.serializeBinaryToWriter", "messageType", + GetMessagePath(options, value_field->message_type())); } printer->Print(");\n"); @@ -3169,25 +3343,23 @@ void Generator::GenerateClassSerializeBinaryField( " $index$,\n" " f", "method", JSBinaryReadWriteMethodName(field, /* is_writer = */ true), - "index", SimpleItoa(field->number())); + "index", StrCat(field->number())); if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && !field->is_map()) { printer->Print( ",\n" " $submsg$.serializeBinaryToWriter\n", - "submsg", SubmessageTypeRef(options, field)); + "submsg", SubmessageTypeRef(options, field)); } else { printer->Print("\n"); } - printer->Print( - " );\n"); + printer->Print(" );\n"); } // Close the `if`. - printer->Print( - " }\n"); + printer->Print(" }\n"); } void Generator::GenerateEnum(const GeneratorOptions& options, @@ -3198,15 +3370,15 @@ void Generator::GenerateEnum(const GeneratorOptions& options, " * @enum {number}\n" " */\n" "$enumprefix$$name$ = {\n", - "enumprefix", GetEnumPathPrefix(options, enumdesc), - "name", enumdesc->name()); + "enumprefix", GetEnumPathPrefix(options, enumdesc), "name", + enumdesc->name()); printer->Annotate("name", enumdesc); for (int i = 0; i < enumdesc->value_count(); i++) { const EnumValueDescriptor* value = enumdesc->value(i); printer->Print(" $name$: $value$$comma$\n", "name", ToEnumCase(value->name()), "value", - SimpleItoa(value->number()), "comma", + StrCat(value->number()), "comma", (i == enumdesc->value_count() - 1) ? "" : ","); printer->Annotate("name", value); } @@ -3219,12 +3391,12 @@ void Generator::GenerateEnum(const GeneratorOptions& options, void Generator::GenerateExtension(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { - string extension_scope = + std::string extension_scope = (field->extension_scope() ? GetMessagePath(options, field->extension_scope()) - : GetFilePath(options, field->file())); + : GetNamespace(options, field->file())); - const string extension_object_name = JSObjectFieldName(options, field); + const std::string extension_object_name = JSObjectFieldName(options, field); printer->Print( "\n" "/**\n" @@ -3233,14 +3405,12 @@ void Generator::GenerateExtension(const GeneratorOptions& options, " * @type {!jspb.ExtensionFieldInfo<$extensionType$>}\n" " */\n" "$class$.$name$ = new jspb.ExtensionFieldInfo(\n", - "nameInComment", extension_object_name, - "name", extension_object_name, - "class", extension_scope, - "extensionType", JSFieldTypeAnnotation( - options, field, - /* is_setter_argument = */ false, - /* force_present = */ true, - /* singular_if_not_packed = */ false)); + "nameInComment", extension_object_name, "name", extension_object_name, + "class", extension_scope, "extensionType", + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ true, + /* singular_if_not_packed = */ false)); printer->Annotate("name", field); printer->Print( " $index$,\n" @@ -3250,15 +3420,15 @@ void Generator::GenerateExtension(const GeneratorOptions& options, "!Object} */ (\n" " $toObject$),\n" " $repeated$);\n", - "index", SimpleItoa(field->number()), "name", - extension_object_name, "ctor", + "index", StrCat(field->number()), "name", extension_object_name, + "ctor", (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ? SubmessageTypeRef(options, field) - : string("null")), + : std::string("null")), "toObject", (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ? (SubmessageTypeRef(options, field) + ".toObject") - : string("null")), + : std::string("null")), "repeated", (field->is_repeated() ? "1" : "0")); printer->Print( @@ -3271,8 +3441,8 @@ void Generator::GenerateExtension(const GeneratorOptions& options, " $binaryMessageDeserializeFn$,\n", "extendName", JSExtensionsObjectName(options, field->file(), field->containing_type()), - "index", SimpleItoa(field->number()), "class", extension_scope, - "name", extension_object_name, "binaryReaderFn", + "index", StrCat(field->number()), "class", extension_scope, "name", + extension_object_name, "binaryReaderFn", JSBinaryReaderMethodName(options, field), "binaryWriterFn", JSBinaryWriterMethodName(options, field), "binaryMessageSerializeFn", (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) @@ -3293,13 +3463,13 @@ void Generator::GenerateExtension(const GeneratorOptions& options, "\n", "extendName", JSExtensionsObjectName(options, field->file(), field->containing_type()), - "index", SimpleItoa(field->number()), "class", extension_scope, - "name", extension_object_name); + "index", StrCat(field->number()), "class", extension_scope, "name", + extension_object_name); } bool GeneratorOptions::ParseFromOptions( - const std::vector< std::pair< string, string > >& options, - string* error) { + const std::vector >& options, + std::string* error) { for (int i = 0; i < options.size(); i++) { if (options[i].first == "add_require_for_enums") { if (options[i].second != "") { @@ -3397,13 +3567,12 @@ GeneratorOptions::OutputMode GeneratorOptions::output_mode() const { return kEverythingInOneFile; } - // Otherwise, we create one output file per type. - return kOneOutputFilePerType; + // Otherwise, we create one output file per SCC. + return kOneOutputFilePerSCC; } void Generator::GenerateFilesInDepOrder( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const std::vector& files) const { // Build a std::set over all files so that the DFS can detect when it recurses // into a dep not specified in the user's command line. @@ -3416,10 +3585,8 @@ void Generator::GenerateFilesInDepOrder( } void Generator::GenerateFileAndDeps( - const GeneratorOptions& options, - io::Printer* printer, - const FileDescriptor* root, - std::set* all_files, + const GeneratorOptions& options, io::Printer* printer, + const FileDescriptor* root, std::set* all_files, std::set* generated) const { // Skip if already generated. if (generated->find(root) != generated->end()) { @@ -3441,10 +3608,40 @@ void Generator::GenerateFileAndDeps( } } +bool Generator::GenerateFile(const FileDescriptor* file, + const GeneratorOptions& options, + GeneratorContext* context, + bool use_short_name) const { + std::string filename = + options.output_dir + "/" + + GetJSFilename(options, use_short_name + ? file->name().substr(file->name().rfind('/')) + : file->name()); + std::unique_ptr output(context->Open(filename)); + GOOGLE_CHECK(output); + GeneratedCodeInfo annotations; + io::AnnotationProtoCollector annotation_collector( + &annotations); + io::Printer printer(output.get(), '$', + options.annotate_code ? &annotation_collector : nullptr); + + GenerateFile(options, &printer, file); + + if (printer.failed()) { + return false; + } + + if (options.annotate_code) { + EmbedCodeAnnotations(annotations, &printer); + } + + return true; +} + void Generator::GenerateFile(const GeneratorOptions& options, io::Printer* printer, const FileDescriptor* file) const { - GenerateHeader(options, printer); + GenerateHeader(options, file, printer); // Generate "require" statements. if ((options.import_style == GeneratorOptions::kImportCommonJs || @@ -3460,7 +3657,7 @@ void Generator::GenerateFile(const GeneratorOptions& options, } for (int i = 0; i < file->dependency_count(); i++) { - const string& name = file->dependency(i)->name(); + const std::string& name = file->dependency(i)->name(); printer->Print( "var $alias$ = require('$file$');\n" "goog.object.extend(proto, $alias$);\n", @@ -3469,7 +3666,7 @@ void Generator::GenerateFile(const GeneratorOptions& options, } } - std::set provided; + std::set provided; std::set extensions; for (int i = 0; i < file->extension_count(); i++) { // We honor the jspb::ignore option here only when working with @@ -3479,7 +3676,7 @@ void Generator::GenerateFile(const GeneratorOptions& options, IgnoreField(file->extension(i))) { continue; } - provided.insert(GetFilePath(options, file) + "." + + provided.insert(GetNamespace(options, file) + "." + JSObjectFieldName(options, file->extension(i))); extensions.insert(file->extension(i)); } @@ -3504,16 +3701,16 @@ void Generator::GenerateFile(const GeneratorOptions& options, // if provided is empty, do not export anything if (options.import_style == GeneratorOptions::kImportCommonJs && !provided.empty()) { - printer->Print("goog.object.extend(exports, $package$);\n", - "package", GetFilePath(options, file)); + printer->Print("goog.object.extend(exports, $package$);\n", "package", + GetNamespace(options, file)); } else if (options.import_style == GeneratorOptions::kImportCommonJsStrict) { printer->Print("goog.object.extend(exports, proto);\n", "package", - GetFilePath(options, file)); + GetNamespace(options, file)); } // Emit well-known type methods. for (FileToc* toc = well_known_types_js; toc->name != NULL; toc++) { - string name = string("google/protobuf/") + toc->name; + std::string name = std::string("google/protobuf/") + toc->name; if (name == StripProto(file->name()) + ".js") { printer->Print(toc->data); } @@ -3521,10 +3718,10 @@ void Generator::GenerateFile(const GeneratorOptions& options, } bool Generator::GenerateAll(const std::vector& files, - const string& parameter, + const std::string& parameter, GeneratorContext* context, - string* error) const { - std::vector< std::pair< string, string > > option_pairs; + std::string* error) const { + std::vector > option_pairs; ParseGeneratorParameter(parameter, &option_pairs); GeneratorOptions options; if (!options.ParseFromOptions(option_pairs, error)) { @@ -3534,11 +3731,16 @@ bool Generator::GenerateAll(const std::vector& files, if (options.output_mode() == GeneratorOptions::kEverythingInOneFile) { // All output should go in a single file. - string filename = options.output_dir + "/" + options.library + - options.GetFileNameExtension(); + std::string filename = options.output_dir + "/" + options.library + + options.GetFileNameExtension(); std::unique_ptr output(context->Open(filename)); GOOGLE_CHECK(output.get()); - io::Printer printer(output.get(), '$'); + GeneratedCodeInfo annotations; + io::AnnotationProtoCollector annotation_collector( + &annotations); + io::Printer printer( + output.get(), '$', + options.annotate_code ? &annotation_collector : nullptr); // Pull out all extensions -- we need these to generate all // provides/requires. @@ -3550,9 +3752,13 @@ bool Generator::GenerateAll(const std::vector& files, } } - GenerateHeader(options, &printer); + if (files.size() == 1) { + GenerateHeader(options, files[0], &printer); + } else { + GenerateHeader(options, nullptr, &printer); + } - std::set provided; + std::set provided; FindProvides(options, &printer, files, &provided); FindProvidesForFields(options, &printer, extensions, &provided); GenerateProvides(options, &printer, &provided); @@ -3570,55 +3776,105 @@ bool Generator::GenerateAll(const std::vector& files, if (printer.failed()) { return false; } - } else if (options.output_mode() == GeneratorOptions::kOneOutputFilePerType) { - std::set allowed_set; - if (!GenerateJspbAllowedSet(options, files, &allowed_set, error)) { + if (options.annotate_code) { + EmbedCodeAnnotations(annotations, &printer); + } + } else if (options.output_mode() == GeneratorOptions::kOneOutputFilePerSCC) { + std::set have_printed; + SCCAnalyzer analyzer; + std::map allowed_map; + if (!GenerateJspbAllowedMap(options, files, &allowed_map, &analyzer, + error)) { return false; } + bool generated = false; for (int i = 0; i < files.size(); i++) { const FileDescriptor* file = files[i]; + // Force well known type to generate in a whole file. + if (IsWellKnownTypeFile(file)) { + if (!GenerateFile(file, options, context, true)) { + return false; + } + generated = true; + continue; + } for (int j = 0; j < file->message_type_count(); j++) { const Descriptor* desc = file->message_type(j); - if (allowed_set.count(desc) == 0) { + if (have_printed.count(desc) || + allowed_map.count(analyzer.GetSCC(desc)) == 0) { continue; } - string filename = GetMessageFileName(options, desc); + generated = true; + const SCC* scc = analyzer.GetSCC(desc); + const std::string& filename = allowed_map[scc]; std::unique_ptr output( context->Open(filename)); GOOGLE_CHECK(output.get()); - io::Printer printer(output.get(), '$'); - - GenerateHeader(options, &printer); - - std::set provided; - FindProvidesForMessage(options, &printer, desc, &provided); + GeneratedCodeInfo annotations; + io::AnnotationProtoCollector annotation_collector( + &annotations); + io::Printer printer( + output.get(), '$', + options.annotate_code ? &annotation_collector : nullptr); + + GenerateHeader(options, file, &printer); + + std::set provided; + for (auto one_desc : scc->descriptors) { + if (one_desc->containing_type() == nullptr) { + FindProvidesForMessage(options, &printer, one_desc, &provided); + } + } GenerateProvides(options, &printer, &provided); GenerateTestOnly(options, &printer); - GenerateRequiresForMessage(options, &printer, desc, &provided); + GenerateRequiresForSCC(options, &printer, scc, &provided); - GenerateClass(options, &printer, desc); + for (auto one_desc : scc->descriptors) { + if (one_desc->containing_type() == nullptr) { + GenerateClassConstructorAndDeclareExtensionFieldInfo( + options, &printer, one_desc); + } + } + for (auto one_desc : scc->descriptors) { + if (one_desc->containing_type() == nullptr) { + GenerateClass(options, &printer, one_desc); + } + } + + for (auto one_desc : scc->descriptors) { + have_printed.insert(one_desc); + } if (printer.failed()) { return false; } + if (options.annotate_code) { + EmbedCodeAnnotations(annotations, &printer); + } } for (int j = 0; j < file->enum_type_count(); j++) { const EnumDescriptor* enumdesc = file->enum_type(j); - if (allowed_set.count(enumdesc) == 0) { + if (allowed_map.count(enumdesc) == 0) { continue; } - string filename = GetEnumFileName(options, enumdesc); + generated = true; + const std::string& filename = allowed_map[enumdesc]; std::unique_ptr output( context->Open(filename)); GOOGLE_CHECK(output.get()); - io::Printer printer(output.get(), '$'); + GeneratedCodeInfo annotations; + io::AnnotationProtoCollector annotation_collector( + &annotations); + io::Printer printer( + output.get(), '$', + options.annotate_code ? &annotation_collector : nullptr); - GenerateHeader(options, &printer); + GenerateHeader(options, file, &printer); - std::set provided; + std::set provided; FindProvidesForEnum(options, &printer, enumdesc, &provided); GenerateProvides(options, &printer, &provided); GenerateTestOnly(options, &printer); @@ -3628,20 +3884,29 @@ bool Generator::GenerateAll(const std::vector& files, if (printer.failed()) { return false; } + if (options.annotate_code) { + EmbedCodeAnnotations(annotations, &printer); + } } // File-level extensions (message-level extensions are generated under // the enclosing message). - if (allowed_set.count(file) == 1) { - string filename = GetExtensionFileName(options, file); + if (allowed_map.count(file) == 1) { + generated = true; + const std::string& filename = allowed_map[file]; std::unique_ptr output( context->Open(filename)); GOOGLE_CHECK(output.get()); - io::Printer printer(output.get(), '$'); + GeneratedCodeInfo annotations; + io::AnnotationProtoCollector annotation_collector( + &annotations); + io::Printer printer( + output.get(), '$', + options.annotate_code ? &annotation_collector : nullptr); - GenerateHeader(options, &printer); + GenerateHeader(options, file, &printer); - std::set provided; + std::set provided; std::vector fields; for (int j = 0; j < files[i]->extension_count(); j++) { @@ -3660,34 +3925,25 @@ bool Generator::GenerateAll(const std::vector& files, GenerateExtension(options, &printer, files[i]->extension(j)); } } + if (options.annotate_code) { + EmbedCodeAnnotations(annotations, &printer); + } } } + if (!generated) { + std::string filename = options.output_dir + "/" + + "empty_no_content_void_file" + + options.GetFileNameExtension(); + std::unique_ptr output(context->Open(filename)); + } } else /* options.output_mode() == kOneOutputFilePerInputFile */ { // Generate one output file per input (.proto) file. for (int i = 0; i < files.size(); i++) { const FileDescriptor* file = files[i]; - - string filename = - options.output_dir + "/" + GetJSFilename(options, file->name()); - std::unique_ptr output(context->Open(filename)); - GOOGLE_CHECK(output.get()); - GeneratedCodeInfo annotations; - io::AnnotationProtoCollector annotation_collector( - &annotations); - io::Printer printer(output.get(), '$', - options.annotate_code ? &annotation_collector : NULL); - - - GenerateFile(options, &printer, file); - - if (printer.failed()) { + if (!GenerateFile(file, options, context, false)) { return false; } - - if (options.annotate_code) { - EmbedCodeAnnotations(annotations, &printer); - } } } return true; diff --git a/src/google/protobuf/compiler/js/js_generator.h b/src/google/protobuf/compiler/js/js_generator.h index 21e03bc1e4..49f19f6eb6 100644 --- a/src/google/protobuf/compiler/js/js_generator.h +++ b/src/google/protobuf/compiler/js/js_generator.h @@ -33,13 +33,16 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ #define GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ -#include #include +#include #include #include +#include #include +#include + namespace google { namespace protobuf { @@ -49,16 +52,18 @@ class FieldDescriptor; class OneofDescriptor; class FileDescriptor; -namespace io { class Printer; } +namespace io { +class Printer; +} namespace compiler { namespace js { struct GeneratorOptions { // Output path. - string output_dir; + std::string output_dir; // Namespace prefix. - string namespace_prefix; + std::string namespace_prefix; // Enable binary-format support? bool binary; // What style of imports should be used. @@ -84,11 +89,11 @@ struct GeneratorOptions { annotate_code(false) {} bool ParseFromOptions( - const std::vector< std::pair< string, string > >& options, - string* error); + const std::vector >& options, + std::string* error); // Returns the file name extension to use for generated code. - string GetFileNameExtension() const { + std::string GetFileNameExtension() const { return import_style == kImportClosure ? extension : "_pb.js"; } @@ -96,7 +101,7 @@ struct GeneratorOptions { // Create an output file for each input .proto file. kOneOutputFilePerInputFile, // Create an output file for each type. - kOneOutputFilePerType, + kOneOutputFilePerSCC, // Put everything in a single file named by the library option. kEverythingInOneFile, }; @@ -113,15 +118,18 @@ struct GeneratorOptions { bool testonly; // Create a library with name _lib.js rather than a separate .js file // per type? - string library; + std::string library; // Error if there are two types that would generate the same output file? bool error_on_name_conflict; // The extension to use for output file names. - string extension; + std::string extension; // Create a separate output file for each input file? bool one_output_file_per_input_file; - // If true, we should build .meta files that contain annotations for - // generated code. See GeneratedCodeInfo in descriptor.proto. + // If true, we should append annotations as commen on the last line for + // generated .js file. Annotations used by tools like https://kythe.io + // to provide cross-references between .js and .proto files. Annotations + // are enced as base64 proto of GeneratedCodeInfo message (see + // descriptor.proto). bool annotate_code; }; @@ -129,15 +137,14 @@ struct GeneratorOptions { // header. If you create your own protocol compiler binary and you want it to // support JavaScript output, you can do so by registering an instance of this // CodeGenerator with the CommandLineInterface in your main() function. -class LIBPROTOC_EXPORT Generator : public CodeGenerator { +class PROTOC_EXPORT Generator : public CodeGenerator { public: Generator() {} virtual ~Generator() {} virtual bool Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const { + const std::string& parameter, GeneratorContext* context, + std::string* error) const { *error = "Unimplemented Generate() method. Call GenerateAll() instead."; return false; } @@ -145,40 +152,34 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { virtual bool HasGenerateAll() const { return true; } virtual bool GenerateAll(const std::vector& files, - const string& parameter, - GeneratorContext* context, - string* error) const; + const std::string& parameter, + GeneratorContext* context, std::string* error) const; private: void GenerateHeader(const GeneratorOptions& options, - io::Printer* printer) const; + const FileDescriptor* file, io::Printer* printer) const; // Generate goog.provides() calls. - void FindProvides(const GeneratorOptions& options, - io::Printer* printer, + void FindProvides(const GeneratorOptions& options, io::Printer* printer, const std::vector& file, - std::set* provided) const; + std::set* provided) const; void FindProvidesForFile(const GeneratorOptions& options, - io::Printer* printer, - const FileDescriptor* file, - std::set* provided) const; + io::Printer* printer, const FileDescriptor* file, + std::set* provided) const; void FindProvidesForMessage(const GeneratorOptions& options, - io::Printer* printer, - const Descriptor* desc, - std::set* provided) const; + io::Printer* printer, const Descriptor* desc, + std::set* provided) const; void FindProvidesForEnum(const GeneratorOptions& options, - io::Printer* printer, - const EnumDescriptor* enumdesc, - std::set* provided) const; + io::Printer* printer, const EnumDescriptor* enumdesc, + std::set* provided) const; // For extension fields at file scope. void FindProvidesForFields(const GeneratorOptions& options, io::Printer* printer, const std::vector& fields, - std::set* provided) const; + std::set* provided) const; // Print the goog.provides() found by the methods above. - void GenerateProvides(const GeneratorOptions& options, - io::Printer* printer, - std::set* provided) const; + void GenerateProvides(const GeneratorOptions& options, io::Printer* printer, + std::set* provided) const; // Generate goog.setTestOnly() if indicated. void GenerateTestOnly(const GeneratorOptions& options, @@ -188,37 +189,40 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { void GenerateRequiresForLibrary( const GeneratorOptions& options, io::Printer* printer, const std::vector& files, - std::set* provided) const; - void GenerateRequiresForMessage(const GeneratorOptions& options, - io::Printer* printer, - const Descriptor* desc, - std::set* provided) const; + std::set* provided) const; + void GenerateRequiresForSCC(const GeneratorOptions& options, + io::Printer* printer, const SCC* scc, + std::set* provided) const; // For extension fields at file scope. void GenerateRequiresForExtensions( const GeneratorOptions& options, io::Printer* printer, const std::vector& fields, - std::set* provided) const; + std::set* provided) const; void GenerateRequiresImpl(const GeneratorOptions& options, - io::Printer* printer, std::set* required, - std::set* forwards, - std::set* provided, bool require_jspb, + io::Printer* printer, + std::set* required, + std::set* forwards, + std::set* provided, bool require_jspb, bool require_extension, bool require_map) const; void FindRequiresForMessage(const GeneratorOptions& options, const Descriptor* desc, - std::set* required, - std::set* forwards, + std::set* required, + std::set* forwards, bool* have_message) const; void FindRequiresForField(const GeneratorOptions& options, const FieldDescriptor* field, - std::set* required, - std::set* forwards) const; + std::set* required, + std::set* forwards) const; void FindRequiresForExtension(const GeneratorOptions& options, const FieldDescriptor* field, - std::set* required, - std::set* forwards) const; - - void GenerateFile(const GeneratorOptions& options, - io::Printer* printer, + std::set* required, + std::set* forwards) const; + // Generate all things in a proto file into one file. + // If use_short_name is true, the generated file's name will only be short + // name that without directory, otherwise filename equals file->name() + bool GenerateFile(const FileDescriptor* file, const GeneratorOptions& options, + GeneratorContext* context, bool use_short_name) const; + void GenerateFile(const GeneratorOptions& options, io::Printer* printer, const FileDescriptor* file) const; // Generate definitions for all message classes and enums in all files, @@ -228,8 +232,7 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { const std::vector& file) const; // Helper for above. void GenerateFileAndDeps(const GeneratorOptions& options, - io::Printer* printer, - const FileDescriptor* root, + io::Printer* printer, const FileDescriptor* root, std::set* all_files, std::set* generated) const; @@ -244,8 +247,7 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { bool use_default) const; // Generate definition for one class. - void GenerateClass(const GeneratorOptions& options, - io::Printer* printer, + void GenerateClass(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; void GenerateClassConstructor(const GeneratorOptions& options, io::Printer* printer, @@ -253,12 +255,17 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { void GenerateClassFieldInfo(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; - void GenerateClassXid(const GeneratorOptions& options, - io::Printer* printer, + void GenerateClassConstructorAndDeclareExtensionFieldInfo( + const GeneratorOptions& options, io::Printer* printer, + const Descriptor* desc) const; + void GenerateClassXid(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; void GenerateOneofCaseDefinition(const GeneratorOptions& options, io::Printer* printer, const OneofDescriptor* oneof) const; + void GenerateObjectTypedef(const GeneratorOptions& options, + io::Printer* printer, + const Descriptor* desc) const; void GenerateClassToObject(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; @@ -271,17 +278,12 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { void GenerateClassFieldFromObject(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const; - void GenerateClassClone(const GeneratorOptions& options, - io::Printer* printer, - const Descriptor* desc) const; void GenerateClassRegistration(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; void GenerateClassFields(const GeneratorOptions& options, - io::Printer* printer, - const Descriptor* desc) const; - void GenerateClassField(const GeneratorOptions& options, - io::Printer* printer, + io::Printer* printer, const Descriptor* desc) const; + void GenerateClassField(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* desc) const; void GenerateClassExtensionFieldInfo(const GeneratorOptions& options, io::Printer* printer, @@ -303,13 +305,11 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { const FieldDescriptor* field) const; // Generate definition for one enum. - void GenerateEnum(const GeneratorOptions& options, - io::Printer* printer, + void GenerateEnum(const GeneratorOptions& options, io::Printer* printer, const EnumDescriptor* enumdesc) const; // Generate an extension definition. - void GenerateExtension(const GeneratorOptions& options, - io::Printer* printer, + void GenerateExtension(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const; // Generate addFoo() method for repeated primitive fields. @@ -331,4 +331,6 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/js/well_known_types_embed.cc b/src/google/protobuf/compiler/js/well_known_types_embed.cc index c00c60cbde..5cb73657e7 100644 --- a/src/google/protobuf/compiler/js/well_known_types_embed.cc +++ b/src/google/protobuf/compiler/js/well_known_types_embed.cc @@ -46,6 +46,7 @@ struct FileToc well_known_types_js[] = { "\n" "/**\n" " * Packs the given message instance into this Any.\n" + " * For binary format usage only.\n" " * @param {!Uint8Array} serialized The serialized data to pack.\n" " * @param {string} name The type name of this message object.\n" " * @param {string=} opt_typeUrlPrefix the type URL prefix.\n" @@ -85,7 +86,8 @@ struct FileToc well_known_types_js[] = { " } else {\n" " return null;\n" " }\n" - "};\n"}, + "};\n" + }, {"timestamp.js", "/* This code will be inserted into generated code for\n" " * google/protobuf/timestamp.proto. */\n" @@ -109,6 +111,19 @@ struct FileToc well_known_types_js[] = { "proto.google.protobuf.Timestamp.prototype.fromDate = function(value) {\n" " this.setSeconds(Math.floor(value.getTime() / 1000));\n" " this.setNanos(value.getMilliseconds() * 1000000);\n" + "};\n" + "\n" + "\n" + "/**\n" + " * Factory method that returns a Timestamp object with value equal to\n" + " * the given Date.\n" + " * @param {!Date} value The value to set.\n" + " * @return {!proto.google.protobuf.Timestamp}\n" + " */\n" + "proto.google.protobuf.Timestamp.fromDate = function(value) {\n" + " var timestamp = new proto.google.protobuf.Timestamp();\n" + " timestamp.fromDate(value);\n" + " return timestamp;\n" "};\n"}, {"struct.js", "/* This code will be inserted into generated code for\n" diff --git a/src/google/protobuf/compiler/main.cc b/src/google/protobuf/compiler/main.cc index 8be2652af7..e1c539ae17 100644 --- a/src/google/protobuf/compiler/main.cc +++ b/src/google/protobuf/compiler/main.cc @@ -41,6 +41,8 @@ #include #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -55,10 +57,9 @@ int ProtobufMain(int argc, char* argv[]) { cli.RegisterGenerator("--cpp_out", "--cpp_opt", &cpp_generator, "Generate C++ header and source."); -#ifdef GOOGLE_PROTOBUF_USE_OPENSOURCE_GOOGLE3_RUNTIME - cpp_generator.set_runtime(cpp::CppGenerator::Runtime::kOpensourceGoogle3); -#elif defined(GOOGLE_PROTOBUF_USE_OPENSOURCE_RUNTIME) - cpp_generator.set_runtime(cpp::CppGenerator::Runtime::kOpensource); +#ifdef GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE + cpp_generator.set_opensource_runtime(true); + cpp_generator.set_runtime_include_base(GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE); #endif // Proto2 Java @@ -105,5 +106,5 @@ int ProtobufMain(int argc, char* argv[]) { } // namespace google int main(int argc, char* argv[]) { - return google::protobuf::compiler::ProtobufMain(argc, argv); + return PROTOBUF_NAMESPACE_ID::compiler::ProtobufMain(argc, argv); } diff --git a/src/google/protobuf/compiler/mock_code_generator.cc b/src/google/protobuf/compiler/mock_code_generator.cc index 4bb1d87227..07760649f2 100644 --- a/src/google/protobuf/compiler/mock_code_generator.cc +++ b/src/google/protobuf/compiler/mock_code_generator.cc @@ -67,8 +67,9 @@ namespace compiler { // Returns the list of the names of files in all_files in the form of a // comma-separated string. -string CommaSeparatedList(const std::vector& all_files) { - std::vector names; +std::string CommaSeparatedList( + const std::vector& all_files) { + std::vector names; for (size_t i = 0; i < all_files.size(); i++) { names.push_back(all_files[i]->name()); } @@ -82,25 +83,23 @@ static const char* kFirstInsertionPoint = static const char* kSecondInsertionPoint = " # @@protoc_insertion_point(second_mock_insertion_point) is here\n"; -MockCodeGenerator::MockCodeGenerator(const string& name) - : name_(name) {} +MockCodeGenerator::MockCodeGenerator(const std::string& name) : name_(name) {} MockCodeGenerator::~MockCodeGenerator() {} void MockCodeGenerator::ExpectGenerated( - const string& name, - const string& parameter, - const string& insertions, - const string& file, - const string& first_message_name, - const string& first_parsed_file_name, - const string& output_directory) { - string content; + const std::string& name, const std::string& parameter, + const std::string& insertions, const std::string& file, + const std::string& first_message_name, + const std::string& first_parsed_file_name, + const std::string& output_directory) { + std::string content; GOOGLE_CHECK_OK( File::GetContents(output_directory + "/" + GetOutputFileName(name, file), &content, true)); - std::vector lines = Split(content, "\n", true); + std::vector lines = + Split(content, "\n", true); while (!lines.empty() && lines.back().empty()) { lines.pop_back(); @@ -109,22 +108,22 @@ void MockCodeGenerator::ExpectGenerated( lines[i] += "\n"; } - std::vector insertion_list; + std::vector insertion_list; if (!insertions.empty()) { SplitStringUsing(insertions, ",", &insertion_list); } EXPECT_EQ(lines.size(), 3 + insertion_list.size() * 2); - EXPECT_EQ(GetOutputFileContent(name, parameter, file, - first_parsed_file_name, first_message_name), + EXPECT_EQ(GetOutputFileContent(name, parameter, file, first_parsed_file_name, + first_message_name), lines[0]); EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]); EXPECT_EQ(kSecondInsertionPoint, lines[2 + insertion_list.size() * 2]); for (size_t i = 0; i < insertion_list.size(); i++) { - EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert", - file, file, first_message_name), + EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert", file, + file, first_message_name), lines[1 + i]); // Second insertion point is indented, so the inserted text should // automatically be indented too. @@ -135,9 +134,9 @@ void MockCodeGenerator::ExpectGenerated( } namespace { -void CheckSingleAnnotation(const string& expected_file, - const string& expected_text, - const string& file_content, +void CheckSingleAnnotation(const std::string& expected_file, + const std::string& expected_text, + const std::string& file_content, const GeneratedCodeInfo::Annotation& annotation) { EXPECT_EQ(expected_file, annotation.source_file()); ASSERT_GE(file_content.size(), annotation.begin()); @@ -150,12 +149,13 @@ void CheckSingleAnnotation(const string& expected_file, } // anonymous namespace void MockCodeGenerator::CheckGeneratedAnnotations( - const string& name, const string& file, const string& output_directory) { - string file_content; + const string& name, const std::string& file, + const std::string& output_directory) { + std::string file_content; GOOGLE_CHECK_OK( File::GetContents(output_directory + "/" + GetOutputFileName(name, file), &file_content, true)); - string meta_content; + std::string meta_content; GOOGLE_CHECK_OK(File::GetContents( output_directory + "/" + GetOutputFileName(name, file) + ".meta", &meta_content, true)); @@ -170,16 +170,15 @@ void MockCodeGenerator::CheckGeneratedAnnotations( annotations.annotation(2)); } -bool MockCodeGenerator::Generate( - const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const { +bool MockCodeGenerator::Generate(const FileDescriptor* file, + const std::string& parameter, + GeneratorContext* context, + std::string* error) const { bool annotate = false; for (int i = 0; i < file->message_type_count(); i++) { if (HasPrefixString(file->message_type(i)->name(), "MockCodeGenerator_")) { - string command = StripPrefixString(file->message_type(i)->name(), - "MockCodeGenerator_"); + std::string command = StripPrefixString( + file->message_type(i)->name(), "MockCodeGenerator_"); if (command == "Error") { *error = "Saw message type MockCodeGenerator_Error."; return false; @@ -201,8 +200,8 @@ bool MockCodeGenerator::Generate( } else if (command == "HasJsonName") { FieldDescriptorProto field_descriptor_proto; file->message_type(i)->field(0)->CopyTo(&field_descriptor_proto); - std::cerr << "Saw json_name: " - << field_descriptor_proto.has_json_name() << std::endl; + std::cerr << "Saw json_name: " << field_descriptor_proto.has_json_name() + << std::endl; abort(); } else if (command == "Annotate") { annotate = true; @@ -211,8 +210,8 @@ bool MockCodeGenerator::Generate( context->GetCompilerVersion(&compiler_version); std::cerr << "Saw compiler_version: " << compiler_version.major() * 1000000 + - compiler_version.minor() * 1000 + - compiler_version.patch() + compiler_version.minor() * 1000 + + compiler_version.patch() << " " << compiler_version.suffix() << std::endl; abort(); } else { @@ -222,7 +221,7 @@ bool MockCodeGenerator::Generate( } if (HasPrefixString(parameter, "insert=")) { - std::vector insert_into; + std::vector insert_into; SplitStringUsing(StripPrefixString(parameter, "insert="), ",", &insert_into); @@ -231,8 +230,8 @@ bool MockCodeGenerator::Generate( std::unique_ptr output(context->OpenForInsert( GetOutputFileName(insert_into[i], file), kFirstInsertionPointName)); io::Printer printer(output.get(), '$'); - printer.PrintRaw(GetOutputFileContent(name_, "first_insert", - file, context)); + printer.PrintRaw( + GetOutputFileContent(name_, "first_insert", file, context)); if (printer.failed()) { *error = "MockCodeGenerator detected write error."; return false; @@ -244,8 +243,8 @@ bool MockCodeGenerator::Generate( context->OpenForInsert(GetOutputFileName(insert_into[i], file), kSecondInsertionPointName)); io::Printer printer(output.get(), '$'); - printer.PrintRaw(GetOutputFileContent(name_, "second_insert", - file, context)); + printer.PrintRaw( + GetOutputFileContent(name_, "second_insert", file, context)); if (printer.failed()) { *error = "MockCodeGenerator detected write error."; return false; @@ -262,7 +261,7 @@ bool MockCodeGenerator::Generate( io::Printer printer(output.get(), '$', annotate ? &annotation_collector : NULL); printer.PrintRaw(GetOutputFileContent(name_, parameter, file, context)); - string annotate_suffix = "_annotation"; + std::string annotate_suffix = "_annotation"; if (annotate) { printer.Print("$p$", "p", "first"); printer.Annotate("p", "first" + annotate_suffix); @@ -295,39 +294,33 @@ bool MockCodeGenerator::Generate( return true; } -string MockCodeGenerator::GetOutputFileName(const string& generator_name, - const FileDescriptor* file) { +std::string MockCodeGenerator::GetOutputFileName( + const std::string& generator_name, const FileDescriptor* file) { return GetOutputFileName(generator_name, file->name()); } -string MockCodeGenerator::GetOutputFileName(const string& generator_name, - const string& file) { +std::string MockCodeGenerator::GetOutputFileName( + const std::string& generator_name, const std::string& file) { return file + ".MockCodeGenerator." + generator_name; } -string MockCodeGenerator::GetOutputFileContent( - const string& generator_name, - const string& parameter, - const FileDescriptor* file, - GeneratorContext *context) { +std::string MockCodeGenerator::GetOutputFileContent( + const std::string& generator_name, const std::string& parameter, + const FileDescriptor* file, GeneratorContext* context) { std::vector all_files; context->ListParsedFiles(&all_files); return GetOutputFileContent( - generator_name, parameter, file->name(), - CommaSeparatedList(all_files), - file->message_type_count() > 0 ? - file->message_type(0)->name() : "(none)"); + generator_name, parameter, file->name(), CommaSeparatedList(all_files), + file->message_type_count() > 0 ? file->message_type(0)->name() + : "(none)"); } -string MockCodeGenerator::GetOutputFileContent( - const string& generator_name, - const string& parameter, - const string& file, - const string& parsed_file_list, - const string& first_message_name) { - return strings::Substitute("$0: $1, $2, $3, $4\n", - generator_name, parameter, file, - first_message_name, parsed_file_list); +std::string MockCodeGenerator::GetOutputFileContent( + const std::string& generator_name, const std::string& parameter, + const std::string& file, const std::string& parsed_file_list, + const std::string& first_message_name) { + return strings::Substitute("$0: $1, $2, $3, $4\n", generator_name, parameter, + file, first_message_name, parsed_file_list); } } // namespace compiler diff --git a/src/google/protobuf/compiler/mock_code_generator.h b/src/google/protobuf/compiler/mock_code_generator.h index 3b59bc4b17..082ba18d4d 100644 --- a/src/google/protobuf/compiler/mock_code_generator.h +++ b/src/google/protobuf/compiler/mock_code_generator.h @@ -74,7 +74,7 @@ namespace compiler { // that can later be verified with CheckGeneratedAnnotations. class MockCodeGenerator : public CodeGenerator { public: - MockCodeGenerator(const string& name); + MockCodeGenerator(const std::string& name); virtual ~MockCodeGenerator(); // Expect (via gTest) that a MockCodeGenerator with the given name was called @@ -84,45 +84,43 @@ class MockCodeGenerator : public CodeGenerator { // should have inserted lines into this file. // |parsed_file_list| is a comma-separated list of names of the files // that are being compiled together in this run. - static void ExpectGenerated(const string& name, - const string& parameter, - const string& insertions, - const string& file, - const string& first_message_name, - const string& parsed_file_list, - const string& output_directory); + static void ExpectGenerated(const std::string& name, + const std::string& parameter, + const std::string& insertions, + const std::string& file, + const std::string& first_message_name, + const std::string& parsed_file_list, + const std::string& output_directory); // Checks that the correct text ranges were annotated by the // MockCodeGenerator_Annotate directive. - static void CheckGeneratedAnnotations(const string& name, - const string& file, - const string& output_directory); + static void CheckGeneratedAnnotations(const std::string& name, + const std::string& file, + const std::string& output_directory); // Get the name of the file which would be written by the given generator. - static string GetOutputFileName(const string& generator_name, - const FileDescriptor* file); - static string GetOutputFileName(const string& generator_name, - const string& file); + static std::string GetOutputFileName(const std::string& generator_name, + const FileDescriptor* file); + static std::string GetOutputFileName(const std::string& generator_name, + const std::string& file); // implements CodeGenerator ---------------------------------------- virtual bool Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const; + const std::string& parameter, GeneratorContext* context, + std::string* error) const; private: - string name_; - - static string GetOutputFileContent(const string& generator_name, - const string& parameter, - const FileDescriptor* file, - GeneratorContext *context); - static string GetOutputFileContent(const string& generator_name, - const string& parameter, - const string& file, - const string& parsed_file_list, - const string& first_message_name); + std::string name_; + + static std::string GetOutputFileContent(const std::string& generator_name, + const std::string& parameter, + const FileDescriptor* file, + GeneratorContext* context); + static std::string GetOutputFileContent( + const std::string& generator_name, const std::string& parameter, + const std::string& file, const std::string& parsed_file_list, + const std::string& first_message_name); }; } // namespace compiler diff --git a/src/google/protobuf/compiler/objectivec/method_dump.sh b/src/google/protobuf/compiler/objectivec/method_dump.sh new file mode 100755 index 0000000000..193825dbc1 --- /dev/null +++ b/src/google/protobuf/compiler/objectivec/method_dump.sh @@ -0,0 +1,179 @@ +#!/bin/bash + +# Updates objectivec_nsobject_methods.h by generating a list of all of the properties +# and methods on NSObject that Protobufs should not overload from iOS and macOS combined. +# +# The rules: +# - No property should ever be overloaded. +# - Do not overload any methods that have 0 args such as "autorelease". +# - Do not overload any methods that start with "set[A-Z]" and have 1 arg such as +# "setValuesForKeysWithDictionary:". Note that these will end up in the list as just +# the "proto field" name, so "setValuesForKeysWithDictionary:" will become +# "valuesForKeysWithDictionary". + +set -eu + +trim() { + local var="$*" + # remove leading whitespace characters + var="${var#"${var%%[![:space:]]*}"}" + # remove trailing whitespace characters + var="${var%"${var##*[![:space:]]}"}" + echo -n "$var" +} + +objc_code=$(cat <<'END_CODE' +#import +#import + +int main(int argc, const char * argv[]) { + @autoreleasepool { + Class cls = [NSObject class]; + + // List out the protocols on NSObject just so we are aware if they change. + unsigned int protocolCount; + __unsafe_unretained Protocol **protocols = + class_copyProtocolList(cls, &protocolCount); + for (unsigned int i = 0; i < protocolCount; i++) { + printf("// Protocol: %s\n", protocol_getName(protocols[i])); + } + free(protocols); + + // Grab all the properties. + unsigned int propCount; + objc_property_t *props = class_copyPropertyList(cls, &propCount); + NSMutableSet *reservedNames = [[NSMutableSet alloc] init]; + for (unsigned int i = 0; i < propCount; ++i) { + NSString *propertyName = [NSString stringWithUTF8String:property_getName(props[i])]; + [reservedNames addObject:propertyName]; + } + free(props); + + // Note that methods have 2 defaults args (_cmd and SEL) so a method "0 arg method" + // actually has 2. + unsigned int methodCount; + Method *methods = class_copyMethodList(cls, &methodCount); + for (unsigned int i = 0; i < methodCount; ++i) { + int argCount = method_getNumberOfArguments(methods[i]); + NSString *methodName = + [NSString stringWithUTF8String:sel_getName(method_getName(methods[i]))]; + if (argCount == 2) { + [reservedNames addObject:methodName]; + } + if (argCount == 3 && [methodName hasPrefix:@"set"] && methodName.length > 4) { + NSString *firstLetter = [methodName substringWithRange:NSMakeRange(3,1)]; + NSString *lowerFirstLetter = [firstLetter lowercaseString]; + if ([lowerFirstLetter isEqual:firstLetter]) { + // Make sure the next letter is a capital letter so we do not take things like + // settingSomething: + continue; + } + // -5 because 3 for set, 1 for the firstLetter and 1 for the colon on the end. + NSString *restOfString = + [methodName substringWithRange:NSMakeRange(4, methodName.length - 5)]; + methodName = [lowerFirstLetter stringByAppendingString:restOfString]; + [reservedNames addObject:methodName]; + } + } + free(methods); + + SEL sortSelector = @selector(caseInsensitiveCompare:); + NSArray *array = [reservedNames.allObjects sortedArrayUsingSelector:sortSelector]; + for (NSString *item in array) { + // Some items with _ in them get returned in quotes, so do not add more. + if ([item hasPrefix:@"\""]) { + printf("\t%s,\n", item.UTF8String); + } else { + printf("\t\"%s\",\n", item.UTF8String); + } + } + } + return 0; +} +END_CODE +) + +file_header=$(cat <<'END_HEADER' +// NSObject methods +// Autogenerated by method_dump.sh. Do not edit by hand. +// Date: %DATE% +// macOS: %MACOS% +// iOS: %IOS% + +const char* const kNSObjectMethodsList[] = { +END_HEADER +) + +file_footer=$(cat <<'END_FOOTER' +}; +END_FOOTER +) + +# Check to make sure we are updating the correct file. +if [[ ! -e "objectivec_nsobject_methods.h" ]]; then + echo "error: Must be run in the src/google/protobuf/compiler/objectivec directory" + exit 1 +fi + +temp_dir=$(mktemp -d) + +echo "$objc_code" >> "$temp_dir"/method_dump.m + +# Compile up iphonesimulator and macos version of cmd line app. +iphone_simulator_sdk=$(xcrun --sdk iphonesimulator --show-sdk-path) +clang -isysroot "$iphone_simulator_sdk" -o "$temp_dir"/method_dump_ios \ + -framework Foundation -framework UIKit "$temp_dir"/method_dump.m +macos_sdk=$(xcrun --sdk macosx --show-sdk-path) +clang -isysroot "$macos_sdk" -o "$temp_dir"/method_dump_macos -framework Foundation \ + -framework Cocoa "$temp_dir"/method_dump.m + +# Create a device of the latest phone and iphonesimulator SDK and run our iOS cmd line. +device_type=$(xcrun simctl list devicetypes | grep \.iPhone- | tail -1 | sed 's/.*(\(.*\))/\1/') +# runtimes come with a space at the end (for Xcode 10) so let's trim all of our input to +# be safe. +device_type=$(trim "$device_type") +runtime=$(xcrun simctl list runtimes | grep \.iOS- | tail -1 | \ + sed 's/.*\(com\.apple.\CoreSimulator\.SimRuntime\.iOS.*\)/\1/') +runtime=$(trim "$runtime") +uuid=$(uuidgen) +device_name="method_dump_device_$uuid" +device=$(xcrun simctl create "$device_name" "$device_type" "$runtime") +xcrun simctl spawn "$device" "$temp_dir"/method_dump_ios > "$temp_dir"/methods_unsorted_ios.txt +xcrun simctl delete "$device" + +# Run the Mac version +"$temp_dir"/method_dump_macos >> "$temp_dir"/methods_unsorted_macos.txt + +# Generate sorted output +echo "$file_header" | sed -e "s|%DATE%|$(date)|" -e "s|%MACOS%|$(basename $macos_sdk)|" \ + -e "s|%IOS%|$(basename $iphone_simulator_sdk)|" > "$temp_dir"/methods_sorted.txt +sort -u "$temp_dir"/methods_unsorted_ios.txt \ + "$temp_dir"/methods_unsorted_macos.txt >> "$temp_dir"/methods_sorted.txt +echo $"$file_footer" >> "$temp_dir"/methods_sorted.txt + +# Check for differences. Turn off error checking because we expect diff to fail when +# there are no differences. +set +e +diff_out=$(diff -I "^//.*$" "$temp_dir"/methods_sorted.txt objectivec_nsobject_methods.h) +removed_methods=$(echo "$diff_out" | grep '^>.*$') +set -e +if [[ -n "$removed_methods" ]]; then + echo "error: Methods removed from NSObject" + echo "It appears that some methods may have been removed from NSObject." + echo "This could mean that there may be some backwards compatibility issues." + echo "You could potentially build apps that may not work on earlier systems than:" + echo "$iphone_simulator_sdk" + echo "$macos_sdk" + echo "If they declare protobuf types that use any of the following as names:" + echo "$removed_methods" + echo "" + echo "New Version: $temp_dir/methods_sorted.txt" + echo "Old Version: objectivec_nsobject_methods.h" + exit 1 +fi +if [[ -n "$diff_out" ]]; then + echo "Added Methods:" + echo "$(echo "$diff_out" | grep '^<.*$' | sed -e 's/^< "\(.*\)",$/ \1/')" +fi; +cp "$temp_dir"/methods_sorted.txt objectivec_nsobject_methods.h +rm -rf "$temp_dir" diff --git a/src/google/protobuf/compiler/objectivec/objectivec_enum.cc b/src/google/protobuf/compiler/objectivec/objectivec_enum.cc index 978e985cf5..f2967683ee 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_enum.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_enum.cc @@ -35,6 +35,7 @@ #include #include #include +#include // std::find() namespace google { namespace protobuf { @@ -44,6 +45,17 @@ namespace objectivec { EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor) : descriptor_(descriptor), name_(EnumName(descriptor_)) { + // Track the names for the enum values, and if an alias overlaps a base + // value, skip making a name for it. Likewise if two alias overlap, the + // first one wins. + // The one gap in this logic is if two base values overlap, but for that + // to happen you have to have "Foo" and "FOO" or "FOO_BAR" and "FooBar", + // and if an enum has that, it is already going to be confusing and a + // compile error is just fine. + // The values are still tracked to support the reflection apis and + // TextFormat handing since they are different there. + std::set value_names; + for (int i = 0; i < descriptor_->value_count(); i++) { const EnumValueDescriptor* value = descriptor_->value(i); const EnumValueDescriptor* canonical_value = @@ -51,6 +63,14 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor) if (value == canonical_value) { base_values_.push_back(value); + value_names.insert(EnumValueName(value)); + } else { + string value_name(EnumValueName(value)); + if (value_names.find(value_name) != value_names.end()) { + alias_values_to_skip_.insert(value); + } else { + value_names.insert(value_name); + } } all_values_.push_back(value); } @@ -72,6 +92,20 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) { "\n", "name", name_); + // Swift 5 included SE0192 "Handling Future Enum Cases" + // https://github.com/apple/swift-evolution/blob/master/proposals/0192-non-exhaustive-enums.md + // Since a .proto file can get new values added to an enum at any time, they + // are effectively "non-frozen". Even in a proto3 syntax file where there is + // support for the unknown value, an edit to the file can always add a new + // value moving something from unknown to known. Since Swift is now ABI + // stable, it also means a binary could contain Swift compiled against one + // version of the .pbobjc.h file, but finally linked against an enum with + // more cases. So the Swift code will always have to treat ObjC Proto Enums + // as "non-frozen". The default behavior in SE0192 is for all objc enums to + // be "non-frozen" unless marked as otherwise, so this means this generation + // doesn't have to bother with the `enum_extensibility` attribute, as the + // default will be what is needed. + printer->Print("$comments$typedef$deprecated_attribute$ GPB_ENUM($name$) {\n", "comments", enum_comments, "deprecated_attribute", GetOptionalDeprecatedAttribute(descriptor_, descriptor_->file()), @@ -90,6 +124,9 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) { "name", name_); } for (int i = 0; i < all_values_.size(); i++) { + if (alias_values_to_skip_.find(all_values_[i]) != alias_values_to_skip_.end()) { + continue; + } SourceLocation location; if (all_values_[i]->GetSourceLocation(&location)) { string comments = BuildCommentsString(location, true).c_str(); @@ -105,7 +142,7 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) { "$name$$deprecated_attribute$ = $value$,\n", "name", EnumValueName(all_values_[i]), "deprecated_attribute", GetOptionalDeprecatedAttribute(all_values_[i]), - "value", SimpleItoa(all_values_[i]->number())); + "value", StrCat(all_values_[i]->number())); } printer->Outdent(); printer->Print( diff --git a/src/google/protobuf/compiler/objectivec/objectivec_enum.h b/src/google/protobuf/compiler/objectivec/objectivec_enum.h index f52e9e687b..50a6564479 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_enum.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_enum.h @@ -35,14 +35,9 @@ #include #include #include +#include namespace google { -namespace protobuf { -namespace io { -class Printer; // printer.h -} -} - namespace protobuf { namespace compiler { namespace objectivec { @@ -52,6 +47,9 @@ class EnumGenerator { explicit EnumGenerator(const EnumDescriptor* descriptor); ~EnumGenerator(); + EnumGenerator(const EnumGenerator&) = delete; + EnumGenerator& operator=(const EnumGenerator&) = delete; + void GenerateHeader(io::Printer* printer); void GenerateSource(io::Printer* printer); @@ -61,13 +59,13 @@ class EnumGenerator { const EnumDescriptor* descriptor_; std::vector base_values_; std::vector all_values_; + std::set alias_values_to_skip_; const string name_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumGenerator); }; } // namespace objectivec } // namespace compiler } // namespace protobuf } // namespace google + #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_ENUM_H__ diff --git a/src/google/protobuf/compiler/objectivec/objectivec_enum_field.cc b/src/google/protobuf/compiler/objectivec/objectivec_enum_field.cc index 8899a13aa9..9890d0a140 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_enum_field.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_enum_field.cc @@ -32,11 +32,9 @@ #include #include -#include #include #include #include -#include namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/objectivec/objectivec_enum_field.h b/src/google/protobuf/compiler/objectivec/objectivec_enum_field.h index ae56c069a1..5a69c97867 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_enum_field.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_enum_field.h @@ -44,6 +44,9 @@ class EnumFieldGenerator : public SingleFieldGenerator { friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field, const Options& options); + EnumFieldGenerator(const EnumFieldGenerator&) = delete; + EnumFieldGenerator& operator=(const EnumFieldGenerator&) = delete; + public: virtual void GenerateCFunctionDeclarations(io::Printer* printer) const; virtual void GenerateCFunctionImplementations(io::Printer* printer) const; @@ -52,9 +55,6 @@ class EnumFieldGenerator : public SingleFieldGenerator { protected: EnumFieldGenerator(const FieldDescriptor* descriptor, const Options& options); virtual ~EnumFieldGenerator(); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator); }; class RepeatedEnumFieldGenerator : public RepeatedFieldGenerator { @@ -68,9 +68,6 @@ class RepeatedEnumFieldGenerator : public RepeatedFieldGenerator { RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, const Options& options); virtual ~RepeatedEnumFieldGenerator(); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedEnumFieldGenerator); }; } // namespace objectivec diff --git a/src/google/protobuf/compiler/objectivec/objectivec_extension.cc b/src/google/protobuf/compiler/objectivec/objectivec_extension.cc index b788d0a32a..bf65de3be2 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_extension.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_extension.cc @@ -61,6 +61,11 @@ ExtensionGenerator::~ExtensionGenerator() {} void ExtensionGenerator::GenerateMembersHeader(io::Printer* printer) { std::map vars; vars["method_name"] = method_name_; + if (IsRetainedName(method_name_)) { + vars["storage_attribute"] = " NS_RETURNS_NOT_RETAINED"; + } else { + vars["storage_attribute"] = ""; + } SourceLocation location; if (descriptor_->GetSourceLocation(&location)) { vars["comments"] = BuildCommentsString(location, true); @@ -72,7 +77,7 @@ void ExtensionGenerator::GenerateMembersHeader(io::Printer* printer) { vars["deprecated_attribute"] = GetOptionalDeprecatedAttribute(descriptor_, descriptor_->file()); printer->Print(vars, "$comments$" - "+ (GPBExtensionDescriptor *)$method_name$$deprecated_attribute$;\n"); + "+ (GPBExtensionDescriptor *)$method_name$$storage_attribute$$deprecated_attribute$;\n"); } void ExtensionGenerator::GenerateStaticVariablesInitialization( @@ -80,7 +85,7 @@ void ExtensionGenerator::GenerateStaticVariablesInitialization( std::map vars; vars["root_class_and_method_name"] = root_class_and_method_name_; vars["extended_type"] = ClassName(descriptor_->containing_type()); - vars["number"] = SimpleItoa(descriptor_->number()); + vars["number"] = StrCat(descriptor_->number()); std::vector options; if (descriptor_->is_repeated()) options.push_back("GPBExtensionRepeated"); diff --git a/src/google/protobuf/compiler/objectivec/objectivec_extension.h b/src/google/protobuf/compiler/objectivec/objectivec_extension.h index e361e639bd..d49a4f1cc2 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_extension.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_extension.h @@ -31,16 +31,10 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_EXTENSION_H__ #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_EXTENSION_H__ -#include +#include +#include namespace google { -namespace protobuf { -class FieldDescriptor; // descriptor.h -namespace io { -class Printer; // printer.h -} -} - namespace protobuf { namespace compiler { namespace objectivec { @@ -51,6 +45,9 @@ class ExtensionGenerator { const FieldDescriptor* descriptor); ~ExtensionGenerator(); + ExtensionGenerator(const ExtensionGenerator&) = delete; + ExtensionGenerator& operator=(const ExtensionGenerator&) = delete; + void GenerateMembersHeader(io::Printer* printer); void GenerateStaticVariablesInitialization(io::Printer* printer); void GenerateRegistrationSource(io::Printer* printer); @@ -59,11 +56,11 @@ class ExtensionGenerator { string method_name_; string root_class_and_method_name_; const FieldDescriptor* descriptor_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionGenerator); }; + } // namespace objectivec } // namespace compiler } // namespace protobuf } // namespace google + #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_MESSAGE_H__ diff --git a/src/google/protobuf/compiler/objectivec/objectivec_field.cc b/src/google/protobuf/compiler/objectivec/objectivec_field.cc index f74599ba85..f585941ea5 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_field.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_field.cc @@ -38,7 +38,6 @@ #include #include #include -#include #include namespace google { @@ -76,7 +75,7 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, (*variables)["raw_field_name"] = raw_field_name; (*variables)["field_number_name"] = classname + "_FieldNumber_" + capitalized_name; - (*variables)["field_number"] = SimpleItoa(descriptor->number()); + (*variables)["field_number"] = StrCat(descriptor->number()); (*variables)["field_type"] = GetCapitalizedType(descriptor); (*variables)["deprecated_attribute"] = GetOptionalDeprecatedAttribute(descriptor); std::vector field_flags; @@ -214,7 +213,7 @@ void FieldGenerator::GenerateFieldDescription( } void FieldGenerator::SetRuntimeHasBit(int has_index) { - variables_["has_index"] = SimpleItoa(has_index); + variables_["has_index"] = StrCat(has_index); } void FieldGenerator::SetNoHasBit(void) { @@ -237,7 +236,7 @@ void FieldGenerator::SetOneofIndexBase(int index_base) { if (descriptor_->containing_oneof() != NULL) { int index = descriptor_->containing_oneof()->index() + index_base; // Flip the sign to mark it as a oneof. - variables_["has_index"] = SimpleItoa(-index); + variables_["has_index"] = StrCat(-index); } } diff --git a/src/google/protobuf/compiler/objectivec/objectivec_field.h b/src/google/protobuf/compiler/objectivec/objectivec_field.h index 216034d067..24465d0a36 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_field.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_field.h @@ -34,16 +34,11 @@ #include #include #include -#include #include +#include namespace google { namespace protobuf { - -namespace io { -class Printer; // printer.h -} // namespace io - namespace compiler { namespace objectivec { @@ -54,6 +49,9 @@ class FieldGenerator { virtual ~FieldGenerator(); + FieldGenerator(const FieldGenerator&) = delete; + FieldGenerator& operator=(const FieldGenerator&) = delete; + // Exposed for subclasses to fill in. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const = 0; virtual void GeneratePropertyDeclaration(io::Printer* printer) const = 0; @@ -101,15 +99,15 @@ class FieldGenerator { const FieldDescriptor* descriptor_; std::map variables_; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator); }; class SingleFieldGenerator : public FieldGenerator { public: virtual ~SingleFieldGenerator(); + SingleFieldGenerator(const SingleFieldGenerator&) = delete; + SingleFieldGenerator& operator=(const SingleFieldGenerator&) = delete; + virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; virtual void GeneratePropertyDeclaration(io::Printer* printer) const; @@ -121,9 +119,6 @@ class SingleFieldGenerator : public FieldGenerator { SingleFieldGenerator(const FieldDescriptor* descriptor, const Options& options); virtual bool WantsHasProperty(void) const; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SingleFieldGenerator); }; // Subclass with common support for when the field ends up as an ObjC Object. @@ -131,21 +126,24 @@ class ObjCObjFieldGenerator : public SingleFieldGenerator { public: virtual ~ObjCObjFieldGenerator(); + ObjCObjFieldGenerator(const ObjCObjFieldGenerator&) = delete; + ObjCObjFieldGenerator& operator=(const ObjCObjFieldGenerator&) = delete; + virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; virtual void GeneratePropertyDeclaration(io::Printer* printer) const; protected: ObjCObjFieldGenerator(const FieldDescriptor* descriptor, const Options& options); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjCObjFieldGenerator); }; class RepeatedFieldGenerator : public ObjCObjFieldGenerator { public: virtual ~RepeatedFieldGenerator(); + RepeatedFieldGenerator(const RepeatedFieldGenerator&) = delete; + RepeatedFieldGenerator& operator=(const RepeatedFieldGenerator&) = delete; + virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; virtual void GeneratePropertyDeclaration(io::Printer* printer) const; @@ -158,9 +156,6 @@ class RepeatedFieldGenerator : public ObjCObjFieldGenerator { const Options& options); virtual void FinishInitialization(void); virtual bool WantsHasProperty(void) const; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedFieldGenerator); }; // Convenience class which constructs FieldGenerators for a Descriptor. @@ -169,6 +164,9 @@ class FieldGeneratorMap { FieldGeneratorMap(const Descriptor* descriptor, const Options& options); ~FieldGeneratorMap(); + FieldGeneratorMap(const FieldGeneratorMap&) = delete; + FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; + const FieldGenerator& get(const FieldDescriptor* field) const; const FieldGenerator& get_extension(int index) const; @@ -184,11 +182,11 @@ class FieldGeneratorMap { const Descriptor* descriptor_; std::vector> field_generators_; std::vector> extension_generators_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); }; + } // namespace objectivec } // namespace compiler } // namespace protobuf } // namespace google + #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__ diff --git a/src/google/protobuf/compiler/objectivec/objectivec_file.cc b/src/google/protobuf/compiler/objectivec/objectivec_file.cc index f0d9b4d5f5..7bc585e9ba 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_file.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_file.cc @@ -192,27 +192,21 @@ FileGenerator::FileGenerator(const FileDescriptor *file, const Options& options) options_(options) { for (int i = 0; i < file_->enum_type_count(); i++) { EnumGenerator *generator = new EnumGenerator(file_->enum_type(i)); - enum_generators_.push_back(generator); + enum_generators_.emplace_back(generator); } for (int i = 0; i < file_->message_type_count(); i++) { MessageGenerator *generator = new MessageGenerator(root_class_name_, file_->message_type(i), options_); - message_generators_.push_back(generator); + message_generators_.emplace_back(generator); } for (int i = 0; i < file_->extension_count(); i++) { ExtensionGenerator *generator = new ExtensionGenerator(root_class_name_, file_->extension(i)); - extension_generators_.push_back(generator); + extension_generators_.emplace_back(generator); } } -FileGenerator::~FileGenerator() { - STLDeleteContainerPointers(enum_generators_.begin(), enum_generators_.end()); - STLDeleteContainerPointers(message_generators_.begin(), - message_generators_.end()); - STLDeleteContainerPointers(extension_generators_.begin(), - extension_generators_.end()); -} +FileGenerator::~FileGenerator() {} void FileGenerator::GenerateHeader(io::Printer *printer) { std::set headers; @@ -241,7 +235,7 @@ void FileGenerator::GenerateHeader(io::Printer *printer) { "#error This file was generated by an older version of protoc which is incompatible with your Protocol Buffer library sources.\n" "#endif\n" "\n", - "google_protobuf_objc_version", SimpleItoa(GOOGLE_PROTOBUF_OBJC_VERSION)); + "google_protobuf_objc_version", StrCat(GOOGLE_PROTOBUF_OBJC_VERSION)); // #import any headers for "public imports" in the proto file. { @@ -270,9 +264,8 @@ void FileGenerator::GenerateHeader(io::Printer *printer) { "\n"); std::set fwd_decls; - for (std::vector::iterator iter = message_generators_.begin(); - iter != message_generators_.end(); ++iter) { - (*iter)->DetermineForwardDeclarations(&fwd_decls); + for (const auto& generator : message_generators_) { + generator->DetermineForwardDeclarations(&fwd_decls); } for (std::set::const_iterator i(fwd_decls.begin()); i != fwd_decls.end(); ++i) { @@ -287,14 +280,12 @@ void FileGenerator::GenerateHeader(io::Printer *printer) { "\n"); // need to write out all enums first - for (std::vector::iterator iter = enum_generators_.begin(); - iter != enum_generators_.end(); ++iter) { - (*iter)->GenerateHeader(printer); + for (const auto& generator : enum_generators_) { + generator->GenerateHeader(printer); } - for (std::vector::iterator iter = message_generators_.begin(); - iter != message_generators_.end(); ++iter) { - (*iter)->GenerateEnumHeader(printer); + for (const auto& generator : message_generators_) { + generator->GenerateEnumHeader(printer); } // For extensions to chain together, the Root gets created even if there @@ -323,18 +314,15 @@ void FileGenerator::GenerateHeader(io::Printer *printer) { "@interface $root_class_name$ (DynamicMethods)\n", "root_class_name", root_class_name_); - for (std::vector::iterator iter = - extension_generators_.begin(); - iter != extension_generators_.end(); ++iter) { - (*iter)->GenerateMembersHeader(printer); + for (const auto& generator : extension_generators_) { + generator->GenerateMembersHeader(printer); } printer->Print("@end\n\n"); } // extension_generators_.size() > 0 - for (std::vector::iterator iter = message_generators_.begin(); - iter != message_generators_.end(); ++iter) { - (*iter)->GenerateMessageHeader(printer); + for (const auto& generator : message_generators_) { + generator->GenerateMessageHeader(printer); } printer->Print( @@ -403,9 +391,8 @@ void FileGenerator::GenerateSource(io::Printer *printer) { } bool includes_oneof = false; - for (std::vector::iterator iter = message_generators_.begin(); - iter != message_generators_.end(); ++iter) { - if ((*iter)->IncludesOneOfDefinition()) { + for (const auto& generator : message_generators_) { + if (generator->IncludesOneOfDefinition()) { includes_oneof = true; break; } @@ -456,15 +443,11 @@ void FileGenerator::GenerateSource(io::Printer *printer) { printer->Print( "static GPBExtensionDescription descriptions[] = {\n"); printer->Indent(); - for (std::vector::iterator iter = - extension_generators_.begin(); - iter != extension_generators_.end(); ++iter) { - (*iter)->GenerateStaticVariablesInitialization(printer); + for (const auto& generator : extension_generators_) { + generator->GenerateStaticVariablesInitialization(printer); } - for (std::vector::iterator iter = - message_generators_.begin(); - iter != message_generators_.end(); ++iter) { - (*iter)->GenerateStaticVariablesInitialization(printer); + for (const auto& generator : message_generators_) { + generator->GenerateStaticVariablesInitialization(printer); } printer->Outdent(); printer->Print( @@ -561,13 +544,11 @@ void FileGenerator::GenerateSource(io::Printer *printer) { "\n"); } - for (std::vector::iterator iter = enum_generators_.begin(); - iter != enum_generators_.end(); ++iter) { - (*iter)->GenerateSource(printer); + for (const auto& generator : enum_generators_) { + generator->GenerateSource(printer); } - for (std::vector::iterator iter = message_generators_.begin(); - iter != message_generators_.end(); ++iter) { - (*iter)->GenerateSource(printer); + for (const auto& generator : message_generators_) { + generator->GenerateSource(printer); } printer->Print( diff --git a/src/google/protobuf/compiler/objectivec/objectivec_file.h b/src/google/protobuf/compiler/objectivec/objectivec_file.h index 1754fc0ad2..fd57263484 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_file.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_file.h @@ -35,16 +35,10 @@ #include #include #include -#include +#include +#include namespace google { -namespace protobuf { -class FileDescriptor; // descriptor.h -namespace io { -class Printer; // printer.h -} -} - namespace protobuf { namespace compiler { namespace objectivec { @@ -58,6 +52,9 @@ class FileGenerator { FileGenerator(const FileDescriptor* file, const Options& options); ~FileGenerator(); + FileGenerator(const FileGenerator&) = delete; + FileGenerator& operator=(const FileGenerator&) = delete; + void GenerateSource(io::Printer* printer); void GenerateHeader(io::Printer* printer); @@ -68,16 +65,14 @@ class FileGenerator { string root_class_name_; bool is_bundled_proto_; - std::vector enum_generators_; - std::vector message_generators_; - std::vector extension_generators_; + std::vector> enum_generators_; + std::vector> message_generators_; + std::vector> extension_generators_; const Options options_; void PrintFileRuntimePreamble( io::Printer* printer, const std::set& headers_to_import) const; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator); }; } // namespace objectivec diff --git a/src/google/protobuf/compiler/objectivec/objectivec_generator.cc b/src/google/protobuf/compiler/objectivec/objectivec_generator.cc index e0597cc7ff..25338ad939 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_generator.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_generator.cc @@ -89,6 +89,12 @@ bool ObjectiveCGenerator::GenerateAll(const std::vector& // There is no validation that the prefixes are good prefixes, it is // assumed that they are when you create the file. generation_options.expected_prefixes_path = options[i].second; + } else if (options[i].first == "expected_prefixes_suppressions") { + // A semicolon delimited string that lists the paths of .proto files to + // exclude from the package prefix validations (expected_prefixes_path). + // This is provided as an "out", to skip some files being checked. + SplitStringUsing(options[i].second, ";", + &generation_options.expected_prefixes_suppressions); } else if (options[i].first == "generate_for_named_framework") { // The name of the framework that protos are being generated for. This // will cause the #import statements to be framework based using this diff --git a/src/google/protobuf/compiler/objectivec/objectivec_generator.h b/src/google/protobuf/compiler/objectivec/objectivec_generator.h index 3e43f732d8..d10efc20ef 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_generator.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_generator.h @@ -35,6 +35,9 @@ #include #include +#include + +#include namespace google { namespace protobuf { @@ -45,11 +48,14 @@ namespace objectivec { // header. If you create your own protocol compiler binary and you want it to // support ObjectiveC output, you can do so by registering an instance of this // CodeGenerator with the CommandLineInterface in your main() function. -class LIBPROTOC_EXPORT ObjectiveCGenerator : public CodeGenerator { +class PROTOC_EXPORT ObjectiveCGenerator : public CodeGenerator { public: ObjectiveCGenerator(); ~ObjectiveCGenerator(); + ObjectiveCGenerator(const ObjectiveCGenerator&) = delete; + ObjectiveCGenerator& operator=(const ObjectiveCGenerator&) = delete; + // implements CodeGenerator ---------------------------------------- bool HasGenerateAll() const; bool Generate(const FileDescriptor* file, @@ -60,13 +66,13 @@ class LIBPROTOC_EXPORT ObjectiveCGenerator : public CodeGenerator { const string& parameter, GeneratorContext* context, string* error) const; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectiveCGenerator); }; } // namespace objectivec } // namespace compiler } // namespace protobuf } // namespace google + +#include + #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc b/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc index 0afa6fb060..e8d6d9e1cb 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc @@ -41,17 +41,16 @@ #include #include -#include #include +#include #include #include #include #include -#include -#include +#include +#include #include - // NOTE: src/google/protobuf/compiler/plugin.cc makes use of cerr for some // error cases, so it seems to be ok to use as a back door for errors. @@ -64,7 +63,7 @@ namespace objectivec { // in this port namespace to avoid ambiguous definition. namespace posix { #ifdef _WIN32 -using ::google::protobuf::internal::win32::open; +using ::google::protobuf::io::win32::open; #else using ::open; #endif @@ -76,6 +75,10 @@ Options::Options() { if (file_path) { expected_prefixes_path = file_path; } + const char* suppressions = getenv("GPB_OBJC_EXPECTED_PACKAGE_PREFIXES_SUPPRESSIONS"); + if (suppressions) { + SplitStringUsing(suppressions, ";", &expected_prefixes_suppressions); + } } namespace { @@ -166,70 +169,111 @@ string UnderscoresToCamelCase(const string& input, bool first_capitalized) { } const char* const kReservedWordList[] = { - // Objective C "keywords" that aren't in C - // From - // http://stackoverflow.com/questions/1873630/reserved-keywords-in-objective-c - "id", "_cmd", "super", "in", "out", "inout", "bycopy", "byref", "oneway", - "self", - - // C/C++ keywords (Incl C++ 0x11) - // From http://en.cppreference.com/w/cpp/keywords - "and", "and_eq", "alignas", "alignof", "asm", "auto", "bitand", "bitor", - "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class", - "compl", "const", "constexpr", "const_cast", "continue", "decltype", - "default", "delete", "double", "dynamic_cast", "else", "enum", "explicit", - "export", "extern ", "false", "float", "for", "friend", "goto", "if", - "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not", - "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", - "public", "register", "reinterpret_cast", "return", "short", "signed", - "sizeof", "static", "static_assert", "static_cast", "struct", "switch", - "template", "this", "thread_local", "throw", "true", "try", "typedef", - "typeid", "typename", "union", "unsigned", "using", "virtual", "void", - "volatile", "wchar_t", "while", "xor", "xor_eq", - - // C99 keywords - // From - // http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fkeyw.htm - "restrict", - - // Objective-C Runtime typedefs - // From - "Category", "Ivar", "Method", "Protocol", - - // NSObject Methods - // new is covered by C++ keywords. - "description", "debugDescription", "finalize", "hash", "dealloc", "init", - "class", "superclass", "retain", "release", "autorelease", "retainCount", - "zone", "isProxy", "copy", "mutableCopy", "classForCoder", - - // GPBMessage Methods - // Only need to add instance methods that may conflict with - // method declared in protos. The main cases are methods - // that take no arguments, or setFoo:/hasFoo: type methods. - "clear", "data", "delimitedData", "descriptor", "extensionRegistry", - "extensionsCurrentlySet", "initialized", "isInitialized", "serializedSize", - "sortedExtensionsInUse", "unknownFields", - - // MacTypes.h names - "Fixed", "Fract", "Size", "LogicalAddress", "PhysicalAddress", "ByteCount", - "ByteOffset", "Duration", "AbsoluteTime", "OptionBits", "ItemCount", - "PBVersion", "ScriptCode", "LangCode", "RegionCode", "OSType", - "ProcessSerialNumber", "Point", "Rect", "FixedPoint", "FixedRect", "Style", - "StyleParameter", "StyleField", "TimeScale", "TimeBase", "TimeRecord", + // Note NSObject Methods: + // These are brought in from objectivec_nsobject_methods.h that is generated + // using method_dump.sh. See kNSObjectMethods below. + + // Objective C "keywords" that aren't in C + // From + // http://stackoverflow.com/questions/1873630/reserved-keywords-in-objective-c + // with some others added on. + "id", "_cmd", "super", "in", "out", "inout", "bycopy", "byref", "oneway", + "self", "instancetype", "nullable", "nonnull", "nil", "Nil", + "YES", "NO", "weak", + + // C/C++ keywords (Incl C++ 0x11) + // From http://en.cppreference.com/w/cpp/keywords + "and", "and_eq", "alignas", "alignof", "asm", "auto", "bitand", "bitor", + "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class", + "compl", "const", "constexpr", "const_cast", "continue", "decltype", + "default", "delete", "double", "dynamic_cast", "else", "enum", "explicit", + "export", "extern ", "false", "float", "for", "friend", "goto", "if", + "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not", + "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", + "public", "register", "reinterpret_cast", "return", "short", "signed", + "sizeof", "static", "static_assert", "static_cast", "struct", "switch", + "template", "this", "thread_local", "throw", "true", "try", "typedef", + "typeid", "typename", "union", "unsigned", "using", "virtual", "void", + "volatile", "wchar_t", "while", "xor", "xor_eq", + + // C99 keywords + // From + // http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fkeyw.htm + "restrict", + + // GCC/Clang extension + "typeof", + + // Not a keyword, but will break you + "NULL", + + // Objective-C Runtime typedefs + // From + "Category", "Ivar", "Method", "Protocol", + + // GPBMessage Methods + // Only need to add instance methods that may conflict with + // method declared in protos. The main cases are methods + // that take no arguments, or setFoo:/hasFoo: type methods. + "clear", "data", "delimitedData", "descriptor", "extensionRegistry", + "extensionsCurrentlySet", "initialized", "isInitialized", "serializedSize", + "sortedExtensionsInUse", "unknownFields", + + // MacTypes.h names + "Fixed", "Fract", "Size", "LogicalAddress", "PhysicalAddress", "ByteCount", + "ByteOffset", "Duration", "AbsoluteTime", "OptionBits", "ItemCount", + "PBVersion", "ScriptCode", "LangCode", "RegionCode", "OSType", + "ProcessSerialNumber", "Point", "Rect", "FixedPoint", "FixedRect", "Style", + "StyleParameter", "StyleField", "TimeScale", "TimeBase", "TimeRecord", }; -std::unordered_set kReservedWords = - MakeWordsMap(kReservedWordList, GOOGLE_ARRAYSIZE(kReservedWordList)); +// returns true is input starts with __ or _[A-Z] which are reserved identifiers +// in C/ C++. All calls should go through UnderscoresToCamelCase before getting here +// but this verifies and allows for future expansion if we decide to redefine what a +// reserved C identifier is (for example the GNU list +// https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html ) +bool IsReservedCIdentifier(const string& input) { + if (input.length() > 2) { + if (input.at(0) == '_') { + if (isupper(input.at(1)) || input.at(1) == '_') { + return true; + } + } + } + return false; +} -string SanitizeNameForObjC(const string& input, +string SanitizeNameForObjC(const string& prefix, + const string& input, const string& extension, string* out_suffix_added) { - if (kReservedWords.count(input) > 0) { + static const std::unordered_set kReservedWords = + MakeWordsMap(kReservedWordList, GOOGLE_ARRAYSIZE(kReservedWordList)); + static const std::unordered_set kNSObjectMethods = + MakeWordsMap(kNSObjectMethodsList, GOOGLE_ARRAYSIZE(kNSObjectMethodsList)); + string sanitized; + // We add the prefix in the cases where the string is missing a prefix. + // We define "missing a prefix" as where 'input': + // a) Doesn't start with the prefix or + // b) Isn't equivalent to the prefix or + // c) Has the prefix, but the letter after the prefix is lowercase + if (HasPrefixString(input, prefix)) { + if (input.length() == prefix.length() || !ascii_isupper(input[prefix.length()])) { + sanitized = prefix + input; + } else { + sanitized = input; + } + } else { + sanitized = prefix + input; + } + if (IsReservedCIdentifier(sanitized) || + (kReservedWords.count(sanitized) > 0) || + (kNSObjectMethods.count(sanitized) > 0)) { if (out_suffix_added) *out_suffix_added = extension; - return input + extension; + return sanitized + extension; } if (out_suffix_added) out_suffix_added->clear(); - return input; + return sanitized; } string NameFromFieldDescriptor(const FieldDescriptor* field) { @@ -320,7 +364,7 @@ string StripProto(const string& filename) { } } -void StringPieceTrimWhitespace(StringPiece* input) { +void TrimWhitespace(StringPiece* input) { while (!input->empty() && ascii_isspace(*input->data())) { input->remove_prefix(1); } @@ -388,12 +432,11 @@ string FilePathBasename(const FileDescriptor* file) { } string FileClassName(const FileDescriptor* file) { - string name = FileClassPrefix(file); - name += UnderscoresToCamelCase(StripProto(BaseFileName(file)), true); - name += "Root"; + const string prefix = FileClassPrefix(file); + const string name = UnderscoresToCamelCase(StripProto(BaseFileName(file)), true) + "Root"; // There aren't really any reserved words that end in "Root", but playing // it safe and checking. - return SanitizeNameForObjC(name, "_RootClass", NULL); + return SanitizeNameForObjC(prefix, name, "_RootClass", NULL); } string ClassNameWorker(const Descriptor* descriptor) { @@ -421,9 +464,9 @@ string ClassName(const Descriptor* descriptor) { string ClassName(const Descriptor* descriptor, string* out_suffix_added) { // 1. Message names are used as is (style calls for CamelCase, trust it). // 2. Check for reserved word at the very end and then suffix things. - string prefix = FileClassPrefix(descriptor->file()); - string name = ClassNameWorker(descriptor); - return SanitizeNameForObjC(prefix + name, "_Class", out_suffix_added); + const string prefix = FileClassPrefix(descriptor->file()); + const string name = ClassNameWorker(descriptor); + return SanitizeNameForObjC(prefix, name, "_Class", out_suffix_added); } string EnumName(const EnumDescriptor* descriptor) { @@ -435,9 +478,9 @@ string EnumName(const EnumDescriptor* descriptor) { // ... // } // yields Fixed_Class, Fixed_Size. - string name = FileClassPrefix(descriptor->file()); - name += ClassNameWorker(descriptor); - return SanitizeNameForObjC(name, "_Enum", NULL); + const string prefix = FileClassPrefix(descriptor->file()); + const string name = ClassNameWorker(descriptor); + return SanitizeNameForObjC(prefix, name, "_Enum", NULL); } string EnumValueName(const EnumValueDescriptor* descriptor) { @@ -447,12 +490,12 @@ string EnumValueName(const EnumValueDescriptor* descriptor) { // FOO = 1 // } // yields Fixed_Enum and Fixed_Enum_Foo (not Fixed_Foo). - const string& class_name = EnumName(descriptor->type()); - const string& value_str = UnderscoresToCamelCase(descriptor->name(), true); - const string& name = class_name + "_" + value_str; + const string class_name = EnumName(descriptor->type()); + const string value_str = UnderscoresToCamelCase(descriptor->name(), true); + const string name = class_name + "_" + value_str; // There aren't really any reserved words with an underscore and a leading // capital letter, but playing it safe and checking. - return SanitizeNameForObjC(name, "_Value", NULL); + return SanitizeNameForObjC("", name, "_Value", NULL); } string EnumValueShortName(const EnumValueDescriptor* descriptor) { @@ -468,9 +511,9 @@ string EnumValueShortName(const EnumValueDescriptor* descriptor) { // So the right way to get the short name is to take the full enum name // and then strip off the enum name (leaving the value name and anything // done by sanitize). - const string& class_name = EnumName(descriptor->type()); - const string& long_name_prefix = class_name + "_"; - const string& long_name = EnumValueName(descriptor); + const string class_name = EnumName(descriptor->type()); + const string long_name_prefix = class_name + "_"; + const string long_name = EnumValueName(descriptor); return StripPrefixString(long_name, long_name_prefix); } @@ -487,13 +530,13 @@ string UnCamelCaseEnumShortName(const string& name) { } string ExtensionMethodName(const FieldDescriptor* descriptor) { - const string& name = NameFromFieldDescriptor(descriptor); - const string& result = UnderscoresToCamelCase(name, false); - return SanitizeNameForObjC(result, "_Extension", NULL); + const string name = NameFromFieldDescriptor(descriptor); + const string result = UnderscoresToCamelCase(name, false); + return SanitizeNameForObjC("", result, "_Extension", NULL); } string FieldName(const FieldDescriptor* field) { - const string& name = NameFromFieldDescriptor(field); + const string name = NameFromFieldDescriptor(field); string result = UnderscoresToCamelCase(name, false); if (field->is_repeated() && !field->is_map()) { // Add "Array" before do check for reserved worlds. @@ -504,7 +547,7 @@ string FieldName(const FieldDescriptor* field) { result += "_p"; } } - return SanitizeNameForObjC(result, "_p", NULL); + return SanitizeNameForObjC("", result, "_p", NULL); } string FieldNameCapitalized(const FieldDescriptor* field) { @@ -616,7 +659,7 @@ string GetCapitalizedType(const FieldDescriptor* field) { // Some compilers report reaching end of function even though all cases of // the enum are handed in the switch. GOOGLE_LOG(FATAL) << "Can't get here."; - return NULL; + return string(); } ObjectiveCType GetObjectiveCType(FieldDescriptor::Type field_type) { @@ -744,7 +787,7 @@ string GPBGenericValueFieldName(const FieldDescriptor* field) { // Some compilers report reaching end of function even though all cases of // the enum are handed in the switch. GOOGLE_LOG(FATAL) << "Can't get here."; - return NULL; + return string(); } @@ -762,17 +805,17 @@ string DefaultValue(const FieldDescriptor* field) { if (field->default_value_int32() == INT_MIN) { return "-0x80000000"; } - return SimpleItoa(field->default_value_int32()); + return StrCat(field->default_value_int32()); case FieldDescriptor::CPPTYPE_UINT32: - return SimpleItoa(field->default_value_uint32()) + "U"; + return StrCat(field->default_value_uint32()) + "U"; case FieldDescriptor::CPPTYPE_INT64: // gcc and llvm reject the decimal form of kint32min and kint64min. if (field->default_value_int64() == LLONG_MIN) { return "-0x8000000000000000LL"; } - return SimpleItoa(field->default_value_int64()) + "LL"; + return StrCat(field->default_value_int64()) + "LL"; case FieldDescriptor::CPPTYPE_UINT64: - return SimpleItoa(field->default_value_uint64()) + "ULL"; + return StrCat(field->default_value_uint64()) + "ULL"; case FieldDescriptor::CPPTYPE_DOUBLE: return HandleExtremeFloatingPoint( SimpleDtoa(field->default_value_double()), false); @@ -816,7 +859,7 @@ string DefaultValue(const FieldDescriptor* field) { // Some compilers report reaching end of function even though all cases of // the enum are handed in the switch. GOOGLE_LOG(FATAL) << "Can't get here."; - return NULL; + return string(); } bool HasNonZeroDefaultValue(const FieldDescriptor* field) { @@ -1004,18 +1047,17 @@ bool ExpectedPrefixesCollector::ConsumeLine( const StringPiece& line, string* out_error) { int offset = line.find('='); if (offset == StringPiece::npos) { - *out_error = - string("Expected prefixes file line without equal sign: '") + - line.ToString() + "'."; + *out_error = string("Expected prefixes file line without equal sign: '") + + string(line) + "'."; return false; } - StringPiece package(line, 0, offset); - StringPiece prefix(line, offset + 1, line.length() - offset - 1); - StringPieceTrimWhitespace(&package); - StringPieceTrimWhitespace(&prefix); + StringPiece package = line.substr(0, offset); + StringPiece prefix = line.substr(offset + 1); + TrimWhitespace(&package); + TrimWhitespace(&prefix); // Don't really worry about error checking the package/prefix for // being valid. Assume the file is validated when it is created/edited. - (*prefix_map_)[package.ToString()] = prefix.ToString(); + (*prefix_map_)[string(package)] = string(prefix); return true; } @@ -1169,6 +1211,15 @@ bool ValidateObjCClassPrefixes(const std::vector& files, } for (int i = 0; i < files.size(); i++) { + bool should_skip = + (std::find(generation_options.expected_prefixes_suppressions.begin(), + generation_options.expected_prefixes_suppressions.end(), + files[i]->name()) + != generation_options.expected_prefixes_suppressions.end()); + if (should_skip) { + continue; + } + bool is_valid = ValidateObjCClassPrefix(files[i], generation_options.expected_prefixes_path, @@ -1422,7 +1473,7 @@ class Parser { bool Parser::ParseChunk(StringPiece chunk) { if (!leftover_.empty()) { - chunk.AppendToString(&leftover_); + leftover_ += string(chunk); p_ = StringPiece(leftover_); } else { p_ = chunk; @@ -1431,7 +1482,7 @@ bool Parser::ParseChunk(StringPiece chunk) { if (p_.empty()) { leftover_.clear(); } else { - leftover_ = p_.ToString(); + leftover_ = string(p_); } return result; } @@ -1454,7 +1505,7 @@ bool Parser::ParseLoop() { while (ReadLine(&p_, &line)) { ++line_; RemoveComment(&line); - StringPieceTrimWhitespace(&line); + TrimWhitespace(&line); if (line.size() == 0) { continue; // Blank line. } @@ -1496,7 +1547,7 @@ bool ParseSimpleFile( if (!parser.ParseChunk(StringPiece(static_cast(buf), buf_len))) { *out_error = string("error: ") + path + - " Line " + SimpleItoa(parser.last_line()) + ", " + parser.error_str(); + " Line " + StrCat(parser.last_line()) + ", " + parser.error_str(); return false; } } @@ -1641,12 +1692,12 @@ bool ImportWriter::ProtoFrameworkCollector::ConsumeLine( if (offset == StringPiece::npos) { *out_error = string("Framework/proto file mapping line without colon sign: '") + - line.ToString() + "'."; + string(line) + "'."; return false; } - StringPiece framework_name(line, 0, offset); - StringPiece proto_file_list(line, offset + 1, line.length() - offset - 1); - StringPieceTrimWhitespace(&framework_name); + StringPiece framework_name = line.substr(0, offset); + StringPiece proto_file_list = line.substr(offset + 1); + TrimWhitespace(&framework_name); int start = 0; while (start < proto_file_list.length()) { @@ -1655,25 +1706,27 @@ bool ImportWriter::ProtoFrameworkCollector::ConsumeLine( offset = proto_file_list.length(); } - StringPiece proto_file(proto_file_list, start, offset - start); - StringPieceTrimWhitespace(&proto_file); + StringPiece proto_file = proto_file_list.substr(start, offset - start); + TrimWhitespace(&proto_file); if (proto_file.size() != 0) { std::map::iterator existing_entry = - map_->find(proto_file.ToString()); + map_->find(string(proto_file)); if (existing_entry != map_->end()) { - std::cerr << "warning: duplicate proto file reference, replacing framework entry for '" - << proto_file.ToString() << "' with '" << framework_name.ToString() - << "' (was '" << existing_entry->second << "')." << std::endl; + std::cerr << "warning: duplicate proto file reference, replacing " + "framework entry for '" + << string(proto_file) << "' with '" << string(framework_name) + << "' (was '" << existing_entry->second << "')." << std::endl; std::cerr.flush(); } if (proto_file.find(' ') != StringPiece::npos) { - std::cerr << "note: framework mapping file had a proto file with a space in, hopefully that isn't a missing comma: '" - << proto_file.ToString() << "'" << std::endl; + std::cerr << "note: framework mapping file had a proto file with a " + "space in, hopefully that isn't a missing comma: '" + << string(proto_file) << "'" << std::endl; std::cerr.flush(); } - (*map_)[proto_file.ToString()] = framework_name.ToString(); + (*map_)[string(proto_file)] = string(framework_name); } start = offset + 1; diff --git a/src/google/protobuf/compiler/objectivec/objectivec_helpers.h b/src/google/protobuf/compiler/objectivec/objectivec_helpers.h index 8999aa595e..98ec7db8fb 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_helpers.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_helpers.h @@ -39,6 +39,8 @@ #include #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -48,71 +50,73 @@ namespace objectivec { struct Options { Options(); string expected_prefixes_path; + std::vector expected_prefixes_suppressions; string generate_for_named_framework; string named_framework_to_proto_path_mappings_path; }; // Escape C++ trigraphs by escaping question marks to "\?". -string LIBPROTOC_EXPORT EscapeTrigraphs(const string& to_escape); +string PROTOC_EXPORT EscapeTrigraphs(const string& to_escape); // Strips ".proto" or ".protodevel" from the end of a filename. -string LIBPROTOC_EXPORT StripProto(const string& filename); +string PROTOC_EXPORT StripProto(const string& filename); // Remove white space from either end of a StringPiece. -void LIBPROTOC_EXPORT StringPieceTrimWhitespace(StringPiece* input); +void PROTOC_EXPORT TrimWhitespace(StringPiece* input); // Returns true if the name requires a ns_returns_not_retained attribute applied // to it. -bool LIBPROTOC_EXPORT IsRetainedName(const string& name); +bool PROTOC_EXPORT IsRetainedName(const string& name); // Returns true if the name starts with "init" and will need to have special // handling under ARC. -bool LIBPROTOC_EXPORT IsInitName(const string& name); +bool PROTOC_EXPORT IsInitName(const string& name); // Gets the objc_class_prefix. -string LIBPROTOC_EXPORT FileClassPrefix(const FileDescriptor* file); +string PROTOC_EXPORT FileClassPrefix(const FileDescriptor* file); // Gets the path of the file we're going to generate (sans the .pb.h // extension). The path will be dependent on the objectivec package // declared in the proto package. -string LIBPROTOC_EXPORT FilePath(const FileDescriptor* file); +string PROTOC_EXPORT FilePath(const FileDescriptor* file); // Just like FilePath(), but without the directory part. -string LIBPROTOC_EXPORT FilePathBasename(const FileDescriptor* file); +string PROTOC_EXPORT FilePathBasename(const FileDescriptor* file); // Gets the name of the root class we'll generate in the file. This class // is not meant for external consumption, but instead contains helpers that // the rest of the classes need -string LIBPROTOC_EXPORT FileClassName(const FileDescriptor* file); +string PROTOC_EXPORT FileClassName(const FileDescriptor* file); // These return the fully-qualified class name corresponding to the given // descriptor. -string LIBPROTOC_EXPORT ClassName(const Descriptor* descriptor); -string LIBPROTOC_EXPORT ClassName(const Descriptor* descriptor, string* out_suffix_added); -string LIBPROTOC_EXPORT EnumName(const EnumDescriptor* descriptor); +string PROTOC_EXPORT ClassName(const Descriptor* descriptor); +string PROTOC_EXPORT ClassName(const Descriptor* descriptor, + string* out_suffix_added); +string PROTOC_EXPORT EnumName(const EnumDescriptor* descriptor); // Returns the fully-qualified name of the enum value corresponding to the // the descriptor. -string LIBPROTOC_EXPORT EnumValueName(const EnumValueDescriptor* descriptor); +string PROTOC_EXPORT EnumValueName(const EnumValueDescriptor* descriptor); // Returns the name of the enum value corresponding to the descriptor. -string LIBPROTOC_EXPORT EnumValueShortName(const EnumValueDescriptor* descriptor); +string PROTOC_EXPORT EnumValueShortName(const EnumValueDescriptor* descriptor); // Reverse what an enum does. -string LIBPROTOC_EXPORT UnCamelCaseEnumShortName(const string& name); +string PROTOC_EXPORT UnCamelCaseEnumShortName(const string& name); // Returns the name to use for the extension (used as the method off the file's // Root class). -string LIBPROTOC_EXPORT ExtensionMethodName(const FieldDescriptor* descriptor); +string PROTOC_EXPORT ExtensionMethodName(const FieldDescriptor* descriptor); // Returns the transformed field name. -string LIBPROTOC_EXPORT FieldName(const FieldDescriptor* field); -string LIBPROTOC_EXPORT FieldNameCapitalized(const FieldDescriptor* field); +string PROTOC_EXPORT FieldName(const FieldDescriptor* field); +string PROTOC_EXPORT FieldNameCapitalized(const FieldDescriptor* field); // Returns the transformed oneof name. -string LIBPROTOC_EXPORT OneofEnumName(const OneofDescriptor* descriptor); -string LIBPROTOC_EXPORT OneofName(const OneofDescriptor* descriptor); -string LIBPROTOC_EXPORT OneofNameCapitalized(const OneofDescriptor* descriptor); +string PROTOC_EXPORT OneofEnumName(const OneofDescriptor* descriptor); +string PROTOC_EXPORT OneofName(const OneofDescriptor* descriptor); +string PROTOC_EXPORT OneofNameCapitalized(const OneofDescriptor* descriptor); inline bool HasFieldPresence(const FileDescriptor* file) { return file->syntax() != FileDescriptor::SYNTAX_PROTO3; @@ -127,7 +131,8 @@ inline bool IsMapEntryMessage(const Descriptor* descriptor) { } // Reverse of the above. -string LIBPROTOC_EXPORT UnCamelCaseFieldName(const string& name, const FieldDescriptor* field); +string PROTOC_EXPORT UnCamelCaseFieldName(const string& name, + const FieldDescriptor* field); enum ObjectiveCType { OBJECTIVECTYPE_INT32, @@ -158,11 +163,22 @@ string GetOptionalDeprecatedAttribute( // The file is only passed when checking Messages & Enums, so those types // get tagged. At the moment, it doesn't seem to make sense to tag every // field or enum value with when the file is deprecated. + bool isFileLevelDeprecation = false; if (!isDeprecated && file) { - isDeprecated = file->options().deprecated(); + isFileLevelDeprecation = file->options().deprecated(); + isDeprecated = isFileLevelDeprecation; } if (isDeprecated) { - string result = "DEPRECATED_ATTRIBUTE"; + string message; + const FileDescriptor* sourceFile = descriptor->file(); + if (isFileLevelDeprecation) { + message = sourceFile->name() + " is deprecated."; + } else { + message = descriptor->full_name() + " is deprecated (see " + + sourceFile->name() + ")."; + } + + string result = string("GPB_DEPRECATED_MSG(\"") + message + "\")"; if (preSpace) { result.insert(0, " "); } @@ -175,52 +191,59 @@ string GetOptionalDeprecatedAttribute( } } -string LIBPROTOC_EXPORT GetCapitalizedType(const FieldDescriptor* field); +string PROTOC_EXPORT GetCapitalizedType(const FieldDescriptor* field); -ObjectiveCType LIBPROTOC_EXPORT GetObjectiveCType(FieldDescriptor::Type field_type); +ObjectiveCType PROTOC_EXPORT +GetObjectiveCType(FieldDescriptor::Type field_type); inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) { return GetObjectiveCType(field->type()); } -bool LIBPROTOC_EXPORT IsPrimitiveType(const FieldDescriptor* field); -bool LIBPROTOC_EXPORT IsReferenceType(const FieldDescriptor* field); +bool PROTOC_EXPORT IsPrimitiveType(const FieldDescriptor* field); +bool PROTOC_EXPORT IsReferenceType(const FieldDescriptor* field); -string LIBPROTOC_EXPORT GPBGenericValueFieldName(const FieldDescriptor* field); -string LIBPROTOC_EXPORT DefaultValue(const FieldDescriptor* field); -bool LIBPROTOC_EXPORT HasNonZeroDefaultValue(const FieldDescriptor* field); +string PROTOC_EXPORT GPBGenericValueFieldName(const FieldDescriptor* field); +string PROTOC_EXPORT DefaultValue(const FieldDescriptor* field); +bool PROTOC_EXPORT HasNonZeroDefaultValue(const FieldDescriptor* field); -string LIBPROTOC_EXPORT BuildFlagsString(const FlagType type, const std::vector& strings); +string PROTOC_EXPORT BuildFlagsString(const FlagType type, + const std::vector& strings); // Builds HeaderDoc/appledoc style comments out of the comments in the .proto // file. -string LIBPROTOC_EXPORT BuildCommentsString(const SourceLocation& location, - bool prefer_single_line); +string PROTOC_EXPORT BuildCommentsString(const SourceLocation& location, + bool prefer_single_line); // The name the commonly used by the library when built as a framework. // This lines up to the name used in the CocoaPod. -extern LIBPROTOC_EXPORT const char* const ProtobufLibraryFrameworkName; +extern PROTOC_EXPORT const char* const ProtobufLibraryFrameworkName; // Returns the CPP symbol name to use as the gate for framework style imports // for the given framework name to use. -string LIBPROTOC_EXPORT ProtobufFrameworkImportSymbol(const string& framework_name); +string PROTOC_EXPORT +ProtobufFrameworkImportSymbol(const string& framework_name); // Checks if the file is one of the proto's bundled with the library. -bool LIBPROTOC_EXPORT IsProtobufLibraryBundledProtoFile(const FileDescriptor* file); +bool PROTOC_EXPORT +IsProtobufLibraryBundledProtoFile(const FileDescriptor* file); // Checks the prefix for the given files and outputs any warnings as needed. If // there are flat out errors, then out_error is filled in with the first error // and the result is false. -bool LIBPROTOC_EXPORT ValidateObjCClassPrefixes(const std::vector& files, - const Options& generation_options, - string* out_error); +bool PROTOC_EXPORT +ValidateObjCClassPrefixes(const std::vector& files, + const Options& generation_options, string* out_error); // Generate decode data needed for ObjC's GPBDecodeTextFormatName() to transform // the input into the expected output. -class LIBPROTOC_EXPORT TextFormatDecodeData { +class PROTOC_EXPORT TextFormatDecodeData { public: TextFormatDecodeData(); ~TextFormatDecodeData(); + TextFormatDecodeData(const TextFormatDecodeData&) = delete; + TextFormatDecodeData& operator=(const TextFormatDecodeData&) = delete; + void AddString(int32 key, const string& input_for_decode, const string& desired_output); size_t num_entries() const { return entries_.size(); } @@ -230,27 +253,25 @@ class LIBPROTOC_EXPORT TextFormatDecodeData { const string& desired_output); private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormatDecodeData); - typedef std::pair DataEntry; std::vector entries_; }; // Helper for parsing simple files. -class LIBPROTOC_EXPORT LineConsumer { +class PROTOC_EXPORT LineConsumer { public: LineConsumer(); virtual ~LineConsumer(); virtual bool ConsumeLine(const StringPiece& line, string* out_error) = 0; }; -bool LIBPROTOC_EXPORT ParseSimpleFile( - const string& path, LineConsumer* line_consumer, string* out_error); - +bool PROTOC_EXPORT ParseSimpleFile(const string& path, + LineConsumer* line_consumer, + string* out_error); // Helper class for parsing framework import mappings and generating // import statements. -class LIBPROTOC_EXPORT ImportWriter { +class PROTOC_EXPORT ImportWriter { public: ImportWriter(const string& generate_for_named_framework, const string& named_framework_to_proto_path_mappings_path, @@ -290,4 +311,7 @@ class LIBPROTOC_EXPORT ImportWriter { } // namespace compiler } // namespace protobuf } // namespace google + +#include + #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__ diff --git a/src/google/protobuf/compiler/objectivec/objectivec_map_field.cc b/src/google/protobuf/compiler/objectivec/objectivec_map_field.cc index bcaf570900..6abad8bd00 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_map_field.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_map_field.cc @@ -32,11 +32,8 @@ #include #include -#include #include #include -#include -#include namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/objectivec/objectivec_map_field.h b/src/google/protobuf/compiler/objectivec/objectivec_map_field.h index dc7beacf2d..c3085019ee 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_map_field.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_map_field.h @@ -47,6 +47,9 @@ class MapFieldGenerator : public RepeatedFieldGenerator { public: virtual void FinishInitialization(void); + MapFieldGenerator(const MapFieldGenerator&) = delete; + MapFieldGenerator& operator=(const MapFieldGenerator&) = delete; + protected: MapFieldGenerator(const FieldDescriptor* descriptor, const Options& options); virtual ~MapFieldGenerator(); @@ -55,8 +58,6 @@ class MapFieldGenerator : public RepeatedFieldGenerator { private: std::unique_ptr value_field_generator_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapFieldGenerator); }; } // namespace objectivec diff --git a/src/google/protobuf/compiler/objectivec/objectivec_message.cc b/src/google/protobuf/compiler/objectivec/objectivec_message.cc index 838888549c..6731e37a77 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_message.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_message.cc @@ -32,7 +32,6 @@ #include #include -#include #include #include #include @@ -43,7 +42,7 @@ #include #include #include -#include +#include #include namespace google { @@ -185,18 +184,18 @@ MessageGenerator::MessageGenerator(const string& root_classname, GetOptionalDeprecatedAttribute(descriptor, descriptor->file(), false, true)) { for (int i = 0; i < descriptor_->extension_count(); i++) { - extension_generators_.push_back( + extension_generators_.emplace_back( new ExtensionGenerator(class_name_, descriptor_->extension(i))); } for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { OneofGenerator* generator = new OneofGenerator(descriptor_->oneof_decl(i)); - oneof_generators_.push_back(generator); + oneof_generators_.emplace_back(generator); } for (int i = 0; i < descriptor_->enum_type_count(); i++) { EnumGenerator* generator = new EnumGenerator(descriptor_->enum_type(i)); - enum_generators_.push_back(generator); + enum_generators_.emplace_back(generator); } for (int i = 0; i < descriptor_->nested_type_count(); i++) { @@ -204,32 +203,20 @@ MessageGenerator::MessageGenerator(const string& root_classname, new MessageGenerator(root_classname_, descriptor_->nested_type(i), options); - nested_message_generators_.push_back(generator); + nested_message_generators_.emplace_back(generator); } } -MessageGenerator::~MessageGenerator() { - STLDeleteContainerPointers(extension_generators_.begin(), - extension_generators_.end()); - STLDeleteContainerPointers(enum_generators_.begin(), enum_generators_.end()); - STLDeleteContainerPointers(nested_message_generators_.begin(), - nested_message_generators_.end()); - STLDeleteContainerPointers(oneof_generators_.begin(), - oneof_generators_.end()); -} +MessageGenerator::~MessageGenerator() {} void MessageGenerator::GenerateStaticVariablesInitialization( io::Printer* printer) { - for (std::vector::iterator iter = - extension_generators_.begin(); - iter != extension_generators_.end(); ++iter) { - (*iter)->GenerateStaticVariablesInitialization(printer); + for (const auto& generator : extension_generators_) { + generator->GenerateStaticVariablesInitialization(printer); } - for (std::vector::iterator iter = - nested_message_generators_.begin(); - iter != nested_message_generators_.end(); ++iter) { - (*iter)->GenerateStaticVariablesInitialization(printer); + for (const auto& generator : nested_message_generators_) { + generator->GenerateStaticVariablesInitialization(printer); } } @@ -242,10 +229,8 @@ void MessageGenerator::DetermineForwardDeclarations(std::set* fwd_decls) } } - for (std::vector::iterator iter = - nested_message_generators_.begin(); - iter != nested_message_generators_.end(); ++iter) { - (*iter)->DetermineForwardDeclarations(fwd_decls); + for (const auto& generator : nested_message_generators_) { + generator->DetermineForwardDeclarations(fwd_decls); } } @@ -254,10 +239,8 @@ bool MessageGenerator::IncludesOneOfDefinition() const { return true; } - for (std::vector::const_iterator iter = - nested_message_generators_.begin(); - iter != nested_message_generators_.end(); ++iter) { - if ((*iter)->IncludesOneOfDefinition()) { + for (const auto& generator : nested_message_generators_) { + if (generator->IncludesOneOfDefinition()) { return true; } } @@ -266,40 +249,31 @@ bool MessageGenerator::IncludesOneOfDefinition() const { } void MessageGenerator::GenerateEnumHeader(io::Printer* printer) { - for (std::vector::iterator iter = enum_generators_.begin(); - iter != enum_generators_.end(); ++iter) { - (*iter)->GenerateHeader(printer); + for (const auto& generator : enum_generators_) { + generator->GenerateHeader(printer); } - for (std::vector::iterator iter = - nested_message_generators_.begin(); - iter != nested_message_generators_.end(); ++iter) { - (*iter)->GenerateEnumHeader(printer); + for (const auto& generator : nested_message_generators_) { + generator->GenerateEnumHeader(printer); } } void MessageGenerator::GenerateExtensionRegistrationSource( io::Printer* printer) { - for (std::vector::iterator iter = - extension_generators_.begin(); - iter != extension_generators_.end(); ++iter) { - (*iter)->GenerateRegistrationSource(printer); + for (const auto& generator : extension_generators_) { + generator->GenerateRegistrationSource(printer); } - for (std::vector::iterator iter = - nested_message_generators_.begin(); - iter != nested_message_generators_.end(); ++iter) { - (*iter)->GenerateExtensionRegistrationSource(printer); + for (const auto& generator : nested_message_generators_) { + generator->GenerateExtensionRegistrationSource(printer); } } void MessageGenerator::GenerateMessageHeader(io::Printer* printer) { // This a a map entry message, just recurse and do nothing directly. if (IsMapEntryMessage(descriptor_)) { - for (std::vector::iterator iter = - nested_message_generators_.begin(); - iter != nested_message_generators_.end(); ++iter) { - (*iter)->GenerateMessageHeader(printer); + for (const auto& generator : nested_message_generators_) { + generator->GenerateMessageHeader(printer); } return; } @@ -326,9 +300,8 @@ void MessageGenerator::GenerateMessageHeader(io::Printer* printer) { printer->Print("};\n\n"); } - for (std::vector::iterator iter = oneof_generators_.begin(); - iter != oneof_generators_.end(); ++iter) { - (*iter)->GenerateCaseEnum(printer); + for (const auto& generator : oneof_generators_) { + generator->GenerateCaseEnum(printer); } string message_comments; @@ -367,9 +340,8 @@ void MessageGenerator::GenerateMessageHeader(io::Printer* printer) { } if (!oneof_generators_.empty()) { - for (std::vector::iterator iter = oneof_generators_.begin(); - iter != oneof_generators_.end(); ++iter) { - (*iter)->GenerateClearFunctionDeclaration(printer); + for (const auto& generator : oneof_generators_) { + generator->GenerateClearFunctionDeclaration(printer); } printer->Print("\n"); } @@ -377,18 +349,14 @@ void MessageGenerator::GenerateMessageHeader(io::Printer* printer) { if (descriptor_->extension_count() > 0) { printer->Print("@interface $classname$ (DynamicMethods)\n\n", "classname", class_name_); - for (std::vector::iterator iter = - extension_generators_.begin(); - iter != extension_generators_.end(); ++iter) { - (*iter)->GenerateMembersHeader(printer); + for (const auto& generator : extension_generators_) { + generator->GenerateMembersHeader(printer); } printer->Print("@end\n\n"); } - for (std::vector::iterator iter = - nested_message_generators_.begin(); - iter != nested_message_generators_.end(); ++iter) { - (*iter)->GenerateMessageHeader(printer); + for (const auto& generator : nested_message_generators_) { + generator->GenerateMessageHeader(printer); } } @@ -410,9 +378,8 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { printer->Print("@implementation $classname$\n\n", "classname", class_name_); - for (std::vector::iterator iter = oneof_generators_.begin(); - iter != oneof_generators_.end(); ++iter) { - (*iter)->GeneratePropertyImplementation(printer); + for (const auto& generator : oneof_generators_) { + generator->GeneratePropertyImplementation(printer); } for (int i = 0; i < descriptor_->field_count(); i++) { @@ -448,9 +415,8 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { sizeof_has_storage = 1; } // Tell all the fields the oneof base. - for (std::vector::iterator iter = oneof_generators_.begin(); - iter != oneof_generators_.end(); ++iter) { - (*iter)->SetOneofIndexBase(sizeof_has_storage); + for (const auto& generator : oneof_generators_) { + generator->SetOneofIndexBase(sizeof_has_storage); } field_generators_.SetOneofIndexBase(sizeof_has_storage); // sizeof_has_storage needs enough bits for the single fields that aren't in @@ -462,7 +428,7 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { "typedef struct $classname$__storage_ {\n" " uint32_t _has_storage_[$sizeof_has_storage$];\n", "classname", class_name_, - "sizeof_has_storage", SimpleItoa(sizeof_has_storage)); + "sizeof_has_storage", StrCat(sizeof_has_storage)); printer->Indent(); for (int i = 0; i < descriptor_->field_count(); i++) { @@ -548,11 +514,9 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { if (oneof_generators_.size() != 0) { printer->Print( " static const char *oneofs[] = {\n"); - for (std::vector::iterator iter = oneof_generators_.begin(); - iter != oneof_generators_.end(); ++iter) { - printer->Print( - " \"$name$\",\n", - "name", (*iter)->DescriptorName()); + for (const auto& generator : oneof_generators_) { + printer->Print(" \"$name$\",\n", "name", + generator->DescriptorName()); } printer->Print( " };\n" @@ -583,8 +547,8 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { " static const GPBExtensionRange ranges[] = {\n"); for (int i = 0; i < sorted_extensions.size(); i++) { printer->Print(" { .start = $start$, .end = $end$ },\n", - "start", SimpleItoa(sorted_extensions[i]->start), - "end", SimpleItoa(sorted_extensions[i]->end)); + "start", StrCat(sorted_extensions[i]->start), + "end", StrCat(sorted_extensions[i]->end)); } printer->Print( " };\n" @@ -605,7 +569,9 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { "suffix", suffix_added); } printer->Print( - " NSAssert(descriptor == nil, @\"Startup recursed!\");\n" + " #if defined(DEBUG) && DEBUG\n" + " NSAssert(descriptor == nil, @\"Startup recursed!\");\n" + " #endif // DEBUG\n" " descriptor = localDescriptor;\n" " }\n" " return descriptor;\n" @@ -623,21 +589,17 @@ void MessageGenerator::GenerateSource(io::Printer* printer) { .GenerateCFunctionImplementations(printer); } - for (std::vector::iterator iter = oneof_generators_.begin(); - iter != oneof_generators_.end(); ++iter) { - (*iter)->GenerateClearFunctionImplementation(printer); + for (const auto& generator : oneof_generators_) { + generator->GenerateClearFunctionImplementation(printer); } } - for (std::vector::iterator iter = enum_generators_.begin(); - iter != enum_generators_.end(); ++iter) { - (*iter)->GenerateSource(printer); + for (const auto& generator : enum_generators_) { + generator->GenerateSource(printer); } - for (std::vector::iterator iter = - nested_message_generators_.begin(); - iter != nested_message_generators_.end(); ++iter) { - (*iter)->GenerateSource(printer); + for (const auto& generator : nested_message_generators_) { + generator->GenerateSource(printer); } } diff --git a/src/google/protobuf/compiler/objectivec/objectivec_message.h b/src/google/protobuf/compiler/objectivec/objectivec_message.h index 2de03f12fb..55eda0b8ef 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_message.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_message.h @@ -37,15 +37,11 @@ #include #include #include -#include +#include +#include namespace google { namespace protobuf { - -namespace io { -class Printer; // printer.h -} // namespace io - namespace compiler { namespace objectivec { @@ -59,6 +55,9 @@ class MessageGenerator { const Options& options); ~MessageGenerator(); + MessageGenerator(const MessageGenerator&) = delete; + MessageGenerator& operator=(const MessageGenerator&) = delete; + void GenerateStaticVariablesInitialization(io::Printer* printer); void GenerateEnumHeader(io::Printer* printer); void GenerateMessageHeader(io::Printer* printer); @@ -86,15 +85,15 @@ class MessageGenerator { FieldGeneratorMap field_generators_; const string class_name_; const string deprecated_attribute_; - std::vector extension_generators_; - std::vector enum_generators_; - std::vector nested_message_generators_; - std::vector oneof_generators_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); + std::vector> extension_generators_; + std::vector> enum_generators_; + std::vector> nested_message_generators_; + std::vector> oneof_generators_; }; + } // namespace objectivec } // namespace compiler } // namespace protobuf } // namespace google + #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_MESSAGE_H__ diff --git a/src/google/protobuf/compiler/objectivec/objectivec_message_field.cc b/src/google/protobuf/compiler/objectivec/objectivec_message_field.cc index 699d25b31f..8a0299e7c2 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_message_field.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_message_field.cc @@ -35,7 +35,6 @@ #include #include #include -#include namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/objectivec/objectivec_message_field.h b/src/google/protobuf/compiler/objectivec/objectivec_message_field.h index 50f4b6d4a9..98d457942e 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_message_field.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_message_field.h @@ -47,14 +47,15 @@ class MessageFieldGenerator : public ObjCObjFieldGenerator { protected: MessageFieldGenerator(const FieldDescriptor* descriptor, const Options& options); + + MessageFieldGenerator(const MessageFieldGenerator&) = delete; + MessageFieldGenerator& operator=(const MessageFieldGenerator&) = delete; + virtual ~MessageFieldGenerator(); virtual bool WantsHasProperty(void) const; public: virtual void DetermineForwardDeclarations(std::set* fwd_decls) const; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator); }; class RepeatedMessageFieldGenerator : public RepeatedFieldGenerator { @@ -66,11 +67,11 @@ class RepeatedMessageFieldGenerator : public RepeatedFieldGenerator { const Options& options); virtual ~RepeatedMessageFieldGenerator(); + RepeatedMessageFieldGenerator(const RepeatedMessageFieldGenerator&) = delete; + RepeatedMessageFieldGenerator operator=(const RepeatedMessageFieldGenerator&) = delete; + public: virtual void DetermineForwardDeclarations(std::set* fwd_decls) const; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedMessageFieldGenerator); }; } // namespace objectivec diff --git a/src/google/protobuf/compiler/objectivec/objectivec_nsobject_methods.h b/src/google/protobuf/compiler/objectivec/objectivec_nsobject_methods.h new file mode 100644 index 0000000000..163304665c --- /dev/null +++ b/src/google/protobuf/compiler/objectivec/objectivec_nsobject_methods.h @@ -0,0 +1,197 @@ +// NSObject methods +// Autogenerated by method_dump.sh. Do not edit by hand. +// Date: Thu Nov 1 14:12:16 PDT 2018 +// macOS: MacOSX10.14.sdk +// iOS: iPhoneSimulator12.1.sdk + +const char* const kNSObjectMethodsList[] = { + "CAMLType", + "CA_copyRenderValue", + "CA_prepareRenderValue", + "NS_copyCGImage", + "NS_tiledLayerVisibleRect", + "___tryRetain_OA", + "__autorelease_OA", + "__dealloc_zombie", + "__release_OA", + "__retain_OA", + "_accessibilityFinalize", + "_accessibilityIsTableViewDescendant", + "_accessibilityUIElementSpecifier", + "_accessibilityUseConvenienceAPI", + "_allowsDirectEncoding", + "_asScriptTerminologyNameArray", + "_asScriptTerminologyNameString", + "_bindingAdaptor", + "_cfTypeID", + "_copyDescription", + "_destroyObserverList", + "_didEndKeyValueObserving", + "_implicitObservationInfo", + "_internalAccessibilityAttributedHint", + "_internalAccessibilityAttributedLabel", + "_internalAccessibilityAttributedValue", + "_isAXConnector", + "_isAccessibilityContainerSectionCandidate", + "_isAccessibilityContentNavigatorSectionCandidate", + "_isAccessibilityContentSectionCandidate", + "_isAccessibilityTopLevelNavigatorSectionCandidate", + "_isDeallocating", + "_isKVOA", + "_isToManyChangeInformation", + "_ivarDescription", + "_localClassNameForClass", + "_methodDescription", + "_observerStorage", + "_overrideUseFastBlockObservers", + "_propertyDescription", + "_releaseBindingAdaptor", + "_scriptingCount", + "_scriptingCountNonrecursively", + "_scriptingDebugDescription", + "_scriptingExists", + "_scriptingShouldCheckObjectIndexes", + "_shortMethodDescription", + "_shouldSearchChildrenForSection", + "_traitStorageList", + "_tryRetain", + "_ui_descriptionBuilder", + "_uikit_variesByTraitCollections", + "_web_description", + "_webkit_invokeOnMainThread", + "_willBeginKeyValueObserving", + "accessibilityActivate", + "accessibilityActivationPoint", + "accessibilityAllowsOverriddenAttributesWhenIgnored", + "accessibilityAssistiveTechnologyFocusedIdentifiers", + "accessibilityAttributedHint", + "accessibilityAttributedLabel", + "accessibilityAttributedValue", + "accessibilityContainer", + "accessibilityContainerType", + "accessibilityCustomActions", + "accessibilityCustomRotors", + "accessibilityDecrement", + "accessibilityDragSourceDescriptors", + "accessibilityDropPointDescriptors", + "accessibilityElementCount", + "accessibilityElementDidBecomeFocused", + "accessibilityElementDidLoseFocus", + "accessibilityElementIsFocused", + "accessibilityElements", + "accessibilityElementsHidden", + "accessibilityFrame", + "accessibilityHeaderElements", + "accessibilityHint", + "accessibilityIdentification", + "accessibilityIdentifier", + "accessibilityIncrement", + "accessibilityLabel", + "accessibilityLanguage", + "accessibilityLocalizedStringKey", + "accessibilityNavigationStyle", + "accessibilityOverriddenAttributes", + "accessibilityParameterizedAttributeNames", + "accessibilityPath", + "accessibilityPerformEscape", + "accessibilityPerformMagicTap", + "accessibilityPresenterProcessIdentifier", + "accessibilityShouldUseUniqueId", + "accessibilitySupportsNotifications", + "accessibilitySupportsOverriddenAttributes", + "accessibilityTemporaryChildren", + "accessibilityTraits", + "accessibilityValue", + "accessibilityViewIsModal", + "accessibilityVisibleArea", + "allPropertyKeys", + "allowsWeakReference", + "attributeKeys", + "autoContentAccessingProxy", + "autorelease", + "awakeFromNib", + "boolValueSafe", + "bs_encoded", + "bs_isPlistableType", + "bs_secureEncoded", + "cl_json_serializeKey", + "class", + "classCode", + "classDescription", + "classForArchiver", + "classForCoder", + "classForKeyedArchiver", + "classForPortCoder", + "className", + "clearProperties", + "copy", + "dealloc", + "debugDescription", + "defaultAccessibilityTraits", + "description", + "doubleValueSafe", + "entityName", + "exposedBindings", + "finalize", + "finishObserving", + "flushKeyBindings", + "hash", + "init", + "int64ValueSafe", + "isAccessibilityElement", + "isAccessibilityElementByDefault", + "isElementAccessibilityExposedToInterfaceBuilder", + "isFault", + "isNSArray__", + "isNSCFConstantString__", + "isNSData__", + "isNSDate__", + "isNSDictionary__", + "isNSNumber__", + "isNSObject__", + "isNSOrderedSet__", + "isNSSet__", + "isNSString__", + "isNSTimeZone__", + "isNSValue__", + "isProxy", + "mutableCopy", + "nilValueForKey", + "objectSpecifier", + "observationInfo", + "pep_onDetachedThread", + "pep_onMainThread", + "pep_onMainThreadIfNecessary", + "prepareForInterfaceBuilder", + "release", + "releaseOnMainThread", + "retain", + "retainCount", + "retainWeakReference", + "scriptingProperties", + "self", + "shouldGroupAccessibilityChildren", + "storedAccessibilityActivationPoint", + "storedAccessibilityContainerType", + "storedAccessibilityElementsHidden", + "storedAccessibilityFrame", + "storedAccessibilityNavigationStyle", + "storedAccessibilityTraits", + "storedAccessibilityViewIsModal", + "storedIsAccessibilityElement", + "storedShouldGroupAccessibilityChildren", + "stringValueSafe", + "superclass", + "toManyRelationshipKeys", + "toOneRelationshipKeys", + "traitStorageList", + "un_safeBoolValue", + "userInterfaceItemIdentifier", + "utf8ValueSafe", + "valuesForKeysWithDictionary", + "zone", +// Protocol: CAAnimatableValue +// Protocol: CARenderValue +// Protocol: NSObject +// Protocol: ROCKRemoteInvocationInterface +}; diff --git a/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc b/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc index 5531ae249f..5b37c4e945 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc @@ -46,7 +46,7 @@ OneofGenerator::OneofGenerator(const OneofDescriptor* descriptor) variables_["enum_name"] = OneofEnumName(descriptor_); variables_["name"] = OneofName(descriptor_); variables_["capitalized_name"] = OneofNameCapitalized(descriptor_); - variables_["raw_index"] = SimpleItoa(descriptor_->index()); + variables_["raw_index"] = StrCat(descriptor_->index()); const Descriptor* msg_descriptor = descriptor_->containing_type(); variables_["owning_message_class"] = ClassName(msg_descriptor); @@ -65,7 +65,7 @@ OneofGenerator::~OneofGenerator() {} void OneofGenerator::SetOneofIndexBase(int index_base) { int index = descriptor_->index() + index_base; // Flip the sign to mark it as a oneof. - variables_["index"] = SimpleItoa(-index); + variables_["index"] = StrCat(-index); } void OneofGenerator::GenerateCaseEnum(io::Printer* printer) { @@ -84,7 +84,7 @@ void OneofGenerator::GenerateCaseEnum(io::Printer* printer) { "$enum_name$_$field_name$ = $field_number$,\n", "enum_name", enum_name, "field_name", field_name, - "field_number", SimpleItoa(field->number())); + "field_number", StrCat(field->number())); } printer->Outdent(); printer->Print( diff --git a/src/google/protobuf/compiler/objectivec/objectivec_oneof.h b/src/google/protobuf/compiler/objectivec/objectivec_oneof.h index ff353a6c62..852ef02241 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_oneof.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_oneof.h @@ -35,14 +35,9 @@ #include #include #include +#include namespace google { -namespace protobuf { -namespace io { -class Printer; // printer.h -} -} - namespace protobuf { namespace compiler { namespace objectivec { @@ -52,6 +47,9 @@ class OneofGenerator { explicit OneofGenerator(const OneofDescriptor* descriptor); ~OneofGenerator(); + OneofGenerator(const OneofGenerator&) = delete; + OneofGenerator& operator=(const OneofGenerator&) = delete; + void SetOneofIndexBase(int index_base); void GenerateCaseEnum(io::Printer* printer); @@ -68,12 +66,11 @@ class OneofGenerator { private: const OneofDescriptor* descriptor_; std::map variables_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OneofGenerator); }; } // namespace objectivec } // namespace compiler } // namespace protobuf } // namespace google + #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_ONEOF_H__ diff --git a/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc b/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc index aa8ac32430..0511b37dd7 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc @@ -31,14 +31,12 @@ #include #include -#include -#include #include +#include #include -#include -#include #include -#include +#include +#include namespace google { namespace protobuf { @@ -154,7 +152,7 @@ int PrimitiveFieldGenerator::ExtraRuntimeHasBitsNeeded(void) const { void PrimitiveFieldGenerator::SetExtraRuntimeHasBitsBase(int has_base) { if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) { // Set into the offset the has bit to use for the actual value. - variables_["storage_offset_value"] = SimpleItoa(has_base); + variables_["storage_offset_value"] = StrCat(has_base); variables_["storage_offset_comment"] = " // Stored in _has_storage_ to save space."; } diff --git a/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.h b/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.h index 69bb1fddc1..642f2d6323 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.h @@ -49,13 +49,13 @@ class PrimitiveFieldGenerator : public SingleFieldGenerator { const Options& options); virtual ~PrimitiveFieldGenerator(); + PrimitiveFieldGenerator(const PrimitiveFieldGenerator&) = delete; + PrimitiveFieldGenerator& operator=(const PrimitiveFieldGenerator&) = delete; + virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; virtual int ExtraRuntimeHasBitsNeeded(void) const; virtual void SetExtraRuntimeHasBitsBase(int index_base); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator); }; class PrimitiveObjFieldGenerator : public ObjCObjFieldGenerator { @@ -67,8 +67,9 @@ class PrimitiveObjFieldGenerator : public ObjCObjFieldGenerator { const Options& options); virtual ~PrimitiveObjFieldGenerator(); - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveObjFieldGenerator); + PrimitiveObjFieldGenerator(const PrimitiveObjFieldGenerator&) = delete; + PrimitiveObjFieldGenerator& operator=(const PrimitiveObjFieldGenerator&) = + delete; }; class RepeatedPrimitiveFieldGenerator : public RepeatedFieldGenerator { @@ -80,8 +81,10 @@ class RepeatedPrimitiveFieldGenerator : public RepeatedFieldGenerator { const Options& options); virtual ~RepeatedPrimitiveFieldGenerator(); - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPrimitiveFieldGenerator); + RepeatedPrimitiveFieldGenerator(const RepeatedPrimitiveFieldGenerator&) = + delete; + RepeatedPrimitiveFieldGenerator& operator=( + const RepeatedPrimitiveFieldGenerator&) = delete; }; } // namespace objectivec diff --git a/src/google/protobuf/compiler/parser.cc b/src/google/protobuf/compiler/parser.cc index 0cec2939b5..1c0efda18d 100644 --- a/src/google/protobuf/compiler/parser.cc +++ b/src/google/protobuf/compiler/parser.cc @@ -38,6 +38,7 @@ #include #include + #include #include @@ -59,28 +60,28 @@ using internal::WireFormat; namespace { -typedef std::unordered_map TypeNameMap; +typedef std::unordered_map TypeNameMap; TypeNameMap MakeTypeNameTable() { TypeNameMap result; - result["double" ] = FieldDescriptorProto::TYPE_DOUBLE; - result["float" ] = FieldDescriptorProto::TYPE_FLOAT; - result["uint64" ] = FieldDescriptorProto::TYPE_UINT64; - result["fixed64" ] = FieldDescriptorProto::TYPE_FIXED64; - result["fixed32" ] = FieldDescriptorProto::TYPE_FIXED32; - result["bool" ] = FieldDescriptorProto::TYPE_BOOL; - result["string" ] = FieldDescriptorProto::TYPE_STRING; - result["group" ] = FieldDescriptorProto::TYPE_GROUP; - - result["bytes" ] = FieldDescriptorProto::TYPE_BYTES; - result["uint32" ] = FieldDescriptorProto::TYPE_UINT32; + result["double"] = FieldDescriptorProto::TYPE_DOUBLE; + result["float"] = FieldDescriptorProto::TYPE_FLOAT; + result["uint64"] = FieldDescriptorProto::TYPE_UINT64; + result["fixed64"] = FieldDescriptorProto::TYPE_FIXED64; + result["fixed32"] = FieldDescriptorProto::TYPE_FIXED32; + result["bool"] = FieldDescriptorProto::TYPE_BOOL; + result["string"] = FieldDescriptorProto::TYPE_STRING; + result["group"] = FieldDescriptorProto::TYPE_GROUP; + + result["bytes"] = FieldDescriptorProto::TYPE_BYTES; + result["uint32"] = FieldDescriptorProto::TYPE_UINT32; result["sfixed32"] = FieldDescriptorProto::TYPE_SFIXED32; result["sfixed64"] = FieldDescriptorProto::TYPE_SFIXED64; - result["int32" ] = FieldDescriptorProto::TYPE_INT32; - result["int64" ] = FieldDescriptorProto::TYPE_INT64; - result["sint32" ] = FieldDescriptorProto::TYPE_SINT32; - result["sint64" ] = FieldDescriptorProto::TYPE_SINT64; + result["int32"] = FieldDescriptorProto::TYPE_INT32; + result["int64"] = FieldDescriptorProto::TYPE_INT64; + result["sint32"] = FieldDescriptorProto::TYPE_SINT32; + result["sint64"] = FieldDescriptorProto::TYPE_SINT64; return result; } @@ -89,8 +90,8 @@ const TypeNameMap kTypeNames = MakeTypeNameTable(); // Camel-case the field name and append "Entry" for generated map entry name. // e.g. map foo_map => FooMapEntry -string MapEntryName(const string& field_name) { - string result; +std::string MapEntryName(const std::string& field_name) { + std::string result; static const char kSuffix[] = "Entry"; result.reserve(field_name.size() + sizeof(kSuffix)); bool cap_next = true; @@ -113,26 +114,81 @@ string MapEntryName(const string& field_name) { return result; } +bool IsUppercase(char c) { return c >= 'A' && c <= 'Z'; } + +bool IsLowercase(char c) { return c >= 'a' && c <= 'z'; } + +bool IsNumber(char c) { return c >= '0' && c <= '9'; } + +bool IsUpperCamelCase(const string& name) { + if (name.empty()) { + return true; + } + // Name must start with an upper case character. + if (!IsUppercase(name[0])) { + return false; + } + // Must not contains underscore. + for (int i = 1; i < name.length(); i++) { + if (name[i] == '_') { + return false; + } + } + return true; +} + +bool IsUpperUnderscore(const string& name) { + for (int i = 0; i < name.length(); i++) { + const char c = name[i]; + if (!IsUppercase(c) && c != '_' && !IsNumber(c)) { + return false; + } + } + return true; +} + +bool IsLowerUnderscore(const string& name) { + for (int i = 0; i < name.length(); i++) { + const char c = name[i]; + if (!IsLowercase(c) && c != '_' && !IsNumber(c)) { + return false; + } + } + return true; +} + +bool IsNumberFollowUnderscore(const string& name) { + for (int i = 1; i < name.length(); i++) { + const char c = name[i]; + if (IsNumber(c) && name[i - 1] == '_') { + return true; + } + } + return false; +} + } // anonymous namespace // Makes code slightly more readable. The meaning of "DO(foo)" is // "Execute foo and fail if it fails.", where failure is indicated by // returning false. -#define DO(STATEMENT) if (STATEMENT) {} else return false +#define DO(STATEMENT) \ + if (STATEMENT) { \ + } else \ + return false // =================================================================== Parser::Parser() - : input_(NULL), - error_collector_(NULL), - source_location_table_(NULL), - had_errors_(false), - require_syntax_identifier_(false), - stop_after_syntax_identifier_(false) { + : input_(NULL), + error_collector_(NULL), + source_location_table_(NULL), + had_errors_(false), + require_syntax_identifier_(false), + stop_after_syntax_identifier_(false) { } -Parser::~Parser() { -} +Parser::~Parser() {} // =================================================================== @@ -144,9 +200,7 @@ inline bool Parser::LookingAtType(io::Tokenizer::TokenType token_type) { return input_->current().type == token_type; } -inline bool Parser::AtEnd() { - return LookingAtType(io::Tokenizer::TYPE_END); -} +inline bool Parser::AtEnd() { return LookingAtType(io::Tokenizer::TYPE_END); } bool Parser::TryConsume(const char* text) { if (LookingAt(text)) { @@ -175,7 +229,7 @@ bool Parser::Consume(const char* text) { } } -bool Parser::ConsumeIdentifier(string* output, const char* error) { +bool Parser::ConsumeIdentifier(std::string* output, const char* error) { if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { *output = input_->current().text; input_->Next(); @@ -189,8 +243,8 @@ bool Parser::ConsumeIdentifier(string* output, const char* error) { bool Parser::ConsumeInteger(int* output, const char* error) { if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) { uint64 value = 0; - if (!io::Tokenizer::ParseInteger(input_->current().text, - kint32max, &value)) { + if (!io::Tokenizer::ParseInteger(input_->current().text, kint32max, + &value)) { AddError("Integer out of range."); // We still return true because we did, in fact, parse an integer. } @@ -242,8 +296,8 @@ bool Parser::ConsumeNumber(double* output, const char* error) { } else if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) { // Also accept integers. uint64 value = 0; - if (!io::Tokenizer::ParseInteger(input_->current().text, - kuint64max, &value)) { + if (!io::Tokenizer::ParseInteger(input_->current().text, kuint64max, + &value)) { AddError("Integer out of range."); // We still return true because we did, in fact, parse a number. } @@ -264,7 +318,7 @@ bool Parser::ConsumeNumber(double* output, const char* error) { } } -bool Parser::ConsumeString(string* output, const char* error) { +bool Parser::ConsumeString(std::string* output, const char* error) { if (LookingAtType(io::Tokenizer::TYPE_STRING)) { io::Tokenizer::ParseString(input_->current().text, output); input_->Next(); @@ -280,11 +334,11 @@ bool Parser::ConsumeString(string* output, const char* error) { } } -bool Parser::TryConsumeEndOfDeclaration( - const char* text, const LocationRecorder* location) { +bool Parser::TryConsumeEndOfDeclaration(const char* text, + const LocationRecorder* location) { if (LookingAt(text)) { - string leading, trailing; - std::vector detached; + std::string leading, trailing; + std::vector detached; input_->NextWithComments(&trailing, &detached, &leading); // Save the leading comments for next time, and recall the leading comments @@ -311,8 +365,8 @@ bool Parser::TryConsumeEndOfDeclaration( } } -bool Parser::ConsumeEndOfDeclaration( - const char* text, const LocationRecorder* location) { +bool Parser::ConsumeEndOfDeclaration(const char* text, + const LocationRecorder* location) { if (TryConsumeEndOfDeclaration(text, location)) { return true; } else { @@ -323,17 +377,24 @@ bool Parser::ConsumeEndOfDeclaration( // ------------------------------------------------------------------- -void Parser::AddError(int line, int column, const string& error) { +void Parser::AddError(int line, int column, const std::string& error) { if (error_collector_ != NULL) { error_collector_->AddError(line, column, error); } had_errors_ = true; } -void Parser::AddError(const string& error) { +void Parser::AddError(const std::string& error) { AddError(input_->current().line, input_->current().column, error); } +void Parser::AddWarning(const string& warning) { + if (error_collector_ != nullptr) { + error_collector_->AddWarning(input_->current().line, + input_->current().column, warning); + } +} + // ------------------------------------------------------------------- Parser::LocationRecorder::LocationRecorder(Parser* parser) @@ -407,7 +468,8 @@ void Parser::LocationRecorder::EndAt(const io::Tokenizer::Token& token) { location_->add_span(token.end_column); } -void Parser::LocationRecorder::RecordLegacyLocation(const Message* descriptor, +void Parser::LocationRecorder::RecordLegacyLocation( + const Message* descriptor, DescriptorPool::ErrorCollector::ErrorLocation location) { if (parser_->source_location_table_ != NULL) { parser_->source_location_table_->Add( @@ -415,13 +477,21 @@ void Parser::LocationRecorder::RecordLegacyLocation(const Message* descriptor, } } +void Parser::LocationRecorder::RecordLegacyImportLocation( + const Message* descriptor, const string& name) { + if (parser_->source_location_table_ != nullptr) { + parser_->source_location_table_->AddImport( + descriptor, name, location_->span(0), location_->span(1)); + } +} + int Parser::LocationRecorder::CurrentPathSize() const { return location_->path_size(); } void Parser::LocationRecorder::AttachComments( - string* leading, string* trailing, - std::vector* detached_comments) const { + std::string* leading, std::string* trailing, + std::vector* detached_comments) const { GOOGLE_CHECK(!location_->has_leading_comments()); GOOGLE_CHECK(!location_->has_trailing_comments()); @@ -432,8 +502,7 @@ void Parser::LocationRecorder::AttachComments( location_->mutable_trailing_comments()->swap(*trailing); } for (int i = 0; i < detached_comments->size(); ++i) { - location_->add_leading_detached_comments()->swap( - (*detached_comments)[i]); + location_->add_leading_detached_comments()->swap((*detached_comments)[i]); } detached_comments->clear(); } @@ -495,7 +564,7 @@ bool Parser::ValidateEnum(const EnumDescriptorProto* proto) { } if (has_allow_alias && !allow_alias) { - string error = + std::string error = "\"" + proto->name() + "\" declares 'option allow_alias = false;' which has no effect. " "Please remove the declaration."; @@ -507,7 +576,7 @@ bool Parser::ValidateEnum(const EnumDescriptorProto* proto) { std::set used_values; bool has_duplicates = false; for (int i = 0; i < proto->value_size(); ++i) { - const EnumValueDescriptorProto enum_value = proto->value(i); + const EnumValueDescriptorProto& enum_value = proto->value(i); if (used_values.find(enum_value.number()) != used_values.end()) { has_duplicates = true; break; @@ -516,7 +585,7 @@ bool Parser::ValidateEnum(const EnumDescriptorProto* proto) { } } if (allow_alias && !has_duplicates) { - string error = + std::string error = "\"" + proto->name() + "\" declares support for enum aliases but no enum values share field " "numbers. Please remove the unnecessary 'option allow_alias = true;' " @@ -527,6 +596,19 @@ bool Parser::ValidateEnum(const EnumDescriptorProto* proto) { return false; } + // Enforce that enum constants must be UPPER_CASE except in case of + // enum_alias. + if (!allow_alias) { + for (const auto& enum_value : proto->value()) { + if (!IsUpperUnderscore(enum_value.name())) { + AddWarning( + "Enum constant should be in UPPER_CASE. Found: " + + enum_value.name() + + ". See https://developers.google.com/protocol-buffers/docs/style"); + } + } + } + return true; } @@ -550,6 +632,8 @@ bool Parser::Parse(io::Tokenizer* input, FileDescriptorProto* file) { { LocationRecorder root_location(this); + root_location.RecordLegacyLocation(file, + DescriptorPool::ErrorCollector::OTHER); if (require_syntax_identifier_ || LookingAt("syntax")) { if (!ParseSyntaxIdentifier(root_location)) { @@ -560,8 +644,8 @@ bool Parser::Parse(io::Tokenizer* input, FileDescriptorProto* file) { // Store the syntax into the file. if (file != NULL) file->set_syntax(syntax_identifier_); } else if (!stop_after_syntax_identifier_) { - GOOGLE_LOG(WARNING) << "No syntax specified for the proto file: " - << file->name() << ". Please use 'syntax = \"proto2\";' " + GOOGLE_LOG(WARNING) << "No syntax specified for the proto file: " << file->name() + << ". Please use 'syntax = \"proto2\";' " << "or 'syntax = \"proto3\";' to specify a syntax " << "version. (Defaulted to proto2 syntax.)"; syntax_identifier_ = "proto2"; @@ -600,7 +684,7 @@ bool Parser::ParseSyntaxIdentifier(const LocationRecorder& parent) { "File must begin with a syntax statement, e.g. 'syntax = \"proto2\";'.")); DO(Consume("=")); io::Tokenizer::Token syntax_token = input_->current(); - string syntax; + std::string syntax; DO(ConsumeString(&syntax, "Expected syntax identifier.")); DO(ConsumeEndOfDeclaration(";", &syntax_location)); @@ -609,8 +693,9 @@ bool Parser::ParseSyntaxIdentifier(const LocationRecorder& parent) { if (syntax != "proto2" && syntax != "proto3" && !stop_after_syntax_identifier_) { AddError(syntax_token.line, syntax_token.column, - "Unrecognized syntax identifier \"" + syntax + "\". This parser " - "only recognizes \"proto2\" and \"proto3\"."); + "Unrecognized syntax identifier \"" + syntax + + "\". This parser " + "only recognizes \"proto2\" and \"proto3\"."); return false; } @@ -624,34 +709,34 @@ bool Parser::ParseTopLevelStatement(FileDescriptorProto* file, return true; } else if (LookingAt("message")) { LocationRecorder location(root_location, - FileDescriptorProto::kMessageTypeFieldNumber, file->message_type_size()); + FileDescriptorProto::kMessageTypeFieldNumber, + file->message_type_size()); return ParseMessageDefinition(file->add_message_type(), location, file); } else if (LookingAt("enum")) { LocationRecorder location(root_location, - FileDescriptorProto::kEnumTypeFieldNumber, file->enum_type_size()); + FileDescriptorProto::kEnumTypeFieldNumber, + file->enum_type_size()); return ParseEnumDefinition(file->add_enum_type(), location, file); } else if (LookingAt("service")) { LocationRecorder location(root_location, - FileDescriptorProto::kServiceFieldNumber, file->service_size()); + FileDescriptorProto::kServiceFieldNumber, + file->service_size()); return ParseServiceDefinition(file->add_service(), location, file); } else if (LookingAt("extend")) { LocationRecorder location(root_location, - FileDescriptorProto::kExtensionFieldNumber); - return ParseExtend(file->mutable_extension(), - file->mutable_message_type(), - root_location, - FileDescriptorProto::kMessageTypeFieldNumber, - location, file); + FileDescriptorProto::kExtensionFieldNumber); + return ParseExtend( + file->mutable_extension(), file->mutable_message_type(), root_location, + FileDescriptorProto::kMessageTypeFieldNumber, location, file); } else if (LookingAt("import")) { return ParseImport(file->mutable_dependency(), file->mutable_public_dependency(), - file->mutable_weak_dependency(), - root_location, file); + file->mutable_weak_dependency(), root_location, file); } else if (LookingAt("package")) { return ParsePackage(file, root_location, file); } else if (LookingAt("option")) { LocationRecorder location(root_location, - FileDescriptorProto::kOptionsFieldNumber); + FileDescriptorProto::kOptionsFieldNumber); return ParseOption(file->mutable_options(), location, file, OPTION_STATEMENT); } else { @@ -664,16 +749,21 @@ bool Parser::ParseTopLevelStatement(FileDescriptorProto* file, // Messages bool Parser::ParseMessageDefinition( - DescriptorProto* message, - const LocationRecorder& message_location, + DescriptorProto* message, const LocationRecorder& message_location, const FileDescriptorProto* containing_file) { DO(Consume("message")); { LocationRecorder location(message_location, DescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - message, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(message, + DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(message->mutable_name(), "Expected message name.")); + if (!IsUpperCamelCase(message->name())) { + AddWarning( + "Message name should be in UpperCamelCase. Found: " + + message->name() + + ". See https://developers.google.com/protocol-buffers/docs/style"); + } } DO(ParseMessageBlock(message, message_location, containing_file)); return true; @@ -701,9 +791,8 @@ bool IsMessageSetWireFormatMessage(const DescriptorProto& message) { // tag number can only be determined after all options have been parsed. void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) { const bool is_message_set = IsMessageSetWireFormatMessage(*message); - const int max_extension_number = is_message_set ? - kint32max : - FieldDescriptor::kMaxNumber + 1; + const int max_extension_number = + is_message_set ? kint32max : FieldDescriptor::kMaxNumber + 1; for (int i = 0; i < message->extension_range_size(); ++i) { if (message->extension_range(i).end() == kMaxRangeSentinel) { message->mutable_extension_range(i)->set_end(max_extension_number); @@ -716,9 +805,8 @@ void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) { // tag number can only be determined after all options have been parsed. void AdjustReservedRangesWithMaxEndNumber(DescriptorProto* message) { const bool is_message_set = IsMessageSetWireFormatMessage(*message); - const int max_field_number = is_message_set ? - kint32max : - FieldDescriptor::kMaxNumber + 1; + const int max_field_number = + is_message_set ? kint32max : FieldDescriptor::kMaxNumber + 1; for (int i = 0; i < message->reserved_range_size(); ++i) { if (message->reserved_range(i).end() == kMaxRangeSentinel) { message->mutable_reserved_range(i)->set_end(max_field_number); @@ -783,34 +871,28 @@ bool Parser::ParseMessageStatement(DescriptorProto* message, LocationRecorder location(message_location, DescriptorProto::kExtensionFieldNumber); return ParseExtend(message->mutable_extension(), - message->mutable_nested_type(), - message_location, - DescriptorProto::kNestedTypeFieldNumber, - location, containing_file); + message->mutable_nested_type(), message_location, + DescriptorProto::kNestedTypeFieldNumber, location, + containing_file); } else if (LookingAt("option")) { LocationRecorder location(message_location, DescriptorProto::kOptionsFieldNumber); - return ParseOption(message->mutable_options(), location, - containing_file, OPTION_STATEMENT); + return ParseOption(message->mutable_options(), location, containing_file, + OPTION_STATEMENT); } else if (LookingAt("oneof")) { int oneof_index = message->oneof_decl_size(); - LocationRecorder oneof_location(message_location, - DescriptorProto::kOneofDeclFieldNumber, - oneof_index); + LocationRecorder oneof_location( + message_location, DescriptorProto::kOneofDeclFieldNumber, oneof_index); - return ParseOneof(message->add_oneof_decl(), message, - oneof_index, oneof_location, message_location, - containing_file); + return ParseOneof(message->add_oneof_decl(), message, oneof_index, + oneof_location, message_location, containing_file); } else { LocationRecorder location(message_location, DescriptorProto::kFieldFieldNumber, message->field_size()); - return ParseMessageField(message->add_field(), - message->mutable_nested_type(), - message_location, - DescriptorProto::kNestedTypeFieldNumber, - location, - containing_file); + return ParseMessageField( + message->add_field(), message->mutable_nested_type(), message_location, + DescriptorProto::kNestedTypeFieldNumber, location, containing_file); } } @@ -838,13 +920,11 @@ bool Parser::ParseMessageField(FieldDescriptorProto* field, return ParseMessageFieldNoLabel(field, messages, parent_location, location_field_number_for_nested_type, - field_location, - containing_file); + field_location, containing_file); } bool Parser::ParseMessageFieldNoLabel( - FieldDescriptorProto* field, - RepeatedPtrField* messages, + FieldDescriptorProto* field, RepeatedPtrField* messages, const LocationRecorder& parent_location, int location_field_number_for_nested_type, const LocationRecorder& field_location, @@ -857,7 +937,7 @@ bool Parser::ParseMessageFieldNoLabel( bool type_parsed = false; FieldDescriptorProto::Type type = FieldDescriptorProto::TYPE_INT32; - string type_name; + std::string type_name; // Special case map field. We only treat the field as a map field if the // field type name starts with the word "map" with a following "<". @@ -928,6 +1008,18 @@ bool Parser::ParseMessageFieldNoLabel( FieldDescriptorProto::kNameFieldNumber); location.RecordLegacyLocation(field, DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(field->mutable_name(), "Expected field name.")); + + if (!IsLowerUnderscore(field->name())) { + AddWarning( + "Field name should be lowercase. Found: " + field->name() + + ". See: https://developers.google.com/protocol-buffers/docs/style"); + } + if (IsNumberFollowUnderscore(field->name())) { + AddWarning( + "Number should not come right after an underscore. Found: " + + field->name() + + ". See: https://developers.google.com/protocol-buffers/docs/style"); + } } DO(Consume("=", "Missing field number.")); @@ -935,8 +1027,8 @@ bool Parser::ParseMessageFieldNoLabel( { LocationRecorder location(field_location, FieldDescriptorProto::kNumberFieldNumber); - location.RecordLegacyLocation( - field, DescriptorPool::ErrorCollector::NUMBER); + location.RecordLegacyLocation(field, + DescriptorPool::ErrorCollector::NUMBER); int number; DO(ConsumeInteger(&number, "Expected field number.")); field->set_number(number); @@ -963,8 +1055,8 @@ bool Parser::ParseMessageFieldNoLabel( DescriptorProto::kNameFieldNumber); location.StartAt(name_token); location.EndAt(name_token); - location.RecordLegacyLocation( - group, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(group, + DescriptorPool::ErrorCollector::NAME); } // The field's type_name also comes from the name. Confusing! @@ -980,7 +1072,7 @@ bool Parser::ParseMessageFieldNoLabel( // not use groups; it should use nested messages. if (group->name()[0] < 'A' || 'Z' < group->name()[0]) { AddError(name_token.line, name_token.column, - "Group names must start with a capital letter."); + "Group names must start with a capital letter."); } LowerString(field->mutable_name()); @@ -1007,7 +1099,7 @@ void Parser::GenerateMapEntry(const MapField& map_field, FieldDescriptorProto* field, RepeatedPtrField* messages) { DescriptorProto* entry = messages->Add(); - string entry_name = MapEntryName(field->name()); + std::string entry_name = MapEntryName(field->name()); field->set_type_name(entry_name); entry->set_name(entry_name); entry->mutable_options()->set_map_entry(true); @@ -1056,12 +1148,12 @@ void Parser::GenerateMapEntry(const MapField& map_field, option.name(0).name_part() == "enforce_utf8" && !option.name(0).is_extension()) { if (key_field->type() == FieldDescriptorProto::TYPE_STRING) { - key_field->mutable_options()->add_uninterpreted_option() - ->CopyFrom(option); + key_field->mutable_options()->add_uninterpreted_option()->CopyFrom( + option); } if (value_field->type() == FieldDescriptorProto::TYPE_STRING) { - value_field->mutable_options()->add_uninterpreted_option() - ->CopyFrom(option); + value_field->mutable_options()->add_uninterpreted_option()->CopyFrom( + option); } } } @@ -1087,8 +1179,8 @@ bool Parser::ParseFieldOptions(FieldDescriptorProto* field, // Like default value, this "json_name" is not an actual option. DO(ParseJsonName(field, field_location, containing_file)); } else { - DO(ParseOption(field->mutable_options(), location, - containing_file, OPTION_ASSIGNMENT)); + DO(ParseOption(field->mutable_options(), location, containing_file, + OPTION_ASSIGNMENT)); } } while (TryConsume(",")); @@ -1097,8 +1189,7 @@ bool Parser::ParseFieldOptions(FieldDescriptorProto* field, } bool Parser::ParseDefaultAssignment( - FieldDescriptorProto* field, - const LocationRecorder& field_location, + FieldDescriptorProto* field, const LocationRecorder& field_location, const FileDescriptorProto* containing_file) { if (field->has_default_value()) { AddError("Already set option \"default\"."); @@ -1110,9 +1201,9 @@ bool Parser::ParseDefaultAssignment( LocationRecorder location(field_location, FieldDescriptorProto::kDefaultValueFieldNumber); - location.RecordLegacyLocation( - field, DescriptorPool::ErrorCollector::DEFAULT_VALUE); - string* default_value = field->mutable_default_value(); + location.RecordLegacyLocation(field, + DescriptorPool::ErrorCollector::DEFAULT_VALUE); + std::string* default_value = field->mutable_default_value(); if (!field->has_type()) { // The field has a type name, but we don't know if it is a message or an @@ -1154,7 +1245,7 @@ bool Parser::ParseDefaultAssignment( DO(ConsumeInteger64(max_value, &value, "Expected integer for field default value.")); // And stringify it again. - default_value->append(SimpleItoa(value)); + default_value->append(StrCat(value)); break; } @@ -1177,7 +1268,7 @@ bool Parser::ParseDefaultAssignment( DO(ConsumeInteger64(max_value, &value, "Expected integer for field default value.")); // And stringify it again. - default_value->append(SimpleItoa(value)); + default_value->append(StrCat(value)); break; } @@ -1210,7 +1301,8 @@ bool Parser::ParseDefaultAssignment( // Note: When file opton java_string_check_utf8 is true, if a // non-string representation (eg byte[]) is later supported, it must // be checked for UTF-8-ness. - DO(ConsumeString(default_value, "Expected string for field default " + DO(ConsumeString(default_value, + "Expected string for field default " "value.")); break; @@ -1220,8 +1312,9 @@ bool Parser::ParseDefaultAssignment( break; case FieldDescriptorProto::TYPE_ENUM: - DO(ConsumeIdentifier(default_value, "Expected enum identifier for field " - "default value.")); + DO(ConsumeIdentifier(default_value, + "Expected enum identifier for field " + "default value.")); break; case FieldDescriptorProto::TYPE_MESSAGE: @@ -1233,34 +1326,37 @@ bool Parser::ParseDefaultAssignment( return true; } -bool Parser::ParseJsonName( - FieldDescriptorProto* field, - const LocationRecorder& field_location, - const FileDescriptorProto* containing_file) { +bool Parser::ParseJsonName(FieldDescriptorProto* field, + const LocationRecorder& field_location, + const FileDescriptorProto* containing_file) { if (field->has_json_name()) { AddError("Already set option \"json_name\"."); field->clear_json_name(); } + LocationRecorder location(field_location, + FieldDescriptorProto::kJsonNameFieldNumber); + location.RecordLegacyLocation(field, + DescriptorPool::ErrorCollector::OPTION_NAME); + DO(Consume("json_name")); DO(Consume("=")); - LocationRecorder location(field_location, - FieldDescriptorProto::kJsonNameFieldNumber); - location.RecordLegacyLocation( + LocationRecorder value_location(location); + value_location.RecordLegacyLocation( field, DescriptorPool::ErrorCollector::OPTION_VALUE); + DO(ConsumeString(field->mutable_json_name(), "Expected string for JSON name.")); return true; } - bool Parser::ParseOptionNamePart(UninterpretedOption* uninterpreted_option, const LocationRecorder& part_location, const FileDescriptorProto* containing_file) { UninterpretedOption::NamePart* name = uninterpreted_option->add_name(); - string identifier; // We parse identifiers into this string. - if (LookingAt("(")) { // This is an extension. + std::string identifier; // We parse identifiers into this string. + if (LookingAt("(")) { // This is an extension. DO(Consume("(")); { @@ -1292,7 +1388,7 @@ bool Parser::ParseOptionNamePart(UninterpretedOption* uninterpreted_option, return true; } -bool Parser::ParseUninterpretedBlock(string* value) { +bool Parser::ParseUninterpretedBlock(std::string* value) { // Note that enclosing braces are not added to *value. // We do NOT use ConsumeEndOfStatement for this brace because it's delimiting // an expression, not a block of statements. @@ -1324,8 +1420,8 @@ bool Parser::ParseOption(Message* options, const FileDescriptorProto* containing_file, OptionStyle style) { // Create an entry in the uninterpreted_option field. - const FieldDescriptor* uninterpreted_option_field = options->GetDescriptor()-> - FindFieldByName("uninterpreted_option"); + const FieldDescriptor* uninterpreted_option_field = + options->GetDescriptor()->FindFieldByName("uninterpreted_option"); GOOGLE_CHECK(uninterpreted_option_field != NULL) << "No field named \"uninterpreted_option\" in the Options proto."; @@ -1339,9 +1435,9 @@ bool Parser::ParseOption(Message* options, DO(Consume("option")); } - UninterpretedOption* uninterpreted_option = down_cast( - options->GetReflection()->AddMessage(options, - uninterpreted_option_field)); + UninterpretedOption* uninterpreted_option = + down_cast(options->GetReflection()->AddMessage( + options, uninterpreted_option_field)); // Parse dot-separated name. { @@ -1393,7 +1489,7 @@ bool Parser::ParseOption(Message* options, AddError("Invalid '-' symbol before identifier."); return false; } - string value; + std::string value; DO(ConsumeIdentifier(&value, "Expected identifier.")); uninterpreted_option->set_identifier_value(value); break; @@ -1431,7 +1527,7 @@ bool Parser::ParseOption(Message* options, AddError("Invalid '-' symbol before string."); return false; } - string value; + std::string value; DO(ConsumeString(&value, "Expected string.")); uninterpreted_option->set_string_value(value); break; @@ -1472,8 +1568,8 @@ bool Parser::ParseExtensions(DescriptorProto* message, message->extension_range_size()); DescriptorProto::ExtensionRange* range = message->add_extension_range(); - location.RecordLegacyLocation( - range, DescriptorPool::ErrorCollector::NUMBER); + location.RecordLegacyLocation(range, + DescriptorPool::ErrorCollector::NUMBER); int start, end; io::Tokenizer::Token start_token; @@ -1538,8 +1634,8 @@ bool Parser::ParseExtensions(DescriptorProto* message, // Then copy the extension range options to all of the other ranges we've // parsed. for (int i = old_range_size + 1; i < message->extension_range_size(); i++) { - message->mutable_extension_range(i)->mutable_options() - ->CopyFrom(*options); + message->mutable_extension_range(i)->mutable_options()->CopyFrom( + *options); } // and copy source locations to the other ranges, too for (int i = old_range_size; i < message->extension_range_size(); i++) { @@ -1564,15 +1660,18 @@ bool Parser::ParseExtensions(DescriptorProto* message, // name literals. bool Parser::ParseReserved(DescriptorProto* message, const LocationRecorder& message_location) { + io::Tokenizer::Token start_token = input_->current(); // Parse the declaration. DO(Consume("reserved")); if (LookingAtType(io::Tokenizer::TYPE_STRING)) { LocationRecorder location(message_location, DescriptorProto::kReservedNameFieldNumber); + location.StartAt(start_token); return ParseReservedNames(message, location); } else { LocationRecorder location(message_location, DescriptorProto::kReservedRangeFieldNumber); + location.StartAt(start_token); return ParseReservedNumbers(message, location); } } @@ -1600,9 +1699,8 @@ bool Parser::ParseReservedNumbers(DescriptorProto* message, LocationRecorder start_location( location, DescriptorProto::ReservedRange::kStartFieldNumber); start_token = input_->current(); - DO(ConsumeInteger(&start, (first ? - "Expected field name or number range." : - "Expected field number range."))); + DO(ConsumeInteger(&start, (first ? "Expected field name or number range." + : "Expected field number range."))); } if (TryConsume("to")) { @@ -1638,16 +1736,19 @@ bool Parser::ParseReservedNumbers(DescriptorProto* message, } bool Parser::ParseReserved(EnumDescriptorProto* message, - const LocationRecorder& message_location) { + const LocationRecorder& message_location) { + io::Tokenizer::Token start_token = input_->current(); // Parse the declaration. DO(Consume("reserved")); if (LookingAtType(io::Tokenizer::TYPE_STRING)) { LocationRecorder location(message_location, DescriptorProto::kReservedNameFieldNumber); + location.StartAt(start_token); return ParseReservedNames(message, location); } else { LocationRecorder location(message_location, DescriptorProto::kReservedRangeFieldNumber); + location.StartAt(start_token); return ParseReservedNumbers(message, location); } } @@ -1676,9 +1777,9 @@ bool Parser::ParseReservedNumbers(EnumDescriptorProto* message, LocationRecorder start_location( location, EnumDescriptorProto::EnumReservedRange::kStartFieldNumber); start_token = input_->current(); - DO(ConsumeSignedInteger(&start, (first ? - "Expected enum value or number range." : - "Expected enum number range."))); + DO(ConsumeSignedInteger(&start, + (first ? "Expected enum value or number range." + : "Expected enum number range."))); } if (TryConsume("to")) { @@ -1718,7 +1819,7 @@ bool Parser::ParseExtend(RepeatedPtrField* extensions, // Parse the extendee type. io::Tokenizer::Token extendee_start = input_->current(); - string extendee; + std::string extendee; DO(ParseUserDefinedType(&extendee)); io::Tokenizer::Token extendee_end = input_->previous(); @@ -1754,8 +1855,7 @@ bool Parser::ParseExtend(RepeatedPtrField* extensions, field->set_extendee(extendee); if (!ParseMessageField(field, messages, parent_location, - location_field_number_for_nested_type, - location, + location_field_number_for_nested_type, location, containing_file)) { // This statement failed to parse. Skip it, but keep looping to parse // other statements. @@ -1767,8 +1867,7 @@ bool Parser::ParseExtend(RepeatedPtrField* extensions, } bool Parser::ParseOneof(OneofDescriptorProto* oneof_decl, - DescriptorProto* containing_type, - int oneof_index, + DescriptorProto* containing_type, int oneof_index, const LocationRecorder& oneof_location, const LocationRecorder& containing_type_location, const FileDescriptorProto* containing_file) { @@ -1800,11 +1899,11 @@ bool Parser::ParseOneof(OneofDescriptorProto* oneof_decl, // Print a nice error if the user accidentally tries to place a label // on an individual member of a oneof. - if (LookingAt("required") || - LookingAt("optional") || + if (LookingAt("required") || LookingAt("optional") || LookingAt("repeated")) { - AddError("Fields in oneofs must not have labels (required / optional " - "/ repeated)."); + AddError( + "Fields in oneofs must not have labels (required / optional " + "/ repeated)."); // We can continue parsing here because we understand what the user // meant. The error report will still make parsing fail overall. input_->Next(); @@ -1818,12 +1917,10 @@ bool Parser::ParseOneof(OneofDescriptorProto* oneof_decl, field->set_label(FieldDescriptorProto::LABEL_OPTIONAL); field->set_oneof_index(oneof_index); - if (!ParseMessageFieldNoLabel(field, - containing_type->mutable_nested_type(), + if (!ParseMessageFieldNoLabel(field, containing_type->mutable_nested_type(), containing_type_location, DescriptorProto::kNestedTypeFieldNumber, - field_location, - containing_file)) { + field_location, containing_file)) { // This statement failed to parse. Skip it, but keep looping to parse // other statements. SkipStatement(); @@ -1844,8 +1941,8 @@ bool Parser::ParseEnumDefinition(EnumDescriptorProto* enum_type, { LocationRecorder location(enum_location, EnumDescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - enum_type, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(enum_type, + DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(enum_type->mutable_name(), "Expected enum name.")); } @@ -1886,13 +1983,14 @@ bool Parser::ParseEnumStatement(EnumDescriptorProto* enum_type, } else if (LookingAt("option")) { LocationRecorder location(enum_location, EnumDescriptorProto::kOptionsFieldNumber); - return ParseOption(enum_type->mutable_options(), location, - containing_file, OPTION_STATEMENT); + return ParseOption(enum_type->mutable_options(), location, containing_file, + OPTION_STATEMENT); } else if (LookingAt("reserved")) { return ParseReserved(enum_type, enum_location); } else { LocationRecorder location(enum_location, - EnumDescriptorProto::kValueFieldNumber, enum_type->value_size()); + EnumDescriptorProto::kValueFieldNumber, + enum_type->value_size()); return ParseEnumConstant(enum_type->add_value(), location, containing_file); } } @@ -1904,8 +2002,8 @@ bool Parser::ParseEnumConstant(EnumValueDescriptorProto* enum_value, { LocationRecorder location(enum_value_location, EnumValueDescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - enum_value, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(enum_value, + DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(enum_value->mutable_name(), "Expected enum constant name.")); } @@ -1914,10 +2012,10 @@ bool Parser::ParseEnumConstant(EnumValueDescriptorProto* enum_value, // Parse value. { - LocationRecorder location( - enum_value_location, EnumValueDescriptorProto::kNumberFieldNumber); - location.RecordLegacyLocation( - enum_value, DescriptorPool::ErrorCollector::NUMBER); + LocationRecorder location(enum_value_location, + EnumValueDescriptorProto::kNumberFieldNumber); + location.RecordLegacyLocation(enum_value, + DescriptorPool::ErrorCollector::NUMBER); int number; DO(ConsumeSignedInteger(&number, "Expected integer.")); @@ -1938,14 +2036,14 @@ bool Parser::ParseEnumConstantOptions( const FileDescriptorProto* containing_file) { if (!LookingAt("[")) return true; - LocationRecorder location( - enum_value_location, EnumValueDescriptorProto::kOptionsFieldNumber); + LocationRecorder location(enum_value_location, + EnumValueDescriptorProto::kOptionsFieldNumber); DO(Consume("[")); do { - DO(ParseOption(value->mutable_options(), location, - containing_file, OPTION_ASSIGNMENT)); + DO(ParseOption(value->mutable_options(), location, containing_file, + OPTION_ASSIGNMENT)); } while (TryConsume(",")); DO(Consume("]")); @@ -1956,16 +2054,15 @@ bool Parser::ParseEnumConstantOptions( // Services bool Parser::ParseServiceDefinition( - ServiceDescriptorProto* service, - const LocationRecorder& service_location, + ServiceDescriptorProto* service, const LocationRecorder& service_location, const FileDescriptorProto* containing_file) { DO(Consume("service")); { LocationRecorder location(service_location, ServiceDescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - service, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(service, + DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(service->mutable_name(), "Expected service name.")); } @@ -2001,13 +2098,14 @@ bool Parser::ParseServiceStatement(ServiceDescriptorProto* service, // empty statement; ignore return true; } else if (LookingAt("option")) { - LocationRecorder location( - service_location, ServiceDescriptorProto::kOptionsFieldNumber); - return ParseOption(service->mutable_options(), location, - containing_file, OPTION_STATEMENT); + LocationRecorder location(service_location, + ServiceDescriptorProto::kOptionsFieldNumber); + return ParseOption(service->mutable_options(), location, containing_file, + OPTION_STATEMENT); } else { LocationRecorder location(service_location, - ServiceDescriptorProto::kMethodFieldNumber, service->method_size()); + ServiceDescriptorProto::kMethodFieldNumber, + service->method_size()); return ParseServiceMethod(service->add_method(), location, containing_file); } } @@ -2020,8 +2118,7 @@ bool Parser::ParseServiceMethod(MethodDescriptorProto* method, { LocationRecorder location(method_location, MethodDescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(method, DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(method->mutable_name(), "Expected method name.")); } @@ -2031,16 +2128,16 @@ bool Parser::ParseServiceMethod(MethodDescriptorProto* method, if (LookingAt("stream")) { LocationRecorder location( method_location, MethodDescriptorProto::kClientStreamingFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::OTHER); + location.RecordLegacyLocation(method, + DescriptorPool::ErrorCollector::OTHER); method->set_client_streaming(true); DO(Consume("stream")); } LocationRecorder location(method_location, MethodDescriptorProto::kInputTypeFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::INPUT_TYPE); + location.RecordLegacyLocation(method, + DescriptorPool::ErrorCollector::INPUT_TYPE); DO(ParseUserDefinedType(method->mutable_input_type())); } DO(Consume(")")); @@ -2052,16 +2149,16 @@ bool Parser::ParseServiceMethod(MethodDescriptorProto* method, if (LookingAt("stream")) { LocationRecorder location( method_location, MethodDescriptorProto::kServerStreamingFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::OTHER); + location.RecordLegacyLocation(method, + DescriptorPool::ErrorCollector::OTHER); DO(Consume("stream")); method->set_server_streaming(true); } LocationRecorder location(method_location, MethodDescriptorProto::kOutputTypeFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::OUTPUT_TYPE); + location.RecordLegacyLocation(method, + DescriptorPool::ErrorCollector::OUTPUT_TYPE); DO(ParseUserDefinedType(method->mutable_output_type())); } DO(Consume(")")); @@ -2094,10 +2191,9 @@ bool Parser::ParseMethodOptions(const LocationRecorder& parent_location, if (TryConsumeEndOfDeclaration(";", NULL)) { // empty statement; ignore } else { - LocationRecorder location(parent_location, - optionsFieldNumber); - if (!ParseOption(mutable_options, location, - containing_file, OPTION_STATEMENT)) { + LocationRecorder location(parent_location, optionsFieldNumber); + if (!ParseOption(mutable_options, location, containing_file, + OPTION_STATEMENT)) { // This statement failed to parse. Skip it, but keep looping to // parse other statements. SkipStatement(); @@ -2126,7 +2222,7 @@ bool Parser::ParseLabel(FieldDescriptorProto::Label* label, } bool Parser::ParseType(FieldDescriptorProto::Type* type, - string* type_name) { + std::string* type_name) { TypeNameMap::const_iterator iter = kTypeNames.find(input_->current().text); if (iter != kTypeNames.end()) { *type = iter->second; @@ -2137,7 +2233,7 @@ bool Parser::ParseType(FieldDescriptorProto::Type* type, return true; } -bool Parser::ParseUserDefinedType(string* type_name) { +bool Parser::ParseUserDefinedType(std::string* type_name) { type_name->clear(); TypeNameMap::const_iterator iter = kTypeNames.find(input_->current().text); @@ -2158,7 +2254,7 @@ bool Parser::ParseUserDefinedType(string* type_name) { if (TryConsume(".")) type_name->append("."); // Consume the first part of the name. - string identifier; + std::string identifier; DO(ConsumeIdentifier(&identifier, "Expected type name.")); type_name->append(identifier); @@ -2184,59 +2280,59 @@ bool Parser::ParsePackage(FileDescriptorProto* file, file->clear_package(); } - DO(Consume("package")); - - { - LocationRecorder location(root_location, - FileDescriptorProto::kPackageFieldNumber); - location.RecordLegacyLocation(file, DescriptorPool::ErrorCollector::NAME); - - while (true) { - string identifier; - DO(ConsumeIdentifier(&identifier, "Expected identifier.")); - file->mutable_package()->append(identifier); - if (!TryConsume(".")) break; - file->mutable_package()->append("."); - } + LocationRecorder location(root_location, + FileDescriptorProto::kPackageFieldNumber); + location.RecordLegacyLocation(file, DescriptorPool::ErrorCollector::NAME); - location.EndAt(input_->previous()); + DO(Consume("package")); - DO(ConsumeEndOfDeclaration(";", &location)); + while (true) { + std::string identifier; + DO(ConsumeIdentifier(&identifier, "Expected identifier.")); + file->mutable_package()->append(identifier); + if (!TryConsume(".")) break; + file->mutable_package()->append("."); } + DO(ConsumeEndOfDeclaration(";", &location)); + return true; } -bool Parser::ParseImport(RepeatedPtrField* dependency, +bool Parser::ParseImport(RepeatedPtrField* dependency, RepeatedField* public_dependency, RepeatedField* weak_dependency, const LocationRecorder& root_location, const FileDescriptorProto* containing_file) { + LocationRecorder location(root_location, + FileDescriptorProto::kDependencyFieldNumber, + dependency->size()); + DO(Consume("import")); + if (LookingAt("public")) { - LocationRecorder location( + LocationRecorder public_location( root_location, FileDescriptorProto::kPublicDependencyFieldNumber, public_dependency->size()); DO(Consume("public")); *public_dependency->Add() = dependency->size(); } else if (LookingAt("weak")) { - LocationRecorder location( + LocationRecorder weak_location( root_location, FileDescriptorProto::kWeakDependencyFieldNumber, weak_dependency->size()); + weak_location.RecordLegacyImportLocation(containing_file, "weak"); DO(Consume("weak")); *weak_dependency->Add() = dependency->size(); } - { - LocationRecorder location(root_location, - FileDescriptorProto::kDependencyFieldNumber, - dependency->size()); - DO(ConsumeString(dependency->Add(), - "Expected a string naming the file to import.")); - location.EndAt(input_->previous()); + string import_file; + DO(ConsumeString(&import_file, + "Expected a string naming the file to import.")); + *dependency->Add() = import_file; + location.RecordLegacyImportLocation(containing_file, import_file); + + DO(ConsumeEndOfDeclaration(";", &location)); - DO(ConsumeEndOfDeclaration(";", &location)); - } return true; } @@ -2247,16 +2343,32 @@ SourceLocationTable::~SourceLocationTable() {} bool SourceLocationTable::Find( const Message* descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - int* line, int* column) const { + DescriptorPool::ErrorCollector::ErrorLocation location, int* line, + int* column) const { const std::pair* result = FindOrNull(location_map_, std::make_pair(descriptor, location)); if (result == NULL) { - *line = -1; + *line = -1; + *column = 0; + return false; + } else { + *line = result->first; + *column = result->second; + return true; + } +} + +bool SourceLocationTable::FindImport(const Message* descriptor, + const string& name, int* line, + int* column) const { + const std::pair* result = + FindOrNull(import_location_map_, std::make_pair(descriptor, name)); + if (result == nullptr) { + *line = -1; *column = 0; return false; } else { - *line = result->first; + *line = result->first; *column = result->second; return true; } @@ -2264,16 +2376,20 @@ bool SourceLocationTable::Find( void SourceLocationTable::Add( const Message* descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - int line, int column) { + DescriptorPool::ErrorCollector::ErrorLocation location, int line, + int column) { location_map_[std::make_pair(descriptor, location)] = std::make_pair(line, column); } -void SourceLocationTable::Clear() { - location_map_.clear(); +void SourceLocationTable::AddImport(const Message* descriptor, + const string& name, int line, int column) { + import_location_map_[std::make_pair(descriptor, name)] = + std::make_pair(line, column); } +void SourceLocationTable::Clear() { location_map_.clear(); } + } // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/parser.h b/src/google/protobuf/compiler/parser.h index b55750ca45..9e2dc5f532 100644 --- a/src/google/protobuf/compiler/parser.h +++ b/src/google/protobuf/compiler/parser.h @@ -45,6 +45,8 @@ #include #include +#include + namespace google { namespace protobuf { @@ -63,7 +65,7 @@ class SourceLocationTable; // to a FileDescriptorProto. It does not resolve import directives or perform // many other kinds of validation needed to construct a complete // FileDescriptor. -class LIBPROTOBUF_EXPORT Parser { +class PROTOBUF_EXPORT Parser { public: Parser(); ~Parser(); @@ -93,7 +95,7 @@ class LIBPROTOBUF_EXPORT Parser { // Returns the identifier used in the "syntax = " declaration, if one was // seen during the last call to Parse(), or the empty string otherwise. - const string& GetSyntaxIdentifier() { return syntax_identifier_; } + const std::string& GetSyntaxIdentifier() { return syntax_identifier_; } // If set true, input files will be required to begin with a syntax // identifier. Otherwise, files may omit this. If a syntax identifier @@ -163,7 +165,7 @@ class LIBPROTOBUF_EXPORT Parser { // where "text" is the expected token text. bool Consume(const char* text); // Consume a token of type IDENTIFIER and store its text in "output". - bool ConsumeIdentifier(string* output, const char* error); + bool ConsumeIdentifier(std::string* output, const char* error); // Consume an integer and store its value in "output". bool ConsumeInteger(int* output, const char* error); // Consume a signed integer and store its value in "output". @@ -175,7 +177,7 @@ class LIBPROTOBUF_EXPORT Parser { // tokens of either INTEGER or FLOAT type. bool ConsumeNumber(double* output, const char* error); // Consume a string literal and store its (unescaped) value in "output". - bool ConsumeString(string* output, const char* error); + bool ConsumeString(std::string* output, const char* error); // Consume a token representing the end of the statement. Comments between // this token and the next will be harvested for documentation. The given @@ -186,30 +188,34 @@ class LIBPROTOBUF_EXPORT Parser { // have been passed around by const reference, for no particularly good // reason. We should probably go through and change them all to mutable // pointer to make this more intuitive. - bool TryConsumeEndOfDeclaration( - const char* text, const LocationRecorder* location); - bool TryConsumeEndOfDeclarationFinishScope( - const char* text, const LocationRecorder* location); + bool TryConsumeEndOfDeclaration(const char* text, + const LocationRecorder* location); + bool TryConsumeEndOfDeclarationFinishScope(const char* text, + const LocationRecorder* location); - bool ConsumeEndOfDeclaration( - const char* text, const LocationRecorder* location); + bool ConsumeEndOfDeclaration(const char* text, + const LocationRecorder* location); // ----------------------------------------------------------------- // Error logging helpers // Invokes error_collector_->AddError(), if error_collector_ is not NULL. - void AddError(int line, int column, const string& error); + void AddError(int line, int column, const std::string& error); // Invokes error_collector_->AddError() with the line and column number // of the current token. - void AddError(const string& error); + void AddError(const std::string& error); + + // Invokes error_collector_->AddWarning() with the line and column number + // of the current token. + void AddWarning(const string& warning); // Records a location in the SourceCodeInfo.location table (see // descriptor.proto). We use RAII to ensure that the start and end locations // are recorded -- the constructor records the start location and the // destructor records the end location. Since the parser is // recursive-descent, this works out beautifully. - class LIBPROTOBUF_EXPORT LocationRecorder { + class PROTOBUF_EXPORT LocationRecorder { public: // Construct the file's "root" location. LocationRecorder(Parser* parser); @@ -252,8 +258,11 @@ class LIBPROTOBUF_EXPORT Parser { // was passed to RecordSourceLocationsTo(), if any. SourceLocationTable // is an older way of keeping track of source locations which is still // used in some places. - void RecordLegacyLocation(const Message* descriptor, + void RecordLegacyLocation( + const Message* descriptor, DescriptorPool::ErrorCollector::ErrorLocation location); + void RecordLegacyImportLocation(const Message* descriptor, + const string& name); // Returns the number of path components in the recorder's current location. int CurrentPathSize() const; @@ -264,8 +273,8 @@ class LIBPROTOBUF_EXPORT Parser { // // TODO(kenton): See comment on TryConsumeEndOfDeclaration(), above, for // why this is const. - void AttachComments(string* leading, string* trailing, - std::vector* detached_comments) const; + void AttachComments(std::string* leading, std::string* trailing, + std::vector* detached_comments) const; private: // Indexes of parent and current location in the parent @@ -313,7 +322,7 @@ class LIBPROTOBUF_EXPORT Parser { bool ParsePackage(FileDescriptorProto* file, const LocationRecorder& root_location, const FileDescriptorProto* containing_file); - bool ParseImport(RepeatedPtrField* dependency, + bool ParseImport(RepeatedPtrField* dependency, RepeatedField* public_dependency, RepeatedField* weak_dependency, const LocationRecorder& root_location, @@ -400,8 +409,7 @@ class LIBPROTOBUF_EXPORT Parser { // oneof_decl->label() since it will have had to parse the label before it // knew it was parsing a oneof. bool ParseOneof(OneofDescriptorProto* oneof_decl, - DescriptorProto* containing_type, - int oneof_index, + DescriptorProto* containing_type, int oneof_index, const LocationRecorder& oneof_location, const LocationRecorder& containing_type_location, const FileDescriptorProto* containing_file); @@ -436,11 +444,10 @@ class LIBPROTOBUF_EXPORT Parser { // Parse a type name and fill in "type" (if it is a primitive) or // "type_name" (if it is not) with the type parsed. - bool ParseType(FieldDescriptorProto::Type* type, - string* type_name); + bool ParseType(FieldDescriptorProto::Type* type, std::string* type_name); // Parse a user-defined type and fill in "type_name" with the name. // If a primitive type is named, it is treated as an error. - bool ParseUserDefinedType(string* type_name); + bool ParseUserDefinedType(std::string* type_name); // Parses field options, i.e. the stuff in square brackets at the end // of a field definition. Also parses default value. @@ -466,8 +473,7 @@ class LIBPROTOBUF_EXPORT Parser { // Parse a single option name/value pair, e.g. "ctype = CORD". The name // identifies a field of the given Message, and the value of that field // is set to the parsed value. - bool ParseOption(Message* options, - const LocationRecorder& options_location, + bool ParseOption(Message* options, const LocationRecorder& options_location, const FileDescriptorProto* containing_file, OptionStyle style); @@ -489,7 +495,7 @@ class LIBPROTOBUF_EXPORT Parser { // REQUIRES: LookingAt("{") // When finished successfully, we are looking at the first token past // the ending brace. - bool ParseUninterpretedBlock(string* value); + bool ParseUninterpretedBlock(std::string* value); struct MapField { // Whether the field is a map field. @@ -498,8 +504,8 @@ class LIBPROTOBUF_EXPORT Parser { FieldDescriptorProto::Type key_type; FieldDescriptorProto::Type value_type; // Or the type names string if the types are customized types. - string key_type_name; - string value_type_name; + std::string key_type_name; + std::string value_type_name; MapField() : is_map_field(false) {} }; @@ -524,18 +530,18 @@ class LIBPROTOBUF_EXPORT Parser { bool had_errors_; bool require_syntax_identifier_; bool stop_after_syntax_identifier_; - string syntax_identifier_; + std::string syntax_identifier_; // Leading doc comments for the next declaration. These are not complete // yet; use ConsumeEndOfDeclaration() to get the complete comments. - string upcoming_doc_comments_; + std::string upcoming_doc_comments_; // Detached comments are not connected to any syntax entities. Elements in // this vector are paragraphs of comments separated by empty lines. The // detached comments will be put into the leading_detached_comments field for // the next element (See SourceCodeInfo.Location in descriptor.proto), when // ConsumeEndOfDeclaration() is called. - std::vector upcoming_detached_comments_; + std::vector upcoming_detached_comments_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Parser); }; @@ -548,7 +554,7 @@ class LIBPROTOBUF_EXPORT Parser { // far more complete information about source locations. However, as of this // writing you still need to use SourceLocationTable when integrating with // DescriptorPool. -class LIBPROTOBUF_EXPORT SourceLocationTable { +class PROTOBUF_EXPORT SourceLocationTable { public: SourceLocationTable(); ~SourceLocationTable(); @@ -559,26 +565,35 @@ class LIBPROTOBUF_EXPORT SourceLocationTable { // location" in the ErrorCollector interface). Returns true if found, false // otherwise. bool Find(const Message* descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - int* line, int* column) const; + DescriptorPool::ErrorCollector::ErrorLocation location, int* line, + int* column) const; + bool FindImport(const Message* descriptor, const string& name, int* line, + int* column) const; // Adds a location to the table. void Add(const Message* descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - int line, int column); + DescriptorPool::ErrorCollector::ErrorLocation location, int line, + int column); + void AddImport(const Message* descriptor, const string& name, int line, + int column); // Clears the contents of the table. void Clear(); private: typedef std::map< - std::pair, - std::pair > LocationMap; + std::pair, + std::pair > + LocationMap; LocationMap location_map_; + std::map, std::pair > + import_location_map_; }; } // namespace compiler } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_PARSER_H__ diff --git a/src/google/protobuf/compiler/parser_unittest.cc b/src/google/protobuf/compiler/parser_unittest.cc index 008d97d57f..43784cb321 100644 --- a/src/google/protobuf/compiler/parser_unittest.cc +++ b/src/google/protobuf/compiler/parser_unittest.cc @@ -32,10 +32,10 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include -#include #include #include +#include +#include #include @@ -62,15 +62,20 @@ namespace { class MockErrorCollector : public io::ErrorCollector { public: - MockErrorCollector() {} - ~MockErrorCollector() {} + MockErrorCollector() = default; + ~MockErrorCollector() override = default; - string text_; + std::string warning_; + std::string text_; // implements ErrorCollector --------------------------------------- - void AddError(int line, int column, const string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", - line, column, message); + void AddWarning(int line, int column, const std::string& message) override { + strings::SubstituteAndAppend(&warning_, "$0:$1: $2\n", line, column, + message); + } + + void AddError(int line, int column, const std::string& message) override { + strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line, column, message); } }; @@ -78,18 +83,20 @@ class MockValidationErrorCollector : public DescriptorPool::ErrorCollector { public: MockValidationErrorCollector(const SourceLocationTable& source_locations, io::ErrorCollector* wrapped_collector) - : source_locations_(source_locations), - wrapped_collector_(wrapped_collector) {} + : source_locations_(source_locations), + wrapped_collector_(wrapped_collector) {} ~MockValidationErrorCollector() {} // implements ErrorCollector --------------------------------------- - void AddError(const string& filename, - const string& element_name, - const Message* descriptor, - ErrorLocation location, - const string& message) { + void AddError(const std::string& filename, const std::string& element_name, + const Message* descriptor, ErrorLocation location, + const std::string& message) { int line, column; - source_locations_.Find(descriptor, location, &line, &column); + if (location == DescriptorPool::ErrorCollector::IMPORT) { + source_locations_.FindImport(descriptor, element_name, &line, &column); + } else { + source_locations_.Find(descriptor, location, &line, &column); + } wrapped_collector_->AddError(line, column, message); } @@ -100,8 +107,7 @@ class MockValidationErrorCollector : public DescriptorPool::ErrorCollector { class ParserTest : public testing::Test { protected: - ParserTest() - : require_syntax_identifier_(false) {} + ParserTest() : require_syntax_identifier_(false) {} // Set up the parser to parse the given text. void SetupParser(const char* text) { @@ -166,10 +172,10 @@ class ParserTest : public testing::Test { EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type); ASSERT_EQ("", error_collector_.text_); - MockValidationErrorCollector validation_error_collector( - source_locations, &error_collector_); + MockValidationErrorCollector validation_error_collector(source_locations, + &error_collector_); EXPECT_TRUE(pool_.BuildFileCollectingErrors( - file, &validation_error_collector) == NULL); + file, &validation_error_collector) == NULL); EXPECT_EQ(expected_errors, error_collector_.text_); } @@ -186,9 +192,9 @@ class ParserTest : public testing::Test { TEST_F(ParserTest, StopAfterSyntaxIdentifier) { SetupParser( - "// blah\n" - "syntax = \"foobar\";\n" - "this line will not be parsed\n"); + "// blah\n" + "syntax = \"foobar\";\n" + "this line will not be parsed\n"); parser_->SetStopAfterSyntaxIdentifier(true); EXPECT_TRUE(parser_->Parse(input_.get(), NULL)); EXPECT_EQ("", error_collector_.text_); @@ -197,8 +203,8 @@ TEST_F(ParserTest, StopAfterSyntaxIdentifier) { TEST_F(ParserTest, StopAfterOmittedSyntaxIdentifier) { SetupParser( - "// blah\n" - "this line will not be parsed\n"); + "// blah\n" + "this line will not be parsed\n"); parser_->SetStopAfterSyntaxIdentifier(true); EXPECT_TRUE(parser_->Parse(input_.get(), NULL)); EXPECT_EQ("", error_collector_.text_); @@ -207,8 +213,8 @@ TEST_F(ParserTest, StopAfterOmittedSyntaxIdentifier) { TEST_F(ParserTest, StopAfterSyntaxIdentifierWithErrors) { SetupParser( - "// blah\n" - "syntax = error;\n"); + "// blah\n" + "syntax = error;\n"); parser_->SetStopAfterSyntaxIdentifier(true); EXPECT_FALSE(parser_->Parse(input_.get(), NULL)); EXPECT_EQ("1:9: Expected syntax identifier.\n", error_collector_.text_); @@ -219,8 +225,45 @@ TEST_F(ParserTest, WarnIfSyntaxIdentifierOmmitted) { FileDescriptorProto file; CaptureTestStderr(); EXPECT_TRUE(parser_->Parse(input_.get(), &file)); - EXPECT_TRUE( - GetCapturedTestStderr().find("No syntax specified") != string::npos); + EXPECT_TRUE(GetCapturedTestStderr().find("No syntax specified") != + string::npos); +} + +TEST_F(ParserTest, WarnIfFieldNameIsNotUpperCamel) { + SetupParser( + "syntax = \"proto2\";" + "message abc {}"); + FileDescriptorProto file; + EXPECT_TRUE(parser_->Parse(input_.get(), &file)); + EXPECT_TRUE(error_collector_.warning_.find( + "Message name should be in UpperCamelCase. Found: abc.") != + string::npos); +} + +TEST_F(ParserTest, WarnIfFieldNameIsNotLowerUnderscore) { + SetupParser( + "syntax = \"proto2\";" + "message A {" + " optional string SongName = 1;" + "}"); + FileDescriptorProto file; + EXPECT_TRUE(parser_->Parse(input_.get(), &file)); + EXPECT_TRUE(error_collector_.warning_.find( + "Field name should be lowercase. Found: SongName") != + string::npos); +} + +TEST_F(ParserTest, WarnIfFieldNameContainsNumberImmediatelyFollowUnderscore) { + SetupParser( + "syntax = \"proto2\";" + "message A {" + " optional string song_name_1 = 1;" + "}"); + FileDescriptorProto file; + EXPECT_TRUE(parser_->Parse(input_.get(), &file)); + EXPECT_TRUE(error_collector_.warning_.find( + "Number should not come right after an underscore. Found: " + "song_name_1.") != string::npos); } // =================================================================== @@ -228,22 +271,25 @@ TEST_F(ParserTest, WarnIfSyntaxIdentifierOmmitted) { typedef ParserTest ParseMessageTest; TEST_F(ParseMessageTest, IgnoreBOM) { - char input[] = " message TestMessage {\n" + char input[] = + " message TestMessage {\n" " required int32 foo = 1;\n" "}\n"; // Set UTF-8 BOM. input[0] = (char)0xEF; input[1] = (char)0xBB; input[2] = (char)0xBF; - ExpectParsesTo(input, - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + ExpectParsesTo( + input, + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); } TEST_F(ParseMessageTest, BOMError) { - char input[] = " message TestMessage {\n" + char input[] = + " message TestMessage {\n" " required int32 foo = 1;\n" "}\n"; input[0] = (char)0xEF; @@ -255,590 +301,634 @@ TEST_F(ParseMessageTest, BOMError) { TEST_F(ParseMessageTest, SimpleMessage) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1;\n" - "}\n", + "message TestMessage {\n" + " required int32 foo = 1;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); } TEST_F(ParseMessageTest, ImplicitSyntaxIdentifier) { require_syntax_identifier_ = false; ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + "message TestMessage {\n" + " required int32 foo = 1;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); EXPECT_EQ("proto2", parser_->GetSyntaxIdentifier()); } TEST_F(ParseMessageTest, ExplicitSyntaxIdentifier) { ExpectParsesTo( - "syntax = \"proto2\";\n" - "message TestMessage {\n" - " required int32 foo = 1;\n" - "}\n", - - "syntax: 'proto2' " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + "syntax = \"proto2\";\n" + "message TestMessage {\n" + " required int32 foo = 1;\n" + "}\n", + + "syntax: 'proto2' " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); EXPECT_EQ("proto2", parser_->GetSyntaxIdentifier()); } TEST_F(ParseMessageTest, ExplicitRequiredSyntaxIdentifier) { require_syntax_identifier_ = true; ExpectParsesTo( - "syntax = \"proto2\";\n" - "message TestMessage {\n" - " required int32 foo = 1;\n" - "}\n", - - "syntax: 'proto2' " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + "syntax = \"proto2\";\n" + "message TestMessage {\n" + " required int32 foo = 1;\n" + "}\n", + + "syntax: 'proto2' " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); EXPECT_EQ("proto2", parser_->GetSyntaxIdentifier()); } TEST_F(ParseMessageTest, SimpleFields) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 15;\n" - " optional int32 bar = 34;\n" - " repeated int32 baz = 3;\n" - "}\n", + "message TestMessage {\n" + " required int32 foo = 15;\n" + " optional int32 bar = 34;\n" + " repeated int32 baz = 3;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:15 }" - " field { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:34 }" - " field { name:\"baz\" label:LABEL_REPEATED type:TYPE_INT32 number:3 }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:15 }" + " field { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:34 }" + " field { name:\"baz\" label:LABEL_REPEATED type:TYPE_INT32 number:3 }" + "}"); } TEST_F(ParseMessageTest, PrimitiveFieldTypes) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1;\n" - " required int64 foo = 1;\n" - " required uint32 foo = 1;\n" - " required uint64 foo = 1;\n" - " required sint32 foo = 1;\n" - " required sint64 foo = 1;\n" - " required fixed32 foo = 1;\n" - " required fixed64 foo = 1;\n" - " required sfixed32 foo = 1;\n" - " required sfixed64 foo = 1;\n" - " required float foo = 1;\n" - " required double foo = 1;\n" - " required string foo = 1;\n" - " required bytes foo = 1;\n" - " required bool foo = 1;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_UINT32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_UINT64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SINT32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SINT64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FIXED32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FIXED64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SFIXED32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SFIXED64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FLOAT number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_DOUBLE number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_STRING number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_BYTES number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_BOOL number:1 }" - "}"); + "message TestMessage {\n" + " required int32 foo = 1;\n" + " required int64 foo = 1;\n" + " required uint32 foo = 1;\n" + " required uint64 foo = 1;\n" + " required sint32 foo = 1;\n" + " required sint64 foo = 1;\n" + " required fixed32 foo = 1;\n" + " required fixed64 foo = 1;\n" + " required sfixed32 foo = 1;\n" + " required sfixed64 foo = 1;\n" + " required float foo = 1;\n" + " required double foo = 1;\n" + " required string foo = 1;\n" + " required bytes foo = 1;\n" + " required bool foo = 1;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_UINT32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_UINT64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SINT32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SINT64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FIXED32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FIXED64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SFIXED32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SFIXED64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FLOAT number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_DOUBLE number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_STRING number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_BYTES number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_BOOL number:1 " + "}" + "}"); } TEST_F(ParseMessageTest, FieldDefaults) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1 [default= 1 ];\n" - " required int32 foo = 1 [default= -2 ];\n" - " required int64 foo = 1 [default= 3 ];\n" - " required int64 foo = 1 [default= -4 ];\n" - " required uint32 foo = 1 [default= 5 ];\n" - " required uint64 foo = 1 [default= 6 ];\n" - " required float foo = 1 [default= 7.5];\n" - " required float foo = 1 [default= -8.5];\n" - " required float foo = 1 [default= 9 ];\n" - " required double foo = 1 [default= 10.5];\n" - " required double foo = 1 [default=-11.5];\n" - " required double foo = 1 [default= 12 ];\n" - " required double foo = 1 [default= inf ];\n" - " required double foo = 1 [default=-inf ];\n" - " required double foo = 1 [default= nan ];\n" - " required string foo = 1 [default='13\\001'];\n" - " required string foo = 1 [default='a' \"b\" \n \"c\"];\n" - " required bytes foo = 1 [default='14\\002'];\n" - " required bytes foo = 1 [default='a' \"b\" \n 'c'];\n" - " required bool foo = 1 [default=true ];\n" - " required Foo foo = 1 [default=FOO ];\n" - - " required int32 foo = 1 [default= 0x7FFFFFFF];\n" - " required int32 foo = 1 [default=-0x80000000];\n" - " required uint32 foo = 1 [default= 0xFFFFFFFF];\n" - " required int64 foo = 1 [default= 0x7FFFFFFFFFFFFFFF];\n" - " required int64 foo = 1 [default=-0x8000000000000000];\n" - " required uint64 foo = 1 [default= 0xFFFFFFFFFFFFFFFF];\n" - " required double foo = 1 [default= 0xabcd];\n" - "}\n", + "message TestMessage {\n" + " required int32 foo = 1 [default= 1 ];\n" + " required int32 foo = 1 [default= -2 ];\n" + " required int64 foo = 1 [default= 3 ];\n" + " required int64 foo = 1 [default= -4 ];\n" + " required uint32 foo = 1 [default= 5 ];\n" + " required uint64 foo = 1 [default= 6 ];\n" + " required float foo = 1 [default= 7.5];\n" + " required float foo = 1 [default= -8.5];\n" + " required float foo = 1 [default= 9 ];\n" + " required double foo = 1 [default= 10.5];\n" + " required double foo = 1 [default=-11.5];\n" + " required double foo = 1 [default= 12 ];\n" + " required double foo = 1 [default= inf ];\n" + " required double foo = 1 [default=-inf ];\n" + " required double foo = 1 [default= nan ];\n" + " required string foo = 1 [default='13\\001'];\n" + " required string foo = 1 [default='a' \"b\" \n \"c\"];\n" + " required bytes foo = 1 [default='14\\002'];\n" + " required bytes foo = 1 [default='a' \"b\" \n 'c'];\n" + " required bool foo = 1 [default=true ];\n" + " required Foo foo = 1 [default=FOO ];\n" + + " required int32 foo = 1 [default= 0x7FFFFFFF];\n" + " required int32 foo = 1 [default=-0x80000000];\n" + " required uint32 foo = 1 [default= 0xFFFFFFFF];\n" + " required int64 foo = 1 [default= 0x7FFFFFFFFFFFFFFF];\n" + " required int64 foo = 1 [default=-0x8000000000000000];\n" + " required uint64 foo = 1 [default= 0xFFFFFFFFFFFFFFFF];\n" + " required double foo = 1 [default= 0xabcd];\n" + "}\n", #define ETC "name:\"foo\" label:LABEL_REQUIRED number:1" - "message_type {" - " name: \"TestMessage\"" - " field { type:TYPE_INT32 default_value:\"1\" " ETC " }" - " field { type:TYPE_INT32 default_value:\"-2\" " ETC " }" - " field { type:TYPE_INT64 default_value:\"3\" " ETC " }" - " field { type:TYPE_INT64 default_value:\"-4\" " ETC " }" - " field { type:TYPE_UINT32 default_value:\"5\" " ETC " }" - " field { type:TYPE_UINT64 default_value:\"6\" " ETC " }" - " field { type:TYPE_FLOAT default_value:\"7.5\" " ETC " }" - " field { type:TYPE_FLOAT default_value:\"-8.5\" " ETC " }" - " field { type:TYPE_FLOAT default_value:\"9\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"10.5\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"-11.5\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"12\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"inf\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"-inf\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"nan\" " ETC " }" - " field { type:TYPE_STRING default_value:\"13\\001\" " ETC " }" - " field { type:TYPE_STRING default_value:\"abc\" " ETC " }" - " field { type:TYPE_BYTES default_value:\"14\\\\002\" " ETC " }" - " field { type:TYPE_BYTES default_value:\"abc\" " ETC " }" - " field { type:TYPE_BOOL default_value:\"true\" " ETC " }" - " field { type_name:\"Foo\" default_value:\"FOO\" " ETC " }" - - " field {" - " type:TYPE_INT32 default_value:\"2147483647\" " ETC - " }" - " field {" - " type:TYPE_INT32 default_value:\"-2147483648\" " ETC - " }" - " field {" - " type:TYPE_UINT32 default_value:\"4294967295\" " ETC - " }" - " field {" - " type:TYPE_INT64 default_value:\"9223372036854775807\" " ETC - " }" - " field {" - " type:TYPE_INT64 default_value:\"-9223372036854775808\" " ETC - " }" - " field {" - " type:TYPE_UINT64 default_value:\"18446744073709551615\" " ETC - " }" - " field {" - " type:TYPE_DOUBLE default_value:\"43981\" " ETC - " }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " field { type:TYPE_INT32 default_value:\"1\" " ETC + " }" + " field { type:TYPE_INT32 default_value:\"-2\" " ETC + " }" + " field { type:TYPE_INT64 default_value:\"3\" " ETC + " }" + " field { type:TYPE_INT64 default_value:\"-4\" " ETC + " }" + " field { type:TYPE_UINT32 default_value:\"5\" " ETC + " }" + " field { type:TYPE_UINT64 default_value:\"6\" " ETC + " }" + " field { type:TYPE_FLOAT default_value:\"7.5\" " ETC + " }" + " field { type:TYPE_FLOAT default_value:\"-8.5\" " ETC + " }" + " field { type:TYPE_FLOAT default_value:\"9\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"10.5\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"-11.5\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"12\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"inf\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"-inf\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"nan\" " ETC + " }" + " field { type:TYPE_STRING default_value:\"13\\001\" " ETC + " }" + " field { type:TYPE_STRING default_value:\"abc\" " ETC + " }" + " field { type:TYPE_BYTES default_value:\"14\\\\002\" " ETC + " }" + " field { type:TYPE_BYTES default_value:\"abc\" " ETC + " }" + " field { type:TYPE_BOOL default_value:\"true\" " ETC + " }" + " field { type_name:\"Foo\" default_value:\"FOO\" " ETC + " }" + + " field {" + " type:TYPE_INT32 default_value:\"2147483647\" " ETC + " }" + " field {" + " type:TYPE_INT32 default_value:\"-2147483648\" " ETC + " }" + " field {" + " type:TYPE_UINT32 default_value:\"4294967295\" " ETC + " }" + " field {" + " type:TYPE_INT64 default_value:\"9223372036854775807\" " ETC + " }" + " field {" + " type:TYPE_INT64 default_value:\"-9223372036854775808\" " ETC + " }" + " field {" + " type:TYPE_UINT64 default_value:\"18446744073709551615\" " ETC + " }" + " field {" + " type:TYPE_DOUBLE default_value:\"43981\" " ETC + " }" + "}"); #undef ETC } TEST_F(ParseMessageTest, FieldJsonName) { ExpectParsesTo( - "message TestMessage {\n" - " optional string foo = 1 [json_name = \"@type\"];\n" - "}\n", - "message_type {" - " name: \"TestMessage\"" - " field {\n" - " name: \"foo\" label: LABEL_OPTIONAL type: TYPE_STRING number: 1" - " json_name: \"@type\"\n" - " }\n" - "}\n"); + "message TestMessage {\n" + " optional string foo = 1 [json_name = \"@type\"];\n" + "}\n", + "message_type {" + " name: \"TestMessage\"" + " field {\n" + " name: \"foo\" label: LABEL_OPTIONAL type: TYPE_STRING number: 1" + " json_name: \"@type\"\n" + " }\n" + "}\n"); } TEST_F(ParseMessageTest, FieldOptions) { ExpectParsesTo( - "message TestMessage {\n" - " optional string foo = 1\n" - " [ctype=CORD, (foo)=7, foo.(.bar.baz).qux.quux.(corge)=-33, \n" - " (quux)=\"x\040y\", (baz.qux)=hey];\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name: \"foo\" label: LABEL_OPTIONAL type: TYPE_STRING number: 1" - " options { uninterpreted_option: { name { name_part: \"ctype\" " - " is_extension: false } " - " identifier_value: \"CORD\" }" - " uninterpreted_option: { name { name_part: \"foo\" " - " is_extension: true } " - " positive_int_value: 7 }" - " uninterpreted_option: { name { name_part: \"foo\" " - " is_extension: false } " - " name { name_part: \".bar.baz\"" - " is_extension: true } " - " name { name_part: \"qux\" " - " is_extension: false } " - " name { name_part: \"quux\" " - " is_extension: false } " - " name { name_part: \"corge\" " - " is_extension: true } " - " negative_int_value: -33 }" - " uninterpreted_option: { name { name_part: \"quux\" " - " is_extension: true } " - " string_value: \"x y\" }" - " uninterpreted_option: { name { name_part: \"baz.qux\" " - " is_extension: true } " - " identifier_value: \"hey\" }" - " }" - " }" - "}"); + "message TestMessage {\n" + " optional string foo = 1\n" + " [ctype=CORD, (foo)=7, foo.(.bar.baz).qux.quux.(corge)=-33, \n" + " (quux)=\"x\040y\", (baz.qux)=hey];\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name: \"foo\" label: LABEL_OPTIONAL type: TYPE_STRING number: " + "1" + " options { uninterpreted_option: { name { name_part: \"ctype\" " + " is_extension: false " + "} " + " identifier_value: \"CORD\" " + "}" + " uninterpreted_option: { name { name_part: \"foo\" " + " is_extension: true } " + " positive_int_value: 7 }" + " uninterpreted_option: { name { name_part: \"foo\" " + " is_extension: false " + "} " + " name { name_part: " + "\".bar.baz\"" + " is_extension: true } " + " name { name_part: \"qux\" " + " is_extension: false " + "} " + " name { name_part: \"quux\" " + " is_extension: false " + "} " + " name { name_part: \"corge\" " + " is_extension: true } " + " negative_int_value: -33 }" + " uninterpreted_option: { name { name_part: \"quux\" " + " is_extension: true } " + " string_value: \"x y\" }" + " uninterpreted_option: { name { name_part: " + "\"baz.qux\" " + " is_extension: true } " + " identifier_value: \"hey\" }" + " }" + " }" + "}"); } TEST_F(ParseMessageTest, Oneof) { ExpectParsesTo( - "message TestMessage {\n" - " oneof foo {\n" - " int32 a = 1;\n" - " string b = 2;\n" - " TestMessage c = 3;\n" - " group D = 4 { optional int32 i = 5; }\n" - " }\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"a\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 " - " oneof_index:0 }" - " field { name:\"b\" label:LABEL_OPTIONAL type:TYPE_STRING number:2 " - " oneof_index:0 }" - " field { name:\"c\" label:LABEL_OPTIONAL type_name:\"TestMessage\" " - " number:3 oneof_index:0 }" - " field { name:\"d\" label:LABEL_OPTIONAL type:TYPE_GROUP " - " type_name:\"D\" number:4 oneof_index:0 }" - " oneof_decl {" - " name: \"foo\"" - " }" - " nested_type {" - " name: \"D\"" - " field { name:\"i\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 }" - " }" - "}"); + "message TestMessage {\n" + " oneof foo {\n" + " int32 a = 1;\n" + " string b = 2;\n" + " TestMessage c = 3;\n" + " group D = 4 { optional int32 i = 5; }\n" + " }\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"a\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 " + " oneof_index:0 }" + " field { name:\"b\" label:LABEL_OPTIONAL type:TYPE_STRING number:2 " + " oneof_index:0 }" + " field { name:\"c\" label:LABEL_OPTIONAL type_name:\"TestMessage\" " + " number:3 oneof_index:0 }" + " field { name:\"d\" label:LABEL_OPTIONAL type:TYPE_GROUP " + " type_name:\"D\" number:4 oneof_index:0 }" + " oneof_decl {" + " name: \"foo\"" + " }" + " nested_type {" + " name: \"D\"" + " field { name:\"i\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 }" + " }" + "}"); } TEST_F(ParseMessageTest, MultipleOneofs) { ExpectParsesTo( - "message TestMessage {\n" - " oneof foo {\n" - " int32 a = 1;\n" - " string b = 2;\n" - " }\n" - " oneof bar {\n" - " int32 c = 3;\n" - " string d = 4;\n" - " }\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"a\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 " - " oneof_index:0 }" - " field { name:\"b\" label:LABEL_OPTIONAL type:TYPE_STRING number:2 " - " oneof_index:0 }" - " field { name:\"c\" label:LABEL_OPTIONAL type:TYPE_INT32 number:3 " - " oneof_index:1 }" - " field { name:\"d\" label:LABEL_OPTIONAL type:TYPE_STRING number:4 " - " oneof_index:1 }" - " oneof_decl {" - " name: \"foo\"" - " }" - " oneof_decl {" - " name: \"bar\"" - " }" - "}"); + "message TestMessage {\n" + " oneof foo {\n" + " int32 a = 1;\n" + " string b = 2;\n" + " }\n" + " oneof bar {\n" + " int32 c = 3;\n" + " string d = 4;\n" + " }\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"a\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 " + " oneof_index:0 }" + " field { name:\"b\" label:LABEL_OPTIONAL type:TYPE_STRING number:2 " + " oneof_index:0 }" + " field { name:\"c\" label:LABEL_OPTIONAL type:TYPE_INT32 number:3 " + " oneof_index:1 }" + " field { name:\"d\" label:LABEL_OPTIONAL type:TYPE_STRING number:4 " + " oneof_index:1 }" + " oneof_decl {" + " name: \"foo\"" + " }" + " oneof_decl {" + " name: \"bar\"" + " }" + "}"); } TEST_F(ParseMessageTest, Maps) { ExpectParsesTo( - "message TestMessage {\n" - " map primitive_type_map = 1;\n" - " map composite_type_map = 2;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " nested_type {" - " name: \"PrimitiveTypeMapEntry\"" - " field { " - " name: \"key\" number: 1 label:LABEL_OPTIONAL" - " type:TYPE_INT32" - " }" - " field { " - " name: \"value\" number: 2 label:LABEL_OPTIONAL" - " type:TYPE_STRING" - " }" - " options { map_entry: true }" - " }" - " nested_type {" - " name: \"CompositeTypeMapEntry\"" - " field { " - " name: \"key\" number: 1 label:LABEL_OPTIONAL" - " type_name: \"KeyType\"" - " }" - " field { " - " name: \"value\" number: 2 label:LABEL_OPTIONAL" - " type_name: \"ValueType\"" - " }" - " options { map_entry: true }" - " }" - " field {" - " name: \"primitive_type_map\"" - " label: LABEL_REPEATED" - " type_name: \"PrimitiveTypeMapEntry\"" - " number: 1" - " }" - " field {" - " name: \"composite_type_map\"" - " label: LABEL_REPEATED" - " type_name: \"CompositeTypeMapEntry\"" - " number: 2" - " }" - "}"); + "message TestMessage {\n" + " map primitive_type_map = 1;\n" + " map composite_type_map = 2;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " nested_type {" + " name: \"PrimitiveTypeMapEntry\"" + " field { " + " name: \"key\" number: 1 label:LABEL_OPTIONAL" + " type:TYPE_INT32" + " }" + " field { " + " name: \"value\" number: 2 label:LABEL_OPTIONAL" + " type:TYPE_STRING" + " }" + " options { map_entry: true }" + " }" + " nested_type {" + " name: \"CompositeTypeMapEntry\"" + " field { " + " name: \"key\" number: 1 label:LABEL_OPTIONAL" + " type_name: \"KeyType\"" + " }" + " field { " + " name: \"value\" number: 2 label:LABEL_OPTIONAL" + " type_name: \"ValueType\"" + " }" + " options { map_entry: true }" + " }" + " field {" + " name: \"primitive_type_map\"" + " label: LABEL_REPEATED" + " type_name: \"PrimitiveTypeMapEntry\"" + " number: 1" + " }" + " field {" + " name: \"composite_type_map\"" + " label: LABEL_REPEATED" + " type_name: \"CompositeTypeMapEntry\"" + " number: 2" + " }" + "}"); } TEST_F(ParseMessageTest, Group) { ExpectParsesTo( - "message TestMessage {\n" - " optional group TestGroup = 1 {};\n" - "}\n", + "message TestMessage {\n" + " optional group TestGroup = 1 {};\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " nested_type { name: \"TestGroup\" }" - " field { name:\"testgroup\" label:LABEL_OPTIONAL number:1" - " type:TYPE_GROUP type_name: \"TestGroup\" }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " nested_type { name: \"TestGroup\" }" + " field { name:\"testgroup\" label:LABEL_OPTIONAL number:1" + " type:TYPE_GROUP type_name: \"TestGroup\" }" + "}"); } TEST_F(ParseMessageTest, NestedMessage) { ExpectParsesTo( - "message TestMessage {\n" - " message Nested {}\n" - " optional Nested test_nested = 1;\n" - "}\n", + "message TestMessage {\n" + " message Nested {}\n" + " optional Nested test_nested = 1;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " nested_type { name: \"Nested\" }" - " field { name:\"test_nested\" label:LABEL_OPTIONAL number:1" - " type_name: \"Nested\" }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " nested_type { name: \"Nested\" }" + " field { name:\"test_nested\" label:LABEL_OPTIONAL number:1" + " type_name: \"Nested\" }" + "}"); } TEST_F(ParseMessageTest, NestedEnum) { ExpectParsesTo( - "message TestMessage {\n" - " enum NestedEnum {}\n" - " optional NestedEnum test_enum = 1;\n" - "}\n", + "message TestMessage {\n" + " enum NestedEnum {}\n" + " optional NestedEnum test_enum = 1;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " enum_type { name: \"NestedEnum\" }" - " field { name:\"test_enum\" label:LABEL_OPTIONAL number:1" - " type_name: \"NestedEnum\" }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " enum_type { name: \"NestedEnum\" }" + " field { name:\"test_enum\" label:LABEL_OPTIONAL number:1" + " type_name: \"NestedEnum\" }" + "}"); } TEST_F(ParseMessageTest, ReservedRange) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1;\n" - " reserved 2, 15, 9 to 11, 3, 20 to max;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - " reserved_range { start:2 end:3 }" - " reserved_range { start:15 end:16 }" - " reserved_range { start:9 end:12 }" - " reserved_range { start:3 end:4 }" - " reserved_range { start:20 end:536870912 }" - "}"); + "message TestMessage {\n" + " required int32 foo = 1;\n" + " reserved 2, 15, 9 to 11, 3, 20 to max;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + " reserved_range { start:2 end:3 }" + " reserved_range { start:15 end:16 }" + " reserved_range { start:9 end:12 }" + " reserved_range { start:3 end:4 }" + " reserved_range { start:20 end:536870912 }" + "}"); } TEST_F(ParseMessageTest, ReservedRangeOnMessageSet) { ExpectParsesTo( - "message TestMessage {\n" - " option message_set_wire_format = true;\n" - " reserved 20 to max;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"message_set_wire_format\"" - " is_extension: false" - " }" - " identifier_value: \"true\"" - " }" - " }" - " reserved_range { start:20 end:2147483647 }" - "}"); + "message TestMessage {\n" + " option message_set_wire_format = true;\n" + " reserved 20 to max;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"message_set_wire_format\"" + " is_extension: false" + " }" + " identifier_value: \"true\"" + " }" + " }" + " reserved_range { start:20 end:2147483647 }" + "}"); } TEST_F(ParseMessageTest, ReservedNames) { ExpectParsesTo( - "message TestMessage {\n" - " reserved \"foo\", \"bar\";\n" - "}\n", + "message TestMessage {\n" + " reserved \"foo\", \"bar\";\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - "}"); + "message_type {" + " name: \"TestMessage\"" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + "}"); } TEST_F(ParseMessageTest, ExtensionRange) { ExpectParsesTo( - "message TestMessage {\n" - " extensions 10 to 19;\n" - " extensions 30 to max;\n" - "}\n", + "message TestMessage {\n" + " extensions 10 to 19;\n" + " extensions 30 to max;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " extension_range { start:10 end:20 }" - " extension_range { start:30 end:536870912 }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " extension_range { start:10 end:20 }" + " extension_range { start:30 end:536870912 }" + "}"); } TEST_F(ParseMessageTest, ExtensionRangeWithOptions) { ExpectParsesTo( - "message TestMessage {\n" - " extensions 10 to 19 [(i) = 5];\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " extension_range {" - " start:10" - " end:20" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - "}"); + "message TestMessage {\n" + " extensions 10 to 19 [(i) = 5];\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " extension_range {" + " start:10" + " end:20" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + "}"); } TEST_F(ParseMessageTest, CompoundExtensionRange) { ExpectParsesTo( - "message TestMessage {\n" - " extensions 2, 15, 9 to 11, 100 to max, 3;\n" - "}\n", + "message TestMessage {\n" + " extensions 2, 15, 9 to 11, 100 to max, 3;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " extension_range { start:2 end:3 }" - " extension_range { start:15 end:16 }" - " extension_range { start:9 end:12 }" - " extension_range { start:100 end:536870912 }" - " extension_range { start:3 end:4 }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " extension_range { start:2 end:3 }" + " extension_range { start:15 end:16 }" + " extension_range { start:9 end:12 }" + " extension_range { start:100 end:536870912 }" + " extension_range { start:3 end:4 }" + "}"); } TEST_F(ParseMessageTest, CompoundExtensionRangeWithOptions) { ExpectParsesTo( - "message TestMessage {\n" - " extensions 2, 15, 9 to 11, 100 to max, 3 [(i) = 5];\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " extension_range {" - " start:2" - " end:3" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - " extension_range {" - " start:15" - " end:16" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - " extension_range {" - " start:9" - " end:12" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - " extension_range {" - " start:100" - " end:536870912" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - " extension_range {" - " start:3" - " end:4" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - "}"); + "message TestMessage {\n" + " extensions 2, 15, 9 to 11, 100 to max, 3 [(i) = 5];\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " extension_range {" + " start:2" + " end:3" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:15" + " end:16" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:9" + " end:12" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:100" + " end:536870912" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:3" + " end:4" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + "}"); } TEST_F(ParseMessageTest, LargerMaxForMessageSetWireFormatMessages) { @@ -846,77 +936,79 @@ TEST_F(ParseMessageTest, LargerMaxForMessageSetWireFormatMessages) { // extension numbers, as the numbers are not encoded as int32 field values // rather than tags. ExpectParsesTo( - "message TestMessage {\n" - " extensions 4 to max;\n" - " option message_set_wire_format = true;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " extension_range { start:4 end: 0x7fffffff }" - " options {\n" - " uninterpreted_option { \n" - " name {\n" - " name_part: \"message_set_wire_format\"\n" - " is_extension: false\n" - " }\n" - " identifier_value: \"true\"\n" - " }\n" - " }\n" - "}"); + "message TestMessage {\n" + " extensions 4 to max;\n" + " option message_set_wire_format = true;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " extension_range { start:4 end: 0x7fffffff }" + " options {\n" + " uninterpreted_option { \n" + " name {\n" + " name_part: \"message_set_wire_format\"\n" + " is_extension: false\n" + " }\n" + " identifier_value: \"true\"\n" + " }\n" + " }\n" + "}"); } TEST_F(ParseMessageTest, Extensions) { ExpectParsesTo( - "extend Extendee1 { optional int32 foo = 12; }\n" - "extend Extendee2 { repeated TestMessage bar = 22; }\n", + "extend Extendee1 { optional int32 foo = 12; }\n" + "extend Extendee2 { repeated TestMessage bar = 22; }\n", - "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" - " extendee: \"Extendee1\" } " - "extension { name:\"bar\" label:LABEL_REPEATED number:22" - " type_name:\"TestMessage\" extendee: \"Extendee2\" }"); + "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" + " extendee: \"Extendee1\" } " + "extension { name:\"bar\" label:LABEL_REPEATED number:22" + " type_name:\"TestMessage\" extendee: \"Extendee2\" }"); } TEST_F(ParseMessageTest, ExtensionsInMessageScope) { ExpectParsesTo( - "message TestMessage {\n" - " extend Extendee1 { optional int32 foo = 12; }\n" - " extend Extendee2 { repeated TestMessage bar = 22; }\n" - "}\n", + "message TestMessage {\n" + " extend Extendee1 { optional int32 foo = 12; }\n" + " extend Extendee2 { repeated TestMessage bar = 22; }\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" - " extendee: \"Extendee1\" }" - " extension { name:\"bar\" label:LABEL_REPEATED number:22" - " type_name:\"TestMessage\" extendee: \"Extendee2\" }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 " + "number:12" + " extendee: \"Extendee1\" }" + " extension { name:\"bar\" label:LABEL_REPEATED number:22" + " type_name:\"TestMessage\" extendee: \"Extendee2\" }" + "}"); } TEST_F(ParseMessageTest, MultipleExtensionsOneExtendee) { ExpectParsesTo( - "extend Extendee1 {\n" - " optional int32 foo = 12;\n" - " repeated TestMessage bar = 22;\n" - "}\n", + "extend Extendee1 {\n" + " optional int32 foo = 12;\n" + " repeated TestMessage bar = 22;\n" + "}\n", - "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" - " extendee: \"Extendee1\" } " - "extension { name:\"bar\" label:LABEL_REPEATED number:22" - " type_name:\"TestMessage\" extendee: \"Extendee1\" }"); + "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" + " extendee: \"Extendee1\" } " + "extension { name:\"bar\" label:LABEL_REPEATED number:22" + " type_name:\"TestMessage\" extendee: \"Extendee1\" }"); } TEST_F(ParseMessageTest, OptionalLabelProto3) { ExpectParsesTo( - "syntax = \"proto3\";\n" - "message TestMessage {\n" - " int32 foo = 1;\n" - "}\n", + "syntax = \"proto3\";\n" + "message TestMessage {\n" + " int32 foo = 1;\n" + "}\n", - "syntax: \"proto3\" " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 } }"); + "syntax: \"proto3\" " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 } " + "}"); } // =================================================================== @@ -925,108 +1017,109 @@ typedef ParserTest ParseEnumTest; TEST_F(ParseEnumTest, SimpleEnum) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 0;\n" - "}\n", + "enum TestEnum {\n" + " FOO = 0;\n" + "}\n", - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"FOO\" number:0 }" - "}"); + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"FOO\" number:0 }" + "}"); } TEST_F(ParseEnumTest, Values) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 13;\n" - " BAR = -10;\n" - " BAZ = 500;\n" - " HEX_MAX = 0x7FFFFFFF;\n" - " HEX_MIN = -0x80000000;\n" - " INT_MAX = 2147483647;\n" - " INT_MIN = -2147483648;\n" - "}\n", - - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"FOO\" number:13 }" - " value { name:\"BAR\" number:-10 }" - " value { name:\"BAZ\" number:500 }" - " value { name:\"HEX_MAX\" number:2147483647 }" - " value { name:\"HEX_MIN\" number:-2147483648 }" - " value { name:\"INT_MAX\" number:2147483647 }" - " value { name:\"INT_MIN\" number:-2147483648 }" - "}"); + "enum TestEnum {\n" + " FOO = 13;\n" + " BAR = -10;\n" + " BAZ = 500;\n" + " HEX_MAX = 0x7FFFFFFF;\n" + " HEX_MIN = -0x80000000;\n" + " INT_MAX = 2147483647;\n" + " INT_MIN = -2147483648;\n" + "}\n", + + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"FOO\" number:13 }" + " value { name:\"BAR\" number:-10 }" + " value { name:\"BAZ\" number:500 }" + " value { name:\"HEX_MAX\" number:2147483647 }" + " value { name:\"HEX_MIN\" number:-2147483648 }" + " value { name:\"INT_MAX\" number:2147483647 }" + " value { name:\"INT_MIN\" number:-2147483648 }" + "}"); } TEST_F(ParseEnumTest, ValueOptions) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 13;\n" - " BAR = -10 [ (something.text) = 'abc' ];\n" - " BAZ = 500 [ (something.text) = 'def', other = 1 ];\n" - "}\n", - - "enum_type {" - " name: \"TestEnum\"" - " value { name: \"FOO\" number: 13 }" - " value { name: \"BAR\" number: -10 " - " options { " - " uninterpreted_option { " - " name { name_part: \"something.text\" is_extension: true } " - " string_value: \"abc\" " - " } " - " } " - " } " - " value { name: \"BAZ\" number: 500 " - " options { " - " uninterpreted_option { " - " name { name_part: \"something.text\" is_extension: true } " - " string_value: \"def\" " - " } " - " uninterpreted_option { " - " name { name_part: \"other\" is_extension: false } " - " positive_int_value: 1 " - " } " - " } " - " } " - "}"); + "enum TestEnum {\n" + " FOO = 13;\n" + " BAR = -10 [ (something.text) = 'abc' ];\n" + " BAZ = 500 [ (something.text) = 'def', other = 1 ];\n" + "}\n", + + "enum_type {" + " name: \"TestEnum\"" + " value { name: \"FOO\" number: 13 }" + " value { name: \"BAR\" number: -10 " + " options { " + " uninterpreted_option { " + " name { name_part: \"something.text\" is_extension: true } " + " string_value: \"abc\" " + " } " + " } " + " } " + " value { name: \"BAZ\" number: 500 " + " options { " + " uninterpreted_option { " + " name { name_part: \"something.text\" is_extension: true } " + " string_value: \"def\" " + " } " + " uninterpreted_option { " + " name { name_part: \"other\" is_extension: false } " + " positive_int_value: 1 " + " } " + " } " + " } " + "}"); } TEST_F(ParseEnumTest, ReservedRange) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 0;\n" - " reserved -2147483648, -6 to -4, -1 to 1, 2, 15, 9 to 11, 3, 20 to max;\n" - "}\n", - - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"FOO\" number:0 }" - " reserved_range { start:-2147483648 end:-2147483648 }" - " reserved_range { start:-6 end:-4 }" - " reserved_range { start:-1 end:1 }" - " reserved_range { start:2 end:2 }" - " reserved_range { start:15 end:15 }" - " reserved_range { start:9 end:11 }" - " reserved_range { start:3 end:3 }" - " reserved_range { start:20 end:2147483647 }" - "}"); + "enum TestEnum {\n" + " FOO = 0;\n" + " reserved -2147483648, -6 to -4, -1 to 1, 2, 15, 9 to 11, 3, 20 to " + "max;\n" + "}\n", + + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"FOO\" number:0 }" + " reserved_range { start:-2147483648 end:-2147483648 }" + " reserved_range { start:-6 end:-4 }" + " reserved_range { start:-1 end:1 }" + " reserved_range { start:2 end:2 }" + " reserved_range { start:15 end:15 }" + " reserved_range { start:9 end:11 }" + " reserved_range { start:3 end:3 }" + " reserved_range { start:20 end:2147483647 }" + "}"); } TEST_F(ParseEnumTest, ReservedNames) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 0;\n" - " reserved \"foo\", \"bar\";\n" - "}\n", + "enum TestEnum {\n" + " FOO = 0;\n" + " reserved \"foo\", \"bar\";\n" + "}\n", - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"FOO\" number:0 }" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - "}"); + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"FOO\" number:0 }" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + "}"); } // =================================================================== @@ -1035,80 +1128,76 @@ typedef ParserTest ParseServiceTest; TEST_F(ParseServiceTest, SimpleService) { ExpectParsesTo( - "service TestService {\n" - " rpc Foo(In) returns (Out);\n" - "}\n", + "service TestService {\n" + " rpc Foo(In) returns (Out);\n" + "}\n", - "service {" - " name: \"TestService\"" - " method { name:\"Foo\" input_type:\"In\" output_type:\"Out\" }" - "}"); + "service {" + " name: \"TestService\"" + " method { name:\"Foo\" input_type:\"In\" output_type:\"Out\" }" + "}"); } TEST_F(ParseServiceTest, MethodsAndStreams) { ExpectParsesTo( - "service TestService {\n" - " rpc Foo(In1) returns (Out1);\n" - " rpc Bar(In2) returns (Out2);\n" - " rpc Baz(In3) returns (Out3);\n" - "}\n", + "service TestService {\n" + " rpc Foo(In1) returns (Out1);\n" + " rpc Bar(In2) returns (Out2);\n" + " rpc Baz(In3) returns (Out3);\n" + "}\n", - "service {" - " name: \"TestService\"" - " method { name:\"Foo\" input_type:\"In1\" output_type:\"Out1\" }" - " method { name:\"Bar\" input_type:\"In2\" output_type:\"Out2\" }" - " method { name:\"Baz\" input_type:\"In3\" output_type:\"Out3\" }" - "}"); + "service {" + " name: \"TestService\"" + " method { name:\"Foo\" input_type:\"In1\" output_type:\"Out1\" }" + " method { name:\"Bar\" input_type:\"In2\" output_type:\"Out2\" }" + " method { name:\"Baz\" input_type:\"In3\" output_type:\"Out3\" }" + "}"); } - // =================================================================== // imports and packages typedef ParserTest ParseMiscTest; TEST_F(ParseMiscTest, ParseImport) { - ExpectParsesTo( - "import \"foo/bar/baz.proto\";\n", - "dependency: \"foo/bar/baz.proto\""); + ExpectParsesTo("import \"foo/bar/baz.proto\";\n", + "dependency: \"foo/bar/baz.proto\""); } TEST_F(ParseMiscTest, ParseMultipleImports) { ExpectParsesTo( - "import \"foo.proto\";\n" - "import \"bar.proto\";\n" - "import \"baz.proto\";\n", - "dependency: \"foo.proto\"" - "dependency: \"bar.proto\"" - "dependency: \"baz.proto\""); + "import \"foo.proto\";\n" + "import \"bar.proto\";\n" + "import \"baz.proto\";\n", + "dependency: \"foo.proto\"" + "dependency: \"bar.proto\"" + "dependency: \"baz.proto\""); } TEST_F(ParseMiscTest, ParsePublicImports) { ExpectParsesTo( - "import \"foo.proto\";\n" - "import public \"bar.proto\";\n" - "import \"baz.proto\";\n" - "import public \"qux.proto\";\n", - "dependency: \"foo.proto\"" - "dependency: \"bar.proto\"" - "dependency: \"baz.proto\"" - "dependency: \"qux.proto\"" - "public_dependency: 1 " - "public_dependency: 3 "); + "import \"foo.proto\";\n" + "import public \"bar.proto\";\n" + "import \"baz.proto\";\n" + "import public \"qux.proto\";\n", + "dependency: \"foo.proto\"" + "dependency: \"bar.proto\"" + "dependency: \"baz.proto\"" + "dependency: \"qux.proto\"" + "public_dependency: 1 " + "public_dependency: 3 "); } TEST_F(ParseMiscTest, ParsePackage) { - ExpectParsesTo( - "package foo.bar.baz;\n", - "package: \"foo.bar.baz\""); + ExpectParsesTo("package foo.bar.baz;\n", "package: \"foo.bar.baz\""); } TEST_F(ParseMiscTest, ParsePackageWithSpaces) { ExpectParsesTo( - "package foo . bar. \n" - " baz;\n", - "package: \"foo.bar.baz\""); + "package foo . bar. \n" + " baz;\n", + "package: \"foo.bar.baz\""); } // =================================================================== @@ -1116,17 +1205,17 @@ TEST_F(ParseMiscTest, ParsePackageWithSpaces) { TEST_F(ParseMiscTest, ParseFileOptions) { ExpectParsesTo( - "option java_package = \"com.google.foo\";\n" - "option optimize_for = CODE_SIZE;", + "option java_package = \"com.google.foo\";\n" + "option optimize_for = CODE_SIZE;", - "options {" - "uninterpreted_option { name { name_part: \"java_package\" " - " is_extension: false }" - " string_value: \"com.google.foo\"} " - "uninterpreted_option { name { name_part: \"optimize_for\" " - " is_extension: false }" - " identifier_value: \"CODE_SIZE\" } " - "}"); + "options {" + "uninterpreted_option { name { name_part: \"java_package\" " + " is_extension: false }" + " string_value: \"com.google.foo\"} " + "uninterpreted_option { name { name_part: \"optimize_for\" " + " is_extension: false }" + " identifier_value: \"CODE_SIZE\" } " + "}"); } // =================================================================== @@ -1151,341 +1240,335 @@ TEST_F(ParseErrorTest, MissingSyntaxIdentifier) { TEST_F(ParseErrorTest, UnknownSyntaxIdentifier) { ExpectHasEarlyExitErrors( - "syntax = \"no_such_syntax\";", - "0:9: Unrecognized syntax identifier \"no_such_syntax\". This parser " + "syntax = \"no_such_syntax\";", + "0:9: Unrecognized syntax identifier \"no_such_syntax\". This parser " "only recognizes \"proto2\" and \"proto3\".\n"); EXPECT_EQ("no_such_syntax", parser_->GetSyntaxIdentifier()); } TEST_F(ParseErrorTest, SimpleSyntaxError) { - ExpectHasErrors( - "message TestMessage @#$ { blah }", - "0:20: Expected \"{\".\n"); + ExpectHasErrors("message TestMessage @#$ { blah }", + "0:20: Expected \"{\".\n"); EXPECT_EQ("proto2", parser_->GetSyntaxIdentifier()); } TEST_F(ParseErrorTest, ExpectedTopLevel) { - ExpectHasErrors( - "blah;", - "0:0: Expected top-level statement (e.g. \"message\").\n"); + ExpectHasErrors("blah;", + "0:0: Expected top-level statement (e.g. \"message\").\n"); } TEST_F(ParseErrorTest, UnmatchedCloseBrace) { // This used to cause an infinite loop. Doh. - ExpectHasErrors( - "}", - "0:0: Expected top-level statement (e.g. \"message\").\n" - "0:0: Unmatched \"}\".\n"); + ExpectHasErrors("}", + "0:0: Expected top-level statement (e.g. \"message\").\n" + "0:0: Unmatched \"}\".\n"); } // ------------------------------------------------------------------- // Message errors TEST_F(ParseErrorTest, MessageMissingName) { - ExpectHasErrors( - "message {}", - "0:8: Expected message name.\n"); + ExpectHasErrors("message {}", "0:8: Expected message name.\n"); } TEST_F(ParseErrorTest, MessageMissingBody) { - ExpectHasErrors( - "message TestMessage;", - "0:19: Expected \"{\".\n"); + ExpectHasErrors("message TestMessage;", "0:19: Expected \"{\".\n"); } TEST_F(ParseErrorTest, EofInMessage) { ExpectHasErrors( - "message TestMessage {", - "0:21: Reached end of input in message definition (missing '}').\n"); + "message TestMessage {", + "0:21: Reached end of input in message definition (missing '}').\n"); } TEST_F(ParseErrorTest, MissingFieldNumber) { ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo;\n" - "}\n", - "1:20: Missing field number.\n"); + "message TestMessage {\n" + " optional int32 foo;\n" + "}\n", + "1:20: Missing field number.\n"); } TEST_F(ParseErrorTest, ExpectedFieldNumber) { ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo = ;\n" - "}\n", - "1:23: Expected field number.\n"); + "message TestMessage {\n" + " optional int32 foo = ;\n" + "}\n", + "1:23: Expected field number.\n"); } TEST_F(ParseErrorTest, FieldNumberOutOfRange) { ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo = 0x100000000;\n" - "}\n", - "1:23: Integer out of range.\n"); + "message TestMessage {\n" + " optional int32 foo = 0x100000000;\n" + "}\n", + "1:23: Integer out of range.\n"); } TEST_F(ParseErrorTest, MissingLabel) { ExpectHasErrors( - "message TestMessage {\n" - " int32 foo = 1;\n" - "}\n", - "1:2: Expected \"required\", \"optional\", or \"repeated\".\n"); + "message TestMessage {\n" + " int32 foo = 1;\n" + "}\n", + "1:2: Expected \"required\", \"optional\", or \"repeated\".\n"); } TEST_F(ParseErrorTest, ExpectedOptionName) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [];\n" - "}\n", - "1:27: Expected identifier.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [];\n" + "}\n", + "1:27: Expected identifier.\n"); } TEST_F(ParseErrorTest, NonExtensionOptionNameBeginningWithDot) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [.foo=1];\n" - "}\n", - "1:27: Expected identifier.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [.foo=1];\n" + "}\n", + "1:27: Expected identifier.\n"); } TEST_F(ParseErrorTest, DefaultValueTypeMismatch) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [default=true];\n" - "}\n", - "1:35: Expected integer for field default value.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [default=true];\n" + "}\n", + "1:35: Expected integer for field default value.\n"); } TEST_F(ParseErrorTest, DefaultValueNotBoolean) { ExpectHasErrors( - "message TestMessage {\n" - " optional bool foo = 1 [default=blah];\n" - "}\n", - "1:33: Expected \"true\" or \"false\".\n"); + "message TestMessage {\n" + " optional bool foo = 1 [default=blah];\n" + "}\n", + "1:33: Expected \"true\" or \"false\".\n"); } TEST_F(ParseErrorTest, DefaultValueNotString) { ExpectHasErrors( - "message TestMessage {\n" - " optional string foo = 1 [default=1];\n" - "}\n", - "1:35: Expected string for field default value.\n"); + "message TestMessage {\n" + " optional string foo = 1 [default=1];\n" + "}\n", + "1:35: Expected string for field default value.\n"); } TEST_F(ParseErrorTest, DefaultValueUnsignedNegative) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [default=-1];\n" - "}\n", - "1:36: Unsigned field can't have negative default value.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [default=-1];\n" + "}\n", + "1:36: Unsigned field can't have negative default value.\n"); } TEST_F(ParseErrorTest, DefaultValueTooLarge) { ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo = 1 [default= 0x80000000];\n" - " optional int32 foo = 1 [default=-0x80000001];\n" - " optional uint32 foo = 1 [default= 0x100000000];\n" - " optional int64 foo = 1 [default= 0x80000000000000000];\n" - " optional int64 foo = 1 [default=-0x80000000000000001];\n" - " optional uint64 foo = 1 [default= 0x100000000000000000];\n" - "}\n", - "1:36: Integer out of range.\n" - "2:36: Integer out of range.\n" - "3:36: Integer out of range.\n" - "4:36: Integer out of range.\n" - "5:36: Integer out of range.\n" - "6:36: Integer out of range.\n"); + "message TestMessage {\n" + " optional int32 foo = 1 [default= 0x80000000];\n" + " optional int32 foo = 1 [default=-0x80000001];\n" + " optional uint32 foo = 1 [default= 0x100000000];\n" + " optional int64 foo = 1 [default= 0x80000000000000000];\n" + " optional int64 foo = 1 [default=-0x80000000000000001];\n" + " optional uint64 foo = 1 [default= 0x100000000000000000];\n" + "}\n", + "1:36: Integer out of range.\n" + "2:36: Integer out of range.\n" + "3:36: Integer out of range.\n" + "4:36: Integer out of range.\n" + "5:36: Integer out of range.\n" + "6:36: Integer out of range.\n"); } TEST_F(ParseErrorTest, JsonNameNotString) { ExpectHasErrors( - "message TestMessage {\n" - " optional string foo = 1 [json_name=1];\n" - "}\n", - "1:37: Expected string for JSON name.\n"); + "message TestMessage {\n" + " optional string foo = 1 [json_name=1];\n" + "}\n", + "1:37: Expected string for JSON name.\n"); } TEST_F(ParseErrorTest, DuplicateJsonName) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [json_name=\"a\",json_name=\"b\"];\n" - "}\n", - "1:41: Already set option \"json_name\".\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [json_name=\"a\",json_name=\"b\"];\n" + "}\n", + "1:41: Already set option \"json_name\".\n"); } TEST_F(ParseErrorTest, EnumValueOutOfRange) { ExpectHasErrors( - "enum TestEnum {\n" - " HEX_TOO_BIG = 0x80000000;\n" - " HEX_TOO_SMALL = -0x80000001;\n" - " INT_TOO_BIG = 2147483648;\n" - " INT_TOO_SMALL = -2147483649;\n" - "}\n", - "1:19: Integer out of range.\n" - "2:19: Integer out of range.\n" - "3:19: Integer out of range.\n" - "4:19: Integer out of range.\n"); + "enum TestEnum {\n" + " HEX_TOO_BIG = 0x80000000;\n" + " HEX_TOO_SMALL = -0x80000001;\n" + " INT_TOO_BIG = 2147483648;\n" + " INT_TOO_SMALL = -2147483649;\n" + "}\n", + "1:19: Integer out of range.\n" + "2:19: Integer out of range.\n" + "3:19: Integer out of range.\n" + "4:19: Integer out of range.\n"); } TEST_F(ParseErrorTest, EnumAllowAliasFalse) { ExpectHasErrors( - "enum Foo {\n" - " option allow_alias = false;\n" - " BAR = 1;\n" - " BAZ = 2;\n" - "}\n", - "5:0: \"Foo\" declares 'option allow_alias = false;' which has no effect. " - "Please remove the declaration.\n"); + "enum Foo {\n" + " option allow_alias = false;\n" + " BAR = 1;\n" + " BAZ = 2;\n" + "}\n", + "5:0: \"Foo\" declares 'option allow_alias = false;' which has no " + "effect. " + "Please remove the declaration.\n"); } TEST_F(ParseErrorTest, UnnecessaryEnumAllowAlias) { ExpectHasErrors( - "enum Foo {\n" - " option allow_alias = true;\n" - " BAR = 1;\n" - " BAZ = 2;\n" - "}\n", - "5:0: \"Foo\" declares support for enum aliases but no enum values share " - "field numbers. Please remove the unnecessary 'option allow_alias = true;' " - "declaration.\n"); + "enum Foo {\n" + " option allow_alias = true;\n" + " BAR = 1;\n" + " BAZ = 2;\n" + "}\n", + "5:0: \"Foo\" declares support for enum aliases but no enum values share " + "field numbers. Please remove the unnecessary 'option allow_alias = " + "true;' " + "declaration.\n"); } TEST_F(ParseErrorTest, DefaultValueMissing) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [default=];\n" - "}\n", - "1:35: Expected integer for field default value.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [default=];\n" + "}\n", + "1:35: Expected integer for field default value.\n"); } TEST_F(ParseErrorTest, DefaultValueForGroup) { ExpectHasErrors( - "message TestMessage {\n" - " optional group Foo = 1 [default=blah] {}\n" - "}\n", - "1:34: Messages can't have default values.\n"); + "message TestMessage {\n" + " optional group Foo = 1 [default=blah] {}\n" + "}\n", + "1:34: Messages can't have default values.\n"); } TEST_F(ParseErrorTest, DuplicateDefaultValue) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [default=1,default=2];\n" - "}\n", - "1:37: Already set option \"default\".\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [default=1,default=2];\n" + "}\n", + "1:37: Already set option \"default\".\n"); } TEST_F(ParseErrorTest, MissingOneofName) { ExpectHasErrors( - "message TestMessage {\n" - " oneof {\n" - " int32 bar = 1;\n" - " }\n" - "}\n", - "1:8: Expected oneof name.\n"); + "message TestMessage {\n" + " oneof {\n" + " int32 bar = 1;\n" + " }\n" + "}\n", + "1:8: Expected oneof name.\n"); } TEST_F(ParseErrorTest, LabelInOneof) { ExpectHasErrors( - "message TestMessage {\n" - " oneof foo {\n" - " optional int32 bar = 1;\n" - " }\n" - "}\n", - "2:4: Fields in oneofs must not have labels (required / optional " + "message TestMessage {\n" + " oneof foo {\n" + " optional int32 bar = 1;\n" + " }\n" + "}\n", + "2:4: Fields in oneofs must not have labels (required / optional " "/ repeated).\n"); } TEST_F(ParseErrorTest, MapInOneof) { ExpectHasErrors( - "message TestMessage {\n" - " oneof foo {\n" - " map foo_map = 1;\n" - " map message_field = 2;\n" // a normal message field is OK - " }\n" - "}\n", - "2:7: Map fields are not allowed in oneofs.\n"); + "message TestMessage {\n" + " oneof foo {\n" + " map foo_map = 1;\n" + " map message_field = 2;\n" // a normal message field is OK + " }\n" + "}\n", + "2:7: Map fields are not allowed in oneofs.\n"); } TEST_F(ParseErrorTest, LabelForMap) { ExpectHasErrors( - "message TestMessage {\n" - " optional map int_map = 1;\n" - " required map int_map2 = 2;\n" - " repeated map int_map3 = 3;\n" - " optional map map_message = 4;\n" // a normal message field is OK - "}\n", - "1:14: Field labels (required/optional/repeated) are not allowed on map " - "fields.\n" - "2:14: Field labels (required/optional/repeated) are not allowed on map " - "fields.\n" - "3:14: Field labels (required/optional/repeated) are not allowed on map " - "fields.\n"); + "message TestMessage {\n" + " optional map int_map = 1;\n" + " required map int_map2 = 2;\n" + " repeated map int_map3 = 3;\n" + " optional map map_message = 4;\n" // a normal message field is OK + "}\n", + "1:14: Field labels (required/optional/repeated) are not allowed on map " + "fields.\n" + "2:14: Field labels (required/optional/repeated) are not allowed on map " + "fields.\n" + "3:14: Field labels (required/optional/repeated) are not allowed on map " + "fields.\n"); } TEST_F(ParseErrorTest, MalformedMaps) { ExpectHasErrors( - "message TestMessage {\n" - " map map_message = 1;\n" // a normal message field lacking label - " map str_map = 2;\n" - " map str_map2 = 3;\n" - " map<,string> str_map3 = 4;\n" - " map<> empty_map = 5;\n" - " map int_map = 1;\n" - "}", - "1:6: Expected \"required\", \"optional\", or \"repeated\".\n" - "2:12: Expected \",\".\n" - "3:13: Expected type name.\n" - "4:6: Expected type name.\n" - "5:6: Expected type name.\n" - "6:20: Expected \">\".\n" - "8:5: Map fields are not allowed to be extensions.\n"); + "message TestMessage {\n" + " map map_message = 1;\n" // a normal message field lacking label + " map str_map = 2;\n" + " map str_map2 = 3;\n" + " map<,string> str_map3 = 4;\n" + " map<> empty_map = 5;\n" + " map int_map = 1;\n" + "}", + "1:6: Expected \"required\", \"optional\", or \"repeated\".\n" + "2:12: Expected \",\".\n" + "3:13: Expected type name.\n" + "4:6: Expected type name.\n" + "5:6: Expected type name.\n" + "6:20: Expected \">\".\n" + "8:5: Map fields are not allowed to be extensions.\n"); } TEST_F(ParseErrorTest, GroupNotCapitalized) { ExpectHasErrors( - "message TestMessage {\n" - " optional group foo = 1 {}\n" - "}\n", - "1:17: Group names must start with a capital letter.\n"); + "message TestMessage {\n" + " optional group foo = 1 {}\n" + "}\n", + "1:17: Group names must start with a capital letter.\n"); } TEST_F(ParseErrorTest, GroupMissingBody) { ExpectHasErrors( - "message TestMessage {\n" - " optional group Foo = 1;\n" - "}\n", - "1:24: Missing group body.\n"); + "message TestMessage {\n" + " optional group Foo = 1;\n" + "}\n", + "1:24: Missing group body.\n"); } TEST_F(ParseErrorTest, ExtendingPrimitive) { - ExpectHasErrors( - "extend int32 { optional string foo = 4; }\n", - "0:7: Expected message type.\n"); + ExpectHasErrors("extend int32 { optional string foo = 4; }\n", + "0:7: Expected message type.\n"); } TEST_F(ParseErrorTest, ErrorInExtension) { ExpectHasErrors( - "message Foo { extensions 100 to 199; }\n" - "extend Foo { optional string foo; }\n", - "1:32: Missing field number.\n"); + "message Foo { extensions 100 to 199; }\n" + "extend Foo { optional string foo; }\n", + "1:32: Missing field number.\n"); } TEST_F(ParseErrorTest, MultipleParseErrors) { // When a statement has a parse error, the parser should be able to continue // parsing at the next statement. ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo;\n" - " !invalid statement ending in a block { blah blah { blah } blah }\n" - " optional int32 bar = 3 {}\n" - "}\n", - "1:20: Missing field number.\n" - "2:2: Expected \"required\", \"optional\", or \"repeated\".\n" - "2:2: Expected type name.\n" - "3:25: Expected \";\".\n"); + "message TestMessage {\n" + " optional int32 foo;\n" + " !invalid statement ending in a block { blah blah { blah } blah }\n" + " optional int32 bar = 3 {}\n" + "}\n", + "1:20: Missing field number.\n" + "2:2: Expected \"required\", \"optional\", or \"repeated\".\n" + "2:2: Expected type name.\n" + "3:25: Expected \";\".\n"); } TEST_F(ParseErrorTest, EofInAggregateValue) { @@ -1505,67 +1588,66 @@ TEST_F(ParseErrorTest, ExplicitOptionalLabelProto3) { "label, as fields are 'optional' by default.\n"); } - // ------------------------------------------------------------------- // Enum errors TEST_F(ParseErrorTest, EofInEnum) { ExpectHasErrors( - "enum TestEnum {", - "0:15: Reached end of input in enum definition (missing '}').\n"); + "enum TestEnum {", + "0:15: Reached end of input in enum definition (missing '}').\n"); } TEST_F(ParseErrorTest, EnumValueMissingNumber) { ExpectHasErrors( - "enum TestEnum {\n" - " FOO;\n" - "}\n", - "1:5: Missing numeric value for enum constant.\n"); + "enum TestEnum {\n" + " FOO;\n" + "}\n", + "1:5: Missing numeric value for enum constant.\n"); } TEST_F(ParseErrorTest, EnumReservedStandaloneMaxNotAllowed) { ExpectHasErrors( - "enum TestEnum {\n" - " FOO = 1;\n" - " reserved max;\n" - "}\n", - "2:11: Expected enum value or number range.\n"); + "enum TestEnum {\n" + " FOO = 1;\n" + " reserved max;\n" + "}\n", + "2:11: Expected enum value or number range.\n"); } TEST_F(ParseErrorTest, EnumReservedMixNameAndNumber) { ExpectHasErrors( - "enum TestEnum {\n" - " FOO = 1;\n" - " reserved 10, \"foo\";\n" - "}\n", - "2:15: Expected enum number range.\n"); + "enum TestEnum {\n" + " FOO = 1;\n" + " reserved 10, \"foo\";\n" + "}\n", + "2:15: Expected enum number range.\n"); } TEST_F(ParseErrorTest, EnumReservedPositiveNumberOutOfRange) { ExpectHasErrors( - "enum TestEnum {\n" - "FOO = 1;\n" - " reserved 2147483648;\n" - "}\n", - "2:11: Integer out of range.\n"); + "enum TestEnum {\n" + "FOO = 1;\n" + " reserved 2147483648;\n" + "}\n", + "2:11: Integer out of range.\n"); } TEST_F(ParseErrorTest, EnumReservedNegativeNumberOutOfRange) { ExpectHasErrors( - "enum TestEnum {\n" - "FOO = 1;\n" - " reserved -2147483649;\n" - "}\n", - "2:12: Integer out of range.\n"); + "enum TestEnum {\n" + "FOO = 1;\n" + " reserved -2147483649;\n" + "}\n", + "2:12: Integer out of range.\n"); } TEST_F(ParseErrorTest, EnumReservedMissingQuotes) { ExpectHasErrors( - "enum TestEnum {\n" - " FOO = 1;\n" - " reserved foo;\n" - "}\n", - "2:11: Expected enum value or number range.\n"); + "enum TestEnum {\n" + " FOO = 1;\n" + " reserved foo;\n" + "}\n", + "2:11: Expected enum value or number range.\n"); } // ------------------------------------------------------------------- @@ -1573,90 +1655,89 @@ TEST_F(ParseErrorTest, EnumReservedMissingQuotes) { TEST_F(ParseErrorTest, ReservedStandaloneMaxNotAllowed) { ExpectHasErrors( - "message Foo {\n" - " reserved max;\n" - "}\n", - "1:11: Expected field name or number range.\n"); + "message Foo {\n" + " reserved max;\n" + "}\n", + "1:11: Expected field name or number range.\n"); } TEST_F(ParseErrorTest, ReservedMixNameAndNumber) { ExpectHasErrors( - "message Foo {\n" - " reserved 10, \"foo\";\n" - "}\n", - "1:15: Expected field number range.\n"); + "message Foo {\n" + " reserved 10, \"foo\";\n" + "}\n", + "1:15: Expected field number range.\n"); } TEST_F(ParseErrorTest, ReservedMissingQuotes) { ExpectHasErrors( - "message Foo {\n" - " reserved foo;\n" - "}\n", - "1:11: Expected field name or number range.\n"); + "message Foo {\n" + " reserved foo;\n" + "}\n", + "1:11: Expected field name or number range.\n"); } TEST_F(ParseErrorTest, ReservedNegativeNumber) { ExpectHasErrors( - "message Foo {\n" - " reserved -10;\n" - "}\n", - "1:11: Expected field name or number range.\n"); + "message Foo {\n" + " reserved -10;\n" + "}\n", + "1:11: Expected field name or number range.\n"); } TEST_F(ParseErrorTest, ReservedNumberOutOfRange) { ExpectHasErrors( - "message Foo {\n" - " reserved 2147483648;\n" - "}\n", - "1:11: Integer out of range.\n"); + "message Foo {\n" + " reserved 2147483648;\n" + "}\n", + "1:11: Integer out of range.\n"); } - // ------------------------------------------------------------------- // Service errors TEST_F(ParseErrorTest, EofInService) { ExpectHasErrors( - "service TestService {", - "0:21: Reached end of input in service definition (missing '}').\n"); + "service TestService {", + "0:21: Reached end of input in service definition (missing '}').\n"); } TEST_F(ParseErrorTest, ServiceMethodPrimitiveParams) { ExpectHasErrors( - "service TestService {\n" - " rpc Foo(int32) returns (string);\n" - "}\n", - "1:10: Expected message type.\n" - "1:26: Expected message type.\n"); + "service TestService {\n" + " rpc Foo(int32) returns (string);\n" + "}\n", + "1:10: Expected message type.\n" + "1:26: Expected message type.\n"); } TEST_F(ParseErrorTest, EofInMethodOptions) { ExpectHasErrors( - "service TestService {\n" - " rpc Foo(Bar) returns(Bar) {", - "1:29: Reached end of input in method options (missing '}').\n" - "1:29: Reached end of input in service definition (missing '}').\n"); + "service TestService {\n" + " rpc Foo(Bar) returns(Bar) {", + "1:29: Reached end of input in method options (missing '}').\n" + "1:29: Reached end of input in service definition (missing '}').\n"); } TEST_F(ParseErrorTest, PrimitiveMethodInput) { ExpectHasErrors( - "service TestService {\n" - " rpc Foo(int32) returns(Bar);\n" - "}\n", - "1:10: Expected message type.\n"); + "service TestService {\n" + " rpc Foo(int32) returns(Bar);\n" + "}\n", + "1:10: Expected message type.\n"); } TEST_F(ParseErrorTest, MethodOptionTypeError) { // This used to cause an infinite loop. ExpectHasErrors( - "message Baz {}\n" - "service Foo {\n" - " rpc Bar(Baz) returns(Baz) { option invalid syntax; }\n" - "}\n", - "2:45: Expected \"=\".\n"); + "message Baz {}\n" + "service Foo {\n" + " rpc Bar(Baz) returns(Baz) { option invalid syntax; }\n" + "}\n", + "2:45: Expected \"=\".\n"); } @@ -1664,16 +1745,15 @@ TEST_F(ParseErrorTest, MethodOptionTypeError) { // Import and package errors TEST_F(ParseErrorTest, ImportNotQuoted) { - ExpectHasErrors( - "import foo;\n", - "0:7: Expected a string naming the file to import.\n"); + ExpectHasErrors("import foo;\n", + "0:7: Expected a string naming the file to import.\n"); } TEST_F(ParseErrorTest, MultiplePackagesInFile) { ExpectHasErrors( - "package foo;\n" - "package bar;\n", - "1:0: Multiple package definitions.\n"); + "package foo;\n" + "package bar;\n", + "1:0: Multiple package definitions.\n"); } // =================================================================== @@ -1692,149 +1772,272 @@ TEST_F(ParserValidationErrorTest, PackageNameError) { // Now try to define it as a package. ExpectHasValidationErrors( - "package foo.bar;", - "0:8: \"foo\" is already defined (as something other than a package) " + "package foo.bar;", + "0:0: \"foo\" is already defined (as something other than a package) " "in file \"bar.proto\".\n"); } +TEST_F(ParserValidationErrorTest, ImportUnloadedError) { + ExpectHasValidationErrors( + "package test;\n" + "\n" + "import \"unloaded.proto\";", + "2:0: Import \"unloaded.proto\" has not been loaded.\n"); +} + +TEST_F(ParserValidationErrorTest, ImportTwice) { + FileDescriptorProto other_file; + other_file.set_name("bar.proto"); + other_file.add_message_type()->set_name("foo"); + EXPECT_TRUE(pool_.BuildFile(other_file) != nullptr); + + ExpectHasValidationErrors( + "package test;\n" + "\n" + "import \"bar.proto\";\n" + " import \"bar.proto\";", + "3:2: Import \"bar.proto\" was listed twice.\n"); +} + +TEST_F(ParserValidationErrorTest, DuplicateFileError) { + FileDescriptorProto other_file; + other_file.set_name("foo.proto"); + EXPECT_TRUE(pool_.BuildFile(other_file) != nullptr); + + ExpectHasValidationErrors( + "package test;", "0:0: A file with this name is already in the pool.\n"); +} + TEST_F(ParserValidationErrorTest, MessageNameError) { ExpectHasValidationErrors( - "message Foo {}\n" - "message Foo {}\n", - "1:8: \"Foo\" is already defined.\n"); + "message Foo {}\n" + "message Foo {}\n", + "1:8: \"Foo\" is already defined.\n"); } TEST_F(ParserValidationErrorTest, FieldNameError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional int32 bar = 1;\n" - " optional int32 bar = 2;\n" - "}\n", - "2:17: \"bar\" is already defined in \"Foo\".\n"); + "message Foo {\n" + " optional int32 bar = 1;\n" + " optional int32 bar = 2;\n" + "}\n", + "2:17: \"bar\" is already defined in \"Foo\".\n"); } TEST_F(ParserValidationErrorTest, FieldTypeError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional Baz bar = 1;\n" - "}\n", - "1:11: \"Baz\" is not defined.\n"); + "message Foo {\n" + " optional Baz bar = 1;\n" + "}\n", + "1:11: \"Baz\" is not defined.\n"); } TEST_F(ParserValidationErrorTest, FieldNumberError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional int32 bar = 0;\n" - "}\n", - "1:23: Field numbers must be positive integers.\n"); + "message Foo {\n" + " optional int32 bar = 0;\n" + "}\n", + "1:23: Field numbers must be positive integers.\n"); } TEST_F(ParserValidationErrorTest, FieldExtendeeError) { + ExpectHasValidationErrors("extend Baz { optional int32 bar = 1; }\n", + "0:7: \"Baz\" is not defined.\n"); +} + +TEST_F(ParserValidationErrorTest, ExtensionJsonNameError) { ExpectHasValidationErrors( - "extend Baz { optional int32 bar = 1; }\n", - "0:7: \"Baz\" is not defined.\n"); + "message TestMessage {\n" + " extensions 1 to 100;\n" + "}\n" + "extend TestMessage {\n" + " optional int32 foo = 12 [json_name = \"bar\"];\n" + "}", + "4:27: option json_name is not allowed on extension fields.\n"); } TEST_F(ParserValidationErrorTest, FieldDefaultValueError) { ExpectHasValidationErrors( - "enum Baz { QUX = 1; }\n" - "message Foo {\n" - " optional Baz bar = 1 [default=NO_SUCH_VALUE];\n" - "}\n", - "2:32: Enum type \"Baz\" has no value named \"NO_SUCH_VALUE\".\n"); + "enum Baz { QUX = 1; }\n" + "message Foo {\n" + " optional Baz bar = 1 [default=NO_SUCH_VALUE];\n" + "}\n", + "2:32: Enum type \"Baz\" has no value named \"NO_SUCH_VALUE\".\n"); } TEST_F(ParserValidationErrorTest, FileOptionNameError) { ExpectHasValidationErrors( - "option foo = 5;", - "0:7: Option \"foo\" unknown. Ensure that your proto definition file " - "imports the proto which defines the option.\n"); + "option foo = 5;", + "0:7: Option \"foo\" unknown. Ensure that your proto definition file " + "imports the proto which defines the option.\n"); } TEST_F(ParserValidationErrorTest, FileOptionValueError) { ExpectHasValidationErrors( - "option java_outer_classname = 5;", - "0:30: Value must be quoted string for string option " - "\"google.protobuf.FileOptions.java_outer_classname\".\n"); + "option java_outer_classname = 5;", + "0:30: Value must be quoted string for string option " + "\"google.protobuf.FileOptions.java_outer_classname\".\n"); } TEST_F(ParserValidationErrorTest, FieldOptionNameError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional bool bar = 1 [foo=1];\n" - "}\n", - "1:25: Option \"foo\" unknown. Ensure that your proto definition file " - "imports the proto which defines the option.\n"); + "message Foo {\n" + " optional bool bar = 1 [foo=1];\n" + "}\n", + "1:25: Option \"foo\" unknown. Ensure that your proto definition file " + "imports the proto which defines the option.\n"); } TEST_F(ParserValidationErrorTest, FieldOptionValueError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional int32 bar = 1 [ctype=1];\n" - "}\n", - "1:32: Value must be identifier for enum-valued option " - "\"google.protobuf.FieldOptions.ctype\".\n"); + "message Foo {\n" + " optional int32 bar = 1 [ctype=1];\n" + "}\n", + "1:32: Value must be identifier for enum-valued option " + "\"google.protobuf.FieldOptions.ctype\".\n"); } TEST_F(ParserValidationErrorTest, ExtensionRangeNumberError) { ExpectHasValidationErrors( - "message Foo {\n" - " extensions 0;\n" - "}\n", - "1:13: Extension numbers must be positive integers.\n"); + "message Foo {\n" + " extensions 0;\n" + "}\n", + "1:13: Extension numbers must be positive integers.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3ExtensionError) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message Foo { \n" + " extensions 100 to 199;\n" + "}\n" + "extend Foo { string foo = 101; }\n", + "4:7: Extensions in proto3 are only allowed for defining options.\n" + "2:13: Extension ranges are not allowed in proto3.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3MessageSet) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message Foo { \n" + " option message_set_wire_format = true;\n" + "}\n", + "1:8: MessageSet is not supported in proto3.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3Required) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message Foo { \n" + " required int32 field = 1;" + "}\n", + "2:11: Required fields are not allowed in proto3.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3Default) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message Foo { \n" + " int32 field = 1 [default = 12];" + "}\n", + "2:29: Explicit default values are not allowed in proto3.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3JsonConflictError) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message TestMessage {\n" + " uint32 foo = 1;\n" + " uint32 Foo = 2;\n" + "}\n", + "3:9: The JSON camel-case name of field \"Foo\" conflicts with field " + "\"foo\". This is not allowed in proto3.\n"); } TEST_F(ParserValidationErrorTest, EnumNameError) { ExpectHasValidationErrors( - "enum Foo {A = 1;}\n" - "enum Foo {B = 1;}\n", - "1:5: \"Foo\" is already defined.\n"); + "enum Foo {A = 1;}\n" + "enum Foo {B = 1;}\n", + "1:5: \"Foo\" is already defined.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3EnumError) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "enum Foo {A = 1;}\n", + "1:14: The first enum value must be zero in proto3.\n"); } TEST_F(ParserValidationErrorTest, EnumValueNameError) { ExpectHasValidationErrors( - "enum Foo {\n" - " BAR = 1;\n" - " BAR = 1;\n" - "}\n", - "2:2: \"BAR\" is already defined.\n"); + "enum Foo {\n" + " BAR = 1;\n" + " BAR = 1;\n" + "}\n", + "2:2: \"BAR\" is already defined.\n"); +} + +TEST_F(ParserValidationErrorTest, EnumValueAliasError) { + ExpectHasValidationErrors( + "enum Foo {\n" + " BAR = 1;\n" + " BAZ = 1;\n" + "}\n", + "2:8: \"BAZ\" uses the same enum value as \"BAR\". If this is " + "intended, set 'option allow_alias = true;' to the enum " + "definition.\n"); +} + +TEST_F(ParserValidationErrorTest, ExplicitlyMapEntryError) { + ExpectHasValidationErrors( + "message Foo {\n" + " message ValueEntry {\n" + " option map_entry = true;\n" + " optional int32 key = 1;\n" + " optional int32 value = 2;\n" + " extensions 99 to 999;\n" + " }\n" + " repeated ValueEntry value = 1;\n" + "}", + "7:11: map_entry should not be set explicitly. Use " + "map instead.\n"); } TEST_F(ParserValidationErrorTest, ServiceNameError) { ExpectHasValidationErrors( - "service Foo {}\n" - "service Foo {}\n", - "1:8: \"Foo\" is already defined.\n"); + "service Foo {}\n" + "service Foo {}\n", + "1:8: \"Foo\" is already defined.\n"); } TEST_F(ParserValidationErrorTest, MethodNameError) { ExpectHasValidationErrors( - "message Baz {}\n" - "service Foo {\n" - " rpc Bar(Baz) returns(Baz);\n" - " rpc Bar(Baz) returns(Baz);\n" - "}\n", - "3:6: \"Bar\" is already defined in \"Foo\".\n"); + "message Baz {}\n" + "service Foo {\n" + " rpc Bar(Baz) returns(Baz);\n" + " rpc Bar(Baz) returns(Baz);\n" + "}\n", + "3:6: \"Bar\" is already defined in \"Foo\".\n"); } TEST_F(ParserValidationErrorTest, MethodInputTypeError) { ExpectHasValidationErrors( - "message Baz {}\n" - "service Foo {\n" - " rpc Bar(Qux) returns(Baz);\n" - "}\n", - "2:10: \"Qux\" is not defined.\n"); + "message Baz {}\n" + "service Foo {\n" + " rpc Bar(Qux) returns(Baz);\n" + "}\n", + "2:10: \"Qux\" is not defined.\n"); } TEST_F(ParserValidationErrorTest, MethodOutputTypeError) { ExpectHasValidationErrors( - "message Baz {}\n" - "service Foo {\n" - " rpc Bar(Baz) returns(Qux);\n" - "}\n", - "2:23: \"Qux\" is not defined.\n"); + "message Baz {}\n" + "service Foo {\n" + " rpc Bar(Baz) returns(Qux);\n" + "}\n", + "2:23: \"Qux\" is not defined.\n"); } @@ -1849,16 +2052,16 @@ TEST_F(ParserValidationErrorTest, ResovledUndefinedError) { // Define "foo.base" and try "base.bar". // "base.bar" is resolved to "foo.base.bar" which is not defined. ExpectHasValidationErrors( - "package foo.base;\n" - "import \"base.proto\";\n" - "message qux {\n" - " optional base.bar baz = 1;\n" - " optional .base.bar quz = 2;\n" - "}\n", - "3:11: \"base.bar\" is resolved to \"foo.base.bar\"," - " which is not defined. The innermost scope is searched first " - "in name resolution. Consider using a leading '.'(i.e., \".base.bar\")" - " to start from the outermost scope.\n"); + "package foo.base;\n" + "import \"base.proto\";\n" + "message qux {\n" + " optional base.bar baz = 1;\n" + " optional .base.bar quz = 2;\n" + "}\n", + "3:11: \"base.bar\" is resolved to \"foo.base.bar\"," + " which is not defined. The innermost scope is searched first " + "in name resolution. Consider using a leading '.'(i.e., \".base.bar\")" + " to start from the outermost scope.\n"); } TEST_F(ParserValidationErrorTest, ResovledUndefinedOptionError) { @@ -1930,35 +2133,35 @@ class CompareDescriptorNames { }; // Sorts nested DescriptorProtos of a DescriptoProto, by name. -void SortMessages(DescriptorProto *descriptor_proto) { +void SortMessages(DescriptorProto* descriptor_proto) { int size = descriptor_proto->nested_type_size(); // recursively sort; we can't guarantee the order of nested messages either for (int i = 0; i < size; ++i) { SortMessages(descriptor_proto->mutable_nested_type(i)); } - DescriptorProto **data = - descriptor_proto->mutable_nested_type()->mutable_data(); + DescriptorProto** data = + descriptor_proto->mutable_nested_type()->mutable_data(); std::sort(data, data + size, CompareDescriptorNames()); } // Sorts DescriptorProtos belonging to a FileDescriptorProto, by name. -void SortMessages(FileDescriptorProto *file_descriptor_proto) { +void SortMessages(FileDescriptorProto* file_descriptor_proto) { int size = file_descriptor_proto->message_type_size(); // recursively sort; we can't guarantee the order of nested messages either for (int i = 0; i < size; ++i) { SortMessages(file_descriptor_proto->mutable_message_type(i)); } - DescriptorProto **data = - file_descriptor_proto->mutable_message_type()->mutable_data(); + DescriptorProto** data = + file_descriptor_proto->mutable_message_type()->mutable_data(); std::sort(data, data + size, CompareDescriptorNames()); } // Strips the message and enum field type names for comparison purpose only. void StripFieldTypeName(DescriptorProto* proto) { for (int i = 0; i < proto->field_size(); ++i) { - string type_name = proto->field(i).type_name(); - string::size_type pos = type_name.find_last_of("."); - if (pos != string::npos) { + std::string type_name = proto->field(i).type_name(); + std::string::size_type pos = type_name.find_last_of("."); + if (pos != std::string::npos) { proto->mutable_field(i)->mutable_type_name()->assign( type_name.begin() + pos + 1, type_name.end()); } @@ -1976,21 +2179,20 @@ void StripFieldTypeName(FileDescriptorProto* file_proto) { TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) { const FileDescriptor* original_file = - protobuf_unittest::TestAllTypes::descriptor()->file(); + protobuf_unittest::TestAllTypes::descriptor()->file(); FileDescriptorProto expected; original_file->CopyTo(&expected); // Get the DebugString of the unittest.proto FileDecriptor, which includes // all other descriptor types - string debug_string = original_file->DebugString(); + std::string debug_string = original_file->DebugString(); // Parse the debug string SetupParser(debug_string.c_str()); FileDescriptorProto parsed; parser_->Parse(input_.get(), &parsed); EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type); - ASSERT_EQ("", error_collector_.text_) - << "Failed to parse:\n" << debug_string; + ASSERT_EQ("", error_collector_.text_) << "Failed to parse:\n" << debug_string; // We now have a FileDescriptorProto, but to compare with the expected we // need to link to a FileDecriptor, then output back to a proto. We'll @@ -2004,14 +2206,13 @@ TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) { public_import->CopyTo(&public_import_proto); ASSERT_TRUE(pool_.BuildFile(public_import_proto) != NULL); const FileDescriptor* import = - protobuf_unittest_import::ImportMessage::descriptor()->file(); + protobuf_unittest_import::ImportMessage::descriptor()->file(); FileDescriptorProto import_proto; import->CopyTo(&import_proto); ASSERT_TRUE(pool_.BuildFile(import_proto) != NULL); const FileDescriptor* actual = pool_.BuildFile(parsed); parsed.Clear(); - ASSERT_TRUE(actual != NULL) - << "Failed to validate:\n" << debug_string; + ASSERT_TRUE(actual != NULL) << "Failed to validate:\n" << debug_string; actual->CopyTo(&parsed); ASSERT_TRUE(actual != NULL); @@ -2029,11 +2230,11 @@ TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) { TEST_F(ParseDescriptorDebugTest, TestCustomOptions) { const FileDescriptor* original_file = - protobuf_unittest::AggregateMessage::descriptor()->file(); + protobuf_unittest::AggregateMessage::descriptor()->file(); FileDescriptorProto expected; original_file->CopyTo(&expected); - string debug_string = original_file->DebugString(); + std::string debug_string = original_file->DebugString(); // Parse the debug string SetupParser(debug_string.c_str()); @@ -2141,37 +2342,38 @@ TEST_F(ParseDescriptorDebugTest, TestCommentsInDebugString) { // We don't test the exact comment placement or formatting, because we do not // want to be too fragile here. const char* expected_comments[] = { - "Detached comment before syntax.", - "Syntax comment.", - "Detached comment before package.", - "Package comment.", - "Detached comment before TestMessage1.", - "Message comment.", - "More detail in message comment.", - "Detached comment before foo.", - "Field comment", - "Detached comment before NestedMessage.", - "Nested-message comment", - "Detached comment before MyEnumType.", - "Enum comment", - "Detached comment before ASDF.", - "Enum-value comment", - "Detached comment before MyService.", - "Service comment", - "Detached comment before MyRPCCall.", - "RPC comment", + "Detached comment before syntax.", + "Syntax comment.", + "Detached comment before package.", + "Package comment.", + "Detached comment before TestMessage1.", + "Message comment.", + "More detail in message comment.", + "Detached comment before foo.", + "Field comment", + "Detached comment before NestedMessage.", + "Nested-message comment", + "Detached comment before MyEnumType.", + "Enum comment", + "Detached comment before ASDF.", + "Enum-value comment", + "Detached comment before MyService.", + "Service comment", + "Detached comment before MyRPCCall.", + "RPC comment", }; DebugStringOptions debug_string_options; debug_string_options.include_comments = true; { - const string debug_string = + const std::string debug_string = descriptor->DebugStringWithOptions(debug_string_options); for (int i = 0; i < GOOGLE_ARRAYSIZE(expected_comments); ++i) { - string::size_type found_pos = debug_string.find(expected_comments[i]); - EXPECT_TRUE(found_pos != string::npos) + std::string::size_type found_pos = + debug_string.find(expected_comments[i]); + EXPECT_TRUE(found_pos != std::string::npos) << "\"" << expected_comments[i] << "\" not found."; } @@ -2180,8 +2382,8 @@ TEST_F(ParseDescriptorDebugTest, TestCommentsInDebugString) { FileDescriptorProto parsed; parser_->Parse(input_.get(), &parsed); EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type); - ASSERT_EQ("", error_collector_.text_) - << "Failed to parse:\n" << debug_string; + ASSERT_EQ("", error_collector_.text_) << "Failed to parse:\n" + << debug_string; } } @@ -2202,7 +2404,7 @@ TEST_F(ParseDescriptorDebugTest, TestMaps) { // Make sure the debug string uses map syntax and does not have the auto // generated entry. - string debug_string = file->DebugString(); + std::string debug_string = file->DebugString(); EXPECT_TRUE(debug_string.find("map<") != string::npos); EXPECT_TRUE(debug_string.find("option map_entry") == string::npos); EXPECT_TRUE(debug_string.find("MapEntry") == string::npos); @@ -2235,11 +2437,9 @@ TEST_F(ParseDescriptorDebugTest, TestMaps) { // *output_field to the descriptor of the field, and *output_index to -1. // Returns true if the path was valid, false otherwise. A gTest failure is // recorded before returning false. -bool FollowPath(const Message& root, - const int* path_begin, const int* path_end, +bool FollowPath(const Message& root, const int* path_begin, const int* path_end, const Message** output_message, - const FieldDescriptor** output_field, - int* output_index) { + const FieldDescriptor** output_field, int* output_index) { if (path_begin == path_end) { // Path refers to this whole message. *output_message = &root; @@ -2254,8 +2454,8 @@ bool FollowPath(const Message& root, const FieldDescriptor* field = descriptor->FindFieldByNumber(*path_begin); if (field == NULL) { - ADD_FAILURE() << descriptor->name() << " has no field number: " - << *path_begin; + ADD_FAILURE() << descriptor->name() + << " has no field number: " << *path_begin; return false; } @@ -2275,16 +2475,16 @@ bool FollowPath(const Message& root, if (index >= size) { ADD_FAILURE() << descriptor->name() << "." << field->name() - << " has size " << size << ", but path contained index: " - << index; + << " has size " << size + << ", but path contained index: " << index; return false; } if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { // Descend into child message. const Message& child = reflection->GetRepeatedMessage(root, field, index); - return FollowPath(child, path_begin, path_end, - output_message, output_field, output_index); + return FollowPath(child, path_begin, path_end, output_message, + output_field, output_index); } else if (path_begin == path_end) { // Path refers to this element. *output_message = &root; @@ -2299,8 +2499,8 @@ bool FollowPath(const Message& root, } else { if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { const Message& child = reflection->GetMessage(root, field); - return FollowPath(child, path_begin, path_end, - output_message, output_field, output_index); + return FollowPath(child, path_begin, path_end, output_message, + output_field, output_index); } else if (path_begin == path_end) { // Path refers to this field. *output_message = &root; @@ -2360,9 +2560,8 @@ class SourceInfoTest : public ParserTest { } virtual void TearDown() { - EXPECT_TRUE(spans_.empty()) - << "Forgot to call HasSpan() for:\n" - << spans_.begin()->second->DebugString(); + EXPECT_TRUE(spans_.empty()) << "Forgot to call HasSpan() for:\n" + << spans_.begin()->second->DebugString(); } // ----------------------------------------------------------------- @@ -2373,8 +2572,8 @@ class SourceInfoTest : public ParserTest { bool HasSpan(char start_marker, char end_marker, const Message& descriptor_proto) { - return HasSpanWithComment( - start_marker, end_marker, descriptor_proto, NULL, -1, NULL, NULL, NULL); + return HasSpanWithComment(start_marker, end_marker, descriptor_proto, NULL, + -1, NULL, NULL, NULL); } bool HasSpanWithComment(char start_marker, char end_marker, @@ -2382,28 +2581,27 @@ class SourceInfoTest : public ParserTest { const char* expected_leading_comments, const char* expected_trailing_comments, const char* expected_leading_detached_comments) { - return HasSpanWithComment( - start_marker, end_marker, descriptor_proto, NULL, -1, - expected_leading_comments, expected_trailing_comments, - expected_leading_detached_comments); + return HasSpanWithComment(start_marker, end_marker, descriptor_proto, NULL, + -1, expected_leading_comments, + expected_trailing_comments, + expected_leading_detached_comments); } bool HasSpan(char start_marker, char end_marker, - const Message& descriptor_proto, const string& field_name) { + const Message& descriptor_proto, const std::string& field_name) { return HasSpan(start_marker, end_marker, descriptor_proto, field_name, -1); } bool HasSpan(char start_marker, char end_marker, - const Message& descriptor_proto, const string& field_name, + const Message& descriptor_proto, const std::string& field_name, int index) { - return HasSpan(start_marker, end_marker, descriptor_proto, - field_name, index, NULL, NULL, NULL); + return HasSpan(start_marker, end_marker, descriptor_proto, field_name, + index, NULL, NULL, NULL); } bool HasSpan(char start_marker, char end_marker, - const Message& descriptor_proto, - const string& field_name, int index, - const char* expected_leading_comments, + const Message& descriptor_proto, const std::string& field_name, + int index, const char* expected_leading_comments, const char* expected_trailing_comments, const char* expected_leading_detached_comments) { const FieldDescriptor* field = @@ -2414,32 +2612,27 @@ class SourceInfoTest : public ParserTest { return false; } - return HasSpanWithComment( - start_marker, end_marker, descriptor_proto, field, index, - expected_leading_comments, expected_trailing_comments, - expected_leading_detached_comments); + return HasSpanWithComment(start_marker, end_marker, descriptor_proto, field, + index, expected_leading_comments, + expected_trailing_comments, + expected_leading_detached_comments); } bool HasSpan(const Message& descriptor_proto) { - return HasSpanWithComment( - '\0', '\0', descriptor_proto, NULL, -1, NULL, NULL, NULL); + return HasSpanWithComment('\0', '\0', descriptor_proto, NULL, -1, NULL, + NULL, NULL); } - bool HasSpan(const Message& descriptor_proto, const string& field_name) { + bool HasSpan(const Message& descriptor_proto, const std::string& field_name) { return HasSpan('\0', '\0', descriptor_proto, field_name, -1); } - bool HasSpan(const Message& descriptor_proto, const string& field_name, - int index) { - return HasSpan('\0', '\0', descriptor_proto, field_name, index); - } - - bool HasSpanWithComment( - char start_marker, char end_marker, const Message& descriptor_proto, - const FieldDescriptor* field, int index, - const char* expected_leading_comments, - const char* expected_trailing_comments, - const char* expected_leading_detached_comments) { + bool HasSpanWithComment(char start_marker, char end_marker, + const Message& descriptor_proto, + const FieldDescriptor* field, int index, + const char* expected_leading_comments, + const char* expected_trailing_comments, + const char* expected_leading_detached_comments) { std::pair range = spans_.equal_range(SpanKey(descriptor_proto, field, index)); @@ -2503,9 +2696,9 @@ class SourceInfoTest : public ParserTest { inline SpanKey() {} inline SpanKey(const Message& descriptor_proto_param, - const FieldDescriptor* field_param, - int index_param) - : descriptor_proto(&descriptor_proto_param), field(field_param), + const FieldDescriptor* field_param, int index_param) + : descriptor_proto(&descriptor_proto_param), + field(field_param), index(index_param) {} inline bool operator<(const SpanKey& other) const { @@ -2520,7 +2713,7 @@ class SourceInfoTest : public ParserTest { typedef std::multimap SpanMap; SpanMap spans_; std::map > markers_; - string text_without_markers_; + std::string text_without_markers_; void ExtractMarkers(const char* text) { markers_.clear(); @@ -2553,25 +2746,31 @@ class SourceInfoTest : public ParserTest { }; TEST_F(SourceInfoTest, BasicFileDecls) { - EXPECT_TRUE(Parse( - "$a$syntax = \"proto2\";$i$\n" - "package $b$foo.bar$c$;\n" - "import $d$\"baz.proto\"$e$;\n" - "import $f$\"qux.proto\"$g$;$h$\n" - "\n" - "// comment ignored\n")); - - EXPECT_TRUE(HasSpan('a', 'h', file_)); + EXPECT_TRUE( + Parse("$a$syntax = \"proto2\";$i$\n" + "$b$package foo.bar;$c$\n" + "$d$import \"baz.proto\";$e$\n" + "$f$import\"qux.proto\";$h$\n" + "$j$import $k$public$l$ \"bar.proto\";$m$\n" + "$n$import $o$weak$p$ \"bar.proto\";$q$\n" + "\n" + "// comment ignored\n")); + + EXPECT_TRUE(HasSpan('a', 'q', file_)); EXPECT_TRUE(HasSpan('b', 'c', file_, "package")); EXPECT_TRUE(HasSpan('d', 'e', file_, "dependency", 0)); - EXPECT_TRUE(HasSpan('f', 'g', file_, "dependency", 1)); + EXPECT_TRUE(HasSpan('f', 'h', file_, "dependency", 1)); + EXPECT_TRUE(HasSpan('j', 'm', file_, "dependency", 2)); + EXPECT_TRUE(HasSpan('k', 'l', file_, "public_dependency", 0)); + EXPECT_TRUE(HasSpan('n', 'q', file_, "dependency", 3)); + EXPECT_TRUE(HasSpan('o', 'p', file_, "weak_dependency", 0)); EXPECT_TRUE(HasSpan('a', 'i', file_, "syntax")); } TEST_F(SourceInfoTest, Messages) { - EXPECT_TRUE(Parse( - "$a$message $b$Foo$c$ {}$d$\n" - "$e$message $f$Bar$g$ {}$h$\n")); + EXPECT_TRUE( + Parse("$a$message $b$Foo$c$ {}$d$\n" + "$e$message $f$Bar$g$ {}$h$\n")); EXPECT_TRUE(HasSpan('a', 'd', file_.message_type(0))); EXPECT_TRUE(HasSpan('b', 'c', file_.message_type(0), "name")); @@ -2583,11 +2782,11 @@ TEST_F(SourceInfoTest, Messages) { } TEST_F(SourceInfoTest, Fields) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$optional$b$ $c$int32$d$ $e$bar$f$ = $g$1$h$;$i$\n" - " $j$repeated$k$ $l$X.Y$m$ $n$baz$o$ = $p$2$q$;$r$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$optional$b$ $c$int32$d$ $e$bar$f$ = $g$1$h$;$i$\n" + " $j$repeated$k$ $l$X.Y$m$ $n$baz$o$ = $p$2$q$;$r$\n" + "}\n")); const FieldDescriptorProto& field1 = file_.message_type(0).field(0); const FieldDescriptorProto& field2 = file_.message_type(0).field(1); @@ -2611,14 +2810,14 @@ TEST_F(SourceInfoTest, Fields) { } TEST_F(SourceInfoTest, Extensions) { - EXPECT_TRUE(Parse( - "$a$extend $b$Foo$c$ {\n" - " $d$optional$e$ int32 bar = 1;$f$\n" - " $g$repeated$h$ X.Y baz = 2;$i$\n" - "}$j$\n" - "$k$extend $l$Bar$m$ {\n" - " $n$optional int32 qux = 1;$o$\n" - "}$p$\n")); + EXPECT_TRUE( + Parse("$a$extend $b$Foo$c$ {\n" + " $d$optional$e$ int32 bar = 1;$f$\n" + " $g$repeated$h$ X.Y baz = 2;$i$\n" + "}$j$\n" + "$k$extend $l$Bar$m$ {\n" + " $n$optional int32 qux = 1;$o$\n" + "}$p$\n")); const FieldDescriptorProto& field1 = file_.extension(0); const FieldDescriptorProto& field2 = file_.extension(1); @@ -2653,16 +2852,16 @@ TEST_F(SourceInfoTest, Extensions) { } TEST_F(SourceInfoTest, NestedExtensions) { - EXPECT_TRUE(Parse( - "message Message {\n" - " $a$extend $b$Foo$c$ {\n" - " $d$optional$e$ int32 bar = 1;$f$\n" - " $g$repeated$h$ X.Y baz = 2;$i$\n" - " }$j$\n" - " $k$extend $l$Bar$m$ {\n" - " $n$optional int32 qux = 1;$o$\n" - " }$p$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Message {\n" + " $a$extend $b$Foo$c$ {\n" + " $d$optional$e$ int32 bar = 1;$f$\n" + " $g$repeated$h$ X.Y baz = 2;$i$\n" + " }$j$\n" + " $k$extend $l$Bar$m$ {\n" + " $n$optional int32 qux = 1;$o$\n" + " }$p$\n" + "}\n")); const FieldDescriptorProto& field1 = file_.message_type(0).extension(0); const FieldDescriptorProto& field2 = file_.message_type(0).extension(1); @@ -2699,11 +2898,11 @@ TEST_F(SourceInfoTest, NestedExtensions) { } TEST_F(SourceInfoTest, ExtensionRanges) { - EXPECT_TRUE(Parse( - "message Message {\n" - " $a$extensions $b$1$c$ to $d$4$e$, $f$6$g$;$h$\n" - " $i$extensions $j$8$k$ to $l$max$m$;$n$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Message {\n" + " $a$extensions $b$1$c$ to $d$4$e$, $f$6$g$;$h$\n" + " $i$extensions $j$8$k$ to $l$max$m$;$n$\n" + "}\n")); const DescriptorProto::ExtensionRange& range1 = file_.message_type(0).extension_range(0); @@ -2733,13 +2932,40 @@ TEST_F(SourceInfoTest, ExtensionRanges) { EXPECT_TRUE(HasSpan(file_.message_type(0), "name")); } +TEST_F(SourceInfoTest, ReservedRanges) { + EXPECT_TRUE( + Parse("message Message {\n" + " $a$reserved $b$1$c$ to $d$4$e$, $f$6$g$;$h$\n" + "}\n")); + + const DescriptorProto::ReservedRange& range1 = + file_.message_type(0).reserved_range(0); + const DescriptorProto::ReservedRange& range2 = + file_.message_type(0).reserved_range(1); + + EXPECT_TRUE(HasSpan('a', 'h', file_.message_type(0), "reserved_range")); + + EXPECT_TRUE(HasSpan('b', 'e', range1)); + EXPECT_TRUE(HasSpan('b', 'c', range1, "start")); + EXPECT_TRUE(HasSpan('d', 'e', range1, "end")); + + EXPECT_TRUE(HasSpan('f', 'g', range2)); + EXPECT_TRUE(HasSpan('f', 'g', range2, "start")); + EXPECT_TRUE(HasSpan('f', 'g', range2, "end")); + + // Ignore these. + EXPECT_TRUE(HasSpan(file_)); + EXPECT_TRUE(HasSpan(file_.message_type(0))); + EXPECT_TRUE(HasSpan(file_.message_type(0), "name")); +} + TEST_F(SourceInfoTest, Oneofs) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$oneof $c$foo$d$ {\n" - " $e$int32$f$ $g$a$h$ = $i$1$j$;$k$\n" - " }$r$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$oneof $c$foo$d$ {\n" + " $e$int32$f$ $g$a$h$ = $i$1$j$;$k$\n" + " }$r$\n" + "}\n")); const OneofDescriptorProto& oneof_decl = file_.message_type(0).oneof_decl(0); const FieldDescriptorProto& field = file_.message_type(0).field(0); @@ -2759,13 +2985,13 @@ TEST_F(SourceInfoTest, Oneofs) { } TEST_F(SourceInfoTest, NestedMessages) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$message $b$Bar$c$ {\n" - " $d$message $e$Baz$f$ {}$g$\n" - " }$h$\n" - " $i$message $j$Qux$k$ {}$l$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$message $b$Bar$c$ {\n" + " $d$message $e$Baz$f$ {}$g$\n" + " }$h$\n" + " $i$message $j$Qux$k$ {}$l$\n" + "}\n")); const DescriptorProto& bar = file_.message_type(0).nested_type(0); const DescriptorProto& baz = bar.nested_type(0); @@ -2785,13 +3011,13 @@ TEST_F(SourceInfoTest, NestedMessages) { } TEST_F(SourceInfoTest, Groups) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " message Bar {}\n" - " $a$optional$b$ $c$group$d$ $e$Baz$f$ = $g$1$h$ {\n" - " $i$message Qux {}$j$\n" - " }$k$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " message Bar {}\n" + " $a$optional$b$ $c$group$d$ $e$Baz$f$ = $g$1$h$ {\n" + " $i$message Qux {}$j$\n" + " }$k$\n" + "}\n")); const DescriptorProto& bar = file_.message_type(0).nested_type(0); const DescriptorProto& baz = file_.message_type(0).nested_type(1); @@ -2819,9 +3045,9 @@ TEST_F(SourceInfoTest, Groups) { } TEST_F(SourceInfoTest, Enums) { - EXPECT_TRUE(Parse( - "$a$enum $b$Foo$c$ {}$d$\n" - "$e$enum $f$Bar$g$ {}$h$\n")); + EXPECT_TRUE( + Parse("$a$enum $b$Foo$c$ {}$d$\n" + "$e$enum $f$Bar$g$ {}$h$\n")); EXPECT_TRUE(HasSpan('a', 'd', file_.enum_type(0))); EXPECT_TRUE(HasSpan('b', 'c', file_.enum_type(0), "name")); @@ -2833,11 +3059,11 @@ TEST_F(SourceInfoTest, Enums) { } TEST_F(SourceInfoTest, EnumValues) { - EXPECT_TRUE(Parse( - "enum Foo {\n" - " $a$BAR$b$ = $c$1$d$;$e$\n" - " $f$BAZ$g$ = $h$2$i$;$j$\n" - "}")); + EXPECT_TRUE( + Parse("enum Foo {\n" + " $a$BAR$b$ = $c$1$d$;$e$\n" + " $f$BAZ$g$ = $h$2$i$;$j$\n" + "}")); const EnumValueDescriptorProto& bar = file_.enum_type(0).value(0); const EnumValueDescriptorProto& baz = file_.enum_type(0).value(1); @@ -2856,11 +3082,11 @@ TEST_F(SourceInfoTest, EnumValues) { } TEST_F(SourceInfoTest, NestedEnums) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$enum $b$Bar$c$ {}$d$\n" - " $e$enum $f$Baz$g$ {}$h$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$enum $b$Bar$c$ {}$d$\n" + " $e$enum $f$Baz$g$ {}$h$\n" + "}\n")); const EnumDescriptorProto& bar = file_.message_type(0).enum_type(0); const EnumDescriptorProto& baz = file_.message_type(0).enum_type(1); @@ -2877,9 +3103,9 @@ TEST_F(SourceInfoTest, NestedEnums) { } TEST_F(SourceInfoTest, Services) { - EXPECT_TRUE(Parse( - "$a$service $b$Foo$c$ {}$d$\n" - "$e$service $f$Bar$g$ {}$h$\n")); + EXPECT_TRUE( + Parse("$a$service $b$Foo$c$ {}$d$\n" + "$e$service $f$Bar$g$ {}$h$\n")); EXPECT_TRUE(HasSpan('a', 'd', file_.service(0))); EXPECT_TRUE(HasSpan('b', 'c', file_.service(0), "name")); @@ -2891,11 +3117,11 @@ TEST_F(SourceInfoTest, Services) { } TEST_F(SourceInfoTest, MethodsAndStreams) { - EXPECT_TRUE(Parse( - "service Foo {\n" - " $a$rpc $b$Bar$c$($d$X$e$) returns($f$Y$g$);$h$" - " $i$rpc $j$Baz$k$($l$Z$m$) returns($n$W$o$);$p$" - "}")); + EXPECT_TRUE( + Parse("service Foo {\n" + " $a$rpc $b$Bar$c$($d$X$e$) returns($f$Y$g$);$h$" + " $i$rpc $j$Baz$k$($l$Z$m$) returns($n$W$o$);$p$" + "}")); const MethodDescriptorProto& bar = file_.service(0).method(0); const MethodDescriptorProto& baz = file_.service(0).method(1); @@ -2918,15 +3144,14 @@ TEST_F(SourceInfoTest, MethodsAndStreams) { TEST_F(SourceInfoTest, Options) { - EXPECT_TRUE(Parse( - "$a$option $b$foo$c$.$d$($e$bar.baz$f$)$g$ = " - "$h$123$i$;$j$\n" - "$k$option qux = $l$-123$m$;$n$\n" - "$o$option corge = $p$abc$q$;$r$\n" - "$s$option grault = $t$'blah'$u$;$v$\n" - "$w$option garply = $x${ yadda yadda }$y$;$z$\n" - "$0$option waldo = $1$123.0$2$;$3$\n" - )); + EXPECT_TRUE( + Parse("$a$option $b$foo$c$.$d$($e$bar.baz$f$)$g$ = " + "$h$123$i$;$j$\n" + "$k$option qux = $l$-123$m$;$n$\n" + "$o$option corge = $p$abc$q$;$r$\n" + "$s$option grault = $t$'blah'$u$;$v$\n" + "$w$option garply = $x${ yadda yadda }$y$;$z$\n" + "$0$option waldo = $1$123.0$2$;$3$\n")); const UninterpretedOption& option1 = file_.options().uninterpreted_option(0); const UninterpretedOption& option2 = file_.options().uninterpreted_option(1); @@ -2984,22 +3209,22 @@ TEST_F(SourceInfoTest, Options) { } TEST_F(SourceInfoTest, ScopedOptions) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$option mopt = 1;$b$\n" - "}\n" - "enum Bar {\n" - " $c$option eopt = 1;$d$\n" - "}\n" - "service Baz {\n" - " $e$option sopt = 1;$f$\n" - " rpc M(X) returns(Y) {\n" - " $g$option mopt = 1;$h$\n" - " }\n" - " rpc MS4($1$stream$2$ X) returns($3$stream$4$ Y) {\n" - " $k$option mopt = 1;$l$\n" - " }\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$option mopt = 1;$b$\n" + "}\n" + "enum Bar {\n" + " $c$option eopt = 1;$d$\n" + "}\n" + "service Baz {\n" + " $e$option sopt = 1;$f$\n" + " rpc M(X) returns(Y) {\n" + " $g$option mopt = 1;$h$\n" + " }\n" + " rpc MS4($1$stream$2$ X) returns($3$stream$4$ Y) {\n" + " $k$option mopt = 1;$l$\n" + " }\n" + "}\n")); EXPECT_TRUE(HasSpan('a', 'b', file_.message_type(0).options())); EXPECT_TRUE(HasSpan('c', 'd', file_.enum_type(0).options())); @@ -3010,87 +3235,89 @@ TEST_F(SourceInfoTest, ScopedOptions) { EXPECT_TRUE(HasSpan(file_)); EXPECT_TRUE(HasSpan(file_.message_type(0))); EXPECT_TRUE(HasSpan(file_.message_type(0), "name")); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0), "positive_int_value")); + EXPECT_TRUE(HasSpan(file_.message_type(0).options().uninterpreted_option(0))); + EXPECT_TRUE( + HasSpan(file_.message_type(0).options().uninterpreted_option(0), "name")); + EXPECT_TRUE( + HasSpan(file_.message_type(0).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE( + HasSpan(file_.message_type(0).options().uninterpreted_option(0).name(0), + "name_part")); + EXPECT_TRUE(HasSpan(file_.message_type(0).options().uninterpreted_option(0), + "positive_int_value")); EXPECT_TRUE(HasSpan(file_.enum_type(0))); EXPECT_TRUE(HasSpan(file_.enum_type(0), "name")); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0), "positive_int_value")); + EXPECT_TRUE(HasSpan(file_.enum_type(0).options().uninterpreted_option(0))); + EXPECT_TRUE( + HasSpan(file_.enum_type(0).options().uninterpreted_option(0), "name")); + EXPECT_TRUE( + HasSpan(file_.enum_type(0).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE( + HasSpan(file_.enum_type(0).options().uninterpreted_option(0).name(0), + "name_part")); + EXPECT_TRUE(HasSpan(file_.enum_type(0).options().uninterpreted_option(0), + "positive_int_value")); EXPECT_TRUE(HasSpan(file_.service(0))); EXPECT_TRUE(HasSpan(file_.service(0), "name")); EXPECT_TRUE(HasSpan(file_.service(0).method(0))); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0), "positive_int_value")); + EXPECT_TRUE(HasSpan(file_.service(0).options().uninterpreted_option(0))); + EXPECT_TRUE( + HasSpan(file_.service(0).options().uninterpreted_option(0), "name")); + EXPECT_TRUE( + HasSpan(file_.service(0).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).options().uninterpreted_option(0).name(0), "name_part")); + EXPECT_TRUE(HasSpan(file_.service(0).options().uninterpreted_option(0), + "positive_int_value")); EXPECT_TRUE(HasSpan(file_.service(0).method(0), "name")); EXPECT_TRUE(HasSpan(file_.service(0).method(0), "input_type")); EXPECT_TRUE(HasSpan(file_.service(0).method(0), "output_type")); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0), "positive_int_value")); + EXPECT_TRUE( + HasSpan(file_.service(0).method(0).options().uninterpreted_option(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).method(0).options().uninterpreted_option(0), "name")); + EXPECT_TRUE(HasSpan( + file_.service(0).method(0).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).method(0).options().uninterpreted_option(0).name(0), + "name_part")); + EXPECT_TRUE( + HasSpan(file_.service(0).method(0).options().uninterpreted_option(0), + "positive_int_value")); EXPECT_TRUE(HasSpan('k', 'l', file_.service(0).method(1).options())); EXPECT_TRUE(HasSpan(file_.service(0).method(1))); EXPECT_TRUE(HasSpan(file_.service(0).method(1), "name")); EXPECT_TRUE(HasSpan(file_.service(0).method(1), "input_type")); EXPECT_TRUE(HasSpan(file_.service(0).method(1), "output_type")); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0), "positive_int_value")); - EXPECT_TRUE(HasSpan('1', '2', file_.service(0).method(1), - "client_streaming")); - EXPECT_TRUE(HasSpan('3', '4', file_.service(0).method(1), - "server_streaming")); + EXPECT_TRUE( + HasSpan(file_.service(0).method(1).options().uninterpreted_option(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).method(1).options().uninterpreted_option(0), "name")); + EXPECT_TRUE(HasSpan( + file_.service(0).method(1).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).method(1).options().uninterpreted_option(0).name(0), + "name_part")); + EXPECT_TRUE( + HasSpan(file_.service(0).method(1).options().uninterpreted_option(0), + "positive_int_value")); + EXPECT_TRUE( + HasSpan('1', '2', file_.service(0).method(1), "client_streaming")); + EXPECT_TRUE( + HasSpan('3', '4', file_.service(0).method(1), "server_streaming")); } TEST_F(SourceInfoTest, FieldOptions) { // The actual "name = value" pairs are parsed by the same code as for // top-level options so we won't re-test that -- just make sure that the // syntax used for field options is understood. - EXPECT_TRUE(Parse( - "message Foo {" - " optional int32 bar = 1 " - "$a$[default=$b$123$c$,$d$opt1=123$e$," - "$f$opt2='hi'$g$]$h$;" - "}\n" - )); + EXPECT_TRUE( + Parse("message Foo {" + " optional int32 bar = 1 " + "$a$[default=$b$123$c$,$d$opt1=123$e$," + "$f$opt2='hi'$g$]$h$;" + "}\n")); const FieldDescriptorProto& field = file_.message_type(0).field(0); const UninterpretedOption& option1 = field.options().uninterpreted_option(0); @@ -3124,11 +3351,10 @@ TEST_F(SourceInfoTest, EnumValueOptions) { // The actual "name = value" pairs are parsed by the same code as for // top-level options so we won't re-test that -- just make sure that the // syntax used for enum options is understood. - EXPECT_TRUE(Parse( - "enum Foo {" - " BAR = 1 $a$[$b$opt1=123$c$,$d$opt2='hi'$e$]$f$;" - "}\n" - )); + EXPECT_TRUE( + Parse("enum Foo {" + " BAR = 1 $a$[$b$opt1=123$c$,$d$opt2='hi'$e$]$f$;" + "}\n")); const EnumValueDescriptorProto& value = file_.enum_type(0).value(0); const UninterpretedOption& option1 = value.options().uninterpreted_option(0); @@ -3156,33 +3382,28 @@ TEST_F(SourceInfoTest, EnumValueOptions) { } TEST_F(SourceInfoTest, DocComments) { - EXPECT_TRUE(Parse( - "// Foo leading\n" - "// line 2\n" - "$a$message Foo {\n" - " // Foo trailing\n" - " // line 2\n" - "\n" - " // detached\n" - "\n" - " // bar leading\n" - " $b$optional int32 bar = 1;$c$\n" - " // bar trailing\n" - "}$d$\n" - "// ignored\n" - )); + EXPECT_TRUE( + Parse("// Foo leading\n" + "// line 2\n" + "$a$message Foo {\n" + " // Foo trailing\n" + " // line 2\n" + "\n" + " // detached\n" + "\n" + " // bar leading\n" + " $b$optional int32 bar = 1;$c$\n" + " // bar trailing\n" + "}$d$\n" + "// ignored\n")); const DescriptorProto& foo = file_.message_type(0); const FieldDescriptorProto& bar = foo.field(0); - EXPECT_TRUE(HasSpanWithComment('a', 'd', foo, - " Foo leading\n line 2\n", - " Foo trailing\n line 2\n", - NULL)); - EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, - " bar leading\n", - " bar trailing\n", - " detached\n")); + EXPECT_TRUE(HasSpanWithComment('a', 'd', foo, " Foo leading\n line 2\n", + " Foo trailing\n line 2\n", NULL)); + EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, " bar leading\n", + " bar trailing\n", " detached\n")); // Ignore these. EXPECT_TRUE(HasSpan(file_)); @@ -3194,45 +3415,40 @@ TEST_F(SourceInfoTest, DocComments) { } TEST_F(SourceInfoTest, DocComments2) { - EXPECT_TRUE(Parse( - "// detached before message.\n" - "\n" - "// Foo leading\n" - "// line 2\n" - "$a$message Foo {\n" - " /* Foo trailing\n" - " * line 2 */\n" - " // detached\n" - " /* bar leading\n" - " */" - " $b$optional int32 bar = 1;$c$ // bar trailing\n" - " // ignored detached\n" - "}$d$\n" - "// ignored\n" - "\n" - "// detached before option\n" - "\n" - "// option leading\n" - "$e$option baz = 123;$f$\n" - "// option trailing\n" - )); + EXPECT_TRUE( + Parse("// detached before message.\n" + "\n" + "// Foo leading\n" + "// line 2\n" + "$a$message Foo {\n" + " /* Foo trailing\n" + " * line 2 */\n" + " // detached\n" + " /* bar leading\n" + " */" + " $b$optional int32 bar = 1;$c$ // bar trailing\n" + " // ignored detached\n" + "}$d$\n" + "// ignored\n" + "\n" + "// detached before option\n" + "\n" + "// option leading\n" + "$e$option baz = 123;$f$\n" + "// option trailing\n")); const DescriptorProto& foo = file_.message_type(0); const FieldDescriptorProto& bar = foo.field(0); const UninterpretedOption& baz = file_.options().uninterpreted_option(0); - EXPECT_TRUE(HasSpanWithComment('a', 'd', foo, - " Foo leading\n line 2\n", - " Foo trailing\n line 2 ", - " detached before message.\n")); - EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, - " bar leading\n", - " bar trailing\n", - " detached\n")); - EXPECT_TRUE(HasSpanWithComment('e', 'f', baz, - " option leading\n", - " option trailing\n", - " detached before option\n")); + EXPECT_TRUE(HasSpanWithComment('a', 'd', foo, " Foo leading\n line 2\n", + " Foo trailing\n line 2 ", + " detached before message.\n")); + EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, " bar leading\n", + " bar trailing\n", " detached\n")); + EXPECT_TRUE(HasSpanWithComment('e', 'f', baz, " option leading\n", + " option trailing\n", + " detached before option\n")); // Ignore these. EXPECT_TRUE(HasSpan(file_)); @@ -3249,22 +3465,19 @@ TEST_F(SourceInfoTest, DocComments2) { } TEST_F(SourceInfoTest, DocComments3) { - EXPECT_TRUE(Parse( - "$a$message Foo {\n" - " // bar leading\n" - " $b$optional int32 bar = 1 [(baz.qux) = {}];$c$\n" - " // bar trailing\n" - "}$d$\n" - "// ignored\n" - )); + EXPECT_TRUE( + Parse("$a$message Foo {\n" + " // bar leading\n" + " $b$optional int32 bar = 1 [(baz.qux) = {}];$c$\n" + " // bar trailing\n" + "}$d$\n" + "// ignored\n")); const DescriptorProto& foo = file_.message_type(0); const FieldDescriptorProto& bar = foo.field(0); - EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, - " bar leading\n", - " bar trailing\n", - NULL)); + EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, " bar leading\n", + " bar trailing\n", NULL)); // Ignore these. EXPECT_TRUE(HasSpan(file_)); @@ -3278,88 +3491,82 @@ TEST_F(SourceInfoTest, DocComments3) { EXPECT_TRUE(HasSpan(bar.options().uninterpreted_option(0))); EXPECT_TRUE(HasSpan(bar.options().uninterpreted_option(0), "name")); EXPECT_TRUE(HasSpan(bar.options().uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan( - bar.options().uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan( - bar.options().uninterpreted_option(0), "aggregate_value")); + EXPECT_TRUE( + HasSpan(bar.options().uninterpreted_option(0).name(0), "name_part")); + EXPECT_TRUE( + HasSpan(bar.options().uninterpreted_option(0), "aggregate_value")); } TEST_F(SourceInfoTest, DocCommentsTopLevel) { - EXPECT_TRUE(Parse( - "// detached before syntax paragraph 1\n" - "\n" - "// detached before syntax paragraph 2\n" - "\n" - "// syntax leading\n" - "$a$syntax = \"proto2\";$b$\n" - "// syntax trailing\n" - "\n" - "// syntax-package detached comments\n" - "\n" - ";\n" - "\n" - "// detached after empty before package\n" - "\n" - "// package leading\n" - "package $c$foo$d$;\n" - "// package trailing\n" - "\n" - "// ignored detach\n" - "\n")); - - EXPECT_TRUE(HasSpan('a', 'b', file_, "syntax", -1, - " syntax leading\n", - " syntax trailing\n", - " detached before syntax paragraph 1\n" - "\n" - " detached before syntax paragraph 2\n")); - EXPECT_TRUE(HasSpan('c', 'd', file_, "package", -1, - " package leading\n", - " package trailing\n", - " syntax-package detached comments\n" - "\n" - " detached after empty before package\n")); + EXPECT_TRUE( + Parse("// detached before syntax paragraph 1\n" + "\n" + "// detached before syntax paragraph 2\n" + "\n" + "// syntax leading\n" + "$a$syntax = \"proto2\";$b$\n" + "// syntax trailing\n" + "\n" + "// syntax-package detached comments\n" + "\n" + ";\n" + "\n" + "// detached after empty before package\n" + "\n" + "// package leading\n" + "$c$package foo;$d$\n" + "// package trailing\n" + "\n" + "// ignored detach\n" + "\n")); + + EXPECT_TRUE(HasSpan('a', 'b', file_, "syntax", -1, " syntax leading\n", + " syntax trailing\n", + " detached before syntax paragraph 1\n" + "\n" + " detached before syntax paragraph 2\n")); + EXPECT_TRUE(HasSpan('c', 'd', file_, "package", -1, " package leading\n", + " package trailing\n", + " syntax-package detached comments\n" + "\n" + " detached after empty before package\n")); // ignore these. EXPECT_TRUE(HasSpan(file_)); } TEST_F(SourceInfoTest, DocCommentsOneof) { - EXPECT_TRUE(Parse( - "// Foo leading\n" - "$a$message Foo {\n" - " /* Foo trailing\n" - " */\n" - " // detached before oneof\n" - " /* bar leading\n" - " * line 2 */\n" - " $b$oneof bar {\n" - " /* bar trailing\n" - " * line 2 */\n" - " // detached before bar_int\n" - " /* bar_int leading\n" - " */\n" - " $c$int32 bar_int = 1;$d$ // bar_int trailing\n" - " // detach comment ignored\n" - " }$e$\n" - "}$f$\n")); + EXPECT_TRUE( + Parse("// Foo leading\n" + "$a$message Foo {\n" + " /* Foo trailing\n" + " */\n" + " // detached before oneof\n" + " /* bar leading\n" + " * line 2 */\n" + " $b$oneof bar {\n" + " /* bar trailing\n" + " * line 2 */\n" + " // detached before bar_int\n" + " /* bar_int leading\n" + " */\n" + " $c$int32 bar_int = 1;$d$ // bar_int trailing\n" + " // detach comment ignored\n" + " }$e$\n" + "}$f$\n")); const DescriptorProto& foo = file_.message_type(0); const OneofDescriptorProto& bar = foo.oneof_decl(0); const FieldDescriptorProto& bar_int = foo.field(0); - EXPECT_TRUE(HasSpanWithComment('a', 'f', foo, - " Foo leading\n", - " Foo trailing\n", - NULL)); - EXPECT_TRUE(HasSpanWithComment('b', 'e', bar, - " bar leading\n line 2 ", - " bar trailing\n line 2 ", - " detached before oneof\n")); - EXPECT_TRUE(HasSpanWithComment('c', 'd', bar_int, - " bar_int leading\n", - " bar_int trailing\n", - " detached before bar_int\n")); + EXPECT_TRUE(HasSpanWithComment('a', 'f', foo, " Foo leading\n", + " Foo trailing\n", NULL)); + EXPECT_TRUE(HasSpanWithComment('b', 'e', bar, " bar leading\n line 2 ", + " bar trailing\n line 2 ", + " detached before oneof\n")); + EXPECT_TRUE(HasSpanWithComment('c', 'd', bar_int, " bar_int leading\n", + " bar_int trailing\n", + " detached before bar_int\n")); // Ignore these. EXPECT_TRUE(HasSpan(file_)); diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index a58e1754d2..232db80b50 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -99,6 +99,10 @@ void GenerateMessageConstructorDocComment(io::Printer* printer, int is_descriptor); void GenerateFieldDocComment(io::Printer* printer, const FieldDescriptor* field, int is_descriptor, int function_type); +void GenerateWrapperFieldGetterDocComment(io::Printer* printer, + const FieldDescriptor* field); +void GenerateWrapperFieldSetterDocComment(io::Printer* printer, + const FieldDescriptor* field); void GenerateEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_, int is_descriptor); void GenerateEnumValueDocComment(io::Printer* printer, @@ -674,6 +678,21 @@ void GenerateFieldAccessor(const FieldDescriptor* field, bool is_descriptor, field->name()); } + // For wrapper types, generate an additional getXXXUnwrapped getter + if (!field->is_map() && + !field->is_repeated() && + field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && + IsWrapperType(field)) { + GenerateWrapperFieldGetterDocComment(printer, field); + printer->Print( + "public function get^camel_name^Unwrapped()\n" + "{\n" + " $wrapper = $this->get^camel_name^();\n" + " return is_null($wrapper) ? null : $wrapper->getValue();\n" + "}\n\n", + "camel_name", UnderscoresToCamelCase(field->name(), true)); + } + // Generate setter. GenerateFieldDocComment(printer, field, is_descriptor, kFieldSetter); printer->Print( @@ -772,6 +791,22 @@ void GenerateFieldAccessor(const FieldDescriptor* field, bool is_descriptor, printer->Print( "}\n\n"); + // For wrapper types, generate an additional setXXXValue getter + if (!field->is_map() && + !field->is_repeated() && + field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && + IsWrapperType(field)) { + GenerateWrapperFieldSetterDocComment(printer, field); + printer->Print( + "public function set^camel_name^Unwrapped($var)\n" + "{\n" + " $wrappedVar = is_null($var) ? null : new \\^wrapper_type^(['value' => $var]);\n" + " return $this->set^camel_name^($wrappedVar);\n" + "}\n\n", + "camel_name", UnderscoresToCamelCase(field->name(), true), + "wrapper_type", LegacyFullClassName(field->message_type(), is_descriptor)); + } + // Generate has method for proto2 only. if (is_descriptor) { printer->Print( @@ -844,7 +879,7 @@ void GenerateMessageToPool(const string& name_prefix, const Descriptor* message, "field", field->name(), "key", ToUpper(key->type_name()), "value", ToUpper(val->type_name()), - "number", SimpleItoa(field->number()), + "number", StrCat(field->number()), "other", EnumOrMessageSuffix(val, true)); } else if (!field->containing_oneof()) { printer->Print( @@ -853,7 +888,7 @@ void GenerateMessageToPool(const string& name_prefix, const Descriptor* message, "field", field->name(), "label", LabelForField(field), "type", ToUpper(field->type_name()), - "number", SimpleItoa(field->number()), + "number", StrCat(field->number()), "other", EnumOrMessageSuffix(field, true)); } } @@ -871,7 +906,7 @@ void GenerateMessageToPool(const string& name_prefix, const Descriptor* message, "\\Google\\Protobuf\\Internal\\GPBType::^type^, ^number^^other^)\n", "field", field->name(), "type", ToUpper(field->type_name()), - "number", SimpleItoa(field->number()), + "number", StrCat(field->number()), "other", EnumOrMessageSuffix(field, true)); } printer->Print("->finish()\n"); @@ -975,7 +1010,7 @@ void GenerateAddFileToPool(const FileDescriptor* file, bool is_descriptor, Outdent(printer); printer->Print( - "));\n\n"); + "), true);\n\n"); } printer->Print( "static::$is_initialized = true;\n"); @@ -1109,14 +1144,18 @@ void GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en, printer.Print( "namespace ^name^;\n\n", "name", fullname.substr(0, lastindex)); + + // We only need this 'use' statement if the enum has a namespace. + // Otherwise, we get a warning that the use statement has no effect. + printer.Print("use UnexpectedValueException;\n\n"); } + GenerateEnumDocComment(&printer, en, is_descriptor); + if (lastindex != string::npos) { fullname = fullname.substr(lastindex + 1); } - GenerateEnumDocComment(&printer, en, is_descriptor); - printer.Print( "class ^name^\n" "{\n", @@ -1131,6 +1170,53 @@ void GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en, "number", IntToString(value->number())); } + printer.Print("\nprivate static $valueToName = [\n"); + Indent(&printer); + for (int i = 0; i < en->value_count(); i++) { + const EnumValueDescriptor* value = en->value(i); + printer.Print("self::^name^ => '^name^',\n", + "name", ConstantNamePrefix(value->name()) + value->name()); + } + Outdent(&printer); + printer.Print("];\n"); + + printer.Print( + "\npublic static function name($value)\n" + "{\n"); + Indent(&printer); + printer.Print("if (!isset(self::$valueToName[$value])) {\n"); + Indent(&printer); + printer.Print("throw new UnexpectedValueException(sprintf(\n"); + Indent(&printer); + Indent(&printer); + printer.Print("'Enum %s has no name defined for value %s', __CLASS__, $value));\n"); + Outdent(&printer); + Outdent(&printer); + Outdent(&printer); + printer.Print("}\n" + "return self::$valueToName[$value];\n"); + Outdent(&printer); + printer.Print("}\n\n"); + + printer.Print( + "\npublic static function value($name)\n" + "{\n"); + Indent(&printer); + printer.Print("$const = __CLASS__ . '::' . strtoupper($name);\n" + "if (!defined($const)) {\n"); + Indent(&printer); + printer.Print("throw new UnexpectedValueException(sprintf(\n"); + Indent(&printer); + Indent(&printer); + printer.Print("'Enum %s has no value defined for name %s', __CLASS__, $name));\n"); + Outdent(&printer); + Outdent(&printer); + Outdent(&printer); + printer.Print("}\n" + "return constant($const);\n"); + Outdent(&printer); + printer.Print("}\n"); + Outdent(&printer); printer.Print("}\n\n"); @@ -1179,7 +1265,7 @@ void GenerateMessageFile(const FileDescriptor* file, const Descriptor* message, } printer.Print( - "class ^name^ extends \\Google\\Protobuf\\Internal\\Message\n" + "final class ^name^ extends \\Google\\Protobuf\\Internal\\Message\n" "{\n", "name", fullname); Indent(&printer); @@ -1497,6 +1583,41 @@ void GenerateFieldDocComment(io::Printer* printer, const FieldDescriptor* field, printer->Print(" */\n"); } +void GenerateWrapperFieldGetterDocComment(io::Printer* printer, const FieldDescriptor* field) { + // Generate a doc comment for the special getXXXValue methods that are + // generated for wrapper types. + const FieldDescriptor* primitiveField = field->message_type()->FindFieldByName("value"); + printer->Print("/**\n"); + printer->Print( + " * Returns the unboxed value from get^camel_name^()\n\n", + "camel_name", UnderscoresToCamelCase(field->name(), true)); + GenerateDocCommentBody(printer, field); + printer->Print( + " * Generated from protobuf field ^def^\n", + "def", EscapePhpdoc(FirstLineOf(field->DebugString()))); + printer->Print(" * @return ^php_type^|null\n", + "php_type", PhpGetterTypeName(primitiveField, false)); + printer->Print(" */\n"); +} + +void GenerateWrapperFieldSetterDocComment(io::Printer* printer, const FieldDescriptor* field) { + // Generate a doc comment for the special setXXXValue methods that are + // generated for wrapper types. + const FieldDescriptor* primitiveField = field->message_type()->FindFieldByName("value"); + printer->Print("/**\n"); + printer->Print( + " * Sets the field by wrapping a primitive type in a ^message_name^ object.\n\n", + "message_name", LegacyFullClassName(field->message_type(), false)); + GenerateDocCommentBody(printer, field); + printer->Print( + " * Generated from protobuf field ^def^\n", + "def", EscapePhpdoc(FirstLineOf(field->DebugString()))); + printer->Print(" * @param ^php_type^|null $var\n", + "php_type", PhpSetterTypeName(primitiveField, false)); + printer->Print(" * @return $this\n"); + printer->Print(" */\n"); +} + void GenerateEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_, int is_descriptor) { printer->Print("/**\n"); diff --git a/src/google/protobuf/compiler/php/php_generator.h b/src/google/protobuf/compiler/php/php_generator.h index b851d9b4a2..ef708ab56d 100644 --- a/src/google/protobuf/compiler/php/php_generator.h +++ b/src/google/protobuf/compiler/php/php_generator.h @@ -36,13 +36,14 @@ #include +#include + namespace google { namespace protobuf { namespace compiler { namespace php { -class LIBPROTOC_EXPORT Generator - : public google::protobuf::compiler::CodeGenerator { +class PROTOC_EXPORT Generator : public CodeGenerator { virtual bool Generate( const FileDescriptor* file, const string& parameter, @@ -53,16 +54,20 @@ class LIBPROTOC_EXPORT Generator // To skip reserved keywords in php, some generated classname are prefixed. // Other code generators may need following API to figure out the actual // classname. -LIBPROTOC_EXPORT std::string GeneratedClassName( - const google::protobuf::Descriptor* desc); -LIBPROTOC_EXPORT std::string GeneratedClassName( - const google::protobuf::EnumDescriptor* desc); -LIBPROTOC_EXPORT std::string GeneratedClassName( - const google::protobuf::ServiceDescriptor* desc); +PROTOC_EXPORT std::string GeneratedClassName(const Descriptor* desc); +PROTOC_EXPORT std::string GeneratedClassName(const EnumDescriptor* desc); +PROTOC_EXPORT std::string GeneratedClassName(const ServiceDescriptor* desc); + +inline bool IsWrapperType(const FieldDescriptor* descriptor) { + return descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && + descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto"; +} } // namespace php } // namespace compiler } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_PHP_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/plugin.cc b/src/google/protobuf/compiler/plugin.cc index 9c1c757c99..ac383f88ed 100644 --- a/src/google/protobuf/compiler/plugin.cc +++ b/src/google/protobuf/compiler/plugin.cc @@ -45,9 +45,9 @@ #include #include #include +#include #include #include -#include namespace google { @@ -57,14 +57,13 @@ namespace compiler { #if defined(_WIN32) // DO NOT include , instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::setmode; +using google::protobuf::io::win32::setmode; #endif class GeneratorResponseContext : public GeneratorContext { public: GeneratorResponseContext( - const Version& compiler_version, - CodeGeneratorResponse* response, + const Version& compiler_version, CodeGeneratorResponse* response, const std::vector& parsed_files) : compiler_version_(compiler_version), response_(response), @@ -73,14 +72,14 @@ class GeneratorResponseContext : public GeneratorContext { // implements GeneratorContext -------------------------------------- - virtual io::ZeroCopyOutputStream* Open(const string& filename) { + virtual io::ZeroCopyOutputStream* Open(const std::string& filename) { CodeGeneratorResponse::File* file = response_->add_file(); file->set_name(filename); return new io::StringOutputStream(file->mutable_content()); } virtual io::ZeroCopyOutputStream* OpenForInsert( - const string& filename, const string& insertion_point) { + const std::string& filename, const std::string& insertion_point) { CodeGeneratorResponse::File* file = response_->add_file(); file->set_name(filename); file->set_insertion_point(insertion_point); @@ -102,8 +101,8 @@ class GeneratorResponseContext : public GeneratorContext { }; bool GenerateCode(const CodeGeneratorRequest& request, - const CodeGenerator& generator, CodeGeneratorResponse* response, - string* error_msg) { + const CodeGenerator& generator, + CodeGeneratorResponse* response, std::string* error_msg) { DescriptorPool pool; for (int i = 0; i < request.proto_file_size(); i++) { const FileDescriptor* file = pool.BuildFile(request.proto_file(i)); @@ -117,24 +116,26 @@ bool GenerateCode(const CodeGeneratorRequest& request, for (int i = 0; i < request.file_to_generate_size(); i++) { parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i))); if (parsed_files.back() == NULL) { - *error_msg = "protoc asked plugin to generate a file but " - "did not provide a descriptor for the file: " + - request.file_to_generate(i); + *error_msg = + "protoc asked plugin to generate a file but " + "did not provide a descriptor for the file: " + + request.file_to_generate(i); return false; } } - GeneratorResponseContext context( - request.compiler_version(), response, parsed_files); + GeneratorResponseContext context(request.compiler_version(), response, + parsed_files); - string error; - bool succeeded = generator.GenerateAll( - parsed_files, request.parameter(), &context, &error); + std::string error; + bool succeeded = generator.GenerateAll(parsed_files, request.parameter(), + &context, &error); if (!succeeded && error.empty()) { - error = "Code generator returned false but provided no error " - "description."; + error = + "Code generator returned false but provided no error " + "description."; } if (!error.empty()) { response->set_error(error); @@ -162,7 +163,7 @@ int PluginMain(int argc, char* argv[], const CodeGenerator* generator) { return 1; } - string error_msg; + std::string error_msg; CodeGeneratorResponse response; if (GenerateCode(request, *generator, &response, &error_msg)) { diff --git a/src/google/protobuf/compiler/plugin.h b/src/google/protobuf/compiler/plugin.h index c664917320..69c5d3f537 100644 --- a/src/google/protobuf/compiler/plugin.h +++ b/src/google/protobuf/compiler/plugin.h @@ -64,27 +64,31 @@ #include -#include +#include + namespace google { namespace protobuf { namespace compiler { -class CodeGenerator; // code_generator.h +class CodeGenerator; // code_generator.h class CodeGeneratorRequest; class CodeGeneratorResponse; // Implements main() for a protoc plugin exposing the given code generator. -LIBPROTOC_EXPORT int PluginMain(int argc, char* argv[], const CodeGenerator* generator); +PROTOC_EXPORT int PluginMain(int argc, char* argv[], + const CodeGenerator* generator); // Generates code using the given code generator. Returns true if the code // generation is successful. If the code geneartion fails, error_msg may be // populated to describe the failure cause. bool GenerateCode(const CodeGeneratorRequest& request, - const CodeGenerator& generator, CodeGeneratorResponse* response, - string* error_msg); + const CodeGenerator& generator, + CodeGeneratorResponse* response, std::string* error_msg); } // namespace compiler } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_PLUGIN_H__ diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index 7569f27ffe..dcfaca3545 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -5,186 +5,164 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fcompiler_2fplugin_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fcompiler_2fplugin_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<6> scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -namespace google { -namespace protobuf { +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<6> scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fcompiler_2fplugin_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fcompiler_2fplugin_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto; +PROTOBUF_NAMESPACE_OPEN namespace compiler { class VersionDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Version_default_instance_; class CodeGeneratorRequestDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _CodeGeneratorRequest_default_instance_; class CodeGeneratorResponse_FileDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _CodeGeneratorResponse_File_default_instance_; class CodeGeneratorResponseDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _CodeGeneratorResponse_default_instance_; } // namespace compiler -} // namespace protobuf -} // namespace google -static void InitDefaultsVersion_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::compiler::_Version_default_instance_; - new (ptr) ::google::protobuf::compiler::Version(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::compiler::_CodeGeneratorRequest_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::compiler::Version::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest::InitAsDefaultInstance(); } -LIBPROTOC_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsVersion_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, {}}; +PROTOC_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<2> scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 2, InitDefaultsscc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, { + &scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base,}}; -static void InitDefaultsCodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { +static void InitDefaultsscc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::compiler::_CodeGeneratorRequest_default_instance_; - new (ptr) ::google::protobuf::compiler::CodeGeneratorRequest(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::compiler::_CodeGeneratorResponse_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::compiler::CodeGeneratorRequest::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse::InitAsDefaultInstance(); } -LIBPROTOC_EXPORT ::google::protobuf::internal::SCCInfo<2> scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 2, InitDefaultsCodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, { - &scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base,}}; +PROTOC_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, { + &scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base,}}; -static void InitDefaultsCodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { +static void InitDefaultsscc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::compiler::_CodeGeneratorResponse_File_default_instance_; - new (ptr) ::google::protobuf::compiler::CodeGeneratorResponse_File(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::compiler::_CodeGeneratorResponse_File_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::compiler::CodeGeneratorResponse_File::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File::InitAsDefaultInstance(); } -LIBPROTOC_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsCodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, {}}; +PROTOC_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, {}}; -static void InitDefaultsCodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { +static void InitDefaultsscc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::compiler::_CodeGeneratorResponse_default_instance_; - new (ptr) ::google::protobuf::compiler::CodeGeneratorResponse(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::compiler::_Version_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::compiler::Version(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::compiler::CodeGeneratorResponse::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::compiler::Version::InitAsDefaultInstance(); } -LIBPROTOC_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsCodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, { - &scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base,}}; - -void InitDefaults_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); -} +PROTOC_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto[4]; -constexpr ::google::protobuf::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto = nullptr; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto[4]; +static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto = nullptr; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::Version, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::Version, _internal_metadata_), +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::Version, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::Version, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::Version, major_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::Version, minor_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::Version, patch_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::Version, suffix_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::Version, major_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::Version, minor_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::Version, patch_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::Version, suffix_), 1, 2, 3, 0, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorRequest, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorRequest, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorRequest, file_to_generate_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorRequest, parameter_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorRequest, proto_file_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorRequest, compiler_version_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest, file_to_generate_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest, parameter_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest, proto_file_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest, compiler_version_), ~0u, 0, ~0u, 1, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse_File, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse_File, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse_File, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse_File, insertion_point_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse_File, content_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File, insertion_point_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File, content_), 0, 1, 2, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse, error_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::compiler::CodeGeneratorResponse, file_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse, error_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse, file_), 0, ~0u, }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, 9, sizeof(::google::protobuf::compiler::Version)}, - { 13, 22, sizeof(::google::protobuf::compiler::CodeGeneratorRequest)}, - { 26, 34, sizeof(::google::protobuf::compiler::CodeGeneratorResponse_File)}, - { 37, 44, sizeof(::google::protobuf::compiler::CodeGeneratorResponse)}, +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, 9, sizeof(PROTOBUF_NAMESPACE_ID::compiler::Version)}, + { 13, 22, sizeof(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest)}, + { 26, 34, sizeof(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File)}, + { 37, 44, sizeof(PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse)}, }; -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::compiler::_Version_default_instance_), - reinterpret_cast(&::google::protobuf::compiler::_CodeGeneratorRequest_default_instance_), - reinterpret_cast(&::google::protobuf::compiler::_CodeGeneratorResponse_File_default_instance_), - reinterpret_cast(&::google::protobuf::compiler::_CodeGeneratorResponse_default_instance_), -}; - -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto, "google/protobuf/compiler/plugin.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto, 4, file_level_enum_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto, file_level_service_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto, +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::compiler::_Version_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::compiler::_CodeGeneratorRequest_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::compiler::_CodeGeneratorResponse_File_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::compiler::_CodeGeneratorResponse_default_instance_), }; -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto = { - false, InitDefaults_google_2fprotobuf_2fcompiler_2fplugin_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2fcompiler_2fplugin_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n%google/protobuf/compiler/plugin.proto\022" "\030google.protobuf.compiler\032 google/protob" "uf/descriptor.proto\"F\n\007Version\022\r\n\005major\030" @@ -201,64 +179,64 @@ static ::google::protobuf::Message const * const file_default_instances[] = { "content\030\017 \001(\tBg\n\034com.google.protobuf.com" "pilerB\014PluginProtosZ9github.com/golang/p" "rotobuf/protoc-gen-go/plugin;plugin_go" -, - "google/protobuf/compiler/plugin.proto", &assign_descriptors_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto, 638, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_deps[1] = { + &::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_sccs[4] = { + &scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base, + &scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base, + &scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base, + &scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto = { + &descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2fcompiler_2fplugin_2eproto, "google/protobuf/compiler/plugin.proto", 638, + &descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_once, descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_sccs, descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto_deps, 4, 1, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto, 4, file_level_enum_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto, file_level_service_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - ::AddDescriptors_google_2fprotobuf_2fdescriptor_2eproto, - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto, deps, 1); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2fcompiler_2fplugin_2eproto = []() { AddDescriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto(); return true; }(); -namespace google { -namespace protobuf { +static bool dynamic_init_dummy_google_2fprotobuf_2fcompiler_2fplugin_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto), true); +PROTOBUF_NAMESPACE_OPEN namespace compiler { // =================================================================== void Version::InitAsDefaultInstance() { } -class Version::HasBitSetters { +class Version::_Internal { public: - static void set_has_major(Version* msg) { - msg->_has_bits_[0] |= 0x00000002u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_major(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_minor(Version* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_minor(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_patch(Version* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_patch(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_suffix(Version* msg) { - msg->_has_bits_[0] |= 0x00000001u; + static void set_has_suffix(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Version::kMajorFieldNumber; -const int Version::kMinorFieldNumber; -const int Version::kPatchFieldNumber; -const int Version::kSuffixFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Version::Version() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.compiler.Version) } Version::Version(const Version& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - suffix_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + suffix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_suffix()) { - suffix_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.suffix_); + suffix_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.suffix_); } ::memcpy(&major_, &from.major_, static_cast(reinterpret_cast(&patch_) - @@ -267,9 +245,8 @@ Version::Version(const Version& from) } void Version::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - suffix_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + suffix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&major_, 0, static_cast( reinterpret_cast(&patch_) - reinterpret_cast(&major_)) + sizeof(patch_)); @@ -281,21 +258,21 @@ Version::~Version() { } void Version::SharedDtor() { - suffix_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + suffix_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void Version::SetCachedSize(int size) const { _cached_size_.Set(size); } const Version& Version::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); return *internal_default_instance(); } void Version::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.Version) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -313,106 +290,82 @@ void Version::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Version::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Version::_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) { - case 0: goto error; // optional int32 major = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_major(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + _Internal::set_has_major(&has_bits); + major_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 minor = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_minor(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_minor(&has_bits); + minor_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 patch = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_patch(value); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { + _Internal::set_has_patch(&has_bits); + patch_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string suffix = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.compiler.Version.suffix"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_suffix(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_suffix(), ptr, ctx, "google.protobuf.compiler.Version.suffix"); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Version::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.compiler.Version) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional int32 major = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_major(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { + _Internal::set_has_major(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &major_))); } else { goto handle_unusual; @@ -422,10 +375,10 @@ bool Version::MergePartialFromCodedStream( // optional int32 minor = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_minor(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_minor(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &minor_))); } else { goto handle_unusual; @@ -435,10 +388,10 @@ bool Version::MergePartialFromCodedStream( // optional int32 patch = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_patch(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { + _Internal::set_has_patch(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &patch_))); } else { goto handle_unusual; @@ -448,12 +401,12 @@ bool Version::MergePartialFromCodedStream( // optional string suffix = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (34 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_suffix())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->suffix().data(), static_cast(this->suffix().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.compiler.Version.suffix"); } else { goto handle_unusual; @@ -466,7 +419,7 @@ bool Version::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -482,82 +435,44 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Version::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.compiler.Version) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional int32 major = 1; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(1, this->major(), output); - } - - // optional int32 minor = 2; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->minor(), output); - } - - // optional int32 patch = 3; - if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->patch(), output); - } - - // optional string suffix = 4; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->suffix().data(), static_cast(this->suffix().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.compiler.Version.suffix"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 4, this->suffix(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.compiler.Version) -} - -::google::protobuf::uint8* Version::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Version::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.compiler.Version) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional int32 major = 1; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(1, this->major(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(1, this->major(), target); } // optional int32 minor = 2; if (cached_has_bits & 0x00000004u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->minor(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(2, this->minor(), target); } // optional int32 patch = 3; if (cached_has_bits & 0x00000008u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->patch(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(3, this->patch(), target); } // optional string suffix = 4; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->suffix().data(), static_cast(this->suffix().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.compiler.Version.suffix"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 4, this->suffix(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.compiler.Version) return target; @@ -567,12 +482,7 @@ size_t Version::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.compiler.Version) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -581,46 +491,50 @@ size_t Version::ByteSizeLong() const { // optional string suffix = 4; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->suffix()); } // optional int32 major = 1; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->major()); } // optional int32 minor = 2; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->minor()); } // optional int32 patch = 3; if (cached_has_bits & 0x00000008u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->patch()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Version::MergeFrom(const ::google::protobuf::Message& from) { +void Version::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.compiler.Version) GOOGLE_DCHECK_NE(&from, this); const Version* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.compiler.Version) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.compiler.Version) MergeFrom(*source); @@ -631,14 +545,14 @@ void Version::MergeFrom(const Version& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.compiler.Version) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = from._has_bits_[0]; if (cached_has_bits & 0x0000000fu) { if (cached_has_bits & 0x00000001u) { _has_bits_[0] |= 0x00000001u; - suffix_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.suffix_); + suffix_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.suffix_); } if (cached_has_bits & 0x00000002u) { major_ = from.major_; @@ -653,7 +567,7 @@ void Version::MergeFrom(const Version& from) { } } -void Version::CopyFrom(const ::google::protobuf::Message& from) { +void Version::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.compiler.Version) if (&from == this) return; Clear(); @@ -671,87 +585,75 @@ bool Version::IsInitialized() const { return true; } -void Version::Swap(Version* other) { - if (other == this) return; - InternalSwap(other); -} void Version::InternalSwap(Version* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); - suffix_.Swap(&other->suffix_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + suffix_.Swap(&other->suffix_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(major_, other->major_); swap(minor_, other->minor_); swap(patch_, other->patch_); } -::google::protobuf::Metadata Version::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Version::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void CodeGeneratorRequest::InitAsDefaultInstance() { - ::google::protobuf::compiler::_CodeGeneratorRequest_default_instance_._instance.get_mutable()->compiler_version_ = const_cast< ::google::protobuf::compiler::Version*>( - ::google::protobuf::compiler::Version::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::compiler::_CodeGeneratorRequest_default_instance_._instance.get_mutable()->compiler_version_ = const_cast< PROTOBUF_NAMESPACE_ID::compiler::Version*>( + PROTOBUF_NAMESPACE_ID::compiler::Version::internal_default_instance()); } -class CodeGeneratorRequest::HasBitSetters { +class CodeGeneratorRequest::_Internal { public: - static void set_has_parameter(CodeGeneratorRequest* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_parameter(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static const ::google::protobuf::compiler::Version& compiler_version(const CodeGeneratorRequest* msg); - static void set_has_compiler_version(CodeGeneratorRequest* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static const PROTOBUF_NAMESPACE_ID::compiler::Version& compiler_version(const CodeGeneratorRequest* msg); + static void set_has_compiler_version(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -const ::google::protobuf::compiler::Version& -CodeGeneratorRequest::HasBitSetters::compiler_version(const CodeGeneratorRequest* msg) { +const PROTOBUF_NAMESPACE_ID::compiler::Version& +CodeGeneratorRequest::_Internal::compiler_version(const CodeGeneratorRequest* msg) { return *msg->compiler_version_; } void CodeGeneratorRequest::clear_proto_file() { proto_file_.Clear(); } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int CodeGeneratorRequest::kFileToGenerateFieldNumber; -const int CodeGeneratorRequest::kParameterFieldNumber; -const int CodeGeneratorRequest::kProtoFileFieldNumber; -const int CodeGeneratorRequest::kCompilerVersionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - CodeGeneratorRequest::CodeGeneratorRequest() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.compiler.CodeGeneratorRequest) } CodeGeneratorRequest::CodeGeneratorRequest(const CodeGeneratorRequest& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), file_to_generate_(from.file_to_generate_), proto_file_(from.proto_file_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - parameter_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + parameter_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_parameter()) { - parameter_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.parameter_); + parameter_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.parameter_); } if (from.has_compiler_version()) { - compiler_version_ = new ::google::protobuf::compiler::Version(*from.compiler_version_); + compiler_version_ = new PROTOBUF_NAMESPACE_ID::compiler::Version(*from.compiler_version_); } else { - compiler_version_ = NULL; + compiler_version_ = nullptr; } // @@protoc_insertion_point(copy_constructor:google.protobuf.compiler.CodeGeneratorRequest) } void CodeGeneratorRequest::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - parameter_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - compiler_version_ = NULL; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + parameter_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + compiler_version_ = nullptr; } CodeGeneratorRequest::~CodeGeneratorRequest() { @@ -760,7 +662,7 @@ CodeGeneratorRequest::~CodeGeneratorRequest() { } void CodeGeneratorRequest::SharedDtor() { - parameter_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + parameter_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete compiler_version_; } @@ -768,14 +670,14 @@ void CodeGeneratorRequest::SetCachedSize(int size) const { _cached_size_.Set(size); } const CodeGeneratorRequest& CodeGeneratorRequest::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); return *internal_default_instance(); } void CodeGeneratorRequest::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorRequest) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -787,7 +689,7 @@ void CodeGeneratorRequest::Clear() { parameter_.ClearNonDefaultToEmptyNoArena(); } if (cached_has_bits & 0x00000002u) { - GOOGLE_DCHECK(compiler_version_ != NULL); + GOOGLE_DCHECK(compiler_version_ != nullptr); compiler_version_->Clear(); } } @@ -796,127 +698,92 @@ void CodeGeneratorRequest::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* CodeGeneratorRequest::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* CodeGeneratorRequest::_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) { - case 0: goto error; // repeated string file_to_generate = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.compiler.CodeGeneratorRequest.file_to_generate"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->add_file_to_generate(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 10 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr -= 1; + do { + ptr += 1; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_file_to_generate(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorRequest.file_to_generate"); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 10); + } else goto handle_unusual; + continue; // optional string parameter = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.compiler.CodeGeneratorRequest.parameter"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_parameter(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_parameter(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorRequest.parameter"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.compiler.Version compiler_version = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::compiler::Version::_InternalParse; - object = msg->mutable_compiler_version(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ctx->ParseMessage(mutable_compiler_version(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.FileDescriptorProto proto_file = 15; - case 15: { - if (static_cast<::google::protobuf::uint8>(tag) != 122) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::FileDescriptorProto::_InternalParse; - object = msg->add_proto_file(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 122 && (ptr += 1)); - break; - } + case 15: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 122)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_proto_file(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 122); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool CodeGeneratorRequest::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.compiler.CodeGeneratorRequest) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated string file_to_generate = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->add_file_to_generate())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->file_to_generate(this->file_to_generate_size() - 1).data(), static_cast(this->file_to_generate(this->file_to_generate_size() - 1).length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.compiler.CodeGeneratorRequest.file_to_generate"); } else { goto handle_unusual; @@ -926,12 +793,12 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // optional string parameter = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_parameter())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->parameter().data(), static_cast(this->parameter().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.compiler.CodeGeneratorRequest.parameter"); } else { goto handle_unusual; @@ -941,8 +808,8 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // optional .google.protobuf.compiler.Version compiler_version = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_compiler_version())); } else { goto handle_unusual; @@ -952,8 +819,8 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // repeated .google.protobuf.FileDescriptorProto proto_file = 15; case 15: { - if (static_cast< ::google::protobuf::uint8>(tag) == (122 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (122 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_proto_file())); } else { goto handle_unusual; @@ -966,7 +833,7 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -982,102 +849,53 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void CodeGeneratorRequest::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.compiler.CodeGeneratorRequest) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated string file_to_generate = 1; - for (int i = 0, n = this->file_to_generate_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->file_to_generate(i).data(), static_cast(this->file_to_generate(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.compiler.CodeGeneratorRequest.file_to_generate"); - ::google::protobuf::internal::WireFormatLite::WriteString( - 1, this->file_to_generate(i), output); - } - - cached_has_bits = _has_bits_[0]; - // optional string parameter = 2; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->parameter().data(), static_cast(this->parameter().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.compiler.CodeGeneratorRequest.parameter"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 2, this->parameter(), output); - } - - // optional .google.protobuf.compiler.Version compiler_version = 3; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 3, HasBitSetters::compiler_version(this), output); - } - - // repeated .google.protobuf.FileDescriptorProto proto_file = 15; - for (unsigned int i = 0, - n = static_cast(this->proto_file_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 15, - this->proto_file(static_cast(i)), - output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.compiler.CodeGeneratorRequest) -} - -::google::protobuf::uint8* CodeGeneratorRequest::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* CodeGeneratorRequest::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.compiler.CodeGeneratorRequest) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated string file_to_generate = 1; - for (int i = 0, n = this->file_to_generate_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->file_to_generate(i).data(), static_cast(this->file_to_generate(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + for (auto it = this->file_to_generate().pointer_begin(), + end = this->file_to_generate().pointer_end(); it < end; ++it) { + const auto& s = **it; + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( + s.data(), static_cast(s.length()), + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.compiler.CodeGeneratorRequest.file_to_generate"); - target = ::google::protobuf::internal::WireFormatLite:: - WriteStringToArray(1, this->file_to_generate(i), target); + target = stream->WriteString(1, s, target); } cached_has_bits = _has_bits_[0]; // optional string parameter = 2; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->parameter().data(), static_cast(this->parameter().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.compiler.CodeGeneratorRequest.parameter"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 2, this->parameter(), target); } // optional .google.protobuf.compiler.Version compiler_version = 3; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 3, HasBitSetters::compiler_version(this), deterministic, target); + 3, _Internal::compiler_version(this), target, stream); } // repeated .google.protobuf.FileDescriptorProto proto_file = 15; - for (unsigned int i = 0, - n = static_cast(this->proto_file_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 15, this->proto_file(static_cast(i)), deterministic, target); + for (auto it = this->proto_file().pointer_begin(), + end = this->proto_file().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(15, **it, target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.compiler.CodeGeneratorRequest) return target; @@ -1087,20 +905,15 @@ size_t CodeGeneratorRequest::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.compiler.CodeGeneratorRequest) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // repeated string file_to_generate = 1; total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->file_to_generate_size()); + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->file_to_generate_size()); for (int i = 0, n = this->file_to_generate_size(); i < n; i++) { - total_size += ::google::protobuf::internal::WireFormatLite::StringSize( + total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->file_to_generate(i)); } @@ -1110,7 +923,7 @@ size_t CodeGeneratorRequest::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->proto_file(static_cast(i))); } } @@ -1120,32 +933,36 @@ size_t CodeGeneratorRequest::ByteSizeLong() const { // optional string parameter = 2; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->parameter()); } // optional .google.protobuf.compiler.Version compiler_version = 3; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *compiler_version_); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void CodeGeneratorRequest::MergeFrom(const ::google::protobuf::Message& from) { +void CodeGeneratorRequest::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.compiler.CodeGeneratorRequest) GOOGLE_DCHECK_NE(&from, this); const CodeGeneratorRequest* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.compiler.CodeGeneratorRequest) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.compiler.CodeGeneratorRequest) MergeFrom(*source); @@ -1156,7 +973,7 @@ void CodeGeneratorRequest::MergeFrom(const CodeGeneratorRequest& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.compiler.CodeGeneratorRequest) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; file_to_generate_.MergeFrom(from.file_to_generate_); @@ -1165,15 +982,15 @@ void CodeGeneratorRequest::MergeFrom(const CodeGeneratorRequest& from) { if (cached_has_bits & 0x00000003u) { if (cached_has_bits & 0x00000001u) { _has_bits_[0] |= 0x00000001u; - parameter_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.parameter_); + parameter_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.parameter_); } if (cached_has_bits & 0x00000002u) { - mutable_compiler_version()->::google::protobuf::compiler::Version::MergeFrom(from.compiler_version()); + mutable_compiler_version()->PROTOBUF_NAMESPACE_ID::compiler::Version::MergeFrom(from.compiler_version()); } } } -void CodeGeneratorRequest::CopyFrom(const ::google::protobuf::Message& from) { +void CodeGeneratorRequest::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.compiler.CodeGeneratorRequest) if (&from == this) return; Clear(); @@ -1188,28 +1005,23 @@ void CodeGeneratorRequest::CopyFrom(const CodeGeneratorRequest& from) { } bool CodeGeneratorRequest::IsInitialized() const { - if (!::google::protobuf::internal::AllAreInitialized(this->proto_file())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->proto_file())) return false; return true; } -void CodeGeneratorRequest::Swap(CodeGeneratorRequest* other) { - if (other == this) return; - InternalSwap(other); -} void CodeGeneratorRequest::InternalSwap(CodeGeneratorRequest* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); file_to_generate_.InternalSwap(CastToBase(&other->file_to_generate_)); CastToBase(&proto_file_)->InternalSwap(CastToBase(&other->proto_file_)); - parameter_.Swap(&other->parameter_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + parameter_.Swap(&other->parameter_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(compiler_version_, other->compiler_version_); } -::google::protobuf::Metadata CodeGeneratorRequest::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata CodeGeneratorRequest::GetMetadata() const { + return GetMetadataStatic(); } @@ -1217,56 +1029,50 @@ void CodeGeneratorRequest::InternalSwap(CodeGeneratorRequest* other) { void CodeGeneratorResponse_File::InitAsDefaultInstance() { } -class CodeGeneratorResponse_File::HasBitSetters { +class CodeGeneratorResponse_File::_Internal { public: - static void set_has_name(CodeGeneratorResponse_File* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_insertion_point(CodeGeneratorResponse_File* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_insertion_point(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_content(CodeGeneratorResponse_File* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_content(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int CodeGeneratorResponse_File::kNameFieldNumber; -const int CodeGeneratorResponse_File::kInsertionPointFieldNumber; -const int CodeGeneratorResponse_File::kContentFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - CodeGeneratorResponse_File::CodeGeneratorResponse_File() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.compiler.CodeGeneratorResponse.File) } CodeGeneratorResponse_File::CodeGeneratorResponse_File(const CodeGeneratorResponse_File& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_); + name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); } - insertion_point_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + insertion_point_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_insertion_point()) { - insertion_point_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.insertion_point_); + insertion_point_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.insertion_point_); } - content_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + content_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_content()) { - content_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.content_); + content_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.content_); } // @@protoc_insertion_point(copy_constructor:google.protobuf.compiler.CodeGeneratorResponse.File) } void CodeGeneratorResponse_File::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - insertion_point_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - content_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + insertion_point_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + content_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } CodeGeneratorResponse_File::~CodeGeneratorResponse_File() { @@ -1275,23 +1081,23 @@ CodeGeneratorResponse_File::~CodeGeneratorResponse_File() { } void CodeGeneratorResponse_File::SharedDtor() { - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - insertion_point_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - content_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + insertion_point_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + content_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void CodeGeneratorResponse_File::SetCachedSize(int size) const { _cached_size_.Set(size); } const CodeGeneratorResponse_File& CodeGeneratorResponse_File::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); return *internal_default_instance(); } void CodeGeneratorResponse_File::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorResponse.File) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1312,110 +1118,74 @@ void CodeGeneratorResponse_File::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* CodeGeneratorResponse_File::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* CodeGeneratorResponse_File::_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) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.compiler.CodeGeneratorResponse.File.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorResponse.File.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string insertion_point = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_insertion_point(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_insertion_point(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string content = 15; - case 15: { - if (static_cast<::google::protobuf::uint8>(tag) != 122) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.compiler.CodeGeneratorResponse.File.content"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_content(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 15: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 122)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_content(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorResponse.File.content"); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool CodeGeneratorResponse_File::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.compiler.CodeGeneratorResponse.File) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.compiler.CodeGeneratorResponse.File.name"); } else { goto handle_unusual; @@ -1425,12 +1195,12 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( // optional string insertion_point = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_insertion_point())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->insertion_point().data(), static_cast(this->insertion_point().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point"); } else { goto handle_unusual; @@ -1440,12 +1210,12 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( // optional string content = 15; case 15: { - if (static_cast< ::google::protobuf::uint8>(tag) == (122 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (122 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_content())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->content().data(), static_cast(this->content().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.compiler.CodeGeneratorResponse.File.content"); } else { goto handle_unusual; @@ -1458,7 +1228,7 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -1474,94 +1244,46 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void CodeGeneratorResponse_File::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.compiler.CodeGeneratorResponse.File) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.compiler.CodeGeneratorResponse.File.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // optional string insertion_point = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->insertion_point().data(), static_cast(this->insertion_point().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 2, this->insertion_point(), output); - } - - // optional string content = 15; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->content().data(), static_cast(this->content().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.compiler.CodeGeneratorResponse.File.content"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 15, this->content(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.compiler.CodeGeneratorResponse.File) -} - -::google::protobuf::uint8* CodeGeneratorResponse_File::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* CodeGeneratorResponse_File::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.compiler.CodeGeneratorResponse.File) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.compiler.CodeGeneratorResponse.File.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // optional string insertion_point = 2; if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->insertion_point().data(), static_cast(this->insertion_point().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 2, this->insertion_point(), target); } // optional string content = 15; if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->content().data(), static_cast(this->content().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.compiler.CodeGeneratorResponse.File.content"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 15, this->content(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.compiler.CodeGeneratorResponse.File) return target; @@ -1571,12 +1293,7 @@ size_t CodeGeneratorResponse_File::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.compiler.CodeGeneratorResponse.File) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1585,39 +1302,43 @@ size_t CodeGeneratorResponse_File::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional string insertion_point = 2; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->insertion_point()); } // optional string content = 15; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->content()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void CodeGeneratorResponse_File::MergeFrom(const ::google::protobuf::Message& from) { +void CodeGeneratorResponse_File::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.compiler.CodeGeneratorResponse.File) GOOGLE_DCHECK_NE(&from, this); const CodeGeneratorResponse_File* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.compiler.CodeGeneratorResponse.File) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.compiler.CodeGeneratorResponse.File) MergeFrom(*source); @@ -1628,27 +1349,27 @@ void CodeGeneratorResponse_File::MergeFrom(const CodeGeneratorResponse_File& fro // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.compiler.CodeGeneratorResponse.File) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { _has_bits_[0] |= 0x00000001u; - name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_); + name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); } if (cached_has_bits & 0x00000002u) { _has_bits_[0] |= 0x00000002u; - insertion_point_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.insertion_point_); + insertion_point_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.insertion_point_); } if (cached_has_bits & 0x00000004u) { _has_bits_[0] |= 0x00000004u; - content_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.content_); + content_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.content_); } } } -void CodeGeneratorResponse_File::CopyFrom(const ::google::protobuf::Message& from) { +void CodeGeneratorResponse_File::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.compiler.CodeGeneratorResponse.File) if (&from == this) return; Clear(); @@ -1666,25 +1387,20 @@ bool CodeGeneratorResponse_File::IsInitialized() const { return true; } -void CodeGeneratorResponse_File::Swap(CodeGeneratorResponse_File* other) { - if (other == this) return; - InternalSwap(other); -} void CodeGeneratorResponse_File::InternalSwap(CodeGeneratorResponse_File* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - insertion_point_.Swap(&other->insertion_point_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + insertion_point_.Swap(&other->insertion_point_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - content_.Swap(&other->content_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + content_.Swap(&other->content_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -::google::protobuf::Metadata CodeGeneratorResponse_File::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata CodeGeneratorResponse_File::GetMetadata() const { + return GetMetadataStatic(); } @@ -1692,40 +1408,35 @@ void CodeGeneratorResponse_File::InternalSwap(CodeGeneratorResponse_File* other) void CodeGeneratorResponse::InitAsDefaultInstance() { } -class CodeGeneratorResponse::HasBitSetters { +class CodeGeneratorResponse::_Internal { public: - static void set_has_error(CodeGeneratorResponse* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_error(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int CodeGeneratorResponse::kErrorFieldNumber; -const int CodeGeneratorResponse::kFileFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - CodeGeneratorResponse::CodeGeneratorResponse() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.compiler.CodeGeneratorResponse) } CodeGeneratorResponse::CodeGeneratorResponse(const CodeGeneratorResponse& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), file_(from.file_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - error_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + error_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_error()) { - error_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.error_); + error_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.error_); } // @@protoc_insertion_point(copy_constructor:google.protobuf.compiler.CodeGeneratorResponse) } void CodeGeneratorResponse::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - error_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + error_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } CodeGeneratorResponse::~CodeGeneratorResponse() { @@ -1734,21 +1445,21 @@ CodeGeneratorResponse::~CodeGeneratorResponse() { } void CodeGeneratorResponse::SharedDtor() { - error_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + error_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void CodeGeneratorResponse::SetCachedSize(int size) const { _cached_size_.Set(size); } const CodeGeneratorResponse& CodeGeneratorResponse::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); return *internal_default_instance(); } void CodeGeneratorResponse::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorResponse) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1762,94 +1473,72 @@ void CodeGeneratorResponse::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* CodeGeneratorResponse::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* CodeGeneratorResponse::_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) { - case 0: goto error; // optional string error = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.compiler.CodeGeneratorResponse.error"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_error(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_error(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorResponse.error"); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; - case 15: { - if (static_cast<::google::protobuf::uint8>(tag) != 122) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::compiler::CodeGeneratorResponse_File::_InternalParse; - object = msg->add_file(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 122 && (ptr += 1)); - break; - } + case 15: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 122)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_file(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 122); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool CodeGeneratorResponse::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.compiler.CodeGeneratorResponse) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string error = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_error())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->error().data(), static_cast(this->error().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.compiler.CodeGeneratorResponse.error"); } else { goto handle_unusual; @@ -1859,8 +1548,8 @@ bool CodeGeneratorResponse::MergePartialFromCodedStream( // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; case 15: { - if (static_cast< ::google::protobuf::uint8>(tag) == (122 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (122 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_file())); } else { goto handle_unusual; @@ -1873,7 +1562,7 @@ bool CodeGeneratorResponse::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -1889,69 +1578,34 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void CodeGeneratorResponse::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.compiler.CodeGeneratorResponse) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string error = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->error().data(), static_cast(this->error().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.compiler.CodeGeneratorResponse.error"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->error(), output); - } - - // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; - for (unsigned int i = 0, - n = static_cast(this->file_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 15, - this->file(static_cast(i)), - output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.compiler.CodeGeneratorResponse) -} - -::google::protobuf::uint8* CodeGeneratorResponse::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* CodeGeneratorResponse::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.compiler.CodeGeneratorResponse) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional string error = 1; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->error().data(), static_cast(this->error().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.compiler.CodeGeneratorResponse.error"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->error(), target); } // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; - for (unsigned int i = 0, - n = static_cast(this->file_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 15, this->file(static_cast(i)), deterministic, target); + for (auto it = this->file().pointer_begin(), + end = this->file().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(15, **it, target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.compiler.CodeGeneratorResponse) return target; @@ -1961,12 +1615,7 @@ size_t CodeGeneratorResponse::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.compiler.CodeGeneratorResponse) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1976,7 +1625,7 @@ size_t CodeGeneratorResponse::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->file(static_cast(i))); } } @@ -1985,24 +1634,28 @@ size_t CodeGeneratorResponse::ByteSizeLong() const { cached_has_bits = _has_bits_[0]; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->error()); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void CodeGeneratorResponse::MergeFrom(const ::google::protobuf::Message& from) { +void CodeGeneratorResponse::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.compiler.CodeGeneratorResponse) GOOGLE_DCHECK_NE(&from, this); const CodeGeneratorResponse* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.compiler.CodeGeneratorResponse) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.compiler.CodeGeneratorResponse) MergeFrom(*source); @@ -2013,17 +1666,17 @@ void CodeGeneratorResponse::MergeFrom(const CodeGeneratorResponse& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.compiler.CodeGeneratorResponse) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; file_.MergeFrom(from.file_); if (from.has_error()) { _has_bits_[0] |= 0x00000001u; - error_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.error_); + error_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.error_); } } -void CodeGeneratorResponse::CopyFrom(const ::google::protobuf::Message& from) { +void CodeGeneratorResponse::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.compiler.CodeGeneratorResponse) if (&from == this) return; Clear(); @@ -2041,44 +1694,37 @@ bool CodeGeneratorResponse::IsInitialized() const { return true; } -void CodeGeneratorResponse::Swap(CodeGeneratorResponse* other) { - if (other == this) return; - InternalSwap(other); -} void CodeGeneratorResponse::InternalSwap(CodeGeneratorResponse* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); CastToBase(&file_)->InternalSwap(CastToBase(&other->file_)); - error_.Swap(&other->error_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + error_.Swap(&other->error_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -::google::protobuf::Metadata CodeGeneratorResponse::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata CodeGeneratorResponse::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) } // namespace compiler -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::compiler::Version* Arena::CreateMaybeMessage< ::google::protobuf::compiler::Version >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::compiler::Version >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::compiler::Version* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::compiler::Version >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::compiler::Version >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::compiler::CodeGeneratorRequest* Arena::CreateMaybeMessage< ::google::protobuf::compiler::CodeGeneratorRequest >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::compiler::CodeGeneratorRequest >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::compiler::CodeGeneratorResponse_File* Arena::CreateMaybeMessage< ::google::protobuf::compiler::CodeGeneratorResponse_File >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::compiler::CodeGeneratorResponse_File >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::compiler::CodeGeneratorResponse* Arena::CreateMaybeMessage< ::google::protobuf::compiler::CodeGeneratorResponse >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::compiler::CodeGeneratorResponse >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h index 4b7527e5c9..5055df2e15 100644 --- a/src/google/protobuf/compiler/plugin.pb.h +++ b/src/google/protobuf/compiler/plugin.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/compiler/plugin.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2fcompiler_2fplugin_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2fcompiler_2fplugin_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fcompiler_2fplugin_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fcompiler_2fplugin_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,6 +27,7 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export @@ -33,72 +35,75 @@ #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fcompiler_2fplugin_2eproto LIBPROTOC_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fcompiler_2fplugin_2eproto PROTOC_EXPORT #ifdef major #undef major #endif #ifdef minor #undef minor #endif +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOC_EXPORT TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[4] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOC_EXPORT TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[4] + 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[]; }; -void LIBPROTOC_EXPORT AddDescriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto(); -namespace google { -namespace protobuf { +extern PROTOC_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto; +PROTOBUF_NAMESPACE_OPEN namespace compiler { class CodeGeneratorRequest; class CodeGeneratorRequestDefaultTypeInternal; -LIBPROTOC_EXPORT extern CodeGeneratorRequestDefaultTypeInternal _CodeGeneratorRequest_default_instance_; +PROTOC_EXPORT extern CodeGeneratorRequestDefaultTypeInternal _CodeGeneratorRequest_default_instance_; class CodeGeneratorResponse; class CodeGeneratorResponseDefaultTypeInternal; -LIBPROTOC_EXPORT extern CodeGeneratorResponseDefaultTypeInternal _CodeGeneratorResponse_default_instance_; +PROTOC_EXPORT extern CodeGeneratorResponseDefaultTypeInternal _CodeGeneratorResponse_default_instance_; class CodeGeneratorResponse_File; class CodeGeneratorResponse_FileDefaultTypeInternal; -LIBPROTOC_EXPORT extern CodeGeneratorResponse_FileDefaultTypeInternal _CodeGeneratorResponse_File_default_instance_; +PROTOC_EXPORT extern CodeGeneratorResponse_FileDefaultTypeInternal _CodeGeneratorResponse_File_default_instance_; class Version; class VersionDefaultTypeInternal; -LIBPROTOC_EXPORT extern VersionDefaultTypeInternal _Version_default_instance_; +PROTOC_EXPORT extern VersionDefaultTypeInternal _Version_default_instance_; } // namespace compiler -template<> LIBPROTOC_EXPORT ::google::protobuf::compiler::CodeGeneratorRequest* Arena::CreateMaybeMessage<::google::protobuf::compiler::CodeGeneratorRequest>(Arena*); -template<> LIBPROTOC_EXPORT ::google::protobuf::compiler::CodeGeneratorResponse* Arena::CreateMaybeMessage<::google::protobuf::compiler::CodeGeneratorResponse>(Arena*); -template<> LIBPROTOC_EXPORT ::google::protobuf::compiler::CodeGeneratorResponse_File* Arena::CreateMaybeMessage<::google::protobuf::compiler::CodeGeneratorResponse_File>(Arena*); -template<> LIBPROTOC_EXPORT ::google::protobuf::compiler::Version* Arena::CreateMaybeMessage<::google::protobuf::compiler::Version>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOC_EXPORT PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorRequest* Arena::CreateMaybeMessage(Arena*); +template<> PROTOC_EXPORT PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse* Arena::CreateMaybeMessage(Arena*); +template<> PROTOC_EXPORT PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File* Arena::CreateMaybeMessage(Arena*); +template<> PROTOC_EXPORT PROTOBUF_NAMESPACE_ID::compiler::Version* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN namespace compiler { // =================================================================== -class LIBPROTOC_EXPORT Version : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.compiler.Version) */ { +class PROTOC_EXPORT Version : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.compiler.Version) */ { public: Version(); virtual ~Version(); Version(const Version& from); - - inline Version& operator=(const Version& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Version(Version&& from) noexcept : Version() { *this = ::std::move(from); } + inline Version& operator=(const Version& from) { + CopyFrom(from); + return *this; + } inline Version& operator=(Version&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -107,16 +112,22 @@ class LIBPROTOC_EXPORT Version : public ::google::protobuf::Message /* @@protoc_ } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Version& default_instance(); @@ -128,129 +139,139 @@ class LIBPROTOC_EXPORT Version : public ::google::protobuf::Message /* @@protoc_ static constexpr int kIndexInFileMessages = 0; - void Swap(Version* other); friend void swap(Version& a, Version& b) { a.Swap(&b); } + inline void Swap(Version* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Version* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Version* New(::google::protobuf::Arena* arena) const final { + Version* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Version& from); void MergeFrom(const Version& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Version* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.compiler.Version"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto); + return ::descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kSuffixFieldNumber = 4, + kMajorFieldNumber = 1, + kMinorFieldNumber = 2, + kPatchFieldNumber = 3, + }; // optional string suffix = 4; bool has_suffix() const; void clear_suffix(); - static const int kSuffixFieldNumber = 4; - const ::std::string& suffix() const; - void set_suffix(const ::std::string& value); - #if LANG_CXX11 - void set_suffix(::std::string&& value); - #endif + const std::string& suffix() const; + void set_suffix(const std::string& value); + void set_suffix(std::string&& value); void set_suffix(const char* value); void set_suffix(const char* value, size_t size); - ::std::string* mutable_suffix(); - ::std::string* release_suffix(); - void set_allocated_suffix(::std::string* suffix); + std::string* mutable_suffix(); + std::string* release_suffix(); + void set_allocated_suffix(std::string* suffix); // optional int32 major = 1; bool has_major() const; void clear_major(); - static const int kMajorFieldNumber = 1; - ::google::protobuf::int32 major() const; - void set_major(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 major() const; + void set_major(::PROTOBUF_NAMESPACE_ID::int32 value); // optional int32 minor = 2; bool has_minor() const; void clear_minor(); - static const int kMinorFieldNumber = 2; - ::google::protobuf::int32 minor() const; - void set_minor(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 minor() const; + void set_minor(::PROTOBUF_NAMESPACE_ID::int32 value); // optional int32 patch = 3; bool has_patch() const; void clear_patch(); - static const int kPatchFieldNumber = 3; - ::google::protobuf::int32 patch() const; - void set_patch(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 patch() const; + void set_patch(::PROTOBUF_NAMESPACE_ID::int32 value); // @@protoc_insertion_point(class_scope:google.protobuf.compiler.Version) private: - class HasBitSetters; - - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::internal::ArenaStringPtr suffix_; - ::google::protobuf::int32 major_; - ::google::protobuf::int32 minor_; - ::google::protobuf::int32 patch_; + class _Internal; + + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr suffix_; + ::PROTOBUF_NAMESPACE_ID::int32 major_; + ::PROTOBUF_NAMESPACE_ID::int32 minor_; + ::PROTOBUF_NAMESPACE_ID::int32 patch_; friend struct ::TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.compiler.CodeGeneratorRequest) */ { +class PROTOC_EXPORT CodeGeneratorRequest : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.compiler.CodeGeneratorRequest) */ { public: CodeGeneratorRequest(); virtual ~CodeGeneratorRequest(); CodeGeneratorRequest(const CodeGeneratorRequest& from); - - inline CodeGeneratorRequest& operator=(const CodeGeneratorRequest& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 CodeGeneratorRequest(CodeGeneratorRequest&& from) noexcept : CodeGeneratorRequest() { *this = ::std::move(from); } + inline CodeGeneratorRequest& operator=(const CodeGeneratorRequest& from) { + CopyFrom(from); + return *this; + } inline CodeGeneratorRequest& operator=(CodeGeneratorRequest&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -259,16 +280,22 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const CodeGeneratorRequest& default_instance(); @@ -280,151 +307,157 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message static constexpr int kIndexInFileMessages = 1; - void Swap(CodeGeneratorRequest* other); friend void swap(CodeGeneratorRequest& a, CodeGeneratorRequest& b) { a.Swap(&b); } + inline void Swap(CodeGeneratorRequest* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline CodeGeneratorRequest* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - CodeGeneratorRequest* New(::google::protobuf::Arena* arena) const final { + CodeGeneratorRequest* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const CodeGeneratorRequest& from); void MergeFrom(const CodeGeneratorRequest& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(CodeGeneratorRequest* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.compiler.CodeGeneratorRequest"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto); + return ::descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kFileToGenerateFieldNumber = 1, + kProtoFileFieldNumber = 15, + kParameterFieldNumber = 2, + kCompilerVersionFieldNumber = 3, + }; // repeated string file_to_generate = 1; int file_to_generate_size() const; void clear_file_to_generate(); - static const int kFileToGenerateFieldNumber = 1; - const ::std::string& file_to_generate(int index) const; - ::std::string* mutable_file_to_generate(int index); - void set_file_to_generate(int index, const ::std::string& value); - #if LANG_CXX11 - void set_file_to_generate(int index, ::std::string&& value); - #endif + const std::string& file_to_generate(int index) const; + std::string* mutable_file_to_generate(int index); + void set_file_to_generate(int index, const std::string& value); + void set_file_to_generate(int index, std::string&& value); void set_file_to_generate(int index, const char* value); void set_file_to_generate(int index, const char* value, size_t size); - ::std::string* add_file_to_generate(); - void add_file_to_generate(const ::std::string& value); - #if LANG_CXX11 - void add_file_to_generate(::std::string&& value); - #endif + std::string* add_file_to_generate(); + void add_file_to_generate(const std::string& value); + void add_file_to_generate(std::string&& value); void add_file_to_generate(const char* value); void add_file_to_generate(const char* value, size_t size); - const ::google::protobuf::RepeatedPtrField<::std::string>& file_to_generate() const; - ::google::protobuf::RepeatedPtrField<::std::string>* mutable_file_to_generate(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& file_to_generate() const; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_file_to_generate(); // repeated .google.protobuf.FileDescriptorProto proto_file = 15; int proto_file_size() const; void clear_proto_file(); - static const int kProtoFileFieldNumber = 15; - ::google::protobuf::FileDescriptorProto* mutable_proto_file(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto >* + PROTOBUF_NAMESPACE_ID::FileDescriptorProto* mutable_proto_file(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >* mutable_proto_file(); - const ::google::protobuf::FileDescriptorProto& proto_file(int index) const; - ::google::protobuf::FileDescriptorProto* add_proto_file(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::FileDescriptorProto& proto_file(int index) const; + PROTOBUF_NAMESPACE_ID::FileDescriptorProto* add_proto_file(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >& proto_file() const; // optional string parameter = 2; bool has_parameter() const; void clear_parameter(); - static const int kParameterFieldNumber = 2; - const ::std::string& parameter() const; - void set_parameter(const ::std::string& value); - #if LANG_CXX11 - void set_parameter(::std::string&& value); - #endif + const std::string& parameter() const; + void set_parameter(const std::string& value); + void set_parameter(std::string&& value); void set_parameter(const char* value); void set_parameter(const char* value, size_t size); - ::std::string* mutable_parameter(); - ::std::string* release_parameter(); - void set_allocated_parameter(::std::string* parameter); + std::string* mutable_parameter(); + std::string* release_parameter(); + void set_allocated_parameter(std::string* parameter); // optional .google.protobuf.compiler.Version compiler_version = 3; bool has_compiler_version() const; void clear_compiler_version(); - static const int kCompilerVersionFieldNumber = 3; - const ::google::protobuf::compiler::Version& compiler_version() const; - ::google::protobuf::compiler::Version* release_compiler_version(); - ::google::protobuf::compiler::Version* mutable_compiler_version(); - void set_allocated_compiler_version(::google::protobuf::compiler::Version* compiler_version); + const PROTOBUF_NAMESPACE_ID::compiler::Version& compiler_version() const; + PROTOBUF_NAMESPACE_ID::compiler::Version* release_compiler_version(); + PROTOBUF_NAMESPACE_ID::compiler::Version* mutable_compiler_version(); + void set_allocated_compiler_version(PROTOBUF_NAMESPACE_ID::compiler::Version* compiler_version); // @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorRequest) private: - class HasBitSetters; - - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField<::std::string> file_to_generate_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto > proto_file_; - ::google::protobuf::internal::ArenaStringPtr parameter_; - ::google::protobuf::compiler::Version* compiler_version_; + class _Internal; + + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField file_to_generate_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto > proto_file_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr parameter_; + PROTOBUF_NAMESPACE_ID::compiler::Version* compiler_version_; friend struct ::TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.compiler.CodeGeneratorResponse.File) */ { +class PROTOC_EXPORT CodeGeneratorResponse_File : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.compiler.CodeGeneratorResponse.File) */ { public: CodeGeneratorResponse_File(); virtual ~CodeGeneratorResponse_File(); CodeGeneratorResponse_File(const CodeGeneratorResponse_File& from); - - inline CodeGeneratorResponse_File& operator=(const CodeGeneratorResponse_File& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 CodeGeneratorResponse_File(CodeGeneratorResponse_File&& from) noexcept : CodeGeneratorResponse_File() { *this = ::std::move(from); } + inline CodeGeneratorResponse_File& operator=(const CodeGeneratorResponse_File& from) { + CopyFrom(from); + return *this; + } inline CodeGeneratorResponse_File& operator=(CodeGeneratorResponse_File&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -433,16 +466,22 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::M } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const CodeGeneratorResponse_File& default_instance(); @@ -454,137 +493,143 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::M static constexpr int kIndexInFileMessages = 2; - void Swap(CodeGeneratorResponse_File* other); friend void swap(CodeGeneratorResponse_File& a, CodeGeneratorResponse_File& b) { a.Swap(&b); } + inline void Swap(CodeGeneratorResponse_File* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline CodeGeneratorResponse_File* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - CodeGeneratorResponse_File* New(::google::protobuf::Arena* arena) const final { + CodeGeneratorResponse_File* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const CodeGeneratorResponse_File& from); void MergeFrom(const CodeGeneratorResponse_File& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(CodeGeneratorResponse_File* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.compiler.CodeGeneratorResponse.File"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto); + return ::descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kNameFieldNumber = 1, + kInsertionPointFieldNumber = 2, + kContentFieldNumber = 15, + }; // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); // optional string insertion_point = 2; bool has_insertion_point() const; void clear_insertion_point(); - static const int kInsertionPointFieldNumber = 2; - const ::std::string& insertion_point() const; - void set_insertion_point(const ::std::string& value); - #if LANG_CXX11 - void set_insertion_point(::std::string&& value); - #endif + const std::string& insertion_point() const; + void set_insertion_point(const std::string& value); + void set_insertion_point(std::string&& value); void set_insertion_point(const char* value); void set_insertion_point(const char* value, size_t size); - ::std::string* mutable_insertion_point(); - ::std::string* release_insertion_point(); - void set_allocated_insertion_point(::std::string* insertion_point); + std::string* mutable_insertion_point(); + std::string* release_insertion_point(); + void set_allocated_insertion_point(std::string* insertion_point); // optional string content = 15; bool has_content() const; void clear_content(); - static const int kContentFieldNumber = 15; - const ::std::string& content() const; - void set_content(const ::std::string& value); - #if LANG_CXX11 - void set_content(::std::string&& value); - #endif + const std::string& content() const; + void set_content(const std::string& value); + void set_content(std::string&& value); void set_content(const char* value); void set_content(const char* value, size_t size); - ::std::string* mutable_content(); - ::std::string* release_content(); - void set_allocated_content(::std::string* content); + std::string* mutable_content(); + std::string* release_content(); + void set_allocated_content(std::string* content); // @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorResponse.File) private: - class HasBitSetters; - - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::internal::ArenaStringPtr insertion_point_; - ::google::protobuf::internal::ArenaStringPtr content_; + class _Internal; + + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::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 insertion_point_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr content_; friend struct ::TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.compiler.CodeGeneratorResponse) */ { +class PROTOC_EXPORT CodeGeneratorResponse : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.compiler.CodeGeneratorResponse) */ { public: CodeGeneratorResponse(); virtual ~CodeGeneratorResponse(); CodeGeneratorResponse(const CodeGeneratorResponse& from); - - inline CodeGeneratorResponse& operator=(const CodeGeneratorResponse& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 CodeGeneratorResponse(CodeGeneratorResponse&& from) noexcept : CodeGeneratorResponse() { *this = ::std::move(from); } + inline CodeGeneratorResponse& operator=(const CodeGeneratorResponse& from) { + CopyFrom(from); + return *this; + } inline CodeGeneratorResponse& operator=(CodeGeneratorResponse&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -593,16 +638,22 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const CodeGeneratorResponse& default_instance(); @@ -614,56 +665,67 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag static constexpr int kIndexInFileMessages = 3; - void Swap(CodeGeneratorResponse* other); friend void swap(CodeGeneratorResponse& a, CodeGeneratorResponse& b) { a.Swap(&b); } + inline void Swap(CodeGeneratorResponse* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline CodeGeneratorResponse* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - CodeGeneratorResponse* New(::google::protobuf::Arena* arena) const final { + CodeGeneratorResponse* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const CodeGeneratorResponse& from); void MergeFrom(const CodeGeneratorResponse& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(CodeGeneratorResponse* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.compiler.CodeGeneratorResponse"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto); + return ::descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- @@ -671,42 +733,42 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag // accessors ------------------------------------------------------- + enum : int { + kFileFieldNumber = 15, + kErrorFieldNumber = 1, + }; // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; int file_size() const; void clear_file(); - static const int kFileFieldNumber = 15; - ::google::protobuf::compiler::CodeGeneratorResponse_File* mutable_file(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::compiler::CodeGeneratorResponse_File >* + PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File* mutable_file(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File >* mutable_file(); - const ::google::protobuf::compiler::CodeGeneratorResponse_File& file(int index) const; - ::google::protobuf::compiler::CodeGeneratorResponse_File* add_file(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::compiler::CodeGeneratorResponse_File >& + const PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File& file(int index) const; + PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File* add_file(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File >& file() const; // optional string error = 1; bool has_error() const; void clear_error(); - static const int kErrorFieldNumber = 1; - const ::std::string& error() const; - void set_error(const ::std::string& value); - #if LANG_CXX11 - void set_error(::std::string&& value); - #endif + const std::string& error() const; + void set_error(const std::string& value); + void set_error(std::string&& value); void set_error(const char* value); void set_error(const char* value, size_t size); - ::std::string* mutable_error(); - ::std::string* release_error(); - void set_allocated_error(::std::string* error); + std::string* mutable_error(); + std::string* release_error(); + void set_allocated_error(std::string* error); // @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorResponse) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::compiler::CodeGeneratorResponse_File > file_; - ::google::protobuf::internal::ArenaStringPtr error_; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File > file_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr error_; friend struct ::TableStruct_google_2fprotobuf_2fcompiler_2fplugin_2eproto; }; // =================================================================== @@ -728,11 +790,11 @@ inline void Version::clear_major() { major_ = 0; _has_bits_[0] &= ~0x00000002u; } -inline ::google::protobuf::int32 Version::major() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 Version::major() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.Version.major) return major_; } -inline void Version::set_major(::google::protobuf::int32 value) { +inline void Version::set_major(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000002u; major_ = value; // @@protoc_insertion_point(field_set:google.protobuf.compiler.Version.major) @@ -746,11 +808,11 @@ inline void Version::clear_minor() { minor_ = 0; _has_bits_[0] &= ~0x00000004u; } -inline ::google::protobuf::int32 Version::minor() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 Version::minor() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.Version.minor) return minor_; } -inline void Version::set_minor(::google::protobuf::int32 value) { +inline void Version::set_minor(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000004u; minor_ = value; // @@protoc_insertion_point(field_set:google.protobuf.compiler.Version.minor) @@ -764,11 +826,11 @@ inline void Version::clear_patch() { patch_ = 0; _has_bits_[0] &= ~0x00000008u; } -inline ::google::protobuf::int32 Version::patch() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 Version::patch() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.Version.patch) return patch_; } -inline void Version::set_patch(::google::protobuf::int32 value) { +inline void Version::set_patch(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000008u; patch_ = value; // @@protoc_insertion_point(field_set:google.protobuf.compiler.Version.patch) @@ -779,58 +841,56 @@ inline bool Version::has_suffix() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void Version::clear_suffix() { - suffix_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + suffix_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& Version::suffix() const { +inline const std::string& Version::suffix() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.Version.suffix) return suffix_.GetNoArena(); } -inline void Version::set_suffix(const ::std::string& value) { +inline void Version::set_suffix(const std::string& value) { _has_bits_[0] |= 0x00000001u; - suffix_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + suffix_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.compiler.Version.suffix) } -#if LANG_CXX11 -inline void Version::set_suffix(::std::string&& value) { +inline void Version::set_suffix(std::string&& value) { _has_bits_[0] |= 0x00000001u; suffix_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.compiler.Version.suffix) } -#endif inline void Version::set_suffix(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - suffix_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + suffix_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.Version.suffix) } inline void Version::set_suffix(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - suffix_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + suffix_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.Version.suffix) } -inline ::std::string* Version::mutable_suffix() { +inline std::string* Version::mutable_suffix() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.Version.suffix) - return suffix_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return suffix_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* Version::release_suffix() { +inline std::string* Version::release_suffix() { // @@protoc_insertion_point(field_release:google.protobuf.compiler.Version.suffix) if (!has_suffix()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return suffix_.ReleaseNonDefaultNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return suffix_.ReleaseNonDefaultNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void Version::set_allocated_suffix(::std::string* suffix) { - if (suffix != NULL) { +inline void Version::set_allocated_suffix(std::string* suffix) { + if (suffix != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - suffix_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), suffix); + suffix_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), suffix); // @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.Version.suffix) } @@ -845,26 +905,24 @@ inline int CodeGeneratorRequest::file_to_generate_size() const { inline void CodeGeneratorRequest::clear_file_to_generate() { file_to_generate_.Clear(); } -inline const ::std::string& CodeGeneratorRequest::file_to_generate(int index) const { +inline const std::string& CodeGeneratorRequest::file_to_generate(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) return file_to_generate_.Get(index); } -inline ::std::string* CodeGeneratorRequest::mutable_file_to_generate(int index) { +inline std::string* CodeGeneratorRequest::mutable_file_to_generate(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) return file_to_generate_.Mutable(index); } -inline void CodeGeneratorRequest::set_file_to_generate(int index, const ::std::string& value) { +inline void CodeGeneratorRequest::set_file_to_generate(int index, const std::string& value) { // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) file_to_generate_.Mutable(index)->assign(value); } -#if LANG_CXX11 -inline void CodeGeneratorRequest::set_file_to_generate(int index, ::std::string&& value) { +inline void CodeGeneratorRequest::set_file_to_generate(int index, std::string&& value) { // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) file_to_generate_.Mutable(index)->assign(std::move(value)); } -#endif inline void CodeGeneratorRequest::set_file_to_generate(int index, const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); file_to_generate_.Mutable(index)->assign(value); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) } @@ -873,22 +931,20 @@ inline void CodeGeneratorRequest::set_file_to_generate(int index, const char* va reinterpret_cast(value), size); // @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) } -inline ::std::string* CodeGeneratorRequest::add_file_to_generate() { +inline std::string* CodeGeneratorRequest::add_file_to_generate() { // @@protoc_insertion_point(field_add_mutable:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) return file_to_generate_.Add(); } -inline void CodeGeneratorRequest::add_file_to_generate(const ::std::string& value) { +inline void CodeGeneratorRequest::add_file_to_generate(const std::string& value) { file_to_generate_.Add()->assign(value); // @@protoc_insertion_point(field_add:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) } -#if LANG_CXX11 -inline void CodeGeneratorRequest::add_file_to_generate(::std::string&& value) { +inline void CodeGeneratorRequest::add_file_to_generate(std::string&& value) { file_to_generate_.Add(std::move(value)); // @@protoc_insertion_point(field_add:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) } -#endif inline void CodeGeneratorRequest::add_file_to_generate(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); file_to_generate_.Add()->assign(value); // @@protoc_insertion_point(field_add_char:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) } @@ -896,12 +952,12 @@ inline void CodeGeneratorRequest::add_file_to_generate(const char* value, size_t file_to_generate_.Add()->assign(reinterpret_cast(value), size); // @@protoc_insertion_point(field_add_pointer:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) } -inline const ::google::protobuf::RepeatedPtrField<::std::string>& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& CodeGeneratorRequest::file_to_generate() const { // @@protoc_insertion_point(field_list:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) return file_to_generate_; } -inline ::google::protobuf::RepeatedPtrField<::std::string>* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* CodeGeneratorRequest::mutable_file_to_generate() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) return &file_to_generate_; @@ -912,58 +968,56 @@ inline bool CodeGeneratorRequest::has_parameter() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void CodeGeneratorRequest::clear_parameter() { - parameter_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + parameter_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& CodeGeneratorRequest::parameter() const { +inline const std::string& CodeGeneratorRequest::parameter() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorRequest.parameter) return parameter_.GetNoArena(); } -inline void CodeGeneratorRequest::set_parameter(const ::std::string& value) { +inline void CodeGeneratorRequest::set_parameter(const std::string& value) { _has_bits_[0] |= 0x00000001u; - parameter_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + parameter_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorRequest.parameter) } -#if LANG_CXX11 -inline void CodeGeneratorRequest::set_parameter(::std::string&& value) { +inline void CodeGeneratorRequest::set_parameter(std::string&& value) { _has_bits_[0] |= 0x00000001u; parameter_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.compiler.CodeGeneratorRequest.parameter) } -#endif inline void CodeGeneratorRequest::set_parameter(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - parameter_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + parameter_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.CodeGeneratorRequest.parameter) } inline void CodeGeneratorRequest::set_parameter(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - parameter_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + parameter_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorRequest.parameter) } -inline ::std::string* CodeGeneratorRequest::mutable_parameter() { +inline std::string* CodeGeneratorRequest::mutable_parameter() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorRequest.parameter) - return parameter_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return parameter_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* CodeGeneratorRequest::release_parameter() { +inline std::string* CodeGeneratorRequest::release_parameter() { // @@protoc_insertion_point(field_release:google.protobuf.compiler.CodeGeneratorRequest.parameter) if (!has_parameter()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return parameter_.ReleaseNonDefaultNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return parameter_.ReleaseNonDefaultNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void CodeGeneratorRequest::set_allocated_parameter(::std::string* parameter) { - if (parameter != NULL) { +inline void CodeGeneratorRequest::set_allocated_parameter(std::string* parameter) { + if (parameter != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - parameter_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), parameter); + parameter_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), parameter); // @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorRequest.parameter) } @@ -971,24 +1025,24 @@ inline void CodeGeneratorRequest::set_allocated_parameter(::std::string* paramet inline int CodeGeneratorRequest::proto_file_size() const { return proto_file_.size(); } -inline ::google::protobuf::FileDescriptorProto* CodeGeneratorRequest::mutable_proto_file(int index) { +inline PROTOBUF_NAMESPACE_ID::FileDescriptorProto* CodeGeneratorRequest::mutable_proto_file(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorRequest.proto_file) return proto_file_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >* CodeGeneratorRequest::mutable_proto_file() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.compiler.CodeGeneratorRequest.proto_file) return &proto_file_; } -inline const ::google::protobuf::FileDescriptorProto& CodeGeneratorRequest::proto_file(int index) const { +inline const PROTOBUF_NAMESPACE_ID::FileDescriptorProto& CodeGeneratorRequest::proto_file(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorRequest.proto_file) return proto_file_.Get(index); } -inline ::google::protobuf::FileDescriptorProto* CodeGeneratorRequest::add_proto_file() { +inline PROTOBUF_NAMESPACE_ID::FileDescriptorProto* CodeGeneratorRequest::add_proto_file() { // @@protoc_insertion_point(field_add:google.protobuf.compiler.CodeGeneratorRequest.proto_file) return proto_file_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >& CodeGeneratorRequest::proto_file() const { // @@protoc_insertion_point(field_list:google.protobuf.compiler.CodeGeneratorRequest.proto_file) return proto_file_; @@ -999,40 +1053,40 @@ inline bool CodeGeneratorRequest::has_compiler_version() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void CodeGeneratorRequest::clear_compiler_version() { - if (compiler_version_ != NULL) compiler_version_->Clear(); + if (compiler_version_ != nullptr) compiler_version_->Clear(); _has_bits_[0] &= ~0x00000002u; } -inline const ::google::protobuf::compiler::Version& CodeGeneratorRequest::compiler_version() const { - const ::google::protobuf::compiler::Version* p = compiler_version_; +inline const PROTOBUF_NAMESPACE_ID::compiler::Version& CodeGeneratorRequest::compiler_version() const { + const PROTOBUF_NAMESPACE_ID::compiler::Version* p = compiler_version_; // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorRequest.compiler_version) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::compiler::_Version_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::compiler::_Version_default_instance_); } -inline ::google::protobuf::compiler::Version* CodeGeneratorRequest::release_compiler_version() { +inline PROTOBUF_NAMESPACE_ID::compiler::Version* CodeGeneratorRequest::release_compiler_version() { // @@protoc_insertion_point(field_release:google.protobuf.compiler.CodeGeneratorRequest.compiler_version) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::compiler::Version* temp = compiler_version_; - compiler_version_ = NULL; + PROTOBUF_NAMESPACE_ID::compiler::Version* temp = compiler_version_; + compiler_version_ = nullptr; return temp; } -inline ::google::protobuf::compiler::Version* CodeGeneratorRequest::mutable_compiler_version() { +inline PROTOBUF_NAMESPACE_ID::compiler::Version* CodeGeneratorRequest::mutable_compiler_version() { _has_bits_[0] |= 0x00000002u; - if (compiler_version_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::compiler::Version>(GetArenaNoVirtual()); + if (compiler_version_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); compiler_version_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorRequest.compiler_version) return compiler_version_; } -inline void CodeGeneratorRequest::set_allocated_compiler_version(::google::protobuf::compiler::Version* compiler_version) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void CodeGeneratorRequest::set_allocated_compiler_version(PROTOBUF_NAMESPACE_ID::compiler::Version* compiler_version) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete compiler_version_; } if (compiler_version) { - ::google::protobuf::Arena* submessage_arena = NULL; + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = nullptr; if (message_arena != submessage_arena) { - compiler_version = ::google::protobuf::internal::GetOwnedMessage( + compiler_version = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, compiler_version, submessage_arena); } _has_bits_[0] |= 0x00000002u; @@ -1052,58 +1106,56 @@ inline bool CodeGeneratorResponse_File::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void CodeGeneratorResponse_File::clear_name() { - name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& CodeGeneratorResponse_File::name() const { +inline const std::string& CodeGeneratorResponse_File::name() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.File.name) return name_.GetNoArena(); } -inline void CodeGeneratorResponse_File::set_name(const ::std::string& value) { +inline void CodeGeneratorResponse_File::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorResponse.File.name) } -#if LANG_CXX11 -inline void CodeGeneratorResponse_File::set_name(::std::string&& value) { +inline void CodeGeneratorResponse_File::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.compiler.CodeGeneratorResponse.File.name) } -#endif inline void CodeGeneratorResponse_File::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.CodeGeneratorResponse.File.name) } inline void CodeGeneratorResponse_File::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorResponse.File.name) } -inline ::std::string* CodeGeneratorResponse_File::mutable_name() { +inline std::string* CodeGeneratorResponse_File::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.File.name) - return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* CodeGeneratorResponse_File::release_name() { +inline std::string* CodeGeneratorResponse_File::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.compiler.CodeGeneratorResponse.File.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefaultNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return name_.ReleaseNonDefaultNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void CodeGeneratorResponse_File::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void CodeGeneratorResponse_File::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name); + name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name); // @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorResponse.File.name) } @@ -1112,58 +1164,56 @@ inline bool CodeGeneratorResponse_File::has_insertion_point() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void CodeGeneratorResponse_File::clear_insertion_point() { - insertion_point_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + insertion_point_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _has_bits_[0] &= ~0x00000002u; } -inline const ::std::string& CodeGeneratorResponse_File::insertion_point() const { +inline const std::string& CodeGeneratorResponse_File::insertion_point() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point) return insertion_point_.GetNoArena(); } -inline void CodeGeneratorResponse_File::set_insertion_point(const ::std::string& value) { +inline void CodeGeneratorResponse_File::set_insertion_point(const std::string& value) { _has_bits_[0] |= 0x00000002u; - insertion_point_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + insertion_point_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point) } -#if LANG_CXX11 -inline void CodeGeneratorResponse_File::set_insertion_point(::std::string&& value) { +inline void CodeGeneratorResponse_File::set_insertion_point(std::string&& value) { _has_bits_[0] |= 0x00000002u; insertion_point_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point) } -#endif inline void CodeGeneratorResponse_File::set_insertion_point(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000002u; - insertion_point_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + insertion_point_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point) } inline void CodeGeneratorResponse_File::set_insertion_point(const char* value, size_t size) { _has_bits_[0] |= 0x00000002u; - insertion_point_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + insertion_point_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point) } -inline ::std::string* CodeGeneratorResponse_File::mutable_insertion_point() { +inline std::string* CodeGeneratorResponse_File::mutable_insertion_point() { _has_bits_[0] |= 0x00000002u; // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point) - return insertion_point_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return insertion_point_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* CodeGeneratorResponse_File::release_insertion_point() { +inline std::string* CodeGeneratorResponse_File::release_insertion_point() { // @@protoc_insertion_point(field_release:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point) if (!has_insertion_point()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000002u; - return insertion_point_.ReleaseNonDefaultNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return insertion_point_.ReleaseNonDefaultNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void CodeGeneratorResponse_File::set_allocated_insertion_point(::std::string* insertion_point) { - if (insertion_point != NULL) { +inline void CodeGeneratorResponse_File::set_allocated_insertion_point(std::string* insertion_point) { + if (insertion_point != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - insertion_point_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), insertion_point); + insertion_point_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), insertion_point); // @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point) } @@ -1172,58 +1222,56 @@ inline bool CodeGeneratorResponse_File::has_content() const { return (_has_bits_[0] & 0x00000004u) != 0; } inline void CodeGeneratorResponse_File::clear_content() { - content_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + content_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _has_bits_[0] &= ~0x00000004u; } -inline const ::std::string& CodeGeneratorResponse_File::content() const { +inline const std::string& CodeGeneratorResponse_File::content() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.File.content) return content_.GetNoArena(); } -inline void CodeGeneratorResponse_File::set_content(const ::std::string& value) { +inline void CodeGeneratorResponse_File::set_content(const std::string& value) { _has_bits_[0] |= 0x00000004u; - content_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + content_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorResponse.File.content) } -#if LANG_CXX11 -inline void CodeGeneratorResponse_File::set_content(::std::string&& value) { +inline void CodeGeneratorResponse_File::set_content(std::string&& value) { _has_bits_[0] |= 0x00000004u; content_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.compiler.CodeGeneratorResponse.File.content) } -#endif inline void CodeGeneratorResponse_File::set_content(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000004u; - content_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + content_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.CodeGeneratorResponse.File.content) } inline void CodeGeneratorResponse_File::set_content(const char* value, size_t size) { _has_bits_[0] |= 0x00000004u; - content_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + content_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorResponse.File.content) } -inline ::std::string* CodeGeneratorResponse_File::mutable_content() { +inline std::string* CodeGeneratorResponse_File::mutable_content() { _has_bits_[0] |= 0x00000004u; // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.File.content) - return content_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return content_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* CodeGeneratorResponse_File::release_content() { +inline std::string* CodeGeneratorResponse_File::release_content() { // @@protoc_insertion_point(field_release:google.protobuf.compiler.CodeGeneratorResponse.File.content) if (!has_content()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000004u; - return content_.ReleaseNonDefaultNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return content_.ReleaseNonDefaultNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void CodeGeneratorResponse_File::set_allocated_content(::std::string* content) { - if (content != NULL) { +inline void CodeGeneratorResponse_File::set_allocated_content(std::string* content) { + if (content != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - content_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), content); + content_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), content); // @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorResponse.File.content) } @@ -1236,58 +1284,56 @@ inline bool CodeGeneratorResponse::has_error() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void CodeGeneratorResponse::clear_error() { - error_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + error_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& CodeGeneratorResponse::error() const { +inline const std::string& CodeGeneratorResponse::error() const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.error) return error_.GetNoArena(); } -inline void CodeGeneratorResponse::set_error(const ::std::string& value) { +inline void CodeGeneratorResponse::set_error(const std::string& value) { _has_bits_[0] |= 0x00000001u; - error_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + error_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorResponse.error) } -#if LANG_CXX11 -inline void CodeGeneratorResponse::set_error(::std::string&& value) { +inline void CodeGeneratorResponse::set_error(std::string&& value) { _has_bits_[0] |= 0x00000001u; error_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.compiler.CodeGeneratorResponse.error) } -#endif inline void CodeGeneratorResponse::set_error(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - error_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + error_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.CodeGeneratorResponse.error) } inline void CodeGeneratorResponse::set_error(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - error_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + error_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorResponse.error) } -inline ::std::string* CodeGeneratorResponse::mutable_error() { +inline std::string* CodeGeneratorResponse::mutable_error() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.error) - return error_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return error_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* CodeGeneratorResponse::release_error() { +inline std::string* CodeGeneratorResponse::release_error() { // @@protoc_insertion_point(field_release:google.protobuf.compiler.CodeGeneratorResponse.error) if (!has_error()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return error_.ReleaseNonDefaultNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return error_.ReleaseNonDefaultNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void CodeGeneratorResponse::set_allocated_error(::std::string* error) { - if (error != NULL) { +inline void CodeGeneratorResponse::set_allocated_error(std::string* error) { + if (error != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - error_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), error); + error_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), error); // @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorResponse.error) } @@ -1298,24 +1344,24 @@ inline int CodeGeneratorResponse::file_size() const { inline void CodeGeneratorResponse::clear_file() { file_.Clear(); } -inline ::google::protobuf::compiler::CodeGeneratorResponse_File* CodeGeneratorResponse::mutable_file(int index) { +inline PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File* CodeGeneratorResponse::mutable_file(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.file) return file_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::compiler::CodeGeneratorResponse_File >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File >* CodeGeneratorResponse::mutable_file() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.compiler.CodeGeneratorResponse.file) return &file_; } -inline const ::google::protobuf::compiler::CodeGeneratorResponse_File& CodeGeneratorResponse::file(int index) const { +inline const PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File& CodeGeneratorResponse::file(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.file) return file_.Get(index); } -inline ::google::protobuf::compiler::CodeGeneratorResponse_File* CodeGeneratorResponse::add_file() { +inline PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File* CodeGeneratorResponse::add_file() { // @@protoc_insertion_point(field_add:google.protobuf.compiler.CodeGeneratorResponse.file) return file_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::compiler::CodeGeneratorResponse_File >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::compiler::CodeGeneratorResponse_File >& CodeGeneratorResponse::file() const { // @@protoc_insertion_point(field_list:google.protobuf.compiler.CodeGeneratorResponse.file) return file_; @@ -1334,10 +1380,9 @@ CodeGeneratorResponse::file() const { // @@protoc_insertion_point(namespace_scope) } // namespace compiler -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2fcompiler_2fplugin_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fcompiler_2fplugin_2eproto diff --git a/src/google/protobuf/compiler/plugin.proto b/src/google/protobuf/compiler/plugin.proto index 5b5574529e..665e5a725d 100644 --- a/src/google/protobuf/compiler/plugin.proto +++ b/src/google/protobuf/compiler/plugin.proto @@ -45,6 +45,7 @@ // flag "--${NAME}_out" is passed to protoc. syntax = "proto2"; + package google.protobuf.compiler; option java_package = "com.google.protobuf.compiler"; option java_outer_classname = "PluginProtos"; diff --git a/src/google/protobuf/compiler/python/python_generator.cc b/src/google/protobuf/compiler/python/python_generator.cc index 6dc61ec124..ec0704a18c 100644 --- a/src/google/protobuf/compiler/python/python_generator.cc +++ b/src/google/protobuf/compiler/python/python_generator.cc @@ -65,6 +65,7 @@ #include + namespace google { namespace protobuf { namespace compiler { @@ -79,28 +80,26 @@ bool StrEndsWith(StringPiece sp, StringPiece x) { // Returns a copy of |filename| with any trailing ".protodevel" or ".proto // suffix stripped. // TODO(robinson): Unify with copy in compiler/cpp/internal/helpers.cc. -string StripProto(const string& filename) { +std::string StripProto(const std::string& filename) { const char* suffix = StrEndsWith(filename, ".protodevel") ? ".protodevel" : ".proto"; return StripSuffixString(filename, suffix); } - // Returns the Python module name expected for a given .proto filename. -string ModuleName(const string& filename) { - string basename = StripProto(filename); +std::string ModuleName(const std::string& filename) { + std::string basename = StripProto(filename); ReplaceCharacters(&basename, "-", '_'); ReplaceCharacters(&basename, "/", '.'); return basename + "_pb2"; } - // Returns the alias we assign to the module of the given .proto filename // when importing. See testPackageInitializationImport in // net/proto2/python/internal/reflection_test.py // to see why we need the alias. -string ModuleAlias(const string& filename) { - string module_name = ModuleName(filename); +std::string ModuleAlias(const std::string& filename) { + std::string module_name = ModuleName(filename); // We can't have dots in the module name, so we replace each with _dot_. // But that could lead to a collision between a.b and a_dot_b, so we also // duplicate each underscore. @@ -115,13 +114,13 @@ const char* const kKeywords[] = { "class", "continue", "def", "del", "elif", "else", "except", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", - "return", "try", "while", "with", "yield", + "return", "try", "while", "with", "yield", "print", }; const char* const* kKeywordsEnd = kKeywords + (sizeof(kKeywords) / sizeof(kKeywords[0])); -bool ContainsPythonKeyword(const string& module_name) { - std::vector tokens = Split(module_name, "."); +bool ContainsPythonKeyword(const std::string& module_name) { + std::vector tokens = Split(module_name, "."); for (int i = 0; i < tokens.size(); ++i) { if (std::find(kKeywords, kKeywordsEnd, tokens[i]) != kKeywordsEnd) { return true; @@ -130,51 +129,66 @@ bool ContainsPythonKeyword(const string& module_name) { return false; } +inline bool IsPythonKeyword(const string& name) { + return (std::find(kKeywords, kKeywordsEnd, name) != kKeywordsEnd); +} + +std::string ResolveKeyword(const std::string& name) { + if (IsPythonKeyword(name)) { + return "globals()['" + name + "']"; + } + return name; +} // Returns the name of all containing types for descriptor, // in order from outermost to innermost, followed by descriptor's // own name. Each name is separated by |separator|. template -string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, - const string& separator) { - string name = descriptor.name(); - for (const Descriptor* current = descriptor.containing_type(); - current != NULL; current = current->containing_type()) { - name = current->name() + separator + name; +std::string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, + const std::string& separator) { + std::string name = descriptor.name(); + const Descriptor* parent = descriptor.containing_type(); + if (parent != nullptr) { + std::string prefix = NamePrefixedWithNestedTypes(*parent, separator); + if (separator == "." && IsPythonKeyword(name)) { + return "getattr(" + prefix + ", '" + name + "')"; + } else { + return prefix + separator + name; + } + } + if (separator == ".") { + name = ResolveKeyword(name); } return name; } - // Name of the class attribute where we store the Python // descriptor.Descriptor instance for the generated class. // Must stay consistent with the _DESCRIPTOR_KEY constant // in proto2/public/reflection.py. const char kDescriptorKey[] = "DESCRIPTOR"; - // Does the file have top-level enums? -inline bool HasTopLevelEnums(const FileDescriptor *file) { +inline bool HasTopLevelEnums(const FileDescriptor* file) { return file->enum_type_count() > 0; } - // Should we generate generic services for this file? -inline bool HasGenericServices(const FileDescriptor *file) { - return file->service_count() > 0 && - file->options().py_generic_services(); +inline bool HasGenericServices(const FileDescriptor* file) { + return file->service_count() > 0 && file->options().py_generic_services(); } - // Prints the common boilerplate needed at the top of every .py // file output by this generator. -void PrintTopBoilerplate( - io::Printer* printer, const FileDescriptor* file, bool descriptor_proto) { +void PrintTopBoilerplate(io::Printer* printer, const FileDescriptor* file, + bool descriptor_proto) { // TODO(robinson): Allow parameterization of Python version? printer->Print( + "# -*- coding: utf-8 -*-\n" "# Generated by the protocol buffer compiler. DO NOT EDIT!\n" "# source: $filename$\n" - "\nimport sys\n_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))" //##PY25 + "\nimport sys\n_b=sys.version_info[0]<3 and (lambda x:x) or (lambda " + "x:x.encode('latin1'))" //##PY25 "\n", "filename", file->name()); if (HasTopLevelEnums(file)) { @@ -199,7 +213,6 @@ void PrintTopBoilerplate( printer->Print("\n\n"); } - // Returns a Python literal giving the default value for a field. // If the field specifies no explicit default value, we'll return // the default default value for the field type (zero for numbers, @@ -210,20 +223,20 @@ void PrintTopBoilerplate( // //compiler/cpp/internal/primitive_field.cc // //compiler/cpp/internal/enum_field.cc // //compiler/cpp/internal/string_field.cc -string StringifyDefaultValue(const FieldDescriptor& field) { +std::string StringifyDefaultValue(const FieldDescriptor& field) { if (field.is_repeated()) { return "[]"; } switch (field.cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: - return SimpleItoa(field.default_value_int32()); + return StrCat(field.default_value_int32()); case FieldDescriptor::CPPTYPE_UINT32: - return SimpleItoa(field.default_value_uint32()); + return StrCat(field.default_value_uint32()); case FieldDescriptor::CPPTYPE_INT64: - return SimpleItoa(field.default_value_int64()); + return StrCat(field.default_value_int64()); case FieldDescriptor::CPPTYPE_UINT64: - return SimpleItoa(field.default_value_uint64()); + return StrCat(field.default_value_uint64()); case FieldDescriptor::CPPTYPE_DOUBLE: { double value = field.default_value_double(); if (value == std::numeric_limits::infinity()) { @@ -259,14 +272,16 @@ string StringifyDefaultValue(const FieldDescriptor& field) { case FieldDescriptor::CPPTYPE_BOOL: return field.default_value_bool() ? "True" : "False"; case FieldDescriptor::CPPTYPE_ENUM: - return SimpleItoa(field.default_value_enum()->number()); + return StrCat(field.default_value_enum()->number()); case FieldDescriptor::CPPTYPE_STRING: -//##!PY25 return "b\"" + CEscape(field.default_value_string()) + -//##!PY25 (field.type() != FieldDescriptor::TYPE_STRING ? "\"" : -//##!PY25 "\".decode('utf-8')"); + //##!PY25 return "b\"" + CEscape(field.default_value_string()) + //+ + //##!PY25 (field.type() != FieldDescriptor::TYPE_STRING ? "\"" + //: + //##!PY25 "\".decode('utf-8')"); return "_b(\"" + CEscape(field.default_value_string()) + //##PY25 (field.type() != FieldDescriptor::TYPE_STRING ? "\")" : //##PY25 - "\").decode('utf-8')"); //##PY25 + "\").decode('utf-8')"); //##PY25 case FieldDescriptor::CPPTYPE_MESSAGE: return "None"; } @@ -276,7 +291,7 @@ string StringifyDefaultValue(const FieldDescriptor& field) { return ""; } -string StringifySyntax(FileDescriptor::Syntax syntax) { +std::string StringifySyntax(FileDescriptor::Syntax syntax) { switch (syntax) { case FileDescriptor::SYNTAX_PROTO2: return "proto2"; @@ -290,20 +305,15 @@ string StringifySyntax(FileDescriptor::Syntax syntax) { } } - } // namespace +Generator::Generator() : file_(NULL) {} -Generator::Generator() : file_(NULL) { -} - -Generator::~Generator() { -} +Generator::~Generator() {} bool Generator::Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const { + const std::string& parameter, + GeneratorContext* context, std::string* error) const { // Completely serialize all Generate() calls on this instance. The // thread-safety constraints of the CodeGenerator interface aren't clear so @@ -314,8 +324,8 @@ bool Generator::Generate(const FileDescriptor* file, // to have any mutable members. Then it is implicitly thread-safe. MutexLock lock(&mutex_); file_ = file; - string module_name = ModuleName(file->name()); - string filename = module_name; + std::string module_name = ModuleName(file->name()); + std::string filename = module_name; ReplaceCharacters(&filename, ".", '/'); filename += ".py"; @@ -353,19 +363,19 @@ bool Generator::Generate(const FileDescriptor* file, PrintServices(); } - printer.Print( - "# @@protoc_insertion_point(module_scope)\n"); + printer.Print("# @@protoc_insertion_point(module_scope)\n"); return !printer.failed(); } + // Prints Python imports for all modules imported by |file|. void Generator::PrintImports() const { for (int i = 0; i < file_->dependency_count(); ++i) { - const string& filename = file_->dependency(i)->name(); + const std::string& filename = file_->dependency(i)->name(); - string module_name = ModuleName(filename); - string module_alias = ModuleAlias(filename); + std::string module_name = ModuleName(filename); + std::string module_alias = ModuleAlias(filename); if (ContainsPythonKeyword(module_name)) { // If the module path contains a Python keyword, we have to quote the // module name and import it using importlib. Otherwise the usual kind of @@ -376,8 +386,8 @@ void Generator::PrintImports() const { module_alias, "name", module_name); } else { int last_dot_pos = module_name.rfind('.'); - string import_statement; - if (last_dot_pos == string::npos) { + std::string import_statement; + if (last_dot_pos == std::string::npos) { // NOTE(petya): this is not tested as it would require a protocol buffer // outside of any package, and I don't think that is easily achievable. import_statement = "import " + module_name; @@ -395,7 +405,7 @@ void Generator::PrintImports() const { // Print public imports. for (int i = 0; i < file_->public_dependency_count(); ++i) { - string module_name = ModuleName(file_->public_dependency(i)->name()); + std::string module_name = ModuleName(file_->public_dependency(i)->name()); printer_->Print("from $module$ import *\n", "module", module_name); } printer_->Print("\n"); @@ -403,7 +413,7 @@ void Generator::PrintImports() const { // Prints the single file descriptor for this file. void Generator::PrintFileDescriptor() const { - std::map m; + std::map m; m["descriptor_name"] = kDescriptorKey; m["name"] = file_->name(); m["package"] = file_->package(); @@ -424,7 +434,7 @@ void Generator::PrintFileDescriptor() const { if (file_->dependency_count() != 0) { printer_->Print(",\ndependencies=["); for (int i = 0; i < file_->dependency_count(); ++i) { - string module_alias = ModuleAlias(file_->dependency(i)->name()); + std::string module_alias = ModuleAlias(file_->dependency(i)->name()); printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias", module_alias); } @@ -433,7 +443,8 @@ void Generator::PrintFileDescriptor() const { if (file_->public_dependency_count() > 0) { printer_->Print(",\npublic_dependencies=["); for (int i = 0; i < file_->public_dependency_count(); ++i) { - string module_alias = ModuleAlias(file_->public_dependency(i)->name()); + std::string module_alias = + ModuleAlias(file_->public_dependency(i)->name()); printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias", module_alias); } @@ -451,15 +462,15 @@ void Generator::PrintFileDescriptor() const { // Prints descriptors and module-level constants for all top-level // enums defined in |file|. void Generator::PrintTopLevelEnums() const { - std::vector > top_level_enum_values; + std::vector > top_level_enum_values; for (int i = 0; i < file_->enum_type_count(); ++i) { const EnumDescriptor& enum_descriptor = *file_->enum_type(i); PrintEnum(enum_descriptor); - printer_->Print("$name$ = " - "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)", - "name", enum_descriptor.name(), - "descriptor_name", - ModuleLevelDescriptorName(enum_descriptor)); + printer_->Print( + "$name$ = " + "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)", + "name", ResolveKeyword(enum_descriptor.name()), "descriptor_name", + ModuleLevelDescriptorName(enum_descriptor)); printer_->Print("\n"); for (int j = 0; j < enum_descriptor.value_count(); ++j) { @@ -471,8 +482,8 @@ void Generator::PrintTopLevelEnums() const { for (int i = 0; i < top_level_enum_values.size(); ++i) { printer_->Print("$name$ = $value$\n", "name", - top_level_enum_values[i].first, "value", - SimpleItoa(top_level_enum_values[i].second)); + ResolveKeyword(top_level_enum_values[i].first), "value", + StrCat(top_level_enum_values[i].second)); } printer_->Print("\n"); } @@ -488,8 +499,8 @@ void Generator::PrintAllNestedEnumsInFile() const { // enum name to a Python EnumDescriptor object equivalent to // enum_descriptor. void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const { - std::map m; - string module_level_descriptor_name = + std::map m; + std::string module_level_descriptor_name = ModuleLevelDescriptorName(enum_descriptor); m["descriptor_name"] = module_level_descriptor_name; m["name"] = enum_descriptor.name(); @@ -502,7 +513,7 @@ void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const { " filename=None,\n" " file=$file$,\n" " values=[\n"; - string options_string; + std::string options_string; enum_descriptor.options().SerializeToString(&options_string); printer_->Print(m, enum_descriptor_template); printer_->Indent(); @@ -514,8 +525,7 @@ void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const { printer_->Outdent(); printer_->Print("],\n"); printer_->Print("containing_type=None,\n"); - printer_->Print("serialized_options=$options_value$,\n", - "options_value", + printer_->Print("serialized_options=$options_value$,\n", "options_value", OptionsValue(options_string)); EnumDescriptorProto edp; PrintSerializedPbInterval(enum_descriptor, edp); @@ -542,12 +552,13 @@ void Generator::PrintTopLevelExtensions() const { const bool is_extension = true; for (int i = 0; i < file_->extension_count(); ++i) { const FieldDescriptor& extension_field = *file_->extension(i); - string constant_name = extension_field.name() + "_FIELD_NUMBER"; + std::string constant_name = extension_field.name() + "_FIELD_NUMBER"; UpperString(&constant_name); printer_->Print("$constant_name$ = $number$\n", "constant_name", constant_name, "number", - SimpleItoa(extension_field.number())); - printer_->Print("$name$ = ", "name", extension_field.name()); + StrCat(extension_field.number())); + printer_->Print("$name$ = ", "name", + ResolveKeyword(extension_field.name())); PrintFieldDescriptor(extension_field, is_extension); printer_->Print("\n"); } @@ -581,19 +592,18 @@ void Generator::PrintServices() const { void Generator::PrintServiceDescriptor( const ServiceDescriptor& descriptor) const { printer_->Print("\n"); - string service_name = ModuleLevelServiceDescriptorName(descriptor); - string options_string; + std::string service_name = ModuleLevelServiceDescriptorName(descriptor); + std::string options_string; descriptor.options().SerializeToString(&options_string); - printer_->Print( - "$service_name$ = _descriptor.ServiceDescriptor(\n", - "service_name", service_name); + printer_->Print("$service_name$ = _descriptor.ServiceDescriptor(\n", + "service_name", service_name); printer_->Indent(); - std::map m; + std::map m; m["name"] = descriptor.name(); m["full_name"] = descriptor.full_name(); m["file"] = kDescriptorKey; - m["index"] = SimpleItoa(descriptor.index()); + m["index"] = StrCat(descriptor.index()); m["options_value"] = OptionsValue(options_string); const char required_function_arguments[] = "name='$name$',\n" @@ -614,22 +624,21 @@ void Generator::PrintServiceDescriptor( m.clear(); m["name"] = method->name(); m["full_name"] = method->full_name(); - m["index"] = SimpleItoa(method->index()); + m["index"] = StrCat(method->index()); m["serialized_options"] = CEscape(options_string); m["input_type"] = ModuleLevelDescriptorName(*(method->input_type())); m["output_type"] = ModuleLevelDescriptorName(*(method->output_type())); m["options_value"] = OptionsValue(options_string); printer_->Print("_descriptor.MethodDescriptor(\n"); printer_->Indent(); - printer_->Print( - m, - "name='$name$',\n" - "full_name='$full_name$',\n" - "index=$index$,\n" - "containing_service=None,\n" - "input_type=$input_type$,\n" - "output_type=$output_type$,\n" - "serialized_options=$options_value$,\n"); + printer_->Print(m, + "name='$name$',\n" + "full_name='$full_name$',\n" + "index=$index$,\n" + "containing_service=None,\n" + "input_type=$input_type$,\n" + "output_type=$output_type$,\n" + "serialized_options=$options_value$,\n"); printer_->Outdent(); printer_->Print("),\n"); } @@ -641,23 +650,21 @@ void Generator::PrintServiceDescriptor( printer_->Print("\n"); } - void Generator::PrintDescriptorKeyAndModuleName( const ServiceDescriptor& descriptor) const { - printer_->Print( - "$descriptor_key$ = $descriptor_name$,\n", - "descriptor_key", kDescriptorKey, - "descriptor_name", ModuleLevelServiceDescriptorName(descriptor)); - printer_->Print( - "__module__ = '$module_name$'\n", - "module_name", ModuleName(file_->name())); + printer_->Print("$descriptor_key$ = $descriptor_name$,\n", "descriptor_key", + kDescriptorKey, "descriptor_name", + ModuleLevelServiceDescriptorName(descriptor)); + std::string module_name = ModuleName(file_->name()); + printer_->Print("__module__ = '$module_name$'\n", "module_name", module_name); } void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { // Print the service. - printer_->Print("$class_name$ = service_reflection.GeneratedServiceType(" - "'$class_name$', (_service.Service,), dict(\n", - "class_name", descriptor.name()); + printer_->Print( + "$class_name$ = service_reflection.GeneratedServiceType(" + "'$class_name$', (_service.Service,), dict(\n", + "class_name", descriptor.name()); printer_->Indent(); Generator::PrintDescriptorKeyAndModuleName(descriptor); printer_->Print("))\n\n"); @@ -666,10 +673,11 @@ void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const { // Print the service stub. - printer_->Print("$class_name$_Stub = " - "service_reflection.GeneratedServiceStubType(" - "'$class_name$_Stub', ($class_name$,), dict(\n", - "class_name", descriptor.name()); + printer_->Print( + "$class_name$_Stub = " + "service_reflection.GeneratedServiceStubType(" + "'$class_name$_Stub', ($class_name$,), dict(\n", + "class_name", descriptor.name()); printer_->Indent(); Generator::PrintDescriptorKeyAndModuleName(descriptor); printer_->Print("))\n\n"); @@ -688,7 +696,7 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { "descriptor_name", ModuleLevelDescriptorName(message_descriptor)); printer_->Indent(); - std::map m; + std::map m; m["name"] = message_descriptor.name(); m["full_name"] = message_descriptor.full_name(); m["file"] = kDescriptorKey; @@ -705,8 +713,8 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { // Nested types printer_->Print("nested_types=["); for (int i = 0; i < message_descriptor.nested_type_count(); ++i) { - const string nested_name = ModuleLevelDescriptorName( - *message_descriptor.nested_type(i)); + const std::string nested_name = + ModuleLevelDescriptorName(*message_descriptor.nested_type(i)); printer_->Print("$name$, ", "name", nested_name); } printer_->Print("],\n"); @@ -715,22 +723,21 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { printer_->Print("enum_types=[\n"); printer_->Indent(); for (int i = 0; i < message_descriptor.enum_type_count(); ++i) { - const string descriptor_name = ModuleLevelDescriptorName( - *message_descriptor.enum_type(i)); + const std::string descriptor_name = + ModuleLevelDescriptorName(*message_descriptor.enum_type(i)); printer_->Print(descriptor_name.c_str()); printer_->Print(",\n"); } printer_->Outdent(); printer_->Print("],\n"); - string options_string; + std::string options_string; message_descriptor.options().SerializeToString(&options_string); printer_->Print( "serialized_options=$options_value$,\n" "is_extendable=$extendable$,\n" "syntax='$syntax$'", - "options_value", OptionsValue(options_string), - "extendable", message_descriptor.extension_range_count() > 0 ? - "True" : "False", + "options_value", OptionsValue(options_string), "extendable", + message_descriptor.extension_range_count() > 0 ? "True" : "False", "syntax", StringifySyntax(message_descriptor.file()->syntax())); printer_->Print(",\n"); @@ -739,32 +746,30 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { for (int i = 0; i < message_descriptor.extension_range_count(); ++i) { const Descriptor::ExtensionRange* range = message_descriptor.extension_range(i); - printer_->Print("($start$, $end$), ", "start", - SimpleItoa(range->start), "end", - SimpleItoa(range->end)); + printer_->Print("($start$, $end$), ", "start", StrCat(range->start), + "end", StrCat(range->end)); } printer_->Print("],\n"); printer_->Print("oneofs=[\n"); printer_->Indent(); for (int i = 0; i < message_descriptor.oneof_decl_count(); ++i) { const OneofDescriptor* desc = message_descriptor.oneof_decl(i); - std::map m; + std::map m; m["name"] = desc->name(); m["full_name"] = desc->full_name(); - m["index"] = SimpleItoa(desc->index()); - string options_string = + m["index"] = StrCat(desc->index()); + std::string options_string = OptionsValue(desc->options().SerializeAsString()); if (options_string == "None") { m["serialized_options"] = ""; } else { m["serialized_options"] = ", serialized_options=" + options_string; } - printer_->Print( - m, - "_descriptor.OneofDescriptor(\n" - " name='$name$', full_name='$full_name$',\n" - " index=$index$, containing_type=None, " - "fields=[]$serialized_options$),\n"); + printer_->Print(m, + "_descriptor.OneofDescriptor(\n" + " name='$name$', full_name='$full_name$',\n" + " index=$index$, containing_type=None, " + "fields=[]$serialized_options$),\n"); } printer_->Outdent(); printer_->Print("],\n"); @@ -790,11 +795,11 @@ void Generator::PrintNestedDescriptors( // Prints all messages in |file|. void Generator::PrintMessages() const { for (int i = 0; i < file_->message_type_count(); ++i) { - std::vector to_register; - PrintMessage(*file_->message_type(i), "", &to_register); + std::vector to_register; + PrintMessage(*file_->message_type(i), "", &to_register, false); for (int j = 0; j < to_register.size(); ++j) { printer_->Print("_sym_db.RegisterMessage($name$)\n", "name", - to_register[j]); + ResolveKeyword(to_register[j])); } printer_->Print("\n"); } @@ -809,37 +814,55 @@ void Generator::PrintMessages() const { // Mutually recursive with PrintNestedMessages(). // Collect nested message names to_register for the symbol_database. void Generator::PrintMessage(const Descriptor& message_descriptor, - const string& prefix, - std::vector* to_register) const { - string qualified_name(prefix + message_descriptor.name()); - to_register->push_back(qualified_name); - printer_->Print( - "$name$ = _reflection.GeneratedProtocolMessageType('$name$', " - "(_message.Message,), dict(\n", - "name", message_descriptor.name()); + const std::string& prefix, + std::vector* to_register, + bool is_nested) const { + std::string qualified_name; + if (is_nested) { + if (IsPythonKeyword(message_descriptor.name())) { + qualified_name = + "getattr(" + prefix + ", '" + message_descriptor.name() + "')"; + } else { + qualified_name = prefix + "." + message_descriptor.name(); + } + printer_->Print( + "'$name$' : _reflection.GeneratedProtocolMessageType('$name$', " + "(_message.Message,), {\n", + "name", message_descriptor.name()); + } else { + qualified_name = ResolveKeyword(message_descriptor.name()); + printer_->Print( + "$qualified_name$ = _reflection.GeneratedProtocolMessageType('$name$', " + "(_message.Message,), {\n", + "qualified_name", qualified_name, "name", message_descriptor.name()); + } printer_->Indent(); - PrintNestedMessages(message_descriptor, qualified_name + ".", to_register); - std::map m; + to_register->push_back(qualified_name); + + PrintNestedMessages(message_descriptor, qualified_name, to_register); + std::map m; m["descriptor_key"] = kDescriptorKey; m["descriptor_name"] = ModuleLevelDescriptorName(message_descriptor); - printer_->Print(m, "$descriptor_key$ = $descriptor_name$,\n"); - printer_->Print("__module__ = '$module_name$'\n", - "module_name", ModuleName(file_->name())); + printer_->Print(m, "'$descriptor_key$' : $descriptor_name$,\n"); + std::string module_name = ModuleName(file_->name()); + printer_->Print("'__module__' : '$module_name$'\n", "module_name", + module_name); printer_->Print("# @@protoc_insertion_point(class_scope:$full_name$)\n", "full_name", message_descriptor.full_name()); - printer_->Print("))\n"); + printer_->Print("})\n"); printer_->Outdent(); } // Prints all nested messages within |containing_descriptor|. // Mutually recursive with PrintMessage(). -void Generator::PrintNestedMessages(const Descriptor& containing_descriptor, - const string& prefix, - std::vector* to_register) const { +void Generator::PrintNestedMessages( + const Descriptor& containing_descriptor, const std::string& prefix, + std::vector* to_register) const { for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { printer_->Print("\n"); - PrintMessage(*containing_descriptor.nested_type(i), prefix, to_register); + PrintMessage(*containing_descriptor.nested_type(i), prefix, to_register, + true); printer_->Print(",\n"); } } @@ -869,7 +892,7 @@ void Generator::FixForeignFieldsInDescriptor( FixContainingTypeInDescriptor(enum_descriptor, &descriptor); } for (int i = 0; i < descriptor.oneof_decl_count(); ++i) { - std::map m; + std::map m; const OneofDescriptor* oneof = descriptor.oneof_decl(i); m["descriptor_name"] = ModuleLevelDescriptorName(descriptor); m["oneof_name"] = oneof->name(); @@ -888,7 +911,7 @@ void Generator::FixForeignFieldsInDescriptor( } void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const { - std::map m; + std::map m; m["descriptor_name"] = kDescriptorKey; m["message_name"] = descriptor.name(); m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor); @@ -900,7 +923,7 @@ void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const { void Generator::AddServiceToFileDescriptor( const ServiceDescriptor& descriptor) const { - std::map m; + std::map m; m["descriptor_name"] = kDescriptorKey; m["service_name"] = descriptor.name(); m["service_descriptor_name"] = ModuleLevelServiceDescriptorName(descriptor); @@ -912,7 +935,7 @@ void Generator::AddServiceToFileDescriptor( void Generator::AddEnumToFileDescriptor( const EnumDescriptor& descriptor) const { - std::map m; + std::map m; m["descriptor_name"] = kDescriptorKey; m["enum_name"] = descriptor.name(); m["enum_descriptor_name"] = ModuleLevelDescriptorName(descriptor); @@ -924,12 +947,13 @@ void Generator::AddEnumToFileDescriptor( void Generator::AddExtensionToFileDescriptor( const FieldDescriptor& descriptor) const { - std::map m; + std::map m; m["descriptor_name"] = kDescriptorKey; m["field_name"] = descriptor.name(); + m["resolved_name"] = ResolveKeyword(descriptor.name()); const char file_descriptor_template[] = "$descriptor_name$.extensions_by_name['$field_name$'] = " - "$field_name$\n"; + "$resolved_name$\n"; printer_->Print(m, file_descriptor_template); } @@ -942,12 +966,12 @@ void Generator::AddExtensionToFileDescriptor( // look the field up in the containing type. (e.g., fields_by_name // or extensions_by_name). We ignore python_dict_name if containing_type // is NULL. -void Generator::FixForeignFieldsInField(const Descriptor* containing_type, - const FieldDescriptor& field, - const string& python_dict_name) const { - const string field_referencing_expression = FieldReferencingExpression( - containing_type, field, python_dict_name); - std::map m; +void Generator::FixForeignFieldsInField( + const Descriptor* containing_type, const FieldDescriptor& field, + const std::string& python_dict_name) const { + const std::string field_referencing_expression = + FieldReferencingExpression(containing_type, field, python_dict_name); + std::map m; m["field_ref"] = field_referencing_expression; const Descriptor* foreign_message_type = field.message_type(); if (foreign_message_type) { @@ -970,21 +994,19 @@ void Generator::FixForeignFieldsInField(const Descriptor* containing_type, // look the field up in the containing type. (e.g., fields_by_name // or extensions_by_name). We ignore python_dict_name if containing_type // is NULL. -string Generator::FieldReferencingExpression( - const Descriptor* containing_type, - const FieldDescriptor& field, - const string& python_dict_name) const { +std::string Generator::FieldReferencingExpression( + const Descriptor* containing_type, const FieldDescriptor& field, + const std::string& python_dict_name) const { // We should only ever be looking up fields in the current file. // The only things we refer to from other files are message descriptors. - GOOGLE_CHECK_EQ(field.file(), file_) << field.file()->name() << " vs. " - << file_->name(); + GOOGLE_CHECK_EQ(field.file(), file_) + << field.file()->name() << " vs. " << file_->name(); if (!containing_type) { - return field.name(); + return ResolveKeyword(field.name()); } - return strings::Substitute( - "$0.$1['$2']", - ModuleLevelDescriptorName(*containing_type), - python_dict_name, field.name()); + return strings::Substitute("$0.$1['$2']", + ModuleLevelDescriptorName(*containing_type), + python_dict_name, field.name()); } // Prints containing_type for nested descriptors or enum descriptors. @@ -993,13 +1015,11 @@ void Generator::FixContainingTypeInDescriptor( const DescriptorT& descriptor, const Descriptor* containing_descriptor) const { if (containing_descriptor != NULL) { - const string nested_name = ModuleLevelDescriptorName(descriptor); - const string parent_name = ModuleLevelDescriptorName( - *containing_descriptor); - printer_->Print( - "$nested_name$.containing_type = $parent_name$\n", - "nested_name", nested_name, - "parent_name", parent_name); + const std::string nested_name = ModuleLevelDescriptorName(descriptor); + const std::string parent_name = + ModuleLevelDescriptorName(*containing_descriptor); + printer_->Print("$nested_name$.containing_type = $parent_name$\n", + "nested_name", nested_name, "parent_name", parent_name); } } @@ -1050,16 +1070,15 @@ void Generator::FixForeignFieldsInExtension( FixForeignFieldsInField(extension_field.extension_scope(), extension_field, "extensions_by_name"); - std::map m; + std::map m; // Confusingly, for FieldDescriptors that happen to be extensions, // containing_type() means "extended type." // On the other hand, extension_scope() will give us what we normally // mean by containing_type(). - m["extended_message_class"] = ModuleLevelMessageName( - *extension_field.containing_type()); - m["field"] = FieldReferencingExpression(extension_field.extension_scope(), - extension_field, - "extensions_by_name"); + m["extended_message_class"] = + ModuleLevelMessageName(*extension_field.containing_type()); + m["field"] = FieldReferencingExpression( + extension_field.extension_scope(), extension_field, "extensions_by_name"); printer_->Print(m, "$extended_message_class$.RegisterExtension($field$)\n"); } @@ -1081,76 +1100,74 @@ void Generator::PrintEnumValueDescriptor( const EnumValueDescriptor& descriptor) const { // TODO(robinson): Fix up EnumValueDescriptor "type" fields. // More circular references. ::sigh:: - string options_string; + std::string options_string; descriptor.options().SerializeToString(&options_string); - std::map m; + std::map m; m["name"] = descriptor.name(); - m["index"] = SimpleItoa(descriptor.index()); - m["number"] = SimpleItoa(descriptor.number()); + m["index"] = StrCat(descriptor.index()); + m["number"] = StrCat(descriptor.number()); m["options"] = OptionsValue(options_string); - printer_->Print( - m, - "_descriptor.EnumValueDescriptor(\n" - " name='$name$', index=$index$, number=$number$,\n" - " serialized_options=$options$,\n" - " type=None)"); + printer_->Print(m, + "_descriptor.EnumValueDescriptor(\n" + " name='$name$', index=$index$, number=$number$,\n" + " serialized_options=$options$,\n" + " type=None)"); } // Returns a CEscaped string of serialized_options. -string Generator::OptionsValue(const string& serialized_options) const { +std::string Generator::OptionsValue( + const std::string& serialized_options) const { if (serialized_options.length() == 0 || GeneratingDescriptorProto()) { return "None"; } else { -//##!PY25 return "b'('" + CEscape(serialized_options)+ "')"; - return "_b('"+ CEscape(serialized_options) + "')"; //##PY25 + //##!PY25 return "b'('" + CEscape(serialized_options)+ "')"; + return "_b('" + CEscape(serialized_options) + "')"; //##PY25 } } // Prints an expression for a Python FieldDescriptor for |field|. -void Generator::PrintFieldDescriptor( - const FieldDescriptor& field, bool is_extension) const { - string options_string; +void Generator::PrintFieldDescriptor(const FieldDescriptor& field, + bool is_extension) const { + std::string options_string; field.options().SerializeToString(&options_string); - std::map m; + std::map m; m["name"] = field.name(); m["full_name"] = field.full_name(); - m["index"] = SimpleItoa(field.index()); - m["number"] = SimpleItoa(field.number()); - m["type"] = SimpleItoa(field.type()); - m["cpp_type"] = SimpleItoa(field.cpp_type()); - m["label"] = SimpleItoa(field.label()); + m["index"] = StrCat(field.index()); + m["number"] = StrCat(field.number()); + m["type"] = StrCat(field.type()); + m["cpp_type"] = StrCat(field.cpp_type()); + m["label"] = StrCat(field.label()); m["has_default_value"] = field.has_default_value() ? "True" : "False"; m["default_value"] = StringifyDefaultValue(field); m["is_extension"] = is_extension ? "True" : "False"; m["serialized_options"] = OptionsValue(options_string); - m["json_name"] = field.has_json_name() ? - ", json_name='" + field.json_name() + "'": ""; + m["json_name"] = + field.has_json_name() ? ", json_name='" + field.json_name() + "'" : ""; // We always set message_type and enum_type to None at this point, and then // these fields in correctly after all referenced descriptors have been // defined and/or imported (see FixForeignFieldsInDescriptors()). const char field_descriptor_decl[] = - "_descriptor.FieldDescriptor(\n" - " name='$name$', full_name='$full_name$', index=$index$,\n" - " number=$number$, type=$type$, cpp_type=$cpp_type$, label=$label$,\n" - " has_default_value=$has_default_value$, default_value=$default_value$,\n" - " message_type=None, enum_type=None, containing_type=None,\n" - " is_extension=$is_extension$, extension_scope=None,\n" - " serialized_options=$serialized_options$$json_name$, file=DESCRIPTOR)"; + "_descriptor.FieldDescriptor(\n" + " name='$name$', full_name='$full_name$', index=$index$,\n" + " number=$number$, type=$type$, cpp_type=$cpp_type$, label=$label$,\n" + " has_default_value=$has_default_value$, " + "default_value=$default_value$,\n" + " message_type=None, enum_type=None, containing_type=None,\n" + " is_extension=$is_extension$, extension_scope=None,\n" + " serialized_options=$serialized_options$$json_name$, file=DESCRIPTOR)"; printer_->Print(m, field_descriptor_decl); } // Helper for Print{Fields,Extensions}InDescriptor(). void Generator::PrintFieldDescriptorsInDescriptor( - const Descriptor& message_descriptor, - bool is_extension, - const string& list_variable_name, - int (Descriptor::*CountFn)() const, - const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const { + const Descriptor& message_descriptor, bool is_extension, + const std::string& list_variable_name, int (Descriptor::*CountFn)() const, + const FieldDescriptor* (Descriptor::*GetterFn)(int)const) const { printer_->Print("$list$=[\n", "list", list_variable_name); printer_->Indent(); for (int i = 0; i < (message_descriptor.*CountFn)(); ++i) { - PrintFieldDescriptor(*(message_descriptor.*GetterFn)(i), - is_extension); + PrintFieldDescriptor(*(message_descriptor.*GetterFn)(i), is_extension); printer_->Print(",\n"); } printer_->Outdent(); @@ -1162,9 +1179,9 @@ void Generator::PrintFieldDescriptorsInDescriptor( void Generator::PrintFieldsInDescriptor( const Descriptor& message_descriptor) const { const bool is_extension = false; - PrintFieldDescriptorsInDescriptor( - message_descriptor, is_extension, "fields", - &Descriptor::field_count, &Descriptor::field); + PrintFieldDescriptorsInDescriptor(message_descriptor, is_extension, "fields", + &Descriptor::field_count, + &Descriptor::field); } // Prints a statement assigning "extensions" to a list of Python @@ -1172,9 +1189,9 @@ void Generator::PrintFieldsInDescriptor( void Generator::PrintExtensionsInDescriptor( const Descriptor& message_descriptor) const { const bool is_extension = true; - PrintFieldDescriptorsInDescriptor( - message_descriptor, is_extension, "extensions", - &Descriptor::extension_count, &Descriptor::extension); + PrintFieldDescriptorsInDescriptor(message_descriptor, is_extension, + "extensions", &Descriptor::extension_count, + &Descriptor::extension); } bool Generator::GeneratingDescriptorProto() const { @@ -1186,7 +1203,7 @@ bool Generator::GeneratingDescriptorProto() const { // This name is module-qualified iff the given descriptor describes an // entity that doesn't come from the current file. template -string Generator::ModuleLevelDescriptorName( +std::string Generator::ModuleLevelDescriptorName( const DescriptorT& descriptor) const { // FIXME(robinson): // We currently don't worry about collisions with underscores in the type @@ -1200,7 +1217,7 @@ string Generator::ModuleLevelDescriptorName( // // The C++ implementation doesn't guard against this either. Leaving // it for now... - string name = NamePrefixedWithNestedTypes(descriptor, "_"); + std::string name = NamePrefixedWithNestedTypes(descriptor, "_"); UpperString(&name); // Module-private for now. Easy to make public later; almost impossible // to make private later. @@ -1217,8 +1234,9 @@ string Generator::ModuleLevelDescriptorName( // Like ModuleLevelDescriptorName(), module-qualifies the name iff // the given descriptor describes an entity that doesn't come from // the current file. -string Generator::ModuleLevelMessageName(const Descriptor& descriptor) const { - string name = NamePrefixedWithNestedTypes(descriptor, "."); +std::string Generator::ModuleLevelMessageName( + const Descriptor& descriptor) const { + std::string name = NamePrefixedWithNestedTypes(descriptor, "."); if (descriptor.file() != file_) { name = ModuleAlias(descriptor.file()->name()) + "." + name; } @@ -1227,9 +1245,9 @@ string Generator::ModuleLevelMessageName(const Descriptor& descriptor) const { // Returns the unique Python module-level identifier given to a service // descriptor. -string Generator::ModuleLevelServiceDescriptorName( +std::string Generator::ModuleLevelServiceDescriptorName( const ServiceDescriptor& descriptor) const { - string name = descriptor.name(); + std::string name = descriptor.name(); UpperString(&name); name = "_" + name; if (descriptor.file() != file_) { @@ -1247,10 +1265,10 @@ string Generator::ModuleLevelServiceDescriptorName( // serialized_end=43, // template -void Generator::PrintSerializedPbInterval( - const DescriptorT& descriptor, DescriptorProtoT& proto) const { +void Generator::PrintSerializedPbInterval(const DescriptorT& descriptor, + DescriptorProtoT& proto) const { descriptor.CopyTo(&proto); - string sp; + std::string sp; proto.SerializeToString(&sp); int offset = file_descriptor_serialized_.find(sp); GOOGLE_CHECK_GE(offset, 0); @@ -1258,26 +1276,24 @@ void Generator::PrintSerializedPbInterval( printer_->Print( "serialized_start=$serialized_start$,\n" "serialized_end=$serialized_end$,\n", - "serialized_start", SimpleItoa(offset), "serialized_end", - SimpleItoa(offset + sp.size())); + "serialized_start", StrCat(offset), "serialized_end", + StrCat(offset + sp.size())); } namespace { -void PrintDescriptorOptionsFixingCode(const string& descriptor, - const string& options, +void PrintDescriptorOptionsFixingCode(const std::string& descriptor, + const std::string& options, io::Printer* printer) { // Reset the _options to None thus DescriptorBase.GetOptions() can // parse _options again after extensions are registered. - printer->Print( - "$descriptor$._options = None\n", - "descriptor", descriptor); + printer->Print("$descriptor$._options = None\n", "descriptor", descriptor); } } // namespace // Prints expressions that set the options field of all descriptors. void Generator::FixAllDescriptorOptions() const { // Prints an expression that sets the file descriptor's options. - string file_options = OptionsValue(file_->options().SerializeAsString()); + std::string file_options = OptionsValue(file_->options().SerializeAsString()); if (file_options != "None") { PrintDescriptorOptionsFixingCode(kDescriptorKey, file_options, printer_); } @@ -1299,11 +1315,10 @@ void Generator::FixAllDescriptorOptions() const { } void Generator::FixOptionsForOneof(const OneofDescriptor& oneof) const { - string oneof_options = OptionsValue(oneof.options().SerializeAsString()); + std::string oneof_options = OptionsValue(oneof.options().SerializeAsString()); if (oneof_options != "None") { - string oneof_name = strings::Substitute( - "$0.$1['$2']", - ModuleLevelDescriptorName(*oneof.containing_type()), + std::string oneof_name = strings::Substitute( + "$0.$1['$2']", ModuleLevelDescriptorName(*oneof.containing_type()), "oneofs_by_name", oneof.name()); PrintDescriptorOptionsFixingCode(oneof_name, oneof_options, printer_); } @@ -1312,16 +1327,16 @@ void Generator::FixOptionsForOneof(const OneofDescriptor& oneof) const { // Prints expressions that set the options for an enum descriptor and its // value descriptors. void Generator::FixOptionsForEnum(const EnumDescriptor& enum_descriptor) const { - string descriptor_name = ModuleLevelDescriptorName(enum_descriptor); - string enum_options = OptionsValue( - enum_descriptor.options().SerializeAsString()); + std::string descriptor_name = ModuleLevelDescriptorName(enum_descriptor); + std::string enum_options = + OptionsValue(enum_descriptor.options().SerializeAsString()); if (enum_options != "None") { PrintDescriptorOptionsFixingCode(descriptor_name, enum_options, printer_); } for (int i = 0; i < enum_descriptor.value_count(); ++i) { const EnumValueDescriptor& value_descriptor = *enum_descriptor.value(i); - string value_options = OptionsValue( - value_descriptor.options().SerializeAsString()); + std::string value_options = + OptionsValue(value_descriptor.options().SerializeAsString()); if (value_options != "None") { PrintDescriptorOptionsFixingCode( StringPrintf("%s.values_by_name[\"%s\"]", descriptor_name.c_str(), @@ -1333,22 +1348,21 @@ void Generator::FixOptionsForEnum(const EnumDescriptor& enum_descriptor) const { // Prints expressions that set the options for field descriptors (including // extensions). -void Generator::FixOptionsForField( - const FieldDescriptor& field) const { - string field_options = OptionsValue(field.options().SerializeAsString()); +void Generator::FixOptionsForField(const FieldDescriptor& field) const { + std::string field_options = OptionsValue(field.options().SerializeAsString()); if (field_options != "None") { - string field_name; + std::string field_name; if (field.is_extension()) { if (field.extension_scope() == NULL) { // Top level extensions. field_name = field.name(); } else { - field_name = FieldReferencingExpression( - field.extension_scope(), field, "extensions_by_name"); + field_name = FieldReferencingExpression(field.extension_scope(), field, + "extensions_by_name"); } } else { - field_name = FieldReferencingExpression( - field.containing_type(), field, "fields_by_name"); + field_name = FieldReferencingExpression(field.containing_type(), field, + "fields_by_name"); } PrintDescriptorOptionsFixingCode(field_name, field_options, printer_); } @@ -1380,12 +1394,11 @@ void Generator::FixOptionsForMessage(const Descriptor& descriptor) const { FixOptionsForField(field); } // Message option for this message. - string message_options = OptionsValue( - descriptor.options().SerializeAsString()); + std::string message_options = + OptionsValue(descriptor.options().SerializeAsString()); if (message_options != "None") { - string descriptor_name = ModuleLevelDescriptorName(descriptor); - PrintDescriptorOptionsFixingCode(descriptor_name, - message_options, + std::string descriptor_name = ModuleLevelDescriptorName(descriptor); + PrintDescriptorOptionsFixingCode(descriptor_name, message_options, printer_); } } @@ -1393,20 +1406,19 @@ void Generator::FixOptionsForMessage(const Descriptor& descriptor) const { // If a dependency forwards other files through public dependencies, let's // copy over the corresponding module aliases. void Generator::CopyPublicDependenciesAliases( - const string& copy_from, const FileDescriptor* file) const { + const std::string& copy_from, const FileDescriptor* file) const { for (int i = 0; i < file->public_dependency_count(); ++i) { - string module_name = ModuleName(file->public_dependency(i)->name()); - string module_alias = ModuleAlias(file->public_dependency(i)->name()); + std::string module_name = ModuleName(file->public_dependency(i)->name()); + std::string module_alias = ModuleAlias(file->public_dependency(i)->name()); // There's no module alias in the dependent file if it was generated by // an old protoc (less than 3.0.0-alpha-1). Use module name in this // situation. - printer_->Print("try:\n" - " $alias$ = $copy_from$.$alias$\n" - "except AttributeError:\n" - " $alias$ = $copy_from$.$module$\n", - "alias", module_alias, - "module", module_name, - "copy_from", copy_from); + printer_->Print( + "try:\n" + " $alias$ = $copy_from$.$alias$\n" + "except AttributeError:\n" + " $alias$ = $copy_from$.$module$\n", + "alias", module_alias, "module", module_name, "copy_from", copy_from); CopyPublicDependenciesAliases(copy_from, file->public_dependency(i)); } } diff --git a/src/google/protobuf/compiler/python/python_generator.h b/src/google/protobuf/compiler/python/python_generator.h index 99a7383280..4630671702 100644 --- a/src/google/protobuf/compiler/python/python_generator.h +++ b/src/google/protobuf/compiler/python/python_generator.h @@ -38,6 +38,9 @@ #include #include +#include + +#include namespace google { namespace protobuf { @@ -49,7 +52,9 @@ class FieldDescriptor; class OneofDescriptor; class ServiceDescriptor; -namespace io { class Printer; } +namespace io { +class Printer; +} namespace compiler { namespace python { @@ -58,16 +63,16 @@ namespace python { // If you create your own protocol compiler binary and you want it to support // Python output, you can do so by registering an instance of this // CodeGenerator with the CommandLineInterface in your main() function. -class LIBPROTOC_EXPORT Generator : public CodeGenerator { +class PROTOC_EXPORT Generator : public CodeGenerator { public: Generator(); virtual ~Generator(); // CodeGenerator methods. virtual bool Generate(const FileDescriptor* file, - const string& parameter, + const std::string& parameter, GeneratorContext* generator_context, - string* error) const; + std::string* error) const; private: void PrintImports() const; @@ -79,14 +84,12 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { void PrintTopLevelExtensions() const; - void PrintFieldDescriptor( - const FieldDescriptor& field, bool is_extension) const; + void PrintFieldDescriptor(const FieldDescriptor& field, + bool is_extension) const; void PrintFieldDescriptorsInDescriptor( - const Descriptor& message_descriptor, - bool is_extension, - const string& list_variable_name, - int (Descriptor::*CountFn)() const, - const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const; + const Descriptor& message_descriptor, bool is_extension, + const std::string& list_variable_name, int (Descriptor::*CountFn)() const, + const FieldDescriptor* (Descriptor::*GetterFn)(int)const) const; void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const; void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const; void PrintMessageDescriptors() const; @@ -94,11 +97,13 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { void PrintNestedDescriptors(const Descriptor& containing_descriptor) const; void PrintMessages() const; - void PrintMessage(const Descriptor& message_descriptor, const string& prefix, - std::vector* to_register) const; + void PrintMessage(const Descriptor& message_descriptor, + const std::string& prefix, + std::vector* to_register, + bool is_nested) const; void PrintNestedMessages(const Descriptor& containing_descriptor, - const string& prefix, - std::vector* to_register) const; + const std::string& prefix, + std::vector* to_register) const; void FixForeignFieldsInDescriptors() const; void FixForeignFieldsInDescriptor( @@ -106,14 +111,14 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { const Descriptor* containing_descriptor) const; void FixForeignFieldsInField(const Descriptor* containing_type, const FieldDescriptor& field, - const string& python_dict_name) const; + const std::string& python_dict_name) const; void AddMessageToFileDescriptor(const Descriptor& descriptor) const; void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const; void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const; void AddServiceToFileDescriptor(const ServiceDescriptor& descriptor) const; - string FieldReferencingExpression(const Descriptor* containing_type, - const FieldDescriptor& field, - const string& python_dict_name) const; + std::string FieldReferencingExpression( + const Descriptor* containing_type, const FieldDescriptor& field, + const std::string& python_dict_name) const; template void FixContainingTypeInDescriptor( const DescriptorT& descriptor, @@ -133,18 +138,18 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { const ServiceDescriptor& descriptor) const; void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const; - string OptionsValue(const string& serialized_options) const; + std::string OptionsValue(const std::string& serialized_options) const; bool GeneratingDescriptorProto() const; template - string ModuleLevelDescriptorName(const DescriptorT& descriptor) const; - string ModuleLevelMessageName(const Descriptor& descriptor) const; - string ModuleLevelServiceDescriptorName( + std::string ModuleLevelDescriptorName(const DescriptorT& descriptor) const; + std::string ModuleLevelMessageName(const Descriptor& descriptor) const; + std::string ModuleLevelServiceDescriptorName( const ServiceDescriptor& descriptor) const; template - void PrintSerializedPbInterval( - const DescriptorT& descriptor, DescriptorProtoT& proto) const; + void PrintSerializedPbInterval(const DescriptorT& descriptor, + DescriptorProtoT& proto) const; void FixAllDescriptorOptions() const; void FixOptionsForField(const FieldDescriptor& field) const; @@ -152,14 +157,14 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { void FixOptionsForEnum(const EnumDescriptor& descriptor) const; void FixOptionsForMessage(const Descriptor& descriptor) const; - void CopyPublicDependenciesAliases( - const string& copy_from, const FileDescriptor* file) const; + void CopyPublicDependenciesAliases(const std::string& copy_from, + const FileDescriptor* file) const; // Very coarse-grained lock to ensure that Generate() is reentrant. // Guards file_, printer_ and file_descriptor_serialized_. mutable Mutex mutex_; mutable const FileDescriptor* file_; // Set in Generate(). Under mutex_. - mutable string file_descriptor_serialized_; + mutable std::string file_descriptor_serialized_; mutable io::Printer* printer_; // Set in Generate(). Under mutex_. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator); @@ -170,4 +175,6 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/python/python_plugin_unittest.cc b/src/google/protobuf/compiler/python/python_plugin_unittest.cc index d19d11f0c4..8d7ef98b27 100644 --- a/src/google/protobuf/compiler/python/python_plugin_unittest.cc +++ b/src/google/protobuf/compiler/python/python_plugin_unittest.cc @@ -36,10 +36,10 @@ #include -#include #include -#include +#include #include +#include #include #include @@ -59,9 +59,8 @@ class TestGenerator : public CodeGenerator { ~TestGenerator() {} virtual bool Generate(const FileDescriptor* file, - const string& parameter, - GeneratorContext* context, - string* error) const { + const std::string& parameter, GeneratorContext* context, + std::string* error) const { TryInsert("test_pb2.py", "imports", context); TryInsert("test_pb2.py", "module_scope", context); TryInsert("test_pb2.py", "class_scope:foo.Bar", context); @@ -69,7 +68,8 @@ class TestGenerator : public CodeGenerator { return true; } - void TryInsert(const string& filename, const string& insertion_point, + void TryInsert(const std::string& filename, + const std::string& insertion_point, GeneratorContext* context) const { std::unique_ptr output( context->OpenForInsert(filename, insertion_point)); @@ -98,17 +98,12 @@ TEST(PythonPluginTest, PluginTest) { cli.RegisterGenerator("--python_out", &python_generator, ""); cli.RegisterGenerator("--test_out", &test_generator, ""); - string proto_path = "-I" + TestTempDir(); - string python_out = "--python_out=" + TestTempDir(); - string test_out = "--test_out=" + TestTempDir(); + std::string proto_path = "-I" + TestTempDir(); + std::string python_out = "--python_out=" + TestTempDir(); + std::string test_out = "--test_out=" + TestTempDir(); - const char* argv[] = { - "protoc", - proto_path.c_str(), - python_out.c_str(), - test_out.c_str(), - "test.proto" - }; + const char* argv[] = {"protoc", proto_path.c_str(), python_out.c_str(), + test_out.c_str(), "test.proto"}; EXPECT_EQ(0, cli.Run(5, argv)); } @@ -137,25 +132,25 @@ TEST(PythonPluginTest, ImportTest) { cli.SetInputsAreProtoPathRelative(true); python::Generator python_generator; cli.RegisterGenerator("--python_out", &python_generator, ""); - string proto_path = "-I" + TestTempDir(); - string python_out = "--python_out=" + TestTempDir(); + std::string proto_path = "-I" + TestTempDir(); + std::string python_out = "--python_out=" + TestTempDir(); const char* argv[] = {"protoc", proto_path.c_str(), "-I.", python_out.c_str(), "test1.proto"}; ASSERT_EQ(0, cli.Run(5, argv)); // Loop over the lines of the generated code and verify that we find an // ordinary Python import but do not find the string "importlib". - string output; + std::string output; GOOGLE_CHECK_OK(File::GetContents(TestTempDir() + "/test1_pb2.py", &output, true)); - std::vector lines = Split(output, "\n"); - string expected_import = "import test2_pb2"; + std::vector lines = Split(output, "\n"); + std::string expected_import = "import test2_pb2"; bool found_expected_import = false; for (int i = 0; i < lines.size(); ++i) { - if (lines[i].find(expected_import) != string::npos) { + if (lines[i].find(expected_import) != std::string::npos) { found_expected_import = true; } - EXPECT_EQ(string::npos, lines[i].find("importlib")); + EXPECT_EQ(std::string::npos, lines[i].find("importlib")); } EXPECT_TRUE(found_expected_import); } diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_code_pb.rb b/src/google/protobuf/compiler/ruby/ruby_generated_code_pb.rb index 49b23fbe8d..7e66d1ecc8 100644 --- a/src/google/protobuf/compiler/ruby/ruby_generated_code_pb.rb +++ b/src/google/protobuf/compiler/ruby/ruby_generated_code_pb.rb @@ -4,71 +4,73 @@ require 'google/protobuf' Google::Protobuf::DescriptorPool.generated_pool.build do - add_message "A.B.C.TestMessage" do - optional :optional_int32, :int32, 1 - optional :optional_int64, :int64, 2 - optional :optional_uint32, :uint32, 3 - optional :optional_uint64, :uint64, 4 - optional :optional_bool, :bool, 5 - optional :optional_double, :double, 6 - optional :optional_float, :float, 7 - optional :optional_string, :string, 8 - optional :optional_bytes, :bytes, 9 - optional :optional_enum, :enum, 10, "A.B.C.TestEnum" - optional :optional_msg, :message, 11, "A.B.C.TestMessage" - repeated :repeated_int32, :int32, 21 - repeated :repeated_int64, :int64, 22 - repeated :repeated_uint32, :uint32, 23 - repeated :repeated_uint64, :uint64, 24 - repeated :repeated_bool, :bool, 25 - repeated :repeated_double, :double, 26 - repeated :repeated_float, :float, 27 - repeated :repeated_string, :string, 28 - repeated :repeated_bytes, :bytes, 29 - repeated :repeated_enum, :enum, 30, "A.B.C.TestEnum" - repeated :repeated_msg, :message, 31, "A.B.C.TestMessage" - map :map_int32_string, :int32, :string, 61 - map :map_int64_string, :int64, :string, 62 - map :map_uint32_string, :uint32, :string, 63 - map :map_uint64_string, :uint64, :string, 64 - map :map_bool_string, :bool, :string, 65 - map :map_string_string, :string, :string, 66 - map :map_string_msg, :string, :message, 67, "A.B.C.TestMessage" - map :map_string_enum, :string, :enum, 68, "A.B.C.TestEnum" - map :map_string_int32, :string, :int32, 69 - map :map_string_bool, :string, :bool, 70 - optional :nested_message, :message, 80, "A.B.C.TestMessage.NestedMessage" - oneof :my_oneof do - optional :oneof_int32, :int32, 41 - optional :oneof_int64, :int64, 42 - optional :oneof_uint32, :uint32, 43 - optional :oneof_uint64, :uint64, 44 - optional :oneof_bool, :bool, 45 - optional :oneof_double, :double, 46 - optional :oneof_float, :float, 47 - optional :oneof_string, :string, 48 - optional :oneof_bytes, :bytes, 49 - optional :oneof_enum, :enum, 50, "A.B.C.TestEnum" - optional :oneof_msg, :message, 51, "A.B.C.TestMessage" + add_file("ruby_generated_code.proto", :syntax => :proto3) do + add_message "A.B.C.TestMessage" do + optional :optional_int32, :int32, 1 + optional :optional_int64, :int64, 2 + optional :optional_uint32, :uint32, 3 + optional :optional_uint64, :uint64, 4 + optional :optional_bool, :bool, 5 + optional :optional_double, :double, 6 + optional :optional_float, :float, 7 + optional :optional_string, :string, 8 + optional :optional_bytes, :bytes, 9 + optional :optional_enum, :enum, 10, "A.B.C.TestEnum" + optional :optional_msg, :message, 11, "A.B.C.TestMessage" + repeated :repeated_int32, :int32, 21 + repeated :repeated_int64, :int64, 22 + repeated :repeated_uint32, :uint32, 23 + repeated :repeated_uint64, :uint64, 24 + repeated :repeated_bool, :bool, 25 + repeated :repeated_double, :double, 26 + repeated :repeated_float, :float, 27 + repeated :repeated_string, :string, 28 + repeated :repeated_bytes, :bytes, 29 + repeated :repeated_enum, :enum, 30, "A.B.C.TestEnum" + repeated :repeated_msg, :message, 31, "A.B.C.TestMessage" + map :map_int32_string, :int32, :string, 61 + map :map_int64_string, :int64, :string, 62 + map :map_uint32_string, :uint32, :string, 63 + map :map_uint64_string, :uint64, :string, 64 + map :map_bool_string, :bool, :string, 65 + map :map_string_string, :string, :string, 66 + map :map_string_msg, :string, :message, 67, "A.B.C.TestMessage" + map :map_string_enum, :string, :enum, 68, "A.B.C.TestEnum" + map :map_string_int32, :string, :int32, 69 + map :map_string_bool, :string, :bool, 70 + optional :nested_message, :message, 80, "A.B.C.TestMessage.NestedMessage" + oneof :my_oneof do + optional :oneof_int32, :int32, 41 + optional :oneof_int64, :int64, 42 + optional :oneof_uint32, :uint32, 43 + optional :oneof_uint64, :uint64, 44 + optional :oneof_bool, :bool, 45 + optional :oneof_double, :double, 46 + optional :oneof_float, :float, 47 + optional :oneof_string, :string, 48 + optional :oneof_bytes, :bytes, 49 + optional :oneof_enum, :enum, 50, "A.B.C.TestEnum" + optional :oneof_msg, :message, 51, "A.B.C.TestMessage" + end + end + add_message "A.B.C.TestMessage.NestedMessage" do + optional :foo, :int32, 1 + end + add_enum "A.B.C.TestEnum" do + value :Default, 0 + value :A, 1 + value :B, 2 + value :C, 3 end - end - add_message "A.B.C.TestMessage.NestedMessage" do - optional :foo, :int32, 1 - end - add_enum "A.B.C.TestEnum" do - value :Default, 0 - value :A, 1 - value :B, 2 - value :C, 3 end end module A module B module C - TestMessage = Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestMessage").msgclass - TestMessage::NestedMessage = Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestMessage.NestedMessage").msgclass - TestEnum = Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestEnum").enummodule + TestMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestMessage").msgclass + TestMessage::NestedMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestMessage.NestedMessage").msgclass + TestEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestEnum").enummodule end end end diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_code_proto2.proto b/src/google/protobuf/compiler/ruby/ruby_generated_code_proto2.proto new file mode 100644 index 0000000000..8d3cc13e03 --- /dev/null +++ b/src/google/protobuf/compiler/ruby/ruby_generated_code_proto2.proto @@ -0,0 +1,68 @@ +syntax = "proto2"; + +package A.B.C; + +message TestMessage { + optional int32 optional_int32 = 1 [default = 1]; + optional int64 optional_int64 = 2 [default = 2]; + optional uint32 optional_uint32 = 3 [default = 3]; + optional uint64 optional_uint64 = 4 [default = 4]; + optional bool optional_bool = 5 [default = true]; + optional double optional_double = 6 [default = 6.0]; + optional float optional_float = 7 [default = 7.0]; + optional string optional_string = 8 [default = "default str"]; + optional bytes optional_bytes = 9 [default = "\0\1\2\100fubar"]; + optional TestEnum optional_enum = 10 [default = A]; + optional TestMessage optional_msg = 11; + + repeated int32 repeated_int32 = 21; + repeated int64 repeated_int64 = 22; + repeated uint32 repeated_uint32 = 23; + repeated uint64 repeated_uint64 = 24; + repeated bool repeated_bool = 25; + repeated double repeated_double = 26; + repeated float repeated_float = 27; + repeated string repeated_string = 28; + repeated bytes repeated_bytes = 29; + repeated TestEnum repeated_enum = 30; + repeated TestMessage repeated_msg = 31; + + required int32 required_int32 = 41; + required int64 required_int64 = 42; + required uint32 required_uint32 = 43; + required uint64 required_uint64 = 44; + required bool required_bool = 45; + required double required_double = 46; + required float required_float = 47; + required string required_string = 48; + required bytes required_bytes = 49; + required TestEnum required_enum = 50; + required TestMessage required_msg = 51; + + oneof my_oneof { + int32 oneof_int32 = 61; + int64 oneof_int64 = 62; + uint32 oneof_uint32 = 63; + uint64 oneof_uint64 = 64; + bool oneof_bool = 65; + double oneof_double = 66; + float oneof_float = 67; + string oneof_string = 68; + bytes oneof_bytes = 69; + TestEnum oneof_enum = 70; + TestMessage oneof_msg = 71; + } + + message NestedMessage { + optional int32 foo = 1; + } + + optional NestedMessage nested_message = 80; +} + +enum TestEnum { + Default = 0; + A = 1; + B = 2; + C = 3; +} diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_code_proto2_pb.rb b/src/google/protobuf/compiler/ruby/ruby_generated_code_proto2_pb.rb new file mode 100644 index 0000000000..c89738fe83 --- /dev/null +++ b/src/google/protobuf/compiler/ruby/ruby_generated_code_proto2_pb.rb @@ -0,0 +1,77 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: ruby_generated_code_proto2.proto + +require 'google/protobuf' + +Google::Protobuf::DescriptorPool.generated_pool.build do + add_file("ruby_generated_code_proto2.proto", :syntax => :proto2) do + add_message "A.B.C.TestMessage" do + optional :optional_int32, :int32, 1, default: 1 + optional :optional_int64, :int64, 2, default: 2 + optional :optional_uint32, :uint32, 3, default: 3 + optional :optional_uint64, :uint64, 4, default: 4 + optional :optional_bool, :bool, 5, default: true + optional :optional_double, :double, 6, default: 6 + optional :optional_float, :float, 7, default: 7 + optional :optional_string, :string, 8, default: "default str" + optional :optional_bytes, :bytes, 9, default: "\x00\x01\x02\x40\x66\x75\x62\x61\x72".force_encoding("ASCII-8BIT") + optional :optional_enum, :enum, 10, "A.B.C.TestEnum", default: 1 + optional :optional_msg, :message, 11, "A.B.C.TestMessage" + repeated :repeated_int32, :int32, 21 + repeated :repeated_int64, :int64, 22 + repeated :repeated_uint32, :uint32, 23 + repeated :repeated_uint64, :uint64, 24 + repeated :repeated_bool, :bool, 25 + repeated :repeated_double, :double, 26 + repeated :repeated_float, :float, 27 + repeated :repeated_string, :string, 28 + repeated :repeated_bytes, :bytes, 29 + repeated :repeated_enum, :enum, 30, "A.B.C.TestEnum" + repeated :repeated_msg, :message, 31, "A.B.C.TestMessage" + required :required_int32, :int32, 41 + required :required_int64, :int64, 42 + required :required_uint32, :uint32, 43 + required :required_uint64, :uint64, 44 + required :required_bool, :bool, 45 + required :required_double, :double, 46 + required :required_float, :float, 47 + required :required_string, :string, 48 + required :required_bytes, :bytes, 49 + required :required_enum, :enum, 50, "A.B.C.TestEnum" + required :required_msg, :message, 51, "A.B.C.TestMessage" + optional :nested_message, :message, 80, "A.B.C.TestMessage.NestedMessage" + oneof :my_oneof do + optional :oneof_int32, :int32, 61 + optional :oneof_int64, :int64, 62 + optional :oneof_uint32, :uint32, 63 + optional :oneof_uint64, :uint64, 64 + optional :oneof_bool, :bool, 65 + optional :oneof_double, :double, 66 + optional :oneof_float, :float, 67 + optional :oneof_string, :string, 68 + optional :oneof_bytes, :bytes, 69 + optional :oneof_enum, :enum, 70, "A.B.C.TestEnum" + optional :oneof_msg, :message, 71, "A.B.C.TestMessage" + end + end + add_message "A.B.C.TestMessage.NestedMessage" do + optional :foo, :int32, 1 + end + add_enum "A.B.C.TestEnum" do + value :Default, 0 + value :A, 1 + value :B, 2 + value :C, 3 + end + end +end + +module A + module B + module C + TestMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestMessage").msgclass + TestMessage::NestedMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestMessage.NestedMessage").msgclass + TestEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("A.B.C.TestEnum").enummodule + end + end +end diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit.proto b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit.proto new file mode 100644 index 0000000000..8d7c948a18 --- /dev/null +++ b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package one.two.a_three; + +option ruby_package = "A::B::C"; + +message Four { + string a_string = 1; +} diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_legacy.proto b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_legacy.proto new file mode 100644 index 0000000000..7a0d260865 --- /dev/null +++ b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_legacy.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package one.two.a_three.and; + +option ruby_package = "AA.BB.CC"; + +message Four { + string another_string = 1; +} diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_legacy_pb.rb b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_legacy_pb.rb new file mode 100644 index 0000000000..cdbbe891bc --- /dev/null +++ b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_legacy_pb.rb @@ -0,0 +1,20 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: ruby_generated_pkg_explicit_legacy.proto + +require 'google/protobuf' + +Google::Protobuf::DescriptorPool.generated_pool.build do + add_file("ruby_generated_pkg_explicit_legacy.proto", :syntax => :proto3) do + add_message "one.two.a_three.and.Four" do + optional :another_string, :string, 1 + end + end +end + +module AA + module BB + module CC + Four = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("one.two.a_three.and.Four").msgclass + end + end +end diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_pb.rb b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_pb.rb new file mode 100644 index 0000000000..e6d47011e8 --- /dev/null +++ b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_explicit_pb.rb @@ -0,0 +1,20 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: ruby_generated_pkg_explicit.proto + +require 'google/protobuf' + +Google::Protobuf::DescriptorPool.generated_pool.build do + add_file("ruby_generated_pkg_explicit.proto", :syntax => :proto3) do + add_message "one.two.a_three.Four" do + optional :a_string, :string, 1 + end + end +end + +module A + module B + module C + Four = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("one.two.a_three.Four").msgclass + end + end +end diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_pkg_implicit.proto b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_implicit.proto new file mode 100644 index 0000000000..544db64d94 --- /dev/null +++ b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_implicit.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +package one.two.a_three; + +message Four { + string a_string = 1; +} diff --git a/src/google/protobuf/compiler/ruby/ruby_generated_pkg_implicit_pb.rb b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_implicit_pb.rb new file mode 100644 index 0000000000..1ac0ef7ad0 --- /dev/null +++ b/src/google/protobuf/compiler/ruby/ruby_generated_pkg_implicit_pb.rb @@ -0,0 +1,20 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: ruby_generated_pkg_implicit.proto + +require 'google/protobuf' + +Google::Protobuf::DescriptorPool.generated_pool.build do + add_file("ruby_generated_pkg_implicit.proto", :syntax => :proto3) do + add_message "one.two.a_three.Four" do + optional :a_string, :string, 1 + end + end +end + +module One + module Two + module AThree + Four = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("one.two.a_three.Four").msgclass + end + end +end diff --git a/src/google/protobuf/compiler/ruby/ruby_generator.cc b/src/google/protobuf/compiler/ruby/ruby_generator.cc index bd737c0278..67b5538cd7 100644 --- a/src/google/protobuf/compiler/ruby/ruby_generator.cc +++ b/src/google/protobuf/compiler/ruby/ruby_generator.cc @@ -28,6 +28,7 @@ // (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 @@ -45,24 +46,22 @@ namespace compiler { namespace ruby { // Forward decls. -std::string IntToString(int32 value); +template +std::string NumberToString(numeric_type value); std::string GetRequireName(const std::string& proto_file); -std::string LabelForField(google::protobuf::FieldDescriptor* field); -std::string TypeName(google::protobuf::FieldDescriptor* field); -void GenerateMessage(const google::protobuf::Descriptor* message, - google::protobuf::io::Printer* printer); -void GenerateEnum(const google::protobuf::EnumDescriptor* en, - google::protobuf::io::Printer* printer); -void GenerateMessageAssignment( - const std::string& prefix, - const google::protobuf::Descriptor* message, - google::protobuf::io::Printer* printer); -void GenerateEnumAssignment( - const std::string& prefix, - const google::protobuf::EnumDescriptor* en, - google::protobuf::io::Printer* printer); - -std::string IntToString(int32 value) { +std::string LabelForField(FieldDescriptor* field); +std::string TypeName(FieldDescriptor* field); +bool GenerateMessage(const Descriptor* message, io::Printer* printer, + std::string* error); +void GenerateEnum(const EnumDescriptor* en, io::Printer* printer); +void GenerateMessageAssignment(const std::string& prefix, + const Descriptor* message, io::Printer* printer); +void GenerateEnumAssignment(const std::string& prefix, const EnumDescriptor* en, + io::Printer* printer); +std::string DefaultValueForField(const FieldDescriptor* field); + +template +std::string NumberToString(numeric_type value) { std::ostringstream os; os << value; return os.str(); @@ -77,7 +76,7 @@ std::string GetOutputFilename(const std::string& proto_file) { return GetRequireName(proto_file) + ".rb"; } -std::string LabelForField(const google::protobuf::FieldDescriptor* field) { +std::string LabelForField(const FieldDescriptor* field) { switch (field->label()) { case FieldDescriptor::LABEL_OPTIONAL: return "optional"; case FieldDescriptor::LABEL_REQUIRED: return "required"; @@ -86,7 +85,7 @@ std::string LabelForField(const google::protobuf::FieldDescriptor* field) { } } -std::string TypeName(const google::protobuf::FieldDescriptor* field) { +std::string TypeName(const FieldDescriptor* field) { switch (field->type()) { case FieldDescriptor::TYPE_INT32: return "int32"; case FieldDescriptor::TYPE_INT64: return "int64"; @@ -110,9 +109,63 @@ std::string TypeName(const google::protobuf::FieldDescriptor* field) { } } -void GenerateField(const google::protobuf::FieldDescriptor* field, - google::protobuf::io::Printer* printer) { +string StringifySyntax(FileDescriptor::Syntax syntax) { + switch (syntax) { + case FileDescriptor::SYNTAX_PROTO2: + return "proto2"; + case FileDescriptor::SYNTAX_PROTO3: + return "proto3"; + case FileDescriptor::SYNTAX_UNKNOWN: + default: + GOOGLE_LOG(FATAL) << "Unsupported syntax; this generator only supports " + "proto2 and proto3 syntax."; + return ""; + } +} + +std::string DefaultValueForField(const FieldDescriptor* field) { + switch(field->cpp_type()) { + case FieldDescriptor::CPPTYPE_INT32: + return NumberToString(field->default_value_int32()); + case FieldDescriptor::CPPTYPE_INT64: + return NumberToString(field->default_value_int64()); + case FieldDescriptor::CPPTYPE_UINT32: + return NumberToString(field->default_value_uint32()); + case FieldDescriptor::CPPTYPE_UINT64: + return NumberToString(field->default_value_uint64()); + case FieldDescriptor::CPPTYPE_FLOAT: + return NumberToString(field->default_value_float()); + case FieldDescriptor::CPPTYPE_DOUBLE: + return NumberToString(field->default_value_double()); + case FieldDescriptor::CPPTYPE_BOOL: + return field->default_value_bool() ? "true" : "false"; + case FieldDescriptor::CPPTYPE_ENUM: + return NumberToString(field->default_value_enum()->number()); + case FieldDescriptor::CPPTYPE_STRING: { + std::ostringstream os; + string default_str = field->default_value_string(); + + if (field->type() == FieldDescriptor::TYPE_STRING) { + os << "\"" << default_str << "\""; + } else if (field->type() == FieldDescriptor::TYPE_BYTES) { + os << "\""; + + os.fill('0'); + for (int i = 0; i < default_str.length(); ++i) { + // Write the hex form of each byte. + os << "\\x" << std::hex << std::setw(2) + << ((uint16)((unsigned char)default_str.at(i))); + } + os << "\".force_encoding(\"ASCII-8BIT\")"; + } + + return os.str(); + } + default: assert(false); return ""; + } +} +void GenerateField(const FieldDescriptor* field, io::Printer* printer) { if (field->is_map()) { const FieldDescriptor* key_field = field->message_type()->FindFieldByNumber(1); @@ -124,7 +177,7 @@ void GenerateField(const google::protobuf::FieldDescriptor* field, "name", field->name(), "key_type", TypeName(key_field), "value_type", TypeName(value_field), - "number", IntToString(field->number())); + "number", NumberToString(field->number())); if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { printer->Print( @@ -146,24 +199,28 @@ void GenerateField(const google::protobuf::FieldDescriptor* field, printer->Print( ":$type$, $number$", "type", TypeName(field), - "number", IntToString(field->number())); + "number", NumberToString(field->number())); if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { printer->Print( - ", \"$subtype$\"\n", + ", \"$subtype$\"", "subtype", field->message_type()->full_name()); } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) { printer->Print( - ", \"$subtype$\"\n", + ", \"$subtype$\"", "subtype", field->enum_type()->full_name()); - } else { - printer->Print("\n"); } + + if (field->has_default_value()) { + printer->Print(", default: $default$", "default", + DefaultValueForField(field)); + } + + printer->Print("\n"); } } -void GenerateOneof(const google::protobuf::OneofDescriptor* oneof, - google::protobuf::io::Printer* printer) { +void GenerateOneof(const OneofDescriptor* oneof, io::Printer* printer) { printer->Print( "oneof :$name$ do\n", "name", oneof->name()); @@ -178,13 +235,17 @@ void GenerateOneof(const google::protobuf::OneofDescriptor* oneof, printer->Print("end\n"); } -void GenerateMessage(const google::protobuf::Descriptor* message, - google::protobuf::io::Printer* printer) { +bool GenerateMessage(const Descriptor* message, io::Printer* printer, + std::string* error) { + if (message->extension_range_count() > 0 || message->extension_count() > 0) { + *error = "Extensions are not yet supported for proto2 .proto files."; + return false; + } // Don't generate MapEntry messages -- we use the Ruby extension's native // support for map fields instead. if (message->options().map_entry()) { - return; + return true; } printer->Print( @@ -208,15 +269,18 @@ void GenerateMessage(const google::protobuf::Descriptor* message, printer->Print("end\n"); for (int i = 0; i < message->nested_type_count(); i++) { - GenerateMessage(message->nested_type(i), printer); + if (!GenerateMessage(message->nested_type(i), printer, error)) { + return false; + } } for (int i = 0; i < message->enum_type_count(); i++) { GenerateEnum(message->enum_type(i), printer); } + + return true; } -void GenerateEnum(const google::protobuf::EnumDescriptor* en, - google::protobuf::io::Printer* printer) { +void GenerateEnum(const EnumDescriptor* en, io::Printer* printer) { printer->Print( "add_enum \"$name$\" do\n", "name", en->full_name()); @@ -227,7 +291,7 @@ void GenerateEnum(const google::protobuf::EnumDescriptor* en, printer->Print( "value :$name$, $number$\n", "name", value->name(), - "number", IntToString(value->number())); + "number", NumberToString(value->number())); } printer->Outdent(); @@ -242,7 +306,7 @@ bool IsUpper(char ch) { return ch >= 'A' && ch <= 'Z'; } bool IsAlpha(char ch) { return IsLower(ch) || IsUpper(ch); } -char ToUpper(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; } +char UpperChar(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; } // Package names in protobuf are snake_case by convention, but Ruby module @@ -259,7 +323,7 @@ std::string PackageToModule(const std::string& name) { next_upper = true; } else { if (next_upper) { - result.push_back(ToUpper(name[i])); + result.push_back(UpperChar(name[i])); } else { result.push_back(name[i]); } @@ -279,7 +343,7 @@ std::string RubifyConstant(const std::string& name) { if (!ret.empty()) { if (IsLower(ret[0])) { // If it starts with a lowercase letter, capitalize it. - ret[0] = ToUpper(ret[0]); + ret[0] = UpperChar(ret[0]); } else if (!IsAlpha(ret[0])) { // Otherwise (e.g. if it begins with an underscore), we need to come up // with some prefix that starts with a capital letter. We could be smarter @@ -293,11 +357,9 @@ std::string RubifyConstant(const std::string& name) { return ret; } -void GenerateMessageAssignment( - const std::string& prefix, - const google::protobuf::Descriptor* message, - google::protobuf::io::Printer* printer) { - +void GenerateMessageAssignment(const std::string& prefix, + const Descriptor* message, + io::Printer* printer) { // Don't generate MapEntry messages -- we use the Ruby extension's native // support for map fields instead. if (message->options().map_entry()) { @@ -309,11 +371,11 @@ void GenerateMessageAssignment( "prefix", prefix, "name", RubifyConstant(message->name())); printer->Print( - "Google::Protobuf::DescriptorPool.generated_pool." + "::Google::Protobuf::DescriptorPool.generated_pool." "lookup(\"$full_name$\").msgclass\n", "full_name", message->full_name()); - std::string nested_prefix = prefix + message->name() + "::"; + std::string nested_prefix = prefix + RubifyConstant(message->name()) + "::"; for (int i = 0; i < message->nested_type_count(); i++) { GenerateMessageAssignment(nested_prefix, message->nested_type(i), printer); } @@ -322,44 +384,57 @@ void GenerateMessageAssignment( } } -void GenerateEnumAssignment( - const std::string& prefix, - const google::protobuf::EnumDescriptor* en, - google::protobuf::io::Printer* printer) { +void GenerateEnumAssignment(const std::string& prefix, const EnumDescriptor* en, + io::Printer* printer) { printer->Print( "$prefix$$name$ = ", "prefix", prefix, "name", RubifyConstant(en->name())); printer->Print( - "Google::Protobuf::DescriptorPool.generated_pool." + "::Google::Protobuf::DescriptorPool.generated_pool." "lookup(\"$full_name$\").enummodule\n", "full_name", en->full_name()); } -int GeneratePackageModules( - const FileDescriptor* file, - google::protobuf::io::Printer* printer) { +int GeneratePackageModules(const FileDescriptor* file, io::Printer* printer) { int levels = 0; - bool need_change_to_module; + bool need_change_to_module = true; std::string package_name; + // Determine the name to use in either format: + // proto package: one.two.three + // option ruby_package: One::Two::Three if (file->options().has_ruby_package()) { package_name = file->options().ruby_package(); - need_change_to_module = false; + + // If :: is in the package use the Ruby formated name as-is + // -> A::B::C + // otherwise, use the dot seperator + // -> A.B.C + if (package_name.find("::") != std::string::npos) { + need_change_to_module = false; + } else { + GOOGLE_LOG(WARNING) << "ruby_package option should be in the form of:" + << " 'A::B::C' and not 'A.B.C'"; + } } else { package_name = file->package(); - need_change_to_module = true; } + // Use the appropriate delimter + string delimiter = need_change_to_module ? "." : "::"; + int delimiter_size = need_change_to_module ? 1 : 2; + + // Extract each module name and indent while (!package_name.empty()) { - size_t dot_index = package_name.find("."); + size_t dot_index = package_name.find(delimiter); string component; if (dot_index == string::npos) { component = package_name; package_name = ""; } else { component = package_name.substr(0, dot_index); - package_name = package_name.substr(dot_index + 1); + package_name = package_name.substr(dot_index + delimiter_size); } if (need_change_to_module) { component = PackageToModule(component); @@ -373,9 +448,7 @@ int GeneratePackageModules( return levels; } -void EndPackageModules( - int levels, - google::protobuf::io::Printer* printer) { +void EndPackageModules(int levels, io::Printer* printer) { while (levels > 0) { levels--; printer->Outdent(); @@ -423,7 +496,8 @@ bool MaybeEmitDependency(const FileDescriptor* import, const FileDescriptor* from, io::Printer* printer, string* error) { - if (import->syntax() == FileDescriptor::SYNTAX_PROTO2) { + if (from->syntax() == FileDescriptor::SYNTAX_PROTO3 && + import->syntax() == FileDescriptor::SYNTAX_PROTO2) { for (int i = 0; i < from->message_type_count(); i++) { if (UsesTypeFromFile(from->message_type(i), import, error)) { // Error text was already set by UsesTypeFromFile(). @@ -462,16 +536,29 @@ bool GenerateFile(const FileDescriptor* file, io::Printer* printer, } } - printer->Print( - "Google::Protobuf::DescriptorPool.generated_pool.build do\n"); + // TODO: Remove this when ruby supports extensions for proto2 syntax. + if (file->extension_count() > 0) { + *error = "Extensions are not yet supported for proto2 .proto files."; + return false; + } + + printer->Print("Google::Protobuf::DescriptorPool.generated_pool.build do\n"); + printer->Indent(); + printer->Print("add_file(\"$filename$\", :syntax => :$syntax$) do\n", + "filename", file->name(), "syntax", + StringifySyntax(file->syntax())); printer->Indent(); for (int i = 0; i < file->message_type_count(); i++) { - GenerateMessage(file->message_type(i), printer); + if (!GenerateMessage(file->message_type(i), printer, error)) { + return false; + } } for (int i = 0; i < file->enum_type_count(); i++) { GenerateEnum(file->enum_type(i), printer); } printer->Outdent(); + printer->Print("end\n"); + printer->Outdent(); printer->Print( "end\n\n"); @@ -492,10 +579,9 @@ bool Generator::Generate( GeneratorContext* generator_context, string* error) const { - if (file->syntax() != FileDescriptor::SYNTAX_PROTO3) { - *error = - "Can only generate Ruby code for proto3 .proto files.\n" - "Please add 'syntax = \"proto3\";' to the top of your .proto file.\n"; + if (file->syntax() != FileDescriptor::SYNTAX_PROTO3 && + file->syntax() != FileDescriptor::SYNTAX_PROTO2) { + *error = "Invalid or unsupported proto syntax"; return false; } diff --git a/src/google/protobuf/compiler/ruby/ruby_generator.h b/src/google/protobuf/compiler/ruby/ruby_generator.h index 8c1dfa2671..731a81a52d 100644 --- a/src/google/protobuf/compiler/ruby/ruby_generator.h +++ b/src/google/protobuf/compiler/ruby/ruby_generator.h @@ -37,6 +37,8 @@ #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -46,8 +48,7 @@ namespace ruby { // If you create your own protocol compiler binary and you want it to support // Ruby output, you can do so by registering an instance of this // CodeGenerator with the CommandLineInterface in your main() function. -class LIBPROTOC_EXPORT Generator - : public google::protobuf::compiler::CodeGenerator { +class PROTOC_EXPORT Generator : public CodeGenerator { virtual bool Generate( const FileDescriptor* file, const string& parameter, @@ -60,5 +61,7 @@ class LIBPROTOC_EXPORT Generator } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_RUBY_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc b/src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc index 8454a5c57f..d93a68d9af 100644 --- a/src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc +++ b/src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc @@ -29,6 +29,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include #include #include @@ -56,7 +57,7 @@ string FindRubyTestDir() { // Some day, we may integrate build systems between protoc and the language // extensions to the point where we can do this test in a more automated way. -TEST(RubyGeneratorTest, GeneratorTest) { +void RubyTest(string proto_file) { string ruby_tests = FindRubyTestDir(); google::protobuf::compiler::CommandLineInterface cli; @@ -68,22 +69,23 @@ TEST(RubyGeneratorTest, GeneratorTest) { // Copy generated_code.proto to the temporary test directory. string test_input; GOOGLE_CHECK_OK(File::GetContents( - ruby_tests + "/ruby_generated_code.proto", + ruby_tests + proto_file + ".proto", &test_input, true)); GOOGLE_CHECK_OK(File::SetContents( - TestTempDir() + "/ruby_generated_code.proto", + TestTempDir() + proto_file + ".proto", test_input, true)); // Invoke the proto compiler (we will be inside TestTempDir() at this point). string ruby_out = "--ruby_out=" + TestTempDir(); string proto_path = "--proto_path=" + TestTempDir(); + string proto_target = TestTempDir() + proto_file + ".proto"; const char* argv[] = { "protoc", ruby_out.c_str(), proto_path.c_str(), - "ruby_generated_code.proto", + proto_target.c_str(), }; EXPECT_EQ(0, cli.Run(4, argv)); @@ -91,17 +93,37 @@ TEST(RubyGeneratorTest, GeneratorTest) { // Load the generated output and compare to the expected result. string output; GOOGLE_CHECK_OK(File::GetContentsAsText( - TestTempDir() + "/ruby_generated_code_pb.rb", + TestTempDir() + proto_file + "_pb.rb", &output, true)); string expected_output; GOOGLE_CHECK_OK(File::GetContentsAsText( - ruby_tests + "/ruby_generated_code_pb.rb", + ruby_tests + proto_file + "_pb.rb", &expected_output, true)); EXPECT_EQ(expected_output, output); } +TEST(RubyGeneratorTest, Proto3GeneratorTest) { + RubyTest("/ruby_generated_code"); +} + +TEST(RubyGeneratorTest, Proto2GeneratorTest) { + RubyTest("/ruby_generated_code_proto2"); +} + +TEST(RubyGeneratorTest, Proto3ImplicitPackageTest) { + RubyTest("/ruby_generated_pkg_implicit"); +} + +TEST(RubyGeneratorTest, Proto3ExplictPackageTest) { + RubyTest("/ruby_generated_pkg_explicit"); +} + +TEST(RubyGeneratorTest, Proto3ExplictLegacyPackageTest) { + RubyTest("/ruby_generated_pkg_explicit_legacy"); +} + } // namespace } // namespace ruby } // namespace compiler diff --git a/src/google/protobuf/compiler/scc.cc b/src/google/protobuf/compiler/scc.cc deleted file mode 100644 index bbdabbfc11..0000000000 --- a/src/google/protobuf/compiler/scc.cc +++ /dev/null @@ -1,111 +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 compiler { - -SCCAnalyzer::NodeData SCCAnalyzer::DFS(const Descriptor* descriptor) { - // Must not have visited already. - GOOGLE_DCHECK_EQ(cache_.count(descriptor), 0); - - // Mark visited by inserting in map. - NodeData& result = cache_[descriptor]; - // Initialize data structures. - result.index = result.lowlink = index_++; - stack_.push_back(descriptor); - - // Recurse the fields / nodes in graph - for (int i = 0; i < descriptor->field_count(); i++) { - const Descriptor* child = descriptor->field(i)->message_type(); - if (child) { - if (cache_.count(child) == 0) { - // unexplored node - NodeData child_data = DFS(child); - result.lowlink = std::min(result.lowlink, child_data.lowlink); - } else { - NodeData child_data = cache_[child]; - if (child_data.scc == nullptr) { - // Still in the stack_ so we found a back edge - result.lowlink = std::min(result.lowlink, child_data.index); - } - } - } - } - if (result.index == result.lowlink) { - // This is the root of a strongly connected component - SCC* scc = CreateSCC(); - while (true) { - const Descriptor* scc_desc = stack_.back(); - scc->descriptors.push_back(scc_desc); - // Remove from stack - stack_.pop_back(); - cache_[scc_desc].scc = scc; - - if (scc_desc == descriptor) break; - } - - // The order of descriptors is random and depends how this SCC was - // discovered. In-order to ensure maximum stability we sort it by name. - std::sort(scc->descriptors.begin(), scc->descriptors.end(), - [](const Descriptor* a, const Descriptor* b) { - return a->full_name() < b->full_name(); - }); - AddChildren(scc); - } - return result; -} - -void SCCAnalyzer::AddChildren(SCC* scc) { - std::set seen; - for (int i = 0; i < scc->descriptors.size(); i++) { - const Descriptor* descriptor = scc->descriptors[i]; - for (int j = 0; j < descriptor->field_count(); j++) { - const Descriptor* child_msg = descriptor->field(j)->message_type(); - if (child_msg) { - const SCC* child = GetSCC(child_msg); - if (child == scc) continue; - if (seen.insert(child).second) { - scc->children.push_back(child); - } - } - } - } -} - -} // namespace compiler -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/compiler/scc.h b/src/google/protobuf/compiler/scc.h index 69a47f1bc1..a1394602f7 100644 --- a/src/google/protobuf/compiler/scc.h +++ b/src/google/protobuf/compiler/scc.h @@ -33,8 +33,12 @@ #include +#include +#include #include +#include + namespace google { namespace protobuf { namespace compiler { @@ -47,12 +51,16 @@ struct SCC { std::vector children; const Descriptor* GetRepresentative() const { return descriptors[0]; } + + // All messages must necessarily be in the same file. + const FileDescriptor* GetFile() const { return descriptors[0]->file(); } }; // This class is used for analyzing the SCC for each message, to ensure linear // instead of quadratic performance, if we do this per message we would get // O(V*(V+E)). -class LIBPROTOC_EXPORT SCCAnalyzer { +template +class PROTOC_EXPORT SCCAnalyzer { public: explicit SCCAnalyzer() : index_(0) {} @@ -79,10 +87,69 @@ class LIBPROTOC_EXPORT SCCAnalyzer { } // Tarjan's Strongly Connected Components algo - NodeData DFS(const Descriptor* descriptor); + NodeData DFS(const Descriptor* descriptor) { + // Must not have visited already. + GOOGLE_DCHECK_EQ(cache_.count(descriptor), 0); + + // Mark visited by inserting in map. + NodeData& result = cache_[descriptor]; + // Initialize data structures. + result.index = result.lowlink = index_++; + stack_.push_back(descriptor); + + // Recurse the fields / nodes in graph + for (auto dep : DepsGenerator()(descriptor)) { + GOOGLE_CHECK(dep); + if (cache_.count(dep) == 0) { + // unexplored node + NodeData child_data = DFS(dep); + result.lowlink = std::min(result.lowlink, child_data.lowlink); + } else { + NodeData child_data = cache_[dep]; + if (child_data.scc == nullptr) { + // Still in the stack_ so we found a back edge + result.lowlink = std::min(result.lowlink, child_data.index); + } + } + } + if (result.index == result.lowlink) { + // This is the root of a strongly connected component + SCC* scc = CreateSCC(); + while (true) { + const Descriptor* scc_desc = stack_.back(); + scc->descriptors.push_back(scc_desc); + // Remove from stack + stack_.pop_back(); + cache_[scc_desc].scc = scc; + + if (scc_desc == descriptor) break; + } + + // The order of descriptors is random and depends how this SCC was + // discovered. In-order to ensure maximum stability we sort it by name. + std::sort(scc->descriptors.begin(), scc->descriptors.end(), + [](const Descriptor* a, const Descriptor* b) { + return a->full_name() < b->full_name(); + }); + AddChildren(scc); + } + return result; + } // Add the SCC's that are children of this SCC to its children. - void AddChildren(SCC* scc); + void AddChildren(SCC* scc) { + std::set seen; + for (auto descriptor : scc->descriptors) { + for (auto child_msg : DepsGenerator()(descriptor)) { + GOOGLE_CHECK(child_msg); + const SCC* child = GetSCC(child_msg); + if (child == scc) continue; + if (seen.insert(child).second) { + scc->children.push_back(child); + } + } + } + } // This is necessary for compiler bug in msvc2015. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SCCAnalyzer); @@ -92,4 +159,6 @@ class LIBPROTOC_EXPORT SCCAnalyzer { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_SCC_H__ diff --git a/src/google/protobuf/compiler/subprocess.cc b/src/google/protobuf/compiler/subprocess.cc index 2e5a89ac49..bd6be2b09b 100644 --- a/src/google/protobuf/compiler/subprocess.cc +++ b/src/google/protobuf/compiler/subprocess.cc @@ -38,9 +38,9 @@ #ifndef _WIN32 #include +#include #include #include -#include #endif #include @@ -54,7 +54,7 @@ namespace compiler { namespace { char* portable_strdup(const char* s) { - char* ns = (char*) malloc(strlen(s) + 1); + char* ns = (char*)malloc(strlen(s) + 1); if (ns != NULL) { strcpy(ns, s); } @@ -73,7 +73,9 @@ static void CloseHandleOrDie(HANDLE handle) { Subprocess::Subprocess() : process_start_error_(ERROR_SUCCESS), - child_handle_(NULL), child_stdin_(NULL), child_stdout_(NULL) {} + child_handle_(NULL), + child_stdin_(NULL), + child_stdout_(NULL) {} Subprocess::~Subprocess() { if (child_stdin_ != NULL) { @@ -84,7 +86,7 @@ Subprocess::~Subprocess() { } } -void Subprocess::Start(const string& program, SearchMode search_mode) { +void Subprocess::Start(const std::string& program, SearchMode search_mode) { // Create the pipes. HANDLE stdin_pipe_read; HANDLE stdin_pipe_write; @@ -99,13 +101,13 @@ void Subprocess::Start(const string& program, SearchMode search_mode) { } // Make child side of the pipes inheritable. - if (!SetHandleInformation(stdin_pipe_read, - HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { + if (!SetHandleInformation(stdin_pipe_read, HANDLE_FLAG_INHERIT, + HANDLE_FLAG_INHERIT)) { GOOGLE_LOG(FATAL) << "SetHandleInformation: " << Win32ErrorMessage(GetLastError()); } - if (!SetHandleInformation(stdout_pipe_write, - HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { + if (!SetHandleInformation(stdout_pipe_write, HANDLE_FLAG_INHERIT, + HANDLE_FLAG_INHERIT)) { GOOGLE_LOG(FATAL) << "SetHandleInformation: " << Win32ErrorMessage(GetLastError()); } @@ -120,26 +122,27 @@ void Subprocess::Start(const string& program, SearchMode search_mode) { startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); if (startup_info.hStdError == INVALID_HANDLE_VALUE) { - GOOGLE_LOG(FATAL) << "GetStdHandle: " - << Win32ErrorMessage(GetLastError()); + GOOGLE_LOG(FATAL) << "GetStdHandle: " << Win32ErrorMessage(GetLastError()); } - // CreateProcess() mutates its second parameter. WTF? - char* name_copy = portable_strdup(program.c_str()); + // Invoking cmd.exe allows for '.bat' files from the path as well as '.exe'. + // Using a malloc'ed string because CreateProcess() can mutate its second + // parameter. + char* command_line = + portable_strdup(("cmd.exe /c \"" + program + "\"").c_str()); // Create the process. PROCESS_INFORMATION process_info; if (CreateProcessA((search_mode == SEARCH_PATH) ? NULL : program.c_str(), - (search_mode == SEARCH_PATH) ? name_copy : NULL, + (search_mode == SEARCH_PATH) ? command_line : NULL, NULL, // process security attributes NULL, // thread security attributes TRUE, // inherit handles? 0, // obscure creation flags NULL, // environment (inherit from parent) NULL, // current directory (inherit from parent) - &startup_info, - &process_info)) { + &startup_info, &process_info)) { child_handle_ = process_info.hProcess; CloseHandleOrDie(process_info.hThread); child_stdin_ = stdin_pipe_write; @@ -152,11 +155,11 @@ void Subprocess::Start(const string& program, SearchMode search_mode) { CloseHandleOrDie(stdin_pipe_read); CloseHandleOrDie(stdout_pipe_write); - free(name_copy); + free(command_line); } bool Subprocess::Communicate(const Message& input, Message* output, - string* error) { + std::string* error) { if (process_start_error_ != ERROR_SUCCESS) { *error = Win32ErrorMessage(process_start_error_); return false; @@ -164,8 +167,8 @@ bool Subprocess::Communicate(const Message& input, Message* output, GOOGLE_CHECK(child_handle_ != NULL) << "Must call Start() first."; - string input_data = input.SerializeAsString(); - string output_data; + std::string input_data = input.SerializeAsString(); + std::string output_data; int input_pos = 0; @@ -197,10 +200,8 @@ bool Subprocess::Communicate(const Message& input, Message* output, if (signaled_handle == child_stdin_) { DWORD n; - if (!WriteFile(child_stdin_, - input_data.data() + input_pos, - input_data.size() - input_pos, - &n, NULL)) { + if (!WriteFile(child_stdin_, input_data.data() + input_pos, + input_data.size() - input_pos, &n, NULL)) { // Child closed pipe. Presumably it will report an error later. // Pretend we're done for now. input_pos = input_data.size(); @@ -254,8 +255,8 @@ bool Subprocess::Communicate(const Message& input, Message* output, child_handle_ = NULL; if (exit_code != 0) { - *error = strings::Substitute( - "Plugin failed with status code $0.", exit_code); + *error = + strings::Substitute("Plugin failed with status code $0.", exit_code); return false; } @@ -267,7 +268,7 @@ bool Subprocess::Communicate(const Message& input, Message* output, return true; } -string Subprocess::Win32ErrorMessage(DWORD error_code) { +std::string Subprocess::Win32ErrorMessage(DWORD error_code) { char* message; // WTF? @@ -277,7 +278,7 @@ string Subprocess::Win32ErrorMessage(DWORD error_code) { (LPSTR)&message, // NOT A BUG! 0, NULL); - string result = message; + std::string result = message; LocalFree(message); return result; } @@ -298,7 +299,7 @@ Subprocess::~Subprocess() { } } -void Subprocess::Start(const string& program, SearchMode search_mode) { +void Subprocess::Start(const std::string& program, SearchMode search_mode) { // Note that we assume that there are no other threads, thus we don't have to // do crazy stuff like using socket pairs or avoiding libc locks. @@ -309,7 +310,7 @@ void Subprocess::Start(const string& program, SearchMode search_mode) { GOOGLE_CHECK(pipe(stdin_pipe) != -1); GOOGLE_CHECK(pipe(stdout_pipe) != -1); - char* argv[2] = { portable_strdup(program.c_str()), NULL }; + char* argv[2] = {portable_strdup(program.c_str()), NULL}; child_pid_ = fork(); if (child_pid_ == -1) { @@ -337,9 +338,12 @@ void Subprocess::Start(const string& program, SearchMode search_mode) { // stuff that is unsafe here. int ignored; ignored = write(STDERR_FILENO, argv[0], strlen(argv[0])); - const char* message = ": program not found or is not executable\n"; + const char* message = + ": program not found or is not executable\n" + "Please specify a program using absolute path or make sure " + "the program is available in your PATH system variable\n"; ignored = write(STDERR_FILENO, message, strlen(message)); - (void) ignored; + (void)ignored; // Must use _exit() rather than exit() to avoid flushing output buffers // that will also be flushed by the parent. @@ -356,7 +360,7 @@ void Subprocess::Start(const string& program, SearchMode search_mode) { } bool Subprocess::Communicate(const Message& input, Message* output, - string* error) { + std::string* error) { GOOGLE_CHECK_NE(child_stdin_, -1) << "Must call Start() first."; // The "sighandler_t" typedef is GNU-specific, so define our own. @@ -365,8 +369,8 @@ bool Subprocess::Communicate(const Message& input, Message* output, // Make sure SIGPIPE is disabled so that if the child dies it doesn't kill us. SignalHandler* old_pipe_handler = signal(SIGPIPE, SIG_IGN); - string input_data = input.SerializeAsString(); - string output_data; + std::string input_data = input.SerializeAsString(); + std::string output_data; int input_pos = 0; int max_fd = std::max(child_stdin_, child_stdout_); @@ -394,7 +398,7 @@ bool Subprocess::Communicate(const Message& input, Message* output, if (child_stdin_ != -1 && FD_ISSET(child_stdin_, &write_fds)) { int n = write(child_stdin_, input_data.data() + input_pos, - input_data.size() - input_pos); + input_data.size() - input_pos); if (n < 0) { // Child closed pipe. Presumably it will report an error later. // Pretend we're done for now. @@ -444,14 +448,13 @@ bool Subprocess::Communicate(const Message& input, Message* output, if (WIFEXITED(status)) { if (WEXITSTATUS(status) != 0) { int error_code = WEXITSTATUS(status); - *error = strings::Substitute( - "Plugin failed with status code $0.", error_code); + *error = + strings::Substitute("Plugin failed with status code $0.", error_code); return false; } } else if (WIFSIGNALED(status)) { int signal = WTERMSIG(status); - *error = strings::Substitute( - "Plugin killed by signal $0.", signal); + *error = strings::Substitute("Plugin killed by signal $0.", signal); return false; } else { *error = "Neither WEXITSTATUS nor WTERMSIG is true?"; diff --git a/src/google/protobuf/compiler/subprocess.h b/src/google/protobuf/compiler/subprocess.h index dad5daba20..9c4f8b7690 100644 --- a/src/google/protobuf/compiler/subprocess.h +++ b/src/google/protobuf/compiler/subprocess.h @@ -34,7 +34,7 @@ #define GOOGLE_PROTOBUF_COMPILER_SUBPROCESS_H__ #ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN // right... +#define WIN32_LEAN_AND_MEAN // right... #include #else // _WIN32 #include @@ -44,6 +44,8 @@ #include +#include + namespace google { namespace protobuf { @@ -52,31 +54,31 @@ class Message; namespace compiler { // Utility class for launching sub-processes. -class LIBPROTOC_EXPORT Subprocess { +class PROTOC_EXPORT Subprocess { public: Subprocess(); ~Subprocess(); enum SearchMode { - SEARCH_PATH, // Use PATH environment variable. - EXACT_NAME // Program is an exact file name; don't use the PATH. + SEARCH_PATH, // Use PATH environment variable. + EXACT_NAME // Program is an exact file name; don't use the PATH. }; // Start the subprocess. Currently we don't provide a way to specify // arguments as protoc plugins don't have any. - void Start(const string& program, SearchMode search_mode); + void Start(const std::string& program, SearchMode search_mode); // Serialize the input message and pipe it to the subprocess's stdin, then // close the pipe. Meanwhile, read from the subprocess's stdout and parse // the data into *output. All this is done carefully to avoid deadlocks. // Returns true if successful. On any sort of error, returns false and sets // *error to a description of the problem. - bool Communicate(const Message& input, Message* output, string* error); + bool Communicate(const Message& input, Message* output, std::string* error); #ifdef _WIN32 // Given an error code, returns a human-readable error message. This is // defined here so that CommandLineInterface can share it. - static string Win32ErrorMessage(DWORD error_code); + static std::string Win32ErrorMessage(DWORD error_code); #endif private: @@ -104,4 +106,6 @@ class LIBPROTOC_EXPORT Subprocess { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMPILER_SUBPROCESS_H__ diff --git a/src/google/protobuf/compiler/test_plugin.cc b/src/google/protobuf/compiler/test_plugin.cc index 4b758764ea..6b56170003 100644 --- a/src/google/protobuf/compiler/test_plugin.cc +++ b/src/google/protobuf/compiler/test_plugin.cc @@ -33,10 +33,10 @@ // This is a dummy code generator plugin used by // command_line_interface_unittest. -#include #include -#include +#include #include +#include namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/zip_writer.cc b/src/google/protobuf/compiler/zip_writer.cc index 1799af6a4a..872dd9edd6 100644 --- a/src/google/protobuf/compiler/zip_writer.cc +++ b/src/google/protobuf/compiler/zip_writer.cc @@ -75,52 +75,51 @@ namespace compiler { static const uint16 kDosEpoch = 1 << 5 | 1; static const uint32 kCRC32Table[256] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, - 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, - 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, - 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, - 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, - 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, - 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, - 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, - 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, - 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, - 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, - 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, - 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, - 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, - 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, - 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, - 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, - 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d -}; - -static uint32 ComputeCRC32(const string &buf) { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; + +static uint32 ComputeCRC32(const std::string& buf) { uint32 x = ~0U; for (int i = 0; i < buf.size(); ++i) { unsigned char c = buf[i]; @@ -129,7 +128,7 @@ static uint32 ComputeCRC32(const string &buf) { return ~x; } -static void WriteShort(io::CodedOutputStream *out, uint16 val) { +static void WriteShort(io::CodedOutputStream* out, uint16 val) { uint8 p[2]; p[0] = static_cast(val); p[1] = static_cast(val >> 8); @@ -137,10 +136,11 @@ static void WriteShort(io::CodedOutputStream *out, uint16 val) { } ZipWriter::ZipWriter(io::ZeroCopyOutputStream* raw_output) - : raw_output_(raw_output) {} + : raw_output_(raw_output) {} ZipWriter::~ZipWriter() {} -bool ZipWriter::Write(const string& filename, const string& contents) { +bool ZipWriter::Write(const std::string& filename, + const std::string& contents) { FileInfo info; info.name = filename; @@ -154,18 +154,18 @@ bool ZipWriter::Write(const string& filename, const string& contents) { // write file header io::CodedOutputStream output(raw_output_); output.WriteLittleEndian32(0x04034b50); // magic - WriteShort(&output, 10); // version needed to extract - WriteShort(&output, 0); // flags - WriteShort(&output, 0); // compression method: stored - WriteShort(&output, 0); // last modified time - WriteShort(&output, kDosEpoch); // last modified date + WriteShort(&output, 10); // version needed to extract + WriteShort(&output, 0); // flags + WriteShort(&output, 0); // compression method: stored + WriteShort(&output, 0); // last modified time + WriteShort(&output, kDosEpoch); // last modified date output.WriteLittleEndian32(info.crc32); // crc-32 - output.WriteLittleEndian32(info.size); // compressed size - output.WriteLittleEndian32(info.size); // uncompressed size - WriteShort(&output, filename_size); // file name length - WriteShort(&output, 0); // extra field length - output.WriteString(filename); // file name - output.WriteString(contents); // file data + output.WriteLittleEndian32(info.size); // compressed size + output.WriteLittleEndian32(info.size); // uncompressed size + WriteShort(&output, filename_size); // file name length + WriteShort(&output, 0); // extra field length + output.WriteString(filename); // file name + output.WriteString(contents); // file data return !output.HadError(); } @@ -177,42 +177,42 @@ bool ZipWriter::WriteDirectory() { // write central directory io::CodedOutputStream output(raw_output_); for (int i = 0; i < num_entries; ++i) { - const string &filename = files_[i].name; + const std::string& filename = files_[i].name; uint16 filename_size = filename.size(); uint32 crc32 = files_[i].crc32; uint32 size = files_[i].size; uint32 offset = files_[i].offset; output.WriteLittleEndian32(0x02014b50); // magic - WriteShort(&output, 10); // version made by - WriteShort(&output, 10); // version needed to extract - WriteShort(&output, 0); // flags - WriteShort(&output, 0); // compression method: stored - WriteShort(&output, 0); // last modified time - WriteShort(&output, kDosEpoch); // last modified date - output.WriteLittleEndian32(crc32); // crc-32 - output.WriteLittleEndian32(size); // compressed size - output.WriteLittleEndian32(size); // uncompressed size - WriteShort(&output, filename_size); // file name length - WriteShort(&output, 0); // extra field length - WriteShort(&output, 0); // file comment length - WriteShort(&output, 0); // starting disk number - WriteShort(&output, 0); // internal file attributes - output.WriteLittleEndian32(0); // external file attributes - output.WriteLittleEndian32(offset); // local header offset - output.WriteString(filename); // file name + WriteShort(&output, 10); // version made by + WriteShort(&output, 10); // version needed to extract + WriteShort(&output, 0); // flags + WriteShort(&output, 0); // compression method: stored + WriteShort(&output, 0); // last modified time + WriteShort(&output, kDosEpoch); // last modified date + output.WriteLittleEndian32(crc32); // crc-32 + output.WriteLittleEndian32(size); // compressed size + output.WriteLittleEndian32(size); // uncompressed size + WriteShort(&output, filename_size); // file name length + WriteShort(&output, 0); // extra field length + WriteShort(&output, 0); // file comment length + WriteShort(&output, 0); // starting disk number + WriteShort(&output, 0); // internal file attributes + output.WriteLittleEndian32(0); // external file attributes + output.WriteLittleEndian32(offset); // local header offset + output.WriteString(filename); // file name } uint32 dir_len = output.ByteCount(); // write end of central directory marker output.WriteLittleEndian32(0x06054b50); // magic - WriteShort(&output, 0); // disk number - WriteShort(&output, 0); // disk with start of central directory - WriteShort(&output, num_entries); // central directory entries (this disk) - WriteShort(&output, num_entries); // central directory entries (total) + WriteShort(&output, 0); // disk number + WriteShort(&output, 0); // disk with start of central directory + WriteShort(&output, num_entries); // central directory entries (this disk) + WriteShort(&output, num_entries); // central directory entries (total) output.WriteLittleEndian32(dir_len); // central directory byte size output.WriteLittleEndian32(dir_ofs); // central directory offset - WriteShort(&output, 0); // comment length + WriteShort(&output, 0); // comment length return output.HadError(); } diff --git a/src/google/protobuf/compiler/zip_writer.h b/src/google/protobuf/compiler/zip_writer.h index 03db4d573c..a99bb78ce7 100644 --- a/src/google/protobuf/compiler/zip_writer.h +++ b/src/google/protobuf/compiler/zip_writer.h @@ -73,12 +73,12 @@ class ZipWriter { ZipWriter(io::ZeroCopyOutputStream* raw_output); ~ZipWriter(); - bool Write(const string& filename, const string& contents); + bool Write(const std::string& filename, const std::string& contents); bool WriteDirectory(); private: struct FileInfo { - string name; + std::string name; uint32 offset; uint32 size; uint32 crc32; diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index 5486ab92c3..5bc64f1fda 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -63,6 +63,7 @@ #include + #include #include #include @@ -70,6 +71,8 @@ #undef PACKAGE // autoheader #defines this. :( +#include + namespace google { namespace protobuf { @@ -97,7 +100,7 @@ struct Symbol { const FileDescriptor* package_file_descriptor; }; - inline Symbol() : type(NULL_SYMBOL) { descriptor = NULL; } + inline Symbol() : type(NULL_SYMBOL) { descriptor = nullptr; } inline bool IsNull() const { return type == NULL_SYMBOL; } inline bool IsType() const { return type == MESSAGE || type == ENUM; } inline bool IsAggregate() const { @@ -124,7 +127,7 @@ struct Symbol { const FileDescriptor* GetFile() const { switch (type) { case NULL_SYMBOL: - return NULL; + return nullptr; case MESSAGE: return descriptor->file(); case FIELD: @@ -142,78 +145,78 @@ struct Symbol { case PACKAGE: return package_file_descriptor; } - return NULL; + return nullptr; } }; const FieldDescriptor::CppType -FieldDescriptor::kTypeToCppTypeMap[MAX_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 + FieldDescriptor::kTypeToCppTypeMap[MAX_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 char * const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = { - "ERROR", // 0 is reserved for errors - - "double", // TYPE_DOUBLE - "float", // TYPE_FLOAT - "int64", // TYPE_INT64 - "uint64", // TYPE_UINT64 - "int32", // TYPE_INT32 - "fixed64", // TYPE_FIXED64 - "fixed32", // TYPE_FIXED32 - "bool", // TYPE_BOOL - "string", // TYPE_STRING - "group", // TYPE_GROUP - "message", // TYPE_MESSAGE - "bytes", // TYPE_BYTES - "uint32", // TYPE_UINT32 - "enum", // TYPE_ENUM - "sfixed32", // TYPE_SFIXED32 - "sfixed64", // TYPE_SFIXED64 - "sint32", // TYPE_SINT32 - "sint64", // TYPE_SINT64 +const char* const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = { + "ERROR", // 0 is reserved for errors + + "double", // TYPE_DOUBLE + "float", // TYPE_FLOAT + "int64", // TYPE_INT64 + "uint64", // TYPE_UINT64 + "int32", // TYPE_INT32 + "fixed64", // TYPE_FIXED64 + "fixed32", // TYPE_FIXED32 + "bool", // TYPE_BOOL + "string", // TYPE_STRING + "group", // TYPE_GROUP + "message", // TYPE_MESSAGE + "bytes", // TYPE_BYTES + "uint32", // TYPE_UINT32 + "enum", // TYPE_ENUM + "sfixed32", // TYPE_SFIXED32 + "sfixed64", // TYPE_SFIXED64 + "sint32", // TYPE_SINT32 + "sint64", // TYPE_SINT64 }; -const char * const FieldDescriptor::kCppTypeToName[MAX_CPPTYPE + 1] = { - "ERROR", // 0 is reserved for errors - - "int32", // CPPTYPE_INT32 - "int64", // CPPTYPE_INT64 - "uint32", // CPPTYPE_UINT32 - "uint64", // CPPTYPE_UINT64 - "double", // CPPTYPE_DOUBLE - "float", // CPPTYPE_FLOAT - "bool", // CPPTYPE_BOOL - "enum", // CPPTYPE_ENUM - "string", // CPPTYPE_STRING - "message", // CPPTYPE_MESSAGE +const char* const FieldDescriptor::kCppTypeToName[MAX_CPPTYPE + 1] = { + "ERROR", // 0 is reserved for errors + + "int32", // CPPTYPE_INT32 + "int64", // CPPTYPE_INT64 + "uint32", // CPPTYPE_UINT32 + "uint64", // CPPTYPE_UINT64 + "double", // CPPTYPE_DOUBLE + "float", // CPPTYPE_FLOAT + "bool", // CPPTYPE_BOOL + "enum", // CPPTYPE_ENUM + "string", // CPPTYPE_STRING + "message", // CPPTYPE_MESSAGE }; -const char * const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = { - "ERROR", // 0 is reserved for errors +const char* const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = { + "ERROR", // 0 is reserved for errors - "optional", // LABEL_OPTIONAL - "required", // LABEL_REQUIRED - "repeated", // LABEL_REPEATED + "optional", // LABEL_OPTIONAL + "required", // LABEL_REQUIRED + "repeated", // LABEL_REPEATED }; const char* FileDescriptor::SyntaxName(FileDescriptor::Syntax syntax) { @@ -226,10 +229,10 @@ const char* FileDescriptor::SyntaxName(FileDescriptor::Syntax syntax) { return "unknown"; } GOOGLE_LOG(FATAL) << "can't reach here."; - return NULL; + return nullptr; } -static const char * const kNonLinkedWeakMessageReplacementName = "google.protobuf.Empty"; +static const char* const kNonLinkedWeakMessageReplacementName = "google.protobuf.Empty"; #if !defined(_MSC_VER) || _MSC_VER >= 1900 const int FieldDescriptor::kMaxNumber; @@ -248,9 +251,9 @@ char ToLower(char ch) { return (ch >= 'A' && ch <= 'Z') ? (ch - 'A' + 'a') : ch; } -string ToCamelCase(const string& input, bool lower_first) { +std::string ToCamelCase(const std::string& input, bool lower_first) { bool capitalize_next = !lower_first; - string result; + std::string result; result.reserve(input.size()); for (int i = 0; i < input.size(); i++) { @@ -272,9 +275,9 @@ string ToCamelCase(const string& input, bool lower_first) { return result; } -string ToJsonName(const string& input) { +std::string ToJsonName(const std::string& input) { bool capitalize_next = false; - string result; + std::string result; result.reserve(input.size()); for (int i = 0; i < input.size(); i++) { @@ -291,9 +294,9 @@ string ToJsonName(const string& input) { return result; } -string EnumValueToPascalCase(const string& input) { +std::string EnumValueToPascalCase(const std::string& input) { bool next_upper = true; - string result; + std::string result; result.reserve(input.size()); for (int i = 0; i < input.size(); i++) { @@ -326,7 +329,7 @@ class PrefixRemover { // Tries to remove the enum prefix from this enum value. // If this is not possible, returns the input verbatim. - string MaybeRemove(StringPiece str) { + std::string MaybeRemove(StringPiece str) { // We can't just lowercase and strip str and look for a prefix. // We need to properly recognize the difference between: // @@ -346,14 +349,14 @@ class PrefixRemover { } if (ascii_tolower(str[i]) != prefix_[j++]) { - return string(str); + return std::string(str); } } // If we didn't make it through the prefix, we've failed to strip the // prefix. if (j < prefix_.size()) { - return string(str); + return std::string(str); } // Skip underscores between prefix and further characters. @@ -363,16 +366,16 @@ class PrefixRemover { // Enum label can't be the empty string. if (i == str.size()) { - return string(str); + return std::string(str); } // We successfully stripped the prefix. str.remove_prefix(i); - return string(str); + return std::string(str); } private: - string prefix_; + std::string prefix_; }; // A DescriptorPool contains a bunch of hash-maps to implement the @@ -403,7 +406,7 @@ typedef std::pair EnumIntPair; #define HASH_SET std::unordered_set #define HASH_FXN hash -template +template struct PointerIntegerPairHash { size_t operator()(const PairType& p) const { static const size_t prime1 = 16777499; @@ -418,8 +421,7 @@ struct PointerIntegerPairHash { static const size_t min_buckets = 8; #endif inline bool operator()(const PairType& a, const PairType& b) const { - return a.first < b.first || - (a.first == b.first && a.second < b.second); + return a.first < b.first || (a.first == b.first && a.second < b.second); } }; @@ -464,35 +466,37 @@ typedef HASH_MAP, - std::equal_to > + std::equal_to> FieldsByNumberMap; typedef HASH_MAP, - std::equal_to > + std::equal_to> EnumValuesByNumberMap; // This is a map rather than a hash-map, since we use it to iterate // through all the extensions that extend a given Descriptor, and an // ordered data structure that implements lower_bound is convenient // for that. typedef std::map - ExtensionsGroupedByDescriptorMap; -typedef HASH_MAP LocationsByPathMap; + ExtensionsGroupedByDescriptorMap; +typedef HASH_MAP + LocationsByPathMap; -std::set* NewAllowedProto3Extendee() { - auto allowed_proto3_extendees = new std::set; +std::set* NewAllowedProto3Extendee() { + auto allowed_proto3_extendees = new std::set; const char* kOptionNames[] = { - "FileOptions", "MessageOptions", "FieldOptions", "EnumOptions", + "FileOptions", "MessageOptions", "FieldOptions", "EnumOptions", "EnumValueOptions", "ServiceOptions", "MethodOptions", "OneofOptions"}; for (int i = 0; i < GOOGLE_ARRAYSIZE(kOptionNames); ++i) { // descriptor.proto has a different package name in opensource. We allow // both so the opensource protocol compiler can also compile internal // proto3 files with custom options. See: b/27567912 - allowed_proto3_extendees->insert(string("google.protobuf.") + + allowed_proto3_extendees->insert(std::string("google.protobuf.") + kOptionNames[i]); // Split the word to trick the opensource processing scripts so they // will keep the origial package name. - allowed_proto3_extendees->insert(string("proto") + "2." + kOptionNames[i]); + allowed_proto3_extendees->insert(std::string("proto") + "2." + + kOptionNames[i]); } return allowed_proto3_extendees; } @@ -501,7 +505,7 @@ std::set* NewAllowedProto3Extendee() { // Only extensions to descriptor options are allowed. We use name comparison // instead of comparing the descriptor directly because the extensions may be // defined in a different pool. -bool AllowedExtendeeInProto3(const string& name) { +bool AllowedExtendeeInProto3(const std::string& name) { static auto allowed_proto3_extendees = internal::OnShutdownDelete(NewAllowedProto3Extendee()); return allowed_proto3_extendees->find(name) != @@ -557,21 +561,21 @@ class DescriptorPool::Tables { // The stack of files which are currently being built. Used to detect // cyclic dependencies when loading files from a DescriptorDatabase. Not - // used when fallback_database_ == NULL. - std::vector pending_files_; + // used when fallback_database_ == nullptr. + std::vector pending_files_; // A set of files which we have tried to load from the fallback database // and encountered errors. We will not attempt to load them again during // execution of the current public API call, but for compatibility with // legacy clients, this is cleared at the beginning of each public API call. - // Not used when fallback_database_ == NULL. - HASH_SET known_bad_files_; + // Not used when fallback_database_ == nullptr. + HASH_SET known_bad_files_; // A set of symbols which we have tried to load from the fallback database // and encountered errors. We will not attempt to load them again during // execution of the current public API call, but for compatibility with // legacy clients, this is cleared at the beginning of each public API call. - HASH_SET known_bad_symbols_; + HASH_SET known_bad_symbols_; // The set of descriptors for which we've already loaded the full // set of extensions numbers from fallback_database_. @@ -582,17 +586,16 @@ class DescriptorPool::Tables { // Find symbols. This returns a null Symbol (symbol.IsNull() is true) // if not found. - inline Symbol FindSymbol(const string& key) const; + inline Symbol FindSymbol(const std::string& key) const; // This implements the body of DescriptorPool::Find*ByName(). It should // really be a private method of DescriptorPool, but that would require // declaring Symbol in descriptor.h, which would drag all kinds of other // stuff into the header. Yay C++. - Symbol FindByNameHelper( - const DescriptorPool* pool, const string& name); + Symbol FindByNameHelper(const DescriptorPool* pool, const std::string& name); - // These return NULL if not found. - inline const FileDescriptor* FindFile(const string& key) const; + // These return nullptr if not found. + inline const FileDescriptor* FindFile(const std::string& key) const; inline const FieldDescriptor* FindExtension(const Descriptor* extendee, int number) const; inline void FindAllExtensions(const Descriptor* extendee, @@ -605,7 +608,7 @@ class DescriptorPool::Tables { // the key already exists in the table. For AddSymbol(), the string passed // in must be one that was constructed using AllocateString(), as it will // be used as a key in the symbols_by_name_ map without copying. - bool AddSymbol(const string& full_name, Symbol symbol); + bool AddSymbol(const std::string& full_name, Symbol symbol); bool AddFile(const FileDescriptor* file); bool AddExtension(const FieldDescriptor* field); @@ -616,15 +619,20 @@ class DescriptorPool::Tables { // destroyed. Note that the object's destructor will never be called, // so its fields must be plain old data (primitive data types and // pointers). All of the descriptor types are such objects. - template Type* Allocate(); + template + Type* Allocate(); // Allocate an array of objects which will be reclaimed when the // pool in destroyed. Again, destructors are never called. - template Type* AllocateArray(int count); + template + Type* AllocateArray(int count); // Allocate a string which will be destroyed when the pool is destroyed. // The string is initialized to the given value for convenience. - string* AllocateString(const string& value); + std::string* AllocateString(const std::string& value); + + // Allocate empty string which will be destroyed when the pool is destroyed. + std::string* AllocateEmptyString(); // Allocate a internal::call_once which will be destroyed when the pool is // destroyed. @@ -634,22 +642,23 @@ class DescriptorPool::Tables { // trouble understanding explicit template instantiations in some cases, so // in those cases we have to pass a dummy pointer of the right type as the // parameter instead of specifying the type explicitly. - template Type* AllocateMessage(Type* dummy = NULL); + template + Type* AllocateMessage(Type* dummy = nullptr); // Allocate a FileDescriptorTables object. FileDescriptorTables* AllocateFileTables(); private: - std::vector strings_; // All strings in the pool. - std::vector messages_; // All messages in the pool. - std::vector - once_dynamics_; // All internal::call_onces in the pool. - std::vector - file_tables_; // All file tables in the pool. - std::vector allocations_; // All other memory allocated in the pool. - - SymbolsByNameMap symbols_by_name_; - FilesByNameMap files_by_name_; + // All other memory allocated in the pool. Must be first as other objects can + // point into these. + std::vector> allocations_; + std::vector> strings_; + std::vector> messages_; + std::vector> once_dynamics_; + std::vector> file_tables_; + + SymbolsByNameMap symbols_by_name_; + FilesByNameMap files_by_name_; ExtensionsGroupedByDescriptorMap extensions_; struct CheckPoint { @@ -675,8 +684,8 @@ class DescriptorPool::Tables { int pending_extensions_before_checkpoint; }; std::vector checkpoints_; - std::vector symbols_after_checkpoint_; - std::vector files_after_checkpoint_; + std::vector symbols_after_checkpoint_; + std::vector files_after_checkpoint_; std::vector extensions_after_checkpoint_; // Allocate some bytes which will be reclaimed when the pool is @@ -707,20 +716,20 @@ class FileDescriptorTables { // Find symbols. These return a null Symbol (symbol.IsNull() is true) // if not found. inline Symbol FindNestedSymbol(const void* parent, - const string& name) const; + const std::string& name) const; inline Symbol FindNestedSymbolOfType(const void* parent, - const string& name, + const std::string& name, const Symbol::Type type) const; - // These return NULL if not found. - inline const FieldDescriptor* FindFieldByNumber( - const Descriptor* parent, int number) const; + // These return nullptr if not found. + inline const FieldDescriptor* FindFieldByNumber(const Descriptor* parent, + int number) const; inline const FieldDescriptor* FindFieldByLowercaseName( - const void* parent, const string& lowercase_name) const; + const void* parent, const std::string& lowercase_name) const; inline const FieldDescriptor* FindFieldByCamelcaseName( - const void* parent, const string& camelcase_name) const; + const void* parent, const std::string& camelcase_name) const; inline const EnumValueDescriptor* FindEnumValueByNumber( - const EnumDescriptor* parent, int number) const; + const EnumDescriptor* parent, int number) const; // This creates a new EnumValueDescriptor if not found, in a thread-safe way. inline const EnumValueDescriptor* FindEnumValueByNumberCreatingIfUnknown( const EnumDescriptor* parent, int number) const; @@ -732,7 +741,7 @@ class FileDescriptorTables { // the key already exists in the table. For AddAliasUnderParent(), the // string passed in must be one that was constructed using AllocateString(), // as it will be used as a key in the symbols_by_parent_ map without copying. - bool AddAliasUnderParent(const void* parent, const string& name, + bool AddAliasUnderParent(const void* parent, const std::string& name, Symbol symbol); bool AddFieldByNumber(const FieldDescriptor* field); bool AddEnumValueByNumber(const EnumValueDescriptor* value); @@ -747,7 +756,7 @@ class FileDescriptorTables { std::pair* p); // Returns the location denoted by the specified path through info, - // or NULL if not found. + // or nullptr if not found. // The value of info must be that of the corresponding FileDescriptor. // (Conceptually a pure function, but stateful as an optimisation.) const SourceCodeInfo_Location* GetSourceLocation( @@ -776,7 +785,7 @@ class FileDescriptorTables { FieldsByNumberMap fields_by_number_; // Not including extensions. EnumValuesByNumberMap enum_values_by_number_; mutable EnumValuesByNumberMap unknown_enum_values_by_number_ - GOOGLE_GUARDED_BY(unknown_enum_values_mu_); + PROTOBUF_GUARDED_BY(unknown_enum_values_mu_); // Populated on first request to save space, hence constness games. mutable internal::once_flag locations_by_path_once_; @@ -797,15 +806,6 @@ DescriptorPool::Tables::Tables() DescriptorPool::Tables::~Tables() { GOOGLE_DCHECK(checkpoints_.empty()); - // Note that the deletion order is important, since the destructors of some - // messages may refer to objects in allocations_. - STLDeleteElements(&messages_); - for (int i = 0; i < allocations_.size(); i++) { - operator delete(allocations_[i]); - } - STLDeleteElements(&strings_); - STLDeleteElements(&file_tables_); - STLDeleteElements(&once_dynamics_); } FileDescriptorTables::FileDescriptorTables() @@ -849,18 +849,15 @@ void DescriptorPool::Tables::RollbackToLastCheckpoint() { const CheckPoint& checkpoint = checkpoints_.back(); for (int i = checkpoint.pending_symbols_before_checkpoint; - i < symbols_after_checkpoint_.size(); - i++) { + i < symbols_after_checkpoint_.size(); i++) { symbols_by_name_.erase(symbols_after_checkpoint_[i]); } for (int i = checkpoint.pending_files_before_checkpoint; - i < files_after_checkpoint_.size(); - i++) { + i < files_after_checkpoint_.size(); i++) { files_by_name_.erase(files_after_checkpoint_[i]); } for (int i = checkpoint.pending_extensions_before_checkpoint; - i < extensions_after_checkpoint_.size(); - i++) { + i < extensions_after_checkpoint_.size(); i++) { extensions_.erase(extensions_after_checkpoint_[i]); } @@ -870,23 +867,6 @@ void DescriptorPool::Tables::RollbackToLastCheckpoint() { extensions_after_checkpoint_.resize( checkpoint.pending_extensions_before_checkpoint); - STLDeleteContainerPointers( - strings_.begin() + checkpoint.strings_before_checkpoint, strings_.end()); - STLDeleteContainerPointers( - messages_.begin() + checkpoint.messages_before_checkpoint, - messages_.end()); - STLDeleteContainerPointers( - once_dynamics_.begin() + checkpoint.once_dynamics_before_checkpoint, - once_dynamics_.end()); - STLDeleteContainerPointers( - file_tables_.begin() + checkpoint.file_tables_before_checkpoint, - file_tables_.end()); - for (int i = checkpoint.allocations_before_checkpoint; - i < allocations_.size(); - i++) { - operator delete(allocations_[i]); - } - strings_.resize(checkpoint.strings_before_checkpoint); messages_.resize(checkpoint.messages_before_checkpoint); once_dynamics_.resize(checkpoint.once_dynamics_before_checkpoint); @@ -897,9 +877,9 @@ void DescriptorPool::Tables::RollbackToLastCheckpoint() { // ------------------------------------------------------------------- -inline Symbol DescriptorPool::Tables::FindSymbol(const string& key) const { +inline Symbol DescriptorPool::Tables::FindSymbol(const std::string& key) const { const Symbol* result = FindOrNull(symbols_by_name_, key.c_str()); - if (result == NULL) { + if (result == nullptr) { return kNullSymbol; } else { return *result; @@ -907,10 +887,10 @@ inline Symbol DescriptorPool::Tables::FindSymbol(const string& key) const { } inline Symbol FileDescriptorTables::FindNestedSymbol( - const void* parent, const string& name) const { + const void* parent, const std::string& name) const { const Symbol* result = FindOrNull( symbols_by_parent_, PointerStringPair(parent, name.c_str())); - if (result == NULL) { + if (result == nullptr) { return kNullSymbol; } else { return *result; @@ -918,25 +898,25 @@ inline Symbol FileDescriptorTables::FindNestedSymbol( } inline Symbol FileDescriptorTables::FindNestedSymbolOfType( - const void* parent, const string& name, const Symbol::Type type) const { + const void* parent, const std::string& name, + const Symbol::Type type) const { Symbol result = FindNestedSymbol(parent, name); if (result.type != type) return kNullSymbol; return result; } -Symbol DescriptorPool::Tables::FindByNameHelper( - const DescriptorPool* pool, const string& name) { +Symbol DescriptorPool::Tables::FindByNameHelper(const DescriptorPool* pool, + const std::string& name) { MutexLockMaybe lock(pool->mutex_); - if (pool->fallback_database_ != NULL) { + if (pool->fallback_database_ != nullptr) { known_bad_symbols_.clear(); known_bad_files_.clear(); } Symbol result = FindSymbol(name); - if (result.IsNull() && pool->underlay_ != NULL) { + if (result.IsNull() && pool->underlay_ != nullptr) { // Symbol not found; check the underlay. - result = - pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name); + result = pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name); } if (result.IsNull()) { @@ -950,7 +930,7 @@ Symbol DescriptorPool::Tables::FindByNameHelper( } inline const FileDescriptor* DescriptorPool::Tables::FindFile( - const string& key) const { + const std::string& key) const { return FindPtrOrNull(files_by_name_, key.c_str()); } @@ -962,7 +942,7 @@ inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber( const void* FileDescriptorTables::FindParentForFieldsByMap( const FieldDescriptor* field) const { if (field->is_extension()) { - if (field->extension_scope() == NULL) { + if (field->extension_scope() == nullptr) { return field->file(); } else { return field->extension_scope(); @@ -988,7 +968,7 @@ void FileDescriptorTables::FieldsByLowercaseNamesLazyInitInternal() const { } inline const FieldDescriptor* FileDescriptorTables::FindFieldByLowercaseName( - const void* parent, const string& lowercase_name) const { + const void* parent, const std::string& lowercase_name) const { internal::call_once( fields_by_lowercase_name_once_, &FileDescriptorTables::FieldsByLowercaseNamesLazyInitStatic, this); @@ -1012,7 +992,7 @@ void FileDescriptorTables::FieldsByCamelcaseNamesLazyInitInternal() const { } inline const FieldDescriptor* FileDescriptorTables::FindFieldByCamelcaseName( - const void* parent, const string& camelcase_name) const { + const void* parent, const std::string& camelcase_name) const { internal::call_once( fields_by_camelcase_name_once_, FileDescriptorTables::FieldsByCamelcaseNamesLazyInitStatic, this); @@ -1033,7 +1013,7 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( { const EnumValueDescriptor* desc = FindPtrOrNull( enum_values_by_number_, std::make_pair(parent, number)); - if (desc != NULL) { + if (desc != nullptr) { return desc; } } @@ -1042,7 +1022,7 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( ReaderMutexLock l(&unknown_enum_values_mu_); const EnumValueDescriptor* desc = FindPtrOrNull( unknown_enum_values_by_number_, std::make_pair(parent, number)); - if (desc != NULL) { + if (desc != nullptr) { return desc; } } @@ -1052,7 +1032,7 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( WriterMutexLock l(&unknown_enum_values_mu_); const EnumValueDescriptor* desc = FindPtrOrNull( unknown_enum_values_by_number_, std::make_pair(parent, number)); - if (desc != NULL) { + if (desc != nullptr) { return desc; } @@ -1060,15 +1040,14 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( // EnumDescriptor (it's not a part of the enum as originally defined), but // we do insert it into the table so that we can return the same pointer // later. - string enum_value_name = StringPrintf( - "UNKNOWN_ENUM_VALUE_%s_%d", parent->name().c_str(), number); - DescriptorPool::Tables* tables = - const_cast(DescriptorPool::generated_pool()-> - tables_.get()); + std::string enum_value_name = StringPrintf("UNKNOWN_ENUM_VALUE_%s_%d", + parent->name().c_str(), number); + DescriptorPool::Tables* tables = const_cast( + DescriptorPool::generated_pool()->tables_.get()); EnumValueDescriptor* result = tables->Allocate(); result->name_ = tables->AllocateString(enum_value_name); - result->full_name_ = tables->AllocateString(parent->full_name() + - "." + enum_value_name); + result->full_name_ = + tables->AllocateString(parent->full_name() + "." + enum_value_name); result->number_ = number; result->type_ = parent; result->options_ = &EnumValueOptions::default_instance(); @@ -1078,7 +1057,6 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( } } - inline const FieldDescriptor* DescriptorPool::Tables::FindExtension( const Descriptor* extendee, int number) const { return FindPtrOrNull(extensions_, std::make_pair(extendee, number)); @@ -1096,8 +1074,8 @@ inline void DescriptorPool::Tables::FindAllExtensions( // ------------------------------------------------------------------- -bool DescriptorPool::Tables::AddSymbol( - const string& full_name, Symbol symbol) { +bool DescriptorPool::Tables::AddSymbol(const std::string& full_name, + Symbol symbol) { if (InsertIfNotPresent(&symbols_by_name_, full_name.c_str(), symbol)) { symbols_after_checkpoint_.push_back(full_name.c_str()); return true; @@ -1106,8 +1084,9 @@ bool DescriptorPool::Tables::AddSymbol( } } -bool FileDescriptorTables::AddAliasUnderParent( - const void* parent, const string& name, Symbol symbol) { +bool FileDescriptorTables::AddAliasUnderParent(const void* parent, + const std::string& name, + Symbol symbol) { PointerStringPair by_parent_key(parent, name.c_str()); return InsertIfNotPresent(&symbols_by_parent_, by_parent_key, symbol); } @@ -1178,38 +1157,44 @@ bool DescriptorPool::Tables::AddExtension(const FieldDescriptor* field) { // ------------------------------------------------------------------- -template +template Type* DescriptorPool::Tables::Allocate() { return reinterpret_cast(AllocateBytes(sizeof(Type))); } -template +template Type* DescriptorPool::Tables::AllocateArray(int count) { return reinterpret_cast(AllocateBytes(sizeof(Type) * count)); } -string* DescriptorPool::Tables::AllocateString(const string& value) { - string* result = new string(value); - strings_.push_back(result); +std::string* DescriptorPool::Tables::AllocateString(const std::string& value) { + std::string* result = new std::string(value); + strings_.emplace_back(result); + return result; +} + +std::string* DescriptorPool::Tables::AllocateEmptyString() { + std::string* result = new std::string(); + strings_.emplace_back(result); return result; } internal::once_flag* DescriptorPool::Tables::AllocateOnceDynamic() { internal::once_flag* result = new internal::once_flag(); - once_dynamics_.push_back(result); + once_dynamics_.emplace_back(result); return result; } -template +template Type* DescriptorPool::Tables::AllocateMessage(Type* /* dummy */) { Type* result = new Type; - messages_.push_back(result); + messages_.emplace_back(result); return result; } FileDescriptorTables* DescriptorPool::Tables::AllocateFileTables() { FileDescriptorTables* result = new FileDescriptorTables; - file_tables_.push_back(result); + file_tables_.emplace_back(result); return result; } @@ -1218,11 +1203,10 @@ void* DescriptorPool::Tables::AllocateBytes(int size) { // sophisticated way? Probably not for the open source release, but for // internal use we could easily plug in one of our existing memory pool // allocators... - if (size == 0) return NULL; + if (size == 0) return nullptr; - void* result = operator new(size); - allocations_.push_back(result); - return result; + allocations_.emplace_back(new char[size]); + return allocations_.back().get(); } void FileDescriptorTables::BuildLocationsByPath( @@ -1248,45 +1232,44 @@ const SourceCodeInfo_Location* FileDescriptorTables::GetSourceLocation( DescriptorPool::ErrorCollector::~ErrorCollector() {} DescriptorPool::DescriptorPool() - : mutex_(NULL), - fallback_database_(NULL), - default_error_collector_(NULL), - underlay_(NULL), - tables_(new Tables), - enforce_dependencies_(true), - lazily_build_dependencies_(false), - allow_unknown_(false), - enforce_weak_(false), - disallow_enforce_utf8_(false) {} + : mutex_(nullptr), + fallback_database_(nullptr), + default_error_collector_(nullptr), + underlay_(nullptr), + tables_(new Tables), + enforce_dependencies_(true), + lazily_build_dependencies_(false), + allow_unknown_(false), + enforce_weak_(false), + disallow_enforce_utf8_(false) {} DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database, ErrorCollector* error_collector) - : mutex_(new internal::WrappedMutex), - fallback_database_(fallback_database), - default_error_collector_(error_collector), - underlay_(NULL), - tables_(new Tables), - enforce_dependencies_(true), - lazily_build_dependencies_(false), - allow_unknown_(false), - enforce_weak_(false), - disallow_enforce_utf8_(false) { -} + : mutex_(new internal::WrappedMutex), + fallback_database_(fallback_database), + default_error_collector_(error_collector), + underlay_(nullptr), + tables_(new Tables), + enforce_dependencies_(true), + lazily_build_dependencies_(false), + allow_unknown_(false), + enforce_weak_(false), + disallow_enforce_utf8_(false) {} DescriptorPool::DescriptorPool(const DescriptorPool* underlay) - : mutex_(NULL), - fallback_database_(NULL), - default_error_collector_(NULL), - underlay_(underlay), - tables_(new Tables), - enforce_dependencies_(true), - lazily_build_dependencies_(false), - allow_unknown_(false), - enforce_weak_(false), - disallow_enforce_utf8_(false) {} + : mutex_(nullptr), + fallback_database_(nullptr), + default_error_collector_(nullptr), + underlay_(underlay), + tables_(new Tables), + enforce_dependencies_(true), + lazily_build_dependencies_(false), + allow_unknown_(false), + enforce_weak_(false), + disallow_enforce_utf8_(false) {} DescriptorPool::~DescriptorPool() { - if (mutex_ != NULL) delete mutex_; + if (mutex_ != nullptr) delete mutex_; } // DescriptorPool::BuildFile() defined later. @@ -1296,7 +1279,7 @@ void DescriptorPool::InternalDontEnforceDependencies() { enforce_dependencies_ = false; } -void DescriptorPool::AddUnusedImportTrackFile(const string& file_name) { +void DescriptorPool::AddUnusedImportTrackFile(const std::string& file_name) { unused_import_track_files_.insert(file_name); } @@ -1304,9 +1287,9 @@ void DescriptorPool::ClearUnusedImportTrackFiles() { unused_import_track_files_.clear(); } -bool DescriptorPool::InternalIsFileLoaded(const string& filename) const { +bool DescriptorPool::InternalIsFileLoaded(const std::string& filename) const { MutexLockMaybe lock(mutex_); - return tables_->FindFile(filename) != NULL; + return tables_->FindFile(filename) != nullptr; } // generated_pool ==================================================== @@ -1335,7 +1318,10 @@ DescriptorPool* DescriptorPool::internal_generated_pool() { } const DescriptorPool* DescriptorPool::generated_pool() { - return internal_generated_pool(); + const DescriptorPool* pool = internal_generated_pool(); + // Ensure that descriptor.proto has been registered in the generated pool. + DescriptorProto::descriptor(); + return pool; } @@ -1374,157 +1360,185 @@ void DescriptorPool::InternalAddGeneratedFile( // there's any good way to factor it out. Think about this some time when // there's nothing more important to do (read: never). -const FileDescriptor* DescriptorPool::FindFileByName(const string& name) const { +const FileDescriptor* DescriptorPool::FindFileByName( + const std::string& name) const { MutexLockMaybe lock(mutex_); - if (fallback_database_ != NULL) { + if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); } const FileDescriptor* result = tables_->FindFile(name); - if (result != NULL) return result; - if (underlay_ != NULL) { + if (result != nullptr) return result; + if (underlay_ != nullptr) { result = underlay_->FindFileByName(name); - if (result != NULL) return result; + if (result != nullptr) return result; } if (TryFindFileInFallbackDatabase(name)) { result = tables_->FindFile(name); - if (result != NULL) return result; + if (result != nullptr) return result; } - return NULL; + return nullptr; } const FileDescriptor* DescriptorPool::FindFileContainingSymbol( - const string& symbol_name) const { + const std::string& symbol_name) const { MutexLockMaybe lock(mutex_); - if (fallback_database_ != NULL) { + if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); } Symbol result = tables_->FindSymbol(symbol_name); if (!result.IsNull()) return result.GetFile(); - if (underlay_ != NULL) { + if (underlay_ != nullptr) { const FileDescriptor* file_result = - underlay_->FindFileContainingSymbol(symbol_name); - if (file_result != NULL) return file_result; + underlay_->FindFileContainingSymbol(symbol_name); + if (file_result != nullptr) return file_result; } if (TryFindSymbolInFallbackDatabase(symbol_name)) { result = tables_->FindSymbol(symbol_name); if (!result.IsNull()) return result.GetFile(); } - return NULL; + return nullptr; } const Descriptor* DescriptorPool::FindMessageTypeByName( - const string& name) const { + const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - return (result.type == Symbol::MESSAGE) ? result.descriptor : NULL; + return (result.type == Symbol::MESSAGE) ? result.descriptor : nullptr; } const FieldDescriptor* DescriptorPool::FindFieldByName( - const string& name) const { + const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); if (result.type == Symbol::FIELD && !result.field_descriptor->is_extension()) { return result.field_descriptor; } else { - return NULL; + return nullptr; } } const FieldDescriptor* DescriptorPool::FindExtensionByName( - const string& name) const { + const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - if (result.type == Symbol::FIELD && - result.field_descriptor->is_extension()) { + if (result.type == Symbol::FIELD && result.field_descriptor->is_extension()) { return result.field_descriptor; } else { - return NULL; + return nullptr; } } const OneofDescriptor* DescriptorPool::FindOneofByName( - const string& name) const { + const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - return (result.type == Symbol::ONEOF) ? result.oneof_descriptor : NULL; + return (result.type == Symbol::ONEOF) ? result.oneof_descriptor : nullptr; } const EnumDescriptor* DescriptorPool::FindEnumTypeByName( - const string& name) const { + const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - return (result.type == Symbol::ENUM) ? result.enum_descriptor : NULL; + return (result.type == Symbol::ENUM) ? result.enum_descriptor : nullptr; } const EnumValueDescriptor* DescriptorPool::FindEnumValueByName( - const string& name) const { + const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - return (result.type == Symbol::ENUM_VALUE) ? - result.enum_value_descriptor : NULL; + return (result.type == Symbol::ENUM_VALUE) ? result.enum_value_descriptor + : nullptr; } const ServiceDescriptor* DescriptorPool::FindServiceByName( - const string& name) const { + const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - return (result.type == Symbol::SERVICE) ? result.service_descriptor : NULL; + return (result.type == Symbol::SERVICE) ? result.service_descriptor : nullptr; } const MethodDescriptor* DescriptorPool::FindMethodByName( - const string& name) const { + const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - return (result.type == Symbol::METHOD) ? result.method_descriptor : NULL; + return (result.type == Symbol::METHOD) ? result.method_descriptor : nullptr; } const FieldDescriptor* DescriptorPool::FindExtensionByNumber( const Descriptor* extendee, int number) const { + if (extendee->extension_range_count() == 0) return nullptr; // A faster path to reduce lock contention in finding extensions, assuming // most extensions will be cache hit. - if (mutex_ != NULL) { + if (mutex_ != nullptr) { ReaderMutexLock lock(mutex_); const FieldDescriptor* result = tables_->FindExtension(extendee, number); - if (result != NULL) { + if (result != nullptr) { return result; } } MutexLockMaybe lock(mutex_); - if (fallback_database_ != NULL) { + if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); } const FieldDescriptor* result = tables_->FindExtension(extendee, number); - if (result != NULL) { + if (result != nullptr) { return result; } - if (underlay_ != NULL) { + if (underlay_ != nullptr) { result = underlay_->FindExtensionByNumber(extendee, number); - if (result != NULL) return result; + if (result != nullptr) return result; } if (TryFindExtensionInFallbackDatabase(extendee, number)) { result = tables_->FindExtension(extendee, number); - if (result != NULL) { + if (result != nullptr) { return result; } } - return NULL; + return nullptr; +} + +const FieldDescriptor* DescriptorPool::FindExtensionByPrintableName( + const Descriptor* extendee, const std::string& printable_name) const { + if (extendee->extension_range_count() == 0) return nullptr; + const FieldDescriptor* result = FindExtensionByName(printable_name); + if (result != nullptr && result->containing_type() == extendee) { + return result; + } + if (extendee->options().message_set_wire_format()) { + // MessageSet extensions may be identified by type name. + const Descriptor* type = FindMessageTypeByName(printable_name); + if (type != nullptr) { + // Look for a matching extension in the foreign type's scope. + const int type_extension_count = type->extension_count(); + for (int i = 0; i < type_extension_count; i++) { + const FieldDescriptor* extension = type->extension(i); + if (extension->containing_type() == extendee && + extension->type() == FieldDescriptor::TYPE_MESSAGE && + extension->is_optional() && extension->message_type() == type) { + // Found it. + return extension; + } + } + } + } + return nullptr; } void DescriptorPool::FindAllExtensions( const Descriptor* extendee, std::vector* out) const { MutexLockMaybe lock(mutex_); - if (fallback_database_ != NULL) { + if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); } // Initialize tables_->extensions_ from the fallback database first // (but do this only once per descriptor). - if (fallback_database_ != NULL && + if (fallback_database_ != nullptr && tables_->extensions_loaded_from_db_.count(extendee) == 0) { std::vector numbers; if (fallback_database_->FindAllExtensionNumbers(extendee->full_name(), &numbers)) { for (int i = 0; i < numbers.size(); ++i) { int number = numbers[i]; - if (tables_->FindExtension(extendee, number) == NULL) { + if (tables_->FindExtension(extendee, number) == nullptr) { TryFindExtensionInFallbackDatabase(extendee, number); } } @@ -1533,7 +1547,7 @@ void DescriptorPool::FindAllExtensions( } tables_->FindAllExtensions(extendee, out); - if (underlay_ != NULL) { + if (underlay_ != nullptr) { underlay_->FindAllExtensions(extendee, out); } } @@ -1541,225 +1555,222 @@ void DescriptorPool::FindAllExtensions( // ------------------------------------------------------------------- -const FieldDescriptor* -Descriptor::FindFieldByNumber(int key) const { - const FieldDescriptor* result = - file()->tables_->FindFieldByNumber(this, key); - if (result == NULL || result->is_extension()) { - return NULL; +const FieldDescriptor* Descriptor::FindFieldByNumber(int key) const { + const FieldDescriptor* result = file()->tables_->FindFieldByNumber(this, key); + if (result == nullptr || result->is_extension()) { + return nullptr; } else { return result; } } -const FieldDescriptor* -Descriptor::FindFieldByLowercaseName(const string& key) const { +const FieldDescriptor* Descriptor::FindFieldByLowercaseName( + const std::string& key) const { const FieldDescriptor* result = - file()->tables_->FindFieldByLowercaseName(this, key); - if (result == NULL || result->is_extension()) { - return NULL; + file()->tables_->FindFieldByLowercaseName(this, key); + if (result == nullptr || result->is_extension()) { + return nullptr; } else { return result; } } -const FieldDescriptor* -Descriptor::FindFieldByCamelcaseName(const string& key) const { +const FieldDescriptor* Descriptor::FindFieldByCamelcaseName( + const std::string& key) const { const FieldDescriptor* result = - file()->tables_->FindFieldByCamelcaseName(this, key); - if (result == NULL || result->is_extension()) { - return NULL; + file()->tables_->FindFieldByCamelcaseName(this, key); + if (result == nullptr || result->is_extension()) { + return nullptr; } else { return result; } } -const FieldDescriptor* -Descriptor::FindFieldByName(const string& key) const { +const FieldDescriptor* Descriptor::FindFieldByName( + const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); if (!result.IsNull() && !result.field_descriptor->is_extension()) { return result.field_descriptor; } else { - return NULL; + return nullptr; } } -const OneofDescriptor* -Descriptor::FindOneofByName(const string& key) const { +const OneofDescriptor* Descriptor::FindOneofByName( + const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ONEOF); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ONEOF); if (!result.IsNull()) { return result.oneof_descriptor; } else { - return NULL; + return nullptr; } } -const FieldDescriptor* -Descriptor::FindExtensionByName(const string& key) const { +const FieldDescriptor* Descriptor::FindExtensionByName( + const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); if (!result.IsNull() && result.field_descriptor->is_extension()) { return result.field_descriptor; } else { - return NULL; + return nullptr; } } -const FieldDescriptor* -Descriptor::FindExtensionByLowercaseName(const string& key) const { +const FieldDescriptor* Descriptor::FindExtensionByLowercaseName( + const std::string& key) const { const FieldDescriptor* result = - file()->tables_->FindFieldByLowercaseName(this, key); - if (result == NULL || !result->is_extension()) { - return NULL; + file()->tables_->FindFieldByLowercaseName(this, key); + if (result == nullptr || !result->is_extension()) { + return nullptr; } else { return result; } } -const FieldDescriptor* -Descriptor::FindExtensionByCamelcaseName(const string& key) const { +const FieldDescriptor* Descriptor::FindExtensionByCamelcaseName( + const std::string& key) const { const FieldDescriptor* result = - file()->tables_->FindFieldByCamelcaseName(this, key); - if (result == NULL || !result->is_extension()) { - return NULL; + file()->tables_->FindFieldByCamelcaseName(this, key); + if (result == nullptr || !result->is_extension()) { + return nullptr; } else { return result; } } -const Descriptor* -Descriptor::FindNestedTypeByName(const string& key) const { +const Descriptor* Descriptor::FindNestedTypeByName( + const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); if (!result.IsNull()) { return result.descriptor; } else { - return NULL; + return nullptr; } } -const EnumDescriptor* -Descriptor::FindEnumTypeByName(const string& key) const { +const EnumDescriptor* Descriptor::FindEnumTypeByName( + const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); if (!result.IsNull()) { return result.enum_descriptor; } else { - return NULL; + return nullptr; } } -const EnumValueDescriptor* -Descriptor::FindEnumValueByName(const string& key) const { +const EnumValueDescriptor* Descriptor::FindEnumValueByName( + const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); if (!result.IsNull()) { return result.enum_value_descriptor; } else { - return NULL; + return nullptr; } } -const EnumValueDescriptor* -EnumDescriptor::FindValueByName(const string& key) const { +const EnumValueDescriptor* EnumDescriptor::FindValueByName( + const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); if (!result.IsNull()) { return result.enum_value_descriptor; } else { - return NULL; + return nullptr; } } -const EnumValueDescriptor* -EnumDescriptor::FindValueByNumber(int key) const { +const EnumValueDescriptor* EnumDescriptor::FindValueByNumber(int key) const { return file()->tables_->FindEnumValueByNumber(this, key); } -const EnumValueDescriptor* -EnumDescriptor::FindValueByNumberCreatingIfUnknown(int key) const { +const EnumValueDescriptor* EnumDescriptor::FindValueByNumberCreatingIfUnknown( + int key) const { return file()->tables_->FindEnumValueByNumberCreatingIfUnknown(this, key); } -const MethodDescriptor* -ServiceDescriptor::FindMethodByName(const string& key) const { +const MethodDescriptor* ServiceDescriptor::FindMethodByName( + const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD); if (!result.IsNull()) { return result.method_descriptor; } else { - return NULL; + return nullptr; } } -const Descriptor* -FileDescriptor::FindMessageTypeByName(const string& key) const { +const Descriptor* FileDescriptor::FindMessageTypeByName( + const std::string& key) const { Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); if (!result.IsNull()) { return result.descriptor; } else { - return NULL; + return nullptr; } } -const EnumDescriptor* -FileDescriptor::FindEnumTypeByName(const string& key) const { +const EnumDescriptor* FileDescriptor::FindEnumTypeByName( + const std::string& key) const { Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); if (!result.IsNull()) { return result.enum_descriptor; } else { - return NULL; + return nullptr; } } -const EnumValueDescriptor* -FileDescriptor::FindEnumValueByName(const string& key) const { +const EnumValueDescriptor* FileDescriptor::FindEnumValueByName( + const std::string& key) const { Symbol result = - tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); + tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); if (!result.IsNull()) { return result.enum_value_descriptor; } else { - return NULL; + return nullptr; } } -const ServiceDescriptor* -FileDescriptor::FindServiceByName(const string& key) const { +const ServiceDescriptor* FileDescriptor::FindServiceByName( + const std::string& key) const { Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::SERVICE); if (!result.IsNull()) { return result.service_descriptor; } else { - return NULL; + return nullptr; } } -const FieldDescriptor* -FileDescriptor::FindExtensionByName(const string& key) const { +const FieldDescriptor* FileDescriptor::FindExtensionByName( + const std::string& key) const { Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); if (!result.IsNull() && result.field_descriptor->is_extension()) { return result.field_descriptor; } else { - return NULL; + return nullptr; } } -const FieldDescriptor* -FileDescriptor::FindExtensionByLowercaseName(const string& key) const { +const FieldDescriptor* FileDescriptor::FindExtensionByLowercaseName( + const std::string& key) const { const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, key); - if (result == NULL || !result->is_extension()) { - return NULL; + if (result == nullptr || !result->is_extension()) { + return nullptr; } else { return result; } } -const FieldDescriptor* -FileDescriptor::FindExtensionByCamelcaseName(const string& key) const { +const FieldDescriptor* FileDescriptor::FindExtensionByCamelcaseName( + const std::string& key) const { const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, key); - if (result == NULL || !result->is_extension()) { - return NULL; + if (result == nullptr || !result->is_extension()) { + return nullptr; } else { return result; } @@ -1780,23 +1791,22 @@ Descriptor::FindExtensionRangeContainingNumber(int number) const { // more than a couple extension ranges. for (int i = 0; i < extension_range_count(); i++) { if (number >= extension_range(i)->start && - number < extension_range(i)->end) { + number < extension_range(i)->end) { return extension_range(i); } } - return NULL; + return nullptr; } -const Descriptor::ReservedRange* -Descriptor::FindReservedRangeContainingNumber(int number) const { +const Descriptor::ReservedRange* Descriptor::FindReservedRangeContainingNumber( + int number) const { // TODO(chrisn): Consider a non-linear search. for (int i = 0; i < reserved_range_count(); i++) { - if (number >= reserved_range(i)->start && - number < reserved_range(i)->end) { + if (number >= reserved_range(i)->start && number < reserved_range(i)->end) { return reserved_range(i); } } - return NULL; + return nullptr; } const EnumDescriptor::ReservedRange* @@ -1808,30 +1818,31 @@ EnumDescriptor::FindReservedRangeContainingNumber(int number) const { return reserved_range(i); } } - return NULL; + return nullptr; } // ------------------------------------------------------------------- -bool DescriptorPool::TryFindFileInFallbackDatabase(const string& name) const { - if (fallback_database_ == NULL) return false; +bool DescriptorPool::TryFindFileInFallbackDatabase( + const std::string& name) const { + if (fallback_database_ == nullptr) return false; if (tables_->known_bad_files_.count(name) > 0) return false; FileDescriptorProto file_proto; if (!fallback_database_->FindFileByName(name, &file_proto) || - BuildFileFromDatabase(file_proto) == NULL) { + BuildFileFromDatabase(file_proto) == nullptr) { tables_->known_bad_files_.insert(name); return false; } return true; } -bool DescriptorPool::IsSubSymbolOfBuiltType(const string& name) const { - string prefix = name; +bool DescriptorPool::IsSubSymbolOfBuiltType(const std::string& name) const { + std::string prefix = name; for (;;) { - string::size_type dot_pos = prefix.find_last_of('.'); - if (dot_pos == string::npos) { + std::string::size_type dot_pos = prefix.find_last_of('.'); + if (dot_pos == std::string::npos) { break; } prefix = prefix.substr(0, dot_pos); @@ -1842,35 +1853,36 @@ bool DescriptorPool::IsSubSymbolOfBuiltType(const string& name) const { return true; } } - if (underlay_ != NULL) { + if (underlay_ != nullptr) { // Check to see if any prefix of this symbol exists in the underlay. return underlay_->IsSubSymbolOfBuiltType(name); } return false; } -bool DescriptorPool::TryFindSymbolInFallbackDatabase(const string& name) const { - if (fallback_database_ == NULL) return false; +bool DescriptorPool::TryFindSymbolInFallbackDatabase( + const std::string& name) const { + if (fallback_database_ == nullptr) return false; if (tables_->known_bad_symbols_.count(name) > 0) return false; FileDescriptorProto file_proto; - if (// We skip looking in the fallback database if the name is a sub-symbol - // of any descriptor that already exists in the descriptor pool (except - // for package descriptors). This is valid because all symbols except - // for packages are defined in a single file, so if the symbol exists - // then we should already have its definition. - // - // The other reason to do this is to support "overriding" type - // definitions by merging two databases that define the same type. (Yes, - // people do this.) The main difficulty with making this work is that - // FindFileContainingSymbol() is allowed to return both false positives - // (e.g., SimpleDescriptorDatabase, UpgradedDescriptorDatabase) and false - // negatives (e.g. ProtoFileParser, SourceTreeDescriptorDatabase). - // When two such databases are merged, looking up a non-existent - // sub-symbol of a type that already exists in the descriptor pool can - // result in an attempt to load multiple definitions of the same type. - // The check below avoids this. + if ( // We skip looking in the fallback database if the name is a sub-symbol + // of any descriptor that already exists in the descriptor pool (except + // for package descriptors). This is valid because all symbols except + // for packages are defined in a single file, so if the symbol exists + // then we should already have its definition. + // + // The other reason to do this is to support "overriding" type + // definitions by merging two databases that define the same type. (Yes, + // people do this.) The main difficulty with making this work is that + // FindFileContainingSymbol() is allowed to return both false positives + // (e.g., SimpleDescriptorDatabase, UpgradedDescriptorDatabase) and + // false negatives (e.g. ProtoFileParser, SourceTreeDescriptorDatabase). + // When two such databases are merged, looking up a non-existent + // sub-symbol of a type that already exists in the descriptor pool can + // result in an attempt to load multiple definitions of the same type. + // The check below avoids this. IsSubSymbolOfBuiltType(name) // Look up file containing this symbol in fallback database. @@ -1879,10 +1891,10 @@ bool DescriptorPool::TryFindSymbolInFallbackDatabase(const string& name) const { // Check if we've already built this file. If so, it apparently doesn't // contain the symbol we're looking for. Some DescriptorDatabases // return false positives. - || tables_->FindFile(file_proto.name()) != NULL + || tables_->FindFile(file_proto.name()) != nullptr // Build the file. - || BuildFileFromDatabase(file_proto) == NULL) { + || BuildFileFromDatabase(file_proto) == nullptr) { tables_->known_bad_symbols_.insert(name); return false; } @@ -1892,22 +1904,22 @@ bool DescriptorPool::TryFindSymbolInFallbackDatabase(const string& name) const { bool DescriptorPool::TryFindExtensionInFallbackDatabase( const Descriptor* containing_type, int field_number) const { - if (fallback_database_ == NULL) return false; + if (fallback_database_ == nullptr) return false; FileDescriptorProto file_proto; if (!fallback_database_->FindFileContainingExtension( - containing_type->full_name(), field_number, &file_proto)) { + containing_type->full_name(), field_number, &file_proto)) { return false; } - if (tables_->FindFile(file_proto.name()) != NULL) { + if (tables_->FindFile(file_proto.name()) != nullptr) { // We've already loaded this file, and it apparently doesn't contain the // extension we're looking for. Some DescriptorDatabases return false // positives. return false; } - if (BuildFileFromDatabase(file_proto) == NULL) { + if (BuildFileFromDatabase(file_proto) == nullptr) { return false; } @@ -1920,20 +1932,21 @@ bool FieldDescriptor::is_map_message_type() const { return message_type_->options().map_entry(); } -string FieldDescriptor::DefaultValueAsString(bool quote_string_type) const { +std::string FieldDescriptor::DefaultValueAsString( + bool quote_string_type) const { GOOGLE_CHECK(has_default_value()) << "No default value"; switch (cpp_type()) { case CPPTYPE_INT32: - return SimpleItoa(default_value_int32()); + return StrCat(default_value_int32()); break; case CPPTYPE_INT64: - return SimpleItoa(default_value_int64()); + return StrCat(default_value_int64()); break; case CPPTYPE_UINT32: - return SimpleItoa(default_value_uint32()); + return StrCat(default_value_uint32()); break; case CPPTYPE_UINT64: - return SimpleItoa(default_value_uint64()); + return StrCat(default_value_uint64()); break; case CPPTYPE_FLOAT: return SimpleFtoa(default_value_float()); @@ -2088,9 +2101,9 @@ void FieldDescriptor::CopyTo(FieldDescriptorProto* proto) const { // Some compilers do not allow static_cast directly between two enum types, // so we must cast to int first. proto->set_label(static_cast( - ::google::protobuf::implicit_cast(label()))); + implicit_cast(label()))); proto->set_type(static_cast( - ::google::protobuf::implicit_cast(type()))); + implicit_cast(type()))); if (is_extension()) { if (!containing_type()->is_unqualified_placeholder_) { @@ -2121,7 +2134,7 @@ void FieldDescriptor::CopyTo(FieldDescriptorProto* proto) const { proto->set_default_value(DefaultValueAsString(false)); } - if (containing_oneof() != NULL && !is_extension()) { + if (containing_oneof() != nullptr && !is_extension()) { proto->set_oneof_index(containing_oneof()->index()); } @@ -2211,8 +2224,9 @@ void MethodDescriptor::CopyTo(MethodDescriptorProto* proto) const { namespace { -bool RetrieveOptionsAssumingRightPool(int depth, const Message& options, - std::vector* option_entries) { +bool RetrieveOptionsAssumingRightPool( + int depth, const Message& options, + std::vector* option_entries) { option_entries->clear(); const Reflection* reflection = options.GetReflection(); std::vector fields; @@ -2225,13 +2239,13 @@ bool RetrieveOptionsAssumingRightPool(int depth, const Message& options, repeated = true; } for (int j = 0; j < count; j++) { - string fieldval; + std::string fieldval; if (fields[i]->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - string tmp; + std::string tmp; TextFormat::Printer printer; printer.SetInitialIndentLevel(depth + 1); - printer.PrintFieldValueToString(options, fields[i], - repeated ? j : -1, &tmp); + printer.PrintFieldValueToString(options, fields[i], repeated ? j : -1, + &tmp); fieldval.append("{\n"); fieldval.append(tmp); fieldval.append(depth * 2, ' '); @@ -2240,7 +2254,7 @@ bool RetrieveOptionsAssumingRightPool(int depth, const Message& options, TextFormat::PrintFieldValueToString(options, fields[i], repeated ? j : -1, &fieldval); } - string name; + std::string name; if (fields[i]->is_extension()) { name = "(." + fields[i]->full_name() + ")"; } else { @@ -2255,7 +2269,7 @@ bool RetrieveOptionsAssumingRightPool(int depth, const Message& options, // Used by each of the option formatters. bool RetrieveOptions(int depth, const Message& options, const DescriptorPool* pool, - std::vector* option_entries) { + std::vector* option_entries) { // When printing custom options for a descriptor, we must use an options // message built on top of the same DescriptorPool where the descriptor // is coming from. This is to ensure we are interpreting custom options @@ -2265,7 +2279,7 @@ bool RetrieveOptions(int depth, const Message& options, } else { const Descriptor* option_descriptor = pool->FindMessageTypeByName(options.GetDescriptor()->full_name()); - if (option_descriptor == NULL) { + if (option_descriptor == nullptr) { // descriptor.proto is not in the pool. This means no custom options are // used so we are safe to proceed with the compiled options message type. return RetrieveOptionsAssumingRightPool(depth, options, option_entries); @@ -2287,8 +2301,8 @@ bool RetrieveOptions(int depth, const Message& options, // Formats options that all appear together in brackets. Does not include // brackets. bool FormatBracketedOptions(int depth, const Message& options, - const DescriptorPool* pool, string* output) { - std::vector all_options; + const DescriptorPool* pool, std::string* output) { + std::vector all_options; if (RetrieveOptions(depth, options, pool, &all_options)) { output->append(Join(all_options, ", ")); } @@ -2297,13 +2311,13 @@ bool FormatBracketedOptions(int depth, const Message& options, // Formats options one per line bool FormatLineOptions(int depth, const Message& options, - const DescriptorPool* pool, string* output) { - string prefix(depth * 2, ' '); - std::vector all_options; + const DescriptorPool* pool, std::string* output) { + std::string prefix(depth * 2, ' '); + std::vector all_options; if (RetrieveOptions(depth, options, pool, &all_options)) { for (int i = 0; i < all_options.size(); i++) { - strings::SubstituteAndAppend(output, "$0option $1;\n", - prefix, all_options[i]); + strings::SubstituteAndAppend(output, "$0option $1;\n", prefix, + all_options[i]); } } return !all_options.empty(); @@ -2311,30 +2325,29 @@ bool FormatLineOptions(int depth, const Message& options, class SourceLocationCommentPrinter { public: - template - SourceLocationCommentPrinter(const DescType* desc, - const string& prefix, + template + SourceLocationCommentPrinter(const DescType* desc, const std::string& prefix, const DebugStringOptions& options) : options_(options), prefix_(prefix) { // Perform the SourceLocation lookup only if we're including user comments, // because the lookup is fairly expensive. - have_source_loc_ = options.include_comments && - desc->GetSourceLocation(&source_loc_); + have_source_loc_ = + options.include_comments && desc->GetSourceLocation(&source_loc_); } SourceLocationCommentPrinter(const FileDescriptor* file, const std::vector& path, - const string& prefix, + const std::string& prefix, const DebugStringOptions& options) : options_(options), prefix_(prefix) { // Perform the SourceLocation lookup only if we're including user comments, // because the lookup is fairly expensive. - have_source_loc_ = options.include_comments && - file->GetSourceLocation(path, &source_loc_); + have_source_loc_ = + options.include_comments && file->GetSourceLocation(path, &source_loc_); } - void AddPreComment(string* output) { + void AddPreComment(std::string* output) { if (have_source_loc_) { // Detached leading comments. - for (int i = 0 ; i < source_loc_.leading_detached_comments.size(); ++i) { + for (int i = 0; i < source_loc_.leading_detached_comments.size(); ++i) { *output += FormatComment(source_loc_.leading_detached_comments[i]); *output += "\n"; } @@ -2344,7 +2357,7 @@ class SourceLocationCommentPrinter { } } } - void AddPostComment(string* output) { + void AddPostComment(std::string* output) { if (have_source_loc_ && source_loc_.trailing_comments.size() > 0) { *output += FormatComment(source_loc_.trailing_comments); } @@ -2352,13 +2365,13 @@ class SourceLocationCommentPrinter { // Format comment such that each line becomes a full-line C++-style comment in // the DebugString() output. - string FormatComment(const string& comment_text) { - string stripped_comment = comment_text; + std::string FormatComment(const std::string& comment_text) { + std::string stripped_comment = comment_text; StripWhitespace(&stripped_comment); - std::vector lines = Split(stripped_comment, "\n"); - string output; + std::vector lines = Split(stripped_comment, "\n"); + std::string output; for (int i = 0; i < lines.size(); ++i) { - const string& line = lines[i]; + const std::string& line = lines[i]; strings::SubstituteAndAppend(&output, "$0// $1\n", prefix_, line); } return output; @@ -2369,32 +2382,31 @@ class SourceLocationCommentPrinter { bool have_source_loc_; SourceLocation source_loc_; DebugStringOptions options_; - string prefix_; + std::string prefix_; }; } // anonymous namespace -string FileDescriptor::DebugString() const { +std::string FileDescriptor::DebugString() const { DebugStringOptions options; // default options return DebugStringWithOptions(options); } -string FileDescriptor::DebugStringWithOptions( +std::string FileDescriptor::DebugStringWithOptions( const DebugStringOptions& debug_string_options) const { - string contents; + std::string contents; { std::vector path; path.push_back(FileDescriptorProto::kSyntaxFieldNumber); - SourceLocationCommentPrinter syntax_comment( - this, path, "", debug_string_options); + SourceLocationCommentPrinter syntax_comment(this, path, "", + debug_string_options); syntax_comment.AddPreComment(&contents); strings::SubstituteAndAppend(&contents, "syntax = \"$0\";\n\n", SyntaxName(syntax())); syntax_comment.AddPostComment(&contents); } - SourceLocationCommentPrinter - comment_printer(this, "", debug_string_options); + SourceLocationCommentPrinter comment_printer(this, "", debug_string_options); comment_printer.AddPreComment(&contents); std::set public_dependencies; @@ -2420,8 +2432,8 @@ string FileDescriptor::DebugStringWithOptions( if (!package().empty()) { std::vector path; path.push_back(FileDescriptorProto::kPackageFieldNumber); - SourceLocationCommentPrinter package_comment( - this, path, "", debug_string_options); + SourceLocationCommentPrinter package_comment(this, path, "", + debug_string_options); package_comment.AddPreComment(&contents); strings::SubstituteAndAppend(&contents, "package $0;\n\n", package()); package_comment.AddPostComment(&contents); @@ -2458,7 +2470,7 @@ string FileDescriptor::DebugStringWithOptions( contents.append("\n"); } - const Descriptor* containing_type = NULL; + const Descriptor* containing_type = nullptr; for (int i = 0; i < extension_count(); i++) { if (extension(i)->containing_type() != containing_type) { if (i > 0) contents.append("}\n\n"); @@ -2476,31 +2488,30 @@ string FileDescriptor::DebugStringWithOptions( return contents; } -string Descriptor::DebugString() const { +std::string Descriptor::DebugString() const { DebugStringOptions options; // default options return DebugStringWithOptions(options); } -string Descriptor::DebugStringWithOptions( +std::string Descriptor::DebugStringWithOptions( const DebugStringOptions& options) const { - string contents; + std::string contents; DebugString(0, &contents, options, /* include_opening_clause */ true); return contents; } -void Descriptor::DebugString(int depth, string *contents, - const DebugStringOptions& - debug_string_options, +void Descriptor::DebugString(int depth, std::string* contents, + const DebugStringOptions& debug_string_options, bool include_opening_clause) const { if (options().map_entry()) { // Do not generate debug string for auto-generated map-entry type. return; } - string prefix(depth * 2, ' '); + std::string prefix(depth * 2, ' '); ++depth; - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); if (include_opening_clause) { @@ -2535,7 +2546,7 @@ void Descriptor::DebugString(int depth, string *contents, enum_type(i)->DebugString(depth, contents, debug_string_options); } for (int i = 0; i < field_count(); i++) { - if (field(i)->containing_oneof() == NULL) { + if (field(i)->containing_oneof() == nullptr) { field(i)->DebugString(depth, FieldDescriptor::PRINT_LABEL, contents, debug_string_options); } else if (field(i)->containing_oneof()->field(0) == field(i)) { @@ -2546,24 +2557,22 @@ void Descriptor::DebugString(int depth, string *contents, } for (int i = 0; i < extension_range_count(); i++) { - strings::SubstituteAndAppend(contents, "$0 extensions $1 to $2;\n", - prefix, + strings::SubstituteAndAppend(contents, "$0 extensions $1 to $2;\n", prefix, extension_range(i)->start, extension_range(i)->end - 1); } // Group extensions by what they extend, so they can be printed out together. - const Descriptor* containing_type = NULL; + const Descriptor* containing_type = nullptr; for (int i = 0; i < extension_count(); i++) { if (extension(i)->containing_type() != containing_type) { if (i > 0) strings::SubstituteAndAppend(contents, "$0 }\n", prefix); containing_type = extension(i)->containing_type(); - strings::SubstituteAndAppend(contents, "$0 extend .$1 {\n", - prefix, containing_type->full_name()); + strings::SubstituteAndAppend(contents, "$0 extend .$1 {\n", prefix, + containing_type->full_name()); } - extension(i)->DebugString( - depth + 1, FieldDescriptor::PRINT_LABEL, contents, - debug_string_options); + extension(i)->DebugString(depth + 1, FieldDescriptor::PRINT_LABEL, contents, + debug_string_options); } if (extension_count() > 0) strings::SubstituteAndAppend(contents, "$0 }\n", prefix); @@ -2575,8 +2584,8 @@ void Descriptor::DebugString(int depth, string *contents, if (range->end == range->start + 1) { strings::SubstituteAndAppend(contents, "$0, ", range->start); } else { - strings::SubstituteAndAppend(contents, "$0 to $1, ", - range->start, range->end - 1); + strings::SubstituteAndAppend(contents, "$0 to $1, ", range->start, + range->end - 1); } } contents->replace(contents->size() - 2, 2, ";\n"); @@ -2595,14 +2604,14 @@ void Descriptor::DebugString(int depth, string *contents, comment_printer.AddPostComment(contents); } -string FieldDescriptor::DebugString() const { +std::string FieldDescriptor::DebugString() const { DebugStringOptions options; // default options return DebugStringWithOptions(options); } -string FieldDescriptor::DebugStringWithOptions( +std::string FieldDescriptor::DebugStringWithOptions( const DebugStringOptions& debug_string_options) const { - string contents; + std::string contents; int depth = 0; if (is_extension()) { strings::SubstituteAndAppend(&contents, "extend .$0 {\n", @@ -2617,8 +2626,8 @@ string FieldDescriptor::DebugStringWithOptions( } // The field type string used in FieldDescriptor::DebugString() -string FieldDescriptor::FieldTypeNameDebugString() const { - switch(type()) { +std::string FieldDescriptor::FieldTypeNameDebugString() const { + switch (type()) { case TYPE_MESSAGE: return "." + message_type()->full_name(); case TYPE_ENUM: @@ -2628,13 +2637,11 @@ string FieldDescriptor::FieldTypeNameDebugString() const { } } -void FieldDescriptor::DebugString(int depth, - PrintLabelFlag print_label_flag, - string *contents, - const DebugStringOptions& - debug_string_options) const { - string prefix(depth * 2, ' '); - string field_type; +void FieldDescriptor::DebugString( + int depth, PrintLabelFlag print_label_flag, std::string* contents, + const DebugStringOptions& debug_string_options) const { + std::string prefix(depth * 2, ' '); + std::string field_type; // Special case map fields. if (is_map()) { @@ -2656,23 +2663,19 @@ void FieldDescriptor::DebugString(int depth, } else if (is_map()) { print_label = false; } - string label; + std::string label; if (print_label) { label = kLabelToName[this->label()]; label.push_back(' '); } - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); - strings::SubstituteAndAppend(contents, "$0$1$2 $3 = $4", - prefix, - label, - field_type, - type() == TYPE_GROUP ? message_type()->name() : - name(), - number()); + strings::SubstituteAndAppend( + contents, "$0$1$2 $3 = $4", prefix, label, field_type, + type() == TYPE_GROUP ? message_type()->name() : name(), number()); bool bracketed = false; if (has_default_value()) { @@ -2692,7 +2695,7 @@ void FieldDescriptor::DebugString(int depth, contents->append("\""); } - string formatted_options; + std::string formatted_options; if (FormatBracketedOptions(depth, options(), file()->pool(), &formatted_options)) { contents->append(bracketed ? ", " : " ["); @@ -2718,25 +2721,25 @@ void FieldDescriptor::DebugString(int depth, comment_printer.AddPostComment(contents); } -string OneofDescriptor::DebugString() const { +std::string OneofDescriptor::DebugString() const { DebugStringOptions options; // default values return DebugStringWithOptions(options); } -string OneofDescriptor::DebugStringWithOptions( +std::string OneofDescriptor::DebugStringWithOptions( const DebugStringOptions& options) const { - string contents; + std::string contents; DebugString(0, &contents, options); return contents; } -void OneofDescriptor::DebugString(int depth, string* contents, - const DebugStringOptions& - debug_string_options) const { - string prefix(depth * 2, ' '); +void OneofDescriptor::DebugString( + int depth, std::string* contents, + const DebugStringOptions& debug_string_options) const { + std::string prefix(depth * 2, ' '); ++depth; - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); strings::SubstituteAndAppend(contents, "$0oneof $1 {", prefix, name()); @@ -2756,30 +2759,29 @@ void OneofDescriptor::DebugString(int depth, string* contents, comment_printer.AddPostComment(contents); } -string EnumDescriptor::DebugString() const { +std::string EnumDescriptor::DebugString() const { DebugStringOptions options; // default values return DebugStringWithOptions(options); } -string EnumDescriptor::DebugStringWithOptions( +std::string EnumDescriptor::DebugStringWithOptions( const DebugStringOptions& options) const { - string contents; + std::string contents; DebugString(0, &contents, options); return contents; } -void EnumDescriptor::DebugString(int depth, string *contents, - const DebugStringOptions& - debug_string_options) const { - string prefix(depth * 2, ' '); +void EnumDescriptor::DebugString( + int depth, std::string* contents, + const DebugStringOptions& debug_string_options) const { + std::string prefix(depth * 2, ' '); ++depth; - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); - strings::SubstituteAndAppend(contents, "$0enum $1 {\n", - prefix, name()); + strings::SubstituteAndAppend(contents, "$0enum $1 {\n", prefix, name()); FormatLineOptions(depth, options(), file()->pool(), contents); @@ -2794,8 +2796,8 @@ void EnumDescriptor::DebugString(int depth, string *contents, if (range->end == range->start) { strings::SubstituteAndAppend(contents, "$0, ", range->start); } else { - strings::SubstituteAndAppend(contents, "$0 to $1, ", - range->start, range->end); + strings::SubstituteAndAppend(contents, "$0 to $1, ", range->start, + range->end); } } contents->replace(contents->size() - 2, 2, ";\n"); @@ -2815,31 +2817,30 @@ void EnumDescriptor::DebugString(int depth, string *contents, comment_printer.AddPostComment(contents); } -string EnumValueDescriptor::DebugString() const { +std::string EnumValueDescriptor::DebugString() const { DebugStringOptions options; // default values return DebugStringWithOptions(options); } -string EnumValueDescriptor::DebugStringWithOptions( +std::string EnumValueDescriptor::DebugStringWithOptions( const DebugStringOptions& options) const { - string contents; + std::string contents; DebugString(0, &contents, options); return contents; } -void EnumValueDescriptor::DebugString(int depth, string *contents, - const DebugStringOptions& - debug_string_options) const { - string prefix(depth * 2, ' '); +void EnumValueDescriptor::DebugString( + int depth, std::string* contents, + const DebugStringOptions& debug_string_options) const { + std::string prefix(depth * 2, ' '); - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); - strings::SubstituteAndAppend(contents, "$0$1 = $2", - prefix, name(), number()); + strings::SubstituteAndAppend(contents, "$0$1 = $2", prefix, name(), number()); - string formatted_options; + std::string formatted_options; if (FormatBracketedOptions(depth, options(), type()->file()->pool(), &formatted_options)) { strings::SubstituteAndAppend(contents, " [$0]", formatted_options); @@ -2849,23 +2850,23 @@ void EnumValueDescriptor::DebugString(int depth, string *contents, comment_printer.AddPostComment(contents); } -string ServiceDescriptor::DebugString() const { +std::string ServiceDescriptor::DebugString() const { DebugStringOptions options; // default values return DebugStringWithOptions(options); } -string ServiceDescriptor::DebugStringWithOptions( +std::string ServiceDescriptor::DebugStringWithOptions( const DebugStringOptions& options) const { - string contents; + std::string contents; DebugString(&contents, options); return contents; } -void ServiceDescriptor::DebugString(string *contents, - const DebugStringOptions& - debug_string_options) const { - SourceLocationCommentPrinter - comment_printer(this, /* prefix */ "", debug_string_options); +void ServiceDescriptor::DebugString( + std::string* contents, + const DebugStringOptions& debug_string_options) const { + SourceLocationCommentPrinter comment_printer(this, /* prefix */ "", + debug_string_options); comment_printer.AddPreComment(contents); strings::SubstituteAndAppend(contents, "service $0 {\n", name()); @@ -2881,40 +2882,38 @@ void ServiceDescriptor::DebugString(string *contents, comment_printer.AddPostComment(contents); } -string MethodDescriptor::DebugString() const { +std::string MethodDescriptor::DebugString() const { DebugStringOptions options; // default values return DebugStringWithOptions(options); } -string MethodDescriptor::DebugStringWithOptions( +std::string MethodDescriptor::DebugStringWithOptions( const DebugStringOptions& options) const { - string contents; + std::string contents; DebugString(0, &contents, options); return contents; } -void MethodDescriptor::DebugString(int depth, string *contents, - const DebugStringOptions& - debug_string_options) const { - string prefix(depth * 2, ' '); +void MethodDescriptor::DebugString( + int depth, std::string* contents, + const DebugStringOptions& debug_string_options) const { + std::string prefix(depth * 2, ' '); ++depth; - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); - strings::SubstituteAndAppend(contents, "$0rpc $1($4.$2) returns ($5.$3)", - prefix, name(), - input_type()->full_name(), - output_type()->full_name(), - client_streaming() ? "stream " : "", - server_streaming() ? "stream " : ""); + strings::SubstituteAndAppend( + contents, "$0rpc $1($4.$2) returns ($5.$3)", prefix, name(), + input_type()->full_name(), output_type()->full_name(), + client_streaming() ? "stream " : "", server_streaming() ? "stream " : ""); - string formatted_options; + std::string formatted_options; if (FormatLineOptions(depth, options(), service()->file()->pool(), &formatted_options)) { - strings::SubstituteAndAppend(contents, " {\n$0$1}\n", - formatted_options, prefix); + strings::SubstituteAndAppend(contents, " {\n$0$1}\n", formatted_options, + prefix); } else { contents->append(";\n"); } @@ -2930,13 +2929,13 @@ bool FileDescriptor::GetSourceLocation(const std::vector& path, GOOGLE_CHECK(out_location != nullptr); if (source_code_info_) { if (const SourceCodeInfo_Location* loc = - tables_->GetSourceLocation(path, source_code_info_)) { + tables_->GetSourceLocation(path, source_code_info_)) { const RepeatedField& span = loc->span(); if (span.size() == 3 || span.size() == 4) { - out_location->start_line = span.Get(0); + out_location->start_line = span.Get(0); out_location->start_column = span.Get(1); - out_location->end_line = span.Get(span.size() == 3 ? 0 : 2); - out_location->end_column = span.Get(span.size() - 1); + out_location->end_line = span.Get(span.size() == 3 ? 0 : 2); + out_location->end_column = span.Get(span.size() - 1); out_location->leading_comments = loc->leading_comments(); out_location->trailing_comments = loc->trailing_comments(); @@ -2958,9 +2957,9 @@ bool FileDescriptor::GetSourceLocation(SourceLocation* out_location) const { bool FieldDescriptor::is_packed() const { if (!is_packable()) return false; if (file_->syntax() == FileDescriptor::SYNTAX_PROTO2) { - return (options_ != NULL) && options_->packed(); + return (options_ != nullptr) && options_->packed(); } else { - return options_ == NULL || !options_->has_packed() || options_->packed(); + return options_ == nullptr || !options_->has_packed() || options_->packed(); } } @@ -3020,7 +3019,7 @@ void Descriptor::GetLocationPath(std::vector* output) const { void FieldDescriptor::GetLocationPath(std::vector* output) const { if (is_extension()) { - if (extension_scope() == NULL) { + if (extension_scope() == nullptr) { output->push_back(FileDescriptorProto::kExtensionFieldNumber); output->push_back(index()); } else { @@ -3079,7 +3078,7 @@ namespace { // pointers in the original options, not the mutable copy). The Message must be // one of the Options messages in descriptor.proto. struct OptionsToInterpret { - OptionsToInterpret(const string& ns, const string& el, + OptionsToInterpret(const std::string& ns, const std::string& el, const std::vector& path, const Message* orig_opt, Message* opt) : name_scope(ns), @@ -3087,8 +3086,8 @@ struct OptionsToInterpret { element_path(path), original_options(orig_opt), options(opt) {} - string name_scope; - string element_name; + std::string name_scope; + std::string element_name; std::vector element_path; const Message* original_options; Message* options; @@ -3098,8 +3097,7 @@ struct OptionsToInterpret { class DescriptorBuilder { public: - DescriptorBuilder(const DescriptorPool* pool, - DescriptorPool::Tables* tables, + DescriptorBuilder(const DescriptorPool* pool, DescriptorPool::Tables* tables, DescriptorPool::ErrorCollector* error_collector); ~DescriptorBuilder(); @@ -3121,7 +3119,7 @@ class DescriptorBuilder { std::vector options_to_interpret_; bool had_errors_; - string filename_; + std::string filename_; FileDescriptor* file_; FileDescriptorTables* file_tables_; std::set dependencies_; @@ -3138,19 +3136,17 @@ class DescriptorBuilder { // actually found in possible_undeclared_dependency_, which may be a parent // of the symbol actually looked for. const FileDescriptor* possible_undeclared_dependency_; - string possible_undeclared_dependency_name_; + std::string possible_undeclared_dependency_name_; // If LookupSymbol() could resolve a symbol which is not defined, // record the resolved name. This is only used by AddNotDefinedError() // to report a more useful error message. - string undefine_resolved_name_; + std::string undefine_resolved_name_; - void AddError(const string& element_name, - const Message& descriptor, + void AddError(const std::string& element_name, const Message& descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, - const string& error); - void AddError(const string& element_name, - const Message& descriptor, + const std::string& error); + void AddError(const std::string& element_name, const Message& descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, const char* error); void AddRecursiveImportError(const FileDescriptorProto& proto, int from_here); @@ -3160,19 +3156,18 @@ class DescriptorBuilder { // Adds an error indicating that undefined_symbol was not defined. Must // only be called after LookupSymbol() fails. void AddNotDefinedError( - const string& element_name, - const Message& descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - const string& undefined_symbol); + const std::string& element_name, const Message& descriptor, + DescriptorPool::ErrorCollector::ErrorLocation location, + const std::string& undefined_symbol); - void AddWarning(const string& element_name, const Message& descriptor, + void AddWarning(const std::string& element_name, const Message& descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, - const string& error); + const std::string& error); // Silly helper which determines if the given file is in the given package. // I.e., either file->package() == package_name or file->package() is a // nested package within package_name. - bool IsInPackage(const FileDescriptor* file, const string& package_name); + bool IsInPackage(const FileDescriptor* file, const std::string& package_name); // Helper function which finds all public dependencies of the given file, and // stores the them in the dependencies_ set in the builder. @@ -3182,15 +3177,16 @@ class DescriptorBuilder { // - Search the pool's underlay if not found in tables_. // - Insure that the resulting Symbol is from one of the file's declared // dependencies. - Symbol FindSymbol(const string& name, bool build_it = true); + Symbol FindSymbol(const std::string& name, bool build_it = true); // Like FindSymbol() but does not require that the symbol is in one of the // file's declared dependencies. - Symbol FindSymbolNotEnforcingDeps(const string& name, bool build_it = true); + Symbol FindSymbolNotEnforcingDeps(const std::string& name, + bool build_it = true); // This implements the body of FindSymbolNotEnforcingDeps(). Symbol FindSymbolNotEnforcingDepsHelper(const DescriptorPool* pool, - const string& name, + const std::string& name, bool build_it = true); // Like FindSymbol(), but looks up the name relative to some other symbol @@ -3207,10 +3203,8 @@ class DescriptorBuilder { // that LookupSymbol may still return a non-type symbol in LOOKUP_TYPES mode, // if it believes that's all it could refer to. The caller should always // check that it receives the type of symbol it was expecting. - enum ResolveMode { - LOOKUP_ALL, LOOKUP_TYPES - }; - Symbol LookupSymbol(const string& name, const string& relative_to, + enum ResolveMode { LOOKUP_ALL, LOOKUP_TYPES }; + Symbol LookupSymbol(const std::string& name, const std::string& relative_to, DescriptorPool::PlaceholderType placeholder_type = DescriptorPool::PLACEHOLDER_MESSAGE, ResolveMode resolve_mode = LOOKUP_ALL, @@ -3218,29 +3212,28 @@ class DescriptorBuilder { // Like LookupSymbol() but will not return a placeholder even if // AllowUnknownDependencies() has been used. - Symbol LookupSymbolNoPlaceholder(const string& name, - const string& relative_to, + Symbol LookupSymbolNoPlaceholder(const std::string& name, + const std::string& relative_to, ResolveMode resolve_mode = LOOKUP_ALL, bool build_it = true); // Calls tables_->AddSymbol() and records an error if it fails. Returns // true if successful or false if failed, though most callers can ignore // the return value since an error has already been recorded. - bool AddSymbol(const string& full_name, - const void* parent, const string& name, - const Message& proto, Symbol symbol); + bool AddSymbol(const std::string& full_name, const void* parent, + const std::string& name, const Message& proto, Symbol symbol); // Like AddSymbol(), but succeeds if the symbol is already defined as long // as the existing definition is also a package (because it's OK to define // the same package in two different files). Also adds all parents of the // packgae to the symbol table (e.g. AddPackage("foo.bar", ...) will add // "foo.bar" and "foo" to the table). - void AddPackage(const string& name, const Message& proto, + void AddPackage(const std::string& name, const Message& proto, const FileDescriptor* file); // Checks that the symbol name contains only alphanumeric characters and // underscores. Records an error otherwise. - void ValidateSymbolName(const string& name, const string& full_name, + void ValidateSymbolName(const std::string& name, const std::string& full_name, const Message& proto); // Used by BUILD_ARRAY macro (below) to avoid having to have the type @@ -3264,55 +3257,53 @@ class DescriptorBuilder { // Implementation for AllocateOptions(). Don't call this directly. template void AllocateOptionsImpl( - const string& name_scope, const string& element_name, + const std::string& name_scope, const std::string& element_name, const typename DescriptorT::OptionsType& orig_options, DescriptorT* descriptor, const std::vector& options_path); + // Allocate string on the string pool and initialize it to full proto name. + // Full proto name is "scope.proto_name" if scope is non-empty and + // "proto_name" otherwise. + std::string* AllocateNameString(const std::string& scope, + const std::string& proto_name); + // These methods all have the same signature for the sake of the BUILD_ARRAY // macro, below. - void BuildMessage(const DescriptorProto& proto, - const Descriptor* parent, + void BuildMessage(const DescriptorProto& proto, const Descriptor* parent, Descriptor* result); void BuildFieldOrExtension(const FieldDescriptorProto& proto, - const Descriptor* parent, - FieldDescriptor* result, + const Descriptor* parent, FieldDescriptor* result, bool is_extension); - void BuildField(const FieldDescriptorProto& proto, - const Descriptor* parent, + void BuildField(const FieldDescriptorProto& proto, const Descriptor* parent, FieldDescriptor* result) { BuildFieldOrExtension(proto, parent, result, false); } void BuildExtension(const FieldDescriptorProto& proto, - const Descriptor* parent, - FieldDescriptor* result) { + const Descriptor* parent, FieldDescriptor* result) { BuildFieldOrExtension(proto, parent, result, true); } void BuildExtensionRange(const DescriptorProto::ExtensionRange& proto, const Descriptor* parent, Descriptor::ExtensionRange* result); void BuildReservedRange(const DescriptorProto::ReservedRange& proto, - const Descriptor* parent, - Descriptor::ReservedRange* result); + const Descriptor* parent, + Descriptor::ReservedRange* result); void BuildReservedRange(const EnumDescriptorProto::EnumReservedRange& proto, const EnumDescriptor* parent, EnumDescriptor::ReservedRange* result); - void BuildOneof(const OneofDescriptorProto& proto, - Descriptor* parent, + void BuildOneof(const OneofDescriptorProto& proto, Descriptor* parent, OneofDescriptor* result); void CheckEnumValueUniqueness(const EnumDescriptorProto& proto, const EnumDescriptor* result); - void BuildEnum(const EnumDescriptorProto& proto, - const Descriptor* parent, + void BuildEnum(const EnumDescriptorProto& proto, const Descriptor* parent, EnumDescriptor* result); void BuildEnumValue(const EnumValueDescriptorProto& proto, const EnumDescriptor* parent, EnumValueDescriptor* result); - void BuildService(const ServiceDescriptorProto& proto, - const void* dummy, + void BuildService(const ServiceDescriptorProto& proto, const void* dummy, ServiceDescriptor* result); void BuildMethod(const MethodDescriptorProto& proto, - const ServiceDescriptor* parent, - MethodDescriptor* result); + const ServiceDescriptor* parent, MethodDescriptor* result); void LogUnusedDependency(const FileDescriptorProto& proto, const FileDescriptor* result); @@ -3344,8 +3335,8 @@ class DescriptorBuilder { class OptionInterpreter { public: // Creates an interpreter that operates in the context of the pool of the - // specified builder, which must not be NULL. We don't take ownership of the - // builder. + // specified builder, which must not be nullptr. We don't take ownership of + // the builder. explicit OptionInterpreter(DescriptorBuilder* builder); ~OptionInterpreter(); @@ -3386,7 +3377,8 @@ class DescriptorBuilder { intermediate_fields_iter, std::vector::const_iterator intermediate_fields_end, - const FieldDescriptor* innermost_field, const string& debug_msg_name, + const FieldDescriptor* innermost_field, + const std::string& debug_msg_name, const UnknownFieldSet& unknown_fields); // Validates the value for the option field of the currently interpreted @@ -3413,7 +3405,7 @@ class DescriptorBuilder { // A helper function that adds an error at the specified location of the // option we're currently interpreting, and returns false. bool AddOptionError(DescriptorPool::ErrorCollector::ErrorLocation location, - const string& msg) { + const std::string& msg) { builder_->AddError(options_to_interpret_->element_name, *uninterpreted_option_, location, msg); return false; @@ -3421,26 +3413,26 @@ class DescriptorBuilder { // A helper function that adds an error at the location of the option name // and returns false. - bool AddNameError(const string& msg) { + bool AddNameError(const std::string& msg) { return AddOptionError(DescriptorPool::ErrorCollector::OPTION_NAME, msg); } // A helper function that adds an error at the location of the option name // and returns false. - bool AddValueError(const string& msg) { + bool AddValueError(const std::string& msg) { return AddOptionError(DescriptorPool::ErrorCollector::OPTION_VALUE, msg); } - // We interpret against this builder's pool. Is never NULL. We don't own + // We interpret against this builder's pool. Is never nullptr. We don't own // this pointer. DescriptorBuilder* builder_; - // The options we're currently interpreting, or NULL if we're not in a call - // to InterpretOptions. + // The options we're currently interpreting, or nullptr if we're not in a + // call to InterpretOptions. const OptionsToInterpret* options_to_interpret_; // The option we're currently interpreting within options_to_interpret_, or - // NULL if we're not in a call to InterpretOptions(). This points to a + // nullptr if we're not in a call to InterpretOptions(). This points to a // submessage of the original option, not the mutable copy. Therefore we // can use it to find locations recorded by the parser. const UninterpretedOption* uninterpreted_option_; @@ -3482,7 +3474,7 @@ class DescriptorBuilder { return descriptor->is_placeholder_; } static inline void assert_mutex_held(const DescriptorPool* pool) { - if (pool->mutex_ != NULL) { + if (pool->mutex_ != nullptr) { pool->mutex_->AssertHeld(); } } @@ -3507,10 +3499,8 @@ class DescriptorBuilder { const ServiceDescriptorProto& proto); void ValidateMethodOptions(MethodDescriptor* method, const MethodDescriptorProto& proto); - void ValidateProto3(FileDescriptor* file, - const FileDescriptorProto& proto); - void ValidateProto3Message(Descriptor* message, - const DescriptorProto& proto); + void ValidateProto3(FileDescriptor* file, const FileDescriptorProto& proto); + void ValidateProto3Message(Descriptor* message, const DescriptorProto& proto); void ValidateProto3Field(FieldDescriptor* field, const FieldDescriptorProto& proto); void ValidateProto3Enum(EnumDescriptor* enm, @@ -3532,119 +3522,116 @@ class DescriptorBuilder { const FileDescriptor* DescriptorPool::BuildFile( const FileDescriptorProto& proto) { - GOOGLE_CHECK(fallback_database_ == NULL) - << "Cannot call BuildFile on a DescriptorPool that uses a " - "DescriptorDatabase. You must instead find a way to get your file " - "into the underlying database."; - GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. + GOOGLE_CHECK(fallback_database_ == nullptr) + << "Cannot call BuildFile on a DescriptorPool that uses a " + "DescriptorDatabase. You must instead find a way to get your file " + "into the underlying database."; + GOOGLE_CHECK(mutex_ == nullptr); // Implied by the above GOOGLE_CHECK. tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); - return DescriptorBuilder(this, tables_.get(), NULL).BuildFile(proto); + return DescriptorBuilder(this, tables_.get(), nullptr).BuildFile(proto); } const FileDescriptor* DescriptorPool::BuildFileCollectingErrors( - const FileDescriptorProto& proto, - ErrorCollector* error_collector) { - GOOGLE_CHECK(fallback_database_ == NULL) - << "Cannot call BuildFile on a DescriptorPool that uses a " - "DescriptorDatabase. You must instead find a way to get your file " - "into the underlying database."; - GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. + const FileDescriptorProto& proto, ErrorCollector* error_collector) { + GOOGLE_CHECK(fallback_database_ == nullptr) + << "Cannot call BuildFile on a DescriptorPool that uses a " + "DescriptorDatabase. You must instead find a way to get your file " + "into the underlying database."; + GOOGLE_CHECK(mutex_ == nullptr); // Implied by the above GOOGLE_CHECK. tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); - return DescriptorBuilder(this, tables_.get(), - error_collector).BuildFile(proto); + return DescriptorBuilder(this, tables_.get(), error_collector) + .BuildFile(proto); } const FileDescriptor* DescriptorPool::BuildFileFromDatabase( const FileDescriptorProto& proto) const { mutex_->AssertHeld(); if (tables_->known_bad_files_.count(proto.name()) > 0) { - return NULL; + return nullptr; } const FileDescriptor* result = - DescriptorBuilder(this, tables_.get(), - default_error_collector_).BuildFile(proto); - if (result == NULL) { + DescriptorBuilder(this, tables_.get(), default_error_collector_) + .BuildFile(proto); + if (result == nullptr) { tables_->known_bad_files_.insert(proto.name()); } return result; } DescriptorBuilder::DescriptorBuilder( - const DescriptorPool* pool, - DescriptorPool::Tables* tables, + const DescriptorPool* pool, DescriptorPool::Tables* tables, DescriptorPool::ErrorCollector* error_collector) - : pool_(pool), - tables_(tables), - error_collector_(error_collector), - had_errors_(false), - possible_undeclared_dependency_(NULL), - undefine_resolved_name_("") {} + : pool_(pool), + tables_(tables), + error_collector_(error_collector), + had_errors_(false), + possible_undeclared_dependency_(nullptr), + undefine_resolved_name_("") {} DescriptorBuilder::~DescriptorBuilder() {} void DescriptorBuilder::AddError( - const string& element_name, - const Message& descriptor, + const std::string& element_name, const Message& descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, - const string& error) { - if (error_collector_ == NULL) { + const std::string& error) { + if (error_collector_ == nullptr) { if (!had_errors_) { GOOGLE_LOG(ERROR) << "Invalid proto descriptor for file \"" << filename_ << "\":"; } GOOGLE_LOG(ERROR) << " " << element_name << ": " << error; } else { - error_collector_->AddError(filename_, element_name, - &descriptor, location, error); + error_collector_->AddError(filename_, element_name, &descriptor, location, + error); } had_errors_ = true; } void DescriptorBuilder::AddError( - const string& element_name, - const Message& descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - const char* error) { - AddError(element_name, descriptor, location, string(error)); + const std::string& element_name, const Message& descriptor, + DescriptorPool::ErrorCollector::ErrorLocation location, const char* error) { + AddError(element_name, descriptor, location, std::string(error)); } void DescriptorBuilder::AddNotDefinedError( - const string& element_name, - const Message& descriptor, + const std::string& element_name, const Message& descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, - const string& undefined_symbol) { - if (possible_undeclared_dependency_ == NULL && + const std::string& undefined_symbol) { + if (possible_undeclared_dependency_ == nullptr && undefine_resolved_name_.empty()) { AddError(element_name, descriptor, location, "\"" + undefined_symbol + "\" is not defined."); } else { - if (possible_undeclared_dependency_ != NULL) { + if (possible_undeclared_dependency_ != nullptr) { AddError(element_name, descriptor, location, "\"" + possible_undeclared_dependency_name_ + - "\" seems to be defined in \"" + - possible_undeclared_dependency_->name() + "\", which is not " - "imported by \"" + filename_ + "\". To use it here, please " - "add the necessary import."); + "\" seems to be defined in \"" + + possible_undeclared_dependency_->name() + + "\", which is not " + "imported by \"" + + filename_ + + "\". To use it here, please " + "add the necessary import."); } if (!undefine_resolved_name_.empty()) { AddError(element_name, descriptor, location, "\"" + undefined_symbol + "\" is resolved to \"" + - undefine_resolved_name_ + "\", which is not defined. " - "The innermost scope is searched first in name resolution. " - "Consider using a leading '.'(i.e., \"." - + undefined_symbol + - "\") to start from the outermost scope."); + undefine_resolved_name_ + + "\", which is not defined. " + "The innermost scope is searched first in name resolution. " + "Consider using a leading '.'(i.e., \"." + + undefined_symbol + "\") to start from the outermost scope."); } } } void DescriptorBuilder::AddWarning( - const string& element_name, const Message& descriptor, + const std::string& element_name, const Message& descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, - const string& error) { - if (error_collector_ == NULL) { + const std::string& error) { + if (error_collector_ == nullptr) { GOOGLE_LOG(WARNING) << filename_ << " " << element_name << ": " << error; } else { error_collector_->AddWarning(filename_, element_name, &descriptor, location, @@ -3653,27 +3640,27 @@ void DescriptorBuilder::AddWarning( } bool DescriptorBuilder::IsInPackage(const FileDescriptor* file, - const string& package_name) { + const std::string& package_name) { return HasPrefixString(file->package(), package_name) && (file->package().size() == package_name.size() || file->package()[package_name.size()] == '.'); } void DescriptorBuilder::RecordPublicDependencies(const FileDescriptor* file) { - if (file == NULL || !dependencies_.insert(file).second) return; - for (int i = 0; file != NULL && i < file->public_dependency_count(); i++) { + if (file == nullptr || !dependencies_.insert(file).second) return; + for (int i = 0; file != nullptr && i < file->public_dependency_count(); i++) { RecordPublicDependencies(file->public_dependency(i)); } } Symbol DescriptorBuilder::FindSymbolNotEnforcingDepsHelper( - const DescriptorPool* pool, const string& name, bool build_it) { + const DescriptorPool* pool, const std::string& name, bool build_it) { // If we are looking at an underlay, we must lock its mutex_, since we are // accessing the underlay's tables_ directly. - MutexLockMaybe lock((pool == pool_) ? NULL : pool->mutex_); + MutexLockMaybe lock((pool == pool_) ? nullptr : pool->mutex_); Symbol result = pool->tables_->FindSymbol(name); - if (result.IsNull() && pool->underlay_ != NULL) { + if (result.IsNull() && pool->underlay_ != nullptr) { // Symbol not found; check the underlay. result = FindSymbolNotEnforcingDepsHelper(pool->underlay_, name); } @@ -3694,12 +3681,12 @@ Symbol DescriptorBuilder::FindSymbolNotEnforcingDepsHelper( return result; } -Symbol DescriptorBuilder::FindSymbolNotEnforcingDeps(const string& name, +Symbol DescriptorBuilder::FindSymbolNotEnforcingDeps(const std::string& name, bool build_it) { return FindSymbolNotEnforcingDepsHelper(pool_, name, build_it); } -Symbol DescriptorBuilder::FindSymbol(const string& name, bool build_it) { +Symbol DescriptorBuilder::FindSymbol(const std::string& name, bool build_it) { Symbol result = FindSymbolNotEnforcingDeps(name, build_it); if (result.IsNull()) return result; @@ -3729,8 +3716,8 @@ Symbol DescriptorBuilder::FindSymbol(const string& name, bool build_it) { for (std::set::const_iterator it = dependencies_.begin(); it != dependencies_.end(); ++it) { - // Note: A dependency may be NULL if it was not found or had errors. - if (*it != NULL && IsInPackage(*it, name)) return result; + // Note: A dependency may be nullptr if it was not found or had errors. + if (*it != nullptr && IsInPackage(*it, name)) return result; } } @@ -3739,11 +3726,10 @@ Symbol DescriptorBuilder::FindSymbol(const string& name, bool build_it) { return kNullSymbol; } -Symbol DescriptorBuilder::LookupSymbolNoPlaceholder(const string& name, - const string& relative_to, - ResolveMode resolve_mode, - bool build_it) { - possible_undeclared_dependency_ = NULL; +Symbol DescriptorBuilder::LookupSymbolNoPlaceholder( + const std::string& name, const std::string& relative_to, + ResolveMode resolve_mode, bool build_it) { + possible_undeclared_dependency_ = nullptr; undefine_resolved_name_.clear(); if (!name.empty() && name[0] == '.') { @@ -3762,27 +3748,27 @@ Symbol DescriptorBuilder::LookupSymbolNoPlaceholder(const string& name, // } // So, we look for just "Foo" first, then look for "Bar.baz" within it if // found. - string::size_type name_dot_pos = name.find_first_of('.'); - string first_part_of_name; - if (name_dot_pos == string::npos) { + std::string::size_type name_dot_pos = name.find_first_of('.'); + std::string first_part_of_name; + if (name_dot_pos == std::string::npos) { first_part_of_name = name; } else { first_part_of_name = name.substr(0, name_dot_pos); } - string scope_to_try(relative_to); + std::string scope_to_try(relative_to); while (true) { // Chop off the last component of the scope. - string::size_type dot_pos = scope_to_try.find_last_of('.'); - if (dot_pos == string::npos) { + std::string::size_type dot_pos = scope_to_try.find_last_of('.'); + if (dot_pos == std::string::npos) { return FindSymbol(name, build_it); } else { scope_to_try.erase(dot_pos); } // Append ".first_part_of_name" and try to find. - string::size_type old_size = scope_to_try.size(); + std::string::size_type old_size = scope_to_try.size(); scope_to_try.append(1, '.'); scope_to_try.append(first_part_of_name); Symbol result = FindSymbol(scope_to_try, build_it); @@ -3816,7 +3802,7 @@ Symbol DescriptorBuilder::LookupSymbolNoPlaceholder(const string& name, } Symbol DescriptorBuilder::LookupSymbol( - const string& name, const string& relative_to, + const std::string& name, const std::string& relative_to, DescriptorPool::PlaceholderType placeholder_type, ResolveMode resolve_mode, bool build_it) { Symbol result = @@ -3829,7 +3815,7 @@ Symbol DescriptorBuilder::LookupSymbol( return result; } -static bool ValidateQualifiedName(const string& name) { +static bool ValidateQualifiedName(const std::string& name) { bool last_was_period = false; for (int i = 0; i < name.size(); i++) { @@ -3849,21 +3835,21 @@ static bool ValidateQualifiedName(const string& name) { return !name.empty() && !last_was_period; } -Symbol DescriptorPool::NewPlaceholder(const string& name, +Symbol DescriptorPool::NewPlaceholder(const std::string& name, PlaceholderType placeholder_type) const { MutexLockMaybe lock(mutex_); return NewPlaceholderWithMutexHeld(name, placeholder_type); } Symbol DescriptorPool::NewPlaceholderWithMutexHeld( - const string& name, PlaceholderType placeholder_type) const { + const std::string& name, PlaceholderType placeholder_type) const { if (mutex_) { mutex_->AssertHeld(); } // Compute names. - const string* placeholder_full_name; - const string* placeholder_name; - const string* placeholder_package; + const std::string* placeholder_full_name; + const std::string* placeholder_name; + const std::string* placeholder_package; if (!ValidateQualifiedName(name)) return kNullSymbol; if (name[0] == '.') { @@ -3873,12 +3859,12 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( placeholder_full_name = tables_->AllocateString(name); } - string::size_type dotpos = placeholder_full_name->find_last_of('.'); - if (dotpos != string::npos) { - placeholder_package = tables_->AllocateString( - placeholder_full_name->substr(0, dotpos)); - placeholder_name = tables_->AllocateString( - placeholder_full_name->substr(dotpos + 1)); + std::string::size_type dotpos = placeholder_full_name->find_last_of('.'); + if (dotpos != std::string::npos) { + placeholder_package = + tables_->AllocateString(placeholder_full_name->substr(0, dotpos)); + placeholder_name = + tables_->AllocateString(placeholder_full_name->substr(dotpos + 1)); } else { placeholder_package = &internal::GetEmptyString(); placeholder_name = placeholder_full_name; @@ -3891,11 +3877,10 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( if (placeholder_type == PLACEHOLDER_ENUM) { placeholder_file->enum_type_count_ = 1; - placeholder_file->enum_types_ = - tables_->AllocateArray(1); + placeholder_file->enum_types_ = tables_->AllocateArray(1); EnumDescriptor* placeholder_enum = &placeholder_file->enum_types_[0]; - memset(placeholder_enum, 0, sizeof(*placeholder_enum)); + memset(static_cast(placeholder_enum), 0, sizeof(*placeholder_enum)); placeholder_enum->full_name_ = placeholder_full_name; placeholder_enum->name_ = placeholder_name; @@ -3909,13 +3894,16 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( placeholder_enum->values_ = tables_->AllocateArray(1); EnumValueDescriptor* placeholder_value = &placeholder_enum->values_[0]; - memset(placeholder_value, 0, sizeof(*placeholder_value)); + memset(static_cast(placeholder_value), 0, + sizeof(*placeholder_value)); placeholder_value->name_ = tables_->AllocateString("PLACEHOLDER_VALUE"); // Note that enum value names are siblings of their type, not children. placeholder_value->full_name_ = - placeholder_package->empty() ? placeholder_value->name_ : - tables_->AllocateString(*placeholder_package + ".PLACEHOLDER_VALUE"); + placeholder_package->empty() + ? placeholder_value->name_ + : tables_->AllocateString(*placeholder_package + + ".PLACEHOLDER_VALUE"); placeholder_value->number_ = 0; placeholder_value->type_ = placeholder_enum; @@ -3924,11 +3912,11 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( return Symbol(placeholder_enum); } else { placeholder_file->message_type_count_ = 1; - placeholder_file->message_types_ = - tables_->AllocateArray(1); + placeholder_file->message_types_ = tables_->AllocateArray(1); Descriptor* placeholder_message = &placeholder_file->message_types_[0]; - memset(placeholder_message, 0, sizeof(*placeholder_message)); + memset(static_cast(placeholder_message), 0, + sizeof(*placeholder_message)); placeholder_message->full_name_ = placeholder_full_name; placeholder_message->name_ = placeholder_name; @@ -3940,29 +3928,30 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( if (placeholder_type == PLACEHOLDER_EXTENDABLE_MESSAGE) { placeholder_message->extension_range_count_ = 1; placeholder_message->extension_ranges_ = - tables_->AllocateArray(1); + tables_->AllocateArray(1); placeholder_message->extension_ranges_->start = 1; // kMaxNumber + 1 because ExtensionRange::end is exclusive. placeholder_message->extension_ranges_->end = - FieldDescriptor::kMaxNumber + 1; + FieldDescriptor::kMaxNumber + 1; } return Symbol(placeholder_message); } } -FileDescriptor* DescriptorPool::NewPlaceholderFile(const string& name) const { +FileDescriptor* DescriptorPool::NewPlaceholderFile( + const std::string& name) const { MutexLockMaybe lock(mutex_); return NewPlaceholderFileWithMutexHeld(name); } FileDescriptor* DescriptorPool::NewPlaceholderFileWithMutexHeld( - const string& name) const { + const std::string& name) const { if (mutex_) { mutex_->AssertHeld(); } FileDescriptor* placeholder = tables_->Allocate(); - memset(placeholder, 0, sizeof(*placeholder)); + memset(static_cast(placeholder), 0, sizeof(*placeholder)); placeholder->name_ = tables_->AllocateString(name); placeholder->package_ = &internal::GetEmptyString(); @@ -3973,24 +3962,25 @@ FileDescriptor* DescriptorPool::NewPlaceholderFileWithMutexHeld( placeholder->is_placeholder_ = true; placeholder->syntax_ = FileDescriptor::SYNTAX_PROTO2; placeholder->finished_building_ = true; - // All other fields are zero or NULL. + // All other fields are zero or nullptr. return placeholder; } -bool DescriptorBuilder::AddSymbol( - const string& full_name, const void* parent, const string& name, - const Message& proto, Symbol symbol) { - // If the caller passed NULL for the parent, the symbol is at file scope. +bool DescriptorBuilder::AddSymbol(const std::string& full_name, + const void* parent, const std::string& name, + const Message& proto, Symbol symbol) { + // If the caller passed nullptr for the parent, the symbol is at file scope. // Use its file as the parent instead. - if (parent == NULL) parent = file_; + if (parent == nullptr) parent = file_; if (tables_->AddSymbol(full_name, symbol)) { if (!file_tables_->AddAliasUnderParent(parent, name, symbol)) { // This is only possible if there was already an error adding something of // the same name. if (!had_errors_) { - GOOGLE_LOG(DFATAL) << "\"" << full_name << "\" not previously defined in " + GOOGLE_LOG(DFATAL) << "\"" << full_name + << "\" not previously defined in " "symbols_by_name_, but was defined in " "symbols_by_parent_; this shouldn't be possible."; } @@ -4000,37 +3990,39 @@ bool DescriptorBuilder::AddSymbol( } else { const FileDescriptor* other_file = tables_->FindSymbol(full_name).GetFile(); if (other_file == file_) { - string::size_type dot_pos = full_name.find_last_of('.'); - if (dot_pos == string::npos) { + std::string::size_type dot_pos = full_name.find_last_of('.'); + if (dot_pos == std::string::npos) { AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, "\"" + full_name + "\" is already defined."); } else { AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, "\"" + full_name.substr(dot_pos + 1) + - "\" is already defined in \"" + - full_name.substr(0, dot_pos) + "\"."); + "\" is already defined in \"" + + full_name.substr(0, dot_pos) + "\"."); } } else { // Symbol seems to have been defined in a different file. AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, "\"" + full_name + "\" is already defined in file \"" + - other_file->name() + "\"."); + other_file->name() + "\"."); } return false; } } -void DescriptorBuilder::AddPackage( - const string& name, const Message& proto, const FileDescriptor* file) { +void DescriptorBuilder::AddPackage(const std::string& name, + const Message& proto, + const FileDescriptor* file) { if (tables_->AddSymbol(name, Symbol(file))) { // Success. Also add parent package, if any. - string::size_type dot_pos = name.find_last_of('.'); - if (dot_pos == string::npos) { + std::string::size_type dot_pos = name.find_last_of('.'); + if (dot_pos == std::string::npos) { // No parents. ValidateSymbolName(name, name, proto); } else { // Has parent. - string* parent_name = tables_->AllocateString(name.substr(0, dot_pos)); + std::string* parent_name = + tables_->AllocateString(name.substr(0, dot_pos)); AddPackage(*parent_name, proto, file); ValidateSymbolName(name.substr(dot_pos + 1), name, proto); } @@ -4040,15 +4032,17 @@ void DescriptorBuilder::AddPackage( if (existing_symbol.type != Symbol::PACKAGE) { // Symbol seems to have been defined in a different file. AddError(name, proto, DescriptorPool::ErrorCollector::NAME, - "\"" + name + "\" is already defined (as something other than " - "a package) in file \"" + existing_symbol.GetFile()->name() + - "\"."); + "\"" + name + + "\" is already defined (as something other than " + "a package) in file \"" + + existing_symbol.GetFile()->name() + "\"."); } } } -void DescriptorBuilder::ValidateSymbolName( - const string& name, const string& full_name, const Message& proto) { +void DescriptorBuilder::ValidateSymbolName(const std::string& name, + const std::string& full_name, + const Message& proto) { if (name.empty()) { AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, "Missing name."); @@ -4057,8 +4051,7 @@ void DescriptorBuilder::ValidateSymbolName( // I don't trust isalnum() due to locales. :( if ((name[i] < 'a' || 'z' < name[i]) && (name[i] < 'A' || 'Z' < name[i]) && - (name[i] < '0' || '9' < name[i]) && - (name[i] != '_')) { + (name[i] < '0' || '9' < name[i]) && (name[i] != '_')) { AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, "\"" + name + "\" is not a valid identifier."); } @@ -4093,14 +4086,14 @@ void DescriptorBuilder::AllocateOptions(const FileOptions& orig_options, template void DescriptorBuilder::AllocateOptionsImpl( - const string& name_scope, const string& element_name, + const std::string& name_scope, const std::string& element_name, const typename DescriptorT::OptionsType& orig_options, DescriptorT* descriptor, const std::vector& options_path) { // We need to use a dummy pointer to work around a bug in older versions of // GCC. Otherwise, the following two lines could be replaced with: // typename DescriptorT::OptionsType* options = // tables_->AllocateMessage(); - typename DescriptorT::OptionsType* const dummy = NULL; + typename DescriptorT::OptionsType* const dummy = nullptr; typename DescriptorT::OptionsType* options = tables_->AllocateMessage(dummy); if (!orig_options.IsInitialized()) { @@ -4132,43 +4125,49 @@ void DescriptorBuilder::AllocateOptionsImpl( // A common pattern: We want to convert a repeated field in the descriptor // to an array of values, calling some method to build each value. -#define BUILD_ARRAY(INPUT, OUTPUT, NAME, METHOD, PARENT) \ - OUTPUT->NAME##_count_ = INPUT.NAME##_size(); \ - AllocateArray(INPUT.NAME##_size(), &OUTPUT->NAME##s_); \ - for (int i = 0; i < INPUT.NAME##_size(); i++) { \ - METHOD(INPUT.NAME(i), PARENT, OUTPUT->NAME##s_ + i); \ +#define BUILD_ARRAY(INPUT, OUTPUT, NAME, METHOD, PARENT) \ + OUTPUT->NAME##_count_ = INPUT.NAME##_size(); \ + AllocateArray(INPUT.NAME##_size(), &OUTPUT->NAME##s_); \ + for (int i = 0; i < INPUT.NAME##_size(); i++) { \ + METHOD(INPUT.NAME(i), PARENT, OUTPUT->NAME##s_ + i); \ } void DescriptorBuilder::AddRecursiveImportError( const FileDescriptorProto& proto, int from_here) { - string error_message("File recursively imports itself: "); + std::string error_message("File recursively imports itself: "); for (int i = from_here; i < tables_->pending_files_.size(); i++) { error_message.append(tables_->pending_files_[i]); error_message.append(" -> "); } error_message.append(proto.name()); - AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, - error_message); + if (from_here < tables_->pending_files_.size() - 1) { + AddError(tables_->pending_files_[from_here + 1], proto, + DescriptorPool::ErrorCollector::IMPORT, error_message); + } else { + AddError(proto.name(), proto, DescriptorPool::ErrorCollector::IMPORT, + error_message); + } } void DescriptorBuilder::AddTwiceListedError(const FileDescriptorProto& proto, int index) { - AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, + AddError(proto.dependency(index), proto, + DescriptorPool::ErrorCollector::IMPORT, "Import \"" + proto.dependency(index) + "\" was listed twice."); } void DescriptorBuilder::AddImportError(const FileDescriptorProto& proto, int index) { - string message; - if (pool_->fallback_database_ == NULL) { - message = "Import \"" + proto.dependency(index) + - "\" has not been loaded."; + std::string message; + if (pool_->fallback_database_ == nullptr) { + message = "Import \"" + proto.dependency(index) + "\" has not been loaded."; } else { message = "Import \"" + proto.dependency(index) + "\" was not found or had errors."; } - AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, message); + AddError(proto.dependency(index), proto, + DescriptorPool::ErrorCollector::IMPORT, message); } static bool ExistingFileMatchesProto(const FileDescriptor* existing_file, @@ -4196,7 +4195,7 @@ const FileDescriptor* DescriptorBuilder::BuildFile( // This is fine, because this idempotency "feature" really only exists to // accommodate one hack in the proto1->proto2 migration layer. const FileDescriptor* existing_file = tables_->FindFile(filename_); - if (existing_file != NULL) { + if (existing_file != nullptr) { // File already in pool. Compare the existing one to the input. if (ExistingFileMatchesProto(existing_file, proto)) { // They're identical. Return the existing descriptor. @@ -4218,7 +4217,7 @@ const FileDescriptor* DescriptorBuilder::BuildFile( for (int i = 0; i < tables_->pending_files_.size(); i++) { if (tables_->pending_files_[i] == proto.name()) { AddRecursiveImportError(proto, i); - return NULL; + return nullptr; } } @@ -4226,12 +4225,13 @@ const FileDescriptor* DescriptorBuilder::BuildFile( // attempt to load all dependencies now, before checkpointing tables_. This // avoids confusion with recursive checkpoints. if (!pool_->lazily_build_dependencies_) { - if (pool_->fallback_database_ != NULL) { + if (pool_->fallback_database_ != nullptr) { tables_->pending_files_.push_back(proto.name()); for (int i = 0; i < proto.dependency_size(); i++) { - if (tables_->FindFile(proto.dependency(i)) == NULL && - (pool_->underlay_ == NULL || - pool_->underlay_->FindFileByName(proto.dependency(i)) == NULL)) { + if (tables_->FindFile(proto.dependency(i)) == nullptr && + (pool_->underlay_ == nullptr || + pool_->underlay_->FindFileByName(proto.dependency(i)) == + nullptr)) { // We don't care what this returns since we'll find out below anyway. pool_->TryFindFileInFallbackDatabase(proto.dependency(i)); } @@ -4310,28 +4310,28 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( "A file with this name is already in the pool."); // Bail out early so that if this is actually the exact same file, we // don't end up reporting that every single symbol is already defined. - return NULL; + return nullptr; } if (!result->package().empty()) { AddPackage(result->package(), proto, result); } // Make sure all dependencies are loaded. - std::set seen_dependencies; + std::set seen_dependencies; result->dependency_count_ = proto.dependency_size(); result->dependencies_ = tables_->AllocateArray(proto.dependency_size()); if (pool_->lazily_build_dependencies_) { result->dependencies_once_ = tables_->AllocateOnceDynamic(); result->dependencies_names_ = - tables_->AllocateArray(proto.dependency_size()); + tables_->AllocateArray(proto.dependency_size()); if (proto.dependency_size() > 0) { memset(result->dependencies_names_, 0, sizeof(*result->dependencies_names_) * proto.dependency_size()); } } else { - result->dependencies_once_ = NULL; - result->dependencies_names_ = NULL; + result->dependencies_once_ = nullptr; + result->dependencies_names_ = nullptr; } unused_dependency_.clear(); std::set weak_deps; @@ -4344,7 +4344,7 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( } const FileDescriptor* dependency = tables_->FindFile(proto.dependency(i)); - if (dependency == NULL && pool_->underlay_ != NULL) { + if (dependency == nullptr && pool_->underlay_ != nullptr) { dependency = pool_->underlay_->FindFileByName(proto.dependency(i)); } @@ -4352,10 +4352,10 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( // Recursive import. dependency/result is not fully initialized, and it's // dangerous to try to do anything with it. The recursive import error // will be detected and reported in DescriptorBuilder::BuildFile(). - return NULL; + return nullptr; } - if (dependency == NULL) { + if (dependency == nullptr) { if (!pool_->lazily_build_dependencies_) { if (pool_->allow_unknown_ || (!pool_->enforce_weak_ && weak_deps.find(i) != weak_deps.end())) { @@ -4385,8 +4385,8 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( // Check public dependencies. int public_dependency_count = 0; - result->public_dependencies_ = tables_->AllocateArray( - proto.public_dependency_size()); + result->public_dependencies_ = + tables_->AllocateArray(proto.public_dependency_size()); for (int i = 0; i < proto.public_dependency_size(); i++) { // Only put valid public dependency indexes. int index = proto.public_dependency(i); @@ -4400,8 +4400,7 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( unused_dependency_.erase(result->dependency(index)); } } else { - AddError(proto.name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, "Invalid public dependency index."); } } @@ -4420,29 +4419,28 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( // Check weak dependencies. int weak_dependency_count = 0; - result->weak_dependencies_ = tables_->AllocateArray( - proto.weak_dependency_size()); + result->weak_dependencies_ = + tables_->AllocateArray(proto.weak_dependency_size()); for (int i = 0; i < proto.weak_dependency_size(); i++) { int index = proto.weak_dependency(i); if (index >= 0 && index < proto.dependency_size()) { result->weak_dependencies_[weak_dependency_count++] = index; } else { - AddError(proto.name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, "Invalid weak dependency index."); } } result->weak_dependency_count_ = weak_dependency_count; // Convert children. - BUILD_ARRAY(proto, result, message_type, BuildMessage , NULL); - BUILD_ARRAY(proto, result, enum_type , BuildEnum , NULL); - BUILD_ARRAY(proto, result, service , BuildService , NULL); - BUILD_ARRAY(proto, result, extension , BuildExtension, NULL); + BUILD_ARRAY(proto, result, message_type, BuildMessage, nullptr); + BUILD_ARRAY(proto, result, enum_type, BuildEnum, nullptr); + BUILD_ARRAY(proto, result, service, BuildService, nullptr); + BUILD_ARRAY(proto, result, extension, BuildExtension, nullptr); // Copy options. if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { AllocateOptions(proto.options(), result); } @@ -4490,44 +4488,54 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( } if (had_errors_) { - return NULL; + return nullptr; } else { return result; } } + +std::string* DescriptorBuilder::AllocateNameString( + const std::string& scope, const std::string& proto_name) { + std::string* full_name; + if (scope.empty()) { + full_name = tables_->AllocateString(proto_name); + } else { + full_name = tables_->AllocateEmptyString(); + *full_name = StrCat(scope, ".", proto_name); + } + return full_name; +} + void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, const Descriptor* parent, Descriptor* result) { - const string& scope = (parent == NULL) ? - file_->package() : parent->full_name(); - string* full_name = tables_->AllocateString(scope); - if (!full_name->empty()) full_name->append(1, '.'); - full_name->append(proto.name()); - + const std::string& scope = + (parent == nullptr) ? file_->package() : parent->full_name(); + std::string* full_name = AllocateNameString(scope, proto.name()); ValidateSymbolName(proto.name(), *full_name, proto); - result->name_ = tables_->AllocateString(proto.name()); - result->full_name_ = full_name; - result->file_ = file_; + result->name_ = tables_->AllocateString(proto.name()); + result->full_name_ = full_name; + result->file_ = file_; result->containing_type_ = parent; - result->is_placeholder_ = false; + result->is_placeholder_ = false; result->is_unqualified_placeholder_ = false; // Build oneofs first so that fields and extension ranges can refer to them. - BUILD_ARRAY(proto, result, oneof_decl , BuildOneof , result); - BUILD_ARRAY(proto, result, field , BuildField , result); - BUILD_ARRAY(proto, result, nested_type , BuildMessage , result); - BUILD_ARRAY(proto, result, enum_type , BuildEnum , result); + BUILD_ARRAY(proto, result, oneof_decl, BuildOneof, result); + BUILD_ARRAY(proto, result, field, BuildField, result); + BUILD_ARRAY(proto, result, nested_type, BuildMessage, result); + BUILD_ARRAY(proto, result, enum_type, BuildEnum, result); BUILD_ARRAY(proto, result, extension_range, BuildExtensionRange, result); - BUILD_ARRAY(proto, result, extension , BuildExtension , result); - BUILD_ARRAY(proto, result, reserved_range , BuildReservedRange , result); + BUILD_ARRAY(proto, result, extension, BuildExtension, result); + BUILD_ARRAY(proto, result, reserved_range, BuildReservedRange, result); // Copy reserved names. int reserved_name_count = proto.reserved_name_size(); result->reserved_name_count_ = reserved_name_count; result->reserved_names_ = - tables_->AllocateArray(reserved_name_count); + tables_->AllocateArray(reserved_name_count); for (int i = 0; i < reserved_name_count; ++i) { result->reserved_names_[i] = tables_->AllocateString(proto.reserved_name(i)); @@ -4535,14 +4543,14 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, // Copy options. if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { AllocateOptions(proto.options(), result, DescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); + for (int i = 0; i < proto.reserved_range_size(); i++) { const DescriptorProto_ReservedRange& range1 = proto.reserved_range(i); @@ -4559,16 +4567,15 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, } } - HASH_SET reserved_name_set; + HASH_SET reserved_name_set; for (int i = 0; i < proto.reserved_name_size(); i++) { - const string& name = proto.reserved_name(i); + const std::string& name = proto.reserved_name(i); if (reserved_name_set.find(name) == reserved_name_set.end()) { reserved_name_set.insert(name); } else { AddError(name, proto, DescriptorPool::ErrorCollector::NAME, strings::Substitute( - "Field name \"$0\" is reserved multiple times.", - name)); + "Field name \"$0\" is reserved multiple times.", name)); } } @@ -4577,12 +4584,12 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, for (int j = 0; j < result->extension_range_count(); j++) { const Descriptor::ExtensionRange* range = result->extension_range(j); if (range->start <= field->number() && field->number() < range->end) { - AddError(field->full_name(), proto.extension_range(j), - DescriptorPool::ErrorCollector::NUMBER, - strings::Substitute( - "Extension range $0 to $1 includes field \"$2\" ($3).", - range->start, range->end - 1, - field->name(), field->number())); + AddError( + field->full_name(), proto.extension_range(j), + DescriptorPool::ErrorCollector::NUMBER, + strings::Substitute( + "Extension range $0 to $1 includes field \"$2\" ($3).", + range->start, range->end - 1, field->name(), field->number())); } } for (int j = 0; j < result->reserved_range_count(); j++) { @@ -4590,16 +4597,15 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, if (range->start <= field->number() && field->number() < range->end) { AddError(field->full_name(), proto.reserved_range(j), DescriptorPool::ErrorCollector::NUMBER, - strings::Substitute( - "Field \"$0\" uses reserved number $1.", - field->name(), field->number())); + strings::Substitute("Field \"$0\" uses reserved number $1.", + field->name(), field->number())); } } if (reserved_name_set.find(field->name()) != reserved_name_set.end()) { - AddError(field->full_name(), proto.field(i), - DescriptorPool::ErrorCollector::NAME, - strings::Substitute( - "Field name \"$0\" is reserved.", field->name())); + AddError( + field->full_name(), proto.field(i), + DescriptorPool::ErrorCollector::NAME, + strings::Substitute("Field name \"$0\" is reserved.", field->name())); } } @@ -4632,29 +4638,25 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, } } - void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, const Descriptor* parent, FieldDescriptor* result, bool is_extension) { - const string& scope = (parent == NULL) ? - file_->package() : parent->full_name(); - string* full_name = tables_->AllocateString(scope); - if (!full_name->empty()) full_name->append(1, '.'); - full_name->append(proto.name()); - + const std::string& scope = + (parent == nullptr) ? file_->package() : parent->full_name(); + std::string* full_name = AllocateNameString(scope, proto.name()); ValidateSymbolName(proto.name(), *full_name, proto); - result->name_ = tables_->AllocateString(proto.name()); - result->full_name_ = full_name; - result->file_ = file_; - result->number_ = proto.number(); + result->name_ = tables_->AllocateString(proto.name()); + result->full_name_ = full_name; + result->file_ = file_; + result->number_ = proto.number(); result->is_extension_ = is_extension; // If .proto files follow the style guide then the name should already be - // lower-cased. If that's the case we can just reuse the string we already - // allocated rather than allocate a new one. - string lowercase_name(proto.name()); + // lower-cased. If that's the case we can just reuse the string we + // already allocated rather than allocate a new one. + std::string lowercase_name(proto.name()); LowerString(&lowercase_name); if (lowercase_name == proto.name()) { result->lowercase_name_ = result->name_; @@ -4680,9 +4682,9 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, // Some compilers do not allow static_cast directly between two enum types, // so we must cast to int first. result->type_ = static_cast( - ::google::protobuf::implicit_cast(proto.type())); + implicit_cast(proto.type())); result->label_ = static_cast( - ::google::protobuf::implicit_cast(proto.label())); + implicit_cast(proto.label())); // An extension cannot have a required field (b/13365836). if (result->is_extension_ && @@ -4698,14 +4700,14 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, } // Some of these may be filled in when cross-linking. - result->containing_type_ = NULL; - result->extension_scope_ = NULL; - result->message_type_ = NULL; - result->enum_type_ = NULL; - result->type_name_ = NULL; - result->type_once_ = NULL; - result->default_value_enum_ = NULL; - result->default_value_enum_name_ = NULL; + result->containing_type_ = nullptr; + result->extension_scope_ = nullptr; + result->message_type_ = nullptr; + result->enum_type_ = nullptr; + result->type_name_ = nullptr; + result->type_once_ = nullptr; + result->default_value_enum_ = nullptr; + result->default_value_enum_name_ = nullptr; result->has_default_value_ = proto.has_default_value(); if (proto.has_default_value() && result->is_repeated()) { @@ -4716,23 +4718,23 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, if (proto.has_type()) { if (proto.has_default_value()) { - char* end_pos = NULL; + char* end_pos = nullptr; switch (result->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: result->default_value_int32_ = - strtol(proto.default_value().c_str(), &end_pos, 0); + strtol(proto.default_value().c_str(), &end_pos, 0); break; case FieldDescriptor::CPPTYPE_INT64: result->default_value_int64_ = - strto64(proto.default_value().c_str(), &end_pos, 0); + strto64(proto.default_value().c_str(), &end_pos, 0); break; case FieldDescriptor::CPPTYPE_UINT32: result->default_value_uint32_ = - strtoul(proto.default_value().c_str(), &end_pos, 0); + strtoul(proto.default_value().c_str(), &end_pos, 0); break; case FieldDescriptor::CPPTYPE_UINT64: result->default_value_uint64_ = - strtou64(proto.default_value().c_str(), &end_pos, 0); + strtou64(proto.default_value().c_str(), &end_pos, 0); break; case FieldDescriptor::CPPTYPE_FLOAT: if (proto.default_value() == "inf") { @@ -4744,7 +4746,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, } else if (proto.default_value() == "nan") { result->default_value_float_ = std::numeric_limits::quiet_NaN(); - } else { + } else { result->default_value_float_ = io::SafeDoubleToFloat( io::NoLocaleStrtod(proto.default_value().c_str(), &end_pos)); } @@ -4759,7 +4761,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, } else if (proto.default_value() == "nan") { result->default_value_double_ = std::numeric_limits::quiet_NaN(); - } else { + } else { result->default_value_double_ = io::NoLocaleStrtod(proto.default_value().c_str(), &end_pos); } @@ -4777,12 +4779,12 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, break; case FieldDescriptor::CPPTYPE_ENUM: // This will be filled in when cross-linking. - result->default_value_enum_ = NULL; + result->default_value_enum_ = nullptr; break; case FieldDescriptor::CPPTYPE_STRING: if (result->type() == FieldDescriptor::TYPE_BYTES) { result->default_value_string_ = tables_->AllocateString( - UnescapeCEscapeString(proto.default_value())); + UnescapeCEscapeString(proto.default_value())); } else { result->default_value_string_ = tables_->AllocateString(proto.default_value()); @@ -4796,15 +4798,15 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, break; } - if (end_pos != NULL) { - // end_pos is only set non-NULL by the parsers for numeric types, above. - // This checks that the default was non-empty and had no extra junk - // after the end of the number. + if (end_pos != nullptr) { + // end_pos is only set non-null by the parsers for numeric types, + // above. This checks that the default was non-empty and had no extra + // junk after the end of the number. if (proto.default_value().empty() || *end_pos != '\0') { AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::DEFAULT_VALUE, "Couldn't parse default value \"" + proto.default_value() + - "\"."); + "\"."); } } } else { @@ -4833,7 +4835,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, break; case FieldDescriptor::CPPTYPE_ENUM: // This will be filled in when cross-linking. - result->default_value_enum_ = NULL; + result->default_value_enum_ = nullptr; break; case FieldDescriptor::CPPTYPE_STRING: result->default_value_string_ = &internal::GetEmptyString(); @@ -4863,10 +4865,10 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, result->number() <= FieldDescriptor::kLastReservedNumber) { AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, strings::Substitute( - "Field numbers $0 through $1 are reserved for the protocol " - "buffer library implementation.", - FieldDescriptor::kFirstReservedNumber, - FieldDescriptor::kLastReservedNumber)); + "Field numbers $0 through $1 are reserved for the protocol " + "buffer library implementation.", + FieldDescriptor::kFirstReservedNumber, + FieldDescriptor::kLastReservedNumber)); } if (is_extension) { @@ -4879,14 +4881,13 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, result->extension_scope_ = parent; if (proto.has_oneof_index()) { - AddError(result->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "FieldDescriptorProto.oneof_index should not be set for " "extensions."); } // Fill in later (maybe). - result->containing_oneof_ = NULL; + result->containing_oneof_ = nullptr; } else { if (proto.has_extendee()) { AddError(result->full_name(), proto, @@ -4900,42 +4901,38 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, if (proto.oneof_index() < 0 || proto.oneof_index() >= parent->oneof_decl_count()) { AddError(result->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + DescriptorPool::ErrorCollector::TYPE, strings::Substitute("FieldDescriptorProto.oneof_index $0 is " "out of range for type \"$1\".", - proto.oneof_index(), - parent->name())); - result->containing_oneof_ = NULL; + proto.oneof_index(), parent->name())); + result->containing_oneof_ = nullptr; } else { result->containing_oneof_ = parent->oneof_decl(proto.oneof_index()); } } else { - result->containing_oneof_ = NULL; + result->containing_oneof_ = nullptr; } } // Copy options. if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { AllocateOptions(proto.options(), result, FieldDescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); } void DescriptorBuilder::BuildExtensionRange( - const DescriptorProto::ExtensionRange& proto, - const Descriptor* parent, + const DescriptorProto::ExtensionRange& proto, const Descriptor* parent, Descriptor::ExtensionRange* result) { result->start = proto.start(); result->end = proto.end(); if (result->start <= 0) { - AddError(parent->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, + AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, "Extension numbers must be positive integers."); } @@ -4945,13 +4942,12 @@ void DescriptorBuilder::BuildExtensionRange( // numbers are actually used as int32s in the message_set_wire_format. if (result->start >= result->end) { - AddError(parent->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, + AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, "Extension range end number must be greater than start number."); } if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { std::vector options_path; parent->GetLocationPath(&options_path); @@ -4968,14 +4964,12 @@ void DescriptorBuilder::BuildExtensionRange( } void DescriptorBuilder::BuildReservedRange( - const DescriptorProto::ReservedRange& proto, - const Descriptor* parent, + const DescriptorProto::ReservedRange& proto, const Descriptor* parent, Descriptor::ReservedRange* result) { result->start = proto.start(); result->end = proto.end(); if (result->start <= 0) { - AddError(parent->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, + AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, "Reserved numbers must be positive integers."); } } @@ -4987,8 +4981,7 @@ void DescriptorBuilder::BuildReservedRange( result->end = proto.end(); if (result->start > result->end) { - AddError(parent->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, + AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, "Reserved range end number must be greater than start number."); } } @@ -4996,10 +4989,8 @@ void DescriptorBuilder::BuildReservedRange( void DescriptorBuilder::BuildOneof(const OneofDescriptorProto& proto, Descriptor* parent, OneofDescriptor* result) { - string* full_name = tables_->AllocateString(parent->full_name()); - full_name->append(1, '.'); - full_name->append(proto.name()); - + std::string* full_name = + AllocateNameString(parent->full_name(), proto.name()); ValidateSymbolName(proto.name(), *full_name, proto); result->name_ = tables_->AllocateString(proto.name()); @@ -5009,18 +5000,17 @@ void DescriptorBuilder::BuildOneof(const OneofDescriptorProto& proto, // We need to fill these in later. result->field_count_ = 0; - result->fields_ = NULL; + result->fields_ = nullptr; // Copy options. if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { AllocateOptions(proto.options(), result, OneofDescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); } void DescriptorBuilder::CheckEnumValueUniqueness( @@ -5052,12 +5042,12 @@ void DescriptorBuilder::CheckEnumValueUniqueness( // NAME_TYPE_LAST_NAME = 2, // } PrefixRemover remover(result->name()); - std::map values; + std::map values; for (int i = 0; i < result->value_count(); i++) { const EnumValueDescriptor* value = result->value(i); - string stripped = + std::string stripped = EnumValueToPascalCase(remover.MaybeRemove(value->name())); - std::pair::iterator, bool> + std::pair::iterator, bool> insert_result = values.insert(std::make_pair(stripped, value)); bool inserted = insert_result.second; @@ -5069,11 +5059,13 @@ void DescriptorBuilder::CheckEnumValueUniqueness( // stripping should de-dup the labels in this case). if (!inserted && insert_result.first->second->name() != value->name() && insert_result.first->second->number() != value->number()) { - string error_message = - "When enum name is stripped and label is PascalCased (" + stripped + - "), this value label conflicts with " + values[stripped]->name() + - ". This will make the proto fail to compile for some languages, such " - "as C#."; + std::string error_message = + "Enum name " + value->name() + " has the same name as " + + values[stripped]->name() + + " if you ignore case and strip out the enum name prefix (if any). " + "This is error-prone and can lead to undefined behavior. " + "Please avoid doing this. If you are using allow_alias, please " + "assign the same numeric value to both enums."; // There are proto2 enums out there with conflicting names, so to preserve // compatibility we issue only a warning for proto2. if (result->file()->syntax() == FileDescriptor::SYNTAX_PROTO2) { @@ -5090,26 +5082,22 @@ void DescriptorBuilder::CheckEnumValueUniqueness( void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, const Descriptor* parent, EnumDescriptor* result) { - const string& scope = (parent == NULL) ? - file_->package() : parent->full_name(); - string* full_name = tables_->AllocateString(scope); - if (!full_name->empty()) full_name->append(1, '.'); - full_name->append(proto.name()); - + const std::string& scope = + (parent == nullptr) ? file_->package() : parent->full_name(); + std::string* full_name = AllocateNameString(scope, proto.name()); ValidateSymbolName(proto.name(), *full_name, proto); - result->name_ = tables_->AllocateString(proto.name()); - result->full_name_ = full_name; - result->file_ = file_; + result->name_ = tables_->AllocateString(proto.name()); + result->full_name_ = full_name; + result->file_ = file_; result->containing_type_ = parent; - result->is_placeholder_ = false; + result->is_placeholder_ = false; result->is_unqualified_placeholder_ = false; if (proto.value_size() == 0) { // We cannot allow enums with no values because this would mean there // would be no valid default value for fields of this type. - AddError(result->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Enums must contain at least one value."); } @@ -5120,7 +5108,7 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, int reserved_name_count = proto.reserved_name_size(); result->reserved_name_count_ = reserved_name_count; result->reserved_names_ = - tables_->AllocateArray(reserved_name_count); + tables_->AllocateArray(reserved_name_count); for (int i = 0; i < reserved_name_count; ++i) { result->reserved_names_[i] = tables_->AllocateString(proto.reserved_name(i)); @@ -5130,14 +5118,13 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, // Copy options. if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { AllocateOptions(proto.options(), result, EnumDescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); for (int i = 0; i < proto.reserved_range_size(); i++) { const EnumDescriptorProto_EnumReservedRange& range1 = @@ -5156,16 +5143,15 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, } } - HASH_SET reserved_name_set; + HASH_SET reserved_name_set; for (int i = 0; i < proto.reserved_name_size(); i++) { - const string& name = proto.reserved_name(i); + const std::string& name = proto.reserved_name(i); if (reserved_name_set.find(name) == reserved_name_set.end()) { reserved_name_set.insert(name); } else { AddError(name, proto, DescriptorPool::ErrorCollector::NAME, strings::Substitute( - "Enum value \"$0\" is reserved multiple times.", - name)); + "Enum value \"$0\" is reserved multiple times.", name)); } } @@ -5174,18 +5160,18 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, for (int j = 0; j < result->reserved_range_count(); j++) { const EnumDescriptor::ReservedRange* range = result->reserved_range(j); if (range->start <= value->number() && value->number() <= range->end) { - AddError(value->full_name(), proto.reserved_range(j), - DescriptorPool::ErrorCollector::NUMBER, - strings::Substitute( - "Enum value \"$0\" uses reserved number $1.", - value->name(), value->number())); + AddError( + value->full_name(), proto.reserved_range(j), + DescriptorPool::ErrorCollector::NUMBER, + strings::Substitute("Enum value \"$0\" uses reserved number $1.", + value->name(), value->number())); } } if (reserved_name_set.find(value->name()) != reserved_name_set.end()) { - AddError(value->full_name(), proto.value(i), - DescriptorPool::ErrorCollector::NAME, - strings::Substitute( - "Enum value \"$0\" is reserved.", value->name())); + AddError( + value->full_name(), proto.value(i), + DescriptorPool::ErrorCollector::NAME, + strings::Substitute("Enum value \"$0\" is reserved.", value->name())); } } } @@ -5193,14 +5179,16 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, const EnumDescriptor* parent, EnumValueDescriptor* result) { - result->name_ = tables_->AllocateString(proto.name()); + result->name_ = tables_->AllocateString(proto.name()); result->number_ = proto.number(); - result->type_ = parent; + result->type_ = parent; // Note: full_name for enum values is a sibling to the parent's name, not a // child of it. - string* full_name = tables_->AllocateString(*parent->full_name_); - full_name->resize(full_name->size() - parent->name_->size()); + std::string* full_name = tables_->AllocateEmptyString(); + size_t scope_len = parent->full_name_->size() - parent->name_->size(); + full_name->reserve(scope_len + result->name_->size()); + full_name->append(parent->full_name_->data(), scope_len); full_name->append(*result->name_); result->full_name_ = full_name; @@ -5208,7 +5196,7 @@ void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, // Copy options. if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { AllocateOptions(proto.options(), result, EnumValueDescriptorProto::kOptionsFieldNumber); @@ -5218,22 +5206,22 @@ void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, // of the enum type instead of children of it. So, we use // parent->containing_type() as the value's parent. bool added_to_outer_scope = - AddSymbol(result->full_name(), parent->containing_type(), result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent->containing_type(), result->name(), + proto, Symbol(result)); // However, we also want to be able to search for values within a single // enum type, so we add it as a child of the enum type itself, too. // Note: This could fail, but if it does, the error has already been // reported by the above AddSymbol() call, so we ignore the return code. bool added_to_inner_scope = - file_tables_->AddAliasUnderParent(parent, result->name(), Symbol(result)); + file_tables_->AddAliasUnderParent(parent, result->name(), Symbol(result)); if (added_to_inner_scope && !added_to_outer_scope) { // This value did not conflict with any values defined in the same enum, // but it did conflict with some other symbol defined in the enum type's // scope. Let's print an additional error to explain this. - string outer_scope; - if (parent->containing_type() == NULL) { + std::string outer_scope; + if (parent->containing_type() == nullptr) { outer_scope = file_->package(); } else { outer_scope = parent->containing_type()->full_name(); @@ -5245,12 +5233,12 @@ void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, outer_scope = "\"" + outer_scope + "\""; } - AddError(result->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Note that enum values use C++ scoping rules, meaning that " "enum values are siblings of their type, not children of it. " - "Therefore, \"" + result->name() + "\" must be unique within " - + outer_scope + ", not just within \"" + parent->name() + "\"."); + "Therefore, \"" + + result->name() + "\" must be unique within " + outer_scope + + ", not just within \"" + parent->name() + "\"."); } // An enum is allowed to define two numbers that refer to the same value. @@ -5262,39 +5250,35 @@ void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, void DescriptorBuilder::BuildService(const ServiceDescriptorProto& proto, const void* /* dummy */, ServiceDescriptor* result) { - string* full_name = tables_->AllocateString(file_->package()); - if (!full_name->empty()) full_name->append(1, '.'); - full_name->append(proto.name()); - + std::string* full_name = AllocateNameString(file_->package(), proto.name()); ValidateSymbolName(proto.name(), *full_name, proto); - result->name_ = tables_->AllocateString(proto.name()); + result->name_ = tables_->AllocateString(proto.name()); result->full_name_ = full_name; - result->file_ = file_; + result->file_ = file_; BUILD_ARRAY(proto, result, method, BuildMethod, result); // Copy options. if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { AllocateOptions(proto.options(), result, ServiceDescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), NULL, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), nullptr, result->name(), proto, + Symbol(result)); } void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto, const ServiceDescriptor* parent, MethodDescriptor* result) { - result->name_ = tables_->AllocateString(proto.name()); + result->name_ = tables_->AllocateString(proto.name()); result->service_ = parent; - string* full_name = tables_->AllocateString(parent->full_name()); - full_name->append(1, '.'); - full_name->append(*result->name_); + std::string* full_name = + AllocateNameString(parent->full_name(), *result->name_); result->full_name_ = full_name; ValidateSymbolName(proto.name(), *full_name, proto); @@ -5305,7 +5289,7 @@ void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto, // Copy options. if (!proto.has_options()) { - result->options_ = NULL; // Will set to default_instance later. + result->options_ = nullptr; // Will set to default_instance later. } else { AllocateOptions(proto.options(), result, MethodDescriptorProto::kOptionsFieldNumber); @@ -5314,17 +5298,16 @@ void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto, result->client_streaming_ = proto.client_streaming(); result->server_streaming_ = proto.server_streaming(); - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); } #undef BUILD_ARRAY // ------------------------------------------------------------------- -void DescriptorBuilder::CrossLinkFile( - FileDescriptor* file, const FileDescriptorProto& proto) { - if (file->options_ == NULL) { +void DescriptorBuilder::CrossLinkFile(FileDescriptor* file, + const FileDescriptorProto& proto) { + if (file->options_ == nullptr) { file->options_ = &FileOptions::default_instance(); } @@ -5345,9 +5328,9 @@ void DescriptorBuilder::CrossLinkFile( } } -void DescriptorBuilder::CrossLinkMessage( - Descriptor* message, const DescriptorProto& proto) { - if (message->options_ == NULL) { +void DescriptorBuilder::CrossLinkMessage(Descriptor* message, + const DescriptorProto& proto) { + if (message->options_ == nullptr) { message->options_ = &MessageOptions::default_instance(); } @@ -5377,7 +5360,7 @@ void DescriptorBuilder::CrossLinkMessage( // First count the number of fields per oneof. for (int i = 0; i < message->field_count(); i++) { const OneofDescriptor* oneof_decl = message->field(i)->containing_oneof(); - if (oneof_decl != NULL) { + if (oneof_decl != nullptr) { // Make sure fields belonging to the same oneof are defined consecutively. // This enables optimizations in codegens and reflection libraries to // skip fields in the oneof group, as only one of the field can be set. @@ -5386,14 +5369,13 @@ void DescriptorBuilder::CrossLinkMessage( // safe. if (oneof_decl->field_count() > 0 && message->field(i - 1)->containing_oneof() != oneof_decl) { - AddError( - message->full_name() + "." + message->field(i - 1)->name(), - proto.field(i - 1), DescriptorPool::ErrorCollector::OTHER, - strings::Substitute( - "Fields in the same oneof must be defined consecutively. " - "\"$0\" cannot be defined before the completion of the " - "\"$1\" oneof definition.", - message->field(i - 1)->name(), oneof_decl->name())); + AddError(message->full_name() + "." + message->field(i - 1)->name(), + proto.field(i - 1), DescriptorPool::ErrorCollector::TYPE, + strings::Substitute( + "Fields in the same oneof must be defined consecutively. " + "\"$0\" cannot be defined before the completion of the " + "\"$1\" oneof definition.", + message->field(i - 1)->name(), oneof_decl->name())); } // Must go through oneof_decls_ array to get a non-const version of the // OneofDescriptor. @@ -5407,16 +5389,15 @@ void DescriptorBuilder::CrossLinkMessage( if (oneof_decl->field_count() == 0) { AddError(message->full_name() + "." + oneof_decl->name(), - proto.oneof_decl(i), - DescriptorPool::ErrorCollector::NAME, + proto.oneof_decl(i), DescriptorPool::ErrorCollector::NAME, "Oneof must have at least one field."); } - oneof_decl->fields_ = - tables_->AllocateArray(oneof_decl->field_count_); + oneof_decl->fields_ = tables_->AllocateArray( + oneof_decl->field_count_); oneof_decl->field_count_ = 0; - if (oneof_decl->options_ == NULL) { + if (oneof_decl->options_ == nullptr) { oneof_decl->options_ = &OneofOptions::default_instance(); } } @@ -5424,7 +5405,7 @@ void DescriptorBuilder::CrossLinkMessage( // Then fill them in. for (int i = 0; i < message->field_count(); i++) { const OneofDescriptor* oneof_decl = message->field(i)->containing_oneof(); - if (oneof_decl != NULL) { + if (oneof_decl != nullptr) { OneofDescriptor* mutable_oneof_decl = &message->oneof_decls_[oneof_decl->index()]; message->fields_[i].index_in_oneof_ = mutable_oneof_decl->field_count_; @@ -5437,14 +5418,14 @@ void DescriptorBuilder::CrossLinkMessage( void DescriptorBuilder::CrossLinkExtensionRange( Descriptor::ExtensionRange* range, const DescriptorProto::ExtensionRange& proto) { - if (range->options_ == NULL) { + if (range->options_ == nullptr) { range->options_ = &ExtensionRangeOptions::default_instance(); } } -void DescriptorBuilder::CrossLinkField( - FieldDescriptor* field, const FieldDescriptorProto& proto) { - if (field->options_ == NULL) { +void DescriptorBuilder::CrossLinkField(FieldDescriptor* field, + const FieldDescriptorProto& proto) { + if (field->options_ == nullptr) { field->options_ = &FieldOptions::default_instance(); } @@ -5468,10 +5449,11 @@ void DescriptorBuilder::CrossLinkField( } field->containing_type_ = extendee.descriptor; - const Descriptor::ExtensionRange* extension_range = field->containing_type() - ->FindExtensionRangeContainingNumber(field->number()); + const Descriptor::ExtensionRange* extension_range = + field->containing_type()->FindExtensionRangeContainingNumber( + field->number()); - if (extension_range == NULL) { + if (extension_range == nullptr) { AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, strings::Substitute("\"$0\" does not declare $1 as an " @@ -5481,13 +5463,12 @@ void DescriptorBuilder::CrossLinkField( } } - if (field->containing_oneof() != NULL) { + if (field->containing_oneof() != nullptr) { if (field->label() != FieldDescriptor::LABEL_OPTIONAL) { // Note that this error will never happen when parsing .proto files. // It can only happen if you manually construct a FileDescriptorProto // that is incorrect. - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Fields of oneofs must themselves have label LABEL_OPTIONAL."); } } @@ -5519,7 +5500,7 @@ void DescriptorBuilder::CrossLinkField( if (is_lazy) { // Save the symbol names for later for lookup, and allocate the once // object needed for the accessors. - string name = proto.type_name(); + std::string name = proto.type_name(); field->type_once_ = tables_->AllocateOnceDynamic(); field->type_name_ = tables_->AllocateString(name); if (proto.has_default_value()) { @@ -5608,20 +5589,19 @@ void DescriptorBuilder::CrossLinkField( // We can't just use field->enum_type()->FindValueByName() here // because that locks the pool's mutex, which we have already locked // at this point. - Symbol default_value = - LookupSymbolNoPlaceholder(proto.default_value(), - field->enum_type()->full_name()); + Symbol default_value = LookupSymbolNoPlaceholder( + proto.default_value(), field->enum_type()->full_name()); if (default_value.type == Symbol::ENUM_VALUE && default_value.enum_value_descriptor->type() == - field->enum_type()) { + field->enum_type()) { field->default_value_enum_ = default_value.enum_value_descriptor; } else { AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::DEFAULT_VALUE, "Enum type \"" + field->enum_type()->full_name() + - "\" has no value named \"" + proto.default_value() + - "\"."); + "\" has no value named \"" + proto.default_value() + + "\"."); } } } else if (field->enum_type()->value_count() > 0) { @@ -5649,27 +5629,25 @@ void DescriptorBuilder::CrossLinkField( // risk to calling containing_type() or other accessors that will build // dependencies. if (!file_tables_->AddFieldByNumber(field)) { - const FieldDescriptor* conflicting_field = - file_tables_->FindFieldByNumber(field->containing_type(), - field->number()); - string containing_type_name = field->containing_type() == NULL - ? "unknown" - : field->containing_type()->full_name(); + const FieldDescriptor* conflicting_field = file_tables_->FindFieldByNumber( + field->containing_type(), field->number()); + std::string containing_type_name = + field->containing_type() == nullptr + ? "unknown" + : field->containing_type()->full_name(); if (field->is_extension()) { AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, strings::Substitute("Extension number $0 has already been used " "in \"$1\" by extension \"$2\".", - field->number(), - containing_type_name, + field->number(), containing_type_name, conflicting_field->full_name())); } else { AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, strings::Substitute("Field number $0 has already been used in " "\"$1\" by field \"$2\".", - field->number(), - containing_type_name, + field->number(), containing_type_name, conflicting_field->name())); } } else { @@ -5677,11 +5655,11 @@ void DescriptorBuilder::CrossLinkField( if (!tables_->AddExtension(field)) { const FieldDescriptor* conflicting_field = tables_->FindExtension(field->containing_type(), field->number()); - string containing_type_name = - field->containing_type() == NULL + std::string containing_type_name = + field->containing_type() == nullptr ? "unknown" : field->containing_type()->full_name(); - string error_msg = strings::Substitute( + std::string error_msg = strings::Substitute( "Extension number $0 has already been used in \"$1\" by extension " "\"$2\" defined in $3.", field->number(), containing_type_name, @@ -5697,9 +5675,9 @@ void DescriptorBuilder::CrossLinkField( } } -void DescriptorBuilder::CrossLinkEnum( - EnumDescriptor* enum_type, const EnumDescriptorProto& proto) { - if (enum_type->options_ == NULL) { +void DescriptorBuilder::CrossLinkEnum(EnumDescriptor* enum_type, + const EnumDescriptorProto& proto) { + if (enum_type->options_ == nullptr) { enum_type->options_ = &EnumOptions::default_instance(); } @@ -5711,14 +5689,14 @@ void DescriptorBuilder::CrossLinkEnum( void DescriptorBuilder::CrossLinkEnumValue( EnumValueDescriptor* enum_value, const EnumValueDescriptorProto& /* proto */) { - if (enum_value->options_ == NULL) { + if (enum_value->options_ == nullptr) { enum_value->options_ = &EnumValueOptions::default_instance(); } } -void DescriptorBuilder::CrossLinkService( - ServiceDescriptor* service, const ServiceDescriptorProto& proto) { - if (service->options_ == NULL) { +void DescriptorBuilder::CrossLinkService(ServiceDescriptor* service, + const ServiceDescriptorProto& proto) { + if (service->options_ == nullptr) { service->options_ = &ServiceOptions::default_instance(); } @@ -5727,9 +5705,9 @@ void DescriptorBuilder::CrossLinkService( } } -void DescriptorBuilder::CrossLinkMethod( - MethodDescriptor* method, const MethodDescriptorProto& proto) { - if (method->options_ == NULL) { +void DescriptorBuilder::CrossLinkMethod(MethodDescriptor* method, + const MethodDescriptorProto& proto) { + if (method->options_ == nullptr) { method->options_ = &MethodOptions::default_instance(); } @@ -5776,10 +5754,10 @@ void DescriptorBuilder::CrossLinkMethod( // ------------------------------------------------------------------- -#define VALIDATE_OPTIONS_FROM_ARRAY(descriptor, array_name, type) \ - for (int i = 0; i < descriptor->array_name##_count(); ++i) { \ - Validate##type##Options(descriptor->array_name##s_ + i, \ - proto.array_name(i)); \ +#define VALIDATE_OPTIONS_FROM_ARRAY(descriptor, array_name, type) \ + for (int i = 0; i < descriptor->array_name##_count(); ++i) { \ + Validate##type##Options(descriptor->array_name##s_ + i, \ + proto.array_name(i)); \ } // Determine if the file uses optimize_for = LITE_RUNTIME, being careful to @@ -5787,7 +5765,7 @@ void DescriptorBuilder::CrossLinkMethod( static bool IsLite(const FileDescriptor* file) { // TODO(kenton): I don't even remember how many of these conditions are // actually possible. I'm just being super-safe. - return file != NULL && + return file != nullptr && &file->options() != &FileOptions::default_instance() && file->options().optimize_for() == FileOptions::LITE_RUNTIME; } @@ -5804,11 +5782,12 @@ void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file, for (int i = 0; i < file->dependency_count(); i++) { if (IsLite(file->dependency(i))) { AddError( - file->name(), proto, - DescriptorPool::ErrorCollector::OTHER, - "Files that do not use optimize_for = LITE_RUNTIME cannot import " - "files which do use this option. This file is not lite, but it " - "imports \"" + file->dependency(i)->name() + "\" which is."); + file->dependency(i)->name(), proto, + DescriptorPool::ErrorCollector::IMPORT, + "Files that do not use optimize_for = LITE_RUNTIME cannot import " + "files which do use this option. This file is not lite, but it " + "imports \"" + + file->dependency(i)->name() + "\" which is."); break; } } @@ -5818,8 +5797,8 @@ void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file, } } -void DescriptorBuilder::ValidateProto3( - FileDescriptor* file, const FileDescriptorProto& proto) { +void DescriptorBuilder::ValidateProto3(FileDescriptor* file, + const FileDescriptorProto& proto) { for (int i = 0; i < file->extension_count(); ++i) { ValidateProto3Field(file->extensions_ + i, proto.extension(i)); } @@ -5831,8 +5810,8 @@ void DescriptorBuilder::ValidateProto3( } } -static string ToLowercaseWithoutUnderscores(const string& name) { - string result; +static std::string ToLowercaseWithoutUnderscores(const std::string& name) { + std::string result; for (int i = 0; i < name.size(); ++i) { if (name[i] != '_') { if (name[i] >= 'A' && name[i] <= 'Z') { @@ -5845,97 +5824,91 @@ static string ToLowercaseWithoutUnderscores(const string& name) { return result; } -void DescriptorBuilder::ValidateProto3Message( - Descriptor* message, const DescriptorProto& proto) { +void DescriptorBuilder::ValidateProto3Message(Descriptor* message, + const DescriptorProto& proto) { for (int i = 0; i < message->nested_type_count(); ++i) { - ValidateProto3Message(message->nested_types_ + i, - proto.nested_type(i)); + ValidateProto3Message(message->nested_types_ + i, proto.nested_type(i)); } for (int i = 0; i < message->enum_type_count(); ++i) { - ValidateProto3Enum(message->enum_types_ + i, - proto.enum_type(i)); + ValidateProto3Enum(message->enum_types_ + i, proto.enum_type(i)); } for (int i = 0; i < message->field_count(); ++i) { ValidateProto3Field(message->fields_ + i, proto.field(i)); } for (int i = 0; i < message->extension_count(); ++i) { - ValidateProto3Field(message->extensions_ +i, proto.extension(i)); + ValidateProto3Field(message->extensions_ + i, proto.extension(i)); } if (message->extension_range_count() > 0) { - AddError(message->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(message->full_name(), proto.extension_range(0), + DescriptorPool::ErrorCollector::NUMBER, "Extension ranges are not allowed in proto3."); } if (message->options().message_set_wire_format()) { // Using MessageSet doesn't make sense since we disallow extensions. - AddError(message->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "MessageSet is not supported in proto3."); } // In proto3, we reject field names if they conflict in camelCase. // Note that we currently enforce a stricter rule: Field names must be // unique after being converted to lowercase with underscores removed. - std::map name_to_field; + std::map name_to_field; for (int i = 0; i < message->field_count(); ++i) { - string lowercase_name = ToLowercaseWithoutUnderscores( - message->field(i)->name()); + std::string lowercase_name = + ToLowercaseWithoutUnderscores(message->field(i)->name()); if (name_to_field.find(lowercase_name) != name_to_field.end()) { - AddError(message->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(message->full_name(), proto.field(i), + DescriptorPool::ErrorCollector::NAME, "The JSON camel-case name of field \"" + - message->field(i)->name() + "\" conflicts with field \"" + - name_to_field[lowercase_name]->name() + "\". This is not " + - "allowed in proto3."); + message->field(i)->name() + "\" conflicts with field \"" + + name_to_field[lowercase_name]->name() + "\". This is not " + + "allowed in proto3."); } else { name_to_field[lowercase_name] = message->field(i); } } } -void DescriptorBuilder::ValidateProto3Field( - FieldDescriptor* field, const FieldDescriptorProto& proto) { +void DescriptorBuilder::ValidateProto3Field(FieldDescriptor* field, + const FieldDescriptorProto& proto) { if (field->is_extension() && !AllowedExtendeeInProto3(field->containing_type()->full_name())) { AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + DescriptorPool::ErrorCollector::EXTENDEE, "Extensions in proto3 are only allowed for defining options."); } if (field->is_required()) { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "Required fields are not allowed in proto3."); } if (field->has_default_value()) { - AddError( - field->full_name(), proto, DescriptorPool::ErrorCollector::OTHER, - "Explicit default values are not allowed in proto3."); + AddError(field->full_name(), proto, + DescriptorPool::ErrorCollector::DEFAULT_VALUE, + "Explicit default values are not allowed in proto3."); } if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM && field->enum_type() && field->enum_type()->file()->syntax() != FileDescriptor::SYNTAX_PROTO3) { // Proto3 messages can only use Proto3 enum types; otherwise we can't // guarantee that the default value is zero. - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::TYPE, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "Enum type \"" + field->enum_type()->full_name() + - "\" is not a proto3 enum, but is used in \"" + - field->containing_type()->full_name() + - "\" which is a proto3 message type."); + "\" is not a proto3 enum, but is used in \"" + + field->containing_type()->full_name() + + "\" which is a proto3 message type."); } if (field->type() == FieldDescriptor::TYPE_GROUP) { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::TYPE, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "Groups are not supported in proto3 syntax."); } } -void DescriptorBuilder::ValidateProto3Enum( - EnumDescriptor* enm, const EnumDescriptorProto& proto) { +void DescriptorBuilder::ValidateProto3Enum(EnumDescriptor* enm, + const EnumDescriptorProto& proto) { if (enm->value_count() > 0 && enm->value(0)->number() != 0) { - AddError( - enm->full_name(), proto, DescriptorPool::ErrorCollector::OTHER, - "The first enum value must be zero in proto3."); + AddError(enm->full_name(), proto.value(0), + DescriptorPool::ErrorCollector::NUMBER, + "The first enum value must be zero in proto3."); } } @@ -5947,9 +5920,9 @@ void DescriptorBuilder::ValidateMessageOptions(Descriptor* message, VALIDATE_OPTIONS_FROM_ARRAY(message, extension, Field); const int64 max_extension_range = - static_cast(message->options().message_set_wire_format() ? - kint32max : - FieldDescriptor::kMaxNumber); + static_cast(message->options().message_set_wire_format() + ? kint32max + : FieldDescriptor::kMaxNumber); for (int i = 0; i < message->extension_range_count(); ++i) { if (message->extension_range(i)->end > max_extension_range + 1) { AddError( @@ -5962,16 +5935,15 @@ void DescriptorBuilder::ValidateMessageOptions(Descriptor* message, } -void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, - const FieldDescriptorProto& proto) { +void DescriptorBuilder::ValidateFieldOptions( + FieldDescriptor* field, const FieldDescriptorProto& proto) { if (pool_->lazily_build_dependencies_ && (!field || !field->message_type())) { return; } // Only message type fields may be lazy. if (field->options().lazy()) { if (field->type() != FieldDescriptor::TYPE_MESSAGE) { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::TYPE, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "[lazy = true] can only be specified for submessage fields."); } } @@ -5979,16 +5951,15 @@ void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, // Only repeated primitive fields may be packed. if (field->options().packed() && !field->is_packable()) { AddError( - field->full_name(), proto, - DescriptorPool::ErrorCollector::TYPE, - "[packed = true] can only be specified for repeated primitive fields."); + field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, + "[packed = true] can only be specified for repeated primitive fields."); } // Note: Default instance may not yet be initialized here, so we have to // avoid reading from it. - if (field->containing_type_ != NULL && + if (field->containing_type_ != nullptr && &field->containing_type()->options() != - &MessageOptions::default_instance() && + &MessageOptions::default_instance() && field->containing_type()->options().message_set_wire_format()) { if (field->is_extension()) { if (!field->is_optional() || @@ -5998,15 +5969,13 @@ void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, "Extensions of MessageSets must be optional messages."); } } else { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "MessageSets cannot have fields, only extensions."); } } // Lite extensions can only be of Lite types. - if (IsLite(field->file()) && - field->containing_type_ != NULL && + if (IsLite(field->file()) && field->containing_type_ != nullptr && !IsLite(field->containing_type()->file())) { AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::EXTENDEE, @@ -6018,8 +5987,7 @@ void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, // Validate map types. if (field->is_map()) { if (!ValidateMapEntry(field, proto)) { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "map_entry should not be set explicitly. Use map instead."); } @@ -6050,24 +6018,20 @@ void DescriptorBuilder::ValidateEnumOptions(EnumDescriptor* enm, const EnumDescriptorProto& proto) { VALIDATE_OPTIONS_FROM_ARRAY(enm, value, EnumValue); if (!enm->options().has_allow_alias() || !enm->options().allow_alias()) { - std::map used_values; + std::map used_values; for (int i = 0; i < enm->value_count(); ++i) { const EnumValueDescriptor* enum_value = enm->value(i); if (used_values.find(enum_value->number()) != used_values.end()) { - string error = + std::string error = "\"" + enum_value->full_name() + "\" uses the same enum value as \"" + - used_values[enum_value->number()] + "\". If this is intended, set " + used_values[enum_value->number()] + + "\". If this is intended, set " "'option allow_alias = true;' to the enum definition."; if (!enm->options().allow_alias()) { // Generate error if duplicated enum values are explicitly disallowed. - AddError(enm->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, - error); - } else { - // Generate warning if duplicated values are found but the option - // isn't set. - GOOGLE_LOG(ERROR) << error; + AddError(enm->full_name(), proto.value(i), + DescriptorPool::ErrorCollector::NUMBER, error); } } else { used_values[enum_value->number()] = enum_value->full_name(); @@ -6081,31 +6045,30 @@ void DescriptorBuilder::ValidateEnumValueOptions( const EnumValueDescriptorProto& /* proto */) { // Nothing to do so far. } -void DescriptorBuilder::ValidateServiceOptions(ServiceDescriptor* service, - const ServiceDescriptorProto& proto) { +void DescriptorBuilder::ValidateServiceOptions( + ServiceDescriptor* service, const ServiceDescriptorProto& proto) { if (IsLite(service->file()) && (service->file()->options().cc_generic_services() || service->file()->options().java_generic_services())) { - AddError(service->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(service->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Files with optimize_for = LITE_RUNTIME cannot define services " "unless you set both options cc_generic_services and " - "java_generic_sevices to false."); + "java_generic_services to false."); } VALIDATE_OPTIONS_FROM_ARRAY(service, method, Method); } -void DescriptorBuilder::ValidateMethodOptions(MethodDescriptor* /* method */, - const MethodDescriptorProto& /* proto */) { +void DescriptorBuilder::ValidateMethodOptions( + MethodDescriptor* /* method */, const MethodDescriptorProto& /* proto */) { // Nothing to do so far. } bool DescriptorBuilder::ValidateMapEntry(FieldDescriptor* field, const FieldDescriptorProto& proto) { const Descriptor* message = field->message_type(); - if (// Must not contain extensions, extension range or nested message or - // enums + if ( // Must not contain extensions, extension range or nested message or + // enums message->extension_count() != 0 || field->label() != FieldDescriptor::LABEL_REPEATED || message->extension_range_count() != 0 || @@ -6133,9 +6096,8 @@ bool DescriptorBuilder::ValidateMapEntry(FieldDescriptor* field, // Check key types are legal. switch (key->type()) { case FieldDescriptor::TYPE_ENUM: - AddError( - field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, - "Key in map fields cannot be enum types."); + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, + "Key in map fields cannot be enum types."); break; case FieldDescriptor::TYPE_FLOAT: case FieldDescriptor::TYPE_DOUBLE: @@ -6160,15 +6122,14 @@ bool DescriptorBuilder::ValidateMapEntry(FieldDescriptor* field, case FieldDescriptor::TYPE_SFIXED64: // Legal cases break; - // Do not add a default, so that the compiler will complain when new types - // are added. + // Do not add a default, so that the compiler will complain when new types + // are added. } if (value->type() == FieldDescriptor::TYPE_ENUM) { if (value->enum_type()->value(0)->number() != 0) { - AddError( - field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, - "Enum value in map must define 0 as the first value."); + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, + "Enum value in map must define 0 as the first value."); } } @@ -6177,10 +6138,10 @@ bool DescriptorBuilder::ValidateMapEntry(FieldDescriptor* field, void DescriptorBuilder::DetectMapConflicts(const Descriptor* message, const DescriptorProto& proto) { - std::map seen_types; + std::map seen_types; for (int i = 0; i < message->nested_type_count(); ++i) { const Descriptor* nested = message->nested_type(i); - std::pair::iterator, bool> result = + std::pair::iterator, bool> result = seen_types.insert(std::make_pair(nested->name(), nested)); if (!result.second) { if (result.first->second->options().map_entry() || @@ -6188,7 +6149,7 @@ void DescriptorBuilder::DetectMapConflicts(const Descriptor* message, AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Expanded map entry type " + nested->name() + - " conflicts with an existing nested message type."); + " conflicts with an existing nested message type."); } } // Recursively test on the nested types. @@ -6197,37 +6158,37 @@ void DescriptorBuilder::DetectMapConflicts(const Descriptor* message, // Check for conflicted field names. for (int i = 0; i < message->field_count(); ++i) { const FieldDescriptor* field = message->field(i); - std::map::iterator iter = + std::map::iterator iter = seen_types.find(field->name()); if (iter != seen_types.end() && iter->second->options().map_entry()) { AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Expanded map entry type " + iter->second->name() + - " conflicts with an existing field."); + " conflicts with an existing field."); } } // Check for conflicted enum names. for (int i = 0; i < message->enum_type_count(); ++i) { const EnumDescriptor* enum_desc = message->enum_type(i); - std::map::iterator iter = + std::map::iterator iter = seen_types.find(enum_desc->name()); if (iter != seen_types.end() && iter->second->options().map_entry()) { AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Expanded map entry type " + iter->second->name() + - " conflicts with an existing enum type."); + " conflicts with an existing enum type."); } } // Check for conflicted oneof names. for (int i = 0; i < message->oneof_decl_count(); ++i) { const OneofDescriptor* oneof_desc = message->oneof_decl(i); - std::map::iterator iter = + std::map::iterator iter = seen_types.find(oneof_desc->name()); if (iter != seen_types.end() && iter->second->options().map_entry()) { AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Expanded map entry type " + iter->second->name() + - " conflicts with an existing oneof type."); + " conflicts with an existing oneof type."); } } } @@ -6255,7 +6216,7 @@ void DescriptorBuilder::ValidateJSType(FieldDescriptor* field, AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "Illegal jstype for int64, uint64, sint64, fixed64 " "or sfixed64 field: " + - FieldOptions_JSType_descriptor()->value(jstype)->name()); + FieldOptions_JSType_descriptor()->value(jstype)->name()); break; // No other types permit a jstype option. @@ -6272,12 +6233,12 @@ void DescriptorBuilder::ValidateJSType(FieldDescriptor* field, // ------------------------------------------------------------------- DescriptorBuilder::OptionInterpreter::OptionInterpreter( - DescriptorBuilder* builder) : builder_(builder) { + DescriptorBuilder* builder) + : builder_(builder) { GOOGLE_CHECK(builder_); } -DescriptorBuilder::OptionInterpreter::~OptionInterpreter() { -} +DescriptorBuilder::OptionInterpreter::~OptionInterpreter() {} bool DescriptorBuilder::OptionInterpreter::InterpretOptions( OptionsToInterpret* options_to_interpret) { @@ -6293,7 +6254,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptions( // and clear them, since we're about to interpret them. const FieldDescriptor* uninterpreted_options_field = options->GetDescriptor()->FindFieldByName("uninterpreted_option"); - GOOGLE_CHECK(uninterpreted_options_field != NULL) + GOOGLE_CHECK(uninterpreted_options_field != nullptr) << "No field named \"uninterpreted_option\" in the Options proto."; options->GetReflection()->ClearField(options, uninterpreted_options_field); @@ -6302,13 +6263,14 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptions( // Find the uninterpreted_option field in the original options. const FieldDescriptor* original_uninterpreted_options_field = - original_options->GetDescriptor()-> - FindFieldByName("uninterpreted_option"); - GOOGLE_CHECK(original_uninterpreted_options_field != NULL) + original_options->GetDescriptor()->FindFieldByName( + "uninterpreted_option"); + GOOGLE_CHECK(original_uninterpreted_options_field != nullptr) << "No field named \"uninterpreted_option\" in the Options proto."; - const int num_uninterpreted_options = original_options->GetReflection()-> - FieldSize(*original_options, original_uninterpreted_options_field); + const int num_uninterpreted_options = + original_options->GetReflection()->FieldSize( + *original_options, original_uninterpreted_options_field); for (int i = 0; i < num_uninterpreted_options; ++i) { src_path.push_back(i); uninterpreted_option_ = down_cast( @@ -6323,8 +6285,8 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptions( src_path.pop_back(); } // Reset these, so we don't have any dangling pointers. - uninterpreted_option_ = NULL; - options_to_interpret_ = NULL; + uninterpreted_option_ = nullptr; + options_to_interpret_ = nullptr; if (!failed) { // InterpretSingleOption() added the interpreted options in the @@ -6340,7 +6302,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptions( std::unique_ptr unparsed_options(options->New()); options->GetReflection()->Swap(unparsed_options.get(), options); - string buf; + std::string buf; if (!unparsed_options->AppendToString(&buf) || !options->ParseFromString(buf)) { builder_->AddError( @@ -6348,8 +6310,11 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptions( DescriptorPool::ErrorCollector::OTHER, "Some options could not be correctly parsed using the proto " "descriptors compiled into this binary.\n" - "Unparsed options: " + unparsed_options->ShortDebugString() + "\n" - "Parsing attempt: " + options->ShortDebugString()); + "Unparsed options: " + + unparsed_options->ShortDebugString() + + "\n" + "Parsing attempt: " + + options->ShortDebugString()); // Restore the unparsed options. options->GetReflection()->Swap(unparsed_options.get(), options); } @@ -6367,11 +6332,12 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( return AddNameError("Option must have a name."); } if (uninterpreted_option_->name(0).name_part() == "uninterpreted_option") { - return AddNameError("Option must not use reserved name " - "\"uninterpreted_option\"."); + return AddNameError( + "Option must not use reserved name " + "\"uninterpreted_option\"."); } - const Descriptor* options_descriptor = NULL; + const Descriptor* options_descriptor = nullptr; // Get the options message's descriptor from the builder's pool, so that we // get the version that knows about any extension options declared in the file // we're currently building. The descriptor should be there as long as the @@ -6383,7 +6349,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( // FindSymbol() because files that use custom options only need to depend on // the file that defines the option, not descriptor.proto itself. Symbol symbol = builder_->FindSymbolNotEnforcingDeps( - options->GetDescriptor()->full_name()); + options->GetDescriptor()->full_name()); if (!symbol.IsNull() && symbol.type == Symbol::MESSAGE) { options_descriptor = symbol.descriptor; } else { @@ -6401,14 +6367,14 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( // through in |intermediate_fields|. As we go, we reconstruct the full option // name in |debug_msg_name|, for use in error messages. const Descriptor* descriptor = options_descriptor; - const FieldDescriptor* field = NULL; + const FieldDescriptor* field = nullptr; std::vector intermediate_fields; - string debug_msg_name = ""; + std::string debug_msg_name = ""; std::vector dest_path = options_path; for (int i = 0; i < uninterpreted_option_->name_size(); ++i) { - const string& name_part = uninterpreted_option_->name(i).name_part(); + const std::string& name_part = uninterpreted_option_->name(i).name_part(); if (debug_msg_name.size() > 0) { debug_msg_name += "."; } @@ -6419,8 +6385,8 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( // DescriptorPool::FindExtensionByName(), for two reasons: 1) It allows // relative lookups, and 2) because we're already holding the pool's // mutex, and the latter method locks it again. - symbol = builder_->LookupSymbol(name_part, - options_to_interpret_->name_scope); + symbol = + builder_->LookupSymbol(name_part, options_to_interpret_->name_scope); if (!symbol.IsNull() && symbol.type == Symbol::FIELD) { field = symbol.field_descriptor; } @@ -6434,7 +6400,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( field = descriptor->FindFieldByName(name_part); } - if (field == NULL) { + if (field == nullptr) { if (get_allow_unknown(builder_->pool_)) { // We can't find the option, but AllowUnknownDependencies() is enabled, // so we will just leave it as uninterpreted. @@ -6451,8 +6417,9 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( "\") to start from the outermost scope."); } else { return AddNameError( - "Option \"" + debug_msg_name + "\" unknown. Ensure that your proto" + - " definition file imports the proto which defines the option."); + "Option \"" + debug_msg_name + + "\" unknown. Ensure that your proto" + + " definition file imports the proto which defines the option."); } } else if (field->containing_type() != descriptor) { if (get_is_placeholder(field->containing_type())) { @@ -6501,10 +6468,10 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( // known will populate them correctly. // First see if the option is already set. - if (!field->is_repeated() && !ExamineIfOptionIsSet( - intermediate_fields.begin(), - intermediate_fields.end(), - field, debug_msg_name, + if (!field->is_repeated() && + !ExamineIfOptionIsSet( + intermediate_fields.begin(), intermediate_fields.end(), field, + debug_msg_name, options->GetReflection()->GetUnknownFields(*options))) { return false; // ExamineIfOptionIsSet() already added the error. } @@ -6536,8 +6503,8 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( } case FieldDescriptor::TYPE_GROUP: { - parent_unknown_fields->AddGroup((*iter)->number()) - ->MergeFrom(*unknown_fields); + parent_unknown_fields->AddGroup((*iter)->number()) + ->MergeFrom(*unknown_fields); break; } @@ -6660,18 +6627,19 @@ void DescriptorBuilder::OptionInterpreter::UpdateSourceCodeInfo( void DescriptorBuilder::OptionInterpreter::AddWithoutInterpreting( const UninterpretedOption& uninterpreted_option, Message* options) { const FieldDescriptor* field = - options->GetDescriptor()->FindFieldByName("uninterpreted_option"); - GOOGLE_CHECK(field != NULL); + options->GetDescriptor()->FindFieldByName("uninterpreted_option"); + GOOGLE_CHECK(field != nullptr); - options->GetReflection()->AddMessage(options, field) - ->CopyFrom(uninterpreted_option); + options->GetReflection() + ->AddMessage(options, field) + ->CopyFrom(uninterpreted_option); } bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( std::vector::const_iterator intermediate_fields_iter, std::vector::const_iterator intermediate_fields_end, - const FieldDescriptor* innermost_field, const string& debug_msg_name, + const FieldDescriptor* innermost_field, const std::string& debug_msg_name, const UnknownFieldSet& unknown_fields) { // We do linear searches of the UnknownFieldSet and its sub-groups. This // should be fine since it's unlikely that any one options structure will @@ -6701,8 +6669,8 @@ bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( if (intermediate_unknown_fields.ParseFromString( unknown_field->length_delimited()) && !ExamineIfOptionIsSet(intermediate_fields_iter + 1, - intermediate_fields_end, - innermost_field, debug_msg_name, + intermediate_fields_end, innermost_field, + debug_msg_name, intermediate_unknown_fields)) { return false; // Error already added. } @@ -6712,9 +6680,8 @@ bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( case FieldDescriptor::TYPE_GROUP: if (unknown_field->type() == UnknownField::TYPE_GROUP) { if (!ExamineIfOptionIsSet(intermediate_fields_iter + 1, - intermediate_fields_end, - innermost_field, debug_msg_name, - unknown_field->group())) { + intermediate_fields_end, innermost_field, + debug_msg_name, unknown_field->group())) { return false; // Error already added. } } @@ -6730,11 +6697,9 @@ bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( } bool DescriptorBuilder::OptionInterpreter::SetOptionValue( - const FieldDescriptor* option_field, - UnknownFieldSet* unknown_fields) { + const FieldDescriptor* option_field, UnknownFieldSet* unknown_fields) { // We switch on the CppType to validate. switch (option_field->cpp_type()) { - case FieldDescriptor::CPPTYPE_INT32: if (uninterpreted_option_->has_positive_int_value()) { if (uninterpreted_option_->positive_int_value() > @@ -6794,8 +6759,10 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( option_field->type(), unknown_fields); } } else { - return AddValueError("Value must be non-negative integer for uint32 " - "option \"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be non-negative integer for uint32 " + "option \"" + + option_field->full_name() + "\"."); } break; @@ -6805,8 +6772,10 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( uninterpreted_option_->positive_int_value(), option_field->type(), unknown_fields); } else { - return AddValueError("Value must be non-negative integer for uint64 " - "option \"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be non-negative integer for uint64 " + "option \"" + + option_field->full_name() + "\"."); } break; @@ -6847,33 +6816,39 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( case FieldDescriptor::CPPTYPE_BOOL: uint64 value; if (!uninterpreted_option_->has_identifier_value()) { - return AddValueError("Value must be identifier for boolean option " - "\"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be identifier for boolean option " + "\"" + + option_field->full_name() + "\"."); } if (uninterpreted_option_->identifier_value() == "true") { value = 1; } else if (uninterpreted_option_->identifier_value() == "false") { value = 0; } else { - return AddValueError("Value must be \"true\" or \"false\" for boolean " - "option \"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be \"true\" or \"false\" for boolean " + "option \"" + + option_field->full_name() + "\"."); } unknown_fields->AddVarint(option_field->number(), value); break; case FieldDescriptor::CPPTYPE_ENUM: { if (!uninterpreted_option_->has_identifier_value()) { - return AddValueError("Value must be identifier for enum-valued option " - "\"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be identifier for enum-valued option " + "\"" + + option_field->full_name() + "\"."); } const EnumDescriptor* enum_type = option_field->enum_type(); - const string& value_name = uninterpreted_option_->identifier_value(); - const EnumValueDescriptor* enum_value = NULL; + const std::string& value_name = uninterpreted_option_->identifier_value(); + const EnumValueDescriptor* enum_value = nullptr; if (enum_type->file()->pool() != DescriptorPool::generated_pool()) { // Note that the enum value's fully-qualified name is a sibling of the // enum's name, not a child of it. - string fully_qualified_name = enum_type->full_name(); + std::string fully_qualified_name = enum_type->full_name(); fully_qualified_name.resize(fully_qualified_name.size() - enum_type->name().size()); fully_qualified_name += value_name; @@ -6883,10 +6858,11 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( // DescriptorPool::FindEnumValueByName() because we're already holding // the pool's mutex, and the latter method locks it again. Symbol symbol = - builder_->FindSymbolNotEnforcingDeps(fully_qualified_name); + builder_->FindSymbolNotEnforcingDeps(fully_qualified_name); if (!symbol.IsNull() && symbol.type == Symbol::ENUM_VALUE) { if (symbol.enum_value_descriptor->type() != enum_type) { - return AddValueError("Enum type \"" + enum_type->full_name() + + return AddValueError( + "Enum type \"" + enum_type->full_name() + "\" has no value named \"" + value_name + "\" for option \"" + option_field->full_name() + "\". This appears to be a value from a sibling type."); @@ -6900,28 +6876,33 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( enum_value = enum_type->FindValueByName(value_name); } - if (enum_value == NULL) { + if (enum_value == nullptr) { return AddValueError("Enum type \"" + option_field->enum_type()->full_name() + - "\" has no value named \"" + value_name + "\" for " - "option \"" + option_field->full_name() + "\"."); + "\" has no value named \"" + value_name + + "\" for " + "option \"" + + option_field->full_name() + "\"."); } else { // Sign-extension is not a problem, since we cast directly from int32 to // uint64, without first going through uint32. - unknown_fields->AddVarint(option_field->number(), - static_cast(static_cast(enum_value->number()))); + unknown_fields->AddVarint( + option_field->number(), + static_cast(static_cast(enum_value->number()))); } break; } case FieldDescriptor::CPPTYPE_STRING: if (!uninterpreted_option_->has_string_value()) { - return AddValueError("Value must be quoted string for string option " - "\"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be quoted string for string option " + "\"" + + option_field->full_name() + "\"."); } // The string has already been unquoted and unescaped by the parser. unknown_fields->AddLengthDelimited(option_field->number(), - uninterpreted_option_->string_value()); + uninterpreted_option_->string_value()); break; case FieldDescriptor::CPPTYPE_MESSAGE: @@ -6940,11 +6921,11 @@ class DescriptorBuilder::OptionInterpreter::AggregateOptionFinder DescriptorBuilder* builder_; const FieldDescriptor* FindExtension(Message* message, - const string& name) const override { + const std::string& name) const override { assert_mutex_held(builder_->pool_); const Descriptor* descriptor = message->GetDescriptor(); - Symbol result = builder_->LookupSymbolNoPlaceholder( - name, descriptor->full_name()); + Symbol result = + builder_->LookupSymbolNoPlaceholder(name, descriptor->full_name()); if (result.type == Symbol::FIELD && result.field_descriptor->is_extension()) { return result.field_descriptor; @@ -6967,7 +6948,7 @@ class DescriptorBuilder::OptionInterpreter::AggregateOptionFinder } } } - return NULL; + return nullptr; } }; @@ -6975,10 +6956,10 @@ class DescriptorBuilder::OptionInterpreter::AggregateOptionFinder namespace { class AggregateErrorCollector : public io::ErrorCollector { public: - string error_; + std::string error_; void AddError(int /* line */, int /* column */, - const string& message) override { + const std::string& message) override { if (!error_.empty()) { error_ += "; "; } @@ -6986,7 +6967,7 @@ class AggregateErrorCollector : public io::ErrorCollector { } void AddWarning(int /* line */, int /* column */, - const string& /* message */) override { + const std::string& /* message */) override { // Ignore warnings } }; @@ -6996,21 +6977,21 @@ class AggregateErrorCollector : public io::ErrorCollector { // option_field, parse the supplied text-format string into this // message, and serialize the resulting message to produce the value. bool DescriptorBuilder::OptionInterpreter::SetAggregateOption( - const FieldDescriptor* option_field, - UnknownFieldSet* unknown_fields) { + const FieldDescriptor* option_field, UnknownFieldSet* unknown_fields) { if (!uninterpreted_option_->has_aggregate_value()) { return AddValueError("Option \"" + option_field->full_name() + "\" is a message. To set the entire message, use " - "syntax like \"" + option_field->name() + + "syntax like \"" + + option_field->name() + " = { }\". " "To set fields within it, use " - "syntax like \"" + option_field->name() + - ".foo = value\"."); + "syntax like \"" + + option_field->name() + ".foo = value\"."); } const Descriptor* type = option_field->message_type(); std::unique_ptr dynamic(dynamic_factory_.GetPrototype(type)->New()); - GOOGLE_CHECK(dynamic.get() != NULL) + GOOGLE_CHECK(dynamic.get() != nullptr) << "Could not create an instance of " << option_field->DebugString(); AggregateErrorCollector collector; @@ -7025,12 +7006,12 @@ bool DescriptorBuilder::OptionInterpreter::SetAggregateOption( option_field->name() + "\": " + collector.error_); return false; } else { - string serial; + std::string serial; dynamic->SerializeToString(&serial); // Never fails if (option_field->type() == FieldDescriptor::TYPE_MESSAGE) { unknown_fields->AddLengthDelimited(option_field->number(), serial); } else { - GOOGLE_CHECK_EQ(option_field->type(), FieldDescriptor::TYPE_GROUP); + GOOGLE_CHECK_EQ(option_field->type(), FieldDescriptor::TYPE_GROUP); UnknownFieldSet* group = unknown_fields->AddGroup(option_field->number()); group->ParseFromString(serial); } @@ -7038,12 +7019,13 @@ bool DescriptorBuilder::OptionInterpreter::SetAggregateOption( } } -void DescriptorBuilder::OptionInterpreter::SetInt32(int number, int32 value, - FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { +void DescriptorBuilder::OptionInterpreter::SetInt32( + int number, int32 value, FieldDescriptor::Type type, + UnknownFieldSet* unknown_fields) { switch (type) { case FieldDescriptor::TYPE_INT32: unknown_fields->AddVarint(number, - static_cast(static_cast(value))); + static_cast(static_cast(value))); break; case FieldDescriptor::TYPE_SFIXED32: @@ -7061,8 +7043,9 @@ void DescriptorBuilder::OptionInterpreter::SetInt32(int number, int32 value, } } -void DescriptorBuilder::OptionInterpreter::SetInt64(int number, int64 value, - FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { +void DescriptorBuilder::OptionInterpreter::SetInt64( + int number, int64 value, FieldDescriptor::Type type, + UnknownFieldSet* unknown_fields) { switch (type) { case FieldDescriptor::TYPE_INT64: unknown_fields->AddVarint(number, static_cast(value)); @@ -7083,8 +7066,9 @@ void DescriptorBuilder::OptionInterpreter::SetInt64(int number, int64 value, } } -void DescriptorBuilder::OptionInterpreter::SetUInt32(int number, uint32 value, - FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { +void DescriptorBuilder::OptionInterpreter::SetUInt32( + int number, uint32 value, FieldDescriptor::Type type, + UnknownFieldSet* unknown_fields) { switch (type) { case FieldDescriptor::TYPE_UINT32: unknown_fields->AddVarint(number, static_cast(value)); @@ -7100,8 +7084,9 @@ void DescriptorBuilder::OptionInterpreter::SetUInt32(int number, uint32 value, } } -void DescriptorBuilder::OptionInterpreter::SetUInt64(int number, uint64 value, - FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { +void DescriptorBuilder::OptionInterpreter::SetUInt64( + int number, uint64 value, FieldDescriptor::Type type, + UnknownFieldSet* unknown_fields) { switch (type) { case FieldDescriptor::TYPE_UINT64: unknown_fields->AddVarint(number, value); @@ -7121,7 +7106,7 @@ void DescriptorBuilder::LogUnusedDependency(const FileDescriptorProto& proto, const FileDescriptor* result) { if (!unused_dependency_.empty()) { - std::set annotation_extensions; + std::set annotation_extensions; annotation_extensions.insert("google.protobuf.MessageOptions"); annotation_extensions.insert("google.protobuf.FileOptions"); annotation_extensions.insert("google.protobuf.FieldOptions"); @@ -7131,31 +7116,32 @@ void DescriptorBuilder::LogUnusedDependency(const FileDescriptorProto& proto, annotation_extensions.insert("google.protobuf.ServiceOptions"); annotation_extensions.insert("google.protobuf.MethodOptions"); annotation_extensions.insert("google.protobuf.StreamOptions"); - for (std::set::const_iterator - it = unused_dependency_.begin(); + for (std::set::const_iterator it = + unused_dependency_.begin(); it != unused_dependency_.end(); ++it) { // Do not log warnings for proto files which extend annotations. int i; - for (i = 0 ; i < (*it)->extension_count(); ++i) { + for (i = 0; i < (*it)->extension_count(); ++i) { if (annotation_extensions.find( - (*it)->extension(i)->containing_type()->full_name()) - != annotation_extensions.end()) { + (*it)->extension(i)->containing_type()->full_name()) != + annotation_extensions.end()) { break; } } // Log warnings for unused imported files. if (i == (*it)->extension_count()) { - string error_message = "Import " + (*it)->name() + " but not used."; - AddWarning((*it)->name(), proto, DescriptorPool::ErrorCollector::OTHER, + std::string error_message = + "Import " + (*it)->name() + " but not used."; + AddWarning((*it)->name(), proto, DescriptorPool::ErrorCollector::IMPORT, error_message); } } } } -Symbol DescriptorPool::CrossLinkOnDemandHelper(const string& name, +Symbol DescriptorPool::CrossLinkOnDemandHelper(const std::string& name, bool expecting_enum) const { - string lookup_name = name; + std::string lookup_name = name; if (!lookup_name.empty() && lookup_name[0] == '.') { lookup_name = lookup_name.substr(1); } @@ -7184,10 +7170,10 @@ void FieldDescriptor::InternalTypeOnceInit() const { if (default_value_enum_name_) { // Have to build the full name now instead of at CrossLink time, // because enum_type_ may not be known at the time. - string name = enum_type_->full_name(); + std::string name = enum_type_->full_name(); // Enum values reside in the same scope as the enum type. - string::size_type last_dot = name.find_last_of('.'); - if (last_dot != string::npos) { + std::string::size_type last_dot = name.find_last_of('.'); + if (last_dot != std::string::npos) { name = name.substr(0, last_dot) + "." + *default_value_enum_name_; } else { name = *default_value_enum_name_; @@ -7234,6 +7220,15 @@ const EnumValueDescriptor* FieldDescriptor::default_value_enum() const { return default_value_enum_; } +const std::string& FieldDescriptor::PrintableNameForExtension() const { + const bool is_message_set_extension = + is_extension() && + containing_type()->options().message_set_wire_format() && + type() == FieldDescriptor::TYPE_MESSAGE && is_optional() && + extension_scope() == message_type(); + return is_message_set_extension ? message_type()->full_name() : full_name(); +} + void FileDescriptor::InternalDependenciesOnceInit() const { GOOGLE_CHECK(finished_building_ == true); for (int i = 0; i < dependency_count(); i++) { @@ -7249,7 +7244,7 @@ void FileDescriptor::DependenciesOnceInit(const FileDescriptor* to_init) { const FileDescriptor* FileDescriptor::dependency(int index) const { if (dependencies_once_) { - // Do once init for all indicies, as it's unlikely only a single index would + // Do once init for all indices, as it's unlikely only a single index would // be called, and saves on internal::call_once allocations. internal::call_once(*dependencies_once_, FileDescriptor::DependenciesOnceInit, this); @@ -7274,7 +7269,8 @@ void LazyDescriptor::Set(const Descriptor* descriptor) { descriptor_ = descriptor; } -void LazyDescriptor::SetLazy(const string& name, const FileDescriptor* file) { +void LazyDescriptor::SetLazy(const std::string& name, + const FileDescriptor* file) { // verify Init() has been called and Set hasn't been called yet. GOOGLE_CHECK(!descriptor_); GOOGLE_CHECK(!file_); diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index 08caab2eb0..a51d93bfe4 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -62,11 +62,18 @@ #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 { @@ -106,6 +113,7 @@ class SourceCodeInfo; // Defined in message.h class Message; +class Reflection; // Defined in descriptor.cc class DescriptorBuilder; @@ -115,11 +123,6 @@ struct Symbol; // Defined in unknown_field_set.h. class UnknownField; -// Defined in generated_message_reflection.h. -namespace internal { -class GeneratedMessageReflection; -} // namespace internal - // Defined in command_line_interface.cc namespace compiler { class CommandLineInterface; @@ -147,9 +150,9 @@ struct SourceLocation { // Doc comments found at the source location. // See the comments in SourceCodeInfo.Location (descriptor.proto) for details. - string leading_comments; - string trailing_comments; - std::vector leading_detached_comments; + std::string leading_comments; + std::string trailing_comments; + std::vector leading_detached_comments; }; // Options when generating machine-parsable output from a descriptor with @@ -167,7 +170,8 @@ struct DebugStringOptions { DebugStringOptions() : include_comments(false), elide_group_body(false), - elide_oneof_body(false) {} + elide_oneof_body(false) { + } }; // A class to handle the simplest cases of a lazily linked descriptor @@ -175,15 +179,15 @@ struct DebugStringOptions { // which is needed when a pool has lazily_build_dependencies_ set. // Must be instantiated as mutable in a descriptor. namespace internal { -class LIBPROTOBUF_EXPORT LazyDescriptor { +class PROTOBUF_EXPORT LazyDescriptor { public: // Init function to be called at init time of a descriptor containing // a LazyDescriptor. void Init() { - descriptor_ = NULL; - name_ = NULL; - once_ = NULL; - file_ = NULL; + descriptor_ = nullptr; + name_ = nullptr; + once_ = nullptr; + file_ = nullptr; } // Sets the value of the descriptor if it is known during the descriptor @@ -197,7 +201,7 @@ class LIBPROTOBUF_EXPORT LazyDescriptor { // 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(const string& name, const FileDescriptor* file); + void SetLazy(const std::string& 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, @@ -213,7 +217,7 @@ class LIBPROTOBUF_EXPORT LazyDescriptor { void Once(); const Descriptor* descriptor_; - const string* name_; + const std::string* name_; internal::once_flag* once_; const FileDescriptor* file_; }; @@ -224,27 +228,29 @@ class LIBPROTOBUF_EXPORT LazyDescriptor { // 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 LIBPROTOBUF_EXPORT Descriptor { +class PROTOBUF_EXPORT Descriptor { public: + typedef DescriptorProto Proto; + // The name of the message type, not including its scope. - const string& name() const; + 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 string& full_name() const; + 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 NULL. + // 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 NULL. + // 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 @@ -260,11 +266,11 @@ class LIBPROTOBUF_EXPORT Descriptor { // Write the contents of this decriptor in a human-readable form. Output // will be suitable for re-parsing. - string DebugString() const; + std::string DebugString() const; // Similar to DebugString(), but additionally takes options (e.g., // include original user comments in output). - string DebugStringWithOptions(const DebugStringOptions& options) const; + 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 @@ -279,24 +285,24 @@ class LIBPROTOBUF_EXPORT Descriptor { // 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 NULL if no such field + // 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 NULL if no such field exists. - const FieldDescriptor* FindFieldByName(const string& name) const; + // Looks up a field by name. Returns nullptr if no such field exists. + const FieldDescriptor* FindFieldByName(const std::string& 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( - const string& lowercase_name) const; + const std::string& 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( - const string& camelcase_name) const; + const std::string& camelcase_name) const; // The number of oneofs in this message type. int oneof_decl_count() const; @@ -304,8 +310,8 @@ class LIBPROTOBUF_EXPORT Descriptor { // 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 NULL if no such oneof exists. - const OneofDescriptor* FindOneofByName(const string& name) const; + // Looks up a oneof by name. Returns nullptr if no such oneof exists. + const OneofDescriptor* FindOneofByName(const std::string& name) const; // Nested type stuff ----------------------------------------------- @@ -315,9 +321,9 @@ class LIBPROTOBUF_EXPORT Descriptor { // 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 NULL if no such nested type + // Looks up a nested type by name. Returns nullptr if no such nested type // exists. - const Descriptor* FindNestedTypeByName(const string& name) const; + const Descriptor* FindNestedTypeByName(const std::string& name) const; // Enum stuff ------------------------------------------------------ @@ -327,18 +333,21 @@ class LIBPROTOBUF_EXPORT Descriptor { // 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 NULL if no such enum type exists. - const EnumDescriptor* FindEnumTypeByName(const string& name) const; + // Looks up an enum type by name. Returns nullptr if no such enum type + // exists. + const EnumDescriptor* FindEnumTypeByName(const std::string& name) const; // Looks up an enum value by name, among all enum types in this message. - // Returns NULL if no such value exists. - const EnumValueDescriptor* FindEnumValueByName(const string& name) const; + // Returns nullptr if no such value exists. + const EnumValueDescriptor* FindEnumValueByName(const std::string& 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(). @@ -360,7 +369,7 @@ class LIBPROTOBUF_EXPORT Descriptor { // Returns true if the number is in one of the extension ranges. bool IsExtensionNumber(int number) const; - // Returns NULL if no extension range contains the given number. + // Returns nullptr if no extension range contains the given number. const ExtensionRange* FindExtensionRangeContainingNumber(int number) const; // The number of extensions -- extending *other* messages -- that were @@ -372,15 +381,17 @@ class LIBPROTOBUF_EXPORT Descriptor { // Looks up a named extension (which extends some *other* message type) // defined within this message type's scope. - const FieldDescriptor* FindExtensionByName(const string& name) const; + const FieldDescriptor* FindExtensionByName(const std::string& name) const; // Similar to FindFieldByLowercaseName(), but finds extensions defined within // this message type's scope. - const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const; + const FieldDescriptor* FindExtensionByLowercaseName( + const std::string& name) const; // Similar to FindFieldByCamelcaseName(), but finds extensions defined within // this message type's scope. - const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const; + const FieldDescriptor* FindExtensionByCamelcaseName( + const std::string& name) const; // Reserved fields ------------------------------------------------- @@ -400,17 +411,17 @@ class LIBPROTOBUF_EXPORT Descriptor { // Returns true if the number is in one of the reserved ranges. bool IsReservedNumber(int number) const; - // Returns NULL if no reserved range contains the given number. + // 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 string& reserved_name(int index) const; + const std::string& reserved_name(int index) const; // Returns true if the field name is reserved. - bool IsReservedName(const string& name) const; + bool IsReservedName(const std::string& name) const; // Source Location --------------------------------------------------- @@ -436,7 +447,7 @@ class LIBPROTOBUF_EXPORT Descriptor { // 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, string *contents, + void DebugString(int depth, std::string* contents, const DebugStringOptions& options, bool include_opening_clause) const; @@ -444,8 +455,8 @@ class LIBPROTOBUF_EXPORT Descriptor { // to this descriptor from the file root. void GetLocationPath(std::vector* output) const; - const string* name_; - const string* full_name_; + const std::string* name_; + const std::string* full_name_; const FileDescriptor* file_; const Descriptor* containing_type_; const MessageOptions* options_; @@ -458,7 +469,7 @@ class LIBPROTOBUF_EXPORT Descriptor { ExtensionRange* extension_ranges_; FieldDescriptor* extensions_; ReservedRange* reserved_ranges_; - const string** reserved_names_; + const std::string** reserved_names_; int field_count_; int oneof_decl_count_; @@ -498,72 +509,72 @@ class LIBPROTOBUF_EXPORT Descriptor { // - Get the Descriptor or FileDescriptor for its containing scope, then // call Descriptor::FindExtensionByName() or // FileDescriptor::FindExtensionByName(). -// - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber(). -// - Given a Reflection for a message object, call -// Reflection::FindKnownExtensionByName() or -// Reflection::FindKnownExtensionByNumber(). +// - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber() or +// DescriptorPool::FindExtensionByPrintableName(). // Use DescriptorPool to construct your own descriptors. -class LIBPROTOBUF_EXPORT FieldDescriptor { +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. + 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. + 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 + LABEL_OPTIONAL = 1, // optional + LABEL_REQUIRED = 2, // required + LABEL_REPEATED = 3, // repeated - MAX_LABEL = 3, // Constant useful for defining lookup tables - // indexed by Label. + MAX_LABEL = 3, // Constant useful for defining lookup tables + // indexed by Label. }; // Valid field numbers are positive integers up to kMaxNumber. @@ -574,14 +585,14 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { 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; + static const int kLastReservedNumber = 19999; - const string& name() const; // Name of this field within the message. - const string& full_name() const; // Fully-qualified name of the field. - const 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. + 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 @@ -589,7 +600,7 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // 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 string& lowercase_name() const; + 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 @@ -600,7 +611,7 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // fooBar -> fooBar // This (and especially the FindFieldByCamelcaseName() method) can be useful // when parsing formats which prefer to use camel-case naming style. - const string& camelcase_name() const; + const std::string& camelcase_name() const; Type type() const; // Declared type of this field. const char* type_name() const; // Name of the declared type. @@ -608,15 +619,15 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { 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() + 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() // Index of this field within the message's field array, or the file or // extension scope's extensions array. @@ -653,18 +664,18 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // 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 NULL. + // 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 string& default_value_string() const; + 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 NULL. + // 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 - // NULL. + // nullptr. const OneofDescriptor* containing_oneof() const; // If the field is a member of a oneof, returns the index in that oneof. @@ -672,7 +683,7 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // 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 NULL if the extension was declared at global + // 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; @@ -695,10 +706,10 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { void CopyTo(FieldDescriptorProto* proto) const; // See Descriptor::DebugString(). - string DebugString() const; + std::string DebugString() const; // See Descriptor::DebugStringWithOptions(). - string DebugStringWithOptions(const DebugStringOptions& options) const; + std::string DebugStringWithOptions(const DebugStringOptions& options) const; // Helper method to get the CppType for a particular Type. static CppType TypeToCppType(Type type); @@ -712,6 +723,21 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // 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 @@ -732,15 +758,16 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // See Descriptor::DebugString(). enum PrintLabelFlag { PRINT_LABEL, OMIT_LABEL }; void DebugString(int depth, PrintLabelFlag print_label_flag, - string* contents, const DebugStringOptions& options) const; + 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. - string DefaultValueAsString(bool quote_string_type) const; + std::string DefaultValueAsString(bool quote_string_type) const; // Helper function that returns the field type name for DebugString. - string FieldTypeNameDebugString() const; + std::string FieldTypeNameDebugString() const; // Walks up the descriptor tree to generate the source location path // to this descriptor from the file root. @@ -749,13 +776,13 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // Returns true if this is a map message type. bool is_map_message_type() const; - const string* name_; - const string* full_name_; - const string* lowercase_name_; - const string* camelcase_name_; + 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 string* json_name_; + const std::string* json_name_; const FileDescriptor* file_; internal::once_flag* type_once_; static void TypeOnceInit(const FieldDescriptor* to_init); @@ -775,32 +802,32 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { mutable const Descriptor* message_type_; mutable const EnumDescriptor* enum_type_; const FieldOptions* options_; - const string* type_name_; - const string* default_value_enum_name_; + 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_; + int32 default_value_int32_; + int64 default_value_int64_; uint32 default_value_uint32_; uint64 default_value_uint64_; - float default_value_float_; + float default_value_float_; double default_value_double_; - bool default_value_bool_; + bool default_value_bool_; mutable const EnumValueDescriptor* default_value_enum_; - const string* default_value_string_; + const std::string* default_value_string_; }; static const CppType kTypeToCppTypeMap[MAX_TYPE + 1]; - static const char * const kTypeToName[MAX_TYPE + 1]; + static const char* const kTypeToName[MAX_TYPE + 1]; - static const char * const kCppTypeToName[MAX_CPPTYPE + 1]; + static const char* const kCppTypeToName[MAX_CPPTYPE + 1]; - static const char * const kLabelToName[MAX_LABEL + 1]; + static const char* const kLabelToName[MAX_LABEL + 1]; // Must be constructed using DescriptorPool. FieldDescriptor() {} @@ -813,15 +840,17 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // Describes a oneof defined in a message type. -class LIBPROTOBUF_EXPORT OneofDescriptor { +class PROTOBUF_EXPORT OneofDescriptor { public: - const string& name() const; // Name of this oneof. - const string& full_name() const; // Fully-qualified name of the oneof. + 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; - // The .proto file in which this oneof was defined. Never NULL. + // 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; @@ -838,10 +867,10 @@ class LIBPROTOBUF_EXPORT OneofDescriptor { void CopyTo(OneofDescriptorProto* proto) const; // See Descriptor::DebugString(). - string DebugString() const; + std::string DebugString() const; // See Descriptor::DebugStringWithOptions(). - string DebugStringWithOptions(const DebugStringOptions& options) const; + std::string DebugStringWithOptions(const DebugStringOptions& options) const; // Source Location --------------------------------------------------- @@ -858,17 +887,16 @@ class LIBPROTOBUF_EXPORT OneofDescriptor { friend class compiler::cpp::Formatter; // See Descriptor::DebugString(). - void DebugString(int depth, string* contents, + 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 string* name_; - const string* full_name_; + const std::string* name_; + const std::string* full_name_; const Descriptor* containing_type_; - bool is_extendable_; int field_count_; const FieldDescriptor** fields_; const OneofOptions* options_; @@ -887,18 +915,20 @@ class LIBPROTOBUF_EXPORT 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 LIBPROTOBUF_EXPORT EnumDescriptor { +class PROTOBUF_EXPORT EnumDescriptor { public: + typedef EnumDescriptorProto Proto; + // The name of this enum type in the containing scope. - const string& name() const; + const std::string& name() const; // The fully-qualified name of the enum type, scope delimited by periods. - const string& full_name() const; + 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 NULL. + // 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 @@ -908,14 +938,14 @@ class LIBPROTOBUF_EXPORT EnumDescriptor { // 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 NULL if no such value exists. - const EnumValueDescriptor* FindValueByName(const string& name) const; - // Looks up a value by number. Returns NULL if no such value exists. If + // Looks up a value by name. Returns nullptr if no such value exists. + const EnumValueDescriptor* FindValueByName(const std::string& 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, NULL. + // Otherwise, nullptr. const Descriptor* containing_type() const; // Get options for this enum type. These are specified in the .proto file by @@ -928,10 +958,10 @@ class LIBPROTOBUF_EXPORT EnumDescriptor { void CopyTo(EnumDescriptorProto* proto) const; // See Descriptor::DebugString(). - string DebugString() const; + std::string DebugString() const; // See Descriptor::DebugStringWithOptions(). - string DebugStringWithOptions(const DebugStringOptions& options) const; + 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 @@ -956,18 +986,18 @@ class LIBPROTOBUF_EXPORT EnumDescriptor { // Returns true if the number is in one of the reserved ranges. bool IsReservedNumber(int number) const; - // Returns NULL if no reserved range contains the given number. - const EnumDescriptor::ReservedRange* - FindReservedRangeContainingNumber(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 string& reserved_name(int index) const; + const std::string& reserved_name(int index) const; // Returns true if the field name is reserved. - bool IsReservedName(const string& name) const; + bool IsReservedName(const std::string& name) const; // Source Location --------------------------------------------------- @@ -988,23 +1018,22 @@ class LIBPROTOBUF_EXPORT EnumDescriptor { // 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 GeneratedMessageReflection (which is - // friended below) to return a valid EnumValueDescriptor from GetEnum() when - // this feature is enabled. - const EnumValueDescriptor* - FindValueByNumberCreatingIfUnknown(int number) const; - + // 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, string *contents, + 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 string* name_; - const string* full_name_; + const std::string* name_; + const std::string* full_name_; const FileDescriptor* file_; const Descriptor* containing_type_; const EnumOptions* options_; @@ -1020,7 +1049,7 @@ class LIBPROTOBUF_EXPORT EnumDescriptor { int reserved_range_count_; int reserved_name_count_; EnumDescriptor::ReservedRange* reserved_ranges_; - const string** reserved_names_; + const std::string** reserved_names_; // IMPORTANT: If you add a new field, make sure to search for all instances // of Allocate() and AllocateArray() in @@ -1034,7 +1063,7 @@ class LIBPROTOBUF_EXPORT EnumDescriptor { friend class EnumValueDescriptor; friend class FileDescriptor; friend class DescriptorPool; - friend class internal::GeneratedMessageReflection; + friend class Reflection; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor); }; @@ -1043,22 +1072,24 @@ class LIBPROTOBUF_EXPORT EnumDescriptor { // for its type, then use EnumDescriptor::FindValueByName() or // EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct // your own descriptors. -class LIBPROTOBUF_EXPORT EnumValueDescriptor { +class PROTOBUF_EXPORT EnumValueDescriptor { public: - const 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. + 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 string& full_name() const; + const std::string& full_name() const; - // The .proto file in which this value was defined. Never NULL. + // The .proto file in which this value was defined. Never nullptr. const FileDescriptor* file() const; - // The type of this value. Never NULL. + // 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 @@ -1071,11 +1102,10 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptor { void CopyTo(EnumValueDescriptorProto* proto) const; // See Descriptor::DebugString(). - string DebugString() const; + std::string DebugString() const; // See Descriptor::DebugStringWithOptions(). - string DebugStringWithOptions(const DebugStringOptions& options) const; - + std::string DebugStringWithOptions(const DebugStringOptions& options) const; // Source Location --------------------------------------------------- @@ -1092,15 +1122,15 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptor { friend class compiler::cpp::Formatter; // See Descriptor::DebugString(). - void DebugString(int depth, string *contents, + 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 string* name_; - const string* full_name_; + const std::string* name_; + const std::string* full_name_; int number_; const EnumDescriptor* type_; const EnumValueOptions* options_; @@ -1117,20 +1147,20 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptor { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor); }; -// Describes an RPC service. To get the ServiceDescriptor for a service, -// call Service::GetDescriptor(). Generated service classes also have a -// static method called descriptor() which returns the type's -// ServiceDescriptor. Use DescriptorPool to construct your own descriptors. -class LIBPROTOBUF_EXPORT ServiceDescriptor { +// 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 string& name() const; + const std::string& name() const; // The fully-qualified name of the service, scope delimited by periods. - const string& full_name() const; + 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 NULL. + // 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 @@ -1146,16 +1176,15 @@ class LIBPROTOBUF_EXPORT ServiceDescriptor { const MethodDescriptor* method(int index) const; // Look up a MethodDescriptor by name. - const MethodDescriptor* FindMethodByName(const string& name) const; + const MethodDescriptor* FindMethodByName(const std::string& name) const; // See Descriptor::CopyTo(). void CopyTo(ServiceDescriptorProto* proto) const; // See Descriptor::DebugString(). - string DebugString() const; + std::string DebugString() const; // See Descriptor::DebugStringWithOptions(). - string DebugStringWithOptions(const DebugStringOptions& options) const; - + std::string DebugStringWithOptions(const DebugStringOptions& options) const; // Source Location --------------------------------------------------- @@ -1172,14 +1201,15 @@ class LIBPROTOBUF_EXPORT ServiceDescriptor { friend class compiler::cpp::Formatter; // See Descriptor::DebugString(). - void DebugString(string *contents, const DebugStringOptions& options) const; + 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 string* name_; - const string* full_name_; + const std::string* name_; + const std::string* full_name_; const FileDescriptor* file_; const ServiceOptions* options_; MethodDescriptor* methods_; @@ -1201,18 +1231,20 @@ class LIBPROTOBUF_EXPORT ServiceDescriptor { // a service, first get its ServiceDescriptor, then call // ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your // own descriptors. -class LIBPROTOBUF_EXPORT MethodDescriptor { +class PROTOBUF_EXPORT MethodDescriptor { public: + typedef MethodDescriptorProto Proto; + // Name of this method, not including containing scope. - const string& name() const; + const std::string& name() const; // The fully-qualified name of the method, scope delimited by periods. - const string& full_name() const; + 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 NULL. + // The .proto file in which this method was defined. Never nullptr. const FileDescriptor* file() const; - // Gets the service to which this method belongs. Never NULL. + // 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. @@ -1235,11 +1267,10 @@ class LIBPROTOBUF_EXPORT MethodDescriptor { void CopyTo(MethodDescriptorProto* proto) const; // See Descriptor::DebugString(). - string DebugString() const; + std::string DebugString() const; // See Descriptor::DebugStringWithOptions(). - string DebugStringWithOptions(const DebugStringOptions& options) const; - + std::string DebugStringWithOptions(const DebugStringOptions& options) const; // Source Location --------------------------------------------------- @@ -1256,15 +1287,15 @@ class LIBPROTOBUF_EXPORT MethodDescriptor { friend class compiler::cpp::Formatter; // See Descriptor::DebugString(). - void DebugString(int depth, string *contents, + 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 string* name_; - const string* full_name_; + const std::string* name_; + const std::string* full_name_; const ServiceDescriptor* service_; mutable internal::LazyDescriptor input_type_; mutable internal::LazyDescriptor output_type_; @@ -1286,17 +1317,19 @@ class LIBPROTOBUF_EXPORT 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 LIBPROTOBUF_EXPORT FileDescriptor { +class PROTOBUF_EXPORT FileDescriptor { public: + typedef FileDescriptorProto Proto; + // The filename, relative to the source tree. // e.g. "foo/bar/baz.proto" - const string& name() const; + const std::string& name() const; // The package, e.g. "google.protobuf.compiler". - const string& package() const; + const std::string& package() const; // The DescriptorPool in which this FileDescriptor and all its contents were - // allocated. Never NULL. + // allocated. Never nullptr. const DescriptorPool* pool() const; // The number of files imported by this one. @@ -1357,29 +1390,32 @@ class LIBPROTOBUF_EXPORT FileDescriptor { // Syntax of this file. enum Syntax { SYNTAX_UNKNOWN = 0, - SYNTAX_PROTO2 = 2, - SYNTAX_PROTO3 = 3, + SYNTAX_PROTO2 = 2, + SYNTAX_PROTO3 = 3, }; Syntax syntax() const; static const char* SyntaxName(Syntax syntax); - // Find a top-level message type by name. Returns NULL if not found. - const Descriptor* FindMessageTypeByName(const string& name) const; - // Find a top-level enum type by name. Returns NULL if not found. - const EnumDescriptor* FindEnumTypeByName(const string& name) const; - // Find an enum value defined in any top-level enum by name. Returns NULL if - // not found. - const EnumValueDescriptor* FindEnumValueByName(const string& name) const; - // Find a service definition by name. Returns NULL if not found. - const ServiceDescriptor* FindServiceByName(const string& name) const; - // Find a top-level extension definition by name. Returns NULL if not found. - const FieldDescriptor* FindExtensionByName(const string& name) const; + // Find a top-level message type by name. Returns nullptr if not found. + const Descriptor* FindMessageTypeByName(const std::string& name) const; + // Find a top-level enum type by name. Returns nullptr if not found. + const EnumDescriptor* FindEnumTypeByName(const std::string& name) const; + // Find an enum value defined in any top-level enum by name. Returns nullptr + // if not found. + const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const; + // Find a service definition by name. Returns nullptr if not found. + const ServiceDescriptor* FindServiceByName(const std::string& name) const; + // Find a top-level extension definition by name. Returns nullptr if not + // found. + const FieldDescriptor* FindExtensionByName(const std::string& name) const; // Similar to FindExtensionByName(), but searches by lowercased-name. See // Descriptor::FindFieldByLowercaseName(). - const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const; + const FieldDescriptor* FindExtensionByLowercaseName( + const std::string& name) const; // Similar to FindExtensionByName(), but searches by camelcased-name. See // Descriptor::FindFieldByCamelcaseName(). - const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const; + const FieldDescriptor* FindExtensionByCamelcaseName( + const std::string& name) const; // See Descriptor::CopyTo(). // Notes: @@ -1394,10 +1430,10 @@ class LIBPROTOBUF_EXPORT FileDescriptor { void CopyJsonNameTo(FileDescriptorProto* proto) const; // See Descriptor::DebugString(). - string DebugString() const; + std::string DebugString() const; // See Descriptor::DebugStringWithOptions(). - string DebugStringWithOptions(const DebugStringOptions& options) const; + 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 @@ -1419,8 +1455,8 @@ class LIBPROTOBUF_EXPORT FileDescriptor { private: typedef FileOptions OptionsType; - const string* name_; - const string* package_; + const std::string* name_; + const std::string* package_; const DescriptorPool* pool_; internal::once_flag* dependencies_once_; static void DependenciesOnceInit(const FileDescriptor* to_init); @@ -1443,7 +1479,7 @@ class LIBPROTOBUF_EXPORT FileDescriptor { bool finished_building_; mutable const FileDescriptor** dependencies_; - const string** dependencies_names_; + const std::string** dependencies_names_; int* public_dependencies_; int* weak_dependencies_; Descriptor* message_types_; @@ -1500,7 +1536,7 @@ class LIBPROTOBUF_EXPORT FileDescriptor { // // You can also search for descriptors within a DescriptorPool by name, and // extensions by number. -class LIBPROTOBUF_EXPORT DescriptorPool { +class PROTOBUF_EXPORT DescriptorPool { public: // Create a normal, empty DescriptorPool. DescriptorPool(); @@ -1530,7 +1566,7 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // in subsequent lookups in the DescriptorPool. class ErrorCollector; explicit DescriptorPool(DescriptorDatabase* fallback_database, - ErrorCollector* error_collector = NULL); + ErrorCollector* error_collector = nullptr); ~DescriptorPool(); @@ -1540,36 +1576,44 @@ class LIBPROTOBUF_EXPORT DescriptorPool { static const DescriptorPool* generated_pool(); - // Find a FileDescriptor in the pool by file name. Returns NULL if not + // Find a FileDescriptor in the pool by file name. Returns nullptr if not // found. - const FileDescriptor* FindFileByName(const string& name) const; + const FileDescriptor* FindFileByName(const std::string& 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 NULL. + // Otherwise this returns nullptr. const FileDescriptor* FindFileContainingSymbol( - const string& symbol_name) const; + const std::string& 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 NULL if not + // top-level descriptors and nested descriptors. They return nullptr if not // found. - const Descriptor* FindMessageTypeByName(const string& name) const; - const FieldDescriptor* FindFieldByName(const string& name) const; - const FieldDescriptor* FindExtensionByName(const string& name) const; - const OneofDescriptor* FindOneofByName(const string& name) const; - const EnumDescriptor* FindEnumTypeByName(const string& name) const; - const EnumValueDescriptor* FindEnumValueByName(const string& name) const; - const ServiceDescriptor* FindServiceByName(const string& name) const; - const MethodDescriptor* FindMethodByName(const string& name) const; + const Descriptor* FindMessageTypeByName(const std::string& name) const; + const FieldDescriptor* FindFieldByName(const std::string& name) const; + const FieldDescriptor* FindExtensionByName(const std::string& name) const; + const OneofDescriptor* FindOneofByName(const std::string& name) const; + const EnumDescriptor* FindEnumTypeByName(const std::string& name) const; + const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const; + const ServiceDescriptor* FindServiceByName(const std::string& name) const; + const MethodDescriptor* FindMethodByName(const std::string& 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, const std::string& 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 @@ -1583,7 +1627,7 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // 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 LIBPROTOBUF_EXPORT ErrorCollector { + class PROTOBUF_EXPORT ErrorCollector { public: inline ErrorCollector() {} virtual ~ErrorCollector(); @@ -1592,37 +1636,40 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // 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 - OTHER // some other problem + 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 string& filename, // File name in which the error occurred. - const 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 string& message // Human-readable error message. - ) = 0; + 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 string& /*filename*/, // File name in which the error occurred. - const 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 string& /*message*/ // Human-readable error message. - ) {} + 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); @@ -1630,15 +1677,14 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // 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 NULL if there were + // 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); + 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 @@ -1693,8 +1739,8 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // 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); + static void InternalAddGeneratedFile(const void* encoded_file_descriptor, + int size); // Disallow [enforce_utf8 = false] in .proto files. void DisallowEnforceUtf8() { disallow_enforce_utf8_ = true; } @@ -1734,12 +1780,11 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // 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(const string& filename) const; - + bool InternalIsFileLoaded(const std::string& filename) const; // Add a file to unused_import_track_files_. DescriptorBuilder will log // warnings for those files if there is any unused import. - void AddUnusedImportTrackFile(const string& file_name); + void AddUnusedImportTrackFile(const std::string& file_name); void ClearUnusedImportTrackFiles(); private: @@ -1757,14 +1802,14 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // 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(const string& name) const; + bool IsSubSymbolOfBuiltType(const std::string& 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(const string& name) const; - bool TryFindSymbolInFallbackDatabase(const string& name) const; + bool TryFindFileInFallbackDatabase(const std::string& name) const; + bool TryFindSymbolInFallbackDatabase(const std::string& name) const; bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type, int field_number) const; @@ -1772,18 +1817,20 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // fallback_database_. Declared const because it is called by (semantically) // const methods. const FileDescriptor* BuildFileFromDatabase( - const FileDescriptorProto& proto) const; + 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(const string& name, bool expecting_enum) const; + Symbol CrossLinkOnDemandHelper(const std::string& name, + bool expecting_enum) const; // Create a placeholder FileDescriptor of the specified name - FileDescriptor* NewPlaceholderFile(const string& name) const; - FileDescriptor* NewPlaceholderFileWithMutexHeld(const string& name) const; + FileDescriptor* NewPlaceholderFile(const std::string& name) const; + FileDescriptor* NewPlaceholderFileWithMutexHeld( + const std::string& name) const; enum PlaceholderType { PLACEHOLDER_MESSAGE, @@ -1791,13 +1838,13 @@ class LIBPROTOBUF_EXPORT DescriptorPool { PLACEHOLDER_EXTENDABLE_MESSAGE }; // Create a placeholder Descriptor of the specified name - Symbol NewPlaceholder(const string& name, + Symbol NewPlaceholder(const std::string& name, PlaceholderType placeholder_type) const; - Symbol NewPlaceholderWithMutexHeld(const string& name, + Symbol NewPlaceholderWithMutexHeld(const std::string& name, PlaceholderType placeholder_type) const; - // If fallback_database_ is NULL, this is NULL. Otherwise, this is a mutex - // which must be locked while accessing tables_. + // 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. @@ -1815,7 +1862,7 @@ class LIBPROTOBUF_EXPORT DescriptorPool { bool allow_unknown_; bool enforce_weak_; bool disallow_enforce_utf8_; - std::set unused_import_track_files_; + std::set unused_import_track_files_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool); }; @@ -1829,7 +1876,7 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // Strings fields are stored as pointers but returned as const references. #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \ - inline const string& CLASS::FIELD() const { return *FIELD##_; } + inline const std::string& CLASS::FIELD() const { return *FIELD##_; } // Arrays take an index parameter, obviously. #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \ @@ -1857,8 +1904,7 @@ 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_ARRAY_ACCESSOR(Descriptor, extension, const FieldDescriptor*) PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_range_count, int) PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range, @@ -1885,13 +1931,13 @@ 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_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_float, float) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool , bool ) +PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool, bool) PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string) PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, name) @@ -1962,14 +2008,14 @@ PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension, // A few accessors differ from the macros... inline bool Descriptor::IsExtensionNumber(int number) const { - return FindExtensionRangeContainingNumber(number) != NULL; + return FindExtensionRangeContainingNumber(number) != nullptr; } inline bool Descriptor::IsReservedNumber(int number) const { - return FindReservedRangeContainingNumber(number) != NULL; + return FindReservedRangeContainingNumber(number) != nullptr; } -inline bool Descriptor::IsReservedName(const string& name) const { +inline bool Descriptor::IsReservedName(const std::string& name) const { for (int i = 0; i < reserved_name_count(); i++) { if (name == reserved_name(i)) { return true; @@ -1980,15 +2026,15 @@ inline bool Descriptor::IsReservedName(const string& name) const { // 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 string& Descriptor::reserved_name(int index) const { +inline const std::string& Descriptor::reserved_name(int index) const { return *reserved_names_[index]; } inline bool EnumDescriptor::IsReservedNumber(int number) const { - return FindReservedRangeContainingNumber(number) != NULL; + return FindReservedRangeContainingNumber(number) != nullptr; } -inline bool EnumDescriptor::IsReservedName(const string& name) const { +inline bool EnumDescriptor::IsReservedName(const std::string& name) const { for (int i = 0; i < reserved_name_count(); i++) { if (name == reserved_name(i)) { return true; @@ -1999,7 +2045,7 @@ inline bool EnumDescriptor::IsReservedName(const string& name) const { // 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 string& EnumDescriptor::reserved_name(int index) const { +inline const std::string& EnumDescriptor::reserved_name(int index) const { return *reserved_names_[index]; } @@ -2035,7 +2081,7 @@ inline bool FieldDescriptor::is_map() const { inline int FieldDescriptor::index() const { if (!is_extension_) { return static_cast(this - containing_type()->fields_); - } else if (extension_scope_ != NULL) { + } else if (extension_scope_ != nullptr) { return static_cast(this - extension_scope_->extensions_); } else { return static_cast(this - file_->extensions_); @@ -2043,7 +2089,7 @@ inline int FieldDescriptor::index() const { } inline int Descriptor::index() const { - if (containing_type_ == NULL) { + if (containing_type_ == nullptr) { return static_cast(this - file_->message_types_); } else { return static_cast(this - containing_type_->nested_types_); @@ -2059,7 +2105,7 @@ inline int OneofDescriptor::index() const { } inline int EnumDescriptor::index() const { - if (containing_type_ == NULL) { + if (containing_type_ == nullptr) { return static_cast(this - file_->enum_types_); } else { return static_cast(this - containing_type_->enum_types_); @@ -2122,14 +2168,11 @@ inline const FileDescriptor* FileDescriptor::public_dependency( return dependency(public_dependencies_[index]); } -inline const FileDescriptor* FileDescriptor::weak_dependency( - int index) const { +inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const { return dependency(weak_dependencies_[index]); } -inline FileDescriptor::Syntax FileDescriptor::syntax() const { - return syntax_; -} +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. @@ -2140,4 +2183,6 @@ inline const FieldDescriptor* OneofDescriptor::field(int index) const { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_DESCRIPTOR_H__ diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index 01accf9411..8feecb88a1 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -5,630 +5,592 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<2> scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<3> scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<6> scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::google::protobuf::internal::SCCInfo<6> scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; -namespace google { -namespace protobuf { +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<6> scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<3> scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<6> scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<2> scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto; +PROTOBUF_NAMESPACE_OPEN class FileDescriptorSetDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _FileDescriptorSet_default_instance_; class FileDescriptorProtoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _FileDescriptorProto_default_instance_; class DescriptorProto_ExtensionRangeDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _DescriptorProto_ExtensionRange_default_instance_; class DescriptorProto_ReservedRangeDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _DescriptorProto_ReservedRange_default_instance_; class DescriptorProtoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _DescriptorProto_default_instance_; class ExtensionRangeOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _ExtensionRangeOptions_default_instance_; class FieldDescriptorProtoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _FieldDescriptorProto_default_instance_; class OneofDescriptorProtoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _OneofDescriptorProto_default_instance_; class EnumDescriptorProto_EnumReservedRangeDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _EnumDescriptorProto_EnumReservedRange_default_instance_; class EnumDescriptorProtoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _EnumDescriptorProto_default_instance_; class EnumValueDescriptorProtoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _EnumValueDescriptorProto_default_instance_; class ServiceDescriptorProtoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _ServiceDescriptorProto_default_instance_; class MethodDescriptorProtoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _MethodDescriptorProto_default_instance_; class FileOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _FileOptions_default_instance_; class MessageOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _MessageOptions_default_instance_; class FieldOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _FieldOptions_default_instance_; class OneofOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _OneofOptions_default_instance_; class EnumOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _EnumOptions_default_instance_; class EnumValueOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _EnumValueOptions_default_instance_; class ServiceOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _ServiceOptions_default_instance_; class MethodOptionsDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _MethodOptions_default_instance_; class UninterpretedOption_NamePartDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _UninterpretedOption_NamePart_default_instance_; class UninterpretedOptionDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _UninterpretedOption_default_instance_; class SourceCodeInfo_LocationDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _SourceCodeInfo_Location_default_instance_; class SourceCodeInfoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _SourceCodeInfo_default_instance_; class GeneratedCodeInfo_AnnotationDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _GeneratedCodeInfo_Annotation_default_instance_; class GeneratedCodeInfoDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _GeneratedCodeInfo_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsFileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_FileDescriptorSet_default_instance_; - new (ptr) ::google::protobuf::FileDescriptorSet(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_DescriptorProto_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::DescriptorProto(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::FileDescriptorSet::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::DescriptorProto::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsFileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<6> scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 6, InitDefaultsscc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsFileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_FileDescriptorProto_default_instance_; - new (ptr) ::google::protobuf::FileDescriptorProto(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_DescriptorProto_ExtensionRange_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::FileDescriptorProto::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<6> scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 6, InitDefaultsFileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsDescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_DescriptorProto_ExtensionRange_default_instance_; - new (ptr) ::google::protobuf::DescriptorProto_ExtensionRange(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_DescriptorProto_ReservedRange_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::DescriptorProto_ExtensionRange::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsDescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto}, {}}; -static void InitDefaultsDescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_DescriptorProto_ReservedRange_default_instance_; - new (ptr) ::google::protobuf::DescriptorProto_ReservedRange(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_EnumDescriptorProto_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::EnumDescriptorProto(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::DescriptorProto_ReservedRange::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::EnumDescriptorProto::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsDescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto}, {}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<3> scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 3, InitDefaultsscc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_DescriptorProto_default_instance_; - new (ptr) ::google::protobuf::DescriptorProto(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_EnumDescriptorProto_EnumReservedRange_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::DescriptorProto::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<6> scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 6, InitDefaultsDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto}, {}}; -static void InitDefaultsExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_ExtensionRangeOptions_default_instance_; - new (ptr) ::google::protobuf::ExtensionRangeOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_EnumOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::EnumOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::ExtensionRangeOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::EnumOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto}, { +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto}, { &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsFieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_FieldDescriptorProto_default_instance_; - new (ptr) ::google::protobuf::FieldDescriptorProto(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_EnumValueDescriptorProto_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::FieldDescriptorProto::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsFieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsOneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_OneofDescriptorProto_default_instance_; - new (ptr) ::google::protobuf::OneofDescriptorProto(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_EnumValueOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::EnumValueOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::OneofDescriptorProto::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::EnumValueOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsOneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsEnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_EnumDescriptorProto_EnumReservedRange_default_instance_; - new (ptr) ::google::protobuf::EnumDescriptorProto_EnumReservedRange(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_ExtensionRangeOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::EnumDescriptorProto_EnumReservedRange::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsEnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto}, {}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsEnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_EnumDescriptorProto_default_instance_; - new (ptr) ::google::protobuf::EnumDescriptorProto(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_FieldDescriptorProto_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::FieldDescriptorProto(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::EnumDescriptorProto::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<3> scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 3, InitDefaultsEnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsEnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_EnumValueDescriptorProto_default_instance_; - new (ptr) ::google::protobuf::EnumValueDescriptorProto(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_FieldOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::FieldOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::EnumValueDescriptorProto::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::FieldOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsEnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_ServiceDescriptorProto_default_instance_; - new (ptr) ::google::protobuf::ServiceDescriptorProto(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_FileDescriptorProto_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::FileDescriptorProto(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::ServiceDescriptorProto::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::FileDescriptorProto::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<2> scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 2, InitDefaultsServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, - &scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<6> scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 6, InitDefaultsscc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsMethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_MethodDescriptorProto_default_instance_; - new (ptr) ::google::protobuf::MethodDescriptorProto(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_FileDescriptorSet_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::FileDescriptorSet(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::MethodDescriptorProto::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::FileDescriptorSet::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsMethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsFileOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_FileOptions_default_instance_; - new (ptr) ::google::protobuf::FileOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_FileOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::FileOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::FileOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::FileOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsFileOptions_google_2fprotobuf_2fdescriptor_2eproto}, { +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto}, { &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsMessageOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_MessageOptions_default_instance_; - new (ptr) ::google::protobuf::MessageOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_GeneratedCodeInfo_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::MessageOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsMessageOptions_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsFieldOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_FieldOptions_default_instance_; - new (ptr) ::google::protobuf::FieldOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_GeneratedCodeInfo_Annotation_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::FieldOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsFieldOptions_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto}, {}}; -static void InitDefaultsOneofOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_OneofOptions_default_instance_; - new (ptr) ::google::protobuf::OneofOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_MessageOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::MessageOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::OneofOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::MessageOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsOneofOptions_google_2fprotobuf_2fdescriptor_2eproto}, { +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto}, { &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsEnumOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_EnumOptions_default_instance_; - new (ptr) ::google::protobuf::EnumOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_MethodDescriptorProto_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::MethodDescriptorProto(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::EnumOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::MethodDescriptorProto::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsEnumOptions_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsEnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_EnumValueOptions_default_instance_; - new (ptr) ::google::protobuf::EnumValueOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_MethodOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::MethodOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::EnumValueOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::MethodOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsEnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto}, { +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto}, { &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsServiceOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_ServiceOptions_default_instance_; - new (ptr) ::google::protobuf::ServiceOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_OneofDescriptorProto_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::OneofDescriptorProto(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::ServiceOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::OneofDescriptorProto::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsServiceOptions_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsMethodOptions_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_MethodOptions_default_instance_; - new (ptr) ::google::protobuf::MethodOptions(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_OneofOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::OneofOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::MethodOptions::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::OneofOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsMethodOptions_google_2fprotobuf_2fdescriptor_2eproto}, { +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto}, { &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsUninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_UninterpretedOption_NamePart_default_instance_; - new (ptr) ::google::protobuf::UninterpretedOption_NamePart(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_ServiceDescriptorProto_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::UninterpretedOption_NamePart::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsUninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto}, {}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<2> scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 2, InitDefaultsscc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsUninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_UninterpretedOption_default_instance_; - new (ptr) ::google::protobuf::UninterpretedOption(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_ServiceOptions_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::ServiceOptions(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::UninterpretedOption::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::ServiceOptions::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsUninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsSourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_SourceCodeInfo_Location_default_instance_; - new (ptr) ::google::protobuf::SourceCodeInfo_Location(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_SourceCodeInfo_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::SourceCodeInfo(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::SourceCodeInfo_Location::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::SourceCodeInfo::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsSourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto}, {}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsSourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_SourceCodeInfo_default_instance_; - new (ptr) ::google::protobuf::SourceCodeInfo(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_SourceCodeInfo_Location_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::SourceCodeInfo::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsSourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto}, {}}; -static void InitDefaultsGeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_GeneratedCodeInfo_Annotation_default_instance_; - new (ptr) ::google::protobuf::GeneratedCodeInfo_Annotation(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_UninterpretedOption_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::UninterpretedOption(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::GeneratedCodeInfo_Annotation::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::UninterpretedOption::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsGeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto}, {}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsscc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto}, { + &scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -static void InitDefaultsGeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto() { +static void InitDefaultsscc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_GeneratedCodeInfo_default_instance_; - new (ptr) ::google::protobuf::GeneratedCodeInfo(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_UninterpretedOption_NamePart_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::GeneratedCodeInfo::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<1> scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsGeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto}, { - &scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base,}}; +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto}, {}}; + +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[27]; +static const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[6]; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fdescriptor_2eproto = nullptr; -void InitDefaults_google_2fprotobuf_2fdescriptor_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base); - ::google::protobuf::internal::InitSCC(&scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); -} - -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[27]; -const ::google::protobuf::EnumDescriptor* file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[6]; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fdescriptor_2eproto = nullptr; - -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fdescriptor_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorSet, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorSet, _internal_metadata_), +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2fdescriptor_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorSet, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorSet, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorSet, file_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorSet, file_), ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, package_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, dependency_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, public_dependency_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, weak_dependency_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, message_type_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, enum_type_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, service_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, extension_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, options_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, source_code_info_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileDescriptorProto, syntax_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, package_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, dependency_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, public_dependency_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, weak_dependency_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, message_type_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, enum_type_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, service_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, extension_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, source_code_info_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileDescriptorProto, syntax_), 0, 1, ~0u, @@ -641,41 +603,41 @@ const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fdescriptor_2epr 3, 4, 2, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ExtensionRange, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ExtensionRange, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ExtensionRange, start_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ExtensionRange, end_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ExtensionRange, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange, start_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange, end_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange, options_), 1, 2, 0, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ReservedRange, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ReservedRange, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ReservedRange, start_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto_ReservedRange, end_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange, start_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange, end_), 0, 1, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, field_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, extension_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, nested_type_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, enum_type_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, extension_range_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, oneof_decl_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, options_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, reserved_range_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::DescriptorProto, reserved_name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, field_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, extension_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, nested_type_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, enum_type_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, extension_range_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, oneof_decl_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, reserved_range_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::DescriptorProto, reserved_name_), 0, ~0u, ~0u, @@ -686,28 +648,28 @@ const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fdescriptor_2epr 1, ~0u, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ExtensionRangeOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ExtensionRangeOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ExtensionRangeOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ExtensionRangeOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions, uninterpreted_option_), ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, number_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, label_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, type_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, type_name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, extendee_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, default_value_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, oneof_index_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, json_name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldDescriptorProto, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, number_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, label_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, type_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, type_name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, extendee_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, default_value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, oneof_index_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, json_name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto, options_), 0, 6, 8, @@ -718,104 +680,104 @@ const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fdescriptor_2epr 7, 4, 5, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::OneofDescriptorProto, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::OneofDescriptorProto, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::OneofDescriptorProto, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::OneofDescriptorProto, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::OneofDescriptorProto, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::OneofDescriptorProto, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::OneofDescriptorProto, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::OneofDescriptorProto, options_), 0, 1, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto_EnumReservedRange, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto_EnumReservedRange, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto_EnumReservedRange, start_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto_EnumReservedRange, end_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange, start_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange, end_), 0, 1, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto, value_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto, options_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto, reserved_range_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumDescriptorProto, reserved_name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto, value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto, reserved_range_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto, reserved_name_), 0, ~0u, 1, ~0u, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueDescriptorProto, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueDescriptorProto, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueDescriptorProto, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueDescriptorProto, number_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueDescriptorProto, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto, number_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto, options_), 0, 2, 1, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceDescriptorProto, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceDescriptorProto, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceDescriptorProto, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceDescriptorProto, method_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceDescriptorProto, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto, method_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto, options_), 0, ~0u, 1, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodDescriptorProto, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodDescriptorProto, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodDescriptorProto, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodDescriptorProto, input_type_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodDescriptorProto, output_type_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodDescriptorProto, options_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodDescriptorProto, client_streaming_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodDescriptorProto, server_streaming_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto, input_type_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto, output_type_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto, options_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto, client_streaming_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto, server_streaming_), 0, 1, 2, 3, 4, 5, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, java_package_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, java_outer_classname_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, java_multiple_files_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, java_generate_equals_and_hash_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, java_string_check_utf8_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, optimize_for_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, go_package_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, cc_generic_services_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, java_generic_services_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, py_generic_services_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, php_generic_services_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, deprecated_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, cc_enable_arenas_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, objc_class_prefix_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, csharp_namespace_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, swift_prefix_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, php_class_prefix_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, php_namespace_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, php_metadata_namespace_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, ruby_package_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FileOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, java_package_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, java_outer_classname_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, java_multiple_files_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, java_generate_equals_and_hash_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, java_string_check_utf8_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, optimize_for_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, go_package_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, cc_generic_services_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, java_generic_services_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, py_generic_services_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, php_generic_services_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, deprecated_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, cc_enable_arenas_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, objc_class_prefix_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, csharp_namespace_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, swift_prefix_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, php_class_prefix_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, php_namespace_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, php_metadata_namespace_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, ruby_package_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FileOptions, uninterpreted_option_), 0, 1, 10, @@ -837,33 +799,33 @@ const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fdescriptor_2epr 8, 9, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MessageOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MessageOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MessageOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MessageOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MessageOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MessageOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MessageOptions, message_set_wire_format_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MessageOptions, no_standard_descriptor_accessor_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MessageOptions, deprecated_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MessageOptions, map_entry_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MessageOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MessageOptions, message_set_wire_format_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MessageOptions, no_standard_descriptor_accessor_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MessageOptions, deprecated_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MessageOptions, map_entry_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MessageOptions, uninterpreted_option_), 0, 1, 2, 3, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, ctype_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, packed_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, jstype_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, lazy_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, deprecated_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, weak_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, ctype_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, packed_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, jstype_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, lazy_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, deprecated_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, weak_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldOptions, uninterpreted_option_), 0, 1, 5, @@ -871,74 +833,74 @@ const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fdescriptor_2epr 3, 4, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::OneofOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::OneofOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::OneofOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::OneofOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::OneofOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::OneofOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::OneofOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::OneofOptions, uninterpreted_option_), ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumOptions, allow_alias_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumOptions, deprecated_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumOptions, allow_alias_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumOptions, deprecated_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumOptions, uninterpreted_option_), 0, 1, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueOptions, deprecated_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::EnumValueOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueOptions, deprecated_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::EnumValueOptions, uninterpreted_option_), 0, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceOptions, deprecated_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ServiceOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceOptions, deprecated_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ServiceOptions, uninterpreted_option_), 0, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodOptions, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodOptions, _internal_metadata_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodOptions, _extensions_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodOptions, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodOptions, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodOptions, _extensions_), ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodOptions, deprecated_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodOptions, idempotency_level_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::MethodOptions, uninterpreted_option_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodOptions, deprecated_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodOptions, idempotency_level_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::MethodOptions, uninterpreted_option_), 0, 1, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption_NamePart, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption_NamePart, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption_NamePart, name_part_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption_NamePart, is_extension_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart, name_part_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart, is_extension_), 0, 1, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, name_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, identifier_value_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, positive_int_value_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, negative_int_value_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, double_value_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, string_value_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::UninterpretedOption, aggregate_value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, identifier_value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, positive_int_value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, negative_int_value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, double_value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, string_value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::UninterpretedOption, aggregate_value_), ~0u, 0, 3, @@ -946,117 +908,110 @@ const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fdescriptor_2epr 5, 1, 2, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo_Location, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo_Location, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo_Location, path_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo_Location, span_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo_Location, leading_comments_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo_Location, trailing_comments_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo_Location, leading_detached_comments_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location, path_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location, span_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location, leading_comments_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location, trailing_comments_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location, leading_detached_comments_), ~0u, ~0u, 0, 1, ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceCodeInfo, location_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceCodeInfo, location_), ~0u, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo_Annotation, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo_Annotation, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo_Annotation, path_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo_Annotation, source_file_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo_Annotation, begin_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo_Annotation, end_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation, path_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation, source_file_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation, begin_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation, end_), ~0u, 0, 1, 2, - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::GeneratedCodeInfo, annotation_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo, annotation_), ~0u, }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, 6, sizeof(::google::protobuf::FileDescriptorSet)}, - { 7, 24, sizeof(::google::protobuf::FileDescriptorProto)}, - { 36, 44, sizeof(::google::protobuf::DescriptorProto_ExtensionRange)}, - { 47, 54, sizeof(::google::protobuf::DescriptorProto_ReservedRange)}, - { 56, 71, sizeof(::google::protobuf::DescriptorProto)}, - { 81, 87, sizeof(::google::protobuf::ExtensionRangeOptions)}, - { 88, 103, sizeof(::google::protobuf::FieldDescriptorProto)}, - { 113, 120, sizeof(::google::protobuf::OneofDescriptorProto)}, - { 122, 129, sizeof(::google::protobuf::EnumDescriptorProto_EnumReservedRange)}, - { 131, 141, sizeof(::google::protobuf::EnumDescriptorProto)}, - { 146, 154, sizeof(::google::protobuf::EnumValueDescriptorProto)}, - { 157, 165, sizeof(::google::protobuf::ServiceDescriptorProto)}, - { 168, 179, sizeof(::google::protobuf::MethodDescriptorProto)}, - { 185, 211, sizeof(::google::protobuf::FileOptions)}, - { 232, 242, sizeof(::google::protobuf::MessageOptions)}, - { 247, 259, sizeof(::google::protobuf::FieldOptions)}, - { 266, 272, sizeof(::google::protobuf::OneofOptions)}, - { 273, 281, sizeof(::google::protobuf::EnumOptions)}, - { 284, 291, sizeof(::google::protobuf::EnumValueOptions)}, - { 293, 300, sizeof(::google::protobuf::ServiceOptions)}, - { 302, 310, sizeof(::google::protobuf::MethodOptions)}, - { 313, 320, sizeof(::google::protobuf::UninterpretedOption_NamePart)}, - { 322, 334, sizeof(::google::protobuf::UninterpretedOption)}, - { 341, 351, sizeof(::google::protobuf::SourceCodeInfo_Location)}, - { 356, 362, sizeof(::google::protobuf::SourceCodeInfo)}, - { 363, 372, sizeof(::google::protobuf::GeneratedCodeInfo_Annotation)}, - { 376, 382, sizeof(::google::protobuf::GeneratedCodeInfo)}, -}; - -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_FileDescriptorSet_default_instance_), - reinterpret_cast(&::google::protobuf::_FileDescriptorProto_default_instance_), - reinterpret_cast(&::google::protobuf::_DescriptorProto_ExtensionRange_default_instance_), - reinterpret_cast(&::google::protobuf::_DescriptorProto_ReservedRange_default_instance_), - reinterpret_cast(&::google::protobuf::_DescriptorProto_default_instance_), - reinterpret_cast(&::google::protobuf::_ExtensionRangeOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_FieldDescriptorProto_default_instance_), - reinterpret_cast(&::google::protobuf::_OneofDescriptorProto_default_instance_), - reinterpret_cast(&::google::protobuf::_EnumDescriptorProto_EnumReservedRange_default_instance_), - reinterpret_cast(&::google::protobuf::_EnumDescriptorProto_default_instance_), - reinterpret_cast(&::google::protobuf::_EnumValueDescriptorProto_default_instance_), - reinterpret_cast(&::google::protobuf::_ServiceDescriptorProto_default_instance_), - reinterpret_cast(&::google::protobuf::_MethodDescriptorProto_default_instance_), - reinterpret_cast(&::google::protobuf::_FileOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_MessageOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_FieldOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_OneofOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_EnumOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_EnumValueOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_ServiceOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_MethodOptions_default_instance_), - reinterpret_cast(&::google::protobuf::_UninterpretedOption_NamePart_default_instance_), - reinterpret_cast(&::google::protobuf::_UninterpretedOption_default_instance_), - reinterpret_cast(&::google::protobuf::_SourceCodeInfo_Location_default_instance_), - reinterpret_cast(&::google::protobuf::_SourceCodeInfo_default_instance_), - reinterpret_cast(&::google::protobuf::_GeneratedCodeInfo_Annotation_default_instance_), - reinterpret_cast(&::google::protobuf::_GeneratedCodeInfo_default_instance_), +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, 6, sizeof(PROTOBUF_NAMESPACE_ID::FileDescriptorSet)}, + { 7, 24, sizeof(PROTOBUF_NAMESPACE_ID::FileDescriptorProto)}, + { 36, 44, sizeof(PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange)}, + { 47, 54, sizeof(PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange)}, + { 56, 71, sizeof(PROTOBUF_NAMESPACE_ID::DescriptorProto)}, + { 81, 87, sizeof(PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions)}, + { 88, 103, sizeof(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto)}, + { 113, 120, sizeof(PROTOBUF_NAMESPACE_ID::OneofDescriptorProto)}, + { 122, 129, sizeof(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange)}, + { 131, 141, sizeof(PROTOBUF_NAMESPACE_ID::EnumDescriptorProto)}, + { 146, 154, sizeof(PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto)}, + { 157, 165, sizeof(PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto)}, + { 168, 179, sizeof(PROTOBUF_NAMESPACE_ID::MethodDescriptorProto)}, + { 185, 211, sizeof(PROTOBUF_NAMESPACE_ID::FileOptions)}, + { 232, 242, sizeof(PROTOBUF_NAMESPACE_ID::MessageOptions)}, + { 247, 259, sizeof(PROTOBUF_NAMESPACE_ID::FieldOptions)}, + { 266, 272, sizeof(PROTOBUF_NAMESPACE_ID::OneofOptions)}, + { 273, 281, sizeof(PROTOBUF_NAMESPACE_ID::EnumOptions)}, + { 284, 291, sizeof(PROTOBUF_NAMESPACE_ID::EnumValueOptions)}, + { 293, 300, sizeof(PROTOBUF_NAMESPACE_ID::ServiceOptions)}, + { 302, 310, sizeof(PROTOBUF_NAMESPACE_ID::MethodOptions)}, + { 313, 320, sizeof(PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart)}, + { 322, 334, sizeof(PROTOBUF_NAMESPACE_ID::UninterpretedOption)}, + { 341, 351, sizeof(PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location)}, + { 356, 362, sizeof(PROTOBUF_NAMESPACE_ID::SourceCodeInfo)}, + { 363, 372, sizeof(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation)}, + { 376, 382, sizeof(PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo)}, }; -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2fdescriptor_2eproto, "google/protobuf/descriptor.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2fdescriptor_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto, 27, file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto, file_level_service_descriptors_google_2fprotobuf_2fdescriptor_2eproto, +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_FileDescriptorSet_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_FileDescriptorProto_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_DescriptorProto_ExtensionRange_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_DescriptorProto_ReservedRange_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_DescriptorProto_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_ExtensionRangeOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_FieldDescriptorProto_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_OneofDescriptorProto_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_EnumDescriptorProto_EnumReservedRange_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_EnumDescriptorProto_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_EnumValueDescriptorProto_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_ServiceDescriptorProto_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_MethodDescriptorProto_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_FileOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_MessageOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_FieldOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_OneofOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_EnumOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_EnumValueOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_ServiceOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_MethodOptions_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_UninterpretedOption_NamePart_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_UninterpretedOption_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_SourceCodeInfo_Location_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_SourceCodeInfo_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_GeneratedCodeInfo_Annotation_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_GeneratedCodeInfo_default_instance_), }; -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fdescriptor_2eproto = { - false, InitDefaults_google_2fprotobuf_2fdescriptor_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n google/protobuf/descriptor.proto\022\017goog" "le.protobuf\"G\n\021FileDescriptorSet\0222\n\004file" "\030\001 \003(\0132$.google.protobuf.FileDescriptorP" @@ -1208,23 +1163,52 @@ static ::google::protobuf::Message const * const file_default_instances[] = { "Z>github.com/golang/protobuf/protoc-gen-" "go/descriptor;descriptor\370\001\001\242\002\003GPB\252\002\032Goog" "le.Protobuf.Reflection" -, - "google/protobuf/descriptor.proto", &assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto, 6022, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_deps[1] = { +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_sccs[27] = { + &scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base, + &scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fdescriptor_2eproto = { + &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto, "google/protobuf/descriptor.proto", 6022, + &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_once, descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_sccs, descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_deps, 27, 0, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2fdescriptor_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto, 27, file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto, file_level_service_descriptors_google_2fprotobuf_2fdescriptor_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2fdescriptor_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, deps, 0); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2fdescriptor_2eproto = []() { AddDescriptors_google_2fprotobuf_2fdescriptor_2eproto(); return true; }(); -namespace google { -namespace protobuf { -const ::google::protobuf::EnumDescriptor* FieldDescriptorProto_Type_descriptor() { - ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); +static bool dynamic_init_dummy_google_2fprotobuf_2fdescriptor_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto), true); +PROTOBUF_NAMESPACE_OPEN +const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldDescriptorProto_Type_descriptor() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); return file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[0]; } bool FieldDescriptorProto_Type_IsValid(int value) { @@ -1253,31 +1237,31 @@ bool FieldDescriptorProto_Type_IsValid(int value) { } } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_DOUBLE; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_FLOAT; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_INT64; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_UINT64; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_INT32; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_FIXED64; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_FIXED32; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_BOOL; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_STRING; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_GROUP; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_MESSAGE; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_BYTES; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_UINT32; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_ENUM; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_SFIXED32; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_SFIXED64; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_SINT32; -const FieldDescriptorProto_Type FieldDescriptorProto::TYPE_SINT64; -const FieldDescriptorProto_Type FieldDescriptorProto::Type_MIN; -const FieldDescriptorProto_Type FieldDescriptorProto::Type_MAX; -const int FieldDescriptorProto::Type_ARRAYSIZE; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 -const ::google::protobuf::EnumDescriptor* FieldDescriptorProto_Label_descriptor() { - ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); +#if (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_DOUBLE; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_FLOAT; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_INT64; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_UINT64; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_INT32; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_FIXED64; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_FIXED32; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_BOOL; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_STRING; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_GROUP; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_MESSAGE; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_BYTES; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_UINT32; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_ENUM; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_SFIXED32; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_SFIXED64; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_SINT32; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::TYPE_SINT64; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::Type_MIN; +constexpr FieldDescriptorProto_Type FieldDescriptorProto::Type_MAX; +constexpr int FieldDescriptorProto::Type_ARRAYSIZE; +#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldDescriptorProto_Label_descriptor() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); return file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[1]; } bool FieldDescriptorProto_Label_IsValid(int value) { @@ -1291,16 +1275,16 @@ bool FieldDescriptorProto_Label_IsValid(int value) { } } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const FieldDescriptorProto_Label FieldDescriptorProto::LABEL_OPTIONAL; -const FieldDescriptorProto_Label FieldDescriptorProto::LABEL_REQUIRED; -const FieldDescriptorProto_Label FieldDescriptorProto::LABEL_REPEATED; -const FieldDescriptorProto_Label FieldDescriptorProto::Label_MIN; -const FieldDescriptorProto_Label FieldDescriptorProto::Label_MAX; -const int FieldDescriptorProto::Label_ARRAYSIZE; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 -const ::google::protobuf::EnumDescriptor* FileOptions_OptimizeMode_descriptor() { - ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); +#if (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +constexpr FieldDescriptorProto_Label FieldDescriptorProto::LABEL_OPTIONAL; +constexpr FieldDescriptorProto_Label FieldDescriptorProto::LABEL_REQUIRED; +constexpr FieldDescriptorProto_Label FieldDescriptorProto::LABEL_REPEATED; +constexpr FieldDescriptorProto_Label FieldDescriptorProto::Label_MIN; +constexpr FieldDescriptorProto_Label FieldDescriptorProto::Label_MAX; +constexpr int FieldDescriptorProto::Label_ARRAYSIZE; +#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FileOptions_OptimizeMode_descriptor() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); return file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[2]; } bool FileOptions_OptimizeMode_IsValid(int value) { @@ -1314,16 +1298,16 @@ bool FileOptions_OptimizeMode_IsValid(int value) { } } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const FileOptions_OptimizeMode FileOptions::SPEED; -const FileOptions_OptimizeMode FileOptions::CODE_SIZE; -const FileOptions_OptimizeMode FileOptions::LITE_RUNTIME; -const FileOptions_OptimizeMode FileOptions::OptimizeMode_MIN; -const FileOptions_OptimizeMode FileOptions::OptimizeMode_MAX; -const int FileOptions::OptimizeMode_ARRAYSIZE; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 -const ::google::protobuf::EnumDescriptor* FieldOptions_CType_descriptor() { - ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); +#if (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +constexpr FileOptions_OptimizeMode FileOptions::SPEED; +constexpr FileOptions_OptimizeMode FileOptions::CODE_SIZE; +constexpr FileOptions_OptimizeMode FileOptions::LITE_RUNTIME; +constexpr FileOptions_OptimizeMode FileOptions::OptimizeMode_MIN; +constexpr FileOptions_OptimizeMode FileOptions::OptimizeMode_MAX; +constexpr int FileOptions::OptimizeMode_ARRAYSIZE; +#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldOptions_CType_descriptor() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); return file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[3]; } bool FieldOptions_CType_IsValid(int value) { @@ -1337,16 +1321,16 @@ bool FieldOptions_CType_IsValid(int value) { } } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const FieldOptions_CType FieldOptions::STRING; -const FieldOptions_CType FieldOptions::CORD; -const FieldOptions_CType FieldOptions::STRING_PIECE; -const FieldOptions_CType FieldOptions::CType_MIN; -const FieldOptions_CType FieldOptions::CType_MAX; -const int FieldOptions::CType_ARRAYSIZE; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 -const ::google::protobuf::EnumDescriptor* FieldOptions_JSType_descriptor() { - ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); +#if (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +constexpr FieldOptions_CType FieldOptions::STRING; +constexpr FieldOptions_CType FieldOptions::CORD; +constexpr FieldOptions_CType FieldOptions::STRING_PIECE; +constexpr FieldOptions_CType FieldOptions::CType_MIN; +constexpr FieldOptions_CType FieldOptions::CType_MAX; +constexpr int FieldOptions::CType_ARRAYSIZE; +#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldOptions_JSType_descriptor() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); return file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[4]; } bool FieldOptions_JSType_IsValid(int value) { @@ -1360,16 +1344,16 @@ bool FieldOptions_JSType_IsValid(int value) { } } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const FieldOptions_JSType FieldOptions::JS_NORMAL; -const FieldOptions_JSType FieldOptions::JS_STRING; -const FieldOptions_JSType FieldOptions::JS_NUMBER; -const FieldOptions_JSType FieldOptions::JSType_MIN; -const FieldOptions_JSType FieldOptions::JSType_MAX; -const int FieldOptions::JSType_ARRAYSIZE; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 -const ::google::protobuf::EnumDescriptor* MethodOptions_IdempotencyLevel_descriptor() { - ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); +#if (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +constexpr FieldOptions_JSType FieldOptions::JS_NORMAL; +constexpr FieldOptions_JSType FieldOptions::JS_STRING; +constexpr FieldOptions_JSType FieldOptions::JS_NUMBER; +constexpr FieldOptions_JSType FieldOptions::JSType_MIN; +constexpr FieldOptions_JSType FieldOptions::JSType_MAX; +constexpr int FieldOptions::JSType_ARRAYSIZE; +#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* MethodOptions_IdempotencyLevel_descriptor() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); return file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[5]; } bool MethodOptions_IdempotencyLevel_IsValid(int value) { @@ -1383,34 +1367,31 @@ bool MethodOptions_IdempotencyLevel_IsValid(int value) { } } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const MethodOptions_IdempotencyLevel MethodOptions::IDEMPOTENCY_UNKNOWN; -const MethodOptions_IdempotencyLevel MethodOptions::NO_SIDE_EFFECTS; -const MethodOptions_IdempotencyLevel MethodOptions::IDEMPOTENT; -const MethodOptions_IdempotencyLevel MethodOptions::IdempotencyLevel_MIN; -const MethodOptions_IdempotencyLevel MethodOptions::IdempotencyLevel_MAX; -const int MethodOptions::IdempotencyLevel_ARRAYSIZE; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 +#if (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) +constexpr MethodOptions_IdempotencyLevel MethodOptions::IDEMPOTENCY_UNKNOWN; +constexpr MethodOptions_IdempotencyLevel MethodOptions::NO_SIDE_EFFECTS; +constexpr MethodOptions_IdempotencyLevel MethodOptions::IDEMPOTENT; +constexpr MethodOptions_IdempotencyLevel MethodOptions::IdempotencyLevel_MIN; +constexpr MethodOptions_IdempotencyLevel MethodOptions::IdempotencyLevel_MAX; +constexpr int MethodOptions::IdempotencyLevel_ARRAYSIZE; +#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) // =================================================================== void FileDescriptorSet::InitAsDefaultInstance() { } -class FileDescriptorSet::HasBitSetters { +class FileDescriptorSet::_Internal { public: + using HasBits = decltype(std::declval()._has_bits_); }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int FileDescriptorSet::kFileFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - FileDescriptorSet::FileDescriptorSet() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.FileDescriptorSet) } -FileDescriptorSet::FileDescriptorSet(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +FileDescriptorSet::FileDescriptorSet(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), file_(arena) { SharedCtor(); @@ -1418,8 +1399,8 @@ FileDescriptorSet::FileDescriptorSet(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.FileDescriptorSet) } FileDescriptorSet::FileDescriptorSet(const FileDescriptorSet& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), file_(from.file_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -1427,8 +1408,7 @@ FileDescriptorSet::FileDescriptorSet(const FileDescriptorSet& from) } void FileDescriptorSet::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto.base); } FileDescriptorSet::~FileDescriptorSet() { @@ -1437,27 +1417,27 @@ FileDescriptorSet::~FileDescriptorSet() { } void FileDescriptorSet::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void FileDescriptorSet::ArenaDtor(void* object) { FileDescriptorSet* _this = reinterpret_cast< FileDescriptorSet* >(object); (void)_this; } -void FileDescriptorSet::RegisterArenaDtor(::google::protobuf::Arena*) { +void FileDescriptorSet::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void FileDescriptorSet::SetCachedSize(int size) const { _cached_size_.Set(size); } const FileDescriptorSet& FileDescriptorSet::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void FileDescriptorSet::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FileDescriptorSet) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1467,74 +1447,60 @@ void FileDescriptorSet::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* FileDescriptorSet::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* FileDescriptorSet::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated .google.protobuf.FileDescriptorProto file = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::FileDescriptorProto::_InternalParse; - object = msg->add_file(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 10 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_file(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 10); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FileDescriptorSet::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.FileDescriptorSet) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated .google.protobuf.FileDescriptorProto file = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_file())); } else { goto handle_unusual; @@ -1547,7 +1513,7 @@ bool FileDescriptorSet::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -1563,46 +1529,23 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void FileDescriptorSet::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.FileDescriptorSet) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .google.protobuf.FileDescriptorProto file = 1; - for (unsigned int i = 0, - n = static_cast(this->file_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 1, - this->file(static_cast(i)), - output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.FileDescriptorSet) -} - -::google::protobuf::uint8* FileDescriptorSet::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* FileDescriptorSet::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.FileDescriptorSet) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated .google.protobuf.FileDescriptorProto file = 1; - for (unsigned int i = 0, - n = static_cast(this->file_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 1, this->file(static_cast(i)), deterministic, target); + for (auto it = this->file().pointer_begin(), + end = this->file().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(1, **it, target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.FileDescriptorSet) return target; @@ -1612,12 +1555,7 @@ size_t FileDescriptorSet::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.FileDescriptorSet) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1627,25 +1565,29 @@ size_t FileDescriptorSet::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->file(static_cast(i))); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void FileDescriptorSet::MergeFrom(const ::google::protobuf::Message& from) { +void FileDescriptorSet::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.FileDescriptorSet) GOOGLE_DCHECK_NE(&from, this); const FileDescriptorSet* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.FileDescriptorSet) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.FileDescriptorSet) MergeFrom(*source); @@ -1656,13 +1598,13 @@ void FileDescriptorSet::MergeFrom(const FileDescriptorSet& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.FileDescriptorSet) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; file_.MergeFrom(from.file_); } -void FileDescriptorSet::CopyFrom(const ::google::protobuf::Message& from) { +void FileDescriptorSet::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.FileDescriptorSet) if (&from == this) return; Clear(); @@ -1677,29 +1619,10 @@ void FileDescriptorSet::CopyFrom(const FileDescriptorSet& from) { } bool FileDescriptorSet::IsInitialized() const { - if (!::google::protobuf::internal::AllAreInitialized(this->file())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->file())) return false; return true; } -void FileDescriptorSet::Swap(FileDescriptorSet* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - FileDescriptorSet* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void FileDescriptorSet::UnsafeArenaSwap(FileDescriptorSet* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void FileDescriptorSet::InternalSwap(FileDescriptorSet* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -1707,52 +1630,52 @@ void FileDescriptorSet::InternalSwap(FileDescriptorSet* other) { CastToBase(&file_)->InternalSwap(CastToBase(&other->file_)); } -::google::protobuf::Metadata FileDescriptorSet::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata FileDescriptorSet::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void FileDescriptorProto::InitAsDefaultInstance() { - ::google::protobuf::_FileDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::FileOptions*>( - ::google::protobuf::FileOptions::internal_default_instance()); - ::google::protobuf::_FileDescriptorProto_default_instance_._instance.get_mutable()->source_code_info_ = const_cast< ::google::protobuf::SourceCodeInfo*>( - ::google::protobuf::SourceCodeInfo::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_FileDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::FileOptions*>( + PROTOBUF_NAMESPACE_ID::FileOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_FileDescriptorProto_default_instance_._instance.get_mutable()->source_code_info_ = const_cast< PROTOBUF_NAMESPACE_ID::SourceCodeInfo*>( + PROTOBUF_NAMESPACE_ID::SourceCodeInfo::internal_default_instance()); } -class FileDescriptorProto::HasBitSetters { +class FileDescriptorProto::_Internal { public: - static void set_has_name(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_package(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_package(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static const ::google::protobuf::FileOptions& options(const FileDescriptorProto* msg); - static void set_has_options(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static const PROTOBUF_NAMESPACE_ID::FileOptions& options(const FileDescriptorProto* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static const ::google::protobuf::SourceCodeInfo& source_code_info(const FileDescriptorProto* msg); - static void set_has_source_code_info(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static const PROTOBUF_NAMESPACE_ID::SourceCodeInfo& source_code_info(const FileDescriptorProto* msg); + static void set_has_source_code_info(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static void set_has_syntax(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_syntax(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } }; -const ::google::protobuf::FileOptions& -FileDescriptorProto::HasBitSetters::options(const FileDescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::FileOptions& +FileDescriptorProto::_Internal::options(const FileDescriptorProto* msg) { return *msg->options_; } -const ::google::protobuf::SourceCodeInfo& -FileDescriptorProto::HasBitSetters::source_code_info(const FileDescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::SourceCodeInfo& +FileDescriptorProto::_Internal::source_code_info(const FileDescriptorProto* msg) { return *msg->source_code_info_; } void FileDescriptorProto::unsafe_arena_set_allocated_options( - ::google::protobuf::FileOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::FileOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -1764,8 +1687,8 @@ void FileDescriptorProto::unsafe_arena_set_allocated_options( // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileDescriptorProto.options) } void FileDescriptorProto::unsafe_arena_set_allocated_source_code_info( - ::google::protobuf::SourceCodeInfo* source_code_info) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::SourceCodeInfo* source_code_info) { + if (GetArenaNoVirtual() == nullptr) { delete source_code_info_; } source_code_info_ = source_code_info; @@ -1776,28 +1699,13 @@ void FileDescriptorProto::unsafe_arena_set_allocated_source_code_info( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileDescriptorProto.source_code_info) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int FileDescriptorProto::kNameFieldNumber; -const int FileDescriptorProto::kPackageFieldNumber; -const int FileDescriptorProto::kDependencyFieldNumber; -const int FileDescriptorProto::kPublicDependencyFieldNumber; -const int FileDescriptorProto::kWeakDependencyFieldNumber; -const int FileDescriptorProto::kMessageTypeFieldNumber; -const int FileDescriptorProto::kEnumTypeFieldNumber; -const int FileDescriptorProto::kServiceFieldNumber; -const int FileDescriptorProto::kExtensionFieldNumber; -const int FileDescriptorProto::kOptionsFieldNumber; -const int FileDescriptorProto::kSourceCodeInfoFieldNumber; -const int FileDescriptorProto::kSyntaxFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - FileDescriptorProto::FileDescriptorProto() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.FileDescriptorProto) } -FileDescriptorProto::FileDescriptorProto(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +FileDescriptorProto::FileDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), dependency_(arena), message_type_(arena), @@ -1811,8 +1719,8 @@ FileDescriptorProto::FileDescriptorProto(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.FileDescriptorProto) } FileDescriptorProto::FileDescriptorProto(const FileDescriptorProto& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), dependency_(from.dependency_), message_type_(from.message_type_), @@ -1822,40 +1730,39 @@ FileDescriptorProto::FileDescriptorProto(const FileDescriptorProto& from) public_dependency_(from.public_dependency_), weak_dependency_(from.weak_dependency_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name(), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name(), GetArenaNoVirtual()); } - package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + package_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_package()) { - package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.package(), + package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.package(), GetArenaNoVirtual()); } - syntax_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + syntax_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_syntax()) { - syntax_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.syntax(), + syntax_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.syntax(), GetArenaNoVirtual()); } if (from.has_options()) { - options_ = new ::google::protobuf::FileOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::FileOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } if (from.has_source_code_info()) { - source_code_info_ = new ::google::protobuf::SourceCodeInfo(*from.source_code_info_); + source_code_info_ = new PROTOBUF_NAMESPACE_ID::SourceCodeInfo(*from.source_code_info_); } else { - source_code_info_ = NULL; + source_code_info_ = nullptr; } // @@protoc_insertion_point(copy_constructor:google.protobuf.FileDescriptorProto) } void FileDescriptorProto::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - syntax_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + package_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + syntax_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&options_, 0, static_cast( reinterpret_cast(&source_code_info_) - reinterpret_cast(&options_)) + sizeof(source_code_info_)); @@ -1867,10 +1774,10 @@ FileDescriptorProto::~FileDescriptorProto() { } void FileDescriptorProto::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - package_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - syntax_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + package_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + syntax_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete options_; if (this != internal_default_instance()) delete source_code_info_; } @@ -1879,20 +1786,20 @@ void FileDescriptorProto::ArenaDtor(void* object) { FileDescriptorProto* _this = reinterpret_cast< FileDescriptorProto* >(object); (void)_this; } -void FileDescriptorProto::RegisterArenaDtor(::google::protobuf::Arena*) { +void FileDescriptorProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void FileDescriptorProto::SetCachedSize(int size) const { _cached_size_.Set(size); } const FileDescriptorProto& FileDescriptorProto::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void FileDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FileDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1915,11 +1822,11 @@ void FileDescriptorProto::Clear() { syntax_.ClearNonDefaultToEmpty(); } if (cached_has_bits & 0x00000008u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } if (cached_has_bits & 0x00000010u) { - GOOGLE_DCHECK(source_code_info_ != NULL); + GOOGLE_DCHECK(source_code_info_ != nullptr); source_code_info_->Clear(); } } @@ -1928,265 +1835,179 @@ void FileDescriptorProto::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* FileDescriptorProto::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* FileDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileDescriptorProto.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.FileDescriptorProto.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string package = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileDescriptorProto.package"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_package(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_package(), ptr, ctx, "google.protobuf.FileDescriptorProto.package"); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated string dependency = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileDescriptorProto.dependency"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->add_dependency(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 26 && (ptr += 1)); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr -= 1; + do { + ptr += 1; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_dependency(), ptr, ctx, "google.protobuf.FileDescriptorProto.dependency"); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 26); + } else goto handle_unusual; + continue; // repeated .google.protobuf.DescriptorProto message_type = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::DescriptorProto::_InternalParse; - object = msg->add_message_type(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 34 && (ptr += 1)); - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_message_type(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 34); + } else goto handle_unusual; + continue; // repeated .google.protobuf.EnumDescriptorProto enum_type = 5; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::EnumDescriptorProto::_InternalParse; - object = msg->add_enum_type(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 42 && (ptr += 1)); - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 42)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_enum_type(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 42); + } else goto handle_unusual; + continue; // repeated .google.protobuf.ServiceDescriptorProto service = 6; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 50) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::ServiceDescriptorProto::_InternalParse; - object = msg->add_service(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 50 && (ptr += 1)); - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 50)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_service(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 50); + } else goto handle_unusual; + continue; // repeated .google.protobuf.FieldDescriptorProto extension = 7; - case 7: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::FieldDescriptorProto::_InternalParse; - object = msg->add_extension(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 58 && (ptr += 1)); - break; - } + case 7: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_extension(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 58); + } else goto handle_unusual; + continue; // optional .google.protobuf.FileOptions options = 8; - case 8: { - if (static_cast<::google::protobuf::uint8>(tag) != 66) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::FileOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 8: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 66)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.SourceCodeInfo source_code_info = 9; - case 9: { - if (static_cast<::google::protobuf::uint8>(tag) != 74) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::SourceCodeInfo::_InternalParse; - object = msg->mutable_source_code_info(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 9: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 74)) { + ptr = ctx->ParseMessage(mutable_source_code_info(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated int32 public_dependency = 10; - case 10: { - if (static_cast<::google::protobuf::uint8>(tag) == 80) { + case 10: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 80)) { + ptr -= 1; do { - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->add_public_dependency(value); - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 80 && (ptr += 1)); - break; - } else if (static_cast<::google::protobuf::uint8>(tag) != 82) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::internal::PackedInt32Parser; - object = msg->mutable_public_dependency(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + ptr += 1; + add_public_dependency(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 80); + } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 82) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_public_dependency(), ptr, ctx); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated int32 weak_dependency = 11; - case 11: { - if (static_cast<::google::protobuf::uint8>(tag) == 88) { + case 11: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 88)) { + ptr -= 1; do { - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->add_weak_dependency(value); - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 88 && (ptr += 1)); - break; - } else if (static_cast<::google::protobuf::uint8>(tag) != 90) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::internal::PackedInt32Parser; - object = msg->mutable_weak_dependency(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + ptr += 1; + add_weak_dependency(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 88); + } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 90) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_weak_dependency(), ptr, ctx); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string syntax = 12; - case 12: { - if (static_cast<::google::protobuf::uint8>(tag) != 98) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileDescriptorProto.syntax"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_syntax(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 12: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 98)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_syntax(), ptr, ctx, "google.protobuf.FileDescriptorProto.syntax"); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FileDescriptorProto::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.FileDescriptorProto) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileDescriptorProto.name"); } else { goto handle_unusual; @@ -2196,12 +2017,12 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // optional string package = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_package())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->package().data(), static_cast(this->package().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileDescriptorProto.package"); } else { goto handle_unusual; @@ -2211,13 +2032,13 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // repeated string dependency = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->add_dependency())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->dependency(this->dependency_size() - 1).data(), static_cast(this->dependency(this->dependency_size() - 1).length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileDescriptorProto.dependency"); } else { goto handle_unusual; @@ -2227,8 +2048,8 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.DescriptorProto message_type = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (34 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_message_type())); } else { goto handle_unusual; @@ -2238,8 +2059,8 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.EnumDescriptorProto enum_type = 5; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (42 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (42 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_enum_type())); } else { goto handle_unusual; @@ -2249,8 +2070,8 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.ServiceDescriptorProto service = 6; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (50 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (50 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_service())); } else { goto handle_unusual; @@ -2260,8 +2081,8 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.FieldDescriptorProto extension = 7; case 7: { - if (static_cast< ::google::protobuf::uint8>(tag) == (58 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (58 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_extension())); } else { goto handle_unusual; @@ -2271,8 +2092,8 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.FileOptions options = 8; case 8: { - if (static_cast< ::google::protobuf::uint8>(tag) == (66 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (66 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -2282,8 +2103,8 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.SourceCodeInfo source_code_info = 9; case 9: { - if (static_cast< ::google::protobuf::uint8>(tag) == (74 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (74 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_source_code_info())); } else { goto handle_unusual; @@ -2293,13 +2114,13 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // repeated int32 public_dependency = 10; case 10: { - if (static_cast< ::google::protobuf::uint8>(tag) == (80 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadRepeatedPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (80 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadRepeatedPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( 1, 80u, input, this->mutable_public_dependency()))); - } else if (static_cast< ::google::protobuf::uint8>(tag) == (82 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPackedPrimitiveNoInline< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + } else if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (82 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPackedPrimitiveNoInline< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, this->mutable_public_dependency()))); } else { goto handle_unusual; @@ -2309,13 +2130,13 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // repeated int32 weak_dependency = 11; case 11: { - if (static_cast< ::google::protobuf::uint8>(tag) == (88 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadRepeatedPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (88 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadRepeatedPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( 1, 88u, input, this->mutable_weak_dependency()))); - } else if (static_cast< ::google::protobuf::uint8>(tag) == (90 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPackedPrimitiveNoInline< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + } else if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (90 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPackedPrimitiveNoInline< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, this->mutable_weak_dependency()))); } else { goto handle_unusual; @@ -2325,12 +2146,12 @@ bool FileDescriptorProto::MergePartialFromCodedStream( // optional string syntax = 12; case 12: { - if (static_cast< ::google::protobuf::uint8>(tag) == (98 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (98 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_syntax())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->syntax().data(), static_cast(this->syntax().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileDescriptorProto.syntax"); } else { goto handle_unusual; @@ -2343,7 +2164,7 @@ bool FileDescriptorProto::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -2359,228 +2180,117 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void FileDescriptorProto::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.FileDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileDescriptorProto.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // optional string package = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->package().data(), static_cast(this->package().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileDescriptorProto.package"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 2, this->package(), output); - } - - // repeated string dependency = 3; - for (int i = 0, n = this->dependency_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->dependency(i).data(), static_cast(this->dependency(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileDescriptorProto.dependency"); - ::google::protobuf::internal::WireFormatLite::WriteString( - 3, this->dependency(i), output); - } - - // repeated .google.protobuf.DescriptorProto message_type = 4; - for (unsigned int i = 0, - n = static_cast(this->message_type_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 4, - this->message_type(static_cast(i)), - output); - } - - // repeated .google.protobuf.EnumDescriptorProto enum_type = 5; - for (unsigned int i = 0, - n = static_cast(this->enum_type_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 5, - this->enum_type(static_cast(i)), - output); - } - - // repeated .google.protobuf.ServiceDescriptorProto service = 6; - for (unsigned int i = 0, - n = static_cast(this->service_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 6, - this->service(static_cast(i)), - output); - } - - // repeated .google.protobuf.FieldDescriptorProto extension = 7; - for (unsigned int i = 0, - n = static_cast(this->extension_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 7, - this->extension(static_cast(i)), - output); - } - - // optional .google.protobuf.FileOptions options = 8; - if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 8, HasBitSetters::options(this), output); - } - - // optional .google.protobuf.SourceCodeInfo source_code_info = 9; - if (cached_has_bits & 0x00000010u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 9, HasBitSetters::source_code_info(this), output); - } - - // repeated int32 public_dependency = 10; - for (int i = 0, n = this->public_dependency_size(); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteInt32( - 10, this->public_dependency(i), output); - } - - // repeated int32 weak_dependency = 11; - for (int i = 0, n = this->weak_dependency_size(); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteInt32( - 11, this->weak_dependency(i), output); - } - - // optional string syntax = 12; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->syntax().data(), static_cast(this->syntax().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileDescriptorProto.syntax"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 12, this->syntax(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.FileDescriptorProto) -} - -::google::protobuf::uint8* FileDescriptorProto::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* FileDescriptorProto::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.FileDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileDescriptorProto.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // optional string package = 2; if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->package().data(), static_cast(this->package().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileDescriptorProto.package"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 2, this->package(), target); } // repeated string dependency = 3; - for (int i = 0, n = this->dependency_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->dependency(i).data(), static_cast(this->dependency(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + for (auto it = this->dependency().pointer_begin(), + end = this->dependency().pointer_end(); it < end; ++it) { + const auto& s = **it; + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( + s.data(), static_cast(s.length()), + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileDescriptorProto.dependency"); - target = ::google::protobuf::internal::WireFormatLite:: - WriteStringToArray(3, this->dependency(i), target); + target = stream->WriteString(3, s, target); } // repeated .google.protobuf.DescriptorProto message_type = 4; - for (unsigned int i = 0, - n = static_cast(this->message_type_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 4, this->message_type(static_cast(i)), deterministic, target); + for (auto it = this->message_type().pointer_begin(), + end = this->message_type().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(4, **it, target, stream); } // repeated .google.protobuf.EnumDescriptorProto enum_type = 5; - for (unsigned int i = 0, - n = static_cast(this->enum_type_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 5, this->enum_type(static_cast(i)), deterministic, target); + for (auto it = this->enum_type().pointer_begin(), + end = this->enum_type().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(5, **it, target, stream); } // repeated .google.protobuf.ServiceDescriptorProto service = 6; - for (unsigned int i = 0, - n = static_cast(this->service_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 6, this->service(static_cast(i)), deterministic, target); + for (auto it = this->service().pointer_begin(), + end = this->service().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(6, **it, target, stream); } // repeated .google.protobuf.FieldDescriptorProto extension = 7; - for (unsigned int i = 0, - n = static_cast(this->extension_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 7, this->extension(static_cast(i)), deterministic, target); + for (auto it = this->extension().pointer_begin(), + end = this->extension().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(7, **it, target, stream); } // optional .google.protobuf.FileOptions options = 8; if (cached_has_bits & 0x00000008u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 8, HasBitSetters::options(this), deterministic, target); + 8, _Internal::options(this), target, stream); } // optional .google.protobuf.SourceCodeInfo source_code_info = 9; if (cached_has_bits & 0x00000010u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 9, HasBitSetters::source_code_info(this), deterministic, target); + 9, _Internal::source_code_info(this), target, stream); } // repeated int32 public_dependency = 10; - target = ::google::protobuf::internal::WireFormatLite:: - WriteInt32ToArray(10, this->public_dependency_, target); + for (const auto& x : this->public_dependency()) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(10, x, target); + } // repeated int32 weak_dependency = 11; - target = ::google::protobuf::internal::WireFormatLite:: - WriteInt32ToArray(11, this->weak_dependency_, target); + for (const auto& x : this->weak_dependency()) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(11, x, target); + } // optional string syntax = 12; if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->syntax().data(), static_cast(this->syntax().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileDescriptorProto.syntax"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 12, this->syntax(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.FileDescriptorProto) return target; @@ -2590,20 +2300,15 @@ size_t FileDescriptorProto::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.FileDescriptorProto) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // repeated string dependency = 3; total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->dependency_size()); + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->dependency_size()); for (int i = 0, n = this->dependency_size(); i < n; i++) { - total_size += ::google::protobuf::internal::WireFormatLite::StringSize( + total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->dependency(i)); } @@ -2613,7 +2318,7 @@ size_t FileDescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->message_type(static_cast(i))); } } @@ -2624,7 +2329,7 @@ size_t FileDescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->enum_type(static_cast(i))); } } @@ -2635,7 +2340,7 @@ size_t FileDescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->service(static_cast(i))); } } @@ -2646,26 +2351,26 @@ size_t FileDescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->extension(static_cast(i))); } } // repeated int32 public_dependency = 10; { - size_t data_size = ::google::protobuf::internal::WireFormatLite:: + size_t data_size = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: Int32Size(this->public_dependency_); total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->public_dependency_size()); + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->public_dependency_size()); total_size += data_size; } // repeated int32 weak_dependency = 11; { - size_t data_size = ::google::protobuf::internal::WireFormatLite:: + size_t data_size = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: Int32Size(this->weak_dependency_); total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->weak_dependency_size()); + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->weak_dependency_size()); total_size += data_size; } @@ -2674,53 +2379,57 @@ size_t FileDescriptorProto::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional string package = 2; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->package()); } // optional string syntax = 12; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->syntax()); } // optional .google.protobuf.FileOptions options = 8; if (cached_has_bits & 0x00000008u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } // optional .google.protobuf.SourceCodeInfo source_code_info = 9; if (cached_has_bits & 0x00000010u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *source_code_info_); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void FileDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) { +void FileDescriptorProto::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.FileDescriptorProto) GOOGLE_DCHECK_NE(&from, this); const FileDescriptorProto* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.FileDescriptorProto) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.FileDescriptorProto) MergeFrom(*source); @@ -2731,7 +2440,7 @@ void FileDescriptorProto::MergeFrom(const FileDescriptorProto& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.FileDescriptorProto) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; dependency_.MergeFrom(from.dependency_); @@ -2753,15 +2462,15 @@ void FileDescriptorProto::MergeFrom(const FileDescriptorProto& from) { set_syntax(from.syntax()); } if (cached_has_bits & 0x00000008u) { - mutable_options()->::google::protobuf::FileOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::FileOptions::MergeFrom(from.options()); } if (cached_has_bits & 0x00000010u) { - mutable_source_code_info()->::google::protobuf::SourceCodeInfo::MergeFrom(from.source_code_info()); + mutable_source_code_info()->PROTOBUF_NAMESPACE_ID::SourceCodeInfo::MergeFrom(from.source_code_info()); } } } -void FileDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) { +void FileDescriptorProto::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.FileDescriptorProto) if (&from == this) return; Clear(); @@ -2776,35 +2485,16 @@ void FileDescriptorProto::CopyFrom(const FileDescriptorProto& from) { } bool FileDescriptorProto::IsInitialized() const { - if (!::google::protobuf::internal::AllAreInitialized(this->message_type())) return false; - if (!::google::protobuf::internal::AllAreInitialized(this->enum_type())) return false; - if (!::google::protobuf::internal::AllAreInitialized(this->service())) return false; - if (!::google::protobuf::internal::AllAreInitialized(this->extension())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->message_type())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->enum_type())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->service())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->extension())) return false; if (has_options()) { if (!this->options_->IsInitialized()) return false; } return true; } -void FileDescriptorProto::Swap(FileDescriptorProto* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - FileDescriptorProto* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void FileDescriptorProto::UnsafeArenaSwap(FileDescriptorProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void FileDescriptorProto::InternalSwap(FileDescriptorProto* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -2816,49 +2506,49 @@ void FileDescriptorProto::InternalSwap(FileDescriptorProto* other) { CastToBase(&extension_)->InternalSwap(CastToBase(&other->extension_)); public_dependency_.InternalSwap(&other->public_dependency_); weak_dependency_.InternalSwap(&other->weak_dependency_); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - package_.Swap(&other->package_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + package_.Swap(&other->package_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - syntax_.Swap(&other->syntax_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + syntax_.Swap(&other->syntax_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(options_, other->options_); swap(source_code_info_, other->source_code_info_); } -::google::protobuf::Metadata FileDescriptorProto::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata FileDescriptorProto::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void DescriptorProto_ExtensionRange::InitAsDefaultInstance() { - ::google::protobuf::_DescriptorProto_ExtensionRange_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::ExtensionRangeOptions*>( - ::google::protobuf::ExtensionRangeOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_DescriptorProto_ExtensionRange_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions*>( + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions::internal_default_instance()); } -class DescriptorProto_ExtensionRange::HasBitSetters { +class DescriptorProto_ExtensionRange::_Internal { public: - static void set_has_start(DescriptorProto_ExtensionRange* msg) { - msg->_has_bits_[0] |= 0x00000002u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_start(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_end(DescriptorProto_ExtensionRange* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_end(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static const ::google::protobuf::ExtensionRangeOptions& options(const DescriptorProto_ExtensionRange* msg); - static void set_has_options(DescriptorProto_ExtensionRange* msg) { - msg->_has_bits_[0] |= 0x00000001u; + static const PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions& options(const DescriptorProto_ExtensionRange* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; -const ::google::protobuf::ExtensionRangeOptions& -DescriptorProto_ExtensionRange::HasBitSetters::options(const DescriptorProto_ExtensionRange* msg) { +const PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions& +DescriptorProto_ExtensionRange::_Internal::options(const DescriptorProto_ExtensionRange* msg) { return *msg->options_; } void DescriptorProto_ExtensionRange::unsafe_arena_set_allocated_options( - ::google::protobuf::ExtensionRangeOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -2869,33 +2559,27 @@ void DescriptorProto_ExtensionRange::unsafe_arena_set_allocated_options( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.DescriptorProto.ExtensionRange.options) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int DescriptorProto_ExtensionRange::kStartFieldNumber; -const int DescriptorProto_ExtensionRange::kEndFieldNumber; -const int DescriptorProto_ExtensionRange::kOptionsFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - DescriptorProto_ExtensionRange::DescriptorProto_ExtensionRange() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.DescriptorProto.ExtensionRange) } -DescriptorProto_ExtensionRange::DescriptorProto_ExtensionRange(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +DescriptorProto_ExtensionRange::DescriptorProto_ExtensionRange(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.DescriptorProto.ExtensionRange) } DescriptorProto_ExtensionRange::DescriptorProto_ExtensionRange(const DescriptorProto_ExtensionRange& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); if (from.has_options()) { - options_ = new ::google::protobuf::ExtensionRangeOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } ::memcpy(&start_, &from.start_, static_cast(reinterpret_cast(&end_) - @@ -2904,8 +2588,7 @@ DescriptorProto_ExtensionRange::DescriptorProto_ExtensionRange(const DescriptorP } void DescriptorProto_ExtensionRange::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base); ::memset(&options_, 0, static_cast( reinterpret_cast(&end_) - reinterpret_cast(&options_)) + sizeof(end_)); @@ -2917,7 +2600,7 @@ DescriptorProto_ExtensionRange::~DescriptorProto_ExtensionRange() { } void DescriptorProto_ExtensionRange::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); if (this != internal_default_instance()) delete options_; } @@ -2925,26 +2608,26 @@ void DescriptorProto_ExtensionRange::ArenaDtor(void* object) { DescriptorProto_ExtensionRange* _this = reinterpret_cast< DescriptorProto_ExtensionRange* >(object); (void)_this; } -void DescriptorProto_ExtensionRange::RegisterArenaDtor(::google::protobuf::Arena*) { +void DescriptorProto_ExtensionRange::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void DescriptorProto_ExtensionRange::SetCachedSize(int size) const { _cached_size_.Set(size); } const DescriptorProto_ExtensionRange& DescriptorProto_ExtensionRange::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void DescriptorProto_ExtensionRange::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.DescriptorProto.ExtensionRange) - ::google::protobuf::uint32 cached_has_bits = 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 & 0x00000001u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } if (cached_has_bits & 0x00000006u) { @@ -2957,93 +2640,75 @@ void DescriptorProto_ExtensionRange::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* DescriptorProto_ExtensionRange::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* DescriptorProto_ExtensionRange::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional int32 start = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_start(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + _Internal::set_has_start(&has_bits); + start_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 end = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_end(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_end(&has_bits); + end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.ExtensionRangeOptions options = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::ExtensionRangeOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool DescriptorProto_ExtensionRange::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.DescriptorProto.ExtensionRange) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional int32 start = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_start(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { + _Internal::set_has_start(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &start_))); } else { goto handle_unusual; @@ -3053,10 +2718,10 @@ bool DescriptorProto_ExtensionRange::MergePartialFromCodedStream( // optional int32 end = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_end(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_end(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &end_))); } else { goto handle_unusual; @@ -3066,8 +2731,8 @@ bool DescriptorProto_ExtensionRange::MergePartialFromCodedStream( // optional .google.protobuf.ExtensionRangeOptions options = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -3080,7 +2745,7 @@ bool DescriptorProto_ExtensionRange::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -3096,79 +2761,46 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void DescriptorProto_ExtensionRange::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.DescriptorProto.ExtensionRange) - ::google::protobuf::uint32 cached_has_bits = 0; +::PROTOBUF_NAMESPACE_ID::uint8* DescriptorProto_ExtensionRange::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { + // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.DescriptorProto.ExtensionRange) + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional int32 start = 1; if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(1, this->start(), output); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(1, this->start(), target); } // optional int32 end = 2; if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->end(), output); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(2, this->end(), target); } // optional .google.protobuf.ExtensionRangeOptions options = 3; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 3, HasBitSetters::options(this), output); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray( + 3, _Internal::options(this), target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } - // @@protoc_insertion_point(serialize_end:google.protobuf.DescriptorProto.ExtensionRange) + // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.DescriptorProto.ExtensionRange) + return target; } -::google::protobuf::uint8* DescriptorProto_ExtensionRange::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused - // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.DescriptorProto.ExtensionRange) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; +size_t DescriptorProto_ExtensionRange::ByteSizeLong() const { +// @@protoc_insertion_point(message_byte_size_start:google.protobuf.DescriptorProto.ExtensionRange) + size_t total_size = 0; - cached_has_bits = _has_bits_[0]; - // optional int32 start = 1; - if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(1, this->start(), target); - } - - // optional int32 end = 2; - if (cached_has_bits & 0x00000004u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->end(), target); - } - - // optional .google.protobuf.ExtensionRangeOptions options = 3; - if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 3, HasBitSetters::options(this), deterministic, target); - } - - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); - } - // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.DescriptorProto.ExtensionRange) - return target; -} - -size_t DescriptorProto_ExtensionRange::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:google.protobuf.DescriptorProto.ExtensionRange) - size_t total_size = 0; - - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -3177,39 +2809,43 @@ size_t DescriptorProto_ExtensionRange::ByteSizeLong() const { // optional .google.protobuf.ExtensionRangeOptions options = 3; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } // optional int32 start = 1; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->start()); } // optional int32 end = 2; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->end()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void DescriptorProto_ExtensionRange::MergeFrom(const ::google::protobuf::Message& from) { +void DescriptorProto_ExtensionRange::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.DescriptorProto.ExtensionRange) GOOGLE_DCHECK_NE(&from, this); const DescriptorProto_ExtensionRange* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.DescriptorProto.ExtensionRange) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.DescriptorProto.ExtensionRange) MergeFrom(*source); @@ -3220,13 +2856,13 @@ void DescriptorProto_ExtensionRange::MergeFrom(const DescriptorProto_ExtensionRa // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.DescriptorProto.ExtensionRange) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - mutable_options()->::google::protobuf::ExtensionRangeOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions::MergeFrom(from.options()); } if (cached_has_bits & 0x00000002u) { start_ = from.start_; @@ -3238,7 +2874,7 @@ void DescriptorProto_ExtensionRange::MergeFrom(const DescriptorProto_ExtensionRa } } -void DescriptorProto_ExtensionRange::CopyFrom(const ::google::protobuf::Message& from) { +void DescriptorProto_ExtensionRange::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.DescriptorProto.ExtensionRange) if (&from == this) return; Clear(); @@ -3259,25 +2895,6 @@ bool DescriptorProto_ExtensionRange::IsInitialized() const { return true; } -void DescriptorProto_ExtensionRange::Swap(DescriptorProto_ExtensionRange* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - DescriptorProto_ExtensionRange* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void DescriptorProto_ExtensionRange::UnsafeArenaSwap(DescriptorProto_ExtensionRange* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void DescriptorProto_ExtensionRange::InternalSwap(DescriptorProto_ExtensionRange* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -3287,9 +2904,8 @@ void DescriptorProto_ExtensionRange::InternalSwap(DescriptorProto_ExtensionRange swap(end_, other->end_); } -::google::protobuf::Metadata DescriptorProto_ExtensionRange::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata DescriptorProto_ExtensionRange::GetMetadata() const { + return GetMetadataStatic(); } @@ -3297,36 +2913,32 @@ void DescriptorProto_ExtensionRange::InternalSwap(DescriptorProto_ExtensionRange void DescriptorProto_ReservedRange::InitAsDefaultInstance() { } -class DescriptorProto_ReservedRange::HasBitSetters { +class DescriptorProto_ReservedRange::_Internal { public: - static void set_has_start(DescriptorProto_ReservedRange* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_start(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_end(DescriptorProto_ReservedRange* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_end(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int DescriptorProto_ReservedRange::kStartFieldNumber; -const int DescriptorProto_ReservedRange::kEndFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - DescriptorProto_ReservedRange::DescriptorProto_ReservedRange() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.DescriptorProto.ReservedRange) } -DescriptorProto_ReservedRange::DescriptorProto_ReservedRange(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +DescriptorProto_ReservedRange::DescriptorProto_ReservedRange(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.DescriptorProto.ReservedRange) } DescriptorProto_ReservedRange::DescriptorProto_ReservedRange(const DescriptorProto_ReservedRange& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); ::memcpy(&start_, &from.start_, @@ -3347,27 +2959,27 @@ DescriptorProto_ReservedRange::~DescriptorProto_ReservedRange() { } void DescriptorProto_ReservedRange::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void DescriptorProto_ReservedRange::ArenaDtor(void* object) { DescriptorProto_ReservedRange* _this = reinterpret_cast< DescriptorProto_ReservedRange* >(object); (void)_this; } -void DescriptorProto_ReservedRange::RegisterArenaDtor(::google::protobuf::Arena*) { +void DescriptorProto_ReservedRange::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void DescriptorProto_ReservedRange::SetCachedSize(int size) const { _cached_size_.Set(size); } const DescriptorProto_ReservedRange& DescriptorProto_ReservedRange::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void DescriptorProto_ReservedRange::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.DescriptorProto.ReservedRange) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -3382,80 +2994,68 @@ void DescriptorProto_ReservedRange::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* DescriptorProto_ReservedRange::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* DescriptorProto_ReservedRange::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional int32 start = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_start(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + _Internal::set_has_start(&has_bits); + start_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 end = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_end(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_end(&has_bits); + end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool DescriptorProto_ReservedRange::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.DescriptorProto.ReservedRange) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional int32 start = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_start(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { + _Internal::set_has_start(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &start_))); } else { goto handle_unusual; @@ -3465,10 +3065,10 @@ bool DescriptorProto_ReservedRange::MergePartialFromCodedStream( // optional int32 end = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_end(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_end(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &end_))); } else { goto handle_unusual; @@ -3481,7 +3081,7 @@ bool DescriptorProto_ReservedRange::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -3497,51 +3097,28 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void DescriptorProto_ReservedRange::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.DescriptorProto.ReservedRange) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional int32 start = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(1, this->start(), output); - } - - // optional int32 end = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->end(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.DescriptorProto.ReservedRange) -} - -::google::protobuf::uint8* DescriptorProto_ReservedRange::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* DescriptorProto_ReservedRange::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.DescriptorProto.ReservedRange) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional int32 start = 1; if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(1, this->start(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(1, this->start(), target); } // optional int32 end = 2; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->end(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(2, this->end(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.DescriptorProto.ReservedRange) return target; @@ -3551,12 +3128,7 @@ size_t DescriptorProto_ReservedRange::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.DescriptorProto.ReservedRange) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -3565,32 +3137,36 @@ size_t DescriptorProto_ReservedRange::ByteSizeLong() const { // optional int32 start = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->start()); } // optional int32 end = 2; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->end()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void DescriptorProto_ReservedRange::MergeFrom(const ::google::protobuf::Message& from) { +void DescriptorProto_ReservedRange::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.DescriptorProto.ReservedRange) GOOGLE_DCHECK_NE(&from, this); const DescriptorProto_ReservedRange* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.DescriptorProto.ReservedRange) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.DescriptorProto.ReservedRange) MergeFrom(*source); @@ -3601,7 +3177,7 @@ void DescriptorProto_ReservedRange::MergeFrom(const DescriptorProto_ReservedRang // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.DescriptorProto.ReservedRange) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = from._has_bits_[0]; @@ -3616,7 +3192,7 @@ void DescriptorProto_ReservedRange::MergeFrom(const DescriptorProto_ReservedRang } } -void DescriptorProto_ReservedRange::CopyFrom(const ::google::protobuf::Message& from) { +void DescriptorProto_ReservedRange::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.DescriptorProto.ReservedRange) if (&from == this) return; Clear(); @@ -3634,25 +3210,6 @@ bool DescriptorProto_ReservedRange::IsInitialized() const { return true; } -void DescriptorProto_ReservedRange::Swap(DescriptorProto_ReservedRange* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - DescriptorProto_ReservedRange* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void DescriptorProto_ReservedRange::UnsafeArenaSwap(DescriptorProto_ReservedRange* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void DescriptorProto_ReservedRange::InternalSwap(DescriptorProto_ReservedRange* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -3661,36 +3218,36 @@ void DescriptorProto_ReservedRange::InternalSwap(DescriptorProto_ReservedRange* swap(end_, other->end_); } -::google::protobuf::Metadata DescriptorProto_ReservedRange::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata DescriptorProto_ReservedRange::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void DescriptorProto::InitAsDefaultInstance() { - ::google::protobuf::_DescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::MessageOptions*>( - ::google::protobuf::MessageOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_DescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::MessageOptions*>( + PROTOBUF_NAMESPACE_ID::MessageOptions::internal_default_instance()); } -class DescriptorProto::HasBitSetters { +class DescriptorProto::_Internal { public: - static void set_has_name(DescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static const ::google::protobuf::MessageOptions& options(const DescriptorProto* msg); - static void set_has_options(DescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static const PROTOBUF_NAMESPACE_ID::MessageOptions& options(const DescriptorProto* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -const ::google::protobuf::MessageOptions& -DescriptorProto::HasBitSetters::options(const DescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::MessageOptions& +DescriptorProto::_Internal::options(const DescriptorProto* msg) { return *msg->options_; } void DescriptorProto::unsafe_arena_set_allocated_options( - ::google::protobuf::MessageOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::MessageOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -3701,26 +3258,13 @@ void DescriptorProto::unsafe_arena_set_allocated_options( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.DescriptorProto.options) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int DescriptorProto::kNameFieldNumber; -const int DescriptorProto::kFieldFieldNumber; -const int DescriptorProto::kExtensionFieldNumber; -const int DescriptorProto::kNestedTypeFieldNumber; -const int DescriptorProto::kEnumTypeFieldNumber; -const int DescriptorProto::kExtensionRangeFieldNumber; -const int DescriptorProto::kOneofDeclFieldNumber; -const int DescriptorProto::kOptionsFieldNumber; -const int DescriptorProto::kReservedRangeFieldNumber; -const int DescriptorProto::kReservedNameFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - DescriptorProto::DescriptorProto() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.DescriptorProto) } -DescriptorProto::DescriptorProto(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +DescriptorProto::DescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), field_(arena), nested_type_(arena), @@ -3735,8 +3279,8 @@ DescriptorProto::DescriptorProto(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.DescriptorProto) } DescriptorProto::DescriptorProto(const DescriptorProto& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), field_(from.field_), nested_type_(from.nested_type_), @@ -3747,24 +3291,23 @@ DescriptorProto::DescriptorProto(const DescriptorProto& from) reserved_range_(from.reserved_range_), reserved_name_(from.reserved_name_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name(), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name(), GetArenaNoVirtual()); } if (from.has_options()) { - options_ = new ::google::protobuf::MessageOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::MessageOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } // @@protoc_insertion_point(copy_constructor:google.protobuf.DescriptorProto) } void DescriptorProto::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - options_ = NULL; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + options_ = nullptr; } DescriptorProto::~DescriptorProto() { @@ -3773,8 +3316,8 @@ DescriptorProto::~DescriptorProto() { } void DescriptorProto::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete options_; } @@ -3782,20 +3325,20 @@ void DescriptorProto::ArenaDtor(void* object) { DescriptorProto* _this = reinterpret_cast< DescriptorProto* >(object); (void)_this; } -void DescriptorProto::RegisterArenaDtor(::google::protobuf::Arena*) { +void DescriptorProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void DescriptorProto::SetCachedSize(int size) const { _cached_size_.Set(size); } const DescriptorProto& DescriptorProto::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void DescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.DescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -3813,7 +3356,7 @@ void DescriptorProto::Clear() { name_.ClearNonDefaultToEmpty(); } if (cached_has_bits & 0x00000002u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } } @@ -3822,222 +3365,164 @@ void DescriptorProto::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* DescriptorProto::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* DescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.DescriptorProto.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.DescriptorProto.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.FieldDescriptorProto field = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::FieldDescriptorProto::_InternalParse; - object = msg->add_field(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 18 && (ptr += 1)); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_field(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 18); + } else goto handle_unusual; + continue; // repeated .google.protobuf.DescriptorProto nested_type = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::DescriptorProto::_InternalParse; - object = msg->add_nested_type(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 26 && (ptr += 1)); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_nested_type(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 26); + } else goto handle_unusual; + continue; // repeated .google.protobuf.EnumDescriptorProto enum_type = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::EnumDescriptorProto::_InternalParse; - object = msg->add_enum_type(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 34 && (ptr += 1)); - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_enum_type(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 34); + } else goto handle_unusual; + continue; // repeated .google.protobuf.DescriptorProto.ExtensionRange extension_range = 5; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::DescriptorProto_ExtensionRange::_InternalParse; - object = msg->add_extension_range(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 42 && (ptr += 1)); - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 42)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_extension_range(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 42); + } else goto handle_unusual; + continue; // repeated .google.protobuf.FieldDescriptorProto extension = 6; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 50) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::FieldDescriptorProto::_InternalParse; - object = msg->add_extension(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 50 && (ptr += 1)); - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 50)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_extension(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 50); + } else goto handle_unusual; + continue; // optional .google.protobuf.MessageOptions options = 7; - case 7: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::MessageOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 7: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.OneofDescriptorProto oneof_decl = 8; - case 8: { - if (static_cast<::google::protobuf::uint8>(tag) != 66) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::OneofDescriptorProto::_InternalParse; - object = msg->add_oneof_decl(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 66 && (ptr += 1)); - break; - } + case 8: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 66)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_oneof_decl(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 66); + } else goto handle_unusual; + continue; // repeated .google.protobuf.DescriptorProto.ReservedRange reserved_range = 9; - case 9: { - if (static_cast<::google::protobuf::uint8>(tag) != 74) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::DescriptorProto_ReservedRange::_InternalParse; - object = msg->add_reserved_range(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 74 && (ptr += 1)); - break; - } + case 9: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 74)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_reserved_range(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 74); + } else goto handle_unusual; + continue; // repeated string reserved_name = 10; - case 10: { - if (static_cast<::google::protobuf::uint8>(tag) != 82) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.DescriptorProto.reserved_name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->add_reserved_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 82 && (ptr += 1)); - break; - } + case 10: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 82)) { + ptr -= 1; + do { + ptr += 1; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_reserved_name(), ptr, ctx, "google.protobuf.DescriptorProto.reserved_name"); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 82); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool DescriptorProto::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.DescriptorProto) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.DescriptorProto.name"); } else { goto handle_unusual; @@ -4047,8 +3532,8 @@ bool DescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.FieldDescriptorProto field = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_field())); } else { goto handle_unusual; @@ -4058,8 +3543,8 @@ bool DescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.DescriptorProto nested_type = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_nested_type())); } else { goto handle_unusual; @@ -4069,8 +3554,8 @@ bool DescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.EnumDescriptorProto enum_type = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (34 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_enum_type())); } else { goto handle_unusual; @@ -4080,8 +3565,8 @@ bool DescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.DescriptorProto.ExtensionRange extension_range = 5; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (42 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (42 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_extension_range())); } else { goto handle_unusual; @@ -4091,8 +3576,8 @@ bool DescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.FieldDescriptorProto extension = 6; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (50 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (50 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_extension())); } else { goto handle_unusual; @@ -4102,8 +3587,8 @@ bool DescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.MessageOptions options = 7; case 7: { - if (static_cast< ::google::protobuf::uint8>(tag) == (58 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (58 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -4113,8 +3598,8 @@ bool DescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.OneofDescriptorProto oneof_decl = 8; case 8: { - if (static_cast< ::google::protobuf::uint8>(tag) == (66 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (66 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_oneof_decl())); } else { goto handle_unusual; @@ -4124,8 +3609,8 @@ bool DescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.DescriptorProto.ReservedRange reserved_range = 9; case 9: { - if (static_cast< ::google::protobuf::uint8>(tag) == (74 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (74 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_reserved_range())); } else { goto handle_unusual; @@ -4135,13 +3620,13 @@ bool DescriptorProto::MergePartialFromCodedStream( // repeated string reserved_name = 10; case 10: { - if (static_cast< ::google::protobuf::uint8>(tag) == (82 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (82 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->add_reserved_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->reserved_name(this->reserved_name_size() - 1).data(), static_cast(this->reserved_name(this->reserved_name_size() - 1).length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.DescriptorProto.reserved_name"); } else { goto handle_unusual; @@ -4154,7 +3639,7 @@ bool DescriptorProto::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -4170,204 +3655,101 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void DescriptorProto::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.DescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.DescriptorProto.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // repeated .google.protobuf.FieldDescriptorProto field = 2; - for (unsigned int i = 0, - n = static_cast(this->field_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 2, - this->field(static_cast(i)), - output); - } - - // repeated .google.protobuf.DescriptorProto nested_type = 3; - for (unsigned int i = 0, - n = static_cast(this->nested_type_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 3, - this->nested_type(static_cast(i)), - output); - } - - // repeated .google.protobuf.EnumDescriptorProto enum_type = 4; - for (unsigned int i = 0, - n = static_cast(this->enum_type_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 4, - this->enum_type(static_cast(i)), - output); - } - - // repeated .google.protobuf.DescriptorProto.ExtensionRange extension_range = 5; - for (unsigned int i = 0, - n = static_cast(this->extension_range_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 5, - this->extension_range(static_cast(i)), - output); - } - - // repeated .google.protobuf.FieldDescriptorProto extension = 6; - for (unsigned int i = 0, - n = static_cast(this->extension_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 6, - this->extension(static_cast(i)), - output); - } - - // optional .google.protobuf.MessageOptions options = 7; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 7, HasBitSetters::options(this), output); - } - - // repeated .google.protobuf.OneofDescriptorProto oneof_decl = 8; - for (unsigned int i = 0, - n = static_cast(this->oneof_decl_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 8, - this->oneof_decl(static_cast(i)), - output); - } - - // repeated .google.protobuf.DescriptorProto.ReservedRange reserved_range = 9; - for (unsigned int i = 0, - n = static_cast(this->reserved_range_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 9, - this->reserved_range(static_cast(i)), - output); - } - - // repeated string reserved_name = 10; - for (int i = 0, n = this->reserved_name_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->reserved_name(i).data(), static_cast(this->reserved_name(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.DescriptorProto.reserved_name"); - ::google::protobuf::internal::WireFormatLite::WriteString( - 10, this->reserved_name(i), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.DescriptorProto) -} - -::google::protobuf::uint8* DescriptorProto::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* DescriptorProto::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.DescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.DescriptorProto.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // repeated .google.protobuf.FieldDescriptorProto field = 2; - for (unsigned int i = 0, - n = static_cast(this->field_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 2, this->field(static_cast(i)), deterministic, target); + for (auto it = this->field().pointer_begin(), + end = this->field().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(2, **it, target, stream); } // repeated .google.protobuf.DescriptorProto nested_type = 3; - for (unsigned int i = 0, - n = static_cast(this->nested_type_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 3, this->nested_type(static_cast(i)), deterministic, target); + for (auto it = this->nested_type().pointer_begin(), + end = this->nested_type().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(3, **it, target, stream); } // repeated .google.protobuf.EnumDescriptorProto enum_type = 4; - for (unsigned int i = 0, - n = static_cast(this->enum_type_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 4, this->enum_type(static_cast(i)), deterministic, target); + for (auto it = this->enum_type().pointer_begin(), + end = this->enum_type().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(4, **it, target, stream); } // repeated .google.protobuf.DescriptorProto.ExtensionRange extension_range = 5; - for (unsigned int i = 0, - n = static_cast(this->extension_range_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 5, this->extension_range(static_cast(i)), deterministic, target); + for (auto it = this->extension_range().pointer_begin(), + end = this->extension_range().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(5, **it, target, stream); } // repeated .google.protobuf.FieldDescriptorProto extension = 6; - for (unsigned int i = 0, - n = static_cast(this->extension_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 6, this->extension(static_cast(i)), deterministic, target); + for (auto it = this->extension().pointer_begin(), + end = this->extension().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(6, **it, target, stream); } // optional .google.protobuf.MessageOptions options = 7; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 7, HasBitSetters::options(this), deterministic, target); + 7, _Internal::options(this), target, stream); } // repeated .google.protobuf.OneofDescriptorProto oneof_decl = 8; - for (unsigned int i = 0, - n = static_cast(this->oneof_decl_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 8, this->oneof_decl(static_cast(i)), deterministic, target); + for (auto it = this->oneof_decl().pointer_begin(), + end = this->oneof_decl().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(8, **it, target, stream); } // repeated .google.protobuf.DescriptorProto.ReservedRange reserved_range = 9; - for (unsigned int i = 0, - n = static_cast(this->reserved_range_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 9, this->reserved_range(static_cast(i)), deterministic, target); + for (auto it = this->reserved_range().pointer_begin(), + end = this->reserved_range().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(9, **it, target, stream); } // repeated string reserved_name = 10; - for (int i = 0, n = this->reserved_name_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->reserved_name(i).data(), static_cast(this->reserved_name(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + for (auto it = this->reserved_name().pointer_begin(), + end = this->reserved_name().pointer_end(); it < end; ++it) { + const auto& s = **it; + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( + s.data(), static_cast(s.length()), + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.DescriptorProto.reserved_name"); - target = ::google::protobuf::internal::WireFormatLite:: - WriteStringToArray(10, this->reserved_name(i), target); + target = stream->WriteString(10, s, target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.DescriptorProto) return target; @@ -4377,12 +3759,7 @@ size_t DescriptorProto::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.DescriptorProto) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -4392,7 +3769,7 @@ size_t DescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->field(static_cast(i))); } } @@ -4403,7 +3780,7 @@ size_t DescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->nested_type(static_cast(i))); } } @@ -4414,7 +3791,7 @@ size_t DescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->enum_type(static_cast(i))); } } @@ -4425,7 +3802,7 @@ size_t DescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->extension_range(static_cast(i))); } } @@ -4436,7 +3813,7 @@ size_t DescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->extension(static_cast(i))); } } @@ -4447,7 +3824,7 @@ size_t DescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->oneof_decl(static_cast(i))); } } @@ -4458,16 +3835,16 @@ size_t DescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->reserved_range(static_cast(i))); } } // repeated string reserved_name = 10; total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->reserved_name_size()); + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->reserved_name_size()); for (int i = 0, n = this->reserved_name_size(); i < n; i++) { - total_size += ::google::protobuf::internal::WireFormatLite::StringSize( + total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->reserved_name(i)); } @@ -4476,32 +3853,36 @@ size_t DescriptorProto::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional .google.protobuf.MessageOptions options = 7; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void DescriptorProto::MergeFrom(const ::google::protobuf::Message& from) { +void DescriptorProto::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.DescriptorProto) GOOGLE_DCHECK_NE(&from, this); const DescriptorProto* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.DescriptorProto) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.DescriptorProto) MergeFrom(*source); @@ -4512,7 +3893,7 @@ void DescriptorProto::MergeFrom(const DescriptorProto& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.DescriptorProto) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; field_.MergeFrom(from.field_); @@ -4529,12 +3910,12 @@ void DescriptorProto::MergeFrom(const DescriptorProto& from) { set_name(from.name()); } if (cached_has_bits & 0x00000002u) { - mutable_options()->::google::protobuf::MessageOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::MessageOptions::MergeFrom(from.options()); } } } -void DescriptorProto::CopyFrom(const ::google::protobuf::Message& from) { +void DescriptorProto::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.DescriptorProto) if (&from == this) return; Clear(); @@ -4549,37 +3930,18 @@ void DescriptorProto::CopyFrom(const DescriptorProto& from) { } bool DescriptorProto::IsInitialized() const { - if (!::google::protobuf::internal::AllAreInitialized(this->field())) return false; - if (!::google::protobuf::internal::AllAreInitialized(this->nested_type())) return false; - if (!::google::protobuf::internal::AllAreInitialized(this->enum_type())) return false; - if (!::google::protobuf::internal::AllAreInitialized(this->extension_range())) return false; - if (!::google::protobuf::internal::AllAreInitialized(this->extension())) return false; - if (!::google::protobuf::internal::AllAreInitialized(this->oneof_decl())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->field())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->nested_type())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->enum_type())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->extension_range())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->extension())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->oneof_decl())) return false; if (has_options()) { if (!this->options_->IsInitialized()) return false; } return true; } -void DescriptorProto::Swap(DescriptorProto* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - DescriptorProto* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void DescriptorProto::UnsafeArenaSwap(DescriptorProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void DescriptorProto::InternalSwap(DescriptorProto* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -4592,14 +3954,13 @@ void DescriptorProto::InternalSwap(DescriptorProto* other) { CastToBase(&oneof_decl_)->InternalSwap(CastToBase(&other->oneof_decl_)); CastToBase(&reserved_range_)->InternalSwap(CastToBase(&other->reserved_range_)); reserved_name_.InternalSwap(CastToBase(&other->reserved_name_)); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(options_, other->options_); } -::google::protobuf::Metadata DescriptorProto::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata DescriptorProto::GetMetadata() const { + return GetMetadataStatic(); } @@ -4607,21 +3968,18 @@ void DescriptorProto::InternalSwap(DescriptorProto* other) { void ExtensionRangeOptions::InitAsDefaultInstance() { } -class ExtensionRangeOptions::HasBitSetters { +class ExtensionRangeOptions::_Internal { public: + using HasBits = decltype(std::declval()._has_bits_); }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int ExtensionRangeOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - ExtensionRangeOptions::ExtensionRangeOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.ExtensionRangeOptions) } -ExtensionRangeOptions::ExtensionRangeOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +ExtensionRangeOptions::ExtensionRangeOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -4630,8 +3988,8 @@ ExtensionRangeOptions::ExtensionRangeOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.ExtensionRangeOptions) } ExtensionRangeOptions::ExtensionRangeOptions(const ExtensionRangeOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -4640,8 +3998,7 @@ ExtensionRangeOptions::ExtensionRangeOptions(const ExtensionRangeOptions& from) } void ExtensionRangeOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base); } ExtensionRangeOptions::~ExtensionRangeOptions() { @@ -4650,27 +4007,27 @@ ExtensionRangeOptions::~ExtensionRangeOptions() { } void ExtensionRangeOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void ExtensionRangeOptions::ArenaDtor(void* object) { ExtensionRangeOptions* _this = reinterpret_cast< ExtensionRangeOptions* >(object); (void)_this; } -void ExtensionRangeOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void ExtensionRangeOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void ExtensionRangeOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const ExtensionRangeOptions& ExtensionRangeOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void ExtensionRangeOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.ExtensionRangeOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -4681,81 +4038,66 @@ void ExtensionRangeOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* ExtensionRangeOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* ExtensionRangeOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ExtensionRangeOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.ExtensionRangeOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -4774,7 +4116,7 @@ bool ExtensionRangeOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -4790,53 +4132,27 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void ExtensionRangeOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.ExtensionRangeOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.ExtensionRangeOptions) -} - -::google::protobuf::uint8* ExtensionRangeOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* ExtensionRangeOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.ExtensionRangeOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.ExtensionRangeOptions) return target; @@ -4848,12 +4164,7 @@ size_t ExtensionRangeOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -4863,25 +4174,29 @@ size_t ExtensionRangeOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void ExtensionRangeOptions::MergeFrom(const ::google::protobuf::Message& from) { +void ExtensionRangeOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.ExtensionRangeOptions) GOOGLE_DCHECK_NE(&from, this); const ExtensionRangeOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.ExtensionRangeOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.ExtensionRangeOptions) MergeFrom(*source); @@ -4893,13 +4208,13 @@ void ExtensionRangeOptions::MergeFrom(const ExtensionRangeOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); } -void ExtensionRangeOptions::CopyFrom(const ::google::protobuf::Message& from) { +void ExtensionRangeOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.ExtensionRangeOptions) if (&from == this) return; Clear(); @@ -4918,29 +4233,10 @@ bool ExtensionRangeOptions::IsInitialized() const { return false; } - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; return true; } -void ExtensionRangeOptions::Swap(ExtensionRangeOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - ExtensionRangeOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void ExtensionRangeOptions::UnsafeArenaSwap(ExtensionRangeOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void ExtensionRangeOptions::InternalSwap(ExtensionRangeOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); @@ -4949,60 +4245,60 @@ void ExtensionRangeOptions::InternalSwap(ExtensionRangeOptions* other) { CastToBase(&uninterpreted_option_)->InternalSwap(CastToBase(&other->uninterpreted_option_)); } -::google::protobuf::Metadata ExtensionRangeOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata ExtensionRangeOptions::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void FieldDescriptorProto::InitAsDefaultInstance() { - ::google::protobuf::_FieldDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::FieldOptions*>( - ::google::protobuf::FieldOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_FieldDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::FieldOptions*>( + PROTOBUF_NAMESPACE_ID::FieldOptions::internal_default_instance()); } -class FieldDescriptorProto::HasBitSetters { +class FieldDescriptorProto::_Internal { public: - static void set_has_name(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_number(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000040u; + static void set_has_number(HasBits* has_bits) { + (*has_bits)[0] |= 64u; } - static void set_has_label(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000100u; + static void set_has_label(HasBits* has_bits) { + (*has_bits)[0] |= 256u; } - static void set_has_type(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000200u; + static void set_has_type(HasBits* has_bits) { + (*has_bits)[0] |= 512u; } - static void set_has_type_name(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_type_name(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_extendee(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_extendee(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_default_value(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_default_value(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_oneof_index(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000080u; + static void set_has_oneof_index(HasBits* has_bits) { + (*has_bits)[0] |= 128u; } - static void set_has_json_name(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_json_name(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static const ::google::protobuf::FieldOptions& options(const FieldDescriptorProto* msg); - static void set_has_options(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static const PROTOBUF_NAMESPACE_ID::FieldOptions& options(const FieldDescriptorProto* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } }; -const ::google::protobuf::FieldOptions& -FieldDescriptorProto::HasBitSetters::options(const FieldDescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::FieldOptions& +FieldDescriptorProto::_Internal::options(const FieldDescriptorProto* msg) { return *msg->options_; } void FieldDescriptorProto::unsafe_arena_set_allocated_options( - ::google::protobuf::FieldOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::FieldOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -5013,65 +4309,52 @@ void FieldDescriptorProto::unsafe_arena_set_allocated_options( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FieldDescriptorProto.options) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int FieldDescriptorProto::kNameFieldNumber; -const int FieldDescriptorProto::kNumberFieldNumber; -const int FieldDescriptorProto::kLabelFieldNumber; -const int FieldDescriptorProto::kTypeFieldNumber; -const int FieldDescriptorProto::kTypeNameFieldNumber; -const int FieldDescriptorProto::kExtendeeFieldNumber; -const int FieldDescriptorProto::kDefaultValueFieldNumber; -const int FieldDescriptorProto::kOneofIndexFieldNumber; -const int FieldDescriptorProto::kJsonNameFieldNumber; -const int FieldDescriptorProto::kOptionsFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - FieldDescriptorProto::FieldDescriptorProto() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.FieldDescriptorProto) } -FieldDescriptorProto::FieldDescriptorProto(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +FieldDescriptorProto::FieldDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.FieldDescriptorProto) } FieldDescriptorProto::FieldDescriptorProto(const FieldDescriptorProto& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name(), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name(), GetArenaNoVirtual()); } - extendee_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + extendee_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_extendee()) { - extendee_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.extendee(), + extendee_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.extendee(), GetArenaNoVirtual()); } - type_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + type_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_type_name()) { - type_name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.type_name(), + type_name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.type_name(), GetArenaNoVirtual()); } - default_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + default_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_default_value()) { - default_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.default_value(), + default_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.default_value(), GetArenaNoVirtual()); } - json_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + json_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_json_name()) { - json_name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.json_name(), + json_name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.json_name(), GetArenaNoVirtual()); } if (from.has_options()) { - options_ = new ::google::protobuf::FieldOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::FieldOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } ::memcpy(&number_, &from.number_, static_cast(reinterpret_cast(&type_) - @@ -5080,13 +4363,12 @@ FieldDescriptorProto::FieldDescriptorProto(const FieldDescriptorProto& from) } void FieldDescriptorProto::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - extendee_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - type_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - default_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - json_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + extendee_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + type_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + default_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + json_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&options_, 0, static_cast( reinterpret_cast(&oneof_index_) - reinterpret_cast(&options_)) + sizeof(oneof_index_)); @@ -5100,12 +4382,12 @@ FieldDescriptorProto::~FieldDescriptorProto() { } void FieldDescriptorProto::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - extendee_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - type_name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - default_value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - json_name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + extendee_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + type_name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + default_value_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + json_name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete options_; } @@ -5113,20 +4395,20 @@ void FieldDescriptorProto::ArenaDtor(void* object) { FieldDescriptorProto* _this = reinterpret_cast< FieldDescriptorProto* >(object); (void)_this; } -void FieldDescriptorProto::RegisterArenaDtor(::google::protobuf::Arena*) { +void FieldDescriptorProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void FieldDescriptorProto::SetCachedSize(int size) const { _cached_size_.Set(size); } const FieldDescriptorProto& FieldDescriptorProto::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void FieldDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FieldDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -5148,7 +4430,7 @@ void FieldDescriptorProto::Clear() { json_name_.ClearNonDefaultToEmpty(); } if (cached_has_bits & 0x00000020u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } } @@ -5166,203 +4448,136 @@ void FieldDescriptorProto::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* FieldDescriptorProto::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* FieldDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FieldDescriptorProto.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.FieldDescriptorProto.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string extendee = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FieldDescriptorProto.extendee"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_extendee(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_extendee(), ptr, ctx, "google.protobuf.FieldDescriptorProto.extendee"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 number = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_number(value); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { + _Internal::set_has_number(&has_bits); + number_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.FieldDescriptorProto.Label label = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 32) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - if (!::google::protobuf::FieldDescriptorProto_Label_IsValid(val)) { - ::google::protobuf::internal::WriteVarint(4, val, msg->mutable_unknown_fields()); - break; - } - ::google::protobuf::FieldDescriptorProto_Label value = static_cast<::google::protobuf::FieldDescriptorProto_Label>(val); - msg->set_label(value); - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 32)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + if (PROTOBUF_PREDICT_TRUE(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label_IsValid(val))) { + set_label(static_cast(val)); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(4, val, mutable_unknown_fields()); + } + } else goto handle_unusual; + continue; // optional .google.protobuf.FieldDescriptorProto.Type type = 5; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 40) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - if (!::google::protobuf::FieldDescriptorProto_Type_IsValid(val)) { - ::google::protobuf::internal::WriteVarint(5, val, msg->mutable_unknown_fields()); - break; - } - ::google::protobuf::FieldDescriptorProto_Type value = static_cast<::google::protobuf::FieldDescriptorProto_Type>(val); - msg->set_type(value); - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 40)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + if (PROTOBUF_PREDICT_TRUE(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type_IsValid(val))) { + set_type(static_cast(val)); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(5, val, mutable_unknown_fields()); + } + } else goto handle_unusual; + continue; // optional string type_name = 6; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 50) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FieldDescriptorProto.type_name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_type_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 50)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_type_name(), ptr, ctx, "google.protobuf.FieldDescriptorProto.type_name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string default_value = 7; - case 7: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FieldDescriptorProto.default_value"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_default_value(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 7: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_default_value(), ptr, ctx, "google.protobuf.FieldDescriptorProto.default_value"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.FieldOptions options = 8; - case 8: { - if (static_cast<::google::protobuf::uint8>(tag) != 66) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::FieldOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 8: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 66)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 oneof_index = 9; - case 9: { - if (static_cast<::google::protobuf::uint8>(tag) != 72) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_oneof_index(value); - break; - } + case 9: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 72)) { + _Internal::set_has_oneof_index(&has_bits); + oneof_index_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string json_name = 10; - case 10: { - if (static_cast<::google::protobuf::uint8>(tag) != 82) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FieldDescriptorProto.json_name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_json_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 10: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 82)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_json_name(), ptr, ctx, "google.protobuf.FieldDescriptorProto.json_name"); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; - } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; + } + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FieldDescriptorProto::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.FieldDescriptorProto) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FieldDescriptorProto.name"); } else { goto handle_unusual; @@ -5372,12 +4587,12 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional string extendee = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_extendee())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->extendee().data(), static_cast(this->extendee().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FieldDescriptorProto.extendee"); } else { goto handle_unusual; @@ -5387,10 +4602,10 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional int32 number = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_number(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { + _Internal::set_has_number(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &number_))); } else { goto handle_unusual; @@ -5400,16 +4615,16 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.FieldDescriptorProto.Label label = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (32 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (32 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - if (::google::protobuf::FieldDescriptorProto_Label_IsValid(value)) { - set_label(static_cast< ::google::protobuf::FieldDescriptorProto_Label >(value)); + if (PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label_IsValid(value)) { + set_label(static_cast< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label >(value)); } else { mutable_unknown_fields()->AddVarint( - 4, static_cast<::google::protobuf::uint64>(value)); + 4, static_cast<::PROTOBUF_NAMESPACE_ID::uint64>(value)); } } else { goto handle_unusual; @@ -5419,16 +4634,16 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.FieldDescriptorProto.Type type = 5; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (40 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (40 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - if (::google::protobuf::FieldDescriptorProto_Type_IsValid(value)) { - set_type(static_cast< ::google::protobuf::FieldDescriptorProto_Type >(value)); + if (PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type_IsValid(value)) { + set_type(static_cast< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type >(value)); } else { mutable_unknown_fields()->AddVarint( - 5, static_cast<::google::protobuf::uint64>(value)); + 5, static_cast<::PROTOBUF_NAMESPACE_ID::uint64>(value)); } } else { goto handle_unusual; @@ -5438,12 +4653,12 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional string type_name = 6; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (50 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (50 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_type_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->type_name().data(), static_cast(this->type_name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FieldDescriptorProto.type_name"); } else { goto handle_unusual; @@ -5453,12 +4668,12 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional string default_value = 7; case 7: { - if (static_cast< ::google::protobuf::uint8>(tag) == (58 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (58 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_default_value())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->default_value().data(), static_cast(this->default_value().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FieldDescriptorProto.default_value"); } else { goto handle_unusual; @@ -5468,8 +4683,8 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.FieldOptions options = 8; case 8: { - if (static_cast< ::google::protobuf::uint8>(tag) == (66 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (66 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -5479,10 +4694,10 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional int32 oneof_index = 9; case 9: { - if (static_cast< ::google::protobuf::uint8>(tag) == (72 & 0xFF)) { - HasBitSetters::set_has_oneof_index(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (72 & 0xFF)) { + _Internal::set_has_oneof_index(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &oneof_index_))); } else { goto handle_unusual; @@ -5492,12 +4707,12 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional string json_name = 10; case 10: { - if (static_cast< ::google::protobuf::uint8>(tag) == (82 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (82 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_json_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->json_name().data(), static_cast(this->json_name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FieldDescriptorProto.json_name"); } else { goto handle_unusual; @@ -5510,7 +4725,7 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -5526,193 +4741,100 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void FieldDescriptorProto::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.FieldDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FieldDescriptorProto.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // optional string extendee = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->extendee().data(), static_cast(this->extendee().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FieldDescriptorProto.extendee"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 2, this->extendee(), output); - } - - // optional int32 number = 3; - if (cached_has_bits & 0x00000040u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->number(), output); - } - - // optional .google.protobuf.FieldDescriptorProto.Label label = 4; - if (cached_has_bits & 0x00000100u) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 4, this->label(), output); - } - - // optional .google.protobuf.FieldDescriptorProto.Type type = 5; - if (cached_has_bits & 0x00000200u) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 5, this->type(), output); - } - - // optional string type_name = 6; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->type_name().data(), static_cast(this->type_name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FieldDescriptorProto.type_name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 6, this->type_name(), output); - } - - // optional string default_value = 7; - if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->default_value().data(), static_cast(this->default_value().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FieldDescriptorProto.default_value"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 7, this->default_value(), output); - } - - // optional .google.protobuf.FieldOptions options = 8; - if (cached_has_bits & 0x00000020u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 8, HasBitSetters::options(this), output); - } - - // optional int32 oneof_index = 9; - if (cached_has_bits & 0x00000080u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(9, this->oneof_index(), output); - } - - // optional string json_name = 10; - if (cached_has_bits & 0x00000010u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->json_name().data(), static_cast(this->json_name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FieldDescriptorProto.json_name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 10, this->json_name(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.FieldDescriptorProto) -} - -::google::protobuf::uint8* FieldDescriptorProto::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* FieldDescriptorProto::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.FieldDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FieldDescriptorProto.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // optional string extendee = 2; if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->extendee().data(), static_cast(this->extendee().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FieldDescriptorProto.extendee"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 2, this->extendee(), target); } // optional int32 number = 3; if (cached_has_bits & 0x00000040u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->number(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(3, this->number(), target); } // optional .google.protobuf.FieldDescriptorProto.Label label = 4; if (cached_has_bits & 0x00000100u) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 4, this->label(), target); } // optional .google.protobuf.FieldDescriptorProto.Type type = 5; if (cached_has_bits & 0x00000200u) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 5, this->type(), target); } // optional string type_name = 6; if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->type_name().data(), static_cast(this->type_name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FieldDescriptorProto.type_name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 6, this->type_name(), target); } // optional string default_value = 7; if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->default_value().data(), static_cast(this->default_value().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FieldDescriptorProto.default_value"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 7, this->default_value(), target); } // optional .google.protobuf.FieldOptions options = 8; if (cached_has_bits & 0x00000020u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 8, HasBitSetters::options(this), deterministic, target); + 8, _Internal::options(this), target, stream); } // optional int32 oneof_index = 9; if (cached_has_bits & 0x00000080u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(9, this->oneof_index(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(9, this->oneof_index(), target); } // optional string json_name = 10; if (cached_has_bits & 0x00000010u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->json_name().data(), static_cast(this->json_name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FieldDescriptorProto.json_name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 10, this->json_name(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.FieldDescriptorProto) return target; @@ -5722,12 +4844,7 @@ size_t FieldDescriptorProto::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.FieldDescriptorProto) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -5736,56 +4853,56 @@ size_t FieldDescriptorProto::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional string extendee = 2; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->extendee()); } // optional string type_name = 6; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->type_name()); } // optional string default_value = 7; if (cached_has_bits & 0x00000008u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->default_value()); } // optional string json_name = 10; if (cached_has_bits & 0x00000010u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->json_name()); } // optional .google.protobuf.FieldOptions options = 8; if (cached_has_bits & 0x00000020u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } // optional int32 number = 3; if (cached_has_bits & 0x00000040u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->number()); } // optional int32 oneof_index = 9; if (cached_has_bits & 0x00000080u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->oneof_index()); } @@ -5794,30 +4911,34 @@ size_t FieldDescriptorProto::ByteSizeLong() const { // optional .google.protobuf.FieldDescriptorProto.Label label = 4; if (cached_has_bits & 0x00000100u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->label()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->label()); } // optional .google.protobuf.FieldDescriptorProto.Type type = 5; if (cached_has_bits & 0x00000200u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->type()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->type()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void FieldDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) { +void FieldDescriptorProto::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.FieldDescriptorProto) GOOGLE_DCHECK_NE(&from, this); const FieldDescriptorProto* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.FieldDescriptorProto) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.FieldDescriptorProto) MergeFrom(*source); @@ -5828,7 +4949,7 @@ void FieldDescriptorProto::MergeFrom(const FieldDescriptorProto& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.FieldDescriptorProto) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = from._has_bits_[0]; @@ -5849,7 +4970,7 @@ void FieldDescriptorProto::MergeFrom(const FieldDescriptorProto& from) { set_json_name(from.json_name()); } if (cached_has_bits & 0x00000020u) { - mutable_options()->::google::protobuf::FieldOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::FieldOptions::MergeFrom(from.options()); } if (cached_has_bits & 0x00000040u) { number_ = from.number_; @@ -5870,7 +4991,7 @@ void FieldDescriptorProto::MergeFrom(const FieldDescriptorProto& from) { } } -void FieldDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) { +void FieldDescriptorProto::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.FieldDescriptorProto) if (&from == this) return; Clear(); @@ -5891,38 +5012,19 @@ bool FieldDescriptorProto::IsInitialized() const { return true; } -void FieldDescriptorProto::Swap(FieldDescriptorProto* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - FieldDescriptorProto* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void FieldDescriptorProto::UnsafeArenaSwap(FieldDescriptorProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void FieldDescriptorProto::InternalSwap(FieldDescriptorProto* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - extendee_.Swap(&other->extendee_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + extendee_.Swap(&other->extendee_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - type_name_.Swap(&other->type_name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + type_name_.Swap(&other->type_name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - default_value_.Swap(&other->default_value_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + default_value_.Swap(&other->default_value_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - json_name_.Swap(&other->json_name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + json_name_.Swap(&other->json_name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(options_, other->options_); swap(number_, other->number_); @@ -5931,36 +5033,36 @@ void FieldDescriptorProto::InternalSwap(FieldDescriptorProto* other) { swap(type_, other->type_); } -::google::protobuf::Metadata FieldDescriptorProto::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata FieldDescriptorProto::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void OneofDescriptorProto::InitAsDefaultInstance() { - ::google::protobuf::_OneofDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::OneofOptions*>( - ::google::protobuf::OneofOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_OneofDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::OneofOptions*>( + PROTOBUF_NAMESPACE_ID::OneofOptions::internal_default_instance()); } -class OneofDescriptorProto::HasBitSetters { +class OneofDescriptorProto::_Internal { public: - static void set_has_name(OneofDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static const ::google::protobuf::OneofOptions& options(const OneofDescriptorProto* msg); - static void set_has_options(OneofDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static const PROTOBUF_NAMESPACE_ID::OneofOptions& options(const OneofDescriptorProto* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -const ::google::protobuf::OneofOptions& -OneofDescriptorProto::HasBitSetters::options(const OneofDescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::OneofOptions& +OneofDescriptorProto::_Internal::options(const OneofDescriptorProto* msg) { return *msg->options_; } void OneofDescriptorProto::unsafe_arena_set_allocated_options( - ::google::protobuf::OneofOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::OneofOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -5971,46 +5073,40 @@ void OneofDescriptorProto::unsafe_arena_set_allocated_options( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.OneofDescriptorProto.options) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int OneofDescriptorProto::kNameFieldNumber; -const int OneofDescriptorProto::kOptionsFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - OneofDescriptorProto::OneofDescriptorProto() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.OneofDescriptorProto) } -OneofDescriptorProto::OneofDescriptorProto(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +OneofDescriptorProto::OneofDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.OneofDescriptorProto) } OneofDescriptorProto::OneofDescriptorProto(const OneofDescriptorProto& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name(), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name(), GetArenaNoVirtual()); } if (from.has_options()) { - options_ = new ::google::protobuf::OneofOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::OneofOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } // @@protoc_insertion_point(copy_constructor:google.protobuf.OneofDescriptorProto) } void OneofDescriptorProto::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - options_ = NULL; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + options_ = nullptr; } OneofDescriptorProto::~OneofDescriptorProto() { @@ -6019,8 +5115,8 @@ OneofDescriptorProto::~OneofDescriptorProto() { } void OneofDescriptorProto::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete options_; } @@ -6028,20 +5124,20 @@ void OneofDescriptorProto::ArenaDtor(void* object) { OneofDescriptorProto* _this = reinterpret_cast< OneofDescriptorProto* >(object); (void)_this; } -void OneofDescriptorProto::RegisterArenaDtor(::google::protobuf::Arena*) { +void OneofDescriptorProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void OneofDescriptorProto::SetCachedSize(int size) const { _cached_size_.Set(size); } const OneofDescriptorProto& OneofDescriptorProto::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void OneofDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.OneofDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -6051,7 +5147,7 @@ void OneofDescriptorProto::Clear() { name_.ClearNonDefaultToEmpty(); } if (cached_has_bits & 0x00000002u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } } @@ -6060,91 +5156,68 @@ void OneofDescriptorProto::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* OneofDescriptorProto::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* OneofDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.OneofDescriptorProto.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.OneofDescriptorProto.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.OneofOptions options = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::OneofOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool OneofDescriptorProto::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.OneofDescriptorProto) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.OneofDescriptorProto.name"); } else { goto handle_unusual; @@ -6154,8 +5227,8 @@ bool OneofDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.OneofOptions options = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -6168,7 +5241,7 @@ bool OneofDescriptorProto::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -6184,65 +5257,34 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void OneofDescriptorProto::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.OneofDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.OneofDescriptorProto.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // optional .google.protobuf.OneofOptions options = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 2, HasBitSetters::options(this), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.OneofDescriptorProto) -} - -::google::protobuf::uint8* OneofDescriptorProto::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* OneofDescriptorProto::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.OneofDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.OneofDescriptorProto.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // optional .google.protobuf.OneofOptions options = 2; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 2, HasBitSetters::options(this), deterministic, target); + 2, _Internal::options(this), target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.OneofDescriptorProto) return target; @@ -6252,12 +5294,7 @@ size_t OneofDescriptorProto::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.OneofDescriptorProto) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -6266,32 +5303,36 @@ size_t OneofDescriptorProto::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional .google.protobuf.OneofOptions options = 2; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void OneofDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) { +void OneofDescriptorProto::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.OneofDescriptorProto) GOOGLE_DCHECK_NE(&from, this); const OneofDescriptorProto* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.OneofDescriptorProto) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.OneofDescriptorProto) MergeFrom(*source); @@ -6302,7 +5343,7 @@ void OneofDescriptorProto::MergeFrom(const OneofDescriptorProto& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.OneofDescriptorProto) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = from._has_bits_[0]; @@ -6311,12 +5352,12 @@ void OneofDescriptorProto::MergeFrom(const OneofDescriptorProto& from) { set_name(from.name()); } if (cached_has_bits & 0x00000002u) { - mutable_options()->::google::protobuf::OneofOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::OneofOptions::MergeFrom(from.options()); } } } -void OneofDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) { +void OneofDescriptorProto::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.OneofDescriptorProto) if (&from == this) return; Clear(); @@ -6337,37 +5378,17 @@ bool OneofDescriptorProto::IsInitialized() const { return true; } -void OneofDescriptorProto::Swap(OneofDescriptorProto* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - OneofDescriptorProto* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void OneofDescriptorProto::UnsafeArenaSwap(OneofDescriptorProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void OneofDescriptorProto::InternalSwap(OneofDescriptorProto* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(options_, other->options_); } -::google::protobuf::Metadata OneofDescriptorProto::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata OneofDescriptorProto::GetMetadata() const { + return GetMetadataStatic(); } @@ -6375,36 +5396,32 @@ void OneofDescriptorProto::InternalSwap(OneofDescriptorProto* other) { void EnumDescriptorProto_EnumReservedRange::InitAsDefaultInstance() { } -class EnumDescriptorProto_EnumReservedRange::HasBitSetters { +class EnumDescriptorProto_EnumReservedRange::_Internal { public: - static void set_has_start(EnumDescriptorProto_EnumReservedRange* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_start(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_end(EnumDescriptorProto_EnumReservedRange* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_end(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int EnumDescriptorProto_EnumReservedRange::kStartFieldNumber; -const int EnumDescriptorProto_EnumReservedRange::kEndFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - EnumDescriptorProto_EnumReservedRange::EnumDescriptorProto_EnumReservedRange() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.EnumDescriptorProto.EnumReservedRange) } -EnumDescriptorProto_EnumReservedRange::EnumDescriptorProto_EnumReservedRange(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +EnumDescriptorProto_EnumReservedRange::EnumDescriptorProto_EnumReservedRange(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.EnumDescriptorProto.EnumReservedRange) } EnumDescriptorProto_EnumReservedRange::EnumDescriptorProto_EnumReservedRange(const EnumDescriptorProto_EnumReservedRange& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); ::memcpy(&start_, &from.start_, @@ -6425,27 +5442,27 @@ EnumDescriptorProto_EnumReservedRange::~EnumDescriptorProto_EnumReservedRange() } void EnumDescriptorProto_EnumReservedRange::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void EnumDescriptorProto_EnumReservedRange::ArenaDtor(void* object) { EnumDescriptorProto_EnumReservedRange* _this = reinterpret_cast< EnumDescriptorProto_EnumReservedRange* >(object); (void)_this; } -void EnumDescriptorProto_EnumReservedRange::RegisterArenaDtor(::google::protobuf::Arena*) { +void EnumDescriptorProto_EnumReservedRange::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void EnumDescriptorProto_EnumReservedRange::SetCachedSize(int size) const { _cached_size_.Set(size); } const EnumDescriptorProto_EnumReservedRange& EnumDescriptorProto_EnumReservedRange::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void EnumDescriptorProto_EnumReservedRange::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -6460,80 +5477,68 @@ void EnumDescriptorProto_EnumReservedRange::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* EnumDescriptorProto_EnumReservedRange::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* EnumDescriptorProto_EnumReservedRange::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional int32 start = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_start(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + _Internal::set_has_start(&has_bits); + start_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 end = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_end(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_end(&has_bits); + end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumDescriptorProto_EnumReservedRange::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional int32 start = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_start(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { + _Internal::set_has_start(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &start_))); } else { goto handle_unusual; @@ -6543,10 +5548,10 @@ bool EnumDescriptorProto_EnumReservedRange::MergePartialFromCodedStream( // optional int32 end = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_end(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_end(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &end_))); } else { goto handle_unusual; @@ -6559,7 +5564,7 @@ bool EnumDescriptorProto_EnumReservedRange::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -6575,51 +5580,28 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void EnumDescriptorProto_EnumReservedRange::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional int32 start = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(1, this->start(), output); - } - - // optional int32 end = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->end(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.EnumDescriptorProto.EnumReservedRange) -} - -::google::protobuf::uint8* EnumDescriptorProto_EnumReservedRange::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* EnumDescriptorProto_EnumReservedRange::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional int32 start = 1; if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(1, this->start(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(1, this->start(), target); } // optional int32 end = 2; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->end(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(2, this->end(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.EnumDescriptorProto.EnumReservedRange) return target; @@ -6629,12 +5611,7 @@ size_t EnumDescriptorProto_EnumReservedRange::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -6643,32 +5620,36 @@ size_t EnumDescriptorProto_EnumReservedRange::ByteSizeLong() const { // optional int32 start = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->start()); } // optional int32 end = 2; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->end()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void EnumDescriptorProto_EnumReservedRange::MergeFrom(const ::google::protobuf::Message& from) { +void EnumDescriptorProto_EnumReservedRange::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) GOOGLE_DCHECK_NE(&from, this); const EnumDescriptorProto_EnumReservedRange* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.EnumDescriptorProto.EnumReservedRange) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.EnumDescriptorProto.EnumReservedRange) MergeFrom(*source); @@ -6679,7 +5660,7 @@ void EnumDescriptorProto_EnumReservedRange::MergeFrom(const EnumDescriptorProto_ // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = from._has_bits_[0]; @@ -6694,7 +5675,7 @@ void EnumDescriptorProto_EnumReservedRange::MergeFrom(const EnumDescriptorProto_ } } -void EnumDescriptorProto_EnumReservedRange::CopyFrom(const ::google::protobuf::Message& from) { +void EnumDescriptorProto_EnumReservedRange::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) if (&from == this) return; Clear(); @@ -6712,25 +5693,6 @@ bool EnumDescriptorProto_EnumReservedRange::IsInitialized() const { return true; } -void EnumDescriptorProto_EnumReservedRange::Swap(EnumDescriptorProto_EnumReservedRange* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - EnumDescriptorProto_EnumReservedRange* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void EnumDescriptorProto_EnumReservedRange::UnsafeArenaSwap(EnumDescriptorProto_EnumReservedRange* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void EnumDescriptorProto_EnumReservedRange::InternalSwap(EnumDescriptorProto_EnumReservedRange* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -6739,36 +5701,36 @@ void EnumDescriptorProto_EnumReservedRange::InternalSwap(EnumDescriptorProto_Enu swap(end_, other->end_); } -::google::protobuf::Metadata EnumDescriptorProto_EnumReservedRange::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata EnumDescriptorProto_EnumReservedRange::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void EnumDescriptorProto::InitAsDefaultInstance() { - ::google::protobuf::_EnumDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::EnumOptions*>( - ::google::protobuf::EnumOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_EnumDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::EnumOptions*>( + PROTOBUF_NAMESPACE_ID::EnumOptions::internal_default_instance()); } -class EnumDescriptorProto::HasBitSetters { +class EnumDescriptorProto::_Internal { public: - static void set_has_name(EnumDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static const ::google::protobuf::EnumOptions& options(const EnumDescriptorProto* msg); - static void set_has_options(EnumDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static const PROTOBUF_NAMESPACE_ID::EnumOptions& options(const EnumDescriptorProto* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -const ::google::protobuf::EnumOptions& -EnumDescriptorProto::HasBitSetters::options(const EnumDescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::EnumOptions& +EnumDescriptorProto::_Internal::options(const EnumDescriptorProto* msg) { return *msg->options_; } void EnumDescriptorProto::unsafe_arena_set_allocated_options( - ::google::protobuf::EnumOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::EnumOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -6779,21 +5741,13 @@ void EnumDescriptorProto::unsafe_arena_set_allocated_options( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.EnumDescriptorProto.options) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int EnumDescriptorProto::kNameFieldNumber; -const int EnumDescriptorProto::kValueFieldNumber; -const int EnumDescriptorProto::kOptionsFieldNumber; -const int EnumDescriptorProto::kReservedRangeFieldNumber; -const int EnumDescriptorProto::kReservedNameFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - EnumDescriptorProto::EnumDescriptorProto() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.EnumDescriptorProto) } -EnumDescriptorProto::EnumDescriptorProto(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +EnumDescriptorProto::EnumDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), value_(arena), reserved_range_(arena), @@ -6803,31 +5757,30 @@ EnumDescriptorProto::EnumDescriptorProto(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.EnumDescriptorProto) } EnumDescriptorProto::EnumDescriptorProto(const EnumDescriptorProto& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), value_(from.value_), reserved_range_(from.reserved_range_), reserved_name_(from.reserved_name_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name(), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name(), GetArenaNoVirtual()); } if (from.has_options()) { - options_ = new ::google::protobuf::EnumOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::EnumOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } // @@protoc_insertion_point(copy_constructor:google.protobuf.EnumDescriptorProto) } void EnumDescriptorProto::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - options_ = NULL; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + options_ = nullptr; } EnumDescriptorProto::~EnumDescriptorProto() { @@ -6836,8 +5789,8 @@ EnumDescriptorProto::~EnumDescriptorProto() { } void EnumDescriptorProto::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete options_; } @@ -6845,20 +5798,20 @@ void EnumDescriptorProto::ArenaDtor(void* object) { EnumDescriptorProto* _this = reinterpret_cast< EnumDescriptorProto* >(object); (void)_this; } -void EnumDescriptorProto::RegisterArenaDtor(::google::protobuf::Arena*) { +void EnumDescriptorProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void EnumDescriptorProto::SetCachedSize(int size) const { _cached_size_.Set(size); } const EnumDescriptorProto& EnumDescriptorProto::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void EnumDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -6871,7 +5824,7 @@ void EnumDescriptorProto::Clear() { name_.ClearNonDefaultToEmpty(); } if (cached_has_bits & 0x00000002u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } } @@ -6880,142 +5833,104 @@ void EnumDescriptorProto::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* EnumDescriptorProto::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* EnumDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.EnumDescriptorProto.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.EnumDescriptorProto.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.EnumValueDescriptorProto value = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::EnumValueDescriptorProto::_InternalParse; - object = msg->add_value(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 18 && (ptr += 1)); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_value(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 18); + } else goto handle_unusual; + continue; // optional .google.protobuf.EnumOptions options = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::EnumOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.EnumDescriptorProto.EnumReservedRange reserved_range = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::EnumDescriptorProto_EnumReservedRange::_InternalParse; - object = msg->add_reserved_range(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 34 && (ptr += 1)); - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_reserved_range(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 34); + } else goto handle_unusual; + continue; // repeated string reserved_name = 5; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.EnumDescriptorProto.reserved_name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->add_reserved_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 42 && (ptr += 1)); - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 42)) { + ptr -= 1; + do { + ptr += 1; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_reserved_name(), ptr, ctx, "google.protobuf.EnumDescriptorProto.reserved_name"); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 42); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumDescriptorProto::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.EnumDescriptorProto) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.EnumDescriptorProto.name"); } else { goto handle_unusual; @@ -7025,8 +5940,8 @@ bool EnumDescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.EnumValueDescriptorProto value = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_value())); } else { goto handle_unusual; @@ -7036,8 +5951,8 @@ bool EnumDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.EnumOptions options = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -7047,8 +5962,8 @@ bool EnumDescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.EnumDescriptorProto.EnumReservedRange reserved_range = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (34 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_reserved_range())); } else { goto handle_unusual; @@ -7058,13 +5973,13 @@ bool EnumDescriptorProto::MergePartialFromCodedStream( // repeated string reserved_name = 5; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (42 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (42 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->add_reserved_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->reserved_name(this->reserved_name_size() - 1).data(), static_cast(this->reserved_name(this->reserved_name_size() - 1).length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.EnumDescriptorProto.reserved_name"); } else { goto handle_unusual; @@ -7077,7 +5992,7 @@ bool EnumDescriptorProto::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -7093,119 +6008,61 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void EnumDescriptorProto::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.EnumDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.EnumDescriptorProto.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // repeated .google.protobuf.EnumValueDescriptorProto value = 2; - for (unsigned int i = 0, - n = static_cast(this->value_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 2, - this->value(static_cast(i)), - output); - } - - // optional .google.protobuf.EnumOptions options = 3; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 3, HasBitSetters::options(this), output); - } - - // repeated .google.protobuf.EnumDescriptorProto.EnumReservedRange reserved_range = 4; - for (unsigned int i = 0, - n = static_cast(this->reserved_range_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 4, - this->reserved_range(static_cast(i)), - output); - } - - // repeated string reserved_name = 5; - for (int i = 0, n = this->reserved_name_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->reserved_name(i).data(), static_cast(this->reserved_name(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.EnumDescriptorProto.reserved_name"); - ::google::protobuf::internal::WireFormatLite::WriteString( - 5, this->reserved_name(i), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.EnumDescriptorProto) -} - -::google::protobuf::uint8* EnumDescriptorProto::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* EnumDescriptorProto::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.EnumDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.EnumDescriptorProto.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // repeated .google.protobuf.EnumValueDescriptorProto value = 2; - for (unsigned int i = 0, - n = static_cast(this->value_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 2, this->value(static_cast(i)), deterministic, target); + for (auto it = this->value().pointer_begin(), + end = this->value().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(2, **it, target, stream); } // optional .google.protobuf.EnumOptions options = 3; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 3, HasBitSetters::options(this), deterministic, target); + 3, _Internal::options(this), target, stream); } // repeated .google.protobuf.EnumDescriptorProto.EnumReservedRange reserved_range = 4; - for (unsigned int i = 0, - n = static_cast(this->reserved_range_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 4, this->reserved_range(static_cast(i)), deterministic, target); + for (auto it = this->reserved_range().pointer_begin(), + end = this->reserved_range().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(4, **it, target, stream); } // repeated string reserved_name = 5; - for (int i = 0, n = this->reserved_name_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->reserved_name(i).data(), static_cast(this->reserved_name(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + for (auto it = this->reserved_name().pointer_begin(), + end = this->reserved_name().pointer_end(); it < end; ++it) { + const auto& s = **it; + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( + s.data(), static_cast(s.length()), + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.EnumDescriptorProto.reserved_name"); - target = ::google::protobuf::internal::WireFormatLite:: - WriteStringToArray(5, this->reserved_name(i), target); + target = stream->WriteString(5, s, target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.EnumDescriptorProto) return target; @@ -7215,12 +6072,7 @@ size_t EnumDescriptorProto::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.EnumDescriptorProto) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -7230,7 +6082,7 @@ size_t EnumDescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->value(static_cast(i))); } } @@ -7241,16 +6093,16 @@ size_t EnumDescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->reserved_range(static_cast(i))); } } // repeated string reserved_name = 5; total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->reserved_name_size()); + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->reserved_name_size()); for (int i = 0, n = this->reserved_name_size(); i < n; i++) { - total_size += ::google::protobuf::internal::WireFormatLite::StringSize( + total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->reserved_name(i)); } @@ -7259,32 +6111,36 @@ size_t EnumDescriptorProto::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional .google.protobuf.EnumOptions options = 3; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void EnumDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) { +void EnumDescriptorProto::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.EnumDescriptorProto) GOOGLE_DCHECK_NE(&from, this); const EnumDescriptorProto* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.EnumDescriptorProto) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.EnumDescriptorProto) MergeFrom(*source); @@ -7295,7 +6151,7 @@ void EnumDescriptorProto::MergeFrom(const EnumDescriptorProto& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.EnumDescriptorProto) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; value_.MergeFrom(from.value_); @@ -7307,12 +6163,12 @@ void EnumDescriptorProto::MergeFrom(const EnumDescriptorProto& from) { set_name(from.name()); } if (cached_has_bits & 0x00000002u) { - mutable_options()->::google::protobuf::EnumOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::EnumOptions::MergeFrom(from.options()); } } } -void EnumDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) { +void EnumDescriptorProto::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.EnumDescriptorProto) if (&from == this) return; Clear(); @@ -7327,32 +6183,13 @@ void EnumDescriptorProto::CopyFrom(const EnumDescriptorProto& from) { } bool EnumDescriptorProto::IsInitialized() const { - if (!::google::protobuf::internal::AllAreInitialized(this->value())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->value())) return false; if (has_options()) { if (!this->options_->IsInitialized()) return false; } return true; } -void EnumDescriptorProto::Swap(EnumDescriptorProto* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - EnumDescriptorProto* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void EnumDescriptorProto::UnsafeArenaSwap(EnumDescriptorProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void EnumDescriptorProto::InternalSwap(EnumDescriptorProto* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -7360,44 +6197,44 @@ void EnumDescriptorProto::InternalSwap(EnumDescriptorProto* other) { CastToBase(&value_)->InternalSwap(CastToBase(&other->value_)); CastToBase(&reserved_range_)->InternalSwap(CastToBase(&other->reserved_range_)); reserved_name_.InternalSwap(CastToBase(&other->reserved_name_)); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(options_, other->options_); } -::google::protobuf::Metadata EnumDescriptorProto::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata EnumDescriptorProto::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void EnumValueDescriptorProto::InitAsDefaultInstance() { - ::google::protobuf::_EnumValueDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::EnumValueOptions*>( - ::google::protobuf::EnumValueOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_EnumValueDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::EnumValueOptions*>( + PROTOBUF_NAMESPACE_ID::EnumValueOptions::internal_default_instance()); } -class EnumValueDescriptorProto::HasBitSetters { +class EnumValueDescriptorProto::_Internal { public: - static void set_has_name(EnumValueDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_number(EnumValueDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_number(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static const ::google::protobuf::EnumValueOptions& options(const EnumValueDescriptorProto* msg); - static void set_has_options(EnumValueDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static const PROTOBUF_NAMESPACE_ID::EnumValueOptions& options(const EnumValueDescriptorProto* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -const ::google::protobuf::EnumValueOptions& -EnumValueDescriptorProto::HasBitSetters::options(const EnumValueDescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::EnumValueOptions& +EnumValueDescriptorProto::_Internal::options(const EnumValueDescriptorProto* msg) { return *msg->options_; } void EnumValueDescriptorProto::unsafe_arena_set_allocated_options( - ::google::protobuf::EnumValueOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::EnumValueOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -7408,47 +6245,40 @@ void EnumValueDescriptorProto::unsafe_arena_set_allocated_options( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.EnumValueDescriptorProto.options) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int EnumValueDescriptorProto::kNameFieldNumber; -const int EnumValueDescriptorProto::kNumberFieldNumber; -const int EnumValueDescriptorProto::kOptionsFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - EnumValueDescriptorProto::EnumValueDescriptorProto() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.EnumValueDescriptorProto) } -EnumValueDescriptorProto::EnumValueDescriptorProto(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +EnumValueDescriptorProto::EnumValueDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.EnumValueDescriptorProto) } EnumValueDescriptorProto::EnumValueDescriptorProto(const EnumValueDescriptorProto& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name(), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name(), GetArenaNoVirtual()); } if (from.has_options()) { - options_ = new ::google::protobuf::EnumValueOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::EnumValueOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } number_ = from.number_; // @@protoc_insertion_point(copy_constructor:google.protobuf.EnumValueDescriptorProto) } void EnumValueDescriptorProto::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&options_, 0, static_cast( reinterpret_cast(&number_) - reinterpret_cast(&options_)) + sizeof(number_)); @@ -7460,8 +6290,8 @@ EnumValueDescriptorProto::~EnumValueDescriptorProto() { } void EnumValueDescriptorProto::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete options_; } @@ -7469,20 +6299,20 @@ void EnumValueDescriptorProto::ArenaDtor(void* object) { EnumValueDescriptorProto* _this = reinterpret_cast< EnumValueDescriptorProto* >(object); (void)_this; } -void EnumValueDescriptorProto::RegisterArenaDtor(::google::protobuf::Arena*) { +void EnumValueDescriptorProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void EnumValueDescriptorProto::SetCachedSize(int size) const { _cached_size_.Set(size); } const EnumValueDescriptorProto& EnumValueDescriptorProto::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void EnumValueDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumValueDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -7492,7 +6322,7 @@ void EnumValueDescriptorProto::Clear() { name_.ClearNonDefaultToEmpty(); } if (cached_has_bits & 0x00000002u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } } @@ -7502,101 +6332,76 @@ void EnumValueDescriptorProto::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* EnumValueDescriptorProto::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* EnumValueDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.EnumValueDescriptorProto.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.EnumValueDescriptorProto.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 number = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_number(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_number(&has_bits); + number_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.EnumValueOptions options = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::EnumValueOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumValueDescriptorProto::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.EnumValueDescriptorProto) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.EnumValueDescriptorProto.name"); } else { goto handle_unusual; @@ -7606,10 +6411,10 @@ bool EnumValueDescriptorProto::MergePartialFromCodedStream( // optional int32 number = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_number(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_number(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &number_))); } else { goto handle_unusual; @@ -7619,8 +6424,8 @@ bool EnumValueDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.EnumValueOptions options = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -7633,7 +6438,7 @@ bool EnumValueDescriptorProto::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -7649,75 +6454,40 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void EnumValueDescriptorProto::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.EnumValueDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.EnumValueDescriptorProto.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // optional int32 number = 2; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->number(), output); - } - - // optional .google.protobuf.EnumValueOptions options = 3; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 3, HasBitSetters::options(this), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.EnumValueDescriptorProto) -} - -::google::protobuf::uint8* EnumValueDescriptorProto::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* EnumValueDescriptorProto::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.EnumValueDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.EnumValueDescriptorProto.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // optional int32 number = 2; if (cached_has_bits & 0x00000004u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->number(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(2, this->number(), target); } // optional .google.protobuf.EnumValueOptions options = 3; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 3, HasBitSetters::options(this), deterministic, target); + 3, _Internal::options(this), target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.EnumValueDescriptorProto) return target; @@ -7727,12 +6497,7 @@ size_t EnumValueDescriptorProto::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.EnumValueDescriptorProto) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -7741,39 +6506,43 @@ size_t EnumValueDescriptorProto::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional .google.protobuf.EnumValueOptions options = 3; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } // optional int32 number = 2; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->number()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void EnumValueDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) { +void EnumValueDescriptorProto::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.EnumValueDescriptorProto) GOOGLE_DCHECK_NE(&from, this); const EnumValueDescriptorProto* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.EnumValueDescriptorProto) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.EnumValueDescriptorProto) MergeFrom(*source); @@ -7784,7 +6553,7 @@ void EnumValueDescriptorProto::MergeFrom(const EnumValueDescriptorProto& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.EnumValueDescriptorProto) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = from._has_bits_[0]; @@ -7793,7 +6562,7 @@ void EnumValueDescriptorProto::MergeFrom(const EnumValueDescriptorProto& from) { set_name(from.name()); } if (cached_has_bits & 0x00000002u) { - mutable_options()->::google::protobuf::EnumValueOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::EnumValueOptions::MergeFrom(from.options()); } if (cached_has_bits & 0x00000004u) { number_ = from.number_; @@ -7802,7 +6571,7 @@ void EnumValueDescriptorProto::MergeFrom(const EnumValueDescriptorProto& from) { } } -void EnumValueDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) { +void EnumValueDescriptorProto::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.EnumValueDescriptorProto) if (&from == this) return; Clear(); @@ -7823,65 +6592,46 @@ bool EnumValueDescriptorProto::IsInitialized() const { return true; } -void EnumValueDescriptorProto::Swap(EnumValueDescriptorProto* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - EnumValueDescriptorProto* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void EnumValueDescriptorProto::UnsafeArenaSwap(EnumValueDescriptorProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void EnumValueDescriptorProto::InternalSwap(EnumValueDescriptorProto* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(options_, other->options_); swap(number_, other->number_); } -::google::protobuf::Metadata EnumValueDescriptorProto::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata EnumValueDescriptorProto::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void ServiceDescriptorProto::InitAsDefaultInstance() { - ::google::protobuf::_ServiceDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::ServiceOptions*>( - ::google::protobuf::ServiceOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_ServiceDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::ServiceOptions*>( + PROTOBUF_NAMESPACE_ID::ServiceOptions::internal_default_instance()); } -class ServiceDescriptorProto::HasBitSetters { +class ServiceDescriptorProto::_Internal { public: - static void set_has_name(ServiceDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static const ::google::protobuf::ServiceOptions& options(const ServiceDescriptorProto* msg); - static void set_has_options(ServiceDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static const PROTOBUF_NAMESPACE_ID::ServiceOptions& options(const ServiceDescriptorProto* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -const ::google::protobuf::ServiceOptions& -ServiceDescriptorProto::HasBitSetters::options(const ServiceDescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::ServiceOptions& +ServiceDescriptorProto::_Internal::options(const ServiceDescriptorProto* msg) { return *msg->options_; } void ServiceDescriptorProto::unsafe_arena_set_allocated_options( - ::google::protobuf::ServiceOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::ServiceOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -7892,19 +6642,13 @@ void ServiceDescriptorProto::unsafe_arena_set_allocated_options( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.ServiceDescriptorProto.options) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int ServiceDescriptorProto::kNameFieldNumber; -const int ServiceDescriptorProto::kMethodFieldNumber; -const int ServiceDescriptorProto::kOptionsFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - ServiceDescriptorProto::ServiceDescriptorProto() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.ServiceDescriptorProto) } -ServiceDescriptorProto::ServiceDescriptorProto(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +ServiceDescriptorProto::ServiceDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), method_(arena) { SharedCtor(); @@ -7912,29 +6656,28 @@ ServiceDescriptorProto::ServiceDescriptorProto(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.ServiceDescriptorProto) } ServiceDescriptorProto::ServiceDescriptorProto(const ServiceDescriptorProto& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), method_(from.method_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name(), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name(), GetArenaNoVirtual()); } if (from.has_options()) { - options_ = new ::google::protobuf::ServiceOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::ServiceOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } // @@protoc_insertion_point(copy_constructor:google.protobuf.ServiceDescriptorProto) } void ServiceDescriptorProto::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - options_ = NULL; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + options_ = nullptr; } ServiceDescriptorProto::~ServiceDescriptorProto() { @@ -7943,8 +6686,8 @@ ServiceDescriptorProto::~ServiceDescriptorProto() { } void ServiceDescriptorProto::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete options_; } @@ -7952,20 +6695,20 @@ void ServiceDescriptorProto::ArenaDtor(void* object) { ServiceDescriptorProto* _this = reinterpret_cast< ServiceDescriptorProto* >(object); (void)_this; } -void ServiceDescriptorProto::RegisterArenaDtor(::google::protobuf::Arena*) { +void ServiceDescriptorProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void ServiceDescriptorProto::SetCachedSize(int size) const { _cached_size_.Set(size); } const ServiceDescriptorProto& ServiceDescriptorProto::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void ServiceDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.ServiceDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -7976,7 +6719,7 @@ void ServiceDescriptorProto::Clear() { name_.ClearNonDefaultToEmpty(); } if (cached_has_bits & 0x00000002u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } } @@ -7985,107 +6728,80 @@ void ServiceDescriptorProto::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* ServiceDescriptorProto::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* ServiceDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.ServiceDescriptorProto.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.ServiceDescriptorProto.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.MethodDescriptorProto method = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::MethodDescriptorProto::_InternalParse; - object = msg->add_method(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 18 && (ptr += 1)); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_method(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 18); + } else goto handle_unusual; + continue; // optional .google.protobuf.ServiceOptions options = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::ServiceOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ServiceDescriptorProto::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.ServiceDescriptorProto) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.ServiceDescriptorProto.name"); } else { goto handle_unusual; @@ -8095,8 +6811,8 @@ bool ServiceDescriptorProto::MergePartialFromCodedStream( // repeated .google.protobuf.MethodDescriptorProto method = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_method())); } else { goto handle_unusual; @@ -8106,8 +6822,8 @@ bool ServiceDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.ServiceOptions options = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -8120,7 +6836,7 @@ bool ServiceDescriptorProto::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -8136,82 +6852,42 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void ServiceDescriptorProto::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.ServiceDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.ServiceDescriptorProto.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // repeated .google.protobuf.MethodDescriptorProto method = 2; - for (unsigned int i = 0, - n = static_cast(this->method_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 2, - this->method(static_cast(i)), - output); - } - - // optional .google.protobuf.ServiceOptions options = 3; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 3, HasBitSetters::options(this), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.ServiceDescriptorProto) -} - -::google::protobuf::uint8* ServiceDescriptorProto::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* ServiceDescriptorProto::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.ServiceDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.ServiceDescriptorProto.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // repeated .google.protobuf.MethodDescriptorProto method = 2; - for (unsigned int i = 0, - n = static_cast(this->method_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 2, this->method(static_cast(i)), deterministic, target); + for (auto it = this->method().pointer_begin(), + end = this->method().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(2, **it, target, stream); } // optional .google.protobuf.ServiceOptions options = 3; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 3, HasBitSetters::options(this), deterministic, target); + 3, _Internal::options(this), target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.ServiceDescriptorProto) return target; @@ -8221,12 +6897,7 @@ size_t ServiceDescriptorProto::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.ServiceDescriptorProto) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -8236,7 +6907,7 @@ size_t ServiceDescriptorProto::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->method(static_cast(i))); } } @@ -8246,32 +6917,36 @@ size_t ServiceDescriptorProto::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional .google.protobuf.ServiceOptions options = 3; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void ServiceDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) { +void ServiceDescriptorProto::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.ServiceDescriptorProto) GOOGLE_DCHECK_NE(&from, this); const ServiceDescriptorProto* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.ServiceDescriptorProto) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.ServiceDescriptorProto) MergeFrom(*source); @@ -8282,7 +6957,7 @@ void ServiceDescriptorProto::MergeFrom(const ServiceDescriptorProto& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.ServiceDescriptorProto) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; method_.MergeFrom(from.method_); @@ -8292,12 +6967,12 @@ void ServiceDescriptorProto::MergeFrom(const ServiceDescriptorProto& from) { set_name(from.name()); } if (cached_has_bits & 0x00000002u) { - mutable_options()->::google::protobuf::ServiceOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::ServiceOptions::MergeFrom(from.options()); } } } -void ServiceDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) { +void ServiceDescriptorProto::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.ServiceDescriptorProto) if (&from == this) return; Clear(); @@ -8312,84 +6987,65 @@ void ServiceDescriptorProto::CopyFrom(const ServiceDescriptorProto& from) { } bool ServiceDescriptorProto::IsInitialized() const { - if (!::google::protobuf::internal::AllAreInitialized(this->method())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->method())) return false; if (has_options()) { if (!this->options_->IsInitialized()) return false; } return true; } -void ServiceDescriptorProto::Swap(ServiceDescriptorProto* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - ServiceDescriptorProto* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void ServiceDescriptorProto::UnsafeArenaSwap(ServiceDescriptorProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void ServiceDescriptorProto::InternalSwap(ServiceDescriptorProto* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); CastToBase(&method_)->InternalSwap(CastToBase(&other->method_)); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(options_, other->options_); } -::google::protobuf::Metadata ServiceDescriptorProto::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata ServiceDescriptorProto::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void MethodDescriptorProto::InitAsDefaultInstance() { - ::google::protobuf::_MethodDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< ::google::protobuf::MethodOptions*>( - ::google::protobuf::MethodOptions::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_MethodDescriptorProto_default_instance_._instance.get_mutable()->options_ = const_cast< PROTOBUF_NAMESPACE_ID::MethodOptions*>( + PROTOBUF_NAMESPACE_ID::MethodOptions::internal_default_instance()); } -class MethodDescriptorProto::HasBitSetters { +class MethodDescriptorProto::_Internal { public: - static void set_has_name(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_input_type(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_input_type(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_output_type(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_output_type(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static const ::google::protobuf::MethodOptions& options(const MethodDescriptorProto* msg); - static void set_has_options(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static const PROTOBUF_NAMESPACE_ID::MethodOptions& options(const MethodDescriptorProto* msg); + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_client_streaming(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_client_streaming(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static void set_has_server_streaming(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_server_streaming(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } }; -const ::google::protobuf::MethodOptions& -MethodDescriptorProto::HasBitSetters::options(const MethodDescriptorProto* msg) { +const PROTOBUF_NAMESPACE_ID::MethodOptions& +MethodDescriptorProto::_Internal::options(const MethodDescriptorProto* msg) { return *msg->options_; } void MethodDescriptorProto::unsafe_arena_set_allocated_options( - ::google::protobuf::MethodOptions* options) { - if (GetArenaNoVirtual() == NULL) { + PROTOBUF_NAMESPACE_ID::MethodOptions* options) { + if (GetArenaNoVirtual() == nullptr) { delete options_; } options_ = options; @@ -8400,51 +7056,42 @@ void MethodDescriptorProto::unsafe_arena_set_allocated_options( } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.MethodDescriptorProto.options) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int MethodDescriptorProto::kNameFieldNumber; -const int MethodDescriptorProto::kInputTypeFieldNumber; -const int MethodDescriptorProto::kOutputTypeFieldNumber; -const int MethodDescriptorProto::kOptionsFieldNumber; -const int MethodDescriptorProto::kClientStreamingFieldNumber; -const int MethodDescriptorProto::kServerStreamingFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - MethodDescriptorProto::MethodDescriptorProto() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.MethodDescriptorProto) } -MethodDescriptorProto::MethodDescriptorProto(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +MethodDescriptorProto::MethodDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.MethodDescriptorProto) } MethodDescriptorProto::MethodDescriptorProto(const MethodDescriptorProto& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name()) { - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name(), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name(), GetArenaNoVirtual()); } - input_type_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + input_type_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_input_type()) { - input_type_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.input_type(), + input_type_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.input_type(), GetArenaNoVirtual()); } - output_type_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + output_type_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_output_type()) { - output_type_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.output_type(), + output_type_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.output_type(), GetArenaNoVirtual()); } if (from.has_options()) { - options_ = new ::google::protobuf::MethodOptions(*from.options_); + options_ = new PROTOBUF_NAMESPACE_ID::MethodOptions(*from.options_); } else { - options_ = NULL; + options_ = nullptr; } ::memcpy(&client_streaming_, &from.client_streaming_, static_cast(reinterpret_cast(&server_streaming_) - @@ -8453,11 +7100,10 @@ MethodDescriptorProto::MethodDescriptorProto(const MethodDescriptorProto& from) } void MethodDescriptorProto::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - input_type_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - output_type_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + input_type_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + output_type_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&options_, 0, static_cast( reinterpret_cast(&server_streaming_) - reinterpret_cast(&options_)) + sizeof(server_streaming_)); @@ -8469,10 +7115,10 @@ MethodDescriptorProto::~MethodDescriptorProto() { } void MethodDescriptorProto::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - input_type_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - output_type_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + input_type_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + output_type_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (this != internal_default_instance()) delete options_; } @@ -8480,20 +7126,20 @@ void MethodDescriptorProto::ArenaDtor(void* object) { MethodDescriptorProto* _this = reinterpret_cast< MethodDescriptorProto* >(object); (void)_this; } -void MethodDescriptorProto::RegisterArenaDtor(::google::protobuf::Arena*) { +void MethodDescriptorProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void MethodDescriptorProto::SetCachedSize(int size) const { _cached_size_.Set(size); } const MethodDescriptorProto& MethodDescriptorProto::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void MethodDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.MethodDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -8509,7 +7155,7 @@ void MethodDescriptorProto::Clear() { output_type_.ClearNonDefaultToEmpty(); } if (cached_has_bits & 0x00000008u) { - GOOGLE_DCHECK(options_ != NULL); + GOOGLE_DCHECK(options_ != nullptr); options_->Clear(); } } @@ -8521,143 +7167,98 @@ void MethodDescriptorProto::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* MethodDescriptorProto::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* MethodDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.MethodDescriptorProto.name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.MethodDescriptorProto.name"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string input_type = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.MethodDescriptorProto.input_type"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_input_type(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_input_type(), ptr, ctx, "google.protobuf.MethodDescriptorProto.input_type"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string output_type = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.MethodDescriptorProto.output_type"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_output_type(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_output_type(), ptr, ctx, "google.protobuf.MethodDescriptorProto.output_type"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.MethodOptions options = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::MethodOptions::_InternalParse; - object = msg->mutable_options(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr = ctx->ParseMessage(mutable_options(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool client_streaming = 5 [default = false]; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 40) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_client_streaming(value); - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 40)) { + _Internal::set_has_client_streaming(&has_bits); + client_streaming_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool server_streaming = 6 [default = false]; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 48) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_server_streaming(value); - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 48)) { + _Internal::set_has_server_streaming(&has_bits); + server_streaming_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool MethodDescriptorProto::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.MethodDescriptorProto) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.MethodDescriptorProto.name"); } else { goto handle_unusual; @@ -8667,12 +7268,12 @@ bool MethodDescriptorProto::MergePartialFromCodedStream( // optional string input_type = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_input_type())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->input_type().data(), static_cast(this->input_type().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.MethodDescriptorProto.input_type"); } else { goto handle_unusual; @@ -8682,12 +7283,12 @@ bool MethodDescriptorProto::MergePartialFromCodedStream( // optional string output_type = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_output_type())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->output_type().data(), static_cast(this->output_type().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.MethodDescriptorProto.output_type"); } else { goto handle_unusual; @@ -8697,8 +7298,8 @@ bool MethodDescriptorProto::MergePartialFromCodedStream( // optional .google.protobuf.MethodOptions options = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (34 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_options())); } else { goto handle_unusual; @@ -8708,10 +7309,10 @@ bool MethodDescriptorProto::MergePartialFromCodedStream( // optional bool client_streaming = 5 [default = false]; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (40 & 0xFF)) { - HasBitSetters::set_has_client_streaming(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (40 & 0xFF)) { + _Internal::set_has_client_streaming(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &client_streaming_))); } else { goto handle_unusual; @@ -8721,10 +7322,10 @@ bool MethodDescriptorProto::MergePartialFromCodedStream( // optional bool server_streaming = 6 [default = false]; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (48 & 0xFF)) { - HasBitSetters::set_has_server_streaming(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (48 & 0xFF)) { + _Internal::set_has_server_streaming(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &server_streaming_))); } else { goto handle_unusual; @@ -8737,7 +7338,7 @@ bool MethodDescriptorProto::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -8753,127 +7354,66 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void MethodDescriptorProto::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.MethodDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.MethodDescriptorProto.name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name(), output); - } - - // optional string input_type = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->input_type().data(), static_cast(this->input_type().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.MethodDescriptorProto.input_type"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 2, this->input_type(), output); - } - - // optional string output_type = 3; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->output_type().data(), static_cast(this->output_type().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.MethodDescriptorProto.output_type"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 3, this->output_type(), output); - } - - // optional .google.protobuf.MethodOptions options = 4; - if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 4, HasBitSetters::options(this), output); - } - - // optional bool client_streaming = 5 [default = false]; - if (cached_has_bits & 0x00000010u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(5, this->client_streaming(), output); - } - - // optional bool server_streaming = 6 [default = false]; - if (cached_has_bits & 0x00000020u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(6, this->server_streaming(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.MethodDescriptorProto) -} - -::google::protobuf::uint8* MethodDescriptorProto::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* MethodDescriptorProto::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.MethodDescriptorProto) - ::google::protobuf::uint32 cached_has_bits = 0; + ::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) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name().data(), static_cast(this->name().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.MethodDescriptorProto.name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name(), target); } // optional string input_type = 2; if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->input_type().data(), static_cast(this->input_type().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.MethodDescriptorProto.input_type"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 2, this->input_type(), target); } // optional string output_type = 3; if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->output_type().data(), static_cast(this->output_type().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.MethodDescriptorProto.output_type"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 3, this->output_type(), target); } // optional .google.protobuf.MethodOptions options = 4; if (cached_has_bits & 0x00000008u) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 4, HasBitSetters::options(this), deterministic, target); + 4, _Internal::options(this), target, stream); } // optional bool client_streaming = 5 [default = false]; if (cached_has_bits & 0x00000010u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(5, this->client_streaming(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(5, this->client_streaming(), target); } // optional bool server_streaming = 6 [default = false]; if (cached_has_bits & 0x00000020u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(6, this->server_streaming(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(6, this->server_streaming(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.MethodDescriptorProto) return target; @@ -8883,12 +7423,7 @@ size_t MethodDescriptorProto::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.MethodDescriptorProto) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -8897,28 +7432,28 @@ size_t MethodDescriptorProto::ByteSizeLong() const { // optional string name = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name()); } // optional string input_type = 2; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->input_type()); } // optional string output_type = 3; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->output_type()); } // optional .google.protobuf.MethodOptions options = 4; if (cached_has_bits & 0x00000008u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *options_); } @@ -8933,20 +7468,24 @@ size_t MethodDescriptorProto::ByteSizeLong() const { } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void MethodDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) { +void MethodDescriptorProto::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.MethodDescriptorProto) GOOGLE_DCHECK_NE(&from, this); const MethodDescriptorProto* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.MethodDescriptorProto) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.MethodDescriptorProto) MergeFrom(*source); @@ -8957,7 +7496,7 @@ void MethodDescriptorProto::MergeFrom(const MethodDescriptorProto& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.MethodDescriptorProto) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = from._has_bits_[0]; @@ -8972,7 +7511,7 @@ void MethodDescriptorProto::MergeFrom(const MethodDescriptorProto& from) { set_output_type(from.output_type()); } if (cached_has_bits & 0x00000008u) { - mutable_options()->::google::protobuf::MethodOptions::MergeFrom(from.options()); + mutable_options()->PROTOBUF_NAMESPACE_ID::MethodOptions::MergeFrom(from.options()); } if (cached_has_bits & 0x00000010u) { client_streaming_ = from.client_streaming_; @@ -8984,7 +7523,7 @@ void MethodDescriptorProto::MergeFrom(const MethodDescriptorProto& from) { } } -void MethodDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) { +void MethodDescriptorProto::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.MethodDescriptorProto) if (&from == this) return; Clear(); @@ -9005,43 +7544,23 @@ bool MethodDescriptorProto::IsInitialized() const { return true; } -void MethodDescriptorProto::Swap(MethodDescriptorProto* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - MethodDescriptorProto* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void MethodDescriptorProto::UnsafeArenaSwap(MethodDescriptorProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void MethodDescriptorProto::InternalSwap(MethodDescriptorProto* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); - name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - input_type_.Swap(&other->input_type_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + input_type_.Swap(&other->input_type_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - output_type_.Swap(&other->output_type_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + output_type_.Swap(&other->output_type_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(options_, other->options_); swap(client_streaming_, other->client_streaming_); swap(server_streaming_, other->server_streaming_); } -::google::protobuf::Metadata MethodDescriptorProto::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata MethodDescriptorProto::GetMetadata() const { + return GetMetadataStatic(); } @@ -9049,101 +7568,78 @@ void MethodDescriptorProto::InternalSwap(MethodDescriptorProto* other) { void FileOptions::InitAsDefaultInstance() { } -class FileOptions::HasBitSetters { +class FileOptions::_Internal { public: - static void set_has_java_package(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_java_package(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_java_outer_classname(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_java_outer_classname(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_java_multiple_files(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000400u; + static void set_has_java_multiple_files(HasBits* has_bits) { + (*has_bits)[0] |= 1024u; } - static void set_has_java_generate_equals_and_hash(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000800u; + static void set_has_java_generate_equals_and_hash(HasBits* has_bits) { + (*has_bits)[0] |= 2048u; } - static void set_has_java_string_check_utf8(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00001000u; + static void set_has_java_string_check_utf8(HasBits* has_bits) { + (*has_bits)[0] |= 4096u; } - static void set_has_optimize_for(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00080000u; + static void set_has_optimize_for(HasBits* has_bits) { + (*has_bits)[0] |= 524288u; } - static void set_has_go_package(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_go_package(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_cc_generic_services(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00002000u; + static void set_has_cc_generic_services(HasBits* has_bits) { + (*has_bits)[0] |= 8192u; } - static void set_has_java_generic_services(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00004000u; + static void set_has_java_generic_services(HasBits* has_bits) { + (*has_bits)[0] |= 16384u; } - static void set_has_py_generic_services(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00008000u; + static void set_has_py_generic_services(HasBits* has_bits) { + (*has_bits)[0] |= 32768u; } - static void set_has_php_generic_services(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00010000u; + static void set_has_php_generic_services(HasBits* has_bits) { + (*has_bits)[0] |= 65536u; } - static void set_has_deprecated(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00020000u; + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 131072u; } - static void set_has_cc_enable_arenas(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00040000u; + static void set_has_cc_enable_arenas(HasBits* has_bits) { + (*has_bits)[0] |= 262144u; } - static void set_has_objc_class_prefix(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_objc_class_prefix(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_csharp_namespace(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_csharp_namespace(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static void set_has_swift_prefix(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_swift_prefix(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } - static void set_has_php_class_prefix(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000040u; + static void set_has_php_class_prefix(HasBits* has_bits) { + (*has_bits)[0] |= 64u; } - static void set_has_php_namespace(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000080u; + static void set_has_php_namespace(HasBits* has_bits) { + (*has_bits)[0] |= 128u; } - static void set_has_php_metadata_namespace(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000100u; + static void set_has_php_metadata_namespace(HasBits* has_bits) { + (*has_bits)[0] |= 256u; } - static void set_has_ruby_package(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000200u; + static void set_has_ruby_package(HasBits* has_bits) { + (*has_bits)[0] |= 512u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int FileOptions::kJavaPackageFieldNumber; -const int FileOptions::kJavaOuterClassnameFieldNumber; -const int FileOptions::kJavaMultipleFilesFieldNumber; -const int FileOptions::kJavaGenerateEqualsAndHashFieldNumber; -const int FileOptions::kJavaStringCheckUtf8FieldNumber; -const int FileOptions::kOptimizeForFieldNumber; -const int FileOptions::kGoPackageFieldNumber; -const int FileOptions::kCcGenericServicesFieldNumber; -const int FileOptions::kJavaGenericServicesFieldNumber; -const int FileOptions::kPyGenericServicesFieldNumber; -const int FileOptions::kPhpGenericServicesFieldNumber; -const int FileOptions::kDeprecatedFieldNumber; -const int FileOptions::kCcEnableArenasFieldNumber; -const int FileOptions::kObjcClassPrefixFieldNumber; -const int FileOptions::kCsharpNamespaceFieldNumber; -const int FileOptions::kSwiftPrefixFieldNumber; -const int FileOptions::kPhpClassPrefixFieldNumber; -const int FileOptions::kPhpNamespaceFieldNumber; -const int FileOptions::kPhpMetadataNamespaceFieldNumber; -const int FileOptions::kRubyPackageFieldNumber; -const int FileOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - FileOptions::FileOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.FileOptions) } -FileOptions::FileOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +FileOptions::FileOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -9152,60 +7648,60 @@ FileOptions::FileOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.FileOptions) } FileOptions::FileOptions(const FileOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); _extensions_.MergeFrom(from._extensions_); - java_package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + java_package_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_java_package()) { - java_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.java_package(), + java_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.java_package(), GetArenaNoVirtual()); } - java_outer_classname_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + java_outer_classname_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_java_outer_classname()) { - java_outer_classname_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.java_outer_classname(), + java_outer_classname_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.java_outer_classname(), GetArenaNoVirtual()); } - go_package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + go_package_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_go_package()) { - go_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.go_package(), + go_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.go_package(), GetArenaNoVirtual()); } - objc_class_prefix_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + objc_class_prefix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_objc_class_prefix()) { - objc_class_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.objc_class_prefix(), + objc_class_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.objc_class_prefix(), GetArenaNoVirtual()); } - csharp_namespace_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + csharp_namespace_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_csharp_namespace()) { - csharp_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.csharp_namespace(), + csharp_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.csharp_namespace(), GetArenaNoVirtual()); } - swift_prefix_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + swift_prefix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_swift_prefix()) { - swift_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.swift_prefix(), + swift_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.swift_prefix(), GetArenaNoVirtual()); } - php_class_prefix_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + php_class_prefix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_php_class_prefix()) { - php_class_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.php_class_prefix(), + php_class_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.php_class_prefix(), GetArenaNoVirtual()); } - php_namespace_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + php_namespace_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_php_namespace()) { - php_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.php_namespace(), + php_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.php_namespace(), GetArenaNoVirtual()); } - php_metadata_namespace_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + php_metadata_namespace_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_php_metadata_namespace()) { - php_metadata_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.php_metadata_namespace(), + php_metadata_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.php_metadata_namespace(), GetArenaNoVirtual()); } - ruby_package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ruby_package_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_ruby_package()) { - ruby_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.ruby_package(), + ruby_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.ruby_package(), GetArenaNoVirtual()); } ::memcpy(&java_multiple_files_, &from.java_multiple_files_, @@ -9215,18 +7711,17 @@ FileOptions::FileOptions(const FileOptions& from) } void FileOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - java_package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - java_outer_classname_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - go_package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - objc_class_prefix_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - csharp_namespace_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - swift_prefix_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - php_class_prefix_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - php_namespace_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - php_metadata_namespace_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - ruby_package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + java_package_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + java_outer_classname_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + go_package_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + objc_class_prefix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + csharp_namespace_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + swift_prefix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + php_class_prefix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + php_namespace_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + php_metadata_namespace_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + ruby_package_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&java_multiple_files_, 0, static_cast( reinterpret_cast(&cc_enable_arenas_) - reinterpret_cast(&java_multiple_files_)) + sizeof(cc_enable_arenas_)); @@ -9239,37 +7734,37 @@ FileOptions::~FileOptions() { } void FileOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - java_package_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - java_outer_classname_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - go_package_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - objc_class_prefix_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - csharp_namespace_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - swift_prefix_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - php_class_prefix_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - php_namespace_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - php_metadata_namespace_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - ruby_package_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + java_package_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + java_outer_classname_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + go_package_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + objc_class_prefix_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + csharp_namespace_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + swift_prefix_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + php_class_prefix_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + php_namespace_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + php_metadata_namespace_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + ruby_package_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void FileOptions::ArenaDtor(void* object) { FileOptions* _this = reinterpret_cast< FileOptions* >(object); (void)_this; } -void FileOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void FileOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void FileOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const FileOptions& FileOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void FileOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FileOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -9326,349 +7821,226 @@ void FileOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* FileOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* FileOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional string java_package = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.java_package"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_java_package(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_java_package(), ptr, ctx, "google.protobuf.FileOptions.java_package"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string java_outer_classname = 8; - case 8: { - if (static_cast<::google::protobuf::uint8>(tag) != 66) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.java_outer_classname"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_java_outer_classname(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 8: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 66)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_java_outer_classname(), ptr, ctx, "google.protobuf.FileOptions.java_outer_classname"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.FileOptions.OptimizeMode optimize_for = 9 [default = SPEED]; - case 9: { - if (static_cast<::google::protobuf::uint8>(tag) != 72) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - if (!::google::protobuf::FileOptions_OptimizeMode_IsValid(val)) { - ::google::protobuf::internal::WriteVarint(9, val, msg->mutable_unknown_fields()); - break; - } - ::google::protobuf::FileOptions_OptimizeMode value = static_cast<::google::protobuf::FileOptions_OptimizeMode>(val); - msg->set_optimize_for(value); - break; - } + case 9: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 72)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + if (PROTOBUF_PREDICT_TRUE(PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode_IsValid(val))) { + set_optimize_for(static_cast(val)); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(9, val, mutable_unknown_fields()); + } + } else goto handle_unusual; + continue; // optional bool java_multiple_files = 10 [default = false]; - case 10: { - if (static_cast<::google::protobuf::uint8>(tag) != 80) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_java_multiple_files(value); - break; - } + case 10: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 80)) { + _Internal::set_has_java_multiple_files(&has_bits); + java_multiple_files_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string go_package = 11; - case 11: { - if (static_cast<::google::protobuf::uint8>(tag) != 90) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.go_package"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_go_package(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 11: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 90)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_go_package(), ptr, ctx, "google.protobuf.FileOptions.go_package"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool cc_generic_services = 16 [default = false]; - case 16: { - if (static_cast<::google::protobuf::uint8>(tag) != 128) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_cc_generic_services(value); - break; - } + case 16: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 128)) { + _Internal::set_has_cc_generic_services(&has_bits); + cc_generic_services_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool java_generic_services = 17 [default = false]; - case 17: { - if (static_cast<::google::protobuf::uint8>(tag) != 136) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_java_generic_services(value); - break; - } + case 17: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 136)) { + _Internal::set_has_java_generic_services(&has_bits); + java_generic_services_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool py_generic_services = 18 [default = false]; - case 18: { - if (static_cast<::google::protobuf::uint8>(tag) != 144) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_py_generic_services(value); - break; - } + case 18: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 144)) { + _Internal::set_has_py_generic_services(&has_bits); + py_generic_services_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool java_generate_equals_and_hash = 20 [deprecated = true]; - case 20: { - if (static_cast<::google::protobuf::uint8>(tag) != 160) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_java_generate_equals_and_hash(value); - break; - } + case 20: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 160)) { + _Internal::set_has_java_generate_equals_and_hash(&has_bits); + java_generate_equals_and_hash_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool deprecated = 23 [default = false]; - case 23: { - if (static_cast<::google::protobuf::uint8>(tag) != 184) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_deprecated(value); - break; - } + case 23: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 184)) { + _Internal::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool java_string_check_utf8 = 27 [default = false]; - case 27: { - if (static_cast<::google::protobuf::uint8>(tag) != 216) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_java_string_check_utf8(value); - break; - } + case 27: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 216)) { + _Internal::set_has_java_string_check_utf8(&has_bits); + java_string_check_utf8_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool cc_enable_arenas = 31 [default = false]; - case 31: { - if (static_cast<::google::protobuf::uint8>(tag) != 248) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_cc_enable_arenas(value); - break; - } + case 31: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 248)) { + _Internal::set_has_cc_enable_arenas(&has_bits); + cc_enable_arenas_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string objc_class_prefix = 36; - case 36: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.objc_class_prefix"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_objc_class_prefix(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 36: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_objc_class_prefix(), ptr, ctx, "google.protobuf.FileOptions.objc_class_prefix"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string csharp_namespace = 37; - case 37: { - if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.csharp_namespace"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_csharp_namespace(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 37: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 42)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_csharp_namespace(), ptr, ctx, "google.protobuf.FileOptions.csharp_namespace"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string swift_prefix = 39; - case 39: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.swift_prefix"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_swift_prefix(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 39: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_swift_prefix(), ptr, ctx, "google.protobuf.FileOptions.swift_prefix"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string php_class_prefix = 40; - case 40: { - if (static_cast<::google::protobuf::uint8>(tag) != 66) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.php_class_prefix"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_php_class_prefix(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 40: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 66)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_php_class_prefix(), ptr, ctx, "google.protobuf.FileOptions.php_class_prefix"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string php_namespace = 41; - case 41: { - if (static_cast<::google::protobuf::uint8>(tag) != 74) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.php_namespace"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_php_namespace(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 41: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 74)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_php_namespace(), ptr, ctx, "google.protobuf.FileOptions.php_namespace"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool php_generic_services = 42 [default = false]; - case 42: { - if (static_cast<::google::protobuf::uint8>(tag) != 80) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_php_generic_services(value); - break; - } + case 42: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 80)) { + _Internal::set_has_php_generic_services(&has_bits); + php_generic_services_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string php_metadata_namespace = 44; - case 44: { - if (static_cast<::google::protobuf::uint8>(tag) != 98) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.php_metadata_namespace"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_php_metadata_namespace(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 44: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 98)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_php_metadata_namespace(), ptr, ctx, "google.protobuf.FileOptions.php_metadata_namespace"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string ruby_package = 45; - case 45: { - if (static_cast<::google::protobuf::uint8>(tag) != 106) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FileOptions.ruby_package"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_ruby_package(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 45: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 106)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_ruby_package(), ptr, ctx, "google.protobuf.FileOptions.ruby_package"); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FileOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.FileOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string java_package = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_java_package())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->java_package().data(), static_cast(this->java_package().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.java_package"); } else { goto handle_unusual; @@ -9678,12 +8050,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string java_outer_classname = 8; case 8: { - if (static_cast< ::google::protobuf::uint8>(tag) == (66 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (66 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_java_outer_classname())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->java_outer_classname().data(), static_cast(this->java_outer_classname().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.java_outer_classname"); } else { goto handle_unusual; @@ -9693,16 +8065,16 @@ bool FileOptions::MergePartialFromCodedStream( // optional .google.protobuf.FileOptions.OptimizeMode optimize_for = 9 [default = SPEED]; case 9: { - if (static_cast< ::google::protobuf::uint8>(tag) == (72 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (72 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - if (::google::protobuf::FileOptions_OptimizeMode_IsValid(value)) { - set_optimize_for(static_cast< ::google::protobuf::FileOptions_OptimizeMode >(value)); + if (PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode_IsValid(value)) { + set_optimize_for(static_cast< PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode >(value)); } else { mutable_unknown_fields()->AddVarint( - 9, static_cast<::google::protobuf::uint64>(value)); + 9, static_cast<::PROTOBUF_NAMESPACE_ID::uint64>(value)); } } else { goto handle_unusual; @@ -9712,10 +8084,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool java_multiple_files = 10 [default = false]; case 10: { - if (static_cast< ::google::protobuf::uint8>(tag) == (80 & 0xFF)) { - HasBitSetters::set_has_java_multiple_files(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (80 & 0xFF)) { + _Internal::set_has_java_multiple_files(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &java_multiple_files_))); } else { goto handle_unusual; @@ -9725,12 +8097,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string go_package = 11; case 11: { - if (static_cast< ::google::protobuf::uint8>(tag) == (90 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (90 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_go_package())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->go_package().data(), static_cast(this->go_package().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.go_package"); } else { goto handle_unusual; @@ -9740,10 +8112,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool cc_generic_services = 16 [default = false]; case 16: { - if (static_cast< ::google::protobuf::uint8>(tag) == (128 & 0xFF)) { - HasBitSetters::set_has_cc_generic_services(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (128 & 0xFF)) { + _Internal::set_has_cc_generic_services(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &cc_generic_services_))); } else { goto handle_unusual; @@ -9753,10 +8125,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool java_generic_services = 17 [default = false]; case 17: { - if (static_cast< ::google::protobuf::uint8>(tag) == (136 & 0xFF)) { - HasBitSetters::set_has_java_generic_services(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (136 & 0xFF)) { + _Internal::set_has_java_generic_services(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &java_generic_services_))); } else { goto handle_unusual; @@ -9766,10 +8138,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool py_generic_services = 18 [default = false]; case 18: { - if (static_cast< ::google::protobuf::uint8>(tag) == (144 & 0xFF)) { - HasBitSetters::set_has_py_generic_services(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (144 & 0xFF)) { + _Internal::set_has_py_generic_services(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &py_generic_services_))); } else { goto handle_unusual; @@ -9779,10 +8151,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool java_generate_equals_and_hash = 20 [deprecated = true]; case 20: { - if (static_cast< ::google::protobuf::uint8>(tag) == (160 & 0xFF)) { - HasBitSetters::set_has_java_generate_equals_and_hash(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (160 & 0xFF)) { + _Internal::set_has_java_generate_equals_and_hash(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &java_generate_equals_and_hash_))); } else { goto handle_unusual; @@ -9792,10 +8164,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool deprecated = 23 [default = false]; case 23: { - if (static_cast< ::google::protobuf::uint8>(tag) == (184 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (184 & 0xFF)) { + _Internal::set_has_deprecated(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); } else { goto handle_unusual; @@ -9805,10 +8177,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool java_string_check_utf8 = 27 [default = false]; case 27: { - if (static_cast< ::google::protobuf::uint8>(tag) == (216 & 0xFF)) { - HasBitSetters::set_has_java_string_check_utf8(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (216 & 0xFF)) { + _Internal::set_has_java_string_check_utf8(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &java_string_check_utf8_))); } else { goto handle_unusual; @@ -9818,10 +8190,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool cc_enable_arenas = 31 [default = false]; case 31: { - if (static_cast< ::google::protobuf::uint8>(tag) == (248 & 0xFF)) { - HasBitSetters::set_has_cc_enable_arenas(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (248 & 0xFF)) { + _Internal::set_has_cc_enable_arenas(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &cc_enable_arenas_))); } else { goto handle_unusual; @@ -9831,12 +8203,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string objc_class_prefix = 36; case 36: { - if (static_cast< ::google::protobuf::uint8>(tag) == (290 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (290 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_objc_class_prefix())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->objc_class_prefix().data(), static_cast(this->objc_class_prefix().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.objc_class_prefix"); } else { goto handle_unusual; @@ -9846,12 +8218,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string csharp_namespace = 37; case 37: { - if (static_cast< ::google::protobuf::uint8>(tag) == (298 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (298 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_csharp_namespace())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->csharp_namespace().data(), static_cast(this->csharp_namespace().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.csharp_namespace"); } else { goto handle_unusual; @@ -9861,12 +8233,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string swift_prefix = 39; case 39: { - if (static_cast< ::google::protobuf::uint8>(tag) == (314 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (314 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_swift_prefix())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->swift_prefix().data(), static_cast(this->swift_prefix().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.swift_prefix"); } else { goto handle_unusual; @@ -9876,12 +8248,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string php_class_prefix = 40; case 40: { - if (static_cast< ::google::protobuf::uint8>(tag) == (322 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (322 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_php_class_prefix())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->php_class_prefix().data(), static_cast(this->php_class_prefix().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.php_class_prefix"); } else { goto handle_unusual; @@ -9891,12 +8263,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string php_namespace = 41; case 41: { - if (static_cast< ::google::protobuf::uint8>(tag) == (330 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (330 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_php_namespace())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->php_namespace().data(), static_cast(this->php_namespace().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.php_namespace"); } else { goto handle_unusual; @@ -9906,10 +8278,10 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool php_generic_services = 42 [default = false]; case 42: { - if (static_cast< ::google::protobuf::uint8>(tag) == (336 & 0xFF)) { - HasBitSetters::set_has_php_generic_services(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (336 & 0xFF)) { + _Internal::set_has_php_generic_services(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &php_generic_services_))); } else { goto handle_unusual; @@ -9919,12 +8291,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string php_metadata_namespace = 44; case 44: { - if (static_cast< ::google::protobuf::uint8>(tag) == (354 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (354 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_php_metadata_namespace())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->php_metadata_namespace().data(), static_cast(this->php_metadata_namespace().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.php_metadata_namespace"); } else { goto handle_unusual; @@ -9934,12 +8306,12 @@ bool FileOptions::MergePartialFromCodedStream( // optional string ruby_package = 45; case 45: { - if (static_cast< ::google::protobuf::uint8>(tag) == (362 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (362 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_ruby_package())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->ruby_package().data(), static_cast(this->ruby_package().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.FileOptions.ruby_package"); } else { goto handle_unusual; @@ -9949,8 +8321,8 @@ bool FileOptions::MergePartialFromCodedStream( // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -9969,7 +8341,7 @@ bool FileOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -9985,367 +8357,189 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void FileOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.FileOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string java_package = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->java_package().data(), static_cast(this->java_package().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.java_package"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->java_package(), output); - } - - // optional string java_outer_classname = 8; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->java_outer_classname().data(), static_cast(this->java_outer_classname().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.java_outer_classname"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 8, this->java_outer_classname(), output); - } - - // optional .google.protobuf.FileOptions.OptimizeMode optimize_for = 9 [default = SPEED]; - if (cached_has_bits & 0x00080000u) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 9, this->optimize_for(), output); - } - - // optional bool java_multiple_files = 10 [default = false]; - if (cached_has_bits & 0x00000400u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(10, this->java_multiple_files(), output); - } - - // optional string go_package = 11; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->go_package().data(), static_cast(this->go_package().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.go_package"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 11, this->go_package(), output); - } - - // optional bool cc_generic_services = 16 [default = false]; - if (cached_has_bits & 0x00002000u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(16, this->cc_generic_services(), output); - } - - // optional bool java_generic_services = 17 [default = false]; - if (cached_has_bits & 0x00004000u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(17, this->java_generic_services(), output); - } - - // optional bool py_generic_services = 18 [default = false]; - if (cached_has_bits & 0x00008000u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(18, this->py_generic_services(), output); - } - - // optional bool java_generate_equals_and_hash = 20 [deprecated = true]; - if (cached_has_bits & 0x00000800u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(20, this->java_generate_equals_and_hash(), output); - } - - // optional bool deprecated = 23 [default = false]; - if (cached_has_bits & 0x00020000u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(23, this->deprecated(), output); - } - - // optional bool java_string_check_utf8 = 27 [default = false]; - if (cached_has_bits & 0x00001000u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(27, this->java_string_check_utf8(), output); - } - - // optional bool cc_enable_arenas = 31 [default = false]; - if (cached_has_bits & 0x00040000u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(31, this->cc_enable_arenas(), output); - } - - // optional string objc_class_prefix = 36; - if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->objc_class_prefix().data(), static_cast(this->objc_class_prefix().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.objc_class_prefix"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 36, this->objc_class_prefix(), output); - } - - // optional string csharp_namespace = 37; - if (cached_has_bits & 0x00000010u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->csharp_namespace().data(), static_cast(this->csharp_namespace().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.csharp_namespace"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 37, this->csharp_namespace(), output); - } - - // optional string swift_prefix = 39; - if (cached_has_bits & 0x00000020u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->swift_prefix().data(), static_cast(this->swift_prefix().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.swift_prefix"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 39, this->swift_prefix(), output); - } - - // optional string php_class_prefix = 40; - if (cached_has_bits & 0x00000040u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->php_class_prefix().data(), static_cast(this->php_class_prefix().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.php_class_prefix"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 40, this->php_class_prefix(), output); - } - - // optional string php_namespace = 41; - if (cached_has_bits & 0x00000080u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->php_namespace().data(), static_cast(this->php_namespace().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.php_namespace"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 41, this->php_namespace(), output); - } - - // optional bool php_generic_services = 42 [default = false]; - if (cached_has_bits & 0x00010000u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(42, this->php_generic_services(), output); - } - - // optional string php_metadata_namespace = 44; - if (cached_has_bits & 0x00000100u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->php_metadata_namespace().data(), static_cast(this->php_metadata_namespace().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.php_metadata_namespace"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 44, this->php_metadata_namespace(), output); - } - - // optional string ruby_package = 45; - if (cached_has_bits & 0x00000200u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->ruby_package().data(), static_cast(this->ruby_package().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.FileOptions.ruby_package"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 45, this->ruby_package(), output); - } - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.FileOptions) -} - -::google::protobuf::uint8* FileOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* FileOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.FileOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional string java_package = 1; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->java_package().data(), static_cast(this->java_package().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.java_package"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->java_package(), target); } // optional string java_outer_classname = 8; if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->java_outer_classname().data(), static_cast(this->java_outer_classname().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.java_outer_classname"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 8, this->java_outer_classname(), target); } // optional .google.protobuf.FileOptions.OptimizeMode optimize_for = 9 [default = SPEED]; if (cached_has_bits & 0x00080000u) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 9, this->optimize_for(), target); } // optional bool java_multiple_files = 10 [default = false]; if (cached_has_bits & 0x00000400u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(10, this->java_multiple_files(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(10, this->java_multiple_files(), target); } // optional string go_package = 11; if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->go_package().data(), static_cast(this->go_package().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.go_package"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 11, this->go_package(), target); } // optional bool cc_generic_services = 16 [default = false]; if (cached_has_bits & 0x00002000u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(16, this->cc_generic_services(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(16, this->cc_generic_services(), target); } // optional bool java_generic_services = 17 [default = false]; if (cached_has_bits & 0x00004000u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(17, this->java_generic_services(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(17, this->java_generic_services(), target); } // optional bool py_generic_services = 18 [default = false]; if (cached_has_bits & 0x00008000u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(18, this->py_generic_services(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(18, this->py_generic_services(), target); } // optional bool java_generate_equals_and_hash = 20 [deprecated = true]; if (cached_has_bits & 0x00000800u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(20, this->java_generate_equals_and_hash(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(20, this->java_generate_equals_and_hash(), target); } // optional bool deprecated = 23 [default = false]; if (cached_has_bits & 0x00020000u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(23, this->deprecated(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(23, this->deprecated(), target); } // optional bool java_string_check_utf8 = 27 [default = false]; if (cached_has_bits & 0x00001000u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(27, this->java_string_check_utf8(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(27, this->java_string_check_utf8(), target); } // optional bool cc_enable_arenas = 31 [default = false]; if (cached_has_bits & 0x00040000u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(31, this->cc_enable_arenas(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(31, this->cc_enable_arenas(), target); } // optional string objc_class_prefix = 36; if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->objc_class_prefix().data(), static_cast(this->objc_class_prefix().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.objc_class_prefix"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 36, this->objc_class_prefix(), target); } // optional string csharp_namespace = 37; if (cached_has_bits & 0x00000010u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->csharp_namespace().data(), static_cast(this->csharp_namespace().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.csharp_namespace"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 37, this->csharp_namespace(), target); } // optional string swift_prefix = 39; if (cached_has_bits & 0x00000020u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->swift_prefix().data(), static_cast(this->swift_prefix().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.swift_prefix"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 39, this->swift_prefix(), target); } // optional string php_class_prefix = 40; if (cached_has_bits & 0x00000040u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->php_class_prefix().data(), static_cast(this->php_class_prefix().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.php_class_prefix"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 40, this->php_class_prefix(), target); } // optional string php_namespace = 41; if (cached_has_bits & 0x00000080u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->php_namespace().data(), static_cast(this->php_namespace().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.php_namespace"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 41, this->php_namespace(), target); } // optional bool php_generic_services = 42 [default = false]; if (cached_has_bits & 0x00010000u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(42, this->php_generic_services(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(42, this->php_generic_services(), target); } // optional string php_metadata_namespace = 44; if (cached_has_bits & 0x00000100u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->php_metadata_namespace().data(), static_cast(this->php_metadata_namespace().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.php_metadata_namespace"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 44, this->php_metadata_namespace(), target); } // optional string ruby_package = 45; if (cached_has_bits & 0x00000200u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->ruby_package().data(), static_cast(this->ruby_package().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.FileOptions.ruby_package"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 45, this->ruby_package(), target); } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.FileOptions) return target; @@ -10357,12 +8551,7 @@ size_t FileOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -10372,7 +8561,7 @@ size_t FileOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } @@ -10382,56 +8571,56 @@ size_t FileOptions::ByteSizeLong() const { // optional string java_package = 1; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->java_package()); } // optional string java_outer_classname = 8; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->java_outer_classname()); } // optional string go_package = 11; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->go_package()); } // optional string objc_class_prefix = 36; if (cached_has_bits & 0x00000008u) { total_size += 2 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->objc_class_prefix()); } // optional string csharp_namespace = 37; if (cached_has_bits & 0x00000010u) { total_size += 2 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->csharp_namespace()); } // optional string swift_prefix = 39; if (cached_has_bits & 0x00000020u) { total_size += 2 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->swift_prefix()); } // optional string php_class_prefix = 40; if (cached_has_bits & 0x00000040u) { total_size += 2 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->php_class_prefix()); } // optional string php_namespace = 41; if (cached_has_bits & 0x00000080u) { total_size += 2 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->php_namespace()); } @@ -10440,14 +8629,14 @@ size_t FileOptions::ByteSizeLong() const { // optional string php_metadata_namespace = 44; if (cached_has_bits & 0x00000100u) { total_size += 2 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->php_metadata_namespace()); } // optional string ruby_package = 45; if (cached_has_bits & 0x00000200u) { total_size += 2 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->ruby_package()); } @@ -10501,24 +8690,28 @@ size_t FileOptions::ByteSizeLong() const { // optional .google.protobuf.FileOptions.OptimizeMode optimize_for = 9 [default = SPEED]; if (cached_has_bits & 0x00080000u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->optimize_for()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->optimize_for()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void FileOptions::MergeFrom(const ::google::protobuf::Message& from) { +void FileOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.FileOptions) GOOGLE_DCHECK_NE(&from, this); const FileOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.FileOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.FileOptions) MergeFrom(*source); @@ -10530,7 +8723,7 @@ void FileOptions::MergeFrom(const FileOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); @@ -10605,7 +8798,7 @@ void FileOptions::MergeFrom(const FileOptions& from) { } } -void FileOptions::CopyFrom(const ::google::protobuf::Message& from) { +void FileOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.FileOptions) if (&from == this) return; Clear(); @@ -10624,54 +8817,35 @@ bool FileOptions::IsInitialized() const { return false; } - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; return true; } -void FileOptions::Swap(FileOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - FileOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void FileOptions::UnsafeArenaSwap(FileOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void FileOptions::InternalSwap(FileOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); CastToBase(&uninterpreted_option_)->InternalSwap(CastToBase(&other->uninterpreted_option_)); - java_package_.Swap(&other->java_package_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + java_package_.Swap(&other->java_package_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - java_outer_classname_.Swap(&other->java_outer_classname_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + java_outer_classname_.Swap(&other->java_outer_classname_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - go_package_.Swap(&other->go_package_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + go_package_.Swap(&other->go_package_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - objc_class_prefix_.Swap(&other->objc_class_prefix_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + objc_class_prefix_.Swap(&other->objc_class_prefix_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - csharp_namespace_.Swap(&other->csharp_namespace_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + csharp_namespace_.Swap(&other->csharp_namespace_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - swift_prefix_.Swap(&other->swift_prefix_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + swift_prefix_.Swap(&other->swift_prefix_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - php_class_prefix_.Swap(&other->php_class_prefix_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + php_class_prefix_.Swap(&other->php_class_prefix_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - php_namespace_.Swap(&other->php_namespace_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + php_namespace_.Swap(&other->php_namespace_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - php_metadata_namespace_.Swap(&other->php_metadata_namespace_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + php_metadata_namespace_.Swap(&other->php_metadata_namespace_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - ruby_package_.Swap(&other->ruby_package_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + ruby_package_.Swap(&other->ruby_package_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(java_multiple_files_, other->java_multiple_files_); swap(java_generate_equals_and_hash_, other->java_generate_equals_and_hash_); @@ -10685,9 +8859,8 @@ void FileOptions::InternalSwap(FileOptions* other) { swap(optimize_for_, other->optimize_for_); } -::google::protobuf::Metadata FileOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata FileOptions::GetMetadata() const { + return GetMetadataStatic(); } @@ -10695,37 +8868,30 @@ void FileOptions::InternalSwap(FileOptions* other) { void MessageOptions::InitAsDefaultInstance() { } -class MessageOptions::HasBitSetters { +class MessageOptions::_Internal { public: - static void set_has_message_set_wire_format(MessageOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_message_set_wire_format(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_no_standard_descriptor_accessor(MessageOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_no_standard_descriptor_accessor(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_deprecated(MessageOptions* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_map_entry(MessageOptions* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_map_entry(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int MessageOptions::kMessageSetWireFormatFieldNumber; -const int MessageOptions::kNoStandardDescriptorAccessorFieldNumber; -const int MessageOptions::kDeprecatedFieldNumber; -const int MessageOptions::kMapEntryFieldNumber; -const int MessageOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - MessageOptions::MessageOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.MessageOptions) } -MessageOptions::MessageOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +MessageOptions::MessageOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -10734,8 +8900,8 @@ MessageOptions::MessageOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.MessageOptions) } MessageOptions::MessageOptions(const MessageOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -10747,8 +8913,7 @@ MessageOptions::MessageOptions(const MessageOptions& from) } void MessageOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base); ::memset(&message_set_wire_format_, 0, static_cast( reinterpret_cast(&map_entry_) - reinterpret_cast(&message_set_wire_format_)) + sizeof(map_entry_)); @@ -10760,27 +8925,27 @@ MessageOptions::~MessageOptions() { } void MessageOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void MessageOptions::ArenaDtor(void* object) { MessageOptions* _this = reinterpret_cast< MessageOptions* >(object); (void)_this; } -void MessageOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void MessageOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void MessageOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const MessageOptions& MessageOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void MessageOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.MessageOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -10794,123 +8959,102 @@ void MessageOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* MessageOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* MessageOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional bool message_set_wire_format = 1 [default = false]; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_message_set_wire_format(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + _Internal::set_has_message_set_wire_format(&has_bits); + message_set_wire_format_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool no_standard_descriptor_accessor = 2 [default = false]; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_no_standard_descriptor_accessor(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_no_standard_descriptor_accessor(&has_bits); + no_standard_descriptor_accessor_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool deprecated = 3 [default = false]; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_deprecated(value); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { + _Internal::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool map_entry = 7; - case 7: { - if (static_cast<::google::protobuf::uint8>(tag) != 56) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_map_entry(value); - break; - } + case 7: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 56)) { + _Internal::set_has_map_entry(&has_bits); + map_entry_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool MessageOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.MessageOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional bool message_set_wire_format = 1 [default = false]; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_message_set_wire_format(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { + _Internal::set_has_message_set_wire_format(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &message_set_wire_format_))); } else { goto handle_unusual; @@ -10920,10 +9064,10 @@ bool MessageOptions::MergePartialFromCodedStream( // optional bool no_standard_descriptor_accessor = 2 [default = false]; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_no_standard_descriptor_accessor(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_no_standard_descriptor_accessor(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &no_standard_descriptor_accessor_))); } else { goto handle_unusual; @@ -10933,10 +9077,10 @@ bool MessageOptions::MergePartialFromCodedStream( // optional bool deprecated = 3 [default = false]; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { + _Internal::set_has_deprecated(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); } else { goto handle_unusual; @@ -10946,10 +9090,10 @@ bool MessageOptions::MergePartialFromCodedStream( // optional bool map_entry = 7; case 7: { - if (static_cast< ::google::protobuf::uint8>(tag) == (56 & 0xFF)) { - HasBitSetters::set_has_map_entry(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (56 & 0xFF)) { + _Internal::set_has_map_entry(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &map_entry_))); } else { goto handle_unusual; @@ -10959,8 +9103,8 @@ bool MessageOptions::MergePartialFromCodedStream( // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -10979,7 +9123,7 @@ bool MessageOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -10995,95 +9139,52 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void MessageOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.MessageOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional bool message_set_wire_format = 1 [default = false]; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(1, this->message_set_wire_format(), output); - } - - // optional bool no_standard_descriptor_accessor = 2 [default = false]; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(2, this->no_standard_descriptor_accessor(), output); - } - - // optional bool deprecated = 3 [default = false]; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(3, this->deprecated(), output); - } - - // optional bool map_entry = 7; - if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(7, this->map_entry(), output); - } - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.MessageOptions) -} - -::google::protobuf::uint8* MessageOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* MessageOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.MessageOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional bool message_set_wire_format = 1 [default = false]; if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(1, this->message_set_wire_format(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(1, this->message_set_wire_format(), target); } // optional bool no_standard_descriptor_accessor = 2 [default = false]; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(2, this->no_standard_descriptor_accessor(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(2, this->no_standard_descriptor_accessor(), target); } // optional bool deprecated = 3 [default = false]; if (cached_has_bits & 0x00000004u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(3, this->deprecated(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(3, this->deprecated(), target); } // optional bool map_entry = 7; if (cached_has_bits & 0x00000008u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(7, this->map_entry(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(7, this->map_entry(), target); } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.MessageOptions) return target; @@ -11095,12 +9196,7 @@ size_t MessageOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -11110,7 +9206,7 @@ size_t MessageOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } @@ -11138,20 +9234,24 @@ size_t MessageOptions::ByteSizeLong() const { } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void MessageOptions::MergeFrom(const ::google::protobuf::Message& from) { +void MessageOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.MessageOptions) GOOGLE_DCHECK_NE(&from, this); const MessageOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.MessageOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.MessageOptions) MergeFrom(*source); @@ -11163,7 +9263,7 @@ void MessageOptions::MergeFrom(const MessageOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); @@ -11185,7 +9285,7 @@ void MessageOptions::MergeFrom(const MessageOptions& from) { } } -void MessageOptions::CopyFrom(const ::google::protobuf::Message& from) { +void MessageOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.MessageOptions) if (&from == this) return; Clear(); @@ -11204,29 +9304,10 @@ bool MessageOptions::IsInitialized() const { return false; } - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; return true; } -void MessageOptions::Swap(MessageOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - MessageOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void MessageOptions::UnsafeArenaSwap(MessageOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void MessageOptions::InternalSwap(MessageOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); @@ -11239,9 +9320,8 @@ void MessageOptions::InternalSwap(MessageOptions* other) { swap(map_entry_, other->map_entry_); } -::google::protobuf::Metadata MessageOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata MessageOptions::GetMetadata() const { + return GetMetadataStatic(); } @@ -11249,45 +9329,36 @@ void MessageOptions::InternalSwap(MessageOptions* other) { void FieldOptions::InitAsDefaultInstance() { } -class FieldOptions::HasBitSetters { +class FieldOptions::_Internal { public: - static void set_has_ctype(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_ctype(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_packed(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_packed(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_jstype(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_jstype(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } - static void set_has_lazy(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_lazy(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_deprecated(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_weak(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_weak(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int FieldOptions::kCtypeFieldNumber; -const int FieldOptions::kPackedFieldNumber; -const int FieldOptions::kJstypeFieldNumber; -const int FieldOptions::kLazyFieldNumber; -const int FieldOptions::kDeprecatedFieldNumber; -const int FieldOptions::kWeakFieldNumber; -const int FieldOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - FieldOptions::FieldOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.FieldOptions) } -FieldOptions::FieldOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +FieldOptions::FieldOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -11296,8 +9367,8 @@ FieldOptions::FieldOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.FieldOptions) } FieldOptions::FieldOptions(const FieldOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -11309,8 +9380,7 @@ FieldOptions::FieldOptions(const FieldOptions& from) } void FieldOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base); ::memset(&ctype_, 0, static_cast( reinterpret_cast(&jstype_) - reinterpret_cast(&ctype_)) + sizeof(jstype_)); @@ -11322,27 +9392,27 @@ FieldOptions::~FieldOptions() { } void FieldOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void FieldOptions::ArenaDtor(void* object) { FieldOptions* _this = reinterpret_cast< FieldOptions* >(object); (void)_this; } -void FieldOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void FieldOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void FieldOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const FieldOptions& FieldOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void FieldOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FieldOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -11359,157 +9429,132 @@ void FieldOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* FieldOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* FieldOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING]; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - if (!::google::protobuf::FieldOptions_CType_IsValid(val)) { - ::google::protobuf::internal::WriteVarint(1, val, msg->mutable_unknown_fields()); - break; - } - ::google::protobuf::FieldOptions_CType value = static_cast<::google::protobuf::FieldOptions_CType>(val); - msg->set_ctype(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + if (PROTOBUF_PREDICT_TRUE(PROTOBUF_NAMESPACE_ID::FieldOptions_CType_IsValid(val))) { + set_ctype(static_cast(val)); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(1, val, mutable_unknown_fields()); + } + } else goto handle_unusual; + continue; // optional bool packed = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_packed(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_packed(&has_bits); + packed_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool deprecated = 3 [default = false]; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_deprecated(value); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { + _Internal::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool lazy = 5 [default = false]; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 40) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_lazy(value); - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 40)) { + _Internal::set_has_lazy(&has_bits); + lazy_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.FieldOptions.JSType jstype = 6 [default = JS_NORMAL]; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 48) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - if (!::google::protobuf::FieldOptions_JSType_IsValid(val)) { - ::google::protobuf::internal::WriteVarint(6, val, msg->mutable_unknown_fields()); - break; - } - ::google::protobuf::FieldOptions_JSType value = static_cast<::google::protobuf::FieldOptions_JSType>(val); - msg->set_jstype(value); - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 48)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + if (PROTOBUF_PREDICT_TRUE(PROTOBUF_NAMESPACE_ID::FieldOptions_JSType_IsValid(val))) { + set_jstype(static_cast(val)); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(6, val, mutable_unknown_fields()); + } + } else goto handle_unusual; + continue; // optional bool weak = 10 [default = false]; - case 10: { - if (static_cast<::google::protobuf::uint8>(tag) != 80) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_weak(value); - break; - } + case 10: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 80)) { + _Internal::set_has_weak(&has_bits); + weak_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FieldOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.FieldOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING]; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - if (::google::protobuf::FieldOptions_CType_IsValid(value)) { - set_ctype(static_cast< ::google::protobuf::FieldOptions_CType >(value)); + if (PROTOBUF_NAMESPACE_ID::FieldOptions_CType_IsValid(value)) { + set_ctype(static_cast< PROTOBUF_NAMESPACE_ID::FieldOptions_CType >(value)); } else { mutable_unknown_fields()->AddVarint( - 1, static_cast<::google::protobuf::uint64>(value)); + 1, static_cast<::PROTOBUF_NAMESPACE_ID::uint64>(value)); } } else { goto handle_unusual; @@ -11519,10 +9564,10 @@ bool FieldOptions::MergePartialFromCodedStream( // optional bool packed = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_packed(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_packed(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &packed_))); } else { goto handle_unusual; @@ -11532,10 +9577,10 @@ bool FieldOptions::MergePartialFromCodedStream( // optional bool deprecated = 3 [default = false]; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { + _Internal::set_has_deprecated(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); } else { goto handle_unusual; @@ -11545,10 +9590,10 @@ bool FieldOptions::MergePartialFromCodedStream( // optional bool lazy = 5 [default = false]; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (40 & 0xFF)) { - HasBitSetters::set_has_lazy(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (40 & 0xFF)) { + _Internal::set_has_lazy(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &lazy_))); } else { goto handle_unusual; @@ -11558,16 +9603,16 @@ bool FieldOptions::MergePartialFromCodedStream( // optional .google.protobuf.FieldOptions.JSType jstype = 6 [default = JS_NORMAL]; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (48 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (48 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - if (::google::protobuf::FieldOptions_JSType_IsValid(value)) { - set_jstype(static_cast< ::google::protobuf::FieldOptions_JSType >(value)); + if (PROTOBUF_NAMESPACE_ID::FieldOptions_JSType_IsValid(value)) { + set_jstype(static_cast< PROTOBUF_NAMESPACE_ID::FieldOptions_JSType >(value)); } else { mutable_unknown_fields()->AddVarint( - 6, static_cast<::google::protobuf::uint64>(value)); + 6, static_cast<::PROTOBUF_NAMESPACE_ID::uint64>(value)); } } else { goto handle_unusual; @@ -11577,10 +9622,10 @@ bool FieldOptions::MergePartialFromCodedStream( // optional bool weak = 10 [default = false]; case 10: { - if (static_cast< ::google::protobuf::uint8>(tag) == (80 & 0xFF)) { - HasBitSetters::set_has_weak(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (80 & 0xFF)) { + _Internal::set_has_weak(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &weak_))); } else { goto handle_unusual; @@ -11590,8 +9635,8 @@ bool FieldOptions::MergePartialFromCodedStream( // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -11610,7 +9655,7 @@ bool FieldOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -11626,119 +9671,66 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void FieldOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.FieldOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING]; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 1, this->ctype(), output); - } - - // optional bool packed = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(2, this->packed(), output); - } - - // optional bool deprecated = 3 [default = false]; - if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(3, this->deprecated(), output); - } - - // optional bool lazy = 5 [default = false]; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(5, this->lazy(), output); - } - - // optional .google.protobuf.FieldOptions.JSType jstype = 6 [default = JS_NORMAL]; - if (cached_has_bits & 0x00000020u) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 6, this->jstype(), output); - } - - // optional bool weak = 10 [default = false]; - if (cached_has_bits & 0x00000010u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(10, this->weak(), output); - } - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.FieldOptions) -} - -::google::protobuf::uint8* FieldOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* FieldOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.FieldOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING]; if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 1, this->ctype(), target); } // optional bool packed = 2; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(2, this->packed(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(2, this->packed(), target); } // optional bool deprecated = 3 [default = false]; if (cached_has_bits & 0x00000008u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(3, this->deprecated(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(3, this->deprecated(), target); } // optional bool lazy = 5 [default = false]; if (cached_has_bits & 0x00000004u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(5, this->lazy(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(5, this->lazy(), target); } // optional .google.protobuf.FieldOptions.JSType jstype = 6 [default = JS_NORMAL]; if (cached_has_bits & 0x00000020u) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 6, this->jstype(), target); } // optional bool weak = 10 [default = false]; if (cached_has_bits & 0x00000010u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(10, this->weak(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(10, this->weak(), target); } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.FieldOptions) return target; @@ -11750,12 +9742,7 @@ size_t FieldOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -11765,7 +9752,7 @@ size_t FieldOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } @@ -11775,7 +9762,7 @@ size_t FieldOptions::ByteSizeLong() const { // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING]; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->ctype()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->ctype()); } // optional bool packed = 2; @@ -11801,24 +9788,28 @@ size_t FieldOptions::ByteSizeLong() const { // optional .google.protobuf.FieldOptions.JSType jstype = 6 [default = JS_NORMAL]; if (cached_has_bits & 0x00000020u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->jstype()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->jstype()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void FieldOptions::MergeFrom(const ::google::protobuf::Message& from) { +void FieldOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.FieldOptions) GOOGLE_DCHECK_NE(&from, this); const FieldOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.FieldOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.FieldOptions) MergeFrom(*source); @@ -11830,7 +9821,7 @@ void FieldOptions::MergeFrom(const FieldOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); @@ -11858,7 +9849,7 @@ void FieldOptions::MergeFrom(const FieldOptions& from) { } } -void FieldOptions::CopyFrom(const ::google::protobuf::Message& from) { +void FieldOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.FieldOptions) if (&from == this) return; Clear(); @@ -11876,30 +9867,11 @@ bool FieldOptions::IsInitialized() const { if (!_extensions_.IsInitialized()) { return false; } - - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; - return true; -} - -void FieldOptions::Swap(FieldOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - FieldOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void FieldOptions::UnsafeArenaSwap(FieldOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); + + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; + return true; } + void FieldOptions::InternalSwap(FieldOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); @@ -11914,9 +9886,8 @@ void FieldOptions::InternalSwap(FieldOptions* other) { swap(jstype_, other->jstype_); } -::google::protobuf::Metadata FieldOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata FieldOptions::GetMetadata() const { + return GetMetadataStatic(); } @@ -11924,21 +9895,18 @@ void FieldOptions::InternalSwap(FieldOptions* other) { void OneofOptions::InitAsDefaultInstance() { } -class OneofOptions::HasBitSetters { +class OneofOptions::_Internal { public: + using HasBits = decltype(std::declval()._has_bits_); }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int OneofOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - OneofOptions::OneofOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.OneofOptions) } -OneofOptions::OneofOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +OneofOptions::OneofOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -11947,8 +9915,8 @@ OneofOptions::OneofOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.OneofOptions) } OneofOptions::OneofOptions(const OneofOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -11957,8 +9925,7 @@ OneofOptions::OneofOptions(const OneofOptions& from) } void OneofOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base); } OneofOptions::~OneofOptions() { @@ -11967,27 +9934,27 @@ OneofOptions::~OneofOptions() { } void OneofOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void OneofOptions::ArenaDtor(void* object) { OneofOptions* _this = reinterpret_cast< OneofOptions* >(object); (void)_this; } -void OneofOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void OneofOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void OneofOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const OneofOptions& OneofOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void OneofOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.OneofOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -11998,81 +9965,66 @@ void OneofOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* OneofOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* OneofOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool OneofOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.OneofOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -12091,7 +10043,7 @@ bool OneofOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -12107,53 +10059,27 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void OneofOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.OneofOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.OneofOptions) -} - -::google::protobuf::uint8* OneofOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* OneofOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.OneofOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.OneofOptions) return target; @@ -12165,12 +10091,7 @@ size_t OneofOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -12180,25 +10101,29 @@ size_t OneofOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void OneofOptions::MergeFrom(const ::google::protobuf::Message& from) { +void OneofOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.OneofOptions) GOOGLE_DCHECK_NE(&from, this); const OneofOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.OneofOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.OneofOptions) MergeFrom(*source); @@ -12210,13 +10135,13 @@ void OneofOptions::MergeFrom(const OneofOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); } -void OneofOptions::CopyFrom(const ::google::protobuf::Message& from) { +void OneofOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.OneofOptions) if (&from == this) return; Clear(); @@ -12235,29 +10160,10 @@ bool OneofOptions::IsInitialized() const { return false; } - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; return true; } -void OneofOptions::Swap(OneofOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - OneofOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void OneofOptions::UnsafeArenaSwap(OneofOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void OneofOptions::InternalSwap(OneofOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); @@ -12266,9 +10172,8 @@ void OneofOptions::InternalSwap(OneofOptions* other) { CastToBase(&uninterpreted_option_)->InternalSwap(CastToBase(&other->uninterpreted_option_)); } -::google::protobuf::Metadata OneofOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata OneofOptions::GetMetadata() const { + return GetMetadataStatic(); } @@ -12276,29 +10181,24 @@ void OneofOptions::InternalSwap(OneofOptions* other) { void EnumOptions::InitAsDefaultInstance() { } -class EnumOptions::HasBitSetters { +class EnumOptions::_Internal { public: - static void set_has_allow_alias(EnumOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_allow_alias(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_deprecated(EnumOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int EnumOptions::kAllowAliasFieldNumber; -const int EnumOptions::kDeprecatedFieldNumber; -const int EnumOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - EnumOptions::EnumOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.EnumOptions) } -EnumOptions::EnumOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +EnumOptions::EnumOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -12307,8 +10207,8 @@ EnumOptions::EnumOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.EnumOptions) } EnumOptions::EnumOptions(const EnumOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -12320,8 +10220,7 @@ EnumOptions::EnumOptions(const EnumOptions& from) } void EnumOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base); ::memset(&allow_alias_, 0, static_cast( reinterpret_cast(&deprecated_) - reinterpret_cast(&allow_alias_)) + sizeof(deprecated_)); @@ -12333,27 +10232,27 @@ EnumOptions::~EnumOptions() { } void EnumOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void EnumOptions::ArenaDtor(void* object) { EnumOptions* _this = reinterpret_cast< EnumOptions* >(object); (void)_this; } -void EnumOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void EnumOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void EnumOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const EnumOptions& EnumOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void EnumOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -12367,103 +10266,86 @@ void EnumOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* EnumOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* EnumOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional bool allow_alias = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_allow_alias(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_allow_alias(&has_bits); + allow_alias_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional bool deprecated = 3 [default = false]; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_deprecated(value); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { + _Internal::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.EnumOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional bool allow_alias = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_allow_alias(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_allow_alias(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &allow_alias_))); } else { goto handle_unusual; @@ -12473,10 +10355,10 @@ bool EnumOptions::MergePartialFromCodedStream( // optional bool deprecated = 3 [default = false]; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { + _Internal::set_has_deprecated(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); } else { goto handle_unusual; @@ -12486,8 +10368,8 @@ bool EnumOptions::MergePartialFromCodedStream( // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -12506,7 +10388,7 @@ bool EnumOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -12522,75 +10404,40 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void EnumOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.EnumOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional bool allow_alias = 2; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(2, this->allow_alias(), output); - } - - // optional bool deprecated = 3 [default = false]; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(3, this->deprecated(), output); - } - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.EnumOptions) -} - -::google::protobuf::uint8* EnumOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* EnumOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.EnumOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional bool allow_alias = 2; if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(2, this->allow_alias(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(2, this->allow_alias(), target); } // optional bool deprecated = 3 [default = false]; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(3, this->deprecated(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(3, this->deprecated(), target); } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.EnumOptions) return target; @@ -12602,12 +10449,7 @@ size_t EnumOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -12617,7 +10459,7 @@ size_t EnumOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } @@ -12635,20 +10477,24 @@ size_t EnumOptions::ByteSizeLong() const { } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void EnumOptions::MergeFrom(const ::google::protobuf::Message& from) { +void EnumOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.EnumOptions) GOOGLE_DCHECK_NE(&from, this); const EnumOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.EnumOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.EnumOptions) MergeFrom(*source); @@ -12660,7 +10506,7 @@ void EnumOptions::MergeFrom(const EnumOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); @@ -12676,7 +10522,7 @@ void EnumOptions::MergeFrom(const EnumOptions& from) { } } -void EnumOptions::CopyFrom(const ::google::protobuf::Message& from) { +void EnumOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.EnumOptions) if (&from == this) return; Clear(); @@ -12695,29 +10541,10 @@ bool EnumOptions::IsInitialized() const { return false; } - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; return true; } -void EnumOptions::Swap(EnumOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - EnumOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void EnumOptions::UnsafeArenaSwap(EnumOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void EnumOptions::InternalSwap(EnumOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); @@ -12728,9 +10555,8 @@ void EnumOptions::InternalSwap(EnumOptions* other) { swap(deprecated_, other->deprecated_); } -::google::protobuf::Metadata EnumOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata EnumOptions::GetMetadata() const { + return GetMetadataStatic(); } @@ -12738,25 +10564,21 @@ void EnumOptions::InternalSwap(EnumOptions* other) { void EnumValueOptions::InitAsDefaultInstance() { } -class EnumValueOptions::HasBitSetters { +class EnumValueOptions::_Internal { public: - static void set_has_deprecated(EnumValueOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int EnumValueOptions::kDeprecatedFieldNumber; -const int EnumValueOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - EnumValueOptions::EnumValueOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.EnumValueOptions) } -EnumValueOptions::EnumValueOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +EnumValueOptions::EnumValueOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -12765,8 +10587,8 @@ EnumValueOptions::EnumValueOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.EnumValueOptions) } EnumValueOptions::EnumValueOptions(const EnumValueOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -12776,8 +10598,7 @@ EnumValueOptions::EnumValueOptions(const EnumValueOptions& from) } void EnumValueOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base); deprecated_ = false; } @@ -12787,27 +10608,27 @@ EnumValueOptions::~EnumValueOptions() { } void EnumValueOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void EnumValueOptions::ArenaDtor(void* object) { EnumValueOptions* _this = reinterpret_cast< EnumValueOptions* >(object); (void)_this; } -void EnumValueOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void EnumValueOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void EnumValueOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const EnumValueOptions& EnumValueOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void EnumValueOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumValueOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -12819,93 +10640,78 @@ void EnumValueOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* EnumValueOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* EnumValueOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional bool deprecated = 1 [default = false]; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_deprecated(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + _Internal::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumValueOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.EnumValueOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional bool deprecated = 1 [default = false]; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { + _Internal::set_has_deprecated(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); } else { goto handle_unusual; @@ -12915,8 +10721,8 @@ bool EnumValueOptions::MergePartialFromCodedStream( // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -12935,7 +10741,7 @@ bool EnumValueOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -12951,65 +10757,34 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void EnumValueOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.EnumValueOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional bool deprecated = 1 [default = false]; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(1, this->deprecated(), output); - } - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.EnumValueOptions) -} - -::google::protobuf::uint8* EnumValueOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* EnumValueOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.EnumValueOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional bool deprecated = 1 [default = false]; if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(1, this->deprecated(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(1, this->deprecated(), target); } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.EnumValueOptions) return target; @@ -13021,12 +10796,7 @@ size_t EnumValueOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -13036,7 +10806,7 @@ size_t EnumValueOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } @@ -13047,20 +10817,24 @@ size_t EnumValueOptions::ByteSizeLong() const { total_size += 1 + 1; } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void EnumValueOptions::MergeFrom(const ::google::protobuf::Message& from) { +void EnumValueOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.EnumValueOptions) GOOGLE_DCHECK_NE(&from, this); const EnumValueOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.EnumValueOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.EnumValueOptions) MergeFrom(*source); @@ -13072,7 +10846,7 @@ void EnumValueOptions::MergeFrom(const EnumValueOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); @@ -13081,7 +10855,7 @@ void EnumValueOptions::MergeFrom(const EnumValueOptions& from) { } } -void EnumValueOptions::CopyFrom(const ::google::protobuf::Message& from) { +void EnumValueOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.EnumValueOptions) if (&from == this) return; Clear(); @@ -13100,29 +10874,10 @@ bool EnumValueOptions::IsInitialized() const { return false; } - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; return true; } -void EnumValueOptions::Swap(EnumValueOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - EnumValueOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void EnumValueOptions::UnsafeArenaSwap(EnumValueOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void EnumValueOptions::InternalSwap(EnumValueOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); @@ -13132,9 +10887,8 @@ void EnumValueOptions::InternalSwap(EnumValueOptions* other) { swap(deprecated_, other->deprecated_); } -::google::protobuf::Metadata EnumValueOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata EnumValueOptions::GetMetadata() const { + return GetMetadataStatic(); } @@ -13142,25 +10896,21 @@ void EnumValueOptions::InternalSwap(EnumValueOptions* other) { void ServiceOptions::InitAsDefaultInstance() { } -class ServiceOptions::HasBitSetters { +class ServiceOptions::_Internal { public: - static void set_has_deprecated(ServiceOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int ServiceOptions::kDeprecatedFieldNumber; -const int ServiceOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - ServiceOptions::ServiceOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.ServiceOptions) } -ServiceOptions::ServiceOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +ServiceOptions::ServiceOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -13169,8 +10919,8 @@ ServiceOptions::ServiceOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.ServiceOptions) } ServiceOptions::ServiceOptions(const ServiceOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -13180,8 +10930,7 @@ ServiceOptions::ServiceOptions(const ServiceOptions& from) } void ServiceOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base); deprecated_ = false; } @@ -13191,27 +10940,27 @@ ServiceOptions::~ServiceOptions() { } void ServiceOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void ServiceOptions::ArenaDtor(void* object) { ServiceOptions* _this = reinterpret_cast< ServiceOptions* >(object); (void)_this; } -void ServiceOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void ServiceOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void ServiceOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const ServiceOptions& ServiceOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void ServiceOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.ServiceOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -13223,93 +10972,78 @@ void ServiceOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* ServiceOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* ServiceOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional bool deprecated = 33 [default = false]; - case 33: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_deprecated(value); - break; - } + case 33: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + _Internal::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ServiceOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.ServiceOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional bool deprecated = 33 [default = false]; case 33: { - if (static_cast< ::google::protobuf::uint8>(tag) == (264 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (264 & 0xFF)) { + _Internal::set_has_deprecated(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); } else { goto handle_unusual; @@ -13319,8 +11053,8 @@ bool ServiceOptions::MergePartialFromCodedStream( // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -13339,7 +11073,7 @@ bool ServiceOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -13355,65 +11089,34 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void ServiceOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.ServiceOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional bool deprecated = 33 [default = false]; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(33, this->deprecated(), output); - } - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.ServiceOptions) -} - -::google::protobuf::uint8* ServiceOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* ServiceOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.ServiceOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional bool deprecated = 33 [default = false]; if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(33, this->deprecated(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(33, this->deprecated(), target); } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.ServiceOptions) return target; @@ -13425,12 +11128,7 @@ size_t ServiceOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -13440,7 +11138,7 @@ size_t ServiceOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } @@ -13451,20 +11149,24 @@ size_t ServiceOptions::ByteSizeLong() const { total_size += 2 + 1; } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void ServiceOptions::MergeFrom(const ::google::protobuf::Message& from) { +void ServiceOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.ServiceOptions) GOOGLE_DCHECK_NE(&from, this); const ServiceOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.ServiceOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.ServiceOptions) MergeFrom(*source); @@ -13476,7 +11178,7 @@ void ServiceOptions::MergeFrom(const ServiceOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); @@ -13485,7 +11187,7 @@ void ServiceOptions::MergeFrom(const ServiceOptions& from) { } } -void ServiceOptions::CopyFrom(const ::google::protobuf::Message& from) { +void ServiceOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.ServiceOptions) if (&from == this) return; Clear(); @@ -13504,29 +11206,10 @@ bool ServiceOptions::IsInitialized() const { return false; } - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; return true; } -void ServiceOptions::Swap(ServiceOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - ServiceOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void ServiceOptions::UnsafeArenaSwap(ServiceOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void ServiceOptions::InternalSwap(ServiceOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); @@ -13536,9 +11219,8 @@ void ServiceOptions::InternalSwap(ServiceOptions* other) { swap(deprecated_, other->deprecated_); } -::google::protobuf::Metadata ServiceOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata ServiceOptions::GetMetadata() const { + return GetMetadataStatic(); } @@ -13546,29 +11228,24 @@ void ServiceOptions::InternalSwap(ServiceOptions* other) { void MethodOptions::InitAsDefaultInstance() { } -class MethodOptions::HasBitSetters { +class MethodOptions::_Internal { public: - static void set_has_deprecated(MethodOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_idempotency_level(MethodOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_idempotency_level(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int MethodOptions::kDeprecatedFieldNumber; -const int MethodOptions::kIdempotencyLevelFieldNumber; -const int MethodOptions::kUninterpretedOptionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - MethodOptions::MethodOptions() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.MethodOptions) } -MethodOptions::MethodOptions(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +MethodOptions::MethodOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _extensions_(arena), _internal_metadata_(arena), uninterpreted_option_(arena) { @@ -13577,8 +11254,8 @@ MethodOptions::MethodOptions(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.MethodOptions) } MethodOptions::MethodOptions(const MethodOptions& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), uninterpreted_option_(from.uninterpreted_option_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -13590,8 +11267,7 @@ MethodOptions::MethodOptions(const MethodOptions& from) } void MethodOptions::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base); ::memset(&deprecated_, 0, static_cast( reinterpret_cast(&idempotency_level_) - reinterpret_cast(&deprecated_)) + sizeof(idempotency_level_)); @@ -13603,27 +11279,27 @@ MethodOptions::~MethodOptions() { } void MethodOptions::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void MethodOptions::ArenaDtor(void* object) { MethodOptions* _this = reinterpret_cast< MethodOptions* >(object); (void)_this; } -void MethodOptions::RegisterArenaDtor(::google::protobuf::Arena*) { +void MethodOptions::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void MethodOptions::SetCachedSize(int size) const { _cached_size_.Set(size); } const MethodOptions& MethodOptions::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void MethodOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.MethodOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -13640,107 +11316,90 @@ void MethodOptions::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* MethodOptions::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* MethodOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // optional bool deprecated = 33 [default = false]; - case 33: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_deprecated(value); - break; - } + case 33: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + _Internal::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional .google.protobuf.MethodOptions.IdempotencyLevel idempotency_level = 34 [default = IDEMPOTENCY_UNKNOWN]; - case 34: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - if (!::google::protobuf::MethodOptions_IdempotencyLevel_IsValid(val)) { - ::google::protobuf::internal::WriteVarint(34, val, msg->mutable_unknown_fields()); - break; - } - ::google::protobuf::MethodOptions_IdempotencyLevel value = static_cast<::google::protobuf::MethodOptions_IdempotencyLevel>(val); - msg->set_idempotency_level(value); - break; - } + case 34: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + if (PROTOBUF_PREDICT_TRUE(PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel_IsValid(val))) { + set_idempotency_level(static_cast(val)); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(34, val, mutable_unknown_fields()); + } + } else goto handle_unusual; + continue; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - case 999: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption::_InternalParse; - object = msg->add_uninterpreted_option(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 65535) == 16058 && (ptr += 2)); - break; - } + case 999: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr -= 2; + do { + ptr += 2; + ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) == 16058); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } if ((8000u <= tag)) { - auto res = msg->_extensions_.ParseField(tag, {_InternalParse, msg}, ptr, end, - internal_default_instance(), &msg->_internal_metadata_, ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = _extensions_.ParseField(tag, ptr, + internal_default_instance(), &_internal_metadata_, ctx); + CHK_(ptr != nullptr); continue; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool MethodOptions::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.MethodOptions) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(16383u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional bool deprecated = 33 [default = false]; case 33: { - if (static_cast< ::google::protobuf::uint8>(tag) == (264 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (264 & 0xFF)) { + _Internal::set_has_deprecated(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); } else { goto handle_unusual; @@ -13750,16 +11409,16 @@ bool MethodOptions::MergePartialFromCodedStream( // optional .google.protobuf.MethodOptions.IdempotencyLevel idempotency_level = 34 [default = IDEMPOTENCY_UNKNOWN]; case 34: { - if (static_cast< ::google::protobuf::uint8>(tag) == (272 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (272 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - if (::google::protobuf::MethodOptions_IdempotencyLevel_IsValid(value)) { - set_idempotency_level(static_cast< ::google::protobuf::MethodOptions_IdempotencyLevel >(value)); + if (PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel_IsValid(value)) { + set_idempotency_level(static_cast< PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel >(value)); } else { mutable_unknown_fields()->AddVarint( - 34, static_cast<::google::protobuf::uint64>(value)); + 34, static_cast<::PROTOBUF_NAMESPACE_ID::uint64>(value)); } } else { goto handle_unusual; @@ -13769,8 +11428,8 @@ bool MethodOptions::MergePartialFromCodedStream( // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { - if (static_cast< ::google::protobuf::uint8>(tag) == (7994 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (7994 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_uninterpreted_option())); } else { goto handle_unusual; @@ -13789,7 +11448,7 @@ bool MethodOptions::MergePartialFromCodedStream( _internal_metadata_.mutable_unknown_fields())); continue; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -13805,77 +11464,41 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void MethodOptions::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.MethodOptions) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional bool deprecated = 33 [default = false]; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(33, this->deprecated(), output); - } - - // optional .google.protobuf.MethodOptions.IdempotencyLevel idempotency_level = 34 [default = IDEMPOTENCY_UNKNOWN]; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 34, this->idempotency_level(), output); - } - - // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 999, - this->uninterpreted_option(static_cast(i)), - output); - } - - // Extension range [1000, 536870912) - _extensions_.SerializeWithCachedSizes(1000, 536870912, output); - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.MethodOptions) -} - -::google::protobuf::uint8* MethodOptions::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* MethodOptions::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.MethodOptions) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // optional bool deprecated = 33 [default = false]; if (cached_has_bits & 0x00000001u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(33, this->deprecated(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(33, this->deprecated(), target); } // optional .google.protobuf.MethodOptions.IdempotencyLevel idempotency_level = 34 [default = IDEMPOTENCY_UNKNOWN]; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 34, this->idempotency_level(), target); } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; - for (unsigned int i = 0, - n = static_cast(this->uninterpreted_option_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 999, this->uninterpreted_option(static_cast(i)), deterministic, target); + for (auto it = this->uninterpreted_option().pointer_begin(), + end = this->uninterpreted_option().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(999, **it, target, stream); } // Extension range [1000, 536870912) target = _extensions_.InternalSerializeWithCachedSizesToArray( - 1000, 536870912, deterministic, target); + 1000, 536870912, target, stream); - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.MethodOptions) return target; @@ -13887,12 +11510,7 @@ size_t MethodOptions::ByteSizeLong() const { total_size += _extensions_.ByteSize(); - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -13902,7 +11520,7 @@ size_t MethodOptions::ByteSizeLong() const { total_size += 2UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->uninterpreted_option(static_cast(i))); } } @@ -13917,24 +11535,28 @@ size_t MethodOptions::ByteSizeLong() const { // optional .google.protobuf.MethodOptions.IdempotencyLevel idempotency_level = 34 [default = IDEMPOTENCY_UNKNOWN]; if (cached_has_bits & 0x00000002u) { total_size += 2 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->idempotency_level()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->idempotency_level()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void MethodOptions::MergeFrom(const ::google::protobuf::Message& from) { +void MethodOptions::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.MethodOptions) GOOGLE_DCHECK_NE(&from, this); const MethodOptions* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.MethodOptions) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.MethodOptions) MergeFrom(*source); @@ -13946,7 +11568,7 @@ void MethodOptions::MergeFrom(const MethodOptions& from) { GOOGLE_DCHECK_NE(&from, this); _extensions_.MergeFrom(from._extensions_); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; uninterpreted_option_.MergeFrom(from.uninterpreted_option_); @@ -13962,7 +11584,7 @@ void MethodOptions::MergeFrom(const MethodOptions& from) { } } -void MethodOptions::CopyFrom(const ::google::protobuf::Message& from) { +void MethodOptions::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.MethodOptions) if (&from == this) return; Clear(); @@ -13981,29 +11603,10 @@ bool MethodOptions::IsInitialized() const { return false; } - if (!::google::protobuf::internal::AllAreInitialized(this->uninterpreted_option())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->uninterpreted_option())) return false; return true; } -void MethodOptions::Swap(MethodOptions* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - MethodOptions* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void MethodOptions::UnsafeArenaSwap(MethodOptions* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void MethodOptions::InternalSwap(MethodOptions* other) { using std::swap; _extensions_.Swap(&other->_extensions_); @@ -14014,9 +11617,8 @@ void MethodOptions::InternalSwap(MethodOptions* other) { swap(idempotency_level_, other->idempotency_level_); } -::google::protobuf::Metadata MethodOptions::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata MethodOptions::GetMetadata() const { + return GetMetadataStatic(); } @@ -14024,41 +11626,37 @@ void MethodOptions::InternalSwap(MethodOptions* other) { void UninterpretedOption_NamePart::InitAsDefaultInstance() { } -class UninterpretedOption_NamePart::HasBitSetters { +class UninterpretedOption_NamePart::_Internal { public: - static void set_has_name_part(UninterpretedOption_NamePart* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_name_part(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_is_extension(UninterpretedOption_NamePart* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_is_extension(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int UninterpretedOption_NamePart::kNamePartFieldNumber; -const int UninterpretedOption_NamePart::kIsExtensionFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - UninterpretedOption_NamePart::UninterpretedOption_NamePart() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.UninterpretedOption.NamePart) } -UninterpretedOption_NamePart::UninterpretedOption_NamePart(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +UninterpretedOption_NamePart::UninterpretedOption_NamePart(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.UninterpretedOption.NamePart) } UninterpretedOption_NamePart::UninterpretedOption_NamePart(const UninterpretedOption_NamePart& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - name_part_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + name_part_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_name_part()) { - name_part_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_part(), + name_part_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_part(), GetArenaNoVirtual()); } is_extension_ = from.is_extension_; @@ -14066,9 +11664,8 @@ UninterpretedOption_NamePart::UninterpretedOption_NamePart(const UninterpretedOp } void UninterpretedOption_NamePart::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base); - name_part_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base); + name_part_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); is_extension_ = false; } @@ -14078,28 +11675,28 @@ UninterpretedOption_NamePart::~UninterpretedOption_NamePart() { } void UninterpretedOption_NamePart::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - name_part_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + name_part_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void UninterpretedOption_NamePart::ArenaDtor(void* object) { UninterpretedOption_NamePart* _this = reinterpret_cast< UninterpretedOption_NamePart* >(object); (void)_this; } -void UninterpretedOption_NamePart::RegisterArenaDtor(::google::protobuf::Arena*) { +void UninterpretedOption_NamePart::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void UninterpretedOption_NamePart::SetCachedSize(int size) const { _cached_size_.Set(size); } const UninterpretedOption_NamePart& UninterpretedOption_NamePart::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void UninterpretedOption_NamePart::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.UninterpretedOption.NamePart) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -14113,88 +11710,69 @@ void UninterpretedOption_NamePart::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* UninterpretedOption_NamePart::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* UninterpretedOption_NamePart::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // required string name_part = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.UninterpretedOption.NamePart.name_part"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_name_part(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name_part(), ptr, ctx, "google.protobuf.UninterpretedOption.NamePart.name_part"); + CHK_(ptr); + } else goto handle_unusual; + continue; // required bool is_extension = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_is_extension(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + _Internal::set_has_is_extension(&has_bits); + is_extension_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool UninterpretedOption_NamePart::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.UninterpretedOption.NamePart) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // required string name_part = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_name_part())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name_part().data(), static_cast(this->name_part().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.UninterpretedOption.NamePart.name_part"); } else { goto handle_unusual; @@ -14204,10 +11782,10 @@ bool UninterpretedOption_NamePart::MergePartialFromCodedStream( // required bool is_extension = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_is_extension(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + _Internal::set_has_is_extension(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &is_extension_))); } else { goto handle_unusual; @@ -14220,7 +11798,7 @@ bool UninterpretedOption_NamePart::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -14236,62 +11814,32 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void UninterpretedOption_NamePart::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.UninterpretedOption.NamePart) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // required string name_part = 1; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->name_part().data(), static_cast(this->name_part().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.UninterpretedOption.NamePart.name_part"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->name_part(), output); - } - - // required bool is_extension = 2; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteBool(2, this->is_extension(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.UninterpretedOption.NamePart) -} - -::google::protobuf::uint8* UninterpretedOption_NamePart::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* UninterpretedOption_NamePart::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.UninterpretedOption.NamePart) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = _has_bits_[0]; // required string name_part = 1; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->name_part().data(), static_cast(this->name_part().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.UninterpretedOption.NamePart.name_part"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->name_part(), target); } // required bool is_extension = 2; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(2, this->is_extension(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(2, this->is_extension(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.UninterpretedOption.NamePart) return target; @@ -14304,7 +11852,7 @@ size_t UninterpretedOption_NamePart::RequiredFieldsByteSizeFallback() const { if (has_name_part()) { // required string name_part = 1; total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name_part()); } @@ -14319,15 +11867,10 @@ size_t UninterpretedOption_NamePart::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.UninterpretedOption.NamePart) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } if (((_has_bits_[0] & 0x00000003) ^ 0x00000003) == 0) { // All required fields are present. // required string name_part = 1; total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->name_part()); // required bool is_extension = 2; @@ -14336,24 +11879,28 @@ size_t UninterpretedOption_NamePart::ByteSizeLong() const { } else { total_size += RequiredFieldsByteSizeFallback(); } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void UninterpretedOption_NamePart::MergeFrom(const ::google::protobuf::Message& from) { +void UninterpretedOption_NamePart::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.UninterpretedOption.NamePart) GOOGLE_DCHECK_NE(&from, this); const UninterpretedOption_NamePart* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.UninterpretedOption.NamePart) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.UninterpretedOption.NamePart) MergeFrom(*source); @@ -14364,7 +11911,7 @@ void UninterpretedOption_NamePart::MergeFrom(const UninterpretedOption_NamePart& // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.UninterpretedOption.NamePart) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; cached_has_bits = from._has_bits_[0]; @@ -14379,7 +11926,7 @@ void UninterpretedOption_NamePart::MergeFrom(const UninterpretedOption_NamePart& } } -void UninterpretedOption_NamePart::CopyFrom(const ::google::protobuf::Message& from) { +void UninterpretedOption_NamePart::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.UninterpretedOption.NamePart) if (&from == this) return; Clear(); @@ -14398,37 +11945,17 @@ bool UninterpretedOption_NamePart::IsInitialized() const { return true; } -void UninterpretedOption_NamePart::Swap(UninterpretedOption_NamePart* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - UninterpretedOption_NamePart* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void UninterpretedOption_NamePart::UnsafeArenaSwap(UninterpretedOption_NamePart* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void UninterpretedOption_NamePart::InternalSwap(UninterpretedOption_NamePart* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); - name_part_.Swap(&other->name_part_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_part_.Swap(&other->name_part_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(is_extension_, other->is_extension_); } -::google::protobuf::Metadata UninterpretedOption_NamePart::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata UninterpretedOption_NamePart::GetMetadata() const { + return GetMetadataStatic(); } @@ -14436,45 +11963,36 @@ void UninterpretedOption_NamePart::InternalSwap(UninterpretedOption_NamePart* ot void UninterpretedOption::InitAsDefaultInstance() { } -class UninterpretedOption::HasBitSetters { +class UninterpretedOption::_Internal { public: - static void set_has_identifier_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_identifier_value(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_positive_int_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_positive_int_value(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_negative_int_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_negative_int_value(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static void set_has_double_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_double_value(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } - static void set_has_string_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_string_value(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_aggregate_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_aggregate_value(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int UninterpretedOption::kNameFieldNumber; -const int UninterpretedOption::kIdentifierValueFieldNumber; -const int UninterpretedOption::kPositiveIntValueFieldNumber; -const int UninterpretedOption::kNegativeIntValueFieldNumber; -const int UninterpretedOption::kDoubleValueFieldNumber; -const int UninterpretedOption::kStringValueFieldNumber; -const int UninterpretedOption::kAggregateValueFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - UninterpretedOption::UninterpretedOption() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.UninterpretedOption) } -UninterpretedOption::UninterpretedOption(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +UninterpretedOption::UninterpretedOption(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), name_(arena) { SharedCtor(); @@ -14482,24 +12000,24 @@ UninterpretedOption::UninterpretedOption(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.UninterpretedOption) } UninterpretedOption::UninterpretedOption(const UninterpretedOption& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), name_(from.name_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - identifier_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + identifier_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_identifier_value()) { - identifier_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.identifier_value(), + identifier_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.identifier_value(), GetArenaNoVirtual()); } - string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + string_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_string_value()) { - string_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.string_value(), + string_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.string_value(), GetArenaNoVirtual()); } - aggregate_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + aggregate_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_aggregate_value()) { - aggregate_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.aggregate_value(), + aggregate_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.aggregate_value(), GetArenaNoVirtual()); } ::memcpy(&positive_int_value_, &from.positive_int_value_, @@ -14509,11 +12027,10 @@ UninterpretedOption::UninterpretedOption(const UninterpretedOption& from) } void UninterpretedOption::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base); - identifier_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - aggregate_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base); + identifier_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + string_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + aggregate_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&positive_int_value_, 0, static_cast( reinterpret_cast(&double_value_) - reinterpret_cast(&positive_int_value_)) + sizeof(double_value_)); @@ -14525,30 +12042,30 @@ UninterpretedOption::~UninterpretedOption() { } void UninterpretedOption::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - identifier_value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - string_value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - aggregate_value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + identifier_value_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + string_value_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + aggregate_value_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void UninterpretedOption::ArenaDtor(void* object) { UninterpretedOption* _this = reinterpret_cast< UninterpretedOption* >(object); (void)_this; } -void UninterpretedOption::RegisterArenaDtor(::google::protobuf::Arena*) { +void UninterpretedOption::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void UninterpretedOption::SetCachedSize(int size) const { _cached_size_.Set(size); } const UninterpretedOption& UninterpretedOption::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void UninterpretedOption::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.UninterpretedOption) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -14575,150 +12092,107 @@ void UninterpretedOption::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* UninterpretedOption::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* UninterpretedOption::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated .google.protobuf.UninterpretedOption.NamePart name = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::UninterpretedOption_NamePart::_InternalParse; - object = msg->add_name(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 18 && (ptr += 1)); - break; - } - // optional string identifier_value = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.UninterpretedOption.identifier_value"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_identifier_value(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_name(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 18); + } else goto handle_unusual; + continue; + // optional string identifier_value = 3; + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_identifier_value(), ptr, ctx, "google.protobuf.UninterpretedOption.identifier_value"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional uint64 positive_int_value = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 32) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::uint64 value = val; - msg->set_positive_int_value(value); - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 32)) { + _Internal::set_has_positive_int_value(&has_bits); + positive_int_value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int64 negative_int_value = 5; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 40) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int64 value = val; - msg->set_negative_int_value(value); - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 40)) { + _Internal::set_has_negative_int_value(&has_bits); + negative_int_value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional double double_value = 6; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 49) goto handle_unusual; - double val; - ::std::memcpy(&val, ptr, 8); - ptr += 8; - msg->set_double_value(val); - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 49)) { + _Internal::set_has_double_value(&has_bits); + double_value_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad(ptr); + ptr += sizeof(double); + } else goto handle_unusual; + continue; // optional bytes string_value = 7; - case 7: { - if (static_cast<::google::protobuf::uint8>(tag) != 58) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::internal::StringParser; - ::std::string* str = msg->mutable_string_value(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 7: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(mutable_string_value(), ptr, ctx); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string aggregate_value = 8; - case 8: { - if (static_cast<::google::protobuf::uint8>(tag) != 66) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.UninterpretedOption.aggregate_value"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_aggregate_value(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 8: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 66)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_aggregate_value(), ptr, ctx, "google.protobuf.UninterpretedOption.aggregate_value"); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool UninterpretedOption::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.UninterpretedOption) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated .google.protobuf.UninterpretedOption.NamePart name = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_name())); } else { goto handle_unusual; @@ -14728,12 +12202,12 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional string identifier_value = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_identifier_value())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->identifier_value().data(), static_cast(this->identifier_value().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.UninterpretedOption.identifier_value"); } else { goto handle_unusual; @@ -14743,10 +12217,10 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional uint64 positive_int_value = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (32 & 0xFF)) { - HasBitSetters::set_has_positive_int_value(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (32 & 0xFF)) { + _Internal::set_has_positive_int_value(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::uint64, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_UINT64>( input, &positive_int_value_))); } else { goto handle_unusual; @@ -14756,10 +12230,10 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional int64 negative_int_value = 5; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (40 & 0xFF)) { - HasBitSetters::set_has_negative_int_value(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int64, ::google::protobuf::internal::WireFormatLite::TYPE_INT64>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (40 & 0xFF)) { + _Internal::set_has_negative_int_value(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int64, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT64>( input, &negative_int_value_))); } else { goto handle_unusual; @@ -14769,10 +12243,10 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional double double_value = 6; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (49 & 0xFF)) { - HasBitSetters::set_has_double_value(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - double, ::google::protobuf::internal::WireFormatLite::TYPE_DOUBLE>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (49 & 0xFF)) { + _Internal::set_has_double_value(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + double, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_DOUBLE>( input, &double_value_))); } else { goto handle_unusual; @@ -14782,8 +12256,8 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional bytes string_value = 7; case 7: { - if (static_cast< ::google::protobuf::uint8>(tag) == (58 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (58 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadBytes( input, this->mutable_string_value())); } else { goto handle_unusual; @@ -14793,12 +12267,12 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional string aggregate_value = 8; case 8: { - if (static_cast< ::google::protobuf::uint8>(tag) == (66 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (66 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_aggregate_value())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->aggregate_value().data(), static_cast(this->aggregate_value().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.UninterpretedOption.aggregate_value"); } else { goto handle_unusual; @@ -14811,7 +12285,7 @@ bool UninterpretedOption::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -14827,133 +12301,68 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void UninterpretedOption::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.UninterpretedOption) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .google.protobuf.UninterpretedOption.NamePart name = 2; - for (unsigned int i = 0, - n = static_cast(this->name_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 2, - this->name(static_cast(i)), - output); - } - - cached_has_bits = _has_bits_[0]; - // optional string identifier_value = 3; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->identifier_value().data(), static_cast(this->identifier_value().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.UninterpretedOption.identifier_value"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 3, this->identifier_value(), output); - } - - // optional uint64 positive_int_value = 4; - if (cached_has_bits & 0x00000008u) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(4, this->positive_int_value(), output); - } - - // optional int64 negative_int_value = 5; - if (cached_has_bits & 0x00000010u) { - ::google::protobuf::internal::WireFormatLite::WriteInt64(5, this->negative_int_value(), output); - } - - // optional double double_value = 6; - if (cached_has_bits & 0x00000020u) { - ::google::protobuf::internal::WireFormatLite::WriteDouble(6, this->double_value(), output); - } - - // optional bytes string_value = 7; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteBytesMaybeAliased( - 7, this->string_value(), output); - } - - // optional string aggregate_value = 8; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->aggregate_value().data(), static_cast(this->aggregate_value().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.UninterpretedOption.aggregate_value"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 8, this->aggregate_value(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.UninterpretedOption) -} - -::google::protobuf::uint8* UninterpretedOption::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* UninterpretedOption::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.UninterpretedOption) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated .google.protobuf.UninterpretedOption.NamePart name = 2; - for (unsigned int i = 0, - n = static_cast(this->name_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 2, this->name(static_cast(i)), deterministic, target); + for (auto it = this->name().pointer_begin(), + end = this->name().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(2, **it, target, stream); } cached_has_bits = _has_bits_[0]; // optional string identifier_value = 3; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->identifier_value().data(), static_cast(this->identifier_value().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.UninterpretedOption.identifier_value"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 3, this->identifier_value(), target); } // optional uint64 positive_int_value = 4; if (cached_has_bits & 0x00000008u) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(4, this->positive_int_value(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt64ToArray(4, this->positive_int_value(), target); } // optional int64 negative_int_value = 5; if (cached_has_bits & 0x00000010u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt64ToArray(5, this->negative_int_value(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt64ToArray(5, this->negative_int_value(), target); } // optional double double_value = 6; if (cached_has_bits & 0x00000020u) { - target = ::google::protobuf::internal::WireFormatLite::WriteDoubleToArray(6, this->double_value(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteDoubleToArray(6, this->double_value(), target); } // optional bytes string_value = 7; if (cached_has_bits & 0x00000002u) { - target = - ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + target = stream->WriteBytesMaybeAliased( 7, this->string_value(), target); } // optional string aggregate_value = 8; if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->aggregate_value().data(), static_cast(this->aggregate_value().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.UninterpretedOption.aggregate_value"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 8, this->aggregate_value(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.UninterpretedOption) return target; @@ -14963,12 +12372,7 @@ size_t UninterpretedOption::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.UninterpretedOption) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -14978,7 +12382,7 @@ size_t UninterpretedOption::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->name(static_cast(i))); } } @@ -14988,35 +12392,35 @@ size_t UninterpretedOption::ByteSizeLong() const { // optional string identifier_value = 3; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->identifier_value()); } // optional bytes string_value = 7; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::BytesSize( this->string_value()); } // optional string aggregate_value = 8; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->aggregate_value()); } // optional uint64 positive_int_value = 4; if (cached_has_bits & 0x00000008u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::UInt64Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt64Size( this->positive_int_value()); } // optional int64 negative_int_value = 5; if (cached_has_bits & 0x00000010u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int64Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int64Size( this->negative_int_value()); } @@ -15026,20 +12430,24 @@ size_t UninterpretedOption::ByteSizeLong() const { } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void UninterpretedOption::MergeFrom(const ::google::protobuf::Message& from) { +void UninterpretedOption::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.UninterpretedOption) GOOGLE_DCHECK_NE(&from, this); const UninterpretedOption* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.UninterpretedOption) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.UninterpretedOption) MergeFrom(*source); @@ -15050,7 +12458,7 @@ void UninterpretedOption::MergeFrom(const UninterpretedOption& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.UninterpretedOption) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; name_.MergeFrom(from.name_); @@ -15078,7 +12486,7 @@ void UninterpretedOption::MergeFrom(const UninterpretedOption& from) { } } -void UninterpretedOption::CopyFrom(const ::google::protobuf::Message& from) { +void UninterpretedOption::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.UninterpretedOption) if (&from == this) return; Clear(); @@ -15093,48 +12501,28 @@ void UninterpretedOption::CopyFrom(const UninterpretedOption& from) { } bool UninterpretedOption::IsInitialized() const { - if (!::google::protobuf::internal::AllAreInitialized(this->name())) return false; + if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(this->name())) return false; return true; } -void UninterpretedOption::Swap(UninterpretedOption* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - UninterpretedOption* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void UninterpretedOption::UnsafeArenaSwap(UninterpretedOption* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void UninterpretedOption::InternalSwap(UninterpretedOption* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); CastToBase(&name_)->InternalSwap(CastToBase(&other->name_)); - identifier_value_.Swap(&other->identifier_value_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + identifier_value_.Swap(&other->identifier_value_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - string_value_.Swap(&other->string_value_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + string_value_.Swap(&other->string_value_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - aggregate_value_.Swap(&other->aggregate_value_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + aggregate_value_.Swap(&other->aggregate_value_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(positive_int_value_, other->positive_int_value_); swap(negative_int_value_, other->negative_int_value_); swap(double_value_, other->double_value_); } -::google::protobuf::Metadata UninterpretedOption::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata UninterpretedOption::GetMetadata() const { + return GetMetadataStatic(); } @@ -15142,31 +12530,24 @@ void UninterpretedOption::InternalSwap(UninterpretedOption* other) { void SourceCodeInfo_Location::InitAsDefaultInstance() { } -class SourceCodeInfo_Location::HasBitSetters { +class SourceCodeInfo_Location::_Internal { public: - static void set_has_leading_comments(SourceCodeInfo_Location* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_leading_comments(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_trailing_comments(SourceCodeInfo_Location* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_trailing_comments(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int SourceCodeInfo_Location::kPathFieldNumber; -const int SourceCodeInfo_Location::kSpanFieldNumber; -const int SourceCodeInfo_Location::kLeadingCommentsFieldNumber; -const int SourceCodeInfo_Location::kTrailingCommentsFieldNumber; -const int SourceCodeInfo_Location::kLeadingDetachedCommentsFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - SourceCodeInfo_Location::SourceCodeInfo_Location() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.SourceCodeInfo.Location) } -SourceCodeInfo_Location::SourceCodeInfo_Location(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +SourceCodeInfo_Location::SourceCodeInfo_Location(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), path_(arena), span_(arena), @@ -15176,31 +12557,30 @@ SourceCodeInfo_Location::SourceCodeInfo_Location(::google::protobuf::Arena* aren // @@protoc_insertion_point(arena_constructor:google.protobuf.SourceCodeInfo.Location) } SourceCodeInfo_Location::SourceCodeInfo_Location(const SourceCodeInfo_Location& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), path_(from.path_), span_(from.span_), leading_detached_comments_(from.leading_detached_comments_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - leading_comments_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + leading_comments_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_leading_comments()) { - leading_comments_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.leading_comments(), + leading_comments_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.leading_comments(), GetArenaNoVirtual()); } - trailing_comments_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + trailing_comments_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_trailing_comments()) { - trailing_comments_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.trailing_comments(), + trailing_comments_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.trailing_comments(), GetArenaNoVirtual()); } // @@protoc_insertion_point(copy_constructor:google.protobuf.SourceCodeInfo.Location) } void SourceCodeInfo_Location::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base); - leading_comments_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - trailing_comments_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base); + leading_comments_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + trailing_comments_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } SourceCodeInfo_Location::~SourceCodeInfo_Location() { @@ -15209,29 +12589,29 @@ SourceCodeInfo_Location::~SourceCodeInfo_Location() { } void SourceCodeInfo_Location::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - leading_comments_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - trailing_comments_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + leading_comments_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + trailing_comments_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void SourceCodeInfo_Location::ArenaDtor(void* object) { SourceCodeInfo_Location* _this = reinterpret_cast< SourceCodeInfo_Location* >(object); (void)_this; } -void SourceCodeInfo_Location::RegisterArenaDtor(::google::protobuf::Arena*) { +void SourceCodeInfo_Location::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void SourceCodeInfo_Location::SetCachedSize(int size) const { _cached_size_.Set(size); } const SourceCodeInfo_Location& SourceCodeInfo_Location::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void SourceCodeInfo_Location::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.SourceCodeInfo.Location) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -15252,160 +12632,101 @@ void SourceCodeInfo_Location::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* SourceCodeInfo_Location::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* SourceCodeInfo_Location::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated int32 path = 1 [packed = true]; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) == 10) { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::internal::PackedInt32Parser; - object = msg->mutable_path(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } else if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - do { - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->add_path(value); - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 8 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_path(), ptr, ctx); + CHK_(ptr); + } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8) { + add_path(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated int32 span = 2 [packed = true]; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) == 18) { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::internal::PackedInt32Parser; - object = msg->mutable_span(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } else if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - do { - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->add_span(value); - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 16 && (ptr += 1)); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_span(), ptr, ctx); + CHK_(ptr); + } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16) { + add_span(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string leading_comments = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.SourceCodeInfo.Location.leading_comments"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_leading_comments(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_leading_comments(), ptr, ctx, "google.protobuf.SourceCodeInfo.Location.leading_comments"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string trailing_comments = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.SourceCodeInfo.Location.trailing_comments"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_trailing_comments(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_trailing_comments(), ptr, ctx, "google.protobuf.SourceCodeInfo.Location.trailing_comments"); + CHK_(ptr); + } else goto handle_unusual; + continue; // repeated string leading_detached_comments = 6; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 50) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.SourceCodeInfo.Location.leading_detached_comments"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->add_leading_detached_comments(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 50 && (ptr += 1)); - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 50)) { + ptr -= 1; + do { + ptr += 1; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_leading_detached_comments(), ptr, ctx, "google.protobuf.SourceCodeInfo.Location.leading_detached_comments"); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 50); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool SourceCodeInfo_Location::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.SourceCodeInfo.Location) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated int32 path = 1 [packed = true]; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPackedPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPackedPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, this->mutable_path()))); - } else if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadRepeatedPrimitiveNoInline< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + } else if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadRepeatedPrimitiveNoInline< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( 1, 10u, input, this->mutable_path()))); } else { goto handle_unusual; @@ -15415,13 +12736,13 @@ bool SourceCodeInfo_Location::MergePartialFromCodedStream( // repeated int32 span = 2 [packed = true]; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPackedPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPackedPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, this->mutable_span()))); - } else if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadRepeatedPrimitiveNoInline< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + } else if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadRepeatedPrimitiveNoInline< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( 1, 18u, input, this->mutable_span()))); } else { goto handle_unusual; @@ -15431,12 +12752,12 @@ bool SourceCodeInfo_Location::MergePartialFromCodedStream( // optional string leading_comments = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_leading_comments())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->leading_comments().data(), static_cast(this->leading_comments().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.SourceCodeInfo.Location.leading_comments"); } else { goto handle_unusual; @@ -15446,12 +12767,12 @@ bool SourceCodeInfo_Location::MergePartialFromCodedStream( // optional string trailing_comments = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (34 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_trailing_comments())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->trailing_comments().data(), static_cast(this->trailing_comments().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.SourceCodeInfo.Location.trailing_comments"); } else { goto handle_unusual; @@ -15461,13 +12782,13 @@ bool SourceCodeInfo_Location::MergePartialFromCodedStream( // repeated string leading_detached_comments = 6; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (50 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (50 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->add_leading_detached_comments())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->leading_detached_comments(this->leading_detached_comments_size() - 1).data(), static_cast(this->leading_detached_comments(this->leading_detached_comments_size() - 1).length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.SourceCodeInfo.Location.leading_detached_comments"); } else { goto handle_unusual; @@ -15480,7 +12801,7 @@ bool SourceCodeInfo_Location::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -15496,141 +12817,65 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void SourceCodeInfo_Location::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.SourceCodeInfo.Location) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated int32 path = 1 [packed = true]; - if (this->path_size() > 0) { - ::google::protobuf::internal::WireFormatLite::WriteTag(1, ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); - output->WriteVarint32(_path_cached_byte_size_.load( - std::memory_order_relaxed)); - } - for (int i = 0, n = this->path_size(); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteInt32NoTag( - this->path(i), output); - } - - // repeated int32 span = 2 [packed = true]; - if (this->span_size() > 0) { - ::google::protobuf::internal::WireFormatLite::WriteTag(2, ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); - output->WriteVarint32(_span_cached_byte_size_.load( - std::memory_order_relaxed)); - } - for (int i = 0, n = this->span_size(); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteInt32NoTag( - this->span(i), output); - } - - cached_has_bits = _has_bits_[0]; - // optional string leading_comments = 3; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->leading_comments().data(), static_cast(this->leading_comments().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.SourceCodeInfo.Location.leading_comments"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 3, this->leading_comments(), output); - } - - // optional string trailing_comments = 4; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->trailing_comments().data(), static_cast(this->trailing_comments().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.SourceCodeInfo.Location.trailing_comments"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 4, this->trailing_comments(), output); - } - - // repeated string leading_detached_comments = 6; - for (int i = 0, n = this->leading_detached_comments_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->leading_detached_comments(i).data(), static_cast(this->leading_detached_comments(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.SourceCodeInfo.Location.leading_detached_comments"); - ::google::protobuf::internal::WireFormatLite::WriteString( - 6, this->leading_detached_comments(i), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.SourceCodeInfo.Location) -} - -::google::protobuf::uint8* SourceCodeInfo_Location::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* SourceCodeInfo_Location::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.SourceCodeInfo.Location) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated int32 path = 1 [packed = true]; - if (this->path_size() > 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray( - 1, - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, - target); - target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray( - _path_cached_byte_size_.load(std::memory_order_relaxed), - target); - target = ::google::protobuf::internal::WireFormatLite:: - WriteInt32NoTagToArray(this->path_, target); + { + int byte_size = _path_cached_byte_size_.load(std::memory_order_relaxed); + if (byte_size > 0) { + target = stream->WriteInt32Packed( + 1, path_, byte_size, target); + } } // repeated int32 span = 2 [packed = true]; - if (this->span_size() > 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray( - 2, - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, - target); - target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray( - _span_cached_byte_size_.load(std::memory_order_relaxed), - target); - target = ::google::protobuf::internal::WireFormatLite:: - WriteInt32NoTagToArray(this->span_, target); + { + int byte_size = _span_cached_byte_size_.load(std::memory_order_relaxed); + if (byte_size > 0) { + target = stream->WriteInt32Packed( + 2, span_, byte_size, target); + } } cached_has_bits = _has_bits_[0]; // optional string leading_comments = 3; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->leading_comments().data(), static_cast(this->leading_comments().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.SourceCodeInfo.Location.leading_comments"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 3, this->leading_comments(), target); } // optional string trailing_comments = 4; if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->trailing_comments().data(), static_cast(this->trailing_comments().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.SourceCodeInfo.Location.trailing_comments"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 4, this->trailing_comments(), target); } // repeated string leading_detached_comments = 6; - for (int i = 0, n = this->leading_detached_comments_size(); i < n; i++) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->leading_detached_comments(i).data(), static_cast(this->leading_detached_comments(i).length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + for (auto it = this->leading_detached_comments().pointer_begin(), + end = this->leading_detached_comments().pointer_end(); it < end; ++it) { + const auto& s = **it; + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( + s.data(), static_cast(s.length()), + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.SourceCodeInfo.Location.leading_detached_comments"); - target = ::google::protobuf::internal::WireFormatLite:: - WriteStringToArray(6, this->leading_detached_comments(i), target); + target = stream->WriteString(6, s, target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.SourceCodeInfo.Location) return target; @@ -15640,25 +12885,20 @@ size_t SourceCodeInfo_Location::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.SourceCodeInfo.Location) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // repeated int32 path = 1 [packed = true]; { - size_t data_size = ::google::protobuf::internal::WireFormatLite:: + size_t data_size = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: Int32Size(this->path_); if (data_size > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( - static_cast<::google::protobuf::int32>(data_size)); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( + static_cast<::PROTOBUF_NAMESPACE_ID::int32>(data_size)); } - int cached_size = ::google::protobuf::internal::ToCachedSize(data_size); + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(data_size); _path_cached_byte_size_.store(cached_size, std::memory_order_relaxed); total_size += data_size; @@ -15666,14 +12906,14 @@ size_t SourceCodeInfo_Location::ByteSizeLong() const { // repeated int32 span = 2 [packed = true]; { - size_t data_size = ::google::protobuf::internal::WireFormatLite:: + size_t data_size = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: Int32Size(this->span_); if (data_size > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( - static_cast<::google::protobuf::int32>(data_size)); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( + static_cast<::PROTOBUF_NAMESPACE_ID::int32>(data_size)); } - int cached_size = ::google::protobuf::internal::ToCachedSize(data_size); + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(data_size); _span_cached_byte_size_.store(cached_size, std::memory_order_relaxed); total_size += data_size; @@ -15681,9 +12921,9 @@ size_t SourceCodeInfo_Location::ByteSizeLong() const { // repeated string leading_detached_comments = 6; total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->leading_detached_comments_size()); + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->leading_detached_comments_size()); for (int i = 0, n = this->leading_detached_comments_size(); i < n; i++) { - total_size += ::google::protobuf::internal::WireFormatLite::StringSize( + total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->leading_detached_comments(i)); } @@ -15692,32 +12932,36 @@ size_t SourceCodeInfo_Location::ByteSizeLong() const { // optional string leading_comments = 3; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->leading_comments()); } // optional string trailing_comments = 4; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->trailing_comments()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void SourceCodeInfo_Location::MergeFrom(const ::google::protobuf::Message& from) { +void SourceCodeInfo_Location::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.SourceCodeInfo.Location) GOOGLE_DCHECK_NE(&from, this); const SourceCodeInfo_Location* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.SourceCodeInfo.Location) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.SourceCodeInfo.Location) MergeFrom(*source); @@ -15728,7 +12972,7 @@ void SourceCodeInfo_Location::MergeFrom(const SourceCodeInfo_Location& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.SourceCodeInfo.Location) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; path_.MergeFrom(from.path_); @@ -15745,7 +12989,7 @@ void SourceCodeInfo_Location::MergeFrom(const SourceCodeInfo_Location& from) { } } -void SourceCodeInfo_Location::CopyFrom(const ::google::protobuf::Message& from) { +void SourceCodeInfo_Location::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.SourceCodeInfo.Location) if (&from == this) return; Clear(); @@ -15763,25 +13007,6 @@ bool SourceCodeInfo_Location::IsInitialized() const { return true; } -void SourceCodeInfo_Location::Swap(SourceCodeInfo_Location* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - SourceCodeInfo_Location* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void SourceCodeInfo_Location::UnsafeArenaSwap(SourceCodeInfo_Location* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void SourceCodeInfo_Location::InternalSwap(SourceCodeInfo_Location* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -15789,15 +13014,14 @@ void SourceCodeInfo_Location::InternalSwap(SourceCodeInfo_Location* other) { path_.InternalSwap(&other->path_); span_.InternalSwap(&other->span_); leading_detached_comments_.InternalSwap(CastToBase(&other->leading_detached_comments_)); - leading_comments_.Swap(&other->leading_comments_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + leading_comments_.Swap(&other->leading_comments_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); - trailing_comments_.Swap(&other->trailing_comments_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + trailing_comments_.Swap(&other->trailing_comments_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -::google::protobuf::Metadata SourceCodeInfo_Location::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata SourceCodeInfo_Location::GetMetadata() const { + return GetMetadataStatic(); } @@ -15805,21 +13029,18 @@ void SourceCodeInfo_Location::InternalSwap(SourceCodeInfo_Location* other) { void SourceCodeInfo::InitAsDefaultInstance() { } -class SourceCodeInfo::HasBitSetters { +class SourceCodeInfo::_Internal { public: + using HasBits = decltype(std::declval()._has_bits_); }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int SourceCodeInfo::kLocationFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - SourceCodeInfo::SourceCodeInfo() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.SourceCodeInfo) } -SourceCodeInfo::SourceCodeInfo(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +SourceCodeInfo::SourceCodeInfo(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), location_(arena) { SharedCtor(); @@ -15827,8 +13048,8 @@ SourceCodeInfo::SourceCodeInfo(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.SourceCodeInfo) } SourceCodeInfo::SourceCodeInfo(const SourceCodeInfo& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), location_(from.location_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -15836,8 +13057,7 @@ SourceCodeInfo::SourceCodeInfo(const SourceCodeInfo& from) } void SourceCodeInfo::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); } SourceCodeInfo::~SourceCodeInfo() { @@ -15846,27 +13066,27 @@ SourceCodeInfo::~SourceCodeInfo() { } void SourceCodeInfo::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void SourceCodeInfo::ArenaDtor(void* object) { SourceCodeInfo* _this = reinterpret_cast< SourceCodeInfo* >(object); (void)_this; } -void SourceCodeInfo::RegisterArenaDtor(::google::protobuf::Arena*) { +void SourceCodeInfo::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void SourceCodeInfo::SetCachedSize(int size) const { _cached_size_.Set(size); } const SourceCodeInfo& SourceCodeInfo::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void SourceCodeInfo::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.SourceCodeInfo) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -15876,74 +13096,60 @@ void SourceCodeInfo::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* SourceCodeInfo::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* SourceCodeInfo::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated .google.protobuf.SourceCodeInfo.Location location = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::SourceCodeInfo_Location::_InternalParse; - object = msg->add_location(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 10 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_location(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 10); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool SourceCodeInfo::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.SourceCodeInfo) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated .google.protobuf.SourceCodeInfo.Location location = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_location())); } else { goto handle_unusual; @@ -15956,7 +13162,7 @@ bool SourceCodeInfo::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -15972,46 +13178,23 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void SourceCodeInfo::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.SourceCodeInfo) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .google.protobuf.SourceCodeInfo.Location location = 1; - for (unsigned int i = 0, - n = static_cast(this->location_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 1, - this->location(static_cast(i)), - output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.SourceCodeInfo) -} - -::google::protobuf::uint8* SourceCodeInfo::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* SourceCodeInfo::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.SourceCodeInfo) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated .google.protobuf.SourceCodeInfo.Location location = 1; - for (unsigned int i = 0, - n = static_cast(this->location_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 1, this->location(static_cast(i)), deterministic, target); + for (auto it = this->location().pointer_begin(), + end = this->location().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(1, **it, target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.SourceCodeInfo) return target; @@ -16021,12 +13204,7 @@ size_t SourceCodeInfo::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.SourceCodeInfo) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -16036,25 +13214,29 @@ size_t SourceCodeInfo::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->location(static_cast(i))); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void SourceCodeInfo::MergeFrom(const ::google::protobuf::Message& from) { +void SourceCodeInfo::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.SourceCodeInfo) GOOGLE_DCHECK_NE(&from, this); const SourceCodeInfo* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.SourceCodeInfo) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.SourceCodeInfo) MergeFrom(*source); @@ -16065,13 +13247,13 @@ void SourceCodeInfo::MergeFrom(const SourceCodeInfo& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.SourceCodeInfo) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; location_.MergeFrom(from.location_); } -void SourceCodeInfo::CopyFrom(const ::google::protobuf::Message& from) { +void SourceCodeInfo::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.SourceCodeInfo) if (&from == this) return; Clear(); @@ -16089,25 +13271,6 @@ bool SourceCodeInfo::IsInitialized() const { return true; } -void SourceCodeInfo::Swap(SourceCodeInfo* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - SourceCodeInfo* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void SourceCodeInfo::UnsafeArenaSwap(SourceCodeInfo* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void SourceCodeInfo::InternalSwap(SourceCodeInfo* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -16115,9 +13278,8 @@ void SourceCodeInfo::InternalSwap(SourceCodeInfo* other) { CastToBase(&location_)->InternalSwap(CastToBase(&other->location_)); } -::google::protobuf::Metadata SourceCodeInfo::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata SourceCodeInfo::GetMetadata() const { + return GetMetadataStatic(); } @@ -16125,33 +13287,27 @@ void SourceCodeInfo::InternalSwap(SourceCodeInfo* other) { void GeneratedCodeInfo_Annotation::InitAsDefaultInstance() { } -class GeneratedCodeInfo_Annotation::HasBitSetters { +class GeneratedCodeInfo_Annotation::_Internal { public: - static void set_has_source_file(GeneratedCodeInfo_Annotation* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval()._has_bits_); + static void set_has_source_file(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_begin(GeneratedCodeInfo_Annotation* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_begin(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_end(GeneratedCodeInfo_Annotation* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_end(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int GeneratedCodeInfo_Annotation::kPathFieldNumber; -const int GeneratedCodeInfo_Annotation::kSourceFileFieldNumber; -const int GeneratedCodeInfo_Annotation::kBeginFieldNumber; -const int GeneratedCodeInfo_Annotation::kEndFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - GeneratedCodeInfo_Annotation::GeneratedCodeInfo_Annotation() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.GeneratedCodeInfo.Annotation) } -GeneratedCodeInfo_Annotation::GeneratedCodeInfo_Annotation(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +GeneratedCodeInfo_Annotation::GeneratedCodeInfo_Annotation(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), path_(arena) { SharedCtor(); @@ -16159,14 +13315,14 @@ GeneratedCodeInfo_Annotation::GeneratedCodeInfo_Annotation(::google::protobuf::A // @@protoc_insertion_point(arena_constructor:google.protobuf.GeneratedCodeInfo.Annotation) } GeneratedCodeInfo_Annotation::GeneratedCodeInfo_Annotation(const GeneratedCodeInfo_Annotation& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), path_(from.path_) { _internal_metadata_.MergeFrom(from._internal_metadata_); - source_file_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + source_file_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); if (from.has_source_file()) { - source_file_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.source_file(), + source_file_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.source_file(), GetArenaNoVirtual()); } ::memcpy(&begin_, &from.begin_, @@ -16176,9 +13332,8 @@ GeneratedCodeInfo_Annotation::GeneratedCodeInfo_Annotation(const GeneratedCodeIn } void GeneratedCodeInfo_Annotation::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base); - source_file_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base); + source_file_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); ::memset(&begin_, 0, static_cast( reinterpret_cast(&end_) - reinterpret_cast(&begin_)) + sizeof(end_)); @@ -16190,28 +13345,28 @@ GeneratedCodeInfo_Annotation::~GeneratedCodeInfo_Annotation() { } void GeneratedCodeInfo_Annotation::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); - source_file_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); + source_file_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void GeneratedCodeInfo_Annotation::ArenaDtor(void* object) { GeneratedCodeInfo_Annotation* _this = reinterpret_cast< GeneratedCodeInfo_Annotation* >(object); (void)_this; } -void GeneratedCodeInfo_Annotation::RegisterArenaDtor(::google::protobuf::Arena*) { +void GeneratedCodeInfo_Annotation::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void GeneratedCodeInfo_Annotation::SetCachedSize(int size) const { _cached_size_.Set(size); } const GeneratedCodeInfo_Annotation& GeneratedCodeInfo_Annotation::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void GeneratedCodeInfo_Annotation::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.GeneratedCodeInfo.Annotation) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -16230,122 +13385,88 @@ void GeneratedCodeInfo_Annotation::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* GeneratedCodeInfo_Annotation::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* GeneratedCodeInfo_Annotation::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + _Internal::HasBits has_bits{}; + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated int32 path = 1 [packed = true]; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) == 10) { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::internal::PackedInt32Parser; - object = msg->mutable_path(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } else if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - do { - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->add_path(value); - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 8 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_path(), ptr, ctx); + CHK_(ptr); + } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8) { + add_path(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional string source_file = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.GeneratedCodeInfo.Annotation.source_file"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8Verify; - ::std::string* str = msg->mutable_source_file(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_source_file(), ptr, ctx, "google.protobuf.GeneratedCodeInfo.Annotation.source_file"); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 begin = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_begin(value); - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { + _Internal::set_has_begin(&has_bits); + begin_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // optional int32 end = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 32) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_end(value); - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 32)) { + _Internal::set_has_end(&has_bits); + end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool GeneratedCodeInfo_Annotation::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.GeneratedCodeInfo.Annotation) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated int32 path = 1 [packed = true]; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPackedPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPackedPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, this->mutable_path()))); - } else if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadRepeatedPrimitiveNoInline< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + } else if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadRepeatedPrimitiveNoInline< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( 1, 10u, input, this->mutable_path()))); } else { goto handle_unusual; @@ -16355,12 +13476,12 @@ bool GeneratedCodeInfo_Annotation::MergePartialFromCodedStream( // optional string source_file = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (18 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_source_file())); - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->source_file().data(), static_cast(this->source_file().length()), - ::google::protobuf::internal::WireFormat::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::PARSE, "google.protobuf.GeneratedCodeInfo.Annotation.source_file"); } else { goto handle_unusual; @@ -16370,10 +13491,10 @@ bool GeneratedCodeInfo_Annotation::MergePartialFromCodedStream( // optional int32 begin = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_begin(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { + _Internal::set_has_begin(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &begin_))); } else { goto handle_unusual; @@ -16383,10 +13504,10 @@ bool GeneratedCodeInfo_Annotation::MergePartialFromCodedStream( // optional int32 end = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (32 & 0xFF)) { - HasBitSetters::set_has_end(this); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (32 & 0xFF)) { + _Internal::set_has_end(&_has_bits_); + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &end_))); } else { goto handle_unusual; @@ -16399,7 +13520,7 @@ bool GeneratedCodeInfo_Annotation::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -16415,96 +13536,47 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void GeneratedCodeInfo_Annotation::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.GeneratedCodeInfo.Annotation) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated int32 path = 1 [packed = true]; - if (this->path_size() > 0) { - ::google::protobuf::internal::WireFormatLite::WriteTag(1, ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); - output->WriteVarint32(_path_cached_byte_size_.load( - std::memory_order_relaxed)); - } - for (int i = 0, n = this->path_size(); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteInt32NoTag( - this->path(i), output); - } - - cached_has_bits = _has_bits_[0]; - // optional string source_file = 2; - if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( - this->source_file().data(), static_cast(this->source_file().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, - "google.protobuf.GeneratedCodeInfo.Annotation.source_file"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 2, this->source_file(), output); - } - - // optional int32 begin = 3; - if (cached_has_bits & 0x00000002u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->begin(), output); - } - - // optional int32 end = 4; - if (cached_has_bits & 0x00000004u) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(4, this->end(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.GeneratedCodeInfo.Annotation) -} - -::google::protobuf::uint8* GeneratedCodeInfo_Annotation::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* GeneratedCodeInfo_Annotation::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.GeneratedCodeInfo.Annotation) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated int32 path = 1 [packed = true]; - if (this->path_size() > 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray( - 1, - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, - target); - target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray( - _path_cached_byte_size_.load(std::memory_order_relaxed), - target); - target = ::google::protobuf::internal::WireFormatLite:: - WriteInt32NoTagToArray(this->path_, target); + { + int byte_size = _path_cached_byte_size_.load(std::memory_order_relaxed); + if (byte_size > 0) { + target = stream->WriteInt32Packed( + 1, path_, byte_size, target); + } } cached_has_bits = _has_bits_[0]; // optional string source_file = 2; if (cached_has_bits & 0x00000001u) { - ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::VerifyUTF8StringNamedField( this->source_file().data(), static_cast(this->source_file().length()), - ::google::protobuf::internal::WireFormat::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SERIALIZE, "google.protobuf.GeneratedCodeInfo.Annotation.source_file"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 2, this->source_file(), target); } // optional int32 begin = 3; if (cached_has_bits & 0x00000002u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->begin(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(3, this->begin(), target); } // optional int32 end = 4; if (cached_has_bits & 0x00000004u) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(4, this->end(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(4, this->end(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.GeneratedCodeInfo.Annotation) return target; @@ -16514,25 +13586,20 @@ size_t GeneratedCodeInfo_Annotation::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.GeneratedCodeInfo.Annotation) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // repeated int32 path = 1 [packed = true]; { - size_t data_size = ::google::protobuf::internal::WireFormatLite:: + size_t data_size = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: Int32Size(this->path_); if (data_size > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( - static_cast<::google::protobuf::int32>(data_size)); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( + static_cast<::PROTOBUF_NAMESPACE_ID::int32>(data_size)); } - int cached_size = ::google::protobuf::internal::ToCachedSize(data_size); + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(data_size); _path_cached_byte_size_.store(cached_size, std::memory_order_relaxed); total_size += data_size; @@ -16543,39 +13610,43 @@ size_t GeneratedCodeInfo_Annotation::ByteSizeLong() const { // optional string source_file = 2; if (cached_has_bits & 0x00000001u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->source_file()); } // optional int32 begin = 3; if (cached_has_bits & 0x00000002u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->begin()); } // optional int32 end = 4; if (cached_has_bits & 0x00000004u) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->end()); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void GeneratedCodeInfo_Annotation::MergeFrom(const ::google::protobuf::Message& from) { +void GeneratedCodeInfo_Annotation::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.GeneratedCodeInfo.Annotation) GOOGLE_DCHECK_NE(&from, this); const GeneratedCodeInfo_Annotation* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.GeneratedCodeInfo.Annotation) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.GeneratedCodeInfo.Annotation) MergeFrom(*source); @@ -16586,7 +13657,7 @@ void GeneratedCodeInfo_Annotation::MergeFrom(const GeneratedCodeInfo_Annotation& // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.GeneratedCodeInfo.Annotation) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; path_.MergeFrom(from.path_); @@ -16605,7 +13676,7 @@ void GeneratedCodeInfo_Annotation::MergeFrom(const GeneratedCodeInfo_Annotation& } } -void GeneratedCodeInfo_Annotation::CopyFrom(const ::google::protobuf::Message& from) { +void GeneratedCodeInfo_Annotation::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.GeneratedCodeInfo.Annotation) if (&from == this) return; Clear(); @@ -16623,39 +13694,19 @@ bool GeneratedCodeInfo_Annotation::IsInitialized() const { return true; } -void GeneratedCodeInfo_Annotation::Swap(GeneratedCodeInfo_Annotation* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - GeneratedCodeInfo_Annotation* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void GeneratedCodeInfo_Annotation::UnsafeArenaSwap(GeneratedCodeInfo_Annotation* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void GeneratedCodeInfo_Annotation::InternalSwap(GeneratedCodeInfo_Annotation* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); swap(_has_bits_[0], other->_has_bits_[0]); path_.InternalSwap(&other->path_); - source_file_.Swap(&other->source_file_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + source_file_.Swap(&other->source_file_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); swap(begin_, other->begin_); swap(end_, other->end_); } -::google::protobuf::Metadata GeneratedCodeInfo_Annotation::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata GeneratedCodeInfo_Annotation::GetMetadata() const { + return GetMetadataStatic(); } @@ -16663,21 +13714,18 @@ void GeneratedCodeInfo_Annotation::InternalSwap(GeneratedCodeInfo_Annotation* ot void GeneratedCodeInfo::InitAsDefaultInstance() { } -class GeneratedCodeInfo::HasBitSetters { +class GeneratedCodeInfo::_Internal { public: + using HasBits = decltype(std::declval()._has_bits_); }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int GeneratedCodeInfo::kAnnotationFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - GeneratedCodeInfo::GeneratedCodeInfo() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.GeneratedCodeInfo) } -GeneratedCodeInfo::GeneratedCodeInfo(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +GeneratedCodeInfo::GeneratedCodeInfo(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), annotation_(arena) { SharedCtor(); @@ -16685,8 +13733,8 @@ GeneratedCodeInfo::GeneratedCodeInfo(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.GeneratedCodeInfo) } GeneratedCodeInfo::GeneratedCodeInfo(const GeneratedCodeInfo& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), _has_bits_(from._has_bits_), annotation_(from.annotation_) { _internal_metadata_.MergeFrom(from._internal_metadata_); @@ -16694,8 +13742,7 @@ GeneratedCodeInfo::GeneratedCodeInfo(const GeneratedCodeInfo& from) } void GeneratedCodeInfo::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); } GeneratedCodeInfo::~GeneratedCodeInfo() { @@ -16704,27 +13751,27 @@ GeneratedCodeInfo::~GeneratedCodeInfo() { } void GeneratedCodeInfo::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void GeneratedCodeInfo::ArenaDtor(void* object) { GeneratedCodeInfo* _this = reinterpret_cast< GeneratedCodeInfo* >(object); (void)_this; } -void GeneratedCodeInfo::RegisterArenaDtor(::google::protobuf::Arena*) { +void GeneratedCodeInfo::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void GeneratedCodeInfo::SetCachedSize(int size) const { _cached_size_.Set(size); } const GeneratedCodeInfo& GeneratedCodeInfo::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); return *internal_default_instance(); } void GeneratedCodeInfo::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.GeneratedCodeInfo) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -16734,74 +13781,60 @@ void GeneratedCodeInfo::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* GeneratedCodeInfo::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* GeneratedCodeInfo::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated .google.protobuf.GeneratedCodeInfo.Annotation annotation = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::GeneratedCodeInfo_Annotation::_InternalParse; - object = msg->add_annotation(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 10 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_annotation(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 10); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool GeneratedCodeInfo::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.GeneratedCodeInfo) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated .google.protobuf.GeneratedCodeInfo.Annotation annotation = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_annotation())); } else { goto handle_unusual; @@ -16814,7 +13847,7 @@ bool GeneratedCodeInfo::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -16830,46 +13863,23 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void GeneratedCodeInfo::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.GeneratedCodeInfo) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .google.protobuf.GeneratedCodeInfo.Annotation annotation = 1; - for (unsigned int i = 0, - n = static_cast(this->annotation_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 1, - this->annotation(static_cast(i)), - output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.GeneratedCodeInfo) -} - -::google::protobuf::uint8* GeneratedCodeInfo::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* GeneratedCodeInfo::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.GeneratedCodeInfo) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated .google.protobuf.GeneratedCodeInfo.Annotation annotation = 1; - for (unsigned int i = 0, - n = static_cast(this->annotation_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 1, this->annotation(static_cast(i)), deterministic, target); + for (auto it = this->annotation().pointer_begin(), + end = this->annotation().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(1, **it, target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.GeneratedCodeInfo) return target; @@ -16879,12 +13889,7 @@ size_t GeneratedCodeInfo::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.GeneratedCodeInfo) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -16894,25 +13899,29 @@ size_t GeneratedCodeInfo::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->annotation(static_cast(i))); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void GeneratedCodeInfo::MergeFrom(const ::google::protobuf::Message& from) { +void GeneratedCodeInfo::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.GeneratedCodeInfo) GOOGLE_DCHECK_NE(&from, this); const GeneratedCodeInfo* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.GeneratedCodeInfo) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.GeneratedCodeInfo) MergeFrom(*source); @@ -16923,13 +13932,13 @@ void GeneratedCodeInfo::MergeFrom(const GeneratedCodeInfo& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.GeneratedCodeInfo) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; annotation_.MergeFrom(from.annotation_); } -void GeneratedCodeInfo::CopyFrom(const ::google::protobuf::Message& from) { +void GeneratedCodeInfo::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.GeneratedCodeInfo) if (&from == this) return; Clear(); @@ -16947,25 +13956,6 @@ bool GeneratedCodeInfo::IsInitialized() const { return true; } -void GeneratedCodeInfo::Swap(GeneratedCodeInfo* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - GeneratedCodeInfo* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void GeneratedCodeInfo::UnsafeArenaSwap(GeneratedCodeInfo* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void GeneratedCodeInfo::InternalSwap(GeneratedCodeInfo* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -16973,99 +13963,96 @@ void GeneratedCodeInfo::InternalSwap(GeneratedCodeInfo* other) { CastToBase(&annotation_)->InternalSwap(CastToBase(&other->annotation_)); } -::google::protobuf::Metadata GeneratedCodeInfo::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata GeneratedCodeInfo::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::FileDescriptorSet* Arena::CreateMaybeMessage< ::google::protobuf::FileDescriptorSet >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::FileDescriptorSet >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::FileDescriptorSet* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::FileDescriptorSet >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::FileDescriptorSet >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::FileDescriptorProto* Arena::CreateMaybeMessage< ::google::protobuf::FileDescriptorProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::FileDescriptorProto >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::FileDescriptorProto* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::DescriptorProto_ExtensionRange* Arena::CreateMaybeMessage< ::google::protobuf::DescriptorProto_ExtensionRange >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::DescriptorProto_ExtensionRange >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::DescriptorProto_ReservedRange* Arena::CreateMaybeMessage< ::google::protobuf::DescriptorProto_ReservedRange >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::DescriptorProto_ReservedRange >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::DescriptorProto* Arena::CreateMaybeMessage< ::google::protobuf::DescriptorProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::DescriptorProto >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::DescriptorProto* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::DescriptorProto >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::DescriptorProto >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::ExtensionRangeOptions* Arena::CreateMaybeMessage< ::google::protobuf::ExtensionRangeOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::ExtensionRangeOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::FieldDescriptorProto* Arena::CreateMaybeMessage< ::google::protobuf::FieldDescriptorProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::FieldDescriptorProto >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::OneofDescriptorProto* Arena::CreateMaybeMessage< ::google::protobuf::OneofDescriptorProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::OneofDescriptorProto >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::OneofDescriptorProto* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::OneofDescriptorProto >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::OneofDescriptorProto >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::EnumDescriptorProto_EnumReservedRange* Arena::CreateMaybeMessage< ::google::protobuf::EnumDescriptorProto_EnumReservedRange >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::EnumDescriptorProto_EnumReservedRange >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::EnumDescriptorProto* Arena::CreateMaybeMessage< ::google::protobuf::EnumDescriptorProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::EnumDescriptorProto >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::EnumValueDescriptorProto* Arena::CreateMaybeMessage< ::google::protobuf::EnumValueDescriptorProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::EnumValueDescriptorProto >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::ServiceDescriptorProto* Arena::CreateMaybeMessage< ::google::protobuf::ServiceDescriptorProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::ServiceDescriptorProto >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::MethodDescriptorProto* Arena::CreateMaybeMessage< ::google::protobuf::MethodDescriptorProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::MethodDescriptorProto >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::MethodDescriptorProto* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::MethodDescriptorProto >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::MethodDescriptorProto >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::FileOptions* Arena::CreateMaybeMessage< ::google::protobuf::FileOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::FileOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::FileOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::FileOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::FileOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::MessageOptions* Arena::CreateMaybeMessage< ::google::protobuf::MessageOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::MessageOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::MessageOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::MessageOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::MessageOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::FieldOptions* Arena::CreateMaybeMessage< ::google::protobuf::FieldOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::FieldOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::FieldOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::FieldOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::FieldOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::OneofOptions* Arena::CreateMaybeMessage< ::google::protobuf::OneofOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::OneofOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::OneofOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::OneofOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::OneofOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::EnumOptions* Arena::CreateMaybeMessage< ::google::protobuf::EnumOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::EnumOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::EnumOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::EnumOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::EnumOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::EnumValueOptions* Arena::CreateMaybeMessage< ::google::protobuf::EnumValueOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::EnumValueOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::EnumValueOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::EnumValueOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::EnumValueOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::ServiceOptions* Arena::CreateMaybeMessage< ::google::protobuf::ServiceOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::ServiceOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::ServiceOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::ServiceOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::ServiceOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::MethodOptions* Arena::CreateMaybeMessage< ::google::protobuf::MethodOptions >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::MethodOptions >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::MethodOptions* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::MethodOptions >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::MethodOptions >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::UninterpretedOption_NamePart* Arena::CreateMaybeMessage< ::google::protobuf::UninterpretedOption_NamePart >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::UninterpretedOption_NamePart >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::UninterpretedOption* Arena::CreateMaybeMessage< ::google::protobuf::UninterpretedOption >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::UninterpretedOption >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::UninterpretedOption* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::UninterpretedOption >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::UninterpretedOption >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::SourceCodeInfo_Location* Arena::CreateMaybeMessage< ::google::protobuf::SourceCodeInfo_Location >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::SourceCodeInfo_Location >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::SourceCodeInfo* Arena::CreateMaybeMessage< ::google::protobuf::SourceCodeInfo >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::SourceCodeInfo >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::SourceCodeInfo* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::SourceCodeInfo >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::SourceCodeInfo >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::GeneratedCodeInfo_Annotation* Arena::CreateMaybeMessage< ::google::protobuf::GeneratedCodeInfo_Annotation >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::GeneratedCodeInfo_Annotation >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::GeneratedCodeInfo* Arena::CreateMaybeMessage< ::google::protobuf::GeneratedCodeInfo >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::GeneratedCodeInfo >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/descriptor.pb.h b/src/google/protobuf/descriptor.pb.h index 3459a073c4..d2601e3b58 100644 --- a/src/google/protobuf/descriptor.pb.h +++ b/src/google/protobuf/descriptor.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/descriptor.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2fdescriptor_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2fdescriptor_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fdescriptor_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fdescriptor_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,6 +27,7 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export @@ -33,137 +35,141 @@ #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fdescriptor_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fdescriptor_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[27] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fdescriptor_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[27] + 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2fdescriptor_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fdescriptor_2eproto; +PROTOBUF_NAMESPACE_OPEN class DescriptorProto; class DescriptorProtoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern DescriptorProtoDefaultTypeInternal _DescriptorProto_default_instance_; +PROTOBUF_EXPORT extern DescriptorProtoDefaultTypeInternal _DescriptorProto_default_instance_; class DescriptorProto_ExtensionRange; class DescriptorProto_ExtensionRangeDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern DescriptorProto_ExtensionRangeDefaultTypeInternal _DescriptorProto_ExtensionRange_default_instance_; +PROTOBUF_EXPORT extern DescriptorProto_ExtensionRangeDefaultTypeInternal _DescriptorProto_ExtensionRange_default_instance_; class DescriptorProto_ReservedRange; class DescriptorProto_ReservedRangeDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern DescriptorProto_ReservedRangeDefaultTypeInternal _DescriptorProto_ReservedRange_default_instance_; +PROTOBUF_EXPORT extern DescriptorProto_ReservedRangeDefaultTypeInternal _DescriptorProto_ReservedRange_default_instance_; class EnumDescriptorProto; class EnumDescriptorProtoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern EnumDescriptorProtoDefaultTypeInternal _EnumDescriptorProto_default_instance_; +PROTOBUF_EXPORT extern EnumDescriptorProtoDefaultTypeInternal _EnumDescriptorProto_default_instance_; class EnumDescriptorProto_EnumReservedRange; class EnumDescriptorProto_EnumReservedRangeDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern EnumDescriptorProto_EnumReservedRangeDefaultTypeInternal _EnumDescriptorProto_EnumReservedRange_default_instance_; +PROTOBUF_EXPORT extern EnumDescriptorProto_EnumReservedRangeDefaultTypeInternal _EnumDescriptorProto_EnumReservedRange_default_instance_; class EnumOptions; class EnumOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern EnumOptionsDefaultTypeInternal _EnumOptions_default_instance_; +PROTOBUF_EXPORT extern EnumOptionsDefaultTypeInternal _EnumOptions_default_instance_; class EnumValueDescriptorProto; class EnumValueDescriptorProtoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern EnumValueDescriptorProtoDefaultTypeInternal _EnumValueDescriptorProto_default_instance_; +PROTOBUF_EXPORT extern EnumValueDescriptorProtoDefaultTypeInternal _EnumValueDescriptorProto_default_instance_; class EnumValueOptions; class EnumValueOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern EnumValueOptionsDefaultTypeInternal _EnumValueOptions_default_instance_; +PROTOBUF_EXPORT extern EnumValueOptionsDefaultTypeInternal _EnumValueOptions_default_instance_; class ExtensionRangeOptions; class ExtensionRangeOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern ExtensionRangeOptionsDefaultTypeInternal _ExtensionRangeOptions_default_instance_; +PROTOBUF_EXPORT extern ExtensionRangeOptionsDefaultTypeInternal _ExtensionRangeOptions_default_instance_; class FieldDescriptorProto; class FieldDescriptorProtoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern FieldDescriptorProtoDefaultTypeInternal _FieldDescriptorProto_default_instance_; +PROTOBUF_EXPORT extern FieldDescriptorProtoDefaultTypeInternal _FieldDescriptorProto_default_instance_; class FieldOptions; class FieldOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern FieldOptionsDefaultTypeInternal _FieldOptions_default_instance_; +PROTOBUF_EXPORT extern FieldOptionsDefaultTypeInternal _FieldOptions_default_instance_; class FileDescriptorProto; class FileDescriptorProtoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern FileDescriptorProtoDefaultTypeInternal _FileDescriptorProto_default_instance_; +PROTOBUF_EXPORT extern FileDescriptorProtoDefaultTypeInternal _FileDescriptorProto_default_instance_; class FileDescriptorSet; class FileDescriptorSetDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern FileDescriptorSetDefaultTypeInternal _FileDescriptorSet_default_instance_; +PROTOBUF_EXPORT extern FileDescriptorSetDefaultTypeInternal _FileDescriptorSet_default_instance_; class FileOptions; class FileOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern FileOptionsDefaultTypeInternal _FileOptions_default_instance_; +PROTOBUF_EXPORT extern FileOptionsDefaultTypeInternal _FileOptions_default_instance_; class GeneratedCodeInfo; class GeneratedCodeInfoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern GeneratedCodeInfoDefaultTypeInternal _GeneratedCodeInfo_default_instance_; +PROTOBUF_EXPORT extern GeneratedCodeInfoDefaultTypeInternal _GeneratedCodeInfo_default_instance_; class GeneratedCodeInfo_Annotation; class GeneratedCodeInfo_AnnotationDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern GeneratedCodeInfo_AnnotationDefaultTypeInternal _GeneratedCodeInfo_Annotation_default_instance_; +PROTOBUF_EXPORT extern GeneratedCodeInfo_AnnotationDefaultTypeInternal _GeneratedCodeInfo_Annotation_default_instance_; class MessageOptions; class MessageOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern MessageOptionsDefaultTypeInternal _MessageOptions_default_instance_; +PROTOBUF_EXPORT extern MessageOptionsDefaultTypeInternal _MessageOptions_default_instance_; class MethodDescriptorProto; class MethodDescriptorProtoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern MethodDescriptorProtoDefaultTypeInternal _MethodDescriptorProto_default_instance_; +PROTOBUF_EXPORT extern MethodDescriptorProtoDefaultTypeInternal _MethodDescriptorProto_default_instance_; class MethodOptions; class MethodOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern MethodOptionsDefaultTypeInternal _MethodOptions_default_instance_; +PROTOBUF_EXPORT extern MethodOptionsDefaultTypeInternal _MethodOptions_default_instance_; class OneofDescriptorProto; class OneofDescriptorProtoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern OneofDescriptorProtoDefaultTypeInternal _OneofDescriptorProto_default_instance_; +PROTOBUF_EXPORT extern OneofDescriptorProtoDefaultTypeInternal _OneofDescriptorProto_default_instance_; class OneofOptions; class OneofOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern OneofOptionsDefaultTypeInternal _OneofOptions_default_instance_; +PROTOBUF_EXPORT extern OneofOptionsDefaultTypeInternal _OneofOptions_default_instance_; class ServiceDescriptorProto; class ServiceDescriptorProtoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern ServiceDescriptorProtoDefaultTypeInternal _ServiceDescriptorProto_default_instance_; +PROTOBUF_EXPORT extern ServiceDescriptorProtoDefaultTypeInternal _ServiceDescriptorProto_default_instance_; class ServiceOptions; class ServiceOptionsDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern ServiceOptionsDefaultTypeInternal _ServiceOptions_default_instance_; +PROTOBUF_EXPORT extern ServiceOptionsDefaultTypeInternal _ServiceOptions_default_instance_; class SourceCodeInfo; class SourceCodeInfoDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern SourceCodeInfoDefaultTypeInternal _SourceCodeInfo_default_instance_; +PROTOBUF_EXPORT extern SourceCodeInfoDefaultTypeInternal _SourceCodeInfo_default_instance_; class SourceCodeInfo_Location; class SourceCodeInfo_LocationDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern SourceCodeInfo_LocationDefaultTypeInternal _SourceCodeInfo_Location_default_instance_; +PROTOBUF_EXPORT extern SourceCodeInfo_LocationDefaultTypeInternal _SourceCodeInfo_Location_default_instance_; class UninterpretedOption; class UninterpretedOptionDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern UninterpretedOptionDefaultTypeInternal _UninterpretedOption_default_instance_; +PROTOBUF_EXPORT extern UninterpretedOptionDefaultTypeInternal _UninterpretedOption_default_instance_; class UninterpretedOption_NamePart; class UninterpretedOption_NamePartDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern UninterpretedOption_NamePartDefaultTypeInternal _UninterpretedOption_NamePart_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::DescriptorProto* Arena::CreateMaybeMessage<::google::protobuf::DescriptorProto>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::DescriptorProto_ExtensionRange* Arena::CreateMaybeMessage<::google::protobuf::DescriptorProto_ExtensionRange>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::DescriptorProto_ReservedRange* Arena::CreateMaybeMessage<::google::protobuf::DescriptorProto_ReservedRange>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::EnumDescriptorProto* Arena::CreateMaybeMessage<::google::protobuf::EnumDescriptorProto>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::EnumDescriptorProto_EnumReservedRange* Arena::CreateMaybeMessage<::google::protobuf::EnumDescriptorProto_EnumReservedRange>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::EnumOptions* Arena::CreateMaybeMessage<::google::protobuf::EnumOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::EnumValueDescriptorProto* Arena::CreateMaybeMessage<::google::protobuf::EnumValueDescriptorProto>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::EnumValueOptions* Arena::CreateMaybeMessage<::google::protobuf::EnumValueOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::ExtensionRangeOptions* Arena::CreateMaybeMessage<::google::protobuf::ExtensionRangeOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::FieldDescriptorProto* Arena::CreateMaybeMessage<::google::protobuf::FieldDescriptorProto>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::FieldOptions* Arena::CreateMaybeMessage<::google::protobuf::FieldOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::FileDescriptorProto* Arena::CreateMaybeMessage<::google::protobuf::FileDescriptorProto>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::FileDescriptorSet* Arena::CreateMaybeMessage<::google::protobuf::FileDescriptorSet>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::FileOptions* Arena::CreateMaybeMessage<::google::protobuf::FileOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::GeneratedCodeInfo* Arena::CreateMaybeMessage<::google::protobuf::GeneratedCodeInfo>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::GeneratedCodeInfo_Annotation* Arena::CreateMaybeMessage<::google::protobuf::GeneratedCodeInfo_Annotation>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::MessageOptions* Arena::CreateMaybeMessage<::google::protobuf::MessageOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::MethodDescriptorProto* Arena::CreateMaybeMessage<::google::protobuf::MethodDescriptorProto>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::MethodOptions* Arena::CreateMaybeMessage<::google::protobuf::MethodOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::OneofDescriptorProto* Arena::CreateMaybeMessage<::google::protobuf::OneofDescriptorProto>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::OneofOptions* Arena::CreateMaybeMessage<::google::protobuf::OneofOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::ServiceDescriptorProto* Arena::CreateMaybeMessage<::google::protobuf::ServiceDescriptorProto>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::ServiceOptions* Arena::CreateMaybeMessage<::google::protobuf::ServiceOptions>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::SourceCodeInfo* Arena::CreateMaybeMessage<::google::protobuf::SourceCodeInfo>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::SourceCodeInfo_Location* Arena::CreateMaybeMessage<::google::protobuf::SourceCodeInfo_Location>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::UninterpretedOption* Arena::CreateMaybeMessage<::google::protobuf::UninterpretedOption>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::UninterpretedOption_NamePart* Arena::CreateMaybeMessage<::google::protobuf::UninterpretedOption_NamePart>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { - -enum FieldDescriptorProto_Type { +PROTOBUF_EXPORT extern UninterpretedOption_NamePartDefaultTypeInternal _UninterpretedOption_NamePart_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::DescriptorProto* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::EnumOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::EnumValueOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::FieldOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::FileDescriptorProto* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::FileDescriptorSet* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::FileOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::MessageOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::MethodDescriptorProto* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::MethodOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::OneofDescriptorProto* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::OneofOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::ServiceOptions* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::SourceCodeInfo* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::UninterpretedOption* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN + +enum FieldDescriptorProto_Type : int { FieldDescriptorProto_Type_TYPE_DOUBLE = 1, FieldDescriptorProto_Type_TYPE_FLOAT = 2, FieldDescriptorProto_Type_TYPE_INT64 = 3, @@ -183,140 +189,163 @@ enum FieldDescriptorProto_Type { FieldDescriptorProto_Type_TYPE_SINT32 = 17, FieldDescriptorProto_Type_TYPE_SINT64 = 18 }; -LIBPROTOBUF_EXPORT bool FieldDescriptorProto_Type_IsValid(int value); -const FieldDescriptorProto_Type FieldDescriptorProto_Type_Type_MIN = FieldDescriptorProto_Type_TYPE_DOUBLE; -const FieldDescriptorProto_Type FieldDescriptorProto_Type_Type_MAX = FieldDescriptorProto_Type_TYPE_SINT64; -const int FieldDescriptorProto_Type_Type_ARRAYSIZE = FieldDescriptorProto_Type_Type_MAX + 1; - -LIBPROTOBUF_EXPORT const ::google::protobuf::EnumDescriptor* FieldDescriptorProto_Type_descriptor(); -inline const ::std::string& FieldDescriptorProto_Type_Name(FieldDescriptorProto_Type value) { - return ::google::protobuf::internal::NameOfEnum( - FieldDescriptorProto_Type_descriptor(), value); +PROTOBUF_EXPORT bool FieldDescriptorProto_Type_IsValid(int value); +constexpr FieldDescriptorProto_Type FieldDescriptorProto_Type_Type_MIN = FieldDescriptorProto_Type_TYPE_DOUBLE; +constexpr FieldDescriptorProto_Type FieldDescriptorProto_Type_Type_MAX = FieldDescriptorProto_Type_TYPE_SINT64; +constexpr int FieldDescriptorProto_Type_Type_ARRAYSIZE = FieldDescriptorProto_Type_Type_MAX + 1; + +PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldDescriptorProto_Type_descriptor(); +template +inline const std::string& FieldDescriptorProto_Type_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function FieldDescriptorProto_Type_Name."); + return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( + FieldDescriptorProto_Type_descriptor(), enum_t_value); } inline bool FieldDescriptorProto_Type_Parse( - const ::std::string& name, FieldDescriptorProto_Type* value) { - return ::google::protobuf::internal::ParseNamedEnum( + const std::string& name, FieldDescriptorProto_Type* value) { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FieldDescriptorProto_Type_descriptor(), name, value); } -enum FieldDescriptorProto_Label { +enum FieldDescriptorProto_Label : int { FieldDescriptorProto_Label_LABEL_OPTIONAL = 1, FieldDescriptorProto_Label_LABEL_REQUIRED = 2, FieldDescriptorProto_Label_LABEL_REPEATED = 3 }; -LIBPROTOBUF_EXPORT bool FieldDescriptorProto_Label_IsValid(int value); -const FieldDescriptorProto_Label FieldDescriptorProto_Label_Label_MIN = FieldDescriptorProto_Label_LABEL_OPTIONAL; -const FieldDescriptorProto_Label FieldDescriptorProto_Label_Label_MAX = FieldDescriptorProto_Label_LABEL_REPEATED; -const int FieldDescriptorProto_Label_Label_ARRAYSIZE = FieldDescriptorProto_Label_Label_MAX + 1; - -LIBPROTOBUF_EXPORT const ::google::protobuf::EnumDescriptor* FieldDescriptorProto_Label_descriptor(); -inline const ::std::string& FieldDescriptorProto_Label_Name(FieldDescriptorProto_Label value) { - return ::google::protobuf::internal::NameOfEnum( - FieldDescriptorProto_Label_descriptor(), value); +PROTOBUF_EXPORT bool FieldDescriptorProto_Label_IsValid(int value); +constexpr FieldDescriptorProto_Label FieldDescriptorProto_Label_Label_MIN = FieldDescriptorProto_Label_LABEL_OPTIONAL; +constexpr FieldDescriptorProto_Label FieldDescriptorProto_Label_Label_MAX = FieldDescriptorProto_Label_LABEL_REPEATED; +constexpr int FieldDescriptorProto_Label_Label_ARRAYSIZE = FieldDescriptorProto_Label_Label_MAX + 1; + +PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldDescriptorProto_Label_descriptor(); +template +inline const std::string& FieldDescriptorProto_Label_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function FieldDescriptorProto_Label_Name."); + return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( + FieldDescriptorProto_Label_descriptor(), enum_t_value); } inline bool FieldDescriptorProto_Label_Parse( - const ::std::string& name, FieldDescriptorProto_Label* value) { - return ::google::protobuf::internal::ParseNamedEnum( + const std::string& name, FieldDescriptorProto_Label* value) { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FieldDescriptorProto_Label_descriptor(), name, value); } -enum FileOptions_OptimizeMode { +enum FileOptions_OptimizeMode : int { FileOptions_OptimizeMode_SPEED = 1, FileOptions_OptimizeMode_CODE_SIZE = 2, FileOptions_OptimizeMode_LITE_RUNTIME = 3 }; -LIBPROTOBUF_EXPORT bool FileOptions_OptimizeMode_IsValid(int value); -const FileOptions_OptimizeMode FileOptions_OptimizeMode_OptimizeMode_MIN = FileOptions_OptimizeMode_SPEED; -const FileOptions_OptimizeMode FileOptions_OptimizeMode_OptimizeMode_MAX = FileOptions_OptimizeMode_LITE_RUNTIME; -const int FileOptions_OptimizeMode_OptimizeMode_ARRAYSIZE = FileOptions_OptimizeMode_OptimizeMode_MAX + 1; - -LIBPROTOBUF_EXPORT const ::google::protobuf::EnumDescriptor* FileOptions_OptimizeMode_descriptor(); -inline const ::std::string& FileOptions_OptimizeMode_Name(FileOptions_OptimizeMode value) { - return ::google::protobuf::internal::NameOfEnum( - FileOptions_OptimizeMode_descriptor(), value); +PROTOBUF_EXPORT bool FileOptions_OptimizeMode_IsValid(int value); +constexpr FileOptions_OptimizeMode FileOptions_OptimizeMode_OptimizeMode_MIN = FileOptions_OptimizeMode_SPEED; +constexpr FileOptions_OptimizeMode FileOptions_OptimizeMode_OptimizeMode_MAX = FileOptions_OptimizeMode_LITE_RUNTIME; +constexpr int FileOptions_OptimizeMode_OptimizeMode_ARRAYSIZE = FileOptions_OptimizeMode_OptimizeMode_MAX + 1; + +PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FileOptions_OptimizeMode_descriptor(); +template +inline const std::string& FileOptions_OptimizeMode_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function FileOptions_OptimizeMode_Name."); + return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( + FileOptions_OptimizeMode_descriptor(), enum_t_value); } inline bool FileOptions_OptimizeMode_Parse( - const ::std::string& name, FileOptions_OptimizeMode* value) { - return ::google::protobuf::internal::ParseNamedEnum( + const std::string& name, FileOptions_OptimizeMode* value) { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FileOptions_OptimizeMode_descriptor(), name, value); } -enum FieldOptions_CType { +enum FieldOptions_CType : int { FieldOptions_CType_STRING = 0, FieldOptions_CType_CORD = 1, FieldOptions_CType_STRING_PIECE = 2 }; -LIBPROTOBUF_EXPORT bool FieldOptions_CType_IsValid(int value); -const FieldOptions_CType FieldOptions_CType_CType_MIN = FieldOptions_CType_STRING; -const FieldOptions_CType FieldOptions_CType_CType_MAX = FieldOptions_CType_STRING_PIECE; -const int FieldOptions_CType_CType_ARRAYSIZE = FieldOptions_CType_CType_MAX + 1; - -LIBPROTOBUF_EXPORT const ::google::protobuf::EnumDescriptor* FieldOptions_CType_descriptor(); -inline const ::std::string& FieldOptions_CType_Name(FieldOptions_CType value) { - return ::google::protobuf::internal::NameOfEnum( - FieldOptions_CType_descriptor(), value); +PROTOBUF_EXPORT bool FieldOptions_CType_IsValid(int value); +constexpr FieldOptions_CType FieldOptions_CType_CType_MIN = FieldOptions_CType_STRING; +constexpr FieldOptions_CType FieldOptions_CType_CType_MAX = FieldOptions_CType_STRING_PIECE; +constexpr int FieldOptions_CType_CType_ARRAYSIZE = FieldOptions_CType_CType_MAX + 1; + +PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldOptions_CType_descriptor(); +template +inline const std::string& FieldOptions_CType_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function FieldOptions_CType_Name."); + return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( + FieldOptions_CType_descriptor(), enum_t_value); } inline bool FieldOptions_CType_Parse( - const ::std::string& name, FieldOptions_CType* value) { - return ::google::protobuf::internal::ParseNamedEnum( + const std::string& name, FieldOptions_CType* value) { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FieldOptions_CType_descriptor(), name, value); } -enum FieldOptions_JSType { +enum FieldOptions_JSType : int { FieldOptions_JSType_JS_NORMAL = 0, FieldOptions_JSType_JS_STRING = 1, FieldOptions_JSType_JS_NUMBER = 2 }; -LIBPROTOBUF_EXPORT bool FieldOptions_JSType_IsValid(int value); -const FieldOptions_JSType FieldOptions_JSType_JSType_MIN = FieldOptions_JSType_JS_NORMAL; -const FieldOptions_JSType FieldOptions_JSType_JSType_MAX = FieldOptions_JSType_JS_NUMBER; -const int FieldOptions_JSType_JSType_ARRAYSIZE = FieldOptions_JSType_JSType_MAX + 1; - -LIBPROTOBUF_EXPORT const ::google::protobuf::EnumDescriptor* FieldOptions_JSType_descriptor(); -inline const ::std::string& FieldOptions_JSType_Name(FieldOptions_JSType value) { - return ::google::protobuf::internal::NameOfEnum( - FieldOptions_JSType_descriptor(), value); +PROTOBUF_EXPORT bool FieldOptions_JSType_IsValid(int value); +constexpr FieldOptions_JSType FieldOptions_JSType_JSType_MIN = FieldOptions_JSType_JS_NORMAL; +constexpr FieldOptions_JSType FieldOptions_JSType_JSType_MAX = FieldOptions_JSType_JS_NUMBER; +constexpr int FieldOptions_JSType_JSType_ARRAYSIZE = FieldOptions_JSType_JSType_MAX + 1; + +PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldOptions_JSType_descriptor(); +template +inline const std::string& FieldOptions_JSType_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function FieldOptions_JSType_Name."); + return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( + FieldOptions_JSType_descriptor(), enum_t_value); } inline bool FieldOptions_JSType_Parse( - const ::std::string& name, FieldOptions_JSType* value) { - return ::google::protobuf::internal::ParseNamedEnum( + const std::string& name, FieldOptions_JSType* value) { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( FieldOptions_JSType_descriptor(), name, value); } -enum MethodOptions_IdempotencyLevel { +enum MethodOptions_IdempotencyLevel : int { MethodOptions_IdempotencyLevel_IDEMPOTENCY_UNKNOWN = 0, MethodOptions_IdempotencyLevel_NO_SIDE_EFFECTS = 1, MethodOptions_IdempotencyLevel_IDEMPOTENT = 2 }; -LIBPROTOBUF_EXPORT bool MethodOptions_IdempotencyLevel_IsValid(int value); -const MethodOptions_IdempotencyLevel MethodOptions_IdempotencyLevel_IdempotencyLevel_MIN = MethodOptions_IdempotencyLevel_IDEMPOTENCY_UNKNOWN; -const MethodOptions_IdempotencyLevel MethodOptions_IdempotencyLevel_IdempotencyLevel_MAX = MethodOptions_IdempotencyLevel_IDEMPOTENT; -const int MethodOptions_IdempotencyLevel_IdempotencyLevel_ARRAYSIZE = MethodOptions_IdempotencyLevel_IdempotencyLevel_MAX + 1; - -LIBPROTOBUF_EXPORT const ::google::protobuf::EnumDescriptor* MethodOptions_IdempotencyLevel_descriptor(); -inline const ::std::string& MethodOptions_IdempotencyLevel_Name(MethodOptions_IdempotencyLevel value) { - return ::google::protobuf::internal::NameOfEnum( - MethodOptions_IdempotencyLevel_descriptor(), value); +PROTOBUF_EXPORT bool MethodOptions_IdempotencyLevel_IsValid(int value); +constexpr MethodOptions_IdempotencyLevel MethodOptions_IdempotencyLevel_IdempotencyLevel_MIN = MethodOptions_IdempotencyLevel_IDEMPOTENCY_UNKNOWN; +constexpr MethodOptions_IdempotencyLevel MethodOptions_IdempotencyLevel_IdempotencyLevel_MAX = MethodOptions_IdempotencyLevel_IDEMPOTENT; +constexpr int MethodOptions_IdempotencyLevel_IdempotencyLevel_ARRAYSIZE = MethodOptions_IdempotencyLevel_IdempotencyLevel_MAX + 1; + +PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* MethodOptions_IdempotencyLevel_descriptor(); +template +inline const std::string& MethodOptions_IdempotencyLevel_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function MethodOptions_IdempotencyLevel_Name."); + return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( + MethodOptions_IdempotencyLevel_descriptor(), enum_t_value); } inline bool MethodOptions_IdempotencyLevel_Parse( - const ::std::string& name, MethodOptions_IdempotencyLevel* value) { - return ::google::protobuf::internal::ParseNamedEnum( + const std::string& name, MethodOptions_IdempotencyLevel* value) { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( MethodOptions_IdempotencyLevel_descriptor(), name, value); } // =================================================================== -class LIBPROTOBUF_EXPORT FileDescriptorSet : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FileDescriptorSet) */ { +class PROTOBUF_EXPORT FileDescriptorSet : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FileDescriptorSet) */ { public: FileDescriptorSet(); virtual ~FileDescriptorSet(); FileDescriptorSet(const FileDescriptorSet& from); - - inline FileDescriptorSet& operator=(const FileDescriptorSet& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 FileDescriptorSet(FileDescriptorSet&& from) noexcept : FileDescriptorSet() { *this = ::std::move(from); } + inline FileDescriptorSet& operator=(const FileDescriptorSet& from) { + CopyFrom(from); + return *this; + } inline FileDescriptorSet& operator=(FileDescriptorSet&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -325,22 +354,28 @@ class LIBPROTOBUF_EXPORT FileDescriptorSet : public ::google::protobuf::Message } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const FileDescriptorSet& default_instance(); @@ -352,54 +387,66 @@ class LIBPROTOBUF_EXPORT FileDescriptorSet : public ::google::protobuf::Message static constexpr int kIndexInFileMessages = 0; - void UnsafeArenaSwap(FileDescriptorSet* other); - void Swap(FileDescriptorSet* other); friend void swap(FileDescriptorSet& a, FileDescriptorSet& b) { a.Swap(&b); } + inline void Swap(FileDescriptorSet* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(FileDescriptorSet* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline FileDescriptorSet* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - FileDescriptorSet* New(::google::protobuf::Arena* arena) const final { + FileDescriptorSet* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const FileDescriptorSet& from); void MergeFrom(const FileDescriptorSet& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(FileDescriptorSet* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.FileDescriptorSet"; + } protected: - explicit FileDescriptorSet(::google::protobuf::Arena* arena); + explicit FileDescriptorSet(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -407,56 +454,64 @@ class LIBPROTOBUF_EXPORT FileDescriptorSet : public ::google::protobuf::Message } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kFileFieldNumber = 1, + }; // repeated .google.protobuf.FileDescriptorProto file = 1; int file_size() const; void clear_file(); - static const int kFileFieldNumber = 1; - ::google::protobuf::FileDescriptorProto* mutable_file(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto >* + PROTOBUF_NAMESPACE_ID::FileDescriptorProto* mutable_file(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >* mutable_file(); - const ::google::protobuf::FileDescriptorProto& file(int index) const; - ::google::protobuf::FileDescriptorProto* add_file(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::FileDescriptorProto& file(int index) const; + PROTOBUF_NAMESPACE_ID::FileDescriptorProto* add_file(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >& file() const; // @@protoc_insertion_point(class_scope:google.protobuf.FileDescriptorSet) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto > file_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto > file_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FileDescriptorProto) */ { +class PROTOBUF_EXPORT FileDescriptorProto : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FileDescriptorProto) */ { public: FileDescriptorProto(); virtual ~FileDescriptorProto(); FileDescriptorProto(const FileDescriptorProto& from); - - inline FileDescriptorProto& operator=(const FileDescriptorProto& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 FileDescriptorProto(FileDescriptorProto&& from) noexcept : FileDescriptorProto() { *this = ::std::move(from); } + inline FileDescriptorProto& operator=(const FileDescriptorProto& from) { + CopyFrom(from); + return *this; + } inline FileDescriptorProto& operator=(FileDescriptorProto&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -465,22 +520,28 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const FileDescriptorProto& default_instance(); @@ -492,54 +553,66 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag static constexpr int kIndexInFileMessages = 1; - void UnsafeArenaSwap(FileDescriptorProto* other); - void Swap(FileDescriptorProto* other); friend void swap(FileDescriptorProto& a, FileDescriptorProto& b) { a.Swap(&b); } + inline void Swap(FileDescriptorProto* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(FileDescriptorProto* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline FileDescriptorProto* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - FileDescriptorProto* New(::google::protobuf::Arena* arena) const final { + FileDescriptorProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const FileDescriptorProto& from); void MergeFrom(const FileDescriptorProto& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(FileDescriptorProto* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.FileDescriptorProto"; + } protected: - explicit FileDescriptorProto(::google::protobuf::Arena* arena); + explicit FileDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -547,245 +620,243 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kDependencyFieldNumber = 3, + kMessageTypeFieldNumber = 4, + kEnumTypeFieldNumber = 5, + kServiceFieldNumber = 6, + kExtensionFieldNumber = 7, + kPublicDependencyFieldNumber = 10, + kWeakDependencyFieldNumber = 11, + kNameFieldNumber = 1, + kPackageFieldNumber = 2, + kSyntaxFieldNumber = 12, + kOptionsFieldNumber = 8, + kSourceCodeInfoFieldNumber = 9, + }; // repeated string dependency = 3; int dependency_size() const; void clear_dependency(); - static const int kDependencyFieldNumber = 3; - const ::std::string& dependency(int index) const; - ::std::string* mutable_dependency(int index); - void set_dependency(int index, const ::std::string& value); - #if LANG_CXX11 - void set_dependency(int index, ::std::string&& value); - #endif + const std::string& dependency(int index) const; + std::string* mutable_dependency(int index); + void set_dependency(int index, const std::string& value); + void set_dependency(int index, std::string&& value); void set_dependency(int index, const char* value); void set_dependency(int index, const char* value, size_t size); - ::std::string* add_dependency(); - void add_dependency(const ::std::string& value); - #if LANG_CXX11 - void add_dependency(::std::string&& value); - #endif + std::string* add_dependency(); + void add_dependency(const std::string& value); + void add_dependency(std::string&& value); void add_dependency(const char* value); void add_dependency(const char* value, size_t size); - const ::google::protobuf::RepeatedPtrField<::std::string>& dependency() const; - ::google::protobuf::RepeatedPtrField<::std::string>* mutable_dependency(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& dependency() const; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_dependency(); // repeated .google.protobuf.DescriptorProto message_type = 4; int message_type_size() const; void clear_message_type(); - static const int kMessageTypeFieldNumber = 4; - ::google::protobuf::DescriptorProto* mutable_message_type(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto >* + PROTOBUF_NAMESPACE_ID::DescriptorProto* mutable_message_type(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto >* mutable_message_type(); - const ::google::protobuf::DescriptorProto& message_type(int index) const; - ::google::protobuf::DescriptorProto* add_message_type(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto >& + const PROTOBUF_NAMESPACE_ID::DescriptorProto& message_type(int index) const; + PROTOBUF_NAMESPACE_ID::DescriptorProto* add_message_type(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto >& message_type() const; // repeated .google.protobuf.EnumDescriptorProto enum_type = 5; int enum_type_size() const; void clear_enum_type(); - static const int kEnumTypeFieldNumber = 5; - ::google::protobuf::EnumDescriptorProto* mutable_enum_type(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto >* + PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* mutable_enum_type(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >* mutable_enum_type(); - const ::google::protobuf::EnumDescriptorProto& enum_type(int index) const; - ::google::protobuf::EnumDescriptorProto* add_enum_type(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::EnumDescriptorProto& enum_type(int index) const; + PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* add_enum_type(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >& enum_type() const; // repeated .google.protobuf.ServiceDescriptorProto service = 6; int service_size() const; void clear_service(); - static const int kServiceFieldNumber = 6; - ::google::protobuf::ServiceDescriptorProto* mutable_service(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::ServiceDescriptorProto >* + PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto* mutable_service(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto >* mutable_service(); - const ::google::protobuf::ServiceDescriptorProto& service(int index) const; - ::google::protobuf::ServiceDescriptorProto* add_service(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::ServiceDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto& service(int index) const; + PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto* add_service(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto >& service() const; // repeated .google.protobuf.FieldDescriptorProto extension = 7; int extension_size() const; void clear_extension(); - static const int kExtensionFieldNumber = 7; - ::google::protobuf::FieldDescriptorProto* mutable_extension(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >* + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* mutable_extension(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >* mutable_extension(); - const ::google::protobuf::FieldDescriptorProto& extension(int index) const; - ::google::protobuf::FieldDescriptorProto* add_extension(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::FieldDescriptorProto& extension(int index) const; + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* add_extension(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >& extension() const; // repeated int32 public_dependency = 10; int public_dependency_size() const; void clear_public_dependency(); - static const int kPublicDependencyFieldNumber = 10; - ::google::protobuf::int32 public_dependency(int index) const; - void set_public_dependency(int index, ::google::protobuf::int32 value); - void add_public_dependency(::google::protobuf::int32 value); - const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& + ::PROTOBUF_NAMESPACE_ID::int32 public_dependency(int index) const; + void set_public_dependency(int index, ::PROTOBUF_NAMESPACE_ID::int32 value); + void add_public_dependency(::PROTOBUF_NAMESPACE_ID::int32 value); + const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& public_dependency() const; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* mutable_public_dependency(); // repeated int32 weak_dependency = 11; int weak_dependency_size() const; void clear_weak_dependency(); - static const int kWeakDependencyFieldNumber = 11; - ::google::protobuf::int32 weak_dependency(int index) const; - void set_weak_dependency(int index, ::google::protobuf::int32 value); - void add_weak_dependency(::google::protobuf::int32 value); - const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& + ::PROTOBUF_NAMESPACE_ID::int32 weak_dependency(int index) const; + void set_weak_dependency(int index, ::PROTOBUF_NAMESPACE_ID::int32 value); + void add_weak_dependency(::PROTOBUF_NAMESPACE_ID::int32 value); + const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& weak_dependency() const; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* mutable_weak_dependency(); // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name(); + std::string* unsafe_arena_release_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name( - ::std::string* name); + std::string* name); // optional string package = 2; bool has_package() const; void clear_package(); - static const int kPackageFieldNumber = 2; - const ::std::string& package() const; - void set_package(const ::std::string& value); - #if LANG_CXX11 - void set_package(::std::string&& value); - #endif + const std::string& package() const; + void set_package(const std::string& value); + void set_package(std::string&& value); void set_package(const char* value); void set_package(const char* value, size_t size); - ::std::string* mutable_package(); - ::std::string* release_package(); - void set_allocated_package(::std::string* package); + std::string* mutable_package(); + std::string* release_package(); + void set_allocated_package(std::string* package); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_package(); + std::string* unsafe_arena_release_package(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_package( - ::std::string* package); + std::string* package); // optional string syntax = 12; bool has_syntax() const; void clear_syntax(); - static const int kSyntaxFieldNumber = 12; - const ::std::string& syntax() const; - void set_syntax(const ::std::string& value); - #if LANG_CXX11 - void set_syntax(::std::string&& value); - #endif + const std::string& syntax() const; + void set_syntax(const std::string& value); + void set_syntax(std::string&& value); void set_syntax(const char* value); void set_syntax(const char* value, size_t size); - ::std::string* mutable_syntax(); - ::std::string* release_syntax(); - void set_allocated_syntax(::std::string* syntax); + std::string* mutable_syntax(); + std::string* release_syntax(); + void set_allocated_syntax(std::string* syntax); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_syntax(); + std::string* unsafe_arena_release_syntax(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_syntax( - ::std::string* syntax); + std::string* syntax); // optional .google.protobuf.FileOptions options = 8; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 8; - const ::google::protobuf::FileOptions& options() const; - ::google::protobuf::FileOptions* release_options(); - ::google::protobuf::FileOptions* mutable_options(); - void set_allocated_options(::google::protobuf::FileOptions* options); + const PROTOBUF_NAMESPACE_ID::FileOptions& options() const; + PROTOBUF_NAMESPACE_ID::FileOptions* release_options(); + PROTOBUF_NAMESPACE_ID::FileOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::FileOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::FileOptions* options); - ::google::protobuf::FileOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::FileOptions* options); + PROTOBUF_NAMESPACE_ID::FileOptions* unsafe_arena_release_options(); // optional .google.protobuf.SourceCodeInfo source_code_info = 9; bool has_source_code_info() const; void clear_source_code_info(); - static const int kSourceCodeInfoFieldNumber = 9; - const ::google::protobuf::SourceCodeInfo& source_code_info() const; - ::google::protobuf::SourceCodeInfo* release_source_code_info(); - ::google::protobuf::SourceCodeInfo* mutable_source_code_info(); - void set_allocated_source_code_info(::google::protobuf::SourceCodeInfo* source_code_info); + const PROTOBUF_NAMESPACE_ID::SourceCodeInfo& source_code_info() const; + PROTOBUF_NAMESPACE_ID::SourceCodeInfo* release_source_code_info(); + PROTOBUF_NAMESPACE_ID::SourceCodeInfo* mutable_source_code_info(); + void set_allocated_source_code_info(PROTOBUF_NAMESPACE_ID::SourceCodeInfo* source_code_info); void unsafe_arena_set_allocated_source_code_info( - ::google::protobuf::SourceCodeInfo* source_code_info); - ::google::protobuf::SourceCodeInfo* unsafe_arena_release_source_code_info(); + PROTOBUF_NAMESPACE_ID::SourceCodeInfo* source_code_info); + PROTOBUF_NAMESPACE_ID::SourceCodeInfo* unsafe_arena_release_source_code_info(); // @@protoc_insertion_point(class_scope:google.protobuf.FileDescriptorProto) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField<::std::string> dependency_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto > message_type_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto > enum_type_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::ServiceDescriptorProto > service_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto > extension_; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 > public_dependency_; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 > weak_dependency_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::internal::ArenaStringPtr package_; - ::google::protobuf::internal::ArenaStringPtr syntax_; - ::google::protobuf::FileOptions* options_; - ::google::protobuf::SourceCodeInfo* source_code_info_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField dependency_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto > message_type_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto > enum_type_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto > service_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto > extension_; + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 > public_dependency_; + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 > weak_dependency_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr package_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr syntax_; + PROTOBUF_NAMESPACE_ID::FileOptions* options_; + PROTOBUF_NAMESPACE_ID::SourceCodeInfo* source_code_info_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT DescriptorProto_ExtensionRange : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.DescriptorProto.ExtensionRange) */ { +class PROTOBUF_EXPORT DescriptorProto_ExtensionRange : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.DescriptorProto.ExtensionRange) */ { public: DescriptorProto_ExtensionRange(); virtual ~DescriptorProto_ExtensionRange(); DescriptorProto_ExtensionRange(const DescriptorProto_ExtensionRange& from); - - inline DescriptorProto_ExtensionRange& operator=(const DescriptorProto_ExtensionRange& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 DescriptorProto_ExtensionRange(DescriptorProto_ExtensionRange&& from) noexcept : DescriptorProto_ExtensionRange() { *this = ::std::move(from); } + inline DescriptorProto_ExtensionRange& operator=(const DescriptorProto_ExtensionRange& from) { + CopyFrom(from); + return *this; + } inline DescriptorProto_ExtensionRange& operator=(DescriptorProto_ExtensionRange&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -794,22 +865,28 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ExtensionRange : public ::google::proto } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const DescriptorProto_ExtensionRange& default_instance(); @@ -821,54 +898,66 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ExtensionRange : public ::google::proto static constexpr int kIndexInFileMessages = 2; - void UnsafeArenaSwap(DescriptorProto_ExtensionRange* other); - void Swap(DescriptorProto_ExtensionRange* other); friend void swap(DescriptorProto_ExtensionRange& a, DescriptorProto_ExtensionRange& b) { a.Swap(&b); } + inline void Swap(DescriptorProto_ExtensionRange* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(DescriptorProto_ExtensionRange* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline DescriptorProto_ExtensionRange* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - DescriptorProto_ExtensionRange* New(::google::protobuf::Arena* arena) const final { + DescriptorProto_ExtensionRange* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const DescriptorProto_ExtensionRange& from); void MergeFrom(const DescriptorProto_ExtensionRange& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(DescriptorProto_ExtensionRange* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.DescriptorProto.ExtensionRange"; + } protected: - explicit DescriptorProto_ExtensionRange(::google::protobuf::Arena* arena); + explicit DescriptorProto_ExtensionRange(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -876,72 +965,80 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ExtensionRange : public ::google::proto } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kOptionsFieldNumber = 3, + kStartFieldNumber = 1, + kEndFieldNumber = 2, + }; // optional .google.protobuf.ExtensionRangeOptions options = 3; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 3; - const ::google::protobuf::ExtensionRangeOptions& options() const; - ::google::protobuf::ExtensionRangeOptions* release_options(); - ::google::protobuf::ExtensionRangeOptions* mutable_options(); - void set_allocated_options(::google::protobuf::ExtensionRangeOptions* options); + const PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions& options() const; + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* release_options(); + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::ExtensionRangeOptions* options); - ::google::protobuf::ExtensionRangeOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* options); + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* unsafe_arena_release_options(); // optional int32 start = 1; bool has_start() const; void clear_start(); - static const int kStartFieldNumber = 1; - ::google::protobuf::int32 start() const; - void set_start(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 start() const; + void set_start(::PROTOBUF_NAMESPACE_ID::int32 value); // optional int32 end = 2; bool has_end() const; void clear_end(); - static const int kEndFieldNumber = 2; - ::google::protobuf::int32 end() const; - void set_end(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 end() const; + void set_end(::PROTOBUF_NAMESPACE_ID::int32 value); // @@protoc_insertion_point(class_scope:google.protobuf.DescriptorProto.ExtensionRange) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::ExtensionRangeOptions* options_; - ::google::protobuf::int32 start_; - ::google::protobuf::int32 end_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* options_; + ::PROTOBUF_NAMESPACE_ID::int32 start_; + ::PROTOBUF_NAMESPACE_ID::int32 end_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT DescriptorProto_ReservedRange : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.DescriptorProto.ReservedRange) */ { +class PROTOBUF_EXPORT DescriptorProto_ReservedRange : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.DescriptorProto.ReservedRange) */ { public: DescriptorProto_ReservedRange(); virtual ~DescriptorProto_ReservedRange(); DescriptorProto_ReservedRange(const DescriptorProto_ReservedRange& from); - - inline DescriptorProto_ReservedRange& operator=(const DescriptorProto_ReservedRange& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 DescriptorProto_ReservedRange(DescriptorProto_ReservedRange&& from) noexcept : DescriptorProto_ReservedRange() { *this = ::std::move(from); } + inline DescriptorProto_ReservedRange& operator=(const DescriptorProto_ReservedRange& from) { + CopyFrom(from); + return *this; + } inline DescriptorProto_ReservedRange& operator=(DescriptorProto_ReservedRange&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -950,22 +1047,28 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ReservedRange : public ::google::protob } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const DescriptorProto_ReservedRange& default_instance(); @@ -977,54 +1080,66 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ReservedRange : public ::google::protob static constexpr int kIndexInFileMessages = 3; - void UnsafeArenaSwap(DescriptorProto_ReservedRange* other); - void Swap(DescriptorProto_ReservedRange* other); friend void swap(DescriptorProto_ReservedRange& a, DescriptorProto_ReservedRange& b) { a.Swap(&b); } + inline void Swap(DescriptorProto_ReservedRange* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(DescriptorProto_ReservedRange* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline DescriptorProto_ReservedRange* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - DescriptorProto_ReservedRange* New(::google::protobuf::Arena* arena) const final { + DescriptorProto_ReservedRange* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const DescriptorProto_ReservedRange& from); void MergeFrom(const DescriptorProto_ReservedRange& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(DescriptorProto_ReservedRange* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.DescriptorProto.ReservedRange"; + } protected: - explicit DescriptorProto_ReservedRange(::google::protobuf::Arena* arena); + explicit DescriptorProto_ReservedRange(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -1032,59 +1147,67 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ReservedRange : public ::google::protob } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kStartFieldNumber = 1, + kEndFieldNumber = 2, + }; // optional int32 start = 1; bool has_start() const; void clear_start(); - static const int kStartFieldNumber = 1; - ::google::protobuf::int32 start() const; - void set_start(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 start() const; + void set_start(::PROTOBUF_NAMESPACE_ID::int32 value); // optional int32 end = 2; bool has_end() const; void clear_end(); - static const int kEndFieldNumber = 2; - ::google::protobuf::int32 end() const; - void set_end(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 end() const; + void set_end(::PROTOBUF_NAMESPACE_ID::int32 value); // @@protoc_insertion_point(class_scope:google.protobuf.DescriptorProto.ReservedRange) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::int32 start_; - ::google::protobuf::int32 end_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::int32 start_; + ::PROTOBUF_NAMESPACE_ID::int32 end_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.DescriptorProto) */ { +class PROTOBUF_EXPORT DescriptorProto : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.DescriptorProto) */ { public: DescriptorProto(); virtual ~DescriptorProto(); DescriptorProto(const DescriptorProto& from); - - inline DescriptorProto& operator=(const DescriptorProto& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 DescriptorProto(DescriptorProto&& from) noexcept : DescriptorProto() { *this = ::std::move(from); } + inline DescriptorProto& operator=(const DescriptorProto& from) { + CopyFrom(from); + return *this; + } inline DescriptorProto& operator=(DescriptorProto&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -1093,22 +1216,28 @@ class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message /* } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const DescriptorProto& default_instance(); @@ -1120,54 +1249,66 @@ class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message /* static constexpr int kIndexInFileMessages = 4; - void UnsafeArenaSwap(DescriptorProto* other); - void Swap(DescriptorProto* other); friend void swap(DescriptorProto& a, DescriptorProto& b) { a.Swap(&b); } + inline void Swap(DescriptorProto* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(DescriptorProto* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline DescriptorProto* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - DescriptorProto* New(::google::protobuf::Arena* arena) const final { + DescriptorProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const DescriptorProto& from); void MergeFrom(const DescriptorProto& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(DescriptorProto* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.DescriptorProto"; + } protected: - explicit DescriptorProto(::google::protobuf::Arena* arena); + explicit DescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -1175,7 +1316,14 @@ class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message /* } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- @@ -1184,189 +1332,184 @@ class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message /* // accessors ------------------------------------------------------- + enum : int { + kFieldFieldNumber = 2, + kNestedTypeFieldNumber = 3, + kEnumTypeFieldNumber = 4, + kExtensionRangeFieldNumber = 5, + kExtensionFieldNumber = 6, + kOneofDeclFieldNumber = 8, + kReservedRangeFieldNumber = 9, + kReservedNameFieldNumber = 10, + kNameFieldNumber = 1, + kOptionsFieldNumber = 7, + }; // repeated .google.protobuf.FieldDescriptorProto field = 2; int field_size() const; void clear_field(); - static const int kFieldFieldNumber = 2; - ::google::protobuf::FieldDescriptorProto* mutable_field(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >* + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* mutable_field(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >* mutable_field(); - const ::google::protobuf::FieldDescriptorProto& field(int index) const; - ::google::protobuf::FieldDescriptorProto* add_field(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::FieldDescriptorProto& field(int index) const; + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* add_field(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >& field() const; // repeated .google.protobuf.DescriptorProto nested_type = 3; int nested_type_size() const; void clear_nested_type(); - static const int kNestedTypeFieldNumber = 3; - ::google::protobuf::DescriptorProto* mutable_nested_type(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto >* + PROTOBUF_NAMESPACE_ID::DescriptorProto* mutable_nested_type(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto >* mutable_nested_type(); - const ::google::protobuf::DescriptorProto& nested_type(int index) const; - ::google::protobuf::DescriptorProto* add_nested_type(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto >& + const PROTOBUF_NAMESPACE_ID::DescriptorProto& nested_type(int index) const; + PROTOBUF_NAMESPACE_ID::DescriptorProto* add_nested_type(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto >& nested_type() const; // repeated .google.protobuf.EnumDescriptorProto enum_type = 4; int enum_type_size() const; void clear_enum_type(); - static const int kEnumTypeFieldNumber = 4; - ::google::protobuf::EnumDescriptorProto* mutable_enum_type(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto >* + PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* mutable_enum_type(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >* mutable_enum_type(); - const ::google::protobuf::EnumDescriptorProto& enum_type(int index) const; - ::google::protobuf::EnumDescriptorProto* add_enum_type(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::EnumDescriptorProto& enum_type(int index) const; + PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* add_enum_type(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >& enum_type() const; // repeated .google.protobuf.DescriptorProto.ExtensionRange extension_range = 5; int extension_range_size() const; void clear_extension_range(); - static const int kExtensionRangeFieldNumber = 5; - ::google::protobuf::DescriptorProto_ExtensionRange* mutable_extension_range(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ExtensionRange >* + PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange* mutable_extension_range(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange >* mutable_extension_range(); - const ::google::protobuf::DescriptorProto_ExtensionRange& extension_range(int index) const; - ::google::protobuf::DescriptorProto_ExtensionRange* add_extension_range(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ExtensionRange >& + const PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange& extension_range(int index) const; + PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange* add_extension_range(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange >& extension_range() const; // repeated .google.protobuf.FieldDescriptorProto extension = 6; int extension_size() const; void clear_extension(); - static const int kExtensionFieldNumber = 6; - ::google::protobuf::FieldDescriptorProto* mutable_extension(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >* + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* mutable_extension(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >* mutable_extension(); - const ::google::protobuf::FieldDescriptorProto& extension(int index) const; - ::google::protobuf::FieldDescriptorProto* add_extension(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::FieldDescriptorProto& extension(int index) const; + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* add_extension(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >& extension() const; // repeated .google.protobuf.OneofDescriptorProto oneof_decl = 8; int oneof_decl_size() const; void clear_oneof_decl(); - static const int kOneofDeclFieldNumber = 8; - ::google::protobuf::OneofDescriptorProto* mutable_oneof_decl(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::OneofDescriptorProto >* + PROTOBUF_NAMESPACE_ID::OneofDescriptorProto* mutable_oneof_decl(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::OneofDescriptorProto >* mutable_oneof_decl(); - const ::google::protobuf::OneofDescriptorProto& oneof_decl(int index) const; - ::google::protobuf::OneofDescriptorProto* add_oneof_decl(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::OneofDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::OneofDescriptorProto& oneof_decl(int index) const; + PROTOBUF_NAMESPACE_ID::OneofDescriptorProto* add_oneof_decl(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::OneofDescriptorProto >& oneof_decl() const; // repeated .google.protobuf.DescriptorProto.ReservedRange reserved_range = 9; int reserved_range_size() const; void clear_reserved_range(); - static const int kReservedRangeFieldNumber = 9; - ::google::protobuf::DescriptorProto_ReservedRange* mutable_reserved_range(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ReservedRange >* + PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange* mutable_reserved_range(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange >* mutable_reserved_range(); - const ::google::protobuf::DescriptorProto_ReservedRange& reserved_range(int index) const; - ::google::protobuf::DescriptorProto_ReservedRange* add_reserved_range(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ReservedRange >& + const PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange& reserved_range(int index) const; + PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange* add_reserved_range(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange >& reserved_range() const; // repeated string reserved_name = 10; int reserved_name_size() const; void clear_reserved_name(); - static const int kReservedNameFieldNumber = 10; - const ::std::string& reserved_name(int index) const; - ::std::string* mutable_reserved_name(int index); - void set_reserved_name(int index, const ::std::string& value); - #if LANG_CXX11 - void set_reserved_name(int index, ::std::string&& value); - #endif + const std::string& reserved_name(int index) const; + std::string* mutable_reserved_name(int index); + void set_reserved_name(int index, const std::string& value); + void set_reserved_name(int index, std::string&& value); void set_reserved_name(int index, const char* value); void set_reserved_name(int index, const char* value, size_t size); - ::std::string* add_reserved_name(); - void add_reserved_name(const ::std::string& value); - #if LANG_CXX11 - void add_reserved_name(::std::string&& value); - #endif + std::string* add_reserved_name(); + void add_reserved_name(const std::string& value); + void add_reserved_name(std::string&& value); void add_reserved_name(const char* value); void add_reserved_name(const char* value, size_t size); - const ::google::protobuf::RepeatedPtrField<::std::string>& reserved_name() const; - ::google::protobuf::RepeatedPtrField<::std::string>* mutable_reserved_name(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& reserved_name() const; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_reserved_name(); // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name(); + std::string* unsafe_arena_release_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name( - ::std::string* name); + std::string* name); // optional .google.protobuf.MessageOptions options = 7; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 7; - const ::google::protobuf::MessageOptions& options() const; - ::google::protobuf::MessageOptions* release_options(); - ::google::protobuf::MessageOptions* mutable_options(); - void set_allocated_options(::google::protobuf::MessageOptions* options); + const PROTOBUF_NAMESPACE_ID::MessageOptions& options() const; + PROTOBUF_NAMESPACE_ID::MessageOptions* release_options(); + PROTOBUF_NAMESPACE_ID::MessageOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::MessageOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::MessageOptions* options); - ::google::protobuf::MessageOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::MessageOptions* options); + PROTOBUF_NAMESPACE_ID::MessageOptions* unsafe_arena_release_options(); // @@protoc_insertion_point(class_scope:google.protobuf.DescriptorProto) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto > field_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto > nested_type_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto > enum_type_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ExtensionRange > extension_range_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto > extension_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::OneofDescriptorProto > oneof_decl_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ReservedRange > reserved_range_; - ::google::protobuf::RepeatedPtrField<::std::string> reserved_name_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::MessageOptions* options_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto > field_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto > nested_type_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto > enum_type_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange > extension_range_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto > extension_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::OneofDescriptorProto > oneof_decl_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange > reserved_range_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField reserved_name_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; + PROTOBUF_NAMESPACE_ID::MessageOptions* options_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT ExtensionRangeOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.ExtensionRangeOptions) */ { +class PROTOBUF_EXPORT ExtensionRangeOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.ExtensionRangeOptions) */ { public: ExtensionRangeOptions(); virtual ~ExtensionRangeOptions(); ExtensionRangeOptions(const ExtensionRangeOptions& from); - - inline ExtensionRangeOptions& operator=(const ExtensionRangeOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 ExtensionRangeOptions(ExtensionRangeOptions&& from) noexcept : ExtensionRangeOptions() { *this = ::std::move(from); } + inline ExtensionRangeOptions& operator=(const ExtensionRangeOptions& from) { + CopyFrom(from); + return *this; + } inline ExtensionRangeOptions& operator=(ExtensionRangeOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -1375,22 +1518,28 @@ class LIBPROTOBUF_EXPORT ExtensionRangeOptions : public ::google::protobuf::Mess } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const ExtensionRangeOptions& default_instance(); @@ -1402,54 +1551,66 @@ class LIBPROTOBUF_EXPORT ExtensionRangeOptions : public ::google::protobuf::Mess static constexpr int kIndexInFileMessages = 5; - void UnsafeArenaSwap(ExtensionRangeOptions* other); - void Swap(ExtensionRangeOptions* other); friend void swap(ExtensionRangeOptions& a, ExtensionRangeOptions& b) { a.Swap(&b); } + inline void Swap(ExtensionRangeOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(ExtensionRangeOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline ExtensionRangeOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - ExtensionRangeOptions* New(::google::protobuf::Arena* arena) const final { + ExtensionRangeOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const ExtensionRangeOptions& from); void MergeFrom(const ExtensionRangeOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(ExtensionRangeOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.ExtensionRangeOptions"; + } protected: - explicit ExtensionRangeOptions(::google::protobuf::Arena* arena); + explicit ExtensionRangeOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -1457,59 +1618,67 @@ class LIBPROTOBUF_EXPORT ExtensionRangeOptions : public ::google::protobuf::Mess } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(ExtensionRangeOptions) // @@protoc_insertion_point(class_scope:google.protobuf.ExtensionRangeOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT FieldDescriptorProto : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FieldDescriptorProto) */ { +class PROTOBUF_EXPORT FieldDescriptorProto : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FieldDescriptorProto) */ { public: FieldDescriptorProto(); virtual ~FieldDescriptorProto(); FieldDescriptorProto(const FieldDescriptorProto& from); - - inline FieldDescriptorProto& operator=(const FieldDescriptorProto& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 FieldDescriptorProto(FieldDescriptorProto&& from) noexcept : FieldDescriptorProto() { *this = ::std::move(from); } + inline FieldDescriptorProto& operator=(const FieldDescriptorProto& from) { + CopyFrom(from); + return *this; + } inline FieldDescriptorProto& operator=(FieldDescriptorProto&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -1518,22 +1687,28 @@ class LIBPROTOBUF_EXPORT FieldDescriptorProto : public ::google::protobuf::Messa } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const FieldDescriptorProto& default_instance(); @@ -1545,54 +1720,66 @@ class LIBPROTOBUF_EXPORT FieldDescriptorProto : public ::google::protobuf::Messa static constexpr int kIndexInFileMessages = 6; - void UnsafeArenaSwap(FieldDescriptorProto* other); - void Swap(FieldDescriptorProto* other); friend void swap(FieldDescriptorProto& a, FieldDescriptorProto& b) { a.Swap(&b); } + inline void Swap(FieldDescriptorProto* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(FieldDescriptorProto* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline FieldDescriptorProto* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - FieldDescriptorProto* New(::google::protobuf::Arena* arena) const final { + FieldDescriptorProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const FieldDescriptorProto& from); void MergeFrom(const FieldDescriptorProto& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(FieldDescriptorProto* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.FieldDescriptorProto"; + } protected: - explicit FieldDescriptorProto(::google::protobuf::Arena* arena); + explicit FieldDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -1600,299 +1787,305 @@ class LIBPROTOBUF_EXPORT FieldDescriptorProto : public ::google::protobuf::Messa } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- typedef FieldDescriptorProto_Type Type; - static const Type TYPE_DOUBLE = + static constexpr Type TYPE_DOUBLE = FieldDescriptorProto_Type_TYPE_DOUBLE; - static const Type TYPE_FLOAT = + static constexpr Type TYPE_FLOAT = FieldDescriptorProto_Type_TYPE_FLOAT; - static const Type TYPE_INT64 = + static constexpr Type TYPE_INT64 = FieldDescriptorProto_Type_TYPE_INT64; - static const Type TYPE_UINT64 = + static constexpr Type TYPE_UINT64 = FieldDescriptorProto_Type_TYPE_UINT64; - static const Type TYPE_INT32 = + static constexpr Type TYPE_INT32 = FieldDescriptorProto_Type_TYPE_INT32; - static const Type TYPE_FIXED64 = + static constexpr Type TYPE_FIXED64 = FieldDescriptorProto_Type_TYPE_FIXED64; - static const Type TYPE_FIXED32 = + static constexpr Type TYPE_FIXED32 = FieldDescriptorProto_Type_TYPE_FIXED32; - static const Type TYPE_BOOL = + static constexpr Type TYPE_BOOL = FieldDescriptorProto_Type_TYPE_BOOL; - static const Type TYPE_STRING = + static constexpr Type TYPE_STRING = FieldDescriptorProto_Type_TYPE_STRING; - static const Type TYPE_GROUP = + static constexpr Type TYPE_GROUP = FieldDescriptorProto_Type_TYPE_GROUP; - static const Type TYPE_MESSAGE = + static constexpr Type TYPE_MESSAGE = FieldDescriptorProto_Type_TYPE_MESSAGE; - static const Type TYPE_BYTES = + static constexpr Type TYPE_BYTES = FieldDescriptorProto_Type_TYPE_BYTES; - static const Type TYPE_UINT32 = + static constexpr Type TYPE_UINT32 = FieldDescriptorProto_Type_TYPE_UINT32; - static const Type TYPE_ENUM = + static constexpr Type TYPE_ENUM = FieldDescriptorProto_Type_TYPE_ENUM; - static const Type TYPE_SFIXED32 = + static constexpr Type TYPE_SFIXED32 = FieldDescriptorProto_Type_TYPE_SFIXED32; - static const Type TYPE_SFIXED64 = + static constexpr Type TYPE_SFIXED64 = FieldDescriptorProto_Type_TYPE_SFIXED64; - static const Type TYPE_SINT32 = + static constexpr Type TYPE_SINT32 = FieldDescriptorProto_Type_TYPE_SINT32; - static const Type TYPE_SINT64 = + static constexpr Type TYPE_SINT64 = FieldDescriptorProto_Type_TYPE_SINT64; static inline bool Type_IsValid(int value) { return FieldDescriptorProto_Type_IsValid(value); } - static const Type Type_MIN = + static constexpr Type Type_MIN = FieldDescriptorProto_Type_Type_MIN; - static const Type Type_MAX = + static constexpr Type Type_MAX = FieldDescriptorProto_Type_Type_MAX; - static const int Type_ARRAYSIZE = + static constexpr int Type_ARRAYSIZE = FieldDescriptorProto_Type_Type_ARRAYSIZE; - static inline const ::google::protobuf::EnumDescriptor* + static inline const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* Type_descriptor() { return FieldDescriptorProto_Type_descriptor(); } - static inline const ::std::string& Type_Name(Type value) { - return FieldDescriptorProto_Type_Name(value); + 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 FieldDescriptorProto_Type_Name(enum_t_value); } - static inline bool Type_Parse(const ::std::string& name, + static inline bool Type_Parse(const std::string& name, Type* value) { return FieldDescriptorProto_Type_Parse(name, value); } typedef FieldDescriptorProto_Label Label; - static const Label LABEL_OPTIONAL = + static constexpr Label LABEL_OPTIONAL = FieldDescriptorProto_Label_LABEL_OPTIONAL; - static const Label LABEL_REQUIRED = + static constexpr Label LABEL_REQUIRED = FieldDescriptorProto_Label_LABEL_REQUIRED; - static const Label LABEL_REPEATED = + static constexpr Label LABEL_REPEATED = FieldDescriptorProto_Label_LABEL_REPEATED; static inline bool Label_IsValid(int value) { return FieldDescriptorProto_Label_IsValid(value); } - static const Label Label_MIN = + static constexpr Label Label_MIN = FieldDescriptorProto_Label_Label_MIN; - static const Label Label_MAX = + static constexpr Label Label_MAX = FieldDescriptorProto_Label_Label_MAX; - static const int Label_ARRAYSIZE = + static constexpr int Label_ARRAYSIZE = FieldDescriptorProto_Label_Label_ARRAYSIZE; - static inline const ::google::protobuf::EnumDescriptor* + static inline const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* Label_descriptor() { return FieldDescriptorProto_Label_descriptor(); } - static inline const ::std::string& Label_Name(Label value) { - return FieldDescriptorProto_Label_Name(value); + template + static inline const std::string& Label_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function Label_Name."); + return FieldDescriptorProto_Label_Name(enum_t_value); } - static inline bool Label_Parse(const ::std::string& name, + static inline bool Label_Parse(const std::string& name, Label* value) { return FieldDescriptorProto_Label_Parse(name, value); } // accessors ------------------------------------------------------- + enum : int { + kNameFieldNumber = 1, + kExtendeeFieldNumber = 2, + kTypeNameFieldNumber = 6, + kDefaultValueFieldNumber = 7, + kJsonNameFieldNumber = 10, + kOptionsFieldNumber = 8, + kNumberFieldNumber = 3, + kOneofIndexFieldNumber = 9, + kLabelFieldNumber = 4, + kTypeFieldNumber = 5, + }; // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name(); + std::string* unsafe_arena_release_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name( - ::std::string* name); + std::string* name); // optional string extendee = 2; bool has_extendee() const; void clear_extendee(); - static const int kExtendeeFieldNumber = 2; - const ::std::string& extendee() const; - void set_extendee(const ::std::string& value); - #if LANG_CXX11 - void set_extendee(::std::string&& value); - #endif + const std::string& extendee() const; + void set_extendee(const std::string& value); + void set_extendee(std::string&& value); void set_extendee(const char* value); void set_extendee(const char* value, size_t size); - ::std::string* mutable_extendee(); - ::std::string* release_extendee(); - void set_allocated_extendee(::std::string* extendee); + std::string* mutable_extendee(); + std::string* release_extendee(); + void set_allocated_extendee(std::string* extendee); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_extendee(); + std::string* unsafe_arena_release_extendee(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_extendee( - ::std::string* extendee); + std::string* extendee); // optional string type_name = 6; bool has_type_name() const; void clear_type_name(); - static const int kTypeNameFieldNumber = 6; - const ::std::string& type_name() const; - void set_type_name(const ::std::string& value); - #if LANG_CXX11 - void set_type_name(::std::string&& value); - #endif + const std::string& type_name() const; + void set_type_name(const std::string& value); + void set_type_name(std::string&& value); void set_type_name(const char* value); void set_type_name(const char* value, size_t size); - ::std::string* mutable_type_name(); - ::std::string* release_type_name(); - void set_allocated_type_name(::std::string* type_name); + std::string* mutable_type_name(); + std::string* release_type_name(); + void set_allocated_type_name(std::string* type_name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_type_name(); + std::string* unsafe_arena_release_type_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_type_name( - ::std::string* type_name); + std::string* type_name); // optional string default_value = 7; bool has_default_value() const; void clear_default_value(); - static const int kDefaultValueFieldNumber = 7; - const ::std::string& default_value() const; - void set_default_value(const ::std::string& value); - #if LANG_CXX11 - void set_default_value(::std::string&& value); - #endif + const std::string& default_value() const; + void set_default_value(const std::string& value); + void set_default_value(std::string&& value); void set_default_value(const char* value); void set_default_value(const char* value, size_t size); - ::std::string* mutable_default_value(); - ::std::string* release_default_value(); - void set_allocated_default_value(::std::string* default_value); + std::string* mutable_default_value(); + std::string* release_default_value(); + void set_allocated_default_value(std::string* default_value); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_default_value(); + std::string* unsafe_arena_release_default_value(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_default_value( - ::std::string* default_value); + std::string* default_value); // optional string json_name = 10; bool has_json_name() const; void clear_json_name(); - static const int kJsonNameFieldNumber = 10; - const ::std::string& json_name() const; - void set_json_name(const ::std::string& value); - #if LANG_CXX11 - void set_json_name(::std::string&& value); - #endif + const std::string& json_name() const; + void set_json_name(const std::string& value); + void set_json_name(std::string&& value); void set_json_name(const char* value); void set_json_name(const char* value, size_t size); - ::std::string* mutable_json_name(); - ::std::string* release_json_name(); - void set_allocated_json_name(::std::string* json_name); + std::string* mutable_json_name(); + std::string* release_json_name(); + void set_allocated_json_name(std::string* json_name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_json_name(); + std::string* unsafe_arena_release_json_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_json_name( - ::std::string* json_name); + std::string* json_name); // optional .google.protobuf.FieldOptions options = 8; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 8; - const ::google::protobuf::FieldOptions& options() const; - ::google::protobuf::FieldOptions* release_options(); - ::google::protobuf::FieldOptions* mutable_options(); - void set_allocated_options(::google::protobuf::FieldOptions* options); + const PROTOBUF_NAMESPACE_ID::FieldOptions& options() const; + PROTOBUF_NAMESPACE_ID::FieldOptions* release_options(); + PROTOBUF_NAMESPACE_ID::FieldOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::FieldOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::FieldOptions* options); - ::google::protobuf::FieldOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::FieldOptions* options); + PROTOBUF_NAMESPACE_ID::FieldOptions* unsafe_arena_release_options(); // optional int32 number = 3; bool has_number() const; void clear_number(); - static const int kNumberFieldNumber = 3; - ::google::protobuf::int32 number() const; - void set_number(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 number() const; + void set_number(::PROTOBUF_NAMESPACE_ID::int32 value); // optional int32 oneof_index = 9; bool has_oneof_index() const; void clear_oneof_index(); - static const int kOneofIndexFieldNumber = 9; - ::google::protobuf::int32 oneof_index() const; - void set_oneof_index(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 oneof_index() const; + void set_oneof_index(::PROTOBUF_NAMESPACE_ID::int32 value); // optional .google.protobuf.FieldDescriptorProto.Label label = 4; bool has_label() const; void clear_label(); - static const int kLabelFieldNumber = 4; - ::google::protobuf::FieldDescriptorProto_Label label() const; - void set_label(::google::protobuf::FieldDescriptorProto_Label value); + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label label() const; + void set_label(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label value); // optional .google.protobuf.FieldDescriptorProto.Type type = 5; bool has_type() const; void clear_type(); - static const int kTypeFieldNumber = 5; - ::google::protobuf::FieldDescriptorProto_Type type() const; - void set_type(::google::protobuf::FieldDescriptorProto_Type value); + PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type type() const; + void set_type(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type value); // @@protoc_insertion_point(class_scope:google.protobuf.FieldDescriptorProto) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::internal::ArenaStringPtr extendee_; - ::google::protobuf::internal::ArenaStringPtr type_name_; - ::google::protobuf::internal::ArenaStringPtr default_value_; - ::google::protobuf::internal::ArenaStringPtr json_name_; - ::google::protobuf::FieldOptions* options_; - ::google::protobuf::int32 number_; - ::google::protobuf::int32 oneof_index_; + ::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 extendee_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr type_name_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr default_value_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr json_name_; + PROTOBUF_NAMESPACE_ID::FieldOptions* options_; + ::PROTOBUF_NAMESPACE_ID::int32 number_; + ::PROTOBUF_NAMESPACE_ID::int32 oneof_index_; int label_; int type_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT OneofDescriptorProto : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.OneofDescriptorProto) */ { +class PROTOBUF_EXPORT OneofDescriptorProto : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.OneofDescriptorProto) */ { public: OneofDescriptorProto(); virtual ~OneofDescriptorProto(); OneofDescriptorProto(const OneofDescriptorProto& from); - - inline OneofDescriptorProto& operator=(const OneofDescriptorProto& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 OneofDescriptorProto(OneofDescriptorProto&& from) noexcept : OneofDescriptorProto() { *this = ::std::move(from); } + inline OneofDescriptorProto& operator=(const OneofDescriptorProto& from) { + CopyFrom(from); + return *this; + } inline OneofDescriptorProto& operator=(OneofDescriptorProto&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -1901,22 +2094,28 @@ class LIBPROTOBUF_EXPORT OneofDescriptorProto : public ::google::protobuf::Messa } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const OneofDescriptorProto& default_instance(); @@ -1928,54 +2127,66 @@ class LIBPROTOBUF_EXPORT OneofDescriptorProto : public ::google::protobuf::Messa static constexpr int kIndexInFileMessages = 7; - void UnsafeArenaSwap(OneofDescriptorProto* other); - void Swap(OneofDescriptorProto* other); friend void swap(OneofDescriptorProto& a, OneofDescriptorProto& b) { a.Swap(&b); } + inline void Swap(OneofDescriptorProto* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(OneofDescriptorProto* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline OneofDescriptorProto* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - OneofDescriptorProto* New(::google::protobuf::Arena* arena) const final { + OneofDescriptorProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const OneofDescriptorProto& from); void MergeFrom(const OneofDescriptorProto& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(OneofDescriptorProto* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.OneofDescriptorProto"; + } protected: - explicit OneofDescriptorProto(::google::protobuf::Arena* arena); + explicit OneofDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -1983,81 +2194,87 @@ class LIBPROTOBUF_EXPORT OneofDescriptorProto : public ::google::protobuf::Messa } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kNameFieldNumber = 1, + kOptionsFieldNumber = 2, + }; // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name(); + std::string* unsafe_arena_release_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name( - ::std::string* name); + std::string* name); // optional .google.protobuf.OneofOptions options = 2; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 2; - const ::google::protobuf::OneofOptions& options() const; - ::google::protobuf::OneofOptions* release_options(); - ::google::protobuf::OneofOptions* mutable_options(); - void set_allocated_options(::google::protobuf::OneofOptions* options); + const PROTOBUF_NAMESPACE_ID::OneofOptions& options() const; + PROTOBUF_NAMESPACE_ID::OneofOptions* release_options(); + PROTOBUF_NAMESPACE_ID::OneofOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::OneofOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::OneofOptions* options); - ::google::protobuf::OneofOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::OneofOptions* options); + PROTOBUF_NAMESPACE_ID::OneofOptions* unsafe_arena_release_options(); // @@protoc_insertion_point(class_scope:google.protobuf.OneofDescriptorProto) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::OneofOptions* options_; + ::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::OneofOptions* options_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT EnumDescriptorProto_EnumReservedRange : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumDescriptorProto.EnumReservedRange) */ { +class PROTOBUF_EXPORT EnumDescriptorProto_EnumReservedRange : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumDescriptorProto.EnumReservedRange) */ { public: EnumDescriptorProto_EnumReservedRange(); virtual ~EnumDescriptorProto_EnumReservedRange(); EnumDescriptorProto_EnumReservedRange(const EnumDescriptorProto_EnumReservedRange& from); - - inline EnumDescriptorProto_EnumReservedRange& operator=(const EnumDescriptorProto_EnumReservedRange& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 EnumDescriptorProto_EnumReservedRange(EnumDescriptorProto_EnumReservedRange&& from) noexcept : EnumDescriptorProto_EnumReservedRange() { *this = ::std::move(from); } + inline EnumDescriptorProto_EnumReservedRange& operator=(const EnumDescriptorProto_EnumReservedRange& from) { + CopyFrom(from); + return *this; + } inline EnumDescriptorProto_EnumReservedRange& operator=(EnumDescriptorProto_EnumReservedRange&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -2066,22 +2283,28 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto_EnumReservedRange : public ::google } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const EnumDescriptorProto_EnumReservedRange& default_instance(); @@ -2093,54 +2316,66 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto_EnumReservedRange : public ::google static constexpr int kIndexInFileMessages = 8; - void UnsafeArenaSwap(EnumDescriptorProto_EnumReservedRange* other); - void Swap(EnumDescriptorProto_EnumReservedRange* other); friend void swap(EnumDescriptorProto_EnumReservedRange& a, EnumDescriptorProto_EnumReservedRange& b) { a.Swap(&b); } + inline void Swap(EnumDescriptorProto_EnumReservedRange* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(EnumDescriptorProto_EnumReservedRange* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline EnumDescriptorProto_EnumReservedRange* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - EnumDescriptorProto_EnumReservedRange* New(::google::protobuf::Arena* arena) const final { + EnumDescriptorProto_EnumReservedRange* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const EnumDescriptorProto_EnumReservedRange& from); void MergeFrom(const EnumDescriptorProto_EnumReservedRange& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(EnumDescriptorProto_EnumReservedRange* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.EnumDescriptorProto.EnumReservedRange"; + } protected: - explicit EnumDescriptorProto_EnumReservedRange(::google::protobuf::Arena* arena); + explicit EnumDescriptorProto_EnumReservedRange(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -2148,59 +2383,67 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto_EnumReservedRange : public ::google } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kStartFieldNumber = 1, + kEndFieldNumber = 2, + }; // optional int32 start = 1; bool has_start() const; void clear_start(); - static const int kStartFieldNumber = 1; - ::google::protobuf::int32 start() const; - void set_start(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 start() const; + void set_start(::PROTOBUF_NAMESPACE_ID::int32 value); // optional int32 end = 2; bool has_end() const; void clear_end(); - static const int kEndFieldNumber = 2; - ::google::protobuf::int32 end() const; - void set_end(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 end() const; + void set_end(::PROTOBUF_NAMESPACE_ID::int32 value); // @@protoc_insertion_point(class_scope:google.protobuf.EnumDescriptorProto.EnumReservedRange) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::int32 start_; - ::google::protobuf::int32 end_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::int32 start_; + ::PROTOBUF_NAMESPACE_ID::int32 end_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumDescriptorProto) */ { +class PROTOBUF_EXPORT EnumDescriptorProto : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumDescriptorProto) */ { public: EnumDescriptorProto(); virtual ~EnumDescriptorProto(); EnumDescriptorProto(const EnumDescriptorProto& from); - - inline EnumDescriptorProto& operator=(const EnumDescriptorProto& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 EnumDescriptorProto(EnumDescriptorProto&& from) noexcept : EnumDescriptorProto() { *this = ::std::move(from); } + inline EnumDescriptorProto& operator=(const EnumDescriptorProto& from) { + CopyFrom(from); + return *this; + } inline EnumDescriptorProto& operator=(EnumDescriptorProto&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -2209,22 +2452,28 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Messag } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const EnumDescriptorProto& default_instance(); @@ -2236,54 +2485,66 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Messag static constexpr int kIndexInFileMessages = 9; - void UnsafeArenaSwap(EnumDescriptorProto* other); - void Swap(EnumDescriptorProto* other); friend void swap(EnumDescriptorProto& a, EnumDescriptorProto& b) { a.Swap(&b); } + inline void Swap(EnumDescriptorProto* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(EnumDescriptorProto* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline EnumDescriptorProto* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - EnumDescriptorProto* New(::google::protobuf::Arena* arena) const final { + EnumDescriptorProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const EnumDescriptorProto& from); void MergeFrom(const EnumDescriptorProto& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(EnumDescriptorProto* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.EnumDescriptorProto"; + } protected: - explicit EnumDescriptorProto(::google::protobuf::Arena* arena); + explicit EnumDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -2291,7 +2552,14 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Messag } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- @@ -2299,124 +2567,119 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Messag // accessors ------------------------------------------------------- + enum : int { + kValueFieldNumber = 2, + kReservedRangeFieldNumber = 4, + kReservedNameFieldNumber = 5, + kNameFieldNumber = 1, + kOptionsFieldNumber = 3, + }; // repeated .google.protobuf.EnumValueDescriptorProto value = 2; int value_size() const; void clear_value(); - static const int kValueFieldNumber = 2; - ::google::protobuf::EnumValueDescriptorProto* mutable_value(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumValueDescriptorProto >* + PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto* mutable_value(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto >* mutable_value(); - const ::google::protobuf::EnumValueDescriptorProto& value(int index) const; - ::google::protobuf::EnumValueDescriptorProto* add_value(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumValueDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto& value(int index) const; + PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto* add_value(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto >& value() const; // repeated .google.protobuf.EnumDescriptorProto.EnumReservedRange reserved_range = 4; int reserved_range_size() const; void clear_reserved_range(); - static const int kReservedRangeFieldNumber = 4; - ::google::protobuf::EnumDescriptorProto_EnumReservedRange* mutable_reserved_range(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto_EnumReservedRange >* + PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange* mutable_reserved_range(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange >* mutable_reserved_range(); - const ::google::protobuf::EnumDescriptorProto_EnumReservedRange& reserved_range(int index) const; - ::google::protobuf::EnumDescriptorProto_EnumReservedRange* add_reserved_range(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto_EnumReservedRange >& + const PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange& reserved_range(int index) const; + PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange* add_reserved_range(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange >& reserved_range() const; // repeated string reserved_name = 5; int reserved_name_size() const; void clear_reserved_name(); - static const int kReservedNameFieldNumber = 5; - const ::std::string& reserved_name(int index) const; - ::std::string* mutable_reserved_name(int index); - void set_reserved_name(int index, const ::std::string& value); - #if LANG_CXX11 - void set_reserved_name(int index, ::std::string&& value); - #endif + const std::string& reserved_name(int index) const; + std::string* mutable_reserved_name(int index); + void set_reserved_name(int index, const std::string& value); + void set_reserved_name(int index, std::string&& value); void set_reserved_name(int index, const char* value); void set_reserved_name(int index, const char* value, size_t size); - ::std::string* add_reserved_name(); - void add_reserved_name(const ::std::string& value); - #if LANG_CXX11 - void add_reserved_name(::std::string&& value); - #endif + std::string* add_reserved_name(); + void add_reserved_name(const std::string& value); + void add_reserved_name(std::string&& value); void add_reserved_name(const char* value); void add_reserved_name(const char* value, size_t size); - const ::google::protobuf::RepeatedPtrField<::std::string>& reserved_name() const; - ::google::protobuf::RepeatedPtrField<::std::string>* mutable_reserved_name(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& reserved_name() const; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_reserved_name(); // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name(); + std::string* unsafe_arena_release_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name( - ::std::string* name); + std::string* name); // optional .google.protobuf.EnumOptions options = 3; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 3; - const ::google::protobuf::EnumOptions& options() const; - ::google::protobuf::EnumOptions* release_options(); - ::google::protobuf::EnumOptions* mutable_options(); - void set_allocated_options(::google::protobuf::EnumOptions* options); + const PROTOBUF_NAMESPACE_ID::EnumOptions& options() const; + PROTOBUF_NAMESPACE_ID::EnumOptions* release_options(); + PROTOBUF_NAMESPACE_ID::EnumOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::EnumOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::EnumOptions* options); - ::google::protobuf::EnumOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::EnumOptions* options); + PROTOBUF_NAMESPACE_ID::EnumOptions* unsafe_arena_release_options(); // @@protoc_insertion_point(class_scope:google.protobuf.EnumDescriptorProto) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumValueDescriptorProto > value_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto_EnumReservedRange > reserved_range_; - ::google::protobuf::RepeatedPtrField<::std::string> reserved_name_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::EnumOptions* options_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto > value_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange > reserved_range_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField reserved_name_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; + PROTOBUF_NAMESPACE_ID::EnumOptions* options_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT EnumValueDescriptorProto : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumValueDescriptorProto) */ { +class PROTOBUF_EXPORT EnumValueDescriptorProto : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumValueDescriptorProto) */ { public: EnumValueDescriptorProto(); virtual ~EnumValueDescriptorProto(); EnumValueDescriptorProto(const EnumValueDescriptorProto& from); - - inline EnumValueDescriptorProto& operator=(const EnumValueDescriptorProto& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 EnumValueDescriptorProto(EnumValueDescriptorProto&& from) noexcept : EnumValueDescriptorProto() { *this = ::std::move(from); } + inline EnumValueDescriptorProto& operator=(const EnumValueDescriptorProto& from) { + CopyFrom(from); + return *this; + } inline EnumValueDescriptorProto& operator=(EnumValueDescriptorProto&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -2425,22 +2688,28 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptorProto : public ::google::protobuf::M } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const EnumValueDescriptorProto& default_instance(); @@ -2452,54 +2721,66 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptorProto : public ::google::protobuf::M static constexpr int kIndexInFileMessages = 10; - void UnsafeArenaSwap(EnumValueDescriptorProto* other); - void Swap(EnumValueDescriptorProto* other); friend void swap(EnumValueDescriptorProto& a, EnumValueDescriptorProto& b) { a.Swap(&b); } + inline void Swap(EnumValueDescriptorProto* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(EnumValueDescriptorProto* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline EnumValueDescriptorProto* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - EnumValueDescriptorProto* New(::google::protobuf::Arena* arena) const final { + EnumValueDescriptorProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const EnumValueDescriptorProto& from); void MergeFrom(const EnumValueDescriptorProto& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(EnumValueDescriptorProto* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.EnumValueDescriptorProto"; + } protected: - explicit EnumValueDescriptorProto(::google::protobuf::Arena* arena); + explicit EnumValueDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -2507,89 +2788,95 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptorProto : public ::google::protobuf::M } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kNameFieldNumber = 1, + kOptionsFieldNumber = 3, + kNumberFieldNumber = 2, + }; // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name(); + std::string* unsafe_arena_release_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name( - ::std::string* name); + std::string* name); // optional .google.protobuf.EnumValueOptions options = 3; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 3; - const ::google::protobuf::EnumValueOptions& options() const; - ::google::protobuf::EnumValueOptions* release_options(); - ::google::protobuf::EnumValueOptions* mutable_options(); - void set_allocated_options(::google::protobuf::EnumValueOptions* options); + const PROTOBUF_NAMESPACE_ID::EnumValueOptions& options() const; + PROTOBUF_NAMESPACE_ID::EnumValueOptions* release_options(); + PROTOBUF_NAMESPACE_ID::EnumValueOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::EnumValueOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::EnumValueOptions* options); - ::google::protobuf::EnumValueOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::EnumValueOptions* options); + PROTOBUF_NAMESPACE_ID::EnumValueOptions* unsafe_arena_release_options(); // optional int32 number = 2; bool has_number() const; void clear_number(); - static const int kNumberFieldNumber = 2; - ::google::protobuf::int32 number() const; - void set_number(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 number() const; + void set_number(::PROTOBUF_NAMESPACE_ID::int32 value); // @@protoc_insertion_point(class_scope:google.protobuf.EnumValueDescriptorProto) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::EnumValueOptions* options_; - ::google::protobuf::int32 number_; + ::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::EnumValueOptions* options_; + ::PROTOBUF_NAMESPACE_ID::int32 number_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT ServiceDescriptorProto : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.ServiceDescriptorProto) */ { +class PROTOBUF_EXPORT ServiceDescriptorProto : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.ServiceDescriptorProto) */ { public: ServiceDescriptorProto(); virtual ~ServiceDescriptorProto(); ServiceDescriptorProto(const ServiceDescriptorProto& from); - - inline ServiceDescriptorProto& operator=(const ServiceDescriptorProto& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 ServiceDescriptorProto(ServiceDescriptorProto&& from) noexcept : ServiceDescriptorProto() { *this = ::std::move(from); } + inline ServiceDescriptorProto& operator=(const ServiceDescriptorProto& from) { + CopyFrom(from); + return *this; + } inline ServiceDescriptorProto& operator=(ServiceDescriptorProto&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -2598,22 +2885,28 @@ class LIBPROTOBUF_EXPORT ServiceDescriptorProto : public ::google::protobuf::Mes } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const ServiceDescriptorProto& default_instance(); @@ -2625,54 +2918,66 @@ class LIBPROTOBUF_EXPORT ServiceDescriptorProto : public ::google::protobuf::Mes static constexpr int kIndexInFileMessages = 11; - void UnsafeArenaSwap(ServiceDescriptorProto* other); - void Swap(ServiceDescriptorProto* other); friend void swap(ServiceDescriptorProto& a, ServiceDescriptorProto& b) { a.Swap(&b); } + inline void Swap(ServiceDescriptorProto* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(ServiceDescriptorProto* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline ServiceDescriptorProto* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - ServiceDescriptorProto* New(::google::protobuf::Arena* arena) const final { + ServiceDescriptorProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const ServiceDescriptorProto& from); void MergeFrom(const ServiceDescriptorProto& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(ServiceDescriptorProto* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.ServiceDescriptorProto"; + } protected: - explicit ServiceDescriptorProto(::google::protobuf::Arena* arena); + explicit ServiceDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -2680,94 +2985,100 @@ class LIBPROTOBUF_EXPORT ServiceDescriptorProto : public ::google::protobuf::Mes } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kMethodFieldNumber = 2, + kNameFieldNumber = 1, + kOptionsFieldNumber = 3, + }; // repeated .google.protobuf.MethodDescriptorProto method = 2; int method_size() const; void clear_method(); - static const int kMethodFieldNumber = 2; - ::google::protobuf::MethodDescriptorProto* mutable_method(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::MethodDescriptorProto >* + PROTOBUF_NAMESPACE_ID::MethodDescriptorProto* mutable_method(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::MethodDescriptorProto >* mutable_method(); - const ::google::protobuf::MethodDescriptorProto& method(int index) const; - ::google::protobuf::MethodDescriptorProto* add_method(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::MethodDescriptorProto >& + const PROTOBUF_NAMESPACE_ID::MethodDescriptorProto& method(int index) const; + PROTOBUF_NAMESPACE_ID::MethodDescriptorProto* add_method(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::MethodDescriptorProto >& method() const; // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name(); + std::string* unsafe_arena_release_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name( - ::std::string* name); + std::string* name); // optional .google.protobuf.ServiceOptions options = 3; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 3; - const ::google::protobuf::ServiceOptions& options() const; - ::google::protobuf::ServiceOptions* release_options(); - ::google::protobuf::ServiceOptions* mutable_options(); - void set_allocated_options(::google::protobuf::ServiceOptions* options); + const PROTOBUF_NAMESPACE_ID::ServiceOptions& options() const; + PROTOBUF_NAMESPACE_ID::ServiceOptions* release_options(); + PROTOBUF_NAMESPACE_ID::ServiceOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::ServiceOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::ServiceOptions* options); - ::google::protobuf::ServiceOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::ServiceOptions* options); + PROTOBUF_NAMESPACE_ID::ServiceOptions* unsafe_arena_release_options(); // @@protoc_insertion_point(class_scope:google.protobuf.ServiceDescriptorProto) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::MethodDescriptorProto > method_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::ServiceOptions* options_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::MethodDescriptorProto > method_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; + PROTOBUF_NAMESPACE_ID::ServiceOptions* options_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.MethodDescriptorProto) */ { +class PROTOBUF_EXPORT MethodDescriptorProto : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.MethodDescriptorProto) */ { public: MethodDescriptorProto(); virtual ~MethodDescriptorProto(); MethodDescriptorProto(const MethodDescriptorProto& from); - - inline MethodDescriptorProto& operator=(const MethodDescriptorProto& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 MethodDescriptorProto(MethodDescriptorProto&& from) noexcept : MethodDescriptorProto() { *this = ::std::move(from); } + inline MethodDescriptorProto& operator=(const MethodDescriptorProto& from) { + CopyFrom(from); + return *this; + } inline MethodDescriptorProto& operator=(MethodDescriptorProto&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -2776,22 +3087,28 @@ class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Mess } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const MethodDescriptorProto& default_instance(); @@ -2803,54 +3120,66 @@ class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Mess static constexpr int kIndexInFileMessages = 12; - void UnsafeArenaSwap(MethodDescriptorProto* other); - void Swap(MethodDescriptorProto* other); friend void swap(MethodDescriptorProto& a, MethodDescriptorProto& b) { a.Swap(&b); } + inline void Swap(MethodDescriptorProto* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(MethodDescriptorProto* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline MethodDescriptorProto* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - MethodDescriptorProto* New(::google::protobuf::Arena* arena) const final { + MethodDescriptorProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const MethodDescriptorProto& from); void MergeFrom(const MethodDescriptorProto& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(MethodDescriptorProto* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.MethodDescriptorProto"; + } protected: - explicit MethodDescriptorProto(::google::protobuf::Arena* arena); + explicit MethodDescriptorProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -2858,147 +3187,149 @@ class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Mess } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kNameFieldNumber = 1, + kInputTypeFieldNumber = 2, + kOutputTypeFieldNumber = 3, + kOptionsFieldNumber = 4, + kClientStreamingFieldNumber = 5, + kServerStreamingFieldNumber = 6, + }; // optional string name = 1; bool has_name() const; void clear_name(); - static const int kNameFieldNumber = 1; - const ::std::string& name() const; - void set_name(const ::std::string& value); - #if LANG_CXX11 - void set_name(::std::string&& value); - #endif + 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); + std::string* mutable_name(); + std::string* release_name(); + void set_allocated_name(std::string* name); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name(); + std::string* unsafe_arena_release_name(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name( - ::std::string* name); + std::string* name); // optional string input_type = 2; bool has_input_type() const; void clear_input_type(); - static const int kInputTypeFieldNumber = 2; - const ::std::string& input_type() const; - void set_input_type(const ::std::string& value); - #if LANG_CXX11 - void set_input_type(::std::string&& value); - #endif + const std::string& input_type() const; + void set_input_type(const std::string& value); + void set_input_type(std::string&& value); void set_input_type(const char* value); void set_input_type(const char* value, size_t size); - ::std::string* mutable_input_type(); - ::std::string* release_input_type(); - void set_allocated_input_type(::std::string* input_type); + std::string* mutable_input_type(); + std::string* release_input_type(); + void set_allocated_input_type(std::string* input_type); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_input_type(); + std::string* unsafe_arena_release_input_type(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_input_type( - ::std::string* input_type); + std::string* input_type); // optional string output_type = 3; bool has_output_type() const; void clear_output_type(); - static const int kOutputTypeFieldNumber = 3; - const ::std::string& output_type() const; - void set_output_type(const ::std::string& value); - #if LANG_CXX11 - void set_output_type(::std::string&& value); - #endif + const std::string& output_type() const; + void set_output_type(const std::string& value); + void set_output_type(std::string&& value); void set_output_type(const char* value); void set_output_type(const char* value, size_t size); - ::std::string* mutable_output_type(); - ::std::string* release_output_type(); - void set_allocated_output_type(::std::string* output_type); + std::string* mutable_output_type(); + std::string* release_output_type(); + void set_allocated_output_type(std::string* output_type); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_output_type(); + std::string* unsafe_arena_release_output_type(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_output_type( - ::std::string* output_type); + std::string* output_type); // optional .google.protobuf.MethodOptions options = 4; bool has_options() const; void clear_options(); - static const int kOptionsFieldNumber = 4; - const ::google::protobuf::MethodOptions& options() const; - ::google::protobuf::MethodOptions* release_options(); - ::google::protobuf::MethodOptions* mutable_options(); - void set_allocated_options(::google::protobuf::MethodOptions* options); + const PROTOBUF_NAMESPACE_ID::MethodOptions& options() const; + PROTOBUF_NAMESPACE_ID::MethodOptions* release_options(); + PROTOBUF_NAMESPACE_ID::MethodOptions* mutable_options(); + void set_allocated_options(PROTOBUF_NAMESPACE_ID::MethodOptions* options); void unsafe_arena_set_allocated_options( - ::google::protobuf::MethodOptions* options); - ::google::protobuf::MethodOptions* unsafe_arena_release_options(); + PROTOBUF_NAMESPACE_ID::MethodOptions* options); + PROTOBUF_NAMESPACE_ID::MethodOptions* unsafe_arena_release_options(); // optional bool client_streaming = 5 [default = false]; bool has_client_streaming() const; void clear_client_streaming(); - static const int kClientStreamingFieldNumber = 5; bool client_streaming() const; void set_client_streaming(bool value); // optional bool server_streaming = 6 [default = false]; bool has_server_streaming() const; void clear_server_streaming(); - static const int kServerStreamingFieldNumber = 6; bool server_streaming() const; void set_server_streaming(bool value); // @@protoc_insertion_point(class_scope:google.protobuf.MethodDescriptorProto) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::internal::ArenaStringPtr name_; - ::google::protobuf::internal::ArenaStringPtr input_type_; - ::google::protobuf::internal::ArenaStringPtr output_type_; - ::google::protobuf::MethodOptions* options_; + ::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 input_type_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr output_type_; + PROTOBUF_NAMESPACE_ID::MethodOptions* options_; bool client_streaming_; bool server_streaming_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FileOptions) */ { +class PROTOBUF_EXPORT FileOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FileOptions) */ { public: FileOptions(); virtual ~FileOptions(); FileOptions(const FileOptions& from); - - inline FileOptions& operator=(const FileOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 FileOptions(FileOptions&& from) noexcept : FileOptions() { *this = ::std::move(from); } + inline FileOptions& operator=(const FileOptions& from) { + CopyFrom(from); + return *this; + } inline FileOptions& operator=(FileOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -3007,22 +3338,28 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message /* @@p } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const FileOptions& default_instance(); @@ -3034,54 +3371,66 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message /* @@p static constexpr int kIndexInFileMessages = 13; - void UnsafeArenaSwap(FileOptions* other); - void Swap(FileOptions* other); friend void swap(FileOptions& a, FileOptions& b) { a.Swap(&b); } + inline void Swap(FileOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(FileOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline FileOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - FileOptions* New(::google::protobuf::Arena* arena) const final { + FileOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const FileOptions& from); void MergeFrom(const FileOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(FileOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.FileOptions"; + } protected: - explicit FileOptions(::google::protobuf::Arena* arena); + explicit FileOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -3089,386 +3438,379 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message /* @@p } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- typedef FileOptions_OptimizeMode OptimizeMode; - static const OptimizeMode SPEED = + static constexpr OptimizeMode SPEED = FileOptions_OptimizeMode_SPEED; - static const OptimizeMode CODE_SIZE = + static constexpr OptimizeMode CODE_SIZE = FileOptions_OptimizeMode_CODE_SIZE; - static const OptimizeMode LITE_RUNTIME = + static constexpr OptimizeMode LITE_RUNTIME = FileOptions_OptimizeMode_LITE_RUNTIME; static inline bool OptimizeMode_IsValid(int value) { return FileOptions_OptimizeMode_IsValid(value); } - static const OptimizeMode OptimizeMode_MIN = + static constexpr OptimizeMode OptimizeMode_MIN = FileOptions_OptimizeMode_OptimizeMode_MIN; - static const OptimizeMode OptimizeMode_MAX = + static constexpr OptimizeMode OptimizeMode_MAX = FileOptions_OptimizeMode_OptimizeMode_MAX; - static const int OptimizeMode_ARRAYSIZE = + static constexpr int OptimizeMode_ARRAYSIZE = FileOptions_OptimizeMode_OptimizeMode_ARRAYSIZE; - static inline const ::google::protobuf::EnumDescriptor* + static inline const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* OptimizeMode_descriptor() { return FileOptions_OptimizeMode_descriptor(); } - static inline const ::std::string& OptimizeMode_Name(OptimizeMode value) { - return FileOptions_OptimizeMode_Name(value); + template + static inline const std::string& OptimizeMode_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function OptimizeMode_Name."); + return FileOptions_OptimizeMode_Name(enum_t_value); } - static inline bool OptimizeMode_Parse(const ::std::string& name, + static inline bool OptimizeMode_Parse(const std::string& name, OptimizeMode* value) { return FileOptions_OptimizeMode_Parse(name, value); } // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + kJavaPackageFieldNumber = 1, + kJavaOuterClassnameFieldNumber = 8, + kGoPackageFieldNumber = 11, + kObjcClassPrefixFieldNumber = 36, + kCsharpNamespaceFieldNumber = 37, + kSwiftPrefixFieldNumber = 39, + kPhpClassPrefixFieldNumber = 40, + kPhpNamespaceFieldNumber = 41, + kPhpMetadataNamespaceFieldNumber = 44, + kRubyPackageFieldNumber = 45, + kJavaMultipleFilesFieldNumber = 10, + kJavaGenerateEqualsAndHashFieldNumber = 20, + kJavaStringCheckUtf8FieldNumber = 27, + kCcGenericServicesFieldNumber = 16, + kJavaGenericServicesFieldNumber = 17, + kPyGenericServicesFieldNumber = 18, + kPhpGenericServicesFieldNumber = 42, + kDeprecatedFieldNumber = 23, + kCcEnableArenasFieldNumber = 31, + kOptimizeForFieldNumber = 9, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; // optional string java_package = 1; bool has_java_package() const; void clear_java_package(); - static const int kJavaPackageFieldNumber = 1; - const ::std::string& java_package() const; - void set_java_package(const ::std::string& value); - #if LANG_CXX11 - void set_java_package(::std::string&& value); - #endif + const std::string& java_package() const; + void set_java_package(const std::string& value); + void set_java_package(std::string&& value); void set_java_package(const char* value); void set_java_package(const char* value, size_t size); - ::std::string* mutable_java_package(); - ::std::string* release_java_package(); - void set_allocated_java_package(::std::string* java_package); + std::string* mutable_java_package(); + std::string* release_java_package(); + void set_allocated_java_package(std::string* java_package); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_java_package(); + std::string* unsafe_arena_release_java_package(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_java_package( - ::std::string* java_package); + std::string* java_package); // optional string java_outer_classname = 8; bool has_java_outer_classname() const; void clear_java_outer_classname(); - static const int kJavaOuterClassnameFieldNumber = 8; - const ::std::string& java_outer_classname() const; - void set_java_outer_classname(const ::std::string& value); - #if LANG_CXX11 - void set_java_outer_classname(::std::string&& value); - #endif + const std::string& java_outer_classname() const; + void set_java_outer_classname(const std::string& value); + void set_java_outer_classname(std::string&& value); void set_java_outer_classname(const char* value); void set_java_outer_classname(const char* value, size_t size); - ::std::string* mutable_java_outer_classname(); - ::std::string* release_java_outer_classname(); - void set_allocated_java_outer_classname(::std::string* java_outer_classname); + std::string* mutable_java_outer_classname(); + std::string* release_java_outer_classname(); + void set_allocated_java_outer_classname(std::string* java_outer_classname); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_java_outer_classname(); + std::string* unsafe_arena_release_java_outer_classname(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_java_outer_classname( - ::std::string* java_outer_classname); + std::string* java_outer_classname); // optional string go_package = 11; bool has_go_package() const; void clear_go_package(); - static const int kGoPackageFieldNumber = 11; - const ::std::string& go_package() const; - void set_go_package(const ::std::string& value); - #if LANG_CXX11 - void set_go_package(::std::string&& value); - #endif + const std::string& go_package() const; + void set_go_package(const std::string& value); + void set_go_package(std::string&& value); void set_go_package(const char* value); void set_go_package(const char* value, size_t size); - ::std::string* mutable_go_package(); - ::std::string* release_go_package(); - void set_allocated_go_package(::std::string* go_package); + std::string* mutable_go_package(); + std::string* release_go_package(); + void set_allocated_go_package(std::string* go_package); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_go_package(); + std::string* unsafe_arena_release_go_package(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_go_package( - ::std::string* go_package); + std::string* go_package); // optional string objc_class_prefix = 36; bool has_objc_class_prefix() const; void clear_objc_class_prefix(); - static const int kObjcClassPrefixFieldNumber = 36; - const ::std::string& objc_class_prefix() const; - void set_objc_class_prefix(const ::std::string& value); - #if LANG_CXX11 - void set_objc_class_prefix(::std::string&& value); - #endif + const std::string& objc_class_prefix() const; + void set_objc_class_prefix(const std::string& value); + void set_objc_class_prefix(std::string&& value); void set_objc_class_prefix(const char* value); void set_objc_class_prefix(const char* value, size_t size); - ::std::string* mutable_objc_class_prefix(); - ::std::string* release_objc_class_prefix(); - void set_allocated_objc_class_prefix(::std::string* objc_class_prefix); + std::string* mutable_objc_class_prefix(); + std::string* release_objc_class_prefix(); + void set_allocated_objc_class_prefix(std::string* objc_class_prefix); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_objc_class_prefix(); + std::string* unsafe_arena_release_objc_class_prefix(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_objc_class_prefix( - ::std::string* objc_class_prefix); + std::string* objc_class_prefix); // optional string csharp_namespace = 37; bool has_csharp_namespace() const; void clear_csharp_namespace(); - static const int kCsharpNamespaceFieldNumber = 37; - const ::std::string& csharp_namespace() const; - void set_csharp_namespace(const ::std::string& value); - #if LANG_CXX11 - void set_csharp_namespace(::std::string&& value); - #endif + const std::string& csharp_namespace() const; + void set_csharp_namespace(const std::string& value); + void set_csharp_namespace(std::string&& value); void set_csharp_namespace(const char* value); void set_csharp_namespace(const char* value, size_t size); - ::std::string* mutable_csharp_namespace(); - ::std::string* release_csharp_namespace(); - void set_allocated_csharp_namespace(::std::string* csharp_namespace); + std::string* mutable_csharp_namespace(); + std::string* release_csharp_namespace(); + void set_allocated_csharp_namespace(std::string* csharp_namespace); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_csharp_namespace(); + std::string* unsafe_arena_release_csharp_namespace(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_csharp_namespace( - ::std::string* csharp_namespace); + std::string* csharp_namespace); // optional string swift_prefix = 39; bool has_swift_prefix() const; void clear_swift_prefix(); - static const int kSwiftPrefixFieldNumber = 39; - const ::std::string& swift_prefix() const; - void set_swift_prefix(const ::std::string& value); - #if LANG_CXX11 - void set_swift_prefix(::std::string&& value); - #endif + const std::string& swift_prefix() const; + void set_swift_prefix(const std::string& value); + void set_swift_prefix(std::string&& value); void set_swift_prefix(const char* value); void set_swift_prefix(const char* value, size_t size); - ::std::string* mutable_swift_prefix(); - ::std::string* release_swift_prefix(); - void set_allocated_swift_prefix(::std::string* swift_prefix); + std::string* mutable_swift_prefix(); + std::string* release_swift_prefix(); + void set_allocated_swift_prefix(std::string* swift_prefix); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_swift_prefix(); + std::string* unsafe_arena_release_swift_prefix(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_swift_prefix( - ::std::string* swift_prefix); + std::string* swift_prefix); // optional string php_class_prefix = 40; bool has_php_class_prefix() const; void clear_php_class_prefix(); - static const int kPhpClassPrefixFieldNumber = 40; - const ::std::string& php_class_prefix() const; - void set_php_class_prefix(const ::std::string& value); - #if LANG_CXX11 - void set_php_class_prefix(::std::string&& value); - #endif + const std::string& php_class_prefix() const; + void set_php_class_prefix(const std::string& value); + void set_php_class_prefix(std::string&& value); void set_php_class_prefix(const char* value); void set_php_class_prefix(const char* value, size_t size); - ::std::string* mutable_php_class_prefix(); - ::std::string* release_php_class_prefix(); - void set_allocated_php_class_prefix(::std::string* php_class_prefix); + std::string* mutable_php_class_prefix(); + std::string* release_php_class_prefix(); + void set_allocated_php_class_prefix(std::string* php_class_prefix); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_php_class_prefix(); + std::string* unsafe_arena_release_php_class_prefix(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_php_class_prefix( - ::std::string* php_class_prefix); + std::string* php_class_prefix); // optional string php_namespace = 41; bool has_php_namespace() const; void clear_php_namespace(); - static const int kPhpNamespaceFieldNumber = 41; - const ::std::string& php_namespace() const; - void set_php_namespace(const ::std::string& value); - #if LANG_CXX11 - void set_php_namespace(::std::string&& value); - #endif + const std::string& php_namespace() const; + void set_php_namespace(const std::string& value); + void set_php_namespace(std::string&& value); void set_php_namespace(const char* value); void set_php_namespace(const char* value, size_t size); - ::std::string* mutable_php_namespace(); - ::std::string* release_php_namespace(); - void set_allocated_php_namespace(::std::string* php_namespace); + std::string* mutable_php_namespace(); + std::string* release_php_namespace(); + void set_allocated_php_namespace(std::string* php_namespace); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_php_namespace(); + std::string* unsafe_arena_release_php_namespace(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_php_namespace( - ::std::string* php_namespace); + std::string* php_namespace); // optional string php_metadata_namespace = 44; bool has_php_metadata_namespace() const; void clear_php_metadata_namespace(); - static const int kPhpMetadataNamespaceFieldNumber = 44; - const ::std::string& php_metadata_namespace() const; - void set_php_metadata_namespace(const ::std::string& value); - #if LANG_CXX11 - void set_php_metadata_namespace(::std::string&& value); - #endif + const std::string& php_metadata_namespace() const; + void set_php_metadata_namespace(const std::string& value); + void set_php_metadata_namespace(std::string&& value); void set_php_metadata_namespace(const char* value); void set_php_metadata_namespace(const char* value, size_t size); - ::std::string* mutable_php_metadata_namespace(); - ::std::string* release_php_metadata_namespace(); - void set_allocated_php_metadata_namespace(::std::string* php_metadata_namespace); + std::string* mutable_php_metadata_namespace(); + std::string* release_php_metadata_namespace(); + void set_allocated_php_metadata_namespace(std::string* php_metadata_namespace); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_php_metadata_namespace(); + std::string* unsafe_arena_release_php_metadata_namespace(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_php_metadata_namespace( - ::std::string* php_metadata_namespace); + std::string* php_metadata_namespace); // optional string ruby_package = 45; bool has_ruby_package() const; void clear_ruby_package(); - static const int kRubyPackageFieldNumber = 45; - const ::std::string& ruby_package() const; - void set_ruby_package(const ::std::string& value); - #if LANG_CXX11 - void set_ruby_package(::std::string&& value); - #endif + const std::string& ruby_package() const; + void set_ruby_package(const std::string& value); + void set_ruby_package(std::string&& value); void set_ruby_package(const char* value); void set_ruby_package(const char* value, size_t size); - ::std::string* mutable_ruby_package(); - ::std::string* release_ruby_package(); - void set_allocated_ruby_package(::std::string* ruby_package); + std::string* mutable_ruby_package(); + std::string* release_ruby_package(); + void set_allocated_ruby_package(std::string* ruby_package); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_ruby_package(); + std::string* unsafe_arena_release_ruby_package(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_ruby_package( - ::std::string* ruby_package); + std::string* ruby_package); // optional bool java_multiple_files = 10 [default = false]; bool has_java_multiple_files() const; void clear_java_multiple_files(); - static const int kJavaMultipleFilesFieldNumber = 10; bool java_multiple_files() const; void set_java_multiple_files(bool value); // optional bool java_generate_equals_and_hash = 20 [deprecated = true]; - GOOGLE_PROTOBUF_DEPRECATED bool has_java_generate_equals_and_hash() const; - GOOGLE_PROTOBUF_DEPRECATED void clear_java_generate_equals_and_hash(); - GOOGLE_PROTOBUF_DEPRECATED static const int kJavaGenerateEqualsAndHashFieldNumber = 20; - GOOGLE_PROTOBUF_DEPRECATED bool java_generate_equals_and_hash() const; - GOOGLE_PROTOBUF_DEPRECATED void set_java_generate_equals_and_hash(bool value); + PROTOBUF_DEPRECATED bool has_java_generate_equals_and_hash() const; + PROTOBUF_DEPRECATED void clear_java_generate_equals_and_hash(); + PROTOBUF_DEPRECATED bool java_generate_equals_and_hash() const; + PROTOBUF_DEPRECATED void set_java_generate_equals_and_hash(bool value); // optional bool java_string_check_utf8 = 27 [default = false]; bool has_java_string_check_utf8() const; void clear_java_string_check_utf8(); - static const int kJavaStringCheckUtf8FieldNumber = 27; bool java_string_check_utf8() const; void set_java_string_check_utf8(bool value); // optional bool cc_generic_services = 16 [default = false]; bool has_cc_generic_services() const; void clear_cc_generic_services(); - static const int kCcGenericServicesFieldNumber = 16; bool cc_generic_services() const; void set_cc_generic_services(bool value); // optional bool java_generic_services = 17 [default = false]; bool has_java_generic_services() const; void clear_java_generic_services(); - static const int kJavaGenericServicesFieldNumber = 17; bool java_generic_services() const; void set_java_generic_services(bool value); // optional bool py_generic_services = 18 [default = false]; bool has_py_generic_services() const; void clear_py_generic_services(); - static const int kPyGenericServicesFieldNumber = 18; bool py_generic_services() const; void set_py_generic_services(bool value); // optional bool php_generic_services = 42 [default = false]; bool has_php_generic_services() const; void clear_php_generic_services(); - static const int kPhpGenericServicesFieldNumber = 42; bool php_generic_services() const; void set_php_generic_services(bool value); // optional bool deprecated = 23 [default = false]; bool has_deprecated() const; void clear_deprecated(); - static const int kDeprecatedFieldNumber = 23; bool deprecated() const; void set_deprecated(bool value); // optional bool cc_enable_arenas = 31 [default = false]; bool has_cc_enable_arenas() const; void clear_cc_enable_arenas(); - static const int kCcEnableArenasFieldNumber = 31; bool cc_enable_arenas() const; void set_cc_enable_arenas(bool value); // optional .google.protobuf.FileOptions.OptimizeMode optimize_for = 9 [default = SPEED]; bool has_optimize_for() const; void clear_optimize_for(); - static const int kOptimizeForFieldNumber = 9; - ::google::protobuf::FileOptions_OptimizeMode optimize_for() const; - void set_optimize_for(::google::protobuf::FileOptions_OptimizeMode value); + PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode optimize_for() const; + void set_optimize_for(PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode value); GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(FileOptions) // @@protoc_insertion_point(class_scope:google.protobuf.FileOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; - ::google::protobuf::internal::ArenaStringPtr java_package_; - ::google::protobuf::internal::ArenaStringPtr java_outer_classname_; - ::google::protobuf::internal::ArenaStringPtr go_package_; - ::google::protobuf::internal::ArenaStringPtr objc_class_prefix_; - ::google::protobuf::internal::ArenaStringPtr csharp_namespace_; - ::google::protobuf::internal::ArenaStringPtr swift_prefix_; - ::google::protobuf::internal::ArenaStringPtr php_class_prefix_; - ::google::protobuf::internal::ArenaStringPtr php_namespace_; - ::google::protobuf::internal::ArenaStringPtr php_metadata_namespace_; - ::google::protobuf::internal::ArenaStringPtr ruby_package_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr java_package_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr java_outer_classname_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr go_package_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr objc_class_prefix_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr csharp_namespace_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr swift_prefix_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr php_class_prefix_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr php_namespace_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr php_metadata_namespace_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr ruby_package_; bool java_multiple_files_; bool java_generate_equals_and_hash_; bool java_string_check_utf8_; @@ -3483,23 +3825,22 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message /* @@p }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.MessageOptions) */ { +class PROTOBUF_EXPORT MessageOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.MessageOptions) */ { public: MessageOptions(); virtual ~MessageOptions(); MessageOptions(const MessageOptions& from); - - inline MessageOptions& operator=(const MessageOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 MessageOptions(MessageOptions&& from) noexcept : MessageOptions() { *this = ::std::move(from); } + inline MessageOptions& operator=(const MessageOptions& from) { + CopyFrom(from); + return *this; + } inline MessageOptions& operator=(MessageOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -3508,22 +3849,28 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message /* } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const MessageOptions& default_instance(); @@ -3535,54 +3882,66 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message /* static constexpr int kIndexInFileMessages = 14; - void UnsafeArenaSwap(MessageOptions* other); - void Swap(MessageOptions* other); friend void swap(MessageOptions& a, MessageOptions& b) { a.Swap(&b); } + inline void Swap(MessageOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(MessageOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline MessageOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - MessageOptions* New(::google::protobuf::Arena* arena) const final { + MessageOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const MessageOptions& from); void MergeFrom(const MessageOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(MessageOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.MessageOptions"; + } protected: - explicit MessageOptions(::google::protobuf::Arena* arena); + explicit MessageOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -3590,66 +3949,75 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message /* } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + kMessageSetWireFormatFieldNumber = 1, + kNoStandardDescriptorAccessorFieldNumber = 2, + kDeprecatedFieldNumber = 3, + kMapEntryFieldNumber = 7, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; // optional bool message_set_wire_format = 1 [default = false]; bool has_message_set_wire_format() const; void clear_message_set_wire_format(); - static const int kMessageSetWireFormatFieldNumber = 1; bool message_set_wire_format() const; void set_message_set_wire_format(bool value); // optional bool no_standard_descriptor_accessor = 2 [default = false]; bool has_no_standard_descriptor_accessor() const; void clear_no_standard_descriptor_accessor(); - static const int kNoStandardDescriptorAccessorFieldNumber = 2; bool no_standard_descriptor_accessor() const; void set_no_standard_descriptor_accessor(bool value); // optional bool deprecated = 3 [default = false]; bool has_deprecated() const; void clear_deprecated(); - static const int kDeprecatedFieldNumber = 3; bool deprecated() const; void set_deprecated(bool value); // optional bool map_entry = 7; bool has_map_entry() const; void clear_map_entry(); - static const int kMapEntryFieldNumber = 7; bool map_entry() const; void set_map_entry(bool value); GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(MessageOptions) // @@protoc_insertion_point(class_scope:google.protobuf.MessageOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; bool message_set_wire_format_; bool no_standard_descriptor_accessor_; bool deprecated_; @@ -3658,23 +4026,22 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message /* }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FieldOptions) */ { +class PROTOBUF_EXPORT FieldOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FieldOptions) */ { public: FieldOptions(); virtual ~FieldOptions(); FieldOptions(const FieldOptions& from); - - inline FieldOptions& operator=(const FieldOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 FieldOptions(FieldOptions&& from) noexcept : FieldOptions() { *this = ::std::move(from); } + inline FieldOptions& operator=(const FieldOptions& from) { + CopyFrom(from); + return *this; + } inline FieldOptions& operator=(FieldOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -3683,22 +4050,28 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message /* @@ } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const FieldOptions& default_instance(); @@ -3710,54 +4083,66 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message /* @@ static constexpr int kIndexInFileMessages = 15; - void UnsafeArenaSwap(FieldOptions* other); - void Swap(FieldOptions* other); friend void swap(FieldOptions& a, FieldOptions& b) { a.Swap(&b); } + inline void Swap(FieldOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(FieldOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline FieldOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - FieldOptions* New(::google::protobuf::Arena* arena) const final { + FieldOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const FieldOptions& from); void MergeFrom(const FieldOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(FieldOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.FieldOptions"; + } protected: - explicit FieldOptions(::google::protobuf::Arena* arena); + explicit FieldOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -3765,136 +4150,153 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message /* @@ } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- typedef FieldOptions_CType CType; - static const CType STRING = + static constexpr CType STRING = FieldOptions_CType_STRING; - static const CType CORD = + static constexpr CType CORD = FieldOptions_CType_CORD; - static const CType STRING_PIECE = + static constexpr CType STRING_PIECE = FieldOptions_CType_STRING_PIECE; static inline bool CType_IsValid(int value) { return FieldOptions_CType_IsValid(value); } - static const CType CType_MIN = + static constexpr CType CType_MIN = FieldOptions_CType_CType_MIN; - static const CType CType_MAX = + static constexpr CType CType_MAX = FieldOptions_CType_CType_MAX; - static const int CType_ARRAYSIZE = + static constexpr int CType_ARRAYSIZE = FieldOptions_CType_CType_ARRAYSIZE; - static inline const ::google::protobuf::EnumDescriptor* + static inline const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* CType_descriptor() { return FieldOptions_CType_descriptor(); } - static inline const ::std::string& CType_Name(CType value) { - return FieldOptions_CType_Name(value); + template + static inline const std::string& CType_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function CType_Name."); + return FieldOptions_CType_Name(enum_t_value); } - static inline bool CType_Parse(const ::std::string& name, + static inline bool CType_Parse(const std::string& name, CType* value) { return FieldOptions_CType_Parse(name, value); } typedef FieldOptions_JSType JSType; - static const JSType JS_NORMAL = + static constexpr JSType JS_NORMAL = FieldOptions_JSType_JS_NORMAL; - static const JSType JS_STRING = + static constexpr JSType JS_STRING = FieldOptions_JSType_JS_STRING; - static const JSType JS_NUMBER = + static constexpr JSType JS_NUMBER = FieldOptions_JSType_JS_NUMBER; static inline bool JSType_IsValid(int value) { return FieldOptions_JSType_IsValid(value); } - static const JSType JSType_MIN = + static constexpr JSType JSType_MIN = FieldOptions_JSType_JSType_MIN; - static const JSType JSType_MAX = + static constexpr JSType JSType_MAX = FieldOptions_JSType_JSType_MAX; - static const int JSType_ARRAYSIZE = + static constexpr int JSType_ARRAYSIZE = FieldOptions_JSType_JSType_ARRAYSIZE; - static inline const ::google::protobuf::EnumDescriptor* + static inline const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* JSType_descriptor() { return FieldOptions_JSType_descriptor(); } - static inline const ::std::string& JSType_Name(JSType value) { - return FieldOptions_JSType_Name(value); + template + static inline const std::string& JSType_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function JSType_Name."); + return FieldOptions_JSType_Name(enum_t_value); } - static inline bool JSType_Parse(const ::std::string& name, + static inline bool JSType_Parse(const std::string& name, JSType* value) { return FieldOptions_JSType_Parse(name, value); } // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + kCtypeFieldNumber = 1, + kPackedFieldNumber = 2, + kLazyFieldNumber = 5, + kDeprecatedFieldNumber = 3, + kWeakFieldNumber = 10, + kJstypeFieldNumber = 6, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING]; bool has_ctype() const; void clear_ctype(); - static const int kCtypeFieldNumber = 1; - ::google::protobuf::FieldOptions_CType ctype() const; - void set_ctype(::google::protobuf::FieldOptions_CType value); + PROTOBUF_NAMESPACE_ID::FieldOptions_CType ctype() const; + void set_ctype(PROTOBUF_NAMESPACE_ID::FieldOptions_CType value); // optional bool packed = 2; bool has_packed() const; void clear_packed(); - static const int kPackedFieldNumber = 2; bool packed() const; void set_packed(bool value); // optional bool lazy = 5 [default = false]; bool has_lazy() const; void clear_lazy(); - static const int kLazyFieldNumber = 5; bool lazy() const; void set_lazy(bool value); // optional bool deprecated = 3 [default = false]; bool has_deprecated() const; void clear_deprecated(); - static const int kDeprecatedFieldNumber = 3; bool deprecated() const; void set_deprecated(bool value); // optional bool weak = 10 [default = false]; bool has_weak() const; void clear_weak(); - static const int kWeakFieldNumber = 10; bool weak() const; void set_weak(bool value); // optional .google.protobuf.FieldOptions.JSType jstype = 6 [default = JS_NORMAL]; bool has_jstype() const; void clear_jstype(); - static const int kJstypeFieldNumber = 6; - ::google::protobuf::FieldOptions_JSType jstype() const; - void set_jstype(::google::protobuf::FieldOptions_JSType value); + PROTOBUF_NAMESPACE_ID::FieldOptions_JSType jstype() const; + void set_jstype(PROTOBUF_NAMESPACE_ID::FieldOptions_JSType value); GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(FieldOptions) // @@protoc_insertion_point(class_scope:google.protobuf.FieldOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; int ctype_; bool packed_; bool lazy_; @@ -3905,23 +4307,22 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message /* @@ }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT OneofOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.OneofOptions) */ { +class PROTOBUF_EXPORT OneofOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.OneofOptions) */ { public: OneofOptions(); virtual ~OneofOptions(); OneofOptions(const OneofOptions& from); - - inline OneofOptions& operator=(const OneofOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 OneofOptions(OneofOptions&& from) noexcept : OneofOptions() { *this = ::std::move(from); } + inline OneofOptions& operator=(const OneofOptions& from) { + CopyFrom(from); + return *this; + } inline OneofOptions& operator=(OneofOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -3930,22 +4331,28 @@ class LIBPROTOBUF_EXPORT OneofOptions : public ::google::protobuf::Message /* @@ } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const OneofOptions& default_instance(); @@ -3957,54 +4364,66 @@ class LIBPROTOBUF_EXPORT OneofOptions : public ::google::protobuf::Message /* @@ static constexpr int kIndexInFileMessages = 16; - void UnsafeArenaSwap(OneofOptions* other); - void Swap(OneofOptions* other); friend void swap(OneofOptions& a, OneofOptions& b) { a.Swap(&b); } + inline void Swap(OneofOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(OneofOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline OneofOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - OneofOptions* New(::google::protobuf::Arena* arena) const final { + OneofOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const OneofOptions& from); void MergeFrom(const OneofOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(OneofOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.OneofOptions"; + } protected: - explicit OneofOptions(::google::protobuf::Arena* arena); + explicit OneofOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -4012,59 +4431,67 @@ class LIBPROTOBUF_EXPORT OneofOptions : public ::google::protobuf::Message /* @@ } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(OneofOptions) // @@protoc_insertion_point(class_scope:google.protobuf.OneofOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT EnumOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumOptions) */ { +class PROTOBUF_EXPORT EnumOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumOptions) */ { public: EnumOptions(); virtual ~EnumOptions(); EnumOptions(const EnumOptions& from); - - inline EnumOptions& operator=(const EnumOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 EnumOptions(EnumOptions&& from) noexcept : EnumOptions() { *this = ::std::move(from); } + inline EnumOptions& operator=(const EnumOptions& from) { + CopyFrom(from); + return *this; + } inline EnumOptions& operator=(EnumOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -4073,22 +4500,28 @@ class LIBPROTOBUF_EXPORT EnumOptions : public ::google::protobuf::Message /* @@p } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const EnumOptions& default_instance(); @@ -4100,54 +4533,66 @@ class LIBPROTOBUF_EXPORT EnumOptions : public ::google::protobuf::Message /* @@p static constexpr int kIndexInFileMessages = 17; - void UnsafeArenaSwap(EnumOptions* other); - void Swap(EnumOptions* other); friend void swap(EnumOptions& a, EnumOptions& b) { a.Swap(&b); } + inline void Swap(EnumOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(EnumOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline EnumOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - EnumOptions* New(::google::protobuf::Arena* arena) const final { + EnumOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const EnumOptions& from); void MergeFrom(const EnumOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(EnumOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.EnumOptions"; + } protected: - explicit EnumOptions(::google::protobuf::Arena* arena); + explicit EnumOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -4155,75 +4600,83 @@ class LIBPROTOBUF_EXPORT EnumOptions : public ::google::protobuf::Message /* @@p } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + kAllowAliasFieldNumber = 2, + kDeprecatedFieldNumber = 3, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; // optional bool allow_alias = 2; bool has_allow_alias() const; void clear_allow_alias(); - static const int kAllowAliasFieldNumber = 2; bool allow_alias() const; void set_allow_alias(bool value); // optional bool deprecated = 3 [default = false]; bool has_deprecated() const; void clear_deprecated(); - static const int kDeprecatedFieldNumber = 3; bool deprecated() const; void set_deprecated(bool value); GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(EnumOptions) // @@protoc_insertion_point(class_scope:google.protobuf.EnumOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; bool allow_alias_; bool deprecated_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT EnumValueOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumValueOptions) */ { +class PROTOBUF_EXPORT EnumValueOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.EnumValueOptions) */ { public: EnumValueOptions(); virtual ~EnumValueOptions(); EnumValueOptions(const EnumValueOptions& from); - - inline EnumValueOptions& operator=(const EnumValueOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 EnumValueOptions(EnumValueOptions&& from) noexcept : EnumValueOptions() { *this = ::std::move(from); } + inline EnumValueOptions& operator=(const EnumValueOptions& from) { + CopyFrom(from); + return *this; + } inline EnumValueOptions& operator=(EnumValueOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -4232,22 +4685,28 @@ class LIBPROTOBUF_EXPORT EnumValueOptions : public ::google::protobuf::Message / } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const EnumValueOptions& default_instance(); @@ -4259,54 +4718,66 @@ class LIBPROTOBUF_EXPORT EnumValueOptions : public ::google::protobuf::Message / static constexpr int kIndexInFileMessages = 18; - void UnsafeArenaSwap(EnumValueOptions* other); - void Swap(EnumValueOptions* other); friend void swap(EnumValueOptions& a, EnumValueOptions& b) { a.Swap(&b); } + inline void Swap(EnumValueOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(EnumValueOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline EnumValueOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - EnumValueOptions* New(::google::protobuf::Arena* arena) const final { + EnumValueOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const EnumValueOptions& from); void MergeFrom(const EnumValueOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(EnumValueOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.EnumValueOptions"; + } protected: - explicit EnumValueOptions(::google::protobuf::Arena* arena); + explicit EnumValueOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -4314,67 +4785,75 @@ class LIBPROTOBUF_EXPORT EnumValueOptions : public ::google::protobuf::Message / } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + kDeprecatedFieldNumber = 1, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; // optional bool deprecated = 1 [default = false]; bool has_deprecated() const; void clear_deprecated(); - static const int kDeprecatedFieldNumber = 1; bool deprecated() const; void set_deprecated(bool value); GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(EnumValueOptions) // @@protoc_insertion_point(class_scope:google.protobuf.EnumValueOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; bool deprecated_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT ServiceOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.ServiceOptions) */ { +class PROTOBUF_EXPORT ServiceOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.ServiceOptions) */ { public: ServiceOptions(); virtual ~ServiceOptions(); ServiceOptions(const ServiceOptions& from); - - inline ServiceOptions& operator=(const ServiceOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 ServiceOptions(ServiceOptions&& from) noexcept : ServiceOptions() { *this = ::std::move(from); } + inline ServiceOptions& operator=(const ServiceOptions& from) { + CopyFrom(from); + return *this; + } inline ServiceOptions& operator=(ServiceOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -4383,22 +4862,28 @@ class LIBPROTOBUF_EXPORT ServiceOptions : public ::google::protobuf::Message /* } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const ServiceOptions& default_instance(); @@ -4410,54 +4895,66 @@ class LIBPROTOBUF_EXPORT ServiceOptions : public ::google::protobuf::Message /* static constexpr int kIndexInFileMessages = 19; - void UnsafeArenaSwap(ServiceOptions* other); - void Swap(ServiceOptions* other); friend void swap(ServiceOptions& a, ServiceOptions& b) { a.Swap(&b); } + inline void Swap(ServiceOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(ServiceOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline ServiceOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - ServiceOptions* New(::google::protobuf::Arena* arena) const final { + ServiceOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const ServiceOptions& from); void MergeFrom(const ServiceOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(ServiceOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.ServiceOptions"; + } protected: - explicit ServiceOptions(::google::protobuf::Arena* arena); + explicit ServiceOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -4465,67 +4962,75 @@ class LIBPROTOBUF_EXPORT ServiceOptions : public ::google::protobuf::Message /* } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + kDeprecatedFieldNumber = 33, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; // optional bool deprecated = 33 [default = false]; bool has_deprecated() const; void clear_deprecated(); - static const int kDeprecatedFieldNumber = 33; bool deprecated() const; void set_deprecated(bool value); GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(ServiceOptions) // @@protoc_insertion_point(class_scope:google.protobuf.ServiceOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; bool deprecated_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT MethodOptions : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.MethodOptions) */ { +class PROTOBUF_EXPORT MethodOptions : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.MethodOptions) */ { public: MethodOptions(); virtual ~MethodOptions(); MethodOptions(const MethodOptions& from); - - inline MethodOptions& operator=(const MethodOptions& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 MethodOptions(MethodOptions&& from) noexcept : MethodOptions() { *this = ::std::move(from); } + inline MethodOptions& operator=(const MethodOptions& from) { + CopyFrom(from); + return *this; + } inline MethodOptions& operator=(MethodOptions&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -4534,22 +5039,28 @@ class LIBPROTOBUF_EXPORT MethodOptions : public ::google::protobuf::Message /* @ } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const MethodOptions& default_instance(); @@ -4561,54 +5072,66 @@ class LIBPROTOBUF_EXPORT MethodOptions : public ::google::protobuf::Message /* @ static constexpr int kIndexInFileMessages = 20; - void UnsafeArenaSwap(MethodOptions* other); - void Swap(MethodOptions* other); friend void swap(MethodOptions& a, MethodOptions& b) { a.Swap(&b); } + inline void Swap(MethodOptions* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(MethodOptions* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline MethodOptions* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - MethodOptions* New(::google::protobuf::Arena* arena) const final { + MethodOptions* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const MethodOptions& from); void MergeFrom(const MethodOptions& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(MethodOptions* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.MethodOptions"; + } protected: - explicit MethodOptions(::google::protobuf::Arena* arena); + explicit MethodOptions(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -4616,103 +5139,115 @@ class LIBPROTOBUF_EXPORT MethodOptions : public ::google::protobuf::Message /* @ } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- typedef MethodOptions_IdempotencyLevel IdempotencyLevel; - static const IdempotencyLevel IDEMPOTENCY_UNKNOWN = + static constexpr IdempotencyLevel IDEMPOTENCY_UNKNOWN = MethodOptions_IdempotencyLevel_IDEMPOTENCY_UNKNOWN; - static const IdempotencyLevel NO_SIDE_EFFECTS = + static constexpr IdempotencyLevel NO_SIDE_EFFECTS = MethodOptions_IdempotencyLevel_NO_SIDE_EFFECTS; - static const IdempotencyLevel IDEMPOTENT = + static constexpr IdempotencyLevel IDEMPOTENT = MethodOptions_IdempotencyLevel_IDEMPOTENT; static inline bool IdempotencyLevel_IsValid(int value) { return MethodOptions_IdempotencyLevel_IsValid(value); } - static const IdempotencyLevel IdempotencyLevel_MIN = + static constexpr IdempotencyLevel IdempotencyLevel_MIN = MethodOptions_IdempotencyLevel_IdempotencyLevel_MIN; - static const IdempotencyLevel IdempotencyLevel_MAX = + static constexpr IdempotencyLevel IdempotencyLevel_MAX = MethodOptions_IdempotencyLevel_IdempotencyLevel_MAX; - static const int IdempotencyLevel_ARRAYSIZE = + static constexpr int IdempotencyLevel_ARRAYSIZE = MethodOptions_IdempotencyLevel_IdempotencyLevel_ARRAYSIZE; - static inline const ::google::protobuf::EnumDescriptor* + static inline const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* IdempotencyLevel_descriptor() { return MethodOptions_IdempotencyLevel_descriptor(); } - static inline const ::std::string& IdempotencyLevel_Name(IdempotencyLevel value) { - return MethodOptions_IdempotencyLevel_Name(value); + template + static inline const std::string& IdempotencyLevel_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function IdempotencyLevel_Name."); + return MethodOptions_IdempotencyLevel_Name(enum_t_value); } - static inline bool IdempotencyLevel_Parse(const ::std::string& name, + static inline bool IdempotencyLevel_Parse(const std::string& name, IdempotencyLevel* value) { return MethodOptions_IdempotencyLevel_Parse(name, value); } // accessors ------------------------------------------------------- + enum : int { + kUninterpretedOptionFieldNumber = 999, + kDeprecatedFieldNumber = 33, + kIdempotencyLevelFieldNumber = 34, + }; // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; int uninterpreted_option_size() const; void clear_uninterpreted_option(); - static const int kUninterpretedOptionFieldNumber = 999; - ::google::protobuf::UninterpretedOption* mutable_uninterpreted_option(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption* mutable_uninterpreted_option(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* mutable_uninterpreted_option(); - const ::google::protobuf::UninterpretedOption& uninterpreted_option(int index) const; - ::google::protobuf::UninterpretedOption* add_uninterpreted_option(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption& uninterpreted_option(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption* add_uninterpreted_option(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& uninterpreted_option() const; // optional bool deprecated = 33 [default = false]; bool has_deprecated() const; void clear_deprecated(); - static const int kDeprecatedFieldNumber = 33; bool deprecated() const; void set_deprecated(bool value); // optional .google.protobuf.MethodOptions.IdempotencyLevel idempotency_level = 34 [default = IDEMPOTENCY_UNKNOWN]; bool has_idempotency_level() const; void clear_idempotency_level(); - static const int kIdempotencyLevelFieldNumber = 34; - ::google::protobuf::MethodOptions_IdempotencyLevel idempotency_level() const; - void set_idempotency_level(::google::protobuf::MethodOptions_IdempotencyLevel value); + PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel idempotency_level() const; + void set_idempotency_level(PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel value); GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(MethodOptions) // @@protoc_insertion_point(class_scope:google.protobuf.MethodOptions) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::ExtensionSet _extensions_; + ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption > uninterpreted_option_; bool deprecated_; int idempotency_level_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT UninterpretedOption_NamePart : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.UninterpretedOption.NamePart) */ { +class PROTOBUF_EXPORT UninterpretedOption_NamePart : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.UninterpretedOption.NamePart) */ { public: UninterpretedOption_NamePart(); virtual ~UninterpretedOption_NamePart(); UninterpretedOption_NamePart(const UninterpretedOption_NamePart& from); - - inline UninterpretedOption_NamePart& operator=(const UninterpretedOption_NamePart& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 UninterpretedOption_NamePart(UninterpretedOption_NamePart&& from) noexcept : UninterpretedOption_NamePart() { *this = ::std::move(from); } + inline UninterpretedOption_NamePart& operator=(const UninterpretedOption_NamePart& from) { + CopyFrom(from); + return *this; + } inline UninterpretedOption_NamePart& operator=(UninterpretedOption_NamePart&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -4721,22 +5256,28 @@ class LIBPROTOBUF_EXPORT UninterpretedOption_NamePart : public ::google::protobu } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const UninterpretedOption_NamePart& default_instance(); @@ -4748,54 +5289,66 @@ class LIBPROTOBUF_EXPORT UninterpretedOption_NamePart : public ::google::protobu static constexpr int kIndexInFileMessages = 21; - void UnsafeArenaSwap(UninterpretedOption_NamePart* other); - void Swap(UninterpretedOption_NamePart* other); friend void swap(UninterpretedOption_NamePart& a, UninterpretedOption_NamePart& b) { a.Swap(&b); } + inline void Swap(UninterpretedOption_NamePart* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(UninterpretedOption_NamePart* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline UninterpretedOption_NamePart* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - UninterpretedOption_NamePart* New(::google::protobuf::Arena* arena) const final { + UninterpretedOption_NamePart* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const UninterpretedOption_NamePart& from); void MergeFrom(const UninterpretedOption_NamePart& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(UninterpretedOption_NamePart* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.UninterpretedOption.NamePart"; + } protected: - explicit UninterpretedOption_NamePart(::google::protobuf::Arena* arena); + explicit UninterpretedOption_NamePart(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -4803,79 +5356,85 @@ class LIBPROTOBUF_EXPORT UninterpretedOption_NamePart : public ::google::protobu } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kNamePartFieldNumber = 1, + kIsExtensionFieldNumber = 2, + }; // required string name_part = 1; bool has_name_part() const; void clear_name_part(); - static const int kNamePartFieldNumber = 1; - const ::std::string& name_part() const; - void set_name_part(const ::std::string& value); - #if LANG_CXX11 - void set_name_part(::std::string&& value); - #endif + const std::string& name_part() const; + void set_name_part(const std::string& value); + void set_name_part(std::string&& value); void set_name_part(const char* value); void set_name_part(const char* value, size_t size); - ::std::string* mutable_name_part(); - ::std::string* release_name_part(); - void set_allocated_name_part(::std::string* name_part); + std::string* mutable_name_part(); + std::string* release_name_part(); + void set_allocated_name_part(std::string* name_part); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_name_part(); + std::string* unsafe_arena_release_name_part(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_name_part( - ::std::string* name_part); + std::string* name_part); // required bool is_extension = 2; bool has_is_extension() const; void clear_is_extension(); - static const int kIsExtensionFieldNumber = 2; bool is_extension() const; void set_is_extension(bool value); // @@protoc_insertion_point(class_scope:google.protobuf.UninterpretedOption.NamePart) private: - class HasBitSetters; + class _Internal; // helper for ByteSizeLong() size_t RequiredFieldsByteSizeFallback() const; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::internal::ArenaStringPtr name_part_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_part_; bool is_extension_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.UninterpretedOption) */ { +class PROTOBUF_EXPORT UninterpretedOption : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.UninterpretedOption) */ { public: UninterpretedOption(); virtual ~UninterpretedOption(); UninterpretedOption(const UninterpretedOption& from); - - inline UninterpretedOption& operator=(const UninterpretedOption& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 UninterpretedOption(UninterpretedOption&& from) noexcept : UninterpretedOption() { *this = ::std::move(from); } + inline UninterpretedOption& operator=(const UninterpretedOption& from) { + CopyFrom(from); + return *this; + } inline UninterpretedOption& operator=(UninterpretedOption&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -4884,22 +5443,28 @@ class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Messag } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const UninterpretedOption& default_instance(); @@ -4911,54 +5476,66 @@ class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Messag static constexpr int kIndexInFileMessages = 22; - void UnsafeArenaSwap(UninterpretedOption* other); - void Swap(UninterpretedOption* other); friend void swap(UninterpretedOption& a, UninterpretedOption& b) { a.Swap(&b); } + inline void Swap(UninterpretedOption* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(UninterpretedOption* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline UninterpretedOption* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - UninterpretedOption* New(::google::protobuf::Arena* arena) const final { + UninterpretedOption* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const UninterpretedOption& from); void MergeFrom(const UninterpretedOption& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(UninterpretedOption* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.UninterpretedOption"; + } protected: - explicit UninterpretedOption(::google::protobuf::Arena* arena); + explicit UninterpretedOption(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -4966,7 +5543,14 @@ class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Messag } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- @@ -4974,149 +5558,144 @@ class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Messag // accessors ------------------------------------------------------- + enum : int { + kNameFieldNumber = 2, + kIdentifierValueFieldNumber = 3, + kStringValueFieldNumber = 7, + kAggregateValueFieldNumber = 8, + kPositiveIntValueFieldNumber = 4, + kNegativeIntValueFieldNumber = 5, + kDoubleValueFieldNumber = 6, + }; // repeated .google.protobuf.UninterpretedOption.NamePart name = 2; int name_size() const; void clear_name(); - static const int kNameFieldNumber = 2; - ::google::protobuf::UninterpretedOption_NamePart* mutable_name(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption_NamePart >* + PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart* mutable_name(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart >* mutable_name(); - const ::google::protobuf::UninterpretedOption_NamePart& name(int index) const; - ::google::protobuf::UninterpretedOption_NamePart* add_name(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption_NamePart >& + const PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart& name(int index) const; + PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart* add_name(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart >& name() const; // optional string identifier_value = 3; bool has_identifier_value() const; void clear_identifier_value(); - static const int kIdentifierValueFieldNumber = 3; - const ::std::string& identifier_value() const; - void set_identifier_value(const ::std::string& value); - #if LANG_CXX11 - void set_identifier_value(::std::string&& value); - #endif + const std::string& identifier_value() const; + void set_identifier_value(const std::string& value); + void set_identifier_value(std::string&& value); void set_identifier_value(const char* value); void set_identifier_value(const char* value, size_t size); - ::std::string* mutable_identifier_value(); - ::std::string* release_identifier_value(); - void set_allocated_identifier_value(::std::string* identifier_value); + std::string* mutable_identifier_value(); + std::string* release_identifier_value(); + void set_allocated_identifier_value(std::string* identifier_value); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_identifier_value(); + std::string* unsafe_arena_release_identifier_value(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_identifier_value( - ::std::string* identifier_value); + std::string* identifier_value); // optional bytes string_value = 7; bool has_string_value() const; void clear_string_value(); - static const int kStringValueFieldNumber = 7; - const ::std::string& string_value() const; - void set_string_value(const ::std::string& value); - #if LANG_CXX11 - void set_string_value(::std::string&& value); - #endif + const std::string& string_value() const; + void set_string_value(const std::string& value); + void set_string_value(std::string&& value); void set_string_value(const char* value); void set_string_value(const void* value, size_t size); - ::std::string* mutable_string_value(); - ::std::string* release_string_value(); - void set_allocated_string_value(::std::string* string_value); + std::string* mutable_string_value(); + std::string* release_string_value(); + void set_allocated_string_value(std::string* string_value); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_string_value(); + std::string* unsafe_arena_release_string_value(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_string_value( - ::std::string* string_value); + std::string* string_value); // optional string aggregate_value = 8; bool has_aggregate_value() const; void clear_aggregate_value(); - static const int kAggregateValueFieldNumber = 8; - const ::std::string& aggregate_value() const; - void set_aggregate_value(const ::std::string& value); - #if LANG_CXX11 - void set_aggregate_value(::std::string&& value); - #endif + const std::string& aggregate_value() const; + void set_aggregate_value(const std::string& value); + void set_aggregate_value(std::string&& value); void set_aggregate_value(const char* value); void set_aggregate_value(const char* value, size_t size); - ::std::string* mutable_aggregate_value(); - ::std::string* release_aggregate_value(); - void set_allocated_aggregate_value(::std::string* aggregate_value); + std::string* mutable_aggregate_value(); + std::string* release_aggregate_value(); + void set_allocated_aggregate_value(std::string* aggregate_value); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_aggregate_value(); + std::string* unsafe_arena_release_aggregate_value(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_aggregate_value( - ::std::string* aggregate_value); + std::string* aggregate_value); // optional uint64 positive_int_value = 4; bool has_positive_int_value() const; void clear_positive_int_value(); - static const int kPositiveIntValueFieldNumber = 4; - ::google::protobuf::uint64 positive_int_value() const; - void set_positive_int_value(::google::protobuf::uint64 value); + ::PROTOBUF_NAMESPACE_ID::uint64 positive_int_value() const; + void set_positive_int_value(::PROTOBUF_NAMESPACE_ID::uint64 value); // optional int64 negative_int_value = 5; bool has_negative_int_value() const; void clear_negative_int_value(); - static const int kNegativeIntValueFieldNumber = 5; - ::google::protobuf::int64 negative_int_value() const; - void set_negative_int_value(::google::protobuf::int64 value); + ::PROTOBUF_NAMESPACE_ID::int64 negative_int_value() const; + void set_negative_int_value(::PROTOBUF_NAMESPACE_ID::int64 value); // optional double double_value = 6; bool has_double_value() const; void clear_double_value(); - static const int kDoubleValueFieldNumber = 6; double double_value() const; void set_double_value(double value); // @@protoc_insertion_point(class_scope:google.protobuf.UninterpretedOption) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption_NamePart > name_; - ::google::protobuf::internal::ArenaStringPtr identifier_value_; - ::google::protobuf::internal::ArenaStringPtr string_value_; - ::google::protobuf::internal::ArenaStringPtr aggregate_value_; - ::google::protobuf::uint64 positive_int_value_; - ::google::protobuf::int64 negative_int_value_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart > name_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr identifier_value_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr string_value_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr aggregate_value_; + ::PROTOBUF_NAMESPACE_ID::uint64 positive_int_value_; + ::PROTOBUF_NAMESPACE_ID::int64 negative_int_value_; double double_value_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT SourceCodeInfo_Location : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.SourceCodeInfo.Location) */ { +class PROTOBUF_EXPORT SourceCodeInfo_Location : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.SourceCodeInfo.Location) */ { public: SourceCodeInfo_Location(); virtual ~SourceCodeInfo_Location(); SourceCodeInfo_Location(const SourceCodeInfo_Location& from); - - inline SourceCodeInfo_Location& operator=(const SourceCodeInfo_Location& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 SourceCodeInfo_Location(SourceCodeInfo_Location&& from) noexcept : SourceCodeInfo_Location() { *this = ::std::move(from); } + inline SourceCodeInfo_Location& operator=(const SourceCodeInfo_Location& from) { + CopyFrom(from); + return *this; + } inline SourceCodeInfo_Location& operator=(SourceCodeInfo_Location&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -5125,22 +5704,28 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo_Location : public ::google::protobuf::Me } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const SourceCodeInfo_Location& default_instance(); @@ -5152,54 +5737,66 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo_Location : public ::google::protobuf::Me static constexpr int kIndexInFileMessages = 23; - void UnsafeArenaSwap(SourceCodeInfo_Location* other); - void Swap(SourceCodeInfo_Location* other); friend void swap(SourceCodeInfo_Location& a, SourceCodeInfo_Location& b) { a.Swap(&b); } + inline void Swap(SourceCodeInfo_Location* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(SourceCodeInfo_Location* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline SourceCodeInfo_Location* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - SourceCodeInfo_Location* New(::google::protobuf::Arena* arena) const final { + SourceCodeInfo_Location* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const SourceCodeInfo_Location& from); void MergeFrom(const SourceCodeInfo_Location& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(SourceCodeInfo_Location* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.SourceCodeInfo.Location"; + } protected: - explicit SourceCodeInfo_Location(::google::protobuf::Arena* arena); + explicit SourceCodeInfo_Location(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -5207,144 +5804,144 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo_Location : public ::google::protobuf::Me } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kPathFieldNumber = 1, + kSpanFieldNumber = 2, + kLeadingDetachedCommentsFieldNumber = 6, + kLeadingCommentsFieldNumber = 3, + kTrailingCommentsFieldNumber = 4, + }; // repeated int32 path = 1 [packed = true]; int path_size() const; void clear_path(); - static const int kPathFieldNumber = 1; - ::google::protobuf::int32 path(int index) const; - void set_path(int index, ::google::protobuf::int32 value); - void add_path(::google::protobuf::int32 value); - const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& + ::PROTOBUF_NAMESPACE_ID::int32 path(int index) const; + void set_path(int index, ::PROTOBUF_NAMESPACE_ID::int32 value); + void add_path(::PROTOBUF_NAMESPACE_ID::int32 value); + const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& path() const; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* mutable_path(); // repeated int32 span = 2 [packed = true]; int span_size() const; void clear_span(); - static const int kSpanFieldNumber = 2; - ::google::protobuf::int32 span(int index) const; - void set_span(int index, ::google::protobuf::int32 value); - void add_span(::google::protobuf::int32 value); - const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& + ::PROTOBUF_NAMESPACE_ID::int32 span(int index) const; + void set_span(int index, ::PROTOBUF_NAMESPACE_ID::int32 value); + void add_span(::PROTOBUF_NAMESPACE_ID::int32 value); + const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& span() const; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* mutable_span(); // repeated string leading_detached_comments = 6; int leading_detached_comments_size() const; void clear_leading_detached_comments(); - static const int kLeadingDetachedCommentsFieldNumber = 6; - const ::std::string& leading_detached_comments(int index) const; - ::std::string* mutable_leading_detached_comments(int index); - void set_leading_detached_comments(int index, const ::std::string& value); - #if LANG_CXX11 - void set_leading_detached_comments(int index, ::std::string&& value); - #endif + const std::string& leading_detached_comments(int index) const; + std::string* mutable_leading_detached_comments(int index); + void set_leading_detached_comments(int index, const std::string& value); + void set_leading_detached_comments(int index, std::string&& value); void set_leading_detached_comments(int index, const char* value); void set_leading_detached_comments(int index, const char* value, size_t size); - ::std::string* add_leading_detached_comments(); - void add_leading_detached_comments(const ::std::string& value); - #if LANG_CXX11 - void add_leading_detached_comments(::std::string&& value); - #endif + std::string* add_leading_detached_comments(); + void add_leading_detached_comments(const std::string& value); + void add_leading_detached_comments(std::string&& value); void add_leading_detached_comments(const char* value); void add_leading_detached_comments(const char* value, size_t size); - const ::google::protobuf::RepeatedPtrField<::std::string>& leading_detached_comments() const; - ::google::protobuf::RepeatedPtrField<::std::string>* mutable_leading_detached_comments(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& leading_detached_comments() const; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_leading_detached_comments(); // optional string leading_comments = 3; bool has_leading_comments() const; void clear_leading_comments(); - static const int kLeadingCommentsFieldNumber = 3; - const ::std::string& leading_comments() const; - void set_leading_comments(const ::std::string& value); - #if LANG_CXX11 - void set_leading_comments(::std::string&& value); - #endif + const std::string& leading_comments() const; + void set_leading_comments(const std::string& value); + void set_leading_comments(std::string&& value); void set_leading_comments(const char* value); void set_leading_comments(const char* value, size_t size); - ::std::string* mutable_leading_comments(); - ::std::string* release_leading_comments(); - void set_allocated_leading_comments(::std::string* leading_comments); + std::string* mutable_leading_comments(); + std::string* release_leading_comments(); + void set_allocated_leading_comments(std::string* leading_comments); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_leading_comments(); + std::string* unsafe_arena_release_leading_comments(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_leading_comments( - ::std::string* leading_comments); + std::string* leading_comments); // optional string trailing_comments = 4; bool has_trailing_comments() const; void clear_trailing_comments(); - static const int kTrailingCommentsFieldNumber = 4; - const ::std::string& trailing_comments() const; - void set_trailing_comments(const ::std::string& value); - #if LANG_CXX11 - void set_trailing_comments(::std::string&& value); - #endif + const std::string& trailing_comments() const; + void set_trailing_comments(const std::string& value); + void set_trailing_comments(std::string&& value); void set_trailing_comments(const char* value); void set_trailing_comments(const char* value, size_t size); - ::std::string* mutable_trailing_comments(); - ::std::string* release_trailing_comments(); - void set_allocated_trailing_comments(::std::string* trailing_comments); + std::string* mutable_trailing_comments(); + std::string* release_trailing_comments(); + void set_allocated_trailing_comments(std::string* trailing_comments); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_trailing_comments(); + std::string* unsafe_arena_release_trailing_comments(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_trailing_comments( - ::std::string* trailing_comments); + std::string* trailing_comments); // @@protoc_insertion_point(class_scope:google.protobuf.SourceCodeInfo.Location) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 > path_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 > path_; mutable std::atomic _path_cached_byte_size_; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 > span_; + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 > span_; mutable std::atomic _span_cached_byte_size_; - ::google::protobuf::RepeatedPtrField<::std::string> leading_detached_comments_; - ::google::protobuf::internal::ArenaStringPtr leading_comments_; - ::google::protobuf::internal::ArenaStringPtr trailing_comments_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField leading_detached_comments_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr leading_comments_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr trailing_comments_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.SourceCodeInfo) */ { +class PROTOBUF_EXPORT SourceCodeInfo : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.SourceCodeInfo) */ { public: SourceCodeInfo(); virtual ~SourceCodeInfo(); SourceCodeInfo(const SourceCodeInfo& from); - - inline SourceCodeInfo& operator=(const SourceCodeInfo& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 SourceCodeInfo(SourceCodeInfo&& from) noexcept : SourceCodeInfo() { *this = ::std::move(from); } + inline SourceCodeInfo& operator=(const SourceCodeInfo& from) { + CopyFrom(from); + return *this; + } inline SourceCodeInfo& operator=(SourceCodeInfo&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -5353,22 +5950,28 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message /* } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const SourceCodeInfo& default_instance(); @@ -5380,54 +5983,66 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message /* static constexpr int kIndexInFileMessages = 24; - void UnsafeArenaSwap(SourceCodeInfo* other); - void Swap(SourceCodeInfo* other); friend void swap(SourceCodeInfo& a, SourceCodeInfo& b) { a.Swap(&b); } + inline void Swap(SourceCodeInfo* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(SourceCodeInfo* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline SourceCodeInfo* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - SourceCodeInfo* New(::google::protobuf::Arena* arena) const final { + SourceCodeInfo* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const SourceCodeInfo& from); void MergeFrom(const SourceCodeInfo& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(SourceCodeInfo* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.SourceCodeInfo"; + } protected: - explicit SourceCodeInfo(::google::protobuf::Arena* arena); + explicit SourceCodeInfo(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -5435,7 +6050,14 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message /* } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- @@ -5443,50 +6065,51 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message /* // accessors ------------------------------------------------------- + enum : int { + kLocationFieldNumber = 1, + }; // repeated .google.protobuf.SourceCodeInfo.Location location = 1; int location_size() const; void clear_location(); - static const int kLocationFieldNumber = 1; - ::google::protobuf::SourceCodeInfo_Location* mutable_location(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::SourceCodeInfo_Location >* + PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location* mutable_location(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location >* mutable_location(); - const ::google::protobuf::SourceCodeInfo_Location& location(int index) const; - ::google::protobuf::SourceCodeInfo_Location* add_location(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::SourceCodeInfo_Location >& + const PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location& location(int index) const; + PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location* add_location(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location >& location() const; // @@protoc_insertion_point(class_scope:google.protobuf.SourceCodeInfo) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::SourceCodeInfo_Location > location_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location > location_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT GeneratedCodeInfo_Annotation : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.GeneratedCodeInfo.Annotation) */ { +class PROTOBUF_EXPORT GeneratedCodeInfo_Annotation : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.GeneratedCodeInfo.Annotation) */ { public: GeneratedCodeInfo_Annotation(); virtual ~GeneratedCodeInfo_Annotation(); GeneratedCodeInfo_Annotation(const GeneratedCodeInfo_Annotation& from); - - inline GeneratedCodeInfo_Annotation& operator=(const GeneratedCodeInfo_Annotation& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 GeneratedCodeInfo_Annotation(GeneratedCodeInfo_Annotation&& from) noexcept : GeneratedCodeInfo_Annotation() { *this = ::std::move(from); } + inline GeneratedCodeInfo_Annotation& operator=(const GeneratedCodeInfo_Annotation& from) { + CopyFrom(from); + return *this; + } inline GeneratedCodeInfo_Annotation& operator=(GeneratedCodeInfo_Annotation&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -5495,22 +6118,28 @@ class LIBPROTOBUF_EXPORT GeneratedCodeInfo_Annotation : public ::google::protobu } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const GeneratedCodeInfo_Annotation& default_instance(); @@ -5522,54 +6151,66 @@ class LIBPROTOBUF_EXPORT GeneratedCodeInfo_Annotation : public ::google::protobu static constexpr int kIndexInFileMessages = 25; - void UnsafeArenaSwap(GeneratedCodeInfo_Annotation* other); - void Swap(GeneratedCodeInfo_Annotation* other); friend void swap(GeneratedCodeInfo_Annotation& a, GeneratedCodeInfo_Annotation& b) { a.Swap(&b); } + inline void Swap(GeneratedCodeInfo_Annotation* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(GeneratedCodeInfo_Annotation* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline GeneratedCodeInfo_Annotation* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - GeneratedCodeInfo_Annotation* New(::google::protobuf::Arena* arena) const final { + GeneratedCodeInfo_Annotation* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const GeneratedCodeInfo_Annotation& from); void MergeFrom(const GeneratedCodeInfo_Annotation& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(GeneratedCodeInfo_Annotation* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.GeneratedCodeInfo.Annotation"; + } protected: - explicit GeneratedCodeInfo_Annotation(::google::protobuf::Arena* arena); + explicit GeneratedCodeInfo_Annotation(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -5577,98 +6218,104 @@ class LIBPROTOBUF_EXPORT GeneratedCodeInfo_Annotation : public ::google::protobu } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kPathFieldNumber = 1, + kSourceFileFieldNumber = 2, + kBeginFieldNumber = 3, + kEndFieldNumber = 4, + }; // repeated int32 path = 1 [packed = true]; int path_size() const; void clear_path(); - static const int kPathFieldNumber = 1; - ::google::protobuf::int32 path(int index) const; - void set_path(int index, ::google::protobuf::int32 value); - void add_path(::google::protobuf::int32 value); - const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& + ::PROTOBUF_NAMESPACE_ID::int32 path(int index) const; + void set_path(int index, ::PROTOBUF_NAMESPACE_ID::int32 value); + void add_path(::PROTOBUF_NAMESPACE_ID::int32 value); + const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& path() const; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* mutable_path(); // optional string source_file = 2; bool has_source_file() const; void clear_source_file(); - static const int kSourceFileFieldNumber = 2; - const ::std::string& source_file() const; - void set_source_file(const ::std::string& value); - #if LANG_CXX11 - void set_source_file(::std::string&& value); - #endif + const std::string& source_file() const; + void set_source_file(const std::string& value); + void set_source_file(std::string&& value); void set_source_file(const char* value); void set_source_file(const char* value, size_t size); - ::std::string* mutable_source_file(); - ::std::string* release_source_file(); - void set_allocated_source_file(::std::string* source_file); + std::string* mutable_source_file(); + std::string* release_source_file(); + void set_allocated_source_file(std::string* source_file); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_source_file(); + std::string* unsafe_arena_release_source_file(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_source_file( - ::std::string* source_file); + std::string* source_file); // optional int32 begin = 3; bool has_begin() const; void clear_begin(); - static const int kBeginFieldNumber = 3; - ::google::protobuf::int32 begin() const; - void set_begin(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 begin() const; + void set_begin(::PROTOBUF_NAMESPACE_ID::int32 value); // optional int32 end = 4; bool has_end() const; void clear_end(); - static const int kEndFieldNumber = 4; - ::google::protobuf::int32 end() const; - void set_end(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 end() const; + void set_end(::PROTOBUF_NAMESPACE_ID::int32 value); // @@protoc_insertion_point(class_scope:google.protobuf.GeneratedCodeInfo.Annotation) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedField< ::google::protobuf::int32 > path_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 > path_; mutable std::atomic _path_cached_byte_size_; - ::google::protobuf::internal::ArenaStringPtr source_file_; - ::google::protobuf::int32 begin_; - ::google::protobuf::int32 end_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr source_file_; + ::PROTOBUF_NAMESPACE_ID::int32 begin_; + ::PROTOBUF_NAMESPACE_ID::int32 end_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT GeneratedCodeInfo : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.GeneratedCodeInfo) */ { +class PROTOBUF_EXPORT GeneratedCodeInfo : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.GeneratedCodeInfo) */ { public: GeneratedCodeInfo(); virtual ~GeneratedCodeInfo(); GeneratedCodeInfo(const GeneratedCodeInfo& from); - - inline GeneratedCodeInfo& operator=(const GeneratedCodeInfo& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 GeneratedCodeInfo(GeneratedCodeInfo&& from) noexcept : GeneratedCodeInfo() { *this = ::std::move(from); } + inline GeneratedCodeInfo& operator=(const GeneratedCodeInfo& from) { + CopyFrom(from); + return *this; + } inline GeneratedCodeInfo& operator=(GeneratedCodeInfo&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -5677,22 +6324,28 @@ class LIBPROTOBUF_EXPORT GeneratedCodeInfo : public ::google::protobuf::Message } return *this; } - #endif - inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + + inline const ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + inline ::PROTOBUF_NAMESPACE_ID::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } - inline ::google::protobuf::Arena* GetArena() const final { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const GeneratedCodeInfo& default_instance(); @@ -5704,54 +6357,66 @@ class LIBPROTOBUF_EXPORT GeneratedCodeInfo : public ::google::protobuf::Message static constexpr int kIndexInFileMessages = 26; - void UnsafeArenaSwap(GeneratedCodeInfo* other); - void Swap(GeneratedCodeInfo* other); friend void swap(GeneratedCodeInfo& a, GeneratedCodeInfo& b) { a.Swap(&b); } + inline void Swap(GeneratedCodeInfo* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(GeneratedCodeInfo* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline GeneratedCodeInfo* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - GeneratedCodeInfo* New(::google::protobuf::Arena* arena) const final { + GeneratedCodeInfo* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const GeneratedCodeInfo& from); void MergeFrom(const GeneratedCodeInfo& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(GeneratedCodeInfo* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.GeneratedCodeInfo"; + } protected: - explicit GeneratedCodeInfo(::google::protobuf::Arena* arena); + explicit GeneratedCodeInfo(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -5759,7 +6424,14 @@ class LIBPROTOBUF_EXPORT GeneratedCodeInfo : public ::google::protobuf::Message } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto); + return ::descriptor_table_google_2fprotobuf_2fdescriptor_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- @@ -5767,29 +6439,31 @@ class LIBPROTOBUF_EXPORT GeneratedCodeInfo : public ::google::protobuf::Message // accessors ------------------------------------------------------- + enum : int { + kAnnotationFieldNumber = 1, + }; // repeated .google.protobuf.GeneratedCodeInfo.Annotation annotation = 1; int annotation_size() const; void clear_annotation(); - static const int kAnnotationFieldNumber = 1; - ::google::protobuf::GeneratedCodeInfo_Annotation* mutable_annotation(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::GeneratedCodeInfo_Annotation >* + PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation* mutable_annotation(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation >* mutable_annotation(); - const ::google::protobuf::GeneratedCodeInfo_Annotation& annotation(int index) const; - ::google::protobuf::GeneratedCodeInfo_Annotation* add_annotation(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::GeneratedCodeInfo_Annotation >& + const PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation& annotation(int index) const; + PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation* add_annotation(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation >& annotation() const; // @@protoc_insertion_point(class_scope:google.protobuf.GeneratedCodeInfo) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::HasBits<1> _has_bits_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::GeneratedCodeInfo_Annotation > annotation_; + ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation > annotation_; friend struct ::TableStruct_google_2fprotobuf_2fdescriptor_2eproto; }; // =================================================================== @@ -5810,24 +6484,24 @@ inline int FileDescriptorSet::file_size() const { inline void FileDescriptorSet::clear_file() { file_.Clear(); } -inline ::google::protobuf::FileDescriptorProto* FileDescriptorSet::mutable_file(int index) { +inline PROTOBUF_NAMESPACE_ID::FileDescriptorProto* FileDescriptorSet::mutable_file(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorSet.file) return file_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >* FileDescriptorSet::mutable_file() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorSet.file) return &file_; } -inline const ::google::protobuf::FileDescriptorProto& FileDescriptorSet::file(int index) const { +inline const PROTOBUF_NAMESPACE_ID::FileDescriptorProto& FileDescriptorSet::file(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorSet.file) return file_.Get(index); } -inline ::google::protobuf::FileDescriptorProto* FileDescriptorSet::add_file() { +inline PROTOBUF_NAMESPACE_ID::FileDescriptorProto* FileDescriptorSet::add_file() { // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorSet.file) return file_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FileDescriptorProto >& FileDescriptorSet::file() const { // @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorSet.file) return file_; @@ -5842,79 +6516,77 @@ inline bool FileDescriptorProto::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void FileDescriptorProto::clear_name() { - name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& FileDescriptorProto::name() const { +inline const std::string& FileDescriptorProto::name() const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.name) return name_.Get(); } -inline void FileDescriptorProto::set_name(const ::std::string& value) { +inline void FileDescriptorProto::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.name) } -#if LANG_CXX11 -inline void FileDescriptorProto::set_name(::std::string&& value) { +inline void FileDescriptorProto::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileDescriptorProto.name) } -#endif inline void FileDescriptorProto::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileDescriptorProto.name) } inline void FileDescriptorProto::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileDescriptorProto.name) } -inline ::std::string* FileDescriptorProto::mutable_name() { +inline std::string* FileDescriptorProto::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.name) - return name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileDescriptorProto::release_name() { +inline std::string* FileDescriptorProto::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.FileDescriptorProto.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileDescriptorProto::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void FileDescriptorProto::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name, + name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileDescriptorProto.name) } -inline ::std::string* FileDescriptorProto::unsafe_arena_release_name() { +inline std::string* FileDescriptorProto::unsafe_arena_release_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileDescriptorProto.name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileDescriptorProto::unsafe_arena_set_allocated_name( - ::std::string* name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name != NULL) { + std::string* name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileDescriptorProto.name) } @@ -5924,79 +6596,77 @@ inline bool FileDescriptorProto::has_package() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void FileDescriptorProto::clear_package() { - package_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + package_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000002u; } -inline const ::std::string& FileDescriptorProto::package() const { +inline const std::string& FileDescriptorProto::package() const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.package) return package_.Get(); } -inline void FileDescriptorProto::set_package(const ::std::string& value) { +inline void FileDescriptorProto::set_package(const std::string& value) { _has_bits_[0] |= 0x00000002u; - package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.package) } -#if LANG_CXX11 -inline void FileDescriptorProto::set_package(::std::string&& value) { +inline void FileDescriptorProto::set_package(std::string&& value) { _has_bits_[0] |= 0x00000002u; package_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileDescriptorProto.package) } -#endif inline void FileDescriptorProto::set_package(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000002u; - package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileDescriptorProto.package) } inline void FileDescriptorProto::set_package(const char* value, size_t size) { _has_bits_[0] |= 0x00000002u; - package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileDescriptorProto.package) } -inline ::std::string* FileDescriptorProto::mutable_package() { +inline std::string* FileDescriptorProto::mutable_package() { _has_bits_[0] |= 0x00000002u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.package) - return package_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return package_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileDescriptorProto::release_package() { +inline std::string* FileDescriptorProto::release_package() { // @@protoc_insertion_point(field_release:google.protobuf.FileDescriptorProto.package) if (!has_package()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000002u; - return package_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return package_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileDescriptorProto::set_allocated_package(::std::string* package) { - if (package != NULL) { +inline void FileDescriptorProto::set_allocated_package(std::string* package) { + if (package != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - package_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), package, + package_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), package, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileDescriptorProto.package) } -inline ::std::string* FileDescriptorProto::unsafe_arena_release_package() { +inline std::string* FileDescriptorProto::unsafe_arena_release_package() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileDescriptorProto.package) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000002u; - return package_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return package_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileDescriptorProto::unsafe_arena_set_allocated_package( - ::std::string* package) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (package != NULL) { + std::string* package) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (package != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - package_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + package_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), package, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileDescriptorProto.package) } @@ -6008,26 +6678,24 @@ inline int FileDescriptorProto::dependency_size() const { inline void FileDescriptorProto::clear_dependency() { dependency_.Clear(); } -inline const ::std::string& FileDescriptorProto::dependency(int index) const { +inline const std::string& FileDescriptorProto::dependency(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.dependency) return dependency_.Get(index); } -inline ::std::string* FileDescriptorProto::mutable_dependency(int index) { +inline std::string* FileDescriptorProto::mutable_dependency(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.dependency) return dependency_.Mutable(index); } -inline void FileDescriptorProto::set_dependency(int index, const ::std::string& value) { +inline void FileDescriptorProto::set_dependency(int index, const std::string& value) { // @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.dependency) dependency_.Mutable(index)->assign(value); } -#if LANG_CXX11 -inline void FileDescriptorProto::set_dependency(int index, ::std::string&& value) { +inline void FileDescriptorProto::set_dependency(int index, std::string&& value) { // @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.dependency) dependency_.Mutable(index)->assign(std::move(value)); } -#endif inline void FileDescriptorProto::set_dependency(int index, const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); dependency_.Mutable(index)->assign(value); // @@protoc_insertion_point(field_set_char:google.protobuf.FileDescriptorProto.dependency) } @@ -6036,22 +6704,20 @@ inline void FileDescriptorProto::set_dependency(int index, const char* value, si reinterpret_cast(value), size); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileDescriptorProto.dependency) } -inline ::std::string* FileDescriptorProto::add_dependency() { +inline std::string* FileDescriptorProto::add_dependency() { // @@protoc_insertion_point(field_add_mutable:google.protobuf.FileDescriptorProto.dependency) return dependency_.Add(); } -inline void FileDescriptorProto::add_dependency(const ::std::string& value) { +inline void FileDescriptorProto::add_dependency(const std::string& value) { dependency_.Add()->assign(value); // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.dependency) } -#if LANG_CXX11 -inline void FileDescriptorProto::add_dependency(::std::string&& value) { +inline void FileDescriptorProto::add_dependency(std::string&& value) { dependency_.Add(std::move(value)); // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.dependency) } -#endif inline void FileDescriptorProto::add_dependency(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); dependency_.Add()->assign(value); // @@protoc_insertion_point(field_add_char:google.protobuf.FileDescriptorProto.dependency) } @@ -6059,12 +6725,12 @@ inline void FileDescriptorProto::add_dependency(const char* value, size_t size) dependency_.Add()->assign(reinterpret_cast(value), size); // @@protoc_insertion_point(field_add_pointer:google.protobuf.FileDescriptorProto.dependency) } -inline const ::google::protobuf::RepeatedPtrField<::std::string>& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& FileDescriptorProto::dependency() const { // @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.dependency) return dependency_; } -inline ::google::protobuf::RepeatedPtrField<::std::string>* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* FileDescriptorProto::mutable_dependency() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.dependency) return &dependency_; @@ -6077,24 +6743,24 @@ inline int FileDescriptorProto::public_dependency_size() const { inline void FileDescriptorProto::clear_public_dependency() { public_dependency_.Clear(); } -inline ::google::protobuf::int32 FileDescriptorProto::public_dependency(int index) const { +inline ::PROTOBUF_NAMESPACE_ID::int32 FileDescriptorProto::public_dependency(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.public_dependency) return public_dependency_.Get(index); } -inline void FileDescriptorProto::set_public_dependency(int index, ::google::protobuf::int32 value) { +inline void FileDescriptorProto::set_public_dependency(int index, ::PROTOBUF_NAMESPACE_ID::int32 value) { public_dependency_.Set(index, value); // @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.public_dependency) } -inline void FileDescriptorProto::add_public_dependency(::google::protobuf::int32 value) { +inline void FileDescriptorProto::add_public_dependency(::PROTOBUF_NAMESPACE_ID::int32 value) { public_dependency_.Add(value); // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.public_dependency) } -inline const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& FileDescriptorProto::public_dependency() const { // @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.public_dependency) return public_dependency_; } -inline ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* FileDescriptorProto::mutable_public_dependency() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.public_dependency) return &public_dependency_; @@ -6107,24 +6773,24 @@ inline int FileDescriptorProto::weak_dependency_size() const { inline void FileDescriptorProto::clear_weak_dependency() { weak_dependency_.Clear(); } -inline ::google::protobuf::int32 FileDescriptorProto::weak_dependency(int index) const { +inline ::PROTOBUF_NAMESPACE_ID::int32 FileDescriptorProto::weak_dependency(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.weak_dependency) return weak_dependency_.Get(index); } -inline void FileDescriptorProto::set_weak_dependency(int index, ::google::protobuf::int32 value) { +inline void FileDescriptorProto::set_weak_dependency(int index, ::PROTOBUF_NAMESPACE_ID::int32 value) { weak_dependency_.Set(index, value); // @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.weak_dependency) } -inline void FileDescriptorProto::add_weak_dependency(::google::protobuf::int32 value) { +inline void FileDescriptorProto::add_weak_dependency(::PROTOBUF_NAMESPACE_ID::int32 value) { weak_dependency_.Add(value); // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.weak_dependency) } -inline const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& FileDescriptorProto::weak_dependency() const { // @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.weak_dependency) return weak_dependency_; } -inline ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* FileDescriptorProto::mutable_weak_dependency() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.weak_dependency) return &weak_dependency_; @@ -6137,24 +6803,24 @@ inline int FileDescriptorProto::message_type_size() const { inline void FileDescriptorProto::clear_message_type() { message_type_.Clear(); } -inline ::google::protobuf::DescriptorProto* FileDescriptorProto::mutable_message_type(int index) { +inline PROTOBUF_NAMESPACE_ID::DescriptorProto* FileDescriptorProto::mutable_message_type(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.message_type) return message_type_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto >* FileDescriptorProto::mutable_message_type() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.message_type) return &message_type_; } -inline const ::google::protobuf::DescriptorProto& FileDescriptorProto::message_type(int index) const { +inline const PROTOBUF_NAMESPACE_ID::DescriptorProto& FileDescriptorProto::message_type(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.message_type) return message_type_.Get(index); } -inline ::google::protobuf::DescriptorProto* FileDescriptorProto::add_message_type() { +inline PROTOBUF_NAMESPACE_ID::DescriptorProto* FileDescriptorProto::add_message_type() { // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.message_type) return message_type_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto >& FileDescriptorProto::message_type() const { // @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.message_type) return message_type_; @@ -6167,24 +6833,24 @@ inline int FileDescriptorProto::enum_type_size() const { inline void FileDescriptorProto::clear_enum_type() { enum_type_.Clear(); } -inline ::google::protobuf::EnumDescriptorProto* FileDescriptorProto::mutable_enum_type(int index) { +inline PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* FileDescriptorProto::mutable_enum_type(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.enum_type) return enum_type_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >* FileDescriptorProto::mutable_enum_type() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.enum_type) return &enum_type_; } -inline const ::google::protobuf::EnumDescriptorProto& FileDescriptorProto::enum_type(int index) const { +inline const PROTOBUF_NAMESPACE_ID::EnumDescriptorProto& FileDescriptorProto::enum_type(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.enum_type) return enum_type_.Get(index); } -inline ::google::protobuf::EnumDescriptorProto* FileDescriptorProto::add_enum_type() { +inline PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* FileDescriptorProto::add_enum_type() { // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.enum_type) return enum_type_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >& FileDescriptorProto::enum_type() const { // @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.enum_type) return enum_type_; @@ -6197,24 +6863,24 @@ inline int FileDescriptorProto::service_size() const { inline void FileDescriptorProto::clear_service() { service_.Clear(); } -inline ::google::protobuf::ServiceDescriptorProto* FileDescriptorProto::mutable_service(int index) { +inline PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto* FileDescriptorProto::mutable_service(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.service) return service_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::ServiceDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto >* FileDescriptorProto::mutable_service() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.service) return &service_; } -inline const ::google::protobuf::ServiceDescriptorProto& FileDescriptorProto::service(int index) const { +inline const PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto& FileDescriptorProto::service(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.service) return service_.Get(index); } -inline ::google::protobuf::ServiceDescriptorProto* FileDescriptorProto::add_service() { +inline PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto* FileDescriptorProto::add_service() { // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.service) return service_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::ServiceDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::ServiceDescriptorProto >& FileDescriptorProto::service() const { // @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.service) return service_; @@ -6227,24 +6893,24 @@ inline int FileDescriptorProto::extension_size() const { inline void FileDescriptorProto::clear_extension() { extension_.Clear(); } -inline ::google::protobuf::FieldDescriptorProto* FileDescriptorProto::mutable_extension(int index) { +inline PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* FileDescriptorProto::mutable_extension(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.extension) return extension_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >* FileDescriptorProto::mutable_extension() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileDescriptorProto.extension) return &extension_; } -inline const ::google::protobuf::FieldDescriptorProto& FileDescriptorProto::extension(int index) const { +inline const PROTOBUF_NAMESPACE_ID::FieldDescriptorProto& FileDescriptorProto::extension(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.extension) return extension_.Get(index); } -inline ::google::protobuf::FieldDescriptorProto* FileDescriptorProto::add_extension() { +inline PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* FileDescriptorProto::add_extension() { // @@protoc_insertion_point(field_add:google.protobuf.FileDescriptorProto.extension) return extension_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >& FileDescriptorProto::extension() const { // @@protoc_insertion_point(field_list:google.protobuf.FileDescriptorProto.extension) return extension_; @@ -6255,51 +6921,51 @@ inline bool FileDescriptorProto::has_options() const { return (_has_bits_[0] & 0x00000008u) != 0; } inline void FileDescriptorProto::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000008u; } -inline const ::google::protobuf::FileOptions& FileDescriptorProto::options() const { - const ::google::protobuf::FileOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::FileOptions& FileDescriptorProto::options() const { + const PROTOBUF_NAMESPACE_ID::FileOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_FileOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_FileOptions_default_instance_); } -inline ::google::protobuf::FileOptions* FileDescriptorProto::release_options() { +inline PROTOBUF_NAMESPACE_ID::FileOptions* FileDescriptorProto::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.FileDescriptorProto.options) _has_bits_[0] &= ~0x00000008u; - ::google::protobuf::FileOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::FileOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::FileOptions* FileDescriptorProto::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::FileOptions* FileDescriptorProto::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileDescriptorProto.options) _has_bits_[0] &= ~0x00000008u; - ::google::protobuf::FileOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::FileOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::FileOptions* FileDescriptorProto::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::FileOptions* FileDescriptorProto::mutable_options() { _has_bits_[0] |= 0x00000008u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::FileOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.options) return options_; } -inline void FileDescriptorProto::set_allocated_options(::google::protobuf::FileOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void FileDescriptorProto::set_allocated_options(PROTOBUF_NAMESPACE_ID::FileOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000008u; @@ -6315,51 +6981,51 @@ inline bool FileDescriptorProto::has_source_code_info() const { return (_has_bits_[0] & 0x00000010u) != 0; } inline void FileDescriptorProto::clear_source_code_info() { - if (source_code_info_ != NULL) source_code_info_->Clear(); + if (source_code_info_ != nullptr) source_code_info_->Clear(); _has_bits_[0] &= ~0x00000010u; } -inline const ::google::protobuf::SourceCodeInfo& FileDescriptorProto::source_code_info() const { - const ::google::protobuf::SourceCodeInfo* p = source_code_info_; +inline const PROTOBUF_NAMESPACE_ID::SourceCodeInfo& FileDescriptorProto::source_code_info() const { + const PROTOBUF_NAMESPACE_ID::SourceCodeInfo* p = source_code_info_; // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.source_code_info) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_SourceCodeInfo_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_SourceCodeInfo_default_instance_); } -inline ::google::protobuf::SourceCodeInfo* FileDescriptorProto::release_source_code_info() { +inline PROTOBUF_NAMESPACE_ID::SourceCodeInfo* FileDescriptorProto::release_source_code_info() { // @@protoc_insertion_point(field_release:google.protobuf.FileDescriptorProto.source_code_info) _has_bits_[0] &= ~0x00000010u; - ::google::protobuf::SourceCodeInfo* temp = source_code_info_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::SourceCodeInfo* temp = source_code_info_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - source_code_info_ = NULL; + source_code_info_ = nullptr; return temp; } -inline ::google::protobuf::SourceCodeInfo* FileDescriptorProto::unsafe_arena_release_source_code_info() { +inline PROTOBUF_NAMESPACE_ID::SourceCodeInfo* FileDescriptorProto::unsafe_arena_release_source_code_info() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileDescriptorProto.source_code_info) _has_bits_[0] &= ~0x00000010u; - ::google::protobuf::SourceCodeInfo* temp = source_code_info_; - source_code_info_ = NULL; + PROTOBUF_NAMESPACE_ID::SourceCodeInfo* temp = source_code_info_; + source_code_info_ = nullptr; return temp; } -inline ::google::protobuf::SourceCodeInfo* FileDescriptorProto::mutable_source_code_info() { +inline PROTOBUF_NAMESPACE_ID::SourceCodeInfo* FileDescriptorProto::mutable_source_code_info() { _has_bits_[0] |= 0x00000010u; - if (source_code_info_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::SourceCodeInfo>(GetArenaNoVirtual()); + if (source_code_info_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); source_code_info_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.source_code_info) return source_code_info_; } -inline void FileDescriptorProto::set_allocated_source_code_info(::google::protobuf::SourceCodeInfo* source_code_info) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void FileDescriptorProto::set_allocated_source_code_info(PROTOBUF_NAMESPACE_ID::SourceCodeInfo* source_code_info) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete source_code_info_; } if (source_code_info) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(source_code_info); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(source_code_info); if (message_arena != submessage_arena) { - source_code_info = ::google::protobuf::internal::GetOwnedMessage( + source_code_info = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, source_code_info, submessage_arena); } _has_bits_[0] |= 0x00000010u; @@ -6375,79 +7041,77 @@ inline bool FileDescriptorProto::has_syntax() const { return (_has_bits_[0] & 0x00000004u) != 0; } inline void FileDescriptorProto::clear_syntax() { - syntax_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + syntax_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000004u; } -inline const ::std::string& FileDescriptorProto::syntax() const { +inline const std::string& FileDescriptorProto::syntax() const { // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.syntax) return syntax_.Get(); } -inline void FileDescriptorProto::set_syntax(const ::std::string& value) { +inline void FileDescriptorProto::set_syntax(const std::string& value) { _has_bits_[0] |= 0x00000004u; - syntax_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + syntax_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.syntax) } -#if LANG_CXX11 -inline void FileDescriptorProto::set_syntax(::std::string&& value) { +inline void FileDescriptorProto::set_syntax(std::string&& value) { _has_bits_[0] |= 0x00000004u; syntax_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileDescriptorProto.syntax) } -#endif inline void FileDescriptorProto::set_syntax(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000004u; - syntax_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + syntax_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileDescriptorProto.syntax) } inline void FileDescriptorProto::set_syntax(const char* value, size_t size) { _has_bits_[0] |= 0x00000004u; - syntax_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + syntax_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileDescriptorProto.syntax) } -inline ::std::string* FileDescriptorProto::mutable_syntax() { +inline std::string* FileDescriptorProto::mutable_syntax() { _has_bits_[0] |= 0x00000004u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.syntax) - return syntax_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return syntax_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileDescriptorProto::release_syntax() { +inline std::string* FileDescriptorProto::release_syntax() { // @@protoc_insertion_point(field_release:google.protobuf.FileDescriptorProto.syntax) if (!has_syntax()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000004u; - return syntax_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return syntax_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileDescriptorProto::set_allocated_syntax(::std::string* syntax) { - if (syntax != NULL) { +inline void FileDescriptorProto::set_allocated_syntax(std::string* syntax) { + if (syntax != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - syntax_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), syntax, + syntax_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), syntax, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileDescriptorProto.syntax) } -inline ::std::string* FileDescriptorProto::unsafe_arena_release_syntax() { +inline std::string* FileDescriptorProto::unsafe_arena_release_syntax() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileDescriptorProto.syntax) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000004u; - return syntax_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return syntax_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileDescriptorProto::unsafe_arena_set_allocated_syntax( - ::std::string* syntax) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (syntax != NULL) { + std::string* syntax) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (syntax != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - syntax_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + syntax_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), syntax, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileDescriptorProto.syntax) } @@ -6464,11 +7128,11 @@ inline void DescriptorProto_ExtensionRange::clear_start() { start_ = 0; _has_bits_[0] &= ~0x00000002u; } -inline ::google::protobuf::int32 DescriptorProto_ExtensionRange::start() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 DescriptorProto_ExtensionRange::start() const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.ExtensionRange.start) return start_; } -inline void DescriptorProto_ExtensionRange::set_start(::google::protobuf::int32 value) { +inline void DescriptorProto_ExtensionRange::set_start(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000002u; start_ = value; // @@protoc_insertion_point(field_set:google.protobuf.DescriptorProto.ExtensionRange.start) @@ -6482,11 +7146,11 @@ inline void DescriptorProto_ExtensionRange::clear_end() { end_ = 0; _has_bits_[0] &= ~0x00000004u; } -inline ::google::protobuf::int32 DescriptorProto_ExtensionRange::end() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 DescriptorProto_ExtensionRange::end() const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.ExtensionRange.end) return end_; } -inline void DescriptorProto_ExtensionRange::set_end(::google::protobuf::int32 value) { +inline void DescriptorProto_ExtensionRange::set_end(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000004u; end_ = value; // @@protoc_insertion_point(field_set:google.protobuf.DescriptorProto.ExtensionRange.end) @@ -6497,51 +7161,51 @@ inline bool DescriptorProto_ExtensionRange::has_options() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void DescriptorProto_ExtensionRange::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000001u; } -inline const ::google::protobuf::ExtensionRangeOptions& DescriptorProto_ExtensionRange::options() const { - const ::google::protobuf::ExtensionRangeOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions& DescriptorProto_ExtensionRange::options() const { + const PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.ExtensionRange.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_ExtensionRangeOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_ExtensionRangeOptions_default_instance_); } -inline ::google::protobuf::ExtensionRangeOptions* DescriptorProto_ExtensionRange::release_options() { +inline PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* DescriptorProto_ExtensionRange::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.DescriptorProto.ExtensionRange.options) _has_bits_[0] &= ~0x00000001u; - ::google::protobuf::ExtensionRangeOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::ExtensionRangeOptions* DescriptorProto_ExtensionRange::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* DescriptorProto_ExtensionRange::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.DescriptorProto.ExtensionRange.options) _has_bits_[0] &= ~0x00000001u; - ::google::protobuf::ExtensionRangeOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::ExtensionRangeOptions* DescriptorProto_ExtensionRange::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* DescriptorProto_ExtensionRange::mutable_options() { _has_bits_[0] |= 0x00000001u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::ExtensionRangeOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.ExtensionRange.options) return options_; } -inline void DescriptorProto_ExtensionRange::set_allocated_options(::google::protobuf::ExtensionRangeOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void DescriptorProto_ExtensionRange::set_allocated_options(PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000001u; @@ -6564,11 +7228,11 @@ inline void DescriptorProto_ReservedRange::clear_start() { start_ = 0; _has_bits_[0] &= ~0x00000001u; } -inline ::google::protobuf::int32 DescriptorProto_ReservedRange::start() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 DescriptorProto_ReservedRange::start() const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.ReservedRange.start) return start_; } -inline void DescriptorProto_ReservedRange::set_start(::google::protobuf::int32 value) { +inline void DescriptorProto_ReservedRange::set_start(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000001u; start_ = value; // @@protoc_insertion_point(field_set:google.protobuf.DescriptorProto.ReservedRange.start) @@ -6582,11 +7246,11 @@ inline void DescriptorProto_ReservedRange::clear_end() { end_ = 0; _has_bits_[0] &= ~0x00000002u; } -inline ::google::protobuf::int32 DescriptorProto_ReservedRange::end() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 DescriptorProto_ReservedRange::end() const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.ReservedRange.end) return end_; } -inline void DescriptorProto_ReservedRange::set_end(::google::protobuf::int32 value) { +inline void DescriptorProto_ReservedRange::set_end(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000002u; end_ = value; // @@protoc_insertion_point(field_set:google.protobuf.DescriptorProto.ReservedRange.end) @@ -6601,79 +7265,77 @@ inline bool DescriptorProto::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void DescriptorProto::clear_name() { - name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& DescriptorProto::name() const { +inline const std::string& DescriptorProto::name() const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.name) return name_.Get(); } -inline void DescriptorProto::set_name(const ::std::string& value) { +inline void DescriptorProto::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.DescriptorProto.name) } -#if LANG_CXX11 -inline void DescriptorProto::set_name(::std::string&& value) { +inline void DescriptorProto::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.DescriptorProto.name) } -#endif inline void DescriptorProto::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.DescriptorProto.name) } inline void DescriptorProto::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.DescriptorProto.name) } -inline ::std::string* DescriptorProto::mutable_name() { +inline std::string* DescriptorProto::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.name) - return name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* DescriptorProto::release_name() { +inline std::string* DescriptorProto::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.DescriptorProto.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void DescriptorProto::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void DescriptorProto::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name, + name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.DescriptorProto.name) } -inline ::std::string* DescriptorProto::unsafe_arena_release_name() { +inline std::string* DescriptorProto::unsafe_arena_release_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.DescriptorProto.name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void DescriptorProto::unsafe_arena_set_allocated_name( - ::std::string* name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name != NULL) { + std::string* name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.DescriptorProto.name) } @@ -6685,24 +7347,24 @@ inline int DescriptorProto::field_size() const { inline void DescriptorProto::clear_field() { field_.Clear(); } -inline ::google::protobuf::FieldDescriptorProto* DescriptorProto::mutable_field(int index) { +inline PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* DescriptorProto::mutable_field(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.field) return field_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >* DescriptorProto::mutable_field() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.DescriptorProto.field) return &field_; } -inline const ::google::protobuf::FieldDescriptorProto& DescriptorProto::field(int index) const { +inline const PROTOBUF_NAMESPACE_ID::FieldDescriptorProto& DescriptorProto::field(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.field) return field_.Get(index); } -inline ::google::protobuf::FieldDescriptorProto* DescriptorProto::add_field() { +inline PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* DescriptorProto::add_field() { // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.field) return field_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >& DescriptorProto::field() const { // @@protoc_insertion_point(field_list:google.protobuf.DescriptorProto.field) return field_; @@ -6715,24 +7377,24 @@ inline int DescriptorProto::extension_size() const { inline void DescriptorProto::clear_extension() { extension_.Clear(); } -inline ::google::protobuf::FieldDescriptorProto* DescriptorProto::mutable_extension(int index) { +inline PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* DescriptorProto::mutable_extension(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.extension) return extension_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >* DescriptorProto::mutable_extension() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.DescriptorProto.extension) return &extension_; } -inline const ::google::protobuf::FieldDescriptorProto& DescriptorProto::extension(int index) const { +inline const PROTOBUF_NAMESPACE_ID::FieldDescriptorProto& DescriptorProto::extension(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.extension) return extension_.Get(index); } -inline ::google::protobuf::FieldDescriptorProto* DescriptorProto::add_extension() { +inline PROTOBUF_NAMESPACE_ID::FieldDescriptorProto* DescriptorProto::add_extension() { // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.extension) return extension_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto >& DescriptorProto::extension() const { // @@protoc_insertion_point(field_list:google.protobuf.DescriptorProto.extension) return extension_; @@ -6745,24 +7407,24 @@ inline int DescriptorProto::nested_type_size() const { inline void DescriptorProto::clear_nested_type() { nested_type_.Clear(); } -inline ::google::protobuf::DescriptorProto* DescriptorProto::mutable_nested_type(int index) { +inline PROTOBUF_NAMESPACE_ID::DescriptorProto* DescriptorProto::mutable_nested_type(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.nested_type) return nested_type_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto >* DescriptorProto::mutable_nested_type() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.DescriptorProto.nested_type) return &nested_type_; } -inline const ::google::protobuf::DescriptorProto& DescriptorProto::nested_type(int index) const { +inline const PROTOBUF_NAMESPACE_ID::DescriptorProto& DescriptorProto::nested_type(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.nested_type) return nested_type_.Get(index); } -inline ::google::protobuf::DescriptorProto* DescriptorProto::add_nested_type() { +inline PROTOBUF_NAMESPACE_ID::DescriptorProto* DescriptorProto::add_nested_type() { // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.nested_type) return nested_type_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto >& DescriptorProto::nested_type() const { // @@protoc_insertion_point(field_list:google.protobuf.DescriptorProto.nested_type) return nested_type_; @@ -6775,24 +7437,24 @@ inline int DescriptorProto::enum_type_size() const { inline void DescriptorProto::clear_enum_type() { enum_type_.Clear(); } -inline ::google::protobuf::EnumDescriptorProto* DescriptorProto::mutable_enum_type(int index) { +inline PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* DescriptorProto::mutable_enum_type(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.enum_type) return enum_type_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >* DescriptorProto::mutable_enum_type() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.DescriptorProto.enum_type) return &enum_type_; } -inline const ::google::protobuf::EnumDescriptorProto& DescriptorProto::enum_type(int index) const { +inline const PROTOBUF_NAMESPACE_ID::EnumDescriptorProto& DescriptorProto::enum_type(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.enum_type) return enum_type_.Get(index); } -inline ::google::protobuf::EnumDescriptorProto* DescriptorProto::add_enum_type() { +inline PROTOBUF_NAMESPACE_ID::EnumDescriptorProto* DescriptorProto::add_enum_type() { // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.enum_type) return enum_type_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto >& DescriptorProto::enum_type() const { // @@protoc_insertion_point(field_list:google.protobuf.DescriptorProto.enum_type) return enum_type_; @@ -6805,24 +7467,24 @@ inline int DescriptorProto::extension_range_size() const { inline void DescriptorProto::clear_extension_range() { extension_range_.Clear(); } -inline ::google::protobuf::DescriptorProto_ExtensionRange* DescriptorProto::mutable_extension_range(int index) { +inline PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange* DescriptorProto::mutable_extension_range(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.extension_range) return extension_range_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ExtensionRange >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange >* DescriptorProto::mutable_extension_range() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.DescriptorProto.extension_range) return &extension_range_; } -inline const ::google::protobuf::DescriptorProto_ExtensionRange& DescriptorProto::extension_range(int index) const { +inline const PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange& DescriptorProto::extension_range(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.extension_range) return extension_range_.Get(index); } -inline ::google::protobuf::DescriptorProto_ExtensionRange* DescriptorProto::add_extension_range() { +inline PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange* DescriptorProto::add_extension_range() { // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.extension_range) return extension_range_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ExtensionRange >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ExtensionRange >& DescriptorProto::extension_range() const { // @@protoc_insertion_point(field_list:google.protobuf.DescriptorProto.extension_range) return extension_range_; @@ -6835,24 +7497,24 @@ inline int DescriptorProto::oneof_decl_size() const { inline void DescriptorProto::clear_oneof_decl() { oneof_decl_.Clear(); } -inline ::google::protobuf::OneofDescriptorProto* DescriptorProto::mutable_oneof_decl(int index) { +inline PROTOBUF_NAMESPACE_ID::OneofDescriptorProto* DescriptorProto::mutable_oneof_decl(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.oneof_decl) return oneof_decl_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::OneofDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::OneofDescriptorProto >* DescriptorProto::mutable_oneof_decl() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.DescriptorProto.oneof_decl) return &oneof_decl_; } -inline const ::google::protobuf::OneofDescriptorProto& DescriptorProto::oneof_decl(int index) const { +inline const PROTOBUF_NAMESPACE_ID::OneofDescriptorProto& DescriptorProto::oneof_decl(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.oneof_decl) return oneof_decl_.Get(index); } -inline ::google::protobuf::OneofDescriptorProto* DescriptorProto::add_oneof_decl() { +inline PROTOBUF_NAMESPACE_ID::OneofDescriptorProto* DescriptorProto::add_oneof_decl() { // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.oneof_decl) return oneof_decl_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::OneofDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::OneofDescriptorProto >& DescriptorProto::oneof_decl() const { // @@protoc_insertion_point(field_list:google.protobuf.DescriptorProto.oneof_decl) return oneof_decl_; @@ -6863,51 +7525,51 @@ inline bool DescriptorProto::has_options() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void DescriptorProto::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000002u; } -inline const ::google::protobuf::MessageOptions& DescriptorProto::options() const { - const ::google::protobuf::MessageOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::MessageOptions& DescriptorProto::options() const { + const PROTOBUF_NAMESPACE_ID::MessageOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_MessageOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_MessageOptions_default_instance_); } -inline ::google::protobuf::MessageOptions* DescriptorProto::release_options() { +inline PROTOBUF_NAMESPACE_ID::MessageOptions* DescriptorProto::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.DescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::MessageOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::MessageOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::MessageOptions* DescriptorProto::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::MessageOptions* DescriptorProto::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.DescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::MessageOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::MessageOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::MessageOptions* DescriptorProto::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::MessageOptions* DescriptorProto::mutable_options() { _has_bits_[0] |= 0x00000002u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::MessageOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.options) return options_; } -inline void DescriptorProto::set_allocated_options(::google::protobuf::MessageOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void DescriptorProto::set_allocated_options(PROTOBUF_NAMESPACE_ID::MessageOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000002u; @@ -6925,24 +7587,24 @@ inline int DescriptorProto::reserved_range_size() const { inline void DescriptorProto::clear_reserved_range() { reserved_range_.Clear(); } -inline ::google::protobuf::DescriptorProto_ReservedRange* DescriptorProto::mutable_reserved_range(int index) { +inline PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange* DescriptorProto::mutable_reserved_range(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.reserved_range) return reserved_range_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ReservedRange >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange >* DescriptorProto::mutable_reserved_range() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.DescriptorProto.reserved_range) return &reserved_range_; } -inline const ::google::protobuf::DescriptorProto_ReservedRange& DescriptorProto::reserved_range(int index) const { +inline const PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange& DescriptorProto::reserved_range(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.reserved_range) return reserved_range_.Get(index); } -inline ::google::protobuf::DescriptorProto_ReservedRange* DescriptorProto::add_reserved_range() { +inline PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange* DescriptorProto::add_reserved_range() { // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.reserved_range) return reserved_range_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto_ReservedRange >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::DescriptorProto_ReservedRange >& DescriptorProto::reserved_range() const { // @@protoc_insertion_point(field_list:google.protobuf.DescriptorProto.reserved_range) return reserved_range_; @@ -6955,26 +7617,24 @@ inline int DescriptorProto::reserved_name_size() const { inline void DescriptorProto::clear_reserved_name() { reserved_name_.Clear(); } -inline const ::std::string& DescriptorProto::reserved_name(int index) const { +inline const std::string& DescriptorProto::reserved_name(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.reserved_name) return reserved_name_.Get(index); } -inline ::std::string* DescriptorProto::mutable_reserved_name(int index) { +inline std::string* DescriptorProto::mutable_reserved_name(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.reserved_name) return reserved_name_.Mutable(index); } -inline void DescriptorProto::set_reserved_name(int index, const ::std::string& value) { +inline void DescriptorProto::set_reserved_name(int index, const std::string& value) { // @@protoc_insertion_point(field_set:google.protobuf.DescriptorProto.reserved_name) reserved_name_.Mutable(index)->assign(value); } -#if LANG_CXX11 -inline void DescriptorProto::set_reserved_name(int index, ::std::string&& value) { +inline void DescriptorProto::set_reserved_name(int index, std::string&& value) { // @@protoc_insertion_point(field_set:google.protobuf.DescriptorProto.reserved_name) reserved_name_.Mutable(index)->assign(std::move(value)); } -#endif inline void DescriptorProto::set_reserved_name(int index, const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); reserved_name_.Mutable(index)->assign(value); // @@protoc_insertion_point(field_set_char:google.protobuf.DescriptorProto.reserved_name) } @@ -6983,22 +7643,20 @@ inline void DescriptorProto::set_reserved_name(int index, const char* value, siz reinterpret_cast(value), size); // @@protoc_insertion_point(field_set_pointer:google.protobuf.DescriptorProto.reserved_name) } -inline ::std::string* DescriptorProto::add_reserved_name() { +inline std::string* DescriptorProto::add_reserved_name() { // @@protoc_insertion_point(field_add_mutable:google.protobuf.DescriptorProto.reserved_name) return reserved_name_.Add(); } -inline void DescriptorProto::add_reserved_name(const ::std::string& value) { +inline void DescriptorProto::add_reserved_name(const std::string& value) { reserved_name_.Add()->assign(value); // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.reserved_name) } -#if LANG_CXX11 -inline void DescriptorProto::add_reserved_name(::std::string&& value) { +inline void DescriptorProto::add_reserved_name(std::string&& value) { reserved_name_.Add(std::move(value)); // @@protoc_insertion_point(field_add:google.protobuf.DescriptorProto.reserved_name) } -#endif inline void DescriptorProto::add_reserved_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); reserved_name_.Add()->assign(value); // @@protoc_insertion_point(field_add_char:google.protobuf.DescriptorProto.reserved_name) } @@ -7006,12 +7664,12 @@ inline void DescriptorProto::add_reserved_name(const char* value, size_t size) { reserved_name_.Add()->assign(reinterpret_cast(value), size); // @@protoc_insertion_point(field_add_pointer:google.protobuf.DescriptorProto.reserved_name) } -inline const ::google::protobuf::RepeatedPtrField<::std::string>& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& DescriptorProto::reserved_name() const { // @@protoc_insertion_point(field_list:google.protobuf.DescriptorProto.reserved_name) return reserved_name_; } -inline ::google::protobuf::RepeatedPtrField<::std::string>* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* DescriptorProto::mutable_reserved_name() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.DescriptorProto.reserved_name) return &reserved_name_; @@ -7028,24 +7686,24 @@ inline int ExtensionRangeOptions::uninterpreted_option_size() const { inline void ExtensionRangeOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* ExtensionRangeOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* ExtensionRangeOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.ExtensionRangeOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* ExtensionRangeOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.ExtensionRangeOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& ExtensionRangeOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& ExtensionRangeOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.ExtensionRangeOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* ExtensionRangeOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* ExtensionRangeOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.ExtensionRangeOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& ExtensionRangeOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.ExtensionRangeOptions.uninterpreted_option) return uninterpreted_option_; @@ -7060,79 +7718,77 @@ inline bool FieldDescriptorProto::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void FieldDescriptorProto::clear_name() { - name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& FieldDescriptorProto::name() const { +inline const std::string& FieldDescriptorProto::name() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.name) return name_.Get(); } -inline void FieldDescriptorProto::set_name(const ::std::string& value) { +inline void FieldDescriptorProto::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.name) } -#if LANG_CXX11 -inline void FieldDescriptorProto::set_name(::std::string&& value) { +inline void FieldDescriptorProto::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FieldDescriptorProto.name) } -#endif inline void FieldDescriptorProto::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.name) } inline void FieldDescriptorProto::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.name) } -inline ::std::string* FieldDescriptorProto::mutable_name() { +inline std::string* FieldDescriptorProto::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.name) - return name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FieldDescriptorProto::release_name() { +inline std::string* FieldDescriptorProto::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.FieldDescriptorProto.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FieldDescriptorProto::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void FieldDescriptorProto::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name, + name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.name) } -inline ::std::string* FieldDescriptorProto::unsafe_arena_release_name() { +inline std::string* FieldDescriptorProto::unsafe_arena_release_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FieldDescriptorProto.name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FieldDescriptorProto::unsafe_arena_set_allocated_name( - ::std::string* name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name != NULL) { + std::string* name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FieldDescriptorProto.name) } @@ -7145,11 +7801,11 @@ inline void FieldDescriptorProto::clear_number() { number_ = 0; _has_bits_[0] &= ~0x00000040u; } -inline ::google::protobuf::int32 FieldDescriptorProto::number() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 FieldDescriptorProto::number() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.number) return number_; } -inline void FieldDescriptorProto::set_number(::google::protobuf::int32 value) { +inline void FieldDescriptorProto::set_number(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000040u; number_ = value; // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.number) @@ -7163,12 +7819,12 @@ inline void FieldDescriptorProto::clear_label() { label_ = 1; _has_bits_[0] &= ~0x00000100u; } -inline ::google::protobuf::FieldDescriptorProto_Label FieldDescriptorProto::label() const { +inline PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label FieldDescriptorProto::label() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.label) - return static_cast< ::google::protobuf::FieldDescriptorProto_Label >(label_); + return static_cast< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label >(label_); } -inline void FieldDescriptorProto::set_label(::google::protobuf::FieldDescriptorProto_Label value) { - assert(::google::protobuf::FieldDescriptorProto_Label_IsValid(value)); +inline void FieldDescriptorProto::set_label(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label value) { + assert(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label_IsValid(value)); _has_bits_[0] |= 0x00000100u; label_ = value; // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.label) @@ -7182,12 +7838,12 @@ inline void FieldDescriptorProto::clear_type() { type_ = 1; _has_bits_[0] &= ~0x00000200u; } -inline ::google::protobuf::FieldDescriptorProto_Type FieldDescriptorProto::type() const { +inline PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type FieldDescriptorProto::type() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.type) - return static_cast< ::google::protobuf::FieldDescriptorProto_Type >(type_); + return static_cast< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type >(type_); } -inline void FieldDescriptorProto::set_type(::google::protobuf::FieldDescriptorProto_Type value) { - assert(::google::protobuf::FieldDescriptorProto_Type_IsValid(value)); +inline void FieldDescriptorProto::set_type(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type value) { + assert(PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type_IsValid(value)); _has_bits_[0] |= 0x00000200u; type_ = value; // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.type) @@ -7198,79 +7854,77 @@ inline bool FieldDescriptorProto::has_type_name() const { return (_has_bits_[0] & 0x00000004u) != 0; } inline void FieldDescriptorProto::clear_type_name() { - type_name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + type_name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000004u; } -inline const ::std::string& FieldDescriptorProto::type_name() const { +inline const std::string& FieldDescriptorProto::type_name() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.type_name) return type_name_.Get(); } -inline void FieldDescriptorProto::set_type_name(const ::std::string& value) { +inline void FieldDescriptorProto::set_type_name(const std::string& value) { _has_bits_[0] |= 0x00000004u; - type_name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + type_name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.type_name) } -#if LANG_CXX11 -inline void FieldDescriptorProto::set_type_name(::std::string&& value) { +inline void FieldDescriptorProto::set_type_name(std::string&& value) { _has_bits_[0] |= 0x00000004u; type_name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FieldDescriptorProto.type_name) } -#endif inline void FieldDescriptorProto::set_type_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000004u; - type_name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + type_name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.type_name) } inline void FieldDescriptorProto::set_type_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000004u; - type_name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + type_name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.type_name) } -inline ::std::string* FieldDescriptorProto::mutable_type_name() { +inline std::string* FieldDescriptorProto::mutable_type_name() { _has_bits_[0] |= 0x00000004u; // @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.type_name) - return type_name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return type_name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FieldDescriptorProto::release_type_name() { +inline std::string* FieldDescriptorProto::release_type_name() { // @@protoc_insertion_point(field_release:google.protobuf.FieldDescriptorProto.type_name) if (!has_type_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000004u; - return type_name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return type_name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FieldDescriptorProto::set_allocated_type_name(::std::string* type_name) { - if (type_name != NULL) { +inline void FieldDescriptorProto::set_allocated_type_name(std::string* type_name) { + if (type_name != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - type_name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), type_name, + type_name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), type_name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.type_name) } -inline ::std::string* FieldDescriptorProto::unsafe_arena_release_type_name() { +inline std::string* FieldDescriptorProto::unsafe_arena_release_type_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FieldDescriptorProto.type_name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000004u; - return type_name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return type_name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FieldDescriptorProto::unsafe_arena_set_allocated_type_name( - ::std::string* type_name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (type_name != NULL) { + std::string* type_name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (type_name != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - type_name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + type_name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), type_name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FieldDescriptorProto.type_name) } @@ -7280,79 +7934,77 @@ inline bool FieldDescriptorProto::has_extendee() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void FieldDescriptorProto::clear_extendee() { - extendee_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + extendee_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000002u; } -inline const ::std::string& FieldDescriptorProto::extendee() const { +inline const std::string& FieldDescriptorProto::extendee() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.extendee) return extendee_.Get(); } -inline void FieldDescriptorProto::set_extendee(const ::std::string& value) { +inline void FieldDescriptorProto::set_extendee(const std::string& value) { _has_bits_[0] |= 0x00000002u; - extendee_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + extendee_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.extendee) } -#if LANG_CXX11 -inline void FieldDescriptorProto::set_extendee(::std::string&& value) { +inline void FieldDescriptorProto::set_extendee(std::string&& value) { _has_bits_[0] |= 0x00000002u; extendee_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FieldDescriptorProto.extendee) } -#endif inline void FieldDescriptorProto::set_extendee(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000002u; - extendee_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + extendee_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.extendee) } inline void FieldDescriptorProto::set_extendee(const char* value, size_t size) { _has_bits_[0] |= 0x00000002u; - extendee_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + extendee_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.extendee) } -inline ::std::string* FieldDescriptorProto::mutable_extendee() { +inline std::string* FieldDescriptorProto::mutable_extendee() { _has_bits_[0] |= 0x00000002u; // @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.extendee) - return extendee_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return extendee_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FieldDescriptorProto::release_extendee() { +inline std::string* FieldDescriptorProto::release_extendee() { // @@protoc_insertion_point(field_release:google.protobuf.FieldDescriptorProto.extendee) if (!has_extendee()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000002u; - return extendee_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return extendee_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FieldDescriptorProto::set_allocated_extendee(::std::string* extendee) { - if (extendee != NULL) { +inline void FieldDescriptorProto::set_allocated_extendee(std::string* extendee) { + if (extendee != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - extendee_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), extendee, + extendee_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), extendee, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.extendee) } -inline ::std::string* FieldDescriptorProto::unsafe_arena_release_extendee() { +inline std::string* FieldDescriptorProto::unsafe_arena_release_extendee() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FieldDescriptorProto.extendee) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000002u; - return extendee_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return extendee_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FieldDescriptorProto::unsafe_arena_set_allocated_extendee( - ::std::string* extendee) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (extendee != NULL) { + std::string* extendee) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (extendee != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - extendee_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + extendee_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), extendee, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FieldDescriptorProto.extendee) } @@ -7362,79 +8014,77 @@ inline bool FieldDescriptorProto::has_default_value() const { return (_has_bits_[0] & 0x00000008u) != 0; } inline void FieldDescriptorProto::clear_default_value() { - default_value_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + default_value_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000008u; } -inline const ::std::string& FieldDescriptorProto::default_value() const { +inline const std::string& FieldDescriptorProto::default_value() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.default_value) return default_value_.Get(); } -inline void FieldDescriptorProto::set_default_value(const ::std::string& value) { +inline void FieldDescriptorProto::set_default_value(const std::string& value) { _has_bits_[0] |= 0x00000008u; - default_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + default_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.default_value) } -#if LANG_CXX11 -inline void FieldDescriptorProto::set_default_value(::std::string&& value) { +inline void FieldDescriptorProto::set_default_value(std::string&& value) { _has_bits_[0] |= 0x00000008u; default_value_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FieldDescriptorProto.default_value) } -#endif inline void FieldDescriptorProto::set_default_value(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000008u; - default_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + default_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.default_value) } inline void FieldDescriptorProto::set_default_value(const char* value, size_t size) { _has_bits_[0] |= 0x00000008u; - default_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + default_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.default_value) } -inline ::std::string* FieldDescriptorProto::mutable_default_value() { +inline std::string* FieldDescriptorProto::mutable_default_value() { _has_bits_[0] |= 0x00000008u; // @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.default_value) - return default_value_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return default_value_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FieldDescriptorProto::release_default_value() { +inline std::string* FieldDescriptorProto::release_default_value() { // @@protoc_insertion_point(field_release:google.protobuf.FieldDescriptorProto.default_value) if (!has_default_value()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000008u; - return default_value_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return default_value_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FieldDescriptorProto::set_allocated_default_value(::std::string* default_value) { - if (default_value != NULL) { +inline void FieldDescriptorProto::set_allocated_default_value(std::string* default_value) { + if (default_value != nullptr) { _has_bits_[0] |= 0x00000008u; } else { _has_bits_[0] &= ~0x00000008u; } - default_value_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), default_value, + default_value_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), default_value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.default_value) } -inline ::std::string* FieldDescriptorProto::unsafe_arena_release_default_value() { +inline std::string* FieldDescriptorProto::unsafe_arena_release_default_value() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FieldDescriptorProto.default_value) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000008u; - return default_value_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return default_value_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FieldDescriptorProto::unsafe_arena_set_allocated_default_value( - ::std::string* default_value) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (default_value != NULL) { + std::string* default_value) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (default_value != nullptr) { _has_bits_[0] |= 0x00000008u; } else { _has_bits_[0] &= ~0x00000008u; } - default_value_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + default_value_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), default_value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FieldDescriptorProto.default_value) } @@ -7447,11 +8097,11 @@ inline void FieldDescriptorProto::clear_oneof_index() { oneof_index_ = 0; _has_bits_[0] &= ~0x00000080u; } -inline ::google::protobuf::int32 FieldDescriptorProto::oneof_index() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 FieldDescriptorProto::oneof_index() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.oneof_index) return oneof_index_; } -inline void FieldDescriptorProto::set_oneof_index(::google::protobuf::int32 value) { +inline void FieldDescriptorProto::set_oneof_index(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000080u; oneof_index_ = value; // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.oneof_index) @@ -7462,79 +8112,77 @@ inline bool FieldDescriptorProto::has_json_name() const { return (_has_bits_[0] & 0x00000010u) != 0; } inline void FieldDescriptorProto::clear_json_name() { - json_name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + json_name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000010u; } -inline const ::std::string& FieldDescriptorProto::json_name() const { +inline const std::string& FieldDescriptorProto::json_name() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.json_name) return json_name_.Get(); } -inline void FieldDescriptorProto::set_json_name(const ::std::string& value) { +inline void FieldDescriptorProto::set_json_name(const std::string& value) { _has_bits_[0] |= 0x00000010u; - json_name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + json_name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.json_name) } -#if LANG_CXX11 -inline void FieldDescriptorProto::set_json_name(::std::string&& value) { +inline void FieldDescriptorProto::set_json_name(std::string&& value) { _has_bits_[0] |= 0x00000010u; json_name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FieldDescriptorProto.json_name) } -#endif inline void FieldDescriptorProto::set_json_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000010u; - json_name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + json_name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.json_name) } inline void FieldDescriptorProto::set_json_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000010u; - json_name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + json_name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.json_name) } -inline ::std::string* FieldDescriptorProto::mutable_json_name() { +inline std::string* FieldDescriptorProto::mutable_json_name() { _has_bits_[0] |= 0x00000010u; // @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.json_name) - return json_name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return json_name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FieldDescriptorProto::release_json_name() { +inline std::string* FieldDescriptorProto::release_json_name() { // @@protoc_insertion_point(field_release:google.protobuf.FieldDescriptorProto.json_name) if (!has_json_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000010u; - return json_name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return json_name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FieldDescriptorProto::set_allocated_json_name(::std::string* json_name) { - if (json_name != NULL) { +inline void FieldDescriptorProto::set_allocated_json_name(std::string* json_name) { + if (json_name != nullptr) { _has_bits_[0] |= 0x00000010u; } else { _has_bits_[0] &= ~0x00000010u; } - json_name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), json_name, + json_name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), json_name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.json_name) } -inline ::std::string* FieldDescriptorProto::unsafe_arena_release_json_name() { +inline std::string* FieldDescriptorProto::unsafe_arena_release_json_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FieldDescriptorProto.json_name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000010u; - return json_name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return json_name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FieldDescriptorProto::unsafe_arena_set_allocated_json_name( - ::std::string* json_name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (json_name != NULL) { + std::string* json_name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (json_name != nullptr) { _has_bits_[0] |= 0x00000010u; } else { _has_bits_[0] &= ~0x00000010u; } - json_name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + json_name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), json_name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FieldDescriptorProto.json_name) } @@ -7544,51 +8192,51 @@ inline bool FieldDescriptorProto::has_options() const { return (_has_bits_[0] & 0x00000020u) != 0; } inline void FieldDescriptorProto::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000020u; } -inline const ::google::protobuf::FieldOptions& FieldDescriptorProto::options() const { - const ::google::protobuf::FieldOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::FieldOptions& FieldDescriptorProto::options() const { + const PROTOBUF_NAMESPACE_ID::FieldOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_FieldOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_FieldOptions_default_instance_); } -inline ::google::protobuf::FieldOptions* FieldDescriptorProto::release_options() { +inline PROTOBUF_NAMESPACE_ID::FieldOptions* FieldDescriptorProto::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.FieldDescriptorProto.options) _has_bits_[0] &= ~0x00000020u; - ::google::protobuf::FieldOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::FieldOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::FieldOptions* FieldDescriptorProto::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::FieldOptions* FieldDescriptorProto::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FieldDescriptorProto.options) _has_bits_[0] &= ~0x00000020u; - ::google::protobuf::FieldOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::FieldOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::FieldOptions* FieldDescriptorProto::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::FieldOptions* FieldDescriptorProto::mutable_options() { _has_bits_[0] |= 0x00000020u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::FieldOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.options) return options_; } -inline void FieldDescriptorProto::set_allocated_options(::google::protobuf::FieldOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void FieldDescriptorProto::set_allocated_options(PROTOBUF_NAMESPACE_ID::FieldOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000020u; @@ -7608,79 +8256,77 @@ inline bool OneofDescriptorProto::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void OneofDescriptorProto::clear_name() { - name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& OneofDescriptorProto::name() const { +inline const std::string& OneofDescriptorProto::name() const { // @@protoc_insertion_point(field_get:google.protobuf.OneofDescriptorProto.name) return name_.Get(); } -inline void OneofDescriptorProto::set_name(const ::std::string& value) { +inline void OneofDescriptorProto::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.OneofDescriptorProto.name) } -#if LANG_CXX11 -inline void OneofDescriptorProto::set_name(::std::string&& value) { +inline void OneofDescriptorProto::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.OneofDescriptorProto.name) } -#endif inline void OneofDescriptorProto::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.OneofDescriptorProto.name) } inline void OneofDescriptorProto::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.OneofDescriptorProto.name) } -inline ::std::string* OneofDescriptorProto::mutable_name() { +inline std::string* OneofDescriptorProto::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.OneofDescriptorProto.name) - return name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* OneofDescriptorProto::release_name() { +inline std::string* OneofDescriptorProto::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.OneofDescriptorProto.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void OneofDescriptorProto::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void OneofDescriptorProto::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name, + name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.OneofDescriptorProto.name) } -inline ::std::string* OneofDescriptorProto::unsafe_arena_release_name() { +inline std::string* OneofDescriptorProto::unsafe_arena_release_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.OneofDescriptorProto.name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void OneofDescriptorProto::unsafe_arena_set_allocated_name( - ::std::string* name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name != NULL) { + std::string* name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.OneofDescriptorProto.name) } @@ -7690,51 +8336,51 @@ inline bool OneofDescriptorProto::has_options() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void OneofDescriptorProto::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000002u; } -inline const ::google::protobuf::OneofOptions& OneofDescriptorProto::options() const { - const ::google::protobuf::OneofOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::OneofOptions& OneofDescriptorProto::options() const { + const PROTOBUF_NAMESPACE_ID::OneofOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.OneofDescriptorProto.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_OneofOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_OneofOptions_default_instance_); } -inline ::google::protobuf::OneofOptions* OneofDescriptorProto::release_options() { +inline PROTOBUF_NAMESPACE_ID::OneofOptions* OneofDescriptorProto::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.OneofDescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::OneofOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::OneofOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::OneofOptions* OneofDescriptorProto::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::OneofOptions* OneofDescriptorProto::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.OneofDescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::OneofOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::OneofOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::OneofOptions* OneofDescriptorProto::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::OneofOptions* OneofDescriptorProto::mutable_options() { _has_bits_[0] |= 0x00000002u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::OneofOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.OneofDescriptorProto.options) return options_; } -inline void OneofDescriptorProto::set_allocated_options(::google::protobuf::OneofOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void OneofDescriptorProto::set_allocated_options(PROTOBUF_NAMESPACE_ID::OneofOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000002u; @@ -7757,11 +8403,11 @@ inline void EnumDescriptorProto_EnumReservedRange::clear_start() { start_ = 0; _has_bits_[0] &= ~0x00000001u; } -inline ::google::protobuf::int32 EnumDescriptorProto_EnumReservedRange::start() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 EnumDescriptorProto_EnumReservedRange::start() const { // @@protoc_insertion_point(field_get:google.protobuf.EnumDescriptorProto.EnumReservedRange.start) return start_; } -inline void EnumDescriptorProto_EnumReservedRange::set_start(::google::protobuf::int32 value) { +inline void EnumDescriptorProto_EnumReservedRange::set_start(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000001u; start_ = value; // @@protoc_insertion_point(field_set:google.protobuf.EnumDescriptorProto.EnumReservedRange.start) @@ -7775,11 +8421,11 @@ inline void EnumDescriptorProto_EnumReservedRange::clear_end() { end_ = 0; _has_bits_[0] &= ~0x00000002u; } -inline ::google::protobuf::int32 EnumDescriptorProto_EnumReservedRange::end() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 EnumDescriptorProto_EnumReservedRange::end() const { // @@protoc_insertion_point(field_get:google.protobuf.EnumDescriptorProto.EnumReservedRange.end) return end_; } -inline void EnumDescriptorProto_EnumReservedRange::set_end(::google::protobuf::int32 value) { +inline void EnumDescriptorProto_EnumReservedRange::set_end(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000002u; end_ = value; // @@protoc_insertion_point(field_set:google.protobuf.EnumDescriptorProto.EnumReservedRange.end) @@ -7794,79 +8440,77 @@ inline bool EnumDescriptorProto::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void EnumDescriptorProto::clear_name() { - name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& EnumDescriptorProto::name() const { +inline const std::string& EnumDescriptorProto::name() const { // @@protoc_insertion_point(field_get:google.protobuf.EnumDescriptorProto.name) return name_.Get(); } -inline void EnumDescriptorProto::set_name(const ::std::string& value) { +inline void EnumDescriptorProto::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.EnumDescriptorProto.name) } -#if LANG_CXX11 -inline void EnumDescriptorProto::set_name(::std::string&& value) { +inline void EnumDescriptorProto::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.EnumDescriptorProto.name) } -#endif inline void EnumDescriptorProto::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.EnumDescriptorProto.name) } inline void EnumDescriptorProto::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.EnumDescriptorProto.name) } -inline ::std::string* EnumDescriptorProto::mutable_name() { +inline std::string* EnumDescriptorProto::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.EnumDescriptorProto.name) - return name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* EnumDescriptorProto::release_name() { +inline std::string* EnumDescriptorProto::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.EnumDescriptorProto.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void EnumDescriptorProto::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void EnumDescriptorProto::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name, + name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.EnumDescriptorProto.name) } -inline ::std::string* EnumDescriptorProto::unsafe_arena_release_name() { +inline std::string* EnumDescriptorProto::unsafe_arena_release_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.EnumDescriptorProto.name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void EnumDescriptorProto::unsafe_arena_set_allocated_name( - ::std::string* name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name != NULL) { + std::string* name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.EnumDescriptorProto.name) } @@ -7878,24 +8522,24 @@ inline int EnumDescriptorProto::value_size() const { inline void EnumDescriptorProto::clear_value() { value_.Clear(); } -inline ::google::protobuf::EnumValueDescriptorProto* EnumDescriptorProto::mutable_value(int index) { +inline PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto* EnumDescriptorProto::mutable_value(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.EnumDescriptorProto.value) return value_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumValueDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto >* EnumDescriptorProto::mutable_value() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.EnumDescriptorProto.value) return &value_; } -inline const ::google::protobuf::EnumValueDescriptorProto& EnumDescriptorProto::value(int index) const { +inline const PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto& EnumDescriptorProto::value(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.EnumDescriptorProto.value) return value_.Get(index); } -inline ::google::protobuf::EnumValueDescriptorProto* EnumDescriptorProto::add_value() { +inline PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto* EnumDescriptorProto::add_value() { // @@protoc_insertion_point(field_add:google.protobuf.EnumDescriptorProto.value) return value_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumValueDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumValueDescriptorProto >& EnumDescriptorProto::value() const { // @@protoc_insertion_point(field_list:google.protobuf.EnumDescriptorProto.value) return value_; @@ -7906,51 +8550,51 @@ inline bool EnumDescriptorProto::has_options() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void EnumDescriptorProto::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000002u; } -inline const ::google::protobuf::EnumOptions& EnumDescriptorProto::options() const { - const ::google::protobuf::EnumOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::EnumOptions& EnumDescriptorProto::options() const { + const PROTOBUF_NAMESPACE_ID::EnumOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.EnumDescriptorProto.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_EnumOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_EnumOptions_default_instance_); } -inline ::google::protobuf::EnumOptions* EnumDescriptorProto::release_options() { +inline PROTOBUF_NAMESPACE_ID::EnumOptions* EnumDescriptorProto::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.EnumDescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::EnumOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::EnumOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::EnumOptions* EnumDescriptorProto::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::EnumOptions* EnumDescriptorProto::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.EnumDescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::EnumOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::EnumOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::EnumOptions* EnumDescriptorProto::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::EnumOptions* EnumDescriptorProto::mutable_options() { _has_bits_[0] |= 0x00000002u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::EnumOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.EnumDescriptorProto.options) return options_; } -inline void EnumDescriptorProto::set_allocated_options(::google::protobuf::EnumOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void EnumDescriptorProto::set_allocated_options(PROTOBUF_NAMESPACE_ID::EnumOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000002u; @@ -7968,24 +8612,24 @@ inline int EnumDescriptorProto::reserved_range_size() const { inline void EnumDescriptorProto::clear_reserved_range() { reserved_range_.Clear(); } -inline ::google::protobuf::EnumDescriptorProto_EnumReservedRange* EnumDescriptorProto::mutable_reserved_range(int index) { +inline PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange* EnumDescriptorProto::mutable_reserved_range(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.EnumDescriptorProto.reserved_range) return reserved_range_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto_EnumReservedRange >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange >* EnumDescriptorProto::mutable_reserved_range() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.EnumDescriptorProto.reserved_range) return &reserved_range_; } -inline const ::google::protobuf::EnumDescriptorProto_EnumReservedRange& EnumDescriptorProto::reserved_range(int index) const { +inline const PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange& EnumDescriptorProto::reserved_range(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.EnumDescriptorProto.reserved_range) return reserved_range_.Get(index); } -inline ::google::protobuf::EnumDescriptorProto_EnumReservedRange* EnumDescriptorProto::add_reserved_range() { +inline PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange* EnumDescriptorProto::add_reserved_range() { // @@protoc_insertion_point(field_add:google.protobuf.EnumDescriptorProto.reserved_range) return reserved_range_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumDescriptorProto_EnumReservedRange >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::EnumDescriptorProto_EnumReservedRange >& EnumDescriptorProto::reserved_range() const { // @@protoc_insertion_point(field_list:google.protobuf.EnumDescriptorProto.reserved_range) return reserved_range_; @@ -7998,26 +8642,24 @@ inline int EnumDescriptorProto::reserved_name_size() const { inline void EnumDescriptorProto::clear_reserved_name() { reserved_name_.Clear(); } -inline const ::std::string& EnumDescriptorProto::reserved_name(int index) const { +inline const std::string& EnumDescriptorProto::reserved_name(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.EnumDescriptorProto.reserved_name) return reserved_name_.Get(index); } -inline ::std::string* EnumDescriptorProto::mutable_reserved_name(int index) { +inline std::string* EnumDescriptorProto::mutable_reserved_name(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.EnumDescriptorProto.reserved_name) return reserved_name_.Mutable(index); } -inline void EnumDescriptorProto::set_reserved_name(int index, const ::std::string& value) { +inline void EnumDescriptorProto::set_reserved_name(int index, const std::string& value) { // @@protoc_insertion_point(field_set:google.protobuf.EnumDescriptorProto.reserved_name) reserved_name_.Mutable(index)->assign(value); } -#if LANG_CXX11 -inline void EnumDescriptorProto::set_reserved_name(int index, ::std::string&& value) { +inline void EnumDescriptorProto::set_reserved_name(int index, std::string&& value) { // @@protoc_insertion_point(field_set:google.protobuf.EnumDescriptorProto.reserved_name) reserved_name_.Mutable(index)->assign(std::move(value)); } -#endif inline void EnumDescriptorProto::set_reserved_name(int index, const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); reserved_name_.Mutable(index)->assign(value); // @@protoc_insertion_point(field_set_char:google.protobuf.EnumDescriptorProto.reserved_name) } @@ -8026,22 +8668,20 @@ inline void EnumDescriptorProto::set_reserved_name(int index, const char* value, reinterpret_cast(value), size); // @@protoc_insertion_point(field_set_pointer:google.protobuf.EnumDescriptorProto.reserved_name) } -inline ::std::string* EnumDescriptorProto::add_reserved_name() { +inline std::string* EnumDescriptorProto::add_reserved_name() { // @@protoc_insertion_point(field_add_mutable:google.protobuf.EnumDescriptorProto.reserved_name) return reserved_name_.Add(); } -inline void EnumDescriptorProto::add_reserved_name(const ::std::string& value) { +inline void EnumDescriptorProto::add_reserved_name(const std::string& value) { reserved_name_.Add()->assign(value); // @@protoc_insertion_point(field_add:google.protobuf.EnumDescriptorProto.reserved_name) } -#if LANG_CXX11 -inline void EnumDescriptorProto::add_reserved_name(::std::string&& value) { +inline void EnumDescriptorProto::add_reserved_name(std::string&& value) { reserved_name_.Add(std::move(value)); // @@protoc_insertion_point(field_add:google.protobuf.EnumDescriptorProto.reserved_name) } -#endif inline void EnumDescriptorProto::add_reserved_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); reserved_name_.Add()->assign(value); // @@protoc_insertion_point(field_add_char:google.protobuf.EnumDescriptorProto.reserved_name) } @@ -8049,12 +8689,12 @@ inline void EnumDescriptorProto::add_reserved_name(const char* value, size_t siz reserved_name_.Add()->assign(reinterpret_cast(value), size); // @@protoc_insertion_point(field_add_pointer:google.protobuf.EnumDescriptorProto.reserved_name) } -inline const ::google::protobuf::RepeatedPtrField<::std::string>& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& EnumDescriptorProto::reserved_name() const { // @@protoc_insertion_point(field_list:google.protobuf.EnumDescriptorProto.reserved_name) return reserved_name_; } -inline ::google::protobuf::RepeatedPtrField<::std::string>* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* EnumDescriptorProto::mutable_reserved_name() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.EnumDescriptorProto.reserved_name) return &reserved_name_; @@ -8069,79 +8709,77 @@ inline bool EnumValueDescriptorProto::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void EnumValueDescriptorProto::clear_name() { - name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& EnumValueDescriptorProto::name() const { +inline const std::string& EnumValueDescriptorProto::name() const { // @@protoc_insertion_point(field_get:google.protobuf.EnumValueDescriptorProto.name) return name_.Get(); } -inline void EnumValueDescriptorProto::set_name(const ::std::string& value) { +inline void EnumValueDescriptorProto::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.EnumValueDescriptorProto.name) } -#if LANG_CXX11 -inline void EnumValueDescriptorProto::set_name(::std::string&& value) { +inline void EnumValueDescriptorProto::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.EnumValueDescriptorProto.name) } -#endif inline void EnumValueDescriptorProto::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.EnumValueDescriptorProto.name) } inline void EnumValueDescriptorProto::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.EnumValueDescriptorProto.name) } -inline ::std::string* EnumValueDescriptorProto::mutable_name() { +inline std::string* EnumValueDescriptorProto::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.EnumValueDescriptorProto.name) - return name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* EnumValueDescriptorProto::release_name() { +inline std::string* EnumValueDescriptorProto::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.EnumValueDescriptorProto.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void EnumValueDescriptorProto::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void EnumValueDescriptorProto::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name, + name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.EnumValueDescriptorProto.name) } -inline ::std::string* EnumValueDescriptorProto::unsafe_arena_release_name() { +inline std::string* EnumValueDescriptorProto::unsafe_arena_release_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.EnumValueDescriptorProto.name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void EnumValueDescriptorProto::unsafe_arena_set_allocated_name( - ::std::string* name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name != NULL) { + std::string* name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.EnumValueDescriptorProto.name) } @@ -8154,11 +8792,11 @@ inline void EnumValueDescriptorProto::clear_number() { number_ = 0; _has_bits_[0] &= ~0x00000004u; } -inline ::google::protobuf::int32 EnumValueDescriptorProto::number() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 EnumValueDescriptorProto::number() const { // @@protoc_insertion_point(field_get:google.protobuf.EnumValueDescriptorProto.number) return number_; } -inline void EnumValueDescriptorProto::set_number(::google::protobuf::int32 value) { +inline void EnumValueDescriptorProto::set_number(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000004u; number_ = value; // @@protoc_insertion_point(field_set:google.protobuf.EnumValueDescriptorProto.number) @@ -8169,51 +8807,51 @@ inline bool EnumValueDescriptorProto::has_options() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void EnumValueDescriptorProto::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000002u; } -inline const ::google::protobuf::EnumValueOptions& EnumValueDescriptorProto::options() const { - const ::google::protobuf::EnumValueOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::EnumValueOptions& EnumValueDescriptorProto::options() const { + const PROTOBUF_NAMESPACE_ID::EnumValueOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.EnumValueDescriptorProto.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_EnumValueOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_EnumValueOptions_default_instance_); } -inline ::google::protobuf::EnumValueOptions* EnumValueDescriptorProto::release_options() { +inline PROTOBUF_NAMESPACE_ID::EnumValueOptions* EnumValueDescriptorProto::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.EnumValueDescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::EnumValueOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::EnumValueOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::EnumValueOptions* EnumValueDescriptorProto::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::EnumValueOptions* EnumValueDescriptorProto::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.EnumValueDescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::EnumValueOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::EnumValueOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::EnumValueOptions* EnumValueDescriptorProto::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::EnumValueOptions* EnumValueDescriptorProto::mutable_options() { _has_bits_[0] |= 0x00000002u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::EnumValueOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.EnumValueDescriptorProto.options) return options_; } -inline void EnumValueDescriptorProto::set_allocated_options(::google::protobuf::EnumValueOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void EnumValueDescriptorProto::set_allocated_options(PROTOBUF_NAMESPACE_ID::EnumValueOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000002u; @@ -8233,79 +8871,77 @@ inline bool ServiceDescriptorProto::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void ServiceDescriptorProto::clear_name() { - name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& ServiceDescriptorProto::name() const { +inline const std::string& ServiceDescriptorProto::name() const { // @@protoc_insertion_point(field_get:google.protobuf.ServiceDescriptorProto.name) return name_.Get(); } -inline void ServiceDescriptorProto::set_name(const ::std::string& value) { +inline void ServiceDescriptorProto::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.ServiceDescriptorProto.name) } -#if LANG_CXX11 -inline void ServiceDescriptorProto::set_name(::std::string&& value) { +inline void ServiceDescriptorProto::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.ServiceDescriptorProto.name) } -#endif inline void ServiceDescriptorProto::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.ServiceDescriptorProto.name) } inline void ServiceDescriptorProto::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.ServiceDescriptorProto.name) } -inline ::std::string* ServiceDescriptorProto::mutable_name() { +inline std::string* ServiceDescriptorProto::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.ServiceDescriptorProto.name) - return name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* ServiceDescriptorProto::release_name() { +inline std::string* ServiceDescriptorProto::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.ServiceDescriptorProto.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void ServiceDescriptorProto::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void ServiceDescriptorProto::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name, + name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.ServiceDescriptorProto.name) } -inline ::std::string* ServiceDescriptorProto::unsafe_arena_release_name() { +inline std::string* ServiceDescriptorProto::unsafe_arena_release_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.ServiceDescriptorProto.name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void ServiceDescriptorProto::unsafe_arena_set_allocated_name( - ::std::string* name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name != NULL) { + std::string* name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.ServiceDescriptorProto.name) } @@ -8317,24 +8953,24 @@ inline int ServiceDescriptorProto::method_size() const { inline void ServiceDescriptorProto::clear_method() { method_.Clear(); } -inline ::google::protobuf::MethodDescriptorProto* ServiceDescriptorProto::mutable_method(int index) { +inline PROTOBUF_NAMESPACE_ID::MethodDescriptorProto* ServiceDescriptorProto::mutable_method(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.ServiceDescriptorProto.method) return method_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::MethodDescriptorProto >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::MethodDescriptorProto >* ServiceDescriptorProto::mutable_method() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.ServiceDescriptorProto.method) return &method_; } -inline const ::google::protobuf::MethodDescriptorProto& ServiceDescriptorProto::method(int index) const { +inline const PROTOBUF_NAMESPACE_ID::MethodDescriptorProto& ServiceDescriptorProto::method(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.ServiceDescriptorProto.method) return method_.Get(index); } -inline ::google::protobuf::MethodDescriptorProto* ServiceDescriptorProto::add_method() { +inline PROTOBUF_NAMESPACE_ID::MethodDescriptorProto* ServiceDescriptorProto::add_method() { // @@protoc_insertion_point(field_add:google.protobuf.ServiceDescriptorProto.method) return method_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::MethodDescriptorProto >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::MethodDescriptorProto >& ServiceDescriptorProto::method() const { // @@protoc_insertion_point(field_list:google.protobuf.ServiceDescriptorProto.method) return method_; @@ -8345,51 +8981,51 @@ inline bool ServiceDescriptorProto::has_options() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void ServiceDescriptorProto::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000002u; } -inline const ::google::protobuf::ServiceOptions& ServiceDescriptorProto::options() const { - const ::google::protobuf::ServiceOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::ServiceOptions& ServiceDescriptorProto::options() const { + const PROTOBUF_NAMESPACE_ID::ServiceOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.ServiceDescriptorProto.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_ServiceOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_ServiceOptions_default_instance_); } -inline ::google::protobuf::ServiceOptions* ServiceDescriptorProto::release_options() { +inline PROTOBUF_NAMESPACE_ID::ServiceOptions* ServiceDescriptorProto::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.ServiceDescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::ServiceOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::ServiceOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::ServiceOptions* ServiceDescriptorProto::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::ServiceOptions* ServiceDescriptorProto::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.ServiceDescriptorProto.options) _has_bits_[0] &= ~0x00000002u; - ::google::protobuf::ServiceOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::ServiceOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::ServiceOptions* ServiceDescriptorProto::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::ServiceOptions* ServiceDescriptorProto::mutable_options() { _has_bits_[0] |= 0x00000002u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::ServiceOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.ServiceDescriptorProto.options) return options_; } -inline void ServiceDescriptorProto::set_allocated_options(::google::protobuf::ServiceOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void ServiceDescriptorProto::set_allocated_options(PROTOBUF_NAMESPACE_ID::ServiceOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000002u; @@ -8409,79 +9045,77 @@ inline bool MethodDescriptorProto::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void MethodDescriptorProto::clear_name() { - name_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& MethodDescriptorProto::name() const { +inline const std::string& MethodDescriptorProto::name() const { // @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.name) return name_.Get(); } -inline void MethodDescriptorProto::set_name(const ::std::string& value) { +inline void MethodDescriptorProto::set_name(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.MethodDescriptorProto.name) } -#if LANG_CXX11 -inline void MethodDescriptorProto::set_name(::std::string&& value) { +inline void MethodDescriptorProto::set_name(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.MethodDescriptorProto.name) } -#endif inline void MethodDescriptorProto::set_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.MethodDescriptorProto.name) } inline void MethodDescriptorProto::set_name(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.MethodDescriptorProto.name) } -inline ::std::string* MethodDescriptorProto::mutable_name() { +inline std::string* MethodDescriptorProto::mutable_name() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.MethodDescriptorProto.name) - return name_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* MethodDescriptorProto::release_name() { +inline std::string* MethodDescriptorProto::release_name() { // @@protoc_insertion_point(field_release:google.protobuf.MethodDescriptorProto.name) if (!has_name()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void MethodDescriptorProto::set_allocated_name(::std::string* name) { - if (name != NULL) { +inline void MethodDescriptorProto::set_allocated_name(std::string* name) { + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name, + name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.MethodDescriptorProto.name) } -inline ::std::string* MethodDescriptorProto::unsafe_arena_release_name() { +inline std::string* MethodDescriptorProto::unsafe_arena_release_name() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.MethodDescriptorProto.name) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void MethodDescriptorProto::unsafe_arena_set_allocated_name( - ::std::string* name) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name != NULL) { + std::string* name) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.MethodDescriptorProto.name) } @@ -8491,79 +9125,77 @@ inline bool MethodDescriptorProto::has_input_type() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void MethodDescriptorProto::clear_input_type() { - input_type_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + input_type_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000002u; } -inline const ::std::string& MethodDescriptorProto::input_type() const { +inline const std::string& MethodDescriptorProto::input_type() const { // @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.input_type) return input_type_.Get(); } -inline void MethodDescriptorProto::set_input_type(const ::std::string& value) { +inline void MethodDescriptorProto::set_input_type(const std::string& value) { _has_bits_[0] |= 0x00000002u; - input_type_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + input_type_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.MethodDescriptorProto.input_type) } -#if LANG_CXX11 -inline void MethodDescriptorProto::set_input_type(::std::string&& value) { +inline void MethodDescriptorProto::set_input_type(std::string&& value) { _has_bits_[0] |= 0x00000002u; input_type_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.MethodDescriptorProto.input_type) } -#endif inline void MethodDescriptorProto::set_input_type(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000002u; - input_type_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + input_type_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.MethodDescriptorProto.input_type) } inline void MethodDescriptorProto::set_input_type(const char* value, size_t size) { _has_bits_[0] |= 0x00000002u; - input_type_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + input_type_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.MethodDescriptorProto.input_type) } -inline ::std::string* MethodDescriptorProto::mutable_input_type() { +inline std::string* MethodDescriptorProto::mutable_input_type() { _has_bits_[0] |= 0x00000002u; // @@protoc_insertion_point(field_mutable:google.protobuf.MethodDescriptorProto.input_type) - return input_type_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return input_type_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* MethodDescriptorProto::release_input_type() { +inline std::string* MethodDescriptorProto::release_input_type() { // @@protoc_insertion_point(field_release:google.protobuf.MethodDescriptorProto.input_type) if (!has_input_type()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000002u; - return input_type_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return input_type_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void MethodDescriptorProto::set_allocated_input_type(::std::string* input_type) { - if (input_type != NULL) { +inline void MethodDescriptorProto::set_allocated_input_type(std::string* input_type) { + if (input_type != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - input_type_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), input_type, + input_type_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), input_type, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.MethodDescriptorProto.input_type) } -inline ::std::string* MethodDescriptorProto::unsafe_arena_release_input_type() { +inline std::string* MethodDescriptorProto::unsafe_arena_release_input_type() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.MethodDescriptorProto.input_type) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000002u; - return input_type_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return input_type_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void MethodDescriptorProto::unsafe_arena_set_allocated_input_type( - ::std::string* input_type) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (input_type != NULL) { + std::string* input_type) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (input_type != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - input_type_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + input_type_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), input_type, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.MethodDescriptorProto.input_type) } @@ -8573,79 +9205,77 @@ inline bool MethodDescriptorProto::has_output_type() const { return (_has_bits_[0] & 0x00000004u) != 0; } inline void MethodDescriptorProto::clear_output_type() { - output_type_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + output_type_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000004u; } -inline const ::std::string& MethodDescriptorProto::output_type() const { +inline const std::string& MethodDescriptorProto::output_type() const { // @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.output_type) return output_type_.Get(); } -inline void MethodDescriptorProto::set_output_type(const ::std::string& value) { +inline void MethodDescriptorProto::set_output_type(const std::string& value) { _has_bits_[0] |= 0x00000004u; - output_type_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + output_type_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.MethodDescriptorProto.output_type) } -#if LANG_CXX11 -inline void MethodDescriptorProto::set_output_type(::std::string&& value) { +inline void MethodDescriptorProto::set_output_type(std::string&& value) { _has_bits_[0] |= 0x00000004u; output_type_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.MethodDescriptorProto.output_type) } -#endif inline void MethodDescriptorProto::set_output_type(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000004u; - output_type_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + output_type_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.MethodDescriptorProto.output_type) } inline void MethodDescriptorProto::set_output_type(const char* value, size_t size) { _has_bits_[0] |= 0x00000004u; - output_type_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + output_type_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.MethodDescriptorProto.output_type) } -inline ::std::string* MethodDescriptorProto::mutable_output_type() { +inline std::string* MethodDescriptorProto::mutable_output_type() { _has_bits_[0] |= 0x00000004u; // @@protoc_insertion_point(field_mutable:google.protobuf.MethodDescriptorProto.output_type) - return output_type_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return output_type_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* MethodDescriptorProto::release_output_type() { +inline std::string* MethodDescriptorProto::release_output_type() { // @@protoc_insertion_point(field_release:google.protobuf.MethodDescriptorProto.output_type) if (!has_output_type()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000004u; - return output_type_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return output_type_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void MethodDescriptorProto::set_allocated_output_type(::std::string* output_type) { - if (output_type != NULL) { +inline void MethodDescriptorProto::set_allocated_output_type(std::string* output_type) { + if (output_type != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - output_type_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), output_type, + output_type_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), output_type, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.MethodDescriptorProto.output_type) } -inline ::std::string* MethodDescriptorProto::unsafe_arena_release_output_type() { +inline std::string* MethodDescriptorProto::unsafe_arena_release_output_type() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.MethodDescriptorProto.output_type) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000004u; - return output_type_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return output_type_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void MethodDescriptorProto::unsafe_arena_set_allocated_output_type( - ::std::string* output_type) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (output_type != NULL) { + std::string* output_type) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (output_type != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - output_type_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + output_type_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), output_type, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.MethodDescriptorProto.output_type) } @@ -8655,51 +9285,51 @@ inline bool MethodDescriptorProto::has_options() const { return (_has_bits_[0] & 0x00000008u) != 0; } inline void MethodDescriptorProto::clear_options() { - if (options_ != NULL) options_->Clear(); + if (options_ != nullptr) options_->Clear(); _has_bits_[0] &= ~0x00000008u; } -inline const ::google::protobuf::MethodOptions& MethodDescriptorProto::options() const { - const ::google::protobuf::MethodOptions* p = options_; +inline const PROTOBUF_NAMESPACE_ID::MethodOptions& MethodDescriptorProto::options() const { + const PROTOBUF_NAMESPACE_ID::MethodOptions* p = options_; // @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.options) - return p != NULL ? *p : *reinterpret_cast( - &::google::protobuf::_MethodOptions_default_instance_); + return p != nullptr ? *p : *reinterpret_cast( + &PROTOBUF_NAMESPACE_ID::_MethodOptions_default_instance_); } -inline ::google::protobuf::MethodOptions* MethodDescriptorProto::release_options() { +inline PROTOBUF_NAMESPACE_ID::MethodOptions* MethodDescriptorProto::release_options() { // @@protoc_insertion_point(field_release:google.protobuf.MethodDescriptorProto.options) _has_bits_[0] &= ~0x00000008u; - ::google::protobuf::MethodOptions* temp = options_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::MethodOptions* temp = options_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - options_ = NULL; + options_ = nullptr; return temp; } -inline ::google::protobuf::MethodOptions* MethodDescriptorProto::unsafe_arena_release_options() { +inline PROTOBUF_NAMESPACE_ID::MethodOptions* MethodDescriptorProto::unsafe_arena_release_options() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.MethodDescriptorProto.options) _has_bits_[0] &= ~0x00000008u; - ::google::protobuf::MethodOptions* temp = options_; - options_ = NULL; + PROTOBUF_NAMESPACE_ID::MethodOptions* temp = options_; + options_ = nullptr; return temp; } -inline ::google::protobuf::MethodOptions* MethodDescriptorProto::mutable_options() { +inline PROTOBUF_NAMESPACE_ID::MethodOptions* MethodDescriptorProto::mutable_options() { _has_bits_[0] |= 0x00000008u; - if (options_ == NULL) { - auto* p = CreateMaybeMessage<::google::protobuf::MethodOptions>(GetArenaNoVirtual()); + if (options_ == nullptr) { + auto* p = CreateMaybeMessage(GetArenaNoVirtual()); options_ = p; } // @@protoc_insertion_point(field_mutable:google.protobuf.MethodDescriptorProto.options) return options_; } -inline void MethodDescriptorProto::set_allocated_options(::google::protobuf::MethodOptions* options) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); - if (message_arena == NULL) { +inline void MethodDescriptorProto::set_allocated_options(PROTOBUF_NAMESPACE_ID::MethodOptions* options) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); + if (message_arena == nullptr) { delete options_; } if (options) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(options); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(options); if (message_arena != submessage_arena) { - options = ::google::protobuf::internal::GetOwnedMessage( + options = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, options, submessage_arena); } _has_bits_[0] |= 0x00000008u; @@ -8755,79 +9385,77 @@ inline bool FileOptions::has_java_package() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void FileOptions::clear_java_package() { - java_package_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + java_package_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& FileOptions::java_package() const { +inline const std::string& FileOptions::java_package() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.java_package) return java_package_.Get(); } -inline void FileOptions::set_java_package(const ::std::string& value) { +inline void FileOptions::set_java_package(const std::string& value) { _has_bits_[0] |= 0x00000001u; - java_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + java_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.java_package) } -#if LANG_CXX11 -inline void FileOptions::set_java_package(::std::string&& value) { +inline void FileOptions::set_java_package(std::string&& value) { _has_bits_[0] |= 0x00000001u; java_package_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.java_package) } -#endif inline void FileOptions::set_java_package(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - java_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + java_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.java_package) } inline void FileOptions::set_java_package(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - java_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + java_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.java_package) } -inline ::std::string* FileOptions::mutable_java_package() { +inline std::string* FileOptions::mutable_java_package() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.java_package) - return java_package_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return java_package_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_java_package() { +inline std::string* FileOptions::release_java_package() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.java_package) if (!has_java_package()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return java_package_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return java_package_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_java_package(::std::string* java_package) { - if (java_package != NULL) { +inline void FileOptions::set_allocated_java_package(std::string* java_package) { + if (java_package != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - java_package_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), java_package, + java_package_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), java_package, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.java_package) } -inline ::std::string* FileOptions::unsafe_arena_release_java_package() { +inline std::string* FileOptions::unsafe_arena_release_java_package() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.java_package) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return java_package_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return java_package_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_java_package( - ::std::string* java_package) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (java_package != NULL) { + std::string* java_package) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (java_package != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - java_package_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + java_package_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), java_package, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.java_package) } @@ -8837,79 +9465,77 @@ inline bool FileOptions::has_java_outer_classname() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void FileOptions::clear_java_outer_classname() { - java_outer_classname_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + java_outer_classname_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000002u; } -inline const ::std::string& FileOptions::java_outer_classname() const { +inline const std::string& FileOptions::java_outer_classname() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.java_outer_classname) return java_outer_classname_.Get(); } -inline void FileOptions::set_java_outer_classname(const ::std::string& value) { +inline void FileOptions::set_java_outer_classname(const std::string& value) { _has_bits_[0] |= 0x00000002u; - java_outer_classname_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + java_outer_classname_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.java_outer_classname) } -#if LANG_CXX11 -inline void FileOptions::set_java_outer_classname(::std::string&& value) { +inline void FileOptions::set_java_outer_classname(std::string&& value) { _has_bits_[0] |= 0x00000002u; java_outer_classname_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.java_outer_classname) } -#endif inline void FileOptions::set_java_outer_classname(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000002u; - java_outer_classname_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + java_outer_classname_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.java_outer_classname) } inline void FileOptions::set_java_outer_classname(const char* value, size_t size) { _has_bits_[0] |= 0x00000002u; - java_outer_classname_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + java_outer_classname_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.java_outer_classname) } -inline ::std::string* FileOptions::mutable_java_outer_classname() { +inline std::string* FileOptions::mutable_java_outer_classname() { _has_bits_[0] |= 0x00000002u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.java_outer_classname) - return java_outer_classname_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return java_outer_classname_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_java_outer_classname() { +inline std::string* FileOptions::release_java_outer_classname() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.java_outer_classname) if (!has_java_outer_classname()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000002u; - return java_outer_classname_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return java_outer_classname_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_java_outer_classname(::std::string* java_outer_classname) { - if (java_outer_classname != NULL) { +inline void FileOptions::set_allocated_java_outer_classname(std::string* java_outer_classname) { + if (java_outer_classname != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - java_outer_classname_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), java_outer_classname, + java_outer_classname_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), java_outer_classname, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.java_outer_classname) } -inline ::std::string* FileOptions::unsafe_arena_release_java_outer_classname() { +inline std::string* FileOptions::unsafe_arena_release_java_outer_classname() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.java_outer_classname) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000002u; - return java_outer_classname_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return java_outer_classname_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_java_outer_classname( - ::std::string* java_outer_classname) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (java_outer_classname != NULL) { + std::string* java_outer_classname) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (java_outer_classname != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - java_outer_classname_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + java_outer_classname_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), java_outer_classname, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.java_outer_classname) } @@ -8976,12 +9602,12 @@ inline void FileOptions::clear_optimize_for() { optimize_for_ = 1; _has_bits_[0] &= ~0x00080000u; } -inline ::google::protobuf::FileOptions_OptimizeMode FileOptions::optimize_for() const { +inline PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode FileOptions::optimize_for() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.optimize_for) - return static_cast< ::google::protobuf::FileOptions_OptimizeMode >(optimize_for_); + return static_cast< PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode >(optimize_for_); } -inline void FileOptions::set_optimize_for(::google::protobuf::FileOptions_OptimizeMode value) { - assert(::google::protobuf::FileOptions_OptimizeMode_IsValid(value)); +inline void FileOptions::set_optimize_for(PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode value) { + assert(PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode_IsValid(value)); _has_bits_[0] |= 0x00080000u; optimize_for_ = value; // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.optimize_for) @@ -8992,79 +9618,77 @@ inline bool FileOptions::has_go_package() const { return (_has_bits_[0] & 0x00000004u) != 0; } inline void FileOptions::clear_go_package() { - go_package_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + go_package_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000004u; } -inline const ::std::string& FileOptions::go_package() const { +inline const std::string& FileOptions::go_package() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.go_package) return go_package_.Get(); } -inline void FileOptions::set_go_package(const ::std::string& value) { +inline void FileOptions::set_go_package(const std::string& value) { _has_bits_[0] |= 0x00000004u; - go_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + go_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.go_package) } -#if LANG_CXX11 -inline void FileOptions::set_go_package(::std::string&& value) { +inline void FileOptions::set_go_package(std::string&& value) { _has_bits_[0] |= 0x00000004u; go_package_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.go_package) } -#endif inline void FileOptions::set_go_package(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000004u; - go_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + go_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.go_package) } inline void FileOptions::set_go_package(const char* value, size_t size) { _has_bits_[0] |= 0x00000004u; - go_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + go_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.go_package) } -inline ::std::string* FileOptions::mutable_go_package() { +inline std::string* FileOptions::mutable_go_package() { _has_bits_[0] |= 0x00000004u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.go_package) - return go_package_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return go_package_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_go_package() { +inline std::string* FileOptions::release_go_package() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.go_package) if (!has_go_package()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000004u; - return go_package_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return go_package_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_go_package(::std::string* go_package) { - if (go_package != NULL) { +inline void FileOptions::set_allocated_go_package(std::string* go_package) { + if (go_package != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - go_package_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), go_package, + go_package_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), go_package, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.go_package) } -inline ::std::string* FileOptions::unsafe_arena_release_go_package() { +inline std::string* FileOptions::unsafe_arena_release_go_package() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.go_package) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000004u; - return go_package_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return go_package_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_go_package( - ::std::string* go_package) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (go_package != NULL) { + std::string* go_package) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (go_package != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - go_package_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + go_package_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), go_package, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.go_package) } @@ -9182,79 +9806,77 @@ inline bool FileOptions::has_objc_class_prefix() const { return (_has_bits_[0] & 0x00000008u) != 0; } inline void FileOptions::clear_objc_class_prefix() { - objc_class_prefix_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + objc_class_prefix_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000008u; } -inline const ::std::string& FileOptions::objc_class_prefix() const { +inline const std::string& FileOptions::objc_class_prefix() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.objc_class_prefix) return objc_class_prefix_.Get(); } -inline void FileOptions::set_objc_class_prefix(const ::std::string& value) { +inline void FileOptions::set_objc_class_prefix(const std::string& value) { _has_bits_[0] |= 0x00000008u; - objc_class_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + objc_class_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.objc_class_prefix) } -#if LANG_CXX11 -inline void FileOptions::set_objc_class_prefix(::std::string&& value) { +inline void FileOptions::set_objc_class_prefix(std::string&& value) { _has_bits_[0] |= 0x00000008u; objc_class_prefix_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.objc_class_prefix) } -#endif inline void FileOptions::set_objc_class_prefix(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000008u; - objc_class_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + objc_class_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.objc_class_prefix) } inline void FileOptions::set_objc_class_prefix(const char* value, size_t size) { _has_bits_[0] |= 0x00000008u; - objc_class_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + objc_class_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.objc_class_prefix) } -inline ::std::string* FileOptions::mutable_objc_class_prefix() { +inline std::string* FileOptions::mutable_objc_class_prefix() { _has_bits_[0] |= 0x00000008u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.objc_class_prefix) - return objc_class_prefix_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return objc_class_prefix_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_objc_class_prefix() { +inline std::string* FileOptions::release_objc_class_prefix() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.objc_class_prefix) if (!has_objc_class_prefix()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000008u; - return objc_class_prefix_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return objc_class_prefix_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_objc_class_prefix(::std::string* objc_class_prefix) { - if (objc_class_prefix != NULL) { +inline void FileOptions::set_allocated_objc_class_prefix(std::string* objc_class_prefix) { + if (objc_class_prefix != nullptr) { _has_bits_[0] |= 0x00000008u; } else { _has_bits_[0] &= ~0x00000008u; } - objc_class_prefix_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), objc_class_prefix, + objc_class_prefix_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), objc_class_prefix, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.objc_class_prefix) } -inline ::std::string* FileOptions::unsafe_arena_release_objc_class_prefix() { +inline std::string* FileOptions::unsafe_arena_release_objc_class_prefix() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.objc_class_prefix) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000008u; - return objc_class_prefix_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return objc_class_prefix_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_objc_class_prefix( - ::std::string* objc_class_prefix) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (objc_class_prefix != NULL) { + std::string* objc_class_prefix) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (objc_class_prefix != nullptr) { _has_bits_[0] |= 0x00000008u; } else { _has_bits_[0] &= ~0x00000008u; } - objc_class_prefix_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + objc_class_prefix_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), objc_class_prefix, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.objc_class_prefix) } @@ -9264,79 +9886,77 @@ inline bool FileOptions::has_csharp_namespace() const { return (_has_bits_[0] & 0x00000010u) != 0; } inline void FileOptions::clear_csharp_namespace() { - csharp_namespace_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + csharp_namespace_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000010u; } -inline const ::std::string& FileOptions::csharp_namespace() const { +inline const std::string& FileOptions::csharp_namespace() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.csharp_namespace) return csharp_namespace_.Get(); } -inline void FileOptions::set_csharp_namespace(const ::std::string& value) { +inline void FileOptions::set_csharp_namespace(const std::string& value) { _has_bits_[0] |= 0x00000010u; - csharp_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + csharp_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.csharp_namespace) } -#if LANG_CXX11 -inline void FileOptions::set_csharp_namespace(::std::string&& value) { +inline void FileOptions::set_csharp_namespace(std::string&& value) { _has_bits_[0] |= 0x00000010u; csharp_namespace_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.csharp_namespace) } -#endif inline void FileOptions::set_csharp_namespace(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000010u; - csharp_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + csharp_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.csharp_namespace) } inline void FileOptions::set_csharp_namespace(const char* value, size_t size) { _has_bits_[0] |= 0x00000010u; - csharp_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + csharp_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.csharp_namespace) } -inline ::std::string* FileOptions::mutable_csharp_namespace() { +inline std::string* FileOptions::mutable_csharp_namespace() { _has_bits_[0] |= 0x00000010u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.csharp_namespace) - return csharp_namespace_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return csharp_namespace_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_csharp_namespace() { +inline std::string* FileOptions::release_csharp_namespace() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.csharp_namespace) if (!has_csharp_namespace()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000010u; - return csharp_namespace_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return csharp_namespace_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_csharp_namespace(::std::string* csharp_namespace) { - if (csharp_namespace != NULL) { +inline void FileOptions::set_allocated_csharp_namespace(std::string* csharp_namespace) { + if (csharp_namespace != nullptr) { _has_bits_[0] |= 0x00000010u; } else { _has_bits_[0] &= ~0x00000010u; } - csharp_namespace_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), csharp_namespace, + csharp_namespace_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), csharp_namespace, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.csharp_namespace) } -inline ::std::string* FileOptions::unsafe_arena_release_csharp_namespace() { +inline std::string* FileOptions::unsafe_arena_release_csharp_namespace() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.csharp_namespace) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000010u; - return csharp_namespace_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return csharp_namespace_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_csharp_namespace( - ::std::string* csharp_namespace) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (csharp_namespace != NULL) { + std::string* csharp_namespace) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (csharp_namespace != nullptr) { _has_bits_[0] |= 0x00000010u; } else { _has_bits_[0] &= ~0x00000010u; } - csharp_namespace_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + csharp_namespace_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), csharp_namespace, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.csharp_namespace) } @@ -9346,79 +9966,77 @@ inline bool FileOptions::has_swift_prefix() const { return (_has_bits_[0] & 0x00000020u) != 0; } inline void FileOptions::clear_swift_prefix() { - swift_prefix_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + swift_prefix_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000020u; } -inline const ::std::string& FileOptions::swift_prefix() const { +inline const std::string& FileOptions::swift_prefix() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.swift_prefix) return swift_prefix_.Get(); } -inline void FileOptions::set_swift_prefix(const ::std::string& value) { +inline void FileOptions::set_swift_prefix(const std::string& value) { _has_bits_[0] |= 0x00000020u; - swift_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + swift_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.swift_prefix) } -#if LANG_CXX11 -inline void FileOptions::set_swift_prefix(::std::string&& value) { +inline void FileOptions::set_swift_prefix(std::string&& value) { _has_bits_[0] |= 0x00000020u; swift_prefix_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.swift_prefix) } -#endif inline void FileOptions::set_swift_prefix(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000020u; - swift_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + swift_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.swift_prefix) } inline void FileOptions::set_swift_prefix(const char* value, size_t size) { _has_bits_[0] |= 0x00000020u; - swift_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + swift_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.swift_prefix) } -inline ::std::string* FileOptions::mutable_swift_prefix() { +inline std::string* FileOptions::mutable_swift_prefix() { _has_bits_[0] |= 0x00000020u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.swift_prefix) - return swift_prefix_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return swift_prefix_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_swift_prefix() { +inline std::string* FileOptions::release_swift_prefix() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.swift_prefix) if (!has_swift_prefix()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000020u; - return swift_prefix_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return swift_prefix_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_swift_prefix(::std::string* swift_prefix) { - if (swift_prefix != NULL) { +inline void FileOptions::set_allocated_swift_prefix(std::string* swift_prefix) { + if (swift_prefix != nullptr) { _has_bits_[0] |= 0x00000020u; } else { _has_bits_[0] &= ~0x00000020u; } - swift_prefix_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), swift_prefix, + swift_prefix_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), swift_prefix, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.swift_prefix) } -inline ::std::string* FileOptions::unsafe_arena_release_swift_prefix() { +inline std::string* FileOptions::unsafe_arena_release_swift_prefix() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.swift_prefix) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000020u; - return swift_prefix_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return swift_prefix_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_swift_prefix( - ::std::string* swift_prefix) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (swift_prefix != NULL) { + std::string* swift_prefix) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (swift_prefix != nullptr) { _has_bits_[0] |= 0x00000020u; } else { _has_bits_[0] &= ~0x00000020u; } - swift_prefix_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + swift_prefix_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), swift_prefix, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.swift_prefix) } @@ -9428,79 +10046,77 @@ inline bool FileOptions::has_php_class_prefix() const { return (_has_bits_[0] & 0x00000040u) != 0; } inline void FileOptions::clear_php_class_prefix() { - php_class_prefix_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + php_class_prefix_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000040u; } -inline const ::std::string& FileOptions::php_class_prefix() const { +inline const std::string& FileOptions::php_class_prefix() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.php_class_prefix) return php_class_prefix_.Get(); } -inline void FileOptions::set_php_class_prefix(const ::std::string& value) { +inline void FileOptions::set_php_class_prefix(const std::string& value) { _has_bits_[0] |= 0x00000040u; - php_class_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + php_class_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.php_class_prefix) } -#if LANG_CXX11 -inline void FileOptions::set_php_class_prefix(::std::string&& value) { +inline void FileOptions::set_php_class_prefix(std::string&& value) { _has_bits_[0] |= 0x00000040u; php_class_prefix_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.php_class_prefix) } -#endif inline void FileOptions::set_php_class_prefix(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000040u; - php_class_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + php_class_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.php_class_prefix) } inline void FileOptions::set_php_class_prefix(const char* value, size_t size) { _has_bits_[0] |= 0x00000040u; - php_class_prefix_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + php_class_prefix_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.php_class_prefix) } -inline ::std::string* FileOptions::mutable_php_class_prefix() { +inline std::string* FileOptions::mutable_php_class_prefix() { _has_bits_[0] |= 0x00000040u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.php_class_prefix) - return php_class_prefix_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return php_class_prefix_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_php_class_prefix() { +inline std::string* FileOptions::release_php_class_prefix() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.php_class_prefix) if (!has_php_class_prefix()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000040u; - return php_class_prefix_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return php_class_prefix_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_php_class_prefix(::std::string* php_class_prefix) { - if (php_class_prefix != NULL) { +inline void FileOptions::set_allocated_php_class_prefix(std::string* php_class_prefix) { + if (php_class_prefix != nullptr) { _has_bits_[0] |= 0x00000040u; } else { _has_bits_[0] &= ~0x00000040u; } - php_class_prefix_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), php_class_prefix, + php_class_prefix_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), php_class_prefix, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.php_class_prefix) } -inline ::std::string* FileOptions::unsafe_arena_release_php_class_prefix() { +inline std::string* FileOptions::unsafe_arena_release_php_class_prefix() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.php_class_prefix) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000040u; - return php_class_prefix_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return php_class_prefix_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_php_class_prefix( - ::std::string* php_class_prefix) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (php_class_prefix != NULL) { + std::string* php_class_prefix) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (php_class_prefix != nullptr) { _has_bits_[0] |= 0x00000040u; } else { _has_bits_[0] &= ~0x00000040u; } - php_class_prefix_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + php_class_prefix_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), php_class_prefix, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.php_class_prefix) } @@ -9510,79 +10126,77 @@ inline bool FileOptions::has_php_namespace() const { return (_has_bits_[0] & 0x00000080u) != 0; } inline void FileOptions::clear_php_namespace() { - php_namespace_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + php_namespace_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000080u; } -inline const ::std::string& FileOptions::php_namespace() const { +inline const std::string& FileOptions::php_namespace() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.php_namespace) return php_namespace_.Get(); } -inline void FileOptions::set_php_namespace(const ::std::string& value) { +inline void FileOptions::set_php_namespace(const std::string& value) { _has_bits_[0] |= 0x00000080u; - php_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + php_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.php_namespace) } -#if LANG_CXX11 -inline void FileOptions::set_php_namespace(::std::string&& value) { +inline void FileOptions::set_php_namespace(std::string&& value) { _has_bits_[0] |= 0x00000080u; php_namespace_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.php_namespace) } -#endif inline void FileOptions::set_php_namespace(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000080u; - php_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + php_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.php_namespace) } inline void FileOptions::set_php_namespace(const char* value, size_t size) { _has_bits_[0] |= 0x00000080u; - php_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + php_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.php_namespace) } -inline ::std::string* FileOptions::mutable_php_namespace() { +inline std::string* FileOptions::mutable_php_namespace() { _has_bits_[0] |= 0x00000080u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.php_namespace) - return php_namespace_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return php_namespace_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_php_namespace() { +inline std::string* FileOptions::release_php_namespace() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.php_namespace) if (!has_php_namespace()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000080u; - return php_namespace_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return php_namespace_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_php_namespace(::std::string* php_namespace) { - if (php_namespace != NULL) { +inline void FileOptions::set_allocated_php_namespace(std::string* php_namespace) { + if (php_namespace != nullptr) { _has_bits_[0] |= 0x00000080u; } else { _has_bits_[0] &= ~0x00000080u; } - php_namespace_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), php_namespace, + php_namespace_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), php_namespace, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.php_namespace) } -inline ::std::string* FileOptions::unsafe_arena_release_php_namespace() { +inline std::string* FileOptions::unsafe_arena_release_php_namespace() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.php_namespace) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000080u; - return php_namespace_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return php_namespace_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_php_namespace( - ::std::string* php_namespace) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (php_namespace != NULL) { + std::string* php_namespace) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (php_namespace != nullptr) { _has_bits_[0] |= 0x00000080u; } else { _has_bits_[0] &= ~0x00000080u; } - php_namespace_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + php_namespace_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), php_namespace, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.php_namespace) } @@ -9592,79 +10206,77 @@ inline bool FileOptions::has_php_metadata_namespace() const { return (_has_bits_[0] & 0x00000100u) != 0; } inline void FileOptions::clear_php_metadata_namespace() { - php_metadata_namespace_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + php_metadata_namespace_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000100u; } -inline const ::std::string& FileOptions::php_metadata_namespace() const { +inline const std::string& FileOptions::php_metadata_namespace() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.php_metadata_namespace) return php_metadata_namespace_.Get(); } -inline void FileOptions::set_php_metadata_namespace(const ::std::string& value) { +inline void FileOptions::set_php_metadata_namespace(const std::string& value) { _has_bits_[0] |= 0x00000100u; - php_metadata_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + php_metadata_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.php_metadata_namespace) } -#if LANG_CXX11 -inline void FileOptions::set_php_metadata_namespace(::std::string&& value) { +inline void FileOptions::set_php_metadata_namespace(std::string&& value) { _has_bits_[0] |= 0x00000100u; php_metadata_namespace_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.php_metadata_namespace) } -#endif inline void FileOptions::set_php_metadata_namespace(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000100u; - php_metadata_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + php_metadata_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.php_metadata_namespace) } inline void FileOptions::set_php_metadata_namespace(const char* value, size_t size) { _has_bits_[0] |= 0x00000100u; - php_metadata_namespace_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + php_metadata_namespace_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.php_metadata_namespace) } -inline ::std::string* FileOptions::mutable_php_metadata_namespace() { +inline std::string* FileOptions::mutable_php_metadata_namespace() { _has_bits_[0] |= 0x00000100u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.php_metadata_namespace) - return php_metadata_namespace_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return php_metadata_namespace_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_php_metadata_namespace() { +inline std::string* FileOptions::release_php_metadata_namespace() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.php_metadata_namespace) if (!has_php_metadata_namespace()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000100u; - return php_metadata_namespace_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return php_metadata_namespace_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_php_metadata_namespace(::std::string* php_metadata_namespace) { - if (php_metadata_namespace != NULL) { +inline void FileOptions::set_allocated_php_metadata_namespace(std::string* php_metadata_namespace) { + if (php_metadata_namespace != nullptr) { _has_bits_[0] |= 0x00000100u; } else { _has_bits_[0] &= ~0x00000100u; } - php_metadata_namespace_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), php_metadata_namespace, + php_metadata_namespace_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), php_metadata_namespace, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.php_metadata_namespace) } -inline ::std::string* FileOptions::unsafe_arena_release_php_metadata_namespace() { +inline std::string* FileOptions::unsafe_arena_release_php_metadata_namespace() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.php_metadata_namespace) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000100u; - return php_metadata_namespace_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return php_metadata_namespace_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_php_metadata_namespace( - ::std::string* php_metadata_namespace) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (php_metadata_namespace != NULL) { + std::string* php_metadata_namespace) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (php_metadata_namespace != nullptr) { _has_bits_[0] |= 0x00000100u; } else { _has_bits_[0] &= ~0x00000100u; } - php_metadata_namespace_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + php_metadata_namespace_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), php_metadata_namespace, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.php_metadata_namespace) } @@ -9674,79 +10286,77 @@ inline bool FileOptions::has_ruby_package() const { return (_has_bits_[0] & 0x00000200u) != 0; } inline void FileOptions::clear_ruby_package() { - ruby_package_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + ruby_package_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000200u; } -inline const ::std::string& FileOptions::ruby_package() const { +inline const std::string& FileOptions::ruby_package() const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.ruby_package) return ruby_package_.Get(); } -inline void FileOptions::set_ruby_package(const ::std::string& value) { +inline void FileOptions::set_ruby_package(const std::string& value) { _has_bits_[0] |= 0x00000200u; - ruby_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + ruby_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.ruby_package) } -#if LANG_CXX11 -inline void FileOptions::set_ruby_package(::std::string&& value) { +inline void FileOptions::set_ruby_package(std::string&& value) { _has_bits_[0] |= 0x00000200u; ruby_package_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.FileOptions.ruby_package) } -#endif inline void FileOptions::set_ruby_package(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000200u; - ruby_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + ruby_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.ruby_package) } inline void FileOptions::set_ruby_package(const char* value, size_t size) { _has_bits_[0] |= 0x00000200u; - ruby_package_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + ruby_package_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.ruby_package) } -inline ::std::string* FileOptions::mutable_ruby_package() { +inline std::string* FileOptions::mutable_ruby_package() { _has_bits_[0] |= 0x00000200u; // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.ruby_package) - return ruby_package_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return ruby_package_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* FileOptions::release_ruby_package() { +inline std::string* FileOptions::release_ruby_package() { // @@protoc_insertion_point(field_release:google.protobuf.FileOptions.ruby_package) if (!has_ruby_package()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000200u; - return ruby_package_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return ruby_package_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void FileOptions::set_allocated_ruby_package(::std::string* ruby_package) { - if (ruby_package != NULL) { +inline void FileOptions::set_allocated_ruby_package(std::string* ruby_package) { + if (ruby_package != nullptr) { _has_bits_[0] |= 0x00000200u; } else { _has_bits_[0] &= ~0x00000200u; } - ruby_package_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ruby_package, + ruby_package_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ruby_package, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.ruby_package) } -inline ::std::string* FileOptions::unsafe_arena_release_ruby_package() { +inline std::string* FileOptions::unsafe_arena_release_ruby_package() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.FileOptions.ruby_package) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000200u; - return ruby_package_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return ruby_package_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void FileOptions::unsafe_arena_set_allocated_ruby_package( - ::std::string* ruby_package) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (ruby_package != NULL) { + std::string* ruby_package) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (ruby_package != nullptr) { _has_bits_[0] |= 0x00000200u; } else { _has_bits_[0] &= ~0x00000200u; } - ruby_package_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + ruby_package_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ruby_package, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.FileOptions.ruby_package) } @@ -9758,24 +10368,24 @@ inline int FileOptions::uninterpreted_option_size() const { inline void FileOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* FileOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* FileOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* FileOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FileOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& FileOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& FileOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* FileOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* FileOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.FileOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& FileOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.FileOptions.uninterpreted_option) return uninterpreted_option_; @@ -9864,24 +10474,24 @@ inline int MessageOptions::uninterpreted_option_size() const { inline void MessageOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* MessageOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* MessageOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.MessageOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* MessageOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.MessageOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& MessageOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& MessageOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.MessageOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* MessageOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* MessageOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.MessageOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& MessageOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.MessageOptions.uninterpreted_option) return uninterpreted_option_; @@ -9899,12 +10509,12 @@ inline void FieldOptions::clear_ctype() { ctype_ = 0; _has_bits_[0] &= ~0x00000001u; } -inline ::google::protobuf::FieldOptions_CType FieldOptions::ctype() const { +inline PROTOBUF_NAMESPACE_ID::FieldOptions_CType FieldOptions::ctype() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldOptions.ctype) - return static_cast< ::google::protobuf::FieldOptions_CType >(ctype_); + return static_cast< PROTOBUF_NAMESPACE_ID::FieldOptions_CType >(ctype_); } -inline void FieldOptions::set_ctype(::google::protobuf::FieldOptions_CType value) { - assert(::google::protobuf::FieldOptions_CType_IsValid(value)); +inline void FieldOptions::set_ctype(PROTOBUF_NAMESPACE_ID::FieldOptions_CType value) { + assert(PROTOBUF_NAMESPACE_ID::FieldOptions_CType_IsValid(value)); _has_bits_[0] |= 0x00000001u; ctype_ = value; // @@protoc_insertion_point(field_set:google.protobuf.FieldOptions.ctype) @@ -9936,12 +10546,12 @@ inline void FieldOptions::clear_jstype() { jstype_ = 0; _has_bits_[0] &= ~0x00000020u; } -inline ::google::protobuf::FieldOptions_JSType FieldOptions::jstype() const { +inline PROTOBUF_NAMESPACE_ID::FieldOptions_JSType FieldOptions::jstype() const { // @@protoc_insertion_point(field_get:google.protobuf.FieldOptions.jstype) - return static_cast< ::google::protobuf::FieldOptions_JSType >(jstype_); + return static_cast< PROTOBUF_NAMESPACE_ID::FieldOptions_JSType >(jstype_); } -inline void FieldOptions::set_jstype(::google::protobuf::FieldOptions_JSType value) { - assert(::google::protobuf::FieldOptions_JSType_IsValid(value)); +inline void FieldOptions::set_jstype(PROTOBUF_NAMESPACE_ID::FieldOptions_JSType value) { + assert(PROTOBUF_NAMESPACE_ID::FieldOptions_JSType_IsValid(value)); _has_bits_[0] |= 0x00000020u; jstype_ = value; // @@protoc_insertion_point(field_set:google.protobuf.FieldOptions.jstype) @@ -10008,24 +10618,24 @@ inline int FieldOptions::uninterpreted_option_size() const { inline void FieldOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* FieldOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* FieldOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FieldOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* FieldOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FieldOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& FieldOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& FieldOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FieldOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* FieldOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* FieldOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.FieldOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& FieldOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.FieldOptions.uninterpreted_option) return uninterpreted_option_; @@ -10042,24 +10652,24 @@ inline int OneofOptions::uninterpreted_option_size() const { inline void OneofOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* OneofOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* OneofOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.OneofOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* OneofOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.OneofOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& OneofOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& OneofOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.OneofOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* OneofOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* OneofOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.OneofOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& OneofOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.OneofOptions.uninterpreted_option) return uninterpreted_option_; @@ -10112,24 +10722,24 @@ inline int EnumOptions::uninterpreted_option_size() const { inline void EnumOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* EnumOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* EnumOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.EnumOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* EnumOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.EnumOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& EnumOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& EnumOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.EnumOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* EnumOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* EnumOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.EnumOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& EnumOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.EnumOptions.uninterpreted_option) return uninterpreted_option_; @@ -10164,24 +10774,24 @@ inline int EnumValueOptions::uninterpreted_option_size() const { inline void EnumValueOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* EnumValueOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* EnumValueOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.EnumValueOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* EnumValueOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.EnumValueOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& EnumValueOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& EnumValueOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.EnumValueOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* EnumValueOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* EnumValueOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.EnumValueOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& EnumValueOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.EnumValueOptions.uninterpreted_option) return uninterpreted_option_; @@ -10216,24 +10826,24 @@ inline int ServiceOptions::uninterpreted_option_size() const { inline void ServiceOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* ServiceOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* ServiceOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.ServiceOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* ServiceOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.ServiceOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& ServiceOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& ServiceOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.ServiceOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* ServiceOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* ServiceOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.ServiceOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& ServiceOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.ServiceOptions.uninterpreted_option) return uninterpreted_option_; @@ -10269,12 +10879,12 @@ inline void MethodOptions::clear_idempotency_level() { idempotency_level_ = 0; _has_bits_[0] &= ~0x00000002u; } -inline ::google::protobuf::MethodOptions_IdempotencyLevel MethodOptions::idempotency_level() const { +inline PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel MethodOptions::idempotency_level() const { // @@protoc_insertion_point(field_get:google.protobuf.MethodOptions.idempotency_level) - return static_cast< ::google::protobuf::MethodOptions_IdempotencyLevel >(idempotency_level_); + return static_cast< PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel >(idempotency_level_); } -inline void MethodOptions::set_idempotency_level(::google::protobuf::MethodOptions_IdempotencyLevel value) { - assert(::google::protobuf::MethodOptions_IdempotencyLevel_IsValid(value)); +inline void MethodOptions::set_idempotency_level(PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel value) { + assert(PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel_IsValid(value)); _has_bits_[0] |= 0x00000002u; idempotency_level_ = value; // @@protoc_insertion_point(field_set:google.protobuf.MethodOptions.idempotency_level) @@ -10287,24 +10897,24 @@ inline int MethodOptions::uninterpreted_option_size() const { inline void MethodOptions::clear_uninterpreted_option() { uninterpreted_option_.Clear(); } -inline ::google::protobuf::UninterpretedOption* MethodOptions::mutable_uninterpreted_option(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* MethodOptions::mutable_uninterpreted_option(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.MethodOptions.uninterpreted_option) return uninterpreted_option_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >* MethodOptions::mutable_uninterpreted_option() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.MethodOptions.uninterpreted_option) return &uninterpreted_option_; } -inline const ::google::protobuf::UninterpretedOption& MethodOptions::uninterpreted_option(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption& MethodOptions::uninterpreted_option(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.MethodOptions.uninterpreted_option) return uninterpreted_option_.Get(index); } -inline ::google::protobuf::UninterpretedOption* MethodOptions::add_uninterpreted_option() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption* MethodOptions::add_uninterpreted_option() { // @@protoc_insertion_point(field_add:google.protobuf.MethodOptions.uninterpreted_option) return uninterpreted_option_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption >& MethodOptions::uninterpreted_option() const { // @@protoc_insertion_point(field_list:google.protobuf.MethodOptions.uninterpreted_option) return uninterpreted_option_; @@ -10319,79 +10929,77 @@ inline bool UninterpretedOption_NamePart::has_name_part() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void UninterpretedOption_NamePart::clear_name_part() { - name_part_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + name_part_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& UninterpretedOption_NamePart::name_part() const { +inline const std::string& UninterpretedOption_NamePart::name_part() const { // @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.NamePart.name_part) return name_part_.Get(); } -inline void UninterpretedOption_NamePart::set_name_part(const ::std::string& value) { +inline void UninterpretedOption_NamePart::set_name_part(const std::string& value) { _has_bits_[0] |= 0x00000001u; - name_part_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + name_part_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.NamePart.name_part) } -#if LANG_CXX11 -inline void UninterpretedOption_NamePart::set_name_part(::std::string&& value) { +inline void UninterpretedOption_NamePart::set_name_part(std::string&& value) { _has_bits_[0] |= 0x00000001u; name_part_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.UninterpretedOption.NamePart.name_part) } -#endif inline void UninterpretedOption_NamePart::set_name_part(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - name_part_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + name_part_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.UninterpretedOption.NamePart.name_part) } inline void UninterpretedOption_NamePart::set_name_part(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - name_part_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + name_part_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.UninterpretedOption.NamePart.name_part) } -inline ::std::string* UninterpretedOption_NamePart::mutable_name_part() { +inline std::string* UninterpretedOption_NamePart::mutable_name_part() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.NamePart.name_part) - return name_part_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_part_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* UninterpretedOption_NamePart::release_name_part() { +inline std::string* UninterpretedOption_NamePart::release_name_part() { // @@protoc_insertion_point(field_release:google.protobuf.UninterpretedOption.NamePart.name_part) if (!has_name_part()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return name_part_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return name_part_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void UninterpretedOption_NamePart::set_allocated_name_part(::std::string* name_part) { - if (name_part != NULL) { +inline void UninterpretedOption_NamePart::set_allocated_name_part(std::string* name_part) { + if (name_part != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_part_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name_part, + name_part_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name_part, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.UninterpretedOption.NamePart.name_part) } -inline ::std::string* UninterpretedOption_NamePart::unsafe_arena_release_name_part() { +inline std::string* UninterpretedOption_NamePart::unsafe_arena_release_name_part() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.UninterpretedOption.NamePart.name_part) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return name_part_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return name_part_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void UninterpretedOption_NamePart::unsafe_arena_set_allocated_name_part( - ::std::string* name_part) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (name_part != NULL) { + std::string* name_part) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (name_part != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - name_part_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + name_part_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name_part, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.UninterpretedOption.NamePart.name_part) } @@ -10425,24 +11033,24 @@ inline int UninterpretedOption::name_size() const { inline void UninterpretedOption::clear_name() { name_.Clear(); } -inline ::google::protobuf::UninterpretedOption_NamePart* UninterpretedOption::mutable_name(int index) { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart* UninterpretedOption::mutable_name(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.name) return name_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption_NamePart >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart >* UninterpretedOption::mutable_name() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.UninterpretedOption.name) return &name_; } -inline const ::google::protobuf::UninterpretedOption_NamePart& UninterpretedOption::name(int index) const { +inline const PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart& UninterpretedOption::name(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.name) return name_.Get(index); } -inline ::google::protobuf::UninterpretedOption_NamePart* UninterpretedOption::add_name() { +inline PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart* UninterpretedOption::add_name() { // @@protoc_insertion_point(field_add:google.protobuf.UninterpretedOption.name) return name_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption_NamePart >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::UninterpretedOption_NamePart >& UninterpretedOption::name() const { // @@protoc_insertion_point(field_list:google.protobuf.UninterpretedOption.name) return name_; @@ -10453,79 +11061,77 @@ inline bool UninterpretedOption::has_identifier_value() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void UninterpretedOption::clear_identifier_value() { - identifier_value_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + identifier_value_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& UninterpretedOption::identifier_value() const { +inline const std::string& UninterpretedOption::identifier_value() const { // @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.identifier_value) return identifier_value_.Get(); } -inline void UninterpretedOption::set_identifier_value(const ::std::string& value) { +inline void UninterpretedOption::set_identifier_value(const std::string& value) { _has_bits_[0] |= 0x00000001u; - identifier_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + identifier_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.identifier_value) } -#if LANG_CXX11 -inline void UninterpretedOption::set_identifier_value(::std::string&& value) { +inline void UninterpretedOption::set_identifier_value(std::string&& value) { _has_bits_[0] |= 0x00000001u; identifier_value_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.UninterpretedOption.identifier_value) } -#endif inline void UninterpretedOption::set_identifier_value(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - identifier_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + identifier_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.UninterpretedOption.identifier_value) } inline void UninterpretedOption::set_identifier_value(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - identifier_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + identifier_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.UninterpretedOption.identifier_value) } -inline ::std::string* UninterpretedOption::mutable_identifier_value() { +inline std::string* UninterpretedOption::mutable_identifier_value() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.identifier_value) - return identifier_value_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return identifier_value_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* UninterpretedOption::release_identifier_value() { +inline std::string* UninterpretedOption::release_identifier_value() { // @@protoc_insertion_point(field_release:google.protobuf.UninterpretedOption.identifier_value) if (!has_identifier_value()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return identifier_value_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return identifier_value_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void UninterpretedOption::set_allocated_identifier_value(::std::string* identifier_value) { - if (identifier_value != NULL) { +inline void UninterpretedOption::set_allocated_identifier_value(std::string* identifier_value) { + if (identifier_value != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - identifier_value_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), identifier_value, + identifier_value_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), identifier_value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.UninterpretedOption.identifier_value) } -inline ::std::string* UninterpretedOption::unsafe_arena_release_identifier_value() { +inline std::string* UninterpretedOption::unsafe_arena_release_identifier_value() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.UninterpretedOption.identifier_value) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return identifier_value_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return identifier_value_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void UninterpretedOption::unsafe_arena_set_allocated_identifier_value( - ::std::string* identifier_value) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (identifier_value != NULL) { + std::string* identifier_value) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (identifier_value != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - identifier_value_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + identifier_value_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), identifier_value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.UninterpretedOption.identifier_value) } @@ -10535,14 +11141,14 @@ inline bool UninterpretedOption::has_positive_int_value() const { return (_has_bits_[0] & 0x00000008u) != 0; } inline void UninterpretedOption::clear_positive_int_value() { - positive_int_value_ = GOOGLE_PROTOBUF_ULONGLONG(0); + positive_int_value_ = PROTOBUF_ULONGLONG(0); _has_bits_[0] &= ~0x00000008u; } -inline ::google::protobuf::uint64 UninterpretedOption::positive_int_value() const { +inline ::PROTOBUF_NAMESPACE_ID::uint64 UninterpretedOption::positive_int_value() const { // @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.positive_int_value) return positive_int_value_; } -inline void UninterpretedOption::set_positive_int_value(::google::protobuf::uint64 value) { +inline void UninterpretedOption::set_positive_int_value(::PROTOBUF_NAMESPACE_ID::uint64 value) { _has_bits_[0] |= 0x00000008u; positive_int_value_ = value; // @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.positive_int_value) @@ -10553,14 +11159,14 @@ inline bool UninterpretedOption::has_negative_int_value() const { return (_has_bits_[0] & 0x00000010u) != 0; } inline void UninterpretedOption::clear_negative_int_value() { - negative_int_value_ = GOOGLE_PROTOBUF_LONGLONG(0); + negative_int_value_ = PROTOBUF_LONGLONG(0); _has_bits_[0] &= ~0x00000010u; } -inline ::google::protobuf::int64 UninterpretedOption::negative_int_value() const { +inline ::PROTOBUF_NAMESPACE_ID::int64 UninterpretedOption::negative_int_value() const { // @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.negative_int_value) return negative_int_value_; } -inline void UninterpretedOption::set_negative_int_value(::google::protobuf::int64 value) { +inline void UninterpretedOption::set_negative_int_value(::PROTOBUF_NAMESPACE_ID::int64 value) { _has_bits_[0] |= 0x00000010u; negative_int_value_ = value; // @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.negative_int_value) @@ -10589,79 +11195,77 @@ inline bool UninterpretedOption::has_string_value() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void UninterpretedOption::clear_string_value() { - string_value_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + string_value_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000002u; } -inline const ::std::string& UninterpretedOption::string_value() const { +inline const std::string& UninterpretedOption::string_value() const { // @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.string_value) return string_value_.Get(); } -inline void UninterpretedOption::set_string_value(const ::std::string& value) { +inline void UninterpretedOption::set_string_value(const std::string& value) { _has_bits_[0] |= 0x00000002u; - string_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + string_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.string_value) } -#if LANG_CXX11 -inline void UninterpretedOption::set_string_value(::std::string&& value) { +inline void UninterpretedOption::set_string_value(std::string&& value) { _has_bits_[0] |= 0x00000002u; string_value_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.UninterpretedOption.string_value) } -#endif inline void UninterpretedOption::set_string_value(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000002u; - string_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + string_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.UninterpretedOption.string_value) } inline void UninterpretedOption::set_string_value(const void* value, size_t size) { _has_bits_[0] |= 0x00000002u; - string_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + string_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.UninterpretedOption.string_value) } -inline ::std::string* UninterpretedOption::mutable_string_value() { +inline std::string* UninterpretedOption::mutable_string_value() { _has_bits_[0] |= 0x00000002u; // @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.string_value) - return string_value_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return string_value_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* UninterpretedOption::release_string_value() { +inline std::string* UninterpretedOption::release_string_value() { // @@protoc_insertion_point(field_release:google.protobuf.UninterpretedOption.string_value) if (!has_string_value()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000002u; - return string_value_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return string_value_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void UninterpretedOption::set_allocated_string_value(::std::string* string_value) { - if (string_value != NULL) { +inline void UninterpretedOption::set_allocated_string_value(std::string* string_value) { + if (string_value != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - string_value_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), string_value, + string_value_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), string_value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.UninterpretedOption.string_value) } -inline ::std::string* UninterpretedOption::unsafe_arena_release_string_value() { +inline std::string* UninterpretedOption::unsafe_arena_release_string_value() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.UninterpretedOption.string_value) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000002u; - return string_value_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return string_value_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void UninterpretedOption::unsafe_arena_set_allocated_string_value( - ::std::string* string_value) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (string_value != NULL) { + std::string* string_value) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (string_value != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - string_value_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + string_value_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), string_value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.UninterpretedOption.string_value) } @@ -10671,79 +11275,77 @@ inline bool UninterpretedOption::has_aggregate_value() const { return (_has_bits_[0] & 0x00000004u) != 0; } inline void UninterpretedOption::clear_aggregate_value() { - aggregate_value_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + aggregate_value_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000004u; } -inline const ::std::string& UninterpretedOption::aggregate_value() const { +inline const std::string& UninterpretedOption::aggregate_value() const { // @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.aggregate_value) return aggregate_value_.Get(); } -inline void UninterpretedOption::set_aggregate_value(const ::std::string& value) { +inline void UninterpretedOption::set_aggregate_value(const std::string& value) { _has_bits_[0] |= 0x00000004u; - aggregate_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + aggregate_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.aggregate_value) } -#if LANG_CXX11 -inline void UninterpretedOption::set_aggregate_value(::std::string&& value) { +inline void UninterpretedOption::set_aggregate_value(std::string&& value) { _has_bits_[0] |= 0x00000004u; aggregate_value_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.UninterpretedOption.aggregate_value) } -#endif inline void UninterpretedOption::set_aggregate_value(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000004u; - aggregate_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + aggregate_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.UninterpretedOption.aggregate_value) } inline void UninterpretedOption::set_aggregate_value(const char* value, size_t size) { _has_bits_[0] |= 0x00000004u; - aggregate_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + aggregate_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.UninterpretedOption.aggregate_value) } -inline ::std::string* UninterpretedOption::mutable_aggregate_value() { +inline std::string* UninterpretedOption::mutable_aggregate_value() { _has_bits_[0] |= 0x00000004u; // @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.aggregate_value) - return aggregate_value_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return aggregate_value_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* UninterpretedOption::release_aggregate_value() { +inline std::string* UninterpretedOption::release_aggregate_value() { // @@protoc_insertion_point(field_release:google.protobuf.UninterpretedOption.aggregate_value) if (!has_aggregate_value()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000004u; - return aggregate_value_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return aggregate_value_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void UninterpretedOption::set_allocated_aggregate_value(::std::string* aggregate_value) { - if (aggregate_value != NULL) { +inline void UninterpretedOption::set_allocated_aggregate_value(std::string* aggregate_value) { + if (aggregate_value != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - aggregate_value_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), aggregate_value, + aggregate_value_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), aggregate_value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.UninterpretedOption.aggregate_value) } -inline ::std::string* UninterpretedOption::unsafe_arena_release_aggregate_value() { +inline std::string* UninterpretedOption::unsafe_arena_release_aggregate_value() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.UninterpretedOption.aggregate_value) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000004u; - return aggregate_value_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return aggregate_value_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void UninterpretedOption::unsafe_arena_set_allocated_aggregate_value( - ::std::string* aggregate_value) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (aggregate_value != NULL) { + std::string* aggregate_value) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (aggregate_value != nullptr) { _has_bits_[0] |= 0x00000004u; } else { _has_bits_[0] &= ~0x00000004u; } - aggregate_value_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + aggregate_value_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), aggregate_value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.UninterpretedOption.aggregate_value) } @@ -10759,24 +11361,24 @@ inline int SourceCodeInfo_Location::path_size() const { inline void SourceCodeInfo_Location::clear_path() { path_.Clear(); } -inline ::google::protobuf::int32 SourceCodeInfo_Location::path(int index) const { +inline ::PROTOBUF_NAMESPACE_ID::int32 SourceCodeInfo_Location::path(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.SourceCodeInfo.Location.path) return path_.Get(index); } -inline void SourceCodeInfo_Location::set_path(int index, ::google::protobuf::int32 value) { +inline void SourceCodeInfo_Location::set_path(int index, ::PROTOBUF_NAMESPACE_ID::int32 value) { path_.Set(index, value); // @@protoc_insertion_point(field_set:google.protobuf.SourceCodeInfo.Location.path) } -inline void SourceCodeInfo_Location::add_path(::google::protobuf::int32 value) { +inline void SourceCodeInfo_Location::add_path(::PROTOBUF_NAMESPACE_ID::int32 value) { path_.Add(value); // @@protoc_insertion_point(field_add:google.protobuf.SourceCodeInfo.Location.path) } -inline const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& SourceCodeInfo_Location::path() const { // @@protoc_insertion_point(field_list:google.protobuf.SourceCodeInfo.Location.path) return path_; } -inline ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* SourceCodeInfo_Location::mutable_path() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.SourceCodeInfo.Location.path) return &path_; @@ -10789,24 +11391,24 @@ inline int SourceCodeInfo_Location::span_size() const { inline void SourceCodeInfo_Location::clear_span() { span_.Clear(); } -inline ::google::protobuf::int32 SourceCodeInfo_Location::span(int index) const { +inline ::PROTOBUF_NAMESPACE_ID::int32 SourceCodeInfo_Location::span(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.SourceCodeInfo.Location.span) return span_.Get(index); } -inline void SourceCodeInfo_Location::set_span(int index, ::google::protobuf::int32 value) { +inline void SourceCodeInfo_Location::set_span(int index, ::PROTOBUF_NAMESPACE_ID::int32 value) { span_.Set(index, value); // @@protoc_insertion_point(field_set:google.protobuf.SourceCodeInfo.Location.span) } -inline void SourceCodeInfo_Location::add_span(::google::protobuf::int32 value) { +inline void SourceCodeInfo_Location::add_span(::PROTOBUF_NAMESPACE_ID::int32 value) { span_.Add(value); // @@protoc_insertion_point(field_add:google.protobuf.SourceCodeInfo.Location.span) } -inline const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& SourceCodeInfo_Location::span() const { // @@protoc_insertion_point(field_list:google.protobuf.SourceCodeInfo.Location.span) return span_; } -inline ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* SourceCodeInfo_Location::mutable_span() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.SourceCodeInfo.Location.span) return &span_; @@ -10817,79 +11419,77 @@ inline bool SourceCodeInfo_Location::has_leading_comments() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void SourceCodeInfo_Location::clear_leading_comments() { - leading_comments_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + leading_comments_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& SourceCodeInfo_Location::leading_comments() const { +inline const std::string& SourceCodeInfo_Location::leading_comments() const { // @@protoc_insertion_point(field_get:google.protobuf.SourceCodeInfo.Location.leading_comments) return leading_comments_.Get(); } -inline void SourceCodeInfo_Location::set_leading_comments(const ::std::string& value) { +inline void SourceCodeInfo_Location::set_leading_comments(const std::string& value) { _has_bits_[0] |= 0x00000001u; - leading_comments_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + leading_comments_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.SourceCodeInfo.Location.leading_comments) } -#if LANG_CXX11 -inline void SourceCodeInfo_Location::set_leading_comments(::std::string&& value) { +inline void SourceCodeInfo_Location::set_leading_comments(std::string&& value) { _has_bits_[0] |= 0x00000001u; leading_comments_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.SourceCodeInfo.Location.leading_comments) } -#endif inline void SourceCodeInfo_Location::set_leading_comments(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - leading_comments_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + leading_comments_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.SourceCodeInfo.Location.leading_comments) } inline void SourceCodeInfo_Location::set_leading_comments(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - leading_comments_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + leading_comments_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.SourceCodeInfo.Location.leading_comments) } -inline ::std::string* SourceCodeInfo_Location::mutable_leading_comments() { +inline std::string* SourceCodeInfo_Location::mutable_leading_comments() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.SourceCodeInfo.Location.leading_comments) - return leading_comments_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return leading_comments_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* SourceCodeInfo_Location::release_leading_comments() { +inline std::string* SourceCodeInfo_Location::release_leading_comments() { // @@protoc_insertion_point(field_release:google.protobuf.SourceCodeInfo.Location.leading_comments) if (!has_leading_comments()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return leading_comments_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return leading_comments_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void SourceCodeInfo_Location::set_allocated_leading_comments(::std::string* leading_comments) { - if (leading_comments != NULL) { +inline void SourceCodeInfo_Location::set_allocated_leading_comments(std::string* leading_comments) { + if (leading_comments != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - leading_comments_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), leading_comments, + leading_comments_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), leading_comments, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.SourceCodeInfo.Location.leading_comments) } -inline ::std::string* SourceCodeInfo_Location::unsafe_arena_release_leading_comments() { +inline std::string* SourceCodeInfo_Location::unsafe_arena_release_leading_comments() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.SourceCodeInfo.Location.leading_comments) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return leading_comments_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return leading_comments_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void SourceCodeInfo_Location::unsafe_arena_set_allocated_leading_comments( - ::std::string* leading_comments) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (leading_comments != NULL) { + std::string* leading_comments) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (leading_comments != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - leading_comments_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + leading_comments_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), leading_comments, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.SourceCodeInfo.Location.leading_comments) } @@ -10899,79 +11499,77 @@ inline bool SourceCodeInfo_Location::has_trailing_comments() const { return (_has_bits_[0] & 0x00000002u) != 0; } inline void SourceCodeInfo_Location::clear_trailing_comments() { - trailing_comments_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + trailing_comments_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000002u; } -inline const ::std::string& SourceCodeInfo_Location::trailing_comments() const { +inline const std::string& SourceCodeInfo_Location::trailing_comments() const { // @@protoc_insertion_point(field_get:google.protobuf.SourceCodeInfo.Location.trailing_comments) return trailing_comments_.Get(); } -inline void SourceCodeInfo_Location::set_trailing_comments(const ::std::string& value) { +inline void SourceCodeInfo_Location::set_trailing_comments(const std::string& value) { _has_bits_[0] |= 0x00000002u; - trailing_comments_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + trailing_comments_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.SourceCodeInfo.Location.trailing_comments) } -#if LANG_CXX11 -inline void SourceCodeInfo_Location::set_trailing_comments(::std::string&& value) { +inline void SourceCodeInfo_Location::set_trailing_comments(std::string&& value) { _has_bits_[0] |= 0x00000002u; trailing_comments_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.SourceCodeInfo.Location.trailing_comments) } -#endif inline void SourceCodeInfo_Location::set_trailing_comments(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000002u; - trailing_comments_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + trailing_comments_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.SourceCodeInfo.Location.trailing_comments) } inline void SourceCodeInfo_Location::set_trailing_comments(const char* value, size_t size) { _has_bits_[0] |= 0x00000002u; - trailing_comments_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + trailing_comments_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.SourceCodeInfo.Location.trailing_comments) } -inline ::std::string* SourceCodeInfo_Location::mutable_trailing_comments() { +inline std::string* SourceCodeInfo_Location::mutable_trailing_comments() { _has_bits_[0] |= 0x00000002u; // @@protoc_insertion_point(field_mutable:google.protobuf.SourceCodeInfo.Location.trailing_comments) - return trailing_comments_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return trailing_comments_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* SourceCodeInfo_Location::release_trailing_comments() { +inline std::string* SourceCodeInfo_Location::release_trailing_comments() { // @@protoc_insertion_point(field_release:google.protobuf.SourceCodeInfo.Location.trailing_comments) if (!has_trailing_comments()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000002u; - return trailing_comments_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return trailing_comments_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void SourceCodeInfo_Location::set_allocated_trailing_comments(::std::string* trailing_comments) { - if (trailing_comments != NULL) { +inline void SourceCodeInfo_Location::set_allocated_trailing_comments(std::string* trailing_comments) { + if (trailing_comments != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - trailing_comments_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), trailing_comments, + trailing_comments_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), trailing_comments, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.SourceCodeInfo.Location.trailing_comments) } -inline ::std::string* SourceCodeInfo_Location::unsafe_arena_release_trailing_comments() { +inline std::string* SourceCodeInfo_Location::unsafe_arena_release_trailing_comments() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.SourceCodeInfo.Location.trailing_comments) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000002u; - return trailing_comments_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return trailing_comments_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void SourceCodeInfo_Location::unsafe_arena_set_allocated_trailing_comments( - ::std::string* trailing_comments) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (trailing_comments != NULL) { + std::string* trailing_comments) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (trailing_comments != nullptr) { _has_bits_[0] |= 0x00000002u; } else { _has_bits_[0] &= ~0x00000002u; } - trailing_comments_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + trailing_comments_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), trailing_comments, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.SourceCodeInfo.Location.trailing_comments) } @@ -10983,26 +11581,24 @@ inline int SourceCodeInfo_Location::leading_detached_comments_size() const { inline void SourceCodeInfo_Location::clear_leading_detached_comments() { leading_detached_comments_.Clear(); } -inline const ::std::string& SourceCodeInfo_Location::leading_detached_comments(int index) const { +inline const std::string& SourceCodeInfo_Location::leading_detached_comments(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) return leading_detached_comments_.Get(index); } -inline ::std::string* SourceCodeInfo_Location::mutable_leading_detached_comments(int index) { +inline std::string* SourceCodeInfo_Location::mutable_leading_detached_comments(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) return leading_detached_comments_.Mutable(index); } -inline void SourceCodeInfo_Location::set_leading_detached_comments(int index, const ::std::string& value) { +inline void SourceCodeInfo_Location::set_leading_detached_comments(int index, const std::string& value) { // @@protoc_insertion_point(field_set:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) leading_detached_comments_.Mutable(index)->assign(value); } -#if LANG_CXX11 -inline void SourceCodeInfo_Location::set_leading_detached_comments(int index, ::std::string&& value) { +inline void SourceCodeInfo_Location::set_leading_detached_comments(int index, std::string&& value) { // @@protoc_insertion_point(field_set:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) leading_detached_comments_.Mutable(index)->assign(std::move(value)); } -#endif inline void SourceCodeInfo_Location::set_leading_detached_comments(int index, const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); leading_detached_comments_.Mutable(index)->assign(value); // @@protoc_insertion_point(field_set_char:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) } @@ -11011,22 +11607,20 @@ inline void SourceCodeInfo_Location::set_leading_detached_comments(int index, co reinterpret_cast(value), size); // @@protoc_insertion_point(field_set_pointer:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) } -inline ::std::string* SourceCodeInfo_Location::add_leading_detached_comments() { +inline std::string* SourceCodeInfo_Location::add_leading_detached_comments() { // @@protoc_insertion_point(field_add_mutable:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) return leading_detached_comments_.Add(); } -inline void SourceCodeInfo_Location::add_leading_detached_comments(const ::std::string& value) { +inline void SourceCodeInfo_Location::add_leading_detached_comments(const std::string& value) { leading_detached_comments_.Add()->assign(value); // @@protoc_insertion_point(field_add:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) } -#if LANG_CXX11 -inline void SourceCodeInfo_Location::add_leading_detached_comments(::std::string&& value) { +inline void SourceCodeInfo_Location::add_leading_detached_comments(std::string&& value) { leading_detached_comments_.Add(std::move(value)); // @@protoc_insertion_point(field_add:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) } -#endif inline void SourceCodeInfo_Location::add_leading_detached_comments(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); leading_detached_comments_.Add()->assign(value); // @@protoc_insertion_point(field_add_char:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) } @@ -11034,12 +11628,12 @@ inline void SourceCodeInfo_Location::add_leading_detached_comments(const char* v leading_detached_comments_.Add()->assign(reinterpret_cast(value), size); // @@protoc_insertion_point(field_add_pointer:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) } -inline const ::google::protobuf::RepeatedPtrField<::std::string>& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& SourceCodeInfo_Location::leading_detached_comments() const { // @@protoc_insertion_point(field_list:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) return leading_detached_comments_; } -inline ::google::protobuf::RepeatedPtrField<::std::string>* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* SourceCodeInfo_Location::mutable_leading_detached_comments() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.SourceCodeInfo.Location.leading_detached_comments) return &leading_detached_comments_; @@ -11056,24 +11650,24 @@ inline int SourceCodeInfo::location_size() const { inline void SourceCodeInfo::clear_location() { location_.Clear(); } -inline ::google::protobuf::SourceCodeInfo_Location* SourceCodeInfo::mutable_location(int index) { +inline PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location* SourceCodeInfo::mutable_location(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.SourceCodeInfo.location) return location_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::SourceCodeInfo_Location >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location >* SourceCodeInfo::mutable_location() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.SourceCodeInfo.location) return &location_; } -inline const ::google::protobuf::SourceCodeInfo_Location& SourceCodeInfo::location(int index) const { +inline const PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location& SourceCodeInfo::location(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.SourceCodeInfo.location) return location_.Get(index); } -inline ::google::protobuf::SourceCodeInfo_Location* SourceCodeInfo::add_location() { +inline PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location* SourceCodeInfo::add_location() { // @@protoc_insertion_point(field_add:google.protobuf.SourceCodeInfo.location) return location_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::SourceCodeInfo_Location >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::SourceCodeInfo_Location >& SourceCodeInfo::location() const { // @@protoc_insertion_point(field_list:google.protobuf.SourceCodeInfo.location) return location_; @@ -11090,24 +11684,24 @@ inline int GeneratedCodeInfo_Annotation::path_size() const { inline void GeneratedCodeInfo_Annotation::clear_path() { path_.Clear(); } -inline ::google::protobuf::int32 GeneratedCodeInfo_Annotation::path(int index) const { +inline ::PROTOBUF_NAMESPACE_ID::int32 GeneratedCodeInfo_Annotation::path(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.GeneratedCodeInfo.Annotation.path) return path_.Get(index); } -inline void GeneratedCodeInfo_Annotation::set_path(int index, ::google::protobuf::int32 value) { +inline void GeneratedCodeInfo_Annotation::set_path(int index, ::PROTOBUF_NAMESPACE_ID::int32 value) { path_.Set(index, value); // @@protoc_insertion_point(field_set:google.protobuf.GeneratedCodeInfo.Annotation.path) } -inline void GeneratedCodeInfo_Annotation::add_path(::google::protobuf::int32 value) { +inline void GeneratedCodeInfo_Annotation::add_path(::PROTOBUF_NAMESPACE_ID::int32 value) { path_.Add(value); // @@protoc_insertion_point(field_add:google.protobuf.GeneratedCodeInfo.Annotation.path) } -inline const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >& GeneratedCodeInfo_Annotation::path() const { // @@protoc_insertion_point(field_list:google.protobuf.GeneratedCodeInfo.Annotation.path) return path_; } -inline ::google::protobuf::RepeatedField< ::google::protobuf::int32 >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< ::PROTOBUF_NAMESPACE_ID::int32 >* GeneratedCodeInfo_Annotation::mutable_path() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.GeneratedCodeInfo.Annotation.path) return &path_; @@ -11118,79 +11712,77 @@ inline bool GeneratedCodeInfo_Annotation::has_source_file() const { return (_has_bits_[0] & 0x00000001u) != 0; } inline void GeneratedCodeInfo_Annotation::clear_source_file() { - source_file_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + source_file_.ClearToEmpty(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); _has_bits_[0] &= ~0x00000001u; } -inline const ::std::string& GeneratedCodeInfo_Annotation::source_file() const { +inline const std::string& GeneratedCodeInfo_Annotation::source_file() const { // @@protoc_insertion_point(field_get:google.protobuf.GeneratedCodeInfo.Annotation.source_file) return source_file_.Get(); } -inline void GeneratedCodeInfo_Annotation::set_source_file(const ::std::string& value) { +inline void GeneratedCodeInfo_Annotation::set_source_file(const std::string& value) { _has_bits_[0] |= 0x00000001u; - source_file_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); + source_file_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.GeneratedCodeInfo.Annotation.source_file) } -#if LANG_CXX11 -inline void GeneratedCodeInfo_Annotation::set_source_file(::std::string&& value) { +inline void GeneratedCodeInfo_Annotation::set_source_file(std::string&& value) { _has_bits_[0] |= 0x00000001u; source_file_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.GeneratedCodeInfo.Annotation.source_file) } -#endif inline void GeneratedCodeInfo_Annotation::set_source_file(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); _has_bits_[0] |= 0x00000001u; - source_file_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value), + source_file_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.GeneratedCodeInfo.Annotation.source_file) } inline void GeneratedCodeInfo_Annotation::set_source_file(const char* value, size_t size) { _has_bits_[0] |= 0x00000001u; - source_file_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + source_file_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.GeneratedCodeInfo.Annotation.source_file) } -inline ::std::string* GeneratedCodeInfo_Annotation::mutable_source_file() { +inline std::string* GeneratedCodeInfo_Annotation::mutable_source_file() { _has_bits_[0] |= 0x00000001u; // @@protoc_insertion_point(field_mutable:google.protobuf.GeneratedCodeInfo.Annotation.source_file) - return source_file_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return source_file_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline ::std::string* GeneratedCodeInfo_Annotation::release_source_file() { +inline std::string* GeneratedCodeInfo_Annotation::release_source_file() { // @@protoc_insertion_point(field_release:google.protobuf.GeneratedCodeInfo.Annotation.source_file) if (!has_source_file()) { - return NULL; + return nullptr; } _has_bits_[0] &= ~0x00000001u; - return source_file_.ReleaseNonDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + return source_file_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -inline void GeneratedCodeInfo_Annotation::set_allocated_source_file(::std::string* source_file) { - if (source_file != NULL) { +inline void GeneratedCodeInfo_Annotation::set_allocated_source_file(std::string* source_file) { + if (source_file != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - source_file_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), source_file, + source_file_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), source_file, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_allocated:google.protobuf.GeneratedCodeInfo.Annotation.source_file) } -inline ::std::string* GeneratedCodeInfo_Annotation::unsafe_arena_release_source_file() { +inline std::string* GeneratedCodeInfo_Annotation::unsafe_arena_release_source_file() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.GeneratedCodeInfo.Annotation.source_file) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); _has_bits_[0] &= ~0x00000001u; - return source_file_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return source_file_.UnsafeArenaRelease(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } inline void GeneratedCodeInfo_Annotation::unsafe_arena_set_allocated_source_file( - ::std::string* source_file) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); - if (source_file != NULL) { + std::string* source_file) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); + if (source_file != nullptr) { _has_bits_[0] |= 0x00000001u; } else { _has_bits_[0] &= ~0x00000001u; } - source_file_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + source_file_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), source_file, GetArenaNoVirtual()); // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.GeneratedCodeInfo.Annotation.source_file) } @@ -11203,11 +11795,11 @@ inline void GeneratedCodeInfo_Annotation::clear_begin() { begin_ = 0; _has_bits_[0] &= ~0x00000002u; } -inline ::google::protobuf::int32 GeneratedCodeInfo_Annotation::begin() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 GeneratedCodeInfo_Annotation::begin() const { // @@protoc_insertion_point(field_get:google.protobuf.GeneratedCodeInfo.Annotation.begin) return begin_; } -inline void GeneratedCodeInfo_Annotation::set_begin(::google::protobuf::int32 value) { +inline void GeneratedCodeInfo_Annotation::set_begin(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000002u; begin_ = value; // @@protoc_insertion_point(field_set:google.protobuf.GeneratedCodeInfo.Annotation.begin) @@ -11221,11 +11813,11 @@ inline void GeneratedCodeInfo_Annotation::clear_end() { end_ = 0; _has_bits_[0] &= ~0x00000004u; } -inline ::google::protobuf::int32 GeneratedCodeInfo_Annotation::end() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 GeneratedCodeInfo_Annotation::end() const { // @@protoc_insertion_point(field_get:google.protobuf.GeneratedCodeInfo.Annotation.end) return end_; } -inline void GeneratedCodeInfo_Annotation::set_end(::google::protobuf::int32 value) { +inline void GeneratedCodeInfo_Annotation::set_end(::PROTOBUF_NAMESPACE_ID::int32 value) { _has_bits_[0] |= 0x00000004u; end_ = value; // @@protoc_insertion_point(field_set:google.protobuf.GeneratedCodeInfo.Annotation.end) @@ -11242,24 +11834,24 @@ inline int GeneratedCodeInfo::annotation_size() const { inline void GeneratedCodeInfo::clear_annotation() { annotation_.Clear(); } -inline ::google::protobuf::GeneratedCodeInfo_Annotation* GeneratedCodeInfo::mutable_annotation(int index) { +inline PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation* GeneratedCodeInfo::mutable_annotation(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.GeneratedCodeInfo.annotation) return annotation_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::GeneratedCodeInfo_Annotation >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation >* GeneratedCodeInfo::mutable_annotation() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.GeneratedCodeInfo.annotation) return &annotation_; } -inline const ::google::protobuf::GeneratedCodeInfo_Annotation& GeneratedCodeInfo::annotation(int index) const { +inline const PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation& GeneratedCodeInfo::annotation(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.GeneratedCodeInfo.annotation) return annotation_.Get(index); } -inline ::google::protobuf::GeneratedCodeInfo_Annotation* GeneratedCodeInfo::add_annotation() { +inline PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation* GeneratedCodeInfo::add_annotation() { // @@protoc_insertion_point(field_add:google.protobuf.GeneratedCodeInfo.annotation) return annotation_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::GeneratedCodeInfo_Annotation >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::GeneratedCodeInfo_Annotation >& GeneratedCodeInfo::annotation() const { // @@protoc_insertion_point(field_list:google.protobuf.GeneratedCodeInfo.annotation) return annotation_; @@ -11323,47 +11915,44 @@ GeneratedCodeInfo::annotation() const { // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_OPEN -template <> struct is_proto_enum< ::google::protobuf::FieldDescriptorProto_Type> : ::std::true_type {}; +template <> struct is_proto_enum< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type> : ::std::true_type {}; template <> -inline const EnumDescriptor* GetEnumDescriptor< ::google::protobuf::FieldDescriptorProto_Type>() { - return ::google::protobuf::FieldDescriptorProto_Type_descriptor(); +inline const EnumDescriptor* GetEnumDescriptor< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type>() { + return PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type_descriptor(); } -template <> struct is_proto_enum< ::google::protobuf::FieldDescriptorProto_Label> : ::std::true_type {}; +template <> struct is_proto_enum< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label> : ::std::true_type {}; template <> -inline const EnumDescriptor* GetEnumDescriptor< ::google::protobuf::FieldDescriptorProto_Label>() { - return ::google::protobuf::FieldDescriptorProto_Label_descriptor(); +inline const EnumDescriptor* GetEnumDescriptor< PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label>() { + return PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label_descriptor(); } -template <> struct is_proto_enum< ::google::protobuf::FileOptions_OptimizeMode> : ::std::true_type {}; +template <> struct is_proto_enum< PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode> : ::std::true_type {}; template <> -inline const EnumDescriptor* GetEnumDescriptor< ::google::protobuf::FileOptions_OptimizeMode>() { - return ::google::protobuf::FileOptions_OptimizeMode_descriptor(); +inline const EnumDescriptor* GetEnumDescriptor< PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode>() { + return PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode_descriptor(); } -template <> struct is_proto_enum< ::google::protobuf::FieldOptions_CType> : ::std::true_type {}; +template <> struct is_proto_enum< PROTOBUF_NAMESPACE_ID::FieldOptions_CType> : ::std::true_type {}; template <> -inline const EnumDescriptor* GetEnumDescriptor< ::google::protobuf::FieldOptions_CType>() { - return ::google::protobuf::FieldOptions_CType_descriptor(); +inline const EnumDescriptor* GetEnumDescriptor< PROTOBUF_NAMESPACE_ID::FieldOptions_CType>() { + return PROTOBUF_NAMESPACE_ID::FieldOptions_CType_descriptor(); } -template <> struct is_proto_enum< ::google::protobuf::FieldOptions_JSType> : ::std::true_type {}; +template <> struct is_proto_enum< PROTOBUF_NAMESPACE_ID::FieldOptions_JSType> : ::std::true_type {}; template <> -inline const EnumDescriptor* GetEnumDescriptor< ::google::protobuf::FieldOptions_JSType>() { - return ::google::protobuf::FieldOptions_JSType_descriptor(); +inline const EnumDescriptor* GetEnumDescriptor< PROTOBUF_NAMESPACE_ID::FieldOptions_JSType>() { + return PROTOBUF_NAMESPACE_ID::FieldOptions_JSType_descriptor(); } -template <> struct is_proto_enum< ::google::protobuf::MethodOptions_IdempotencyLevel> : ::std::true_type {}; +template <> struct is_proto_enum< PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel> : ::std::true_type {}; template <> -inline const EnumDescriptor* GetEnumDescriptor< ::google::protobuf::MethodOptions_IdempotencyLevel>() { - return ::google::protobuf::MethodOptions_IdempotencyLevel_descriptor(); +inline const EnumDescriptor* GetEnumDescriptor< PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel>() { + return PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel_descriptor(); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2fdescriptor_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fdescriptor_2eproto diff --git a/src/google/protobuf/descriptor.proto b/src/google/protobuf/descriptor.proto index ed08fcbc54..a2102d7aa9 100644 --- a/src/google/protobuf/descriptor.proto +++ b/src/google/protobuf/descriptor.proto @@ -40,6 +40,7 @@ syntax = "proto2"; package google.protobuf; + option go_package = "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor"; option java_package = "com.google.protobuf"; option java_outer_classname = "DescriptorProtos"; @@ -59,8 +60,8 @@ message FileDescriptorSet { // Describes a complete .proto file. message FileDescriptorProto { - optional string name = 1; // file name, relative to root of source tree - optional string package = 2; // e.g. "foo", "foo.bar", etc. + optional string name = 1; // file name, relative to root of source tree + optional string package = 2; // e.g. "foo", "foo.bar", etc. // Names of files imported by this file. repeated string dependency = 3; @@ -100,8 +101,8 @@ message DescriptorProto { repeated EnumDescriptorProto enum_type = 4; message ExtensionRange { - optional int32 start = 1; - optional int32 end = 2; + optional int32 start = 1; // Inclusive. + optional int32 end = 2; // Exclusive. optional ExtensionRangeOptions options = 3; } @@ -115,8 +116,8 @@ message DescriptorProto { // fields or extension ranges in the same message. Reserved ranges may // not overlap. message ReservedRange { - optional int32 start = 1; // Inclusive. - optional int32 end = 2; // Exclusive. + optional int32 start = 1; // Inclusive. + optional int32 end = 2; // Exclusive. } repeated ReservedRange reserved_range = 9; // Reserved field names, which may not be used by fields in the same message. @@ -137,42 +138,42 @@ message FieldDescriptorProto { enum Type { // 0 is reserved for errors. // Order is weird for historical reasons. - TYPE_DOUBLE = 1; - TYPE_FLOAT = 2; + TYPE_DOUBLE = 1; + TYPE_FLOAT = 2; // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if // negative values are likely. - TYPE_INT64 = 3; - TYPE_UINT64 = 4; + TYPE_INT64 = 3; + TYPE_UINT64 = 4; // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if // negative values are likely. - TYPE_INT32 = 5; - TYPE_FIXED64 = 6; - TYPE_FIXED32 = 7; - TYPE_BOOL = 8; - TYPE_STRING = 9; + TYPE_INT32 = 5; + TYPE_FIXED64 = 6; + TYPE_FIXED32 = 7; + TYPE_BOOL = 8; + TYPE_STRING = 9; // Tag-delimited aggregate. // Group type is deprecated and not supported in proto3. However, Proto3 // implementations should still be able to parse the group wire format and // treat group fields as unknown fields. - TYPE_GROUP = 10; - TYPE_MESSAGE = 11; // Length-delimited aggregate. + TYPE_GROUP = 10; + TYPE_MESSAGE = 11; // Length-delimited aggregate. // New in version 2. - TYPE_BYTES = 12; - TYPE_UINT32 = 13; - TYPE_ENUM = 14; - TYPE_SFIXED32 = 15; - TYPE_SFIXED64 = 16; - TYPE_SINT32 = 17; // Uses ZigZag encoding. - TYPE_SINT64 = 18; // Uses ZigZag encoding. - }; + TYPE_BYTES = 12; + TYPE_UINT32 = 13; + TYPE_ENUM = 14; + TYPE_SFIXED32 = 15; + TYPE_SFIXED64 = 16; + TYPE_SINT32 = 17; // Uses ZigZag encoding. + TYPE_SINT64 = 18; // Uses ZigZag encoding. + } enum Label { // 0 is reserved for errors - LABEL_OPTIONAL = 1; - LABEL_REQUIRED = 2; - LABEL_REPEATED = 3; - }; + LABEL_OPTIONAL = 1; + LABEL_REQUIRED = 2; + LABEL_REPEATED = 3; + } optional string name = 1; optional int32 number = 3; @@ -234,8 +235,8 @@ message EnumDescriptorProto { // is inclusive such that it can appropriately represent the entire int32 // domain. message EnumReservedRange { - optional int32 start = 1; // Inclusive. - optional int32 end = 2; // Inclusive. + optional int32 start = 1; // Inclusive. + optional int32 end = 2; // Inclusive. } // Range of reserved numeric values. Reserved numeric values may not be used @@ -276,9 +277,9 @@ message MethodDescriptorProto { optional MethodOptions options = 4; // Identifies if client streams multiple client messages - optional bool client_streaming = 5 [default=false]; + optional bool client_streaming = 5 [default = false]; // Identifies if server streams multiple server messages - optional bool server_streaming = 6 [default=false]; + optional bool server_streaming = 6 [default = false]; } @@ -314,7 +315,6 @@ message MethodDescriptorProto { // If this turns out to be popular, a web service will be set up // to automatically assign option numbers. - message FileOptions { // Sets the Java package where classes generated from this .proto will be @@ -337,7 +337,7 @@ message FileOptions { // named by java_outer_classname. However, the outer class will still be // generated to contain the file's getDescriptor() method as well as any // top-level extensions defined in the file. - optional bool java_multiple_files = 10 [default=false]; + optional bool java_multiple_files = 10 [default = false]; // This option does nothing. optional bool java_generate_equals_and_hash = 20 [deprecated=true]; @@ -348,17 +348,17 @@ message FileOptions { // Message reflection will do the same. // However, an extension field still accepts non-UTF-8 byte sequences. // This option has no effect on when used with the lite runtime. - optional bool java_string_check_utf8 = 27 [default=false]; + optional bool java_string_check_utf8 = 27 [default = false]; // Generated classes can be optimized for speed or code size. enum OptimizeMode { - SPEED = 1; // Generate complete code for parsing, serialization, - // etc. - CODE_SIZE = 2; // Use ReflectionOps to implement these methods. - LITE_RUNTIME = 3; // Generate code using MessageLite and the lite runtime. + SPEED = 1; // Generate complete code for parsing, serialization, + // etc. + CODE_SIZE = 2; // Use ReflectionOps to implement these methods. + LITE_RUNTIME = 3; // Generate code using MessageLite and the lite runtime. } - optional OptimizeMode optimize_for = 9 [default=SPEED]; + optional OptimizeMode optimize_for = 9 [default = SPEED]; // Sets the Go package where structs generated from this .proto will be // placed. If omitted, the Go package will be derived from the following: @@ -369,6 +369,7 @@ message FileOptions { + // Should generic services be generated in each language? "Generic" services // are not specific to any particular RPC system. They are generated by the // main code generators in each language (without additional plugins). @@ -379,20 +380,20 @@ message FileOptions { // that generate code specific to your particular RPC system. Therefore, // these default to false. Old code which depends on generic services should // explicitly set them to true. - optional bool cc_generic_services = 16 [default=false]; - optional bool java_generic_services = 17 [default=false]; - optional bool py_generic_services = 18 [default=false]; - optional bool php_generic_services = 42 [default=false]; + optional bool cc_generic_services = 16 [default = false]; + optional bool java_generic_services = 17 [default = false]; + optional bool py_generic_services = 18 [default = false]; + optional bool php_generic_services = 42 [default = false]; // Is this file deprecated? // Depending on the target platform, this can emit Deprecated annotations // for everything in the file, or it will be completely ignored; in the very // least, this is a formalization for deprecating files. - optional bool deprecated = 23 [default=false]; + optional bool deprecated = 23 [default = false]; // Enables the use of arenas for the proto messages in this file. This applies // only to generated classes for C++. - optional bool cc_enable_arenas = 31 [default=false]; + optional bool cc_enable_arenas = 31 [default = false]; // Sets the objective c class prefix which is prepended to all objective c @@ -417,10 +418,9 @@ message FileOptions { // determining the namespace. optional string php_namespace = 41; - // Use this option to change the namespace of php generated metadata classes. - // Default is empty. When this option is empty, the proto file name will be used - // for determining the namespace. + // Default is empty. When this option is empty, the proto file name will be + // used for determining the namespace. optional string php_metadata_namespace = 44; // Use this option to change the package of ruby generated classes. Default @@ -428,6 +428,7 @@ message FileOptions { // determining the ruby package. optional string ruby_package = 45; + // The parser stores options it doesn't recognize here. // See the documentation for the "Options" section above. repeated UninterpretedOption uninterpreted_option = 999; @@ -458,18 +459,18 @@ message MessageOptions { // // Because this is an option, the above two restrictions are not enforced by // the protocol compiler. - optional bool message_set_wire_format = 1 [default=false]; + optional bool message_set_wire_format = 1 [default = false]; // Disables the generation of the standard "descriptor()" accessor, which can // conflict with a field of the same name. This is meant to make migration // from proto1 easier; new code should avoid fields named "descriptor". - optional bool no_standard_descriptor_accessor = 2 [default=false]; + optional bool no_standard_descriptor_accessor = 2 [default = false]; // Is this message deprecated? // Depending on the target platform, this can emit Deprecated annotations // for the message, or it will be completely ignored; in the very least, // this is a formalization for deprecating messages. - optional bool deprecated = 3 [default=false]; + optional bool deprecated = 3 [default = false]; // Whether the message is an automatically generated map entry type for the // maps field. @@ -486,7 +487,7 @@ message MessageOptions { // // Implementations may choose not to generate the map_entry=true message, but // use a native map in the target language to hold the keys and values. - // The reflection APIs in such implementions still need to work as + // The reflection APIs in such implementations still need to work as // if the field is a repeated message field. // // NOTE: Do not set the option in .proto files. Always use the maps syntax @@ -497,6 +498,7 @@ message MessageOptions { reserved 8; // javalite_serializable reserved 9; // javanano_as_lite + // The parser stores options it doesn't recognize here. See above. repeated UninterpretedOption uninterpreted_option = 999; @@ -576,16 +578,16 @@ message FieldOptions { // implementation must either *always* check its required fields, or *never* // check its required fields, regardless of whether or not the message has // been parsed. - optional bool lazy = 5 [default=false]; + optional bool lazy = 5 [default = false]; // Is this field deprecated? // Depending on the target platform, this can emit Deprecated annotations // for accessors, or it will be completely ignored; in the very least, this // is a formalization for deprecating fields. - optional bool deprecated = 3 [default=false]; + optional bool deprecated = 3 [default = false]; // For Google-internal migration only. Do not use. - optional bool weak = 10 [default=false]; + optional bool weak = 10 [default = false]; // The parser stores options it doesn't recognize here. See above. @@ -615,7 +617,7 @@ message EnumOptions { // Depending on the target platform, this can emit Deprecated annotations // for the enum, or it will be completely ignored; in the very least, this // is a formalization for deprecating enums. - optional bool deprecated = 3 [default=false]; + optional bool deprecated = 3 [default = false]; reserved 5; // javanano_as_lite @@ -631,7 +633,7 @@ message EnumValueOptions { // Depending on the target platform, this can emit Deprecated annotations // for the enum value, or it will be completely ignored; in the very least, // this is a formalization for deprecating enum values. - optional bool deprecated = 1 [default=false]; + optional bool deprecated = 1 [default = false]; // The parser stores options it doesn't recognize here. See above. repeated UninterpretedOption uninterpreted_option = 999; @@ -651,7 +653,7 @@ message ServiceOptions { // Depending on the target platform, this can emit Deprecated annotations // for the service, or it will be completely ignored; in the very least, // this is a formalization for deprecating services. - optional bool deprecated = 33 [default=false]; + optional bool deprecated = 33 [default = false]; // The parser stores options it doesn't recognize here. See above. repeated UninterpretedOption uninterpreted_option = 999; @@ -671,18 +673,18 @@ message MethodOptions { // Depending on the target platform, this can emit Deprecated annotations // for the method, or it will be completely ignored; in the very least, // this is a formalization for deprecating methods. - optional bool deprecated = 33 [default=false]; + optional bool deprecated = 33 [default = false]; // Is this method side-effect-free (or safe in HTTP parlance), or idempotent, // or neither? HTTP based RPC implementation may choose GET verb for safe // methods, and PUT verb for idempotent methods instead of the default POST. enum IdempotencyLevel { IDEMPOTENCY_UNKNOWN = 0; - NO_SIDE_EFFECTS = 1; // implies idempotent - IDEMPOTENT = 2; // idempotent, but may have side effects + NO_SIDE_EFFECTS = 1; // implies idempotent + IDEMPOTENT = 2; // idempotent, but may have side effects } - optional IdempotencyLevel idempotency_level = - 34 [default=IDEMPOTENCY_UNKNOWN]; + optional IdempotencyLevel idempotency_level = 34 + [default = IDEMPOTENCY_UNKNOWN]; // The parser stores options it doesn't recognize here. See above. repeated UninterpretedOption uninterpreted_option = 999; @@ -763,7 +765,7 @@ message SourceCodeInfo { // beginning of the "extend" block and is shared by all extensions within // the block. // - Just because a location's span is a subset of some other location's span - // does not mean that it is a descendent. For example, a "group" defines + // does not mean that it is a descendant. For example, a "group" defines // both a type and a field in a single declaration. Thus, the locations // corresponding to the type and field and their components will overlap. // - Code which tries to interpret locations should probably be designed to @@ -794,14 +796,14 @@ message SourceCodeInfo { // [ 4, 3, 2, 7 ] // this path refers to the whole field declaration (from the beginning // of the label to the terminating semicolon). - repeated int32 path = 1 [packed=true]; + repeated int32 path = 1 [packed = true]; // Always has exactly three or four elements: start line, start column, // end line (optional, otherwise assumed same as start line), end column. // These are packed into a single field for efficiency. Note that line // and column numbers are zero-based -- typically you will want to add // 1 to each before displaying to a user. - repeated int32 span = 2 [packed=true]; + repeated int32 span = 2 [packed = true]; // If this SourceCodeInfo represents a complete declaration, these are any // comments appearing before and after the declaration which appear to be @@ -866,7 +868,7 @@ message GeneratedCodeInfo { message Annotation { // Identifies the element in the original source .proto file. This field // is formatted the same as SourceCodeInfo.Location.path. - repeated int32 path = 1 [packed=true]; + repeated int32 path = 1 [packed = true]; // Identifies the filesystem path to the original source .proto. optional string source_file = 2; diff --git a/src/google/protobuf/descriptor_database.cc b/src/google/protobuf/descriptor_database.cc index 1f5ba48087..fe6b02c40a 100644 --- a/src/google/protobuf/descriptor_database.cc +++ b/src/google/protobuf/descriptor_database.cc @@ -37,7 +37,6 @@ #include #include -#include #include #include @@ -46,14 +45,78 @@ namespace google { namespace protobuf { +namespace { +void RecordMessageNames(const DescriptorProto& desc_proto, + const std::string& prefix, + std::set* output) { + GOOGLE_CHECK(desc_proto.has_name()); + std::string full_name = prefix.empty() + ? desc_proto.name() + : StrCat(prefix, ".", desc_proto.name()); + output->insert(full_name); + + for (const auto& d : desc_proto.nested_type()) { + RecordMessageNames(d, full_name, output); + } +} + +void RecordMessageNames(const FileDescriptorProto& file_proto, + std::set* output) { + for (const auto& d : file_proto.message_type()) { + RecordMessageNames(d, file_proto.package(), output); + } +} + +template +bool ForAllFileProtos(DescriptorDatabase* db, Fn callback, + std::vector* output) { + std::vector file_names; + if (!db->FindAllFileNames(&file_names)) { + return false; + } + std::set set; + FileDescriptorProto file_proto; + for (const auto& f : file_names) { + file_proto.Clear(); + if (!db->FindFileByName(f, &file_proto)) { + GOOGLE_LOG(ERROR) << "File not found in database (unexpected): " << f; + return false; + } + callback(file_proto, &set); + } + output->insert(output->end(), set.begin(), set.end()); + return true; +} +} // namespace + DescriptorDatabase::~DescriptorDatabase() {} +bool DescriptorDatabase::FindAllPackageNames(std::vector* output) { + return ForAllFileProtos( + this, + [](const FileDescriptorProto& file_proto, std::set* set) { + set->insert(file_proto.package()); + }, + output); +} + +bool DescriptorDatabase::FindAllMessageNames(std::vector* output) { + return ForAllFileProtos( + this, + [](const FileDescriptorProto& file_proto, std::set* set) { + RecordMessageNames(file_proto, set); + }, + output); +} + // =================================================================== +SimpleDescriptorDatabase::SimpleDescriptorDatabase() {} +SimpleDescriptorDatabase::~SimpleDescriptorDatabase() {} + template bool SimpleDescriptorDatabase::DescriptorIndex::AddFile( - const FileDescriptorProto& file, - Value value) { + const FileDescriptorProto& file, Value value) { if (!InsertIfNotPresent(&by_name_, file.name(), value)) { GOOGLE_LOG(ERROR) << "File already exists in database: " << file.name(); return false; @@ -62,19 +125,20 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddFile( // We must be careful here -- calling file.package() if file.has_package() is // false could access an uninitialized static-storage variable if we are being // run at startup time. - string path = file.has_package() ? file.package() : string(); + std::string path = file.has_package() ? file.package() : std::string(); if (!path.empty()) path += '.'; for (int i = 0; i < file.message_type_size(); i++) { if (!AddSymbol(path + file.message_type(i).name(), value)) return false; - if (!AddNestedExtensions(file.message_type(i), value)) return false; + if (!AddNestedExtensions(file.name(), file.message_type(i), value)) + return false; } for (int i = 0; i < file.enum_type_size(); i++) { if (!AddSymbol(path + file.enum_type(i).name(), value)) return false; } for (int i = 0; i < file.extension_size(); i++) { if (!AddSymbol(path + file.extension(i).name(), value)) return false; - if (!AddExtension(file.extension(i), value)) return false; + if (!AddExtension(file.name(), file.extension(i), value)) return false; } for (int i = 0; i < file.service_size(); i++) { if (!AddSymbol(path + file.service(i).name(), value)) return false; @@ -85,7 +149,7 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddFile( template bool SimpleDescriptorDatabase::DescriptorIndex::AddSymbol( - const string& name, Value value) { + const std::string& name, Value value) { // We need to make sure not to violate our map invariant. // If the symbol name is invalid it could break our lookup algorithm (which @@ -98,18 +162,21 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddSymbol( // Try to look up the symbol to make sure a super-symbol doesn't already // exist. - typename std::map::iterator iter = FindLastLessOrEqual(name); + typename std::map::iterator iter = + FindLastLessOrEqual(name); if (iter == by_symbol_.end()) { // Apparently the map is currently empty. Just insert and be done with it. by_symbol_.insert( - typename std::map::value_type(name, value)); + typename std::map::value_type(name, value)); return true; } if (IsSubSymbol(iter->first, name)) { - GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the existing " - "symbol \"" << iter->first << "\"."; + GOOGLE_LOG(ERROR) << "Symbol name \"" << name + << "\" conflicts with the existing " + "symbol \"" + << iter->first << "\"."; return false; } @@ -121,8 +188,10 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddSymbol( ++iter; if (iter != by_symbol_.end() && IsSubSymbol(name, iter->first)) { - GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the existing " - "symbol \"" << iter->first << "\"."; + GOOGLE_LOG(ERROR) << "Symbol name \"" << name + << "\" conflicts with the existing " + "symbol \"" + << iter->first << "\"."; return false; } @@ -130,28 +199,29 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddSymbol( // Insert the new symbol using the iterator as a hint, the new entry will // appear immediately before the one the iterator is pointing at. - by_symbol_.insert(iter, - typename std::map::value_type(name, value)); + by_symbol_.insert( + iter, typename std::map::value_type(name, value)); return true; } template bool SimpleDescriptorDatabase::DescriptorIndex::AddNestedExtensions( - const DescriptorProto& message_type, + const std::string& filename, const DescriptorProto& message_type, Value value) { for (int i = 0; i < message_type.nested_type_size(); i++) { - if (!AddNestedExtensions(message_type.nested_type(i), value)) return false; + if (!AddNestedExtensions(filename, message_type.nested_type(i), value)) + return false; } for (int i = 0; i < message_type.extension_size(); i++) { - if (!AddExtension(message_type.extension(i), value)) return false; + if (!AddExtension(filename, message_type.extension(i), value)) return false; } return true; } template bool SimpleDescriptorDatabase::DescriptorIndex::AddExtension( - const FieldDescriptorProto& field, + const std::string& filename, const FieldDescriptorProto& field, Value value) { if (!field.extendee().empty() && field.extendee()[0] == '.') { // The extension is fully-qualified. We can use it as a lookup key in @@ -161,8 +231,9 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddExtension( std::make_pair(field.extendee().substr(1), field.number()), value)) { GOOGLE_LOG(ERROR) << "Extension conflicts with extension already in database: " - "extend " << field.extendee() << " { " - << field.name() << " = " << field.number() << " }"; + "extend " + << field.extendee() << " { " << field.name() << " = " + << field.number() << " } from:" << filename; return false; } } else { @@ -175,32 +246,32 @@ bool SimpleDescriptorDatabase::DescriptorIndex::AddExtension( template Value SimpleDescriptorDatabase::DescriptorIndex::FindFile( - const string& filename) { + const std::string& filename) { return FindWithDefault(by_name_, filename, Value()); } template Value SimpleDescriptorDatabase::DescriptorIndex::FindSymbol( - const string& name) { - typename std::map::iterator iter = FindLastLessOrEqual(name); + const std::string& name) { + typename std::map::iterator iter = + FindLastLessOrEqual(name); - return (iter != by_symbol_.end() && IsSubSymbol(iter->first, name)) ? - iter->second : Value(); + return (iter != by_symbol_.end() && IsSubSymbol(iter->first, name)) + ? iter->second + : Value(); } template Value SimpleDescriptorDatabase::DescriptorIndex::FindExtension( - const string& containing_type, - int field_number) { + const std::string& containing_type, int field_number) { return FindWithDefault( by_extension_, std::make_pair(containing_type, field_number), Value()); } template bool SimpleDescriptorDatabase::DescriptorIndex::FindAllExtensionNumbers( - const string& containing_type, - std::vector* output) { - typename std::map, Value>::const_iterator it = + const std::string& containing_type, std::vector* output) { + typename std::map, Value>::const_iterator it = by_extension_.lower_bound(std::make_pair(containing_type, 0)); bool success = false; @@ -215,7 +286,7 @@ bool SimpleDescriptorDatabase::DescriptorIndex::FindAllExtensionNumbers( template void SimpleDescriptorDatabase::DescriptorIndex::FindAllFileNames( - std::vector* output) { + std::vector* output) { output->resize(by_name_.size()); int i = 0; for (const auto& kv : by_name_) { @@ -225,13 +296,13 @@ void SimpleDescriptorDatabase::DescriptorIndex::FindAllFileNames( } template -typename std::map::iterator +typename std::map::iterator SimpleDescriptorDatabase::DescriptorIndex::FindLastLessOrEqual( - const string& name) { + const std::string& name) { // Find the last key in the map which sorts less than or equal to the // symbol name. Since upper_bound() returns the *first* key that sorts // *greater* than the input, we want the element immediately before that. - typename std::map::iterator iter = + typename std::map::iterator iter = by_symbol_.upper_bound(name); if (iter != by_symbol_.begin()) --iter; return iter; @@ -239,7 +310,7 @@ SimpleDescriptorDatabase::DescriptorIndex::FindLastLessOrEqual( template bool SimpleDescriptorDatabase::DescriptorIndex::IsSubSymbol( - const string& sub_symbol, const string& super_symbol) { + const std::string& sub_symbol, const std::string& super_symbol) { return sub_symbol == super_symbol || (HasPrefixString(super_symbol, sub_symbol) && super_symbol[sub_symbol.size()] == '.'); @@ -247,13 +318,11 @@ bool SimpleDescriptorDatabase::DescriptorIndex::IsSubSymbol( template bool SimpleDescriptorDatabase::DescriptorIndex::ValidateSymbolName( - const string& name) { + const std::string& name) { for (int i = 0; i < name.size(); i++) { // I don't trust ctype.h due to locales. :( - if (name[i] != '.' && name[i] != '_' && - (name[i] < '0' || name[i] > '9') && - (name[i] < 'A' || name[i] > 'Z') && - (name[i] < 'a' || name[i] > 'z')) { + if (name[i] != '.' && name[i] != '_' && (name[i] < '0' || name[i] > '9') && + (name[i] < 'A' || name[i] > 'Z') && (name[i] < 'a' || name[i] > 'z')) { return false; } } @@ -262,11 +331,6 @@ bool SimpleDescriptorDatabase::DescriptorIndex::ValidateSymbolName( // ------------------------------------------------------------------- -SimpleDescriptorDatabase::SimpleDescriptorDatabase() {} -SimpleDescriptorDatabase::~SimpleDescriptorDatabase() { - STLDeleteElements(&files_to_delete_); -} - bool SimpleDescriptorDatabase::Add(const FileDescriptorProto& file) { FileDescriptorProto* new_file = new FileDescriptorProto; new_file->CopyFrom(file); @@ -274,37 +338,34 @@ bool SimpleDescriptorDatabase::Add(const FileDescriptorProto& file) { } bool SimpleDescriptorDatabase::AddAndOwn(const FileDescriptorProto* file) { - files_to_delete_.push_back(file); + files_to_delete_.emplace_back(file); return index_.AddFile(*file, file); } -bool SimpleDescriptorDatabase::FindFileByName( - const string& filename, - FileDescriptorProto* output) { +bool SimpleDescriptorDatabase::FindFileByName(const std::string& filename, + FileDescriptorProto* output) { return MaybeCopy(index_.FindFile(filename), output); } bool SimpleDescriptorDatabase::FindFileContainingSymbol( - const string& symbol_name, - FileDescriptorProto* output) { + const std::string& symbol_name, FileDescriptorProto* output) { return MaybeCopy(index_.FindSymbol(symbol_name), output); } bool SimpleDescriptorDatabase::FindFileContainingExtension( - const string& containing_type, - int field_number, + const std::string& containing_type, int field_number, FileDescriptorProto* output) { return MaybeCopy(index_.FindExtension(containing_type, field_number), output); } bool SimpleDescriptorDatabase::FindAllExtensionNumbers( - const string& extendee_type, - std::vector* output) { + const std::string& extendee_type, std::vector* output) { return index_.FindAllExtensionNumbers(extendee_type, output); } -bool SimpleDescriptorDatabase::FindAllFileNames(std::vector* output) { +bool SimpleDescriptorDatabase::FindAllFileNames( + std::vector* output) { index_.FindAllFileNames(output); return true; } @@ -325,8 +386,8 @@ EncodedDescriptorDatabase::~EncodedDescriptorDatabase() { } } -bool EncodedDescriptorDatabase::Add( - const void* encoded_file_descriptor, int size) { +bool EncodedDescriptorDatabase::Add(const void* encoded_file_descriptor, + int size) { FileDescriptorProto file; if (file.ParseFromArray(encoded_file_descriptor, size)) { return index_.AddFile(file, std::make_pair(encoded_file_descriptor, size)); @@ -337,29 +398,26 @@ bool EncodedDescriptorDatabase::Add( } } -bool EncodedDescriptorDatabase::AddCopy( - const void* encoded_file_descriptor, int size) { +bool EncodedDescriptorDatabase::AddCopy(const void* encoded_file_descriptor, + int size) { void* copy = operator new(size); memcpy(copy, encoded_file_descriptor, size); files_to_delete_.push_back(copy); return Add(copy, size); } -bool EncodedDescriptorDatabase::FindFileByName( - const string& filename, - FileDescriptorProto* output) { +bool EncodedDescriptorDatabase::FindFileByName(const std::string& filename, + FileDescriptorProto* output) { return MaybeParse(index_.FindFile(filename), output); } bool EncodedDescriptorDatabase::FindFileContainingSymbol( - const string& symbol_name, - FileDescriptorProto* output) { + const std::string& symbol_name, FileDescriptorProto* output) { return MaybeParse(index_.FindSymbol(symbol_name), output); } bool EncodedDescriptorDatabase::FindNameOfFileContainingSymbol( - const string& symbol_name, - string* output) { + const std::string& symbol_name, std::string* output) { std::pair encoded_file = index_.FindSymbol(symbol_name); if (encoded_file.first == NULL) return false; @@ -387,22 +445,25 @@ bool EncodedDescriptorDatabase::FindNameOfFileContainingSymbol( } bool EncodedDescriptorDatabase::FindFileContainingExtension( - const string& containing_type, - int field_number, + const std::string& containing_type, int field_number, FileDescriptorProto* output) { return MaybeParse(index_.FindExtension(containing_type, field_number), output); } bool EncodedDescriptorDatabase::FindAllExtensionNumbers( - const string& extendee_type, - std::vector* output) { + const std::string& extendee_type, std::vector* output) { return index_.FindAllExtensionNumbers(extendee_type, output); } +bool EncodedDescriptorDatabase::FindAllFileNames( + std::vector* output) { + index_.FindAllFileNames(output); + return true; +} + bool EncodedDescriptorDatabase::MaybeParse( - std::pair encoded_file, - FileDescriptorProto* output) { + std::pair encoded_file, FileDescriptorProto* output) { if (encoded_file.first == NULL) return false; return output->ParseFromArray(encoded_file.first, encoded_file.second); } @@ -410,12 +471,11 @@ bool EncodedDescriptorDatabase::MaybeParse( // =================================================================== DescriptorPoolDatabase::DescriptorPoolDatabase(const DescriptorPool& pool) - : pool_(pool) {} + : pool_(pool) {} DescriptorPoolDatabase::~DescriptorPoolDatabase() {} -bool DescriptorPoolDatabase::FindFileByName( - const string& filename, - FileDescriptorProto* output) { +bool DescriptorPoolDatabase::FindFileByName(const std::string& filename, + FileDescriptorProto* output) { const FileDescriptor* file = pool_.FindFileByName(filename); if (file == NULL) return false; output->Clear(); @@ -424,8 +484,7 @@ bool DescriptorPoolDatabase::FindFileByName( } bool DescriptorPoolDatabase::FindFileContainingSymbol( - const string& symbol_name, - FileDescriptorProto* output) { + const std::string& symbol_name, FileDescriptorProto* output) { const FileDescriptor* file = pool_.FindFileContainingSymbol(symbol_name); if (file == NULL) return false; output->Clear(); @@ -434,14 +493,13 @@ bool DescriptorPoolDatabase::FindFileContainingSymbol( } bool DescriptorPoolDatabase::FindFileContainingExtension( - const string& containing_type, - int field_number, + const std::string& containing_type, int field_number, FileDescriptorProto* output) { const Descriptor* extendee = pool_.FindMessageTypeByName(containing_type); if (extendee == NULL) return false; const FieldDescriptor* extension = - pool_.FindExtensionByNumber(extendee, field_number); + pool_.FindExtensionByNumber(extendee, field_number); if (extension == NULL) return false; output->Clear(); @@ -450,8 +508,7 @@ bool DescriptorPoolDatabase::FindFileContainingExtension( } bool DescriptorPoolDatabase::FindAllExtensionNumbers( - const string& extendee_type, - std::vector* output) { + const std::string& extendee_type, std::vector* output) { const Descriptor* extendee = pool_.FindMessageTypeByName(extendee_type); if (extendee == NULL) return false; @@ -468,19 +525,17 @@ bool DescriptorPoolDatabase::FindAllExtensionNumbers( // =================================================================== MergedDescriptorDatabase::MergedDescriptorDatabase( - DescriptorDatabase* source1, - DescriptorDatabase* source2) { + DescriptorDatabase* source1, DescriptorDatabase* source2) { sources_.push_back(source1); sources_.push_back(source2); } MergedDescriptorDatabase::MergedDescriptorDatabase( const std::vector& sources) - : sources_(sources) {} + : sources_(sources) {} MergedDescriptorDatabase::~MergedDescriptorDatabase() {} -bool MergedDescriptorDatabase::FindFileByName( - const string& filename, - FileDescriptorProto* output) { +bool MergedDescriptorDatabase::FindFileByName(const std::string& filename, + FileDescriptorProto* output) { for (int i = 0; i < sources_.size(); i++) { if (sources_[i]->FindFileByName(filename, output)) { return true; @@ -490,8 +545,7 @@ bool MergedDescriptorDatabase::FindFileByName( } bool MergedDescriptorDatabase::FindFileContainingSymbol( - const string& symbol_name, - FileDescriptorProto* output) { + const std::string& symbol_name, FileDescriptorProto* output) { for (int i = 0; i < sources_.size(); i++) { if (sources_[i]->FindFileContainingSymbol(symbol_name, output)) { // The symbol was found in source i. However, if one of the previous @@ -512,12 +566,11 @@ bool MergedDescriptorDatabase::FindFileContainingSymbol( } bool MergedDescriptorDatabase::FindFileContainingExtension( - const string& containing_type, - int field_number, + const std::string& containing_type, int field_number, FileDescriptorProto* output) { for (int i = 0; i < sources_.size(); i++) { - if (sources_[i]->FindFileContainingExtension( - containing_type, field_number, output)) { + if (sources_[i]->FindFileContainingExtension(containing_type, field_number, + output)) { // The symbol was found in source i. However, if one of the previous // sources defines a file with the same name (which presumably doesn't // contain the symbol, since it wasn't found in that source), then we @@ -536,8 +589,7 @@ bool MergedDescriptorDatabase::FindFileContainingExtension( } bool MergedDescriptorDatabase::FindAllExtensionNumbers( - const string& extendee_type, - std::vector* output) { + const std::string& extendee_type, std::vector* output) { std::set merged_results; std::vector results; bool success = false; diff --git a/src/google/protobuf/descriptor_database.h b/src/google/protobuf/descriptor_database.h index 0a87a14743..e6d9266995 100644 --- a/src/google/protobuf/descriptor_database.h +++ b/src/google/protobuf/descriptor_database.h @@ -44,6 +44,12 @@ #include #include +#include + +#ifdef SWIG +#error "You cannot SWIG proto headers" +#endif + namespace google { namespace protobuf { @@ -62,27 +68,27 @@ class MergedDescriptorDatabase; // calling DescriptorPool::BuildFile() for each one. Instead, a DescriptorPool // can be created which wraps a DescriptorDatabase and only builds particular // descriptors when they are needed. -class LIBPROTOBUF_EXPORT DescriptorDatabase { +class PROTOBUF_EXPORT DescriptorDatabase { public: inline DescriptorDatabase() {} virtual ~DescriptorDatabase(); // Find a file by file name. Fills in in *output and returns true if found. // Otherwise, returns false, leaving the contents of *output undefined. - virtual bool FindFileByName(const string& filename, + virtual bool FindFileByName(const std::string& filename, FileDescriptorProto* output) = 0; // Find the file that declares the given fully-qualified symbol name. // If found, fills in *output and returns true, otherwise returns false // and leaves *output undefined. - virtual bool FindFileContainingSymbol(const string& symbol_name, + virtual bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) = 0; // Find the file which defines an extension extending the given message type // with the given field number. If found, fills in *output and returns true, // otherwise returns false and leaves *output undefined. containing_type // must be a fully-qualified type name. - virtual bool FindFileContainingExtension(const string& containing_type, + virtual bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) = 0; @@ -96,7 +102,7 @@ class LIBPROTOBUF_EXPORT DescriptorDatabase { // // This method has a default implementation that always returns // false. - virtual bool FindAllExtensionNumbers(const string& /* extendee_type */, + virtual bool FindAllExtensionNumbers(const std::string& /* extendee_type */, std::vector* /* output */) { return false; } @@ -110,10 +116,24 @@ class LIBPROTOBUF_EXPORT DescriptorDatabase { // // This method has a default implementation that always returns // false. - virtual bool FindAllFileNames(std::vector* output) { + virtual bool FindAllFileNames(std::vector* output) { return false; } + // Finds the package names and appends them to the output in an + // undefined order. This method is best-effort: it's not guaranteed that the + // database will find all packages. Returns true if the database supports + // searching all package names, otherwise returns false and leaves output + // unchanged. + bool FindAllPackageNames(std::vector* output); + + // Finds the message names and appends them to the output in an + // undefined order. This method is best-effort: it's not guaranteed that the + // database will find all messages. Returns true if the database supports + // searching all message names, otherwise returns false and leaves output + // unchanged. + bool FindAllMessageNames(std::vector* output); + private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorDatabase); }; @@ -139,7 +159,7 @@ class LIBPROTOBUF_EXPORT DescriptorDatabase { // FileDescriptor::CopyTo()) will always use fully-qualified names for all // types. You only need to worry if you are constructing FileDescriptorProtos // yourself, or are calling compiler::Parser directly. -class LIBPROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { +class PROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { public: SimpleDescriptorDatabase(); ~SimpleDescriptorDatabase() override; @@ -154,17 +174,17 @@ class LIBPROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { bool AddAndOwn(const FileDescriptorProto* file); // implements DescriptorDatabase ----------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) override; - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) override; - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) override; - bool FindAllExtensionNumbers(const string& extendee_type, + bool FindAllExtensionNumbers(const std::string& extendee_type, std::vector* output) override; - bool FindAllFileNames(std::vector* output) override; + bool FindAllFileNames(std::vector* output) override; private: // So that it can use DescriptorIndex. @@ -177,25 +197,24 @@ class LIBPROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { public: // Helpers to recursively add particular descriptors and all their contents // to the index. - bool AddFile(const FileDescriptorProto& file, - Value value); - bool AddSymbol(const string& name, Value value); - bool AddNestedExtensions(const DescriptorProto& message_type, - Value value); - bool AddExtension(const FieldDescriptorProto& field, - Value value); - - Value FindFile(const string& filename); - Value FindSymbol(const string& name); - Value FindExtension(const string& containing_type, int field_number); - bool FindAllExtensionNumbers(const string& containing_type, + bool AddFile(const FileDescriptorProto& file, Value value); + bool AddSymbol(const std::string& name, Value value); + bool AddNestedExtensions(const std::string& filename, + const DescriptorProto& message_type, Value value); + bool AddExtension(const std::string& filename, + const FieldDescriptorProto& field, Value value); + + Value FindFile(const std::string& filename); + Value FindSymbol(const std::string& name); + Value FindExtension(const std::string& containing_type, int field_number); + bool FindAllExtensionNumbers(const std::string& containing_type, std::vector* output); - void FindAllFileNames(std::vector* output); + void FindAllFileNames(std::vector* output); private: - std::map by_name_; - std::map by_symbol_; - std::map, Value> by_extension_; + std::map by_name_; + std::map by_symbol_; + std::map, Value> by_extension_; // Invariant: The by_symbol_ map does not contain any symbols which are // prefixes of other symbols in the map. For example, "foo.bar" is a @@ -250,27 +269,26 @@ class LIBPROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { // Find the last entry in the by_symbol_ map whose key is less than or // equal to the given name. - typename std::map::iterator FindLastLessOrEqual( - const string& name); + typename std::map::iterator FindLastLessOrEqual( + const std::string& name); // True if either the arguments are equal or super_symbol identifies a // parent symbol of sub_symbol (e.g. "foo.bar" is a parent of // "foo.bar.baz", but not a parent of "foo.barbaz"). - bool IsSubSymbol(const string& sub_symbol, const string& super_symbol); + bool IsSubSymbol(const std::string& sub_symbol, + const std::string& super_symbol); // Returns true if and only if all characters in the name are alphanumerics, // underscores, or periods. - bool ValidateSymbolName(const string& name); + bool ValidateSymbolName(const std::string& name); }; - DescriptorIndex index_; - std::vector files_to_delete_; + std::vector> files_to_delete_; // If file is non-NULL, copy it into *output and return true, otherwise // return false. - bool MaybeCopy(const FileDescriptorProto* file, - FileDescriptorProto* output); + bool MaybeCopy(const FileDescriptorProto* file, FileDescriptorProto* output); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SimpleDescriptorDatabase); }; @@ -280,7 +298,7 @@ class LIBPROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { // // The same caveats regarding FindFileContainingExtension() apply as with // SimpleDescriptorDatabase. -class LIBPROTOBUF_EXPORT EncodedDescriptorDatabase : public DescriptorDatabase { +class PROTOBUF_EXPORT EncodedDescriptorDatabase : public DescriptorDatabase { public: EncodedDescriptorDatabase(); ~EncodedDescriptorDatabase() override; @@ -298,19 +316,20 @@ class LIBPROTOBUF_EXPORT EncodedDescriptorDatabase : public DescriptorDatabase { bool AddCopy(const void* encoded_file_descriptor, int size); // Like FindFileContainingSymbol but returns only the name of the file. - bool FindNameOfFileContainingSymbol(const string& symbol_name, - string* output); + bool FindNameOfFileContainingSymbol(const std::string& symbol_name, + std::string* output); // implements DescriptorDatabase ----------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) override; - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) override; - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) override; - bool FindAllExtensionNumbers(const string& extendee_type, + bool FindAllExtensionNumbers(const std::string& extendee_type, std::vector* output) override; + bool FindAllFileNames(std::vector* output) override; private: SimpleDescriptorDatabase::DescriptorIndex > @@ -326,20 +345,20 @@ class LIBPROTOBUF_EXPORT EncodedDescriptorDatabase : public DescriptorDatabase { }; // A DescriptorDatabase that fetches files from a given pool. -class LIBPROTOBUF_EXPORT DescriptorPoolDatabase : public DescriptorDatabase { +class PROTOBUF_EXPORT DescriptorPoolDatabase : public DescriptorDatabase { public: explicit DescriptorPoolDatabase(const DescriptorPool& pool); ~DescriptorPoolDatabase() override; // implements DescriptorDatabase ----------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) override; - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) override; - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) override; - bool FindAllExtensionNumbers(const string& extendee_type, + bool FindAllExtensionNumbers(const std::string& extendee_type, std::vector* output) override; private: @@ -349,7 +368,7 @@ class LIBPROTOBUF_EXPORT DescriptorPoolDatabase : public DescriptorDatabase { // A DescriptorDatabase that wraps two or more others. It first searches the // first database and, if that fails, tries the second, and so on. -class LIBPROTOBUF_EXPORT MergedDescriptorDatabase : public DescriptorDatabase { +class PROTOBUF_EXPORT MergedDescriptorDatabase : public DescriptorDatabase { public: // Merge just two databases. The sources remain property of the caller. MergedDescriptorDatabase(DescriptorDatabase* source1, @@ -362,16 +381,16 @@ class LIBPROTOBUF_EXPORT MergedDescriptorDatabase : public DescriptorDatabase { ~MergedDescriptorDatabase() override; // implements DescriptorDatabase ----------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) override; - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) override; - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) override; // Merges the results of calling all databases. Returns true iff any // of the databases returned true. - bool FindAllExtensionNumbers(const string& extendee_type, + bool FindAllExtensionNumbers(const std::string& extendee_type, std::vector* output) override; @@ -383,4 +402,6 @@ class LIBPROTOBUF_EXPORT MergedDescriptorDatabase : public DescriptorDatabase { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_DESCRIPTOR_DATABASE_H__ diff --git a/src/google/protobuf/descriptor_database_unittest.cc b/src/google/protobuf/descriptor_database_unittest.cc index 7d3f17ca32..8653193abd 100644 --- a/src/google/protobuf/descriptor_database_unittest.cc +++ b/src/google/protobuf/descriptor_database_unittest.cc @@ -60,12 +60,11 @@ static void AddToDatabase(SimpleDescriptorDatabase* database, } static void ExpectContainsType(const FileDescriptorProto& proto, - const string& type_name) { + const std::string& type_name) { for (int i = 0; i < proto.message_type_size(); i++) { if (proto.message_type(i).name() == type_name) return; } - ADD_FAILURE() << "\"" << proto.name() - << "\" did not contain expected type \"" + ADD_FAILURE() << "\"" << proto.name() << "\" did not contain expected type \"" << type_name << "\"."; } @@ -100,9 +99,7 @@ class SimpleDescriptorDatabaseTestCase : public DescriptorDatabaseTestCase { virtual ~SimpleDescriptorDatabaseTestCase() {} - virtual DescriptorDatabase* GetDatabase() { - return &database_; - } + virtual DescriptorDatabase* GetDatabase() { return &database_; } virtual bool AddToDatabase(const FileDescriptorProto& file) { return database_.Add(file); } @@ -120,11 +117,9 @@ class EncodedDescriptorDatabaseTestCase : public DescriptorDatabaseTestCase { virtual ~EncodedDescriptorDatabaseTestCase() {} - virtual DescriptorDatabase* GetDatabase() { - return &database_; - } + virtual DescriptorDatabase* GetDatabase() { return &database_; } virtual bool AddToDatabase(const FileDescriptorProto& file) { - string data; + std::string data; file.SerializeToString(&data); return database_.AddCopy(data.data(), data.size()); } @@ -143,9 +138,7 @@ class DescriptorPoolDatabaseTestCase : public DescriptorDatabaseTestCase { DescriptorPoolDatabaseTestCase() : database_(pool_) {} virtual ~DescriptorPoolDatabaseTestCase() {} - virtual DescriptorDatabase* GetDatabase() { - return &database_; - } + virtual DescriptorDatabase* GetDatabase() { return &database_; } virtual bool AddToDatabase(const FileDescriptorProto& file) { return pool_.BuildFile(file); } @@ -183,11 +176,11 @@ class DescriptorDatabaseTest TEST_P(DescriptorDatabaseTest, FindFileByName) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { name:\"Foo\" }"); + "name: \"foo.proto\" " + "message_type { name:\"Foo\" }"); AddToDatabase( - "name: \"bar.proto\" " - "message_type { name:\"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name:\"Bar\" }"); { FileDescriptorProto file; @@ -212,27 +205,26 @@ TEST_P(DescriptorDatabaseTest, FindFileByName) { TEST_P(DescriptorDatabaseTest, FindFileContainingSymbol) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - " field { name:\"qux\" }" - " nested_type { name: \"Grault\" } " - " enum_type { name: \"Garply\" } " - "} " - "enum_type { " - " name: \"Waldo\" " - " value { name:\"FRED\" } " - "} " - "extension { name: \"plugh\" } " - "service { " - " name: \"Xyzzy\" " - " method { name: \"Thud\" } " - "}" - ); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + " field { name:\"qux\" }" + " nested_type { name: \"Grault\" } " + " enum_type { name: \"Garply\" } " + "} " + "enum_type { " + " name: \"Waldo\" " + " value { name:\"FRED\" } " + "} " + "extension { name: \"plugh\" } " + "service { " + " name: \"Xyzzy\" " + " method { name: \"Thud\" } " + "}"); AddToDatabase( - "name: \"bar.proto\" " - "package: \"corge\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "package: \"corge\" " + "message_type { name: \"Bar\" }"); { FileDescriptorProto file; @@ -247,8 +239,8 @@ TEST_P(DescriptorDatabaseTest, FindFileContainingSymbol) { EXPECT_EQ("foo.proto", file.name()); // Non-existent field under a valid top level symbol can also be // found. - EXPECT_TRUE(database_->FindFileContainingSymbol("Foo.none_field.none", - &file)); + EXPECT_TRUE( + database_->FindFileContainingSymbol("Foo.none_field.none", &file)); } { @@ -322,24 +314,25 @@ TEST_P(DescriptorDatabaseTest, FindFileContainingSymbol) { TEST_P(DescriptorDatabaseTest, FindFileContainingExtension) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - " extension_range { start: 1 end: 1000 } " - " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " - " extendee: \".Foo\" }" - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + " extension_range { start: 1 end: 1000 } " + " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 " + "number:5 " + " extendee: \".Foo\" }" + "}"); AddToDatabase( - "name: \"bar.proto\" " - "package: \"corge\" " - "dependency: \"foo.proto\" " - "message_type { " - " name: \"Bar\" " - " extension_range { start: 1 end: 1000 } " - "} " - "extension { name:\"grault\" extendee: \".Foo\" number:32 } " - "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } " - "extension { name:\"waldo\" extendee: \"Bar\" number:56 } "); + "name: \"bar.proto\" " + "package: \"corge\" " + "dependency: \"foo.proto\" " + "message_type { " + " name: \"Bar\" " + " extension_range { start: 1 end: 1000 } " + "} " + "extension { name:\"grault\" extendee: \".Foo\" number:32 } " + "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } " + "extension { name:\"waldo\" extendee: \"Bar\" number:56 } "); { FileDescriptorProto file; @@ -391,24 +384,25 @@ TEST_P(DescriptorDatabaseTest, FindFileContainingExtension) { TEST_P(DescriptorDatabaseTest, FindAllExtensionNumbers) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - " extension_range { start: 1 end: 1000 } " - " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " - " extendee: \".Foo\" }" - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + " extension_range { start: 1 end: 1000 } " + " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 " + "number:5 " + " extendee: \".Foo\" }" + "}"); AddToDatabase( - "name: \"bar.proto\" " - "package: \"corge\" " - "dependency: \"foo.proto\" " - "message_type { " - " name: \"Bar\" " - " extension_range { start: 1 end: 1000 } " - "} " - "extension { name:\"grault\" extendee: \".Foo\" number:32 } " - "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } " - "extension { name:\"waldo\" extendee: \"Bar\" number:56 } "); + "name: \"bar.proto\" " + "package: \"corge\" " + "dependency: \"foo.proto\" " + "message_type { " + " name: \"Bar\" " + " extension_range { start: 1 end: 1000 } " + "} " + "extension { name:\"grault\" extendee: \".Foo\" number:32 } " + "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } " + "extension { name:\"waldo\" extendee: \"Bar\" number:56 } "); { std::vector numbers; @@ -442,47 +436,49 @@ TEST_P(DescriptorDatabaseTest, FindAllExtensionNumbers) { TEST_P(DescriptorDatabaseTest, ConflictingFileError) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + "}"); AddToDatabaseWithError( - "name: \"foo.proto\" " - "message_type { " - " name: \"Bar\" " - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Bar\" " + "}"); } TEST_P(DescriptorDatabaseTest, ConflictingTypeError) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + "}"); AddToDatabaseWithError( - "name: \"bar.proto\" " - "message_type { " - " name: \"Foo\" " - "}"); + "name: \"bar.proto\" " + "message_type { " + " name: \"Foo\" " + "}"); } TEST_P(DescriptorDatabaseTest, ConflictingExtensionError) { AddToDatabase( - "name: \"foo.proto\" " - "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " - " extendee: \".Foo\" }"); + "name: \"foo.proto\" " + "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " + " extendee: \".Foo\" }"); AddToDatabaseWithError( - "name: \"bar.proto\" " - "extension { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " - " extendee: \".Foo\" }"); + "name: \"bar.proto\" " + "extension { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " + " extendee: \".Foo\" }"); } -INSTANTIATE_TEST_CASE_P(Simple, DescriptorDatabaseTest, +INSTANTIATE_TEST_CASE_P( + Simple, DescriptorDatabaseTest, testing::Values(&SimpleDescriptorDatabaseTestCase::New)); -INSTANTIATE_TEST_CASE_P(MemoryConserving, DescriptorDatabaseTest, +INSTANTIATE_TEST_CASE_P( + MemoryConserving, DescriptorDatabaseTest, testing::Values(&EncodedDescriptorDatabaseTestCase::New)); INSTANTIATE_TEST_CASE_P(Pool, DescriptorDatabaseTest, - testing::Values(&DescriptorPoolDatabaseTestCase::New)); + testing::Values(&DescriptorPoolDatabaseTestCase::New)); #endif // GTEST_HAS_PARAM_TEST @@ -497,10 +493,10 @@ TEST(EncodedDescriptorDatabaseExtraTest, FindNameOfFileContainingSymbol) { file2b.add_message_type()->set_name("Bar"); // Normal serialization allows our optimization to kick in. - string data1 = file1.SerializeAsString(); + std::string data1 = file1.SerializeAsString(); // Force out-of-order serialization to test slow path. - string data2 = file2b.SerializeAsString() + file2a.SerializeAsString(); + std::string data2 = file2b.SerializeAsString() + file2a.SerializeAsString(); // Create EncodedDescriptorDatabase containing both files. EncodedDescriptorDatabase db; @@ -508,7 +504,7 @@ TEST(EncodedDescriptorDatabaseExtraTest, FindNameOfFileContainingSymbol) { db.Add(data2.data(), data2.size()); // Test! - string filename; + std::string filename; EXPECT_TRUE(db.FindNameOfFileContainingSymbol("foo.Foo", &filename)); EXPECT_EQ("foo.proto", filename); EXPECT_TRUE(db.FindNameOfFileContainingSymbol("foo.Foo.Blah", &filename)); @@ -530,46 +526,90 @@ TEST(SimpleDescriptorDatabaseExtraTest, FindAllFileNames) { db.Add(f); // Test! - std::vector all_files; + std::vector all_files; db.FindAllFileNames(&all_files); EXPECT_THAT(all_files, testing::ElementsAre("foo.proto")); } +TEST(SimpleDescriptorDatabaseExtraTest, FindAllPackageNames) { + FileDescriptorProto f; + f.set_name("foo.proto"); + f.set_package("foo"); + f.add_message_type()->set_name("Foo"); + + FileDescriptorProto b; + b.set_name("bar.proto"); + b.set_package(""); + b.add_message_type()->set_name("Bar"); + + SimpleDescriptorDatabase db; + db.Add(f); + db.Add(b); + + std::vector packages; + EXPECT_TRUE(db.FindAllPackageNames(&packages)); + EXPECT_THAT(packages, ::testing::UnorderedElementsAre("foo", "")); +} + +TEST(SimpleDescriptorDatabaseExtraTest, FindAllMessageNames) { + FileDescriptorProto f; + f.set_name("foo.proto"); + f.set_package("foo"); + f.add_message_type()->set_name("Foo"); + + FileDescriptorProto b; + b.set_name("bar.proto"); + b.set_package(""); + b.add_message_type()->set_name("Bar"); + + SimpleDescriptorDatabase db; + db.Add(f); + db.Add(b); + + std::vector messages; + EXPECT_TRUE(db.FindAllMessageNames(&messages)); + EXPECT_THAT(messages, ::testing::UnorderedElementsAre("foo.Foo", "Bar")); +} + // =================================================================== class MergedDescriptorDatabaseTest : public testing::Test { protected: MergedDescriptorDatabaseTest() - : forward_merged_(&database1_, &database2_), - reverse_merged_(&database2_, &database1_) {} + : forward_merged_(&database1_, &database2_), + reverse_merged_(&database2_, &database1_) {} virtual void SetUp() { - AddToDatabase(&database1_, - "name: \"foo.proto\" " - "message_type { name:\"Foo\" extension_range { start: 1 end: 100 } } " - "extension { name:\"foo_ext\" extendee: \".Foo\" number:3 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); - AddToDatabase(&database2_, - "name: \"bar.proto\" " - "message_type { name:\"Bar\" extension_range { start: 1 end: 100 } } " - "extension { name:\"bar_ext\" extendee: \".Bar\" number:5 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + AddToDatabase( + &database1_, + "name: \"foo.proto\" " + "message_type { name:\"Foo\" extension_range { start: 1 end: 100 } } " + "extension { name:\"foo_ext\" extendee: \".Foo\" number:3 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + AddToDatabase( + &database2_, + "name: \"bar.proto\" " + "message_type { name:\"Bar\" extension_range { start: 1 end: 100 } } " + "extension { name:\"bar_ext\" extendee: \".Bar\" number:5 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); // baz.proto exists in both pools, with different definitions. - AddToDatabase(&database1_, - "name: \"baz.proto\" " - "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } " - "message_type { name:\"FromPool1\" } " - "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } " - "extension { name:\"database1_only_ext\" extendee: \".Baz\" number:13 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); - AddToDatabase(&database2_, - "name: \"baz.proto\" " - "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } " - "message_type { name:\"FromPool2\" } " - "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + AddToDatabase( + &database1_, + "name: \"baz.proto\" " + "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } " + "message_type { name:\"FromPool1\" } " + "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } " + "extension { name:\"database1_only_ext\" extendee: \".Baz\" number:13 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + AddToDatabase( + &database2_, + "name: \"baz.proto\" " + "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } " + "message_type { name:\"FromPool2\" } " + "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); } SimpleDescriptorDatabase database1_; @@ -663,8 +703,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingSymbol) { { // Can't find non-existent symbol. FileDescriptorProto file; - EXPECT_FALSE( - forward_merged_.FindFileContainingSymbol("NoSuchType", &file)); + EXPECT_FALSE(forward_merged_.FindFileContainingSymbol("NoSuchType", &file)); } } @@ -672,8 +711,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // Can find file that is only in database1_. FileDescriptorProto file; - EXPECT_TRUE( - forward_merged_.FindFileContainingExtension("Foo", 3, &file)); + EXPECT_TRUE(forward_merged_.FindFileContainingExtension("Foo", 3, &file)); EXPECT_EQ("foo.proto", file.name()); ExpectContainsType(file, "Foo"); } @@ -681,8 +719,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // Can find file that is only in database2_. FileDescriptorProto file; - EXPECT_TRUE( - forward_merged_.FindFileContainingExtension("Bar", 5, &file)); + EXPECT_TRUE(forward_merged_.FindFileContainingExtension("Bar", 5, &file)); EXPECT_EQ("bar.proto", file.name()); ExpectContainsType(file, "Bar"); } @@ -690,8 +727,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // In forward_merged_, database1_'s baz.proto takes precedence. FileDescriptorProto file; - EXPECT_TRUE( - forward_merged_.FindFileContainingExtension("Baz", 12, &file)); + EXPECT_TRUE(forward_merged_.FindFileContainingExtension("Baz", 12, &file)); EXPECT_EQ("baz.proto", file.name()); ExpectContainsType(file, "FromPool1"); } @@ -699,8 +735,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // In reverse_merged_, database2_'s baz.proto takes precedence. FileDescriptorProto file; - EXPECT_TRUE( - reverse_merged_.FindFileContainingExtension("Baz", 12, &file)); + EXPECT_TRUE(reverse_merged_.FindFileContainingExtension("Baz", 12, &file)); EXPECT_EQ("baz.proto", file.name()); ExpectContainsType(file, "FromPool2"); } @@ -716,8 +751,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // Can't find non-existent extension. FileDescriptorProto file; - EXPECT_FALSE( - forward_merged_.FindFileContainingExtension("Foo", 6, &file)); + EXPECT_FALSE(forward_merged_.FindFileContainingExtension("Foo", 6, &file)); } } diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index 0d16078e40..568e983738 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -62,6 +62,8 @@ #include +#include + namespace google { namespace protobuf { @@ -69,41 +71,43 @@ namespace protobuf { namespace descriptor_unittest { // Some helpers to make assembling descriptors faster. -DescriptorProto* AddMessage(FileDescriptorProto* file, const string& name) { +DescriptorProto* AddMessage(FileDescriptorProto* file, + const std::string& name) { DescriptorProto* result = file->add_message_type(); result->set_name(name); return result; } -DescriptorProto* AddNestedMessage(DescriptorProto* parent, const string& name) { +DescriptorProto* AddNestedMessage(DescriptorProto* parent, + const std::string& name) { DescriptorProto* result = parent->add_nested_type(); result->set_name(name); return result; } -EnumDescriptorProto* AddEnum(FileDescriptorProto* file, const string& name) { +EnumDescriptorProto* AddEnum(FileDescriptorProto* file, + const std::string& name) { EnumDescriptorProto* result = file->add_enum_type(); result->set_name(name); return result; } EnumDescriptorProto* AddNestedEnum(DescriptorProto* parent, - const string& name) { + const std::string& name) { EnumDescriptorProto* result = parent->add_enum_type(); result->set_name(name); return result; } ServiceDescriptorProto* AddService(FileDescriptorProto* file, - const string& name) { + const std::string& name) { ServiceDescriptorProto* result = file->add_service(); result->set_name(name); return result; } -FieldDescriptorProto* AddField(DescriptorProto* parent, - const string& name, int number, - FieldDescriptorProto::Label label, +FieldDescriptorProto* AddField(DescriptorProto* parent, const std::string& name, + int number, FieldDescriptorProto::Label label, FieldDescriptorProto::Type type) { FieldDescriptorProto* result = parent->add_field(); result->set_name(name); @@ -114,8 +118,8 @@ FieldDescriptorProto* AddField(DescriptorProto* parent, } FieldDescriptorProto* AddExtension(FileDescriptorProto* file, - const string& extendee, - const string& name, int number, + const std::string& extendee, + const std::string& name, int number, FieldDescriptorProto::Label label, FieldDescriptorProto::Type type) { FieldDescriptorProto* result = file->add_extension(); @@ -128,8 +132,8 @@ FieldDescriptorProto* AddExtension(FileDescriptorProto* file, } FieldDescriptorProto* AddNestedExtension(DescriptorProto* parent, - const string& extendee, - const string& name, int number, + const std::string& extendee, + const std::string& name, int number, FieldDescriptorProto::Label label, FieldDescriptorProto::Type type) { FieldDescriptorProto* result = parent->add_extension(); @@ -166,7 +170,7 @@ EnumDescriptorProto::EnumReservedRange* AddReservedRange( } EnumValueDescriptorProto* AddEnumValue(EnumDescriptorProto* enum_proto, - const string& name, int number) { + const std::string& name, int number) { EnumValueDescriptorProto* result = enum_proto->add_value(); result->set_name(name); result->set_number(number); @@ -174,9 +178,9 @@ EnumValueDescriptorProto* AddEnumValue(EnumDescriptorProto* enum_proto, } MethodDescriptorProto* AddMethod(ServiceDescriptorProto* service, - const string& name, - const string& input_type, - const string& output_type) { + const std::string& name, + const std::string& input_type, + const std::string& output_type) { MethodDescriptorProto* result = service->add_method(); result->set_name(name); result->set_input_type(input_type); @@ -186,7 +190,7 @@ MethodDescriptorProto* AddMethod(ServiceDescriptorProto* service, // Empty enums technically aren't allowed. We need to insert a dummy value // into them. -void AddEmptyEnum(FileDescriptorProto* file, const string& name) { +void AddEmptyEnum(FileDescriptorProto* file, const std::string& name) { AddEnumValue(AddEnum(file, name), name + "_DUMMY", 1); } @@ -195,53 +199,97 @@ class MockErrorCollector : public DescriptorPool::ErrorCollector { MockErrorCollector() {} ~MockErrorCollector() {} - string text_; - string warning_text_; + std::string text_; + std::string warning_text_; // implements ErrorCollector --------------------------------------- - void AddError(const string& filename, - const string& element_name, const Message* descriptor, - ErrorLocation location, const string& message) { - const char* location_name = NULL; + void AddError(const std::string& filename, const std::string& element_name, + const Message* descriptor, ErrorLocation location, + const std::string& message) { + const char* location_name = nullptr; switch (location) { - case NAME : location_name = "NAME" ; break; - case NUMBER : location_name = "NUMBER" ; break; - case TYPE : location_name = "TYPE" ; break; - case EXTENDEE : location_name = "EXTENDEE" ; break; - case DEFAULT_VALUE: location_name = "DEFAULT_VALUE"; break; - case OPTION_NAME : location_name = "OPTION_NAME" ; break; - case OPTION_VALUE : location_name = "OPTION_VALUE" ; break; - case INPUT_TYPE : location_name = "INPUT_TYPE" ; break; - case OUTPUT_TYPE : location_name = "OUTPUT_TYPE" ; break; - case OTHER : location_name = "OTHER" ; break; + case NAME: + location_name = "NAME"; + break; + case NUMBER: + location_name = "NUMBER"; + break; + case TYPE: + location_name = "TYPE"; + break; + case EXTENDEE: + location_name = "EXTENDEE"; + break; + case DEFAULT_VALUE: + location_name = "DEFAULT_VALUE"; + break; + case OPTION_NAME: + location_name = "OPTION_NAME"; + break; + case OPTION_VALUE: + location_name = "OPTION_VALUE"; + break; + case INPUT_TYPE: + location_name = "INPUT_TYPE"; + break; + case OUTPUT_TYPE: + location_name = "OUTPUT_TYPE"; + break; + case IMPORT: + location_name = "IMPORT"; + break; + case OTHER: + location_name = "OTHER"; + break; } - strings::SubstituteAndAppend( - &text_, "$0: $1: $2: $3\n", - filename, element_name, location_name, message); + strings::SubstituteAndAppend(&text_, "$0: $1: $2: $3\n", filename, + element_name, location_name, message); } // implements ErrorCollector --------------------------------------- - void AddWarning(const string& filename, const string& element_name, + void AddWarning(const std::string& filename, const std::string& element_name, const Message* descriptor, ErrorLocation location, - const string& message) { - const char* location_name = NULL; + const std::string& message) { + const char* location_name = nullptr; switch (location) { - case NAME : location_name = "NAME" ; break; - case NUMBER : location_name = "NUMBER" ; break; - case TYPE : location_name = "TYPE" ; break; - case EXTENDEE : location_name = "EXTENDEE" ; break; - case DEFAULT_VALUE: location_name = "DEFAULT_VALUE"; break; - case OPTION_NAME : location_name = "OPTION_NAME" ; break; - case OPTION_VALUE : location_name = "OPTION_VALUE" ; break; - case INPUT_TYPE : location_name = "INPUT_TYPE" ; break; - case OUTPUT_TYPE : location_name = "OUTPUT_TYPE" ; break; - case OTHER : location_name = "OTHER" ; break; + case NAME: + location_name = "NAME"; + break; + case NUMBER: + location_name = "NUMBER"; + break; + case TYPE: + location_name = "TYPE"; + break; + case EXTENDEE: + location_name = "EXTENDEE"; + break; + case DEFAULT_VALUE: + location_name = "DEFAULT_VALUE"; + break; + case OPTION_NAME: + location_name = "OPTION_NAME"; + break; + case OPTION_VALUE: + location_name = "OPTION_VALUE"; + break; + case INPUT_TYPE: + location_name = "INPUT_TYPE"; + break; + case OUTPUT_TYPE: + location_name = "OUTPUT_TYPE"; + break; + case IMPORT: + location_name = "IMPORT"; + break; + case OTHER: + location_name = "OTHER"; + break; } - strings::SubstituteAndAppend( - &warning_text_, "$0: $1: $2: $3\n", - filename, element_name, location_name, message); + strings::SubstituteAndAppend(&warning_text_, "$0: $1: $2: $3\n", filename, + element_name, location_name, message); } }; @@ -295,13 +343,13 @@ class FileDescriptorTest : public testing::Test { // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); bar_file_ = pool_.BuildFile(bar_file); - ASSERT_TRUE(bar_file_ != NULL); + ASSERT_TRUE(bar_file_ != nullptr); baz_file_ = pool_.BuildFile(baz_file); - ASSERT_TRUE(baz_file_ != NULL); + ASSERT_TRUE(baz_file_ != nullptr); ASSERT_EQ(1, foo_file_->message_type_count()); foo_message_ = foo_file_->message_type(0); @@ -332,17 +380,17 @@ class FileDescriptorTest : public testing::Test { const FileDescriptor* bar_file_; const FileDescriptor* baz_file_; - const Descriptor* foo_message_; - const EnumDescriptor* foo_enum_; + const Descriptor* foo_message_; + const EnumDescriptor* foo_enum_; const EnumValueDescriptor* foo_enum_value_; - const ServiceDescriptor* foo_service_; - const FieldDescriptor* foo_extension_; + const ServiceDescriptor* foo_service_; + const FieldDescriptor* foo_extension_; - const Descriptor* bar_message_; - const EnumDescriptor* bar_enum_; + const Descriptor* bar_message_; + const EnumDescriptor* bar_enum_; const EnumValueDescriptor* bar_enum_value_; - const ServiceDescriptor* bar_service_; - const FieldDescriptor* bar_extension_; + const ServiceDescriptor* bar_service_; + const FieldDescriptor* bar_extension_; }; TEST_F(FileDescriptorTest, Name) { @@ -366,67 +414,67 @@ TEST_F(FileDescriptorTest, FindMessageTypeByName) { EXPECT_EQ(foo_message_, foo_file_->FindMessageTypeByName("FooMessage")); EXPECT_EQ(bar_message_, bar_file_->FindMessageTypeByName("BarMessage")); - EXPECT_TRUE(foo_file_->FindMessageTypeByName("BarMessage") == NULL); - EXPECT_TRUE(bar_file_->FindMessageTypeByName("FooMessage") == NULL); - EXPECT_TRUE(baz_file_->FindMessageTypeByName("FooMessage") == NULL); + EXPECT_TRUE(foo_file_->FindMessageTypeByName("BarMessage") == nullptr); + EXPECT_TRUE(bar_file_->FindMessageTypeByName("FooMessage") == nullptr); + EXPECT_TRUE(baz_file_->FindMessageTypeByName("FooMessage") == nullptr); - EXPECT_TRUE(foo_file_->FindMessageTypeByName("NoSuchMessage") == NULL); - EXPECT_TRUE(foo_file_->FindMessageTypeByName("FooEnum") == NULL); + EXPECT_TRUE(foo_file_->FindMessageTypeByName("NoSuchMessage") == nullptr); + EXPECT_TRUE(foo_file_->FindMessageTypeByName("FooEnum") == nullptr); } TEST_F(FileDescriptorTest, FindEnumTypeByName) { EXPECT_EQ(foo_enum_, foo_file_->FindEnumTypeByName("FooEnum")); EXPECT_EQ(bar_enum_, bar_file_->FindEnumTypeByName("BarEnum")); - EXPECT_TRUE(foo_file_->FindEnumTypeByName("BarEnum") == NULL); - EXPECT_TRUE(bar_file_->FindEnumTypeByName("FooEnum") == NULL); - EXPECT_TRUE(baz_file_->FindEnumTypeByName("FooEnum") == NULL); + EXPECT_TRUE(foo_file_->FindEnumTypeByName("BarEnum") == nullptr); + EXPECT_TRUE(bar_file_->FindEnumTypeByName("FooEnum") == nullptr); + EXPECT_TRUE(baz_file_->FindEnumTypeByName("FooEnum") == nullptr); - EXPECT_TRUE(foo_file_->FindEnumTypeByName("NoSuchEnum") == NULL); - EXPECT_TRUE(foo_file_->FindEnumTypeByName("FooMessage") == NULL); + EXPECT_TRUE(foo_file_->FindEnumTypeByName("NoSuchEnum") == nullptr); + EXPECT_TRUE(foo_file_->FindEnumTypeByName("FooMessage") == nullptr); } TEST_F(FileDescriptorTest, FindEnumValueByName) { EXPECT_EQ(foo_enum_value_, foo_file_->FindEnumValueByName("FOO_ENUM_VALUE")); EXPECT_EQ(bar_enum_value_, bar_file_->FindEnumValueByName("BAR_ENUM_VALUE")); - EXPECT_TRUE(foo_file_->FindEnumValueByName("BAR_ENUM_VALUE") == NULL); - EXPECT_TRUE(bar_file_->FindEnumValueByName("FOO_ENUM_VALUE") == NULL); - EXPECT_TRUE(baz_file_->FindEnumValueByName("FOO_ENUM_VALUE") == NULL); + EXPECT_TRUE(foo_file_->FindEnumValueByName("BAR_ENUM_VALUE") == nullptr); + EXPECT_TRUE(bar_file_->FindEnumValueByName("FOO_ENUM_VALUE") == nullptr); + EXPECT_TRUE(baz_file_->FindEnumValueByName("FOO_ENUM_VALUE") == nullptr); - EXPECT_TRUE(foo_file_->FindEnumValueByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(foo_file_->FindEnumValueByName("FooMessage") == NULL); + EXPECT_TRUE(foo_file_->FindEnumValueByName("NO_SUCH_VALUE") == nullptr); + EXPECT_TRUE(foo_file_->FindEnumValueByName("FooMessage") == nullptr); } TEST_F(FileDescriptorTest, FindServiceByName) { EXPECT_EQ(foo_service_, foo_file_->FindServiceByName("FooService")); EXPECT_EQ(bar_service_, bar_file_->FindServiceByName("BarService")); - EXPECT_TRUE(foo_file_->FindServiceByName("BarService") == NULL); - EXPECT_TRUE(bar_file_->FindServiceByName("FooService") == NULL); - EXPECT_TRUE(baz_file_->FindServiceByName("FooService") == NULL); + EXPECT_TRUE(foo_file_->FindServiceByName("BarService") == nullptr); + EXPECT_TRUE(bar_file_->FindServiceByName("FooService") == nullptr); + EXPECT_TRUE(baz_file_->FindServiceByName("FooService") == nullptr); - EXPECT_TRUE(foo_file_->FindServiceByName("NoSuchService") == NULL); - EXPECT_TRUE(foo_file_->FindServiceByName("FooMessage") == NULL); + EXPECT_TRUE(foo_file_->FindServiceByName("NoSuchService") == nullptr); + EXPECT_TRUE(foo_file_->FindServiceByName("FooMessage") == nullptr); } TEST_F(FileDescriptorTest, FindExtensionByName) { EXPECT_EQ(foo_extension_, foo_file_->FindExtensionByName("foo_extension")); EXPECT_EQ(bar_extension_, bar_file_->FindExtensionByName("bar_extension")); - EXPECT_TRUE(foo_file_->FindExtensionByName("bar_extension") == NULL); - EXPECT_TRUE(bar_file_->FindExtensionByName("foo_extension") == NULL); - EXPECT_TRUE(baz_file_->FindExtensionByName("foo_extension") == NULL); + EXPECT_TRUE(foo_file_->FindExtensionByName("bar_extension") == nullptr); + EXPECT_TRUE(bar_file_->FindExtensionByName("foo_extension") == nullptr); + EXPECT_TRUE(baz_file_->FindExtensionByName("foo_extension") == nullptr); - EXPECT_TRUE(foo_file_->FindExtensionByName("no_such_extension") == NULL); - EXPECT_TRUE(foo_file_->FindExtensionByName("FooMessage") == NULL); + EXPECT_TRUE(foo_file_->FindExtensionByName("no_such_extension") == nullptr); + EXPECT_TRUE(foo_file_->FindExtensionByName("FooMessage") == nullptr); } TEST_F(FileDescriptorTest, FindExtensionByNumber) { EXPECT_EQ(foo_extension_, pool_.FindExtensionByNumber(foo_message_, 1)); EXPECT_EQ(bar_extension_, pool_.FindExtensionByNumber(bar_message_, 1)); - EXPECT_TRUE(pool_.FindExtensionByNumber(foo_message_, 2) == NULL); + EXPECT_TRUE(pool_.FindExtensionByNumber(foo_message_, 2) == nullptr); } @@ -439,7 +487,7 @@ TEST_F(FileDescriptorTest, BuildAgain) { // But if we change the file then it won't work. file.set_package("some.other.package"); - EXPECT_TRUE(pool_.BuildFile(file) == NULL); + EXPECT_TRUE(pool_.BuildFile(file) == nullptr); } TEST_F(FileDescriptorTest, BuildAgainWithSyntax) { @@ -450,7 +498,7 @@ TEST_F(FileDescriptorTest, BuildAgainWithSyntax) { proto_syntax2.set_syntax("proto2"); const FileDescriptor* proto2_descriptor = pool_.BuildFile(proto_syntax2); - EXPECT_TRUE(proto2_descriptor != NULL); + EXPECT_TRUE(proto2_descriptor != nullptr); EXPECT_EQ(proto2_descriptor, pool_.BuildFile(proto_syntax2)); FileDescriptorProto implicit_proto2; @@ -458,7 +506,7 @@ TEST_F(FileDescriptorTest, BuildAgainWithSyntax) { const FileDescriptor* implicit_proto2_descriptor = pool_.BuildFile(implicit_proto2); - EXPECT_TRUE(implicit_proto2_descriptor != NULL); + EXPECT_TRUE(implicit_proto2_descriptor != nullptr); // We get the same FileDescriptor back if syntax param is explicitly // specified. implicit_proto2.set_syntax("proto2"); @@ -469,7 +517,7 @@ TEST_F(FileDescriptorTest, BuildAgainWithSyntax) { proto_syntax3.set_syntax("proto3"); const FileDescriptor* proto3_descriptor = pool_.BuildFile(proto_syntax3); - EXPECT_TRUE(proto3_descriptor != NULL); + EXPECT_TRUE(proto3_descriptor != nullptr); EXPECT_EQ(proto3_descriptor, pool_.BuildFile(proto_syntax3)); } @@ -482,7 +530,7 @@ TEST_F(FileDescriptorTest, Syntax) { proto.set_syntax("proto2"); DescriptorPool pool; const FileDescriptor* file = pool.BuildFile(proto); - EXPECT_TRUE(file != NULL); + EXPECT_TRUE(file != nullptr); EXPECT_EQ(FileDescriptor::SYNTAX_PROTO2, file->syntax()); FileDescriptorProto other; file->CopyTo(&other); @@ -493,7 +541,7 @@ TEST_F(FileDescriptorTest, Syntax) { proto.set_syntax("proto3"); DescriptorPool pool; const FileDescriptor* file = pool.BuildFile(proto); - EXPECT_TRUE(file != NULL); + EXPECT_TRUE(file != nullptr); EXPECT_EQ(FileDescriptor::SYNTAX_PROTO3, file->syntax()); FileDescriptorProto other; file->CopyTo(&other); @@ -502,8 +550,8 @@ TEST_F(FileDescriptorTest, Syntax) { } void ExtractDebugString( - const FileDescriptor* file, std::set* visited, - std::vector >* debug_strings) { + const FileDescriptor* file, std::set* visited, + std::vector>* debug_strings) { if (!visited->insert(file->name()).second) { return; } @@ -516,20 +564,20 @@ void ExtractDebugString( class SimpleErrorCollector : public io::ErrorCollector { public: // implements ErrorCollector --------------------------------------- - void AddError(int line, int column, const string& message) { + void AddError(int line, int column, const std::string& message) { last_error_ = StringPrintf("%d:%d:", line, column) + message; } - const string& last_error() { return last_error_; } + const std::string& last_error() { return last_error_; } private: - string last_error_; + std::string last_error_; }; // Test that the result of FileDescriptor::DebugString() can be used to create // the original descriptors. TEST_F(FileDescriptorTest, DebugStringRoundTrip) { - std::set visited; - std::vector > debug_strings; + std::set visited; + std::vector> debug_strings; ExtractDebugString(protobuf_unittest::TestAllTypes::descriptor()->file(), &visited, &debug_strings); ExtractDebugString( @@ -541,8 +589,8 @@ TEST_F(FileDescriptorTest, DebugStringRoundTrip) { DescriptorPool pool; for (int i = 0; i < debug_strings.size(); ++i) { - const string& name = debug_strings[i].first; - const string& content = debug_strings[i].second; + const std::string& name = debug_strings[i].first; + const std::string& content = debug_strings[i].second; io::ArrayInputStream input_stream(content.data(), content.size()); SimpleErrorCollector error_collector; io::Tokenizer tokenizer(&input_stream, &error_collector); @@ -555,7 +603,7 @@ TEST_F(FileDescriptorTest, DebugStringRoundTrip) { ASSERT_EQ("", error_collector.last_error()); proto.set_name(name); const FileDescriptor* descriptor = pool.BuildFile(proto); - ASSERT_TRUE(descriptor != NULL) << proto.DebugString(); + ASSERT_TRUE(descriptor != nullptr) << proto.DebugString(); EXPECT_EQ(content, descriptor->DebugString()); } } @@ -618,35 +666,28 @@ class DescriptorTest : public testing::Test { AddEmptyEnum(&foo_file, "TestEnum"); DescriptorProto* message = AddMessage(&foo_file, "TestMessage"); - AddField(message, "foo", 1, - FieldDescriptorProto::LABEL_REQUIRED, + AddField(message, "foo", 1, FieldDescriptorProto::LABEL_REQUIRED, FieldDescriptorProto::TYPE_STRING); - AddField(message, "bar", 6, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message, "bar", 6, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_ENUM) - ->set_type_name("TestEnum"); - AddField(message, "baz", 500000000, - FieldDescriptorProto::LABEL_REPEATED, + ->set_type_name("TestEnum"); + AddField(message, "baz", 500000000, FieldDescriptorProto::LABEL_REPEATED, FieldDescriptorProto::TYPE_MESSAGE) - ->set_type_name("TestForeign"); - AddField(message, "qux", 15, - FieldDescriptorProto::LABEL_OPTIONAL, + ->set_type_name("TestForeign"); + AddField(message, "qux", 15, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_GROUP) - ->set_type_name("TestForeign"); + ->set_type_name("TestForeign"); FileDescriptorProto bar_file; bar_file.set_name("bar.proto"); bar_file.set_package("corge.grault"); DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2"); - AddField(message2, "foo", 1, - FieldDescriptorProto::LABEL_REQUIRED, + AddField(message2, "foo", 1, FieldDescriptorProto::LABEL_REQUIRED, FieldDescriptorProto::TYPE_STRING); - AddField(message2, "bar", 2, - FieldDescriptorProto::LABEL_REQUIRED, + AddField(message2, "bar", 2, FieldDescriptorProto::LABEL_REQUIRED, FieldDescriptorProto::TYPE_STRING); - AddField(message2, "quux", 6, - FieldDescriptorProto::LABEL_REQUIRED, + AddField(message2, "quux", 6, FieldDescriptorProto::LABEL_REQUIRED, FieldDescriptorProto::TYPE_STRING); FileDescriptorProto map_file; @@ -654,11 +695,9 @@ class DescriptorTest : public testing::Test { DescriptorProto* message3 = AddMessage(&map_file, "TestMessage3"); DescriptorProto* entry = AddNestedMessage(message3, "MapInt32Int32Entry"); - AddField(entry, "key", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(entry, "key", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(entry, "value", 2, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(entry, "value", 2, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); entry->mutable_options()->set_map_entry(true); @@ -671,38 +710,32 @@ class DescriptorTest : public testing::Test { json_file.set_name("json.proto"); json_file.set_syntax("proto3"); DescriptorProto* message4 = AddMessage(&json_file, "TestMessage4"); - AddField(message4, "field_name1", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "field_name1", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "fieldName2", 2, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "fieldName2", 2, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "FieldName3", 3, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "FieldName3", 3, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "_field_name4", 4, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "_field_name4", 4, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "FIELD_NAME5", 5, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "FIELD_NAME5", 5, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "field_name6", 6, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "field_name6", 6, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32) ->set_json_name("@type"); // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); bar_file_ = pool_.BuildFile(bar_file); - ASSERT_TRUE(bar_file_ != NULL); + ASSERT_TRUE(bar_file_ != nullptr); map_file_ = pool_.BuildFile(map_file); - ASSERT_TRUE(map_file_ != NULL); + ASSERT_TRUE(map_file_ != nullptr); json_file_ = pool_.BuildFile(json_file); - ASSERT_TRUE(json_file_ != NULL); + ASSERT_TRUE(json_file_ != nullptr); ASSERT_EQ(1, foo_file_->enum_type_count()); enum_ = foo_file_->enum_type(0); @@ -721,15 +754,15 @@ class DescriptorTest : public testing::Test { message2_ = bar_file_->message_type(0); ASSERT_EQ(3, message2_->field_count()); - foo2_ = message2_->field(0); - bar2_ = message2_->field(1); + foo2_ = message2_->field(0); + bar2_ = message2_->field(1); quux2_ = message2_->field(2); ASSERT_EQ(1, map_file_->message_type_count()); message3_ = map_file_->message_type(0); ASSERT_EQ(1, message3_->field_count()); - map_ = message3_->field(0); + map_ = message3_->field(0); ASSERT_EQ(1, json_file_->message_type_count()); message4_ = json_file_->message_type(0); @@ -777,8 +810,8 @@ TEST_F(DescriptorTest, Name) { } TEST_F(DescriptorTest, ContainingType) { - EXPECT_TRUE(message_->containing_type() == NULL); - EXPECT_TRUE(message2_->containing_type() == NULL); + EXPECT_TRUE(message_->containing_type() == nullptr); + EXPECT_TRUE(message2_->containing_type() == nullptr); } TEST_F(DescriptorTest, FieldsByIndex) { @@ -799,14 +832,14 @@ TEST_F(DescriptorTest, FindFieldByName) { EXPECT_EQ(bar_, message_->FindFieldByName("bar")); EXPECT_EQ(baz_, message_->FindFieldByName("baz")); EXPECT_EQ(qux_, message_->FindFieldByName("qux")); - EXPECT_TRUE(message_->FindFieldByName("no_such_field") == NULL); - EXPECT_TRUE(message_->FindFieldByName("quux") == NULL); + EXPECT_TRUE(message_->FindFieldByName("no_such_field") == nullptr); + EXPECT_TRUE(message_->FindFieldByName("quux") == nullptr); - EXPECT_EQ(foo2_ , message2_->FindFieldByName("foo" )); - EXPECT_EQ(bar2_ , message2_->FindFieldByName("bar" )); + EXPECT_EQ(foo2_, message2_->FindFieldByName("foo")); + EXPECT_EQ(bar2_, message2_->FindFieldByName("bar")); EXPECT_EQ(quux2_, message2_->FindFieldByName("quux")); - EXPECT_TRUE(message2_->FindFieldByName("baz") == NULL); - EXPECT_TRUE(message2_->FindFieldByName("qux") == NULL); + EXPECT_TRUE(message2_->FindFieldByName("baz") == nullptr); + EXPECT_TRUE(message2_->FindFieldByName("qux") == nullptr); } TEST_F(DescriptorTest, FindFieldByNumber) { @@ -814,14 +847,14 @@ TEST_F(DescriptorTest, FindFieldByNumber) { EXPECT_EQ(bar_, message_->FindFieldByNumber(6)); EXPECT_EQ(baz_, message_->FindFieldByNumber(500000000)); EXPECT_EQ(qux_, message_->FindFieldByNumber(15)); - EXPECT_TRUE(message_->FindFieldByNumber(837592) == NULL); - EXPECT_TRUE(message_->FindFieldByNumber(2) == NULL); + EXPECT_TRUE(message_->FindFieldByNumber(837592) == nullptr); + EXPECT_TRUE(message_->FindFieldByNumber(2) == nullptr); - EXPECT_EQ(foo2_ , message2_->FindFieldByNumber(1)); - EXPECT_EQ(bar2_ , message2_->FindFieldByNumber(2)); + EXPECT_EQ(foo2_, message2_->FindFieldByNumber(1)); + EXPECT_EQ(bar2_, message2_->FindFieldByNumber(2)); EXPECT_EQ(quux2_, message2_->FindFieldByNumber(6)); - EXPECT_TRUE(message2_->FindFieldByNumber(15) == NULL); - EXPECT_TRUE(message2_->FindFieldByNumber(500000000) == NULL); + EXPECT_TRUE(message2_->FindFieldByNumber(15) == nullptr); + EXPECT_TRUE(message2_->FindFieldByNumber(500000000) == nullptr); } TEST_F(DescriptorTest, FieldName) { @@ -842,6 +875,34 @@ TEST_F(DescriptorTest, FieldFullName) { EXPECT_EQ("corge.grault.TestMessage2.quux", quux2_->full_name()); } +TEST_F(DescriptorTest, PrintableNameIsFullNameForNonExtensionFields) { + EXPECT_EQ("TestMessage.foo", foo_->PrintableNameForExtension()); + EXPECT_EQ("TestMessage.bar", bar_->PrintableNameForExtension()); + EXPECT_EQ("TestMessage.baz", baz_->PrintableNameForExtension()); + EXPECT_EQ("TestMessage.qux", qux_->PrintableNameForExtension()); + + EXPECT_EQ("corge.grault.TestMessage2.foo", + foo2_->PrintableNameForExtension()); + EXPECT_EQ("corge.grault.TestMessage2.bar", + bar2_->PrintableNameForExtension()); + EXPECT_EQ("corge.grault.TestMessage2.quux", + quux2_->PrintableNameForExtension()); +} + +TEST_F(DescriptorTest, PrintableNameIsFullNameForNonMessageSetExtension) { + EXPECT_EQ("protobuf_unittest.Aggregate.nested", + protobuf_unittest::Aggregate::descriptor() + ->FindExtensionByName("nested") + ->PrintableNameForExtension()); +} + +TEST_F(DescriptorTest, PrintableNameIsExtendingTypeForMessageSetExtension) { + EXPECT_EQ("protobuf_unittest.AggregateMessageSetElement", + protobuf_unittest::AggregateMessageSetElement::descriptor() + ->FindExtensionByName("message_set_extension") + ->PrintableNameForExtension()); +} + TEST_F(DescriptorTest, FieldJsonName) { EXPECT_EQ("fieldName1", message4_->field(0)->json_name()); EXPECT_EQ("fieldName2", message4_->field(1)->json_name()); @@ -900,17 +961,17 @@ TEST_F(DescriptorTest, FieldIndex) { } TEST_F(DescriptorTest, FieldNumber) { - EXPECT_EQ( 1, foo_->number()); - EXPECT_EQ( 6, bar_->number()); + EXPECT_EQ(1, foo_->number()); + EXPECT_EQ(6, bar_->number()); EXPECT_EQ(500000000, baz_->number()); - EXPECT_EQ( 15, qux_->number()); + EXPECT_EQ(15, qux_->number()); } TEST_F(DescriptorTest, FieldType) { - EXPECT_EQ(FieldDescriptor::TYPE_STRING , foo_->type()); - EXPECT_EQ(FieldDescriptor::TYPE_ENUM , bar_->type()); + EXPECT_EQ(FieldDescriptor::TYPE_STRING, foo_->type()); + EXPECT_EQ(FieldDescriptor::TYPE_ENUM, bar_->type()); EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, baz_->type()); - EXPECT_EQ(FieldDescriptor::TYPE_GROUP , qux_->type()); + EXPECT_EQ(FieldDescriptor::TYPE_GROUP, qux_->type()); } TEST_F(DescriptorTest, FieldLabel) { @@ -919,17 +980,17 @@ TEST_F(DescriptorTest, FieldLabel) { EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, baz_->label()); EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, qux_->label()); - EXPECT_TRUE (foo_->is_required()); + EXPECT_TRUE(foo_->is_required()); EXPECT_FALSE(foo_->is_optional()); EXPECT_FALSE(foo_->is_repeated()); EXPECT_FALSE(bar_->is_required()); - EXPECT_TRUE (bar_->is_optional()); + EXPECT_TRUE(bar_->is_optional()); EXPECT_FALSE(bar_->is_repeated()); EXPECT_FALSE(baz_->is_required()); EXPECT_FALSE(baz_->is_optional()); - EXPECT_TRUE (baz_->is_repeated()); + EXPECT_TRUE(baz_->is_repeated()); } TEST_F(DescriptorTest, IsMap) { @@ -951,23 +1012,23 @@ TEST_F(DescriptorTest, FieldContainingType) { EXPECT_EQ(message_, baz_->containing_type()); EXPECT_EQ(message_, qux_->containing_type()); - EXPECT_EQ(message2_, foo2_ ->containing_type()); - EXPECT_EQ(message2_, bar2_ ->containing_type()); + EXPECT_EQ(message2_, foo2_->containing_type()); + EXPECT_EQ(message2_, bar2_->containing_type()); EXPECT_EQ(message2_, quux2_->containing_type()); } TEST_F(DescriptorTest, FieldMessageType) { - EXPECT_TRUE(foo_->message_type() == NULL); - EXPECT_TRUE(bar_->message_type() == NULL); + EXPECT_TRUE(foo_->message_type() == nullptr); + EXPECT_TRUE(bar_->message_type() == nullptr); EXPECT_EQ(foreign_, baz_->message_type()); EXPECT_EQ(foreign_, qux_->message_type()); } TEST_F(DescriptorTest, FieldEnumType) { - EXPECT_TRUE(foo_->enum_type() == NULL); - EXPECT_TRUE(baz_->enum_type() == NULL); - EXPECT_TRUE(qux_->enum_type() == NULL); + EXPECT_TRUE(foo_->enum_type() == nullptr); + EXPECT_TRUE(baz_->enum_type() == nullptr); + EXPECT_TRUE(qux_->enum_type() == nullptr); EXPECT_EQ(enum_, bar_->enum_type()); } @@ -1001,27 +1062,23 @@ class OneofDescriptorTest : public testing::Test { oneof_message->add_oneof_decl()->set_name("foo"); oneof_message->add_oneof_decl()->set_name("bar"); - AddField(oneof_message, "a", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(oneof_message, "a", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(oneof_message, "b", 2, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(oneof_message, "b", 2, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_STRING); oneof_message->mutable_field(1)->set_oneof_index(0); - AddField(oneof_message, "c", 3, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(oneof_message, "c", 3, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_MESSAGE); oneof_message->mutable_field(2)->set_oneof_index(0); oneof_message->mutable_field(2)->set_type_name("TestOneof"); - AddField(oneof_message, "d", 4, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(oneof_message, "d", 4, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_FLOAT); oneof_message->mutable_field(3)->set_oneof_index(1); // Build the descriptors and get the pointers. baz_file_ = pool_.BuildFile(baz_file); - ASSERT_TRUE(baz_file_ != NULL); + ASSERT_TRUE(baz_file_ != nullptr); ASSERT_EQ(1, baz_file_->message_type_count()); oneof_message_ = baz_file_->message_type(0); @@ -1049,8 +1106,6 @@ class OneofDescriptorTest : public testing::Test { const FieldDescriptor* b_; const FieldDescriptor* c_; const FieldDescriptor* d_; - const FieldDescriptor* e_; - const FieldDescriptor* f_; }; TEST_F(OneofDescriptorTest, Normal) { @@ -1060,7 +1115,7 @@ TEST_F(OneofDescriptorTest, Normal) { ASSERT_EQ(2, oneof_->field_count()); EXPECT_EQ(b_, oneof_->field(0)); EXPECT_EQ(c_, oneof_->field(1)); - EXPECT_TRUE(a_->containing_oneof() == NULL); + EXPECT_TRUE(a_->containing_oneof() == nullptr); EXPECT_EQ(oneof_, b_->containing_oneof()); EXPECT_EQ(oneof_, c_->containing_oneof()); } @@ -1068,7 +1123,7 @@ TEST_F(OneofDescriptorTest, Normal) { TEST_F(OneofDescriptorTest, FindByName) { EXPECT_EQ(oneof_, oneof_message_->FindOneofByName("foo")); EXPECT_EQ(oneof2_, oneof_message_->FindOneofByName("bar")); - EXPECT_TRUE(oneof_message_->FindOneofByName("no_such_oneof") == NULL); + EXPECT_TRUE(oneof_message_->FindOneofByName("no_such_oneof") == nullptr); } // =================================================================== @@ -1082,14 +1137,11 @@ class StylizedFieldNamesTest : public testing::Test { AddExtensionRange(AddMessage(&file, "ExtendableMessage"), 1, 1000); DescriptorProto* message = AddMessage(&file, "TestMessage"); - AddField(message, "foo_foo", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message, "foo_foo", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message, "FooBar", 2, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message, "FooBar", 2, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message, "fooBaz", 3, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message, "fooBaz", 3, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); AddField(message, "fooFoo", 4, // Camel-case conflict with foo_foo. FieldDescriptorProto::LABEL_OPTIONAL, @@ -1131,7 +1183,7 @@ class StylizedFieldNamesTest : public testing::Test { FieldDescriptorProto::TYPE_INT32); file_ = pool_.BuildFile(file); - ASSERT_TRUE(file_ != NULL); + ASSERT_TRUE(file_ != nullptr); ASSERT_EQ(2, file_->message_type_count()); message_ = file_->message_type(1); ASSERT_EQ("TestMessage", message_->name()); @@ -1147,22 +1199,22 @@ class StylizedFieldNamesTest : public testing::Test { TEST_F(StylizedFieldNamesTest, LowercaseName) { EXPECT_EQ("foo_foo", message_->field(0)->lowercase_name()); - EXPECT_EQ("foobar" , message_->field(1)->lowercase_name()); - EXPECT_EQ("foobaz" , message_->field(2)->lowercase_name()); - EXPECT_EQ("foofoo" , message_->field(3)->lowercase_name()); - EXPECT_EQ("foobar" , message_->field(4)->lowercase_name()); + EXPECT_EQ("foobar", message_->field(1)->lowercase_name()); + EXPECT_EQ("foobaz", message_->field(2)->lowercase_name()); + EXPECT_EQ("foofoo", message_->field(3)->lowercase_name()); + EXPECT_EQ("foobar", message_->field(4)->lowercase_name()); EXPECT_EQ("bar_foo", message_->extension(0)->lowercase_name()); - EXPECT_EQ("barbar" , message_->extension(1)->lowercase_name()); - EXPECT_EQ("barbaz" , message_->extension(2)->lowercase_name()); - EXPECT_EQ("barfoo" , message_->extension(3)->lowercase_name()); - EXPECT_EQ("barbar" , message_->extension(4)->lowercase_name()); + EXPECT_EQ("barbar", message_->extension(1)->lowercase_name()); + EXPECT_EQ("barbaz", message_->extension(2)->lowercase_name()); + EXPECT_EQ("barfoo", message_->extension(3)->lowercase_name()); + EXPECT_EQ("barbar", message_->extension(4)->lowercase_name()); EXPECT_EQ("baz_foo", file_->extension(0)->lowercase_name()); - EXPECT_EQ("bazbar" , file_->extension(1)->lowercase_name()); - EXPECT_EQ("bazbaz" , file_->extension(2)->lowercase_name()); - EXPECT_EQ("bazfoo" , file_->extension(3)->lowercase_name()); - EXPECT_EQ("bazbar" , file_->extension(4)->lowercase_name()); + EXPECT_EQ("bazbar", file_->extension(1)->lowercase_name()); + EXPECT_EQ("bazbaz", file_->extension(2)->lowercase_name()); + EXPECT_EQ("bazfoo", file_->extension(3)->lowercase_name()); + EXPECT_EQ("bazbar", file_->extension(4)->lowercase_name()); } TEST_F(StylizedFieldNamesTest, CamelcaseName) { @@ -1186,16 +1238,13 @@ TEST_F(StylizedFieldNamesTest, CamelcaseName) { } TEST_F(StylizedFieldNamesTest, FindByLowercaseName) { - EXPECT_EQ(message_->field(0), - message_->FindFieldByLowercaseName("foo_foo")); - EXPECT_EQ(message_->field(1), - message_->FindFieldByLowercaseName("foobar")); - EXPECT_EQ(message_->field(2), - message_->FindFieldByLowercaseName("foobaz")); - EXPECT_TRUE(message_->FindFieldByLowercaseName("FooBar") == NULL); - EXPECT_TRUE(message_->FindFieldByLowercaseName("fooBaz") == NULL); - EXPECT_TRUE(message_->FindFieldByLowercaseName("bar_foo") == NULL); - EXPECT_TRUE(message_->FindFieldByLowercaseName("nosuchfield") == NULL); + EXPECT_EQ(message_->field(0), message_->FindFieldByLowercaseName("foo_foo")); + EXPECT_EQ(message_->field(1), message_->FindFieldByLowercaseName("foobar")); + EXPECT_EQ(message_->field(2), message_->FindFieldByLowercaseName("foobaz")); + EXPECT_TRUE(message_->FindFieldByLowercaseName("FooBar") == nullptr); + EXPECT_TRUE(message_->FindFieldByLowercaseName("fooBaz") == nullptr); + EXPECT_TRUE(message_->FindFieldByLowercaseName("bar_foo") == nullptr); + EXPECT_TRUE(message_->FindFieldByLowercaseName("nosuchfield") == nullptr); EXPECT_EQ(message_->extension(0), message_->FindExtensionByLowercaseName("bar_foo")); @@ -1203,33 +1252,28 @@ TEST_F(StylizedFieldNamesTest, FindByLowercaseName) { message_->FindExtensionByLowercaseName("barbar")); EXPECT_EQ(message_->extension(2), message_->FindExtensionByLowercaseName("barbaz")); - EXPECT_TRUE(message_->FindExtensionByLowercaseName("BarBar") == NULL); - EXPECT_TRUE(message_->FindExtensionByLowercaseName("barBaz") == NULL); - EXPECT_TRUE(message_->FindExtensionByLowercaseName("foo_foo") == NULL); - EXPECT_TRUE(message_->FindExtensionByLowercaseName("nosuchfield") == NULL); + EXPECT_TRUE(message_->FindExtensionByLowercaseName("BarBar") == nullptr); + EXPECT_TRUE(message_->FindExtensionByLowercaseName("barBaz") == nullptr); + EXPECT_TRUE(message_->FindExtensionByLowercaseName("foo_foo") == nullptr); + EXPECT_TRUE(message_->FindExtensionByLowercaseName("nosuchfield") == nullptr); EXPECT_EQ(file_->extension(0), file_->FindExtensionByLowercaseName("baz_foo")); - EXPECT_EQ(file_->extension(1), - file_->FindExtensionByLowercaseName("bazbar")); - EXPECT_EQ(file_->extension(2), - file_->FindExtensionByLowercaseName("bazbaz")); - EXPECT_TRUE(file_->FindExtensionByLowercaseName("BazBar") == NULL); - EXPECT_TRUE(file_->FindExtensionByLowercaseName("bazBaz") == NULL); - EXPECT_TRUE(file_->FindExtensionByLowercaseName("nosuchfield") == NULL); + EXPECT_EQ(file_->extension(1), file_->FindExtensionByLowercaseName("bazbar")); + EXPECT_EQ(file_->extension(2), file_->FindExtensionByLowercaseName("bazbaz")); + EXPECT_TRUE(file_->FindExtensionByLowercaseName("BazBar") == nullptr); + EXPECT_TRUE(file_->FindExtensionByLowercaseName("bazBaz") == nullptr); + EXPECT_TRUE(file_->FindExtensionByLowercaseName("nosuchfield") == nullptr); } TEST_F(StylizedFieldNamesTest, FindByCamelcaseName) { - EXPECT_EQ(message_->field(0), - message_->FindFieldByCamelcaseName("fooFoo")); - EXPECT_EQ(message_->field(1), - message_->FindFieldByCamelcaseName("fooBar")); - EXPECT_EQ(message_->field(2), - message_->FindFieldByCamelcaseName("fooBaz")); - EXPECT_TRUE(message_->FindFieldByCamelcaseName("foo_foo") == NULL); - EXPECT_TRUE(message_->FindFieldByCamelcaseName("FooBar") == NULL); - EXPECT_TRUE(message_->FindFieldByCamelcaseName("barFoo") == NULL); - EXPECT_TRUE(message_->FindFieldByCamelcaseName("nosuchfield") == NULL); + EXPECT_EQ(message_->field(0), message_->FindFieldByCamelcaseName("fooFoo")); + EXPECT_EQ(message_->field(1), message_->FindFieldByCamelcaseName("fooBar")); + EXPECT_EQ(message_->field(2), message_->FindFieldByCamelcaseName("fooBaz")); + EXPECT_TRUE(message_->FindFieldByCamelcaseName("foo_foo") == nullptr); + EXPECT_TRUE(message_->FindFieldByCamelcaseName("FooBar") == nullptr); + EXPECT_TRUE(message_->FindFieldByCamelcaseName("barFoo") == nullptr); + EXPECT_TRUE(message_->FindFieldByCamelcaseName("nosuchfield") == nullptr); EXPECT_EQ(message_->extension(0), message_->FindExtensionByCamelcaseName("barFoo")); @@ -1237,20 +1281,17 @@ TEST_F(StylizedFieldNamesTest, FindByCamelcaseName) { message_->FindExtensionByCamelcaseName("barBar")); EXPECT_EQ(message_->extension(2), message_->FindExtensionByCamelcaseName("barBaz")); - EXPECT_TRUE(message_->FindExtensionByCamelcaseName("bar_foo") == NULL); - EXPECT_TRUE(message_->FindExtensionByCamelcaseName("BarBar") == NULL); - EXPECT_TRUE(message_->FindExtensionByCamelcaseName("fooFoo") == NULL); - EXPECT_TRUE(message_->FindExtensionByCamelcaseName("nosuchfield") == NULL); + EXPECT_TRUE(message_->FindExtensionByCamelcaseName("bar_foo") == nullptr); + EXPECT_TRUE(message_->FindExtensionByCamelcaseName("BarBar") == nullptr); + EXPECT_TRUE(message_->FindExtensionByCamelcaseName("fooFoo") == nullptr); + EXPECT_TRUE(message_->FindExtensionByCamelcaseName("nosuchfield") == nullptr); - EXPECT_EQ(file_->extension(0), - file_->FindExtensionByCamelcaseName("bazFoo")); - EXPECT_EQ(file_->extension(1), - file_->FindExtensionByCamelcaseName("bazBar")); - EXPECT_EQ(file_->extension(2), - file_->FindExtensionByCamelcaseName("bazBaz")); - EXPECT_TRUE(file_->FindExtensionByCamelcaseName("baz_foo") == NULL); - EXPECT_TRUE(file_->FindExtensionByCamelcaseName("BazBar") == NULL); - EXPECT_TRUE(file_->FindExtensionByCamelcaseName("nosuchfield") == NULL); + EXPECT_EQ(file_->extension(0), file_->FindExtensionByCamelcaseName("bazFoo")); + EXPECT_EQ(file_->extension(1), file_->FindExtensionByCamelcaseName("bazBar")); + EXPECT_EQ(file_->extension(2), file_->FindExtensionByCamelcaseName("bazBaz")); + EXPECT_TRUE(file_->FindExtensionByCamelcaseName("baz_foo") == nullptr); + EXPECT_TRUE(file_->FindExtensionByCamelcaseName("BazBar") == nullptr); + EXPECT_TRUE(file_->FindExtensionByCamelcaseName("nosuchfield") == nullptr); } // =================================================================== @@ -1297,10 +1338,10 @@ class EnumDescriptorTest : public testing::Test { // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); bar_file_ = pool_.BuildFile(bar_file); - ASSERT_TRUE(bar_file_ != NULL); + ASSERT_TRUE(bar_file_ != nullptr); ASSERT_EQ(1, foo_file_->enum_type_count()); enum_ = foo_file_->enum_type(0); @@ -1343,8 +1384,8 @@ TEST_F(EnumDescriptorTest, Name) { } TEST_F(EnumDescriptorTest, ContainingType) { - EXPECT_TRUE(enum_->containing_type() == NULL); - EXPECT_TRUE(enum2_->containing_type() == NULL); + EXPECT_TRUE(enum_->containing_type() == nullptr); + EXPECT_TRUE(enum2_->containing_type() == nullptr); } TEST_F(EnumDescriptorTest, ValuesByIndex) { @@ -1354,25 +1395,25 @@ TEST_F(EnumDescriptorTest, ValuesByIndex) { } TEST_F(EnumDescriptorTest, FindValueByName) { - EXPECT_EQ(foo_ , enum_ ->FindValueByName("FOO")); - EXPECT_EQ(bar_ , enum_ ->FindValueByName("BAR")); + EXPECT_EQ(foo_, enum_->FindValueByName("FOO")); + EXPECT_EQ(bar_, enum_->FindValueByName("BAR")); EXPECT_EQ(foo2_, enum2_->FindValueByName("FOO")); EXPECT_EQ(baz2_, enum2_->FindValueByName("BAZ")); - EXPECT_TRUE(enum_ ->FindValueByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(enum_ ->FindValueByName("BAZ" ) == NULL); - EXPECT_TRUE(enum2_->FindValueByName("BAR" ) == NULL); + EXPECT_TRUE(enum_->FindValueByName("NO_SUCH_VALUE") == nullptr); + EXPECT_TRUE(enum_->FindValueByName("BAZ") == nullptr); + EXPECT_TRUE(enum2_->FindValueByName("BAR") == nullptr); } TEST_F(EnumDescriptorTest, FindValueByNumber) { - EXPECT_EQ(foo_ , enum_ ->FindValueByNumber(1)); - EXPECT_EQ(bar_ , enum_ ->FindValueByNumber(2)); + EXPECT_EQ(foo_, enum_->FindValueByNumber(1)); + EXPECT_EQ(bar_, enum_->FindValueByNumber(2)); EXPECT_EQ(foo2_, enum2_->FindValueByNumber(1)); EXPECT_EQ(baz2_, enum2_->FindValueByNumber(3)); - EXPECT_TRUE(enum_ ->FindValueByNumber(416) == NULL); - EXPECT_TRUE(enum_ ->FindValueByNumber(3) == NULL); - EXPECT_TRUE(enum2_->FindValueByNumber(2) == NULL); + EXPECT_TRUE(enum_->FindValueByNumber(416) == nullptr); + EXPECT_TRUE(enum_->FindValueByNumber(3) == nullptr); + EXPECT_TRUE(enum2_->FindValueByNumber(2) == nullptr); } TEST_F(EnumDescriptorTest, ValueName) { @@ -1398,8 +1439,8 @@ TEST_F(EnumDescriptorTest, ValueNumber) { } TEST_F(EnumDescriptorTest, ValueType) { - EXPECT_EQ(enum_ , foo_ ->type()); - EXPECT_EQ(enum_ , bar_ ->type()); + EXPECT_EQ(enum_, foo_->type()); + EXPECT_EQ(enum_, bar_->type()); EXPECT_EQ(enum2_, foo2_->type()); EXPECT_EQ(enum2_, baz2_->type()); } @@ -1456,17 +1497,17 @@ class ServiceDescriptorTest : public testing::Test { // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); bar_file_ = pool_.BuildFile(bar_file); - ASSERT_TRUE(bar_file_ != NULL); + ASSERT_TRUE(bar_file_ != nullptr); ASSERT_EQ(6, foo_file_->message_type_count()); - foo_request_ = foo_file_->message_type(0); + foo_request_ = foo_file_->message_type(0); foo_response_ = foo_file_->message_type(1); - bar_request_ = foo_file_->message_type(2); + bar_request_ = foo_file_->message_type(2); bar_response_ = foo_file_->message_type(3); - baz_request_ = foo_file_->message_type(4); + baz_request_ = foo_file_->message_type(4); baz_response_ = foo_file_->message_type(5); ASSERT_EQ(1, foo_file_->service_count()); @@ -1523,14 +1564,14 @@ TEST_F(ServiceDescriptorTest, MethodsByIndex) { } TEST_F(ServiceDescriptorTest, FindMethodByName) { - EXPECT_EQ(foo_ , service_ ->FindMethodByName("Foo")); - EXPECT_EQ(bar_ , service_ ->FindMethodByName("Bar")); + EXPECT_EQ(foo_, service_->FindMethodByName("Foo")); + EXPECT_EQ(bar_, service_->FindMethodByName("Bar")); EXPECT_EQ(foo2_, service2_->FindMethodByName("Foo")); EXPECT_EQ(baz2_, service2_->FindMethodByName("Baz")); - EXPECT_TRUE(service_ ->FindMethodByName("NoSuchMethod") == NULL); - EXPECT_TRUE(service_ ->FindMethodByName("Baz" ) == NULL); - EXPECT_TRUE(service2_->FindMethodByName("Bar" ) == NULL); + EXPECT_TRUE(service_->FindMethodByName("NoSuchMethod") == nullptr); + EXPECT_TRUE(service_->FindMethodByName("Baz") == nullptr); + EXPECT_TRUE(service2_->FindMethodByName("Bar") == nullptr); } TEST_F(ServiceDescriptorTest, MethodName) { @@ -1622,10 +1663,10 @@ class NestedDescriptorTest : public testing::Test { // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); bar_file_ = pool_.BuildFile(bar_file); - ASSERT_TRUE(bar_file_ != NULL); + ASSERT_TRUE(bar_file_ != nullptr); ASSERT_EQ(1, foo_file_->message_type_count()); message_ = foo_file_->message_type(0); @@ -1684,8 +1725,8 @@ class NestedDescriptorTest : public testing::Test { }; TEST_F(NestedDescriptorTest, MessageName) { - EXPECT_EQ("Foo", foo_ ->name()); - EXPECT_EQ("Bar", bar_ ->name()); + EXPECT_EQ("Foo", foo_->name()); + EXPECT_EQ("Bar", bar_->name()); EXPECT_EQ("Foo", foo2_->name()); EXPECT_EQ("Baz", baz2_->name()); @@ -1696,8 +1737,8 @@ TEST_F(NestedDescriptorTest, MessageName) { } TEST_F(NestedDescriptorTest, MessageContainingType) { - EXPECT_EQ(message_ , foo_ ->containing_type()); - EXPECT_EQ(message_ , bar_ ->containing_type()); + EXPECT_EQ(message_, foo_->containing_type()); + EXPECT_EQ(message_, bar_->containing_type()); EXPECT_EQ(message2_, foo2_->containing_type()); EXPECT_EQ(message2_, baz2_->containing_type()); } @@ -1709,41 +1750,41 @@ TEST_F(NestedDescriptorTest, NestedMessagesByIndex) { } TEST_F(NestedDescriptorTest, FindFieldByNameDoesntFindNestedTypes) { - EXPECT_TRUE(message_->FindFieldByName("Foo") == NULL); - EXPECT_TRUE(message_->FindFieldByName("Qux") == NULL); - EXPECT_TRUE(message_->FindExtensionByName("Foo") == NULL); - EXPECT_TRUE(message_->FindExtensionByName("Qux") == NULL); + EXPECT_TRUE(message_->FindFieldByName("Foo") == nullptr); + EXPECT_TRUE(message_->FindFieldByName("Qux") == nullptr); + EXPECT_TRUE(message_->FindExtensionByName("Foo") == nullptr); + EXPECT_TRUE(message_->FindExtensionByName("Qux") == nullptr); } TEST_F(NestedDescriptorTest, FindNestedTypeByName) { - EXPECT_EQ(foo_ , message_ ->FindNestedTypeByName("Foo")); - EXPECT_EQ(bar_ , message_ ->FindNestedTypeByName("Bar")); + EXPECT_EQ(foo_, message_->FindNestedTypeByName("Foo")); + EXPECT_EQ(bar_, message_->FindNestedTypeByName("Bar")); EXPECT_EQ(foo2_, message2_->FindNestedTypeByName("Foo")); EXPECT_EQ(baz2_, message2_->FindNestedTypeByName("Baz")); - EXPECT_TRUE(message_ ->FindNestedTypeByName("NoSuchType") == NULL); - EXPECT_TRUE(message_ ->FindNestedTypeByName("Baz" ) == NULL); - EXPECT_TRUE(message2_->FindNestedTypeByName("Bar" ) == NULL); + EXPECT_TRUE(message_->FindNestedTypeByName("NoSuchType") == nullptr); + EXPECT_TRUE(message_->FindNestedTypeByName("Baz") == nullptr); + EXPECT_TRUE(message2_->FindNestedTypeByName("Bar") == nullptr); - EXPECT_TRUE(message_->FindNestedTypeByName("Qux") == NULL); + EXPECT_TRUE(message_->FindNestedTypeByName("Qux") == nullptr); } TEST_F(NestedDescriptorTest, EnumName) { - EXPECT_EQ("Baz" , baz_ ->name()); - EXPECT_EQ("Qux" , qux_ ->name()); - EXPECT_EQ("Qux" , qux2_->name()); + EXPECT_EQ("Baz", baz_->name()); + EXPECT_EQ("Qux", qux_->name()); + EXPECT_EQ("Qux", qux2_->name()); EXPECT_EQ("Quux", quux2_->name()); EXPECT_EQ("TestMessage.Baz", baz_->full_name()); EXPECT_EQ("TestMessage.Qux", qux_->full_name()); - EXPECT_EQ("corge.grault.TestMessage2.Qux" , qux2_ ->full_name()); + EXPECT_EQ("corge.grault.TestMessage2.Qux", qux2_->full_name()); EXPECT_EQ("corge.grault.TestMessage2.Quux", quux2_->full_name()); } TEST_F(NestedDescriptorTest, EnumContainingType) { - EXPECT_EQ(message_ , baz_ ->containing_type()); - EXPECT_EQ(message_ , qux_ ->containing_type()); - EXPECT_EQ(message2_, qux2_ ->containing_type()); + EXPECT_EQ(message_, baz_->containing_type()); + EXPECT_EQ(message_, qux_->containing_type()); + EXPECT_EQ(message2_, qux2_->containing_type()); EXPECT_EQ(message2_, quux2_->containing_type()); } @@ -1754,29 +1795,29 @@ TEST_F(NestedDescriptorTest, NestedEnumsByIndex) { } TEST_F(NestedDescriptorTest, FindEnumTypeByName) { - EXPECT_EQ(baz_ , message_ ->FindEnumTypeByName("Baz" )); - EXPECT_EQ(qux_ , message_ ->FindEnumTypeByName("Qux" )); - EXPECT_EQ(qux2_ , message2_->FindEnumTypeByName("Qux" )); + EXPECT_EQ(baz_, message_->FindEnumTypeByName("Baz")); + EXPECT_EQ(qux_, message_->FindEnumTypeByName("Qux")); + EXPECT_EQ(qux2_, message2_->FindEnumTypeByName("Qux")); EXPECT_EQ(quux2_, message2_->FindEnumTypeByName("Quux")); - EXPECT_TRUE(message_ ->FindEnumTypeByName("NoSuchType") == NULL); - EXPECT_TRUE(message_ ->FindEnumTypeByName("Quux" ) == NULL); - EXPECT_TRUE(message2_->FindEnumTypeByName("Baz" ) == NULL); + EXPECT_TRUE(message_->FindEnumTypeByName("NoSuchType") == nullptr); + EXPECT_TRUE(message_->FindEnumTypeByName("Quux") == nullptr); + EXPECT_TRUE(message2_->FindEnumTypeByName("Baz") == nullptr); - EXPECT_TRUE(message_->FindEnumTypeByName("Foo") == NULL); + EXPECT_TRUE(message_->FindEnumTypeByName("Foo") == nullptr); } TEST_F(NestedDescriptorTest, FindEnumValueByName) { - EXPECT_EQ(a_ , message_ ->FindEnumValueByName("A")); - EXPECT_EQ(b_ , message_ ->FindEnumValueByName("B")); + EXPECT_EQ(a_, message_->FindEnumValueByName("A")); + EXPECT_EQ(b_, message_->FindEnumValueByName("B")); EXPECT_EQ(a2_, message2_->FindEnumValueByName("A")); EXPECT_EQ(c2_, message2_->FindEnumValueByName("C")); - EXPECT_TRUE(message_ ->FindEnumValueByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(message_ ->FindEnumValueByName("C" ) == NULL); - EXPECT_TRUE(message2_->FindEnumValueByName("B" ) == NULL); + EXPECT_TRUE(message_->FindEnumValueByName("NO_SUCH_VALUE") == nullptr); + EXPECT_TRUE(message_->FindEnumValueByName("C") == nullptr); + EXPECT_TRUE(message2_->FindEnumValueByName("B") == nullptr); - EXPECT_TRUE(message_->FindEnumValueByName("Foo") == NULL); + EXPECT_TRUE(message_->FindEnumValueByName("Foo") == nullptr); } // =================================================================== @@ -1818,21 +1859,21 @@ class ExtensionDescriptorTest : public testing::Test { AddExtension(&foo_file, "Foo", "foo_enum", 19, FieldDescriptorProto::LABEL_REPEATED, FieldDescriptorProto::TYPE_ENUM) - ->set_type_name("Baz"); + ->set_type_name("Baz"); DescriptorProto* bar = AddMessage(&foo_file, "Bar"); AddNestedExtension(bar, "Foo", "foo_message", 30, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_MESSAGE) - ->set_type_name("Qux"); + ->set_type_name("Qux"); AddNestedExtension(bar, "Foo", "foo_group", 39, FieldDescriptorProto::LABEL_REPEATED, FieldDescriptorProto::TYPE_GROUP) - ->set_type_name("Qux"); + ->set_type_name("Qux"); // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); ASSERT_EQ(1, foo_file_->enum_type_count()); baz_ = foo_file_->enum_type(0); @@ -1874,20 +1915,20 @@ TEST_F(ExtensionDescriptorTest, Extensions) { EXPECT_TRUE(bar_->extension(0)->is_extension()); EXPECT_TRUE(bar_->extension(1)->is_extension()); - EXPECT_EQ("foo_int32" , foo_file_->extension(0)->name()); - EXPECT_EQ("foo_enum" , foo_file_->extension(1)->name()); + EXPECT_EQ("foo_int32", foo_file_->extension(0)->name()); + EXPECT_EQ("foo_enum", foo_file_->extension(1)->name()); EXPECT_EQ("foo_message", bar_->extension(0)->name()); - EXPECT_EQ("foo_group" , bar_->extension(1)->name()); + EXPECT_EQ("foo_group", bar_->extension(1)->name()); EXPECT_EQ(10, foo_file_->extension(0)->number()); EXPECT_EQ(19, foo_file_->extension(1)->number()); EXPECT_EQ(30, bar_->extension(0)->number()); EXPECT_EQ(39, bar_->extension(1)->number()); - EXPECT_EQ(FieldDescriptor::TYPE_INT32 , foo_file_->extension(0)->type()); - EXPECT_EQ(FieldDescriptor::TYPE_ENUM , foo_file_->extension(1)->type()); + EXPECT_EQ(FieldDescriptor::TYPE_INT32, foo_file_->extension(0)->type()); + EXPECT_EQ(FieldDescriptor::TYPE_ENUM, foo_file_->extension(1)->type()); EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, bar_->extension(0)->type()); - EXPECT_EQ(FieldDescriptor::TYPE_GROUP , bar_->extension(1)->type()); + EXPECT_EQ(FieldDescriptor::TYPE_GROUP, bar_->extension(1)->type()); EXPECT_EQ(baz_, foo_file_->extension(1)->enum_type()); EXPECT_EQ(qux_, bar_->extension(0)->message_type()); @@ -1903,20 +1944,20 @@ TEST_F(ExtensionDescriptorTest, Extensions) { EXPECT_EQ(foo_, bar_->extension(0)->containing_type()); EXPECT_EQ(foo_, bar_->extension(1)->containing_type()); - EXPECT_TRUE(foo_file_->extension(0)->extension_scope() == NULL); - EXPECT_TRUE(foo_file_->extension(1)->extension_scope() == NULL); + EXPECT_TRUE(foo_file_->extension(0)->extension_scope() == nullptr); + EXPECT_TRUE(foo_file_->extension(1)->extension_scope() == nullptr); EXPECT_EQ(bar_, bar_->extension(0)->extension_scope()); EXPECT_EQ(bar_, bar_->extension(1)->extension_scope()); }; TEST_F(ExtensionDescriptorTest, IsExtensionNumber) { - EXPECT_FALSE(foo_->IsExtensionNumber( 9)); - EXPECT_TRUE (foo_->IsExtensionNumber(10)); - EXPECT_TRUE (foo_->IsExtensionNumber(19)); + EXPECT_FALSE(foo_->IsExtensionNumber(9)); + EXPECT_TRUE(foo_->IsExtensionNumber(10)); + EXPECT_TRUE(foo_->IsExtensionNumber(19)); EXPECT_FALSE(foo_->IsExtensionNumber(20)); EXPECT_FALSE(foo_->IsExtensionNumber(29)); - EXPECT_TRUE (foo_->IsExtensionNumber(30)); - EXPECT_TRUE (foo_->IsExtensionNumber(39)); + EXPECT_TRUE(foo_->IsExtensionNumber(30)); + EXPECT_TRUE(foo_->IsExtensionNumber(39)); EXPECT_FALSE(foo_->IsExtensionNumber(40)); } @@ -1926,11 +1967,40 @@ TEST_F(ExtensionDescriptorTest, FindExtensionByName) { ASSERT_EQ(2, bar_->extension_count()); EXPECT_EQ(bar_->extension(0), bar_->FindExtensionByName("foo_message")); - EXPECT_EQ(bar_->extension(1), bar_->FindExtensionByName("foo_group" )); - - EXPECT_TRUE(bar_->FindExtensionByName("no_such_extension") == NULL); - EXPECT_TRUE(foo_->FindExtensionByName("foo_int32") == NULL); - EXPECT_TRUE(foo_->FindExtensionByName("foo_message") == NULL); + EXPECT_EQ(bar_->extension(1), bar_->FindExtensionByName("foo_group")); + + EXPECT_TRUE(bar_->FindExtensionByName("no_such_extension") == nullptr); + EXPECT_TRUE(foo_->FindExtensionByName("foo_int32") == nullptr); + EXPECT_TRUE(foo_->FindExtensionByName("foo_message") == nullptr); +} + +TEST_F(ExtensionDescriptorTest, FindExtensionByPrintableName) { + EXPECT_TRUE(pool_.FindExtensionByPrintableName(foo_, "no_such_extension") == + nullptr); + EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "no_such_extension") == + nullptr); + + ASSERT_FALSE(pool_.FindExtensionByPrintableName(foo_, "Bar.foo_message") == + nullptr); + ASSERT_FALSE(pool_.FindExtensionByPrintableName(foo_, "Bar.foo_group") == + nullptr); + EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "foo_message") == + nullptr); + EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "foo_group") == nullptr); + EXPECT_EQ(bar_->FindExtensionByName("foo_message"), + pool_.FindExtensionByPrintableName(foo_, "Bar.foo_message")); + EXPECT_EQ(bar_->FindExtensionByName("foo_group"), + pool_.FindExtensionByPrintableName(foo_, "Bar.foo_group")); + + ASSERT_FALSE(pool_.FindExtensionByPrintableName(foo_, "foo_int32") == + nullptr); + ASSERT_FALSE(pool_.FindExtensionByPrintableName(foo_, "foo_enum") == nullptr); + EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "foo_int32") == nullptr); + EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "foo_enum") == nullptr); + EXPECT_EQ(foo_file_->FindExtensionByName("foo_int32"), + pool_.FindExtensionByPrintableName(foo_, "foo_int32")); + EXPECT_EQ(foo_file_->FindExtensionByName("foo_enum"), + pool_.FindExtensionByPrintableName(foo_, "foo_enum")); } TEST_F(ExtensionDescriptorTest, FindAllExtensions) { @@ -1949,7 +2019,7 @@ TEST_F(ExtensionDescriptorTest, DuplicateFieldNumber) { FileDescriptorProto file_proto; // Add "google/protobuf/descriptor.proto". FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // Add "foo.proto": // import "google/protobuf/descriptor.proto"; // extend google.protobuf.FieldOptions { @@ -1961,7 +2031,7 @@ TEST_F(ExtensionDescriptorTest, DuplicateFieldNumber) { AddExtension(&file_proto, "google.protobuf.FieldOptions", "option1", 1000, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // Add "bar.proto": // import "google/protobuf/descriptor.proto"; // extend google.protobuf.FieldOptions { @@ -1975,7 +2045,7 @@ TEST_F(ExtensionDescriptorTest, DuplicateFieldNumber) { FieldDescriptorProto::TYPE_INT32); // Currently we only generate a warning for conflicting extension numbers. // TODO(xiaofeng): Change it to an error. - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); } // =================================================================== @@ -2004,7 +2074,7 @@ class ReservedDescriptorTest : public testing::Test { // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); ASSERT_EQ(1, foo_file_->message_type_count()); foo_ = foo_file_->message_type(0); @@ -2030,16 +2100,16 @@ TEST_F(ReservedDescriptorTest, ReservedRanges) { TEST_F(ReservedDescriptorTest, IsReservedNumber) { EXPECT_FALSE(foo_->IsReservedNumber(1)); - EXPECT_TRUE (foo_->IsReservedNumber(2)); + EXPECT_TRUE(foo_->IsReservedNumber(2)); EXPECT_FALSE(foo_->IsReservedNumber(3)); EXPECT_FALSE(foo_->IsReservedNumber(8)); - EXPECT_TRUE (foo_->IsReservedNumber(9)); - EXPECT_TRUE (foo_->IsReservedNumber(10)); - EXPECT_TRUE (foo_->IsReservedNumber(11)); + EXPECT_TRUE(foo_->IsReservedNumber(9)); + EXPECT_TRUE(foo_->IsReservedNumber(10)); + EXPECT_TRUE(foo_->IsReservedNumber(11)); EXPECT_FALSE(foo_->IsReservedNumber(12)); EXPECT_FALSE(foo_->IsReservedNumber(13)); EXPECT_FALSE(foo_->IsReservedNumber(14)); - EXPECT_TRUE (foo_->IsReservedNumber(15)); + EXPECT_TRUE(foo_->IsReservedNumber(15)); EXPECT_FALSE(foo_->IsReservedNumber(16)); }; @@ -2051,8 +2121,8 @@ TEST_F(ReservedDescriptorTest, ReservedNames) { }; TEST_F(ReservedDescriptorTest, IsReservedName) { - EXPECT_TRUE (foo_->IsReservedName("foo")); - EXPECT_TRUE (foo_->IsReservedName("bar")); + EXPECT_TRUE(foo_->IsReservedName("foo")); + EXPECT_TRUE(foo_->IsReservedName("bar")); EXPECT_FALSE(foo_->IsReservedName("baz")); }; @@ -2099,7 +2169,7 @@ class ReservedEnumDescriptorTest : public testing::Test { // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); ASSERT_EQ(3, foo_file_->enum_type_count()); foo_ = foo_file_->enum_type(0); @@ -2149,17 +2219,17 @@ TEST_F(ReservedEnumDescriptorTest, IsReservedNumber) { EXPECT_TRUE(foo_->IsReservedNumber(-1)); EXPECT_TRUE(foo_->IsReservedNumber(0)); EXPECT_TRUE(foo_->IsReservedNumber(1)); - EXPECT_TRUE (foo_->IsReservedNumber(2)); + EXPECT_TRUE(foo_->IsReservedNumber(2)); EXPECT_TRUE(foo_->IsReservedNumber(3)); EXPECT_FALSE(foo_->IsReservedNumber(8)); - EXPECT_TRUE (foo_->IsReservedNumber(9)); - EXPECT_TRUE (foo_->IsReservedNumber(10)); - EXPECT_TRUE (foo_->IsReservedNumber(11)); + EXPECT_TRUE(foo_->IsReservedNumber(9)); + EXPECT_TRUE(foo_->IsReservedNumber(10)); + EXPECT_TRUE(foo_->IsReservedNumber(11)); EXPECT_TRUE(foo_->IsReservedNumber(12)); EXPECT_FALSE(foo_->IsReservedNumber(13)); EXPECT_FALSE(foo_->IsReservedNumber(13)); EXPECT_FALSE(foo_->IsReservedNumber(14)); - EXPECT_TRUE (foo_->IsReservedNumber(15)); + EXPECT_TRUE(foo_->IsReservedNumber(15)); EXPECT_TRUE(foo_->IsReservedNumber(16)); EXPECT_FALSE(foo_->IsReservedNumber(17)); @@ -2182,8 +2252,8 @@ TEST_F(ReservedEnumDescriptorTest, ReservedNames) { } TEST_F(ReservedEnumDescriptorTest, IsReservedName) { - EXPECT_TRUE (foo_->IsReservedName("foo")); - EXPECT_TRUE (foo_->IsReservedName("bar")); + EXPECT_TRUE(foo_->IsReservedName("foo")); + EXPECT_TRUE(foo_->IsReservedName("bar")); EXPECT_FALSE(foo_->IsReservedName("baz")); } @@ -2198,9 +2268,9 @@ class MiscTest : public testing::Test { AddEmptyEnum(&file_proto, "DummyEnum"); DescriptorProto* message = AddMessage(&file_proto, "TestMessage"); - FieldDescriptorProto* field = - AddField(message, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL, - static_cast(static_cast(type))); + FieldDescriptorProto* field = AddField( + message, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL, + static_cast(static_cast(type))); if (type == FieldDescriptor::TYPE_MESSAGE || type == FieldDescriptor::TYPE_GROUP) { @@ -2213,41 +2283,40 @@ class MiscTest : public testing::Test { pool_.reset(new DescriptorPool()); const FileDescriptor* file = pool_->BuildFile(file_proto); - if (file != NULL && - file->message_type_count() == 1 && + if (file != nullptr && file->message_type_count() == 1 && file->message_type(0)->field_count() == 1) { return file->message_type(0)->field(0); } else { - return NULL; + return nullptr; } } const char* GetTypeNameForFieldType(FieldDescriptor::Type type) { const FieldDescriptor* field = GetFieldDescriptorOfType(type); - return field != NULL ? field->type_name() : ""; + return field != nullptr ? field->type_name() : ""; } FieldDescriptor::CppType GetCppTypeForFieldType(FieldDescriptor::Type type) { const FieldDescriptor* field = GetFieldDescriptorOfType(type); - return field != NULL ? field->cpp_type() : - static_cast(0); + return field != nullptr ? field->cpp_type() + : static_cast(0); } const char* GetCppTypeNameForFieldType(FieldDescriptor::Type type) { const FieldDescriptor* field = GetFieldDescriptorOfType(type); - return field != NULL ? field->cpp_type_name() : ""; + return field != nullptr ? field->cpp_type_name() : ""; } const Descriptor* GetMessageDescriptorForFieldType( FieldDescriptor::Type type) { const FieldDescriptor* field = GetFieldDescriptorOfType(type); - return field != NULL ? field->message_type() : NULL; + return field != nullptr ? field->message_type() : nullptr; } const EnumDescriptor* GetEnumDescriptorForFieldType( - FieldDescriptor::Type type) { + FieldDescriptor::Type type) { const FieldDescriptor* field = GetFieldDescriptorOfType(type); - return field != NULL ? field->enum_type() : NULL; + return field != nullptr ? field->enum_type() : nullptr; } std::unique_ptr pool_; @@ -2258,24 +2327,24 @@ TEST_F(MiscTest, TypeNames) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_STREQ("double" , GetTypeNameForFieldType(FD::TYPE_DOUBLE )); - EXPECT_STREQ("float" , GetTypeNameForFieldType(FD::TYPE_FLOAT )); - EXPECT_STREQ("int64" , GetTypeNameForFieldType(FD::TYPE_INT64 )); - EXPECT_STREQ("uint64" , GetTypeNameForFieldType(FD::TYPE_UINT64 )); - EXPECT_STREQ("int32" , GetTypeNameForFieldType(FD::TYPE_INT32 )); - EXPECT_STREQ("fixed64" , GetTypeNameForFieldType(FD::TYPE_FIXED64 )); - EXPECT_STREQ("fixed32" , GetTypeNameForFieldType(FD::TYPE_FIXED32 )); - EXPECT_STREQ("bool" , GetTypeNameForFieldType(FD::TYPE_BOOL )); - EXPECT_STREQ("string" , GetTypeNameForFieldType(FD::TYPE_STRING )); - EXPECT_STREQ("group" , GetTypeNameForFieldType(FD::TYPE_GROUP )); - EXPECT_STREQ("message" , GetTypeNameForFieldType(FD::TYPE_MESSAGE )); - EXPECT_STREQ("bytes" , GetTypeNameForFieldType(FD::TYPE_BYTES )); - EXPECT_STREQ("uint32" , GetTypeNameForFieldType(FD::TYPE_UINT32 )); - EXPECT_STREQ("enum" , GetTypeNameForFieldType(FD::TYPE_ENUM )); + EXPECT_STREQ("double", GetTypeNameForFieldType(FD::TYPE_DOUBLE)); + EXPECT_STREQ("float", GetTypeNameForFieldType(FD::TYPE_FLOAT)); + EXPECT_STREQ("int64", GetTypeNameForFieldType(FD::TYPE_INT64)); + EXPECT_STREQ("uint64", GetTypeNameForFieldType(FD::TYPE_UINT64)); + EXPECT_STREQ("int32", GetTypeNameForFieldType(FD::TYPE_INT32)); + EXPECT_STREQ("fixed64", GetTypeNameForFieldType(FD::TYPE_FIXED64)); + EXPECT_STREQ("fixed32", GetTypeNameForFieldType(FD::TYPE_FIXED32)); + EXPECT_STREQ("bool", GetTypeNameForFieldType(FD::TYPE_BOOL)); + EXPECT_STREQ("string", GetTypeNameForFieldType(FD::TYPE_STRING)); + EXPECT_STREQ("group", GetTypeNameForFieldType(FD::TYPE_GROUP)); + EXPECT_STREQ("message", GetTypeNameForFieldType(FD::TYPE_MESSAGE)); + EXPECT_STREQ("bytes", GetTypeNameForFieldType(FD::TYPE_BYTES)); + EXPECT_STREQ("uint32", GetTypeNameForFieldType(FD::TYPE_UINT32)); + EXPECT_STREQ("enum", GetTypeNameForFieldType(FD::TYPE_ENUM)); EXPECT_STREQ("sfixed32", GetTypeNameForFieldType(FD::TYPE_SFIXED32)); EXPECT_STREQ("sfixed64", GetTypeNameForFieldType(FD::TYPE_SFIXED64)); - EXPECT_STREQ("sint32" , GetTypeNameForFieldType(FD::TYPE_SINT32 )); - EXPECT_STREQ("sint64" , GetTypeNameForFieldType(FD::TYPE_SINT64 )); + EXPECT_STREQ("sint32", GetTypeNameForFieldType(FD::TYPE_SINT32)); + EXPECT_STREQ("sint64", GetTypeNameForFieldType(FD::TYPE_SINT64)); } TEST_F(MiscTest, StaticTypeNames) { @@ -2283,24 +2352,24 @@ TEST_F(MiscTest, StaticTypeNames) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_STREQ("double" , FD::TypeName(FD::TYPE_DOUBLE )); - EXPECT_STREQ("float" , FD::TypeName(FD::TYPE_FLOAT )); - EXPECT_STREQ("int64" , FD::TypeName(FD::TYPE_INT64 )); - EXPECT_STREQ("uint64" , FD::TypeName(FD::TYPE_UINT64 )); - EXPECT_STREQ("int32" , FD::TypeName(FD::TYPE_INT32 )); - EXPECT_STREQ("fixed64" , FD::TypeName(FD::TYPE_FIXED64 )); - EXPECT_STREQ("fixed32" , FD::TypeName(FD::TYPE_FIXED32 )); - EXPECT_STREQ("bool" , FD::TypeName(FD::TYPE_BOOL )); - EXPECT_STREQ("string" , FD::TypeName(FD::TYPE_STRING )); - EXPECT_STREQ("group" , FD::TypeName(FD::TYPE_GROUP )); - EXPECT_STREQ("message" , FD::TypeName(FD::TYPE_MESSAGE )); - EXPECT_STREQ("bytes" , FD::TypeName(FD::TYPE_BYTES )); - EXPECT_STREQ("uint32" , FD::TypeName(FD::TYPE_UINT32 )); - EXPECT_STREQ("enum" , FD::TypeName(FD::TYPE_ENUM )); + EXPECT_STREQ("double", FD::TypeName(FD::TYPE_DOUBLE)); + EXPECT_STREQ("float", FD::TypeName(FD::TYPE_FLOAT)); + EXPECT_STREQ("int64", FD::TypeName(FD::TYPE_INT64)); + EXPECT_STREQ("uint64", FD::TypeName(FD::TYPE_UINT64)); + EXPECT_STREQ("int32", FD::TypeName(FD::TYPE_INT32)); + EXPECT_STREQ("fixed64", FD::TypeName(FD::TYPE_FIXED64)); + EXPECT_STREQ("fixed32", FD::TypeName(FD::TYPE_FIXED32)); + EXPECT_STREQ("bool", FD::TypeName(FD::TYPE_BOOL)); + EXPECT_STREQ("string", FD::TypeName(FD::TYPE_STRING)); + EXPECT_STREQ("group", FD::TypeName(FD::TYPE_GROUP)); + EXPECT_STREQ("message", FD::TypeName(FD::TYPE_MESSAGE)); + EXPECT_STREQ("bytes", FD::TypeName(FD::TYPE_BYTES)); + EXPECT_STREQ("uint32", FD::TypeName(FD::TYPE_UINT32)); + EXPECT_STREQ("enum", FD::TypeName(FD::TYPE_ENUM)); EXPECT_STREQ("sfixed32", FD::TypeName(FD::TYPE_SFIXED32)); EXPECT_STREQ("sfixed64", FD::TypeName(FD::TYPE_SFIXED64)); - EXPECT_STREQ("sint32" , FD::TypeName(FD::TYPE_SINT32 )); - EXPECT_STREQ("sint64" , FD::TypeName(FD::TYPE_SINT64 )); + EXPECT_STREQ("sint32", FD::TypeName(FD::TYPE_SINT32)); + EXPECT_STREQ("sint64", FD::TypeName(FD::TYPE_SINT64)); } TEST_F(MiscTest, CppTypes) { @@ -2308,24 +2377,24 @@ TEST_F(MiscTest, CppTypes) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_EQ(FD::CPPTYPE_DOUBLE , GetCppTypeForFieldType(FD::TYPE_DOUBLE )); - EXPECT_EQ(FD::CPPTYPE_FLOAT , GetCppTypeForFieldType(FD::TYPE_FLOAT )); - EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_INT64 )); - EXPECT_EQ(FD::CPPTYPE_UINT64 , GetCppTypeForFieldType(FD::TYPE_UINT64 )); - EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_INT32 )); - EXPECT_EQ(FD::CPPTYPE_UINT64 , GetCppTypeForFieldType(FD::TYPE_FIXED64 )); - EXPECT_EQ(FD::CPPTYPE_UINT32 , GetCppTypeForFieldType(FD::TYPE_FIXED32 )); - EXPECT_EQ(FD::CPPTYPE_BOOL , GetCppTypeForFieldType(FD::TYPE_BOOL )); - EXPECT_EQ(FD::CPPTYPE_STRING , GetCppTypeForFieldType(FD::TYPE_STRING )); - EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_GROUP )); - EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_MESSAGE )); - EXPECT_EQ(FD::CPPTYPE_STRING , GetCppTypeForFieldType(FD::TYPE_BYTES )); - EXPECT_EQ(FD::CPPTYPE_UINT32 , GetCppTypeForFieldType(FD::TYPE_UINT32 )); - EXPECT_EQ(FD::CPPTYPE_ENUM , GetCppTypeForFieldType(FD::TYPE_ENUM )); - EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_SFIXED32)); - EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_SFIXED64)); - EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_SINT32 )); - EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_SINT64 )); + EXPECT_EQ(FD::CPPTYPE_DOUBLE, GetCppTypeForFieldType(FD::TYPE_DOUBLE)); + EXPECT_EQ(FD::CPPTYPE_FLOAT, GetCppTypeForFieldType(FD::TYPE_FLOAT)); + EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_INT64)); + EXPECT_EQ(FD::CPPTYPE_UINT64, GetCppTypeForFieldType(FD::TYPE_UINT64)); + EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_INT32)); + EXPECT_EQ(FD::CPPTYPE_UINT64, GetCppTypeForFieldType(FD::TYPE_FIXED64)); + EXPECT_EQ(FD::CPPTYPE_UINT32, GetCppTypeForFieldType(FD::TYPE_FIXED32)); + EXPECT_EQ(FD::CPPTYPE_BOOL, GetCppTypeForFieldType(FD::TYPE_BOOL)); + EXPECT_EQ(FD::CPPTYPE_STRING, GetCppTypeForFieldType(FD::TYPE_STRING)); + EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_GROUP)); + EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_MESSAGE)); + EXPECT_EQ(FD::CPPTYPE_STRING, GetCppTypeForFieldType(FD::TYPE_BYTES)); + EXPECT_EQ(FD::CPPTYPE_UINT32, GetCppTypeForFieldType(FD::TYPE_UINT32)); + EXPECT_EQ(FD::CPPTYPE_ENUM, GetCppTypeForFieldType(FD::TYPE_ENUM)); + EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_SFIXED32)); + EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_SFIXED64)); + EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_SINT32)); + EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_SINT64)); } TEST_F(MiscTest, CppTypeNames) { @@ -2333,24 +2402,24 @@ TEST_F(MiscTest, CppTypeNames) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_STREQ("double" , GetCppTypeNameForFieldType(FD::TYPE_DOUBLE )); - EXPECT_STREQ("float" , GetCppTypeNameForFieldType(FD::TYPE_FLOAT )); - EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_INT64 )); - EXPECT_STREQ("uint64" , GetCppTypeNameForFieldType(FD::TYPE_UINT64 )); - EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_INT32 )); - EXPECT_STREQ("uint64" , GetCppTypeNameForFieldType(FD::TYPE_FIXED64 )); - EXPECT_STREQ("uint32" , GetCppTypeNameForFieldType(FD::TYPE_FIXED32 )); - EXPECT_STREQ("bool" , GetCppTypeNameForFieldType(FD::TYPE_BOOL )); - EXPECT_STREQ("string" , GetCppTypeNameForFieldType(FD::TYPE_STRING )); - EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_GROUP )); - EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_MESSAGE )); - EXPECT_STREQ("string" , GetCppTypeNameForFieldType(FD::TYPE_BYTES )); - EXPECT_STREQ("uint32" , GetCppTypeNameForFieldType(FD::TYPE_UINT32 )); - EXPECT_STREQ("enum" , GetCppTypeNameForFieldType(FD::TYPE_ENUM )); - EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_SFIXED32)); - EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_SFIXED64)); - EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_SINT32 )); - EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_SINT64 )); + EXPECT_STREQ("double", GetCppTypeNameForFieldType(FD::TYPE_DOUBLE)); + EXPECT_STREQ("float", GetCppTypeNameForFieldType(FD::TYPE_FLOAT)); + EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_INT64)); + EXPECT_STREQ("uint64", GetCppTypeNameForFieldType(FD::TYPE_UINT64)); + EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_INT32)); + EXPECT_STREQ("uint64", GetCppTypeNameForFieldType(FD::TYPE_FIXED64)); + EXPECT_STREQ("uint32", GetCppTypeNameForFieldType(FD::TYPE_FIXED32)); + EXPECT_STREQ("bool", GetCppTypeNameForFieldType(FD::TYPE_BOOL)); + EXPECT_STREQ("string", GetCppTypeNameForFieldType(FD::TYPE_STRING)); + EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_GROUP)); + EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_MESSAGE)); + EXPECT_STREQ("string", GetCppTypeNameForFieldType(FD::TYPE_BYTES)); + EXPECT_STREQ("uint32", GetCppTypeNameForFieldType(FD::TYPE_UINT32)); + EXPECT_STREQ("enum", GetCppTypeNameForFieldType(FD::TYPE_ENUM)); + EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_SFIXED32)); + EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_SFIXED64)); + EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_SINT32)); + EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_SINT64)); } TEST_F(MiscTest, StaticCppTypeNames) { @@ -2358,69 +2427,68 @@ TEST_F(MiscTest, StaticCppTypeNames) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_STREQ("int32" , FD::CppTypeName(FD::CPPTYPE_INT32 )); - EXPECT_STREQ("int64" , FD::CppTypeName(FD::CPPTYPE_INT64 )); - EXPECT_STREQ("uint32" , FD::CppTypeName(FD::CPPTYPE_UINT32 )); - EXPECT_STREQ("uint64" , FD::CppTypeName(FD::CPPTYPE_UINT64 )); - EXPECT_STREQ("double" , FD::CppTypeName(FD::CPPTYPE_DOUBLE )); - EXPECT_STREQ("float" , FD::CppTypeName(FD::CPPTYPE_FLOAT )); - EXPECT_STREQ("bool" , FD::CppTypeName(FD::CPPTYPE_BOOL )); - EXPECT_STREQ("enum" , FD::CppTypeName(FD::CPPTYPE_ENUM )); - EXPECT_STREQ("string" , FD::CppTypeName(FD::CPPTYPE_STRING )); + EXPECT_STREQ("int32", FD::CppTypeName(FD::CPPTYPE_INT32)); + EXPECT_STREQ("int64", FD::CppTypeName(FD::CPPTYPE_INT64)); + EXPECT_STREQ("uint32", FD::CppTypeName(FD::CPPTYPE_UINT32)); + EXPECT_STREQ("uint64", FD::CppTypeName(FD::CPPTYPE_UINT64)); + EXPECT_STREQ("double", FD::CppTypeName(FD::CPPTYPE_DOUBLE)); + EXPECT_STREQ("float", FD::CppTypeName(FD::CPPTYPE_FLOAT)); + EXPECT_STREQ("bool", FD::CppTypeName(FD::CPPTYPE_BOOL)); + EXPECT_STREQ("enum", FD::CppTypeName(FD::CPPTYPE_ENUM)); + EXPECT_STREQ("string", FD::CppTypeName(FD::CPPTYPE_STRING)); EXPECT_STREQ("message", FD::CppTypeName(FD::CPPTYPE_MESSAGE)); } TEST_F(MiscTest, MessageType) { - // Test that message_type() is NULL for non-aggregate fields + // Test that message_type() is nullptr for non-aggregate fields typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_DOUBLE )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FLOAT )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_INT64 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_UINT64 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_INT32 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FIXED64 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FIXED32 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_BOOL )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_STRING )); - EXPECT_TRUE(NULL != GetMessageDescriptorForFieldType(FD::TYPE_GROUP )); - EXPECT_TRUE(NULL != GetMessageDescriptorForFieldType(FD::TYPE_MESSAGE )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_BYTES )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_UINT32 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_ENUM )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED32)); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED64)); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SINT32 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SINT64 )); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_DOUBLE)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_FLOAT)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_INT64)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_UINT64)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_INT32)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_FIXED64)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_FIXED32)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_BOOL)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_STRING)); + EXPECT_TRUE(nullptr != GetMessageDescriptorForFieldType(FD::TYPE_GROUP)); + EXPECT_TRUE(nullptr != GetMessageDescriptorForFieldType(FD::TYPE_MESSAGE)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_BYTES)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_UINT32)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_ENUM)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED32)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED64)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_SINT32)); + EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_SINT64)); } TEST_F(MiscTest, EnumType) { - // Test that enum_type() is NULL for non-enum fields + // Test that enum_type() is nullptr for non-enum fields typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_DOUBLE )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FLOAT )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_INT64 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_UINT64 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_INT32 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FIXED64 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FIXED32 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_BOOL )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_STRING )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_GROUP )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_MESSAGE )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_BYTES )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_UINT32 )); - EXPECT_TRUE(NULL != GetEnumDescriptorForFieldType(FD::TYPE_ENUM )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED32)); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED64)); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SINT32 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SINT64 )); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_DOUBLE)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_FLOAT)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_INT64)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_UINT64)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_INT32)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_FIXED64)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_FIXED32)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_BOOL)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_STRING)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_GROUP)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_MESSAGE)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_BYTES)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_UINT32)); + EXPECT_TRUE(nullptr != GetEnumDescriptorForFieldType(FD::TYPE_ENUM)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED32)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED64)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_SINT32)); + EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_SINT64)); } - TEST_F(MiscTest, DefaultValues) { // Test that setting default values works. FileDescriptorProto file_proto; @@ -2436,52 +2504,52 @@ TEST_F(MiscTest, DefaultValues) { const FD::Label label = FD::LABEL_OPTIONAL; // Create fields of every CPP type with default values. - AddField(message_proto, "int32" , 1, label, FD::TYPE_INT32 ) - ->set_default_value("-1"); - AddField(message_proto, "int64" , 2, label, FD::TYPE_INT64 ) - ->set_default_value("-1000000000000"); + AddField(message_proto, "int32", 1, label, FD::TYPE_INT32) + ->set_default_value("-1"); + AddField(message_proto, "int64", 2, label, FD::TYPE_INT64) + ->set_default_value("-1000000000000"); AddField(message_proto, "uint32", 3, label, FD::TYPE_UINT32) - ->set_default_value("42"); + ->set_default_value("42"); AddField(message_proto, "uint64", 4, label, FD::TYPE_UINT64) - ->set_default_value("2000000000000"); - AddField(message_proto, "float" , 5, label, FD::TYPE_FLOAT ) - ->set_default_value("4.5"); + ->set_default_value("2000000000000"); + AddField(message_proto, "float", 5, label, FD::TYPE_FLOAT) + ->set_default_value("4.5"); AddField(message_proto, "double", 6, label, FD::TYPE_DOUBLE) - ->set_default_value("10e100"); - AddField(message_proto, "bool" , 7, label, FD::TYPE_BOOL ) - ->set_default_value("true"); + ->set_default_value("10e100"); + AddField(message_proto, "bool", 7, label, FD::TYPE_BOOL) + ->set_default_value("true"); AddField(message_proto, "string", 8, label, FD::TYPE_STRING) - ->set_default_value("hello"); - AddField(message_proto, "data" , 9, label, FD::TYPE_BYTES ) - ->set_default_value("\\001\\002\\003"); + ->set_default_value("hello"); + AddField(message_proto, "data", 9, label, FD::TYPE_BYTES) + ->set_default_value("\\001\\002\\003"); FieldDescriptorProto* enum_field = - AddField(message_proto, "enum", 10, label, FD::TYPE_ENUM); + AddField(message_proto, "enum", 10, label, FD::TYPE_ENUM); enum_field->set_type_name("DummyEnum"); enum_field->set_default_value("B"); // Strings are allowed to have empty defaults. (At one point, due to // a bug, empty defaults for strings were rejected. Oops.) AddField(message_proto, "empty_string", 11, label, FD::TYPE_STRING) - ->set_default_value(""); + ->set_default_value(""); // Add a second set of fields with implicit defalut values. - AddField(message_proto, "implicit_int32" , 21, label, FD::TYPE_INT32 ); - AddField(message_proto, "implicit_int64" , 22, label, FD::TYPE_INT64 ); + AddField(message_proto, "implicit_int32", 21, label, FD::TYPE_INT32); + AddField(message_proto, "implicit_int64", 22, label, FD::TYPE_INT64); AddField(message_proto, "implicit_uint32", 23, label, FD::TYPE_UINT32); AddField(message_proto, "implicit_uint64", 24, label, FD::TYPE_UINT64); - AddField(message_proto, "implicit_float" , 25, label, FD::TYPE_FLOAT ); + AddField(message_proto, "implicit_float", 25, label, FD::TYPE_FLOAT); AddField(message_proto, "implicit_double", 26, label, FD::TYPE_DOUBLE); - AddField(message_proto, "implicit_bool" , 27, label, FD::TYPE_BOOL ); + AddField(message_proto, "implicit_bool", 27, label, FD::TYPE_BOOL); AddField(message_proto, "implicit_string", 28, label, FD::TYPE_STRING); - AddField(message_proto, "implicit_data" , 29, label, FD::TYPE_BYTES ); - AddField(message_proto, "implicit_enum" , 30, label, FD::TYPE_ENUM) - ->set_type_name("DummyEnum"); + AddField(message_proto, "implicit_data", 29, label, FD::TYPE_BYTES); + AddField(message_proto, "implicit_enum", 30, label, FD::TYPE_ENUM) + ->set_type_name("DummyEnum"); // Build it. DescriptorPool pool; const FileDescriptor* file = pool.BuildFile(file_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); ASSERT_EQ(1, file->enum_type_count()); const EnumDescriptor* enum_type = file->enum_type(0); @@ -2507,19 +2575,19 @@ TEST_F(MiscTest, DefaultValues) { ASSERT_TRUE(message->field(9)->has_default_value()); ASSERT_TRUE(message->field(10)->has_default_value()); - EXPECT_EQ(-1 , message->field(0)->default_value_int32 ()); - EXPECT_EQ(-GOOGLE_ULONGLONG(1000000000000), - message->field(1)->default_value_int64 ()); - EXPECT_EQ(42 , message->field(2)->default_value_uint32()); - EXPECT_EQ(GOOGLE_ULONGLONG(2000000000000), + EXPECT_EQ(-1, message->field(0)->default_value_int32()); + EXPECT_EQ(-PROTOBUF_ULONGLONG(1000000000000), + message->field(1)->default_value_int64()); + EXPECT_EQ(42, message->field(2)->default_value_uint32()); + EXPECT_EQ(PROTOBUF_ULONGLONG(2000000000000), message->field(3)->default_value_uint64()); - EXPECT_EQ(4.5 , message->field(4)->default_value_float ()); - EXPECT_EQ(10e100 , message->field(5)->default_value_double()); - EXPECT_TRUE( message->field(6)->default_value_bool ()); - EXPECT_EQ("hello" , message->field(7)->default_value_string()); - EXPECT_EQ("\001\002\003" , message->field(8)->default_value_string()); - EXPECT_EQ(enum_value_b , message->field(9)->default_value_enum ()); - EXPECT_EQ("" , message->field(10)->default_value_string()); + EXPECT_EQ(4.5, message->field(4)->default_value_float()); + EXPECT_EQ(10e100, message->field(5)->default_value_double()); + EXPECT_TRUE(message->field(6)->default_value_bool()); + EXPECT_EQ("hello", message->field(7)->default_value_string()); + EXPECT_EQ("\001\002\003", message->field(8)->default_value_string()); + EXPECT_EQ(enum_value_b, message->field(9)->default_value_enum()); + EXPECT_EQ("", message->field(10)->default_value_string()); ASSERT_FALSE(message->field(11)->has_default_value()); ASSERT_FALSE(message->field(12)->has_default_value()); @@ -2532,15 +2600,15 @@ TEST_F(MiscTest, DefaultValues) { ASSERT_FALSE(message->field(19)->has_default_value()); ASSERT_FALSE(message->field(20)->has_default_value()); - EXPECT_EQ(0 , message->field(11)->default_value_int32 ()); - EXPECT_EQ(0 , message->field(12)->default_value_int64 ()); - EXPECT_EQ(0 , message->field(13)->default_value_uint32()); - EXPECT_EQ(0 , message->field(14)->default_value_uint64()); - EXPECT_EQ(0.0f , message->field(15)->default_value_float ()); - EXPECT_EQ(0.0 , message->field(16)->default_value_double()); - EXPECT_FALSE( message->field(17)->default_value_bool ()); - EXPECT_EQ("" , message->field(18)->default_value_string()); - EXPECT_EQ("" , message->field(19)->default_value_string()); + EXPECT_EQ(0, message->field(11)->default_value_int32()); + EXPECT_EQ(0, message->field(12)->default_value_int64()); + EXPECT_EQ(0, message->field(13)->default_value_uint32()); + EXPECT_EQ(0, message->field(14)->default_value_uint64()); + EXPECT_EQ(0.0f, message->field(15)->default_value_float()); + EXPECT_EQ(0.0, message->field(16)->default_value_double()); + EXPECT_FALSE(message->field(17)->default_value_bool()); + EXPECT_EQ("", message->field(18)->default_value_string()); + EXPECT_EQ("", message->field(19)->default_value_string()); EXPECT_EQ(enum_value_a, message->field(20)->default_value_enum()); } @@ -2551,13 +2619,11 @@ TEST_F(MiscTest, FieldOptions) { file_proto.set_name("foo.proto"); DescriptorProto* message_proto = AddMessage(&file_proto, "TestMessage"); - AddField(message_proto, "foo", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message_proto, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); FieldDescriptorProto* bar_proto = - AddField(message_proto, "bar", 2, - FieldDescriptorProto::LABEL_OPTIONAL, - FieldDescriptorProto::TYPE_INT32); + AddField(message_proto, "bar", 2, FieldDescriptorProto::LABEL_OPTIONAL, + FieldDescriptorProto::TYPE_INT32); FieldOptions* options = bar_proto->mutable_options(); options->set_ctype(FieldOptions::CORD); @@ -2565,7 +2631,7 @@ TEST_F(MiscTest, FieldOptions) { // Build the descriptors and get the pointers. DescriptorPool pool; const FileDescriptor* file = pool.BuildFile(file_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); ASSERT_EQ(1, file->message_type_count()); const Descriptor* message = file->message_type(0); @@ -2584,17 +2650,12 @@ TEST_F(MiscTest, FieldOptions) { } // =================================================================== -enum DescriptorPoolMode { - NO_DATABASE, - FALLBACK_DATABASE -}; +enum DescriptorPoolMode { NO_DATABASE, FALLBACK_DATABASE }; class AllowUnknownDependenciesTest : public testing::TestWithParam { protected: - DescriptorPoolMode mode() { - return GetParam(); - } + DescriptorPoolMode mode() { return GetParam(); } virtual void SetUp() { FileDescriptorProto foo_proto, bar_proto; @@ -2611,42 +2672,42 @@ class AllowUnknownDependenciesTest pool_->AllowUnknownDependencies(); ASSERT_TRUE(TextFormat::ParseFromString( - "name: 'foo.proto'" - "dependency: 'bar.proto'" - "dependency: 'baz.proto'" - "message_type {" - " name: 'Foo'" - " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'Bar' }" - " field { name:'baz' number:2 label:LABEL_OPTIONAL type_name:'Baz' }" - " field { name:'qux' number:3 label:LABEL_OPTIONAL" - " type_name: '.corge.Qux'" - " type: TYPE_ENUM" - " options {" - " uninterpreted_option {" - " name {" - " name_part: 'grault'" - " is_extension: true" - " }" - " positive_int_value: 1234" - " }" - " }" - " }" - "}", - &foo_proto)); - ASSERT_TRUE(TextFormat::ParseFromString( - "name: 'bar.proto'" - "message_type { name: 'Bar' }", - &bar_proto)); + "name: 'foo.proto'" + "dependency: 'bar.proto'" + "dependency: 'baz.proto'" + "message_type {" + " name: 'Foo'" + " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'Bar' }" + " field { name:'baz' number:2 label:LABEL_OPTIONAL type_name:'Baz' }" + " field { name:'qux' number:3 label:LABEL_OPTIONAL" + " type_name: '.corge.Qux'" + " type: TYPE_ENUM" + " options {" + " uninterpreted_option {" + " name {" + " name_part: 'grault'" + " is_extension: true" + " }" + " positive_int_value: 1234" + " }" + " }" + " }" + "}", + &foo_proto)); + ASSERT_TRUE( + TextFormat::ParseFromString("name: 'bar.proto'" + "message_type { name: 'Bar' }", + &bar_proto)); // Collect pointers to stuff. bar_file_ = BuildFile(bar_proto); - ASSERT_TRUE(bar_file_ != NULL); + ASSERT_TRUE(bar_file_ != nullptr); ASSERT_EQ(1, bar_file_->message_type_count()); bar_type_ = bar_file_->message_type(0); foo_file_ = BuildFile(foo_proto); - ASSERT_TRUE(foo_file_ != NULL); + ASSERT_TRUE(foo_file_ != nullptr); ASSERT_EQ(1, foo_file_->message_type_count()); foo_type_ = foo_file_->message_type(0); @@ -2668,7 +2729,7 @@ class AllowUnknownDependenciesTest } } GOOGLE_LOG(FATAL) << "Can't get here."; - return NULL; + return nullptr; } const FileDescriptor* bar_file_; @@ -2679,7 +2740,7 @@ class AllowUnknownDependenciesTest const FieldDescriptor* baz_field_; const FieldDescriptor* qux_field_; - SimpleDescriptorDatabase db_; // used if in FALLBACK_DATABASE mode. + SimpleDescriptorDatabase db_; // used if in FALLBACK_DATABASE mode. std::unique_ptr pool_; }; @@ -2695,7 +2756,7 @@ TEST_P(AllowUnknownDependenciesTest, PlaceholderFile) { // Placeholder files should not be findable. EXPECT_EQ(bar_file_, pool_->FindFileByName(bar_file_->name())); - EXPECT_TRUE(pool_->FindFileByName(baz_file->name()) == NULL); + EXPECT_TRUE(pool_->FindFileByName(baz_file->name()) == nullptr); // Copy*To should not crash for placeholder files. FileDescriptorProto baz_file_proto; @@ -2724,8 +2785,8 @@ TEST_P(AllowUnknownDependenciesTest, PlaceholderTypes) { // Placeholder types should not be findable. EXPECT_EQ(bar_type_, pool_->FindMessageTypeByName(bar_type_->full_name())); - EXPECT_TRUE(pool_->FindMessageTypeByName(baz_type->full_name()) == NULL); - EXPECT_TRUE(pool_->FindEnumTypeByName(qux_type->full_name()) == NULL); + EXPECT_TRUE(pool_->FindMessageTypeByName(baz_type->full_name()) == nullptr); + EXPECT_TRUE(pool_->FindEnumTypeByName(qux_type->full_name()) == nullptr); } TEST_P(AllowUnknownDependenciesTest, CopyTo) { @@ -2755,7 +2816,7 @@ TEST_P(AllowUnknownDependenciesTest, CustomOptions) { // Qux should still have the uninterpreted option attached. ASSERT_EQ(1, qux_field_->options().uninterpreted_option_size()); const UninterpretedOption& option = - qux_field_->options().uninterpreted_option(0); + qux_field_->options().uninterpreted_option(0); ASSERT_EQ(1, option.name_size()); EXPECT_EQ("grault", option.name(0).name_part()); } @@ -2767,13 +2828,13 @@ TEST_P(AllowUnknownDependenciesTest, UnknownExtendee) { FileDescriptorProto extension_proto; ASSERT_TRUE(TextFormat::ParseFromString( - "name: 'extension.proto'" - "extension { extendee: 'UnknownType' name:'some_extension' number:123" - " label:LABEL_OPTIONAL type:TYPE_INT32 }", - &extension_proto)); + "name: 'extension.proto'" + "extension { extendee: 'UnknownType' name:'some_extension' number:123" + " label:LABEL_OPTIONAL type:TYPE_INT32 }", + &extension_proto)); const FileDescriptor* file = BuildFile(extension_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); ASSERT_EQ(1, file->extension_count()); const Descriptor* extendee = file->extension(0)->containing_type(); @@ -2791,42 +2852,42 @@ TEST_P(AllowUnknownDependenciesTest, CustomOption) { FileDescriptorProto option_proto; ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"unknown_custom_options.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { " - " extendee: \"google.protobuf.FileOptions\" " - " name: \"some_option\" " - " number: 123456 " - " label: LABEL_OPTIONAL " - " type: TYPE_INT32 " - "} " - "options { " - " uninterpreted_option { " - " name { " - " name_part: \"some_option\" " - " is_extension: true " - " } " - " positive_int_value: 1234 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"unknown_option\" " - " is_extension: true " - " } " - " positive_int_value: 1234 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"optimize_for\" " - " is_extension: false " - " } " - " identifier_value: \"SPEED\" " - " } " - "}", - &option_proto)); + "name: \"unknown_custom_options.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { " + " extendee: \"google.protobuf.FileOptions\" " + " name: \"some_option\" " + " number: 123456 " + " label: LABEL_OPTIONAL " + " type: TYPE_INT32 " + "} " + "options { " + " uninterpreted_option { " + " name { " + " name_part: \"some_option\" " + " is_extension: true " + " } " + " positive_int_value: 1234 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"unknown_option\" " + " is_extension: true " + " } " + " positive_int_value: 1234 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"optimize_for\" " + " is_extension: false " + " } " + " identifier_value: \"SPEED\" " + " } " + "}", + &option_proto)); const FileDescriptor* file = BuildFile(option_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); // Verify that no extension options were set, but they were left as // uninterpreted_options. @@ -2850,25 +2911,25 @@ TEST_P(AllowUnknownDependenciesTest, FileDescriptorProto undeclared_dep_proto; // We make this file fail to build by giving it two fields with tag 1. ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"invalid_file_as_undeclared_dep.proto\" " - "package: \"undeclared\" " - "message_type: { " - " name: \"Quux\" " - " field { " - " name:'qux' number:1 label:LABEL_OPTIONAL type: TYPE_INT32 " - " }" - " field { " - " name:'quux' number:1 label:LABEL_OPTIONAL type: TYPE_INT64 " - " }" - "}", - &undeclared_dep_proto)); + "name: \"invalid_file_as_undeclared_dep.proto\" " + "package: \"undeclared\" " + "message_type: { " + " name: \"Quux\" " + " field { " + " name:'qux' number:1 label:LABEL_OPTIONAL type: TYPE_INT32 " + " }" + " field { " + " name:'quux' number:1 label:LABEL_OPTIONAL type: TYPE_INT64 " + " }" + "}", + &undeclared_dep_proto)); // We can't use the BuildFile() helper because we don't actually want to build // it into the descriptor pool in the fallback database case: it just needs to // be sitting in the database so that it gets built during the building of // test.proto below. switch (mode()) { case NO_DATABASE: { - ASSERT_TRUE(pool_->BuildFile(undeclared_dep_proto) == NULL); + ASSERT_TRUE(pool_->BuildFile(undeclared_dep_proto) == nullptr); break; } case FALLBACK_DATABASE: { @@ -2878,18 +2939,18 @@ TEST_P(AllowUnknownDependenciesTest, FileDescriptorProto test_proto; ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"test.proto\" " - "message_type: { " - " name: \"Corge\" " - " field { " - " name:'quux' number:1 label: LABEL_OPTIONAL " - " type_name:'undeclared.Quux' type: TYPE_MESSAGE " - " }" - "}", - &test_proto)); + "name: \"test.proto\" " + "message_type: { " + " name: \"Corge\" " + " field { " + " name:'quux' number:1 label: LABEL_OPTIONAL " + " type_name:'undeclared.Quux' type: TYPE_MESSAGE " + " }" + "}", + &test_proto)); const FileDescriptor* file = BuildFile(test_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); GOOGLE_LOG(INFO) << file->DebugString(); EXPECT_EQ(0, file->dependency_count()); @@ -2905,12 +2966,11 @@ TEST_P(AllowUnknownDependenciesTest, ASSERT_EQ("undeclared.Quux", quux_field->message_type()->full_name()); EXPECT_TRUE(quux_field->message_type()->is_placeholder()); // The place holder type should not be findable. - ASSERT_TRUE(pool_->FindMessageTypeByName("undeclared.Quux") == NULL); + ASSERT_TRUE(pool_->FindMessageTypeByName("undeclared.Quux") == nullptr); } -INSTANTIATE_TEST_CASE_P(DatabaseSource, - AllowUnknownDependenciesTest, - testing::Values(NO_DATABASE, FALLBACK_DATABASE)); +INSTANTIATE_TEST_SUITE_P(DatabaseSource, AllowUnknownDependenciesTest, + testing::Values(NO_DATABASE, FALLBACK_DATABASE)); // =================================================================== @@ -2926,22 +2986,19 @@ TEST(CustomOptions, OptionLocations) { file->FindServiceByName("TestServiceWithCustomOptions"); const MethodDescriptor* method = service->FindMethodByName("Foo"); - EXPECT_EQ(GOOGLE_LONGLONG(9876543210), + EXPECT_EQ(PROTOBUF_LONGLONG(9876543210), file->options().GetExtension(protobuf_unittest::file_opt1)); EXPECT_EQ(-56, message->options().GetExtension(protobuf_unittest::message_opt1)); - EXPECT_EQ(GOOGLE_LONGLONG(8765432109), + EXPECT_EQ(PROTOBUF_LONGLONG(8765432109), field->options().GetExtension(protobuf_unittest::field_opt1)); EXPECT_EQ(42, // Check that we get the default for an option we don't set. field->options().GetExtension(protobuf_unittest::field_opt2)); - EXPECT_EQ(-99, - oneof->options().GetExtension(protobuf_unittest::oneof_opt1)); - EXPECT_EQ(-789, - enm->options().GetExtension(protobuf_unittest::enum_opt1)); - EXPECT_EQ(123, - enm->value(1)->options().GetExtension( - protobuf_unittest::enum_value_opt1)); - EXPECT_EQ(GOOGLE_LONGLONG(-9876543210), + EXPECT_EQ(-99, oneof->options().GetExtension(protobuf_unittest::oneof_opt1)); + EXPECT_EQ(-789, enm->options().GetExtension(protobuf_unittest::enum_opt1)); + EXPECT_EQ(123, enm->value(1)->options().GetExtension( + protobuf_unittest::enum_value_opt1)); + EXPECT_EQ(PROTOBUF_LONGLONG(-9876543210), service->options().GetExtension(protobuf_unittest::service_opt1)); EXPECT_EQ(protobuf_unittest::METHODOPT1_VAL2, method->options().GetExtension(protobuf_unittest::method_opt1)); @@ -2952,38 +3009,37 @@ TEST(CustomOptions, OptionLocations) { } TEST(CustomOptions, OptionTypes) { - const MessageOptions* options = NULL; + const MessageOptions* options = nullptr; options = &protobuf_unittest::CustomOptionMinIntegerValues::descriptor()->options(); - EXPECT_EQ(false , options->GetExtension(protobuf_unittest::bool_opt)); + EXPECT_EQ(false, options->GetExtension(protobuf_unittest::bool_opt)); EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::int32_opt)); EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::int64_opt)); - EXPECT_EQ(0 , options->GetExtension(protobuf_unittest::uint32_opt)); - EXPECT_EQ(0 , options->GetExtension(protobuf_unittest::uint64_opt)); + EXPECT_EQ(0, options->GetExtension(protobuf_unittest::uint32_opt)); + EXPECT_EQ(0, options->GetExtension(protobuf_unittest::uint64_opt)); EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::sint32_opt)); EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::sint64_opt)); - EXPECT_EQ(0 , options->GetExtension(protobuf_unittest::fixed32_opt)); - EXPECT_EQ(0 , options->GetExtension(protobuf_unittest::fixed64_opt)); + EXPECT_EQ(0, options->GetExtension(protobuf_unittest::fixed32_opt)); + EXPECT_EQ(0, options->GetExtension(protobuf_unittest::fixed64_opt)); EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::sfixed32_opt)); EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::sfixed64_opt)); options = &protobuf_unittest::CustomOptionMaxIntegerValues::descriptor()->options(); - EXPECT_EQ(true , options->GetExtension(protobuf_unittest::bool_opt)); - EXPECT_EQ(kint32max , options->GetExtension(protobuf_unittest::int32_opt)); - EXPECT_EQ(kint64max , options->GetExtension(protobuf_unittest::int64_opt)); + EXPECT_EQ(true, options->GetExtension(protobuf_unittest::bool_opt)); + EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::int32_opt)); + EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::int64_opt)); EXPECT_EQ(kuint32max, options->GetExtension(protobuf_unittest::uint32_opt)); EXPECT_EQ(kuint64max, options->GetExtension(protobuf_unittest::uint64_opt)); - EXPECT_EQ(kint32max , options->GetExtension(protobuf_unittest::sint32_opt)); - EXPECT_EQ(kint64max , options->GetExtension(protobuf_unittest::sint64_opt)); + EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::sint32_opt)); + EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::sint64_opt)); EXPECT_EQ(kuint32max, options->GetExtension(protobuf_unittest::fixed32_opt)); EXPECT_EQ(kuint64max, options->GetExtension(protobuf_unittest::fixed64_opt)); - EXPECT_EQ(kint32max , options->GetExtension(protobuf_unittest::sfixed32_opt)); - EXPECT_EQ(kint64max , options->GetExtension(protobuf_unittest::sfixed64_opt)); + EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::sfixed32_opt)); + EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::sfixed64_opt)); - options = - &protobuf_unittest::CustomOptionOtherValues::descriptor()->options(); + options = &protobuf_unittest::CustomOptionOtherValues::descriptor()->options(); EXPECT_EQ(-100, options->GetExtension(protobuf_unittest::int32_opt)); EXPECT_FLOAT_EQ(12.3456789, options->GetExtension(protobuf_unittest::float_opt)); @@ -2992,7 +3048,7 @@ TEST(CustomOptions, OptionTypes) { EXPECT_EQ("Hello, \"World\"", options->GetExtension(protobuf_unittest::string_opt)); - EXPECT_EQ(string("Hello\0World", 11), + EXPECT_EQ(std::string("Hello\0World", 11), options->GetExtension(protobuf_unittest::bytes_opt)); EXPECT_EQ(protobuf_unittest::DummyMessageContainingEnum::TEST_OPTION_ENUM_TYPE2, @@ -3013,35 +3069,52 @@ TEST(CustomOptions, ComplexExtensionOptions) { const MessageOptions* options = &protobuf_unittest::VariousComplexOptions::descriptor()->options(); EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1).foo(), 42); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1). - GetExtension(protobuf_unittest::quux), 324); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1). - GetExtension(protobuf_unittest::corge).qux(), 876); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1) + .GetExtension(protobuf_unittest::quux), + 324); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1) + .GetExtension(protobuf_unittest::corge) + .qux(), + 876); EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).baz(), 987); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - GetExtension(protobuf_unittest::grault), 654); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .GetExtension(protobuf_unittest::grault), + 654); EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).bar().foo(), 743); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).bar(). - GetExtension(protobuf_unittest::quux), 1999); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).bar(). - GetExtension(protobuf_unittest::corge).qux(), 2008); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - GetExtension(protobuf_unittest::garply).foo(), 741); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - GetExtension(protobuf_unittest::garply). - GetExtension(protobuf_unittest::quux), 1998); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - GetExtension(protobuf_unittest::garply). - GetExtension(protobuf_unittest::corge).qux(), 2121); - EXPECT_EQ(options->GetExtension( - protobuf_unittest::ComplexOptionType2::ComplexOptionType4::complex_opt4). - waldo(), 1971); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - fred().waldo(), 321); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .bar() + .GetExtension(protobuf_unittest::quux), + 1999); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .bar() + .GetExtension(protobuf_unittest::corge) + .qux(), + 2008); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .GetExtension(protobuf_unittest::garply) + .foo(), + 741); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .GetExtension(protobuf_unittest::garply) + .GetExtension(protobuf_unittest::quux), + 1998); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .GetExtension(protobuf_unittest::garply) + .GetExtension(protobuf_unittest::corge) + .qux(), + 2121); + EXPECT_EQ(options + ->GetExtension(protobuf_unittest::ComplexOptionType2:: + ComplexOptionType4::complex_opt4) + .waldo(), + 1971); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).fred().waldo(), + 321); EXPECT_EQ(9, options->GetExtension(protobuf_unittest::complex_opt3).qux()); - EXPECT_EQ(22, options->GetExtension(protobuf_unittest::complex_opt3). - complexoptiontype5().plugh()); + EXPECT_EQ(22, options->GetExtension(protobuf_unittest::complex_opt3) + .complexoptiontype5() + .plugh()); EXPECT_EQ(24, options->GetExtension(protobuf_unittest::complexopt6).xyzzy()); } @@ -3052,48 +3125,47 @@ TEST(CustomOptions, OptionsFromOtherFile) { FileDescriptorProto file_proto; FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" " - "options { " - " uninterpreted_option { " - " name { " - " name_part: \"file_opt1\" " - " is_extension: true " - " } " - " positive_int_value: 1234 " - " } " - // Test a non-extension option too. (At one point this failed due to a - // bug.) - " uninterpreted_option { " - " name { " - " name_part: \"java_package\" " - " is_extension: false " - " } " - " string_value: \"foo\" " - " } " - // Test that enum-typed options still work too. (At one point this also - // failed due to a bug.) - " uninterpreted_option { " - " name { " - " name_part: \"optimize_for\" " - " is_extension: false " - " } " - " identifier_value: \"SPEED\" " - " } " - "}" - , - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" " + "options { " + " uninterpreted_option { " + " name { " + " name_part: \"file_opt1\" " + " is_extension: true " + " } " + " positive_int_value: 1234 " + " } " + // Test a non-extension option too. (At one point this failed due to a + // bug.) + " uninterpreted_option { " + " name { " + " name_part: \"java_package\" " + " is_extension: false " + " } " + " string_value: \"foo\" " + " } " + // Test that enum-typed options still work too. (At one point this also + // failed due to a bug.) + " uninterpreted_option { " + " name { " + " name_part: \"optimize_for\" " + " is_extension: false " + " } " + " identifier_value: \"SPEED\" " + " } " + "}", + &file_proto)); const FileDescriptor* file = pool.BuildFile(file_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); EXPECT_EQ(1234, file->options().GetExtension(protobuf_unittest::file_opt1)); EXPECT_TRUE(file->options().has_java_package()); EXPECT_EQ("foo", file->options().java_package()); @@ -3111,11 +3183,11 @@ TEST(CustomOptions, MessageOptionThreeFieldsSet) { FileDescriptorProto file_proto; FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // The following represents the definition: // @@ -3127,51 +3199,51 @@ TEST(CustomOptions, MessageOptionThreeFieldsSet) { // option (complex_opt1).foo3 = 1234; // } ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" " - "message_type { " - " name: \"Foo\" " - " options { " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo\" " - " is_extension: false " - " } " - " positive_int_value: 1234 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo2\" " - " is_extension: false " - " } " - " positive_int_value: 1234 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo3\" " - " is_extension: false " - " } " - " positive_int_value: 1234 " - " } " - " } " - "}", - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" " + "message_type { " + " name: \"Foo\" " + " options { " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo\" " + " is_extension: false " + " } " + " positive_int_value: 1234 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo2\" " + " is_extension: false " + " } " + " positive_int_value: 1234 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo3\" " + " is_extension: false " + " } " + " positive_int_value: 1234 " + " } " + " } " + "}", + &file_proto)); const FileDescriptor* file = pool.BuildFile(file_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); ASSERT_EQ(1, file->message_type_count()); const MessageOptions& options = file->message_type(0)->options(); @@ -3188,11 +3260,11 @@ TEST(CustomOptions, MessageOptionRepeatedLeafFieldSet) { FileDescriptorProto file_proto; FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // The following represents the definition: // @@ -3204,51 +3276,51 @@ TEST(CustomOptions, MessageOptionRepeatedLeafFieldSet) { // option (complex_opt1).foo4 = 56; // } ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" " - "message_type { " - " name: \"Foo\" " - " options { " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo4\" " - " is_extension: false " - " } " - " positive_int_value: 12 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo4\" " - " is_extension: false " - " } " - " positive_int_value: 34 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo4\" " - " is_extension: false " - " } " - " positive_int_value: 56 " - " } " - " } " - "}", - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" " + "message_type { " + " name: \"Foo\" " + " options { " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo4\" " + " is_extension: false " + " } " + " positive_int_value: 12 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo4\" " + " is_extension: false " + " } " + " positive_int_value: 34 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo4\" " + " is_extension: false " + " } " + " positive_int_value: 56 " + " } " + " } " + "}", + &file_proto)); const FileDescriptor* file = pool.BuildFile(file_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); ASSERT_EQ(1, file->message_type_count()); const MessageOptions& options = file->message_type(0)->options(); @@ -3268,11 +3340,11 @@ TEST(CustomOptions, MessageOptionRepeatedMsgFieldSet) { FileDescriptorProto file_proto; FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // The following represents the definition: // @@ -3284,62 +3356,64 @@ TEST(CustomOptions, MessageOptionRepeatedMsgFieldSet) { // option (complex_opt2).barney = {waldo: 100}; // } ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" " - "message_type { " - " name: \"Foo\" " - " options { " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt2\" " - " is_extension: true " - " } " - " name { " - " name_part: \"barney\" " - " is_extension: false " - " } " - " aggregate_value: \"waldo: 1\" " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt2\" " - " is_extension: true " - " } " - " name { " - " name_part: \"barney\" " - " is_extension: false " - " } " - " aggregate_value: \"waldo: 10\" " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt2\" " - " is_extension: true " - " } " - " name { " - " name_part: \"barney\" " - " is_extension: false " - " } " - " aggregate_value: \"waldo: 100\" " - " } " - " } " - "}", - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" " + "message_type { " + " name: \"Foo\" " + " options { " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt2\" " + " is_extension: true " + " } " + " name { " + " name_part: \"barney\" " + " is_extension: false " + " } " + " aggregate_value: \"waldo: 1\" " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt2\" " + " is_extension: true " + " } " + " name { " + " name_part: \"barney\" " + " is_extension: false " + " } " + " aggregate_value: \"waldo: 10\" " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt2\" " + " is_extension: true " + " } " + " name { " + " name_part: \"barney\" " + " is_extension: false " + " } " + " aggregate_value: \"waldo: 100\" " + " } " + " } " + "}", + &file_proto)); const FileDescriptor* file = pool.BuildFile(file_proto); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); ASSERT_EQ(1, file->message_type_count()); const MessageOptions& options = file->message_type(0)->options(); - EXPECT_EQ(3, options.GetExtension( - protobuf_unittest::complex_opt2).barney_size()); - EXPECT_EQ(1,options.GetExtension( - protobuf_unittest::complex_opt2).barney(0).waldo()); - EXPECT_EQ(10, options.GetExtension( - protobuf_unittest::complex_opt2).barney(1).waldo()); - EXPECT_EQ(100, options.GetExtension( - protobuf_unittest::complex_opt2).barney(2).waldo()); + EXPECT_EQ(3, + options.GetExtension(protobuf_unittest::complex_opt2).barney_size()); + EXPECT_EQ( + 1, options.GetExtension(protobuf_unittest::complex_opt2).barney(0).waldo()); + EXPECT_EQ( + 10, + options.GetExtension(protobuf_unittest::complex_opt2).barney(1).waldo()); + EXPECT_EQ( + 100, + options.GetExtension(protobuf_unittest::complex_opt2).barney(2).waldo()); } // Check that aggregate options were parsed and saved correctly in @@ -3350,8 +3424,8 @@ TEST(CustomOptions, AggregateOptions) { const FieldDescriptor* field = msg->FindFieldByName("fieldname"); const EnumDescriptor* enumd = file->FindEnumTypeByName("AggregateEnum"); const EnumValueDescriptor* enumv = enumd->FindValueByName("VALUE"); - const ServiceDescriptor* service = file->FindServiceByName( - "AggregateService"); + const ServiceDescriptor* service = + file->FindServiceByName("AggregateService"); const MethodDescriptor* method = service->FindMethodByName("Method"); // Tests for the different types of data embedded in fileopt @@ -3363,9 +3437,10 @@ TEST(CustomOptions, AggregateOptions) { EXPECT_EQ("FileExtensionAnnotation", file_options.file().GetExtension(protobuf_unittest::fileopt).s()); EXPECT_EQ("EmbeddedMessageSetElement", - file_options.mset().GetExtension( - protobuf_unittest::AggregateMessageSetElement - ::message_set_extension).s()); + file_options.mset() + .GetExtension(protobuf_unittest::AggregateMessageSetElement :: + message_set_extension) + .s()); // Simple tests for all the other types of annotations EXPECT_EQ("MessageAnnotation", @@ -3387,18 +3462,18 @@ TEST(CustomOptions, UnusedImportWarning) { FileDescriptorProto file_proto; FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); pool.AddUnusedImportTrackFile("custom_options_import.proto"); ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" ", - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" ", + &file_proto)); MockErrorCollector error_collector; EXPECT_TRUE(pool.BuildFileCollectingErrors(file_proto, &error_collector)); @@ -3413,7 +3488,7 @@ TEST(CustomOptions, OptionsWithIncompatibleDescriptors) { FileDescriptorProto file_proto; MessageOptions::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // Create a new file descriptor proto containing a subset of the // messages defined in google/protobuf/unittest_custom_options.proto. @@ -3424,35 +3499,36 @@ TEST(CustomOptions, OptionsWithIncompatibleDescriptors) { // Add the "required_enum_opt" extension. FieldDescriptorProto* extension = file_proto.add_extension(); - protobuf_unittest::OldOptionType::descriptor()->file() - ->FindExtensionByName("required_enum_opt")->CopyTo(extension); + protobuf_unittest::OldOptionType::descriptor() + ->file() + ->FindExtensionByName("required_enum_opt") + ->CopyTo(extension); // Add a test message that uses the "required_enum_opt" option. DescriptorProto* test_message_type = file_proto.add_message_type(); - protobuf_unittest::TestMessageWithRequiredEnumOption::descriptor() - ->CopyTo(test_message_type); + protobuf_unittest::TestMessageWithRequiredEnumOption::descriptor()->CopyTo( + test_message_type); // Instruct the extension to use NewOptionType instead of // OldOptionType, and add the descriptor of NewOptionType. extension->set_type_name(".protobuf_unittest.NewOptionType"); DescriptorProto* new_option_type = file_proto.add_message_type(); - protobuf_unittest::NewOptionType::descriptor() - ->CopyTo(new_option_type); + protobuf_unittest::NewOptionType::descriptor()->CopyTo(new_option_type); // Replace the value of the "required_enum_opt" option used in the // test message with an enum value that only exists in NewOptionType. - ASSERT_TRUE(TextFormat::ParseFromString( - "uninterpreted_option { " - " name { " - " name_part: 'required_enum_opt' " - " is_extension: true " - " } " - " aggregate_value: 'value: NEW_VALUE'" - "}", - test_message_type->mutable_options())); + ASSERT_TRUE( + TextFormat::ParseFromString("uninterpreted_option { " + " name { " + " name_part: 'required_enum_opt' " + " is_extension: true " + " } " + " aggregate_value: 'value: NEW_VALUE'" + "}", + test_message_type->mutable_options())); // Adding the file descriptor to the pool should fail. - EXPECT_TRUE(pool.BuildFile(file_proto) == NULL); + EXPECT_TRUE(pool.BuildFile(file_proto) == nullptr); } // Test that FileDescriptor::DebugString() formats custom options correctly. @@ -3461,7 +3537,7 @@ TEST(CustomOptions, DebugString) { FileDescriptorProto file_proto; MessageOptions::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // Add "foo.proto": // import "google/protobuf/descriptor.proto"; @@ -3513,7 +3589,7 @@ TEST(CustomOptions, DebugString) { "}", &file_proto)); const FileDescriptor* descriptor = pool.BuildFile(file_proto); - ASSERT_TRUE(descriptor != NULL); + ASSERT_TRUE(descriptor != nullptr); EXPECT_EQ(2, descriptor->extension_count()); @@ -3540,7 +3616,7 @@ class ValidationErrorTest : public testing::Test { protected: // Parse file_text as a FileDescriptorProto in text format and add it // to the DescriptorPool. Expect no errors. - const FileDescriptor* BuildFile(const string& file_text) { + const FileDescriptor* BuildFile(const std::string& file_text) { FileDescriptorProto file_proto; EXPECT_TRUE(TextFormat::ParseFromString(file_text, &file_proto)); return GOOGLE_CHECK_NOTNULL(pool_.BuildFile(file_proto)); @@ -3549,22 +3625,22 @@ class ValidationErrorTest : public testing::Test { // Parse file_text as a FileDescriptorProto in text format and add it // to the DescriptorPool. Expect errors to be produced which match the // given error text. - void BuildFileWithErrors(const string& file_text, - const string& expected_errors) { + void BuildFileWithErrors(const std::string& file_text, + const std::string& expected_errors) { FileDescriptorProto file_proto; ASSERT_TRUE(TextFormat::ParseFromString(file_text, &file_proto)); MockErrorCollector error_collector; - EXPECT_TRUE( - pool_.BuildFileCollectingErrors(file_proto, &error_collector) == NULL); + EXPECT_TRUE(pool_.BuildFileCollectingErrors(file_proto, &error_collector) == + nullptr); EXPECT_EQ(expected_errors, error_collector.text_); } // Parse file_text as a FileDescriptorProto in text format and add it // to the DescriptorPool. Expect errors to be produced which match the // given warning text. - void BuildFileWithWarnings(const string& file_text, - const string& expected_warnings) { + void BuildFileWithWarnings(const std::string& file_text, + const std::string& expected_warnings) { FileDescriptorProto file_proto; ASSERT_TRUE(TextFormat::ParseFromString(file_text, &file_proto)); @@ -3577,7 +3653,7 @@ class ValidationErrorTest : public testing::Test { void BuildFileInTestPool(const FileDescriptor* file) { FileDescriptorProto file_proto; file->CopyTo(&file_proto); - ASSERT_TRUE(pool_.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool_.BuildFile(file_proto) != nullptr); } // Build descriptor.proto in our test pool. This allows us to extend it in @@ -3591,57 +3667,57 @@ class ValidationErrorTest : public testing::Test { TEST_F(ValidationErrorTest, AlreadyDefined) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" }" - "message_type { name: \"Foo\" }", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" }" + "message_type { name: \"Foo\" }", - "foo.proto: Foo: NAME: \"Foo\" is already defined.\n"); + "foo.proto: Foo: NAME: \"Foo\" is already defined.\n"); } TEST_F(ValidationErrorTest, AlreadyDefinedInPackage) { BuildFileWithErrors( - "name: \"foo.proto\" " - "package: \"foo.bar\" " - "message_type { name: \"Foo\" }" - "message_type { name: \"Foo\" }", + "name: \"foo.proto\" " + "package: \"foo.bar\" " + "message_type { name: \"Foo\" }" + "message_type { name: \"Foo\" }", - "foo.proto: foo.bar.Foo: NAME: \"Foo\" is already defined in " + "foo.proto: foo.bar.Foo: NAME: \"Foo\" is already defined in " "\"foo.bar\".\n"); } TEST_F(ValidationErrorTest, AlreadyDefinedInOtherFile) { BuildFile( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" }"); + "name: \"foo.proto\" " + "message_type { name: \"Foo\" }"); BuildFileWithErrors( - "name: \"bar.proto\" " - "message_type { name: \"Foo\" }", + "name: \"bar.proto\" " + "message_type { name: \"Foo\" }", - "bar.proto: Foo: NAME: \"Foo\" is already defined in file " + "bar.proto: Foo: NAME: \"Foo\" is already defined in file " "\"foo.proto\".\n"); } TEST_F(ValidationErrorTest, PackageAlreadyDefined) { BuildFile( - "name: \"foo.proto\" " - "message_type { name: \"foo\" }"); + "name: \"foo.proto\" " + "message_type { name: \"foo\" }"); BuildFileWithErrors( - "name: \"bar.proto\" " - "package: \"foo.bar\"", + "name: \"bar.proto\" " + "package: \"foo.bar\"", - "bar.proto: foo: NAME: \"foo\" is already defined (as something other " + "bar.proto: foo: NAME: \"foo\" is already defined (as something other " "than a package) in file \"foo.proto\".\n"); } TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParent) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } " - "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ", + "name: \"foo.proto\" " + "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } " + "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ", - "foo.proto: FOO: NAME: \"FOO\" is already defined.\n" - "foo.proto: FOO: NAME: Note that enum values use C++ scoping rules, " + "foo.proto: FOO: NAME: \"FOO\" is already defined.\n" + "foo.proto: FOO: NAME: Note that enum values use C++ scoping rules, " "meaning that enum values are siblings of their type, not children of " "it. Therefore, \"FOO\" must be unique within the global scope, not " "just within \"Bar\".\n"); @@ -3649,13 +3725,13 @@ TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParent) { TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParentNonGlobal) { BuildFileWithErrors( - "name: \"foo.proto\" " - "package: \"pkg\" " - "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } " - "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ", + "name: \"foo.proto\" " + "package: \"pkg\" " + "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } " + "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ", - "foo.proto: pkg.FOO: NAME: \"FOO\" is already defined in \"pkg\".\n" - "foo.proto: pkg.FOO: NAME: Note that enum values use C++ scoping rules, " + "foo.proto: pkg.FOO: NAME: \"FOO\" is already defined in \"pkg\".\n" + "foo.proto: pkg.FOO: NAME: Note that enum values use C++ scoping rules, " "meaning that enum values are siblings of their type, not children of " "it. Therefore, \"FOO\" must be unique within \"pkg\", not just within " "\"Bar\".\n"); @@ -3663,60 +3739,60 @@ TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParentNonGlobal) { TEST_F(ValidationErrorTest, MissingName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { }", + "name: \"foo.proto\" " + "message_type { }", - "foo.proto: : NAME: Missing name.\n"); + "foo.proto: : NAME: Missing name.\n"); } TEST_F(ValidationErrorTest, InvalidName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"$\" }", + "name: \"foo.proto\" " + "message_type { name: \"$\" }", - "foo.proto: $: NAME: \"$\" is not a valid identifier.\n"); + "foo.proto: $: NAME: \"$\" is not a valid identifier.\n"); } TEST_F(ValidationErrorTest, InvalidPackageName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "package: \"foo.$\"", + "name: \"foo.proto\" " + "package: \"foo.$\"", - "foo.proto: foo.$: NAME: \"$\" is not a valid identifier.\n"); + "foo.proto: foo.$: NAME: \"$\" is not a valid identifier.\n"); } TEST_F(ValidationErrorTest, MissingFileName) { - BuildFileWithErrors( - "", + BuildFileWithErrors("", - ": : OTHER: Missing field: FileDescriptorProto.name.\n"); + ": : OTHER: Missing field: FileDescriptorProto.name.\n"); } TEST_F(ValidationErrorTest, DupeDependency) { BuildFile("name: \"foo.proto\""); BuildFileWithErrors( - "name: \"bar.proto\" " - "dependency: \"foo.proto\" " - "dependency: \"foo.proto\" ", + "name: \"bar.proto\" " + "dependency: \"foo.proto\" " + "dependency: \"foo.proto\" ", - "bar.proto: bar.proto: OTHER: Import \"foo.proto\" was listed twice.\n"); + "bar.proto: foo.proto: IMPORT: Import \"foo.proto\" was listed twice.\n"); } TEST_F(ValidationErrorTest, UnknownDependency) { BuildFileWithErrors( - "name: \"bar.proto\" " - "dependency: \"foo.proto\" ", + "name: \"bar.proto\" " + "dependency: \"foo.proto\" ", - "bar.proto: bar.proto: OTHER: Import \"foo.proto\" has not been loaded.\n"); + "bar.proto: foo.proto: IMPORT: Import \"foo.proto\" has not been " + "loaded.\n"); } TEST_F(ValidationErrorTest, InvalidPublicDependencyIndex) { BuildFile("name: \"foo.proto\""); BuildFileWithErrors( - "name: \"bar.proto\" " - "dependency: \"foo.proto\" " - "public_dependency: 1", - "bar.proto: bar.proto: OTHER: Invalid public dependency index.\n"); + "name: \"bar.proto\" " + "dependency: \"foo.proto\" " + "public_dependency: 1", + "bar.proto: bar.proto: OTHER: Invalid public dependency index.\n"); } TEST_F(ValidationErrorTest, ForeignUnimportedPackageNoCrash) { @@ -3726,475 +3802,494 @@ TEST_F(ValidationErrorTest, ForeignUnimportedPackageNoCrash) { // include that parent package in the name (i.e. we do a relative lookup)... // Yes, really. BuildFile( - "name: 'foo.proto' " - "package: 'outer.foo' "); - BuildFileWithErrors( - "name: 'bar.proto' " - "dependency: 'baz.proto' " - "package: 'outer.bar' " - "message_type { " - " name: 'Bar' " - " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'foo.Foo' }" - "}", - - "bar.proto: bar.proto: OTHER: Import \"baz.proto\" has not been loaded.\n" - "bar.proto: outer.bar.Bar.bar: TYPE: \"outer.foo\" seems to be defined in " + "name: 'foo.proto' " + "package: 'outer.foo' "); + BuildFileWithErrors( + "name: 'bar.proto' " + "dependency: 'baz.proto' " + "package: 'outer.bar' " + "message_type { " + " name: 'Bar' " + " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'foo.Foo' }" + "}", + + "bar.proto: baz.proto: IMPORT: Import \"baz.proto\" has not been " + "loaded.\n" + "bar.proto: outer.bar.Bar.bar: TYPE: \"outer.foo\" seems to be defined " + "in " "\"foo.proto\", which is not imported by \"bar.proto\". To use it here, " "please add the necessary import.\n"); } TEST_F(ValidationErrorTest, DupeFile) { BuildFile( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" }"); + "name: \"foo.proto\" " + "message_type { name: \"Foo\" }"); // Note: We should *not* get redundant errors about "Foo" already being // defined. BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - // Add another type so that the files aren't identical (in which case there - // would be no error). - "enum_type { name: \"Bar\" }", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + // Add another type so that the files aren't identical (in which case + // there would be no error). + "enum_type { name: \"Bar\" }", - "foo.proto: foo.proto: OTHER: A file with this name is already in the " + "foo.proto: foo.proto: OTHER: A file with this name is already in the " "pool.\n"); } TEST_F(ValidationErrorTest, FieldInExtensionRange) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 9 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"bar\" number: 10 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"baz\" number: 19 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"qux\" number: 20 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " extension_range { start: 10 end: 20 }" - "}", - - "foo.proto: Foo.bar: NUMBER: Extension range 10 to 19 includes field " + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 9 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"bar\" number: 10 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"baz\" number: 19 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"qux\" number: 20 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " extension_range { start: 10 end: 20 }" + "}", + + "foo.proto: Foo.bar: NUMBER: Extension range 10 to 19 includes field " "\"bar\" (10).\n" - "foo.proto: Foo.baz: NUMBER: Extension range 10 to 19 includes field " + "foo.proto: Foo.baz: NUMBER: Extension range 10 to 19 includes field " "\"baz\" (19).\n"); } TEST_F(ValidationErrorTest, OverlappingExtensionRanges) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 20 }" - " extension_range { start: 20 end: 30 }" - " extension_range { start: 19 end: 21 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 20 }" + " extension_range { start: 20 end: 30 }" + " extension_range { start: 19 end: 21 }" + "}", - "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with " + "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with " "already-defined range 10 to 19.\n" - "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with " + "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with " "already-defined range 20 to 29.\n"); } TEST_F(ValidationErrorTest, ReservedFieldError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " reserved_range { start: 10 end: 20 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " reserved_range { start: 10 end: 20 }" + "}", - "foo.proto: Foo.foo: NUMBER: Field \"foo\" uses reserved number 15.\n"); + "foo.proto: Foo.foo: NUMBER: Field \"foo\" uses reserved number 15.\n"); } TEST_F(ValidationErrorTest, ReservedExtensionRangeError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 15 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 15 }" + "}", - "foo.proto: Foo: NUMBER: Extension range 10 to 19" - " overlaps with reserved range 5 to 14.\n"); + "foo.proto: Foo: NUMBER: Extension range 10 to 19" + " overlaps with reserved range 5 to 14.\n"); } TEST_F(ValidationErrorTest, ReservedExtensionRangeAdjacent) { BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 10 }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 10 }" + "}"); } TEST_F(ValidationErrorTest, ReservedRangeOverlap) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " reserved_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 15 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " reserved_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 15 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range 5 to 14" - " overlaps with already-defined range 10 to 19.\n"); + "foo.proto: Foo: NUMBER: Reserved range 5 to 14" + " overlaps with already-defined range 10 to 19.\n"); } TEST_F(ValidationErrorTest, ReservedNameError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"bar\" number: 16 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"baz\" number: 17 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"bar\" number: 16 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"baz\" number: 17 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + "}", - "foo.proto: Foo.foo: NAME: Field name \"foo\" is reserved.\n" - "foo.proto: Foo.bar: NAME: Field name \"bar\" is reserved.\n"); + "foo.proto: Foo.foo: NAME: Field name \"foo\" is reserved.\n" + "foo.proto: Foo.bar: NAME: Field name \"bar\" is reserved.\n"); } TEST_F(ValidationErrorTest, ReservedNameRedundant) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " reserved_name: \"foo\"" - " reserved_name: \"foo\"" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " reserved_name: \"foo\"" + " reserved_name: \"foo\"" + "}", - "foo.proto: foo: NAME: Field name \"foo\" is reserved multiple times.\n"); + "foo.proto: foo: NAME: Field name \"foo\" is reserved multiple times.\n"); } TEST_F(ValidationErrorTest, ReservedFieldsDebugString) { const FileDescriptor* file = BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - " reserved_range { start: 5 end: 6 }" - " reserved_range { start: 10 end: 20 }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + " reserved_range { start: 5 end: 6 }" + " reserved_range { start: 10 end: 20 }" + "}"); ASSERT_EQ( - "syntax = \"proto2\";\n\n" - "message Foo {\n" - " reserved 5, 10 to 19;\n" - " reserved \"foo\", \"bar\";\n" - "}\n\n", - file->DebugString()); + "syntax = \"proto2\";\n\n" + "message Foo {\n" + " reserved 5, 10 to 19;\n" + " reserved \"foo\", \"bar\";\n" + "}\n\n", + file->DebugString()); } TEST_F(ValidationErrorTest, EnumReservedFieldError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:15 }" - " reserved_range { start: 10 end: 20 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:15 }" + " reserved_range { start: 10 end: 20 }" + "}", - "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number 15.\n"); + "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number 15.\n"); } TEST_F(ValidationErrorTest, EnumNegativeReservedFieldError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:-15 }" - " reserved_range { start: -20 end: -10 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:-15 }" + " reserved_range { start: -20 end: -10 }" + "}", - "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number -15.\n"); + "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number -15.\n"); } TEST_F(ValidationErrorTest, EnumReservedRangeOverlap) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:0 }" - " reserved_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 15 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:0 }" + " reserved_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 15 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range 5 to 15" - " overlaps with already-defined range 10 to 20.\n"); + "foo.proto: Foo: NUMBER: Reserved range 5 to 15" + " overlaps with already-defined range 10 to 20.\n"); } TEST_F(ValidationErrorTest, EnumReservedRangeOverlapByOne) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:0 }" - " reserved_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 10 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:0 }" + " reserved_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 10 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range 5 to 10" - " overlaps with already-defined range 10 to 20.\n"); + "foo.proto: Foo: NUMBER: Reserved range 5 to 10" + " overlaps with already-defined range 10 to 20.\n"); } TEST_F(ValidationErrorTest, EnumNegativeReservedRangeOverlap) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:0 }" - " reserved_range { start: -20 end: -10 }" - " reserved_range { start: -15 end: -5 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:0 }" + " reserved_range { start: -20 end: -10 }" + " reserved_range { start: -15 end: -5 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range -15 to -5" - " overlaps with already-defined range -20 to -10.\n"); + "foo.proto: Foo: NUMBER: Reserved range -15 to -5" + " overlaps with already-defined range -20 to -10.\n"); } TEST_F(ValidationErrorTest, EnumMixedReservedRangeOverlap) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:20 }" - " reserved_range { start: -20 end: 10 }" - " reserved_range { start: -15 end: 5 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:20 }" + " reserved_range { start: -20 end: 10 }" + " reserved_range { start: -15 end: 5 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range -15 to 5" - " overlaps with already-defined range -20 to 10.\n"); + "foo.proto: Foo: NUMBER: Reserved range -15 to 5" + " overlaps with already-defined range -20 to 10.\n"); } TEST_F(ValidationErrorTest, EnumMixedReservedRangeOverlap2) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:20 }" - " reserved_range { start: -20 end: 10 }" - " reserved_range { start: 10 end: 10 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:20 }" + " reserved_range { start: -20 end: 10 }" + " reserved_range { start: 10 end: 10 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range 10 to 10" - " overlaps with already-defined range -20 to 10.\n"); + "foo.proto: Foo: NUMBER: Reserved range 10 to 10" + " overlaps with already-defined range -20 to 10.\n"); } TEST_F(ValidationErrorTest, EnumReservedRangeStartGreaterThanEnd) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:20 }" - " reserved_range { start: 11 end: 10 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:20 }" + " reserved_range { start: 11 end: 10 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range end number must be greater" - " than start number.\n"); + "foo.proto: Foo: NUMBER: Reserved range end number must be greater" + " than start number.\n"); } TEST_F(ValidationErrorTest, EnumReservedNameError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"FOO\" number:15 }" - " value { name:\"BAR\" number:15 }" - " reserved_name: \"FOO\"" - " reserved_name: \"BAR\"" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"FOO\" number:15 }" + " value { name:\"BAR\" number:15 }" + " reserved_name: \"FOO\"" + " reserved_name: \"BAR\"" + "}", - "foo.proto: FOO: NAME: Enum value \"FOO\" is reserved.\n" - "foo.proto: BAR: NAME: Enum value \"BAR\" is reserved.\n"); + "foo.proto: FOO: NAME: Enum value \"FOO\" is reserved.\n" + "foo.proto: BAR: NAME: Enum value \"BAR\" is reserved.\n"); } TEST_F(ValidationErrorTest, EnumReservedNameRedundant) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"FOO\" number:15 }" - " reserved_name: \"foo\"" - " reserved_name: \"foo\"" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"FOO\" number:15 }" + " reserved_name: \"foo\"" + " reserved_name: \"foo\"" + "}", - "foo.proto: foo: NAME: Enum value \"foo\" is reserved multiple times.\n"); + "foo.proto: foo: NAME: Enum value \"foo\" is reserved multiple times.\n"); } TEST_F(ValidationErrorTest, EnumReservedFieldsDebugString) { const FileDescriptor* file = BuildFile( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"FOO\" number:3 }" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - " reserved_range { start: -6 end: -6 }" - " reserved_range { start: -5 end: -4 }" - " reserved_range { start: -1 end: 1 }" - " reserved_range { start: 5 end: 5 }" - " reserved_range { start: 10 end: 19 }" - "}"); + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"FOO\" number:3 }" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + " reserved_range { start: -6 end: -6 }" + " reserved_range { start: -5 end: -4 }" + " reserved_range { start: -1 end: 1 }" + " reserved_range { start: 5 end: 5 }" + " reserved_range { start: 10 end: 19 }" + "}"); ASSERT_EQ( - "syntax = \"proto2\";\n\n" - "enum Foo {\n" - " FOO = 3;\n" - " reserved -6, -5 to -4, -1 to 1, 5, 10 to 19;\n" - " reserved \"foo\", \"bar\";\n" - "}\n\n", - file->DebugString()); + "syntax = \"proto2\";\n\n" + "enum Foo {\n" + " FOO = 3;\n" + " reserved -6, -5 to -4, -1 to 1, 5, 10 to 19;\n" + " reserved \"foo\", \"bar\";\n" + "}\n\n", + file->DebugString()); } TEST_F(ValidationErrorTest, InvalidDefaults) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" - // Invalid number. - " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32" - " default_value: \"abc\" }" + // Invalid number. + " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32" + " default_value: \"abc\" }" - // Empty default value. - " field { name: \"bar\" number: 2 label: LABEL_OPTIONAL type: TYPE_INT32" - " default_value: \"\" }" + // Empty default value. + " field { name: \"bar\" number: 2 label: LABEL_OPTIONAL type: TYPE_INT32" + " default_value: \"\" }" - // Invalid boolean. - " field { name: \"baz\" number: 3 label: LABEL_OPTIONAL type: TYPE_BOOL" - " default_value: \"abc\" }" + // Invalid boolean. + " field { name: \"baz\" number: 3 label: LABEL_OPTIONAL type: TYPE_BOOL" + " default_value: \"abc\" }" - // Messages can't have defaults. - " field { name: \"qux\" number: 4 label: LABEL_OPTIONAL type: TYPE_MESSAGE" - " default_value: \"abc\" type_name: \"Foo\" }" + // Messages can't have defaults. + " field { name: \"qux\" number: 4 label: LABEL_OPTIONAL type: " + "TYPE_MESSAGE" + " default_value: \"abc\" type_name: \"Foo\" }" - // Same thing, but we don't know that this field has message type until - // we look up the type name. - " field { name: \"quux\" number: 5 label: LABEL_OPTIONAL" - " default_value: \"abc\" type_name: \"Foo\" }" + // Same thing, but we don't know that this field has message type until + // we look up the type name. + " field { name: \"quux\" number: 5 label: LABEL_OPTIONAL" + " default_value: \"abc\" type_name: \"Foo\" }" - // Repeateds can't have defaults. - " field { name: \"corge\" number: 6 label: LABEL_REPEATED type: TYPE_INT32" - " default_value: \"1\" }" - "}", + // Repeateds can't have defaults. + " field { name: \"corge\" number: 6 label: LABEL_REPEATED type: " + "TYPE_INT32" + " default_value: \"1\" }" + "}", - "foo.proto: Foo.foo: DEFAULT_VALUE: Couldn't parse default value \"abc\".\n" - "foo.proto: Foo.bar: DEFAULT_VALUE: Couldn't parse default value \"\".\n" - "foo.proto: Foo.baz: DEFAULT_VALUE: Boolean default must be true or " + "foo.proto: Foo.foo: DEFAULT_VALUE: Couldn't parse default value " + "\"abc\".\n" + "foo.proto: Foo.bar: DEFAULT_VALUE: Couldn't parse default value \"\".\n" + "foo.proto: Foo.baz: DEFAULT_VALUE: Boolean default must be true or " "false.\n" - "foo.proto: Foo.qux: DEFAULT_VALUE: Messages can't have default values.\n" - "foo.proto: Foo.corge: DEFAULT_VALUE: Repeated fields can't have default " + "foo.proto: Foo.qux: DEFAULT_VALUE: Messages can't have default values.\n" + "foo.proto: Foo.corge: DEFAULT_VALUE: Repeated fields can't have default " "values.\n" - // This ends up being reported later because the error is detected at - // cross-linking time. - "foo.proto: Foo.quux: DEFAULT_VALUE: Messages can't have default " + // This ends up being reported later because the error is detected at + // cross-linking time. + "foo.proto: Foo.quux: DEFAULT_VALUE: Messages can't have default " "values.\n"); } TEST_F(ValidationErrorTest, NegativeFieldNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: -1 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: -1 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + "}", - "foo.proto: Foo.foo: NUMBER: Field numbers must be positive integers.\n"); + "foo.proto: Foo.foo: NUMBER: Field numbers must be positive integers.\n"); } TEST_F(ValidationErrorTest, HugeFieldNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 0x70000000 " - " label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 0x70000000 " + " label:LABEL_OPTIONAL type:TYPE_INT32 }" + "}", - "foo.proto: Foo.foo: NUMBER: Field numbers cannot be greater than " + "foo.proto: Foo.foo: NUMBER: Field numbers cannot be greater than " "536870911.\n"); } TEST_F(ValidationErrorTest, ReservedFieldNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field {name:\"foo\" number: 18999 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field {name:\"bar\" number: 19000 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field {name:\"baz\" number: 19999 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field {name:\"qux\" number: 20000 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field {name:\"foo\" number: 18999 label:LABEL_OPTIONAL " + "type:TYPE_INT32 }" + " field {name:\"bar\" number: 19000 label:LABEL_OPTIONAL " + "type:TYPE_INT32 }" + " field {name:\"baz\" number: 19999 label:LABEL_OPTIONAL " + "type:TYPE_INT32 }" + " field {name:\"qux\" number: 20000 label:LABEL_OPTIONAL " + "type:TYPE_INT32 }" + "}", - "foo.proto: Foo.bar: NUMBER: Field numbers 19000 through 19999 are " + "foo.proto: Foo.bar: NUMBER: Field numbers 19000 through 19999 are " "reserved for the protocol buffer library implementation.\n" - "foo.proto: Foo.baz: NUMBER: Field numbers 19000 through 19999 are " + "foo.proto: Foo.baz: NUMBER: Field numbers 19000 through 19999 are " "reserved for the protocol buffer library implementation.\n"); } TEST_F(ValidationErrorTest, ExtensionMissingExtendee) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension { name: \"foo\" number: 1 label: LABEL_OPTIONAL" - " type_name: \"Foo\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension { name: \"foo\" number: 1 label: LABEL_OPTIONAL" + " type_name: \"Foo\" }" + "}", - "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee not set for " + "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee not set for " "extension field.\n"); } TEST_F(ValidationErrorTest, NonExtensionWithExtendee) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - " extension_range { start: 1 end: 2 }" - "}" - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL" - " type_name: \"Foo\" extendee: \"Bar\" }" - "}", - - "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee set for " + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + " extension_range { start: 1 end: 2 }" + "}" + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL" + " type_name: \"Foo\" extendee: \"Bar\" }" + "}", + + "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee set for " "non-extension field.\n"); } TEST_F(ValidationErrorTest, FieldOneofIndexTooLarge) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " - " oneof_index: 1 }" - " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 " - " oneof_index: 0 }" - " oneof_decl { name:\"bar\" }" - "}", - - "foo.proto: Foo.foo: OTHER: FieldDescriptorProto.oneof_index 1 is out of " + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " + " oneof_index: 1 }" + " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 " + " oneof_index: 0 }" + " oneof_decl { name:\"bar\" }" + "}", + + "foo.proto: Foo.foo: TYPE: FieldDescriptorProto.oneof_index 1 is out of " "range for type \"Foo\".\n"); } TEST_F(ValidationErrorTest, FieldOneofIndexNegative) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " - " oneof_index: -1 }" - " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 " - " oneof_index: 0 }" - " oneof_decl { name:\"bar\" }" - "}", - - "foo.proto: Foo.foo: OTHER: FieldDescriptorProto.oneof_index -1 is out of " + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " + " oneof_index: -1 }" + " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 " + " oneof_index: 0 }" + " oneof_decl { name:\"bar\" }" + "}", + + "foo.proto: Foo.foo: TYPE: FieldDescriptorProto.oneof_index -1 is out " + "of " "range for type \"Foo\".\n"); } @@ -4212,7 +4307,7 @@ TEST_F(ValidationErrorTest, OneofFieldsConsecutiveDefinition) { " oneof_decl { name:\"foos\" }" "}", - "foo.proto: Foo.bar: OTHER: Fields in the same oneof must be defined " + "foo.proto: Foo.bar: TYPE: Fields in the same oneof must be defined " "consecutively. \"bar\" cannot be defined before the completion of the " "\"foos\" oneof definition.\n"); @@ -4232,10 +4327,10 @@ TEST_F(ValidationErrorTest, OneofFieldsConsecutiveDefinition) { " oneof_decl { name:\"foos\" }" " oneof_decl { name:\"bars\" }" "}", - "foo2.proto: Foo2.bar1: OTHER: Fields in the same oneof must be defined " + "foo2.proto: Foo2.bar1: TYPE: Fields in the same oneof must be defined " "consecutively. \"bar1\" cannot be defined before the completion of the " "\"foos\" oneof definition.\n" - "foo2.proto: Foo2.foo2: OTHER: Fields in the same oneof must be defined " + "foo2.proto: Foo2.foo2: TYPE: Fields in the same oneof must be defined " "consecutively. \"foo2\" cannot be defined before the completion of the " "\"bars\" oneof definition.\n"); @@ -4254,202 +4349,205 @@ TEST_F(ValidationErrorTest, OneofFieldsConsecutiveDefinition) { " oneof_decl { name:\"foos\" }" " oneof_decl { name:\"bars\" }" "}", - "foo3.proto: Foo3.baz: OTHER: Fields in the same oneof must be defined " + "foo3.proto: Foo3.baz: TYPE: Fields in the same oneof must be defined " "consecutively. \"baz\" cannot be defined before the completion of the " "\"foos\" oneof definition.\n"); } TEST_F(ValidationErrorTest, FieldNumberConflict) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"bar\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" + " field { name: \"bar\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" + "}", - "foo.proto: Foo.bar: NUMBER: Field number 1 has already been used in " + "foo.proto: Foo.bar: NUMBER: Field number 1 has already been used in " "\"Foo\" by field \"foo\".\n"); } TEST_F(ValidationErrorTest, BadMessageSetExtensionType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"MessageSet\"" - " options { message_set_wire_format: true }" - " extension_range { start: 4 end: 5 }" - "}" - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:4 label:LABEL_OPTIONAL type:TYPE_INT32" - " extendee: \"MessageSet\" }" - "}", - - "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional " + "name: \"foo.proto\" " + "message_type {" + " name: \"MessageSet\"" + " options { message_set_wire_format: true }" + " extension_range { start: 4 end: 5 }" + "}" + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:4 label:LABEL_OPTIONAL type:TYPE_INT32" + " extendee: \"MessageSet\" }" + "}", + + "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional " "messages.\n"); } TEST_F(ValidationErrorTest, BadMessageSetExtensionLabel) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"MessageSet\"" - " options { message_set_wire_format: true }" - " extension_range { start: 4 end: 5 }" - "}" - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:4 label:LABEL_REPEATED type:TYPE_MESSAGE" - " type_name: \"Foo\" extendee: \"MessageSet\" }" - "}", - - "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional " + "name: \"foo.proto\" " + "message_type {" + " name: \"MessageSet\"" + " options { message_set_wire_format: true }" + " extension_range { start: 4 end: 5 }" + "}" + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:4 label:LABEL_REPEATED " + "type:TYPE_MESSAGE" + " type_name: \"Foo\" extendee: \"MessageSet\" }" + "}", + + "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional " "messages.\n"); } TEST_F(ValidationErrorTest, FieldInMessageSet) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " options { message_set_wire_format: true }" - " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " options { message_set_wire_format: true }" + " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" + "}", - "foo.proto: Foo.foo: NAME: MessageSets cannot have fields, only " + "foo.proto: Foo.foo: NAME: MessageSets cannot have fields, only " "extensions.\n"); } TEST_F(ValidationErrorTest, NegativeExtensionRangeNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: -10 end: -1 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: -10 end: -1 }" + "}", - "foo.proto: Foo: NUMBER: Extension numbers must be positive integers.\n"); + "foo.proto: Foo: NUMBER: Extension numbers must be positive integers.\n"); } TEST_F(ValidationErrorTest, HugeExtensionRangeNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 1 end: 0x70000000 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 1 end: 0x70000000 }" + "}", - "foo.proto: Foo: NUMBER: Extension numbers cannot be greater than " + "foo.proto: Foo: NUMBER: Extension numbers cannot be greater than " "536870911.\n"); } TEST_F(ValidationErrorTest, ExtensionRangeEndBeforeStart) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 10 }" - " extension_range { start: 10 end: 5 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 10 }" + " extension_range { start: 10 end: 5 }" + "}", - "foo.proto: Foo: NUMBER: Extension range end number must be greater than " + "foo.proto: Foo: NUMBER: Extension range end number must be greater than " "start number.\n" - "foo.proto: Foo: NUMBER: Extension range end number must be greater than " + "foo.proto: Foo: NUMBER: Extension range end number must be greater than " "start number.\n"); } TEST_F(ValidationErrorTest, EmptyEnum) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Foo\" }" - // Also use the empty enum in a message to make sure there are no crashes - // during validation (possible if the code attempts to derive a default - // value for the field). - "message_type {" - " name: \"Bar\"" - " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type_name:\"Foo\" }" - " field { name: \"bar\" number: 2 label:LABEL_OPTIONAL type_name:\"Foo\" " - " default_value: \"NO_SUCH_VALUE\" }" - "}", - - "foo.proto: Foo: NAME: Enums must contain at least one value.\n" - "foo.proto: Bar.bar: DEFAULT_VALUE: Enum type \"Foo\" has no value named " + "name: \"foo.proto\" " + "enum_type { name: \"Foo\" }" + // Also use the empty enum in a message to make sure there are no crashes + // during validation (possible if the code attempts to derive a default + // value for the field). + "message_type {" + " name: \"Bar\"" + " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL " + "type_name:\"Foo\" }" + " field { name: \"bar\" number: 2 label:LABEL_OPTIONAL " + "type_name:\"Foo\" " + " default_value: \"NO_SUCH_VALUE\" }" + "}", + + "foo.proto: Foo: NAME: Enums must contain at least one value.\n" + "foo.proto: Bar.bar: DEFAULT_VALUE: Enum type \"Foo\" has no value named " "\"NO_SUCH_VALUE\".\n"); } TEST_F(ValidationErrorTest, UndefinedExtendee) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" - " extendee: \"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" + " extendee: \"Bar\" }" + "}", - "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not defined.\n"); + "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not defined.\n"); } TEST_F(ValidationErrorTest, NonMessageExtendee) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } }" - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" - " extendee: \"Bar\" }" - "}", + "name: \"foo.proto\" " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } }" + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" + " extendee: \"Bar\" }" + "}", - "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not a message type.\n"); + "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not a message type.\n"); } TEST_F(ValidationErrorTest, NotAnExtensionNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - "}" - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" - " extendee: \"Bar\" }" - "}", - - "foo.proto: Foo.foo: NUMBER: \"Bar\" does not declare 1 as an extension " + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + "}" + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" + " extendee: \"Bar\" }" + "}", + + "foo.proto: Foo.foo: NUMBER: \"Bar\" does not declare 1 as an extension " "number.\n"); } TEST_F(ValidationErrorTest, RequiredExtension) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - " extension_range { start: 1000 end: 10000 }" - "}" - "message_type {" - " name: \"Foo\"" - " extension {" - " name:\"foo\"" - " number:1000" - " label:LABEL_REQUIRED" - " type:TYPE_INT32" - " extendee: \"Bar\"" - " }" - "}", - - "foo.proto: Foo.foo: TYPE: Message extensions cannot have required " - "fields.\n"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + " extension_range { start: 1000 end: 10000 }" + "}" + "message_type {" + " name: \"Foo\"" + " extension {" + " name:\"foo\"" + " number:1000" + " label:LABEL_REQUIRED" + " type:TYPE_INT32" + " extendee: \"Bar\"" + " }" + "}", + + "foo.proto: Foo.foo: TYPE: Message extensions cannot have required " + "fields.\n"); } TEST_F(ValidationErrorTest, UndefinedFieldType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" is not defined.\n"); + "foo.proto: Foo.foo: TYPE: \"Bar\" is not defined.\n"); } TEST_F(ValidationErrorTest, UndefinedFieldTypeWithDefault) { @@ -4458,42 +4556,42 @@ TEST_F(ValidationErrorTest, UndefinedFieldTypeWithDefault) { // error message. We want this input to yield a validation error instead, // since the unknown type is the primary problem. BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"int\" " - " default_value:\"1\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"int\" " + " default_value:\"1\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"int\" is not defined.\n"); + "foo.proto: Foo.foo: TYPE: \"int\" is not defined.\n"); } TEST_F(ValidationErrorTest, UndefinedNestedFieldType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " nested_type { name:\"Baz\" }" - " field { name:\"foo\" number:1" - " label:LABEL_OPTIONAL" - " type_name:\"Foo.Baz.Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " nested_type { name:\"Baz\" }" + " field { name:\"foo\" number:1" + " label:LABEL_OPTIONAL" + " type_name:\"Foo.Baz.Bar\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"Foo.Baz.Bar\" is not defined.\n"); + "foo.proto: Foo.foo: TYPE: \"Foo.Baz.Bar\" is not defined.\n"); } TEST_F(ValidationErrorTest, FieldTypeDefinedInUndeclaredDependency) { BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" } "); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" } "); BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}", + "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " "which is not imported by \"foo.proto\". To use it here, please add the " "necessary import.\n"); } @@ -4514,21 +4612,21 @@ TEST_F(ValidationErrorTest, FieldTypeDefinedInIndirectDependency) { // } // BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"forward.proto\"" - "dependency: \"bar.proto\""); + "name: \"forward.proto\"" + "dependency: \"bar.proto\""); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"forward.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " + "name: \"foo.proto\" " + "dependency: \"forward.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}", + "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " "which is not imported by \"foo.proto\". To use it here, please add the " "necessary import.\n"); } @@ -4551,21 +4649,21 @@ TEST_F(ValidationErrorTest, FieldTypeDefinedInPublicDependency) { // } // BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"forward.proto\"" - "dependency: \"bar.proto\" " - "public_dependency: 0"); + "name: \"forward.proto\"" + "dependency: \"bar.proto\" " + "public_dependency: 0"); BuildFile( - "name: \"foo.proto\" " - "dependency: \"forward.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}"); + "name: \"foo.proto\" " + "dependency: \"forward.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}"); } TEST_F(ValidationErrorTest, FieldTypeDefinedInTransitivePublicDependency) { @@ -4587,26 +4685,26 @@ TEST_F(ValidationErrorTest, FieldTypeDefinedInTransitivePublicDependency) { // } // BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"forward.proto\"" - "dependency: \"bar.proto\" " - "public_dependency: 0"); + "name: \"forward.proto\"" + "dependency: \"bar.proto\" " + "public_dependency: 0"); BuildFile( - "name: \"forward2.proto\"" - "dependency: \"forward.proto\" " - "public_dependency: 0"); + "name: \"forward2.proto\"" + "dependency: \"forward.proto\" " + "public_dependency: 0"); BuildFile( - "name: \"foo.proto\" " - "dependency: \"forward2.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}"); + "name: \"foo.proto\" " + "dependency: \"forward2.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}"); } TEST_F(ValidationErrorTest, @@ -4631,26 +4729,26 @@ TEST_F(ValidationErrorTest, // } // BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"forward.proto\"" - "dependency: \"bar.proto\""); + "name: \"forward.proto\"" + "dependency: \"bar.proto\""); BuildFile( - "name: \"forward2.proto\"" - "dependency: \"forward.proto\" " - "public_dependency: 0"); - - BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"forward2.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " + "name: \"forward2.proto\"" + "dependency: \"forward.proto\" " + "public_dependency: 0"); + + BuildFileWithErrors( + "name: \"foo.proto\" " + "dependency: \"forward2.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}", + "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " "which is not imported by \"foo.proto\". To use it here, please add the " "necessary import.\n"); } @@ -4672,22 +4770,22 @@ TEST_F(ValidationErrorTest, SearchMostLocalFirst) { // fail, and ten try "Bar.Baz" and succeed, even though "Bar" should actually // refer to the inner Bar, not the outer one. BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - " nested_type { name: \"Baz\" }" - "}" - "message_type {" - " name: \"Foo\"" - " nested_type { name: \"Bar\" }" - " field { name:\"baz\" number:1 label:LABEL_OPTIONAL" - " type_name:\"Bar.Baz\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + " nested_type { name: \"Baz\" }" + "}" + "message_type {" + " name: \"Foo\"" + " nested_type { name: \"Bar\" }" + " field { name:\"baz\" number:1 label:LABEL_OPTIONAL" + " type_name:\"Bar.Baz\" }" + "}", - "foo.proto: Foo.baz: TYPE: \"Bar.Baz\" is resolved to \"Foo.Bar.Baz\"," - " which is not defined. The innermost scope is searched first in name " - "resolution. Consider using a leading '.'(i.e., \".Bar.Baz\") to start " - "from the outermost scope.\n"); + "foo.proto: Foo.baz: TYPE: \"Bar.Baz\" is resolved to \"Foo.Bar.Baz\"," + " which is not defined. The innermost scope is searched first in name " + "resolution. Consider using a leading '.'(i.e., \".Bar.Baz\") to start " + "from the outermost scope.\n"); } TEST_F(ValidationErrorTest, SearchMostLocalFirst2) { @@ -4695,17 +4793,17 @@ TEST_F(ValidationErrorTest, SearchMostLocalFirst2) { // proceeds to find the outer one because the inner one's not an // aggregate. BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - " nested_type { name: \"Baz\" }" - "}" - "message_type {" - " name: \"Foo\"" - " field { name: \"Bar\" number:1 type:TYPE_BYTES } " - " field { name:\"baz\" number:2 label:LABEL_OPTIONAL" - " type_name:\"Bar.Baz\" }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + " nested_type { name: \"Baz\" }" + "}" + "message_type {" + " name: \"Foo\"" + " field { name: \"Bar\" number:1 type:TYPE_BYTES } " + " field { name:\"baz\" number:2 label:LABEL_OPTIONAL" + " type_name:\"Bar.Baz\" }" + "}"); } TEST_F(ValidationErrorTest, PackageOriginallyDeclaredInTransitiveDependent) { @@ -4731,349 +4829,348 @@ TEST_F(ValidationErrorTest, PackageOriginallyDeclaredInTransitiveDependent) { // prevent it from finding "foo.bar". BuildFile( - "name: \"foo.proto\" " - "package: \"foo.bar\" "); + "name: \"foo.proto\" " + "package: \"foo.bar\" "); BuildFile( - "name: \"bar.proto\" " - "package: \"foo.bar\" " - "dependency: \"foo.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "package: \"foo.bar\" " + "dependency: \"foo.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"baz.proto\" " - "package: \"foo\" " - "dependency: \"bar.proto\" " - "message_type { " - " name: \"Baz\" " - " field { name:\"qux\" number:1 label:LABEL_OPTIONAL " - " type_name:\"bar.Bar\" }" - "}"); + "name: \"baz.proto\" " + "package: \"foo\" " + "dependency: \"bar.proto\" " + "message_type { " + " name: \"Baz\" " + " field { name:\"qux\" number:1 label:LABEL_OPTIONAL " + " type_name:\"bar.Bar\" }" + "}"); } TEST_F(ValidationErrorTest, FieldTypeNotAType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL " - " type_name:\".Foo.bar\" }" - " field { name:\"bar\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL " + " type_name:\".Foo.bar\" }" + " field { name:\"bar\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" + "}", - "foo.proto: Foo.foo: TYPE: \".Foo.bar\" is not a type.\n"); + "foo.proto: Foo.foo: TYPE: \".Foo.bar\" is not a type.\n"); } TEST_F(ValidationErrorTest, RelativeFieldTypeNotAType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " nested_type {" - " name: \"Bar\"" - " field { name:\"Baz\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " }" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL " - " type_name:\"Bar.Baz\" }" - "}", - "foo.proto: Foo.foo: TYPE: \"Bar.Baz\" is not a type.\n"); + "name: \"foo.proto\" " + "message_type {" + " nested_type {" + " name: \"Bar\"" + " field { name:\"Baz\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" + " }" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL " + " type_name:\"Bar.Baz\" }" + "}", + "foo.proto: Foo.foo: TYPE: \"Bar.Baz\" is not a type.\n"); } TEST_F(ValidationErrorTest, FieldTypeMayBeItsName) { BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - "}" - "message_type {" - " name: \"Foo\"" - " field { name:\"Bar\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + "}" + "message_type {" + " name: \"Foo\"" + " field { name:\"Bar\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}"); } TEST_F(ValidationErrorTest, EnumFieldTypeIsMessage) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Bar\" } " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM" - " type_name:\"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Bar\" } " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM" + " type_name:\"Bar\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" is not an enum type.\n"); + "foo.proto: Foo.foo: TYPE: \"Bar\" is not an enum type.\n"); } TEST_F(ValidationErrorTest, MessageFieldTypeIsEnum) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE" - " type_name:\"Bar\" }" - "}", + "name: \"foo.proto\" " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE" + " type_name:\"Bar\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" is not a message type.\n"); + "foo.proto: Foo.foo: TYPE: \"Bar\" is not a message type.\n"); } TEST_F(ValidationErrorTest, BadEnumDefaultValue) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\"" - " default_value:\"NO_SUCH_VALUE\" }" - "}", + "name: \"foo.proto\" " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\"" + " default_value:\"NO_SUCH_VALUE\" }" + "}", - "foo.proto: Foo.foo: DEFAULT_VALUE: Enum type \"Bar\" has no value named " + "foo.proto: Foo.foo: DEFAULT_VALUE: Enum type \"Bar\" has no value named " "\"NO_SUCH_VALUE\".\n"); } TEST_F(ValidationErrorTest, EnumDefaultValueIsInteger) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\"" - " default_value:\"0\" }" - "}", + "name: \"foo.proto\" " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\"" + " default_value:\"0\" }" + "}", - "foo.proto: Foo.foo: DEFAULT_VALUE: Default value for an enum field must " - "be an identifier.\n"); + "foo.proto: Foo.foo: DEFAULT_VALUE: Default value for an enum field must " + "be an identifier.\n"); } TEST_F(ValidationErrorTest, PrimitiveWithTypeName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" - " type_name:\"Foo\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" + " type_name:\"Foo\" }" + "}", - "foo.proto: Foo.foo: TYPE: Field with primitive type has type_name.\n"); + "foo.proto: Foo.foo: TYPE: Field with primitive type has type_name.\n"); } TEST_F(ValidationErrorTest, NonPrimitiveWithoutTypeName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE }" + "}", - "foo.proto: Foo.foo: TYPE: Field with message or enum type missing " + "foo.proto: Foo.foo: TYPE: Field with message or enum type missing " "type_name.\n"); } TEST_F(ValidationErrorTest, OneofWithNoFields) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " oneof_decl { name:\"bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " oneof_decl { name:\"bar\" }" + "}", - "foo.proto: Foo.bar: NAME: Oneof must have at least one field.\n"); + "foo.proto: Foo.bar: NAME: Oneof must have at least one field.\n"); } TEST_F(ValidationErrorTest, OneofLabelMismatch) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_REPEATED type:TYPE_INT32 " - " oneof_index:0 }" - " oneof_decl { name:\"bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_REPEATED type:TYPE_INT32 " + " oneof_index:0 }" + " oneof_decl { name:\"bar\" }" + "}", - "foo.proto: Foo.foo: NAME: Fields of oneofs must themselves have label " + "foo.proto: Foo.foo: NAME: Fields of oneofs must themselves have label " "LABEL_OPTIONAL.\n"); } TEST_F(ValidationErrorTest, InputTypeNotDefined) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "service {" - " name: \"TestService\"" - " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "service {" + " name: \"TestService\"" + " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }" + "}", - "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not defined.\n" - ); + "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not defined.\n" + ); } TEST_F(ValidationErrorTest, InputTypeNotAMessage) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "service {" - " name: \"TestService\"" - " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "service {" + " name: \"TestService\"" + " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }" + "}", - "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not a message type.\n" - ); + "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not a message type.\n" + ); } TEST_F(ValidationErrorTest, OutputTypeNotDefined) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "service {" - " name: \"TestService\"" - " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "service {" + " name: \"TestService\"" + " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }" + "}", - "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not defined.\n" - ); + "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not defined.\n" + ); } TEST_F(ValidationErrorTest, OutputTypeNotAMessage) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "service {" - " name: \"TestService\"" - " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "service {" + " name: \"TestService\"" + " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }" + "}", - "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not a message type.\n" - ); + "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not a message type.\n" + ); } TEST_F(ValidationErrorTest, IllegalPackedField) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {\n" - " name: \"Foo\"" - " field { name:\"packed_string\" number:1 label:LABEL_REPEATED " - " type:TYPE_STRING " - " options { uninterpreted_option {" - " name { name_part: \"packed\" is_extension: false }" - " identifier_value: \"true\" }}}\n" - " field { name:\"packed_message\" number:3 label:LABEL_REPEATED " - " type_name: \"Foo\"" - " options { uninterpreted_option {" - " name { name_part: \"packed\" is_extension: false }" - " identifier_value: \"true\" }}}\n" - " field { name:\"optional_int32\" number: 4 label: LABEL_OPTIONAL " - " type:TYPE_INT32 " - " options { uninterpreted_option {" - " name { name_part: \"packed\" is_extension: false }" - " identifier_value: \"true\" }}}\n" - "}", - - "foo.proto: Foo.packed_string: TYPE: [packed = true] can only be " - "specified for repeated primitive fields.\n" - "foo.proto: Foo.packed_message: TYPE: [packed = true] can only be " - "specified for repeated primitive fields.\n" - "foo.proto: Foo.optional_int32: TYPE: [packed = true] can only be " - "specified for repeated primitive fields.\n" - ); + "name: \"foo.proto\" " + "message_type {\n" + " name: \"Foo\"" + " field { name:\"packed_string\" number:1 label:LABEL_REPEATED " + " type:TYPE_STRING " + " options { uninterpreted_option {" + " name { name_part: \"packed\" is_extension: false }" + " identifier_value: \"true\" }}}\n" + " field { name:\"packed_message\" number:3 label:LABEL_REPEATED " + " type_name: \"Foo\"" + " options { uninterpreted_option {" + " name { name_part: \"packed\" is_extension: false }" + " identifier_value: \"true\" }}}\n" + " field { name:\"optional_int32\" number: 4 label: LABEL_OPTIONAL " + " type:TYPE_INT32 " + " options { uninterpreted_option {" + " name { name_part: \"packed\" is_extension: false }" + " identifier_value: \"true\" }}}\n" + "}", + + "foo.proto: Foo.packed_string: TYPE: [packed = true] can only be " + "specified for repeated primitive fields.\n" + "foo.proto: Foo.packed_message: TYPE: [packed = true] can only be " + "specified for repeated primitive fields.\n" + "foo.proto: Foo.optional_int32: TYPE: [packed = true] can only be " + "specified for repeated primitive fields.\n"); } TEST_F(ValidationErrorTest, OptionWrongType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { " - " name: \"TestMessage\" " - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING " - " options { uninterpreted_option { name { name_part: \"ctype\" " - " is_extension: false }" - " positive_int_value: 1 }" - " }" - " }" - "}\n", + "name: \"foo.proto\" " + "message_type { " + " name: \"TestMessage\" " + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING " + " options { uninterpreted_option { name { name_part: \"ctype\" " + " is_extension: false }" + " positive_int_value: 1 }" + " }" + " }" + "}\n", - "foo.proto: TestMessage.foo: OPTION_VALUE: Value must be identifier for " - "enum-valued option \"google.protobuf.FieldOptions.ctype\".\n"); + "foo.proto: TestMessage.foo: OPTION_VALUE: Value must be identifier for " + "enum-valued option \"google.protobuf.FieldOptions.ctype\".\n"); } TEST_F(ValidationErrorTest, OptionExtendsAtomicType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { " - " name: \"TestMessage\" " - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING " - " options { uninterpreted_option { name { name_part: \"ctype\" " - " is_extension: false }" - " name { name_part: \"foo\" " - " is_extension: true }" - " positive_int_value: 1 }" - " }" - " }" - "}\n", + "name: \"foo.proto\" " + "message_type { " + " name: \"TestMessage\" " + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING " + " options { uninterpreted_option { name { name_part: \"ctype\" " + " is_extension: false }" + " name { name_part: \"foo\" " + " is_extension: true }" + " positive_int_value: 1 }" + " }" + " }" + "}\n", - "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" is an " - "atomic type, not a message.\n"); + "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" is an " + "atomic type, not a message.\n"); } TEST_F(ValidationErrorTest, DupOption) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { " - " name: \"TestMessage\" " - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_UINT32 " - " options { uninterpreted_option { name { name_part: \"ctype\" " - " is_extension: false }" - " identifier_value: \"CORD\" }" - " uninterpreted_option { name { name_part: \"ctype\" " - " is_extension: false }" - " identifier_value: \"CORD\" }" - " }" - " }" - "}\n", + "name: \"foo.proto\" " + "message_type { " + " name: \"TestMessage\" " + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_UINT32 " + " options { uninterpreted_option { name { name_part: \"ctype\" " + " is_extension: false }" + " identifier_value: \"CORD\" }" + " uninterpreted_option { name { name_part: \"ctype\" " + " is_extension: false }" + " identifier_value: \"CORD\" }" + " }" + " }" + "}\n", - "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" was " - "already set.\n"); + "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" was " + "already set.\n"); } TEST_F(ValidationErrorTest, InvalidOptionName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { " - " name: \"TestMessage\" " - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL " - " options { uninterpreted_option { " - " name { name_part: \"uninterpreted_option\" " - " is_extension: false }" - " positive_int_value: 1 " - " }" - " }" - " }" - "}\n", + "name: \"foo.proto\" " + "message_type { " + " name: \"TestMessage\" " + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL " + " options { uninterpreted_option { " + " name { name_part: \"uninterpreted_option\" " + " is_extension: false }" + " positive_int_value: 1 " + " }" + " }" + " }" + "}\n", - "foo.proto: TestMessage.foo: OPTION_NAME: Option must not use " - "reserved name \"uninterpreted_option\".\n"); + "foo.proto: TestMessage.foo: OPTION_NAME: Option must not use " + "reserved name \"uninterpreted_option\".\n"); } TEST_F(ValidationErrorTest, RepeatedMessageOption) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "message_type: { name: \"Bar\" field: { " - " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } " - "} " - "extension { name: \"bar\" number: 7672757 label: LABEL_REPEATED " - " type: TYPE_MESSAGE type_name: \"Bar\" " - " extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"bar\" " - " is_extension: true } " - " name { name_part: \"foo\" " - " is_extension: false } " - " positive_int_value: 1 } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "message_type: { name: \"Bar\" field: { " + " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } " + "} " + "extension { name: \"bar\" number: 7672757 label: LABEL_REPEATED " + " type: TYPE_MESSAGE type_name: \"Bar\" " + " extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"bar\" " + " is_extension: true } " + " name { name_part: \"foo\" " + " is_extension: false } " + " positive_int_value: 1 } }", - "foo.proto: foo.proto: OPTION_NAME: Option field \"(bar)\" is a " - "repeated message. Repeated message options must be initialized " - "using an aggregate value.\n"); + "foo.proto: foo.proto: OPTION_NAME: Option field \"(bar)\" is a " + "repeated message. Repeated message options must be initialized " + "using an aggregate value.\n"); } TEST_F(ValidationErrorTest, ResolveUndefinedOption) { @@ -5095,335 +5192,338 @@ TEST_F(ValidationErrorTest, ResolveUndefinedOption) { BuildDescriptorMessagesInTestPool(); BuildFile( - "name: \"foo.proto\" " - "package: \"baz\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "message_type: { name: \"Bar\" field: { " - " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } " - "} " - "extension { name: \"bar\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_MESSAGE type_name: \"Bar\" " - " extendee: \"google.protobuf.FileOptions\" }"); - - BuildFileWithErrors( - "name: \"qux.proto\" " - "package: \"qux.baz\" " - "options { uninterpreted_option { name { name_part: \"baz.bar\" " - " is_extension: true } " - " name { name_part: \"foo\" " - " is_extension: false } " - " positive_int_value: 1 } }", - - "qux.proto: qux.proto: OPTION_NAME: Option \"(baz.bar)\" is resolved to " - "\"(qux.baz.bar)\"," - " which is not defined. The innermost scope is searched first in name " - "resolution. Consider using a leading '.'(i.e., \"(.baz.bar)\") to start " - "from the outermost scope.\n"); + "name: \"foo.proto\" " + "package: \"baz\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "message_type: { name: \"Bar\" field: { " + " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } " + "} " + "extension { name: \"bar\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_MESSAGE type_name: \"Bar\" " + " extendee: \"google.protobuf.FileOptions\" }"); + + BuildFileWithErrors( + "name: \"qux.proto\" " + "package: \"qux.baz\" " + "options { uninterpreted_option { name { name_part: \"baz.bar\" " + " is_extension: true } " + " name { name_part: \"foo\" " + " is_extension: false } " + " positive_int_value: 1 } }", + + "qux.proto: qux.proto: OPTION_NAME: Option \"(baz.bar)\" is resolved to " + "\"(qux.baz.bar)\"," + " which is not defined. The innermost scope is searched first in name " + "resolution. Consider using a leading '.'(i.e., \"(.baz.bar)\") to start " + "from the outermost scope.\n"); } TEST_F(ValidationErrorTest, UnknownOption) { BuildFileWithErrors( - "name: \"qux.proto\" " - "package: \"qux.baz\" " - "options { uninterpreted_option { name { name_part: \"baaz.bar\" " - " is_extension: true } " - " name { name_part: \"foo\" " - " is_extension: false } " - " positive_int_value: 1 } }", + "name: \"qux.proto\" " + "package: \"qux.baz\" " + "options { uninterpreted_option { name { name_part: \"baaz.bar\" " + " is_extension: true } " + " name { name_part: \"foo\" " + " is_extension: false } " + " positive_int_value: 1 } }", - "qux.proto: qux.proto: OPTION_NAME: Option \"(baaz.bar)\" unknown. Ensure " - "that your proto definition file imports the proto which defines the " - "option.\n"); + "qux.proto: qux.proto: OPTION_NAME: Option \"(baaz.bar)\" unknown. " + "Ensure " + "that your proto definition file imports the proto which defines the " + "option.\n"); } TEST_F(ValidationErrorTest, CustomOptionConflictingFieldNumber) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo1\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }" - "extension { name: \"foo2\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo1\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }" + "extension { name: \"foo2\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }", - "foo.proto: foo2: NUMBER: Extension number 7672757 has already been used " - "in \"google.protobuf.FieldOptions\" by extension \"foo1\".\n"); + "foo.proto: foo2: NUMBER: Extension number 7672757 has already been used " + "in \"google.protobuf.FieldOptions\" by extension \"foo1\".\n"); } TEST_F(ValidationErrorTest, Int32OptionValueOutOfPositiveRange) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " positive_int_value: 0x80000000 } " - "}", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " positive_int_value: 0x80000000 } " + "}", - "foo.proto: foo.proto: OPTION_VALUE: Value out of range " - "for int32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value out of range " + "for int32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, Int32OptionValueOutOfNegativeRange) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " negative_int_value: -0x80000001 } " - "}", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " negative_int_value: -0x80000001 } " + "}", - "foo.proto: foo.proto: OPTION_VALUE: Value out of range " - "for int32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value out of range " + "for int32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, Int32OptionValueIsNotPositiveInt) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " string_value: \"5\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " string_value: \"5\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be integer " - "for int32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be integer " + "for int32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, Int64OptionValueOutOfRange) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " positive_int_value: 0x8000000000000000 } " - "}", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " positive_int_value: 0x8000000000000000 " + "} " + "}", - "foo.proto: foo.proto: OPTION_VALUE: Value out of range " - "for int64 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value out of range " + "for int64 option \"foo\".\n"); } TEST_F(ValidationErrorTest, Int64OptionValueIsNotPositiveInt) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"5\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"5\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be integer " - "for int64 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be integer " + "for int64 option \"foo\".\n"); } TEST_F(ValidationErrorTest, UInt32OptionValueOutOfRange) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " positive_int_value: 0x100000000 } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " positive_int_value: 0x100000000 } }", - "foo.proto: foo.proto: OPTION_VALUE: Value out of range " - "for uint32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value out of range " + "for uint32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, UInt32OptionValueIsNotPositiveInt) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " double_value: -5.6 } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " double_value: -5.6 } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer " - "for uint32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer " + "for uint32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, UInt64OptionValueIsNotPositiveInt) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_UINT64 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " negative_int_value: -5 } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_UINT64 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " negative_int_value: -5 } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer " - "for uint64 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer " + "for uint64 option \"foo\".\n"); } TEST_F(ValidationErrorTest, FloatOptionValueIsNotNumber) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_FLOAT extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " string_value: \"bar\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_FLOAT extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " string_value: \"bar\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be number " - "for float option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be number " + "for float option \"foo\".\n"); } TEST_F(ValidationErrorTest, DoubleOptionValueIsNotNumber) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_DOUBLE extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " string_value: \"bar\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_DOUBLE extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " string_value: \"bar\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be number " - "for double option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be number " + "for double option \"foo\".\n"); } TEST_F(ValidationErrorTest, BoolOptionValueIsNotTrueOrFalse) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_BOOL extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"bar\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_BOOL extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"bar\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be \"true\" or \"false\" " - "for boolean option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be \"true\" or \"false\" " + "for boolean option \"foo\".\n"); } TEST_F(ValidationErrorTest, EnumOptionValueIsNotIdentifier) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } " - " value { name: \"BAZ\" number: 2 } }" - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_ENUM type_name: \"FooEnum\" " - " extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " string_value: \"QUUX\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } " + " value { name: \"BAZ\" number: 2 } }" + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_ENUM type_name: \"FooEnum\" " + " extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " string_value: \"QUUX\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be identifier for " - "enum-valued option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be identifier for " + "enum-valued option \"foo\".\n"); } TEST_F(ValidationErrorTest, EnumOptionValueIsNotEnumValueName) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } " - " value { name: \"BAZ\" number: 2 } }" - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_ENUM type_name: \"FooEnum\" " - " extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"QUUX\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } " + " value { name: \"BAZ\" number: 2 } }" + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_ENUM type_name: \"FooEnum\" " + " extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"QUUX\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum\" has no value " - "named \"QUUX\" for option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum\" has no value " + "named \"QUUX\" for option \"foo\".\n"); } TEST_F(ValidationErrorTest, EnumOptionValueIsSiblingEnumValueName) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "enum_type { name: \"FooEnum1\" value { name: \"BAR\" number: 1 } " - " value { name: \"BAZ\" number: 2 } }" - "enum_type { name: \"FooEnum2\" value { name: \"QUX\" number: 1 } " - " value { name: \"QUUX\" number: 2 } }" - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_ENUM type_name: \"FooEnum1\" " - " extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"QUUX\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "enum_type { name: \"FooEnum1\" value { name: \"BAR\" number: 1 } " + " value { name: \"BAZ\" number: 2 } }" + "enum_type { name: \"FooEnum2\" value { name: \"QUX\" number: 1 } " + " value { name: \"QUUX\" number: 2 } }" + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_ENUM type_name: \"FooEnum1\" " + " extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"QUUX\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum1\" has no value " - "named \"QUUX\" for option \"foo\". This appears to be a value from a " - "sibling type.\n"); + "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum1\" has no value " + "named \"QUUX\" for option \"foo\". This appears to be a value from a " + "sibling type.\n"); } TEST_F(ValidationErrorTest, StringOptionValueIsNotString) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_STRING extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"QUUX\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_STRING extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"QUUX\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be quoted string for " - "string option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be quoted string " + "for " + "string option \"foo\".\n"); } TEST_F(ValidationErrorTest, JsonNameOptionOnExtensions) { BuildFileWithErrors( - "name: \"foo.proto\" " - "package: \"foo\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 20 }" - "}" - "extension {" - " name: \"value\"" - " number: 10" - " label: LABEL_OPTIONAL" - " type: TYPE_INT32" - " extendee: \"foo.Foo\"" - " json_name: \"myName\"" - "}", - "foo.proto: foo.value: OPTION_NAME: option json_name is not allowed on " - "extension fields.\n"); + "name: \"foo.proto\" " + "package: \"foo\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 20 }" + "}" + "extension {" + " name: \"value\"" + " number: 10" + " label: LABEL_OPTIONAL" + " type: TYPE_INT32" + " extendee: \"foo.Foo\"" + " json_name: \"myName\"" + "}", + "foo.proto: foo.value: OPTION_NAME: option json_name is not allowed on " + "extension fields.\n"); } TEST_F(ValidationErrorTest, DuplicateExtensionFieldNumber) { @@ -5448,7 +5548,7 @@ TEST_F(ValidationErrorTest, DuplicateExtensionFieldNumber) { // Helper function for tests that check for aggregate value parsing // errors. The "value" argument is embedded inside the // "uninterpreted_option" portion of the result. -static string EmbedAggregateValue(const char* value) { +static std::string EmbedAggregateValue(const char* value) { return strings::Substitute( "name: \"foo.proto\" " "dependency: \"google/protobuf/descriptor.proto\" " @@ -5493,60 +5593,60 @@ TEST_F(ValidationErrorTest, AggregateValueUnknownFields) { TEST_F(ValidationErrorTest, NotLiteImportsLite) { BuildFile( - "name: \"bar.proto\" " - "options { optimize_for: LITE_RUNTIME } "); + "name: \"bar.proto\" " + "options { optimize_for: LITE_RUNTIME } "); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"bar.proto\" ", + "name: \"foo.proto\" " + "dependency: \"bar.proto\" ", - "foo.proto: foo.proto: OTHER: Files that do not use optimize_for = " + "foo.proto: bar.proto: IMPORT: Files that do not use optimize_for = " "LITE_RUNTIME cannot import files which do use this option. This file " "is not lite, but it imports \"bar.proto\" which is.\n"); } TEST_F(ValidationErrorTest, LiteExtendsNotLite) { BuildFile( - "name: \"bar.proto\" " - "message_type: {" - " name: \"Bar\"" - " extension_range { start: 1 end: 1000 }" - "}"); + "name: \"bar.proto\" " + "message_type: {" + " name: \"Bar\"" + " extension_range { start: 1 end: 1000 }" + "}"); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"bar.proto\" " - "options { optimize_for: LITE_RUNTIME } " - "extension { name: \"ext\" number: 123 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"Bar\" }", + "name: \"foo.proto\" " + "dependency: \"bar.proto\" " + "options { optimize_for: LITE_RUNTIME } " + "extension { name: \"ext\" number: 123 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"Bar\" }", - "foo.proto: ext: EXTENDEE: Extensions to non-lite types can only be " + "foo.proto: ext: EXTENDEE: Extensions to non-lite types can only be " "declared in non-lite files. Note that you cannot extend a non-lite " "type to contain a lite type, but the reverse is allowed.\n"); } TEST_F(ValidationErrorTest, NoLiteServices) { BuildFileWithErrors( - "name: \"foo.proto\" " - "options {" - " optimize_for: LITE_RUNTIME" - " cc_generic_services: true" - " java_generic_services: true" - "} " - "service { name: \"Foo\" }", + "name: \"foo.proto\" " + "options {" + " optimize_for: LITE_RUNTIME" + " cc_generic_services: true" + " java_generic_services: true" + "} " + "service { name: \"Foo\" }", - "foo.proto: Foo: NAME: Files with optimize_for = LITE_RUNTIME cannot " - "define services unless you set both options cc_generic_services and " - "java_generic_sevices to false.\n"); + "foo.proto: Foo: NAME: Files with optimize_for = LITE_RUNTIME cannot " + "define services unless you set both options cc_generic_services and " + "java_generic_services to false.\n"); BuildFile( - "name: \"bar.proto\" " - "options {" - " optimize_for: LITE_RUNTIME" - " cc_generic_services: false" - " java_generic_services: false" - "} " - "service { name: \"Bar\" }"); + "name: \"bar.proto\" " + "options {" + " optimize_for: LITE_RUNTIME" + " cc_generic_services: false" + " java_generic_services: false" + "} " + "service { name: \"Bar\" }"); } TEST_F(ValidationErrorTest, RollbackAfterError) { @@ -5555,47 +5655,47 @@ TEST_F(ValidationErrorTest, RollbackAfterError) { // before the undefined type error is noticed. The DescriptorPool will then // have to roll everything back. BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }" - "} " - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"BAR\" number:1 }" - "} " - "service {" - " name: \"TestService\"" - " method {" - " name: \"Baz\"" - " input_type: \"NoSuchType\"" // error - " output_type: \"TestMessage\"" - " }" - "}", - - "foo.proto: TestService.Baz: INPUT_TYPE: \"NoSuchType\" is not defined.\n" - ); + "name: \"foo.proto\" " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }" + "} " + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"BAR\" number:1 }" + "} " + "service {" + " name: \"TestService\"" + " method {" + " name: \"Baz\"" + " input_type: \"NoSuchType\"" // error + " output_type: \"TestMessage\"" + " }" + "}", + + "foo.proto: TestService.Baz: INPUT_TYPE: \"NoSuchType\" is not defined.\n" + ); // Make sure that if we build the same file again with the error fixed, // it works. If the above rollback was incomplete, then some symbols will // be left defined, and this second attempt will fail since it tries to // re-define the same symbols. BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }" - "} " - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"BAR\" number:1 }" - "} " - "service {" - " name: \"TestService\"" - " method { name:\"Baz\"" - " input_type:\"TestMessage\"" - " output_type:\"TestMessage\" }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }" + "} " + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"BAR\" number:1 }" + "} " + "service {" + " name: \"TestService\"" + " method { name:\"Baz\"" + " input_type:\"TestMessage\"" + " output_type:\"TestMessage\" }" + "}"); } TEST_F(ValidationErrorTest, ErrorsReportedToLogError) { @@ -5603,17 +5703,17 @@ TEST_F(ValidationErrorTest, ErrorsReportedToLogError) { // provided. FileDescriptorProto file_proto; - ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "message_type { name: \"Foo\" } ", - &file_proto)); + ASSERT_TRUE( + TextFormat::ParseFromString("name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "message_type { name: \"Foo\" } ", + &file_proto)); - std::vector errors; + std::vector errors; { ScopedMemoryLog log; - EXPECT_TRUE(pool_.BuildFile(file_proto) == NULL); + EXPECT_TRUE(pool_.BuildFile(file_proto) == nullptr); errors = log.GetMessages(ERROR); } @@ -5625,50 +5725,50 @@ TEST_F(ValidationErrorTest, ErrorsReportedToLogError) { TEST_F(ValidationErrorTest, DisallowEnumAlias) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Bar\"" - " value { name:\"ENUM_A\" number:0 }" - " value { name:\"ENUM_B\" number:0 }" - "}", - "foo.proto: Bar: NUMBER: " - "\"ENUM_B\" uses the same enum value as \"ENUM_A\". " - "If this is intended, set 'option allow_alias = true;' to the enum " - "definition.\n"); + "name: \"foo.proto\" " + "enum_type {" + " name: \"Bar\"" + " value { name:\"ENUM_A\" number:0 }" + " value { name:\"ENUM_B\" number:0 }" + "}", + "foo.proto: Bar: NUMBER: " + "\"ENUM_B\" uses the same enum value as \"ENUM_A\". " + "If this is intended, set 'option allow_alias = true;' to the enum " + "definition.\n"); } TEST_F(ValidationErrorTest, AllowEnumAlias) { BuildFile( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Bar\"" - " value { name:\"ENUM_A\" number:0 }" - " value { name:\"ENUM_B\" number:0 }" - " options { allow_alias: true }" - "}"); + "name: \"foo.proto\" " + "enum_type {" + " name: \"Bar\"" + " value { name:\"ENUM_A\" number:0 }" + " value { name:\"ENUM_B\" number:0 }" + " options { allow_alias: true }" + "}"); } TEST_F(ValidationErrorTest, UnusedImportWarning) { pool_.AddUnusedImportTrackFile("bar.proto"); BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); pool_.AddUnusedImportTrackFile("base.proto"); BuildFile( - "name: \"base.proto\" " - "message_type { name: \"Base\" }"); + "name: \"base.proto\" " + "message_type { name: \"Base\" }"); pool_.AddUnusedImportTrackFile("baz.proto"); BuildFile( - "name: \"baz.proto\" " - "message_type { name: \"Baz\" }"); + "name: \"baz.proto\" " + "message_type { name: \"Baz\" }"); pool_.AddUnusedImportTrackFile("public.proto"); BuildFile( - "name: \"public.proto\" " - "dependency: \"bar.proto\"" - "public_dependency: 0"); + "name: \"public.proto\" " + "dependency: \"bar.proto\"" + "public_dependency: 0"); // // forward.proto // import "base.proto" // No warning: Base message is used. @@ -5681,17 +5781,18 @@ TEST_F(ValidationErrorTest, UnusedImportWarning) { // pool_.AddUnusedImportTrackFile("forward.proto"); BuildFileWithWarnings( - "name: \"forward.proto\"" - "dependency: \"base.proto\"" - "dependency: \"bar.proto\"" - "dependency: \"baz.proto\"" - "dependency: \"public.proto\"" - "public_dependency: 2 " - "message_type {" - " name: \"Forward\"" - " field { name:\"base\" number:1 label:LABEL_OPTIONAL type_name:\"Base\" }" - "}", - "forward.proto: bar.proto: OTHER: Import bar.proto but not used.\n"); + "name: \"forward.proto\"" + "dependency: \"base.proto\"" + "dependency: \"bar.proto\"" + "dependency: \"baz.proto\"" + "dependency: \"public.proto\"" + "public_dependency: 2 " + "message_type {" + " name: \"Forward\"" + " field { name:\"base\" number:1 label:LABEL_OPTIONAL " + "type_name:\"Base\" }" + "}", + "forward.proto: bar.proto: IMPORT: Import bar.proto but not used.\n"); } namespace { @@ -5722,7 +5823,7 @@ void FillValidMapEntry(FileDescriptorProto* file_proto) { file_proto)); } static const char* kMapEntryErrorMessage = - "foo.proto: Foo.foo_map: OTHER: map_entry should not be set explicitly. " + "foo.proto: Foo.foo_map: TYPE: map_entry should not be set explicitly. " "Use map instead.\n"; static const char* kMapEntryKeyTypeErrorMessage = "foo.proto: Foo.foo_map: TYPE: Key in map fields cannot be float/double, " @@ -5826,9 +5927,9 @@ TEST_F(ValidationErrorTest, MapEntryDifferentContainingType) { TEST_F(ValidationErrorTest, MapEntryKeyName) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_name("Key"); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5836,9 +5937,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyName) { TEST_F(ValidationErrorTest, MapEntryKeyLabel) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_label(FieldDescriptorProto::LABEL_REQUIRED); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5846,9 +5947,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyLabel) { TEST_F(ValidationErrorTest, MapEntryKeyNumber) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_number(3); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5856,9 +5957,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyNumber) { TEST_F(ValidationErrorTest, MapEntryValueName) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* value = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(1); + FieldDescriptorProto* value = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 1); value->set_name("Value"); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5866,9 +5967,9 @@ TEST_F(ValidationErrorTest, MapEntryValueName) { TEST_F(ValidationErrorTest, MapEntryValueLabel) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* value = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(1); + FieldDescriptorProto* value = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 1); value->set_label(FieldDescriptorProto::LABEL_REQUIRED); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5876,9 +5977,9 @@ TEST_F(ValidationErrorTest, MapEntryValueLabel) { TEST_F(ValidationErrorTest, MapEntryValueNumber) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* value = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(1); + FieldDescriptorProto* value = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 1); value->set_number(3); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5886,9 +5987,9 @@ TEST_F(ValidationErrorTest, MapEntryValueNumber) { TEST_F(ValidationErrorTest, MapEntryKeyTypeFloat) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_type(FieldDescriptorProto::TYPE_FLOAT); BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage); } @@ -5896,9 +5997,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeFloat) { TEST_F(ValidationErrorTest, MapEntryKeyTypeDouble) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_type(FieldDescriptorProto::TYPE_DOUBLE); BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage); } @@ -5906,9 +6007,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeDouble) { TEST_F(ValidationErrorTest, MapEntryKeyTypeBytes) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_type(FieldDescriptorProto::TYPE_BYTES); BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage); } @@ -5916,9 +6017,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeBytes) { TEST_F(ValidationErrorTest, MapEntryKeyTypeEnum) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->clear_type(); key->set_type_name("BarEnum"); EnumDescriptorProto* enum_proto = file_proto.add_enum_type(); @@ -5938,13 +6039,12 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeEnum) { "be enum types.\n"); } - TEST_F(ValidationErrorTest, MapEntryKeyTypeMessage) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->clear_type(); key->set_type_name(".Bar"); BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage); @@ -6003,6 +6103,34 @@ TEST_F(ValidationErrorTest, MapEntryConflictsWithEnum) { "with an existing enum type.\n"); } +TEST_F(ValidationErrorTest, EnumValuesConflictWithDifferentCasing) { + BuildFileWithErrors( + "syntax: 'proto3'" + "name: 'foo.proto' " + "enum_type {" + " name: 'FooEnum' " + " value { name: 'BAR' number: 0 }" + " value { name: 'bar' number: 1 }" + "}", + "foo.proto: bar: NAME: Enum name bar has the same name as BAR " + "if you ignore case and strip out the enum name prefix (if any). " + "This is error-prone and can lead to undefined behavior. " + "Please avoid doing this. If you are using allow_alias, please assign " + "the same numeric value to both enums.\n"); + + // Not an error because both enums are mapped to the same value. + BuildFile( + "syntax: 'proto3'" + "name: 'foo.proto' " + "enum_type {" + " name: 'FooEnum' " + " options { allow_alias: true }" + " value { name: 'UNKNOWN' number: 0 }" + " value { name: 'BAR' number: 1 }" + " value { name: 'bar' number: 1 }" + "}"); +} + TEST_F(ValidationErrorTest, EnumValuesConflictWhenPrefixesStripped) { BuildFileWithErrors( "syntax: 'proto3'" @@ -6012,9 +6140,11 @@ TEST_F(ValidationErrorTest, EnumValuesConflictWhenPrefixesStripped) { " value { name: 'FOO_ENUM_BAZ' number: 0 }" " value { name: 'BAZ' number: 1 }" "}", - "foo.proto: BAZ: NAME: When enum name is stripped and label is " - "PascalCased (Baz), this value label conflicts with FOO_ENUM_BAZ. This " - "will make the proto fail to compile for some languages, such as C#.\n"); + "foo.proto: BAZ: NAME: Enum name BAZ has the same name as FOO_ENUM_BAZ " + "if you ignore case and strip out the enum name prefix (if any). " + "This is error-prone and can lead to undefined behavior. " + "Please avoid doing this. If you are using allow_alias, please assign " + "the same numeric value to both enums.\n"); BuildFileWithErrors( "syntax: 'proto3'" @@ -6024,9 +6154,11 @@ TEST_F(ValidationErrorTest, EnumValuesConflictWhenPrefixesStripped) { " value { name: 'FOOENUM_BAZ' number: 0 }" " value { name: 'BAZ' number: 1 }" "}", - "foo.proto: BAZ: NAME: When enum name is stripped and label is " - "PascalCased (Baz), this value label conflicts with FOOENUM_BAZ. This " - "will make the proto fail to compile for some languages, such as C#.\n"); + "foo.proto: BAZ: NAME: Enum name BAZ has the same name as FOOENUM_BAZ " + "if you ignore case and strip out the enum name prefix (if any). " + "This is error-prone and can lead to undefined behavior. " + "Please avoid doing this. If you are using allow_alias, please assign " + "the same numeric value to both enums.\n"); BuildFileWithErrors( "syntax: 'proto3'" @@ -6036,10 +6168,11 @@ TEST_F(ValidationErrorTest, EnumValuesConflictWhenPrefixesStripped) { " value { name: 'FOO_ENUM_BAR_BAZ' number: 0 }" " value { name: 'BAR__BAZ' number: 1 }" "}", - "foo.proto: BAR__BAZ: NAME: When enum name is stripped and label is " - "PascalCased (BarBaz), this value label conflicts with " - "FOO_ENUM_BAR_BAZ. This will make the proto fail to compile for some " - "languages, such as C#.\n"); + "foo.proto: BAR__BAZ: NAME: Enum name BAR__BAZ has the same name as " + "FOO_ENUM_BAR_BAZ if you ignore case and strip out the enum name prefix " + "(if any). This is error-prone and can lead to undefined behavior. " + "Please avoid doing this. If you are using allow_alias, please assign " + "the same numeric value to both enums.\n"); BuildFileWithErrors( "syntax: 'proto3'" @@ -6049,10 +6182,11 @@ TEST_F(ValidationErrorTest, EnumValuesConflictWhenPrefixesStripped) { " value { name: 'FOO_ENUM__BAR_BAZ' number: 0 }" " value { name: 'BAR_BAZ' number: 1 }" "}", - "foo.proto: BAR_BAZ: NAME: When enum name is stripped and label is " - "PascalCased (BarBaz), this value label conflicts with " - "FOO_ENUM__BAR_BAZ. This will make the proto fail to compile for some " - "languages, such as C#.\n"); + "foo.proto: BAR_BAZ: NAME: Enum name BAR_BAZ has the same name as " + "FOO_ENUM__BAR_BAZ if you ignore case and strip out the enum name prefix " + "(if any). This is error-prone and can lead to undefined behavior. " + "Please avoid doing this. If you are using allow_alias, please assign " + "the same numeric value to both enums.\n"); // This isn't an error because the underscore will cause the PascalCase to // differ by case (BarBaz vs. Barbaz). @@ -6092,31 +6226,31 @@ TEST_F(ValidationErrorTest, MapEntryConflictsWithOneof) { TEST_F(ValidationErrorTest, MapEntryUsesNoneZeroEnumDefaultValue) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Bar\"" - " value { name:\"ENUM_A\" number:1 }" - " value { name:\"ENUM_B\" number:2 }" - "}" - "message_type {" - " name: 'Foo' " - " field { " - " name: 'foo_map' number: 1 label:LABEL_REPEATED " - " type_name: 'FooMapEntry' " - " } " - " nested_type { " - " name: 'FooMapEntry' " - " options { map_entry: true } " - " field { " - " name: 'key' number: 1 type:TYPE_INT32 label:LABEL_OPTIONAL " - " } " - " field { " - " name: 'value' number: 2 type_name:\"Bar\" label:LABEL_OPTIONAL " - " } " - " } " - "}", - "foo.proto: Foo.foo_map: " - "TYPE: Enum value in map must define 0 as the first value.\n"); + "name: \"foo.proto\" " + "enum_type {" + " name: \"Bar\"" + " value { name:\"ENUM_A\" number:1 }" + " value { name:\"ENUM_B\" number:2 }" + "}" + "message_type {" + " name: 'Foo' " + " field { " + " name: 'foo_map' number: 1 label:LABEL_REPEATED " + " type_name: 'FooMapEntry' " + " } " + " nested_type { " + " name: 'FooMapEntry' " + " options { map_entry: true } " + " field { " + " name: 'key' number: 1 type:TYPE_INT32 label:LABEL_OPTIONAL " + " } " + " field { " + " name: 'value' number: 2 type_name:\"Bar\" label:LABEL_OPTIONAL " + " } " + " } " + "}", + "foo.proto: Foo.foo_map: " + "TYPE: Enum value in map must define 0 as the first value.\n"); } TEST_F(ValidationErrorTest, Proto3RequiredFields) { @@ -6127,7 +6261,7 @@ TEST_F(ValidationErrorTest, Proto3RequiredFields) { " name: 'Foo' " " field { name:'foo' number:1 label:LABEL_REQUIRED type:TYPE_INT32 } " "}", - "foo.proto: Foo.foo: OTHER: Required fields are not allowed in " + "foo.proto: Foo.foo: TYPE: Required fields are not allowed in " "proto3.\n"); // applied to nested types as well. @@ -6141,7 +6275,7 @@ TEST_F(ValidationErrorTest, Proto3RequiredFields) { " field { name:'bar' number:1 label:LABEL_REQUIRED type:TYPE_INT32 } " " } " "}", - "foo.proto: Foo.Bar.bar: OTHER: Required fields are not allowed in " + "foo.proto: Foo.Bar.bar: TYPE: Required fields are not allowed in " "proto3.\n"); // optional and repeated fields are OK. @@ -6164,8 +6298,8 @@ TEST_F(ValidationErrorTest, ValidateProto3DefaultValue) { " field { name:'foo' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " " default_value: '1' }" "}", - "foo.proto: Foo.foo: OTHER: Explicit default values are not allowed in " - "proto3.\n"); + "foo.proto: Foo.foo: DEFAULT_VALUE: Explicit default values are not " + "allowed in proto3.\n"); BuildFileWithErrors( "name: 'foo.proto' " @@ -6178,8 +6312,8 @@ TEST_F(ValidationErrorTest, ValidateProto3DefaultValue) { " default_value: '1' }" " } " "}", - "foo.proto: Foo.Bar.bar: OTHER: Explicit default values are not allowed " - "in proto3.\n"); + "foo.proto: Foo.Bar.bar: DEFAULT_VALUE: Explicit default values are not " + "allowed in proto3.\n"); } TEST_F(ValidationErrorTest, ValidateProto3ExtensionRange) { @@ -6191,7 +6325,7 @@ TEST_F(ValidationErrorTest, ValidateProto3ExtensionRange) { " field { name:'foo' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 } " " extension_range { start:10 end:100 } " "}", - "foo.proto: Foo: OTHER: Extension ranges are not allowed in " + "foo.proto: Foo: NUMBER: Extension ranges are not allowed in " "proto3.\n"); BuildFileWithErrors( @@ -6205,7 +6339,7 @@ TEST_F(ValidationErrorTest, ValidateProto3ExtensionRange) { " extension_range { start:10 end:100 } " " } " "}", - "foo.proto: Foo.Bar: OTHER: Extension ranges are not allowed in " + "foo.proto: Foo.Bar: NUMBER: Extension ranges are not allowed in " "proto3.\n"); } @@ -6217,7 +6351,7 @@ TEST_F(ValidationErrorTest, ValidateProto3MessageSetWireFormat) { " name: 'Foo' " " options { message_set_wire_format: true } " "}", - "foo.proto: Foo: OTHER: MessageSet is not supported " + "foo.proto: Foo: NAME: MessageSet is not supported " "in proto3.\n"); } @@ -6229,7 +6363,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Enum) { " name: 'FooEnum' " " value { name: 'FOO_FOO' number:1 } " "}", - "foo.proto: FooEnum: OTHER: The first enum value must be " + "foo.proto: FooEnum: NUMBER: The first enum value must be " "zero in proto3.\n"); BuildFileWithErrors( @@ -6242,7 +6376,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Enum) { " value { name: 'FOO_FOO' number:1 } " " } " "}", - "foo.proto: Foo.FooEnum: OTHER: The first enum value must be " + "foo.proto: Foo.FooEnum: NUMBER: The first enum value must be " "zero in proto3.\n"); // valid case. @@ -6304,7 +6438,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Extension) { FileDescriptorProto file_proto; // Add "google/protobuf/descriptor.proto". FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // Add "foo.proto": // import "google/protobuf/descriptor.proto"; // extend google.protobuf.FieldOptions { @@ -6317,7 +6451,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Extension) { AddExtension(&file_proto, "google.protobuf.FieldOptions", "option1", 1000, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); + ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr); // Copy and change the package of the descriptor.proto BuildFile( @@ -6334,7 +6468,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Extension) { " name: 'bar' number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 " " extendee: 'Container' " "}", - "bar.proto: bar: OTHER: Extensions in proto3 are only allowed for " + "bar.proto: bar: EXTENDEE: Extensions in proto3 are only allowed for " "defining options.\n"); } @@ -6349,7 +6483,7 @@ TEST_F(ValidationErrorTest, ValidateProto3JsonName) { " field { name:'name' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }" " field { name:'Name' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" "}", - "foo.proto: Foo: OTHER: The JSON camel-case name of field \"Name\" " + "foo.proto: Foo: NAME: The JSON camel-case name of field \"Name\" " "conflicts with field \"name\". This is not allowed in proto3.\n"); // Underscores are ignored. BuildFileWithErrors( @@ -6360,7 +6494,7 @@ TEST_F(ValidationErrorTest, ValidateProto3JsonName) { " field { name:'ab' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }" " field { name:'_a__b_' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" "}", - "foo.proto: Foo: OTHER: The JSON camel-case name of field \"_a__b_\" " + "foo.proto: Foo: NAME: The JSON camel-case name of field \"_a__b_\" " "conflicts with field \"ab\". This is not allowed in proto3.\n"); } @@ -6382,24 +6516,26 @@ class DatabaseBackedPoolTest : public testing::Test { SimpleDescriptorDatabase database_; virtual void SetUp() { + AddToDatabase( + &database_, + "name: 'foo.proto' " + "message_type { name:'Foo' extension_range { start: 1 end: 100 } } " + "enum_type { name:'TestEnum' value { name:'DUMMY' number:0 } } " + "service { name:'TestService' } "); AddToDatabase(&database_, - "name: 'foo.proto' " - "message_type { name:'Foo' extension_range { start: 1 end: 100 } } " - "enum_type { name:'TestEnum' value { name:'DUMMY' number:0 } } " - "service { name:'TestService' } "); - AddToDatabase(&database_, - "name: 'bar.proto' " - "dependency: 'foo.proto' " - "message_type { name:'Bar' } " - "extension { name:'foo_ext' extendee: '.Foo' number:5 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + "name: 'bar.proto' " + "dependency: 'foo.proto' " + "message_type { name:'Bar' } " + "extension { name:'foo_ext' extendee: '.Foo' number:5 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); // Baz has an undeclared dependency on Foo. - AddToDatabase(&database_, - "name: 'baz.proto' " - "message_type { " - " name:'Baz' " - " field { name:'foo' number:1 label:LABEL_OPTIONAL type_name:'Foo' } " - "}"); + AddToDatabase( + &database_, + "name: 'baz.proto' " + "message_type { " + " name:'Baz' " + " field { name:'foo' number:1 label:LABEL_OPTIONAL type_name:'Foo' } " + "}"); } // We can't inject a file containing errors into a DescriptorPool, so we @@ -6410,7 +6546,7 @@ class DatabaseBackedPoolTest : public testing::Test { ~ErrorDescriptorDatabase() {} // implements DescriptorDatabase --------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) { // error.proto and error2.proto cyclically import each other. if (filename == "error.proto") { @@ -6427,11 +6563,11 @@ class DatabaseBackedPoolTest : public testing::Test { return false; } } - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) { return false; } - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) { return false; @@ -6443,7 +6579,7 @@ class DatabaseBackedPoolTest : public testing::Test { class CallCountingDatabase : public DescriptorDatabase { public: CallCountingDatabase(DescriptorDatabase* wrapped_db) - : wrapped_db_(wrapped_db) { + : wrapped_db_(wrapped_db) { Clear(); } ~CallCountingDatabase() {} @@ -6452,27 +6588,25 @@ class DatabaseBackedPoolTest : public testing::Test { int call_count_; - void Clear() { - call_count_ = 0; - } + void Clear() { call_count_ = 0; } // implements DescriptorDatabase --------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) { ++call_count_; return wrapped_db_->FindFileByName(filename, output); } - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) { ++call_count_; return wrapped_db_->FindFileContainingSymbol(symbol_name, output); } - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) { ++call_count_; - return wrapped_db_->FindFileContainingExtension( - containing_type, field_number, output); + return wrapped_db_->FindFileContainingExtension(containing_type, + field_number, output); } }; @@ -6482,21 +6616,21 @@ class DatabaseBackedPoolTest : public testing::Test { class FalsePositiveDatabase : public DescriptorDatabase { public: FalsePositiveDatabase(DescriptorDatabase* wrapped_db) - : wrapped_db_(wrapped_db) {} + : wrapped_db_(wrapped_db) {} ~FalsePositiveDatabase() {} DescriptorDatabase* wrapped_db_; // implements DescriptorDatabase --------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) { return wrapped_db_->FindFileByName(filename, output); } - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) { return FindFileByName("foo.proto", output); } - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) { return FindFileByName("foo.proto", output); @@ -6508,27 +6642,27 @@ TEST_F(DatabaseBackedPoolTest, FindFileByName) { DescriptorPool pool(&database_); const FileDescriptor* foo = pool.FindFileByName("foo.proto"); - ASSERT_TRUE(foo != NULL); + ASSERT_TRUE(foo != nullptr); EXPECT_EQ("foo.proto", foo->name()); ASSERT_EQ(1, foo->message_type_count()); EXPECT_EQ("Foo", foo->message_type(0)->name()); EXPECT_EQ(foo, pool.FindFileByName("foo.proto")); - EXPECT_TRUE(pool.FindFileByName("no_such_file.proto") == NULL); + EXPECT_TRUE(pool.FindFileByName("no_such_file.proto") == nullptr); } TEST_F(DatabaseBackedPoolTest, FindDependencyBeforeDependent) { DescriptorPool pool(&database_); const FileDescriptor* foo = pool.FindFileByName("foo.proto"); - ASSERT_TRUE(foo != NULL); + ASSERT_TRUE(foo != nullptr); EXPECT_EQ("foo.proto", foo->name()); ASSERT_EQ(1, foo->message_type_count()); EXPECT_EQ("Foo", foo->message_type(0)->name()); const FileDescriptor* bar = pool.FindFileByName("bar.proto"); - ASSERT_TRUE(bar != NULL); + ASSERT_TRUE(bar != nullptr); EXPECT_EQ("bar.proto", bar->name()); ASSERT_EQ(1, bar->message_type_count()); EXPECT_EQ("Bar", bar->message_type(0)->name()); @@ -6541,13 +6675,13 @@ TEST_F(DatabaseBackedPoolTest, FindDependentBeforeDependency) { DescriptorPool pool(&database_); const FileDescriptor* bar = pool.FindFileByName("bar.proto"); - ASSERT_TRUE(bar != NULL); + ASSERT_TRUE(bar != nullptr); EXPECT_EQ("bar.proto", bar->name()); ASSERT_EQ(1, bar->message_type_count()); ASSERT_EQ("Bar", bar->message_type(0)->name()); const FileDescriptor* foo = pool.FindFileByName("foo.proto"); - ASSERT_TRUE(foo != NULL); + ASSERT_TRUE(foo != nullptr); EXPECT_EQ("foo.proto", foo->name()); ASSERT_EQ(1, foo->message_type_count()); ASSERT_EQ("Foo", foo->message_type(0)->name()); @@ -6560,36 +6694,36 @@ TEST_F(DatabaseBackedPoolTest, FindFileContainingSymbol) { DescriptorPool pool(&database_); const FileDescriptor* file = pool.FindFileContainingSymbol("Foo"); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); EXPECT_EQ("foo.proto", file->name()); EXPECT_EQ(file, pool.FindFileByName("foo.proto")); - EXPECT_TRUE(pool.FindFileContainingSymbol("NoSuchSymbol") == NULL); + EXPECT_TRUE(pool.FindFileContainingSymbol("NoSuchSymbol") == nullptr); } TEST_F(DatabaseBackedPoolTest, FindMessageTypeByName) { DescriptorPool pool(&database_); const Descriptor* type = pool.FindMessageTypeByName("Foo"); - ASSERT_TRUE(type != NULL); + ASSERT_TRUE(type != nullptr); EXPECT_EQ("Foo", type->name()); EXPECT_EQ(type->file(), pool.FindFileByName("foo.proto")); - EXPECT_TRUE(pool.FindMessageTypeByName("NoSuchType") == NULL); + EXPECT_TRUE(pool.FindMessageTypeByName("NoSuchType") == nullptr); } TEST_F(DatabaseBackedPoolTest, FindExtensionByNumber) { DescriptorPool pool(&database_); const Descriptor* foo = pool.FindMessageTypeByName("Foo"); - ASSERT_TRUE(foo != NULL); + ASSERT_TRUE(foo != nullptr); const FieldDescriptor* extension = pool.FindExtensionByNumber(foo, 5); - ASSERT_TRUE(extension != NULL); + ASSERT_TRUE(extension != nullptr); EXPECT_EQ("foo_ext", extension->name()); EXPECT_EQ(extension->file(), pool.FindFileByName("bar.proto")); - EXPECT_TRUE(pool.FindExtensionByNumber(foo, 12) == NULL); + EXPECT_TRUE(pool.FindExtensionByNumber(foo, 12) == nullptr); } TEST_F(DatabaseBackedPoolTest, FindAllExtensions) { @@ -6611,11 +6745,11 @@ TEST_F(DatabaseBackedPoolTest, ErrorWithoutErrorCollector) { ErrorDescriptorDatabase error_database; DescriptorPool pool(&error_database); - std::vector errors; + std::vector errors; { ScopedMemoryLog log; - EXPECT_TRUE(pool.FindFileByName("error.proto") == NULL); + EXPECT_TRUE(pool.FindFileByName("error.proto") == nullptr); errors = log.GetMessages(ERROR); } @@ -6627,15 +6761,15 @@ TEST_F(DatabaseBackedPoolTest, ErrorWithErrorCollector) { MockErrorCollector error_collector; DescriptorPool pool(&error_database, &error_collector); - EXPECT_TRUE(pool.FindFileByName("error.proto") == NULL); + EXPECT_TRUE(pool.FindFileByName("error.proto") == nullptr); EXPECT_EQ( - "error.proto: error.proto: OTHER: File recursively imports itself: " + "error.proto: error2.proto: IMPORT: File recursively imports itself: " "error.proto -> error2.proto -> error.proto\n" - "error2.proto: error2.proto: OTHER: Import \"error.proto\" was not " + "error2.proto: error.proto: IMPORT: Import \"error.proto\" was not " "found or had errors.\n" - "error.proto: error.proto: OTHER: Import \"error2.proto\" was not " + "error.proto: error2.proto: IMPORT: Import \"error2.proto\" was not " "found or had errors.\n", - error_collector.text_); + error_collector.text_); } TEST_F(DatabaseBackedPoolTest, UndeclaredDependencyOnUnbuiltType) { @@ -6643,12 +6777,12 @@ TEST_F(DatabaseBackedPoolTest, UndeclaredDependencyOnUnbuiltType) { // in the descriptor database but that have not not been built yet. MockErrorCollector error_collector; DescriptorPool pool(&database_, &error_collector); - EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == NULL); + EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == nullptr); EXPECT_EQ( - "baz.proto: Baz.foo: TYPE: \"Foo\" seems to be defined in \"foo.proto\", " - "which is not imported by \"baz.proto\". To use it here, please add " - "the necessary import.\n", - error_collector.text_); + "baz.proto: Baz.foo: TYPE: \"Foo\" seems to be defined in \"foo.proto\", " + "which is not imported by \"baz.proto\". To use it here, please add " + "the necessary import.\n", + error_collector.text_); } TEST_F(DatabaseBackedPoolTest, RollbackAfterError) { @@ -6659,25 +6793,25 @@ TEST_F(DatabaseBackedPoolTest, RollbackAfterError) { // Baz is defined in the database, but the file is invalid because it is // missing a necessary import. DescriptorPool pool(&database_); - EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == NULL); + EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == nullptr); // Make sure that searching again for the file or the type fails. - EXPECT_TRUE(pool.FindFileByName("baz.proto") == NULL); - EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == NULL); + EXPECT_TRUE(pool.FindFileByName("baz.proto") == nullptr); + EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == nullptr); } TEST_F(DatabaseBackedPoolTest, UnittestProto) { // Try to load all of unittest.proto from a DescriptorDatabase. This should // thoroughly test all paths through DescriptorBuilder to insure that there - // are no deadlocking problems when pool_->mutex_ is non-NULL. + // are no deadlocking problems when pool_->mutex_ is non-null. const FileDescriptor* original_file = - protobuf_unittest::TestAllTypes::descriptor()->file(); + protobuf_unittest::TestAllTypes::descriptor()->file(); DescriptorPoolDatabase database(*DescriptorPool::generated_pool()); DescriptorPool pool(&database); const FileDescriptor* file_from_database = - pool.FindFileByName(original_file->name()); + pool.FindFileByName(original_file->name()); - ASSERT_TRUE(file_from_database != NULL); + ASSERT_TRUE(file_from_database != nullptr); FileDescriptorProto original_file_proto; original_file->CopyTo(&original_file_proto); @@ -6689,8 +6823,7 @@ TEST_F(DatabaseBackedPoolTest, UnittestProto) { file_from_database_proto.DebugString()); // Also verify that CopyTo() did not omit any information. - EXPECT_EQ(original_file->DebugString(), - file_from_database->DebugString()); + EXPECT_EQ(original_file->DebugString(), file_from_database->DebugString()); } TEST_F(DatabaseBackedPoolTest, DoesntRetryDbUnnecessarily) { @@ -6701,39 +6834,39 @@ TEST_F(DatabaseBackedPoolTest, DoesntRetryDbUnnecessarily) { DescriptorPool pool(&call_counter); const FileDescriptor* file = pool.FindFileByName("foo.proto"); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); const Descriptor* foo = pool.FindMessageTypeByName("Foo"); - ASSERT_TRUE(foo != NULL); + ASSERT_TRUE(foo != nullptr); const EnumDescriptor* test_enum = pool.FindEnumTypeByName("TestEnum"); - ASSERT_TRUE(test_enum != NULL); + ASSERT_TRUE(test_enum != nullptr); const ServiceDescriptor* test_service = pool.FindServiceByName("TestService"); - ASSERT_TRUE(test_service != NULL); + ASSERT_TRUE(test_service != nullptr); EXPECT_NE(0, call_counter.call_count_); call_counter.Clear(); - EXPECT_TRUE(foo->FindFieldByName("no_such_field") == NULL); - EXPECT_TRUE(foo->FindExtensionByName("no_such_extension") == NULL); - EXPECT_TRUE(foo->FindNestedTypeByName("NoSuchMessageType") == NULL); - EXPECT_TRUE(foo->FindEnumTypeByName("NoSuchEnumType") == NULL); - EXPECT_TRUE(foo->FindEnumValueByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(test_enum->FindValueByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(test_service->FindMethodByName("NoSuchMethod") == NULL); - - EXPECT_TRUE(file->FindMessageTypeByName("NoSuchMessageType") == NULL); - EXPECT_TRUE(file->FindEnumTypeByName("NoSuchEnumType") == NULL); - EXPECT_TRUE(file->FindEnumValueByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(file->FindServiceByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(file->FindExtensionByName("no_such_extension") == NULL); - - EXPECT_TRUE(pool.FindFileContainingSymbol("Foo.no.such.field") == NULL); - EXPECT_TRUE(pool.FindFileContainingSymbol("Foo.no_such_field") == NULL); - EXPECT_TRUE(pool.FindMessageTypeByName("Foo.NoSuchMessageType") == NULL); - EXPECT_TRUE(pool.FindFieldByName("Foo.no_such_field") == NULL); - EXPECT_TRUE(pool.FindExtensionByName("Foo.no_such_extension") == NULL); - EXPECT_TRUE(pool.FindEnumTypeByName("Foo.NoSuchEnumType") == NULL); - EXPECT_TRUE(pool.FindEnumValueByName("Foo.NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(pool.FindMethodByName("TestService.NoSuchMethod") == NULL); + EXPECT_TRUE(foo->FindFieldByName("no_such_field") == nullptr); + EXPECT_TRUE(foo->FindExtensionByName("no_such_extension") == nullptr); + EXPECT_TRUE(foo->FindNestedTypeByName("NoSuchMessageType") == nullptr); + EXPECT_TRUE(foo->FindEnumTypeByName("NoSuchEnumType") == nullptr); + EXPECT_TRUE(foo->FindEnumValueByName("NO_SUCH_VALUE") == nullptr); + EXPECT_TRUE(test_enum->FindValueByName("NO_SUCH_VALUE") == nullptr); + EXPECT_TRUE(test_service->FindMethodByName("NoSuchMethod") == nullptr); + + EXPECT_TRUE(file->FindMessageTypeByName("NoSuchMessageType") == nullptr); + EXPECT_TRUE(file->FindEnumTypeByName("NoSuchEnumType") == nullptr); + EXPECT_TRUE(file->FindEnumValueByName("NO_SUCH_VALUE") == nullptr); + EXPECT_TRUE(file->FindServiceByName("NO_SUCH_VALUE") == nullptr); + EXPECT_TRUE(file->FindExtensionByName("no_such_extension") == nullptr); + + EXPECT_TRUE(pool.FindFileContainingSymbol("Foo.no.such.field") == nullptr); + EXPECT_TRUE(pool.FindFileContainingSymbol("Foo.no_such_field") == nullptr); + EXPECT_TRUE(pool.FindMessageTypeByName("Foo.NoSuchMessageType") == nullptr); + EXPECT_TRUE(pool.FindFieldByName("Foo.no_such_field") == nullptr); + EXPECT_TRUE(pool.FindExtensionByName("Foo.no_such_extension") == nullptr); + EXPECT_TRUE(pool.FindEnumTypeByName("Foo.NoSuchEnumType") == nullptr); + EXPECT_TRUE(pool.FindEnumValueByName("Foo.NO_SUCH_VALUE") == nullptr); + EXPECT_TRUE(pool.FindMethodByName("TestService.NoSuchMethod") == nullptr); EXPECT_EQ(0, call_counter.call_count_); } @@ -6748,11 +6881,11 @@ TEST_F(DatabaseBackedPoolTest, DoesntReloadFilesUncesessarily) { // First make sure foo.proto is loaded. const Descriptor* foo = pool.FindMessageTypeByName("Foo"); - ASSERT_TRUE(foo != NULL); + ASSERT_TRUE(foo != nullptr); // Try inducing false positives. - EXPECT_TRUE(pool.FindMessageTypeByName("NoSuchSymbol") == NULL); - EXPECT_TRUE(pool.FindExtensionByNumber(foo, 22) == NULL); + EXPECT_TRUE(pool.FindMessageTypeByName("NoSuchSymbol") == nullptr); + EXPECT_TRUE(pool.FindExtensionByNumber(foo, 22) == nullptr); // No errors should have been reported. (If foo.proto was incorrectly // loaded multiple times, errors would have been reported.) @@ -6774,7 +6907,7 @@ class ExponentialErrorDatabase : public DescriptorDatabase { ~ExponentialErrorDatabase() {} // implements DescriptorDatabase --------------------------------- - bool FindFileByName(const string& filename, + bool FindFileByName(const std::string& filename, FileDescriptorProto* output) { int file_num = -1; FullMatch(filename, "file", ".proto", &file_num); @@ -6784,7 +6917,7 @@ class ExponentialErrorDatabase : public DescriptorDatabase { return false; } } - bool FindFileContainingSymbol(const string& symbol_name, + bool FindFileContainingSymbol(const std::string& symbol_name, FileDescriptorProto* output) { int file_num = -1; FullMatch(symbol_name, "Message", "", &file_num); @@ -6794,21 +6927,19 @@ class ExponentialErrorDatabase : public DescriptorDatabase { return false; } } - bool FindFileContainingExtension(const string& containing_type, + bool FindFileContainingExtension(const std::string& containing_type, int field_number, FileDescriptorProto* output) { return false; } private: - void FullMatch(const string& name, - const string& begin_with, - const string& end_with, - int* file_num) { + void FullMatch(const std::string& name, const std::string& begin_with, + const std::string& end_with, int* file_num) { int begin_size = begin_with.size(); int end_size = end_with.size(); if (name.substr(0, begin_size) != begin_with || - name.substr(name.size()- end_size, end_size) != end_with) { + name.substr(name.size() - end_size, end_size) != end_with) { return; } safe_strto32( @@ -6844,8 +6975,8 @@ TEST_F(DatabaseBackedPoolTest, DoesntReloadKnownBadFiles) { GOOGLE_LOG(INFO) << "A timeout in this test probably indicates a real bug."; - EXPECT_TRUE(pool.FindFileByName("file40.proto") == NULL); - EXPECT_TRUE(pool.FindMessageTypeByName("Message40") == NULL); + EXPECT_TRUE(pool.FindFileByName("file40.proto") == nullptr); + EXPECT_TRUE(pool.FindMessageTypeByName("Message40") == nullptr); } TEST_F(DatabaseBackedPoolTest, DoesntFallbackOnWrongType) { @@ -6856,22 +6987,22 @@ TEST_F(DatabaseBackedPoolTest, DoesntFallbackOnWrongType) { DescriptorPool pool(&call_counter); const FileDescriptor* file = pool.FindFileByName("foo.proto"); - ASSERT_TRUE(file != NULL); + ASSERT_TRUE(file != nullptr); const Descriptor* foo = pool.FindMessageTypeByName("Foo"); - ASSERT_TRUE(foo != NULL); + ASSERT_TRUE(foo != nullptr); const EnumDescriptor* test_enum = pool.FindEnumTypeByName("TestEnum"); - ASSERT_TRUE(test_enum != NULL); + ASSERT_TRUE(test_enum != nullptr); EXPECT_NE(0, call_counter.call_count_); call_counter.Clear(); - EXPECT_TRUE(pool.FindMessageTypeByName("TestEnum") == NULL); - EXPECT_TRUE(pool.FindFieldByName("Foo") == NULL); - EXPECT_TRUE(pool.FindExtensionByName("Foo") == NULL); - EXPECT_TRUE(pool.FindEnumTypeByName("Foo") == NULL); - EXPECT_TRUE(pool.FindEnumValueByName("Foo") == NULL); - EXPECT_TRUE(pool.FindServiceByName("Foo") == NULL); - EXPECT_TRUE(pool.FindMethodByName("Foo") == NULL); + EXPECT_TRUE(pool.FindMessageTypeByName("TestEnum") == nullptr); + EXPECT_TRUE(pool.FindFieldByName("Foo") == nullptr); + EXPECT_TRUE(pool.FindExtensionByName("Foo") == nullptr); + EXPECT_TRUE(pool.FindEnumTypeByName("Foo") == nullptr); + EXPECT_TRUE(pool.FindEnumValueByName("Foo") == nullptr); + EXPECT_TRUE(pool.FindServiceByName("Foo") == nullptr); + EXPECT_TRUE(pool.FindMethodByName("Foo") == nullptr); EXPECT_EQ(0, call_counter.call_count_); } @@ -6882,15 +7013,14 @@ class AbortingErrorCollector : public DescriptorPool::ErrorCollector { public: AbortingErrorCollector() {} - virtual void AddError( - const string &filename, - const string &element_name, - const Message *message, - ErrorLocation location, - const string &error_message) { + virtual void AddError(const std::string& filename, + const std::string& element_name, const Message* message, + ErrorLocation location, + const std::string& error_message) { GOOGLE_LOG(FATAL) << "AddError() called unexpectedly: " << filename << " [" << element_name << "]: " << error_message; } + private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AbortingErrorCollector); }; @@ -6898,17 +7028,18 @@ class AbortingErrorCollector : public DescriptorPool::ErrorCollector { // A source tree containing only one file. class SingletonSourceTree : public compiler::SourceTree { public: - SingletonSourceTree(const string& filename, const string& contents) + SingletonSourceTree(const std::string& filename, const std::string& contents) : filename_(filename), contents_(contents) {} - virtual io::ZeroCopyInputStream* Open(const string& filename) { - return filename == filename_ ? - new io::ArrayInputStream(contents_.data(), contents_.size()) : NULL; + virtual io::ZeroCopyInputStream* Open(const std::string& filename) { + return filename == filename_ + ? new io::ArrayInputStream(contents_.data(), contents_.size()) + : nullptr; } private: - const string filename_; - const string contents_; + const std::string filename_; + const std::string contents_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SingletonSourceTree); }; @@ -7004,11 +7135,9 @@ class SourceLocationTest : public testing::Test { simple_db_.Add(file_proto_); } - static string PrintSourceLocation(const SourceLocation &loc) { - return strings::Substitute("$0:$1-$2:$3", - 1 + loc.start_line, - 1 + loc.start_column, - 1 + loc.end_line, + static std::string PrintSourceLocation(const SourceLocation& loc) { + return strings::Substitute("$0:$1-$2:$3", 1 + loc.start_line, + 1 + loc.start_column, 1 + loc.end_line, 1 + loc.end_column); } @@ -7038,27 +7167,27 @@ TEST_F(SourceLocationTest, GetSourceLocation) { const FileDescriptor* file_desc = GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto")); - const Descriptor *a_desc = file_desc->FindMessageTypeByName("A"); + const Descriptor* a_desc = file_desc->FindMessageTypeByName("A"); EXPECT_TRUE(a_desc->GetSourceLocation(&loc)); EXPECT_EQ("4:1-16:2", PrintSourceLocation(loc)); - const Descriptor *a_b_desc = a_desc->FindNestedTypeByName("B"); + const Descriptor* a_b_desc = a_desc->FindNestedTypeByName("B"); EXPECT_TRUE(a_b_desc->GetSourceLocation(&loc)); EXPECT_EQ("7:3-9:4", PrintSourceLocation(loc)); - const EnumDescriptor *e_desc = file_desc->FindEnumTypeByName("Indecision"); + const EnumDescriptor* e_desc = file_desc->FindEnumTypeByName("Indecision"); EXPECT_TRUE(e_desc->GetSourceLocation(&loc)); EXPECT_EQ("17:1-24:2", PrintSourceLocation(loc)); - const EnumValueDescriptor *yes_desc = e_desc->FindValueByName("YES"); + const EnumValueDescriptor* yes_desc = e_desc->FindValueByName("YES"); EXPECT_TRUE(yes_desc->GetSourceLocation(&loc)); EXPECT_EQ("21:3-21:42", PrintSourceLocation(loc)); - const ServiceDescriptor *s_desc = file_desc->FindServiceByName("S"); + const ServiceDescriptor* s_desc = file_desc->FindServiceByName("S"); EXPECT_TRUE(s_desc->GetSourceLocation(&loc)); EXPECT_EQ("25:1-35:2", PrintSourceLocation(loc)); - const MethodDescriptor *m_desc = s_desc->FindMethodByName("Method"); + const MethodDescriptor* m_desc = s_desc->FindMethodByName("Method"); EXPECT_TRUE(m_desc->GetSourceLocation(&loc)); EXPECT_EQ("29:3-29:31", PrintSourceLocation(loc)); @@ -7070,16 +7199,16 @@ TEST_F(SourceLocationTest, ExtensionSourceLocation) { const FileDescriptor* file_desc = GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto")); - const FieldDescriptor *int32_extension_desc = + const FieldDescriptor* int32_extension_desc = file_desc->FindExtensionByName("int32_extension"); EXPECT_TRUE(int32_extension_desc->GetSourceLocation(&loc)); EXPECT_EQ("40:3-40:55", PrintSourceLocation(loc)); - const Descriptor *c_desc = file_desc->FindMessageTypeByName("C"); + const Descriptor* c_desc = file_desc->FindMessageTypeByName("C"); EXPECT_TRUE(c_desc->GetSourceLocation(&loc)); EXPECT_EQ("42:1-46:2", PrintSourceLocation(loc)); - const FieldDescriptor *message_extension_desc = + const FieldDescriptor* message_extension_desc = c_desc->FindExtensionByName("message_extension"); EXPECT_TRUE(message_extension_desc->GetSourceLocation(&loc)); EXPECT_EQ("44:5-44:41", PrintSourceLocation(loc)); @@ -7467,7 +7596,7 @@ TEST_F(SourceLocationTest, GetSourceLocation_MissingSourceCodeInfo) { DescriptorPool bad1_pool(&pool_); const FileDescriptor* bad1_file_desc = GOOGLE_CHECK_NOTNULL(bad1_pool.BuildFile(proto)); - const Descriptor *bad1_a_desc = bad1_file_desc->FindMessageTypeByName("A"); + const Descriptor* bad1_a_desc = bad1_file_desc->FindMessageTypeByName("A"); EXPECT_FALSE(bad1_a_desc->GetSourceLocation(&loc)); } @@ -7481,7 +7610,7 @@ TEST_F(SourceLocationTest, GetSourceLocation_BogusSourceCodeInfo) { FileDescriptorProto proto; file_desc->CopyTo(&proto); // Note, this discards the SourceCodeInfo. EXPECT_FALSE(proto.has_source_code_info()); - SourceCodeInfo_Location *loc_msg = + SourceCodeInfo_Location* loc_msg = proto.mutable_source_code_info()->add_location(); loc_msg->add_path(1); loc_msg->add_path(2); @@ -7493,15 +7622,15 @@ TEST_F(SourceLocationTest, GetSourceLocation_BogusSourceCodeInfo) { DescriptorPool bad2_pool(&pool_); const FileDescriptor* bad2_file_desc = GOOGLE_CHECK_NOTNULL(bad2_pool.BuildFile(proto)); - const Descriptor *bad2_a_desc = bad2_file_desc->FindMessageTypeByName("A"); + const Descriptor* bad2_a_desc = bad2_file_desc->FindMessageTypeByName("A"); EXPECT_FALSE(bad2_a_desc->GetSourceLocation(&loc)); } // =================================================================== const char* const kCopySourceCodeInfoToTestInput = - "syntax = \"proto2\";\n" - "message Foo {}\n"; + "syntax = \"proto2\";\n" + "message Foo {}\n"; // Required since source code information is not preserved by // FileDescriptorTest. @@ -7555,7 +7684,7 @@ TEST_F(CopySourceCodeInfoToTest, CopySourceCodeInfoTo) { class LazilyBuildDependenciesTest : public testing::Test { public: - LazilyBuildDependenciesTest() : pool_(&db_, NULL) { + LazilyBuildDependenciesTest() : pool_(&db_, nullptr) { pool_.InternalSetLazilyBuildDependencies(); } @@ -7565,7 +7694,7 @@ class LazilyBuildDependenciesTest : public testing::Test { db_.Add(tmp); } - void ParseProtoAndAddToDb(const string& proto) { + void ParseProtoAndAddToDb(const std::string& proto) { FileDescriptorProto tmp; ASSERT_TRUE(TextFormat::ParseFromString(proto, &tmp)); db_.Add(tmp); @@ -7573,12 +7702,12 @@ class LazilyBuildDependenciesTest : public testing::Test { void AddSimpleMessageProtoFileToDb(const char* file_name, const char* message_name) { - ParseProtoAndAddToDb("name: '" + string(file_name) + + ParseProtoAndAddToDb("name: '" + std::string(file_name) + ".proto' " "package: \"protobuf_unittest\" " "message_type { " " name:'" + - string(message_name) + + std::string(message_name) + "' " " field { name:'a' number:1 " " label:LABEL_OPTIONAL " @@ -7588,15 +7717,15 @@ class LazilyBuildDependenciesTest : public testing::Test { void AddSimpleEnumProtoFileToDb(const char* file_name, const char* enum_name, const char* enum_value_name) { - ParseProtoAndAddToDb("name: '" + string(file_name) + + ParseProtoAndAddToDb("name: '" + std::string(file_name) + ".proto' " "package: 'protobuf_unittest' " "enum_type { " " name:'" + - string(enum_name) + + std::string(enum_name) + "' " " value { name:'" + - string(enum_value_name) + + std::string(enum_value_name) + "' number:1 } " "}"); } @@ -7625,7 +7754,7 @@ TEST_F(LazilyBuildDependenciesTest, Message) { const FileDescriptor* file = pool_.FindFileByName("foo.proto"); // Verify only foo gets built when asking for foo.proto - EXPECT_TRUE(file != NULL); + EXPECT_TRUE(file != nullptr); EXPECT_TRUE(pool_.InternalIsFileLoaded("foo.proto")); EXPECT_FALSE(pool_.InternalIsFileLoaded("bar.proto")); @@ -7634,7 +7763,7 @@ TEST_F(LazilyBuildDependenciesTest, Message) { // the field's type is defined in, as well. const Descriptor* desc = file->FindMessageTypeByName("Foo"); const FieldDescriptor* field = desc->FindFieldByName("bar"); - EXPECT_TRUE(field != NULL); + EXPECT_TRUE(field != nullptr); EXPECT_EQ(field, desc->FindFieldByNumber(1)); EXPECT_EQ(field, desc->FindFieldByLowercaseName("bar")); EXPECT_EQ(field, desc->FindFieldByCamelcaseName("bar")); @@ -7642,7 +7771,7 @@ TEST_F(LazilyBuildDependenciesTest, Message) { // Finally, verify that if we call message_type() on the field, we will // buid the file where the message is defined, and get a valid descriptor - EXPECT_TRUE(field->message_type() != NULL); + EXPECT_TRUE(field->message_type() != nullptr); EXPECT_TRUE(pool_.InternalIsFileLoaded("bar.proto")); } @@ -7668,18 +7797,18 @@ TEST_F(LazilyBuildDependenciesTest, Enum) { // yet built will build the file and return a descriptor. EXPECT_FALSE(pool_.InternalIsFileLoaded("enum1.proto")); const Descriptor* desc = file->FindMessageTypeByName("Lazy"); - EXPECT_TRUE(desc != NULL); + EXPECT_TRUE(desc != nullptr); const FieldDescriptor* field = desc->FindFieldByName("enum1"); - EXPECT_TRUE(field != NULL); - EXPECT_TRUE(field->enum_type() != NULL); + EXPECT_TRUE(field != nullptr); + EXPECT_TRUE(field->enum_type() != nullptr); EXPECT_TRUE(pool_.InternalIsFileLoaded("enum1.proto")); // Verify calling default_value_enum() on a field whose definition is not // yet built will build the file and return a descriptor to the value. EXPECT_FALSE(pool_.InternalIsFileLoaded("enum2.proto")); field = desc->FindFieldByName("enum2"); - EXPECT_TRUE(field != NULL); - EXPECT_TRUE(field->default_value_enum() != NULL); + EXPECT_TRUE(field != nullptr); + EXPECT_TRUE(field->default_value_enum() != nullptr); EXPECT_TRUE(pool_.InternalIsFileLoaded("enum2.proto")); } @@ -7713,9 +7842,9 @@ TEST_F(LazilyBuildDependenciesTest, Type) { // build the type defined in another file. EXPECT_FALSE(pool_.InternalIsFileLoaded("message1.proto")); const Descriptor* desc = file->FindMessageTypeByName("Lazy"); - EXPECT_TRUE(desc != NULL); + EXPECT_TRUE(desc != nullptr); const FieldDescriptor* field = desc->FindFieldByName("message1"); - EXPECT_TRUE(field != NULL); + EXPECT_TRUE(field != nullptr); EXPECT_EQ(field->type(), FieldDescriptor::TYPE_MESSAGE); EXPECT_TRUE(pool_.InternalIsFileLoaded("message1.proto")); @@ -7723,7 +7852,7 @@ TEST_F(LazilyBuildDependenciesTest, Type) { // build the type defined in another file. EXPECT_FALSE(pool_.InternalIsFileLoaded("message2.proto")); field = desc->FindFieldByName("message2"); - EXPECT_TRUE(field != NULL); + EXPECT_TRUE(field != nullptr); EXPECT_EQ(field->cpp_type(), FieldDescriptor::CPPTYPE_MESSAGE); EXPECT_TRUE(pool_.InternalIsFileLoaded("message2.proto")); @@ -7731,7 +7860,7 @@ TEST_F(LazilyBuildDependenciesTest, Type) { // build the type defined in another file. EXPECT_FALSE(pool_.InternalIsFileLoaded("enum1.proto")); field = desc->FindFieldByName("enum1"); - EXPECT_TRUE(field != NULL); + EXPECT_TRUE(field != nullptr); EXPECT_EQ(field->type(), FieldDescriptor::TYPE_ENUM); EXPECT_TRUE(pool_.InternalIsFileLoaded("enum1.proto")); @@ -7739,7 +7868,7 @@ TEST_F(LazilyBuildDependenciesTest, Type) { // build the type defined in another file. EXPECT_FALSE(pool_.InternalIsFileLoaded("enum2.proto")); field = desc->FindFieldByName("enum2"); - EXPECT_TRUE(field != NULL); + EXPECT_TRUE(field != nullptr); EXPECT_EQ(field->cpp_type(), FieldDescriptor::CPPTYPE_ENUM); EXPECT_TRUE(pool_.InternalIsFileLoaded("enum2.proto")); } @@ -7770,7 +7899,7 @@ TEST_F(LazilyBuildDependenciesTest, Extension) { // Verify foo.bar gets loaded, and bar.proto gets loaded // to register the extension. baz.proto should not get loaded. - EXPECT_TRUE(file != NULL); + EXPECT_TRUE(file != nullptr); EXPECT_TRUE(pool_.InternalIsFileLoaded("foo.proto")); EXPECT_TRUE(pool_.InternalIsFileLoaded("bar.proto")); EXPECT_FALSE(pool_.InternalIsFileLoaded("baz.proto")); @@ -7800,15 +7929,15 @@ TEST_F(LazilyBuildDependenciesTest, Service) { // files defining the input and output type, and input_type() and // output_type() does indeed build the appropriate files. const ServiceDescriptor* service = file->FindServiceByName("LazyService"); - EXPECT_TRUE(service != NULL); + EXPECT_TRUE(service != nullptr); const MethodDescriptor* method = service->FindMethodByName("A"); EXPECT_FALSE(pool_.InternalIsFileLoaded("message1.proto")); EXPECT_FALSE(pool_.InternalIsFileLoaded("message2.proto")); - EXPECT_TRUE(method != NULL); - EXPECT_TRUE(method->input_type() != NULL); + EXPECT_TRUE(method != nullptr); + EXPECT_TRUE(method->input_type() != nullptr); EXPECT_TRUE(pool_.InternalIsFileLoaded("message1.proto")); EXPECT_FALSE(pool_.InternalIsFileLoaded("message2.proto")); - EXPECT_TRUE(method->output_type() != NULL); + EXPECT_TRUE(method->output_type() != nullptr); EXPECT_TRUE(pool_.InternalIsFileLoaded("message2.proto")); } @@ -7833,7 +7962,7 @@ TEST_F(LazilyBuildDependenciesTest, GeneratedFile) { // the generated function "descriptor()" doesn't somehow subvert the laziness // by manually loading the dependencies or something. EXPECT_TRUE(protobuf_unittest::lazy_imports::ImportedMessage::descriptor() != - NULL); + nullptr); EXPECT_TRUE(DescriptorPool::generated_pool()->InternalIsFileLoaded( "google/protobuf/unittest_lazy_dependencies.proto")); EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded( @@ -7891,7 +8020,7 @@ TEST_F(LazilyBuildDependenciesTest, Dependency) { AddSimpleMessageProtoFileToDb("baz", "Baz"); const FileDescriptor* foo_file = pool_.FindFileByName("foo.proto"); - EXPECT_TRUE(foo_file != NULL); + EXPECT_TRUE(foo_file != nullptr); // As expected, requesting foo.proto shouldn't build it's dependencies EXPECT_TRUE(pool_.InternalIsFileLoaded("foo.proto")); EXPECT_FALSE(pool_.InternalIsFileLoaded("bar.proto")); @@ -7900,7 +8029,7 @@ TEST_F(LazilyBuildDependenciesTest, Dependency) { // Verify calling dependency(N) will build the dependency, but // not that file's dependencies. const FileDescriptor* bar_file = foo_file->dependency(0); - EXPECT_TRUE(bar_file != NULL); + EXPECT_TRUE(bar_file != nullptr); EXPECT_TRUE(pool_.InternalIsFileLoaded("bar.proto")); EXPECT_FALSE(pool_.InternalIsFileLoaded("baz.proto")); } diff --git a/src/google/protobuf/duration.pb.cc b/src/google/protobuf/duration.pb.cc index e9a82770c0..6f0cec796f 100644 --- a/src/google/protobuf/duration.pb.cc +++ b/src/google/protobuf/duration.pb.cc @@ -5,126 +5,105 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_OPEN class DurationDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Duration_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsDuration_google_2fprotobuf_2fduration_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_Duration_google_2fprotobuf_2fduration_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_Duration_default_instance_; - new (ptr) ::google::protobuf::Duration(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Duration_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Duration(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::Duration::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Duration::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_Duration_google_2fprotobuf_2fduration_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsDuration_google_2fprotobuf_2fduration_2eproto}, {}}; - -void InitDefaults_google_2fprotobuf_2fduration_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_Duration_google_2fprotobuf_2fduration_2eproto.base); -} +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Duration_google_2fprotobuf_2fduration_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_Duration_google_2fprotobuf_2fduration_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2fduration_2eproto[1]; -constexpr ::google::protobuf::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fduration_2eproto = nullptr; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fduration_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fduration_2eproto[1]; +static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fduration_2eproto = nullptr; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fduration_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fduration_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2fduration_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Duration, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Duration, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Duration, seconds_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Duration, nanos_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Duration, seconds_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Duration, nanos_), }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, -1, sizeof(::google::protobuf::Duration)}, +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, -1, sizeof(PROTOBUF_NAMESPACE_ID::Duration)}, }; -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_Duration_default_instance_), -}; - -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2fduration_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2fduration_2eproto, "google/protobuf/duration.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2fduration_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2fduration_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2fduration_2eproto, file_level_service_descriptors_google_2fprotobuf_2fduration_2eproto, +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Duration_default_instance_), }; -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fduration_2eproto = { - false, InitDefaults_google_2fprotobuf_2fduration_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2fduration_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n\036google/protobuf/duration.proto\022\017google" ".protobuf\"*\n\010Duration\022\017\n\007seconds\030\001 \001(\003\022\r" "\n\005nanos\030\002 \001(\005B|\n\023com.google.protobufB\rDu" "rationProtoP\001Z*github.com/golang/protobu" "f/ptypes/duration\370\001\001\242\002\003GPB\252\002\036Google.Prot" "obuf.WellKnownTypesb\006proto3" -, - "google/protobuf/duration.proto", &assign_descriptors_table_google_2fprotobuf_2fduration_2eproto, 227, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2fduration_2eproto_deps[1] = { +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2fduration_2eproto_sccs[1] = { + &scc_info_Duration_google_2fprotobuf_2fduration_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fduration_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2fduration_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fduration_2eproto = { + &descriptor_table_google_2fprotobuf_2fduration_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2fduration_2eproto, "google/protobuf/duration.proto", 227, + &descriptor_table_google_2fprotobuf_2fduration_2eproto_once, descriptor_table_google_2fprotobuf_2fduration_2eproto_sccs, descriptor_table_google_2fprotobuf_2fduration_2eproto_deps, 1, 0, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2fduration_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2fduration_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2fduration_2eproto, file_level_service_descriptors_google_2fprotobuf_2fduration_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2fduration_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fduration_2eproto, deps, 0); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2fduration_2eproto = []() { AddDescriptors_google_2fprotobuf_2fduration_2eproto(); return true; }(); -namespace google { -namespace protobuf { +static bool dynamic_init_dummy_google_2fprotobuf_2fduration_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fduration_2eproto), true); +PROTOBUF_NAMESPACE_OPEN // =================================================================== void Duration::InitAsDefaultInstance() { } -class Duration::HasBitSetters { +class Duration::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Duration::kSecondsFieldNumber; -const int Duration::kNanosFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Duration::Duration() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Duration) } -Duration::Duration(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +Duration::Duration(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.Duration) } Duration::Duration(const Duration& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr) { _internal_metadata_.MergeFrom(from._internal_metadata_); ::memcpy(&seconds_, &from.seconds_, static_cast(reinterpret_cast(&nanos_) - @@ -144,27 +123,27 @@ Duration::~Duration() { } void Duration::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void Duration::ArenaDtor(void* object) { Duration* _this = reinterpret_cast< Duration* >(object); (void)_this; } -void Duration::RegisterArenaDtor(::google::protobuf::Arena*) { +void Duration::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void Duration::SetCachedSize(int size) const { _cached_size_.Set(size); } const Duration& Duration::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_Duration_google_2fprotobuf_2fduration_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_Duration_google_2fprotobuf_2fduration_2eproto.base); return *internal_default_instance(); } void Duration::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Duration) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -175,80 +154,64 @@ void Duration::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Duration::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Duration::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // int64 seconds = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int64 value = val; - msg->set_seconds(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + seconds_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // int32 nanos = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_nanos(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + nanos_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Duration::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Duration) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // int64 seconds = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int64, ::google::protobuf::internal::WireFormatLite::TYPE_INT64>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int64, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT64>( input, &seconds_))); } else { goto handle_unusual; @@ -258,10 +221,10 @@ bool Duration::MergePartialFromCodedStream( // int32 nanos = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &nanos_))); } else { goto handle_unusual; @@ -274,7 +237,7 @@ bool Duration::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -290,49 +253,27 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Duration::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Duration) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // int64 seconds = 1; - if (this->seconds() != 0) { - ::google::protobuf::internal::WireFormatLite::WriteInt64(1, this->seconds(), output); - } - - // int32 nanos = 2; - if (this->nanos() != 0) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->nanos(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Duration) -} - -::google::protobuf::uint8* Duration::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Duration::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Duration) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // int64 seconds = 1; if (this->seconds() != 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt64ToArray(1, this->seconds(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt64ToArray(1, this->seconds(), target); } // int32 nanos = 2; if (this->nanos() != 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->nanos(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(2, this->nanos(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Duration) return target; @@ -342,43 +283,42 @@ size_t Duration::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Duration) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // int64 seconds = 1; if (this->seconds() != 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int64Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int64Size( this->seconds()); } // int32 nanos = 2; if (this->nanos() != 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->nanos()); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Duration::MergeFrom(const ::google::protobuf::Message& from) { +void Duration::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Duration) GOOGLE_DCHECK_NE(&from, this); const Duration* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Duration) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Duration) MergeFrom(*source); @@ -389,7 +329,7 @@ void Duration::MergeFrom(const Duration& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Duration) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; if (from.seconds() != 0) { @@ -400,7 +340,7 @@ void Duration::MergeFrom(const Duration& from) { } } -void Duration::CopyFrom(const ::google::protobuf::Message& from) { +void Duration::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Duration) if (&from == this) return; Clear(); @@ -418,25 +358,6 @@ bool Duration::IsInitialized() const { return true; } -void Duration::Swap(Duration* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - Duration* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void Duration::UnsafeArenaSwap(Duration* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void Duration::InternalSwap(Duration* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -444,21 +365,18 @@ void Duration::InternalSwap(Duration* other) { swap(nanos_, other->nanos_); } -::google::protobuf::Metadata Duration::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fduration_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fduration_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Duration::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Duration* Arena::CreateMaybeMessage< ::google::protobuf::Duration >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::Duration >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Duration* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Duration >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::Duration >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/duration.pb.h b/src/google/protobuf/duration.pb.h index 1d32e1bbbf..dd0c6a8749 100644 --- a/src/google/protobuf/duration.pb.h +++ b/src/google/protobuf/duration.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/duration.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2fduration_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2fduration_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fduration_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fduration_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,57 +27,61 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fduration_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fduration_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fduration_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[1] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fduration_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] + 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2fduration_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fduration_2eproto; +PROTOBUF_NAMESPACE_OPEN class Duration; class DurationDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern DurationDefaultTypeInternal _Duration_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Duration* Arena::CreateMaybeMessage<::google::protobuf::Duration>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { +PROTOBUF_EXPORT extern DurationDefaultTypeInternal _Duration_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Duration* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN // =================================================================== -class LIBPROTOBUF_EXPORT Duration : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Duration) */ { +class PROTOBUF_EXPORT Duration : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Duration) */ { public: Duration(); virtual ~Duration(); Duration(const Duration& from); - - inline Duration& operator=(const Duration& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Duration(Duration&& from) noexcept : Duration() { *this = ::std::move(from); } + inline Duration& operator=(const Duration& from) { + CopyFrom(from); + return *this; + } inline Duration& operator=(Duration&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -85,15 +90,21 @@ class LIBPROTOBUF_EXPORT Duration : public ::google::protobuf::Message /* @@prot } return *this; } - #endif - inline ::google::protobuf::Arena* GetArena() const final { + + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Duration& default_instance(); @@ -105,54 +116,66 @@ class LIBPROTOBUF_EXPORT Duration : public ::google::protobuf::Message /* @@prot static constexpr int kIndexInFileMessages = 0; - void UnsafeArenaSwap(Duration* other); - void Swap(Duration* other); friend void swap(Duration& a, Duration& b) { a.Swap(&b); } + inline void Swap(Duration* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(Duration* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Duration* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Duration* New(::google::protobuf::Arena* arena) const final { + Duration* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Duration& from); void MergeFrom(const Duration& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Duration* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Duration"; + } protected: - explicit Duration(::google::protobuf::Arena* arena); + explicit Duration(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -160,35 +183,44 @@ class LIBPROTOBUF_EXPORT Duration : public ::google::protobuf::Message /* @@prot } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fduration_2eproto); + return ::descriptor_table_google_2fprotobuf_2fduration_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kSecondsFieldNumber = 1, + kNanosFieldNumber = 2, + }; // int64 seconds = 1; void clear_seconds(); - static const int kSecondsFieldNumber = 1; - ::google::protobuf::int64 seconds() const; - void set_seconds(::google::protobuf::int64 value); + ::PROTOBUF_NAMESPACE_ID::int64 seconds() const; + void set_seconds(::PROTOBUF_NAMESPACE_ID::int64 value); // int32 nanos = 2; void clear_nanos(); - static const int kNanosFieldNumber = 2; - ::google::protobuf::int32 nanos() const; - void set_nanos(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 nanos() const; + void set_nanos(::PROTOBUF_NAMESPACE_ID::int32 value); // @@protoc_insertion_point(class_scope:google.protobuf.Duration) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::int64 seconds_; - ::google::protobuf::int32 nanos_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::int64 seconds_; + ::PROTOBUF_NAMESPACE_ID::int32 nanos_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2fduration_2eproto; }; // =================================================================== @@ -204,13 +236,13 @@ class LIBPROTOBUF_EXPORT Duration : public ::google::protobuf::Message /* @@prot // int64 seconds = 1; inline void Duration::clear_seconds() { - seconds_ = GOOGLE_PROTOBUF_LONGLONG(0); + seconds_ = PROTOBUF_LONGLONG(0); } -inline ::google::protobuf::int64 Duration::seconds() const { +inline ::PROTOBUF_NAMESPACE_ID::int64 Duration::seconds() const { // @@protoc_insertion_point(field_get:google.protobuf.Duration.seconds) return seconds_; } -inline void Duration::set_seconds(::google::protobuf::int64 value) { +inline void Duration::set_seconds(::PROTOBUF_NAMESPACE_ID::int64 value) { seconds_ = value; // @@protoc_insertion_point(field_set:google.protobuf.Duration.seconds) @@ -220,11 +252,11 @@ inline void Duration::set_seconds(::google::protobuf::int64 value) { inline void Duration::clear_nanos() { nanos_ = 0; } -inline ::google::protobuf::int32 Duration::nanos() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 Duration::nanos() const { // @@protoc_insertion_point(field_get:google.protobuf.Duration.nanos) return nanos_; } -inline void Duration::set_nanos(::google::protobuf::int32 value) { +inline void Duration::set_nanos(::PROTOBUF_NAMESPACE_ID::int32 value) { nanos_ = value; // @@protoc_insertion_point(field_set:google.protobuf.Duration.nanos) @@ -236,10 +268,9 @@ inline void Duration::set_nanos(::google::protobuf::int32 value) { // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2fduration_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fduration_2eproto diff --git a/src/google/protobuf/duration.proto b/src/google/protobuf/duration.proto index 975fce41aa..9d8a52de29 100644 --- a/src/google/protobuf/duration.proto +++ b/src/google/protobuf/duration.proto @@ -101,7 +101,6 @@ option objc_class_prefix = "GPB"; // // message Duration { - // Signed seconds of the span of time. Must be from -315,576,000,000 // to +315,576,000,000 inclusive. Note: these bounds are computed from: // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years diff --git a/src/google/protobuf/dynamic_message.cc b/src/google/protobuf/dynamic_message.cc index 6e4338f413..0ef5dd9a6d 100644 --- a/src/google/protobuf/dynamic_message.cc +++ b/src/google/protobuf/dynamic_message.cc @@ -34,10 +34,10 @@ // // DynamicMessage is implemented by constructing a data structure which // has roughly the same memory layout as a generated message would have. -// Then, we use GeneratedMessageReflection to implement our reflection -// interface. All the other operations we need to implement (e.g. -// parsing, copying, etc.) are already implemented in terms of -// Reflection, so the rest is easy. +// Then, we use Reflection to implement our reflection interface. All +// the other operations we need to implement (e.g. parsing, copying, +// etc.) are already implemented in terms of Reflection, so the rest is +// easy. // // The up side of this strategy is that it's very efficient. We don't // need to use hash_maps or generic representations of fields. The @@ -82,13 +82,11 @@ #include #include - namespace google { namespace protobuf { using internal::DynamicMapField; using internal::ExtensionSet; -using internal::GeneratedMessageReflection; using internal::InternalMetadataWithArena; using internal::MapField; @@ -100,23 +98,29 @@ using internal::ArenaStringPtr; namespace { -bool IsMapFieldInApi(const FieldDescriptor* field) { - return field->is_map(); -} +bool IsMapFieldInApi(const FieldDescriptor* field) { return field->is_map(); } // Compute the byte size of the in-memory representation of the field. int FieldSpaceUsed(const FieldDescriptor* field) { typedef FieldDescriptor FD; // avoid line wrapping if (field->label() == FD::LABEL_REPEATED) { switch (field->cpp_type()) { - case FD::CPPTYPE_INT32 : return sizeof(RepeatedField); - case FD::CPPTYPE_INT64 : return sizeof(RepeatedField); - case FD::CPPTYPE_UINT32 : return sizeof(RepeatedField); - case FD::CPPTYPE_UINT64 : return sizeof(RepeatedField); - case FD::CPPTYPE_DOUBLE : return sizeof(RepeatedField); - case FD::CPPTYPE_FLOAT : return sizeof(RepeatedField); - case FD::CPPTYPE_BOOL : return sizeof(RepeatedField); - case FD::CPPTYPE_ENUM : return sizeof(RepeatedField); + case FD::CPPTYPE_INT32: + return sizeof(RepeatedField); + case FD::CPPTYPE_INT64: + return sizeof(RepeatedField); + case FD::CPPTYPE_UINT32: + return sizeof(RepeatedField); + case FD::CPPTYPE_UINT64: + return sizeof(RepeatedField); + case FD::CPPTYPE_DOUBLE: + return sizeof(RepeatedField); + case FD::CPPTYPE_FLOAT: + return sizeof(RepeatedField); + case FD::CPPTYPE_BOOL: + return sizeof(RepeatedField); + case FD::CPPTYPE_ENUM: + return sizeof(RepeatedField); case FD::CPPTYPE_MESSAGE: if (IsMapFieldInApi(field)) { return sizeof(DynamicMapField); @@ -128,20 +132,28 @@ int FieldSpaceUsed(const FieldDescriptor* field) { switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - return sizeof(RepeatedPtrField); + return sizeof(RepeatedPtrField); } break; } } else { switch (field->cpp_type()) { - case FD::CPPTYPE_INT32 : return sizeof(int32 ); - case FD::CPPTYPE_INT64 : return sizeof(int64 ); - case FD::CPPTYPE_UINT32 : return sizeof(uint32 ); - case FD::CPPTYPE_UINT64 : return sizeof(uint64 ); - case FD::CPPTYPE_DOUBLE : return sizeof(double ); - case FD::CPPTYPE_FLOAT : return sizeof(float ); - case FD::CPPTYPE_BOOL : return sizeof(bool ); - case FD::CPPTYPE_ENUM : return sizeof(int ); + case FD::CPPTYPE_INT32: + return sizeof(int32); + case FD::CPPTYPE_INT64: + return sizeof(int64); + case FD::CPPTYPE_UINT32: + return sizeof(uint32); + case FD::CPPTYPE_UINT64: + return sizeof(uint64); + case FD::CPPTYPE_DOUBLE: + return sizeof(double); + case FD::CPPTYPE_FLOAT: + return sizeof(float); + case FD::CPPTYPE_BOOL: + return sizeof(bool); + case FD::CPPTYPE_ENUM: + return sizeof(int); case FD::CPPTYPE_MESSAGE: return sizeof(Message*); @@ -165,14 +177,22 @@ int FieldSpaceUsed(const FieldDescriptor* field) { int OneofFieldSpaceUsed(const FieldDescriptor* field) { typedef FieldDescriptor FD; // avoid line wrapping switch (field->cpp_type()) { - case FD::CPPTYPE_INT32 : return sizeof(int32 ); - case FD::CPPTYPE_INT64 : return sizeof(int64 ); - case FD::CPPTYPE_UINT32 : return sizeof(uint32 ); - case FD::CPPTYPE_UINT64 : return sizeof(uint64 ); - case FD::CPPTYPE_DOUBLE : return sizeof(double ); - case FD::CPPTYPE_FLOAT : return sizeof(float ); - case FD::CPPTYPE_BOOL : return sizeof(bool ); - case FD::CPPTYPE_ENUM : return sizeof(int ); + case FD::CPPTYPE_INT32: + return sizeof(int32); + case FD::CPPTYPE_INT64: + return sizeof(int64); + case FD::CPPTYPE_UINT32: + return sizeof(uint32); + case FD::CPPTYPE_UINT64: + return sizeof(uint64); + case FD::CPPTYPE_DOUBLE: + return sizeof(double); + case FD::CPPTYPE_FLOAT: + return sizeof(float); + case FD::CPPTYPE_BOOL: + return sizeof(bool); + case FD::CPPTYPE_ENUM: + return sizeof(int); case FD::CPPTYPE_MESSAGE: return sizeof(Message*); @@ -190,9 +210,7 @@ int OneofFieldSpaceUsed(const FieldDescriptor* field) { return 0; } -inline int DivideRoundingUp(int i, int j) { - return (i + (j - 1)) / j; -} +inline int DivideRoundingUp(int i, int j) { return (i + (j - 1)) / j; } static const int kSafeAlignment = sizeof(uint64); static const int kMaxOneofUnionSize = sizeof(uint64); @@ -203,9 +221,7 @@ inline int AlignTo(int offset, int alignment) { // Rounds the given byte offset up to the next offset aligned such that any // type may be stored at it. -inline int AlignOffset(int offset) { - return AlignTo(offset, kSafeAlignment); -} +inline int AlignOffset(int offset) { return AlignTo(offset, kSafeAlignment); } #define bitsizeof(T) (sizeof(T) * 8) @@ -231,7 +247,7 @@ class DynamicMessage : public Message { // important (the prototype must be deleted *before* the offsets). std::unique_ptr offsets; std::unique_ptr has_bits_indices; - std::unique_ptr reflection; + std::unique_ptr reflection; // Don't use a unique_ptr to hold the prototype: the destructor for // DynamicMessage needs to know whether it is the prototype, and does so by // looking back at this field. This would assume details about the @@ -241,15 +257,13 @@ class DynamicMessage : public Message { TypeInfo() : prototype(NULL) {} - ~TypeInfo() { - delete prototype; - } + ~TypeInfo() { delete prototype; } }; DynamicMessage(const TypeInfo* type_info); // This should only be used by GetPrototypeNoLock() to avoid dead lock. - DynamicMessage(const TypeInfo* type_info, bool lock_factory); + DynamicMessage(TypeInfo* type_info, bool lock_factory); ~DynamicMessage(); @@ -273,9 +287,7 @@ class DynamicMessage : public Message { // care of for us. This makes DynamicMessage compatible with -fsized-delete. // It doesn't work for MSVC though. #ifndef _MSC_VER - static void operator delete(void* ptr) { - ::operator delete(ptr); - } + static void operator delete(void* ptr) { ::operator delete(ptr); } #endif // !_MSC_VER private: @@ -313,8 +325,15 @@ DynamicMessage::DynamicMessage(const TypeInfo* type_info, Arena* arena) SharedCtor(true); } -DynamicMessage::DynamicMessage(const TypeInfo* type_info, bool lock_factory) +DynamicMessage::DynamicMessage(TypeInfo* type_info, bool lock_factory) : type_info_(type_info), arena_(NULL), cached_byte_size_(0) { + // The prototype in type_info has to be set before creating the prototype + // instance on memory. e.g., message Foo { map a = 1; }. When + // creating prototype for Foo, prototype of the map entry will also be + // created, which needs the address of the prototype of Foo (the value in + // map). To break the cyclic dependency, we have to assign the address of + // prototype into type_info first. + type_info->prototype = this; SharedCtor(lock_factory); } @@ -330,7 +349,7 @@ void DynamicMessage::SharedCtor(bool lock_factory) { const Descriptor* descriptor = type_info_->type; // Initialize oneof cases. - for (int i = 0 ; i < descriptor->oneof_decl_count(); ++i) { + for (int i = 0; i < descriptor->oneof_decl_count(); ++i) { new (OffsetToPointer(type_info_->oneof_case_offset + sizeof(uint32) * i)) uint32(0); } @@ -357,18 +376,18 @@ void DynamicMessage::SharedCtor(bool lock_factory) { } \ break; - HANDLE_TYPE(INT32 , int32 ); - HANDLE_TYPE(INT64 , int64 ); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE(FLOAT , float ); - HANDLE_TYPE(BOOL , bool ); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_ENUM: if (!field->is_repeated()) { - new(field_ptr) int(field->default_value_enum()->number()); + new (field_ptr) int(field->default_value_enum()->number()); } else { new (field_ptr) RepeatedField(arena_); } @@ -379,7 +398,7 @@ void DynamicMessage::SharedCtor(bool lock_factory) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: if (!field->is_repeated()) { - const string* default_value; + const std::string* default_value; if (is_prototype()) { default_value = &field->default_value_string(); } else { @@ -388,10 +407,10 @@ void DynamicMessage::SharedCtor(bool lock_factory) { type_info_->offsets[i])) ->Get()); } - ArenaStringPtr* asp = new(field_ptr) ArenaStringPtr(); + ArenaStringPtr* asp = new (field_ptr) ArenaStringPtr(); asp->UnsafeSetDefault(default_value); } else { - new (field_ptr) RepeatedPtrField(arena_); + new (field_ptr) RepeatedPtrField(arena_); } break; } @@ -399,7 +418,7 @@ void DynamicMessage::SharedCtor(bool lock_factory) { case FieldDescriptor::CPPTYPE_MESSAGE: { if (!field->is_repeated()) { - new(field_ptr) Message*(NULL); + new (field_ptr) Message*(NULL); } else { if (IsMapFieldInApi(field)) { // We need to lock in most cases to avoid data racing. Only not lock @@ -445,7 +464,8 @@ DynamicMessage::~DynamicMessage() { if (type_info_->extensions_offset != -1) { reinterpret_cast( - OffsetToPointer(type_info_->extensions_offset))->~ExtensionSet(); + OffsetToPointer(type_info_->extensions_offset)) + ->~ExtensionSet(); } // We need to manually run the destructors for repeated fields and strings, @@ -459,21 +479,20 @@ DynamicMessage::~DynamicMessage() { for (int i = 0; i < descriptor->field_count(); i++) { const FieldDescriptor* field = descriptor->field(i); if (field->containing_oneof()) { - void* field_ptr = OffsetToPointer( - type_info_->oneof_case_offset - + sizeof(uint32) * field->containing_oneof()->index()); - if (*(reinterpret_cast(field_ptr)) == - field->number()) { - field_ptr = OffsetToPointer(type_info_->offsets[ - descriptor->field_count() + field->containing_oneof()->index()]); + void* field_ptr = + OffsetToPointer(type_info_->oneof_case_offset + + sizeof(uint32) * field->containing_oneof()->index()); + if (*(reinterpret_cast(field_ptr)) == field->number()) { + field_ptr = OffsetToPointer( + type_info_->offsets[descriptor->field_count() + + field->containing_oneof()->index()]); if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) { switch (field->options().ctype()) { default: case FieldOptions::STRING: { - const ::std::string* default_value = + const std::string* default_value = &(reinterpret_cast( - reinterpret_cast( - type_info_->prototype) + + reinterpret_cast(type_info_->prototype) + type_info_->offsets[i]) ->Get()); reinterpret_cast(field_ptr)->Destroy( @@ -491,28 +510,28 @@ DynamicMessage::~DynamicMessage() { if (field->is_repeated()) { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - reinterpret_cast*>(field_ptr) \ - ->~RepeatedField(); \ - break - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + reinterpret_cast*>(field_ptr) \ + ->~RepeatedField(); \ + break + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - reinterpret_cast*>(field_ptr) - ->~RepeatedPtrField(); + reinterpret_cast*>(field_ptr) + ->~RepeatedPtrField(); break; } break; @@ -531,18 +550,18 @@ DynamicMessage::~DynamicMessage() { switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: { - const ::std::string* default_value = + const std::string* default_value = &(reinterpret_cast( type_info_->prototype->OffsetToPointer( type_info_->offsets[i])) ->Get()); - reinterpret_cast(field_ptr)->Destroy( - default_value, NULL); + reinterpret_cast(field_ptr)->Destroy(default_value, + NULL); break; } } } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - if (!is_prototype()) { + if (!is_prototype()) { Message* message = *reinterpret_cast(field_ptr); if (message != NULL) { delete message; @@ -570,7 +589,7 @@ void DynamicMessage::CrossLinkPrototypes() { // For singular fields, the field is just a pointer which should // point to the prototype. *reinterpret_cast(field_ptr) = - factory->GetPrototypeNoLock(field->message_type()); + factory->GetPrototypeNoLock(field->message_type()); } } } @@ -613,20 +632,19 @@ struct DynamicMessageFactory::PrototypeMap { }; DynamicMessageFactory::DynamicMessageFactory() - : pool_(NULL), delegate_to_generated_factory_(false), - prototypes_(new PrototypeMap) { -} + : pool_(NULL), + delegate_to_generated_factory_(false), + prototypes_(new PrototypeMap) {} DynamicMessageFactory::DynamicMessageFactory(const DescriptorPool* pool) - : pool_(pool), delegate_to_generated_factory_(false), - prototypes_(new PrototypeMap) { -} + : pool_(pool), + delegate_to_generated_factory_(false), + prototypes_(new PrototypeMap) {} DynamicMessageFactory::~DynamicMessageFactory() { for (PrototypeMap::Map::iterator iter = prototypes_->map_.begin(); iter != prototypes_->map_.end(); ++iter) { - DeleteDefaultOneofInstance(iter->second->type, - iter->second->offsets.get(), + DeleteDefaultOneofInstance(iter->second->type, iter->second->offsets.get(), iter->second->prototype); delete iter->second; } @@ -657,8 +675,8 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( type_info->pool = (pool_ == NULL) ? type->file()->pool() : pool_; type_info->factory = this; - // We need to construct all the structures passed to - // GeneratedMessageReflection's constructor. This includes: + // We need to construct all the structures passed to Reflection's constructor. + // This includes: // - A block of memory that contains space for all the message's fields. // - An array of integers indicating the byte offset of each field within // this block. @@ -666,8 +684,7 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( // or not that field is set. // Compute size and offsets. - uint32* offsets = - new uint32[type->field_count() + type->oneof_decl_count()]; + uint32* offsets = new uint32[type->field_count() + type->oneof_decl_count()]; type_info->offsets.reset(offsets); // Decide all field offsets by packing in order. @@ -682,7 +699,7 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( } else { type_info->has_bits_offset = size; int has_bits_array_size = - DivideRoundingUp(type->field_count(), bitsizeof(uint32)); + DivideRoundingUp(type->field_count(), bitsizeof(uint32)); size += has_bits_array_size * sizeof(uint32); size = AlignOffset(size); @@ -743,7 +760,6 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( // alignment is not necessary. type_info->size = size; - // Construct the reflection object. if (type->oneof_decl_count() > 0) { @@ -764,38 +780,28 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( void* base = operator new(size); memset(base, 0, size); - // The prototype in type_info has to be set before creating the prototype - // instance on memory. e.g., message Foo { map a = 1; }. When - // creating prototype for Foo, prototype of the map entry will also be - // created, which needs the address of the prototype of Foo (the value in - // map). To break the cyclic dependency, we have to assgin the address of - // prototype into type_info first. - type_info->prototype = static_cast(base); - // We have already locked the factory so we should not lock in the constructor // of dynamic message to avoid dead lock. DynamicMessage* prototype = new (base) DynamicMessage(type_info, false); if (type->oneof_decl_count() > 0 || num_weak_fields > 0) { // Construct default oneof instance. - ConstructDefaultOneofInstance(type_info->type, - type_info->offsets.get(), + ConstructDefaultOneofInstance(type_info->type, type_info->offsets.get(), prototype); } - internal::ReflectionSchema schema = { - type_info->prototype, - type_info->offsets.get(), - type_info->has_bits_indices.get(), - type_info->has_bits_offset, - type_info->internal_metadata_offset, - type_info->extensions_offset, - type_info->oneof_case_offset, - type_info->size, - type_info->weak_field_map_offset}; + internal::ReflectionSchema schema = {type_info->prototype, + type_info->offsets.get(), + type_info->has_bits_indices.get(), + type_info->has_bits_offset, + type_info->internal_metadata_offset, + type_info->extensions_offset, + type_info->oneof_case_offset, + type_info->size, + type_info->weak_field_map_offset}; - type_info->reflection.reset(new GeneratedMessageReflection( - type_info->type, schema, type_info->pool, this)); + type_info->reflection.reset( + new Reflection(type_info->type, schema, type_info->pool, this)); // Cross link prototypes. prototype->CrossLinkPrototypes(); @@ -804,31 +810,31 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( } void DynamicMessageFactory::ConstructDefaultOneofInstance( - const Descriptor* type, - const uint32 offsets[], + const Descriptor* type, const uint32 offsets[], void* default_oneof_or_weak_instance) { for (int i = 0; i < type->oneof_decl_count(); i++) { for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = type->oneof_decl(i)->field(j); - void* field_ptr = reinterpret_cast( - default_oneof_or_weak_instance) + offsets[field->index()]; + void* field_ptr = + reinterpret_cast(default_oneof_or_weak_instance) + + offsets[field->index()]; switch (field->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - new(field_ptr) TYPE(field->default_value_##TYPE()); \ - break; +#define HANDLE_TYPE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + new (field_ptr) TYPE(field->default_value_##TYPE()); \ + break; - HANDLE_TYPE(INT32 , int32 ); - HANDLE_TYPE(INT64 , int64 ); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE(FLOAT , float ); - HANDLE_TYPE(BOOL , bool ); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_ENUM: - new(field_ptr) int(field->default_value_enum()->number()); + new (field_ptr) int(field->default_value_enum()->number()); break; case FieldDescriptor::CPPTYPE_STRING: switch (field->options().ctype()) { @@ -841,7 +847,7 @@ void DynamicMessageFactory::ConstructDefaultOneofInstance( break; case FieldDescriptor::CPPTYPE_MESSAGE: { - new(field_ptr) Message*(NULL); + new (field_ptr) Message*(NULL); break; } } @@ -850,8 +856,7 @@ void DynamicMessageFactory::ConstructDefaultOneofInstance( } void DynamicMessageFactory::DeleteDefaultOneofInstance( - const Descriptor* type, - const uint32 offsets[], + const Descriptor* type, const uint32 offsets[], const void* default_oneof_instance) { for (int i = 0; i < type->oneof_decl_count(); i++) { for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) { diff --git a/src/google/protobuf/dynamic_message.h b/src/google/protobuf/dynamic_message.h index c614ee5a22..765f9e3a63 100644 --- a/src/google/protobuf/dynamic_message.h +++ b/src/google/protobuf/dynamic_message.h @@ -42,21 +42,23 @@ #include #include +#include #include #include #include -#include #ifdef SWIG #error "You cannot SWIG proto headers" #endif +#include + namespace google { namespace protobuf { // Defined in other files. -class Descriptor; // descriptor.h -class DescriptorPool; // descriptor.h +class Descriptor; // descriptor.h +class DescriptorPool; // descriptor.h // Constructs implementations of Message which can emulate types which are not // known at compile-time. @@ -75,7 +77,7 @@ class DescriptorPool; // descriptor.h // encapsulates this "cache". All DynamicMessages of the same type created // from the same factory will share the same support data. Any Descriptors // used with a particular factory must outlive the factory. -class LIBPROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory { +class PROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory { public: // Construct a DynamicMessageFactory that will search for extensions in // the DescriptorPool in which the extendee is defined. @@ -151,10 +153,9 @@ class LIBPROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory { }; // Helper for computing a sorted list of map entries via reflection. -class LIBPROTOBUF_EXPORT DynamicMapSorter { +class PROTOBUF_EXPORT DynamicMapSorter { public: - static std::vector Sort(const Message& message, - int map_size, + static std::vector Sort(const Message& message, int map_size, const Reflection* reflection, const FieldDescriptor* field) { std::vector result(static_cast(map_size)); @@ -162,7 +163,8 @@ class LIBPROTOBUF_EXPORT DynamicMapSorter { reflection->GetRepeatedPtrField(message, field); size_t i = 0; for (RepeatedPtrField::const_pointer_iterator it = - map_field.pointer_begin(); it != map_field.pointer_end(); ) { + map_field.pointer_begin(); + it != map_field.pointer_end();) { result[i++] = *it++; } GOOGLE_DCHECK_EQ(result.size(), i); @@ -172,9 +174,9 @@ class LIBPROTOBUF_EXPORT DynamicMapSorter { #ifndef NDEBUG for (size_t j = 1; j < static_cast(map_size); j++) { if (!comparator(result[j - 1], result[j])) { - GOOGLE_LOG(ERROR) << (comparator(result[j], result[j - 1]) ? - "internal error in map key sorting" : - "map keys are not unique"); + GOOGLE_LOG(ERROR) << (comparator(result[j], result[j - 1]) + ? "internal error in map key sorting" + : "map keys are not unique"); } } #endif @@ -182,7 +184,7 @@ class LIBPROTOBUF_EXPORT DynamicMapSorter { } private: - class LIBPROTOBUF_EXPORT MapEntryMessageComparator { + class PROTOBUF_EXPORT MapEntryMessageComparator { public: explicit MapEntryMessageComparator(const Descriptor* descriptor) : field_(descriptor->field(0)) {} @@ -216,8 +218,8 @@ class LIBPROTOBUF_EXPORT DynamicMapSorter { return first < second; } case FieldDescriptor::CPPTYPE_STRING: { - string first = reflection->GetString(*a, field_); - string second = reflection->GetString(*b, field_); + std::string first = reflection->GetString(*a, field_); + std::string second = reflection->GetString(*b, field_); return first < second; } default: @@ -234,4 +236,6 @@ class LIBPROTOBUF_EXPORT DynamicMapSorter { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__ diff --git a/src/google/protobuf/dynamic_message_unittest.cc b/src/google/protobuf/dynamic_message_unittest.cc index 94446b08f7..42759de776 100644 --- a/src/google/protobuf/dynamic_message_unittest.cc +++ b/src/google/protobuf/dynamic_message_unittest.cc @@ -72,7 +72,7 @@ class DynamicMessageTest : public ::testing::TestWithParam { const Descriptor* proto3_descriptor_; const Message* proto3_prototype_; - DynamicMessageTest(): factory_(&pool_) {} + DynamicMessageTest() : factory_(&pool_) {} virtual void SetUp() { // We want to make sure that DynamicMessage works (particularly with @@ -86,11 +86,11 @@ class DynamicMessageTest : public ::testing::TestWithParam { unittest::TestAllTypes::descriptor()->file()->CopyTo(&unittest_file); unittest_import::ImportMessage::descriptor()->file()->CopyTo( - &unittest_import_file); + &unittest_import_file); unittest_import::PublicImportMessage::descriptor()->file()->CopyTo( - &unittest_import_public_file); - proto2_nofieldpresence_unittest::TestAllTypes::descriptor()-> - file()->CopyTo(&unittest_no_field_presence_file); + &unittest_import_public_file); + proto2_nofieldpresence_unittest::TestAllTypes::descriptor()->file()->CopyTo( + &unittest_no_field_presence_file); ASSERT_TRUE(pool_.BuildFile(unittest_import_public_file) != NULL); ASSERT_TRUE(pool_.BuildFile(unittest_import_file) != NULL); @@ -102,23 +102,22 @@ class DynamicMessageTest : public ::testing::TestWithParam { prototype_ = factory_.GetPrototype(descriptor_); extensions_descriptor_ = - pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions"); + pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions"); ASSERT_TRUE(extensions_descriptor_ != NULL); extensions_prototype_ = factory_.GetPrototype(extensions_descriptor_); packed_descriptor_ = - pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes"); + pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes"); ASSERT_TRUE(packed_descriptor_ != NULL); packed_prototype_ = factory_.GetPrototype(packed_descriptor_); oneof_descriptor_ = - pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2"); + pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2"); ASSERT_TRUE(oneof_descriptor_ != NULL); oneof_prototype_ = factory_.GetPrototype(oneof_descriptor_); - proto3_descriptor_ = - pool_.FindMessageTypeByName( - "proto2_nofieldpresence_unittest.TestAllTypes"); + proto3_descriptor_ = pool_.FindMessageTypeByName( + "proto2_nofieldpresence_unittest.TestAllTypes"); ASSERT_TRUE(proto3_descriptor_ != NULL); proto3_prototype_ = factory_.GetPrototype(proto3_descriptor_); } @@ -146,7 +145,7 @@ TEST_P(DynamicMessageTest, IndependentOffsets) { // one to a unique value then checking that they all still have those // unique values (i.e. they don't stomp each other). Arena arena; - Message* message = prototype_->New(GetParam()? &arena : NULL); + Message* message = prototype_->New(GetParam() ? &arena : NULL); TestUtil::ReflectionTester reflection_tester(descriptor_); reflection_tester.SetAllFieldsViaReflection(message); @@ -160,7 +159,7 @@ TEST_P(DynamicMessageTest, IndependentOffsets) { TEST_P(DynamicMessageTest, Extensions) { // Check that extensions work. Arena arena; - Message* message = extensions_prototype_->New(GetParam()? &arena : NULL); + Message* message = extensions_prototype_->New(GetParam() ? &arena : NULL); TestUtil::ReflectionTester reflection_tester(extensions_descriptor_); reflection_tester.SetAllFieldsViaReflection(message); @@ -174,7 +173,7 @@ TEST_P(DynamicMessageTest, Extensions) { TEST_P(DynamicMessageTest, PackedFields) { // Check that packed fields work properly. Arena arena; - Message* message = packed_prototype_->New(GetParam()? &arena : NULL); + Message* message = packed_prototype_->New(GetParam() ? &arena : NULL); TestUtil::ReflectionTester reflection_tester(packed_descriptor_); reflection_tester.SetPackedFieldsViaReflection(message); @@ -188,23 +187,25 @@ TEST_P(DynamicMessageTest, PackedFields) { TEST_P(DynamicMessageTest, Oneof) { // Check that oneof fields work properly. Arena arena; - Message* message = oneof_prototype_->New(GetParam()? &arena : NULL); + Message* message = oneof_prototype_->New(GetParam() ? &arena : NULL); // Check default values. const Descriptor* descriptor = message->GetDescriptor(); const Reflection* reflection = message->GetReflection(); - EXPECT_EQ(0, reflection->GetInt32( - *message, descriptor->FindFieldByName("foo_int"))); + EXPECT_EQ(0, reflection->GetInt32(*message, + descriptor->FindFieldByName("foo_int"))); EXPECT_EQ("", reflection->GetString( - *message, descriptor->FindFieldByName("foo_string"))); + *message, descriptor->FindFieldByName("foo_string"))); + EXPECT_EQ("", reflection->GetString(*message, + descriptor->FindFieldByName("foo_cord"))); EXPECT_EQ("", reflection->GetString( - *message, descriptor->FindFieldByName("foo_cord"))); + *message, descriptor->FindFieldByName("foo_string_piece"))); EXPECT_EQ("", reflection->GetString( - *message, descriptor->FindFieldByName("foo_string_piece"))); - EXPECT_EQ("", reflection->GetString( - *message, descriptor->FindFieldByName("foo_bytes"))); - EXPECT_EQ(unittest::TestOneof2::FOO, reflection->GetEnum( - *message, descriptor->FindFieldByName("foo_enum"))->number()); + *message, descriptor->FindFieldByName("foo_bytes"))); + EXPECT_EQ( + unittest::TestOneof2::FOO, + reflection->GetEnum(*message, descriptor->FindFieldByName("foo_enum")) + ->number()); const Descriptor* nested_descriptor; const Message* nested_prototype; nested_descriptor = @@ -219,23 +220,26 @@ TEST_P(DynamicMessageTest, Oneof) { pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2.FooGroup"); foogroup_prototype = factory_.GetPrototype(foogroup_descriptor); EXPECT_EQ(foogroup_prototype, - &reflection->GetMessage( - *message, descriptor->FindFieldByName("foogroup"))); + &reflection->GetMessage(*message, + descriptor->FindFieldByName("foogroup"))); EXPECT_NE(foogroup_prototype, &reflection->GetMessage( *message, descriptor->FindFieldByName("foo_lazy_message"))); - EXPECT_EQ(5, reflection->GetInt32( - *message, descriptor->FindFieldByName("bar_int"))); + EXPECT_EQ(5, reflection->GetInt32(*message, + descriptor->FindFieldByName("bar_int"))); EXPECT_EQ("STRING", reflection->GetString( - *message, descriptor->FindFieldByName("bar_string"))); + *message, descriptor->FindFieldByName("bar_string"))); EXPECT_EQ("CORD", reflection->GetString( - *message, descriptor->FindFieldByName("bar_cord"))); - EXPECT_EQ("SPIECE", reflection->GetString( - *message, descriptor->FindFieldByName("bar_string_piece"))); + *message, descriptor->FindFieldByName("bar_cord"))); + EXPECT_EQ("SPIECE", + reflection->GetString( + *message, descriptor->FindFieldByName("bar_string_piece"))); EXPECT_EQ("BYTES", reflection->GetString( - *message, descriptor->FindFieldByName("bar_bytes"))); - EXPECT_EQ(unittest::TestOneof2::BAR, reflection->GetEnum( - *message, descriptor->FindFieldByName("bar_enum"))->number()); + *message, descriptor->FindFieldByName("bar_bytes"))); + EXPECT_EQ( + unittest::TestOneof2::BAR, + reflection->GetEnum(*message, descriptor->FindFieldByName("bar_enum")) + ->number()); // Check set functions. TestUtil::ReflectionTester reflection_tester(oneof_descriptor_); @@ -254,7 +258,7 @@ TEST_P(DynamicMessageTest, SpaceUsed) { // to test very much here. Just make sure it appears to be working. Arena arena; - Message* message = prototype_->New(GetParam()? &arena : NULL); + Message* message = prototype_->New(GetParam() ? &arena : NULL); TestUtil::ReflectionTester reflection_tester(descriptor_); int initial_space_used = message->SpaceUsed(); @@ -316,7 +320,7 @@ TEST_F(DynamicMessageTest, Proto3) { delete message; } -INSTANTIATE_TEST_CASE_P(UseArena, DynamicMessageTest, ::testing::Bool()); +INSTANTIATE_TEST_SUITE_P(UseArena, DynamicMessageTest, ::testing::Bool()); } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/empty.pb.cc b/src/google/protobuf/empty.pb.cc index 5ca92661de..caa48d04ac 100644 --- a/src/google/protobuf/empty.pb.cc +++ b/src/google/protobuf/empty.pb.cc @@ -5,121 +5,102 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_OPEN class EmptyDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Empty_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsEmpty_google_2fprotobuf_2fempty_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_Empty_google_2fprotobuf_2fempty_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_Empty_default_instance_; - new (ptr) ::google::protobuf::Empty(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Empty_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Empty(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::Empty::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Empty::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_Empty_google_2fprotobuf_2fempty_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsEmpty_google_2fprotobuf_2fempty_2eproto}, {}}; - -void InitDefaults_google_2fprotobuf_2fempty_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_Empty_google_2fprotobuf_2fempty_2eproto.base); -} +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Empty_google_2fprotobuf_2fempty_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_Empty_google_2fprotobuf_2fempty_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2fempty_2eproto[1]; -constexpr ::google::protobuf::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fempty_2eproto = nullptr; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fempty_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fempty_2eproto[1]; +static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fempty_2eproto = nullptr; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fempty_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fempty_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2fempty_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Empty, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Empty, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, -1, sizeof(::google::protobuf::Empty)}, +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, -1, sizeof(PROTOBUF_NAMESPACE_ID::Empty)}, }; -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_Empty_default_instance_), +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Empty_default_instance_), }; -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2fempty_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2fempty_2eproto, "google/protobuf/empty.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2fempty_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2fempty_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2fempty_2eproto, file_level_service_descriptors_google_2fprotobuf_2fempty_2eproto, -}; - -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fempty_2eproto = { - false, InitDefaults_google_2fprotobuf_2fempty_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2fempty_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n\033google/protobuf/empty.proto\022\017google.pr" "otobuf\"\007\n\005EmptyBv\n\023com.google.protobufB\n" "EmptyProtoP\001Z\'github.com/golang/protobuf" "/ptypes/empty\370\001\001\242\002\003GPB\252\002\036Google.Protobuf" ".WellKnownTypesb\006proto3" -, - "google/protobuf/empty.proto", &assign_descriptors_table_google_2fprotobuf_2fempty_2eproto, 183, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2fempty_2eproto_deps[1] = { +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2fempty_2eproto_sccs[1] = { + &scc_info_Empty_google_2fprotobuf_2fempty_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fempty_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2fempty_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fempty_2eproto = { + &descriptor_table_google_2fprotobuf_2fempty_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2fempty_2eproto, "google/protobuf/empty.proto", 183, + &descriptor_table_google_2fprotobuf_2fempty_2eproto_once, descriptor_table_google_2fprotobuf_2fempty_2eproto_sccs, descriptor_table_google_2fprotobuf_2fempty_2eproto_deps, 1, 0, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2fempty_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2fempty_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2fempty_2eproto, file_level_service_descriptors_google_2fprotobuf_2fempty_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2fempty_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fempty_2eproto, deps, 0); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2fempty_2eproto = []() { AddDescriptors_google_2fprotobuf_2fempty_2eproto(); return true; }(); -namespace google { -namespace protobuf { +static bool dynamic_init_dummy_google_2fprotobuf_2fempty_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fempty_2eproto), true); +PROTOBUF_NAMESPACE_OPEN // =================================================================== void Empty::InitAsDefaultInstance() { } -class Empty::HasBitSetters { +class Empty::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Empty::Empty() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Empty) } -Empty::Empty(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +Empty::Empty(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.Empty) } Empty::Empty(const Empty& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr) { _internal_metadata_.MergeFrom(from._internal_metadata_); // @@protoc_insertion_point(copy_constructor:google.protobuf.Empty) } @@ -133,27 +114,27 @@ Empty::~Empty() { } void Empty::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void Empty::ArenaDtor(void* object) { Empty* _this = reinterpret_cast< Empty* >(object); (void)_this; } -void Empty::RegisterArenaDtor(::google::protobuf::Arena*) { +void Empty::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void Empty::SetCachedSize(int size) const { _cached_size_.Set(size); } const Empty& Empty::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_Empty_google_2fprotobuf_2fempty_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_Empty_google_2fprotobuf_2fempty_2eproto.base); return *internal_default_instance(); } void Empty::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Empty) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -161,58 +142,47 @@ void Empty::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Empty::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Empty::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Empty::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Empty) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; handle_unusual: if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); } success: @@ -225,29 +195,15 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Empty::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Empty) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Empty) -} - -::google::protobuf::uint8* Empty::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Empty::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Empty) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Empty) return target; @@ -257,29 +213,28 @@ size_t Empty::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Empty) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Empty::MergeFrom(const ::google::protobuf::Message& from) { +void Empty::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Empty) GOOGLE_DCHECK_NE(&from, this); const Empty* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Empty) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Empty) MergeFrom(*source); @@ -290,12 +245,12 @@ void Empty::MergeFrom(const Empty& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Empty) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; } -void Empty::CopyFrom(const ::google::protobuf::Message& from) { +void Empty::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Empty) if (&from == this) return; Clear(); @@ -313,45 +268,23 @@ bool Empty::IsInitialized() const { return true; } -void Empty::Swap(Empty* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - Empty* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void Empty::UnsafeArenaSwap(Empty* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void Empty::InternalSwap(Empty* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); } -::google::protobuf::Metadata Empty::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fempty_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fempty_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Empty::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Empty* Arena::CreateMaybeMessage< ::google::protobuf::Empty >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::Empty >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Empty* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Empty >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::Empty >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/empty.pb.h b/src/google/protobuf/empty.pb.h index 65f0b192f9..50e82713b8 100644 --- a/src/google/protobuf/empty.pb.h +++ b/src/google/protobuf/empty.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/empty.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2fempty_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2fempty_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fempty_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fempty_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,57 +27,61 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fempty_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fempty_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fempty_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[1] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fempty_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] + 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2fempty_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fempty_2eproto; +PROTOBUF_NAMESPACE_OPEN class Empty; class EmptyDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern EmptyDefaultTypeInternal _Empty_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Empty* Arena::CreateMaybeMessage<::google::protobuf::Empty>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { +PROTOBUF_EXPORT extern EmptyDefaultTypeInternal _Empty_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Empty* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN // =================================================================== -class LIBPROTOBUF_EXPORT Empty : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Empty) */ { +class PROTOBUF_EXPORT Empty : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Empty) */ { public: Empty(); virtual ~Empty(); Empty(const Empty& from); - - inline Empty& operator=(const Empty& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Empty(Empty&& from) noexcept : Empty() { *this = ::std::move(from); } + inline Empty& operator=(const Empty& from) { + CopyFrom(from); + return *this; + } inline Empty& operator=(Empty&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -85,15 +90,21 @@ class LIBPROTOBUF_EXPORT Empty : public ::google::protobuf::Message /* @@protoc_ } return *this; } - #endif - inline ::google::protobuf::Arena* GetArena() const final { + + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Empty& default_instance(); @@ -105,54 +116,66 @@ class LIBPROTOBUF_EXPORT Empty : public ::google::protobuf::Message /* @@protoc_ static constexpr int kIndexInFileMessages = 0; - void UnsafeArenaSwap(Empty* other); - void Swap(Empty* other); friend void swap(Empty& a, Empty& b) { a.Swap(&b); } + inline void Swap(Empty* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(Empty* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Empty* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Empty* New(::google::protobuf::Arena* arena) const final { + Empty* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Empty& from); void MergeFrom(const Empty& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Empty* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Empty"; + } protected: - explicit Empty(::google::protobuf::Arena* arena); + explicit Empty(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -160,7 +183,14 @@ class LIBPROTOBUF_EXPORT Empty : public ::google::protobuf::Message /* @@protoc_ } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fempty_2eproto); + return ::descriptor_table_google_2fprotobuf_2fempty_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- @@ -168,13 +198,13 @@ class LIBPROTOBUF_EXPORT Empty : public ::google::protobuf::Message /* @@protoc_ // @@protoc_insertion_point(class_scope:google.protobuf.Empty) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2fempty_2eproto; }; // =================================================================== @@ -194,10 +224,9 @@ class LIBPROTOBUF_EXPORT Empty : public ::google::protobuf::Message /* @@protoc_ // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2fempty_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fempty_2eproto diff --git a/src/google/protobuf/extension_set.cc b/src/google/protobuf/extension_set.cc index cb40ab7188..8342482b38 100644 --- a/src/google/protobuf/extension_set.cc +++ b/src/google/protobuf/extension_set.cc @@ -38,19 +38,17 @@ #include #include #include +#include +#include #include #include #include #include #include -#include #include #include #include -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -#include -#endif namespace google { namespace protobuf { @@ -78,8 +76,8 @@ inline bool is_packable(WireFormatLite::WireType type) { case WireFormatLite::WIRETYPE_END_GROUP: return false; - // Do not add a default statement. Let the compiler complain when someone - // adds a new wire type. + // 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; @@ -101,20 +99,20 @@ 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) { +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 << "."; + << containing_type->GetTypeName() << "\", field number " + << number << "."; } } -const ExtensionInfo* FindRegisteredExtension( - const MessageLite* containing_type, int number) { +const ExtensionInfo* FindRegisteredExtension(const MessageLite* containing_type, + int number) { return global_registry == nullptr ? nullptr : FindOrNull(*global_registry, @@ -178,7 +176,7 @@ void ExtensionSet::RegisterMessageExtension(const MessageLite* containing_type, GOOGLE_CHECK(type == WireFormatLite::TYPE_MESSAGE || type == WireFormatLite::TYPE_GROUP); ExtensionInfo info(type, is_repeated, is_packed); - info.message_prototype = prototype; + info.message_info = {prototype}; Register(containing_type, number, info); } @@ -206,7 +204,7 @@ ExtensionSet::~ExtensionSet() { // Deletes all allocated extensions. if (arena_ == NULL) { ForEach([](int /* number */, Extension& ext) { ext.Free(); }); - if (GOOGLE_PREDICT_FALSE(is_large())) { + if (PROTOBUF_PREDICT_FALSE(is_large())) { delete map_.large; } else { DeleteFlatMap(map_.flat, flat_capacity_); @@ -214,8 +212,8 @@ ExtensionSet::~ExtensionSet() { } } -void ExtensionSet::DeleteFlatMap( - const ExtensionSet::KeyValue* flat, uint16 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. @@ -223,9 +221,9 @@ void ExtensionSet::DeleteFlatMap( "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 + ::operator delete[](const_cast(flat), + sizeof(*flat) * flat_capacity); +#else // !__cpp_sized_deallocation delete[] flat; #endif // !__cpp_sized_deallocation } @@ -280,88 +278,88 @@ void ExtensionSet::ClearExtension(int number) { namespace { -enum Cardinality { - REPEATED, - OPTIONAL -}; +enum { REPEATED_FIELD, OPTIONAL_FIELD }; } // namespace -#define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \ - GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? REPEATED : OPTIONAL, LABEL); \ +#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, 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, 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, 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, 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, UPPERCASE); \ - GOOGLE_DCHECK_EQ(extension->is_packed, packed); \ - } \ - extension->repeated_##LOWERCASE##_value->Add(value); \ -} - -PRIMITIVE_ACCESSORS( INT32, int32, Int32) -PRIMITIVE_ACCESSORS( INT64, int64, Int64) +#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(FLOAT, float, Float) PRIMITIVE_ACCESSORS(DOUBLE, double, Double) -PRIMITIVE_ACCESSORS( BOOL, bool, Bool) +PRIMITIVE_ACCESSORS(BOOL, bool, Bool) #undef PRIMITIVE_ACCESSORS @@ -392,43 +390,43 @@ void* ExtensionSet::MutableRawRepeatedField(int number, FieldType field_type, static_cast(field_type))) { case WireFormatLite::CPPTYPE_INT32: extension->repeated_int32_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_INT64: extension->repeated_int64_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_UINT32: extension->repeated_uint32_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_UINT64: extension->repeated_uint64_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_DOUBLE: extension->repeated_double_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_FLOAT: extension->repeated_float_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_BOOL: extension->repeated_bool_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_ENUM: extension->repeated_enum_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_STRING: extension->repeated_string_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; case WireFormatLite::CPPTYPE_MESSAGE: extension->repeated_message_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); break; } } @@ -448,7 +446,6 @@ void* ExtensionSet::MutableRawRepeatedField(int number) { return extension->repeated_int32_value; } - // ------------------------------------------------------------------- // Enums @@ -458,7 +455,7 @@ int ExtensionSet::GetEnum(int number, int default_value) const { // Not present. Return the default value. return default_value; } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, ENUM); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, ENUM); return extension->enum_value; } } @@ -471,7 +468,7 @@ void ExtensionSet::SetEnum(int number, FieldType type, int value, GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM); extension->is_repeated = false; } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, ENUM); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, ENUM); } extension->is_cleared = false; extension->enum_value = value; @@ -480,19 +477,18 @@ void ExtensionSet::SetEnum(int number, FieldType type, int 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, ENUM); + 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, ENUM); + 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, +void ExtensionSet::AddEnum(int number, FieldType type, bool packed, int value, const FieldDescriptor* descriptor) { Extension* extension; if (MaybeNewExtension(number, descriptor, &extension)) { @@ -501,9 +497,9 @@ void ExtensionSet::AddEnum(int number, FieldType type, extension->is_repeated = true; extension->is_packed = packed; extension->repeated_enum_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); } else { - GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM); + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, ENUM); GOOGLE_DCHECK_EQ(extension->is_packed, packed); } extension->repeated_enum_value->Add(value); @@ -512,49 +508,50 @@ void ExtensionSet::AddEnum(int number, FieldType type, // ------------------------------------------------------------------- // Strings -const string& ExtensionSet::GetString(int number, - const string& default_value) const { +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, STRING); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, STRING); return *extension->string_value; } } -string* ExtensionSet::MutableString(int number, FieldType type, - const FieldDescriptor* descriptor) { +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_); + extension->string_value = Arena::Create(arena_); } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, STRING); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, STRING); } extension->is_cleared = false; return extension->string_value; } -const string& ExtensionSet::GetRepeatedString(int number, int index) const { +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, STRING); + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING); return extension->repeated_string_value->Get(index); } -string* ExtensionSet::MutableRepeatedString(int number, int 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, STRING); + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING); return extension->repeated_string_value->Mutable(index); } -string* ExtensionSet::AddString(int number, FieldType type, - const FieldDescriptor* descriptor) { +std::string* ExtensionSet::AddString(int number, FieldType type, + const FieldDescriptor* descriptor) { Extension* extension; if (MaybeNewExtension(number, descriptor, &extension)) { extension->type = type; @@ -562,9 +559,9 @@ string* ExtensionSet::AddString(int number, FieldType type, extension->is_repeated = true; extension->is_packed = false; extension->repeated_string_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); } else { - GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING); + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING); } return extension->repeated_string_value->Add(); } @@ -579,7 +576,7 @@ const MessageLite& ExtensionSet::GetMessage( // Not present. Return the default value. return default_value; } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); if (extension->is_lazy) { return extension->lazymessage_value->GetMessage(default_value); } else { @@ -606,7 +603,7 @@ MessageLite* ExtensionSet::MutableMessage(int number, FieldType type, extension->is_cleared = false; return extension->message_value; } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); extension->is_cleared = false; if (extension->is_lazy) { return extension->lazymessage_value->MutableMessage(prototype); @@ -645,7 +642,7 @@ void ExtensionSet::SetAllocatedMessage(int number, FieldType type, extension->message_value->CheckTypeAndMergeFrom(*message); } } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); if (extension->is_lazy) { extension->lazymessage_value->SetAllocatedMessage(message); } else { @@ -681,7 +678,7 @@ void ExtensionSet::UnsafeArenaSetAllocatedMessage( extension->is_lazy = false; extension->message_value = message; } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); if (extension->is_lazy) { extension->lazymessage_value->UnsafeArenaSetAllocatedMessage(message); } else { @@ -701,7 +698,7 @@ MessageLite* ExtensionSet::ReleaseMessage(int number, // Not present. Return NULL. return NULL; } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); MessageLite* ret = NULL; if (extension->is_lazy) { ret = extension->lazymessage_value->ReleaseMessage(prototype); @@ -730,7 +727,7 @@ MessageLite* ExtensionSet::UnsafeArenaReleaseMessage( // Not present. Return NULL. return NULL; } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE); + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); MessageLite* ret = NULL; if (extension->is_lazy) { ret = extension->lazymessage_value->UnsafeArenaReleaseMessage(prototype); @@ -749,18 +746,18 @@ MessageLite* ExtensionSet::UnsafeArenaReleaseMessage( // MessageLite* ExtensionSet::ReleaseMessage(const FieldDescriptor* descriptor, // MessageFactory* factory); -const MessageLite& ExtensionSet::GetRepeatedMessage( - int number, int index) const { +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, MESSAGE); + 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, MESSAGE); + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE); return extension->repeated_message_value->Mutable(index); } @@ -773,9 +770,9 @@ MessageLite* ExtensionSet::AddMessage(int number, FieldType type, GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE); extension->is_repeated = true; extension->repeated_message_value = - Arena::CreateMessage >(arena_); + Arena::CreateMessage>(arena_); } else { - GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE); + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE); } // RepeatedPtrField does not know how to Add() since it cannot @@ -802,7 +799,7 @@ void ExtensionSet::RemoveLast(int number) { GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; GOOGLE_DCHECK(extension->is_repeated); - switch(cpp_type(extension->type)) { + switch (cpp_type(extension->type)) { case WireFormatLite::CPPTYPE_INT32: extension->repeated_int32_value->RemoveLast(); break; @@ -849,7 +846,7 @@ void ExtensionSet::SwapElements(int number, int index1, int index2) { GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; GOOGLE_DCHECK(extension->is_repeated); - switch(cpp_type(extension->type)) { + switch (cpp_type(extension->type)) { case WireFormatLite::CPPTYPE_INT32: extension->repeated_int32_value->SwapElements(index1, index2); break; @@ -912,8 +909,8 @@ size_t SizeOfUnion(ItX it_xs, ItX end_xs, ItY it_ys, ItY end_ys) { } // namespace void ExtensionSet::MergeFrom(const ExtensionSet& other) { - if (GOOGLE_PREDICT_TRUE(!is_large())) { - if (GOOGLE_PREDICT_TRUE(!other.is_large())) { + 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 { @@ -928,11 +925,11 @@ void ExtensionSet::MergeFrom(const ExtensionSet& other) { } void ExtensionSet::InternalExtensionMergeFrom( - int number, const Extension& other_extension) { + int number, const Extension& other_extension) { if (other_extension.is_repeated) { Extension* extension; - bool is_new = MaybeNewExtension(number, other_extension.descriptor, - &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; @@ -945,31 +942,31 @@ void ExtensionSet::InternalExtensionMergeFrom( } 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 < int32>); - HANDLE_TYPE( INT64, int64, RepeatedField < int64>); - HANDLE_TYPE( UINT32, uint32, RepeatedField < uint32>); - HANDLE_TYPE( UINT64, uint64, RepeatedField < uint64>); - HANDLE_TYPE( FLOAT, float, RepeatedField < float>); - HANDLE_TYPE( DOUBLE, double, RepeatedField < double>); - HANDLE_TYPE( BOOL, bool, RepeatedField < bool>); - HANDLE_TYPE( ENUM, enum, RepeatedField < int>); - HANDLE_TYPE( STRING, string, RepeatedPtrField< string>); +#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_); + Arena::CreateMessage>(arena_); } // We can't call RepeatedPtrField::MergeFrom() because // it would attempt to allocate new objects. @@ -992,32 +989,30 @@ void ExtensionSet::InternalExtensionMergeFrom( } 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); +#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(FLOAT, float, Float); HANDLE_TYPE(DOUBLE, double, Double); - HANDLE_TYPE( BOOL, bool, Bool); - HANDLE_TYPE( ENUM, enum, Enum); + 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, + 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); + bool is_new = + MaybeNewExtension(number, other_extension.descriptor, &extension); if (is_new) { extension->type = other_extension.type; extension->is_packed = other_extension.is_packed; @@ -1037,7 +1032,7 @@ void ExtensionSet::InternalExtensionMergeFrom( } } else { GOOGLE_DCHECK_EQ(extension->type, other_extension.type); - GOOGLE_DCHECK_EQ(extension->is_packed,other_extension.is_packed); + GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed); GOOGLE_DCHECK(!extension->is_repeated); if (other_extension.is_lazy) { if (extension->is_lazy) { @@ -1050,9 +1045,9 @@ void ExtensionSet::InternalExtensionMergeFrom( } } else { if (extension->is_lazy) { - extension->lazymessage_value->MutableMessage( - *other_extension.message_value)->CheckTypeAndMergeFrom( - *other_extension.message_value); + extension->lazymessage_value + ->MutableMessage(*other_extension.message_value) + ->CheckTypeAndMergeFrom(*other_extension.message_value); } else { extension->message_value->CheckTypeAndMergeFrom( *other_extension.message_value); @@ -1086,8 +1081,7 @@ void ExtensionSet::Swap(ExtensionSet* x) { } } -void ExtensionSet::SwapExtension(ExtensionSet* other, - int number) { +void ExtensionSet::SwapExtension(ExtensionSet* other, int number) { if (this == other) return; Extension* this_ext = FindOrNull(number); Extension* other_ext = other->FindOrNull(number); @@ -1141,7 +1135,7 @@ void ExtensionSet::SwapExtension(ExtensionSet* other, bool ExtensionSet::IsInitialized() const { // Extensions are never required. However, we need to check that all // embedded messages are initialized. - if (GOOGLE_PREDICT_FALSE(is_large())) { + if (PROTOBUF_PREDICT_FALSE(is_large())) { for (const auto& kv : *map_.large) { if (!kv.second.IsInitialized()) return false; } @@ -1153,9 +1147,11 @@ bool ExtensionSet::IsInitialized() const { return true; } -bool ExtensionSet::FindExtensionInfoFromTag( - uint32 tag, ExtensionFinder* extension_finder, int* field_number, - ExtensionInfo* extension, bool* was_packed_on_wire) { +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, @@ -1191,227 +1187,46 @@ bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input, int number; bool was_packed_on_wire; ExtensionInfo extension; - if (!FindExtensionInfoFromTag( - tag, extension_finder, &number, &extension, &was_packed_on_wire)) { + 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); + return ParseFieldWithExtensionInfo(number, was_packed_on_wire, extension, + input, field_skipper); } } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -std::pair ExtensionSet::ParseField( - uint32 tag, ParseClosure parent, const char* begin, const char* end, - const MessageLite* containing_type, +const char* ExtensionSet::ParseField( + uint64 tag, const char* ptr, const MessageLite* containing_type, internal::InternalMetadataWithArenaLite* metadata, internal::ParseContext* ctx) { GeneratedExtensionFinder finder(containing_type); - int number; + int number = tag >> 3; bool was_packed_on_wire; ExtensionInfo extension; - if (!FindExtensionInfoFromTag(tag, &finder, &number, &extension, - &was_packed_on_wire)) { - return UnknownFieldParse(tag, parent, begin, end, - metadata->mutable_unknown_fields(), ctx); - } - auto ptr = begin; - ParseClosure child; - int depth; - if (was_packed_on_wire) { - switch (extension.type) { -#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - child = { \ - internal::Packed##CPP_CAMELCASE##Parser, \ - MutableRawRepeatedField(number, extension.type, extension.is_packed, \ - extension.descriptor)}; \ - goto length_delim - 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: - ctx->extra_parse_data().SetEnumValidatorArg( - extension.enum_validity_check.func, - extension.enum_validity_check.arg, - metadata->mutable_unknown_fields(), tag >> 3); - child = { - internal::PackedValidEnumParserLiteArg, - MutableRawRepeatedField(number, extension.type, extension.is_packed, - extension.descriptor)}; - goto length_delim; - 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 = Varint::Parse64(ptr, &value); \ - if (ptr == nullptr) goto error; \ - 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); -#undef HANDLE_VARINT_TYPE -#define HANDLE_SVARINT_TYPE(UPPERCASE, CPP_CAMELCASE, SIZE) \ - case WireFormatLite::TYPE_##UPPERCASE: { \ - uint64 val; \ - ptr = Varint::Parse64(ptr, &val); \ - 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: { \ - CPPTYPE value; \ - std::memcpy(&value, ptr, sizeof(CPPTYPE)); \ - 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); - HANDLE_FIXED_TYPE(BOOL, Bool, bool); -#undef HANDLE_FIXED_TYPE - - case WireFormatLite::TYPE_ENUM: { - uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (ptr == nullptr) goto error; - 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: { - string* value = extension.is_repeated - ? AddString(number, WireFormatLite::TYPE_STRING, - extension.descriptor) - : MutableString(number, WireFormatLite::TYPE_STRING, - extension.descriptor); - child = {StringParser, value}; - goto length_delim; - } - - case WireFormatLite::TYPE_GROUP: { - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_prototype, extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_prototype, - extension.descriptor); - child = {value->_ParseFunc(), value}; - if (!ctx->PrepareGroup(tag, &depth)) goto error; - ptr = child(ptr, end, ctx); - if (!ptr) goto error; - if (ctx->GroupContinues(depth)) goto group_continues; - break; - } - - case WireFormatLite::TYPE_MESSAGE: { - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_prototype, extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_prototype, - extension.descriptor); - child = {value->_ParseFunc(), value}; - goto length_delim; - } - } + 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); +} - return std::make_pair(ptr, false); - -error: - return std::make_pair(nullptr, true); - -length_delim: - uint32 size; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - if (size > end - ptr) goto len_delim_till_end; - { - auto newend = ptr + size; - if (!ctx->ParseExactRange(child, ptr, newend)) { - goto error; - } - ptr = newend; - } - return std::make_pair(ptr, false); -len_delim_till_end: - return std::make_pair(ctx->StoreAndTailCall(ptr, end, parent, child, size), - true); - -group_continues: - ctx->StoreGroup(parent, child, depth); - return std::make_pair(ptr, true); +const char* ExtensionSet::ParseMessageSetItem( + const char* ptr, const MessageLite* containing_type, + internal::InternalMetadataWithArenaLite* metadata, + internal::ParseContext* ctx) { + return ParseMessageSetItemTmpl(ptr, containing_type, metadata, ctx); } + #endif -bool ExtensionSet::ParseFieldWithExtensionInfo( - int number, bool was_packed_on_wire, const ExtensionInfo& extension, - io::CodedInputStream* input, - FieldSkipper* field_skipper) { +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) { @@ -1420,39 +1235,40 @@ bool ExtensionSet::ParseFieldWithExtensionInfo( 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< \ - CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \ - 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); +#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; + input, &value)) + return false; if (extension.enum_validity_check.func( extension.enum_validity_check.arg, value)) { AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed, @@ -1475,41 +1291,42 @@ bool ExtensionSet::ParseFieldWithExtensionInfo( 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< \ - CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \ - 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); +#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; + input, &value)) + return false; if (!extension.enum_validity_check.func( extension.enum_validity_check.arg, value)) { @@ -1525,40 +1342,50 @@ bool ExtensionSet::ParseFieldWithExtensionInfo( break; } - case WireFormatLite::TYPE_STRING: { - string* value = extension.is_repeated ? - AddString(number, WireFormatLite::TYPE_STRING, extension.descriptor) : - MutableString(number, WireFormatLite::TYPE_STRING, - extension.descriptor); + 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: { - string* value = extension.is_repeated ? - AddString(number, WireFormatLite::TYPE_BYTES, extension.descriptor) : - MutableString(number, WireFormatLite::TYPE_BYTES, - extension.descriptor); + 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_prototype, extension.descriptor) : - MutableMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_prototype, extension.descriptor); + 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_prototype, extension.descriptor) : - MutableMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_prototype, extension.descriptor); + 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; } @@ -1630,7 +1457,7 @@ bool ExtensionSet::ParseMessageSetItemLite(io::CodedInputStream* input, bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input, const MessageLite* containing_type, - string* unknown_fields) { + std::string* unknown_fields) { io::StringOutputStream zcis(unknown_fields); io::CodedOutputStream output(&zcis); CodedOutputStreamFieldSkipper skipper(&output); @@ -1638,23 +1465,35 @@ bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input, return ParseMessageSetLite(input, &finder, &skipper); } -void ExtensionSet::SerializeWithCachedSizes( - int start_field_number, int end_field_number, - io::CodedOutputStream* output) const { - if (GOOGLE_PREDICT_FALSE(is_large())) { +uint8* ExtensionSet::InternalSerializeWithCachedSizesToArray( + 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) { - it->second.SerializeFieldWithCachedSizes(it->first, output); + target = it->second.InternalSerializeFieldWithCachedSizesToArray( + it->first, target, stream); } - return; + 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) { - it->second.SerializeFieldWithCachedSizes(it->first, output); + 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 { @@ -1683,20 +1522,20 @@ bool ExtensionSet::MaybeNewExtension(int number, 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); +#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 } @@ -1725,152 +1564,42 @@ void ExtensionSet::Extension::Clear() { } } -void ExtensionSet::Extension::SerializeFieldWithCachedSizes( - int number, - io::CodedOutputStream* output) const { - if (is_repeated) { - if (is_packed) { - if (cached_size == 0) return; - - WireFormatLite::WriteTag(number, - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); - output->WriteVarint32(cached_size); - - switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - WireFormatLite::Write##CAMELCASE##NoTag( \ - repeated_##LOWERCASE##_value->Get(i), output); \ - } \ - 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++) { \ - WireFormatLite::Write##CAMELCASE(number, \ - repeated_##LOWERCASE##_value->Get(i), output); \ - } \ - 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( 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 - } - } - } else if (!is_cleared) { - switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - WireFormatLite::Write##CAMELCASE(number, VALUE, output); \ - 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( 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) { - lazymessage_value->WriteMessage(number, output); - } else { - WireFormatLite::WriteMessage(number, *message_value, output); - } - break; - } - } -} - 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); +#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); +#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: @@ -1884,71 +1613,69 @@ size_t ExtensionSet::Extension::ByteSize(int number) const { cached_size = ToCachedSize(result); if (result > 0) { result += io::CodedOutputStream::VarintSize32(result); - result += io::CodedOutputStream::VarintSize32( - WireFormatLite::MakeTag(number, - WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); + 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); + 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); +#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); +#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) { @@ -1961,17 +1688,17 @@ size_t ExtensionSet::Extension::ByteSize(int number) const { } // 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); +#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 } } @@ -1982,19 +1709,19 @@ size_t ExtensionSet::Extension::ByteSize(int number) const { 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); +#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 } @@ -2008,20 +1735,20 @@ int ExtensionSet::Extension::GetSize() const { 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); +#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 } @@ -2071,7 +1798,7 @@ bool ExtensionSet::Extension::IsInitialized() const { void ExtensionSet::LazyMessageExtension::UnusedKeyMethod() {} const ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) const { - if (GOOGLE_PREDICT_FALSE(is_large())) { + if (PROTOBUF_PREDICT_FALSE(is_large())) { return FindOrNullInLargeMap(key); } const KeyValue* end = flat_end(); @@ -2094,7 +1821,7 @@ const ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap( } ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) { - if (GOOGLE_PREDICT_FALSE(is_large())) { + if (PROTOBUF_PREDICT_FALSE(is_large())) { return FindOrNullInLargeMap(key); } KeyValue* end = flat_end(); @@ -2116,7 +1843,7 @@ ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap(int key) { } std::pair ExtensionSet::Insert(int key) { - if (GOOGLE_PREDICT_FALSE(is_large())) { + if (PROTOBUF_PREDICT_FALSE(is_large())) { auto maybe = map_.large->insert({key, Extension()}); return {&maybe.first->second, maybe.second}; } @@ -2138,7 +1865,7 @@ std::pair ExtensionSet::Insert(int key) { } void ExtensionSet::GrowCapacity(size_t minimum_new_capacity) { - if (GOOGLE_PREDICT_FALSE(is_large())) { + if (PROTOBUF_PREDICT_FALSE(is_large())) { return; // LargeMap does not have a "reserve" method. } if (flat_capacity_ >= minimum_new_capacity) { @@ -2174,7 +1901,7 @@ void ExtensionSet::GrowCapacity(size_t minimum_new_capacity) { constexpr uint16 ExtensionSet::kMaximumFlatCapacity; void ExtensionSet::Erase(int key) { - if (GOOGLE_PREDICT_FALSE(is_large())) { + if (PROTOBUF_PREDICT_FALSE(is_large())) { map_.large->erase(key); return; } @@ -2201,6 +1928,222 @@ RepeatedStringTypeTraits::GetDefaultRepeatedField() { 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; + + 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++) { \ + 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++) { \ + 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++) { \ + 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++) { \ + stream->EnsureSpace(&target); \ + target = WireFormatLite::InternalWrite##CAMELCASE##ToArray( \ + 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: \ + 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: \ + 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: + stream->EnsureSpace(&target); + target = WireFormatLite::InternalWriteGroupToArray( + number, *message_value, target, stream); + break; + case WireFormatLite::TYPE_MESSAGE: + if (is_lazy) { + target = + lazymessage_value->WriteMessageToArray(number, target, stream); + } else { + stream->EnsureSpace(&target); + target = WireFormatLite::InternalWriteMessageToArray( + 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; + + 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::InternalWriteMessageToArray( + WireFormatLite::kMessageSetMessageNumber, *message_value, target, + stream); + } + // End group. + 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/src/google/protobuf/extension_set.h b/src/google/protobuf/extension_set.h index c22dfe07f7..a7de200590 100644 --- a/src/google/protobuf/extension_set.h +++ b/src/google/protobuf/extension_set.h @@ -47,14 +47,14 @@ #include #include +#include +#include +#include #include +#include #include -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -#include -#endif - #ifdef SWIG #error "You cannot SWIG proto headers" #endif @@ -69,13 +69,9 @@ class MessageLite; // message_lite.h class Message; // message.h class MessageFactory; // message.h class UnknownFieldSet; // unknown_field_set.h -namespace io { -class CodedInputStream; // coded_stream.h -class CodedOutputStream; // coded_stream.h -} // namespace io namespace internal { class FieldSkipper; // wire_format_lite.h -} +} // namespace internal } // namespace protobuf } // namespace google @@ -119,9 +115,13 @@ struct ExtensionInfo { const void* arg; }; + struct MessageInfo { + const MessageLite* prototype; + }; + union { EnumValidityCheck enum_validity_check; - const MessageLite* message_prototype; + MessageInfo message_info; }; // The descriptor for this extension, if one exists and is known. May be @@ -132,7 +132,7 @@ struct ExtensionInfo { // Abstract interface for an object which looks up extension definitions. Used // when parsing. -class LIBPROTOBUF_EXPORT ExtensionFinder { +class PROTOBUF_EXPORT ExtensionFinder { public: virtual ~ExtensionFinder(); @@ -142,7 +142,7 @@ class LIBPROTOBUF_EXPORT ExtensionFinder { // Implementation of ExtensionFinder which finds extensions defined in .proto // files which have been compiled into the binary. -class LIBPROTOBUF_EXPORT GeneratedExtensionFinder : public ExtensionFinder { +class PROTOBUF_EXPORT GeneratedExtensionFinder : public ExtensionFinder { public: GeneratedExtensionFinder(const MessageLite* containing_type) : containing_type_(containing_type) {} @@ -172,7 +172,7 @@ class MessageSetFieldSkipper; // 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 LIBPROTOBUF_EXPORT ExtensionSet { +class PROTOBUF_EXPORT ExtensionSet { public: ExtensionSet(); explicit ExtensionSet(Arena* arena); @@ -249,7 +249,8 @@ class LIBPROTOBUF_EXPORT ExtensionSet { 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 string& GetString(int number, const string& 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, @@ -267,8 +268,8 @@ class LIBPROTOBUF_EXPORT ExtensionSet { 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, const string& value, desc); - string* MutableString(int number, FieldType type, desc); + void SetString(int number, FieldType type, const 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* decsriptor, @@ -319,7 +320,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { double GetRepeatedDouble(int number, int index) const; bool GetRepeatedBool(int number, int index) const; int GetRepeatedEnum(int number, int index) const; - const string& GetRepeatedString(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); @@ -330,8 +331,8 @@ class LIBPROTOBUF_EXPORT ExtensionSet { 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, const string& value); - string* MutableRepeatedString(int number, int index); + void SetRepeatedString(int number, int index, const std::string& value); + std::string* MutableRepeatedString(int number, int index); MessageLite* MutableRepeatedMessage(int number, int index); #define desc const FieldDescriptor* descriptor // avoid line wrapping @@ -343,8 +344,8 @@ class LIBPROTOBUF_EXPORT ExtensionSet { 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, const string& value, desc); - string* AddString(int number, FieldType type, desc); + void AddString(int number, FieldType type, const 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, @@ -395,22 +396,44 @@ class LIBPROTOBUF_EXPORT ExtensionSet { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER // Lite parser - std::pair ParseField( - uint32 tag, ParseClosure parent, const char* begin, const char* end, - const MessageLite* containing_type, - internal::InternalMetadataWithArenaLite* metadata, - internal::ParseContext* ctx); + const char* ParseField(uint64 tag, const char* ptr, + const MessageLite* containing_type, + internal::InternalMetadataWithArenaLite* metadata, + internal::ParseContext* ctx); // Full parser - std::pair ParseField( - uint32 tag, ParseClosure parent, const char* begin, const char* end, - const Message* containing_type, - internal::InternalMetadataWithArena* metadata, - internal::ParseContext* ctx); - const char* ParseMessageSetItem(ParseClosure parent, const char* begin, - const char* end, - const Message* containing_type, - internal::InternalMetadataWithArena* metadata, - internal::ParseContext* ctx); + const char* ParseField(uint64 tag, const char* ptr, + const Message* containing_type, + internal::InternalMetadataWithArena* metadata, + internal::ParseContext* ctx); + template + const char* ParseMessageSet(const char* ptr, const Msg* containing_type, + Metadata* 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; + Metadata* 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; + } #endif // Parse an entire message in MessageSet format. Such messages have no @@ -426,7 +449,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { // FieldSkipper automatically). bool ParseMessageSet(io::CodedInputStream* input, const MessageLite* containing_type, - string* unknown_fields); + std::string* unknown_fields); bool ParseMessageSet(io::CodedInputStream* input, const Message* containing_type, UnknownFieldSet* unknown_fields); @@ -436,22 +459,28 @@ class LIBPROTOBUF_EXPORT ExtensionSet { // 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; + io::CodedOutputStream* output) const { + output->SetCur(InternalSerializeWithCachedSizesToArray( + 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* InternalSerializeWithCachedSizesToArray(int start_field_number, - int end_field_number, - bool deterministic, - uint8* target) const; + uint8* InternalSerializeWithCachedSizesToArray( + 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; - uint8* InternalSerializeMessageSetWithCachedSizesToArray(bool deterministic, - uint8* target) const; + 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() @@ -485,7 +514,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { private: // Interface of a lazily parsed singular message extension. - class LIBPROTOBUF_EXPORT LazyMessageExtension { + class PROTOBUF_EXPORT LazyMessageExtension { public: LazyMessageExtension() {} virtual ~LazyMessageExtension() {} @@ -502,7 +531,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { virtual bool IsInitialized() const = 0; - GOOGLE_PROTOBUF_DEPRECATED_MSG("Please use ByteSizeLong() instead") + 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; @@ -512,15 +541,11 @@ class LIBPROTOBUF_EXPORT ExtensionSet { virtual bool ReadMessage(const MessageLite& prototype, io::CodedInputStream* input) = 0; - virtual void WriteMessage(int number, - io::CodedOutputStream* output) const = 0; - virtual uint8* WriteMessageToArray(int number, uint8* target) const = 0; - virtual uint8* InternalWriteMessageToArray(int number, bool, - uint8* target) const { - // TODO(gpike): make this pure virtual. This is a placeholder because we - // need to update third_party/upb, for example. - return WriteMessageToArray(number, target); - } +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + virtual const char* _InternalParse(const char* ptr, ParseContext* ctx) = 0; +#endif + virtual uint8* WriteMessageToArray( + int number, uint8* target, io::EpsCopyOutputStream* stream) const = 0; private: virtual void UnusedKeyMethod(); // Dummy key method to avoid weak vtable. @@ -539,7 +564,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { double double_value; bool bool_value; int enum_value; - string* string_value; + std::string* string_value; MessageLite* message_value; LazyMessageExtension* lazymessage_value; @@ -551,7 +576,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { RepeatedField* repeated_double_value; RepeatedField* repeated_bool_value; RepeatedField* repeated_enum_value; - RepeatedPtrField* repeated_string_value; + RepeatedPtrField* repeated_string_value; RepeatedPtrField* repeated_message_value; }; @@ -587,15 +612,10 @@ class LIBPROTOBUF_EXPORT ExtensionSet { const FieldDescriptor* descriptor; // Some helper methods for operations on a single Extension. - void SerializeFieldWithCachedSizes(int number, - io::CodedOutputStream* output) const; - uint8* InternalSerializeFieldWithCachedSizesToArray(int number, - bool deterministic, - uint8* target) const; - void SerializeMessageSetItemWithCachedSizes( - int number, io::CodedOutputStream* output) const; + uint8* InternalSerializeFieldWithCachedSizesToArray( + int number, uint8* target, io::EpsCopyOutputStream* stream) const; uint8* InternalSerializeMessageSetItemWithCachedSizesToArray( - int number, bool deterministic, uint8* target) const; + int number, uint8* target, io::EpsCopyOutputStream* stream) const; size_t ByteSize(int number) const; size_t MessageSetItemByteSize(int number) const; void Clear(); @@ -655,7 +675,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { void Erase(int key); size_t Size() const { - return GOOGLE_PREDICT_FALSE(is_large()) ? map_.large->size() : flat_size_; + return PROTOBUF_PREDICT_FALSE(is_large()) ? map_.large->size() : flat_size_; } // Similar to std::for_each. @@ -671,7 +691,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { // Applies a functor to the pairs in sorted order. template KeyValueFunctor ForEach(KeyValueFunctor func) { - if (GOOGLE_PREDICT_FALSE(is_large())) { + 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)); @@ -680,7 +700,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet { // Applies a functor to the pairs in sorted order. template KeyValueFunctor ForEach(KeyValueFunctor func) const { - if (GOOGLE_PREDICT_FALSE(is_large())) { + 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)); @@ -689,13 +709,6 @@ class LIBPROTOBUF_EXPORT ExtensionSet { // Merges existing Extension from other_extension void InternalExtensionMergeFrom(int number, const Extension& other_extension); -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - bool FindExtension(uint32 tag, const Message* containing_type, - const internal::ParseContext* ctx, - ExtensionInfo* extension, int* number, - bool* was_packed_on_wire); -#endif - // 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 @@ -749,6 +762,53 @@ class LIBPROTOBUF_EXPORT ExtensionSet { ExtensionFinder* extension_finder, MessageSetFieldSkipper* field_skipper); +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + 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::InternalMetadataWithArenaLite* 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::InternalMetadataWithArena* metadata, + internal::ParseContext* ctx); + const char* ParseMessageSetItem( + const char* ptr, const MessageLite* containing_type, + internal::InternalMetadataWithArenaLite* metadata, + internal::ParseContext* ctx); + const char* ParseMessageSetItem(const char* ptr, + const Message* containing_type, + internal::InternalMetadataWithArena* 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, + T* metadata, const char* ptr, + internal::ParseContext* ctx); + template + const char* ParseMessageSetItemTmpl(const char* ptr, + const Msg* containing_type, + Metadata* metadata, + internal::ParseContext* ctx); +#endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + // 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 @@ -798,16 +858,16 @@ class LIBPROTOBUF_EXPORT ExtensionSet { // These are just for convenience... inline void ExtensionSet::SetString(int number, FieldType type, - const string& value, + const std::string& value, const FieldDescriptor* descriptor) { MutableString(number, type, descriptor)->assign(value); } inline void ExtensionSet::SetRepeatedString(int number, int index, - const string& value) { + const std::string& value) { MutableRepeatedString(number, index)->assign(value); } inline void ExtensionSet::AddString(int number, FieldType type, - const string& value, + const std::string& value, const FieldDescriptor* descriptor) { AddString(number, type, descriptor)->assign(value); } @@ -922,7 +982,7 @@ class RepeatedPrimitiveTypeTraits { } }; -class LIBPROTOBUF_EXPORT RepeatedPrimitiveDefaults { +class PROTOBUF_EXPORT RepeatedPrimitiveDefaults { private: template friend class RepeatedPrimitiveTypeTraits; @@ -999,22 +1059,22 @@ PROTOBUF_DEFINE_PRIMITIVE_TYPE(bool, Bool) // StringTypeTraits // Strings support both Set() and Mutable(). -class LIBPROTOBUF_EXPORT StringTypeTraits { +class PROTOBUF_EXPORT StringTypeTraits { public: - typedef const string& ConstType; - typedef string* MutableType; + typedef const std::string& ConstType; + typedef std::string* MutableType; typedef StringTypeTraits Singular; - static inline const string& Get(int number, const ExtensionSet& set, - ConstType default_value) { + 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 string& value, - ExtensionSet* set) { + static inline void Set(int number, FieldType field_type, + const std::string& value, ExtensionSet* set) { set->SetString(number, field_type, value, NULL); } - static inline string* Mutable(int number, FieldType field_type, - ExtensionSet* set) { + static inline std::string* Mutable(int number, FieldType field_type, + ExtensionSet* set) { return set->MutableString(number, field_type, NULL); } template @@ -1024,44 +1084,42 @@ class LIBPROTOBUF_EXPORT StringTypeTraits { } }; -class LIBPROTOBUF_EXPORT RepeatedStringTypeTraits { +class PROTOBUF_EXPORT RepeatedStringTypeTraits { public: - typedef const string& ConstType; - typedef string* MutableType; + typedef const std::string& ConstType; + typedef std::string* MutableType; typedef RepeatedStringTypeTraits Repeated; - typedef RepeatedPtrField RepeatedFieldType; + typedef RepeatedPtrField RepeatedFieldType; - static inline const string& Get(int number, const ExtensionSet& set, - int index) { + 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 string& value, + static inline void Set(int number, int index, const std::string& value, ExtensionSet* set) { set->SetRepeatedString(number, index, value); } - static inline string* Mutable(int number, int index, ExtensionSet* set) { + 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 string& value, ExtensionSet* set) { + const std::string& value, ExtensionSet* set) { set->AddString(number, field_type, value, NULL); } - static inline string* Add(int number, FieldType field_type, - ExtensionSet* set) { + static inline std::string* Add(int number, FieldType field_type, + ExtensionSet* set) { return set->AddString(number, field_type, NULL); } - static inline const RepeatedPtrField& GetRepeated( + static inline const RepeatedPtrField& GetRepeated( int number, const ExtensionSet& set) { - return *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*>( + 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)); } @@ -1325,101 +1383,101 @@ class ExtensionIdentifier { // // 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) \ +#define GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(CLASSNAME) \ /* Has, Size, Clear */ \ template \ inline bool HasExtension( \ - const ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ _extensions_.ClearExtension(id.number()); \ } \ \ template \ inline int ExtensionSize( \ - const ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ return _proto_TypeTraits::UnsafeArenaRelease(id.number(), _field_type, \ &_extensions_); \ @@ -1427,49 +1485,49 @@ class ExtensionIdentifier { \ /* Repeated accessors */ \ template \ inline typename _proto_TypeTraits::Repeated::ConstType GetExtension( \ - const ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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, \ @@ -1477,27 +1535,59 @@ class ExtensionIdentifier { } \ \ template \ inline const typename _proto_TypeTraits::Repeated::RepeatedFieldType& \ GetRepeatedExtension( \ - const ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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 ::GOOGLE_PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ + 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) { + const void* volatile unused = &extension; + (void)&unused; // Use address to avoid an extra load of volatile variable. +} + } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/extension_set_heavy.cc b/src/google/protobuf/extension_set_heavy.cc index 7c93c61d16..36cedd0f90 100644 --- a/src/google/protobuf/extension_set_heavy.cc +++ b/src/google/protobuf/extension_set_heavy.cc @@ -35,17 +35,20 @@ // Contains methods defined in extension_set.h which cannot be part of the // lite library because they use descriptors or reflection. +#include + #include #include +#include +#include #include #include #include -#include #include #include #include #include -#include +#include #include @@ -55,8 +58,7 @@ namespace protobuf { namespace internal { // A FieldSkipper used to store unknown MessageSet fields into UnknownFieldSet. -class MessageSetFieldSkipper - : public UnknownFieldSetFieldSkipper { +class MessageSetFieldSkipper : public UnknownFieldSetFieldSkipper { public: explicit MessageSetFieldSkipper(UnknownFieldSet* unknown_fields) : UnknownFieldSetFieldSkipper(unknown_fields) {} @@ -65,15 +67,15 @@ class MessageSetFieldSkipper virtual bool SkipMessageSetField(io::CodedInputStream* input, int field_number); }; -bool MessageSetFieldSkipper::SkipMessageSetField( - io::CodedInputStream* input, int field_number) { +bool MessageSetFieldSkipper::SkipMessageSetField(io::CodedInputStream* input, + int field_number) { uint32 length; if (!input->ReadVarint32(&length)) return false; if (unknown_fields_ == NULL) { return input->Skip(length); } else { - return input->ReadString( - unknown_fields_->AddLengthDelimited(field_number), length); + return input->ReadString(unknown_fields_->AddLengthDelimited(field_number), + length); } } @@ -138,10 +140,10 @@ inline WireFormatLite::FieldType field_type(FieldType type) { return static_cast(type); } -#define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \ - GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? FieldDescriptor::LABEL_REPEATED \ - : FieldDescriptor::LABEL_OPTIONAL, \ - FieldDescriptor::LABEL_##LABEL); \ +#define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \ + GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? FieldDescriptor::LABEL_REPEATED \ + : FieldDescriptor::LABEL_OPTIONAL, \ + FieldDescriptor::LABEL_##LABEL); \ GOOGLE_DCHECK_EQ(cpp_type((EXTENSION).type), FieldDescriptor::CPPTYPE_##CPPTYPE) const MessageLite& ExtensionSet::GetMessage(int number, @@ -239,7 +241,8 @@ MessageLite* ExtensionSet::UnsafeArenaReleaseMessage( } } -ExtensionSet::Extension* ExtensionSet::MaybeNewRepeatedExtension(const FieldDescriptor* descriptor) { +ExtensionSet::Extension* ExtensionSet::MaybeNewRepeatedExtension( + const FieldDescriptor* descriptor) { Extension* extension; if (MaybeNewExtension(descriptor->number(), descriptor, &extension)) { extension->type = descriptor->type(); @@ -285,8 +288,8 @@ void ExtensionSet::AddAllocatedMessage(const FieldDescriptor* descriptor, } static bool ValidateEnumUsingDescriptor(const void* arg, int number) { - return reinterpret_cast(arg) - ->FindValueByNumber(number) != NULL; + return reinterpret_cast(arg)->FindValueByNumber( + number) != NULL; } bool DescriptorPoolExtensionFinder::Find(int number, ExtensionInfo* output) { @@ -300,9 +303,9 @@ bool DescriptorPoolExtensionFinder::Find(int number, ExtensionInfo* output) { output->is_packed = extension->options().packed(); output->descriptor = extension; if (extension->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - output->message_prototype = + output->message_info.prototype = factory_->GetPrototype(extension->message_type()); - GOOGLE_CHECK(output->message_prototype != NULL) + GOOGLE_CHECK(output->message_info.prototype != nullptr) << "Extension factory's GetPrototype() returned NULL for extension: " << extension->full_name(); } else if (extension->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) { @@ -314,233 +317,59 @@ bool DescriptorPoolExtensionFinder::Find(int number, ExtensionInfo* output) { } } + #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -bool ExtensionSet::FindExtension(uint32 tag, const Message* containing_type, +bool ExtensionSet::FindExtension(int wire_type, uint32 field, + const Message* containing_type, const internal::ParseContext* ctx, - ExtensionInfo* extension, int* number, + ExtensionInfo* extension, bool* was_packed_on_wire) { - if (ctx->extra_parse_data().pool == nullptr) { + if (ctx->data().pool == nullptr) { GeneratedExtensionFinder finder(containing_type); - if (!FindExtensionInfoFromTag(tag, &finder, number, extension, - was_packed_on_wire)) { + if (!FindExtensionInfoFromFieldNumber(wire_type, field, &finder, extension, + was_packed_on_wire)) { return false; } } else { - DescriptorPoolExtensionFinder finder(ctx->extra_parse_data().pool, - ctx->extra_parse_data().factory, + DescriptorPoolExtensionFinder finder(ctx->data().pool, ctx->data().factory, containing_type->GetDescriptor()); - if (!FindExtensionInfoFromTag(tag, &finder, number, extension, - was_packed_on_wire)) { + if (!FindExtensionInfoFromFieldNumber(wire_type, field, &finder, extension, + was_packed_on_wire)) { return false; } } return true; } -std::pair ExtensionSet::ParseField( - uint32 tag, ParseClosure parent, const char* begin, const char* end, - const Message* containing_type, +const char* ExtensionSet::ParseField( + uint64 tag, const char* ptr, const Message* containing_type, internal::InternalMetadataWithArena* metadata, internal::ParseContext* ctx) { - int number; + int number = tag >> 3; bool was_packed_on_wire; ExtensionInfo extension; - if (!FindExtension(tag, containing_type, ctx, &extension, &number, + if (!FindExtension(tag & 7, number, containing_type, ctx, &extension, &was_packed_on_wire)) { - return UnknownFieldParse(tag, parent, begin, end, - metadata->mutable_unknown_fields(), ctx); + return UnknownFieldParse(tag, metadata->mutable_unknown_fields(), ptr, ctx); } - auto ptr = begin; - ParseClosure child; - int depth; - if (was_packed_on_wire) { - switch (extension.type) { -#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - child = { \ - internal::Packed##CPP_CAMELCASE##Parser, \ - MutableRawRepeatedField(number, extension.type, extension.is_packed, \ - extension.descriptor)}; \ - goto length_delim - 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: - ctx->extra_parse_data().SetEnumValidatorArg( - extension.enum_validity_check.func, - extension.enum_validity_check.arg, - metadata->mutable_unknown_fields(), tag >> 3); - child = { - internal::PackedValidEnumParserArg, - MutableRawRepeatedField(number, extension.type, extension.is_packed, - extension.descriptor)}; - goto length_delim; - 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 = Varint::Parse64(ptr, &value); \ - if (ptr == nullptr) goto error; \ - 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); -#undef HANDLE_VARINT_TYPE -#define HANDLE_SVARINT_TYPE(UPPERCASE, CPP_CAMELCASE, SIZE) \ - case WireFormatLite::TYPE_##UPPERCASE: { \ - uint64 val; \ - ptr = Varint::Parse64(ptr, &val); \ - 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: { \ - CPPTYPE value; \ - std::memcpy(&value, ptr, sizeof(CPPTYPE)); \ - 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); - HANDLE_FIXED_TYPE(BOOL, Bool, bool); -#undef HANDLE_FIXED_TYPE - - case WireFormatLite::TYPE_ENUM: { - uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (ptr == nullptr) goto error; - 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: { - string* value = extension.is_repeated - ? AddString(number, WireFormatLite::TYPE_STRING, - extension.descriptor) - : MutableString(number, WireFormatLite::TYPE_STRING, - extension.descriptor); - child = {StringParser, value}; - goto length_delim; - } - - case WireFormatLite::TYPE_GROUP: { - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_prototype, extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_prototype, - extension.descriptor); - child = {value->_ParseFunc(), value}; - if (!ctx->PrepareGroup(tag, &depth)) goto error; - ptr = child(ptr, end, ctx); - if (!ptr) goto error; - if (ctx->GroupContinues(depth)) goto group_continues; - break; - } - - case WireFormatLite::TYPE_MESSAGE: { - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_prototype, extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_prototype, - extension.descriptor); - child = {value->_ParseFunc(), value}; - goto length_delim; - } - } - } - - return std::make_pair(ptr, false); - -error: - return std::make_pair(nullptr, true); + return ParseFieldWithExtensionInfo(number, was_packed_on_wire, extension, + metadata, ptr, ctx); +} -length_delim: - uint32 size; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - if (size > end - ptr) goto len_delim_till_end; - { - auto newend = ptr + size; - if (!ctx->ParseExactRange(child, ptr, newend)) { - goto error; - } - ptr = newend; - } - return std::make_pair(ptr, false); -len_delim_till_end: - return std::make_pair(ctx->StoreAndTailCall(ptr, end, parent, child, size), - true); +const char* ExtensionSet::ParseFieldMaybeLazily( + uint64 tag, const char* ptr, const Message* containing_type, + internal::InternalMetadataWithArena* metadata, + internal::ParseContext* ctx) { + return ParseField(tag, ptr, containing_type, metadata, ctx); +} -group_continues: - ctx->StoreGroup(parent, child, depth); - return std::make_pair(ptr, true); +const char* ExtensionSet::ParseMessageSetItem( + const char* ptr, const Message* containing_type, + internal::InternalMetadataWithArena* metadata, + internal::ParseContext* ctx) { + return ParseMessageSetItemTmpl(ptr, containing_type, metadata, ctx); } + #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input, @@ -558,88 +387,6 @@ bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input, } } -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* ExtensionSet::ParseMessageSetItem( - ParseClosure parent, const char* begin, const char* end, - const Message* containing_type, - internal::InternalMetadataWithArena* metadata, - internal::ParseContext* ctx) { - auto ptr = begin; - while (ptr < end) { - uint32 tag = *ptr++; - if (tag == WireFormatLite::kMessageSetTypeIdTag) { - uint32 type_id; - ptr = Varint::Parse32(ptr, &type_id); - if (!ptr) goto error; - - if (ctx->extra_parse_data().payload.empty()) { - tag = *ptr++; - if (tag == WireFormatLite::kMessageSetMessageTag) { - auto res = ParseField(type_id * 8 + 2, parent, ptr, end, - containing_type, metadata, ctx); - ptr = res.first; - if (res.second) break; - } else { - goto error; - } - } else { - ExtensionInfo extension; - GeneratedExtensionFinder finder(containing_type); - int number; - bool was_packed_on_wire; - if (!FindExtension(type_id * 8 + 2, containing_type, ctx, &extension, - &number, &was_packed_on_wire)) { - metadata->mutable_unknown_fields()->AddLengthDelimited( - type_id, ctx->extra_parse_data().payload); - continue; - } - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_prototype, extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_prototype, - extension.descriptor); - ParseClosure parser = {value->_ParseFunc(), value}; - StringPiece chunk(ctx->extra_parse_data().payload.data()); - if (!ctx->ParseExactRange(parser, chunk.begin(), chunk.end())) { - return nullptr; - } - } - } else if (tag == WireFormatLite::kMessageSetItemEndTag) { - if (!ctx->ValidEndGroup(tag)) goto error; - break; - } else if (tag == WireFormatLite::kMessageSetMessageTag) { - uint32 size; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ParseClosure child = {internal::StringParser, - &ctx->extra_parse_data().payload}; - if (size > end - ptr) { - return ctx->StoreAndTailCall(ptr, end, parent, child, size); - } else { - auto newend = ptr + size; - if (!ctx->ParseExactRange(child, ptr, newend)) { - goto error; - } - ptr = newend; - } - } else { - ptr--; - ptr = Varint::Parse32(ptr, &tag); - if (ptr == nullptr) goto error; - auto res = - ParseField(tag, parent, ptr, end, containing_type, metadata, ctx); - ptr = res.first; - if (res.second) break; - } - } - return ptr; -error: - return nullptr; -} -#endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input, const Message* containing_type, UnknownFieldSet* unknown_fields) { @@ -682,15 +429,15 @@ size_t ExtensionSet::Extension::SpaceUsedExcludingSelfLong() const { repeated_##LOWERCASE##_value->SpaceUsedExcludingSelfLong(); \ 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(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); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: @@ -725,217 +472,21 @@ size_t ExtensionSet::Extension::SpaceUsedExcludingSelfLong() const { return total_size; } -// The Serialize*ToArray methods are only needed in the heavy library, as -// the lite library only generates SerializeWithCachedSizes. -uint8* ExtensionSet::SerializeWithCachedSizesToArray(int start_field_number, - int end_field_number, - uint8* target) const { - return InternalSerializeWithCachedSizesToArray( - start_field_number, end_field_number, - io::CodedOutputStream::IsDefaultSerializationDeterministic(), target); -} - uint8* ExtensionSet::SerializeMessageSetWithCachedSizesToArray( uint8* target) const { - return InternalSerializeMessageSetWithCachedSizesToArray( - io::CodedOutputStream::IsDefaultSerializationDeterministic(), target); -} - -uint8* ExtensionSet::InternalSerializeWithCachedSizesToArray( - int start_field_number, int end_field_number, bool deterministic, - uint8* target) const { - if (GOOGLE_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, deterministic, target); - } - 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, deterministic, target); - } - return target; -} - -uint8* ExtensionSet::InternalSerializeMessageSetWithCachedSizesToArray( - bool deterministic, uint8* target) const { - ForEach([deterministic, &target](int number, const Extension& ext) { - target = ext.InternalSerializeMessageSetItemWithCachedSizesToArray( - number, deterministic, target); - }); - return target; + io::EpsCopyOutputStream stream( + target, MessageSetByteSize(), + io::CodedOutputStream::IsDefaultSerializationDeterministic()); + return InternalSerializeMessageSetWithCachedSizesToArray(target, &stream); } -uint8* ExtensionSet::Extension::InternalSerializeFieldWithCachedSizesToArray( - int number, bool deterministic, uint8* target) const { - if (is_repeated) { - if (is_packed) { - if (cached_size == 0) return 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 FieldDescriptor::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - 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 FieldDescriptor::TYPE_STRING: - case FieldDescriptor::TYPE_BYTES: - case FieldDescriptor::TYPE_GROUP: - case FieldDescriptor::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 FieldDescriptor::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - 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( STRING, String, string); - HANDLE_TYPE( BYTES, Bytes, string); - HANDLE_TYPE( ENUM, Enum, enum); -#undef HANDLE_TYPE -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case FieldDescriptor::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - target = WireFormatLite::InternalWrite##CAMELCASE##ToArray( \ - number, repeated_##LOWERCASE##_value->Get(i), \ - deterministic, target); \ - } \ - 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 FieldDescriptor::TYPE_##UPPERCASE: \ - 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( 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 FieldDescriptor::TYPE_MESSAGE: - if (is_lazy) { - target = lazymessage_value->InternalWriteMessageToArray( - number, deterministic, target); - } else { - target = WireFormatLite::InternalWriteMessageToArray( - number, *message_value, deterministic, target); - } - break; - } - } - return target; -} - -uint8* -ExtensionSet::Extension::InternalSerializeMessageSetItemWithCachedSizesToArray( - int number, bool deterministic, uint8* target) 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, deterministic, - target); - } - - if (is_cleared) return 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->InternalWriteMessageToArray( - WireFormatLite::kMessageSetMessageNumber, deterministic, target); - } else { - target = WireFormatLite::InternalWriteMessageToArray( - WireFormatLite::kMessageSetMessageNumber, *message_value, deterministic, - target); - } - // End group. - target = io::CodedOutputStream::WriteTagToArray( - WireFormatLite::kMessageSetItemEndTag, target); - return target; -} - - bool ExtensionSet::ParseFieldMaybeLazily( int wire_type, int field_number, io::CodedInputStream* input, - ExtensionFinder* extension_finder, - MessageSetFieldSkipper* field_skipper) { - return ParseField(WireFormatLite::MakeTag( - field_number, static_cast(wire_type)), - input, extension_finder, field_skipper); + ExtensionFinder* extension_finder, MessageSetFieldSkipper* field_skipper) { + return ParseField( + WireFormatLite::MakeTag(field_number, + static_cast(wire_type)), + input, extension_finder, field_skipper); } bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input, @@ -983,82 +534,6 @@ bool ExtensionSet::ParseMessageSetItem(io::CodedInputStream* input, MSFull{this, extension_finder, field_skipper}); } -void ExtensionSet::Extension::SerializeMessageSetItemWithCachedSizes( - int number, - io::CodedOutputStream* output) const { - if (type != WireFormatLite::TYPE_MESSAGE || is_repeated) { - // Not a valid MessageSet extension, but serialize it the normal way. - SerializeFieldWithCachedSizes(number, output); - return; - } - - if (is_cleared) return; - - // Start group. - output->WriteTag(WireFormatLite::kMessageSetItemStartTag); - - // Write type ID. - WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber, - number, - output); - // Write message. - if (is_lazy) { - lazymessage_value->WriteMessage( - WireFormatLite::kMessageSetMessageNumber, output); - } else { - WireFormatLite::WriteMessageMaybeToArray( - WireFormatLite::kMessageSetMessageNumber, - *message_value, - output); - } - - // End group. - output->WriteTag(WireFormatLite::kMessageSetItemEndTag); -} - -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; -} - -void ExtensionSet::SerializeMessageSetWithCachedSizes( - io::CodedOutputStream* output) const { - ForEach([output](int number, const Extension& ext) { - ext.SerializeMessageSetItemWithCachedSizes(number, output); - }); -} - -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/src/google/protobuf/extension_set_inl.h b/src/google/protobuf/extension_set_inl.h new file mode 100644 index 0000000000..9e15a17c4a --- /dev/null +++ b/src/google/protobuf/extension_set_inl.h @@ -0,0 +1,283 @@ +// 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 + +namespace google { +namespace protobuf { +namespace internal { + +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER +template +const char* ExtensionSet::ParseFieldWithExtensionInfo( + int number, bool was_packed_on_wire, const ExtensionInfo& extension, + T* 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 = ParseVarint64(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); +#undef HANDLE_VARINT_TYPE +#define HANDLE_SVARINT_TYPE(UPPERCASE, CPP_CAMELCASE, SIZE) \ + case WireFormatLite::TYPE_##UPPERCASE: { \ + uint64 val; \ + ptr = ParseVarint64(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: { \ + CPPTYPE value; \ + std::memcpy(&value, ptr, sizeof(CPPTYPE)); \ + 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); + HANDLE_FIXED_TYPE(BOOL, Bool, bool); +#undef HANDLE_FIXED_TYPE + + case WireFormatLite::TYPE_ENUM: { + uint64 val; + ptr = ParseVarint64(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, + Metadata* metadata, + internal::ParseContext* ctx) { + std::string payload; + uint32 type_id = 0; + while (!ctx->Done(&ptr)) { + uint32 tag = static_cast(*ptr++); + if (tag == WireFormatLite::kMessageSetTypeIdTag) { + uint64 tmp; + ptr = ParseVarint64Inline(ptr, &tmp); + GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + type_id = tmp; + if (!payload.empty()) { + 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); + } + } else { + if (tag >= 128) { + // Parse remainder of tag varint + uint32 tmp; + ptr = VarintParse<4>(ptr, &tmp); + GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + tag += (tmp - 1) << 7; + } + 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; +} +#endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + +} // namespace internal +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_EXTENSION_SET_INL_H__ diff --git a/src/google/protobuf/extension_set_unittest.cc b/src/google/protobuf/extension_set_unittest.cc index f3346c8827..6dd6f7d8f5 100644 --- a/src/google/protobuf/extension_set_unittest.cc +++ b/src/google/protobuf/extension_set_unittest.cc @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -59,6 +60,8 @@ namespace protobuf { namespace internal { namespace { +using TestUtil::EqualsToSerialized; + // This test closely mirrors net/proto2/compiler/cpp/internal/unittest.cc // except that it uses extensions rather than regular fields. @@ -75,9 +78,9 @@ TEST(ExtensionSetTest, Defaults) { &message.GetExtension(unittest::optionalgroup_extension)); EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(), &message.GetExtension(unittest::optional_nested_message_extension)); - EXPECT_EQ(&unittest::ForeignMessage::default_instance(), - &message.GetExtension( - unittest::optional_foreign_message_extension)); + EXPECT_EQ( + &unittest::ForeignMessage::default_instance(), + &message.GetExtension(unittest::optional_foreign_message_extension)); EXPECT_EQ(&unittest_import::ImportMessage::default_instance(), &message.GetExtension(unittest::optional_import_message_extension)); } @@ -111,9 +114,9 @@ TEST(ExtensionSetTest, Clear) { &message.GetExtension(unittest::optionalgroup_extension)); EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(), &message.GetExtension(unittest::optional_nested_message_extension)); - EXPECT_NE(&unittest::ForeignMessage::default_instance(), - &message.GetExtension( - unittest::optional_foreign_message_extension)); + EXPECT_NE( + &unittest::ForeignMessage::default_instance(), + &message.GetExtension(unittest::optional_foreign_message_extension)); EXPECT_NE(&unittest_import::ImportMessage::default_instance(), &message.GetExtension(unittest::optional_import_message_extension)); @@ -130,7 +133,7 @@ TEST(ExtensionSetTest, ClearOneField) { TestUtil::SetAllExtensions(&message); int64 original_value = - message.GetExtension(unittest::optional_int64_extension); + message.GetExtension(unittest::optional_int64_extension); // Clear the field and make sure it shows up as cleared. message.ClearExtension(unittest::optional_int64_extension); @@ -148,20 +151,18 @@ TEST(ExtensionSetTest, ClearOneField) { TEST(ExtensionSetTest, SetAllocatedExtension) { unittest::TestAllExtensions message; - EXPECT_FALSE(message.HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_message_extension)); // Add a extension using SetAllocatedExtension unittest::ForeignMessage* foreign_message = new unittest::ForeignMessage(); message.SetAllocatedExtension(unittest::optional_foreign_message_extension, foreign_message); - EXPECT_TRUE(message.HasExtension( - unittest::optional_foreign_message_extension)); - EXPECT_EQ(foreign_message, - message.MutableExtension( - unittest::optional_foreign_message_extension)); - EXPECT_EQ(foreign_message, - &message.GetExtension( - unittest::optional_foreign_message_extension)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_foreign_message_extension)); + EXPECT_EQ(foreign_message, message.MutableExtension( + unittest::optional_foreign_message_extension)); + EXPECT_EQ(foreign_message, &message.GetExtension( + unittest::optional_foreign_message_extension)); // SetAllocatedExtension should delete the previously existing extension. // (We reply on unittest to check memory leaks for this case) @@ -171,8 +172,8 @@ TEST(ExtensionSetTest, SetAllocatedExtension) { // SetAllocatedExtension with a NULL parameter is equivalent to ClearExtenion. message.SetAllocatedExtension(unittest::optional_foreign_message_extension, NULL); - EXPECT_FALSE(message.HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_message_extension)); } TEST(ExtensionSetTest, ReleaseExtension) { @@ -183,8 +184,7 @@ TEST(ExtensionSetTest, ReleaseExtension) { unittest::TestMessageSetExtension1* extension = new unittest::TestMessageSetExtension1(); message.SetAllocatedExtension( - unittest::TestMessageSetExtension1::message_set_extension, - extension); + unittest::TestMessageSetExtension1::message_set_extension, extension); EXPECT_TRUE(message.HasExtension( unittest::TestMessageSetExtension1::message_set_extension)); // Release the extension using ReleaseExtension @@ -197,8 +197,7 @@ TEST(ExtensionSetTest, ReleaseExtension) { // ReleaseExtension will return the underlying object even after // ClearExtension is called. message.SetAllocatedExtension( - unittest::TestMessageSetExtension1::message_set_extension, - extension); + unittest::TestMessageSetExtension1::message_set_extension, extension); message.ClearExtension( unittest::TestMessageSetExtension1::message_set_extension); released_extension = message.ReleaseExtension( @@ -213,8 +212,7 @@ TEST(ExtensionSetTest, ArenaUnsafeArenaSetAllocatedAndRelease) { Arena::CreateMessage(&arena); unittest::ForeignMessage extension; message->UnsafeArenaSetAllocatedExtension( - unittest::optional_foreign_message_extension, - &extension); + unittest::optional_foreign_message_extension, &extension); // No copy when set. unittest::ForeignMessage* mutable_extension = message->MutableExtension(unittest::optional_foreign_message_extension); @@ -224,21 +222,19 @@ TEST(ExtensionSetTest, ArenaUnsafeArenaSetAllocatedAndRelease) { message->UnsafeArenaReleaseExtension( unittest::optional_foreign_message_extension); EXPECT_EQ(&extension, released_extension); - EXPECT_FALSE(message->HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message->HasExtension(unittest::optional_foreign_message_extension)); // Set the ownership back and let the destructors run. It should not take // ownership, so this should not crash. message->UnsafeArenaSetAllocatedExtension( - unittest::optional_foreign_message_extension, - &extension); + unittest::optional_foreign_message_extension, &extension); } TEST(ExtensionSetTest, UnsafeArenaSetAllocatedAndRelease) { unittest::TestAllExtensions message; unittest::ForeignMessage* extension = new unittest::ForeignMessage(); message.UnsafeArenaSetAllocatedExtension( - unittest::optional_foreign_message_extension, - extension); + unittest::optional_foreign_message_extension, extension); // No copy when set. unittest::ForeignMessage* mutable_extension = message.MutableExtension(unittest::optional_foreign_message_extension); @@ -248,13 +244,12 @@ TEST(ExtensionSetTest, UnsafeArenaSetAllocatedAndRelease) { message.UnsafeArenaReleaseExtension( unittest::optional_foreign_message_extension); EXPECT_EQ(extension, released_extension); - EXPECT_FALSE(message.HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_message_extension)); // Set the ownership back and let the destructors run. It should take // ownership, so this should not leak. message.UnsafeArenaSetAllocatedExtension( - unittest::optional_foreign_message_extension, - extension); + unittest::optional_foreign_message_extension, extension); } TEST(ExtensionSetTest, ArenaUnsafeArenaReleaseOfHeapAlloc) { @@ -262,17 +257,16 @@ TEST(ExtensionSetTest, ArenaUnsafeArenaReleaseOfHeapAlloc) { unittest::TestAllExtensions* message = Arena::CreateMessage(&arena); unittest::ForeignMessage* extension = new unittest::ForeignMessage; - message->SetAllocatedExtension( - unittest::optional_foreign_message_extension, - extension); + message->SetAllocatedExtension(unittest::optional_foreign_message_extension, + extension); // The arena should maintain ownership of the heap allocated proto because we // used UnsafeArenaReleaseExtension. The leak checker will ensure this. unittest::ForeignMessage* released_extension = message->UnsafeArenaReleaseExtension( unittest::optional_foreign_message_extension); EXPECT_EQ(extension, released_extension); - EXPECT_FALSE(message->HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message->HasExtension(unittest::optional_foreign_message_extension)); } @@ -342,10 +336,8 @@ TEST(ExtensionSetTest, SwapExtension) { TestUtil::ExpectExtensionsClear(message2); // Swap two extensions. - fields.push_back( - reflection->FindKnownExtensionByNumber(12)); - fields.push_back( - reflection->FindKnownExtensionByNumber(25)); + fields.push_back(reflection->FindKnownExtensionByNumber(12)); + fields.push_back(reflection->FindKnownExtensionByNumber(25)); reflection->SwapFields(&message1, &message2, fields); EXPECT_TRUE(message1.HasExtension(unittest::optional_int32_extension)); @@ -424,23 +416,22 @@ TEST(ExtensionSetTest, ArenaSetAllocatedMessageAndRelease) { Arena arena; unittest::TestAllExtensions* message = Arena::CreateMessage(&arena); - EXPECT_FALSE(message->HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message->HasExtension(unittest::optional_foreign_message_extension)); // Add a extension using SetAllocatedExtension unittest::ForeignMessage* foreign_message = new unittest::ForeignMessage(); message->SetAllocatedExtension(unittest::optional_foreign_message_extension, foreign_message); // foreign_message is now owned by the arena. - EXPECT_EQ(foreign_message, - message->MutableExtension( - unittest::optional_foreign_message_extension)); + EXPECT_EQ(foreign_message, message->MutableExtension( + unittest::optional_foreign_message_extension)); // Underlying message is copied, and returned. - unittest::ForeignMessage* released_message = message->ReleaseExtension( - unittest::optional_foreign_message_extension); + unittest::ForeignMessage* released_message = + message->ReleaseExtension(unittest::optional_foreign_message_extension); delete released_message; - EXPECT_FALSE(message->HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message->HasExtension(unittest::optional_foreign_message_extension)); } TEST(ExtensionSetTest, SwapExtensionBothFullWithArena) { @@ -525,7 +516,7 @@ TEST(ExtensionSetTest, SerializationToArray) { unittest::TestAllTypes destination; TestUtil::SetAllExtensions(&source); int size = source.ByteSize(); - string data; + std::string data; data.resize(size); uint8* target = reinterpret_cast(::google::protobuf::string_as_array(&data)); uint8* end = source.SerializeWithCachedSizesToArray(target); @@ -546,7 +537,7 @@ TEST(ExtensionSetTest, SerializationToStream) { unittest::TestAllTypes destination; TestUtil::SetAllExtensions(&source); int size = source.ByteSize(); - string data; + std::string data; data.resize(size); { io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); @@ -569,7 +560,7 @@ TEST(ExtensionSetTest, PackedSerializationToArray) { unittest::TestPackedTypes destination; TestUtil::SetPackedExtensions(&source); int size = source.ByteSize(); - string data; + std::string data; data.resize(size); uint8* target = reinterpret_cast(::google::protobuf::string_as_array(&data)); uint8* end = source.SerializeWithCachedSizesToArray(target); @@ -590,7 +581,7 @@ TEST(ExtensionSetTest, PackedSerializationToStream) { unittest::TestPackedTypes destination; TestUtil::SetPackedExtensions(&source); int size = source.ByteSize(); - string data; + std::string data; data.resize(size); { io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); @@ -606,27 +597,33 @@ TEST(ExtensionSetTest, NestedExtensionGroup) { // Serialize as TestGroup and parse as TestGroupExtension. unittest::TestGroup source; unittest::TestGroupExtension destination; - string data; + std::string data; source.mutable_optionalgroup()->set_a(117); source.set_optional_foreign_enum(unittest::FOREIGN_BAZ); source.SerializeToString(&data); EXPECT_TRUE(destination.ParseFromString(data)); - EXPECT_TRUE(destination.GetExtension( - unittest::TestNestedExtension::optionalgroup_extension).has_a()); - EXPECT_EQ(117, destination.GetExtension( - unittest::TestNestedExtension::optionalgroup_extension).a()); + EXPECT_TRUE( + destination + .GetExtension(unittest::TestNestedExtension::optionalgroup_extension) + .has_a()); + EXPECT_EQ(117, destination + .GetExtension( + unittest::TestNestedExtension::optionalgroup_extension) + .a()); EXPECT_TRUE(destination.HasExtension( unittest::TestNestedExtension::optional_foreign_enum_extension)); - EXPECT_EQ(unittest::FOREIGN_BAZ, destination.GetExtension( - unittest::TestNestedExtension::optional_foreign_enum_extension)); + EXPECT_EQ( + unittest::FOREIGN_BAZ, + destination.GetExtension( + unittest::TestNestedExtension::optional_foreign_enum_extension)); } TEST(ExtensionSetTest, Parsing) { // Serialize as TestAllTypes and parse as TestAllExtensions. unittest::TestAllTypes source; unittest::TestAllExtensions destination; - string data; + std::string data; TestUtil::SetAllFields(&source); source.SerializeToString(&data); @@ -639,7 +636,7 @@ TEST(ExtensionSetTest, PackedParsing) { // Serialize as TestPackedTypes and parse as TestPackedExtensions. unittest::TestPackedTypes source; unittest::TestPackedExtensions destination; - string data; + std::string data; TestUtil::SetPackedFields(&source); source.SerializeToString(&data); @@ -650,7 +647,7 @@ TEST(ExtensionSetTest, PackedParsing) { TEST(ExtensionSetTest, PackedToUnpackedParsing) { unittest::TestPackedTypes source; unittest::TestUnpackedExtensions destination; - string data; + std::string data; TestUtil::SetPackedFields(&source); source.SerializeToString(&data); @@ -660,7 +657,10 @@ TEST(ExtensionSetTest, PackedToUnpackedParsing) { // Reserialize unittest::TestUnpackedTypes unpacked; TestUtil::SetUnpackedFields(&unpacked); - EXPECT_TRUE(unpacked.SerializeAsString() == destination.SerializeAsString()); + // Serialized proto has to be the same size and parsed to the same message. + EXPECT_EQ(unpacked.SerializeAsString().size(), + destination.SerializeAsString().size()); + EXPECT_TRUE(EqualsToSerialized(unpacked, destination.SerializeAsString())); // Make sure we can add extensions. destination.AddExtension(unittest::unpacked_int32_extension, 1); @@ -671,7 +671,7 @@ TEST(ExtensionSetTest, PackedToUnpackedParsing) { TEST(ExtensionSetTest, UnpackedToPackedParsing) { unittest::TestUnpackedTypes source; unittest::TestPackedExtensions destination; - string data; + std::string data; TestUtil::SetUnpackedFields(&source); source.SerializeToString(&data); @@ -681,7 +681,10 @@ TEST(ExtensionSetTest, UnpackedToPackedParsing) { // Reserialize unittest::TestPackedTypes packed; TestUtil::SetPackedFields(&packed); - EXPECT_TRUE(packed.SerializeAsString() == destination.SerializeAsString()); + // Serialized proto has to be the same size and parsed to the same message. + EXPECT_EQ(packed.SerializeAsString().size(), + destination.SerializeAsString().size()); + EXPECT_TRUE(EqualsToSerialized(packed, destination.SerializeAsString())); // Make sure we can add extensions. destination.AddExtension(unittest::packed_int32_extension, 1); @@ -734,36 +737,38 @@ TEST(ExtensionSetTest, MutableString) { TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { // Scalar primitive extensions should increase the extension set size by a // minimum of the size of the primitive type. -#define TEST_SCALAR_EXTENSIONS_SPACE_USED(type, value) \ - do { \ - unittest::TestAllExtensions message; \ - const int base_size = message.SpaceUsed(); \ - message.SetExtension(unittest::optional_##type##_extension, value); \ - int min_expected_size = base_size + \ - sizeof(message.GetExtension(unittest::optional_##type##_extension)); \ - EXPECT_LE(min_expected_size, message.SpaceUsed()); \ +#define TEST_SCALAR_EXTENSIONS_SPACE_USED(type, value) \ + do { \ + unittest::TestAllExtensions message; \ + const int base_size = message.SpaceUsed(); \ + message.SetExtension(unittest::optional_##type##_extension, value); \ + int min_expected_size = \ + base_size + \ + sizeof(message.GetExtension(unittest::optional_##type##_extension)); \ + EXPECT_LE(min_expected_size, message.SpaceUsed()); \ } while (0) - TEST_SCALAR_EXTENSIONS_SPACE_USED(int32 , 101); - TEST_SCALAR_EXTENSIONS_SPACE_USED(int64 , 102); - TEST_SCALAR_EXTENSIONS_SPACE_USED(uint32 , 103); - TEST_SCALAR_EXTENSIONS_SPACE_USED(uint64 , 104); - TEST_SCALAR_EXTENSIONS_SPACE_USED(sint32 , 105); - TEST_SCALAR_EXTENSIONS_SPACE_USED(sint64 , 106); - TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed32 , 107); - TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed64 , 108); + TEST_SCALAR_EXTENSIONS_SPACE_USED(int32, 101); + TEST_SCALAR_EXTENSIONS_SPACE_USED(int64, 102); + TEST_SCALAR_EXTENSIONS_SPACE_USED(uint32, 103); + TEST_SCALAR_EXTENSIONS_SPACE_USED(uint64, 104); + TEST_SCALAR_EXTENSIONS_SPACE_USED(sint32, 105); + TEST_SCALAR_EXTENSIONS_SPACE_USED(sint64, 106); + TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed32, 107); + TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed64, 108); TEST_SCALAR_EXTENSIONS_SPACE_USED(sfixed32, 109); TEST_SCALAR_EXTENSIONS_SPACE_USED(sfixed64, 110); - TEST_SCALAR_EXTENSIONS_SPACE_USED(float , 111); - TEST_SCALAR_EXTENSIONS_SPACE_USED(double , 112); - TEST_SCALAR_EXTENSIONS_SPACE_USED(bool , true); + TEST_SCALAR_EXTENSIONS_SPACE_USED(float, 111); + TEST_SCALAR_EXTENSIONS_SPACE_USED(double, 112); + TEST_SCALAR_EXTENSIONS_SPACE_USED(bool, true); #undef TEST_SCALAR_EXTENSIONS_SPACE_USED { unittest::TestAllExtensions message; const int base_size = message.SpaceUsed(); message.SetExtension(unittest::optional_nested_enum_extension, unittest::TestAllTypes::FOO); - int min_expected_size = base_size + + int min_expected_size = + base_size + sizeof(message.GetExtension(unittest::optional_nested_enum_extension)); EXPECT_LE(min_expected_size, message.SpaceUsed()); } @@ -772,8 +777,9 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { // that gets included as well. unittest::TestAllExtensions message; const int base_size = message.SpaceUsed(); - const string s("this is a fairly large string that will cause some " - "allocation in order to store it in the extension"); + const std::string s( + "this is a fairly large string that will cause some " + "allocation in order to store it in the extension"); message.SetExtension(unittest::optional_string_extension, s); int min_expected_size = base_size + s.length(); EXPECT_LE(min_expected_size, message.SpaceUsed()); @@ -784,8 +790,8 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { const int base_size = message.SpaceUsed(); unittest::ForeignMessage foreign; foreign.set_c(42); - message.MutableExtension(unittest::optional_foreign_message_extension)-> - CopyFrom(foreign); + message.MutableExtension(unittest::optional_foreign_message_extension) + ->CopyFrom(foreign); int min_expected_size = base_size + foreign.SpaceUsed(); EXPECT_LE(min_expected_size, message.SpaceUsed()); } @@ -800,45 +806,49 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { // SpaceUsed() // - Adds a large number of values (requiring allocation in the repeated // field), and ensures that that allocation is included in SpaceUsed() -#define TEST_REPEATED_EXTENSIONS_SPACE_USED(type, cpptype, value) \ - do { \ - unittest::TestAllExtensions message; \ - const int base_size = message.SpaceUsed(); \ - int min_expected_size = sizeof(RepeatedField) + base_size; \ - message.AddExtension(unittest::repeated_##type##_extension, value); \ - message.ClearExtension(unittest::repeated_##type##_extension); \ - const int empty_repeated_field_size = message.SpaceUsed(); \ - EXPECT_LE(min_expected_size, empty_repeated_field_size) << #type; \ - message.AddExtension(unittest::repeated_##type##_extension, value); \ - message.AddExtension(unittest::repeated_##type##_extension, value); \ - EXPECT_EQ(empty_repeated_field_size, message.SpaceUsed()) << #type; \ - message.ClearExtension(unittest::repeated_##type##_extension); \ - const int old_capacity = \ - message.GetRepeatedExtension(unittest::repeated_##type##_extension) \ - .Capacity(); \ - EXPECT_GE(old_capacity, kMinRepeatedFieldAllocationSize); \ - for (int i = 0; i < 16; ++i) { \ - message.AddExtension(unittest::repeated_##type##_extension, value); \ - } \ - int expected_size = sizeof(cpptype) * \ - (message.GetRepeatedExtension(unittest::repeated_##type##_extension) \ - .Capacity() - old_capacity) + empty_repeated_field_size; \ - EXPECT_LE(expected_size, message.SpaceUsed()) << #type; \ +#define TEST_REPEATED_EXTENSIONS_SPACE_USED(type, cpptype, value) \ + do { \ + unittest::TestAllExtensions message; \ + const int base_size = message.SpaceUsed(); \ + int min_expected_size = sizeof(RepeatedField) + base_size; \ + message.AddExtension(unittest::repeated_##type##_extension, value); \ + message.ClearExtension(unittest::repeated_##type##_extension); \ + const int empty_repeated_field_size = message.SpaceUsed(); \ + EXPECT_LE(min_expected_size, empty_repeated_field_size) << #type; \ + message.AddExtension(unittest::repeated_##type##_extension, value); \ + message.AddExtension(unittest::repeated_##type##_extension, value); \ + EXPECT_EQ(empty_repeated_field_size, message.SpaceUsed()) << #type; \ + message.ClearExtension(unittest::repeated_##type##_extension); \ + const int old_capacity = \ + message.GetRepeatedExtension(unittest::repeated_##type##_extension) \ + .Capacity(); \ + EXPECT_GE(old_capacity, kMinRepeatedFieldAllocationSize); \ + for (int i = 0; i < 16; ++i) { \ + message.AddExtension(unittest::repeated_##type##_extension, value); \ + } \ + int expected_size = \ + sizeof(cpptype) * \ + (message \ + .GetRepeatedExtension(unittest::repeated_##type##_extension) \ + .Capacity() - \ + old_capacity) + \ + empty_repeated_field_size; \ + EXPECT_LE(expected_size, message.SpaceUsed()) << #type; \ } while (0) - TEST_REPEATED_EXTENSIONS_SPACE_USED(int32 , int32 , 101); - TEST_REPEATED_EXTENSIONS_SPACE_USED(int64 , int64 , 102); - TEST_REPEATED_EXTENSIONS_SPACE_USED(uint32 , uint32, 103); - TEST_REPEATED_EXTENSIONS_SPACE_USED(uint64 , uint64, 104); - TEST_REPEATED_EXTENSIONS_SPACE_USED(sint32 , int32 , 105); - TEST_REPEATED_EXTENSIONS_SPACE_USED(sint64 , int64 , 106); - TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed32 , uint32, 107); - TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed64 , uint64, 108); - TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed32, int32 , 109); - TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed64, int64 , 110); - TEST_REPEATED_EXTENSIONS_SPACE_USED(float , float , 111); - TEST_REPEATED_EXTENSIONS_SPACE_USED(double , double, 112); - TEST_REPEATED_EXTENSIONS_SPACE_USED(bool , bool , true); + TEST_REPEATED_EXTENSIONS_SPACE_USED(int32, int32, 101); + TEST_REPEATED_EXTENSIONS_SPACE_USED(int64, int64, 102); + TEST_REPEATED_EXTENSIONS_SPACE_USED(uint32, uint32, 103); + TEST_REPEATED_EXTENSIONS_SPACE_USED(uint64, uint64, 104); + TEST_REPEATED_EXTENSIONS_SPACE_USED(sint32, int32, 105); + TEST_REPEATED_EXTENSIONS_SPACE_USED(sint64, int64, 106); + TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed32, uint32, 107); + TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed64, uint64, 108); + TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed32, int32, 109); + TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed64, int64, 110); + TEST_REPEATED_EXTENSIONS_SPACE_USED(float, float, 111); + TEST_REPEATED_EXTENSIONS_SPACE_USED(double, double, 112); + TEST_REPEATED_EXTENSIONS_SPACE_USED(bool, bool, true); TEST_REPEATED_EXTENSIONS_SPACE_USED(nested_enum, int, unittest::TestAllTypes::FOO); #undef TEST_REPEATED_EXTENSIONS_SPACE_USED @@ -846,29 +856,29 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { { unittest::TestAllExtensions message; const int base_size = message.SpaceUsed(); - int min_expected_size = sizeof(RepeatedPtrField) + base_size; - const string value(256, 'x'); + int min_expected_size = sizeof(RepeatedPtrField) + base_size; + const std::string value(256, 'x'); // Once items are allocated, they may stick around even when cleared so // without the hardcore memory management accessors there isn't a notion of // the empty repeated field memory usage as there is with primitive types. for (int i = 0; i < 16; ++i) { message.AddExtension(unittest::repeated_string_extension, value); } - min_expected_size += (sizeof(value) + value.size()) * - (16 - kMinRepeatedFieldAllocationSize); + min_expected_size += + (sizeof(value) + value.size()) * (16 - kMinRepeatedFieldAllocationSize); EXPECT_LE(min_expected_size, message.SpaceUsed()); } // Repeated messages { unittest::TestAllExtensions message; const int base_size = message.SpaceUsed(); - int min_expected_size = sizeof(RepeatedPtrField) + - base_size; + int min_expected_size = + sizeof(RepeatedPtrField) + base_size; unittest::ForeignMessage prototype; prototype.set_c(2); for (int i = 0; i < 16; ++i) { - message.AddExtension(unittest::repeated_foreign_message_extension)-> - CopyFrom(prototype); + message.AddExtension(unittest::repeated_foreign_message_extension) + ->CopyFrom(prototype); } min_expected_size += (16 - kMinRepeatedFieldAllocationSize) * prototype.SpaceUsed(); @@ -877,7 +887,7 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { } // N.B.: We do not test range-based for here because we remain C++03 compatible. -template +template inline T SumAllExtensions(const M& message, ID extension, T zero) { T sum = zero; typename RepeatedField::const_iterator iter = @@ -890,12 +900,11 @@ inline T SumAllExtensions(const M& message, ID extension, T zero) { return sum; } -template -inline void IncAllExtensions(M* message, ID extension, - T val) { +template +inline void IncAllExtensions(M* message, ID extension, T val) { typename RepeatedField::iterator iter = message->MutableRepeatedExtension(extension)->begin(); - typename RepeatedField::iterator end = + typename RepeatedField::iterator end = message->MutableRepeatedExtension(extension)->end(); for (; iter != end; ++iter) { *iter += val; @@ -906,14 +915,19 @@ TEST(ExtensionSetTest, RepeatedFields) { unittest::TestAllExtensions message; // Test empty repeated-field case (b/12926163) - ASSERT_EQ(0, message.GetRepeatedExtension( - unittest::repeated_int32_extension).size()); - ASSERT_EQ(0, message.GetRepeatedExtension( - unittest::repeated_nested_enum_extension).size()); - ASSERT_EQ(0, message.GetRepeatedExtension( - unittest::repeated_string_extension).size()); - ASSERT_EQ(0, message.GetRepeatedExtension( - unittest::repeated_nested_message_extension).size()); + ASSERT_EQ( + 0, + message.GetRepeatedExtension(unittest::repeated_int32_extension).size()); + ASSERT_EQ( + 0, message.GetRepeatedExtension(unittest::repeated_nested_enum_extension) + .size()); + ASSERT_EQ( + 0, + message.GetRepeatedExtension(unittest::repeated_string_extension).size()); + ASSERT_EQ( + 0, + message.GetRepeatedExtension(unittest::repeated_nested_message_extension) + .size()); unittest::TestAllTypes::NestedMessage nested_message; nested_message.set_bb(42); @@ -936,114 +950,113 @@ TEST(ExtensionSetTest, RepeatedFields) { message.AddExtension(unittest::repeated_bool_extension, true); message.AddExtension(unittest::repeated_nested_enum_extension, nested_enum); message.AddExtension(unittest::repeated_string_extension, - ::std::string("test")); + std::string("test")); message.AddExtension(unittest::repeated_bytes_extension, - ::std::string("test\xFF")); - message.AddExtension( - unittest::repeated_nested_message_extension)->CopyFrom(nested_message); - message.AddExtension(unittest::repeated_nested_enum_extension, - nested_enum); + std::string("test\xFF")); + message.AddExtension(unittest::repeated_nested_message_extension) + ->CopyFrom(nested_message); + message.AddExtension(unittest::repeated_nested_enum_extension, nested_enum); } - ASSERT_EQ(10, SumAllExtensions( - message, unittest::repeated_int32_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_int32_extension, 1); - ASSERT_EQ(20, SumAllExtensions( - message, unittest::repeated_int32_extension, 0)); + ASSERT_EQ(10, SumAllExtensions(message, + unittest::repeated_int32_extension, 0)); + IncAllExtensions(&message, unittest::repeated_int32_extension, 1); + ASSERT_EQ(20, SumAllExtensions(message, + unittest::repeated_int32_extension, 0)); - ASSERT_EQ(20, SumAllExtensions( - message, unittest::repeated_int64_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_int64_extension, 1); - ASSERT_EQ(30, SumAllExtensions( - message, unittest::repeated_int64_extension, 0)); + ASSERT_EQ(20, SumAllExtensions(message, + unittest::repeated_int64_extension, 0)); + IncAllExtensions(&message, unittest::repeated_int64_extension, 1); + ASSERT_EQ(30, SumAllExtensions(message, + unittest::repeated_int64_extension, 0)); ASSERT_EQ(30, SumAllExtensions( - message, unittest::repeated_uint32_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_uint32_extension, 1); + message, unittest::repeated_uint32_extension, 0)); + IncAllExtensions(&message, unittest::repeated_uint32_extension, 1); ASSERT_EQ(40, SumAllExtensions( - message, unittest::repeated_uint32_extension, 0)); + message, unittest::repeated_uint32_extension, 0)); ASSERT_EQ(40, SumAllExtensions( - message, unittest::repeated_uint64_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_uint64_extension, 1); + message, unittest::repeated_uint64_extension, 0)); + IncAllExtensions(&message, unittest::repeated_uint64_extension, 1); ASSERT_EQ(50, SumAllExtensions( - message, unittest::repeated_uint64_extension, 0)); + message, unittest::repeated_uint64_extension, 0)); ASSERT_EQ(50, SumAllExtensions( - message, unittest::repeated_sint32_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_sint32_extension, 1); + message, unittest::repeated_sint32_extension, 0)); + IncAllExtensions(&message, unittest::repeated_sint32_extension, 1); ASSERT_EQ(60, SumAllExtensions( - message, unittest::repeated_sint32_extension, 0)); + message, unittest::repeated_sint32_extension, 0)); ASSERT_EQ(60, SumAllExtensions( - message, unittest::repeated_sint64_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_sint64_extension, 1); + message, unittest::repeated_sint64_extension, 0)); + IncAllExtensions(&message, unittest::repeated_sint64_extension, 1); ASSERT_EQ(70, SumAllExtensions( - message, unittest::repeated_sint64_extension, 0)); + message, unittest::repeated_sint64_extension, 0)); ASSERT_EQ(70, SumAllExtensions( - message, unittest::repeated_fixed32_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_fixed32_extension, 1); + message, unittest::repeated_fixed32_extension, 0)); + IncAllExtensions(&message, unittest::repeated_fixed32_extension, 1); ASSERT_EQ(80, SumAllExtensions( - message, unittest::repeated_fixed32_extension, 0)); + message, unittest::repeated_fixed32_extension, 0)); ASSERT_EQ(80, SumAllExtensions( - message, unittest::repeated_fixed64_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_fixed64_extension, 1); + message, unittest::repeated_fixed64_extension, 0)); + IncAllExtensions(&message, unittest::repeated_fixed64_extension, 1); ASSERT_EQ(90, SumAllExtensions( - message, unittest::repeated_fixed64_extension, 0)); + message, unittest::repeated_fixed64_extension, 0)); // Usually, floating-point arithmetic cannot be trusted to be exact, so it is // a Bad Idea to assert equality in a test like this. However, we're dealing // with integers with a small number of significant mantissa bits, so we // should actually have exact precision here. - ASSERT_EQ(90, SumAllExtensions( - message, unittest::repeated_float_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_float_extension, 1); + ASSERT_EQ(90, SumAllExtensions(message, + unittest::repeated_float_extension, 0)); + IncAllExtensions(&message, unittest::repeated_float_extension, 1); ASSERT_EQ(100, SumAllExtensions( - message, unittest::repeated_float_extension, 0)); + message, unittest::repeated_float_extension, 0)); ASSERT_EQ(100, SumAllExtensions( - message, unittest::repeated_double_extension, 0)); - IncAllExtensions( - &message, unittest::repeated_double_extension, 1); + message, unittest::repeated_double_extension, 0)); + IncAllExtensions(&message, unittest::repeated_double_extension, 1); ASSERT_EQ(110, SumAllExtensions( - message, unittest::repeated_double_extension, 0)); - - RepeatedPtrField<::std::string>::iterator string_iter; - RepeatedPtrField<::std::string>::iterator string_end; - for (string_iter = message.MutableRepeatedExtension( - unittest::repeated_string_extension)->begin(), - string_end = message.MutableRepeatedExtension( - unittest::repeated_string_extension)->end(); + message, unittest::repeated_double_extension, 0)); + + RepeatedPtrField::iterator string_iter; + RepeatedPtrField::iterator string_end; + for (string_iter = + message + .MutableRepeatedExtension(unittest::repeated_string_extension) + ->begin(), + string_end = + message + .MutableRepeatedExtension(unittest::repeated_string_extension) + ->end(); string_iter != string_end; ++string_iter) { *string_iter += "test"; } - RepeatedPtrField<::std::string>::const_iterator string_const_iter; - RepeatedPtrField<::std::string>::const_iterator string_const_end; - for (string_const_iter = message.GetRepeatedExtension( - unittest::repeated_string_extension).begin(), - string_const_end = message.GetRepeatedExtension( - unittest::repeated_string_extension).end(); + RepeatedPtrField::const_iterator string_const_iter; + RepeatedPtrField::const_iterator string_const_end; + for (string_const_iter = + message.GetRepeatedExtension(unittest::repeated_string_extension) + .begin(), + string_const_end = + message.GetRepeatedExtension(unittest::repeated_string_extension) + .end(); string_iter != string_end; ++string_iter) { ASSERT_TRUE(*string_iter == "testtest"); } RepeatedField::iterator enum_iter; RepeatedField::iterator enum_end; - for (enum_iter = message.MutableRepeatedExtension( - unittest::repeated_nested_enum_extension)->begin(), - enum_end = message.MutableRepeatedExtension( - unittest::repeated_nested_enum_extension)->end(); + for (enum_iter = message + .MutableRepeatedExtension( + unittest::repeated_nested_enum_extension) + ->begin(), + enum_end = message + .MutableRepeatedExtension( + unittest::repeated_nested_enum_extension) + ->end(); enum_iter != enum_end; ++enum_iter) { *enum_iter = unittest::TestAllTypes::NestedEnum_MAX; } @@ -1051,33 +1064,43 @@ TEST(ExtensionSetTest, RepeatedFields) { enum_const_iter; RepeatedField::const_iterator enum_const_end; - for (enum_const_iter = message.GetRepeatedExtension( - unittest::repeated_nested_enum_extension).begin(), - enum_const_end = message.GetRepeatedExtension( - unittest::repeated_nested_enum_extension).end(); + for (enum_const_iter = + message + .GetRepeatedExtension(unittest::repeated_nested_enum_extension) + .begin(), + enum_const_end = + message + .GetRepeatedExtension(unittest::repeated_nested_enum_extension) + .end(); enum_const_iter != enum_const_end; ++enum_const_iter) { ASSERT_EQ(*enum_const_iter, unittest::TestAllTypes::NestedEnum_MAX); } - RepeatedPtrField::iterator - msg_iter; - RepeatedPtrField::iterator - msg_end; - for (msg_iter = message.MutableRepeatedExtension( - unittest::repeated_nested_message_extension)->begin(), - msg_end = message.MutableRepeatedExtension( - unittest::repeated_nested_message_extension)->end(); + RepeatedPtrField::iterator msg_iter; + RepeatedPtrField::iterator msg_end; + for (msg_iter = message + .MutableRepeatedExtension( + unittest::repeated_nested_message_extension) + ->begin(), + msg_end = message + .MutableRepeatedExtension( + unittest::repeated_nested_message_extension) + ->end(); msg_iter != msg_end; ++msg_iter) { msg_iter->set_bb(1234); } - RepeatedPtrField:: - const_iterator msg_const_iter; - RepeatedPtrField:: - const_iterator msg_const_end; - for (msg_const_iter = message.GetRepeatedExtension( - unittest::repeated_nested_message_extension).begin(), - msg_const_end = message.GetRepeatedExtension( - unittest::repeated_nested_message_extension).end(); + RepeatedPtrField::const_iterator + msg_const_iter; + RepeatedPtrField::const_iterator + msg_const_end; + for (msg_const_iter = message + .GetRepeatedExtension( + unittest::repeated_nested_message_extension) + .begin(), + msg_const_end = message + .GetRepeatedExtension( + unittest::repeated_nested_message_extension) + .end(); msg_const_iter != msg_const_end; ++msg_const_iter) { ASSERT_EQ(msg_const_iter->bb(), 1234); } @@ -1085,30 +1108,30 @@ TEST(ExtensionSetTest, RepeatedFields) { // Test range-based for as well, but only if compiled as C++11. #if __cplusplus >= 201103L // Test one primitive field. - for (auto& x : *message.MutableRepeatedExtension( - unittest::repeated_int32_extension)) { + for (auto& x : + *message.MutableRepeatedExtension(unittest::repeated_int32_extension)) { x = 4321; } - for (const auto& x : message.GetRepeatedExtension( - unittest::repeated_int32_extension)) { + for (const auto& x : + message.GetRepeatedExtension(unittest::repeated_int32_extension)) { ASSERT_EQ(x, 4321); } // Test one string field. - for (auto& x : *message.MutableRepeatedExtension( - unittest::repeated_string_extension)) { + for (auto& x : + *message.MutableRepeatedExtension(unittest::repeated_string_extension)) { x = "test_range_based_for"; } - for (const auto& x : message.GetRepeatedExtension( - unittest::repeated_string_extension)) { + for (const auto& x : + message.GetRepeatedExtension(unittest::repeated_string_extension)) { ASSERT_TRUE(x == "test_range_based_for"); } // Test one message field. for (auto& x : *message.MutableRepeatedExtension( - unittest::repeated_nested_message_extension)) { + unittest::repeated_nested_message_extension)) { x.set_bb(4321); } for (const auto& x : *message.MutableRepeatedExtension( - unittest::repeated_nested_message_extension)) { + unittest::repeated_nested_message_extension)) { ASSERT_EQ(x.bb(), 4321); } #endif @@ -1118,12 +1141,13 @@ TEST(ExtensionSetTest, RepeatedFields) { TEST(ExtensionSetTest, AbsentExtension) { unittest::TestAllExtensions message; message.MutableRepeatedExtension(unittest::repeated_nested_message_extension) - ->Add()->set_bb(123); - ASSERT_EQ(1, message.ExtensionSize( - unittest::repeated_nested_message_extension)); - EXPECT_EQ( - 123, message.GetExtension( - unittest::repeated_nested_message_extension, 0).bb()); + ->Add() + ->set_bb(123); + ASSERT_EQ(1, + message.ExtensionSize(unittest::repeated_nested_message_extension)); + EXPECT_EQ(123, + message.GetExtension(unittest::repeated_nested_message_extension, 0) + .bb()); } #ifdef PROTOBUF_HAS_DEATH_TEST @@ -1131,9 +1155,9 @@ TEST(ExtensionSetTest, AbsentExtension) { TEST(ExtensionSetTest, InvalidEnumDeath) { unittest::TestAllExtensions message; EXPECT_DEBUG_DEATH( - message.SetExtension(unittest::optional_foreign_enum_extension, - static_cast(53)), - "IsValid"); + message.SetExtension(unittest::optional_foreign_enum_extension, + static_cast(53)), + "IsValid"); } #endif // PROTOBUF_HAS_DEATH_TEST @@ -1169,9 +1193,9 @@ TEST(ExtensionSetTest, DynamicExtensions) { // If the field refers to one of the types nested in TestDynamicExtensions, // make it refer to the type in our dynamic proto instead. - string prefix = "." + template_descriptor->full_name() + "."; + std::string prefix = "." + template_descriptor->full_name() + "."; if (extension->has_type_name()) { - string* type_name = extension->mutable_type_name(); + std::string* type_name = extension->mutable_type_name(); if (HasPrefixString(*type_name, prefix)) { type_name->replace(0, prefix.size(), ".dynamic_extensions."); } @@ -1188,7 +1212,8 @@ TEST(ExtensionSetTest, DynamicExtensions) { // Construct a message that we can parse with the extensions we defined. // Since the extensions were based off of the fields of TestDynamicExtensions, // we can use that message to create this test message. - string data; + std::string data; + unittest::TestDynamicExtensions dynamic_extension; { unittest::TestDynamicExtensions message; message.set_scalar_extension(123); @@ -1208,12 +1233,13 @@ TEST(ExtensionSetTest, DynamicExtensions) { // An unknown enum value (for a known field). message.mutable_unknown_fields()->AddVarint( - unittest::TestDynamicExtensions::kDynamicEnumExtensionFieldNumber, - 12345); + unittest::TestDynamicExtensions::kDynamicEnumExtensionFieldNumber, + 12345); // A regular unknown field. message.mutable_unknown_fields()->AddLengthDelimited(54321, "unknown"); message.SerializeToString(&data); + dynamic_extension = message; } // Now we can parse this using our dynamic extension definitions... @@ -1228,29 +1254,28 @@ TEST(ExtensionSetTest, DynamicExtensions) { // Can we print it? EXPECT_EQ( - "[dynamic_extensions.scalar_extension]: 123\n" - "[dynamic_extensions.enum_extension]: FOREIGN_BAR\n" - "[dynamic_extensions.dynamic_enum_extension]: DYNAMIC_BAZ\n" - "[dynamic_extensions.message_extension] {\n" - " c: 456\n" - "}\n" - "[dynamic_extensions.dynamic_message_extension] {\n" - " dynamic_field: 789\n" - "}\n" - "[dynamic_extensions.repeated_extension]: \"foo\"\n" - "[dynamic_extensions.repeated_extension]: \"bar\"\n" - "[dynamic_extensions.packed_extension]: 12\n" - "[dynamic_extensions.packed_extension]: -34\n" - "[dynamic_extensions.packed_extension]: 56\n" - "[dynamic_extensions.packed_extension]: -78\n" - "2002: 12345\n" - "54321: \"unknown\"\n", - message.DebugString()); + "[dynamic_extensions.scalar_extension]: 123\n" + "[dynamic_extensions.enum_extension]: FOREIGN_BAR\n" + "[dynamic_extensions.dynamic_enum_extension]: DYNAMIC_BAZ\n" + "[dynamic_extensions.message_extension] {\n" + " c: 456\n" + "}\n" + "[dynamic_extensions.dynamic_message_extension] {\n" + " dynamic_field: 789\n" + "}\n" + "[dynamic_extensions.repeated_extension]: \"foo\"\n" + "[dynamic_extensions.repeated_extension]: \"bar\"\n" + "[dynamic_extensions.packed_extension]: 12\n" + "[dynamic_extensions.packed_extension]: -34\n" + "[dynamic_extensions.packed_extension]: 56\n" + "[dynamic_extensions.packed_extension]: -78\n" + "2002: 12345\n" + "54321: \"unknown\"\n", + message.DebugString()); // Can we serialize it? - // (Don't use EXPECT_EQ because we don't want to dump raw binary data to the - // terminal on failure.) - EXPECT_TRUE(message.SerializeAsString() == data); + EXPECT_TRUE( + EqualsToSerialized(dynamic_extension, message.SerializeAsString())); // What if we parse using the reflection-based parser? { @@ -1271,7 +1296,7 @@ TEST(ExtensionSetTest, DynamicExtensions) { const Message& sub_message = message.GetReflection()->GetMessage(message, message_extension); const unittest::ForeignMessage* typed_sub_message = -#if GOOGLE_PROTOBUF_RTTI +#if PROTOBUF_RTTI dynamic_cast(&sub_message); #else static_cast(&sub_message); @@ -1287,9 +1312,8 @@ TEST(ExtensionSetTest, DynamicExtensions) { file->FindExtensionByName("dynamic_message_extension"); ASSERT_TRUE(dynamic_message_extension != NULL); const Message& parent = unittest::TestAllExtensions::default_instance(); - const Message& sub_message = - parent.GetReflection()->GetMessage(parent, dynamic_message_extension, - &dynamic_factory); + const Message& sub_message = parent.GetReflection()->GetMessage( + parent, dynamic_message_extension, &dynamic_factory); const Message* prototype = dynamic_factory.GetPrototype(dynamic_message_extension->message_type()); EXPECT_EQ(prototype, &sub_message); diff --git a/src/google/protobuf/field_mask.pb.cc b/src/google/protobuf/field_mask.pb.cc index 9791b97430..57619091f5 100644 --- a/src/google/protobuf/field_mask.pb.cc +++ b/src/google/protobuf/field_mask.pb.cc @@ -5,116 +5,96 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_OPEN class FieldMaskDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _FieldMask_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsFieldMask_google_2fprotobuf_2ffield_5fmask_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_FieldMask_default_instance_; - new (ptr) ::google::protobuf::FieldMask(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_FieldMask_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::FieldMask(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::FieldMask::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::FieldMask::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsFieldMask_google_2fprotobuf_2ffield_5fmask_2eproto}, {}}; - -void InitDefaults_google_2fprotobuf_2ffield_5fmask_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto.base); -} +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2ffield_5fmask_2eproto[1]; -constexpr ::google::protobuf::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto = nullptr; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2ffield_5fmask_2eproto[1]; +static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto = nullptr; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2ffield_5fmask_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2ffield_5fmask_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldMask, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldMask, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::FieldMask, paths_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::FieldMask, paths_), }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, -1, sizeof(::google::protobuf::FieldMask)}, -}; - -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_FieldMask_default_instance_), +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, -1, sizeof(PROTOBUF_NAMESPACE_ID::FieldMask)}, }; -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2ffield_5fmask_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2ffield_5fmask_2eproto, "google/protobuf/field_mask.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2ffield_5fmask_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2ffield_5fmask_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto, file_level_service_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto, +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_FieldMask_default_instance_), }; -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto = { - false, InitDefaults_google_2fprotobuf_2ffield_5fmask_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2ffield_5fmask_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n google/protobuf/field_mask.proto\022\017goog" "le.protobuf\"\032\n\tFieldMask\022\r\n\005paths\030\001 \003(\tB" "\214\001\n\023com.google.protobufB\016FieldMaskProtoP" "\001Z9google.golang.org/genproto/protobuf/f" "ield_mask;field_mask\370\001\001\242\002\003GPB\252\002\036Google.P" "rotobuf.WellKnownTypesb\006proto3" -, - "google/protobuf/field_mask.proto", &assign_descriptors_table_google_2fprotobuf_2ffield_5fmask_2eproto, 230, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_deps[1] = { +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_sccs[1] = { + &scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto = { + &descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2ffield_5fmask_2eproto, "google/protobuf/field_mask.proto", 230, + &descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_once, descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_sccs, descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto_deps, 1, 0, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2ffield_5fmask_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2ffield_5fmask_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto, file_level_service_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2ffield_5fmask_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto, deps, 0); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2ffield_5fmask_2eproto = []() { AddDescriptors_google_2fprotobuf_2ffield_5fmask_2eproto(); return true; }(); -namespace google { -namespace protobuf { +static bool dynamic_init_dummy_google_2fprotobuf_2ffield_5fmask_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto), true); +PROTOBUF_NAMESPACE_OPEN // =================================================================== void FieldMask::InitAsDefaultInstance() { } -class FieldMask::HasBitSetters { +class FieldMask::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int FieldMask::kPathsFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - FieldMask::FieldMask() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.FieldMask) } -FieldMask::FieldMask(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +FieldMask::FieldMask(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), paths_(arena) { SharedCtor(); @@ -122,16 +102,15 @@ FieldMask::FieldMask(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.FieldMask) } FieldMask::FieldMask(const FieldMask& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), paths_(from.paths_) { _internal_metadata_.MergeFrom(from._internal_metadata_); // @@protoc_insertion_point(copy_constructor:google.protobuf.FieldMask) } void FieldMask::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto.base); } FieldMask::~FieldMask() { @@ -140,27 +119,27 @@ FieldMask::~FieldMask() { } void FieldMask::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void FieldMask::ArenaDtor(void* object) { FieldMask* _this = reinterpret_cast< FieldMask* >(object); (void)_this; } -void FieldMask::RegisterArenaDtor(::google::protobuf::Arena*) { +void FieldMask::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void FieldMask::SetCachedSize(int size) const { _cached_size_.Set(size); } const FieldMask& FieldMask::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto.base); return *internal_default_instance(); } void FieldMask::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FieldMask) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -169,82 +148,65 @@ void FieldMask::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* FieldMask::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* FieldMask::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated string paths = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.FieldMask.paths"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->add_paths(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 10 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr -= 1; + do { + ptr += 1; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(add_paths(), ptr, ctx, "google.protobuf.FieldMask.paths"); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 10); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FieldMask::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.FieldMask) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated string paths = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->add_paths())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->paths(this->paths_size() - 1).data(), static_cast(this->paths(this->paths_size() - 1).length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.FieldMask.paths")); } else { goto handle_unusual; @@ -257,7 +219,7 @@ bool FieldMask::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -273,49 +235,26 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void FieldMask::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.FieldMask) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated string paths = 1; - for (int i = 0, n = this->paths_size(); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->paths(i).data(), static_cast(this->paths(i).length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.FieldMask.paths"); - ::google::protobuf::internal::WireFormatLite::WriteString( - 1, this->paths(i), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.FieldMask) -} - -::google::protobuf::uint8* FieldMask::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* FieldMask::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.FieldMask) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated string paths = 1; - for (int i = 0, n = this->paths_size(); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->paths(i).data(), static_cast(this->paths(i).length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + for (auto it = this->paths().pointer_begin(), + end = this->paths().pointer_end(); it < end; ++it) { + const auto& s = **it; + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( + s.data(), static_cast(s.length()), + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.FieldMask.paths"); - target = ::google::protobuf::internal::WireFormatLite:: - WriteStringToArray(1, this->paths(i), target); + target = stream->WriteString(1, s, target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.FieldMask) return target; @@ -325,37 +264,36 @@ size_t FieldMask::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.FieldMask) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // repeated string paths = 1; total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->paths_size()); + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->paths_size()); for (int i = 0, n = this->paths_size(); i < n; i++) { - total_size += ::google::protobuf::internal::WireFormatLite::StringSize( + total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->paths(i)); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void FieldMask::MergeFrom(const ::google::protobuf::Message& from) { +void FieldMask::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.FieldMask) GOOGLE_DCHECK_NE(&from, this); const FieldMask* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.FieldMask) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.FieldMask) MergeFrom(*source); @@ -366,13 +304,13 @@ void FieldMask::MergeFrom(const FieldMask& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.FieldMask) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; paths_.MergeFrom(from.paths_); } -void FieldMask::CopyFrom(const ::google::protobuf::Message& from) { +void FieldMask::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.FieldMask) if (&from == this) return; Clear(); @@ -390,46 +328,24 @@ bool FieldMask::IsInitialized() const { return true; } -void FieldMask::Swap(FieldMask* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - FieldMask* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void FieldMask::UnsafeArenaSwap(FieldMask* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void FieldMask::InternalSwap(FieldMask* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); paths_.InternalSwap(CastToBase(&other->paths_)); } -::google::protobuf::Metadata FieldMask::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2ffield_5fmask_2eproto); - return ::file_level_metadata_google_2fprotobuf_2ffield_5fmask_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata FieldMask::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::FieldMask* Arena::CreateMaybeMessage< ::google::protobuf::FieldMask >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::FieldMask >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::FieldMask* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::FieldMask >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::FieldMask >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/field_mask.pb.h b/src/google/protobuf/field_mask.pb.h index 2055e964af..44160db43d 100644 --- a/src/google/protobuf/field_mask.pb.h +++ b/src/google/protobuf/field_mask.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/field_mask.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2ffield_5fmask_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2ffield_5fmask_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2ffield_5fmask_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2ffield_5fmask_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,57 +27,61 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ffield_5fmask_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ffield_5fmask_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2ffield_5fmask_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[1] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2ffield_5fmask_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] + 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2ffield_5fmask_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto; +PROTOBUF_NAMESPACE_OPEN class FieldMask; class FieldMaskDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern FieldMaskDefaultTypeInternal _FieldMask_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::FieldMask* Arena::CreateMaybeMessage<::google::protobuf::FieldMask>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { +PROTOBUF_EXPORT extern FieldMaskDefaultTypeInternal _FieldMask_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::FieldMask* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN // =================================================================== -class LIBPROTOBUF_EXPORT FieldMask : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FieldMask) */ { +class PROTOBUF_EXPORT FieldMask : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.FieldMask) */ { public: FieldMask(); virtual ~FieldMask(); FieldMask(const FieldMask& from); - - inline FieldMask& operator=(const FieldMask& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 FieldMask(FieldMask&& from) noexcept : FieldMask() { *this = ::std::move(from); } + inline FieldMask& operator=(const FieldMask& from) { + CopyFrom(from); + return *this; + } inline FieldMask& operator=(FieldMask&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -85,15 +90,21 @@ class LIBPROTOBUF_EXPORT FieldMask : public ::google::protobuf::Message /* @@pro } return *this; } - #endif - inline ::google::protobuf::Arena* GetArena() const final { + + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const FieldMask& default_instance(); @@ -105,54 +116,66 @@ class LIBPROTOBUF_EXPORT FieldMask : public ::google::protobuf::Message /* @@pro static constexpr int kIndexInFileMessages = 0; - void UnsafeArenaSwap(FieldMask* other); - void Swap(FieldMask* other); friend void swap(FieldMask& a, FieldMask& b) { a.Swap(&b); } + inline void Swap(FieldMask* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(FieldMask* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline FieldMask* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - FieldMask* New(::google::protobuf::Arena* arena) const final { + FieldMask* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const FieldMask& from); void MergeFrom(const FieldMask& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(FieldMask* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.FieldMask"; + } protected: - explicit FieldMask(::google::protobuf::Arena* arena); + explicit FieldMask(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -160,44 +183,49 @@ class LIBPROTOBUF_EXPORT FieldMask : public ::google::protobuf::Message /* @@pro } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto); + return ::descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kPathsFieldNumber = 1, + }; // repeated string paths = 1; int paths_size() const; void clear_paths(); - static const int kPathsFieldNumber = 1; - const ::std::string& paths(int index) const; - ::std::string* mutable_paths(int index); - void set_paths(int index, const ::std::string& value); - #if LANG_CXX11 - void set_paths(int index, ::std::string&& value); - #endif + const std::string& paths(int index) const; + std::string* mutable_paths(int index); + void set_paths(int index, const std::string& value); + void set_paths(int index, std::string&& value); void set_paths(int index, const char* value); void set_paths(int index, const char* value, size_t size); - ::std::string* add_paths(); - void add_paths(const ::std::string& value); - #if LANG_CXX11 - void add_paths(::std::string&& value); - #endif + std::string* add_paths(); + void add_paths(const std::string& value); + void add_paths(std::string&& value); void add_paths(const char* value); void add_paths(const char* value, size_t size); - const ::google::protobuf::RepeatedPtrField<::std::string>& paths() const; - ::google::protobuf::RepeatedPtrField<::std::string>* mutable_paths(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& paths() const; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_paths(); // @@protoc_insertion_point(class_scope:google.protobuf.FieldMask) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::RepeatedPtrField<::std::string> paths_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField paths_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2ffield_5fmask_2eproto; }; // =================================================================== @@ -218,26 +246,24 @@ inline int FieldMask::paths_size() const { inline void FieldMask::clear_paths() { paths_.Clear(); } -inline const ::std::string& FieldMask::paths(int index) const { +inline const std::string& FieldMask::paths(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.FieldMask.paths) return paths_.Get(index); } -inline ::std::string* FieldMask::mutable_paths(int index) { +inline std::string* FieldMask::mutable_paths(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.FieldMask.paths) return paths_.Mutable(index); } -inline void FieldMask::set_paths(int index, const ::std::string& value) { +inline void FieldMask::set_paths(int index, const std::string& value) { // @@protoc_insertion_point(field_set:google.protobuf.FieldMask.paths) paths_.Mutable(index)->assign(value); } -#if LANG_CXX11 -inline void FieldMask::set_paths(int index, ::std::string&& value) { +inline void FieldMask::set_paths(int index, std::string&& value) { // @@protoc_insertion_point(field_set:google.protobuf.FieldMask.paths) paths_.Mutable(index)->assign(std::move(value)); } -#endif inline void FieldMask::set_paths(int index, const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); paths_.Mutable(index)->assign(value); // @@protoc_insertion_point(field_set_char:google.protobuf.FieldMask.paths) } @@ -246,22 +272,20 @@ inline void FieldMask::set_paths(int index, const char* value, size_t size) { reinterpret_cast(value), size); // @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldMask.paths) } -inline ::std::string* FieldMask::add_paths() { +inline std::string* FieldMask::add_paths() { // @@protoc_insertion_point(field_add_mutable:google.protobuf.FieldMask.paths) return paths_.Add(); } -inline void FieldMask::add_paths(const ::std::string& value) { +inline void FieldMask::add_paths(const std::string& value) { paths_.Add()->assign(value); // @@protoc_insertion_point(field_add:google.protobuf.FieldMask.paths) } -#if LANG_CXX11 -inline void FieldMask::add_paths(::std::string&& value) { +inline void FieldMask::add_paths(std::string&& value) { paths_.Add(std::move(value)); // @@protoc_insertion_point(field_add:google.protobuf.FieldMask.paths) } -#endif inline void FieldMask::add_paths(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); paths_.Add()->assign(value); // @@protoc_insertion_point(field_add_char:google.protobuf.FieldMask.paths) } @@ -269,12 +293,12 @@ inline void FieldMask::add_paths(const char* value, size_t size) { paths_.Add()->assign(reinterpret_cast(value), size); // @@protoc_insertion_point(field_add_pointer:google.protobuf.FieldMask.paths) } -inline const ::google::protobuf::RepeatedPtrField<::std::string>& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& FieldMask::paths() const { // @@protoc_insertion_point(field_list:google.protobuf.FieldMask.paths) return paths_; } -inline ::google::protobuf::RepeatedPtrField<::std::string>* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* FieldMask::mutable_paths() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.FieldMask.paths) return &paths_; @@ -286,10 +310,9 @@ FieldMask::mutable_paths() { // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2ffield_5fmask_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2ffield_5fmask_2eproto diff --git a/src/google/protobuf/generated_enum_reflection.h b/src/google/protobuf/generated_enum_reflection.h index 27aac3f3c4..c25e51bc12 100644 --- a/src/google/protobuf/generated_enum_reflection.h +++ b/src/google/protobuf/generated_enum_reflection.h @@ -41,16 +41,18 @@ #include -#include #include +#include #ifdef SWIG #error "You cannot SWIG proto headers" #endif +#include + namespace google { namespace protobuf { - class EnumDescriptor; +class EnumDescriptor; } // namespace protobuf } // namespace google @@ -65,16 +67,14 @@ 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. -LIBPROTOBUF_EXPORT bool ParseNamedEnum(const EnumDescriptor* descriptor, - const string& name, - int* value); +// 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, + const std::string& name, int* value); -template -bool ParseNamedEnum(const EnumDescriptor* descriptor, - const string& name, +template +bool ParseNamedEnum(const EnumDescriptor* descriptor, const std::string& name, EnumType* value) { int tmp; if (!ParseNamedEnum(descriptor, name, &tmp)) return false; @@ -85,10 +85,13 @@ bool ParseNamedEnum(const EnumDescriptor* descriptor, // 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. -LIBPROTOBUF_EXPORT const string& NameOfEnum(const EnumDescriptor* descriptor, int value); +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/src/google/protobuf/generated_enum_util.cc b/src/google/protobuf/generated_enum_util.cc new file mode 100755 index 0000000000..d0c25a96d9 --- /dev/null +++ b/src/google/protobuf/generated_enum_util.cc @@ -0,0 +1,95 @@ +// 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/src/google/protobuf/generated_enum_util.h b/src/google/protobuf/generated_enum_util.h index 5c4dd65973..f1002e2d14 100644 --- a/src/google/protobuf/generated_enum_util.h +++ b/src/google/protobuf/generated_enum_util.h @@ -33,6 +33,11 @@ #include +#include +#include + +#include + #ifdef SWIG #error "You cannot SWIG proto headers" #endif @@ -42,9 +47,37 @@ 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 {}; +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/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index e24f03063b..f7e6ed365e 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -50,32 +50,50 @@ #include +#include + #define GOOGLE_PROTOBUF_HAS_ONEOF +using google::protobuf::internal::ArenaStringPtr; +using google::protobuf::internal::DescriptorTable; +using google::protobuf::internal::ExtensionSet; +using google::protobuf::internal::GenericTypeHandler; +using google::protobuf::internal::GetEmptyString; +using google::protobuf::internal::InlinedStringField; +using google::protobuf::internal::InternalMetadataWithArena; +using google::protobuf::internal::LazyField; +using google::protobuf::internal::MapFieldBase; +using google::protobuf::internal::MigrationSchema; +using google::protobuf::internal::OnShutdownDelete; +using google::protobuf::internal::ReflectionSchema; +using google::protobuf::internal::RepeatedPtrFieldBase; +using google::protobuf::internal::StringSpaceUsedExcludingSelfLong; +using google::protobuf::internal::WrappedMutex; + namespace google { namespace protobuf { -namespace internal { namespace { -bool IsMapFieldInApi(const FieldDescriptor* field) { - return field->is_map(); -} +bool IsMapFieldInApi(const FieldDescriptor* field) { return field->is_map(); } } // anonymous namespace -bool ParseNamedEnum(const EnumDescriptor* descriptor, - const string& name, +namespace internal { + +bool ParseNamedEnum(const EnumDescriptor* descriptor, const std::string& name, int* value) { const EnumValueDescriptor* d = descriptor->FindValueByName(name); - if (d == NULL) return false; + if (d == nullptr) return false; *value = d->number(); return true; } -const string& NameOfEnum(const EnumDescriptor* descriptor, int value) { +const std::string& NameOfEnum(const EnumDescriptor* descriptor, int value) { const EnumValueDescriptor* d = descriptor->FindValueByNumber(value); - return (d == NULL ? GetEmptyString() : d->name()); + return (d == nullptr ? GetEmptyString() : d->name()); } +} // namespace internal + // =================================================================== // Helpers for reporting usage errors (e.g. trying to use GetInt32() on // a string field). @@ -98,119 +116,129 @@ const To& GetConstRefAtOffset(const Message& message, uint32 offset) { return *GetConstPointerAtOffset(&message, offset); } -void ReportReflectionUsageError( - const Descriptor* descriptor, const FieldDescriptor* field, - const char* method, const char* description) { - GOOGLE_LOG(FATAL) - << "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::" << method << "\n" - " Message type: " << descriptor->full_name() << "\n" - " Field : " << field->full_name() << "\n" - " Problem : " << description; +void ReportReflectionUsageError(const Descriptor* descriptor, + const FieldDescriptor* field, + const char* method, const char* description) { + GOOGLE_LOG(FATAL) << "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::" + << method + << "\n" + " Message type: " + << descriptor->full_name() + << "\n" + " Field : " + << field->full_name() + << "\n" + " Problem : " + << description; } const char* cpptype_names_[FieldDescriptor::MAX_CPPTYPE + 1] = { - "INVALID_CPPTYPE", - "CPPTYPE_INT32", - "CPPTYPE_INT64", - "CPPTYPE_UINT32", - "CPPTYPE_UINT64", - "CPPTYPE_DOUBLE", - "CPPTYPE_FLOAT", - "CPPTYPE_BOOL", - "CPPTYPE_ENUM", - "CPPTYPE_STRING", - "CPPTYPE_MESSAGE" -}; + "INVALID_CPPTYPE", "CPPTYPE_INT32", "CPPTYPE_INT64", "CPPTYPE_UINT32", + "CPPTYPE_UINT64", "CPPTYPE_DOUBLE", "CPPTYPE_FLOAT", "CPPTYPE_BOOL", + "CPPTYPE_ENUM", "CPPTYPE_STRING", "CPPTYPE_MESSAGE"}; static void ReportReflectionUsageTypeError( const Descriptor* descriptor, const FieldDescriptor* field, - const char* method, - FieldDescriptor::CppType expected_type) { + const char* method, FieldDescriptor::CppType expected_type) { GOOGLE_LOG(FATAL) - << "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::" << method << "\n" - " Message type: " << descriptor->full_name() << "\n" - " Field : " << field->full_name() << "\n" - " Problem : Field is not the right type for this message:\n" - " Expected : " << cpptype_names_[expected_type] << "\n" - " Field type: " << cpptype_names_[field->cpp_type()]; + << "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::" + << method + << "\n" + " Message type: " + << descriptor->full_name() + << "\n" + " Field : " + << field->full_name() + << "\n" + " Problem : Field is not the right type for this message:\n" + " Expected : " + << cpptype_names_[expected_type] + << "\n" + " Field type: " + << cpptype_names_[field->cpp_type()]; } static void ReportReflectionUsageEnumTypeError( const Descriptor* descriptor, const FieldDescriptor* field, const char* method, const EnumValueDescriptor* value) { - GOOGLE_LOG(FATAL) - << "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::" << method << "\n" - " Message type: " << descriptor->full_name() << "\n" - " Field : " << field->full_name() << "\n" - " Problem : Enum value did not match field type:\n" - " Expected : " << field->enum_type()->full_name() << "\n" - " Actual : " << value->full_name(); -} - -#define USAGE_CHECK(CONDITION, METHOD, ERROR_DESCRIPTION) \ - if (!(CONDITION)) \ - ReportReflectionUsageError(descriptor_, field, #METHOD, ERROR_DESCRIPTION) -#define USAGE_CHECK_EQ(A, B, METHOD, ERROR_DESCRIPTION) \ + GOOGLE_LOG(FATAL) << "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::" + << method + << "\n" + " Message type: " + << descriptor->full_name() + << "\n" + " Field : " + << field->full_name() + << "\n" + " Problem : Enum value did not match field type:\n" + " Expected : " + << field->enum_type()->full_name() + << "\n" + " Actual : " + << value->full_name(); +} + +#define USAGE_CHECK(CONDITION, METHOD, ERROR_DESCRIPTION) \ + if (!(CONDITION)) \ + ReportReflectionUsageError(descriptor_, field, #METHOD, ERROR_DESCRIPTION) +#define USAGE_CHECK_EQ(A, B, METHOD, ERROR_DESCRIPTION) \ USAGE_CHECK((A) == (B), METHOD, ERROR_DESCRIPTION) -#define USAGE_CHECK_NE(A, B, METHOD, ERROR_DESCRIPTION) \ +#define USAGE_CHECK_NE(A, B, METHOD, ERROR_DESCRIPTION) \ USAGE_CHECK((A) != (B), METHOD, ERROR_DESCRIPTION) -#define USAGE_CHECK_TYPE(METHOD, CPPTYPE) \ - if (field->cpp_type() != FieldDescriptor::CPPTYPE_##CPPTYPE) \ - ReportReflectionUsageTypeError(descriptor_, field, #METHOD, \ - FieldDescriptor::CPPTYPE_##CPPTYPE) +#define USAGE_CHECK_TYPE(METHOD, CPPTYPE) \ + if (field->cpp_type() != FieldDescriptor::CPPTYPE_##CPPTYPE) \ + ReportReflectionUsageTypeError(descriptor_, field, #METHOD, \ + FieldDescriptor::CPPTYPE_##CPPTYPE) -#define USAGE_CHECK_ENUM_VALUE(METHOD) \ - if (value->type() != field->enum_type()) \ - ReportReflectionUsageEnumTypeError(descriptor_, field, #METHOD, value) +#define USAGE_CHECK_ENUM_VALUE(METHOD) \ + if (value->type() != field->enum_type()) \ + ReportReflectionUsageEnumTypeError(descriptor_, field, #METHOD, value) -#define USAGE_CHECK_MESSAGE_TYPE(METHOD) \ - USAGE_CHECK_EQ(field->containing_type(), descriptor_, \ - METHOD, "Field does not match message type."); -#define USAGE_CHECK_SINGULAR(METHOD) \ - USAGE_CHECK_NE(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ +#define USAGE_CHECK_MESSAGE_TYPE(METHOD) \ + USAGE_CHECK_EQ(field->containing_type(), descriptor_, METHOD, \ + "Field does not match message type."); +#define USAGE_CHECK_SINGULAR(METHOD) \ + USAGE_CHECK_NE(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ "Field is repeated; the method requires a singular field.") -#define USAGE_CHECK_REPEATED(METHOD) \ - USAGE_CHECK_EQ(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ +#define USAGE_CHECK_REPEATED(METHOD) \ + USAGE_CHECK_EQ(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ "Field is singular; the method requires a repeated field.") -#define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \ - USAGE_CHECK_MESSAGE_TYPE(METHOD); \ - USAGE_CHECK_##LABEL(METHOD); \ - USAGE_CHECK_TYPE(METHOD, CPPTYPE) +#define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \ + USAGE_CHECK_MESSAGE_TYPE(METHOD); \ + USAGE_CHECK_##LABEL(METHOD); \ + USAGE_CHECK_TYPE(METHOD, CPPTYPE) } // namespace // =================================================================== -GeneratedMessageReflection::GeneratedMessageReflection( - const Descriptor* descriptor, const ReflectionSchema& schema, - const DescriptorPool* pool, MessageFactory* factory) +Reflection::Reflection(const Descriptor* descriptor, + const internal::ReflectionSchema& schema, + const DescriptorPool* pool, MessageFactory* factory) : descriptor_(descriptor), schema_(schema), - descriptor_pool_((pool == NULL) ? DescriptorPool::generated_pool() - : pool), + descriptor_pool_( + (pool == nullptr) ? DescriptorPool::internal_generated_pool() : pool), message_factory_(factory), last_non_weak_field_index_(-1) { last_non_weak_field_index_ = descriptor_->field_count() - 1; } -GeneratedMessageReflection::~GeneratedMessageReflection() {} - -const UnknownFieldSet& GeneratedMessageReflection::GetUnknownFields( +const UnknownFieldSet& Reflection::GetUnknownFields( const Message& message) const { return GetInternalMetadataWithArena(message).unknown_fields(); } -UnknownFieldSet* GeneratedMessageReflection::MutableUnknownFields( - Message* message) const { +UnknownFieldSet* Reflection::MutableUnknownFields(Message* message) const { return MutableInternalMetadataWithArena(message)->mutable_unknown_fields(); } -size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { +size_t Reflection::SpaceUsedLong(const Message& message) const { // object_size_ already includes the in-memory representation of each field // in the message, so we only need to account for additional memory used by // the fields. @@ -225,35 +253,36 @@ size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { const FieldDescriptor* field = descriptor_->field(i); if (field->is_repeated()) { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - total_size += GetRaw >(message, field) \ - .SpaceUsedExcludingSelfLong(); \ - break - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + total_size += GetRaw >(message, field) \ + .SpaceUsedExcludingSelfLong(); \ + break + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - total_size += GetRaw >(message, field) - .SpaceUsedExcludingSelfLong(); + total_size += + GetRaw >(message, field) + .SpaceUsedExcludingSelfLong(); break; } break; case FieldDescriptor::CPPTYPE_MESSAGE: if (IsMapFieldInApi(field)) { - total_size += GetRaw(message, field) + total_size += GetRaw(message, field) .SpaceUsedExcludingSelfLong(); } else { // We don't know which subclass of RepeatedPtrFieldBase the type is, @@ -270,14 +299,14 @@ size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { continue; } switch (field->cpp_type()) { - case FieldDescriptor::CPPTYPE_INT32 : - case FieldDescriptor::CPPTYPE_INT64 : + case FieldDescriptor::CPPTYPE_INT32: + case FieldDescriptor::CPPTYPE_INT64: case FieldDescriptor::CPPTYPE_UINT32: case FieldDescriptor::CPPTYPE_UINT64: case FieldDescriptor::CPPTYPE_DOUBLE: - case FieldDescriptor::CPPTYPE_FLOAT : - case FieldDescriptor::CPPTYPE_BOOL : - case FieldDescriptor::CPPTYPE_ENUM : + case FieldDescriptor::CPPTYPE_FLOAT: + case FieldDescriptor::CPPTYPE_BOOL: + case FieldDescriptor::CPPTYPE_ENUM: // Field is inline, so we've already counted it. break; @@ -286,18 +315,18 @@ size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: { if (IsInlined(field)) { - const string* ptr = + const std::string* ptr = &GetField(message, field).GetNoArena(); total_size += StringSpaceUsedExcludingSelfLong(*ptr); break; } - // Initially, the string points to the default value stored in - // the prototype. Only count the string if it has been changed - // from the default value. - const string* default_ptr = + // Initially, the string points to the default value stored + // in the prototype. Only count the string if it has been + // changed from the default value. + const std::string* default_ptr = &DefaultRaw(field).Get(); - const string* ptr = + const std::string* ptr = &GetField(message, field).Get(); if (ptr != default_ptr) { @@ -318,7 +347,7 @@ size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { // external type's prototype, so there is no extra memory usage. } else { const Message* sub_message = GetRaw(message, field); - if (sub_message != NULL) { + if (sub_message != nullptr) { total_size += sub_message->SpaceUsedLong(); } } @@ -329,42 +358,40 @@ size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { return total_size; } -void GeneratedMessageReflection::SwapField( - Message* message1, - Message* message2, - const FieldDescriptor* field) const { +void Reflection::SwapField(Message* message1, Message* message2, + const FieldDescriptor* field) const { if (field->is_repeated()) { switch (field->cpp_type()) { -#define SWAP_ARRAYS(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - MutableRaw >(message1, field)->Swap( \ - MutableRaw >(message2, field)); \ - break; - - SWAP_ARRAYS(INT32 , int32 ); - SWAP_ARRAYS(INT64 , int64 ); +#define SWAP_ARRAYS(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + MutableRaw >(message1, field) \ + ->Swap(MutableRaw >(message2, field)); \ + break; + + SWAP_ARRAYS(INT32, int32); + SWAP_ARRAYS(INT64, int64); SWAP_ARRAYS(UINT32, uint32); SWAP_ARRAYS(UINT64, uint64); - SWAP_ARRAYS(FLOAT , float ); + SWAP_ARRAYS(FLOAT, float); SWAP_ARRAYS(DOUBLE, double); - SWAP_ARRAYS(BOOL , bool ); - SWAP_ARRAYS(ENUM , int ); + SWAP_ARRAYS(BOOL, bool); + SWAP_ARRAYS(ENUM, int); #undef SWAP_ARRAYS case FieldDescriptor::CPPTYPE_STRING: switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - MutableRaw(message1, field)-> - Swap >( + MutableRaw(message1, field) + ->Swap >( MutableRaw(message2, field)); break; } break; case FieldDescriptor::CPPTYPE_MESSAGE: if (IsMapFieldInApi(field)) { - MutableRaw(message1, field)->Swap( - MutableRaw(message2, field)); + MutableRaw(message1, field) + ->Swap(MutableRaw(message2, field)); } else { MutableRaw(message1, field) ->Swap >( @@ -377,20 +404,20 @@ void GeneratedMessageReflection::SwapField( } } else { switch (field->cpp_type()) { -#define SWAP_VALUES(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - std::swap(*MutableRaw(message1, field), \ - *MutableRaw(message2, field)); \ - break; - - SWAP_VALUES(INT32 , int32 ); - SWAP_VALUES(INT64 , int64 ); +#define SWAP_VALUES(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + std::swap(*MutableRaw(message1, field), \ + *MutableRaw(message2, field)); \ + break; + + SWAP_VALUES(INT32, int32); + SWAP_VALUES(INT64, int64); SWAP_VALUES(UINT32, uint32); SWAP_VALUES(UINT64, uint64); - SWAP_VALUES(FLOAT , float ); + SWAP_VALUES(FLOAT, float); SWAP_VALUES(DOUBLE, double); - SWAP_VALUES(BOOL , bool ); - SWAP_VALUES(ENUM , int ); + SWAP_VALUES(BOOL, bool); + SWAP_VALUES(ENUM, int); #undef SWAP_VALUES case FieldDescriptor::CPPTYPE_MESSAGE: if (GetArena(message1) == GetArena(message2)) { @@ -399,12 +426,12 @@ void GeneratedMessageReflection::SwapField( } else { Message** sub_msg1 = MutableRaw(message1, field); Message** sub_msg2 = MutableRaw(message2, field); - if (*sub_msg1 == NULL && *sub_msg2 == NULL) break; + if (*sub_msg1 == nullptr && *sub_msg2 == nullptr) break; if (*sub_msg1 && *sub_msg2) { (*sub_msg1)->GetReflection()->Swap(*sub_msg1, *sub_msg2); break; } - if (*sub_msg1 == NULL) { + if (*sub_msg1 == nullptr) { *sub_msg1 = (*sub_msg2)->New(message1->GetArena()); (*sub_msg1)->CopyFrom(**sub_msg2); ClearField(message2, field); @@ -419,35 +446,33 @@ void GeneratedMessageReflection::SwapField( case FieldDescriptor::CPPTYPE_STRING: switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. - case FieldOptions::STRING: - { - Arena* arena1 = GetArena(message1); - Arena* arena2 = GetArena(message2); + case FieldOptions::STRING: { + Arena* arena1 = GetArena(message1); + Arena* arena2 = GetArena(message2); - if (IsInlined(field)) { - InlinedStringField* string1 = - MutableRaw(message1, field); - InlinedStringField* string2 = - MutableRaw(message2, field); - string1->Swap(string2); - break; - } + if (IsInlined(field)) { + InlinedStringField* string1 = + MutableRaw(message1, field); + InlinedStringField* string2 = + MutableRaw(message2, field); + string1->Swap(string2); + break; + } - ArenaStringPtr* string1 = - MutableRaw(message1, field); - ArenaStringPtr* string2 = - MutableRaw(message2, field); - const string* default_ptr = - &DefaultRaw(field).Get(); - if (arena1 == arena2) { - string1->Swap(string2, default_ptr, arena1); - } else { - const string temp = string1->Get(); - string1->Set(default_ptr, string2->Get(), arena1); - string2->Set(default_ptr, temp, arena2); - } + ArenaStringPtr* string1 = + MutableRaw(message1, field); + ArenaStringPtr* string2 = + MutableRaw(message2, field); + const std::string* default_ptr = + &DefaultRaw(field).Get(); + if (arena1 == arena2) { + string1->Swap(string2, default_ptr, arena1); + } else { + const std::string temp = string1->Get(); + string1->Set(default_ptr, string2->Get(), arena1); + string2->Set(default_ptr, temp, arena2); } - break; + } break; } break; @@ -457,10 +482,8 @@ void GeneratedMessageReflection::SwapField( } } -void GeneratedMessageReflection::SwapOneofField( - Message* message1, - Message* message2, - const OneofDescriptor* oneof_descriptor) const { +void Reflection::SwapOneofField(Message* message1, Message* message2, + const OneofDescriptor* oneof_descriptor) const { uint32 oneof_case1 = GetOneofCase(*message1, oneof_descriptor); uint32 oneof_case2 = GetOneofCase(*message2, oneof_descriptor); @@ -472,28 +495,28 @@ void GeneratedMessageReflection::SwapOneofField( double temp_double; bool temp_bool; int temp_int; - Message* temp_message = NULL; - string temp_string; + Message* temp_message = nullptr; + std::string temp_string; // Stores message1's oneof field to a temp variable. - const FieldDescriptor* field1 = NULL; + const FieldDescriptor* field1 = nullptr; if (oneof_case1 > 0) { field1 = descriptor_->FindFieldByNumber(oneof_case1); - //oneof_descriptor->field(oneof_case1); + // oneof_descriptor->field(oneof_case1); switch (field1->cpp_type()) { -#define GET_TEMP_VALUE(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - temp_##TYPE = GetField(*message1, field1); \ - break; +#define GET_TEMP_VALUE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + temp_##TYPE = GetField(*message1, field1); \ + break; - GET_TEMP_VALUE(INT32 , int32 ); - GET_TEMP_VALUE(INT64 , int64 ); + GET_TEMP_VALUE(INT32, int32); + GET_TEMP_VALUE(INT64, int64); GET_TEMP_VALUE(UINT32, uint32); GET_TEMP_VALUE(UINT64, uint64); - GET_TEMP_VALUE(FLOAT , float ); + GET_TEMP_VALUE(FLOAT, float); GET_TEMP_VALUE(DOUBLE, double); - GET_TEMP_VALUE(BOOL , bool ); - GET_TEMP_VALUE(ENUM , int ); + GET_TEMP_VALUE(BOOL, bool); + GET_TEMP_VALUE(ENUM, int); #undef GET_TEMP_VALUE case FieldDescriptor::CPPTYPE_MESSAGE: temp_message = ReleaseMessage(message1, field1); @@ -510,27 +533,24 @@ void GeneratedMessageReflection::SwapOneofField( // Sets message1's oneof field from the message2's oneof field. if (oneof_case2 > 0) { - const FieldDescriptor* field2 = - descriptor_->FindFieldByNumber(oneof_case2); + const FieldDescriptor* field2 = descriptor_->FindFieldByNumber(oneof_case2); switch (field2->cpp_type()) { -#define SET_ONEOF_VALUE1(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - SetField(message1, field2, GetField(*message2, field2)); \ - break; +#define SET_ONEOF_VALUE1(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + SetField(message1, field2, GetField(*message2, field2)); \ + break; - SET_ONEOF_VALUE1(INT32 , int32 ); - SET_ONEOF_VALUE1(INT64 , int64 ); + SET_ONEOF_VALUE1(INT32, int32); + SET_ONEOF_VALUE1(INT64, int64); SET_ONEOF_VALUE1(UINT32, uint32); SET_ONEOF_VALUE1(UINT64, uint64); - SET_ONEOF_VALUE1(FLOAT , float ); + SET_ONEOF_VALUE1(FLOAT, float); SET_ONEOF_VALUE1(DOUBLE, double); - SET_ONEOF_VALUE1(BOOL , bool ); - SET_ONEOF_VALUE1(ENUM , int ); + SET_ONEOF_VALUE1(BOOL, bool); + SET_ONEOF_VALUE1(ENUM, int); #undef SET_ONEOF_VALUE1 case FieldDescriptor::CPPTYPE_MESSAGE: - SetAllocatedMessage(message1, - ReleaseMessage(message2, field2), - field2); + SetAllocatedMessage(message1, ReleaseMessage(message2, field2), field2); break; case FieldDescriptor::CPPTYPE_STRING: @@ -547,19 +567,19 @@ void GeneratedMessageReflection::SwapOneofField( // Sets message2's oneof field from the temp variable. if (oneof_case1 > 0) { switch (field1->cpp_type()) { -#define SET_ONEOF_VALUE2(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - SetField(message2, field1, temp_##TYPE); \ - break; +#define SET_ONEOF_VALUE2(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + SetField(message2, field1, temp_##TYPE); \ + break; - SET_ONEOF_VALUE2(INT32 , int32 ); - SET_ONEOF_VALUE2(INT64 , int64 ); + SET_ONEOF_VALUE2(INT32, int32); + SET_ONEOF_VALUE2(INT64, int64); SET_ONEOF_VALUE2(UINT32, uint32); SET_ONEOF_VALUE2(UINT64, uint64); - SET_ONEOF_VALUE2(FLOAT , float ); + SET_ONEOF_VALUE2(FLOAT, float); SET_ONEOF_VALUE2(DOUBLE, double); - SET_ONEOF_VALUE2(BOOL , bool ); - SET_ONEOF_VALUE2(ENUM , int ); + SET_ONEOF_VALUE2(BOOL, bool); + SET_ONEOF_VALUE2(ENUM, int); #undef SET_ONEOF_VALUE2 case FieldDescriptor::CPPTYPE_MESSAGE: SetAllocatedMessage(message2, temp_message, field1); @@ -577,26 +597,26 @@ void GeneratedMessageReflection::SwapOneofField( } } -void GeneratedMessageReflection::Swap( - Message* message1, - Message* message2) const { +void Reflection::Swap(Message* message1, Message* message2) const { if (message1 == message2) return; // TODO(kenton): Other Reflection methods should probably check this too. GOOGLE_CHECK_EQ(message1->GetReflection(), this) - << "First argument to Swap() (of type \"" - << message1->GetDescriptor()->full_name() - << "\") is not compatible with this reflection object (which is for type \"" - << descriptor_->full_name() - << "\"). Note that the exact same class is required; not just the same " - "descriptor."; + << "First argument to Swap() (of type \"" + << message1->GetDescriptor()->full_name() + << "\") is not compatible with this reflection object (which is for type " + "\"" + << descriptor_->full_name() + << "\"). Note that the exact same class is required; not just the same " + "descriptor."; GOOGLE_CHECK_EQ(message2->GetReflection(), this) - << "Second argument to Swap() (of type \"" - << message2->GetDescriptor()->full_name() - << "\") is not compatible with this reflection object (which is for type \"" - << descriptor_->full_name() - << "\"). Note that the exact same class is required; not just the same " - "descriptor."; + << "Second argument to Swap() (of type \"" + << message2->GetDescriptor()->full_name() + << "\") is not compatible with this reflection object (which is for type " + "\"" + << descriptor_->full_name() + << "\"). Note that the exact same class is required; not just the same " + "descriptor."; // Check that both messages are in the same arena (or both on the heap). We // need to copy all data if not, due to ownership semantics. @@ -607,7 +627,7 @@ void GeneratedMessageReflection::Swap( temp->MergeFrom(*message2); message2->CopyFrom(*message1); Swap(message1, temp); - if (GetArena(message1) == NULL) { + if (GetArena(message1) == nullptr) { delete temp; } return; @@ -650,27 +670,28 @@ void GeneratedMessageReflection::Swap( MutableUnknownFields(message1)->Swap(MutableUnknownFields(message2)); } -void GeneratedMessageReflection::SwapFields( - Message* message1, - Message* message2, +void Reflection::SwapFields( + Message* message1, Message* message2, const std::vector& fields) const { if (message1 == message2) return; // TODO(kenton): Other Reflection methods should probably check this too. GOOGLE_CHECK_EQ(message1->GetReflection(), this) - << "First argument to SwapFields() (of type \"" - << message1->GetDescriptor()->full_name() - << "\") is not compatible with this reflection object (which is for type \"" - << descriptor_->full_name() - << "\"). Note that the exact same class is required; not just the same " - "descriptor."; + << "First argument to SwapFields() (of type \"" + << message1->GetDescriptor()->full_name() + << "\") is not compatible with this reflection object (which is for type " + "\"" + << descriptor_->full_name() + << "\"). Note that the exact same class is required; not just the same " + "descriptor."; GOOGLE_CHECK_EQ(message2->GetReflection(), this) - << "Second argument to SwapFields() (of type \"" - << message2->GetDescriptor()->full_name() - << "\") is not compatible with this reflection object (which is for type \"" - << descriptor_->full_name() - << "\"). Note that the exact same class is required; not just the same " - "descriptor."; + << "Second argument to SwapFields() (of type \"" + << message2->GetDescriptor()->full_name() + << "\") is not compatible with this reflection object (which is for type " + "\"" + << descriptor_->full_name() + << "\"). Note that the exact same class is required; not just the same " + "descriptor."; std::set swapped_oneof; @@ -679,8 +700,7 @@ void GeneratedMessageReflection::SwapFields( const FieldDescriptor* field = fields[i]; if (field->is_extension()) { MutableExtensionSet(message1)->SwapExtension( - MutableExtensionSet(message2), - field->number()); + MutableExtensionSet(message2), field->number()); } else { if (field->containing_oneof()) { int oneof_index = field->containing_oneof()->index(); @@ -705,8 +725,8 @@ void GeneratedMessageReflection::SwapFields( // ------------------------------------------------------------------- -bool GeneratedMessageReflection::HasField(const Message& message, - const FieldDescriptor* field) const { +bool Reflection::HasField(const Message& message, + const FieldDescriptor* field) const { USAGE_CHECK_MESSAGE_TYPE(HasField); USAGE_CHECK_SINGULAR(HasField); @@ -721,8 +741,8 @@ bool GeneratedMessageReflection::HasField(const Message& message, } } -int GeneratedMessageReflection::FieldSize(const Message& message, - const FieldDescriptor* field) const { +int Reflection::FieldSize(const Message& message, + const FieldDescriptor* field) const { USAGE_CHECK_MESSAGE_TYPE(FieldSize); USAGE_CHECK_REPEATED(FieldSize); @@ -730,18 +750,18 @@ int GeneratedMessageReflection::FieldSize(const Message& message, return GetExtensionSet(message).ExtensionSize(field->number()); } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - return GetRaw >(message, field).size() +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + return GetRaw >(message, field).size() - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: @@ -766,8 +786,8 @@ int GeneratedMessageReflection::FieldSize(const Message& message, } } -void GeneratedMessageReflection::ClearField( - Message* message, const FieldDescriptor* field) const { +void Reflection::ClearField(Message* message, + const FieldDescriptor* field) const { USAGE_CHECK_MESSAGE_TYPE(ClearField); if (field->is_extension()) { @@ -782,24 +802,23 @@ void GeneratedMessageReflection::ClearField( // We need to set the field back to its default value. switch (field->cpp_type()) { -#define CLEAR_TYPE(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - *MutableRaw(message, field) = \ - field->default_value_##TYPE(); \ - break; +#define CLEAR_TYPE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + *MutableRaw(message, field) = field->default_value_##TYPE(); \ + break; - CLEAR_TYPE(INT32 , int32 ); - CLEAR_TYPE(INT64 , int64 ); + CLEAR_TYPE(INT32, int32); + CLEAR_TYPE(INT64, int64); CLEAR_TYPE(UINT32, uint32); CLEAR_TYPE(UINT64, uint64); - CLEAR_TYPE(FLOAT , float ); + CLEAR_TYPE(FLOAT, float); CLEAR_TYPE(DOUBLE, double); - CLEAR_TYPE(BOOL , bool ); + CLEAR_TYPE(BOOL, bool); #undef CLEAR_TYPE case FieldDescriptor::CPPTYPE_ENUM: *MutableRaw(message, field) = - field->default_value_enum()->number(); + field->default_value_enum()->number(); break; case FieldDescriptor::CPPTYPE_STRING: { @@ -807,17 +826,17 @@ void GeneratedMessageReflection::ClearField( default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: { if (IsInlined(field)) { - const string* default_ptr = + const std::string* default_ptr = &DefaultRaw(field).GetNoArena(); - MutableRaw(message, field)->SetNoArena( - default_ptr, *default_ptr); + MutableRaw(message, field) + ->SetNoArena(default_ptr, *default_ptr); break; } - const string* default_ptr = + const std::string* default_ptr = &DefaultRaw(field).Get(); - MutableRaw(message, field)->SetAllocated( - default_ptr, NULL, GetArena(message)); + MutableRaw(message, field) + ->SetAllocated(default_ptr, nullptr, GetArena(message)); break; } } @@ -827,11 +846,11 @@ void GeneratedMessageReflection::ClearField( case FieldDescriptor::CPPTYPE_MESSAGE: if (!schema_.HasHasbits()) { // Proto3 does not have has-bits and we need to set a message field - // to NULL in order to indicate its un-presence. - if (GetArena(message) == NULL) { + // to nullptr in order to indicate its un-presence. + if (GetArena(message) == nullptr) { delete *MutableRaw(message, field); } - *MutableRaw(message, field) = NULL; + *MutableRaw(message, field) = nullptr; } else { (*MutableRaw(message, field))->Clear(); } @@ -840,26 +859,26 @@ void GeneratedMessageReflection::ClearField( } } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - MutableRaw >(message, field)->Clear(); \ - break +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + MutableRaw >(message, field)->Clear(); \ + break - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: { switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - MutableRaw >(message, field)->Clear(); + MutableRaw >(message, field)->Clear(); break; } break; @@ -867,9 +886,7 @@ void GeneratedMessageReflection::ClearField( case FieldDescriptor::CPPTYPE_MESSAGE: { if (IsMapFieldInApi(field)) { - MutableRaw(message, field) - ->MutableRepeatedField() - ->Clear >(); + MutableRaw(message, field)->Clear(); } else { // We don't know which subclass of RepeatedPtrFieldBase the type is, // so we use RepeatedPtrFieldBase directly. @@ -882,9 +899,8 @@ void GeneratedMessageReflection::ClearField( } } -void GeneratedMessageReflection::RemoveLast( - Message* message, - const FieldDescriptor* field) const { +void Reflection::RemoveLast(Message* message, + const FieldDescriptor* field) const { USAGE_CHECK_MESSAGE_TYPE(RemoveLast); USAGE_CHECK_REPEATED(RemoveLast); @@ -892,26 +908,27 @@ void GeneratedMessageReflection::RemoveLast( MutableExtensionSet(message)->RemoveLast(field->number()); } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - MutableRaw >(message, field)->RemoveLast(); \ - break +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + MutableRaw >(message, field)->RemoveLast(); \ + break - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - MutableRaw >(message, field)->RemoveLast(); + MutableRaw >(message, field) + ->RemoveLast(); break; } break; @@ -923,16 +940,15 @@ void GeneratedMessageReflection::RemoveLast( ->RemoveLast >(); } else { MutableRaw(message, field) - ->RemoveLast >(); + ->RemoveLast >(); } break; } } } -Message* GeneratedMessageReflection::ReleaseLast( - Message* message, - const FieldDescriptor* field) const { +Message* Reflection::ReleaseLast(Message* message, + const FieldDescriptor* field) const { USAGE_CHECK_ALL(ReleaseLast, REPEATED, MESSAGE); if (field->is_extension()) { @@ -945,16 +961,13 @@ Message* GeneratedMessageReflection::ReleaseLast( ->ReleaseLast >(); } else { return MutableRaw(message, field) - ->ReleaseLast >(); + ->ReleaseLast >(); } } } -void GeneratedMessageReflection::SwapElements( - Message* message, - const FieldDescriptor* field, - int index1, - int index2) const { +void Reflection::SwapElements(Message* message, const FieldDescriptor* field, + int index1, int index2) const { USAGE_CHECK_MESSAGE_TYPE(Swap); USAGE_CHECK_REPEATED(Swap); @@ -962,20 +975,20 @@ void GeneratedMessageReflection::SwapElements( MutableExtensionSet(message)->SwapElements(field->number(), index1, index2); } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - MutableRaw >(message, field) \ - ->SwapElements(index1, index2); \ - break - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + MutableRaw >(message, field) \ + ->SwapElements(index1, index2); \ + break + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: @@ -986,7 +999,7 @@ void GeneratedMessageReflection::SwapElements( ->SwapElements(index1, index2); } else { MutableRaw(message, field) - ->SwapElements(index1, index2); + ->SwapElements(index1, index2); } break; } @@ -1002,17 +1015,19 @@ struct FieldNumberSorter { } }; -inline bool IsIndexInHasBitSet( - const uint32* has_bit_set, uint32 has_bit_index) { +bool IsIndexInHasBitSet(const uint32* has_bit_set, uint32 has_bit_index) { GOOGLE_DCHECK_NE(has_bit_index, ~0u); return ((has_bit_set[has_bit_index / 32] >> (has_bit_index % 32)) & static_cast(1)) != 0; } + +bool CreateUnknownEnumValues(const FileDescriptor* file) { + return file->syntax() == FileDescriptor::SYNTAX_PROTO3; +} } // namespace -void GeneratedMessageReflection::ListFields( - const Message& message, - std::vector* output) const { +void Reflection::ListFields(const Message& message, + std::vector* output) const { output->clear(); // Optimization: The default instance never has any fields set. @@ -1024,7 +1039,7 @@ void GeneratedMessageReflection::ListFields( // fleetwide and properly allowing this optimization through public interfaces // seems more trouble than it is worth. const uint32* const has_bits = - schema_.HasHasbits() ? GetHasBits(message) : NULL; + schema_.HasHasbits() ? GetHasBits(message) : nullptr; const uint32* const has_bits_indices = schema_.has_bit_indices_; output->reserve(descriptor_->field_count()); for (int i = 0; i <= last_non_weak_field_index_; i++) { @@ -1064,80 +1079,77 @@ void GeneratedMessageReflection::ListFields( // ------------------------------------------------------------------- #undef DEFINE_PRIMITIVE_ACCESSORS -#define DEFINE_PRIMITIVE_ACCESSORS(TYPENAME, TYPE, PASSTYPE, CPPTYPE) \ - PASSTYPE GeneratedMessageReflection::Get##TYPENAME( \ - const Message& message, const FieldDescriptor* field) const { \ - USAGE_CHECK_ALL(Get##TYPENAME, SINGULAR, CPPTYPE); \ - if (field->is_extension()) { \ - return GetExtensionSet(message).Get##TYPENAME( \ - field->number(), field->default_value_##PASSTYPE()); \ - } else { \ - return GetField(message, field); \ - } \ - } \ - \ - void GeneratedMessageReflection::Set##TYPENAME( \ - Message* message, const FieldDescriptor* field, \ - PASSTYPE value) const { \ - USAGE_CHECK_ALL(Set##TYPENAME, SINGULAR, CPPTYPE); \ - if (field->is_extension()) { \ - return MutableExtensionSet(message)->Set##TYPENAME( \ - field->number(), field->type(), value, field); \ - } else { \ - SetField(message, field, value); \ - } \ - } \ - \ - PASSTYPE GeneratedMessageReflection::GetRepeated##TYPENAME( \ - const Message& message, \ - const FieldDescriptor* field, int index) const { \ - USAGE_CHECK_ALL(GetRepeated##TYPENAME, REPEATED, CPPTYPE); \ - if (field->is_extension()) { \ - return GetExtensionSet(message).GetRepeated##TYPENAME( \ - field->number(), index); \ - } else { \ - return GetRepeatedField(message, field, index); \ - } \ - } \ - \ - void GeneratedMessageReflection::SetRepeated##TYPENAME( \ - Message* message, const FieldDescriptor* field, \ - int index, PASSTYPE value) const { \ - USAGE_CHECK_ALL(SetRepeated##TYPENAME, REPEATED, CPPTYPE); \ - if (field->is_extension()) { \ - MutableExtensionSet(message)->SetRepeated##TYPENAME( \ - field->number(), index, value); \ - } else { \ - SetRepeatedField(message, field, index, value); \ - } \ - } \ - \ - void GeneratedMessageReflection::Add##TYPENAME( \ - Message* message, const FieldDescriptor* field, \ - PASSTYPE value) const { \ - USAGE_CHECK_ALL(Add##TYPENAME, REPEATED, CPPTYPE); \ - if (field->is_extension()) { \ - MutableExtensionSet(message)->Add##TYPENAME( \ - field->number(), field->type(), field->options().packed(), value, \ - field); \ - } else { \ - AddField(message, field, value); \ - } \ - } - -DEFINE_PRIMITIVE_ACCESSORS(Int32 , int32 , int32 , INT32 ) -DEFINE_PRIMITIVE_ACCESSORS(Int64 , int64 , int64 , INT64 ) +#define DEFINE_PRIMITIVE_ACCESSORS(TYPENAME, TYPE, PASSTYPE, CPPTYPE) \ + PASSTYPE Reflection::Get##TYPENAME(const Message& message, \ + const FieldDescriptor* field) const { \ + USAGE_CHECK_ALL(Get##TYPENAME, SINGULAR, CPPTYPE); \ + if (field->is_extension()) { \ + return GetExtensionSet(message).Get##TYPENAME( \ + field->number(), field->default_value_##PASSTYPE()); \ + } else { \ + return GetField(message, field); \ + } \ + } \ + \ + void Reflection::Set##TYPENAME( \ + Message* message, const FieldDescriptor* field, PASSTYPE value) const { \ + USAGE_CHECK_ALL(Set##TYPENAME, SINGULAR, CPPTYPE); \ + if (field->is_extension()) { \ + return MutableExtensionSet(message)->Set##TYPENAME( \ + field->number(), field->type(), value, field); \ + } else { \ + SetField(message, field, value); \ + } \ + } \ + \ + PASSTYPE Reflection::GetRepeated##TYPENAME( \ + const Message& message, const FieldDescriptor* field, int index) const { \ + USAGE_CHECK_ALL(GetRepeated##TYPENAME, REPEATED, CPPTYPE); \ + if (field->is_extension()) { \ + return GetExtensionSet(message).GetRepeated##TYPENAME(field->number(), \ + index); \ + } else { \ + return GetRepeatedField(message, field, index); \ + } \ + } \ + \ + void Reflection::SetRepeated##TYPENAME(Message* message, \ + const FieldDescriptor* field, \ + int index, PASSTYPE value) const { \ + USAGE_CHECK_ALL(SetRepeated##TYPENAME, REPEATED, CPPTYPE); \ + if (field->is_extension()) { \ + MutableExtensionSet(message)->SetRepeated##TYPENAME(field->number(), \ + index, value); \ + } else { \ + SetRepeatedField(message, field, index, value); \ + } \ + } \ + \ + void Reflection::Add##TYPENAME( \ + Message* message, const FieldDescriptor* field, PASSTYPE value) const { \ + USAGE_CHECK_ALL(Add##TYPENAME, REPEATED, CPPTYPE); \ + if (field->is_extension()) { \ + MutableExtensionSet(message)->Add##TYPENAME( \ + field->number(), field->type(), field->options().packed(), value, \ + field); \ + } else { \ + AddField(message, field, value); \ + } \ + } + +DEFINE_PRIMITIVE_ACCESSORS(Int32, int32, int32, INT32) +DEFINE_PRIMITIVE_ACCESSORS(Int64, int64, int64, INT64) DEFINE_PRIMITIVE_ACCESSORS(UInt32, uint32, uint32, UINT32) DEFINE_PRIMITIVE_ACCESSORS(UInt64, uint64, uint64, UINT64) -DEFINE_PRIMITIVE_ACCESSORS(Float , float , float , FLOAT ) +DEFINE_PRIMITIVE_ACCESSORS(Float, float, float, FLOAT) DEFINE_PRIMITIVE_ACCESSORS(Double, double, double, DOUBLE) -DEFINE_PRIMITIVE_ACCESSORS(Bool , bool , bool , BOOL ) +DEFINE_PRIMITIVE_ACCESSORS(Bool, bool, bool, BOOL) #undef DEFINE_PRIMITIVE_ACCESSORS // ------------------------------------------------------------------- -string GeneratedMessageReflection::GetString( - const Message& message, const FieldDescriptor* field) const { +std::string Reflection::GetString(const Message& message, + const FieldDescriptor* field) const { USAGE_CHECK_ALL(GetString, SINGULAR, STRING); if (field->is_extension()) { return GetExtensionSet(message).GetString(field->number(), @@ -1156,9 +1168,9 @@ string GeneratedMessageReflection::GetString( } } -const string& GeneratedMessageReflection::GetStringReference( - const Message& message, - const FieldDescriptor* field, string* scratch) const { +const std::string& Reflection::GetStringReference(const Message& message, + const FieldDescriptor* field, + std::string* scratch) const { USAGE_CHECK_ALL(GetStringReference, SINGULAR, STRING); if (field->is_extension()) { return GetExtensionSet(message).GetString(field->number(), @@ -1178,9 +1190,8 @@ const string& GeneratedMessageReflection::GetStringReference( } -void GeneratedMessageReflection::SetString( - Message* message, const FieldDescriptor* field, - const string& value) const { +void Reflection::SetString(Message* message, const FieldDescriptor* field, + const std::string& value) const { USAGE_CHECK_ALL(SetString, SINGULAR, STRING); if (field->is_extension()) { return MutableExtensionSet(message)->SetString(field->number(), @@ -1190,19 +1201,21 @@ void GeneratedMessageReflection::SetString( default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: { if (IsInlined(field)) { - MutableField(message, field)->SetNoArena( - NULL, value); + MutableField(message, field) + ->SetNoArena(nullptr, value); break; } - const string* default_ptr = &DefaultRaw(field).Get(); + const std::string* default_ptr = + &DefaultRaw(field).Get(); if (field->containing_oneof() && !HasOneofField(*message, field)) { ClearOneof(message, field->containing_oneof()); - MutableField(message, field)->UnsafeSetDefault( - default_ptr); + MutableField(message, field) + ->UnsafeSetDefault(default_ptr); } - MutableField(message, field)->Set(default_ptr, - value, GetArena(message)); + MutableField(message, field) + ->Mutable(default_ptr, GetArena(message)) + ->assign(value); break; } } @@ -1210,8 +1223,9 @@ void GeneratedMessageReflection::SetString( } -string GeneratedMessageReflection::GetRepeatedString( - const Message& message, const FieldDescriptor* field, int index) const { +std::string Reflection::GetRepeatedString(const Message& message, + const FieldDescriptor* field, + int index) const { USAGE_CHECK_ALL(GetRepeatedString, REPEATED, STRING); if (field->is_extension()) { return GetExtensionSet(message).GetRepeatedString(field->number(), index); @@ -1219,14 +1233,14 @@ string GeneratedMessageReflection::GetRepeatedString( switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - return GetRepeatedPtrField(message, field, index); + return GetRepeatedPtrField(message, field, index); } } } -const string& GeneratedMessageReflection::GetRepeatedStringReference( - const Message& message, const FieldDescriptor* field, - int index, string* scratch) const { +const std::string& Reflection::GetRepeatedStringReference( + const Message& message, const FieldDescriptor* field, int index, + std::string* scratch) const { USAGE_CHECK_ALL(GetRepeatedStringReference, REPEATED, STRING); if (field->is_extension()) { return GetExtensionSet(message).GetRepeatedString(field->number(), index); @@ -1234,42 +1248,41 @@ const string& GeneratedMessageReflection::GetRepeatedStringReference( switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - return GetRepeatedPtrField(message, field, index); + return GetRepeatedPtrField(message, field, index); } } } -void GeneratedMessageReflection::SetRepeatedString( - Message* message, const FieldDescriptor* field, - int index, const string& value) const { +void Reflection::SetRepeatedString(Message* message, + const FieldDescriptor* field, int index, + const std::string& value) const { USAGE_CHECK_ALL(SetRepeatedString, REPEATED, STRING); if (field->is_extension()) { - MutableExtensionSet(message)->SetRepeatedString( - field->number(), index, value); + MutableExtensionSet(message)->SetRepeatedString(field->number(), index, + value); } else { switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - *MutableRepeatedField(message, field, index) = value; + *MutableRepeatedField(message, field, index) = value; break; } } } -void GeneratedMessageReflection::AddString( - Message* message, const FieldDescriptor* field, - const string& value) const { +void Reflection::AddString(Message* message, const FieldDescriptor* field, + const std::string& value) const { USAGE_CHECK_ALL(AddString, REPEATED, STRING); if (field->is_extension()) { - MutableExtensionSet(message)->AddString(field->number(), - field->type(), value, field); + MutableExtensionSet(message)->AddString(field->number(), field->type(), + value, field); } else { switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: - *AddField(message, field) = value; + *AddField(message, field) = value; break; } } @@ -1278,49 +1291,43 @@ void GeneratedMessageReflection::AddString( // ------------------------------------------------------------------- -inline bool CreateUnknownEnumValues(const FileDescriptor* file) { - return file->syntax() == FileDescriptor::SYNTAX_PROTO3; -} - -const EnumValueDescriptor* GeneratedMessageReflection::GetEnum( +const EnumValueDescriptor* Reflection::GetEnum( const Message& message, const FieldDescriptor* field) const { // Usage checked by GetEnumValue. int value = GetEnumValue(message, field); return field->enum_type()->FindValueByNumberCreatingIfUnknown(value); } -int GeneratedMessageReflection::GetEnumValue( - const Message& message, const FieldDescriptor* field) const { +int Reflection::GetEnumValue(const Message& message, + const FieldDescriptor* field) const { USAGE_CHECK_ALL(GetEnumValue, SINGULAR, ENUM); int32 value; if (field->is_extension()) { value = GetExtensionSet(message).GetEnum( - field->number(), field->default_value_enum()->number()); + field->number(), field->default_value_enum()->number()); } else { value = GetField(message, field); } return value; } -void GeneratedMessageReflection::SetEnum( - Message* message, const FieldDescriptor* field, - const EnumValueDescriptor* value) const { +void Reflection::SetEnum(Message* message, const FieldDescriptor* field, + const EnumValueDescriptor* value) const { // Usage checked by SetEnumValue. USAGE_CHECK_ENUM_VALUE(SetEnum); SetEnumValueInternal(message, field, value->number()); } -void GeneratedMessageReflection::SetEnumValue( - Message* message, const FieldDescriptor* field, - int value) const { +void Reflection::SetEnumValue(Message* message, const FieldDescriptor* field, + int value) const { USAGE_CHECK_ALL(SetEnumValue, SINGULAR, ENUM); if (!CreateUnknownEnumValues(descriptor_->file())) { // Check that the value is valid if we don't support direct storage of // unknown enum values. const EnumValueDescriptor* value_desc = field->enum_type()->FindValueByNumber(value); - if (value_desc == NULL) { + if (value_desc == nullptr) { MutableUnknownFields(message)->AddVarint(field->number(), value); return; } @@ -1328,26 +1335,27 @@ void GeneratedMessageReflection::SetEnumValue( SetEnumValueInternal(message, field, value); } -void GeneratedMessageReflection::SetEnumValueInternal( - Message* message, const FieldDescriptor* field, - int value) const { +void Reflection::SetEnumValueInternal(Message* message, + const FieldDescriptor* field, + int value) const { if (field->is_extension()) { - MutableExtensionSet(message)->SetEnum(field->number(), field->type(), - value, field); + MutableExtensionSet(message)->SetEnum(field->number(), field->type(), value, + field); } else { SetField(message, field, value); } } -const EnumValueDescriptor* GeneratedMessageReflection::GetRepeatedEnum( +const EnumValueDescriptor* Reflection::GetRepeatedEnum( const Message& message, const FieldDescriptor* field, int index) const { // Usage checked by GetRepeatedEnumValue. int value = GetRepeatedEnumValue(message, field, index); return field->enum_type()->FindValueByNumberCreatingIfUnknown(value); } -int GeneratedMessageReflection::GetRepeatedEnumValue( - const Message& message, const FieldDescriptor* field, int index) const { +int Reflection::GetRepeatedEnumValue(const Message& message, + const FieldDescriptor* field, + int index) const { USAGE_CHECK_ALL(GetRepeatedEnumValue, REPEATED, ENUM); int value; @@ -1359,26 +1367,24 @@ int GeneratedMessageReflection::GetRepeatedEnumValue( return value; } -void GeneratedMessageReflection::SetRepeatedEnum( - Message* message, - const FieldDescriptor* field, int index, - const EnumValueDescriptor* value) const { +void Reflection::SetRepeatedEnum(Message* message, const FieldDescriptor* field, + int index, + const EnumValueDescriptor* value) const { // Usage checked by SetRepeatedEnumValue. USAGE_CHECK_ENUM_VALUE(SetRepeatedEnum); SetRepeatedEnumValueInternal(message, field, index, value->number()); } -void GeneratedMessageReflection::SetRepeatedEnumValue( - Message* message, - const FieldDescriptor* field, int index, - int value) const { +void Reflection::SetRepeatedEnumValue(Message* message, + const FieldDescriptor* field, int index, + int value) const { USAGE_CHECK_ALL(SetRepeatedEnum, REPEATED, ENUM); if (!CreateUnknownEnumValues(descriptor_->file())) { // Check that the value is valid if we don't support direct storage of // unknown enum values. const EnumValueDescriptor* value_desc = field->enum_type()->FindValueByNumber(value); - if (value_desc == NULL) { + if (value_desc == nullptr) { MutableUnknownFields(message)->AddVarint(field->number(), value); return; } @@ -1386,36 +1392,33 @@ void GeneratedMessageReflection::SetRepeatedEnumValue( SetRepeatedEnumValueInternal(message, field, index, value); } -void GeneratedMessageReflection::SetRepeatedEnumValueInternal( - Message* message, - const FieldDescriptor* field, int index, - int value) const { +void Reflection::SetRepeatedEnumValueInternal(Message* message, + const FieldDescriptor* field, + int index, int value) const { if (field->is_extension()) { - MutableExtensionSet(message)->SetRepeatedEnum( - field->number(), index, value); + MutableExtensionSet(message)->SetRepeatedEnum(field->number(), index, + value); } else { SetRepeatedField(message, field, index, value); } } -void GeneratedMessageReflection::AddEnum( - Message* message, const FieldDescriptor* field, - const EnumValueDescriptor* value) const { +void Reflection::AddEnum(Message* message, const FieldDescriptor* field, + const EnumValueDescriptor* value) const { // Usage checked by AddEnumValue. USAGE_CHECK_ENUM_VALUE(AddEnum); AddEnumValueInternal(message, field, value->number()); } -void GeneratedMessageReflection::AddEnumValue( - Message* message, const FieldDescriptor* field, - int value) const { +void Reflection::AddEnumValue(Message* message, const FieldDescriptor* field, + int value) const { USAGE_CHECK_ALL(AddEnum, REPEATED, ENUM); if (!CreateUnknownEnumValues(descriptor_->file())) { // Check that the value is valid if we don't support direct storage of // unknown enum values. const EnumValueDescriptor* value_desc = field->enum_type()->FindValueByNumber(value); - if (value_desc == NULL) { + if (value_desc == nullptr) { MutableUnknownFields(message)->AddVarint(field->number(), value); return; } @@ -1423,13 +1426,13 @@ void GeneratedMessageReflection::AddEnumValue( AddEnumValueInternal(message, field, value); } -void GeneratedMessageReflection::AddEnumValueInternal( - Message* message, const FieldDescriptor* field, - int value) const { +void Reflection::AddEnumValueInternal(Message* message, + const FieldDescriptor* field, + int value) const { if (field->is_extension()) { MutableExtensionSet(message)->AddEnum(field->number(), field->type(), - field->options().packed(), - value, field); + field->options().packed(), value, + field); } else { AddField(message, field, value); } @@ -1437,32 +1440,31 @@ void GeneratedMessageReflection::AddEnumValueInternal( // ------------------------------------------------------------------- -const Message& GeneratedMessageReflection::GetMessage( - const Message& message, const FieldDescriptor* field, - MessageFactory* factory) const { +const Message& Reflection::GetMessage(const Message& message, + const FieldDescriptor* field, + MessageFactory* factory) const { USAGE_CHECK_ALL(GetMessage, SINGULAR, MESSAGE); - if (factory == NULL) factory = message_factory_; + if (factory == nullptr) factory = message_factory_; if (field->is_extension()) { - return static_cast( - GetExtensionSet(message).GetMessage( - field->number(), field->message_type(), factory)); + return static_cast(GetExtensionSet(message).GetMessage( + field->number(), field->message_type(), factory)); } else { const Message* result = GetRaw(message, field); - if (result == NULL) { + if (result == nullptr) { result = DefaultRaw(field); } return *result; } } -Message* GeneratedMessageReflection::MutableMessage( - Message* message, const FieldDescriptor* field, - MessageFactory* factory) const { +Message* Reflection::MutableMessage(Message* message, + const FieldDescriptor* field, + MessageFactory* factory) const { USAGE_CHECK_ALL(MutableMessage, SINGULAR, MESSAGE); - if (factory == NULL) factory = message_factory_; + if (factory == nullptr) factory = message_factory_; if (field->is_extension()) { return static_cast( @@ -1483,7 +1485,7 @@ Message* GeneratedMessageReflection::MutableMessage( SetBit(message, field); } - if (*result_holder == NULL) { + if (*result_holder == nullptr) { const Message* default_message = DefaultRaw(field); *result_holder = default_message->New(message->GetArena()); } @@ -1492,9 +1494,8 @@ Message* GeneratedMessageReflection::MutableMessage( } } -void GeneratedMessageReflection::UnsafeArenaSetAllocatedMessage( - Message* message, - Message* sub_message, +void Reflection::UnsafeArenaSetAllocatedMessage( + Message* message, Message* sub_message, const FieldDescriptor* field) const { USAGE_CHECK_ALL(SetAllocatedMessage, SINGULAR, MESSAGE); @@ -1503,7 +1504,7 @@ void GeneratedMessageReflection::UnsafeArenaSetAllocatedMessage( field->number(), field->type(), field, sub_message); } else { if (field->containing_oneof()) { - if (sub_message == NULL) { + if (sub_message == nullptr) { ClearOneof(message, field->containing_oneof()); return; } @@ -1513,29 +1514,27 @@ void GeneratedMessageReflection::UnsafeArenaSetAllocatedMessage( return; } - if (sub_message == NULL) { + if (sub_message == nullptr) { ClearBit(message, field); } else { SetBit(message, field); } Message** sub_message_holder = MutableRaw(message, field); - if (GetArena(message) == NULL) { + if (GetArena(message) == nullptr) { delete *sub_message_holder; } *sub_message_holder = sub_message; } } -void GeneratedMessageReflection::SetAllocatedMessage( - Message* message, - Message* sub_message, - const FieldDescriptor* field) const { +void Reflection::SetAllocatedMessage(Message* message, Message* sub_message, + const FieldDescriptor* field) const { // If message and sub-message are in different memory ownership domains // (different arenas, or one is on heap and one is not), then we may need to // do a copy. - if (sub_message != NULL && + if (sub_message != nullptr && sub_message->GetArena() != message->GetArena()) { - if (sub_message->GetArena() == NULL && message->GetArena() != NULL) { + if (sub_message->GetArena() == nullptr && message->GetArena() != nullptr) { // Case 1: parent is on an arena and child is heap-allocated. We can add // the child to the arena's Own() list to free on arena destruction, then // set our pointer. @@ -1554,13 +1553,12 @@ void GeneratedMessageReflection::SetAllocatedMessage( } } -Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage( - Message* message, - const FieldDescriptor* field, - MessageFactory* factory) const { +Message* Reflection::UnsafeArenaReleaseMessage(Message* message, + const FieldDescriptor* field, + MessageFactory* factory) const { USAGE_CHECK_ALL(ReleaseMessage, SINGULAR, MESSAGE); - if (factory == NULL) factory = message_factory_; + if (factory == nullptr) factory = message_factory_; if (field->is_extension()) { return static_cast( @@ -1574,22 +1572,21 @@ Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage( if (HasOneofField(*message, field)) { *MutableOneofCase(message, field->containing_oneof()) = 0; } else { - return NULL; + return nullptr; } } Message** result = MutableRaw(message, field); Message* ret = *result; - *result = NULL; + *result = nullptr; return ret; } } -Message* GeneratedMessageReflection::ReleaseMessage( - Message* message, - const FieldDescriptor* field, - MessageFactory* factory) const { +Message* Reflection::ReleaseMessage(Message* message, + const FieldDescriptor* field, + MessageFactory* factory) const { Message* released = UnsafeArenaReleaseMessage(message, field, factory); - if (GetArena(message) != NULL && released != NULL) { + if (GetArena(message) != nullptr && released != nullptr) { Message* copy_from_arena = released->New(); copy_from_arena->CopyFrom(*released); released = copy_from_arena; @@ -1597,8 +1594,9 @@ Message* GeneratedMessageReflection::ReleaseMessage( return released; } -const Message& GeneratedMessageReflection::GetRepeatedMessage( - const Message& message, const FieldDescriptor* field, int index) const { +const Message& Reflection::GetRepeatedMessage(const Message& message, + const FieldDescriptor* field, + int index) const { USAGE_CHECK_ALL(GetRepeatedMessage, REPEATED, MESSAGE); if (field->is_extension()) { @@ -1616,14 +1614,15 @@ const Message& GeneratedMessageReflection::GetRepeatedMessage( } } -Message* GeneratedMessageReflection::MutableRepeatedMessage( - Message* message, const FieldDescriptor* field, int index) const { +Message* Reflection::MutableRepeatedMessage(Message* message, + const FieldDescriptor* field, + int index) const { USAGE_CHECK_ALL(MutableRepeatedMessage, REPEATED, MESSAGE); if (field->is_extension()) { return static_cast( - MutableExtensionSet(message)->MutableRepeatedMessage( - field->number(), index)); + MutableExtensionSet(message)->MutableRepeatedMessage(field->number(), + index)); } else { if (IsMapFieldInApi(field)) { return MutableRaw(message, field) @@ -1631,27 +1630,26 @@ Message* GeneratedMessageReflection::MutableRepeatedMessage( ->Mutable >(index); } else { return MutableRaw(message, field) - ->Mutable >(index); + ->Mutable >(index); } } } -Message* GeneratedMessageReflection::AddMessage( - Message* message, const FieldDescriptor* field, - MessageFactory* factory) const { +Message* Reflection::AddMessage(Message* message, const FieldDescriptor* field, + MessageFactory* factory) const { USAGE_CHECK_ALL(AddMessage, REPEATED, MESSAGE); - if (factory == NULL) factory = message_factory_; + if (factory == nullptr) factory = message_factory_; if (field->is_extension()) { return static_cast( MutableExtensionSet(message)->AddMessage(field, factory)); } else { - Message* result = NULL; + Message* result = nullptr; // We can't use AddField() because RepeatedPtrFieldBase doesn't // know how to allocate one. - RepeatedPtrFieldBase* repeated = NULL; + RepeatedPtrFieldBase* repeated = nullptr; if (IsMapFieldInApi(field)) { repeated = MutableRaw(message, field)->MutableRepeatedField(); @@ -1659,7 +1657,7 @@ Message* GeneratedMessageReflection::AddMessage( repeated = MutableRaw(message, field); } result = repeated->AddFromCleared >(); - if (result == NULL) { + if (result == nullptr) { // We must allocate a new object. const Message* prototype; if (repeated->size() == 0) { @@ -1678,15 +1676,15 @@ Message* GeneratedMessageReflection::AddMessage( } } -void GeneratedMessageReflection::AddAllocatedMessage( - Message* message, const FieldDescriptor* field, - Message* new_entry) const { +void Reflection::AddAllocatedMessage(Message* message, + const FieldDescriptor* field, + Message* new_entry) const { USAGE_CHECK_ALL(AddAllocatedMessage, REPEATED, MESSAGE); if (field->is_extension()) { MutableExtensionSet(message)->AddAllocatedMessage(field, new_entry); } else { - RepeatedPtrFieldBase* repeated = NULL; + RepeatedPtrFieldBase* repeated = nullptr; if (IsMapFieldInApi(field)) { repeated = MutableRaw(message, field)->MutableRepeatedField(); @@ -1697,17 +1695,18 @@ void GeneratedMessageReflection::AddAllocatedMessage( } } -void* GeneratedMessageReflection::MutableRawRepeatedField( - Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType cpptype, - int ctype, const Descriptor* desc) const { +void* Reflection::MutableRawRepeatedField(Message* message, + const FieldDescriptor* field, + FieldDescriptor::CppType cpptype, + int ctype, + const Descriptor* desc) const { USAGE_CHECK_REPEATED("MutableRawRepeatedField"); - if (field->cpp_type() != cpptype) - ReportReflectionUsageTypeError(descriptor_, - field, "MutableRawRepeatedField", cpptype); - if (ctype >= 0) - GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch"; - if (desc != NULL) + if (field->cpp_type() != cpptype && + (field->cpp_type() != FieldDescriptor::CPPTYPE_ENUM || + cpptype != FieldDescriptor::CPPTYPE_INT32)) + ReportReflectionUsageTypeError(descriptor_, field, + "MutableRawRepeatedField", cpptype); + if (desc != nullptr) GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type"; if (field->is_extension()) { return MutableExtensionSet(message)->MutableRawRepeatedField( @@ -1722,17 +1721,18 @@ void* GeneratedMessageReflection::MutableRawRepeatedField( } } -const void* GeneratedMessageReflection::GetRawRepeatedField( - const Message& message, const FieldDescriptor* field, - FieldDescriptor::CppType cpptype, - int ctype, const Descriptor* desc) const { +const void* Reflection::GetRawRepeatedField(const Message& message, + const FieldDescriptor* field, + FieldDescriptor::CppType cpptype, + int ctype, + const Descriptor* desc) const { USAGE_CHECK_REPEATED("GetRawRepeatedField"); if (field->cpp_type() != cpptype) - ReportReflectionUsageTypeError(descriptor_, - field, "GetRawRepeatedField", cpptype); + ReportReflectionUsageTypeError(descriptor_, field, "GetRawRepeatedField", + cpptype); if (ctype >= 0) GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch"; - if (desc != NULL) + if (desc != nullptr) GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type"; if (field->is_extension()) { // Should use extension_set::GetRawRepeatedField. However, the required @@ -1740,8 +1740,8 @@ const void* GeneratedMessageReflection::GetRawRepeatedField( // Map is not supported in extensions, it is acceptable to use // extension_set::MutableRawRepeatedField which does not change the message. return MutableExtensionSet(const_cast(&message)) - ->MutableRawRepeatedField( - field->number(), field->type(), field->is_packed(), field); + ->MutableRawRepeatedField(field->number(), field->type(), + field->is_packed(), field); } else { // Trigger transform for MapField if (IsMapFieldInApi(field)) { @@ -1751,120 +1751,78 @@ const void* GeneratedMessageReflection::GetRawRepeatedField( } } -const FieldDescriptor* GeneratedMessageReflection::GetOneofFieldDescriptor( - const Message& message, - const OneofDescriptor* oneof_descriptor) const { +const FieldDescriptor* Reflection::GetOneofFieldDescriptor( + const Message& message, const OneofDescriptor* oneof_descriptor) const { uint32 field_number = GetOneofCase(message, oneof_descriptor); if (field_number == 0) { - return NULL; + return nullptr; } return descriptor_->FindFieldByNumber(field_number); } -bool GeneratedMessageReflection::ContainsMapKey( - const Message& message, - const FieldDescriptor* field, - const MapKey& key) const { - USAGE_CHECK(IsMapFieldInApi(field), - "LookupMapValue", +bool Reflection::ContainsMapKey(const Message& message, + const FieldDescriptor* field, + const MapKey& key) const { + USAGE_CHECK(IsMapFieldInApi(field), "LookupMapValue", "Field is not a map field."); return GetRaw(message, field).ContainsMapKey(key); } -bool GeneratedMessageReflection::InsertOrLookupMapValue( - Message* message, - const FieldDescriptor* field, - const MapKey& key, - MapValueRef* val) const { - USAGE_CHECK(IsMapFieldInApi(field), - "InsertOrLookupMapValue", +bool Reflection::InsertOrLookupMapValue(Message* message, + const FieldDescriptor* field, + const MapKey& key, + MapValueRef* val) const { + USAGE_CHECK(IsMapFieldInApi(field), "InsertOrLookupMapValue", "Field is not a map field."); val->SetType(field->message_type()->FindFieldByName("value")->cpp_type()); - return MutableRaw(message, field)->InsertOrLookupMapValue( - key, val); + return MutableRaw(message, field) + ->InsertOrLookupMapValue(key, val); } -bool GeneratedMessageReflection::DeleteMapValue( - Message* message, - const FieldDescriptor* field, - const MapKey& key) const { - USAGE_CHECK(IsMapFieldInApi(field), - "DeleteMapValue", +bool Reflection::DeleteMapValue(Message* message, const FieldDescriptor* field, + const MapKey& key) const { + USAGE_CHECK(IsMapFieldInApi(field), "DeleteMapValue", "Field is not a map field."); return MutableRaw(message, field)->DeleteMapValue(key); } -MapIterator GeneratedMessageReflection::MapBegin( - Message* message, - const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "MapBegin", - "Field is not a map field."); +MapIterator Reflection::MapBegin(Message* message, + const FieldDescriptor* field) const { + USAGE_CHECK(IsMapFieldInApi(field), "MapBegin", "Field is not a map field."); MapIterator iter(message, field); GetRaw(*message, field).MapBegin(&iter); return iter; } -MapIterator GeneratedMessageReflection::MapEnd( - Message* message, - const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "MapEnd", - "Field is not a map field."); +MapIterator Reflection::MapEnd(Message* message, + const FieldDescriptor* field) const { + USAGE_CHECK(IsMapFieldInApi(field), "MapEnd", "Field is not a map field."); MapIterator iter(message, field); GetRaw(*message, field).MapEnd(&iter); return iter; } -int GeneratedMessageReflection::MapSize( - const Message& message, - const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "MapSize", - "Field is not a map field."); +int Reflection::MapSize(const Message& message, + const FieldDescriptor* field) const { + USAGE_CHECK(IsMapFieldInApi(field), "MapSize", "Field is not a map field."); return GetRaw(message, field).size(); } // ----------------------------------------------------------------------------- -const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByName( - const string& name) const { - if (!schema_.HasExtensionSet()) return NULL; - - const FieldDescriptor* result = descriptor_pool_->FindExtensionByName(name); - if (result != NULL && result->containing_type() == descriptor_) { - return result; - } - - if (descriptor_->options().message_set_wire_format()) { - // MessageSet extensions may be identified by type name. - const Descriptor* type = descriptor_pool_->FindMessageTypeByName(name); - if (type != NULL) { - // Look for a matching extension in the foreign type's scope. - const int type_extension_count = type->extension_count(); - for (int i = 0; i < type_extension_count; i++) { - const FieldDescriptor* extension = type->extension(i); - if (extension->containing_type() == descriptor_ && - extension->type() == FieldDescriptor::TYPE_MESSAGE && - extension->is_optional() && - extension->message_type() == type) { - // Found it. - return extension; - } - } - } - } - - return NULL; +const FieldDescriptor* Reflection::FindKnownExtensionByName( + const std::string& name) const { + if (!schema_.HasExtensionSet()) return nullptr; + return descriptor_pool_->FindExtensionByPrintableName(descriptor_, name); } -const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByNumber( +const FieldDescriptor* Reflection::FindKnownExtensionByNumber( int number) const { - if (!schema_.HasExtensionSet()) return NULL; + if (!schema_.HasExtensionSet()) return nullptr; return descriptor_pool_->FindExtensionByNumber(descriptor_, number); } -bool GeneratedMessageReflection::SupportsUnknownEnumValues() const { +bool Reflection::SupportsUnknownEnumValues() const { return CreateUnknownEnumValues(descriptor_->file()); } @@ -1875,119 +1833,111 @@ bool GeneratedMessageReflection::SupportsUnknownEnumValues() const { // the given field. template -const Type& GeneratedMessageReflection::GetRawNonOneof( - const Message& message, const FieldDescriptor* field) const { +const Type& Reflection::GetRawNonOneof(const Message& message, + const FieldDescriptor* field) const { return GetConstRefAtOffset(message, schema_.GetFieldOffsetNonOneof(field)); } template -Type* GeneratedMessageReflection::MutableRawNonOneof( - Message* message, const FieldDescriptor* field) const { +Type* Reflection::MutableRawNonOneof(Message* message, + const FieldDescriptor* field) const { return GetPointerAtOffset(message, schema_.GetFieldOffsetNonOneof(field)); } template -const Type& GeneratedMessageReflection::GetRaw( - const Message& message, const FieldDescriptor* field) const { +const Type& Reflection::GetRaw(const Message& message, + const FieldDescriptor* field) const { if (field->containing_oneof() && !HasOneofField(message, field)) { return DefaultRaw(field); } return GetConstRefAtOffset(message, schema_.GetFieldOffset(field)); } -bool GeneratedMessageReflection::IsInlined(const FieldDescriptor* field) const { +bool Reflection::IsInlined(const FieldDescriptor* field) const { return schema_.IsFieldInlined(field); } template -Type* GeneratedMessageReflection::MutableRaw(Message* message, - const FieldDescriptor* field) const { +Type* Reflection::MutableRaw(Message* message, + const FieldDescriptor* field) const { return GetPointerAtOffset(message, schema_.GetFieldOffset(field)); } - -inline const uint32* GeneratedMessageReflection::GetHasBits( - const Message& message) const { +const uint32* Reflection::GetHasBits(const Message& message) const { GOOGLE_DCHECK(schema_.HasHasbits()); return &GetConstRefAtOffset(message, schema_.HasBitsOffset()); } -inline uint32* GeneratedMessageReflection::MutableHasBits( - Message* message) const { +uint32* Reflection::MutableHasBits(Message* message) const { GOOGLE_DCHECK(schema_.HasHasbits()); return GetPointerAtOffset(message, schema_.HasBitsOffset()); } -inline uint32 GeneratedMessageReflection::GetOneofCase( - const Message& message, const OneofDescriptor* oneof_descriptor) const { +uint32 Reflection::GetOneofCase(const Message& message, + const OneofDescriptor* oneof_descriptor) const { return GetConstRefAtOffset( message, schema_.GetOneofCaseOffset(oneof_descriptor)); } -inline uint32* GeneratedMessageReflection::MutableOneofCase( +uint32* Reflection::MutableOneofCase( Message* message, const OneofDescriptor* oneof_descriptor) const { return GetPointerAtOffset( message, schema_.GetOneofCaseOffset(oneof_descriptor)); } -inline const ExtensionSet& GeneratedMessageReflection::GetExtensionSet( - const Message& message) const { +const ExtensionSet& Reflection::GetExtensionSet(const Message& message) const { return GetConstRefAtOffset(message, schema_.GetExtensionSetOffset()); } -inline ExtensionSet* GeneratedMessageReflection::MutableExtensionSet( - Message* message) const { +ExtensionSet* Reflection::MutableExtensionSet(Message* message) const { return GetPointerAtOffset(message, schema_.GetExtensionSetOffset()); } -inline Arena* GeneratedMessageReflection::GetArena(Message* message) const { +Arena* Reflection::GetArena(Message* message) const { return GetInternalMetadataWithArena(*message).arena(); } -inline const InternalMetadataWithArena& -GeneratedMessageReflection::GetInternalMetadataWithArena( +const InternalMetadataWithArena& Reflection::GetInternalMetadataWithArena( const Message& message) const { return GetConstRefAtOffset( message, schema_.GetMetadataOffset()); } -inline InternalMetadataWithArena* -GeneratedMessageReflection::MutableInternalMetadataWithArena( +InternalMetadataWithArena* Reflection::MutableInternalMetadataWithArena( Message* message) const { return GetPointerAtOffset( message, schema_.GetMetadataOffset()); } template -inline const Type& GeneratedMessageReflection::DefaultRaw( - const FieldDescriptor* field) const { +const Type& Reflection::DefaultRaw(const FieldDescriptor* field) const { return *reinterpret_cast(schema_.GetFieldDefault(field)); } // Simple accessors for manipulating has_bits_. -inline bool GeneratedMessageReflection::HasBit( - const Message& message, const FieldDescriptor* field) const { +bool Reflection::HasBit(const Message& message, + const FieldDescriptor* field) const { GOOGLE_DCHECK(!field->options().weak()); if (schema_.HasHasbits()) { return IsIndexInHasBitSet(GetHasBits(message), schema_.HasBitIndex(field)); } // proto3: no has-bits. All fields present except messages, which are - // present only if their message-field pointer is non-NULL. + // present only if their message-field pointer is non-null. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { return !schema_.IsDefaultInstance(message) && - GetRaw(message, field) != NULL; + GetRaw(message, field) != nullptr; } else { // Non-message field (and non-oneof, since that was handled in HasField() // before calling us), and singular (again, checked in HasField). So, this // field must be a scalar. // Scalar primitive (numeric or string/bytes) fields are present if - // their value is non-zero (numeric) or non-empty (string/bytes). N.B.: + // their value is non-zero (numeric) or non-empty (string/bytes). N.B.: // we must use this definition here, rather than the "scalar fields // always present" in the proto3 docs, because MergeFrom() semantics // require presence as "present on wire", and reflection-based merge @@ -1998,7 +1948,8 @@ inline bool GeneratedMessageReflection::HasBit( default: { if (IsInlined(field)) { return !GetField(message, field) - .GetNoArena().empty(); + .GetNoArena() + .empty(); } return GetField(message, field).Get().size() > 0; } @@ -2029,8 +1980,7 @@ inline bool GeneratedMessageReflection::HasBit( } } -inline void GeneratedMessageReflection::SetBit( - Message* message, const FieldDescriptor* field) const { +void Reflection::SetBit(Message* message, const FieldDescriptor* field) const { GOOGLE_DCHECK(!field->options().weak()); if (!schema_.HasHasbits()) { return; @@ -2040,8 +1990,8 @@ inline void GeneratedMessageReflection::SetBit( (static_cast(1) << (index % 32)); } -inline void GeneratedMessageReflection::ClearBit( - Message* message, const FieldDescriptor* field) const { +void Reflection::ClearBit(Message* message, + const FieldDescriptor* field) const { GOOGLE_DCHECK(!field->options().weak()); if (!schema_.HasHasbits()) { return; @@ -2051,8 +2001,8 @@ inline void GeneratedMessageReflection::ClearBit( ~(static_cast(1) << (index % 32)); } -inline void GeneratedMessageReflection::SwapBit( - Message* message1, Message* message2, const FieldDescriptor* field) const { +void Reflection::SwapBit(Message* message1, Message* message2, + const FieldDescriptor* field) const { GOOGLE_DCHECK(!field->options().weak()); if (!schema_.HasHasbits()) { return; @@ -2070,46 +2020,46 @@ inline void GeneratedMessageReflection::SwapBit( } } -inline bool GeneratedMessageReflection::HasOneof( - const Message& message, const OneofDescriptor* oneof_descriptor) const { +bool Reflection::HasOneof(const Message& message, + const OneofDescriptor* oneof_descriptor) const { return (GetOneofCase(message, oneof_descriptor) > 0); } -inline bool GeneratedMessageReflection::HasOneofField( - const Message& message, const FieldDescriptor* field) const { +bool Reflection::HasOneofField(const Message& message, + const FieldDescriptor* field) const { return (GetOneofCase(message, field->containing_oneof()) == field->number()); } -inline void GeneratedMessageReflection::SetOneofCase( - Message* message, const FieldDescriptor* field) const { +void Reflection::SetOneofCase(Message* message, + const FieldDescriptor* field) const { *MutableOneofCase(message, field->containing_oneof()) = field->number(); } -inline void GeneratedMessageReflection::ClearOneofField( - Message* message, const FieldDescriptor* field) const { +void Reflection::ClearOneofField(Message* message, + const FieldDescriptor* field) const { if (HasOneofField(*message, field)) { ClearOneof(message, field->containing_oneof()); } } -inline void GeneratedMessageReflection::ClearOneof( - Message* message, const OneofDescriptor* oneof_descriptor) const { +void Reflection::ClearOneof(Message* message, + const OneofDescriptor* oneof_descriptor) const { // TODO(jieluo): Consider to cache the unused object instead of deleting // it. It will be much faster if an application switches a lot from // a few oneof fields. Time/space tradeoff uint32 oneof_case = GetOneofCase(*message, oneof_descriptor); if (oneof_case > 0) { const FieldDescriptor* field = descriptor_->FindFieldByNumber(oneof_case); - if (GetArena(message) == NULL) { + if (GetArena(message) == nullptr) { switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_STRING: { switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: { - const string* default_ptr = + const std::string* default_ptr = &DefaultRaw(field).Get(); - MutableField(message, field)-> - Destroy(default_ptr, GetArena(message)); + MutableField(message, field) + ->Destroy(default_ptr, GetArena(message)); break; } } @@ -2128,83 +2078,120 @@ inline void GeneratedMessageReflection::ClearOneof( } } +#define HANDLE_TYPE(TYPE, CPPTYPE, CTYPE) \ + template <> \ + const RepeatedField& Reflection::GetRepeatedField( \ + const Message& message, const FieldDescriptor* field) const { \ + return *static_cast*>(MutableRawRepeatedField( \ + const_cast(&message), field, CPPTYPE, CTYPE, NULL)); \ + } \ + \ + template <> \ + RepeatedField* Reflection::MutableRepeatedField( \ + Message * message, const FieldDescriptor* field) const { \ + return static_cast*>( \ + MutableRawRepeatedField(message, field, CPPTYPE, CTYPE, NULL)); \ + } + +HANDLE_TYPE(int32, FieldDescriptor::CPPTYPE_INT32, -1); +HANDLE_TYPE(int64, FieldDescriptor::CPPTYPE_INT64, -1); +HANDLE_TYPE(uint32, FieldDescriptor::CPPTYPE_UINT32, -1); +HANDLE_TYPE(uint64, FieldDescriptor::CPPTYPE_UINT64, -1); +HANDLE_TYPE(float, FieldDescriptor::CPPTYPE_FLOAT, -1); +HANDLE_TYPE(double, FieldDescriptor::CPPTYPE_DOUBLE, -1); +HANDLE_TYPE(bool, FieldDescriptor::CPPTYPE_BOOL, -1); + + +#undef HANDLE_TYPE + +void* Reflection::MutableRawRepeatedString(Message* message, + const FieldDescriptor* field, + bool is_string) const { + return MutableRawRepeatedField(message, field, + FieldDescriptor::CPPTYPE_STRING, + FieldOptions::STRING, NULL); +} + // Template implementations of basic accessors. Inline because each // template instance is only called from one location. These are // used for all types except messages. template -inline const Type& GeneratedMessageReflection::GetField( - const Message& message, const FieldDescriptor* field) const { +const Type& Reflection::GetField(const Message& message, + const FieldDescriptor* field) const { return GetRaw(message, field); } template -inline void GeneratedMessageReflection::SetField( - Message* message, const FieldDescriptor* field, const Type& value) const { +void Reflection::SetField(Message* message, const FieldDescriptor* field, + const Type& value) const { if (field->containing_oneof() && !HasOneofField(*message, field)) { ClearOneof(message, field->containing_oneof()); } *MutableRaw(message, field) = value; - field->containing_oneof() ? - SetOneofCase(message, field) : SetBit(message, field); + field->containing_oneof() ? SetOneofCase(message, field) + : SetBit(message, field); } template -inline Type* GeneratedMessageReflection::MutableField( - Message* message, const FieldDescriptor* field) const { - field->containing_oneof() ? - SetOneofCase(message, field) : SetBit(message, field); +Type* Reflection::MutableField(Message* message, + const FieldDescriptor* field) const { + field->containing_oneof() ? SetOneofCase(message, field) + : SetBit(message, field); return MutableRaw(message, field); } template -inline const Type& GeneratedMessageReflection::GetRepeatedField( - const Message& message, const FieldDescriptor* field, int index) const { +const Type& Reflection::GetRepeatedField(const Message& message, + const FieldDescriptor* field, + int index) const { return GetRaw >(message, field).Get(index); } template -inline const Type& GeneratedMessageReflection::GetRepeatedPtrField( - const Message& message, const FieldDescriptor* field, int index) const { +const Type& Reflection::GetRepeatedPtrField(const Message& message, + const FieldDescriptor* field, + int index) const { return GetRaw >(message, field).Get(index); } template -inline void GeneratedMessageReflection::SetRepeatedField( - Message* message, const FieldDescriptor* field, - int index, Type value) const { +void Reflection::SetRepeatedField(Message* message, + const FieldDescriptor* field, int index, + Type value) const { MutableRaw >(message, field)->Set(index, value); } template -inline Type* GeneratedMessageReflection::MutableRepeatedField( - Message* message, const FieldDescriptor* field, int index) const { +Type* Reflection::MutableRepeatedField(Message* message, + const FieldDescriptor* field, + int index) const { RepeatedPtrField* repeated = - MutableRaw >(message, field); + MutableRaw >(message, field); return repeated->Mutable(index); } template -inline void GeneratedMessageReflection::AddField( - Message* message, const FieldDescriptor* field, const Type& value) const { +void Reflection::AddField(Message* message, const FieldDescriptor* field, + const Type& value) const { MutableRaw >(message, field)->Add(value); } template -inline Type* GeneratedMessageReflection::AddField( - Message* message, const FieldDescriptor* field) const { +Type* Reflection::AddField(Message* message, + const FieldDescriptor* field) const { RepeatedPtrField* repeated = - MutableRaw >(message, field); + MutableRaw >(message, field); return repeated->Add(); } -MessageFactory* GeneratedMessageReflection::GetMessageFactory() const { +MessageFactory* Reflection::GetMessageFactory() const { return message_factory_; } -void* GeneratedMessageReflection::RepeatedFieldData( - Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType cpp_type, - const Descriptor* message_type) const { +void* Reflection::RepeatedFieldData(Message* message, + const FieldDescriptor* field, + FieldDescriptor::CppType cpp_type, + const Descriptor* message_type) const { GOOGLE_CHECK(field->is_repeated()); GOOGLE_CHECK(field->cpp_type() == cpp_type || (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM && @@ -2212,7 +2199,7 @@ void* GeneratedMessageReflection::RepeatedFieldData( << "The type parameter T in RepeatedFieldRef API doesn't match " << "the actual field type (for enums T should be the generated enum " << "type or int32)."; - if (message_type != NULL) { + if (message_type != nullptr) { GOOGLE_CHECK_EQ(message_type, field->message_type()); } if (field->is_extension()) { @@ -2223,14 +2210,20 @@ void* GeneratedMessageReflection::RepeatedFieldData( } } -MapFieldBase* GeneratedMessageReflection::MapData( - Message* message, const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "GetMapData", +MapFieldBase* Reflection::MutableMapData(Message* message, + const FieldDescriptor* field) const { + USAGE_CHECK(IsMapFieldInApi(field), "GetMapData", "Field is not a map field."); return MutableRaw(message, field); } +const MapFieldBase* Reflection::GetMapData(const Message& message, + const FieldDescriptor* field) const { + USAGE_CHECK(IsMapFieldInApi(field), "GetMapData", + "Field is not a map field."); + return &(GetRaw(message, field)); +} + namespace { // Helper function to transform migration schema into reflection schema. @@ -2252,13 +2245,14 @@ ReflectionSchema MigrationToReflectionSchema( return result; } -template +} // namespace + class AssignDescriptorsHelper { public: AssignDescriptorsHelper(MessageFactory* factory, Metadata* file_level_metadata, const EnumDescriptor** file_level_enum_descriptors, - const Schema* schemas, + const MigrationSchema* schemas, const Message* const* default_instance_data, const uint32* offsets) : factory_(factory), @@ -2275,11 +2269,11 @@ class AssignDescriptorsHelper { file_level_metadata_->descriptor = descriptor; - file_level_metadata_->reflection = new GeneratedMessageReflection( - descriptor, - MigrationToReflectionSchema(default_instance_data_, offsets_, - *schemas_), - DescriptorPool::generated_pool(), factory_); + file_level_metadata_->reflection = + new Reflection(descriptor, + MigrationToReflectionSchema(default_instance_data_, + offsets_, *schemas_), + DescriptorPool::internal_generated_pool(), factory_); for (int i = 0; i < descriptor->enum_type_count(); i++) { AssignEnumDescriptor(descriptor->enum_type(i)); } @@ -2299,11 +2293,13 @@ class AssignDescriptorsHelper { MessageFactory* factory_; Metadata* file_level_metadata_; const EnumDescriptor** file_level_enum_descriptors_; - const Schema* schemas_; - const Message* const * default_instance_data_; + const MigrationSchema* schemas_; + const Message* const* default_instance_data_; const uint32* offsets_; }; +namespace { + // We have the routines that assign descriptors and build reflection // automatically delete the allocated reflection. MetadataOwner owns // all the allocated reflection instances. @@ -2334,24 +2330,25 @@ struct MetadataOwner { std::vector > metadata_arrays_; }; -void AssignDescriptorsImpl(const AssignDescriptorsTable* table) { +void AssignDescriptorsImpl(const DescriptorTable* table) { // Ensure the file descriptor is added to the pool. { // This only happens once per proto file. So a global mutex to serialize // calls to AddDescriptors. static WrappedMutex mu{GOOGLE_PROTOBUF_LINKER_INITIALIZED}; mu.Lock(); - table->add_descriptors(); + AddDescriptors(table); mu.Unlock(); } // Fill the arrays with pointers to descriptors and reflection classes. const FileDescriptor* file = - DescriptorPool::generated_pool()->FindFileByName(table->filename); - GOOGLE_CHECK(file != NULL); + DescriptorPool::internal_generated_pool()->FindFileByName( + table->filename); + GOOGLE_CHECK(file != nullptr); MessageFactory* factory = MessageFactory::generated_factory(); - AssignDescriptorsHelper helper( + AssignDescriptorsHelper helper( factory, table->file_level_metadata, table->file_level_enum_descriptors, table->schemas, table->default_instances, table->offsets); @@ -2371,57 +2368,55 @@ void AssignDescriptorsImpl(const AssignDescriptorsTable* table) { helper.GetCurrentMetadataPtr()); } -void AddDescriptorsImpl(const DescriptorTable* table, const InitFunc* deps, - int num_deps) { - // Ensure default instances of this proto file are initialized. - table->init_defaults(); +void AddDescriptorsImpl(const DescriptorTable* table) { + // Reflection refers to the default instances so make sure they are + // initialized. + for (int i = 0; i < table->num_sccs; i++) { + internal::InitSCC(table->init_default_instances[i]); + } + // Ensure all dependent descriptors are registered to the generated descriptor // pool and message factory. - for (int i = 0; i < num_deps; i++) { + for (int i = 0; i < table->num_deps; i++) { // In case of weak fields deps[i] could be null. - if (deps[i]) deps[i](); + if (table->deps[i]) AddDescriptors(table->deps[i]); } + // Register the descriptor of this file. DescriptorPool::InternalAddGeneratedFile(table->descriptor, table->size); - MessageFactory::InternalRegisterGeneratedFile( - table->filename, table->assign_descriptors_table); + MessageFactory::InternalRegisterGeneratedFile(table); } } // namespace -void AssignDescriptors(AssignDescriptorsTable* table) { - call_once(table->once, AssignDescriptorsImpl, table); +// Separate function because it needs to be a friend of +// Reflection +void RegisterAllTypesInternal(const Metadata* file_level_metadata, int size) { + for (int i = 0; i < size; i++) { + const Reflection* reflection = file_level_metadata[i].reflection; + MessageFactory::InternalRegisterGeneratedMessage( + file_level_metadata[i].descriptor, + reflection->schema_.default_instance_); + } +} + +namespace internal { + +void AssignDescriptors(const DescriptorTable* table) { + call_once(*table->once, AssignDescriptorsImpl, table); } -void AddDescriptors(DescriptorTable* table, const InitFunc* deps, - int num_deps) { +void AddDescriptors(const DescriptorTable* table) { // AddDescriptors is not thread safe. Callers need to ensure calls are // properly serialized. This function is only called pre-main by global // descriptors and we can assume single threaded access or it's called // by AssignDescriptorImpl which uses a mutex to sequence calls. - if (table->is_initialized) return; - table->is_initialized = true; - AddDescriptorsImpl(table, deps, num_deps); -} - -// Separate function because it needs to be a friend of -// GeneratedMessageReflection -void RegisterAllTypesInternal(const Metadata* file_level_metadata, int size) { - for (int i = 0; i < size; i++) { - const GeneratedMessageReflection* reflection = - static_cast( - file_level_metadata[i].reflection); - if (reflection) { - // It's not a map type - MessageFactory::InternalRegisterGeneratedMessage( - file_level_metadata[i].descriptor, - reflection->schema_.default_instance_); - } - } + if (*table->is_initialized) return; + *table->is_initialized = true; + AddDescriptorsImpl(table); } -void RegisterFileLevelMetadata(void* assign_descriptors_table) { - auto table = static_cast(assign_descriptors_table); +void RegisterFileLevelMetadata(const DescriptorTable* table) { AssignDescriptors(table); RegisterAllTypesInternal(table->file_level_metadata, table->num_messages); } diff --git a/src/google/protobuf/generated_message_reflection.h b/src/google/protobuf/generated_message_reflection.h index 177312cf7a..f61dce4091 100644 --- a/src/google/protobuf/generated_message_reflection.h +++ b/src/google/protobuf/generated_message_reflection.h @@ -44,8 +44,8 @@ #include // TODO(jasonh): Remove this once the compiler change to directly include this // is released to components. +#include #include -#include #include #include #include @@ -58,27 +58,17 @@ #error "You cannot SWIG proto headers" #endif -namespace upb { -namespace google_opensource { -class GMR_Handlers; -} // namespace google_opensource -} // namespace upb - namespace google { namespace protobuf { class DescriptorPool; class MapKey; class MapValueRef; +class MessageLayoutInspector; +class Message; +struct Metadata; } // namespace protobuf } // namespace google -namespace google { -namespace protobuf { -namespace flat { -class MetadataBuilder; -} // namespace flat -} // namespace protobuf -} // namespace google namespace google { namespace protobuf { @@ -86,12 +76,9 @@ namespace internal { class DefaultEmptyOneof; class ReflectionAccessor; -// Defined in this file. -class GeneratedMessageReflection; - // Defined in other files. -class ExtensionSet; // extension_set.h -class WeakFieldMap; // weak_field_map.h +class ExtensionSet; // extension_set.h +class WeakFieldMap; // weak_field_map.h // This struct describes the internal layout of the message, hence this is // used to act on the message reflectively. @@ -99,7 +86,7 @@ class WeakFieldMap; // weak_field_map.h // used to obtain pointers to default instances of embedded // messages, which GetMessage() will return if the particular // sub-message has not been initialized yet. (Thus, all -// embedded message fields *must* have non-NULL pointers +// embedded message fields *must* have non-null pointers // in the default instance.) // offsets: An array of ints giving the byte offsets. // For each oneof or weak field, the offset is relative to the @@ -153,7 +140,7 @@ struct ReflectionSchema { if (field->containing_oneof()) { size_t offset = static_cast(field->containing_type()->field_count() + - field->containing_oneof()->index()); + field->containing_oneof()->index()); return OffsetValue(offsets_[offset], field->type()); } else { return GetFieldOffsetNonOneof(field); @@ -173,8 +160,8 @@ struct ReflectionSchema { uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const { return static_cast(oneof_case_offset_) + - static_cast( - static_cast(oneof_descriptor->index()) * sizeof(uint32)); + static_cast(static_cast(oneof_descriptor->index()) * + sizeof(uint32)); } bool HasHasbits() const { return has_bits_offset_ != -1; } @@ -218,7 +205,7 @@ struct ReflectionSchema { // Returns a pointer to the default value for this field. The size and type // of the underlying data depends on the field's type. - const void *GetFieldDefault(const FieldDescriptor* field) const { + const void* GetFieldDefault(const FieldDescriptor* field) const { return reinterpret_cast(default_instance_) + OffsetValue(offsets_[field->index()], field->type()); } @@ -231,7 +218,7 @@ struct ReflectionSchema { // them, ie. // // ReflectionSchema schema = {a, b, c, d, e, ...}; - // private: + // private: const Message* default_instance_; const uint32* offsets_; const uint32* has_bit_indices_; @@ -276,415 +263,16 @@ struct MigrationSchema { int object_size; }; -// THIS CLASS IS NOT INTENDED FOR DIRECT USE. It is intended for use -// by generated code. This class is just a big hack that reduces code -// size. -// -// A GeneratedMessageReflection is an implementation of Reflection -// which expects all fields to be backed by simple variables located in -// memory. The locations are given using a base pointer and a set of -// offsets. -// -// It is required that the user represents fields of each type in a standard -// way, so that GeneratedMessageReflection can cast the void* pointer to -// the appropriate type. For primitive fields and string fields, each field -// should be represented using the obvious C++ primitive type. Enums and -// Messages are different: -// - Singular Message fields are stored as a pointer to a Message. These -// should start out NULL, except for in the default instance where they -// should start out pointing to other default instances. -// - Enum fields are stored as an int. This int must always contain -// a valid value, such that EnumDescriptor::FindValueByNumber() would -// not return NULL. -// - Repeated fields are stored as RepeatedFields or RepeatedPtrFields -// of whatever type the individual field would be. Strings and -// Messages use RepeatedPtrFields while everything else uses -// RepeatedFields. -class GeneratedMessageReflection final : public Reflection { - public: - // Constructs a GeneratedMessageReflection. - // Parameters: - // descriptor: The descriptor for the message type being implemented. - // schema: The description of the internal guts of the message. - // pool: DescriptorPool to search for extension definitions. Only - // used by FindKnownExtensionByName() and - // FindKnownExtensionByNumber(). - // factory: MessageFactory to use to construct extension messages. - GeneratedMessageReflection(const Descriptor* descriptor, - const ReflectionSchema& schema, - const DescriptorPool* pool, - MessageFactory* factory); - - ~GeneratedMessageReflection() override; - - // implements Reflection ------------------------------------------- - - const UnknownFieldSet& GetUnknownFields( - const Message& message) const override; - UnknownFieldSet* MutableUnknownFields(Message* message) const override; - - size_t SpaceUsedLong(const Message& message) const override; - - bool HasField(const Message& message, - const FieldDescriptor* field) const override; - int FieldSize(const Message& message, - const FieldDescriptor* field) const override; - void ClearField(Message* message, - const FieldDescriptor* field) const override; - bool HasOneof(const Message& message, - const OneofDescriptor* oneof_descriptor) const override; - void ClearOneof(Message* message, - const OneofDescriptor* oneof_descriptor) const override; - void RemoveLast(Message* message, - const FieldDescriptor* field) const override; - Message* ReleaseLast(Message* message, - const FieldDescriptor* field) const override; - void Swap(Message* message1, Message* message2) const override; - void SwapFields( - Message* message1, Message* message2, - const std::vector& fields) const override; - void SwapElements(Message* message, const FieldDescriptor* field, int index1, - int index2) const override; - void ListFields(const Message& message, - std::vector* output) const override; - int32 GetInt32(const Message& message, - const FieldDescriptor* field) const override; - int64 GetInt64(const Message& message, - const FieldDescriptor* field) const override; - uint32 GetUInt32(const Message& message, - const FieldDescriptor* field) const override; - uint64 GetUInt64(const Message& message, - const FieldDescriptor* field) const override; - float GetFloat(const Message& message, - const FieldDescriptor* field) const override; - double GetDouble(const Message& message, - const FieldDescriptor* field) const override; - bool GetBool(const Message& message, - const FieldDescriptor* field) const override; - string GetString(const Message& message, - const FieldDescriptor* field) const override; - const string& GetStringReference(const Message& message, - const FieldDescriptor* field, - string* scratch) const override; - const EnumValueDescriptor* GetEnum( - const Message& message, const FieldDescriptor* field) const override; - int GetEnumValue(const Message& message, - const FieldDescriptor* field) const override; - const Message& GetMessage(const Message& message, - const FieldDescriptor* field, - MessageFactory* factory = NULL) const override; - - const FieldDescriptor* GetOneofFieldDescriptor( - const Message& message, - const OneofDescriptor* oneof_descriptor) const override; - - private: - bool ContainsMapKey(const Message& message, const FieldDescriptor* field, - const MapKey& key) const override; - bool InsertOrLookupMapValue(Message* message, const FieldDescriptor* field, - const MapKey& key, - MapValueRef* val) const override; - bool DeleteMapValue(Message* message, const FieldDescriptor* field, - const MapKey& key) const override; - MapIterator MapBegin(Message* message, - const FieldDescriptor* field) const override; - MapIterator MapEnd(Message* message, - const FieldDescriptor* field) const override; - int MapSize(const Message& message, - const FieldDescriptor* field) const override; - - public: - void SetInt32(Message* message, const FieldDescriptor* field, - int32 value) const override; - void SetInt64(Message* message, const FieldDescriptor* field, - int64 value) const override; - void SetUInt32(Message* message, const FieldDescriptor* field, - uint32 value) const override; - void SetUInt64(Message* message, const FieldDescriptor* field, - uint64 value) const override; - void SetFloat(Message* message, const FieldDescriptor* field, - float value) const override; - void SetDouble(Message* message, const FieldDescriptor* field, - double value) const override; - void SetBool(Message* message, const FieldDescriptor* field, - bool value) const override; - void SetString(Message* message, const FieldDescriptor* field, - const string& value) const override; - void SetEnum(Message* message, const FieldDescriptor* field, - const EnumValueDescriptor* value) const override; - void SetEnumValue(Message* message, const FieldDescriptor* field, - int value) const override; - Message* MutableMessage(Message* message, const FieldDescriptor* field, - MessageFactory* factory = NULL) const override; - void SetAllocatedMessage(Message* message, Message* sub_message, - const FieldDescriptor* field) const override; - Message* ReleaseMessage(Message* message, const FieldDescriptor* field, - MessageFactory* factory = NULL) const override; - - int32 GetRepeatedInt32(const Message& message, const FieldDescriptor* field, - int index) const override; - int64 GetRepeatedInt64(const Message& message, const FieldDescriptor* field, - int index) const override; - uint32 GetRepeatedUInt32(const Message& message, const FieldDescriptor* field, - int index) const override; - uint64 GetRepeatedUInt64(const Message& message, const FieldDescriptor* field, - int index) const override; - float GetRepeatedFloat(const Message& message, const FieldDescriptor* field, - int index) const override; - double GetRepeatedDouble(const Message& message, const FieldDescriptor* field, - int index) const override; - bool GetRepeatedBool(const Message& message, const FieldDescriptor* field, - int index) const override; - string GetRepeatedString(const Message& message, const FieldDescriptor* field, - int index) const override; - const string& GetRepeatedStringReference(const Message& message, - const FieldDescriptor* field, - int index, - string* scratch) const override; - const EnumValueDescriptor* GetRepeatedEnum(const Message& message, - const FieldDescriptor* field, - int index) const override; - int GetRepeatedEnumValue(const Message& message, const FieldDescriptor* field, - int index) const override; - const Message& GetRepeatedMessage(const Message& message, - const FieldDescriptor* field, - int index) const override; - - // Set the value of a field. - void SetRepeatedInt32(Message* message, const FieldDescriptor* field, - int index, int32 value) const override; - void SetRepeatedInt64(Message* message, const FieldDescriptor* field, - int index, int64 value) const override; - void SetRepeatedUInt32(Message* message, const FieldDescriptor* field, - int index, uint32 value) const override; - void SetRepeatedUInt64(Message* message, const FieldDescriptor* field, - int index, uint64 value) const override; - void SetRepeatedFloat(Message* message, const FieldDescriptor* field, - int index, float value) const override; - void SetRepeatedDouble(Message* message, const FieldDescriptor* field, - int index, double value) const override; - void SetRepeatedBool(Message* message, const FieldDescriptor* field, - int index, bool value) const override; - void SetRepeatedString(Message* message, const FieldDescriptor* field, - int index, const string& value) const override; - void SetRepeatedEnum(Message* message, const FieldDescriptor* field, - int index, - const EnumValueDescriptor* value) const override; - void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field, - int index, int value) const override; - // Get a mutable pointer to a field with a message type. - Message* MutableRepeatedMessage(Message* message, - const FieldDescriptor* field, - int index) const override; - - void AddInt32(Message* message, const FieldDescriptor* field, - int32 value) const override; - void AddInt64(Message* message, const FieldDescriptor* field, - int64 value) const override; - void AddUInt32(Message* message, const FieldDescriptor* field, - uint32 value) const override; - void AddUInt64(Message* message, const FieldDescriptor* field, - uint64 value) const override; - void AddFloat(Message* message, const FieldDescriptor* field, - float value) const override; - void AddDouble(Message* message, const FieldDescriptor* field, - double value) const override; - void AddBool(Message* message, const FieldDescriptor* field, - bool value) const override; - void AddString(Message* message, const FieldDescriptor* field, - const string& value) const override; - void AddEnum(Message* message, const FieldDescriptor* field, - const EnumValueDescriptor* value) const override; - void AddEnumValue(Message* message, const FieldDescriptor* field, - int value) const override; - Message* AddMessage(Message* message, const FieldDescriptor* field, - MessageFactory* factory = NULL) const override; - void AddAllocatedMessage(Message* message, const FieldDescriptor* field, - Message* new_entry) const override; - - const FieldDescriptor* FindKnownExtensionByName( - const string& name) const override; - const FieldDescriptor* FindKnownExtensionByNumber(int number) const override; - - bool SupportsUnknownEnumValues() const override; - - // This value for arena_offset_ indicates that there is no arena pointer in - // this message (e.g., old generated code). - static const int kNoArenaPointer = -1; - - // This value for unknown_field_offset_ indicates that there is no - // UnknownFieldSet in this message, and that instead, we are using the - // Zero-Overhead Arena Pointer trick. When this is the case, arena_offset_ - // actually indexes to an InternalMetadataWithArena instance, which can return - // either an arena pointer or an UnknownFieldSet or both. It is never the case - // that unknown_field_offset_ == kUnknownFieldSetInMetadata && arena_offset_ - // == kNoArenaPointer. - static const int kUnknownFieldSetInMetadata = -1; - - protected: - void* MutableRawRepeatedField(Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType, int ctype, - const Descriptor* desc) const override; - - const void* GetRawRepeatedField(const Message& message, - const FieldDescriptor* field, - FieldDescriptor::CppType, int ctype, - const Descriptor* desc) const override; - - MessageFactory* GetMessageFactory() const override; - - void* RepeatedFieldData(Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType cpp_type, - const Descriptor* message_type) const override; - - private: - friend class google::protobuf::flat::MetadataBuilder; - friend class ReflectionAccessor; - friend class upb::google_opensource::GMR_Handlers; - - const Descriptor* const descriptor_; - const ReflectionSchema schema_; - const DescriptorPool* const descriptor_pool_; - MessageFactory* const message_factory_; - - // Last non weak field index. This is an optimization when most weak fields - // are at the end of the containing message. If a message proto doesn't - // contain weak fields, then this field equals descriptor_->field_count(). - int last_non_weak_field_index_; - - template - const T& GetRawNonOneof(const Message& message, - const FieldDescriptor* field) const; - template - T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const; - - template - const Type& GetRaw(const Message& message, - const FieldDescriptor* field) const; - template - inline Type* MutableRaw(Message* message, - const FieldDescriptor* field) const; - template - inline const Type& DefaultRaw(const FieldDescriptor* field) const; - - inline const uint32* GetHasBits(const Message& message) const; - inline uint32* MutableHasBits(Message* message) const; - inline uint32 GetOneofCase( - const Message& message, - const OneofDescriptor* oneof_descriptor) const; - inline uint32* MutableOneofCase( - Message* message, - const OneofDescriptor* oneof_descriptor) const; - inline const ExtensionSet& GetExtensionSet(const Message& message) const; - inline ExtensionSet* MutableExtensionSet(Message* message) const; - inline Arena* GetArena(Message* message) const; - - inline const InternalMetadataWithArena& GetInternalMetadataWithArena( - const Message& message) const; - - inline InternalMetadataWithArena* - MutableInternalMetadataWithArena(Message* message) const; - - inline bool IsInlined(const FieldDescriptor* field) const; - - inline bool HasBit(const Message& message, - const FieldDescriptor* field) const; - inline void SetBit(Message* message, - const FieldDescriptor* field) const; - inline void ClearBit(Message* message, - const FieldDescriptor* field) const; - inline void SwapBit(Message* message1, - Message* message2, - const FieldDescriptor* field) const; - - // This function only swaps the field. Should swap corresponding has_bit - // before or after using this function. - void SwapField(Message* message1, - Message* message2, - const FieldDescriptor* field) const; - - void SwapOneofField(Message* message1, - Message* message2, - const OneofDescriptor* oneof_descriptor) const; - - inline bool HasOneofField(const Message& message, - const FieldDescriptor* field) const; - inline void SetOneofCase(Message* message, - const FieldDescriptor* field) const; - inline void ClearOneofField(Message* message, - const FieldDescriptor* field) const; - - template - inline const Type& GetField(const Message& message, - const FieldDescriptor* field) const; - template - inline void SetField(Message* message, - const FieldDescriptor* field, const Type& value) const; - template - inline Type* MutableField(Message* message, - const FieldDescriptor* field) const; - template - inline const Type& GetRepeatedField(const Message& message, - const FieldDescriptor* field, - int index) const; - template - inline const Type& GetRepeatedPtrField(const Message& message, - const FieldDescriptor* field, - int index) const; - template - inline void SetRepeatedField(Message* message, - const FieldDescriptor* field, int index, - Type value) const; - template - inline Type* MutableRepeatedField(Message* message, - const FieldDescriptor* field, - int index) const; - template - inline void AddField(Message* message, - const FieldDescriptor* field, const Type& value) const; - template - inline Type* AddField(Message* message, - const FieldDescriptor* field) const; - - int GetExtensionNumberOrDie(const Descriptor* type) const; - - // Internal versions of EnumValue API perform no checking. Called after checks - // by public methods. - void SetEnumValueInternal(Message* message, - const FieldDescriptor* field, - int value) const; - void SetRepeatedEnumValueInternal(Message* message, - const FieldDescriptor* field, - int index, - int value) const; - void AddEnumValueInternal(Message* message, - const FieldDescriptor* field, - int value) const; - - - Message* UnsafeArenaReleaseMessage(Message* message, - const FieldDescriptor* field, - MessageFactory* factory = NULL) const; - - void UnsafeArenaSetAllocatedMessage(Message* message, - Message* sub_message, - const FieldDescriptor* field) const; - - internal::MapFieldBase* MapData(Message* message, - const FieldDescriptor* field) const override; - - friend inline // inline so nobody can call this function. - void - RegisterAllTypesInternal(const Metadata* file_level_metadata, int size); - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection); -}; - -typedef void (*InitFunc)(); - -struct LIBPROTOBUF_EXPORT AssignDescriptorsTable { - once_flag once; - InitFunc add_descriptors; +struct PROTOBUF_EXPORT DescriptorTable { + bool* is_initialized; + const char* descriptor; const char* filename; + int size; // of serialized descriptor + once_flag* once; + SCCInfoBase* const* init_default_instances; + const DescriptorTable* const* deps; + int num_sccs; + int num_deps; const MigrationSchema* schemas; const Message* const* default_instances; const uint32* offsets; @@ -695,25 +283,24 @@ struct LIBPROTOBUF_EXPORT AssignDescriptorsTable { const ServiceDescriptor** file_level_service_descriptors; }; -void LIBPROTOBUF_EXPORT AssignDescriptors(AssignDescriptorsTable* table); - -struct LIBPROTOBUF_EXPORT DescriptorTable { - bool is_initialized; - InitFunc init_defaults; - const char* descriptor; - const char* filename; - AssignDescriptorsTable* assign_descriptors_table; - int size; // of serialized descriptor -}; +// AssignDescriptors() pulls the compiled FileDescriptor from the DescriptorPool +// and uses it to populate all of the global variables which store pointers to +// the descriptor objects. It also constructs the reflection objects. It is +// called the first time anyone calls descriptor() or GetReflection() on one of +// the types defined in the file. AssignDescriptors() is thread-safe. +void PROTOBUF_EXPORT AssignDescriptors(const DescriptorTable* table); -void LIBPROTOBUF_EXPORT AddDescriptors(DescriptorTable* table, - const InitFunc* deps, - int num_deps); +// AddDescriptors() is a file-level procedure which adds the encoded +// FileDescriptorProto for this .proto file to the global DescriptorPool for +// generated files (DescriptorPool::generated_pool()). It ordinarily runs at +// static initialization time, but is not used at all in LITE_RUNTIME mode. +// AddDescriptors() is *not* thread-safe. +void PROTOBUF_EXPORT AddDescriptors(const DescriptorTable* table); // These cannot be in lite so we put them in the reflection. -LIBPROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset, uint32 tag, - uint32 has_offset, - io::CodedOutputStream* output); +PROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset, + uint32 tag, uint32 has_offset, + io::CodedOutputStream* output); } // namespace internal } // namespace protobuf diff --git a/src/google/protobuf/generated_message_reflection_unittest.cc b/src/google/protobuf/generated_message_reflection_unittest.cc index c74e4f5ee5..eea5da53d7 100644 --- a/src/google/protobuf/generated_message_reflection_unittest.cc +++ b/src/google/protobuf/generated_message_reflection_unittest.cc @@ -61,9 +61,9 @@ namespace protobuf { namespace { // Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes. -const FieldDescriptor* F(const string& name) { +const FieldDescriptor* F(const std::string& name) { const FieldDescriptor* result = - unittest::TestAllTypes::descriptor()->FindFieldByName(name); + unittest::TestAllTypes::descriptor()->FindFieldByName(name); GOOGLE_CHECK(result != NULL); return result; } @@ -72,7 +72,7 @@ TEST(GeneratedMessageReflectionTest, Defaults) { // Check that all default values are set correctly in the initial message. unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); reflection_tester.ExpectClearViaReflection(message); @@ -96,7 +96,7 @@ TEST(GeneratedMessageReflectionTest, Accessors) { // values. unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); reflection_tester.SetAllFieldsViaReflection(&message); TestUtil::ExpectAllFieldsSet(message); @@ -107,24 +107,26 @@ TEST(GeneratedMessageReflectionTest, Accessors) { } TEST(GeneratedMessageReflectionTest, GetStringReference) { - // Test that GetStringReference() returns the underlying string when it is - // a normal string field. + // Test that GetStringReference() returns the underlying string when it + // is a normal string field. unittest::TestAllTypes message; message.set_optional_string("foo"); message.add_repeated_string("foo"); const Reflection* reflection = message.GetReflection(); - string scratch; + std::string scratch; - EXPECT_EQ(&message.optional_string(), + EXPECT_EQ( + &message.optional_string(), &reflection->GetStringReference(message, F("optional_string"), &scratch)) - << "For simple string fields, GetStringReference() should return a " - "reference to the underlying string."; + << "For simple string fields, GetStringReference() should return a " + "reference to the underlying string."; EXPECT_EQ(&message.repeated_string(0), - &reflection->GetRepeatedStringReference(message, F("repeated_string"), - 0, &scratch)) - << "For simple string fields, GetRepeatedStringReference() should return " - "a reference to the underlying string."; + &reflection->GetRepeatedStringReference( + message, F("repeated_string"), 0, &scratch)) + << "For simple string fields, GetRepeatedStringReference() should " + "return " + "a reference to the underlying string."; } @@ -133,7 +135,7 @@ TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) { // embedded message does NOT return the default instance. unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); TestUtil::SetAllFields(&message); message.Clear(); @@ -150,7 +152,6 @@ TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) { &reflection->GetMessage(message, F("optional_import_message"))); } - TEST(GeneratedMessageReflectionTest, Swap) { unittest::TestAllTypes message1; unittest::TestAllTypes message2; @@ -319,7 +320,7 @@ TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) { TEST(GeneratedMessageReflectionTest, RemoveLast) { unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); TestUtil::SetAllFields(&message); @@ -331,7 +332,7 @@ TEST(GeneratedMessageReflectionTest, RemoveLast) { TEST(GeneratedMessageReflectionTest, RemoveLastExtensions) { unittest::TestAllExtensions message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); TestUtil::SetAllExtensions(&message); @@ -376,21 +377,20 @@ TEST(GeneratedMessageReflectionTest, ReleaseLastExtensions) { // Now test that we actually release the right message. message.Clear(); TestUtil::SetAllExtensions(&message); - ASSERT_EQ(2, message.ExtensionSize( - unittest::repeated_foreign_message_extension)); - const protobuf_unittest::ForeignMessage* expected = message.MutableExtension( - unittest::repeated_foreign_message_extension, 1); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_foreign_message_extension)); + const protobuf_unittest::ForeignMessage* expected = + message.MutableExtension(unittest::repeated_foreign_message_extension, 1); std::unique_ptr released(message.GetReflection()->ReleaseLast( &message, descriptor->file()->FindExtensionByName( "repeated_foreign_message_extension"))); EXPECT_EQ(expected, released.get()); - } TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) { unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); TestUtil::SetAllFields(&message); @@ -406,7 +406,7 @@ TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) { TEST(GeneratedMessageReflectionTest, SwapRepeatedElementsExtension) { unittest::TestAllExtensions message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); TestUtil::SetAllExtensions(&message); @@ -424,7 +424,7 @@ TEST(GeneratedMessageReflectionTest, Extensions) { // values. unittest::TestAllExtensions message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); reflection_tester.SetAllFieldsViaReflection(&message); TestUtil::ExpectAllExtensionsSet(message); @@ -436,14 +436,14 @@ TEST(GeneratedMessageReflectionTest, Extensions) { TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) { const Reflection* reflection = - unittest::TestAllExtensions::default_instance().GetReflection(); + unittest::TestAllExtensions::default_instance().GetReflection(); const FieldDescriptor* extension1 = - unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( - "optional_int32_extension"); + unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( + "optional_int32_extension"); const FieldDescriptor* extension2 = - unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( - "repeated_string_extension"); + unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( + "repeated_string_extension"); EXPECT_EQ(extension1, reflection->FindKnownExtensionByNumber(extension1->number())); @@ -455,20 +455,21 @@ TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) { // Extensions of TestAllExtensions should not show up as extensions of // other types. - EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()-> - FindKnownExtensionByNumber(extension1->number()) == NULL); + EXPECT_TRUE(unittest::TestAllTypes::default_instance() + .GetReflection() + ->FindKnownExtensionByNumber(extension1->number()) == NULL); } TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) { const Reflection* reflection = - unittest::TestAllExtensions::default_instance().GetReflection(); + unittest::TestAllExtensions::default_instance().GetReflection(); const FieldDescriptor* extension1 = - unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( - "optional_int32_extension"); + unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( + "optional_int32_extension"); const FieldDescriptor* extension2 = - unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( - "repeated_string_extension"); + unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( + "repeated_string_extension"); EXPECT_EQ(extension1, reflection->FindKnownExtensionByName(extension1->full_name())); @@ -480,8 +481,9 @@ TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) { // Extensions of TestAllExtensions should not show up as extensions of // other types. - EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()-> - FindKnownExtensionByName(extension1->full_name()) == NULL); + EXPECT_TRUE(unittest::TestAllTypes::default_instance() + .GetReflection() + ->FindKnownExtensionByName(extension1->full_name()) == NULL); } TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) { @@ -489,7 +491,7 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) { unittest::TestAllTypes from_message2; unittest::TestAllTypes to_message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); reflection_tester.SetAllFieldsViaReflection(&from_message1); reflection_tester.SetAllFieldsViaReflection(&from_message2); @@ -524,7 +526,7 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedMessageOnArenaTest) { unittest::TestAllTypes* to_message = Arena::CreateMessage(&arena); TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); reflection_tester.SetAllFieldsViaReflection(&from_message1); reflection_tester.SetAllFieldsViaReflection(&from_message2); @@ -557,7 +559,7 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageTest) { unittest::TestAllExtensions from_message2; unittest::TestAllExtensions to_message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); reflection_tester.SetAllFieldsViaReflection(&from_message1); reflection_tester.SetAllFieldsViaReflection(&from_message2); @@ -592,7 +594,7 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageOnArenaTest) { unittest::TestAllExtensions from_message1; unittest::TestAllExtensions from_message2; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); reflection_tester.SetAllFieldsViaReflection(&from_message1); reflection_tester.SetAllFieldsViaReflection(&from_message2); @@ -631,8 +633,8 @@ TEST(GeneratedMessageReflectionTest, AddRepeatedMessage) { unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName( "bb"); - Message* nested = reflection->AddMessage( - &message, F("repeated_nested_message")); + Message* nested = + reflection->AddMessage(&message, F("repeated_nested_message")); nested_reflection->SetInt32(nested, nested_bb, 11); EXPECT_EQ(11, message.repeated_nested_message(0).bb()); @@ -666,7 +668,8 @@ TEST(GeneratedMessageReflectionTest, AddAllocatedMessage) { unittest::TestAllTypes::NestedMessage* nested = new unittest::TestAllTypes::NestedMessage(); nested->set_bb(11); - reflection->AddAllocatedMessage(&message, F("repeated_nested_message"), nested); + reflection->AddAllocatedMessage(&message, F("repeated_nested_message"), + nested); EXPECT_EQ(1, message.repeated_nested_message_size()); EXPECT_EQ(11, message.repeated_nested_message(0).bb()); } @@ -687,62 +690,68 @@ TEST(GeneratedMessageReflectionTest, Oneof) { const Reflection* reflection = message.GetReflection(); // Check default values. - EXPECT_EQ(0, reflection->GetInt32( - message, descriptor->FindFieldByName("foo_int"))); - EXPECT_EQ("", reflection->GetString( - message, descriptor->FindFieldByName("foo_string"))); + EXPECT_EQ( + 0, reflection->GetInt32(message, descriptor->FindFieldByName("foo_int"))); EXPECT_EQ("", reflection->GetString( - message, descriptor->FindFieldByName("foo_cord"))); + message, descriptor->FindFieldByName("foo_string"))); + EXPECT_EQ("", reflection->GetString(message, + descriptor->FindFieldByName("foo_cord"))); EXPECT_EQ("", reflection->GetString( - message, descriptor->FindFieldByName("foo_string_piece"))); + message, descriptor->FindFieldByName("foo_string_piece"))); EXPECT_EQ("", reflection->GetString( - message, descriptor->FindFieldByName("foo_bytes"))); - EXPECT_EQ(unittest::TestOneof2::FOO, reflection->GetEnum( - message, descriptor->FindFieldByName("foo_enum"))->number()); + message, descriptor->FindFieldByName("foo_bytes"))); + EXPECT_EQ( + unittest::TestOneof2::FOO, + reflection->GetEnum(message, descriptor->FindFieldByName("foo_enum")) + ->number()); EXPECT_EQ(&unittest::TestOneof2::NestedMessage::default_instance(), &reflection->GetMessage( message, descriptor->FindFieldByName("foo_message"))); EXPECT_EQ(&unittest::TestOneof2::FooGroup::default_instance(), - &reflection->GetMessage( - message, descriptor->FindFieldByName("foogroup"))); + &reflection->GetMessage(message, + descriptor->FindFieldByName("foogroup"))); EXPECT_NE(&unittest::TestOneof2::FooGroup::default_instance(), &reflection->GetMessage( message, descriptor->FindFieldByName("foo_lazy_message"))); - EXPECT_EQ(5, reflection->GetInt32( - message, descriptor->FindFieldByName("bar_int"))); + EXPECT_EQ( + 5, reflection->GetInt32(message, descriptor->FindFieldByName("bar_int"))); EXPECT_EQ("STRING", reflection->GetString( - message, descriptor->FindFieldByName("bar_string"))); + message, descriptor->FindFieldByName("bar_string"))); EXPECT_EQ("CORD", reflection->GetString( - message, descriptor->FindFieldByName("bar_cord"))); - EXPECT_EQ("SPIECE", reflection->GetString( - message, descriptor->FindFieldByName("bar_string_piece"))); + message, descriptor->FindFieldByName("bar_cord"))); + EXPECT_EQ("SPIECE", + reflection->GetString( + message, descriptor->FindFieldByName("bar_string_piece"))); EXPECT_EQ("BYTES", reflection->GetString( - message, descriptor->FindFieldByName("bar_bytes"))); - EXPECT_EQ(unittest::TestOneof2::BAR, reflection->GetEnum( - message, descriptor->FindFieldByName("bar_enum"))->number()); + message, descriptor->FindFieldByName("bar_bytes"))); + EXPECT_EQ( + unittest::TestOneof2::BAR, + reflection->GetEnum(message, descriptor->FindFieldByName("bar_enum")) + ->number()); // Check Set functions. - reflection->SetInt32( - &message, descriptor->FindFieldByName("foo_int"), 123); - EXPECT_EQ(123, reflection->GetInt32( - message, descriptor->FindFieldByName("foo_int"))); - reflection->SetString( - &message, descriptor->FindFieldByName("foo_string"), "abc"); + reflection->SetInt32(&message, descriptor->FindFieldByName("foo_int"), 123); + EXPECT_EQ(123, reflection->GetInt32(message, + descriptor->FindFieldByName("foo_int"))); + reflection->SetString(&message, descriptor->FindFieldByName("foo_string"), + "abc"); EXPECT_EQ("abc", reflection->GetString( - message, descriptor->FindFieldByName("foo_string"))); - reflection->SetString( - &message, descriptor->FindFieldByName("foo_bytes"), "bytes"); + message, descriptor->FindFieldByName("foo_string"))); + reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes"), + "bytes"); EXPECT_EQ("bytes", reflection->GetString( - message, descriptor->FindFieldByName("foo_bytes"))); - reflection->SetString( - &message, descriptor->FindFieldByName("bar_cord"), "change_cord"); - EXPECT_EQ("change_cord", reflection->GetString( - message, descriptor->FindFieldByName("bar_cord"))); - reflection->SetString( - &message, descriptor->FindFieldByName("bar_string_piece"), - "change_spiece"); - EXPECT_EQ("change_spiece", reflection->GetString( - message, descriptor->FindFieldByName("bar_string_piece"))); + message, descriptor->FindFieldByName("foo_bytes"))); + reflection->SetString(&message, descriptor->FindFieldByName("bar_cord"), + "change_cord"); + EXPECT_EQ( + "change_cord", + reflection->GetString(message, descriptor->FindFieldByName("bar_cord"))); + reflection->SetString(&message, + descriptor->FindFieldByName("bar_string_piece"), + "change_spiece"); + EXPECT_EQ("change_spiece", + reflection->GetString( + message, descriptor->FindFieldByName("bar_string_piece"))); message.clear_foo(); message.clear_bar(); @@ -767,8 +776,8 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) { TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1); TestUtil::ReflectionTester:: - SetAllocatedOptionalMessageFieldsToMessageViaReflection( - &from_message1, &to_message); + SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message1, + &to_message); const Message& sub_message = reflection->GetMessage( to_message, descriptor->FindFieldByName("foo_lazy_message")); released = reflection->ReleaseMessage( @@ -779,12 +788,12 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) { TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2); - reflection->MutableMessage( - &from_message2, descriptor->FindFieldByName("foo_message")); + reflection->MutableMessage(&from_message2, + descriptor->FindFieldByName("foo_message")); TestUtil::ReflectionTester:: - SetAllocatedOptionalMessageFieldsToMessageViaReflection( - &from_message2, &to_message); + SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message2, + &to_message); const Message& sub_message2 = reflection->GetMessage( to_message, descriptor->FindFieldByName("foo_message")); @@ -815,8 +824,8 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) { TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1); TestUtil::ReflectionTester:: - SetAllocatedOptionalMessageFieldsToMessageViaReflection( - &from_message1, to_message); + SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message1, + to_message); const Message& sub_message = reflection->GetMessage( *to_message, descriptor->FindFieldByName("foo_lazy_message")); released = reflection->ReleaseMessage( @@ -829,12 +838,12 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) { TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2); - reflection->MutableMessage( - &from_message2, descriptor->FindFieldByName("foo_message")); + reflection->MutableMessage(&from_message2, + descriptor->FindFieldByName("foo_message")); TestUtil::ReflectionTester:: - SetAllocatedOptionalMessageFieldsToMessageViaReflection( - &from_message2, to_message); + SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message2, + to_message); const Message& sub_message2 = reflection->GetMessage( *to_message, descriptor->FindFieldByName("foo_message")); @@ -847,11 +856,10 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) { delete released; } - TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) { unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); // When nothing is set, we expect all released messages to be NULL. reflection_tester.ExpectMessagesReleasedViaReflection( @@ -877,7 +885,7 @@ TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) { TEST(GeneratedMessageReflectionTest, ReleaseExtensionMessageTest) { unittest::TestAllExtensions message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); // When nothing is set, we expect all released messages to be NULL. reflection_tester.ExpectMessagesReleasedViaReflection( @@ -999,39 +1007,41 @@ TEST(GeneratedMessageReflectionTest, UsageErrors) { // Testing every single failure mode would be too much work. Let's just // check a few. EXPECT_DEATH( - reflection->GetInt32( - message, descriptor->FindFieldByName("optional_int64")), - "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::GetInt32\n" - " Message type: protobuf_unittest\\.TestAllTypes\n" - " Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n" - " Problem : Field is not the right type for this message:\n" - " Expected : CPPTYPE_INT32\n" - " Field type: CPPTYPE_INT64"); - EXPECT_DEATH( - reflection->GetInt32( - message, descriptor->FindFieldByName("repeated_int32")), - "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::GetInt32\n" - " Message type: protobuf_unittest.TestAllTypes\n" - " Field : protobuf_unittest.TestAllTypes.repeated_int32\n" - " Problem : Field is repeated; the method requires a singular field."); + reflection->GetInt32(message, + descriptor->FindFieldByName("optional_int64")), + "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::GetInt32\n" + " Message type: protobuf_unittest\\.TestAllTypes\n" + " Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n" + " Problem : Field is not the right type for this message:\n" + " Expected : CPPTYPE_INT32\n" + " Field type: CPPTYPE_INT64"); + EXPECT_DEATH(reflection->GetInt32( + message, descriptor->FindFieldByName("repeated_int32")), + "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::GetInt32\n" + " Message type: protobuf_unittest.TestAllTypes\n" + " Field : protobuf_unittest.TestAllTypes.repeated_int32\n" + " Problem : Field is repeated; the method requires a " + "singular field."); EXPECT_DEATH( - reflection->GetInt32( - message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")), - "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::GetInt32\n" - " Message type: protobuf_unittest.TestAllTypes\n" - " Field : protobuf_unittest.ForeignMessage.c\n" - " Problem : Field does not match message type."); + reflection->GetInt32( + message, + unittest::ForeignMessage::descriptor()->FindFieldByName("c")), + "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::GetInt32\n" + " Message type: protobuf_unittest.TestAllTypes\n" + " Field : protobuf_unittest.ForeignMessage.c\n" + " Problem : Field does not match message type."); EXPECT_DEATH( - reflection->HasField( - message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")), - "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::HasField\n" - " Message type: protobuf_unittest.TestAllTypes\n" - " Field : protobuf_unittest.ForeignMessage.c\n" - " Problem : Field does not match message type."); + reflection->HasField( + message, + unittest::ForeignMessage::descriptor()->FindFieldByName("c")), + "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::HasField\n" + " Message type: protobuf_unittest.TestAllTypes\n" + " Field : protobuf_unittest.ForeignMessage.c\n" + " Problem : Field does not match message type."); #undef f } diff --git a/src/google/protobuf/generated_message_table_driven.cc b/src/google/protobuf/generated_message_table_driven.cc index d4450a6cd5..fb82a8ec37 100644 --- a/src/google/protobuf/generated_message_table_driven.cc +++ b/src/google/protobuf/generated_message_table_driven.cc @@ -39,7 +39,6 @@ #include #include #include -#include namespace google { namespace protobuf { @@ -53,33 +52,35 @@ UnknownFieldSet* MutableUnknownFields(MessageLite* msg, int64 arena_offset) { } struct UnknownFieldHandler { + // TODO(mvels): consider renaming UnknownFieldHandler to (TableDrivenTraits?), + // and conflating InternalMetaData into it, simplifying the template. + static constexpr bool IsLite() { return false; } + static bool Skip(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, - int tag) { + io::CodedInputStream* input, int tag) { GOOGLE_DCHECK(table.unknown_field_set); return WireFormat::SkipField(input, tag, - MutableUnknownFields(msg, table.arena_offset)); + MutableUnknownFields(msg, table.arena_offset)); } - static void Varint(MessageLite* msg, const ParseTable& table, - int tag, int value) { + static void Varint(MessageLite* msg, const ParseTable& table, int tag, + int value) { GOOGLE_DCHECK(table.unknown_field_set); - MutableUnknownFields(msg, table.arena_offset)->AddVarint( - WireFormatLite::GetTagFieldNumber(tag), value); + MutableUnknownFields(msg, table.arena_offset) + ->AddVarint(WireFormatLite::GetTagFieldNumber(tag), value); } - static bool ParseExtension( - MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, int tag) { + 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 Message* prototype = down_cast( - table.default_instance()); + const Message* prototype = + down_cast(table.default_instance()); GOOGLE_DCHECK(prototype != NULL); GOOGLE_DCHECK(table.unknown_field_set); @@ -92,8 +93,8 @@ struct UnknownFieldHandler { } // namespace -bool MergePartialFromCodedStream( - MessageLite* msg, const ParseTable& table, io::CodedInputStream* input) { +bool MergePartialFromCodedStream(MessageLite* msg, const ParseTable& table, + io::CodedInputStream* input) { return MergePartialFromCodedStreamImpl(msg, table, input); diff --git a/src/google/protobuf/generated_message_table_driven.h b/src/google/protobuf/generated_message_table_driven.h index eb85ef5bde..1394ab1440 100644 --- a/src/google/protobuf/generated_message_table_driven.h +++ b/src/google/protobuf/generated_message_table_driven.h @@ -36,7 +36,6 @@ #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 @@ -52,6 +51,8 @@ #error "You cannot SWIG proto headers" #endif +#include + namespace google { namespace protobuf { namespace internal { @@ -79,6 +80,40 @@ enum ProcessingTypes { 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 distinquish the same types if they have different wire format. + enum { + kCordType = 19, + kStringPieceType = 20, + kInlinedType = 21, + kNumTypes = 21, + 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. @@ -172,21 +207,18 @@ 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, ""); -#ifndef __NVCC__ // This assertion currently fails under NVCC. -static_assert(std::is_pod::value, ""); -#endif - // 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); + io::CodedInputStream* input); template bool ParseMap(io::CodedInputStream* input, void* map_field) { @@ -199,8 +231,109 @@ bool ParseMap(io::CodedInputStream* input, void* 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/src/google/protobuf/generated_message_table_driven_lite.cc b/src/google/protobuf/generated_message_table_driven_lite.cc index 83c8180600..82de6bc9b0 100644 --- a/src/google/protobuf/generated_message_table_driven_lite.cc +++ b/src/google/protobuf/generated_message_table_driven_lite.cc @@ -36,7 +36,6 @@ #include #include #include -#include namespace google { namespace protobuf { @@ -44,15 +43,18 @@ namespace internal { namespace { -string* MutableUnknownFields(MessageLite* msg, int64 arena_offset) { +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) { + io::CodedInputStream* input, int tag) { GOOGLE_DCHECK(!table.unknown_field_set); io::StringOutputStream unknown_fields_string( MutableUnknownFields(msg, table.arena_offset)); @@ -62,8 +64,8 @@ struct UnknownFieldHandlerLite { &unknown_fields_stream); } - static void Varint(MessageLite* msg, const ParseTable& table, - int tag, int value) { + static void Varint(MessageLite* msg, const ParseTable& table, int tag, + int value) { GOOGLE_DCHECK(!table.unknown_field_set); io::StringOutputStream unknown_fields_string( @@ -73,9 +75,8 @@ struct UnknownFieldHandlerLite { unknown_fields_stream.WriteVarint32(value); } - static bool ParseExtension( - MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, int tag) { + 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; @@ -87,15 +88,15 @@ struct UnknownFieldHandlerLite { 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); + return extensions->ParseField(tag, input, prototype, + &unknown_fields_stream); } }; } // namespace -bool MergePartialFromCodedStreamLite( - MessageLite* msg, const ParseTable& table, io::CodedInputStream* input) { +bool MergePartialFromCodedStreamLite(MessageLite* msg, const ParseTable& table, + io::CodedInputStream* input) { return MergePartialFromCodedStreamImpl( msg, table, input); diff --git a/src/google/protobuf/generated_message_table_driven_lite.h b/src/google/protobuf/generated_message_table_driven_lite.h index 4b461f1b3c..8cf177be46 100644 --- a/src/google/protobuf/generated_message_table_driven_lite.h +++ b/src/google/protobuf/generated_message_table_driven_lite.h @@ -40,10 +40,11 @@ #include #include #include -#include #include +#include + namespace google { namespace protobuf { namespace internal { @@ -83,7 +84,7 @@ inline const Type* Raw(const MessageLite* msg, int64 offset) { template inline Arena* GetArena(MessageLite* msg, int64 arena_offset) { - if (GOOGLE_PREDICT_FALSE(arena_offset == -1)) { + if (PROTOBUF_PREDICT_FALSE(arena_offset == -1)) { return NULL; } @@ -101,7 +102,7 @@ inline ExtensionSet* GetExtensionSet(MessageLite* msg, int64 extension_offset) { template inline Type* AddField(MessageLite* msg, int64 offset) { static_assert(std::is_pod::value || - std::is_same::value, + std::is_same::value, "Do not assign"); RepeatedField* repeated = Raw>(msg, offset); @@ -109,9 +110,9 @@ inline Type* AddField(MessageLite* msg, int64 offset) { } template <> -inline string* AddField(MessageLite* msg, int64 offset) { - RepeatedPtrField* repeated = - Raw>(msg, offset); +inline std::string* AddField(MessageLite* msg, int64 offset) { + RepeatedPtrField* repeated = + Raw>(msg, offset); return repeated->Add(); } @@ -156,7 +157,7 @@ inline void SetOneofField(MessageLite* msg, uint32* oneof_case, // 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) { + MessageLite* msg) { switch (field.processing_type & kTypeMask) { case WireFormatLite::TYPE_MESSAGE: if (arena == NULL) { @@ -205,11 +206,11 @@ inline void ResetOneofField(const ParseTable& table, int field_number, switch (field_type) { case ProcessingType_STRING: Raw(msg, offset) - ->UnsafeSetDefault(static_cast(default_ptr)); + ->UnsafeSetDefault(static_cast(default_ptr)); break; case ProcessingType_INLINED: new (Raw(msg, offset)) - InlinedStringField(*static_cast(default_ptr)); + InlinedStringField(*static_cast(default_ptr)); break; case ProcessingType_MESSAGE: MessageLite** submessage = Raw(msg, offset); @@ -220,25 +221,28 @@ inline void ResetOneofField(const ParseTable& table, int field_number, } } -template +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 - const char* sdata; - size_t size; -#endif + constexpr bool kValidateUtf8 = is_string_type; +#else + constexpr bool kValidateUtf8 = false; +#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED switch (ctype) { case StringType_INLINED: { - InlinedStringField* s; + InlinedStringField* s = nullptr; switch (cardinality) { case Cardinality_SINGULAR: // TODO(ckennelly): Is this optimal? - s = MutableField( - msg, has_bits, has_bit_index, offset); + s = MutableField(msg, has_bits, has_bit_index, + offset); break; case Cardinality_REPEATED: s = AddField(msg, offset); @@ -248,56 +252,55 @@ static inline bool HandleString(io::CodedInputStream* input, MessageLite* msg, break; } GOOGLE_DCHECK(s != nullptr); - ::std::string* value = s->MutableNoArena(NULL); - - if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadString(input, value))) { + std::string* value = s->MutableNoArena(NULL); + if (PROTOBUF_PREDICT_FALSE(!WireFormatLite::ReadString(input, value))) { return false; } - -#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - sdata = value->data(); - size = value->size(); -#endif + utf8_string_data = *value; break; } case StringType_STRING: { - string* value; switch (cardinality) { - case Cardinality_SINGULAR: - // TODO(ckennelly): Is this optimal? - value = - MutableField(msg, has_bits, has_bit_index, offset) - ->Mutable(static_cast(default_ptr), arena); - break; - case Cardinality_REPEATED: - value = AddField(msg, offset); - break; - case Cardinality_ONEOF: - value = Raw(msg, offset) - ->Mutable(static_cast(default_ptr), arena); - break; - } - GOOGLE_DCHECK(value != nullptr); - - if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadString(input, value))) { - return false; + case Cardinality_SINGULAR: { + ArenaStringPtr* field = MutableField( + msg, has_bits, has_bit_index, offset); + std::string* value = field->Mutable( + 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->Mutable( + static_cast(default_ptr), arena); + if (PROTOBUF_PREDICT_FALSE( + !WireFormatLite::ReadString(input, value))) { + return false; + } + utf8_string_data = field->Get(); + } break; } - -#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - sdata = value->data(); - size = value->size(); -#endif break; } } -#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - if (validate) { - WireFormatLite::VerifyUtf8String(sdata, size, WireFormatLite::PARSE, - field_name); + if (kValidateUtf8) { + // TODO(b/118759213): fail if proto3 + WireFormatLite::VerifyUtf8String(utf8_string_data.data(), + utf8_string_data.length(), + WireFormatLite::PARSE, field_name); } -#endif - return true; } @@ -308,7 +311,7 @@ inline bool HandleEnum(const ParseTable& table, io::CodedInputStream* input, uint32 presence_index, int64 offset, uint32 tag, int field_number) { int value; - if (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( (!WireFormatLite::ReadPrimitive( input, &value)))) { return false; @@ -316,7 +319,7 @@ inline bool HandleEnum(const ParseTable& table, io::CodedInputStream* input, AuxillaryParseTableField::EnumValidator validator = table.aux[field_number].enums.validator; - if (validator(value)) { + if (validator == nullptr || validator(value)) { switch (cardinality) { case Cardinality_SINGULAR: SetField(msg, presence, presence_index, offset, value); @@ -369,9 +372,11 @@ class MergePartialFromCodedStreamHelper { } }; -template -bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input) { +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. // @@ -381,20 +386,19 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, GOOGLE_DCHECK(has_bits != NULL); while (true) { - uint32 tag = input->ReadTag(); - + uint32 tag = input->ReadTagWithCutoffNoLastTag(kMaxTag).first; const WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag); const int field_number = WireFormatLite::GetTagFieldNumber(tag); - if (field_number > table.max_field_number) { + 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 (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( !UnknownFieldHandler::Skip(msg, table, input, tag))) { return false; } @@ -422,7 +426,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, #define HANDLE_TYPE(TYPE, CPPTYPE) \ case (WireFormatLite::TYPE_##TYPE): { \ CPPTYPE value; \ - if (GOOGLE_PREDICT_FALSE( \ + if (PROTOBUF_PREDICT_FALSE( \ (!WireFormatLite::ReadPrimitive< \ CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)))) { \ return false; \ @@ -432,8 +436,8 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, } \ case (WireFormatLite::TYPE_##TYPE) | kRepeatedMask: { \ RepeatedField* values = Raw>(msg, offset); \ - if (GOOGLE_PREDICT_FALSE((!WireFormatLite::ReadRepeatedPrimitive< \ - CPPTYPE, WireFormatLite::TYPE_##TYPE>( \ + if (PROTOBUF_PREDICT_FALSE((!WireFormatLite::ReadRepeatedPrimitive< \ + CPPTYPE, WireFormatLite::TYPE_##TYPE>( \ data->tag_size, tag, input, values)))) { \ return false; \ } \ @@ -442,7 +446,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, case (WireFormatLite::TYPE_##TYPE) | kOneofMask: { \ uint32* oneof_case = Raw(msg, table.oneof_case_offset); \ CPPTYPE value; \ - if (GOOGLE_PREDICT_FALSE( \ + if (PROTOBUF_PREDICT_FALSE( \ (!WireFormatLite::ReadPrimitive< \ CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)))) { \ return false; \ @@ -474,16 +478,17 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, case WireFormatLite::TYPE_BYTES: #ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED case WireFormatLite::TYPE_STRING: -#endif +#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED { Arena* const arena = GetArena(msg, table.arena_offset); const void* default_ptr = table.aux[field_number].strings.default_ptr; - if (GOOGLE_PREDICT_FALSE(( - !HandleString( + if (PROTOBUF_PREDICT_FALSE( + (!HandleString( input, msg, arena, has_bits, presence_index, offset, - default_ptr, NULL)))) { + default_ptr, nullptr)))) { return false; } break; @@ -491,16 +496,17 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, case TYPE_BYTES_INLINED: #ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED case TYPE_STRING_INLINED: -#endif +#endif // !GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED { Arena* const arena = GetArena(msg, table.arena_offset); const void* default_ptr = table.aux[field_number].strings.default_ptr; - if (GOOGLE_PREDICT_FALSE((!HandleString( - input, msg, arena, has_bits, presence_index, offset, - default_ptr, NULL)))) { + if (PROTOBUF_PREDICT_FALSE( + (!HandleString( + input, msg, arena, has_bits, presence_index, offset, + default_ptr, nullptr)))) { return false; } break; @@ -508,7 +514,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, case WireFormatLite::TYPE_BYTES | kOneofMask: #ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED case WireFormatLite::TYPE_STRING | kOneofMask: -#endif +#endif // !GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED { Arena* const arena = GetArena(msg, table.arena_offset); @@ -519,10 +525,11 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, table, field_number, arena, msg, oneof_case + presence_index, offset, default_ptr); - if (GOOGLE_PREDICT_FALSE( - (!HandleString( - input, msg, arena, has_bits, presence_index, offset, - default_ptr, NULL)))) { + if (PROTOBUF_PREDICT_FALSE( + (!HandleString(input, msg, arena, has_bits, + presence_index, offset, + default_ptr, nullptr)))) { return false; } break; @@ -532,17 +539,17 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, #ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED case (WireFormatLite::TYPE_STRING) | kRepeatedMask: case TYPE_STRING_INLINED | kRepeatedMask: -#endif +#endif // !GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED { Arena* const arena = GetArena(msg, table.arena_offset); - const void* default_ptr = - table.aux[field_number].strings.default_ptr; + const void* default_ptr = table.aux[field_number].strings.default_ptr; - if (GOOGLE_PREDICT_FALSE(( - !HandleString( + if (PROTOBUF_PREDICT_FALSE( + (!HandleString( input, msg, arena, has_bits, presence_index, offset, - default_ptr, NULL)))) { + default_ptr, nullptr)))) { return false; } break; @@ -554,8 +561,9 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, const void* default_ptr = table.aux[field_number].strings.default_ptr; const char* field_name = table.aux[field_number].strings.field_name; - if (GOOGLE_PREDICT_FALSE( - (!HandleString( + if (PROTOBUF_PREDICT_FALSE( + (!HandleString( input, msg, arena, has_bits, presence_index, offset, default_ptr, field_name)))) { return false; @@ -569,8 +577,9 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, const void* default_ptr = table.aux[field_number].strings.default_ptr; const char* field_name = table.aux[field_number].strings.field_name; - if (GOOGLE_PREDICT_FALSE( - (!HandleString( + if (PROTOBUF_PREDICT_FALSE( + (!HandleString( input, msg, arena, has_bits, presence_index, offset, default_ptr, field_name)))) { return false; @@ -588,17 +597,18 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, table, field_number, arena, msg, oneof_case + presence_index, offset, default_ptr); - if (GOOGLE_PREDICT_FALSE( - (!HandleString( + if (PROTOBUF_PREDICT_FALSE( + (!HandleString( input, msg, arena, has_bits, presence_index, offset, default_ptr, field_name)))) { return false; } break; } -#endif +#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED case WireFormatLite::TYPE_ENUM: { - if (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( (!HandleEnum( table, input, msg, has_bits, presence_index, offset, tag, @@ -608,7 +618,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, break; } case WireFormatLite::TYPE_ENUM | kRepeatedMask: { - if (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( (!HandleEnum( table, input, msg, has_bits, presence_index, offset, tag, @@ -619,7 +629,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, } case WireFormatLite::TYPE_ENUM | kOneofMask: { uint32* oneof_case = Raw(msg, table.oneof_case_offset); - if (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( (!HandleEnum(table, input, msg, oneof_case, presence_index, offset, tag, @@ -642,7 +652,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, *submsg_holder = submsg; } - if (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( !WireFormatLite::ReadGroup(field_number, input, submsg))) { return false; } @@ -658,7 +668,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, MessageLite* submsg = MergePartialFromCodedStreamHelper::Add(field, prototype); - if (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( !WireFormatLite::ReadGroup(field_number, input, submsg))) { return false; } @@ -682,7 +692,8 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, *submsg_holder = submsg; } - if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadMessage(input, submsg))) { + if (PROTOBUF_PREDICT_FALSE( + !WireFormatLite::ReadMessage(input, submsg))) { return false; } @@ -701,7 +712,8 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, MessageLite* submsg = MergePartialFromCodedStreamHelper::Add(field, prototype); - if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadMessage(input, submsg))) { + if (PROTOBUF_PREDICT_FALSE( + !WireFormatLite::ReadMessage(input, submsg))) { return false; } @@ -717,7 +729,8 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, offset, NULL); MessageLite* submsg = *submsg_holder; - if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadMessage(input, submsg))) { + if (PROTOBUF_PREDICT_FALSE( + !WireFormatLite::ReadMessage(input, submsg))) { return false; } @@ -730,8 +743,9 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, const void* default_ptr = table.aux[field_number].strings.default_ptr; const char* field_name = table.aux[field_number].strings.field_name; - if (GOOGLE_PREDICT_FALSE(( - !HandleString( + if (PROTOBUF_PREDICT_FALSE( + (!HandleString( input, msg, arena, has_bits, presence_index, offset, default_ptr, field_name)))) { return false; @@ -740,7 +754,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, } #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED case TYPE_MAP: { - if (GOOGLE_PREDICT_FALSE(!(*table.aux[field_number].maps.parse_map)( + if (PROTOBUF_PREDICT_FALSE(!(*table.aux[field_number].maps.parse_map)( input, Raw(msg, offset)))) { return false; } @@ -748,6 +762,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, } case 0: { // Done. + input->SetLastTag(tag); return true; } default: @@ -766,12 +781,12 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, // TODO(ckennelly): Use a computed goto on GCC/LLVM. // // Mask out kRepeatedMask bit, allowing the jump table to be smaller. - switch (static_cast( - processing_type ^ kRepeatedMask)) { + switch (static_cast(processing_type ^ + kRepeatedMask)) { #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \ case WireFormatLite::TYPE_##TYPE: { \ RepeatedField* values = Raw>(msg, offset); \ - if (GOOGLE_PREDICT_FALSE( \ + if (PROTOBUF_PREDICT_FALSE( \ (!WireFormatLite::ReadPackedPrimitive< \ CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, values)))) { \ return false; \ @@ -802,7 +817,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, // are valid, we implement our own parser rather than call // WireFormat::ReadPackedEnumPreserveUnknowns. uint32 length; - if (GOOGLE_PREDICT_FALSE(!input->ReadVarint32(&length))) { + if (PROTOBUF_PREDICT_FALSE(!input->ReadVarint32(&length))) { return false; } @@ -813,13 +828,13 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, io::CodedInputStream::Limit limit = input->PushLimit(length); while (input->BytesUntilLimit() > 0) { int value; - if (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( (!WireFormatLite::ReadPrimitive< int, WireFormatLite::TYPE_ENUM>(input, &value)))) { return false; } - if (validator(value)) { + if (validator == nullptr || validator(value)) { values->Add(value); } else { // TODO(ckennelly): Consider caching here. @@ -842,6 +857,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, } else { if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) { // Must be the end of the message. + input->SetLastTag(tag); return true; } @@ -852,7 +868,7 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, } // process unknown field. - if (GOOGLE_PREDICT_FALSE( + if (PROTOBUF_PREDICT_FALSE( !UnknownFieldHandler::Skip(msg, table, input, tag))) { return false; } @@ -860,8 +876,30 @@ bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, } } +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, InternalMetadata, + 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/src/google/protobuf/generated_message_util.cc b/src/google/protobuf/generated_message_util.cc index ba8ec6d423..6cf87aa2ef 100644 --- a/src/google/protobuf/generated_message_util.cc +++ b/src/google/protobuf/generated_message_util.cc @@ -35,21 +35,25 @@ #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 #include @@ -61,9 +65,11 @@ namespace internal { void DestroyMessage(const void* message) { static_cast(message)->~MessageLite(); } -void DestroyString(const void* s) { static_cast(s)->~string(); } +void DestroyString(const void* s) { + static_cast(s)->~string(); +} -ExplicitlyConstructed<::std::string> fixed_address_empty_string; +ExplicitlyConstructed fixed_address_empty_string; static bool InitProtobufDefaultsImpl() { @@ -77,7 +83,7 @@ void InitProtobufDefaults() { (void)is_inited; } -size_t StringSpaceUsedExcludingSelfLong(const string& str) { +size_t StringSpaceUsedExcludingSelfLong(const std::string& str) { const void* start = &str; const void* end = &str + 1; if (start <= str.data() && str.data() < end) { @@ -224,7 +230,7 @@ struct PrimitiveTypeHelper template <> struct PrimitiveTypeHelper { - typedef string Type; + typedef std::string Type; static void Serialize(const void* ptr, io::CodedOutputStream* output) { const Type& value = *static_cast(ptr); output->WriteVarint32(value.size()); @@ -293,8 +299,11 @@ void SerializeMessageNoTable(const MessageLite* msg, } void SerializeMessageNoTable(const MessageLite* msg, ArrayOutput* output) { - output->ptr = msg->InternalSerializeWithCachedSizesToArray( - output->is_deterministic, output->ptr); + 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 @@ -303,15 +312,6 @@ void SerializeMessageDispatch(const MessageLite& msg, int32 cached_size, io::CodedOutputStream* output) { const uint8* base = reinterpret_cast(&msg); - // Try the fast path - uint8* ptr = output->GetDirectBufferForNBytesAndAdvance(cached_size); - if (ptr) { - // We use virtual dispatch to enable dedicated generated code for the - // fast path. - msg.InternalSerializeWithCachedSizesToArray( - output->IsSerializationDeterministic(), ptr); - return; - } SerializeInternal(base, field_table, num_fields, output); } @@ -414,7 +414,7 @@ struct SingularFieldHelper { template static void Serialize(const void* field, const FieldMetadata& md, O* output) { WriteTagTo(md.tag, output); - SerializeTo(&Get<::std::string>(field), output); + SerializeTo(&Get(field), output); } }; @@ -483,8 +483,8 @@ struct RepeatedFieldHelper { for (int i = 0; i < AccessorHelper::Size(array); i++) { WriteTagTo(md.tag, output); SerializeMessageTo( - static_cast(AccessorHelper::Get(array, i)), md.ptr, - output); + static_cast(AccessorHelper::Get(array, i)), + md.ptr, output); } } }; @@ -547,7 +547,7 @@ struct OneOfFieldHelper { template static void Serialize(const void* field, const FieldMetadata& md, O* output) { SingularFieldHelper::Serialize( - Get(field), md, output); + Get(field), md, output); } }; @@ -593,7 +593,7 @@ bool IsNull(const void* ptr) { template <> bool IsNull(const void* ptr) { - return static_cast(ptr)->empty(); + return static_cast(ptr)->empty(); } #define SERIALIZERS_FOR_TYPE(type) \ @@ -620,6 +620,7 @@ bool IsNull(const void* ptr) { 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; @@ -646,10 +647,10 @@ void SerializeInternal(const uint8* base, // Special cases case FieldMetadata::kSpecial: - reinterpret_cast( - const_cast(field_metadata.ptr))( - base, field_metadata.offset, field_metadata.tag, - field_metadata.has_offset, output); + 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() @@ -664,6 +665,7 @@ uint8* SerializeInternalToArray(const uint8* base, 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; @@ -692,10 +694,10 @@ uint8* SerializeInternalToArray(const uint8* base, io::ArrayOutputStream array_stream(array_output.ptr, INT_MAX); io::CodedOutputStream output(&array_stream); output.SetSerializationDeterministic(is_deterministic); - reinterpret_cast( - const_cast(field_metadata.ptr))( - base, field_metadata.offset, field_metadata.tag, - field_metadata.has_offset, &output); + 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: @@ -731,6 +733,15 @@ MessageLite* DuplicateIfNonNullInternal(MessageLite* message) { } } +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, @@ -752,7 +763,8 @@ namespace { void InitSCC_DFS(SCCInfoBase* scc) { if (scc->visit_status.load(std::memory_order_relaxed) != - SCCInfoBase::kUninitialized) return; + SCCInfoBase::kUninitialized) + return; scc->visit_status.store(SCCInfoBase::kRunning, std::memory_order_relaxed); // Each base is followed by an array of pointers to deps auto deps = reinterpret_cast(scc + 1); @@ -772,8 +784,17 @@ 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) { @@ -787,7 +808,13 @@ void InitSCCImpl(SCCInfoBase* scc) { 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(); } diff --git a/src/google/protobuf/generated_message_util.h b/src/google/protobuf/generated_message_util.h index 23d1009e1c..4e8ad9142f 100644 --- a/src/google/protobuf/generated_message_util.h +++ b/src/google/protobuf/generated_message_util.h @@ -44,15 +44,15 @@ #include #include -#include #include #include #include -#include #include #include // Add direct dep on port for pb.cc #include #include +#include +#include #include @@ -60,53 +60,31 @@ #error "You cannot SWIG proto headers" #endif -#if GOOGLE_PROTOBUF_ENABLE_MOMI_PARSER -#include -#endif - - namespace google { namespace protobuf { class Arena; -namespace io { class CodedInputStream; } +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); +} -// 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 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ - _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \ - __builtin_offsetof(TYPE, FIELD) \ - _Pragma("clang diagnostic pop") -#else -// 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 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \ - static_cast< ::google::protobuf::uint32>(reinterpret_cast( \ - &reinterpret_cast(16)->FIELD) - \ - reinterpret_cast(16)) -#endif -LIBPROTOBUF_EXPORT void InitProtobufDefaults(); +PROTOBUF_EXPORT void InitProtobufDefaults(); // This used by proto1 -LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyString() { +PROTOBUF_EXPORT inline const std::string& GetEmptyString() { InitProtobufDefaults(); return GetEmptyStringAlreadyInited(); } @@ -117,8 +95,9 @@ LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyString() { // 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 Type& t) { - for (int i = t.size(); --i >= 0; ) { +template +bool AllAreInitialized(const Type& t) { + for (int i = t.size(); --i >= 0;) { if (!t.Get(i).IsInitialized()) return false; } return true; @@ -139,40 +118,6 @@ bool AllAreInitializedWeak(const RepeatedPtrField& t) { return true; } -struct LIBPROTOBUF_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 distinquish the same types if they have different wire format. - enum { - kCordType = 19, - kStringPieceType = 20, - kInlinedType = 21, - kNumTypes = 21, - kSpecial = kNumTypes * kNumTypeClasses, - }; - - static int CalculateType(int fundamental_type, FieldTypeClass type_class); -}; - 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; @@ -188,112 +133,19 @@ typedef void (*SpecialSerializer)(const uint8* base, uint32 offset, uint32 tag, uint32 has_offset, io::CodedOutputStream* output); -LIBPROTOBUF_EXPORT void ExtensionSerializer(const uint8* base, uint32 offset, uint32 tag, - uint32 has_offset, io::CodedOutputStream* output); -LIBPROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8* base, uint32 offset, uint32 tag, - uint32 has_offset, - io::CodedOutputStream* output); - -struct SerializationTable { - int num_fields; - const FieldMetadata* field_table; -}; - -LIBPROTOBUF_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) { return a < b; } -}; +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); -template <> -struct CompareHelper { - bool operator()(const ArenaStringPtr& a, const ArenaStringPtr& b) { - return a.Get() < b.Get(); - } -}; - -struct CompareMapKey { - template - bool operator()(const MapEntryHelper& a, const MapEntryHelper& b) { - return Compare(a.key_, b.key_); - } - template - bool Compare(const T& a, const T& b) { - 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); - } - } -} - -LIBPROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message); -LIBPROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena, - MessageLite* submessage, - Arena* submessage_arena); +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); template T* DuplicateIfNonNull(T* message) { @@ -315,17 +167,18 @@ T* GetOwnedMessage(Arena* message_arena, T* submessage, // Hide atomic from the public header and allow easy change to regular int // on platforms where the atomic might have a perf impact. -class LIBPROTOBUF_EXPORT CachedSize { +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 LIBPROTOBUF_EXPORT SCCInfoBase { +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 { @@ -359,21 +212,22 @@ struct SCCInfo { SCCInfoBase* deps[N ? N : 1]; }; -LIBPROTOBUF_EXPORT void InitSCCImpl(SCCInfoBase* scc); +PROTOBUF_EXPORT void InitSCCImpl(SCCInfoBase* scc); inline void InitSCC(SCCInfoBase* scc) { auto status = scc->visit_status.load(std::memory_order_acquire); - if (GOOGLE_PREDICT_FALSE(status != SCCInfoBase::kInitialized)) InitSCCImpl(scc); + if (PROTOBUF_PREDICT_FALSE(status != SCCInfoBase::kInitialized)) + InitSCCImpl(scc); } -LIBPROTOBUF_EXPORT void DestroyMessage(const void* message); -LIBPROTOBUF_EXPORT void DestroyString(const void* s); +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 string destructor) -inline void OnShutdownDestroyString(const ::std::string* ptr) { +// Destroy the string (call std::string destructor) +inline void OnShutdownDestroyString(const std::string* ptr) { OnShutdownRun(DestroyString, ptr); } diff --git a/src/google/protobuf/has_bits.h b/src/google/protobuf/has_bits.h index edf4e82992..11afde7193 100644 --- a/src/google/protobuf/has_bits.h +++ b/src/google/protobuf/has_bits.h @@ -44,21 +44,20 @@ namespace google { namespace protobuf { namespace internal { -template +template class HasBits { public: - HasBits() GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { Clear(); } + HasBits() PROTOBUF_ALWAYS_INLINE { Clear(); } - void Clear() GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void Clear() PROTOBUF_ALWAYS_INLINE { memset(has_bits_, 0, sizeof(has_bits_)); } - ::google::protobuf::uint32& operator[](int index) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + uint32& operator[](int index) PROTOBUF_ALWAYS_INLINE { return has_bits_[index]; } - const ::google::protobuf::uint32& operator[](int index) const - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + const uint32& operator[](int index) const PROTOBUF_ALWAYS_INLINE { return has_bits_[index]; } @@ -70,10 +69,14 @@ class HasBits { return !(*this == rhs); } + void Or(const HasBits& rhs) { + for (int i = 0; i < doublewords; i++) has_bits_[i] |= rhs[i]; + } + bool empty() const; private: - ::google::protobuf::uint32 has_bits_[doublewords]; + uint32 has_bits_[doublewords]; }; template <> diff --git a/src/google/protobuf/implicit_weak_message.cc b/src/google/protobuf/implicit_weak_message.cc index 7e47cdcc32..35e64f0758 100644 --- a/src/google/protobuf/implicit_weak_message.cc +++ b/src/google/protobuf/implicit_weak_message.cc @@ -30,30 +30,29 @@ #include -#include -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER #include -#endif +#include #include #include +#include + namespace google { namespace protobuf { namespace internal { -bool ImplicitWeakMessage::MergePartialFromCodedStream(io::CodedInputStream* input) { +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER +const char* ImplicitWeakMessage::_InternalParse(const char* ptr, + ParseContext* ctx) { + return ctx->AppendString(ptr, &data_); +} +#else +bool ImplicitWeakMessage::MergePartialFromCodedStream( + io::CodedInputStream* input) { io::StringOutputStream string_stream(&data_); io::CodedOutputStream coded_stream(&string_stream, false); return WireFormatLite::SkipMessage(input, &coded_stream); } - -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* ImplicitWeakMessage::_InternalParse(const char* begin, - const char* end, void* object, - ParseContext* ctx) { - return internal::StringParser( - begin, end, &(static_cast(object)->data_), ctx); -} #endif ExplicitlyConstructed diff --git a/src/google/protobuf/implicit_weak_message.h b/src/google/protobuf/implicit_weak_message.h index 2983628934..a0cb1c179f 100644 --- a/src/google/protobuf/implicit_weak_message.h +++ b/src/google/protobuf/implicit_weak_message.h @@ -36,6 +36,8 @@ #include #include +#include + #ifdef SWIG #error "You cannot SWIG proto headers" #endif @@ -50,52 +52,51 @@ 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 LIBPROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite { +class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite { public: ImplicitWeakMessage() : arena_(NULL) {} explicit ImplicitWeakMessage(Arena* arena) : arena_(arena) {} static const ImplicitWeakMessage* default_instance(); - string GetTypeName() const { return ""; } + std::string GetTypeName() const override { return ""; } - MessageLite* New() const { return new ImplicitWeakMessage; } - MessageLite* New(Arena* arena) const { + MessageLite* New() const override { return new ImplicitWeakMessage; } + MessageLite* New(Arena* arena) const override { return Arena::CreateMessage(arena); } - Arena* GetArena() const { return arena_; } + Arena* GetArena() const override { return arena_; } - void Clear() { data_.clear(); } + void Clear() override { data_.clear(); } - bool IsInitialized() const { return true; } + bool IsInitialized() const override { return true; } - void CheckTypeAndMergeFrom(const MessageLite& other) { + void CheckTypeAndMergeFrom(const MessageLite& other) override { data_.append(static_cast(other).data_); } - bool MergePartialFromCodedStream(io::CodedInputStream* input); - #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - ParseFunc _ParseFunc() const { return _InternalParse; } - - static const char* _InternalParse(const char* begin, const char* end, - void* object, ParseContext* ctx); + const char* _InternalParse(const char* ptr, ParseContext* ctx) final; +#else + bool MergePartialFromCodedStream(io::CodedInputStream* input) override; #endif - size_t ByteSizeLong() const { return data_.size(); } + size_t ByteSizeLong() const override { return data_.size(); } - void SerializeWithCachedSizes(io::CodedOutputStream* output) const { - output->WriteString(data_); + uint8* InternalSerializeWithCachedSizesToArray( + uint8* target, io::EpsCopyOutputStream* stream) const final { + return stream->WriteRaw(data_.data(), static_cast(data_.size()), + target); } - int GetCachedSize() const { return static_cast(data_.size()); } + int GetCachedSize() const override { return static_cast(data_.size()); } typedef void InternalArenaConstructable_; private: Arena* const arena_; - string data_; + std::string data_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImplicitWeakMessage); }; @@ -141,4 +142,6 @@ class ImplicitWeakTypeHandler { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__ diff --git a/src/google/protobuf/inlined_string_field.h b/src/google/protobuf/inlined_string_field.h index 1509193f97..781059dae7 100644 --- a/src/google/protobuf/inlined_string_field.h +++ b/src/google/protobuf/inlined_string_field.h @@ -50,153 +50,143 @@ class Arena; namespace internal { -// InlinedStringField wraps a ::std::string instance and exposes an API similar to -// ArenaStringPtr's wrapping of a ::std::string* instance. As ::std::string is never -// allocated on the Arena, we expose only the *NoArena methods of +// InlinedStringField wraps a std::string instance and exposes an API similar to +// ArenaStringPtr's wrapping of a std::string* instance. As std::string is +// never allocated on the Arena, we expose only the *NoArena methods of // ArenaStringPtr. // // default_value parameters are taken for consistency with ArenaStringPtr, but // are not used for most methods. With inlining, these should be removed from // the generated binary. -class LIBPROTOBUF_EXPORT InlinedStringField { +class PROTOBUF_EXPORT InlinedStringField { public: - InlinedStringField() GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; - explicit InlinedStringField(const ::std::string& default_value); + InlinedStringField() PROTOBUF_ALWAYS_INLINE; + explicit InlinedStringField(const std::string& default_value); - void AssignWithDefault(const ::std::string* default_value, - const InlinedStringField& from) - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; + void AssignWithDefault(const std::string* default_value, + const InlinedStringField& from) PROTOBUF_ALWAYS_INLINE; - void ClearToEmpty(const ::std::string* default_value, - Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void ClearToEmpty(const std::string* default_value, + Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE { ClearToEmptyNoArena(default_value); } - void ClearNonDefaultToEmpty() GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void ClearNonDefaultToEmpty() PROTOBUF_ALWAYS_INLINE { ClearNonDefaultToEmptyNoArena(); } - void ClearToEmptyNoArena(const ::std::string* default_value) - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void ClearToEmptyNoArena(const std::string* /*default_value*/) + PROTOBUF_ALWAYS_INLINE { ClearNonDefaultToEmptyNoArena(); } - void ClearNonDefaultToEmptyNoArena() GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; + void ClearNonDefaultToEmptyNoArena() PROTOBUF_ALWAYS_INLINE; - void ClearToDefault(const ::std::string* default_value, - Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void ClearToDefault(const std::string* default_value, + Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE { ClearToDefaultNoArena(default_value); } - void ClearToDefaultNoArena(const ::std::string* default_value) - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; + void ClearToDefaultNoArena(const std::string* default_value) + PROTOBUF_ALWAYS_INLINE; - void Destroy(const ::std::string* default_value, - Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void Destroy(const std::string* default_value, + Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE { DestroyNoArena(default_value); } - void DestroyNoArena(const ::std::string* default_value) - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; + void DestroyNoArena(const std::string* default_value) PROTOBUF_ALWAYS_INLINE; - const ::std::string& Get() const GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { - return GetNoArena(); - } - const ::std::string& GetNoArena() const GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; + const std::string& Get() const PROTOBUF_ALWAYS_INLINE { return GetNoArena(); } + const std::string& GetNoArena() const PROTOBUF_ALWAYS_INLINE; - ::std::string* Mutable(const ::std::string* default_value, - Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + std::string* Mutable(const std::string* default_value, + Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE { return MutableNoArena(default_value); } - ::std::string* MutableNoArena(const ::std::string* default_value) - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; + std::string* MutableNoArena(const std::string* default_value) + PROTOBUF_ALWAYS_INLINE; - ::std::string* Release(const ::std::string* default_value, Arena* arena) { + std::string* Release(const std::string* default_value, Arena* /*arena*/) { return ReleaseNoArena(default_value); } - ::std::string* ReleaseNonDefault(const ::std::string* default_value, Arena* arena) { + std::string* ReleaseNonDefault(const std::string* default_value, + Arena* /*arena*/) { return ReleaseNonDefaultNoArena(default_value); } - ::std::string* ReleaseNoArena(const ::std::string* default_value) { + std::string* ReleaseNoArena(const std::string* default_value) { return ReleaseNonDefaultNoArena(default_value); } - ::std::string* ReleaseNonDefaultNoArena(const ::std::string* default_value); + std::string* ReleaseNonDefaultNoArena(const std::string* default_value); - void Set(const ::std::string* default_value, StringPiece value, - Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void Set(const std::string* default_value, StringPiece value, + Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE { SetNoArena(default_value, value); } - void SetLite(const ::std::string* default_value, StringPiece value, - Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void SetLite(const std::string* default_value, StringPiece value, + Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE { SetNoArena(default_value, value); } - void SetNoArena(const ::std::string* default_value, StringPiece value) - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; + void SetNoArena(const std::string* default_value, + StringPiece value) PROTOBUF_ALWAYS_INLINE; - void Set(const ::std::string* default_value, const ::std::string& value, - Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void Set(const std::string* default_value, const std::string& value, + Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE { SetNoArena(default_value, value); } - void SetLite(const ::std::string* default_value, const ::std::string& value, - Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { + void SetLite(const std::string* default_value, const std::string& value, + Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE { SetNoArena(default_value, value); } - void SetNoArena(const ::std::string* default_value, const ::std::string& value) - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; + void SetNoArena(const std::string* default_value, + const std::string& value) PROTOBUF_ALWAYS_INLINE; -#if LANG_CXX11 - void SetNoArena(const ::std::string* default_value, - ::std::string&& value) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; -#endif - void SetAllocated(const ::std::string* default_value, - ::std::string* value, - Arena* arena) { + void SetNoArena(const std::string* default_value, + std::string&& value) PROTOBUF_ALWAYS_INLINE; + void SetAllocated(const std::string* default_value, std::string* value, + Arena* /*arena*/) { SetAllocatedNoArena(default_value, value); } - void SetAllocatedNoArena(const ::std::string* default_value, - ::std::string* value); - void Swap(InlinedStringField* from) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; - ::std::string* UnsafeMutablePointer(); - void UnsafeSetDefault(const ::std::string* default_value); - ::std::string* UnsafeArenaRelease(const ::std::string* default_value, Arena* arena); - void UnsafeArenaSetAllocated( - const ::std::string* default_value, ::std::string* value, Arena* arena); - - bool IsDefault(const ::std::string* default_value) { - return false; - } + void SetAllocatedNoArena(const std::string* default_value, + std::string* value); + void Swap(InlinedStringField* from) PROTOBUF_ALWAYS_INLINE; + std::string* UnsafeMutablePointer(); + void UnsafeSetDefault(const std::string* default_value); + std::string* UnsafeArenaRelease(const std::string* default_value, + Arena* arena); + void UnsafeArenaSetAllocated(const std::string* default_value, + std::string* value, Arena* arena); + + bool IsDefault(const std::string* /*default_value*/) { return false; } + private: - ::std::string value_; + std::string value_; }; inline InlinedStringField::InlinedStringField() {} -inline InlinedStringField::InlinedStringField(const ::std::string& default_value) : - value_(default_value) {} +inline InlinedStringField::InlinedStringField(const std::string& default_value) + : value_(default_value) {} inline void InlinedStringField::AssignWithDefault( - const ::std::string* default_value, const InlinedStringField& from) { + const std::string* /*default_value*/, const InlinedStringField& from) { value_ = from.value_; } -inline const ::std::string& InlinedStringField::GetNoArena() const { +inline const std::string& InlinedStringField::GetNoArena() const { return value_; } -inline ::std::string* InlinedStringField::MutableNoArena(const ::std::string*) { +inline std::string* InlinedStringField::MutableNoArena(const std::string*) { return &value_; } inline void InlinedStringField::SetAllocatedNoArena( - const ::std::string* default_value, ::std::string* value) { + const std::string* default_value, std::string* value) { if (value == NULL) { value_.assign(*default_value); } else { -#if LANG_CXX11 value_.assign(std::move(*value)); -#else - value_.swap(*value); -#endif delete value; } } -inline void InlinedStringField::DestroyNoArena(const ::std::string*) { +inline void InlinedStringField::DestroyNoArena(const std::string*) { // This is invoked from the generated message's ArenaDtor, which is used to // clean up objects not allocated on the Arena. this->~InlinedStringField(); @@ -207,54 +197,52 @@ inline void InlinedStringField::ClearNonDefaultToEmptyNoArena() { } inline void InlinedStringField::ClearToDefaultNoArena( - const ::std::string* default_value) { + const std::string* default_value) { value_.assign(*default_value); } -inline ::std::string* InlinedStringField::ReleaseNonDefaultNoArena( - const ::std::string* default_value) { - ::std::string* released = new ::std::string(*default_value); +inline std::string* InlinedStringField::ReleaseNonDefaultNoArena( + const std::string* default_value) { + std::string* released = new std::string(*default_value); value_.swap(*released); return released; } -inline void InlinedStringField::SetNoArena(const ::std::string* default_value, +inline void InlinedStringField::SetNoArena(const std::string* /*default_value*/, StringPiece value) { value_.assign(value.data(), value.length()); } -inline void InlinedStringField::SetNoArena( - const ::std::string* default_value, const ::std::string& value) { +inline void InlinedStringField::SetNoArena(const std::string* /*default_value*/, + const std::string& value) { value_.assign(value); } -#if LANG_CXX11 -inline void InlinedStringField::SetNoArena( - const ::std::string* default_value, ::std::string&& value) { +inline void InlinedStringField::SetNoArena(const std::string* /*default_value*/, + std::string&& value) { value_.assign(std::move(value)); } -#endif inline void InlinedStringField::Swap(InlinedStringField* from) { value_.swap(from->value_); } -inline ::std::string* InlinedStringField::UnsafeMutablePointer() { +inline std::string* InlinedStringField::UnsafeMutablePointer() { return &value_; } inline void InlinedStringField::UnsafeSetDefault( - const ::std::string* default_value) { + const std::string* default_value) { value_.assign(*default_value); } -inline ::std::string* InlinedStringField::UnsafeArenaRelease( - const ::std::string* default_value, Arena* arena) { +inline std::string* InlinedStringField::UnsafeArenaRelease( + const std::string* default_value, Arena* /*arena*/) { return ReleaseNoArena(default_value); } inline void InlinedStringField::UnsafeArenaSetAllocated( - const ::std::string* default_value, ::std::string* value, Arena* arena) { + const std::string* default_value, std::string* value, Arena* /*arena*/) { if (value == NULL) { value_.assign(*default_value); } else { diff --git a/src/google/protobuf/io/coded_stream.cc b/src/google/protobuf/io/coded_stream.cc index 311668ce03..f04dc1db2e 100644 --- a/src/google/protobuf/io/coded_stream.cc +++ b/src/google/protobuf/io/coded_stream.cc @@ -38,14 +38,19 @@ // 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 +#include +#include #include @@ -61,8 +66,8 @@ static const int kMaxVarintBytes = 10; static const int kMaxVarint32Bytes = 5; -inline bool NextNonEmpty(ZeroCopyInputStream* input, - const void** data, int* size) { +inline bool NextNonEmpty(ZeroCopyInputStream* input, const void** data, + int* size) { bool success; do { success = input->Next(data, size); @@ -84,10 +89,6 @@ CodedInputStream::~CodedInputStream() { int CodedInputStream::default_recursion_limit_ = 100; -void CodedOutputStream::EnableAliasing(bool enabled) { - aliasing_enabled_ = enabled && output_->AllowsAliasing(); -} - void CodedInputStream::BackUpInputToCurrentPosition() { int backup_bytes = BufferSize() + buffer_size_after_limit_ + overflow_bytes_; if (backup_bytes > 0) { @@ -123,9 +124,9 @@ CodedInputStream::Limit CodedInputStream::PushLimit(int byte_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 (GOOGLE_PREDICT_TRUE(byte_limit >= 0 && - byte_limit <= INT_MAX - current_position && - byte_limit < current_limit_ - current_position)) { + 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(); } @@ -190,7 +191,8 @@ int CodedInputStream::BytesUntilTotalBytesLimit() const { void CodedInputStream::PrintTotalBytesLimitError() { GOOGLE_LOG(ERROR) << "A protocol message was rejected because it was too " - "big (more than " << total_bytes_limit_ + "big (more than " + << total_bytes_limit_ << " bytes). To increase the limit (or to disable these " "warnings), see CodedInputStream::SetTotalBytesLimit() " "in net/proto2/io/public/coded_stream.h."; @@ -239,12 +241,12 @@ bool CodedInputStream::ReadRaw(void* buffer, int size) { return InternalReadRawInline(buffer, size); } -bool CodedInputStream::ReadString(string* buffer, int size) { +bool CodedInputStream::ReadString(std::string* buffer, int size) { if (size < 0) return false; // security: size is often user-supplied return InternalReadStringInline(buffer, size); } -bool CodedInputStream::ReadStringFallback(string* buffer, int size) { +bool CodedInputStream::ReadStringFallback(std::string* buffer, int size) { if (!buffer->empty()) { buffer->clear(); } @@ -314,14 +316,28 @@ bool CodedInputStream::ReadLittleEndian64Fallback(uint64* value) { 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. -GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE -::std::pair ReadVarint32FromArray( - uint32 first_byte, const uint8* buffer, - uint32* value); +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 @@ -332,71 +348,72 @@ inline ::std::pair ReadVarint32FromArray( 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; + b = *(ptr++); + result += b << 7; + if (!(b & 0x80)) goto done; result -= 0x80 << 7; - b = *(ptr++); result += b << 14; if (!(b & 0x80)) goto done; + b = *(ptr++); + result += b << 14; + if (!(b & 0x80)) goto done; result -= 0x80 << 14; - b = *(ptr++); result += b << 21; if (!(b & 0x80)) goto done; + b = *(ptr++); + result += b << 21; + if (!(b & 0x80)) goto done; result -= 0x80 << 21; - b = *(ptr++); result += b << 28; if (!(b & 0x80)) goto done; + 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; + 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: +done: *value = result; return std::make_pair(true, ptr); } -GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE::std::pair -ReadVarint64FromArray(const uint8* buffer, uint64* value); +PROTOBUF_ALWAYS_INLINE::std::pair ReadVarint64FromArray( + const uint8* buffer, uint64* value); inline ::std::pair ReadVarint64FromArray( const uint8* buffer, uint64* value) { - const uint8* ptr = buffer; - uint32 b; - - // Splitting into 32-bit pieces gives better performance on 32-bit - // processors. - uint32 part0 = 0, part1 = 0, part2 = 0; - - b = *(ptr++); part0 = b ; if (!(b & 0x80)) goto done; - part0 -= 0x80; - b = *(ptr++); part0 += b << 7; if (!(b & 0x80)) goto done; - part0 -= 0x80 << 7; - b = *(ptr++); part0 += b << 14; if (!(b & 0x80)) goto done; - part0 -= 0x80 << 14; - b = *(ptr++); part0 += b << 21; if (!(b & 0x80)) goto done; - part0 -= 0x80 << 21; - b = *(ptr++); part1 = b ; if (!(b & 0x80)) goto done; - part1 -= 0x80; - b = *(ptr++); part1 += b << 7; if (!(b & 0x80)) goto done; - part1 -= 0x80 << 7; - b = *(ptr++); part1 += b << 14; if (!(b & 0x80)) goto done; - part1 -= 0x80 << 14; - b = *(ptr++); part1 += b << 21; if (!(b & 0x80)) goto done; - part1 -= 0x80 << 21; - b = *(ptr++); part2 = b ; if (!(b & 0x80)) goto done; - part2 -= 0x80; - b = *(ptr++); part2 += b << 7; if (!(b & 0x80)) goto done; - // "part2 -= 0x80 << 7" is irrelevant because (0x80 << 7) << 56 is 0. - - // We have overrun the maximum size of a varint (10 bytes). Assume - // the data is corrupt. - return std::make_pair(false, ptr); + // 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); + } - done: - *value = (static_cast(part0)) | - (static_cast(part1) << 28) | - (static_cast(part2) << 56); - return std::make_pair(true, ptr); + return std::make_pair(true, next); } } // namespace @@ -418,7 +435,7 @@ int64 CodedInputStream::ReadVarint32Fallback(uint32 first_byte_or_zero) { << "Caller should provide us with *buffer_ when buffer is non-empty"; uint32 temp; ::std::pair p = - ReadVarint32FromArray(first_byte_or_zero, buffer_, &temp); + ReadVarint32FromArray(first_byte_or_zero, buffer_, &temp); if (!p.first) return -1; buffer_ = p.second; return temp; @@ -621,156 +638,285 @@ bool CodedInputStream::Refresh() { // CodedOutputStream ================================================= -std::atomic CodedOutputStream::default_serialization_deterministic_{ - false}; +void EpsCopyOutputStream::EnableAliasing(bool enabled) { + aliasing_enabled_ = enabled && stream_->AllowsAliasing(); +} -CodedOutputStream::CodedOutputStream(ZeroCopyOutputStream* output) - : CodedOutputStream(output, true) {} +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; +} -CodedOutputStream::CodedOutputStream(ZeroCopyOutputStream* output, - bool do_eager_refresh) - : output_(output), - buffer_(NULL), - buffer_size_(0), - total_bytes_(0), - had_error_(false), - aliasing_enabled_(false), - is_serialization_deterministic_(IsDefaultSerializationDeterministic()) { - if (do_eager_refresh) { - // Eagerly Refresh() so buffer space is immediately available. - Refresh(); - // The Refresh() may have failed. If the client doesn't write any data, - // though, don't consider this an error. If the client does write data, then - // another Refresh() will be attempted and it will set the error once again. - had_error_ = false; +// 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; } -CodedOutputStream::~CodedOutputStream() { - Trim(); +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_; } -void CodedOutputStream::Trim() { - if (buffer_size_ > 0) { - output_->BackUp(buffer_size_); - total_bytes_ -= buffer_size_; - buffer_size_ = 0; - buffer_ = NULL; - } + +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 CodedOutputStream::Skip(int count) { +bool EpsCopyOutputStream::Skip(int count, uint8** pp) { if (count < 0) return false; - - while (count > buffer_size_) { - count -= buffer_size_; - if (!Refresh()) return false; + if (had_error_) { + *pp = buffer_; + return false; } - - Advance(count); + 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 CodedOutputStream::GetDirectBufferPointer(void** data, int* size) { - if (buffer_size_ == 0 && !Refresh()) return false; - - *data = buffer_; - *size = buffer_size_; +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; } -void CodedOutputStream::WriteRaw(const void* data, int size) { - while (buffer_size_ < size) { - memcpy(buffer_, data, buffer_size_); - size -= buffer_size_; - data = reinterpret_cast(data) + buffer_size_; - if (!Refresh()) return; +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; } - - memcpy(buffer_, data, size); - Advance(size); -} - -uint8* CodedOutputStream::WriteRawToArray( - const void* data, int size, uint8* target) { - memcpy(target, data, size); - return target + size; } - -void CodedOutputStream::WriteAliasedRaw(const void* data, int size) { - if (size < buffer_size_ - ) { - WriteRaw(data, size); +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 { - Trim(); - - total_bytes_ += size; - had_error_ |= !output_->WriteAliasedRaw(data, size); + std::memcpy(buffer_, end_, kSlopBytes); + buffer_end_ = end_; + end_ = buffer_ + kSlopBytes; + return buffer_; } } -void CodedOutputStream::WriteLittleEndian32(uint32 value) { - uint8 bytes[sizeof(value)]; - - bool use_fast = buffer_size_ >= sizeof(value); - uint8* ptr = use_fast ? buffer_ : bytes; - - WriteLittleEndian32ToArray(value, ptr); +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; +} - if (use_fast) { - Advance(sizeof(value)); +uint8* EpsCopyOutputStream::WriteAliasedRaw(const void* data, int size, + uint8* ptr) { + if (size < GetSize(ptr) + ) { + return WriteRaw(data, size, ptr); } else { - WriteRaw(bytes, sizeof(value)); + ptr = Trim(ptr); + if (stream_->WriteAliasedRaw(data, size)) return ptr; + return Error(); } } -void CodedOutputStream::WriteLittleEndian64(uint64 value) { - uint8 bytes[sizeof(value)]; - - bool use_fast = buffer_size_ >= sizeof(value); - uint8* ptr = use_fast ? buffer_ : bytes; - - WriteLittleEndian64ToArray(value, ptr); - - if (use_fast) { - Advance(sizeof(value)); - } else { - WriteRaw(bytes, sizeof(value)); +#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) { + 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) { + 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) { + 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) { + EnsureSpace(&ptr); + uint64 buffer; + std::memcpy(&buffer, p, 8); + p += 8; + ptr = CodedOutputStream::WriteLittleEndian64ToArray(buffer, ptr); + } + return ptr; } +#endif + -void CodedOutputStream::WriteVarint32SlowPath(uint32 value) { - uint8 bytes[kMaxVarint32Bytes]; - uint8* target = &bytes[0]; - uint8* end = WriteVarint32ToArray(value, target); - int size = end - target; - WriteRaw(bytes, size); +uint8* EpsCopyOutputStream::WriteStringMaybeAliasedOutline(uint32 num, + const std::string& s, + uint8* ptr) { + EnsureSpace(&ptr); + uint32 size = s.size(); + ptr = WriteLengthDelim(num, size, ptr); + return WriteRawMaybeAliased(s.data(), size, ptr); } -void CodedOutputStream::WriteVarint64SlowPath(uint64 value) { - uint8 bytes[kMaxVarintBytes]; - uint8* target = &bytes[0]; - uint8* end = WriteVarint64ToArray(value, target); - int size = end - target; - WriteRaw(bytes, size); +uint8* EpsCopyOutputStream::WriteStringOutline(uint32 num, const std::string& s, + uint8* ptr) { + EnsureSpace(&ptr); + uint32 size = s.size(); + ptr = WriteLengthDelim(num, size, ptr); + return WriteRaw(s.data(), size, ptr); } -bool CodedOutputStream::Refresh() { - void* void_buffer; - if (output_->Next(&void_buffer, &buffer_size_)) { - buffer_ = reinterpret_cast(void_buffer); - total_bytes_ += buffer_size_; - return true; - } else { - buffer_ = NULL; - buffer_size_ = 0; - had_error_ = true; - return false; +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); } } -uint8* CodedOutputStream::WriteStringWithSizeToArray(const string& str, +CodedOutputStream::~CodedOutputStream() { Trim(); } + + +uint8* CodedOutputStream::WriteStringWithSizeToArray(const std::string& str, uint8* target) { GOOGLE_DCHECK_LE(str.size(), kuint32max); target = WriteVarint32ToArray(str.size(), target); diff --git a/src/google/protobuf/io/coded_stream.h b/src/google/protobuf/io/coded_stream.h old mode 100644 new mode 100755 index 71a4f5fb25..c620d89c63 --- a/src/google/protobuf/io/coded_stream.h +++ b/src/google/protobuf/io/coded_stream.h @@ -67,7 +67,7 @@ // // 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); +// CodedInputStream* coded_input = new CodedInputStream(raw_input); // // coded_input->ReadLittleEndian32(&magic_number); // if (magic_number != 1234) { @@ -110,46 +110,53 @@ #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 +// 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 +#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 -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -#include "util/coding/varint.h" -#endif - namespace google { namespace protobuf { class DescriptorPool; class MessageFactory; +class ZeroCopyCodedInputStream; -namespace internal { void MapTestForceDeterministic(); } +namespace internal { +void MapTestForceDeterministic(); +class EpsCopyByteStream; +} // namespace internal namespace io { @@ -158,8 +165,8 @@ class CodedInputStream; class CodedOutputStream; // Defined in other files. -class ZeroCopyInputStream; // zero_copy_stream.h -class ZeroCopyOutputStream; // zero_copy_stream.h +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. @@ -168,7 +175,9 @@ class ZeroCopyOutputStream; // zero_copy_stream.h // 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. -class LIBPROTOBUF_EXPORT CodedInputStream { +// 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); @@ -204,7 +213,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // Like GetDirectBufferPointer, but this method is inlined, and does not // attempt to Refresh() if the buffer is currently empty. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE + PROTOBUF_ALWAYS_INLINE void GetDirectBufferPointerInline(const void** data, int* size); // Read raw bytes, copying them into the given buffer. @@ -212,15 +221,15 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // Like the above, with inlined optimizations. This should only be used // by the protobuf implementation. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE + PROTOBUF_ALWAYS_INLINE bool InternalReadRawInline(void* buffer, int size); // Like ReadRaw, but reads into a string. - bool ReadString(string* buffer, int size); + bool ReadString(std::string* buffer, int size); // Like the above, with inlined optimizations. This should only be used // by the protobuf implementation. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE - bool InternalReadStringInline(string* buffer, int size); + PROTOBUF_ALWAYS_INLINE + bool InternalReadStringInline(std::string* buffer, int size); // Read a 32-bit little-endian integer. @@ -232,10 +241,10 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // 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); + uint32* value); // Read a 64-bit little-endian integer. static const uint8* ReadLittleEndian64FromArray(const uint8* buffer, - uint64* value); + 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 @@ -263,11 +272,11 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // 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. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE uint32 ReadTag() { + PROTOBUF_ALWAYS_INLINE uint32 ReadTag() { return last_tag_ = ReadTagNoLastTag(); } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE uint32 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 @@ -277,14 +286,14 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // 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.) - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE + PROTOBUF_ALWAYS_INLINE std::pair ReadTagWithCutoff(uint32 cutoff) { std::pair result = ReadTagWithCutoffNoLastTag(cutoff); last_tag_ = result.first; return result; } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE + PROTOBUF_ALWAYS_INLINE std::pair ReadTagWithCutoffNoLastTag(uint32 cutoff); // Usually returns true if calling ReadVarint32() now would produce the given @@ -294,7 +303,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // 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. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool ExpectTag(uint32 expected); + 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 @@ -303,7 +312,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // // Returns a pointer beyond the expected tag if it was found, or NULL if it // was not. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE + 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 @@ -390,7 +399,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // This is unrelated to PushLimit()/PopLimit(). void SetTotalBytesLimit(int total_bytes_limit); - GOOGLE_PROTOBUF_DEPRECATED_MSG( + PROTOBUF_DEPRECATED_MSG( "Please use the single parameter version of SetTotalBytesLimit(). The " "second parameter is ignored.") void SetTotalBytesLimit(int total_bytes_limit, int) { @@ -409,7 +418,9 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // 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. @@ -533,7 +544,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream); const uint8* buffer_; - const uint8* buffer_end_; // pointer to the end of the 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 @@ -543,7 +554,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { int overflow_bytes_; // LastTagWas() stuff. - uint32 last_tag_; // result of last ReadTag() or ReadTagWithCutoff(). + 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 @@ -554,7 +565,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { bool aliasing_enabled_; // Limits - Limit current_limit_; // if position = -1, no limit is applied + 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_ @@ -627,7 +638,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // stream. uint32 ReadTagFallback(uint32 first_byte_or_zero); uint32 ReadTagSlow(); - bool ReadStringFallback(string* buffer, int size); + bool ReadStringFallback(std::string* buffer, int size); // Return the size of the buffer. int BufferSize() const; @@ -635,8 +646,389 @@ class LIBPROTOBUF_EXPORT CodedInputStream { 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. + void EnsureSpace(uint8** ptr) { + if (PROTOBUF_PREDICT_FALSE(*ptr >= end_)) { + *ptr = EnsureSpaceFallback(*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) { + 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); + + // Deterministic serialization, if requested, guarantees 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 of the same binary (which may be executing on + // different machines) will serialize equal messages to the same bytes. + // + // Note the deterministic serialization is NOT canonical across languages; it + // is also unstable across different builds with schema changes due to unknown + // fields. Users who need canonical serialization, e.g., persistent storage in + // a canonical form, fingerprinting, etc., should define their own + // canonicalization specification and implement the serializer using + // reflection APIs rather than relying on this API. + // + // If deterministic serialization is requested, the serializer will + // sort map entries by keys in lexicographical order or numerical order. + // (This is an implementation detail and may subject to change.) + // + // There are two ways to determine whether serialization should be + // deterministic for this CodedOutputStream. If SetSerializationDeterministic + // has not yet been called, then the default comes from the global default, + // which is false, until SetDefaultSerializationDeterministic has been called. + // Otherwise, SetSerializationDeterministic has been called, and the last + // value passed to it is all that matters. + void SetSerializationDeterministic(bool value) { + is_serialization_deterministic_ = value; + } + // See above. Also, note that users of this CodedOutputStream may need to + // call IsSerializationDeterministic() to serialize in the intended way. This + // CodedOutputStream cannot enforce a desire for deterministic serialization + // by itself. + bool IsSerializationDeterministic() const { + return is_serialization_deterministic_; + } + + // The number of bytes writen 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) { + EnsureSpace(&ptr); + ptr = WriteLengthDelim(num, size, ptr); + auto it = r.data(); + auto end = it + r.size(); + do { + 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 mimick 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. @@ -657,7 +1049,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // individual value. // i.e., in the example above: // -// CodedOutputStream coded_output = new CodedOutputStream(raw_output); +// CodedOutputStream* coded_output = new CodedOutputStream(raw_output); // int magic_number = 1234; // char text[] = "Hello world!"; // @@ -667,7 +1059,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // // uint8* buffer = // coded_output->GetDirectBufferForNBytesAndAdvance(coded_size); -// if (buffer != NULL) { +// if (buffer != nullptr) { // // The output stream has enough space in the buffer: write directly to // // the array. // buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number, @@ -683,27 +1075,40 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // } // // delete coded_output; -class LIBPROTOBUF_EXPORT CodedOutputStream { +class PROTOBUF_EXPORT CodedOutputStream { public: // Create an CodedOutputStream that writes to the given ZeroCopyOutputStream. - explicit CodedOutputStream(ZeroCopyOutputStream* output); - CodedOutputStream(ZeroCopyOutputStream* output, bool do_eager_refresh); + 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(); + 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(). - bool Skip(int count); + // 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 @@ -713,7 +1118,9 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { // 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); + 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. @@ -722,10 +1129,14 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { // 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); + 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); + 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); @@ -737,30 +1148,26 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { static uint8* WriteRawToArray(const void* buffer, int size, uint8* target); // Equivalent to WriteRaw(str.data(), str.size()). - void WriteString(const string& str); + void WriteString(const std::string& str); // Like WriteString() but writing directly to the target array. - static uint8* WriteStringToArray(const string& str, uint8* target); + static uint8* WriteStringToArray(const std::string& str, uint8* target); // Write the varint-encoded size of str followed by str. - static uint8* WriteStringWithSizeToArray(const string& str, uint8* target); + static uint8* WriteStringWithSizeToArray(const std::string& str, + uint8* target); - // 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); - // Write a 32-bit little-endian integer. - void WriteLittleEndian32(uint32 value); + void WriteLittleEndian32(uint32 value) { + 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); + void WriteLittleEndian64(uint64 value) { + impl_.EnsureSpace(&cur_); + SetCur(WriteLittleEndian64ToArray(value, Cur())); + } // Like WriteLittleEndian64() but writing directly to the target array. static uint8* WriteLittleEndian64ToArray(uint64 value, uint8* target); @@ -788,7 +1195,7 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { // but GCC by default doesn't want to inline this. void WriteTag(uint32 value); // Like WriteTag() but writing directly to the target array. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE + PROTOBUF_ALWAYS_INLINE static uint8* WriteTagToArray(uint32 value, uint8* target); // Returns the number of bytes needed to encode the given value as a varint. @@ -806,54 +1213,36 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { (Value < (1 << 7)) ? 1 : (Value < (1 << 14)) - ? 2 - : (Value < (1 << 21)) - ? 3 - : (Value < (1 << 28)) - ? 4 - : 5; + ? 2 + : (Value < (1 << 21)) ? 3 : (Value < (1 << 28)) ? 4 : 5; }; // Returns the total number of bytes written since this object was created. - inline int ByteCount() const; - - // Returns true if there was an underlying I/O error since this object was - // created. - bool HadError() const { return had_error_; } + int ByteCount() const { + return static_cast(impl_.ByteCount(cur_) - start_count_); + } - // Deterministic serialization, if requested, guarantees 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 of the same binary (which may be executing on - // different machines) will serialize equal messages to the same bytes. - // - // Note the deterministic serialization is NOT canonical across languages; it - // is also unstable across different builds with schema changes due to unknown - // fields. Users who need canonical serialization, e.g., persistent storage in - // a canonical form, fingerprinting, etc., should define their own - // canonicalization specification and implement the serializer using - // reflection APIs rather than relying on this API. - // - // If deterministic serialization is requested, the serializer will - // sort map entries by keys in lexicographical order or numerical order. - // (This is an implementation detail and may subject to change.) + // 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(). // - // There are two ways to determine whether serialization should be - // deterministic for this CodedOutputStream. If SetSerializationDeterministic - // has not yet been called, then the default comes from the global default, - // which is false, until SetDefaultSerializationDeterministic has been called. - // Otherwise, SetSerializationDeterministic has been called, and the last - // value passed to it is all that matters. + // 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); } + void SetSerializationDeterministic(bool value) { - is_serialization_deterministic_ = value; + impl_.SetSerializationDeterministic(value); } // See above. Also, note that users of this CodedOutputStream may need to // call IsSerializationDeterministic() to serialize in the intended way. This // CodedOutputStream cannot enforce a desire for deterministic serialization // by itself. + bool IsSerializationDeterministic() const { - return is_serialization_deterministic_; + return impl_.IsSerializationDeterministic(); } static bool IsDefaultSerializationDeterministic() { @@ -861,33 +1250,18 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { std::memory_order_relaxed) != 0; } - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream); + template + void Serialize(const Func& func); - ZeroCopyOutputStream* output_; - uint8* buffer_; - int buffer_size_; - int total_bytes_; // Sum of sizes of all buffers seen so far. - bool had_error_; // Whether an error occurred during output. - bool aliasing_enabled_; // See EnableAliasing(). - bool is_serialization_deterministic_; - static std::atomic default_serialization_deterministic_; - - // Advance the buffer by a given number of bytes. - void Advance(int amount); + uint8* Cur() const { return cur_; } + void SetCur(uint8* ptr) { cur_ = ptr; } + EpsCopyOutputStream* EpsCopy() { return &impl_; } - // Called when the buffer runs out to request more data. Implies an - // Advance(buffer_size_). - bool Refresh(); - - // Like WriteRaw() but may avoid copying if the underlying - // ZeroCopyOutputStream supports it. - void WriteAliasedRaw(const void* buffer, int size); - - // If this write might cross the end of the buffer, we compose the bytes first - // then use WriteRaw(). - void WriteVarint32SlowPath(uint32 value); - void WriteVarint64SlowPath(uint64 value); + 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 @@ -900,6 +1274,7 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { static void SetDefaultSerializationDeterministic() { default_serialization_deterministic_.store(true, std::memory_order_relaxed); } + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream); }; // inline methods ==================================================== @@ -908,7 +1283,7 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { inline bool CodedInputStream::ReadVarint32(uint32* value) { uint32 v = 0; - if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) { + if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) { v = *buffer_; if (v < 0x80) { *value = v; @@ -922,7 +1297,7 @@ inline bool CodedInputStream::ReadVarint32(uint32* value) { } inline bool CodedInputStream::ReadVarint64(uint64* value) { - if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) { + if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) { *value = *buffer_; Advance(1); return true; @@ -933,7 +1308,7 @@ inline bool CodedInputStream::ReadVarint64(uint64* value) { } inline bool CodedInputStream::ReadVarintSizeAsInt(int* value) { - if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) { + if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) { int v = *buffer_; if (v < 0x80) { *value = v; @@ -947,14 +1322,13 @@ inline bool CodedInputStream::ReadVarintSizeAsInt(int* value) { // static inline const uint8* CodedInputStream::ReadLittleEndian32FromArray( - const uint8* buffer, - uint32* value) { + 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) | + *value = (static_cast(buffer[0])) | + (static_cast(buffer[1]) << 8) | (static_cast(buffer[2]) << 16) | (static_cast(buffer[3]) << 24); return buffer + sizeof(*value); @@ -962,29 +1336,27 @@ inline const uint8* CodedInputStream::ReadLittleEndian32FromArray( } // static inline const uint8* CodedInputStream::ReadLittleEndian64FromArray( - const uint8* buffer, - uint64* value) { + 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) | + 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) | + 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); + *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 (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast(sizeof(*value)))) { + if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast(sizeof(*value)))) { buffer_ = ReadLittleEndian32FromArray(buffer_, value); return true; } else { @@ -997,7 +1369,7 @@ inline bool CodedInputStream::ReadLittleEndian32(uint32* value) { inline bool CodedInputStream::ReadLittleEndian64(uint64* value) { #if defined(PROTOBUF_LITTLE_ENDIAN) - if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast(sizeof(*value)))) { + if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast(sizeof(*value)))) { buffer_ = ReadLittleEndian64FromArray(buffer_, value); return true; } else { @@ -1010,7 +1382,7 @@ inline bool CodedInputStream::ReadLittleEndian64(uint64* value) { inline uint32 CodedInputStream::ReadTagNoLastTag() { uint32 v = 0; - if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) { + if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) { v = *buffer_; if (v < 0x80) { Advance(1); @@ -1027,7 +1399,7 @@ inline std::pair CodedInputStream::ReadTagWithCutoffNoLastTag( // constant, and things like "cutoff >= kMax1ByteVarint" to be evaluated at // compile time. uint32 first_byte_or_zero = 0; - if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) { + 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? @@ -1041,8 +1413,8 @@ inline std::pair CodedInputStream::ReadTagWithCutoffNoLastTag( // 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 && GOOGLE_PREDICT_TRUE(buffer_ + 1 < buffer_end_) && - GOOGLE_PREDICT_TRUE((buffer_[0] & ~buffer_[1]) >= 0x80)) { + 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); @@ -1071,14 +1443,15 @@ inline bool CodedInputStream::ConsumedEntireMessage() { inline bool CodedInputStream::ExpectTag(uint32 expected) { if (expected < (1 << 7)) { - if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] == expected) { + if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && + buffer_[0] == expected) { Advance(1); return true; } else { return false; } } else if (expected < (1 << 14)) { - if (GOOGLE_PREDICT_TRUE(BufferSize() >= 2) && + if (PROTOBUF_PREDICT_TRUE(BufferSize() >= 2) && buffer_[0] == static_cast(expected | 0x80) && buffer_[1] == static_cast(expected >> 7)) { Advance(2); @@ -1092,8 +1465,8 @@ inline bool CodedInputStream::ExpectTag(uint32 expected) { } } -inline const uint8* CodedInputStream::ExpectTagFromArray( - const uint8* buffer, uint32 expected) { +inline const uint8* CodedInputStream::ExpectTagFromArray(const uint8* buffer, + uint32 expected) { if (expected < (1 << 7)) { if (buffer[0] == expected) { return buffer + 1; @@ -1104,7 +1477,7 @@ inline const uint8* CodedInputStream::ExpectTagFromArray( return buffer + 2; } } - return NULL; + return nullptr; } inline void CodedInputStream::GetDirectBufferPointerInline(const void** data, @@ -1117,9 +1490,8 @@ 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_))) { + 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; @@ -1132,36 +1504,109 @@ inline int CodedInputStream::CurrentPosition() const { return total_bytes_read_ - (BufferSize() + buffer_size_after_limit_); } -inline uint8* CodedOutputStream::GetDirectBufferForNBytesAndAdvance(int size) { - if (buffer_size_ < size) { - return NULL; - } else { - uint8* result = buffer_; - Advance(size); - return result; +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) { - while (value >= 0x80) { - *target = static_cast(value | 0x80); - value >>= 7; - ++target; - } - *target = static_cast(value); - return target + 1; + return EpsCopyOutputStream::UnsafeVarint(value, target); } inline uint8* CodedOutputStream::WriteVarint64ToArray(uint64 value, uint8* target) { - while (value >= 0x80) { - *target = static_cast(value | 0x80); - value >>= 7; - ++target; - } - *target = static_cast(value); - return target + 1; + return EpsCopyOutputStream::UnsafeVarint(value, target); } inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) { @@ -1179,7 +1624,7 @@ inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value, memcpy(target, &value, sizeof(value)); #else target[0] = static_cast(value); - target[1] = static_cast(value >> 8); + target[1] = static_cast(value >> 8); target[2] = static_cast(value >> 16); target[3] = static_cast(value >> 24); #endif @@ -1195,11 +1640,11 @@ inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value, uint32 part1 = static_cast(value >> 32); target[0] = static_cast(part0); - target[1] = static_cast(part0 >> 8); + 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[5] = static_cast(part1 >> 8); target[6] = static_cast(part1 >> 16); target[7] = static_cast(part1 >> 24); #endif @@ -1207,37 +1652,18 @@ inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value, } inline void CodedOutputStream::WriteVarint32(uint32 value) { - if (buffer_size_ >= 5) { - // Fast path: We have enough bytes left in the buffer to guarantee that - // this write won't cross the end, so we can skip the checks. - uint8* target = buffer_; - uint8* end = WriteVarint32ToArray(value, target); - int size = static_cast(end - target); - Advance(size); - } else { - WriteVarint32SlowPath(value); - } + impl_.EnsureSpace(&cur_); + SetCur(WriteVarint32ToArray(value, Cur())); } inline void CodedOutputStream::WriteVarint64(uint64 value) { - if (buffer_size_ >= 10) { - // Fast path: We have enough bytes left in the buffer to guarantee that - // this write won't cross the end, so we can skip the checks. - uint8* target = buffer_; - uint8* end = WriteVarint64ToArray(value, target); - int size = static_cast(end - target); - Advance(size); - } else { - WriteVarint64SlowPath(value); - } + impl_.EnsureSpace(&cur_); + SetCur(WriteVarint64ToArray(value, Cur())); } -inline void CodedOutputStream::WriteTag(uint32 value) { - WriteVarint32(value); -} +inline void CodedOutputStream::WriteTag(uint32 value) { WriteVarint32(value); } -inline uint8* CodedOutputStream::WriteTagToArray( - uint32 value, uint8* target) { +inline uint8* CodedOutputStream::WriteTagToArray(uint32 value, uint8* target) { return WriteVarint32ToArray(value, target); } @@ -1263,145 +1689,38 @@ inline size_t CodedOutputStream::VarintSize64(uint64 value) { inline size_t CodedOutputStream::VarintSize32SignExtended(int32 value) { if (value < 0) { - return 10; // TODO(kenton): Make this a symbolic constant. + return 10; // TODO(kenton): Make this a symbolic constant. } else { return VarintSize32(static_cast(value)); } } -inline void CodedOutputStream::WriteString(const string& str) { +inline void CodedOutputStream::WriteString(const std::string& str) { WriteRaw(str.data(), static_cast(str.size())); } -inline void CodedOutputStream::WriteRawMaybeAliased( - const void* data, int size) { - if (aliasing_enabled_) { - WriteAliasedRaw(data, size); - } else { - WriteRaw(data, size); - } -} - -inline uint8* CodedOutputStream::WriteStringToArray( - const string& str, uint8* target) { - return WriteRawToArray(str.data(), static_cast(str.size()), target); -} - -inline int CodedOutputStream::ByteCount() const { - return total_bytes_ - buffer_size_; -} - -inline void CodedInputStream::Advance(int amount) { - buffer_ += amount; -} - -inline void CodedOutputStream::Advance(int amount) { - buffer_ += amount; - buffer_size_ -= 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_(NULL), - buffer_end_(NULL), - 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_(NULL), - extension_factory_(NULL) { - // Eagerly Refresh() so buffer space is immediately available. - Refresh(); +inline void CodedOutputStream::WriteRawMaybeAliased(const void* data, + int size) { + cur_ = impl_.WriteRawMaybeAliased(data, size, cur_); } -inline CodedInputStream::CodedInputStream(const uint8* buffer, int size) - : buffer_(buffer), - buffer_end_(buffer + size), - input_(NULL), - 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_(NULL), - extension_factory_(NULL) { - // Note that setting current_limit_ == size is important to prevent some - // code paths from trying to access input_ and segfaulting. +inline uint8* CodedOutputStream::WriteRawToArray(const void* data, int size, + uint8* target) { + memcpy(target, data, size); + return target + size; } -inline bool CodedInputStream::IsFlat() const { - return input_ == NULL; -} - -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::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) +#pragma runtime_checks("c", restore) #endif // _MSC_VER && !defined(__INTEL_COMPILER) #include diff --git a/src/google/protobuf/io/coded_stream_inl.h b/src/google/protobuf/io/coded_stream_inl.h index d95b06e04e..0c10e65546 100644 --- a/src/google/protobuf/io/coded_stream_inl.h +++ b/src/google/protobuf/io/coded_stream_inl.h @@ -36,18 +36,18 @@ #ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ #define GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ +#include #include #include #include #include -#include #include namespace google { namespace protobuf { namespace io { -inline bool CodedInputStream::InternalReadStringInline(string* buffer, +inline bool CodedInputStream::InternalReadStringInline(std::string* buffer, int size) { if (size < 0) return false; // security: size is often user-supplied diff --git a/src/google/protobuf/io/coded_stream_unittest.cc b/src/google/protobuf/io/coded_stream_unittest.cc index bcda8c81fd..52b7a7bada 100644 --- a/src/google/protobuf/io/coded_stream_unittest.cc +++ b/src/google/protobuf/io/coded_stream_unittest.cc @@ -49,10 +49,12 @@ #include #include +#include + // This declares an unsigned long long integer literal in a portable way. // (The original macro is way too big and ruins my formatting.) #undef ULL -#define ULL(x) GOOGLE_ULONGLONG(x) +#define ULL(x) PROTOBUF_ULONGLONG(x) namespace google { @@ -84,57 +86,51 @@ namespace { // TODO(kenton): gTest now supports "parameterized tests" which would be // a better way to accomplish this. Rewrite when time permits. -#define TEST_1D(FIXTURE, NAME, CASES) \ - class FIXTURE##_##NAME##_DD : public FIXTURE { \ - protected: \ - template \ - void DoSingleCase(const CaseType& CASES##_case); \ - }; \ - \ - TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ - for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \ - SCOPED_TRACE(testing::Message() \ - << #CASES " case #" << i << ": " << CASES[i]); \ - DoSingleCase(CASES[i]); \ - } \ - } \ - \ - template \ +#define TEST_1D(FIXTURE, NAME, CASES) \ + class FIXTURE##_##NAME##_DD : public FIXTURE { \ + protected: \ + template \ + void DoSingleCase(const CaseType& CASES##_case); \ + }; \ + \ + TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ + for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \ + SCOPED_TRACE(testing::Message() \ + << #CASES " case #" << i << ": " << CASES[i]); \ + DoSingleCase(CASES[i]); \ + } \ + } \ + \ + template \ void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType& CASES##_case) -#define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \ - class FIXTURE##_##NAME##_DD : public FIXTURE { \ - protected: \ - template \ - void DoSingleCase(const CaseType1& CASES1##_case, \ - const CaseType2& CASES2##_case); \ - }; \ - \ - TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ - for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \ - for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \ - SCOPED_TRACE(testing::Message() \ - << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \ - << #CASES2 " case #" << j << ": " << CASES2[j]); \ - DoSingleCase(CASES1[i], CASES2[j]); \ - } \ - } \ - } \ - \ - template \ - void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \ +#define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \ + class FIXTURE##_##NAME##_DD : public FIXTURE { \ + protected: \ + template \ + void DoSingleCase(const CaseType1& CASES1##_case, \ + const CaseType2& CASES2##_case); \ + }; \ + \ + TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ + for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \ + for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \ + SCOPED_TRACE(testing::Message() \ + << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \ + << #CASES2 " case #" << j << ": " << CASES2[j]); \ + DoSingleCase(CASES1[i], CASES2[j]); \ + } \ + } \ + } \ + \ + template \ + void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \ const CaseType2& CASES2##_case) // =================================================================== class CodedStreamTest : public testing::Test { protected: - // Helper method used by tests for bytes warning. See implementation comment - // for further information. - static void SetupTotalBytesLimitWarningTest( - int total_bytes_limit, int warning_threshold, - std::vector* out_errors, std::vector* out_warnings); - // Buffer used during most of the tests. This assumes tests run sequentially. static const int kBufferSize = 1024 * 64; static uint8 buffer_[kBufferSize]; @@ -154,9 +150,9 @@ const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024}; // Varint tests. struct VarintCase { - uint8 bytes[10]; // Encoded bytes. - int size; // Encoded size, in bytes. - uint64 value; // Parsed value. + uint8 bytes[10]; // Encoded bytes. + int size; // Encoded size, in bytes. + uint64 value; // Parsed value. }; inline std::ostream& operator<<(std::ostream& os, const VarintCase& c) { @@ -164,28 +160,32 @@ inline std::ostream& operator<<(std::ostream& os, const VarintCase& c) { } VarintCase kVarintCases[] = { - // 32-bit values - {{0x00} , 1, 0}, - {{0x01} , 1, 1}, - {{0x7f} , 1, 127}, - {{0xa2, 0x74}, 2, (0x22 << 0) | (0x74 << 7)}, // 14882 - {{0xbe, 0xf7, 0x92, 0x84, 0x0b}, 5, // 2961488830 - (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | - (ULL(0x0b) << 28)}, - - // 64-bit - {{0xbe, 0xf7, 0x92, 0x84, 0x1b}, 5, // 7256456126 - (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | - (ULL(0x1b) << 28)}, - {{0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49}, 8, // 41256202580718336 - (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) | - (ULL(0x43) << 28) | (ULL(0x49) << 35) | (ULL(0x24) << 42) | - (ULL(0x49) << 49)}, - // 11964378330978735131 - {{0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01}, 10, - (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | - (ULL(0x3b) << 28) | (ULL(0x56) << 35) | (ULL(0x00) << 42) | - (ULL(0x05) << 49) | (ULL(0x26) << 56) | (ULL(0x01) << 63)}, + // 32-bit values + {{0x00}, 1, 0}, + {{0x01}, 1, 1}, + {{0x7f}, 1, 127}, + {{0xa2, 0x74}, 2, (0x22 << 0) | (0x74 << 7)}, // 14882 + {{0xbe, 0xf7, 0x92, 0x84, 0x0b}, + 5, // 2961488830 + (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | + (ULL(0x0b) << 28)}, + + // 64-bit + {{0xbe, 0xf7, 0x92, 0x84, 0x1b}, + 5, // 7256456126 + (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | + (ULL(0x1b) << 28)}, + {{0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49}, + 8, // 41256202580718336 + (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) | + (ULL(0x43) << 28) | (ULL(0x49) << 35) | (ULL(0x24) << 42) | + (ULL(0x49) << 49)}, + // 11964378330978735131 + {{0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01}, + 10, + (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | + (ULL(0x3b) << 28) | (ULL(0x56) << 35) | (ULL(0x00) << 42) | + (ULL(0x05) << 49) | (ULL(0x26) << 56) | (ULL(0x01) << 63)}, }; TEST_2D(CodedStreamTest, ReadVarint32, kVarintCases, kBlockSizes) { @@ -226,15 +226,14 @@ TEST_F(CodedStreamTest, EmptyInputBeforeEos) { class In : public ZeroCopyInputStream { public: In() : count_(0) {} + private: virtual bool Next(const void** data, int* size) { *data = NULL; *size = 0; return count_++ < 2; } - virtual void BackUp(int count) { - GOOGLE_LOG(FATAL) << "Tests never call this."; - } + virtual void BackUp(int count) { GOOGLE_LOG(FATAL) << "Tests never call this."; } virtual bool Skip(int count) { GOOGLE_LOG(FATAL) << "Tests never call this."; return false; @@ -288,9 +287,8 @@ TEST_1D(CodedStreamTest, ExpectTagFromArray, kVarintCases) { // If the expectation succeeds, it should return a pointer past the tag. if (kVarintCases_case.size <= 2) { - EXPECT_TRUE(NULL == - CodedInputStream::ExpectTagFromArray(buffer_, - expected_value + 1)); + EXPECT_TRUE(NULL == CodedInputStream::ExpectTagFromArray( + buffer_, expected_value + 1)); EXPECT_TRUE(buffer_ + kVarintCases_case.size == CodedInputStream::ExpectTagFromArray(buffer_, expected_value)); } else { @@ -333,7 +331,7 @@ TEST_2D(CodedStreamTest, WriteVarint32, kVarintCases, kBlockSizes) { EXPECT_EQ(kVarintCases_case.size, output.ByteCount()); EXPECT_EQ(0, - memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size)); + memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size)); } TEST_2D(CodedStreamTest, WriteVarint64, kVarintCases, kBlockSizes) { @@ -350,19 +348,17 @@ TEST_2D(CodedStreamTest, WriteVarint64, kVarintCases, kBlockSizes) { EXPECT_EQ(kVarintCases_case.size, output.ByteCount()); EXPECT_EQ(0, - memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size)); + memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size)); } // This test causes gcc 3.3.5 (and earlier?) to give the cryptic error: // "sorry, unimplemented: `method_call_expr' not supported by dump_expr" #if !defined(__GNUC__) || __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) -int32 kSignExtendedVarintCases[] = { - 0, 1, -1, 1237894, -37895138 -}; +int32 kSignExtendedVarintCases[] = {0, 1, -1, 1237894, -37895138}; -TEST_2D(CodedStreamTest, WriteVarint32SignExtended, - kSignExtendedVarintCases, kBlockSizes) { +TEST_2D(CodedStreamTest, WriteVarint32SignExtended, kSignExtendedVarintCases, + kBlockSizes) { ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case); { @@ -416,22 +412,23 @@ inline std::ostream& operator<<(std::ostream& os, const VarintErrorCase& c) { } const VarintErrorCase kVarintErrorCases[] = { - // Control case. (Insures that there isn't something else wrong that - // makes parsing always fail.) - {{0x00}, 1, true}, + // Control case. (Insures that there isn't something else wrong that + // makes parsing always fail.) + {{0x00}, 1, true}, - // No input data. - {{}, 0, false}, + // No input data. + {{}, 0, false}, - // Input ends unexpectedly. - {{0xf0, 0xab}, 2, false}, + // Input ends unexpectedly. + {{0xf0, 0xab}, 2, false}, - // Input ends unexpectedly after 32 bits. - {{0xf0, 0xab, 0xc9, 0x9a, 0xf8, 0xb2}, 6, false}, + // Input ends unexpectedly after 32 bits. + {{0xf0, 0xab, 0xc9, 0x9a, 0xf8, 0xb2}, 6, false}, - // Longer than 10 bytes. - {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, - 11, false}, + // Longer than 10 bytes. + {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, + 11, + false}, }; TEST_2D(CodedStreamTest, ReadVarint32Error, kVarintErrorCases, kBlockSizes) { @@ -497,14 +494,14 @@ inline std::ostream& operator<<(std::ostream& os, const VarintSizeCase& c) { } VarintSizeCase kVarintSizeCases[] = { - {0u, 1}, - {1u, 1}, - {127u, 1}, - {128u, 2}, - {758923u, 3}, - {4000000000u, 5}, - {ULL(41256202580718336), 8}, - {ULL(11964378330978735131), 10}, + {0u, 1}, + {1u, 1}, + {127u, 1}, + {128u, 2}, + {758923u, 3}, + {4000000000u, 5}, + {ULL(41256202580718336), 8}, + {ULL(11964378330978735131), 10}, }; TEST_1D(CodedStreamTest, VarintSize32, kVarintSizeCases) { @@ -514,13 +511,13 @@ TEST_1D(CodedStreamTest, VarintSize32, kVarintSizeCases) { } EXPECT_EQ(kVarintSizeCases_case.size, - CodedOutputStream::VarintSize32( - static_cast(kVarintSizeCases_case.value))); + CodedOutputStream::VarintSize32( + static_cast(kVarintSizeCases_case.value))); } TEST_1D(CodedStreamTest, VarintSize64, kVarintSizeCases) { EXPECT_EQ(kVarintSizeCases_case.size, - CodedOutputStream::VarintSize64(kVarintSizeCases_case.value)); + CodedOutputStream::VarintSize64(kVarintSizeCases_case.value)); } TEST_F(CodedStreamTest, VarintSize32PowersOfTwo) { @@ -549,13 +546,13 @@ TEST_F(CodedStreamTest, VarintSize64PowersOfTwo) { // Fixed-size int tests struct Fixed32Case { - uint8 bytes[sizeof(uint32)]; // Encoded bytes. - uint32 value; // Parsed value. + uint8 bytes[sizeof(uint32)]; // Encoded bytes. + uint32 value; // Parsed value. }; struct Fixed64Case { - uint8 bytes[sizeof(uint64)]; // Encoded bytes. - uint64 value; // Parsed value. + uint8 bytes[sizeof(uint64)]; // Encoded bytes. + uint64 value; // Parsed value. }; inline std::ostream& operator<<(std::ostream& os, const Fixed32Case& c) { @@ -567,13 +564,13 @@ inline std::ostream& operator<<(std::ostream& os, const Fixed64Case& c) { } Fixed32Case kFixed32Cases[] = { - {{0xef, 0xcd, 0xab, 0x90}, 0x90abcdefu}, - {{0x12, 0x34, 0x56, 0x78}, 0x78563412u}, + {{0xef, 0xcd, 0xab, 0x90}, 0x90abcdefu}, + {{0x12, 0x34, 0x56, 0x78}, 0x78563412u}, }; Fixed64Case kFixed64Cases[] = { - {{0xef, 0xcd, 0xab, 0x90, 0x12, 0x34, 0x56, 0x78}, ULL(0x7856341290abcdef)}, - {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, ULL(0x8877665544332211)}, + {{0xef, 0xcd, 0xab, 0x90, 0x12, 0x34, 0x56, 0x78}, ULL(0x7856341290abcdef)}, + {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, ULL(0x8877665544332211)}, }; TEST_2D(CodedStreamTest, ReadLittleEndian32, kFixed32Cases, kBlockSizes) { @@ -644,8 +641,8 @@ TEST_1D(CodedStreamTest, ReadLittleEndian32FromArray, kFixed32Cases) { memcpy(buffer_, kFixed32Cases_case.bytes, sizeof(kFixed32Cases_case.bytes)); uint32 value; - const uint8* end = CodedInputStream::ReadLittleEndian32FromArray( - buffer_, &value); + const uint8* end = + CodedInputStream::ReadLittleEndian32FromArray(buffer_, &value); EXPECT_EQ(kFixed32Cases_case.value, value); EXPECT_TRUE(end == buffer_ + sizeof(value)); } @@ -654,8 +651,8 @@ TEST_1D(CodedStreamTest, ReadLittleEndian64FromArray, kFixed64Cases) { memcpy(buffer_, kFixed64Cases_case.bytes, sizeof(kFixed64Cases_case.bytes)); uint64 value; - const uint8* end = CodedInputStream::ReadLittleEndian64FromArray( - buffer_, &value); + const uint8* end = + CodedInputStream::ReadLittleEndian64FromArray(buffer_, &value); EXPECT_EQ(kFixed64Cases_case.value, value); EXPECT_TRUE(end == buffer_ + sizeof(value)); } @@ -703,7 +700,7 @@ TEST_1D(CodedStreamTest, ReadString, kBlockSizes) { { CodedInputStream coded_input(&input); - string str; + std::string str; EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes))); EXPECT_EQ(kRawBytes, str); } @@ -718,7 +715,7 @@ TEST_1D(CodedStreamTest, ReadStringImpossiblyLarge, kBlockSizes) { { CodedInputStream coded_input(&input); - string str; + std::string str; // Try to read a gigabyte. EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30)); } @@ -729,14 +726,14 @@ TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnStack) { // crashes while the above did not. uint8 buffer[8]; CodedInputStream coded_input(buffer, 8); - string str; + std::string str; EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30)); } TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnHeap) { std::unique_ptr buffer(new uint8[8]); CodedInputStream coded_input(buffer.get(), 8); - string str; + std::string str; EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30)); } @@ -749,7 +746,7 @@ TEST_1D(CodedStreamTest, ReadStringReservesMemoryOnTotalLimit, kBlockSizes) { coded_input.SetTotalBytesLimit(sizeof(kRawBytes), sizeof(kRawBytes)); EXPECT_EQ(sizeof(kRawBytes), coded_input.BytesUntilTotalBytesLimit()); - string str; + std::string str; EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes))); EXPECT_EQ(sizeof(kRawBytes) - strlen(kRawBytes), coded_input.BytesUntilTotalBytesLimit()); @@ -769,7 +766,7 @@ TEST_1D(CodedStreamTest, ReadStringReservesMemoryOnPushedLimit, kBlockSizes) { CodedInputStream coded_input(&input); coded_input.PushLimit(sizeof(buffer_)); - string str; + std::string str; EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes))); EXPECT_EQ(kRawBytes, str); // TODO(liujisi): Replace with a more meaningful test (see cl/60966023). @@ -789,7 +786,7 @@ TEST_F(CodedStreamTest, ReadStringNoReservationIfLimitsNotSet) { { CodedInputStream coded_input(&input); - string str; + std::string str; EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes))); EXPECT_EQ(kRawBytes, str); // Note: this check depends on string class implementation. It @@ -814,12 +811,12 @@ TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsNegative) { CodedInputStream coded_input(&input); coded_input.PushLimit(sizeof(buffer_)); - string str; + std::string str; EXPECT_FALSE(coded_input.ReadString(&str, -1)); // Note: this check depends on string class implementation. It // expects that string will always allocate the same amount of // memory for an empty string. - EXPECT_EQ(string().capacity(), str.capacity()); + EXPECT_EQ(std::string().capacity(), str.capacity()); } } @@ -834,7 +831,7 @@ TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsLarge) { CodedInputStream coded_input(&input); coded_input.PushLimit(sizeof(buffer_)); - string str; + std::string str; EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30)); EXPECT_GT(1 << 30, str.capacity()); } @@ -851,7 +848,7 @@ TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheLimit) { CodedInputStream coded_input(&input); coded_input.PushLimit(16); - string str; + std::string str; EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes))); // Note: this check depends on string class implementation. It // expects that string will allocate less than strlen(kRawBytes) @@ -871,7 +868,7 @@ TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheTotalBytesLimit) { CodedInputStream coded_input(&input); coded_input.SetTotalBytesLimit(16, 16); - string str; + std::string str; EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes))); // Note: this check depends on string class implementation. It // expects that string will allocate less than strlen(kRawBytes) @@ -893,7 +890,7 @@ TEST_F(CodedStreamTest, coded_input.PushLimit(sizeof(buffer_)); coded_input.SetTotalBytesLimit(16, 16); - string str; + std::string str; EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes))); // Note: this check depends on string class implementation. It // expects that string will allocate less than strlen(kRawBytes) @@ -916,7 +913,7 @@ TEST_F(CodedStreamTest, coded_input.SetTotalBytesLimit(sizeof(buffer_), sizeof(buffer_)); EXPECT_EQ(sizeof(buffer_), coded_input.BytesUntilTotalBytesLimit()); - string str; + std::string str; EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes))); // Note: this check depends on string class implementation. It // expects that string will allocate less than strlen(kRawBytes) @@ -930,7 +927,7 @@ TEST_F(CodedStreamTest, // Skip const char kSkipTestBytes[] = - ""; + ""; TEST_1D(CodedStreamTest, SkipInput, kBlockSizes) { memcpy(buffer_, kSkipTestBytes, sizeof(kSkipTestBytes)); @@ -939,7 +936,7 @@ TEST_1D(CodedStreamTest, SkipInput, kBlockSizes) { { CodedInputStream coded_input(&input); - string str; + std::string str; EXPECT_TRUE(coded_input.ReadString(&str, strlen(""))); EXPECT_EQ("", str); EXPECT_TRUE(coded_input.Skip(strlen(""))); @@ -1012,41 +1009,6 @@ TEST_F(CodedStreamTest, GetDirectBufferPointerInlineInput) { EXPECT_EQ(0, size); } -TEST_F(CodedStreamTest, GetDirectBufferPointerOutput) { - ArrayOutputStream output(buffer_, sizeof(buffer_), 8); - CodedOutputStream coded_output(&output); - - void* ptr; - int size; - - EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size)); - EXPECT_EQ(buffer_, ptr); - EXPECT_EQ(8, size); - - // Peeking again should return the same pointer. - EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size)); - EXPECT_EQ(buffer_, ptr); - EXPECT_EQ(8, size); - - // Skip forward in the same buffer then peek again. - EXPECT_TRUE(coded_output.Skip(3)); - EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size)); - EXPECT_EQ(buffer_ + 3, ptr); - EXPECT_EQ(5, size); - - // Skip to end of buffer and peek -- should get next buffer. - EXPECT_TRUE(coded_output.Skip(5)); - EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size)); - EXPECT_EQ(buffer_ + 8, ptr); - EXPECT_EQ(8, size); - - // Skip over multiple buffers. - EXPECT_TRUE(coded_output.Skip(22)); - EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size)); - EXPECT_EQ(buffer_ + 30, ptr); - EXPECT_EQ(2, size); -} - // ------------------------------------------------------------------- // Limits @@ -1223,11 +1185,11 @@ TEST_F(CodedStreamTest, TotalBytesLimit) { coded_input.SetTotalBytesLimit(16, -1); EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit()); - string str; + std::string str; EXPECT_TRUE(coded_input.ReadString(&str, 16)); EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit()); - std::vector errors; + std::vector errors; { ScopedMemoryLog error_log; @@ -1237,7 +1199,8 @@ TEST_F(CodedStreamTest, TotalBytesLimit) { ASSERT_EQ(1, errors.size()); EXPECT_PRED_FORMAT2(testing::IsSubstring, - "A protocol message was rejected because it was too big", errors[0]); + "A protocol message was rejected because it was too big", + errors[0]); coded_input.SetTotalBytesLimit(32, -1); EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit()); @@ -1256,7 +1219,7 @@ TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) { CodedInputStream::Limit limit = coded_input.PushLimit(16); // Read 16 bytes. - string str; + std::string str; EXPECT_TRUE(coded_input.ReadString(&str, 16)); // Read a tag. Should fail, but report being a valid endpoint since it's @@ -1273,62 +1236,41 @@ TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) { EXPECT_FALSE(coded_input.ConsumedEntireMessage()); } -// This method is used by the tests below. -// It constructs a CodedInputStream with the given limits and tries to read 2KiB -// of data from it. Then it returns the logged errors and warnings in the given -// vectors. -void CodedStreamTest::SetupTotalBytesLimitWarningTest( - int total_bytes_limit, int warning_threshold, - std::vector* out_errors, std::vector* out_warnings) { - ArrayInputStream raw_input(buffer_, sizeof(buffer_), 128); - - ScopedMemoryLog scoped_log; - { - CodedInputStream input(&raw_input); - input.SetTotalBytesLimit(total_bytes_limit, warning_threshold); - string str; - EXPECT_TRUE(input.ReadString(&str, 2048)); - } - - *out_errors = scoped_log.GetMessages(ERROR); - *out_warnings = scoped_log.GetMessages(WARNING); -} - TEST_F(CodedStreamTest, RecursionLimit) { ArrayInputStream input(buffer_, sizeof(buffer_)); CodedInputStream coded_input(&input); coded_input.SetRecursionLimit(4); // This is way too much testing for a counter. - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6 - coded_input.DecrementRecursionDepth(); // 5 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6 - coded_input.DecrementRecursionDepth(); // 5 - coded_input.DecrementRecursionDepth(); // 4 - coded_input.DecrementRecursionDepth(); // 3 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 - coded_input.DecrementRecursionDepth(); // 4 - coded_input.DecrementRecursionDepth(); // 3 - coded_input.DecrementRecursionDepth(); // 2 - coded_input.DecrementRecursionDepth(); // 1 - coded_input.DecrementRecursionDepth(); // 0 - coded_input.DecrementRecursionDepth(); // 0 - coded_input.DecrementRecursionDepth(); // 0 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6 + coded_input.DecrementRecursionDepth(); // 5 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6 + coded_input.DecrementRecursionDepth(); // 5 + coded_input.DecrementRecursionDepth(); // 4 + coded_input.DecrementRecursionDepth(); // 3 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 + coded_input.DecrementRecursionDepth(); // 4 + coded_input.DecrementRecursionDepth(); // 3 + coded_input.DecrementRecursionDepth(); // 2 + coded_input.DecrementRecursionDepth(); // 1 + coded_input.DecrementRecursionDepth(); // 0 + coded_input.DecrementRecursionDepth(); // 0 + coded_input.DecrementRecursionDepth(); // 0 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 coded_input.SetRecursionLimit(6); - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 6 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 7 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 6 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 7 } @@ -1360,12 +1302,16 @@ class ReallyBigInputStream : public ZeroCopyInputStream { } } - void BackUp(int count) { - backup_amount_ = count; - } + void BackUp(int count) { backup_amount_ = count; } - bool Skip(int count) { GOOGLE_LOG(FATAL) << "Not implemented."; return false; } - int64 ByteCount() const { GOOGLE_LOG(FATAL) << "Not implemented."; return 0; } + bool Skip(int count) { + GOOGLE_LOG(FATAL) << "Not implemented."; + return false; + } + int64 ByteCount() const { + GOOGLE_LOG(FATAL) << "Not implemented."; + return 0; + } int backup_amount_; @@ -1379,12 +1325,12 @@ TEST_F(CodedStreamTest, InputOver2G) { // input.BackUp() with the correct number of bytes on destruction. ReallyBigInputStream input; - std::vector errors; + std::vector errors; { ScopedMemoryLog error_log; CodedInputStream coded_input(&input); - string str; + std::string str; EXPECT_TRUE(coded_input.ReadString(&str, 512)); EXPECT_TRUE(coded_input.ReadString(&str, 1024)); errors = error_log.GetMessages(ERROR); diff --git a/src/google/protobuf/io/gzip_stream.cc b/src/google/protobuf/io/gzip_stream.cc index a569eff0a7..a3e04384eb 100644 --- a/src/google/protobuf/io/gzip_stream.cc +++ b/src/google/protobuf/io/gzip_stream.cc @@ -46,8 +46,8 @@ namespace io { static const int kDefaultBufferSize = 65536; -GzipInputStream::GzipInputStream( - ZeroCopyInputStream* sub_stream, Format format, int buffer_size) +GzipInputStream::GzipInputStream(ZeroCopyInputStream* sub_stream, Format format, + int buffer_size) : format_(format), sub_stream_(sub_stream), zerror_(Z_OK), byte_count_(0) { zcontext_.state = Z_NULL; zcontext_.zalloc = Z_NULL; @@ -74,15 +74,21 @@ GzipInputStream::~GzipInputStream() { zerror_ = inflateEnd(&zcontext_); } -static inline int internalInflateInit2( - z_stream* zcontext, GzipInputStream::Format format) { +static inline int internalInflateInit2(z_stream* zcontext, + GzipInputStream::Format format) { int windowBitsFormat = 0; switch (format) { - case GzipInputStream::GZIP: windowBitsFormat = 16; break; - case GzipInputStream::AUTO: windowBitsFormat = 32; break; - case GzipInputStream::ZLIB: windowBitsFormat = 0; break; + case GzipInputStream::GZIP: + windowBitsFormat = 16; + break; + case GzipInputStream::AUTO: + windowBitsFormat = 32; + break; + case GzipInputStream::ZLIB: + windowBitsFormat = 0; + break; } - return inflateInit2(zcontext, /* windowBits */15 | windowBitsFormat); + return inflateInit2(zcontext, /* windowBits */ 15 | windowBitsFormat); } int GzipInputStream::Inflate(int flush) { @@ -122,8 +128,8 @@ void GzipInputStream::DoNextOutput(const void** data, int* size) { // implements ZeroCopyInputStream ---------------------------------- bool GzipInputStream::Next(const void** data, int* size) { - bool ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) - || (zerror_ == Z_BUF_ERROR); + bool ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) || + (zerror_ == Z_BUF_ERROR); if ((!ok) || (zcontext_.next_out == NULL)) { return false; } @@ -154,8 +160,8 @@ bool GzipInputStream::Next(const void** data, int* size) { // The underlying stream's Next returned false inside Inflate. return false; } - ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) - || (zerror_ == Z_BUF_ERROR); + ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) || + (zerror_ == Z_BUF_ERROR); if (!ok) { return false; } @@ -230,13 +236,10 @@ void GzipOutputStream::Init(ZeroCopyOutputStream* sub_stream, if (options.format == ZLIB) { windowBitsFormat = 0; } - zerror_ = deflateInit2( - &zcontext_, - options.compression_level, - Z_DEFLATED, - /* windowBits */15 | windowBitsFormat, - /* memLevel (default) */8, - options.compression_strategy); + zerror_ = + deflateInit2(&zcontext_, options.compression_level, Z_DEFLATED, + /* windowBits */ 15 | windowBitsFormat, + /* memLevel (default) */ 8, options.compression_strategy); } GzipOutputStream::~GzipOutputStream() { @@ -305,9 +308,9 @@ int64 GzipOutputStream::ByteCount() const { bool GzipOutputStream::Flush() { zerror_ = Deflate(Z_FULL_FLUSH); // Return true if the flush succeeded or if it was a no-op. - return (zerror_ == Z_OK) || - (zerror_ == Z_BUF_ERROR && zcontext_.avail_in == 0 && - zcontext_.avail_out != 0); + return (zerror_ == Z_OK) || + (zerror_ == Z_BUF_ERROR && zcontext_.avail_in == 0 && + zcontext_.avail_out != 0); } bool GzipOutputStream::Close() { diff --git a/src/google/protobuf/io/gzip_stream.h b/src/google/protobuf/io/gzip_stream.h index ba1475c7bc..9980e5b4f6 100644 --- a/src/google/protobuf/io/gzip_stream.h +++ b/src/google/protobuf/io/gzip_stream.h @@ -48,12 +48,14 @@ #include #include +#include + namespace google { namespace protobuf { namespace io { // A ZeroCopyInputStream that reads compressed data through zlib -class LIBPROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream { +class PROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream { public: // Format key for constructor enum Format { @@ -68,19 +70,13 @@ class LIBPROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream { }; // buffer_size and format may be -1 for default of 64kB and GZIP format - explicit GzipInputStream( - ZeroCopyInputStream* sub_stream, - Format format = AUTO, - int buffer_size = -1); + explicit GzipInputStream(ZeroCopyInputStream* sub_stream, + Format format = AUTO, int buffer_size = -1); virtual ~GzipInputStream(); // Return last error message or NULL if no error. - inline const char* ZlibErrorMessage() const { - return zcontext_.msg; - } - inline int ZlibErrorCode() const { - return zerror_; - } + inline const char* ZlibErrorMessage() const { return zcontext_.msg; } + inline int ZlibErrorCode() const { return zerror_; } // implements ZeroCopyInputStream ---------------------------------- bool Next(const void** data, int* size); @@ -107,8 +103,7 @@ class LIBPROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipInputStream); }; - -class LIBPROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream { +class PROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream { public: // Format key for constructor enum Format { @@ -119,7 +114,7 @@ class LIBPROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream { ZLIB = 2, }; - struct LIBPROTOBUF_EXPORT Options { + struct PROTOBUF_EXPORT Options { // Defaults to GZIP. Format format; @@ -142,19 +137,13 @@ class LIBPROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream { explicit GzipOutputStream(ZeroCopyOutputStream* sub_stream); // Create a GzipOutputStream with the given options. - GzipOutputStream( - ZeroCopyOutputStream* sub_stream, - const Options& options); + GzipOutputStream(ZeroCopyOutputStream* sub_stream, const Options& options); virtual ~GzipOutputStream(); // Return last error message or NULL if no error. - inline const char* ZlibErrorMessage() const { - return zcontext_.msg; - } - inline int ZlibErrorCode() const { - return zerror_; - } + inline const char* ZlibErrorMessage() const { return zcontext_.msg; } + inline int ZlibErrorCode() const { return zerror_; } // Flushes data written so far to zipped data in the underlying stream. // It is the caller's responsibility to flush the underlying stream if @@ -207,4 +196,6 @@ class LIBPROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__ diff --git a/src/google/protobuf/stubs/io_win32.cc b/src/google/protobuf/io/io_win32.cc old mode 100644 new mode 100755 similarity index 98% rename from src/google/protobuf/stubs/io_win32.cc rename to src/google/protobuf/io/io_win32.cc index 4fa95fefb9..59f3ec5c9d --- a/src/google/protobuf/stubs/io_win32.cc +++ b/src/google/protobuf/io/io_win32.cc @@ -29,7 +29,9 @@ // 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. // @@ -40,7 +42,7 @@ // // This file is only used on Windows, it's empty on other platforms. -#if defined(_WIN32) +#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 @@ -63,7 +65,7 @@ #include -#include +#include #include #include @@ -72,7 +74,7 @@ namespace google { namespace protobuf { -namespace internal { +namespace io { namespace win32 { namespace { @@ -339,7 +341,7 @@ FILE* fopen(const char* path, const char* mode) { #endif } -int close(int fd) { return ::close(fd); } +int close(int fd) { return ::_close(fd); } int dup(int fd) { return ::_dup(fd); } @@ -498,7 +500,7 @@ bool wcs_to_utf8(const wchar_t* input, string* out) { } // namespace strings } // namespace win32 -} // namespace internal +} // namespace io } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/stubs/io_win32.h b/src/google/protobuf/io/io_win32.h old mode 100644 new mode 100755 similarity index 73% rename from src/google/protobuf/stubs/io_win32.h rename to src/google/protobuf/io/io_win32.h index f96746fb7c..ae6ed0a66f --- a/src/google/protobuf/stubs/io_win32.h +++ b/src/google/protobuf/io/io_win32.h @@ -29,48 +29,52 @@ // 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::internal::win32::{open/access/etc.}. +// ::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_STUBS_IO_WIN32_H__ -#define GOOGLE_PROTOBUF_STUBS_IO_WIN32_H__ +#ifndef GOOGLE_PROTOBUF_IO_IO_WIN32_H__ +#define GOOGLE_PROTOBUF_IO_IO_WIN32_H__ #if defined(_WIN32) #include #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 internal { +namespace io { namespace win32 { -LIBPROTOBUF_EXPORT FILE* fopen(const char* path, const char* mode); -LIBPROTOBUF_EXPORT int access(const char* path, int mode); -LIBPROTOBUF_EXPORT int chdir(const char* path); -LIBPROTOBUF_EXPORT int close(int fd); -LIBPROTOBUF_EXPORT int dup(int fd); -LIBPROTOBUF_EXPORT int dup2(int fd1, int fd2); -LIBPROTOBUF_EXPORT int mkdir(const char* path, int _mode); -LIBPROTOBUF_EXPORT int open(const char* path, int flags, int mode = 0); -LIBPROTOBUF_EXPORT int read(int fd, void* buffer, size_t size); -LIBPROTOBUF_EXPORT int setmode(int fd, int mode); -LIBPROTOBUF_EXPORT int stat(const char* path, struct _stat* buffer); -LIBPROTOBUF_EXPORT int write(int fd, const void* buffer, size_t size); -LIBPROTOBUF_EXPORT std::wstring testonly_utf8_to_winpath(const char* path); +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); struct ExpandWildcardsResult { enum { @@ -99,23 +103,22 @@ LIBPROTOBUF_EXPORT bool expand_wildcards( namespace strings { // Convert from UTF-16 to Active-Code-Page-encoded or to UTF-8-encoded text. -LIBPROTOBUF_EXPORT bool wcs_to_mbs( - const wchar_t* s, std::string* out, bool outUtf8); +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. -LIBPROTOBUF_EXPORT bool mbs_to_wcs( - const char* s, std::wstring* out, bool inUtf8); +PROTOBUF_EXPORT bool mbs_to_wcs(const char* s, std::wstring* out, bool inUtf8); // Convert from UTF-8-encoded text to UTF-16. -LIBPROTOBUF_EXPORT bool utf8_to_wcs(const char* input, std::wstring* out); +PROTOBUF_EXPORT bool utf8_to_wcs(const char* input, std::wstring* out); // Convert from UTF-16-encoded text to UTF-8. -LIBPROTOBUF_EXPORT bool wcs_to_utf8(const wchar_t* input, std::string* out); +PROTOBUF_EXPORT bool wcs_to_utf8(const wchar_t* input, std::string* out); } // namespace strings } // namespace win32 -} // namespace internal +} // namespace io } // namespace protobuf } // namespace google @@ -135,6 +138,8 @@ LIBPROTOBUF_EXPORT bool wcs_to_utf8(const wchar_t* input, std::string* out); #define STDOUT_FILENO 1 #endif +#include + #endif // defined(_WIN32) -#endif // GOOGLE_PROTOBUF_STUBS_IO_WIN32_H__ +#endif // GOOGLE_PROTOBUF_IO_IO_WIN32_H__ diff --git a/src/google/protobuf/stubs/io_win32_unittest.cc b/src/google/protobuf/io/io_win32_unittest.cc old mode 100644 new mode 100755 similarity index 99% rename from src/google/protobuf/stubs/io_win32_unittest.cc rename to src/google/protobuf/io/io_win32_unittest.cc index 39e36d9143..e1396d0851 --- a/src/google/protobuf/stubs/io_win32_unittest.cc +++ b/src/google/protobuf/io/io_win32_unittest.cc @@ -29,7 +29,9 @@ // 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. + // Unit tests for long-path-aware open/mkdir/access/etc. on Windows, as well as // for the supporting utility functions. // @@ -47,7 +49,7 @@ #include #include -#include +#include #include #include @@ -57,7 +59,7 @@ namespace google { namespace protobuf { -namespace internal { +namespace io { namespace win32 { namespace { @@ -631,7 +633,7 @@ TEST_F(IoWin32Test, Utf8Utf16ConversionTest) { } // namespace } // namespace win32 -} // namespace internal +} // namespace io } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/io/printer.cc b/src/google/protobuf/io/printer.cc index e988db80c9..6a86c58c1a 100644 --- a/src/google/protobuf/io/printer.cc +++ b/src/google/protobuf/io/printer.cc @@ -34,10 +34,10 @@ #include -#include -#include #include #include +#include +#include namespace google { namespace protobuf { @@ -73,7 +73,7 @@ Printer::~Printer() { bool Printer::GetSubstitutionRange(const char* varname, std::pair* range) { - std::map >::const_iterator iter = + std::map >::const_iterator iter = substitutions_.find(varname); if (iter == substitutions_.end()) { GOOGLE_LOG(DFATAL) << " Undefined variable in annotation: " << varname; @@ -89,7 +89,8 @@ bool Printer::GetSubstitutionRange(const char* varname, } void Printer::Annotate(const char* begin_varname, const char* end_varname, - const string& file_path, const std::vector& path) { + const std::string& file_path, + const std::vector& path) { if (annotation_collector_ == NULL) { // Can't generate signatures with this Printer. return; @@ -108,7 +109,7 @@ void Printer::Annotate(const char* begin_varname, const char* end_varname, } } -void Printer::Print(const std::map& variables, +void Printer::Print(const std::map& variables, const char* text) { int size = strlen(text); int pos = 0; // The number of bytes we've written so far. @@ -142,13 +143,14 @@ void Printer::Print(const std::map& variables, } int endpos = end - text; - string varname(text + pos, endpos - pos); + std::string varname(text + pos, endpos - pos); if (varname.empty()) { // Two delimiters in a row reduce to a literal delimiter character. WriteRaw(&variable_delimiter_, 1); } else { // Replace with the variable's value. - std::map::const_iterator iter = variables.find(varname); + std::map::const_iterator iter = + variables.find(varname); if (iter == variables.end()) { GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname; } else { @@ -156,7 +158,7 @@ void Printer::Print(const std::map& variables, line_start_variables_.push_back(varname); } WriteRaw(iter->second.data(), iter->second.size()); - std::pair >::iterator, + std::pair >::iterator, bool> inserted = substitutions_.insert(std::make_pair( varname, @@ -180,9 +182,7 @@ void Printer::Print(const std::map& variables, WriteRaw(text + pos, size - pos); } -void Printer::Indent() { - indent_ += " "; -} +void Printer::Indent() { indent_ += " "; } void Printer::Outdent() { if (indent_.empty()) { @@ -193,7 +193,7 @@ void Printer::Outdent() { indent_.resize(indent_.size() - 2); } -void Printer::PrintRaw(const string& data) { +void Printer::PrintRaw(const std::string& data) { WriteRaw(data.data(), data.size()); } @@ -213,7 +213,7 @@ void Printer::WriteRaw(const char* data, int size) { if (failed_) return; // Fix up empty variables (e.g., "{") that should be annotated as // coming after the indent. - for (std::vector::iterator i = line_start_variables_.begin(); + for (std::vector::iterator i = line_start_variables_.begin(); i != line_start_variables_.end(); ++i) { substitutions_[*i].first += indent_.size(); substitutions_[*i].second += indent_.size(); @@ -273,8 +273,8 @@ void Printer::IndentIfAtStart() { } } -void Printer::FormatInternal(const std::vector& args, - const std::map& vars, +void Printer::FormatInternal(const std::vector& args, + const std::map& vars, const char* format) { auto save = format; int arg_index = 0; @@ -304,9 +304,9 @@ void Printer::FormatInternal(const std::vector& args, } const char* Printer::WriteVariable( - const std::vector& args, const std::map& vars, - const char* format, int* arg_index, - std::vector* annotations) { + const std::vector& args, + const std::map& vars, const char* format, + int* arg_index, std::vector* annotations) { auto start = format; auto end = strchr(format, '$'); if (!end) { @@ -353,9 +353,9 @@ const char* Printer::WriteVariable( } auto end_var = end; while (start_var < end_var && *(end_var - 1) == ' ') end_var--; - string var_name{start_var, - static_cast(end_var - start_var)}; - string sub; + std::string var_name{ + start_var, static_cast(end_var - start_var)}; + std::string sub; if (std::isdigit(var_name[0])) { GOOGLE_CHECK_EQ(var_name.size(), 1); // No need for multi-digits int idx = var_name[0] - '1'; // Start counting at 1 diff --git a/src/google/protobuf/io/printer.h b/src/google/protobuf/io/printer.h index 236aed43d1..a55946e929 100644 --- a/src/google/protobuf/io/printer.h +++ b/src/google/protobuf/io/printer.h @@ -37,27 +37,29 @@ #ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__ #define GOOGLE_PROTOBUF_IO_PRINTER_H__ -#include #include +#include #include #include +#include + namespace google { namespace protobuf { namespace io { -class ZeroCopyOutputStream; // zero_copy_stream.h +class ZeroCopyOutputStream; // zero_copy_stream.h // Records annotations about a Printer's output. -class LIBPROTOBUF_EXPORT AnnotationCollector { +class PROTOBUF_EXPORT AnnotationCollector { public: // Annotation is a ofset range and a payload pair. - typedef std::pair, string> Annotation; + typedef std::pair, std::string> Annotation; // Records that the bytes in file_path beginning with begin_offset and ending // before end_offset are associated with the SourceCodeInfo-style path. virtual void AddAnnotation(size_t begin_offset, size_t end_offset, - const string& file_path, + const std::string& file_path, const std::vector& path) = 0; // TODO(gerbens) I don't see why we need virtuals here. Just a vector of @@ -80,7 +82,7 @@ class AnnotationProtoCollector : public AnnotationCollector { // Override for AnnotationCollector::AddAnnotation. virtual void AddAnnotation(size_t begin_offset, size_t end_offset, - const string& file_path, + const std::string& file_path, const std::vector& path) { typename AnnotationProto::Annotation* annotation = annotation_proto_->add_annotation(); @@ -176,7 +178,7 @@ class AnnotationProtoCollector : public AnnotationCollector { // This code associates the span covering "call(bar,bar)" in the output with the // call_ descriptor. -class LIBPROTOBUF_EXPORT Printer { +class PROTOBUF_EXPORT Printer { public: // Create a printer that writes text to the given output stream. Use the // given character as the delimiter for variables. @@ -217,7 +219,7 @@ class LIBPROTOBUF_EXPORT Printer { // Link a subsitution variable emitted by the last call to Print to the file // with path file_name. - void Annotate(const char* varname, const string& file_name) { + void Annotate(const char* varname, const std::string& file_name) { Annotate(varname, varname, file_name); } @@ -226,7 +228,7 @@ class LIBPROTOBUF_EXPORT Printer { // at begin_varname's value and ends after the last character of the value // substituted for end_varname. void Annotate(const char* begin_varname, const char* end_varname, - const string& file_name) { + const std::string& file_name) { if (annotation_collector_ == NULL) { // Annotations aren't turned on for this Printer. return; @@ -240,12 +242,13 @@ class LIBPROTOBUF_EXPORT Printer { // substituted are identified by their names surrounded by delimiter // characters (as given to the constructor). The variable bindings are // defined by the given map. - void Print(const std::map& variables, const char* text); + void Print(const std::map& variables, + const char* text); // Like the first Print(), except the substitutions are given as parameters. template void Print(const char* text, const Args&... args) { - std::map vars; + std::map vars; PrintInternal(&vars, text, args...); } @@ -260,7 +263,7 @@ class LIBPROTOBUF_EXPORT Printer { // Write a string to the output buffer. // This method does not look for newlines to add indentation. - void PrintRaw(const string& data); + void PrintRaw(const std::string& data); // Write a zero-delimited string to output buffer. // This method does not look for newlines to add indentation. @@ -275,8 +278,9 @@ class LIBPROTOBUF_EXPORT Printer { // formatting text using named variables (eq. "$foo$) from a lookup map (vars) // and variables directly supplied by arguments (eq "$1$" meaning first // argument which is the zero index element of args). - void FormatInternal(const std::vector& args, - const std::map& vars, const char* format); + void FormatInternal(const std::vector& args, + const std::map& vars, + const char* format); // True if any write to the underlying stream failed. (We don't just // crash in this case because this is an I/O failure, not a programming @@ -291,16 +295,17 @@ class LIBPROTOBUF_EXPORT Printer { // substituted for end_varname. Note that begin_varname and end_varname // may refer to the same variable. void Annotate(const char* begin_varname, const char* end_varname, - const string& file_path, const std::vector& path); + const std::string& file_path, const std::vector& path); // Base case - void PrintInternal(std::map* vars, const char* text) { + void PrintInternal(std::map* vars, + const char* text) { Print(*vars, text); } template - void PrintInternal(std::map* vars, const char* text, - const char* key, const string& value, + void PrintInternal(std::map* vars, const char* text, + const char* key, const std::string& value, const Args&... args) { (*vars)[key] = value; PrintInternal(vars, text, args...); @@ -323,8 +328,9 @@ class LIBPROTOBUF_EXPORT Printer { inline void IndentIfAtStart(); const char* WriteVariable( - const std::vector& args, const std::map& vars, - const char* format, int* arg_index, + const std::vector& args, + const std::map& vars, const char* format, + int* arg_index, std::vector* annotations); const char variable_delimiter_; @@ -337,7 +343,7 @@ class LIBPROTOBUF_EXPORT Printer { // used to calculate annotation ranges in the substitutions_ map below. size_t offset_; - string indent_; + std::string indent_; bool at_start_of_line_; bool failed_; @@ -348,12 +354,12 @@ class LIBPROTOBUF_EXPORT Printer { // start offset is the beginning of the substitution; the end offset is the // last byte of the substitution plus one (such that (end - start) is the // length of the substituted string). - std::map > substitutions_; + std::map > substitutions_; // Keeps track of the keys in substitutions_ that need to be updated when // indents are inserted. These are keys that refer to the beginning of the // current line. - std::vector line_start_variables_; + std::vector line_start_variables_; // Returns true and sets range to the substitution range in the output for // varname if varname was used once in the last call to Print. If varname @@ -373,4 +379,6 @@ class LIBPROTOBUF_EXPORT Printer { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ diff --git a/src/google/protobuf/io/printer_unittest.cc b/src/google/protobuf/io/printer_unittest.cc index fe46cee101..b847fa70dc 100644 --- a/src/google/protobuf/io/printer_unittest.cc +++ b/src/google/protobuf/io/printer_unittest.cc @@ -77,10 +77,11 @@ TEST(Printer, BasicPrinting) { buffer[output.ByteCount()] = '\0'; - EXPECT_STREQ("Hello World! This is the same line.\n" - "But this is a new one.\n" - "And this is another one.", - buffer); + EXPECT_STREQ( + "Hello World! This is the same line.\n" + "But this is a new one.\n" + "And this is another one.", + buffer); } } @@ -91,7 +92,7 @@ TEST(Printer, WriteRaw) { ArrayOutputStream output(buffer, sizeof(buffer), block_size); { - string string_obj = "From an object\n"; + std::string string_obj = "From an object\n"; Printer printer(&output, '$'); printer.WriteRaw("Hello World!", 12); printer.PrintRaw(" This is the same line.\n"); @@ -103,12 +104,13 @@ TEST(Printer, WriteRaw) { buffer[output.ByteCount()] = '\0'; - EXPECT_STREQ("Hello World! This is the same line.\n" - "But this is a new one.\n" - "And this is another one." - "\n" - "From an object\n", - buffer); + EXPECT_STREQ( + "Hello World! This is the same line.\n" + "But this is a new one.\n" + "And this is another one." + "\n" + "From an object\n", + buffer); } } @@ -120,7 +122,7 @@ TEST(Printer, VariableSubstitution) { { Printer printer(&output, '$'); - std::map vars; + std::map vars; vars["foo"] = "World"; vars["bar"] = "$foo$"; @@ -138,13 +140,14 @@ TEST(Printer, VariableSubstitution) { buffer[output.ByteCount()] = '\0'; - EXPECT_STREQ("Hello World!\n" - "bar = $foo$\n" - "RawBit\n" - "1234\n" - "A literal dollar sign: $\n" - "Now foo = blah.", - buffer); + EXPECT_STREQ( + "Hello World!\n" + "bar = $foo$\n" + "RawBit\n" + "1234\n" + "A literal dollar sign: $\n" + "Now foo = blah.", + buffer); } } @@ -163,30 +166,31 @@ TEST(Printer, InlineVariableSubstitution) { buffer[output.ByteCount()] = '\0'; - EXPECT_STREQ("Hello World!\n" - "RawBit\n" - "one two\n", - buffer); + EXPECT_STREQ( + "Hello World!\n" + "RawBit\n" + "one two\n", + buffer); } // MockDescriptorFile defines only those members that Printer uses to write out // annotations. class MockDescriptorFile { public: - explicit MockDescriptorFile(const string& file) : file_(file) {} + explicit MockDescriptorFile(const std::string& file) : file_(file) {} // The mock filename for this file. - const string& name() const { return file_; } + const std::string& name() const { return file_; } private: - string file_; + std::string file_; }; // MockDescriptor defines only those members that Printer uses to write out // annotations. class MockDescriptor { public: - MockDescriptor(const string& file, const std::vector& path) + MockDescriptor(const std::string& file, const std::vector& path) : file_(file), path_(path) {} // The mock file in which this descriptor was defined. @@ -210,7 +214,7 @@ TEST(Printer, AnnotateMap) { AnnotationProtoCollector info_collector(&info); { Printer printer(&output, '$', &info_collector); - std::map vars; + std::map vars; vars["foo"] = "3"; vars["bar"] = "5"; printer.Print(vars, "012$foo$4$bar$\n"); @@ -435,7 +439,6 @@ TEST(Printer, AnnotateIndentNewline) { EXPECT_EQ(4, ab->end()); } - TEST(Printer, Indenting) { char buffer[8192]; @@ -444,7 +447,7 @@ TEST(Printer, Indenting) { { Printer printer(&output, '$'); - std::map vars; + std::map vars; vars["newline"] = "\n"; @@ -454,11 +457,13 @@ TEST(Printer, Indenting) { printer.Outdent(); printer.Print("But this is not."); printer.Indent(); - printer.Print(" And this is still the same line.\n" - "But this is indented.\n"); + printer.Print( + " And this is still the same line.\n" + "But this is indented.\n"); printer.PrintRaw("RawBit has indent at start\n"); printer.PrintRaw("but not after a raw newline\n"); - printer.Print(vars, "Note that a newline in a variable will break " + printer.Print(vars, + "Note that a newline in a variable will break " "indenting, as we see$newline$here.\n"); printer.Indent(); printer.Print("And this"); @@ -472,18 +477,18 @@ TEST(Printer, Indenting) { buffer[output.ByteCount()] = '\0'; EXPECT_STREQ( - "This is not indented.\n" - " This is indented\n" - " And so is this\n" - "But this is not. And this is still the same line.\n" - " But this is indented.\n" - " RawBit has indent at start\n" - "but not after a raw newline\n" - "Note that a newline in a variable will break indenting, as we see\n" - "here.\n" - " And this is double-indented\n" - "Back to normal.", - buffer); + "This is not indented.\n" + " This is indented\n" + " And so is this\n" + "But this is not. And this is still the same line.\n" + " But this is indented.\n" + " RawBit has indent at start\n" + "but not after a raw newline\n" + "Note that a newline in a variable will break indenting, as we see\n" + "here.\n" + " And this is double-indented\n" + "Back to normal.", + buffer); } } @@ -594,12 +599,13 @@ TEST(Printer, WriteFailureExact) { } TEST(Printer, FormatInternal) { - std::vector args{"arg1", "arg2"}; - std::map vars{{"foo", "bar"}, {"baz", "bla"}, {"empty", ""}}; + std::vector args{"arg1", "arg2"}; + std::map vars{ + {"foo", "bar"}, {"baz", "bla"}, {"empty", ""}}; // Substitution tests { // Direct arg substitution - string s; + std::string s; { StringOutputStream output(&s); Printer printer(&output, '$'); @@ -609,7 +615,7 @@ TEST(Printer, FormatInternal) { } { // Variable substitution including spaces left - string s; + std::string s; { StringOutputStream output(&s); Printer printer(&output, '$'); @@ -619,7 +625,7 @@ TEST(Printer, FormatInternal) { } { // Variable substitution including spaces right - string s; + std::string s; { StringOutputStream output(&s); Printer printer(&output, '$'); @@ -629,7 +635,7 @@ TEST(Printer, FormatInternal) { } { // Mixed variable substitution - string s; + std::string s; { StringOutputStream output(&s); Printer printer(&output, '$'); @@ -641,7 +647,7 @@ TEST(Printer, FormatInternal) { // Indentation tests { // Empty lines shouldn't indent. - string s; + std::string s; { StringOutputStream output(&s); Printer printer(&output, '$'); @@ -653,7 +659,7 @@ TEST(Printer, FormatInternal) { } { // Annotations should respect indentation. - string s; + std::string s; GeneratedCodeInfo info; { StringOutputStream output(&s); @@ -663,7 +669,8 @@ TEST(Printer, FormatInternal) { GeneratedCodeInfo::Annotation annotation; annotation.set_source_file("file.proto"); annotation.add_path(33); - std::vector args{annotation.SerializeAsString(), "arg1", "arg2"}; + std::vector args{annotation.SerializeAsString(), "arg1", + "arg2"}; printer.FormatInternal(args, vars, "$empty $\n\n${1$$2$$}$ $3$\n$baz$"); printer.Outdent(); } @@ -680,42 +687,42 @@ TEST(Printer, FormatInternal) { // Death tests in case of illegal format strings. { // Unused arguments - string s; + std::string s; StringOutputStream output(&s); Printer printer(&output, '$'); EXPECT_DEATH(printer.FormatInternal(args, vars, "$empty $$1$"), "Unused"); } { // Wrong order arguments - string s; + std::string s; StringOutputStream output(&s); Printer printer(&output, '$'); EXPECT_DEATH(printer.FormatInternal(args, vars, "$2$ $1$"), "order"); } { // Zero is illegal argument - string s; + std::string s; StringOutputStream output(&s); Printer printer(&output, '$'); EXPECT_DEATH(printer.FormatInternal(args, vars, "$0$"), "failed"); } { // Argument out of bounds - string s; + std::string s; StringOutputStream output(&s); Printer printer(&output, '$'); EXPECT_DEATH(printer.FormatInternal(args, vars, "$1$ $2$ $3$"), "bounds"); } { // Unknown variable - string s; + std::string s; StringOutputStream output(&s); Printer printer(&output, '$'); EXPECT_DEATH(printer.FormatInternal(args, vars, "$huh$ $1$$2$"), "Unknown"); } { // Illegal variable - string s; + std::string s; StringOutputStream output(&s); Printer printer(&output, '$'); EXPECT_DEATH(printer.FormatInternal({}, vars, "$ $"), "Empty"); diff --git a/src/google/protobuf/io/strtod.cc b/src/google/protobuf/io/strtod.cc index a90bb9a31e..01164441a6 100644 --- a/src/google/protobuf/io/strtod.cc +++ b/src/google/protobuf/io/strtod.cc @@ -49,10 +49,14 @@ namespace io { namespace { +// This approximately 0x1.ffffffp127, but we don't use 0x1.ffffffp127 because +// it won't compile in MSVC. +const double MAX_FLOAT_AS_DOUBLE_ROUNDED = 3.4028235677973366e+38; + // 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. -string LocalizeRadix(const char* input, const char* radix_pos) { +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 @@ -61,11 +65,11 @@ string LocalizeRadix(const char* input, const char* radix_pos) { char temp[16]; int size = sprintf(temp, "%.1f", 1.5); GOOGLE_CHECK_EQ(temp[0], '1'); - GOOGLE_CHECK_EQ(temp[size-1], '5'); + GOOGLE_CHECK_EQ(temp[size - 1], '5'); GOOGLE_CHECK_LE(size, 6); // Now replace the '.' in the input with it. - string result; + std::string result; result.reserve(strlen(input) + size - 3); result.append(input, radix_pos); result.append(temp + 1, size - 2); @@ -90,12 +94,11 @@ double NoLocaleStrtod(const char* text, char** original_endptr) { // 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. - string localized = LocalizeRadix(text, temp_endptr); + std::string localized = LocalizeRadix(text, 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 - text)) { + if ((localized_endptr - localized_cstr) > (temp_endptr - text)) { // This attempt got further, so replacing the decimal must have helped. // Update original_endptr to point at the right location. if (original_endptr != NULL) { @@ -103,7 +106,7 @@ double NoLocaleStrtod(const char* text, char** original_endptr) { int size_diff = localized.size() - strlen(text); // const_cast is necessary to match the strtod() interface. *original_endptr = const_cast( - text + (localized_endptr - localized_cstr - size_diff)); + text + (localized_endptr - localized_cstr - size_diff)); } } @@ -111,9 +114,24 @@ double NoLocaleStrtod(const char* text, char** original_endptr) { } float SafeDoubleToFloat(double value) { + // static_cast on a number larger than float can result in illegal + // instruction error, so we need to manually convert it to infinity or max. if (value > std::numeric_limits::max()) { + // Max float value is about 3.4028234664E38 when represented as a double. + // However, when printing float as text, it will be rounded as + // 3.4028235e+38. If we parse the value of 3.4028235e+38 from text and + // compare it to 3.4028234664E38, we may think that it is larger, but + // actually, any number between these two numbers could only be represented + // as the same max float number in float, so we should treat them the same + // as max float. + if (value <= MAX_FLOAT_AS_DOUBLE_ROUNDED) { + return std::numeric_limits::max(); + } return std::numeric_limits::infinity(); } else if (value < -std::numeric_limits::max()) { + if (value >= -MAX_FLOAT_AS_DOUBLE_ROUNDED) { + return -std::numeric_limits::max(); + } return -std::numeric_limits::infinity(); } else { return static_cast(value); diff --git a/src/google/protobuf/io/tokenizer.cc b/src/google/protobuf/io/tokenizer.cc index 916d1606b7..f8cf2ebd1a 100644 --- a/src/google/protobuf/io/tokenizer.cc +++ b/src/google/protobuf/io/tokenizer.cc @@ -113,39 +113,34 @@ namespace { // Note: No class is allowed to contain '\0', since this is used to mark end- // of-input and is handled specially. -#define CHARACTER_CLASS(NAME, EXPRESSION) \ - class NAME { \ - public: \ - static inline bool InClass(char c) { \ - return EXPRESSION; \ - } \ +#define CHARACTER_CLASS(NAME, EXPRESSION) \ + class NAME { \ + public: \ + static inline bool InClass(char c) { return EXPRESSION; } \ } -CHARACTER_CLASS(Whitespace, c == ' ' || c == '\n' || c == '\t' || - c == '\r' || c == '\v' || c == '\f'); -CHARACTER_CLASS(WhitespaceNoNewline, c == ' ' || c == '\t' || - c == '\r' || c == '\v' || c == '\f'); +CHARACTER_CLASS(Whitespace, c == ' ' || c == '\n' || c == '\t' || c == '\r' || + c == '\v' || c == '\f'); +CHARACTER_CLASS(WhitespaceNoNewline, + c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f'); -CHARACTER_CLASS(Unprintable, c < ' ' && c > '\0'); +CHARACTER_CLASS(Unprintable, c<' ' && c> '\0'); CHARACTER_CLASS(Digit, '0' <= c && c <= '9'); CHARACTER_CLASS(OctalDigit, '0' <= c && c <= '7'); -CHARACTER_CLASS(HexDigit, ('0' <= c && c <= '9') || - ('a' <= c && c <= 'f') || - ('A' <= c && c <= 'F')); +CHARACTER_CLASS(HexDigit, ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || + ('A' <= c && c <= 'F')); -CHARACTER_CLASS(Letter, ('a' <= c && c <= 'z') || - ('A' <= c && c <= 'Z') || - (c == '_')); +CHARACTER_CLASS(Letter, + ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == '_')); CHARACTER_CLASS(Alphanumeric, ('a' <= c && c <= 'z') || - ('A' <= c && c <= 'Z') || - ('0' <= c && c <= '9') || - (c == '_')); + ('A' <= c && c <= 'Z') || + ('0' <= c && c <= '9') || (c == '_')); CHARACTER_CLASS(Escape, c == 'a' || c == 'b' || c == 'f' || c == 'n' || - c == 'r' || c == 't' || c == 'v' || c == '\\' || - c == '?' || c == '\'' || c == '\"'); + c == 'r' || c == 't' || c == 'v' || c == '\\' || + c == '?' || c == '\'' || c == '\"'); #undef CHARACTER_CLASS @@ -161,20 +156,32 @@ inline int DigitValue(char digit) { // Inline because it's only used in one place. inline char TranslateEscape(char c) { switch (c) { - 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 '\?'; // Trigraphs = :( - case '\'': return '\''; - case '"': return '\"'; + 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 '\?'; // Trigraphs = :( + case '\'': + return '\''; + case '"': + return '\"'; // We expect escape sequences to have been validated separately. - default: return '?'; + default: + return '?'; } } @@ -186,21 +193,20 @@ ErrorCollector::~ErrorCollector() {} Tokenizer::Tokenizer(ZeroCopyInputStream* input, ErrorCollector* error_collector) - : input_(input), - error_collector_(error_collector), - buffer_(NULL), - buffer_size_(0), - buffer_pos_(0), - read_error_(false), - line_(0), - column_(0), - record_target_(NULL), - record_start_(-1), - allow_f_after_float_(false), - comment_style_(CPP_COMMENT_STYLE), - require_space_after_number_(true), - allow_multiline_strings_(false) { - + : input_(input), + error_collector_(error_collector), + buffer_(NULL), + buffer_size_(0), + buffer_pos_(0), + read_error_(false), + line_(0), + column_(0), + record_target_(NULL), + record_start_(-1), + allow_f_after_float_(false), + comment_style_(CPP_COMMENT_STYLE), + require_space_after_number_(true), + allow_multiline_strings_(false) { current_.line = 0; current_.column = 0; current_.end_column = 0; @@ -249,7 +255,8 @@ void Tokenizer::Refresh() { // If we're in a token, append the rest of the buffer to it. if (record_target_ != NULL && record_start_ < buffer_size_) { - record_target_->append(buffer_ + record_start_, buffer_size_ - record_start_); + record_target_->append(buffer_ + record_start_, + buffer_size_ - record_start_); record_start_ = 0; } @@ -271,7 +278,7 @@ void Tokenizer::Refresh() { current_char_ = buffer_[0]; } -inline void Tokenizer::RecordTo(string* target) { +inline void Tokenizer::RecordTo(std::string* target) { record_target_ = target; record_start_ = buffer_pos_; } @@ -282,14 +289,15 @@ inline void Tokenizer::StopRecording() { // be helpful by detecting the NULL pointer, even though there's nothing // wrong with reading zero bytes from NULL. if (buffer_pos_ != record_start_) { - record_target_->append(buffer_ + record_start_, buffer_pos_ - record_start_); + record_target_->append(buffer_ + record_start_, + buffer_pos_ - record_start_); } record_target_ = NULL; record_start_ = -1; } inline void Tokenizer::StartToken() { - current_.type = TYPE_START; // Just for the sake of initializing it. + current_.type = TYPE_START; // Just for the sake of initializing it. current_.text.clear(); current_.line = line_; current_.column = column_; @@ -304,12 +312,12 @@ inline void Tokenizer::EndToken() { // ------------------------------------------------------------------- // Helper methods that consume characters. -template +template inline bool Tokenizer::LookingAt() { return CharacterClass::InClass(current_char_); } -template +template inline bool Tokenizer::TryConsumeOne() { if (CharacterClass::InClass(current_char_)) { NextChar(); @@ -328,14 +336,14 @@ inline bool Tokenizer::TryConsume(char c) { } } -template +template inline void Tokenizer::ConsumeZeroOrMore() { while (CharacterClass::InClass(current_char_)) { NextChar(); } } -template +template inline void Tokenizer::ConsumeOneOrMore(const char* error) { if (!CharacterClass::InClass(current_char_)) { AddError(error); @@ -381,25 +389,21 @@ void Tokenizer::ConsumeString(char delimiter) { } // Possibly followed by another hex digit, but again we don't care. } else if (TryConsume('u')) { - if (!TryConsumeOne() || - !TryConsumeOne() || - !TryConsumeOne() || - !TryConsumeOne()) { + if (!TryConsumeOne() || !TryConsumeOne() || + !TryConsumeOne() || !TryConsumeOne()) { AddError("Expected four hex digits for \\u escape sequence."); } } else if (TryConsume('U')) { // We expect 8 hex digits; but only the range up to 0x10ffff is // legal. - if (!TryConsume('0') || - !TryConsume('0') || + if (!TryConsume('0') || !TryConsume('0') || !(TryConsume('0') || TryConsume('1')) || - !TryConsumeOne() || - !TryConsumeOne() || - !TryConsumeOne() || - !TryConsumeOne() || + !TryConsumeOne() || !TryConsumeOne() || + !TryConsumeOne() || !TryConsumeOne() || !TryConsumeOne()) { - AddError("Expected eight hex digits up to 10ffff for \\U escape " - "sequence"); + AddError( + "Expected eight hex digits up to 10ffff for \\U escape " + "sequence"); } } else { AddError("Invalid escape sequence in string literal."); @@ -465,7 +469,7 @@ Tokenizer::TokenType Tokenizer::ConsumeNumber(bool started_with_zero, } else if (current_char_ == '.') { if (is_float) { AddError( - "Already saw decimal point or exponent; can't have another one."); + "Already saw decimal point or exponent; can't have another one."); } else { AddError("Hex and octal numbers must be integers."); } @@ -474,7 +478,7 @@ Tokenizer::TokenType Tokenizer::ConsumeNumber(bool started_with_zero, return is_float ? TYPE_FLOAT : TYPE_INTEGER; } -void Tokenizer::ConsumeLineComment(string* content) { +void Tokenizer::ConsumeLineComment(std::string* content) { if (content != NULL) RecordTo(content); while (current_char_ != '\0' && current_char_ != '\n') { @@ -485,17 +489,15 @@ void Tokenizer::ConsumeLineComment(string* content) { if (content != NULL) StopRecording(); } -void Tokenizer::ConsumeBlockComment(string* content) { +void Tokenizer::ConsumeBlockComment(std::string* content) { int start_line = line_; int start_column = column_ - 2; if (content != NULL) RecordTo(content); while (true) { - while (current_char_ != '\0' && - current_char_ != '*' && - current_char_ != '/' && - current_char_ != '\n') { + while (current_char_ != '\0' && current_char_ != '*' && + current_char_ != '/' && current_char_ != '\n') { NextChar(); } @@ -524,11 +526,11 @@ void Tokenizer::ConsumeBlockComment(string* content) { // Note: We didn't consume the '*' because if there is a '/' after it // we want to interpret that as the end of the comment. AddError( - "\"/*\" inside block comment. Block comments cannot be nested."); + "\"/*\" inside block comment. Block comments cannot be nested."); } else if (current_char_ == '\0') { AddError("End-of-file inside block comment."); - error_collector_->AddError( - start_line, start_column, " Comment started here."); + error_collector_->AddError(start_line, start_column, + " Comment started here."); if (content != NULL) StopRecording(); break; } @@ -613,8 +615,9 @@ bool Tokenizer::Next() { current_.line == previous_.line && current_.column == previous_.end_column) { // We don't accept syntax like "blah.123". - error_collector_->AddError(line_, column_ - 2, - "Need space between identifier and decimal point."); + error_collector_->AddError( + line_, column_ - 2, + "Need space between identifier and decimal point."); } current_.type = ConsumeNumber(false, true); } else { @@ -631,7 +634,8 @@ bool Tokenizer::Next() { } else { // Check if the high order bit is set. if (current_char_ & 0x80) { - error_collector_->AddError(line_, column_, + error_collector_->AddError( + line_, column_, StringPrintf("Interpreting non ascii codepoint %d.", static_cast(current_char_))); } @@ -664,9 +668,9 @@ namespace { // next_leading_comments. class CommentCollector { public: - CommentCollector(string* prev_trailing_comments, - std::vector* detached_comments, - string* next_leading_comments) + CommentCollector(std::string* prev_trailing_comments, + std::vector* detached_comments, + std::string* next_leading_comments) : prev_trailing_comments_(prev_trailing_comments), detached_comments_(detached_comments), next_leading_comments_(next_leading_comments), @@ -687,7 +691,7 @@ class CommentCollector { // About to read a line comment. Get the comment buffer pointer in order to // read into it. - string* GetBufferForLineComment() { + std::string* GetBufferForLineComment() { // We want to combine with previous line comments, but not block comments. if (has_comment_ && !is_line_comment_) { Flush(); @@ -699,7 +703,7 @@ class CommentCollector { // About to read a block comment. Get the comment buffer pointer in order to // read into it. - string* GetBufferForBlockComment() { + std::string* GetBufferForBlockComment() { if (has_comment_) { Flush(); } @@ -731,16 +735,14 @@ class CommentCollector { } } - void DetachFromPrev() { - can_attach_to_prev_ = false; - } + void DetachFromPrev() { can_attach_to_prev_ = false; } private: - string* prev_trailing_comments_; - std::vector* detached_comments_; - string* next_leading_comments_; + std::string* prev_trailing_comments_; + std::vector* detached_comments_; + std::string* next_leading_comments_; - string comment_buffer_; + std::string comment_buffer_; // True if any comments were read into comment_buffer_. This can be true even // if comment_buffer_ is empty, namely if the comment was "/**/". @@ -754,11 +756,11 @@ class CommentCollector { bool can_attach_to_prev_; }; -} // namespace +} // namespace -bool Tokenizer::NextWithComments(string* prev_trailing_comments, - std::vector* detached_comments, - string* next_leading_comments) { +bool Tokenizer::NextWithComments(std::string* prev_trailing_comments, + std::vector* detached_comments, + std::string* next_leading_comments) { CommentCollector collector(prev_trailing_comments, detached_comments, next_leading_comments); @@ -767,8 +769,9 @@ bool Tokenizer::NextWithComments(string* prev_trailing_comments, // beginning. Only UTF-8 BOM (0xEF 0xBB 0xBF) is accepted. if (TryConsume((char)0xEF)) { if (!TryConsume((char)0xBB) || !TryConsume((char)0xBF)) { - AddError("Proto file starts with 0xEF but not UTF-8 BOM. " - "Only UTF-8 is accepted for proto file."); + AddError( + "Proto file starts with 0xEF but not UTF-8 BOM. " + "Only UTF-8 is accepted for proto file."); return false; } } @@ -836,9 +839,7 @@ bool Tokenizer::NextWithComments(string* prev_trailing_comments, collector.DetachFromPrev(); } else { bool result = Next(); - if (!result || - current_.text == "}" || - current_.text == "]" || + if (!result || current_.text == "}" || current_.text == "]" || current_.text == ")") { // It looks like we're at the end of a scope. In this case it // makes no sense to attach a comment to the following token. @@ -858,12 +859,12 @@ bool Tokenizer::NextWithComments(string* prev_trailing_comments, // are given is text that the tokenizer actually parsed as a token // of the given type. -bool Tokenizer::ParseInteger(const string& text, uint64 max_value, +bool Tokenizer::ParseInteger(const std::string& text, uint64 max_value, uint64* output) { // Sadly, we can't just use strtoul() since it is only 32-bit and strtoull() // is non-standard. I hate the C standard library. :( -// return strtoull(text.c_str(), NULL, 0); + // return strtoull(text.c_str(), NULL, 0); const char* ptr = text.c_str(); int base = 10; @@ -897,7 +898,7 @@ bool Tokenizer::ParseInteger(const string& text, uint64 max_value, return true; } -double Tokenizer::ParseFloat(const string& text) { +double Tokenizer::ParseFloat(const std::string& text) { const char* start = text.c_str(); char* end; double result = NoLocaleStrtod(start, &end); @@ -917,36 +918,31 @@ double Tokenizer::ParseFloat(const string& text) { } GOOGLE_LOG_IF(DFATAL, end - start != text.size() || *start == '-') - << " Tokenizer::ParseFloat() passed text that could not have been" - " tokenized as a float: " << CEscape(text); + << " Tokenizer::ParseFloat() passed text that could not have been" + " tokenized as a float: " + << CEscape(text); return result; } // Helper to append a Unicode code point to a string as UTF8, without bringing // in any external dependencies. -static void AppendUTF8(uint32 code_point, string* output) { +static void AppendUTF8(uint32 code_point, std::string* 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); + 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); + tmp = 0x00e08080 | ((code_point & 0xf000) << 4) | + ((code_point & 0x0fc0) << 2) | (code_point & 0x003f); len = 3; } else if (code_point <= 0x1fffff) { - tmp = 0xf0808080 | - ((code_point & 0x1c0000) << 6) | - ((code_point & 0x03f000) << 4) | - ((code_point & 0x000fc0) << 2) | - (code_point & 0x003f); + tmp = 0xf0808080 | ((code_point & 0x1c0000) << 6) | + ((code_point & 0x03f000) << 4) | ((code_point & 0x000fc0) << 2) | + (code_point & 0x003f); len = 4; } else { // UTF-16 is only defined for code points up to 0x10FFFF, and UTF-8 is @@ -986,7 +982,7 @@ static inline bool IsHeadSurrogate(uint32 code_point) { static inline bool IsTrailSurrogate(uint32 code_point) { return (code_point >= kMinTrailSurrogate) && - (code_point < kMaxTrailSurrogate); + (code_point < kMaxTrailSurrogate); } // Combine a head and trail surrogate into a single Unicode code point. @@ -994,7 +990,7 @@ static uint32 AssembleUTF16(uint32 head_surrogate, uint32 trail_surrogate) { GOOGLE_DCHECK(IsHeadSurrogate(head_surrogate)); GOOGLE_DCHECK(IsTrailSurrogate(trail_surrogate)); return 0x10000 + (((head_surrogate - kMinHeadSurrogate) << 10) | - (trail_surrogate - kMinTrailSurrogate)); + (trail_surrogate - kMinTrailSurrogate)); } // Convert the escape sequence parameter to a number of expected hex digits. @@ -1012,8 +1008,7 @@ static const char* FetchUnicodePoint(const char* ptr, uint32* code_point) { const char* p = ptr; // Fetch the code point. const int len = UnicodeLength(*p++); - if (!ReadHexDigits(p, len, code_point)) - return ptr; + if (!ReadHexDigits(p, len, code_point)) return ptr; p += len; // Check if the code point we read is a "head surrogate." If so, then we @@ -1036,14 +1031,15 @@ static const char* FetchUnicodePoint(const char* ptr, uint32* code_point) { // The text string must begin and end with single or double quote // characters. -void Tokenizer::ParseStringAppend(const string& text, string* output) { +void Tokenizer::ParseStringAppend(const std::string& text, + std::string* output) { // Reminder: text[0] is always a quote character. (If text is // empty, it's invalid, so we'll just return). const size_t text_size = text.size(); if (text_size == 0) { - GOOGLE_LOG(DFATAL) - << " Tokenizer::ParseStringAppend() passed text that could not" - " have been tokenized as a string: " << CEscape(text); + GOOGLE_LOG(DFATAL) << " Tokenizer::ParseStringAppend() passed text that could not" + " have been tokenized as a string: " + << CEscape(text); return; } @@ -1114,23 +1110,19 @@ void Tokenizer::ParseStringAppend(const string& text, string* output) { } } -template -static bool AllInClass(const string& s) { +template +static bool AllInClass(const std::string& s) { for (int i = 0; i < s.size(); ++i) { - if (!CharacterClass::InClass(s[i])) - return false; + if (!CharacterClass::InClass(s[i])) return false; } return true; } -bool Tokenizer::IsIdentifier(const string& text) { +bool Tokenizer::IsIdentifier(const std::string& text) { // Mirrors IDENTIFIER definition in Tokenizer::Next() above. - if (text.size() == 0) - return false; - if (!Letter::InClass(text.at(0))) - return false; - if (!AllInClass(text.substr(1))) - return false; + if (text.size() == 0) return false; + if (!Letter::InClass(text.at(0))) return false; + if (!AllInClass(text.substr(1))) return false; return true; } diff --git a/src/google/protobuf/io/tokenizer.h b/src/google/protobuf/io/tokenizer.h index 59b6772933..cb33b931c9 100644 --- a/src/google/protobuf/io/tokenizer.h +++ b/src/google/protobuf/io/tokenizer.h @@ -42,11 +42,13 @@ #include #include +#include + namespace google { namespace protobuf { namespace io { -class ZeroCopyInputStream; // zero_copy_stream.h +class ZeroCopyInputStream; // zero_copy_stream.h // Defined in this file. class ErrorCollector; @@ -61,7 +63,7 @@ typedef int ColumnNumber; // Abstract interface for an object which collects the errors that occur // during parsing. A typical implementation might simply print the errors // to stdout. -class LIBPROTOBUF_EXPORT ErrorCollector { +class PROTOBUF_EXPORT ErrorCollector { public: inline ErrorCollector() {} virtual ~ErrorCollector(); @@ -70,13 +72,13 @@ class LIBPROTOBUF_EXPORT ErrorCollector { // column numbers. The numbers are zero-based, so you may want to add // 1 to each before printing them. virtual void AddError(int line, ColumnNumber column, - const string& message) = 0; + const std::string& message) = 0; // Indicates that there was a warning in the input at the given line and // column numbers. The numbers are zero-based, so you may want to add // 1 to each before printing them. virtual void AddWarning(int line, ColumnNumber column, - const string& message) { } + const std::string& message) {} private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector); @@ -88,7 +90,7 @@ class LIBPROTOBUF_EXPORT ErrorCollector { // precise descriptions. Whitespace and comments are skipped. By default, // C- and C++-style comments are recognized, but other styles can be used by // calling set_comment_style(). -class LIBPROTOBUF_EXPORT Tokenizer { +class PROTOBUF_EXPORT Tokenizer { public: // Construct a Tokenizer that reads and tokenizes text from the given // input stream and writes errors to the given error_collector. @@ -97,8 +99,8 @@ class LIBPROTOBUF_EXPORT Tokenizer { ~Tokenizer(); enum TokenType { - TYPE_START, // Next() has not yet been called. - TYPE_END, // End of input reached. "text" is empty. + TYPE_START, // Next() has not yet been called. + TYPE_END, // End of input reached. "text" is empty. TYPE_IDENTIFIER, // A sequence of letters, digits, and underscores, not // starting with a digit. It is an error for a number @@ -124,7 +126,7 @@ class LIBPROTOBUF_EXPORT Tokenizer { // Structure representing a token read from the token stream. struct Token { TokenType type; - string text; // The exact text of the token as it appeared in + std::string text; // The exact text of the token as it appeared in // the input. e.g. tokens of TYPE_STRING will still // be escaped and in quotes. @@ -190,31 +192,31 @@ class LIBPROTOBUF_EXPORT Tokenizer { // /* Block comment attached to // * grault. */ // optional int32 grault = 6; - bool NextWithComments(string* prev_trailing_comments, - std::vector* detached_comments, - string* next_leading_comments); + bool NextWithComments(std::string* prev_trailing_comments, + std::vector* detached_comments, + std::string* next_leading_comments); // Parse helpers --------------------------------------------------- // Parses a TYPE_FLOAT token. This never fails, so long as the text actually // comes from a TYPE_FLOAT token parsed by Tokenizer. If it doesn't, the // result is undefined (possibly an assert failure). - static double ParseFloat(const string& text); + static double ParseFloat(const std::string& text); // Parses a TYPE_STRING token. This never fails, so long as the text actually // comes from a TYPE_STRING token parsed by Tokenizer. If it doesn't, the // result is undefined (possibly an assert failure). - static void ParseString(const string& text, string* output); + static void ParseString(const std::string& text, std::string* output); // Identical to ParseString, but appends to output. - static void ParseStringAppend(const string& text, string* output); + static void ParseStringAppend(const std::string& text, std::string* output); // Parses a TYPE_INTEGER token. Returns false if the result would be // greater than max_value. Otherwise, returns true and sets *output to the // result. If the text is not from a Token of type TYPE_INTEGER originally // parsed by a Tokenizer, the result is undefined (possibly an assert // failure). - static bool ParseInteger(const string& text, uint64 max_value, + static bool ParseInteger(const std::string& text, uint64 max_value, uint64* output); // Options --------------------------------------------------------- @@ -250,23 +252,23 @@ class LIBPROTOBUF_EXPORT Tokenizer { } // External helper: validate an identifier. - static bool IsIdentifier(const string& text); + static bool IsIdentifier(const std::string& text); // ----------------------------------------------------------------- private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Tokenizer); - Token current_; // Returned by current(). - Token previous_; // Returned by previous(). + Token current_; // Returned by current(). + Token previous_; // Returned by previous(). ZeroCopyInputStream* input_; ErrorCollector* error_collector_; - char current_char_; // == buffer_[buffer_pos_], updated by NextChar(). - const char* buffer_; // Current buffer returned from input_. - int buffer_size_; // Size of buffer_. - int buffer_pos_; // Current position within the buffer. - bool read_error_; // Did we previously encounter a read error? + char current_char_; // == buffer_[buffer_pos_], updated by NextChar(). + const char* buffer_; // Current buffer returned from input_. + int buffer_size_; // Size of buffer_. + int buffer_pos_; // Current position within the buffer. + bool read_error_; // Did we previously encounter a read error? // Line and column number of current_char_ within the whole input stream. int line_; @@ -276,7 +278,7 @@ class LIBPROTOBUF_EXPORT Tokenizer { // Call RecordTo(&str) to start recording and StopRecording() to stop. // E.g. StartToken() calls RecordTo(¤t_.text). record_start_ is the // position within the current buffer where recording started. - string* record_target_; + std::string* record_target_; int record_start_; // Options. @@ -299,7 +301,7 @@ class LIBPROTOBUF_EXPORT Tokenizer { // Read a new buffer from the input. void Refresh(); - inline void RecordTo(string* target); + inline void RecordTo(std::string* target); inline void StopRecording(); // Called when the current character is the first character of a new @@ -311,7 +313,7 @@ class LIBPROTOBUF_EXPORT Tokenizer { inline void EndToken(); // Convenience method to add an error at the current line and column. - void AddError(const string& message) { + void AddError(const std::string& message) { error_collector_->AddError(line_, column_, message); } @@ -334,9 +336,9 @@ class LIBPROTOBUF_EXPORT Tokenizer { TokenType ConsumeNumber(bool started_with_zero, bool started_with_dot); // Consume the rest of a line. - void ConsumeLineComment(string* content); + void ConsumeLineComment(std::string* content); // Consume until "*/". - void ConsumeBlockComment(string* content); + void ConsumeBlockComment(std::string* content); enum NextCommentStatus { // Started a line comment. @@ -367,39 +369,36 @@ class LIBPROTOBUF_EXPORT Tokenizer { // Returns true if the current character is of the given character // class, but does not consume anything. - template + template inline bool LookingAt(); // If the current character is in the given class, consume it and return // true. Otherwise return false. // e.g. TryConsumeOne() - template + template inline bool TryConsumeOne(); // Like above, but try to consume the specific character indicated. inline bool TryConsume(char c); // Consume zero or more of the given character class. - template + template inline void ConsumeZeroOrMore(); // Consume one or more of the given character class or log the given // error message. // e.g. ConsumeOneOrMore("Expected digits."); - template + template inline void ConsumeOneOrMore(const char* error); }; // inline methods ==================================================== -inline const Tokenizer::Token& Tokenizer::current() { - return current_; -} +inline const Tokenizer::Token& Tokenizer::current() { return current_; } -inline const Tokenizer::Token& Tokenizer::previous() { - return previous_; -} +inline const Tokenizer::Token& Tokenizer::previous() { return previous_; } -inline void Tokenizer::ParseString(const string& text, string* output) { +inline void Tokenizer::ParseString(const std::string& text, + std::string* output) { output->clear(); ParseStringAppend(text, output); } @@ -408,4 +407,6 @@ inline void Tokenizer::ParseString(const string& text, string* output) { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_IO_TOKENIZER_H__ diff --git a/src/google/protobuf/io/tokenizer_unittest.cc b/src/google/protobuf/io/tokenizer_unittest.cc index e55288e2ff..37628ab2f9 100644 --- a/src/google/protobuf/io/tokenizer_unittest.cc +++ b/src/google/protobuf/io/tokenizer_unittest.cc @@ -75,45 +75,45 @@ namespace { // which failed will be printed. The case type must be printable using // ostream::operator<<. -#define TEST_1D(FIXTURE, NAME, CASES) \ - class FIXTURE##_##NAME##_DD : public FIXTURE { \ - protected: \ - template \ - void DoSingleCase(const CaseType& CASES##_case); \ - }; \ - \ - TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ - for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \ - SCOPED_TRACE(testing::Message() \ - << #CASES " case #" << i << ": " << CASES[i]); \ - DoSingleCase(CASES[i]); \ - } \ - } \ - \ - template \ +#define TEST_1D(FIXTURE, NAME, CASES) \ + class FIXTURE##_##NAME##_DD : public FIXTURE { \ + protected: \ + template \ + void DoSingleCase(const CaseType& CASES##_case); \ + }; \ + \ + TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ + for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \ + SCOPED_TRACE(testing::Message() \ + << #CASES " case #" << i << ": " << CASES[i]); \ + DoSingleCase(CASES[i]); \ + } \ + } \ + \ + template \ void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType& CASES##_case) -#define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \ - class FIXTURE##_##NAME##_DD : public FIXTURE { \ - protected: \ - template \ - void DoSingleCase(const CaseType1& CASES1##_case, \ - const CaseType2& CASES2##_case); \ - }; \ - \ - TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ - for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \ - for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \ - SCOPED_TRACE(testing::Message() \ - << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \ - << #CASES2 " case #" << j << ": " << CASES2[j]); \ - DoSingleCase(CASES1[i], CASES2[j]); \ - } \ - } \ - } \ - \ - template \ - void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \ +#define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \ + class FIXTURE##_##NAME##_DD : public FIXTURE { \ + protected: \ + template \ + void DoSingleCase(const CaseType1& CASES1##_case, \ + const CaseType2& CASES2##_case); \ + }; \ + \ + TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ + for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \ + for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \ + SCOPED_TRACE(testing::Message() \ + << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \ + << #CASES2 " case #" << j << ": " << CASES2[j]); \ + DoSingleCase(CASES1[i], CASES2[j]); \ + } \ + } \ + } \ + \ + template \ + void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \ const CaseType2& CASES2##_case) // ------------------------------------------------------------------- @@ -123,7 +123,7 @@ namespace { class TestInputStream : public ZeroCopyInputStream { public: TestInputStream(const void* data, int size, int block_size) - : array_stream_(data, size, block_size), counter_(0) {} + : array_stream_(data, size, block_size), counter_(0) {} ~TestInputStream() {} // implements ZeroCopyInputStream ---------------------------------- @@ -141,9 +141,9 @@ class TestInputStream : public ZeroCopyInputStream { } } - void BackUp(int count) { return array_stream_.BackUp(count); } - bool Skip(int count) { return array_stream_.Skip(count); } - int64 ByteCount() const { return array_stream_.ByteCount(); } + void BackUp(int count) { return array_stream_.BackUp(count); } + bool Skip(int count) { return array_stream_.Skip(count); } + int64 ByteCount() const { return array_stream_.ByteCount(); } private: ArrayInputStream array_stream_; @@ -159,12 +159,11 @@ class TestErrorCollector : public ErrorCollector { TestErrorCollector() {} ~TestErrorCollector() {} - string text_; + std::string text_; // implements ErrorCollector --------------------------------------- - void AddError(int line, int column, const string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", - line, column, message); + void AddError(int line, int column, const std::string& message) { + strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line, column, message); } }; @@ -179,7 +178,7 @@ const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024}; class TokenizerTest : public testing::Test { protected: // For easy testing. - uint64 ParseInteger(const string& text) { + uint64 ParseInteger(const std::string& text) { uint64 result; EXPECT_TRUE(Tokenizer::ParseInteger(text, kuint64max, &result)); return result; @@ -195,7 +194,7 @@ class TokenizerTest : public testing::Test { // In each test case, the entire input text should parse as a single token // of the given type. struct SimpleTokenCase { - string input; + std::string input; Tokenizer::TokenType type; }; @@ -205,52 +204,51 @@ inline std::ostream& operator<<(std::ostream& out, } SimpleTokenCase kSimpleTokenCases[] = { - // Test identifiers. - { "hello", Tokenizer::TYPE_IDENTIFIER }, - - // Test integers. - { "123", Tokenizer::TYPE_INTEGER }, - { "0xab6", Tokenizer::TYPE_INTEGER }, - { "0XAB6", Tokenizer::TYPE_INTEGER }, - { "0X1234567", Tokenizer::TYPE_INTEGER }, - { "0x89abcdef", Tokenizer::TYPE_INTEGER }, - { "0x89ABCDEF", Tokenizer::TYPE_INTEGER }, - { "01234567", Tokenizer::TYPE_INTEGER }, - - // Test floats. - { "123.45", Tokenizer::TYPE_FLOAT }, - { "1.", Tokenizer::TYPE_FLOAT }, - { "1e3", Tokenizer::TYPE_FLOAT }, - { "1E3", Tokenizer::TYPE_FLOAT }, - { "1e-3", Tokenizer::TYPE_FLOAT }, - { "1e+3", Tokenizer::TYPE_FLOAT }, - { "1.e3", Tokenizer::TYPE_FLOAT }, - { "1.2e3", Tokenizer::TYPE_FLOAT }, - { ".1", Tokenizer::TYPE_FLOAT }, - { ".1e3", Tokenizer::TYPE_FLOAT }, - { ".1e-3", Tokenizer::TYPE_FLOAT }, - { ".1e+3", Tokenizer::TYPE_FLOAT }, - - // Test strings. - { "'hello'", Tokenizer::TYPE_STRING }, - { "\"foo\"", Tokenizer::TYPE_STRING }, - { "'a\"b'", Tokenizer::TYPE_STRING }, - { "\"a'b\"", Tokenizer::TYPE_STRING }, - { "'a\\'b'", Tokenizer::TYPE_STRING }, - { "\"a\\\"b\"", Tokenizer::TYPE_STRING }, - { "'\\xf'", Tokenizer::TYPE_STRING }, - { "'\\0'", Tokenizer::TYPE_STRING }, - - // Test symbols. - { "+", Tokenizer::TYPE_SYMBOL }, - { ".", Tokenizer::TYPE_SYMBOL }, + // Test identifiers. + {"hello", Tokenizer::TYPE_IDENTIFIER}, + + // Test integers. + {"123", Tokenizer::TYPE_INTEGER}, + {"0xab6", Tokenizer::TYPE_INTEGER}, + {"0XAB6", Tokenizer::TYPE_INTEGER}, + {"0X1234567", Tokenizer::TYPE_INTEGER}, + {"0x89abcdef", Tokenizer::TYPE_INTEGER}, + {"0x89ABCDEF", Tokenizer::TYPE_INTEGER}, + {"01234567", Tokenizer::TYPE_INTEGER}, + + // Test floats. + {"123.45", Tokenizer::TYPE_FLOAT}, + {"1.", Tokenizer::TYPE_FLOAT}, + {"1e3", Tokenizer::TYPE_FLOAT}, + {"1E3", Tokenizer::TYPE_FLOAT}, + {"1e-3", Tokenizer::TYPE_FLOAT}, + {"1e+3", Tokenizer::TYPE_FLOAT}, + {"1.e3", Tokenizer::TYPE_FLOAT}, + {"1.2e3", Tokenizer::TYPE_FLOAT}, + {".1", Tokenizer::TYPE_FLOAT}, + {".1e3", Tokenizer::TYPE_FLOAT}, + {".1e-3", Tokenizer::TYPE_FLOAT}, + {".1e+3", Tokenizer::TYPE_FLOAT}, + + // Test strings. + {"'hello'", Tokenizer::TYPE_STRING}, + {"\"foo\"", Tokenizer::TYPE_STRING}, + {"'a\"b'", Tokenizer::TYPE_STRING}, + {"\"a'b\"", Tokenizer::TYPE_STRING}, + {"'a\\'b'", Tokenizer::TYPE_STRING}, + {"\"a\\\"b\"", Tokenizer::TYPE_STRING}, + {"'\\xf'", Tokenizer::TYPE_STRING}, + {"'\\0'", Tokenizer::TYPE_STRING}, + + // Test symbols. + {"+", Tokenizer::TYPE_SYMBOL}, + {".", Tokenizer::TYPE_SYMBOL}, }; TEST_2D(TokenizerTest, SimpleTokens, kSimpleTokenCases, kBlockSizes) { // Set up the tokenizer. TestInputStream input(kSimpleTokenCases_case.input.data(), - kSimpleTokenCases_case.input.size(), - kBlockSizes_case); + kSimpleTokenCases_case.input.size(), kBlockSizes_case); TestErrorCollector error_collector; Tokenizer tokenizer(&input, &error_collector); @@ -326,7 +324,7 @@ TEST_1D(TokenizerTest, FloatSuffix, kBlockSizes) { // In each case, the input is parsed to produce a list of tokens. The // last token in "output" must have type TYPE_END. struct MultiTokenCase { - string input; + std::string input; Tokenizer::Token output[10]; // The compiler wants a constant array // size for initialization to work. There // is no reason this can't be increased if @@ -339,92 +337,101 @@ inline std::ostream& operator<<(std::ostream& out, } MultiTokenCase kMultiTokenCases[] = { - // Test empty input. - { "", { - { Tokenizer::TYPE_END , "" , 0, 0, 0 }, - }}, - - // Test all token types at the same time. - { "foo 1 1.2 + 'bar'", { - { Tokenizer::TYPE_IDENTIFIER, "foo" , 0, 0, 3 }, - { Tokenizer::TYPE_INTEGER , "1" , 0, 4, 5 }, - { Tokenizer::TYPE_FLOAT , "1.2" , 0, 6, 9 }, - { Tokenizer::TYPE_SYMBOL , "+" , 0, 10, 11 }, - { Tokenizer::TYPE_STRING , "'bar'", 0, 12, 17 }, - { Tokenizer::TYPE_END , "" , 0, 17, 17 }, - }}, - - // Test that consecutive symbols are parsed as separate tokens. - { "!@+%", { - { Tokenizer::TYPE_SYMBOL , "!" , 0, 0, 1 }, - { Tokenizer::TYPE_SYMBOL , "@" , 0, 1, 2 }, - { Tokenizer::TYPE_SYMBOL , "+" , 0, 2, 3 }, - { Tokenizer::TYPE_SYMBOL , "%" , 0, 3, 4 }, - { Tokenizer::TYPE_END , "" , 0, 4, 4 }, - }}, - - // Test that newlines affect line numbers correctly. - { "foo bar\nrab oof", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 0, 4, 7 }, - { Tokenizer::TYPE_IDENTIFIER, "rab", 1, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "oof", 1, 4, 7 }, - { Tokenizer::TYPE_END , "" , 1, 7, 7 }, - }}, - - // Test that tabs affect column numbers correctly. - { "foo\tbar \tbaz", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 0, 8, 11 }, - { Tokenizer::TYPE_IDENTIFIER, "baz", 0, 16, 19 }, - { Tokenizer::TYPE_END , "" , 0, 19, 19 }, - }}, - - // Test that tabs in string literals affect column numbers correctly. - { "\"foo\tbar\" baz", { - { Tokenizer::TYPE_STRING , "\"foo\tbar\"", 0, 0, 12 }, - { Tokenizer::TYPE_IDENTIFIER, "baz" , 0, 13, 16 }, - { Tokenizer::TYPE_END , "" , 0, 16, 16 }, - }}, - - // Test that line comments are ignored. - { "foo // This is a comment\n" - "bar // This is another comment", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 1, 0, 3 }, - { Tokenizer::TYPE_END , "" , 1, 30, 30 }, - }}, - - // Test that block comments are ignored. - { "foo /* This is a block comment */ bar", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 0, 34, 37 }, - { Tokenizer::TYPE_END , "" , 0, 37, 37 }, - }}, - - // Test that sh-style comments are not ignored by default. - { "foo # bar\n" - "baz", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_SYMBOL , "#" , 0, 4, 5 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 0, 6, 9 }, - { Tokenizer::TYPE_IDENTIFIER, "baz", 1, 0, 3 }, - { Tokenizer::TYPE_END , "" , 1, 3, 3 }, - }}, - - // Test all whitespace chars - { "foo\n\t\r\v\fbar", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 1, 11, 14 }, - { Tokenizer::TYPE_END , "" , 1, 14, 14 }, - }}, + // Test empty input. + {"", + { + {Tokenizer::TYPE_END, "", 0, 0, 0}, + }}, + + // Test all token types at the same time. + {"foo 1 1.2 + 'bar'", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_INTEGER, "1", 0, 4, 5}, + {Tokenizer::TYPE_FLOAT, "1.2", 0, 6, 9}, + {Tokenizer::TYPE_SYMBOL, "+", 0, 10, 11}, + {Tokenizer::TYPE_STRING, "'bar'", 0, 12, 17}, + {Tokenizer::TYPE_END, "", 0, 17, 17}, + }}, + + // Test that consecutive symbols are parsed as separate tokens. + {"!@+%", + { + {Tokenizer::TYPE_SYMBOL, "!", 0, 0, 1}, + {Tokenizer::TYPE_SYMBOL, "@", 0, 1, 2}, + {Tokenizer::TYPE_SYMBOL, "+", 0, 2, 3}, + {Tokenizer::TYPE_SYMBOL, "%", 0, 3, 4}, + {Tokenizer::TYPE_END, "", 0, 4, 4}, + }}, + + // Test that newlines affect line numbers correctly. + {"foo bar\nrab oof", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 0, 4, 7}, + {Tokenizer::TYPE_IDENTIFIER, "rab", 1, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "oof", 1, 4, 7}, + {Tokenizer::TYPE_END, "", 1, 7, 7}, + }}, + + // Test that tabs affect column numbers correctly. + {"foo\tbar \tbaz", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 0, 8, 11}, + {Tokenizer::TYPE_IDENTIFIER, "baz", 0, 16, 19}, + {Tokenizer::TYPE_END, "", 0, 19, 19}, + }}, + + // Test that tabs in string literals affect column numbers correctly. + {"\"foo\tbar\" baz", + { + {Tokenizer::TYPE_STRING, "\"foo\tbar\"", 0, 0, 12}, + {Tokenizer::TYPE_IDENTIFIER, "baz", 0, 13, 16}, + {Tokenizer::TYPE_END, "", 0, 16, 16}, + }}, + + // Test that line comments are ignored. + {"foo // This is a comment\n" + "bar // This is another comment", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 1, 0, 3}, + {Tokenizer::TYPE_END, "", 1, 30, 30}, + }}, + + // Test that block comments are ignored. + {"foo /* This is a block comment */ bar", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 0, 34, 37}, + {Tokenizer::TYPE_END, "", 0, 37, 37}, + }}, + + // Test that sh-style comments are not ignored by default. + {"foo # bar\n" + "baz", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_SYMBOL, "#", 0, 4, 5}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 0, 6, 9}, + {Tokenizer::TYPE_IDENTIFIER, "baz", 1, 0, 3}, + {Tokenizer::TYPE_END, "", 1, 3, 3}, + }}, + + // Test all whitespace chars + {"foo\n\t\r\v\fbar", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 1, 11, 14}, + {Tokenizer::TYPE_END, "", 1, 14, 14}, + }}, }; TEST_2D(TokenizerTest, MultipleTokens, kMultiTokenCases, kBlockSizes) { // Set up the tokenizer. TestInputStream input(kMultiTokenCases_case.input.data(), - kMultiTokenCases_case.input.size(), - kBlockSizes_case); + kMultiTokenCases_case.input.size(), kBlockSizes_case); TestErrorCollector error_collector; Tokenizer tokenizer(&input, &error_collector); @@ -479,14 +486,14 @@ TEST_2D(TokenizerTest, MultipleTokens, kMultiTokenCases, kBlockSizes) { TEST_1D(TokenizerTest, ShCommentStyle, kBlockSizes) { // Test the "comment_style" option. - const char* text = "foo # bar\n" - "baz // qux\n" - "corge /* grault */\n" - "garply"; + const char* text = + "foo # bar\n" + "baz // qux\n" + "corge /* grault */\n" + "garply"; const char* const kTokens[] = {"foo", // "# bar" is ignored - "baz", "/", "/", "qux", - "corge", "/", "*", "grault", "*", "/", - "garply"}; + "baz", "/", "/", "qux", "corge", "/", + "*", "grault", "*", "/", "garply"}; // Set up the tokenizer. TestInputStream input(text, strlen(text), kBlockSizes_case); @@ -513,7 +520,7 @@ TEST_1D(TokenizerTest, ShCommentStyle, kBlockSizes) { // In each case, the input is expected to have two tokens named "prev" and // "next" with comments in between. struct DocCommentCase { - string input; + std::string input; const char* prev_trailing_comments; const char* detached_comments[10]; @@ -526,164 +533,136 @@ inline std::ostream& operator<<(std::ostream& out, } DocCommentCase kDocCommentCases[] = { - { - "prev next", - - "", - {}, - "" - }, - - { - "prev /* ignored */ next", - - "", - {}, - "" - }, - - { - "prev // trailing comment\n" - "next", - - " trailing comment\n", - {}, - "" - }, - - { - "prev\n" - "// leading comment\n" - "// line 2\n" - "next", - - "", - {}, - " leading comment\n" - " line 2\n" - }, - - { - "prev\n" - "// trailing comment\n" - "// line 2\n" - "\n" - "next", - - " trailing comment\n" - " line 2\n", - {}, - "" - }, - - { - "prev // trailing comment\n" - "// leading comment\n" - "// line 2\n" - "next", - - " trailing comment\n", - {}, - " leading comment\n" - " line 2\n" - }, - - { - "prev /* trailing block comment */\n" - "/* leading block comment\n" - " * line 2\n" - " * line 3 */" - "next", - - " trailing block comment ", - {}, - " leading block comment\n" - " line 2\n" - " line 3 " - }, - - { - "prev\n" - "/* trailing block comment\n" - " * line 2\n" - " * line 3\n" - " */\n" - "/* leading block comment\n" - " * line 2\n" - " * line 3 */" - "next", - - " trailing block comment\n" - " line 2\n" - " line 3\n", - {}, - " leading block comment\n" - " line 2\n" - " line 3 " - }, - - { - "prev\n" - "// trailing comment\n" - "\n" - "// detached comment\n" - "// line 2\n" - "\n" - "// second detached comment\n" - "/* third detached comment\n" - " * line 2 */\n" - "// leading comment\n" - "next", - - " trailing comment\n", - { - " detached comment\n" - " line 2\n", - " second detached comment\n", - " third detached comment\n" - " line 2 " - }, - " leading comment\n" - }, - - { - "prev /**/\n" - "\n" - "// detached comment\n" - "\n" - "// leading comment\n" - "next", - - "", - { - " detached comment\n" - }, - " leading comment\n" - }, - - { - "prev /**/\n" - "// leading comment\n" - "next", - - "", - {}, - " leading comment\n" - }, - }; + {"prev next", + + "", + {}, + ""}, + + {"prev /* ignored */ next", + + "", + {}, + ""}, + + {"prev // trailing comment\n" + "next", + + " trailing comment\n", + {}, + ""}, + + {"prev\n" + "// leading comment\n" + "// line 2\n" + "next", + + "", + {}, + " leading comment\n" + " line 2\n"}, + + {"prev\n" + "// trailing comment\n" + "// line 2\n" + "\n" + "next", + + " trailing comment\n" + " line 2\n", + {}, + ""}, + + {"prev // trailing comment\n" + "// leading comment\n" + "// line 2\n" + "next", + + " trailing comment\n", + {}, + " leading comment\n" + " line 2\n"}, + + {"prev /* trailing block comment */\n" + "/* leading block comment\n" + " * line 2\n" + " * line 3 */" + "next", + + " trailing block comment ", + {}, + " leading block comment\n" + " line 2\n" + " line 3 "}, + + {"prev\n" + "/* trailing block comment\n" + " * line 2\n" + " * line 3\n" + " */\n" + "/* leading block comment\n" + " * line 2\n" + " * line 3 */" + "next", + + " trailing block comment\n" + " line 2\n" + " line 3\n", + {}, + " leading block comment\n" + " line 2\n" + " line 3 "}, + + {"prev\n" + "// trailing comment\n" + "\n" + "// detached comment\n" + "// line 2\n" + "\n" + "// second detached comment\n" + "/* third detached comment\n" + " * line 2 */\n" + "// leading comment\n" + "next", + + " trailing comment\n", + {" detached comment\n" + " line 2\n", + " second detached comment\n", + " third detached comment\n" + " line 2 "}, + " leading comment\n"}, + + {"prev /**/\n" + "\n" + "// detached comment\n" + "\n" + "// leading comment\n" + "next", + + "", + {" detached comment\n"}, + " leading comment\n"}, + + {"prev /**/\n" + "// leading comment\n" + "next", + + "", + {}, + " leading comment\n"}, +}; TEST_2D(TokenizerTest, DocComments, kDocCommentCases, kBlockSizes) { // Set up the tokenizer. TestInputStream input(kDocCommentCases_case.input.data(), - kDocCommentCases_case.input.size(), - kBlockSizes_case); + kDocCommentCases_case.input.size(), kBlockSizes_case); TestErrorCollector error_collector; Tokenizer tokenizer(&input, &error_collector); // Set up a second tokenizer where we'll pass all NULLs to NextWithComments(). TestInputStream input2(kDocCommentCases_case.input.data(), - kDocCommentCases_case.input.size(), - kBlockSizes_case); + kDocCommentCases_case.input.size(), kBlockSizes_case); Tokenizer tokenizer2(&input2, &error_collector); tokenizer.Next(); @@ -692,9 +671,9 @@ TEST_2D(TokenizerTest, DocComments, kDocCommentCases, kBlockSizes) { EXPECT_EQ("prev", tokenizer.current().text); EXPECT_EQ("prev", tokenizer2.current().text); - string prev_trailing_comments; - std::vector detached_comments; - string next_leading_comments; + std::string prev_trailing_comments; + std::vector detached_comments; + std::string next_leading_comments; tokenizer.NextWithComments(&prev_trailing_comments, &detached_comments, &next_leading_comments); tokenizer2.NextWithComments(NULL, NULL, NULL); @@ -707,16 +686,14 @@ TEST_2D(TokenizerTest, DocComments, kDocCommentCases, kBlockSizes) { for (int i = 0; i < detached_comments.size(); i++) { ASSERT_LT(i, GOOGLE_ARRAYSIZE(kDocCommentCases)); ASSERT_TRUE(kDocCommentCases_case.detached_comments[i] != NULL); - EXPECT_EQ(kDocCommentCases_case.detached_comments[i], - detached_comments[i]); + EXPECT_EQ(kDocCommentCases_case.detached_comments[i], detached_comments[i]); } // Verify that we matched all the detached comments. EXPECT_EQ(NULL, - kDocCommentCases_case.detached_comments[detached_comments.size()]); + kDocCommentCases_case.detached_comments[detached_comments.size()]); - EXPECT_EQ(kDocCommentCases_case.next_leading_comments, - next_leading_comments); + EXPECT_EQ(kDocCommentCases_case.next_leading_comments, next_leading_comments); } // ------------------------------------------------------------------- @@ -745,30 +722,30 @@ TEST_F(TokenizerTest, ParseInteger) { EXPECT_FALSE(Tokenizer::ParseInteger("-1", kuint64max, &i)); // Test overflows. - EXPECT_TRUE (Tokenizer::ParseInteger("0", 0, &i)); + EXPECT_TRUE(Tokenizer::ParseInteger("0", 0, &i)); EXPECT_FALSE(Tokenizer::ParseInteger("1", 0, &i)); - EXPECT_TRUE (Tokenizer::ParseInteger("1", 1, &i)); - EXPECT_TRUE (Tokenizer::ParseInteger("12345", 12345, &i)); + EXPECT_TRUE(Tokenizer::ParseInteger("1", 1, &i)); + EXPECT_TRUE(Tokenizer::ParseInteger("12345", 12345, &i)); EXPECT_FALSE(Tokenizer::ParseInteger("12346", 12345, &i)); - EXPECT_TRUE (Tokenizer::ParseInteger("0xFFFFFFFFFFFFFFFF" , kuint64max, &i)); + EXPECT_TRUE(Tokenizer::ParseInteger("0xFFFFFFFFFFFFFFFF", kuint64max, &i)); EXPECT_FALSE(Tokenizer::ParseInteger("0x10000000000000000", kuint64max, &i)); } TEST_F(TokenizerTest, ParseFloat) { - EXPECT_DOUBLE_EQ(1 , Tokenizer::ParseFloat("1.")); - EXPECT_DOUBLE_EQ(1e3 , Tokenizer::ParseFloat("1e3")); - EXPECT_DOUBLE_EQ(1e3 , Tokenizer::ParseFloat("1E3")); + EXPECT_DOUBLE_EQ(1, Tokenizer::ParseFloat("1.")); + EXPECT_DOUBLE_EQ(1e3, Tokenizer::ParseFloat("1e3")); + EXPECT_DOUBLE_EQ(1e3, Tokenizer::ParseFloat("1E3")); EXPECT_DOUBLE_EQ(1.5e3, Tokenizer::ParseFloat("1.5e3")); - EXPECT_DOUBLE_EQ(.1 , Tokenizer::ParseFloat(".1")); - EXPECT_DOUBLE_EQ(.25 , Tokenizer::ParseFloat(".25")); - EXPECT_DOUBLE_EQ(.1e3 , Tokenizer::ParseFloat(".1e3")); + EXPECT_DOUBLE_EQ(.1, Tokenizer::ParseFloat(".1")); + EXPECT_DOUBLE_EQ(.25, Tokenizer::ParseFloat(".25")); + EXPECT_DOUBLE_EQ(.1e3, Tokenizer::ParseFloat(".1e3")); EXPECT_DOUBLE_EQ(.25e3, Tokenizer::ParseFloat(".25e3")); EXPECT_DOUBLE_EQ(.1e+3, Tokenizer::ParseFloat(".1e+3")); EXPECT_DOUBLE_EQ(.1e-3, Tokenizer::ParseFloat(".1e-3")); - EXPECT_DOUBLE_EQ(5 , Tokenizer::ParseFloat("5")); + EXPECT_DOUBLE_EQ(5, Tokenizer::ParseFloat("5")); EXPECT_DOUBLE_EQ(6e-12, Tokenizer::ParseFloat("6e-12")); - EXPECT_DOUBLE_EQ(1.2 , Tokenizer::ParseFloat("1.2")); - EXPECT_DOUBLE_EQ(1.e2 , Tokenizer::ParseFloat("1.e2")); + EXPECT_DOUBLE_EQ(1.2, Tokenizer::ParseFloat("1.2")); + EXPECT_DOUBLE_EQ(1.e2, Tokenizer::ParseFloat("1.e2")); // Test invalid integers that may still be tokenized as integers. EXPECT_DOUBLE_EQ(1, Tokenizer::ParseFloat("1e")); @@ -782,22 +759,25 @@ TEST_F(TokenizerTest, ParseFloat) { // These should parse successfully even though they are out of range. // Overflows become infinity and underflows become zero. - EXPECT_EQ( 0.0, Tokenizer::ParseFloat("1e-9999999999999999999999999999")); + EXPECT_EQ(0.0, Tokenizer::ParseFloat("1e-9999999999999999999999999999")); EXPECT_EQ(HUGE_VAL, Tokenizer::ParseFloat("1e+9999999999999999999999999999")); #ifdef PROTOBUF_HAS_DEATH_TEST // death tests do not work on Windows yet // Test invalid integers that will never be tokenized as integers. - EXPECT_DEBUG_DEATH(Tokenizer::ParseFloat("zxy"), - "passed text that could not have been tokenized as a float"); - EXPECT_DEBUG_DEATH(Tokenizer::ParseFloat("1-e0"), - "passed text that could not have been tokenized as a float"); - EXPECT_DEBUG_DEATH(Tokenizer::ParseFloat("-1.0"), - "passed text that could not have been tokenized as a float"); + EXPECT_DEBUG_DEATH( + Tokenizer::ParseFloat("zxy"), + "passed text that could not have been tokenized as a float"); + EXPECT_DEBUG_DEATH( + Tokenizer::ParseFloat("1-e0"), + "passed text that could not have been tokenized as a float"); + EXPECT_DEBUG_DEATH( + Tokenizer::ParseFloat("-1.0"), + "passed text that could not have been tokenized as a float"); #endif // PROTOBUF_HAS_DEATH_TEST } TEST_F(TokenizerTest, ParseString) { - string output; + std::string output; Tokenizer::ParseString("'hello'", &output); EXPECT_EQ("hello", output); Tokenizer::ParseString("\"blah\\nblah2\"", &output); @@ -833,14 +813,15 @@ TEST_F(TokenizerTest, ParseString) { // Test invalid strings that will never be tokenized as strings. #ifdef PROTOBUF_HAS_DEATH_TEST // death tests do not work on Windows yet - EXPECT_DEBUG_DEATH(Tokenizer::ParseString("", &output), - "passed text that could not have been tokenized as a string"); + EXPECT_DEBUG_DEATH( + Tokenizer::ParseString("", &output), + "passed text that could not have been tokenized as a string"); #endif // PROTOBUF_HAS_DEATH_TEST } TEST_F(TokenizerTest, ParseStringAppend) { // Check that ParseString and ParseStringAppend differ. - string output("stuff+"); + std::string output("stuff+"); Tokenizer::ParseStringAppend("'hello'", &output); EXPECT_EQ("stuff+hello", output); Tokenizer::ParseString("'hello'", &output); @@ -852,7 +833,7 @@ TEST_F(TokenizerTest, ParseStringAppend) { // Each case parses some input text, ignoring the tokens produced, and // checks that the error output matches what is expected. struct ErrorCase { - string input; + std::string input; bool recoverable; // True if the tokenizer should be able to recover and // parse more tokens after seeing this error. Cases // for which this is true must end with "foo" as @@ -865,93 +846,76 @@ inline std::ostream& operator<<(std::ostream& out, const ErrorCase& test_case) { } ErrorCase kErrorCases[] = { - // String errors. - { "'\\l' foo", true, - "0:2: Invalid escape sequence in string literal.\n" }, - { "'\\X' foo", true, - "0:2: Invalid escape sequence in string literal.\n" }, - { "'\\x' foo", true, - "0:3: Expected hex digits for escape sequence.\n" }, - { "'foo", false, - "0:4: Unexpected end of string.\n" }, - { "'bar\nfoo", true, - "0:4: String literals cannot cross line boundaries.\n" }, - { "'\\u01' foo", true, - "0:5: Expected four hex digits for \\u escape sequence.\n" }, - { "'\\u01' foo", true, - "0:5: Expected four hex digits for \\u escape sequence.\n" }, - { "'\\uXYZ' foo", true, - "0:3: Expected four hex digits for \\u escape sequence.\n" }, - - // Integer errors. - { "123foo", true, - "0:3: Need space between number and identifier.\n" }, - - // Hex/octal errors. - { "0x foo", true, - "0:2: \"0x\" must be followed by hex digits.\n" }, - { "0541823 foo", true, - "0:4: Numbers starting with leading zero must be in octal.\n" }, - { "0x123z foo", true, - "0:5: Need space between number and identifier.\n" }, - { "0x123.4 foo", true, - "0:5: Hex and octal numbers must be integers.\n" }, - { "0123.4 foo", true, - "0:4: Hex and octal numbers must be integers.\n" }, - - // Float errors. - { "1e foo", true, - "0:2: \"e\" must be followed by exponent.\n" }, - { "1e- foo", true, - "0:3: \"e\" must be followed by exponent.\n" }, - { "1.2.3 foo", true, - "0:3: Already saw decimal point or exponent; can't have another one.\n" }, - { "1e2.3 foo", true, - "0:3: Already saw decimal point or exponent; can't have another one.\n" }, - { "a.1 foo", true, - "0:1: Need space between identifier and decimal point.\n" }, - // allow_f_after_float not enabled, so this should be an error. - { "1.0f foo", true, - "0:3: Need space between number and identifier.\n" }, - - // Block comment errors. - { "/*", false, - "0:2: End-of-file inside block comment.\n" - "0:0: Comment started here.\n"}, - { "/*/*/ foo", true, - "0:3: \"/*\" inside block comment. Block comments cannot be nested.\n"}, - - // Control characters. Multiple consecutive control characters should only - // produce one error. - { "\b foo", true, - "0:0: Invalid control characters encountered in text.\n" }, - { "\b\b foo", true, - "0:0: Invalid control characters encountered in text.\n" }, - - // Check that control characters at end of input don't result in an - // infinite loop. - { "\b", false, - "0:0: Invalid control characters encountered in text.\n" }, - - // Check recovery from '\0'. We have to explicitly specify the length of - // these strings because otherwise the string constructor will just call - // strlen() which will see the first '\0' and think that is the end of the - // string. - { string("\0foo", 4), true, - "0:0: Invalid control characters encountered in text.\n" }, - { string("\0\0foo", 5), true, - "0:0: Invalid control characters encountered in text.\n" }, - - // Check error from high order bits set - { "\300foo", true, - "0:0: Interpreting non ascii codepoint 192.\n" }, + // String errors. + {"'\\l' foo", true, "0:2: Invalid escape sequence in string literal.\n"}, + {"'\\X' foo", true, "0:2: Invalid escape sequence in string literal.\n"}, + {"'\\x' foo", true, "0:3: Expected hex digits for escape sequence.\n"}, + {"'foo", false, "0:4: Unexpected end of string.\n"}, + {"'bar\nfoo", true, "0:4: String literals cannot cross line boundaries.\n"}, + {"'\\u01' foo", true, + "0:5: Expected four hex digits for \\u escape sequence.\n"}, + {"'\\u01' foo", true, + "0:5: Expected four hex digits for \\u escape sequence.\n"}, + {"'\\uXYZ' foo", true, + "0:3: Expected four hex digits for \\u escape sequence.\n"}, + + // Integer errors. + {"123foo", true, "0:3: Need space between number and identifier.\n"}, + + // Hex/octal errors. + {"0x foo", true, "0:2: \"0x\" must be followed by hex digits.\n"}, + {"0541823 foo", true, + "0:4: Numbers starting with leading zero must be in octal.\n"}, + {"0x123z foo", true, "0:5: Need space between number and identifier.\n"}, + {"0x123.4 foo", true, "0:5: Hex and octal numbers must be integers.\n"}, + {"0123.4 foo", true, "0:4: Hex and octal numbers must be integers.\n"}, + + // Float errors. + {"1e foo", true, "0:2: \"e\" must be followed by exponent.\n"}, + {"1e- foo", true, "0:3: \"e\" must be followed by exponent.\n"}, + {"1.2.3 foo", true, + "0:3: Already saw decimal point or exponent; can't have another one.\n"}, + {"1e2.3 foo", true, + "0:3: Already saw decimal point or exponent; can't have another one.\n"}, + {"a.1 foo", true, + "0:1: Need space between identifier and decimal point.\n"}, + // allow_f_after_float not enabled, so this should be an error. + {"1.0f foo", true, "0:3: Need space between number and identifier.\n"}, + + // Block comment errors. + {"/*", false, + "0:2: End-of-file inside block comment.\n" + "0:0: Comment started here.\n"}, + {"/*/*/ foo", true, + "0:3: \"/*\" inside block comment. Block comments cannot be nested.\n"}, + + // Control characters. Multiple consecutive control characters should only + // produce one error. + {"\b foo", true, "0:0: Invalid control characters encountered in text.\n"}, + {"\b\b foo", true, + "0:0: Invalid control characters encountered in text.\n"}, + + // Check that control characters at end of input don't result in an + // infinite loop. + {"\b", false, "0:0: Invalid control characters encountered in text.\n"}, + + // Check recovery from '\0'. We have to explicitly specify the length of + // these strings because otherwise the string constructor will just call + // strlen() which will see the first '\0' and think that is the end of the + // string. + {std::string("\0foo", 4), true, + "0:0: Invalid control characters encountered in text.\n"}, + {std::string("\0\0foo", 5), true, + "0:0: Invalid control characters encountered in text.\n"}, + + // Check error from high order bits set + {"\300foo", true, "0:0: Interpreting non ascii codepoint 192.\n"}, }; TEST_2D(TokenizerTest, Errors, kErrorCases, kBlockSizes) { // Set up the tokenizer. TestInputStream input(kErrorCases_case.input.data(), - kErrorCases_case.input.size(), - kBlockSizes_case); + kErrorCases_case.input.size(), kBlockSizes_case); TestErrorCollector error_collector; Tokenizer tokenizer(&input, &error_collector); @@ -973,7 +937,7 @@ TEST_2D(TokenizerTest, Errors, kErrorCases, kBlockSizes) { // ------------------------------------------------------------------- TEST_1D(TokenizerTest, BackUpOnDestruction, kBlockSizes) { - string text = "foo bar"; + std::string text = "foo bar"; TestInputStream input(text.data(), text.size(), kBlockSizes_case); // Create a tokenizer, read one token, then destroy it. diff --git a/src/google/protobuf/io/zero_copy_stream.h b/src/google/protobuf/io/zero_copy_stream.h index f532d75967..de2374b513 100644 --- a/src/google/protobuf/io/zero_copy_stream.h +++ b/src/google/protobuf/io/zero_copy_stream.h @@ -110,6 +110,8 @@ #include #include +#include + namespace google { namespace protobuf { @@ -121,7 +123,7 @@ class ZeroCopyOutputStream; // Abstract interface similar to an input stream but designed to minimize // copying. -class LIBPROTOBUF_EXPORT ZeroCopyInputStream { +class PROTOBUF_EXPORT ZeroCopyInputStream { public: ZeroCopyInputStream() {} virtual ~ZeroCopyInputStream() {} @@ -178,7 +180,7 @@ class LIBPROTOBUF_EXPORT ZeroCopyInputStream { // Abstract interface similar to an output stream but designed to minimize // copying. -class LIBPROTOBUF_EXPORT ZeroCopyOutputStream { +class PROTOBUF_EXPORT ZeroCopyOutputStream { public: ZeroCopyOutputStream() {} virtual ~ZeroCopyOutputStream() {} @@ -245,4 +247,6 @@ class LIBPROTOBUF_EXPORT ZeroCopyOutputStream { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_H__ diff --git a/src/google/protobuf/io/zero_copy_stream_impl.cc b/src/google/protobuf/io/zero_copy_stream_impl.cc index dd92113573..4af76b786e 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl.cc +++ b/src/google/protobuf/io/zero_copy_stream_impl.cc @@ -33,19 +33,19 @@ // Sanjay Ghemawat, Jeff Dean, and others. #ifndef _MSC_VER -#include -#include -#include #include +#include +#include +#include #endif #include -#include #include -#include +#include #include #include +#include +#include #include -#include namespace google { @@ -58,11 +58,11 @@ namespace io { #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::internal::win32::access; -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::open; -using google::protobuf::internal::win32::read; -using google::protobuf::internal::win32::write; +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 { @@ -78,42 +78,30 @@ int close_no_eintr(int fd) { } // namespace - // =================================================================== FileInputStream::FileInputStream(int file_descriptor, int block_size) - : copying_input_(file_descriptor), - impl_(©ing_input_, block_size) { -} + : copying_input_(file_descriptor), impl_(©ing_input_, block_size) {} -bool FileInputStream::Close() { - return copying_input_.Close(); -} +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); -} +void FileInputStream::BackUp(int count) { impl_.BackUp(count); } -bool FileInputStream::Skip(int count) { - return impl_.Skip(count); -} +bool FileInputStream::Skip(int count) { return impl_.Skip(count); } -int64 FileInputStream::ByteCount() const { - return impl_.ByteCount(); -} +int64 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) { -} + : file_(file_descriptor), + close_on_delete_(false), + is_closed_(false), + errno_(0), + previous_seek_failed_(false) {} FileInputStream::CopyingFileInputStream::~CopyingFileInputStream() { if (close_on_delete_) { @@ -157,8 +145,7 @@ int FileInputStream::CopyingFileInputStream::Read(void* buffer, int size) { int FileInputStream::CopyingFileInputStream::Skip(int count) { GOOGLE_CHECK(!is_closed_); - if (!previous_seek_failed_ && - lseek(file_, count, SEEK_CUR) != (off_t)-1) { + if (!previous_seek_failed_ && lseek(file_, count, SEEK_CUR) != (off_t)-1) { // Seek succeeded. return count; } else { @@ -176,42 +163,31 @@ int FileInputStream::CopyingFileInputStream::Skip(int count) { // =================================================================== FileOutputStream::FileOutputStream(int file_descriptor, int block_size) - : copying_output_(file_descriptor), - impl_(©ing_output_, block_size) { -} + : copying_output_(file_descriptor), impl_(©ing_output_, block_size) {} -FileOutputStream::~FileOutputStream() { - impl_.Flush(); -} +FileOutputStream::~FileOutputStream() { impl_.Flush(); } bool FileOutputStream::Close() { bool flush_succeeded = impl_.Flush(); return copying_output_.Close() && flush_succeeded; } -bool FileOutputStream::Flush() { - return impl_.Flush(); -} +bool FileOutputStream::Flush() { return impl_.Flush(); } bool FileOutputStream::Next(void** data, int* size) { return impl_.Next(data, size); } -void FileOutputStream::BackUp(int count) { - impl_.BackUp(count); -} +void FileOutputStream::BackUp(int count) { impl_.BackUp(count); } -int64 FileOutputStream::ByteCount() const { - return impl_.ByteCount(); -} +int64 FileOutputStream::ByteCount() const { return impl_.ByteCount(); } FileOutputStream::CopyingFileOutputStream::CopyingFileOutputStream( int file_descriptor) - : file_(file_descriptor), - close_on_delete_(false), - is_closed_(false), - errno_(0) { -} + : file_(file_descriptor), + close_on_delete_(false), + is_closed_(false), + errno_(0) {} FileOutputStream::CopyingFileOutputStream::~CopyingFileOutputStream() { if (close_on_delete_) { @@ -236,8 +212,8 @@ bool FileOutputStream::CopyingFileOutputStream::Close() { return true; } -bool FileOutputStream::CopyingFileOutputStream::Write( - const void* buffer, int size) { +bool FileOutputStream::CopyingFileOutputStream::Write(const void* buffer, + int size) { GOOGLE_CHECK(!is_closed_); int total_written = 0; @@ -280,17 +256,11 @@ bool IstreamInputStream::Next(const void** data, int* size) { return impl_.Next(data, size); } -void IstreamInputStream::BackUp(int count) { - impl_.BackUp(count); -} +void IstreamInputStream::BackUp(int count) { impl_.BackUp(count); } -bool IstreamInputStream::Skip(int count) { - return impl_.Skip(count); -} +bool IstreamInputStream::Skip(int count) { return impl_.Skip(count); } -int64 IstreamInputStream::ByteCount() const { - return impl_.ByteCount(); -} +int64 IstreamInputStream::ByteCount() const { return impl_.ByteCount(); } IstreamInputStream::CopyingIstreamInputStream::CopyingIstreamInputStream( std::istream* input) @@ -298,8 +268,8 @@ IstreamInputStream::CopyingIstreamInputStream::CopyingIstreamInputStream( IstreamInputStream::CopyingIstreamInputStream::~CopyingIstreamInputStream() {} -int IstreamInputStream::CopyingIstreamInputStream::Read( - void* buffer, int size) { +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()) { @@ -313,21 +283,15 @@ int IstreamInputStream::CopyingIstreamInputStream::Read( OstreamOutputStream::OstreamOutputStream(std::ostream* output, int block_size) : copying_output_(output), impl_(©ing_output_, block_size) {} -OstreamOutputStream::~OstreamOutputStream() { - impl_.Flush(); -} +OstreamOutputStream::~OstreamOutputStream() { impl_.Flush(); } bool OstreamOutputStream::Next(void** data, int* size) { return impl_.Next(data, size); } -void OstreamOutputStream::BackUp(int count) { - impl_.BackUp(count); -} +void OstreamOutputStream::BackUp(int count) { impl_.BackUp(count); } -int64 OstreamOutputStream::ByteCount() const { - return impl_.ByteCount(); -} +int64 OstreamOutputStream::ByteCount() const { return impl_.ByteCount(); } OstreamOutputStream::CopyingOstreamOutputStream::CopyingOstreamOutputStream( std::ostream* output) @@ -336,8 +300,8 @@ OstreamOutputStream::CopyingOstreamOutputStream::CopyingOstreamOutputStream( OstreamOutputStream::CopyingOstreamOutputStream::~CopyingOstreamOutputStream() { } -bool OstreamOutputStream::CopyingOstreamOutputStream::Write( - const void* buffer, int size) { +bool OstreamOutputStream::CopyingOstreamOutputStream::Write(const void* buffer, + int size) { output_->write(reinterpret_cast(buffer), size); return output_->good(); } @@ -346,7 +310,7 @@ bool OstreamOutputStream::CopyingOstreamOutputStream::Write( ConcatenatingInputStream::ConcatenatingInputStream( ZeroCopyInputStream* const streams[], int count) - : streams_(streams), stream_count_(count), bytes_retired_(0) { + : streams_(streams), stream_count_(count), bytes_retired_(0) { } bool ConcatenatingInputStream::Next(const void** data, int* size) { @@ -402,63 +366,6 @@ int64 ConcatenatingInputStream::ByteCount() const { } -// =================================================================== - -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 LimitingInputStream::ByteCount() const { - if (limit_ < 0) { - return input_->ByteCount() + limit_ - prior_bytes_read_; - } else { - return input_->ByteCount() - prior_bytes_read_; - } -} - - // =================================================================== } // namespace io diff --git a/src/google/protobuf/io/zero_copy_stream_impl.h b/src/google/protobuf/io/zero_copy_stream_impl.h index 206fd0d459..fd06c06cc2 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl.h +++ b/src/google/protobuf/io/zero_copy_stream_impl.h @@ -40,18 +40,19 @@ #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__ #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__ -#include #include +#include +#include #include #include -#include +#include + namespace google { namespace protobuf { namespace io { - // =================================================================== // A ZeroCopyInputStream which reads from a file descriptor. @@ -60,7 +61,7 @@ namespace io { // 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 LIBPROTOBUF_EXPORT FileInputStream : public ZeroCopyInputStream { +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 @@ -84,7 +85,7 @@ class LIBPROTOBUF_EXPORT FileInputStream : public ZeroCopyInputStream { // errno from that error. Otherwise, this is zero. Once an error // occurs, the stream is broken and all subsequent operations will // fail. - int GetErrno() { return copying_input_.GetErrno(); } + int GetErrno() const { return copying_input_.GetErrno(); } // implements ZeroCopyInputStream ---------------------------------- bool Next(const void** data, int* size) override; @@ -93,14 +94,14 @@ class LIBPROTOBUF_EXPORT FileInputStream : public ZeroCopyInputStream { int64 ByteCount() const override; private: - class LIBPROTOBUF_EXPORT CopyingFileInputStream : public CopyingInputStream { + 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() { return errno_; } + int GetErrno() const { return errno_; } // implements CopyingInputStream --------------------------------- int Read(void* buffer, int size) override; @@ -137,7 +138,7 @@ class LIBPROTOBUF_EXPORT FileInputStream : public ZeroCopyInputStream { // harming performance. Also, it's conceivable that FileOutputStream could // someday be enhanced to use zero-copy file descriptors on OSs which // support them. -class LIBPROTOBUF_EXPORT FileOutputStream : public ZeroCopyOutputStream { +class PROTOBUF_EXPORT FileOutputStream : 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 @@ -167,7 +168,7 @@ class LIBPROTOBUF_EXPORT FileOutputStream : public ZeroCopyOutputStream { // errno from that error. Otherwise, this is zero. Once an error // occurs, the stream is broken and all subsequent operations will // fail. - int GetErrno() { return copying_output_.GetErrno(); } + int GetErrno() const { return copying_output_.GetErrno(); } // implements ZeroCopyOutputStream --------------------------------- bool Next(void** data, int* size) override; @@ -175,14 +176,14 @@ class LIBPROTOBUF_EXPORT FileOutputStream : public ZeroCopyOutputStream { int64 ByteCount() const override; private: - class LIBPROTOBUF_EXPORT CopyingFileOutputStream : public CopyingOutputStream { + 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() { return errno_; } + int GetErrno() const { return errno_; } // implements CopyingOutputStream -------------------------------- bool Write(const void* buffer, int size) override; @@ -211,7 +212,7 @@ class LIBPROTOBUF_EXPORT FileOutputStream : public ZeroCopyOutputStream { // // Note that for reading files (or anything represented by a file descriptor), // FileInputStream is more efficient. -class LIBPROTOBUF_EXPORT IstreamInputStream : public ZeroCopyInputStream { +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 @@ -226,7 +227,7 @@ class LIBPROTOBUF_EXPORT IstreamInputStream : public ZeroCopyInputStream { int64 ByteCount() const override; private: - class LIBPROTOBUF_EXPORT CopyingIstreamInputStream : public CopyingInputStream { + class PROTOBUF_EXPORT CopyingIstreamInputStream : public CopyingInputStream { public: CopyingIstreamInputStream(std::istream* input); ~CopyingIstreamInputStream() override; @@ -254,7 +255,7 @@ class LIBPROTOBUF_EXPORT IstreamInputStream : public ZeroCopyInputStream { // // Note that for writing files (or anything represented by a file descriptor), // FileOutputStream is more efficient. -class LIBPROTOBUF_EXPORT OstreamOutputStream : public ZeroCopyOutputStream { +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 @@ -269,7 +270,8 @@ class LIBPROTOBUF_EXPORT OstreamOutputStream : public ZeroCopyOutputStream { int64 ByteCount() const override; private: - class LIBPROTOBUF_EXPORT CopyingOstreamOutputStream : public CopyingOutputStream { + class PROTOBUF_EXPORT CopyingOstreamOutputStream + : public CopyingOutputStream { public: CopyingOstreamOutputStream(std::ostream* output); ~CopyingOstreamOutputStream() override; @@ -299,7 +301,7 @@ class LIBPROTOBUF_EXPORT OstreamOutputStream : public ZeroCopyOutputStream { // 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 LIBPROTOBUF_EXPORT ConcatenatingInputStream : public ZeroCopyInputStream { +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. @@ -325,32 +327,10 @@ class LIBPROTOBUF_EXPORT ConcatenatingInputStream : public ZeroCopyInputStream { // =================================================================== -// A ZeroCopyInputStream which wraps some other stream and limits it to -// a particular byte count. -class LIBPROTOBUF_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 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); -}; - -// =================================================================== - } // namespace io } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__ diff --git a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc index 7d7b689b13..dd45a4cc2e 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc +++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc @@ -55,14 +55,12 @@ static const int kDefaultBlockSize = 8192; // =================================================================== -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) { -} +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_) { @@ -73,7 +71,7 @@ bool ArrayInputStream::Next(const void** data, int* size) { return true; } else { // We're at the end of the array. - last_returned_size_ = 0; // Don't let caller back up. + last_returned_size_ = 0; // Don't let caller back up. return false; } } @@ -89,7 +87,7 @@ void ArrayInputStream::BackUp(int count) { bool ArrayInputStream::Skip(int count) { GOOGLE_CHECK_GE(count, 0); - last_returned_size_ = 0; // Don't let caller back up. + last_returned_size_ = 0; // Don't let caller back up. if (count > size_ - position_) { position_ = size_; return false; @@ -99,20 +97,17 @@ bool ArrayInputStream::Skip(int count) { } } -int64 ArrayInputStream::ByteCount() const { - return position_; -} +int64 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) { -} + : 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_) { @@ -123,7 +118,7 @@ bool ArrayOutputStream::Next(void** data, int* size) { return true; } else { // We're at the end of the array. - last_returned_size_ = 0; // Don't let caller back up. + last_returned_size_ = 0; // Don't let caller back up. return false; } } @@ -137,15 +132,11 @@ void ArrayOutputStream::BackUp(int count) { last_returned_size_ = 0; // Don't let caller back up further. } -int64 ArrayOutputStream::ByteCount() const { - return position_; -} +int64 ArrayOutputStream::ByteCount() const { return position_; } // =================================================================== -StringOutputStream::StringOutputStream(string* target) - : target_(target) { -} +StringOutputStream::StringOutputStream(std::string* target) : target_(target) {} bool StringOutputStream::Next(void** data, int* size) { GOOGLE_CHECK(target_ != NULL); @@ -190,10 +181,6 @@ int64 StringOutputStream::ByteCount() const { return target_->size(); } -void StringOutputStream::SetString(string* target) { - target_ = target; -} - // =================================================================== int CopyingInputStream::Skip(int count) { @@ -201,7 +188,7 @@ int CopyingInputStream::Skip(int count) { int skipped = 0; while (skipped < count) { int bytes = Read(junk, std::min(count - skipped, - ::google::protobuf::implicit_cast(sizeof(junk)))); + implicit_cast(sizeof(junk)))); if (bytes <= 0) { // EOF or read error. return skipped; @@ -213,14 +200,13 @@ int CopyingInputStream::Skip(int count) { 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) { -} + : 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_) { @@ -264,12 +250,11 @@ bool CopyingInputStreamAdaptor::Next(const void** data, int* size) { void CopyingInputStreamAdaptor::BackUp(int count) { GOOGLE_CHECK(backup_bytes_ == 0 && buffer_.get() != NULL) - << " BackUp() can only be called after Next()."; + << " 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."; + << " 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; } @@ -317,13 +302,12 @@ void CopyingInputStreamAdaptor::FreeBuffer() { 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) { -} + : 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(); @@ -332,9 +316,7 @@ CopyingOutputStreamAdaptor::~CopyingOutputStreamAdaptor() { } } -bool CopyingOutputStreamAdaptor::Flush() { - return WriteBuffer(); -} +bool CopyingOutputStreamAdaptor::Flush() { return WriteBuffer(); } bool CopyingOutputStreamAdaptor::Next(void** data, int* size) { if (buffer_used_ == buffer_size_) { @@ -352,10 +334,10 @@ bool CopyingOutputStreamAdaptor::Next(void** data, int* size) { void CopyingOutputStreamAdaptor::BackUp(int count) { GOOGLE_CHECK_GE(count, 0); GOOGLE_CHECK_EQ(buffer_used_, buffer_size_) - << " BackUp() can only be called after Next()."; + << " 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()."; + << " Can't back up over more bytes than were returned by the last call" + " to Next()."; buffer_used_ -= count; } @@ -394,6 +376,63 @@ void CopyingOutputStreamAdaptor::FreeBuffer() { 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 LimitingInputStream::ByteCount() const { + if (limit_ < 0) { + return input_->ByteCount() + limit_ - prior_bytes_read_; + } else { + return input_->ByteCount() - prior_bytes_read_; + } +} + + // =================================================================== } // namespace io diff --git a/src/google/protobuf/io/zero_copy_stream_impl_lite.h b/src/google/protobuf/io/zero_copy_stream_impl_lite.h index da4ef4556f..ebb20176e3 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl_lite.h +++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.h @@ -44,15 +44,17 @@ #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 #include +#include + namespace google { namespace protobuf { namespace io { @@ -60,7 +62,7 @@ namespace io { // =================================================================== // A ZeroCopyInputStream backed by an in-memory array of bytes. -class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream { +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 @@ -85,8 +87,8 @@ class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream { 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). + int last_returned_size_; // How many bytes we returned last time Next() + // was called (used for error checking only). GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream); }; @@ -94,7 +96,7 @@ class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream { // =================================================================== // A ZeroCopyOutputStream backed by an in-memory array of bytes. -class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream { +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 @@ -112,13 +114,13 @@ class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream { int64 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. + 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). + int last_returned_size_; // How many bytes we returned last time Next() + // was called (used for error checking only). GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream); }; @@ -126,7 +128,7 @@ class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream { // =================================================================== // A ZeroCopyOutputStream which appends bytes to a string. -class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream { +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 @@ -137,7 +139,7 @@ class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream { // 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(string* target); + explicit StringOutputStream(std::string* target); ~StringOutputStream() override = default; // implements ZeroCopyOutputStream --------------------------------- @@ -145,13 +147,10 @@ class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream { void BackUp(int count) override; int64 ByteCount() const override; - protected: - void SetString(string* target); - private: static const int kMinimumSize = 16; - string* target_; + std::string* target_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOutputStream); }; @@ -173,7 +172,7 @@ class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream { // CopyingInputStream implementations should avoid buffering if possible. // CopyingInputStreamAdaptor does its own buffering and will read data // in large blocks. -class LIBPROTOBUF_EXPORT CopyingInputStream { +class PROTOBUF_EXPORT CopyingInputStream { public: virtual ~CopyingInputStream() {} @@ -199,7 +198,7 @@ class LIBPROTOBUF_EXPORT CopyingInputStream { // If you want to read from file descriptors or C++ istreams, this is // already implemented for you: use FileInputStream or IstreamInputStream // respectively. -class LIBPROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream { +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 @@ -267,7 +266,7 @@ class LIBPROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream // CopyingOutputStream implementations should avoid buffering if possible. // CopyingOutputStreamAdaptor does its own buffering and will write data // in large blocks. -class LIBPROTOBUF_EXPORT CopyingOutputStream { +class PROTOBUF_EXPORT CopyingOutputStream { public: virtual ~CopyingOutputStream() {} @@ -283,7 +282,7 @@ class LIBPROTOBUF_EXPORT CopyingOutputStream { // If you want to write to file descriptors or C++ ostreams, this is // already implemented for you: use FileOutputStream or OstreamOutputStream // respectively. -class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream { +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 @@ -339,6 +338,31 @@ class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStrea 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 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 @@ -356,31 +380,25 @@ class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStrea // 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(string* s) { -#ifdef LANG_CXX11 +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]; -#else - return string_as_array(s); -#endif } // 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(string* s) { - char *p = mutable_string_data(s); -#ifdef LANG_CXX11 +inline std::pair as_string_data(std::string* s) { + char* p = mutable_string_data(s); return std::make_pair(p, true); -#else - return std::make_pair(p, p != NULL); -#endif } } // namespace io } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__ diff --git a/src/google/protobuf/io/zero_copy_stream_unittest.cc b/src/google/protobuf/io/zero_copy_stream_unittest.cc index 4813206430..5f7b6104b9 100644 --- a/src/google/protobuf/io/zero_copy_stream_unittest.cc +++ b/src/google/protobuf/io/zero_copy_stream_unittest.cc @@ -50,16 +50,17 @@ #ifndef _MSC_VER #include #endif +#include +#include #include -#include #include -#include -#include +#include #include #include #include #include +#include #include #include @@ -69,10 +70,9 @@ #include #include -#include #include +#include #include -#include namespace google { namespace protobuf { @@ -83,17 +83,17 @@ namespace { #define pipe(fds) _pipe(fds, 4096, O_BINARY) // DO NOT include , instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::mkdir; -using google::protobuf::internal::win32::open; -using google::protobuf::internal::win32::close; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::mkdir; +using google::protobuf::io::win32::open; #endif #ifndef O_BINARY #ifdef _O_BINARY #define O_BINARY _O_BINARY #else -#define O_BINARY 0 // If this isn't defined, the platform doesn't need it. +#define O_BINARY 0 // If this isn't defined, the platform doesn't need it. #endif #endif @@ -106,10 +106,10 @@ class IoTest : public testing::Test { // Helper to read a fixed-length array of data from an input stream. int ReadFromInput(ZeroCopyInputStream* input, void* data, int size); // Write a string to the output stream. - void WriteString(ZeroCopyOutputStream* output, const string& str); + void WriteString(ZeroCopyOutputStream* output, const std::string& str); // Read a number of bytes equal to the size of the given string and checks // that it matches the string. - void ReadString(ZeroCopyInputStream* input, const string& str); + void ReadString(ZeroCopyInputStream* input, const std::string& str); // Writes some text to the output stream in a particular order. Returns // the number of bytes written, incase the caller needs that to set up an // input stream. @@ -125,8 +125,9 @@ class IoTest : public testing::Test { void ReadStuffLarge(ZeroCopyInputStream* input); #if HAVE_ZLIB - string Compress(const string& data, const GzipOutputStream::Options& options); - string Uncompress(const string& data); + std::string Compress(const std::string& data, + const GzipOutputStream::Options& options); + std::string Uncompress(const std::string& data); #endif static const int kBlockSizes[]; @@ -136,8 +137,8 @@ class IoTest : public testing::Test { const int IoTest::kBlockSizes[] = {-1, 1, 2, 5, 7, 10, 23, 64}; const int IoTest::kBlockSizeCount = GOOGLE_ARRAYSIZE(IoTest::kBlockSizes); -bool IoTest::WriteToOutput(ZeroCopyOutputStream* output, - const void* data, int size) { +bool IoTest::WriteToOutput(ZeroCopyOutputStream* output, const void* data, + int size) { const uint8* in = reinterpret_cast(data); int in_size = size; @@ -199,11 +200,11 @@ int IoTest::ReadFromInput(ZeroCopyInputStream* input, void* data, int size) { } } -void IoTest::WriteString(ZeroCopyOutputStream* output, const string& str) { +void IoTest::WriteString(ZeroCopyOutputStream* output, const std::string& str) { EXPECT_TRUE(WriteToOutput(output, str.c_str(), str.size())); } -void IoTest::ReadString(ZeroCopyInputStream* input, const string& str) { +void IoTest::ReadString(ZeroCopyInputStream* input, const std::string& str) { std::unique_ptr buffer(new char[str.size() + 1]); buffer[str.size()] = '\0'; EXPECT_EQ(ReadFromInput(input, buffer.get(), str.size()), str.size()); @@ -246,8 +247,8 @@ int IoTest::WriteStuffLarge(ZeroCopyOutputStream* output) { WriteString(output, "Hello world!\n"); WriteString(output, "Some te"); WriteString(output, "xt. Blah blah."); - WriteString(output, string(100000, 'x')); // A very long string - WriteString(output, string(100000, 'y')); // A very long string + WriteString(output, std::string(100000, 'x')); // A very long string + WriteString(output, std::string(100000, 'y')); // A very long string WriteString(output, "01234567890123456789"); EXPECT_EQ(output->ByteCount(), 200055); @@ -263,7 +264,7 @@ void IoTest::ReadStuffLarge(ZeroCopyInputStream* input) { EXPECT_TRUE(input->Skip(5)); ReadString(input, "blah."); EXPECT_TRUE(input->Skip(100000 - 10)); - ReadString(input, string(10, 'x') + string(100000 - 20000, 'y')); + ReadString(input, std::string(10, 'x') + std::string(100000 - 20000, 'y')); EXPECT_TRUE(input->Skip(20000 - 10)); ReadString(input, "yyyyyyyyyy01234567890123456789"); @@ -299,7 +300,7 @@ TEST_F(IoTest, TwoSessionWrite) { static const char* strA = "0123456789"; static const char* strB = "WhirledPeas"; - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; char* temp_buffer = new char[40]; @@ -313,8 +314,8 @@ TEST_F(IoTest, TwoSessionWrite) { delete coded_output; // flush int64 pos = output->ByteCount(); delete output; - output = new ArrayOutputStream( - buffer + pos, kBufferSize - pos, kBlockSizes[i]); + output = new ArrayOutputStream(buffer + pos, kBufferSize - pos, + kBlockSizes[i]); coded_output = new CodedOutputStream(output); coded_output->WriteVarint32(strlen(strB)); coded_output->WriteRaw(strB, strlen(strB)); @@ -341,13 +342,13 @@ TEST_F(IoTest, TwoSessionWrite) { } } - delete [] temp_buffer; - delete [] buffer; + delete[] temp_buffer; + delete[] buffer; } #if HAVE_ZLIB TEST_F(IoTest, GzipIo) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; for (int i = 0; i < kBlockSizeCount; i++) { for (int j = 0; j < kBlockSizeCount; j++) { @@ -368,18 +369,17 @@ TEST_F(IoTest, GzipIo) { } { ArrayInputStream input(buffer, size, kBlockSizes[j]); - GzipInputStream gzin( - &input, GzipInputStream::GZIP, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::GZIP, gzip_buffer_size); ReadStuff(&gzin); } } } } - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, GzipIoWithFlush) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; // We start with i = 4 as we want a block size > 6. With block size <= 6 // Flush() fills up the entire 2K buffer with flush markers and the test @@ -404,18 +404,17 @@ TEST_F(IoTest, GzipIoWithFlush) { } { ArrayInputStream input(buffer, size, kBlockSizes[j]); - GzipInputStream gzin( - &input, GzipInputStream::GZIP, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::GZIP, gzip_buffer_size); ReadStuff(&gzin); } } } } - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, GzipIoContiguousFlushes) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; int block_size = kBlockSizes[4]; @@ -436,15 +435,14 @@ TEST_F(IoTest, GzipIoContiguousFlushes) { size = output.ByteCount(); ArrayInputStream input(buffer, size, block_size); - GzipInputStream gzin( - &input, GzipInputStream::GZIP, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::GZIP, gzip_buffer_size); ReadStuff(&gzin); - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, GzipIoReadAfterFlush) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; int block_size = kBlockSizes[4]; @@ -463,17 +461,16 @@ TEST_F(IoTest, GzipIoReadAfterFlush) { size = output.ByteCount(); ArrayInputStream input(buffer, size, block_size); - GzipInputStream gzin( - &input, GzipInputStream::GZIP, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::GZIP, gzip_buffer_size); ReadStuff(&gzin); gzout.Close(); - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, ZlibIo) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; for (int i = 0; i < kBlockSizeCount; i++) { for (int j = 0; j < kBlockSizeCount; j++) { @@ -494,18 +491,17 @@ TEST_F(IoTest, ZlibIo) { } { ArrayInputStream input(buffer, size, kBlockSizes[j]); - GzipInputStream gzin( - &input, GzipInputStream::ZLIB, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::ZLIB, gzip_buffer_size); ReadStuff(&gzin); } } } } - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, ZlibIoInputAutodetect) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; int size; { @@ -536,12 +532,12 @@ TEST_F(IoTest, ZlibIoInputAutodetect) { GzipInputStream gzin(&input, GzipInputStream::AUTO); ReadStuff(&gzin); } - delete [] buffer; + delete[] buffer; } -string IoTest::Compress(const string& data, - const GzipOutputStream::Options& options) { - string result; +std::string IoTest::Compress(const std::string& data, + const GzipOutputStream::Options& options) { + std::string result; { StringOutputStream output(&result); GzipOutputStream gzout(&output, options); @@ -550,8 +546,8 @@ string IoTest::Compress(const string& data, return result; } -string IoTest::Uncompress(const string& data) { - string result; +std::string IoTest::Uncompress(const std::string& data) { + std::string result; { ArrayInputStream input(data.data(), data.size()); GzipInputStream gzin(&input); @@ -567,21 +563,21 @@ string IoTest::Uncompress(const string& data) { TEST_F(IoTest, CompressionOptions) { // Some ad-hoc testing of compression options. - string golden_filename = + std::string golden_filename = TestUtil::GetTestDataPath("net/proto2/internal/testdata/golden_message"); - string golden; + std::string golden; GOOGLE_CHECK_OK(File::GetContents(golden_filename, &golden, true)); GzipOutputStream::Options options; - string gzip_compressed = Compress(golden, options); + std::string gzip_compressed = Compress(golden, options); options.compression_level = 0; - string not_compressed = Compress(golden, options); + std::string not_compressed = Compress(golden, options); // Try zlib compression for fun. options = GzipOutputStream::Options(); options.format = GzipOutputStream::ZLIB; - string zlib_compressed = Compress(golden, options); + std::string zlib_compressed = Compress(golden, options); // Uncompressed should be bigger than the original since it should have some // sort of header. @@ -604,7 +600,7 @@ TEST_F(IoTest, TwoSessionWriteGzip) { static const char* strA = "0123456789"; static const char* strB = "QuickBrownFox"; - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; char* temp_buffer = new char[40]; @@ -618,18 +614,18 @@ TEST_F(IoTest, TwoSessionWriteGzip) { coded_output->WriteVarint32(outlen); coded_output->WriteRaw(strA, outlen); delete coded_output; // flush - delete gzout; // flush + delete gzout; // flush int64 pos = output->ByteCount(); delete output; - output = new ArrayOutputStream( - buffer + pos, kBufferSize - pos, kBlockSizes[i]); + output = new ArrayOutputStream(buffer + pos, kBufferSize - pos, + kBlockSizes[i]); gzout = new GzipOutputStream(output); coded_output = new CodedOutputStream(gzout); outlen = strlen(strB) + 1; coded_output->WriteVarint32(outlen); coded_output->WriteRaw(strB, outlen); delete coded_output; // flush - delete gzout; // flush + delete gzout; // flush int64 size = pos + output->ByteCount(); delete output; @@ -649,10 +645,8 @@ TEST_F(IoTest, TwoSessionWriteGzip) { EXPECT_TRUE(coded_input->ReadRaw(temp_buffer, insize)); EXPECT_EQ(0, memcmp(temp_buffer, strB, insize)) << " out_block_size=" << kBlockSizes[i] - << " in_block_size=" << kBlockSizes[j] - << " pos=" << pos - << " size=" << size - << " strB=" << strB << " in=" << temp_buffer; + << " in_block_size=" << kBlockSizes[j] << " pos=" << pos + << " size=" << size << " strB=" << strB << " in=" << temp_buffer; delete coded_input; delete gzin; @@ -660,13 +654,13 @@ TEST_F(IoTest, TwoSessionWriteGzip) { } } - delete [] temp_buffer; - delete [] buffer; + delete[] temp_buffer; + delete[] buffer; } TEST_F(IoTest, GzipInputByteCountAfterClosed) { - string golden = "abcdefghijklmnopqrstuvwxyz"; - string compressed = Compress(golden, GzipOutputStream::Options()); + std::string golden = "abcdefghijklmnopqrstuvwxyz"; + std::string compressed = Compress(golden, GzipOutputStream::Options()); for (int i = 0; i < kBlockSizeCount; i++) { ArrayInputStream arr_input(compressed.data(), compressed.size(), @@ -682,11 +676,11 @@ TEST_F(IoTest, GzipInputByteCountAfterClosed) { } TEST_F(IoTest, GzipInputByteCountAfterClosedConcatenatedStreams) { - string golden1 = "abcdefghijklmnopqrstuvwxyz"; - string golden2 = "the quick brown fox jumps over the lazy dog"; + std::string golden1 = "abcdefghijklmnopqrstuvwxyz"; + std::string golden2 = "the quick brown fox jumps over the lazy dog"; const size_t total_size = golden1.size() + golden2.size(); - string compressed = Compress(golden1, GzipOutputStream::Options()) + - Compress(golden2, GzipOutputStream::Options()); + std::string compressed = Compress(golden1, GzipOutputStream::Options()) + + Compress(golden2, GzipOutputStream::Options()); for (int i = 0; i < kBlockSizeCount; i++) { ArrayInputStream arr_input(compressed.data(), compressed.size(), @@ -706,7 +700,7 @@ TEST_F(IoTest, GzipInputByteCountAfterClosedConcatenatedStreams) { // explicit block sizes. So, we'll only run one test and we'll use // ArrayInput to read back the results. TEST_F(IoTest, StringIo) { - string str; + std::string str; { StringOutputStream output(&str); WriteStuff(&output); @@ -720,13 +714,13 @@ TEST_F(IoTest, StringIo) { // To test files, we create a temporary file, write, read, truncate, repeat. TEST_F(IoTest, FileIo) { - string filename = TestTempDir() + "/zero_copy_stream_test_file"; + std::string filename = TestTempDir() + "/zero_copy_stream_test_file"; for (int i = 0; i < kBlockSizeCount; i++) { for (int j = 0; j < kBlockSizeCount; j++) { // Make a temporary file. int file = - open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0777); + open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0777); ASSERT_GE(file, 0); { @@ -751,13 +745,13 @@ TEST_F(IoTest, FileIo) { #if HAVE_ZLIB TEST_F(IoTest, GzipFileIo) { - string filename = TestTempDir() + "/zero_copy_stream_test_file"; + std::string filename = TestTempDir() + "/zero_copy_stream_test_file"; for (int i = 0; i < kBlockSizeCount; i++) { for (int j = 0; j < kBlockSizeCount; j++) { // Make a temporary file. int file = - open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0777); + open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0777); ASSERT_GE(file, 0); { FileOutputStream output(file, kBlockSizes[i]); @@ -799,10 +793,8 @@ class MsvcDebugDisabler { old_mode_ = _CrtSetReportMode(_CRT_ASSERT, old_mode_); } - static void MyHandler(const wchar_t *expr, - const wchar_t *func, - const wchar_t *file, - unsigned int line, + static void MyHandler(const wchar_t* expr, const wchar_t* func, + const wchar_t* file, unsigned int line, uintptr_t pReserved) { // do nothing } @@ -930,19 +922,19 @@ TEST_F(IoTest, ConcatenatingInputStream) { // Now split it up into multiple streams of varying sizes. ASSERT_EQ(68, output.ByteCount()); // Test depends on this. - ArrayInputStream input1(buffer , 12); - ArrayInputStream input2(buffer + 12, 7); - ArrayInputStream input3(buffer + 19, 6); + ArrayInputStream input1(buffer, 12); + ArrayInputStream input2(buffer + 12, 7); + ArrayInputStream input3(buffer + 19, 6); ArrayInputStream input4(buffer + 25, 15); - ArrayInputStream input5(buffer + 40, 0); + ArrayInputStream input5(buffer + 40, 0); // Note: We want to make sure we have a stream boundary somewhere between // bytes 42 and 62, which is the range that it Skip()ed by ReadStuff(). This // tests that a bug that existed in the original code for Skip() is fixed. ArrayInputStream input6(buffer + 40, 10); ArrayInputStream input7(buffer + 50, 18); // Total = 68 bytes. - ZeroCopyInputStream* streams[] = - {&input1, &input2, &input3, &input4, &input5, &input6, &input7}; + ZeroCopyInputStream* streams[] = {&input1, &input2, &input3, &input4, + &input5, &input6, &input7}; // Create the concatenating stream and read. ConcatenatingInputStream input(streams, GOOGLE_ARRAYSIZE(streams)); @@ -985,7 +977,7 @@ TEST_F(IoTest, LimitingInputStreamByteCount) { LimitingInputStream input(&array_input, kHalfBufferSize - 1); EXPECT_EQ(0, input.ByteCount()); EXPECT_TRUE(input.Next(&data, &size)); - EXPECT_EQ(kHalfBufferSize - 1 , input.ByteCount()); + EXPECT_EQ(kHalfBufferSize - 1, input.ByteCount()); } // Check that a zero-size array doesn't confuse the code. diff --git a/src/google/protobuf/lite_arena_unittest.cc b/src/google/protobuf/lite_arena_unittest.cc index df88d2cc93..d68c6c9bbe 100644 --- a/src/google/protobuf/lite_arena_unittest.cc +++ b/src/google/protobuf/lite_arena_unittest.cc @@ -52,7 +52,7 @@ class LiteArenaTest : public testing::Test { }; TEST_F(LiteArenaTest, MapNoHeapAllocation) { - string data; + std::string data; data.reserve(128 * 1024); { @@ -76,7 +76,7 @@ TEST_F(LiteArenaTest, UnknownFieldMemLeak) { protobuf_unittest::ForeignMessageArenaLite* message = Arena::CreateMessage( arena_.get()); - string data = "\012\000"; + std::string data = "\012\000"; int original_capacity = data.capacity(); while (data.capacity() <= original_capacity) { data.append("a"); diff --git a/src/google/protobuf/lite_unittest.cc b/src/google/protobuf/lite_unittest.cc index ee44f51b19..ccaea13c33 100644 --- a/src/google/protobuf/lite_unittest.cc +++ b/src/google/protobuf/lite_unittest.cc @@ -30,8 +30,8 @@ // Author: kenton@google.com (Kenton Varda) -#include #include +#include #include #include @@ -43,13 +43,9 @@ #include #include #include -#include #include - #include -using std::string; - namespace google { namespace protobuf { @@ -60,10 +56,9 @@ void ExpectMessageMerged(const unittest::TestAllTypesLite& message) { EXPECT_EQ(message.optional_string(), "hello"); } -void AssignParsingMergeMessages( - unittest::TestAllTypesLite* msg1, - unittest::TestAllTypesLite* msg2, - unittest::TestAllTypesLite* msg3) { +void AssignParsingMergeMessages(unittest::TestAllTypesLite* msg1, + unittest::TestAllTypesLite* msg2, + unittest::TestAllTypesLite* msg3) { msg1->set_optional_int32(1); msg2->set_optional_int64(2); msg3->set_optional_int32(3); @@ -75,7 +70,7 @@ void SetAllTypesInEmptyMessageUnknownFields( protobuf_unittest::TestAllTypesLite message; TestUtilLite::ExpectClear(message); TestUtilLite::SetAllFields(&message); - string data = message.SerializeAsString(); + std::string data = message.SerializeAsString(); empty_message->ParseFromString(data); } @@ -87,12 +82,12 @@ void SetSomeTypesInEmptyMessageUnknownFields( message.set_optional_int64(102); message.set_optional_uint32(103); message.set_optional_uint64(104); - string data = message.SerializeAsString(); + std::string data = message.SerializeAsString(); empty_message->ParseFromString(data); } TEST(Lite, AllLite1) { - string data; + std::string data; { protobuf_unittest::TestAllTypesLite message, message2, message3; @@ -112,13 +107,13 @@ TEST(Lite, AllLite1) { } TEST(Lite, AllLite2) { - string data; + std::string data; { protobuf_unittest::TestAllExtensionsLite message, message2, message3; TestUtilLite::ExpectExtensionsClear(message); TestUtilLite::SetAllExtensions(&message); message2.CopyFrom(message); - string extensions_data = message.SerializeAsString(); + std::string extensions_data = message.SerializeAsString(); message3.ParseFromString(extensions_data); TestUtilLite::ExpectAllExtensionsSet(message); TestUtilLite::ExpectAllExtensionsSet(message2); @@ -131,7 +126,7 @@ TEST(Lite, AllLite2) { } TEST(Lite, AllLite3) { - string data, packed_data; + std::string data, packed_data; { protobuf_unittest::TestPackedTypesLite message, message2, message3; @@ -154,7 +149,7 @@ TEST(Lite, AllLite3) { TestUtilLite::ExpectPackedExtensionsClear(message); TestUtilLite::SetPackedExtensions(&message); message2.CopyFrom(message); - string packed_extensions_data = message.SerializeAsString(); + std::string packed_extensions_data = message.SerializeAsString(); EXPECT_EQ(packed_extensions_data, packed_data); message3.ParseFromString(packed_extensions_data); TestUtilLite::ExpectPackedExtensionsSet(message); @@ -168,7 +163,7 @@ TEST(Lite, AllLite3) { } TEST(Lite, AllLite5) { - string data; + std::string data; { // Test that if an optional or required message/group field appears multiple @@ -178,10 +173,10 @@ TEST(Lite, AllLite5) { unittest::TestAllTypesLite* msg2; unittest::TestAllTypesLite* msg3; -#define ASSIGN_REPEATED_FIELD(FIELD) \ - msg1 = generator.add_##FIELD(); \ - msg2 = generator.add_##FIELD(); \ - msg3 = generator.add_##FIELD(); \ +#define ASSIGN_REPEATED_FIELD(FIELD) \ + msg1 = generator.add_##FIELD(); \ + msg2 = generator.add_##FIELD(); \ + msg3 = generator.add_##FIELD(); \ AssignParsingMergeMessages(msg1, msg2, msg3) ASSIGN_REPEATED_FIELD(field1); @@ -202,7 +197,7 @@ TEST(Lite, AllLite5) { #undef ASSIGN_REPEATED_GROUP - string buffer; + std::string buffer; generator.SerializeToString(&buffer); unittest::TestParsingMergeLite parsing_merge; parsing_merge.ParseFromString(buffer); @@ -225,7 +220,7 @@ TEST(Lite, AllLite5) { } TEST(Lite, AllLite6) { - string data; + std::string data; // Test unknown fields support for lite messages. { @@ -234,10 +229,10 @@ TEST(Lite, AllLite6) { TestUtilLite::ExpectClear(message); TestUtilLite::SetAllFields(&message); data = message.SerializeAsString(); - empty_message.ParseFromString(data); + ASSERT_TRUE(empty_message.ParseFromString(data)); data.clear(); data = empty_message.SerializeAsString(); - message2.ParseFromString(data); + EXPECT_TRUE(message2.ParseFromString(data)); data = message2.SerializeAsString(); TestUtilLite::ExpectAllFieldsSet(message2); message.Clear(); @@ -246,7 +241,7 @@ TEST(Lite, AllLite6) { } TEST(Lite, AllLite7) { - string data; + std::string data; { protobuf_unittest::TestAllExtensionsLite message, message2; @@ -266,7 +261,7 @@ TEST(Lite, AllLite7) { } TEST(Lite, AllLite8) { - string data; + std::string data; { protobuf_unittest::TestPackedTypesLite message, message2; @@ -286,7 +281,7 @@ TEST(Lite, AllLite8) { } TEST(Lite, AllLite9) { - string data; + std::string data; { protobuf_unittest::TestPackedExtensionsLite message, message2; @@ -306,7 +301,7 @@ TEST(Lite, AllLite9) { } TEST(Lite, AllLite10) { - string data; + std::string data; { // Test Unknown fields swap @@ -314,7 +309,7 @@ TEST(Lite, AllLite10) { SetAllTypesInEmptyMessageUnknownFields(&empty_message); SetSomeTypesInEmptyMessageUnknownFields(&empty_message2); data = empty_message.SerializeAsString(); - string data2 = empty_message2.SerializeAsString(); + std::string data2 = empty_message2.SerializeAsString(); empty_message.Swap(&empty_message2); EXPECT_EQ(data, empty_message2.SerializeAsString()); EXPECT_EQ(data2, empty_message.SerializeAsString()); @@ -322,7 +317,7 @@ TEST(Lite, AllLite10) { } TEST(Lite, AllLite11) { - string data; + std::string data; { // Test unknown fields swap with self @@ -335,7 +330,7 @@ TEST(Lite, AllLite11) { } TEST(Lite, AllLite12) { - string data; + std::string data; { // Test MergeFrom with unknown fields @@ -365,12 +360,12 @@ TEST(Lite, AllLite12) { } TEST(Lite, AllLite13) { - string data; + std::string data; { // Test unknown enum value protobuf_unittest::TestAllTypesLite message; - string buffer; + std::string buffer; { io::StringOutputStream output_stream(&buffer); io::CodedOutputStream coded_output(&output_stream); @@ -390,7 +385,7 @@ TEST(Lite, AllLite13) { } TEST(Lite, AllLite14) { - string data; + std::string data; { // Test Clear with unknown fields @@ -404,7 +399,7 @@ TEST(Lite, AllLite14) { // Tests for map lite ============================================= TEST(Lite, AllLite15) { - string data; + std::string data; { // Accessors @@ -419,7 +414,7 @@ TEST(Lite, AllLite15) { } TEST(Lite, AllLite16) { - string data; + std::string data; { // SetMapFieldsInitialized @@ -431,7 +426,7 @@ TEST(Lite, AllLite16) { } TEST(Lite, AllLite17) { - string data; + std::string data; { // Clear @@ -444,20 +439,19 @@ TEST(Lite, AllLite17) { } TEST(Lite, AllLite18) { - string data; + std::string data; { // ClearMessageMap protobuf_unittest::TestMessageMapLite message; // Creates a TestAllTypes with default value - TestUtilLite::ExpectClear( - (*message.mutable_map_int32_message())[0]); + TestUtilLite::ExpectClear((*message.mutable_map_int32_message())[0]); } } TEST(Lite, AllLite19) { - string data; + std::string data; { // CopyFrom @@ -474,7 +468,7 @@ TEST(Lite, AllLite19) { } TEST(Lite, AllLite20) { - string data; + std::string data; { // CopyFromMessageMap @@ -492,7 +486,7 @@ TEST(Lite, AllLite20) { } TEST(Lite, AllLite21) { - string data; + std::string data; { // SwapWithEmpty @@ -509,7 +503,7 @@ TEST(Lite, AllLite21) { } TEST(Lite, AllLite22) { - string data; + std::string data; { // SwapWithSelf @@ -524,7 +518,7 @@ TEST(Lite, AllLite22) { } TEST(Lite, AllLite23) { - string data; + std::string data; { // SwapWithOther @@ -541,7 +535,7 @@ TEST(Lite, AllLite23) { } TEST(Lite, AllLite24) { - string data; + std::string data; { // CopyConstructor @@ -554,7 +548,7 @@ TEST(Lite, AllLite24) { } TEST(Lite, AllLite25) { - string data; + std::string data; { // CopyAssignmentOperator @@ -572,7 +566,7 @@ TEST(Lite, AllLite25) { } TEST(Lite, AllLite26) { - string data; + std::string data; { // NonEmptyMergeFrom @@ -594,7 +588,7 @@ TEST(Lite, AllLite26) { } TEST(Lite, AllLite27) { - string data; + std::string data; { // MergeFromMessageMap @@ -612,12 +606,12 @@ TEST(Lite, AllLite27) { } TEST(Lite, AllLite28) { - string data; + std::string data; { // Test the generated SerializeWithCachedSizesToArray() protobuf_unittest::TestMapLite message1, message2; - string data; + std::string data; MapLiteTestUtil::SetMapFields(&message1); int size = message1.ByteSize(); data.resize(size); @@ -630,14 +624,14 @@ TEST(Lite, AllLite28) { } TEST(Lite, AllLite29) { - string data; + std::string data; { // Test the generated SerializeWithCachedSizes() protobuf_unittest::TestMapLite message1, message2; MapLiteTestUtil::SetMapFields(&message1); int size = message1.ByteSize(); - string data; + std::string data; data.resize(size); { // Allow the output stream to buffer only one byte at a time. @@ -654,7 +648,7 @@ TEST(Lite, AllLite29) { TEST(Lite, AllLite32) { - string data; + std::string data; { // Proto2UnknownEnum @@ -663,14 +657,14 @@ TEST(Lite, AllLite32) { protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE; (*from.mutable_unknown_map_field())[0] = protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE; - string data; + std::string data; from.SerializeToString(&data); protobuf_unittest::TestEnumMapLite to; EXPECT_TRUE(to.ParseFromString(data)); EXPECT_EQ(0, to.unknown_map_field().size()); EXPECT_FALSE(to.mutable_unknown_fields()->empty()); - EXPECT_EQ(1, to.known_map_field().size()); + ASSERT_EQ(1, to.known_map_field().size()); EXPECT_EQ(protobuf_unittest::PROTO2_MAP_ENUM_FOO_LITE, to.known_map_field().at(0)); @@ -678,141 +672,147 @@ TEST(Lite, AllLite32) { from.Clear(); to.SerializeToString(&data); EXPECT_TRUE(from.ParseFromString(data)); - EXPECT_EQ(1, from.known_map_field().size()); + ASSERT_EQ(1, from.known_map_field().size()); EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE, from.known_map_field().at(0)); - EXPECT_EQ(1, from.unknown_map_field().size()); + ASSERT_EQ(1, from.unknown_map_field().size()); EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE, from.unknown_map_field().at(0)); } } TEST(Lite, AllLite33) { - string data; + std::string data; { // StandardWireFormat protobuf_unittest::TestMapLite message; - string data = "\x0A\x04\x08\x01\x10\x01"; + std::string data = "\x0A\x04\x08\x01\x10\x01"; EXPECT_TRUE(message.ParseFromString(data)); - EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_EQ(1, message.map_int32_int32().size()); EXPECT_EQ(1, message.map_int32_int32().at(1)); } } TEST(Lite, AllLite34) { - string data; + std::string data; { // UnorderedWireFormat protobuf_unittest::TestMapLite message; // put value before key in wire format - string data = "\x0A\x04\x10\x01\x08\x02"; + std::string data = "\x0A\x04\x10\x01\x08\x02"; EXPECT_TRUE(message.ParseFromString(data)); - EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_EQ(1, message.map_int32_int32().size()); + ASSERT_NE(message.map_int32_int32().find(2), + message.map_int32_int32().end()); EXPECT_EQ(1, message.map_int32_int32().at(2)); } } TEST(Lite, AllLite35) { - string data; + std::string data; { // DuplicatedKeyWireFormat protobuf_unittest::TestMapLite message; // Two key fields in wire format - string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; + std::string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; EXPECT_TRUE(message.ParseFromString(data)); - EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_EQ(1, message.map_int32_int32().size()); EXPECT_EQ(1, message.map_int32_int32().at(2)); } } TEST(Lite, AllLite36) { - string data; + std::string data; { // DuplicatedValueWireFormat protobuf_unittest::TestMapLite message; // Two value fields in wire format - string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; + std::string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; EXPECT_TRUE(message.ParseFromString(data)); - EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_EQ(1, message.map_int32_int32().size()); EXPECT_EQ(2, message.map_int32_int32().at(1)); } } TEST(Lite, AllLite37) { - string data; + std::string data; { // MissedKeyWireFormat protobuf_unittest::TestMapLite message; // No key field in wire format - string data = "\x0A\x02\x10\x01"; + std::string data = "\x0A\x02\x10\x01"; EXPECT_TRUE(message.ParseFromString(data)); - EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_EQ(1, message.map_int32_int32().size()); + ASSERT_NE(message.map_int32_int32().find(0), + message.map_int32_int32().end()); EXPECT_EQ(1, message.map_int32_int32().at(0)); } } TEST(Lite, AllLite38) { - string data; + std::string data; { // MissedValueWireFormat protobuf_unittest::TestMapLite message; // No value field in wire format - string data = "\x0A\x02\x08\x01"; + std::string data = "\x0A\x02\x08\x01"; EXPECT_TRUE(message.ParseFromString(data)); - EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_EQ(1, message.map_int32_int32().size()); + ASSERT_NE(message.map_int32_int32().find(1), + message.map_int32_int32().end()); EXPECT_EQ(0, message.map_int32_int32().at(1)); } } TEST(Lite, AllLite39) { - string data; + std::string data; { // UnknownFieldWireFormat protobuf_unittest::TestMapLite message; // Unknown field in wire format - string data = "\x0A\x06\x08\x02\x10\x03\x18\x01"; + std::string data = "\x0A\x06\x08\x02\x10\x03\x18\x01"; EXPECT_TRUE(message.ParseFromString(data)); - EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_EQ(1, message.map_int32_int32().size()); EXPECT_EQ(3, message.map_int32_int32().at(2)); } } TEST(Lite, AllLite40) { - string data; + std::string data; { // CorruptedWireFormat protobuf_unittest::TestMapLite message; // corrupted data in wire format - string data = "\x0A\x06\x08\x02\x11\x03"; + std::string data = "\x0A\x06\x08\x02\x11\x03"; EXPECT_FALSE(message.ParseFromString(data)); } } TEST(Lite, AllLite41) { - string data; + std::string data; { // IsInitialized @@ -831,33 +831,33 @@ TEST(Lite, AllLite41) { } TEST(Lite, AllLite42) { - string data; + std::string data; { - // Check that adding more values to enum does not corrupt message - // when passed through an old client. - protobuf_unittest::V2MessageLite v2_message; - v2_message.set_int_field(800); - // Set enum field to the value not understood by the old client. - v2_message.set_enum_field(protobuf_unittest::V2_SECOND); - string v2_bytes = v2_message.SerializeAsString(); - - protobuf_unittest::V1MessageLite v1_message; - v1_message.ParseFromString(v2_bytes); - EXPECT_TRUE(v1_message.IsInitialized()); - EXPECT_EQ(v1_message.int_field(), v2_message.int_field()); - // V1 client does not understand V2_SECOND value, so it discards it and - // uses default value instead. - EXPECT_EQ(v1_message.enum_field(), protobuf_unittest::V1_FIRST); - - // However, when re-serialized, it should preserve enum value. - string v1_bytes = v1_message.SerializeAsString(); - - protobuf_unittest::V2MessageLite same_v2_message; - same_v2_message.ParseFromString(v1_bytes); - - EXPECT_EQ(v2_message.int_field(), same_v2_message.int_field()); - EXPECT_EQ(v2_message.enum_field(), same_v2_message.enum_field()); + // Check that adding more values to enum does not corrupt message + // when passed through an old client. + protobuf_unittest::V2MessageLite v2_message; + v2_message.set_int_field(800); + // Set enum field to the value not understood by the old client. + v2_message.set_enum_field(protobuf_unittest::V2_SECOND); + std::string v2_bytes = v2_message.SerializeAsString(); + + protobuf_unittest::V1MessageLite v1_message; + v1_message.ParseFromString(v2_bytes); + EXPECT_TRUE(v1_message.IsInitialized()); + EXPECT_EQ(v1_message.int_field(), v2_message.int_field()); + // V1 client does not understand V2_SECOND value, so it discards it and + // uses default value instead. + EXPECT_EQ(v1_message.enum_field(), protobuf_unittest::V1_FIRST); + + // However, when re-serialized, it should preserve enum value. + std::string v1_bytes = v1_message.SerializeAsString(); + + protobuf_unittest::V2MessageLite same_v2_message; + same_v2_message.ParseFromString(v1_bytes); + + EXPECT_EQ(v2_message.int_field(), same_v2_message.int_field()); + EXPECT_EQ(v2_message.enum_field(), same_v2_message.enum_field()); } } @@ -867,7 +867,7 @@ TEST(Lite, AllLite43) { protobuf_unittest::TestOneofParsingLite message1; message1.set_oneof_int32(17); - string serialized; + std::string serialized; EXPECT_TRUE(message1.SerializeToString(&serialized)); // Submessage @@ -909,7 +909,7 @@ TEST(Lite, AllLite44) { { protobuf_unittest::TestOneofParsingLite original; original.set_oneof_int32(17); - string serialized; + std::string serialized; EXPECT_TRUE(original.SerializeToString(&serialized)); protobuf_unittest::TestOneofParsingLite parsed; for (int i = 0; i < 2; ++i) { @@ -925,7 +925,7 @@ TEST(Lite, AllLite44) { { protobuf_unittest::TestOneofParsingLite original; original.mutable_oneof_submessage()->set_optional_int32(5); - string serialized; + std::string serialized; EXPECT_TRUE(original.SerializeToString(&serialized)); protobuf_unittest::TestOneofParsingLite parsed; for (int i = 0; i < 2; ++i) { @@ -941,7 +941,7 @@ TEST(Lite, AllLite44) { { protobuf_unittest::TestOneofParsingLite original; original.set_oneof_string("string"); - string serialized; + std::string serialized; EXPECT_TRUE(original.SerializeToString(&serialized)); protobuf_unittest::TestOneofParsingLite parsed; for (int i = 0; i < 2; ++i) { @@ -957,7 +957,7 @@ TEST(Lite, AllLite44) { { protobuf_unittest::TestOneofParsingLite original; original.set_oneof_bytes("bytes"); - string serialized; + std::string serialized; EXPECT_TRUE(original.SerializeToString(&serialized)); protobuf_unittest::TestOneofParsingLite parsed; for (int i = 0; i < 2; ++i) { @@ -973,7 +973,7 @@ TEST(Lite, AllLite44) { { protobuf_unittest::TestOneofParsingLite original; original.set_oneof_enum(protobuf_unittest::V2_SECOND); - string serialized; + std::string serialized; EXPECT_TRUE(original.SerializeToString(&serialized)); protobuf_unittest::TestOneofParsingLite parsed; for (int i = 0; i < 2; ++i) { @@ -990,7 +990,7 @@ TEST(Lite, AllLite44) { TEST(Lite, AllLite45) { // Test unknown fields are not discarded upon parsing. - string data = "\20\1"; // varint 1 with field number 2 + std::string data = "\20\1"; // varint 1 with field number 2 protobuf_unittest::ForeignMessageLite a; EXPECT_TRUE(a.ParseFromString(data)); @@ -998,7 +998,7 @@ TEST(Lite, AllLite45) { reinterpret_cast(data.data()), data.size()); EXPECT_TRUE(a.MergePartialFromCodedStream(&input_stream)); - string serialized = a.SerializeAsString(); + std::string serialized = a.SerializeAsString(); EXPECT_EQ(serialized.substr(0, 2), data); EXPECT_EQ(serialized.substr(2), data); } @@ -1013,7 +1013,7 @@ TEST(Lite, AllLite45) { TEST(Lite, AllLite46) { protobuf_unittest::PackedInt32 packed; packed.add_repeated_int32(42); - string serialized; + std::string serialized; ASSERT_TRUE(packed.SerializeToString(&serialized)); protobuf_unittest::NonPackedInt32 non_packed; @@ -1025,7 +1025,7 @@ TEST(Lite, AllLite46) { TEST(Lite, AllLite47) { protobuf_unittest::NonPackedFixed32 non_packed; non_packed.add_repeated_fixed32(42); - string serialized; + std::string serialized; ASSERT_TRUE(non_packed.SerializeToString(&serialized)); protobuf_unittest::PackedFixed32 packed; @@ -1034,5 +1034,162 @@ TEST(Lite, AllLite47) { EXPECT_EQ(42, packed.repeated_fixed32(0)); } +TEST(Lite, MapCrash) { + // See b/113635730 + Arena arena; + auto msg = Arena::CreateMessage(&arena); + // Payload for the map with a enum varint that's longer > + // 10 bytes. This causes a parse fail and a subsequent delete. field 16 + // (map) tag = 128+2 = \202 \1 + // 13 long \15 + // int32 key = 1 (\10 \1) + // MapEnumLite value = too long varint (parse error) + EXPECT_FALSE(msg->ParseFromString( + "\202\1\15\10\1\200\200\200\200\200\200\200\200\200\200\1")); +} + +TEST(Lite, CorrectEnding) { + protobuf_unittest::TestAllTypesLite msg; + { + // All proto wireformat parsers should act the same on parsing data in as + // much as it concerns the parsing, ie. not the interpretation of the data. + // TestAllTypesLite is not a group inside another message. So in practice + // will not encounter an end-group tag. However the parser should behave + // like any wire format parser should. + static const char kWireFormat[] = "\204\1"; + io::CodedInputStream cis(reinterpret_cast(kWireFormat), 2); + // The old CodedInputStream parser got an optimization (ReadTagNoLastTag) + // for non-group messages (like TestAllTypesLite) which made it not accept + // end-group. This is not a real big deal, but I think going forward its + // good to have all parse loops behave 'exactly' the same. +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis)); + EXPECT_FALSE(cis.ConsumedEntireMessage()); + EXPECT_TRUE(cis.LastTagWas(132)); +#endif + } + { + // This is an incomplete end-group tag. This should be a genuine parse + // failure. + static const char kWireFormat[] = "\214"; + io::CodedInputStream cis(reinterpret_cast(kWireFormat), 1); + // Unfortunately the old parser detects a parse error in ReadTag and returns + // 0 (as it states 0 is an invalid tag). However 0 is not an invalid tag + // as it can be used to terminate the stream, so this returns true. +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + EXPECT_FALSE(msg.MergePartialFromCodedStream(&cis)); +#endif + } +} + +TEST(Lite, DebugString) { + protobuf_unittest::TestAllTypesLite message1, message2; + EXPECT_TRUE(HasPrefixString(message1.DebugString(), "MessageLite at 0x")); + EXPECT_TRUE(HasPrefixString(message2.DebugString(), "MessageLite at 0x")); + + // DebugString() and ShortDebugString() are the same for now. + EXPECT_EQ(message1.DebugString(), message1.ShortDebugString()); + + // Even identical lite protos should have different DebugString() output. Part + // of the reason for including the memory address is so that we get some + // non-determinism, which should make it easier for us to change the output + // later without breaking any code. + EXPECT_NE(message1.DebugString(), message2.DebugString()); +} + +TEST(Lite, EnumValueToName) { + EXPECT_EQ("FOREIGN_LITE_FOO", protobuf_unittest::ForeignEnumLite_Name( + protobuf_unittest::FOREIGN_LITE_FOO)); + EXPECT_EQ("FOREIGN_LITE_BAR", protobuf_unittest::ForeignEnumLite_Name( + protobuf_unittest::FOREIGN_LITE_BAR)); + EXPECT_EQ("FOREIGN_LITE_BAZ", protobuf_unittest::ForeignEnumLite_Name( + protobuf_unittest::FOREIGN_LITE_BAZ)); + EXPECT_EQ("", protobuf_unittest::ForeignEnumLite_Name(0)); + EXPECT_EQ("", protobuf_unittest::ForeignEnumLite_Name(999)); +} + +TEST(Lite, NestedEnumValueToName) { + EXPECT_EQ("FOO", protobuf_unittest::TestAllTypesLite::NestedEnum_Name( + protobuf_unittest::TestAllTypesLite::FOO)); + EXPECT_EQ("BAR", protobuf_unittest::TestAllTypesLite::NestedEnum_Name( + protobuf_unittest::TestAllTypesLite::BAR)); + EXPECT_EQ("BAZ", protobuf_unittest::TestAllTypesLite::NestedEnum_Name( + protobuf_unittest::TestAllTypesLite::BAZ)); + EXPECT_EQ("", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(0)); + EXPECT_EQ("", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(999)); +} + +TEST(Lite, EnumNameToValue) { + protobuf_unittest::ForeignEnumLite value; + + ASSERT_TRUE( + protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_FOO", &value)); + EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_FOO, value); + + ASSERT_TRUE( + protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_BAR", &value)); + EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_BAR, value); + + ASSERT_TRUE( + protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_BAZ", &value)); + EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_BAZ, value); + + // Non-existent values + EXPECT_FALSE(protobuf_unittest::ForeignEnumLite_Parse("E", &value)); + EXPECT_FALSE( + protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_C", &value)); + EXPECT_FALSE(protobuf_unittest::ForeignEnumLite_Parse("G", &value)); +} + +TEST(Lite, NestedEnumNameToValue) { + protobuf_unittest::TestAllTypesLite::NestedEnum value; + + ASSERT_TRUE( + protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("FOO", &value)); + EXPECT_EQ(protobuf_unittest::TestAllTypesLite::FOO, value); + + ASSERT_TRUE( + protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("BAR", &value)); + EXPECT_EQ(protobuf_unittest::TestAllTypesLite::BAR, value); + + ASSERT_TRUE( + protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("BAZ", &value)); + EXPECT_EQ(protobuf_unittest::TestAllTypesLite::BAZ, value); + + // Non-existent values + EXPECT_FALSE( + protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("A", &value)); + EXPECT_FALSE( + protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("C", &value)); + EXPECT_FALSE( + protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("G", &value)); +} + +TEST(Lite, AliasedEnum) { + // Enums with allow_alias = true can have multiple entries with the same + // value. + EXPECT_EQ("FOO1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name( + protobuf_unittest::DupEnum::FOO1)); + EXPECT_EQ("FOO1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name( + protobuf_unittest::DupEnum::FOO2)); + EXPECT_EQ("BAR1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name( + protobuf_unittest::DupEnum::BAR1)); + EXPECT_EQ("BAR1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name( + protobuf_unittest::DupEnum::BAR2)); + EXPECT_EQ("BAZ", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name( + protobuf_unittest::DupEnum::BAZ)); + EXPECT_EQ("", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(999)); + + protobuf_unittest::DupEnum::TestEnumWithDupValueLite value; + ASSERT_TRUE( + protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Parse("FOO1", &value)); + EXPECT_EQ(protobuf_unittest::DupEnum::FOO1, value); + + value = static_cast(0); + ASSERT_TRUE( + protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Parse("FOO2", &value)); + EXPECT_EQ(protobuf_unittest::DupEnum::FOO2, value); +} + } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/map.h b/src/google/protobuf/map.h index c5afa2001c..bc97701597 100644 --- a/src/google/protobuf/map.h +++ b/src/google/protobuf/map.h @@ -53,6 +53,8 @@ #error "You cannot SWIG proto headers" #endif +#include + namespace google { namespace protobuf { @@ -61,7 +63,8 @@ class Map; class MapIterator; -template struct is_proto_enum; +template +struct is_proto_enum; namespace internal { template (arena_, 0u, hasher(), Allocator(arena_)); + elements_ = + Arena::Create(arena_, 0u, hasher(), Allocator(arena_)); } // re-implement std::allocator to use arena allocator for memory allocation. @@ -229,7 +232,7 @@ class Map { #if __cplusplus >= 201103L && !defined(GOOGLE_PROTOBUF_OS_APPLE) && \ !defined(GOOGLE_PROTOBUF_OS_NACL) && \ !defined(GOOGLE_PROTOBUF_OS_EMSCRIPTEN) - template + 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 @@ -239,7 +242,7 @@ class Map { NodeType(std::forward(args)...); } - template + template void destroy(NodeType* p) { p->~NodeType(); } @@ -272,9 +275,7 @@ class Map { // To support gcc-4.4, which does not properly // support templated friend classes - Arena* arena() const { - return arena_; - } + Arena* arena() const { return arena_; } private: typedef void DestructorSkippable_; @@ -412,7 +413,7 @@ class Map { : node_(NodePtrFromKeyPtr(*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, 0); + GOOGLE_DCHECK_EQ(bucket_index_ % 2, 0u); } // Advance through buckets, looking for the first that isn't empty. @@ -452,7 +453,7 @@ class Map { if (is_list) { SearchFrom(bucket_index_ + 1); } else { - GOOGLE_DCHECK_EQ(bucket_index_ & 1, 0); + GOOGLE_DCHECK_EQ(bucket_index_ & 1, 0u); Tree* tree = static_cast(m_->table_[bucket_index_]); if (++tree_it == tree->end()) { SearchFrom(bucket_index_ + 2); @@ -481,8 +482,7 @@ class Map { // 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; + 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_)) { @@ -557,6 +557,7 @@ class Map { iterator find(const Key& k) { return iterator(FindHelper(k).first); } const_iterator find(const Key& k) const { return find(k, NULL); } + bool contains(const Key& k) const { return find(k) != end(); } // In traditional C++ style, this performs "insert if not present." std::pair insert(const KeyValuePair& kv) { @@ -626,7 +627,7 @@ class Map { } DestroyNode(item); --num_elements_; - if (GOOGLE_PREDICT_FALSE(b == index_of_first_non_null_)) { + if (PROTOBUF_PREDICT_FALSE(b == index_of_first_non_null_)) { while (index_of_first_non_null_ < num_buckets_ && table_[index_of_first_non_null_] == NULL) { ++index_of_first_non_null_; @@ -683,7 +684,7 @@ class Map { if (TableEntryIsEmpty(b)) { result = InsertUniqueInList(b, node); } else if (TableEntryIsNonEmptyList(b)) { - if (GOOGLE_PREDICT_FALSE(TableEntryIsTooLong(b))) { + if (PROTOBUF_PREDICT_FALSE(TableEntryIsTooLong(b))) { TreeConvert(b); result = InsertUniqueInTree(b, node); GOOGLE_DCHECK_EQ(result.bucket_index_, b & ~static_cast(1)); @@ -717,10 +718,9 @@ class Map { GOOGLE_DCHECK_EQ(table_[b], table_[b ^ 1]); // Maintain the invariant that node->next is NULL for all Nodes in Trees. node->next = NULL; - return iterator(static_cast(table_[b]) - ->insert(KeyPtrFromNodePtr(node)) - .first, - this, b & ~static_cast(1)); + return iterator( + static_cast(table_[b])->insert(KeyPtrFromNodePtr(node)).first, + this, b & ~static_cast(1)); } // Returns whether it did resize. Currently this is only used when @@ -738,13 +738,13 @@ class Map { // 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 (GOOGLE_PREDICT_FALSE(new_size >= hi_cutoff)) { + if (PROTOBUF_PREDICT_FALSE(new_size >= hi_cutoff)) { if (num_buckets_ <= max_size() / 2) { Resize(num_buckets_ * 2); return true; } - } else if (GOOGLE_PREDICT_FALSE(new_size <= lo_cutoff && - num_buckets_ > kMinTableSize)) { + } 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 @@ -831,7 +831,7 @@ class Map { } static bool TableEntryIsTree(void* const* table, size_type b) { return !TableEntryIsEmpty(table, b) && - !TableEntryIsNonEmptyList(table, b); + !TableEntryIsNonEmptyList(table, b); } static bool TableEntryIsList(void* const* table, size_type b) { return !TableEntryIsTree(table, b); @@ -937,9 +937,10 @@ class Map { // Return a randomish value. size_type Seed() const { size_type s = static_cast(reinterpret_cast(this)); -#if defined(__x86_64__) && defined(__GNUC__) +#if defined(__x86_64__) && defined(__GNUC__) && \ + !defined(GOOGLE_PROTOBUF_NO_RDTSC) uint32 hi, lo; - asm("rdtsc" : "=a" (lo), "=d" (hi)); + asm("rdtsc" : "=a"(lo), "=d"(hi)); s += ((static_cast(hi) << 32) | lo); #endif return s; @@ -969,9 +970,7 @@ class Map { const_iterator() {} explicit const_iterator(const InnerIt& it) : it_(it) {} - const_reference operator*() const { - return *it_->value(); - } + const_reference operator*() const { return *it_->value(); } const_pointer operator->() const { return &(operator*()); } const_iterator& operator++() { @@ -1048,7 +1047,7 @@ class Map { // Element access T& operator[](const key_type& key) { - value_type** value = &(*elements_)[key]; + value_type** value = &(*elements_)[key]; if (*value == NULL) { *value = CreateValueTypeInternal(key); internal::MapValueInitializer::value, T>::Initialize( @@ -1077,6 +1076,7 @@ class Map { return const_iterator(iterator(elements_->find(key))); } iterator find(const key_type& key) { return iterator(elements_->find(key)); } + bool contains(const Key& key) const { return elements_->contains(key); } std::pair equal_range( const key_type& key) const { const_iterator it = find(key); @@ -1219,4 +1219,6 @@ class Map { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_MAP_H__ diff --git a/src/google/protobuf/map_entry.h b/src/google/protobuf/map_entry.h index 53f35077b4..a650c875ea 100644 --- a/src/google/protobuf/map_entry.h +++ b/src/google/protobuf/map_entry.h @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include @@ -121,12 +121,11 @@ class MapEntry InternalMetadataWithArena _internal_metadata_; private: - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::Arena; + friend class ::PROTOBUF_NAMESPACE_ID::Arena; template friend class internal::MapField; - friend class internal::GeneratedMessageReflection; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntry); }; diff --git a/src/google/protobuf/map_entry_lite.h b/src/google/protobuf/map_entry_lite.h index 982f0f4468..e2d6756c20 100644 --- a/src/google/protobuf/map_entry_lite.h +++ b/src/google/protobuf/map_entry_lite.h @@ -35,15 +35,17 @@ #include #include +#include +#include #include #include +#include #include #include #include -#include +#include #include - #ifdef SWIG #error "You cannot SWIG proto headers" #endif @@ -99,6 +101,43 @@ struct MoveHelper { // strings and similar } }; +// 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) { + 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(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. @@ -106,6 +145,9 @@ template class MapEntryImpl : public Base { + public: + typedef MapEntryFuncs Funcs; + protected: // Provide utilities to parse/serialize key/value. Provide utilities to // manipulate internal stored type. @@ -120,16 +162,16 @@ class MapEntryImpl : public Base { // 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; + 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 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; @@ -186,12 +228,41 @@ class MapEntryImpl : public Base { // MapEntryImpl is for implementation only and this function isn't called // anywhere. Just provide a fake implementation here for MessageLite. - string GetTypeName() const override { return ""; } + std::string GetTypeName() const override { return ""; } void CheckTypeAndMergeFrom(const MessageLite& other) override { - MergeFromInternal(*::google::protobuf::down_cast(&other)); + MergeFromInternal(*::google::protobuf::internal::DownCast(&other)); } +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + 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; + } +#else bool MergePartialFromCodedStream(io::CodedInputStream* input) override { uint32 tag; @@ -222,9 +293,8 @@ class MapEntryImpl : public Base { break; default: - if (tag == 0 || - WireFormatLite::GetTagWireType(tag) == - WireFormatLite::WIRETYPE_END_GROUP) { + if (tag == 0 || WireFormatLite::GetTagWireType(tag) == + WireFormatLite::WIRETYPE_END_GROUP) { return true; } if (!WireFormatLite::SkipField(input, tag)) return false; @@ -232,40 +302,36 @@ class MapEntryImpl : public Base { } } } +#endif size_t ByteSizeLong() const override { size_t size = 0; - size += has_key() ? - kTagSize + static_cast(KeyTypeHandler::ByteSize(key())) : 0; - size += has_value() ? - kTagSize + static_cast(ValueTypeHandler::ByteSize(value())) : 0; + size += has_key() ? kTagSize + + static_cast(KeyTypeHandler::ByteSize(key())) + : 0; + size += has_value() + ? kTagSize + + static_cast(ValueTypeHandler::ByteSize(value())) + : 0; return size; } - void SerializeWithCachedSizes(io::CodedOutputStream* output) const override { - KeyTypeHandler::Write(kKeyFieldNumber, key(), output); - ValueTypeHandler::Write(kValueFieldNumber, value(), output); - } - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* output) const override { - output = KeyTypeHandler::InternalWriteToArray(kKeyFieldNumber, key(), - deterministic, output); - output = ValueTypeHandler::InternalWriteToArray(kValueFieldNumber, value(), - deterministic, output); - return output; + ::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; + size += has_key() ? static_cast(kTagSize) + + KeyTypeHandler::GetCachedSize(key()) + : 0; + size += has_value() ? static_cast(kTagSize) + + ValueTypeHandler::GetCachedSize(value()) + : 0; return size; } @@ -304,8 +370,8 @@ class MapEntryImpl : public Base { public: void Clear() override { KeyTypeHandler::Clear(&key_, GetArenaNoVirtual()); - ValueTypeHandler::ClearMaybeByDefaultEnum( - &value_, GetArenaNoVirtual(), default_enum_value); + ValueTypeHandler::ClearMaybeByDefaultEnum(&value_, GetArenaNoVirtual(), + default_enum_value); clear_has_key(); clear_has_value(); } @@ -318,26 +384,15 @@ class MapEntryImpl : public Base { Arena* GetArena() const override { return GetArenaNoVirtual(); } - // Create a MapEntryImpl for given key and value from Map in - // serialization. This function is only called when value is enum. Enum is - // treated differently because its type in MapEntry is int and its type in - // Map is enum. We cannot create a reference to int from an enum. - static Derived* EnumWrap(const Key& key, const Value value, Arena* arena) { - return Arena::CreateMessage(arena, key, value); - } - - // Like above, but for all the other types. This avoids value copy to create - // MapEntryImpl from Map in serialization. - static Derived* Wrap(const Key& key, const Value& value, Arena* arena) { - return Arena::CreateMessage(arena, key, 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 @@ -355,11 +410,11 @@ class MapEntryImpl : public Base { int size; input->GetDirectBufferPointerInline(&data, &size); // We could use memcmp here, but we don't bother. The tag is one byte. - GOOGLE_COMPILE_ASSERT(kTagSize == 1, tag_size_error); + 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 (GOOGLE_PREDICT_TRUE(map_size != map_->size())) { + if (PROTOBUF_PREDICT_TRUE(map_size != map_->size())) { // We created a new key-value pair. Fill in the value. typedef typename MapIf::type T; @@ -377,64 +432,118 @@ class MapEntryImpl : public Base { key_ = Key(); } - entry_.reset(mf_->NewEntry()); + NewEntry(); *entry_->mutable_key() = key_; const bool result = entry_->MergePartialFromCodedStream(input); if (result) UseKeyAndValueFromEntry(); - if (entry_->GetArena() != NULL) entry_.release(); 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, + Metadata* 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() GOOGLE_PROTOBUF_ATTRIBUTE_COLD { + 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_]; - MoveHelper::Move(entry_->mutable_value(), value_ptr_); + 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) - GOOGLE_PROTOBUF_ATTRIBUTE_COLD { - typedef MoveHelper KeyMover; - typedef MoveHelper ValueMover; - entry_.reset(mf_->NewEntry()); + 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(); - if (entry_->GetArena() != NULL) entry_.release(); return result; } + typedef MoveHelper + KeyMover; + typedef MoveHelper + ValueMover; + MapField* const mf_; Map* const map_; Key key_; Value* value_ptr_; - // On the fast path entry_ is not used. And, when entry_ is used, it's set - // to mf_->NewEntry(), so in the arena case we must call entry_.release. - std::unique_ptr entry_; + MapEntryImpl* entry_ = nullptr; }; protected: @@ -445,75 +554,8 @@ class MapEntryImpl : public Base { bool has_value() const { return (_has_bits_[0] & 0x00000002u) != 0; } void clear_has_value() { _has_bits_[0] &= ~0x00000002u; } - private: - // Serializing a generated message containing map field involves serializing - // key-value pairs from Map. The wire format of each key-value pair - // after serialization should be the same as that of a MapEntry message - // containing the same key and value inside it. However, Map doesn't - // store key and value as MapEntry message, which disables us to use existing - // code to serialize message. In order to use existing code to serialize - // message, we need to construct a MapEntry from key-value pair. But it - // involves copy of key and value to construct a MapEntry. In order to avoid - // this copy in constructing a MapEntry, we need the following class which - // only takes references of given key and value. - class MapEntryWrapper : public Derived { - typedef Derived BaseClass; - typedef typename BaseClass::KeyMapEntryAccessorType KeyMapEntryAccessorType; - typedef - typename BaseClass::ValueMapEntryAccessorType ValueMapEntryAccessorType; - - public: - MapEntryWrapper(Arena* arena, const Key& key, const Value& value) - : Derived(arena), key_(key), value_(value) { - BaseClass::set_has_key(); - BaseClass::set_has_value(); - } - inline const KeyMapEntryAccessorType& key() const override { return key_; } - inline const ValueMapEntryAccessorType& value() const override { - return value_; - } - - private: - const Key& key_; - const Value& value_; - - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::Arena; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - }; - - // Like above, but for enum value only, which stores value instead of - // reference of value field inside. This is needed because the type of value - // field in constructor is an enum, while we need to store it as an int. If we - // initialize a reference to int with a reference to enum, compiler will - // generate a temporary int from enum and initialize the reference to int with - // the temporary. - class MapEnumEntryWrapper : public Derived { - typedef Derived BaseClass; - typedef typename BaseClass::KeyMapEntryAccessorType KeyMapEntryAccessorType; - typedef - typename BaseClass::ValueMapEntryAccessorType ValueMapEntryAccessorType; - - public: - MapEnumEntryWrapper(Arena* arena, const Key& key, const Value& value) - : Derived(arena), key_(key), value_(value) { - BaseClass::set_has_key(); - BaseClass::set_has_value(); - } - inline const KeyMapEntryAccessorType& key() const { return key_; } - inline const ValueMapEntryAccessorType& value() const { return value_; } - - private: - const KeyMapEntryAccessorType& key_; - const ValueMapEntryAccessorType value_; - - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::Arena; - typedef void DestructorSkippable_; - }; - - inline Arena* GetArenaNoVirtual() const { - return arena_; - } + public: + inline Arena* GetArenaNoVirtual() const { return arena_; } public: // Needed for constructing tables KeyOnMemory key_; @@ -522,7 +564,7 @@ class MapEntryImpl : public Base { uint32 _has_bits_[1]; private: - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::Arena; + friend class ::PROTOBUF_NAMESPACE_ID::Arena; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; template > { // 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 { +template +struct SortItem { SortItem() {} explicit SortItem(PtrToKeyValuePair p) : first(p->first), second(p) {} @@ -582,16 +625,14 @@ template struct SortItem { PtrToKeyValuePair second; }; -template struct CompareByFirstField { - bool operator()(const T& a, const T& b) const { - return a.first < b.first; - } +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; - } +template +struct CompareByDerefFirst { + bool operator()(const T& a, const T& b) const { return a->first < b->first; } }; // Helper for table driven serialization @@ -606,20 +647,20 @@ struct FromHelper { template <> struct FromHelper { - static ArenaStringPtr From(const string& x) { + static ArenaStringPtr From(const std::string& x) { ArenaStringPtr res; - TaggedPtr<::std::string> ptr; - ptr.Set(const_cast(&x)); + TaggedPtr ptr; + ptr.Set(const_cast(&x)); res.UnsafeSetTaggedPointer(ptr); return res; } }; template <> struct FromHelper { - static ArenaStringPtr From(const string& x) { + static ArenaStringPtr From(const std::string& x) { ArenaStringPtr res; - TaggedPtr<::std::string> ptr; - ptr.Set(const_cast(&x)); + TaggedPtr ptr; + ptr.Set(const_cast(&x)); res.UnsafeSetTaggedPointer(ptr); return res; } diff --git a/src/google/protobuf/map_field.cc b/src/google/protobuf/map_field.cc index 2ce54cb3c0..880f3e188f 100644 --- a/src/google/protobuf/map_field.cc +++ b/src/google/protobuf/map_field.cc @@ -33,6 +33,8 @@ #include +#include + namespace google { namespace protobuf { namespace internal { @@ -91,20 +93,38 @@ void MapFieldBase::SetRepeatedDirty() { state_.store(STATE_MODIFIED_REPEATED, std::memory_order_relaxed); } -void* MapFieldBase::MutableRepeatedPtrField() const { return repeated_field_; } - void MapFieldBase::SyncRepeatedFieldWithMap() const { // acquire here matches with release below to ensure that we can only see a // value of CLEAN after all previous changes have been synced. - if (state_.load(std::memory_order_acquire) == STATE_MODIFIED_MAP) { - mutex_.Lock(); - // Double check state, because another thread may have seen the same state - // and done the synchronization before the current thread. - if (state_.load(std::memory_order_relaxed) == STATE_MODIFIED_MAP) { - SyncRepeatedFieldWithMapNoLock(); - state_.store(CLEAN, std::memory_order_release); - } - mutex_.Unlock(); + switch (state_.load(std::memory_order_acquire)) { + case STATE_MODIFIED_MAP: + mutex_.Lock(); + // Double check state, because another thread may have seen the same + // state and done the synchronization before the current thread. + if (state_.load(std::memory_order_relaxed) == STATE_MODIFIED_MAP) { + SyncRepeatedFieldWithMapNoLock(); + state_.store(CLEAN, std::memory_order_release); + } + mutex_.Unlock(); + break; + case CLEAN: + mutex_.Lock(); + // Double check state + if (state_.load(std::memory_order_relaxed) == CLEAN) { + if (repeated_field_ == nullptr) { + if (arena_ == nullptr) { + repeated_field_ = new RepeatedPtrField(); + } else { + repeated_field_ = + Arena::CreateMessage >(arena_); + } + } + state_.store(CLEAN, std::memory_order_release); + } + mutex_.Unlock(); + break; + default: + break; } } @@ -131,15 +151,12 @@ void MapFieldBase::SyncMapWithRepeatedField() const { // ------------------DynamicMapField------------------ DynamicMapField::DynamicMapField(const Message* default_entry) - : default_entry_(default_entry) { -} + : default_entry_(default_entry) {} -DynamicMapField::DynamicMapField(const Message* default_entry, - Arena* arena) +DynamicMapField::DynamicMapField(const Message* default_entry, Arena* arena) : TypeDefinedMapFieldBase(arena), map_(arena), - default_entry_(default_entry) { -} + default_entry_(default_entry) {} DynamicMapField::~DynamicMapField() { // DynamicMapField owns map values. Need to delete them before clearing @@ -151,56 +168,72 @@ DynamicMapField::~DynamicMapField() { map_.clear(); } -int DynamicMapField::size() const { - return GetMap().size(); +int DynamicMapField::size() const { return GetMap().size(); } + +void DynamicMapField::Clear() { + Map* map = &const_cast(this)->map_; + for (Map::iterator iter = map->begin(); + iter != map->end(); ++iter) { + iter->second.DeleteData(); + } + map->clear(); + + if (MapFieldBase::repeated_field_ != nullptr) { + MapFieldBase::repeated_field_->Clear(); + } + // Data in map and repeated field are both empty, but we can't set status + // CLEAN which will invalidate previous reference to map. + MapFieldBase::SetMapDirty(); } -bool DynamicMapField::ContainsMapKey( - const MapKey& map_key) const { +bool DynamicMapField::ContainsMapKey(const MapKey& map_key) const { const Map& map = GetMap(); Map::const_iterator iter = map.find(map_key); return iter != map.end(); } -bool DynamicMapField::InsertOrLookupMapValue( - const MapKey& map_key, MapValueRef* val) { - // Always use mutable map because users may change the map value by - // MapValueRef. - Map* map = MutableMap(); - Map::iterator iter = map->find(map_key); - if (iter == map->end()) { - // Insert - MapValueRef& map_val = (*map)[map_key]; - const FieldDescriptor* val_des = - default_entry_->GetDescriptor()->FindFieldByName("value"); - map_val.SetType(val_des->cpp_type()); - // Allocate memory for the inserted MapValueRef, and initialize to - // default value. - switch (val_des->cpp_type()) { +void DynamicMapField::AllocateMapValue(MapValueRef* map_val) { + const FieldDescriptor* val_des = + default_entry_->GetDescriptor()->FindFieldByName("value"); + map_val->SetType(val_des->cpp_type()); + // Allocate memory for the MapValueRef, and initialize to + // default value. + switch (val_des->cpp_type()) { #define HANDLE_TYPE(CPPTYPE, TYPE) \ case FieldDescriptor::CPPTYPE_##CPPTYPE: { \ TYPE* value = new TYPE(); \ - map_val.SetValue(value); \ + map_val->SetValue(value); \ break; \ } - HANDLE_TYPE(INT32, int32); - HANDLE_TYPE(INT64, int64); - HANDLE_TYPE(UINT32, uint32); - HANDLE_TYPE(UINT64, uint64); - HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE(FLOAT, float); - HANDLE_TYPE(BOOL, bool); - HANDLE_TYPE(STRING, string); - HANDLE_TYPE(ENUM, int32); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); + HANDLE_TYPE(UINT32, uint32); + HANDLE_TYPE(UINT64, uint64); + HANDLE_TYPE(DOUBLE, double); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(STRING, std::string); + HANDLE_TYPE(ENUM, int32); #undef HANDLE_TYPE - case FieldDescriptor::CPPTYPE_MESSAGE: { - const Message& message = default_entry_->GetReflection()->GetMessage( - *default_entry_, val_des); - Message* value = message.New(); - map_val.SetValue(value); - break; - } + case FieldDescriptor::CPPTYPE_MESSAGE: { + const Message& message = + default_entry_->GetReflection()->GetMessage(*default_entry_, val_des); + Message* value = message.New(); + map_val->SetValue(value); + break; } + } +} + +bool DynamicMapField::InsertOrLookupMapValue(const MapKey& map_key, + MapValueRef* val) { + // Always use mutable map because users may change the map value by + // MapValueRef. + Map* map = MutableMap(); + Map::iterator iter = map->find(map_key); + if (iter == map->end()) { + MapValueRef& map_val = map_[map_key]; + AllocateMapValue(&map_val); val->CopyFrom(map_val); return true; } @@ -243,6 +276,72 @@ void DynamicMapField::SetMapIteratorValue(MapIterator* map_iter) const { map_iter->value_.CopyFrom(iter->second); } +void DynamicMapField::MergeFrom(const MapFieldBase& other) { + GOOGLE_DCHECK(IsMapValid() && other.IsMapValid()); + Map* map = MutableMap(); + const DynamicMapField& other_field = + reinterpret_cast(other); + for (Map::const_iterator other_it = + other_field.map_.begin(); + other_it != other_field.map_.end(); ++other_it) { + Map::iterator iter = map->find(other_it->first); + MapValueRef* map_val; + if (iter == map->end()) { + map_val = &map_[other_it->first]; + AllocateMapValue(map_val); + } else { + map_val = &iter->second; + } + + // Copy map value + const FieldDescriptor* field_descriptor = + default_entry_->GetDescriptor()->FindFieldByName("value"); + switch (field_descriptor->cpp_type()) { + case FieldDescriptor::CPPTYPE_INT32: { + map_val->SetInt32Value(other_it->second.GetInt32Value()); + break; + } + case FieldDescriptor::CPPTYPE_INT64: { + map_val->SetInt64Value(other_it->second.GetInt64Value()); + break; + } + case FieldDescriptor::CPPTYPE_UINT32: { + map_val->SetUInt32Value(other_it->second.GetUInt32Value()); + break; + } + case FieldDescriptor::CPPTYPE_UINT64: { + map_val->SetUInt64Value(other_it->second.GetUInt64Value()); + break; + } + case FieldDescriptor::CPPTYPE_FLOAT: { + map_val->SetFloatValue(other_it->second.GetFloatValue()); + break; + } + case FieldDescriptor::CPPTYPE_DOUBLE: { + map_val->SetDoubleValue(other_it->second.GetDoubleValue()); + break; + } + case FieldDescriptor::CPPTYPE_BOOL: { + map_val->SetBoolValue(other_it->second.GetBoolValue()); + break; + } + case FieldDescriptor::CPPTYPE_STRING: { + map_val->SetStringValue(other_it->second.GetStringValue()); + break; + } + case FieldDescriptor::CPPTYPE_ENUM: { + map_val->SetEnumValue(other_it->second.GetEnumValue()); + break; + } + case FieldDescriptor::CPPTYPE_MESSAGE: { + map_val->MutableMessageValue()->CopyFrom( + other_it->second.GetMessageValue()); + break; + } + } + } +} + void DynamicMapField::Swap(MapFieldBase* other) { DynamicMapField* other_field = down_cast(other); std::swap(this->MapFieldBase::repeated_field_, other_field->repeated_field_); @@ -409,7 +508,7 @@ void DynamicMapField::SyncMapWithRepeatedFieldNoLock() const { HANDLE_TYPE(DOUBLE, double, Double); HANDLE_TYPE(FLOAT, float, Float); HANDLE_TYPE(BOOL, bool, Bool); - HANDLE_TYPE(STRING, string, String); + HANDLE_TYPE(STRING, std::string, String); HANDLE_TYPE(ENUM, int32, EnumValue); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: { @@ -436,7 +535,7 @@ size_t DynamicMapField::SpaceUsedExcludingSelfNoLock() const { size += sizeof(it->second) * map_size; // If key is string, add the allocated space. if (it->first.type() == FieldDescriptor::CPPTYPE_STRING) { - size += sizeof(string) * map_size; + size += sizeof(std::string) * map_size; } // Add the allocated space in MapValueRef. switch (it->second.type()) { @@ -452,7 +551,7 @@ size_t DynamicMapField::SpaceUsedExcludingSelfNoLock() const { HANDLE_TYPE(DOUBLE, double); HANDLE_TYPE(FLOAT, float); HANDLE_TYPE(BOOL, bool); - HANDLE_TYPE(STRING, string); + HANDLE_TYPE(STRING, std::string); HANDLE_TYPE(ENUM, int32); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: { diff --git a/src/google/protobuf/map_field.h b/src/google/protobuf/map_field.h index 9e60191a4d..3314f4e75a 100644 --- a/src/google/protobuf/map_field.h +++ b/src/google/protobuf/map_field.h @@ -66,16 +66,12 @@ class MapFieldAccessor; // This class provides access to map field using reflection, which is the same // as those provided for RepeatedPtrField. It is used for internal // reflection implentation only. Users should never use this directly. -class LIBPROTOBUF_EXPORT MapFieldBase { +class PROTOBUF_EXPORT MapFieldBase { public: MapFieldBase() - : arena_(NULL), - repeated_field_(NULL), - state_(STATE_MODIFIED_MAP) {} + : arena_(NULL), repeated_field_(NULL), state_(STATE_MODIFIED_MAP) {} explicit MapFieldBase(Arena* arena) - : arena_(arena), - repeated_field_(NULL), - state_(STATE_MODIFIED_MAP) { + : arena_(arena), repeated_field_(NULL), state_(STATE_MODIFIED_MAP) { // Mutex's destructor needs to be called explicitly to release resources // acquired in its constructor. arena->OwnDestructor(&mutex_); @@ -92,8 +88,8 @@ class LIBPROTOBUF_EXPORT MapFieldBase { // Pure virtual map APIs for Map Reflection. virtual bool ContainsMapKey(const MapKey& map_key) const = 0; - virtual bool InsertOrLookupMapValue( - const MapKey& map_key, MapValueRef* val) = 0; + virtual bool InsertOrLookupMapValue(const MapKey& map_key, + MapValueRef* val) = 0; // Returns whether changes to the map are reflected in the repeated field. bool IsRepeatedFieldValid() const; // Insures operations after won't get executed before calling this. @@ -103,9 +99,11 @@ class LIBPROTOBUF_EXPORT MapFieldBase { const MapIterator& b) const = 0; virtual void MapBegin(MapIterator* map_iter) const = 0; virtual void MapEnd(MapIterator* map_iter) const = 0; + virtual void MergeFrom(const MapFieldBase& other) = 0; virtual void Swap(MapFieldBase* other) = 0; // Sync Map with repeated field and returns the size of map. virtual int size() const = 0; + virtual void Clear() = 0; // Returns the number of bytes used by the repeated field, excluding // sizeof(*this) @@ -143,14 +141,15 @@ class LIBPROTOBUF_EXPORT MapFieldBase { // synchronized to repeated field STATE_MODIFIED_REPEATED = 1, // repeated field has newly added data that // has not been synchronized to map - CLEAN = 2, // data in map and repeated field are same + CLEAN = 2, // data in map and repeated field are same }; Arena* arena_; mutable RepeatedPtrField* repeated_field_; - mutable internal::WrappedMutex mutex_; // The thread to synchronize map and repeated field - // needs to get lock first; + mutable internal::WrappedMutex + mutex_; // The thread to synchronize map and repeated field + // needs to get lock first; mutable std::atomic state_; private: @@ -163,7 +162,7 @@ class LIBPROTOBUF_EXPORT MapFieldBase { // type helper for key and value. Call these help methods to deal with // different types. Real helper methods are implemented in // TypeDefinedMapFieldBase. - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::MapIterator; + friend class ::PROTOBUF_NAMESPACE_ID::MapIterator; // Allocate map<...>::iterator for MapIterator. virtual void InitializeIterator(MapIterator* map_iter) const = 0; @@ -184,7 +183,7 @@ class LIBPROTOBUF_EXPORT MapFieldBase { // This class provides common Map Reflection implementations for generated // message and dynamic message. -template +template class TypeDefinedMapFieldBase : public MapFieldBase { public: TypeDefinedMapFieldBase() {} @@ -226,9 +225,6 @@ class MapField : public TypeDefinedMapFieldBase { // Define message type for internal repeated field. typedef Derived EntryType; - typedef MapEntryLite - EntryLiteType; // Define abbreviation for parent MapFieldLite typedef MapFieldLite { return result; } - // Convenient methods for generated message implementation. int size() const override; - void Clear(); - void MergeFrom(const MapField& other); + void Clear() override; + void MergeFrom(const MapFieldBase& other) override; void Swap(MapFieldBase* other) override; // Used in the implementation of parsing. Caller should take the ownership iff @@ -287,6 +282,17 @@ class MapField : public TypeDefinedMapFieldBase { return impl_.NewEntryWrapper(key, t); } + const char* _InternalParse(const char* ptr, ParseContext* ctx) { + return impl_._InternalParse(ptr, ctx); + } + template + const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx, + bool (*is_valid)(int), uint32 field_num, + Metadata* metadata) { + return impl_.ParseWithEnumValidation(ptr, ctx, is_valid, field_num, + metadata); + } + private: MapFieldLiteType impl_; @@ -300,7 +306,7 @@ class MapField : public TypeDefinedMapFieldBase { void SetMapIteratorValue(MapIterator* map_iter) const override; - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::Arena; + friend class ::PROTOBUF_NAMESPACE_ID::Arena; friend class MapFieldStateTest; // For testing, it needs raw access to impl_ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapField); }; @@ -309,13 +315,14 @@ template struct MapEntryToMapField > { + kValueFieldType, default_enum_value>> { typedef MapField MapFieldType; }; -class LIBPROTOBUF_EXPORT DynamicMapField: public TypeDefinedMapFieldBase { +class PROTOBUF_EXPORT DynamicMapField + : public TypeDefinedMapFieldBase { public: explicit DynamicMapField(const Message* default_entry); DynamicMapField(const Message* default_entry, Arena* arena); @@ -325,17 +332,21 @@ class LIBPROTOBUF_EXPORT DynamicMapField: public TypeDefinedMapFieldBase& GetMap() const override; Map* MutableMap() override; int size() const override; + void Clear() override; private: Map map_; const Message* default_entry_; + void AllocateMapValue(MapValueRef* map_val); + // Implements MapFieldBase void SyncRepeatedFieldWithMapNoLock() const override; void SyncMapWithRepeatedFieldNoLock() const override; @@ -346,26 +357,21 @@ class LIBPROTOBUF_EXPORT DynamicMapField: public TypeDefinedMapFieldBase friend class internal::TypeDefinedMapFieldBase; - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::MapIterator; + friend class ::PROTOBUF_NAMESPACE_ID::MapIterator; friend class internal::DynamicMapField; union KeyValue { KeyValue() {} - string* string_value_; + std::string* string_value_; int64 int64_value_; int32 int32_value_; uint64 uint64_value_; @@ -554,7 +553,7 @@ class LIBPROTOBUF_EXPORT MapKey { } type_ = type; if (type_ == FieldDescriptor::CPPTYPE_STRING) { - val_.string_value_ = new string; + val_.string_value_ = new std::string; } } @@ -563,100 +562,82 @@ class LIBPROTOBUF_EXPORT MapKey { }; // MapValueRef points to a map value. -class LIBPROTOBUF_EXPORT MapValueRef { +class PROTOBUF_EXPORT MapValueRef { public: MapValueRef() : data_(NULL), type_(0) {} void SetInt64Value(int64 value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, - "MapValueRef::SetInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, "MapValueRef::SetInt64Value"); *reinterpret_cast(data_) = value; } void SetUInt64Value(uint64 value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, - "MapValueRef::SetUInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, "MapValueRef::SetUInt64Value"); *reinterpret_cast(data_) = value; } void SetInt32Value(int32 value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, - "MapValueRef::SetInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, "MapValueRef::SetInt32Value"); *reinterpret_cast(data_) = value; } void SetUInt32Value(uint32 value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, - "MapValueRef::SetUInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, "MapValueRef::SetUInt32Value"); *reinterpret_cast(data_) = value; } void SetBoolValue(bool value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, - "MapValueRef::SetBoolValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapValueRef::SetBoolValue"); *reinterpret_cast(data_) = value; } // TODO(jieluo) - Checks that enum is member. void SetEnumValue(int value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, - "MapValueRef::SetEnumValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueRef::SetEnumValue"); *reinterpret_cast(data_) = value; } - void SetStringValue(const string& value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, - "MapValueRef::SetStringValue"); - *reinterpret_cast(data_) = value; + void SetStringValue(const std::string& value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapValueRef::SetStringValue"); + *reinterpret_cast(data_) = value; } void SetFloatValue(float value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, - "MapValueRef::SetFloatValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, "MapValueRef::SetFloatValue"); *reinterpret_cast(data_) = value; } void SetDoubleValue(double value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, - "MapValueRef::SetDoubleValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, "MapValueRef::SetDoubleValue"); *reinterpret_cast(data_) = value; } int64 GetInt64Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, - "MapValueRef::GetInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, "MapValueRef::GetInt64Value"); return *reinterpret_cast(data_); } uint64 GetUInt64Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, - "MapValueRef::GetUInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, "MapValueRef::GetUInt64Value"); return *reinterpret_cast(data_); } int32 GetInt32Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, - "MapValueRef::GetInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, "MapValueRef::GetInt32Value"); return *reinterpret_cast(data_); } uint32 GetUInt32Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, - "MapValueRef::GetUInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, "MapValueRef::GetUInt32Value"); return *reinterpret_cast(data_); } bool GetBoolValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, - "MapValueRef::GetBoolValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapValueRef::GetBoolValue"); return *reinterpret_cast(data_); } int GetEnumValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, - "MapValueRef::GetEnumValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueRef::GetEnumValue"); return *reinterpret_cast(data_); } - const string& GetStringValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, - "MapValueRef::GetStringValue"); - return *reinterpret_cast(data_); + const std::string& GetStringValue() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapValueRef::GetStringValue"); + return *reinterpret_cast(data_); } float GetFloatValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, - "MapValueRef::GetFloatValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, "MapValueRef::GetFloatValue"); return *reinterpret_cast(data_); } double GetDoubleValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, - "MapValueRef::GetDoubleValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, "MapValueRef::GetDoubleValue"); return *reinterpret_cast(data_); } @@ -680,25 +661,20 @@ class LIBPROTOBUF_EXPORT MapValueRef { friend class internal::MapField; template friend class internal::TypeDefinedMapFieldBase; - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::MapIterator; - friend class internal::GeneratedMessageReflection; + friend class ::PROTOBUF_NAMESPACE_ID::MapIterator; + friend class Reflection; friend class internal::DynamicMapField; - void SetType(FieldDescriptor::CppType type) { - type_ = type; - } + void SetType(FieldDescriptor::CppType type) { type_ = type; } FieldDescriptor::CppType type() const { if (type_ == 0 || data_ == NULL) { - GOOGLE_LOG(FATAL) - << "Protocol Buffer map usage error:\n" - << "MapValueRef::type MapValueRef is not initialized."; + GOOGLE_LOG(FATAL) << "Protocol Buffer map usage error:\n" + << "MapValueRef::type MapValueRef is not initialized."; } return (FieldDescriptor::CppType)type_; } - void SetValue(const void* val) { - data_ = const_cast(val); - } + void SetValue(const void* val) { data_ = const_cast(val); } void CopyFrom(const MapValueRef& other) { type_ = other.type_; data_ = other.data_; @@ -718,7 +694,7 @@ class LIBPROTOBUF_EXPORT MapValueRef { HANDLE_TYPE(DOUBLE, double); HANDLE_TYPE(FLOAT, float); HANDLE_TYPE(BOOL, bool); - HANDLE_TYPE(STRING, string); + HANDLE_TYPE(STRING, std::string); HANDLE_TYPE(ENUM, int32); HANDLE_TYPE(MESSAGE, Message); #undef HANDLE_TYPE @@ -733,11 +709,11 @@ class LIBPROTOBUF_EXPORT MapValueRef { #undef TYPE_CHECK -class LIBPROTOBUF_EXPORT MapIterator { +class PROTOBUF_EXPORT MapIterator { public: MapIterator(Message* message, const FieldDescriptor* field) { const Reflection* reflection = message->GetReflection(); - map_ = reflection->MapData(message, field); + map_ = reflection->MutableMapData(message, field); key_.SetType(field->message_type()->FindFieldByName("key")->cpp_type()); value_.SetType(field->message_type()->FindFieldByName("value")->cpp_type()); map_->InitializeIterator(this); @@ -747,9 +723,7 @@ class LIBPROTOBUF_EXPORT MapIterator { map_->InitializeIterator(this); map_->CopyIterator(this, other); } - ~MapIterator() { - map_->DeleteIterator(this); - } + ~MapIterator() { map_->DeleteIterator(this); } MapIterator& operator=(const MapIterator& other) { map_ = other.map_; map_->CopyIterator(this, other); @@ -772,12 +746,8 @@ class LIBPROTOBUF_EXPORT MapIterator { map_->IncreaseIterator(this); return *this; } - const MapKey& GetKey() { - return key_; - } - const MapValueRef& GetValueRef() { - return value_; - } + const MapKey& GetKey() { return key_; } + const MapValueRef& GetValueRef() { return value_; } MapValueRef* MutableValueRef() { map_->SetMapDirty(); return &value_; @@ -810,35 +780,33 @@ class LIBPROTOBUF_EXPORT MapIterator { GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START template <> -struct hash<::GOOGLE_PROTOBUF_NAMESPACE_ID::MapKey> { - size_t operator()( - const ::GOOGLE_PROTOBUF_NAMESPACE_ID::MapKey& map_key) const { +struct hash<::PROTOBUF_NAMESPACE_ID::MapKey> { + size_t operator()(const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key) const { switch (map_key.type()) { - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_DOUBLE: - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_FLOAT: - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_ENUM: - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_MESSAGE: + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_DOUBLE: + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_FLOAT: + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_ENUM: + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_MESSAGE: GOOGLE_LOG(FATAL) << "Unsupported"; break; - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_STRING: - return hash()(map_key.GetStringValue()); - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_INT64: - return hash<::google::protobuf::int64>()(map_key.GetInt64Value()); - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_INT32: - return hash<::google::protobuf::int32>()(map_key.GetInt32Value()); - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_UINT64: - return hash<::google::protobuf::uint64>()(map_key.GetUInt64Value()); - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_UINT32: - return hash<::google::protobuf::uint32>()(map_key.GetUInt32Value()); - case ::GOOGLE_PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_BOOL: + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_STRING: + return hash()(map_key.GetStringValue()); + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_INT64: + return hash()(map_key.GetInt64Value()); + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_INT32: + return hash()(map_key.GetInt32Value()); + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_UINT64: + return hash()(map_key.GetUInt64Value()); + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_UINT32: + return hash()(map_key.GetUInt32Value()); + case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_BOOL: return hash()(map_key.GetBoolValue()); } GOOGLE_LOG(FATAL) << "Can't get here."; return 0; } - bool operator()( - const ::GOOGLE_PROTOBUF_NAMESPACE_ID::MapKey& map_key1, - const ::GOOGLE_PROTOBUF_NAMESPACE_ID::MapKey& map_key2) const { + bool operator()(const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key1, + const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key2) const { return map_key1 < map_key2; } }; diff --git a/src/google/protobuf/map_field_inl.h b/src/google/protobuf/map_field_inl.h index 169b3e2bf4..7baaa5ff31 100644 --- a/src/google/protobuf/map_field_inl.h +++ b/src/google/protobuf/map_field_inl.h @@ -46,58 +46,58 @@ namespace google { namespace protobuf { namespace internal { // UnwrapMapKey template -template +template T UnwrapMapKey(const MapKey& map_key); -template<> +template <> inline int32 UnwrapMapKey(const MapKey& map_key) { return map_key.GetInt32Value(); } -template<> +template <> inline uint32 UnwrapMapKey(const MapKey& map_key) { return map_key.GetUInt32Value(); } -template<> +template <> inline int64 UnwrapMapKey(const MapKey& map_key) { return map_key.GetInt64Value(); } -template<> +template <> inline uint64 UnwrapMapKey(const MapKey& map_key) { return map_key.GetUInt64Value(); } -template<> +template <> inline bool UnwrapMapKey(const MapKey& map_key) { return map_key.GetBoolValue(); } -template<> -inline string UnwrapMapKey(const MapKey& map_key) { +template <> +inline std::string UnwrapMapKey(const MapKey& map_key) { return map_key.GetStringValue(); } // SetMapKey template -template +template inline void SetMapKey(MapKey* map_key, const T& value); -template<> +template <> inline void SetMapKey(MapKey* map_key, const int32& value) { map_key->SetInt32Value(value); } -template<> +template <> inline void SetMapKey(MapKey* map_key, const uint32& value) { map_key->SetUInt32Value(value); } -template<> +template <> inline void SetMapKey(MapKey* map_key, const int64& value) { map_key->SetInt64Value(value); } -template<> +template <> inline void SetMapKey(MapKey* map_key, const uint64& value) { map_key->SetUInt64Value(value); } -template<> +template <> inline void SetMapKey(MapKey* map_key, const bool& value) { map_key->SetBoolValue(value); } -template<> -inline void SetMapKey(MapKey* map_key, const string& value) { +template <> +inline void SetMapKey(MapKey* map_key, const std::string& value) { map_key->SetStringValue(value); } @@ -106,7 +106,7 @@ template typename Map::const_iterator& TypeDefinedMapFieldBase::InternalGetIterator( const MapIterator* map_iter) const { - return *reinterpret_cast::const_iterator *>( + return *reinterpret_cast::const_iterator*>( map_iter->iter_); } @@ -122,15 +122,14 @@ void TypeDefinedMapFieldBase::MapEnd(MapIterator* map_iter) const { } template -bool TypeDefinedMapFieldBase::EqualIterator(const MapIterator& a, - const MapIterator& b) - const { +bool TypeDefinedMapFieldBase::EqualIterator( + const MapIterator& a, const MapIterator& b) const { return InternalGetIterator(&a) == InternalGetIterator(&b); } template -void TypeDefinedMapFieldBase::IncreaseIterator(MapIterator* map_iter) - const { +void TypeDefinedMapFieldBase::IncreaseIterator( + MapIterator* map_iter) const { ++InternalGetIterator(map_iter); SetMapIteratorValue(map_iter); } @@ -143,16 +142,15 @@ void TypeDefinedMapFieldBase::InitializeIterator( } template -void TypeDefinedMapFieldBase::DeleteIterator(MapIterator* map_iter) - const { - delete reinterpret_cast::const_iterator *>( +void TypeDefinedMapFieldBase::DeleteIterator( + MapIterator* map_iter) const { + delete reinterpret_cast::const_iterator*>( map_iter->iter_); } template void TypeDefinedMapFieldBase::CopyIterator( - MapIterator* this_iter, - const MapIterator& that_iter) const { + MapIterator* this_iter, const MapIterator& that_iter) const { InternalGetIterator(this_iter) = InternalGetIterator(&that_iter); this_iter->key_.SetType(that_iter.key_.type()); // MapValueRef::type() fails when containing data is null. However, if @@ -178,8 +176,17 @@ template void MapField::Clear() { - MapFieldBase::SyncMapWithRepeatedField(); + if (this->MapFieldBase::repeated_field_ != nullptr) { + RepeatedPtrField* repeated_field = + reinterpret_cast*>( + this->MapFieldBase::repeated_field_); + repeated_field->Clear(); + } + impl_.MutableMap()->clear(); + // Data in map and repeated field are both empty, but we can't set status + // CLEAN. Because clear is a generated API, we cannot invalidate previous + // reference to map. MapFieldBase::SetMapDirty(); } @@ -242,10 +249,11 @@ template void MapField::MergeFrom(const MapField& other) { + default_enum_value>::MergeFrom(const MapFieldBase& other) { MapFieldBase::SyncMapWithRepeatedField(); - other.SyncMapWithRepeatedField(); - impl_.MergeFrom(other.impl_); + const MapField& other_field = static_cast(other); + other_field.SyncMapWithRepeatedField(); + impl_.MergeFrom(other_field.impl_); MapFieldBase::SetMapDirty(); } @@ -291,8 +299,8 @@ void MapField::const_iterator it = map.begin(); - it != map.end(); ++it) { + for (typename Map::const_iterator it = map.begin(); it != map.end(); + ++it) { EntryType* new_entry = down_cast(default_entry->New(this->MapFieldBase::arena_)); repeated_field->AddAllocated(new_entry); @@ -313,7 +321,8 @@ void MapFieldMapFieldBase::repeated_field_ != NULL); map->clear(); for (typename RepeatedPtrField::iterator it = - repeated_field->begin(); it != repeated_field->end(); ++it) { + repeated_field->begin(); + it != repeated_field->end(); ++it) { // Cast is needed because Map's api and internal storage is different when // value is enum. For enum, we cannot cast an int to enum. Thus, we have to // copy value. For other types, they have same exposed api type and internal diff --git a/src/google/protobuf/map_field_lite.h b/src/google/protobuf/map_field_lite.h index 36b5b80d2b..180a788454 100644 --- a/src/google/protobuf/map_field_lite.h +++ b/src/google/protobuf/map_field_lite.h @@ -31,6 +31,9 @@ #ifndef GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__ #define GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__ +#include +#include +#include #include #include #include @@ -106,15 +109,53 @@ class MapFieldLite { return EntryType::Wrap(key, t, 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, + Metadata* metadata) { + typename Derived::template Parser> parser(this); + return parser.ParseWithEnumValidation(ptr, ctx, is_valid, field_num, + metadata); + } + private: typedef void DestructorSkippable_; Arena* arena_; Map map_; - friend class ::GOOGLE_PROTOBUF_NAMESPACE_ID::Arena; + friend class ::PROTOBUF_NAMESPACE_ID::Arena; }; +template +struct EnumParseWrapper { + const char* _InternalParse(const char* ptr, ParseContext* ctx) { + return map_field->ParseWithEnumValidation(ptr, ctx, is_valid, field_num, + metadata); + } + T* map_field; + bool (*is_valid)(int); + uint32 field_num; + Metadata* 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, + Metadata* 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. @@ -135,7 +176,7 @@ template struct MapEntryToMapField > { + kValueFieldType, default_enum_value>> { typedef MapFieldLite, Key, Value, kKeyFieldType, kValueFieldType, diff --git a/src/google/protobuf/map_field_test.cc b/src/google/protobuf/map_field_test.cc index dcfcca76f0..05f83e23fa 100644 --- a/src/google/protobuf/map_field_test.cc +++ b/src/google/protobuf/map_field_test.cc @@ -43,7 +43,6 @@ #include #include #include -#include #include namespace google { @@ -59,16 +58,8 @@ class MapFieldBaseStub : public MapFieldBase { typedef void DestructorSkippable_; MapFieldBaseStub() {} explicit MapFieldBaseStub(Arena* arena) : MapFieldBase(arena) {} - void SyncRepeatedFieldWithMap() const { - MapFieldBase::SyncRepeatedFieldWithMap(); - } - void SyncMapWithRepeatedField() const { - MapFieldBase::SyncMapWithRepeatedField(); - } // Get underlined repeated field without synchronizing map. - RepeatedPtrField* InternalRepeatedField() { - return repeated_field_; - } + RepeatedPtrField* InternalRepeatedField() { return repeated_field_; } bool IsMapClean() { return state_.load(std::memory_order_relaxed) != STATE_MODIFIED_MAP; } @@ -81,29 +72,27 @@ class MapFieldBaseStub : public MapFieldBase { void SetRepeatedDirty() { state_.store(STATE_MODIFIED_REPEATED, std::memory_order_relaxed); } - bool ContainsMapKey(const MapKey& map_key) const { + bool ContainsMapKey(const MapKey& map_key) const override { return false; } + bool InsertOrLookupMapValue(const MapKey& map_key, + MapValueRef* val) override { return false; } - bool InsertOrLookupMapValue(const MapKey& map_key, MapValueRef* val) { + bool DeleteMapValue(const MapKey& map_key) override { return false; } + bool EqualIterator(const MapIterator& a, + const MapIterator& b) const override { return false; } - bool DeleteMapValue(const MapKey& map_key) { - return false; - } - bool EqualIterator(const MapIterator& a, const MapIterator& b) const { - return false; - } - int size() const { return 0; } - void MapBegin(MapIterator* map_iter) const {} - void MapEnd(MapIterator* map_iter) const {} - void Swap(MapFieldBase* other) {} - void InitializeIterator(MapIterator* map_iter) const {} - void DeleteIterator(MapIterator* map_iter) const {} + int size() const override { return 0; } + void Clear() override {} + void MapBegin(MapIterator* map_iter) const override {} + void MapEnd(MapIterator* map_iter) const override {} + void MergeFrom(const MapFieldBase& other) override {} + void Swap(MapFieldBase* other) override {} + void InitializeIterator(MapIterator* map_iter) const override {} + void DeleteIterator(MapIterator* map_iter) const override {} void CopyIterator(MapIterator* this_iterator, - const MapIterator& other_iterator) const {} - void IncreaseIterator(MapIterator* map_iter) const {} - void SetDefaultMessageEntry(const Message* message) const {} - const Message* GetDefaultMessageEntry() const { return NULL; } + const MapIterator& other_iterator) const override {} + void IncreaseIterator(MapIterator* map_iter) const override {} }; class MapFieldBasePrimitiveTest : public ::testing::Test { @@ -209,8 +198,7 @@ namespace { enum State { CLEAN, MAP_DIRTY, REPEATED_DIRTY }; } // anonymous namespace -class MapFieldStateTest - : public testing::TestWithParam { +class MapFieldStateTest : public testing::TestWithParam { public: protected: typedef unittest::TestMap_MapInt32Int32Entry_DoNotUse EntryType; @@ -294,7 +282,11 @@ class MapFieldStateTest if (is_repeated_null) { EXPECT_TRUE(repeated_field == NULL); } else { - EXPECT_EQ(repeated_size, repeated_field->size()); + if (repeated_field == nullptr) { + EXPECT_EQ(repeated_size, 0); + } else { + EXPECT_EQ(repeated_size, repeated_field->size()); + } } } @@ -303,8 +295,8 @@ class MapFieldStateTest State state_; }; -INSTANTIATE_TEST_CASE_P(MapFieldStateTestInstance, MapFieldStateTest, - ::testing::Values(CLEAN, MAP_DIRTY, REPEATED_DIRTY)); +INSTANTIATE_TEST_SUITE_P(MapFieldStateTestInstance, MapFieldStateTest, + ::testing::Values(CLEAN, MAP_DIRTY, REPEATED_DIRTY)); TEST_P(MapFieldStateTest, GetMap) { map_field_->GetMap(); @@ -441,11 +433,7 @@ TEST_P(MapFieldStateTest, SwapRepeatedDirty) { TEST_P(MapFieldStateTest, Clear) { map_field_->Clear(); - if (state_ != MAP_DIRTY) { - Expect(map_field_.get(), MAP_DIRTY, 0, 1, false); - } else { - Expect(map_field_.get(), MAP_DIRTY, 0, 0, true); - } + Expect(map_field_.get(), MAP_DIRTY, 0, 0, false); } TEST_P(MapFieldStateTest, SpaceUsedExcludingSelf) { diff --git a/src/google/protobuf/map_test.cc b/src/google/protobuf/map_test.cc index beaacc4755..99a7f67411 100644 --- a/src/google/protobuf/map_test.cc +++ b/src/google/protobuf/map_test.cc @@ -69,7 +69,6 @@ #include #include #include -#include #include #include #include @@ -79,6 +78,8 @@ #include +#include + namespace google { namespace protobuf { @@ -124,15 +125,16 @@ class MapImplTest : public ::testing::Test { // Test map size is correct. EXPECT_EQ(value, map_[key]); EXPECT_EQ(1, map_.count(key)); + EXPECT_TRUE(map_.contains(key)); // Check mutable at and find work correctly. EXPECT_EQ(value, map_.at(key)); Map::iterator it = map_.find(key); // iterator dereferenceable - EXPECT_EQ(key, (*it).first); + EXPECT_EQ(key, (*it).first); EXPECT_EQ(value, (*it).second); - EXPECT_EQ(key, it->first); + EXPECT_EQ(key, it->first); EXPECT_EQ(value, it->second); // iterator mutable @@ -242,8 +244,12 @@ TEST_F(MapImplTest, MapKeyAssignment) { EXPECT_EQ("abc", to.GetStringValue()); } -TEST_F(MapImplTest, CountNonExist) { - EXPECT_EQ(0, map_.count(0)); +TEST_F(MapImplTest, CountNonExist) { EXPECT_EQ(0, map_.count(0)); } + +TEST_F(MapImplTest, ContainNotExist) { EXPECT_FALSE(map_.contains(0)); } + +TEST_F(MapImplTest, ImmutableContainNotExist) { + EXPECT_FALSE(const_map_.contains(0)); } TEST_F(MapImplTest, MutableFindNonExist) { @@ -423,8 +429,7 @@ TEST_F(MapImplTest, CopyIteratorStressTest) { } template -static void TestValidityForAllKeysExcept(int key_to_avoid, - const T& check_map, +static void TestValidityForAllKeysExcept(int key_to_avoid, const T& check_map, const U& map) { typedef typename U::value_type value_type; // a key-value pair for (typename U::const_iterator it = map.begin(); it != map.end(); ++it) { @@ -456,7 +461,8 @@ template static void TestOldVersusNewIterator(int skip, Map* m) { const int initial_size = m->size(); IteratorType it = m->begin(); - for (int i = 0; i < skip && it != m->end(); it++, i++) {} + for (int i = 0; i < skip && it != m->end(); it++, i++) { + } if (it == m->end()) return; const IteratorType old = it; GOOGLE_LOG(INFO) << "skip=" << skip << ", old->first=" << old->first; @@ -615,16 +621,13 @@ TEST_F(MapImplTest, IteratorConstness) { } bool IsForwardIteratorHelper(std::forward_iterator_tag /*tag*/) { return true; } -template -bool IsForwardIteratorHelper(T /*t*/) { - return false; -} TEST_F(MapImplTest, IteratorCategory) { EXPECT_TRUE(IsForwardIteratorHelper( std::iterator_traits::iterator>::iterator_category())); - EXPECT_TRUE(IsForwardIteratorHelper(std::iterator_traits< - Map::const_iterator>::iterator_category())); + EXPECT_TRUE(IsForwardIteratorHelper( + std::iterator_traits< + Map::const_iterator>::iterator_category())); } TEST_F(MapImplTest, InsertSingle) { @@ -938,11 +941,10 @@ TEST_F(MapImplTest, SwapBasic) { another[9398] = 41999; another[8070] = 42056; another.swap(map_); - EXPECT_THAT(another, testing::UnorderedElementsAre( - testing::Pair(9398, 41999))); - EXPECT_THAT(map_, testing::UnorderedElementsAre( - testing::Pair(8070, 42056), - testing::Pair(9398, 41999))); + EXPECT_THAT(another, + testing::UnorderedElementsAre(testing::Pair(9398, 41999))); + EXPECT_THAT(map_, testing::UnorderedElementsAre(testing::Pair(8070, 42056), + testing::Pair(9398, 41999))); } TEST_F(MapImplTest, SwapArena) { @@ -955,23 +957,18 @@ TEST_F(MapImplTest, SwapArena) { m2[10244] = 10247; m2[8070] = 42056; m1.swap(map_); - EXPECT_THAT(m1, testing::UnorderedElementsAre( - testing::Pair(9398, 41999))); - EXPECT_THAT(map_, testing::UnorderedElementsAre( - testing::Pair(8070, 42056), - testing::Pair(9398, 41999))); + EXPECT_THAT(m1, testing::UnorderedElementsAre(testing::Pair(9398, 41999))); + EXPECT_THAT(map_, testing::UnorderedElementsAre(testing::Pair(8070, 42056), + testing::Pair(9398, 41999))); m2.swap(m1); - EXPECT_THAT(m1, testing::UnorderedElementsAre( - testing::Pair(8070, 42056), - testing::Pair(10244, 10247))); - EXPECT_THAT(m2, testing::UnorderedElementsAre( - testing::Pair(9398, 41999))); + EXPECT_THAT(m1, testing::UnorderedElementsAre(testing::Pair(8070, 42056), + testing::Pair(10244, 10247))); + EXPECT_THAT(m2, testing::UnorderedElementsAre(testing::Pair(9398, 41999))); } TEST_F(MapImplTest, CopyAssignMapIterator) { TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaMapReflection(&message); MapIterator it1 = reflection_tester.MapBegin(&message, "map_int32_int32"); MapIterator it2 = reflection_tester.MapEnd(&message, "map_int32_int32"); @@ -981,17 +978,15 @@ TEST_F(MapImplTest, CopyAssignMapIterator) { // Map Field Reflection Test ======================================== -static int Func(int i, int j) { - return i * j; -} +static int Func(int i, int j) { return i * j; } -static string StrFunc(int i, int j) { - string str; +static std::string StrFunc(int i, int j) { + std::string str; SStringPrintf(&str, "%d", Func(i, j)); return str; } -static int Int(const string& value) { +static int Int(const std::string& value) { int result = 0; std::istringstream(value) >> result; return result; @@ -1014,7 +1009,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { Map* map_int32_int32 = message.mutable_map_int32_int32(); Map* map_int32_double = message.mutable_map_int32_double(); - Map* map_string_string = message.mutable_map_string_string(); + Map* map_string_string = + message.mutable_map_string_string(); Map* map_int32_foreign_message = message.mutable_map_int32_foreign_message(); @@ -1059,10 +1055,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { refl->GetRepeatedPtrField(message, fd_map_int32_double); const RepeatedPtrField& mf_string_string = refl->GetRepeatedPtrField(message, fd_map_string_string); - const RepeatedPtrField& - mf_int32_foreign_message = - refl->GetRepeatedPtrField( - message, fd_map_int32_foreign_message); + const RepeatedPtrField& mf_int32_foreign_message = + refl->GetRepeatedPtrField(message, fd_map_int32_foreign_message); // Get mutable RepeatedPtrField objects for all fields of interest. RepeatedPtrField* mmf_int32_int32 = @@ -1072,8 +1066,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { RepeatedPtrField* mmf_string_string = refl->MutableRepeatedPtrField(&message, fd_map_string_string); RepeatedPtrField* mmf_int32_foreign_message = - refl->MutableRepeatedPtrField( - &message, fd_map_int32_foreign_message); + refl->MutableRepeatedPtrField(&message, + fd_map_int32_foreign_message); // Make sure we can do gets through the RepeatedPtrField objects. for (int i = 0; i < 10; ++i) { @@ -1095,10 +1089,10 @@ TEST_F(MapFieldReflectionTest, RegularFields) { EXPECT_EQ(value_int32_double, Func(key_int32_double, 2)); const Message& message_string_string = mf_string_string.Get(i); - string key_string_string = + std::string key_string_string = message_string_string.GetReflection()->GetString( message_string_string, fd_map_string_string_key); - string value_string_string = + std::string value_string_string = message_string_string.GetReflection()->GetString( message_string_string, fd_map_string_string_value); EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5)); @@ -1108,9 +1102,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = down_cast( - message_int32_message.GetReflection() - ->GetMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message.GetReflection()->GetMessage( + message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); } @@ -1132,10 +1125,10 @@ TEST_F(MapFieldReflectionTest, RegularFields) { EXPECT_EQ(value_int32_double, Func(key_int32_double, 2)); const Message& message_string_string = mmf_string_string->Get(i); - string key_string_string = + std::string key_string_string = message_string_string.GetReflection()->GetString( message_string_string, fd_map_string_string_key); - string value_string_string = + std::string value_string_string = message_string_string.GetReflection()->GetString( message_string_string, fd_map_string_string_value); EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5)); @@ -1145,9 +1138,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = down_cast( - message_int32_message.GetReflection() - ->GetMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message.GetReflection()->GetMessage( + message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); } } @@ -1170,7 +1162,7 @@ TEST_F(MapFieldReflectionTest, RegularFields) { Func(key_int32_double, -2)); Message* message_string_string = mmf_string_string->Mutable(i); - string key_string_string = + std::string key_string_string = message_string_string->GetReflection()->GetString( *message_string_string, fd_map_string_string_key); message_string_string->GetReflection()->SetString( @@ -1182,9 +1174,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message->GetReflection()->GetInt32( *message_int32_message, fd_map_int32_foreign_message_key); ForeignMessage* value_int32_message = down_cast( - message_int32_message->GetReflection() - ->MutableMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message->GetReflection()->MutableMessage( + message_int32_message, fd_map_int32_foreign_message_value)); value_int32_message->set_c(Func(key_int32_message, -6)); } } @@ -1205,7 +1196,8 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { Map* map_int32_int32 = message.mutable_map_int32_int32(); Map* map_int32_double = message.mutable_map_int32_double(); - Map* map_string_string = message.mutable_map_string_string(); + Map* map_string_string = + message.mutable_map_string_string(); Map* map_int32_foreign_message = message.mutable_map_int32_foreign_message(); @@ -1260,10 +1252,9 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { refl->GetMutableRepeatedFieldRef(&message, fd_map_int32_double); const MutableRepeatedFieldRef mmf_string_string = refl->GetMutableRepeatedFieldRef(&message, fd_map_string_string); - const MutableRepeatedFieldRef - mmf_int32_foreign_message = - refl->GetMutableRepeatedFieldRef( - &message, fd_map_int32_foreign_message); + const MutableRepeatedFieldRef mmf_int32_foreign_message = + refl->GetMutableRepeatedFieldRef(&message, + fd_map_int32_foreign_message); // Get entry default instances std::unique_ptr entry_int32_int32( @@ -1324,10 +1315,10 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { const Message& message_string_string = mf_string_string.Get(i, entry_string_string.get()); - string key_string_string = + std::string key_string_string = message_string_string.GetReflection()->GetString( message_string_string, fd_map_string_string_key); - string value_string_string = + std::string value_string_string = message_string_string.GetReflection()->GetString( message_string_string, fd_map_string_string_value); EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5)); @@ -1338,9 +1329,8 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = down_cast( - message_int32_message.GetReflection() - ->GetMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message.GetReflection()->GetMessage( + message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); } @@ -1365,10 +1355,10 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { const Message& message_string_string = mmf_string_string.Get(i, entry_string_string.get()); - string key_string_string = + std::string key_string_string = message_string_string.GetReflection()->GetString( message_string_string, fd_map_string_string_key); - string value_string_string = + std::string value_string_string = message_string_string.GetReflection()->GetString( message_string_string, fd_map_string_string_value); EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5)); @@ -1379,9 +1369,8 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = down_cast( - message_int32_message.GetReflection() - ->GetMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message.GetReflection()->GetMessage( + message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); } } @@ -1478,19 +1467,20 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { { int index = 0; - std::unordered_map result; + std::unordered_map result; for (RepeatedFieldRef::iterator it = mf_string_string.begin(); it != mf_string_string.end(); ++it) { const Message& message = *it; - string key = + std::string key = message.GetReflection()->GetString(message, fd_map_string_string_key); - string value = message.GetReflection()->GetString( + std::string value = message.GetReflection()->GetString( message, fd_map_string_string_value); result[key] = value; ++index; } EXPECT_EQ(10, index); - for (std::unordered_map::const_iterator it = result.begin(); + for (std::unordered_map::const_iterator it = + result.begin(); it != result.end(); ++it) { EXPECT_EQ(message.map_string_string().at(it->first), it->second); } @@ -1505,9 +1495,9 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { const Message& message = *it; int32 key = message.GetReflection()->GetInt32( message, fd_map_int32_foreign_message_key); - const ForeignMessage& sub_message = down_cast( - message.GetReflection() - ->GetMessage(message, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message = + down_cast(message.GetReflection()->GetMessage( + message, fd_map_int32_foreign_message_value)); result[key].MergeFrom(sub_message); ++index; } @@ -1539,8 +1529,8 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(1234.0, message.map_int32_double().at(4321)); entry_string_string->GetReflection()->SetString( - entry_string_string.get(), - fd_map_string_string->message_type()->field(0), "4321"); + entry_string_string.get(), fd_map_string_string->message_type()->field(0), + "4321"); entry_string_string->GetReflection()->SetString( entry_string_string.get(), fd_map_string_string->message_type()->field(1), "1234"); @@ -1562,12 +1552,13 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(1234, message.map_int32_foreign_message().at(4321).c()); // Test Reflection::AddAllocatedMessage - Message* free_entry_string_string = MessageFactory::generated_factory() - ->GetPrototype(fd_map_string_string->message_type()) - ->New(); + Message* free_entry_string_string = + MessageFactory::generated_factory() + ->GetPrototype(fd_map_string_string->message_type()) + ->New(); entry_string_string->GetReflection()->SetString( - free_entry_string_string, - fd_map_string_string->message_type()->field(0), "4321"); + free_entry_string_string, fd_map_string_string->message_type()->field(0), + "4321"); entry_string_string->GetReflection()->SetString( free_entry_string_string, fd_map_string_string->message_type()->field(1), "1234"); @@ -1634,22 +1625,22 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { { const Message& message0a = mmf_string_string.Get(0, entry_string_string.get()); - string string_value0a = message0a.GetReflection()->GetString( + std::string string_value0a = message0a.GetReflection()->GetString( message0a, fd_map_string_string_value); const Message& message9a = mmf_string_string.Get(9, entry_string_string.get()); - string string_value9a = message9a.GetReflection()->GetString( + std::string string_value9a = message9a.GetReflection()->GetString( message9a, fd_map_string_string_value); mmf_string_string.SwapElements(0, 9); const Message& message0b = mmf_string_string.Get(0, entry_string_string.get()); - string string_value0b = message0b.GetReflection()->GetString( + std::string string_value0b = message0b.GetReflection()->GetString( message0b, fd_map_string_string_value); const Message& message9b = mmf_string_string.Get(9, entry_string_string.get()); - string string_value9b = message9b.GetReflection()->GetString( + std::string string_value9b = message9b.GetReflection()->GetString( message9b, fd_map_string_string_value); EXPECT_EQ(string_value9a, string_value0b); @@ -1659,30 +1650,30 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { { const Message& message0a = mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message0a = down_cast( - message0a.GetReflection() - ->GetMessage(message0a, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message0a = + down_cast(message0a.GetReflection()->GetMessage( + message0a, fd_map_int32_foreign_message_value)); int32 int32_value0a = sub_message0a.c(); const Message& message9a = mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message9a = down_cast( - message9a.GetReflection() - ->GetMessage(message9a, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message9a = + down_cast(message9a.GetReflection()->GetMessage( + message9a, fd_map_int32_foreign_message_value)); int32 int32_value9a = sub_message9a.c(); mmf_int32_foreign_message.SwapElements(0, 9); const Message& message0b = mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message0b = down_cast( - message0b.GetReflection() - ->GetMessage(message0b, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message0b = + down_cast(message0b.GetReflection()->GetMessage( + message0b, fd_map_int32_foreign_message_value)); int32 int32_value0b = sub_message0b.c(); const Message& message9b = mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message9b = down_cast( - message9b.GetReflection() - ->GetMessage(message9b, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message9b = + down_cast(message9b.GetReflection()->GetMessage( + message9b, fd_map_int32_foreign_message_value)); int32 int32_value9b = sub_message9b.c(); EXPECT_EQ(int32_value9a, int32_value0b); @@ -1721,34 +1712,26 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { const FieldDescriptor* fd_map_int32_foreign_message = desc->FindFieldByName("map_int32_foreign_message"); - // Get MutableRepeatedFieldRef objects for all fields of interest. + // Get MutableRepeatedFieldRef objects for all fields of interest. const MutableRepeatedFieldRef mmf_int32_int32 = - refl->GetMutableRepeatedFieldRef( - &m0, fd_map_int32_int32); + refl->GetMutableRepeatedFieldRef(&m0, fd_map_int32_int32); const MutableRepeatedFieldRef mmf_int32_double = - refl->GetMutableRepeatedFieldRef( - &m0, fd_map_int32_double); + refl->GetMutableRepeatedFieldRef(&m0, fd_map_int32_double); const MutableRepeatedFieldRef mmf_string_string = - refl->GetMutableRepeatedFieldRef( - &m0, fd_map_string_string); - const MutableRepeatedFieldRef - mmf_int32_foreign_message = - refl->GetMutableRepeatedFieldRef( - &m0, fd_map_int32_foreign_message); + refl->GetMutableRepeatedFieldRef(&m0, fd_map_string_string); + const MutableRepeatedFieldRef mmf_int32_foreign_message = + refl->GetMutableRepeatedFieldRef(&m0, + fd_map_int32_foreign_message); // Test MutableRepeatedRef::CopyFrom mmf_int32_int32.CopyFrom( - refl->GetRepeatedFieldRef( - m1, fd_map_int32_int32)); + refl->GetRepeatedFieldRef(m1, fd_map_int32_int32)); mmf_int32_double.CopyFrom( - refl->GetRepeatedFieldRef( - m1, fd_map_int32_double)); + refl->GetRepeatedFieldRef(m1, fd_map_int32_double)); mmf_string_string.CopyFrom( - refl->GetRepeatedFieldRef( - m1, fd_map_string_string)); + refl->GetRepeatedFieldRef(m1, fd_map_string_string)); mmf_int32_foreign_message.CopyFrom( - refl->GetRepeatedFieldRef( - m1, fd_map_int32_foreign_message)); + refl->GetRepeatedFieldRef(m1, fd_map_int32_foreign_message)); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 11), m0.map_int32_int32().at(i + 10)); @@ -1759,17 +1742,13 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { // Test MutableRepeatedRef::MergeFrom mmf_int32_int32.MergeFrom( - refl->GetRepeatedFieldRef( - m2, fd_map_int32_int32)); + refl->GetRepeatedFieldRef(m2, fd_map_int32_int32)); mmf_int32_double.MergeFrom( - refl->GetRepeatedFieldRef( - m2, fd_map_int32_double)); + refl->GetRepeatedFieldRef(m2, fd_map_int32_double)); mmf_string_string.MergeFrom( - refl->GetRepeatedFieldRef( - m2, fd_map_string_string)); + refl->GetRepeatedFieldRef(m2, fd_map_string_string)); mmf_int32_foreign_message.MergeFrom( - refl->GetRepeatedFieldRef( - m2, fd_map_int32_foreign_message)); + refl->GetRepeatedFieldRef(m2, fd_map_int32_foreign_message)); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20)); EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20)); @@ -1780,17 +1759,13 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { // Test MutableRepeatedRef::Swap // Swap between m0 and m2. mmf_int32_int32.Swap( - refl->GetMutableRepeatedFieldRef( - &m2, fd_map_int32_int32)); + refl->GetMutableRepeatedFieldRef(&m2, fd_map_int32_int32)); mmf_int32_double.Swap( - refl->GetMutableRepeatedFieldRef( - &m2, fd_map_int32_double)); + refl->GetMutableRepeatedFieldRef(&m2, fd_map_int32_double)); mmf_string_string.Swap( - refl->GetMutableRepeatedFieldRef( - &m2, fd_map_string_string)); - mmf_int32_foreign_message.Swap( - refl->GetMutableRepeatedFieldRef( - &m2, fd_map_int32_foreign_message)); + refl->GetMutableRepeatedFieldRef(&m2, fd_map_string_string)); + mmf_int32_foreign_message.Swap(refl->GetMutableRepeatedFieldRef( + &m2, fd_map_int32_foreign_message)); for (int i = 0; i < 10; ++i) { // Check the content of m0. EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20)); @@ -1977,7 +1952,7 @@ TEST(GeneratedMapFieldTest, CopyAssignmentOperator) { MapTestUtil::ExpectMapFieldsSet(message2); } -#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || GOOGLE_PROTOBUF_RTTI +#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || PROTOBUF_RTTI TEST(GeneratedMapFieldTest, UpcastCopyFrom) { // Test the CopyFrom method that takes in the generic const Message& // parameter. @@ -1985,7 +1960,7 @@ TEST(GeneratedMapFieldTest, UpcastCopyFrom) { MapTestUtil::SetMapFields(&message1); - const Message* source = ::google::protobuf::implicit_cast(&message1); + const Message* source = implicit_cast(&message1); message2.CopyFrom(*source); MapTestUtil::ExpectMapFieldsSet(message2); @@ -2002,10 +1977,8 @@ TEST(GeneratedMapFieldTest, CopyFromDynamicMessage) { // Construct a new version of the dynamic message via the factory. DynamicMessageFactory factory; std::unique_ptr message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaReflection(message1.get()); reflection_tester.ExpectMapFieldsSetViaReflection(*message1); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get()); @@ -2019,10 +1992,8 @@ TEST(GeneratedMapFieldTest, CopyFromDynamicMessageMapReflection) { // Construct a new version of the dynamic message via the factory. DynamicMessageFactory factory; std::unique_ptr message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaMapReflection(message1.get()); reflection_tester.ExpectMapFieldsSetViaReflection(*message1); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get()); @@ -2030,6 +2001,38 @@ TEST(GeneratedMapFieldTest, CopyFromDynamicMessageMapReflection) { MapTestUtil::ExpectMapFieldsSet(message2); } +TEST(GeneratedMapFieldTest, DynamicMessageMergeFromDynamicMessage) { + // Construct two dynamic message and sets via map reflection. + DynamicMessageFactory factory; + std::unique_ptr message1; + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); + reflection_tester.SetMapFieldsViaMapReflection(message1.get()); + + // message2 is created by same factory. + std::unique_ptr message2; + message2.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); + reflection_tester.SetMapFieldsViaMapReflection(message2.get()); + + // message3 is created by different factory. + DynamicMessageFactory factory3; + std::unique_ptr message3; + message3.reset(factory3.GetPrototype(unittest::TestMap::descriptor())->New()); + reflection_tester.SetMapFieldsViaMapReflection(message3.get()); + + message2->MergeFrom(*message1); + message3->MergeFrom(*message1); + + // Test MergeFrom does not sync to repeated fields and + // there is no duplicate keys in text format. + std::string output1, output2, output3; + TextFormat::PrintToString(*message1, &output1); + TextFormat::PrintToString(*message2, &output2); + TextFormat::PrintToString(*message3, &output3); + EXPECT_EQ(output1, output2); + EXPECT_EQ(output1, output3); +} + TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) { // Test copying to a DynamicMessage, which must fall back to using reflection. unittest::TestMap message2; @@ -2038,27 +2041,23 @@ TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) { // Construct a new version of the dynamic message via the factory. DynamicMessageFactory factory; std::unique_ptr message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); message1->MergeFrom(message2); reflection_tester.ExpectMapFieldsSetViaReflection(*message1); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get()); } TEST(GeneratedMapFieldTest, DynamicMessageCopyFromMapReflection) { - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); unittest::TestMap message2; reflection_tester.SetMapFieldsViaMapReflection(&message2); // Construct a dynamic message via the factory. DynamicMessageFactory factory; std::unique_ptr message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); message1->MergeFrom(message2); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get()); @@ -2067,12 +2066,10 @@ TEST(GeneratedMapFieldTest, DynamicMessageCopyFromMapReflection) { TEST(GeneratedMapFieldTest, SyncDynamicMapWithRepeatedField) { // Construct a dynamic message via the factory. - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); DynamicMessageFactory factory; std::unique_ptr message; - message.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); + message.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); reflection_tester.SetMapFieldsViaReflection(message.get()); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message.get()); reflection_tester.ExpectMapFieldsSetViaReflection(*message); @@ -2095,6 +2092,18 @@ TEST(GeneratedMapFieldTest, NonEmptyMergeFrom) { message1.MergeFrom(message2); MapTestUtil::ExpectMapFieldsSet(message1); + + // Test reflection MergeFrom does not sync to repeated field + // and there is no duplicated keys. + MapTestUtil::SetMapFields(&message1); + MapTestUtil::SetMapFields(&message2); + + message2.MergeFrom(message1); + + std::string output1, output2; + TextFormat::PrintToString(message1, &output1); + TextFormat::PrintToString(message2, &output2); + EXPECT_EQ(output1, output2); } TEST(GeneratedMapFieldTest, MergeFromMessageMap) { @@ -2113,7 +2122,7 @@ TEST(GeneratedMapFieldTest, MergeFromMessageMap) { // Test the generated SerializeWithCachedSizesToArray() TEST(GeneratedMapFieldTest, SerializationToArray) { unittest::TestMap message1, message2; - string data; + std::string data; MapTestUtil::SetMapFields(&message1); int size = message1.ByteSize(); data.resize(size); @@ -2129,7 +2138,7 @@ TEST(GeneratedMapFieldTest, SerializationToStream) { unittest::TestMap message1, message2; MapTestUtil::SetMapFields(&message1); int size = message1.ByteSize(); - string data; + std::string data; data.resize(size); { // Allow the output stream to buffer only one byte at a time. @@ -2165,7 +2174,7 @@ TEST(GeneratedMapFieldTest, Proto2UnknownEnum) { unittest::TestEnumMapPlusExtra from; (*from.mutable_known_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_FOO; (*from.mutable_unknown_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_EXTRA; - string data; + std::string data; from.SerializeToString(&data); unittest::TestEnumMap to; @@ -2190,7 +2199,7 @@ TEST(GeneratedMapFieldTest, Proto2UnknownEnum) { TEST(GeneratedMapFieldTest, StandardWireFormat) { unittest::TestMap message; - string data = "\x0A\x04\x08\x01\x10\x01"; + std::string data = "\x0A\x04\x08\x01\x10\x01"; EXPECT_TRUE(message.ParseFromString(data)); EXPECT_EQ(1, message.map_int32_int32().size()); @@ -2201,10 +2210,11 @@ TEST(GeneratedMapFieldTest, UnorderedWireFormat) { unittest::TestMap message; // put value before key in wire format - string data = "\x0A\x04\x10\x01\x08\x02"; + std::string data = "\x0A\x04\x10\x01\x08\x02"; EXPECT_TRUE(message.ParseFromString(data)); EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_NE(message.map_int32_int32().find(2), message.map_int32_int32().end()); EXPECT_EQ(1, message.map_int32_int32().at(2)); } @@ -2212,7 +2222,7 @@ TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) { unittest::TestMap message; // Two key fields in wire format - string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; + std::string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; EXPECT_TRUE(message.ParseFromString(data)); EXPECT_EQ(1, message.map_int32_int32().size()); @@ -2229,14 +2239,14 @@ TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) { with_dummy4.set_c(0); with_dummy4.set_dummy4(11); (*map_message.mutable_map_field())[key] = with_dummy4; - string s = map_message.SerializeAsString(); + std::string s = map_message.SerializeAsString(); unittest::TestRequired with_dummy5; with_dummy5.set_a(0); with_dummy5.set_b(0); with_dummy5.set_c(0); with_dummy5.set_dummy5(12); (*map_message.mutable_map_field())[key] = with_dummy5; - string both = s + map_message.SerializeAsString(); + std::string both = s + map_message.SerializeAsString(); // We don't expect a merge now. The "second one wins." ASSERT_TRUE(map_message.ParseFromString(both)); ASSERT_EQ(1, map_message.map_field().size()); @@ -2260,7 +2270,7 @@ TEST(GeneratedMapFieldTest, KeysValuesUnknownsWireFormat) { const char kValueTag = 0x10; const char kJunkTag = 0x20; for (int items = 0; items <= kMaxNumKeysAndValuesAndJunk; items++) { - string data = "\x0A"; + std::string data = "\x0A"; // Encode length of what will follow. data.push_back(items * 2); static const int kBitsOfIPerItem = 4; @@ -2268,22 +2278,33 @@ TEST(GeneratedMapFieldTest, KeysValuesUnknownsWireFormat) { // Each iteration of the following is a test. It uses i as bit vector // encoding the keys and values to put in the wire format. for (int i = 0; i < (1 << (items * kBitsOfIPerItem)); i++) { - string wire_format = data; + std::string wire_format = data; int expected_key = 0; int expected_value = 0; for (int k = i, j = 0; j < items; j++, k >>= kBitsOfIPerItem) { bool is_key = k & 0x1; bool is_value = !is_key && (k & 0x2); - wire_format.push_back(is_key ? kKeyTag : - is_value ? kValueTag : kJunkTag); + wire_format.push_back(is_key ? kKeyTag + : is_value ? kValueTag : kJunkTag); char c = static_cast(k & mask) >> 2; // One char after the tag. wire_format.push_back(c); if (is_key) expected_key = static_cast(c); if (is_value) expected_value = static_cast(c); - ASSERT_TRUE(message.ParseFromString(wire_format)); - ASSERT_EQ(1, message.map_int32_int32().size()); - ASSERT_EQ(expected_key, message.map_int32_int32().begin()->first); - ASSERT_EQ(expected_value, message.map_int32_int32().begin()->second); + bool res = message.ParseFromString(wire_format); + bool expect_success = true; +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + // Unfortunately the old map parser accepts malformed input, the new + // parser accepts only correct input. + if (j != items - 1) expect_success = false; +#endif + if (expect_success) { + ASSERT_TRUE(res); + ASSERT_EQ(1, message.map_int32_int32().size()); + ASSERT_EQ(expected_key, message.map_int32_int32().begin()->first); + ASSERT_EQ(expected_value, message.map_int32_int32().begin()->second); + } else { + ASSERT_FALSE(res); + } } } } @@ -2293,7 +2314,7 @@ TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) { unittest::TestMap message; // Two value fields in wire format - string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; + std::string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; EXPECT_TRUE(message.ParseFromString(data)); EXPECT_EQ(1, message.map_int32_int32().size()); @@ -2304,10 +2325,11 @@ TEST(GeneratedMapFieldTest, MissedKeyWireFormat) { unittest::TestMap message; // No key field in wire format - string data = "\x0A\x02\x10\x01"; + std::string data = "\x0A\x02\x10\x01"; EXPECT_TRUE(message.ParseFromString(data)); EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_NE(message.map_int32_int32().find(0), message.map_int32_int32().end()); EXPECT_EQ(1, message.map_int32_int32().at(0)); } @@ -2315,10 +2337,11 @@ TEST(GeneratedMapFieldTest, MissedValueWireFormat) { unittest::TestMap message; // No value field in wire format - string data = "\x0A\x02\x08\x01"; + std::string data = "\x0A\x02\x08\x01"; EXPECT_TRUE(message.ParseFromString(data)); EXPECT_EQ(1, message.map_int32_int32().size()); + ASSERT_NE(message.map_int32_int32().find(1), message.map_int32_int32().end()); EXPECT_EQ(0, message.map_int32_int32().at(1)); } @@ -2326,7 +2349,7 @@ TEST(GeneratedMapFieldTest, MissedValueTextFormat) { unittest::TestMap message; // No value field in text format - string text = + std::string text = "map_int32_foreign_message {\n" " key: 1234567890\n" "}"; @@ -2340,7 +2363,7 @@ TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) { unittest::TestMap message; // Unknown field in wire format - string data = "\x0A\x06\x08\x02\x10\x03\x18\x01"; + std::string data = "\x0A\x06\x08\x02\x10\x03\x18\x01"; EXPECT_TRUE(message.ParseFromString(data)); EXPECT_EQ(1, message.map_int32_int32().size()); @@ -2351,7 +2374,7 @@ TEST(GeneratedMapFieldTest, CorruptedWireFormat) { unittest::TestMap message; // corrupted data in wire format - string data = "\x0A\x06\x08\x02\x11\x03"; + std::string data = "\x0A\x06\x08\x02\x11\x03"; EXPECT_FALSE(message.ParseFromString(data)); } @@ -2381,14 +2404,14 @@ TEST(GeneratedMapFieldTest, MessagesMustMerge) { EXPECT_TRUE(with_dummy4.IsInitialized()); (*map_message.mutable_map_field())[0] = with_dummy4; EXPECT_TRUE(map_message.IsInitialized()); - string s = map_message.SerializeAsString(); + std::string s = map_message.SerializeAsString(); // Modify s so that there are two values in the entry for key 0. // The first will have no value for c. The second will have no value for a. // Those are required fields. Also, make some other little changes, to // ensure we are merging the two values (because they're messages). ASSERT_EQ(s.size() - 2, s[1]); // encoding of the length of what follows - string encoded_val(s.data() + 4, s.data() + s.size()); + std::string encoded_val(s.data() + 4, s.data() + s.size()); // In s, change the encoding of c to an encoding of dummy32. s[s.size() - 3] -= 8; // Make encoded_val slightly different from what's in s. @@ -2424,10 +2447,10 @@ TEST(GeneratedMapFieldTest, MessagesMustMerge) { EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5()); // Test key then value then value then key. - s.push_back(s[2]); // Copy the key's tag. + s.push_back(s[2]); // Copy the key's tag. key = 19; - s.push_back(key); // Second key is 19 instead of 0. - s[1] += 2; // Adjust encoded size. + s.push_back(key); // Second key is 19 instead of 0. + s[1] += 2; // Adjust encoded size. ASSERT_TRUE(map_message.ParseFromString(s)); ASSERT_EQ(1, map_message.map_field().size()); ASSERT_EQ(1, map_message.map_field().count(key)); @@ -2442,8 +2465,7 @@ TEST(GeneratedMapFieldTest, MessagesMustMerge) { TEST(GeneratedMapFieldReflectionTest, SpaceUsed) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaReflection(&message); EXPECT_LT(0, message.GetReflection()->SpaceUsed(message)); @@ -2453,8 +2475,7 @@ TEST(GeneratedMapFieldReflectionTest, Accessors) { // Set every field to a unique value then go back and check all those // values. unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaReflection(&message); MapTestUtil::ExpectMapFieldsSet(message); reflection_tester.ExpectMapFieldsSetViaReflection(message); @@ -2512,8 +2533,7 @@ TEST(GeneratedMapFieldReflectionTest, ClearField) { MapTestUtil::SetMapFields(&message); MapTestUtil::ExpectMapFieldsSet(message); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.ClearMapFieldsViaReflection(&message); reflection_tester.ExpectClearViaReflection(message); reflection_tester.ExpectClearViaReflectionIterator(&message); @@ -2521,8 +2541,7 @@ TEST(GeneratedMapFieldReflectionTest, ClearField) { TEST(GeneratedMapFieldReflectionTest, RemoveLast) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); MapTestUtil::SetMapFields(&message); MapTestUtil::ExpectMapsSize(message, 2); @@ -2569,8 +2588,7 @@ TEST(GeneratedMapFieldReflectionTest, ReleaseLast) { TEST(GeneratedMapFieldReflectionTest, SwapElements) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); MapTestUtil::SetMapFields(&message); @@ -2609,8 +2627,7 @@ TEST(GeneratedMapFieldReflectionTest, SwapElements) { TEST(GeneratedMapFieldReflectionTest, MutableUnknownFields) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message); } @@ -2665,8 +2682,7 @@ TEST(GeneratedMapFieldReflectionTest, MergeFromClearMapEntry) { TEST(GeneratedMapFieldReflectionTest, MapEntryClear) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message); } @@ -2687,8 +2703,7 @@ TEST(GeneratedMapFieldReflectionTest, Proto2MapEntryClear) { TEST(GeneratedMapFieldReflectionTest, SetViaMapReflection) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaMapReflection(&message); reflection_tester.ExpectMapFieldsSetViaReflection(message); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message); @@ -2708,8 +2723,7 @@ class MapFieldInDynamicMessageTest : public testing::Test { : pool_(DescriptorPool::generated_pool()), factory_(pool_) {} virtual void SetUp() { - map_descriptor_ = - pool_->FindMessageTypeByName("protobuf_unittest.TestMap"); + map_descriptor_ = pool_->FindMessageTypeByName("protobuf_unittest.TestMap"); recursive_map_descriptor_ = pool_->FindMessageTypeByName("protobuf_unittest.TestRecursiveMapMessage"); ASSERT_TRUE(map_descriptor_ != NULL); @@ -2757,7 +2771,7 @@ TEST_F(MapFieldInDynamicMessageTest, MapSpaceUsed) { TEST_F(MapFieldInDynamicMessageTest, RecursiveMap) { TestRecursiveMapMessage from; (*from.mutable_a())[""]; - string data = from.SerializeAsString(); + std::string data = from.SerializeAsString(); std::unique_ptr to( factory_.GetPrototype(recursive_map_descriptor_)->New()); ASSERT_TRUE(to->ParseFromString(data)); @@ -2780,7 +2794,7 @@ TEST_F(MapFieldInDynamicMessageTest, MapValueReferernceValidAfterSerialize) { // In previous implementation, calling SerializeToString will cause syncing // from map to repeated field, which will invalidate the submsg we previously // got. - string data; + std::string data; message->SerializeToString(&data); const Reflection* submsg_reflection = submsg->GetReflection(); @@ -2812,7 +2826,7 @@ TEST_F(MapFieldInDynamicMessageTest, MapEntryReferernceValidAfterSerialize) { // In previous implementation, calling SerializeToString will cause syncing // from repeated field to map, which will invalidate the map_entry we // previously got. - string data; + std::string data; message->SerializeToString(&data); const Reflection* submsg_reflection = submsg->GetReflection(); @@ -2877,15 +2891,15 @@ TEST(ReflectionOpsForMapFieldTest, MapDiscardUnknownFields) { MapTestUtil::SetMapFields(&message); // Set some unknown fields in message. - message.GetReflection()->MutableUnknownFields(&message)-> - AddVarint(123456, 654321); + message.GetReflection()->MutableUnknownFields(&message)->AddVarint(123456, + 654321); // Discard them. ReflectionOps::DiscardUnknownFields(&message); MapTestUtil::ExpectMapFieldsSet(message); - EXPECT_EQ(0, message.GetReflection()-> - GetUnknownFields(message).field_count()); + EXPECT_EQ(0, + message.GetReflection()->GetUnknownFields(message).field_count()); } TEST(ReflectionOpsForMapFieldTest, IsInitialized) { @@ -2906,7 +2920,7 @@ TEST(ReflectionOpsForMapFieldTest, IsInitialized) { TEST(WireFormatForMapFieldTest, ParseMap) { unittest::TestMap source, dest; - string data; + std::string data; // Serialize using the generated code. MapTestUtil::SetMapFields(&source); @@ -2933,8 +2947,8 @@ TEST(WireFormatForMapFieldTest, MapByteSize) { TEST(WireFormatForMapFieldTest, SerializeMap) { unittest::TestMap message; - string generated_data; - string dynamic_data; + std::string generated_data; + std::string dynamic_data; MapTestUtil::SetMapFields(&message); @@ -2967,8 +2981,7 @@ TEST(WireFormatForMapFieldTest, SerializeMapDynamicMessage) { std::unique_ptr dynamic_message; dynamic_message.reset( factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaReflection(dynamic_message.get()); reflection_tester.ExpectMapFieldsSetViaReflection(*dynamic_message); @@ -2976,8 +2989,8 @@ TEST(WireFormatForMapFieldTest, SerializeMapDynamicMessage) { MapTestUtil::SetMapFields(&generated_message); MapTestUtil::ExpectMapFieldsSet(generated_message); - string generated_data; - string dynamic_data; + std::string generated_data; + std::string dynamic_data; // Serialize. generated_message.SerializeToString(&generated_data); @@ -2990,7 +3003,7 @@ TEST(WireFormatForMapFieldTest, SerializeMapDynamicMessage) { } TEST(WireFormatForMapFieldTest, MapParseHelpers) { - string data; + std::string data; { // Set up. @@ -3017,7 +3030,7 @@ TEST(WireFormatForMapFieldTest, MapParseHelpers) { { // Test ParseFromBoundedZeroCopyStream. - string data_with_junk(data); + std::string data_with_junk(data); data_with_junk.append("some junk on the end"); io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size()); protobuf_unittest::TestMap message; @@ -3031,17 +3044,17 @@ TEST(WireFormatForMapFieldTest, MapParseHelpers) { io::ArrayInputStream stream(data.data(), data.size()); protobuf_unittest::TestAllTypes message; EXPECT_FALSE( - message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); + message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); } } // Deterministic Serialization Test ========================================== template -static string DeterministicSerializationWithSerializePartialToCodedStream( +static std::string DeterministicSerializationWithSerializePartialToCodedStream( const T& t) { const int size = t.ByteSize(); - string result(size, '\0'); + std::string result(size, '\0'); io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&result), size); io::CodedOutputStream output_stream(&array_stream); output_stream.SetSerializationDeterministic(true); @@ -3052,9 +3065,10 @@ static string DeterministicSerializationWithSerializePartialToCodedStream( } template -static string DeterministicSerializationWithSerializeToCodedStream(const T& t) { +static std::string DeterministicSerializationWithSerializeToCodedStream( + const T& t) { const int size = t.ByteSize(); - string result(size, '\0'); + std::string result(size, '\0'); io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&result), size); io::CodedOutputStream output_stream(&array_stream); output_stream.SetSerializationDeterministic(true); @@ -3065,15 +3079,17 @@ static string DeterministicSerializationWithSerializeToCodedStream(const T& t) { } template -static string DeterministicSerialization(const T& t) { +static std::string DeterministicSerialization(const T& t) { const int size = t.ByteSize(); - string result(size, '\0'); + std::string result(size, '\0'); io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&result), size); - io::CodedOutputStream output_stream(&array_stream); - output_stream.SetSerializationDeterministic(true); - t.SerializeWithCachedSizes(&output_stream); - EXPECT_FALSE(output_stream.HadError()); - EXPECT_EQ(size, output_stream.ByteCount()); + { + io::CodedOutputStream output_stream(&array_stream); + output_stream.SetSerializationDeterministic(true); + t.SerializeWithCachedSizes(&output_stream); + EXPECT_FALSE(output_stream.HadError()); + EXPECT_EQ(size, output_stream.ByteCount()); + } EXPECT_EQ(result, DeterministicSerializationWithSerializeToCodedStream(t)); EXPECT_EQ(result, DeterministicSerializationWithSerializePartialToCodedStream(t)); @@ -3082,12 +3098,12 @@ static string DeterministicSerialization(const T& t) { // Helper to test the serialization of the first arg against a golden file. static void TestDeterministicSerialization(const protobuf_unittest::TestMaps& t, - const string& filename) { - string expected; + const std::string& filename) { + std::string expected; GOOGLE_CHECK_OK(File::GetContents( TestUtil::GetTestDataPath("net/proto2/internal/testdata/" + filename), &expected, true)); - const string actual = DeterministicSerialization(t); + const std::string actual = DeterministicSerialization(t); EXPECT_EQ(expected, actual); protobuf_unittest::TestMaps u; EXPECT_TRUE(u.ParseFromString(actual)); @@ -3095,8 +3111,8 @@ static void TestDeterministicSerialization(const protobuf_unittest::TestMaps& t, } // Helper for MapSerializationTest. Return a 7-bit ASCII string. -static string ConstructKey(uint64 n) { - string s(n % static_cast(9), '\0'); +static std::string ConstructKey(uint64 n) { + std::string s(n % static_cast(9), '\0'); if (s.empty()) { return StrCat(n); } else { @@ -3122,7 +3138,7 @@ TEST(MapSerializationTest, Deterministic) { const int64 i64 = static_cast(frog); const uint64 u64 = frog * static_cast(187321); const bool b = i32 > 0; - const string s = ConstructKey(frog); + const std::string s = ConstructKey(frog); (*inner.mutable_m())[i] = i32; (*t.mutable_m_int32())[i32] = (*t.mutable_m_sint32())[i32] = (*t.mutable_m_sfixed32())[i32] = inner; @@ -3132,8 +3148,8 @@ TEST(MapSerializationTest, Deterministic) { (*t.mutable_m_uint64())[u64] = (*t.mutable_m_fixed64())[u64] = inner; (*t.mutable_m_bool())[b] = inner; (*t.mutable_m_string())[s] = inner; - (*t.mutable_m_string())[s + string(1 << (u32 % static_cast(9)), - b)] = inner; + (*t.mutable_m_string())[s + std::string(1 << (u32 % static_cast(9)), + b)] = inner; inner.mutable_m()->erase(i); frog = frog * multiplier + i; frog ^= (frog >> 41); @@ -3144,14 +3160,14 @@ TEST(MapSerializationTest, Deterministic) { TEST(MapSerializationTest, DeterministicSubmessage) { protobuf_unittest::TestSubmessageMaps p; protobuf_unittest::TestMaps t; - const string filename = "golden_message_maps"; - string golden; + const std::string filename = "golden_message_maps"; + std::string golden; GOOGLE_CHECK_OK(File::GetContents( TestUtil::GetTestDataPath("net/proto2/internal/testdata/" + filename), &golden, true)); t.ParseFromString(golden); *(p.mutable_m()) = t; - std::vector v; + std::vector v; // Use multiple attempts to increase the chance of a failure if something is // buggy. For example, each separate copy of a map might use a different // randomly-chosen hash function. @@ -3168,7 +3184,7 @@ TEST(TextFormatMapTest, SerializeAndParse) { unittest::TestMap source; unittest::TestMap dest; MapTestUtil::SetMapFields(&source); - string output; + std::string output; // Test compact ASCII TextFormat::Printer printer; @@ -3178,12 +3194,29 @@ TEST(TextFormatMapTest, SerializeAndParse) { MapTestUtil::ExpectMapFieldsSet(dest); } +TEST(TextFormatMapTest, DynamicMessage) { + TestMap prototype; + DynamicMessageFactory factory; + std::unique_ptr message( + factory.GetPrototype(prototype.GetDescriptor())->New()); + MapReflectionTester tester(message->GetDescriptor()); + tester.SetMapFieldsViaReflection(message.get()); + + std::string expected_text; + GOOGLE_CHECK_OK( + File::GetContents(TestUtil::GetTestDataPath("net/proto2/internal/" + "testdata/map_test_data.txt"), + &expected_text, true)); + + EXPECT_EQ(message->DebugString(), expected_text); +} + TEST(TextFormatMapTest, Sorted) { unittest::TestMap message; MapReflectionTester tester(message.GetDescriptor()); tester.SetMapFieldsViaReflection(&message); - string expected_text; + std::string expected_text; GOOGLE_CHECK_OK( File::GetContents(TestUtil::GetTestDataPath("net/proto2/internal/" "testdata/map_test_data.txt"), @@ -3200,7 +3233,7 @@ TEST(TextFormatMapTest, Sorted) { } TEST(TextFormatMapTest, ParseCorruptedString) { - string serialized_message; + std::string serialized_message; GOOGLE_CHECK_OK( File::GetContents(TestUtil::GetTestDataPath( "net/proto2/internal/testdata/golden_message_maps"), @@ -3211,6 +3244,77 @@ TEST(TextFormatMapTest, ParseCorruptedString) { TestParseCorruptedString(message); } +// Previously, serializing to text format will disable iterator from generated +// API. Now, the iterator can be still used even after serializing to text +// format. +TEST(TextFormatMapTest, NoDisableIterator) { + unittest::TestMap source; + (*source.mutable_map_int32_int32())[1] = 1; + + // Get iterator. + Map::iterator iter = source.mutable_map_int32_int32()->find(1); + + // Serialize message to text format, which will invalidate the previous + // iterator previously. + std::string output; + TextFormat::Printer printer; + printer.PrintToString(source, &output); + + // Modify map via the iterator (invalidated in prvious implementation.). + iter->second = 2; + + // In previous implementation, the new change won't be reflected in text + // format, because the previous iterator has been invalidated. + output.clear(); + printer.PrintToString(source, &output); + std::string expected = + "map_int32_int32 {\n" + " key: 1\n" + " value: 2\n" + "}\n"; + EXPECT_EQ(output, expected); +} + +// Previously, serializing to text format will disable iterator from reflection +// API. +TEST(TextFormatMapTest, NoDisableReflectionIterator) { + unittest::TestMap source; + (*source.mutable_map_int32_int32())[1] = 1; + + // Get iterator. This will also sync internal repeated field with map inside + // of MapField. + const Reflection* reflection = source.GetReflection(); + const FieldDescriptor* field_desc = + source.GetDescriptor()->FindFieldByName("map_int32_int32"); + RepeatedPtrField* map_field = + reflection->MutableRepeatedPtrField(&source, field_desc); + RepeatedPtrField::iterator iter = map_field->begin(); + + // Serialize message to text format, which will invalidate the prvious + // iterator previously. + std::string output; + TextFormat::Printer printer; + printer.PrintToString(source, &output); + + // Modify map via the iterator (invalidated in prvious implementation.). + const Reflection* map_entry_reflection = iter->GetReflection(); + const FieldDescriptor* value_field_desc = + iter->GetDescriptor()->FindFieldByName("value"); + map_entry_reflection->SetInt32(&(*iter), value_field_desc, 2); + GOOGLE_LOG(INFO) << iter->DebugString(); + + // In previous implementation, the new change won't be reflected in text + // format, because the previous iterator has been invalidated. + output.clear(); + printer.PrintToString(source, &output); + std::string expected = + "map_int32_int32 {\n" + " key: 1\n" + " value: 2\n" + "}\n"; + EXPECT_EQ(output, expected); +} + // arena support ================================================= TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { @@ -3220,7 +3324,7 @@ TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { options.initial_block = &arena_block[0]; options.initial_block_size = arena_block.size(); Arena arena(options); - string data; + std::string data; data.reserve(128 * 1024); { @@ -3242,7 +3346,7 @@ TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { // Use text format parsing and serializing to test reflection api. TEST(ArenaTest, ReflectionInTextFormat) { Arena arena; - string data; + std::string data; TextFormat::Printer printer; TextFormat::Parser parser; @@ -3264,7 +3368,7 @@ TEST(ArenaTest, StringMapNoLeak) { Arena arena; unittest::TestArenaMap* message = Arena::CreateMessage(&arena); - string data; + std::string data; // String with length less than 16 will not be allocated from heap. int original_capacity = data.capacity(); while (data.capacity() <= original_capacity) { diff --git a/src/google/protobuf/map_test_util.cc b/src/google/protobuf/map_test_util.cc deleted file mode 100644 index 31ac173633..0000000000 --- a/src/google/protobuf/map_test_util.cc +++ /dev/null @@ -1,1829 +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 { - -void MapTestUtil::SetMapFields(unittest::TestMap* message) { - MapTestUtilImpl::SetMapFields(message); -} - -void MapTestUtil::SetArenaMapFields(unittest::TestArenaMap* message) { - MapTestUtilImpl::SetArenaMapFields(message); -} - -void MapTestUtil::SetMapFieldsInitialized(unittest::TestMap* message) { - MapTestUtilImpl::SetMapFieldsInitialized(message); -} - -void MapTestUtil::ModifyMapFields(unittest::TestMap* message) { - MapTestUtilImpl::ModifyMapFields( - message); -} - -void MapTestUtil::ExpectClear(const unittest::TestMap& message) { - MapTestUtilImpl::ExpectClear(message); -} - -void MapTestUtil::ExpectMapFieldsSet(const unittest::TestMap& message) { - MapTestUtilImpl::ExpectMapFieldsSet(message); -} - -void MapTestUtil::ExpectArenaMapFieldsSet( - const unittest::TestArenaMap& message) { - MapTestUtilImpl::ExpectArenaMapFieldsSet< - unittest::MapEnum, unittest::MAP_ENUM_BAR, unittest::MAP_ENUM_BAZ>( - message); -} - -void MapTestUtil::ExpectMapFieldsSetInitialized( - const unittest::TestMap& message) { - MapTestUtilImpl::ExpectMapFieldsSetInitialized( - message); -} - -void MapTestUtil::ExpectMapFieldsModified( - const unittest::TestMap& message) { - MapTestUtilImpl::ExpectMapFieldsModified< - unittest::MapEnum, unittest::MAP_ENUM_BAR, unittest::MAP_ENUM_FOO>( - message); -} - -void MapTestUtil::ExpectMapsSize( - const unittest::TestMap& message, int size) { - const Descriptor* descriptor = message.GetDescriptor(); - - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_int32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int64_int64"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_uint32_uint32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_uint64_uint64"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_sint32_sint32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_sint64_sint64"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_fixed32_fixed32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_fixed64_fixed64"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_float"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_double"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_bool_bool"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_string_string"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_bytes"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_foreign_message"))); -} - -std::vector MapTestUtil::GetMapEntries( - const unittest::TestMap& message, int index) { - const Descriptor* descriptor = message.GetDescriptor(); - std::vector result; - - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_int32"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int64_int64"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_uint32_uint32"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_uint64_uint64"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_sint32_sint32"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_sint64_sint64"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_fixed32_fixed32"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_fixed64_fixed64"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_sfixed32_sfixed32"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_sfixed64_sfixed64"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_float"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_double"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_bool_bool"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_string_string"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_bytes"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_enum"), index)); - result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_foreign_message"), index)); - - return result; -} - -std::vector MapTestUtil::GetMapEntriesFromRelease( - unittest::TestMap* message) { - const Descriptor* descriptor = message->GetDescriptor(); - std::vector result; - - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_int32"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int64_int64"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_uint32_uint32"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_uint64_uint64"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_sint32_sint32"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_sint64_sint64"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_fixed32_fixed32"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_fixed64_fixed64"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_float"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_double"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_bool_bool"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_string_string"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_bytes"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_enum"))); - result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_foreign_message"))); - - return result; -} - -MapReflectionTester::MapReflectionTester( - const Descriptor* base_descriptor) - : base_descriptor_(base_descriptor) { - const DescriptorPool* pool = base_descriptor->file()->pool(); - - map_enum_foo_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_FOO"); - map_enum_bar_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_BAR"); - map_enum_baz_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_BAZ"); - - foreign_c_ = pool->FindFieldByName( - "protobuf_unittest.ForeignMessage.c"); - map_int32_int32_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32Int32Entry.key"); - map_int32_int32_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32Int32Entry.value"); - map_int64_int64_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt64Int64Entry.key"); - map_int64_int64_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt64Int64Entry.value"); - map_uint32_uint32_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapUint32Uint32Entry.key"); - map_uint32_uint32_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapUint32Uint32Entry.value"); - map_uint64_uint64_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapUint64Uint64Entry.key"); - map_uint64_uint64_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapUint64Uint64Entry.value"); - map_sint32_sint32_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSint32Sint32Entry.key"); - map_sint32_sint32_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSint32Sint32Entry.value"); - map_sint64_sint64_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSint64Sint64Entry.key"); - map_sint64_sint64_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSint64Sint64Entry.value"); - map_fixed32_fixed32_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapFixed32Fixed32Entry.key"); - map_fixed32_fixed32_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapFixed32Fixed32Entry.value"); - map_fixed64_fixed64_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapFixed64Fixed64Entry.key"); - map_fixed64_fixed64_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapFixed64Fixed64Entry.value"); - map_sfixed32_sfixed32_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSfixed32Sfixed32Entry.key"); - map_sfixed32_sfixed32_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSfixed32Sfixed32Entry.value"); - map_sfixed64_sfixed64_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSfixed64Sfixed64Entry.key"); - map_sfixed64_sfixed64_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSfixed64Sfixed64Entry.value"); - map_int32_float_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32FloatEntry.key"); - map_int32_float_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32FloatEntry.value"); - map_int32_double_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32DoubleEntry.key"); - map_int32_double_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32DoubleEntry.value"); - map_bool_bool_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapBoolBoolEntry.key"); - map_bool_bool_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapBoolBoolEntry.value"); - map_string_string_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapStringStringEntry.key"); - map_string_string_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapStringStringEntry.value"); - map_int32_bytes_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32BytesEntry.key"); - map_int32_bytes_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32BytesEntry.value"); - map_int32_enum_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32EnumEntry.key"); - map_int32_enum_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32EnumEntry.value"); - map_int32_foreign_message_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32ForeignMessageEntry.key"); - map_int32_foreign_message_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32ForeignMessageEntry.value"); - - EXPECT_FALSE(map_enum_foo_ == NULL); - EXPECT_FALSE(map_enum_bar_ == NULL); - EXPECT_FALSE(map_enum_baz_ == NULL); - EXPECT_FALSE(map_int32_int32_key_ == NULL); - EXPECT_FALSE(map_int32_int32_val_ == NULL); - EXPECT_FALSE(map_int64_int64_key_ == NULL); - EXPECT_FALSE(map_int64_int64_val_ == NULL); - EXPECT_FALSE(map_uint32_uint32_key_ == NULL); - EXPECT_FALSE(map_uint32_uint32_val_ == NULL); - EXPECT_FALSE(map_uint64_uint64_key_ == NULL); - EXPECT_FALSE(map_uint64_uint64_val_ == NULL); - EXPECT_FALSE(map_sint32_sint32_key_ == NULL); - EXPECT_FALSE(map_sint32_sint32_val_ == NULL); - EXPECT_FALSE(map_sint64_sint64_key_ == NULL); - EXPECT_FALSE(map_sint64_sint64_val_ == NULL); - EXPECT_FALSE(map_fixed32_fixed32_key_ == NULL); - EXPECT_FALSE(map_fixed32_fixed32_val_ == NULL); - EXPECT_FALSE(map_fixed64_fixed64_key_ == NULL); - EXPECT_FALSE(map_fixed64_fixed64_val_ == NULL); - EXPECT_FALSE(map_sfixed32_sfixed32_key_ == NULL); - EXPECT_FALSE(map_sfixed32_sfixed32_val_ == NULL); - EXPECT_FALSE(map_sfixed64_sfixed64_key_ == NULL); - EXPECT_FALSE(map_sfixed64_sfixed64_val_ == NULL); - EXPECT_FALSE(map_int32_float_key_ == NULL); - EXPECT_FALSE(map_int32_float_val_ == NULL); - EXPECT_FALSE(map_int32_double_key_ == NULL); - EXPECT_FALSE(map_int32_double_val_ == NULL); - EXPECT_FALSE(map_bool_bool_key_ == NULL); - EXPECT_FALSE(map_bool_bool_val_ == NULL); - EXPECT_FALSE(map_string_string_key_ == NULL); - EXPECT_FALSE(map_string_string_val_ == NULL); - EXPECT_FALSE(map_int32_bytes_key_ == NULL); - EXPECT_FALSE(map_int32_bytes_val_ == NULL); - EXPECT_FALSE(map_int32_enum_key_ == NULL); - EXPECT_FALSE(map_int32_enum_val_ == NULL); - EXPECT_FALSE(map_int32_foreign_message_key_ == NULL); - EXPECT_FALSE(map_int32_foreign_message_val_ == NULL); -} - -// Shorthand to get a FieldDescriptor for a field of unittest::TestMap. -const FieldDescriptor* MapReflectionTester::F(const string& name) { - const FieldDescriptor* result = NULL; - result = base_descriptor_->FindFieldByName(name); - GOOGLE_CHECK(result != NULL); - return result; -} - -void MapReflectionTester::SetMapFieldsViaReflection( - Message* message) { - const Reflection* reflection = message->GetReflection(); - Message* sub_message = NULL; - Message* sub_foreign_message = NULL; - - // Add first element. - sub_message = reflection->AddMessage(message, F("map_int32_int32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_int32_key_, 0); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_int32_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_int64_int64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_int64_int64_key_, 0); - sub_message->GetReflection() - ->SetInt64(sub_message, map_int64_int64_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_uint32_uint32_key_, 0); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_uint32_uint32_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_uint64_uint64_key_, 0); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_uint64_uint64_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sint32_sint32_key_, 0); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sint32_sint32_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sint64_sint64_key_, 0); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sint64_sint64_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_fixed32_fixed32_key_, 0); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_fixed32_fixed32_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_fixed64_fixed64_key_, 0); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_fixed64_fixed64_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sfixed32_sfixed32_key_, 0); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sfixed32_sfixed32_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sfixed64_sfixed64_key_, 0); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sfixed64_sfixed64_val_, 0); - - sub_message = reflection->AddMessage(message, F("map_int32_float")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_float_key_, 0); - sub_message->GetReflection() - ->SetFloat(sub_message, map_int32_float_val_, 0.0); - - sub_message = reflection->AddMessage(message, F("map_int32_double")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_double_key_, 0); - sub_message->GetReflection() - ->SetDouble(sub_message, map_int32_double_val_, 0.0); - - sub_message = reflection->AddMessage(message, F("map_bool_bool")); - sub_message->GetReflection() - ->SetBool(sub_message, map_bool_bool_key_, false); - sub_message->GetReflection() - ->SetBool(sub_message, map_bool_bool_val_, false); - - sub_message = reflection->AddMessage(message, F("map_string_string")); - sub_message->GetReflection() - ->SetString(sub_message, map_string_string_key_, "0"); - sub_message->GetReflection() - ->SetString(sub_message, map_string_string_val_, "0"); - - sub_message = reflection->AddMessage(message, F("map_int32_bytes")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_bytes_key_, 0); - sub_message->GetReflection() - ->SetString(sub_message, map_int32_bytes_val_, "0"); - - sub_message = reflection->AddMessage(message, F("map_int32_enum")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_enum_key_, 0); - sub_message->GetReflection() - ->SetEnum(sub_message, map_int32_enum_val_, map_enum_bar_); - - sub_message = reflection - ->AddMessage(message, F("map_int32_foreign_message")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_foreign_message_key_, 0); - sub_foreign_message = sub_message->GetReflection()-> - MutableMessage(sub_message, map_int32_foreign_message_val_, NULL); - sub_foreign_message->GetReflection()-> - SetInt32(sub_foreign_message, foreign_c_, 0); - - // Add second element - sub_message = reflection->AddMessage(message, F("map_int32_int32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_int32_key_, 1); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_int32_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_int64_int64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_int64_int64_key_, 1); - sub_message->GetReflection() - ->SetInt64(sub_message, map_int64_int64_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_uint32_uint32_key_, 1); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_uint32_uint32_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_uint64_uint64_key_, 1); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_uint64_uint64_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sint32_sint32_key_, 1); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sint32_sint32_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sint64_sint64_key_, 1); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sint64_sint64_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_fixed32_fixed32_key_, 1); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_fixed32_fixed32_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_fixed64_fixed64_key_, 1); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_fixed64_fixed64_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sfixed32_sfixed32_key_, 1); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sfixed32_sfixed32_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sfixed64_sfixed64_key_, 1); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sfixed64_sfixed64_val_, 1); - - sub_message = reflection->AddMessage(message, F("map_int32_float")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_float_key_, 1); - sub_message->GetReflection() - ->SetFloat(sub_message, map_int32_float_val_, 1.0); - - sub_message = reflection->AddMessage(message, F("map_int32_double")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_double_key_, 1); - sub_message->GetReflection() - ->SetDouble(sub_message, map_int32_double_val_, 1.0); - - sub_message = reflection->AddMessage(message, F("map_bool_bool")); - sub_message->GetReflection() - ->SetBool(sub_message, map_bool_bool_key_, true); - sub_message->GetReflection() - ->SetBool(sub_message, map_bool_bool_val_, true); - - sub_message = reflection->AddMessage(message, F("map_string_string")); - sub_message->GetReflection() - ->SetString(sub_message, map_string_string_key_, "1"); - sub_message->GetReflection() - ->SetString(sub_message, map_string_string_val_, "1"); - - sub_message = reflection->AddMessage(message, F("map_int32_bytes")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_bytes_key_, 1); - sub_message->GetReflection() - ->SetString(sub_message, map_int32_bytes_val_, "1"); - - sub_message = reflection->AddMessage(message, F("map_int32_enum")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_enum_key_, 1); - sub_message->GetReflection() - ->SetEnum(sub_message, map_int32_enum_val_, map_enum_baz_); - - sub_message = reflection - ->AddMessage(message, F("map_int32_foreign_message")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_foreign_message_key_, 1); - sub_foreign_message = sub_message->GetReflection()-> - MutableMessage(sub_message, map_int32_foreign_message_val_, NULL); - sub_foreign_message->GetReflection()-> - SetInt32(sub_foreign_message, foreign_c_, 1); -} - -void MapReflectionTester::SetMapFieldsViaMapReflection( - Message* message) { - const Reflection* reflection = message->GetReflection(); - - Message* sub_foreign_message = NULL; - MapValueRef map_val; - - // Add first element. - MapKey map_key; - map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_int32"), map_key, &map_val)); - map_val.SetInt32Value(0); - - map_key.SetInt64Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int64_int64"), map_key, &map_val)); - map_val.SetInt64Value(0); - - map_key.SetUInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_uint32_uint32"), map_key, &map_val)); - map_val.SetUInt32Value(0); - - map_key.SetUInt64Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_uint64_uint64"), map_key, &map_val)); - map_val.SetUInt64Value(0); - - map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_sint32_sint32"), map_key, &map_val)); - map_val.SetInt32Value(0); - - map_key.SetInt64Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_sint64_sint64"), map_key, &map_val)); - map_val.SetInt64Value(0); - - map_key.SetUInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_fixed32_fixed32"), map_key, &map_val)); - map_val.SetUInt32Value(0); - - map_key.SetUInt64Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_fixed64_fixed64"), map_key, &map_val)); - map_val.SetUInt64Value(0); - - map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_sfixed32_sfixed32"), map_key, &map_val)); - map_val.SetInt32Value(0); - - map_key.SetInt64Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_sfixed64_sfixed64"), map_key, &map_val)); - map_val.SetInt64Value(0); - - map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_float"), map_key, &map_val)); - map_val.SetFloatValue(0.0); - - map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_double"), map_key, &map_val)); - map_val.SetDoubleValue(0.0); - - map_key.SetBoolValue(false); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_bool_bool"), map_key, &map_val)); - map_val.SetBoolValue(false); - - map_key.SetStringValue("0"); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_string_string"), map_key, &map_val)); - map_val.SetStringValue("0"); - - map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_bytes"), map_key, &map_val)); - map_val.SetStringValue("0"); - - map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_enum"), map_key, &map_val)); - map_val.SetEnumValue(map_enum_bar_->number()); - - map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_foreign_message"), map_key, &map_val)); - sub_foreign_message = map_val.MutableMessageValue(); - sub_foreign_message->GetReflection()->SetInt32( - sub_foreign_message, foreign_c_, 0); - - // Add second element - map_key.SetInt32Value(1); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_int32"), map_key, &map_val)); - map_val.SetInt32Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int32_int32"), map_key, &map_val)); - - map_key.SetInt64Value(1); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int64_int64"), map_key, &map_val)); - map_val.SetInt64Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int64_int64"), map_key, &map_val)); - - map_key.SetUInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_uint32_uint32"), map_key, &map_val); - map_val.SetUInt32Value(1); - - map_key.SetUInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_uint64_uint64"), map_key, &map_val); - map_val.SetUInt64Value(1); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sint32_sint32"), map_key, &map_val); - map_val.SetInt32Value(1); - - map_key.SetInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sint64_sint64"), map_key, &map_val); - map_val.SetInt64Value(1); - - map_key.SetUInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_fixed32_fixed32"), map_key, &map_val); - map_val.SetUInt32Value(1); - - map_key.SetUInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_fixed64_fixed64"), map_key, &map_val); - map_val.SetUInt64Value(1); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sfixed32_sfixed32"), map_key, &map_val); - map_val.SetInt32Value(1); - - map_key.SetInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sfixed64_sfixed64"), map_key, &map_val); - map_val.SetInt64Value(1); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_float"), map_key, &map_val); - map_val.SetFloatValue(1.0); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_double"), map_key, &map_val); - map_val.SetDoubleValue(1.0); - - map_key.SetBoolValue(true); - reflection->InsertOrLookupMapValue( - message, F("map_bool_bool"), map_key, &map_val); - map_val.SetBoolValue(true); - - map_key.SetStringValue("1"); - reflection->InsertOrLookupMapValue( - message, F("map_string_string"), map_key, &map_val); - map_val.SetStringValue("1"); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_bytes"), map_key, &map_val); - map_val.SetStringValue("1"); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_enum"), map_key, &map_val); - map_val.SetEnumValue(map_enum_baz_->number()); - - map_key.SetInt32Value(1); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_foreign_message"), map_key, &map_val)); - sub_foreign_message = map_val.MutableMessageValue(); - sub_foreign_message->GetReflection()->SetInt32( - sub_foreign_message, foreign_c_, 1); -} - -void MapReflectionTester::GetMapValueViaMapReflection(Message* message, - const string& field_name, - const MapKey& map_key, - MapValueRef* map_val) { - const Reflection* reflection = message->GetReflection(); - EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F(field_name), - map_key, map_val)); -} - -Message* MapReflectionTester::GetMapEntryViaReflection(Message* message, - const string& field_name, - int index) { - const Reflection* reflection = message->GetReflection(); - return reflection->MutableRepeatedMessage(message, F(field_name), index); -} - -MapIterator MapReflectionTester::MapBegin(Message* message, - const string& field_name) { - const Reflection* reflection = message->GetReflection(); - return reflection->MapBegin(message, F(field_name)); -} - -MapIterator MapReflectionTester::MapEnd(Message* message, - const string& field_name) { - const Reflection* reflection = message->GetReflection(); - return reflection->MapEnd(message, F(field_name)); -} - -void MapReflectionTester::ClearMapFieldsViaReflection( - Message* message) { - const Reflection* reflection = message->GetReflection(); - - reflection->ClearField(message, F("map_int32_int32")); - reflection->ClearField(message, F("map_int64_int64")); - reflection->ClearField(message, F("map_uint32_uint32")); - reflection->ClearField(message, F("map_uint64_uint64")); - reflection->ClearField(message, F("map_sint32_sint32")); - reflection->ClearField(message, F("map_sint64_sint64")); - reflection->ClearField(message, F("map_fixed32_fixed32")); - reflection->ClearField(message, F("map_fixed64_fixed64")); - reflection->ClearField(message, F("map_sfixed32_sfixed32")); - reflection->ClearField(message, F("map_sfixed64_sfixed64")); - reflection->ClearField(message, F("map_int32_float")); - reflection->ClearField(message, F("map_int32_double")); - reflection->ClearField(message, F("map_bool_bool")); - reflection->ClearField(message, F("map_string_string")); - reflection->ClearField(message, F("map_int32_bytes")); - reflection->ClearField(message, F("map_int32_enum")); - reflection->ClearField(message, F("map_int32_foreign_message")); -} - -void MapReflectionTester::ModifyMapFieldsViaReflection( - Message* message) { - const Reflection* reflection = message->GetReflection(); - MapValueRef map_val; - Message* sub_foreign_message; - - // Modify the second element - MapKey map_key; - map_key.SetInt32Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int32_int32"), map_key, &map_val)); - map_val.SetInt32Value(2); - - map_key.SetInt64Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int64_int64"), map_key, &map_val)); - map_val.SetInt64Value(2); - - map_key.SetUInt32Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_uint32_uint32"), map_key, &map_val)); - map_val.SetUInt32Value(2); - - map_key.SetUInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_uint64_uint64"), map_key, &map_val); - map_val.SetUInt64Value(2); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sint32_sint32"), map_key, &map_val); - map_val.SetInt32Value(2); - - map_key.SetInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sint64_sint64"), map_key, &map_val); - map_val.SetInt64Value(2); - - map_key.SetUInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_fixed32_fixed32"), map_key, &map_val); - map_val.SetUInt32Value(2); - - map_key.SetUInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_fixed64_fixed64"), map_key, &map_val); - map_val.SetUInt64Value(2); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sfixed32_sfixed32"), map_key, &map_val); - map_val.SetInt32Value(2); - - map_key.SetInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sfixed64_sfixed64"), map_key, &map_val); - map_val.SetInt64Value(2); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_float"), map_key, &map_val); - map_val.SetFloatValue(2.0); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_double"), map_key, &map_val); - map_val.SetDoubleValue(2.0); - - map_key.SetBoolValue(true); - reflection->InsertOrLookupMapValue( - message, F("map_bool_bool"), map_key, &map_val); - map_val.SetBoolValue(false); - - map_key.SetStringValue("1"); - reflection->InsertOrLookupMapValue( - message, F("map_string_string"), map_key, &map_val); - map_val.SetStringValue("2"); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_bytes"), map_key, &map_val); - map_val.SetStringValue("2"); - - map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_enum"), map_key, &map_val); - map_val.SetEnumValue(map_enum_foo_->number()); - - map_key.SetInt32Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int32_foreign_message"), map_key, &map_val)); - sub_foreign_message = map_val.MutableMessageValue(); - sub_foreign_message->GetReflection()->SetInt32( - sub_foreign_message, foreign_c_, 2); -} - -void MapReflectionTester::RemoveLastMapsViaReflection( - Message* message) { - const Reflection* reflection = message->GetReflection(); - - std::vector output; - reflection->ListFields(*message, &output); - for (int i = 0; i < output.size(); ++i) { - const FieldDescriptor* field = output[i]; - if (!field->is_repeated()) continue; - reflection->RemoveLast(message, field); - } -} - -void MapReflectionTester::ReleaseLastMapsViaReflection( - Message* message) { - const Reflection* reflection = message->GetReflection(); - - std::vector output; - reflection->ListFields(*message, &output); - for (int i = 0; i < output.size(); ++i) { - const FieldDescriptor* field = output[i]; - if (!field->is_repeated()) continue; - if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; - - Message* released = reflection->ReleaseLast(message, field); - ASSERT_TRUE(released != NULL) << "ReleaseLast returned NULL for: " - << field->name(); - delete released; - } -} - -void MapReflectionTester::SwapMapsViaReflection(Message* message) { - const Reflection* reflection = message->GetReflection(); - std::vector output; - reflection->ListFields(*message, &output); - for (int i = 0; i < output.size(); ++i) { - const FieldDescriptor* field = output[i]; - if (!field->is_repeated()) continue; - reflection->SwapElements(message, field, 0, 1); - } -} - -void MapReflectionTester:: - MutableUnknownFieldsOfMapFieldsViaReflection(Message* message) { - const Reflection* reflection = message->GetReflection(); - Message* sub_message = NULL; - - sub_message = reflection->AddMessage(message, F("map_int32_int32")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_int64_int64")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_int32_float")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_int32_double")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_bool_bool")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_string_string")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_int32_bytes")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_int32_enum")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); - sub_message = reflection->AddMessage(message, F("map_int32_foreign_message")); - EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != - NULL); -} - -void MapReflectionTester::ExpectMapFieldsSetViaReflection( - const Message& message) { - string scratch; - const Reflection* reflection = message.GetReflection(); - const Message* sub_message; - MapKey map_key; - - // ----------------------------------------------------------------- - - ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_int32"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_int64_int64"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_uint32_uint32"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_uint64_uint64"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_sint32_sint32"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_sint64_sint64"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_fixed32_fixed32"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_fixed64_fixed64"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_sfixed32_sfixed32"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_sfixed64_sfixed64"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_float"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_double"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_bool_bool"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_string_string"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_bytes"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_enum"))); - ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_foreign_message"))); - - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - // Check with RepeatedField Reflection - sub_message = - &reflection->GetRepeatedMessage(message, F("map_int32_int32"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_int32_key_); - int32 val = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_int32_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt32Value(key); - EXPECT_TRUE(reflection->ContainsMapKey( - message, F("map_int32_int32"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - // Check with RepeatedField Reflection - sub_message = - &reflection->GetRepeatedMessage(message, F("map_int64_int64"), i); - int64 key = sub_message->GetReflection()->GetInt64( - *sub_message, map_int64_int64_key_); - int64 val = sub_message->GetReflection()->GetInt64( - *sub_message, map_int64_int64_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt64Value(key); - EXPECT_TRUE(reflection->ContainsMapKey( - message, F("map_int64_int64"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - // Check with RepeatedField Reflection - sub_message = - &reflection->GetRepeatedMessage(message, F("map_uint32_uint32"), i); - uint32 key = sub_message->GetReflection()->GetUInt32( - *sub_message, map_uint32_uint32_key_); - uint32 val = sub_message->GetReflection()->GetUInt32( - *sub_message, map_uint32_uint32_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetUInt32Value(key); - EXPECT_TRUE(reflection->ContainsMapKey( - message, F("map_uint32_uint32"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_uint64_uint64"), i); - uint64 key = sub_message->GetReflection()->GetUInt64( - *sub_message, map_uint64_uint64_key_); - uint64 val = sub_message->GetReflection()->GetUInt64( - *sub_message, map_uint64_uint64_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetUInt64Value(key); - EXPECT_TRUE(reflection->ContainsMapKey( - message, F("map_uint64_uint64"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_sint32_sint32"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_sint32_sint32_key_); - int32 val = sub_message->GetReflection()->GetInt32( - *sub_message, map_sint32_sint32_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_sint32_sint32"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_sint64_sint64"), i); - int64 key = sub_message->GetReflection()->GetInt64( - *sub_message, map_sint64_sint64_key_); - int64 val = sub_message->GetReflection()->GetInt64( - *sub_message, map_sint64_sint64_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt64Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_sint64_sint64"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_fixed32_fixed32"), i); - uint32 key = sub_message->GetReflection()->GetUInt32( - *sub_message, map_fixed32_fixed32_key_); - uint32 val = sub_message->GetReflection()->GetUInt32( - *sub_message, map_fixed32_fixed32_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetUInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_fixed32_fixed32"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_fixed64_fixed64"), i); - uint64 key = sub_message->GetReflection()->GetUInt64( - *sub_message, map_fixed64_fixed64_key_); - uint64 val = sub_message->GetReflection()->GetUInt64( - *sub_message, map_fixed64_fixed64_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetUInt64Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_fixed64_fixed64"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - sub_message = &reflection->GetRepeatedMessage( - message, F("map_sfixed32_sfixed32"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_sfixed32_sfixed32_key_); - int32 val = sub_message->GetReflection()->GetInt32( - *sub_message, map_sfixed32_sfixed32_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_sfixed32_sfixed32"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - sub_message = &reflection->GetRepeatedMessage( - message, F("map_sfixed64_sfixed64"), i); - int64 key = sub_message->GetReflection()->GetInt64( - *sub_message, map_sfixed64_sfixed64_key_); - int64 val = sub_message->GetReflection()->GetInt64( - *sub_message, map_sfixed64_sfixed64_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt64Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_sfixed64_sfixed64"), map_key)); - } - } - { - std::map map; - map[0] = 0.0; - map[1] = 1.0; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_int32_float"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_float_key_); - float val = sub_message->GetReflection()->GetFloat( - *sub_message, map_int32_float_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_float"), map_key)); - } - } - { - std::map map; - map[0] = 0.0; - map[1] = 1.0; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_int32_double"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_double_key_); - double val = sub_message->GetReflection()->GetDouble( - *sub_message, map_int32_double_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_double"), map_key)); - } - } - { - std::map map; - map[false] = false; - map[true] = true; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_bool_bool"), i); - bool key = sub_message->GetReflection()->GetBool( - *sub_message, map_bool_bool_key_); - bool val = sub_message->GetReflection()->GetBool( - *sub_message, map_bool_bool_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetBoolValue(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_bool_bool"), map_key)); - } - } - { - std::map map; - map["0"] = "0"; - map["1"] = "1"; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_string_string"), i); - string key = sub_message->GetReflection()->GetString( - *sub_message, map_string_string_key_); - string val = sub_message->GetReflection()->GetString( - *sub_message, map_string_string_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetStringValue(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_string_string"), map_key)); - } - } - { - std::map map; - map[0] = "0"; - map[1] = "1"; - for (int i = 0; i < 2; i++) { - sub_message = - &reflection->GetRepeatedMessage(message, F("map_int32_bytes"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_bytes_key_); - string val = sub_message->GetReflection()->GetString( - *sub_message, map_int32_bytes_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_bytes"), map_key)); - } - } - { - std::map map; - map[0] = map_enum_bar_; - map[1] = map_enum_baz_; - for (int i = 0; i < 2; i++) { - sub_message = &reflection->GetRepeatedMessage( - message, F("map_int32_enum"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_enum_key_); - const EnumValueDescriptor* val = sub_message->GetReflection()->GetEnum( - *sub_message, map_int32_enum_val_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_enum"), map_key)); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (int i = 0; i < 2; i++) { - sub_message = &reflection->GetRepeatedMessage( - message, F("map_int32_foreign_message"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_foreign_message_key_); - const Message& foreign_message = sub_message->GetReflection()->GetMessage( - *sub_message, map_int32_foreign_message_val_); - int32 val = foreign_message.GetReflection()->GetInt32( - foreign_message, foreign_c_); - EXPECT_EQ(map[key], val); - // Check with Map Reflection - map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_foreign_message"), map_key)); - } - } -} - -void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( - Message* message) { - string scratch; - string serialized; - const Reflection* reflection = message->GetReflection(); - - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_int32"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int64_int64"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_uint32_uint32"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_uint64_uint64"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sint32_sint32"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sint64_sint64"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_fixed32_fixed32"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_fixed64_fixed64"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sfixed32_sfixed32"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sfixed64_sfixed64"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_float"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_double"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_bool_bool"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_string_string"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_bytes"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_enum"))); - ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_foreign_message"))); - - { - std::map map; - map[0] = 0; - map[1] = 1; - int size = 0; - for (MapIterator iter = reflection->MapBegin(message, F("map_int32_int32")); - iter != reflection->MapEnd(message, F("map_int32_int32")); - ++iter, ++size) { - // Check const methods do not invalidate map. - message->DebugString(); - message->ShortDebugString(); - message->SerializeToString(&serialized); - message->SpaceUsed(); - message->ByteSize(); - EXPECT_EQ(map[iter.GetKey().GetInt32Value()], - iter.GetValueRef().GetInt32Value()); - } - EXPECT_EQ(size, 2); - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (MapIterator iter = reflection->MapBegin(message, F("map_int64_int64")); - iter != reflection->MapEnd(message, F("map_int64_int64")); ++iter) { - EXPECT_EQ(map[iter.GetKey().GetInt64Value()], - iter.GetValueRef().GetInt64Value()); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_uint32_uint32")); - iter != reflection->MapEnd(message, F("map_uint32_uint32")); - ++iter) { - EXPECT_EQ(map[iter.GetKey().GetUInt32Value()], - iter.GetValueRef().GetUInt32Value()); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_uint64_uint64")); - iter != reflection->MapEnd(message, F("map_uint64_uint64")); - ++iter) { - EXPECT_EQ(map[iter.GetKey().GetUInt64Value()], - iter.GetValueRef().GetUInt64Value()); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_sint32_sint32")); - iter != reflection->MapEnd(message, F("map_sint32_sint32")); - ++iter) { - EXPECT_EQ(map[iter.GetKey().GetInt32Value()], - iter.GetValueRef().GetInt32Value()); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_sint64_sint64")); - iter != reflection->MapEnd(message, F("map_sint64_sint64")); ++iter) { - EXPECT_EQ(map[iter.GetKey().GetInt64Value()], - iter.GetValueRef().GetInt64Value()); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_fixed32_fixed32")); - iter != reflection->MapEnd(message, F("map_fixed32_fixed32")); - ++iter) { - EXPECT_EQ(map[iter.GetKey().GetUInt32Value()], - iter.GetValueRef().GetUInt32Value()); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_fixed64_fixed64")); - iter != reflection->MapEnd(message, F("map_fixed64_fixed64")); - ++iter) { - EXPECT_EQ(map[iter.GetKey().GetUInt64Value()], - iter.GetValueRef().GetUInt64Value()); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_sfixed32_sfixed32")); - iter != reflection->MapEnd(message, F("map_sfixed32_sfixed32")); - ++iter) { - EXPECT_EQ(map[iter.GetKey().GetInt32Value()], - iter.GetValueRef().GetInt32Value()); - } - } - { - std::map map; - map[0] = 0.0; - map[1] = 1.0; - for (MapIterator iter = reflection->MapBegin(message, F("map_int32_float")); - iter != reflection->MapEnd(message, F("map_int32_float")); ++iter) { - EXPECT_EQ(map[iter.GetKey().GetInt32Value()], - iter.GetValueRef().GetFloatValue()); - } - } - { - std::map map; - map[0] = 0.0; - map[1] = 1.0; - for (MapIterator iter = reflection->MapBegin( - message, F("map_int32_double")); - iter != reflection->MapEnd(message, F("map_int32_double")); ++iter) { - EXPECT_EQ(map[iter.GetKey().GetInt32Value()], - iter.GetValueRef().GetDoubleValue()); - } - } - { - std::map map; - map[false] = false; - map[true] = true; - for (MapIterator iter = reflection->MapBegin(message, F("map_bool_bool")); - iter != reflection->MapEnd(message, F("map_bool_bool")); ++iter) { - EXPECT_EQ(map[iter.GetKey().GetBoolValue()], - iter.GetValueRef().GetBoolValue()); - } - } - { - std::map map; - map["0"] = "0"; - map["1"] = "1"; - int size = 0; - for (MapIterator iter = reflection->MapBegin( - message, F("map_string_string")); - iter != reflection->MapEnd(message, F("map_string_string")); - ++iter, ++size) { - // Check const methods do not invalidate map. - message->DebugString(); - message->ShortDebugString(); - message->SerializeToString(&serialized); - message->SpaceUsed(); - message->ByteSize(); - EXPECT_EQ(map[iter.GetKey().GetStringValue()], - iter.GetValueRef().GetStringValue()); - } - EXPECT_EQ(size, 2); - } - { - std::map map; - map[0] = "0"; - map[1] = "1"; - for (MapIterator iter = reflection->MapBegin(message, F("map_int32_bytes")); - iter != reflection->MapEnd(message, F("map_int32_bytes")); ++iter) { - EXPECT_EQ(map[iter.GetKey().GetInt32Value()], - iter.GetValueRef().GetStringValue()); - } - } - { - std::map map; - map[0] = map_enum_bar_; - map[1] = map_enum_baz_; - for (MapIterator iter = reflection->MapBegin(message, F("map_int32_enum")); - iter != reflection->MapEnd(message, F("map_int32_enum")); ++iter) { - EXPECT_EQ(map[iter.GetKey().GetInt32Value()]->number(), - iter.GetValueRef().GetEnumValue()); - } - } - { - std::map map; - map[0] = 0; - map[1] = 1; - int size = 0; - for (MapIterator iter = reflection->MapBegin( - message, F("map_int32_foreign_message")); - iter != reflection->MapEnd(message, F("map_int32_foreign_message")); - ++iter, ++size) { - // Check const methods do not invalidate map. - message->DebugString(); - message->ShortDebugString(); - message->SerializeToString(&serialized); - message->SpaceUsed(); - message->ByteSize(); - const Message& sub_message = iter.GetValueRef().GetMessageValue(); - EXPECT_EQ(map[iter.GetKey().GetInt32Value()], - sub_message.GetReflection()->GetInt32(sub_message, foreign_c_)); - } - EXPECT_EQ(size, 2); - } -} - -void MapReflectionTester::ExpectClearViaReflection( - const Message& message) { - const Reflection* reflection = message.GetReflection(); - // Map fields are empty. - EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_int32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_int64_int64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_uint32_uint32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_uint64_uint64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_sint32_sint32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_sint64_sint64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_fixed32_fixed32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_fixed64_fixed64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_sfixed32_sfixed32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_sfixed64_sfixed64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_float"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_double"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_bool_bool"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_string_string"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_bytes"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_enum"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_foreign_message"))); -} - -void MapReflectionTester::ExpectClearViaReflectionIterator( - Message* message) { - const Reflection* reflection = message->GetReflection(); - EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_int32")) == - reflection->MapEnd(message, F("map_int32_int32"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_int64_int64")) == - reflection->MapEnd(message, F("map_int64_int64"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_uint32_uint32")) == - reflection->MapEnd(message, F("map_uint32_uint32"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_uint64_uint64")) == - reflection->MapEnd(message, F("map_uint64_uint64"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_sint32_sint32")) == - reflection->MapEnd(message, F("map_sint32_sint32"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_sint64_sint64")) == - reflection->MapEnd(message, F("map_sint64_sint64"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_fixed32_fixed32")) == - reflection->MapEnd(message, F("map_fixed32_fixed32"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_fixed64_fixed64")) == - reflection->MapEnd(message, F("map_fixed64_fixed64"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_sfixed32_sfixed32")) == - reflection->MapEnd(message, F("map_sfixed32_sfixed32"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_sfixed64_sfixed64")) == - reflection->MapEnd(message, F("map_sfixed64_sfixed64"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_float")) == - reflection->MapEnd(message, F("map_int32_float"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_double")) == - reflection->MapEnd(message, F("map_int32_double"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_bool_bool")) == - reflection->MapEnd(message, F("map_bool_bool"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_string_string")) == - reflection->MapEnd(message, F("map_string_string"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_bytes")) == - reflection->MapEnd(message, F("map_int32_bytes"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_enum")) == - reflection->MapEnd(message, F("map_int32_enum"))); - EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_foreign_message")) == - reflection->MapEnd(message, F("map_int32_foreign_message"))); -} - -void MapReflectionTester::ExpectMapEntryClearViaReflection( - Message* message) { - const Reflection* reflection = message->GetReflection(); - const Message* sub_message; - - { - const FieldDescriptor* descriptor = F("map_int32_int32"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_int32_int32")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_int64_int64"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_int64_int64")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_uint32_uint32"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); - EXPECT_EQ(0, sub_message->GetReflection()->GetUInt32(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetUInt32(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_uint64_uint64"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); - EXPECT_EQ(0, sub_message->GetReflection()->GetUInt64(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetUInt64(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_sint32_sint32"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_sint64_sint64"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_fixed32_fixed32"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); - EXPECT_EQ(0, sub_message->GetReflection()->GetUInt32(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetUInt32(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_fixed64_fixed64"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); - EXPECT_EQ(0, sub_message->GetReflection()->GetUInt64(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetUInt64(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_sfixed32_sfixed32"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_sfixed64_sfixed64"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_int32_float"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_int32_float")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetFloat(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_int32_double"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_int32_double")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection()->GetDouble(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_bool_bool"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_bool_bool")); - EXPECT_EQ(false, sub_message->GetReflection()->GetBool(*sub_message, - key_descriptor)); - EXPECT_EQ(false, sub_message->GetReflection()->GetBool(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_string_string"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_string_string")); - EXPECT_EQ("", sub_message->GetReflection()->GetString(*sub_message, - key_descriptor)); - EXPECT_EQ("", sub_message->GetReflection()->GetString(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_int32_bytes"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_int32_bytes")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - key_descriptor)); - EXPECT_EQ("", sub_message->GetReflection()->GetString(*sub_message, - value_descriptor)); - } - { - const FieldDescriptor* descriptor = F("map_int32_enum"); - const FieldDescriptor* key_descriptor = - descriptor->message_type()->FindFieldByName("key"); - const FieldDescriptor* value_descriptor = - descriptor->message_type()->FindFieldByName("value"); - sub_message = reflection->AddMessage(message, F("map_int32_enum")); - EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, - key_descriptor)); - EXPECT_EQ(0, sub_message->GetReflection() - ->GetEnum(*sub_message, value_descriptor) - ->number()); - } - // Map using message as value has been tested in other place. Thus, we don't - // test it here. -} - -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/map_test_util.h b/src/google/protobuf/map_test_util.h index 53d1575652..54ccfbb8d0 100644 --- a/src/google/protobuf/map_test_util.h +++ b/src/google/protobuf/map_test_util.h @@ -33,57 +33,18 @@ #include +#define UNITTEST ::protobuf_unittest +// Must define UNITTEST before including map_test_util.inc. +#include +#undef UNITTEST + +#include + namespace google { namespace protobuf { namespace unittest = ::protobuf_unittest; -class MapTestUtil { - public: - // Set every field in the TestMap message to a unique value. - static void SetMapFields(unittest::TestMap* message); - - // Set every field in the TestArenaMap message to a unique value. - static void SetArenaMapFields(unittest::TestArenaMap* message); - - // Set every field in the message to a default value. - static void SetMapFieldsInitialized(unittest::TestMap* message); - - // Modify all the map fields of the message (which should already have been - // initialized with SetMapFields()). - static void ModifyMapFields(unittest::TestMap* message); - - // Check that all fields have the values that they should have after - // SetMapFields() is called. - static void ExpectMapFieldsSet(const unittest::TestMap& message); - - // Check that all fields have the values that they should have after - // SetMapFields() is called for TestArenaMap. - static void ExpectArenaMapFieldsSet(const unittest::TestArenaMap& message); - - // Check that all fields have the values that they should have after - // SetMapFieldsInitialized() is called. - static void ExpectMapFieldsSetInitialized( - const unittest::TestMap& message); - - // Expect that the message is modified as would be expected from - // ModifyMapFields(). - static void ExpectMapFieldsModified(const unittest::TestMap& message); - - // Check that all fields are empty. - static void ExpectClear(const unittest::TestMap& message); - - // Check that all map fields have the given size. - static void ExpectMapsSize(const unittest::TestMap& message, int size); - - // Get pointers of map entries at given index. - static std::vector GetMapEntries( - const unittest::TestMap& message, int index); - - // Get pointers of map entries from release. - static std::vector GetMapEntriesFromRelease( - unittest::TestMap* message); -}; // Like above, but use the reflection interface. class MapReflectionTester { @@ -105,17 +66,16 @@ class MapReflectionTester { void ExpectMapFieldsSetViaReflectionIterator(Message* message); void ExpectClearViaReflection(const Message& message); void ExpectClearViaReflectionIterator(Message* message); - void ExpectMapEntryClearViaReflection(Message* message); void GetMapValueViaMapReflection(Message* message, - const string& field_name, + const std::string& field_name, const MapKey& map_key, MapValueRef* map_val); - Message* GetMapEntryViaReflection(Message* message, const string& field_name, - int index); - MapIterator MapBegin(Message* message, const string& field_name); - MapIterator MapEnd(Message* message, const string& field_name); + Message* GetMapEntryViaReflection(Message* message, + const std::string& field_name, int index); + MapIterator MapBegin(Message* message, const std::string& field_name); + MapIterator MapEnd(Message* message, const std::string& field_name); private: - const FieldDescriptor* F(const string& name); + const FieldDescriptor* F(const std::string& name); const Descriptor* base_descriptor_; @@ -160,7 +120,1395 @@ class MapReflectionTester { const FieldDescriptor* map_int32_foreign_message_val_; }; +inline MapReflectionTester::MapReflectionTester( + const Descriptor* base_descriptor) + : base_descriptor_(base_descriptor) { + const DescriptorPool* pool = base_descriptor->file()->pool(); + std::string package = base_descriptor->file()->package(); + + map_enum_foo_ = pool->FindEnumValueByName(package + ".MAP_ENUM_FOO"); + map_enum_bar_ = pool->FindEnumValueByName(package + ".MAP_ENUM_BAR"); + map_enum_baz_ = pool->FindEnumValueByName(package + ".MAP_ENUM_BAZ"); + + foreign_c_ = pool->FindFieldByName(package + ".ForeignMessage.c"); + map_int32_int32_key_ = + pool->FindFieldByName(package + ".TestMap.MapInt32Int32Entry.key"); + map_int32_int32_val_ = + pool->FindFieldByName(package + ".TestMap.MapInt32Int32Entry.value"); + map_int64_int64_key_ = + pool->FindFieldByName(package + ".TestMap.MapInt64Int64Entry.key"); + map_int64_int64_val_ = + pool->FindFieldByName(package + ".TestMap.MapInt64Int64Entry.value"); + map_uint32_uint32_key_ = + pool->FindFieldByName(package + ".TestMap.MapUint32Uint32Entry.key"); + map_uint32_uint32_val_ = + pool->FindFieldByName(package + ".TestMap.MapUint32Uint32Entry.value"); + map_uint64_uint64_key_ = + pool->FindFieldByName(package + ".TestMap.MapUint64Uint64Entry.key"); + map_uint64_uint64_val_ = + pool->FindFieldByName(package + ".TestMap.MapUint64Uint64Entry.value"); + map_sint32_sint32_key_ = + pool->FindFieldByName(package + ".TestMap.MapSint32Sint32Entry.key"); + map_sint32_sint32_val_ = + pool->FindFieldByName(package + ".TestMap.MapSint32Sint32Entry.value"); + map_sint64_sint64_key_ = + pool->FindFieldByName(package + ".TestMap.MapSint64Sint64Entry.key"); + map_sint64_sint64_val_ = + pool->FindFieldByName(package + ".TestMap.MapSint64Sint64Entry.value"); + map_fixed32_fixed32_key_ = + pool->FindFieldByName(package + ".TestMap.MapFixed32Fixed32Entry.key"); + map_fixed32_fixed32_val_ = + pool->FindFieldByName(package + ".TestMap.MapFixed32Fixed32Entry.value"); + map_fixed64_fixed64_key_ = + pool->FindFieldByName(package + ".TestMap.MapFixed64Fixed64Entry.key"); + map_fixed64_fixed64_val_ = + pool->FindFieldByName(package + ".TestMap.MapFixed64Fixed64Entry.value"); + map_sfixed32_sfixed32_key_ = + pool->FindFieldByName(package + ".TestMap.MapSfixed32Sfixed32Entry.key"); + map_sfixed32_sfixed32_val_ = pool->FindFieldByName( + package + ".TestMap.MapSfixed32Sfixed32Entry.value"); + map_sfixed64_sfixed64_key_ = + pool->FindFieldByName(package + ".TestMap.MapSfixed64Sfixed64Entry.key"); + map_sfixed64_sfixed64_val_ = pool->FindFieldByName( + package + ".TestMap.MapSfixed64Sfixed64Entry.value"); + map_int32_float_key_ = + pool->FindFieldByName(package + ".TestMap.MapInt32FloatEntry.key"); + map_int32_float_val_ = + pool->FindFieldByName(package + ".TestMap.MapInt32FloatEntry.value"); + map_int32_double_key_ = + pool->FindFieldByName(package + ".TestMap.MapInt32DoubleEntry.key"); + map_int32_double_val_ = + pool->FindFieldByName(package + ".TestMap.MapInt32DoubleEntry.value"); + map_bool_bool_key_ = + pool->FindFieldByName(package + ".TestMap.MapBoolBoolEntry.key"); + map_bool_bool_val_ = + pool->FindFieldByName(package + ".TestMap.MapBoolBoolEntry.value"); + map_string_string_key_ = + pool->FindFieldByName(package + ".TestMap.MapStringStringEntry.key"); + map_string_string_val_ = + pool->FindFieldByName(package + ".TestMap.MapStringStringEntry.value"); + map_int32_bytes_key_ = + pool->FindFieldByName(package + ".TestMap.MapInt32BytesEntry.key"); + map_int32_bytes_val_ = + pool->FindFieldByName(package + ".TestMap.MapInt32BytesEntry.value"); + map_int32_enum_key_ = + pool->FindFieldByName(package + ".TestMap.MapInt32EnumEntry.key"); + map_int32_enum_val_ = + pool->FindFieldByName(package + ".TestMap.MapInt32EnumEntry.value"); + map_int32_foreign_message_key_ = pool->FindFieldByName( + package + ".TestMap.MapInt32ForeignMessageEntry.key"); + map_int32_foreign_message_val_ = pool->FindFieldByName( + package + ".TestMap.MapInt32ForeignMessageEntry.value"); + + EXPECT_FALSE(map_enum_foo_ == nullptr); + EXPECT_FALSE(map_enum_bar_ == nullptr); + EXPECT_FALSE(map_enum_baz_ == nullptr); + EXPECT_FALSE(map_int32_int32_key_ == nullptr); + EXPECT_FALSE(map_int32_int32_val_ == nullptr); + EXPECT_FALSE(map_int64_int64_key_ == nullptr); + EXPECT_FALSE(map_int64_int64_val_ == nullptr); + EXPECT_FALSE(map_uint32_uint32_key_ == nullptr); + EXPECT_FALSE(map_uint32_uint32_val_ == nullptr); + EXPECT_FALSE(map_uint64_uint64_key_ == nullptr); + EXPECT_FALSE(map_uint64_uint64_val_ == nullptr); + EXPECT_FALSE(map_sint32_sint32_key_ == nullptr); + EXPECT_FALSE(map_sint32_sint32_val_ == nullptr); + EXPECT_FALSE(map_sint64_sint64_key_ == nullptr); + EXPECT_FALSE(map_sint64_sint64_val_ == nullptr); + EXPECT_FALSE(map_fixed32_fixed32_key_ == nullptr); + EXPECT_FALSE(map_fixed32_fixed32_val_ == nullptr); + EXPECT_FALSE(map_fixed64_fixed64_key_ == nullptr); + EXPECT_FALSE(map_fixed64_fixed64_val_ == nullptr); + EXPECT_FALSE(map_sfixed32_sfixed32_key_ == nullptr); + EXPECT_FALSE(map_sfixed32_sfixed32_val_ == nullptr); + EXPECT_FALSE(map_sfixed64_sfixed64_key_ == nullptr); + EXPECT_FALSE(map_sfixed64_sfixed64_val_ == nullptr); + EXPECT_FALSE(map_int32_float_key_ == nullptr); + EXPECT_FALSE(map_int32_float_val_ == nullptr); + EXPECT_FALSE(map_int32_double_key_ == nullptr); + EXPECT_FALSE(map_int32_double_val_ == nullptr); + EXPECT_FALSE(map_bool_bool_key_ == nullptr); + EXPECT_FALSE(map_bool_bool_val_ == nullptr); + EXPECT_FALSE(map_string_string_key_ == nullptr); + EXPECT_FALSE(map_string_string_val_ == nullptr); + EXPECT_FALSE(map_int32_bytes_key_ == nullptr); + EXPECT_FALSE(map_int32_bytes_val_ == nullptr); + EXPECT_FALSE(map_int32_enum_key_ == nullptr); + EXPECT_FALSE(map_int32_enum_val_ == nullptr); + EXPECT_FALSE(map_int32_foreign_message_key_ == nullptr); + EXPECT_FALSE(map_int32_foreign_message_val_ == nullptr); +} + +// Shorthand to get a FieldDescriptor for a field of unittest::TestMap. +inline const FieldDescriptor* MapReflectionTester::F(const std::string& name) { + const FieldDescriptor* result = nullptr; + result = base_descriptor_->FindFieldByName(name); + GOOGLE_CHECK(result != nullptr); + return result; +} + +inline void MapReflectionTester::SetMapFieldsViaReflection(Message* message) { + const Reflection* reflection = message->GetReflection(); + Message* sub_message = nullptr; + Message* sub_foreign_message = nullptr; + + // Add first element. + sub_message = reflection->AddMessage(message, F("map_int32_int32")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_int32_key_, 0); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_int32_val_, 0); + + sub_message = reflection->AddMessage(message, F("map_int64_int64")); + sub_message->GetReflection()->SetInt64(sub_message, map_int64_int64_key_, 0); + sub_message->GetReflection()->SetInt64(sub_message, map_int64_int64_val_, 0); + + sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); + sub_message->GetReflection()->SetUInt32(sub_message, map_uint32_uint32_key_, + 0); + sub_message->GetReflection()->SetUInt32(sub_message, map_uint32_uint32_val_, + 0); + + sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); + sub_message->GetReflection()->SetUInt64(sub_message, map_uint64_uint64_key_, + 0); + sub_message->GetReflection()->SetUInt64(sub_message, map_uint64_uint64_val_, + 0); + + sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); + sub_message->GetReflection()->SetInt32(sub_message, map_sint32_sint32_key_, + 0); + sub_message->GetReflection()->SetInt32(sub_message, map_sint32_sint32_val_, + 0); + + sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); + sub_message->GetReflection()->SetInt64(sub_message, map_sint64_sint64_key_, + 0); + sub_message->GetReflection()->SetInt64(sub_message, map_sint64_sint64_val_, + 0); + + sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); + sub_message->GetReflection()->SetUInt32(sub_message, map_fixed32_fixed32_key_, + 0); + sub_message->GetReflection()->SetUInt32(sub_message, map_fixed32_fixed32_val_, + 0); + + sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); + sub_message->GetReflection()->SetUInt64(sub_message, map_fixed64_fixed64_key_, + 0); + sub_message->GetReflection()->SetUInt64(sub_message, map_fixed64_fixed64_val_, + 0); + + sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); + sub_message->GetReflection()->SetInt32(sub_message, + map_sfixed32_sfixed32_key_, 0); + sub_message->GetReflection()->SetInt32(sub_message, + map_sfixed32_sfixed32_val_, 0); + + sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); + sub_message->GetReflection()->SetInt64(sub_message, + map_sfixed64_sfixed64_key_, 0); + sub_message->GetReflection()->SetInt64(sub_message, + map_sfixed64_sfixed64_val_, 0); + + sub_message = reflection->AddMessage(message, F("map_int32_float")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_float_key_, 0); + sub_message->GetReflection()->SetFloat(sub_message, map_int32_float_val_, + 0.0); + + sub_message = reflection->AddMessage(message, F("map_int32_double")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_double_key_, 0); + sub_message->GetReflection()->SetDouble(sub_message, map_int32_double_val_, + 0.0); + + sub_message = reflection->AddMessage(message, F("map_bool_bool")); + sub_message->GetReflection()->SetBool(sub_message, map_bool_bool_key_, false); + sub_message->GetReflection()->SetBool(sub_message, map_bool_bool_val_, false); + + sub_message = reflection->AddMessage(message, F("map_string_string")); + sub_message->GetReflection()->SetString(sub_message, map_string_string_key_, + "0"); + sub_message->GetReflection()->SetString(sub_message, map_string_string_val_, + "0"); + + sub_message = reflection->AddMessage(message, F("map_int32_bytes")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_bytes_key_, 0); + sub_message->GetReflection()->SetString(sub_message, map_int32_bytes_val_, + "0"); + + sub_message = reflection->AddMessage(message, F("map_int32_enum")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_enum_key_, 0); + sub_message->GetReflection()->SetEnum(sub_message, map_int32_enum_val_, + map_enum_bar_); + + sub_message = reflection->AddMessage(message, F("map_int32_foreign_message")); + sub_message->GetReflection()->SetInt32(sub_message, + map_int32_foreign_message_key_, 0); + sub_foreign_message = sub_message->GetReflection()->MutableMessage( + sub_message, map_int32_foreign_message_val_, nullptr); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 0); + + // Add second element + sub_message = reflection->AddMessage(message, F("map_int32_int32")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_int32_key_, 1); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_int32_val_, 1); + + sub_message = reflection->AddMessage(message, F("map_int64_int64")); + sub_message->GetReflection()->SetInt64(sub_message, map_int64_int64_key_, 1); + sub_message->GetReflection()->SetInt64(sub_message, map_int64_int64_val_, 1); + + sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); + sub_message->GetReflection()->SetUInt32(sub_message, map_uint32_uint32_key_, + 1); + sub_message->GetReflection()->SetUInt32(sub_message, map_uint32_uint32_val_, + 1); + + sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); + sub_message->GetReflection()->SetUInt64(sub_message, map_uint64_uint64_key_, + 1); + sub_message->GetReflection()->SetUInt64(sub_message, map_uint64_uint64_val_, + 1); + + sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); + sub_message->GetReflection()->SetInt32(sub_message, map_sint32_sint32_key_, + 1); + sub_message->GetReflection()->SetInt32(sub_message, map_sint32_sint32_val_, + 1); + + sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); + sub_message->GetReflection()->SetInt64(sub_message, map_sint64_sint64_key_, + 1); + sub_message->GetReflection()->SetInt64(sub_message, map_sint64_sint64_val_, + 1); + + sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); + sub_message->GetReflection()->SetUInt32(sub_message, map_fixed32_fixed32_key_, + 1); + sub_message->GetReflection()->SetUInt32(sub_message, map_fixed32_fixed32_val_, + 1); + + sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); + sub_message->GetReflection()->SetUInt64(sub_message, map_fixed64_fixed64_key_, + 1); + sub_message->GetReflection()->SetUInt64(sub_message, map_fixed64_fixed64_val_, + 1); + + sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); + sub_message->GetReflection()->SetInt32(sub_message, + map_sfixed32_sfixed32_key_, 1); + sub_message->GetReflection()->SetInt32(sub_message, + map_sfixed32_sfixed32_val_, 1); + + sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); + sub_message->GetReflection()->SetInt64(sub_message, + map_sfixed64_sfixed64_key_, 1); + sub_message->GetReflection()->SetInt64(sub_message, + map_sfixed64_sfixed64_val_, 1); + + sub_message = reflection->AddMessage(message, F("map_int32_float")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_float_key_, 1); + sub_message->GetReflection()->SetFloat(sub_message, map_int32_float_val_, + 1.0); + + sub_message = reflection->AddMessage(message, F("map_int32_double")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_double_key_, 1); + sub_message->GetReflection()->SetDouble(sub_message, map_int32_double_val_, + 1.0); + + sub_message = reflection->AddMessage(message, F("map_bool_bool")); + sub_message->GetReflection()->SetBool(sub_message, map_bool_bool_key_, true); + sub_message->GetReflection()->SetBool(sub_message, map_bool_bool_val_, true); + + sub_message = reflection->AddMessage(message, F("map_string_string")); + sub_message->GetReflection()->SetString(sub_message, map_string_string_key_, + "1"); + sub_message->GetReflection()->SetString(sub_message, map_string_string_val_, + "1"); + + sub_message = reflection->AddMessage(message, F("map_int32_bytes")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_bytes_key_, 1); + sub_message->GetReflection()->SetString(sub_message, map_int32_bytes_val_, + "1"); + + sub_message = reflection->AddMessage(message, F("map_int32_enum")); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_enum_key_, 1); + sub_message->GetReflection()->SetEnum(sub_message, map_int32_enum_val_, + map_enum_baz_); + + sub_message = reflection->AddMessage(message, F("map_int32_foreign_message")); + sub_message->GetReflection()->SetInt32(sub_message, + map_int32_foreign_message_key_, 1); + sub_foreign_message = sub_message->GetReflection()->MutableMessage( + sub_message, map_int32_foreign_message_val_, nullptr); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 1); +} + +inline void MapReflectionTester::SetMapFieldsViaMapReflection( + Message* message) { + const Reflection* reflection = message->GetReflection(); + + Message* sub_foreign_message = nullptr; + MapValueRef map_val; + + // Add first element. + MapKey map_key; + map_key.SetInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_int32"), + map_key, &map_val)); + map_val.SetInt32Value(0); + + map_key.SetInt64Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int64_int64"), + map_key, &map_val)); + map_val.SetInt64Value(0); + + map_key.SetUInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_uint32_uint32"), map_key, &map_val)); + map_val.SetUInt32Value(0); + + map_key.SetUInt64Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_uint64_uint64"), map_key, &map_val)); + map_val.SetUInt64Value(0); + + map_key.SetInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_sint32_sint32"), map_key, &map_val)); + map_val.SetInt32Value(0); + + map_key.SetInt64Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_sint64_sint64"), map_key, &map_val)); + map_val.SetInt64Value(0); + + map_key.SetUInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_fixed32_fixed32"), map_key, &map_val)); + map_val.SetUInt32Value(0); + + map_key.SetUInt64Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_fixed64_fixed64"), map_key, &map_val)); + map_val.SetUInt64Value(0); + + map_key.SetInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_sfixed32_sfixed32"), map_key, &map_val)); + map_val.SetInt32Value(0); + + map_key.SetInt64Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_sfixed64_sfixed64"), map_key, &map_val)); + map_val.SetInt64Value(0); + + map_key.SetInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_float"), + map_key, &map_val)); + map_val.SetFloatValue(0.0); + + map_key.SetInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_double"), + map_key, &map_val)); + map_val.SetDoubleValue(0.0); + + map_key.SetBoolValue(false); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_bool_bool"), + map_key, &map_val)); + map_val.SetBoolValue(false); + + map_key.SetStringValue("0"); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_string_string"), map_key, &map_val)); + map_val.SetStringValue("0"); + + map_key.SetInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_bytes"), + map_key, &map_val)); + map_val.SetStringValue("0"); + + map_key.SetInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_enum"), + map_key, &map_val)); + map_val.SetEnumValue(map_enum_bar_->number()); + + map_key.SetInt32Value(0); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_int32_foreign_message"), map_key, &map_val)); + sub_foreign_message = map_val.MutableMessageValue(); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 0); + + // Add second element + map_key.SetInt32Value(1); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_int32"), + map_key, &map_val)); + map_val.SetInt32Value(1); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F("map_int32_int32"), + map_key, &map_val)); + + map_key.SetInt64Value(1); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int64_int64"), + map_key, &map_val)); + map_val.SetInt64Value(1); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F("map_int64_int64"), + map_key, &map_val)); + + map_key.SetUInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_uint32_uint32"), map_key, + &map_val); + map_val.SetUInt32Value(1); + + map_key.SetUInt64Value(1); + reflection->InsertOrLookupMapValue(message, F("map_uint64_uint64"), map_key, + &map_val); + map_val.SetUInt64Value(1); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_sint32_sint32"), map_key, + &map_val); + map_val.SetInt32Value(1); + + map_key.SetInt64Value(1); + reflection->InsertOrLookupMapValue(message, F("map_sint64_sint64"), map_key, + &map_val); + map_val.SetInt64Value(1); + + map_key.SetUInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_fixed32_fixed32"), map_key, + &map_val); + map_val.SetUInt32Value(1); + + map_key.SetUInt64Value(1); + reflection->InsertOrLookupMapValue(message, F("map_fixed64_fixed64"), map_key, + &map_val); + map_val.SetUInt64Value(1); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_sfixed32_sfixed32"), + map_key, &map_val); + map_val.SetInt32Value(1); + + map_key.SetInt64Value(1); + reflection->InsertOrLookupMapValue(message, F("map_sfixed64_sfixed64"), + map_key, &map_val); + map_val.SetInt64Value(1); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_int32_float"), map_key, + &map_val); + map_val.SetFloatValue(1.0); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_int32_double"), map_key, + &map_val); + map_val.SetDoubleValue(1.0); + + map_key.SetBoolValue(true); + reflection->InsertOrLookupMapValue(message, F("map_bool_bool"), map_key, + &map_val); + map_val.SetBoolValue(true); + + map_key.SetStringValue("1"); + reflection->InsertOrLookupMapValue(message, F("map_string_string"), map_key, + &map_val); + map_val.SetStringValue("1"); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_int32_bytes"), map_key, + &map_val); + map_val.SetStringValue("1"); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_int32_enum"), map_key, + &map_val); + map_val.SetEnumValue(map_enum_baz_->number()); + + map_key.SetInt32Value(1); + EXPECT_TRUE(reflection->InsertOrLookupMapValue( + message, F("map_int32_foreign_message"), map_key, &map_val)); + sub_foreign_message = map_val.MutableMessageValue(); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 1); +} + +inline void MapReflectionTester::GetMapValueViaMapReflection( + Message* message, const std::string& field_name, const MapKey& map_key, + MapValueRef* map_val) { + const Reflection* reflection = message->GetReflection(); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F(field_name), + map_key, map_val)); +} + +inline Message* MapReflectionTester::GetMapEntryViaReflection( + Message* message, const std::string& field_name, int index) { + const Reflection* reflection = message->GetReflection(); + return reflection->MutableRepeatedMessage(message, F(field_name), index); +} + +inline MapIterator MapReflectionTester::MapBegin( + Message* message, const std::string& field_name) { + const Reflection* reflection = message->GetReflection(); + return reflection->MapBegin(message, F(field_name)); +} + +inline MapIterator MapReflectionTester::MapEnd(Message* message, + const std::string& field_name) { + const Reflection* reflection = message->GetReflection(); + return reflection->MapEnd(message, F(field_name)); +} + +inline void MapReflectionTester::ClearMapFieldsViaReflection(Message* message) { + const Reflection* reflection = message->GetReflection(); + + reflection->ClearField(message, F("map_int32_int32")); + reflection->ClearField(message, F("map_int64_int64")); + reflection->ClearField(message, F("map_uint32_uint32")); + reflection->ClearField(message, F("map_uint64_uint64")); + reflection->ClearField(message, F("map_sint32_sint32")); + reflection->ClearField(message, F("map_sint64_sint64")); + reflection->ClearField(message, F("map_fixed32_fixed32")); + reflection->ClearField(message, F("map_fixed64_fixed64")); + reflection->ClearField(message, F("map_sfixed32_sfixed32")); + reflection->ClearField(message, F("map_sfixed64_sfixed64")); + reflection->ClearField(message, F("map_int32_float")); + reflection->ClearField(message, F("map_int32_double")); + reflection->ClearField(message, F("map_bool_bool")); + reflection->ClearField(message, F("map_string_string")); + reflection->ClearField(message, F("map_int32_bytes")); + reflection->ClearField(message, F("map_int32_enum")); + reflection->ClearField(message, F("map_int32_foreign_message")); +} + +inline void MapReflectionTester::ModifyMapFieldsViaReflection( + Message* message) { + const Reflection* reflection = message->GetReflection(); + MapValueRef map_val; + Message* sub_foreign_message; + + // Modify the second element + MapKey map_key; + map_key.SetInt32Value(1); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F("map_int32_int32"), + map_key, &map_val)); + map_val.SetInt32Value(2); + + map_key.SetInt64Value(1); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F("map_int64_int64"), + map_key, &map_val)); + map_val.SetInt64Value(2); + + map_key.SetUInt32Value(1); + EXPECT_FALSE(reflection->InsertOrLookupMapValue( + message, F("map_uint32_uint32"), map_key, &map_val)); + map_val.SetUInt32Value(2); + + map_key.SetUInt64Value(1); + reflection->InsertOrLookupMapValue(message, F("map_uint64_uint64"), map_key, + &map_val); + map_val.SetUInt64Value(2); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_sint32_sint32"), map_key, + &map_val); + map_val.SetInt32Value(2); + + map_key.SetInt64Value(1); + reflection->InsertOrLookupMapValue(message, F("map_sint64_sint64"), map_key, + &map_val); + map_val.SetInt64Value(2); + + map_key.SetUInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_fixed32_fixed32"), map_key, + &map_val); + map_val.SetUInt32Value(2); + + map_key.SetUInt64Value(1); + reflection->InsertOrLookupMapValue(message, F("map_fixed64_fixed64"), map_key, + &map_val); + map_val.SetUInt64Value(2); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_sfixed32_sfixed32"), + map_key, &map_val); + map_val.SetInt32Value(2); + + map_key.SetInt64Value(1); + reflection->InsertOrLookupMapValue(message, F("map_sfixed64_sfixed64"), + map_key, &map_val); + map_val.SetInt64Value(2); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_int32_float"), map_key, + &map_val); + map_val.SetFloatValue(2.0); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_int32_double"), map_key, + &map_val); + map_val.SetDoubleValue(2.0); + + map_key.SetBoolValue(true); + reflection->InsertOrLookupMapValue(message, F("map_bool_bool"), map_key, + &map_val); + map_val.SetBoolValue(false); + + map_key.SetStringValue("1"); + reflection->InsertOrLookupMapValue(message, F("map_string_string"), map_key, + &map_val); + map_val.SetStringValue("2"); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_int32_bytes"), map_key, + &map_val); + map_val.SetStringValue("2"); + + map_key.SetInt32Value(1); + reflection->InsertOrLookupMapValue(message, F("map_int32_enum"), map_key, + &map_val); + map_val.SetEnumValue(map_enum_foo_->number()); + + map_key.SetInt32Value(1); + EXPECT_FALSE(reflection->InsertOrLookupMapValue( + message, F("map_int32_foreign_message"), map_key, &map_val)); + sub_foreign_message = map_val.MutableMessageValue(); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 2); +} + +inline void MapReflectionTester::RemoveLastMapsViaReflection(Message* message) { + const Reflection* reflection = message->GetReflection(); + + std::vector output; + reflection->ListFields(*message, &output); + for (int i = 0; i < output.size(); ++i) { + const FieldDescriptor* field = output[i]; + if (!field->is_repeated()) continue; + reflection->RemoveLast(message, field); + } +} + +inline void MapReflectionTester::ReleaseLastMapsViaReflection( + Message* message) { + const Reflection* reflection = message->GetReflection(); + + std::vector output; + reflection->ListFields(*message, &output); + for (int i = 0; i < output.size(); ++i) { + const FieldDescriptor* field = output[i]; + if (!field->is_repeated()) continue; + if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; + + Message* released = reflection->ReleaseLast(message, field); + ASSERT_TRUE(released != nullptr) + << "ReleaseLast returned nullptr for: " << field->name(); + delete released; + } +} + +inline void MapReflectionTester::SwapMapsViaReflection(Message* message) { + const Reflection* reflection = message->GetReflection(); + std::vector output; + reflection->ListFields(*message, &output); + for (int i = 0; i < output.size(); ++i) { + const FieldDescriptor* field = output[i]; + if (!field->is_repeated()) continue; + reflection->SwapElements(message, field, 0, 1); + } +} + +inline void MapReflectionTester::MutableUnknownFieldsOfMapFieldsViaReflection( + Message* message) { + const Reflection* reflection = message->GetReflection(); + Message* sub_message = nullptr; + + sub_message = reflection->AddMessage(message, F("map_int32_int32")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_int64_int64")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_int32_float")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_int32_double")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_bool_bool")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_string_string")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_int32_bytes")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_int32_enum")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); + sub_message = reflection->AddMessage(message, F("map_int32_foreign_message")); + EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) != + nullptr); +} + +inline void MapReflectionTester::ExpectMapFieldsSetViaReflection( + const Message& message) { + std::string scratch; + const Reflection* reflection = message.GetReflection(); + const Message* sub_message; + MapKey map_key; + + // ----------------------------------------------------------------- + + ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_int32"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_int64_int64"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_uint32_uint32"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_uint64_uint64"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_sint32_sint32"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_sint64_sint64"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_fixed32_fixed32"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_fixed64_fixed64"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_sfixed32_sfixed32"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_sfixed64_sfixed64"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_float"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_double"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_bool_bool"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_string_string"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_bytes"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_enum"))); + ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_foreign_message"))); + + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + // Check with RepeatedField Reflection + sub_message = + &reflection->GetRepeatedMessage(message, F("map_int32_int32"), i); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_int32_key_); + int32 val = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_int32_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt32Value(key); + EXPECT_TRUE( + reflection->ContainsMapKey(message, F("map_int32_int32"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + // Check with RepeatedField Reflection + sub_message = + &reflection->GetRepeatedMessage(message, F("map_int64_int64"), i); + int64 key = sub_message->GetReflection()->GetInt64(*sub_message, + map_int64_int64_key_); + int64 val = sub_message->GetReflection()->GetInt64(*sub_message, + map_int64_int64_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt64Value(key); + EXPECT_TRUE( + reflection->ContainsMapKey(message, F("map_int64_int64"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + // Check with RepeatedField Reflection + sub_message = + &reflection->GetRepeatedMessage(message, F("map_uint32_uint32"), i); + uint32 key = sub_message->GetReflection()->GetUInt32( + *sub_message, map_uint32_uint32_key_); + uint32 val = sub_message->GetReflection()->GetUInt32( + *sub_message, map_uint32_uint32_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetUInt32Value(key); + EXPECT_TRUE( + reflection->ContainsMapKey(message, F("map_uint32_uint32"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_uint64_uint64"), i); + uint64 key = sub_message->GetReflection()->GetUInt64( + *sub_message, map_uint64_uint64_key_); + uint64 val = sub_message->GetReflection()->GetUInt64( + *sub_message, map_uint64_uint64_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetUInt64Value(key); + EXPECT_TRUE( + reflection->ContainsMapKey(message, F("map_uint64_uint64"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_sint32_sint32"), i); + int32 key = sub_message->GetReflection()->GetInt32( + *sub_message, map_sint32_sint32_key_); + int32 val = sub_message->GetReflection()->GetInt32( + *sub_message, map_sint32_sint32_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt32Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey( + message, F("map_sint32_sint32"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_sint64_sint64"), i); + int64 key = sub_message->GetReflection()->GetInt64( + *sub_message, map_sint64_sint64_key_); + int64 val = sub_message->GetReflection()->GetInt64( + *sub_message, map_sint64_sint64_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt64Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey( + message, F("map_sint64_sint64"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_fixed32_fixed32"), i); + uint32 key = sub_message->GetReflection()->GetUInt32( + *sub_message, map_fixed32_fixed32_key_); + uint32 val = sub_message->GetReflection()->GetUInt32( + *sub_message, map_fixed32_fixed32_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetUInt32Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey( + message, F("map_fixed32_fixed32"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_fixed64_fixed64"), i); + uint64 key = sub_message->GetReflection()->GetUInt64( + *sub_message, map_fixed64_fixed64_key_); + uint64 val = sub_message->GetReflection()->GetUInt64( + *sub_message, map_fixed64_fixed64_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetUInt64Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey( + message, F("map_fixed64_fixed64"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + sub_message = &reflection->GetRepeatedMessage( + message, F("map_sfixed32_sfixed32"), i); + int32 key = sub_message->GetReflection()->GetInt32( + *sub_message, map_sfixed32_sfixed32_key_); + int32 val = sub_message->GetReflection()->GetInt32( + *sub_message, map_sfixed32_sfixed32_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt32Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey( + message, F("map_sfixed32_sfixed32"), map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + sub_message = &reflection->GetRepeatedMessage( + message, F("map_sfixed64_sfixed64"), i); + int64 key = sub_message->GetReflection()->GetInt64( + *sub_message, map_sfixed64_sfixed64_key_); + int64 val = sub_message->GetReflection()->GetInt64( + *sub_message, map_sfixed64_sfixed64_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt64Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey( + message, F("map_sfixed64_sfixed64"), map_key)); + } + } + { + std::map map; + map[0] = 0.0; + map[1] = 1.0; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_int32_float"), i); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_float_key_); + float val = sub_message->GetReflection()->GetFloat(*sub_message, + map_int32_float_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt32Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_int32_float"), + map_key)); + } + } + { + std::map map; + map[0] = 0.0; + map[1] = 1.0; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_int32_double"), i); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_double_key_); + double val = sub_message->GetReflection()->GetDouble( + *sub_message, map_int32_double_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt32Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_int32_double"), + map_key)); + } + } + { + std::map map; + map[false] = false; + map[true] = true; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_bool_bool"), i); + bool key = sub_message->GetReflection()->GetBool(*sub_message, + map_bool_bool_key_); + bool val = sub_message->GetReflection()->GetBool(*sub_message, + map_bool_bool_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetBoolValue(key); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_bool_bool"), + map_key)); + } + } + { + std::map map; + map["0"] = "0"; + map["1"] = "1"; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_string_string"), i); + std::string key = sub_message->GetReflection()->GetString( + *sub_message, map_string_string_key_); + std::string val = sub_message->GetReflection()->GetString( + *sub_message, map_string_string_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetStringValue(key); + EXPECT_EQ(true, reflection->ContainsMapKey( + message, F("map_string_string"), map_key)); + } + } + { + std::map map; + map[0] = "0"; + map[1] = "1"; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_int32_bytes"), i); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_bytes_key_); + std::string val = sub_message->GetReflection()->GetString( + *sub_message, map_int32_bytes_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt32Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_int32_bytes"), + map_key)); + } + } + { + std::map map; + map[0] = map_enum_bar_; + map[1] = map_enum_baz_; + for (int i = 0; i < 2; i++) { + sub_message = + &reflection->GetRepeatedMessage(message, F("map_int32_enum"), i); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_enum_key_); + const EnumValueDescriptor* val = sub_message->GetReflection()->GetEnum( + *sub_message, map_int32_enum_val_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt32Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_int32_enum"), + map_key)); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (int i = 0; i < 2; i++) { + sub_message = &reflection->GetRepeatedMessage( + message, F("map_int32_foreign_message"), i); + int32 key = sub_message->GetReflection()->GetInt32( + *sub_message, map_int32_foreign_message_key_); + const Message& foreign_message = sub_message->GetReflection()->GetMessage( + *sub_message, map_int32_foreign_message_val_); + int32 val = foreign_message.GetReflection()->GetInt32(foreign_message, + foreign_c_); + EXPECT_EQ(map[key], val); + // Check with Map Reflection + map_key.SetInt32Value(key); + EXPECT_EQ(true, reflection->ContainsMapKey( + message, F("map_int32_foreign_message"), map_key)); + } + } +} + +inline void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( + Message* message) { + std::string scratch; + std::string serialized; + const Reflection* reflection = message->GetReflection(); + + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_int32"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int64_int64"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_uint32_uint32"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_uint64_uint64"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sint32_sint32"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sint64_sint64"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_fixed32_fixed32"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_fixed64_fixed64"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sfixed32_sfixed32"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sfixed64_sfixed64"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_float"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_double"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_bool_bool"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_string_string"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_bytes"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_enum"))); + ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_foreign_message"))); + + { + std::map map; + map[0] = 0; + map[1] = 1; + int size = 0; + for (MapIterator iter = reflection->MapBegin(message, F("map_int32_int32")); + iter != reflection->MapEnd(message, F("map_int32_int32")); + ++iter, ++size) { + // Check const methods do not invalidate map. + message->DebugString(); + message->ShortDebugString(); + message->SerializeToString(&serialized); + message->SpaceUsedLong(); + message->ByteSizeLong(); + EXPECT_EQ(map[iter.GetKey().GetInt32Value()], + iter.GetValueRef().GetInt32Value()); + } + EXPECT_EQ(size, 2); + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (MapIterator iter = reflection->MapBegin(message, F("map_int64_int64")); + iter != reflection->MapEnd(message, F("map_int64_int64")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetInt64Value()], + iter.GetValueRef().GetInt64Value()); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (MapIterator iter = + reflection->MapBegin(message, F("map_uint32_uint32")); + iter != reflection->MapEnd(message, F("map_uint32_uint32")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetUInt32Value()], + iter.GetValueRef().GetUInt32Value()); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (MapIterator iter = + reflection->MapBegin(message, F("map_uint64_uint64")); + iter != reflection->MapEnd(message, F("map_uint64_uint64")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetUInt64Value()], + iter.GetValueRef().GetUInt64Value()); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (MapIterator iter = + reflection->MapBegin(message, F("map_sint32_sint32")); + iter != reflection->MapEnd(message, F("map_sint32_sint32")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetInt32Value()], + iter.GetValueRef().GetInt32Value()); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (MapIterator iter = + reflection->MapBegin(message, F("map_sint64_sint64")); + iter != reflection->MapEnd(message, F("map_sint64_sint64")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetInt64Value()], + iter.GetValueRef().GetInt64Value()); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (MapIterator iter = + reflection->MapBegin(message, F("map_fixed32_fixed32")); + iter != reflection->MapEnd(message, F("map_fixed32_fixed32")); + ++iter) { + EXPECT_EQ(map[iter.GetKey().GetUInt32Value()], + iter.GetValueRef().GetUInt32Value()); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (MapIterator iter = + reflection->MapBegin(message, F("map_fixed64_fixed64")); + iter != reflection->MapEnd(message, F("map_fixed64_fixed64")); + ++iter) { + EXPECT_EQ(map[iter.GetKey().GetUInt64Value()], + iter.GetValueRef().GetUInt64Value()); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + for (MapIterator iter = + reflection->MapBegin(message, F("map_sfixed32_sfixed32")); + iter != reflection->MapEnd(message, F("map_sfixed32_sfixed32")); + ++iter) { + EXPECT_EQ(map[iter.GetKey().GetInt32Value()], + iter.GetValueRef().GetInt32Value()); + } + } + { + std::map map; + map[0] = 0.0; + map[1] = 1.0; + for (MapIterator iter = reflection->MapBegin(message, F("map_int32_float")); + iter != reflection->MapEnd(message, F("map_int32_float")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetInt32Value()], + iter.GetValueRef().GetFloatValue()); + } + } + { + std::map map; + map[0] = 0.0; + map[1] = 1.0; + for (MapIterator iter = + reflection->MapBegin(message, F("map_int32_double")); + iter != reflection->MapEnd(message, F("map_int32_double")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetInt32Value()], + iter.GetValueRef().GetDoubleValue()); + } + } + { + std::map map; + map[false] = false; + map[true] = true; + for (MapIterator iter = reflection->MapBegin(message, F("map_bool_bool")); + iter != reflection->MapEnd(message, F("map_bool_bool")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetBoolValue()], + iter.GetValueRef().GetBoolValue()); + } + } + { + std::map map; + map["0"] = "0"; + map["1"] = "1"; + int size = 0; + for (MapIterator iter = + reflection->MapBegin(message, F("map_string_string")); + iter != reflection->MapEnd(message, F("map_string_string")); + ++iter, ++size) { + // Check const methods do not invalidate map. + message->DebugString(); + message->ShortDebugString(); + message->SerializeToString(&serialized); + message->SpaceUsedLong(); + message->ByteSizeLong(); + EXPECT_EQ(map[iter.GetKey().GetStringValue()], + iter.GetValueRef().GetStringValue()); + } + EXPECT_EQ(size, 2); + } + { + std::map map; + map[0] = "0"; + map[1] = "1"; + for (MapIterator iter = reflection->MapBegin(message, F("map_int32_bytes")); + iter != reflection->MapEnd(message, F("map_int32_bytes")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetInt32Value()], + iter.GetValueRef().GetStringValue()); + } + } + { + std::map map; + map[0] = map_enum_bar_; + map[1] = map_enum_baz_; + for (MapIterator iter = reflection->MapBegin(message, F("map_int32_enum")); + iter != reflection->MapEnd(message, F("map_int32_enum")); ++iter) { + EXPECT_EQ(map[iter.GetKey().GetInt32Value()]->number(), + iter.GetValueRef().GetEnumValue()); + } + } + { + std::map map; + map[0] = 0; + map[1] = 1; + int size = 0; + for (MapIterator iter = + reflection->MapBegin(message, F("map_int32_foreign_message")); + iter != reflection->MapEnd(message, F("map_int32_foreign_message")); + ++iter, ++size) { + // Check const methods do not invalidate map. + message->DebugString(); + message->ShortDebugString(); + message->SerializeToString(&serialized); + message->SpaceUsedLong(); + message->ByteSizeLong(); + const Message& sub_message = iter.GetValueRef().GetMessageValue(); + EXPECT_EQ(map[iter.GetKey().GetInt32Value()], + sub_message.GetReflection()->GetInt32(sub_message, foreign_c_)); + } + EXPECT_EQ(size, 2); + } +} + +inline void MapReflectionTester::ExpectClearViaReflection( + const Message& message) { + const Reflection* reflection = message.GetReflection(); + // Map fields are empty. + EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_int32"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_int64_int64"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_uint32_uint32"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_uint64_uint64"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_sint32_sint32"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_sint64_sint64"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_fixed32_fixed32"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_fixed64_fixed64"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_sfixed32_sfixed32"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_sfixed64_sfixed64"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_float"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_double"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_bool_bool"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_string_string"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_bytes"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_enum"))); + EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_foreign_message"))); + EXPECT_TRUE(reflection->GetMapData(message, F("map_int32_foreign_message")) + ->IsMapValid()); +} + +inline void MapReflectionTester::ExpectClearViaReflectionIterator( + Message* message) { + const Reflection* reflection = message->GetReflection(); + EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_int32")) == + reflection->MapEnd(message, F("map_int32_int32"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_int64_int64")) == + reflection->MapEnd(message, F("map_int64_int64"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_uint32_uint32")) == + reflection->MapEnd(message, F("map_uint32_uint32"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_uint64_uint64")) == + reflection->MapEnd(message, F("map_uint64_uint64"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_sint32_sint32")) == + reflection->MapEnd(message, F("map_sint32_sint32"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_sint64_sint64")) == + reflection->MapEnd(message, F("map_sint64_sint64"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_fixed32_fixed32")) == + reflection->MapEnd(message, F("map_fixed32_fixed32"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_fixed64_fixed64")) == + reflection->MapEnd(message, F("map_fixed64_fixed64"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_sfixed32_sfixed32")) == + reflection->MapEnd(message, F("map_sfixed32_sfixed32"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_sfixed64_sfixed64")) == + reflection->MapEnd(message, F("map_sfixed64_sfixed64"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_float")) == + reflection->MapEnd(message, F("map_int32_float"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_double")) == + reflection->MapEnd(message, F("map_int32_double"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_bool_bool")) == + reflection->MapEnd(message, F("map_bool_bool"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_string_string")) == + reflection->MapEnd(message, F("map_string_string"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_bytes")) == + reflection->MapEnd(message, F("map_int32_bytes"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_enum")) == + reflection->MapEnd(message, F("map_int32_enum"))); + EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_foreign_message")) == + reflection->MapEnd(message, F("map_int32_foreign_message"))); +} + } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_MAP_TEST_UTIL_H__ diff --git a/src/google/protobuf/map_test_util.inc b/src/google/protobuf/map_test_util.inc new file mode 100755 index 0000000000..600fdc36a0 --- /dev/null +++ b/src/google/protobuf/map_test_util.inc @@ -0,0 +1,271 @@ +// 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 { + +class MapTestUtil { + public: + // Set every field in the TestMap message to a unique value. + static void SetMapFields(UNITTEST::TestMap* message); + + // Set every field in the TestArenaMap message to a unique value. + static void SetArenaMapFields(UNITTEST::TestArenaMap* message); + + // Set every field in the message to a default value. + static void SetMapFieldsInitialized(UNITTEST::TestMap* message); + + // Modify all the map fields of the message (which should already have been + // initialized with SetMapFields()). + static void ModifyMapFields(UNITTEST::TestMap* message); + + // Check that all fields have the values that they should have after + // SetMapFields() is called. + static void ExpectMapFieldsSet(const UNITTEST::TestMap& message); + + // Check that all fields have the values that they should have after + // SetMapFields() is called for TestArenaMap. + static void ExpectArenaMapFieldsSet(const UNITTEST::TestArenaMap& message); + + // Check that all fields have the values that they should have after + // SetMapFieldsInitialized() is called. + static void ExpectMapFieldsSetInitialized(const UNITTEST::TestMap& message); + + // Expect that the message is modified as would be expected from + // ModifyMapFields(). + static void ExpectMapFieldsModified(const UNITTEST::TestMap& message); + + // Check that all fields are empty. + static void ExpectClear(const UNITTEST::TestMap& message); + + // Check that all map fields have the given size. + static void ExpectMapsSize(const UNITTEST::TestMap& message, int size); + + // Get pointers of map entries at given index. + static std::vector GetMapEntries( + const UNITTEST::TestMap& message, int index); + + // Get pointers of map entries from release. + static std::vector GetMapEntriesFromRelease( + UNITTEST::TestMap* message); +}; + +inline void MapTestUtil::SetMapFields(UNITTEST::TestMap* message) { + MapTestUtilImpl::SetMapFields(message); +} + +inline void MapTestUtil::SetArenaMapFields(UNITTEST::TestArenaMap* message) { + MapTestUtilImpl::SetArenaMapFields(message); +} + +inline void MapTestUtil::SetMapFieldsInitialized(UNITTEST::TestMap* message) { + MapTestUtilImpl::SetMapFieldsInitialized(message); +} + +inline void MapTestUtil::ModifyMapFields(UNITTEST::TestMap* message) { + MapTestUtilImpl::ModifyMapFields( + message); +} + +inline void MapTestUtil::ExpectClear(const UNITTEST::TestMap& message) { + MapTestUtilImpl::ExpectClear(message); +} + +inline void MapTestUtil::ExpectMapFieldsSet(const UNITTEST::TestMap& message) { + MapTestUtilImpl::ExpectMapFieldsSet(message); +} + +inline void MapTestUtil::ExpectArenaMapFieldsSet( + const UNITTEST::TestArenaMap& message) { + MapTestUtilImpl::ExpectArenaMapFieldsSet< + UNITTEST::MapEnum, UNITTEST::MAP_ENUM_BAR, UNITTEST::MAP_ENUM_BAZ>( + message); +} + +inline void MapTestUtil::ExpectMapFieldsSetInitialized( + const UNITTEST::TestMap& message) { + MapTestUtilImpl::ExpectMapFieldsSetInitialized( + message); +} + +inline void MapTestUtil::ExpectMapFieldsModified( + const UNITTEST::TestMap& message) { + MapTestUtilImpl::ExpectMapFieldsModified< + UNITTEST::MapEnum, UNITTEST::MAP_ENUM_BAR, UNITTEST::MAP_ENUM_FOO>( + message); +} + +inline void MapTestUtil::ExpectMapsSize(const UNITTEST::TestMap& message, + int size) { + const Descriptor* descriptor = message.GetDescriptor(); + + EXPECT_EQ(size, message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_int32_int32"))); + EXPECT_EQ(size, message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_int64_int64"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_uint32_uint32"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_uint64_uint64"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_sint32_sint32"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_sint64_sint64"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_fixed32_fixed32"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_fixed64_fixed64"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); + EXPECT_EQ(size, message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_int32_float"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_int32_double"))); + EXPECT_EQ(size, message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_bool_bool"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_string_string"))); + EXPECT_EQ(size, message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_int32_bytes"))); + EXPECT_EQ( + size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_int32_foreign_message"))); +} + +inline std::vector MapTestUtil::GetMapEntries( + const UNITTEST::TestMap& message, int index) { + const Descriptor* descriptor = message.GetDescriptor(); + std::vector result; + + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_int32_int32"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_int64_int64"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_uint32_uint32"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_uint64_uint64"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_sint32_sint32"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_sint64_sint64"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_fixed32_fixed32"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_fixed64_fixed64"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_sfixed32_sfixed32"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_sfixed64_sfixed64"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_int32_float"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_int32_double"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_bool_bool"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_string_string"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_int32_bytes"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_int32_enum"), index)); + result.push_back(&message.GetReflection()->GetRepeatedMessage( + message, descriptor->FindFieldByName("map_int32_foreign_message"), + index)); + + return result; +} + +inline std::vector MapTestUtil::GetMapEntriesFromRelease( + UNITTEST::TestMap* message) { + const Descriptor* descriptor = message->GetDescriptor(); + std::vector result; + + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_int32_int32"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_int64_int64"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_uint32_uint32"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_uint64_uint64"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_sint32_sint32"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_sint64_sint64"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_fixed32_fixed32"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_fixed64_fixed64"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_int32_float"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_int32_double"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_bool_bool"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_string_string"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_int32_bytes"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_int32_enum"))); + result.push_back(message->GetReflection()->ReleaseLast( + message, descriptor->FindFieldByName("map_int32_foreign_message"))); + + return result; +} + +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/map_test_util_impl.h b/src/google/protobuf/map_test_util_impl.h index 2e8b4ba4c7..70145b7e51 100644 --- a/src/google/protobuf/map_test_util_impl.h +++ b/src/google/protobuf/map_test_util_impl.h @@ -36,7 +36,7 @@ #include -namespace protobuf_unittest {} // forward declaration +namespace protobuf_unittest {} // namespace protobuf_unittest namespace google { namespace protobuf { @@ -46,13 +46,13 @@ namespace unittest = ::protobuf_unittest; class MapTestUtilImpl { public: // Set every field in the TestMap message to a unique value. - template + template static void SetMapFields(MapMessage* message); // Set every field in the TestArenaMap message to a unique value. - template + template static void SetArenaMapFields(MapMessage* message); // Set every field in the message to a default value. @@ -66,14 +66,14 @@ class MapTestUtilImpl { // Check that all fields have the values that they should have after // SetMapFields() is called. - template + template static void ExpectMapFieldsSet(const MapMessage& message); // Check that all fields have the values that they should have after // SetMapFields() is called for TestArenaMap. - template + template static void ExpectArenaMapFieldsSet(const MapMessage& message); // Check that all fields have the values that they should have after @@ -83,8 +83,8 @@ class MapTestUtilImpl { // Expect that the message is modified as would be expected from // ModifyMapFields(). - template + template static void ExpectMapFieldsModified(const MapMessage& message); // Check that all fields are empty. @@ -104,8 +104,8 @@ class MapTestUtilImpl { // MapMessage* message); }; -template +template void MapTestUtilImpl::SetMapFields(MapMessage* message) { // Add first element. (*message->mutable_map_int32_int32())[0] = 0; @@ -146,8 +146,8 @@ void MapTestUtilImpl::SetMapFields(MapMessage* message) { (*message->mutable_map_int32_foreign_message())[1].set_c(1); } -template +template void MapTestUtilImpl::SetArenaMapFields(MapMessage* message) { // Add first element. (*message->mutable_map_int32_int32())[0] = 0; @@ -255,10 +255,8 @@ void MapTestUtilImpl::ExpectClear(const MapMessage& message) { EXPECT_EQ(0, message.map_int32_foreign_message().size()); } - - -template +template void MapTestUtilImpl::ExpectMapFieldsSet(const MapMessage& message) { ASSERT_EQ(2, message.map_int32_int32().size()); ASSERT_EQ(2, message.map_int64_int64().size()); @@ -315,8 +313,8 @@ void MapTestUtilImpl::ExpectMapFieldsSet(const MapMessage& message) { EXPECT_EQ(1, message.map_int32_foreign_message().at(1).c()); } -template +template void MapTestUtilImpl::ExpectArenaMapFieldsSet(const MapMessage& message) { EXPECT_EQ(2, message.map_int32_int32().size()); EXPECT_EQ(2, message.map_int64_int64().size()); @@ -377,8 +375,7 @@ void MapTestUtilImpl::ExpectArenaMapFieldsSet(const MapMessage& message) { } template -void MapTestUtilImpl::ExpectMapFieldsSetInitialized( - const MapMessage& message) { +void MapTestUtilImpl::ExpectMapFieldsSetInitialized(const MapMessage& message) { EXPECT_EQ(1, message.map_int32_int32().size()); EXPECT_EQ(1, message.map_int64_int64().size()); EXPECT_EQ(1, message.map_uint32_uint32().size()); @@ -416,10 +413,9 @@ void MapTestUtilImpl::ExpectMapFieldsSetInitialized( EXPECT_EQ(0, message.map_int32_foreign_message().at(0).ByteSize()); } -template -void MapTestUtilImpl::ExpectMapFieldsModified( - const MapMessage& message) { +template +void MapTestUtilImpl::ExpectMapFieldsModified(const MapMessage& message) { // ModifyMapFields only sets the second element of each field. In addition to // verifying this, we also verify that the first element and size were *not* // modified. diff --git a/src/google/protobuf/map_type_handler.h b/src/google/protobuf/map_type_handler.h index 0489851e55..b633969615 100644 --- a/src/google/protobuf/map_type_handler.h +++ b/src/google/protobuf/map_type_handler.h @@ -31,8 +31,10 @@ #ifndef GOOGLE_PROTOBUF_TYPE_HANDLER_H__ #define GOOGLE_PROTOBUF_TYPE_HANDLER_H__ +#include +#include #include -#include +#include #ifdef SWIG #error "You cannot SWIG proto headers" @@ -44,15 +46,15 @@ namespace internal { // Used for compile time type selection. MapIf::type will be TrueType if Flag is // true and FalseType otherwise. -template +template struct MapIf; -template +template struct MapIf { typedef TrueType type; }; -template +template struct MapIf { typedef FalseType type; }; @@ -76,7 +78,8 @@ class MapValueInitializer { template class MapValueInitializer { public: - static inline void Initialize(Type& /* value */, int /* default_enum_value */) {} + static inline void Initialize(Type& /* value */, + int /* default_enum_value */) {} }; template @@ -117,23 +120,23 @@ class MapWireFieldTypeTraits {}; 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) +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 @@ -146,7 +149,8 @@ class MapTypeHandler { // Enum type cannot be used for MapTypeHandler::Read. Define a type which will // replace Enum with int. typedef typename MapWireFieldTypeTraits::MapEntryAccessorType MapEntryAccessorType; + Type>::MapEntryAccessorType + MapEntryAccessorType; // Internal stored type in MapEntryLite for given wire field type. typedef typename MapWireFieldTypeTraits::TypeOnMemory TypeOnMemory; @@ -165,14 +169,11 @@ class MapTypeHandler { static inline int GetCachedSize(const MapEntryAccessorType& value); static inline bool Read(io::CodedInputStream* input, MapEntryAccessorType* value); - static inline void Write(int field, const MapEntryAccessorType& value, - io::CodedOutputStream* output); - static inline uint8* InternalWriteToArray(int field, - const MapEntryAccessorType& value, - bool deterministic, uint8* target); - static inline uint8* WriteToArray(int field, - const MapEntryAccessorType& value, - uint8* target); + 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); @@ -196,8 +197,8 @@ class MapTypeHandler { static inline void AssignDefaultValue(Type** value); // Return default instance if value is not initialized when calling const // reference accessor. - static inline const Type& DefaultIfNotInitialized( - const Type* value, const Type* default_value); + static inline const Type& DefaultIfNotInitialized(const Type* value, + const Type* default_value); // Check if all required fields have values set. static inline bool IsInitialized(Type* value); }; @@ -224,16 +225,10 @@ class MapTypeHandler { static inline int GetCachedSize(const MapEntryAccessorType& value); \ static inline bool Read(io::CodedInputStream* input, \ MapEntryAccessorType* value); \ - static inline void Write(int field, const MapEntryAccessorType& value, \ - io::CodedOutputStream* output); \ - static inline uint8* InternalWriteToArray( \ - int field, const MapEntryAccessorType& value, bool deterministic, \ - uint8* target); \ - static inline uint8* WriteToArray(int field, \ - const MapEntryAccessorType& value, \ - uint8* target) { \ - return InternalWriteToArray(field, value, false, target); \ - } \ + 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); \ @@ -245,7 +240,7 @@ class MapTypeHandler { int default_enum); \ static inline size_t SpaceUsedInMapEntryLong(const TypeOnMemory& value); \ static inline size_t SpaceUsedInMapLong(const TypeOnMemory& value); \ - static inline size_t SpaceUsedInMapLong(const string& value); \ + static inline size_t SpaceUsedInMapLong(const std::string& value); \ static inline void AssignDefaultValue(TypeOnMemory* value); \ static inline const MapEntryAccessorType& DefaultIfNotInitialized( \ const TypeOnMemory& value, const TypeOnMemory& default_value); \ @@ -277,8 +272,7 @@ MAP_HANDLER(BOOL) #undef MAP_HANDLER template -inline size_t -MapTypeHandler::ByteSize( +inline size_t MapTypeHandler::ByteSize( const MapEntryAccessorType& value) { return WireFormatLite::MessageSizeNoVirtual(value); } @@ -291,14 +285,14 @@ MapTypeHandler::ByteSize( } GOOGLE_PROTOBUF_BYTE_SIZE(STRING, String) -GOOGLE_PROTOBUF_BYTE_SIZE(BYTES , Bytes) -GOOGLE_PROTOBUF_BYTE_SIZE(INT64 , Int64) +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(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) +GOOGLE_PROTOBUF_BYTE_SIZE(ENUM, Enum) #undef GOOGLE_PROTOBUF_BYTE_SIZE @@ -309,23 +303,21 @@ GOOGLE_PROTOBUF_BYTE_SIZE(ENUM , Enum) 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(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) +FIXED_BYTE_SIZE(BOOL, Bool) #undef FIXED_BYTE_SIZE template -inline int -MapTypeHandler::GetCachedSize( +inline int MapTypeHandler::GetCachedSize( const MapEntryAccessorType& value) { - return static_cast( - WireFormatLite::LengthDelimitedSize( - static_cast(value.GetCachedSize()))); + return static_cast(WireFormatLite::LengthDelimitedSize( + static_cast(value.GetCachedSize()))); } #define GET_CACHED_SIZE(FieldType, DeclaredType) \ @@ -337,14 +329,14 @@ MapTypeHandler::GetCachedSize( } GET_CACHED_SIZE(STRING, String) -GET_CACHED_SIZE(BYTES , Bytes) -GET_CACHED_SIZE(INT64 , Int64) +GET_CACHED_SIZE(BYTES, Bytes) +GET_CACHED_SIZE(INT64, Int64) GET_CACHED_SIZE(UINT64, UInt64) -GET_CACHED_SIZE(INT32 , Int32) +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) +GET_CACHED_SIZE(ENUM, Enum) #undef GET_CACHED_SIZE @@ -356,63 +348,60 @@ GET_CACHED_SIZE(ENUM , Enum) 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(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) +GET_FIXED_CACHED_SIZE(BOOL, Bool) #undef GET_FIXED_CACHED_SIZE template -inline void MapTypeHandler::Write( - int field, const MapEntryAccessorType& value, - io::CodedOutputStream* output) { - WireFormatLite::WriteMessageMaybeToArray(field, value, output); -} - -template -inline uint8* -MapTypeHandler::InternalWriteToArray( - int field, const MapEntryAccessorType& value, bool deterministic, - uint8* target) { - return WireFormatLite::InternalWriteMessageToArray(field, value, - deterministic, target); +inline uint8* MapTypeHandler::Write( + int field, const MapEntryAccessorType& value, uint8* ptr, + io::EpsCopyOutputStream* stream) { + stream->EnsureSpace(&ptr); + return WireFormatLite::InternalWriteMessageToArray(field, value, ptr, stream); } #define WRITE_METHOD(FieldType, DeclaredType) \ template \ - inline void MapTypeHandler::Write( \ - int field, const MapEntryAccessorType& value, \ - io::CodedOutputStream* output) { \ - return WireFormatLite::Write##DeclaredType(field, value, output); \ - } \ + inline uint8* MapTypeHandler::Write( \ + int field, const MapEntryAccessorType& value, uint8* ptr, \ + io::EpsCopyOutputStream* stream) { \ + 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::InternalWriteToArray( \ - int field, const MapEntryAccessorType& value, bool, uint8* target) { \ - return WireFormatLite::Write##DeclaredType##ToArray(field, value, target); \ + inline uint8* MapTypeHandler::Write( \ + int field, const MapEntryAccessorType& value, uint8* ptr, \ + io::EpsCopyOutputStream* stream) { \ + stream->EnsureSpace(&ptr); \ + return WireFormatLite::Write##DeclaredType##ToArray(field, value, ptr); \ } -WRITE_METHOD(STRING , String) -WRITE_METHOD(BYTES , Bytes) -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(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) +WRITE_METHOD(BOOL, Bool) #undef WRITE_METHOD @@ -434,6 +423,96 @@ inline bool MapTypeHandler::Read( 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 ParseVarint64(ptr, reinterpret_cast(value)); +} +inline const char* ReadUINT64(const char* ptr, uint64* value) { + return ParseVarint64(ptr, value); +} +inline const char* ReadINT32(const char* ptr, int32* value) { + uint64 tmp; + auto res = ParseVarint64(ptr, &tmp); + *value = static_cast(tmp); + return res; +} +inline const char* ReadUINT32(const char* ptr, uint32* value) { + uint64 tmp; + auto res = ParseVarint64(ptr, &tmp); + *value = static_cast(tmp); + return res; +} +inline const char* ReadSINT64(const char* ptr, int64* value) { + uint64 tmp; + auto res = ParseVarint64(ptr, &tmp); + *value = WireFormatLite::ZigZagDecode64(tmp); + return res; +} +inline const char* ReadSINT32(const char* ptr, int32* value) { + uint64 tmp; + auto res = ParseVarint64(ptr, &tmp); + *value = WireFormatLite::ZigZagDecode32(static_cast(tmp)); + return res; +} +template +inline const char* ReadENUM(const char* ptr, E* value) { + uint64 tmp; + auto res = ParseVarint64(ptr, &tmp); + *value = static_cast(tmp); + return res; +} +inline const char* ReadBOOL(const char* ptr, bool* value) { + uint64 tmp; + auto res = ParseVarint64(ptr, &tmp); + *value = static_cast(tmp); + return res; +} + +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( \ @@ -441,6 +520,12 @@ inline bool MapTypeHandler::Read( 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) @@ -464,8 +549,8 @@ READ_METHOD(BOOL) template inline const Type& -MapTypeHandler::GetExternalReference(const Type* value) { +MapTypeHandler::GetExternalReference( + const Type* value) { return *value; } @@ -488,10 +573,8 @@ inline void MapTypeHandler::Clear( } template inline void -MapTypeHandler::ClearMaybeByDefaultEnum(Type** value, - Arena* /* arena */, - int /* default_enum_value */) { +MapTypeHandler::ClearMaybeByDefaultEnum( + Type** value, Arena* /* arena */, int /* default_enum_value */) { if (*value != NULL) (*value)->Clear(); } template @@ -508,14 +591,13 @@ void MapTypeHandler::DeleteNoArena( template inline void MapTypeHandler::AssignDefaultValue(Type** value) { + Type>::AssignDefaultValue(Type** value) { *value = const_cast(Type::internal_default_instance()); } template -inline void MapTypeHandler::Initialize(Type** x, - Arena* /* arena */) { +inline void MapTypeHandler::Initialize( + Type** x, Arena* /* arena */) { *x = NULL; } @@ -527,26 +609,26 @@ inline void MapTypeHandler:: } template -inline Type* MapTypeHandler::EnsureMutable(Type** value, - Arena* arena) { +inline Type* MapTypeHandler::EnsureMutable( + Type** value, Arena* arena) { if (*value == NULL) { - *value = - MapArenaMessageCreator:: - type::value>::CreateMessage(arena); + *value = MapArenaMessageCreator< + Type, + Arena::is_arena_constructable::type::value>::CreateMessage(arena); } return *value; } template -inline const Type& MapTypeHandler:: - DefaultIfNotInitialized(const Type* value, const Type* default_value) { +inline const Type& +MapTypeHandler::DefaultIfNotInitialized( + const Type* value, const Type* default_value) { return value != NULL ? *value : *default_value; } template -inline bool MapTypeHandler::IsInitialized(Type* value) { +inline bool MapTypeHandler::IsInitialized( + Type* value) { return value->IsInitialized(); } @@ -575,7 +657,7 @@ inline bool MapTypeHandler \ inline size_t \ MapTypeHandler::SpaceUsedInMapLong( \ - const string& value) { \ + const std::string& value) { \ return sizeof(value); \ } \ template \ @@ -640,82 +722,81 @@ 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 size_t \ - MapTypeHandler::SpaceUsedInMapLong( \ - const TypeOnMemory& /* value */) { \ - return sizeof(Type); \ - } \ - template \ - inline void MapTypeHandler::Clear( \ - TypeOnMemory* value, Arena* /* arena */) { \ - *value = 0; \ - } \ - template \ - inline void MapTypeHandler:: \ - ClearMaybeByDefaultEnum(TypeOnMemory* value, Arena* /* arena */, \ - int default_enum_value) { \ - *value = static_cast(default_enum_value); \ - } \ - template \ - inline void MapTypeHandler::Merge( \ - const MapEntryAccessorType& from, TypeOnMemory* to, \ - Arena* /* arena */) { \ - *to = from; \ - } \ - template \ - inline void MapTypeHandler::DeleteNoArena(TypeOnMemory& /* x */) {} \ - template \ - inline void MapTypeHandler::AssignDefaultValue(TypeOnMemory* /* value */) {} \ - template \ - inline void \ - MapTypeHandler::Initialize( \ - TypeOnMemory* value, Arena* /* arena */) { \ - *value = 0; \ - } \ - template \ - inline void MapTypeHandler:: \ - InitializeMaybeByDefaultEnum(TypeOnMemory* value, \ - int default_enum_value, \ - Arena* /* arena */) { \ - *value = static_cast(default_enum_value); \ - } \ - template \ - inline typename MapTypeHandler::MapEntryAccessorType* \ - MapTypeHandler::EnsureMutable( \ - TypeOnMemory* value, Arena* /* arena */) { \ - return value; \ - } \ - template \ - inline const typename MapTypeHandler::MapEntryAccessorType& \ - MapTypeHandler::DefaultIfNotInitialized(const TypeOnMemory& value, \ - const TypeOnMemory& \ - /* default_value */) { \ - return value; \ - } \ - template \ - inline bool MapTypeHandler::IsInitialized(const TypeOnMemory& /* value */) { \ - return true; \ +#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 size_t \ + MapTypeHandler::SpaceUsedInMapLong( \ + const TypeOnMemory& /* value */) { \ + return sizeof(Type); \ + } \ + template \ + inline void MapTypeHandler::Clear( \ + TypeOnMemory* value, Arena* /* arena */) { \ + *value = 0; \ + } \ + template \ + inline void MapTypeHandler:: \ + ClearMaybeByDefaultEnum(TypeOnMemory* value, Arena* /* arena */, \ + int default_enum_value) { \ + *value = static_cast(default_enum_value); \ + } \ + template \ + inline void MapTypeHandler::Merge( \ + const MapEntryAccessorType& from, TypeOnMemory* to, \ + Arena* /* arena */) { \ + *to = from; \ + } \ + template \ + inline void MapTypeHandler::DeleteNoArena(TypeOnMemory& /* x */) {} \ + template \ + inline void \ + MapTypeHandler::AssignDefaultValue( \ + TypeOnMemory* /* value */) {} \ + template \ + inline void \ + MapTypeHandler::Initialize( \ + TypeOnMemory* value, Arena* /* arena */) { \ + *value = 0; \ + } \ + template \ + inline void MapTypeHandler:: \ + InitializeMaybeByDefaultEnum( \ + TypeOnMemory* value, int default_enum_value, Arena* /* arena */) { \ + *value = static_cast(default_enum_value); \ + } \ + template \ + inline typename MapTypeHandler::MapEntryAccessorType* \ + MapTypeHandler::EnsureMutable( \ + TypeOnMemory* value, Arena* /* arena */) { \ + return value; \ + } \ + template \ + inline const typename MapTypeHandler::MapEntryAccessorType& \ + MapTypeHandler:: \ + DefaultIfNotInitialized(const TypeOnMemory& value, \ + const TypeOnMemory& /* default_value */) { \ + return value; \ + } \ + template \ + inline bool \ + MapTypeHandler::IsInitialized( \ + const TypeOnMemory& /* value */) { \ + return true; \ } PRIMITIVE_HANDLER_FUNCTIONS(INT64) PRIMITIVE_HANDLER_FUNCTIONS(UINT64) diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc index b5a624b718..ab7f5a2df6 100644 --- a/src/google/protobuf/message.cc +++ b/src/google/protobuf/message.cc @@ -36,12 +36,14 @@ #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -50,7 +52,9 @@ #include #include #include +#include #include +#include #include #include @@ -62,9 +66,15 @@ namespace google { namespace protobuf { -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -using internal::ParseClosure; -#endif +namespace internal { + +// TODO(gerbens) make this factorized better. This should not have to hop +// to reflection. Currently uses GeneratedMessageReflection and thus is +// defined in generated_message_reflection.cc +void RegisterFileLevelMetadata(const DescriptorTable* descriptor_table); + +} // namespace internal + using internal::ReflectionOps; using internal::WireFormat; using internal::WireFormatLite; @@ -72,9 +82,12 @@ using internal::WireFormatLite; void Message::MergeFrom(const Message& from) { const Descriptor* descriptor = GetDescriptor(); GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) - << ": Tried to merge from a message with a different type. " - "to: " << descriptor->full_name() << ", " - "from: " << from.GetDescriptor()->full_name(); + << ": Tried to merge from a message with a different type. " + "to: " + << descriptor->full_name() + << ", " + "from: " + << from.GetDescriptor()->full_name(); ReflectionOps::Merge(from, this); } @@ -85,38 +98,39 @@ void Message::CheckTypeAndMergeFrom(const MessageLite& other) { void Message::CopyFrom(const Message& from) { const Descriptor* descriptor = GetDescriptor(); GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) - << ": Tried to copy from a message with a different type. " - "to: " << descriptor->full_name() << ", " - "from: " << from.GetDescriptor()->full_name(); + << ": Tried to copy from a message with a different type. " + "to: " + << descriptor->full_name() + << ", " + "from: " + << from.GetDescriptor()->full_name(); ReflectionOps::Copy(from, this); } -string Message::GetTypeName() const { +std::string Message::GetTypeName() const { return GetDescriptor()->full_name(); } -void Message::Clear() { - ReflectionOps::Clear(this); -} +void Message::Clear() { ReflectionOps::Clear(this); } bool Message::IsInitialized() const { return ReflectionOps::IsInitialized(*this); } -void Message::FindInitializationErrors(std::vector* errors) const { +void Message::FindInitializationErrors(std::vector* errors) const { return ReflectionOps::FindInitializationErrors(*this, "", errors); } -string Message::InitializationErrorString() const { - std::vector errors; +std::string Message::InitializationErrorString() const { + std::vector errors; FindInitializationErrors(&errors); return Join(errors, ", "); } void Message::CheckInitialized() const { - GOOGLE_CHECK(IsInitialized()) - << "Message of type \"" << GetDescriptor()->full_name() - << "\" is missing required fields: " << InitializationErrorString(); + GOOGLE_CHECK(IsInitialized()) << "Message of type \"" << GetDescriptor()->full_name() + << "\" is missing required fields: " + << InitializationErrorString(); } void Message::DiscardUnknownFields() { @@ -129,26 +143,6 @@ bool Message::MergePartialFromCodedStream(io::CodedInputStream* input) { } #endif -bool Message::ParseFromFileDescriptor(int file_descriptor) { - io::FileInputStream input(file_descriptor); - return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0; -} - -bool Message::ParsePartialFromFileDescriptor(int file_descriptor) { - io::FileInputStream input(file_descriptor); - return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0; -} - -bool Message::ParseFromIstream(std::istream* input) { - io::IstreamInputStream zero_copy_input(input); - return ParseFromZeroCopyStream(&zero_copy_input) && input->eof(); -} - -bool Message::ParsePartialFromIstream(std::istream* input) { - io::IstreamInputStream zero_copy_input(input); - return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof(); -} - #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER namespace internal { @@ -156,27 +150,18 @@ class ReflectionAccessor { public: static void* GetOffset(void* msg, const google::protobuf::FieldDescriptor* f, const google::protobuf::Reflection* r) { - auto gr = - dynamic_cast(r); - GOOGLE_CHECK(gr != nullptr); - return static_cast(msg) + gr->schema_.GetFieldOffset(f); + return static_cast(msg) + r->schema_.GetFieldOffset(f); } - static google::protobuf::internal::ExtensionSet* GetExtensionSet( - void* msg, const google::protobuf::Reflection* r) { - auto gr = - dynamic_cast(r); - GOOGLE_CHECK(gr != nullptr); - return reinterpret_cast( - static_cast(msg) + gr->schema_.GetExtensionSetOffset()); + static void* GetRepeatedEnum(const Reflection* reflection, + const FieldDescriptor* field, Message* msg) { + return reflection->MutableRawRepeatedField( + msg, field, FieldDescriptor::CPPTYPE_ENUM, 0, nullptr); } - static google::protobuf::internal::InternalMetadataWithArena* GetMetadata( - void* msg, const google::protobuf::Reflection* r) { - auto gr = - dynamic_cast(r); - GOOGLE_CHECK(gr != nullptr); - return reinterpret_cast( - static_cast(msg) + gr->schema_.GetMetadataOffset()); + + static InternalMetadataWithArena* MutableInternalMetadataWithArena( + const Reflection* reflection, Message* msg) { + return reflection->MutableInternalMetadataWithArena(msg); } }; @@ -260,14 +245,14 @@ bool ReflectiveValidator(const void* arg, int val) { return d->FindValueByNumber(val) != nullptr; } -ParseClosure GetPackedField(const FieldDescriptor* field, Message* msg, - const Reflection* reflection, - internal::ParseContext* ctx) { +const char* ParsePackedField(const FieldDescriptor* field, Message* msg, + const Reflection* reflection, const char* ptr, + internal::ParseContext* ctx) { switch (field->type()) { #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, METHOD_NAME) \ case FieldDescriptor::TYPE_##TYPE: \ - return {internal::Packed##METHOD_NAME##Parser, \ - reflection->MutableRepeatedField(msg, field)} + return internal::Packed##METHOD_NAME##Parser( \ + reflection->MutableRepeatedField(msg, field), ptr, ctx) HANDLE_PACKED_TYPE(INT32, int32, Int32); HANDLE_PACKED_TYPE(INT64, int64, Int64); HANDLE_PACKED_TYPE(SINT32, int32, SInt32); @@ -276,17 +261,16 @@ ParseClosure GetPackedField(const FieldDescriptor* field, Message* msg, HANDLE_PACKED_TYPE(UINT64, uint64, UInt64); HANDLE_PACKED_TYPE(BOOL, bool, Bool); case FieldDescriptor::TYPE_ENUM: { + auto object = + internal::ReflectionAccessor::GetRepeatedEnum(reflection, field, msg); if (field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) { - auto object = - internal::ReflectionAccessor::GetOffset(msg, field, reflection); - return {internal::PackedEnumParser, object}; + return internal::PackedEnumParser(object, ptr, ctx); } else { - ctx->extra_parse_data().SetEnumValidatorArg( - ReflectiveValidator, field->enum_type(), - reflection->MutableUnknownFields(msg), field->number()); - auto object = - internal::ReflectionAccessor::GetOffset(msg, field, reflection); - return {internal::PackedValidEnumParserArg, object}; + return internal::PackedEnumParserArg( + object, ptr, ctx, ReflectiveValidator, field->enum_type(), + internal::ReflectionAccessor::MutableInternalMetadataWithArena( + reflection, msg), + field->number()); } } HANDLE_PACKED_TYPE(FIXED32, uint32, Fixed32); @@ -299,42 +283,44 @@ ParseClosure GetPackedField(const FieldDescriptor* field, Message* msg, default: GOOGLE_LOG(FATAL) << "Type is not packable " << field->type(); + return nullptr; // Make compiler happy } } -ParseClosure GetLenDelim(int field_number, const FieldDescriptor* field, - Message* msg, UnknownFieldSet* unknown, - const Reflection* reflection, - internal::ParseContext* ctx) { - if (field == nullptr || WireFormat::WireTypeForFieldType(field->type()) != - WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - if (field && field->is_packable()) { - return GetPackedField(field, msg, reflection, ctx); - } - return {internal::StringParser, unknown->AddLengthDelimited(field_number)}; +const char* ParseLenDelim(int field_number, const FieldDescriptor* field, + Message* msg, const Reflection* reflection, + const char* ptr, internal::ParseContext* ctx) { + if (WireFormat::WireTypeForFieldType(field->type()) != + WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + GOOGLE_DCHECK(field->is_packable()); + return ParsePackedField(field, msg, reflection, ptr, ctx); } enum { kNone = 0, kVerify, kStrict } utf8_level = kNone; - internal::ParseFunc string_parsers[] = {internal::StringParser, - internal::StringParserUTF8Verify, - internal::StringParserUTF8}; - internal::ParseFunc cord_parsers[] = {internal::CordParser, - internal::CordParserUTF8Verify, - internal::CordParserUTF8}; - internal::ParseFunc string_piece_parsers[] = { - internal::StringPieceParser, internal::StringPieceParserUTF8Verify, - internal::StringPieceParserUTF8}; + const char* field_name = nullptr; + auto parse_string = [ptr, ctx, &utf8_level, &field_name](std::string* s) { + switch (utf8_level) { + case kNone: + return internal::InlineGreedyStringParser(s, ptr, ctx); + case kVerify: + return internal::InlineGreedyStringParserUTF8Verify(s, ptr, ctx, + field_name); + case kStrict: + return internal::InlineGreedyStringParserUTF8(s, ptr, ctx, field_name); + } + }; switch (field->type()) { - case FieldDescriptor::TYPE_STRING: - if (field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 - ) { - ctx->extra_parse_data().SetFieldName(field->full_name().c_str()); + case FieldDescriptor::TYPE_STRING: { + bool enforce_utf8 = true; + bool utf8_verification = true; + if (enforce_utf8 && + field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) { utf8_level = kStrict; - } else if ( - true) { - ctx->extra_parse_data().SetFieldName(field->full_name().c_str()); + } else if (utf8_verification) { utf8_level = kVerify; } - GOOGLE_FALLTHROUGH_INTENDED; + field_name = field->full_name().c_str(); + PROTOBUF_FALLTHROUGH_INTENDED; + } case FieldDescriptor::TYPE_BYTES: { if (field->is_repeated()) { int index = reflection->FieldSize(*msg, field); @@ -342,20 +328,15 @@ ParseClosure GetLenDelim(int field_number, const FieldDescriptor* field, reflection->AddString(msg, field, ""); if (field->options().ctype() == FieldOptions::STRING || field->is_extension()) { - auto object = reflection->MutableRepeatedPtrField(msg, field) - ->Mutable(index); - return {string_parsers[utf8_level], object}; - } else if (field->options().ctype() == FieldOptions::CORD) { - auto object = reflection->MutableRepeatedField(msg, field) - ->Mutable(index); - return {cord_parsers[utf8_level], object}; - } else if (field->options().ctype() == FieldOptions::STRING_PIECE) { auto object = - reflection - ->MutableRepeatedPtrField(msg, - field) + reflection->MutableRepeatedPtrField(msg, field) + ->Mutable(index); + return parse_string(object); + } else { + auto object = + reflection->MutableRepeatedPtrField(msg, field) ->Mutable(index); - return {string_piece_parsers[utf8_level], object}; + return parse_string(object); } } else { // Clear value and make sure it's set. @@ -363,20 +344,14 @@ ParseClosure GetLenDelim(int field_number, const FieldDescriptor* field, if (field->options().ctype() == FieldOptions::STRING || field->is_extension()) { // HACK around inability to get mutable_string in reflection - string* object = &const_cast( + std::string* object = &const_cast( reflection->GetStringReference(*msg, field, nullptr)); - return {string_parsers[utf8_level], object}; + return parse_string(object); } else { - void* object = - internal::ReflectionAccessor::GetOffset(msg, field, reflection); - if (field->containing_oneof()) { - object = *static_cast(object); - } - if (field->options().ctype() == FieldOptions::CORD) { - return {cord_parsers[utf8_level], object}; - } else if (field->options().ctype() == FieldOptions::STRING_PIECE) { - return {string_piece_parsers[utf8_level], object}; - } + // HACK around inability to get mutable_string in reflection + std::string* object = &const_cast( + reflection->GetStringReference(*msg, field, nullptr)); + return parse_string(object); } } GOOGLE_LOG(FATAL) << "No other type than string supported"; @@ -384,224 +359,178 @@ ParseClosure GetLenDelim(int field_number, const FieldDescriptor* field, case FieldDescriptor::TYPE_MESSAGE: { Message* object; if (field->is_repeated()) { - object = reflection->AddMessage(msg, field, nullptr); + object = reflection->AddMessage(msg, field, ctx->data().factory); } else { - object = reflection->MutableMessage(msg, field, nullptr); + object = reflection->MutableMessage(msg, field, ctx->data().factory); } - return {object->_ParseFunc(), object}; + return ctx->ParseMessage(object, ptr); } default: GOOGLE_LOG(FATAL) << "Wrong type for length delim " << field->type(); } + return nullptr; // Make compiler happy. } -const char* ReflectiveParseMessageSetItem(const char* begin, const char* end, - void* object, - internal::ParseContext* ctx) { - ParseClosure child; - auto msg = static_cast(object); - auto reflection = msg->GetReflection(); - uint32 size; - auto ptr = begin; - while (ptr < end) { - uint32 tag = *ptr++; - if (tag == WireFormatLite::kMessageSetTypeIdTag) { - uint32 type_id; - ptr = Varint::Parse32(ptr, &type_id); - if (!ptr) goto error; - - auto field = reflection->FindKnownExtensionByNumber(type_id); - - if (ctx->extra_parse_data().payload.empty()) { - tag = *ptr++; - if (tag == WireFormatLite::kMessageSetMessageTag) { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - child = GetLenDelim(type_id * 8 + 2, field, msg, - reflection->MutableUnknownFields(msg), reflection, - ctx); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange(child, ptr, newend)) goto error; - ptr = newend; - } else { - goto error; - } - } else { - GOOGLE_LOG(FATAL) << "Wrong order"; - } - } else if (tag == WireFormatLite::kMessageSetItemEndTag) { - if (!ctx->ValidEndGroup(tag)) goto error; - break; - } else if (tag == WireFormatLite::kMessageSetMessageTag) { - uint32 size; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - child = {internal::StringParser, &ctx->extra_parse_data().payload}; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange(child, ptr, newend)) goto error; - ptr = newend; - } else { - GOOGLE_LOG(FATAL) << "Unknown message set item tag"; - } - } - return ptr; -error: - return nullptr; -len_delim_till_end: - return ctx->StoreAndTailCall(ptr, end, {ReflectiveParseMessageSetItem, msg}, - child, size); -} - -ParseClosure GetGroup(int field_number, const FieldDescriptor* field, - Message* msg, UnknownFieldSet* unknown, - const Reflection* reflection) { - if (field == nullptr && field_number == 1 && - msg->GetDescriptor()->options().message_set_wire_format()) { - return {ReflectiveParseMessageSetItem, msg}; - } - if (field == nullptr || WireFormat::WireTypeForFieldType(field->type()) != - WireFormatLite::WIRETYPE_START_GROUP) { - return {internal::UnknownGroupParse, unknown->AddGroup(field_number)}; - } - - Message* object; +Message* GetGroup(int field_number, const FieldDescriptor* field, Message* msg, + const Reflection* reflection) { if (field->is_repeated()) { - object = reflection->AddMessage(msg, field, nullptr); + return reflection->AddMessage(msg, field, nullptr); } else { - object = reflection->MutableMessage(msg, field, nullptr); + return reflection->MutableMessage(msg, field, nullptr); } - return {object->_ParseFunc(), object}; -} - -const char* Message::_InternalParse(const char* begin, const char* end, - void* object, internal::ParseContext* ctx) { - auto msg = static_cast(object); - const Descriptor* descriptor = msg->GetDescriptor(); - const Reflection* reflection = msg->GetReflection(); - auto unknown = reflection->MutableUnknownFields(msg); - GOOGLE_DCHECK(descriptor); - GOOGLE_DCHECK(reflection); - - GOOGLE_DCHECK(begin <= end); - uint32 size; - internal::ParseFunc parser_till_end; - uint32 tag; - int depth; - auto ptr = begin; - while (ptr < end) { - ptr = Varint::Parse32Inline(ptr, &tag); - if (ptr == nullptr) return nullptr; - if ((tag >> 3) == 0) return nullptr; - const FieldDescriptor* field = nullptr; - - int field_number = WireFormatLite::GetTagFieldNumber(tag); - field = descriptor->FindFieldByNumber(field_number); - - // If that failed, check if the field is an extension. - if (field == nullptr && descriptor->IsExtensionNumber(field_number)) { - field = reflection->FindKnownExtensionByNumber(field_number); - } +} - // if (field) GOOGLE_LOG(ERROR) << "Encountered field " << field->name(); - - switch (tag & 7) { - case 0: { - uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - if (field == nullptr || - WireFormat::WireTypeForFieldType(field->type()) != 0) { - unknown->AddVarint(field_number, val); - break; +const char* Message::_InternalParse(const char* ptr, + internal::ParseContext* ctx) { + class ReflectiveFieldParser { + public: + ReflectiveFieldParser(Message* msg, internal::ParseContext* ctx) + : ReflectiveFieldParser(msg, ctx, false) {} + + void AddVarint(uint32 num, uint64 value) { + if (is_item_ && num == 2) { + if (!payload_.empty()) { + auto field = Field(value, 2); + if (field && field->message_type()) { + auto child = reflection_->MutableMessage(msg_, field); + // TODO(gerbens) signal error + child->ParsePartialFromString(payload_); + } else { + MutableUnknown()->AddLengthDelimited(value)->swap(payload_); + } + return; } - SetField(val, field, msg, reflection); - break; + type_id_ = value; + return; } - case 1: { - uint64 val = UNALIGNED_LOAD64(ptr); - ptr = ptr + 8; - if (field == nullptr || - WireFormat::WireTypeForFieldType(field->type()) != 1) { - unknown->AddFixed64(field_number, val); - break; - } - SetField(val, field, msg, reflection); - break; + auto field = Field(num, 0); + if (field) { + SetField(value, field, msg_, reflection_); + } else { + MutableUnknown()->AddVarint(num, value); + } + } + void AddFixed64(uint32 num, uint64 value) { + auto field = Field(num, 1); + if (field) { + SetField(value, field, msg_, reflection_); + } else { + MutableUnknown()->AddFixed64(num, value); } - case 2: { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ParseClosure child = - GetLenDelim(field_number, field, msg, unknown, reflection, ctx); - parser_till_end = child.func; - object = child.object; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange(child, ptr, newend)) goto error; - ptr = newend; - break; + } + const char* ParseLengthDelimited(uint32 num, const char* ptr, + internal::ParseContext* ctx) { + if (is_item_ && num == 3) { + if (type_id_ == 0) { + return InlineGreedyStringParser(&payload_, ptr, ctx); + } + num = type_id_; + type_id_ = 0; } - case 3: { - if (!ctx->PrepareGroup(tag, &depth)) goto error; - - ParseClosure child = - GetGroup(field_number, field, msg, unknown, reflection); - parser_till_end = child.func; - object = child.object; - - if (ptr < end) ptr = child(ptr, end, ctx); - if (!ptr) goto error; - if (ctx->GroupContinues(depth)) goto group_continues; - break; + auto field = Field(num, 2); + if (field) { + return ParseLenDelim(num, field, msg_, reflection_, ptr, ctx); + } else { + return InlineGreedyStringParser( + MutableUnknown()->AddLengthDelimited(num), ptr, ctx); } - case 4: { - if (!ctx->ValidEndGroup(tag)) goto error; + } + const char* ParseGroup(uint32 num, const char* ptr, + internal::ParseContext* ctx) { + if (!is_item_ && descriptor_->options().message_set_wire_format() && + num == 1) { + is_item_ = true; + ptr = ctx->ParseGroup(this, ptr, num * 8 + 3); + is_item_ = false; + type_id_ = 0; return ptr; } - case 5: { - uint32 val = UNALIGNED_LOAD32(ptr); - ptr = ptr + 4; - if (field == nullptr || - WireFormat::WireTypeForFieldType(field->type()) != 5) { - unknown->AddFixed64(field_number, val); - break; + auto field = Field(num, 3); + if (field) { + auto msg = GetGroup(num, field, msg_, reflection_); + return ctx->ParseGroup(msg, ptr, num * 8 + 3); + } else { + return UnknownFieldParse(num * 8 + 3, MutableUnknown(), ptr, ctx); + } + } + void AddFixed32(uint32 num, uint32 value) { + auto field = Field(num, 5); + if (field) { + SetField(value, field, msg_, reflection_); + } else { + MutableUnknown()->AddFixed32(num, value); + } + } + + const char* _InternalParse(const char* ptr, internal::ParseContext* ctx) { + // We're parsing the a MessageSetItem + GOOGLE_DCHECK(is_item_); + return internal::WireFormatParser(*this, ptr, ctx); + } + + private: + Message* msg_; + const Descriptor* descriptor_; + const Reflection* reflection_; + internal::ParseContext* ctx_; + UnknownFieldSet* unknown_ = nullptr; + bool is_item_ = false; + uint32 type_id_ = 0; + std::string payload_; + + ReflectiveFieldParser(Message* msg, internal::ParseContext* ctx, + bool is_item) + : msg_(msg), + descriptor_(msg->GetDescriptor()), + reflection_(msg->GetReflection()), + ctx_(ctx), + is_item_(is_item) { + GOOGLE_CHECK(descriptor_) << msg->GetTypeName(); + GOOGLE_CHECK(reflection_) << msg->GetTypeName(); + } + + const FieldDescriptor* Field(int num, int wire_type) { + auto field = descriptor_->FindFieldByNumber(num); + + // If that failed, check if the field is an extension. + if (field == nullptr && descriptor_->IsExtensionNumber(num)) { + const DescriptorPool* pool = ctx_->data().pool; + if (pool == NULL) { + field = reflection_->FindKnownExtensionByNumber(num); + } else { + field = pool->FindExtensionByNumber(descriptor_, num); + } + } + if (field == nullptr) return nullptr; + + if (internal::WireFormat::WireTypeForFieldType(field->type()) != + wire_type) { + if (field->is_packable()) { + if (wire_type == + internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + return field; + } } - SetField(val, field, msg, reflection); - break; + return nullptr; } - default: - goto error; + return field; } - } - return ptr; -error: - return nullptr; -len_delim_till_end: - // Length delimited field crosses end - return ctx->StoreAndTailCall(ptr, end, {Message::_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: - GOOGLE_DCHECK(ptr >= end); - // Group crossed end and must be continued. Either this a parse failure - // or we need to resume on the next chunk and thus save the state. - ctx->StoreGroup({Message::_InternalParse, msg}, {parser_till_end, object}, - depth); - return ptr; + + UnknownFieldSet* MutableUnknown() { + if (unknown_) return unknown_; + return unknown_ = reflection_->MutableUnknownFields(msg_); + } + }; + + ReflectiveFieldParser field_parser(this, ctx); + return internal::WireFormatParser(field_parser, ptr, ctx); } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - -void Message::SerializeWithCachedSizes( - io::CodedOutputStream* output) const { - const internal::SerializationTable* table = - static_cast(InternalGetTable()); - if (table == 0) { - WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output); - } else { - internal::TableSerialize(*this, table, output); - } +uint8* Message::InternalSerializeWithCachedSizesToArray( + uint8* target, io::EpsCopyOutputStream* stream) const { + return WireFormat::InternalSerializeWithCachedSizesToArray(*this, target, + stream); } size_t Message::ByteSizeLong() const { @@ -620,120 +549,18 @@ size_t Message::SpaceUsedLong() const { return GetReflection()->SpaceUsedLong(*this); } -bool Message::SerializeToFileDescriptor(int file_descriptor) const { - io::FileOutputStream output(file_descriptor); - return SerializeToZeroCopyStream(&output) && output.Flush(); -} - -bool Message::SerializePartialToFileDescriptor(int file_descriptor) const { - io::FileOutputStream output(file_descriptor); - return SerializePartialToZeroCopyStream(&output) && output.Flush(); -} - -bool Message::SerializeToOstream(std::ostream* output) const { - { - io::OstreamOutputStream zero_copy_output(output); - if (!SerializeToZeroCopyStream(&zero_copy_output)) return false; - } - return output->good(); -} - -bool Message::SerializePartialToOstream(std::ostream* output) const { - io::OstreamOutputStream zero_copy_output(output); - return SerializePartialToZeroCopyStream(&zero_copy_output); -} - - -// ============================================================================= -// Reflection and associated Template Specializations - -Reflection::~Reflection() {} - -void Reflection::AddAllocatedMessage(Message* /* message */, - const FieldDescriptor* /*field */, - Message* /* new_entry */) const {} - -#define HANDLE_TYPE(TYPE, CPPTYPE, CTYPE) \ -template<> \ -const RepeatedField& Reflection::GetRepeatedField( \ - const Message& message, const FieldDescriptor* field) const { \ - return *static_cast* >( \ - MutableRawRepeatedField(const_cast(&message), \ - field, CPPTYPE, CTYPE, NULL)); \ -} \ - \ -template<> \ -RepeatedField* Reflection::MutableRepeatedField( \ - Message* message, const FieldDescriptor* field) const { \ - return static_cast* >( \ - MutableRawRepeatedField(message, field, CPPTYPE, CTYPE, NULL)); \ -} - -HANDLE_TYPE(int32, FieldDescriptor::CPPTYPE_INT32, -1); -HANDLE_TYPE(int64, FieldDescriptor::CPPTYPE_INT64, -1); -HANDLE_TYPE(uint32, FieldDescriptor::CPPTYPE_UINT32, -1); -HANDLE_TYPE(uint64, FieldDescriptor::CPPTYPE_UINT64, -1); -HANDLE_TYPE(float, FieldDescriptor::CPPTYPE_FLOAT, -1); -HANDLE_TYPE(double, FieldDescriptor::CPPTYPE_DOUBLE, -1); -HANDLE_TYPE(bool, FieldDescriptor::CPPTYPE_BOOL, -1); - - -#undef HANDLE_TYPE - -void* Reflection::MutableRawRepeatedString( - Message* message, const FieldDescriptor* field, bool is_string) const { - return MutableRawRepeatedField(message, field, - FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL); -} - - -MapIterator Reflection::MapBegin( - Message* message, - const FieldDescriptor* field) const { - GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API."; - MapIterator iter(message, field); - return iter; -} - -MapIterator Reflection::MapEnd( - Message* message, - const FieldDescriptor* field) const { - GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API."; - MapIterator iter(message, field); - return iter; -} - // ============================================================================= // MessageFactory MessageFactory::~MessageFactory() {} -namespace internal { - -// TODO(gerbens) make this factorized better. This should not have to hop -// to reflection. Currently uses GeneratedMessageReflection and thus is -// defined in generated_message_reflection.cc -void RegisterFileLevelMetadata(void* assign_descriptors_table); - -} // namespace internal - namespace { -void RegisterFileLevelMetadata(void* assign_descriptors_table, - const string& filename) { - internal::RegisterFileLevelMetadata(assign_descriptors_table); -} - class GeneratedMessageFactory : public MessageFactory { public: static GeneratedMessageFactory* singleton(); - struct RegistrationData { - const Metadata* file_level_metadata; - int size; - }; - - void RegisterFile(const char* file, void* registration_data); + void RegisterFile(const google::protobuf::internal::DescriptorTable* table); void RegisterType(const Descriptor* descriptor, const Message* prototype); // implements MessageFactory --------------------------------------- @@ -741,8 +568,8 @@ class GeneratedMessageFactory : public MessageFactory { private: // Only written at static init time, so does not require locking. - std::unordered_map, - streq> + std::unordered_map, streq> file_map_; internal::WrappedMutex mutex_; @@ -756,18 +583,18 @@ GeneratedMessageFactory* GeneratedMessageFactory::singleton() { return instance; } -void GeneratedMessageFactory::RegisterFile(const char* file, - void* registration_data) { - if (!InsertIfNotPresent(&file_map_, file, registration_data)) { - GOOGLE_LOG(FATAL) << "File is already registered: " << file; +void GeneratedMessageFactory::RegisterFile( + const google::protobuf::internal::DescriptorTable* table) { + if (!InsertIfNotPresent(&file_map_, table->filename, table)) { + GOOGLE_LOG(FATAL) << "File is already registered: " << table->filename; } } void GeneratedMessageFactory::RegisterType(const Descriptor* descriptor, const Message* prototype) { GOOGLE_DCHECK_EQ(descriptor->file()->pool(), DescriptorPool::generated_pool()) - << "Tried to register a non-generated type with the generated " - "type registry."; + << "Tried to register a non-generated type with the generated " + "type registry."; // This should only be called as a result of calling a file registration // function during GetPrototype(), in which case we already have locked @@ -791,11 +618,12 @@ const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) { if (type->file()->pool() != DescriptorPool::generated_pool()) return NULL; // Apparently the file hasn't been registered yet. Let's do that now. - void* registration_data = + const internal::DescriptorTable* registration_data = FindPtrOrNull(file_map_, type->file()->name().c_str()); if (registration_data == NULL) { GOOGLE_LOG(DFATAL) << "File appears to be in generated pool but wasn't " - "registered: " << type->file()->name(); + "registered: " + << type->file()->name(); return NULL; } @@ -805,7 +633,7 @@ const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) { const Message* result = FindPtrOrNull(type_map_, type); if (result == NULL) { // Nope. OK, register everything. - RegisterFileLevelMetadata(registration_data, type->file()->name()); + internal::RegisterFileLevelMetadata(registration_data); // Should be here now. result = FindPtrOrNull(type_map_, type); } @@ -825,9 +653,8 @@ MessageFactory* MessageFactory::generated_factory() { } void MessageFactory::InternalRegisterGeneratedFile( - const char* filename, void* assign_descriptors_table) { - GeneratedMessageFactory::singleton()->RegisterFile(filename, - assign_descriptors_table); + const google::protobuf::internal::DescriptorTable* table) { + GeneratedMessageFactory::singleton()->RegisterFile(table); } void MessageFactory::InternalRegisterGeneratedMessage( @@ -836,19 +663,6 @@ void MessageFactory::InternalRegisterGeneratedMessage( } -MessageFactory* Reflection::GetMessageFactory() const { - GOOGLE_LOG(FATAL) << "Not implemented."; - return NULL; -} - -void* Reflection::RepeatedFieldData( - Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType cpp_type, - const Descriptor* message_type) const { - GOOGLE_LOG(FATAL) << "Not implemented."; - return NULL; -} - namespace { template T* GetSingleton() { @@ -896,7 +710,7 @@ template <> #if defined(_MSC_VER) && (_MSC_VER >= 1800) // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue // #240 -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE #endif Message* GenericTypeHandler::NewFromPrototype(const Message* prototype, @@ -907,7 +721,7 @@ template <> #if defined(_MSC_VER) && (_MSC_VER >= 1800) // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue // #240 -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE #endif Arena* GenericTypeHandler::GetArena(Message* value) { @@ -917,7 +731,7 @@ template <> #if defined(_MSC_VER) && (_MSC_VER >= 1800) // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue // #240 -GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE +PROTOBUF_NOINLINE #endif void* GenericTypeHandler::GetMaybeArenaPointer(Message* value) { diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h index 007dafa288..19d77b1649 100644 --- a/src/google/protobuf/message.h +++ b/src/google/protobuf/message.h @@ -49,7 +49,7 @@ // Then, if you used the protocol compiler to generate a class from the above // definition, you could use it like so: // -// string data; // Will store a serialized version of the message. +// std::string data; // Will store a serialized version of the message. // // { // // Create a message and serialize it. @@ -84,12 +84,12 @@ // // Get the descriptors for the fields we're interested in and verify // // their types. // const FieldDescriptor* text_field = descriptor->FindFieldByName("text"); -// assert(text_field != NULL); +// assert(text_field != nullptr); // assert(text_field->type() == FieldDescriptor::TYPE_STRING); // assert(text_field->label() == FieldDescriptor::LABEL_OPTIONAL); // const FieldDescriptor* numbers_field = descriptor-> // FindFieldByName("numbers"); -// assert(numbers_field != NULL); +// assert(numbers_field != nullptr); // assert(numbers_field->type() == FieldDescriptor::TYPE_INT32); // assert(numbers_field->label() == FieldDescriptor::LABEL_REPEATED); // @@ -119,6 +119,7 @@ #include #include #include +#include #include #include @@ -141,40 +142,48 @@ class Reflection; class MessageFactory; // Defined in other files. +class AssignDescriptorsHelper; +class DynamicMessageFactory; class MapKey; class MapValueRef; class MapIterator; class MapReflectionTester; namespace internal { +struct DescriptorTable; class MapFieldBase; } -class UnknownFieldSet; // unknown_field_set.h +class UnknownFieldSet; // unknown_field_set.h namespace io { -class ZeroCopyInputStream; // zero_copy_stream.h -class ZeroCopyOutputStream; // zero_copy_stream.h -class CodedInputStream; // coded_stream.h -class CodedOutputStream; // coded_stream.h -} +class ZeroCopyInputStream; // zero_copy_stream.h +class ZeroCopyOutputStream; // zero_copy_stream.h +class CodedInputStream; // coded_stream.h +class CodedOutputStream; // coded_stream.h +} // namespace io namespace python { -class MapReflectionFriend; // scalar_map_container.h +class MapReflectionFriend; // scalar_map_container.h } namespace expr { class CelMapReflectionFriend; // field_backed_map_impl.cc } +namespace internal { +class MapFieldPrinterHelper; // text_format.cc +} + namespace internal { -class ReflectionOps; // reflection_ops.h -class MapKeySorter; // wire_format.cc -class WireFormat; // wire_format.h +class ReflectionAccessor; // message.cc +class ReflectionOps; // reflection_ops.h +class MapKeySorter; // wire_format.cc +class WireFormat; // wire_format.h class MapFieldReflectionTest; // map_test.cc -} +} // namespace internal -template -class RepeatedField; // repeated_field.h +template +class RepeatedField; // repeated_field.h -template +template class RepeatedPtrField; // repeated_field.h // A container to hold message metadata. @@ -193,10 +202,10 @@ struct Metadata { // optimized for speed will want to override these with faster implementations, // but classes optimized for code size may be happy with keeping them. See // the optimize_for option in descriptor.proto. -class LIBPROTOBUF_EXPORT Message : public MessageLite { +class PROTOBUF_EXPORT Message : public MessageLite { public: inline Message() {} - virtual ~Message() {} + ~Message() override {} // Basic Operations ------------------------------------------------ @@ -206,11 +215,11 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite { Message* New() const override = 0; // Construct a new instance on the arena. Ownership is passed to the caller - // if arena is a NULL. Default implementation allows for API compatibility + // if arena is a nullptr. Default implementation allows for API compatibility // during the Arena transition. Message* New(Arena* arena) const override { Message* message = New(); - if (arena != NULL) { + if (arena != nullptr) { arena->Own(message); } return message; @@ -236,11 +245,11 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite { // This is much, much slower than IsInitialized() as it is implemented // purely via reflection. Generally, you should not call this unless you // have already determined that an error exists by calling IsInitialized(). - void FindInitializationErrors(std::vector* errors) const; + void FindInitializationErrors(std::vector* errors) const; // Like FindInitializationErrors, but joins all the strings, delimited by // commas, and returns them. - string InitializationErrorString() const override; + std::string InitializationErrorString() const override; // Clears all unknown fields from this message and all embedded messages. // Normally, if unknown tag numbers are encountered when parsing a message, @@ -264,71 +273,39 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite { // fields defined for the proto. virtual size_t SpaceUsedLong() const; - GOOGLE_PROTOBUF_DEPRECATED_MSG("Please use SpaceUsedLong() instead") + PROTOBUF_DEPRECATED_MSG("Please use SpaceUsedLong() instead") int SpaceUsed() const { return internal::ToIntSize(SpaceUsedLong()); } // Debugging & Testing---------------------------------------------- // Generates a human readable form of this message, useful for debugging // and other purposes. - string DebugString() const; + std::string DebugString() const; // Like DebugString(), but with less whitespace. - string ShortDebugString() const; + std::string ShortDebugString() const; // Like DebugString(), but do not escape UTF-8 byte sequences. - string Utf8DebugString() const; + std::string Utf8DebugString() const; // Convenience function useful in GDB. Prints DebugString() to stdout. void PrintDebugString() const; - // Heavy I/O ------------------------------------------------------- - // Additional parsing and serialization methods not implemented by - // MessageLite because they are not supported by the lite library. - - // Parse a protocol buffer from a file descriptor. If successful, the entire - // input will be consumed. - bool ParseFromFileDescriptor(int file_descriptor); - // Like ParseFromFileDescriptor(), but accepts messages that are missing - // required fields. - bool ParsePartialFromFileDescriptor(int file_descriptor); - // Parse a protocol buffer from a C++ istream. If successful, the entire - // input will be consumed. - bool ParseFromIstream(std::istream* input); - // Like ParseFromIstream(), but accepts messages that are missing - // required fields. - bool ParsePartialFromIstream(std::istream* input); - - // 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; - - // Reflection-based methods ---------------------------------------- // These methods are pure-virtual in MessageLite, but Message provides // reflection-based default implementations. - string GetTypeName() const override; + std::string GetTypeName() const override; void Clear() override; bool IsInitialized() const override; void CheckTypeAndMergeFrom(const MessageLite& other) override; -#if !GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - bool MergePartialFromCodedStream(io::CodedInputStream* input) override; -#endif - size_t ByteSizeLong() const override; - void SerializeWithCachedSizes(io::CodedOutputStream* output) const override; - #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - internal::ParseFunc _ParseFunc() const override { return _InternalParse; } - // Reflective parser - static const char* _InternalParse(const char* begin, const char* end, - void* object, internal::ParseContext* ctx); + const char* _InternalParse(const char* ptr, + internal::ParseContext* ctx) override; +#else + bool MergePartialFromCodedStream(io::CodedInputStream* input) override; #endif + size_t ByteSizeLong() const override; + uint8* InternalSerializeWithCachedSizesToArray( + uint8* target, io::EpsCopyOutputStream* stream) const override; private: // This is called only by the default implementation of ByteSize(), to @@ -341,7 +318,6 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite { virtual void SetCachedSize(int size) const; public: - // Introspection --------------------------------------------------- @@ -357,15 +333,13 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite { // // This method remains virtual in case a subclass does not implement // reflection and wants to override the default behavior. - virtual const Reflection* GetReflection() const final { - return GetMetadata().reflection; - } + const Reflection* GetReflection() const { return GetMetadata().reflection; } protected: // Get a struct containing the metadata for the Message. Most subclasses only // need to implement this method, rather than the GetDescriptor() and // GetReflection() wrappers. - virtual Metadata GetMetadata() const = 0; + virtual Metadata GetMetadata() const = 0; private: @@ -380,10 +354,10 @@ class RepeatedFieldAccessor; // Forward-declare RepeatedFieldRef templates. The second type parameter is // used for SFINAE tricks. Users should ignore it. -template +template class RepeatedFieldRef; -template +template class MutableRepeatedFieldRef; // This interface contains methods that can be used to dynamically access @@ -424,64 +398,46 @@ class MutableRepeatedFieldRef; // double the message's memory footprint, probably worse. Allocating the // objects on-demand, on the other hand, would be expensive and prone to // memory leaks. So, instead we ended up with this flat interface. -class LIBPROTOBUF_EXPORT Reflection { +class PROTOBUF_EXPORT Reflection final { public: - inline Reflection() {} - virtual ~Reflection(); - // Get the UnknownFieldSet for the message. This contains fields which // were seen when the Message was parsed but were not recognized according - // to the Message's definition. For proto3 protos, this method will always - // return an empty UnknownFieldSet. - virtual const UnknownFieldSet& GetUnknownFields( - const Message& message) const = 0; + // to the Message's definition. + const UnknownFieldSet& GetUnknownFields(const Message& message) const; // Get a mutable pointer to the UnknownFieldSet for the message. This // contains fields which were seen when the Message was parsed but were not - // recognized according to the Message's definition. For proto3 protos, this - // method will return a valid mutable UnknownFieldSet pointer but modifying - // it won't affect the serialized bytes of the message. - virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0; + // recognized according to the Message's definition. + UnknownFieldSet* MutableUnknownFields(Message* message) const; // Estimate the amount of memory used by the message object. - virtual size_t SpaceUsedLong(const Message& message) const = 0; + size_t SpaceUsedLong(const Message& message) const; - GOOGLE_PROTOBUF_DEPRECATED_MSG("Please use SpaceUsedLong() instead") + PROTOBUF_DEPRECATED_MSG("Please use SpaceUsedLong() instead") int SpaceUsed(const Message& message) const { return internal::ToIntSize(SpaceUsedLong(message)); } // Check if the given non-repeated field is set. - virtual bool HasField(const Message& message, - const FieldDescriptor* field) const = 0; + bool HasField(const Message& message, const FieldDescriptor* field) const; // Get the number of elements of a repeated field. - virtual int FieldSize(const Message& message, - const FieldDescriptor* field) const = 0; + int FieldSize(const Message& message, const FieldDescriptor* field) const; // Clear the value of a field, so that HasField() returns false or // FieldSize() returns zero. - virtual void ClearField(Message* message, - const FieldDescriptor* field) const = 0; + void ClearField(Message* message, const FieldDescriptor* field) const; // Check if the oneof is set. Returns true if any field in oneof // is set, false otherwise. - // TODO(jieluo) - make it pure virtual after updating all - // the subclasses. - virtual bool HasOneof(const Message& /*message*/, - const OneofDescriptor* /*oneof_descriptor*/) const { - return false; - } + bool HasOneof(const Message& message, + const OneofDescriptor* oneof_descriptor) const; - virtual void ClearOneof(Message* /*message*/, - const OneofDescriptor* /*oneof_descriptor*/) const {} + void ClearOneof(Message* message, + const OneofDescriptor* oneof_descriptor) const; - // Returns the field descriptor if the oneof is set. NULL otherwise. - // TODO(jieluo) - make it pure virtual. - virtual const FieldDescriptor* GetOneofFieldDescriptor( - const Message& /*message*/, - const OneofDescriptor* /*oneof_descriptor*/) const { - return NULL; - } + // Returns the field descriptor if the oneof is set. nullptr otherwise. + const FieldDescriptor* GetOneofFieldDescriptor( + const Message& message, const OneofDescriptor* oneof_descriptor) const; // Removes the last element of a repeated field. // We don't provide a way to remove any element other than the last @@ -490,27 +446,21 @@ class LIBPROTOBUF_EXPORT Reflection { // than the last, the best way to do it is to re-arrange the elements // (using Swap()) so that the one you want removed is at the end, then // call RemoveLast(). - virtual void RemoveLast(Message* message, - const FieldDescriptor* field) const = 0; + void RemoveLast(Message* message, const FieldDescriptor* field) const; // Removes the last element of a repeated message field, and returns the // pointer to the caller. Caller takes ownership of the returned pointer. - virtual Message* ReleaseLast(Message* message, - const FieldDescriptor* field) const = 0; + Message* ReleaseLast(Message* message, const FieldDescriptor* field) const; // Swap the complete contents of two messages. - virtual void Swap(Message* message1, Message* message2) const = 0; + void Swap(Message* message1, Message* message2) const; // Swap fields listed in fields vector of two messages. - virtual void SwapFields(Message* message1, - Message* message2, - const std::vector& fields) - const = 0; + void SwapFields(Message* message1, Message* message2, + const std::vector& fields) const; // Swap two elements of a repeated field. - virtual void SwapElements(Message* message, - const FieldDescriptor* field, - int index1, - int index2) const = 0; + void SwapElements(Message* message, const FieldDescriptor* field, int index1, + int index2) const; // List all fields of the message which are currently set, except for unknown // fields, but including extension known to the parser (i.e. compiled in). @@ -520,89 +470,78 @@ class LIBPROTOBUF_EXPORT Reflection { // ordered by field number. // Use Reflection::GetUnknownFields() or message.unknown_fields() to also get // access to fields/extensions unknown to the parser. - virtual void ListFields( - const Message& message, - std::vector* output) const = 0; + void ListFields(const Message& message, + std::vector* output) const; // Singular field getters ------------------------------------------ // These get the value of a non-repeated field. They return the default // value for fields that aren't set. - virtual int32 GetInt32 (const Message& message, - const FieldDescriptor* field) const = 0; - virtual int64 GetInt64 (const Message& message, - const FieldDescriptor* field) const = 0; - virtual uint32 GetUInt32(const Message& message, - const FieldDescriptor* field) const = 0; - virtual uint64 GetUInt64(const Message& message, - const FieldDescriptor* field) const = 0; - virtual float GetFloat (const Message& message, - const FieldDescriptor* field) const = 0; - virtual double GetDouble(const Message& message, - const FieldDescriptor* field) const = 0; - virtual bool GetBool (const Message& message, - const FieldDescriptor* field) const = 0; - virtual string GetString(const Message& message, - const FieldDescriptor* field) const = 0; - virtual const EnumValueDescriptor* GetEnum( - const Message& message, const FieldDescriptor* field) const = 0; + int32 GetInt32(const Message& message, const FieldDescriptor* field) const; + int64 GetInt64(const Message& message, const FieldDescriptor* field) const; + uint32 GetUInt32(const Message& message, const FieldDescriptor* field) const; + uint64 GetUInt64(const Message& message, const FieldDescriptor* field) const; + float GetFloat(const Message& message, const FieldDescriptor* field) const; + double GetDouble(const Message& message, const FieldDescriptor* field) const; + bool GetBool(const Message& message, const FieldDescriptor* field) const; + std::string GetString(const Message& message, + const FieldDescriptor* field) const; + const EnumValueDescriptor* GetEnum(const Message& message, + const FieldDescriptor* field) const; // GetEnumValue() returns an enum field's value as an integer rather than // an EnumValueDescriptor*. If the integer value does not correspond to a // known value descriptor, a new value descriptor is created. (Such a value // will only be present when the new unknown-enum-value semantics are enabled // for a message.) - virtual int GetEnumValue( - const Message& message, const FieldDescriptor* field) const = 0; + int GetEnumValue(const Message& message, const FieldDescriptor* field) const; // See MutableMessage() for the meaning of the "factory" parameter. - virtual const Message& GetMessage(const Message& message, - const FieldDescriptor* field, - MessageFactory* factory = NULL) const = 0; + const Message& GetMessage(const Message& message, + const FieldDescriptor* field, + MessageFactory* factory = nullptr) const; // Get a string value without copying, if possible. // // GetString() necessarily returns a copy of the string. This can be - // inefficient when the string is already stored in a string object in the - // underlying message. GetStringReference() will return a reference to the - // underlying string in this case. Otherwise, it will copy the string into - // *scratch and return that. + // inefficient when the std::string is already stored in a std::string object + // in the underlying message. GetStringReference() will return a reference to + // the underlying std::string in this case. Otherwise, it will copy the + // string into *scratch and return that. // // Note: It is perfectly reasonable and useful to write code like: // str = reflection->GetStringReference(message, field, &str); // This line would ensure that only one copy of the string is made // regardless of the field's underlying representation. When initializing - // a newly-constructed string, though, it's just as fast and more readable - // to use code like: - // string str = reflection->GetString(message, field); - virtual const string& GetStringReference(const Message& message, - const FieldDescriptor* field, - string* scratch) const = 0; + // a newly-constructed string, though, it's just as fast and more + // readable to use code like: + // std::string str = reflection->GetString(message, field); + const std::string& GetStringReference(const Message& message, + const FieldDescriptor* field, + std::string* scratch) const; // Singular field mutators ----------------------------------------- // These mutate the value of a non-repeated field. - virtual void SetInt32 (Message* message, - const FieldDescriptor* field, int32 value) const = 0; - virtual void SetInt64 (Message* message, - const FieldDescriptor* field, int64 value) const = 0; - virtual void SetUInt32(Message* message, - const FieldDescriptor* field, uint32 value) const = 0; - virtual void SetUInt64(Message* message, - const FieldDescriptor* field, uint64 value) const = 0; - virtual void SetFloat (Message* message, - const FieldDescriptor* field, float value) const = 0; - virtual void SetDouble(Message* message, - const FieldDescriptor* field, double value) const = 0; - virtual void SetBool (Message* message, - const FieldDescriptor* field, bool value) const = 0; - virtual void SetString(Message* message, - const FieldDescriptor* field, - const string& value) const = 0; - virtual void SetEnum (Message* message, - const FieldDescriptor* field, - const EnumValueDescriptor* value) const = 0; + void SetInt32(Message* message, const FieldDescriptor* field, + int32 value) const; + void SetInt64(Message* message, const FieldDescriptor* field, + int64 value) const; + void SetUInt32(Message* message, const FieldDescriptor* field, + uint32 value) const; + void SetUInt64(Message* message, const FieldDescriptor* field, + uint64 value) const; + void SetFloat(Message* message, const FieldDescriptor* field, + float value) const; + void SetDouble(Message* message, const FieldDescriptor* field, + double value) const; + void SetBool(Message* message, const FieldDescriptor* field, + bool value) const; + void SetString(Message* message, const FieldDescriptor* field, + const std::string& value) const; + void SetEnum(Message* message, const FieldDescriptor* field, + const EnumValueDescriptor* value) const; // Set an enum field's value with an integer rather than EnumValueDescriptor. // For proto3 this is just setting the enum field to the value specified, for // proto2 it's more complicated. If value is a known enum value the field is @@ -610,9 +549,8 @@ class LIBPROTOBUF_EXPORT Reflection { // set. Note this matches the behavior of parsing unknown enum values. // If multiple calls with unknown values happen than they are all added to the // unknown field set in order of the calls. - virtual void SetEnumValue(Message* message, - const FieldDescriptor* field, - int value) const = 0; + void SetEnumValue(Message* message, const FieldDescriptor* field, + int value) const; // Get a mutable pointer to a field with a message type. If a MessageFactory // is provided, it will be used to construct instances of the sub-message; @@ -624,106 +562,86 @@ class LIBPROTOBUF_EXPORT Reflection { // FieldDescriptor is for a compiled-in extension, then // factory->GetPrototype(field->message_type()) MUST return an instance of // the compiled-in class for this type, NOT DynamicMessage. - virtual Message* MutableMessage(Message* message, - const FieldDescriptor* field, - MessageFactory* factory = NULL) const = 0; + Message* MutableMessage(Message* message, const FieldDescriptor* field, + MessageFactory* factory = nullptr) const; // Replaces the message specified by 'field' with the already-allocated object // sub_message, passing ownership to the message. If the field contained a - // message, that message is deleted. If sub_message is NULL, the field is + // message, that message is deleted. If sub_message is nullptr, the field is // cleared. - virtual void SetAllocatedMessage(Message* message, - Message* sub_message, - const FieldDescriptor* field) const = 0; + void SetAllocatedMessage(Message* message, Message* sub_message, + const FieldDescriptor* field) const; // Releases the message specified by 'field' and returns the pointer, // ReleaseMessage() will return the message the message object if it exists. - // Otherwise, it may or may not return NULL. In any case, if the return value - // is non-NULL, the caller takes ownership of the pointer. + // Otherwise, it may or may not return nullptr. In any case, if the return + // value is non-null, the caller takes ownership of the pointer. // If the field existed (HasField() is true), then the returned pointer will // be the same as the pointer returned by MutableMessage(). // This function has the same effect as ClearField(). - virtual Message* ReleaseMessage(Message* message, - const FieldDescriptor* field, - MessageFactory* factory = NULL) const = 0; + Message* ReleaseMessage(Message* message, const FieldDescriptor* field, + MessageFactory* factory = nullptr) const; // Repeated field getters ------------------------------------------ // These get the value of one element of a repeated field. - virtual int32 GetRepeatedInt32 (const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual int64 GetRepeatedInt64 (const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual uint32 GetRepeatedUInt32(const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual uint64 GetRepeatedUInt64(const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual float GetRepeatedFloat (const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual double GetRepeatedDouble(const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual bool GetRepeatedBool (const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual string GetRepeatedString(const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual const EnumValueDescriptor* GetRepeatedEnum( - const Message& message, - const FieldDescriptor* field, int index) const = 0; + int32 GetRepeatedInt32(const Message& message, const FieldDescriptor* field, + int index) const; + int64 GetRepeatedInt64(const Message& message, const FieldDescriptor* field, + int index) const; + uint32 GetRepeatedUInt32(const Message& message, const FieldDescriptor* field, + int index) const; + uint64 GetRepeatedUInt64(const Message& message, const FieldDescriptor* field, + int index) const; + float GetRepeatedFloat(const Message& message, const FieldDescriptor* field, + int index) const; + double GetRepeatedDouble(const Message& message, const FieldDescriptor* field, + int index) const; + bool GetRepeatedBool(const Message& message, const FieldDescriptor* field, + int index) const; + std::string GetRepeatedString(const Message& message, + const FieldDescriptor* field, int index) const; + const EnumValueDescriptor* GetRepeatedEnum(const Message& message, + const FieldDescriptor* field, + int index) const; // GetRepeatedEnumValue() returns an enum field's value as an integer rather // than an EnumValueDescriptor*. If the integer value does not correspond to a // known value descriptor, a new value descriptor is created. (Such a value // will only be present when the new unknown-enum-value semantics are enabled // for a message.) - virtual int GetRepeatedEnumValue( - const Message& message, - const FieldDescriptor* field, int index) const = 0; - virtual const Message& GetRepeatedMessage( - const Message& message, - const FieldDescriptor* field, int index) const = 0; + int GetRepeatedEnumValue(const Message& message, const FieldDescriptor* field, + int index) const; + const Message& GetRepeatedMessage(const Message& message, + const FieldDescriptor* field, + int index) const; // See GetStringReference(), above. - virtual const string& GetRepeatedStringReference( - const Message& message, const FieldDescriptor* field, - int index, string* scratch) const = 0; + const std::string& GetRepeatedStringReference(const Message& message, + const FieldDescriptor* field, + int index, + std::string* scratch) const; // Repeated field mutators ----------------------------------------- // These mutate the value of one element of a repeated field. - virtual void SetRepeatedInt32 (Message* message, - const FieldDescriptor* field, - int index, int32 value) const = 0; - virtual void SetRepeatedInt64 (Message* message, - const FieldDescriptor* field, - int index, int64 value) const = 0; - virtual void SetRepeatedUInt32(Message* message, - const FieldDescriptor* field, - int index, uint32 value) const = 0; - virtual void SetRepeatedUInt64(Message* message, - const FieldDescriptor* field, - int index, uint64 value) const = 0; - virtual void SetRepeatedFloat (Message* message, - const FieldDescriptor* field, - int index, float value) const = 0; - virtual void SetRepeatedDouble(Message* message, - const FieldDescriptor* field, - int index, double value) const = 0; - virtual void SetRepeatedBool (Message* message, - const FieldDescriptor* field, - int index, bool value) const = 0; - virtual void SetRepeatedString(Message* message, - const FieldDescriptor* field, - int index, const string& value) const = 0; - virtual void SetRepeatedEnum(Message* message, - const FieldDescriptor* field, int index, - const EnumValueDescriptor* value) const = 0; + void SetRepeatedInt32(Message* message, const FieldDescriptor* field, + int index, int32 value) const; + void SetRepeatedInt64(Message* message, const FieldDescriptor* field, + int index, int64 value) const; + void SetRepeatedUInt32(Message* message, const FieldDescriptor* field, + int index, uint32 value) const; + void SetRepeatedUInt64(Message* message, const FieldDescriptor* field, + int index, uint64 value) const; + void SetRepeatedFloat(Message* message, const FieldDescriptor* field, + int index, float value) const; + void SetRepeatedDouble(Message* message, const FieldDescriptor* field, + int index, double value) const; + void SetRepeatedBool(Message* message, const FieldDescriptor* field, + int index, bool value) const; + void SetRepeatedString(Message* message, const FieldDescriptor* field, + int index, const std::string& value) const; + void SetRepeatedEnum(Message* message, const FieldDescriptor* field, + int index, const EnumValueDescriptor* value) const; // Set an enum field's value with an integer rather than EnumValueDescriptor. // For proto3 this is just setting the enum field to the value specified, for // proto2 it's more complicated. If value is a known enum value the field is @@ -731,38 +649,36 @@ class LIBPROTOBUF_EXPORT Reflection { // set. Note this matches the behavior of parsing unknown enum values. // If multiple calls with unknown values happen than they are all added to the // unknown field set in order of the calls. - virtual void SetRepeatedEnumValue(Message* message, - const FieldDescriptor* field, int index, - int value) const = 0; + void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field, + int index, int value) const; // Get a mutable pointer to an element of a repeated field with a message // type. - virtual Message* MutableRepeatedMessage( - Message* message, const FieldDescriptor* field, int index) const = 0; + Message* MutableRepeatedMessage(Message* message, + const FieldDescriptor* field, + int index) const; // Repeated field adders ------------------------------------------- // These add an element to a repeated field. - virtual void AddInt32 (Message* message, - const FieldDescriptor* field, int32 value) const = 0; - virtual void AddInt64 (Message* message, - const FieldDescriptor* field, int64 value) const = 0; - virtual void AddUInt32(Message* message, - const FieldDescriptor* field, uint32 value) const = 0; - virtual void AddUInt64(Message* message, - const FieldDescriptor* field, uint64 value) const = 0; - virtual void AddFloat (Message* message, - const FieldDescriptor* field, float value) const = 0; - virtual void AddDouble(Message* message, - const FieldDescriptor* field, double value) const = 0; - virtual void AddBool (Message* message, - const FieldDescriptor* field, bool value) const = 0; - virtual void AddString(Message* message, - const FieldDescriptor* field, - const string& value) const = 0; - virtual void AddEnum (Message* message, - const FieldDescriptor* field, - const EnumValueDescriptor* value) const = 0; + void AddInt32(Message* message, const FieldDescriptor* field, + int32 value) const; + void AddInt64(Message* message, const FieldDescriptor* field, + int64 value) const; + void AddUInt32(Message* message, const FieldDescriptor* field, + uint32 value) const; + void AddUInt64(Message* message, const FieldDescriptor* field, + uint64 value) const; + void AddFloat(Message* message, const FieldDescriptor* field, + float value) const; + void AddDouble(Message* message, const FieldDescriptor* field, + double value) const; + void AddBool(Message* message, const FieldDescriptor* field, + bool value) const; + void AddString(Message* message, const FieldDescriptor* field, + const std::string& value) const; + void AddEnum(Message* message, const FieldDescriptor* field, + const EnumValueDescriptor* value) const; // Add an integer value to a repeated enum field rather than // EnumValueDescriptor. For proto3 this is just setting the enum field to the // value specified, for proto2 it's more complicated. If value is a known enum @@ -770,21 +686,16 @@ class LIBPROTOBUF_EXPORT Reflection { // to the unknown field set. Note this matches the behavior of parsing unknown // enum values. If multiple calls with unknown values happen than they are all // added to the unknown field set in order of the calls. - virtual void AddEnumValue(Message* message, - const FieldDescriptor* field, - int value) const = 0; + void AddEnumValue(Message* message, const FieldDescriptor* field, + int value) const; // See MutableMessage() for comments on the "factory" parameter. - virtual Message* AddMessage(Message* message, - const FieldDescriptor* field, - MessageFactory* factory = NULL) const = 0; + Message* AddMessage(Message* message, const FieldDescriptor* field, + MessageFactory* factory = nullptr) const; // Appends an already-allocated object 'new_entry' to the repeated field // specified by 'field' passing ownership to the message. - // TODO(tmarek): Make virtual after all subclasses have been - // updated. - virtual void AddAllocatedMessage(Message* message, - const FieldDescriptor* field, - Message* new_entry) const; + void AddAllocatedMessage(Message* message, const FieldDescriptor* field, + Message* new_entry) const; // Get a RepeatedFieldRef object that can be used to read the underlying @@ -801,7 +712,7 @@ class LIBPROTOBUF_EXPORT Reflection { // CPPTYPE_FLOAT float // CPPTYPE_BOOL bool // CPPTYPE_ENUM generated enum type or int32 - // CPPTYPE_STRING string + // CPPTYPE_STRING std::string // CPPTYPE_MESSAGE generated message type or google::protobuf::Message // // A RepeatedFieldRef object can be copied and the resulted object will point @@ -811,13 +722,13 @@ class LIBPROTOBUF_EXPORT Reflection { // Note that to use this method users need to include the header file // "net/proto2/public/reflection.h" (which defines the RepeatedFieldRef // class templates). - template - RepeatedFieldRef GetRepeatedFieldRef( - const Message& message, const FieldDescriptor* field) const; + template + RepeatedFieldRef GetRepeatedFieldRef(const Message& message, + const FieldDescriptor* field) const; // Like GetRepeatedFieldRef() but return an object that can also be used // manipulate the underlying repeated field. - template + template MutableRepeatedFieldRef GetMutableRepeatedFieldRef( Message* message, const FieldDescriptor* field) const; @@ -839,8 +750,7 @@ class LIBPROTOBUF_EXPORT Reflection { // // for T = Cord and all protobuf scalar types except enums. template - GOOGLE_PROTOBUF_DEPRECATED_MSG( - "Please use GetRepeatedFieldRef() instead") + PROTOBUF_DEPRECATED_MSG("Please use GetRepeatedFieldRef() instead") const RepeatedField& GetRepeatedField(const Message&, const FieldDescriptor*) const; @@ -848,42 +758,42 @@ class LIBPROTOBUF_EXPORT Reflection { // // for T = Cord and all protobuf scalar types except enums. template - GOOGLE_PROTOBUF_DEPRECATED_MSG( - "Please use GetMutableRepeatedFieldRef() instead") + PROTOBUF_DEPRECATED_MSG("Please use GetMutableRepeatedFieldRef() instead") RepeatedField* MutableRepeatedField(Message*, const FieldDescriptor*) const; // DEPRECATED. Please use GetRepeatedFieldRef(). // - // for T = string, google::protobuf::internal::StringPieceField + // for T = std::string, google::protobuf::internal::StringPieceField // google::protobuf::Message & descendants. template - GOOGLE_PROTOBUF_DEPRECATED_MSG( - "Please use GetRepeatedFieldRef() instead") + PROTOBUF_DEPRECATED_MSG("Please use GetRepeatedFieldRef() instead") const RepeatedPtrField& GetRepeatedPtrField(const Message&, const FieldDescriptor*) const; // DEPRECATED. Please use GetMutableRepeatedFieldRef(). // - // for T = string, google::protobuf::internal::StringPieceField + // for T = std::string, google::protobuf::internal::StringPieceField // google::protobuf::Message & descendants. template - GOOGLE_PROTOBUF_DEPRECATED_MSG( - "Please use GetMutableRepeatedFieldRef() instead") + PROTOBUF_DEPRECATED_MSG("Please use GetMutableRepeatedFieldRef() instead") RepeatedPtrField* MutableRepeatedPtrField(Message*, const FieldDescriptor*) const; // Extensions ---------------------------------------------------------------- // Try to find an extension of this message type by fully-qualified field - // name. Returns NULL if no extension is known for this name or number. - virtual const FieldDescriptor* FindKnownExtensionByName( - const string& name) const = 0; + // name. Returns nullptr if no extension is known for this name or number. + PROTOBUF_DEPRECATED_MSG( + "Please use DescriptorPool::FindExtensionByPrintableName instead") + const FieldDescriptor* FindKnownExtensionByName( + const std::string& name) const; // Try to find an extension of this message type by field number. - // Returns NULL if no extension is known for this name or number. - virtual const FieldDescriptor* FindKnownExtensionByNumber( - int number) const = 0; + // Returns nullptr if no extension is known for this name or number. + PROTOBUF_DEPRECATED_MSG( + "Please use DescriptorPool::FindExtensionByNumber instead") + const FieldDescriptor* FindKnownExtensionByNumber(int number) const; // Feature Flags ------------------------------------------------------------- @@ -904,7 +814,7 @@ class LIBPROTOBUF_EXPORT Reflection { // reflection->SetEnumValue(message, field, new_value); // } else { // if (field_descriptor->enum_type()-> - // FindValueByNumber(new_value) != NULL) { + // FindValueByNumber(new_value) != nullptr) { // reflection->SetEnumValue(message, field, new_value); // } else if (emit_unknown_enum_values) { // reflection->MutableUnknownFields(message)->AddVarint( @@ -915,7 +825,7 @@ class LIBPROTOBUF_EXPORT Reflection { // reflection->SetEnumValue(message, field, new_value); // } // } - virtual bool SupportsUnknownEnumValues() const { return false; } + bool SupportsUnknownEnumValues() const; // Returns the MessageFactory associated with this message. This can be // useful for determining if a message is a generated message or not, for @@ -926,60 +836,63 @@ class LIBPROTOBUF_EXPORT Reflection { // } // It can also be used to create more messages of this type, though // Message::New() is an easier way to accomplish this. - virtual MessageFactory* GetMessageFactory() const; - - // --------------------------------------------------------------------------- + MessageFactory* GetMessageFactory() const; - protected: + private: // Obtain a pointer to a Repeated Field Structure and do some type checking: // on field->cpp_type(), // on field->field_option().ctype() (if ctype >= 0) - // of field->message_type() (if message_type != NULL). + // of field->message_type() (if message_type != nullptr). // We use 2 routine rather than 4 (const vs mutable) x (scalar vs pointer). - virtual void* MutableRawRepeatedField( - Message* message, const FieldDescriptor* field, FieldDescriptor::CppType, - int ctype, const Descriptor* message_type) const = 0; - - // TODO(jieluo) - make it pure virtual after updating all the subclasses. - virtual const void* GetRawRepeatedField( - const Message& message, const FieldDescriptor* field, - FieldDescriptor::CppType cpptype, int ctype, - const Descriptor* message_type) const { - return MutableRawRepeatedField( - const_cast(&message), field, cpptype, ctype, message_type); - } + void* MutableRawRepeatedField(Message* message, const FieldDescriptor* field, + FieldDescriptor::CppType, int ctype, + const Descriptor* message_type) const; + + const void* GetRawRepeatedField(const Message& message, + const FieldDescriptor* field, + FieldDescriptor::CppType cpptype, int ctype, + const Descriptor* message_type) const; // The following methods are used to implement (Mutable)RepeatedFieldRef. // A Ref object will store a raw pointer to the repeated field data (obtained // from RepeatedFieldData()) and a pointer to a Accessor (obtained from // RepeatedFieldAccessor) which will be used to access the raw data. - // - // TODO(xiaofeng): Make these methods pure-virtual. // Returns a raw pointer to the repeated field // // "cpp_type" and "message_type" are deduced from the type parameter T passed // to Get(Mutable)RepeatedFieldRef. If T is a generated message type, // "message_type" should be set to its descriptor. Otherwise "message_type" - // should be set to NULL. Implementations of this method should check whether - // "cpp_type"/"message_type" is consistent with the actual type of the field. - // We use 1 routine rather than 2 (const vs mutable) because it is protected - // and it doesn't change the message. - virtual void* RepeatedFieldData( - Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType cpp_type, - const Descriptor* message_type) const; + // should be set to nullptr. Implementations of this method should check + // whether "cpp_type"/"message_type" is consistent with the actual type of the + // field. We use 1 routine rather than 2 (const vs mutable) because it is + // protected and it doesn't change the message. + void* RepeatedFieldData(Message* message, const FieldDescriptor* field, + FieldDescriptor::CppType cpp_type, + const Descriptor* message_type) const; // The returned pointer should point to a singleton instance which implements // the RepeatedFieldAccessor interface. - virtual const internal::RepeatedFieldAccessor* RepeatedFieldAccessor( + const internal::RepeatedFieldAccessor* RepeatedFieldAccessor( const FieldDescriptor* field) const; - private: - template + const Descriptor* const descriptor_; + const internal::ReflectionSchema schema_; + const DescriptorPool* const descriptor_pool_; + MessageFactory* const message_factory_; + + // Last non weak field index. This is an optimization when most weak fields + // are at the end of the containing message. If a message proto doesn't + // contain weak fields, then this field equals descriptor_->field_count(). + int last_non_weak_field_index_; + + template friend class RepeatedFieldRef; - template + template friend class MutableRepeatedFieldRef; + friend class ::PROTOBUF_NAMESPACE_ID::MessageLayoutInspector; + friend class ::PROTOBUF_NAMESPACE_ID::AssignDescriptorsHelper; + friend class DynamicMessageFactory; friend class python::MapReflectionFriend; #define GOOGLE_PROTOBUF_HAS_CEL_MAP_REFLECTION_FRIEND friend class expr::CelMapReflectionFriend; @@ -987,77 +900,174 @@ class LIBPROTOBUF_EXPORT Reflection { friend class internal::MapKeySorter; friend class internal::WireFormat; friend class internal::ReflectionOps; + // Needed for implementing text format for map. + friend class internal::MapFieldPrinterHelper; + friend class internal::ReflectionAccessor; - // Special version for specialized implementations of string. We can't call - // MutableRawRepeatedField directly here because we don't have access to + Reflection(const Descriptor* descriptor, + const internal::ReflectionSchema& schema, + const DescriptorPool* pool, MessageFactory* factory); + + // Special version for specialized implementations of string. We can't + // call MutableRawRepeatedField directly here because we don't have access to // FieldOptions::* which are defined in descriptor.pb.h. Including that // file here is not possible because it would cause a circular include cycle. // We use 1 routine rather than 2 (const vs mutable) because it is private // and mutable a repeated string field doesn't change the message. - void* MutableRawRepeatedString( - Message* message, const FieldDescriptor* field, bool is_string) const; + void* MutableRawRepeatedString(Message* message, const FieldDescriptor* field, + bool is_string) const; friend class MapReflectionTester; - // TODO(jieluo) - make the map APIs pure virtual after updating - // all the subclasses. // Returns true if key is in map. Returns false if key is not in map field. - virtual bool ContainsMapKey(const Message& /* message */, - const FieldDescriptor* /* field */, - const MapKey& /* key */) const { - return false; - } + bool ContainsMapKey(const Message& message, const FieldDescriptor* field, + const MapKey& key) const; // If key is in map field: Saves the value pointer to val and returns // false. If key in not in map field: Insert the key into map, saves // value pointer to val and retuns true. - virtual bool InsertOrLookupMapValue(Message* /* message */, - const FieldDescriptor* /* field */, - const MapKey& /* key */, - MapValueRef* /* val */) const { - return false; - } + bool InsertOrLookupMapValue(Message* message, const FieldDescriptor* field, + const MapKey& key, MapValueRef* val) const; // Delete and returns true if key is in the map field. Returns false // otherwise. - virtual bool DeleteMapValue(Message* /* message */, - const FieldDescriptor* /* field */, - const MapKey& /* key */) const { - return false; - } + bool DeleteMapValue(Message* message, const FieldDescriptor* field, + const MapKey& key) const; // Returns a MapIterator referring to the first element in the map field. // If the map field is empty, this function returns the same as // reflection::MapEnd. Mutation to the field may invalidate the iterator. - virtual MapIterator MapBegin( - Message* message, - const FieldDescriptor* field) const; + MapIterator MapBegin(Message* message, const FieldDescriptor* field) const; // Returns a MapIterator referring to the theoretical element that would // follow the last element in the map field. It does not point to any // real element. Mutation to the field may invalidate the iterator. - virtual MapIterator MapEnd( - Message* message, - const FieldDescriptor* field) const; + MapIterator MapEnd(Message* message, const FieldDescriptor* field) const; // Get the number of pair of a map field. The result may be // different from FieldSize which can have duplicate keys. - virtual int MapSize(const Message& /* message */, - const FieldDescriptor* /* field */) const { - return 0; - } + int MapSize(const Message& message, const FieldDescriptor* field) const; // Help method for MapIterator. friend class MapIterator; - virtual internal::MapFieldBase* MapData( - Message* /* message */, const FieldDescriptor* /* field */) const { - return NULL; - } + internal::MapFieldBase* MutableMapData(Message* message, + const FieldDescriptor* field) const; + + const internal::MapFieldBase* GetMapData(const Message& message, + const FieldDescriptor* field) const; + + template + const T& GetRawNonOneof(const Message& message, + const FieldDescriptor* field) const; + template + T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const; + + template + const Type& GetRaw(const Message& message, + const FieldDescriptor* field) const; + template + inline Type* MutableRaw(Message* message, const FieldDescriptor* field) const; + template + inline const Type& DefaultRaw(const FieldDescriptor* field) const; + + inline const uint32* GetHasBits(const Message& message) const; + inline uint32* MutableHasBits(Message* message) const; + inline uint32 GetOneofCase(const Message& message, + const OneofDescriptor* oneof_descriptor) const; + inline uint32* MutableOneofCase( + Message* message, const OneofDescriptor* oneof_descriptor) const; + inline const internal::ExtensionSet& GetExtensionSet( + const Message& message) const; + inline internal::ExtensionSet* MutableExtensionSet(Message* message) const; + inline Arena* GetArena(Message* message) const; + + inline const internal::InternalMetadataWithArena& + GetInternalMetadataWithArena(const Message& message) const; + + internal::InternalMetadataWithArena* MutableInternalMetadataWithArena( + Message* message) const; + + inline bool IsInlined(const FieldDescriptor* field) const; + + inline bool HasBit(const Message& message, + const FieldDescriptor* field) const; + inline void SetBit(Message* message, const FieldDescriptor* field) const; + inline void ClearBit(Message* message, const FieldDescriptor* field) const; + inline void SwapBit(Message* message1, Message* message2, + const FieldDescriptor* field) const; + + // This function only swaps the field. Should swap corresponding has_bit + // before or after using this function. + void SwapField(Message* message1, Message* message2, + const FieldDescriptor* field) const; + + void SwapOneofField(Message* message1, Message* message2, + const OneofDescriptor* oneof_descriptor) const; + + inline bool HasOneofField(const Message& message, + const FieldDescriptor* field) const; + inline void SetOneofCase(Message* message, + const FieldDescriptor* field) const; + inline void ClearOneofField(Message* message, + const FieldDescriptor* field) const; + + template + inline const Type& GetField(const Message& message, + const FieldDescriptor* field) const; + template + inline void SetField(Message* message, const FieldDescriptor* field, + const Type& value) const; + template + inline Type* MutableField(Message* message, + const FieldDescriptor* field) const; + template + inline const Type& GetRepeatedField(const Message& message, + const FieldDescriptor* field, + int index) const; + template + inline const Type& GetRepeatedPtrField(const Message& message, + const FieldDescriptor* field, + int index) const; + template + inline void SetRepeatedField(Message* message, const FieldDescriptor* field, + int index, Type value) const; + template + inline Type* MutableRepeatedField(Message* message, + const FieldDescriptor* field, + int index) const; + template + inline void AddField(Message* message, const FieldDescriptor* field, + const Type& value) const; + template + inline Type* AddField(Message* message, const FieldDescriptor* field) const; + + int GetExtensionNumberOrDie(const Descriptor* type) const; + + // Internal versions of EnumValue API perform no checking. Called after checks + // by public methods. + void SetEnumValueInternal(Message* message, const FieldDescriptor* field, + int value) const; + void SetRepeatedEnumValueInternal(Message* message, + const FieldDescriptor* field, int index, + int value) const; + void AddEnumValueInternal(Message* message, const FieldDescriptor* field, + int value) const; + + Message* UnsafeArenaReleaseMessage(Message* message, + const FieldDescriptor* field, + MessageFactory* factory = nullptr) const; + + void UnsafeArenaSetAllocatedMessage(Message* message, Message* sub_message, + const FieldDescriptor* field) const; + + friend inline // inline so nobody can call this function. + void + RegisterAllTypesInternal(const Metadata* file_level_metadata, int size); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection); }; // Abstract interface for a factory for message objects. -class LIBPROTOBUF_EXPORT MessageFactory { +class PROTOBUF_EXPORT MessageFactory { public: inline MessageFactory() {} virtual ~MessageFactory(); @@ -1076,7 +1086,7 @@ class LIBPROTOBUF_EXPORT MessageFactory { // outlive the MessageFactory. // // Some implementations do not support all types. GetPrototype() will - // return NULL if the descriptor passed in is not supported. + // return nullptr if the descriptor passed in is not supported. // // This method may or may not be thread-safe depending on the implementation. // Each implementation should document its own degree thread-safety. @@ -1088,7 +1098,7 @@ class LIBPROTOBUF_EXPORT MessageFactory { // FooMessage::descriptor()) == FooMessage::default_instance() // This factory supports all types which are found in // DescriptorPool::generated_pool(). If given a descriptor from any other - // pool, GetPrototype() will return NULL. (You can also check if a + // pool, GetPrototype() will return nullptr. (You can also check if a // descriptor is for a generated message by checking if // descriptor->file()->pool() == DescriptorPool::generated_pool().) // @@ -1106,8 +1116,8 @@ class LIBPROTOBUF_EXPORT MessageFactory { // in the file. This strange mechanism is necessary because descriptors are // built lazily, so we can't register types by their descriptor until we // know that the descriptor exists. |filename| must be a permanent string. - static void InternalRegisterGeneratedFile(const char* filename, - void* assign_descriptors_table); + static void InternalRegisterGeneratedFile( + const google::protobuf::internal::DescriptorTable* table); // For internal use only: Registers a message type. Called only by the // functions which are registered with InternalRegisterGeneratedFile(), @@ -1120,16 +1130,15 @@ class LIBPROTOBUF_EXPORT MessageFactory { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFactory); }; -#define DECLARE_GET_REPEATED_FIELD(TYPE) \ -template<> \ -LIBPROTOBUF_EXPORT \ -const RepeatedField& Reflection::GetRepeatedField( \ - const Message& message, const FieldDescriptor* field) const; \ - \ -template<> \ -LIBPROTOBUF_EXPORT \ -RepeatedField* Reflection::MutableRepeatedField( \ - Message* message, const FieldDescriptor* field) const; +#define DECLARE_GET_REPEATED_FIELD(TYPE) \ + template <> \ + PROTOBUF_EXPORT const RepeatedField& \ + Reflection::GetRepeatedField(const Message& message, \ + const FieldDescriptor* field) const; \ + \ + template <> \ + PROTOBUF_EXPORT RepeatedField* Reflection::MutableRepeatedField( \ + Message * message, const FieldDescriptor* field) const; DECLARE_GET_REPEATED_FIELD(int32) DECLARE_GET_REPEATED_FIELD(int64) @@ -1141,8 +1150,8 @@ DECLARE_GET_REPEATED_FIELD(bool) #undef DECLARE_GET_REPEATED_FIELD -// Tries to downcast this message to a generated message type. Returns NULL if -// this class is not an instance of T. This works even if RTTI is disabled. +// Tries to downcast this message to a generated message type. Returns nullptr +// if this class is not an instance of T. This works even if RTTI is disabled. // // This also has the effect of creating a strong reference to T that will // prevent the linker from stripping it out at link time. This can be important @@ -1152,7 +1161,7 @@ template const T* DynamicCastToGenerated(const Message* from) { // Compile-time assert that T is a generated type that has a // default_instance() accessor, but avoid actually calling it. - const T&(*get_default_instance)() = &T::default_instance; + const T& (*get_default_instance)() = &T::default_instance; (void)get_default_instance; // Compile-time assert that T is a subclass of google::protobuf::Message. @@ -1173,77 +1182,87 @@ T* DynamicCastToGenerated(Message* from) { return const_cast(DynamicCastToGenerated(message_const)); } -namespace internal { - -// Legacy functions, to preserve compatibility with existing callers. -// These had a slightly different signature, so we have to adjust "T". -template -T dynamic_cast_if_available(const Message* from) { - return DynamicCastToGenerated::type>(from); -} +// Call this function to ensure that this message's reflection is linked into +// the binary: +// +// google::protobuf::LinkMessageReflection(); +// +// This will ensure that the following lookup will succeed: +// +// DescriptorPool::generated_pool()->FindMessageTypeByName("FooMessage"); +// +// 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 message, 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 the tightest +// of loops (on x86-64 it compiles into two "mov" instructions). template -T dynamic_cast_if_available(Message* from) { - return DynamicCastToGenerated::type>(from); +void LinkMessageReflection() { + typedef const T& GetDefaultInstanceFunction(); + GetDefaultInstanceFunction* volatile unused = &T::default_instance; + (void)&unused; // Use address to avoid an extra load of volatile variable. } -} // namespace internal - // ============================================================================= // Implementation details for {Get,Mutable}RawRepeatedPtrField. We provide -// specializations for , and and handle -// everything else with the default template which will match any type having -// a method with signature "static const google::protobuf::Descriptor* descriptor()". -// Such a type presumably is a descendant of google::protobuf::Message. - -template<> -inline const RepeatedPtrField& Reflection::GetRepeatedPtrField( +// specializations for , and and +// handle everything else with the default template which will match any type +// having a method with signature "static const google::protobuf::Descriptor* +// descriptor()". Such a type presumably is a descendant of google::protobuf::Message. + +template <> +inline const RepeatedPtrField& +Reflection::GetRepeatedPtrField( const Message& message, const FieldDescriptor* field) const { - return *static_cast* >( + return *static_cast*>( MutableRawRepeatedString(const_cast(&message), field, true)); } -template<> -inline RepeatedPtrField* Reflection::MutableRepeatedPtrField( +template <> +inline RepeatedPtrField* +Reflection::MutableRepeatedPtrField( Message* message, const FieldDescriptor* field) const { - return static_cast* >( + return static_cast*>( MutableRawRepeatedString(message, field, true)); } // ----- -template<> +template <> inline const RepeatedPtrField& Reflection::GetRepeatedPtrField( const Message& message, const FieldDescriptor* field) const { - return *static_cast* >( - GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, - -1, NULL)); + return *static_cast*>(GetRawRepeatedField( + message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr)); } -template<> +template <> inline RepeatedPtrField* Reflection::MutableRepeatedPtrField( Message* message, const FieldDescriptor* field) const { - return static_cast* >( - MutableRawRepeatedField(message, field, - FieldDescriptor::CPPTYPE_MESSAGE, -1, - NULL)); + return static_cast*>(MutableRawRepeatedField( + message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr)); } -template +template inline const RepeatedPtrField& Reflection::GetRepeatedPtrField( const Message& message, const FieldDescriptor* field) const { - return *static_cast* >( - GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, - -1, PB::default_instance().GetDescriptor())); + return *static_cast*>( + GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, + PB::default_instance().GetDescriptor())); } -template +template inline RepeatedPtrField* Reflection::MutableRepeatedPtrField( Message* message, const FieldDescriptor* field) const { - return static_cast* >( - MutableRawRepeatedField(message, field, - FieldDescriptor::CPPTYPE_MESSAGE, -1, - PB::default_instance().GetDescriptor())); + return static_cast*>( + MutableRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, + -1, PB::default_instance().GetDescriptor())); } } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index 33ee632326..2babba0b65 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -33,35 +33,42 @@ // 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 -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -#include -#include "util/utf8/public/unilib.h" -#include "util/utf8/public/unilib_utf8_utils.h" -#endif - namespace google { namespace protobuf { -string MessageLite::InitializationErrorString() const { +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. @@ -80,12 +87,13 @@ void ByteSizeConsistencyError(size_t byte_size_before_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() << "."; + "concurrent modification of " + << message.GetTypeName() << "."; GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal."; } -string InitializationErrorMessage(const char* action, - const MessageLite& message) { +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: // @@ -95,7 +103,7 @@ string InitializationErrorMessage(const char* action, // action, message.GetTypeName(), // message.InitializationErrorString()); - string result; + std::string result; result += "Can't "; result += action; result += " message of type \""; @@ -105,291 +113,95 @@ string InitializationErrorMessage(const char* action, return result; } -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -// This is wrapper to turn a ZeroCopyInputStream (ZCIS) into a -// InputStreamWithOverlap. This is done by copying data around the seams, -// pictorially if ZCIS presents a stream in chunks like so -// [---------------------------------------------------------------] -// [---------------------] chunk 1 -// [----------------------------] chunk 2 -// chunk 3 [--------------] -// this class will convert this into chunks -// [-----------------....] chunk 1 -// [----....] patch -// [------------------------....] chunk 2 -// [----....] patch -// chunk 3 [----------....] -// patch [----****] -// by using a fixed size buffer to patch over the seams. This requires -// copying of an "epsilon" neighboorhood around the seams. - -template -class EpsCopyInputStream { - public: - EpsCopyInputStream(io::CodedInputStream* input) : input_(input) {} - ~EpsCopyInputStream() { - if (skip_) input_->Skip(skip_); - } - - StringPiece NextWithOverlap() { - switch (next_state_) { - case kEOS: - // End of stream - return nullptr; - case kChunk: - // chunk_ contains a buffer of sufficient size (> kSlopBytes). - // To parse the last kSlopBytes we need to copy the bytes into the - // buffer. Hence we set, - next_state_ = kBuffer; - return {chunk_.begin(), chunk_.size() - kSlopBytes}; - case kBuffer: - // We have to parse the last kSlopBytes of chunk_, which could alias - // buffer_ so we have to memmove. - std::memmove(buffer_, chunk_.end() - kSlopBytes, kSlopBytes); - chunk_ = GetChunk(); - if (chunk_.size() > kSlopBytes) { - next_state_ = kChunk; - std::memcpy(buffer_ + kSlopBytes, chunk_.begin(), kSlopBytes); - return {buffer_, kSlopBytes}; - } else if (chunk_.empty()) { - next_state_ = kEOS; - return {buffer_, kSlopBytes}; - } else { - auto size = chunk_.size(); - // The next chunk is not big enough. So we copy it in the current - // after the current buffer. Resulting in a buffer with - // size + kSlopBytes bytes. - std::memcpy(buffer_ + kSlopBytes, chunk_.begin(), size); - chunk_ = {buffer_, size + kSlopBytes}; - return {buffer_, size}; - } - case kStart: { - size_t i = 0; - do { - chunk_ = GetChunk(); - if (chunk_.size() > kSlopBytes) { - if (i == 0) { - next_state_ = kBuffer; - return {chunk_.begin(), chunk_.size() - kSlopBytes}; - } - std::memcpy(buffer_ + i, chunk_.begin(), kSlopBytes); - next_state_ = kChunk; - return {buffer_, i}; - } - if (chunk_.empty()) { - next_state_ = kEOS; - return {buffer_, i}; - } - std::memcpy(buffer_ + i, chunk_.begin(), chunk_.size()); - i += chunk_.size(); - } while (i <= kSlopBytes); - chunk_ = {buffer_, i}; - next_state_ = kBuffer; - return {buffer_, i - kSlopBytes}; - } - } - } - - StringPiece NextWithOverlapEndingSafe(const char* ptr, int nesting) { - switch (next_state_) { - case kEOS: - // End of stream - return nullptr; - case kChunk: - // chunk_ contains a buffer of sufficient size (> kSlopBytes). - // To parse the last kSlopBytes we need to copy the bytes into the - // buffer. Hence we set, - next_state_ = kBuffer; - return {chunk_.begin(), chunk_.size() - kSlopBytes}; - case kBuffer: - // We have to parse the last kSlopBytes of chunk_, which could alias - // buffer_ so we have to memmove. - if (!SafeCopy(buffer_, chunk_.end() - kSlopBytes, nesting)) { - // We will terminate - } - chunk_ = GetChunk(); - if (chunk_.size() > kSlopBytes) { - next_state_ = kChunk; - std::memcpy(buffer_ + kSlopBytes, chunk_.begin(), kSlopBytes); - return {buffer_, kSlopBytes}; - } else if (chunk_.empty()) { - next_state_ = kEOS; - return {buffer_, kSlopBytes}; - } else { - auto size = chunk_.size(); - // The next chunk is not big enough. So we copy it in the current - // after the current buffer. Resulting in a buffer with - // size + kSlopBytes bytes. - std::memcpy(buffer_ + kSlopBytes, chunk_.begin(), size); - chunk_ = {buffer_, size + kSlopBytes}; - return {buffer_, size}; - } - case kStart: { - size_t i = 0; - do { - chunk_ = GetChunk(); - if (chunk_.size() > kSlopBytes) { - if (i == 0) { - next_state_ = kBuffer; - return {chunk_.begin(), chunk_.size() - kSlopBytes}; - } - std::memcpy(buffer_ + i, chunk_.begin(), kSlopBytes); - next_state_ = kChunk; - return {buffer_, i}; - } - if (chunk_.empty()) { - next_state_ = kEOS; - return {buffer_, i}; - } - std::memcpy(buffer_ + i, chunk_.begin(), chunk_.size()); - i += chunk_.size(); - } while (i <= kSlopBytes); - chunk_ = {buffer_, i}; - next_state_ = kBuffer; - return {buffer_, i - kSlopBytes}; - } - } - } - - void Backup(const char* ptr) { skip_ = ptr - chunk_.data(); } - - private: - io::CodedInputStream* input_; - StringPiece chunk_; - char buffer_[2 * kSlopBytes]; - enum State { - kEOS = 0, // -> end of stream. - kChunk = 1, // -> chunk_ contains the data for Next. - kBuffer = 2, // -> We need to copy the left over from previous chunk_ and - // load and patch the start of the next chunk in the - // local buffer. - kStart = 3, - }; - State next_state_ = kStart; - int skip_ = 0; - - StringPiece GetChunk() { - const void* ptr; - if (skip_) input_->Skip(skip_); - if (!input_->GetDirectBufferPointer(&ptr, &skip_)) { - return nullptr; - } - return StringPiece(static_cast(ptr), skip_); - } -}; -#endif - -// Several of the Parse methods below just do one thing and then call another -// method. In a naive implementation, we might have ParseFromString() call -// ParseFromArray() which would call ParseFromZeroCopyStream() which would call -// ParseFromCodedStream() which would call MergeFromCodedStream() which would -// call MergePartialFromCodedStream(). However, when parsing very small -// messages, every function call introduces significant overhead. To avoid -// this without reproducing code, we use these forced-inline helpers. - -inline bool InlineMergePartialFromCodedStream(io::CodedInputStream* input, - MessageLite* message) { -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - EpsCopyInputStream eps_input(input); - internal::ParseContext ctx; - auto res = ctx.ParseNoLimit({message->_ParseFunc(), message}, &eps_input); - if (res == 1) { - input->SetConsumed(); - return true; - } else if (res == 2) { - return false; - } else { - input->SetLastTag(res); - return true; - } -#else - return message->MergePartialFromCodedStream(input); -#endif +inline StringPiece as_string_view(const void* data, int size) { + return StringPiece(static_cast(data), size); } -inline bool InlineMergeFromCodedStream(io::CodedInputStream* input, - MessageLite* message) { - if (!InlineMergePartialFromCodedStream(input, message)) return false; - if (!message->IsInitialized()) { - GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *message); - return false; - } - return true; -} +} // namespace -inline bool InlineParsePartialFromCodedStream(io::CodedInputStream* input, - MessageLite* message) { - message->Clear(); - return InlineMergePartialFromCodedStream(input, message); +void MessageLite::LogInitializationErrorMessage() const { + GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this); } -inline bool InlineParseFromCodedStream(io::CodedInputStream* input, - MessageLite* message) { - message->Clear(); - return InlineMergeFromCodedStream(input, message); -} +namespace internal { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -template -class ArrayInput { - public: - ArrayInput(StringPiece chunk) : chunk_(chunk) {} - StringPiece NextWithOverlap() { - auto res = chunk_; - chunk_ = nullptr; - return res; - } - - void Backup(const char*) { GOOGLE_CHECK(false) << "Can't backup arrayinput"; } - private: - StringPiece chunk_; -}; -#endif - -inline bool InlineMergePartialFromArray(const void* data, int size, - MessageLite* message, - bool aliasing = false) { -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - internal::ParseContext ctx; - ArrayInput input( - StringPiece(static_cast(data), size)); - return ctx.Parse({message->_ParseFunc(), message}, size, &input); -#else - io::CodedInputStream input(static_cast(data), size); - return message->MergePartialFromCodedStream(&input) && - input.ConsumedEntireMessage(); -#endif -} - -inline bool InlineMergeFromArray(const void* data, int size, - MessageLite* message, bool aliasing = false) { - if (!InlineMergePartialFromArray(data, size, message, aliasing)) return false; - if (!message->IsInitialized()) { - GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *message); - return false; - } - return true; +template +bool MergePartialFromImpl(StringPiece input, MessageLite* msg) { + 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). + return ptr && ctx.EndedAtLimit(); +} + +template +bool MergePartialFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg) { + 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) + return ptr && ctx.EndedAtEndOfStream(); +} + +template +bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg) { + 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); + return ctx.EndedAtLimit(); +} + +#else // !GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER + +inline bool InlineMergePartialEntireStream(io::CodedInputStream* cis, + MessageLite* message, + bool aliasing) { + return message->MergePartialFromCodedStream(cis) && + cis->ConsumedEntireMessage(); +} + +template +bool MergePartialFromImpl(StringPiece input, MessageLite* msg) { + io::CodedInputStream decoder(reinterpret_cast(input.data()), + input.size()); + return InlineMergePartialEntireStream(&decoder, msg, aliasing); +} + +template +bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg) { + io::CodedInputStream decoder(input.zcis); + decoder.PushLimit(input.limit); + return InlineMergePartialEntireStream(&decoder, msg, aliasing) && + decoder.BytesUntilLimit() == 0; } -inline bool InlineParsePartialFromArray(const void* data, int size, - MessageLite* message, - bool aliasing = false) { - message->Clear(); - return InlineMergePartialFromArray(data, size, message, aliasing); +template +bool MergePartialFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg) { + io::CodedInputStream decoder(input); + return InlineMergePartialEntireStream(&decoder, msg, aliasing); } -inline bool InlineParseFromArray(const void* data, int size, - MessageLite* message, bool aliasing = false) { - if (!InlineParsePartialFromArray(data, size, message, aliasing)) return false; - if (!message->IsInitialized()) { - GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *message); - return false; - } - return true; -} +#endif // !GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -} // namespace +template bool MergePartialFromImpl(StringPiece input, + MessageLite* msg); +template bool MergePartialFromImpl(StringPiece input, + MessageLite* msg); +template bool MergePartialFromImpl(io::ZeroCopyInputStream* input, + MessageLite* msg); +template bool MergePartialFromImpl(io::ZeroCopyInputStream* input, + MessageLite* msg); +template bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg); +template bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg); + +} // namespace internal MessageLite* MessageLite::New(Arena* arena) const { MessageLite* message = New(); @@ -400,75 +212,165 @@ MessageLite* MessageLite::New(Arena* arena) const { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER +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 ByteCount() const final { return 0; } + + bool aliasing_enabled() { return cis_->aliasing_enabled_; } + + private: + io::CodedInputStream* cis_; +}; + bool MessageLite::MergePartialFromCodedStream(io::CodedInputStream* input) { - return InlineMergePartialFromCodedStream(input, this); + 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()); + return true; + } + input->SetConsumed(); + return true; } -#endif +#endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool MessageLite::MergeFromCodedStream(io::CodedInputStream* input) { - return InlineMergeFromCodedStream(input, this); + return MergePartialFromCodedStream(input) && IsInitializedWithErrors(); } bool MessageLite::ParseFromCodedStream(io::CodedInputStream* input) { - return InlineParseFromCodedStream(input, this); + Clear(); + return MergeFromCodedStream(input); } bool MessageLite::ParsePartialFromCodedStream(io::CodedInputStream* input) { - return InlineParsePartialFromCodedStream(input, this); + Clear(); + return MergePartialFromCodedStream(input); } bool MessageLite::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) { - io::CodedInputStream decoder(input); - return ParseFromCodedStream(&decoder) && decoder.ConsumedEntireMessage(); + return ParseFrom(input); } bool MessageLite::ParsePartialFromZeroCopyStream( io::ZeroCopyInputStream* input) { - io::CodedInputStream decoder(input); - return ParsePartialFromCodedStream(&decoder) && - decoder.ConsumedEntireMessage(); + return ParseFrom(input); +} + +bool MessageLite::ParseFromFileDescriptor(int file_descriptor) { + io::FileInputStream input(file_descriptor); + return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0; } -bool MessageLite::ParseFromBoundedZeroCopyStream( +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) { - io::CodedInputStream decoder(input); - decoder.PushLimit(size); - return ParseFromCodedStream(&decoder) && - decoder.ConsumedEntireMessage() && - decoder.BytesUntilLimit() == 0; + 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) { - io::CodedInputStream decoder(input); - decoder.PushLimit(size); - return ParsePartialFromCodedStream(&decoder) && - decoder.ConsumedEntireMessage() && - decoder.BytesUntilLimit() == 0; + return ParseFrom(internal::BoundedZCIS{input, size}); } -bool MessageLite::ParseFromString(const string& data) { - return InlineParseFromArray(data.data(), data.size(), this); +bool MessageLite::ParseFromString(const std::string& data) { + return ParseFrom(data); } -bool MessageLite::ParsePartialFromString(const string& data) { - return InlineParsePartialFromArray(data.data(), data.size(), this); +bool MessageLite::ParsePartialFromString(const std::string& data) { + return ParseFrom(data); } bool MessageLite::ParseFromArray(const void* data, int size) { - return InlineParseFromArray(data, size, this); + return ParseFrom(as_string_view(data, size)); } bool MessageLite::ParsePartialFromArray(const void* data, int size) { - return InlineParsePartialFromArray(data, size, this); + return ParseFrom(as_string_view(data, size)); +} + +bool MessageLite::MergeFromString(const std::string& 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.InternalSerializeWithCachedSizesToArray(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.InternalSerializeWithCachedSizesToArray(target, &out); + GOOGLE_DCHECK(target + size == res); + return res; + } +} + uint8* MessageLite::SerializeWithCachedSizesToArray(uint8* target) const { - return InternalSerializeWithCachedSizesToArray( - io::CodedOutputStream::IsDefaultSerializationDeterministic(), target); + // 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 { @@ -480,78 +382,101 @@ 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; + GOOGLE_LOG(ERROR) << GetTypeName() + << " exceeded maximum protobuf size of 2GB: " << size; return false; } - uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size); - if (buffer != NULL) { - uint8* end = InternalSerializeWithCachedSizesToArray( - output->IsSerializationDeterministic(), buffer); - if (end - buffer != size) { - ByteSizeConsistencyError(size, ByteSizeLong(), end - buffer, *this); - } - return true; - } else { - 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); - } + int original_byte_count = output->ByteCount(); + SerializeWithCachedSizes(output); + if (output->HadError()) { + return false; + } + int final_byte_count = output->ByteCount(); - return true; + 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 { - io::CodedOutputStream encoder(output); - return SerializeToCodedStream(&encoder); + GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); + return SerializePartialToZeroCopyStream(output); } bool MessageLite::SerializePartialToZeroCopyStream( io::ZeroCopyOutputStream* output) const { - io::CodedOutputStream encoder(output); - return SerializePartialToCodedStream(&encoder); + 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 = InternalSerializeWithCachedSizesToArray(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::AppendToString(string* output) const { +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(string* output) const { +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; + 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); - uint8* end = SerializeWithCachedSizesToArray(start); - if (end - start != byte_size) { - ByteSizeConsistencyError(byte_size, ByteSizeLong(), end - start, *this); - } + SerializeToArrayImpl(*this, start, byte_size); return true; } -bool MessageLite::SerializeToString(string* output) const { +bool MessageLite::SerializeToString(std::string* output) const { output->clear(); return AppendToString(output); } -bool MessageLite::SerializePartialToString(string* output) const { +bool MessageLite::SerializePartialToString(std::string* output) const { output->clear(); return AppendPartialToString(output); } @@ -564,69 +489,32 @@ bool MessageLite::SerializeToArray(void* data, int size) const { 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; + 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); - uint8* end = SerializeWithCachedSizesToArray(start); - if (end - start != byte_size) { - ByteSizeConsistencyError(byte_size, ByteSizeLong(), end - start, *this); - } + SerializeToArrayImpl(*this, start, byte_size); return true; } -string MessageLite::SerializeAsString() const { +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. - string output; - if (!AppendToString(&output)) - output.clear(); + std::string output; + if (!AppendToString(&output)) output.clear(); return output; } -string MessageLite::SerializePartialAsString() const { - string output; - if (!AppendPartialToString(&output)) - output.clear(); +std::string MessageLite::SerializePartialAsString() const { + std::string output; + if (!AppendPartialToString(&output)) output.clear(); return output; } -void MessageLite::SerializeWithCachedSizes( - io::CodedOutputStream* output) const { - GOOGLE_DCHECK(InternalGetTable()); - internal::TableSerialize( - *this, - static_cast(InternalGetTable()), - output); -} - -// The table driven code optimizes the case that the CodedOutputStream buffer -// is large enough to serialize into it directly. -// If the proto is optimized for speed, this method will be overridden by -// generated code for maximum speed. If the proto is optimized for size or -// is lite, then we need to specialize this to avoid infinite recursion. -uint8* MessageLite::InternalSerializeWithCachedSizesToArray( - bool deterministic, uint8* target) const { - const internal::SerializationTable* table = - static_cast(InternalGetTable()); - if (table == NULL) { - // We only optimize this when using optimize_for = SPEED. In other cases - // we just use the CodedOutputStream path. - int size = GetCachedSize(); - io::ArrayOutputStream out(target, size); - io::CodedOutputStream coded_out(&out); - coded_out.SetSerializationDeterministic(deterministic); - SerializeWithCachedSizes(&coded_out); - GOOGLE_CHECK(!coded_out.HadError()); - return target + size; - } else { - return internal::TableSerializeToArray(*this, table, deterministic, target); - } -} namespace internal { @@ -640,9 +528,9 @@ void GenericTypeHandler::Merge(const MessageLite& from, MessageLite* to) { to->CheckTypeAndMergeFrom(from); } -template<> -void GenericTypeHandler::Merge(const string& from, - string* to) { +template <> +void GenericTypeHandler::Merge(const std::string& from, + std::string* to) { *to = from; } diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index 21d5491558..949e200e95 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -41,11 +41,14 @@ #include #include + #include #include +#include #include #include #include +#include #include @@ -70,12 +73,8 @@ class ZeroCopyOutputStream; } // namespace io namespace internal { -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER // See parse_context.h for explanation class ParseContext; -typedef const char* (*ParseFunc)(const char* ptr, const char* end, void* object, - ParseContext* ctx); -#endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER class RepeatedPtrFieldBase; class WireFormatLite; @@ -116,22 +115,23 @@ inline int ToIntSize(size_t size) { // // Pay special attention to the initialization state of the object. // 1. The object is "uninitialized" to begin with. -// 2. Call DefaultConstruct() only if the object is uninitialized. -// After the call, the object becomes "initialized". +// 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(); - } + void DefaultConstruct() { new (&union_) T(); } - void Destruct() { - get_mutable()->~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_); } @@ -146,15 +146,15 @@ class ExplicitlyConstructed { // Default empty string object. Don't use this directly. Instead, call // GetEmptyString() to get the reference. -LIBPROTOBUF_EXPORT extern ExplicitlyConstructed<::std::string> +PROTOBUF_EXPORT extern ExplicitlyConstructed fixed_address_empty_string; -LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyStringAlreadyInited() { +PROTOBUF_EXPORT inline const std::string& GetEmptyStringAlreadyInited() { return fixed_address_empty_string.get(); } -LIBPROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const string& str); +PROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const std::string& str); } // namespace internal @@ -181,7 +181,7 @@ LIBPROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const string& str); // 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. -class LIBPROTOBUF_EXPORT MessageLite { +class PROTOBUF_EXPORT MessageLite { public: inline MessageLite() {} virtual ~MessageLite() {} @@ -189,7 +189,7 @@ class LIBPROTOBUF_EXPORT MessageLite { // Basic Operations ------------------------------------------------ // Get the name of this message type, e.g. "foo.bar.BazProto". - virtual string GetTypeName() const = 0; + virtual std::string GetTypeName() const = 0; // Construct a new instance of the same type. Ownership is passed to the // caller. @@ -230,12 +230,27 @@ class LIBPROTOBUF_EXPORT MessageLite { // 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 string InitializationErrorString() const; + 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 @@ -256,9 +271,26 @@ class LIBPROTOBUF_EXPORT MessageLite { // Like ParseFromZeroCopyStream(), but accepts messages that are missing // required fields. bool ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream* input); + // Parse a protocol buffer from a file descriptor. If successful, the entire + // input will be consumed. + bool ParseFromFileDescriptor(int file_descriptor); + // Like ParseFromFileDescriptor(), but accepts messages that are missing + // required fields. + bool ParsePartialFromFileDescriptor(int file_descriptor); + // Parse a protocol buffer from a C++ istream. If successful, the entire + // input will be consumed. + bool ParseFromIstream(std::istream* input); + // Like ParseFromIstream(), but accepts messages that are missing + // required fields. + 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); bool ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size); // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are // missing required fields. @@ -269,10 +301,10 @@ class LIBPROTOBUF_EXPORT MessageLite { // 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(). - bool ParseFromString(const string& data); + bool ParseFromString(const std::string& data); // Like ParseFromString(), but accepts messages that are missing // required fields. - bool ParsePartialFromString(const string& data); + bool ParsePartialFromString(const std::string& data); // Parse a protocol buffer contained in an array of bytes. bool ParseFromArray(const void* data, int size); // Like ParseFromArray(), but accepts messages that are missing @@ -289,7 +321,7 @@ class LIBPROTOBUF_EXPORT MessageLite { // (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 + // ParseFromCodedStream() is implemented as Clear() followed by // MergeFromCodedStream(). bool MergeFromCodedStream(io::CodedInputStream* input); @@ -299,13 +331,13 @@ class LIBPROTOBUF_EXPORT MessageLite { // MergeFromCodedStream() is just implemented as MergePartialFromCodedStream() // followed by IsInitialized(). #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - virtual bool MergePartialFromCodedStream(io::CodedInputStream* input); + bool MergePartialFromCodedStream(io::CodedInputStream* input); #else virtual bool MergePartialFromCodedStream(io::CodedInputStream* input) = 0; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER // Merge a protocol buffer contained in a string. - bool MergeFromString(const string& data); + bool MergeFromString(const std::string& data); // Serialization --------------------------------------------------- @@ -325,9 +357,9 @@ class LIBPROTOBUF_EXPORT MessageLite { bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const; // Serialize the message and store it in the given string. All required // fields must be set. - bool SerializeToString(string* output) const; + bool SerializeToString(std::string* output) const; // Like SerializeToString(), but allows missing required fields. - bool SerializePartialToString(string* output) const; + 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; @@ -340,15 +372,27 @@ class LIBPROTOBUF_EXPORT MessageLite { // 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(). - string SerializeAsString() const; + std::string SerializeAsString() const; // Like SerializeAsString(), but allows missing required fields. - string SerializePartialAsString() const; - - // Like SerializeToString(), but appends to the data to the string's existing - // contents. All required fields must be set. - bool AppendToString(string* output) const; + 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(string* output) const; + bool AppendPartialToString(std::string* output) const; + // Computes the serialized size of the message. This recursively calls // ByteSizeLong() on all embedded messages. @@ -358,16 +402,16 @@ class LIBPROTOBUF_EXPORT MessageLite { virtual size_t ByteSizeLong() const = 0; // Legacy ByteSize() API. - GOOGLE_PROTOBUF_DEPRECATED_MSG("Please use ByteSizeLong() instead") - int ByteSize() const { - return internal::ToIntSize(ByteSizeLong()); - } + 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. - virtual void SerializeWithCachedSizes( - io::CodedOutputStream* output) const; + void SerializeWithCachedSizes(io::CodedOutputStream* output) const { + output->SetCur(InternalSerializeWithCachedSizesToArray(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 @@ -379,7 +423,7 @@ class LIBPROTOBUF_EXPORT MessageLite { // 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(). - virtual uint8* SerializeWithCachedSizesToArray(uint8* target) const; + 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 @@ -394,11 +438,9 @@ class LIBPROTOBUF_EXPORT MessageLite { // method.) virtual int GetCachedSize() const = 0; - virtual uint8* InternalSerializeWithCachedSizesToArray(bool deterministic, - uint8* target) const; - #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - virtual internal::ParseFunc _ParseFunc() const { + virtual const char* _InternalParse(const char* ptr, + internal::ParseContext* ctx) { return nullptr; } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER @@ -424,6 +466,26 @@ class LIBPROTOBUF_EXPORT MessageLite { return Arena::CreateMaybeMessage(arena); } + 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* InternalSerializeWithCachedSizesToArray(uint8* ptr) const; + virtual uint8* InternalSerializeWithCachedSizesToArray( + uint8* ptr, io::EpsCopyOutputStream* stream) const = 0; + private: // TODO(gerbens) make this a pure abstract function virtual const void* InternalGetTable() const { return NULL; } @@ -432,9 +494,63 @@ class LIBPROTOBUF_EXPORT MessageLite { friend class Message; friend class internal::WeakFieldMap; + bool IsInitializedWithErrors() const { + if (IsInitialized()) return true; + LogInitializationErrorMessage(); + return false; + } + + void LogInitializationErrorMessage() const; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite); }; +namespace internal { + +template +bool MergePartialFromImpl(StringPiece input, MessageLite* msg); +extern template bool MergePartialFromImpl(StringPiece input, + MessageLite* msg); +extern template bool MergePartialFromImpl(StringPiece input, + MessageLite* msg); + +template +bool MergePartialFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg); +extern template bool MergePartialFromImpl(io::ZeroCopyInputStream* input, + MessageLite* msg); +extern template bool MergePartialFromImpl(io::ZeroCopyInputStream* input, + MessageLite* msg); + +struct BoundedZCIS { + io::ZeroCopyInputStream* zcis; + int limit; +}; + +template +bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg); +extern template bool MergePartialFromImpl(BoundedZCIS input, + MessageLite* msg); +extern template bool MergePartialFromImpl(BoundedZCIS input, + MessageLite* msg); + +template +struct SourceWrapper; + +template +bool MergePartialFromImpl(const SourceWrapper& input, MessageLite* msg) { + return input.template MergePartialInto(msg); +} + +} // namespace internal + +template +bool MessageLite::ParseFrom(const T& input) { + if (flags & kParse) Clear(); + constexpr bool alias = flags & kMergeWithAliasing; + bool res = internal::MergePartialFromImpl(input, this); + return res && ((flags & kMergePartial) || IsInitializedWithErrors()); +} + } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/message_unittest.inc b/src/google/protobuf/message_unittest.inc index 0b9d565fdd..e92171f624 100644 --- a/src/google/protobuf/message_unittest.inc +++ b/src/google/protobuf/message_unittest.inc @@ -46,7 +46,11 @@ #include #include +#include +#include +#include #include +#include #include #include #include @@ -54,13 +58,9 @@ #include #include #include - -#include -#include -#include #include #include -#include + namespace google { namespace protobuf { @@ -68,8 +68,8 @@ namespace protobuf { #if defined(_WIN32) // DO NOT include , instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::open; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::open; #endif #ifndef O_BINARY @@ -88,8 +88,8 @@ TEST(MESSAGE_TEST_NAME, SerializeHelpers) { TestUtil::SetAllFields(&message); std::stringstream stream; - string str1("foo"); - string str2("bar"); + std::string str1("foo"); + std::string str2("bar"); EXPECT_TRUE(message.SerializeToString(&str1)); EXPECT_TRUE(message.AppendToString(&str2)); @@ -102,7 +102,7 @@ TEST(MESSAGE_TEST_NAME, SerializeHelpers) { EXPECT_TRUE(str2.substr(3) == str1); // GCC gives some sort of error if we try to just do stream.str() == str1. - string temp = stream.str(); + std::string temp = stream.str(); EXPECT_TRUE(temp == str1); EXPECT_TRUE(message.SerializeAsString() == str1); @@ -118,7 +118,7 @@ TEST(MESSAGE_TEST_NAME, SerializeToBrokenOstream) { } TEST(MESSAGE_TEST_NAME, ParseFromFileDescriptor) { - string filename = + std::string filename = TestUtil::GetTestDataPath("net/proto2/internal/testdata/golden_message"); int file = open(filename.c_str(), O_RDONLY | O_BINARY); ASSERT_GE(file, 0); @@ -131,7 +131,7 @@ TEST(MESSAGE_TEST_NAME, ParseFromFileDescriptor) { } TEST(MESSAGE_TEST_NAME, ParsePackedFromFileDescriptor) { - string filename = TestUtil::GetTestDataPath( + std::string filename = TestUtil::GetTestDataPath( "net/proto2/internal/testdata/golden_packed_fields_message"); int file = open(filename.c_str(), O_RDONLY | O_BINARY); ASSERT_GE(file, 0); @@ -146,7 +146,7 @@ TEST(MESSAGE_TEST_NAME, ParsePackedFromFileDescriptor) { TEST(MESSAGE_TEST_NAME, ParseHelpers) { // TODO(kenton): Test more helpers? They're all two-liners so it seems // like a waste of time. - string data; + std::string data; { // Set up. @@ -173,7 +173,7 @@ TEST(MESSAGE_TEST_NAME, ParseHelpers) { { // Test ParseFromBoundedZeroCopyStream. - string data_with_junk(data); + std::string data_with_junk(data); data_with_junk.append("some junk on the end"); io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size()); UNITTEST::TestAllTypes message; @@ -193,7 +193,7 @@ TEST(MESSAGE_TEST_NAME, ParseHelpers) { TEST(MESSAGE_TEST_NAME, ParseFailsIfNotInitialized) { UNITTEST::TestRequired message; - std::vector errors; + std::vector errors; { ScopedMemoryLog log; @@ -203,7 +203,7 @@ TEST(MESSAGE_TEST_NAME, ParseFailsIfNotInitialized) { ASSERT_EQ(1, errors.size()); EXPECT_EQ( - "Can't parse message of type \"" + string(UNITTEST_PACKAGE_NAME) + + "Can't parse message of type \"" + std::string(UNITTEST_PACKAGE_NAME) + ".TestRequired\" because it is missing required fields: a, b, c", errors[0]); } @@ -241,10 +241,10 @@ TEST(MESSAGE_TEST_NAME, DynamicCastToGenerated) { TEST(MESSAGE_TEST_NAME, SerializeFailsIfNotInitialized) { UNITTEST::TestRequired message; - string data; + std::string data; EXPECT_DEBUG_DEATH(EXPECT_TRUE(message.SerializeToString(&data)), "Can't serialize message of type \"" + - string(UNITTEST_PACKAGE_NAME) + + std::string(UNITTEST_PACKAGE_NAME) + ".TestRequired\" because " "it is missing required fields: a, b, c"); } @@ -252,8 +252,7 @@ TEST(MESSAGE_TEST_NAME, SerializeFailsIfNotInitialized) { TEST(MESSAGE_TEST_NAME, CheckInitialized) { UNITTEST::TestRequired message; EXPECT_DEATH(message.CheckInitialized(), - "Message of type \"" + - string(UNITTEST_PACKAGE_NAME) + + "Message of type \"" + std::string(UNITTEST_PACKAGE_NAME) + ".TestRequired\" is missing required " "fields: a, b, c"); } @@ -261,12 +260,13 @@ TEST(MESSAGE_TEST_NAME, CheckInitialized) { #endif // PROTOBUF_HAS_DEATH_TEST namespace { -// An input stream that repeats a string's content for a number of times. It -// helps us create a really large input without consuming too much memory. Used -// to test the parsing behavior when the input size exceeds 2G or close to it. +// An input stream that repeats a std::string's content for a number of times. +// It helps us create a really large input without consuming too much memory. +// Used to test the parsing behavior when the input size exceeds 2G or close to +// it. class RepeatedInputStream : public io::ZeroCopyInputStream { public: - RepeatedInputStream(const string& data, size_t count) + RepeatedInputStream(const std::string& data, size_t count) : data_(data), count_(count), position_(0), total_byte_count_(0) {} virtual bool Next(const void** data, int* size) { @@ -308,22 +308,22 @@ class RepeatedInputStream : public io::ZeroCopyInputStream { virtual int64 ByteCount() const { return total_byte_count_; } private: - string data_; + std::string data_; size_t count_; // The number of strings that haven't been consuemd. - size_t position_; // Position in the string for the next read. + size_t position_; // Position in the std::string for the next read. int64 total_byte_count_; }; } // namespace TEST(MESSAGE_TEST_NAME, TestParseMessagesCloseTo2G) { - // Create a message with a large string field. - string value = string(64 * 1024 * 1024, 'x'); + // Create a message with a large std::string field. + std::string value = std::string(64 * 1024 * 1024, 'x'); UNITTEST::TestAllTypes message; message.set_optional_string(value); // Repeat this message in the input stream to make the total input size // close to 2G. - string data = message.SerializeAsString(); + std::string data = message.SerializeAsString(); size_t count = static_cast(kint32max) / data.size(); RepeatedInputStream input(data, count); @@ -337,14 +337,14 @@ TEST(MESSAGE_TEST_NAME, TestParseMessagesCloseTo2G) { } TEST(MESSAGE_TEST_NAME, TestParseMessagesOver2G) { - // Create a message with a large string field. - string value = string(64 * 1024 * 1024, 'x'); + // Create a message with a large std::string field. + std::string value = std::string(64 * 1024 * 1024, 'x'); UNITTEST::TestAllTypes message; message.set_optional_string(value); // Repeat this message in the input stream to make the total input size // larger than 2G. - string data = message.SerializeAsString(); + std::string data = message.SerializeAsString(); size_t count = static_cast(kint32max) / data.size() + 1; RepeatedInputStream input(data, count); @@ -362,7 +362,7 @@ TEST(MESSAGE_TEST_NAME, BypassInitializationCheckOnSerialize) { TEST(MESSAGE_TEST_NAME, FindInitializationErrors) { UNITTEST::TestRequired message; - std::vector errors; + std::vector errors; message.FindInitializationErrors(&errors); ASSERT_EQ(3, errors.size()); EXPECT_EQ("a", errors[0]); @@ -391,7 +391,7 @@ TEST(MESSAGE_TEST_NAME, MessageIsStillValidAfterParseFails) { UNITTEST::TestAllTypes message; // 9 0xFFs for the "optional_uint64" field. - string invalid_data = "\x20\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"; + std::string invalid_data = "\x20\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"; EXPECT_FALSE(message.ParseFromString(invalid_data)); message.Clear(); @@ -399,7 +399,7 @@ TEST(MESSAGE_TEST_NAME, MessageIsStillValidAfterParseFails) { // invalid data for field "optional_string". Length prefix is 1 but no // payload. - string invalid_string_data = "\x72\x01"; + std::string invalid_string_data = "\x72\x01"; { Arena arena; UNITTEST::TestAllTypes* arena_message = @@ -462,7 +462,7 @@ TEST(MESSAGE_TEST_NAME, ParsingMerge) { #undef ASSIGN_REPEATED_GROUP - string buffer; + std::string buffer; generator.SerializeToString(&buffer); UNITTEST::TestParsingMerge parsing_merge; parsing_merge.ParseFromString(buffer); @@ -567,6 +567,67 @@ TEST(MESSAGE_FACTORY_TEST_NAME, GeneratedFactoryUnknownType) { nullptr); } +TEST(MESSAGE_TEST_NAME, MOMIParserEdgeCases) { + { + UNITTEST::TestAllTypes msg; + // Parser ends in last 16 bytes of buffer due to a 0. + std::string data; + // 12 bytes of data + for (int i = 0; i < 4; i++) data += "\370\1\1"; + // 13 byte is terminator + data += '\0'; // Terminator + // followed by the rest of the stream + // space is ascii 32 so no end group + data += std::string(30, ' '); + io::ArrayInputStream zcis(data.data(), data.size(), 17); + io::CodedInputStream cis(&zcis); + EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis)); + EXPECT_EQ(cis.CurrentPosition(), 3 * 4 + 1); + } + { + // Parser ends in last 16 bytes of buffer due to a end-group. + // Must use a message that is a group. Otherwise ending on a group end is + // a failure. + UNITTEST::TestAllTypes::OptionalGroup msg; + std::string data; + for (int i = 0; i < 3; i++) data += "\370\1\1"; + data += '\14'; // Octal end-group tag 12 (1 * 8 + 4( + data += std::string(30, ' '); + io::ArrayInputStream zcis(data.data(), data.size(), 17); + io::CodedInputStream cis(&zcis); + EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis)); + EXPECT_EQ(cis.CurrentPosition(), 3 * 3 + 1); + EXPECT_TRUE(cis.LastTagWas(12)); + } + { + // Parser ends in last 16 bytes of buffer due to a end-group. But is inside + // a length delimited field. + // a failure. + UNITTEST::TestAllTypes::OptionalGroup msg; + std::string data; + data += "\22\3foo"; + data += '\14'; // Octal end-group tag 12 (1 * 8 + 4( + data += std::string(30, ' '); + io::ArrayInputStream zcis(data.data(), data.size(), 17); + io::CodedInputStream cis(&zcis); + EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis)); + EXPECT_EQ(cis.CurrentPosition(), 6); + EXPECT_TRUE(cis.LastTagWas(12)); + } + { + // Parser fails when ending on 0 if from ZeroCopyInputStream + UNITTEST::TestAllTypes msg; + std::string data; + // 12 bytes of data + for (int i = 0; i < 4; i++) data += "\370\1\1"; + // 13 byte is terminator + data += '\0'; // Terminator + data += std::string(30, ' '); + io::ArrayInputStream zcis(data.data(), data.size(), 17); + EXPECT_FALSE(msg.ParsePartialFromZeroCopyStream(&zcis)); + } +} + } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/metadata.h b/src/google/protobuf/metadata.h index 928bbf5c0d..009a7e93a6 100644 --- a/src/google/protobuf/metadata.h +++ b/src/google/protobuf/metadata.h @@ -56,19 +56,15 @@ class InternalMetadataWithArena InternalMetadataWithArena() {} explicit InternalMetadataWithArena(Arena* arena) : InternalMetadataWithArenaBase(arena) {} + InternalMetadataWithArena>(arena) {} - void DoSwap(UnknownFieldSet* other) { - mutable_unknown_fields()->Swap(other); - } + void DoSwap(UnknownFieldSet* other) { mutable_unknown_fields()->Swap(other); } void DoMergeFrom(const UnknownFieldSet& other) { mutable_unknown_fields()->MergeFrom(other); } - void DoClear() { - mutable_unknown_fields()->Clear(); - } + void DoClear() { mutable_unknown_fields()->Clear(); } static const UnknownFieldSet& default_instance() { return *UnknownFieldSet::default_instance(); diff --git a/src/google/protobuf/metadata_lite.h b/src/google/protobuf/metadata_lite.h index 3c36a55334..40c7ea785d 100644 --- a/src/google/protobuf/metadata_lite.h +++ b/src/google/protobuf/metadata_lite.h @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -70,35 +71,35 @@ class InternalMetadataWithArenaBase { ptr_ = NULL; } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE const T& unknown_fields() const { - if (GOOGLE_PREDICT_FALSE(have_unknown_fields())) { + PROTOBUF_ALWAYS_INLINE const T& unknown_fields() const { + if (PROTOBUF_PREDICT_FALSE(have_unknown_fields())) { return PtrValue()->unknown_fields; } else { return Derived::default_instance(); } } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE T* mutable_unknown_fields() { - if (GOOGLE_PREDICT_TRUE(have_unknown_fields())) { + PROTOBUF_ALWAYS_INLINE T* mutable_unknown_fields() { + if (PROTOBUF_PREDICT_TRUE(have_unknown_fields())) { return &PtrValue()->unknown_fields; } else { return mutable_unknown_fields_slow(); } } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE Arena* arena() const { - if (GOOGLE_PREDICT_FALSE(have_unknown_fields())) { + PROTOBUF_ALWAYS_INLINE Arena* arena() const { + if (PROTOBUF_PREDICT_FALSE(have_unknown_fields())) { return PtrValue()->arena; } else { return PtrValue(); } } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool have_unknown_fields() const { + PROTOBUF_ALWAYS_INLINE bool have_unknown_fields() const { return PtrTag() == kTagContainer; } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void Swap(Derived* other) { + PROTOBUF_ALWAYS_INLINE void Swap(Derived* 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 @@ -110,22 +111,19 @@ class InternalMetadataWithArenaBase { } } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void MergeFrom( - const Derived& other) { + PROTOBUF_ALWAYS_INLINE void MergeFrom(const Derived& other) { if (other.have_unknown_fields()) { static_cast(this)->DoMergeFrom(other.unknown_fields()); } } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void Clear() { + PROTOBUF_ALWAYS_INLINE void Clear() { if (have_unknown_fields()) { static_cast(this)->DoClear(); } } - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void* raw_arena_ptr() const { - return ptr_; - } + PROTOBUF_ALWAYS_INLINE void* raw_arena_ptr() const { return ptr_; } private: void* ptr_; @@ -141,13 +139,14 @@ class InternalMetadataWithArenaBase { static const intptr_t kPtrValueMask = ~kPtrTagMask; // Accessors for pointer tag and pointer value. - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE int PtrTag() const { + PROTOBUF_ALWAYS_INLINE int PtrTag() const { return reinterpret_cast(ptr_) & kPtrTagMask; } - template U* PtrValue() const { - return reinterpret_cast( - reinterpret_cast(ptr_) & kPtrValueMask); + 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. @@ -156,58 +155,60 @@ class InternalMetadataWithArenaBase { Arena* arena; }; - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE T* mutable_unknown_fields_slow() { + 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); + ptr_ = reinterpret_cast(reinterpret_cast(ptr_) | + kTagContainer); container->arena = my_arena; return &(container->unknown_fields); } }; -// We store unknown fields as a string right now, because there is currently no -// good interface for reading unknown fields into an ArenaString. We may want -// to revisit this to allow unknown fields to be parsed onto the Arena. +// We store unknown fields as a std::string right now, because there is +// currently no good interface for reading unknown fields into an ArenaString. +// We may want to revisit this to allow unknown fields to be parsed onto the +// Arena. class InternalMetadataWithArenaLite - : public InternalMetadataWithArenaBase { public: InternalMetadataWithArenaLite() {} explicit InternalMetadataWithArenaLite(Arena* arena) - : InternalMetadataWithArenaBase(arena) {} - void DoSwap(string* other) { - mutable_unknown_fields()->swap(*other); - } + void DoSwap(std::string* other) { mutable_unknown_fields()->swap(*other); } - void DoMergeFrom(const string& other) { + void DoMergeFrom(const std::string& other) { mutable_unknown_fields()->append(other); } - void DoClear() { - mutable_unknown_fields()->clear(); - } + void DoClear() { mutable_unknown_fields()->clear(); } - static const string& default_instance() { - return GetEmptyStringAlreadyInited(); + static const std::string& default_instance() { + // Can't use GetEmptyStringAlreadyInited() here because empty string + // may not have been initalized yet. This happens when protocol compiler + // statically determines the user can't access defaults and omits init code + // from proto constructors. However unknown fields are always part of a + // proto so it needs to be lazily initailzed. See b/112613846. + return GetEmptyString(); } }; // 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 +// 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 LIBPROTOBUF_EXPORT LiteUnknownFieldSetter { +class PROTOBUF_EXPORT LiteUnknownFieldSetter { public: explicit LiteUnknownFieldSetter(InternalMetadataWithArenaLite* metadata) : metadata_(metadata) { @@ -218,11 +219,11 @@ class LIBPROTOBUF_EXPORT LiteUnknownFieldSetter { ~LiteUnknownFieldSetter() { if (!buffer_.empty()) metadata_->mutable_unknown_fields()->swap(buffer_); } - string* buffer() { return &buffer_; } + std::string* buffer() { return &buffer_; } private: InternalMetadataWithArenaLite* metadata_; - string buffer_; + std::string buffer_; }; } // namespace internal diff --git a/src/google/protobuf/no_field_presence_test.cc b/src/google/protobuf/no_field_presence_test.cc index d212fb110d..8b05277f0a 100644 --- a/src/google/protobuf/no_field_presence_test.cc +++ b/src/google/protobuf/no_field_presence_test.cc @@ -30,8 +30,8 @@ #include -#include #include +#include #include #include #include @@ -120,8 +120,7 @@ void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) { m->mutable_optional_proto2_message()->set_optional_int32(44); m->set_optional_nested_enum( proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ); - m->set_optional_foreign_enum( - proto2_nofieldpresence_unittest::FOREIGN_BAZ); + m->set_optional_foreign_enum(proto2_nofieldpresence_unittest::FOREIGN_BAZ); m->mutable_optional_lazy_message()->set_bb(45); m->add_repeated_int32(100); m->add_repeated_int64(101); @@ -143,8 +142,7 @@ void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) { m->add_repeated_proto2_message()->set_optional_int32(48); m->add_repeated_nested_enum( proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ); - m->add_repeated_foreign_enum( - proto2_nofieldpresence_unittest::FOREIGN_BAZ); + m->add_repeated_foreign_enum(proto2_nofieldpresence_unittest::FOREIGN_BAZ); m->add_repeated_lazy_message()->set_bb(49); m->set_oneof_uint32(1); @@ -153,7 +151,7 @@ void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) { } void CheckNonDefaultValues( -const proto2_nofieldpresence_unittest::TestAllTypes& m) { + const proto2_nofieldpresence_unittest::TestAllTypes& m) { EXPECT_EQ(100, m.optional_int32()); EXPECT_EQ(101, m.optional_int64()); EXPECT_EQ(102, m.optional_uint32()); @@ -271,8 +269,9 @@ TEST(NoFieldPresenceTest, MessageFieldPresenceTest) { EXPECT_EQ(false, message.has_optional_lazy_message()); // Test field presence of a message field on the default instance. - EXPECT_EQ(false, proto2_nofieldpresence_unittest::TestAllTypes:: - default_instance().has_optional_nested_message()); + EXPECT_EQ(false, + proto2_nofieldpresence_unittest::TestAllTypes::default_instance() + .has_optional_nested_message()); } TEST(NoFieldPresenceTest, ReflectionHasFieldTest) { @@ -294,9 +293,11 @@ TEST(NoFieldPresenceTest, ReflectionHasFieldTest) { // Test field presence of a message field on the default instance. const FieldDescriptor* msg_field = desc->FindFieldByName("optional_nested_message"); - EXPECT_EQ(false, r->HasField( - proto2_nofieldpresence_unittest::TestAllTypes:: - default_instance(), msg_field)); + EXPECT_EQ( + false, + r->HasField( + proto2_nofieldpresence_unittest::TestAllTypes::default_instance(), + msg_field)); // Fill all fields, expect everything to report true (check oneofs below). FillValues(&message); @@ -418,7 +419,7 @@ TEST(NoFieldPresenceTest, DontSerializeDefaultValuesTest) { // check that serialized data contains only non-zero numeric fields/non-empty // string/byte fields. proto2_nofieldpresence_unittest::TestAllTypes message; - string output; + std::string output; // All default values -> no output. message.SerializeToString(&output); @@ -441,7 +442,8 @@ TEST(NoFieldPresenceTest, DontSerializeDefaultValuesTest) { message.set_optional_string(""); message.set_optional_bytes(""); message.set_optional_nested_enum( - proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO); // first enum entry + proto2_nofieldpresence_unittest:: + TestAllTypes_NestedEnum_FOO); // first enum entry message.set_optional_foreign_enum( proto2_nofieldpresence_unittest::FOREIGN_FOO); // first enum entry @@ -510,7 +512,7 @@ TEST(NoFieldPresenceTest, LazyMessageFieldHasBit) { // Serialize and parse with a new message object so that lazy field on new // object is in unparsed state. - string output; + std::string output; message.SerializeToString(&output); proto2_nofieldpresence_unittest::TestAllTypes message2; message2.ParseFromString(output); @@ -529,7 +531,7 @@ TEST(NoFieldPresenceTest, OneofPresence) { // oneof fields still have field presence -- ensure that this goes on the wire // even though its value is the empty string. message.set_oneof_string(""); - string serialized; + std::string serialized; message.SerializeToString(&serialized); // Tag: 113 --> tag is (113 << 3) | 2 (length delimited) = 906 // varint: 0x8a 0x07 @@ -554,8 +556,9 @@ TEST(NoFieldPresenceTest, OneofPresence) { message.oneof_field_case()); message.Clear(); - message.set_oneof_enum(proto2_nofieldpresence_unittest:: - TestAllTypes_NestedEnum_FOO); // default value. + message.set_oneof_enum( + proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO); // default + // value. message.SerializeToString(&serialized); EXPECT_EQ(3, serialized.size()); EXPECT_TRUE(message.ParseFromString(serialized)); @@ -571,4 +574,3 @@ TEST(NoFieldPresenceTest, OneofPresence) { } // namespace } // namespace protobuf } // namespace google - diff --git a/src/google/protobuf/parse_context.cc b/src/google/protobuf/parse_context.cc new file mode 100644 index 0000000000..d514079e9f --- /dev/null +++ b/src/google/protobuf/parse_context.cc @@ -0,0 +1,559 @@ +// 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 d) { + 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 = ParseVarint64(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 + d++; + break; + } + case 4: { // end group + if (--d < 0) return true; // We exit early + break; + } + case 5: { // fixed32 + ptr += 4; + break; + } + default: + return false; // Unknown wireformat + } + } + return false; +} + +} // namespace + +const char* EpsCopyInputStream::Next(int overrun, int d) { + 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 && + (d < 0 || !ParseEndsInSlopRegion(buffer_, overrun, d))) { + const void* data; + // ZeroCopyInputStream indicates Next may return 0 size buffers. Hence + // we loop. + while (zcis_->Next(&data, &size_)) { + overall_limit_ -= size_; + 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_; +} + +std::pair EpsCopyInputStream::DoneFallback(const char* ptr, + int d) { + GOOGLE_DCHECK(ptr >= limit_end_); + int overrun = ptr - buffer_end_; + GOOGLE_DCHECK(overrun <= kSlopBytes); // Guaranteed by parse loop. + // 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 + do { + // We are past the end of buffer_end_, in the slop region. + GOOGLE_DCHECK(overrun >= 0); + auto p = Next(overrun, d); + 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_; + // Distinquish ending on a pushed limit or ending on end-of-stream. + SetEndOfStream(); + return {ptr, true}; + } + limit_ -= buffer_end_ - p; // Adjust limit_ relative to new anchor + ptr = p + overrun; + overrun = ptr - buffer_end_; + } while (overrun >= 0); + limit_end_ = buffer_end_ + std::min(0, limit_); + return {ptr, 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* s) { + s->clear(); + // TODO(gerbens) assess security. At the moment its parity with + // CodedInputStream but it allows a payload to reserve large memory. + if (PROTOBUF_PREDICT_TRUE(size <= buffer_end_ - ptr + limit_)) { + s->reserve(size); + } + return AppendStringFallback(ptr, size, s); +} + +const char* EpsCopyInputStream::AppendStringFallback(const char* ptr, int size, + std::string* str) { + // TODO(gerbens) assess security. At the moment its parity with + // CodedInputStream but it allows a payload to reserve large memory. + if (PROTOBUF_PREDICT_TRUE(size <= buffer_end_ - ptr + limit_)) { + str->reserve(size); + } + 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 +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); + std::memcpy(out->AddNAlreadyReserved(num), ptr, block_size); + ptr += block_size; + size -= block_size; + if (DoneWithCheck(&ptr, -1)) return nullptr; + 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); + std::memcpy(out->AddNAlreadyReserved(num), ptr, block_size); + 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_; +} + +#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER +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); +} +#endif + +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 ReadTagFallback(const char* p, uint32 res) { + for (std::uint32_t i = 0; i < 3; i++) { + std::uint32_t byte = static_cast(p[i]); + res += (byte - 1) << (7 * (i + 2)); + if (PROTOBUF_PREDICT_TRUE(byte < 128)) { + return {p + i + 1, res}; + } + } + return {nullptr, 0}; +} + +std::pair ParseVarint64Fallback(const char* p, + uint64 res) { + return ParseVarint64FallbackInline(p, res); +} + +std::pair ReadSizeFallback(const char* p, uint32 first) { + uint32 tmp; + auto res = VarintParse<4>(p + 1, &tmp); + if (tmp >= (1 << 24) - ParseContext::kSlopBytes) return {nullptr, 0}; + return {res, (tmp << 7) + first - 0x80}; +} + +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); +} + +const char* InlineGreedyStringParserUTF8(std::string* s, const char* ptr, + ParseContext* ctx, + const char* field_name) { + auto p = InlineGreedyStringParser(s, ptr, ctx); + GOOGLE_PROTOBUF_PARSER_ASSERT(VerifyUTF8(*s, field_name)); + return p; +} + + +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* PackedEnumParser(void* object, const char* ptr, ParseContext* ctx, + bool (*is_valid)(int), + InternalMetadataWithArenaLite* 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()); + } + }); +} + +const char* PackedEnumParserArg(void* object, const char* ptr, + ParseContext* ctx, + bool (*is_valid)(const void*, int), + const void* data, + InternalMetadataWithArenaLite* 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()); + } + }); +} + +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]; + std::memcpy(buffer, &value, 8); + 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]; + std::memcpy(buffer, &value, 4); + 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); +} + +const char* UnknownFieldParse(uint32 tag, + InternalMetadataWithArenaLite* metadata, + const char* ptr, ParseContext* ctx) { + return UnknownFieldParse(tag, metadata->mutable_unknown_fields(), ptr, ctx); +} + +} // namespace internal +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/parse_context.h b/src/google/protobuf/parse_context.h new file mode 100644 index 0000000000..72355f81cf --- /dev/null +++ b/src/google/protobuf/parse_context.h @@ -0,0 +1,760 @@ +// 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 + + +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 +// similarily 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) zcis_->BackUp(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 += 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_); + } + // 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; + // No need to fetch buffer if we ended on a limit in the slop region + if ((*ptr - buffer_end_) == limit_) return true; + auto res = DoneFallback(*ptr, 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.end() - kSlopBytes; + next_chunk_ = buffer_; + if (aliasing_ == kOnPatch) aliasing_ = kNoDelta; + return flat.begin(); + } else { + std::memcpy(buffer_, flat.begin(), 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) { + 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. + + std::pair DoneFallback(const char* ptr, int d); + const char* Next(int overrun, int d); + 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); + + 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); + append(ptr, chunk_size); + ptr += chunk_size; + size -= chunk_size; + // DoneFallBack asserts it isn't called when exactly on the limit. If this + // happens we fail the parse, as we are at the limit and still more bytes + // to read. + if (limit_ == kSlopBytes) return nullptr; + auto res = DoneFallback(ptr, -1); + if (res.second) return nullptr; // If done we passed the limit + ptr = res.first; + 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) { + while (!DoneWithCheck(&ptr, -1)) { + append(ptr, limit_end_ - ptr); + ptr = limit_end_; + } + return ptr; + } + + 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_); } + bool DoneNoSlopCheck(const char** ptr) { return DoneWithCheck(ptr, -1); } + + int depth() const { return depth_; } + + Data& data() { return data_; } + const Data& data() const { return data_; } + + template + PROTOBUF_MUST_USE_RESULT PROTOBUF_ALWAYS_INLINE 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 +T UnalignedLoad(const void* p) { + T res; + memcpy(&res, p, sizeof(T)); + return res; +} + +// TODO(gerbens) Experiment with best implementation. +// Clang unrolls loop and generating pretty good code on O2, gcc doesn't. +// Unclear if we want 64 bit parse loop unrolled, inlined or opaque function +// call. Hence experimentation is needed. +// Important guarantee is that it doesn't read more than size bytes from p. +template +PROTOBUF_MUST_USE_RESULT const char* VarintParse(const char* p, T* out) { + T res = 1; + for (int i = 0; i < size; i++) { + T byte = static_cast(p[i]); + res += (byte - 1) << (i * 7); + int j = i + 1; + if (PROTOBUF_PREDICT_TRUE(byte < 128)) { + *out = res; + return p + j; + } + } + *out = 0; + return nullptr; +} + +// 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(uint32 value, const char** 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 +} + +// Used for tags, could read up to 5 bytes which must be available. +// Caller must ensure its safe to call. + +std::pair ReadTagFallback(const char* p, uint32 res); + +inline const char* ReadTag(const char* p, uint32* out) { + 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 + 2, res); + *out = tmp.second; + return tmp.first; +} + +// Will preload the next 2 bytes +inline const char* ReadTag(const char* p, uint32* out, uint32* preload) { + uint32 res = static_cast(p[0]); + if (res < 128) { + *out = res; + *preload = UnalignedLoad(p + 1); + return p + 1; + } + uint32 second = static_cast(p[1]); + res += (second - 1) << 7; + if (second < 128) { + *out = res; + *preload = UnalignedLoad(p + 2); + return p + 2; + } + auto tmp = ReadTagFallback(p + 2, res); + *out = tmp.second; + return tmp.first; +} + +inline std::pair ParseVarint64FallbackInline(const char* p, + uint64 res) { + res >>= 1; + for (std::uint32_t i = 0; i < 4; i++) { + auto pnew = p + 2 * i; + auto tmp = DecodeTwoBytes(UnalignedLoad(pnew), &pnew); + res += (static_cast(tmp) - 2) << (14 * (i + 1) - 1); + if (PROTOBUF_PREDICT_TRUE(std::int16_t(tmp) >= 0)) { + return {pnew, res}; + } + } + return {nullptr, res}; +} + +inline const char* ParseVarint64Inline(const char* p, uint64* out) { + auto tmp = DecodeTwoBytes(UnalignedLoad(p), &p); + if (PROTOBUF_PREDICT_TRUE(static_cast(tmp) >= 0)) { + *out = tmp >> 1; + return p; + } + auto x = ParseVarint64FallbackInline(p, tmp); + *out = x.second; + return x.first; +} + +std::pair ParseVarint64Fallback(const char* p, uint64 res); + +inline const char* ParseVarint64(const char* p, uint32 preload, uint64* out) { + auto tmp = DecodeTwoBytes(preload, &p); + if (PROTOBUF_PREDICT_TRUE(static_cast(tmp) >= 0)) { + *out = tmp >> 1; + return p; + } + auto x = ParseVarint64Fallback(p, tmp); + *out = x.second; + return x.first; +} + +// Used for reading varint wiretype values, could read up to 10 bytes. +// Caller must ensure its safe to call. +inline const char* ParseVarint64(const char* p, uint64* out) { + return ParseVarint64(p, UnalignedLoad(p), out); +} + +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 ReadVarint(const char** p) { + uint64 tmp; + *p = ParseVarint64(*p, &tmp); + return tmp; +} + +inline int64 ReadVarintZigZag64(const char** p) { + uint64 tmp; + *p = ParseVarint64(*p, &tmp); + return WireFormatLite::ZigZagDecode64(tmp); +} + +inline int32 ReadVarintZigZag32(const char** p) { + uint64 tmp; + *p = ParseVarint64(*p, &tmp); + return WireFormatLite::ZigZagDecode32(static_cast(tmp)); +} + +inline uint64 ReadVarint(const char** p, uint32 preload) { + uint64 tmp; + *p = ParseVarint64(*p, preload, &tmp); + return tmp; +} + +inline int64 ReadVarintZigZag64(const char** p, uint32 preload) { + uint64 tmp; + *p = ParseVarint64(*p, preload, &tmp); + return WireFormatLite::ZigZagDecode64(tmp); +} + +inline int32 ReadVarintZigZag32(const char** p, uint32 preload) { + uint64 tmp; + *p = ParseVarint64(*p, preload, &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* EpsCopyInputStream::ReadPackedVarint(const char* ptr, Add add) { + int size = ReadSize(&ptr); + if (ptr == nullptr) return nullptr; + auto old = PushLimit(ptr, size); + if (old < 0) return nullptr; + while (!DoneWithCheck(&ptr, -1)) { + uint64 varint; + ptr = ParseVarint64(ptr, &varint); + if (!ptr) return nullptr; + add(varint); + } + if (!PopLimit(old)) return nullptr; + return ptr; +} + +// Helper for verification of utf8 +PROTOBUF_EXPORT +bool VerifyUTF8(StringPiece s, const char* 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); + +PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* +InlineGreedyStringParserUTF8(std::string* s, const char* ptr, ParseContext* ctx, + const char* field_name); +// Inline because we don't want to pay the price of field_name in opt mode. +inline PROTOBUF_MUST_USE_RESULT const char* InlineGreedyStringParserUTF8Verify( + std::string* s, const char* ptr, ParseContext* ctx, + const char* field_name) { + auto p = InlineGreedyStringParser(s, ptr, ctx); +#ifndef NDEBUG + VerifyUTF8(*s, field_name); +#endif // !NDEBUG + return p; +} + + +// 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 = ParseVarint64(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); +PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedEnumParser( + void* object, const char* ptr, ParseContext* ctx, bool (*is_valid)(int), + InternalMetadataWithArenaLite* metadata, int field_num); +PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedEnumParserArg( + void* object, const char* ptr, ParseContext* ctx, + bool (*is_valid)(const void*, int), const void* data, + InternalMetadataWithArenaLite* metadata, int field_num); + +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); +PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* UnknownFieldParse( + uint32 tag, InternalMetadataWithArenaLite* metadata, const char* ptr, + ParseContext* ctx); + +} // namespace internal +} // namespace protobuf +} // namespace google + +#include + +#endif // GOOGLE_PROTOBUF_PARSE_CONTEXT_H__ diff --git a/src/google/protobuf/port.h b/src/google/protobuf/port.h index 6345226595..555fd4ebc4 100644 --- a/src/google/protobuf/port.h +++ b/src/google/protobuf/port.h @@ -1,6 +1,43 @@ -#ifndef THIRD_PARTY_PROTOBUF_TESTING_PROTOBUF_SRC_GOOGLE_PROTOBUF_PORT_H_ -#define THIRD_PARTY_PROTOBUF_TESTING_PROTOBUF_SRC_GOOGLE_PROTOBUF_PORT_H_ +// 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 // THIRD_PARTY_PROTOBUF_TESTING_PROTOBUF_SRC_GOOGLE_PROTOBUF_PORT_H_ + +#endif // GOOGLE_PROTOBUF_PORT_H__ diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 3c119c1e15..17ab7ed83a 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -1,15 +1,364 @@ +// 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 + + +#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 */ +#define PROTOBUF_DEPRECATED +#define PROTOBUF_DEPRECATED_MSG(x) +#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 +#ifdef __GNUC__ +#define PROTOBUF_RETURNS_NONNULL __attribute__((returns_nonnull)) +#else +#define PROTOBUF_RETURNS_NONNULL +#endif +#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 3009000 +#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 3009000 +#define PROTOBUF_MIN_PROTOC_VERSION 3009000 +#define PROTOBUF_VERSION_SUFFIX "" + +// The minimum library version which works with the current version of the +// headers. +#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3009000 + #if defined(GOOGLE_PROTOBUF_NO_RTTI) && GOOGLE_PROTOBUF_NO_RTTI -#define GOOGLE_PROTOBUF_RTTI 0 +#define PROTOBUF_RTTI 0 #else -#define GOOGLE_PROTOBUF_RTTI 1 +#define PROTOBUF_RTTI 1 #endif -#define GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER 0 -#define GOOGLE_PROTOBUF_PREDICT_TRUE(x) GOOGLE_PREDICT_TRUE(x) -#define GOOGLE_PROTOBUF_PREDICT_FALSE(x) GOOGLE_PREDICT_FALSE(x) -#define GOOGLE_PROTOBUF_DEPRECATED -#define GOOGLE_PROTOBUF_DEPRECATED_MSG(x) -#define GOOGLE_PROTOBUF_LONGLONG(x) GOOGLE_LONGLONG(x) -#define GOOGLE_PROTOBUF_ULONGLONG(x) GOOGLE_ULONGLONG(x) +// 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") +#else +// 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(_MSC_VER) && defined(PROTOBUF_USE_DLLS) +#ifdef LIBPROTOBUF_EXPORTS +#define PROTOBUF_EXPORT __declspec(dllexport) +#else +#define PROTOBUF_EXPORT __declspec(dllimport) +#endif +#ifdef LIBPROTOC_EXPORTS +#define PROTOC_EXPORT __declspec(dllexport) +#else +#define PROTOC_EXPORT __declspec(dllimport) +#endif +#else +#define PROTOBUF_EXPORT +#define PROTOC_EXPORT +#endif + +// Windows declares several inconvenient macro names. We #undef them and then +// restore them in port_undef.inc. +#ifdef _MSC_VER +#pragma push_macro("GetMessage") +#undef GetMessage +#pragma push_macro("IGNORE") +#undef IGNORE +#pragma push_macro("IN") +#undef IN +#endif // _MSC_VER -#define GOOGLE_PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 3006000 +#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" +#endif diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc index 721241cfb5..7df776b5aa 100644 --- a/src/google/protobuf/port_undef.inc +++ b/src/google/protobuf/port_undef.inc @@ -1,9 +1,77 @@ -#undef GOOGLE_PROTOBUF_RTTI -#undef GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -#undef GOOGLE_PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC -#undef GOOGLE_PROTOBUF_PREDICT_TRUE -#undef GOOGLE_PROTOBUF_PREDICT_FALSE -#undef GOOGLE_PROTOBUF_DEPRECATED -#undef GOOGLE_PROTOBUF_DEPRECATED_MSG -#undef GOOGLE_PROTOBUF_LONGLONG -#undef GOOGLE_PROTOBUF_ULONGLONG +// 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_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 + +// Restore macro that may have been #undef'd in port_def.inc. +#ifdef _MSC_VER +#pragma pop_macro("GetMessage") +#pragma pop_macro("IGNORE") +#pragma pop_macro("IN") +#endif + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif diff --git a/src/google/protobuf/preserve_unknown_enum_test.cc b/src/google/protobuf/preserve_unknown_enum_test.cc index 71826c3815..1ac4e13fb7 100644 --- a/src/google/protobuf/preserve_unknown_enum_test.cc +++ b/src/google/protobuf/preserve_unknown_enum_test.cc @@ -31,8 +31,8 @@ #include #include #include -#include #include +#include #include namespace google { @@ -41,22 +41,18 @@ namespace { void FillMessage( proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra* message) { - message->set_e( - proto3_preserve_unknown_enum_unittest::E_EXTRA); - message->add_repeated_e( - proto3_preserve_unknown_enum_unittest::E_EXTRA); + message->set_e(proto3_preserve_unknown_enum_unittest::E_EXTRA); + message->add_repeated_e(proto3_preserve_unknown_enum_unittest::E_EXTRA); message->add_repeated_packed_e( proto3_preserve_unknown_enum_unittest::E_EXTRA); message->add_repeated_packed_unexpected_e( proto3_preserve_unknown_enum_unittest::E_EXTRA); - message->set_oneof_e_1( - proto3_preserve_unknown_enum_unittest::E_EXTRA); + message->set_oneof_e_1(proto3_preserve_unknown_enum_unittest::E_EXTRA); } void CheckMessage( const proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra& message) { - EXPECT_EQ(proto3_preserve_unknown_enum_unittest::E_EXTRA, - message.e()); + EXPECT_EQ(proto3_preserve_unknown_enum_unittest::E_EXTRA, message.e()); EXPECT_EQ(1, message.repeated_e_size()); EXPECT_EQ(proto3_preserve_unknown_enum_unittest::E_EXTRA, message.repeated_e(0)); @@ -72,23 +68,18 @@ void CheckMessage( void CheckMessage( const proto3_preserve_unknown_enum_unittest::MyMessage& message) { - EXPECT_EQ(static_cast( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast(message.e())); EXPECT_EQ(1, message.repeated_e_size()); - EXPECT_EQ(static_cast( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast(message.repeated_e(0))); EXPECT_EQ(1, message.repeated_packed_e_size()); - EXPECT_EQ(static_cast( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast(message.repeated_packed_e(0))); EXPECT_EQ(1, message.repeated_packed_unexpected_e_size()); - EXPECT_EQ(static_cast( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast(message.repeated_packed_unexpected_e(0))); - EXPECT_EQ(static_cast( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast(message.oneof_e_1())); } @@ -99,7 +90,7 @@ void CheckMessage( TEST(PreserveUnknownEnumTest, PreserveParseAndSerialize) { proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra orig_message; FillMessage(&orig_message); - string serialized; + std::string serialized; orig_message.SerializeToString(&serialized); proto3_preserve_unknown_enum_unittest::MyMessage message; @@ -117,7 +108,7 @@ TEST(PreserveUnknownEnumTest, PreserveParseAndSerialize) { TEST(PreserveUnknownEnumTest, PreserveParseAndSerializeDynamicMessage) { proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra orig_message; FillMessage(&orig_message); - string serialized = orig_message.SerializeAsString(); + std::string serialized = orig_message.SerializeAsString(); DynamicMessageFactory factory; std::unique_ptr message( @@ -138,7 +129,7 @@ TEST(PreserveUnknownEnumTest, Proto2HidesUnknownValues) { proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra orig_message; FillMessage(&orig_message); - string serialized; + std::string serialized; orig_message.SerializeToString(&serialized); proto2_preserve_unknown_enum_unittest::MyMessage message; @@ -160,7 +151,7 @@ TEST(PreserveUnknownEnumTest, DynamicProto2HidesUnknownValues) { proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra orig_message; FillMessage(&orig_message); - string serialized; + std::string serialized; orig_message.SerializeToString(&serialized); DynamicMessageFactory factory; @@ -187,7 +178,7 @@ TEST(PreserveUnknownEnumTest, DynamicProto2HidesUnknownValues) { TEST(PreserveUnknownEnumTest, DynamicEnumValueDescriptors) { proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra orig_message; FillMessage(&orig_message); - string serialized; + std::string serialized; orig_message.SerializeToString(&serialized); proto3_preserve_unknown_enum_unittest::MyMessage message; diff --git a/src/google/protobuf/proto3_arena_lite_unittest.cc b/src/google/protobuf/proto3_arena_lite_unittest.cc index 6d2abf63ac..e2693708a2 100644 --- a/src/google/protobuf/proto3_arena_lite_unittest.cc +++ b/src/google/protobuf/proto3_arena_lite_unittest.cc @@ -28,8 +28,8 @@ // (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 @@ -52,8 +52,7 @@ void SetAllFields(TestAllTypes* m) { m->mutable_optional_foreign_message()->set_c(43); m->set_optional_nested_enum( proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ); - m->set_optional_foreign_enum( - proto3_arena_unittest::FOREIGN_BAZ); + m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ); m->mutable_optional_lazy_message()->set_bb(45); m->add_repeated_int32(100); m->add_repeated_string("asdf"); @@ -62,8 +61,7 @@ void SetAllFields(TestAllTypes* m) { m->add_repeated_foreign_message()->set_c(47); m->add_repeated_nested_enum( proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ); - m->add_repeated_foreign_enum( - proto3_arena_unittest::FOREIGN_BAZ); + m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ); m->add_repeated_lazy_message()->set_bb(49); m->set_oneof_uint32(1); @@ -81,8 +79,7 @@ void ExpectAllFieldsSet(const TestAllTypes& m) { EXPECT_EQ(43, m.optional_foreign_message().c()); EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ, m.optional_nested_enum()); - EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, - m.optional_foreign_enum()); + EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum()); EXPECT_EQ(true, m.has_optional_lazy_message()); EXPECT_EQ(45, m.optional_lazy_message().bb()); @@ -100,8 +97,7 @@ void ExpectAllFieldsSet(const TestAllTypes& m) { EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ, m.repeated_nested_enum(0)); EXPECT_EQ(1, m.repeated_foreign_enum_size()); - EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, - m.repeated_foreign_enum(0)); + EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0)); EXPECT_EQ(1, m.repeated_lazy_message_size()); EXPECT_EQ(49, m.repeated_lazy_message(0).bb()); @@ -138,7 +134,7 @@ TEST(Proto3ArenaLiteTest, Swap) { TEST(Proto3ArenaLiteTest, SetAllocatedMessage) { Arena arena; - TestAllTypes *arena_message = Arena::CreateMessage(&arena); + TestAllTypes* arena_message = Arena::CreateMessage(&arena); TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage; nested->set_bb(118); arena_message->set_allocated_optional_nested_message(nested); diff --git a/src/google/protobuf/proto3_arena_unittest.cc b/src/google/protobuf/proto3_arena_unittest.cc index 3091aa0d65..d4b0135aab 100644 --- a/src/google/protobuf/proto3_arena_unittest.cc +++ b/src/google/protobuf/proto3_arena_unittest.cc @@ -28,8 +28,8 @@ // (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 @@ -54,8 +54,7 @@ void SetAllFields(TestAllTypes* m) { m->mutable_optional_foreign_message()->set_c(43); m->set_optional_nested_enum( proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ); - m->set_optional_foreign_enum( - proto3_arena_unittest::FOREIGN_BAZ); + m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ); m->mutable_optional_lazy_message()->set_bb(45); m->add_repeated_int32(100); m->add_repeated_string("asdf"); @@ -64,8 +63,7 @@ void SetAllFields(TestAllTypes* m) { m->add_repeated_foreign_message()->set_c(47); m->add_repeated_nested_enum( proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ); - m->add_repeated_foreign_enum( - proto3_arena_unittest::FOREIGN_BAZ); + m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ); m->add_repeated_lazy_message()->set_bb(49); m->set_oneof_uint32(1); @@ -83,8 +81,7 @@ void ExpectAllFieldsSet(const TestAllTypes& m) { EXPECT_EQ(43, m.optional_foreign_message().c()); EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ, m.optional_nested_enum()); - EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, - m.optional_foreign_enum()); + EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum()); EXPECT_EQ(true, m.has_optional_lazy_message()); EXPECT_EQ(45, m.optional_lazy_message().bb()); @@ -102,8 +99,7 @@ void ExpectAllFieldsSet(const TestAllTypes& m) { EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ, m.repeated_nested_enum(0)); EXPECT_EQ(1, m.repeated_foreign_enum_size()); - EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, - m.repeated_foreign_enum(0)); + EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0)); EXPECT_EQ(1, m.repeated_lazy_message_size()); EXPECT_EQ(49, m.repeated_lazy_message(0).bb()); @@ -161,7 +157,7 @@ TEST(Proto3ArenaTest, Swap) { TEST(Proto3ArenaTest, SetAllocatedMessage) { Arena arena; - TestAllTypes *arena_message = Arena::CreateMessage(&arena); + TestAllTypes* arena_message = Arena::CreateMessage(&arena); TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage; nested->set_bb(118); arena_message->set_allocated_optional_nested_message(nested); @@ -178,7 +174,7 @@ TEST(Proto3ArenaTest, ReleaseMessage) { } TEST(Proto3ArenaTest, MessageFieldClear) { - // GitHub issue #310: https://github.com/google/protobuf/issues/310 + // GitHub issue #310: https://github.com/protocolbuffers/protobuf/issues/310 Arena arena; TestAllTypes* arena_message = Arena::CreateMessage(&arena); arena_message->mutable_optional_nested_message()->set_bb(118); @@ -192,8 +188,8 @@ TEST(Proto3ArenaTest, MessageFieldClearViaReflection) { TestAllTypes* message = Arena::CreateMessage(&arena); const Reflection* r = message->GetReflection(); const Descriptor* d = message->GetDescriptor(); - const FieldDescriptor* msg_field = d->FindFieldByName( - "optional_nested_message"); + const FieldDescriptor* msg_field = + d->FindFieldByName("optional_nested_message"); message->mutable_optional_nested_message()->set_bb(1); r->ClearField(message, msg_field); diff --git a/src/google/protobuf/proto3_lite_unittest.inc b/src/google/protobuf/proto3_lite_unittest.inc index eca9b7062b..636691b49c 100644 --- a/src/google/protobuf/proto3_lite_unittest.inc +++ b/src/google/protobuf/proto3_lite_unittest.inc @@ -28,8 +28,8 @@ // (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 diff --git a/src/google/protobuf/reflection.h b/src/google/protobuf/reflection.h index c14a896cdb..6b1e5f2eda 100644 --- a/src/google/protobuf/reflection.h +++ b/src/google/protobuf/reflection.h @@ -42,42 +42,38 @@ #error "You cannot SWIG proto headers" #endif +#include + namespace google { namespace protobuf { namespace internal { -template +template struct RefTypeTraits; } // namespace internal -template +template RepeatedFieldRef Reflection::GetRepeatedFieldRef( const Message& message, const FieldDescriptor* field) const { return RepeatedFieldRef(message, field); } -template +template MutableRepeatedFieldRef Reflection::GetMutableRepeatedFieldRef( Message* message, const FieldDescriptor* field) const { return MutableRepeatedFieldRef(message, field); } // RepeatedFieldRef definition for non-message types. -template +template class RepeatedFieldRef< T, typename std::enable_if::value>::type> { typedef typename internal::RefTypeTraits::iterator IteratorType; typedef typename internal::RefTypeTraits::AccessorType AccessorType; public: - bool empty() const { - return accessor_->IsEmpty(data_); - } - int size() const { - return accessor_->Size(data_); - } - T Get(int index) const { - return accessor_->template Get(data_, index); - } + bool empty() const { return accessor_->IsEmpty(data_); } + int size() const { return accessor_->Size(data_); } + T Get(int index) const { return accessor_->template Get(data_, index); } typedef IteratorType iterator; typedef IteratorType const_iterator; @@ -87,22 +83,16 @@ class RepeatedFieldRef< typedef int size_type; typedef ptrdiff_t difference_type; - iterator begin() const { - return iterator(data_, accessor_, true); - } - iterator end() const { - return iterator(data_, accessor_, false); - } + iterator begin() const { return iterator(data_, accessor_, true); } + iterator end() const { return iterator(data_, accessor_, false); } private: friend class Reflection; - RepeatedFieldRef( - const Message& message, - const FieldDescriptor* field) { + RepeatedFieldRef(const Message& message, const FieldDescriptor* field) { const Reflection* reflection = message.GetReflection(); - data_ = reflection->RepeatedFieldData( - const_cast(&message), field, - internal::RefTypeTraits::cpp_type, NULL); + data_ = reflection->RepeatedFieldData(const_cast(&message), field, + internal::RefTypeTraits::cpp_type, + NULL); accessor_ = reflection->RepeatedFieldAccessor(field); } @@ -111,50 +101,38 @@ class RepeatedFieldRef< }; // MutableRepeatedFieldRef definition for non-message types. -template +template class MutableRepeatedFieldRef< T, typename std::enable_if::value>::type> { typedef typename internal::RefTypeTraits::AccessorType AccessorType; public: - bool empty() const { - return accessor_->IsEmpty(data_); - } - int size() const { - return accessor_->Size(data_); - } - T Get(int index) const { - return accessor_->template Get(data_, index); - } + bool empty() const { return accessor_->IsEmpty(data_); } + int size() const { return accessor_->Size(data_); } + T Get(int index) const { return accessor_->template Get(data_, index); } void Set(int index, const T& value) const { accessor_->template Set(data_, index, value); } - void Add(const T& value) const { - accessor_->template Add(data_, value); - } - void RemoveLast() const { - accessor_->RemoveLast(data_); - } + void Add(const T& value) const { accessor_->template Add(data_, value); } + void RemoveLast() const { accessor_->RemoveLast(data_); } void SwapElements(int index1, int index2) const { accessor_->SwapElements(data_, index1, index2); } - void Clear() const { - accessor_->Clear(data_); - } + void Clear() const { accessor_->Clear(data_); } void Swap(const MutableRepeatedFieldRef& other) const { accessor_->Swap(data_, other.accessor_, other.data_); } - template + template void MergeFrom(const Container& container) const { typedef typename Container::const_iterator Iterator; for (Iterator it = container.begin(); it != container.end(); ++it) { Add(*it); } } - template + template void CopyFrom(const Container& container) const { Clear(); MergeFrom(container); @@ -162,9 +140,7 @@ class MutableRepeatedFieldRef< private: friend class Reflection; - MutableRepeatedFieldRef( - Message* message, - const FieldDescriptor* field) { + MutableRepeatedFieldRef(Message* message, const FieldDescriptor* field) { const Reflection* reflection = message->GetReflection(); data_ = reflection->RepeatedFieldData( message, field, internal::RefTypeTraits::cpp_type, NULL); @@ -176,19 +152,15 @@ class MutableRepeatedFieldRef< }; // RepeatedFieldRef definition for message types. -template +template class RepeatedFieldRef< T, typename std::enable_if::value>::type> { typedef typename internal::RefTypeTraits::iterator IteratorType; typedef typename internal::RefTypeTraits::AccessorType AccessorType; public: - bool empty() const { - return accessor_->IsEmpty(data_); - } - int size() const { - return accessor_->Size(data_); - } + bool empty() const { return accessor_->IsEmpty(data_); } + int size() const { return accessor_->Size(data_); } // This method returns a reference to the underlying message object if it // exists. If a message object doesn't exist (e.g., data stored in serialized // form), scratch_space will be filled with the data and a reference to it @@ -203,9 +175,7 @@ class RepeatedFieldRef< } // Create a new message of the same type as the messages stored in this // repeated field. Caller takes ownership of the returned object. - T* NewMessage() const { - return static_cast(default_instance_->New()); - } + T* NewMessage() const { return static_cast(default_instance_->New()); } typedef IteratorType iterator; typedef IteratorType const_iterator; @@ -225,9 +195,7 @@ class RepeatedFieldRef< private: friend class Reflection; - RepeatedFieldRef( - const Message& message, - const FieldDescriptor* field) { + RepeatedFieldRef(const Message& message, const FieldDescriptor* field) { const Reflection* reflection = message.GetReflection(); data_ = reflection->RepeatedFieldData( const_cast(&message), field, @@ -244,56 +212,44 @@ class RepeatedFieldRef< }; // MutableRepeatedFieldRef definition for message types. -template +template class MutableRepeatedFieldRef< T, typename std::enable_if::value>::type> { typedef typename internal::RefTypeTraits::AccessorType AccessorType; public: - bool empty() const { - return accessor_->IsEmpty(data_); - } - int size() const { - return accessor_->Size(data_); - } + bool empty() const { return accessor_->IsEmpty(data_); } + int size() const { return accessor_->Size(data_); } // See comments for RepeatedFieldRef::Get() const T& Get(int index, T* scratch_space) const { return *static_cast(accessor_->Get(data_, index, scratch_space)); } // Create a new message of the same type as the messages stored in this // repeated field. Caller takes ownership of the returned object. - T* NewMessage() const { - return static_cast(default_instance_->New()); - } + T* NewMessage() const { return static_cast(default_instance_->New()); } void Set(int index, const T& value) const { accessor_->Set(data_, index, &value); } - void Add(const T& value) const { - accessor_->Add(data_, &value); - } - void RemoveLast() const { - accessor_->RemoveLast(data_); - } + void Add(const T& value) const { accessor_->Add(data_, &value); } + void RemoveLast() const { accessor_->RemoveLast(data_); } void SwapElements(int index1, int index2) const { accessor_->SwapElements(data_, index1, index2); } - void Clear() const { - accessor_->Clear(data_); - } + void Clear() const { accessor_->Clear(data_); } void Swap(const MutableRepeatedFieldRef& other) const { accessor_->Swap(data_, other.accessor_, other.data_); } - template + template void MergeFrom(const Container& container) const { typedef typename Container::const_iterator Iterator; for (Iterator it = container.begin(); it != container.end(); ++it) { Add(*it); } } - template + template void CopyFrom(const Container& container) const { Clear(); MergeFrom(container); @@ -301,9 +257,7 @@ class MutableRepeatedFieldRef< private: friend class Reflection; - MutableRepeatedFieldRef( - Message* message, - const FieldDescriptor* field) { + MutableRepeatedFieldRef(Message* message, const FieldDescriptor* field) { const Reflection* reflection = message->GetReflection(); data_ = reflection->RepeatedFieldData( message, field, internal::RefTypeTraits::cpp_type, @@ -336,7 +290,7 @@ namespace internal { // CPPTYPE_FLOAT float float // CPPTYPE_BOOL bool bool // CPPTYPE_ENUM generated enum type int32 -// CPPTYPE_STRING string string +// CPPTYPE_STRING string std::string // CPPTYPE_MESSAGE generated message type google::protobuf::Message // or google::protobuf::Message // @@ -344,7 +298,7 @@ namespace internal { // // You can map from T to the actual type using RefTypeTraits: // typedef RefTypeTraits::AccessorValueType ActualType; -class LIBPROTOBUF_EXPORT RepeatedFieldAccessor { +class PROTOBUF_EXPORT RepeatedFieldAccessor { public: // Typedefs for clarity. typedef void Field; @@ -358,8 +312,8 @@ class LIBPROTOBUF_EXPORT RepeatedFieldAccessor { // exists, or fill the data into scratch_space and return scratch_space. // Callers of this method must ensure scratch_space is a valid pointer // to a mutable object of the correct type. - virtual const Value* Get( - const Field* data, int index, Value* scratch_space) const = 0; + virtual const Value* Get(const Field* data, int index, + Value* scratch_space) const = 0; virtual void Clear(Field* data) const = 0; virtual void Set(Field* data, int index, const Value* value) const = 0; @@ -392,16 +346,15 @@ class LIBPROTOBUF_EXPORT RepeatedFieldAccessor { // Templated methods that make using this interface easier for non-message // types. - template + template T Get(const Field* data, int index) const { typedef typename RefTypeTraits::AccessorValueType ActualType; ActualType scratch_space; - return static_cast( - *reinterpret_cast( - Get(data, index, static_cast(&scratch_space)))); + return static_cast(*reinterpret_cast( + Get(data, index, static_cast(&scratch_space)))); } - template + template void Set(Field* data, int index, const ValueType& value) const { typedef typename RefTypeTraits::AccessorValueType ActualType; // In this RepeatedFieldAccessor interface we pass/return data using @@ -415,7 +368,7 @@ class LIBPROTOBUF_EXPORT RepeatedFieldAccessor { Set(data, index, static_cast(&tmp)); } - template + template void Add(Field* data, const ValueType& value) const { typedef typename RefTypeTraits::AccessorValueType ActualType; // In this RepeatedFieldAccessor interface we pass/return data using @@ -438,7 +391,7 @@ class LIBPROTOBUF_EXPORT RepeatedFieldAccessor { }; // Implement (Mutable)RepeatedFieldRef::iterator -template +template class RepeatedFieldRefIterator : public std::iterator { typedef typename RefTypeTraits::AccessorValueType AccessorValueType; @@ -457,17 +410,14 @@ class RepeatedFieldRefIterator scratch_space_(begin ? new AccessorValueType : nullptr) {} // Constructor for message fields. RepeatedFieldRefIterator(const void* data, - const RepeatedFieldAccessor* accessor, - bool begin, + const RepeatedFieldAccessor* accessor, bool begin, AccessorValueType* scratch_space) - : data_(data), accessor_(accessor), - iterator_(begin ? accessor->BeginIterator(data) : - accessor->EndIterator(data)), - scratch_space_(scratch_space) { - } - ~RepeatedFieldRefIterator() { - accessor_->DeleteIterator(data_, iterator_); - } + : data_(data), + accessor_(accessor), + iterator_(begin ? accessor->BeginIterator(data) + : accessor->EndIterator(data)), + scratch_space_(scratch_space) {} + ~RepeatedFieldRefIterator() { accessor_->DeleteIterator(data_, iterator_); } RepeatedFieldRefIterator operator++(int) { RepeatedFieldRefIterator tmp(*this); iterator_ = accessor_->AdvanceIterator(data_, iterator_); @@ -479,14 +429,12 @@ class RepeatedFieldRefIterator } IteratorValueType operator*() const { return static_cast( - *static_cast( - accessor_->GetIteratorValue( - data_, iterator_, scratch_space_.get()))); + *static_cast(accessor_->GetIteratorValue( + data_, iterator_, scratch_space_.get()))); } IteratorPointerType operator->() const { return static_cast( - accessor_->GetIteratorValue( - data_, iterator_, scratch_space_.get())); + accessor_->GetIteratorValue(data_, iterator_, scratch_space_.get())); } bool operator!=(const RepeatedFieldRefIterator& other) const { assert(data_ == other.data_); @@ -498,9 +446,9 @@ class RepeatedFieldRefIterator } RepeatedFieldRefIterator(const RepeatedFieldRefIterator& other) - : data_(other.data_), accessor_(other.accessor_), - iterator_(accessor_->CopyIterator(data_, other.iterator_)) { - } + : data_(other.data_), + accessor_(other.accessor_), + iterator_(accessor_->CopyIterator(data_, other.iterator_)) {} RepeatedFieldRefIterator& operator=(const RepeatedFieldRefIterator& other) { if (this != &other) { accessor_->DeleteIterator(data_, iterator_); @@ -521,16 +469,17 @@ class RepeatedFieldRefIterator // TypeTraits that maps the type parameter T of RepeatedFieldRef or // MutableRepeatedFieldRef to corresponding iterator type, // RepeatedFieldAccessor type, etc. -template +template struct PrimitiveTraits { static const bool is_primitive = false; }; -#define DEFINE_PRIMITIVE(TYPE, type) \ - template<> struct PrimitiveTraits { \ - static const bool is_primitive = true; \ - static const FieldDescriptor::CppType cpp_type = \ - FieldDescriptor::CPPTYPE_ ## TYPE; \ - }; +#define DEFINE_PRIMITIVE(TYPE, type) \ + template <> \ + struct PrimitiveTraits { \ + static const bool is_primitive = true; \ + static const FieldDescriptor::CppType cpp_type = \ + FieldDescriptor::CPPTYPE_##TYPE; \ + }; DEFINE_PRIMITIVE(INT32, int32) DEFINE_PRIMITIVE(UINT32, uint32) DEFINE_PRIMITIVE(INT64, int64) @@ -540,7 +489,7 @@ DEFINE_PRIMITIVE(DOUBLE, double) DEFINE_PRIMITIVE(BOOL, bool) #undef DEFINE_PRIMITIVE -template +template struct RefTypeTraits< T, typename std::enable_if::is_primitive>::type> { typedef RepeatedFieldRefIterator iterator; @@ -548,14 +497,11 @@ struct RefTypeTraits< typedef T AccessorValueType; typedef T IteratorValueType; typedef T* IteratorPointerType; - static const FieldDescriptor::CppType cpp_type = - PrimitiveTraits::cpp_type; - static const Descriptor* GetMessageFieldDescriptor() { - return NULL; - } + static const FieldDescriptor::CppType cpp_type = PrimitiveTraits::cpp_type; + static const Descriptor* GetMessageFieldDescriptor() { return NULL; } }; -template +template struct RefTypeTraits< T, typename std::enable_if::value>::type> { typedef RepeatedFieldRefIterator iterator; @@ -566,40 +512,34 @@ struct RefTypeTraits< typedef int32* IteratorPointerType; static const FieldDescriptor::CppType cpp_type = FieldDescriptor::CPPTYPE_ENUM; - static const Descriptor* GetMessageFieldDescriptor() { - return NULL; - } + static const Descriptor* GetMessageFieldDescriptor() { return NULL; } }; -template +template struct RefTypeTraits< - T, typename std::enable_if::value>::type> { + T, typename std::enable_if::value>::type> { typedef RepeatedFieldRefIterator iterator; typedef RepeatedFieldAccessor AccessorType; - typedef string AccessorValueType; - typedef const string IteratorValueType; - typedef const string* IteratorPointerType; + typedef std::string AccessorValueType; + typedef const std::string IteratorValueType; + typedef const std::string* IteratorPointerType; static const FieldDescriptor::CppType cpp_type = FieldDescriptor::CPPTYPE_STRING; - static const Descriptor* GetMessageFieldDescriptor() { - return NULL; - } + static const Descriptor* GetMessageFieldDescriptor() { return NULL; } }; -template +template struct MessageDescriptorGetter { static const Descriptor* get() { return T::default_instance().GetDescriptor(); } }; -template<> +template <> struct MessageDescriptorGetter { - static const Descriptor* get() { - return NULL; - } + static const Descriptor* get() { return NULL; } }; -template +template struct RefTypeTraits< T, typename std::enable_if::value>::type> { typedef RepeatedFieldRefIterator iterator; @@ -617,4 +557,6 @@ struct RefTypeTraits< } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_REFLECTION_H__ diff --git a/src/google/protobuf/reflection_internal.h b/src/google/protobuf/reflection_internal.h index f664d8e2a2..40c070fc79 100644 --- a/src/google/protobuf/reflection_internal.h +++ b/src/google/protobuf/reflection_internal.h @@ -82,7 +82,7 @@ class RandomAccessRepeatedFieldAccessor : public RepeatedFieldAccessor { // Base class for RepeatedFieldAccessor implementations that manipulates // RepeatedField. -template +template class RepeatedFieldWrapper : public RandomAccessRepeatedFieldAccessor { public: RepeatedFieldWrapper() {} @@ -127,9 +127,9 @@ class RepeatedFieldWrapper : public RandomAccessRepeatedFieldAccessor { virtual T ConvertToT(const Value* value) const = 0; // Convert an object stored in RepeatedPtrField to an object that will be - // returned by this accessor. If the two objects have the same type (true - // for string fields with ctype=STRING), a pointer to the source object can - // be returned directly. Otherwise, data should be copied from value to + // returned by this accessor. If the two objects have the same type (true for + // string fields with ctype=STRING), a pointer to the source object can be + // returned directly. Otherwise, data should be copied from value to // scratch_space and scratch_space should be returned. virtual const Value* ConvertFromT(const T& value, Value* scratch_space) const = 0; @@ -137,7 +137,7 @@ class RepeatedFieldWrapper : public RandomAccessRepeatedFieldAccessor { // Base class for RepeatedFieldAccessor implementations that manipulates // RepeatedPtrField. -template +template class RepeatedPtrFieldWrapper : public RandomAccessRepeatedFieldAccessor { public: bool IsEmpty(const Field* data) const override { @@ -189,9 +189,9 @@ class RepeatedPtrFieldWrapper : public RandomAccessRepeatedFieldAccessor { virtual void ConvertToT(const Value* value, T* result) const = 0; // Convert an object stored in RepeatedPtrField to an object that will be - // returned by this accessor. If the two objects have the same type (true - // for string fields with ctype=STRING), a pointer to the source object can - // be returned directly. Otherwise, data should be copied from value to + // returned by this accessor. If the two objects have the same type (true for + // string fields with ctype=STRING), a pointer to the source object can be + // returned directly. Otherwise, data should be copied from value to // scratch_space and scratch_space should be returned. virtual const Value* ConvertFromT(const T& value, Value* scratch_space) const = 0; @@ -203,7 +203,7 @@ class MapFieldAccessor final : public RandomAccessRepeatedFieldAccessor { public: MapFieldAccessor() {} virtual ~MapFieldAccessor() {} - virtual bool IsEmpty(const Field* data) const { + bool IsEmpty(const Field* data) const override { return GetRepeatedField(data)->empty(); } int Size(const Field* data) const override { @@ -293,7 +293,7 @@ class RepeatedFieldPrimitiveAccessor final : public RepeatedFieldWrapper { // Default implementation of RepeatedFieldAccessor for string fields with // ctype=STRING. class RepeatedPtrFieldStringAccessor final - : public RepeatedPtrFieldWrapper { + : public RepeatedPtrFieldWrapper { typedef void Field; typedef void Value; using RepeatedFieldAccessor::Add; @@ -305,26 +305,26 @@ class RepeatedPtrFieldStringAccessor final if (this == other_mutator) { MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data)); } else { - RepeatedPtrField tmp; + RepeatedPtrField tmp; tmp.Swap(MutableRepeatedField(data)); int other_size = other_mutator->Size(other_data); for (int i = 0; i < other_size; ++i) { - Add(data, other_mutator->Get(other_data, i)); + Add(data, other_mutator->Get(other_data, i)); } int size = Size(data); other_mutator->Clear(other_data); for (int i = 0; i < size; ++i) { - other_mutator->Add(other_data, tmp.Get(i)); + other_mutator->Add(other_data, tmp.Get(i)); } } } protected: - string* New(const Value*) const override { return new string(); } - void ConvertToT(const Value* value, string* result) const override { - *result = *static_cast(value); + std::string* New(const Value*) const override { return new std::string(); } + void ConvertToT(const Value* value, std::string* result) const override { + *result = *static_cast(value); } - const Value* ConvertFromT(const string& value, + const Value* ConvertFromT(const std::string& value, Value* scratch_space) const override { return static_cast(&value); } diff --git a/src/google/protobuf/reflection_ops.cc b/src/google/protobuf/reflection_ops.cc index f028b2b05b..3ced1419f4 100644 --- a/src/google/protobuf/reflection_ops.cc +++ b/src/google/protobuf/reflection_ops.cc @@ -45,6 +45,9 @@ #include #include + +#include + namespace google { namespace protobuf { namespace internal { @@ -53,7 +56,7 @@ static const Reflection* GetReflectionOrDie(const Message& m) { const Reflection* r = m.GetReflection(); if (r == nullptr) { const Descriptor* d = m.GetDescriptor(); - const string& mtype = d ? d->name() : "unknown"; + const std::string& mtype = d ? d->name() : "unknown"; // RawMessage is one known type for which GetReflection() returns nullptr. GOOGLE_LOG(FATAL) << "Message does not support reflection (type " << mtype << ")."; } @@ -71,12 +74,16 @@ void ReflectionOps::Merge(const Message& from, Message* to) { const Descriptor* descriptor = from.GetDescriptor(); GOOGLE_CHECK_EQ(to->GetDescriptor(), descriptor) - << "Tried to merge messages of different types " - << "(merge " << descriptor->full_name() - << " to " << to->GetDescriptor()->full_name() << ")"; + << "Tried to merge messages of different types " + << "(merge " << descriptor->full_name() << " to " + << to->GetDescriptor()->full_name() << ")"; const Reflection* from_reflection = GetReflectionOrDie(from); const Reflection* to_reflection = GetReflectionOrDie(*to); + bool is_from_generated = (from_reflection->GetMessageFactory() == + google::protobuf::MessageFactory::generated_factory()); + bool is_to_generated = (to_reflection->GetMessageFactory() == + google::protobuf::MessageFactory::generated_factory()); std::vector fields; from_reflection->ListFields(from, &fields); @@ -84,61 +91,75 @@ void ReflectionOps::Merge(const Message& from, Message* to) { const FieldDescriptor* field = fields[i]; if (field->is_repeated()) { + // Use map reflection if both are in map status and have the + // same map type to avoid sync with repeated field. + // Note: As from and to messages have the same descriptor, the + // map field types are the same if they are both generated + // messages or both dynamic messages. + if (is_from_generated == is_to_generated && field->is_map()) { + const MapFieldBase* from_field = + from_reflection->GetMapData(from, field); + MapFieldBase* to_field = to_reflection->MutableMapData(to, field); + if (to_field->IsMapValid() && from_field->IsMapValid()) { + to_field->MergeFrom(*from_field); + continue; + } + } int count = from_reflection->FieldSize(from, field); for (int j = 0; j < count; j++) { switch (field->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, METHOD) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - to_reflection->Add##METHOD(to, field, \ - from_reflection->GetRepeated##METHOD(from, field, j)); \ - break; - - HANDLE_TYPE(INT32 , Int32 ); - HANDLE_TYPE(INT64 , Int64 ); +#define HANDLE_TYPE(CPPTYPE, METHOD) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + to_reflection->Add##METHOD( \ + to, field, from_reflection->GetRepeated##METHOD(from, field, j)); \ + break; + + HANDLE_TYPE(INT32, Int32); + HANDLE_TYPE(INT64, Int64); HANDLE_TYPE(UINT32, UInt32); HANDLE_TYPE(UINT64, UInt64); - HANDLE_TYPE(FLOAT , Float ); + HANDLE_TYPE(FLOAT, Float); HANDLE_TYPE(DOUBLE, Double); - HANDLE_TYPE(BOOL , Bool ); + HANDLE_TYPE(BOOL, Bool); HANDLE_TYPE(STRING, String); - HANDLE_TYPE(ENUM , Enum ); + HANDLE_TYPE(ENUM, Enum); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: to_reflection->AddMessage(to, field)->MergeFrom( - from_reflection->GetRepeatedMessage(from, field, j)); + from_reflection->GetRepeatedMessage(from, field, j)); break; } } } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, METHOD) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - to_reflection->Set##METHOD(to, field, \ - from_reflection->Get##METHOD(from, field)); \ - break; - - HANDLE_TYPE(INT32 , Int32 ); - HANDLE_TYPE(INT64 , Int64 ); +#define HANDLE_TYPE(CPPTYPE, METHOD) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + to_reflection->Set##METHOD(to, field, \ + from_reflection->Get##METHOD(from, field)); \ + break; + + HANDLE_TYPE(INT32, Int32); + HANDLE_TYPE(INT64, Int64); HANDLE_TYPE(UINT32, UInt32); HANDLE_TYPE(UINT64, UInt64); - HANDLE_TYPE(FLOAT , Float ); + HANDLE_TYPE(FLOAT, Float); HANDLE_TYPE(DOUBLE, Double); - HANDLE_TYPE(BOOL , Bool ); + HANDLE_TYPE(BOOL, Bool); HANDLE_TYPE(STRING, String); - HANDLE_TYPE(ENUM , Enum ); + HANDLE_TYPE(ENUM, Enum); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: to_reflection->MutableMessage(to, field)->MergeFrom( - from_reflection->GetMessage(from, field)); + from_reflection->GetMessage(from, field)); break; } } } to_reflection->MutableUnknownFields(to)->MergeFrom( - from_reflection->GetUnknownFields(from)); + from_reflection->GetUnknownFields(from)); } void ReflectionOps::Clear(Message* message) { @@ -176,8 +197,8 @@ bool ReflectionOps::IsInitialized(const Message& message) { if (field->is_map()) { const FieldDescriptor* value_field = field->message_type()->field(1); if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - MapFieldBase* map_field = - reflection->MapData(const_cast(&message), field); + const MapFieldBase* map_field = + reflection->GetMapData(message, field); if (map_field->IsMapValid()) { MapIterator iter(const_cast(&message), field); MapIterator end(const_cast(&message), field); @@ -199,7 +220,7 @@ bool ReflectionOps::IsInitialized(const Message& message) { for (int j = 0; j < size; j++) { if (!reflection->GetRepeatedMessage(message, field, j) - .IsInitialized()) { + .IsInitialized()) { return false; } } @@ -225,10 +246,30 @@ void ReflectionOps::DiscardUnknownFields(Message* message) { const FieldDescriptor* field = fields[i]; if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { if (field->is_repeated()) { + if (field->is_map()) { + const FieldDescriptor* value_field = field->message_type()->field(1); + if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { + const MapFieldBase* map_field = + reflection->MutableMapData(message, field); + if (map_field->IsMapValid()) { + MapIterator iter(message, field); + MapIterator end(message, field); + for (map_field->MapBegin(&iter), map_field->MapEnd(&end); + iter != end; ++iter) { + iter.MutableValueRef() + ->MutableMessageValue() + ->DiscardUnknownFields(); + } + continue; + } + } else { + continue; + } + } int size = reflection->FieldSize(*message, field); for (int j = 0; j < size; j++) { reflection->MutableRepeatedMessage(message, field, j) - ->DiscardUnknownFields(); + ->DiscardUnknownFields(); } } else { reflection->MutableMessage(message, field)->DiscardUnknownFields(); @@ -237,10 +278,9 @@ void ReflectionOps::DiscardUnknownFields(Message* message) { } } -static string SubMessagePrefix(const string& prefix, - const FieldDescriptor* field, - int index) { - string result(prefix); +static std::string SubMessagePrefix(const std::string& prefix, + const FieldDescriptor* field, int index) { + std::string result(prefix); if (field->is_extension()) { result.append("("); result.append(field->full_name()); @@ -250,17 +290,16 @@ static string SubMessagePrefix(const string& prefix, } if (index != -1) { result.append("["); - result.append(SimpleItoa(index)); + result.append(StrCat(index)); result.append("]"); } result.append("."); return result; } -void ReflectionOps::FindInitializationErrors( - const Message& message, - const string& prefix, - std::vector* errors) { +void ReflectionOps::FindInitializationErrors(const Message& message, + const std::string& prefix, + std::vector* errors) { const Descriptor* descriptor = message.GetDescriptor(); const Reflection* reflection = GetReflectionOrDie(message); @@ -285,16 +324,14 @@ void ReflectionOps::FindInitializationErrors( for (int j = 0; j < size; j++) { const Message& sub_message = - reflection->GetRepeatedMessage(message, field, j); + reflection->GetRepeatedMessage(message, field, j); FindInitializationErrors(sub_message, - SubMessagePrefix(prefix, field, j), - errors); + SubMessagePrefix(prefix, field, j), errors); } } else { const Message& sub_message = reflection->GetMessage(message, field); FindInitializationErrors(sub_message, - SubMessagePrefix(prefix, field, -1), - errors); + SubMessagePrefix(prefix, field, -1), errors); } } } diff --git a/src/google/protobuf/reflection_ops.h b/src/google/protobuf/reflection_ops.h index 78666f30a1..b30c050a75 100644 --- a/src/google/protobuf/reflection_ops.h +++ b/src/google/protobuf/reflection_ops.h @@ -45,6 +45,8 @@ #error "You cannot SWIG proto headers" #endif +#include + namespace google { namespace protobuf { namespace internal { @@ -58,7 +60,7 @@ namespace internal { // the Message interface. // // This class is really a namespace that contains only static methods. -class LIBPROTOBUF_EXPORT ReflectionOps { +class PROTOBUF_EXPORT ReflectionOps { public: static void Copy(const Message& from, Message* to); static void Merge(const Message& from, Message* to); @@ -70,8 +72,8 @@ class LIBPROTOBUF_EXPORT ReflectionOps { // paths (e.g. "foo.bar[5].baz") to *names. "prefix" will be attached to // the front of each name. static void FindInitializationErrors(const Message& message, - const string& prefix, - std::vector* errors); + const std::string& prefix, + std::vector* errors); private: // All methods are static. No need to construct. @@ -82,4 +84,6 @@ class LIBPROTOBUF_EXPORT ReflectionOps { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_REFLECTION_OPS_H__ diff --git a/src/google/protobuf/reflection_ops_unittest.cc b/src/google/protobuf/reflection_ops_unittest.cc index 9cedb34229..bd141933c6 100644 --- a/src/google/protobuf/reflection_ops_unittest.cc +++ b/src/google/protobuf/reflection_ops_unittest.cc @@ -33,9 +33,9 @@ // Sanjay Ghemawat, Jeff Dean, and others. #include -#include -#include #include +#include +#include #include #include @@ -128,18 +128,21 @@ TEST(ReflectionOpsTest, MergeExtensions) { TestUtil::SetAllExtensions(&message); // This field will test merging into an empty spot. - message2.SetExtension(unittest::optional_int32_extension, - message.GetExtension(unittest::optional_int32_extension)); + message2.SetExtension( + unittest::optional_int32_extension, + message.GetExtension(unittest::optional_int32_extension)); message.ClearExtension(unittest::optional_int32_extension); // This tests overwriting. - message2.SetExtension(unittest::optional_string_extension, - message.GetExtension(unittest::optional_string_extension)); + message2.SetExtension( + unittest::optional_string_extension, + message.GetExtension(unittest::optional_string_extension)); message.SetExtension(unittest::optional_string_extension, "something else"); // This tests concatenating. - message2.AddExtension(unittest::repeated_int32_extension, - message.GetExtension(unittest::repeated_int32_extension, 1)); + message2.AddExtension( + unittest::repeated_int32_extension, + message.GetExtension(unittest::repeated_int32_extension, 1)); int32 i = message.GetExtension(unittest::repeated_int32_extension, 0); message.ClearExtension(unittest::repeated_int32_extension); message.AddExtension(unittest::repeated_int32_extension, i); @@ -192,9 +195,7 @@ TEST(ReflectionOpsTest, MergeFromSelf) { unittest::TestAllTypes message; - EXPECT_DEATH( - ReflectionOps::Merge(message, &message), - "&from"); + EXPECT_DEATH(ReflectionOps::Merge(message, &message), "&from"); } #endif // PROTOBUF_HAS_DEATH_TEST @@ -235,9 +236,9 @@ TEST(ReflectionOpsTest, ClearExtensions) { &message.GetExtension(unittest::optionalgroup_extension)); EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(), &message.GetExtension(unittest::optional_nested_message_extension)); - EXPECT_NE(&unittest::ForeignMessage::default_instance(), - &message.GetExtension( - unittest::optional_foreign_message_extension)); + EXPECT_NE( + &unittest::ForeignMessage::default_instance(), + &message.GetExtension(unittest::optional_foreign_message_extension)); EXPECT_NE(&unittest_import::ImportMessage::default_instance(), &message.GetExtension(unittest::optional_import_message_extension)); } @@ -274,30 +275,29 @@ TEST(ReflectionOpsTest, DiscardUnknownFields) { TestUtil::SetAllFields(&message); // Set some unknown fields in message. - message.mutable_unknown_fields() - ->AddVarint(123456, 654321); + message.mutable_unknown_fields()->AddVarint(123456, 654321); message.mutable_optional_nested_message() - ->mutable_unknown_fields() - ->AddVarint(123456, 654321); + ->mutable_unknown_fields() + ->AddVarint(123456, 654321); message.mutable_repeated_nested_message(0) - ->mutable_unknown_fields() - ->AddVarint(123456, 654321); + ->mutable_unknown_fields() + ->AddVarint(123456, 654321); EXPECT_EQ(1, message.unknown_fields().field_count()); - EXPECT_EQ(1, message.optional_nested_message() - .unknown_fields().field_count()); - EXPECT_EQ(1, message.repeated_nested_message(0) - .unknown_fields().field_count()); + EXPECT_EQ(1, + message.optional_nested_message().unknown_fields().field_count()); + EXPECT_EQ(1, + message.repeated_nested_message(0).unknown_fields().field_count()); // Discard them. ReflectionOps::DiscardUnknownFields(&message); TestUtil::ExpectAllFieldsSet(message); EXPECT_EQ(0, message.unknown_fields().field_count()); - EXPECT_EQ(0, message.optional_nested_message() - .unknown_fields().field_count()); - EXPECT_EQ(0, message.repeated_nested_message(0) - .unknown_fields().field_count()); + EXPECT_EQ(0, + message.optional_nested_message().unknown_fields().field_count()); + EXPECT_EQ(0, + message.repeated_nested_message(0).unknown_fields().field_count()); } TEST(ReflectionOpsTest, DiscardUnknownExtensions) { @@ -305,34 +305,35 @@ TEST(ReflectionOpsTest, DiscardUnknownExtensions) { TestUtil::SetAllExtensions(&message); // Set some unknown fields. - message.mutable_unknown_fields() - ->AddVarint(123456, 654321); + message.mutable_unknown_fields()->AddVarint(123456, 654321); message.MutableExtension(unittest::optional_nested_message_extension) - ->mutable_unknown_fields() - ->AddVarint(123456, 654321); + ->mutable_unknown_fields() + ->AddVarint(123456, 654321); message.MutableExtension(unittest::repeated_nested_message_extension, 0) - ->mutable_unknown_fields() - ->AddVarint(123456, 654321); + ->mutable_unknown_fields() + ->AddVarint(123456, 654321); EXPECT_EQ(1, message.unknown_fields().field_count()); + EXPECT_EQ(1, message.GetExtension(unittest::optional_nested_message_extension) + .unknown_fields() + .field_count()); EXPECT_EQ(1, - message.GetExtension(unittest::optional_nested_message_extension) - .unknown_fields().field_count()); - EXPECT_EQ(1, - message.GetExtension(unittest::repeated_nested_message_extension, 0) - .unknown_fields().field_count()); + message.GetExtension(unittest::repeated_nested_message_extension, 0) + .unknown_fields() + .field_count()); // Discard them. ReflectionOps::DiscardUnknownFields(&message); TestUtil::ExpectAllExtensionsSet(message); EXPECT_EQ(0, message.unknown_fields().field_count()); + EXPECT_EQ(0, message.GetExtension(unittest::optional_nested_message_extension) + .unknown_fields() + .field_count()); EXPECT_EQ(0, - message.GetExtension(unittest::optional_nested_message_extension) - .unknown_fields().field_count()); - EXPECT_EQ(0, - message.GetExtension(unittest::repeated_nested_message_extension, 0) - .unknown_fields().field_count()); + message.GetExtension(unittest::repeated_nested_message_extension, 0) + .unknown_fields() + .field_count()); } TEST(ReflectionOpsTest, IsInitialized) { @@ -419,8 +420,8 @@ TEST(ReflectionOpsTest, OneofIsInitialized) { EXPECT_TRUE(ReflectionOps::IsInitialized(message)); } -static string FindInitializationErrors(const Message& message) { - std::vector errors; +static std::string FindInitializationErrors(const Message& message) { + std::vector errors; ReflectionOps::FindInitializationErrors(message, "", &errors); return Join(errors, ","); } @@ -435,16 +436,17 @@ TEST(ReflectionOpsTest, FindForeignInitializationErrors) { message.mutable_optional_message(); message.add_repeated_message(); message.add_repeated_message(); - EXPECT_EQ("optional_message.a," - "optional_message.b," - "optional_message.c," - "repeated_message[0].a," - "repeated_message[0].b," - "repeated_message[0].c," - "repeated_message[1].a," - "repeated_message[1].b," - "repeated_message[1].c", - FindInitializationErrors(message)); + EXPECT_EQ( + "optional_message.a," + "optional_message.b," + "optional_message.c," + "repeated_message[0].a," + "repeated_message[0].b," + "repeated_message[0].c," + "repeated_message[1].a," + "repeated_message[1].b," + "repeated_message[1].c", + FindInitializationErrors(message)); } TEST(ReflectionOpsTest, FindExtensionInitializationErrors) { @@ -452,23 +454,23 @@ TEST(ReflectionOpsTest, FindExtensionInitializationErrors) { message.MutableExtension(unittest::TestRequired::single); message.AddExtension(unittest::TestRequired::multi); message.AddExtension(unittest::TestRequired::multi); - EXPECT_EQ("(protobuf_unittest.TestRequired.single).a," - "(protobuf_unittest.TestRequired.single).b," - "(protobuf_unittest.TestRequired.single).c," - "(protobuf_unittest.TestRequired.multi)[0].a," - "(protobuf_unittest.TestRequired.multi)[0].b," - "(protobuf_unittest.TestRequired.multi)[0].c," - "(protobuf_unittest.TestRequired.multi)[1].a," - "(protobuf_unittest.TestRequired.multi)[1].b," - "(protobuf_unittest.TestRequired.multi)[1].c", - FindInitializationErrors(message)); + EXPECT_EQ( + "(protobuf_unittest.TestRequired.single).a," + "(protobuf_unittest.TestRequired.single).b," + "(protobuf_unittest.TestRequired.single).c," + "(protobuf_unittest.TestRequired.multi)[0].a," + "(protobuf_unittest.TestRequired.multi)[0].b," + "(protobuf_unittest.TestRequired.multi)[0].c," + "(protobuf_unittest.TestRequired.multi)[1].a," + "(protobuf_unittest.TestRequired.multi)[1].b," + "(protobuf_unittest.TestRequired.multi)[1].c", + FindInitializationErrors(message)); } TEST(ReflectionOpsTest, FindOneofInitializationErrors) { unittest::TestRequiredOneof message; message.mutable_foo_message(); - EXPECT_EQ("foo_message.required_double", - FindInitializationErrors(message)); + EXPECT_EQ("foo_message.required_double", FindInitializationErrors(message)); } } // namespace diff --git a/src/google/protobuf/repeated_field.cc b/src/google/protobuf/repeated_field.cc index 89d70a7127..e5dbe025be 100644 --- a/src/google/protobuf/repeated_field.cc +++ b/src/google/protobuf/repeated_field.cc @@ -34,9 +34,11 @@ #include -#include #include #include +#include + +#include namespace google { namespace protobuf { @@ -54,9 +56,8 @@ void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) { Arena* arena = GetArenaNoVirtual(); new_size = std::max(kMinRepeatedFieldAllocationSize, std::max(total_size_ * 2, new_size)); - GOOGLE_CHECK_LE(new_size, - (std::numeric_limits::max() - kRepHeaderSize) / - sizeof(old_rep->elements[0])) + 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) { @@ -120,14 +121,14 @@ MessageLite* RepeatedPtrFieldBase::AddWeak(const MessageLite* prototype) { } // namespace internal -template class LIBPROTOBUF_EXPORT RepeatedField; -template class LIBPROTOBUF_EXPORT RepeatedField; -template class LIBPROTOBUF_EXPORT RepeatedField; -template class LIBPROTOBUF_EXPORT RepeatedField; -template class LIBPROTOBUF_EXPORT RepeatedField; -template class LIBPROTOBUF_EXPORT RepeatedField; -template class LIBPROTOBUF_EXPORT RepeatedField; -template class LIBPROTOBUF_EXPORT RepeatedPtrField; +template class PROTOBUF_EXPORT RepeatedField; +template class PROTOBUF_EXPORT RepeatedField; +template class PROTOBUF_EXPORT RepeatedField; +template class PROTOBUF_EXPORT RepeatedField; +template class PROTOBUF_EXPORT RepeatedField; +template class PROTOBUF_EXPORT RepeatedField; +template class PROTOBUF_EXPORT RepeatedField; +template class PROTOBUF_EXPORT RepeatedPtrField; } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h index 63062c1093..47c9987041 100644 --- a/src/google/protobuf/repeated_field.h +++ b/src/google/protobuf/repeated_field.h @@ -55,6 +55,7 @@ #include #include #include +#include #include #include #include @@ -82,6 +83,7 @@ namespace google { namespace protobuf { class Message; +class Reflection; namespace internal { @@ -110,13 +112,16 @@ inline int CalculateReserve(Iter begin, Iter end) { } } // 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: RepeatedField(); explicit RepeatedField(Arena* arena); @@ -139,11 +144,19 @@ class RepeatedField final { 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(); @@ -216,15 +229,11 @@ class RepeatedField final { // Reverse iterator support typedef std::reverse_iterator const_reverse_iterator; typedef std::reverse_iterator reverse_iterator; - reverse_iterator rbegin() { - return reverse_iterator(end()); - } + reverse_iterator rbegin() { return reverse_iterator(end()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } - reverse_iterator rend() { - return reverse_iterator(begin()); - } + reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } @@ -284,23 +293,30 @@ class RepeatedField final { // Element is double and pointer is 32bit). static const size_t kRepHeaderSize; - // We reuse the Rep* for an Arena* when total_size == 0, to avoid having to do - // an allocation in the constructor when we have an Arena. - union Pointer { - Pointer(Arena* a) : arena(a) {} - Arena* arena; // When total_size_ == 0. - Element* elements; // When total_size_ != 0, this is Rep->elements of Rep. - } ptr_; + // 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); - return ptr_.elements; + // 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 { - GOOGLE_DCHECK_GT(total_size_, 0); - char* addr = - reinterpret_cast(ptr_.elements) - offsetof(Rep, elements); + char* addr = reinterpret_cast(elements()) - offsetof(Rep, elements); return reinterpret_cast(addr); } @@ -318,7 +334,8 @@ class RepeatedField final { // Internal helper expected by Arena methods. inline Arena* GetArenaNoVirtual() const { - return (total_size_ == 0) ? ptr_.arena : rep()->arena; + return (total_size_ == 0) ? static_cast(arena_or_elements_) + : rep()->arena; } // Internal helper to delete all elements and deallocate the storage. @@ -341,18 +358,17 @@ class RepeatedField final { } } } - - friend class internal::WireFormatLite; - const Element* unsafe_data() const; }; -template +template const size_t RepeatedField::kRepHeaderSize = reinterpret_cast(&reinterpret_cast(16)->elements[0]) - 16; namespace internal { -template class RepeatedPtrIterator; -template class RepeatedPtrOverPtrsIterator; +template +class RepeatedPtrIterator; +template +class RepeatedPtrOverPtrsIterator; } // namespace internal namespace internal { @@ -376,7 +392,7 @@ namespace internal { // 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 string*. +// specific override for std::string*. template struct TypeImplementsMergeBehaviorProbeForMergeFrom { typedef char HasMerge; @@ -389,29 +405,36 @@ struct TypeImplementsMergeBehaviorProbeForMergeFrom { // 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(...); + 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; + (sizeof(Check(0)) == sizeof(HasMerge))> + type; }; template -struct TypeImplementsMergeBehavior : - TypeImplementsMergeBehaviorProbeForMergeFrom {}; +struct TypeImplementsMergeBehavior + : TypeImplementsMergeBehaviorProbeForMergeFrom {}; template <> -struct TypeImplementsMergeBehavior< ::std::string> { +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. // @@ -433,7 +456,7 @@ struct TypeImplementsMergeBehavior< ::std::string> { // // Only needs to be implemented if SpaceUsedExcludingSelf() is called. // static int SpaceUsedLong(const Type&); // }; -class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { +class PROTOBUF_EXPORT RepeatedPtrFieldBase { protected: RepeatedPtrFieldBase(); explicit RepeatedPtrFieldBase(Arena* arena); @@ -446,6 +469,11 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { 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 @@ -476,9 +504,10 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { inline void InternalSwap(RepeatedPtrFieldBase* other); protected: - template - void Add(typename TypeHandler::Type&& value, - typename std::enable_if::type* dummy = NULL); + template < + typename TypeHandler, + typename std::enable_if::type* = nullptr> + void Add(typename TypeHandler::Type&& value); template void RemoveLast(); @@ -501,8 +530,7 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { const typename TypeHandler::Type* const* data() const; template - GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE void Swap( - RepeatedPtrFieldBase* other); + PROTOBUF_ALWAYS_INLINE void Swap(RepeatedPtrFieldBase* other); void SwapElements(int index1, int index2); @@ -515,7 +543,7 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { template typename TypeHandler::Type* AddFromCleared(); - template + template void AddAllocated(typename TypeHandler::Type* value) { typename TypeImplementsMergeBehavior::type t; AddAllocatedInternal(value, t); @@ -547,10 +575,10 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { void AddAllocatedInternal(typename TypeHandler::Type* value, std::false_type); template - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE void AddAllocatedSlowWithCopy( + PROTOBUF_NOINLINE void AddAllocatedSlowWithCopy( typename TypeHandler::Type* value, Arena* value_arena, Arena* my_arena); template - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE void AddAllocatedSlowWithoutCopy( + PROTOBUF_NOINLINE void AddAllocatedSlowWithoutCopy( typename TypeHandler::Type* value); template @@ -559,12 +587,9 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { typename TypeHandler::Type* ReleaseLastInternal(std::false_type); template - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE void SwapFallback( - RepeatedPtrFieldBase* other); + PROTOBUF_NOINLINE void SwapFallback(RepeatedPtrFieldBase* other); - inline Arena* GetArenaNoVirtual() const { - return arena_; - } + inline Arena* GetArenaNoVirtual() const { return arena_; } private: static const int kInitialSize = 0; @@ -580,11 +605,11 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { // Placing all fields directly in the RepeatedPtrFieldBase instance costs // significant performance for memory-sensitive workloads. Arena* arena_; - int current_size_; - int total_size_; + int current_size_; + int total_size_; struct Rep { - int allocated_size; - void* elements[1]; + int allocated_size; + void* elements[1]; }; static const size_t kRepHeaderSize = sizeof(Rep) - sizeof(void*); // Contains arena ptr and the elements array. We also keep the invariant that @@ -602,13 +627,14 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { // 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)); + 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); + 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 @@ -619,7 +645,7 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase { // The reflection implementation needs to call protected methods directly, // reinterpreting pointers as being to Message instead of a specific Message // subclass. - friend class GeneratedMessageReflection; + friend class ::PROTOBUF_NAMESPACE_ID::Reflection; // ExtensionSet stores repeated message extensions as // RepeatedPtrField, but non-lite ExtensionSets need to implement @@ -651,11 +677,14 @@ class GenericTypeHandler { public: typedef GenericType Type; typedef GenericType WeakType; - static const bool Moveable = false; + 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) { @@ -671,7 +700,7 @@ class GenericTypeHandler { } static inline void Clear(GenericType* value) { value->Clear(); } - GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE + PROTOBUF_NOINLINE static void Merge(const GenericType& from, GenericType* to); static inline size_t SpaceUsedLong(const GenericType& value) { return value.SpaceUsedLong(); @@ -699,7 +728,7 @@ template <> inline Arena* GenericTypeHandler::GetArena(MessageLite* value) { return value->GetArena(); } -template<> +template <> inline void* GenericTypeHandler::GetMaybeArenaPointer( MessageLite* value) { return value->GetMaybeArenaPointer(); @@ -707,24 +736,26 @@ inline void* GenericTypeHandler::GetMaybeArenaPointer( template <> void GenericTypeHandler::Merge(const MessageLite& from, MessageLite* to); -template<> -inline void GenericTypeHandler::Clear(string* value) { +template <> +inline void GenericTypeHandler::Clear(std::string* value) { value->clear(); } -template<> -void GenericTypeHandler::Merge(const string& from, - string* to); +template <> +void GenericTypeHandler::Merge(const std::string& from, + std::string* to); // Declarations of the specialization as we cannot define them here, as the // header that defines ProtocolMessage depends on types defined in this header. -#define DECLARE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES(TypeName) \ - template <> LIBPROTOBUF_EXPORT \ - TypeName* GenericTypeHandler::NewFromPrototype( \ - const TypeName* prototype, Arena* arena); \ - template <> LIBPROTOBUF_EXPORT \ - Arena* GenericTypeHandler::GetArena(TypeName* value); \ - template <> LIBPROTOBUF_EXPORT \ - void* GenericTypeHandler::GetMaybeArenaPointer(TypeName* value); +#define DECLARE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES(TypeName) \ + template <> \ + PROTOBUF_EXPORT TypeName* GenericTypeHandler::NewFromPrototype( \ + const TypeName* prototype, Arena* arena); \ + template <> \ + PROTOBUF_EXPORT Arena* GenericTypeHandler::GetArena( \ + TypeName* value); \ + template <> \ + PROTOBUF_EXPORT void* GenericTypeHandler::GetMaybeArenaPointer( \ + TypeName* value); // Message specialization bodies defined in message.cc. This split is necessary // to allow proto2-lite (which includes this header) to be independent of @@ -736,32 +767,34 @@ DECLARE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES(Message) class StringTypeHandler { public: - typedef string Type; - typedef string WeakType; - static const bool Moveable = std::is_move_constructible::value && - std::is_move_assignable::value; + typedef std::string Type; + typedef std::string WeakType; + using Movable = IsMovable; - static inline string* New(Arena* arena) { - return Arena::Create(arena); + static inline std::string* New(Arena* arena) { + return Arena::Create(arena); } - static inline string* New(Arena* arena, string&& value) { - return Arena::Create(arena, std::move(value)); + static inline std::string* New(Arena* arena, std::string&& value) { + return Arena::Create(arena, std::move(value)); } - static inline string* NewFromPrototype(const string*, Arena* arena) { + static inline std::string* NewFromPrototype(const std::string*, + Arena* arena) { return New(arena); } - static inline Arena* GetArena(string*) { return NULL; } - static inline void* GetMaybeArenaPointer(string* /* value */) { + static inline Arena* GetArena(std::string*) { return NULL; } + static inline void* GetMaybeArenaPointer(std::string* /* value */) { return NULL; } - static inline void Delete(string* value, Arena* arena) { + static inline void Delete(std::string* value, Arena* arena) { if (arena == NULL) { delete value; } } - static inline void Clear(string* value) { value->clear(); } - static inline void Merge(const string& from, string* to) { *to = from; } - static size_t SpaceUsedLong(const string& 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); } }; @@ -797,6 +830,9 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase { 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(); @@ -856,15 +892,11 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase { // Reverse iterator support typedef std::reverse_iterator const_reverse_iterator; typedef std::reverse_iterator reverse_iterator; - reverse_iterator rbegin() { - return reverse_iterator(end()); - } + reverse_iterator rbegin() { return reverse_iterator(end()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } - reverse_iterator rend() { - return reverse_iterator(begin()); - } + reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } @@ -1034,23 +1066,15 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase { template inline RepeatedField::RepeatedField() - : current_size_(0), - total_size_(0), - ptr_(NULL) { -} + : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {} template inline RepeatedField::RepeatedField(Arena* arena) - : current_size_(0), - total_size_(0), - ptr_(arena) { -} + : current_size_(0), total_size_(0), arena_or_elements_(arena) {} template inline RepeatedField::RepeatedField(const RepeatedField& other) - : current_size_(0), - total_size_(0), - ptr_(NULL) { + : current_size_(0), total_size_(0), arena_or_elements_(nullptr) { if (other.current_size_ != 0) { Reserve(other.size()); AddNAlreadyReserved(other.size()); @@ -1061,20 +1085,8 @@ inline RepeatedField::RepeatedField(const RepeatedField& other) template template RepeatedField::RepeatedField(Iter begin, const Iter& end) - : current_size_(0), - total_size_(0), - ptr_(NULL) { - int reserve = internal::CalculateReserve(begin, end); - if (reserve != -1) { - Reserve(reserve); - for (; begin != end; ++begin) { - AddAlreadyReserved(*begin); - } - } else { - for (; begin != end; ++begin) { - Add(*begin); - } - } + : current_size_(0), total_size_(0), arena_or_elements_(nullptr) { + Add(begin, end); } template @@ -1085,10 +1097,9 @@ RepeatedField::~RepeatedField() { } template -inline RepeatedField& -RepeatedField::operator=(const RepeatedField& other) { - if (this != &other) - CopyFrom(other); +inline RepeatedField& RepeatedField::operator=( + const RepeatedField& other) { + if (this != &other) CopyFrom(other); return *this; } @@ -1096,7 +1107,8 @@ template inline RepeatedField::RepeatedField(RepeatedField&& other) noexcept : RepeatedField() { // We don't just call Swap(&other) here because it would perform 3 copies if - // the two fields are on different arenas. + // other is on an arena. This field can't be on an arena because arena + // construction always uses the Arena* accepting constructor. if (other.GetArenaNoVirtual()) { CopyFrom(other); } else { @@ -1134,34 +1146,32 @@ inline int RepeatedField::Capacity() const { return total_size_; } -template +template inline void RepeatedField::AddAlreadyReserved(const Element& value) { GOOGLE_DCHECK_LT(current_size_, total_size_); elements()[current_size_++] = value; } -template +template inline Element* RepeatedField::AddAlreadyReserved() { GOOGLE_DCHECK_LT(current_size_, total_size_); return &elements()[current_size_++]; } -template +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. This - // forces us to add this branch, to avoid reading the non-active union member - // (which is UB). Luckily the compiler is smart enough to optimize the branch - // away. - Element* ret = - total_size_ == 0 ? reinterpret_cast(ptr_.arena) : ptr_.elements; - ret += 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 +template inline void RepeatedField::Resize(int new_size, const Element& value) { GOOGLE_DCHECK_GE(new_size, 0); if (new_size > current_size_) { @@ -1178,6 +1188,20 @@ inline const Element& RepeatedField::Get(int index) const { 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); @@ -1204,6 +1228,31 @@ inline Element* RepeatedField::Add() { return &elements()[current_size_++]; } +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 { + for (; begin != end; ++begin) { + Add(*begin); + } + } +} + template inline void RepeatedField::RemoveLast() { GOOGLE_DCHECK_GT(current_size_, 0); @@ -1211,16 +1260,15 @@ inline void RepeatedField::RemoveLast() { } template -void RepeatedField::ExtractSubrange( - int start, int num, Element* elements) { +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); + for (int i = 0; i < num; ++i) elements[i] = this->Get(i + start); } // Slide remaining elements down to fill the gap. @@ -1272,17 +1320,12 @@ inline typename RepeatedField::iterator RepeatedField::erase( template inline Element* RepeatedField::mutable_data() { - return total_size_ > 0 ? elements() : NULL; + return unsafe_elements(); } template inline const Element* RepeatedField::data() const { - return total_size_ > 0 ? elements() : NULL; -} - -template -inline const Element* RepeatedField::unsafe_data() const { - return elements(); + return unsafe_elements(); } template @@ -1290,7 +1333,7 @@ inline void RepeatedField::InternalSwap(RepeatedField* other) { GOOGLE_DCHECK(this != other); GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - std::swap(ptr_, other->ptr_); + std::swap(arena_or_elements_, other->arena_or_elements_); std::swap(current_size_, other->current_size_); std::swap(total_size_, other->total_size_); } @@ -1323,32 +1366,31 @@ void RepeatedField::SwapElements(int index1, int index2) { template inline typename RepeatedField::iterator RepeatedField::begin() { - return total_size_ > 0 ? elements() : NULL; + return unsafe_elements(); } template inline typename RepeatedField::const_iterator RepeatedField::begin() const { - return total_size_ > 0 ? elements() : NULL; + return unsafe_elements(); } template inline typename RepeatedField::const_iterator RepeatedField::cbegin() const { - return total_size_ > 0 ? elements() : NULL; + return unsafe_elements(); } template -inline typename RepeatedField::iterator -RepeatedField::end() { - return total_size_ > 0 ? elements() + current_size_ : NULL; +inline typename RepeatedField::iterator RepeatedField::end() { + return unsafe_elements() + current_size_; } template inline typename RepeatedField::const_iterator RepeatedField::end() const { - return total_size_ > 0 ? elements() + current_size_ : NULL; + return unsafe_elements() + current_size_; } template inline typename RepeatedField::const_iterator RepeatedField::cend() const { - return total_size_ > 0 ? elements() + current_size_ : NULL; + return unsafe_elements() + current_size_; } template @@ -1370,7 +1412,8 @@ void RepeatedField::Reserve(int new_size) { 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); + size_t bytes = + kRepHeaderSize + sizeof(Element) * static_cast(new_size); if (arena == NULL) { new_rep = static_cast(::operator new(bytes)); } else { @@ -1379,7 +1422,7 @@ void RepeatedField::Reserve(int new_size) { new_rep->arena = arena; int old_total_size = total_size_; total_size_ = new_size; - ptr_.elements = new_rep->elements; + 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 @@ -1412,22 +1455,23 @@ inline void RepeatedField::Truncate(int new_size) { } template -inline void RepeatedField::MoveArray( - Element* to, Element* from, int array_size) { +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) { +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) { +void ElementCopier::operator()(Element* to, + const Element* from, + int array_size) { std::copy(from, from + array_size, to); } @@ -1446,11 +1490,7 @@ struct ElementCopier { namespace internal { inline RepeatedPtrFieldBase::RepeatedPtrFieldBase() - : arena_(NULL), - current_size_(0), - total_size_(0), - rep_(NULL) { -} + : 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) {} @@ -1498,25 +1538,35 @@ void RepeatedPtrFieldBase::SwapFallback(RepeatedPtrFieldBase* other) { temp.Destroy(); // Frees rep_ if `other` had no arena. } -inline bool RepeatedPtrFieldBase::empty() const { - return current_size_ == 0; -} +inline bool RepeatedPtrFieldBase::empty() const { return current_size_ == 0; } -inline int RepeatedPtrFieldBase::size() const { - return current_size_; -} +inline int RepeatedPtrFieldBase::size() const { return current_size_; } template -inline const typename TypeHandler::WeakType& -RepeatedPtrFieldBase::Get(int index) const { +inline const typename TypeHandler::WeakType& RepeatedPtrFieldBase::Get( + int index) const { GOOGLE_DCHECK_GE(index, 0); GOOGLE_DCHECK_LT(index, current_size_); return *cast(rep_->elements[index]); } template -inline typename TypeHandler::Type* -RepeatedPtrFieldBase::Mutable(int index) { +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]); @@ -1545,10 +1595,9 @@ inline typename TypeHandler::Type* RepeatedPtrFieldBase::Add( return result; } -template -inline void RepeatedPtrFieldBase::Add( - typename TypeHandler::Type&& value, - typename std::enable_if::type*) { +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; @@ -1592,8 +1641,8 @@ template inline void RepeatedPtrFieldBase::MergeFrom(const RepeatedPtrFieldBase& other) { GOOGLE_DCHECK_NE(&other, this); if (other.current_size_ == 0) return; - MergeFromInternal( - other, &RepeatedPtrFieldBase::MergeFromInnerLoop); + MergeFromInternal(other, + &RepeatedPtrFieldBase::MergeFromInnerLoop); } inline void RepeatedPtrFieldBase::MergeFromInternal( @@ -1604,8 +1653,8 @@ inline void RepeatedPtrFieldBase::MergeFromInternal( 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); + (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_; @@ -1613,9 +1662,10 @@ inline void RepeatedPtrFieldBase::MergeFromInternal( } // Merges other_elems to our_elems. -template -void RepeatedPtrFieldBase::MergeFromInnerLoop( - void** our_elems, void** other_elems, int length, int already_allocated) { +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++) { @@ -1645,9 +1695,7 @@ inline void RepeatedPtrFieldBase::CopyFrom(const RepeatedPtrFieldBase& other) { RepeatedPtrFieldBase::MergeFrom(other); } -inline int RepeatedPtrFieldBase::Capacity() const { - return total_size_; -} +inline int RepeatedPtrFieldBase::Capacity() const { return total_size_; } inline void* const* RepeatedPtrFieldBase::raw_data() const { return rep_ ? rep_->elements : NULL; @@ -1665,8 +1713,8 @@ inline typename TypeHandler::Type** RepeatedPtrFieldBase::mutable_data() { } template -inline const typename TypeHandler::Type* const* -RepeatedPtrFieldBase::data() const { +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()); @@ -1682,8 +1730,8 @@ 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 += + TypeHandler::SpaceUsedLong(*cast(rep_->elements[i])); } allocated_bytes += kRepHeaderSize; } @@ -1702,13 +1750,11 @@ inline typename TypeHandler::Type* RepeatedPtrFieldBase::AddFromCleared() { // 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)); + typename TypeHandler::Type* value, std::true_type) { + Arena* element_arena = + reinterpret_cast(TypeHandler::GetMaybeArenaPointer(value)); Arena* arena = GetArenaNoVirtual(); - if (arena == element_arena && rep_ && - rep_->allocated_size < total_size_) { + 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). @@ -1722,13 +1768,13 @@ void RepeatedPtrFieldBase::AddAllocatedInternal( current_size_ = current_size_ + 1; rep_->allocated_size = rep_->allocated_size + 1; } else { - AddAllocatedSlowWithCopy( - value, TypeHandler::GetArena(value), arena); + AddAllocatedSlowWithCopy(value, TypeHandler::GetArena(value), + arena); } } // Slowpath handles all cases, copying if necessary. -template +template void RepeatedPtrFieldBase::AddAllocatedSlowWithCopy( // Pass value_arena and my_arena to avoid duplicate virtual call (value) or // load (mine). @@ -1752,9 +1798,8 @@ void RepeatedPtrFieldBase::AddAllocatedSlowWithCopy( // 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_) { + 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). @@ -1785,8 +1830,8 @@ void RepeatedPtrFieldBase::UnsafeArenaAddAllocated( // 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_); + 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. @@ -1802,8 +1847,8 @@ void RepeatedPtrFieldBase::UnsafeArenaAddAllocated( // ReleaseLast() for types that implement merge/copy behavior. template -inline typename TypeHandler::Type* -RepeatedPtrFieldBase::ReleaseLastInternal(std::true_type) { +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. @@ -1823,8 +1868,8 @@ RepeatedPtrFieldBase::ReleaseLastInternal(std::true_type) { // an arena, since the user really should implement the copy operation in this // case. template -inline typename TypeHandler::Type* -RepeatedPtrFieldBase::ReleaseLastInternal(std::false_type) { +inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal( + std::false_type) { GOOGLE_DCHECK(GetArenaNoVirtual() == NULL) << "ReleaseLast() called on a RepeatedPtrField that is on an arena, " << "with a type that does not implement MergeFrom. This is unsafe; " @@ -1834,7 +1879,7 @@ RepeatedPtrFieldBase::ReleaseLastInternal(std::false_type) { template inline typename TypeHandler::Type* - RepeatedPtrFieldBase::UnsafeArenaReleaseLast() { +RepeatedPtrFieldBase::UnsafeArenaReleaseLast() { GOOGLE_DCHECK_GT(current_size_, 0); typename TypeHandler::Type* result = cast(rep_->elements[--current_size_]); @@ -1881,17 +1926,14 @@ inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseCleared() { template class RepeatedPtrField::TypeHandler - : public internal::GenericTypeHandler { -}; + : public internal::GenericTypeHandler {}; template <> -class RepeatedPtrField::TypeHandler - : public internal::StringTypeHandler { -}; +class RepeatedPtrField::TypeHandler + : public internal::StringTypeHandler {}; template -inline RepeatedPtrField::RepeatedPtrField() - : RepeatedPtrFieldBase() {} +inline RepeatedPtrField::RepeatedPtrField() : RepeatedPtrFieldBase() {} template inline RepeatedPtrField::RepeatedPtrField(Arena* arena) @@ -1900,14 +1942,14 @@ inline RepeatedPtrField::RepeatedPtrField(Arena* arena) template inline RepeatedPtrField::RepeatedPtrField( const RepeatedPtrField& other) - : RepeatedPtrFieldBase() { + : RepeatedPtrFieldBase() { MergeFrom(other); } template template -inline RepeatedPtrField::RepeatedPtrField( - Iter begin, const Iter& end) { +inline RepeatedPtrField::RepeatedPtrField(Iter begin, + const Iter& end) { int reserve = internal::CalculateReserve(begin, end); if (reserve != -1) { Reserve(reserve); @@ -1925,8 +1967,7 @@ RepeatedPtrField::~RepeatedPtrField() { template inline RepeatedPtrField& RepeatedPtrField::operator=( const RepeatedPtrField& other) { - if (this != &other) - CopyFrom(other); + if (this != &other) CopyFrom(other); return *this; } @@ -1935,7 +1976,8 @@ inline RepeatedPtrField::RepeatedPtrField( RepeatedPtrField&& other) noexcept : RepeatedPtrField() { // We don't just call Swap(&other) here because it would perform 3 copies if - // the two fields are on different arenas. + // other is on an arena. This field can't be on an arena because arena + // construction always uses the Arena* accepting constructor. if (other.GetArenaNoVirtual()) { CopyFrom(other); } else { @@ -1973,6 +2015,16 @@ 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) { @@ -2006,8 +2058,8 @@ inline void RepeatedPtrField::DeleteSubrange(int start, int num) { } template -inline void RepeatedPtrField::ExtractSubrange( - int start, int num, Element** elements) { +inline void RepeatedPtrField::ExtractSubrange(int start, int num, + Element** elements) { typename internal::TypeImplementsMergeBehavior< typename TypeHandler::Type>::type t; ExtractSubrangeInternal(start, num, elements, t); @@ -2029,8 +2081,8 @@ inline void RepeatedPtrField::ExtractSubrangeInternal( // 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); + Element* element = + RepeatedPtrFieldBase::Mutable(i + start); typename TypeHandler::Type* new_value = TypeHandler::NewFromPrototype(element, NULL); TypeHandler::Merge(*element, new_value); @@ -2048,7 +2100,7 @@ inline void RepeatedPtrField::ExtractSubrangeInternal( // ExtractSubrange() implementation for types that do not implement merge/copy // behavior. -template +template inline void RepeatedPtrField::ExtractSubrangeInternal( int start, int num, Element** elements, std::false_type) { // This case is identical to UnsafeArenaExtractSubrange(). However, since @@ -2091,8 +2143,7 @@ inline void RepeatedPtrField::MergeFrom( } template -inline void RepeatedPtrField::CopyFrom( - const RepeatedPtrField& other) { +inline void RepeatedPtrField::CopyFrom(const RepeatedPtrField& other) { RepeatedPtrFieldBase::CopyFrom(other); } @@ -2123,16 +2174,14 @@ inline const Element* const* RepeatedPtrField::data() const { template inline void RepeatedPtrField::Swap(RepeatedPtrField* other) { - if (this == other) - return; + if (this == other) return; RepeatedPtrFieldBase::Swap(other); } template inline void RepeatedPtrField::UnsafeArenaSwap( RepeatedPtrField* other) { - if (this == other) - return; + if (this == other) return; RepeatedPtrFieldBase::InternalSwap(other); } @@ -2211,48 +2260,45 @@ namespace internal { // // This code based on net/proto/proto-array-internal.h by Jeffrey Yasskin // (jyasskin@google.com). -template -class RepeatedPtrIterator - : public std::iterator< - std::random_access_iterator_tag, Element> { +template +class RepeatedPtrIterator { public: - typedef RepeatedPtrIterator iterator; - typedef std::iterator< - std::random_access_iterator_tag, Element> superclass; - - // Shadow the value_type in std::iterator<> because const_iterator::value_type - // needs to be T, not const T. - typedef typename std::remove_const::type value_type; - - // Let the compiler know that these are type names, so we don't have to - // write "typename" in front of them everywhere. - typedef typename superclass::reference reference; - typedef typename superclass::pointer pointer; - typedef typename superclass::difference_type difference_type; + 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 + template RepeatedPtrIterator(const RepeatedPtrIterator& other) : it_(other.it_) { // Force a compiler error if the other type is not convertible to ours. if (false) { - ::google::protobuf::implicit_cast(static_cast(nullptr)); + implicit_cast(static_cast(nullptr)); } } // dereferenceable reference operator*() const { return *reinterpret_cast(*it_); } - pointer operator->() const { return &(operator*()); } + 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_--); } + 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_; } @@ -2293,7 +2339,7 @@ class RepeatedPtrIterator difference_type operator-(const iterator& x) const { return it_ - x.it_; } private: - template + template friend class RepeatedPtrIterator; // The internal iterator. @@ -2308,34 +2354,33 @@ class RepeatedPtrIterator // 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 std::iterator { +class RepeatedPtrOverPtrsIterator { public: - typedef RepeatedPtrOverPtrsIterator iterator; - typedef std::iterator superclass; - - // Shadow the value_type in std::iterator<> because const_iterator::value_type - // needs to be T, not const T. - typedef typename std::remove_const::type value_type; - - // Let the compiler know that these are type names, so we don't have to - // write "typename" in front of them everywhere. - typedef typename superclass::reference reference; - typedef typename superclass::pointer pointer; - typedef typename superclass::difference_type difference_type; + 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*()); } + 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_--); } + 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_; } @@ -2376,7 +2421,7 @@ class RepeatedPtrOverPtrsIterator difference_type operator-(const iterator& x) const { return it_ - x.it_; } private: - template + template friend class RepeatedPtrIterator; // The internal iterator. @@ -2458,23 +2503,19 @@ RepeatedPtrField::pointer_end() const { namespace internal { // A back inserter for RepeatedField objects. -template class RepeatedFieldBackInsertIterator +template +class RepeatedFieldBackInsertIterator : public std::iterator { public: explicit RepeatedFieldBackInsertIterator( RepeatedField* const mutable_field) - : field_(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*() { return *this; } + RepeatedFieldBackInsertIterator& operator++() { return *this; } RepeatedFieldBackInsertIterator& operator++(int /* unused */) { return *this; } @@ -2484,13 +2525,12 @@ template class RepeatedFieldBackInsertIterator }; // A back inserter for RepeatedPtrField objects. -template class RepeatedPtrFieldBackInsertIterator +template +class RepeatedPtrFieldBackInsertIterator : public std::iterator { public: - RepeatedPtrFieldBackInsertIterator( - RepeatedPtrField* const mutable_field) - : field_(mutable_field) { - } + RepeatedPtrFieldBackInsertIterator(RepeatedPtrField* const mutable_field) + : field_(mutable_field) {} RepeatedPtrFieldBackInsertIterator& operator=(const T& value) { *field_->Add() = value; return *this; @@ -2504,12 +2544,8 @@ template class RepeatedPtrFieldBackInsertIterator *field_->Add() = std::move(value); return *this; } - RepeatedPtrFieldBackInsertIterator& operator*() { - return *this; - } - RepeatedPtrFieldBackInsertIterator& operator++() { - return *this; - } + RepeatedPtrFieldBackInsertIterator& operator*() { return *this; } + RepeatedPtrFieldBackInsertIterator& operator++() { return *this; } RepeatedPtrFieldBackInsertIterator& operator++(int /* unused */) { return *this; } @@ -2520,26 +2556,21 @@ template class RepeatedPtrFieldBackInsertIterator // A back inserter for RepeatedPtrFields that inserts by transferring ownership // of a pointer. -template class AllocatedRepeatedPtrFieldBackInsertIterator +template +class AllocatedRepeatedPtrFieldBackInsertIterator : public std::iterator { public: explicit AllocatedRepeatedPtrFieldBackInsertIterator( RepeatedPtrField* const mutable_field) - : field_(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 */) { + AllocatedRepeatedPtrFieldBackInsertIterator& operator*() { return *this; } + AllocatedRepeatedPtrFieldBackInsertIterator& operator++() { return *this; } + AllocatedRepeatedPtrFieldBackInsertIterator& operator++(int /* unused */) { return *this; } @@ -2549,7 +2580,7 @@ template class AllocatedRepeatedPtrFieldBackInsertIterator // Almost identical to AllocatedRepeatedPtrFieldBackInsertIterator. This one // uses the UnsafeArenaAddAllocated instead. -template +template class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator : public std::iterator { public: @@ -2557,7 +2588,7 @@ class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator RepeatedPtrField* const mutable_field) : field_(mutable_field) {} UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator& operator=( - T const* const ptr_to_value) { + T const* const ptr_to_value) { field_->UnsafeArenaAddAllocated(const_cast(ptr_to_value)); return *this; } @@ -2580,30 +2611,34 @@ class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator // Provides a back insert iterator for RepeatedField instances, // similar to std::back_inserter(). -template internal::RepeatedFieldBackInsertIterator -RepeatedFieldBackInserter(RepeatedField* const mutable_field) { +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) { +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) { +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 +template +internal::AllocatedRepeatedPtrFieldBackInsertIterator AllocatedRepeatedPtrFieldBackInserter( RepeatedPtrField* const mutable_field) { return internal::AllocatedRepeatedPtrFieldBackInsertIterator( @@ -2630,14 +2665,14 @@ UnsafeArenaAllocatedRepeatedPtrFieldBackInserter( } // Extern declarations of common instantiations to reduce libray bloat. -extern template class LIBPROTOBUF_EXPORT RepeatedField; -extern template class LIBPROTOBUF_EXPORT RepeatedField; -extern template class LIBPROTOBUF_EXPORT RepeatedField; -extern template class LIBPROTOBUF_EXPORT RepeatedField; -extern template class LIBPROTOBUF_EXPORT RepeatedField; -extern template class LIBPROTOBUF_EXPORT RepeatedField; -extern template class LIBPROTOBUF_EXPORT RepeatedField; -extern template class LIBPROTOBUF_EXPORT RepeatedPtrField; +extern template class PROTOBUF_EXPORT RepeatedField; +extern template class PROTOBUF_EXPORT RepeatedField; +extern template class PROTOBUF_EXPORT RepeatedField; +extern template class PROTOBUF_EXPORT RepeatedField; +extern template class PROTOBUF_EXPORT RepeatedField; +extern template class PROTOBUF_EXPORT RepeatedField; +extern template class PROTOBUF_EXPORT RepeatedField; +extern template class PROTOBUF_EXPORT RepeatedPtrField; } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/repeated_field_reflection_unittest.cc b/src/google/protobuf/repeated_field_reflection_unittest.cc index 74ec83b050..04991027ee 100644 --- a/src/google/protobuf/repeated_field_reflection_unittest.cc +++ b/src/google/protobuf/repeated_field_reflection_unittest.cc @@ -35,9 +35,9 @@ #include #include -#include -#include #include +#include +#include #include #include @@ -45,17 +45,15 @@ namespace google { namespace protobuf { using unittest::ForeignMessage; -using unittest::TestAllTypes; using unittest::TestAllExtensions; +using unittest::TestAllTypes; namespace { -static int Func(int i, int j) { - return i * j; -} +static int Func(int i, int j) { return i * j; } -static string StrFunc(int i, int j) { - string str; +static std::string StrFunc(int i, int j) { + std::string str; SStringPrintf(&str, "%d", Func(i, 4)); return str; } @@ -95,24 +93,23 @@ TEST(RepeatedFieldReflectionTest, RegularFields) { refl->MutableRepeatedField(&message, fd_repeated_double); // Get RepeatedPtrField objects for all fields of interest. - const RepeatedPtrField& rpf_string = - refl->GetRepeatedPtrField(message, fd_repeated_string); + const RepeatedPtrField& rpf_string = + refl->GetRepeatedPtrField(message, fd_repeated_string); const RepeatedPtrField& rpf_foreign_message = - refl->GetRepeatedPtrField( - message, fd_repeated_foreign_message); + refl->GetRepeatedPtrField(message, + fd_repeated_foreign_message); const RepeatedPtrField& rpf_message = - refl->GetRepeatedPtrField( - message, fd_repeated_foreign_message); + refl->GetRepeatedPtrField(message, fd_repeated_foreign_message); // Get mutable RepeatedPtrField objects for all fields of interest. - RepeatedPtrField* mrpf_string = - refl->MutableRepeatedPtrField(&message, fd_repeated_string); + RepeatedPtrField* mrpf_string = + refl->MutableRepeatedPtrField(&message, fd_repeated_string); RepeatedPtrField* mrpf_foreign_message = refl->MutableRepeatedPtrField( &message, fd_repeated_foreign_message); RepeatedPtrField* mrpf_message = - refl->MutableRepeatedPtrField( - &message, fd_repeated_foreign_message); + refl->MutableRepeatedPtrField(&message, + fd_repeated_foreign_message); // Make sure we can do gets and sets through the Repeated[Ptr]Field objects. for (int i = 0; i < 10; ++i) { @@ -149,12 +146,13 @@ TEST(RepeatedFieldReflectionTest, RegularFields) { // Make sure types are checked correctly at runtime. const FieldDescriptor* fd_optional_int32 = desc->FindFieldByName("optional_int32"); - EXPECT_DEATH(refl->GetRepeatedField( - message, fd_optional_int32), "requires a repeated field"); - EXPECT_DEATH(refl->GetRepeatedField( - message, fd_repeated_int32), "not the right type"); + EXPECT_DEATH(refl->GetRepeatedField(message, fd_optional_int32), + "requires a repeated field"); + EXPECT_DEATH(refl->GetRepeatedField(message, fd_repeated_int32), + "not the right type"); EXPECT_DEATH(refl->GetRepeatedPtrField( - message, fd_repeated_foreign_message), "wrong submessage type"); + message, fd_repeated_foreign_message), + "wrong submessage type"); #endif // PROTOBUF_HAS_DEATH_TEST } @@ -165,8 +163,8 @@ TEST(RepeatedFieldReflectionTest, ExtensionFields) { const Descriptor* desc = extended_message.GetDescriptor(); for (int i = 0; i < 10; ++i) { - extended_message.AddExtension( - unittest::repeated_int64_extension, Func(i, 1)); + extended_message.AddExtension(unittest::repeated_int64_extension, + Func(i, 1)); } const FieldDescriptor* fd_repeated_int64_extension = @@ -177,15 +175,14 @@ TEST(RepeatedFieldReflectionTest, ExtensionFields) { refl->GetRepeatedField(extended_message, fd_repeated_int64_extension); - RepeatedField* mrf_int64_extension = - refl->MutableRepeatedField(&extended_message, - fd_repeated_int64_extension); + RepeatedField* mrf_int64_extension = refl->MutableRepeatedField( + &extended_message, fd_repeated_int64_extension); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i)); mrf_int64_extension->Set(i, Func(i, -1)); - EXPECT_EQ(Func(i, -1), - extended_message.GetExtension(unittest::repeated_int64_extension, i)); + EXPECT_EQ(Func(i, -1), extended_message.GetExtension( + unittest::repeated_int64_extension, i)); } } @@ -194,8 +191,8 @@ void TestRepeatedFieldRefIteratorForPrimitive( const Ref& handle, const MessageType& message, ValueType (MessageType::*GetFunc)(int) const) { int index = 0; - for (typename Ref::const_iterator it = handle.begin(); - it != handle.end(); ++it) { + for (typename Ref::const_iterator it = handle.begin(); it != handle.end(); + ++it) { EXPECT_EQ((message.*GetFunc)(index), *it); ++index; } @@ -204,10 +201,11 @@ void TestRepeatedFieldRefIteratorForPrimitive( template void TestRepeatedFieldRefIteratorForString( - const RepeatedFieldRef& handle, const MessageType& message, + const RepeatedFieldRef& handle, const MessageType& message, ValueType (MessageType::*GetFunc)(int) const) { int index = 0; - for (typename RepeatedFieldRef::const_iterator it = handle.begin(); + for (typename RepeatedFieldRef::const_iterator it = + handle.begin(); it != handle.end(); ++it) { // Test both operator* and operator-> EXPECT_EQ((message.*GetFunc)(index), *it); @@ -244,28 +242,28 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { refl->GetRepeatedFieldRef(message, fd_repeated_int32); const RepeatedFieldRef rf_double = refl->GetRepeatedFieldRef(message, fd_repeated_double); - const RepeatedFieldRef rf_string = - refl->GetRepeatedFieldRef(message, fd_repeated_string); + const RepeatedFieldRef rf_string = + refl->GetRepeatedFieldRef(message, fd_repeated_string); const RepeatedFieldRef rf_foreign_message = - refl->GetRepeatedFieldRef( - message, fd_repeated_foreign_message); + refl->GetRepeatedFieldRef(message, + fd_repeated_foreign_message); const RepeatedFieldRef rf_message = - refl->GetRepeatedFieldRef( - message, fd_repeated_foreign_message); + refl->GetRepeatedFieldRef(message, fd_repeated_foreign_message); // Get MutableRepeatedFieldRef objects for all fields of interest. const MutableRepeatedFieldRef mrf_int32 = refl->GetMutableRepeatedFieldRef(&message, fd_repeated_int32); const MutableRepeatedFieldRef mrf_double = refl->GetMutableRepeatedFieldRef(&message, fd_repeated_double); - const MutableRepeatedFieldRef mrf_string = - refl->GetMutableRepeatedFieldRef(&message, fd_repeated_string); + const MutableRepeatedFieldRef mrf_string = + refl->GetMutableRepeatedFieldRef(&message, + fd_repeated_string); const MutableRepeatedFieldRef mrf_foreign_message = refl->GetMutableRepeatedFieldRef( &message, fd_repeated_foreign_message); const MutableRepeatedFieldRef mrf_message = - refl->GetMutableRepeatedFieldRef( - &message, fd_repeated_foreign_message); + refl->GetMutableRepeatedFieldRef(&message, + fd_repeated_foreign_message); EXPECT_EQ(message.repeated_int32_size(), rf_int32.size()); EXPECT_EQ(message.repeated_int32_size(), mrf_int32.size()); @@ -273,8 +271,7 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(message.repeated_double_size(), mrf_double.size()); EXPECT_EQ(message.repeated_string_size(), rf_string.size()); EXPECT_EQ(message.repeated_string_size(), mrf_string.size()); - EXPECT_EQ(message.repeated_foreign_message_size(), - rf_foreign_message.size()); + EXPECT_EQ(message.repeated_foreign_message_size(), rf_foreign_message.size()); EXPECT_EQ(message.repeated_foreign_message_size(), mrf_foreign_message.size()); EXPECT_EQ(message.repeated_foreign_message_size(), rf_message.size()); @@ -299,16 +296,19 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(rf_string.Get(i), StrFunc(i, 5)); ForeignMessage scratch_space; EXPECT_EQ(rf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6)); - EXPECT_EQ(down_cast( - rf_message.Get(i, &scratch_space)).c(), Func(i, 6)); + EXPECT_EQ( + down_cast(rf_message.Get(i, &scratch_space)).c(), + Func(i, 6)); // Check gets through mutable objects. EXPECT_EQ(mrf_int32.Get(i), Func(i, 1)); EXPECT_EQ(mrf_double.Get(i), Func(i, 2)); EXPECT_EQ(mrf_string.Get(i), StrFunc(i, 5)); EXPECT_EQ(mrf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6)); - EXPECT_EQ(down_cast( - mrf_message.Get(i, &scratch_space)).c(), Func(i, 6)); + EXPECT_EQ( + down_cast(mrf_message.Get(i, &scratch_space)) + .c(), + Func(i, 6)); // Check sets through mutable objects. mrf_int32.Set(i, Func(i, -1)); @@ -429,12 +429,13 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { // Make sure types are checked correctly at runtime. const FieldDescriptor* fd_optional_int32 = desc->FindFieldByName("optional_int32"); - EXPECT_DEATH(refl->GetRepeatedFieldRef( - message, fd_optional_int32), ""); - EXPECT_DEATH(refl->GetRepeatedFieldRef( - message, fd_repeated_int32), ""); + EXPECT_DEATH(refl->GetRepeatedFieldRef(message, fd_optional_int32), + ""); + EXPECT_DEATH(refl->GetRepeatedFieldRef(message, fd_repeated_int32), + ""); EXPECT_DEATH(refl->GetRepeatedFieldRef( - message, fd_repeated_foreign_message), ""); + message, fd_repeated_foreign_message), + ""); #endif // PROTOBUF_HAS_DEATH_TEST } @@ -453,16 +454,14 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForEnums) { const RepeatedFieldRef enum_ref = refl->GetRepeatedFieldRef( message, fd_repeated_nested_enum); - const MutableRepeatedFieldRef - mutable_enum_ref = + const MutableRepeatedFieldRef mutable_enum_ref = refl->GetMutableRepeatedFieldRef( &message, fd_repeated_nested_enum); const RepeatedFieldRef int32_ref = - refl->GetRepeatedFieldRef( - message, fd_repeated_nested_enum); + refl->GetRepeatedFieldRef(message, fd_repeated_nested_enum); const MutableRepeatedFieldRef mutable_int32_ref = - refl->GetMutableRepeatedFieldRef( - &message, fd_repeated_nested_enum); + refl->GetMutableRepeatedFieldRef(&message, + fd_repeated_nested_enum); EXPECT_EQ(message.repeated_nested_enum_size(), enum_ref.size()); EXPECT_EQ(message.repeated_nested_enum_size(), mutable_enum_ref.size()); @@ -537,8 +536,8 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForExtensionFields) { const Descriptor* desc = extended_message.GetDescriptor(); for (int i = 0; i < 10; ++i) { - extended_message.AddExtension( - unittest::repeated_int64_extension, Func(i, 1)); + extended_message.AddExtension(unittest::repeated_int64_extension, + Func(i, 1)); } const FieldDescriptor* fd_repeated_int64_extension = @@ -547,17 +546,17 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForExtensionFields) { const RepeatedFieldRef rf_int64_extension = refl->GetRepeatedFieldRef(extended_message, - fd_repeated_int64_extension); + fd_repeated_int64_extension); const MutableRepeatedFieldRef mrf_int64_extension = refl->GetMutableRepeatedFieldRef(&extended_message, - fd_repeated_int64_extension); + fd_repeated_int64_extension); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i)); mrf_int64_extension.Set(i, Func(i, -1)); - EXPECT_EQ(Func(i, -1), - extended_message.GetExtension(unittest::repeated_int64_extension, i)); + EXPECT_EQ(Func(i, -1), extended_message.GetExtension( + unittest::repeated_int64_extension, i)); } } @@ -603,29 +602,25 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { refl->GetMutableRepeatedFieldRef(&m0, fd_repeated_int32); const MutableRepeatedFieldRef mrf_double = refl->GetMutableRepeatedFieldRef(&m0, fd_repeated_double); - const MutableRepeatedFieldRef mrf_string = - refl->GetMutableRepeatedFieldRef(&m0, fd_repeated_string); + const MutableRepeatedFieldRef mrf_string = + refl->GetMutableRepeatedFieldRef(&m0, fd_repeated_string); const MutableRepeatedFieldRef mrf_foreign_message = refl->GetMutableRepeatedFieldRef( &m0, fd_repeated_foreign_message); - const MutableRepeatedFieldRef - mrf_nested_enum = + const MutableRepeatedFieldRef mrf_nested_enum = refl->GetMutableRepeatedFieldRef( &m0, fd_repeated_nested_enum); // Test MutableRepeatedRef::CopyFrom - mrf_int32.CopyFrom( - refl->GetRepeatedFieldRef(m1, fd_repeated_int32)); + mrf_int32.CopyFrom(refl->GetRepeatedFieldRef(m1, fd_repeated_int32)); mrf_double.CopyFrom( refl->GetRepeatedFieldRef(m1, fd_repeated_double)); mrf_string.CopyFrom( - refl->GetRepeatedFieldRef(m1, fd_repeated_string)); - mrf_foreign_message.CopyFrom( - refl->GetRepeatedFieldRef( - m1, fd_repeated_foreign_message)); - mrf_nested_enum.CopyFrom( - refl->GetRepeatedFieldRef( - m1, fd_repeated_nested_enum)); + refl->GetRepeatedFieldRef(m1, fd_repeated_string)); + mrf_foreign_message.CopyFrom(refl->GetRepeatedFieldRef( + m1, fd_repeated_foreign_message)); + mrf_nested_enum.CopyFrom(refl->GetRepeatedFieldRef( + m1, fd_repeated_nested_enum)); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 11), m0.repeated_int32(i)); EXPECT_EQ(Func(i, 12), m0.repeated_double(i)); @@ -635,18 +630,15 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { } // Test MutableRepeatedRef::MergeFrom - mrf_int32.MergeFrom( - refl->GetRepeatedFieldRef(m2, fd_repeated_int32)); + mrf_int32.MergeFrom(refl->GetRepeatedFieldRef(m2, fd_repeated_int32)); mrf_double.MergeFrom( refl->GetRepeatedFieldRef(m2, fd_repeated_double)); mrf_string.MergeFrom( - refl->GetRepeatedFieldRef(m2, fd_repeated_string)); - mrf_foreign_message.MergeFrom( - refl->GetRepeatedFieldRef( - m2, fd_repeated_foreign_message)); - mrf_nested_enum.MergeFrom( - refl->GetRepeatedFieldRef( - m2, fd_repeated_nested_enum)); + refl->GetRepeatedFieldRef(m2, fd_repeated_string)); + mrf_foreign_message.MergeFrom(refl->GetRepeatedFieldRef( + m2, fd_repeated_foreign_message)); + mrf_nested_enum.MergeFrom(refl->GetRepeatedFieldRef( + m2, fd_repeated_nested_enum)); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 21), m0.repeated_int32(i + 10)); EXPECT_EQ(Func(i, 22), m0.repeated_double(i + 10)); @@ -662,10 +654,9 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { mrf_double.Swap( refl->GetMutableRepeatedFieldRef(&m2, fd_repeated_double)); mrf_string.Swap( - refl->GetMutableRepeatedFieldRef(&m2, fd_repeated_string)); - mrf_foreign_message.Swap( - refl->GetMutableRepeatedFieldRef( - &m2, fd_repeated_foreign_message)); + refl->GetMutableRepeatedFieldRef(&m2, fd_repeated_string)); + mrf_foreign_message.Swap(refl->GetMutableRepeatedFieldRef( + &m2, fd_repeated_foreign_message)); mrf_nested_enum.Swap( refl->GetMutableRepeatedFieldRef( &m2, fd_repeated_nested_enum)); @@ -708,12 +699,12 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefDynamicMessage) { const Reflection* refl = dynamic_message->GetReflection(); MutableRepeatedFieldRef rf_int32 = - refl->GetMutableRepeatedFieldRef( - dynamic_message.get(), fd_repeated_int32); + refl->GetMutableRepeatedFieldRef(dynamic_message.get(), + fd_repeated_int32); rf_int32.Add(1234); EXPECT_EQ(1, refl->FieldSize(*dynamic_message, fd_repeated_int32)); - EXPECT_EQ(1234, refl->GetRepeatedInt32(*dynamic_message, - fd_repeated_int32, 0)); + EXPECT_EQ(1234, + refl->GetRepeatedInt32(*dynamic_message, fd_repeated_int32, 0)); } } // namespace diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc index fa86c593b3..dd79d42e15 100644 --- a/src/google/protobuf/repeated_field_unittest.cc +++ b/src/google/protobuf/repeated_field_unittest.cc @@ -35,13 +35,15 @@ // TODO(kenton): Improve this unittest to bring it up to the standards of // other proto2 unittests. +#include + #include #include #include +#include +#include #include -#include - #include #include #include @@ -49,6 +51,7 @@ #include #include #include + #include namespace google { @@ -70,26 +73,41 @@ TEST(RepeatedField, Small) { EXPECT_FALSE(field.empty()); EXPECT_EQ(field.size(), 1); EXPECT_EQ(field.Get(0), 5); + EXPECT_EQ(field.at(0), 5); field.Add(42); EXPECT_FALSE(field.empty()); EXPECT_EQ(field.size(), 2); EXPECT_EQ(field.Get(0), 5); + EXPECT_EQ(field.at(0), 5); EXPECT_EQ(field.Get(1), 42); + EXPECT_EQ(field.at(1), 42); field.Set(1, 23); EXPECT_FALSE(field.empty()); EXPECT_EQ(field.size(), 2); EXPECT_EQ(field.Get(0), 5); + EXPECT_EQ(field.at(0), 5); EXPECT_EQ(field.Get(1), 23); + EXPECT_EQ(field.at(1), 23); + + field.at(1) = 25; + + EXPECT_FALSE(field.empty()); + EXPECT_EQ(field.size(), 2); + EXPECT_EQ(field.Get(0), 5); + EXPECT_EQ(field.at(0), 5); + EXPECT_EQ(field.Get(1), 25); + EXPECT_EQ(field.at(1), 25); field.RemoveLast(); EXPECT_FALSE(field.empty()); EXPECT_EQ(field.size(), 1); EXPECT_EQ(field.Get(0), 5); + EXPECT_EQ(field.at(0), 5); field.Clear(); @@ -326,6 +344,71 @@ TEST(RepeatedField, Erase) { EXPECT_EQ(8, me.Get(2)); } +// Add contents of empty container to an empty field. +TEST(RepeatedField, AddRange1) { + RepeatedField me; + std::vector values; + + me.Add(values.begin(), values.end()); + ASSERT_EQ(me.size(), 0); +} + +// Add contents of container with one thing to an empty field. +TEST(RepeatedField, AddRange2) { + RepeatedField me; + std::vector values; + values.push_back(-1); + + me.Add(values.begin(), values.end()); + ASSERT_EQ(me.size(), 1); + ASSERT_EQ(me.Get(0), values[0]); +} + +// Add contents of container with more than one thing to an empty field. +TEST(RepeatedField, AddRange3) { + RepeatedField me; + std::vector values; + values.push_back(0); + values.push_back(1); + + me.Add(values.begin(), values.end()); + ASSERT_EQ(me.size(), 2); + ASSERT_EQ(me.Get(0), values[0]); + ASSERT_EQ(me.Get(1), values[1]); +} + +// Add contents of container with more than one thing to a non-empty field. +TEST(RepeatedField, AddRange4) { + RepeatedField me; + me.Add(0); + me.Add(1); + + std::vector values; + values.push_back(2); + values.push_back(3); + + me.Add(values.begin(), values.end()); + ASSERT_EQ(me.size(), 4); + ASSERT_EQ(me.Get(0), 0); + ASSERT_EQ(me.Get(1), 1); + ASSERT_EQ(me.Get(2), values[0]); + ASSERT_EQ(me.Get(3), values[1]); +} + +// Add contents of a stringstream in order to test code paths where there is +// an input iterator. +TEST(RepeatedField, AddRange5) { + RepeatedField me; + + std::stringstream ss; + ss << 1 << ' ' << 2; + + me.Add(std::istream_iterator(ss), std::istream_iterator()); + ASSERT_EQ(me.size(), 2); + ASSERT_EQ(me.Get(0), 1); + ASSERT_EQ(me.Get(1), 2); +} + TEST(RepeatedField, CopyConstruct) { RepeatedField source; source.Add(1); @@ -340,6 +423,9 @@ TEST(RepeatedField, CopyConstruct) { TEST(RepeatedField, IteratorConstruct) { std::vector values; + RepeatedField empty(values.begin(), values.end()); + ASSERT_EQ(values.size(), empty.size()); + values.push_back(1); values.push_back(2); @@ -515,6 +601,48 @@ TEST(RepeatedField, MoveAssign) { } } +TEST(Movable, Works) { + class NonMoveConstructible { + public: + NonMoveConstructible(NonMoveConstructible&&) = delete; + NonMoveConstructible& operator=(NonMoveConstructible&&) { return *this; } + }; + class NonMoveAssignable { + public: + NonMoveAssignable(NonMoveAssignable&&) {} + NonMoveAssignable& operator=(NonMoveConstructible&&) = delete; + }; + class NonMovable { + public: + NonMovable(NonMovable&&) = delete; + NonMovable& operator=(NonMovable&&) = delete; + }; + + EXPECT_TRUE(internal::IsMovable::value); + + EXPECT_FALSE(std::is_move_constructible::value); + EXPECT_TRUE(std::is_move_assignable::value); + EXPECT_FALSE(internal::IsMovable::value); + + EXPECT_TRUE(std::is_move_constructible::value); + EXPECT_FALSE(std::is_move_assignable::value); + EXPECT_FALSE(internal::IsMovable::value); + + EXPECT_FALSE(internal::IsMovable::value); +} + +TEST(RepeatedField, MoveAdd) { + RepeatedPtrField field; + TestAllTypes test_all_types; + auto* optional_nested_message = + test_all_types.mutable_optional_nested_message(); + optional_nested_message->set_bb(42); + field.Add(std::move(test_all_types)); + + EXPECT_EQ(optional_nested_message, + field.Mutable(0)->mutable_optional_nested_message()); +} + TEST(RepeatedField, MutableDataIsMutable) { RepeatedField field; field.Add(1); @@ -567,27 +695,23 @@ TEST(RepeatedField, ExtractSubrange) { for (int start = 0; start < sz - num; ++start) { // Create RepeatedField with sz elements having values 0 through sz-1. RepeatedField field; - for (int i = 0; i < sz; ++i) - field.Add(i); + for (int i = 0; i < sz; ++i) field.Add(i); EXPECT_EQ(field.size(), sz); // Create a catcher array and call ExtractSubrange. int32 catcher[10]; - for (int i = 0; i < 10; ++i) - catcher[i] = -1; + for (int i = 0; i < 10; ++i) catcher[i] = -1; field.ExtractSubrange(start, num, catcher); // Does the resulting array have the right size? EXPECT_EQ(field.size(), sz - num); // Were the removed elements extracted into the catcher array? - for (int i = 0; i < num; ++i) - EXPECT_EQ(catcher[i], start + i); + for (int i = 0; i < num; ++i) EXPECT_EQ(catcher[i], start + i); EXPECT_EQ(catcher[num], -1); // Does the resulting array contain the right values? - for (int i = 0; i < start; ++i) - EXPECT_EQ(field.Get(i), i); + for (int i = 0; i < start; ++i) EXPECT_EQ(field.Get(i), i); for (int i = start; i < field.size(); ++i) EXPECT_EQ(field.Get(i), i + num); } @@ -601,9 +725,9 @@ TEST(RepeatedField, ClearThenReserveMore) { // present. Use a 'string' and > 16 bytes length so that the elements are // non-POD and allocate -- the leak checker will catch any skipped destructor // calls here. - RepeatedField field; + RepeatedField field; for (int i = 0; i < 32; i++) { - field.Add(string("abcdefghijklmnopqrstuvwxyz0123456789")); + field.Add(std::string("abcdefghijklmnopqrstuvwxyz0123456789")); } EXPECT_EQ(32, field.size()); field.Clear(); @@ -622,7 +746,7 @@ TEST(RepeatedField, ClearThenReserveMore) { // tests above. TEST(RepeatedPtrField, Small) { - RepeatedPtrField field; + RepeatedPtrField field; EXPECT_TRUE(field.empty()); EXPECT_EQ(field.size(), 0); @@ -632,26 +756,32 @@ TEST(RepeatedPtrField, Small) { EXPECT_FALSE(field.empty()); EXPECT_EQ(field.size(), 1); EXPECT_EQ(field.Get(0), "foo"); + EXPECT_EQ(field.at(0), "foo"); field.Add()->assign("bar"); EXPECT_FALSE(field.empty()); EXPECT_EQ(field.size(), 2); EXPECT_EQ(field.Get(0), "foo"); + EXPECT_EQ(field.at(0), "foo"); EXPECT_EQ(field.Get(1), "bar"); + EXPECT_EQ(field.at(1), "bar"); field.Mutable(1)->assign("baz"); EXPECT_FALSE(field.empty()); EXPECT_EQ(field.size(), 2); EXPECT_EQ(field.Get(0), "foo"); + EXPECT_EQ(field.at(0), "foo"); EXPECT_EQ(field.Get(1), "baz"); + EXPECT_EQ(field.at(1), "baz"); field.RemoveLast(); EXPECT_FALSE(field.empty()); EXPECT_EQ(field.size(), 1); EXPECT_EQ(field.Get(0), "foo"); + EXPECT_EQ(field.at(0), "foo"); field.Clear(); @@ -660,7 +790,7 @@ TEST(RepeatedPtrField, Small) { } TEST(RepeatedPtrField, Large) { - RepeatedPtrField field; + RepeatedPtrField field; for (int i = 0; i < 16; i++) { *field.Add() += 'a' + i; @@ -673,13 +803,13 @@ TEST(RepeatedPtrField, Large) { EXPECT_EQ(field.Get(i)[0], 'a' + i); } - int min_expected_usage = 16 * sizeof(string); + int min_expected_usage = 16 * sizeof(std::string); EXPECT_GE(field.SpaceUsedExcludingSelf(), min_expected_usage); } TEST(RepeatedPtrField, SwapSmallSmall) { - RepeatedPtrField field1; - RepeatedPtrField field2; + RepeatedPtrField field1; + RepeatedPtrField field2; EXPECT_TRUE(field1.empty()); EXPECT_EQ(field1.size(), 0); @@ -708,8 +838,8 @@ TEST(RepeatedPtrField, SwapSmallSmall) { } TEST(RepeatedPtrField, SwapLargeSmall) { - RepeatedPtrField field1; - RepeatedPtrField field2; + RepeatedPtrField field1; + RepeatedPtrField field2; field2.Add()->assign("foo"); field2.Add()->assign("bar"); @@ -729,8 +859,8 @@ TEST(RepeatedPtrField, SwapLargeSmall) { } TEST(RepeatedPtrField, SwapLargeLarge) { - RepeatedPtrField field1; - RepeatedPtrField field2; + RepeatedPtrField field1; + RepeatedPtrField field2; field1.Add()->assign("foo"); field1.Add()->assign("bar"); @@ -754,8 +884,8 @@ TEST(RepeatedPtrField, SwapLargeLarge) { } } -static int ReservedSpace(RepeatedPtrField* field) { - const string* const* ptr = field->data(); +static int ReservedSpace(RepeatedPtrField* field) { + const std::string* const* ptr = field->data(); do { field->Add(); } while (field->data() == ptr); @@ -764,14 +894,14 @@ static int ReservedSpace(RepeatedPtrField* field) { } TEST(RepeatedPtrField, ReserveMoreThanDouble) { - RepeatedPtrField field; + RepeatedPtrField field; field.Reserve(20); EXPECT_LE(20, ReservedSpace(&field)); } TEST(RepeatedPtrField, ReserveLessThanDouble) { - RepeatedPtrField field; + RepeatedPtrField field; field.Reserve(20); int capacity = field.Capacity(); @@ -782,9 +912,9 @@ TEST(RepeatedPtrField, ReserveLessThanDouble) { } TEST(RepeatedPtrField, ReserveLessThanExisting) { - RepeatedPtrField field; + RepeatedPtrField field; field.Reserve(20); - const string* const* previous_ptr = field.data(); + const std::string* const* previous_ptr = field.data(); field.Reserve(10); EXPECT_EQ(previous_ptr, field.data()); @@ -795,8 +925,8 @@ TEST(RepeatedPtrField, ReserveDoesntLoseAllocated) { // Check that a bug is fixed: An earlier implementation of Reserve() // failed to copy pointers to allocated-but-cleared objects, possibly // leading to segfaults. - RepeatedPtrField field; - string* first = field.Add(); + RepeatedPtrField field; + std::string* first = field.Add(); field.RemoveLast(); field.Reserve(20); @@ -806,9 +936,9 @@ TEST(RepeatedPtrField, ReserveDoesntLoseAllocated) { // Clearing elements is tricky with RepeatedPtrFields since the memory for // the elements is retained and reused. TEST(RepeatedPtrField, ClearedElements) { - RepeatedPtrField field; + RepeatedPtrField field; - string* original = field.Add(); + std::string* original = field.Add(); *original = "foo"; EXPECT_EQ(field.ClearedCount(), 0); @@ -817,7 +947,8 @@ TEST(RepeatedPtrField, ClearedElements) { EXPECT_TRUE(original->empty()); EXPECT_EQ(field.ClearedCount(), 1); - EXPECT_EQ(field.Add(), original); // Should return same string for reuse. + EXPECT_EQ(field.Add(), + original); // Should return same string for reuse. EXPECT_EQ(field.ReleaseLast(), original); // We take ownership. EXPECT_EQ(field.ClearedCount(), 0); @@ -846,7 +977,7 @@ TEST(RepeatedPtrField, ClearedElements) { // Test all code paths in AddAllocated(). TEST(RepeatedPtrField, AddAlocated) { - RepeatedPtrField field; + RepeatedPtrField field; while (field.size() < field.Capacity()) { field.Add()->assign("filler"); } @@ -854,14 +985,14 @@ TEST(RepeatedPtrField, AddAlocated) { int index = field.size(); // First branch: Field is at capacity with no cleared objects. - string* foo = new string("foo"); + std::string* foo = new std::string("foo"); field.AddAllocated(foo); EXPECT_EQ(index + 1, field.size()); EXPECT_EQ(0, field.ClearedCount()); EXPECT_EQ(foo, &field.Get(index)); // Last branch: Field is not at capacity and there are no cleared objects. - string* bar = new string("bar"); + std::string* bar = new std::string("bar"); field.AddAllocated(bar); ++index; EXPECT_EQ(index + 1, field.size()); @@ -870,7 +1001,7 @@ TEST(RepeatedPtrField, AddAlocated) { // Third branch: Field is not at capacity and there are no cleared objects. field.RemoveLast(); - string* baz = new string("baz"); + std::string* baz = new std::string("baz"); field.AddAllocated(baz); EXPECT_EQ(index + 1, field.size()); EXPECT_EQ(1, field.ClearedCount()); @@ -882,7 +1013,7 @@ TEST(RepeatedPtrField, AddAlocated) { } field.RemoveLast(); index = field.size(); - string* qux = new string("qux"); + std::string* qux = new std::string("qux"); field.AddAllocated(qux); EXPECT_EQ(index + 1, field.size()); // We should have discarded the cleared object. @@ -891,7 +1022,7 @@ TEST(RepeatedPtrField, AddAlocated) { } TEST(RepeatedPtrField, MergeFrom) { - RepeatedPtrField source, destination; + RepeatedPtrField source, destination; source.Add()->assign("4"); source.Add()->assign("5"); destination.Add()->assign("1"); @@ -910,7 +1041,7 @@ TEST(RepeatedPtrField, MergeFrom) { TEST(RepeatedPtrField, CopyFrom) { - RepeatedPtrField source, destination; + RepeatedPtrField source, destination; source.Add()->assign("4"); source.Add()->assign("5"); destination.Add()->assign("1"); @@ -925,7 +1056,7 @@ TEST(RepeatedPtrField, CopyFrom) { } TEST(RepeatedPtrField, CopyFromSelf) { - RepeatedPtrField me; + RepeatedPtrField me; me.Add()->assign("1"); me.CopyFrom(me); ASSERT_EQ(1, me.size()); @@ -933,8 +1064,8 @@ TEST(RepeatedPtrField, CopyFromSelf) { } TEST(RepeatedPtrField, Erase) { - RepeatedPtrField me; - RepeatedPtrField::iterator it = me.erase(me.begin(), me.end()); + RepeatedPtrField me; + RepeatedPtrField::iterator it = me.erase(me.begin(), me.end()); EXPECT_TRUE(me.begin() == it); EXPECT_EQ(0, me.size()); @@ -966,11 +1097,11 @@ TEST(RepeatedPtrField, Erase) { } TEST(RepeatedPtrField, CopyConstruct) { - RepeatedPtrField source; + RepeatedPtrField source; source.Add()->assign("1"); source.Add()->assign("2"); - RepeatedPtrField destination(source); + RepeatedPtrField destination(source); ASSERT_EQ(2, destination.size()); EXPECT_EQ("1", destination.Get(0)); @@ -978,16 +1109,16 @@ TEST(RepeatedPtrField, CopyConstruct) { } TEST(RepeatedPtrField, IteratorConstruct_String) { - std::vector values; + std::vector values; values.push_back("1"); values.push_back("2"); - RepeatedPtrField field(values.begin(), values.end()); + RepeatedPtrField field(values.begin(), values.end()); ASSERT_EQ(values.size(), field.size()); EXPECT_EQ(values[0], field.Get(0)); EXPECT_EQ(values[1], field.Get(1)); - RepeatedPtrField other(field.begin(), field.end()); + RepeatedPtrField other(field.begin(), field.end()); ASSERT_EQ(values.size(), other.size()); EXPECT_EQ(values[0], other.Get(0)); EXPECT_EQ(values[1], other.Get(1)); @@ -1013,7 +1144,7 @@ TEST(RepeatedPtrField, IteratorConstruct_Proto) { } TEST(RepeatedPtrField, CopyAssign) { - RepeatedPtrField source, destination; + RepeatedPtrField source, destination; source.Add()->assign("4"); source.Add()->assign("5"); destination.Add()->assign("1"); @@ -1029,7 +1160,7 @@ TEST(RepeatedPtrField, CopyAssign) { TEST(RepeatedPtrField, SelfAssign) { // Verify that assignment to self does not destroy data. - RepeatedPtrField source, *p; + RepeatedPtrField source, *p; p = &source; source.Add()->assign("7"); source.Add()->assign("8"); @@ -1043,11 +1174,11 @@ TEST(RepeatedPtrField, SelfAssign) { TEST(RepeatedPtrField, MoveConstruct) { { - RepeatedPtrField source; + RepeatedPtrField source; *source.Add() = "1"; *source.Add() = "2"; - const string* const* data = source.data(); - RepeatedPtrField destination = std::move(source); + const std::string* const* data = source.data(); + RepeatedPtrField destination = std::move(source); EXPECT_EQ(data, destination.data()); EXPECT_THAT(destination, ElementsAre("1", "2")); // This property isn't guaranteed but it's useful to have a test that would @@ -1056,11 +1187,11 @@ TEST(RepeatedPtrField, MoveConstruct) { } { Arena arena; - RepeatedPtrField* source = - Arena::CreateMessage>(&arena); + RepeatedPtrField* source = + Arena::CreateMessage>(&arena); *source->Add() = "1"; *source->Add() = "2"; - RepeatedPtrField destination = std::move(*source); + RepeatedPtrField destination = std::move(*source); EXPECT_EQ(nullptr, destination.GetArena()); EXPECT_THAT(destination, ElementsAre("1", "2")); // This property isn't guaranteed but it's useful to have a test that would @@ -1071,13 +1202,13 @@ TEST(RepeatedPtrField, MoveConstruct) { TEST(RepeatedPtrField, MoveAssign) { { - RepeatedPtrField source; + RepeatedPtrField source; *source.Add() = "1"; *source.Add() = "2"; - RepeatedPtrField destination; + RepeatedPtrField destination; *destination.Add() = "3"; - const string* const* source_data = source.data(); - const string* const* destination_data = destination.data(); + const std::string* const* source_data = source.data(); + const std::string* const* destination_data = destination.data(); destination = std::move(source); EXPECT_EQ(source_data, destination.data()); EXPECT_THAT(destination, ElementsAre("1", "2")); @@ -1088,15 +1219,15 @@ TEST(RepeatedPtrField, MoveAssign) { } { Arena arena; - RepeatedPtrField* source = - Arena::CreateMessage>(&arena); + RepeatedPtrField* source = + Arena::CreateMessage>(&arena); *source->Add() = "1"; *source->Add() = "2"; - RepeatedPtrField* destination = - Arena::CreateMessage>(&arena); + RepeatedPtrField* destination = + Arena::CreateMessage>(&arena); *destination->Add() = "3"; - const string* const* source_data = source->data(); - const string* const* destination_data = destination->data(); + const std::string* const* source_data = source->data(); + const std::string* const* destination_data = destination->data(); *destination = std::move(*source); EXPECT_EQ(source_data, destination->data()); EXPECT_THAT(*destination, ElementsAre("1", "2")); @@ -1107,13 +1238,13 @@ TEST(RepeatedPtrField, MoveAssign) { } { Arena source_arena; - RepeatedPtrField* source = - Arena::CreateMessage>(&source_arena); + RepeatedPtrField* source = + Arena::CreateMessage>(&source_arena); *source->Add() = "1"; *source->Add() = "2"; Arena destination_arena; - RepeatedPtrField* destination = - Arena::CreateMessage>(&destination_arena); + RepeatedPtrField* destination = + Arena::CreateMessage>(&destination_arena); *destination->Add() = "3"; *destination = std::move(*source); EXPECT_THAT(*destination, ElementsAre("1", "2")); @@ -1123,11 +1254,11 @@ TEST(RepeatedPtrField, MoveAssign) { } { Arena arena; - RepeatedPtrField* source = - Arena::CreateMessage>(&arena); + RepeatedPtrField* source = + Arena::CreateMessage>(&arena); *source->Add() = "1"; *source->Add() = "2"; - RepeatedPtrField destination; + RepeatedPtrField destination; *destination.Add() = "3"; destination = std::move(*source); EXPECT_THAT(destination, ElementsAre("1", "2")); @@ -1136,12 +1267,12 @@ TEST(RepeatedPtrField, MoveAssign) { EXPECT_THAT(*source, ElementsAre("1", "2")); } { - RepeatedPtrField source; + RepeatedPtrField source; *source.Add() = "1"; *source.Add() = "2"; Arena arena; - RepeatedPtrField* destination = - Arena::CreateMessage>(&arena); + RepeatedPtrField* destination = + Arena::CreateMessage>(&arena); *destination->Add() = "3"; *destination = std::move(source); EXPECT_THAT(*destination, ElementsAre("1", "2")); @@ -1150,23 +1281,23 @@ TEST(RepeatedPtrField, MoveAssign) { EXPECT_THAT(source, ElementsAre("1", "2")); } { - RepeatedPtrField field; + RepeatedPtrField field; // An alias to defeat -Wself-move. - RepeatedPtrField& alias = field; + RepeatedPtrField& alias = field; *field.Add() = "1"; *field.Add() = "2"; - const string* const* data = field.data(); + const std::string* const* data = field.data(); field = std::move(alias); EXPECT_EQ(data, field.data()); EXPECT_THAT(field, ElementsAre("1", "2")); } { Arena arena; - RepeatedPtrField* field = - Arena::CreateMessage>(&arena); + RepeatedPtrField* field = + Arena::CreateMessage>(&arena); *field->Add() = "1"; *field->Add() = "2"; - const string* const* data = field->data(); + const std::string* const* data = field->data(); *field = std::move(*field); EXPECT_EQ(data, field->data()); EXPECT_THAT(*field, ElementsAre("1", "2")); @@ -1174,23 +1305,23 @@ TEST(RepeatedPtrField, MoveAssign) { } TEST(RepeatedPtrField, MutableDataIsMutable) { - RepeatedPtrField field; + RepeatedPtrField field; *field.Add() = "1"; EXPECT_EQ("1", field.Get(0)); // The fact that this line compiles would be enough, but we'll check the // value anyway. - string** data = field.mutable_data(); + std::string** data = field.mutable_data(); **data = "2"; EXPECT_EQ("2", field.Get(0)); } TEST(RepeatedPtrField, SubscriptOperators) { - RepeatedPtrField field; + RepeatedPtrField field; *field.Add() = "1"; EXPECT_EQ("1", field.Get(0)); EXPECT_EQ("1", field[0]); EXPECT_EQ(field.Mutable(0), &field[0]); - const RepeatedPtrField& const_field = field; + const RepeatedPtrField& const_field = field; EXPECT_EQ(*field.data(), &const_field[0]); } @@ -1201,24 +1332,22 @@ TEST(RepeatedPtrField, ExtractSubrange) { for (int num = 0; num <= sz; ++num) { for (int start = 0; start < sz - num; ++start) { for (int extra = 0; extra < 4; ++extra) { - std::vector subject; + std::vector subject; // Create an array with "sz" elements and "extra" cleared elements. - RepeatedPtrField field; + RepeatedPtrField field; for (int i = 0; i < sz + extra; ++i) { - subject.push_back(new string()); + subject.push_back(new std::string()); field.AddAllocated(subject[i]); } EXPECT_EQ(field.size(), sz + extra); - for (int i = 0; i < extra; ++i) - field.RemoveLast(); + for (int i = 0; i < extra; ++i) field.RemoveLast(); EXPECT_EQ(field.size(), sz); EXPECT_EQ(field.ClearedCount(), extra); // Create a catcher array and call ExtractSubrange. - string* catcher[10]; - for (int i = 0; i < 10; ++i) - catcher[i] = NULL; + std::string* catcher[10]; + for (int i = 0; i < 10; ++i) catcher[i] = NULL; field.ExtractSubrange(start, num, catcher); // Does the resulting array have the right size? @@ -1237,8 +1366,7 @@ TEST(RepeatedPtrField, ExtractSubrange) { // Reinstate the cleared elements. EXPECT_EQ(field.ClearedCount(), extra); - for (int i = 0; i < extra; ++i) - field.Add(); + for (int i = 0; i < extra; ++i) field.Add(); EXPECT_EQ(field.ClearedCount(), 0); EXPECT_EQ(field.size(), sz - num + extra); @@ -1246,15 +1374,13 @@ TEST(RepeatedPtrField, ExtractSubrange) { for (int i = sz; i < sz + extra; ++i) { int count = 0; for (int j = sz; j < sz + extra; ++j) { - if (field.Mutable(j - num) == subject[i]) - count += 1; + if (field.Mutable(j - num) == subject[i]) count += 1; } EXPECT_EQ(count, 1); } // Release the caught elements. - for (int i = 0; i < num; ++i) - delete catcher[i]; + for (int i = 0; i < num; ++i) delete catcher[i]; } } } @@ -1326,18 +1452,18 @@ class RepeatedPtrFieldIteratorTest : public testing::Test { proto_array_.Add()->assign("baz"); } - RepeatedPtrField proto_array_; + RepeatedPtrField proto_array_; }; TEST_F(RepeatedPtrFieldIteratorTest, Convertible) { - RepeatedPtrField::iterator iter = proto_array_.begin(); - RepeatedPtrField::const_iterator c_iter = iter; - RepeatedPtrField::value_type value = *c_iter; + RepeatedPtrField::iterator iter = proto_array_.begin(); + RepeatedPtrField::const_iterator c_iter = iter; + RepeatedPtrField::value_type value = *c_iter; EXPECT_EQ("foo", value); } TEST_F(RepeatedPtrFieldIteratorTest, MutableIteration) { - RepeatedPtrField::iterator iter = proto_array_.begin(); + RepeatedPtrField::iterator iter = proto_array_.begin(); EXPECT_EQ("foo", *iter); ++iter; EXPECT_EQ("bar", *(iter++)); @@ -1348,8 +1474,9 @@ TEST_F(RepeatedPtrFieldIteratorTest, MutableIteration) { } TEST_F(RepeatedPtrFieldIteratorTest, ConstIteration) { - const RepeatedPtrField& const_proto_array = proto_array_; - RepeatedPtrField::const_iterator iter = const_proto_array.begin(); + const RepeatedPtrField& const_proto_array = proto_array_; + RepeatedPtrField::const_iterator iter = + const_proto_array.begin(); EXPECT_EQ("foo", *iter); ++iter; EXPECT_EQ("bar", *(iter++)); @@ -1360,7 +1487,7 @@ TEST_F(RepeatedPtrFieldIteratorTest, ConstIteration) { } TEST_F(RepeatedPtrFieldIteratorTest, MutableReverseIteration) { - RepeatedPtrField::reverse_iterator iter = proto_array_.rbegin(); + RepeatedPtrField::reverse_iterator iter = proto_array_.rbegin(); EXPECT_EQ("baz", *iter); ++iter; EXPECT_EQ("bar", *(iter++)); @@ -1371,9 +1498,9 @@ TEST_F(RepeatedPtrFieldIteratorTest, MutableReverseIteration) { } TEST_F(RepeatedPtrFieldIteratorTest, ConstReverseIteration) { - const RepeatedPtrField& const_proto_array = proto_array_; - RepeatedPtrField::const_reverse_iterator iter - = const_proto_array.rbegin(); + const RepeatedPtrField& const_proto_array = proto_array_; + RepeatedPtrField::const_reverse_iterator iter = + const_proto_array.rbegin(); EXPECT_EQ("baz", *iter); ++iter; EXPECT_EQ("bar", *(iter++)); @@ -1384,8 +1511,8 @@ TEST_F(RepeatedPtrFieldIteratorTest, ConstReverseIteration) { } TEST_F(RepeatedPtrFieldIteratorTest, RandomAccess) { - RepeatedPtrField::iterator iter = proto_array_.begin(); - RepeatedPtrField::iterator iter2 = iter; + RepeatedPtrField::iterator iter = proto_array_.begin(); + RepeatedPtrField::iterator iter2 = iter; ++iter2; ++iter2; EXPECT_TRUE(iter + 2 == iter2); @@ -1396,8 +1523,8 @@ TEST_F(RepeatedPtrFieldIteratorTest, RandomAccess) { } TEST_F(RepeatedPtrFieldIteratorTest, Comparable) { - RepeatedPtrField::const_iterator iter = proto_array_.begin(); - RepeatedPtrField::const_iterator iter2 = iter + 1; + RepeatedPtrField::const_iterator iter = proto_array_.begin(); + RepeatedPtrField::const_iterator iter2 = iter + 1; EXPECT_TRUE(iter == iter); EXPECT_TRUE(iter != iter2); EXPECT_TRUE(iter < iter2); @@ -1410,7 +1537,7 @@ TEST_F(RepeatedPtrFieldIteratorTest, Comparable) { // Uninitialized iterator does not point to any of the RepeatedPtrField. TEST_F(RepeatedPtrFieldIteratorTest, UninitializedIterator) { - RepeatedPtrField::iterator iter; + RepeatedPtrField::iterator iter; EXPECT_TRUE(iter != proto_array_.begin()); EXPECT_TRUE(iter != proto_array_.begin() + 1); EXPECT_TRUE(iter != proto_array_.begin() + 2); @@ -1428,8 +1555,8 @@ TEST_F(RepeatedPtrFieldIteratorTest, STLAlgorithms_lower_bound) { proto_array_.Add()->assign("x"); proto_array_.Add()->assign("y"); - string v = "f"; - RepeatedPtrField::const_iterator it = + std::string v = "f"; + RepeatedPtrField::const_iterator it = std::lower_bound(proto_array_.begin(), proto_array_.end(), v); EXPECT_EQ(*it, "n"); @@ -1437,7 +1564,7 @@ TEST_F(RepeatedPtrFieldIteratorTest, STLAlgorithms_lower_bound) { } TEST_F(RepeatedPtrFieldIteratorTest, Mutation) { - RepeatedPtrField::iterator iter = proto_array_.begin(); + RepeatedPtrField::iterator iter = proto_array_.begin(); *iter = "qux"; EXPECT_EQ("qux", proto_array_.Get(0)); } @@ -1453,24 +1580,24 @@ class RepeatedPtrFieldPtrsIteratorTest : public testing::Test { const_proto_array_ = &proto_array_; } - RepeatedPtrField proto_array_; - const RepeatedPtrField* const_proto_array_; + RepeatedPtrField proto_array_; + const RepeatedPtrField* const_proto_array_; }; TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertiblePtr) { - RepeatedPtrField::pointer_iterator iter = + RepeatedPtrField::pointer_iterator iter = proto_array_.pointer_begin(); static_cast(iter); } TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertibleConstPtr) { - RepeatedPtrField::const_pointer_iterator iter = + RepeatedPtrField::const_pointer_iterator iter = const_proto_array_->pointer_begin(); static_cast(iter); } TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) { - RepeatedPtrField::pointer_iterator iter = + RepeatedPtrField::pointer_iterator iter = proto_array_.pointer_begin(); EXPECT_EQ("foo", **iter); ++iter; @@ -1482,7 +1609,7 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) { } TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutableConstPtrIteration) { - RepeatedPtrField::const_pointer_iterator iter = + RepeatedPtrField::const_pointer_iterator iter = const_proto_array_->pointer_begin(); EXPECT_EQ("foo", **iter); ++iter; @@ -1494,9 +1621,9 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutableConstPtrIteration) { } TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomPtrAccess) { - RepeatedPtrField::pointer_iterator iter = + RepeatedPtrField::pointer_iterator iter = proto_array_.pointer_begin(); - RepeatedPtrField::pointer_iterator iter2 = iter; + RepeatedPtrField::pointer_iterator iter2 = iter; ++iter2; ++iter2; EXPECT_TRUE(iter + 2 == iter2); @@ -1507,9 +1634,9 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomPtrAccess) { } TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomConstPtrAccess) { - RepeatedPtrField::const_pointer_iterator iter = + RepeatedPtrField::const_pointer_iterator iter = const_proto_array_->pointer_begin(); - RepeatedPtrField::const_pointer_iterator iter2 = iter; + RepeatedPtrField::const_pointer_iterator iter2 = iter; ++iter2; ++iter2; EXPECT_TRUE(iter + 2 == iter2); @@ -1520,9 +1647,9 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomConstPtrAccess) { } TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparablePtr) { - RepeatedPtrField::pointer_iterator iter = + RepeatedPtrField::pointer_iterator iter = proto_array_.pointer_begin(); - RepeatedPtrField::pointer_iterator iter2 = iter + 1; + RepeatedPtrField::pointer_iterator iter2 = iter + 1; EXPECT_TRUE(iter == iter); EXPECT_TRUE(iter != iter2); EXPECT_TRUE(iter < iter2); @@ -1534,9 +1661,9 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparablePtr) { } TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparableConstPtr) { - RepeatedPtrField::const_pointer_iterator iter = + RepeatedPtrField::const_pointer_iterator iter = const_proto_array_->pointer_begin(); - RepeatedPtrField::const_pointer_iterator iter2 = iter + 1; + RepeatedPtrField::const_pointer_iterator iter2 = iter + 1; EXPECT_TRUE(iter == iter); EXPECT_TRUE(iter != iter2); EXPECT_TRUE(iter < iter2); @@ -1550,7 +1677,7 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparableConstPtr) { // Uninitialized iterator does not point to any of the RepeatedPtrOverPtrs. // Dereferencing an uninitialized iterator crashes the process. TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedPtrIterator) { - RepeatedPtrField::pointer_iterator iter; + RepeatedPtrField::pointer_iterator iter; EXPECT_TRUE(iter != proto_array_.pointer_begin()); EXPECT_TRUE(iter != proto_array_.pointer_begin() + 1); EXPECT_TRUE(iter != proto_array_.pointer_begin() + 2); @@ -1559,7 +1686,7 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedPtrIterator) { } TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedConstPtrIterator) { - RepeatedPtrField::const_pointer_iterator iter; + RepeatedPtrField::const_pointer_iterator iter; EXPECT_TRUE(iter != const_proto_array_->pointer_begin()); EXPECT_TRUE(iter != const_proto_array_->pointer_begin() + 1); EXPECT_TRUE(iter != const_proto_array_->pointer_begin() + 2); @@ -1569,13 +1696,13 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedConstPtrIterator) { // This comparison functor is required by the tests for RepeatedPtrOverPtrs. // They operate on strings and need to compare strings as strings in -// any stl algorithm, even though the iterator returns a pointer to a string -// - i.e. *iter has type string*. +// any stl algorithm, even though the iterator returns a pointer to a +// string +// - i.e. *iter has type std::string*. struct StringLessThan { - bool operator()(const string* z, const string& y) { - return *z < y; + bool operator()(const std::string* z, const std::string* y) const { + return *z < *y; } - bool operator()(const string* z, const string* y) const { return *z < *y; } }; TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) { @@ -1589,8 +1716,8 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) { proto_array_.Add()->assign("y"); { - string v = "f"; - RepeatedPtrField::pointer_iterator it = + std::string v = "f"; + RepeatedPtrField::pointer_iterator it = std::lower_bound(proto_array_.pointer_begin(), proto_array_.pointer_end(), &v, StringLessThan()); @@ -1600,8 +1727,8 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) { EXPECT_TRUE(it == proto_array_.pointer_begin() + 3); } { - string v = "f"; - RepeatedPtrField::const_pointer_iterator it = std::lower_bound( + std::string v = "f"; + RepeatedPtrField::const_pointer_iterator it = std::lower_bound( const_proto_array_->pointer_begin(), const_proto_array_->pointer_end(), &v, StringLessThan()); @@ -1613,7 +1740,7 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) { } TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) { - RepeatedPtrField::pointer_iterator iter = + RepeatedPtrField::pointer_iterator iter = proto_array_.pointer_begin(); **iter = "qux"; EXPECT_EQ("qux", proto_array_.Get(0)); @@ -1622,10 +1749,10 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) { EXPECT_EQ("baz", proto_array_.Get(2)); ++iter; delete *iter; - *iter = new string("a"); + *iter = new std::string("a"); ++iter; delete *iter; - *iter = new string("b"); + *iter = new std::string("b"); EXPECT_EQ("a", proto_array_.Get(1)); EXPECT_EQ("b", proto_array_.Get(2)); } @@ -1648,7 +1775,6 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, Sort) { EXPECT_EQ("y", proto_array_.Get(9)); } - // ----------------------------------------------------------------------------- // Unit-tests for the insert iterators // google::protobuf::RepeatedFieldBackInserter, @@ -1659,7 +1785,7 @@ class RepeatedFieldInsertionIteratorsTest : public testing::Test { protected: std::list halves; std::list fibonacci; - std::vector words; + std::vector words; typedef TestAllTypes::NestedMessage Nested; Nested nesteds[2]; std::vector nested_ptrs; @@ -1709,13 +1835,14 @@ class RepeatedFieldInsertionIteratorsTest : public testing::Test { } virtual void TearDown() { - STLDeleteContainerPointers(nested_ptrs.begin(), nested_ptrs.end()); + for (auto ptr : nested_ptrs) { + delete ptr; + } } }; TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) { - EXPECT_TRUE(std::equal(fibonacci.begin(), - fibonacci.end(), + EXPECT_TRUE(std::equal(fibonacci.begin(), fibonacci.end(), protobuffer.repeated_int32().begin())); EXPECT_TRUE(std::equal(protobuffer.repeated_int32().begin(), protobuffer.repeated_int32().end(), @@ -1723,12 +1850,10 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) { } TEST_F(RepeatedFieldInsertionIteratorsTest, Halves) { - EXPECT_TRUE(std::equal(halves.begin(), - halves.end(), + EXPECT_TRUE(std::equal(halves.begin(), halves.end(), protobuffer.repeated_double().begin())); EXPECT_TRUE(std::equal(protobuffer.repeated_double().begin(), - protobuffer.repeated_double().end(), - halves.begin())); + protobuffer.repeated_double().end(), halves.begin())); } TEST_F(RepeatedFieldInsertionIteratorsTest, Words) { @@ -1746,8 +1871,9 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, Words2) { words.push_back("six"); words.push_back("pence"); protobuffer.mutable_repeated_string()->Clear(); - std::copy(words.begin(), words.end(), RepeatedPtrFieldBackInserter( - protobuffer.mutable_repeated_string())); + std::copy( + words.begin(), words.end(), + RepeatedPtrFieldBackInserter(protobuffer.mutable_repeated_string())); ASSERT_EQ(words.size(), protobuffer.repeated_string_size()); for (int i = 0; i < words.size(); ++i) EXPECT_EQ(words.at(i), protobuffer.repeated_string(i)); @@ -1782,19 +1908,20 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, TEST_F(RepeatedFieldInsertionIteratorsTest, AllocatedRepeatedPtrFieldWithString) { - std::vector data; + std::vector data; TestAllTypes goldenproto; for (int i = 0; i < 10; ++i) { - string* new_data = new string; - *new_data = "name-" + SimpleItoa(i); + std::string* new_data = new std::string; + *new_data = "name-" + StrCat(i); data.push_back(new_data); new_data = goldenproto.add_repeated_string(); - *new_data = "name-" + SimpleItoa(i); + *new_data = "name-" + StrCat(i); } TestAllTypes testproto; - std::copy(data.begin(), data.end(), AllocatedRepeatedPtrFieldBackInserter( - testproto.mutable_repeated_string())); + std::copy(data.begin(), data.end(), + AllocatedRepeatedPtrFieldBackInserter( + testproto.mutable_repeated_string())); EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString()); } @@ -1819,15 +1946,15 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, TEST_F(RepeatedFieldInsertionIteratorsTest, UnsafeArenaAllocatedRepeatedPtrFieldWithString) { - std::vector data; + std::vector data; TestAllTypes goldenproto; for (int i = 0; i < 10; ++i) { - string* new_data = new string; - *new_data = "name-" + SimpleItoa(i); + std::string* new_data = new std::string; + *new_data = "name-" + StrCat(i); data.push_back(new_data); new_data = goldenproto.add_repeated_string(); - *new_data = "name-" + SimpleItoa(i); + *new_data = "name-" + StrCat(i); } TestAllTypes testproto; std::copy(data.begin(), data.end(), @@ -1837,8 +1964,9 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, } TEST_F(RepeatedFieldInsertionIteratorsTest, MoveStrings) { - std::vector src = {"a", "b", "c", "d"}; - std::vector copy = src; // copy since move leaves in undefined state + std::vector src = {"a", "b", "c", "d"}; + std::vector copy = + src; // copy since move leaves in undefined state TestAllTypes testproto; std::move(copy.begin(), copy.end(), RepeatedFieldBackInserter(testproto.mutable_repeated_string())); diff --git a/src/google/protobuf/service.cc b/src/google/protobuf/service.cc index a6616b40cf..53945684cb 100644 --- a/src/google/protobuf/service.cc +++ b/src/google/protobuf/service.cc @@ -43,4 +43,3 @@ RpcController::~RpcController() {} } // namespace protobuf } // namespace google - diff --git a/src/google/protobuf/service.h b/src/google/protobuf/service.h index 5c6d068f12..d6b4ab82e7 100644 --- a/src/google/protobuf/service.h +++ b/src/google/protobuf/service.h @@ -101,13 +101,15 @@ #define GOOGLE_PROTOBUF_SERVICE_H__ #include -#include #include +#include #ifdef SWIG #error "You cannot SWIG proto headers" #endif +#include + namespace google { namespace protobuf { @@ -117,17 +119,17 @@ class RpcController; class RpcChannel; // Defined in other files. -class Descriptor; // descriptor.h -class ServiceDescriptor; // descriptor.h -class MethodDescriptor; // descriptor.h -class Message; // message.h +class Descriptor; // descriptor.h +class ServiceDescriptor; // descriptor.h +class MethodDescriptor; // descriptor.h +class Message; // message.h // Abstract base interface for protocol-buffer-based RPC services. Services // themselves are abstract interfaces (implemented either by servers or as // stubs), but they subclass this base interface. The methods of this // interface can be used to call the methods of the Service without knowing // its exact type at compile time (analogous to Reflection). -class LIBPROTOBUF_EXPORT Service { +class PROTOBUF_EXPORT Service { public: inline Service() {} virtual ~Service(); @@ -135,10 +137,7 @@ class LIBPROTOBUF_EXPORT Service { // When constructing a stub, you may pass STUB_OWNS_CHANNEL as the second // parameter to the constructor to tell it to delete its RpcChannel when // destroyed. - enum ChannelOwnership { - STUB_OWNS_CHANNEL, - STUB_DOESNT_OWN_CHANNEL - }; + enum ChannelOwnership { STUB_OWNS_CHANNEL, STUB_DOESNT_OWN_CHANNEL }; // Get the ServiceDescriptor describing this service and its methods. virtual const ServiceDescriptor* GetDescriptor() = 0; @@ -169,10 +168,8 @@ class LIBPROTOBUF_EXPORT Service { // RpcController can be queried to determine if an error occurred and // possibly to get more information about the error. virtual void CallMethod(const MethodDescriptor* method, - RpcController* controller, - const Message* request, - Message* response, - Closure* done) = 0; + RpcController* controller, const Message* request, + Message* response, Closure* done) = 0; // CallMethod() requires that the request and response passed in are of a // particular subclass of Message. GetRequestPrototype() and @@ -188,9 +185,9 @@ class LIBPROTOBUF_EXPORT Service { // request->ParseFromString(input); // service->CallMethod(method, *request, response, callback); virtual const Message& GetRequestPrototype( - const MethodDescriptor* method) const = 0; + const MethodDescriptor* method) const = 0; virtual const Message& GetResponsePrototype( - const MethodDescriptor* method) const = 0; + const MethodDescriptor* method) const = 0; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Service); @@ -204,7 +201,7 @@ class LIBPROTOBUF_EXPORT Service { // "least common denominator" set of features which we expect all // implementations to support. Specific implementations may provide more // advanced features (e.g. deadline propagation). -class LIBPROTOBUF_EXPORT RpcController { +class PROTOBUF_EXPORT RpcController { public: inline RpcController() {} virtual ~RpcController(); @@ -224,7 +221,7 @@ class LIBPROTOBUF_EXPORT RpcController { virtual bool Failed() const = 0; // If Failed() is true, returns a human-readable description of the error. - virtual string ErrorText() const = 0; + virtual std::string ErrorText() const = 0; // Advises the RPC system that the caller desires that the RPC call be // canceled. The RPC system may cancel it immediately, may wait awhile and @@ -242,7 +239,7 @@ class LIBPROTOBUF_EXPORT RpcController { // you need to return machine-readable information about failures, you // should incorporate it into your response protocol buffer and should // NOT call SetFailed(). - virtual void SetFailed(const string& reason) = 0; + virtual void SetFailed(const std::string& reason) = 0; // If true, indicates that the client canceled the RPC, so the server may // as well give up on replying to it. The server should still call the @@ -270,7 +267,7 @@ class LIBPROTOBUF_EXPORT RpcController { // RpcChannel* channel = new MyRpcChannel("remotehost.example.com:1234"); // MyService* service = new MyService::Stub(channel); // service->MyMethod(request, &response, callback); -class LIBPROTOBUF_EXPORT RpcChannel { +class PROTOBUF_EXPORT RpcChannel { public: inline RpcChannel() {} virtual ~RpcChannel(); @@ -281,10 +278,8 @@ class LIBPROTOBUF_EXPORT RpcChannel { // need not be of any specific class as long as their descriptors are // method->input_type() and method->output_type(). virtual void CallMethod(const MethodDescriptor* method, - RpcController* controller, - const Message* request, - Message* response, - Closure* done) = 0; + RpcController* controller, const Message* request, + Message* response, Closure* done) = 0; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcChannel); @@ -293,4 +288,6 @@ class LIBPROTOBUF_EXPORT RpcChannel { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_SERVICE_H__ diff --git a/src/google/protobuf/source_context.pb.cc b/src/google/protobuf/source_context.pb.cc index 067fcae032..877814bfdc 100644 --- a/src/google/protobuf/source_context.pb.cc +++ b/src/google/protobuf/source_context.pb.cc @@ -5,75 +5,56 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_OPEN class SourceContextDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _SourceContext_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsSourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_SourceContext_default_instance_; - new (ptr) ::google::protobuf::SourceContext(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_SourceContext_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::SourceContext(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::SourceContext::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::SourceContext::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsSourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto}, {}}; - -void InitDefaults_google_2fprotobuf_2fsource_5fcontext_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base); -} +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2fsource_5fcontext_2eproto[1]; -constexpr ::google::protobuf::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto = nullptr; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fsource_5fcontext_2eproto[1]; +static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto = nullptr; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fsource_5fcontext_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2fsource_5fcontext_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceContext, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceContext, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::SourceContext, file_name_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::SourceContext, file_name_), }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, -1, sizeof(::google::protobuf::SourceContext)}, +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, -1, sizeof(PROTOBUF_NAMESPACE_ID::SourceContext)}, }; -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_SourceContext_default_instance_), +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_SourceContext_default_instance_), }; -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2fsource_5fcontext_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2fsource_5fcontext_2eproto, "google/protobuf/source_context.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2fsource_5fcontext_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2fsource_5fcontext_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto, file_level_service_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto, -}; - -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto = { - false, InitDefaults_google_2fprotobuf_2fsource_5fcontext_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2fsource_5fcontext_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n$google/protobuf/source_context.proto\022\017" "google.protobuf\"\"\n\rSourceContext\022\021\n\tfile" "_name\030\001 \001(\tB\225\001\n\023com.google.protobufB\022Sou" @@ -81,54 +62,52 @@ static ::google::protobuf::Message const * const file_default_instances[] = { "proto/protobuf/source_context;source_con" "text\242\002\003GPB\252\002\036Google.Protobuf.WellKnownTy" "pesb\006proto3" -, - "google/protobuf/source_context.proto", &assign_descriptors_table_google_2fprotobuf_2fsource_5fcontext_2eproto, 251, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_deps[1] = { +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_sccs[1] = { + &scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto = { + &descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2fsource_5fcontext_2eproto, "google/protobuf/source_context.proto", 251, + &descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_once, descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_sccs, descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto_deps, 1, 0, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2fsource_5fcontext_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2fsource_5fcontext_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto, file_level_service_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2fsource_5fcontext_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto, deps, 0); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2fsource_5fcontext_2eproto = []() { AddDescriptors_google_2fprotobuf_2fsource_5fcontext_2eproto(); return true; }(); -namespace google { -namespace protobuf { +static bool dynamic_init_dummy_google_2fprotobuf_2fsource_5fcontext_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto), true); +PROTOBUF_NAMESPACE_OPEN // =================================================================== void SourceContext::InitAsDefaultInstance() { } -class SourceContext::HasBitSetters { +class SourceContext::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int SourceContext::kFileNameFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - SourceContext::SourceContext() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.SourceContext) } SourceContext::SourceContext(const SourceContext& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr) { _internal_metadata_.MergeFrom(from._internal_metadata_); - file_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (from.file_name().size() > 0) { - file_name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.file_name_); + file_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + if (!from.file_name().empty()) { + file_name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.file_name_); } // @@protoc_insertion_point(copy_constructor:google.protobuf.SourceContext) } void SourceContext::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base); - file_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base); + file_name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } SourceContext::~SourceContext() { @@ -137,101 +116,81 @@ SourceContext::~SourceContext() { } void SourceContext::SharedDtor() { - file_name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + file_name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } void SourceContext::SetCachedSize(int size) const { _cached_size_.Set(size); } const SourceContext& SourceContext::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base); return *internal_default_instance(); } void SourceContext::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.SourceContext) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; - file_name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + file_name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); _internal_metadata_.Clear(); } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* SourceContext::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* SourceContext::_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) { - case 0: goto error; // string file_name = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.SourceContext.file_name"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_file_name(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_file_name(), ptr, ctx, "google.protobuf.SourceContext.file_name"); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool SourceContext::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.SourceContext) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // string file_name = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_file_name())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->file_name().data(), static_cast(this->file_name().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.SourceContext.file_name")); } else { goto handle_unusual; @@ -244,7 +203,7 @@ bool SourceContext::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -260,50 +219,25 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void SourceContext::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.SourceContext) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // string file_name = 1; - if (this->file_name().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->file_name().data(), static_cast(this->file_name().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.SourceContext.file_name"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 1, this->file_name(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.SourceContext) -} - -::google::protobuf::uint8* SourceContext::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* SourceContext::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.SourceContext) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // string file_name = 1; if (this->file_name().size() > 0) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->file_name().data(), static_cast(this->file_name().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.SourceContext.file_name"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 1, this->file_name(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.SourceContext) return target; @@ -313,36 +247,35 @@ size_t SourceContext::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.SourceContext) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // string file_name = 1; if (this->file_name().size() > 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->file_name()); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void SourceContext::MergeFrom(const ::google::protobuf::Message& from) { +void SourceContext::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.SourceContext) GOOGLE_DCHECK_NE(&from, this); const SourceContext* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.SourceContext) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.SourceContext) MergeFrom(*source); @@ -353,16 +286,16 @@ void SourceContext::MergeFrom(const SourceContext& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.SourceContext) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; if (from.file_name().size() > 0) { - file_name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.file_name_); + file_name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.file_name_); } } -void SourceContext::CopyFrom(const ::google::protobuf::Message& from) { +void SourceContext::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.SourceContext) if (&from == this) return; Clear(); @@ -380,32 +313,25 @@ bool SourceContext::IsInitialized() const { return true; } -void SourceContext::Swap(SourceContext* other) { - if (other == this) return; - InternalSwap(other); -} void SourceContext::InternalSwap(SourceContext* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); - file_name_.Swap(&other->file_name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(), + file_name_.Swap(&other->file_name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } -::google::protobuf::Metadata SourceContext::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fsource_5fcontext_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fsource_5fcontext_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata SourceContext::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::SourceContext* Arena::CreateMaybeMessage< ::google::protobuf::SourceContext >(Arena* arena) { - return Arena::CreateInternal< ::google::protobuf::SourceContext >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::SourceContext* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::SourceContext >(Arena* arena) { + return Arena::CreateInternal< PROTOBUF_NAMESPACE_ID::SourceContext >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/source_context.pb.h b/src/google/protobuf/source_context.pb.h index 5355e1c04d..13c4e073e2 100644 --- a/src/google/protobuf/source_context.pb.h +++ b/src/google/protobuf/source_context.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/source_context.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2fsource_5fcontext_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2fsource_5fcontext_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fsource_5fcontext_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fsource_5fcontext_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,57 +27,61 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fsource_5fcontext_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fsource_5fcontext_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fsource_5fcontext_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[1] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fsource_5fcontext_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] + 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2fsource_5fcontext_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto; +PROTOBUF_NAMESPACE_OPEN class SourceContext; class SourceContextDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern SourceContextDefaultTypeInternal _SourceContext_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::SourceContext* Arena::CreateMaybeMessage<::google::protobuf::SourceContext>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { +PROTOBUF_EXPORT extern SourceContextDefaultTypeInternal _SourceContext_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::SourceContext* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN // =================================================================== -class LIBPROTOBUF_EXPORT SourceContext : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.SourceContext) */ { +class PROTOBUF_EXPORT SourceContext : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.SourceContext) */ { public: SourceContext(); virtual ~SourceContext(); SourceContext(const SourceContext& from); - - inline SourceContext& operator=(const SourceContext& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 SourceContext(SourceContext&& from) noexcept : SourceContext() { *this = ::std::move(from); } + inline SourceContext& operator=(const SourceContext& from) { + CopyFrom(from); + return *this; + } inline SourceContext& operator=(SourceContext&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -85,9 +90,15 @@ class LIBPROTOBUF_EXPORT SourceContext : public ::google::protobuf::Message /* @ } return *this; } - #endif - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const SourceContext& default_instance(); @@ -99,82 +110,93 @@ class LIBPROTOBUF_EXPORT SourceContext : public ::google::protobuf::Message /* @ static constexpr int kIndexInFileMessages = 0; - void Swap(SourceContext* other); friend void swap(SourceContext& a, SourceContext& b) { a.Swap(&b); } + inline void Swap(SourceContext* other) { + if (other == this) return; + InternalSwap(other); + } // implements Message ---------------------------------------------- inline SourceContext* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - SourceContext* New(::google::protobuf::Arena* arena) const final { + SourceContext* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const SourceContext& from); void MergeFrom(const SourceContext& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(SourceContext* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.SourceContext"; + } private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { - return NULL; + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { + return nullptr; } inline void* MaybeArenaPtr() const { - return NULL; + return nullptr; } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto); + return ::descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kFileNameFieldNumber = 1, + }; // string file_name = 1; void clear_file_name(); - static const int kFileNameFieldNumber = 1; - const ::std::string& file_name() const; - void set_file_name(const ::std::string& value); - #if LANG_CXX11 - void set_file_name(::std::string&& value); - #endif + const std::string& file_name() const; + void set_file_name(const std::string& value); + void set_file_name(std::string&& value); void set_file_name(const char* value); void set_file_name(const char* value, size_t size); - ::std::string* mutable_file_name(); - ::std::string* release_file_name(); - void set_allocated_file_name(::std::string* file_name); + std::string* mutable_file_name(); + std::string* release_file_name(); + void set_allocated_file_name(std::string* file_name); // @@protoc_insertion_point(class_scope:google.protobuf.SourceContext) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - ::google::protobuf::internal::ArenaStringPtr file_name_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr file_name_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2fsource_5fcontext_2eproto; }; // =================================================================== @@ -190,54 +212,52 @@ class LIBPROTOBUF_EXPORT SourceContext : public ::google::protobuf::Message /* @ // string file_name = 1; inline void SourceContext::clear_file_name() { - file_name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + file_name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline const ::std::string& SourceContext::file_name() const { +inline const std::string& SourceContext::file_name() const { // @@protoc_insertion_point(field_get:google.protobuf.SourceContext.file_name) return file_name_.GetNoArena(); } -inline void SourceContext::set_file_name(const ::std::string& value) { +inline void SourceContext::set_file_name(const std::string& value) { - file_name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value); + file_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); // @@protoc_insertion_point(field_set:google.protobuf.SourceContext.file_name) } -#if LANG_CXX11 -inline void SourceContext::set_file_name(::std::string&& value) { +inline void SourceContext::set_file_name(std::string&& value) { file_name_.SetNoArena( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.SourceContext.file_name) } -#endif inline void SourceContext::set_file_name(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); - file_name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); + file_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); // @@protoc_insertion_point(field_set_char:google.protobuf.SourceContext.file_name) } inline void SourceContext::set_file_name(const char* value, size_t size) { - file_name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + file_name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(reinterpret_cast(value), size)); // @@protoc_insertion_point(field_set_pointer:google.protobuf.SourceContext.file_name) } -inline ::std::string* SourceContext::mutable_file_name() { +inline std::string* SourceContext::mutable_file_name() { // @@protoc_insertion_point(field_mutable:google.protobuf.SourceContext.file_name) - return file_name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return file_name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline ::std::string* SourceContext::release_file_name() { +inline std::string* SourceContext::release_file_name() { // @@protoc_insertion_point(field_release:google.protobuf.SourceContext.file_name) - return file_name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + return file_name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } -inline void SourceContext::set_allocated_file_name(::std::string* file_name) { - if (file_name != NULL) { +inline void SourceContext::set_allocated_file_name(std::string* file_name) { + if (file_name != nullptr) { } else { } - file_name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), file_name); + file_name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), file_name); // @@protoc_insertion_point(field_set_allocated:google.protobuf.SourceContext.file_name) } @@ -247,10 +267,9 @@ inline void SourceContext::set_allocated_file_name(::std::string* file_name) { // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2fsource_5fcontext_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fsource_5fcontext_2eproto diff --git a/src/google/protobuf/struct.pb.cc b/src/google/protobuf/struct.pb.cc index 4294f407da..2f9876fb71 100644 --- a/src/google/protobuf/struct.pb.cc +++ b/src/google/protobuf/struct.pb.cc @@ -5,144 +5,125 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fstruct_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto; -namespace google { -namespace protobuf { +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fstruct_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto; +PROTOBUF_NAMESPACE_OPEN class Struct_FieldsEntry_DoNotUseDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Struct_FieldsEntry_DoNotUse_default_instance_; class StructDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Struct_default_instance_; class ValueDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; int null_value_; double number_value_; - ::google::protobuf::internal::ArenaStringPtr string_value_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr string_value_; bool bool_value_; - const ::google::protobuf::Struct* struct_value_; - const ::google::protobuf::ListValue* list_value_; + const PROTOBUF_NAMESPACE_ID::Struct* struct_value_; + const PROTOBUF_NAMESPACE_ID::ListValue* list_value_; } _Value_default_instance_; class ListValueDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _ListValue_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsListValue_google_2fprotobuf_2fstruct_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_ListValue_google_2fprotobuf_2fstruct_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_Struct_FieldsEntry_DoNotUse_default_instance_; - new (ptr) ::google::protobuf::Struct_FieldsEntry_DoNotUse(); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Struct_FieldsEntry_DoNotUse_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse(); } { - void* ptr = &::google::protobuf::_Struct_default_instance_; - new (ptr) ::google::protobuf::Struct(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Struct_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Struct(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } { - void* ptr = &::google::protobuf::_Value_default_instance_; - new (ptr) ::google::protobuf::Value(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Value_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Value(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } { - void* ptr = &::google::protobuf::_ListValue_default_instance_; - new (ptr) ::google::protobuf::ListValue(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_ListValue_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::ListValue(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::Struct_FieldsEntry_DoNotUse::InitAsDefaultInstance(); - ::google::protobuf::Struct::InitAsDefaultInstance(); - ::google::protobuf::Value::InitAsDefaultInstance(); - ::google::protobuf::ListValue::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Struct::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Value::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::ListValue::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsListValue_google_2fprotobuf_2fstruct_2eproto}, {}}; - -void InitDefaults_google_2fprotobuf_2fstruct_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); -} +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_ListValue_google_2fprotobuf_2fstruct_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2fstruct_2eproto[4]; -const ::google::protobuf::EnumDescriptor* file_level_enum_descriptors_google_2fprotobuf_2fstruct_2eproto[1]; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fstruct_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fstruct_2eproto[4]; +static const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* file_level_enum_descriptors_google_2fprotobuf_2fstruct_2eproto[1]; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fstruct_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2fstruct_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Struct_FieldsEntry_DoNotUse, _has_bits_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Struct_FieldsEntry_DoNotUse, _internal_metadata_), +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2fstruct_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse, _has_bits_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Struct_FieldsEntry_DoNotUse, key_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Struct_FieldsEntry_DoNotUse, value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse, key_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse, value_), 0, 1, ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Struct, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Struct, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Struct, fields_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Struct, fields_), ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Value, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Value, _internal_metadata_), ~0u, // no _extensions_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Value, _oneof_case_[0]), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Value, _oneof_case_[0]), ~0u, // no _weak_field_map_ - offsetof(::google::protobuf::ValueDefaultTypeInternal, null_value_), - offsetof(::google::protobuf::ValueDefaultTypeInternal, number_value_), - offsetof(::google::protobuf::ValueDefaultTypeInternal, string_value_), - offsetof(::google::protobuf::ValueDefaultTypeInternal, bool_value_), - offsetof(::google::protobuf::ValueDefaultTypeInternal, struct_value_), - offsetof(::google::protobuf::ValueDefaultTypeInternal, list_value_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Value, kind_), + offsetof(PROTOBUF_NAMESPACE_ID::ValueDefaultTypeInternal, null_value_), + offsetof(PROTOBUF_NAMESPACE_ID::ValueDefaultTypeInternal, number_value_), + offsetof(PROTOBUF_NAMESPACE_ID::ValueDefaultTypeInternal, string_value_), + offsetof(PROTOBUF_NAMESPACE_ID::ValueDefaultTypeInternal, bool_value_), + offsetof(PROTOBUF_NAMESPACE_ID::ValueDefaultTypeInternal, struct_value_), + offsetof(PROTOBUF_NAMESPACE_ID::ValueDefaultTypeInternal, list_value_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Value, kind_), ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ListValue, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ListValue, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::ListValue, values_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::ListValue, values_), }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, 7, sizeof(::google::protobuf::Struct_FieldsEntry_DoNotUse)}, - { 9, -1, sizeof(::google::protobuf::Struct)}, - { 15, -1, sizeof(::google::protobuf::Value)}, - { 27, -1, sizeof(::google::protobuf::ListValue)}, +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, 7, sizeof(PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse)}, + { 9, -1, sizeof(PROTOBUF_NAMESPACE_ID::Struct)}, + { 15, -1, sizeof(PROTOBUF_NAMESPACE_ID::Value)}, + { 27, -1, sizeof(PROTOBUF_NAMESPACE_ID::ListValue)}, }; -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_Struct_FieldsEntry_DoNotUse_default_instance_), - reinterpret_cast(&::google::protobuf::_Struct_default_instance_), - reinterpret_cast(&::google::protobuf::_Value_default_instance_), - reinterpret_cast(&::google::protobuf::_ListValue_default_instance_), -}; - -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2fstruct_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2fstruct_2eproto, "google/protobuf/struct.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2fstruct_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2fstruct_2eproto, 4, file_level_enum_descriptors_google_2fprotobuf_2fstruct_2eproto, file_level_service_descriptors_google_2fprotobuf_2fstruct_2eproto, +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Struct_FieldsEntry_DoNotUse_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Struct_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Value_default_instance_), + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_ListValue_default_instance_), }; -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fstruct_2eproto = { - false, InitDefaults_google_2fprotobuf_2fstruct_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2fstruct_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n\034google/protobuf/struct.proto\022\017google.p" "rotobuf\"\204\001\n\006Struct\0223\n\006fields\030\001 \003(\0132#.goo" "gle.protobuf.Struct.FieldsEntry\032E\n\013Field" @@ -160,23 +141,26 @@ static ::google::protobuf::Message const * const file_default_instances[] = { "protobuf/ptypes/struct;structpb\370\001\001\242\002\003GPB" "\252\002\036Google.Protobuf.WellKnownTypesb\006proto" "3" -, - "google/protobuf/struct.proto", &assign_descriptors_table_google_2fprotobuf_2fstruct_2eproto, 641, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2fstruct_2eproto_deps[1] = { +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2fstruct_2eproto_sccs[1] = { + &scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fstruct_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2fstruct_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fstruct_2eproto = { + &descriptor_table_google_2fprotobuf_2fstruct_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2fstruct_2eproto, "google/protobuf/struct.proto", 641, + &descriptor_table_google_2fprotobuf_2fstruct_2eproto_once, descriptor_table_google_2fprotobuf_2fstruct_2eproto_sccs, descriptor_table_google_2fprotobuf_2fstruct_2eproto_deps, 1, 0, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2fstruct_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2fstruct_2eproto, 4, file_level_enum_descriptors_google_2fprotobuf_2fstruct_2eproto, file_level_service_descriptors_google_2fprotobuf_2fstruct_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2fstruct_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fstruct_2eproto, deps, 0); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2fstruct_2eproto = []() { AddDescriptors_google_2fprotobuf_2fstruct_2eproto(); return true; }(); -namespace google { -namespace protobuf { -const ::google::protobuf::EnumDescriptor* NullValue_descriptor() { - ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_google_2fprotobuf_2fstruct_2eproto); +static bool dynamic_init_dummy_google_2fprotobuf_2fstruct_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fstruct_2eproto), true); +PROTOBUF_NAMESPACE_OPEN +const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* NullValue_descriptor() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_google_2fprotobuf_2fstruct_2eproto); return file_level_enum_descriptors_google_2fprotobuf_2fstruct_2eproto[0]; } bool NullValue_IsValid(int value) { @@ -192,44 +176,35 @@ bool NullValue_IsValid(int value) { // =================================================================== Struct_FieldsEntry_DoNotUse::Struct_FieldsEntry_DoNotUse() {} -Struct_FieldsEntry_DoNotUse::Struct_FieldsEntry_DoNotUse(::google::protobuf::Arena* arena) +Struct_FieldsEntry_DoNotUse::Struct_FieldsEntry_DoNotUse(::PROTOBUF_NAMESPACE_ID::Arena* arena) : SuperType(arena) {} void Struct_FieldsEntry_DoNotUse::MergeFrom(const Struct_FieldsEntry_DoNotUse& other) { MergeFromInternal(other); } -::google::protobuf::Metadata Struct_FieldsEntry_DoNotUse::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fstruct_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fstruct_2eproto[0]; +::PROTOBUF_NAMESPACE_ID::Metadata Struct_FieldsEntry_DoNotUse::GetMetadata() const { + return GetMetadataStatic(); } void Struct_FieldsEntry_DoNotUse::MergeFrom( - const ::google::protobuf::Message& other) { - ::google::protobuf::Message::MergeFrom(other); + const ::PROTOBUF_NAMESPACE_ID::Message& other) { + ::PROTOBUF_NAMESPACE_ID::Message::MergeFrom(other); } -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Struct_FieldsEntry_DoNotUse::_InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx) { return end; } -#endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - // =================================================================== void Struct::InitAsDefaultInstance() { } -class Struct::HasBitSetters { +class Struct::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Struct::kFieldsFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Struct::Struct() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Struct) } -Struct::Struct(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +Struct::Struct(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), fields_(arena) { SharedCtor(); @@ -237,16 +212,15 @@ Struct::Struct(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.Struct) } Struct::Struct(const Struct& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr) { _internal_metadata_.MergeFrom(from._internal_metadata_); fields_.MergeFrom(from.fields_); // @@protoc_insertion_point(copy_constructor:google.protobuf.Struct) } void Struct::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); } Struct::~Struct() { @@ -255,27 +229,27 @@ Struct::~Struct() { } void Struct::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void Struct::ArenaDtor(void* object) { Struct* _this = reinterpret_cast< Struct* >(object); (void)_this; } -void Struct::RegisterArenaDtor(::google::protobuf::Arena*) { +void Struct::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void Struct::SetCachedSize(int size) const { _cached_size_.Set(size); } const Struct& Struct::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); return *internal_default_instance(); } void Struct::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Struct) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -284,85 +258,71 @@ void Struct::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Struct::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Struct::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // map fields = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::Struct_FieldsEntry_DoNotUse::_InternalParse; - object = msg->mutable_fields(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 10 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(&fields_, ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 10); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Struct::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Struct) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // map fields = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - Struct_FieldsEntry_DoNotUse::Parser< ::google::protobuf::internal::MapField< + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + Struct_FieldsEntry_DoNotUse::Parser< ::PROTOBUF_NAMESPACE_ID::internal::MapField< Struct_FieldsEntry_DoNotUse, - ::std::string, ::google::protobuf::Value, - ::google::protobuf::internal::WireFormatLite::TYPE_STRING, - ::google::protobuf::internal::WireFormatLite::TYPE_MESSAGE, + std::string, PROTOBUF_NAMESPACE_ID::Value, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_STRING, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_MESSAGE, 0 >, - ::google::protobuf::Map< ::std::string, ::google::protobuf::Value > > parser(&fields_); - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + ::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value > > parser(&fields_); + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessageNoVirtual( input, &parser)); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( parser.key().data(), static_cast(parser.key().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Struct.FieldsEntry.key")); } else { goto handle_unusual; @@ -375,7 +335,7 @@ bool Struct::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -391,131 +351,56 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Struct::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Struct) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // map fields = 1; - if (!this->fields().empty()) { - typedef ::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::const_pointer - ConstPtr; - typedef ConstPtr SortItem; - typedef ::google::protobuf::internal::CompareByDerefFirst Less; - struct Utf8Check { - static void Check(ConstPtr p) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - p->first.data(), static_cast(p->first.length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Struct.FieldsEntry.key"); - } - }; - - if (output->IsSerializationDeterministic() && - this->fields().size() > 1) { - ::std::unique_ptr items( - new SortItem[this->fields().size()]); - typedef ::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::size_type size_type; - size_type n = 0; - for (::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::const_iterator - it = this->fields().begin(); - it != this->fields().end(); ++it, ++n) { - items[static_cast(n)] = SortItem(&*it); - } - ::std::sort(&items[0], &items[static_cast(n)], Less()); - ::std::unique_ptr entry; - for (size_type i = 0; i < n; i++) { - entry.reset(fields_.NewEntryWrapper(items[static_cast(i)]->first, items[static_cast(i)]->second)); - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(1, *entry, output); - if (entry->GetArena() != NULL) { - entry.release(); - } - Utf8Check::Check(&(*items[static_cast(i)])); - } - } else { - ::std::unique_ptr entry; - for (::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::const_iterator - it = this->fields().begin(); - it != this->fields().end(); ++it) { - entry.reset(fields_.NewEntryWrapper(it->first, it->second)); - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(1, *entry, output); - if (entry->GetArena() != NULL) { - entry.release(); - } - Utf8Check::Check(&(*it)); - } - } - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Struct) -} - -::google::protobuf::uint8* Struct::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Struct::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Struct) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // map fields = 1; if (!this->fields().empty()) { - typedef ::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::const_pointer + typedef ::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >::const_pointer ConstPtr; typedef ConstPtr SortItem; - typedef ::google::protobuf::internal::CompareByDerefFirst Less; + typedef ::PROTOBUF_NAMESPACE_ID::internal::CompareByDerefFirst Less; struct Utf8Check { static void Check(ConstPtr p) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( p->first.data(), static_cast(p->first.length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Struct.FieldsEntry.key"); } }; - if (deterministic && + if (stream->IsSerializationDeterministic() && this->fields().size() > 1) { ::std::unique_ptr items( new SortItem[this->fields().size()]); - typedef ::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::size_type size_type; + typedef ::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >::size_type size_type; size_type n = 0; - for (::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::const_iterator + for (::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >::const_iterator it = this->fields().begin(); it != this->fields().end(); ++it, ++n) { items[static_cast(n)] = SortItem(&*it); } ::std::sort(&items[0], &items[static_cast(n)], Less()); - ::std::unique_ptr entry; for (size_type i = 0; i < n; i++) { - entry.reset(fields_.NewEntryWrapper(items[static_cast(i)]->first, items[static_cast(i)]->second)); - target = ::google::protobuf::internal::WireFormatLite::InternalWriteMessageNoVirtualToArray(1, *entry, deterministic, target); - if (entry->GetArena() != NULL) { - entry.release(); - } + target = Struct_FieldsEntry_DoNotUse::Funcs::InternalSerialize(1, items[static_cast(i)]->first, items[static_cast(i)]->second, target, stream); Utf8Check::Check(&(*items[static_cast(i)])); } } else { - ::std::unique_ptr entry; - for (::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::const_iterator + for (::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >::const_iterator it = this->fields().begin(); it != this->fields().end(); ++it) { - entry.reset(fields_.NewEntryWrapper(it->first, it->second)); - target = ::google::protobuf::internal::WireFormatLite::InternalWriteMessageNoVirtualToArray(1, *entry, deterministic, target); - if (entry->GetArena() != NULL) { - entry.release(); - } + target = Struct_FieldsEntry_DoNotUse::Funcs::InternalSerialize(1, it->first, it->second, target, stream); Utf8Check::Check(&(*it)); } } } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Struct) return target; @@ -525,49 +410,37 @@ size_t Struct::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Struct) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // map fields = 1; total_size += 1 * - ::google::protobuf::internal::FromIntSize(this->fields_size()); - { - ::std::unique_ptr entry; - for (::google::protobuf::Map< ::std::string, ::google::protobuf::Value >::const_iterator - it = this->fields().begin(); - it != this->fields().end(); ++it) { - if (entry.get() != NULL && entry->GetArena() != NULL) { - entry.release(); - } - entry.reset(fields_.NewEntryWrapper(it->first, it->second)); - total_size += ::google::protobuf::internal::WireFormatLite:: - MessageSizeNoVirtual(*entry); - } - if (entry.get() != NULL && entry->GetArena() != NULL) { - entry.release(); - } + ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(this->fields_size()); + for (::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >::const_iterator + it = this->fields().begin(); + it != this->fields().end(); ++it) { + total_size += Struct_FieldsEntry_DoNotUse::Funcs::ByteSizeLong(it->first, it->second); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Struct::MergeFrom(const ::google::protobuf::Message& from) { +void Struct::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Struct) GOOGLE_DCHECK_NE(&from, this); const Struct* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Struct) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Struct) MergeFrom(*source); @@ -578,13 +451,13 @@ void Struct::MergeFrom(const Struct& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Struct) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; fields_.MergeFrom(from.fields_); } -void Struct::CopyFrom(const ::google::protobuf::Message& from) { +void Struct::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Struct) if (&from == this) return; Clear(); @@ -602,72 +475,52 @@ bool Struct::IsInitialized() const { return true; } -void Struct::Swap(Struct* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - Struct* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void Struct::UnsafeArenaSwap(Struct* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void Struct::InternalSwap(Struct* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); fields_.Swap(&other->fields_); } -::google::protobuf::Metadata Struct::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fstruct_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fstruct_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Struct::GetMetadata() const { + return GetMetadataStatic(); } // =================================================================== void Value::InitAsDefaultInstance() { - ::google::protobuf::_Value_default_instance_.null_value_ = 0; - ::google::protobuf::_Value_default_instance_.number_value_ = 0; - ::google::protobuf::_Value_default_instance_.string_value_.UnsafeSetDefault( - &::google::protobuf::internal::GetEmptyStringAlreadyInited()); - ::google::protobuf::_Value_default_instance_.bool_value_ = false; - ::google::protobuf::_Value_default_instance_.struct_value_ = const_cast< ::google::protobuf::Struct*>( - ::google::protobuf::Struct::internal_default_instance()); - ::google::protobuf::_Value_default_instance_.list_value_ = const_cast< ::google::protobuf::ListValue*>( - ::google::protobuf::ListValue::internal_default_instance()); -} -class Value::HasBitSetters { + PROTOBUF_NAMESPACE_ID::_Value_default_instance_.null_value_ = 0; + PROTOBUF_NAMESPACE_ID::_Value_default_instance_.number_value_ = 0; + PROTOBUF_NAMESPACE_ID::_Value_default_instance_.string_value_.UnsafeSetDefault( + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); + PROTOBUF_NAMESPACE_ID::_Value_default_instance_.bool_value_ = false; + PROTOBUF_NAMESPACE_ID::_Value_default_instance_.struct_value_ = const_cast< PROTOBUF_NAMESPACE_ID::Struct*>( + PROTOBUF_NAMESPACE_ID::Struct::internal_default_instance()); + PROTOBUF_NAMESPACE_ID::_Value_default_instance_.list_value_ = const_cast< PROTOBUF_NAMESPACE_ID::ListValue*>( + PROTOBUF_NAMESPACE_ID::ListValue::internal_default_instance()); +} +class Value::_Internal { public: - static const ::google::protobuf::Struct& struct_value(const Value* msg); - static const ::google::protobuf::ListValue& list_value(const Value* msg); + static const PROTOBUF_NAMESPACE_ID::Struct& struct_value(const Value* msg); + static const PROTOBUF_NAMESPACE_ID::ListValue& list_value(const Value* msg); }; -const ::google::protobuf::Struct& -Value::HasBitSetters::struct_value(const Value* msg) { +const PROTOBUF_NAMESPACE_ID::Struct& +Value::_Internal::struct_value(const Value* msg) { return *msg->kind_.struct_value_; } -const ::google::protobuf::ListValue& -Value::HasBitSetters::list_value(const Value* msg) { +const PROTOBUF_NAMESPACE_ID::ListValue& +Value::_Internal::list_value(const Value* msg) { return *msg->kind_.list_value_; } -void Value::set_allocated_struct_value(::google::protobuf::Struct* struct_value) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); +void Value::set_allocated_struct_value(PROTOBUF_NAMESPACE_ID::Struct* struct_value) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); clear_kind(); if (struct_value) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(struct_value); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(struct_value); if (message_arena != submessage_arena) { - struct_value = ::google::protobuf::internal::GetOwnedMessage( + struct_value = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, struct_value, submessage_arena); } set_has_struct_value(); @@ -675,14 +528,14 @@ void Value::set_allocated_struct_value(::google::protobuf::Struct* struct_value) } // @@protoc_insertion_point(field_set_allocated:google.protobuf.Value.struct_value) } -void Value::set_allocated_list_value(::google::protobuf::ListValue* list_value) { - ::google::protobuf::Arena* message_arena = GetArenaNoVirtual(); +void Value::set_allocated_list_value(PROTOBUF_NAMESPACE_ID::ListValue* list_value) { + ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaNoVirtual(); clear_kind(); if (list_value) { - ::google::protobuf::Arena* submessage_arena = - ::google::protobuf::Arena::GetArena(list_value); + ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = + ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(list_value); if (message_arena != submessage_arena) { - list_value = ::google::protobuf::internal::GetOwnedMessage( + list_value = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( message_arena, list_value, submessage_arena); } set_has_list_value(); @@ -690,30 +543,21 @@ void Value::set_allocated_list_value(::google::protobuf::ListValue* list_value) } // @@protoc_insertion_point(field_set_allocated:google.protobuf.Value.list_value) } -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Value::kNullValueFieldNumber; -const int Value::kNumberValueFieldNumber; -const int Value::kStringValueFieldNumber; -const int Value::kBoolValueFieldNumber; -const int Value::kStructValueFieldNumber; -const int Value::kListValueFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Value::Value() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Value) } -Value::Value(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +Value::Value(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.Value) } Value::Value(const Value& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr) { _internal_metadata_.MergeFrom(from._internal_metadata_); clear_has_kind(); switch (from.kind_case()) { @@ -734,11 +578,11 @@ Value::Value(const Value& from) break; } case kStructValue: { - mutable_struct_value()->::google::protobuf::Struct::MergeFrom(from.struct_value()); + mutable_struct_value()->PROTOBUF_NAMESPACE_ID::Struct::MergeFrom(from.struct_value()); break; } case kListValue: { - mutable_list_value()->::google::protobuf::ListValue::MergeFrom(from.list_value()); + mutable_list_value()->PROTOBUF_NAMESPACE_ID::ListValue::MergeFrom(from.list_value()); break; } case KIND_NOT_SET: { @@ -749,8 +593,7 @@ Value::Value(const Value& from) } void Value::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); clear_has_kind(); } @@ -760,7 +603,7 @@ Value::~Value() { } void Value::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); if (has_kind()) { clear_kind(); } @@ -770,13 +613,13 @@ void Value::ArenaDtor(void* object) { Value* _this = reinterpret_cast< Value* >(object); (void)_this; } -void Value::RegisterArenaDtor(::google::protobuf::Arena*) { +void Value::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void Value::SetCachedSize(int size) const { _cached_size_.Set(size); } const Value& Value::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); return *internal_default_instance(); } @@ -793,7 +636,7 @@ void Value::clear_kind() { break; } case kStringValue: { - kind_.string_value_.Destroy(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + kind_.string_value_.Destroy(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); break; } @@ -802,13 +645,13 @@ void Value::clear_kind() { break; } case kStructValue: { - if (GetArenaNoVirtual() == NULL) { + if (GetArenaNoVirtual() == nullptr) { delete kind_.struct_value_; } break; } case kListValue: { - if (GetArenaNoVirtual() == NULL) { + if (GetArenaNoVirtual() == nullptr) { delete kind_.list_value_; } break; @@ -823,7 +666,7 @@ void Value::clear_kind() { void Value::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Value) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -832,133 +675,95 @@ void Value::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Value::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // .google.protobuf.NullValue null_value = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::NullValue value = static_cast<::google::protobuf::NullValue>(val); - msg->set_null_value(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + set_null_value(static_cast(val)); + } else goto handle_unusual; + continue; // double number_value = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 17) goto handle_unusual; - double val; - ::std::memcpy(&val, ptr, 8); - ptr += 8; - msg->set_number_value(val); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 17)) { + set_number_value(::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad(ptr)); + ptr += sizeof(double); + } else goto handle_unusual; + continue; // string string_value = 3; - case 3: { - if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - ctx->extra_parse_data().SetFieldName("google.protobuf.Value.string_value"); - parser_till_end = ::google::protobuf::internal::StringParserUTF8; - ::std::string* str = msg->mutable_string_value(); - str->clear(); - object = str; - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 3: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { + ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_string_value(), ptr, ctx, "google.protobuf.Value.string_value"); + CHK_(ptr); + } else goto handle_unusual; + continue; // bool bool_value = 4; - case 4: { - if (static_cast<::google::protobuf::uint8>(tag) != 32) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - bool value = val; - msg->set_bool_value(value); - break; - } + case 4: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 32)) { + set_bool_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); + CHK_(ptr); + } else goto handle_unusual; + continue; // .google.protobuf.Struct struct_value = 5; - case 5: { - if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::Struct::_InternalParse; - object = msg->mutable_struct_value(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 42)) { + ptr = ctx->ParseMessage(mutable_struct_value(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // .google.protobuf.ListValue list_value = 6; - case 6: { - if (static_cast<::google::protobuf::uint8>(tag) != 50) goto handle_unusual; - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::ListValue::_InternalParse; - object = msg->mutable_list_value(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - break; - } + case 6: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 50)) { + ptr = ctx->ParseMessage(mutable_list_value(), ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Value::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Value) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // .google.protobuf.NullValue null_value = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { int value = 0; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + int, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_ENUM>( input, &value))); - set_null_value(static_cast< ::google::protobuf::NullValue >(value)); + set_null_value(static_cast< PROTOBUF_NAMESPACE_ID::NullValue >(value)); } else { goto handle_unusual; } @@ -967,10 +772,10 @@ bool Value::MergePartialFromCodedStream( // double number_value = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (17 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (17 & 0xFF)) { clear_kind(); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - double, ::google::protobuf::internal::WireFormatLite::TYPE_DOUBLE>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + double, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_DOUBLE>( input, &kind_.number_value_))); set_has_number_value(); } else { @@ -981,12 +786,12 @@ bool Value::MergePartialFromCodedStream( // string string_value = 3; case 3: { - if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (26 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadString( input, this->mutable_string_value())); - DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->string_value().data(), static_cast(this->string_value().length()), - ::google::protobuf::internal::WireFormatLite::PARSE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Value.string_value")); } else { goto handle_unusual; @@ -996,10 +801,10 @@ bool Value::MergePartialFromCodedStream( // bool bool_value = 4; case 4: { - if (static_cast< ::google::protobuf::uint8>(tag) == (32 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (32 & 0xFF)) { clear_kind(); - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &kind_.bool_value_))); set_has_bool_value(); } else { @@ -1010,8 +815,8 @@ bool Value::MergePartialFromCodedStream( // .google.protobuf.Struct struct_value = 5; case 5: { - if (static_cast< ::google::protobuf::uint8>(tag) == (42 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (42 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_struct_value())); } else { goto handle_unusual; @@ -1021,8 +826,8 @@ bool Value::MergePartialFromCodedStream( // .google.protobuf.ListValue list_value = 6; case 6: { - if (static_cast< ::google::protobuf::uint8>(tag) == (50 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (50 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, mutable_list_value())); } else { goto handle_unusual; @@ -1035,7 +840,7 @@ bool Value::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -1051,108 +856,60 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Value::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Value) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // .google.protobuf.NullValue null_value = 1; - if (has_null_value()) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 1, this->null_value(), output); - } - - // double number_value = 2; - if (has_number_value()) { - ::google::protobuf::internal::WireFormatLite::WriteDouble(2, this->number_value(), output); - } - - // string string_value = 3; - if (has_string_value()) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - this->string_value().data(), static_cast(this->string_value().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, - "google.protobuf.Value.string_value"); - ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( - 3, this->string_value(), output); - } - - // bool bool_value = 4; - if (has_bool_value()) { - ::google::protobuf::internal::WireFormatLite::WriteBool(4, this->bool_value(), output); - } - - // .google.protobuf.Struct struct_value = 5; - if (has_struct_value()) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 5, HasBitSetters::struct_value(this), output); - } - - // .google.protobuf.ListValue list_value = 6; - if (has_list_value()) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 6, HasBitSetters::list_value(this), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Value) -} - -::google::protobuf::uint8* Value::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Value::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Value) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // .google.protobuf.NullValue null_value = 1; if (has_null_value()) { - target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( 1, this->null_value(), target); } // double number_value = 2; if (has_number_value()) { - target = ::google::protobuf::internal::WireFormatLite::WriteDoubleToArray(2, this->number_value(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteDoubleToArray(2, this->number_value(), target); } // string string_value = 3; if (has_string_value()) { - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( this->string_value().data(), static_cast(this->string_value().length()), - ::google::protobuf::internal::WireFormatLite::SERIALIZE, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, "google.protobuf.Value.string_value"); - target = - ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + target = stream->WriteStringMaybeAliased( 3, this->string_value(), target); } // bool bool_value = 4; if (has_bool_value()) { - target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(4, this->bool_value(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(4, this->bool_value(), target); } // .google.protobuf.Struct struct_value = 5; if (has_struct_value()) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 5, HasBitSetters::struct_value(this), deterministic, target); + 5, _Internal::struct_value(this), target, stream); } // .google.protobuf.ListValue list_value = 6; if (has_list_value()) { - target = ::google::protobuf::internal::WireFormatLite:: + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: InternalWriteMessageToArray( - 6, HasBitSetters::list_value(this), deterministic, target); + 6, _Internal::list_value(this), target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Value) return target; @@ -1162,12 +919,7 @@ size_t Value::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Value) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1175,7 +927,7 @@ size_t Value::ByteSizeLong() const { // .google.protobuf.NullValue null_value = 1; case kNullValue: { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->null_value()); + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->null_value()); break; } // double number_value = 2; @@ -1186,7 +938,7 @@ size_t Value::ByteSizeLong() const { // string string_value = 3; case kStringValue: { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( this->string_value()); break; } @@ -1198,14 +950,14 @@ size_t Value::ByteSizeLong() const { // .google.protobuf.Struct struct_value = 5; case kStructValue: { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *kind_.struct_value_); break; } // .google.protobuf.ListValue list_value = 6; case kListValue: { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( *kind_.list_value_); break; } @@ -1213,20 +965,24 @@ size_t Value::ByteSizeLong() const { break; } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Value::MergeFrom(const ::google::protobuf::Message& from) { +void Value::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Value) GOOGLE_DCHECK_NE(&from, this); const Value* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Value) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Value) MergeFrom(*source); @@ -1237,7 +993,7 @@ void Value::MergeFrom(const Value& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Value) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; switch (from.kind_case()) { @@ -1258,11 +1014,11 @@ void Value::MergeFrom(const Value& from) { break; } case kStructValue: { - mutable_struct_value()->::google::protobuf::Struct::MergeFrom(from.struct_value()); + mutable_struct_value()->PROTOBUF_NAMESPACE_ID::Struct::MergeFrom(from.struct_value()); break; } case kListValue: { - mutable_list_value()->::google::protobuf::ListValue::MergeFrom(from.list_value()); + mutable_list_value()->PROTOBUF_NAMESPACE_ID::ListValue::MergeFrom(from.list_value()); break; } case KIND_NOT_SET: { @@ -1271,7 +1027,7 @@ void Value::MergeFrom(const Value& from) { } } -void Value::CopyFrom(const ::google::protobuf::Message& from) { +void Value::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Value) if (&from == this) return; Clear(); @@ -1289,25 +1045,6 @@ bool Value::IsInitialized() const { return true; } -void Value::Swap(Value* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - Value* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void Value::UnsafeArenaSwap(Value* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void Value::InternalSwap(Value* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -1315,9 +1052,8 @@ void Value::InternalSwap(Value* other) { swap(_oneof_case_[0], other->_oneof_case_[0]); } -::google::protobuf::Metadata Value::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fstruct_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fstruct_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Value::GetMetadata() const { + return GetMetadataStatic(); } @@ -1325,21 +1061,17 @@ void Value::InternalSwap(Value* other) { void ListValue::InitAsDefaultInstance() { } -class ListValue::HasBitSetters { +class ListValue::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int ListValue::kValuesFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - ListValue::ListValue() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.ListValue) } -ListValue::ListValue(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +ListValue::ListValue(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena), values_(arena) { SharedCtor(); @@ -1347,16 +1079,15 @@ ListValue::ListValue(::google::protobuf::Arena* arena) // @@protoc_insertion_point(arena_constructor:google.protobuf.ListValue) } ListValue::ListValue(const ListValue& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL), + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr), values_(from.values_) { _internal_metadata_.MergeFrom(from._internal_metadata_); // @@protoc_insertion_point(copy_constructor:google.protobuf.ListValue) } void ListValue::SharedCtor() { - ::google::protobuf::internal::InitSCC( - &scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); } ListValue::~ListValue() { @@ -1365,27 +1096,27 @@ ListValue::~ListValue() { } void ListValue::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void ListValue::ArenaDtor(void* object) { ListValue* _this = reinterpret_cast< ListValue* >(object); (void)_this; } -void ListValue::RegisterArenaDtor(::google::protobuf::Arena*) { +void ListValue::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void ListValue::SetCachedSize(int size) const { _cached_size_.Set(size); } const ListValue& ListValue::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); return *internal_default_instance(); } void ListValue::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.ListValue) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1394,74 +1125,60 @@ void ListValue::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* ListValue::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* ListValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // repeated .google.protobuf.Value values = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual; - do { - ptr = Varint::Parse32Inline(ptr, &size); - if (!ptr) goto error; - parser_till_end = ::google::protobuf::Value::_InternalParse; - object = msg->add_values(); - if (size > end - ptr) goto len_delim_till_end; - auto newend = ptr + size; - if (!ctx->ParseExactRange({parser_till_end, object}, ptr, newend)) goto error; - ptr = newend; - if (ptr >= end) break; - } while((*reinterpret_cast(ptr) & 255) == 10 && (ptr += 1)); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { + ptr -= 1; + do { + ptr += 1; + ptr = ctx->ParseMessage(add_values(), ptr); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + } while (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) == 10); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ListValue::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.ListValue) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated .google.protobuf.Value values = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessage( + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (10 & 0xFF)) { + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadMessage( input, add_values())); } else { goto handle_unusual; @@ -1474,7 +1191,7 @@ bool ListValue::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -1490,46 +1207,23 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void ListValue::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.ListValue) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .google.protobuf.Value values = 1; - for (unsigned int i = 0, - n = static_cast(this->values_size()); i < n; i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( - 1, - this->values(static_cast(i)), - output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.ListValue) -} - -::google::protobuf::uint8* ListValue::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* ListValue::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.ListValue) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // repeated .google.protobuf.Value values = 1; - for (unsigned int i = 0, - n = static_cast(this->values_size()); i < n; i++) { - target = ::google::protobuf::internal::WireFormatLite:: - InternalWriteMessageToArray( - 1, this->values(static_cast(i)), deterministic, target); + for (auto it = this->values().pointer_begin(), + end = this->values().pointer_end(); it < end; ++it) { + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: + InternalWriteMessageToArray(1, **it, target, stream); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.ListValue) return target; @@ -1539,12 +1233,7 @@ size_t ListValue::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.ListValue) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -1554,25 +1243,29 @@ size_t ListValue::ByteSizeLong() const { total_size += 1UL * count; for (unsigned int i = 0; i < count; i++) { total_size += - ::google::protobuf::internal::WireFormatLite::MessageSize( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( this->values(static_cast(i))); } } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void ListValue::MergeFrom(const ::google::protobuf::Message& from) { +void ListValue::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.ListValue) GOOGLE_DCHECK_NE(&from, this); const ListValue* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.ListValue) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.ListValue) MergeFrom(*source); @@ -1583,13 +1276,13 @@ void ListValue::MergeFrom(const ListValue& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.ListValue) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; values_.MergeFrom(from.values_); } -void ListValue::CopyFrom(const ::google::protobuf::Message& from) { +void ListValue::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.ListValue) if (&from == this) return; Clear(); @@ -1607,55 +1300,33 @@ bool ListValue::IsInitialized() const { return true; } -void ListValue::Swap(ListValue* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - ListValue* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void ListValue::UnsafeArenaSwap(ListValue* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void ListValue::InternalSwap(ListValue* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); CastToBase(&values_)->InternalSwap(CastToBase(&other->values_)); } -::google::protobuf::Metadata ListValue::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2fstruct_2eproto); - return ::file_level_metadata_google_2fprotobuf_2fstruct_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata ListValue::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Struct_FieldsEntry_DoNotUse* Arena::CreateMaybeMessage< ::google::protobuf::Struct_FieldsEntry_DoNotUse >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::Struct_FieldsEntry_DoNotUse >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Struct* Arena::CreateMaybeMessage< ::google::protobuf::Struct >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::Struct >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Struct* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Struct >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::Struct >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Value* Arena::CreateMaybeMessage< ::google::protobuf::Value >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::Value >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Value* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Value >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::Value >(arena); } -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::ListValue* Arena::CreateMaybeMessage< ::google::protobuf::ListValue >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::ListValue >(arena); +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::ListValue* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::ListValue >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::ListValue >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/struct.pb.h b/src/google/protobuf/struct.pb.h index fa1f568d7c..c7c08f1572 100644 --- a/src/google/protobuf/struct.pb.h +++ b/src/google/protobuf/struct.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/struct.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2fstruct_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2fstruct_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fstruct_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fstruct_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,6 +27,7 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export @@ -36,107 +38,122 @@ #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fstruct_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fstruct_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fstruct_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[4] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2fstruct_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[4] + 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2fstruct_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fstruct_2eproto; +PROTOBUF_NAMESPACE_OPEN class ListValue; class ListValueDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern ListValueDefaultTypeInternal _ListValue_default_instance_; +PROTOBUF_EXPORT extern ListValueDefaultTypeInternal _ListValue_default_instance_; class Struct; class StructDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern StructDefaultTypeInternal _Struct_default_instance_; +PROTOBUF_EXPORT extern StructDefaultTypeInternal _Struct_default_instance_; class Struct_FieldsEntry_DoNotUse; class Struct_FieldsEntry_DoNotUseDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern Struct_FieldsEntry_DoNotUseDefaultTypeInternal _Struct_FieldsEntry_DoNotUse_default_instance_; +PROTOBUF_EXPORT extern Struct_FieldsEntry_DoNotUseDefaultTypeInternal _Struct_FieldsEntry_DoNotUse_default_instance_; class Value; class ValueDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern ValueDefaultTypeInternal _Value_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::ListValue* Arena::CreateMaybeMessage<::google::protobuf::ListValue>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Struct* Arena::CreateMaybeMessage<::google::protobuf::Struct>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Struct_FieldsEntry_DoNotUse* Arena::CreateMaybeMessage<::google::protobuf::Struct_FieldsEntry_DoNotUse>(Arena*); -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Value* Arena::CreateMaybeMessage<::google::protobuf::Value>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { - -enum NullValue { +PROTOBUF_EXPORT extern ValueDefaultTypeInternal _Value_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::ListValue* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Struct* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Struct_FieldsEntry_DoNotUse* Arena::CreateMaybeMessage(Arena*); +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Value* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN + +enum NullValue : int { NULL_VALUE = 0, - NullValue_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits<::google::protobuf::int32>::min(), - NullValue_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits<::google::protobuf::int32>::max() + NullValue_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits<::PROTOBUF_NAMESPACE_ID::int32>::min(), + NullValue_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits<::PROTOBUF_NAMESPACE_ID::int32>::max() }; -LIBPROTOBUF_EXPORT bool NullValue_IsValid(int value); -const NullValue NullValue_MIN = NULL_VALUE; -const NullValue NullValue_MAX = NULL_VALUE; -const int NullValue_ARRAYSIZE = NullValue_MAX + 1; - -LIBPROTOBUF_EXPORT const ::google::protobuf::EnumDescriptor* NullValue_descriptor(); -inline const ::std::string& NullValue_Name(NullValue value) { - return ::google::protobuf::internal::NameOfEnum( - NullValue_descriptor(), value); +PROTOBUF_EXPORT bool NullValue_IsValid(int value); +constexpr NullValue NullValue_MIN = NULL_VALUE; +constexpr NullValue NullValue_MAX = NULL_VALUE; +constexpr int NullValue_ARRAYSIZE = NullValue_MAX + 1; + +PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* NullValue_descriptor(); +template +inline const std::string& NullValue_Name(T enum_t_value) { + static_assert(::std::is_same::value || + ::std::is_integral::value, + "Incorrect type passed to function NullValue_Name."); + return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( + NullValue_descriptor(), enum_t_value); } inline bool NullValue_Parse( - const ::std::string& name, NullValue* value) { - return ::google::protobuf::internal::ParseNamedEnum( + const std::string& name, NullValue* value) { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( NullValue_descriptor(), name, value); } // =================================================================== -class Struct_FieldsEntry_DoNotUse : public ::google::protobuf::internal::MapEntry { public: -#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); -#endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - typedef ::google::protobuf::internal::MapEntry SuperType; Struct_FieldsEntry_DoNotUse(); - Struct_FieldsEntry_DoNotUse(::google::protobuf::Arena* arena); + Struct_FieldsEntry_DoNotUse(::PROTOBUF_NAMESPACE_ID::Arena* arena); void MergeFrom(const Struct_FieldsEntry_DoNotUse& other); static const Struct_FieldsEntry_DoNotUse* internal_default_instance() { return reinterpret_cast(&_Struct_FieldsEntry_DoNotUse_default_instance_); } - void MergeFrom(const ::google::protobuf::Message& other) final; - ::google::protobuf::Metadata GetMetadata() const; + static bool ValidateKey(std::string* s) { + return ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String(s->data(), s->size(), ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Struct.FieldsEntry.key"); + } + static bool ValidateValue(void*) { return true; } + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& other) final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fstruct_2eproto); + return ::descriptor_table_google_2fprotobuf_2fstruct_2eproto.file_level_metadata[0]; + } + + public: }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT Struct : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Struct) */ { +class PROTOBUF_EXPORT Struct : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Struct) */ { public: Struct(); virtual ~Struct(); Struct(const Struct& from); - - inline Struct& operator=(const Struct& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Struct(Struct&& from) noexcept : Struct() { *this = ::std::move(from); } + inline Struct& operator=(const Struct& from) { + CopyFrom(from); + return *this; + } inline Struct& operator=(Struct&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -145,15 +162,21 @@ class LIBPROTOBUF_EXPORT Struct : public ::google::protobuf::Message /* @@protoc } return *this; } - #endif - inline ::google::protobuf::Arena* GetArena() const final { + + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Struct& default_instance(); @@ -165,54 +188,66 @@ class LIBPROTOBUF_EXPORT Struct : public ::google::protobuf::Message /* @@protoc static constexpr int kIndexInFileMessages = 1; - void UnsafeArenaSwap(Struct* other); - void Swap(Struct* other); friend void swap(Struct& a, Struct& b) { a.Swap(&b); } + inline void Swap(Struct* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(Struct* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Struct* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Struct* New(::google::protobuf::Arena* arena) const final { + Struct* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Struct& from); void MergeFrom(const Struct& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Struct* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Struct"; + } protected: - explicit Struct(::google::protobuf::Arena* arena); + explicit Struct(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -220,58 +255,66 @@ class LIBPROTOBUF_EXPORT Struct : public ::google::protobuf::Message /* @@protoc } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fstruct_2eproto); + return ::descriptor_table_google_2fprotobuf_2fstruct_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kFieldsFieldNumber = 1, + }; // map fields = 1; int fields_size() const; void clear_fields(); - static const int kFieldsFieldNumber = 1; - const ::google::protobuf::Map< ::std::string, ::google::protobuf::Value >& + const ::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >& fields() const; - ::google::protobuf::Map< ::std::string, ::google::protobuf::Value >* + ::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >* mutable_fields(); // @@protoc_insertion_point(class_scope:google.protobuf.Struct) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::internal::MapField< + ::PROTOBUF_NAMESPACE_ID::internal::MapField< Struct_FieldsEntry_DoNotUse, - ::std::string, ::google::protobuf::Value, - ::google::protobuf::internal::WireFormatLite::TYPE_STRING, - ::google::protobuf::internal::WireFormatLite::TYPE_MESSAGE, + std::string, PROTOBUF_NAMESPACE_ID::Value, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_STRING, + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_MESSAGE, 0 > fields_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2fstruct_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT Value : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Value) */ { +class PROTOBUF_EXPORT Value : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Value) */ { public: Value(); virtual ~Value(); Value(const Value& from); - - inline Value& operator=(const Value& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Value(Value&& from) noexcept : Value() { *this = ::std::move(from); } + inline Value& operator=(const Value& from) { + CopyFrom(from); + return *this; + } inline Value& operator=(Value&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -280,15 +323,21 @@ class LIBPROTOBUF_EXPORT Value : public ::google::protobuf::Message /* @@protoc_ } return *this; } - #endif - inline ::google::protobuf::Arena* GetArena() const final { + + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Value& default_instance(); @@ -310,54 +359,66 @@ class LIBPROTOBUF_EXPORT Value : public ::google::protobuf::Message /* @@protoc_ static constexpr int kIndexInFileMessages = 2; - void UnsafeArenaSwap(Value* other); - void Swap(Value* other); friend void swap(Value& a, Value& b) { a.Swap(&b); } + inline void Swap(Value* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(Value* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Value* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Value* New(::google::protobuf::Arena* arena) const final { + Value* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Value& from); void MergeFrom(const Value& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Value* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Value"; + } protected: - explicit Value(::google::protobuf::Arena* arena); + explicit Value(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -365,27 +426,40 @@ class LIBPROTOBUF_EXPORT Value : public ::google::protobuf::Message /* @@protoc_ } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fstruct_2eproto); + return ::descriptor_table_google_2fprotobuf_2fstruct_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kNullValueFieldNumber = 1, + kNumberValueFieldNumber = 2, + kStringValueFieldNumber = 3, + kBoolValueFieldNumber = 4, + kStructValueFieldNumber = 5, + kListValueFieldNumber = 6, + }; // .google.protobuf.NullValue null_value = 1; private: bool has_null_value() const; public: void clear_null_value(); - static const int kNullValueFieldNumber = 1; - ::google::protobuf::NullValue null_value() const; - void set_null_value(::google::protobuf::NullValue value); + PROTOBUF_NAMESPACE_ID::NullValue null_value() const; + void set_null_value(PROTOBUF_NAMESPACE_ID::NullValue value); // double number_value = 2; private: bool has_number_value() const; public: void clear_number_value(); - static const int kNumberValueFieldNumber = 2; double number_value() const; void set_number_value(double value); @@ -394,65 +468,59 @@ class LIBPROTOBUF_EXPORT Value : public ::google::protobuf::Message /* @@protoc_ bool has_string_value() const; public: void clear_string_value(); - static const int kStringValueFieldNumber = 3; - const ::std::string& string_value() const; - void set_string_value(const ::std::string& value); - #if LANG_CXX11 - void set_string_value(::std::string&& value); - #endif + const std::string& string_value() const; + void set_string_value(const std::string& value); + void set_string_value(std::string&& value); void set_string_value(const char* value); void set_string_value(const char* value, size_t size); - ::std::string* mutable_string_value(); - ::std::string* release_string_value(); - void set_allocated_string_value(::std::string* string_value); + std::string* mutable_string_value(); + std::string* release_string_value(); + void set_allocated_string_value(std::string* string_value); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") - ::std::string* unsafe_arena_release_string_value(); + std::string* unsafe_arena_release_string_value(); GOOGLE_PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for" " string fields are deprecated and will be removed in a" " future release.") void unsafe_arena_set_allocated_string_value( - ::std::string* string_value); + std::string* string_value); // bool bool_value = 4; private: bool has_bool_value() const; public: void clear_bool_value(); - static const int kBoolValueFieldNumber = 4; bool bool_value() const; void set_bool_value(bool value); // .google.protobuf.Struct struct_value = 5; bool has_struct_value() const; void clear_struct_value(); - static const int kStructValueFieldNumber = 5; - const ::google::protobuf::Struct& struct_value() const; - ::google::protobuf::Struct* release_struct_value(); - ::google::protobuf::Struct* mutable_struct_value(); - void set_allocated_struct_value(::google::protobuf::Struct* struct_value); + const PROTOBUF_NAMESPACE_ID::Struct& struct_value() const; + PROTOBUF_NAMESPACE_ID::Struct* release_struct_value(); + PROTOBUF_NAMESPACE_ID::Struct* mutable_struct_value(); + void set_allocated_struct_value(PROTOBUF_NAMESPACE_ID::Struct* struct_value); void unsafe_arena_set_allocated_struct_value( - ::google::protobuf::Struct* struct_value); - ::google::protobuf::Struct* unsafe_arena_release_struct_value(); + PROTOBUF_NAMESPACE_ID::Struct* struct_value); + PROTOBUF_NAMESPACE_ID::Struct* unsafe_arena_release_struct_value(); // .google.protobuf.ListValue list_value = 6; bool has_list_value() const; void clear_list_value(); - static const int kListValueFieldNumber = 6; - const ::google::protobuf::ListValue& list_value() const; - ::google::protobuf::ListValue* release_list_value(); - ::google::protobuf::ListValue* mutable_list_value(); - void set_allocated_list_value(::google::protobuf::ListValue* list_value); + const PROTOBUF_NAMESPACE_ID::ListValue& list_value() const; + PROTOBUF_NAMESPACE_ID::ListValue* release_list_value(); + PROTOBUF_NAMESPACE_ID::ListValue* mutable_list_value(); + void set_allocated_list_value(PROTOBUF_NAMESPACE_ID::ListValue* list_value); void unsafe_arena_set_allocated_list_value( - ::google::protobuf::ListValue* list_value); - ::google::protobuf::ListValue* unsafe_arena_release_list_value(); + PROTOBUF_NAMESPACE_ID::ListValue* list_value); + PROTOBUF_NAMESPACE_ID::ListValue* unsafe_arena_release_list_value(); void clear_kind(); KindCase kind_case() const; // @@protoc_insertion_point(class_scope:google.protobuf.Value) private: - class HasBitSetters; + class _Internal; void set_has_null_value(); void set_has_number_value(); void set_has_string_value(); @@ -463,43 +531,42 @@ class LIBPROTOBUF_EXPORT Value : public ::google::protobuf::Message /* @@protoc_ inline bool has_kind() const; inline void clear_has_kind(); - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; union KindUnion { KindUnion() {} int null_value_; double number_value_; - ::google::protobuf::internal::ArenaStringPtr string_value_; + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr string_value_; bool bool_value_; - ::google::protobuf::Struct* struct_value_; - ::google::protobuf::ListValue* list_value_; + PROTOBUF_NAMESPACE_ID::Struct* struct_value_; + PROTOBUF_NAMESPACE_ID::ListValue* list_value_; } kind_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; - ::google::protobuf::uint32 _oneof_case_[1]; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::uint32 _oneof_case_[1]; friend struct ::TableStruct_google_2fprotobuf_2fstruct_2eproto; }; // ------------------------------------------------------------------- -class LIBPROTOBUF_EXPORT ListValue : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.ListValue) */ { +class PROTOBUF_EXPORT ListValue : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.ListValue) */ { public: ListValue(); virtual ~ListValue(); ListValue(const ListValue& from); - - inline ListValue& operator=(const ListValue& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 ListValue(ListValue&& from) noexcept : ListValue() { *this = ::std::move(from); } + inline ListValue& operator=(const ListValue& from) { + CopyFrom(from); + return *this; + } inline ListValue& operator=(ListValue&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -508,15 +575,21 @@ class LIBPROTOBUF_EXPORT ListValue : public ::google::protobuf::Message /* @@pro } return *this; } - #endif - inline ::google::protobuf::Arena* GetArena() const final { + + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const ListValue& default_instance(); @@ -528,54 +601,66 @@ class LIBPROTOBUF_EXPORT ListValue : public ::google::protobuf::Message /* @@pro static constexpr int kIndexInFileMessages = 3; - void UnsafeArenaSwap(ListValue* other); - void Swap(ListValue* other); friend void swap(ListValue& a, ListValue& b) { a.Swap(&b); } + inline void Swap(ListValue* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(ListValue* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline ListValue* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - ListValue* New(::google::protobuf::Arena* arena) const final { + ListValue* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const ListValue& from); void MergeFrom(const ListValue& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(ListValue* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.ListValue"; + } protected: - explicit ListValue(::google::protobuf::Arena* arena); + explicit ListValue(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -583,34 +668,43 @@ class LIBPROTOBUF_EXPORT ListValue : public ::google::protobuf::Message /* @@pro } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2fstruct_2eproto); + return ::descriptor_table_google_2fprotobuf_2fstruct_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kValuesFieldNumber = 1, + }; // repeated .google.protobuf.Value values = 1; int values_size() const; void clear_values(); - static const int kValuesFieldNumber = 1; - ::google::protobuf::Value* mutable_values(int index); - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Value >* + PROTOBUF_NAMESPACE_ID::Value* mutable_values(int index); + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Value >* mutable_values(); - const ::google::protobuf::Value& values(int index) const; - ::google::protobuf::Value* add_values(); - const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Value >& + const PROTOBUF_NAMESPACE_ID::Value& values(int index) const; + PROTOBUF_NAMESPACE_ID::Value* add_values(); + const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Value >& values() const; // @@protoc_insertion_point(class_scope:google.protobuf.ListValue) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::RepeatedPtrField< ::google::protobuf::Value > values_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Value > values_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2fstruct_2eproto; }; // =================================================================== @@ -633,12 +727,12 @@ inline int Struct::fields_size() const { inline void Struct::clear_fields() { fields_.Clear(); } -inline const ::google::protobuf::Map< ::std::string, ::google::protobuf::Value >& +inline const ::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >& Struct::fields() const { // @@protoc_insertion_point(field_map:google.protobuf.Struct.fields) return fields_.GetMap(); } -inline ::google::protobuf::Map< ::std::string, ::google::protobuf::Value >* +inline ::PROTOBUF_NAMESPACE_ID::Map< std::string, PROTOBUF_NAMESPACE_ID::Value >* Struct::mutable_fields() { // @@protoc_insertion_point(field_mutable_map:google.protobuf.Struct.fields) return fields_.MutableMap(); @@ -661,14 +755,14 @@ inline void Value::clear_null_value() { clear_has_kind(); } } -inline ::google::protobuf::NullValue Value::null_value() const { +inline PROTOBUF_NAMESPACE_ID::NullValue Value::null_value() const { // @@protoc_insertion_point(field_get:google.protobuf.Value.null_value) if (has_null_value()) { - return static_cast< ::google::protobuf::NullValue >(kind_.null_value_); + return static_cast< PROTOBUF_NAMESPACE_ID::NullValue >(kind_.null_value_); } - return static_cast< ::google::protobuf::NullValue >(0); + return static_cast< PROTOBUF_NAMESPACE_ID::NullValue >(0); } -inline void Value::set_null_value(::google::protobuf::NullValue value) { +inline void Value::set_null_value(PROTOBUF_NAMESPACE_ID::NullValue value) { if (!has_null_value()) { clear_kind(); set_has_null_value(); @@ -715,49 +809,47 @@ inline void Value::set_has_string_value() { } inline void Value::clear_string_value() { if (has_string_value()) { - kind_.string_value_.Destroy(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + kind_.string_value_.Destroy(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); clear_has_kind(); } } -inline const ::std::string& Value::string_value() const { +inline const std::string& Value::string_value() const { // @@protoc_insertion_point(field_get:google.protobuf.Value.string_value) if (has_string_value()) { return kind_.string_value_.Get(); } - return *&::google::protobuf::internal::GetEmptyStringAlreadyInited(); + return *&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(); } -inline void Value::set_string_value(const ::std::string& value) { +inline void Value::set_string_value(const std::string& value) { if (!has_string_value()) { clear_kind(); set_has_string_value(); - kind_.string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + kind_.string_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } - kind_.string_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, + kind_.string_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual()); // @@protoc_insertion_point(field_set:google.protobuf.Value.string_value) } -#if LANG_CXX11 -inline void Value::set_string_value(::std::string&& value) { +inline void Value::set_string_value(std::string&& value) { // @@protoc_insertion_point(field_set:google.protobuf.Value.string_value) if (!has_string_value()) { clear_kind(); set_has_string_value(); - kind_.string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + kind_.string_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } kind_.string_value_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_rvalue:google.protobuf.Value.string_value) } -#endif inline void Value::set_string_value(const char* value) { - GOOGLE_DCHECK(value != NULL); + GOOGLE_DCHECK(value != nullptr); if (!has_string_value()) { clear_kind(); set_has_string_value(); - kind_.string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + kind_.string_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } - kind_.string_value_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + kind_.string_value_.Set(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_char:google.protobuf.Value.string_value) } @@ -766,64 +858,64 @@ inline void Value::set_string_value(const char* value, if (!has_string_value()) { clear_kind(); set_has_string_value(); - kind_.string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + kind_.string_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } kind_.string_value_.Set( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string( + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string( reinterpret_cast(value), size), GetArenaNoVirtual()); // @@protoc_insertion_point(field_set_pointer:google.protobuf.Value.string_value) } -inline ::std::string* Value::mutable_string_value() { +inline std::string* Value::mutable_string_value() { if (!has_string_value()) { clear_kind(); set_has_string_value(); - kind_.string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + kind_.string_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } - return kind_.string_value_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return kind_.string_value_.Mutable(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); // @@protoc_insertion_point(field_mutable:google.protobuf.Value.string_value) } -inline ::std::string* Value::release_string_value() { +inline std::string* Value::release_string_value() { // @@protoc_insertion_point(field_release:google.protobuf.Value.string_value) if (has_string_value()) { clear_has_kind(); - return kind_.string_value_.Release(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), + return kind_.string_value_.Release(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } else { - return NULL; + return nullptr; } } -inline void Value::set_allocated_string_value(::std::string* string_value) { +inline void Value::set_allocated_string_value(std::string* string_value) { if (has_kind()) { clear_kind(); } - if (string_value != NULL) { + if (string_value != nullptr) { set_has_string_value(); kind_.string_value_.UnsafeSetDefault(string_value); } // @@protoc_insertion_point(field_set_allocated:google.protobuf.Value.string_value) } -inline ::std::string* Value::unsafe_arena_release_string_value() { +inline std::string* Value::unsafe_arena_release_string_value() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.Value.string_value) - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); if (has_string_value()) { clear_has_kind(); return kind_.string_value_.UnsafeArenaRelease( - &::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); + &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual()); } else { - return NULL; + return nullptr; } } -inline void Value::unsafe_arena_set_allocated_string_value(::std::string* string_value) { - GOOGLE_DCHECK(GetArenaNoVirtual() != NULL); +inline void Value::unsafe_arena_set_allocated_string_value(std::string* string_value) { + GOOGLE_DCHECK(GetArenaNoVirtual() != nullptr); if (!has_string_value()) { - kind_.string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); + kind_.string_value_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); } clear_kind(); if (string_value) { set_has_string_value(); - kind_.string_value_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), string_value, GetArenaNoVirtual()); + kind_.string_value_.UnsafeArenaSetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), string_value, GetArenaNoVirtual()); } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.Value.string_value) } @@ -866,44 +958,44 @@ inline void Value::set_has_struct_value() { } inline void Value::clear_struct_value() { if (has_struct_value()) { - if (GetArenaNoVirtual() == NULL) { + if (GetArenaNoVirtual() == nullptr) { delete kind_.struct_value_; } clear_has_kind(); } } -inline ::google::protobuf::Struct* Value::release_struct_value() { +inline PROTOBUF_NAMESPACE_ID::Struct* Value::release_struct_value() { // @@protoc_insertion_point(field_release:google.protobuf.Value.struct_value) if (has_struct_value()) { clear_has_kind(); - ::google::protobuf::Struct* temp = kind_.struct_value_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::Struct* temp = kind_.struct_value_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - kind_.struct_value_ = NULL; + kind_.struct_value_ = nullptr; return temp; } else { - return NULL; + return nullptr; } } -inline const ::google::protobuf::Struct& Value::struct_value() const { +inline const PROTOBUF_NAMESPACE_ID::Struct& Value::struct_value() const { // @@protoc_insertion_point(field_get:google.protobuf.Value.struct_value) return has_struct_value() ? *kind_.struct_value_ - : *reinterpret_cast< ::google::protobuf::Struct*>(&::google::protobuf::_Struct_default_instance_); + : *reinterpret_cast< PROTOBUF_NAMESPACE_ID::Struct*>(&PROTOBUF_NAMESPACE_ID::_Struct_default_instance_); } -inline ::google::protobuf::Struct* Value::unsafe_arena_release_struct_value() { +inline PROTOBUF_NAMESPACE_ID::Struct* Value::unsafe_arena_release_struct_value() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.Value.struct_value) if (has_struct_value()) { clear_has_kind(); - ::google::protobuf::Struct* temp = kind_.struct_value_; - kind_.struct_value_ = NULL; + PROTOBUF_NAMESPACE_ID::Struct* temp = kind_.struct_value_; + kind_.struct_value_ = nullptr; return temp; } else { - return NULL; + return nullptr; } } -inline void Value::unsafe_arena_set_allocated_struct_value(::google::protobuf::Struct* struct_value) { +inline void Value::unsafe_arena_set_allocated_struct_value(PROTOBUF_NAMESPACE_ID::Struct* struct_value) { clear_kind(); if (struct_value) { set_has_struct_value(); @@ -911,11 +1003,11 @@ inline void Value::unsafe_arena_set_allocated_struct_value(::google::protobuf::S } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.Value.struct_value) } -inline ::google::protobuf::Struct* Value::mutable_struct_value() { +inline PROTOBUF_NAMESPACE_ID::Struct* Value::mutable_struct_value() { if (!has_struct_value()) { clear_kind(); set_has_struct_value(); - kind_.struct_value_ = CreateMaybeMessage< ::google::protobuf::Struct >( + kind_.struct_value_ = CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Struct >( GetArenaNoVirtual()); } // @@protoc_insertion_point(field_mutable:google.protobuf.Value.struct_value) @@ -931,44 +1023,44 @@ inline void Value::set_has_list_value() { } inline void Value::clear_list_value() { if (has_list_value()) { - if (GetArenaNoVirtual() == NULL) { + if (GetArenaNoVirtual() == nullptr) { delete kind_.list_value_; } clear_has_kind(); } } -inline ::google::protobuf::ListValue* Value::release_list_value() { +inline PROTOBUF_NAMESPACE_ID::ListValue* Value::release_list_value() { // @@protoc_insertion_point(field_release:google.protobuf.Value.list_value) if (has_list_value()) { clear_has_kind(); - ::google::protobuf::ListValue* temp = kind_.list_value_; - if (GetArenaNoVirtual() != NULL) { - temp = ::google::protobuf::internal::DuplicateIfNonNull(temp); + PROTOBUF_NAMESPACE_ID::ListValue* temp = kind_.list_value_; + if (GetArenaNoVirtual() != nullptr) { + temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); } - kind_.list_value_ = NULL; + kind_.list_value_ = nullptr; return temp; } else { - return NULL; + return nullptr; } } -inline const ::google::protobuf::ListValue& Value::list_value() const { +inline const PROTOBUF_NAMESPACE_ID::ListValue& Value::list_value() const { // @@protoc_insertion_point(field_get:google.protobuf.Value.list_value) return has_list_value() ? *kind_.list_value_ - : *reinterpret_cast< ::google::protobuf::ListValue*>(&::google::protobuf::_ListValue_default_instance_); + : *reinterpret_cast< PROTOBUF_NAMESPACE_ID::ListValue*>(&PROTOBUF_NAMESPACE_ID::_ListValue_default_instance_); } -inline ::google::protobuf::ListValue* Value::unsafe_arena_release_list_value() { +inline PROTOBUF_NAMESPACE_ID::ListValue* Value::unsafe_arena_release_list_value() { // @@protoc_insertion_point(field_unsafe_arena_release:google.protobuf.Value.list_value) if (has_list_value()) { clear_has_kind(); - ::google::protobuf::ListValue* temp = kind_.list_value_; - kind_.list_value_ = NULL; + PROTOBUF_NAMESPACE_ID::ListValue* temp = kind_.list_value_; + kind_.list_value_ = nullptr; return temp; } else { - return NULL; + return nullptr; } } -inline void Value::unsafe_arena_set_allocated_list_value(::google::protobuf::ListValue* list_value) { +inline void Value::unsafe_arena_set_allocated_list_value(PROTOBUF_NAMESPACE_ID::ListValue* list_value) { clear_kind(); if (list_value) { set_has_list_value(); @@ -976,11 +1068,11 @@ inline void Value::unsafe_arena_set_allocated_list_value(::google::protobuf::Lis } // @@protoc_insertion_point(field_unsafe_arena_set_allocated:google.protobuf.Value.list_value) } -inline ::google::protobuf::ListValue* Value::mutable_list_value() { +inline PROTOBUF_NAMESPACE_ID::ListValue* Value::mutable_list_value() { if (!has_list_value()) { clear_kind(); set_has_list_value(); - kind_.list_value_ = CreateMaybeMessage< ::google::protobuf::ListValue >( + kind_.list_value_ = CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::ListValue >( GetArenaNoVirtual()); } // @@protoc_insertion_point(field_mutable:google.protobuf.Value.list_value) @@ -1007,24 +1099,24 @@ inline int ListValue::values_size() const { inline void ListValue::clear_values() { values_.Clear(); } -inline ::google::protobuf::Value* ListValue::mutable_values(int index) { +inline PROTOBUF_NAMESPACE_ID::Value* ListValue::mutable_values(int index) { // @@protoc_insertion_point(field_mutable:google.protobuf.ListValue.values) return values_.Mutable(index); } -inline ::google::protobuf::RepeatedPtrField< ::google::protobuf::Value >* +inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Value >* ListValue::mutable_values() { // @@protoc_insertion_point(field_mutable_list:google.protobuf.ListValue.values) return &values_; } -inline const ::google::protobuf::Value& ListValue::values(int index) const { +inline const PROTOBUF_NAMESPACE_ID::Value& ListValue::values(int index) const { // @@protoc_insertion_point(field_get:google.protobuf.ListValue.values) return values_.Get(index); } -inline ::google::protobuf::Value* ListValue::add_values() { +inline PROTOBUF_NAMESPACE_ID::Value* ListValue::add_values() { // @@protoc_insertion_point(field_add:google.protobuf.ListValue.values) return values_.Add(); } -inline const ::google::protobuf::RepeatedPtrField< ::google::protobuf::Value >& +inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< PROTOBUF_NAMESPACE_ID::Value >& ListValue::values() const { // @@protoc_insertion_point(field_list:google.protobuf.ListValue.values) return values_; @@ -1042,22 +1134,19 @@ ListValue::values() const { // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_OPEN -template <> struct is_proto_enum< ::google::protobuf::NullValue> : ::std::true_type {}; +template <> struct is_proto_enum< PROTOBUF_NAMESPACE_ID::NullValue> : ::std::true_type {}; template <> -inline const EnumDescriptor* GetEnumDescriptor< ::google::protobuf::NullValue>() { - return ::google::protobuf::NullValue_descriptor(); +inline const EnumDescriptor* GetEnumDescriptor< PROTOBUF_NAMESPACE_ID::NullValue>() { + return PROTOBUF_NAMESPACE_ID::NullValue_descriptor(); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2fstruct_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fstruct_2eproto diff --git a/src/google/protobuf/struct.proto b/src/google/protobuf/struct.proto index 7d7808e7fb..ed990e31d9 100644 --- a/src/google/protobuf/struct.proto +++ b/src/google/protobuf/struct.proto @@ -40,7 +40,6 @@ option java_outer_classname = "StructProto"; option java_multiple_files = true; option objc_class_prefix = "GPB"; - // `Struct` represents a structured data value, consisting of fields // which map to dynamically typed values. In some languages, `Struct` // might be supported by a native representation. For example, in diff --git a/src/google/protobuf/stubs/bytestream.cc b/src/google/protobuf/stubs/bytestream.cc index 8df2566abd..a0f298edb9 100644 --- a/src/google/protobuf/stubs/bytestream.cc +++ b/src/google/protobuf/stubs/bytestream.cc @@ -33,6 +33,8 @@ #include #include +#include + namespace google { namespace protobuf { namespace strings { diff --git a/src/google/protobuf/stubs/bytestream.h b/src/google/protobuf/stubs/bytestream.h index cfa19f2965..29a9fbe7d6 100644 --- a/src/google/protobuf/stubs/bytestream.h +++ b/src/google/protobuf/stubs/bytestream.h @@ -56,6 +56,8 @@ #include #include +#include + class CordByteSink; namespace google { @@ -73,7 +75,7 @@ namespace strings { // sink->Append(my_data.data(), my_data.size()); // sink->Flush(); // -class LIBPROTOBUF_EXPORT ByteSink { +class PROTOBUF_EXPORT ByteSink { public: ByteSink() {} virtual ~ByteSink() {} @@ -102,7 +104,7 @@ class LIBPROTOBUF_EXPORT ByteSink { // source->Skip(data.length()); // } // -class LIBPROTOBUF_EXPORT ByteSource { +class PROTOBUF_EXPORT ByteSource { public: ByteSource() {} virtual ~ByteSource() {} @@ -158,7 +160,7 @@ class LIBPROTOBUF_EXPORT ByteSource { // sink.Append("hi", 2); // OK // sink.Append(data, 100); // WOOPS! Overflows buf[10]. // -class LIBPROTOBUF_EXPORT UncheckedArrayByteSink : public ByteSink { +class PROTOBUF_EXPORT UncheckedArrayByteSink : public ByteSink { public: explicit UncheckedArrayByteSink(char* dest) : dest_(dest) {} virtual void Append(const char* data, size_t n) override; @@ -186,7 +188,7 @@ class LIBPROTOBUF_EXPORT UncheckedArrayByteSink : public ByteSink { // sink.Append("hi", 2); // OK // sink.Append(data, 100); // Will only write 8 more bytes // -class LIBPROTOBUF_EXPORT CheckedArrayByteSink : public ByteSink { +class PROTOBUF_EXPORT CheckedArrayByteSink : public ByteSink { public: CheckedArrayByteSink(char* outbuf, size_t capacity); virtual void Append(const char* bytes, size_t n) override; @@ -222,7 +224,7 @@ class LIBPROTOBUF_EXPORT CheckedArrayByteSink : public ByteSink { // const char* buf = sink.GetBuffer(); // Ownership transferred // delete[] buf; // -class LIBPROTOBUF_EXPORT GrowingArrayByteSink : public strings::ByteSink { +class PROTOBUF_EXPORT GrowingArrayByteSink : public strings::ByteSink { public: explicit GrowingArrayByteSink(size_t estimated_size); virtual ~GrowingArrayByteSink(); @@ -252,7 +254,7 @@ class LIBPROTOBUF_EXPORT GrowingArrayByteSink : public strings::ByteSink { // sink.Append("World", 5); // assert(dest == "Hello World"); // -class LIBPROTOBUF_EXPORT StringByteSink : public ByteSink { +class PROTOBUF_EXPORT StringByteSink : public ByteSink { public: explicit StringByteSink(string* dest) : dest_(dest) {} virtual void Append(const char* data, size_t n) override; @@ -269,7 +271,7 @@ class LIBPROTOBUF_EXPORT StringByteSink : public ByteSink { // NullByteSink sink; // sink.Append(data, data.size()); // All data ignored. // -class LIBPROTOBUF_EXPORT NullByteSink : public ByteSink { +class PROTOBUF_EXPORT NullByteSink : public ByteSink { public: NullByteSink() {} virtual void Append(const char *data, size_t n) override {} @@ -291,7 +293,7 @@ class LIBPROTOBUF_EXPORT NullByteSink : public ByteSink { // assert(source.Available() == 5); // assert(source.Peek() == "Hello"); // -class LIBPROTOBUF_EXPORT ArrayByteSource : public ByteSource { +class PROTOBUF_EXPORT ArrayByteSource : public ByteSource { public: explicit ArrayByteSource(StringPiece s) : input_(s) {} @@ -322,7 +324,7 @@ class LIBPROTOBUF_EXPORT ArrayByteSource : public ByteSource { // assert(limit.Available() == 5); // assert(limit.Peek() == "Hello"); // -class LIBPROTOBUF_EXPORT LimitByteSource : public ByteSource { +class PROTOBUF_EXPORT LimitByteSource : public ByteSource { public: // Returns at most "limit" bytes from "source". LimitByteSource(ByteSource* source, size_t limit); @@ -344,4 +346,6 @@ class LIBPROTOBUF_EXPORT LimitByteSource : public ByteSource { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_ diff --git a/src/google/protobuf/stubs/callback.h b/src/google/protobuf/stubs/callback.h index dae972f2fb..ab4a3aae5e 100644 --- a/src/google/protobuf/stubs/callback.h +++ b/src/google/protobuf/stubs/callback.h @@ -5,6 +5,8 @@ #include +#include + // =================================================================== // emulates google3/base/callback.h @@ -68,7 +70,7 @@ namespace protobuf { // string my_str; // NewCallback(&Foo, my_str); // WON'T WORK: Can't use referecnes. // However, correctly-typed pointers will work just fine. -class LIBPROTOBUF_EXPORT Closure { +class PROTOBUF_EXPORT Closure { public: Closure() {} virtual ~Closure(); @@ -91,8 +93,8 @@ class ResultCallback { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback); }; -template -class LIBPROTOBUF_EXPORT ResultCallback1 { +template +class PROTOBUF_EXPORT ResultCallback1 { public: ResultCallback1() {} virtual ~ResultCallback1() {} @@ -103,8 +105,8 @@ class LIBPROTOBUF_EXPORT ResultCallback1 { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback1); }; -template -class LIBPROTOBUF_EXPORT ResultCallback2 { +template +class PROTOBUF_EXPORT ResultCallback2 { public: ResultCallback2() {} virtual ~ResultCallback2() {} @@ -117,7 +119,7 @@ class LIBPROTOBUF_EXPORT ResultCallback2 { namespace internal { -class LIBPROTOBUF_EXPORT FunctionClosure0 : public Closure { +class PROTOBUF_EXPORT FunctionClosure0 : public Closure { public: typedef void (*FunctionType)(); @@ -371,12 +373,12 @@ class MethodResultCallback_0_0 : public ResultCallback { }; template -class MethodResultCallback_5_2 : public ResultCallback2 { + typename P4, typename P5, typename P6, typename A1, typename A2> +class MethodResultCallback_6_2 : public ResultCallback2 { public: - typedef R (T::*MethodType)(P1, P2, P3, P4, P5, A1, A2); - MethodResultCallback_5_2(T* object, MethodType method, bool self_deleting, - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) + 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), @@ -384,12 +386,13 @@ class MethodResultCallback_5_2 : public ResultCallback2 { p2_(p2), p3_(p3), p4_(p4), - p5_(p5) {} - ~MethodResultCallback_5_2() {} + 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_, a1, a2); + R result = (object_->*method_)(p1_, p2_, p3_, p4_, p5_, p6_, a1, a2); if (needs_delete) delete this; return result; } @@ -403,6 +406,7 @@ class MethodResultCallback_5_2 : public ResultCallback2 { 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 @@ -551,27 +555,29 @@ inline ResultCallback* NewPermanentCallback( return new internal::MethodResultCallback_0_0(object, function, false); } -// See MethodResultCallback_5_2 +// See MethodResultCallback_6_2 template + typename P4, typename P5, typename P6, typename A1, typename A2> inline ResultCallback2* NewPermanentCallback( - T* object, R (T::*function)(P1, P2, P3, P4, P5, A1, A2), + 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) { - return new internal::MethodResultCallback_5_2(object, function, false, p1, - p2, p3, p4, p5); + 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 LIBPROTOBUF_EXPORT DoNothing(); - +void PROTOBUF_EXPORT DoNothing(); } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_CALLBACK_H_ diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc index 4b65eb8994..ff9a32d503 100644 --- a/src/google/protobuf/stubs/common.cc +++ b/src/google/protobuf/stubs/common.cc @@ -31,11 +31,8 @@ // Author: kenton@google.com (Kenton Varda) #include -#include -#include -#include -#include -#include + +#include #include #include #include @@ -54,6 +51,17 @@ #include #endif +#include +#include +#include +#include +#include +#include +#include +#include + +#include + namespace google { namespace protobuf { @@ -173,22 +181,7 @@ void NullLogHandler(LogLevel /* level */, const char* /* filename */, } static LogHandler* log_handler_ = &DefaultLogHandler; -static int log_silencer_count_ = 0; - -static Mutex* log_silencer_count_mutex_ = nullptr; -GOOGLE_PROTOBUF_DECLARE_ONCE(log_silencer_count_init_); - -void DeleteLogSilencerCount() { - delete log_silencer_count_mutex_; - log_silencer_count_mutex_ = nullptr; -} -void InitLogSilencerCount() { - log_silencer_count_mutex_ = new Mutex; - OnShutdown(&DeleteLogSilencerCount); -} -void InitLogSilencerCountOnce() { - GoogleOnceInit(&log_silencer_count_init_, &InitLogSilencerCount); -} +static std::atomic log_silencer_count_ = ATOMIC_VAR_INIT(0); LogMessage& LogMessage::operator<<(const string& value) { message_ += value; @@ -205,8 +198,7 @@ LogMessage& LogMessage::operator<<(const StringPiece& value) { return *this; } -LogMessage& LogMessage::operator<<( - const ::google::protobuf::util::Status& status) { +LogMessage& LogMessage::operator<<(const util::Status& status) { message_ += status.ToString(); return *this; } @@ -242,8 +234,8 @@ 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 , "%" GOOGLE_LL_FORMAT "d") -DECLARE_STREAM_OPERATOR(unsigned long long, "%" GOOGLE_LL_FORMAT "u") +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) @@ -254,8 +246,6 @@ void LogMessage::Finish() { bool suppress = false; if (level_ != LOGLEVEL_FATAL) { - InitLogSilencerCountOnce(); - MutexLock lock(log_silencer_count_mutex_); suppress = log_silencer_count_ > 0; } @@ -292,14 +282,10 @@ LogHandler* SetLogHandler(LogHandler* new_func) { } LogSilencer::LogSilencer() { - internal::InitLogSilencerCountOnce(); - MutexLock lock(internal::log_silencer_count_mutex_); ++internal::log_silencer_count_; }; LogSilencer::~LogSilencer() { - internal::InitLogSilencerCountOnce(); - MutexLock lock(internal::log_silencer_count_mutex_); --internal::log_silencer_count_; }; @@ -335,7 +321,6 @@ uint32 ghtonl(uint32 x) { namespace internal { -typedef void OnShutdownFunc(); struct ShutdownData { ~ShutdownData() { std::reverse(functions.begin(), functions.end()); @@ -352,7 +337,8 @@ struct ShutdownData { }; static void RunZeroArgFunc(const void* arg) { - reinterpret_cast(const_cast(arg))(); + void (*func)() = reinterpret_cast(const_cast(arg)); + func(); } void OnShutdown(void (*func)()) { diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h index 585e0128bf..b9599010d5 100644 --- a/src/google/protobuf/stubs/common.h +++ b/src/google/protobuf/stubs/common.h @@ -47,11 +47,6 @@ #include #include -// TODO(liujisi): Remove the following includes after the include clean-up. -#include -#include -#include - #ifndef PROTOBUF_USE_EXCEPTIONS #if defined(_MSC_VER) && defined(_CPPUNWIND) #define PROTOBUF_USE_EXCEPTIONS 1 @@ -73,22 +68,7 @@ #include #endif -#if defined(_WIN32) && defined(GetMessage) -// Allow GetMessage to be used as a valid method name in protobuf classes. -// windows.h defines GetMessage() as a macro. Let's re-define it as an inline -// function. The inline function should be equivalent for C++ users. -inline BOOL GetMessage_Win32( - LPMSG lpMsg, HWND hWnd, - UINT wMsgFilterMin, UINT wMsgFilterMax) { - return GetMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax); -} -#undef GetMessage -inline BOOL GetMessage( - LPMSG lpMsg, HWND hWnd, - UINT wMsgFilterMin, UINT wMsgFilterMax) { - return GetMessage_Win32(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax); -} -#endif +#include namespace std {} @@ -101,35 +81,31 @@ namespace internal { // The current version, represented as a single integer to make comparison // easier: major * 10^6 + minor * 10^3 + micro -#define GOOGLE_PROTOBUF_VERSION 3006001 +#define GOOGLE_PROTOBUF_VERSION 3009000 // A suffix string for alpha, beta or rc releases. Empty for stable releases. #define GOOGLE_PROTOBUF_VERSION_SUFFIX "" -// The minimum library version which works with the current version of the -// headers. -#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3006001 - // 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 = 3006001; +static const int kMinHeaderVersionForLibrary = 3009000; // The minimum protoc version which works with the current version of the // headers. -#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3006001 +#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3009000 // The minimum header version which works with the current version of // protoc. This constant should only be used in VerifyVersion(). -static const int kMinHeaderVersionForProtoc = 3006001; +static const int kMinHeaderVersionForProtoc = 3009000; // Verifies that the headers and libraries are compatible. Use the macro // below to call this. -void LIBPROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion, - const char* filename); +void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion, + const char* filename); // Converts a numeric version number to a string. -std::string LIBPROTOBUF_EXPORT VersionString(int version); +std::string PROTOBUF_EXPORT VersionString(int version); } // namespace internal @@ -151,14 +127,14 @@ namespace internal { // Checks if the buffer contains structurally-valid UTF-8. Implemented in // structurally_valid.cc. -LIBPROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len); +PROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len); inline bool IsStructurallyValidUTF8(const std::string& str) { return IsStructurallyValidUTF8(str.data(), static_cast(str.length())); } // Returns initial number of bytes of structually valid UTF-8. -LIBPROTOBUF_EXPORT int UTF8SpnStructurallyValid(const StringPiece& str); +PROTOBUF_EXPORT int UTF8SpnStructurallyValid(const StringPiece& str); // Coerce UTF-8 byte string in src_str to be // a structurally-valid equal-length string by selectively @@ -172,8 +148,9 @@ LIBPROTOBUF_EXPORT int UTF8SpnStructurallyValid(const StringPiece& str); // // Optimized for: all structurally valid and no byte copying is done. // -LIBPROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid( - const StringPiece& str, char* dst, char replace_char); +PROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid(const StringPiece& str, + char* dst, + char replace_char); } // namespace internal @@ -195,14 +172,14 @@ LIBPROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid( // 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. -LIBPROTOBUF_EXPORT void ShutdownProtobufLibrary(); +PROTOBUF_EXPORT void ShutdownProtobufLibrary(); namespace internal { // Register a function to be called when ShutdownProtocolBuffers() is called. -LIBPROTOBUF_EXPORT void OnShutdown(void (*func)()); +PROTOBUF_EXPORT void OnShutdown(void (*func)()); // Run an arbitrary function on an arg -LIBPROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg); +PROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg); template T* OnShutdownDelete(T* p) { @@ -239,4 +216,6 @@ using std::string; } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_COMMON_H__ diff --git a/src/google/protobuf/stubs/common_unittest.cc b/src/google/protobuf/stubs/common_unittest.cc index 3ce1c49acf..5852458922 100644 --- a/src/google/protobuf/stubs/common_unittest.cc +++ b/src/google/protobuf/stubs/common_unittest.cc @@ -31,8 +31,10 @@ // Author: kenton@google.com (Kenton Varda) #include +#include #include #include +#include #include #include diff --git a/src/google/protobuf/stubs/fastmem.h b/src/google/protobuf/stubs/fastmem.h index 1f1f6ed3df..76c8a3aea6 100644 --- a/src/google/protobuf/stubs/fastmem.h +++ b/src/google/protobuf/stubs/fastmem.h @@ -51,6 +51,8 @@ #include +#include + namespace google { namespace protobuf { namespace internal { @@ -64,7 +66,7 @@ namespace internal { // if it makes sense to do so.:w inline bool memeq(const char* a, const char* b, size_t n) { size_t n_rounded_down = n & ~static_cast(7); - if (GOOGLE_PREDICT_FALSE(n_rounded_down == 0)) { // n <= 7 + if (PROTOBUF_PREDICT_FALSE(n_rounded_down == 0)) { // n <= 7 return memcmp(a, b, n) == 0; } // n >= 8 @@ -150,4 +152,6 @@ inline void memcpy_inlined(char *dst, const char *src, size_t size) { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_FASTMEM_H_ diff --git a/src/google/protobuf/stubs/int128.cc b/src/google/protobuf/stubs/int128.cc index 7b993e8fd7..2119e65505 100644 --- a/src/google/protobuf/stubs/int128.cc +++ b/src/google/protobuf/stubs/int128.cc @@ -34,12 +34,16 @@ #include // NOLINT(readability/streams) #include +#include + +#include + namespace google { namespace protobuf { const uint128_pod kuint128max = { - static_cast(GOOGLE_LONGLONG(0xFFFFFFFFFFFFFFFF)), - static_cast(GOOGLE_LONGLONG(0xFFFFFFFFFFFFFFFF)) + 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) @@ -63,7 +67,7 @@ static inline int Fls64(uint64 n) { STEP(uint32, n32, pos, 0x10); STEP(uint32, n32, pos, 0x08); STEP(uint32, n32, pos, 0x04); - return pos + ((GOOGLE_ULONGLONG(0x3333333322221100) >> (n32 << 2)) & 0x3); + return pos + ((PROTOBUF_ULONGLONG(0x3333333322221100) >> (n32 << 2)) & 0x3); } #undef STEP @@ -129,15 +133,18 @@ std::ostream& operator<<(std::ostream& o, const uint128& b) { std::streamsize div_base_log; switch (flags & std::ios::basefield) { case std::ios::hex: - div = static_cast(GOOGLE_ULONGLONG(0x1000000000000000)); // 16^15 + div = + static_cast(PROTOBUF_ULONGLONG(0x1000000000000000)); // 16^15 div_base_log = 15; break; case std::ios::oct: - div = static_cast(GOOGLE_ULONGLONG(01000000000000000000000)); // 8^21 + div = static_cast( + PROTOBUF_ULONGLONG(01000000000000000000000)); // 8^21 div_base_log = 21; break; default: // std::ios::dec - div = static_cast(GOOGLE_ULONGLONG(10000000000000000000)); // 10^19 + div = static_cast( + PROTOBUF_ULONGLONG(10000000000000000000)); // 10^19 div_base_log = 19; break; } diff --git a/src/google/protobuf/stubs/int128.h b/src/google/protobuf/stubs/int128.h index 1499bb76d5..dc70d96eb3 100644 --- a/src/google/protobuf/stubs/int128.h +++ b/src/google/protobuf/stubs/int128.h @@ -34,6 +34,8 @@ #include +#include + namespace google { namespace protobuf { @@ -48,7 +50,7 @@ struct uint128_pod; #endif // An unsigned 128-bit integer type. Thread-compatible. -class LIBPROTOBUF_EXPORT uint128 { +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); @@ -84,8 +86,8 @@ class LIBPROTOBUF_EXPORT uint128 { friend uint64 Uint128High64(const uint128& v); // We add "std::" to avoid including all of port.h. - LIBPROTOBUF_EXPORT friend std::ostream& operator<<(std::ostream& o, - const uint128& b); + PROTOBUF_EXPORT friend std::ostream& operator<<(std::ostream& o, + const uint128& b); private: static void DivModImpl(uint128 dividend, uint128 divisor, @@ -116,11 +118,11 @@ struct uint128_pod { uint64 lo; }; -LIBPROTOBUF_EXPORT extern const uint128_pod kuint128max; +PROTOBUF_EXPORT extern const uint128_pod kuint128max; // allow uint128 to be logged -LIBPROTOBUF_EXPORT extern std::ostream& operator<<(std::ostream& o, - const uint128& b); +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 @@ -380,4 +382,6 @@ inline uint128& uint128::operator--() { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_INT128_H_ diff --git a/src/google/protobuf/stubs/int128_unittest.cc b/src/google/protobuf/stubs/int128_unittest.cc index 1ec899ad7b..9a8125d488 100644 --- a/src/google/protobuf/stubs/int128_unittest.cc +++ b/src/google/protobuf/stubs/int128_unittest.cc @@ -37,6 +37,8 @@ #include #include +#include + namespace google { namespace protobuf { @@ -291,24 +293,26 @@ TEST(Int128, Multiply) { } // Verified with dc. - a = uint128(GOOGLE_ULONGLONG(0xffffeeeeddddcccc), - GOOGLE_ULONGLONG(0xbbbbaaaa99998888)); - b = uint128(GOOGLE_ULONGLONG(0x7777666655554444), - GOOGLE_ULONGLONG(0x3333222211110000)); + a = uint128(PROTOBUF_ULONGLONG(0xffffeeeeddddcccc), + PROTOBUF_ULONGLONG(0xbbbbaaaa99998888)); + b = uint128(PROTOBUF_ULONGLONG(0x7777666655554444), + PROTOBUF_ULONGLONG(0x3333222211110000)); c = a * b; - EXPECT_EQ(uint128(GOOGLE_ULONGLONG(0x530EDA741C71D4C3), - GOOGLE_ULONGLONG(0xBF25975319080000)), c); + EXPECT_EQ(uint128(PROTOBUF_ULONGLONG(0x530EDA741C71D4C3), + PROTOBUF_ULONGLONG(0xBF25975319080000)), + c); EXPECT_EQ(0, c - b * a); - EXPECT_EQ(a*a - b*b, (a+b) * (a-b)); + EXPECT_EQ(a * a - b * b, (a + b) * (a - b)); // Verified with dc. - a = uint128(GOOGLE_ULONGLONG(0x0123456789abcdef), - GOOGLE_ULONGLONG(0xfedcba9876543210)); - b = uint128(GOOGLE_ULONGLONG(0x02468ace13579bdf), - GOOGLE_ULONGLONG(0xfdb97531eca86420)); + a = uint128(PROTOBUF_ULONGLONG(0x0123456789abcdef), + PROTOBUF_ULONGLONG(0xfedcba9876543210)); + b = uint128(PROTOBUF_ULONGLONG(0x02468ace13579bdf), + PROTOBUF_ULONGLONG(0xfdb97531eca86420)); c = a * b; - EXPECT_EQ(uint128(GOOGLE_ULONGLONG(0x97a87f4f261ba3f2), - GOOGLE_ULONGLONG(0x342d0bbf48948200)), c); + EXPECT_EQ(uint128(PROTOBUF_ULONGLONG(0x97a87f4f261ba3f2), + PROTOBUF_ULONGLONG(0x342d0bbf48948200)), + c); EXPECT_EQ(0, c - b * a); EXPECT_EQ(a*a - b*b, (a+b) * (a-b)); } @@ -355,10 +359,10 @@ TEST(Int128, DivideAndMod) { EXPECT_EQ(0, q); EXPECT_EQ(0, r); - a = uint128(GOOGLE_ULONGLONG(0x530eda741c71d4c3), - GOOGLE_ULONGLONG(0xbf25975319080000)); - q = uint128(GOOGLE_ULONGLONG(0x4de2cab081), - GOOGLE_ULONGLONG(0x14c34ab4676e4bab)); + a = uint128(PROTOBUF_ULONGLONG(0x530eda741c71d4c3), + PROTOBUF_ULONGLONG(0xbf25975319080000)); + q = uint128(PROTOBUF_ULONGLONG(0x4de2cab081), + PROTOBUF_ULONGLONG(0x14c34ab4676e4bab)); b = uint128(0x1110001); r = uint128(0x3eb455); ASSERT_EQ(a, q * b + r); // Sanity-check. @@ -396,8 +400,8 @@ TEST(Int128, DivideAndMod) { // Try a large remainder. b = a / 2 + 1; - uint128 expected_r(GOOGLE_ULONGLONG(0x29876d3a0e38ea61), - GOOGLE_ULONGLONG(0xdf92cba98c83ffff)); + uint128 expected_r(PROTOBUF_ULONGLONG(0x29876d3a0e38ea61), + PROTOBUF_ULONGLONG(0xdf92cba98c83ffff)); // Sanity checks. ASSERT_EQ(a / 2 - 1, expected_r); ASSERT_EQ(a, b + expected_r); @@ -455,50 +459,50 @@ TEST(Int128, OStream) { char fill; const char* rep; } cases[] = { - // zero with different bases - {uint128(0), std::ios::dec, 0, '_', "0"}, - {uint128(0), std::ios::oct, 0, '_', "0"}, - {uint128(0), std::ios::hex, 0, '_', "0"}, - // crossover between lo_ and hi_ - {uint128(0, -1), std::ios::dec, 0, '_', "18446744073709551615"}, - {uint128(0, -1), std::ios::oct, 0, '_', "1777777777777777777777"}, - {uint128(0, -1), std::ios::hex, 0, '_', "ffffffffffffffff"}, - {uint128(1, 0), std::ios::dec, 0, '_', "18446744073709551616"}, - {uint128(1, 0), std::ios::oct, 0, '_', "2000000000000000000000"}, - {uint128(1, 0), std::ios::hex, 0, '_', "10000000000000000"}, - // just the top bit - {uint128(GOOGLE_ULONGLONG(0x8000000000000000), 0), std::ios::dec, 0, '_', - "170141183460469231731687303715884105728"}, - {uint128(GOOGLE_ULONGLONG(0x8000000000000000), 0), std::ios::oct, 0, '_', - "2000000000000000000000000000000000000000000"}, - {uint128(GOOGLE_ULONGLONG(0x8000000000000000), 0), std::ios::hex, 0, '_', - "80000000000000000000000000000000"}, - // maximum uint128 value - {uint128(-1, -1), std::ios::dec, 0, '_', - "340282366920938463463374607431768211455"}, - {uint128(-1, -1), std::ios::oct, 0, '_', - "3777777777777777777777777777777777777777777"}, - {uint128(-1, -1), std::ios::hex, 0, '_', - "ffffffffffffffffffffffffffffffff"}, - // uppercase - {uint128(-1, -1), std::ios::hex | std::ios::uppercase, 0, '_', - "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"}, - // showbase - {uint128(1), std::ios::dec | std::ios::showbase, 0, '_', "1"}, - {uint128(1), std::ios::oct | std::ios::showbase, 0, '_', "01"}, - {uint128(1), std::ios::hex | std::ios::showbase, 0, '_', "0x1"}, - // showbase does nothing on zero - {uint128(0), std::ios::dec | std::ios::showbase, 0, '_', "0"}, - {uint128(0), std::ios::oct | std::ios::showbase, 0, '_', "0"}, - {uint128(0), std::ios::hex | std::ios::showbase, 0, '_', "0"}, - // showpos does nothing on unsigned types - {uint128(1), std::ios::dec | std::ios::showpos, 0, '_', "1"}, - // padding - {uint128(9), std::ios::dec, 6, '_', "_____9"}, - {uint128(12345), std::ios::dec, 6, '_', "_12345"}, - // left adjustment - {uint128(9), std::ios::dec | std::ios::left, 6, '_', "9_____"}, - {uint128(12345), std::ios::dec | std::ios::left, 6, '_', "12345_"}, + // zero with different bases + {uint128(0), std::ios::dec, 0, '_', "0"}, + {uint128(0), std::ios::oct, 0, '_', "0"}, + {uint128(0), std::ios::hex, 0, '_', "0"}, + // crossover between lo_ and hi_ + {uint128(0, -1), std::ios::dec, 0, '_', "18446744073709551615"}, + {uint128(0, -1), std::ios::oct, 0, '_', "1777777777777777777777"}, + {uint128(0, -1), std::ios::hex, 0, '_', "ffffffffffffffff"}, + {uint128(1, 0), std::ios::dec, 0, '_', "18446744073709551616"}, + {uint128(1, 0), std::ios::oct, 0, '_', "2000000000000000000000"}, + {uint128(1, 0), std::ios::hex, 0, '_', "10000000000000000"}, + // just the top bit + {uint128(PROTOBUF_ULONGLONG(0x8000000000000000), 0), std::ios::dec, 0, + '_', "170141183460469231731687303715884105728"}, + {uint128(PROTOBUF_ULONGLONG(0x8000000000000000), 0), std::ios::oct, 0, + '_', "2000000000000000000000000000000000000000000"}, + {uint128(PROTOBUF_ULONGLONG(0x8000000000000000), 0), std::ios::hex, 0, + '_', "80000000000000000000000000000000"}, + // maximum uint128 value + {uint128(-1, -1), std::ios::dec, 0, '_', + "340282366920938463463374607431768211455"}, + {uint128(-1, -1), std::ios::oct, 0, '_', + "3777777777777777777777777777777777777777777"}, + {uint128(-1, -1), std::ios::hex, 0, '_', + "ffffffffffffffffffffffffffffffff"}, + // uppercase + {uint128(-1, -1), std::ios::hex | std::ios::uppercase, 0, '_', + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"}, + // showbase + {uint128(1), std::ios::dec | std::ios::showbase, 0, '_', "1"}, + {uint128(1), std::ios::oct | std::ios::showbase, 0, '_', "01"}, + {uint128(1), std::ios::hex | std::ios::showbase, 0, '_', "0x1"}, + // showbase does nothing on zero + {uint128(0), std::ios::dec | std::ios::showbase, 0, '_', "0"}, + {uint128(0), std::ios::oct | std::ios::showbase, 0, '_', "0"}, + {uint128(0), std::ios::hex | std::ios::showbase, 0, '_', "0"}, + // showpos does nothing on unsigned types + {uint128(1), std::ios::dec | std::ios::showpos, 0, '_', "1"}, + // padding + {uint128(9), std::ios::dec, 6, '_', "_____9"}, + {uint128(12345), std::ios::dec, 6, '_', "_12345"}, + // left adjustment + {uint128(9), std::ios::dec | std::ios::left, 6, '_', "9_____"}, + {uint128(12345), std::ios::dec | std::ios::left, 6, '_', "12345_"}, }; for (size_t i = 0; i < GOOGLE_ARRAYSIZE(cases); ++i) { std::ostringstream os; diff --git a/src/google/protobuf/stubs/logging.h b/src/google/protobuf/stubs/logging.h index f7c6d6a81b..f37048d677 100644 --- a/src/google/protobuf/stubs/logging.h +++ b/src/google/protobuf/stubs/logging.h @@ -34,6 +34,8 @@ #include #include +#include + // =================================================================== // emulates google3/base/logging.h @@ -70,7 +72,7 @@ namespace internal { class LogFinisher; -class LIBPROTOBUF_EXPORT LogMessage { +class PROTOBUF_EXPORT LogMessage { public: LogMessage(LogLevel level, const char* filename, int line); ~LogMessage(); @@ -87,7 +89,7 @@ class LIBPROTOBUF_EXPORT LogMessage { LogMessage& operator<<(double value); LogMessage& operator<<(void* value); LogMessage& operator<<(const StringPiece& value); - LogMessage& operator<<(const ::google::protobuf::util::Status& status); + LogMessage& operator<<(const util::Status& status); LogMessage& operator<<(const uint128& value); private: @@ -102,7 +104,7 @@ class LIBPROTOBUF_EXPORT LogMessage { // Used to make the entire "LOG(BLAH) << etc." expression have a void return // type and print a newline after each message. -class LIBPROTOBUF_EXPORT LogFinisher { +class PROTOBUF_EXPORT LogFinisher { public: void operator=(LogMessage& other); }; @@ -141,10 +143,10 @@ inline bool IsOk(bool status) { return status; } #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(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) @@ -168,8 +170,8 @@ T* CheckNotNull(const char* /* file */, int /* line */, return val; } } // namespace internal -#define GOOGLE_CHECK_NOTNULL(A) \ - ::google::protobuf::internal::CheckNotNull(\ +#define GOOGLE_CHECK_NOTNULL(A) \ + ::google::protobuf::internal::CheckNotNull( \ __FILE__, __LINE__, "'" #A "' must not be nullptr", (A)) #ifdef NDEBUG @@ -216,7 +218,7 @@ typedef void LogHandler(LogLevel level, const char* filename, int line, // have some code that tends to trigger them frequently and you know // the warnings are not important to you), use the LogSilencer class // below. -LIBPROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func); +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 @@ -225,7 +227,7 @@ LIBPROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func); // 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 LIBPROTOBUF_EXPORT LogSilencer { +class PROTOBUF_EXPORT LogSilencer { public: LogSilencer(); ~LogSilencer(); @@ -234,4 +236,6 @@ class LIBPROTOBUF_EXPORT LogSilencer { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_LOGGING_H_ diff --git a/src/google/protobuf/stubs/macros.h b/src/google/protobuf/stubs/macros.h index 0e9a9ec198..c556d02233 100644 --- a/src/google/protobuf/stubs/macros.h +++ b/src/google/protobuf/stubs/macros.h @@ -112,55 +112,7 @@ struct CompileAssert { } // namespace internal -#undef GOOGLE_COMPILE_ASSERT -#if __cplusplus >= 201103L #define GOOGLE_COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) -#else -#define GOOGLE_COMPILE_ASSERT(expr, msg) \ - ::google::protobuf::internal::CompileAssert<(bool(expr))> \ - msg[bool(expr) ? 1 : -1]; \ - (void)msg -// Implementation details of COMPILE_ASSERT: -// -// - COMPILE_ASSERT works by defining an array type that has -1 -// elements (and thus is invalid) when the expression is false. -// -// - The simpler definition -// -// #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] -// -// does not work, as gcc supports variable-length arrays whose sizes -// are determined at run-time (this is gcc's extension and not part -// of the C++ standard). As a result, gcc fails to reject the -// following code with the simple definition: -// -// int foo; -// COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is -// // not a compile-time constant. -// -// - By using the type CompileAssert<(bool(expr))>, we ensures that -// expr is a compile-time constant. (Template arguments must be -// determined at compile-time.) -// -// - The outter parentheses in CompileAssert<(bool(expr))> are necessary -// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written -// -// CompileAssert -// -// instead, these compilers will refuse to compile -// -// COMPILE_ASSERT(5 > 0, some_message); -// -// (They seem to think the ">" in "5 > 0" marks the end of the -// template argument list.) -// -// - The array size is (bool(expr) ? 1 : -1), instead of simply -// -// ((expr) ? 1 : -1). -// -// This is to avoid running into a bug in MS VC 7.1, which -// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. -#endif // __cplusplus >= 201103L } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/stubs/map_util.h b/src/google/protobuf/stubs/map_util.h index 2313e1f511..24e098ad1b 100644 --- a/src/google/protobuf/stubs/map_util.h +++ b/src/google/protobuf/stubs/map_util.h @@ -620,9 +620,8 @@ bool UpdateReturnCopy(Collection* const collection, // 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* const -InsertOrReturnExisting(Collection* const collection, - const typename Collection::value_type& vt) { +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. @@ -633,8 +632,7 @@ InsertOrReturnExisting(Collection* const collection, // Same as above, except for explicit key and data. template -typename Collection::value_type::second_type* const -InsertOrReturnExisting( +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) { diff --git a/src/google/protobuf/stubs/mathlimits.cc b/src/google/protobuf/stubs/mathlimits.cc index 0373b2bb97..7a335bd6c5 100644 --- a/src/google/protobuf/stubs/mathlimits.cc +++ b/src/google/protobuf/stubs/mathlimits.cc @@ -40,42 +40,6 @@ namespace google { namespace protobuf { -// MSVC++ 2005 and older compilers think the header declaration was a -// definition, and erroneously flag these as a duplicate definition. -#if defined(COMPILER_MSVC) || __cpluscplus < 201103L - -#define DEF_COMMON_LIMITS(Type) -#define DEF_UNSIGNED_INT_LIMITS(Type) -#define DEF_SIGNED_INT_LIMITS(Type) -#define DEF_PRECISION_LIMITS(Type) - -#else - -#define DEF_COMMON_LIMITS(Type) \ -const bool MathLimits::kIsSigned; \ -const bool MathLimits::kIsInteger; \ -const int MathLimits::kMin10Exp; \ -const int MathLimits::kMax10Exp; - -#define DEF_UNSIGNED_INT_LIMITS(Type) \ -DEF_COMMON_LIMITS(Type) \ -const Type MathLimits::kPosMin; \ -const Type MathLimits::kPosMax; \ -const Type MathLimits::kMin; \ -const Type MathLimits::kMax; \ -const Type MathLimits::kEpsilon; \ -const Type MathLimits::kStdError; - -#define DEF_SIGNED_INT_LIMITS(Type) \ -DEF_UNSIGNED_INT_LIMITS(Type) \ -const Type MathLimits::kNegMin; \ -const Type MathLimits::kNegMax; - -#define DEF_PRECISION_LIMITS(Type) \ -const int MathLimits::kPrecisionDigits; - -#endif // not COMPILER_MSVC - // http://en.wikipedia.org/wiki/Quadruple_precision_floating-point_format#Double-double_arithmetic // With some compilers (gcc 4.6.x) on some platforms (powerpc64), // "long double" is implemented as a pair of double: "double double" format. @@ -101,7 +65,6 @@ const int MathLimits::kPrecisionDigits; // max(DBL_EPSILON * DBL_EPSILON, kEpsilon) rather than a multiple of kEpsilon. #define DEF_FP_LIMITS(Type, PREFIX) \ -DEF_COMMON_LIMITS(Type) \ const Type MathLimits::kPosMin = PREFIX##_MIN; \ const Type MathLimits::kPosMax = PREFIX##_MAX; \ const Type MathLimits::kMin = -MathLimits::kPosMax; \ @@ -113,32 +76,14 @@ const Type MathLimits::kEpsilon = PREFIX##_EPSILON; \ const Type MathLimits::kStdError = \ 32 * (DBL_EPSILON * DBL_EPSILON > MathLimits::kEpsilon \ ? DBL_EPSILON * DBL_EPSILON : MathLimits::kEpsilon); \ -DEF_PRECISION_LIMITS(Type) \ const Type MathLimits::kNaN = HUGE_VAL - HUGE_VAL; \ const Type MathLimits::kPosInf = HUGE_VAL; \ const Type MathLimits::kNegInf = -HUGE_VAL; -// The following are *not* casts! -DEF_SIGNED_INT_LIMITS(int8) -DEF_SIGNED_INT_LIMITS(int16) // NOLINT(readability/casting) -DEF_SIGNED_INT_LIMITS(int32) // NOLINT(readability/casting) -DEF_SIGNED_INT_LIMITS(int64) // NOLINT(readability/casting) -DEF_UNSIGNED_INT_LIMITS(uint8) -DEF_UNSIGNED_INT_LIMITS(uint16) // NOLINT(readability/casting) -DEF_UNSIGNED_INT_LIMITS(uint32) // NOLINT(readability/casting) -DEF_UNSIGNED_INT_LIMITS(uint64) // NOLINT(readability/casting) - -DEF_SIGNED_INT_LIMITS(long int) -DEF_UNSIGNED_INT_LIMITS(unsigned long int) - DEF_FP_LIMITS(float, FLT) DEF_FP_LIMITS(double, DBL) DEF_FP_LIMITS(long double, LDBL); -#undef DEF_COMMON_LIMITS -#undef DEF_SIGNED_INT_LIMITS -#undef DEF_UNSIGNED_INT_LIMITS #undef DEF_FP_LIMITS -#undef DEF_PRECISION_LIMITS } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/stubs/mathlimits.h b/src/google/protobuf/stubs/mathlimits.h index 9c9d0e9a2b..0e8443228c 100644 --- a/src/google/protobuf/stubs/mathlimits.h +++ b/src/google/protobuf/stubs/mathlimits.h @@ -66,6 +66,8 @@ #include +#include + // ========================================================================= // // Useful integer and floating point limits and type traits. @@ -173,43 +175,43 @@ template struct MathLimits { static bool IsPosInf(const Type /*x*/) { return false; } \ static bool IsNegInf(const Type /*x*/) { return false; } -#define DECL_SIGNED_INT_LIMITS(IntType, UnsignedIntType) \ -template<> \ -struct LIBPROTOBUF_EXPORT MathLimits { \ - typedef IntType Type; \ - typedef UnsignedIntType UnsignedType; \ - static const bool kIsSigned = true; \ - static const bool kIsInteger = true; \ - static const Type kPosMin = 1; \ - static const Type kPosMax = SIGNED_INT_MAX(Type); \ - static const Type kMin = SIGNED_INT_MIN(Type); \ - static const Type kMax = kPosMax; \ - static const Type kNegMin = -1; \ - static const Type kNegMax = kMin; \ - static const int kMin10Exp = 0; \ - static const int kMax10Exp = SIGNED_MAX_10_EXP(Type); \ - static const Type kEpsilon = 1; \ - static const Type kStdError = 0; \ - DECL_INT_LIMIT_FUNCS \ -}; +#define DECL_SIGNED_INT_LIMITS(IntType, UnsignedIntType) \ + template <> \ + struct PROTOBUF_EXPORT MathLimits { \ + typedef IntType Type; \ + typedef UnsignedIntType UnsignedType; \ + static const bool kIsSigned = true; \ + static const bool kIsInteger = true; \ + static const Type kPosMin = 1; \ + static const Type kPosMax = SIGNED_INT_MAX(Type); \ + static const Type kMin = SIGNED_INT_MIN(Type); \ + static const Type kMax = kPosMax; \ + static const Type kNegMin = -1; \ + static const Type kNegMax = kMin; \ + static const int kMin10Exp = 0; \ + static const int kMax10Exp = SIGNED_MAX_10_EXP(Type); \ + static const Type kEpsilon = 1; \ + static const Type kStdError = 0; \ + DECL_INT_LIMIT_FUNCS \ + }; -#define DECL_UNSIGNED_INT_LIMITS(IntType) \ -template<> \ -struct LIBPROTOBUF_EXPORT MathLimits { \ - typedef IntType Type; \ - typedef IntType UnsignedType; \ - static const bool kIsSigned = false; \ - static const bool kIsInteger = true; \ - static const Type kPosMin = 1; \ - static const Type kPosMax = UNSIGNED_INT_MAX(Type); \ - static const Type kMin = 0; \ - static const Type kMax = kPosMax; \ - static const int kMin10Exp = 0; \ - static const int kMax10Exp = UNSIGNED_MAX_10_EXP(Type); \ - static const Type kEpsilon = 1; \ - static const Type kStdError = 0; \ - DECL_INT_LIMIT_FUNCS \ -}; +#define DECL_UNSIGNED_INT_LIMITS(IntType) \ + template <> \ + struct PROTOBUF_EXPORT MathLimits { \ + typedef IntType Type; \ + typedef IntType UnsignedType; \ + static const bool kIsSigned = false; \ + static const bool kIsInteger = true; \ + static const Type kPosMin = 1; \ + static const Type kPosMax = UNSIGNED_INT_MAX(Type); \ + static const Type kMin = 0; \ + static const Type kMax = kPosMax; \ + static const int kMin10Exp = 0; \ + static const int kMax10Exp = UNSIGNED_MAX_10_EXP(Type); \ + static const Type kEpsilon = 1; \ + static const Type kStdError = 0; \ + DECL_INT_LIMIT_FUNCS \ + }; DECL_SIGNED_INT_LIMITS(signed char, unsigned char) DECL_SIGNED_INT_LIMITS(signed short int, unsigned short int) @@ -243,7 +245,7 @@ DECL_UNSIGNED_INT_LIMITS(unsigned long long int) #endif // ========================================================================= // -#if WIN32 && !__MINGW32__ // Lacks built-in isnan() and isinf() +#if defined(_WIN32) && !defined(__MINGW32__) // Lacks built-in isnan() and isinf() #define DECL_FP_LIMIT_FUNCS \ static bool IsFinite(const Type x) { return _finite(x); } \ static bool IsNaN(const Type x) { return _isnan(x); } \ @@ -263,29 +265,29 @@ DECL_UNSIGNED_INT_LIMITS(unsigned long long int) // such constants are not considered to be primitive-type constants by gcc. // CAVEAT: Hence, they are going to be initialized only during // the global objects construction time. -#define DECL_FP_LIMITS(FP_Type, PREFIX) \ -template<> \ -struct LIBPROTOBUF_EXPORT MathLimits { \ - typedef FP_Type Type; \ - typedef FP_Type UnsignedType; \ - static const bool kIsSigned = true; \ - static const bool kIsInteger = false; \ - static const Type kPosMin; \ - static const Type kPosMax; \ - static const Type kMin; \ - static const Type kMax; \ - static const Type kNegMin; \ - static const Type kNegMax; \ - static const int kMin10Exp = PREFIX##_MIN_10_EXP; \ - static const int kMax10Exp = PREFIX##_MAX_10_EXP; \ - static const Type kEpsilon; \ - static const Type kStdError; \ - static const int kPrecisionDigits = PREFIX##_DIG; \ - static const Type kNaN; \ - static const Type kPosInf; \ - static const Type kNegInf; \ - DECL_FP_LIMIT_FUNCS \ -}; +#define DECL_FP_LIMITS(FP_Type, PREFIX) \ + template <> \ + struct PROTOBUF_EXPORT MathLimits { \ + typedef FP_Type Type; \ + typedef FP_Type UnsignedType; \ + static const bool kIsSigned = true; \ + static const bool kIsInteger = false; \ + static const Type kPosMin; \ + static const Type kPosMax; \ + static const Type kMin; \ + static const Type kMax; \ + static const Type kNegMin; \ + static const Type kNegMax; \ + static const int kMin10Exp = PREFIX##_MIN_10_EXP; \ + static const int kMax10Exp = PREFIX##_MAX_10_EXP; \ + static const Type kEpsilon; \ + static const Type kStdError; \ + static const int kPrecisionDigits = PREFIX##_DIG; \ + static const Type kNaN; \ + static const Type kPosInf; \ + static const Type kNegInf; \ + DECL_FP_LIMIT_FUNCS \ + }; DECL_FP_LIMITS(float, FLT) DECL_FP_LIMITS(double, DBL) @@ -300,4 +302,6 @@ DECL_FP_LIMITS(long double, LDBL) } // namespace protobuf } // namespace google +#include + #endif // UTIL_MATH_MATHLIMITS_H__ diff --git a/src/google/protobuf/stubs/mathutil.h b/src/google/protobuf/stubs/mathutil.h index 8a9f69a0b2..479032d064 100644 --- a/src/google/protobuf/stubs/mathutil.h +++ b/src/google/protobuf/stubs/mathutil.h @@ -67,7 +67,7 @@ class MathUtil { template static bool AlmostEquals(T a, T b) { - return ::google::protobuf::internal::AlmostEquals(a, b); + return internal::AlmostEquals(a, b); } // Largest of two values. @@ -126,8 +126,7 @@ bool MathUtil::WithinFractionOrMargin(const T x, const T y, if (MathLimits::kIsInteger) { return x == y; } else { - // IsFinite checks are to make kPosInf and kNegInf not within fraction - if (!MathLimits::IsFinite(x) && !MathLimits::IsFinite(y)) { + if (!MathLimits::IsFinite(x) || !MathLimits::IsFinite(y)) { return false; } T relative_margin = static_cast(fraction * Max(Abs(x), Abs(y))); diff --git a/src/google/protobuf/stubs/mutex.h b/src/google/protobuf/stubs/mutex.h index 2b1aab87ac..a311889610 100644 --- a/src/google/protobuf/stubs/mutex.h +++ b/src/google/protobuf/stubs/mutex.h @@ -32,6 +32,17 @@ #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 @@ -41,11 +52,15 @@ __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 { @@ -54,10 +69,31 @@ 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 LIBPROTOBUF_EXPORT WrappedMutex { +class PROTOBUF_EXPORT GOOGLE_PROTOBUF_CAPABILITY("mutex") WrappedMutex { public: WrappedMutex() = default; void Lock() GOOGLE_PROTOBUF_ACQUIRE() { mu_.lock(); } @@ -67,13 +103,17 @@ class LIBPROTOBUF_EXPORT WrappedMutex { 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 LIBPROTOBUF_EXPORT MutexLock { +class PROTOBUF_EXPORT MutexLock { public: explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); } ~MutexLock() { this->mu_->Unlock(); } @@ -87,7 +127,7 @@ typedef MutexLock ReaderMutexLock; typedef MutexLock WriterMutexLock; // MutexLockMaybe is like MutexLock, but is a no-op when mu is nullptr. -class LIBPROTOBUF_EXPORT MutexLockMaybe { +class PROTOBUF_EXPORT MutexLockMaybe { public: explicit MutexLockMaybe(Mutex *mu) : mu_(mu) { if (this->mu_ != nullptr) { this->mu_->Lock(); } } @@ -141,4 +181,6 @@ using internal::MutexLockMaybe; #undef GOOGLE_PROTOBUF_ACQUIRE #undef GOOGLE_PROTOBUF_RELEASE +#include + #endif // GOOGLE_PROTOBUF_STUBS_MUTEX_H_ diff --git a/src/google/protobuf/stubs/once.h b/src/google/protobuf/stubs/once.h index f3835ccd04..070d36d193 100644 --- a/src/google/protobuf/stubs/once.h +++ b/src/google/protobuf/stubs/once.h @@ -28,59 +28,14 @@ // (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) -// -// emulates google3/base/once.h -// -// This header is intended to be included only by internal .cc files and -// generated .pb.cc files. Users should not use this directly. -// -// This is basically a portable version of pthread_once(). -// -// This header declares: -// * A type called ProtobufOnceType. -// * A macro GOOGLE_PROTOBUF_DECLARE_ONCE() which declares a variable of type -// ProtobufOnceType. This is the only legal way to declare such a variable. -// The macro may only be used at the global scope (you cannot create local or -// class member variables of this type). -// * A function GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()). -// This function, when invoked multiple times given the same ProtobufOnceType -// object, will invoke init_func on the first call only, and will make sure -// none of the calls return before that first call to init_func has finished. -// * The user can provide a parameter which GoogleOnceInit() forwards to the -// user-provided function when it is called. Usage example: -// int a = 10; -// GoogleOnceInit(&my_once, &MyFunctionExpectingIntArgument, &a); -// * This implementation guarantees that ProtobufOnceType is a POD (i.e. no -// static initializer generated). -// -// This implements a way to perform lazy initialization. It's more efficient -// than using mutexes as no lock is needed if initialization has already -// happened. -// -// Example usage: -// void Init(); -// GOOGLE_PROTOBUF_DECLARE_ONCE(once_init); -// -// // Calls Init() exactly once. -// void InitOnce() { -// GoogleOnceInit(&once_init, &Init); -// } -// -// Note that if GoogleOnceInit() is called before main() has begun, it must -// only be called by the thread that will eventually call main() -- that is, -// the thread that performs dynamic initialization. In general this is a safe -// assumption since people don't usually construct threads before main() starts, -// but it is technically not guaranteed. Unfortunately, Win32 provides no way -// whatsoever to statically-initialize its synchronization primitives, so our -// only choice is to assume that dynamic initialization is single-threaded. - #ifndef GOOGLE_PROTOBUF_STUBS_ONCE_H__ #define GOOGLE_PROTOBUF_STUBS_ONCE_H__ #include #include +#include + namespace google { namespace protobuf { namespace internal { @@ -92,39 +47,9 @@ void call_once(Args&&... args ) { } } // namespace internal - -// TODO(gerbens) remove this once third_party is fully extracted -using ProtobufOnceType = internal::once_flag; - -inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) { - std::call_once(*once, init_func); -} - -template -inline void GoogleOnceInitArg(ProtobufOnceType* once, void (*init_func)(Arg*), - Arg* arg) { - std::call_once(*once, init_func, arg); -} - -class GoogleOnceDynamic { - public: - // If this->Init() has not been called before by any thread, - // execute (*func_with_arg)(arg) then return. - // Otherwise, wait until that prior invocation has finished - // executing its function, then return. - template - void Init(void (*func_with_arg)(T*), T* arg) { - GoogleOnceInitArg(&this->state_, func_with_arg, arg); - } - private: - ProtobufOnceType state_; -}; - -#define GOOGLE_PROTOBUF_ONCE_TYPE ::google::protobuf::ProtobufOnceType -#define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \ - ::google::protobuf::ProtobufOnceType NAME - } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_ONCE_H__ diff --git a/src/google/protobuf/stubs/port.h b/src/google/protobuf/stubs/port.h index 4d3d000865..5b8e85dc74 100644 --- a/src/google/protobuf/stubs/port.h +++ b/src/google/protobuf/stubs/port.h @@ -46,6 +46,8 @@ #include +#include + #undef PROTOBUF_LITTLE_ENDIAN #ifdef _WIN32 // Assuming windows is always little-endian. @@ -72,6 +74,19 @@ #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) @@ -88,16 +103,6 @@ #define LIBPROTOC_EXPORT #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 - #define PROTOBUF_RUNTIME_DEPRECATED(message) #define GOOGLE_PROTOBUF_RUNTIME_DEPRECATED(message) @@ -151,131 +156,12 @@ typedef uint32_t uint32; typedef uint64_t uint64; #endif -// long long macros to be used because gcc and vc++ use different suffixes, -// and different size specifiers in format strings -#undef GOOGLE_LONGLONG -#undef GOOGLE_ULONGLONG -#undef GOOGLE_LL_FORMAT - -#ifdef _MSC_VER -#define GOOGLE_LONGLONG(x) x##I64 -#define GOOGLE_ULONGLONG(x) x##UI64 -#define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...) -#else -// By long long, we actually mean int64. -#define GOOGLE_LONGLONG(x) x##LL -#define GOOGLE_ULONGLONG(x) x##ULL -// Used to format real long long integers. -#define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also. -#endif - static const int32 kint32max = 0x7FFFFFFF; static const int32 kint32min = -kint32max - 1; -static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF); +static const int64 kint64max = PROTOBUF_LONGLONG(0x7FFFFFFFFFFFFFFF); static const int64 kint64min = -kint64max - 1; static const uint32 kuint32max = 0xFFFFFFFFu; -static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); - -#define GOOGLE_PROTOBUF_NAMESPACE "google::protobuf" -#define GOOGLE_PROTOBUF_NAMESPACE_ID google::protobuf - -// ------------------------------------------------------------------- -// 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. - -#ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE -#if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) -// For functions we want to force inline. -// Introduced in gcc 3.1. -#define GOOGLE_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline)) -#else -// Other compilers will have to figure it out for themselves. -#define GOOGLE_ATTRIBUTE_ALWAYS_INLINE -#endif -#endif - -#define GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE GOOGLE_ATTRIBUTE_ALWAYS_INLINE - -#ifndef GOOGLE_ATTRIBUTE_NOINLINE -#if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) -// For functions we want to force not inline. -// Introduced in gcc 3.1. -#define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline)) -#elif defined(_MSC_VER) && (_MSC_VER >= 1400) -// Seems to have been around since at least Visual Studio 2005 -#define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline) -#else -// Other compilers will have to figure it out for themselves. -#define GOOGLE_ATTRIBUTE_NOINLINE -#endif -#endif - -#define GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE GOOGLE_ATTRIBUTE_NOINLINE - -#ifndef GOOGLE_ATTRIBUTE_FUNC_ALIGN -#if defined(__clang__) || \ - defined(__GNUC__) && (__GNUC__ > 4 ||(__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) -// Function alignment attribute introduced in gcc 4.3 -#define GOOGLE_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__ ((aligned(bytes))) -#else -#define GOOGLE_ATTRIBUTE_FUNC_ALIGN(bytes) -#endif -#endif - -#define GOOGLE_PROTOBUF_ATTRIBUTE_FUNC_ALIGN(bytes) \ - GOOGLE_ATTRIBUTE_FUNC_ALIGN(bytes) - -#ifndef GOOGLE_PREDICT_TRUE -#ifdef __GNUC__ -// Provided at least since GCC 3.0. -#define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) -#else -#define GOOGLE_PREDICT_TRUE(x) (x) -#endif -#endif - -#ifndef GOOGLE_PREDICT_FALSE -#ifdef __GNUC__ -// Provided at least since GCC 3.0. -#define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(x, 0)) -#else -#define GOOGLE_PREDICT_FALSE(x) (x) -#endif -#endif - -#ifndef GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL -#ifdef __GNUC__ -#define GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL \ - __attribute__((returns_nonnull)) -#endif -#endif - -// Delimits a block of code which may write to memory which is simultaneously -// written by other threads, but which has been determined to be thread-safe -// (e.g. because it is an idempotent write). -#ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN -#define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN() -#endif -#ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END -#define GOOGLE_SAFE_CONCURRENT_WRITES_END() -#endif - -#define GOOGLE_GUARDED_BY(x) -#define GOOGLE_ATTRIBUTE_COLD - -#ifdef GOOGLE_PROTOBUF_DONT_USE_UNALIGNED -# define GOOGLE_PROTOBUF_USE_UNALIGNED 0 -#else -# if defined(_M_X64) || defined(__x86_64__) || defined(_M_IX86) || defined(__i386__) -# define GOOGLE_PROTOBUF_USE_UNALIGNED 1 -# else -# define GOOGLE_PROTOBUF_USE_UNALIGNED 0 -# endif -#endif - -#define GOOGLE_PROTOBUF_ATTRIBUTE_COLD GOOGLE_ATTRIBUTE_COLD +static const uint64 kuint64max = PROTOBUF_ULONGLONG(0xFFFFFFFFFFFFFFFF); #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\ defined(MEMORY_SANITIZER) @@ -405,14 +291,14 @@ static inline uint32 bswap_32(uint32 x) { #ifndef bswap_64 static inline uint64 bswap_64(uint64 x) { - return (((x & GOOGLE_ULONGLONG(0xFF)) << 56) | - ((x & GOOGLE_ULONGLONG(0xFF00)) << 40) | - ((x & GOOGLE_ULONGLONG(0xFF0000)) << 24) | - ((x & GOOGLE_ULONGLONG(0xFF000000)) << 8) | - ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) | - ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) | - ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) | - ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56)); + 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 @@ -484,7 +370,7 @@ class Bits { // =================================================================== // from google3/util/endian/endian.h -LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x); +PROTOBUF_EXPORT uint32 ghtonl(uint32 x); class BigEndian { public: @@ -542,13 +428,16 @@ class BigEndian { } }; -#ifndef GOOGLE_ATTRIBUTE_SECTION_VARIABLE -#define GOOGLE_ATTRIBUTE_SECTION_VARIABLE(name) -#endif - -#define GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(name) - } // namespace protobuf } // namespace google +#ifdef PROTOBUF_ENABLE_EXPERIMENTAL_PARSER +#define GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER \ + PROTOBUF_ENABLE_EXPERIMENTAL_PARSER +#else +#define GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER 0 +#endif + +#include + #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_ diff --git a/src/google/protobuf/stubs/status.h b/src/google/protobuf/stubs/status.h index c5d38f0b46..440a0ed41a 100644 --- a/src/google/protobuf/stubs/status.h +++ b/src/google/protobuf/stubs/status.h @@ -36,6 +36,8 @@ #include #include +#include + namespace google { namespace protobuf { namespace util { @@ -62,7 +64,7 @@ enum Code { }; } // namespace error -class LIBPROTOBUF_EXPORT Status { +class PROTOBUF_EXPORT Status { public: // Creates a "successful" status. Status(); @@ -88,9 +90,15 @@ class LIBPROTOBUF_EXPORT Status { 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 { @@ -106,11 +114,14 @@ class LIBPROTOBUF_EXPORT Status { }; // Prints a human-readable representation of 'x' to 'os'. -LIBPROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x); +PROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x); #define EXPECT_OK(value) EXPECT_TRUE((value).ok()) } // namespace util } // namespace protobuf } // namespace google + +#include + #endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_ diff --git a/src/google/protobuf/stubs/status_macros.h b/src/google/protobuf/stubs/status_macros.h index 743e79a727..0c64317d3c 100644 --- a/src/google/protobuf/stubs/status_macros.h +++ b/src/google/protobuf/stubs/status_macros.h @@ -46,11 +46,11 @@ namespace util { // // Example: // RETURN_IF_ERROR(DoThings(4)); -#define RETURN_IF_ERROR(expr) \ - do { \ +#define RETURN_IF_ERROR(expr) \ + do { \ /* Using _status below to avoid capture problems if expr is "status". */ \ - const ::google::protobuf::util::Status _status = (expr); \ - if (GOOGLE_PREDICT_FALSE(!_status.ok())) return _status; \ + const PROTOBUF_NAMESPACE_ID::util::Status _status = (expr); \ + if (PROTOBUF_PREDICT_FALSE(!_status.ok())) return _status; \ } while (0) // Internal helper for concatenating macro values. @@ -67,7 +67,7 @@ Status DoAssignOrReturn(T& lhs, StatusOr result) { #define ASSIGN_OR_RETURN_IMPL(status, lhs, rexpr) \ Status status = DoAssignOrReturn(lhs, (rexpr)); \ - if (GOOGLE_PREDICT_FALSE(!status.ok())) return status; + if (PROTOBUF_PREDICT_FALSE(!status.ok())) return status; // Executes an expression that returns a util::StatusOr, extracting its value // into the variable defined by lhs (or returning on error). diff --git a/src/google/protobuf/stubs/status_test.cc b/src/google/protobuf/stubs/status_test.cc index c70c33c474..9a18778fba 100644 --- a/src/google/protobuf/stubs/status_test.cc +++ b/src/google/protobuf/stubs/status_test.cc @@ -40,13 +40,17 @@ namespace { TEST(Status, Empty) { util::Status status; EXPECT_EQ(util::error::OK, util::Status::OK.error_code()); + EXPECT_EQ(util::error::OK, util::Status::OK.code()); EXPECT_EQ("OK", util::Status::OK.ToString()); } TEST(Status, GenericCodes) { EXPECT_EQ(util::error::OK, util::Status::OK.error_code()); + EXPECT_EQ(util::error::OK, util::Status::OK.code()); EXPECT_EQ(util::error::CANCELLED, util::Status::CANCELLED.error_code()); + EXPECT_EQ(util::error::CANCELLED, util::Status::CANCELLED.code()); EXPECT_EQ(util::error::UNKNOWN, util::Status::UNKNOWN.error_code()); + EXPECT_EQ(util::error::UNKNOWN, util::Status::UNKNOWN.code()); } TEST(Status, ConstructorZero) { @@ -66,14 +70,17 @@ TEST(Status, ErrorMessage) { util::Status status(util::error::INVALID_ARGUMENT, ""); EXPECT_FALSE(status.ok()); EXPECT_EQ("", status.error_message().ToString()); + EXPECT_EQ("", status.message().ToString()); EXPECT_EQ("INVALID_ARGUMENT", status.ToString()); status = util::Status(util::error::INVALID_ARGUMENT, "msg"); EXPECT_FALSE(status.ok()); EXPECT_EQ("msg", status.error_message().ToString()); + EXPECT_EQ("msg", status.message().ToString()); EXPECT_EQ("INVALID_ARGUMENT:msg", status.ToString()); status = util::Status(util::error::OK, "msg"); EXPECT_TRUE(status.ok()); EXPECT_EQ("", status.error_message().ToString()); + EXPECT_EQ("", status.message().ToString()); EXPECT_EQ("OK", status.ToString()); } diff --git a/src/google/protobuf/stubs/statusor.cc b/src/google/protobuf/stubs/statusor.cc index 48d1402ae0..c744b8d237 100644 --- a/src/google/protobuf/stubs/statusor.cc +++ b/src/google/protobuf/stubs/statusor.cc @@ -30,6 +30,8 @@ #include +#include + namespace google { namespace protobuf { namespace util { diff --git a/src/google/protobuf/stubs/statusor.h b/src/google/protobuf/stubs/statusor.h index b1c1e6b482..90fd5f0df1 100644 --- a/src/google/protobuf/stubs/statusor.h +++ b/src/google/protobuf/stubs/statusor.h @@ -89,6 +89,8 @@ #include +#include + namespace google { namespace protobuf { namespace util { @@ -162,7 +164,7 @@ class StatusOr { namespace internal { -class LIBPROTOBUF_EXPORT StatusOrHelper { +class PROTOBUF_EXPORT StatusOrHelper { public: // Move type-agnostic error handling to the .cc. static void Crash(const util::Status& status); @@ -256,4 +258,6 @@ inline const T& StatusOr::ValueOrDie() const { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_STATUSOR_H_ diff --git a/src/google/protobuf/stubs/stl_util.h b/src/google/protobuf/stubs/stl_util.h index 2c265e9218..aa81eb6f72 100644 --- a/src/google/protobuf/stubs/stl_util.h +++ b/src/google/protobuf/stubs/stl_util.h @@ -38,26 +38,6 @@ namespace google { namespace protobuf { -// STLDeleteContainerPointers() -// For a range within a container of pointers, calls delete -// (non-array version) on these pointers. -// NOTE: for these three functions, we could just implement a DeleteObject -// functor and then call for_each() on the range and functor, but this -// requires us to pull in all of algorithm.h, which seems expensive. -// For hash_[multi]set, it is important that this deletes behind the iterator -// because the hash_set may call the hash function on the iterator when it is -// advanced, which could result in the hash function trying to deference a -// stale pointer. -template -void STLDeleteContainerPointers(ForwardIterator begin, - ForwardIterator end) { - while (begin != end) { - ForwardIterator temp = begin; - ++begin; - delete *temp; - } -} - // 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 @@ -85,36 +65,6 @@ inline char* string_as_array(string* str) { return str->empty() ? nullptr : &*str->begin(); } -// STLDeleteElements() deletes all the elements in an STL container and clears -// the container. This function is suitable for use with a vector, set, -// hash_set, or any other STL container which defines sensible begin(), end(), -// and clear() methods. -// -// If container is nullptr, this function is a no-op. -// -// As an alternative to calling STLDeleteElements() directly, consider -// ElementDeleter (defined below), which ensures that your container's elements -// are deleted when the ElementDeleter goes out of scope. -template -void STLDeleteElements(T *container) { - if (!container) return; - STLDeleteContainerPointers(container->begin(), container->end()); - container->clear(); -} - -// Given an STL container consisting of (key, value) pairs, STLDeleteValues -// deletes all the "value" components and clears the container. Does nothing -// in the case it's given a null pointer. - -template -void STLDeleteValues(T *v) { - if (!v) return; - for (typename T::iterator i = v->begin(); i != v->end(); ++i) { - delete i->second; - } - v->clear(); -} - } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/stubs/stringpiece.cc b/src/google/protobuf/stubs/stringpiece.cc index 96a7d3da86..fc2f521059 100644 --- a/src/google/protobuf/stubs/stringpiece.cc +++ b/src/google/protobuf/stubs/stringpiece.cc @@ -35,6 +35,8 @@ #include #include +#include + namespace google { namespace protobuf { std::ostream& operator<<(std::ostream& o, StringPiece piece) { diff --git a/src/google/protobuf/stubs/stringpiece.h b/src/google/protobuf/stubs/stringpiece.h index 6ab985cce7..2ae38813f6 100644 --- a/src/google/protobuf/stubs/stringpiece.h +++ b/src/google/protobuf/stubs/stringpiece.h @@ -151,6 +151,8 @@ #include #include +#include + namespace google { namespace protobuf { // StringPiece has *two* size types. @@ -175,7 +177,7 @@ typedef string::difference_type stringpiece_ssize_type; #define STRINGPIECE_CHECK_SIZE 0 #endif -class LIBPROTOBUF_EXPORT StringPiece { +class PROTOBUF_EXPORT StringPiece { private: const char* ptr_; stringpiece_ssize_type length_; @@ -479,7 +481,7 @@ 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++) { + for (const char *str = s.data(), *end = str + s.size(); str < end; str++) { result = 5 * result + static_cast(*str); } return result; @@ -487,4 +489,6 @@ template<> struct hash { }; GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END +#include + #endif // STRINGS_STRINGPIECE_H_ diff --git a/src/google/protobuf/stubs/stringprintf.cc b/src/google/protobuf/stubs/stringprintf.cc index 820bfe7f75..e6019fc664 100644 --- a/src/google/protobuf/stubs/stringprintf.cc +++ b/src/google/protobuf/stubs/stringprintf.cc @@ -36,7 +36,9 @@ #include // For va_list and related operations #include // MSVC requires this for _vsnprintf #include + #include +#include namespace google { namespace protobuf { diff --git a/src/google/protobuf/stubs/stringprintf.h b/src/google/protobuf/stubs/stringprintf.h index 7183ec6a07..253d736a21 100644 --- a/src/google/protobuf/stubs/stringprintf.h +++ b/src/google/protobuf/stubs/stringprintf.h @@ -46,31 +46,38 @@ #include +#include + namespace google { namespace protobuf { // Return a C++ string -LIBPROTOBUF_EXPORT extern string StringPrintf(const char* format, ...); +PROTOBUF_EXPORT extern string StringPrintf(const char* format, ...); // Store result into a supplied string and return it -LIBPROTOBUF_EXPORT extern const string& SStringPrintf(string* dst, const char* format, ...); +PROTOBUF_EXPORT extern const string& SStringPrintf(string* dst, + const char* format, ...); // Append result to a supplied string -LIBPROTOBUF_EXPORT extern void StringAppendF(string* dst, const char* format, ...); +PROTOBUF_EXPORT extern void StringAppendF(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. -LIBPROTOBUF_EXPORT extern void StringAppendV(string* dst, const char* format, va_list ap); +PROTOBUF_EXPORT extern void StringAppendV(string* dst, const char* format, + va_list ap); // The max arguments supported by StringPrintfVector -LIBPROTOBUF_EXPORT extern const int kStringPrintfVectorMaxArgs; +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 -LIBPROTOBUF_EXPORT extern string StringPrintfVector(const char* format, const std::vector& v); +PROTOBUF_EXPORT extern string StringPrintfVector(const char* format, + const std::vector& v); } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_STRINGPRINTF_H diff --git a/src/google/protobuf/stubs/structurally_valid.cc b/src/google/protobuf/stubs/structurally_valid.cc index b22396829c..0598427d20 100644 --- a/src/google/protobuf/stubs/structurally_valid.cc +++ b/src/google/protobuf/stubs/structurally_valid.cc @@ -555,7 +555,7 @@ InitDetector init_detector; bool IsStructurallyValidUTF8(const char* buf, int len) { if (!module_initialized_) return true; - + int bytes_consumed = 0; UTF8GenericScanFastAscii(&utf8acceptnonsurrogates_obj, buf, len, &bytes_consumed); diff --git a/src/google/protobuf/stubs/strutil.cc b/src/google/protobuf/stubs/strutil.cc index 5bf9f2e6a8..5486887295 100644 --- a/src/google/protobuf/stubs/strutil.cc +++ b/src/google/protobuf/stubs/strutil.cc @@ -40,7 +40,9 @@ #include #include +#include #include +#include #ifdef _WIN32 // MSVC has only _snprintf, not snprintf. @@ -1063,10 +1065,12 @@ done: } char* FastInt32ToBufferLeft(int32 i, char* buffer) { - uint32 u = i; + uint32 u = 0; if (i < 0) { *buffer++ = '-'; - u = -i; + u -= i; + } else { + u = i; } return FastUInt32ToBufferLeft(u, buffer); } @@ -1114,10 +1118,12 @@ char* FastUInt64ToBufferLeft(uint64 u64, char* buffer) { } char* FastInt64ToBufferLeft(int64 i, char* buffer) { - uint64 u = i; + uint64 u = 0; if (i < 0) { *buffer++ = '-'; - u = -i; + u -= i; + } else { + u = i; } return FastUInt64ToBufferLeft(u, buffer); } @@ -1286,7 +1292,7 @@ char* DoubleToBuffer(double value, char* buffer) { // 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 = strtod(buffer, nullptr); + volatile double parsed_value = io::NoLocaleStrtod(buffer, nullptr); if (parsed_value != value) { int snprintf_result = snprintf(buffer, kDoubleToBufferSize, "%.*g", DBL_DIG+2, value); @@ -1338,7 +1344,7 @@ 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 = strtod(str, &endptr); + *value = io::NoLocaleStrtod(str, &endptr); #else *value = strtof(str, &endptr); #endif @@ -1347,7 +1353,7 @@ bool safe_strtof(const char* str, float* value) { bool safe_strtod(const char* str, double* value) { char* endptr; - *value = strtod(str, &endptr); + *value = io::NoLocaleStrtod(str, &endptr); if (endptr != str) { while (ascii_isspace(*endptr)) ++endptr; } @@ -1959,11 +1965,12 @@ int Base64UnescapeInternal(const char *src_param, int szsrc, // #include // #include // #include +// #include // main() // { // static const char Base64[] = // "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -// char *pos; +// const char *pos; // int idx, i, j; // printf(" "); // for (i = 0; i < 255; i += 8) { @@ -1976,7 +1983,7 @@ int Base64UnescapeInternal(const char *src_param, int szsrc, // if (idx == -1) // printf(" %2d, ", idx); // else -// printf(" %2d/*%c*/,", idx, j); +// printf(" %2d/""*%c*""/,", idx, j); // } // printf("\n "); // } @@ -1994,7 +2001,7 @@ static const signed char kUnBase64[] = { 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*/, - 07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/, + 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*/, @@ -2028,7 +2035,7 @@ static const signed char kUnWebSafeBase64[] = { 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*/, - 07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/, + 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*/, diff --git a/src/google/protobuf/stubs/strutil.h b/src/google/protobuf/stubs/strutil.h index ec5512d6f0..790500d26e 100644 --- a/src/google/protobuf/stubs/strutil.h +++ b/src/google/protobuf/stubs/strutil.h @@ -38,10 +38,12 @@ #include #include +#include + namespace google { namespace protobuf { -#ifdef _MSC_VER +#if defined(_MSC_VER) && _MSC_VER < 1800 #define strtoll _strtoi64 #define strtoull _strtoui64 #elif defined(__DECCXX) && defined(__osf__) @@ -155,13 +157,12 @@ inline string StripSuffixString(const string& str, const string& suffix) { // StripWhitespace // Removes whitespaces from both ends of the given string. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT void ReplaceCharacters(string* s, const char* remove, - char replacewith); -LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove, - char replacewith); - -LIBPROTOBUF_EXPORT void StripWhitespace(string* s); +PROTOBUF_EXPORT void ReplaceCharacters(string* s, const char* remove, + char replacewith); +PROTOBUF_EXPORT void StripString(string* s, const char* remove, + char replacewith); +PROTOBUF_EXPORT void StripWhitespace(string* s); // ---------------------------------------------------------------------- // LowerString() @@ -203,8 +204,8 @@ inline string ToUpper(const string& s) { // happened or not. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub, - const string& newsub, bool replace_all); +PROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub, + const string& newsub, bool replace_all); // ---------------------------------------------------------------------- // SplitStringUsing() @@ -212,8 +213,8 @@ LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub, // to 'result'. If there are consecutive delimiters, this function skips // over all of them. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim, - std::vector* res); +PROTOBUF_EXPORT void SplitStringUsing(const string& 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'. @@ -223,9 +224,9 @@ LIBPROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim, // // If "full" is the empty string, yields an empty string as the only value. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT void SplitStringAllowEmpty(const string& full, - const char* delim, - std::vector* result); +PROTOBUF_EXPORT void SplitStringAllowEmpty(const string& full, + const char* delim, + std::vector* result); // ---------------------------------------------------------------------- // Split() @@ -250,8 +251,8 @@ inline std::vector Split( // another takes a pointer to the target string. In the latter case the // target string is cleared and overwritten. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT void JoinStrings(const std::vector& components, - const char* delim, string* result); +PROTOBUF_EXPORT void JoinStrings(const std::vector& components, + const char* delim, string* result); inline string JoinStrings(const std::vector& components, const char* delim) { @@ -291,9 +292,9 @@ inline string JoinStrings(const std::vector& components, // processed. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest); -LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest, - std::vector *errors); +PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest); +PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest, + std::vector* errors); // ---------------------------------------------------------------------- // UnescapeCEscapeString() @@ -310,10 +311,10 @@ LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest, // the third call, the new string is returned. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest); -LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest, - std::vector *errors); -LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src); +PROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest); +PROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest, + std::vector* errors); +PROTOBUF_EXPORT string UnescapeCEscapeString(const string& src); // ---------------------------------------------------------------------- // CEscape() @@ -322,21 +323,21 @@ LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src); // // Escaped chars: \n, \r, \t, ", ', \, and !isprint(). // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT string CEscape(const string& src); +PROTOBUF_EXPORT string CEscape(const string& src); // ---------------------------------------------------------------------- // CEscapeAndAppend() // Escapes 'src' using C-style escape sequences, and appends the escaped // string to 'dest'. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT void CEscapeAndAppend(StringPiece src, string* dest); +PROTOBUF_EXPORT void CEscapeAndAppend(StringPiece src, string* dest); namespace strings { // Like CEscape() but does not escape bytes with the upper bit set. -LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src); +PROTOBUF_EXPORT string Utf8SafeCEscape(const string& src); // Like CEscape() but uses hex (\x) escapes instead of octals. -LIBPROTOBUF_EXPORT string CHexEscape(const string& src); +PROTOBUF_EXPORT string CHexEscape(const string& src); } // namespace strings // ---------------------------------------------------------------------- @@ -349,10 +350,10 @@ LIBPROTOBUF_EXPORT string CHexEscape(const string& src); // platforms, so using these is safer, from the point of view of // overflow behavior, than using the standard libc functions. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT int32 strto32_adaptor(const char *nptr, char **endptr, - int base); -LIBPROTOBUF_EXPORT uint32 strtou32_adaptor(const char *nptr, char **endptr, - int base); +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)) @@ -391,10 +392,10 @@ inline uint64 strtou64(const char *nptr, char **endptr, int base) { // safe_strtof() // safe_strtod() // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value); +PROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value); -LIBPROTOBUF_EXPORT bool safe_strto32(const string& str, int32* value); -LIBPROTOBUF_EXPORT bool safe_strtou32(const string& str, uint32* value); +PROTOBUF_EXPORT bool safe_strto32(const string& str, int32* value); +PROTOBUF_EXPORT bool safe_strtou32(const string& str, uint32* value); inline bool safe_strto32(const char* str, int32* value) { return safe_strto32(string(str), value); } @@ -408,8 +409,8 @@ inline bool safe_strtou32(StringPiece str, uint32* value) { return safe_strtou32(str.ToString(), value); } -LIBPROTOBUF_EXPORT bool safe_strto64(const string& str, int64* value); -LIBPROTOBUF_EXPORT bool safe_strtou64(const string& str, uint64* value); +PROTOBUF_EXPORT bool safe_strto64(const string& str, int64* value); +PROTOBUF_EXPORT bool safe_strtou64(const string& str, uint64* value); inline bool safe_strto64(const char* str, int64* value) { return safe_strto64(string(str), value); } @@ -423,8 +424,8 @@ inline bool safe_strtou64(StringPiece str, uint64* value) { return safe_strtou64(str.ToString(), value); } -LIBPROTOBUF_EXPORT bool safe_strtof(const char* str, float* value); -LIBPROTOBUF_EXPORT bool safe_strtod(const char* str, double* 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 string& str, float* value) { return safe_strtof(str.c_str(), value); } @@ -464,13 +465,13 @@ inline bool safe_strtod(StringPiece str, double* value) { // DoubleToBuffer() and FloatToBuffer(). static const int kFastToBufferSize = 32; -LIBPROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer); -LIBPROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer); +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 -LIBPROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer); -LIBPROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer); -LIBPROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer); +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) { @@ -506,10 +507,10 @@ inline char* FastULongToBuffer(unsigned long i, char* buffer) { // terminating the string). // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer); -LIBPROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer); -LIBPROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer); -LIBPROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer); +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) { @@ -531,12 +532,12 @@ inline string SimpleBtoa(bool value) { // // Return value: string // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT string SimpleItoa(int i); -LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i); -LIBPROTOBUF_EXPORT string SimpleItoa(long i); -LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i); -LIBPROTOBUF_EXPORT string SimpleItoa(long long i); -LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long long i); +PROTOBUF_EXPORT string SimpleItoa(int i); +PROTOBUF_EXPORT string SimpleItoa(unsigned int i); +PROTOBUF_EXPORT string SimpleItoa(long i); +PROTOBUF_EXPORT string SimpleItoa(unsigned long i); +PROTOBUF_EXPORT string SimpleItoa(long long i); +PROTOBUF_EXPORT string SimpleItoa(unsigned long long i); // ---------------------------------------------------------------------- // SimpleDtoa() @@ -557,11 +558,11 @@ LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long long i); // // Return value: string // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT string SimpleDtoa(double value); -LIBPROTOBUF_EXPORT string SimpleFtoa(float value); +PROTOBUF_EXPORT string SimpleDtoa(double value); +PROTOBUF_EXPORT string SimpleFtoa(float value); -LIBPROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer); -LIBPROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer); +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 @@ -610,7 +611,7 @@ struct Hex { } }; -struct LIBPROTOBUF_EXPORT AlphaNum { +struct PROTOBUF_EXPORT AlphaNum { const char *piece_data_; // move these to string_ref eventually size_t piece_size_; // move these to string_ref eventually @@ -619,16 +620,26 @@ struct LIBPROTOBUF_EXPORT AlphaNum { // No bool ctor -- bools convert to an integral type. // A bool ctor would also convert incoming pointers (bletch). - AlphaNum(int32 i32) + AlphaNum(int i32) : piece_data_(digits), piece_size_(FastInt32ToBufferLeft(i32, digits) - &digits[0]) {} - AlphaNum(uint32 u32) + AlphaNum(unsigned int u32) : piece_data_(digits), piece_size_(FastUInt32ToBufferLeft(u32, digits) - &digits[0]) {} - AlphaNum(int64 i64) + 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(uint64 u64) + AlphaNum(unsigned long u64) : piece_data_(digits), piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {} @@ -692,30 +703,30 @@ using strings::AlphaNum; // be a reference into str. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b); -LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c); -LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d); -LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e); -LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f); -LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f, - const AlphaNum& g); -LIBPROTOBUF_EXPORT 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); -LIBPROTOBUF_EXPORT 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); +PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b); +PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, + const AlphaNum& c); +PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, + const AlphaNum& c, const AlphaNum& d); +PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, + const AlphaNum& c, const AlphaNum& d, + const AlphaNum& e); +PROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b, + const AlphaNum& c, const AlphaNum& d, + const AlphaNum& e, const AlphaNum& f); +PROTOBUF_EXPORT 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 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 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 string StrCat(const AlphaNum& a) { return string(a.data(), a.size()); } @@ -740,14 +751,14 @@ inline string StrCat(const AlphaNum& a) { return string(a.data(), a.size()); } // worked around as consecutive calls to StrAppend are quite efficient. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a); -LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a, - const AlphaNum& b); -LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a, - const AlphaNum& b, const AlphaNum& c); -LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a, - const AlphaNum& b, const AlphaNum& c, - const AlphaNum& d); +PROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a); +PROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a, + const AlphaNum& b); +PROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a, + const AlphaNum& b, const AlphaNum& c); +PROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a, + const AlphaNum& b, const AlphaNum& c, + const AlphaNum& d); // ---------------------------------------------------------------------- // Join() @@ -777,7 +788,7 @@ string Join(const Range& components, // ToHex() // Return a lower-case hex string representation of the given integer. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT string ToHex(uint64 num); +PROTOBUF_EXPORT string ToHex(uint64 num); // ---------------------------------------------------------------------- // GlobalReplaceSubstring() @@ -786,9 +797,9 @@ LIBPROTOBUF_EXPORT string ToHex(uint64 num); // // NOTE: The string pieces must not overlap s. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT int GlobalReplaceSubstring(const string& substring, - const string& replacement, - string* s); +PROTOBUF_EXPORT int GlobalReplaceSubstring(const string& substring, + const string& replacement, + string* s); // ---------------------------------------------------------------------- // Base64Unescape() @@ -796,7 +807,7 @@ LIBPROTOBUF_EXPORT int GlobalReplaceSubstring(const string& substring, // writes it to "dest". If src contains invalid characters, dest is cleared // and the function returns false. Returns true on success. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT bool Base64Unescape(StringPiece src, string* dest); +PROTOBUF_EXPORT bool Base64Unescape(StringPiece src, string* dest); // ---------------------------------------------------------------------- // WebSafeBase64Unescape() @@ -809,18 +820,17 @@ LIBPROTOBUF_EXPORT bool Base64Unescape(StringPiece src, string* dest); // returns false (with dest empty) if src contains invalid chars; for // this version src and dest must be different strings. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT int WebSafeBase64Unescape(const char* src, int slen, - char* dest, int szdest); -LIBPROTOBUF_EXPORT bool WebSafeBase64Unescape(StringPiece src, string* dest); +PROTOBUF_EXPORT int WebSafeBase64Unescape(const char* src, int slen, char* dest, + int szdest); +PROTOBUF_EXPORT bool WebSafeBase64Unescape(StringPiece src, string* dest); // Return the length to use for the output buffer given to the base64 escape // routines. Make sure to use the same value for do_padding in both. // This function may return incorrect results if given input_len values that // are extremely high, which should happen rarely. -LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len, - bool do_padding); +PROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len, bool do_padding); // Use this version when calling Base64Escape without a do_padding arg. -LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len); +PROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len); // ---------------------------------------------------------------------- // Base64Escape() @@ -834,23 +844,23 @@ LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len); // to escape them. It also has an extra parameter "do_padding", // which when set to false will prevent padding with "=". // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT int Base64Escape(const unsigned char* src, int slen, - char* dest, int szdest); -LIBPROTOBUF_EXPORT int WebSafeBase64Escape( - const unsigned char* src, int slen, char* dest, - int szdest, bool do_padding); +PROTOBUF_EXPORT int Base64Escape(const unsigned char* src, int slen, char* dest, + int szdest); +PROTOBUF_EXPORT int WebSafeBase64Escape(const unsigned char* src, int slen, + char* dest, int szdest, + bool do_padding); // Encode src into dest with padding. -LIBPROTOBUF_EXPORT void Base64Escape(StringPiece src, string* dest); +PROTOBUF_EXPORT void Base64Escape(StringPiece src, string* dest); // Encode src into dest web-safely without padding. -LIBPROTOBUF_EXPORT void WebSafeBase64Escape(StringPiece src, string* dest); +PROTOBUF_EXPORT void WebSafeBase64Escape(StringPiece src, string* dest); // Encode src into dest web-safely with padding. -LIBPROTOBUF_EXPORT void WebSafeBase64EscapeWithPadding(StringPiece src, - string* dest); +PROTOBUF_EXPORT void WebSafeBase64EscapeWithPadding(StringPiece src, + string* dest); -LIBPROTOBUF_EXPORT void Base64Escape(const unsigned char* src, int szsrc, - string* dest, bool do_padding); -LIBPROTOBUF_EXPORT void WebSafeBase64Escape(const unsigned char* src, int szsrc, - string* dest, bool do_padding); +PROTOBUF_EXPORT void Base64Escape(const unsigned char* src, int szsrc, + string* dest, bool do_padding); +PROTOBUF_EXPORT void WebSafeBase64Escape(const unsigned char* src, int szsrc, + string* dest, bool do_padding); inline bool IsValidCodePoint(uint32 code_point) { return code_point < 0xD800 || @@ -864,13 +874,13 @@ static const int UTFmax = 4; // in any external dependencies. The output buffer must be as least 4 bytes // large. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT int EncodeAsUTF8Char(uint32 code_point, char* output); +PROTOBUF_EXPORT int EncodeAsUTF8Char(uint32 code_point, char* output); // ---------------------------------------------------------------------- // UTF8FirstLetterNumBytes() // Length of the first UTF-8 character. // ---------------------------------------------------------------------- -LIBPROTOBUF_EXPORT int UTF8FirstLetterNumBytes(const char* src, int len); +PROTOBUF_EXPORT int UTF8FirstLetterNumBytes(const char* src, int len); // From google3/third_party/absl/strings/escaping.h @@ -906,12 +916,12 @@ LIBPROTOBUF_EXPORT int UTF8FirstLetterNumBytes(const char* src, int len); // // (1) determines the presence of LF (first one is ok) // (2) if yes, removes any CR, else convert every CR to LF -LIBPROTOBUF_EXPORT void CleanStringLineEndings(const string& src, string* dst, - bool auto_end_last_line); +PROTOBUF_EXPORT void CleanStringLineEndings(const string& src, string* dst, + bool auto_end_last_line); // Same as above, but transforms the argument in place. -LIBPROTOBUF_EXPORT void CleanStringLineEndings(string* str, - bool auto_end_last_line); +PROTOBUF_EXPORT void CleanStringLineEndings(string* str, + bool auto_end_last_line); namespace strings { inline bool EndsWith(StringPiece text, StringPiece suffix) { @@ -925,4 +935,6 @@ inline bool EndsWith(StringPiece text, StringPiece suffix) { } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ diff --git a/src/google/protobuf/stubs/strutil_unittest.cc b/src/google/protobuf/stubs/strutil_unittest.cc index 9460a66582..f830e37704 100644 --- a/src/google/protobuf/stubs/strutil_unittest.cc +++ b/src/google/protobuf/stubs/strutil_unittest.cc @@ -805,6 +805,37 @@ TEST(Base64, EscapeAndUnescape) { } } +// Test StrCat of ints and longs of various sizes and signdedness. +TEST(StrCat, Ints) { + const short s = -1; // NOLINT(runtime/int) + const uint16_t us = 2; + const int i = -3; + const unsigned int ui = 4; + const long l = -5; // NOLINT(runtime/int) + const unsigned long ul = 6; // NOLINT(runtime/int) + const long long ll = -7; // NOLINT(runtime/int) + const unsigned long long ull = 8; // NOLINT(runtime/int) + const ptrdiff_t ptrdiff = -9; + const size_t size = 10; + const intptr_t intptr = -12; + const uintptr_t uintptr = 13; + string answer; + answer = StrCat(s, us); + EXPECT_EQ(answer, "-12"); + answer = StrCat(i, ui); + EXPECT_EQ(answer, "-34"); + answer = StrCat(l, ul); + EXPECT_EQ(answer, "-56"); + answer = StrCat(ll, ull); + EXPECT_EQ(answer, "-78"); + answer = StrCat(ptrdiff, size); + EXPECT_EQ(answer, "-910"); + answer = StrCat(ptrdiff, intptr); + EXPECT_EQ(answer, "-9-12"); + answer = StrCat(uintptr, 0); + EXPECT_EQ(answer, "130"); +} + } // anonymous namespace } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/stubs/substitute.cc b/src/google/protobuf/stubs/substitute.cc index 1ac9ab45a2..a36f2f026c 100644 --- a/src/google/protobuf/stubs/substitute.cc +++ b/src/google/protobuf/stubs/substitute.cc @@ -31,6 +31,8 @@ // Author: kenton@google.com (Kenton Varda) #include + +#include #include #include diff --git a/src/google/protobuf/stubs/substitute.h b/src/google/protobuf/stubs/substitute.h index e05ca8d87d..267dead236 100644 --- a/src/google/protobuf/stubs/substitute.h +++ b/src/google/protobuf/stubs/substitute.h @@ -38,6 +38,8 @@ #ifndef GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ #define GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ +#include + namespace google { namespace protobuf { namespace strings { @@ -137,34 +139,36 @@ class SubstituteArg { } // namespace internal -LIBPROTOBUF_EXPORT string Substitute( - const char* format, - const internal::SubstituteArg& arg0 = internal::SubstituteArg(), - const internal::SubstituteArg& arg1 = internal::SubstituteArg(), - const internal::SubstituteArg& arg2 = internal::SubstituteArg(), - const internal::SubstituteArg& arg3 = internal::SubstituteArg(), - const internal::SubstituteArg& arg4 = internal::SubstituteArg(), - const internal::SubstituteArg& arg5 = internal::SubstituteArg(), - const internal::SubstituteArg& arg6 = internal::SubstituteArg(), - const internal::SubstituteArg& arg7 = internal::SubstituteArg(), - const internal::SubstituteArg& arg8 = internal::SubstituteArg(), - const internal::SubstituteArg& arg9 = internal::SubstituteArg()); - -LIBPROTOBUF_EXPORT void SubstituteAndAppend( - string* output, const char* format, - const internal::SubstituteArg& arg0 = internal::SubstituteArg(), - const internal::SubstituteArg& arg1 = internal::SubstituteArg(), - const internal::SubstituteArg& arg2 = internal::SubstituteArg(), - const internal::SubstituteArg& arg3 = internal::SubstituteArg(), - const internal::SubstituteArg& arg4 = internal::SubstituteArg(), - const internal::SubstituteArg& arg5 = internal::SubstituteArg(), - const internal::SubstituteArg& arg6 = internal::SubstituteArg(), - const internal::SubstituteArg& arg7 = internal::SubstituteArg(), - const internal::SubstituteArg& arg8 = internal::SubstituteArg(), - const internal::SubstituteArg& arg9 = internal::SubstituteArg()); +PROTOBUF_EXPORT string +Substitute(const char* format, + const internal::SubstituteArg& arg0 = internal::SubstituteArg(), + const internal::SubstituteArg& arg1 = internal::SubstituteArg(), + const internal::SubstituteArg& arg2 = internal::SubstituteArg(), + const internal::SubstituteArg& arg3 = internal::SubstituteArg(), + const internal::SubstituteArg& arg4 = internal::SubstituteArg(), + const internal::SubstituteArg& arg5 = internal::SubstituteArg(), + const internal::SubstituteArg& arg6 = internal::SubstituteArg(), + const internal::SubstituteArg& arg7 = internal::SubstituteArg(), + const internal::SubstituteArg& arg8 = internal::SubstituteArg(), + const internal::SubstituteArg& arg9 = internal::SubstituteArg()); + +PROTOBUF_EXPORT void SubstituteAndAppend( + string* output, const char* format, + const internal::SubstituteArg& arg0 = internal::SubstituteArg(), + const internal::SubstituteArg& arg1 = internal::SubstituteArg(), + const internal::SubstituteArg& arg2 = internal::SubstituteArg(), + const internal::SubstituteArg& arg3 = internal::SubstituteArg(), + const internal::SubstituteArg& arg4 = internal::SubstituteArg(), + const internal::SubstituteArg& arg5 = internal::SubstituteArg(), + const internal::SubstituteArg& arg6 = internal::SubstituteArg(), + const internal::SubstituteArg& arg7 = internal::SubstituteArg(), + const internal::SubstituteArg& arg8 = internal::SubstituteArg(), + const internal::SubstituteArg& arg9 = internal::SubstituteArg()); } // namespace strings } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ diff --git a/src/google/protobuf/stubs/time.h b/src/google/protobuf/stubs/time.h index 45607ca9bb..12091de30b 100644 --- a/src/google/protobuf/stubs/time.h +++ b/src/google/protobuf/stubs/time.h @@ -32,6 +32,8 @@ #include +#include + namespace google { namespace protobuf { namespace internal { @@ -49,12 +51,12 @@ struct DateTime { // 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 LIBPROTOBUF_EXPORT SecondsToDateTime(int64 seconds, DateTime* time); +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 LIBPROTOBUF_EXPORT DateTimeToSeconds(const DateTime& time, int64* seconds); +bool PROTOBUF_EXPORT DateTimeToSeconds(const DateTime& time, int64* seconds); -void LIBPROTOBUF_EXPORT GetCurrentTime(int64* seconds, int32* nanos); +void PROTOBUF_EXPORT GetCurrentTime(int64* seconds, int32* nanos); // Formats a time string in RFC3339 fromat. // @@ -63,13 +65,16 @@ void LIBPROTOBUF_EXPORT GetCurrentTime(int64* seconds, int32* nanos); // value. // // Note that "nanos" must in the range of [0, 999999999]. -string LIBPROTOBUF_EXPORT FormatTime(int64 seconds, int32 nanos); +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 LIBPROTOBUF_EXPORT ParseTime(const string& value, int64* seconds, int32* nanos); +bool PROTOBUF_EXPORT ParseTime(const string& value, int64* seconds, + int32* nanos); } // namespace internal } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_STUBS_TIME_H_ diff --git a/src/google/protobuf/stubs/time_test.cc b/src/google/protobuf/stubs/time_test.cc index 53da948049..4dd06a64ca 100644 --- a/src/google/protobuf/stubs/time_test.cc +++ b/src/google/protobuf/stubs/time_test.cc @@ -201,7 +201,7 @@ TEST(DateTimeTest, WrongDays) { time.month = 13; ASSERT_FALSE(DateTimeToSeconds(time, &seconds)); } - + TEST(DateTimeTest, StringFormat) { DateTime start, end; start.year = 1; diff --git a/src/google/protobuf/test_messages_proto2.proto b/src/google/protobuf/test_messages_proto2.proto index 60dbfc753a..dc6aaa378a 100644 --- a/src/google/protobuf/test_messages_proto2.proto +++ b/src/google/protobuf/test_messages_proto2.proto @@ -33,6 +33,8 @@ // - conformance tests // +// LINT: ALLOW_GROUPS + syntax = "proto2"; package protobuf_test_messages.proto2; @@ -180,6 +182,9 @@ message TestAllTypesProto2 { optional int32 field_name17__ = 417; optional int32 Field_name18__ = 418; + // Reserved for unknown fields test. + reserved 1000 to 9999; + // message_set test case. message MessageSetCorrect { option message_set_wire_format = true; @@ -214,3 +219,15 @@ enum ForeignEnumProto2 { extend TestAllTypesProto2 { optional int32 extension_int32 = 120; } + +message UnknownToTestAllTypes { + optional int32 optional_int32 = 1001; + optional string optional_string = 1002; + optional ForeignMessageProto2 nested_message = 1003; + optional group OptionalGroup = 1004 { + optional int32 a = 1; + } + optional bool optional_bool = 1006; + repeated int32 repeated_int32 = 1011; +} + diff --git a/src/google/protobuf/test_messages_proto3.proto b/src/google/protobuf/test_messages_proto3.proto index 4f295aac49..908511e59a 100644 --- a/src/google/protobuf/test_messages_proto3.proto +++ b/src/google/protobuf/test_messages_proto3.proto @@ -73,6 +73,17 @@ message TestAllTypesProto3 { NEG = -1; // Intentionally negative. } + enum AliasedEnum { + option allow_alias = true; + + ALIAS_FOO = 0; + ALIAS_BAR = 1; + ALIAS_BAZ = 2; + QUX = 2; + qux = 2; + bAz = 2; + } + // Singular int32 optional_int32 = 1; int64 optional_int64 = 2; @@ -95,6 +106,7 @@ message TestAllTypesProto3 { NestedEnum optional_nested_enum = 21; ForeignEnum optional_foreign_enum = 22; + AliasedEnum optional_aliased_enum = 23; string optional_string_piece = 24 [ctype=STRING_PIECE]; string optional_cord = 25 [ctype=CORD]; @@ -194,6 +206,7 @@ message TestAllTypesProto3 { repeated google.protobuf.Struct repeated_struct = 324; repeated google.protobuf.Any repeated_any = 315; repeated google.protobuf.Value repeated_value = 316; + repeated google.protobuf.ListValue repeated_list_value = 317; // Test field-name-to-JSON-name convention. // (protobuf says names can be any valid C/C++ identifier.) diff --git a/src/google/protobuf/test_util.h b/src/google/protobuf/test_util.h index 0308eb129c..de48c1a469 100644 --- a/src/google/protobuf/test_util.h +++ b/src/google/protobuf/test_util.h @@ -44,6 +44,7 @@ #undef UNITTEST #undef UNITTEST_IMPORT +#include namespace google { namespace protobuf { @@ -68,9 +69,7 @@ class ReflectionTester { void ExpectClearViaReflection(const Message& message); void SetPackedFieldsViaReflection(Message* message); - void ModifyPackedFieldsViaReflection(Message* message); void ExpectPackedFieldsSetViaReflection(const Message& message); - void ExpectPackedClearViaReflection(const Message& message); void RemoveLastRepeatedsViaReflection(Message* message); void ReleaseLastRepeatedsViaReflection(Message* message, @@ -94,7 +93,7 @@ class ReflectionTester { static void ExpectOneofSetViaReflection(const Message& message); private: - const FieldDescriptor* F(const string& name); + const FieldDescriptor* F(const std::string& name); const Descriptor* base_descriptor_; @@ -128,10 +127,11 @@ inline TestUtil::ReflectionTester::ReflectionTester( const Descriptor* base_descriptor) : base_descriptor_(base_descriptor) { const DescriptorPool* pool = base_descriptor->file()->pool(); - string package = base_descriptor->file()->package(); + std::string package = base_descriptor->file()->package(); const FieldDescriptor* import_descriptor = pool->FindFieldByName(package + ".TestAllTypes.optional_import_message"); - string import_package = import_descriptor->message_type()->file()->package(); + std::string import_package = + import_descriptor->message_type()->file()->package(); nested_b_ = pool->FindFieldByName(package + ".TestAllTypes.NestedMessage.bb"); foreign_c_ = pool->FindFieldByName(package + ".ForeignMessage.c"); @@ -176,7 +176,7 @@ inline TestUtil::ReflectionTester::ReflectionTester( // Shorthand to get a FieldDescriptor for a field of TestAllTypes. inline const FieldDescriptor* TestUtil::ReflectionTester::F( - const string& name) { + const std::string& name) { const FieldDescriptor* result = nullptr; if (base_descriptor_->name() == "TestAllExtensions" || base_descriptor_->name() == "TestPackedExtensions") { @@ -361,7 +361,7 @@ inline void TestUtil::ReflectionTester::ExpectOneofSetViaReflection( const Message& message) { const Descriptor* descriptor = message.GetDescriptor(); const Reflection* reflection = message.GetReflection(); - string scratch; + std::string scratch; EXPECT_TRUE(reflection->HasField( message, descriptor->FindFieldByName("foo_lazy_message"))); EXPECT_TRUE( @@ -441,7 +441,7 @@ inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection( inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection1( const Message& message) { const Reflection* reflection = message.GetReflection(); - string scratch; + std::string scratch; const Message* sub_message; EXPECT_TRUE(reflection->HasField(message, F("optional_int32"))); @@ -565,7 +565,7 @@ inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection1( inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection2( const Message& message) { const Reflection* reflection = message.GetReflection(); - string scratch; + std::string scratch; const Message* sub_message; // ----------------------------------------------------------------- @@ -740,7 +740,7 @@ inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection2( inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection3( const Message& message) { const Reflection* reflection = message.GetReflection(); - string scratch; + std::string scratch; // ----------------------------------------------------------------- @@ -869,7 +869,7 @@ inline void TestUtil::ReflectionTester::ExpectPackedFieldsSetViaReflection( inline void TestUtil::ReflectionTester::ExpectClearViaReflection( const Message& message) { const Reflection* reflection = message.GetReflection(); - string scratch; + std::string scratch; const Message* sub_message; // has_blah() should initially be false for all optional fields. @@ -1056,26 +1056,6 @@ inline void TestUtil::ReflectionTester::ExpectClearViaReflection( &scratch)); } -inline void TestUtil::ReflectionTester::ExpectPackedClearViaReflection( - const Message& message) { - const Reflection* reflection = message.GetReflection(); - - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_int32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_int64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_uint32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_uint64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sint32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sint64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_fixed32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_fixed64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sfixed32"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sfixed64"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_float"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_double"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_bool"))); - EXPECT_EQ(0, reflection->FieldSize(message, F("packed_enum"))); -} - // ------------------------------------------------------------------- inline void TestUtil::ReflectionTester::ModifyRepeatedFieldsViaReflection( @@ -1126,25 +1106,6 @@ inline void TestUtil::ReflectionTester::ModifyRepeatedFieldsViaReflection( reflection->SetRepeatedString(message, F("repeated_cord"), 1, "525"); } -inline void TestUtil::ReflectionTester::ModifyPackedFieldsViaReflection( - Message* message) { - const Reflection* reflection = message->GetReflection(); - reflection->SetRepeatedInt32(message, F("packed_int32"), 1, 801); - reflection->SetRepeatedInt64(message, F("packed_int64"), 1, 802); - reflection->SetRepeatedUInt32(message, F("packed_uint32"), 1, 803); - reflection->SetRepeatedUInt64(message, F("packed_uint64"), 1, 804); - reflection->SetRepeatedInt32(message, F("packed_sint32"), 1, 805); - reflection->SetRepeatedInt64(message, F("packed_sint64"), 1, 806); - reflection->SetRepeatedUInt32(message, F("packed_fixed32"), 1, 807); - reflection->SetRepeatedUInt64(message, F("packed_fixed64"), 1, 808); - reflection->SetRepeatedInt32(message, F("packed_sfixed32"), 1, 809); - reflection->SetRepeatedInt64(message, F("packed_sfixed64"), 1, 810); - reflection->SetRepeatedFloat(message, F("packed_float"), 1, 811); - reflection->SetRepeatedDouble(message, F("packed_double"), 1, 812); - reflection->SetRepeatedBool(message, F("packed_bool"), 1, true); - reflection->SetRepeatedEnum(message, F("packed_enum"), 1, foreign_foo_); -} - inline void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection( Message* message) { const Reflection* reflection = message->GetReflection(); @@ -1268,11 +1229,10 @@ inline void TestUtil::ReflectionTester::ExpectMessagesReleasedViaReflection( // Check that the passed-in serialization is the canonical serialization we // expect for a TestFieldOrderings message filled in by // SetAllFieldsAndExtensions(). -inline void ExpectAllFieldsAndExtensionsInOrder( - const string& serialized) { +inline void ExpectAllFieldsAndExtensionsInOrder(const std::string& serialized) { // We set each field individually, serialize separately, and concatenate all // the strings in canonical order to determine the expected serialization. - string expected; + std::string expected; unittest::TestFieldOrderings message; message.set_my_int(1); // Field 1. message.AppendToString(&expected); @@ -1298,4 +1258,6 @@ inline void ExpectAllFieldsAndExtensionsInOrder( } // namespace protobuf } // namespace google +#include + #endif // GOOGLE_PROTOBUF_TEST_UTIL_H__ diff --git a/src/google/protobuf/test_util.inc b/src/google/protobuf/test_util.inc index 185f68d901..0f1b147ba4 100644 --- a/src/google/protobuf/test_util.inc +++ b/src/google/protobuf/test_util.inc @@ -41,7 +41,6 @@ #include #include #include -#include #include namespace google { @@ -70,8 +69,6 @@ inline void SetOneof2(UNITTEST::TestOneof2* message); // the add_*() accessors. inline void ModifyRepeatedFields(UNITTEST::TestAllTypes* message); inline void ModifyRepeatedExtensions(UNITTEST::TestAllExtensions* message); -inline void ModifyPackedFields(UNITTEST::TestPackedTypes* message); -inline void ModifyPackedExtensions(UNITTEST::TestPackedExtensions* message); // Check that all fields have the values that they should have after // Set*Fields() is called. @@ -91,17 +88,10 @@ inline void ExpectOneofSet2(const UNITTEST::TestOneof2& message); inline void ExpectRepeatedFieldsModified(const UNITTEST::TestAllTypes& message); inline void ExpectRepeatedExtensionsModified( const UNITTEST::TestAllExtensions& message); -inline void ExpectPackedFieldsModified( - const UNITTEST::TestPackedTypes& message); -inline void ExpectPackedExtensionsModified( - const UNITTEST::TestPackedExtensions& message); // Check that all fields have their default values. inline void ExpectClear(const UNITTEST::TestAllTypes& message); inline void ExpectExtensionsClear(const UNITTEST::TestAllExtensions& message); -inline void ExpectPackedClear(const UNITTEST::TestPackedTypes& message); -inline void ExpectPackedExtensionsClear( - const UNITTEST::TestPackedExtensions& message); inline void ExpectOneofClear(const UNITTEST::TestOneof2& message); // Check that all repeated fields have had their last elements removed. @@ -854,25 +844,6 @@ inline void TestUtil::SetUnpackedFields(UNITTEST::TestUnpackedTypes* message) { // ------------------------------------------------------------------- -inline void TestUtil::ModifyPackedFields(UNITTEST::TestPackedTypes* message) { - message->set_packed_int32(1, 801); - message->set_packed_int64(1, 802); - message->set_packed_uint32(1, 803); - message->set_packed_uint64(1, 804); - message->set_packed_sint32(1, 805); - message->set_packed_sint64(1, 806); - message->set_packed_fixed32(1, 807); - message->set_packed_fixed64(1, 808); - message->set_packed_sfixed32(1, 809); - message->set_packed_sfixed64(1, 810); - message->set_packed_float(1, 811); - message->set_packed_double(1, 812); - message->set_packed_bool(1, true); - message->set_packed_enum(1, UNITTEST::FOREIGN_FOO); -} - -// ------------------------------------------------------------------- - inline void TestUtil::ExpectPackedFieldsSet( const UNITTEST::TestPackedTypes& message) { ASSERT_EQ(2, message.packed_int32_size()); @@ -971,78 +942,6 @@ inline void TestUtil::ExpectUnpackedFieldsSet( EXPECT_EQ(UNITTEST::FOREIGN_BAZ, message.unpacked_enum(1)); } -// ------------------------------------------------------------------- - -inline void TestUtil::ExpectPackedClear( - const UNITTEST::TestPackedTypes& message) { - // Packed repeated fields are empty. - EXPECT_EQ(0, message.packed_int32_size()); - EXPECT_EQ(0, message.packed_int64_size()); - EXPECT_EQ(0, message.packed_uint32_size()); - EXPECT_EQ(0, message.packed_uint64_size()); - EXPECT_EQ(0, message.packed_sint32_size()); - EXPECT_EQ(0, message.packed_sint64_size()); - EXPECT_EQ(0, message.packed_fixed32_size()); - EXPECT_EQ(0, message.packed_fixed64_size()); - EXPECT_EQ(0, message.packed_sfixed32_size()); - EXPECT_EQ(0, message.packed_sfixed64_size()); - EXPECT_EQ(0, message.packed_float_size()); - EXPECT_EQ(0, message.packed_double_size()); - EXPECT_EQ(0, message.packed_bool_size()); - EXPECT_EQ(0, message.packed_enum_size()); -} - -// ------------------------------------------------------------------- - -inline void TestUtil::ExpectPackedFieldsModified( - const UNITTEST::TestPackedTypes& message) { - // Do the same for packed repeated fields. - ASSERT_EQ(2, message.packed_int32_size()); - ASSERT_EQ(2, message.packed_int64_size()); - ASSERT_EQ(2, message.packed_uint32_size()); - ASSERT_EQ(2, message.packed_uint64_size()); - ASSERT_EQ(2, message.packed_sint32_size()); - ASSERT_EQ(2, message.packed_sint64_size()); - ASSERT_EQ(2, message.packed_fixed32_size()); - ASSERT_EQ(2, message.packed_fixed64_size()); - ASSERT_EQ(2, message.packed_sfixed32_size()); - ASSERT_EQ(2, message.packed_sfixed64_size()); - ASSERT_EQ(2, message.packed_float_size()); - ASSERT_EQ(2, message.packed_double_size()); - ASSERT_EQ(2, message.packed_bool_size()); - ASSERT_EQ(2, message.packed_enum_size()); - - EXPECT_EQ(601, message.packed_int32(0)); - EXPECT_EQ(602, message.packed_int64(0)); - EXPECT_EQ(603, message.packed_uint32(0)); - EXPECT_EQ(604, message.packed_uint64(0)); - EXPECT_EQ(605, message.packed_sint32(0)); - EXPECT_EQ(606, message.packed_sint64(0)); - EXPECT_EQ(607, message.packed_fixed32(0)); - EXPECT_EQ(608, message.packed_fixed64(0)); - EXPECT_EQ(609, message.packed_sfixed32(0)); - EXPECT_EQ(610, message.packed_sfixed64(0)); - EXPECT_EQ(611, message.packed_float(0)); - EXPECT_EQ(612, message.packed_double(0)); - EXPECT_TRUE(message.packed_bool(0)); - EXPECT_EQ(UNITTEST::FOREIGN_BAR, message.packed_enum(0)); - // Actually verify the second (modified) elements now. - EXPECT_EQ(801, message.packed_int32(1)); - EXPECT_EQ(802, message.packed_int64(1)); - EXPECT_EQ(803, message.packed_uint32(1)); - EXPECT_EQ(804, message.packed_uint64(1)); - EXPECT_EQ(805, message.packed_sint32(1)); - EXPECT_EQ(806, message.packed_sint64(1)); - EXPECT_EQ(807, message.packed_fixed32(1)); - EXPECT_EQ(808, message.packed_fixed64(1)); - EXPECT_EQ(809, message.packed_sfixed32(1)); - EXPECT_EQ(810, message.packed_sfixed64(1)); - EXPECT_EQ(811, message.packed_float(1)); - EXPECT_EQ(812, message.packed_double(1)); - EXPECT_TRUE(message.packed_bool(1)); - EXPECT_EQ(UNITTEST::FOREIGN_FOO, message.packed_enum(1)); -} - // =================================================================== // Extensions // @@ -1545,7 +1444,7 @@ inline void TestUtil::ExpectAllExtensionsSet( inline void TestUtil::ExpectExtensionsClear( const UNITTEST::TestAllExtensions& message) { - string serialized; + std::string serialized; ASSERT_TRUE(message.SerializeToString(&serialized)); EXPECT_EQ("", serialized); EXPECT_EQ(0, message.ByteSizeLong()); @@ -1909,27 +1808,6 @@ inline void TestUtil::SetPackedExtensions( // ------------------------------------------------------------------- -inline void TestUtil::ModifyPackedExtensions( - UNITTEST::TestPackedExtensions* message) { - message->SetExtension(UNITTEST::packed_int32_extension, 1, 801); - message->SetExtension(UNITTEST::packed_int64_extension, 1, 802); - message->SetExtension(UNITTEST::packed_uint32_extension, 1, 803); - message->SetExtension(UNITTEST::packed_uint64_extension, 1, 804); - message->SetExtension(UNITTEST::packed_sint32_extension, 1, 805); - message->SetExtension(UNITTEST::packed_sint64_extension, 1, 806); - message->SetExtension(UNITTEST::packed_fixed32_extension, 1, 807); - message->SetExtension(UNITTEST::packed_fixed64_extension, 1, 808); - message->SetExtension(UNITTEST::packed_sfixed32_extension, 1, 809); - message->SetExtension(UNITTEST::packed_sfixed64_extension, 1, 810); - message->SetExtension(UNITTEST::packed_float_extension, 1, 811); - message->SetExtension(UNITTEST::packed_double_extension, 1, 812); - message->SetExtension(UNITTEST::packed_bool_extension, 1, true); - message->SetExtension(UNITTEST::packed_enum_extension, 1, - UNITTEST::FOREIGN_FOO); -} - -// ------------------------------------------------------------------- - inline void TestUtil::ExpectPackedExtensionsSet( const UNITTEST::TestPackedExtensions& message) { ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_int32_extension)); @@ -1981,78 +1859,6 @@ inline void TestUtil::ExpectPackedExtensionsSet( // ------------------------------------------------------------------- -inline void TestUtil::ExpectPackedExtensionsClear( - const UNITTEST::TestPackedExtensions& message) { - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_int32_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_int64_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_uint32_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_uint64_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_sint32_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_sint64_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_fixed32_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_fixed64_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_sfixed32_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_sfixed64_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_float_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_double_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_bool_extension)); - EXPECT_EQ(0, message.ExtensionSize(UNITTEST::packed_enum_extension)); -} - -// ------------------------------------------------------------------- - -inline void TestUtil::ExpectPackedExtensionsModified( - const UNITTEST::TestPackedExtensions& message) { - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_int32_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_int64_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_uint32_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_uint64_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_sint32_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_sint64_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_fixed32_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_fixed64_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_sfixed32_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_sfixed64_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_float_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_double_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_bool_extension)); - ASSERT_EQ(2, message.ExtensionSize(UNITTEST::packed_enum_extension)); - EXPECT_EQ(601, message.GetExtension(UNITTEST::packed_int32_extension, 0)); - EXPECT_EQ(602, message.GetExtension(UNITTEST::packed_int64_extension, 0)); - EXPECT_EQ(603, message.GetExtension(UNITTEST::packed_uint32_extension, 0)); - EXPECT_EQ(604, message.GetExtension(UNITTEST::packed_uint64_extension, 0)); - EXPECT_EQ(605, message.GetExtension(UNITTEST::packed_sint32_extension, 0)); - EXPECT_EQ(606, message.GetExtension(UNITTEST::packed_sint64_extension, 0)); - EXPECT_EQ(607, message.GetExtension(UNITTEST::packed_fixed32_extension, 0)); - EXPECT_EQ(608, message.GetExtension(UNITTEST::packed_fixed64_extension, 0)); - EXPECT_EQ(609, message.GetExtension(UNITTEST::packed_sfixed32_extension, 0)); - EXPECT_EQ(610, message.GetExtension(UNITTEST::packed_sfixed64_extension, 0)); - EXPECT_EQ(611, message.GetExtension(UNITTEST::packed_float_extension, 0)); - EXPECT_EQ(612, message.GetExtension(UNITTEST::packed_double_extension, 0)); - EXPECT_TRUE(message.GetExtension(UNITTEST::packed_bool_extension, 0)); - EXPECT_EQ(UNITTEST::FOREIGN_BAR, - message.GetExtension(UNITTEST::packed_enum_extension, 0)); - - // Actually verify the second (modified) elements now. - EXPECT_EQ(801, message.GetExtension(UNITTEST::packed_int32_extension, 1)); - EXPECT_EQ(802, message.GetExtension(UNITTEST::packed_int64_extension, 1)); - EXPECT_EQ(803, message.GetExtension(UNITTEST::packed_uint32_extension, 1)); - EXPECT_EQ(804, message.GetExtension(UNITTEST::packed_uint64_extension, 1)); - EXPECT_EQ(805, message.GetExtension(UNITTEST::packed_sint32_extension, 1)); - EXPECT_EQ(806, message.GetExtension(UNITTEST::packed_sint64_extension, 1)); - EXPECT_EQ(807, message.GetExtension(UNITTEST::packed_fixed32_extension, 1)); - EXPECT_EQ(808, message.GetExtension(UNITTEST::packed_fixed64_extension, 1)); - EXPECT_EQ(809, message.GetExtension(UNITTEST::packed_sfixed32_extension, 1)); - EXPECT_EQ(810, message.GetExtension(UNITTEST::packed_sfixed64_extension, 1)); - EXPECT_EQ(811, message.GetExtension(UNITTEST::packed_float_extension, 1)); - EXPECT_EQ(812, message.GetExtension(UNITTEST::packed_double_extension, 1)); - EXPECT_TRUE(message.GetExtension(UNITTEST::packed_bool_extension, 1)); - EXPECT_EQ(UNITTEST::FOREIGN_FOO, - message.GetExtension(UNITTEST::packed_enum_extension, 1)); -} - -// ------------------------------------------------------------------- - inline void TestUtil::ExpectUnpackedExtensionsSet( const UNITTEST::TestUnpackedExtensions& message) { ASSERT_EQ(2, message.ExtensionSize(UNITTEST::unpacked_int32_extension)); diff --git a/src/google/protobuf/test_util2.h b/src/google/protobuf/test_util2.h index b1c03772fb..d1cf2119e3 100644 --- a/src/google/protobuf/test_util2.h +++ b/src/google/protobuf/test_util2.h @@ -33,6 +33,7 @@ #include +#include #include @@ -41,10 +42,10 @@ namespace protobuf { namespace TestUtil { // Translate net/proto2/* -> google/protobuf/* -inline ::std::string TranslatePathToOpensource(const ::std::string& google3_path) { - const ::std::string prefix = "net/proto2/"; +inline std::string TranslatePathToOpensource(const std::string& google3_path) { + const std::string prefix = "net/proto2/"; GOOGLE_CHECK(google3_path.find(prefix) == 0) << google3_path; - ::std::string path = google3_path.substr(prefix.size()); + std::string path = google3_path.substr(prefix.size()); path = StringReplace(path, "internal/", "", false); path = StringReplace(path, "proto/", "", false); @@ -52,20 +53,29 @@ inline ::std::string TranslatePathToOpensource(const ::std::string& google3_path return "google/protobuf/" + path; } -inline ::std::string MaybeTranslatePath(const ::std::string& google3_path) { - string path = google3_path; +inline std::string MaybeTranslatePath(const std::string& google3_path) { + std::string path = google3_path; path = TranslatePathToOpensource(path); return path; } -inline ::std::string TestSourceDir() { +inline std::string TestSourceDir() { return google::protobuf::TestSourceDir(); } -inline ::std::string GetTestDataPath(const ::std::string& google3_path) { +inline std::string GetTestDataPath(const std::string& google3_path) { return TestSourceDir() + "/" + MaybeTranslatePath(google3_path); } +// Checks the equality of "message" and serialized proto of type "ProtoType". +// Do not directly compare two serialized protos. +template +bool EqualsToSerialized(const ProtoType& message, const std::string& data) { + ProtoType other; + other.ParsePartialFromString(data); + return util::MessageDifferencer::Equals(message, other); +} + } // namespace TestUtil } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/test_util_lite.cc b/src/google/protobuf/test_util_lite.cc index 79c5abec6d..a83d109298 100644 --- a/src/google/protobuf/test_util_lite.cc +++ b/src/google/protobuf/test_util_lite.cc @@ -42,112 +42,112 @@ namespace google { namespace protobuf { void TestUtilLite::SetAllFields(unittest::TestAllTypesLite* message) { - message->set_optional_int32 (101); - message->set_optional_int64 (102); - message->set_optional_uint32 (103); - message->set_optional_uint64 (104); - message->set_optional_sint32 (105); - message->set_optional_sint64 (106); - message->set_optional_fixed32 (107); - message->set_optional_fixed64 (108); + message->set_optional_int32(101); + message->set_optional_int64(102); + message->set_optional_uint32(103); + message->set_optional_uint64(104); + message->set_optional_sint32(105); + message->set_optional_sint64(106); + message->set_optional_fixed32(107); + message->set_optional_fixed64(108); message->set_optional_sfixed32(109); message->set_optional_sfixed64(110); - message->set_optional_float (111); - message->set_optional_double (112); - message->set_optional_bool (true); - message->set_optional_string ("115"); - message->set_optional_bytes ("116"); - - message->mutable_optionalgroup ()->set_a(117); - message->mutable_optional_nested_message ()->set_bb(118); - message->mutable_optional_foreign_message ()->set_c(119); - message->mutable_optional_import_message ()->set_d(120); + message->set_optional_float(111); + message->set_optional_double(112); + message->set_optional_bool(true); + message->set_optional_string("115"); + message->set_optional_bytes("116"); + + message->mutable_optionalgroup()->set_a(117); + message->mutable_optional_nested_message()->set_bb(118); + message->mutable_optional_foreign_message()->set_c(119); + message->mutable_optional_import_message()->set_d(120); message->mutable_optional_public_import_message()->set_e(126); - message->mutable_optional_lazy_message ()->set_bb(127); + message->mutable_optional_lazy_message()->set_bb(127); - message->set_optional_nested_enum (unittest::TestAllTypesLite::BAZ ); - message->set_optional_foreign_enum(unittest::FOREIGN_LITE_BAZ ); - message->set_optional_import_enum (unittest_import::IMPORT_LITE_BAZ); + message->set_optional_nested_enum(unittest::TestAllTypesLite::BAZ); + message->set_optional_foreign_enum(unittest::FOREIGN_LITE_BAZ); + message->set_optional_import_enum(unittest_import::IMPORT_LITE_BAZ); // ----------------------------------------------------------------- - message->add_repeated_int32 (201); - message->add_repeated_int64 (202); - message->add_repeated_uint32 (203); - message->add_repeated_uint64 (204); - message->add_repeated_sint32 (205); - message->add_repeated_sint64 (206); - message->add_repeated_fixed32 (207); - message->add_repeated_fixed64 (208); + message->add_repeated_int32(201); + message->add_repeated_int64(202); + message->add_repeated_uint32(203); + message->add_repeated_uint64(204); + message->add_repeated_sint32(205); + message->add_repeated_sint64(206); + message->add_repeated_fixed32(207); + message->add_repeated_fixed64(208); message->add_repeated_sfixed32(209); message->add_repeated_sfixed64(210); - message->add_repeated_float (211); - message->add_repeated_double (212); - message->add_repeated_bool (true); - message->add_repeated_string ("215"); - message->add_repeated_bytes ("216"); - - message->add_repeatedgroup ()->set_a(217); - message->add_repeated_nested_message ()->set_bb(218); + message->add_repeated_float(211); + message->add_repeated_double(212); + message->add_repeated_bool(true); + message->add_repeated_string("215"); + message->add_repeated_bytes("216"); + + message->add_repeatedgroup()->set_a(217); + message->add_repeated_nested_message()->set_bb(218); message->add_repeated_foreign_message()->set_c(219); - message->add_repeated_import_message ()->set_d(220); - message->add_repeated_lazy_message ()->set_bb(227); + message->add_repeated_import_message()->set_d(220); + message->add_repeated_lazy_message()->set_bb(227); - message->add_repeated_nested_enum (unittest::TestAllTypesLite::BAR ); - message->add_repeated_foreign_enum(unittest::FOREIGN_LITE_BAR ); - message->add_repeated_import_enum (unittest_import::IMPORT_LITE_BAR); + message->add_repeated_nested_enum(unittest::TestAllTypesLite::BAR); + message->add_repeated_foreign_enum(unittest::FOREIGN_LITE_BAR); + message->add_repeated_import_enum(unittest_import::IMPORT_LITE_BAR); // Add a second one of each field. - message->add_repeated_int32 (301); - message->add_repeated_int64 (302); - message->add_repeated_uint32 (303); - message->add_repeated_uint64 (304); - message->add_repeated_sint32 (305); - message->add_repeated_sint64 (306); - message->add_repeated_fixed32 (307); - message->add_repeated_fixed64 (308); + message->add_repeated_int32(301); + message->add_repeated_int64(302); + message->add_repeated_uint32(303); + message->add_repeated_uint64(304); + message->add_repeated_sint32(305); + message->add_repeated_sint64(306); + message->add_repeated_fixed32(307); + message->add_repeated_fixed64(308); message->add_repeated_sfixed32(309); message->add_repeated_sfixed64(310); - message->add_repeated_float (311); - message->add_repeated_double (312); - message->add_repeated_bool (false); - message->add_repeated_string ("315"); - message->add_repeated_bytes ("316"); - - message->add_repeatedgroup ()->set_a(317); - message->add_repeated_nested_message ()->set_bb(318); + message->add_repeated_float(311); + message->add_repeated_double(312); + message->add_repeated_bool(false); + message->add_repeated_string("315"); + message->add_repeated_bytes("316"); + + message->add_repeatedgroup()->set_a(317); + message->add_repeated_nested_message()->set_bb(318); message->add_repeated_foreign_message()->set_c(319); - message->add_repeated_import_message ()->set_d(320); - message->add_repeated_lazy_message ()->set_bb(327); + message->add_repeated_import_message()->set_d(320); + message->add_repeated_lazy_message()->set_bb(327); - message->add_repeated_nested_enum (unittest::TestAllTypesLite::BAZ ); - message->add_repeated_foreign_enum(unittest::FOREIGN_LITE_BAZ ); - message->add_repeated_import_enum (unittest_import::IMPORT_LITE_BAZ); + message->add_repeated_nested_enum(unittest::TestAllTypesLite::BAZ); + message->add_repeated_foreign_enum(unittest::FOREIGN_LITE_BAZ); + message->add_repeated_import_enum(unittest_import::IMPORT_LITE_BAZ); // ----------------------------------------------------------------- - message->set_default_int32 (401); - message->set_default_int64 (402); - message->set_default_uint32 (403); - message->set_default_uint64 (404); - message->set_default_sint32 (405); - message->set_default_sint64 (406); - message->set_default_fixed32 (407); - message->set_default_fixed64 (408); + message->set_default_int32(401); + message->set_default_int64(402); + message->set_default_uint32(403); + message->set_default_uint64(404); + message->set_default_sint32(405); + message->set_default_sint64(406); + message->set_default_fixed32(407); + message->set_default_fixed64(408); message->set_default_sfixed32(409); message->set_default_sfixed64(410); - message->set_default_float (411); - message->set_default_double (412); - message->set_default_bool (false); - message->set_default_string ("415"); - message->set_default_bytes ("416"); + message->set_default_float(411); + message->set_default_double(412); + message->set_default_bool(false); + message->set_default_string("415"); + message->set_default_bytes("416"); - message->set_default_nested_enum (unittest::TestAllTypesLite::FOO ); - message->set_default_foreign_enum(unittest::FOREIGN_LITE_FOO ); - message->set_default_import_enum (unittest_import::IMPORT_LITE_FOO); + message->set_default_nested_enum(unittest::TestAllTypesLite::FOO); + message->set_default_foreign_enum(unittest::FOREIGN_LITE_FOO); + message->set_default_import_enum(unittest_import::IMPORT_LITE_FOO); message->set_oneof_uint32(601); @@ -159,31 +159,31 @@ void TestUtilLite::SetAllFields(unittest::TestAllTypesLite* message) { // ------------------------------------------------------------------- void TestUtilLite::ModifyRepeatedFields(unittest::TestAllTypesLite* message) { - message->set_repeated_int32 (1, 501); - message->set_repeated_int64 (1, 502); - message->set_repeated_uint32 (1, 503); - message->set_repeated_uint64 (1, 504); - message->set_repeated_sint32 (1, 505); - message->set_repeated_sint64 (1, 506); - message->set_repeated_fixed32 (1, 507); - message->set_repeated_fixed64 (1, 508); + message->set_repeated_int32(1, 501); + message->set_repeated_int64(1, 502); + message->set_repeated_uint32(1, 503); + message->set_repeated_uint64(1, 504); + message->set_repeated_sint32(1, 505); + message->set_repeated_sint64(1, 506); + message->set_repeated_fixed32(1, 507); + message->set_repeated_fixed64(1, 508); message->set_repeated_sfixed32(1, 509); message->set_repeated_sfixed64(1, 510); - message->set_repeated_float (1, 511); - message->set_repeated_double (1, 512); - message->set_repeated_bool (1, true); - message->set_repeated_string (1, "515"); - message->set_repeated_bytes (1, "516"); - - message->mutable_repeatedgroup (1)->set_a(517); - message->mutable_repeated_nested_message (1)->set_bb(518); + message->set_repeated_float(1, 511); + message->set_repeated_double(1, 512); + message->set_repeated_bool(1, true); + message->set_repeated_string(1, "515"); + message->set_repeated_bytes(1, "516"); + + message->mutable_repeatedgroup(1)->set_a(517); + message->mutable_repeated_nested_message(1)->set_bb(518); message->mutable_repeated_foreign_message(1)->set_c(519); - message->mutable_repeated_import_message (1)->set_d(520); - message->mutable_repeated_lazy_message (1)->set_bb(527); + message->mutable_repeated_import_message(1)->set_d(520); + message->mutable_repeated_lazy_message(1)->set_bb(527); - message->set_repeated_nested_enum (1, unittest::TestAllTypesLite::FOO ); - message->set_repeated_foreign_enum(1, unittest::FOREIGN_LITE_FOO ); - message->set_repeated_import_enum (1, unittest_import::IMPORT_LITE_FOO); + message->set_repeated_nested_enum(1, unittest::TestAllTypesLite::FOO); + message->set_repeated_foreign_enum(1, unittest::FOREIGN_LITE_FOO); + message->set_repeated_import_enum(1, unittest_import::IMPORT_LITE_FOO); } @@ -191,199 +191,199 @@ void TestUtilLite::ModifyRepeatedFields(unittest::TestAllTypesLite* message) { void TestUtilLite::ExpectAllFieldsSet( const unittest::TestAllTypesLite& message) { - EXPECT_TRUE(message.has_optional_int32 ()); - EXPECT_TRUE(message.has_optional_int64 ()); - EXPECT_TRUE(message.has_optional_uint32 ()); - EXPECT_TRUE(message.has_optional_uint64 ()); - EXPECT_TRUE(message.has_optional_sint32 ()); - EXPECT_TRUE(message.has_optional_sint64 ()); - EXPECT_TRUE(message.has_optional_fixed32 ()); - EXPECT_TRUE(message.has_optional_fixed64 ()); + EXPECT_TRUE(message.has_optional_int32()); + EXPECT_TRUE(message.has_optional_int64()); + EXPECT_TRUE(message.has_optional_uint32()); + EXPECT_TRUE(message.has_optional_uint64()); + EXPECT_TRUE(message.has_optional_sint32()); + EXPECT_TRUE(message.has_optional_sint64()); + EXPECT_TRUE(message.has_optional_fixed32()); + EXPECT_TRUE(message.has_optional_fixed64()); EXPECT_TRUE(message.has_optional_sfixed32()); EXPECT_TRUE(message.has_optional_sfixed64()); - EXPECT_TRUE(message.has_optional_float ()); - EXPECT_TRUE(message.has_optional_double ()); - EXPECT_TRUE(message.has_optional_bool ()); - EXPECT_TRUE(message.has_optional_string ()); - EXPECT_TRUE(message.has_optional_bytes ()); - - EXPECT_TRUE(message.has_optionalgroup ()); - EXPECT_TRUE(message.has_optional_nested_message ()); - EXPECT_TRUE(message.has_optional_foreign_message ()); - EXPECT_TRUE(message.has_optional_import_message ()); + EXPECT_TRUE(message.has_optional_float()); + EXPECT_TRUE(message.has_optional_double()); + EXPECT_TRUE(message.has_optional_bool()); + EXPECT_TRUE(message.has_optional_string()); + EXPECT_TRUE(message.has_optional_bytes()); + + EXPECT_TRUE(message.has_optionalgroup()); + EXPECT_TRUE(message.has_optional_nested_message()); + EXPECT_TRUE(message.has_optional_foreign_message()); + EXPECT_TRUE(message.has_optional_import_message()); EXPECT_TRUE(message.has_optional_public_import_message()); - EXPECT_TRUE(message.has_optional_lazy_message ()); + EXPECT_TRUE(message.has_optional_lazy_message()); - EXPECT_TRUE(message.optionalgroup ().has_a()); - EXPECT_TRUE(message.optional_nested_message ().has_bb()); - EXPECT_TRUE(message.optional_foreign_message ().has_c()); - EXPECT_TRUE(message.optional_import_message ().has_d()); + EXPECT_TRUE(message.optionalgroup().has_a()); + EXPECT_TRUE(message.optional_nested_message().has_bb()); + EXPECT_TRUE(message.optional_foreign_message().has_c()); + EXPECT_TRUE(message.optional_import_message().has_d()); EXPECT_TRUE(message.optional_public_import_message().has_e()); - EXPECT_TRUE(message.optional_lazy_message ().has_bb()); + EXPECT_TRUE(message.optional_lazy_message().has_bb()); - EXPECT_TRUE(message.has_optional_nested_enum ()); + EXPECT_TRUE(message.has_optional_nested_enum()); EXPECT_TRUE(message.has_optional_foreign_enum()); - EXPECT_TRUE(message.has_optional_import_enum ()); - - - EXPECT_EQ(101 , message.optional_int32 ()); - EXPECT_EQ(102 , message.optional_int64 ()); - EXPECT_EQ(103 , message.optional_uint32 ()); - EXPECT_EQ(104 , message.optional_uint64 ()); - EXPECT_EQ(105 , message.optional_sint32 ()); - EXPECT_EQ(106 , message.optional_sint64 ()); - EXPECT_EQ(107 , message.optional_fixed32 ()); - EXPECT_EQ(108 , message.optional_fixed64 ()); - EXPECT_EQ(109 , message.optional_sfixed32()); - EXPECT_EQ(110 , message.optional_sfixed64()); - EXPECT_EQ(111 , message.optional_float ()); - EXPECT_EQ(112 , message.optional_double ()); - EXPECT_EQ(true , message.optional_bool ()); - EXPECT_EQ("115", message.optional_string ()); - EXPECT_EQ("116", message.optional_bytes ()); - - EXPECT_EQ(117, message.optionalgroup ().a()); - EXPECT_EQ(118, message.optional_nested_message ().bb()); - EXPECT_EQ(119, message.optional_foreign_message ().c()); - EXPECT_EQ(120, message.optional_import_message ().d()); + EXPECT_TRUE(message.has_optional_import_enum()); + + + EXPECT_EQ(101, message.optional_int32()); + EXPECT_EQ(102, message.optional_int64()); + EXPECT_EQ(103, message.optional_uint32()); + EXPECT_EQ(104, message.optional_uint64()); + EXPECT_EQ(105, message.optional_sint32()); + EXPECT_EQ(106, message.optional_sint64()); + EXPECT_EQ(107, message.optional_fixed32()); + EXPECT_EQ(108, message.optional_fixed64()); + EXPECT_EQ(109, message.optional_sfixed32()); + EXPECT_EQ(110, message.optional_sfixed64()); + EXPECT_EQ(111, message.optional_float()); + EXPECT_EQ(112, message.optional_double()); + EXPECT_EQ(true, message.optional_bool()); + EXPECT_EQ("115", message.optional_string()); + EXPECT_EQ("116", message.optional_bytes()); + + EXPECT_EQ(117, message.optionalgroup().a()); + EXPECT_EQ(118, message.optional_nested_message().bb()); + EXPECT_EQ(119, message.optional_foreign_message().c()); + EXPECT_EQ(120, message.optional_import_message().d()); EXPECT_EQ(126, message.optional_public_import_message().e()); - EXPECT_EQ(127, message.optional_lazy_message ().bb()); + EXPECT_EQ(127, message.optional_lazy_message().bb()); - EXPECT_EQ(unittest::TestAllTypesLite::BAZ , message.optional_nested_enum ()); - EXPECT_EQ(unittest::FOREIGN_LITE_BAZ , message.optional_foreign_enum()); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.optional_import_enum ()); + EXPECT_EQ(unittest::TestAllTypesLite::BAZ, message.optional_nested_enum()); + EXPECT_EQ(unittest::FOREIGN_LITE_BAZ, message.optional_foreign_enum()); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.optional_import_enum()); // ----------------------------------------------------------------- - ASSERT_EQ(2, message.repeated_int32_size ()); - ASSERT_EQ(2, message.repeated_int64_size ()); - ASSERT_EQ(2, message.repeated_uint32_size ()); - ASSERT_EQ(2, message.repeated_uint64_size ()); - ASSERT_EQ(2, message.repeated_sint32_size ()); - ASSERT_EQ(2, message.repeated_sint64_size ()); - ASSERT_EQ(2, message.repeated_fixed32_size ()); - ASSERT_EQ(2, message.repeated_fixed64_size ()); + ASSERT_EQ(2, message.repeated_int32_size()); + ASSERT_EQ(2, message.repeated_int64_size()); + ASSERT_EQ(2, message.repeated_uint32_size()); + ASSERT_EQ(2, message.repeated_uint64_size()); + ASSERT_EQ(2, message.repeated_sint32_size()); + ASSERT_EQ(2, message.repeated_sint64_size()); + ASSERT_EQ(2, message.repeated_fixed32_size()); + ASSERT_EQ(2, message.repeated_fixed64_size()); ASSERT_EQ(2, message.repeated_sfixed32_size()); ASSERT_EQ(2, message.repeated_sfixed64_size()); - ASSERT_EQ(2, message.repeated_float_size ()); - ASSERT_EQ(2, message.repeated_double_size ()); - ASSERT_EQ(2, message.repeated_bool_size ()); - ASSERT_EQ(2, message.repeated_string_size ()); - ASSERT_EQ(2, message.repeated_bytes_size ()); - - ASSERT_EQ(2, message.repeatedgroup_size ()); - ASSERT_EQ(2, message.repeated_nested_message_size ()); + ASSERT_EQ(2, message.repeated_float_size()); + ASSERT_EQ(2, message.repeated_double_size()); + ASSERT_EQ(2, message.repeated_bool_size()); + ASSERT_EQ(2, message.repeated_string_size()); + ASSERT_EQ(2, message.repeated_bytes_size()); + + ASSERT_EQ(2, message.repeatedgroup_size()); + ASSERT_EQ(2, message.repeated_nested_message_size()); ASSERT_EQ(2, message.repeated_foreign_message_size()); - ASSERT_EQ(2, message.repeated_import_message_size ()); - ASSERT_EQ(2, message.repeated_lazy_message_size ()); - ASSERT_EQ(2, message.repeated_nested_enum_size ()); - ASSERT_EQ(2, message.repeated_foreign_enum_size ()); - ASSERT_EQ(2, message.repeated_import_enum_size ()); - - - EXPECT_EQ(201 , message.repeated_int32 (0)); - EXPECT_EQ(202 , message.repeated_int64 (0)); - EXPECT_EQ(203 , message.repeated_uint32 (0)); - EXPECT_EQ(204 , message.repeated_uint64 (0)); - EXPECT_EQ(205 , message.repeated_sint32 (0)); - EXPECT_EQ(206 , message.repeated_sint64 (0)); - EXPECT_EQ(207 , message.repeated_fixed32 (0)); - EXPECT_EQ(208 , message.repeated_fixed64 (0)); - EXPECT_EQ(209 , message.repeated_sfixed32(0)); - EXPECT_EQ(210 , message.repeated_sfixed64(0)); - EXPECT_EQ(211 , message.repeated_float (0)); - EXPECT_EQ(212 , message.repeated_double (0)); - EXPECT_EQ(true , message.repeated_bool (0)); - EXPECT_EQ("215", message.repeated_string (0)); - EXPECT_EQ("216", message.repeated_bytes (0)); - - EXPECT_EQ(217, message.repeatedgroup (0).a()); - EXPECT_EQ(218, message.repeated_nested_message (0).bb()); + ASSERT_EQ(2, message.repeated_import_message_size()); + ASSERT_EQ(2, message.repeated_lazy_message_size()); + ASSERT_EQ(2, message.repeated_nested_enum_size()); + ASSERT_EQ(2, message.repeated_foreign_enum_size()); + ASSERT_EQ(2, message.repeated_import_enum_size()); + + + EXPECT_EQ(201, message.repeated_int32(0)); + EXPECT_EQ(202, message.repeated_int64(0)); + EXPECT_EQ(203, message.repeated_uint32(0)); + EXPECT_EQ(204, message.repeated_uint64(0)); + EXPECT_EQ(205, message.repeated_sint32(0)); + EXPECT_EQ(206, message.repeated_sint64(0)); + EXPECT_EQ(207, message.repeated_fixed32(0)); + EXPECT_EQ(208, message.repeated_fixed64(0)); + EXPECT_EQ(209, message.repeated_sfixed32(0)); + EXPECT_EQ(210, message.repeated_sfixed64(0)); + EXPECT_EQ(211, message.repeated_float(0)); + EXPECT_EQ(212, message.repeated_double(0)); + EXPECT_EQ(true, message.repeated_bool(0)); + EXPECT_EQ("215", message.repeated_string(0)); + EXPECT_EQ("216", message.repeated_bytes(0)); + + EXPECT_EQ(217, message.repeatedgroup(0).a()); + EXPECT_EQ(218, message.repeated_nested_message(0).bb()); EXPECT_EQ(219, message.repeated_foreign_message(0).c()); - EXPECT_EQ(220, message.repeated_import_message (0).d()); - EXPECT_EQ(227, message.repeated_lazy_message (0).bb()); - - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.repeated_nested_enum (0)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.repeated_foreign_enum(0)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.repeated_import_enum (0)); - - EXPECT_EQ(301 , message.repeated_int32 (1)); - EXPECT_EQ(302 , message.repeated_int64 (1)); - EXPECT_EQ(303 , message.repeated_uint32 (1)); - EXPECT_EQ(304 , message.repeated_uint64 (1)); - EXPECT_EQ(305 , message.repeated_sint32 (1)); - EXPECT_EQ(306 , message.repeated_sint64 (1)); - EXPECT_EQ(307 , message.repeated_fixed32 (1)); - EXPECT_EQ(308 , message.repeated_fixed64 (1)); - EXPECT_EQ(309 , message.repeated_sfixed32(1)); - EXPECT_EQ(310 , message.repeated_sfixed64(1)); - EXPECT_EQ(311 , message.repeated_float (1)); - EXPECT_EQ(312 , message.repeated_double (1)); - EXPECT_EQ(false, message.repeated_bool (1)); - EXPECT_EQ("315", message.repeated_string (1)); - EXPECT_EQ("316", message.repeated_bytes (1)); - - EXPECT_EQ(317, message.repeatedgroup (1).a()); - EXPECT_EQ(318, message.repeated_nested_message (1).bb()); + EXPECT_EQ(220, message.repeated_import_message(0).d()); + EXPECT_EQ(227, message.repeated_lazy_message(0).bb()); + + + EXPECT_EQ(unittest::TestAllTypesLite::BAR, message.repeated_nested_enum(0)); + EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.repeated_foreign_enum(0)); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.repeated_import_enum(0)); + + EXPECT_EQ(301, message.repeated_int32(1)); + EXPECT_EQ(302, message.repeated_int64(1)); + EXPECT_EQ(303, message.repeated_uint32(1)); + EXPECT_EQ(304, message.repeated_uint64(1)); + EXPECT_EQ(305, message.repeated_sint32(1)); + EXPECT_EQ(306, message.repeated_sint64(1)); + EXPECT_EQ(307, message.repeated_fixed32(1)); + EXPECT_EQ(308, message.repeated_fixed64(1)); + EXPECT_EQ(309, message.repeated_sfixed32(1)); + EXPECT_EQ(310, message.repeated_sfixed64(1)); + EXPECT_EQ(311, message.repeated_float(1)); + EXPECT_EQ(312, message.repeated_double(1)); + EXPECT_EQ(false, message.repeated_bool(1)); + EXPECT_EQ("315", message.repeated_string(1)); + EXPECT_EQ("316", message.repeated_bytes(1)); + + EXPECT_EQ(317, message.repeatedgroup(1).a()); + EXPECT_EQ(318, message.repeated_nested_message(1).bb()); EXPECT_EQ(319, message.repeated_foreign_message(1).c()); - EXPECT_EQ(320, message.repeated_import_message (1).d()); - EXPECT_EQ(327, message.repeated_lazy_message (1).bb()); + EXPECT_EQ(320, message.repeated_import_message(1).d()); + EXPECT_EQ(327, message.repeated_lazy_message(1).bb()); - EXPECT_EQ(unittest::TestAllTypesLite::BAZ , message.repeated_nested_enum (1)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAZ , message.repeated_foreign_enum(1)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.repeated_import_enum (1)); + EXPECT_EQ(unittest::TestAllTypesLite::BAZ, message.repeated_nested_enum(1)); + EXPECT_EQ(unittest::FOREIGN_LITE_BAZ, message.repeated_foreign_enum(1)); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.repeated_import_enum(1)); // ----------------------------------------------------------------- - EXPECT_TRUE(message.has_default_int32 ()); - EXPECT_TRUE(message.has_default_int64 ()); - EXPECT_TRUE(message.has_default_uint32 ()); - EXPECT_TRUE(message.has_default_uint64 ()); - EXPECT_TRUE(message.has_default_sint32 ()); - EXPECT_TRUE(message.has_default_sint64 ()); - EXPECT_TRUE(message.has_default_fixed32 ()); - EXPECT_TRUE(message.has_default_fixed64 ()); + EXPECT_TRUE(message.has_default_int32()); + EXPECT_TRUE(message.has_default_int64()); + EXPECT_TRUE(message.has_default_uint32()); + EXPECT_TRUE(message.has_default_uint64()); + EXPECT_TRUE(message.has_default_sint32()); + EXPECT_TRUE(message.has_default_sint64()); + EXPECT_TRUE(message.has_default_fixed32()); + EXPECT_TRUE(message.has_default_fixed64()); EXPECT_TRUE(message.has_default_sfixed32()); EXPECT_TRUE(message.has_default_sfixed64()); - EXPECT_TRUE(message.has_default_float ()); - EXPECT_TRUE(message.has_default_double ()); - EXPECT_TRUE(message.has_default_bool ()); - EXPECT_TRUE(message.has_default_string ()); - EXPECT_TRUE(message.has_default_bytes ()); + EXPECT_TRUE(message.has_default_float()); + EXPECT_TRUE(message.has_default_double()); + EXPECT_TRUE(message.has_default_bool()); + EXPECT_TRUE(message.has_default_string()); + EXPECT_TRUE(message.has_default_bytes()); - EXPECT_TRUE(message.has_default_nested_enum ()); + EXPECT_TRUE(message.has_default_nested_enum()); EXPECT_TRUE(message.has_default_foreign_enum()); - EXPECT_TRUE(message.has_default_import_enum ()); - - - EXPECT_EQ(401 , message.default_int32 ()); - EXPECT_EQ(402 , message.default_int64 ()); - EXPECT_EQ(403 , message.default_uint32 ()); - EXPECT_EQ(404 , message.default_uint64 ()); - EXPECT_EQ(405 , message.default_sint32 ()); - EXPECT_EQ(406 , message.default_sint64 ()); - EXPECT_EQ(407 , message.default_fixed32 ()); - EXPECT_EQ(408 , message.default_fixed64 ()); - EXPECT_EQ(409 , message.default_sfixed32()); - EXPECT_EQ(410 , message.default_sfixed64()); - EXPECT_EQ(411 , message.default_float ()); - EXPECT_EQ(412 , message.default_double ()); - EXPECT_EQ(false, message.default_bool ()); - EXPECT_EQ("415", message.default_string ()); - EXPECT_EQ("416", message.default_bytes ()); - - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.default_nested_enum ()); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.default_foreign_enum()); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.default_import_enum ()); - - - EXPECT_FALSE(message.has_oneof_uint32 ()); + EXPECT_TRUE(message.has_default_import_enum()); + + + EXPECT_EQ(401, message.default_int32()); + EXPECT_EQ(402, message.default_int64()); + EXPECT_EQ(403, message.default_uint32()); + EXPECT_EQ(404, message.default_uint64()); + EXPECT_EQ(405, message.default_sint32()); + EXPECT_EQ(406, message.default_sint64()); + EXPECT_EQ(407, message.default_fixed32()); + EXPECT_EQ(408, message.default_fixed64()); + EXPECT_EQ(409, message.default_sfixed32()); + EXPECT_EQ(410, message.default_sfixed64()); + EXPECT_EQ(411, message.default_float()); + EXPECT_EQ(412, message.default_double()); + EXPECT_EQ(false, message.default_bool()); + EXPECT_EQ("415", message.default_string()); + EXPECT_EQ("416", message.default_bytes()); + + EXPECT_EQ(unittest::TestAllTypesLite::FOO, message.default_nested_enum()); + EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.default_foreign_enum()); + EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.default_import_enum()); + + + EXPECT_FALSE(message.has_oneof_uint32()); EXPECT_FALSE(message.has_oneof_nested_message()); - EXPECT_FALSE(message.has_oneof_string ()); - EXPECT_TRUE(message.has_oneof_bytes ()); + EXPECT_FALSE(message.has_oneof_string()); + EXPECT_TRUE(message.has_oneof_bytes()); EXPECT_EQ("604", message.oneof_bytes()); } @@ -392,145 +392,145 @@ void TestUtilLite::ExpectAllFieldsSet( void TestUtilLite::ExpectClear(const unittest::TestAllTypesLite& message) { // has_blah() should initially be false for all optional fields. - EXPECT_FALSE(message.has_optional_int32 ()); - EXPECT_FALSE(message.has_optional_int64 ()); - EXPECT_FALSE(message.has_optional_uint32 ()); - EXPECT_FALSE(message.has_optional_uint64 ()); - EXPECT_FALSE(message.has_optional_sint32 ()); - EXPECT_FALSE(message.has_optional_sint64 ()); - EXPECT_FALSE(message.has_optional_fixed32 ()); - EXPECT_FALSE(message.has_optional_fixed64 ()); + EXPECT_FALSE(message.has_optional_int32()); + EXPECT_FALSE(message.has_optional_int64()); + EXPECT_FALSE(message.has_optional_uint32()); + EXPECT_FALSE(message.has_optional_uint64()); + EXPECT_FALSE(message.has_optional_sint32()); + EXPECT_FALSE(message.has_optional_sint64()); + EXPECT_FALSE(message.has_optional_fixed32()); + EXPECT_FALSE(message.has_optional_fixed64()); EXPECT_FALSE(message.has_optional_sfixed32()); EXPECT_FALSE(message.has_optional_sfixed64()); - EXPECT_FALSE(message.has_optional_float ()); - EXPECT_FALSE(message.has_optional_double ()); - EXPECT_FALSE(message.has_optional_bool ()); - EXPECT_FALSE(message.has_optional_string ()); - EXPECT_FALSE(message.has_optional_bytes ()); - - EXPECT_FALSE(message.has_optionalgroup ()); - EXPECT_FALSE(message.has_optional_nested_message ()); - EXPECT_FALSE(message.has_optional_foreign_message ()); - EXPECT_FALSE(message.has_optional_import_message ()); + EXPECT_FALSE(message.has_optional_float()); + EXPECT_FALSE(message.has_optional_double()); + EXPECT_FALSE(message.has_optional_bool()); + EXPECT_FALSE(message.has_optional_string()); + EXPECT_FALSE(message.has_optional_bytes()); + + EXPECT_FALSE(message.has_optionalgroup()); + EXPECT_FALSE(message.has_optional_nested_message()); + EXPECT_FALSE(message.has_optional_foreign_message()); + EXPECT_FALSE(message.has_optional_import_message()); EXPECT_FALSE(message.has_optional_public_import_message()); - EXPECT_FALSE(message.has_optional_lazy_message ()); + EXPECT_FALSE(message.has_optional_lazy_message()); - EXPECT_FALSE(message.has_optional_nested_enum ()); + EXPECT_FALSE(message.has_optional_nested_enum()); EXPECT_FALSE(message.has_optional_foreign_enum()); - EXPECT_FALSE(message.has_optional_import_enum ()); + EXPECT_FALSE(message.has_optional_import_enum()); // Optional fields without defaults are set to zero or something like it. - EXPECT_EQ(0 , message.optional_int32 ()); - EXPECT_EQ(0 , message.optional_int64 ()); - EXPECT_EQ(0 , message.optional_uint32 ()); - EXPECT_EQ(0 , message.optional_uint64 ()); - EXPECT_EQ(0 , message.optional_sint32 ()); - EXPECT_EQ(0 , message.optional_sint64 ()); - EXPECT_EQ(0 , message.optional_fixed32 ()); - EXPECT_EQ(0 , message.optional_fixed64 ()); - EXPECT_EQ(0 , message.optional_sfixed32()); - EXPECT_EQ(0 , message.optional_sfixed64()); - EXPECT_EQ(0 , message.optional_float ()); - EXPECT_EQ(0 , message.optional_double ()); - EXPECT_EQ(false, message.optional_bool ()); - EXPECT_EQ("" , message.optional_string ()); - EXPECT_EQ("" , message.optional_bytes ()); + EXPECT_EQ(0, message.optional_int32()); + EXPECT_EQ(0, message.optional_int64()); + EXPECT_EQ(0, message.optional_uint32()); + EXPECT_EQ(0, message.optional_uint64()); + EXPECT_EQ(0, message.optional_sint32()); + EXPECT_EQ(0, message.optional_sint64()); + EXPECT_EQ(0, message.optional_fixed32()); + EXPECT_EQ(0, message.optional_fixed64()); + EXPECT_EQ(0, message.optional_sfixed32()); + EXPECT_EQ(0, message.optional_sfixed64()); + EXPECT_EQ(0, message.optional_float()); + EXPECT_EQ(0, message.optional_double()); + EXPECT_EQ(false, message.optional_bool()); + EXPECT_EQ("", message.optional_string()); + EXPECT_EQ("", message.optional_bytes()); // Embedded messages should also be clear. - EXPECT_FALSE(message.optionalgroup ().has_a()); - EXPECT_FALSE(message.optional_nested_message ().has_bb()); - EXPECT_FALSE(message.optional_foreign_message ().has_c()); - EXPECT_FALSE(message.optional_import_message ().has_d()); + EXPECT_FALSE(message.optionalgroup().has_a()); + EXPECT_FALSE(message.optional_nested_message().has_bb()); + EXPECT_FALSE(message.optional_foreign_message().has_c()); + EXPECT_FALSE(message.optional_import_message().has_d()); EXPECT_FALSE(message.optional_public_import_message().has_e()); - EXPECT_FALSE(message.optional_lazy_message ().has_bb()); + EXPECT_FALSE(message.optional_lazy_message().has_bb()); - EXPECT_EQ(0, message.optionalgroup ().a()); - EXPECT_EQ(0, message.optional_nested_message ().bb()); + EXPECT_EQ(0, message.optionalgroup().a()); + EXPECT_EQ(0, message.optional_nested_message().bb()); EXPECT_EQ(0, message.optional_foreign_message().c()); - EXPECT_EQ(0, message.optional_import_message ().d()); + EXPECT_EQ(0, message.optional_import_message().d()); // Enums without defaults are set to the first value in the enum. - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.optional_nested_enum ()); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.optional_foreign_enum()); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.optional_import_enum ()); + EXPECT_EQ(unittest::TestAllTypesLite::FOO, message.optional_nested_enum()); + EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.optional_foreign_enum()); + EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.optional_import_enum()); // Repeated fields are empty. - EXPECT_EQ(0, message.repeated_int32_size ()); - EXPECT_EQ(0, message.repeated_int64_size ()); - EXPECT_EQ(0, message.repeated_uint32_size ()); - EXPECT_EQ(0, message.repeated_uint64_size ()); - EXPECT_EQ(0, message.repeated_sint32_size ()); - EXPECT_EQ(0, message.repeated_sint64_size ()); - EXPECT_EQ(0, message.repeated_fixed32_size ()); - EXPECT_EQ(0, message.repeated_fixed64_size ()); + EXPECT_EQ(0, message.repeated_int32_size()); + EXPECT_EQ(0, message.repeated_int64_size()); + EXPECT_EQ(0, message.repeated_uint32_size()); + EXPECT_EQ(0, message.repeated_uint64_size()); + EXPECT_EQ(0, message.repeated_sint32_size()); + EXPECT_EQ(0, message.repeated_sint64_size()); + EXPECT_EQ(0, message.repeated_fixed32_size()); + EXPECT_EQ(0, message.repeated_fixed64_size()); EXPECT_EQ(0, message.repeated_sfixed32_size()); EXPECT_EQ(0, message.repeated_sfixed64_size()); - EXPECT_EQ(0, message.repeated_float_size ()); - EXPECT_EQ(0, message.repeated_double_size ()); - EXPECT_EQ(0, message.repeated_bool_size ()); - EXPECT_EQ(0, message.repeated_string_size ()); - EXPECT_EQ(0, message.repeated_bytes_size ()); - - EXPECT_EQ(0, message.repeatedgroup_size ()); - EXPECT_EQ(0, message.repeated_nested_message_size ()); + EXPECT_EQ(0, message.repeated_float_size()); + EXPECT_EQ(0, message.repeated_double_size()); + EXPECT_EQ(0, message.repeated_bool_size()); + EXPECT_EQ(0, message.repeated_string_size()); + EXPECT_EQ(0, message.repeated_bytes_size()); + + EXPECT_EQ(0, message.repeatedgroup_size()); + EXPECT_EQ(0, message.repeated_nested_message_size()); EXPECT_EQ(0, message.repeated_foreign_message_size()); - EXPECT_EQ(0, message.repeated_import_message_size ()); - EXPECT_EQ(0, message.repeated_lazy_message_size ()); - EXPECT_EQ(0, message.repeated_nested_enum_size ()); - EXPECT_EQ(0, message.repeated_foreign_enum_size ()); - EXPECT_EQ(0, message.repeated_import_enum_size ()); + EXPECT_EQ(0, message.repeated_import_message_size()); + EXPECT_EQ(0, message.repeated_lazy_message_size()); + EXPECT_EQ(0, message.repeated_nested_enum_size()); + EXPECT_EQ(0, message.repeated_foreign_enum_size()); + EXPECT_EQ(0, message.repeated_import_enum_size()); // has_blah() should also be false for all default fields. - EXPECT_FALSE(message.has_default_int32 ()); - EXPECT_FALSE(message.has_default_int64 ()); - EXPECT_FALSE(message.has_default_uint32 ()); - EXPECT_FALSE(message.has_default_uint64 ()); - EXPECT_FALSE(message.has_default_sint32 ()); - EXPECT_FALSE(message.has_default_sint64 ()); - EXPECT_FALSE(message.has_default_fixed32 ()); - EXPECT_FALSE(message.has_default_fixed64 ()); + EXPECT_FALSE(message.has_default_int32()); + EXPECT_FALSE(message.has_default_int64()); + EXPECT_FALSE(message.has_default_uint32()); + EXPECT_FALSE(message.has_default_uint64()); + EXPECT_FALSE(message.has_default_sint32()); + EXPECT_FALSE(message.has_default_sint64()); + EXPECT_FALSE(message.has_default_fixed32()); + EXPECT_FALSE(message.has_default_fixed64()); EXPECT_FALSE(message.has_default_sfixed32()); EXPECT_FALSE(message.has_default_sfixed64()); - EXPECT_FALSE(message.has_default_float ()); - EXPECT_FALSE(message.has_default_double ()); - EXPECT_FALSE(message.has_default_bool ()); - EXPECT_FALSE(message.has_default_string ()); - EXPECT_FALSE(message.has_default_bytes ()); + EXPECT_FALSE(message.has_default_float()); + EXPECT_FALSE(message.has_default_double()); + EXPECT_FALSE(message.has_default_bool()); + EXPECT_FALSE(message.has_default_string()); + EXPECT_FALSE(message.has_default_bytes()); - EXPECT_FALSE(message.has_default_nested_enum ()); + EXPECT_FALSE(message.has_default_nested_enum()); EXPECT_FALSE(message.has_default_foreign_enum()); - EXPECT_FALSE(message.has_default_import_enum ()); + EXPECT_FALSE(message.has_default_import_enum()); // Fields with defaults have their default values (duh). - EXPECT_EQ( 41 , message.default_int32 ()); - EXPECT_EQ( 42 , message.default_int64 ()); - EXPECT_EQ( 43 , message.default_uint32 ()); - EXPECT_EQ( 44 , message.default_uint64 ()); - EXPECT_EQ(-45 , message.default_sint32 ()); - EXPECT_EQ( 46 , message.default_sint64 ()); - EXPECT_EQ( 47 , message.default_fixed32 ()); - EXPECT_EQ( 48 , message.default_fixed64 ()); - EXPECT_EQ( 49 , message.default_sfixed32()); - EXPECT_EQ(-50 , message.default_sfixed64()); - EXPECT_EQ( 51.5 , message.default_float ()); - EXPECT_EQ( 52e3 , message.default_double ()); - EXPECT_EQ(true , message.default_bool ()); - EXPECT_EQ("hello", message.default_string ()); - EXPECT_EQ("world", message.default_bytes ()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.default_nested_enum ()); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.default_foreign_enum()); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.default_import_enum ()); - - - EXPECT_FALSE(message.has_oneof_uint32 ()); + EXPECT_EQ(41, message.default_int32()); + EXPECT_EQ(42, message.default_int64()); + EXPECT_EQ(43, message.default_uint32()); + EXPECT_EQ(44, message.default_uint64()); + EXPECT_EQ(-45, message.default_sint32()); + EXPECT_EQ(46, message.default_sint64()); + EXPECT_EQ(47, message.default_fixed32()); + EXPECT_EQ(48, message.default_fixed64()); + EXPECT_EQ(49, message.default_sfixed32()); + EXPECT_EQ(-50, message.default_sfixed64()); + EXPECT_EQ(51.5, message.default_float()); + EXPECT_EQ(52e3, message.default_double()); + EXPECT_EQ(true, message.default_bool()); + EXPECT_EQ("hello", message.default_string()); + EXPECT_EQ("world", message.default_bytes()); + + EXPECT_EQ(unittest::TestAllTypesLite::BAR, message.default_nested_enum()); + EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.default_foreign_enum()); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.default_import_enum()); + + + EXPECT_FALSE(message.has_oneof_uint32()); EXPECT_FALSE(message.has_oneof_nested_message()); - EXPECT_FALSE(message.has_oneof_string ()); - EXPECT_FALSE(message.has_oneof_bytes ()); + EXPECT_FALSE(message.has_oneof_string()); + EXPECT_FALSE(message.has_oneof_bytes()); } // ------------------------------------------------------------------- @@ -540,188 +540,188 @@ void TestUtilLite::ExpectRepeatedFieldsModified( // ModifyRepeatedFields only sets the second repeated element of each // field. In addition to verifying this, we also verify that the first // element and size were *not* modified. - ASSERT_EQ(2, message.repeated_int32_size ()); - ASSERT_EQ(2, message.repeated_int64_size ()); - ASSERT_EQ(2, message.repeated_uint32_size ()); - ASSERT_EQ(2, message.repeated_uint64_size ()); - ASSERT_EQ(2, message.repeated_sint32_size ()); - ASSERT_EQ(2, message.repeated_sint64_size ()); - ASSERT_EQ(2, message.repeated_fixed32_size ()); - ASSERT_EQ(2, message.repeated_fixed64_size ()); + ASSERT_EQ(2, message.repeated_int32_size()); + ASSERT_EQ(2, message.repeated_int64_size()); + ASSERT_EQ(2, message.repeated_uint32_size()); + ASSERT_EQ(2, message.repeated_uint64_size()); + ASSERT_EQ(2, message.repeated_sint32_size()); + ASSERT_EQ(2, message.repeated_sint64_size()); + ASSERT_EQ(2, message.repeated_fixed32_size()); + ASSERT_EQ(2, message.repeated_fixed64_size()); ASSERT_EQ(2, message.repeated_sfixed32_size()); ASSERT_EQ(2, message.repeated_sfixed64_size()); - ASSERT_EQ(2, message.repeated_float_size ()); - ASSERT_EQ(2, message.repeated_double_size ()); - ASSERT_EQ(2, message.repeated_bool_size ()); - ASSERT_EQ(2, message.repeated_string_size ()); - ASSERT_EQ(2, message.repeated_bytes_size ()); - - ASSERT_EQ(2, message.repeatedgroup_size ()); - ASSERT_EQ(2, message.repeated_nested_message_size ()); + ASSERT_EQ(2, message.repeated_float_size()); + ASSERT_EQ(2, message.repeated_double_size()); + ASSERT_EQ(2, message.repeated_bool_size()); + ASSERT_EQ(2, message.repeated_string_size()); + ASSERT_EQ(2, message.repeated_bytes_size()); + + ASSERT_EQ(2, message.repeatedgroup_size()); + ASSERT_EQ(2, message.repeated_nested_message_size()); ASSERT_EQ(2, message.repeated_foreign_message_size()); - ASSERT_EQ(2, message.repeated_import_message_size ()); - ASSERT_EQ(2, message.repeated_lazy_message_size ()); - ASSERT_EQ(2, message.repeated_nested_enum_size ()); - ASSERT_EQ(2, message.repeated_foreign_enum_size ()); - ASSERT_EQ(2, message.repeated_import_enum_size ()); - - - EXPECT_EQ(201 , message.repeated_int32 (0)); - EXPECT_EQ(202 , message.repeated_int64 (0)); - EXPECT_EQ(203 , message.repeated_uint32 (0)); - EXPECT_EQ(204 , message.repeated_uint64 (0)); - EXPECT_EQ(205 , message.repeated_sint32 (0)); - EXPECT_EQ(206 , message.repeated_sint64 (0)); - EXPECT_EQ(207 , message.repeated_fixed32 (0)); - EXPECT_EQ(208 , message.repeated_fixed64 (0)); - EXPECT_EQ(209 , message.repeated_sfixed32(0)); - EXPECT_EQ(210 , message.repeated_sfixed64(0)); - EXPECT_EQ(211 , message.repeated_float (0)); - EXPECT_EQ(212 , message.repeated_double (0)); - EXPECT_EQ(true , message.repeated_bool (0)); - EXPECT_EQ("215", message.repeated_string (0)); - EXPECT_EQ("216", message.repeated_bytes (0)); - - EXPECT_EQ(217, message.repeatedgroup (0).a()); - EXPECT_EQ(218, message.repeated_nested_message (0).bb()); + ASSERT_EQ(2, message.repeated_import_message_size()); + ASSERT_EQ(2, message.repeated_lazy_message_size()); + ASSERT_EQ(2, message.repeated_nested_enum_size()); + ASSERT_EQ(2, message.repeated_foreign_enum_size()); + ASSERT_EQ(2, message.repeated_import_enum_size()); + + + EXPECT_EQ(201, message.repeated_int32(0)); + EXPECT_EQ(202, message.repeated_int64(0)); + EXPECT_EQ(203, message.repeated_uint32(0)); + EXPECT_EQ(204, message.repeated_uint64(0)); + EXPECT_EQ(205, message.repeated_sint32(0)); + EXPECT_EQ(206, message.repeated_sint64(0)); + EXPECT_EQ(207, message.repeated_fixed32(0)); + EXPECT_EQ(208, message.repeated_fixed64(0)); + EXPECT_EQ(209, message.repeated_sfixed32(0)); + EXPECT_EQ(210, message.repeated_sfixed64(0)); + EXPECT_EQ(211, message.repeated_float(0)); + EXPECT_EQ(212, message.repeated_double(0)); + EXPECT_EQ(true, message.repeated_bool(0)); + EXPECT_EQ("215", message.repeated_string(0)); + EXPECT_EQ("216", message.repeated_bytes(0)); + + EXPECT_EQ(217, message.repeatedgroup(0).a()); + EXPECT_EQ(218, message.repeated_nested_message(0).bb()); EXPECT_EQ(219, message.repeated_foreign_message(0).c()); - EXPECT_EQ(220, message.repeated_import_message (0).d()); - EXPECT_EQ(227, message.repeated_lazy_message (0).bb()); + EXPECT_EQ(220, message.repeated_import_message(0).d()); + EXPECT_EQ(227, message.repeated_lazy_message(0).bb()); - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.repeated_nested_enum (0)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.repeated_foreign_enum(0)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.repeated_import_enum (0)); + EXPECT_EQ(unittest::TestAllTypesLite::BAR, message.repeated_nested_enum(0)); + EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.repeated_foreign_enum(0)); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.repeated_import_enum(0)); // Actually verify the second (modified) elements now. - EXPECT_EQ(501 , message.repeated_int32 (1)); - EXPECT_EQ(502 , message.repeated_int64 (1)); - EXPECT_EQ(503 , message.repeated_uint32 (1)); - EXPECT_EQ(504 , message.repeated_uint64 (1)); - EXPECT_EQ(505 , message.repeated_sint32 (1)); - EXPECT_EQ(506 , message.repeated_sint64 (1)); - EXPECT_EQ(507 , message.repeated_fixed32 (1)); - EXPECT_EQ(508 , message.repeated_fixed64 (1)); - EXPECT_EQ(509 , message.repeated_sfixed32(1)); - EXPECT_EQ(510 , message.repeated_sfixed64(1)); - EXPECT_EQ(511 , message.repeated_float (1)); - EXPECT_EQ(512 , message.repeated_double (1)); - EXPECT_EQ(true , message.repeated_bool (1)); - EXPECT_EQ("515", message.repeated_string (1)); - EXPECT_EQ("516", message.repeated_bytes (1)); - - EXPECT_EQ(517, message.repeatedgroup (1).a()); - EXPECT_EQ(518, message.repeated_nested_message (1).bb()); + EXPECT_EQ(501, message.repeated_int32(1)); + EXPECT_EQ(502, message.repeated_int64(1)); + EXPECT_EQ(503, message.repeated_uint32(1)); + EXPECT_EQ(504, message.repeated_uint64(1)); + EXPECT_EQ(505, message.repeated_sint32(1)); + EXPECT_EQ(506, message.repeated_sint64(1)); + EXPECT_EQ(507, message.repeated_fixed32(1)); + EXPECT_EQ(508, message.repeated_fixed64(1)); + EXPECT_EQ(509, message.repeated_sfixed32(1)); + EXPECT_EQ(510, message.repeated_sfixed64(1)); + EXPECT_EQ(511, message.repeated_float(1)); + EXPECT_EQ(512, message.repeated_double(1)); + EXPECT_EQ(true, message.repeated_bool(1)); + EXPECT_EQ("515", message.repeated_string(1)); + EXPECT_EQ("516", message.repeated_bytes(1)); + + EXPECT_EQ(517, message.repeatedgroup(1).a()); + EXPECT_EQ(518, message.repeated_nested_message(1).bb()); EXPECT_EQ(519, message.repeated_foreign_message(1).c()); - EXPECT_EQ(520, message.repeated_import_message (1).d()); - EXPECT_EQ(527, message.repeated_lazy_message (1).bb()); + EXPECT_EQ(520, message.repeated_import_message(1).d()); + EXPECT_EQ(527, message.repeated_lazy_message(1).bb()); - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.repeated_nested_enum (1)); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.repeated_foreign_enum(1)); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.repeated_import_enum (1)); + EXPECT_EQ(unittest::TestAllTypesLite::FOO, message.repeated_nested_enum(1)); + EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.repeated_foreign_enum(1)); + EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.repeated_import_enum(1)); } // ------------------------------------------------------------------- void TestUtilLite::SetPackedFields(unittest::TestPackedTypesLite* message) { - message->add_packed_int32 (601); - message->add_packed_int64 (602); - message->add_packed_uint32 (603); - message->add_packed_uint64 (604); - message->add_packed_sint32 (605); - message->add_packed_sint64 (606); - message->add_packed_fixed32 (607); - message->add_packed_fixed64 (608); + message->add_packed_int32(601); + message->add_packed_int64(602); + message->add_packed_uint32(603); + message->add_packed_uint64(604); + message->add_packed_sint32(605); + message->add_packed_sint64(606); + message->add_packed_fixed32(607); + message->add_packed_fixed64(608); message->add_packed_sfixed32(609); message->add_packed_sfixed64(610); - message->add_packed_float (611); - message->add_packed_double (612); - message->add_packed_bool (true); - message->add_packed_enum (unittest::FOREIGN_LITE_BAR); + message->add_packed_float(611); + message->add_packed_double(612); + message->add_packed_bool(true); + message->add_packed_enum(unittest::FOREIGN_LITE_BAR); // add a second one of each field - message->add_packed_int32 (701); - message->add_packed_int64 (702); - message->add_packed_uint32 (703); - message->add_packed_uint64 (704); - message->add_packed_sint32 (705); - message->add_packed_sint64 (706); - message->add_packed_fixed32 (707); - message->add_packed_fixed64 (708); + message->add_packed_int32(701); + message->add_packed_int64(702); + message->add_packed_uint32(703); + message->add_packed_uint64(704); + message->add_packed_sint32(705); + message->add_packed_sint64(706); + message->add_packed_fixed32(707); + message->add_packed_fixed64(708); message->add_packed_sfixed32(709); message->add_packed_sfixed64(710); - message->add_packed_float (711); - message->add_packed_double (712); - message->add_packed_bool (false); - message->add_packed_enum (unittest::FOREIGN_LITE_BAZ); + message->add_packed_float(711); + message->add_packed_double(712); + message->add_packed_bool(false); + message->add_packed_enum(unittest::FOREIGN_LITE_BAZ); } // ------------------------------------------------------------------- void TestUtilLite::ModifyPackedFields(unittest::TestPackedTypesLite* message) { - message->set_packed_int32 (1, 801); - message->set_packed_int64 (1, 802); - message->set_packed_uint32 (1, 803); - message->set_packed_uint64 (1, 804); - message->set_packed_sint32 (1, 805); - message->set_packed_sint64 (1, 806); - message->set_packed_fixed32 (1, 807); - message->set_packed_fixed64 (1, 808); + message->set_packed_int32(1, 801); + message->set_packed_int64(1, 802); + message->set_packed_uint32(1, 803); + message->set_packed_uint64(1, 804); + message->set_packed_sint32(1, 805); + message->set_packed_sint64(1, 806); + message->set_packed_fixed32(1, 807); + message->set_packed_fixed64(1, 808); message->set_packed_sfixed32(1, 809); message->set_packed_sfixed64(1, 810); - message->set_packed_float (1, 811); - message->set_packed_double (1, 812); - message->set_packed_bool (1, true); - message->set_packed_enum (1, unittest::FOREIGN_LITE_FOO); + message->set_packed_float(1, 811); + message->set_packed_double(1, 812); + message->set_packed_bool(1, true); + message->set_packed_enum(1, unittest::FOREIGN_LITE_FOO); } // ------------------------------------------------------------------- void TestUtilLite::ExpectPackedFieldsSet( const unittest::TestPackedTypesLite& message) { - ASSERT_EQ(2, message.packed_int32_size ()); - ASSERT_EQ(2, message.packed_int64_size ()); - ASSERT_EQ(2, message.packed_uint32_size ()); - ASSERT_EQ(2, message.packed_uint64_size ()); - ASSERT_EQ(2, message.packed_sint32_size ()); - ASSERT_EQ(2, message.packed_sint64_size ()); - ASSERT_EQ(2, message.packed_fixed32_size ()); - ASSERT_EQ(2, message.packed_fixed64_size ()); + ASSERT_EQ(2, message.packed_int32_size()); + ASSERT_EQ(2, message.packed_int64_size()); + ASSERT_EQ(2, message.packed_uint32_size()); + ASSERT_EQ(2, message.packed_uint64_size()); + ASSERT_EQ(2, message.packed_sint32_size()); + ASSERT_EQ(2, message.packed_sint64_size()); + ASSERT_EQ(2, message.packed_fixed32_size()); + ASSERT_EQ(2, message.packed_fixed64_size()); ASSERT_EQ(2, message.packed_sfixed32_size()); ASSERT_EQ(2, message.packed_sfixed64_size()); - ASSERT_EQ(2, message.packed_float_size ()); - ASSERT_EQ(2, message.packed_double_size ()); - ASSERT_EQ(2, message.packed_bool_size ()); - ASSERT_EQ(2, message.packed_enum_size ()); - - EXPECT_EQ(601 , message.packed_int32 (0)); - EXPECT_EQ(602 , message.packed_int64 (0)); - EXPECT_EQ(603 , message.packed_uint32 (0)); - EXPECT_EQ(604 , message.packed_uint64 (0)); - EXPECT_EQ(605 , message.packed_sint32 (0)); - EXPECT_EQ(606 , message.packed_sint64 (0)); - EXPECT_EQ(607 , message.packed_fixed32 (0)); - EXPECT_EQ(608 , message.packed_fixed64 (0)); - EXPECT_EQ(609 , message.packed_sfixed32(0)); - EXPECT_EQ(610 , message.packed_sfixed64(0)); - EXPECT_EQ(611 , message.packed_float (0)); - EXPECT_EQ(612 , message.packed_double (0)); - EXPECT_EQ(true , message.packed_bool (0)); + ASSERT_EQ(2, message.packed_float_size()); + ASSERT_EQ(2, message.packed_double_size()); + ASSERT_EQ(2, message.packed_bool_size()); + ASSERT_EQ(2, message.packed_enum_size()); + + EXPECT_EQ(601, message.packed_int32(0)); + EXPECT_EQ(602, message.packed_int64(0)); + EXPECT_EQ(603, message.packed_uint32(0)); + EXPECT_EQ(604, message.packed_uint64(0)); + EXPECT_EQ(605, message.packed_sint32(0)); + EXPECT_EQ(606, message.packed_sint64(0)); + EXPECT_EQ(607, message.packed_fixed32(0)); + EXPECT_EQ(608, message.packed_fixed64(0)); + EXPECT_EQ(609, message.packed_sfixed32(0)); + EXPECT_EQ(610, message.packed_sfixed64(0)); + EXPECT_EQ(611, message.packed_float(0)); + EXPECT_EQ(612, message.packed_double(0)); + EXPECT_EQ(true, message.packed_bool(0)); EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.packed_enum(0)); - EXPECT_EQ(701 , message.packed_int32 (1)); - EXPECT_EQ(702 , message.packed_int64 (1)); - EXPECT_EQ(703 , message.packed_uint32 (1)); - EXPECT_EQ(704 , message.packed_uint64 (1)); - EXPECT_EQ(705 , message.packed_sint32 (1)); - EXPECT_EQ(706 , message.packed_sint64 (1)); - EXPECT_EQ(707 , message.packed_fixed32 (1)); - EXPECT_EQ(708 , message.packed_fixed64 (1)); - EXPECT_EQ(709 , message.packed_sfixed32(1)); - EXPECT_EQ(710 , message.packed_sfixed64(1)); - EXPECT_EQ(711 , message.packed_float (1)); - EXPECT_EQ(712 , message.packed_double (1)); - EXPECT_EQ(false, message.packed_bool (1)); + EXPECT_EQ(701, message.packed_int32(1)); + EXPECT_EQ(702, message.packed_int64(1)); + EXPECT_EQ(703, message.packed_uint32(1)); + EXPECT_EQ(704, message.packed_uint64(1)); + EXPECT_EQ(705, message.packed_sint32(1)); + EXPECT_EQ(706, message.packed_sint64(1)); + EXPECT_EQ(707, message.packed_fixed32(1)); + EXPECT_EQ(708, message.packed_fixed64(1)); + EXPECT_EQ(709, message.packed_sfixed32(1)); + EXPECT_EQ(710, message.packed_sfixed64(1)); + EXPECT_EQ(711, message.packed_float(1)); + EXPECT_EQ(712, message.packed_double(1)); + EXPECT_EQ(false, message.packed_bool(1)); EXPECT_EQ(unittest::FOREIGN_LITE_BAZ, message.packed_enum(1)); } @@ -730,20 +730,20 @@ void TestUtilLite::ExpectPackedFieldsSet( void TestUtilLite::ExpectPackedClear( const unittest::TestPackedTypesLite& message) { // Packed repeated fields are empty. - EXPECT_EQ(0, message.packed_int32_size ()); - EXPECT_EQ(0, message.packed_int64_size ()); - EXPECT_EQ(0, message.packed_uint32_size ()); - EXPECT_EQ(0, message.packed_uint64_size ()); - EXPECT_EQ(0, message.packed_sint32_size ()); - EXPECT_EQ(0, message.packed_sint64_size ()); - EXPECT_EQ(0, message.packed_fixed32_size ()); - EXPECT_EQ(0, message.packed_fixed64_size ()); + EXPECT_EQ(0, message.packed_int32_size()); + EXPECT_EQ(0, message.packed_int64_size()); + EXPECT_EQ(0, message.packed_uint32_size()); + EXPECT_EQ(0, message.packed_uint64_size()); + EXPECT_EQ(0, message.packed_sint32_size()); + EXPECT_EQ(0, message.packed_sint64_size()); + EXPECT_EQ(0, message.packed_fixed32_size()); + EXPECT_EQ(0, message.packed_fixed64_size()); EXPECT_EQ(0, message.packed_sfixed32_size()); EXPECT_EQ(0, message.packed_sfixed64_size()); - EXPECT_EQ(0, message.packed_float_size ()); - EXPECT_EQ(0, message.packed_double_size ()); - EXPECT_EQ(0, message.packed_bool_size ()); - EXPECT_EQ(0, message.packed_enum_size ()); + EXPECT_EQ(0, message.packed_float_size()); + EXPECT_EQ(0, message.packed_double_size()); + EXPECT_EQ(0, message.packed_bool_size()); + EXPECT_EQ(0, message.packed_enum_size()); } // ------------------------------------------------------------------- @@ -751,49 +751,49 @@ void TestUtilLite::ExpectPackedClear( void TestUtilLite::ExpectPackedFieldsModified( const unittest::TestPackedTypesLite& message) { // Do the same for packed repeated fields. - ASSERT_EQ(2, message.packed_int32_size ()); - ASSERT_EQ(2, message.packed_int64_size ()); - ASSERT_EQ(2, message.packed_uint32_size ()); - ASSERT_EQ(2, message.packed_uint64_size ()); - ASSERT_EQ(2, message.packed_sint32_size ()); - ASSERT_EQ(2, message.packed_sint64_size ()); - ASSERT_EQ(2, message.packed_fixed32_size ()); - ASSERT_EQ(2, message.packed_fixed64_size ()); + ASSERT_EQ(2, message.packed_int32_size()); + ASSERT_EQ(2, message.packed_int64_size()); + ASSERT_EQ(2, message.packed_uint32_size()); + ASSERT_EQ(2, message.packed_uint64_size()); + ASSERT_EQ(2, message.packed_sint32_size()); + ASSERT_EQ(2, message.packed_sint64_size()); + ASSERT_EQ(2, message.packed_fixed32_size()); + ASSERT_EQ(2, message.packed_fixed64_size()); ASSERT_EQ(2, message.packed_sfixed32_size()); ASSERT_EQ(2, message.packed_sfixed64_size()); - ASSERT_EQ(2, message.packed_float_size ()); - ASSERT_EQ(2, message.packed_double_size ()); - ASSERT_EQ(2, message.packed_bool_size ()); - ASSERT_EQ(2, message.packed_enum_size ()); - - EXPECT_EQ(601 , message.packed_int32 (0)); - EXPECT_EQ(602 , message.packed_int64 (0)); - EXPECT_EQ(603 , message.packed_uint32 (0)); - EXPECT_EQ(604 , message.packed_uint64 (0)); - EXPECT_EQ(605 , message.packed_sint32 (0)); - EXPECT_EQ(606 , message.packed_sint64 (0)); - EXPECT_EQ(607 , message.packed_fixed32 (0)); - EXPECT_EQ(608 , message.packed_fixed64 (0)); - EXPECT_EQ(609 , message.packed_sfixed32(0)); - EXPECT_EQ(610 , message.packed_sfixed64(0)); - EXPECT_EQ(611 , message.packed_float (0)); - EXPECT_EQ(612 , message.packed_double (0)); - EXPECT_EQ(true , message.packed_bool (0)); + ASSERT_EQ(2, message.packed_float_size()); + ASSERT_EQ(2, message.packed_double_size()); + ASSERT_EQ(2, message.packed_bool_size()); + ASSERT_EQ(2, message.packed_enum_size()); + + EXPECT_EQ(601, message.packed_int32(0)); + EXPECT_EQ(602, message.packed_int64(0)); + EXPECT_EQ(603, message.packed_uint32(0)); + EXPECT_EQ(604, message.packed_uint64(0)); + EXPECT_EQ(605, message.packed_sint32(0)); + EXPECT_EQ(606, message.packed_sint64(0)); + EXPECT_EQ(607, message.packed_fixed32(0)); + EXPECT_EQ(608, message.packed_fixed64(0)); + EXPECT_EQ(609, message.packed_sfixed32(0)); + EXPECT_EQ(610, message.packed_sfixed64(0)); + EXPECT_EQ(611, message.packed_float(0)); + EXPECT_EQ(612, message.packed_double(0)); + EXPECT_EQ(true, message.packed_bool(0)); EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.packed_enum(0)); // Actually verify the second (modified) elements now. - EXPECT_EQ(801 , message.packed_int32 (1)); - EXPECT_EQ(802 , message.packed_int64 (1)); - EXPECT_EQ(803 , message.packed_uint32 (1)); - EXPECT_EQ(804 , message.packed_uint64 (1)); - EXPECT_EQ(805 , message.packed_sint32 (1)); - EXPECT_EQ(806 , message.packed_sint64 (1)); - EXPECT_EQ(807 , message.packed_fixed32 (1)); - EXPECT_EQ(808 , message.packed_fixed64 (1)); - EXPECT_EQ(809 , message.packed_sfixed32(1)); - EXPECT_EQ(810 , message.packed_sfixed64(1)); - EXPECT_EQ(811 , message.packed_float (1)); - EXPECT_EQ(812 , message.packed_double (1)); - EXPECT_EQ(true , message.packed_bool (1)); + EXPECT_EQ(801, message.packed_int32(1)); + EXPECT_EQ(802, message.packed_int64(1)); + EXPECT_EQ(803, message.packed_uint32(1)); + EXPECT_EQ(804, message.packed_uint64(1)); + EXPECT_EQ(805, message.packed_sint32(1)); + EXPECT_EQ(806, message.packed_sint64(1)); + EXPECT_EQ(807, message.packed_fixed32(1)); + EXPECT_EQ(808, message.packed_fixed64(1)); + EXPECT_EQ(809, message.packed_sfixed32(1)); + EXPECT_EQ(810, message.packed_sfixed64(1)); + EXPECT_EQ(811, message.packed_float(1)); + EXPECT_EQ(812, message.packed_double(1)); + EXPECT_EQ(true, message.packed_bool(1)); EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.packed_enum(1)); } @@ -806,116 +806,145 @@ void TestUtilLite::ExpectPackedFieldsModified( // I gave up on the 80-char limit here. Sorry. void TestUtilLite::SetAllExtensions(unittest::TestAllExtensionsLite* message) { - message->SetExtension(unittest::optional_int32_extension_lite , 101); - message->SetExtension(unittest::optional_int64_extension_lite , 102); - message->SetExtension(unittest::optional_uint32_extension_lite , 103); - message->SetExtension(unittest::optional_uint64_extension_lite , 104); - message->SetExtension(unittest::optional_sint32_extension_lite , 105); - message->SetExtension(unittest::optional_sint64_extension_lite , 106); - message->SetExtension(unittest::optional_fixed32_extension_lite , 107); - message->SetExtension(unittest::optional_fixed64_extension_lite , 108); + message->SetExtension(unittest::optional_int32_extension_lite, 101); + message->SetExtension(unittest::optional_int64_extension_lite, 102); + message->SetExtension(unittest::optional_uint32_extension_lite, 103); + message->SetExtension(unittest::optional_uint64_extension_lite, 104); + message->SetExtension(unittest::optional_sint32_extension_lite, 105); + message->SetExtension(unittest::optional_sint64_extension_lite, 106); + message->SetExtension(unittest::optional_fixed32_extension_lite, 107); + message->SetExtension(unittest::optional_fixed64_extension_lite, 108); message->SetExtension(unittest::optional_sfixed32_extension_lite, 109); message->SetExtension(unittest::optional_sfixed64_extension_lite, 110); - message->SetExtension(unittest::optional_float_extension_lite , 111); - message->SetExtension(unittest::optional_double_extension_lite , 112); - message->SetExtension(unittest::optional_bool_extension_lite , true); - message->SetExtension(unittest::optional_string_extension_lite , "115"); - message->SetExtension(unittest::optional_bytes_extension_lite , "116"); - - message->MutableExtension(unittest::optionalgroup_extension_lite )->set_a(117); - message->MutableExtension(unittest::optional_nested_message_extension_lite )->set_bb(118); - message->MutableExtension(unittest::optional_foreign_message_extension_lite )->set_c(119); - message->MutableExtension(unittest::optional_import_message_extension_lite )->set_d(120); - message->MutableExtension(unittest::optional_public_import_message_extension_lite)->set_e(126); - message->MutableExtension(unittest::optional_lazy_message_extension_lite )->set_bb(127); - - message->SetExtension(unittest::optional_nested_enum_extension_lite , unittest::TestAllTypesLite::BAZ ); - message->SetExtension(unittest::optional_foreign_enum_extension_lite, unittest::FOREIGN_LITE_BAZ ); - message->SetExtension(unittest::optional_import_enum_extension_lite , unittest_import::IMPORT_LITE_BAZ); + message->SetExtension(unittest::optional_float_extension_lite, 111); + message->SetExtension(unittest::optional_double_extension_lite, 112); + message->SetExtension(unittest::optional_bool_extension_lite, true); + message->SetExtension(unittest::optional_string_extension_lite, "115"); + message->SetExtension(unittest::optional_bytes_extension_lite, "116"); + + message->MutableExtension(unittest::optionalgroup_extension_lite)->set_a(117); + message->MutableExtension(unittest::optional_nested_message_extension_lite) + ->set_bb(118); + message->MutableExtension(unittest::optional_foreign_message_extension_lite) + ->set_c(119); + message->MutableExtension(unittest::optional_import_message_extension_lite) + ->set_d(120); + message + ->MutableExtension( + unittest::optional_public_import_message_extension_lite) + ->set_e(126); + message->MutableExtension(unittest::optional_lazy_message_extension_lite) + ->set_bb(127); + + message->SetExtension(unittest::optional_nested_enum_extension_lite, + unittest::TestAllTypesLite::BAZ); + message->SetExtension(unittest::optional_foreign_enum_extension_lite, + unittest::FOREIGN_LITE_BAZ); + message->SetExtension(unittest::optional_import_enum_extension_lite, + unittest_import::IMPORT_LITE_BAZ); // ----------------------------------------------------------------- - message->AddExtension(unittest::repeated_int32_extension_lite , 201); - message->AddExtension(unittest::repeated_int64_extension_lite , 202); - message->AddExtension(unittest::repeated_uint32_extension_lite , 203); - message->AddExtension(unittest::repeated_uint64_extension_lite , 204); - message->AddExtension(unittest::repeated_sint32_extension_lite , 205); - message->AddExtension(unittest::repeated_sint64_extension_lite , 206); - message->AddExtension(unittest::repeated_fixed32_extension_lite , 207); - message->AddExtension(unittest::repeated_fixed64_extension_lite , 208); + message->AddExtension(unittest::repeated_int32_extension_lite, 201); + message->AddExtension(unittest::repeated_int64_extension_lite, 202); + message->AddExtension(unittest::repeated_uint32_extension_lite, 203); + message->AddExtension(unittest::repeated_uint64_extension_lite, 204); + message->AddExtension(unittest::repeated_sint32_extension_lite, 205); + message->AddExtension(unittest::repeated_sint64_extension_lite, 206); + message->AddExtension(unittest::repeated_fixed32_extension_lite, 207); + message->AddExtension(unittest::repeated_fixed64_extension_lite, 208); message->AddExtension(unittest::repeated_sfixed32_extension_lite, 209); message->AddExtension(unittest::repeated_sfixed64_extension_lite, 210); - message->AddExtension(unittest::repeated_float_extension_lite , 211); - message->AddExtension(unittest::repeated_double_extension_lite , 212); - message->AddExtension(unittest::repeated_bool_extension_lite , true); - message->AddExtension(unittest::repeated_string_extension_lite , "215"); - message->AddExtension(unittest::repeated_bytes_extension_lite , "216"); - - message->AddExtension(unittest::repeatedgroup_extension_lite )->set_a(217); - message->AddExtension(unittest::repeated_nested_message_extension_lite )->set_bb(218); - message->AddExtension(unittest::repeated_foreign_message_extension_lite)->set_c(219); - message->AddExtension(unittest::repeated_import_message_extension_lite )->set_d(220); - message->AddExtension(unittest::repeated_lazy_message_extension_lite )->set_bb(227); - - message->AddExtension(unittest::repeated_nested_enum_extension_lite , unittest::TestAllTypesLite::BAR ); - message->AddExtension(unittest::repeated_foreign_enum_extension_lite, unittest::FOREIGN_LITE_BAR ); - message->AddExtension(unittest::repeated_import_enum_extension_lite , unittest_import::IMPORT_LITE_BAR); + message->AddExtension(unittest::repeated_float_extension_lite, 211); + message->AddExtension(unittest::repeated_double_extension_lite, 212); + message->AddExtension(unittest::repeated_bool_extension_lite, true); + message->AddExtension(unittest::repeated_string_extension_lite, "215"); + message->AddExtension(unittest::repeated_bytes_extension_lite, "216"); + + message->AddExtension(unittest::repeatedgroup_extension_lite)->set_a(217); + message->AddExtension(unittest::repeated_nested_message_extension_lite) + ->set_bb(218); + message->AddExtension(unittest::repeated_foreign_message_extension_lite) + ->set_c(219); + message->AddExtension(unittest::repeated_import_message_extension_lite) + ->set_d(220); + message->AddExtension(unittest::repeated_lazy_message_extension_lite) + ->set_bb(227); + + message->AddExtension(unittest::repeated_nested_enum_extension_lite, + unittest::TestAllTypesLite::BAR); + message->AddExtension(unittest::repeated_foreign_enum_extension_lite, + unittest::FOREIGN_LITE_BAR); + message->AddExtension(unittest::repeated_import_enum_extension_lite, + unittest_import::IMPORT_LITE_BAR); // Add a second one of each field. - message->AddExtension(unittest::repeated_int32_extension_lite , 301); - message->AddExtension(unittest::repeated_int64_extension_lite , 302); - message->AddExtension(unittest::repeated_uint32_extension_lite , 303); - message->AddExtension(unittest::repeated_uint64_extension_lite , 304); - message->AddExtension(unittest::repeated_sint32_extension_lite , 305); - message->AddExtension(unittest::repeated_sint64_extension_lite , 306); - message->AddExtension(unittest::repeated_fixed32_extension_lite , 307); - message->AddExtension(unittest::repeated_fixed64_extension_lite , 308); + message->AddExtension(unittest::repeated_int32_extension_lite, 301); + message->AddExtension(unittest::repeated_int64_extension_lite, 302); + message->AddExtension(unittest::repeated_uint32_extension_lite, 303); + message->AddExtension(unittest::repeated_uint64_extension_lite, 304); + message->AddExtension(unittest::repeated_sint32_extension_lite, 305); + message->AddExtension(unittest::repeated_sint64_extension_lite, 306); + message->AddExtension(unittest::repeated_fixed32_extension_lite, 307); + message->AddExtension(unittest::repeated_fixed64_extension_lite, 308); message->AddExtension(unittest::repeated_sfixed32_extension_lite, 309); message->AddExtension(unittest::repeated_sfixed64_extension_lite, 310); - message->AddExtension(unittest::repeated_float_extension_lite , 311); - message->AddExtension(unittest::repeated_double_extension_lite , 312); - message->AddExtension(unittest::repeated_bool_extension_lite , false); - message->AddExtension(unittest::repeated_string_extension_lite , "315"); - message->AddExtension(unittest::repeated_bytes_extension_lite , "316"); - - message->AddExtension(unittest::repeatedgroup_extension_lite )->set_a(317); - message->AddExtension(unittest::repeated_nested_message_extension_lite )->set_bb(318); - message->AddExtension(unittest::repeated_foreign_message_extension_lite)->set_c(319); - message->AddExtension(unittest::repeated_import_message_extension_lite )->set_d(320); - message->AddExtension(unittest::repeated_lazy_message_extension_lite )->set_bb(327); - - message->AddExtension(unittest::repeated_nested_enum_extension_lite , unittest::TestAllTypesLite::BAZ ); - message->AddExtension(unittest::repeated_foreign_enum_extension_lite, unittest::FOREIGN_LITE_BAZ ); - message->AddExtension(unittest::repeated_import_enum_extension_lite , unittest_import::IMPORT_LITE_BAZ); + message->AddExtension(unittest::repeated_float_extension_lite, 311); + message->AddExtension(unittest::repeated_double_extension_lite, 312); + message->AddExtension(unittest::repeated_bool_extension_lite, false); + message->AddExtension(unittest::repeated_string_extension_lite, "315"); + message->AddExtension(unittest::repeated_bytes_extension_lite, "316"); + + message->AddExtension(unittest::repeatedgroup_extension_lite)->set_a(317); + message->AddExtension(unittest::repeated_nested_message_extension_lite) + ->set_bb(318); + message->AddExtension(unittest::repeated_foreign_message_extension_lite) + ->set_c(319); + message->AddExtension(unittest::repeated_import_message_extension_lite) + ->set_d(320); + message->AddExtension(unittest::repeated_lazy_message_extension_lite) + ->set_bb(327); + + message->AddExtension(unittest::repeated_nested_enum_extension_lite, + unittest::TestAllTypesLite::BAZ); + message->AddExtension(unittest::repeated_foreign_enum_extension_lite, + unittest::FOREIGN_LITE_BAZ); + message->AddExtension(unittest::repeated_import_enum_extension_lite, + unittest_import::IMPORT_LITE_BAZ); // ----------------------------------------------------------------- - message->SetExtension(unittest::default_int32_extension_lite , 401); - message->SetExtension(unittest::default_int64_extension_lite , 402); - message->SetExtension(unittest::default_uint32_extension_lite , 403); - message->SetExtension(unittest::default_uint64_extension_lite , 404); - message->SetExtension(unittest::default_sint32_extension_lite , 405); - message->SetExtension(unittest::default_sint64_extension_lite , 406); - message->SetExtension(unittest::default_fixed32_extension_lite , 407); - message->SetExtension(unittest::default_fixed64_extension_lite , 408); + message->SetExtension(unittest::default_int32_extension_lite, 401); + message->SetExtension(unittest::default_int64_extension_lite, 402); + message->SetExtension(unittest::default_uint32_extension_lite, 403); + message->SetExtension(unittest::default_uint64_extension_lite, 404); + message->SetExtension(unittest::default_sint32_extension_lite, 405); + message->SetExtension(unittest::default_sint64_extension_lite, 406); + message->SetExtension(unittest::default_fixed32_extension_lite, 407); + message->SetExtension(unittest::default_fixed64_extension_lite, 408); message->SetExtension(unittest::default_sfixed32_extension_lite, 409); message->SetExtension(unittest::default_sfixed64_extension_lite, 410); - message->SetExtension(unittest::default_float_extension_lite , 411); - message->SetExtension(unittest::default_double_extension_lite , 412); - message->SetExtension(unittest::default_bool_extension_lite , false); - message->SetExtension(unittest::default_string_extension_lite , "415"); - message->SetExtension(unittest::default_bytes_extension_lite , "416"); - - message->SetExtension(unittest::default_nested_enum_extension_lite , unittest::TestAllTypesLite::FOO ); - message->SetExtension(unittest::default_foreign_enum_extension_lite, unittest::FOREIGN_LITE_FOO ); - message->SetExtension(unittest::default_import_enum_extension_lite , unittest_import::IMPORT_LITE_FOO); + message->SetExtension(unittest::default_float_extension_lite, 411); + message->SetExtension(unittest::default_double_extension_lite, 412); + message->SetExtension(unittest::default_bool_extension_lite, false); + message->SetExtension(unittest::default_string_extension_lite, "415"); + message->SetExtension(unittest::default_bytes_extension_lite, "416"); + + message->SetExtension(unittest::default_nested_enum_extension_lite, + unittest::TestAllTypesLite::FOO); + message->SetExtension(unittest::default_foreign_enum_extension_lite, + unittest::FOREIGN_LITE_FOO); + message->SetExtension(unittest::default_import_enum_extension_lite, + unittest_import::IMPORT_LITE_FOO); message->SetExtension(unittest::oneof_uint32_extension_lite, 601); - message->MutableExtension(unittest::oneof_nested_message_extension_lite)->set_bb(602);; + message->MutableExtension(unittest::oneof_nested_message_extension_lite) + ->set_bb(602); + ; message->SetExtension(unittest::oneof_string_extension_lite, "603"); message->SetExtension(unittest::oneof_bytes_extension_lite, "604"); } @@ -924,31 +953,40 @@ void TestUtilLite::SetAllExtensions(unittest::TestAllExtensionsLite* message) { void TestUtilLite::ModifyRepeatedExtensions( unittest::TestAllExtensionsLite* message) { - message->SetExtension(unittest::repeated_int32_extension_lite , 1, 501); - message->SetExtension(unittest::repeated_int64_extension_lite , 1, 502); - message->SetExtension(unittest::repeated_uint32_extension_lite , 1, 503); - message->SetExtension(unittest::repeated_uint64_extension_lite , 1, 504); - message->SetExtension(unittest::repeated_sint32_extension_lite , 1, 505); - message->SetExtension(unittest::repeated_sint64_extension_lite , 1, 506); - message->SetExtension(unittest::repeated_fixed32_extension_lite , 1, 507); - message->SetExtension(unittest::repeated_fixed64_extension_lite , 1, 508); + message->SetExtension(unittest::repeated_int32_extension_lite, 1, 501); + message->SetExtension(unittest::repeated_int64_extension_lite, 1, 502); + message->SetExtension(unittest::repeated_uint32_extension_lite, 1, 503); + message->SetExtension(unittest::repeated_uint64_extension_lite, 1, 504); + message->SetExtension(unittest::repeated_sint32_extension_lite, 1, 505); + message->SetExtension(unittest::repeated_sint64_extension_lite, 1, 506); + message->SetExtension(unittest::repeated_fixed32_extension_lite, 1, 507); + message->SetExtension(unittest::repeated_fixed64_extension_lite, 1, 508); message->SetExtension(unittest::repeated_sfixed32_extension_lite, 1, 509); message->SetExtension(unittest::repeated_sfixed64_extension_lite, 1, 510); - message->SetExtension(unittest::repeated_float_extension_lite , 1, 511); - message->SetExtension(unittest::repeated_double_extension_lite , 1, 512); - message->SetExtension(unittest::repeated_bool_extension_lite , 1, true); - message->SetExtension(unittest::repeated_string_extension_lite , 1, "515"); - message->SetExtension(unittest::repeated_bytes_extension_lite , 1, "516"); - - message->MutableExtension(unittest::repeatedgroup_extension_lite , 1)->set_a(517); - message->MutableExtension(unittest::repeated_nested_message_extension_lite , 1)->set_bb(518); - message->MutableExtension(unittest::repeated_foreign_message_extension_lite, 1)->set_c(519); - message->MutableExtension(unittest::repeated_import_message_extension_lite , 1)->set_d(520); - message->MutableExtension(unittest::repeated_lazy_message_extension_lite , 1)->set_bb(527); - - message->SetExtension(unittest::repeated_nested_enum_extension_lite , 1, unittest::TestAllTypesLite::FOO ); - message->SetExtension(unittest::repeated_foreign_enum_extension_lite, 1, unittest::FOREIGN_LITE_FOO ); - message->SetExtension(unittest::repeated_import_enum_extension_lite , 1, unittest_import::IMPORT_LITE_FOO); + message->SetExtension(unittest::repeated_float_extension_lite, 1, 511); + message->SetExtension(unittest::repeated_double_extension_lite, 1, 512); + message->SetExtension(unittest::repeated_bool_extension_lite, 1, true); + message->SetExtension(unittest::repeated_string_extension_lite, 1, "515"); + message->SetExtension(unittest::repeated_bytes_extension_lite, 1, "516"); + + message->MutableExtension(unittest::repeatedgroup_extension_lite, 1) + ->set_a(517); + message->MutableExtension(unittest::repeated_nested_message_extension_lite, 1) + ->set_bb(518); + message + ->MutableExtension(unittest::repeated_foreign_message_extension_lite, 1) + ->set_c(519); + message->MutableExtension(unittest::repeated_import_message_extension_lite, 1) + ->set_d(520); + message->MutableExtension(unittest::repeated_lazy_message_extension_lite, 1) + ->set_bb(527); + + message->SetExtension(unittest::repeated_nested_enum_extension_lite, 1, + unittest::TestAllTypesLite::FOO); + message->SetExtension(unittest::repeated_foreign_enum_extension_lite, 1, + unittest::FOREIGN_LITE_FOO); + message->SetExtension(unittest::repeated_import_enum_extension_lite, 1, + unittest_import::IMPORT_LITE_FOO); } @@ -956,202 +994,351 @@ void TestUtilLite::ModifyRepeatedExtensions( void TestUtilLite::ExpectAllExtensionsSet( const unittest::TestAllExtensionsLite& message) { - EXPECT_TRUE(message.HasExtension(unittest::optional_int32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_int64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_uint32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_uint64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_sint32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_sint64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_fixed32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_fixed64_extension_lite )); + EXPECT_TRUE(message.HasExtension(unittest::optional_int32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_int64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_uint32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_uint64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_sint32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_sint64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_fixed32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_fixed64_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::optional_sfixed32_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::optional_sfixed64_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::optional_float_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_double_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_bool_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_string_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_bytes_extension_lite )); - - EXPECT_TRUE(message.HasExtension(unittest::optionalgroup_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_nested_message_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_foreign_message_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_import_message_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_public_import_message_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::optional_lazy_message_extension_lite )); - - EXPECT_TRUE(message.GetExtension(unittest::optionalgroup_extension_lite ).has_a()); - EXPECT_TRUE(message.GetExtension(unittest::optional_nested_message_extension_lite ).has_bb()); - EXPECT_TRUE(message.GetExtension(unittest::optional_foreign_message_extension_lite ).has_c()); - EXPECT_TRUE(message.GetExtension(unittest::optional_import_message_extension_lite ).has_d()); - EXPECT_TRUE(message.GetExtension(unittest::optional_public_import_message_extension_lite).has_e()); - EXPECT_TRUE(message.GetExtension(unittest::optional_lazy_message_extension_lite ).has_bb()); - - EXPECT_TRUE(message.HasExtension(unittest::optional_nested_enum_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_foreign_enum_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::optional_import_enum_extension_lite )); - - - EXPECT_EQ(101 , message.GetExtension(unittest::optional_int32_extension_lite )); - EXPECT_EQ(102 , message.GetExtension(unittest::optional_int64_extension_lite )); - EXPECT_EQ(103 , message.GetExtension(unittest::optional_uint32_extension_lite )); - EXPECT_EQ(104 , message.GetExtension(unittest::optional_uint64_extension_lite )); - EXPECT_EQ(105 , message.GetExtension(unittest::optional_sint32_extension_lite )); - EXPECT_EQ(106 , message.GetExtension(unittest::optional_sint64_extension_lite )); - EXPECT_EQ(107 , message.GetExtension(unittest::optional_fixed32_extension_lite )); - EXPECT_EQ(108 , message.GetExtension(unittest::optional_fixed64_extension_lite )); - EXPECT_EQ(109 , message.GetExtension(unittest::optional_sfixed32_extension_lite)); - EXPECT_EQ(110 , message.GetExtension(unittest::optional_sfixed64_extension_lite)); - EXPECT_EQ(111 , message.GetExtension(unittest::optional_float_extension_lite )); - EXPECT_EQ(112 , message.GetExtension(unittest::optional_double_extension_lite )); - EXPECT_EQ(true , message.GetExtension(unittest::optional_bool_extension_lite )); - EXPECT_EQ("115", message.GetExtension(unittest::optional_string_extension_lite )); - EXPECT_EQ("116", message.GetExtension(unittest::optional_bytes_extension_lite )); - - EXPECT_EQ(117, message.GetExtension(unittest::optionalgroup_extension_lite ).a()); - EXPECT_EQ(118, message.GetExtension(unittest::optional_nested_message_extension_lite ).bb()); - EXPECT_EQ(119, message.GetExtension(unittest::optional_foreign_message_extension_lite ).c()); - EXPECT_EQ(120, message.GetExtension(unittest::optional_import_message_extension_lite ).d()); - EXPECT_EQ(126, message.GetExtension(unittest::optional_public_import_message_extension_lite).e()); - EXPECT_EQ(127, message.GetExtension(unittest::optional_lazy_message_extension_lite ).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAZ , message.GetExtension(unittest::optional_nested_enum_extension_lite )); - EXPECT_EQ(unittest::FOREIGN_LITE_BAZ , message.GetExtension(unittest::optional_foreign_enum_extension_lite)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.GetExtension(unittest::optional_import_enum_extension_lite )); + EXPECT_TRUE(message.HasExtension(unittest::optional_float_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_double_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_bool_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_string_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_bytes_extension_lite)); + + EXPECT_TRUE(message.HasExtension(unittest::optionalgroup_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_nested_message_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_foreign_message_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_import_message_extension_lite)); + EXPECT_TRUE(message.HasExtension( + unittest::optional_public_import_message_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_lazy_message_extension_lite)); + + EXPECT_TRUE( + message.GetExtension(unittest::optionalgroup_extension_lite).has_a()); + EXPECT_TRUE( + message.GetExtension(unittest::optional_nested_message_extension_lite) + .has_bb()); + EXPECT_TRUE( + message.GetExtension(unittest::optional_foreign_message_extension_lite) + .has_c()); + EXPECT_TRUE( + message.GetExtension(unittest::optional_import_message_extension_lite) + .has_d()); + EXPECT_TRUE( + message + .GetExtension(unittest::optional_public_import_message_extension_lite) + .has_e()); + EXPECT_TRUE( + message.GetExtension(unittest::optional_lazy_message_extension_lite) + .has_bb()); + + EXPECT_TRUE( + message.HasExtension(unittest::optional_nested_enum_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_foreign_enum_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_import_enum_extension_lite)); + + + EXPECT_EQ(101, message.GetExtension(unittest::optional_int32_extension_lite)); + EXPECT_EQ(102, message.GetExtension(unittest::optional_int64_extension_lite)); + EXPECT_EQ(103, + message.GetExtension(unittest::optional_uint32_extension_lite)); + EXPECT_EQ(104, + message.GetExtension(unittest::optional_uint64_extension_lite)); + EXPECT_EQ(105, + message.GetExtension(unittest::optional_sint32_extension_lite)); + EXPECT_EQ(106, + message.GetExtension(unittest::optional_sint64_extension_lite)); + EXPECT_EQ(107, + message.GetExtension(unittest::optional_fixed32_extension_lite)); + EXPECT_EQ(108, + message.GetExtension(unittest::optional_fixed64_extension_lite)); + EXPECT_EQ(109, + message.GetExtension(unittest::optional_sfixed32_extension_lite)); + EXPECT_EQ(110, + message.GetExtension(unittest::optional_sfixed64_extension_lite)); + EXPECT_EQ(111, message.GetExtension(unittest::optional_float_extension_lite)); + EXPECT_EQ(112, + message.GetExtension(unittest::optional_double_extension_lite)); + EXPECT_EQ(true, message.GetExtension(unittest::optional_bool_extension_lite)); + EXPECT_EQ("115", + message.GetExtension(unittest::optional_string_extension_lite)); + EXPECT_EQ("116", + message.GetExtension(unittest::optional_bytes_extension_lite)); + + EXPECT_EQ(117, + message.GetExtension(unittest::optionalgroup_extension_lite).a()); + EXPECT_EQ( + 118, + message.GetExtension(unittest::optional_nested_message_extension_lite) + .bb()); + EXPECT_EQ( + 119, + message.GetExtension(unittest::optional_foreign_message_extension_lite) + .c()); + EXPECT_EQ( + 120, + message.GetExtension(unittest::optional_import_message_extension_lite) + .d()); + EXPECT_EQ( + 126, + message + .GetExtension(unittest::optional_public_import_message_extension_lite) + .e()); + EXPECT_EQ(127, + message.GetExtension(unittest::optional_lazy_message_extension_lite) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::BAZ, + message.GetExtension(unittest::optional_nested_enum_extension_lite)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAZ, + message.GetExtension(unittest::optional_foreign_enum_extension_lite)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_BAZ, + message.GetExtension(unittest::optional_import_enum_extension_lite)); // ----------------------------------------------------------------- - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension_lite )); - - ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_message_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_enum_extension_lite )); - - - EXPECT_EQ(201 , message.GetExtension(unittest::repeated_int32_extension_lite , 0)); - EXPECT_EQ(202 , message.GetExtension(unittest::repeated_int64_extension_lite , 0)); - EXPECT_EQ(203 , message.GetExtension(unittest::repeated_uint32_extension_lite , 0)); - EXPECT_EQ(204 , message.GetExtension(unittest::repeated_uint64_extension_lite , 0)); - EXPECT_EQ(205 , message.GetExtension(unittest::repeated_sint32_extension_lite , 0)); - EXPECT_EQ(206 , message.GetExtension(unittest::repeated_sint64_extension_lite , 0)); - EXPECT_EQ(207 , message.GetExtension(unittest::repeated_fixed32_extension_lite , 0)); - EXPECT_EQ(208 , message.GetExtension(unittest::repeated_fixed64_extension_lite , 0)); - EXPECT_EQ(209 , message.GetExtension(unittest::repeated_sfixed32_extension_lite, 0)); - EXPECT_EQ(210 , message.GetExtension(unittest::repeated_sfixed64_extension_lite, 0)); - EXPECT_EQ(211 , message.GetExtension(unittest::repeated_float_extension_lite , 0)); - EXPECT_EQ(212 , message.GetExtension(unittest::repeated_double_extension_lite , 0)); - EXPECT_EQ(true , message.GetExtension(unittest::repeated_bool_extension_lite , 0)); - EXPECT_EQ("215", message.GetExtension(unittest::repeated_string_extension_lite , 0)); - EXPECT_EQ("216", message.GetExtension(unittest::repeated_bytes_extension_lite , 0)); - - EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension_lite , 0).a()); - EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension_lite , 0).bb()); - EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension_lite, 0).c()); - EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension_lite , 0).d()); - EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension_lite , 0).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.GetExtension(unittest::repeated_nested_enum_extension_lite , 0)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 0)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.GetExtension(unittest::repeated_import_enum_extension_lite , 0)); - - - EXPECT_EQ(301 , message.GetExtension(unittest::repeated_int32_extension_lite , 1)); - EXPECT_EQ(302 , message.GetExtension(unittest::repeated_int64_extension_lite , 1)); - EXPECT_EQ(303 , message.GetExtension(unittest::repeated_uint32_extension_lite , 1)); - EXPECT_EQ(304 , message.GetExtension(unittest::repeated_uint64_extension_lite , 1)); - EXPECT_EQ(305 , message.GetExtension(unittest::repeated_sint32_extension_lite , 1)); - EXPECT_EQ(306 , message.GetExtension(unittest::repeated_sint64_extension_lite , 1)); - EXPECT_EQ(307 , message.GetExtension(unittest::repeated_fixed32_extension_lite , 1)); - EXPECT_EQ(308 , message.GetExtension(unittest::repeated_fixed64_extension_lite , 1)); - EXPECT_EQ(309 , message.GetExtension(unittest::repeated_sfixed32_extension_lite, 1)); - EXPECT_EQ(310 , message.GetExtension(unittest::repeated_sfixed64_extension_lite, 1)); - EXPECT_EQ(311 , message.GetExtension(unittest::repeated_float_extension_lite , 1)); - EXPECT_EQ(312 , message.GetExtension(unittest::repeated_double_extension_lite , 1)); - EXPECT_EQ(false, message.GetExtension(unittest::repeated_bool_extension_lite , 1)); - EXPECT_EQ("315", message.GetExtension(unittest::repeated_string_extension_lite , 1)); - EXPECT_EQ("316", message.GetExtension(unittest::repeated_bytes_extension_lite , 1)); - - EXPECT_EQ(317, message.GetExtension(unittest::repeatedgroup_extension_lite , 1).a()); - EXPECT_EQ(318, message.GetExtension(unittest::repeated_nested_message_extension_lite , 1).bb()); - EXPECT_EQ(319, message.GetExtension(unittest::repeated_foreign_message_extension_lite, 1).c()); - EXPECT_EQ(320, message.GetExtension(unittest::repeated_import_message_extension_lite , 1).d()); - EXPECT_EQ(327, message.GetExtension(unittest::repeated_lazy_message_extension_lite , 1).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAZ , message.GetExtension(unittest::repeated_nested_enum_extension_lite , 1)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAZ , message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 1)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.GetExtension(unittest::repeated_import_enum_extension_lite , 1)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_fixed32_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_fixed64_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension_lite)); + + ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_nested_message_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_foreign_message_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_import_message_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_import_enum_extension_lite)); + + + EXPECT_EQ(201, + message.GetExtension(unittest::repeated_int32_extension_lite, 0)); + EXPECT_EQ(202, + message.GetExtension(unittest::repeated_int64_extension_lite, 0)); + EXPECT_EQ(203, + message.GetExtension(unittest::repeated_uint32_extension_lite, 0)); + EXPECT_EQ(204, + message.GetExtension(unittest::repeated_uint64_extension_lite, 0)); + EXPECT_EQ(205, + message.GetExtension(unittest::repeated_sint32_extension_lite, 0)); + EXPECT_EQ(206, + message.GetExtension(unittest::repeated_sint64_extension_lite, 0)); + EXPECT_EQ(207, + message.GetExtension(unittest::repeated_fixed32_extension_lite, 0)); + EXPECT_EQ(208, + message.GetExtension(unittest::repeated_fixed64_extension_lite, 0)); + EXPECT_EQ( + 209, message.GetExtension(unittest::repeated_sfixed32_extension_lite, 0)); + EXPECT_EQ( + 210, message.GetExtension(unittest::repeated_sfixed64_extension_lite, 0)); + EXPECT_EQ(211, + message.GetExtension(unittest::repeated_float_extension_lite, 0)); + EXPECT_EQ(212, + message.GetExtension(unittest::repeated_double_extension_lite, 0)); + EXPECT_EQ(true, + message.GetExtension(unittest::repeated_bool_extension_lite, 0)); + EXPECT_EQ("215", + message.GetExtension(unittest::repeated_string_extension_lite, 0)); + EXPECT_EQ("216", + message.GetExtension(unittest::repeated_bytes_extension_lite, 0)); + + EXPECT_EQ( + 217, message.GetExtension(unittest::repeatedgroup_extension_lite, 0).a()); + EXPECT_EQ( + 218, + message.GetExtension(unittest::repeated_nested_message_extension_lite, 0) + .bb()); + EXPECT_EQ( + 219, + message.GetExtension(unittest::repeated_foreign_message_extension_lite, 0) + .c()); + EXPECT_EQ( + 220, + message.GetExtension(unittest::repeated_import_message_extension_lite, 0) + .d()); + EXPECT_EQ( + 227, + message.GetExtension(unittest::repeated_lazy_message_extension_lite, 0) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::BAR, + message.GetExtension(unittest::repeated_nested_enum_extension_lite, 0)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAR, + message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 0)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_BAR, + message.GetExtension(unittest::repeated_import_enum_extension_lite, 0)); + + + EXPECT_EQ(301, + message.GetExtension(unittest::repeated_int32_extension_lite, 1)); + EXPECT_EQ(302, + message.GetExtension(unittest::repeated_int64_extension_lite, 1)); + EXPECT_EQ(303, + message.GetExtension(unittest::repeated_uint32_extension_lite, 1)); + EXPECT_EQ(304, + message.GetExtension(unittest::repeated_uint64_extension_lite, 1)); + EXPECT_EQ(305, + message.GetExtension(unittest::repeated_sint32_extension_lite, 1)); + EXPECT_EQ(306, + message.GetExtension(unittest::repeated_sint64_extension_lite, 1)); + EXPECT_EQ(307, + message.GetExtension(unittest::repeated_fixed32_extension_lite, 1)); + EXPECT_EQ(308, + message.GetExtension(unittest::repeated_fixed64_extension_lite, 1)); + EXPECT_EQ( + 309, message.GetExtension(unittest::repeated_sfixed32_extension_lite, 1)); + EXPECT_EQ( + 310, message.GetExtension(unittest::repeated_sfixed64_extension_lite, 1)); + EXPECT_EQ(311, + message.GetExtension(unittest::repeated_float_extension_lite, 1)); + EXPECT_EQ(312, + message.GetExtension(unittest::repeated_double_extension_lite, 1)); + EXPECT_EQ(false, + message.GetExtension(unittest::repeated_bool_extension_lite, 1)); + EXPECT_EQ("315", + message.GetExtension(unittest::repeated_string_extension_lite, 1)); + EXPECT_EQ("316", + message.GetExtension(unittest::repeated_bytes_extension_lite, 1)); + + EXPECT_EQ( + 317, message.GetExtension(unittest::repeatedgroup_extension_lite, 1).a()); + EXPECT_EQ( + 318, + message.GetExtension(unittest::repeated_nested_message_extension_lite, 1) + .bb()); + EXPECT_EQ( + 319, + message.GetExtension(unittest::repeated_foreign_message_extension_lite, 1) + .c()); + EXPECT_EQ( + 320, + message.GetExtension(unittest::repeated_import_message_extension_lite, 1) + .d()); + EXPECT_EQ( + 327, + message.GetExtension(unittest::repeated_lazy_message_extension_lite, 1) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::BAZ, + message.GetExtension(unittest::repeated_nested_enum_extension_lite, 1)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAZ, + message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 1)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_BAZ, + message.GetExtension(unittest::repeated_import_enum_extension_lite, 1)); // ----------------------------------------------------------------- - EXPECT_TRUE(message.HasExtension(unittest::default_int32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_int64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_uint32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_uint64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_sint32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_sint64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_fixed32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_fixed64_extension_lite )); + EXPECT_TRUE(message.HasExtension(unittest::default_int32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_int64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_uint32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_uint64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_sint32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_sint64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_fixed32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_fixed64_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::default_sfixed32_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::default_sfixed64_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::default_float_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_double_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_bool_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_string_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_bytes_extension_lite )); - - EXPECT_TRUE(message.HasExtension(unittest::default_nested_enum_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_foreign_enum_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::default_import_enum_extension_lite )); - - - EXPECT_EQ(401 , message.GetExtension(unittest::default_int32_extension_lite )); - EXPECT_EQ(402 , message.GetExtension(unittest::default_int64_extension_lite )); - EXPECT_EQ(403 , message.GetExtension(unittest::default_uint32_extension_lite )); - EXPECT_EQ(404 , message.GetExtension(unittest::default_uint64_extension_lite )); - EXPECT_EQ(405 , message.GetExtension(unittest::default_sint32_extension_lite )); - EXPECT_EQ(406 , message.GetExtension(unittest::default_sint64_extension_lite )); - EXPECT_EQ(407 , message.GetExtension(unittest::default_fixed32_extension_lite )); - EXPECT_EQ(408 , message.GetExtension(unittest::default_fixed64_extension_lite )); - EXPECT_EQ(409 , message.GetExtension(unittest::default_sfixed32_extension_lite)); - EXPECT_EQ(410 , message.GetExtension(unittest::default_sfixed64_extension_lite)); - EXPECT_EQ(411 , message.GetExtension(unittest::default_float_extension_lite )); - EXPECT_EQ(412 , message.GetExtension(unittest::default_double_extension_lite )); - EXPECT_EQ(false, message.GetExtension(unittest::default_bool_extension_lite )); - EXPECT_EQ("415", message.GetExtension(unittest::default_string_extension_lite )); - EXPECT_EQ("416", message.GetExtension(unittest::default_bytes_extension_lite )); - - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.GetExtension(unittest::default_nested_enum_extension_lite )); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.GetExtension(unittest::default_foreign_enum_extension_lite)); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.GetExtension(unittest::default_import_enum_extension_lite )); + EXPECT_TRUE(message.HasExtension(unittest::default_float_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_double_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_bool_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_string_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_bytes_extension_lite)); + + EXPECT_TRUE( + message.HasExtension(unittest::default_nested_enum_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::default_foreign_enum_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::default_import_enum_extension_lite)); + + + EXPECT_EQ(401, message.GetExtension(unittest::default_int32_extension_lite)); + EXPECT_EQ(402, message.GetExtension(unittest::default_int64_extension_lite)); + EXPECT_EQ(403, message.GetExtension(unittest::default_uint32_extension_lite)); + EXPECT_EQ(404, message.GetExtension(unittest::default_uint64_extension_lite)); + EXPECT_EQ(405, message.GetExtension(unittest::default_sint32_extension_lite)); + EXPECT_EQ(406, message.GetExtension(unittest::default_sint64_extension_lite)); + EXPECT_EQ(407, + message.GetExtension(unittest::default_fixed32_extension_lite)); + EXPECT_EQ(408, + message.GetExtension(unittest::default_fixed64_extension_lite)); + EXPECT_EQ(409, + message.GetExtension(unittest::default_sfixed32_extension_lite)); + EXPECT_EQ(410, + message.GetExtension(unittest::default_sfixed64_extension_lite)); + EXPECT_EQ(411, message.GetExtension(unittest::default_float_extension_lite)); + EXPECT_EQ(412, message.GetExtension(unittest::default_double_extension_lite)); + EXPECT_EQ(false, message.GetExtension(unittest::default_bool_extension_lite)); + EXPECT_EQ("415", + message.GetExtension(unittest::default_string_extension_lite)); + EXPECT_EQ("416", + message.GetExtension(unittest::default_bytes_extension_lite)); + + EXPECT_EQ(unittest::TestAllTypesLite::FOO, + message.GetExtension(unittest::default_nested_enum_extension_lite)); + EXPECT_EQ( + unittest::FOREIGN_LITE_FOO, + message.GetExtension(unittest::default_foreign_enum_extension_lite)); + EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, + message.GetExtension(unittest::default_import_enum_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::oneof_uint32_extension_lite)); - EXPECT_TRUE(message.GetExtension(unittest::oneof_nested_message_extension_lite).has_bb()); + EXPECT_TRUE( + message.GetExtension(unittest::oneof_nested_message_extension_lite) + .has_bb()); EXPECT_TRUE(message.HasExtension(unittest::oneof_string_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::oneof_bytes_extension_lite)); EXPECT_EQ(601, message.GetExtension(unittest::oneof_uint32_extension_lite)); - EXPECT_EQ(602, message.GetExtension(unittest::oneof_nested_message_extension_lite).bb()); + EXPECT_EQ( + 602, + message.GetExtension(unittest::oneof_nested_message_extension_lite).bb()); EXPECT_EQ("603", message.GetExtension(unittest::oneof_string_extension_lite)); EXPECT_EQ("604", message.GetExtension(unittest::oneof_bytes_extension_lite)); } @@ -1160,151 +1347,219 @@ void TestUtilLite::ExpectAllExtensionsSet( void TestUtilLite::ExpectExtensionsClear( const unittest::TestAllExtensionsLite& message) { - string serialized; + std::string serialized; ASSERT_TRUE(message.SerializeToString(&serialized)); EXPECT_EQ("", serialized); EXPECT_EQ(0, message.ByteSize()); // has_blah() should initially be false for all optional fields. - EXPECT_FALSE(message.HasExtension(unittest::optional_int32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_int64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_uint32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_uint64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_sint32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_sint64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_fixed32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_fixed64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_sfixed32_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::optional_sfixed64_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::optional_float_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_double_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_bool_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_string_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_bytes_extension_lite )); - - EXPECT_FALSE(message.HasExtension(unittest::optionalgroup_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_nested_message_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_foreign_message_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_import_message_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_public_import_message_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::optional_lazy_message_extension_lite )); - - EXPECT_FALSE(message.HasExtension(unittest::optional_nested_enum_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_foreign_enum_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::optional_import_enum_extension_lite )); + EXPECT_FALSE(message.HasExtension(unittest::optional_int32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_int64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_uint32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_uint64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_sint32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_sint64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_fixed32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_fixed64_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_sfixed32_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_sfixed64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_float_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_double_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_bool_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_string_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_bytes_extension_lite)); + + EXPECT_FALSE(message.HasExtension(unittest::optionalgroup_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_nested_message_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_message_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_import_message_extension_lite)); + EXPECT_FALSE(message.HasExtension( + unittest::optional_public_import_message_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_lazy_message_extension_lite)); + + EXPECT_FALSE( + message.HasExtension(unittest::optional_nested_enum_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_enum_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_import_enum_extension_lite)); // Optional fields without defaults are set to zero or something like it. - EXPECT_EQ(0 , message.GetExtension(unittest::optional_int32_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_int64_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_uint32_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_uint64_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_sint32_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_sint64_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_fixed32_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_fixed64_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_sfixed32_extension_lite)); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_sfixed64_extension_lite)); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_float_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_double_extension_lite )); - EXPECT_EQ(false, message.GetExtension(unittest::optional_bool_extension_lite )); - EXPECT_EQ("" , message.GetExtension(unittest::optional_string_extension_lite )); - EXPECT_EQ("" , message.GetExtension(unittest::optional_bytes_extension_lite )); + EXPECT_EQ(0, message.GetExtension(unittest::optional_int32_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_int64_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_uint32_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_uint64_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_sint32_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_sint64_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_fixed32_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_fixed64_extension_lite)); + EXPECT_EQ(0, + message.GetExtension(unittest::optional_sfixed32_extension_lite)); + EXPECT_EQ(0, + message.GetExtension(unittest::optional_sfixed64_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_float_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_double_extension_lite)); + EXPECT_EQ(false, + message.GetExtension(unittest::optional_bool_extension_lite)); + EXPECT_EQ("", message.GetExtension(unittest::optional_string_extension_lite)); + EXPECT_EQ("", message.GetExtension(unittest::optional_bytes_extension_lite)); // Embedded messages should also be clear. - EXPECT_FALSE(message.GetExtension(unittest::optionalgroup_extension_lite ).has_a()); - EXPECT_FALSE(message.GetExtension(unittest::optional_nested_message_extension_lite ).has_bb()); - EXPECT_FALSE(message.GetExtension(unittest::optional_foreign_message_extension_lite ).has_c()); - EXPECT_FALSE(message.GetExtension(unittest::optional_import_message_extension_lite ).has_d()); - EXPECT_FALSE(message.GetExtension(unittest::optional_public_import_message_extension_lite).has_e()); - EXPECT_FALSE(message.GetExtension(unittest::optional_lazy_message_extension_lite ).has_bb()); - - EXPECT_EQ(0, message.GetExtension(unittest::optionalgroup_extension_lite ).a()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_nested_message_extension_lite ).bb()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_foreign_message_extension_lite ).c()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_import_message_extension_lite ).d()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_public_import_message_extension_lite).e()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_lazy_message_extension_lite ).bb()); + EXPECT_FALSE( + message.GetExtension(unittest::optionalgroup_extension_lite).has_a()); + EXPECT_FALSE( + message.GetExtension(unittest::optional_nested_message_extension_lite) + .has_bb()); + EXPECT_FALSE( + message.GetExtension(unittest::optional_foreign_message_extension_lite) + .has_c()); + EXPECT_FALSE( + message.GetExtension(unittest::optional_import_message_extension_lite) + .has_d()); + EXPECT_FALSE( + message + .GetExtension(unittest::optional_public_import_message_extension_lite) + .has_e()); + EXPECT_FALSE( + message.GetExtension(unittest::optional_lazy_message_extension_lite) + .has_bb()); + + EXPECT_EQ(0, + message.GetExtension(unittest::optionalgroup_extension_lite).a()); + EXPECT_EQ( + 0, message.GetExtension(unittest::optional_nested_message_extension_lite) + .bb()); + EXPECT_EQ( + 0, message.GetExtension(unittest::optional_foreign_message_extension_lite) + .c()); + EXPECT_EQ( + 0, message.GetExtension(unittest::optional_import_message_extension_lite) + .d()); + EXPECT_EQ(0, message + .GetExtension( + unittest::optional_public_import_message_extension_lite) + .e()); + EXPECT_EQ(0, + message.GetExtension(unittest::optional_lazy_message_extension_lite) + .bb()); // Enums without defaults are set to the first value in the enum. - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.GetExtension(unittest::optional_nested_enum_extension_lite )); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.GetExtension(unittest::optional_foreign_enum_extension_lite)); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.GetExtension(unittest::optional_import_enum_extension_lite )); + EXPECT_EQ( + unittest::TestAllTypesLite::FOO, + message.GetExtension(unittest::optional_nested_enum_extension_lite)); + EXPECT_EQ( + unittest::FOREIGN_LITE_FOO, + message.GetExtension(unittest::optional_foreign_enum_extension_lite)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_FOO, + message.GetExtension(unittest::optional_import_enum_extension_lite)); // Repeated fields are empty. - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_fixed32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_fixed64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_float_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_double_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bool_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_string_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bytes_extension_lite )); - - EXPECT_EQ(0, message.ExtensionSize(unittest::repeatedgroup_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_nested_message_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_foreign_message_extension_lite)); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_import_message_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_import_enum_extension_lite )); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint64_extension_lite)); + EXPECT_EQ(0, + message.ExtensionSize(unittest::repeated_fixed32_extension_lite)); + EXPECT_EQ(0, + message.ExtensionSize(unittest::repeated_fixed64_extension_lite)); + EXPECT_EQ(0, + message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); + EXPECT_EQ(0, + message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_float_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_double_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bool_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_string_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bytes_extension_lite)); + + EXPECT_EQ(0, message.ExtensionSize(unittest::repeatedgroup_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize( + unittest::repeated_nested_message_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize( + unittest::repeated_foreign_message_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize( + unittest::repeated_import_message_extension_lite)); + EXPECT_EQ( + 0, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite)); + EXPECT_EQ( + 0, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite)); + EXPECT_EQ( + 0, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite)); + EXPECT_EQ( + 0, message.ExtensionSize(unittest::repeated_import_enum_extension_lite)); // has_blah() should also be false for all default fields. - EXPECT_FALSE(message.HasExtension(unittest::default_int32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_int64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_uint32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_uint64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_sint32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_sint64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_fixed32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_fixed64_extension_lite )); + EXPECT_FALSE(message.HasExtension(unittest::default_int32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_int64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_uint32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_uint64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_sint32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_sint64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_fixed32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_fixed64_extension_lite)); EXPECT_FALSE(message.HasExtension(unittest::default_sfixed32_extension_lite)); EXPECT_FALSE(message.HasExtension(unittest::default_sfixed64_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::default_float_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_double_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_bool_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_string_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_bytes_extension_lite )); + EXPECT_FALSE(message.HasExtension(unittest::default_float_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_double_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_bool_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_string_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_bytes_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::default_nested_enum_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_foreign_enum_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::default_import_enum_extension_lite )); + EXPECT_FALSE( + message.HasExtension(unittest::default_nested_enum_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::default_foreign_enum_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::default_import_enum_extension_lite)); // Fields with defaults have their default values (duh). - EXPECT_EQ( 41 , message.GetExtension(unittest::default_int32_extension_lite )); - EXPECT_EQ( 42 , message.GetExtension(unittest::default_int64_extension_lite )); - EXPECT_EQ( 43 , message.GetExtension(unittest::default_uint32_extension_lite )); - EXPECT_EQ( 44 , message.GetExtension(unittest::default_uint64_extension_lite )); - EXPECT_EQ(-45 , message.GetExtension(unittest::default_sint32_extension_lite )); - EXPECT_EQ( 46 , message.GetExtension(unittest::default_sint64_extension_lite )); - EXPECT_EQ( 47 , message.GetExtension(unittest::default_fixed32_extension_lite )); - EXPECT_EQ( 48 , message.GetExtension(unittest::default_fixed64_extension_lite )); - EXPECT_EQ( 49 , message.GetExtension(unittest::default_sfixed32_extension_lite)); - EXPECT_EQ(-50 , message.GetExtension(unittest::default_sfixed64_extension_lite)); - EXPECT_EQ( 51.5 , message.GetExtension(unittest::default_float_extension_lite )); - EXPECT_EQ( 52e3 , message.GetExtension(unittest::default_double_extension_lite )); - EXPECT_EQ(true , message.GetExtension(unittest::default_bool_extension_lite )); - EXPECT_EQ("hello", message.GetExtension(unittest::default_string_extension_lite )); - EXPECT_EQ("world", message.GetExtension(unittest::default_bytes_extension_lite )); - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.GetExtension(unittest::default_nested_enum_extension_lite )); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.GetExtension(unittest::default_foreign_enum_extension_lite)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.GetExtension(unittest::default_import_enum_extension_lite )); + EXPECT_EQ(41, message.GetExtension(unittest::default_int32_extension_lite)); + EXPECT_EQ(42, message.GetExtension(unittest::default_int64_extension_lite)); + EXPECT_EQ(43, message.GetExtension(unittest::default_uint32_extension_lite)); + EXPECT_EQ(44, message.GetExtension(unittest::default_uint64_extension_lite)); + EXPECT_EQ(-45, message.GetExtension(unittest::default_sint32_extension_lite)); + EXPECT_EQ(46, message.GetExtension(unittest::default_sint64_extension_lite)); + EXPECT_EQ(47, message.GetExtension(unittest::default_fixed32_extension_lite)); + EXPECT_EQ(48, message.GetExtension(unittest::default_fixed64_extension_lite)); + EXPECT_EQ(49, + message.GetExtension(unittest::default_sfixed32_extension_lite)); + EXPECT_EQ(-50, + message.GetExtension(unittest::default_sfixed64_extension_lite)); + EXPECT_EQ(51.5, message.GetExtension(unittest::default_float_extension_lite)); + EXPECT_EQ(52e3, + message.GetExtension(unittest::default_double_extension_lite)); + EXPECT_EQ(true, message.GetExtension(unittest::default_bool_extension_lite)); + EXPECT_EQ("hello", + message.GetExtension(unittest::default_string_extension_lite)); + EXPECT_EQ("world", + message.GetExtension(unittest::default_bytes_extension_lite)); + + EXPECT_EQ(unittest::TestAllTypesLite::BAR, + message.GetExtension(unittest::default_nested_enum_extension_lite)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAR, + message.GetExtension(unittest::default_foreign_enum_extension_lite)); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, + message.GetExtension(unittest::default_import_enum_extension_lite)); EXPECT_FALSE(message.HasExtension(unittest::oneof_uint32_extension_lite)); - EXPECT_FALSE(message.GetExtension(unittest::oneof_nested_message_extension_lite).has_bb()); + EXPECT_FALSE( + message.GetExtension(unittest::oneof_nested_message_extension_lite) + .has_bb()); EXPECT_FALSE(message.HasExtension(unittest::oneof_string_extension_lite)); EXPECT_FALSE(message.HasExtension(unittest::oneof_bytes_extension_lite)); } @@ -1316,85 +1571,164 @@ void TestUtilLite::ExpectRepeatedExtensionsModified( // ModifyRepeatedFields only sets the second repeated element of each // field. In addition to verifying this, we also verify that the first // element and size were *not* modified. - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension_lite )); - - ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_message_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_enum_extension_lite )); - - - EXPECT_EQ(201 , message.GetExtension(unittest::repeated_int32_extension_lite , 0)); - EXPECT_EQ(202 , message.GetExtension(unittest::repeated_int64_extension_lite , 0)); - EXPECT_EQ(203 , message.GetExtension(unittest::repeated_uint32_extension_lite , 0)); - EXPECT_EQ(204 , message.GetExtension(unittest::repeated_uint64_extension_lite , 0)); - EXPECT_EQ(205 , message.GetExtension(unittest::repeated_sint32_extension_lite , 0)); - EXPECT_EQ(206 , message.GetExtension(unittest::repeated_sint64_extension_lite , 0)); - EXPECT_EQ(207 , message.GetExtension(unittest::repeated_fixed32_extension_lite , 0)); - EXPECT_EQ(208 , message.GetExtension(unittest::repeated_fixed64_extension_lite , 0)); - EXPECT_EQ(209 , message.GetExtension(unittest::repeated_sfixed32_extension_lite, 0)); - EXPECT_EQ(210 , message.GetExtension(unittest::repeated_sfixed64_extension_lite, 0)); - EXPECT_EQ(211 , message.GetExtension(unittest::repeated_float_extension_lite , 0)); - EXPECT_EQ(212 , message.GetExtension(unittest::repeated_double_extension_lite , 0)); - EXPECT_EQ(true , message.GetExtension(unittest::repeated_bool_extension_lite , 0)); - EXPECT_EQ("215", message.GetExtension(unittest::repeated_string_extension_lite , 0)); - EXPECT_EQ("216", message.GetExtension(unittest::repeated_bytes_extension_lite , 0)); - - EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension_lite , 0).a()); - EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension_lite , 0).bb()); - EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension_lite, 0).c()); - EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension_lite , 0).d()); - EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension_lite , 0).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.GetExtension(unittest::repeated_nested_enum_extension_lite , 0)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 0)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.GetExtension(unittest::repeated_import_enum_extension_lite , 0)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_fixed32_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_fixed64_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension_lite)); + + ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_nested_message_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_foreign_message_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_import_message_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_import_enum_extension_lite)); + + + EXPECT_EQ(201, + message.GetExtension(unittest::repeated_int32_extension_lite, 0)); + EXPECT_EQ(202, + message.GetExtension(unittest::repeated_int64_extension_lite, 0)); + EXPECT_EQ(203, + message.GetExtension(unittest::repeated_uint32_extension_lite, 0)); + EXPECT_EQ(204, + message.GetExtension(unittest::repeated_uint64_extension_lite, 0)); + EXPECT_EQ(205, + message.GetExtension(unittest::repeated_sint32_extension_lite, 0)); + EXPECT_EQ(206, + message.GetExtension(unittest::repeated_sint64_extension_lite, 0)); + EXPECT_EQ(207, + message.GetExtension(unittest::repeated_fixed32_extension_lite, 0)); + EXPECT_EQ(208, + message.GetExtension(unittest::repeated_fixed64_extension_lite, 0)); + EXPECT_EQ( + 209, message.GetExtension(unittest::repeated_sfixed32_extension_lite, 0)); + EXPECT_EQ( + 210, message.GetExtension(unittest::repeated_sfixed64_extension_lite, 0)); + EXPECT_EQ(211, + message.GetExtension(unittest::repeated_float_extension_lite, 0)); + EXPECT_EQ(212, + message.GetExtension(unittest::repeated_double_extension_lite, 0)); + EXPECT_EQ(true, + message.GetExtension(unittest::repeated_bool_extension_lite, 0)); + EXPECT_EQ("215", + message.GetExtension(unittest::repeated_string_extension_lite, 0)); + EXPECT_EQ("216", + message.GetExtension(unittest::repeated_bytes_extension_lite, 0)); + + EXPECT_EQ( + 217, message.GetExtension(unittest::repeatedgroup_extension_lite, 0).a()); + EXPECT_EQ( + 218, + message.GetExtension(unittest::repeated_nested_message_extension_lite, 0) + .bb()); + EXPECT_EQ( + 219, + message.GetExtension(unittest::repeated_foreign_message_extension_lite, 0) + .c()); + EXPECT_EQ( + 220, + message.GetExtension(unittest::repeated_import_message_extension_lite, 0) + .d()); + EXPECT_EQ( + 227, + message.GetExtension(unittest::repeated_lazy_message_extension_lite, 0) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::BAR, + message.GetExtension(unittest::repeated_nested_enum_extension_lite, 0)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAR, + message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 0)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_BAR, + message.GetExtension(unittest::repeated_import_enum_extension_lite, 0)); // Actually verify the second (modified) elements now. - EXPECT_EQ(501 , message.GetExtension(unittest::repeated_int32_extension_lite , 1)); - EXPECT_EQ(502 , message.GetExtension(unittest::repeated_int64_extension_lite , 1)); - EXPECT_EQ(503 , message.GetExtension(unittest::repeated_uint32_extension_lite , 1)); - EXPECT_EQ(504 , message.GetExtension(unittest::repeated_uint64_extension_lite , 1)); - EXPECT_EQ(505 , message.GetExtension(unittest::repeated_sint32_extension_lite , 1)); - EXPECT_EQ(506 , message.GetExtension(unittest::repeated_sint64_extension_lite , 1)); - EXPECT_EQ(507 , message.GetExtension(unittest::repeated_fixed32_extension_lite , 1)); - EXPECT_EQ(508 , message.GetExtension(unittest::repeated_fixed64_extension_lite , 1)); - EXPECT_EQ(509 , message.GetExtension(unittest::repeated_sfixed32_extension_lite, 1)); - EXPECT_EQ(510 , message.GetExtension(unittest::repeated_sfixed64_extension_lite, 1)); - EXPECT_EQ(511 , message.GetExtension(unittest::repeated_float_extension_lite , 1)); - EXPECT_EQ(512 , message.GetExtension(unittest::repeated_double_extension_lite , 1)); - EXPECT_EQ(true , message.GetExtension(unittest::repeated_bool_extension_lite , 1)); - EXPECT_EQ("515", message.GetExtension(unittest::repeated_string_extension_lite , 1)); - EXPECT_EQ("516", message.GetExtension(unittest::repeated_bytes_extension_lite , 1)); - - EXPECT_EQ(517, message.GetExtension(unittest::repeatedgroup_extension_lite , 1).a()); - EXPECT_EQ(518, message.GetExtension(unittest::repeated_nested_message_extension_lite , 1).bb()); - EXPECT_EQ(519, message.GetExtension(unittest::repeated_foreign_message_extension_lite, 1).c()); - EXPECT_EQ(520, message.GetExtension(unittest::repeated_import_message_extension_lite , 1).d()); - EXPECT_EQ(527, message.GetExtension(unittest::repeated_lazy_message_extension_lite , 1).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.GetExtension(unittest::repeated_nested_enum_extension_lite , 1)); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 1)); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.GetExtension(unittest::repeated_import_enum_extension_lite , 1)); + EXPECT_EQ(501, + message.GetExtension(unittest::repeated_int32_extension_lite, 1)); + EXPECT_EQ(502, + message.GetExtension(unittest::repeated_int64_extension_lite, 1)); + EXPECT_EQ(503, + message.GetExtension(unittest::repeated_uint32_extension_lite, 1)); + EXPECT_EQ(504, + message.GetExtension(unittest::repeated_uint64_extension_lite, 1)); + EXPECT_EQ(505, + message.GetExtension(unittest::repeated_sint32_extension_lite, 1)); + EXPECT_EQ(506, + message.GetExtension(unittest::repeated_sint64_extension_lite, 1)); + EXPECT_EQ(507, + message.GetExtension(unittest::repeated_fixed32_extension_lite, 1)); + EXPECT_EQ(508, + message.GetExtension(unittest::repeated_fixed64_extension_lite, 1)); + EXPECT_EQ( + 509, message.GetExtension(unittest::repeated_sfixed32_extension_lite, 1)); + EXPECT_EQ( + 510, message.GetExtension(unittest::repeated_sfixed64_extension_lite, 1)); + EXPECT_EQ(511, + message.GetExtension(unittest::repeated_float_extension_lite, 1)); + EXPECT_EQ(512, + message.GetExtension(unittest::repeated_double_extension_lite, 1)); + EXPECT_EQ(true, + message.GetExtension(unittest::repeated_bool_extension_lite, 1)); + EXPECT_EQ("515", + message.GetExtension(unittest::repeated_string_extension_lite, 1)); + EXPECT_EQ("516", + message.GetExtension(unittest::repeated_bytes_extension_lite, 1)); + + EXPECT_EQ( + 517, message.GetExtension(unittest::repeatedgroup_extension_lite, 1).a()); + EXPECT_EQ( + 518, + message.GetExtension(unittest::repeated_nested_message_extension_lite, 1) + .bb()); + EXPECT_EQ( + 519, + message.GetExtension(unittest::repeated_foreign_message_extension_lite, 1) + .c()); + EXPECT_EQ( + 520, + message.GetExtension(unittest::repeated_import_message_extension_lite, 1) + .d()); + EXPECT_EQ( + 527, + message.GetExtension(unittest::repeated_lazy_message_extension_lite, 1) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::FOO, + message.GetExtension(unittest::repeated_nested_enum_extension_lite, 1)); + EXPECT_EQ( + unittest::FOREIGN_LITE_FOO, + message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 1)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_FOO, + message.GetExtension(unittest::repeated_import_enum_extension_lite, 1)); } @@ -1402,55 +1736,57 @@ void TestUtilLite::ExpectRepeatedExtensionsModified( void TestUtilLite::SetPackedExtensions( unittest::TestPackedExtensionsLite* message) { - message->AddExtension(unittest::packed_int32_extension_lite , 601); - message->AddExtension(unittest::packed_int64_extension_lite , 602); - message->AddExtension(unittest::packed_uint32_extension_lite , 603); - message->AddExtension(unittest::packed_uint64_extension_lite , 604); - message->AddExtension(unittest::packed_sint32_extension_lite , 605); - message->AddExtension(unittest::packed_sint64_extension_lite , 606); - message->AddExtension(unittest::packed_fixed32_extension_lite , 607); - message->AddExtension(unittest::packed_fixed64_extension_lite , 608); + message->AddExtension(unittest::packed_int32_extension_lite, 601); + message->AddExtension(unittest::packed_int64_extension_lite, 602); + message->AddExtension(unittest::packed_uint32_extension_lite, 603); + message->AddExtension(unittest::packed_uint64_extension_lite, 604); + message->AddExtension(unittest::packed_sint32_extension_lite, 605); + message->AddExtension(unittest::packed_sint64_extension_lite, 606); + message->AddExtension(unittest::packed_fixed32_extension_lite, 607); + message->AddExtension(unittest::packed_fixed64_extension_lite, 608); message->AddExtension(unittest::packed_sfixed32_extension_lite, 609); message->AddExtension(unittest::packed_sfixed64_extension_lite, 610); - message->AddExtension(unittest::packed_float_extension_lite , 611); - message->AddExtension(unittest::packed_double_extension_lite , 612); - message->AddExtension(unittest::packed_bool_extension_lite , true); - message->AddExtension(unittest::packed_enum_extension_lite, unittest::FOREIGN_LITE_BAR); + message->AddExtension(unittest::packed_float_extension_lite, 611); + message->AddExtension(unittest::packed_double_extension_lite, 612); + message->AddExtension(unittest::packed_bool_extension_lite, true); + message->AddExtension(unittest::packed_enum_extension_lite, + unittest::FOREIGN_LITE_BAR); // add a second one of each field - message->AddExtension(unittest::packed_int32_extension_lite , 701); - message->AddExtension(unittest::packed_int64_extension_lite , 702); - message->AddExtension(unittest::packed_uint32_extension_lite , 703); - message->AddExtension(unittest::packed_uint64_extension_lite , 704); - message->AddExtension(unittest::packed_sint32_extension_lite , 705); - message->AddExtension(unittest::packed_sint64_extension_lite , 706); - message->AddExtension(unittest::packed_fixed32_extension_lite , 707); - message->AddExtension(unittest::packed_fixed64_extension_lite , 708); + message->AddExtension(unittest::packed_int32_extension_lite, 701); + message->AddExtension(unittest::packed_int64_extension_lite, 702); + message->AddExtension(unittest::packed_uint32_extension_lite, 703); + message->AddExtension(unittest::packed_uint64_extension_lite, 704); + message->AddExtension(unittest::packed_sint32_extension_lite, 705); + message->AddExtension(unittest::packed_sint64_extension_lite, 706); + message->AddExtension(unittest::packed_fixed32_extension_lite, 707); + message->AddExtension(unittest::packed_fixed64_extension_lite, 708); message->AddExtension(unittest::packed_sfixed32_extension_lite, 709); message->AddExtension(unittest::packed_sfixed64_extension_lite, 710); - message->AddExtension(unittest::packed_float_extension_lite , 711); - message->AddExtension(unittest::packed_double_extension_lite , 712); - message->AddExtension(unittest::packed_bool_extension_lite , false); - message->AddExtension(unittest::packed_enum_extension_lite, unittest::FOREIGN_LITE_BAZ); + message->AddExtension(unittest::packed_float_extension_lite, 711); + message->AddExtension(unittest::packed_double_extension_lite, 712); + message->AddExtension(unittest::packed_bool_extension_lite, false); + message->AddExtension(unittest::packed_enum_extension_lite, + unittest::FOREIGN_LITE_BAZ); } // ------------------------------------------------------------------- void TestUtilLite::ModifyPackedExtensions( unittest::TestPackedExtensionsLite* message) { - message->SetExtension(unittest::packed_int32_extension_lite , 1, 801); - message->SetExtension(unittest::packed_int64_extension_lite , 1, 802); - message->SetExtension(unittest::packed_uint32_extension_lite , 1, 803); - message->SetExtension(unittest::packed_uint64_extension_lite , 1, 804); - message->SetExtension(unittest::packed_sint32_extension_lite , 1, 805); - message->SetExtension(unittest::packed_sint64_extension_lite , 1, 806); - message->SetExtension(unittest::packed_fixed32_extension_lite , 1, 807); - message->SetExtension(unittest::packed_fixed64_extension_lite , 1, 808); + message->SetExtension(unittest::packed_int32_extension_lite, 1, 801); + message->SetExtension(unittest::packed_int64_extension_lite, 1, 802); + message->SetExtension(unittest::packed_uint32_extension_lite, 1, 803); + message->SetExtension(unittest::packed_uint64_extension_lite, 1, 804); + message->SetExtension(unittest::packed_sint32_extension_lite, 1, 805); + message->SetExtension(unittest::packed_sint64_extension_lite, 1, 806); + message->SetExtension(unittest::packed_fixed32_extension_lite, 1, 807); + message->SetExtension(unittest::packed_fixed64_extension_lite, 1, 808); message->SetExtension(unittest::packed_sfixed32_extension_lite, 1, 809); message->SetExtension(unittest::packed_sfixed64_extension_lite, 1, 810); - message->SetExtension(unittest::packed_float_extension_lite , 1, 811); - message->SetExtension(unittest::packed_double_extension_lite , 1, 812); - message->SetExtension(unittest::packed_bool_extension_lite , 1, true); - message->SetExtension(unittest::packed_enum_extension_lite , 1, + message->SetExtension(unittest::packed_float_extension_lite, 1, 811); + message->SetExtension(unittest::packed_double_extension_lite, 1, 812); + message->SetExtension(unittest::packed_bool_extension_lite, 1, true); + message->SetExtension(unittest::packed_enum_extension_lite, 1, unittest::FOREIGN_LITE_FOO); } @@ -1458,49 +1794,75 @@ void TestUtilLite::ModifyPackedExtensions( void TestUtilLite::ExpectPackedExtensionsSet( const unittest::TestPackedExtensionsLite& message) { - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension_lite )); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension_lite)); ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed32_extension_lite)); ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed64_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension_lite )); - - EXPECT_EQ(601 , message.GetExtension(unittest::packed_int32_extension_lite , 0)); - EXPECT_EQ(602 , message.GetExtension(unittest::packed_int64_extension_lite , 0)); - EXPECT_EQ(603 , message.GetExtension(unittest::packed_uint32_extension_lite , 0)); - EXPECT_EQ(604 , message.GetExtension(unittest::packed_uint64_extension_lite , 0)); - EXPECT_EQ(605 , message.GetExtension(unittest::packed_sint32_extension_lite , 0)); - EXPECT_EQ(606 , message.GetExtension(unittest::packed_sint64_extension_lite , 0)); - EXPECT_EQ(607 , message.GetExtension(unittest::packed_fixed32_extension_lite , 0)); - EXPECT_EQ(608 , message.GetExtension(unittest::packed_fixed64_extension_lite , 0)); - EXPECT_EQ(609 , message.GetExtension(unittest::packed_sfixed32_extension_lite, 0)); - EXPECT_EQ(610 , message.GetExtension(unittest::packed_sfixed64_extension_lite, 0)); - EXPECT_EQ(611 , message.GetExtension(unittest::packed_float_extension_lite , 0)); - EXPECT_EQ(612 , message.GetExtension(unittest::packed_double_extension_lite , 0)); - EXPECT_EQ(true , message.GetExtension(unittest::packed_bool_extension_lite , 0)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension_lite)); + + EXPECT_EQ(601, + message.GetExtension(unittest::packed_int32_extension_lite, 0)); + EXPECT_EQ(602, + message.GetExtension(unittest::packed_int64_extension_lite, 0)); + EXPECT_EQ(603, + message.GetExtension(unittest::packed_uint32_extension_lite, 0)); + EXPECT_EQ(604, + message.GetExtension(unittest::packed_uint64_extension_lite, 0)); + EXPECT_EQ(605, + message.GetExtension(unittest::packed_sint32_extension_lite, 0)); + EXPECT_EQ(606, + message.GetExtension(unittest::packed_sint64_extension_lite, 0)); + EXPECT_EQ(607, + message.GetExtension(unittest::packed_fixed32_extension_lite, 0)); + EXPECT_EQ(608, + message.GetExtension(unittest::packed_fixed64_extension_lite, 0)); + EXPECT_EQ(609, + message.GetExtension(unittest::packed_sfixed32_extension_lite, 0)); + EXPECT_EQ(610, + message.GetExtension(unittest::packed_sfixed64_extension_lite, 0)); + EXPECT_EQ(611, + message.GetExtension(unittest::packed_float_extension_lite, 0)); + EXPECT_EQ(612, + message.GetExtension(unittest::packed_double_extension_lite, 0)); + EXPECT_EQ(true, + message.GetExtension(unittest::packed_bool_extension_lite, 0)); EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.GetExtension(unittest::packed_enum_extension_lite, 0)); - EXPECT_EQ(701 , message.GetExtension(unittest::packed_int32_extension_lite , 1)); - EXPECT_EQ(702 , message.GetExtension(unittest::packed_int64_extension_lite , 1)); - EXPECT_EQ(703 , message.GetExtension(unittest::packed_uint32_extension_lite , 1)); - EXPECT_EQ(704 , message.GetExtension(unittest::packed_uint64_extension_lite , 1)); - EXPECT_EQ(705 , message.GetExtension(unittest::packed_sint32_extension_lite , 1)); - EXPECT_EQ(706 , message.GetExtension(unittest::packed_sint64_extension_lite , 1)); - EXPECT_EQ(707 , message.GetExtension(unittest::packed_fixed32_extension_lite , 1)); - EXPECT_EQ(708 , message.GetExtension(unittest::packed_fixed64_extension_lite , 1)); - EXPECT_EQ(709 , message.GetExtension(unittest::packed_sfixed32_extension_lite, 1)); - EXPECT_EQ(710 , message.GetExtension(unittest::packed_sfixed64_extension_lite, 1)); - EXPECT_EQ(711 , message.GetExtension(unittest::packed_float_extension_lite , 1)); - EXPECT_EQ(712 , message.GetExtension(unittest::packed_double_extension_lite , 1)); - EXPECT_EQ(false, message.GetExtension(unittest::packed_bool_extension_lite , 1)); + EXPECT_EQ(701, + message.GetExtension(unittest::packed_int32_extension_lite, 1)); + EXPECT_EQ(702, + message.GetExtension(unittest::packed_int64_extension_lite, 1)); + EXPECT_EQ(703, + message.GetExtension(unittest::packed_uint32_extension_lite, 1)); + EXPECT_EQ(704, + message.GetExtension(unittest::packed_uint64_extension_lite, 1)); + EXPECT_EQ(705, + message.GetExtension(unittest::packed_sint32_extension_lite, 1)); + EXPECT_EQ(706, + message.GetExtension(unittest::packed_sint64_extension_lite, 1)); + EXPECT_EQ(707, + message.GetExtension(unittest::packed_fixed32_extension_lite, 1)); + EXPECT_EQ(708, + message.GetExtension(unittest::packed_fixed64_extension_lite, 1)); + EXPECT_EQ(709, + message.GetExtension(unittest::packed_sfixed32_extension_lite, 1)); + EXPECT_EQ(710, + message.GetExtension(unittest::packed_sfixed64_extension_lite, 1)); + EXPECT_EQ(711, + message.GetExtension(unittest::packed_float_extension_lite, 1)); + EXPECT_EQ(712, + message.GetExtension(unittest::packed_double_extension_lite, 1)); + EXPECT_EQ(false, + message.GetExtension(unittest::packed_bool_extension_lite, 1)); EXPECT_EQ(unittest::FOREIGN_LITE_BAZ, message.GetExtension(unittest::packed_enum_extension_lite, 1)); } @@ -1509,70 +1871,96 @@ void TestUtilLite::ExpectPackedExtensionsSet( void TestUtilLite::ExpectPackedExtensionsClear( const unittest::TestPackedExtensionsLite& message) { - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed64_extension_lite )); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed64_extension_lite)); EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sfixed32_extension_lite)); EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sfixed64_extension_lite)); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_float_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_double_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_bool_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_enum_extension_lite )); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_float_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_double_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_bool_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_enum_extension_lite)); } // ------------------------------------------------------------------- void TestUtilLite::ExpectPackedExtensionsModified( const unittest::TestPackedExtensionsLite& message) { - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension_lite )); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension_lite)); ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed32_extension_lite)); ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed64_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension_lite )); - EXPECT_EQ(601 , message.GetExtension(unittest::packed_int32_extension_lite , 0)); - EXPECT_EQ(602 , message.GetExtension(unittest::packed_int64_extension_lite , 0)); - EXPECT_EQ(603 , message.GetExtension(unittest::packed_uint32_extension_lite , 0)); - EXPECT_EQ(604 , message.GetExtension(unittest::packed_uint64_extension_lite , 0)); - EXPECT_EQ(605 , message.GetExtension(unittest::packed_sint32_extension_lite , 0)); - EXPECT_EQ(606 , message.GetExtension(unittest::packed_sint64_extension_lite , 0)); - EXPECT_EQ(607 , message.GetExtension(unittest::packed_fixed32_extension_lite , 0)); - EXPECT_EQ(608 , message.GetExtension(unittest::packed_fixed64_extension_lite , 0)); - EXPECT_EQ(609 , message.GetExtension(unittest::packed_sfixed32_extension_lite, 0)); - EXPECT_EQ(610 , message.GetExtension(unittest::packed_sfixed64_extension_lite, 0)); - EXPECT_EQ(611 , message.GetExtension(unittest::packed_float_extension_lite , 0)); - EXPECT_EQ(612 , message.GetExtension(unittest::packed_double_extension_lite , 0)); - EXPECT_EQ(true , message.GetExtension(unittest::packed_bool_extension_lite , 0)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension_lite)); + EXPECT_EQ(601, + message.GetExtension(unittest::packed_int32_extension_lite, 0)); + EXPECT_EQ(602, + message.GetExtension(unittest::packed_int64_extension_lite, 0)); + EXPECT_EQ(603, + message.GetExtension(unittest::packed_uint32_extension_lite, 0)); + EXPECT_EQ(604, + message.GetExtension(unittest::packed_uint64_extension_lite, 0)); + EXPECT_EQ(605, + message.GetExtension(unittest::packed_sint32_extension_lite, 0)); + EXPECT_EQ(606, + message.GetExtension(unittest::packed_sint64_extension_lite, 0)); + EXPECT_EQ(607, + message.GetExtension(unittest::packed_fixed32_extension_lite, 0)); + EXPECT_EQ(608, + message.GetExtension(unittest::packed_fixed64_extension_lite, 0)); + EXPECT_EQ(609, + message.GetExtension(unittest::packed_sfixed32_extension_lite, 0)); + EXPECT_EQ(610, + message.GetExtension(unittest::packed_sfixed64_extension_lite, 0)); + EXPECT_EQ(611, + message.GetExtension(unittest::packed_float_extension_lite, 0)); + EXPECT_EQ(612, + message.GetExtension(unittest::packed_double_extension_lite, 0)); + EXPECT_EQ(true, + message.GetExtension(unittest::packed_bool_extension_lite, 0)); EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.GetExtension(unittest::packed_enum_extension_lite, 0)); // Actually verify the second (modified) elements now. - EXPECT_EQ(801 , message.GetExtension(unittest::packed_int32_extension_lite , 1)); - EXPECT_EQ(802 , message.GetExtension(unittest::packed_int64_extension_lite , 1)); - EXPECT_EQ(803 , message.GetExtension(unittest::packed_uint32_extension_lite , 1)); - EXPECT_EQ(804 , message.GetExtension(unittest::packed_uint64_extension_lite , 1)); - EXPECT_EQ(805 , message.GetExtension(unittest::packed_sint32_extension_lite , 1)); - EXPECT_EQ(806 , message.GetExtension(unittest::packed_sint64_extension_lite , 1)); - EXPECT_EQ(807 , message.GetExtension(unittest::packed_fixed32_extension_lite , 1)); - EXPECT_EQ(808 , message.GetExtension(unittest::packed_fixed64_extension_lite , 1)); - EXPECT_EQ(809 , message.GetExtension(unittest::packed_sfixed32_extension_lite, 1)); - EXPECT_EQ(810 , message.GetExtension(unittest::packed_sfixed64_extension_lite, 1)); - EXPECT_EQ(811 , message.GetExtension(unittest::packed_float_extension_lite , 1)); - EXPECT_EQ(812 , message.GetExtension(unittest::packed_double_extension_lite , 1)); - EXPECT_EQ(true , message.GetExtension(unittest::packed_bool_extension_lite , 1)); + EXPECT_EQ(801, + message.GetExtension(unittest::packed_int32_extension_lite, 1)); + EXPECT_EQ(802, + message.GetExtension(unittest::packed_int64_extension_lite, 1)); + EXPECT_EQ(803, + message.GetExtension(unittest::packed_uint32_extension_lite, 1)); + EXPECT_EQ(804, + message.GetExtension(unittest::packed_uint64_extension_lite, 1)); + EXPECT_EQ(805, + message.GetExtension(unittest::packed_sint32_extension_lite, 1)); + EXPECT_EQ(806, + message.GetExtension(unittest::packed_sint64_extension_lite, 1)); + EXPECT_EQ(807, + message.GetExtension(unittest::packed_fixed32_extension_lite, 1)); + EXPECT_EQ(808, + message.GetExtension(unittest::packed_fixed64_extension_lite, 1)); + EXPECT_EQ(809, + message.GetExtension(unittest::packed_sfixed32_extension_lite, 1)); + EXPECT_EQ(810, + message.GetExtension(unittest::packed_sfixed64_extension_lite, 1)); + EXPECT_EQ(811, + message.GetExtension(unittest::packed_float_extension_lite, 1)); + EXPECT_EQ(812, + message.GetExtension(unittest::packed_double_extension_lite, 1)); + EXPECT_EQ(true, + message.GetExtension(unittest::packed_bool_extension_lite, 1)); EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.GetExtension(unittest::packed_enum_extension_lite, 1)); } diff --git a/src/google/protobuf/testing/file.cc b/src/google/protobuf/testing/file.cc index def92a00e4..7d3708e3d6 100644 --- a/src/google/protobuf/testing/file.cc +++ b/src/google/protobuf/testing/file.cc @@ -45,7 +45,8 @@ #endif #include -#include +#include +#include namespace google { namespace protobuf { @@ -58,11 +59,11 @@ namespace protobuf { #endif #ifdef _WIN32 -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::chdir; -using google::protobuf::internal::win32::fopen; -using google::protobuf::internal::win32::mkdir; -using google::protobuf::internal::win32::stat; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::chdir; +using google::protobuf::io::win32::fopen; +using google::protobuf::io::win32::mkdir; +using google::protobuf::io::win32::stat; #endif bool File::Exists(const string& name) { diff --git a/src/google/protobuf/testing/googletest.cc b/src/google/protobuf/testing/googletest.cc index 8c89e5afba..1856971cb8 100644 --- a/src/google/protobuf/testing/googletest.cc +++ b/src/google/protobuf/testing/googletest.cc @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include #include @@ -55,11 +55,11 @@ namespace protobuf { #ifdef _WIN32 // DO NOT include , instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::dup2; -using google::protobuf::internal::win32::dup; -using google::protobuf::internal::win32::mkdir; -using google::protobuf::internal::win32::open; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::dup2; +using google::protobuf::io::win32::dup; +using google::protobuf::io::win32::mkdir; +using google::protobuf::io::win32::open; #endif #ifndef O_BINARY @@ -86,7 +86,11 @@ string TestSourceDir() { // Look for the "src" directory. string prefix = "."; - while (!File::Exists(prefix + "/src/google/protobuf")) { + // Keep looking further up the directory tree until we find + // src/.../descriptor.cc. It is important to look for a particular file, + // keeping in mind that with Bazel builds the directory structure under + // bazel-bin/ looks similar to the main directory tree in the Git repo. + while (!File::Exists(prefix + "/src/google/protobuf/descriptor.cc")) { if (!File::Exists(prefix)) { GOOGLE_LOG(FATAL) << "Could not find protobuf source code. Please run tests from " @@ -115,7 +119,10 @@ string GetTemporaryDirectoryName() { // testing. We cannot use tmpfile() or mkstemp() since we're creating a // directory. char b[L_tmpnam + 1]; // HPUX multithread return 0 if s is 0 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" string result = tmpnam(b); +#pragma GCC diagnostic pop #ifdef _WIN32 // Avoid a trailing dot by changing it to an underscore. On Win32 the names of // files and directories can, but should not, end with dot. diff --git a/src/google/protobuf/testing/googletest.h b/src/google/protobuf/testing/googletest.h index 8163748603..4e0cb83a85 100644 --- a/src/google/protobuf/testing/googletest.h +++ b/src/google/protobuf/testing/googletest.h @@ -37,6 +37,7 @@ #include #include #include +#include #include // Disable death tests if we use exceptions in CHECK(). #if !PROTOBUF_USE_EXCEPTIONS && defined(GTEST_HAS_DEATH_TEST) && \ diff --git a/src/google/protobuf/text_format.cc b/src/google/protobuf/text_format.cc index 93c24b2396..420c1e8c6d 100644 --- a/src/google/protobuf/text_format.cc +++ b/src/google/protobuf/text_format.cc @@ -32,17 +32,16 @@ // 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 @@ -53,6 +52,8 @@ #include #include #include +#include +#include #include #include #include @@ -60,30 +61,31 @@ - #include #include +#include + namespace google { namespace protobuf { namespace { -inline bool IsHexNumber(const string& str) { +inline bool IsHexNumber(const std::string& str) { return (str.length() >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')); } -inline bool IsOctNumber(const string& str) { +inline bool IsOctNumber(const std::string& str) { return (str.length() >= 2 && str[0] == '0' && (str[1] >= '0' && str[1] < '8')); } } // namespace -string Message::DebugString() const { - string debug_string; +std::string Message::DebugString() const { + std::string debug_string; TextFormat::Printer printer; printer.SetExpandAny(true); @@ -93,8 +95,8 @@ string Message::DebugString() const { return debug_string; } -string Message::ShortDebugString() const { - string debug_string; +std::string Message::ShortDebugString() const { + std::string debug_string; TextFormat::Printer printer; printer.SetSingleLineMode(true); @@ -102,16 +104,15 @@ string Message::ShortDebugString() const { printer.PrintToString(*this, &debug_string); // Single line mode currently might have an extra space at the end. - if (debug_string.size() > 0 && - debug_string[debug_string.size() - 1] == ' ') { + if (debug_string.size() > 0 && debug_string[debug_string.size() - 1] == ' ') { debug_string.resize(debug_string.size() - 1); } return debug_string; } -string Message::Utf8DebugString() const { - string debug_string; +std::string Message::Utf8DebugString() const { + std::string debug_string; TextFormat::Printer printer; printer.SetUseUtf8StringEscaping(true); @@ -122,40 +123,28 @@ string Message::Utf8DebugString() const { return debug_string; } -void Message::PrintDebugString() const { - printf("%s", DebugString().c_str()); -} +void Message::PrintDebugString() const { printf("%s", DebugString().c_str()); } // =========================================================================== // Implementation of the parse information tree class. -TextFormat::ParseInfoTree::ParseInfoTree() { } - -TextFormat::ParseInfoTree::~ParseInfoTree() { - // Remove any nested information trees, as they are owned by this tree. - for (NestedMap::iterator it = nested_.begin(); it != nested_.end(); ++it) { - STLDeleteElements(&(it->second)); - } -} - void TextFormat::ParseInfoTree::RecordLocation( - const FieldDescriptor* field, - TextFormat::ParseLocation location) { + const FieldDescriptor* field, TextFormat::ParseLocation location) { locations_[field].push_back(location); } TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::CreateNested( const FieldDescriptor* field) { // Owned by us in the map. - TextFormat::ParseInfoTree* instance = new TextFormat::ParseInfoTree(); - std::vector* trees = &nested_[field]; - GOOGLE_CHECK(trees); - trees->push_back(instance); - return instance; + auto& vec = nested_[field]; + vec.emplace_back(new TextFormat::ParseInfoTree()); + return vec.back().get(); } void CheckFieldIndex(const FieldDescriptor* field, int index) { - if (field == NULL) { return; } + if (field == nullptr) { + return; + } if (field->is_repeated() && index == -1) { GOOGLE_LOG(DFATAL) << "Index must be in range of repeated field values. " @@ -169,11 +158,13 @@ void CheckFieldIndex(const FieldDescriptor* field, int index) { TextFormat::ParseLocation TextFormat::ParseInfoTree::GetLocation( const FieldDescriptor* field, int index) const { CheckFieldIndex(field, index); - if (index == -1) { index = 0; } + if (index == -1) { + index = 0; + } const std::vector* locations = FindOrNull(locations_, field); - if (locations == NULL || index >= locations->size()) { + if (locations == nullptr || index >= locations->size()) { return TextFormat::ParseLocation(); } @@ -183,31 +174,39 @@ TextFormat::ParseLocation TextFormat::ParseInfoTree::GetLocation( TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::GetTreeForNested( const FieldDescriptor* field, int index) const { CheckFieldIndex(field, index); - if (index == -1) { index = 0; } + if (index == -1) { + index = 0; + } - const std::vector* trees = - FindOrNull(nested_, field); - if (trees == NULL || index >= trees->size()) { - return NULL; + auto it = nested_.find(field); + if (it == nested_.end() || index >= it->second.size()) { + return nullptr; } - return (*trees)[index]; + return it->second[index].get(); } namespace { // These functions implement the behavior of the "default" TextFormat::Finder, -// they are defined as standalone to be called when finder_ is NULL. +// they are defined as standalone to be called when finder_ is nullptr. const FieldDescriptor* DefaultFinderFindExtension(Message* message, - const string& name) { - return message->GetReflection()->FindKnownExtensionByName(name); + const std::string& name) { + const Descriptor* descriptor = message->GetDescriptor(); + return descriptor->file()->pool()->FindExtensionByPrintableName(descriptor, + name); +} + +const FieldDescriptor* DefaultFinderFindExtensionByNumber( + const Descriptor* descriptor, int number) { + return descriptor->file()->pool()->FindExtensionByNumber(descriptor, number); } const Descriptor* DefaultFinderFindAnyType(const Message& message, - const string& prefix, - const string& name) { + const std::string& prefix, + const std::string& name) { if (prefix != internal::kTypeGoogleApisComPrefix && prefix != internal::kTypeGoogleProdComPrefix) { - return NULL; + return nullptr; } return message.GetDescriptor()->file()->pool()->FindMessageTypeByName(name); } @@ -223,11 +222,14 @@ const Descriptor* DefaultFinderFindAnyType(const Message& message, // Makes code slightly more readable. The meaning of "DO(foo)" is // "Execute foo and fail if it fails.", where failure is indicated by // returning false. Borrowed from parser.cc (Thanks Kenton!). -#define DO(STATEMENT) if (STATEMENT) {} else return false +#define DO(STATEMENT) \ + if (STATEMENT) { \ + } else { \ + return false; \ + } class TextFormat::Parser::ParserImpl { public: - // Determines if repeated values for non-repeated fields and // oneofs are permitted, e.g., the string "foo: 1 foo: 2" for a // required/optional field named "foo", or "baz: 1 qux: 2" @@ -240,30 +242,27 @@ class TextFormat::Parser::ParserImpl { ParserImpl(const Descriptor* root_message_type, io::ZeroCopyInputStream* input_stream, io::ErrorCollector* error_collector, - const TextFormat::Finder* finder, - ParseInfoTree* parse_info_tree, + const TextFormat::Finder* finder, ParseInfoTree* parse_info_tree, SingularOverwritePolicy singular_overwrite_policy, - bool allow_case_insensitive_field, - bool allow_unknown_field, - bool allow_unknown_extension, - bool allow_unknown_enum, - bool allow_field_number, - bool allow_relaxed_whitespace, - bool allow_partial) - : error_collector_(error_collector), - finder_(finder), - parse_info_tree_(parse_info_tree), - tokenizer_error_collector_(this), - tokenizer_(input_stream, &tokenizer_error_collector_), - root_message_type_(root_message_type), - singular_overwrite_policy_(singular_overwrite_policy), - allow_case_insensitive_field_(allow_case_insensitive_field), - allow_unknown_field_(allow_unknown_field), - allow_unknown_extension_(allow_unknown_extension), - allow_unknown_enum_(allow_unknown_enum), - allow_field_number_(allow_field_number), - allow_partial_(allow_partial), - had_errors_(false) { + bool allow_case_insensitive_field, bool allow_unknown_field, + bool allow_unknown_extension, bool allow_unknown_enum, + bool allow_field_number, bool allow_relaxed_whitespace, + bool allow_partial, int recursion_limit) + : error_collector_(error_collector), + finder_(finder), + parse_info_tree_(parse_info_tree), + tokenizer_error_collector_(this), + tokenizer_(input_stream, &tokenizer_error_collector_), + root_message_type_(root_message_type), + singular_overwrite_policy_(singular_overwrite_policy), + allow_case_insensitive_field_(allow_case_insensitive_field), + allow_unknown_field_(allow_unknown_field), + allow_unknown_extension_(allow_unknown_extension), + allow_unknown_enum_(allow_unknown_enum), + allow_field_number_(allow_field_number), + allow_partial_(allow_partial), + recursion_limit_(recursion_limit), + had_errors_(false) { // For backwards-compatibility with proto1, we need to allow the 'f' suffix // for floats. tokenizer_.set_allow_f_after_float(true); @@ -279,7 +278,7 @@ class TextFormat::Parser::ParserImpl { // Consume the starting token. tokenizer_.Next(); } - ~ParserImpl() { } + ~ParserImpl() {} // Parses the ASCII representation specified in input and saves the // information into the output pointer (a Message). Returns @@ -306,35 +305,31 @@ class TextFormat::Parser::ParserImpl { return suc && LookingAtType(io::Tokenizer::TYPE_END); } - void ReportError(int line, int col, const string& message) { + void ReportError(int line, int col, const std::string& message) { had_errors_ = true; - if (error_collector_ == NULL) { + if (error_collector_ == nullptr) { if (line >= 0) { GOOGLE_LOG(ERROR) << "Error parsing text-format " - << root_message_type_->full_name() - << ": " << (line + 1) << ":" - << (col + 1) << ": " << message; + << root_message_type_->full_name() << ": " << (line + 1) + << ":" << (col + 1) << ": " << message; } else { GOOGLE_LOG(ERROR) << "Error parsing text-format " - << root_message_type_->full_name() - << ": " << message; + << root_message_type_->full_name() << ": " << message; } } else { error_collector_->AddError(line, col, message); } } - void ReportWarning(int line, int col, const string& message) { - if (error_collector_ == NULL) { + void ReportWarning(int line, int col, const std::string& message) { + if (error_collector_ == nullptr) { if (line >= 0) { GOOGLE_LOG(WARNING) << "Warning parsing text-format " - << root_message_type_->full_name() - << ": " << (line + 1) << ":" - << (col + 1) << ": " << message; + << root_message_type_->full_name() << ": " << (line + 1) + << ":" << (col + 1) << ": " << message; } else { GOOGLE_LOG(WARNING) << "Warning parsing text-format " - << root_message_type_->full_name() - << ": " << message; + << root_message_type_->full_name() << ": " << message; } } else { error_collector_->AddWarning(line, col, message); @@ -346,14 +341,14 @@ class TextFormat::Parser::ParserImpl { // Reports an error with the given message with information indicating // the position (as derived from the current token). - void ReportError(const string& message) { + void ReportError(const std::string& message) { ReportError(tokenizer_.current().line, tokenizer_.current().column, message); } // Reports a warning with the given message with information indicating // the position (as derived from the current token). - void ReportWarning(const string& message) { + void ReportWarning(const std::string& message) { ReportWarning(tokenizer_.current().line, tokenizer_.current().column, message); } @@ -361,8 +356,8 @@ class TextFormat::Parser::ParserImpl { // Consumes the specified message with the given starting delimiter. // This method checks to see that the end delimiter at the conclusion of // the consumption matches the starting delimiter passed in here. - bool ConsumeMessage(Message* message, const string delimiter) { - while (!LookingAt(">") && !LookingAt("}")) { + bool ConsumeMessage(Message* message, const std::string delimiter) { + while (!LookingAt(">") && !LookingAt("}")) { DO(ConsumeField(message)); } @@ -372,7 +367,7 @@ class TextFormat::Parser::ParserImpl { } // Consume either "<" or "{". - bool ConsumeMessageDelimiter(string* delimiter) { + bool ConsumeMessageDelimiter(std::string* delimiter) { if (TryConsume("<")) { *delimiter = ">"; } else { @@ -389,9 +384,9 @@ class TextFormat::Parser::ParserImpl { const Reflection* reflection = message->GetReflection(); const Descriptor* descriptor = message->GetDescriptor(); - string field_name; + std::string field_name; bool reserved_field = false; - const FieldDescriptor* field = NULL; + const FieldDescriptor* field = nullptr; int start_line = tokenizer_.current().line; int start_column = tokenizer_.current().column; @@ -400,15 +395,15 @@ class TextFormat::Parser::ParserImpl { if (internal::GetAnyFieldDescriptors(*message, &any_type_url_field, &any_value_field) && TryConsume("[")) { - string full_type_name, prefix; + std::string full_type_name, prefix; DO(ConsumeAnyTypeUrl(&full_type_name, &prefix)); DO(Consume("]")); TryConsume(":"); // ':' is optional between message labels and values. - string serialized_value; + std::string serialized_value; const Descriptor* value_descriptor = finder_ ? finder_->FindAnyType(*message, prefix, full_type_name) : DefaultFinderFindAnyType(*message, prefix, full_type_name); - if (value_descriptor == NULL) { + if (value_descriptor == nullptr) { ReportError("Could not find type \"" + prefix + full_type_name + "\" stored in google.protobuf.Any."); return false; @@ -424,9 +419,8 @@ class TextFormat::Parser::ParserImpl { return false; } } - reflection->SetString( - message, any_type_url_field, - string(prefix + full_type_name)); + reflection->SetString(message, any_type_url_field, + std::string(prefix + full_type_name)); reflection->SetString(message, any_value_field, serialized_value); return true; } @@ -438,15 +432,16 @@ class TextFormat::Parser::ParserImpl { field = finder_ ? finder_->FindExtension(message, field_name) : DefaultFinderFindExtension(message, field_name); - if (field == NULL) { + if (field == nullptr) { if (!allow_unknown_field_ && !allow_unknown_extension_) { - ReportError("Extension \"" + field_name + "\" is not defined or " + ReportError("Extension \"" + field_name + + "\" is not defined or " "is not an extension of \"" + descriptor->full_name() + "\"."); return false; } else { - ReportWarning("Extension \"" + field_name + "\" is not defined or " - "is not an extension of \"" + + ReportWarning("Ignoring extension \"" + field_name + + "\" which is not defined or is not an extension of \"" + descriptor->full_name() + "\"."); } } @@ -457,7 +452,10 @@ class TextFormat::Parser::ParserImpl { if (allow_field_number_ && safe_strto32(field_name, &field_number)) { if (descriptor->IsExtensionNumber(field_number)) { - field = reflection->FindKnownExtensionByNumber(field_number); + field = finder_ + ? finder_->FindExtensionByNumber(descriptor, field_number) + : DefaultFinderFindExtensionByNumber(descriptor, + field_number); } else if (descriptor->IsReservedNumber(field_number)) { reserved_field = true; } else { @@ -468,33 +466,34 @@ class TextFormat::Parser::ParserImpl { // Group names are expected to be capitalized as they appear in the // .proto file, which actually matches their type names, not their // field names. - if (field == NULL) { - string lower_field_name = field_name; + if (field == nullptr) { + std::string lower_field_name = field_name; LowerString(&lower_field_name); field = descriptor->FindFieldByName(lower_field_name); // If the case-insensitive match worked but the field is NOT a group, - if (field != NULL && field->type() != FieldDescriptor::TYPE_GROUP) { - field = NULL; + if (field != nullptr && + field->type() != FieldDescriptor::TYPE_GROUP) { + field = nullptr; } } // Again, special-case group names as described above. - if (field != NULL && field->type() == FieldDescriptor::TYPE_GROUP - && field->message_type()->name() != field_name) { - field = NULL; + if (field != nullptr && field->type() == FieldDescriptor::TYPE_GROUP && + field->message_type()->name() != field_name) { + field = nullptr; } - if (field == NULL && allow_case_insensitive_field_) { - string lower_field_name = field_name; + if (field == nullptr && allow_case_insensitive_field_) { + std::string lower_field_name = field_name; LowerString(&lower_field_name); field = descriptor->FindFieldByLowercaseName(lower_field_name); } - if (field == NULL) { + if (field == nullptr) { reserved_field = descriptor->IsReservedName(field_name); } } - if (field == NULL && !reserved_field) { + if (field == nullptr && !reserved_field) { if (!allow_unknown_field_) { ReportError("Message type \"" + descriptor->full_name() + "\" has no field named \"" + field_name + "\"."); @@ -507,7 +506,7 @@ class TextFormat::Parser::ParserImpl { } // Skips unknown or reserved fields. - if (field == NULL) { + if (field == nullptr) { GOOGLE_CHECK(allow_unknown_field_ || allow_unknown_extension_ || reserved_field); // Try to guess the type of this field. @@ -533,12 +532,16 @@ class TextFormat::Parser::ParserImpl { // Fail if the field is a member of a oneof and another member has already // been specified. const OneofDescriptor* oneof = field->containing_oneof(); - if (oneof != NULL && reflection->HasOneof(*message, oneof)) { + if (oneof != nullptr && reflection->HasOneof(*message, oneof)) { const FieldDescriptor* other_field = reflection->GetOneofFieldDescriptor(*message, oneof); - ReportError("Field \"" + field_name + "\" is specified along with " - "field \"" + other_field->name() + "\", another member " - "of oneof \"" + oneof->name() + "\"."); + ReportError("Field \"" + field_name + + "\" is specified along with " + "field \"" + + other_field->name() + + "\", another member " + "of oneof \"" + + oneof->name() + "\"."); return false; } } @@ -547,11 +550,15 @@ class TextFormat::Parser::ParserImpl { if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { // ':' is optional here. bool consumed_semicolon = TryConsume(":"); - if (consumed_semicolon && field->options().weak() && LookingAtType(io::Tokenizer::TYPE_STRING)) { + if (consumed_semicolon && field->options().weak() && + LookingAtType(io::Tokenizer::TYPE_STRING)) { // we are getting a bytes string for a weak field. - string tmp; + std::string tmp; DO(ConsumeString(&tmp)); - reflection->MutableMessage(message, field)->ParseFromString(tmp); + MessageFactory* factory = + finder_ ? finder_->FindExtensionFactory(field) : nullptr; + reflection->MutableMessage(message, field, factory) + ->ParseFromString(tmp); goto label_skip_parsing; } } else { @@ -581,19 +588,19 @@ class TextFormat::Parser::ParserImpl { } else { DO(ConsumeFieldValue(message, reflection, field)); } -label_skip_parsing: + label_skip_parsing: // For historical reasons, fields may optionally be separated by commas or // semicolons. TryConsume(";") || TryConsume(","); if (field->options().deprecated()) { - ReportWarning("text format contains deprecated field \"" - + field_name + "\""); + ReportWarning("text format contains deprecated field \"" + field_name + + "\""); } // If a parse info tree exists, add the location for the parsed // field. - if (parse_info_tree_ != NULL) { + if (parse_info_tree_ != nullptr) { RecordLocation(parse_info_tree_, field, ParseLocation(start_line, start_column)); } @@ -608,7 +615,7 @@ label_skip_parsing: DO(ConsumeTypeUrlOrFullTypeName()); DO(Consume("]")); } else { - string field_name; + std::string field_name; DO(ConsumeIdentifier(&field_name)); } @@ -629,26 +636,33 @@ label_skip_parsing: return true; } - bool ConsumeFieldMessage(Message* message, - const Reflection* reflection, + bool ConsumeFieldMessage(Message* message, const Reflection* reflection, const FieldDescriptor* field) { - - // If the parse information tree is not NULL, create a nested one + if (--recursion_limit_ < 0) { + ReportError("Message is too deep"); + return false; + } + // If the parse information tree is not nullptr, create a nested one // for the nested message. ParseInfoTree* parent = parse_info_tree_; - if (parent != NULL) { + if (parent != nullptr) { parse_info_tree_ = CreateNested(parent, field); } - string delimiter; + std::string delimiter; DO(ConsumeMessageDelimiter(&delimiter)); + MessageFactory* factory = + finder_ ? finder_->FindExtensionFactory(field) : nullptr; if (field->is_repeated()) { - DO(ConsumeMessage(reflection->AddMessage(message, field), delimiter)); + DO(ConsumeMessage(reflection->AddMessage(message, field, factory), + delimiter)); } else { - DO(ConsumeMessage(reflection->MutableMessage(message, field), + DO(ConsumeMessage(reflection->MutableMessage(message, field, factory), delimiter)); } + ++recursion_limit_; + // Reset the parse information tree. parse_info_tree_ = parent; return true; @@ -657,30 +671,28 @@ label_skip_parsing: // Skips the whole body of a message including the beginning delimiter and // the ending delimiter. bool SkipFieldMessage() { - string delimiter; + std::string delimiter; DO(ConsumeMessageDelimiter(&delimiter)); - while (!LookingAt(">") && !LookingAt("}")) { + while (!LookingAt(">") && !LookingAt("}")) { DO(SkipField()); } DO(Consume(delimiter)); return true; } - bool ConsumeFieldValue(Message* message, - const Reflection* reflection, + bool ConsumeFieldValue(Message* message, const Reflection* reflection, const FieldDescriptor* field) { - // Define an easy to use macro for setting fields. This macro checks // to see if the field is repeated (in which case we need to use the Add // methods or not (in which case we need to use the Set methods). -#define SET_FIELD(CPPTYPE, VALUE) \ - if (field->is_repeated()) { \ - reflection->Add##CPPTYPE(message, field, VALUE); \ - } else { \ - reflection->Set##CPPTYPE(message, field, VALUE); \ - } \ - - switch(field->cpp_type()) { +#define SET_FIELD(CPPTYPE, VALUE) \ + if (field->is_repeated()) { \ + reflection->Add##CPPTYPE(message, field, VALUE); \ + } else { \ + reflection->Set##CPPTYPE(message, field, VALUE); \ + } + + switch (field->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: { int64 value; DO(ConsumeSignedInteger(&value, kint32max)); @@ -724,7 +736,7 @@ label_skip_parsing: } case FieldDescriptor::CPPTYPE_STRING: { - string value; + std::string value; DO(ConsumeString(&value)); SET_FIELD(String, value); break; @@ -736,15 +748,15 @@ label_skip_parsing: DO(ConsumeUnsignedInteger(&value, 1)); SET_FIELD(Bool, value); } else { - string value; + std::string value; DO(ConsumeIdentifier(&value)); if (value == "true" || value == "True" || value == "t") { SET_FIELD(Bool, true); } else if (value == "false" || value == "False" || value == "f") { SET_FIELD(Bool, false); } else { - ReportError("Invalid value for boolean field \"" + field->name() - + "\". Value: \"" + value + "\"."); + ReportError("Invalid value for boolean field \"" + field->name() + + "\". Value: \"" + value + "\"."); return false; } } @@ -752,9 +764,10 @@ label_skip_parsing: } case FieldDescriptor::CPPTYPE_ENUM: { - string value; + std::string value; + int64 int_value = kint64max; const EnumDescriptor* enum_type = field->enum_type(); - const EnumValueDescriptor* enum_value = NULL; + const EnumValueDescriptor* enum_value = nullptr; if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { DO(ConsumeIdentifier(&value)); @@ -763,9 +776,8 @@ label_skip_parsing: } else if (LookingAt("-") || LookingAtType(io::Tokenizer::TYPE_INTEGER)) { - int64 int_value; DO(ConsumeSignedInteger(&int_value, kint32max)); - value = SimpleItoa(int_value); // for error reporting + value = StrCat(int_value); // for error reporting enum_value = enum_type->FindValueByNumber(int_value); } else { ReportError("Expected integer or identifier, got: " + @@ -773,14 +785,22 @@ label_skip_parsing: return false; } - if (enum_value == NULL) { - if (!allow_unknown_enum_) { - ReportError("Unknown enumeration value of \"" + value + "\" for " - "field \"" + field->name() + "\"."); + if (enum_value == nullptr) { + if (int_value != kint64max && + reflection->SupportsUnknownEnumValues()) { + SET_FIELD(EnumValue, int_value); + return true; + } else if (!allow_unknown_enum_) { + ReportError("Unknown enumeration value of \"" + value + + "\" for " + "field \"" + + field->name() + "\"."); return false; } else { - ReportWarning("Unknown enumeration value of \"" + value + "\" for " - "field \"" + field->name() + "\"."); + ReportWarning("Unknown enumeration value of \"" + value + + "\" for " + "field \"" + + field->name() + "\"."); return true; } } @@ -846,6 +866,8 @@ label_skip_parsing: if (!LookingAtType(io::Tokenizer::TYPE_INTEGER) && !LookingAtType(io::Tokenizer::TYPE_FLOAT) && !LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { + std::string text = tokenizer_.current().text; + ReportError("Cannot skip field value, unexpected token: " + text); return false; } // Combination of '-' and TYPE_IDENTIFIER may result in an invalid field @@ -855,11 +877,10 @@ label_skip_parsing: // below: // inf, inff, infinity, nan if (has_minus && LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { - string text = tokenizer_.current().text; + std::string text = tokenizer_.current().text; LowerString(&text); if (text != "inf" && - text != "infinity" && - text != "nan") { + text != "infinity" && text != "nan") { ReportError("Invalid float number: " + text); return false; } @@ -869,7 +890,7 @@ label_skip_parsing: } // Returns true if the current token's text is equal to that specified. - bool LookingAt(const string& text) { + bool LookingAt(const std::string& text) { return tokenizer_.current().text == text; } @@ -880,7 +901,7 @@ label_skip_parsing: // Consumes an identifier and saves its value in the identifier parameter. // Returns false if the token is not of type IDENTFIER. - bool ConsumeIdentifier(string* identifier) { + bool ConsumeIdentifier(std::string* identifier) { if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { *identifier = tokenizer_.current().text; tokenizer_.Next(); @@ -902,10 +923,10 @@ label_skip_parsing: } // Consume a string of form ".....". - bool ConsumeFullTypeName(string* name) { + bool ConsumeFullTypeName(std::string* name) { DO(ConsumeIdentifier(name)); while (TryConsume(".")) { - string part; + std::string part; DO(ConsumeIdentifier(&part)); *name += "."; *name += part; @@ -914,7 +935,7 @@ label_skip_parsing: } bool ConsumeTypeUrlOrFullTypeName() { - string discarded; + std::string discarded; DO(ConsumeIdentifier(&discarded)); while (TryConsume(".") || TryConsume("/")) { DO(ConsumeIdentifier(&discarded)); @@ -924,7 +945,7 @@ label_skip_parsing: // Consumes a string and saves its value in the text parameter. // Returns false if the token is not of type STRING. - bool ConsumeString(string* text) { + bool ConsumeString(std::string* text) { if (!LookingAtType(io::Tokenizer::TYPE_STRING)) { ReportError("Expected string, got: " + tokenizer_.current().text); return false; @@ -948,8 +969,8 @@ label_skip_parsing: return false; } - if (!io::Tokenizer::ParseInteger(tokenizer_.current().text, - max_value, value)) { + if (!io::Tokenizer::ParseInteger(tokenizer_.current().text, max_value, + value)) { ReportError("Integer out of range (" + tokenizer_.current().text + ")"); return false; } @@ -990,23 +1011,26 @@ label_skip_parsing: return true; } - // Consumes a uint64 and saves its value in the value parameter. + // Consumes a double and saves its value in the value parameter. // Accepts decimal numbers only, rejects hex or oct numbers. - bool ConsumeUnsignedDecimalInteger(uint64* value, uint64 max_value) { + bool ConsumeUnsignedDecimalAsDouble(double* value, uint64 max_value) { if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) { ReportError("Expected integer, got: " + tokenizer_.current().text); return false; } - const string& text = tokenizer_.current().text; + const std::string& text = tokenizer_.current().text; if (IsHexNumber(text) || IsOctNumber(text)) { ReportError("Expect a decimal number, got: " + text); return false; } - if (!io::Tokenizer::ParseInteger(text, max_value, value)) { - ReportError("Integer out of range (" + text + ")"); - return false; + uint64 uint64_value; + if (io::Tokenizer::ParseInteger(text, max_value, &uint64_value)) { + *value = static_cast(uint64_value); + } else { + // Uint64 overflow, attempt to parse as a double instead. + *value = io::Tokenizer::ParseFloat(text); } tokenizer_.Next(); @@ -1029,10 +1053,7 @@ label_skip_parsing: // Therefore, we must check both cases here. if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) { // We have found an integer value for the double. - uint64 integer_value; - DO(ConsumeUnsignedDecimalInteger(&integer_value, kuint64max)); - - *value = static_cast(integer_value); + DO(ConsumeUnsignedDecimalAsDouble(value, kuint64max)); } else if (LookingAtType(io::Tokenizer::TYPE_FLOAT)) { // We have found a float value for the double. *value = io::Tokenizer::ParseFloat(tokenizer_.current().text); @@ -1040,7 +1061,7 @@ label_skip_parsing: // Mark the current token as consumed. tokenizer_.Next(); } else if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { - string text = tokenizer_.current().text; + std::string text = tokenizer_.current().text; LowerString(&text); if (text == "inf" || text == "infinity") { @@ -1067,12 +1088,12 @@ label_skip_parsing: // Consumes Any::type_url value, of form "type.googleapis.com/full.type.Name" // or "type.googleprod.com/full.type.Name" - bool ConsumeAnyTypeUrl(string* full_type_name, string* prefix) { + bool ConsumeAnyTypeUrl(std::string* full_type_name, std::string* prefix) { // TODO(saito) Extend Consume() to consume multiple tokens at once, so that // this code can be written as just DO(Consume(kGoogleApisTypePrefix)). DO(ConsumeIdentifier(prefix)); while (TryConsume(".")) { - string url; + std::string url; DO(ConsumeIdentifier(&url)); *prefix += "." + url; } @@ -1086,14 +1107,14 @@ label_skip_parsing: // A helper function for reconstructing Any::value. Consumes a text of // full_type_name, then serializes it into serialized_value. bool ConsumeAnyValue(const Descriptor* value_descriptor, - string* serialized_value) { + std::string* serialized_value) { DynamicMessageFactory factory; const Message* value_prototype = factory.GetPrototype(value_descriptor); - if (value_prototype == NULL) { + if (value_prototype == nullptr) { return false; } std::unique_ptr value(value_prototype->New()); - string sub_delimiter; + std::string sub_delimiter; DO(ConsumeMessageDelimiter(&sub_delimiter)); DO(ConsumeMessage(value.get(), sub_delimiter)); @@ -1114,12 +1135,12 @@ label_skip_parsing: // Consumes a token and confirms that it matches that specified in the // value parameter. Returns false if the token found does not match that // which was specified. - bool Consume(const string& value) { - const string& current_value = tokenizer_.current().text; + bool Consume(const std::string& value) { + const std::string& current_value = tokenizer_.current().text; if (current_value != value) { - ReportError("Expected \"" + value + "\", found \"" + current_value - + "\"."); + ReportError("Expected \"" + value + "\", found \"" + current_value + + "\"."); return false; } @@ -1130,7 +1151,7 @@ label_skip_parsing: // Attempts to consume the supplied value. Returns false if a the // token found does not match the value specified. - bool TryConsume(const string& value) { + bool TryConsume(const std::string& value) { if (tokenizer_.current().text == value) { tokenizer_.Next(); return true; @@ -1143,16 +1164,16 @@ label_skip_parsing: // collect any base-level parse errors and feed them to the ParserImpl. class ParserErrorCollector : public io::ErrorCollector { public: - explicit ParserErrorCollector(TextFormat::Parser::ParserImpl* parser) : - parser_(parser) { } + explicit ParserErrorCollector(TextFormat::Parser::ParserImpl* parser) + : parser_(parser) {} ~ParserErrorCollector() override {} - void AddError(int line, int column, const string& message) override { + void AddError(int line, int column, const std::string& message) override { parser_->ReportError(line, column, message); } - void AddWarning(int line, int column, const string& message) override { + void AddWarning(int line, int column, const std::string& message) override { parser_->ReportWarning(line, column, message); } @@ -1174,6 +1195,7 @@ label_skip_parsing: const bool allow_unknown_enum_; const bool allow_field_number_; const bool allow_partial_; + int recursion_limit_; bool had_errors_; }; @@ -1185,14 +1207,13 @@ class TextFormat::Printer::TextGenerator public: explicit TextGenerator(io::ZeroCopyOutputStream* output, int initial_indent_level) - : output_(output), - buffer_(NULL), - buffer_size_(0), - at_start_of_line_(true), - failed_(false), - indent_level_(initial_indent_level), - initial_indent_level_(initial_indent_level) { - } + : output_(output), + buffer_(nullptr), + buffer_size_(0), + at_start_of_line_(true), + failed_(false), + indent_level_(initial_indent_level), + initial_indent_level_(initial_indent_level) {} ~TextGenerator() { // Only BackUp() if we're sure we've successfully called Next() at least @@ -1210,8 +1231,7 @@ class TextFormat::Printer::TextGenerator // Reduces the current indent level by two spaces, or crashes if the indent // level is zero. void Outdent() override { - if (indent_level_ == 0 || - indent_level_ < initial_indent_level_) { + if (indent_level_ == 0 || indent_level_ < initial_indent_level_) { GOOGLE_LOG(DFATAL) << " Outdent() without matching Indent()."; return; } @@ -1272,7 +1292,7 @@ class TextFormat::Printer::TextGenerator data += buffer_size_; size -= buffer_size_; } - void* void_buffer = NULL; + void* void_buffer = nullptr; failed_ = !output_->Next(&void_buffer, &buffer_size_); if (failed_) return; buffer_ = reinterpret_cast(void_buffer); @@ -1285,14 +1305,18 @@ class TextFormat::Printer::TextGenerator } void WriteIndent() { - if (indent_level_ == 0) { return; } + if (indent_level_ == 0) { + return; + } GOOGLE_DCHECK(!failed_); int size = 2 * indent_level_; while (size > buffer_size_) { // Data exceeds space in the buffer. Write what we can and request a new // buffer. - memset(buffer_, ' ', buffer_size_); + if (buffer_size_ > 0) { + memset(buffer_, ' ', buffer_size_); + } size -= buffer_size_; void* void_buffer; failed_ = !output_->Next(&void_buffer, &buffer_size_); @@ -1318,35 +1342,44 @@ class TextFormat::Printer::TextGenerator // =========================================================================== // Implementation of the default Finder for extensions. -TextFormat::Finder::~Finder() { -} +TextFormat::Finder::~Finder() {} const FieldDescriptor* TextFormat::Finder::FindExtension( - Message* message, const string& name) const { + Message* message, const std::string& name) const { return DefaultFinderFindExtension(message, name); } -const Descriptor* TextFormat::Finder::FindAnyType(const Message& message, - const string& prefix, - const string& name) const { +const FieldDescriptor* TextFormat::Finder::FindExtensionByNumber( + const Descriptor* descriptor, int number) const { + return DefaultFinderFindExtensionByNumber(descriptor, number); +} + +const Descriptor* TextFormat::Finder::FindAnyType( + const Message& message, const std::string& prefix, + const std::string& name) const { return DefaultFinderFindAnyType(message, prefix, name); } +MessageFactory* TextFormat::Finder::FindExtensionFactory( + const FieldDescriptor* field) const { + return nullptr; +} + // =========================================================================== TextFormat::Parser::Parser() - : error_collector_(NULL), - finder_(NULL), - parse_info_tree_(NULL), - allow_partial_(false), - allow_case_insensitive_field_(false), - allow_unknown_field_(false), - allow_unknown_extension_(false), - allow_unknown_enum_(false), - allow_field_number_(false), - allow_relaxed_whitespace_(false), - allow_singular_overwrites_(false) { -} + : error_collector_(nullptr), + finder_(nullptr), + parse_info_tree_(nullptr), + allow_partial_(false), + allow_case_insensitive_field_(false), + allow_unknown_field_(false), + allow_unknown_extension_(false), + allow_unknown_enum_(false), + allow_field_number_(false), + allow_relaxed_whitespace_(false), + allow_singular_overwrites_(false), + recursion_limit_(std::numeric_limits::max()) {} TextFormat::Parser::~Parser() {} @@ -1356,9 +1389,9 @@ bool CheckParseInputSize(StringPiece input, io::ErrorCollector* error_collector) { if (input.size() > INT_MAX) { error_collector->AddError( - -1, 0, StrCat("Input size too large: ", - static_cast(input.size()), " bytes", - " > ", INT_MAX, " bytes.")); + -1, 0, + StrCat("Input size too large: ", static_cast(input.size()), + " bytes", " > ", INT_MAX, " bytes.")); return false; } return true; @@ -1371,19 +1404,19 @@ bool TextFormat::Parser::Parse(io::ZeroCopyInputStream* input, output->Clear(); ParserImpl::SingularOverwritePolicy overwrites_policy = - allow_singular_overwrites_ - ? ParserImpl::ALLOW_SINGULAR_OVERWRITES - : ParserImpl::FORBID_SINGULAR_OVERWRITES; + allow_singular_overwrites_ ? ParserImpl::ALLOW_SINGULAR_OVERWRITES + : ParserImpl::FORBID_SINGULAR_OVERWRITES; - ParserImpl parser( - output->GetDescriptor(), input, error_collector_, finder_, - parse_info_tree_, overwrites_policy, allow_case_insensitive_field_, - allow_unknown_field_, allow_unknown_extension_, allow_unknown_enum_, - allow_field_number_, allow_relaxed_whitespace_, allow_partial_); + ParserImpl parser(output->GetDescriptor(), input, error_collector_, finder_, + parse_info_tree_, overwrites_policy, + allow_case_insensitive_field_, allow_unknown_field_, + allow_unknown_extension_, allow_unknown_enum_, + allow_field_number_, allow_relaxed_whitespace_, + allow_partial_, recursion_limit_); return MergeUsingImpl(input, output, &parser); } -bool TextFormat::Parser::ParseFromString(const string& input, +bool TextFormat::Parser::ParseFromString(const std::string& input, Message* output) { DO(CheckParseInputSize(input, error_collector_)); io::ArrayInputStream input_stream(input.data(), input.size()); @@ -1398,11 +1431,11 @@ bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input, allow_case_insensitive_field_, allow_unknown_field_, allow_unknown_extension_, allow_unknown_enum_, allow_field_number_, allow_relaxed_whitespace_, - allow_partial_); + allow_partial_, recursion_limit_); return MergeUsingImpl(input, output, &parser); } -bool TextFormat::Parser::MergeFromString(const string& input, +bool TextFormat::Parser::MergeFromString(const std::string& input, Message* output) { DO(CheckParseInputSize(input, error_collector_)); io::ArrayInputStream input_stream(input.data(), input.size()); @@ -1415,7 +1448,7 @@ bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */, ParserImpl* parser_impl) { if (!parser_impl->Parse(output)) return false; if (!allow_partial_ && !output->IsInitialized()) { - std::vector missing_fields; + std::vector missing_fields; output->FindInitializationErrors(&missing_fields); parser_impl->ReportError(-1, 0, "Message missing required fields: " + @@ -1425,17 +1458,16 @@ bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */, return true; } -bool TextFormat::Parser::ParseFieldValueFromString( - const string& input, - const FieldDescriptor* field, - Message* output) { +bool TextFormat::Parser::ParseFieldValueFromString(const std::string& input, + const FieldDescriptor* field, + Message* output) { io::ArrayInputStream input_stream(input.data(), input.size()); ParserImpl parser( output->GetDescriptor(), &input_stream, error_collector_, finder_, parse_info_tree_, ParserImpl::ALLOW_SINGULAR_OVERWRITES, allow_case_insensitive_field_, allow_unknown_field_, allow_unknown_extension_, allow_unknown_enum_, allow_field_number_, - allow_relaxed_whitespace_, allow_partial_); + allow_relaxed_whitespace_, allow_partial_, recursion_limit_); return parser.ParseField(field, output); } @@ -1449,12 +1481,12 @@ bool TextFormat::Parser::ParseFieldValueFromString( return Parser().Merge(input, output); } -/* static */ bool TextFormat::ParseFromString(const string& input, +/* static */ bool TextFormat::ParseFromString(const std::string& input, Message* output) { return Parser().ParseFromString(input, output); } -/* static */ bool TextFormat::MergeFromString(const string& input, +/* static */ bool TextFormat::MergeFromString(const std::string& input, Message* output) { return Parser().MergeFromString(input, output); } @@ -1477,14 +1509,14 @@ class StringBaseTextGenerator : public TextFormat::BaseTextGenerator { // Some compilers do not support ref-qualifiers even in C++11 mode. // Disable the optimization for now and revisit it later. -#if 0 // LANG_CXX11 - string Consume() && { return std::move(output_); } +#if 0 // LANG_CXX11 + std::string Consume() && { return std::move(output_); } #else // !LANG_CXX11 - const string& Get() { return output_; } + const std::string& Get() { return output_; } #endif // LANG_CXX11 private: - string output_; + std::string output_; }; } // namespace @@ -1495,7 +1527,7 @@ class StringBaseTextGenerator : public TextFormat::BaseTextGenerator { TextFormat::FieldValuePrinter::FieldValuePrinter() {} TextFormat::FieldValuePrinter::~FieldValuePrinter() {} -#if 0 // LANG_CXX11 +#if 0 // LANG_CXX11 #define FORWARD_IMPL(fn, ...) \ StringBaseTextGenerator generator; \ delegate_.fn(__VA_ARGS__, &generator); \ @@ -1507,55 +1539,52 @@ TextFormat::FieldValuePrinter::~FieldValuePrinter() {} return generator.Get() #endif // LANG_CXX11 -string TextFormat::FieldValuePrinter::PrintBool(bool val) const { +std::string TextFormat::FieldValuePrinter::PrintBool(bool val) const { FORWARD_IMPL(PrintBool, val); } -string TextFormat::FieldValuePrinter::PrintInt32(int32 val) const { +std::string TextFormat::FieldValuePrinter::PrintInt32(int32 val) const { FORWARD_IMPL(PrintInt32, val); } -string TextFormat::FieldValuePrinter::PrintUInt32(uint32 val) const { +std::string TextFormat::FieldValuePrinter::PrintUInt32(uint32 val) const { FORWARD_IMPL(PrintUInt32, val); } -string TextFormat::FieldValuePrinter::PrintInt64(int64 val) const { +std::string TextFormat::FieldValuePrinter::PrintInt64(int64 val) const { FORWARD_IMPL(PrintInt64, val); } -string TextFormat::FieldValuePrinter::PrintUInt64(uint64 val) const { +std::string TextFormat::FieldValuePrinter::PrintUInt64(uint64 val) const { FORWARD_IMPL(PrintUInt64, val); } -string TextFormat::FieldValuePrinter::PrintFloat(float val) const { +std::string TextFormat::FieldValuePrinter::PrintFloat(float val) const { FORWARD_IMPL(PrintFloat, val); } -string TextFormat::FieldValuePrinter::PrintDouble(double val) const { +std::string TextFormat::FieldValuePrinter::PrintDouble(double val) const { FORWARD_IMPL(PrintDouble, val); } -string TextFormat::FieldValuePrinter::PrintString(const string& val) const { +std::string TextFormat::FieldValuePrinter::PrintString( + const std::string& val) const { FORWARD_IMPL(PrintString, val); } -string TextFormat::FieldValuePrinter::PrintBytes(const string& val) const { +std::string TextFormat::FieldValuePrinter::PrintBytes( + const std::string& val) const { return PrintString(val); } -string TextFormat::FieldValuePrinter::PrintEnum(int32 val, - const string& name) const { +std::string TextFormat::FieldValuePrinter::PrintEnum( + int32 val, const std::string& name) const { FORWARD_IMPL(PrintEnum, val, name); } -string TextFormat::FieldValuePrinter::PrintFieldName( - const Message& message, - const Reflection* reflection, +std::string TextFormat::FieldValuePrinter::PrintFieldName( + const Message& message, const Reflection* reflection, const FieldDescriptor* field) const { FORWARD_IMPL(PrintFieldName, message, reflection, field); } -string TextFormat::FieldValuePrinter::PrintMessageStart( - const Message& message, - int field_index, - int field_count, +std::string TextFormat::FieldValuePrinter::PrintMessageStart( + const Message& message, int field_index, int field_count, bool single_line_mode) const { FORWARD_IMPL(PrintMessageStart, message, field_index, field_count, single_line_mode); } -string TextFormat::FieldValuePrinter::PrintMessageEnd( - const Message& message, - int field_index, - int field_count, +std::string TextFormat::FieldValuePrinter::PrintMessageEnd( + const Message& message, int field_index, int field_count, bool single_line_mode) const { FORWARD_IMPL(PrintMessageEnd, message, field_index, field_count, single_line_mode); @@ -1574,19 +1603,19 @@ void TextFormat::FastFieldValuePrinter::PrintBool( } void TextFormat::FastFieldValuePrinter::PrintInt32( int32 val, BaseTextGenerator* generator) const { - generator->PrintString(SimpleItoa(val)); + generator->PrintString(StrCat(val)); } void TextFormat::FastFieldValuePrinter::PrintUInt32( uint32 val, BaseTextGenerator* generator) const { - generator->PrintString(SimpleItoa(val)); + generator->PrintString(StrCat(val)); } void TextFormat::FastFieldValuePrinter::PrintInt64( int64 val, BaseTextGenerator* generator) const { - generator->PrintString(SimpleItoa(val)); + generator->PrintString(StrCat(val)); } void TextFormat::FastFieldValuePrinter::PrintUInt64( uint64 val, BaseTextGenerator* generator) const { - generator->PrintString(SimpleItoa(val)); + generator->PrintString(StrCat(val)); } void TextFormat::FastFieldValuePrinter::PrintFloat( float val, BaseTextGenerator* generator) const { @@ -1597,18 +1626,18 @@ void TextFormat::FastFieldValuePrinter::PrintDouble( generator->PrintString(SimpleDtoa(val)); } void TextFormat::FastFieldValuePrinter::PrintEnum( - int32 val, const string& name, BaseTextGenerator* generator) const { + int32 val, const std::string& name, BaseTextGenerator* generator) const { generator->PrintString(name); } void TextFormat::FastFieldValuePrinter::PrintString( - const string& val, BaseTextGenerator* generator) const { + const std::string& val, BaseTextGenerator* generator) const { generator->PrintLiteral("\""); generator->PrintString(CEscape(val)); generator->PrintLiteral("\""); } void TextFormat::FastFieldValuePrinter::PrintBytes( - const string& val, BaseTextGenerator* generator) const { + const std::string& val, BaseTextGenerator* generator) const { PrintString(val, generator); } void TextFormat::FastFieldValuePrinter::PrintFieldName( @@ -1622,15 +1651,7 @@ void TextFormat::FastFieldValuePrinter::PrintFieldName( const FieldDescriptor* field, BaseTextGenerator* generator) const { if (field->is_extension()) { generator->PrintLiteral("["); - // We special-case MessageSet elements for compatibility with proto1. - if (field->containing_type()->options().message_set_wire_format() && - field->type() == FieldDescriptor::TYPE_MESSAGE && - field->is_optional() && - field->extension_scope() == field->message_type()) { - generator->PrintString(field->message_type()->full_name()); - } else { - generator->PrintString(field->full_name()); - } + generator->PrintString(field->PrintableNameForExtension()); generator->PrintLiteral("]"); } else if (field->type() == FieldDescriptor::TYPE_GROUP) { // Groups must be serialized with their original capitalization. @@ -1699,24 +1720,24 @@ class FieldValuePrinterWrapper : public TextFormat::FastFieldValuePrinter { TextFormat::BaseTextGenerator* generator) const override { generator->PrintString(delegate_->PrintDouble(val)); } - void PrintString(const string& val, + void PrintString(const std::string& val, TextFormat::BaseTextGenerator* generator) const override { generator->PrintString(delegate_->PrintString(val)); } - void PrintBytes(const string& val, + void PrintBytes(const std::string& val, TextFormat::BaseTextGenerator* generator) const override { generator->PrintString(delegate_->PrintBytes(val)); } - void PrintEnum(int32 val, const string& name, + void PrintEnum(int32 val, const std::string& name, TextFormat::BaseTextGenerator* generator) const override { generator->PrintString(delegate_->PrintEnum(val, name)); } void PrintFieldName(const Message& message, int field_index, int field_count, const Reflection* reflection, const FieldDescriptor* field, - TextFormat::BaseTextGenerator* generator) const { - generator->PrintString(delegate_->PrintFieldName( - message, reflection, field)); + TextFormat::BaseTextGenerator* generator) const override { + generator->PrintString( + delegate_->PrintFieldName(message, reflection, field)); } void PrintFieldName(const Message& message, const Reflection* reflection, const FieldDescriptor* field, @@ -1747,13 +1768,13 @@ class FieldValuePrinterWrapper : public TextFormat::FastFieldValuePrinter { class FastFieldValuePrinterUtf8Escaping : public TextFormat::FastFieldValuePrinter { public: - void PrintString(const string& val, + void PrintString(const std::string& val, TextFormat::BaseTextGenerator* generator) const override { generator->PrintLiteral("\""); generator->PrintString(strings::Utf8SafeCEscape(val)); generator->PrintLiteral("\""); } - void PrintBytes(const string& val, + void PrintBytes(const std::string& val, TextFormat::BaseTextGenerator* generator) const override { return FastFieldValuePrinter::PrintString(val, generator); } @@ -1762,23 +1783,18 @@ class FastFieldValuePrinterUtf8Escaping } // namespace TextFormat::Printer::Printer() - : initial_indent_level_(0), - single_line_mode_(false), - use_field_number_(false), - use_short_repeated_primitives_(false), - hide_unknown_fields_(false), - print_message_fields_in_index_order_(false), - expand_any_(false), - truncate_string_field_longer_than_(0LL), - finder_(NULL) { + : initial_indent_level_(0), + single_line_mode_(false), + use_field_number_(false), + use_short_repeated_primitives_(false), + hide_unknown_fields_(false), + print_message_fields_in_index_order_(false), + expand_any_(false), + truncate_string_field_longer_than_(0LL), + finder_(nullptr) { SetUseUtf8StringEscaping(false); } -TextFormat::Printer::~Printer() { - STLDeleteValues(&custom_printers_); - STLDeleteValues(&custom_message_printers_); -} - void TextFormat::Printer::SetUseUtf8StringEscaping(bool as_utf8) { SetDefaultFieldValuePrinter(as_utf8 ? new FastFieldValuePrinterUtf8Escaping() : new FastFieldValuePrinter()); @@ -1795,38 +1811,54 @@ void TextFormat::Printer::SetDefaultFieldValuePrinter( } bool TextFormat::Printer::RegisterFieldValuePrinter( - const FieldDescriptor* field, - const FieldValuePrinter* printer) { - if (field == NULL || printer == NULL) { + const FieldDescriptor* field, const FieldValuePrinter* printer) { + if (field == nullptr || printer == nullptr) { return false; } - FieldValuePrinterWrapper* const wrapper = - new FieldValuePrinterWrapper(nullptr); - if (custom_printers_.insert(std::make_pair(field, wrapper)).second) { + std::unique_ptr wrapper( + new FieldValuePrinterWrapper(nullptr)); + auto pair = custom_printers_.insert(std::make_pair(field, nullptr)); + if (pair.second) { wrapper->SetDelegate(printer); + pair.first->second = std::move(wrapper); return true; } else { - delete wrapper; return false; } } bool TextFormat::Printer::RegisterFieldValuePrinter( const FieldDescriptor* field, const FastFieldValuePrinter* printer) { - return field != NULL && printer != NULL && - custom_printers_.insert(std::make_pair(field, printer)).second; + if (field == nullptr || printer == nullptr) { + return false; + } + auto pair = custom_printers_.insert(std::make_pair(field, nullptr)); + if (pair.second) { + pair.first->second.reset(printer); + return true; + } else { + return false; + } } bool TextFormat::Printer::RegisterMessagePrinter( const Descriptor* descriptor, const MessagePrinter* printer) { - return descriptor != nullptr && printer != nullptr && - custom_message_printers_.insert(std::make_pair(descriptor, printer)) - .second; + if (descriptor == nullptr || printer == nullptr) { + return false; + } + auto pair = + custom_message_printers_.insert(std::make_pair(descriptor, nullptr)); + if (pair.second) { + pair.first->second.reset(printer); + return true; + } else { + return false; + } } bool TextFormat::Printer::PrintToString(const Message& message, - string* output) const { - GOOGLE_DCHECK(output) << "output specified is NULL"; + std::string* output) const { + GOOGLE_DCHECK(output) << "output specified is nullptr"; output->clear(); io::StringOutputStream output_stream(output); @@ -1835,9 +1867,8 @@ bool TextFormat::Printer::PrintToString(const Message& message, } bool TextFormat::Printer::PrintUnknownFieldsToString( - const UnknownFieldSet& unknown_fields, - string* output) const { - GOOGLE_DCHECK(output) << "output specified is NULL"; + const UnknownFieldSet& unknown_fields, std::string* output) const { + GOOGLE_DCHECK(output) << "output specified is nullptr"; output->clear(); io::StringOutputStream output_stream(output); @@ -1897,9 +1928,9 @@ bool TextFormat::Printer::PrintAny(const Message& message, const Reflection* reflection = message.GetReflection(); // Extract the full type name from the type_url field. - const string& type_url = reflection->GetString(message, type_url_field); - string url_prefix; - string full_type_name; + const std::string& type_url = reflection->GetString(message, type_url_field); + std::string url_prefix; + std::string full_type_name; if (!internal::ParseAnyTypeUrl(type_url, &url_prefix, &full_type_name)) { return false; } @@ -1908,14 +1939,14 @@ bool TextFormat::Printer::PrintAny(const Message& message, const Descriptor* value_descriptor = finder_ ? finder_->FindAnyType(message, url_prefix, full_type_name) : DefaultFinderFindAnyType(message, url_prefix, full_type_name); - if (value_descriptor == NULL) { + if (value_descriptor == nullptr) { GOOGLE_LOG(WARNING) << "Proto type " << type_url << " not found"; return false; } DynamicMessageFactory factory; std::unique_ptr value_message( factory.GetPrototype(value_descriptor)->New()); - string serialized_value = reflection->GetString(message, value_field); + std::string serialized_value = reflection->GetString(message, value_field); if (!value_message->ParseFromString(serialized_value)) { GOOGLE_LOG(WARNING) << type_url << ": failed to parse contents"; return false; @@ -1923,8 +1954,7 @@ bool TextFormat::Printer::PrintAny(const Message& message, generator->PrintLiteral("["); generator->PrintString(type_url); generator->PrintLiteral("]"); - const FastFieldValuePrinter* printer = FindWithDefault( - custom_printers_, value_field, default_field_value_printer_.get()); + const FastFieldValuePrinter* printer = GetFieldPrinter(value_field); printer->PrintMessageStart(message, -1, 0, single_line_mode_, generator); generator->Indent(); Print(*value_message, generator); @@ -1935,19 +1965,37 @@ bool TextFormat::Printer::PrintAny(const Message& message, void TextFormat::Printer::Print(const Message& message, TextGenerator* generator) const { + const Reflection* reflection = message.GetReflection(); + if (!reflection) { + // This message does not provide any way to describe its structure. + // Parse it again in an UnknownFieldSet, and display this instead. + UnknownFieldSet unknown_fields; + { + std::string serialized = message.SerializeAsString(); + io::ArrayInputStream input(serialized.data(), serialized.size()); + unknown_fields.ParseFromZeroCopyStream(&input); + } + PrintUnknownFields(unknown_fields, generator); + return; + } const Descriptor* descriptor = message.GetDescriptor(); auto itr = custom_message_printers_.find(descriptor); if (itr != custom_message_printers_.end()) { itr->second->Print(message, single_line_mode_, generator); return; } - const Reflection* reflection = message.GetReflection(); if (descriptor->full_name() == internal::kAnyFullTypeName && expand_any_ && PrintAny(message, generator)) { return; } std::vector fields; - reflection->ListFields(message, &fields); + if (descriptor->options().map_entry()) { + fields.push_back(descriptor->field(0)); + fields.push_back(descriptor->field(1)); + } else { + reflection->ListFields(message, &fields); + } + if (print_message_fields_in_index_order_) { std::sort(fields.begin(), fields.end(), FieldIndexSorter()); } @@ -1959,12 +2007,11 @@ void TextFormat::Printer::Print(const Message& message, } } -void TextFormat::Printer::PrintFieldValueToString( - const Message& message, - const FieldDescriptor* field, - int index, - string* output) const { - GOOGLE_DCHECK(output) << "output specified is NULL"; +void TextFormat::Printer::PrintFieldValueToString(const Message& message, + const FieldDescriptor* field, + int index, + std::string* output) const { + GOOGLE_DCHECK(output) << "output specified is nullptr"; output->clear(); io::StringOutputStream output_stream(output); @@ -1973,12 +2020,185 @@ void TextFormat::Printer::PrintFieldValueToString( PrintFieldValue(message, message.GetReflection(), field, index, &generator); } +class MapEntryMessageComparator { + public: + explicit MapEntryMessageComparator(const Descriptor* descriptor) + : field_(descriptor->field(0)) {} + + bool operator()(const Message* a, const Message* b) { + const Reflection* reflection = a->GetReflection(); + switch (field_->cpp_type()) { + case FieldDescriptor::CPPTYPE_BOOL: { + bool first = reflection->GetBool(*a, field_); + bool second = reflection->GetBool(*b, field_); + return first < second; + } + case FieldDescriptor::CPPTYPE_INT32: { + int32 first = reflection->GetInt32(*a, field_); + int32 second = reflection->GetInt32(*b, field_); + return first < second; + } + case FieldDescriptor::CPPTYPE_INT64: { + int64 first = reflection->GetInt64(*a, field_); + int64 second = reflection->GetInt64(*b, field_); + return first < second; + } + case FieldDescriptor::CPPTYPE_UINT32: { + uint32 first = reflection->GetUInt32(*a, field_); + uint32 second = reflection->GetUInt32(*b, field_); + return first < second; + } + case FieldDescriptor::CPPTYPE_UINT64: { + uint64 first = reflection->GetUInt64(*a, field_); + uint64 second = reflection->GetUInt64(*b, field_); + return first < second; + } + case FieldDescriptor::CPPTYPE_STRING: { + std::string first = reflection->GetString(*a, field_); + std::string second = reflection->GetString(*b, field_); + return first < second; + } + default: + GOOGLE_LOG(DFATAL) << "Invalid key for map field."; + return true; + } + } + + private: + const FieldDescriptor* field_; +}; + +namespace internal { +class MapFieldPrinterHelper { + public: + // DynamicMapSorter::Sort cannot be used because it enfores syncing with + // repeated field. + static bool SortMap(const Message& message, const Reflection* reflection, + const FieldDescriptor* field, MessageFactory* factory, + std::vector* sorted_map_field); + static void CopyKey(const MapKey& key, Message* message, + const FieldDescriptor* field_desc); + static void CopyValue(const MapValueRef& value, Message* message, + const FieldDescriptor* field_desc); +}; + +// Returns true if elements contained in sorted_map_field need to be released. +bool MapFieldPrinterHelper::SortMap( + const Message& message, const Reflection* reflection, + const FieldDescriptor* field, MessageFactory* factory, + std::vector* sorted_map_field) { + bool need_release = false; + const MapFieldBase& base = *reflection->GetMapData(message, field); + + if (base.IsRepeatedFieldValid()) { + const RepeatedPtrField& map_field = + reflection->GetRepeatedPtrField(message, field); + for (int i = 0; i < map_field.size(); ++i) { + sorted_map_field->push_back( + const_cast*>(&map_field)->Mutable(i)); + } + } else { + // TODO(teboring): For performance, instead of creating map entry message + // for each element, just store map keys and sort them. + const Descriptor* map_entry_desc = field->message_type(); + const Message* prototype = factory->GetPrototype(map_entry_desc); + for (MapIterator iter = + reflection->MapBegin(const_cast(&message), field); + iter != reflection->MapEnd(const_cast(&message), field); + ++iter) { + Message* map_entry_message = prototype->New(); + CopyKey(iter.GetKey(), map_entry_message, map_entry_desc->field(0)); + CopyValue(iter.GetValueRef(), map_entry_message, + map_entry_desc->field(1)); + sorted_map_field->push_back(map_entry_message); + } + need_release = true; + } + + MapEntryMessageComparator comparator(field->message_type()); + std::stable_sort(sorted_map_field->begin(), sorted_map_field->end(), + comparator); + return need_release; +} + +void MapFieldPrinterHelper::CopyKey(const MapKey& key, Message* message, + const FieldDescriptor* field_desc) { + const Reflection* reflection = message->GetReflection(); + switch (field_desc->cpp_type()) { + case FieldDescriptor::CPPTYPE_DOUBLE: + case FieldDescriptor::CPPTYPE_FLOAT: + case FieldDescriptor::CPPTYPE_ENUM: + case FieldDescriptor::CPPTYPE_MESSAGE: + GOOGLE_LOG(ERROR) << "Not supported."; + break; + case FieldDescriptor::CPPTYPE_STRING: + reflection->SetString(message, field_desc, key.GetStringValue()); + return; + case FieldDescriptor::CPPTYPE_INT64: + reflection->SetInt64(message, field_desc, key.GetInt64Value()); + return; + case FieldDescriptor::CPPTYPE_INT32: + reflection->SetInt32(message, field_desc, key.GetInt32Value()); + return; + case FieldDescriptor::CPPTYPE_UINT64: + reflection->SetUInt64(message, field_desc, key.GetUInt64Value()); + return; + case FieldDescriptor::CPPTYPE_UINT32: + reflection->SetUInt32(message, field_desc, key.GetUInt32Value()); + return; + case FieldDescriptor::CPPTYPE_BOOL: + reflection->SetBool(message, field_desc, key.GetBoolValue()); + return; + } +} + +void MapFieldPrinterHelper::CopyValue(const MapValueRef& value, + Message* message, + const FieldDescriptor* field_desc) { + const Reflection* reflection = message->GetReflection(); + switch (field_desc->cpp_type()) { + case FieldDescriptor::CPPTYPE_DOUBLE: + reflection->SetDouble(message, field_desc, value.GetDoubleValue()); + return; + case FieldDescriptor::CPPTYPE_FLOAT: + reflection->SetFloat(message, field_desc, value.GetFloatValue()); + return; + case FieldDescriptor::CPPTYPE_ENUM: + reflection->SetEnumValue(message, field_desc, value.GetEnumValue()); + return; + case FieldDescriptor::CPPTYPE_MESSAGE: { + Message* sub_message = value.GetMessageValue().New(); + sub_message->CopyFrom(value.GetMessageValue()); + reflection->SetAllocatedMessage(message, sub_message, field_desc); + return; + } + case FieldDescriptor::CPPTYPE_STRING: + reflection->SetString(message, field_desc, value.GetStringValue()); + return; + case FieldDescriptor::CPPTYPE_INT64: + reflection->SetInt64(message, field_desc, value.GetInt64Value()); + return; + case FieldDescriptor::CPPTYPE_INT32: + reflection->SetInt32(message, field_desc, value.GetInt32Value()); + return; + case FieldDescriptor::CPPTYPE_UINT64: + reflection->SetUInt64(message, field_desc, value.GetUInt64Value()); + return; + case FieldDescriptor::CPPTYPE_UINT32: + reflection->SetUInt32(message, field_desc, value.GetUInt32Value()); + return; + case FieldDescriptor::CPPTYPE_BOOL: + reflection->SetBool(message, field_desc, value.GetBoolValue()); + return; + } +} +} // namespace internal + void TextFormat::Printer::PrintField(const Message& message, const Reflection* reflection, const FieldDescriptor* field, TextGenerator* generator) const { - if (use_short_repeated_primitives_ && - field->is_repeated() && + if (use_short_repeated_primitives_ && field->is_repeated() && field->cpp_type() != FieldDescriptor::CPPTYPE_STRING && field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { PrintShortRepeatedField(message, reflection, field, generator); @@ -1989,14 +2209,18 @@ void TextFormat::Printer::PrintField(const Message& message, if (field->is_repeated()) { count = reflection->FieldSize(message, field); - } else if (reflection->HasField(message, field)) { + } else if (reflection->HasField(message, field) || + field->containing_type()->options().map_entry()) { count = 1; } - std::vector map_entries; - const bool is_map = field->is_map(); + DynamicMessageFactory factory; + std::vector sorted_map_field; + bool need_release = false; + bool is_map = field->is_map(); if (is_map) { - map_entries = DynamicMapSorter::Sort(message, count, reflection, field); + need_release = internal::MapFieldPrinterHelper::SortMap( + message, reflection, field, &factory, &sorted_map_field); } for (int j = 0; j < count; ++j) { @@ -2005,11 +2229,10 @@ void TextFormat::Printer::PrintField(const Message& message, PrintFieldName(message, field_index, count, reflection, field, generator); if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - const FastFieldValuePrinter* printer = FindWithDefault( - custom_printers_, field, default_field_value_printer_.get()); + const FastFieldValuePrinter* printer = GetFieldPrinter(field); const Message& sub_message = field->is_repeated() - ? (is_map ? *map_entries[j] + ? (is_map ? *sorted_map_field[j] : reflection->GetRepeatedMessage(message, field, j)) : reflection->GetMessage(message, field); printer->PrintMessageStart(sub_message, field_index, count, @@ -2030,6 +2253,12 @@ void TextFormat::Printer::PrintField(const Message& message, } } } + + if (need_release) { + for (int j = 0; j < sorted_map_field.size(); ++j) { + delete sorted_map_field[j]; + } + } } void TextFormat::Printer::PrintShortRepeatedField( @@ -2059,12 +2288,11 @@ void TextFormat::Printer::PrintFieldName(const Message& message, // if use_field_number_ is true, prints field number instead // of field name. if (use_field_number_) { - generator->PrintString(SimpleItoa(field->number())); + generator->PrintString(StrCat(field->number())); return; } - const FastFieldValuePrinter* printer = FindWithDefault( - custom_printers_, field, default_field_value_printer_.get()); + const FastFieldValuePrinter* printer = GetFieldPrinter(field); printer->PrintFieldName(message, field_index, field_count, reflection, field, generator); } @@ -2077,8 +2305,7 @@ void TextFormat::Printer::PrintFieldValue(const Message& message, GOOGLE_DCHECK(field->is_repeated() || (index == -1)) << "Index must be -1 for non-repeated fields"; - const FastFieldValuePrinter* printer = FindWithDefault( - custom_printers_, field, default_field_value_printer_.get()); + const FastFieldValuePrinter* printer = GetFieldPrinter(field); switch (field->cpp_type()) { #define OUTPUT_FIELD(CPPTYPE, METHOD) \ @@ -2090,23 +2317,24 @@ void TextFormat::Printer::PrintFieldValue(const Message& message, generator); \ break - OUTPUT_FIELD( INT32, Int32); - OUTPUT_FIELD( INT64, Int64); + OUTPUT_FIELD(INT32, Int32); + OUTPUT_FIELD(INT64, Int64); OUTPUT_FIELD(UINT32, UInt32); OUTPUT_FIELD(UINT64, UInt64); - OUTPUT_FIELD( FLOAT, Float); + OUTPUT_FIELD(FLOAT, Float); OUTPUT_FIELD(DOUBLE, Double); - OUTPUT_FIELD( BOOL, Bool); + OUTPUT_FIELD(BOOL, Bool); #undef OUTPUT_FIELD case FieldDescriptor::CPPTYPE_STRING: { - string scratch; - const string& value = field->is_repeated() - ? reflection->GetRepeatedStringReference( - message, field, index, &scratch) - : reflection->GetStringReference(message, field, &scratch); - const string* value_to_print = &value; - string truncated_value; + std::string scratch; + const std::string& value = + field->is_repeated() + ? reflection->GetRepeatedStringReference(message, field, index, + &scratch) + : reflection->GetStringReference(message, field, &scratch); + const std::string* value_to_print = &value; + std::string truncated_value; if (truncate_string_field_longer_than_ > 0 && truncate_string_field_longer_than_ < value.size()) { truncated_value = value.substr(0, truncate_string_field_longer_than_) + @@ -2123,12 +2351,13 @@ void TextFormat::Printer::PrintFieldValue(const Message& message, } case FieldDescriptor::CPPTYPE_ENUM: { - int enum_value = field->is_repeated() - ? reflection->GetRepeatedEnumValue(message, field, index) - : reflection->GetEnumValue(message, field); + int enum_value = + field->is_repeated() + ? reflection->GetRepeatedEnumValue(message, field, index) + : reflection->GetEnumValue(message, field); const EnumValueDescriptor* enum_desc = field->enum_type()->FindValueByNumber(enum_value); - if (enum_desc != NULL) { + if (enum_desc != nullptr) { printer->PrintEnum(enum_value, enum_desc->name(), generator); } else { // Ordinarily, enum_desc should not be null, because proto2 has the @@ -2145,8 +2374,8 @@ void TextFormat::Printer::PrintFieldValue(const Message& message, case FieldDescriptor::CPPTYPE_MESSAGE: Print(field->is_repeated() - ? reflection->GetRepeatedMessage(message, field, index) - : reflection->GetMessage(message, field), + ? reflection->GetRepeatedMessage(message, field, index) + : reflection->GetMessage(message, field), generator); break; } @@ -2158,59 +2387,42 @@ void TextFormat::Printer::PrintFieldValue(const Message& message, } /* static */ bool TextFormat::PrintUnknownFields( - const UnknownFieldSet& unknown_fields, - io::ZeroCopyOutputStream* output) { + const UnknownFieldSet& unknown_fields, io::ZeroCopyOutputStream* output) { return Printer().PrintUnknownFields(unknown_fields, output); } -/* static */ bool TextFormat::PrintToString( - const Message& message, string* output) { +/* static */ bool TextFormat::PrintToString(const Message& message, + std::string* output) { return Printer().PrintToString(message, output); } /* static */ bool TextFormat::PrintUnknownFieldsToString( - const UnknownFieldSet& unknown_fields, string* output) { + const UnknownFieldSet& unknown_fields, std::string* output) { return Printer().PrintUnknownFieldsToString(unknown_fields, output); } /* static */ void TextFormat::PrintFieldValueToString( - const Message& message, - const FieldDescriptor* field, - int index, - string* output) { + const Message& message, const FieldDescriptor* field, int index, + std::string* output) { return Printer().PrintFieldValueToString(message, field, index, output); } /* static */ bool TextFormat::ParseFieldValueFromString( - const string& input, - const FieldDescriptor* field, - Message* message) { + const std::string& input, const FieldDescriptor* field, Message* message) { return Parser().ParseFieldValueFromString(input, field, message); } -// Prints an integer as hex with a fixed number of digits dependent on the -// integer type. -template -static string PaddedHex(IntType value) { - string result; - result.reserve(sizeof(value) * 2); - for (int i = sizeof(value) * 2 - 1; i >= 0; i--) { - result.push_back(int_to_hex_digit(value >> (i*4) & 0x0F)); - } - return result; -} - void TextFormat::Printer::PrintUnknownFields( const UnknownFieldSet& unknown_fields, TextGenerator* generator) const { for (int i = 0; i < unknown_fields.field_count(); i++) { const UnknownField& field = unknown_fields.field(i); - string field_number = SimpleItoa(field.number()); + std::string field_number = StrCat(field.number()); switch (field.type()) { case UnknownField::TYPE_VARINT: generator->PrintString(field_number); generator->PrintLiteral(": "); - generator->PrintString(SimpleItoa(field.varint())); + generator->PrintString(StrCat(field.varint())); if (single_line_mode_) { generator->PrintLiteral(" "); } else { @@ -2243,7 +2455,7 @@ void TextFormat::Printer::PrintUnknownFields( } case UnknownField::TYPE_LENGTH_DELIMITED: { generator->PrintString(field_number); - const string& value = field.length_delimited(); + const std::string& value = field.length_delimited(); UnknownFieldSet embedded_unknown_fields; if (!value.empty() && embedded_unknown_fields.ParseFromString(value)) { // This field is parseable as a Message. diff --git a/src/google/protobuf/text_format.h b/src/google/protobuf/text_format.h index df12f13dba..2d755a9d8a 100644 --- a/src/google/protobuf/text_format.h +++ b/src/google/protobuf/text_format.h @@ -59,7 +59,7 @@ namespace google { namespace protobuf { namespace io { - class ErrorCollector; // tokenizer.h +class ErrorCollector; // tokenizer.h } // This class implements protocol buffer text format. Printing and parsing @@ -67,7 +67,7 @@ namespace io { // of messages. // // This class is really a namespace that contains only static methods. -class LIBPROTOBUF_EXPORT TextFormat { +class PROTOBUF_EXPORT TextFormat { public: // Outputs a textual representation of the given message to the given // output stream. Returns false if printing fails. @@ -82,23 +82,22 @@ class LIBPROTOBUF_EXPORT TextFormat { // Like Print(), but outputs directly to a string. // Note: output will be cleared prior to printing, and will be left empty // even if printing fails. Returns false if printing fails. - static bool PrintToString(const Message& message, string* output); + static bool PrintToString(const Message& message, std::string* output); - // Like PrintUnknownFields(), but outputs directly to a string. Returns false - // if printing fails. + // Like PrintUnknownFields(), but outputs directly to a string. Returns + // false if printing fails. static bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields, - string* output); + std::string* output); // Outputs a textual representation of the value of the field supplied on // the message supplied. For non-repeated fields, an index of -1 must // be supplied. Note that this method will print the default value for a // field if it is not set. static void PrintFieldValueToString(const Message& message, - const FieldDescriptor* field, - int index, - string* output); + const FieldDescriptor* field, int index, + std::string* output); - class LIBPROTOBUF_EXPORT BaseTextGenerator { + class PROTOBUF_EXPORT BaseTextGenerator { public: virtual ~BaseTextGenerator(); @@ -108,7 +107,7 @@ class LIBPROTOBUF_EXPORT TextFormat { // Print text to the output stream. virtual void Print(const char* text, size_t size) = 0; - void PrintString(const string& str) { Print(str.data(), str.size()); } + void PrintString(const std::string& str) { Print(str.data(), str.size()); } template void PrintLiteral(const char (&text)[n]) { @@ -120,7 +119,7 @@ class LIBPROTOBUF_EXPORT TextFormat { // string representation. // You can derive from this FastFieldValuePrinter if you want to have fields // to be printed in a different way and register it at the Printer. - class LIBPROTOBUF_EXPORT FastFieldValuePrinter { + class PROTOBUF_EXPORT FastFieldValuePrinter { public: FastFieldValuePrinter(); virtual ~FastFieldValuePrinter(); @@ -131,11 +130,11 @@ class LIBPROTOBUF_EXPORT TextFormat { virtual void PrintUInt64(uint64 val, BaseTextGenerator* generator) const; virtual void PrintFloat(float val, BaseTextGenerator* generator) const; virtual void PrintDouble(double val, BaseTextGenerator* generator) const; - virtual void PrintString(const string& val, + virtual void PrintString(const std::string& val, BaseTextGenerator* generator) const; - virtual void PrintBytes(const string& val, + virtual void PrintBytes(const std::string& val, BaseTextGenerator* generator) const; - virtual void PrintEnum(int32 val, const string& name, + virtual void PrintEnum(int32 val, const std::string& name, BaseTextGenerator* generator) const; virtual void PrintFieldName(const Message& message, int field_index, int field_count, const Reflection* reflection, @@ -156,39 +155,37 @@ class LIBPROTOBUF_EXPORT TextFormat { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastFieldValuePrinter); }; - class LIBPROTOBUF_EXPORT GOOGLE_PROTOBUF_DEPRECATED_MSG( + class PROTOBUF_EXPORT PROTOBUF_DEPRECATED_MSG( "Please use FastFieldValuePrinter") FieldValuePrinter { public: FieldValuePrinter(); virtual ~FieldValuePrinter(); - virtual string PrintBool(bool val) const; - virtual string PrintInt32(int32 val) const; - virtual string PrintUInt32(uint32 val) const; - virtual string PrintInt64(int64 val) const; - virtual string PrintUInt64(uint64 val) const; - virtual string PrintFloat(float val) const; - virtual string PrintDouble(double val) const; - virtual string PrintString(const string& val) const; - virtual string PrintBytes(const string& val) const; - virtual string PrintEnum(int32 val, const string& name) const; - virtual string PrintFieldName(const Message& message, - const Reflection* reflection, - const FieldDescriptor* field) const; - virtual string PrintMessageStart(const Message& message, - int field_index, - int field_count, - bool single_line_mode) const; - virtual string PrintMessageEnd(const Message& message, - int field_index, - int field_count, - bool single_line_mode) const; + virtual std::string PrintBool(bool val) const; + virtual std::string PrintInt32(int32 val) const; + virtual std::string PrintUInt32(uint32 val) const; + virtual std::string PrintInt64(int64 val) const; + virtual std::string PrintUInt64(uint64 val) const; + virtual std::string PrintFloat(float val) const; + virtual std::string PrintDouble(double val) const; + virtual std::string PrintString(const std::string& val) const; + virtual std::string PrintBytes(const std::string& val) const; + virtual std::string PrintEnum(int32 val, const std::string& name) const; + virtual std::string PrintFieldName(const Message& message, + const Reflection* reflection, + const FieldDescriptor* field) const; + virtual std::string PrintMessageStart(const Message& message, + int field_index, int field_count, + bool single_line_mode) const; + virtual std::string PrintMessageEnd(const Message& message, int field_index, + int field_count, + bool single_line_mode) const; private: FastFieldValuePrinter delegate_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldValuePrinter); }; - class LIBPROTOBUF_EXPORT MessagePrinter { + class PROTOBUF_EXPORT MessagePrinter { public: MessagePrinter() {} virtual ~MessagePrinter() {} @@ -201,16 +198,20 @@ class LIBPROTOBUF_EXPORT TextFormat { // Interface that Printers or Parsers can use to find extensions, or types // referenced in Any messages. - class LIBPROTOBUF_EXPORT Finder { + class PROTOBUF_EXPORT Finder { public: virtual ~Finder(); // Try to find an extension of *message by fully-qualified field // name. Returns NULL if no extension is known for this name or number. // The base implementation uses the extensions already known by the message. - virtual const FieldDescriptor* FindExtension( - Message* message, - const string& name) const; + virtual const FieldDescriptor* FindExtension(Message* message, + const std::string& name) const; + + // Similar to FindExtension, but uses a Descriptor and the extension number + // instead of using a Message and the name when doing the look up. + virtual const FieldDescriptor* FindExtensionByNumber( + const Descriptor* descriptor, int number) const; // Find the message type for an Any proto. // Returns NULL if no message is known for this name. @@ -218,16 +219,21 @@ class LIBPROTOBUF_EXPORT TextFormat { // type.googleapis.com/, and searches the DescriptorPool of the parent // message. virtual const Descriptor* FindAnyType(const Message& message, - const string& prefix, - const string& name) const; + const std::string& prefix, + const std::string& name) const; + + // Find the message factory for the given extension field. This can be used + // to generalize the Parser to add extension fields to a message in the same + // way as the "input" message for the Parser. + virtual MessageFactory* FindExtensionFactory( + const FieldDescriptor* field) const; }; // Class for those users which require more fine-grained control over how // a protobuffer message is printed out. - class LIBPROTOBUF_EXPORT Printer { + class PROTOBUF_EXPORT Printer { public: Printer(); - ~Printer(); // Like TextFormat::Print bool Print(const Message& message, io::ZeroCopyOutputStream* output) const; @@ -235,15 +241,14 @@ class LIBPROTOBUF_EXPORT TextFormat { bool PrintUnknownFields(const UnknownFieldSet& unknown_fields, io::ZeroCopyOutputStream* output) const; // Like TextFormat::PrintToString - bool PrintToString(const Message& message, string* output) const; + bool PrintToString(const Message& message, std::string* output) const; // Like TextFormat::PrintUnknownFieldsToString bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields, - string* output) const; + std::string* output) const; // Like TextFormat::PrintFieldValueToString void PrintFieldValueToString(const Message& message, - const FieldDescriptor* field, - int index, - string* output) const; + const FieldDescriptor* field, int index, + std::string* output) const; // Adjust the initial indent level of all output. Each indent level is // equal to two spaces. @@ -257,9 +262,7 @@ class LIBPROTOBUF_EXPORT TextFormat { single_line_mode_ = single_line_mode; } - bool IsInSingleLineMode() const { - return single_line_mode_; - } + bool IsInSingleLineMode() const { return single_line_mode_; } // If use_field_number is true, uses field number instead of field name. void SetUseFieldNumber(bool use_field_number) { @@ -293,9 +296,7 @@ class LIBPROTOBUF_EXPORT TextFormat { // is useful to be able to print the message without unknown fields (e.g. // for the python protobuf version to maintain consistency between its pure // python and c++ implementations). - void SetHideUnknownFields(bool hide) { - hide_unknown_fields_ = hide; - } + void SetHideUnknownFields(bool hide) { hide_unknown_fields_ = hide; } // If print_message_fields_in_index_order is true, fields of a proto message // will be printed using the order defined in source code instead of the @@ -315,18 +316,14 @@ class LIBPROTOBUF_EXPORT TextFormat { // If expand==false, print Any using the default printer. The output will // look like // type_url: "" value: "serialized_content" - void SetExpandAny(bool expand) { - expand_any_ = expand; - } + void SetExpandAny(bool expand) { expand_any_ = expand; } // Set how parser finds message for Any payloads. - void SetFinder(Finder* finder) { - finder_ = finder; - } + void SetFinder(const Finder* finder) { finder_ = finder; } - // If non-zero, we truncate all string fields that are longer than this - // threshold. This is useful when the proto message has very long strings, - // e.g., dump of encoded image file. + // If non-zero, we truncate all string fields that are longer than + // this threshold. This is useful when the proto message has very long + // strings, e.g., dump of encoded image file. // // NOTE(hfgong): Setting a non-zero value breaks round-trip safe // property of TextFormat::Printer. That is, from the printed message, we @@ -394,28 +391,29 @@ class LIBPROTOBUF_EXPORT TextFormat { bool PrintAny(const Message& message, TextGenerator* generator) const; - int initial_indent_level_; + const FastFieldValuePrinter* GetFieldPrinter( + const FieldDescriptor* field) const { + auto it = custom_printers_.find(field); + return it == custom_printers_.end() ? default_field_value_printer_.get() + : it->second.get(); + } + int initial_indent_level_; bool single_line_mode_; - bool use_field_number_; - bool use_short_repeated_primitives_; - bool hide_unknown_fields_; - bool print_message_fields_in_index_order_; - bool expand_any_; - int64 truncate_string_field_longer_than_; std::unique_ptr default_field_value_printer_; - typedef std::map + typedef std::map> CustomPrinterMap; CustomPrinterMap custom_printers_; - typedef std::map + typedef std::map> CustomMessagePrinterMap; CustomMessagePrinterMap custom_message_printers_; @@ -439,18 +437,18 @@ class LIBPROTOBUF_EXPORT TextFormat { // google::protobuf::MessageLite::ParseFromString(). static bool Parse(io::ZeroCopyInputStream* input, Message* output); // Like Parse(), but reads directly from a string. - static bool ParseFromString(const string& input, Message* output); + static bool ParseFromString(const std::string& input, Message* output); // Like Parse(), but the data is merged into the given message, as if // using Message::MergeFrom(). static bool Merge(io::ZeroCopyInputStream* input, Message* output); // Like Merge(), but reads directly from a string. - static bool MergeFromString(const string& input, Message* output); + static bool MergeFromString(const std::string& input, Message* output); // Parse the given text as a single field value and store it into the // given field of the given message. If the field is a repeated field, // the new value will be added to the end - static bool ParseFieldValueFromString(const string& input, + static bool ParseFieldValueFromString(const std::string& input, const FieldDescriptor* field, Message* message); @@ -466,10 +464,11 @@ class LIBPROTOBUF_EXPORT TextFormat { // Data structure which is populated with the locations of each field // value parsed from the text. - class LIBPROTOBUF_EXPORT ParseInfoTree { + class PROTOBUF_EXPORT ParseInfoTree { public: - ParseInfoTree(); - ~ParseInfoTree(); + ParseInfoTree() = default; + ParseInfoTree(const ParseInfoTree&) = delete; + ParseInfoTree& operator=(const ParseInfoTree&) = delete; // Returns the parse location for index-th value of the field in the parsed // text. If none exists, returns a location with line = -1. Index should be @@ -493,22 +492,21 @@ class LIBPROTOBUF_EXPORT TextFormat { ParseInfoTree* CreateNested(const FieldDescriptor* field); // Defines the map from the index-th field descriptor to its parse location. - typedef std::map > LocationMap; + typedef std::map > + LocationMap; // Defines the map from the index-th field descriptor to the nested parse // info tree. typedef std::map > NestedMap; + std::vector>> + NestedMap; LocationMap locations_; NestedMap nested_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParseInfoTree); }; // For more control over parsing, use this class. - class LIBPROTOBUF_EXPORT Parser { + class PROTOBUF_EXPORT Parser { public: Parser(); ~Parser(); @@ -516,11 +514,11 @@ class LIBPROTOBUF_EXPORT TextFormat { // Like TextFormat::Parse(). bool Parse(io::ZeroCopyInputStream* input, Message* output); // Like TextFormat::ParseFromString(). - bool ParseFromString(const string& input, Message* output); + bool ParseFromString(const std::string& input, Message* output); // Like TextFormat::Merge(). bool Merge(io::ZeroCopyInputStream* input, Message* output); // Like TextFormat::MergeFromString(). - bool MergeFromString(const string& input, Message* output); + bool MergeFromString(const std::string& input, Message* output); // Set where to report parse errors. If NULL (the default), errors will // be printed to stderr. @@ -531,21 +529,15 @@ class LIBPROTOBUF_EXPORT TextFormat { // Set how parser finds extensions. If NULL (the default), the // parser will use the standard Reflection object associated with // the message being parsed. - void SetFinder(Finder* finder) { - finder_ = finder; - } + void SetFinder(const Finder* finder) { finder_ = finder; } // Sets where location information about the parse will be written. If NULL // (the default), then no location will be written. - void WriteLocationsTo(ParseInfoTree* tree) { - parse_info_tree_ = tree; - } + void WriteLocationsTo(ParseInfoTree* tree) { parse_info_tree_ = tree; } // Normally parsing fails if, after parsing, output->IsInitialized() // returns false. Call AllowPartialMessage(true) to skip this check. - void AllowPartialMessage(bool allow) { - allow_partial_ = allow; - } + void AllowPartialMessage(bool allow) { allow_partial_ = allow; } // Allow field names to be matched case-insensitively. // This is not advisable if there are fields that only differ in case, or @@ -556,21 +548,28 @@ class LIBPROTOBUF_EXPORT TextFormat { } // Like TextFormat::ParseFieldValueFromString - bool ParseFieldValueFromString(const string& input, + bool ParseFieldValueFromString(const std::string& input, const FieldDescriptor* field, Message* output); // When an unknown extension is met, parsing will fail if this option is set // to false (the default). If true, unknown extensions will be ignored and // a warning message will be generated. - void AllowUnknownExtension(bool allow) { - allow_unknown_extension_ = allow; - } + void AllowUnknownExtension(bool allow) { allow_unknown_extension_ = allow; } + // When an unknown field is met, parsing will fail if this option is set + // to false(the default). If true, unknown fields will be ignored and + // a warning message will be generated. + // Please aware that set this option true may hide some errors (e.g. + // spelling error on field name). Avoid to use this option if possible. + void AllowUnknownField(bool allow) { allow_unknown_field_ = allow; } - void AllowFieldNumber(bool allow) { - allow_field_number_ = allow; - } + + void AllowFieldNumber(bool allow) { allow_field_number_ = allow; } + + // Sets maximum recursion depth which parser can use. This is effectively + // the maximum allowed nesting of proto messages. + void SetRecursionLimit(int limit) { recursion_limit_ = limit; } private: // Forward declaration of an internal class used to parse text @@ -579,8 +578,7 @@ class LIBPROTOBUF_EXPORT TextFormat { // Like TextFormat::Merge(). The provided implementation is used // to do the parsing. - bool MergeUsingImpl(io::ZeroCopyInputStream* input, - Message* output, + bool MergeUsingImpl(io::ZeroCopyInputStream* input, Message* output, ParserImpl* parser_impl); io::ErrorCollector* error_collector_; @@ -594,6 +592,7 @@ class LIBPROTOBUF_EXPORT TextFormat { bool allow_field_number_; bool allow_relaxed_whitespace_; bool allow_singular_overwrites_; + int recursion_limit_; }; @@ -617,7 +616,6 @@ inline void TextFormat::RecordLocation(ParseInfoTree* info_tree, info_tree->RecordLocation(field, location); } - inline TextFormat::ParseInfoTree* TextFormat::CreateNested( ParseInfoTree* info_tree, const FieldDescriptor* field) { return info_tree->CreateNested(field); diff --git a/src/google/protobuf/text_format_unittest.cc b/src/google/protobuf/text_format_unittest.cc index 8df2fe3f83..56801c3467 100644 --- a/src/google/protobuf/text_format_unittest.cc +++ b/src/google/protobuf/text_format_unittest.cc @@ -44,21 +44,26 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include + #include #include #include #include +#include + namespace google { namespace protobuf { @@ -66,18 +71,18 @@ namespace protobuf { namespace text_format_unittest { // A basic string with different escapable characters for testing. -const string kEscapeTestString = - "\"A string with ' characters \n and \r newlines and \t tabs and \001 " - "slashes \\ and multiple spaces"; +const std::string kEscapeTestString = + "\"A string with ' characters \n and \r newlines and \t tabs and \001 " + "slashes \\ and multiple spaces"; // A representation of the above string with all the characters escaped. -const string kEscapeTestStringEscaped = - "\"\\\"A string with \\' characters \\n and \\r newlines " - "and \\t tabs and \\001 slashes \\\\ and multiple spaces\""; +const std::string kEscapeTestStringEscaped = + "\"\\\"A string with \\' characters \\n and \\r newlines " + "and \\t tabs and \\001 slashes \\\\ and multiple spaces\""; class TextFormatTest : public testing::Test { public: - static void SetUpTestCase() { + static void SetUpTestSuite() { GOOGLE_CHECK_OK(File::GetContents( TestUtil::GetTestDataPath( "net/proto2/internal/" @@ -90,17 +95,17 @@ class TextFormatTest : public testing::Test { protected: // Debug string read from text_format_unittest_data.txt. - const string proto_debug_string_; + const std::string proto_debug_string_; unittest::TestAllTypes proto_; private: - static string static_proto_debug_string_; + static std::string static_proto_debug_string_; }; -string TextFormatTest::static_proto_debug_string_; +std::string TextFormatTest::static_proto_debug_string_; class TextFormatExtensionsTest : public testing::Test { public: - static void SetUpTestCase() { + static void SetUpTestSuite() { GOOGLE_CHECK_OK(File::GetContents( TestUtil::GetTestDataPath("net/proto2/internal/testdata/" "text_format_unittest_extensions_data.txt"), @@ -113,14 +118,13 @@ class TextFormatExtensionsTest : public testing::Test { protected: // Debug string read from text_format_unittest_data.txt. - const string proto_debug_string_; + const std::string proto_debug_string_; unittest::TestAllExtensions proto_; private: - static string static_proto_debug_string_; + static std::string static_proto_debug_string_; }; -string TextFormatExtensionsTest::static_proto_debug_string_; - +std::string TextFormatExtensionsTest::static_proto_debug_string_; TEST_F(TextFormatTest, Basic) { TestUtil::SetAllFields(&proto_); @@ -138,10 +142,11 @@ TEST_F(TextFormatTest, ShortDebugString) { proto_.mutable_optional_nested_message()->set_bb(2); proto_.mutable_optional_foreign_message(); - EXPECT_EQ("optional_int32: 1 optional_string: \"hello\" " - "optional_nested_message { bb: 2 } " - "optional_foreign_message { }", - proto_.ShortDebugString()); + EXPECT_EQ( + "optional_int32: 1 optional_string: \"hello\" " + "optional_nested_message { bb: 2 } " + "optional_foreign_message { }", + proto_.ShortDebugString()); } TEST_F(TextFormatTest, ShortPrimitiveRepeateds) { @@ -157,44 +162,47 @@ TEST_F(TextFormatTest, ShortPrimitiveRepeateds) { TextFormat::Printer printer; printer.SetUseShortRepeatedPrimitives(true); - string text; + std::string text; EXPECT_TRUE(printer.PrintToString(proto_, &text)); - EXPECT_EQ("optional_int32: 123\n" - "repeated_int32: [456, 789]\n" - "repeated_string: \"foo\"\n" - "repeated_string: \"bar\"\n" - "repeated_nested_message {\n bb: 2\n}\n" - "repeated_nested_message {\n bb: 3\n}\n" - "repeated_nested_enum: [FOO, BAR]\n", - text); - - // Verify that any existing data in the string is cleared when - // PrintToString() is called. + EXPECT_EQ( + "optional_int32: 123\n" + "repeated_int32: [456, 789]\n" + "repeated_string: \"foo\"\n" + "repeated_string: \"bar\"\n" + "repeated_nested_message {\n bb: 2\n}\n" + "repeated_nested_message {\n bb: 3\n}\n" + "repeated_nested_enum: [FOO, BAR]\n", + text); + + // Verify that any existing data in the string is cleared when PrintToString() + // is called. text = "just some data here...\n\nblah blah"; EXPECT_TRUE(printer.PrintToString(proto_, &text)); - EXPECT_EQ("optional_int32: 123\n" - "repeated_int32: [456, 789]\n" - "repeated_string: \"foo\"\n" - "repeated_string: \"bar\"\n" - "repeated_nested_message {\n bb: 2\n}\n" - "repeated_nested_message {\n bb: 3\n}\n" - "repeated_nested_enum: [FOO, BAR]\n", - text); + EXPECT_EQ( + "optional_int32: 123\n" + "repeated_int32: [456, 789]\n" + "repeated_string: \"foo\"\n" + "repeated_string: \"bar\"\n" + "repeated_nested_message {\n bb: 2\n}\n" + "repeated_nested_message {\n bb: 3\n}\n" + "repeated_nested_enum: [FOO, BAR]\n", + text); // Try in single-line mode. printer.SetSingleLineMode(true); EXPECT_TRUE(printer.PrintToString(proto_, &text)); - EXPECT_EQ("optional_int32: 123 " - "repeated_int32: [456, 789] " - "repeated_string: \"foo\" " - "repeated_string: \"bar\" " - "repeated_nested_message { bb: 2 } " - "repeated_nested_message { bb: 3 } " - "repeated_nested_enum: [FOO, BAR] ", - text); + EXPECT_EQ( + "optional_int32: 123 " + "repeated_int32: [456, 789] " + "repeated_string: \"foo\" " + "repeated_string: \"bar\" " + "repeated_nested_message { bb: 2 } " + "repeated_nested_message { bb: 3 } " + "repeated_nested_enum: [FOO, BAR] ", + text); } @@ -203,13 +211,12 @@ TEST_F(TextFormatTest, StringEscape) { proto_.set_optional_string(kEscapeTestString); // Get the DebugString from the proto. - string debug_string = proto_.DebugString(); - string utf8_debug_string = proto_.Utf8DebugString(); + std::string debug_string = proto_.DebugString(); + std::string utf8_debug_string = proto_.Utf8DebugString(); // Hardcode a correct value to test against. - string correct_string = "optional_string: " - + kEscapeTestStringEscaped - + "\n"; + std::string correct_string = + "optional_string: " + kEscapeTestStringEscaped + "\n"; // Compare. EXPECT_EQ(correct_string, debug_string); @@ -217,8 +224,8 @@ TEST_F(TextFormatTest, StringEscape) { // the protocol buffer contains no UTF-8 text. EXPECT_EQ(correct_string, utf8_debug_string); - string expected_short_debug_string = "optional_string: " - + kEscapeTestStringEscaped; + std::string expected_short_debug_string = + "optional_string: " + kEscapeTestStringEscaped; EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString()); } @@ -228,18 +235,18 @@ TEST_F(TextFormatTest, Utf8DebugString) { proto_.set_optional_bytes("\350\260\267\346\255\214"); // Get the DebugString from the proto. - string debug_string = proto_.DebugString(); - string utf8_debug_string = proto_.Utf8DebugString(); + std::string debug_string = proto_.DebugString(); + std::string utf8_debug_string = proto_.Utf8DebugString(); // Hardcode a correct value to test against. - string correct_utf8_string = + std::string correct_utf8_string = "optional_string: " "\"\350\260\267\346\255\214\"" "\n" "optional_bytes: " "\"\\350\\260\\267\\346\\255\\214\"" "\n"; - string correct_string = + std::string correct_string = "optional_string: " "\"\\350\\260\\267\\346\\255\\214\"" "\n" @@ -269,17 +276,17 @@ TEST_F(TextFormatTest, PrintUnknownFields) { unknown_fields->AddVarint(8, 3); EXPECT_EQ( - "5: 1\n" - "5: 0x00000002\n" - "5: 0x0000000000000003\n" - "5: \"4\"\n" - "5 {\n" - " 10: 5\n" - "}\n" - "8: 1\n" - "8: 2\n" - "8: 3\n", - message.DebugString()); + "5: 1\n" + "5: 0x00000002\n" + "5: 0x0000000000000003\n" + "5: \"4\"\n" + "5 {\n" + " 10: 5\n" + "}\n" + "8: 1\n" + "8: 2\n" + "8: 3\n", + message.DebugString()); } TEST_F(TextFormatTest, PrintUnknownFieldsHidden) { @@ -301,7 +308,7 @@ TEST_F(TextFormatTest, PrintUnknownFieldsHidden) { TextFormat::Printer printer; printer.SetHideUnknownFields(true); - string output; + std::string output; printer.PrintToString(message, &output); EXPECT_EQ("data: \"data\"\n", output); @@ -334,21 +341,21 @@ TEST_F(TextFormatTest, PrintUnknownMessage) { // nested message. message.add_repeated_nested_message()->set_bb(123); - string data; + std::string data; message.SerializeToString(&data); - string text; + std::string text; UnknownFieldSet unknown_fields; EXPECT_TRUE(unknown_fields.ParseFromString(data)); EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text)); EXPECT_EQ( - "44: \"abc\"\n" - "44: \"def\"\n" - "44: \"\"\n" - "48 {\n" - " 1: 123\n" - "}\n", - text); + "44: \"abc\"\n" + "44: \"def\"\n" + "44: \"\"\n" + "48 {\n" + " 1: 123\n" + "}\n", + text); } TEST_F(TextFormatTest, PrintMessageWithIndent) { @@ -360,17 +367,17 @@ TEST_F(TextFormatTest, PrintMessageWithIndent) { message.add_repeated_string("def"); message.add_repeated_nested_message()->set_bb(123); - string text; + std::string text; TextFormat::Printer printer; printer.SetInitialIndentLevel(1); EXPECT_TRUE(printer.PrintToString(message, &text)); EXPECT_EQ( - " repeated_string: \"abc\"\n" - " repeated_string: \"def\"\n" - " repeated_nested_message {\n" - " bb: 123\n" - " }\n", - text); + " repeated_string: \"abc\"\n" + " repeated_string: \"def\"\n" + " repeated_nested_message {\n" + " bb: 123\n" + " }\n", + text); } TEST_F(TextFormatTest, PrintMessageSingleLine) { @@ -382,15 +389,15 @@ TEST_F(TextFormatTest, PrintMessageSingleLine) { message.add_repeated_string("def"); message.add_repeated_nested_message()->set_bb(123); - string text; + std::string text; TextFormat::Printer printer; printer.SetInitialIndentLevel(1); printer.SetSingleLineMode(true); EXPECT_TRUE(printer.PrintToString(message, &text)); EXPECT_EQ( - " repeated_string: \"abc\" repeated_string: \"def\" " - "repeated_nested_message { bb: 123 } ", - text); + " repeated_string: \"abc\" repeated_string: \"def\" " + "repeated_nested_message { bb: 123 } ", + text); } TEST_F(TextFormatTest, PrintBufferTooSmall) { @@ -411,7 +418,7 @@ TEST_F(TextFormatTest, PrintBufferTooSmall) { // A printer that appends 'u' to all unsigned int32. class CustomUInt32FieldValuePrinter : public TextFormat::FieldValuePrinter { public: - virtual string PrintUInt32(uint32 val) const { + virtual std::string PrintUInt32(uint32 val) const { return StrCat(FieldValuePrinter::PrintUInt32(val), "u"); } }; @@ -428,14 +435,14 @@ TEST_F(TextFormatTest, DefaultCustomFieldPrinter) { printer.SetDefaultFieldValuePrinter(new CustomUInt32FieldValuePrinter()); // Let's see if that works well together with the repeated primitives: printer.SetUseShortRepeatedPrimitives(true); - string text; + std::string text; printer.PrintToString(message, &text); EXPECT_EQ("optional_uint32: 42u\nrepeated_uint32: [1u, 2u, 3u]\n", text); } class CustomInt32FieldValuePrinter : public TextFormat::FieldValuePrinter { public: - virtual string PrintInt32(int32 val) const { + virtual std::string PrintInt32(int32 val) const { return StrCat("value-is(", FieldValuePrinter::PrintInt32(val), ")"); } }; @@ -450,7 +457,7 @@ TEST_F(TextFormatTest, FieldSpecificCustomPrinter) { EXPECT_TRUE(printer.RegisterFieldValuePrinter( message.GetDescriptor()->FindFieldByName("optional_int32"), new CustomInt32FieldValuePrinter())); - string text; + std::string text; printer.PrintToString(message, &text); EXPECT_EQ("optional_int32: value-is(42)\nrepeated_int32: 42\n", text); } @@ -486,15 +493,14 @@ TEST_F(TextFormatTest, ErrorCasesRegisteringFieldValuePrinterShouldFail) { class CustomMessageFieldValuePrinter : public TextFormat::FieldValuePrinter { public: - virtual string PrintInt32(int32 v) const { + virtual std::string PrintInt32(int32 v) const { return StrCat(FieldValuePrinter::PrintInt32(v), " # x", strings::Hex(v)); } - virtual string PrintMessageStart(const Message& message, - int field_index, - int field_count, - bool single_line_mode) const { + virtual std::string PrintMessageStart(const Message& message, int field_index, + int field_count, + bool single_line_mode) const { if (single_line_mode) { return " { "; } @@ -514,7 +520,7 @@ TEST_F(TextFormatTest, CustomPrinterForComments) { TextFormat::Printer printer; CustomMessageFieldValuePrinter my_field_printer; printer.SetDefaultFieldValuePrinter(new CustomMessageFieldValuePrinter()); - string text; + std::string text; printer.PrintToString(message, &text); EXPECT_EQ( "optional_nested_message { # NestedMessage: -1\n" @@ -537,10 +543,9 @@ TEST_F(TextFormatTest, CustomPrinterForComments) { class CustomMultilineCommentPrinter : public TextFormat::FieldValuePrinter { public: - virtual string PrintMessageStart(const Message& message, - int field_index, - int field_count, - bool single_line_comment) const { + virtual std::string PrintMessageStart(const Message& message, int field_index, + int field_count, + bool single_line_comment) const { return StrCat(" { # 1\n", " # 2\n"); } }; @@ -552,7 +557,7 @@ TEST_F(TextFormatTest, CustomPrinterForMultilineComments) { TextFormat::Printer printer; CustomMessageFieldValuePrinter my_field_printer; printer.SetDefaultFieldValuePrinter(new CustomMultilineCommentPrinter()); - string text; + std::string text; printer.PrintToString(message, &text); EXPECT_EQ( "optional_nested_message { # 1\n" @@ -623,7 +628,7 @@ TEST_F(TextFormatTest, CompactRepeatedFieldPrinter) { message.add_repeated_nested_message()->set_bb(2); message.add_repeated_nested_message()->set_bb(3); - string text; + std::string text; ASSERT_TRUE(printer.PrintToString(message, &text)); EXPECT_EQ( "repeated_nested_message {\n" @@ -638,12 +643,12 @@ TEST_F(TextFormatTest, CompactRepeatedFieldPrinter) { // BaseTextGenerator::Indent and BaseTextGenerator::Outdent. class MultilineStringPrinter : public TextFormat::FastFieldValuePrinter { public: - void PrintString(const string& val, + void PrintString(const std::string& val, TextFormat::BaseTextGenerator* generator) const override { generator->Indent(); int last_pos = 0; int newline_pos = val.find('\n'); - while (newline_pos != string::npos) { + while (newline_pos != std::string::npos) { generator->PrintLiteral("\n"); TextFormat::FastFieldValuePrinter::PrintString( val.substr(last_pos, newline_pos + 1 - last_pos), generator); @@ -671,7 +676,7 @@ TEST_F(TextFormatTest, MultilineStringPrinter) { protobuf_unittest::TestAllTypes message; message.set_optional_string("first line\nsecond line\nthird line"); - string text; + std::string text; ASSERT_TRUE(printer.PrintToString(message, &text)); EXPECT_EQ( "optional_string: \n" @@ -698,7 +703,7 @@ TEST_F(TextFormatTest, CustomMessagePrinter) { new CustomNestedMessagePrinter); unittest::TestAllTypes message; - string text; + std::string text; EXPECT_TRUE(printer.PrintToString(message, &text)); EXPECT_EQ("", text); @@ -723,8 +728,8 @@ TEST_F(TextFormatExtensionsTest, ParseExtensions) { TEST_F(TextFormatTest, ParseEnumFieldFromNumber) { // Create a parse string with a numerical value for an enum field. - string parse_string = strings::Substitute("optional_nested_enum: $0", - unittest::TestAllTypes::BAZ); + std::string parse_string = strings::Substitute("optional_nested_enum: $0", + unittest::TestAllTypes::BAZ); EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto_)); EXPECT_TRUE(proto_.has_optional_nested_enum()); EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum()); @@ -732,22 +737,52 @@ TEST_F(TextFormatTest, ParseEnumFieldFromNumber) { TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) { ASSERT_LT(unittest::SPARSE_E, 0); - string parse_string = strings::Substitute("sparse_enum: $0", - unittest::SPARSE_E); + std::string parse_string = + strings::Substitute("sparse_enum: $0", unittest::SPARSE_E); unittest::SparseEnumMessage proto; EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto)); EXPECT_TRUE(proto.has_sparse_enum()); EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum()); } +TEST_F(TextFormatTest, PrintUnknownEnumFieldProto3) { + proto3_unittest::TestAllTypes proto; + + proto.add_repeated_nested_enum( + static_cast(10)); + proto.add_repeated_nested_enum( + static_cast(-10)); + proto.add_repeated_nested_enum( + static_cast(2147483647)); + proto.add_repeated_nested_enum( + static_cast(-2147483648)); + + EXPECT_EQ( + "repeated_nested_enum: 10\n" + "repeated_nested_enum: -10\n" + "repeated_nested_enum: 2147483647\n" + "repeated_nested_enum: -2147483648\n", + proto.DebugString()); +} + +TEST_F(TextFormatTest, ParseUnknownEnumFieldProto3) { + proto3_unittest::TestAllTypes proto; + std::string parse_string = + "repeated_nested_enum: [10, -10, 2147483647, -2147483648]"; + EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto)); + ASSERT_EQ(4, proto.repeated_nested_enum_size()); + EXPECT_EQ(10, proto.repeated_nested_enum(0)); + EXPECT_EQ(-10, proto.repeated_nested_enum(1)); + EXPECT_EQ(2147483647, proto.repeated_nested_enum(2)); + EXPECT_EQ(-2147483648, proto.repeated_nested_enum(3)); +} + TEST_F(TextFormatTest, ParseStringEscape) { // Create a parse string with escpaed characters in it. - string parse_string = "optional_string: " - + kEscapeTestStringEscaped - + "\n"; + std::string parse_string = + "optional_string: " + kEscapeTestStringEscaped + "\n"; - io::ArrayInputStream input_stream(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream, &proto_); // Compare. @@ -756,21 +791,20 @@ TEST_F(TextFormatTest, ParseStringEscape) { TEST_F(TextFormatTest, ParseConcatenatedString) { // Create a parse string with multiple parts on one line. - string parse_string = "optional_string: \"foo\" \"bar\"\n"; + std::string parse_string = "optional_string: \"foo\" \"bar\"\n"; - io::ArrayInputStream input_stream1(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream1(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream1, &proto_); // Compare. EXPECT_EQ("foobar", proto_.optional_string()); // Create a parse string with multiple parts on separate lines. - parse_string = "optional_string: \"foo\"\n" - "\"bar\"\n"; + parse_string = + "optional_string: \"foo\"\n" + "\"bar\"\n"; - io::ArrayInputStream input_stream2(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream2(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream2, &proto_); // Compare. @@ -782,10 +816,9 @@ TEST_F(TextFormatTest, ParseFloatWithSuffix) { // end. This is needed for backwards-compatibility with proto1. // Have it parse a float with the 'f' suffix. - string parse_string = "optional_float: 1.0f\n"; + std::string parse_string = "optional_float: 1.0f\n"; - io::ArrayInputStream input_stream(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream, &proto_); @@ -794,7 +827,7 @@ TEST_F(TextFormatTest, ParseFloatWithSuffix) { } TEST_F(TextFormatTest, ParseShortRepeatedForm) { - string parse_string = + std::string parse_string = // Mixed short-form and long-form are simply concatenated. "repeated_int32: 1\n" "repeated_int32: [456, 789]\n" @@ -834,7 +867,7 @@ TEST_F(TextFormatTest, ParseShortRepeatedForm) { } TEST_F(TextFormatTest, ParseShortRepeatedWithTrailingComma) { - string parse_string = "repeated_int32: [456,]\n"; + std::string parse_string = "repeated_int32: [456,]\n"; ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_)); parse_string = "repeated_nested_enum: [ FOO , ]"; ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_)); @@ -846,7 +879,7 @@ TEST_F(TextFormatTest, ParseShortRepeatedWithTrailingComma) { } TEST_F(TextFormatTest, ParseShortRepeatedEmpty) { - string parse_string = + std::string parse_string = "repeated_int32: []\n" "repeated_nested_enum: []\n" "repeated_string: []\n" @@ -863,7 +896,7 @@ TEST_F(TextFormatTest, ParseShortRepeatedEmpty) { } TEST_F(TextFormatTest, ParseShortRepeatedConcatenatedWithEmpty) { - string parse_string = + std::string parse_string = // Starting with empty [] should have no impact. "repeated_int32: []\n" "repeated_nested_enum: []\n" @@ -918,11 +951,11 @@ TEST_F(TextFormatTest, ParseShortRepeatedConcatenatedWithEmpty) { TEST_F(TextFormatTest, Comments) { // Test that comments are ignored. - string parse_string = "optional_int32: 1 # a comment\n" - "optional_int64: 2 # another comment"; + std::string parse_string = + "optional_int32: 1 # a comment\n" + "optional_int64: 2 # another comment"; - io::ArrayInputStream input_stream(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream, &proto_); @@ -935,10 +968,9 @@ TEST_F(TextFormatTest, OptionalColon) { // Test that we can place a ':' after the field name of a nested message, // even though we don't have to. - string parse_string = "optional_nested_message: { bb: 1}\n"; + std::string parse_string = "optional_nested_message: { bb: 1}\n"; - io::ArrayInputStream input_stream(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream, &proto_); @@ -949,7 +981,7 @@ TEST_F(TextFormatTest, OptionalColon) { // Some platforms (e.g. Windows) insist on padding the exponent to three // digits when one or two would be just fine. -static string RemoveRedundantZeros(string text) { +static std::string RemoveRedundantZeros(std::string text) { text = StringReplace(text, "e+0", "e+", true); text = StringReplace(text, "e-0", "e-", true); return text; @@ -963,15 +995,15 @@ TEST_F(TextFormatTest, PrintExotic) { // 9223372036854775808 is outside the range of int64. However, it is not // outside the range of uint64. Confusingly, this means that everything // works if we make the literal unsigned, even though we are negating it. - message.add_repeated_int64(-GOOGLE_ULONGLONG(9223372036854775808)); - message.add_repeated_uint64(GOOGLE_ULONGLONG(18446744073709551615)); + message.add_repeated_int64(-PROTOBUF_ULONGLONG(9223372036854775808)); + message.add_repeated_uint64(PROTOBUF_ULONGLONG(18446744073709551615)); message.add_repeated_double(123.456); message.add_repeated_double(1.23e21); message.add_repeated_double(1.23e-18); message.add_repeated_double(std::numeric_limits::infinity()); message.add_repeated_double(-std::numeric_limits::infinity()); message.add_repeated_double(std::numeric_limits::quiet_NaN()); - message.add_repeated_string(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12)); + message.add_repeated_string(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12)); // Fun story: We used to use 1.23e22 instead of 1.23e21 above, but this // seemed to trigger an odd case on MinGW/GCC 3.4.5 where GCC's parsing of @@ -985,16 +1017,17 @@ TEST_F(TextFormatTest, PrintExotic) { // have this problem, so we switched to that instead. EXPECT_EQ( - "repeated_int64: -9223372036854775808\n" - "repeated_uint64: 18446744073709551615\n" - "repeated_double: 123.456\n" - "repeated_double: 1.23e+21\n" - "repeated_double: 1.23e-18\n" - "repeated_double: inf\n" - "repeated_double: -inf\n" - "repeated_double: nan\n" - "repeated_string: \"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n", - RemoveRedundantZeros(message.DebugString())); + "repeated_int64: -9223372036854775808\n" + "repeated_uint64: 18446744073709551615\n" + "repeated_double: 123.456\n" + "repeated_double: 1.23e+21\n" + "repeated_double: 1.23e-18\n" + "repeated_double: inf\n" + "repeated_double: -inf\n" + "repeated_double: nan\n" + "repeated_string: " + "\"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n", + RemoveRedundantZeros(message.DebugString())); } TEST_F(TextFormatTest, PrintFloatPrecision) { @@ -1041,49 +1074,48 @@ TEST_F(TextFormatTest, PrintFloatPrecision) { message.add_repeated_double(1.23456789876543e100); EXPECT_EQ( - "repeated_float: 1\n" - "repeated_float: 1.2\n" - "repeated_float: 1.23\n" - "repeated_float: 1.234\n" - "repeated_float: 1.2345\n" - "repeated_float: 1.23456\n" - "repeated_float: 1.2e+10\n" - "repeated_float: 1.23e+10\n" - "repeated_float: 1.234e+10\n" - "repeated_float: 1.2345e+10\n" - "repeated_float: 1.23456e+10\n" - "repeated_double: 1.2\n" - "repeated_double: 1.23\n" - "repeated_double: 1.234\n" - "repeated_double: 1.2345\n" - "repeated_double: 1.23456\n" - "repeated_double: 1.234567\n" - "repeated_double: 1.2345678\n" - "repeated_double: 1.23456789\n" - "repeated_double: 1.234567898\n" - "repeated_double: 1.2345678987\n" - "repeated_double: 1.23456789876\n" - "repeated_double: 1.234567898765\n" - "repeated_double: 1.2345678987654\n" - "repeated_double: 1.23456789876543\n" - "repeated_double: 1.2e+100\n" - "repeated_double: 1.23e+100\n" - "repeated_double: 1.234e+100\n" - "repeated_double: 1.2345e+100\n" - "repeated_double: 1.23456e+100\n" - "repeated_double: 1.234567e+100\n" - "repeated_double: 1.2345678e+100\n" - "repeated_double: 1.23456789e+100\n" - "repeated_double: 1.234567898e+100\n" - "repeated_double: 1.2345678987e+100\n" - "repeated_double: 1.23456789876e+100\n" - "repeated_double: 1.234567898765e+100\n" - "repeated_double: 1.2345678987654e+100\n" - "repeated_double: 1.23456789876543e+100\n", - RemoveRedundantZeros(message.DebugString())); + "repeated_float: 1\n" + "repeated_float: 1.2\n" + "repeated_float: 1.23\n" + "repeated_float: 1.234\n" + "repeated_float: 1.2345\n" + "repeated_float: 1.23456\n" + "repeated_float: 1.2e+10\n" + "repeated_float: 1.23e+10\n" + "repeated_float: 1.234e+10\n" + "repeated_float: 1.2345e+10\n" + "repeated_float: 1.23456e+10\n" + "repeated_double: 1.2\n" + "repeated_double: 1.23\n" + "repeated_double: 1.234\n" + "repeated_double: 1.2345\n" + "repeated_double: 1.23456\n" + "repeated_double: 1.234567\n" + "repeated_double: 1.2345678\n" + "repeated_double: 1.23456789\n" + "repeated_double: 1.234567898\n" + "repeated_double: 1.2345678987\n" + "repeated_double: 1.23456789876\n" + "repeated_double: 1.234567898765\n" + "repeated_double: 1.2345678987654\n" + "repeated_double: 1.23456789876543\n" + "repeated_double: 1.2e+100\n" + "repeated_double: 1.23e+100\n" + "repeated_double: 1.234e+100\n" + "repeated_double: 1.2345e+100\n" + "repeated_double: 1.23456e+100\n" + "repeated_double: 1.234567e+100\n" + "repeated_double: 1.2345678e+100\n" + "repeated_double: 1.23456789e+100\n" + "repeated_double: 1.234567898e+100\n" + "repeated_double: 1.2345678987e+100\n" + "repeated_double: 1.23456789876e+100\n" + "repeated_double: 1.234567898765e+100\n" + "repeated_double: 1.2345678987654e+100\n" + "repeated_double: 1.23456789876543e+100\n", + RemoveRedundantZeros(message.DebugString())); } - TEST_F(TextFormatTest, AllowPartial) { unittest::TestRequired message; TextFormat::Parser parser; @@ -1097,29 +1129,29 @@ TEST_F(TextFormatTest, AllowPartial) { TEST_F(TextFormatTest, ParseExotic) { unittest::TestAllTypes message; ASSERT_TRUE(TextFormat::ParseFromString( - "repeated_int32: -1\n" - "repeated_int32: -2147483648\n" - "repeated_int64: -1\n" - "repeated_int64: -9223372036854775808\n" - "repeated_uint32: 4294967295\n" - "repeated_uint32: 2147483648\n" - "repeated_uint64: 18446744073709551615\n" - "repeated_uint64: 9223372036854775808\n" - "repeated_double: 123.0\n" - "repeated_double: 123.5\n" - "repeated_double: 0.125\n" - "repeated_double: 1.23E17\n" - "repeated_double: 1.235E+22\n" - "repeated_double: 1.235e-18\n" - "repeated_double: 123.456789\n" - "repeated_double: inf\n" - "repeated_double: Infinity\n" - "repeated_double: -inf\n" - "repeated_double: -Infinity\n" - "repeated_double: nan\n" - "repeated_double: NaN\n" - "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n", - &message)); + "repeated_int32: -1\n" + "repeated_int32: -2147483648\n" + "repeated_int64: -1\n" + "repeated_int64: -9223372036854775808\n" + "repeated_uint32: 4294967295\n" + "repeated_uint32: 2147483648\n" + "repeated_uint64: 18446744073709551615\n" + "repeated_uint64: 9223372036854775808\n" + "repeated_double: 123.0\n" + "repeated_double: 123.5\n" + "repeated_double: 0.125\n" + "repeated_double: 1.23E17\n" + "repeated_double: 1.235E+22\n" + "repeated_double: 1.235e-18\n" + "repeated_double: 123.456789\n" + "repeated_double: inf\n" + "repeated_double: Infinity\n" + "repeated_double: -inf\n" + "repeated_double: -Infinity\n" + "repeated_double: nan\n" + "repeated_double: NaN\n" + "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n", + &message)); ASSERT_EQ(2, message.repeated_int32_size()); EXPECT_EQ(-1, message.repeated_int32(0)); @@ -1137,23 +1169,26 @@ TEST_F(TextFormatTest, ParseExotic) { // 9223372036854775808 is outside the range of int64. However, it is not // outside the range of uint64. Confusingly, this means that everything // works if we make the literal unsigned, even though we are negating it. - EXPECT_EQ(-GOOGLE_ULONGLONG(9223372036854775808), message.repeated_int64(1)); + EXPECT_EQ(-PROTOBUF_ULONGLONG(9223372036854775808), + message.repeated_int64(1)); ASSERT_EQ(2, message.repeated_uint32_size()); EXPECT_EQ(4294967295u, message.repeated_uint32(0)); EXPECT_EQ(2147483648u, message.repeated_uint32(1)); ASSERT_EQ(2, message.repeated_uint64_size()); - EXPECT_EQ(GOOGLE_ULONGLONG(18446744073709551615), message.repeated_uint64(0)); - EXPECT_EQ(GOOGLE_ULONGLONG(9223372036854775808), message.repeated_uint64(1)); + EXPECT_EQ(PROTOBUF_ULONGLONG(18446744073709551615), + message.repeated_uint64(0)); + EXPECT_EQ(PROTOBUF_ULONGLONG(9223372036854775808), + message.repeated_uint64(1)); ASSERT_EQ(13, message.repeated_double_size()); - EXPECT_EQ(123.0 , message.repeated_double(0)); - EXPECT_EQ(123.5 , message.repeated_double(1)); - EXPECT_EQ(0.125 , message.repeated_double(2)); - EXPECT_EQ(1.23E17 , message.repeated_double(3)); - EXPECT_EQ(1.235E22 , message.repeated_double(4)); - EXPECT_EQ(1.235E-18 , message.repeated_double(5)); + EXPECT_EQ(123.0, message.repeated_double(0)); + EXPECT_EQ(123.5, message.repeated_double(1)); + EXPECT_EQ(0.125, message.repeated_double(2)); + EXPECT_EQ(1.23E17, message.repeated_double(3)); + EXPECT_EQ(1.235E22, message.repeated_double(4)); + EXPECT_EQ(1.235E-18, message.repeated_double(5)); EXPECT_EQ(123.456789, message.repeated_double(6)); EXPECT_EQ(message.repeated_double(7), std::numeric_limits::infinity()); @@ -1167,18 +1202,30 @@ TEST_F(TextFormatTest, ParseExotic) { EXPECT_TRUE(MathLimits::IsNaN(message.repeated_double(12))); // Note: Since these string literals have \0's in them, we must explicitly - // pass their sizes to string's constructor. + // pass their sizes to string's constructor. ASSERT_EQ(1, message.repeated_string_size()); - EXPECT_EQ(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12), + EXPECT_EQ(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12), message.repeated_string(0)); + + ASSERT_TRUE( + TextFormat::ParseFromString("repeated_float: 3.4028235e+38\n" + "repeated_float: -3.4028235e+38\n" + "repeated_float: 3.402823567797337e+38\n" + "repeated_float: -3.402823567797337e+38\n", + &message)); + EXPECT_EQ(message.repeated_float(0), std::numeric_limits::max()); + EXPECT_EQ(message.repeated_float(1), -std::numeric_limits::max()); + EXPECT_EQ(message.repeated_float(2), std::numeric_limits::infinity()); + EXPECT_EQ(message.repeated_float(3), -std::numeric_limits::infinity()); + } TEST_F(TextFormatTest, PrintFieldsInIndexOrder) { protobuf_unittest::TestFieldOrderings message; // Fields are listed in index order instead of field number. - message.set_my_string("str"); // Field number 11 - message.set_my_int(12345); // Field number 1 - message.set_my_float(0.999); // Field number 101 + message.set_my_string("str"); // Field number 11 + message.set_my_int(12345); // Field number 1 + message.set_my_float(0.999); // Field number 101 // Extensions are listed based on the order of extension number. // Extension number 12. message @@ -1199,7 +1246,7 @@ TEST_F(TextFormatTest, PrintFieldsInIndexOrder) { *message.MutableExtension(protobuf_unittest::my_extension_string) = "ext_str0"; TextFormat::Printer printer; - string text; + std::string text; // By default, print in field number order. // my_int: 12345 @@ -1260,44 +1307,44 @@ TEST_F(TextFormatTest, PrintFieldsInIndexOrder) { class TextFormatParserTest : public testing::Test { protected: - void ExpectFailure(const string& input, const string& message, int line, - int col) { + void ExpectFailure(const std::string& input, const std::string& message, + int line, int col) { std::unique_ptr proto(new unittest::TestAllTypes); ExpectFailure(input, message, line, col, proto.get()); } - void ExpectFailure(const string& input, const string& message, int line, - int col, Message* proto) { + void ExpectFailure(const std::string& input, const std::string& message, + int line, int col, Message* proto) { ExpectMessage(input, message, line, col, proto, false); } - void ExpectMessage(const string& input, const string& message, int line, - int col, Message* proto, bool expected_result) { - TextFormat::Parser parser; + void ExpectMessage(const std::string& input, const std::string& message, + int line, int col, Message* proto, bool expected_result) { MockErrorCollector error_collector; - parser.RecordErrorsTo(&error_collector); - EXPECT_EQ(expected_result, parser.ParseFromString(input, proto)) + parser_.RecordErrorsTo(&error_collector); + EXPECT_EQ(expected_result, parser_.ParseFromString(input, proto)) << input << " -> " << proto->DebugString(); - EXPECT_EQ(SimpleItoa(line) + ":" + SimpleItoa(col) + - ": " + message + "\n", - error_collector.text_); + EXPECT_EQ( + StrCat(line) + ":" + StrCat(col) + ": " + message + "\n", + error_collector.text_); + parser_.RecordErrorsTo(nullptr); } - void ExpectSuccessAndTree(const string& input, Message* proto, + void ExpectSuccessAndTree(const std::string& input, Message* proto, TextFormat::ParseInfoTree* info_tree) { - TextFormat::Parser parser; MockErrorCollector error_collector; - parser.RecordErrorsTo(&error_collector); - parser.WriteLocationsTo(info_tree); - - EXPECT_TRUE(parser.ParseFromString(input, proto)); + parser_.RecordErrorsTo(&error_collector); + parser_.WriteLocationsTo(info_tree); + EXPECT_TRUE(parser_.ParseFromString(input, proto)); + parser_.WriteLocationsTo(nullptr); + parser_.RecordErrorsTo(nullptr); } - void ExpectLocation(TextFormat::ParseInfoTree* tree, - const Descriptor* d, const string& field_name, - int index, int line, int column) { - TextFormat::ParseLocation location = tree->GetLocation( - d->FindFieldByName(field_name), index); + void ExpectLocation(TextFormat::ParseInfoTree* tree, const Descriptor* d, + const std::string& field_name, int index, int line, + int column) { + TextFormat::ParseLocation location = + tree->GetLocation(d->FindFieldByName(field_name), index); EXPECT_EQ(line, location.line); EXPECT_EQ(column, location.column); } @@ -1309,25 +1356,27 @@ class TextFormatParserTest : public testing::Test { MockErrorCollector() {} ~MockErrorCollector() {} - string text_; + std::string text_; // implements ErrorCollector ------------------------------------- - void AddError(int line, int column, const string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", - line + 1, column + 1, message); + void AddError(int line, int column, const std::string& message) { + strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line + 1, column + 1, + message); } - void AddWarning(int line, int column, const string& message) { + void AddWarning(int line, int column, const std::string& message) { AddError(line, column, "WARNING:" + message); } }; + + TextFormat::Parser parser_; }; TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) { std::unique_ptr message(new unittest::TestAllTypes); const Descriptor* d = message->GetDescriptor(); - string stringData = + std::string stringData = "optional_int32: 1\n" "optional_int64: 2\n" " optional_double: 2.4\n" @@ -1343,7 +1392,6 @@ TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) { " bb: 80\n" ">"; - TextFormat::ParseInfoTree tree; ExpectSuccessAndTree(stringData, message.get(), &tree); @@ -1392,33 +1440,33 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { std::unique_ptr message(new unittest::TestAllTypes); const Descriptor* d = message->GetDescriptor(); -#define EXPECT_FIELD(name, value, valuestring) \ - EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); \ - EXPECT_EQ(value, message->optional_##name()); \ +#define EXPECT_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_EQ(value, message->optional_##name()); \ EXPECT_TRUE(message->has_optional_##name()); -#define EXPECT_BOOL_FIELD(name, value, valuestring) \ - EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); \ - EXPECT_TRUE(message->optional_##name() == value); \ +#define EXPECT_BOOL_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_TRUE(message->optional_##name() == value); \ EXPECT_TRUE(message->has_optional_##name()); -#define EXPECT_FLOAT_FIELD(name, value, valuestring) \ - EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); \ - EXPECT_FLOAT_EQ(value, message->optional_##name()); \ +#define EXPECT_FLOAT_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_FLOAT_EQ(value, message->optional_##name()); \ EXPECT_TRUE(message->has_optional_##name()); -#define EXPECT_DOUBLE_FIELD(name, value, valuestring) \ - EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); \ - EXPECT_DOUBLE_EQ(value, message->optional_##name()); \ +#define EXPECT_DOUBLE_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_DOUBLE_EQ(value, message->optional_##name()); \ EXPECT_TRUE(message->has_optional_##name()); -#define EXPECT_INVALID(name, valuestring) \ +#define EXPECT_INVALID(name, valuestring) \ EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); + valuestring, d->FindFieldByName("optional_" #name), message.get())); // int32 EXPECT_FIELD(int32, 1, "1"); @@ -1503,13 +1551,13 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { // enum EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR"); EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ, - SimpleItoa(unittest::TestAllTypes::BAZ)); + StrCat(unittest::TestAllTypes::BAZ)); EXPECT_INVALID(nested_enum, "FOOBAR"); // message EXPECT_TRUE(TextFormat::ParseFieldValueFromString( - "", d->FindFieldByName("optional_nested_message"), message.get())); - EXPECT_EQ(12, message->optional_nested_message().bb()); \ + "", d->FindFieldByName("optional_nested_message"), message.get())); + EXPECT_EQ(12, message->optional_nested_message().bb()); EXPECT_TRUE(message->has_optional_nested_message()); EXPECT_INVALID(nested_message, "any"); @@ -1520,15 +1568,13 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { #undef EXPECT_INVALID } - TEST_F(TextFormatParserTest, InvalidToken) { - ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -", - 2, 1); + ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -", 2, + 1); - ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1, - 20); - ExpectFailure("\"some string\"", - "Expected identifier, got: \"some string\"", 1, 1); + ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1, 20); + ExpectFailure("\"some string\"", "Expected identifier, got: \"some string\"", + 1, 1); } TEST_F(TextFormatParserTest, InvalidFieldName) { @@ -1578,11 +1624,10 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { // Invalid values for a double/float field. ExpectFailure("optional_double: \"hello\"\n", "Expected double, got: \"hello\"", 1, 18); - ExpectFailure("optional_double: true\n", "Expected double, got: true", 1, - 18); + ExpectFailure("optional_double: true\n", "Expected double, got: true", 1, 18); ExpectFailure("optional_double: !\n", "Expected double, got: !", 1, 18); - ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", - 1, 17); + ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", 1, + 17); // Invalid values for a signed integer field. ExpectFailure("optional_int32: \"hello\"\n", @@ -1590,8 +1635,8 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { ExpectFailure("optional_int32: true\n", "Expected integer, got: true", 1, 17); ExpectFailure("optional_int32: 4.5\n", "Expected integer, got: 4.5", 1, 17); ExpectFailure("optional_int32: !\n", "Expected integer, got: !", 1, 17); - ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", - 1, 16); + ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", 1, + 16); ExpectFailure("optional_int32: 0x80000000\n", "Integer out of range (0x80000000)", 1, 17); ExpectFailure("optional_int64: 0x8000000000000000\n", @@ -1604,13 +1649,13 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { // Invalid values for an unsigned integer field. ExpectFailure("optional_uint64: \"hello\"\n", "Expected integer, got: \"hello\"", 1, 18); - ExpectFailure("optional_uint64: true\n", - "Expected integer, got: true", 1, 18); + ExpectFailure("optional_uint64: true\n", "Expected integer, got: true", 1, + 18); ExpectFailure("optional_uint64: 4.5\n", "Expected integer, got: 4.5", 1, 18); ExpectFailure("optional_uint64: -5\n", "Expected integer, got: -", 1, 18); ExpectFailure("optional_uint64: !\n", "Expected integer, got: !", 1, 18); - ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", - 1, 17); + ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", 1, + 17); ExpectFailure("optional_uint32: 0x100000000\n", "Integer out of range (0x100000000)", 1, 18); ExpectFailure("optional_uint64: 0x10000000000000000\n", @@ -1625,19 +1670,19 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { ExpectFailure( "optional_bool: meh\n", - "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", - 2, 1); + "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", 2, + 1); - ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", - 1, 15); + ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", 1, + 15); // Invalid values for a string field. ExpectFailure("optional_string: true\n", "Expected string, got: true", 1, 18); ExpectFailure("optional_string: 5\n", "Expected string, got: 5", 1, 18); ExpectFailure("optional_string: -7.5\n", "Expected string, got: -", 1, 18); ExpectFailure("optional_string: !\n", "Expected string, got: !", 1, 18); - ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", - 1, 17); + ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", 1, + 17); // Invalid values for an enumeration field. ExpectFailure("optional_nested_enum: \"hello\"\n", @@ -1646,7 +1691,8 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { // Valid token, but enum value is not defined. ExpectFailure("optional_nested_enum: 5\n", "Unknown enumeration value of \"5\" for field " - "\"optional_nested_enum\".", 2, 1); + "\"optional_nested_enum\".", + 2, 1); // We consume the negative sign, so the error position starts one character // later. ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer, got: 7.5", 1, @@ -1654,29 +1700,27 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { ExpectFailure("optional_nested_enum: !\n", "Expected integer or identifier, got: !", 1, 23); - ExpectFailure( - "optional_nested_enum: grah\n", - "Unknown enumeration value of \"grah\" for field " - "\"optional_nested_enum\".", 2, 1); + ExpectFailure("optional_nested_enum: grah\n", + "Unknown enumeration value of \"grah\" for field " + "\"optional_nested_enum\".", + 2, 1); - ExpectFailure( - "optional_nested_enum {\n \n}\n", - "Expected \":\", found \"{\".", 1, 22); + ExpectFailure("optional_nested_enum {\n \n}\n", + "Expected \":\", found \"{\".", 1, 22); } TEST_F(TextFormatParserTest, MessageDelimiters) { // Non-matching delimiters. - ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", - 3, 1); + ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", 3, + 1); // Invalid delimiters. - ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", - 1, 15); + ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", 1, + 15); // Unending message. ExpectFailure("optional_nested_message {\n \nbb: 118\n", - "Expected identifier, got: ", - 4, 1); + "Expected identifier, got: ", 4, 1); } TEST_F(TextFormatParserTest, UnknownExtension) { @@ -1689,23 +1733,22 @@ TEST_F(TextFormatParserTest, UnknownExtension) { TEST_F(TextFormatParserTest, MissingRequired) { unittest::TestRequired message; - ExpectFailure("a: 1", - "Message missing required fields: b, c", - 0, 1, &message); + ExpectFailure("a: 1", "Message missing required fields: b, c", 0, 1, + &message); } TEST_F(TextFormatParserTest, ParseDuplicateRequired) { unittest::TestRequired message; ExpectFailure("a: 1 b: 2 c: 3 a: 1", - "Non-repeated field \"a\" is specified multiple times.", - 1, 17, &message); + "Non-repeated field \"a\" is specified multiple times.", 1, 17, + &message); } TEST_F(TextFormatParserTest, ParseDuplicateOptional) { unittest::ForeignMessage message; ExpectFailure("c: 1 c: 2", - "Non-repeated field \"c\" is specified multiple times.", - 1, 7, &message); + "Non-repeated field \"c\" is specified multiple times.", 1, 7, + &message); } TEST_F(TextFormatParserTest, MergeDuplicateRequired) { @@ -1731,7 +1774,7 @@ TEST_F(TextFormatParserTest, ExplicitDelimiters) { } TEST_F(TextFormatParserTest, PrintErrorsToStderr) { - std::vector errors; + std::vector errors; { ScopedMemoryLog log; @@ -1741,14 +1784,15 @@ TEST_F(TextFormatParserTest, PrintErrorsToStderr) { } ASSERT_EQ(1, errors.size()); - EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " - "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field " - "named \"no_such_field\".", - errors[0]); + EXPECT_EQ( + "Error parsing text-format protobuf_unittest.TestAllTypes: " + "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field " + "named \"no_such_field\".", + errors[0]); } TEST_F(TextFormatParserTest, FailsOnTokenizationError) { - std::vector errors; + std::vector errors; { ScopedMemoryLog log; @@ -1758,16 +1802,34 @@ TEST_F(TextFormatParserTest, FailsOnTokenizationError) { } ASSERT_EQ(1, errors.size()); - EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " - "1:1: Invalid control characters encountered in text.", - errors[0]); + EXPECT_EQ( + "Error parsing text-format protobuf_unittest.TestAllTypes: " + "1:1: Invalid control characters encountered in text.", + errors[0]); } TEST_F(TextFormatParserTest, ParseDeprecatedField) { unittest::TestDeprecatedFields message; ExpectMessage("deprecated_int32: 42", "WARNING:text format contains deprecated field " - "\"deprecated_int32\"", 1, 21, &message, true); + "\"deprecated_int32\"", + 1, 21, &message, true); +} + +TEST_F(TextFormatParserTest, SetRecursionLimit) { + const char* format = "child: { $0 }"; + std::string input; + for (int i = 0; i < 100; ++i) input = strings::Substitute(format, input); + + unittest::NestedTestAllTypes message; + ExpectSuccessAndTree(input, &message, nullptr); + + input = strings::Substitute(format, input); + parser_.SetRecursionLimit(100); + ExpectMessage(input, "Message is too deep", 1, 908, &message, false); + + parser_.SetRecursionLimit(101); + ExpectSuccessAndTree(input, &message, nullptr); } class TextFormatMessageSetTest : public testing::Test { @@ -1775,25 +1837,24 @@ class TextFormatMessageSetTest : public testing::Test { static const char proto_debug_string_[]; }; const char TextFormatMessageSetTest::proto_debug_string_[] = -"message_set {\n" -" [protobuf_unittest.TestMessageSetExtension1] {\n" -" i: 23\n" -" }\n" -" [protobuf_unittest.TestMessageSetExtension2] {\n" -" str: \"foo\"\n" -" }\n" -"}\n"; - + "message_set {\n" + " [protobuf_unittest.TestMessageSetExtension1] {\n" + " i: 23\n" + " }\n" + " [protobuf_unittest.TestMessageSetExtension2] {\n" + " str: \"foo\"\n" + " }\n" + "}\n"; TEST_F(TextFormatMessageSetTest, Serialize) { protobuf_unittest::TestMessageSetContainer proto; protobuf_unittest::TestMessageSetExtension1* item_a = - proto.mutable_message_set()->MutableExtension( - protobuf_unittest::TestMessageSetExtension1::message_set_extension); + proto.mutable_message_set()->MutableExtension( + protobuf_unittest::TestMessageSetExtension1::message_set_extension); item_a->set_i(23); protobuf_unittest::TestMessageSetExtension2* item_b = - proto.mutable_message_set()->MutableExtension( - protobuf_unittest::TestMessageSetExtension2::message_set_extension); + proto.mutable_message_set()->MutableExtension( + protobuf_unittest::TestMessageSetExtension2::message_set_extension); item_b->set_str("foo"); EXPECT_EQ(proto_debug_string_, proto.DebugString()); } @@ -1801,18 +1862,165 @@ TEST_F(TextFormatMessageSetTest, Serialize) { TEST_F(TextFormatMessageSetTest, Deserialize) { protobuf_unittest::TestMessageSetContainer proto; ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto)); - EXPECT_EQ(23, proto.message_set().GetExtension( - protobuf_unittest::TestMessageSetExtension1::message_set_extension).i()); - EXPECT_EQ("foo", proto.message_set().GetExtension( - protobuf_unittest::TestMessageSetExtension2::message_set_extension).str()); + EXPECT_EQ( + 23, + proto.message_set() + .GetExtension( + protobuf_unittest::TestMessageSetExtension1::message_set_extension) + .i()); + EXPECT_EQ( + "foo", + proto.message_set() + .GetExtension( + protobuf_unittest::TestMessageSetExtension2::message_set_extension) + .str()); // Ensure that these are the only entries present. std::vector descriptors; - proto.message_set().GetReflection()->ListFields( - proto.message_set(), &descriptors); + proto.message_set().GetReflection()->ListFields(proto.message_set(), + &descriptors); EXPECT_EQ(2, descriptors.size()); } +TEST(TextFormatUnknownFieldTest, TestUnknownField) { + protobuf_unittest::TestAllTypes proto; + TextFormat::Parser parser; + // Unknown field is not permitted by default. + EXPECT_FALSE(parser.ParseFromString("unknown_field: 12345", &proto)); + EXPECT_FALSE(parser.ParseFromString("12345678: 12345", &proto)); + + parser.AllowUnknownField(true); + EXPECT_TRUE(parser.ParseFromString("unknown_field: 12345", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: -12345", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345f", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345f", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: inf", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: -inf", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: TYPE_STRING", &proto)); + EXPECT_TRUE( + parser.ParseFromString("unknown_field: \"string value\"", &proto)); + // Invalid field value + EXPECT_FALSE(parser.ParseFromString("unknown_field: -TYPE_STRING", &proto)); + // Two or more unknown fields + EXPECT_TRUE( + parser.ParseFromString("unknown_field1: TYPE_STRING\n" + "unknown_field2: 12345", + &proto)); + // Unknown nested message + EXPECT_TRUE( + parser.ParseFromString("unknown_message1: {}\n" + "unknown_message2 {\n" + " unknown_field: 12345\n" + "}\n" + "unknown_message3 <\n" + " unknown_nested_message {\n" + " unknown_field: 12345\n" + " }\n" + ">", + &proto)); + // Unmatched delimeters for message body + EXPECT_FALSE(parser.ParseFromString("unknown_message: {>", &proto)); + // Unknown extension + EXPECT_TRUE( + parser.ParseFromString("[somewhere.unknown_extension1]: 12345\n" + "[somewhere.unknown_extension2] {\n" + " unknown_field: 12345\n" + "}", + &proto)); + // Unknown fields between known fields + ASSERT_TRUE( + parser.ParseFromString("optional_int32: 1\n" + "unknown_field: 12345\n" + "optional_string: \"string\"\n" + "unknown_message { unknown: 0 }\n" + "optional_nested_message { bb: 2 }", + &proto)); + EXPECT_EQ(1, proto.optional_int32()); + EXPECT_EQ("string", proto.optional_string()); + EXPECT_EQ(2, proto.optional_nested_message().bb()); + + // Unknown field with numeric tag number instead of identifier. + EXPECT_TRUE(parser.ParseFromString("12345678: 12345", &proto)); + + // Nested unknown extensions. + EXPECT_TRUE( + parser.ParseFromString("[test.extension1] <\n" + " unknown_nested_message <\n" + " [test.extension2] <\n" + " unknown_field: 12345\n" + " >\n" + " >\n" + ">", + &proto)); + EXPECT_TRUE( + parser.ParseFromString("[test.extension1] {\n" + " unknown_nested_message {\n" + " [test.extension2] {\n" + " unknown_field: 12345\n" + " }\n" + " }\n" + "}", + &proto)); + EXPECT_TRUE( + parser.ParseFromString("[test.extension1] <\n" + " some_unknown_fields: <\n" + " unknown_field: 12345\n" + " >\n" + ">", + &proto)); + EXPECT_TRUE( + parser.ParseFromString("[test.extension1] {\n" + " some_unknown_fields: {\n" + " unknown_field: 12345\n" + " }\n" + "}", + &proto)); + + // Unknown field with compact repetition. + EXPECT_TRUE(parser.ParseFromString("unknown_field: [1, 2]", &proto)); + // Unknown field with compact repetition of some unknown enum. + EXPECT_TRUE(parser.ParseFromString("unknown_field: [VAL1, VAL2]", &proto)); + // Unknown field with compact repetition with sub-message. + EXPECT_TRUE(parser.ParseFromString("unknown_field: [{a:1}, ]", &proto)); +} + +TEST(TextFormatUnknownFieldTest, TestAnyInUnknownField) { + protobuf_unittest::TestAllTypes proto; + TextFormat::Parser parser; + parser.AllowUnknownField(true); + EXPECT_TRUE( + parser.ParseFromString("unknown {\n" + " [type.googleapis.com/foo.bar] {\n" + " }\n" + "}", + &proto)); +} + +TEST(TextFormatUnknownFieldTest, TestUnknownExtension) { + protobuf_unittest::TestAllTypes proto; + TextFormat::Parser parser; + std::string message_with_ext = + "[test.extension1] {\n" + " some_unknown_fields: {\n" + " unknown_field: 12345\n" + " }\n" + "}"; + // Unknown extensions are not permitted by default. + EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto)); + // AllowUnknownField implies AllowUnknownExtension. + parser.AllowUnknownField(true); + EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto)); + + parser.AllowUnknownField(false); + EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto)); + parser.AllowUnknownExtension(true); + EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto)); + // Unknown fields are still not accepted. + EXPECT_FALSE(parser.ParseFromString("unknown_field: 1", &proto)); +} + } // namespace text_format_unittest } // namespace protobuf diff --git a/src/google/protobuf/timestamp.pb.cc b/src/google/protobuf/timestamp.pb.cc index 10857919e7..009320e440 100644 --- a/src/google/protobuf/timestamp.pb.cc +++ b/src/google/protobuf/timestamp.pb.cc @@ -5,126 +5,105 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -namespace google { -namespace protobuf { +PROTOBUF_NAMESPACE_OPEN class TimestampDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Timestamp_default_instance_; -} // namespace protobuf -} // namespace google -static void InitDefaultsTimestamp_google_2fprotobuf_2ftimestamp_2eproto() { +PROTOBUF_NAMESPACE_CLOSE +static void InitDefaultsscc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; { - void* ptr = &::google::protobuf::_Timestamp_default_instance_; - new (ptr) ::google::protobuf::Timestamp(); - ::google::protobuf::internal::OnShutdownDestroyMessage(ptr); + void* ptr = &PROTOBUF_NAMESPACE_ID::_Timestamp_default_instance_; + new (ptr) PROTOBUF_NAMESPACE_ID::Timestamp(); + ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); } - ::google::protobuf::Timestamp::InitAsDefaultInstance(); + PROTOBUF_NAMESPACE_ID::Timestamp::InitAsDefaultInstance(); } -LIBPROTOBUF_EXPORT ::google::protobuf::internal::SCCInfo<0> scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto = - {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsTimestamp_google_2fprotobuf_2ftimestamp_2eproto}, {}}; - -void InitDefaults_google_2fprotobuf_2ftimestamp_2eproto() { - ::google::protobuf::internal::InitSCC(&scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto.base); -} +PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto = + {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsscc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto}, {}}; -::google::protobuf::Metadata file_level_metadata_google_2fprotobuf_2ftimestamp_2eproto[1]; -constexpr ::google::protobuf::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2ftimestamp_2eproto = nullptr; -constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2ftimestamp_2eproto = nullptr; +static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2ftimestamp_2eproto[1]; +static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2ftimestamp_2eproto = nullptr; +static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2ftimestamp_2eproto = nullptr; -const ::google::protobuf::uint32 TableStruct_google_2fprotobuf_2ftimestamp_2eproto::offsets[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { +const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_google_2fprotobuf_2ftimestamp_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { ~0u, // no _has_bits_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Timestamp, _internal_metadata_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Timestamp, _internal_metadata_), ~0u, // no _extensions_ ~0u, // no _oneof_case_ ~0u, // no _weak_field_map_ - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Timestamp, seconds_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(::google::protobuf::Timestamp, nanos_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Timestamp, seconds_), + PROTOBUF_FIELD_OFFSET(PROTOBUF_NAMESPACE_ID::Timestamp, nanos_), }; -static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - { 0, -1, sizeof(::google::protobuf::Timestamp)}, +static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + { 0, -1, sizeof(PROTOBUF_NAMESPACE_ID::Timestamp)}, }; -static ::google::protobuf::Message const * const file_default_instances[] = { - reinterpret_cast(&::google::protobuf::_Timestamp_default_instance_), -}; - -::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_google_2fprotobuf_2ftimestamp_2eproto = { - {}, AddDescriptors_google_2fprotobuf_2ftimestamp_2eproto, "google/protobuf/timestamp.proto", schemas, - file_default_instances, TableStruct_google_2fprotobuf_2ftimestamp_2eproto::offsets, - file_level_metadata_google_2fprotobuf_2ftimestamp_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2ftimestamp_2eproto, file_level_service_descriptors_google_2fprotobuf_2ftimestamp_2eproto, +static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { + reinterpret_cast(&PROTOBUF_NAMESPACE_ID::_Timestamp_default_instance_), }; -::google::protobuf::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ftimestamp_2eproto = { - false, InitDefaults_google_2fprotobuf_2ftimestamp_2eproto, +const char descriptor_table_protodef_google_2fprotobuf_2ftimestamp_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = "\n\037google/protobuf/timestamp.proto\022\017googl" "e.protobuf\"+\n\tTimestamp\022\017\n\007seconds\030\001 \001(\003" "\022\r\n\005nanos\030\002 \001(\005B~\n\023com.google.protobufB\016" "TimestampProtoP\001Z+github.com/golang/prot" "obuf/ptypes/timestamp\370\001\001\242\002\003GPB\252\002\036Google." "Protobuf.WellKnownTypesb\006proto3" -, - "google/protobuf/timestamp.proto", &assign_descriptors_table_google_2fprotobuf_2ftimestamp_2eproto, 231, + ; +static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_deps[1] = { +}; +static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_sccs[1] = { + &scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto.base, +}; +static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_once; +static bool descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_initialized = false; +const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ftimestamp_2eproto = { + &descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_initialized, descriptor_table_protodef_google_2fprotobuf_2ftimestamp_2eproto, "google/protobuf/timestamp.proto", 231, + &descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_once, descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_sccs, descriptor_table_google_2fprotobuf_2ftimestamp_2eproto_deps, 1, 0, + schemas, file_default_instances, TableStruct_google_2fprotobuf_2ftimestamp_2eproto::offsets, + file_level_metadata_google_2fprotobuf_2ftimestamp_2eproto, 1, file_level_enum_descriptors_google_2fprotobuf_2ftimestamp_2eproto, file_level_service_descriptors_google_2fprotobuf_2ftimestamp_2eproto, }; - -void AddDescriptors_google_2fprotobuf_2ftimestamp_2eproto() { - static constexpr ::google::protobuf::internal::InitFunc deps[1] = - { - }; - ::google::protobuf::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2ftimestamp_2eproto, deps, 0); -} // Force running AddDescriptors() at dynamic initialization time. -static bool dynamic_init_dummy_google_2fprotobuf_2ftimestamp_2eproto = []() { AddDescriptors_google_2fprotobuf_2ftimestamp_2eproto(); return true; }(); -namespace google { -namespace protobuf { +static bool dynamic_init_dummy_google_2fprotobuf_2ftimestamp_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2ftimestamp_2eproto), true); +PROTOBUF_NAMESPACE_OPEN // =================================================================== void Timestamp::InitAsDefaultInstance() { } -class Timestamp::HasBitSetters { +class Timestamp::_Internal { public: }; -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -const int Timestamp::kSecondsFieldNumber; -const int Timestamp::kNanosFieldNumber; -#endif // !defined(_MSC_VER) || _MSC_VER >= 1900 - Timestamp::Timestamp() - : ::google::protobuf::Message(), _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { SharedCtor(); // @@protoc_insertion_point(constructor:google.protobuf.Timestamp) } -Timestamp::Timestamp(::google::protobuf::Arena* arena) - : ::google::protobuf::Message(), +Timestamp::Timestamp(::PROTOBUF_NAMESPACE_ID::Arena* arena) + : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(arena) { SharedCtor(); RegisterArenaDtor(arena); // @@protoc_insertion_point(arena_constructor:google.protobuf.Timestamp) } Timestamp::Timestamp(const Timestamp& from) - : ::google::protobuf::Message(), - _internal_metadata_(NULL) { + : ::PROTOBUF_NAMESPACE_ID::Message(), + _internal_metadata_(nullptr) { _internal_metadata_.MergeFrom(from._internal_metadata_); ::memcpy(&seconds_, &from.seconds_, static_cast(reinterpret_cast(&nanos_) - @@ -144,27 +123,27 @@ Timestamp::~Timestamp() { } void Timestamp::SharedDtor() { - GOOGLE_DCHECK(GetArenaNoVirtual() == NULL); + GOOGLE_DCHECK(GetArenaNoVirtual() == nullptr); } void Timestamp::ArenaDtor(void* object) { Timestamp* _this = reinterpret_cast< Timestamp* >(object); (void)_this; } -void Timestamp::RegisterArenaDtor(::google::protobuf::Arena*) { +void Timestamp::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { } void Timestamp::SetCachedSize(int size) const { _cached_size_.Set(size); } const Timestamp& Timestamp::default_instance() { - ::google::protobuf::internal::InitSCC(&::scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto.base); return *internal_default_instance(); } void Timestamp::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.Timestamp) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; @@ -175,80 +154,64 @@ void Timestamp::Clear() { } #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -const char* Timestamp::_InternalParse(const char* begin, const char* end, void* object, - ::google::protobuf::internal::ParseContext* ctx) { - auto msg = static_cast(object); - ::google::protobuf::uint32 size; (void)size; - int depth; (void)depth; - ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end; - auto ptr = begin; - while (ptr < end) { - ::google::protobuf::uint32 tag; - ptr = Varint::Parse32Inline(ptr, &tag); - if (!ptr) goto error; +const char* Timestamp::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; + while (!ctx->Done(&ptr)) { + ::PROTOBUF_NAMESPACE_ID::uint32 tag; + ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); + CHK_(ptr); switch (tag >> 3) { - case 0: goto error; // int64 seconds = 1; - case 1: { - if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int64 value = val; - msg->set_seconds(value); - break; - } + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { + seconds_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; // int32 nanos = 2; - case 2: { - if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual; - ::google::protobuf::uint64 val; - ptr = Varint::Parse64(ptr, &val); - if (!ptr) goto error; - ::google::protobuf::int32 value = val; - msg->set_nanos(value); - break; - } + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { + nanos_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); + } else goto handle_unusual; + continue; default: { - handle_unusual: (void)&&handle_unusual; - if ((tag & 7) == 4) { - if (!ctx->ValidEndGroup(tag)) goto error; - return ptr; + handle_unusual: + if ((tag & 7) == 4 || tag == 0) { + ctx->SetLastTag(tag); + goto success; } - auto res = UnknownFieldParse(tag, {_InternalParse, msg}, - ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx); - ptr = res.first; - if (res.second) return ptr; + ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); + CHK_(ptr != nullptr); + continue; } } // switch } // while +success: return ptr; -error: - return nullptr; -len_delim_till_end: (void)&&len_delim_till_end; - return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg}, - {parser_till_end, object}, size); -group_continues: (void)&&group_continues; - GOOGLE_DCHECK(ptr >= end); - ctx->StoreGroup({_InternalParse, msg}, {parser_till_end, object}, depth); - return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Timestamp::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!GOOGLE_PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure - ::google::protobuf::uint32 tag; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure + ::PROTOBUF_NAMESPACE_ID::uint32 tag; // @@protoc_insertion_point(parse_start:google.protobuf.Timestamp) for (;;) { - ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); + ::std::pair<::PROTOBUF_NAMESPACE_ID::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u); tag = p.first; if (!p.second) goto handle_unusual; - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + switch (::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::GetTagFieldNumber(tag)) { // int64 seconds = 1; case 1: { - if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int64, ::google::protobuf::internal::WireFormatLite::TYPE_INT64>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int64, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT64>( input, &seconds_))); } else { goto handle_unusual; @@ -258,10 +221,10 @@ bool Timestamp::MergePartialFromCodedStream( // int32 nanos = 2; case 2: { - if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) { + if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< + ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &nanos_))); } else { goto handle_unusual; @@ -274,7 +237,7 @@ bool Timestamp::MergePartialFromCodedStream( if (tag == 0) { goto success; } - DO_(::google::protobuf::internal::WireFormat::SkipField( + DO_(::PROTOBUF_NAMESPACE_ID::internal::WireFormat::SkipField( input, tag, _internal_metadata_.mutable_unknown_fields())); break; } @@ -290,49 +253,27 @@ failure: } #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER -void Timestamp::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // @@protoc_insertion_point(serialize_start:google.protobuf.Timestamp) - ::google::protobuf::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // int64 seconds = 1; - if (this->seconds() != 0) { - ::google::protobuf::internal::WireFormatLite::WriteInt64(1, this->seconds(), output); - } - - // int32 nanos = 2; - if (this->nanos() != 0) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->nanos(), output); - } - - if (_internal_metadata_.have_unknown_fields()) { - ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - _internal_metadata_.unknown_fields(), output); - } - // @@protoc_insertion_point(serialize_end:google.protobuf.Timestamp) -} - -::google::protobuf::uint8* Timestamp::InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const { - (void)deterministic; // Unused +::PROTOBUF_NAMESPACE_ID::uint8* Timestamp::InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.Timestamp) - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; // int64 seconds = 1; if (this->seconds() != 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt64ToArray(1, this->seconds(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt64ToArray(1, this->seconds(), target); } // int32 nanos = 2; if (this->nanos() != 0) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->nanos(), target); + stream->EnsureSpace(&target); + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(2, this->nanos(), target); } - if (_internal_metadata_.have_unknown_fields()) { - target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields(), target); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields(), target, stream); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.Timestamp) return target; @@ -342,43 +283,42 @@ size_t Timestamp::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:google.protobuf.Timestamp) size_t total_size = 0; - if (_internal_metadata_.have_unknown_fields()) { - total_size += - ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - _internal_metadata_.unknown_fields()); - } - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; // int64 seconds = 1; if (this->seconds() != 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int64Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int64Size( this->seconds()); } // int32 nanos = 2; if (this->nanos() != 0) { total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int32Size( + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( this->nanos()); } - int cached_size = ::google::protobuf::internal::ToCachedSize(total_size); + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( + _internal_metadata_, total_size, &_cached_size_); + } + int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); SetCachedSize(cached_size); return total_size; } -void Timestamp::MergeFrom(const ::google::protobuf::Message& from) { +void Timestamp::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_merge_from_start:google.protobuf.Timestamp) GOOGLE_DCHECK_NE(&from, this); const Timestamp* source = - ::google::protobuf::DynamicCastToGenerated( + ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( &from); - if (source == NULL) { + if (source == nullptr) { // @@protoc_insertion_point(generalized_merge_from_cast_fail:google.protobuf.Timestamp) - ::google::protobuf::internal::ReflectionOps::Merge(from, this); + ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); } else { // @@protoc_insertion_point(generalized_merge_from_cast_success:google.protobuf.Timestamp) MergeFrom(*source); @@ -389,7 +329,7 @@ void Timestamp::MergeFrom(const Timestamp& from) { // @@protoc_insertion_point(class_specific_merge_from_start:google.protobuf.Timestamp) GOOGLE_DCHECK_NE(&from, this); _internal_metadata_.MergeFrom(from._internal_metadata_); - ::google::protobuf::uint32 cached_has_bits = 0; + ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; (void) cached_has_bits; if (from.seconds() != 0) { @@ -400,7 +340,7 @@ void Timestamp::MergeFrom(const Timestamp& from) { } } -void Timestamp::CopyFrom(const ::google::protobuf::Message& from) { +void Timestamp::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { // @@protoc_insertion_point(generalized_copy_from_start:google.protobuf.Timestamp) if (&from == this) return; Clear(); @@ -418,25 +358,6 @@ bool Timestamp::IsInitialized() const { return true; } -void Timestamp::Swap(Timestamp* other) { - if (other == this) return; - if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { - InternalSwap(other); - } else { - Timestamp* temp = New(GetArenaNoVirtual()); - temp->MergeFrom(*other); - other->CopyFrom(*this); - InternalSwap(temp); - if (GetArenaNoVirtual() == NULL) { - delete temp; - } - } -} -void Timestamp::UnsafeArenaSwap(Timestamp* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); - InternalSwap(other); -} void Timestamp::InternalSwap(Timestamp* other) { using std::swap; _internal_metadata_.Swap(&other->_internal_metadata_); @@ -444,21 +365,18 @@ void Timestamp::InternalSwap(Timestamp* other) { swap(nanos_, other->nanos_); } -::google::protobuf::Metadata Timestamp::GetMetadata() const { - ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_google_2fprotobuf_2ftimestamp_2eproto); - return ::file_level_metadata_google_2fprotobuf_2ftimestamp_2eproto[kIndexInFileMessages]; +::PROTOBUF_NAMESPACE_ID::Metadata Timestamp::GetMetadata() const { + return GetMetadataStatic(); } // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { -template<> GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE ::google::protobuf::Timestamp* Arena::CreateMaybeMessage< ::google::protobuf::Timestamp >(Arena* arena) { - return Arena::CreateMessageInternal< ::google::protobuf::Timestamp >(arena); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_NOINLINE PROTOBUF_NAMESPACE_ID::Timestamp* Arena::CreateMaybeMessage< PROTOBUF_NAMESPACE_ID::Timestamp >(Arena* arena) { + return Arena::CreateMessageInternal< PROTOBUF_NAMESPACE_ID::Timestamp >(arena); } -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) +#include diff --git a/src/google/protobuf/timestamp.pb.h b/src/google/protobuf/timestamp.pb.h index e77adbe0be..4c42992562 100644 --- a/src/google/protobuf/timestamp.pb.h +++ b/src/google/protobuf/timestamp.pb.h @@ -1,24 +1,25 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/timestamp.proto -#ifndef PROTOBUF_INCLUDED_google_2fprotobuf_2ftimestamp_2eproto -#define PROTOBUF_INCLUDED_google_2fprotobuf_2ftimestamp_2eproto +#ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2ftimestamp_2eproto +#define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2ftimestamp_2eproto #include #include -#include -#if GOOGLE_PROTOBUF_VERSION < 3006000 +#include +#if PROTOBUF_VERSION < 3009000 #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 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#if 3009000 < 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 @@ -26,57 +27,61 @@ #include #include #include +#include #include #include // IWYU pragma: export #include // IWYU pragma: export #include // @@protoc_insertion_point(includes) #include -#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftimestamp_2eproto LIBPROTOBUF_EXPORT +#define PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftimestamp_2eproto PROTOBUF_EXPORT +PROTOBUF_NAMESPACE_OPEN +namespace internal { +class AnyMetadata; +} // namespace internal +PROTOBUF_NAMESPACE_CLOSE // Internal implementation detail -- do not use these members. -struct LIBPROTOBUF_EXPORT TableStruct_google_2fprotobuf_2ftimestamp_2eproto { - static const ::google::protobuf::internal::ParseTableField entries[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::AuxillaryParseTableField aux[] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::ParseTable schema[1] - GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold); - static const ::google::protobuf::internal::FieldMetadata field_metadata[]; - static const ::google::protobuf::internal::SerializationTable serialization_table[]; - static const ::google::protobuf::uint32 offsets[]; +struct PROTOBUF_EXPORT TableStruct_google_2fprotobuf_2ftimestamp_2eproto { + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] + PROTOBUF_SECTION_VARIABLE(protodesc_cold); + static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] + 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[]; }; -void LIBPROTOBUF_EXPORT AddDescriptors_google_2fprotobuf_2ftimestamp_2eproto(); -namespace google { -namespace protobuf { +extern PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ftimestamp_2eproto; +PROTOBUF_NAMESPACE_OPEN class Timestamp; class TimestampDefaultTypeInternal; -LIBPROTOBUF_EXPORT extern TimestampDefaultTypeInternal _Timestamp_default_instance_; -template<> LIBPROTOBUF_EXPORT ::google::protobuf::Timestamp* Arena::CreateMaybeMessage<::google::protobuf::Timestamp>(Arena*); -} // namespace protobuf -} // namespace google -namespace google { -namespace protobuf { +PROTOBUF_EXPORT extern TimestampDefaultTypeInternal _Timestamp_default_instance_; +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN +template<> PROTOBUF_EXPORT PROTOBUF_NAMESPACE_ID::Timestamp* Arena::CreateMaybeMessage(Arena*); +PROTOBUF_NAMESPACE_CLOSE +PROTOBUF_NAMESPACE_OPEN // =================================================================== -class LIBPROTOBUF_EXPORT Timestamp : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Timestamp) */ { +class PROTOBUF_EXPORT Timestamp : + public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:google.protobuf.Timestamp) */ { public: Timestamp(); virtual ~Timestamp(); Timestamp(const Timestamp& from); - - inline Timestamp& operator=(const Timestamp& from) { - CopyFrom(from); - return *this; - } - #if LANG_CXX11 Timestamp(Timestamp&& from) noexcept : Timestamp() { *this = ::std::move(from); } + inline Timestamp& operator=(const Timestamp& from) { + CopyFrom(from); + return *this; + } inline Timestamp& operator=(Timestamp&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); @@ -85,15 +90,21 @@ class LIBPROTOBUF_EXPORT Timestamp : public ::google::protobuf::Message /* @@pro } return *this; } - #endif - inline ::google::protobuf::Arena* GetArena() const final { + + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArena() const final { return GetArenaNoVirtual(); } inline void* GetMaybeArenaPointer() const final { return MaybeArenaPtr(); } - static const ::google::protobuf::Descriptor* descriptor() { - return default_instance().GetDescriptor(); + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { + return GetMetadataStatic().descriptor; + } + static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { + return GetMetadataStatic().reflection; } static const Timestamp& default_instance(); @@ -105,54 +116,66 @@ class LIBPROTOBUF_EXPORT Timestamp : public ::google::protobuf::Message /* @@pro static constexpr int kIndexInFileMessages = 0; - void UnsafeArenaSwap(Timestamp* other); - void Swap(Timestamp* other); friend void swap(Timestamp& a, Timestamp& b) { a.Swap(&b); } + inline void Swap(Timestamp* other) { + if (other == this) return; + if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) { + InternalSwap(other); + } else { + ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(Timestamp* other) { + if (other == this) return; + GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual()); + InternalSwap(other); + } // implements Message ---------------------------------------------- inline Timestamp* New() const final { - return CreateMaybeMessage(NULL); + return CreateMaybeMessage(nullptr); } - Timestamp* New(::google::protobuf::Arena* arena) const final { + Timestamp* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { return CreateMaybeMessage(arena); } - void CopyFrom(const ::google::protobuf::Message& from) final; - void MergeFrom(const ::google::protobuf::Message& from) final; + void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; + void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; void CopyFrom(const Timestamp& from); void MergeFrom(const Timestamp& from); - void Clear() final; + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - static const char* _InternalParse(const char* begin, const char* end, void* object, ::google::protobuf::internal::ParseContext* ctx); - ::google::protobuf::internal::ParseFunc _ParseFunc() const final { return _InternalParse; } + const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; #else bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) final; + ::PROTOBUF_NAMESPACE_ID::io::CodedInputStream* input) final; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const final; - ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray( - bool deterministic, ::google::protobuf::uint8* target) const final; + ::PROTOBUF_NAMESPACE_ID::uint8* InternalSerializeWithCachedSizesToArray( + ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const final { return _cached_size_.Get(); } private: - void SharedCtor(); - void SharedDtor(); + inline void SharedCtor(); + inline void SharedDtor(); void SetCachedSize(int size) const final; void InternalSwap(Timestamp* other); + friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; + static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { + return "google.protobuf.Timestamp"; + } protected: - explicit Timestamp(::google::protobuf::Arena* arena); + explicit Timestamp(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::google::protobuf::Arena* arena); + inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); private: - inline ::google::protobuf::Arena* GetArenaNoVirtual() const { + inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { return _internal_metadata_.arena(); } inline void* MaybeArenaPtr() const { @@ -160,35 +183,44 @@ class LIBPROTOBUF_EXPORT Timestamp : public ::google::protobuf::Message /* @@pro } public: - ::google::protobuf::Metadata GetMetadata() const final; + ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; + private: + static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_google_2fprotobuf_2ftimestamp_2eproto); + return ::descriptor_table_google_2fprotobuf_2ftimestamp_2eproto.file_level_metadata[kIndexInFileMessages]; + } + + public: // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { + kSecondsFieldNumber = 1, + kNanosFieldNumber = 2, + }; // int64 seconds = 1; void clear_seconds(); - static const int kSecondsFieldNumber = 1; - ::google::protobuf::int64 seconds() const; - void set_seconds(::google::protobuf::int64 value); + ::PROTOBUF_NAMESPACE_ID::int64 seconds() const; + void set_seconds(::PROTOBUF_NAMESPACE_ID::int64 value); // int32 nanos = 2; void clear_nanos(); - static const int kNanosFieldNumber = 2; - ::google::protobuf::int32 nanos() const; - void set_nanos(::google::protobuf::int32 value); + ::PROTOBUF_NAMESPACE_ID::int32 nanos() const; + void set_nanos(::PROTOBUF_NAMESPACE_ID::int32 value); // @@protoc_insertion_point(class_scope:google.protobuf.Timestamp) private: - class HasBitSetters; + class _Internal; - ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_; - template friend class ::google::protobuf::Arena::InternalHelper; + ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; + template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - ::google::protobuf::int64 seconds_; - ::google::protobuf::int32 nanos_; - mutable ::google::protobuf::internal::CachedSize _cached_size_; + ::PROTOBUF_NAMESPACE_ID::int64 seconds_; + ::PROTOBUF_NAMESPACE_ID::int32 nanos_; + mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; friend struct ::TableStruct_google_2fprotobuf_2ftimestamp_2eproto; }; // =================================================================== @@ -204,13 +236,13 @@ class LIBPROTOBUF_EXPORT Timestamp : public ::google::protobuf::Message /* @@pro // int64 seconds = 1; inline void Timestamp::clear_seconds() { - seconds_ = GOOGLE_PROTOBUF_LONGLONG(0); + seconds_ = PROTOBUF_LONGLONG(0); } -inline ::google::protobuf::int64 Timestamp::seconds() const { +inline ::PROTOBUF_NAMESPACE_ID::int64 Timestamp::seconds() const { // @@protoc_insertion_point(field_get:google.protobuf.Timestamp.seconds) return seconds_; } -inline void Timestamp::set_seconds(::google::protobuf::int64 value) { +inline void Timestamp::set_seconds(::PROTOBUF_NAMESPACE_ID::int64 value) { seconds_ = value; // @@protoc_insertion_point(field_set:google.protobuf.Timestamp.seconds) @@ -220,11 +252,11 @@ inline void Timestamp::set_seconds(::google::protobuf::int64 value) { inline void Timestamp::clear_nanos() { nanos_ = 0; } -inline ::google::protobuf::int32 Timestamp::nanos() const { +inline ::PROTOBUF_NAMESPACE_ID::int32 Timestamp::nanos() const { // @@protoc_insertion_point(field_get:google.protobuf.Timestamp.nanos) return nanos_; } -inline void Timestamp::set_nanos(::google::protobuf::int32 value) { +inline void Timestamp::set_nanos(::PROTOBUF_NAMESPACE_ID::int32 value) { nanos_ = value; // @@protoc_insertion_point(field_set:google.protobuf.Timestamp.nanos) @@ -236,10 +268,9 @@ inline void Timestamp::set_nanos(::google::protobuf::int32 value) { // @@protoc_insertion_point(namespace_scope) -} // namespace protobuf -} // namespace google +PROTOBUF_NAMESPACE_CLOSE // @@protoc_insertion_point(global_scope) #include -#endif // PROTOBUF_INCLUDED_google_2fprotobuf_2ftimestamp_2eproto +#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2ftimestamp_2eproto diff --git a/src/google/protobuf/timestamp.proto b/src/google/protobuf/timestamp.proto index eafb3fa03a..cd357864a9 100644 --- a/src/google/protobuf/timestamp.proto +++ b/src/google/protobuf/timestamp.proto @@ -40,17 +40,19 @@ option java_outer_classname = "TimestampProto"; option java_multiple_files = true; option objc_class_prefix = "GPB"; -// A Timestamp represents a point in time independent of any time zone -// or calendar, represented as seconds and fractions of seconds at -// nanosecond resolution in UTC Epoch time. It is encoded using the -// Proleptic Gregorian Calendar which extends the Gregorian calendar -// backwards to year one. It is encoded assuming all minutes are 60 -// seconds long, i.e. leap seconds are "smeared" so that no leap second -// table is needed for interpretation. Range is from -// 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. -// By restricting to that range, we ensure that we can convert to -// and from RFC 3339 date strings. -// See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt). +// A Timestamp represents a point in time independent of any time zone or local +// calendar, encoded as a count of seconds and fractions of seconds at +// nanosecond resolution. The count is relative to an epoch at UTC midnight on +// January 1, 1970, in the proleptic Gregorian calendar which extends the +// Gregorian calendar backwards to year one. +// +// All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap +// second table is needed for interpretation, using a [24-hour linear +// smear](https://developers.google.com/time/smear). +// +// The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By +// restricting to that range, we ensure that we can convert to and from [RFC +// 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings. // // # Examples // @@ -111,17 +113,18 @@ option objc_class_prefix = "GPB"; // 01:30 UTC on January 15, 2017. // // In JavaScript, one can convert a Date object to this format using the -// standard [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString] +// standard +// [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) // method. In Python, a standard `datetime.datetime` object can be converted -// to this format using [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) -// with the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one -// can use the Joda Time's [`ISODateTimeFormat.dateTime()`]( -// http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime-- +// to this format using +// [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with +// the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use +// the Joda Time's [`ISODateTimeFormat.dateTime()`]( +// http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D // ) to obtain a formatter capable of generating timestamps in this format. // // message Timestamp { - // Represents seconds of UTC time since Unix epoch // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to // 9999-12-31T23:59:59Z inclusive. diff --git a/src/google/protobuf/type.pb.cc b/src/google/protobuf/type.pb.cc index de1bd6c9c6..734010d817 100644 --- a/src/google/protobuf/type.pb.cc +++ b/src/google/protobuf/type.pb.cc @@ -5,218 +5,195 @@ #include -#include #include #include -#include +#include #include #include #include #include -// This is a temporary google only hack -#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS -#include "third_party/protobuf/version.h" -#endif // @@protoc_insertion_point(includes) #include - -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fany_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_Any_google_2fprotobuf_2fany_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fsource_5fcontext_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftype_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_EnumValue_google_2fprotobuf_2ftype_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftype_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_Field_google_2fprotobuf_2ftype_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftype_2eproto ::google::protobuf::internal::SCCInfo<1> scc_info_Option_google_2fprotobuf_2ftype_2eproto; -namespace google { -namespace protobuf { +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fany_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Any_google_2fprotobuf_2fany_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftype_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_EnumValue_google_2fprotobuf_2ftype_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftype_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_Field_google_2fprotobuf_2ftype_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2ftype_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_Option_google_2fprotobuf_2ftype_2eproto; +extern PROTOBUF_INTERNAL_EXPORT_google_2fprotobuf_2fsource_5fcontext_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto; +PROTOBUF_NAMESPACE_OPEN class TypeDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Type_default_instance_; class FieldDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Field_default_instance_; class EnumDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _Enum_default_instance_; class EnumValueDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed _instance; + ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; } _EnumValue_default_instance_; class OptionDefaultTypeInternal { public: - ::google::protobuf::internal::ExplicitlyConstructed